13e6b86b5SThomas Chou /* 23e6b86b5SThomas Chou * Status LED driver based on GPIO access conventions of Linux 33e6b86b5SThomas Chou * 43e6b86b5SThomas Chou * Copyright (C) 2010 Thomas Chou <thomas@wytron.com.tw> 5ec3b4981SThomas Chou * Licensed under the GPL-2 or later. 63e6b86b5SThomas Chou */ 73e6b86b5SThomas Chou 83e6b86b5SThomas Chou #include <common.h> 93e6b86b5SThomas Chou #include <status_led.h> 103e6b86b5SThomas Chou #include <asm/gpio.h> 113e6b86b5SThomas Chou 129dfdcdfeSIgor Grinberg #ifndef CONFIG_GPIO_LED_INVERTED_TABLE 139dfdcdfeSIgor Grinberg #define CONFIG_GPIO_LED_INVERTED_TABLE {} 149dfdcdfeSIgor Grinberg #endif 159dfdcdfeSIgor Grinberg 169dfdcdfeSIgor Grinberg static led_id_t gpio_led_inv[] = CONFIG_GPIO_LED_INVERTED_TABLE; 179dfdcdfeSIgor Grinberg 189dfdcdfeSIgor Grinberg static int gpio_led_gpio_value(led_id_t mask, int state) 199dfdcdfeSIgor Grinberg { 209dfdcdfeSIgor Grinberg int i, gpio_value = (state == STATUS_LED_ON); 219dfdcdfeSIgor Grinberg 229dfdcdfeSIgor Grinberg for (i = 0; i < ARRAY_SIZE(gpio_led_inv); i++) { 239dfdcdfeSIgor Grinberg if (gpio_led_inv[i] == mask) 249dfdcdfeSIgor Grinberg gpio_value = !gpio_value; 259dfdcdfeSIgor Grinberg } 269dfdcdfeSIgor Grinberg 279dfdcdfeSIgor Grinberg return gpio_value; 289dfdcdfeSIgor Grinberg } 299dfdcdfeSIgor Grinberg 303e6b86b5SThomas Chou void __led_init(led_id_t mask, int state) 313e6b86b5SThomas Chou { 329dfdcdfeSIgor Grinberg int gpio_value; 339dfdcdfeSIgor Grinberg 346516f81bSIgor Grinberg if (gpio_request(mask, "gpio_led") != 0) { 356516f81bSIgor Grinberg printf("%s: failed requesting GPIO%lu!\n", __func__, mask); 366516f81bSIgor Grinberg return; 376516f81bSIgor Grinberg } 386516f81bSIgor Grinberg 399dfdcdfeSIgor Grinberg gpio_value = gpio_led_gpio_value(mask, state); 409dfdcdfeSIgor Grinberg gpio_direction_output(mask, gpio_value); 413e6b86b5SThomas Chou } 423e6b86b5SThomas Chou 433e6b86b5SThomas Chou void __led_set(led_id_t mask, int state) 443e6b86b5SThomas Chou { 459dfdcdfeSIgor Grinberg int gpio_value = gpio_led_gpio_value(mask, state); 469dfdcdfeSIgor Grinberg 479dfdcdfeSIgor Grinberg gpio_set_value(mask, gpio_value); 483e6b86b5SThomas Chou } 493e6b86b5SThomas Chou 503e6b86b5SThomas Chou void __led_toggle(led_id_t mask) 513e6b86b5SThomas Chou { 523e6b86b5SThomas Chou gpio_set_value(mask, !gpio_get_value(mask)); 533e6b86b5SThomas Chou } 54*d375ebbcSBernhard Nortmann 55*d375ebbcSBernhard Nortmann #ifdef CONFIG_GPIO_LED_STUBS 56*d375ebbcSBernhard Nortmann 57*d375ebbcSBernhard Nortmann /* 'generic' override of colored LED stubs, to use GPIO functions instead */ 58*d375ebbcSBernhard Nortmann 59*d375ebbcSBernhard Nortmann #ifdef STATUS_LED_RED 60*d375ebbcSBernhard Nortmann void red_led_on(void) 61*d375ebbcSBernhard Nortmann { 62*d375ebbcSBernhard Nortmann __led_set(STATUS_LED_RED, STATUS_LED_ON); 63*d375ebbcSBernhard Nortmann } 64*d375ebbcSBernhard Nortmann 65*d375ebbcSBernhard Nortmann void red_led_off(void) 66*d375ebbcSBernhard Nortmann { 67*d375ebbcSBernhard Nortmann __led_set(STATUS_LED_RED, STATUS_LED_OFF); 68*d375ebbcSBernhard Nortmann } 69*d375ebbcSBernhard Nortmann #endif 70*d375ebbcSBernhard Nortmann 71*d375ebbcSBernhard Nortmann #ifdef STATUS_LED_GREEN 72*d375ebbcSBernhard Nortmann void green_led_on(void) 73*d375ebbcSBernhard Nortmann { 74*d375ebbcSBernhard Nortmann __led_set(STATUS_LED_GREEN, STATUS_LED_ON); 75*d375ebbcSBernhard Nortmann } 76*d375ebbcSBernhard Nortmann 77*d375ebbcSBernhard Nortmann void green_led_off(void) 78*d375ebbcSBernhard Nortmann { 79*d375ebbcSBernhard Nortmann __led_set(STATUS_LED_GREEN, STATUS_LED_OFF); 80*d375ebbcSBernhard Nortmann } 81*d375ebbcSBernhard Nortmann #endif 82*d375ebbcSBernhard Nortmann 83*d375ebbcSBernhard Nortmann #ifdef STATUS_LED_YELLOW 84*d375ebbcSBernhard Nortmann void yellow_led_on(void) 85*d375ebbcSBernhard Nortmann { 86*d375ebbcSBernhard Nortmann __led_set(STATUS_LED_YELLOW, STATUS_LED_ON); 87*d375ebbcSBernhard Nortmann } 88*d375ebbcSBernhard Nortmann 89*d375ebbcSBernhard Nortmann void yellow_led_off(void) 90*d375ebbcSBernhard Nortmann { 91*d375ebbcSBernhard Nortmann __led_set(STATUS_LED_YELLOW, STATUS_LED_OFF); 92*d375ebbcSBernhard Nortmann } 93*d375ebbcSBernhard Nortmann #endif 94*d375ebbcSBernhard Nortmann 95*d375ebbcSBernhard Nortmann #ifdef STATUS_LED_BLUE 96*d375ebbcSBernhard Nortmann void blue_led_on(void) 97*d375ebbcSBernhard Nortmann { 98*d375ebbcSBernhard Nortmann __led_set(STATUS_LED_BLUE, STATUS_LED_ON); 99*d375ebbcSBernhard Nortmann } 100*d375ebbcSBernhard Nortmann 101*d375ebbcSBernhard Nortmann void blue_led_off(void) 102*d375ebbcSBernhard Nortmann { 103*d375ebbcSBernhard Nortmann __led_set(STATUS_LED_BLUE, STATUS_LED_OFF); 104*d375ebbcSBernhard Nortmann } 105*d375ebbcSBernhard Nortmann #endif 106*d375ebbcSBernhard Nortmann 107*d375ebbcSBernhard Nortmann #endif /* CONFIG_GPIO_LED_STUBS */ 108