blob: 30679f80cf15539e0cdb7c383ad44602569cd09b [file] [log] [blame]
Thomas Chou3e6b86b2010-04-30 11:34:14 +08001/*
2 * Status LED driver based on GPIO access conventions of Linux
3 *
4 * Copyright (C) 2010 Thomas Chou <thomas@wytron.com.tw>
Thomas Chouec3b4982010-06-09 13:32:46 +08005 * Licensed under the GPL-2 or later.
Thomas Chou3e6b86b2010-04-30 11:34:14 +08006 */
7
8#include <common.h>
9#include <status_led.h>
10#include <asm/gpio.h>
11
Tom Rini6e7df1d2023-01-10 11:19:45 -050012#ifndef CFG_GPIO_LED_INVERTED_TABLE
13#define CFG_GPIO_LED_INVERTED_TABLE {}
Igor Grinberg9dfdcdf2013-11-08 01:03:52 +020014#endif
15
Tom Rini6e7df1d2023-01-10 11:19:45 -050016static led_id_t gpio_led_inv[] = CFG_GPIO_LED_INVERTED_TABLE;
Igor Grinberg9dfdcdf2013-11-08 01:03:52 +020017
18static int gpio_led_gpio_value(led_id_t mask, int state)
19{
Uri Mashiach2d8d1902017-01-19 10:51:45 +020020 int i, gpio_value = (state == CONFIG_LED_STATUS_ON);
Igor Grinberg9dfdcdf2013-11-08 01:03:52 +020021
22 for (i = 0; i < ARRAY_SIZE(gpio_led_inv); i++) {
23 if (gpio_led_inv[i] == mask)
24 gpio_value = !gpio_value;
25 }
26
27 return gpio_value;
28}
29
Thomas Chou3e6b86b2010-04-30 11:34:14 +080030void __led_init(led_id_t mask, int state)
31{
Igor Grinberg9dfdcdf2013-11-08 01:03:52 +020032 int gpio_value;
33
Igor Grinberg6516f812013-11-08 01:03:51 +020034 if (gpio_request(mask, "gpio_led") != 0) {
35 printf("%s: failed requesting GPIO%lu!\n", __func__, mask);
36 return;
37 }
38
Igor Grinberg9dfdcdf2013-11-08 01:03:52 +020039 gpio_value = gpio_led_gpio_value(mask, state);
40 gpio_direction_output(mask, gpio_value);
Thomas Chou3e6b86b2010-04-30 11:34:14 +080041}
42
43void __led_set(led_id_t mask, int state)
44{
Igor Grinberg9dfdcdf2013-11-08 01:03:52 +020045 int gpio_value = gpio_led_gpio_value(mask, state);
46
47 gpio_set_value(mask, gpio_value);
Thomas Chou3e6b86b2010-04-30 11:34:14 +080048}
49
50void __led_toggle(led_id_t mask)
51{
52 gpio_set_value(mask, !gpio_get_value(mask));
53}
Bernhard Nortmannd375ebb2015-08-21 15:13:20 +020054
55#ifdef CONFIG_GPIO_LED_STUBS
56
57/* 'generic' override of colored LED stubs, to use GPIO functions instead */
58
Uri Mashiach2d8d1902017-01-19 10:51:45 +020059#ifdef CONFIG_LED_STATUS_RED
Bernhard Nortmannd375ebb2015-08-21 15:13:20 +020060void red_led_on(void)
61{
Uri Mashiach2d8d1902017-01-19 10:51:45 +020062 __led_set(CONFIG_LED_STATUS_RED, CONFIG_LED_STATUS_ON);
Bernhard Nortmannd375ebb2015-08-21 15:13:20 +020063}
64
65void red_led_off(void)
66{
Uri Mashiach2d8d1902017-01-19 10:51:45 +020067 __led_set(CONFIG_LED_STATUS_RED, CONFIG_LED_STATUS_OFF);
Bernhard Nortmannd375ebb2015-08-21 15:13:20 +020068}
69#endif
70
Uri Mashiach2d8d1902017-01-19 10:51:45 +020071#ifdef CONFIG_LED_STATUS_GREEN
Bernhard Nortmannd375ebb2015-08-21 15:13:20 +020072void green_led_on(void)
73{
Uri Mashiach2d8d1902017-01-19 10:51:45 +020074 __led_set(CONFIG_LED_STATUS_GREEN, CONFIG_LED_STATUS_ON);
Bernhard Nortmannd375ebb2015-08-21 15:13:20 +020075}
76
77void green_led_off(void)
78{
Uri Mashiach2d8d1902017-01-19 10:51:45 +020079 __led_set(CONFIG_LED_STATUS_GREEN, CONFIG_LED_STATUS_OFF);
Bernhard Nortmannd375ebb2015-08-21 15:13:20 +020080}
81#endif
82
Uri Mashiach2d8d1902017-01-19 10:51:45 +020083#ifdef CONFIG_LED_STATUS_YELLOW
Bernhard Nortmannd375ebb2015-08-21 15:13:20 +020084void yellow_led_on(void)
85{
Uri Mashiach2d8d1902017-01-19 10:51:45 +020086 __led_set(CONFIG_LED_STATUS_YELLOW, CONFIG_LED_STATUS_ON);
Bernhard Nortmannd375ebb2015-08-21 15:13:20 +020087}
88
89void yellow_led_off(void)
90{
Uri Mashiach2d8d1902017-01-19 10:51:45 +020091 __led_set(CONFIG_LED_STATUS_YELLOW, CONFIG_LED_STATUS_OFF);
Bernhard Nortmannd375ebb2015-08-21 15:13:20 +020092}
93#endif
94
Uri Mashiach2d8d1902017-01-19 10:51:45 +020095#ifdef CONFIG_LED_STATUS_BLUE
Bernhard Nortmannd375ebb2015-08-21 15:13:20 +020096void blue_led_on(void)
97{
Uri Mashiach2d8d1902017-01-19 10:51:45 +020098 __led_set(CONFIG_LED_STATUS_BLUE, CONFIG_LED_STATUS_ON);
Bernhard Nortmannd375ebb2015-08-21 15:13:20 +020099}
100
101void blue_led_off(void)
102{
Uri Mashiach2d8d1902017-01-19 10:51:45 +0200103 __led_set(CONFIG_LED_STATUS_BLUE, CONFIG_LED_STATUS_OFF);
Bernhard Nortmannd375ebb2015-08-21 15:13:20 +0200104}
105#endif
106
107#endif /* CONFIG_GPIO_LED_STUBS */