blob: ce22fb49f2a96f1d0022135288f57517a676c6d7 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass5a9390b2015-06-23 15:38:46 -06002/*
3 * Copyright (c) 2015 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
Simon Glass5a9390b2015-06-23 15:38:46 -06005 */
6
Simon Glass5a9390b2015-06-23 15:38:46 -06007#include <dm.h>
8#include <errno.h>
9#include <led.h>
Simon Glass0f2af882020-05-10 11:40:05 -060010#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070011#include <malloc.h>
Simon Glass5a9390b2015-06-23 15:38:46 -060012#include <asm/gpio.h>
Simon Glass5a9390b2015-06-23 15:38:46 -060013
Simon Glass5a9390b2015-06-23 15:38:46 -060014struct led_gpio_priv {
15 struct gpio_desc gpio;
16};
17
Simon Glass6ca19772017-04-10 11:34:54 -060018static int gpio_led_set_state(struct udevice *dev, enum led_state_t state)
Simon Glass5a9390b2015-06-23 15:38:46 -060019{
20 struct led_gpio_priv *priv = dev_get_priv(dev);
Simon Glasse445dfd2017-04-10 11:34:56 -060021 int ret;
Simon Glass5a9390b2015-06-23 15:38:46 -060022
23 if (!dm_gpio_is_valid(&priv->gpio))
24 return -EREMOTEIO;
Simon Glassdc531662017-04-10 11:34:55 -060025 switch (state) {
26 case LEDST_OFF:
27 case LEDST_ON:
28 break;
Simon Glasse445dfd2017-04-10 11:34:56 -060029 case LEDST_TOGGLE:
30 ret = dm_gpio_get_value(&priv->gpio);
31 if (ret < 0)
32 return ret;
33 state = !ret;
34 break;
Simon Glassdc531662017-04-10 11:34:55 -060035 default:
36 return -ENOSYS;
37 }
Simon Glass5a9390b2015-06-23 15:38:46 -060038
Simon Glass6ca19772017-04-10 11:34:54 -060039 return dm_gpio_set_value(&priv->gpio, state);
Simon Glass5a9390b2015-06-23 15:38:46 -060040}
41
Simon Glassdc531662017-04-10 11:34:55 -060042static enum led_state_t gpio_led_get_state(struct udevice *dev)
43{
44 struct led_gpio_priv *priv = dev_get_priv(dev);
45 int ret;
46
47 if (!dm_gpio_is_valid(&priv->gpio))
48 return -EREMOTEIO;
49 ret = dm_gpio_get_value(&priv->gpio);
50 if (ret < 0)
51 return ret;
52
53 return ret ? LEDST_ON : LEDST_OFF;
54}
55
Simon Glass5a9390b2015-06-23 15:38:46 -060056static int led_gpio_probe(struct udevice *dev)
57{
Simon Glass5a9390b2015-06-23 15:38:46 -060058 struct led_gpio_priv *priv = dev_get_priv(dev);
Patrick Bruenn10ab0232018-04-11 11:16:27 +020059
Marek Vasutf3baefa2022-04-22 15:34:00 +020060 return gpio_request_by_name(dev, "gpios", 0, &priv->gpio, GPIOD_IS_OUT);
Simon Glass5a9390b2015-06-23 15:38:46 -060061}
62
63static int led_gpio_remove(struct udevice *dev)
64{
Simon Glassd783eb32015-07-06 12:54:34 -060065 /*
66 * The GPIO driver may have already been removed. We will need to
67 * address this more generally.
68 */
69#ifndef CONFIG_SANDBOX
Simon Glass5a9390b2015-06-23 15:38:46 -060070 struct led_gpio_priv *priv = dev_get_priv(dev);
71
72 if (dm_gpio_is_valid(&priv->gpio))
73 dm_gpio_free(dev, &priv->gpio);
Simon Glassd783eb32015-07-06 12:54:34 -060074#endif
Simon Glass5a9390b2015-06-23 15:38:46 -060075
76 return 0;
77}
78
79static int led_gpio_bind(struct udevice *parent)
80{
Rasmus Villemoese2828a42023-11-17 12:38:09 +010081 return led_bind_generic(parent, "gpio_led");
Simon Glass5a9390b2015-06-23 15:38:46 -060082}
83
84static const struct led_ops gpio_led_ops = {
Simon Glass6ca19772017-04-10 11:34:54 -060085 .set_state = gpio_led_set_state,
Simon Glassdc531662017-04-10 11:34:55 -060086 .get_state = gpio_led_get_state,
Simon Glass5a9390b2015-06-23 15:38:46 -060087};
88
Simon Glass5a9390b2015-06-23 15:38:46 -060089U_BOOT_DRIVER(led_gpio) = {
90 .name = "gpio_led",
91 .id = UCLASS_LED,
Simon Glass5a9390b2015-06-23 15:38:46 -060092 .ops = &gpio_led_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -070093 .priv_auto = sizeof(struct led_gpio_priv),
Simon Glass5a9390b2015-06-23 15:38:46 -060094 .probe = led_gpio_probe,
95 .remove = led_gpio_remove,
96};
Marek Vasutf3baefa2022-04-22 15:34:00 +020097
98static const struct udevice_id led_gpio_ids[] = {
99 { .compatible = "gpio-leds" },
100 { }
101};
102
103U_BOOT_DRIVER(led_gpio_wrap) = {
104 .name = "gpio_led_wrap",
105 .id = UCLASS_NOP,
106 .of_match = led_gpio_ids,
107 .bind = led_gpio_bind,
108};