blob: 4106ecb6799b163bd8eec2b6dc560c1ebeb89cec [file] [log] [blame]
Simon Glass5ac76ba2015-06-23 15:38:46 -06001/*
2 * Copyright (c) 2015 Google, Inc
3 * Written by Simon Glass <sjg@chromium.org>
4 *
5 * SPDX-License-Identifier: GPL-2.0+
6 */
7
8#include <common.h>
9#include <dm.h>
10#include <errno.h>
11#include <led.h>
12#include <asm/gpio.h>
13#include <dm/lists.h>
14
15DECLARE_GLOBAL_DATA_PTR;
16
17struct led_gpio_priv {
18 struct gpio_desc gpio;
19};
20
Simon Glassddae9fc2017-04-10 11:34:54 -060021static int gpio_led_set_state(struct udevice *dev, enum led_state_t state)
Simon Glass5ac76ba2015-06-23 15:38:46 -060022{
23 struct led_gpio_priv *priv = dev_get_priv(dev);
Simon Glass9413ad42017-04-10 11:34:56 -060024 int ret;
Simon Glass5ac76ba2015-06-23 15:38:46 -060025
26 if (!dm_gpio_is_valid(&priv->gpio))
27 return -EREMOTEIO;
Simon Glass8f4b6122017-04-10 11:34:55 -060028 switch (state) {
29 case LEDST_OFF:
30 case LEDST_ON:
31 break;
Simon Glass9413ad42017-04-10 11:34:56 -060032 case LEDST_TOGGLE:
33 ret = dm_gpio_get_value(&priv->gpio);
34 if (ret < 0)
35 return ret;
36 state = !ret;
37 break;
Simon Glass8f4b6122017-04-10 11:34:55 -060038 default:
39 return -ENOSYS;
40 }
Simon Glass5ac76ba2015-06-23 15:38:46 -060041
Simon Glassddae9fc2017-04-10 11:34:54 -060042 return dm_gpio_set_value(&priv->gpio, state);
Simon Glass5ac76ba2015-06-23 15:38:46 -060043}
44
Simon Glass8f4b6122017-04-10 11:34:55 -060045static enum led_state_t gpio_led_get_state(struct udevice *dev)
46{
47 struct led_gpio_priv *priv = dev_get_priv(dev);
48 int ret;
49
50 if (!dm_gpio_is_valid(&priv->gpio))
51 return -EREMOTEIO;
52 ret = dm_gpio_get_value(&priv->gpio);
53 if (ret < 0)
54 return ret;
55
56 return ret ? LEDST_ON : LEDST_OFF;
57}
58
Simon Glass5ac76ba2015-06-23 15:38:46 -060059static int led_gpio_probe(struct udevice *dev)
60{
Simon Glass56e19872017-04-10 11:34:53 -060061 struct led_uc_plat *uc_plat = dev_get_uclass_platdata(dev);
Simon Glass5ac76ba2015-06-23 15:38:46 -060062 struct led_gpio_priv *priv = dev_get_priv(dev);
63
64 /* Ignore the top-level LED node */
65 if (!uc_plat->label)
66 return 0;
67 return gpio_request_by_name(dev, "gpios", 0, &priv->gpio, GPIOD_IS_OUT);
68}
69
70static int led_gpio_remove(struct udevice *dev)
71{
Simon Glass3c43fba2015-07-06 12:54:34 -060072 /*
73 * The GPIO driver may have already been removed. We will need to
74 * address this more generally.
75 */
76#ifndef CONFIG_SANDBOX
Simon Glass5ac76ba2015-06-23 15:38:46 -060077 struct led_gpio_priv *priv = dev_get_priv(dev);
78
79 if (dm_gpio_is_valid(&priv->gpio))
80 dm_gpio_free(dev, &priv->gpio);
Simon Glass3c43fba2015-07-06 12:54:34 -060081#endif
Simon Glass5ac76ba2015-06-23 15:38:46 -060082
83 return 0;
84}
85
86static int led_gpio_bind(struct udevice *parent)
87{
88 const void *blob = gd->fdt_blob;
89 struct udevice *dev;
90 int node;
91 int ret;
92
Simon Glasse160f7d2017-01-17 16:52:55 -070093 for (node = fdt_first_subnode(blob, dev_of_offset(parent));
Simon Glass5ac76ba2015-06-23 15:38:46 -060094 node > 0;
95 node = fdt_next_subnode(blob, node)) {
Simon Glass56e19872017-04-10 11:34:53 -060096 struct led_uc_plat *uc_plat;
Simon Glass5ac76ba2015-06-23 15:38:46 -060097 const char *label;
98
99 label = fdt_getprop(blob, node, "label", NULL);
100 if (!label) {
101 debug("%s: node %s has no label\n", __func__,
102 fdt_get_name(blob, node, NULL));
103 return -EINVAL;
104 }
105 ret = device_bind_driver_to_node(parent, "gpio_led",
106 fdt_get_name(blob, node, NULL),
107 node, &dev);
108 if (ret)
109 return ret;
110 uc_plat = dev_get_uclass_platdata(dev);
111 uc_plat->label = label;
112 }
113
114 return 0;
115}
116
117static const struct led_ops gpio_led_ops = {
Simon Glassddae9fc2017-04-10 11:34:54 -0600118 .set_state = gpio_led_set_state,
Simon Glass8f4b6122017-04-10 11:34:55 -0600119 .get_state = gpio_led_get_state,
Simon Glass5ac76ba2015-06-23 15:38:46 -0600120};
121
122static const struct udevice_id led_gpio_ids[] = {
123 { .compatible = "gpio-leds" },
124 { }
125};
126
127U_BOOT_DRIVER(led_gpio) = {
128 .name = "gpio_led",
129 .id = UCLASS_LED,
130 .of_match = led_gpio_ids,
131 .ops = &gpio_led_ops,
132 .priv_auto_alloc_size = sizeof(struct led_gpio_priv),
133 .bind = led_gpio_bind,
134 .probe = led_gpio_probe,
135 .remove = led_gpio_remove,
136};