1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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
101#define TCA6507_LS_LED_OFF 0x0
102#define TCA6507_LS_LED_OFF1 0x1
103#define TCA6507_LS_LED_PWM0 0x2
104#define TCA6507_LS_LED_PWM1 0x3
105#define TCA6507_LS_LED_ON 0x4
106#define TCA6507_LS_LED_MIR 0x5
107#define TCA6507_LS_BLINK0 0x6
108#define TCA6507_LS_BLINK1 0x7
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
126#define TCA6507_REG_CNT 11
127
128
129
130
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
150static inline int TO_LEVEL(int brightness)
151{
152 return brightness >> 4;
153}
154
155
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;
166
167
168 u8 reg_file[TCA6507_REG_CNT];
169
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;
187 int blink;
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
206
207
208
209
210
211
212
213
214
215
216
217
218 int c1, c2;
219 int tmax = msec * 9 / 8;
220 int tmin = msec * 7 / 8;
221 int diff = 65536;
222
223
224
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
240 d = abs(msec - tt);
241 if (d >= diff)
242 continue;
243
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
265 return -EINVAL;
266}
267
268
269
270
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
289
290
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
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
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 dev_dbg(&tca->client->dev,
331 "Chose on times %d(%d) %d(%d) for %dms\n",
332 c1, time_codes[c1],
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,
340 "Chose off times %d(%d) %d(%d) for %dms\n",
341 c1, time_codes[c1],
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
352
353static 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
373static void led_release(struct tca6507_led *led)
374{
375
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
387static int led_prepare(struct tca6507_led *led)
388{
389
390
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
406
407
408
409
410
411 int best = -1;
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
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
450
451
452
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
462 break;
463 if (tca->bank[i].level != level)
464
465
466
467
468 continue;
469
470 if (tca->bank[i].time_use == 0)
471
472 break;
473
474 if (!(tca->bank[i].on_dflt ||
475 led->on_dflt ||
476 tca->bank[i].ontime == led->ontime))
477
478 continue;
479
480 if (!(tca->bank[i].off_dflt ||
481 led->off_dflt ||
482 tca->bank[i].offtime == led->offtime))
483
484 continue;
485
486
487 break;
488 }
489
490 if (i > BANK1)
491
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
529static 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
540
541
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
554static 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
565static 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
603static 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
611
612
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
621static 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
628static 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
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;
655#ifdef CONFIG_OF_GPIO
656 tca->gpio.of_node = of_node_get(client->dev.of_node);
657#endif
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
668static void tca6507_remove_gpio(struct tca6507_chip *tca)
669{
670 if (tca->gpio.ngpio)
671 gpiochip_remove(&tca->gpio);
672}
673#else
674static int tca6507_probe_gpios(struct i2c_client *client,
675 struct tca6507_chip *tca,
676 struct tca6507_platform_data *pdata)
677{
678 return 0;
679}
680static void tca6507_remove_gpio(struct tca6507_chip *tca)
681{
682}
683#endif
684
685#ifdef CONFIG_OF
686static struct tca6507_platform_data *
687tca6507_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;
692 int count;
693
694 count = of_get_child_count(np);
695 if (!count || count > NUM_LEDS)
696 return ERR_PTR(-ENODEV);
697
698 tca_leds = devm_kzalloc(&client->dev,
699 sizeof(struct led_info) * NUM_LEDS, GFP_KERNEL);
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);
712 led.flags = 0;
713 if (of_property_match_string(child, "compatible", "gpio") >= 0)
714 led.flags |= TCA6507_MAKE_GPIO;
715 ret = of_property_read_u32(child, "reg", ®);
716 if (ret != 0 || reg < 0 || reg >= NUM_LEDS)
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;
727 pdata->leds.num_leds = NUM_LEDS;
728#ifdef CONFIG_GPIOLIB
729 pdata->gpio_base = -1;
730#endif
731 return pdata;
732}
733
734static const struct of_device_id of_tca6507_leds_match[] = {
735 { .compatible = "ti,tca6507", },
736 {},
737};
738
739#else
740static struct tca6507_platform_data *
741tca6507_led_dt_init(struct i2c_client *client)
742{
743 return ERR_PTR(-ENODEV);
744}
745
746#endif
747
748static int tca6507_probe(struct i2c_client *client,
749 const struct i2c_device_id *id)
750{
751 struct tca6507_chip *tca;
752 struct i2c_adapter *adapter;
753 struct tca6507_platform_data *pdata;
754 int err;
755 int i = 0;
756
757 adapter = to_i2c_adapter(client->dev.parent);
758 pdata = dev_get_platdata(&client->dev);
759
760 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C))
761 return -EIO;
762
763 if (!pdata || pdata->leds.num_leds != NUM_LEDS) {
764 pdata = tca6507_led_dt_init(client);
765 if (IS_ERR(pdata)) {
766 dev_err(&client->dev, "Need %d entries in platform-data list\n",
767 NUM_LEDS);
768 return PTR_ERR(pdata);
769 }
770 }
771 tca = devm_kzalloc(&client->dev, sizeof(*tca), GFP_KERNEL);
772 if (!tca)
773 return -ENOMEM;
774
775 tca->client = client;
776 INIT_WORK(&tca->work, tca6507_work);
777 spin_lock_init(&tca->lock);
778 i2c_set_clientdata(client, tca);
779
780 for (i = 0; i < NUM_LEDS; i++) {
781 struct tca6507_led *l = tca->leds + i;
782
783 l->chip = tca;
784 l->num = i;
785 if (pdata->leds.leds[i].name && !pdata->leds.leds[i].flags) {
786 l->led_cdev.name = pdata->leds.leds[i].name;
787 l->led_cdev.default_trigger
788 = pdata->leds.leds[i].default_trigger;
789 l->led_cdev.brightness_set = tca6507_brightness_set;
790 l->led_cdev.blink_set = tca6507_blink_set;
791 l->bank = -1;
792 err = led_classdev_register(&client->dev,
793 &l->led_cdev);
794 if (err < 0)
795 goto exit;
796 }
797 }
798 err = tca6507_probe_gpios(client, tca, pdata);
799 if (err)
800 goto exit;
801
802 tca->reg_set = 0x7f;
803 schedule_work(&tca->work);
804
805 return 0;
806exit:
807 while (i--) {
808 if (tca->leds[i].led_cdev.name)
809 led_classdev_unregister(&tca->leds[i].led_cdev);
810 }
811 return err;
812}
813
814static int tca6507_remove(struct i2c_client *client)
815{
816 int i;
817 struct tca6507_chip *tca = i2c_get_clientdata(client);
818 struct tca6507_led *tca_leds = tca->leds;
819
820 for (i = 0; i < NUM_LEDS; i++) {
821 if (tca_leds[i].led_cdev.name)
822 led_classdev_unregister(&tca_leds[i].led_cdev);
823 }
824 tca6507_remove_gpio(tca);
825 cancel_work_sync(&tca->work);
826
827 return 0;
828}
829
830static struct i2c_driver tca6507_driver = {
831 .driver = {
832 .name = "leds-tca6507",
833 .owner = THIS_MODULE,
834 .of_match_table = of_match_ptr(of_tca6507_leds_match),
835 },
836 .probe = tca6507_probe,
837 .remove = tca6507_remove,
838 .id_table = tca6507_id,
839};
840
841module_i2c_driver(tca6507_driver);
842
843MODULE_AUTHOR("NeilBrown <neilb@suse.de>");
844MODULE_DESCRIPTION("TCA6507 LED/GPO driver");
845MODULE_LICENSE("GPL v2");
846