1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2015 Stefan Roese <sr@denx.de>
10 #ifndef CONFIG_PCA9551_I2C_ADDR
11 #error "CONFIG_PCA9551_I2C_ADDR not defined!"
14 #define PCA9551_REG_INPUT 0x00 /* Input register (read only) */
15 #define PCA9551_REG_PSC0 0x01 /* Frequency prescaler 0 */
16 #define PCA9551_REG_PWM0 0x02 /* PWM0 */
17 #define PCA9551_REG_PSC1 0x03 /* Frequency prescaler 1 */
18 #define PCA9551_REG_PWM1 0x04 /* PWM1 */
19 #define PCA9551_REG_LS0 0x05 /* LED0 to LED3 selector */
20 #define PCA9551_REG_LS1 0x06 /* LED4 to LED7 selector */
22 #define PCA9551_CTRL_AI (1 << 4) /* Auto-increment flag */
24 #define PCA9551_LED_STATE_ON 0x00
25 #define PCA9551_LED_STATE_OFF 0x01
26 #define PCA9551_LED_STATE_BLINK0 0x02
27 #define PCA9551_LED_STATE_BLINK1 0x03
29 struct pca9551_blink_rate {
30 u8 psc; /* Frequency preescaler, see PCA9551_7.pdf p. 6 */
31 u8 pwm; /* Pulse width modulation, see PCA9551_7.pdf p. 6 */
34 static int freq_last = -1;
35 static int mask_last = -1;
36 static int idx_last = -1;
39 static int pca9551_led_get_state(int led, int *state)
45 if (led < 0 || led > 7) {
48 reg = PCA9551_REG_LS0;
51 reg = PCA9551_REG_LS1;
52 shift = (led - 4) << 1;
55 ret = i2c_read(CONFIG_PCA9551_I2C_ADDR, reg, 1, &buf, 1);
59 *state = (buf >> shift) & 0x03;
63 static int pca9551_led_set_state(int led, int state)
69 if (led < 0 || led > 7) {
72 reg = PCA9551_REG_LS0;
75 reg = PCA9551_REG_LS1;
76 shift = (led - 4) << 1;
80 ret = i2c_read(CONFIG_PCA9551_I2C_ADDR, reg, 1, &buf, 1);
84 buf = (buf & ~mask) | ((state & 0x03) << shift);
86 ret = i2c_write(CONFIG_PCA9551_I2C_ADDR, reg, 1, &buf, 1);
93 static int pca9551_led_set_blink_rate(int idx, struct pca9551_blink_rate rate)
100 reg = PCA9551_REG_PSC0;
103 reg = PCA9551_REG_PSC1;
108 reg |= PCA9551_CTRL_AI;
110 ret = i2c_write(CONFIG_PCA9551_I2C_ADDR, reg, 1, (u8 *)&rate, 2);
118 * Functions referenced by cmd_led.c or status_led.c
120 void __led_init(led_id_t id, int state)
124 void __led_set(led_id_t mask, int state)
126 if (state == CONFIG_LED_STATUS_OFF)
127 pca9551_led_set_state(mask, PCA9551_LED_STATE_OFF);
129 pca9551_led_set_state(mask, PCA9551_LED_STATE_ON);
132 void __led_toggle(led_id_t mask)
136 pca9551_led_get_state(mask, &state);
137 pca9551_led_set_state(mask, !state);
140 void __led_blink(led_id_t mask, int freq)
142 struct pca9551_blink_rate rate;
146 if ((freq == freq_last) || (mask == mask_last)) {
150 /* Toggle blink index */
153 mode = PCA9551_LED_STATE_BLINK1;
156 mode = PCA9551_LED_STATE_BLINK0;
165 rate.psc = ((freq * 38) / 1000) - 1;
166 rate.pwm = 128; /* 50% duty cycle */
168 pca9551_led_set_blink_rate(idx, rate);
169 pca9551_led_set_state(mask, mode);