1 /*
2  * Copyright (c) 2013 Corscience GmbH & Co.KG
3  * Andreas Bießmann <andreas.biessmann@corscience.de>
4  *
5  * SPDX-License-Identifier:	GPL-2.0+
6  */
7 #include <common.h>
8 #include <status_led.h>
9 #include <twl4030.h>
10 #include <asm/arch/cpu.h>
11 #include <asm/io.h>
12 #include <asm/arch/sys_proto.h>
13 #include <asm/gpio.h>
14 
15 #define TRICORDER_STATUS_LED_YELLOW 42
16 #define TRICORDER_STATUS_LED_GREEN  43
17 
18 void __led_init(led_id_t mask, int state)
19 {
20 	__led_set(mask, state);
21 }
22 
23 void __led_toggle(led_id_t mask)
24 {
25 	int toggle_gpio = 0;
26 #ifdef CONFIG_LED_STATUS0
27 	if (!toggle_gpio && CONFIG_LED_STATUS_BIT & mask)
28 		toggle_gpio = TRICORDER_STATUS_LED_GREEN;
29 #endif
30 #ifdef CONFIG_LED_STATUS1
31 	if (!toggle_gpio && CONFIG_LED_STATUS_BIT1 & mask)
32 		toggle_gpio = TRICORDER_STATUS_LED_YELLOW;
33 #endif
34 #ifdef CONFIG_LED_STATUS2
35 	if (!toggle_gpio && CONFIG_LED_STATUS_BIT2 & mask) {
36 		uint8_t val;
37 		twl4030_i2c_read_u8(TWL4030_CHIP_LED, TWL4030_LED_LEDEN,
38 				    &val);
39 		val ^= (TWL4030_LED_LEDEN_LEDAON | TWL4030_LED_LEDEN_LEDAPWM);
40 		twl4030_i2c_write_u8(TWL4030_CHIP_LED, TWL4030_LED_LEDEN,
41 				     val);
42 	}
43 #endif
44 	if (toggle_gpio) {
45 		int state;
46 		gpio_request(toggle_gpio, "");
47 		state = gpio_get_value(toggle_gpio);
48 		gpio_set_value(toggle_gpio, !state);
49 	}
50 }
51 
52 void __led_set(led_id_t mask, int state)
53 {
54 #ifdef CONFIG_LED_STATUS0
55 	if (CONFIG_LED_STATUS_BIT & mask) {
56 		gpio_request(TRICORDER_STATUS_LED_GREEN, "");
57 		gpio_direction_output(TRICORDER_STATUS_LED_GREEN, 0);
58 		gpio_set_value(TRICORDER_STATUS_LED_GREEN, state);
59 	}
60 #endif
61 #ifdef CONFIG_LED_STATUS1
62 	if (CONFIG_LED_STATUS_BIT1 & mask) {
63 		gpio_request(TRICORDER_STATUS_LED_YELLOW, "");
64 		gpio_direction_output(TRICORDER_STATUS_LED_YELLOW, 0);
65 		gpio_set_value(TRICORDER_STATUS_LED_YELLOW, state);
66 	}
67 #endif
68 #ifdef CONFIG_LED_STATUS2
69 	if (CONFIG_LED_STATUS_BIT2 & mask) {
70 		if (CONFIG_LED_STATUS_OFF == state)
71 			twl4030_i2c_write_u8(TWL4030_CHIP_LED,
72 					     TWL4030_LED_LEDEN, 0);
73 		else
74 			twl4030_i2c_write_u8(TWL4030_CHIP_LED,
75 					     TWL4030_LED_LEDEN,
76 					     (TWL4030_LED_LEDEN_LEDAON |
77 					      TWL4030_LED_LEDEN_LEDAPWM));
78 	}
79 #endif
80 }
81