Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 5917112 | 2015-06-23 15:38:45 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2015 Google, Inc |
| 4 | * Written by Simon Glass <sjg@chromium.org> |
Simon Glass | 5917112 | 2015-06-23 15:38:45 -0600 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
| 8 | #include <dm.h> |
| 9 | #include <errno.h> |
| 10 | #include <led.h> |
| 11 | #include <dm/root.h> |
| 12 | #include <dm/uclass-internal.h> |
| 13 | |
| 14 | int led_get_by_label(const char *label, struct udevice **devp) |
| 15 | { |
| 16 | struct udevice *dev; |
| 17 | struct uclass *uc; |
| 18 | int ret; |
| 19 | |
| 20 | ret = uclass_get(UCLASS_LED, &uc); |
| 21 | if (ret) |
| 22 | return ret; |
| 23 | uclass_foreach_dev(dev, uc) { |
Simon Glass | 56e1987 | 2017-04-10 11:34:53 -0600 | [diff] [blame] | 24 | struct led_uc_plat *uc_plat = dev_get_uclass_platdata(dev); |
Simon Glass | 5917112 | 2015-06-23 15:38:45 -0600 | [diff] [blame] | 25 | |
Simon Glass | fb8a5ff | 2015-07-06 12:54:33 -0600 | [diff] [blame] | 26 | /* Ignore the top-level LED node */ |
| 27 | if (uc_plat->label && !strcmp(label, uc_plat->label)) |
Simon Glass | 5917112 | 2015-06-23 15:38:45 -0600 | [diff] [blame] | 28 | return uclass_get_device_tail(dev, 0, devp); |
| 29 | } |
| 30 | |
Simon Glass | fb8a5ff | 2015-07-06 12:54:33 -0600 | [diff] [blame] | 31 | return -ENODEV; |
Simon Glass | 5917112 | 2015-06-23 15:38:45 -0600 | [diff] [blame] | 32 | } |
| 33 | |
Simon Glass | ddae9fc | 2017-04-10 11:34:54 -0600 | [diff] [blame] | 34 | int led_set_state(struct udevice *dev, enum led_state_t state) |
Simon Glass | 5917112 | 2015-06-23 15:38:45 -0600 | [diff] [blame] | 35 | { |
| 36 | struct led_ops *ops = led_get_ops(dev); |
| 37 | |
Simon Glass | ddae9fc | 2017-04-10 11:34:54 -0600 | [diff] [blame] | 38 | if (!ops->set_state) |
Simon Glass | 5917112 | 2015-06-23 15:38:45 -0600 | [diff] [blame] | 39 | return -ENOSYS; |
| 40 | |
Simon Glass | ddae9fc | 2017-04-10 11:34:54 -0600 | [diff] [blame] | 41 | return ops->set_state(dev, state); |
Simon Glass | 5917112 | 2015-06-23 15:38:45 -0600 | [diff] [blame] | 42 | } |
| 43 | |
Simon Glass | 8f4b612 | 2017-04-10 11:34:55 -0600 | [diff] [blame] | 44 | enum led_state_t led_get_state(struct udevice *dev) |
| 45 | { |
| 46 | struct led_ops *ops = led_get_ops(dev); |
| 47 | |
| 48 | if (!ops->get_state) |
| 49 | return -ENOSYS; |
| 50 | |
| 51 | return ops->get_state(dev); |
| 52 | } |
| 53 | |
Simon Glass | 53378da | 2017-04-10 11:34:57 -0600 | [diff] [blame] | 54 | #ifdef CONFIG_LED_BLINK |
| 55 | int led_set_period(struct udevice *dev, int period_ms) |
| 56 | { |
| 57 | struct led_ops *ops = led_get_ops(dev); |
| 58 | |
| 59 | if (!ops->set_period) |
| 60 | return -ENOSYS; |
| 61 | |
| 62 | return ops->set_period(dev, period_ms); |
| 63 | } |
| 64 | #endif |
| 65 | |
Simon Glass | 5917112 | 2015-06-23 15:38:45 -0600 | [diff] [blame] | 66 | UCLASS_DRIVER(led) = { |
| 67 | .id = UCLASS_LED, |
| 68 | .name = "led", |
Simon Glass | 56e1987 | 2017-04-10 11:34:53 -0600 | [diff] [blame] | 69 | .per_device_platdata_auto_alloc_size = sizeof(struct led_uc_plat), |
Simon Glass | 5917112 | 2015-06-23 15:38:45 -0600 | [diff] [blame] | 70 | }; |