blob: 93f6b913c6477de33a82387f8c24c95bd2faa53f [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
7#include <common.h>
8#include <dm.h>
9#include <errno.h>
10#include <led.h>
11#include <asm/gpio.h>
12#include <dm/lists.h>
13
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 Glassda01de92017-04-10 11:34:53 -060058 struct led_uc_plat *uc_plat = dev_get_uclass_platdata(dev);
Simon Glass5a9390b2015-06-23 15:38:46 -060059 struct led_gpio_priv *priv = dev_get_priv(dev);
Patrick Bruenn10ab0232018-04-11 11:16:27 +020060 int ret;
Simon Glass5a9390b2015-06-23 15:38:46 -060061
62 /* Ignore the top-level LED node */
63 if (!uc_plat->label)
64 return 0;
Patrick Bruenn10ab0232018-04-11 11:16:27 +020065
66 ret = gpio_request_by_name(dev, "gpios", 0, &priv->gpio, GPIOD_IS_OUT);
67 if (ret)
68 return ret;
69
Patrick Bruenn10ab0232018-04-11 11:16:27 +020070 return 0;
Simon Glass5a9390b2015-06-23 15:38:46 -060071}
72
73static int led_gpio_remove(struct udevice *dev)
74{
Simon Glassd783eb32015-07-06 12:54:34 -060075 /*
76 * The GPIO driver may have already been removed. We will need to
77 * address this more generally.
78 */
79#ifndef CONFIG_SANDBOX
Simon Glass5a9390b2015-06-23 15:38:46 -060080 struct led_gpio_priv *priv = dev_get_priv(dev);
81
82 if (dm_gpio_is_valid(&priv->gpio))
83 dm_gpio_free(dev, &priv->gpio);
Simon Glassd783eb32015-07-06 12:54:34 -060084#endif
Simon Glass5a9390b2015-06-23 15:38:46 -060085
86 return 0;
87}
88
89static int led_gpio_bind(struct udevice *parent)
90{
Simon Glass5a9390b2015-06-23 15:38:46 -060091 struct udevice *dev;
Simon Glasse6dd8da2017-05-18 20:09:07 -060092 ofnode node;
Simon Glass5a9390b2015-06-23 15:38:46 -060093 int ret;
94
Simon Glasse6dd8da2017-05-18 20:09:07 -060095 dev_for_each_subnode(node, parent) {
Simon Glassda01de92017-04-10 11:34:53 -060096 struct led_uc_plat *uc_plat;
Simon Glass5a9390b2015-06-23 15:38:46 -060097 const char *label;
98
Simon Glasse6dd8da2017-05-18 20:09:07 -060099 label = ofnode_read_string(node, "label");
Simon Glass5a9390b2015-06-23 15:38:46 -0600100 if (!label) {
101 debug("%s: node %s has no label\n", __func__,
Simon Glasse6dd8da2017-05-18 20:09:07 -0600102 ofnode_get_name(node));
Simon Glass5a9390b2015-06-23 15:38:46 -0600103 return -EINVAL;
104 }
105 ret = device_bind_driver_to_node(parent, "gpio_led",
Simon Glasse6dd8da2017-05-18 20:09:07 -0600106 ofnode_get_name(node),
Simon Glass5a9390b2015-06-23 15:38:46 -0600107 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 Glass6ca19772017-04-10 11:34:54 -0600118 .set_state = gpio_led_set_state,
Simon Glassdc531662017-04-10 11:34:55 -0600119 .get_state = gpio_led_get_state,
Simon Glass5a9390b2015-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};