blob: a36942b9348bdad938f8478cd91be17931dab499 [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>
Patrick Bruenn4fc67a32018-04-11 11:16:28 +020013#include <dm/uclass-internal.h>
Simon Glass5a9390b2015-06-23 15:38:46 -060014
Simon Glass5a9390b2015-06-23 15:38:46 -060015struct led_gpio_priv {
16 struct gpio_desc gpio;
17};
18
Simon Glass6ca19772017-04-10 11:34:54 -060019static int gpio_led_set_state(struct udevice *dev, enum led_state_t state)
Simon Glass5a9390b2015-06-23 15:38:46 -060020{
21 struct led_gpio_priv *priv = dev_get_priv(dev);
Simon Glasse445dfd2017-04-10 11:34:56 -060022 int ret;
Simon Glass5a9390b2015-06-23 15:38:46 -060023
24 if (!dm_gpio_is_valid(&priv->gpio))
25 return -EREMOTEIO;
Simon Glassdc531662017-04-10 11:34:55 -060026 switch (state) {
27 case LEDST_OFF:
28 case LEDST_ON:
29 break;
Simon Glasse445dfd2017-04-10 11:34:56 -060030 case LEDST_TOGGLE:
31 ret = dm_gpio_get_value(&priv->gpio);
32 if (ret < 0)
33 return ret;
34 state = !ret;
35 break;
Simon Glassdc531662017-04-10 11:34:55 -060036 default:
37 return -ENOSYS;
38 }
Simon Glass5a9390b2015-06-23 15:38:46 -060039
Simon Glass6ca19772017-04-10 11:34:54 -060040 return dm_gpio_set_value(&priv->gpio, state);
Simon Glass5a9390b2015-06-23 15:38:46 -060041}
42
Simon Glassdc531662017-04-10 11:34:55 -060043static enum led_state_t gpio_led_get_state(struct udevice *dev)
44{
45 struct led_gpio_priv *priv = dev_get_priv(dev);
46 int ret;
47
48 if (!dm_gpio_is_valid(&priv->gpio))
49 return -EREMOTEIO;
50 ret = dm_gpio_get_value(&priv->gpio);
51 if (ret < 0)
52 return ret;
53
54 return ret ? LEDST_ON : LEDST_OFF;
55}
56
Simon Glass5a9390b2015-06-23 15:38:46 -060057static int led_gpio_probe(struct udevice *dev)
58{
Simon Glassda01de92017-04-10 11:34:53 -060059 struct led_uc_plat *uc_plat = dev_get_uclass_platdata(dev);
Simon Glass5a9390b2015-06-23 15:38:46 -060060 struct led_gpio_priv *priv = dev_get_priv(dev);
Patrick Bruenn10ab0232018-04-11 11:16:27 +020061 const char *default_state;
62 int ret;
Simon Glass5a9390b2015-06-23 15:38:46 -060063
64 /* Ignore the top-level LED node */
65 if (!uc_plat->label)
66 return 0;
Patrick Bruenn10ab0232018-04-11 11:16:27 +020067
68 ret = gpio_request_by_name(dev, "gpios", 0, &priv->gpio, GPIOD_IS_OUT);
69 if (ret)
70 return ret;
71
72 default_state = dev_read_string(dev, "default-state");
73 if (default_state) {
74 if (!strncmp(default_state, "on", 2))
75 gpio_led_set_state(dev, LEDST_ON);
76 else if (!strncmp(default_state, "off", 3))
77 gpio_led_set_state(dev, LEDST_OFF);
78 }
79 return 0;
Simon Glass5a9390b2015-06-23 15:38:46 -060080}
81
82static int led_gpio_remove(struct udevice *dev)
83{
Simon Glassd783eb32015-07-06 12:54:34 -060084 /*
85 * The GPIO driver may have already been removed. We will need to
86 * address this more generally.
87 */
88#ifndef CONFIG_SANDBOX
Simon Glass5a9390b2015-06-23 15:38:46 -060089 struct led_gpio_priv *priv = dev_get_priv(dev);
90
91 if (dm_gpio_is_valid(&priv->gpio))
92 dm_gpio_free(dev, &priv->gpio);
Simon Glassd783eb32015-07-06 12:54:34 -060093#endif
Simon Glass5a9390b2015-06-23 15:38:46 -060094
95 return 0;
96}
97
98static int led_gpio_bind(struct udevice *parent)
99{
Simon Glass5a9390b2015-06-23 15:38:46 -0600100 struct udevice *dev;
Simon Glasse6dd8da2017-05-18 20:09:07 -0600101 ofnode node;
Simon Glass5a9390b2015-06-23 15:38:46 -0600102 int ret;
103
Simon Glasse6dd8da2017-05-18 20:09:07 -0600104 dev_for_each_subnode(node, parent) {
Simon Glassda01de92017-04-10 11:34:53 -0600105 struct led_uc_plat *uc_plat;
Simon Glass5a9390b2015-06-23 15:38:46 -0600106 const char *label;
107
Simon Glasse6dd8da2017-05-18 20:09:07 -0600108 label = ofnode_read_string(node, "label");
Simon Glass5a9390b2015-06-23 15:38:46 -0600109 if (!label) {
110 debug("%s: node %s has no label\n", __func__,
Simon Glasse6dd8da2017-05-18 20:09:07 -0600111 ofnode_get_name(node));
Simon Glass5a9390b2015-06-23 15:38:46 -0600112 return -EINVAL;
113 }
114 ret = device_bind_driver_to_node(parent, "gpio_led",
Simon Glasse6dd8da2017-05-18 20:09:07 -0600115 ofnode_get_name(node),
Simon Glass5a9390b2015-06-23 15:38:46 -0600116 node, &dev);
117 if (ret)
118 return ret;
119 uc_plat = dev_get_uclass_platdata(dev);
120 uc_plat->label = label;
Patrick Bruenn4fc67a32018-04-11 11:16:28 +0200121
122 if (ofnode_read_bool(node, "default-state")) {
123 struct udevice *devp;
124
125 ret = uclass_get_device_tail(dev, 0, &devp);
126 if (ret)
127 return ret;
128 }
Simon Glass5a9390b2015-06-23 15:38:46 -0600129 }
130
131 return 0;
132}
133
134static const struct led_ops gpio_led_ops = {
Simon Glass6ca19772017-04-10 11:34:54 -0600135 .set_state = gpio_led_set_state,
Simon Glassdc531662017-04-10 11:34:55 -0600136 .get_state = gpio_led_get_state,
Simon Glass5a9390b2015-06-23 15:38:46 -0600137};
138
139static const struct udevice_id led_gpio_ids[] = {
140 { .compatible = "gpio-leds" },
141 { }
142};
143
144U_BOOT_DRIVER(led_gpio) = {
145 .name = "gpio_led",
146 .id = UCLASS_LED,
147 .of_match = led_gpio_ids,
148 .ops = &gpio_led_ops,
149 .priv_auto_alloc_size = sizeof(struct led_gpio_priv),
150 .bind = led_gpio_bind,
151 .probe = led_gpio_probe,
152 .remove = led_gpio_remove,
153};