xref: /rk3399_rockchip-uboot/drivers/misc/gpio_led.c (revision 2d8d190c8394b43c0989cdb04a50cb48d4e1f8da)
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 
gpio_led_gpio_value(led_id_t mask,int state)189dfdcdfeSIgor Grinberg static int gpio_led_gpio_value(led_id_t mask, int state)
199dfdcdfeSIgor Grinberg {
20*2d8d190cSUri Mashiach 	int i, gpio_value = (state == CONFIG_LED_STATUS_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 
__led_init(led_id_t mask,int state)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 
__led_set(led_id_t mask,int state)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 
__led_toggle(led_id_t mask)503e6b86b5SThomas Chou void __led_toggle(led_id_t mask)
513e6b86b5SThomas Chou {
523e6b86b5SThomas Chou 	gpio_set_value(mask, !gpio_get_value(mask));
533e6b86b5SThomas Chou }
54d375ebbcSBernhard Nortmann 
55d375ebbcSBernhard Nortmann #ifdef CONFIG_GPIO_LED_STUBS
56d375ebbcSBernhard Nortmann 
57d375ebbcSBernhard Nortmann /* 'generic' override of colored LED stubs, to use GPIO functions instead */
58d375ebbcSBernhard Nortmann 
59*2d8d190cSUri Mashiach #ifdef CONFIG_LED_STATUS_RED
red_led_on(void)60d375ebbcSBernhard Nortmann void red_led_on(void)
61d375ebbcSBernhard Nortmann {
62*2d8d190cSUri Mashiach 	__led_set(CONFIG_LED_STATUS_RED, CONFIG_LED_STATUS_ON);
63d375ebbcSBernhard Nortmann }
64d375ebbcSBernhard Nortmann 
red_led_off(void)65d375ebbcSBernhard Nortmann void red_led_off(void)
66d375ebbcSBernhard Nortmann {
67*2d8d190cSUri Mashiach 	__led_set(CONFIG_LED_STATUS_RED, CONFIG_LED_STATUS_OFF);
68d375ebbcSBernhard Nortmann }
69d375ebbcSBernhard Nortmann #endif
70d375ebbcSBernhard Nortmann 
71*2d8d190cSUri Mashiach #ifdef CONFIG_LED_STATUS_GREEN
green_led_on(void)72d375ebbcSBernhard Nortmann void green_led_on(void)
73d375ebbcSBernhard Nortmann {
74*2d8d190cSUri Mashiach 	__led_set(CONFIG_LED_STATUS_GREEN, CONFIG_LED_STATUS_ON);
75d375ebbcSBernhard Nortmann }
76d375ebbcSBernhard Nortmann 
green_led_off(void)77d375ebbcSBernhard Nortmann void green_led_off(void)
78d375ebbcSBernhard Nortmann {
79*2d8d190cSUri Mashiach 	__led_set(CONFIG_LED_STATUS_GREEN, CONFIG_LED_STATUS_OFF);
80d375ebbcSBernhard Nortmann }
81d375ebbcSBernhard Nortmann #endif
82d375ebbcSBernhard Nortmann 
83*2d8d190cSUri Mashiach #ifdef CONFIG_LED_STATUS_YELLOW
yellow_led_on(void)84d375ebbcSBernhard Nortmann void yellow_led_on(void)
85d375ebbcSBernhard Nortmann {
86*2d8d190cSUri Mashiach 	__led_set(CONFIG_LED_STATUS_YELLOW, CONFIG_LED_STATUS_ON);
87d375ebbcSBernhard Nortmann }
88d375ebbcSBernhard Nortmann 
yellow_led_off(void)89d375ebbcSBernhard Nortmann void yellow_led_off(void)
90d375ebbcSBernhard Nortmann {
91*2d8d190cSUri Mashiach 	__led_set(CONFIG_LED_STATUS_YELLOW, CONFIG_LED_STATUS_OFF);
92d375ebbcSBernhard Nortmann }
93d375ebbcSBernhard Nortmann #endif
94d375ebbcSBernhard Nortmann 
95*2d8d190cSUri Mashiach #ifdef CONFIG_LED_STATUS_BLUE
blue_led_on(void)96d375ebbcSBernhard Nortmann void blue_led_on(void)
97d375ebbcSBernhard Nortmann {
98*2d8d190cSUri Mashiach 	__led_set(CONFIG_LED_STATUS_BLUE, CONFIG_LED_STATUS_ON);
99d375ebbcSBernhard Nortmann }
100d375ebbcSBernhard Nortmann 
blue_led_off(void)101d375ebbcSBernhard Nortmann void blue_led_off(void)
102d375ebbcSBernhard Nortmann {
103*2d8d190cSUri Mashiach 	__led_set(CONFIG_LED_STATUS_BLUE, CONFIG_LED_STATUS_OFF);
104d375ebbcSBernhard Nortmann }
105d375ebbcSBernhard Nortmann #endif
106d375ebbcSBernhard Nortmann 
107d375ebbcSBernhard Nortmann #endif /* CONFIG_GPIO_LED_STUBS */
108