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