2018-05-06 21:58:06 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2015-06-23 21:38:46 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2015 Google, Inc
|
|
|
|
* Written by Simon Glass <sjg@chromium.org>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
|
|
|
#include <dm.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <led.h>
|
2020-05-10 17:40:05 +00:00
|
|
|
#include <log.h>
|
2020-02-03 14:36:16 +00:00
|
|
|
#include <malloc.h>
|
2015-06-23 21:38:46 +00:00
|
|
|
#include <asm/gpio.h>
|
|
|
|
#include <dm/lists.h>
|
|
|
|
|
|
|
|
struct led_gpio_priv {
|
|
|
|
struct gpio_desc gpio;
|
|
|
|
};
|
|
|
|
|
2017-04-10 17:34:54 +00:00
|
|
|
static int gpio_led_set_state(struct udevice *dev, enum led_state_t state)
|
2015-06-23 21:38:46 +00:00
|
|
|
{
|
|
|
|
struct led_gpio_priv *priv = dev_get_priv(dev);
|
2017-04-10 17:34:56 +00:00
|
|
|
int ret;
|
2015-06-23 21:38:46 +00:00
|
|
|
|
|
|
|
if (!dm_gpio_is_valid(&priv->gpio))
|
|
|
|
return -EREMOTEIO;
|
2017-04-10 17:34:55 +00:00
|
|
|
switch (state) {
|
|
|
|
case LEDST_OFF:
|
|
|
|
case LEDST_ON:
|
|
|
|
break;
|
2017-04-10 17:34:56 +00:00
|
|
|
case LEDST_TOGGLE:
|
|
|
|
ret = dm_gpio_get_value(&priv->gpio);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
state = !ret;
|
|
|
|
break;
|
2017-04-10 17:34:55 +00:00
|
|
|
default:
|
|
|
|
return -ENOSYS;
|
|
|
|
}
|
2015-06-23 21:38:46 +00:00
|
|
|
|
2017-04-10 17:34:54 +00:00
|
|
|
return dm_gpio_set_value(&priv->gpio, state);
|
2015-06-23 21:38:46 +00:00
|
|
|
}
|
|
|
|
|
2017-04-10 17:34:55 +00:00
|
|
|
static enum led_state_t gpio_led_get_state(struct udevice *dev)
|
|
|
|
{
|
|
|
|
struct led_gpio_priv *priv = dev_get_priv(dev);
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!dm_gpio_is_valid(&priv->gpio))
|
|
|
|
return -EREMOTEIO;
|
|
|
|
ret = dm_gpio_get_value(&priv->gpio);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
return ret ? LEDST_ON : LEDST_OFF;
|
|
|
|
}
|
|
|
|
|
2015-06-23 21:38:46 +00:00
|
|
|
static int led_gpio_probe(struct udevice *dev)
|
|
|
|
{
|
|
|
|
struct led_gpio_priv *priv = dev_get_priv(dev);
|
2018-04-11 09:16:27 +00:00
|
|
|
|
2022-04-22 13:34:00 +00:00
|
|
|
return gpio_request_by_name(dev, "gpios", 0, &priv->gpio, GPIOD_IS_OUT);
|
2015-06-23 21:38:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int led_gpio_remove(struct udevice *dev)
|
|
|
|
{
|
2015-07-06 18:54:34 +00:00
|
|
|
/*
|
|
|
|
* The GPIO driver may have already been removed. We will need to
|
|
|
|
* address this more generally.
|
|
|
|
*/
|
|
|
|
#ifndef CONFIG_SANDBOX
|
2015-06-23 21:38:46 +00:00
|
|
|
struct led_gpio_priv *priv = dev_get_priv(dev);
|
|
|
|
|
|
|
|
if (dm_gpio_is_valid(&priv->gpio))
|
|
|
|
dm_gpio_free(dev, &priv->gpio);
|
2015-07-06 18:54:34 +00:00
|
|
|
#endif
|
2015-06-23 21:38:46 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int led_gpio_bind(struct udevice *parent)
|
|
|
|
{
|
|
|
|
struct udevice *dev;
|
2017-05-19 02:09:07 +00:00
|
|
|
ofnode node;
|
2015-06-23 21:38:46 +00:00
|
|
|
int ret;
|
|
|
|
|
2017-05-19 02:09:07 +00:00
|
|
|
dev_for_each_subnode(node, parent) {
|
2015-06-23 21:38:46 +00:00
|
|
|
ret = device_bind_driver_to_node(parent, "gpio_led",
|
2017-05-19 02:09:07 +00:00
|
|
|
ofnode_get_name(node),
|
2015-06-23 21:38:46 +00:00
|
|
|
node, &dev);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct led_ops gpio_led_ops = {
|
2017-04-10 17:34:54 +00:00
|
|
|
.set_state = gpio_led_set_state,
|
2017-04-10 17:34:55 +00:00
|
|
|
.get_state = gpio_led_get_state,
|
2015-06-23 21:38:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
U_BOOT_DRIVER(led_gpio) = {
|
|
|
|
.name = "gpio_led",
|
|
|
|
.id = UCLASS_LED,
|
|
|
|
.ops = &gpio_led_ops,
|
2020-12-03 23:55:17 +00:00
|
|
|
.priv_auto = sizeof(struct led_gpio_priv),
|
2015-06-23 21:38:46 +00:00
|
|
|
.probe = led_gpio_probe,
|
|
|
|
.remove = led_gpio_remove,
|
|
|
|
};
|
2022-04-22 13:34:00 +00:00
|
|
|
|
|
|
|
static const struct udevice_id led_gpio_ids[] = {
|
|
|
|
{ .compatible = "gpio-leds" },
|
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
|
|
|
U_BOOT_DRIVER(led_gpio_wrap) = {
|
|
|
|
.name = "gpio_led_wrap",
|
|
|
|
.id = UCLASS_NOP,
|
|
|
|
.of_match = led_gpio_ids,
|
|
|
|
.bind = led_gpio_bind,
|
|
|
|
};
|