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