blob: fbed151b5d9345c5885ca6c27ec8aee374b42a4a [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>
Simon Glass0f2af882020-05-10 11:40:05 -060011#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070012#include <malloc.h>
Simon Glass5a9390b2015-06-23 15:38:46 -060013#include <asm/gpio.h>
14#include <dm/lists.h>
15
Simon Glass5a9390b2015-06-23 15:38:46 -060016struct led_gpio_priv {
17 struct gpio_desc gpio;
18};
19
Simon Glass6ca19772017-04-10 11:34:54 -060020static int gpio_led_set_state(struct udevice *dev, enum led_state_t state)
Simon Glass5a9390b2015-06-23 15:38:46 -060021{
22 struct led_gpio_priv *priv = dev_get_priv(dev);
Simon Glasse445dfd2017-04-10 11:34:56 -060023 int ret;
Simon Glass5a9390b2015-06-23 15:38:46 -060024
25 if (!dm_gpio_is_valid(&priv->gpio))
26 return -EREMOTEIO;
Simon Glassdc531662017-04-10 11:34:55 -060027 switch (state) {
28 case LEDST_OFF:
29 case LEDST_ON:
30 break;
Simon Glasse445dfd2017-04-10 11:34:56 -060031 case LEDST_TOGGLE:
32 ret = dm_gpio_get_value(&priv->gpio);
33 if (ret < 0)
34 return ret;
35 state = !ret;
36 break;
Simon Glassdc531662017-04-10 11:34:55 -060037 default:
38 return -ENOSYS;
39 }
Simon Glass5a9390b2015-06-23 15:38:46 -060040
Simon Glass6ca19772017-04-10 11:34:54 -060041 return dm_gpio_set_value(&priv->gpio, state);
Simon Glass5a9390b2015-06-23 15:38:46 -060042}
43
Simon Glassdc531662017-04-10 11:34:55 -060044static enum led_state_t gpio_led_get_state(struct udevice *dev)
45{
46 struct led_gpio_priv *priv = dev_get_priv(dev);
47 int ret;
48
49 if (!dm_gpio_is_valid(&priv->gpio))
50 return -EREMOTEIO;
51 ret = dm_gpio_get_value(&priv->gpio);
52 if (ret < 0)
53 return ret;
54
55 return ret ? LEDST_ON : LEDST_OFF;
56}
57
Simon Glass5a9390b2015-06-23 15:38:46 -060058static int led_gpio_probe(struct udevice *dev)
59{
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
Marek Vasutf3baefa2022-04-22 15:34:00 +020062 return gpio_request_by_name(dev, "gpios", 0, &priv->gpio, GPIOD_IS_OUT);
Simon Glass5a9390b2015-06-23 15:38:46 -060063}
64
65static int led_gpio_remove(struct udevice *dev)
66{
Simon Glassd783eb32015-07-06 12:54:34 -060067 /*
68 * The GPIO driver may have already been removed. We will need to
69 * address this more generally.
70 */
71#ifndef CONFIG_SANDBOX
Simon Glass5a9390b2015-06-23 15:38:46 -060072 struct led_gpio_priv *priv = dev_get_priv(dev);
73
74 if (dm_gpio_is_valid(&priv->gpio))
75 dm_gpio_free(dev, &priv->gpio);
Simon Glassd783eb32015-07-06 12:54:34 -060076#endif
Simon Glass5a9390b2015-06-23 15:38:46 -060077
78 return 0;
79}
80
81static int led_gpio_bind(struct udevice *parent)
82{
Simon Glass5a9390b2015-06-23 15:38:46 -060083 struct udevice *dev;
Simon Glasse6dd8da2017-05-18 20:09:07 -060084 ofnode node;
Simon Glass5a9390b2015-06-23 15:38:46 -060085 int ret;
86
Simon Glasse6dd8da2017-05-18 20:09:07 -060087 dev_for_each_subnode(node, parent) {
Simon Glass5a9390b2015-06-23 15:38:46 -060088 ret = device_bind_driver_to_node(parent, "gpio_led",
Simon Glasse6dd8da2017-05-18 20:09:07 -060089 ofnode_get_name(node),
Simon Glass5a9390b2015-06-23 15:38:46 -060090 node, &dev);
91 if (ret)
92 return ret;
Simon Glass5a9390b2015-06-23 15:38:46 -060093 }
94
95 return 0;
96}
97
98static const struct led_ops gpio_led_ops = {
Simon Glass6ca19772017-04-10 11:34:54 -060099 .set_state = gpio_led_set_state,
Simon Glassdc531662017-04-10 11:34:55 -0600100 .get_state = gpio_led_get_state,
Simon Glass5a9390b2015-06-23 15:38:46 -0600101};
102
Simon Glass5a9390b2015-06-23 15:38:46 -0600103U_BOOT_DRIVER(led_gpio) = {
104 .name = "gpio_led",
105 .id = UCLASS_LED,
Simon Glass5a9390b2015-06-23 15:38:46 -0600106 .ops = &gpio_led_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700107 .priv_auto = sizeof(struct led_gpio_priv),
Simon Glass5a9390b2015-06-23 15:38:46 -0600108 .probe = led_gpio_probe,
109 .remove = led_gpio_remove,
110};
Marek Vasutf3baefa2022-04-22 15:34:00 +0200111
112static const struct udevice_id led_gpio_ids[] = {
113 { .compatible = "gpio-leds" },
114 { }
115};
116
117U_BOOT_DRIVER(led_gpio_wrap) = {
118 .name = "gpio_led_wrap",
119 .id = UCLASS_NOP,
120 .of_match = led_gpio_ids,
121 .bind = led_gpio_bind,
122};