Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Stefan Roese | 1cdd941 | 2015-03-12 11:22:46 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2015 Stefan Roese <sr@denx.de> |
Stefan Roese | 1cdd941 | 2015-03-12 11:22:46 +0100 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
| 7 | #include <errno.h> |
| 8 | #include <i2c.h> |
| 9 | |
| 10 | #ifndef CONFIG_PCA9551_I2C_ADDR |
| 11 | #error "CONFIG_PCA9551_I2C_ADDR not defined!" |
| 12 | #endif |
| 13 | |
| 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 */ |
| 21 | |
| 22 | #define PCA9551_CTRL_AI (1 << 4) /* Auto-increment flag */ |
| 23 | |
| 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 |
| 28 | |
| 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 */ |
| 32 | }; |
| 33 | |
Stefan Roese | f5df36d | 2015-07-06 13:35:55 +0200 | [diff] [blame] | 34 | static int freq_last = -1; |
| 35 | static int mask_last = -1; |
| 36 | static int idx_last = -1; |
| 37 | static int mode_last; |
Stefan Roese | 1cdd941 | 2015-03-12 11:22:46 +0100 | [diff] [blame] | 38 | |
| 39 | static int pca9551_led_get_state(int led, int *state) |
| 40 | { |
| 41 | unsigned int reg; |
| 42 | u8 shift, buf; |
| 43 | int ret; |
| 44 | |
| 45 | if (led < 0 || led > 7) { |
| 46 | return -EINVAL; |
| 47 | } else if (led < 4) { |
| 48 | reg = PCA9551_REG_LS0; |
| 49 | shift = led << 1; |
| 50 | } else { |
| 51 | reg = PCA9551_REG_LS1; |
| 52 | shift = (led - 4) << 1; |
| 53 | } |
| 54 | |
| 55 | ret = i2c_read(CONFIG_PCA9551_I2C_ADDR, reg, 1, &buf, 1); |
| 56 | if (ret) |
| 57 | return ret; |
| 58 | |
| 59 | *state = (buf >> shift) & 0x03; |
| 60 | return 0; |
| 61 | } |
| 62 | |
| 63 | static int pca9551_led_set_state(int led, int state) |
| 64 | { |
| 65 | unsigned int reg; |
| 66 | u8 shift, buf, mask; |
| 67 | int ret; |
| 68 | |
| 69 | if (led < 0 || led > 7) { |
| 70 | return -EINVAL; |
| 71 | } else if (led < 4) { |
| 72 | reg = PCA9551_REG_LS0; |
| 73 | shift = led << 1; |
| 74 | } else { |
| 75 | reg = PCA9551_REG_LS1; |
| 76 | shift = (led - 4) << 1; |
| 77 | } |
| 78 | mask = 0x03 << shift; |
| 79 | |
| 80 | ret = i2c_read(CONFIG_PCA9551_I2C_ADDR, reg, 1, &buf, 1); |
| 81 | if (ret) |
| 82 | return ret; |
| 83 | |
| 84 | buf = (buf & ~mask) | ((state & 0x03) << shift); |
| 85 | |
| 86 | ret = i2c_write(CONFIG_PCA9551_I2C_ADDR, reg, 1, &buf, 1); |
| 87 | if (ret) |
| 88 | return ret; |
| 89 | |
| 90 | return 0; |
| 91 | } |
| 92 | |
| 93 | static int pca9551_led_set_blink_rate(int idx, struct pca9551_blink_rate rate) |
| 94 | { |
| 95 | unsigned int reg; |
| 96 | int ret; |
| 97 | |
| 98 | switch (idx) { |
| 99 | case 0: |
| 100 | reg = PCA9551_REG_PSC0; |
| 101 | break; |
| 102 | case 1: |
| 103 | reg = PCA9551_REG_PSC1; |
| 104 | break; |
| 105 | default: |
| 106 | return -EINVAL; |
| 107 | } |
| 108 | reg |= PCA9551_CTRL_AI; |
| 109 | |
| 110 | ret = i2c_write(CONFIG_PCA9551_I2C_ADDR, reg, 1, (u8 *)&rate, 2); |
| 111 | if (ret) |
| 112 | return ret; |
| 113 | |
| 114 | return 0; |
| 115 | } |
| 116 | |
| 117 | /* |
Bernhard Nortmann | 13cfbe5 | 2015-08-21 15:13:21 +0200 | [diff] [blame] | 118 | * Functions referenced by cmd_led.c or status_led.c |
Stefan Roese | 1cdd941 | 2015-03-12 11:22:46 +0100 | [diff] [blame] | 119 | */ |
Bernhard Nortmann | 13cfbe5 | 2015-08-21 15:13:21 +0200 | [diff] [blame] | 120 | void __led_init(led_id_t id, int state) |
| 121 | { |
| 122 | } |
| 123 | |
Stefan Roese | 1cdd941 | 2015-03-12 11:22:46 +0100 | [diff] [blame] | 124 | void __led_set(led_id_t mask, int state) |
| 125 | { |
Uri Mashiach | 2d8d190 | 2017-01-19 10:51:45 +0200 | [diff] [blame] | 126 | if (state == CONFIG_LED_STATUS_OFF) |
Stefan Roese | 1cdd941 | 2015-03-12 11:22:46 +0100 | [diff] [blame] | 127 | pca9551_led_set_state(mask, PCA9551_LED_STATE_OFF); |
| 128 | else |
| 129 | pca9551_led_set_state(mask, PCA9551_LED_STATE_ON); |
| 130 | } |
| 131 | |
| 132 | void __led_toggle(led_id_t mask) |
| 133 | { |
| 134 | int state = 0; |
| 135 | |
| 136 | pca9551_led_get_state(mask, &state); |
| 137 | pca9551_led_set_state(mask, !state); |
| 138 | } |
| 139 | |
| 140 | void __led_blink(led_id_t mask, int freq) |
| 141 | { |
| 142 | struct pca9551_blink_rate rate; |
| 143 | int mode; |
Stefan Roese | f5df36d | 2015-07-06 13:35:55 +0200 | [diff] [blame] | 144 | int idx; |
Stefan Roese | 1cdd941 | 2015-03-12 11:22:46 +0100 | [diff] [blame] | 145 | |
Stefan Roese | f5df36d | 2015-07-06 13:35:55 +0200 | [diff] [blame] | 146 | if ((freq == freq_last) || (mask == mask_last)) { |
| 147 | idx = idx_last; |
| 148 | mode = mode_last; |
Stefan Roese | 1cdd941 | 2015-03-12 11:22:46 +0100 | [diff] [blame] | 149 | } else { |
Stefan Roese | f5df36d | 2015-07-06 13:35:55 +0200 | [diff] [blame] | 150 | /* Toggle blink index */ |
| 151 | if (idx_last == 0) { |
| 152 | idx = 1; |
| 153 | mode = PCA9551_LED_STATE_BLINK1; |
| 154 | } else { |
| 155 | idx = 0; |
| 156 | mode = PCA9551_LED_STATE_BLINK0; |
| 157 | } |
| 158 | |
| 159 | idx_last = idx; |
| 160 | mode_last = mode; |
Stefan Roese | 1cdd941 | 2015-03-12 11:22:46 +0100 | [diff] [blame] | 161 | } |
Stefan Roese | f5df36d | 2015-07-06 13:35:55 +0200 | [diff] [blame] | 162 | freq_last = freq; |
| 163 | mask_last = mask; |
Stefan Roese | 1cdd941 | 2015-03-12 11:22:46 +0100 | [diff] [blame] | 164 | |
| 165 | rate.psc = ((freq * 38) / 1000) - 1; |
| 166 | rate.pwm = 128; /* 50% duty cycle */ |
| 167 | |
Stefan Roese | f5df36d | 2015-07-06 13:35:55 +0200 | [diff] [blame] | 168 | pca9551_led_set_blink_rate(idx, rate); |
Stefan Roese | 1cdd941 | 2015-03-12 11:22:46 +0100 | [diff] [blame] | 169 | pca9551_led_set_state(mask, mode); |
| 170 | } |