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 | |
| 8 | #include <common.h> |
| 9 | #include <status_led.h> |
| 10 | #include <asm/gpio.h> |
| 11 | |
Igor Grinberg | 203bd9f | 2013-11-08 01:03:52 +0200 | [diff] [blame] | 12 | #ifndef CONFIG_GPIO_LED_INVERTED_TABLE |
| 13 | #define CONFIG_GPIO_LED_INVERTED_TABLE {} |
| 14 | #endif |
| 15 | |
| 16 | static led_id_t gpio_led_inv[] = CONFIG_GPIO_LED_INVERTED_TABLE; |
| 17 | |
| 18 | static int gpio_led_gpio_value(led_id_t mask, int state) |
| 19 | { |
| 20 | int i, gpio_value = (state == STATUS_LED_ON); |
| 21 | |
| 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 Chou | 836fcc5 | 2010-04-30 11:34:14 +0800 | [diff] [blame] | 30 | void __led_init(led_id_t mask, int state) |
| 31 | { |
Igor Grinberg | 203bd9f | 2013-11-08 01:03:52 +0200 | [diff] [blame] | 32 | int gpio_value; |
| 33 | |
Igor Grinberg | 80dfc42 | 2013-11-08 01:03:51 +0200 | [diff] [blame] | 34 | if (gpio_request(mask, "gpio_led") != 0) { |
| 35 | printf("%s: failed requesting GPIO%lu!\n", __func__, mask); |
| 36 | return; |
| 37 | } |
| 38 | |
Igor Grinberg | 203bd9f | 2013-11-08 01:03:52 +0200 | [diff] [blame] | 39 | gpio_value = gpio_led_gpio_value(mask, state); |
| 40 | gpio_direction_output(mask, gpio_value); |
Thomas Chou | 836fcc5 | 2010-04-30 11:34:14 +0800 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | void __led_set(led_id_t mask, int state) |
| 44 | { |
Igor Grinberg | 203bd9f | 2013-11-08 01:03:52 +0200 | [diff] [blame] | 45 | int gpio_value = gpio_led_gpio_value(mask, state); |
| 46 | |
| 47 | gpio_set_value(mask, gpio_value); |
Thomas Chou | 836fcc5 | 2010-04-30 11:34:14 +0800 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | void __led_toggle(led_id_t mask) |
| 51 | { |
| 52 | gpio_set_value(mask, !gpio_get_value(mask)); |
| 53 | } |
Bernhard Nortmann | 191be39 | 2015-08-21 15:13:20 +0200 | [diff] [blame] | 54 | |
| 55 | #ifdef CONFIG_GPIO_LED_STUBS |
| 56 | |
| 57 | /* 'generic' override of colored LED stubs, to use GPIO functions instead */ |
| 58 | |
| 59 | #ifdef STATUS_LED_RED |
| 60 | void red_led_on(void) |
| 61 | { |
| 62 | __led_set(STATUS_LED_RED, STATUS_LED_ON); |
| 63 | } |
| 64 | |
| 65 | void red_led_off(void) |
| 66 | { |
| 67 | __led_set(STATUS_LED_RED, STATUS_LED_OFF); |
| 68 | } |
| 69 | #endif |
| 70 | |
| 71 | #ifdef STATUS_LED_GREEN |
| 72 | void green_led_on(void) |
| 73 | { |
| 74 | __led_set(STATUS_LED_GREEN, STATUS_LED_ON); |
| 75 | } |
| 76 | |
| 77 | void green_led_off(void) |
| 78 | { |
| 79 | __led_set(STATUS_LED_GREEN, STATUS_LED_OFF); |
| 80 | } |
| 81 | #endif |
| 82 | |
| 83 | #ifdef STATUS_LED_YELLOW |
| 84 | void yellow_led_on(void) |
| 85 | { |
| 86 | __led_set(STATUS_LED_YELLOW, STATUS_LED_ON); |
| 87 | } |
| 88 | |
| 89 | void yellow_led_off(void) |
| 90 | { |
| 91 | __led_set(STATUS_LED_YELLOW, STATUS_LED_OFF); |
| 92 | } |
| 93 | #endif |
| 94 | |
| 95 | #ifdef STATUS_LED_BLUE |
| 96 | void blue_led_on(void) |
| 97 | { |
| 98 | __led_set(STATUS_LED_BLUE, STATUS_LED_ON); |
| 99 | } |
| 100 | |
| 101 | void blue_led_off(void) |
| 102 | { |
| 103 | __led_set(STATUS_LED_BLUE, STATUS_LED_OFF); |
| 104 | } |
| 105 | #endif |
| 106 | |
| 107 | #endif /* CONFIG_GPIO_LED_STUBS */ |