xref: /openbmc/u-boot/board/corscience/tricorder/led.c (revision 83d290c56fab2d38cd1ab4c4cc7099559c1d5046)
1*83d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+
2ad9f072cSAndreas Bießmann /*
3ad9f072cSAndreas Bießmann  * Copyright (c) 2013 Corscience GmbH & Co.KG
4ad9f072cSAndreas Bießmann  * Andreas Bießmann <andreas.biessmann@corscience.de>
5ad9f072cSAndreas Bießmann  */
6ad9f072cSAndreas Bießmann #include <common.h>
7ad9f072cSAndreas Bießmann #include <status_led.h>
8ad9f072cSAndreas Bießmann #include <twl4030.h>
9ad9f072cSAndreas Bießmann #include <asm/arch/cpu.h>
10ad9f072cSAndreas Bießmann #include <asm/io.h>
11ad9f072cSAndreas Bießmann #include <asm/arch/sys_proto.h>
12ad9f072cSAndreas Bießmann #include <asm/gpio.h>
13ad9f072cSAndreas Bießmann 
14ad9f072cSAndreas Bießmann #define TRICORDER_STATUS_LED_YELLOW 42
15ad9f072cSAndreas Bießmann #define TRICORDER_STATUS_LED_GREEN  43
16ad9f072cSAndreas Bießmann 
__led_init(led_id_t mask,int state)17ad9f072cSAndreas Bießmann void __led_init(led_id_t mask, int state)
18ad9f072cSAndreas Bießmann {
19ad9f072cSAndreas Bießmann 	__led_set(mask, state);
20ad9f072cSAndreas Bießmann }
21ad9f072cSAndreas Bießmann 
__led_toggle(led_id_t mask)22ad9f072cSAndreas Bießmann void __led_toggle(led_id_t mask)
23ad9f072cSAndreas Bießmann {
24ad9f072cSAndreas Bießmann 	int toggle_gpio = 0;
252d8d190cSUri Mashiach #ifdef CONFIG_LED_STATUS0
262d8d190cSUri Mashiach 	if (!toggle_gpio && CONFIG_LED_STATUS_BIT & mask)
27ad9f072cSAndreas Bießmann 		toggle_gpio = TRICORDER_STATUS_LED_GREEN;
28ad9f072cSAndreas Bießmann #endif
292d8d190cSUri Mashiach #ifdef CONFIG_LED_STATUS1
302d8d190cSUri Mashiach 	if (!toggle_gpio && CONFIG_LED_STATUS_BIT1 & mask)
31ad9f072cSAndreas Bießmann 		toggle_gpio = TRICORDER_STATUS_LED_YELLOW;
32ad9f072cSAndreas Bießmann #endif
332d8d190cSUri Mashiach #ifdef CONFIG_LED_STATUS2
342d8d190cSUri Mashiach 	if (!toggle_gpio && CONFIG_LED_STATUS_BIT2 & mask) {
35ad9f072cSAndreas Bießmann 		uint8_t val;
36ad9f072cSAndreas Bießmann 		twl4030_i2c_read_u8(TWL4030_CHIP_LED, TWL4030_LED_LEDEN,
37ad9f072cSAndreas Bießmann 				    &val);
38ad9f072cSAndreas Bießmann 		val ^= (TWL4030_LED_LEDEN_LEDAON | TWL4030_LED_LEDEN_LEDAPWM);
39ad9f072cSAndreas Bießmann 		twl4030_i2c_write_u8(TWL4030_CHIP_LED, TWL4030_LED_LEDEN,
40ad9f072cSAndreas Bießmann 				     val);
41ad9f072cSAndreas Bießmann 	}
42ad9f072cSAndreas Bießmann #endif
43ad9f072cSAndreas Bießmann 	if (toggle_gpio) {
44ad9f072cSAndreas Bießmann 		int state;
45ad9f072cSAndreas Bießmann 		gpio_request(toggle_gpio, "");
46ad9f072cSAndreas Bießmann 		state = gpio_get_value(toggle_gpio);
47ad9f072cSAndreas Bießmann 		gpio_set_value(toggle_gpio, !state);
48ad9f072cSAndreas Bießmann 	}
49ad9f072cSAndreas Bießmann }
50ad9f072cSAndreas Bießmann 
__led_set(led_id_t mask,int state)51ad9f072cSAndreas Bießmann void __led_set(led_id_t mask, int state)
52ad9f072cSAndreas Bießmann {
532d8d190cSUri Mashiach #ifdef CONFIG_LED_STATUS0
542d8d190cSUri Mashiach 	if (CONFIG_LED_STATUS_BIT & mask) {
55ad9f072cSAndreas Bießmann 		gpio_request(TRICORDER_STATUS_LED_GREEN, "");
56ad9f072cSAndreas Bießmann 		gpio_direction_output(TRICORDER_STATUS_LED_GREEN, 0);
57ad9f072cSAndreas Bießmann 		gpio_set_value(TRICORDER_STATUS_LED_GREEN, state);
58ad9f072cSAndreas Bießmann 	}
59ad9f072cSAndreas Bießmann #endif
602d8d190cSUri Mashiach #ifdef CONFIG_LED_STATUS1
612d8d190cSUri Mashiach 	if (CONFIG_LED_STATUS_BIT1 & mask) {
62ad9f072cSAndreas Bießmann 		gpio_request(TRICORDER_STATUS_LED_YELLOW, "");
63ad9f072cSAndreas Bießmann 		gpio_direction_output(TRICORDER_STATUS_LED_YELLOW, 0);
64ad9f072cSAndreas Bießmann 		gpio_set_value(TRICORDER_STATUS_LED_YELLOW, state);
65ad9f072cSAndreas Bießmann 	}
66ad9f072cSAndreas Bießmann #endif
672d8d190cSUri Mashiach #ifdef CONFIG_LED_STATUS2
682d8d190cSUri Mashiach 	if (CONFIG_LED_STATUS_BIT2 & mask) {
692d8d190cSUri Mashiach 		if (CONFIG_LED_STATUS_OFF == state)
70ad9f072cSAndreas Bießmann 			twl4030_i2c_write_u8(TWL4030_CHIP_LED,
71ad9f072cSAndreas Bießmann 					     TWL4030_LED_LEDEN, 0);
72ad9f072cSAndreas Bießmann 		else
73ad9f072cSAndreas Bießmann 			twl4030_i2c_write_u8(TWL4030_CHIP_LED,
74ad9f072cSAndreas Bießmann 					     TWL4030_LED_LEDEN,
75ad9f072cSAndreas Bießmann 					     (TWL4030_LED_LEDEN_LEDAON |
76ad9f072cSAndreas Bießmann 					      TWL4030_LED_LEDEN_LEDAPWM));
77ad9f072cSAndreas Bießmann 	}
78ad9f072cSAndreas Bießmann #endif
79ad9f072cSAndreas Bießmann }
80