Thomas Chou | 836fcc5 | 2010-04-30 11:34:14 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Status LED driver based on GPIO access conventions of Linux |
| 3 | * |
| 4 | * Copyright (C) 2010 Thomas Chou <thomas@wytron.com.tw> |
Thomas Chou | 2658e66 | 2010-06-09 13:32:46 +0800 | [diff] [blame] | 5 | * Licensed under the GPL-2 or later. |
Thomas Chou | 836fcc5 | 2010-04-30 11:34:14 +0800 | [diff] [blame] | 6 | */ |
| 7 | |
Thomas Chou | 836fcc5 | 2010-04-30 11:34:14 +0800 | [diff] [blame] | 8 | #include <status_led.h> |
| 9 | #include <asm/gpio.h> |
| 10 | |
Tom Rini | 364d002 | 2023-01-10 11:19:45 -0500 | [diff] [blame] | 11 | #ifndef CFG_GPIO_LED_INVERTED_TABLE |
| 12 | #define CFG_GPIO_LED_INVERTED_TABLE {} |
Igor Grinberg | 203bd9f | 2013-11-08 01:03:52 +0200 | [diff] [blame] | 13 | #endif |
| 14 | |
Tom Rini | 364d002 | 2023-01-10 11:19:45 -0500 | [diff] [blame] | 15 | static led_id_t gpio_led_inv[] = CFG_GPIO_LED_INVERTED_TABLE; |
Igor Grinberg | 203bd9f | 2013-11-08 01:03:52 +0200 | [diff] [blame] | 16 | |
| 17 | static int gpio_led_gpio_value(led_id_t mask, int state) |
| 18 | { |
Uri Mashiach | 4892d39 | 2017-01-19 10:51:45 +0200 | [diff] [blame] | 19 | int i, gpio_value = (state == CONFIG_LED_STATUS_ON); |
Igor Grinberg | 203bd9f | 2013-11-08 01:03:52 +0200 | [diff] [blame] | 20 | |
| 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 Chou | 836fcc5 | 2010-04-30 11:34:14 +0800 | [diff] [blame] | 29 | void __led_init(led_id_t mask, int state) |
| 30 | { |
Igor Grinberg | 203bd9f | 2013-11-08 01:03:52 +0200 | [diff] [blame] | 31 | int gpio_value; |
| 32 | |
Igor Grinberg | 80dfc42 | 2013-11-08 01:03:51 +0200 | [diff] [blame] | 33 | if (gpio_request(mask, "gpio_led") != 0) { |
| 34 | printf("%s: failed requesting GPIO%lu!\n", __func__, mask); |
| 35 | return; |
| 36 | } |
| 37 | |
Igor Grinberg | 203bd9f | 2013-11-08 01:03:52 +0200 | [diff] [blame] | 38 | gpio_value = gpio_led_gpio_value(mask, state); |
| 39 | gpio_direction_output(mask, gpio_value); |
Thomas Chou | 836fcc5 | 2010-04-30 11:34:14 +0800 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | void __led_set(led_id_t mask, int state) |
| 43 | { |
Igor Grinberg | 203bd9f | 2013-11-08 01:03:52 +0200 | [diff] [blame] | 44 | int gpio_value = gpio_led_gpio_value(mask, state); |
| 45 | |
| 46 | gpio_set_value(mask, gpio_value); |
Thomas Chou | 836fcc5 | 2010-04-30 11:34:14 +0800 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | void __led_toggle(led_id_t mask) |
| 50 | { |
| 51 | gpio_set_value(mask, !gpio_get_value(mask)); |
| 52 | } |
Bernhard Nortmann | 191be39 | 2015-08-21 15:13:20 +0200 | [diff] [blame] | 53 | |
| 54 | #ifdef CONFIG_GPIO_LED_STUBS |
| 55 | |
| 56 | /* 'generic' override of colored LED stubs, to use GPIO functions instead */ |
| 57 | |
Uri Mashiach | 4892d39 | 2017-01-19 10:51:45 +0200 | [diff] [blame] | 58 | #ifdef CONFIG_LED_STATUS_RED |
Bernhard Nortmann | 191be39 | 2015-08-21 15:13:20 +0200 | [diff] [blame] | 59 | void red_led_on(void) |
| 60 | { |
Uri Mashiach | 4892d39 | 2017-01-19 10:51:45 +0200 | [diff] [blame] | 61 | __led_set(CONFIG_LED_STATUS_RED, CONFIG_LED_STATUS_ON); |
Bernhard Nortmann | 191be39 | 2015-08-21 15:13:20 +0200 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | void red_led_off(void) |
| 65 | { |
Uri Mashiach | 4892d39 | 2017-01-19 10:51:45 +0200 | [diff] [blame] | 66 | __led_set(CONFIG_LED_STATUS_RED, CONFIG_LED_STATUS_OFF); |
Bernhard Nortmann | 191be39 | 2015-08-21 15:13:20 +0200 | [diff] [blame] | 67 | } |
| 68 | #endif |
| 69 | |
Uri Mashiach | 4892d39 | 2017-01-19 10:51:45 +0200 | [diff] [blame] | 70 | #ifdef CONFIG_LED_STATUS_GREEN |
Bernhard Nortmann | 191be39 | 2015-08-21 15:13:20 +0200 | [diff] [blame] | 71 | void green_led_on(void) |
| 72 | { |
Uri Mashiach | 4892d39 | 2017-01-19 10:51:45 +0200 | [diff] [blame] | 73 | __led_set(CONFIG_LED_STATUS_GREEN, CONFIG_LED_STATUS_ON); |
Bernhard Nortmann | 191be39 | 2015-08-21 15:13:20 +0200 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | void green_led_off(void) |
| 77 | { |
Uri Mashiach | 4892d39 | 2017-01-19 10:51:45 +0200 | [diff] [blame] | 78 | __led_set(CONFIG_LED_STATUS_GREEN, CONFIG_LED_STATUS_OFF); |
Bernhard Nortmann | 191be39 | 2015-08-21 15:13:20 +0200 | [diff] [blame] | 79 | } |
| 80 | #endif |
| 81 | |
Uri Mashiach | 4892d39 | 2017-01-19 10:51:45 +0200 | [diff] [blame] | 82 | #ifdef CONFIG_LED_STATUS_YELLOW |
Bernhard Nortmann | 191be39 | 2015-08-21 15:13:20 +0200 | [diff] [blame] | 83 | void yellow_led_on(void) |
| 84 | { |
Uri Mashiach | 4892d39 | 2017-01-19 10:51:45 +0200 | [diff] [blame] | 85 | __led_set(CONFIG_LED_STATUS_YELLOW, CONFIG_LED_STATUS_ON); |
Bernhard Nortmann | 191be39 | 2015-08-21 15:13:20 +0200 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | void yellow_led_off(void) |
| 89 | { |
Uri Mashiach | 4892d39 | 2017-01-19 10:51:45 +0200 | [diff] [blame] | 90 | __led_set(CONFIG_LED_STATUS_YELLOW, CONFIG_LED_STATUS_OFF); |
Bernhard Nortmann | 191be39 | 2015-08-21 15:13:20 +0200 | [diff] [blame] | 91 | } |
| 92 | #endif |
| 93 | |
Uri Mashiach | 4892d39 | 2017-01-19 10:51:45 +0200 | [diff] [blame] | 94 | #ifdef CONFIG_LED_STATUS_BLUE |
Bernhard Nortmann | 191be39 | 2015-08-21 15:13:20 +0200 | [diff] [blame] | 95 | void blue_led_on(void) |
| 96 | { |
Uri Mashiach | 4892d39 | 2017-01-19 10:51:45 +0200 | [diff] [blame] | 97 | __led_set(CONFIG_LED_STATUS_BLUE, CONFIG_LED_STATUS_ON); |
Bernhard Nortmann | 191be39 | 2015-08-21 15:13:20 +0200 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | void blue_led_off(void) |
| 101 | { |
Uri Mashiach | 4892d39 | 2017-01-19 10:51:45 +0200 | [diff] [blame] | 102 | __led_set(CONFIG_LED_STATUS_BLUE, CONFIG_LED_STATUS_OFF); |
Bernhard Nortmann | 191be39 | 2015-08-21 15:13:20 +0200 | [diff] [blame] | 103 | } |
| 104 | #endif |
| 105 | |
| 106 | #endif /* CONFIG_GPIO_LED_STUBS */ |