blob: e63689967a7ac0ee0f1bdc7e607bfb728ec11a67 [file] [log] [blame]
Thomas Chou836fcc52010-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 Chou2658e662010-06-09 13:32:46 +08005 * Licensed under the GPL-2 or later.
Thomas Chou836fcc52010-04-30 11:34:14 +08006 */
7
Thomas Chou836fcc52010-04-30 11:34:14 +08008#include <status_led.h>
9#include <asm/gpio.h>
10
Tom Rini364d0022023-01-10 11:19:45 -050011#ifndef CFG_GPIO_LED_INVERTED_TABLE
12#define CFG_GPIO_LED_INVERTED_TABLE {}
Igor Grinberg203bd9f2013-11-08 01:03:52 +020013#endif
14
Tom Rini364d0022023-01-10 11:19:45 -050015static led_id_t gpio_led_inv[] = CFG_GPIO_LED_INVERTED_TABLE;
Igor Grinberg203bd9f2013-11-08 01:03:52 +020016
17static int gpio_led_gpio_value(led_id_t mask, int state)
18{
Uri Mashiach4892d392017-01-19 10:51:45 +020019 int i, gpio_value = (state == CONFIG_LED_STATUS_ON);
Igor Grinberg203bd9f2013-11-08 01:03:52 +020020
21 for (i = 0; i < ARRAY_SIZE(gpio_led_inv); i++) {
22 if (gpio_led_inv[i] == mask)
23 gpio_value = !gpio_value;
24 }
25
26 return gpio_value;
27}
28
Thomas Chou836fcc52010-04-30 11:34:14 +080029void __led_init(led_id_t mask, int state)
30{
Igor Grinberg203bd9f2013-11-08 01:03:52 +020031 int gpio_value;
32
Igor Grinberg80dfc422013-11-08 01:03:51 +020033 if (gpio_request(mask, "gpio_led") != 0) {
34 printf("%s: failed requesting GPIO%lu!\n", __func__, mask);
35 return;
36 }
37
Igor Grinberg203bd9f2013-11-08 01:03:52 +020038 gpio_value = gpio_led_gpio_value(mask, state);
39 gpio_direction_output(mask, gpio_value);
Thomas Chou836fcc52010-04-30 11:34:14 +080040}
41
42void __led_set(led_id_t mask, int state)
43{
Igor Grinberg203bd9f2013-11-08 01:03:52 +020044 int gpio_value = gpio_led_gpio_value(mask, state);
45
46 gpio_set_value(mask, gpio_value);
Thomas Chou836fcc52010-04-30 11:34:14 +080047}
48
49void __led_toggle(led_id_t mask)
50{
51 gpio_set_value(mask, !gpio_get_value(mask));
52}
Bernhard Nortmann191be392015-08-21 15:13:20 +020053
54#ifdef CONFIG_GPIO_LED_STUBS
55
56/* 'generic' override of colored LED stubs, to use GPIO functions instead */
57
Uri Mashiach4892d392017-01-19 10:51:45 +020058#ifdef CONFIG_LED_STATUS_RED
Bernhard Nortmann191be392015-08-21 15:13:20 +020059void red_led_on(void)
60{
Uri Mashiach4892d392017-01-19 10:51:45 +020061 __led_set(CONFIG_LED_STATUS_RED, CONFIG_LED_STATUS_ON);
Bernhard Nortmann191be392015-08-21 15:13:20 +020062}
63
64void red_led_off(void)
65{
Uri Mashiach4892d392017-01-19 10:51:45 +020066 __led_set(CONFIG_LED_STATUS_RED, CONFIG_LED_STATUS_OFF);
Bernhard Nortmann191be392015-08-21 15:13:20 +020067}
68#endif
69
Uri Mashiach4892d392017-01-19 10:51:45 +020070#ifdef CONFIG_LED_STATUS_GREEN
Bernhard Nortmann191be392015-08-21 15:13:20 +020071void green_led_on(void)
72{
Uri Mashiach4892d392017-01-19 10:51:45 +020073 __led_set(CONFIG_LED_STATUS_GREEN, CONFIG_LED_STATUS_ON);
Bernhard Nortmann191be392015-08-21 15:13:20 +020074}
75
76void green_led_off(void)
77{
Uri Mashiach4892d392017-01-19 10:51:45 +020078 __led_set(CONFIG_LED_STATUS_GREEN, CONFIG_LED_STATUS_OFF);
Bernhard Nortmann191be392015-08-21 15:13:20 +020079}
80#endif
81
Uri Mashiach4892d392017-01-19 10:51:45 +020082#ifdef CONFIG_LED_STATUS_YELLOW
Bernhard Nortmann191be392015-08-21 15:13:20 +020083void yellow_led_on(void)
84{
Uri Mashiach4892d392017-01-19 10:51:45 +020085 __led_set(CONFIG_LED_STATUS_YELLOW, CONFIG_LED_STATUS_ON);
Bernhard Nortmann191be392015-08-21 15:13:20 +020086}
87
88void yellow_led_off(void)
89{
Uri Mashiach4892d392017-01-19 10:51:45 +020090 __led_set(CONFIG_LED_STATUS_YELLOW, CONFIG_LED_STATUS_OFF);
Bernhard Nortmann191be392015-08-21 15:13:20 +020091}
92#endif
93
Uri Mashiach4892d392017-01-19 10:51:45 +020094#ifdef CONFIG_LED_STATUS_BLUE
Bernhard Nortmann191be392015-08-21 15:13:20 +020095void blue_led_on(void)
96{
Uri Mashiach4892d392017-01-19 10:51:45 +020097 __led_set(CONFIG_LED_STATUS_BLUE, CONFIG_LED_STATUS_ON);
Bernhard Nortmann191be392015-08-21 15:13:20 +020098}
99
100void blue_led_off(void)
101{
Uri Mashiach4892d392017-01-19 10:51:45 +0200102 __led_set(CONFIG_LED_STATUS_BLUE, CONFIG_LED_STATUS_OFF);
Bernhard Nortmann191be392015-08-21 15:13:20 +0200103}
104#endif
105
106#endif /* CONFIG_GPIO_LED_STUBS */