NeilBrown | a6d511e | 2012-01-10 15:09:40 -0800 | [diff] [blame] | 1 | /* |
| 2 | * leds-tca6507 |
| 3 | * |
| 4 | * The TCA6507 is a programmable LED controller that can drive 7 |
| 5 | * separate lines either by holding them low, or by pulsing them |
| 6 | * with modulated width. |
NeilBrown | 1f431af | 2013-11-12 21:52:43 -0800 | [diff] [blame] | 7 | * The modulation can be varied in a simple pattern to produce a |
| 8 | * blink or double-blink. |
NeilBrown | a6d511e | 2012-01-10 15:09:40 -0800 | [diff] [blame] | 9 | * |
NeilBrown | 1f431af | 2013-11-12 21:52:43 -0800 | [diff] [blame] | 10 | * This driver can configure each line either as a 'GPIO' which is |
| 11 | * out-only (pull-up resistor required) or as an LED with variable |
| 12 | * brightness and hardware-assisted blinking. |
NeilBrown | a6d511e | 2012-01-10 15:09:40 -0800 | [diff] [blame] | 13 | * |
NeilBrown | 1f431af | 2013-11-12 21:52:43 -0800 | [diff] [blame] | 14 | * Apart from OFF and ON there are three programmable brightness |
| 15 | * levels which can be programmed from 0 to 15 and indicate how many |
| 16 | * 500usec intervals in each 8msec that the led is 'on'. The levels |
| 17 | * are named MASTER, BANK0 and BANK1. |
NeilBrown | a6d511e | 2012-01-10 15:09:40 -0800 | [diff] [blame] | 18 | * |
NeilBrown | 1f431af | 2013-11-12 21:52:43 -0800 | [diff] [blame] | 19 | * There are two different blink rates that can be programmed, each |
| 20 | * with separate time for rise, on, fall, off and second-off. Thus if |
| 21 | * 3 or more different non-trivial rates are required, software must |
| 22 | * be used for the extra rates. The two different blink rates must |
| 23 | * align with the two levels BANK0 and BANK1. This driver does not |
| 24 | * support double-blink so 'second-off' always matches 'off'. |
NeilBrown | a6d511e | 2012-01-10 15:09:40 -0800 | [diff] [blame] | 25 | * |
NeilBrown | 1f431af | 2013-11-12 21:52:43 -0800 | [diff] [blame] | 26 | * Only 16 different times can be programmed in a roughly logarithmic |
| 27 | * scale from 64ms to 16320ms. To be precise the possible times are: |
NeilBrown | a6d511e | 2012-01-10 15:09:40 -0800 | [diff] [blame] | 28 | * 0, 64, 128, 192, 256, 384, 512, 768, |
| 29 | * 1024, 1536, 2048, 3072, 4096, 5760, 8128, 16320 |
| 30 | * |
NeilBrown | 1f431af | 2013-11-12 21:52:43 -0800 | [diff] [blame] | 31 | * Times that cannot be closely matched with these must be handled in |
| 32 | * software. This driver allows 12.5% error in matching. |
NeilBrown | a6d511e | 2012-01-10 15:09:40 -0800 | [diff] [blame] | 33 | * |
NeilBrown | 1f431af | 2013-11-12 21:52:43 -0800 | [diff] [blame] | 34 | * This driver does not allow rise/fall rates to be set explicitly. |
| 35 | * When trying to match a given 'on' or 'off' period, an appropriate |
| 36 | * pair of 'change' and 'hold' times are chosen to get a close match. |
| 37 | * If the target delay is even, the 'change' number will be the |
| 38 | * smaller; if odd, the 'hold' number will be the smaller. |
NeilBrown | a6d511e | 2012-01-10 15:09:40 -0800 | [diff] [blame] | 39 | |
NeilBrown | 1f431af | 2013-11-12 21:52:43 -0800 | [diff] [blame] | 40 | * Choosing pairs of delays with 12.5% errors allows us to match |
| 41 | * delays in the ranges: 56-72, 112-144, 168-216, 224-27504, |
| 42 | * 28560-36720. |
| 43 | * 26% of the achievable sums can be matched by multiple pairings. |
| 44 | * For example 1536 == 1536+0, 1024+512, or 768+768. |
| 45 | * This driver will always choose the pairing with the least |
| 46 | * maximum - 768+768 in this case. Other pairings are not available. |
NeilBrown | a6d511e | 2012-01-10 15:09:40 -0800 | [diff] [blame] | 47 | * |
NeilBrown | 1f431af | 2013-11-12 21:52:43 -0800 | [diff] [blame] | 48 | * Access to the 3 levels and 2 blinks are on a first-come, |
| 49 | * first-served basis. Access can be shared by multiple leds if they |
| 50 | * have the same level and either same blink rates, or some don't |
| 51 | * blink. When a led changes, it relinquishes access and tries again, |
| 52 | * so it might lose access to hardware blink. |
NeilBrown | a6d511e | 2012-01-10 15:09:40 -0800 | [diff] [blame] | 53 | * |
NeilBrown | 1f431af | 2013-11-12 21:52:43 -0800 | [diff] [blame] | 54 | * If a blink engine cannot be allocated, software blink is used. If |
| 55 | * the desired brightness cannot be allocated, the closest available |
| 56 | * non-zero brightness is used. As 'full' is always available, the |
| 57 | * worst case would be to have two different blink rates at '1', with |
| 58 | * Max at '2', then other leds will have to choose between '2' and |
| 59 | * '16'. Hopefully this is not likely. |
NeilBrown | a6d511e | 2012-01-10 15:09:40 -0800 | [diff] [blame] | 60 | * |
NeilBrown | 1f431af | 2013-11-12 21:52:43 -0800 | [diff] [blame] | 61 | * Each bank (BANK0 and BANK1) has two usage counts - LEDs using the |
| 62 | * brightness and LEDs using the blink. It can only be reprogrammed |
| 63 | * when the appropriate counter is zero. The MASTER level has a |
| 64 | * single usage count. |
| 65 | * |
| 66 | * Each LED has programmable 'on' and 'off' time as milliseconds. |
| 67 | * With each there is a flag saying if it was explicitly requested or |
| 68 | * defaulted. Similarly the banks know if each time was explicit or a |
| 69 | * default. Defaults are permitted to be changed freely - they are |
| 70 | * not recognised when matching. |
NeilBrown | a6d511e | 2012-01-10 15:09:40 -0800 | [diff] [blame] | 71 | * |
| 72 | * |
NeilBrown | 1f431af | 2013-11-12 21:52:43 -0800 | [diff] [blame] | 73 | * An led-tca6507 device must be provided with platform data or |
| 74 | * configured via devicetree. |
NeilBrown | a6d511e | 2012-01-10 15:09:40 -0800 | [diff] [blame] | 75 | * |
NeilBrown | 1f431af | 2013-11-12 21:52:43 -0800 | [diff] [blame] | 76 | * The platform-data lists for each output: the name, default trigger, |
| 77 | * and whether the signal is being used as a GPIO rather than an LED. |
| 78 | * 'struct led_plaform_data' is used for this. If 'name' is NULL, the |
| 79 | * output isn't used. If 'flags' is TCA6507_MAKE_GPIO, the output is |
| 80 | * a GPO. The "struct led_platform_data" can be embedded in a "struct |
| 81 | * tca6507_platform_data" which adds a 'gpio_base' for the GPIOs, and |
| 82 | * a 'setup' callback which is called once the GPIOs are available. |
| 83 | * |
| 84 | * When configured via devicetree there is one child for each output. |
| 85 | * The "reg" determines the output number and "compatible" determines |
| 86 | * whether it is an LED or a GPIO. "linux,default-trigger" can set a |
| 87 | * default trigger. |
NeilBrown | a6d511e | 2012-01-10 15:09:40 -0800 | [diff] [blame] | 88 | */ |
| 89 | |
| 90 | #include <linux/module.h> |
| 91 | #include <linux/slab.h> |
| 92 | #include <linux/leds.h> |
| 93 | #include <linux/err.h> |
| 94 | #include <linux/i2c.h> |
| 95 | #include <linux/gpio.h> |
| 96 | #include <linux/workqueue.h> |
| 97 | #include <linux/leds-tca6507.h> |
Sachin Kamat | fbd9df2 | 2013-03-14 03:09:49 -0700 | [diff] [blame] | 98 | #include <linux/of.h> |
NeilBrown | a6d511e | 2012-01-10 15:09:40 -0800 | [diff] [blame] | 99 | |
| 100 | /* LED select registers determine the source that drives LED outputs */ |
| 101 | #define TCA6507_LS_LED_OFF 0x0 /* Output HI-Z (off) */ |
| 102 | #define TCA6507_LS_LED_OFF1 0x1 /* Output HI-Z (off) - not used */ |
| 103 | #define TCA6507_LS_LED_PWM0 0x2 /* Output LOW with Bank0 rate */ |
| 104 | #define TCA6507_LS_LED_PWM1 0x3 /* Output LOW with Bank1 rate */ |
| 105 | #define TCA6507_LS_LED_ON 0x4 /* Output LOW (on) */ |
| 106 | #define TCA6507_LS_LED_MIR 0x5 /* Output LOW with Master Intensity */ |
| 107 | #define TCA6507_LS_BLINK0 0x6 /* Blink at Bank0 rate */ |
| 108 | #define TCA6507_LS_BLINK1 0x7 /* Blink at Bank1 rate */ |
| 109 | |
| 110 | enum { |
| 111 | BANK0, |
| 112 | BANK1, |
| 113 | MASTER, |
| 114 | }; |
| 115 | static int bank_source[3] = { |
| 116 | TCA6507_LS_LED_PWM0, |
| 117 | TCA6507_LS_LED_PWM1, |
| 118 | TCA6507_LS_LED_MIR, |
| 119 | }; |
| 120 | static int blink_source[2] = { |
| 121 | TCA6507_LS_BLINK0, |
| 122 | TCA6507_LS_BLINK1, |
| 123 | }; |
| 124 | |
| 125 | /* PWM registers */ |
| 126 | #define TCA6507_REG_CNT 11 |
| 127 | |
| 128 | /* |
| 129 | * 0x00, 0x01, 0x02 encode the TCA6507_LS_* values, each output |
| 130 | * owns one bit in each register |
| 131 | */ |
| 132 | #define TCA6507_FADE_ON 0x03 |
| 133 | #define TCA6507_FULL_ON 0x04 |
| 134 | #define TCA6507_FADE_OFF 0x05 |
| 135 | #define TCA6507_FIRST_OFF 0x06 |
| 136 | #define TCA6507_SECOND_OFF 0x07 |
| 137 | #define TCA6507_MAX_INTENSITY 0x08 |
| 138 | #define TCA6507_MASTER_INTENSITY 0x09 |
| 139 | #define TCA6507_INITIALIZE 0x0A |
| 140 | |
| 141 | #define INIT_CODE 0x8 |
| 142 | |
| 143 | #define TIMECODES 16 |
| 144 | static int time_codes[TIMECODES] = { |
| 145 | 0, 64, 128, 192, 256, 384, 512, 768, |
| 146 | 1024, 1536, 2048, 3072, 4096, 5760, 8128, 16320 |
| 147 | }; |
| 148 | |
| 149 | /* Convert an led.brightness level (0..255) to a TCA6507 level (0..15) */ |
| 150 | static inline int TO_LEVEL(int brightness) |
| 151 | { |
| 152 | return brightness >> 4; |
| 153 | } |
| 154 | |
| 155 | /* ...and convert back */ |
| 156 | static inline int TO_BRIGHT(int level) |
| 157 | { |
| 158 | if (level) |
| 159 | return (level << 4) | 0xf; |
| 160 | return 0; |
| 161 | } |
| 162 | |
| 163 | #define NUM_LEDS 7 |
| 164 | struct tca6507_chip { |
| 165 | int reg_set; /* One bit per register where |
| 166 | * a '1' means the register |
| 167 | * should be written */ |
| 168 | u8 reg_file[TCA6507_REG_CNT]; |
| 169 | /* Bank 2 is Master Intensity and doesn't use times */ |
| 170 | struct bank { |
| 171 | int level; |
| 172 | int ontime, offtime; |
| 173 | int on_dflt, off_dflt; |
| 174 | int time_use, level_use; |
| 175 | } bank[3]; |
| 176 | struct i2c_client *client; |
| 177 | struct work_struct work; |
| 178 | spinlock_t lock; |
| 179 | |
| 180 | struct tca6507_led { |
| 181 | struct tca6507_chip *chip; |
| 182 | struct led_classdev led_cdev; |
| 183 | int num; |
| 184 | int ontime, offtime; |
| 185 | int on_dflt, off_dflt; |
| 186 | int bank; /* Bank used, or -1 */ |
| 187 | int blink; /* Set if hardware-blinking */ |
| 188 | } leds[NUM_LEDS]; |
| 189 | #ifdef CONFIG_GPIOLIB |
| 190 | struct gpio_chip gpio; |
| 191 | const char *gpio_name[NUM_LEDS]; |
| 192 | int gpio_map[NUM_LEDS]; |
| 193 | #endif |
| 194 | }; |
| 195 | |
| 196 | static const struct i2c_device_id tca6507_id[] = { |
| 197 | { "tca6507" }, |
| 198 | { } |
| 199 | }; |
| 200 | MODULE_DEVICE_TABLE(i2c, tca6507_id); |
| 201 | |
| 202 | static int choose_times(int msec, int *c1p, int *c2p) |
| 203 | { |
| 204 | /* |
NeilBrown | 1f431af | 2013-11-12 21:52:43 -0800 | [diff] [blame] | 205 | * Choose two timecodes which add to 'msec' as near as |
| 206 | * possible. The first returned is the 'on' or 'off' time. |
| 207 | * The second is to be used as a 'fade-on' or 'fade-off' time. |
| 208 | * If 'msec' is even, the first will not be smaller than the |
| 209 | * second. If 'msec' is odd, the first will not be larger |
| 210 | * than the second. |
| 211 | * If we cannot get a sum within 1/8 of 'msec' fail with |
| 212 | * -EINVAL, otherwise return the sum that was achieved, plus 1 |
| 213 | * if the first is smaller. |
| 214 | * If two possibilities are equally good (e.g. 512+0, |
| 215 | * 256+256), choose the first pair so there is more |
| 216 | * change-time visible (i.e. it is softer). |
NeilBrown | a6d511e | 2012-01-10 15:09:40 -0800 | [diff] [blame] | 217 | */ |
| 218 | int c1, c2; |
| 219 | int tmax = msec * 9 / 8; |
| 220 | int tmin = msec * 7 / 8; |
| 221 | int diff = 65536; |
| 222 | |
| 223 | /* We start at '1' to ensure we never even think of choosing a |
| 224 | * total time of '0'. |
| 225 | */ |
| 226 | for (c1 = 1; c1 < TIMECODES; c1++) { |
| 227 | int t = time_codes[c1]; |
| 228 | if (t*2 < tmin) |
| 229 | continue; |
| 230 | if (t > tmax) |
| 231 | break; |
| 232 | for (c2 = 0; c2 <= c1; c2++) { |
| 233 | int tt = t + time_codes[c2]; |
| 234 | int d; |
| 235 | if (tt < tmin) |
| 236 | continue; |
| 237 | if (tt > tmax) |
| 238 | break; |
| 239 | /* This works! */ |
| 240 | d = abs(msec - tt); |
| 241 | if (d >= diff) |
| 242 | continue; |
| 243 | /* Best yet */ |
| 244 | *c1p = c1; |
| 245 | *c2p = c2; |
| 246 | diff = d; |
| 247 | if (d == 0) |
| 248 | return msec; |
| 249 | } |
| 250 | } |
| 251 | if (diff < 65536) { |
| 252 | int actual; |
| 253 | if (msec & 1) { |
| 254 | c1 = *c2p; |
| 255 | *c2p = *c1p; |
| 256 | *c1p = c1; |
| 257 | } |
| 258 | actual = time_codes[*c1p] + time_codes[*c2p]; |
| 259 | if (*c1p < *c2p) |
| 260 | return actual + 1; |
| 261 | else |
| 262 | return actual; |
| 263 | } |
| 264 | /* No close match */ |
| 265 | return -EINVAL; |
| 266 | } |
| 267 | |
| 268 | /* |
NeilBrown | 1f431af | 2013-11-12 21:52:43 -0800 | [diff] [blame] | 269 | * Update the register file with the appropriate 3-bit state for the |
| 270 | * given led. |
NeilBrown | a6d511e | 2012-01-10 15:09:40 -0800 | [diff] [blame] | 271 | */ |
| 272 | static void set_select(struct tca6507_chip *tca, int led, int val) |
| 273 | { |
| 274 | int mask = (1 << led); |
| 275 | int bit; |
| 276 | |
| 277 | for (bit = 0; bit < 3; bit++) { |
| 278 | int n = tca->reg_file[bit] & ~mask; |
| 279 | if (val & (1 << bit)) |
| 280 | n |= mask; |
| 281 | if (tca->reg_file[bit] != n) { |
| 282 | tca->reg_file[bit] = n; |
| 283 | tca->reg_set |= (1 << bit); |
| 284 | } |
| 285 | } |
| 286 | } |
| 287 | |
NeilBrown | 1f431af | 2013-11-12 21:52:43 -0800 | [diff] [blame] | 288 | /* Update the register file with the appropriate 4-bit code for one |
| 289 | * bank or other. This can be used for timers, for levels, or for |
| 290 | * initialization. |
NeilBrown | a6d511e | 2012-01-10 15:09:40 -0800 | [diff] [blame] | 291 | */ |
| 292 | static void set_code(struct tca6507_chip *tca, int reg, int bank, int new) |
| 293 | { |
| 294 | int mask = 0xF; |
| 295 | int n; |
| 296 | if (bank) { |
| 297 | mask <<= 4; |
| 298 | new <<= 4; |
| 299 | } |
| 300 | n = tca->reg_file[reg] & ~mask; |
| 301 | n |= new; |
| 302 | if (tca->reg_file[reg] != n) { |
| 303 | tca->reg_file[reg] = n; |
| 304 | tca->reg_set |= 1 << reg; |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | /* Update brightness level. */ |
| 309 | static void set_level(struct tca6507_chip *tca, int bank, int level) |
| 310 | { |
| 311 | switch (bank) { |
| 312 | case BANK0: |
| 313 | case BANK1: |
| 314 | set_code(tca, TCA6507_MAX_INTENSITY, bank, level); |
| 315 | break; |
| 316 | case MASTER: |
| 317 | set_code(tca, TCA6507_MASTER_INTENSITY, 0, level); |
| 318 | break; |
| 319 | } |
| 320 | tca->bank[bank].level = level; |
| 321 | } |
| 322 | |
NeilBrown | 1f431af | 2013-11-12 21:52:43 -0800 | [diff] [blame] | 323 | /* Record all relevant time codes for a given bank */ |
NeilBrown | a6d511e | 2012-01-10 15:09:40 -0800 | [diff] [blame] | 324 | static void set_times(struct tca6507_chip *tca, int bank) |
| 325 | { |
| 326 | int c1, c2; |
| 327 | int result; |
| 328 | |
| 329 | result = choose_times(tca->bank[bank].ontime, &c1, &c2); |
| 330 | dev_dbg(&tca->client->dev, |
NeilBrown | 1f431af | 2013-11-12 21:52:43 -0800 | [diff] [blame] | 331 | "Chose on times %d(%d) %d(%d) for %dms\n", |
| 332 | c1, time_codes[c1], |
NeilBrown | a6d511e | 2012-01-10 15:09:40 -0800 | [diff] [blame] | 333 | c2, time_codes[c2], tca->bank[bank].ontime); |
| 334 | set_code(tca, TCA6507_FADE_ON, bank, c2); |
| 335 | set_code(tca, TCA6507_FULL_ON, bank, c1); |
| 336 | tca->bank[bank].ontime = result; |
| 337 | |
| 338 | result = choose_times(tca->bank[bank].offtime, &c1, &c2); |
| 339 | dev_dbg(&tca->client->dev, |
NeilBrown | 1f431af | 2013-11-12 21:52:43 -0800 | [diff] [blame] | 340 | "Chose off times %d(%d) %d(%d) for %dms\n", |
| 341 | c1, time_codes[c1], |
NeilBrown | a6d511e | 2012-01-10 15:09:40 -0800 | [diff] [blame] | 342 | c2, time_codes[c2], tca->bank[bank].offtime); |
| 343 | set_code(tca, TCA6507_FADE_OFF, bank, c2); |
| 344 | set_code(tca, TCA6507_FIRST_OFF, bank, c1); |
| 345 | set_code(tca, TCA6507_SECOND_OFF, bank, c1); |
| 346 | tca->bank[bank].offtime = result; |
| 347 | |
| 348 | set_code(tca, TCA6507_INITIALIZE, bank, INIT_CODE); |
| 349 | } |
| 350 | |
| 351 | /* Write all needed register of tca6507 */ |
| 352 | |
| 353 | static void tca6507_work(struct work_struct *work) |
| 354 | { |
| 355 | struct tca6507_chip *tca = container_of(work, struct tca6507_chip, |
| 356 | work); |
| 357 | struct i2c_client *cl = tca->client; |
| 358 | int set; |
| 359 | u8 file[TCA6507_REG_CNT]; |
| 360 | int r; |
| 361 | |
| 362 | spin_lock_irq(&tca->lock); |
| 363 | set = tca->reg_set; |
| 364 | memcpy(file, tca->reg_file, TCA6507_REG_CNT); |
| 365 | tca->reg_set = 0; |
| 366 | spin_unlock_irq(&tca->lock); |
| 367 | |
| 368 | for (r = 0; r < TCA6507_REG_CNT; r++) |
| 369 | if (set & (1<<r)) |
| 370 | i2c_smbus_write_byte_data(cl, r, file[r]); |
| 371 | } |
| 372 | |
| 373 | static void led_release(struct tca6507_led *led) |
| 374 | { |
| 375 | /* If led owns any resource, release it. */ |
| 376 | struct tca6507_chip *tca = led->chip; |
| 377 | if (led->bank >= 0) { |
| 378 | struct bank *b = tca->bank + led->bank; |
| 379 | if (led->blink) |
| 380 | b->time_use--; |
| 381 | b->level_use--; |
| 382 | } |
| 383 | led->blink = 0; |
| 384 | led->bank = -1; |
| 385 | } |
| 386 | |
| 387 | static int led_prepare(struct tca6507_led *led) |
| 388 | { |
NeilBrown | 1f431af | 2013-11-12 21:52:43 -0800 | [diff] [blame] | 389 | /* Assign this led to a bank, configuring that bank if |
| 390 | * necessary. */ |
NeilBrown | a6d511e | 2012-01-10 15:09:40 -0800 | [diff] [blame] | 391 | int level = TO_LEVEL(led->led_cdev.brightness); |
| 392 | struct tca6507_chip *tca = led->chip; |
| 393 | int c1, c2; |
| 394 | int i; |
| 395 | struct bank *b; |
| 396 | int need_init = 0; |
| 397 | |
| 398 | led->led_cdev.brightness = TO_BRIGHT(level); |
| 399 | if (level == 0) { |
| 400 | set_select(tca, led->num, TCA6507_LS_LED_OFF); |
| 401 | return 0; |
| 402 | } |
| 403 | |
| 404 | if (led->ontime == 0 || led->offtime == 0) { |
| 405 | /* |
NeilBrown | 1f431af | 2013-11-12 21:52:43 -0800 | [diff] [blame] | 406 | * Just set the brightness, choosing first usable |
| 407 | * bank. If none perfect, choose best. Count |
| 408 | * backwards so we check MASTER bank first to avoid |
| 409 | * wasting a timer. |
NeilBrown | a6d511e | 2012-01-10 15:09:40 -0800 | [diff] [blame] | 410 | */ |
| 411 | int best = -1;/* full-on */ |
| 412 | int diff = 15-level; |
| 413 | |
| 414 | if (level == 15) { |
| 415 | set_select(tca, led->num, TCA6507_LS_LED_ON); |
| 416 | return 0; |
| 417 | } |
| 418 | |
| 419 | for (i = MASTER; i >= BANK0; i--) { |
| 420 | int d; |
| 421 | if (tca->bank[i].level == level || |
| 422 | tca->bank[i].level_use == 0) { |
| 423 | best = i; |
| 424 | break; |
| 425 | } |
| 426 | d = abs(level - tca->bank[i].level); |
| 427 | if (d < diff) { |
| 428 | diff = d; |
| 429 | best = i; |
| 430 | } |
| 431 | } |
| 432 | if (best == -1) { |
| 433 | /* Best brightness is full-on */ |
| 434 | set_select(tca, led->num, TCA6507_LS_LED_ON); |
| 435 | led->led_cdev.brightness = LED_FULL; |
| 436 | return 0; |
| 437 | } |
| 438 | |
| 439 | if (!tca->bank[best].level_use) |
| 440 | set_level(tca, best, level); |
| 441 | |
| 442 | tca->bank[best].level_use++; |
| 443 | led->bank = best; |
| 444 | set_select(tca, led->num, bank_source[best]); |
| 445 | led->led_cdev.brightness = TO_BRIGHT(tca->bank[best].level); |
| 446 | return 0; |
| 447 | } |
| 448 | |
| 449 | /* |
NeilBrown | 1f431af | 2013-11-12 21:52:43 -0800 | [diff] [blame] | 450 | * We have on/off time so we need to try to allocate a timing |
| 451 | * bank. First check if times are compatible with hardware |
| 452 | * and give up if not. |
NeilBrown | a6d511e | 2012-01-10 15:09:40 -0800 | [diff] [blame] | 453 | */ |
| 454 | if (choose_times(led->ontime, &c1, &c2) < 0) |
| 455 | return -EINVAL; |
| 456 | if (choose_times(led->offtime, &c1, &c2) < 0) |
| 457 | return -EINVAL; |
| 458 | |
| 459 | for (i = BANK0; i <= BANK1; i++) { |
| 460 | if (tca->bank[i].level_use == 0) |
| 461 | /* not in use - it is ours! */ |
| 462 | break; |
| 463 | if (tca->bank[i].level != level) |
| 464 | /* Incompatible level - skip */ |
| 465 | /* FIX: if timer matches we maybe should consider |
| 466 | * this anyway... |
| 467 | */ |
| 468 | continue; |
| 469 | |
| 470 | if (tca->bank[i].time_use == 0) |
| 471 | /* Timer not in use, and level matches - use it */ |
| 472 | break; |
| 473 | |
| 474 | if (!(tca->bank[i].on_dflt || |
| 475 | led->on_dflt || |
| 476 | tca->bank[i].ontime == led->ontime)) |
| 477 | /* on time is incompatible */ |
| 478 | continue; |
| 479 | |
| 480 | if (!(tca->bank[i].off_dflt || |
| 481 | led->off_dflt || |
| 482 | tca->bank[i].offtime == led->offtime)) |
| 483 | /* off time is incompatible */ |
| 484 | continue; |
| 485 | |
| 486 | /* looks like a suitable match */ |
| 487 | break; |
| 488 | } |
| 489 | |
| 490 | if (i > BANK1) |
| 491 | /* Nothing matches - how sad */ |
| 492 | return -EINVAL; |
| 493 | |
| 494 | b = &tca->bank[i]; |
| 495 | if (b->level_use == 0) |
| 496 | set_level(tca, i, level); |
| 497 | b->level_use++; |
| 498 | led->bank = i; |
| 499 | |
| 500 | if (b->on_dflt || |
| 501 | !led->on_dflt || |
| 502 | b->time_use == 0) { |
| 503 | b->ontime = led->ontime; |
| 504 | b->on_dflt = led->on_dflt; |
| 505 | need_init = 1; |
| 506 | } |
| 507 | |
| 508 | if (b->off_dflt || |
| 509 | !led->off_dflt || |
| 510 | b->time_use == 0) { |
| 511 | b->offtime = led->offtime; |
| 512 | b->off_dflt = led->off_dflt; |
| 513 | need_init = 1; |
| 514 | } |
| 515 | |
| 516 | if (need_init) |
| 517 | set_times(tca, i); |
| 518 | |
| 519 | led->ontime = b->ontime; |
| 520 | led->offtime = b->offtime; |
| 521 | |
| 522 | b->time_use++; |
| 523 | led->blink = 1; |
| 524 | led->led_cdev.brightness = TO_BRIGHT(b->level); |
| 525 | set_select(tca, led->num, blink_source[i]); |
| 526 | return 0; |
| 527 | } |
| 528 | |
| 529 | static int led_assign(struct tca6507_led *led) |
| 530 | { |
| 531 | struct tca6507_chip *tca = led->chip; |
| 532 | int err; |
| 533 | unsigned long flags; |
| 534 | |
| 535 | spin_lock_irqsave(&tca->lock, flags); |
| 536 | led_release(led); |
| 537 | err = led_prepare(led); |
| 538 | if (err) { |
| 539 | /* |
NeilBrown | 1f431af | 2013-11-12 21:52:43 -0800 | [diff] [blame] | 540 | * Can only fail on timer setup. In that case we need |
| 541 | * to re-establish as steady level. |
NeilBrown | a6d511e | 2012-01-10 15:09:40 -0800 | [diff] [blame] | 542 | */ |
| 543 | led->ontime = 0; |
| 544 | led->offtime = 0; |
| 545 | led_prepare(led); |
| 546 | } |
| 547 | spin_unlock_irqrestore(&tca->lock, flags); |
| 548 | |
| 549 | if (tca->reg_set) |
| 550 | schedule_work(&tca->work); |
| 551 | return err; |
| 552 | } |
| 553 | |
| 554 | static void tca6507_brightness_set(struct led_classdev *led_cdev, |
| 555 | enum led_brightness brightness) |
| 556 | { |
| 557 | struct tca6507_led *led = container_of(led_cdev, struct tca6507_led, |
| 558 | led_cdev); |
| 559 | led->led_cdev.brightness = brightness; |
| 560 | led->ontime = 0; |
| 561 | led->offtime = 0; |
| 562 | led_assign(led); |
| 563 | } |
| 564 | |
| 565 | static int tca6507_blink_set(struct led_classdev *led_cdev, |
| 566 | unsigned long *delay_on, |
| 567 | unsigned long *delay_off) |
| 568 | { |
| 569 | struct tca6507_led *led = container_of(led_cdev, struct tca6507_led, |
| 570 | led_cdev); |
| 571 | |
| 572 | if (*delay_on == 0) |
| 573 | led->on_dflt = 1; |
| 574 | else if (delay_on != &led_cdev->blink_delay_on) |
| 575 | led->on_dflt = 0; |
| 576 | led->ontime = *delay_on; |
| 577 | |
| 578 | if (*delay_off == 0) |
| 579 | led->off_dflt = 1; |
| 580 | else if (delay_off != &led_cdev->blink_delay_off) |
| 581 | led->off_dflt = 0; |
| 582 | led->offtime = *delay_off; |
| 583 | |
| 584 | if (led->ontime == 0) |
| 585 | led->ontime = 512; |
| 586 | if (led->offtime == 0) |
| 587 | led->offtime = 512; |
| 588 | |
| 589 | if (led->led_cdev.brightness == LED_OFF) |
| 590 | led->led_cdev.brightness = LED_FULL; |
| 591 | if (led_assign(led) < 0) { |
| 592 | led->ontime = 0; |
| 593 | led->offtime = 0; |
| 594 | led->led_cdev.brightness = LED_OFF; |
| 595 | return -EINVAL; |
| 596 | } |
| 597 | *delay_on = led->ontime; |
| 598 | *delay_off = led->offtime; |
| 599 | return 0; |
| 600 | } |
| 601 | |
| 602 | #ifdef CONFIG_GPIOLIB |
| 603 | static void tca6507_gpio_set_value(struct gpio_chip *gc, |
| 604 | unsigned offset, int val) |
| 605 | { |
| 606 | struct tca6507_chip *tca = container_of(gc, struct tca6507_chip, gpio); |
| 607 | unsigned long flags; |
| 608 | |
| 609 | spin_lock_irqsave(&tca->lock, flags); |
| 610 | /* |
NeilBrown | 1f431af | 2013-11-12 21:52:43 -0800 | [diff] [blame] | 611 | * 'OFF' is floating high, and 'ON' is pulled down, so it has |
| 612 | * the inverse sense of 'val'. |
NeilBrown | a6d511e | 2012-01-10 15:09:40 -0800 | [diff] [blame] | 613 | */ |
| 614 | set_select(tca, tca->gpio_map[offset], |
| 615 | val ? TCA6507_LS_LED_OFF : TCA6507_LS_LED_ON); |
| 616 | spin_unlock_irqrestore(&tca->lock, flags); |
| 617 | if (tca->reg_set) |
| 618 | schedule_work(&tca->work); |
| 619 | } |
| 620 | |
| 621 | static int tca6507_gpio_direction_output(struct gpio_chip *gc, |
| 622 | unsigned offset, int val) |
| 623 | { |
| 624 | tca6507_gpio_set_value(gc, offset, val); |
| 625 | return 0; |
| 626 | } |
| 627 | |
| 628 | static int tca6507_probe_gpios(struct i2c_client *client, |
| 629 | struct tca6507_chip *tca, |
| 630 | struct tca6507_platform_data *pdata) |
| 631 | { |
| 632 | int err; |
| 633 | int i = 0; |
| 634 | int gpios = 0; |
| 635 | |
| 636 | for (i = 0; i < NUM_LEDS; i++) |
| 637 | if (pdata->leds.leds[i].name && pdata->leds.leds[i].flags) { |
| 638 | /* Configure as a gpio */ |
| 639 | tca->gpio_name[gpios] = pdata->leds.leds[i].name; |
| 640 | tca->gpio_map[gpios] = i; |
| 641 | gpios++; |
| 642 | } |
| 643 | |
| 644 | if (!gpios) |
| 645 | return 0; |
| 646 | |
| 647 | tca->gpio.label = "gpio-tca6507"; |
| 648 | tca->gpio.names = tca->gpio_name; |
| 649 | tca->gpio.ngpio = gpios; |
| 650 | tca->gpio.base = pdata->gpio_base; |
| 651 | tca->gpio.owner = THIS_MODULE; |
| 652 | tca->gpio.direction_output = tca6507_gpio_direction_output; |
| 653 | tca->gpio.set = tca6507_gpio_set_value; |
| 654 | tca->gpio.dev = &client->dev; |
NeilBrown | 10ead6e | 2013-10-31 19:41:20 -0700 | [diff] [blame] | 655 | #ifdef CONFIG_OF_GPIO |
| 656 | tca->gpio.of_node = of_node_get(client->dev.of_node); |
| 657 | #endif |
NeilBrown | a6d511e | 2012-01-10 15:09:40 -0800 | [diff] [blame] | 658 | err = gpiochip_add(&tca->gpio); |
| 659 | if (err) { |
| 660 | tca->gpio.ngpio = 0; |
| 661 | return err; |
| 662 | } |
| 663 | if (pdata->setup) |
| 664 | pdata->setup(tca->gpio.base, tca->gpio.ngpio); |
| 665 | return 0; |
| 666 | } |
| 667 | |
| 668 | static void tca6507_remove_gpio(struct tca6507_chip *tca) |
| 669 | { |
abdoulaye berthe | 88d5e52 | 2014-07-12 22:30:14 +0200 | [diff] [blame] | 670 | if (tca->gpio.ngpio) |
| 671 | gpiochip_remove(&tca->gpio); |
NeilBrown | a6d511e | 2012-01-10 15:09:40 -0800 | [diff] [blame] | 672 | } |
| 673 | #else /* CONFIG_GPIOLIB */ |
| 674 | static int tca6507_probe_gpios(struct i2c_client *client, |
| 675 | struct tca6507_chip *tca, |
| 676 | struct tca6507_platform_data *pdata) |
| 677 | { |
| 678 | return 0; |
| 679 | } |
| 680 | static void tca6507_remove_gpio(struct tca6507_chip *tca) |
| 681 | { |
| 682 | } |
| 683 | #endif /* CONFIG_GPIOLIB */ |
| 684 | |
Marek Belisko | 0c596a7 | 2012-11-30 14:00:49 -0800 | [diff] [blame] | 685 | #ifdef CONFIG_OF |
| 686 | static struct tca6507_platform_data * |
| 687 | tca6507_led_dt_init(struct i2c_client *client) |
| 688 | { |
| 689 | struct device_node *np = client->dev.of_node, *child; |
| 690 | struct tca6507_platform_data *pdata; |
| 691 | struct led_info *tca_leds; |
Axel Lin | ef754e8 | 2013-01-27 01:14:14 -0800 | [diff] [blame] | 692 | int count; |
Marek Belisko | 0c596a7 | 2012-11-30 14:00:49 -0800 | [diff] [blame] | 693 | |
Axel Lin | ef754e8 | 2013-01-27 01:14:14 -0800 | [diff] [blame] | 694 | count = of_get_child_count(np); |
| 695 | if (!count || count > NUM_LEDS) |
Marek Belisko | 0c596a7 | 2012-11-30 14:00:49 -0800 | [diff] [blame] | 696 | return ERR_PTR(-ENODEV); |
| 697 | |
| 698 | tca_leds = devm_kzalloc(&client->dev, |
NeilBrown | 9334129 | 2013-10-31 19:33:45 -0700 | [diff] [blame] | 699 | sizeof(struct led_info) * NUM_LEDS, GFP_KERNEL); |
Marek Belisko | 0c596a7 | 2012-11-30 14:00:49 -0800 | [diff] [blame] | 700 | if (!tca_leds) |
| 701 | return ERR_PTR(-ENOMEM); |
| 702 | |
| 703 | for_each_child_of_node(np, child) { |
| 704 | struct led_info led; |
| 705 | u32 reg; |
| 706 | int ret; |
| 707 | |
| 708 | led.name = |
| 709 | of_get_property(child, "label", NULL) ? : child->name; |
| 710 | led.default_trigger = |
| 711 | of_get_property(child, "linux,default-trigger", NULL); |
NeilBrown | 9334129 | 2013-10-31 19:33:45 -0700 | [diff] [blame] | 712 | led.flags = 0; |
NeilBrown | 10ead6e | 2013-10-31 19:41:20 -0700 | [diff] [blame] | 713 | if (of_property_match_string(child, "compatible", "gpio") >= 0) |
| 714 | led.flags |= TCA6507_MAKE_GPIO; |
Marek Belisko | 0c596a7 | 2012-11-30 14:00:49 -0800 | [diff] [blame] | 715 | ret = of_property_read_u32(child, "reg", ®); |
NeilBrown | 9334129 | 2013-10-31 19:33:45 -0700 | [diff] [blame] | 716 | if (ret != 0 || reg < 0 || reg >= NUM_LEDS) |
Marek Belisko | 0c596a7 | 2012-11-30 14:00:49 -0800 | [diff] [blame] | 717 | continue; |
| 718 | |
| 719 | tca_leds[reg] = led; |
| 720 | } |
| 721 | pdata = devm_kzalloc(&client->dev, |
| 722 | sizeof(struct tca6507_platform_data), GFP_KERNEL); |
| 723 | if (!pdata) |
| 724 | return ERR_PTR(-ENOMEM); |
| 725 | |
| 726 | pdata->leds.leds = tca_leds; |
NeilBrown | 9334129 | 2013-10-31 19:33:45 -0700 | [diff] [blame] | 727 | pdata->leds.num_leds = NUM_LEDS; |
Chen Gang | 33ca153 | 2013-12-08 20:41:37 -0800 | [diff] [blame] | 728 | #ifdef CONFIG_GPIOLIB |
NeilBrown | 10ead6e | 2013-10-31 19:41:20 -0700 | [diff] [blame] | 729 | pdata->gpio_base = -1; |
Chen Gang | 33ca153 | 2013-12-08 20:41:37 -0800 | [diff] [blame] | 730 | #endif |
Marek Belisko | 0c596a7 | 2012-11-30 14:00:49 -0800 | [diff] [blame] | 731 | return pdata; |
| 732 | } |
| 733 | |
| 734 | static const struct of_device_id of_tca6507_leds_match[] = { |
| 735 | { .compatible = "ti,tca6507", }, |
| 736 | {}, |
| 737 | }; |
Javier Martinez Canillas | 4d59ed8 | 2015-08-25 08:31:16 +0200 | [diff] [blame] | 738 | MODULE_DEVICE_TABLE(of, of_tca6507_leds_match); |
Marek Belisko | 0c596a7 | 2012-11-30 14:00:49 -0800 | [diff] [blame] | 739 | |
| 740 | #else |
| 741 | static struct tca6507_platform_data * |
| 742 | tca6507_led_dt_init(struct i2c_client *client) |
| 743 | { |
| 744 | return ERR_PTR(-ENODEV); |
| 745 | } |
| 746 | |
Marek Belisko | 0c596a7 | 2012-11-30 14:00:49 -0800 | [diff] [blame] | 747 | #endif |
| 748 | |
Bill Pemberton | 98ea1ea | 2012-11-19 13:23:02 -0500 | [diff] [blame] | 749 | static int tca6507_probe(struct i2c_client *client, |
Marek Belisko | 0c596a7 | 2012-11-30 14:00:49 -0800 | [diff] [blame] | 750 | const struct i2c_device_id *id) |
NeilBrown | a6d511e | 2012-01-10 15:09:40 -0800 | [diff] [blame] | 751 | { |
| 752 | struct tca6507_chip *tca; |
| 753 | struct i2c_adapter *adapter; |
| 754 | struct tca6507_platform_data *pdata; |
| 755 | int err; |
| 756 | int i = 0; |
| 757 | |
| 758 | adapter = to_i2c_adapter(client->dev.parent); |
Jingoo Han | 87aae1e | 2013-07-30 01:07:35 -0700 | [diff] [blame] | 759 | pdata = dev_get_platdata(&client->dev); |
NeilBrown | a6d511e | 2012-01-10 15:09:40 -0800 | [diff] [blame] | 760 | |
| 761 | if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) |
| 762 | return -EIO; |
| 763 | |
| 764 | if (!pdata || pdata->leds.num_leds != NUM_LEDS) { |
Marek Belisko | 0c596a7 | 2012-11-30 14:00:49 -0800 | [diff] [blame] | 765 | pdata = tca6507_led_dt_init(client); |
| 766 | if (IS_ERR(pdata)) { |
| 767 | dev_err(&client->dev, "Need %d entries in platform-data list\n", |
| 768 | NUM_LEDS); |
| 769 | return PTR_ERR(pdata); |
| 770 | } |
NeilBrown | a6d511e | 2012-01-10 15:09:40 -0800 | [diff] [blame] | 771 | } |
Bryan Wu | 6770fc6 | 2012-07-04 12:20:41 +0800 | [diff] [blame] | 772 | tca = devm_kzalloc(&client->dev, sizeof(*tca), GFP_KERNEL); |
NeilBrown | a6d511e | 2012-01-10 15:09:40 -0800 | [diff] [blame] | 773 | if (!tca) |
Dan Carpenter | 920c4f4 | 2012-03-23 15:02:07 -0700 | [diff] [blame] | 774 | return -ENOMEM; |
NeilBrown | a6d511e | 2012-01-10 15:09:40 -0800 | [diff] [blame] | 775 | |
| 776 | tca->client = client; |
| 777 | INIT_WORK(&tca->work, tca6507_work); |
| 778 | spin_lock_init(&tca->lock); |
| 779 | i2c_set_clientdata(client, tca); |
| 780 | |
| 781 | for (i = 0; i < NUM_LEDS; i++) { |
| 782 | struct tca6507_led *l = tca->leds + i; |
| 783 | |
| 784 | l->chip = tca; |
| 785 | l->num = i; |
| 786 | if (pdata->leds.leds[i].name && !pdata->leds.leds[i].flags) { |
| 787 | l->led_cdev.name = pdata->leds.leds[i].name; |
| 788 | l->led_cdev.default_trigger |
| 789 | = pdata->leds.leds[i].default_trigger; |
| 790 | l->led_cdev.brightness_set = tca6507_brightness_set; |
| 791 | l->led_cdev.blink_set = tca6507_blink_set; |
| 792 | l->bank = -1; |
| 793 | err = led_classdev_register(&client->dev, |
| 794 | &l->led_cdev); |
| 795 | if (err < 0) |
| 796 | goto exit; |
| 797 | } |
| 798 | } |
| 799 | err = tca6507_probe_gpios(client, tca, pdata); |
| 800 | if (err) |
| 801 | goto exit; |
| 802 | /* set all registers to known state - zero */ |
| 803 | tca->reg_set = 0x7f; |
| 804 | schedule_work(&tca->work); |
| 805 | |
| 806 | return 0; |
| 807 | exit: |
Dan Carpenter | 920c4f4 | 2012-03-23 15:02:07 -0700 | [diff] [blame] | 808 | while (i--) { |
NeilBrown | a6d511e | 2012-01-10 15:09:40 -0800 | [diff] [blame] | 809 | if (tca->leds[i].led_cdev.name) |
| 810 | led_classdev_unregister(&tca->leds[i].led_cdev); |
Dan Carpenter | 920c4f4 | 2012-03-23 15:02:07 -0700 | [diff] [blame] | 811 | } |
NeilBrown | a6d511e | 2012-01-10 15:09:40 -0800 | [diff] [blame] | 812 | return err; |
| 813 | } |
| 814 | |
Bill Pemberton | 678e8a6 | 2012-11-19 13:26:00 -0500 | [diff] [blame] | 815 | static int tca6507_remove(struct i2c_client *client) |
NeilBrown | a6d511e | 2012-01-10 15:09:40 -0800 | [diff] [blame] | 816 | { |
| 817 | int i; |
| 818 | struct tca6507_chip *tca = i2c_get_clientdata(client); |
| 819 | struct tca6507_led *tca_leds = tca->leds; |
| 820 | |
| 821 | for (i = 0; i < NUM_LEDS; i++) { |
| 822 | if (tca_leds[i].led_cdev.name) |
| 823 | led_classdev_unregister(&tca_leds[i].led_cdev); |
| 824 | } |
| 825 | tca6507_remove_gpio(tca); |
| 826 | cancel_work_sync(&tca->work); |
NeilBrown | a6d511e | 2012-01-10 15:09:40 -0800 | [diff] [blame] | 827 | |
| 828 | return 0; |
| 829 | } |
| 830 | |
| 831 | static struct i2c_driver tca6507_driver = { |
| 832 | .driver = { |
| 833 | .name = "leds-tca6507", |
Sachin Kamat | fbd9df2 | 2013-03-14 03:09:49 -0700 | [diff] [blame] | 834 | .of_match_table = of_match_ptr(of_tca6507_leds_match), |
NeilBrown | a6d511e | 2012-01-10 15:09:40 -0800 | [diff] [blame] | 835 | }, |
| 836 | .probe = tca6507_probe, |
Bill Pemberton | df07cf8 | 2012-11-19 13:20:20 -0500 | [diff] [blame] | 837 | .remove = tca6507_remove, |
NeilBrown | a6d511e | 2012-01-10 15:09:40 -0800 | [diff] [blame] | 838 | .id_table = tca6507_id, |
| 839 | }; |
| 840 | |
Peter Meerwald | 2e91176 | 2012-07-03 16:03:38 +0800 | [diff] [blame] | 841 | module_i2c_driver(tca6507_driver); |
NeilBrown | a6d511e | 2012-01-10 15:09:40 -0800 | [diff] [blame] | 842 | |
| 843 | MODULE_AUTHOR("NeilBrown <neilb@suse.de>"); |
| 844 | MODULE_DESCRIPTION("TCA6507 LED/GPO driver"); |
| 845 | MODULE_LICENSE("GPL v2"); |