2 * Copyright (C) 2015 Stefan Roese <sr@denx.de>
4 * SPDX-License-Identifier: GPL-2.0+
11 #ifndef CONFIG_PCA9551_I2C_ADDR
12 #error "CONFIG_PCA9551_I2C_ADDR not defined!"
15 #define PCA9551_REG_INPUT 0x00 /* Input register (read only) */
16 #define PCA9551_REG_PSC0 0x01 /* Frequency prescaler 0 */
17 #define PCA9551_REG_PWM0 0x02 /* PWM0 */
18 #define PCA9551_REG_PSC1 0x03 /* Frequency prescaler 1 */
19 #define PCA9551_REG_PWM1 0x04 /* PWM1 */
20 #define PCA9551_REG_LS0 0x05 /* LED0 to LED3 selector */
21 #define PCA9551_REG_LS1 0x06 /* LED4 to LED7 selector */
23 #define PCA9551_CTRL_AI (1 << 4) /* Auto-increment flag */
25 #define PCA9551_LED_STATE_ON 0x00
26 #define PCA9551_LED_STATE_OFF 0x01
27 #define PCA9551_LED_STATE_BLINK0 0x02
28 #define PCA9551_LED_STATE_BLINK1 0x03
30 struct pca9551_blink_rate {
31 u8 psc; /* Frequency preescaler, see PCA9551_7.pdf p. 6 */
32 u8 pwm; /* Pulse width modulation, see PCA9551_7.pdf p. 6 */
35 static int freq0, freq1;
37 static int pca9551_led_get_state(int led, int *state)
43 if (led < 0 || led > 7) {
46 reg = PCA9551_REG_LS0;
49 reg = PCA9551_REG_LS1;
50 shift = (led - 4) << 1;
53 ret = i2c_read(CONFIG_PCA9551_I2C_ADDR, reg, 1, &buf, 1);
57 *state = (buf >> shift) & 0x03;
61 static int pca9551_led_set_state(int led, int state)
67 if (led < 0 || led > 7) {
70 reg = PCA9551_REG_LS0;
73 reg = PCA9551_REG_LS1;
74 shift = (led - 4) << 1;
78 ret = i2c_read(CONFIG_PCA9551_I2C_ADDR, reg, 1, &buf, 1);
82 buf = (buf & ~mask) | ((state & 0x03) << shift);
84 ret = i2c_write(CONFIG_PCA9551_I2C_ADDR, reg, 1, &buf, 1);
91 static int pca9551_led_set_blink_rate(int idx, struct pca9551_blink_rate rate)
98 reg = PCA9551_REG_PSC0;
101 reg = PCA9551_REG_PSC1;
106 reg |= PCA9551_CTRL_AI;
108 ret = i2c_write(CONFIG_PCA9551_I2C_ADDR, reg, 1, (u8 *)&rate, 2);
116 * Functions referenced by cmd_led.c
118 void __led_set(led_id_t mask, int state)
120 if (state == STATUS_LED_OFF)
121 pca9551_led_set_state(mask, PCA9551_LED_STATE_OFF);
123 pca9551_led_set_state(mask, PCA9551_LED_STATE_ON);
126 void __led_toggle(led_id_t mask)
130 pca9551_led_get_state(mask, &state);
131 pca9551_led_set_state(mask, !state);
134 void __led_blink(led_id_t mask, int freq)
136 struct pca9551_blink_rate rate;
140 if ((freq0 == 0) || (freq == freq0)) {
142 mode = PCA9551_LED_STATE_BLINK0;
146 mode = PCA9551_LED_STATE_BLINK1;
150 rate.psc = ((freq * 38) / 1000) - 1;
151 rate.pwm = 128; /* 50% duty cycle */
153 pca9551_led_set_blink_rate(blink, rate);
154 pca9551_led_set_state(mask, mode);