This source file includes following definitions.
- TO_LEVEL
- TO_BRIGHT
- choose_times
- set_select
- set_code
- set_level
- set_times
- tca6507_work
- led_release
- led_prepare
- led_assign
- tca6507_brightness_set
- tca6507_blink_set
- tca6507_gpio_set_value
- tca6507_gpio_direction_output
- tca6507_probe_gpios
- tca6507_remove_gpio
- tca6507_probe_gpios
- tca6507_remove_gpio
- tca6507_led_dt_init
- tca6507_led_dt_init
- tca6507_probe
- tca6507_remove
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
91 #include <linux/module.h>
92 #include <linux/slab.h>
93 #include <linux/leds.h>
94 #include <linux/err.h>
95 #include <linux/i2c.h>
96 #include <linux/gpio.h>
97 #include <linux/workqueue.h>
98 #include <linux/leds-tca6507.h>
99 #include <linux/of.h>
100
101
102 #define TCA6507_LS_LED_OFF 0x0
103 #define TCA6507_LS_LED_OFF1 0x1
104 #define TCA6507_LS_LED_PWM0 0x2
105 #define TCA6507_LS_LED_PWM1 0x3
106 #define TCA6507_LS_LED_ON 0x4
107 #define TCA6507_LS_LED_MIR 0x5
108 #define TCA6507_LS_BLINK0 0x6
109 #define TCA6507_LS_BLINK1 0x7
110
111 enum {
112 BANK0,
113 BANK1,
114 MASTER,
115 };
116 static int bank_source[3] = {
117 TCA6507_LS_LED_PWM0,
118 TCA6507_LS_LED_PWM1,
119 TCA6507_LS_LED_MIR,
120 };
121 static int blink_source[2] = {
122 TCA6507_LS_BLINK0,
123 TCA6507_LS_BLINK1,
124 };
125
126
127 #define TCA6507_REG_CNT 11
128
129
130
131
132
133 #define TCA6507_FADE_ON 0x03
134 #define TCA6507_FULL_ON 0x04
135 #define TCA6507_FADE_OFF 0x05
136 #define TCA6507_FIRST_OFF 0x06
137 #define TCA6507_SECOND_OFF 0x07
138 #define TCA6507_MAX_INTENSITY 0x08
139 #define TCA6507_MASTER_INTENSITY 0x09
140 #define TCA6507_INITIALIZE 0x0A
141
142 #define INIT_CODE 0x8
143
144 #define TIMECODES 16
145 static int time_codes[TIMECODES] = {
146 0, 64, 128, 192, 256, 384, 512, 768,
147 1024, 1536, 2048, 3072, 4096, 5760, 8128, 16320
148 };
149
150
151 static inline int TO_LEVEL(int brightness)
152 {
153 return brightness >> 4;
154 }
155
156
157 static inline int TO_BRIGHT(int level)
158 {
159 if (level)
160 return (level << 4) | 0xf;
161 return 0;
162 }
163
164 #define NUM_LEDS 7
165 struct tca6507_chip {
166 int reg_set;
167
168
169 u8 reg_file[TCA6507_REG_CNT];
170
171 struct bank {
172 int level;
173 int ontime, offtime;
174 int on_dflt, off_dflt;
175 int time_use, level_use;
176 } bank[3];
177 struct i2c_client *client;
178 struct work_struct work;
179 spinlock_t lock;
180
181 struct tca6507_led {
182 struct tca6507_chip *chip;
183 struct led_classdev led_cdev;
184 int num;
185 int ontime, offtime;
186 int on_dflt, off_dflt;
187 int bank;
188 int blink;
189 } leds[NUM_LEDS];
190 #ifdef CONFIG_GPIOLIB
191 struct gpio_chip gpio;
192 const char *gpio_name[NUM_LEDS];
193 int gpio_map[NUM_LEDS];
194 #endif
195 };
196
197 static const struct i2c_device_id tca6507_id[] = {
198 { "tca6507" },
199 { }
200 };
201 MODULE_DEVICE_TABLE(i2c, tca6507_id);
202
203 static int choose_times(int msec, int *c1p, int *c2p)
204 {
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219 int c1, c2;
220 int tmax = msec * 9 / 8;
221 int tmin = msec * 7 / 8;
222 int diff = 65536;
223
224
225
226
227 for (c1 = 1; c1 < TIMECODES; c1++) {
228 int t = time_codes[c1];
229 if (t*2 < tmin)
230 continue;
231 if (t > tmax)
232 break;
233 for (c2 = 0; c2 <= c1; c2++) {
234 int tt = t + time_codes[c2];
235 int d;
236 if (tt < tmin)
237 continue;
238 if (tt > tmax)
239 break;
240
241 d = abs(msec - tt);
242 if (d >= diff)
243 continue;
244
245 *c1p = c1;
246 *c2p = c2;
247 diff = d;
248 if (d == 0)
249 return msec;
250 }
251 }
252 if (diff < 65536) {
253 int actual;
254 if (msec & 1) {
255 c1 = *c2p;
256 *c2p = *c1p;
257 *c1p = c1;
258 }
259 actual = time_codes[*c1p] + time_codes[*c2p];
260 if (*c1p < *c2p)
261 return actual + 1;
262 else
263 return actual;
264 }
265
266 return -EINVAL;
267 }
268
269
270
271
272
273 static void set_select(struct tca6507_chip *tca, int led, int val)
274 {
275 int mask = (1 << led);
276 int bit;
277
278 for (bit = 0; bit < 3; bit++) {
279 int n = tca->reg_file[bit] & ~mask;
280 if (val & (1 << bit))
281 n |= mask;
282 if (tca->reg_file[bit] != n) {
283 tca->reg_file[bit] = n;
284 tca->reg_set |= (1 << bit);
285 }
286 }
287 }
288
289
290
291
292
293 static void set_code(struct tca6507_chip *tca, int reg, int bank, int new)
294 {
295 int mask = 0xF;
296 int n;
297 if (bank) {
298 mask <<= 4;
299 new <<= 4;
300 }
301 n = tca->reg_file[reg] & ~mask;
302 n |= new;
303 if (tca->reg_file[reg] != n) {
304 tca->reg_file[reg] = n;
305 tca->reg_set |= 1 << reg;
306 }
307 }
308
309
310 static void set_level(struct tca6507_chip *tca, int bank, int level)
311 {
312 switch (bank) {
313 case BANK0:
314 case BANK1:
315 set_code(tca, TCA6507_MAX_INTENSITY, bank, level);
316 break;
317 case MASTER:
318 set_code(tca, TCA6507_MASTER_INTENSITY, 0, level);
319 break;
320 }
321 tca->bank[bank].level = level;
322 }
323
324
325 static void set_times(struct tca6507_chip *tca, int bank)
326 {
327 int c1, c2;
328 int result;
329
330 result = choose_times(tca->bank[bank].ontime, &c1, &c2);
331 if (result < 0)
332 return;
333 dev_dbg(&tca->client->dev,
334 "Chose on times %d(%d) %d(%d) for %dms\n",
335 c1, time_codes[c1],
336 c2, time_codes[c2], tca->bank[bank].ontime);
337 set_code(tca, TCA6507_FADE_ON, bank, c2);
338 set_code(tca, TCA6507_FULL_ON, bank, c1);
339 tca->bank[bank].ontime = result;
340
341 result = choose_times(tca->bank[bank].offtime, &c1, &c2);
342 dev_dbg(&tca->client->dev,
343 "Chose off times %d(%d) %d(%d) for %dms\n",
344 c1, time_codes[c1],
345 c2, time_codes[c2], tca->bank[bank].offtime);
346 set_code(tca, TCA6507_FADE_OFF, bank, c2);
347 set_code(tca, TCA6507_FIRST_OFF, bank, c1);
348 set_code(tca, TCA6507_SECOND_OFF, bank, c1);
349 tca->bank[bank].offtime = result;
350
351 set_code(tca, TCA6507_INITIALIZE, bank, INIT_CODE);
352 }
353
354
355
356 static void tca6507_work(struct work_struct *work)
357 {
358 struct tca6507_chip *tca = container_of(work, struct tca6507_chip,
359 work);
360 struct i2c_client *cl = tca->client;
361 int set;
362 u8 file[TCA6507_REG_CNT];
363 int r;
364
365 spin_lock_irq(&tca->lock);
366 set = tca->reg_set;
367 memcpy(file, tca->reg_file, TCA6507_REG_CNT);
368 tca->reg_set = 0;
369 spin_unlock_irq(&tca->lock);
370
371 for (r = 0; r < TCA6507_REG_CNT; r++)
372 if (set & (1<<r))
373 i2c_smbus_write_byte_data(cl, r, file[r]);
374 }
375
376 static void led_release(struct tca6507_led *led)
377 {
378
379 struct tca6507_chip *tca = led->chip;
380 if (led->bank >= 0) {
381 struct bank *b = tca->bank + led->bank;
382 if (led->blink)
383 b->time_use--;
384 b->level_use--;
385 }
386 led->blink = 0;
387 led->bank = -1;
388 }
389
390 static int led_prepare(struct tca6507_led *led)
391 {
392
393
394 int level = TO_LEVEL(led->led_cdev.brightness);
395 struct tca6507_chip *tca = led->chip;
396 int c1, c2;
397 int i;
398 struct bank *b;
399 int need_init = 0;
400
401 led->led_cdev.brightness = TO_BRIGHT(level);
402 if (level == 0) {
403 set_select(tca, led->num, TCA6507_LS_LED_OFF);
404 return 0;
405 }
406
407 if (led->ontime == 0 || led->offtime == 0) {
408
409
410
411
412
413
414 int best = -1;
415 int diff = 15-level;
416
417 if (level == 15) {
418 set_select(tca, led->num, TCA6507_LS_LED_ON);
419 return 0;
420 }
421
422 for (i = MASTER; i >= BANK0; i--) {
423 int d;
424 if (tca->bank[i].level == level ||
425 tca->bank[i].level_use == 0) {
426 best = i;
427 break;
428 }
429 d = abs(level - tca->bank[i].level);
430 if (d < diff) {
431 diff = d;
432 best = i;
433 }
434 }
435 if (best == -1) {
436
437 set_select(tca, led->num, TCA6507_LS_LED_ON);
438 led->led_cdev.brightness = LED_FULL;
439 return 0;
440 }
441
442 if (!tca->bank[best].level_use)
443 set_level(tca, best, level);
444
445 tca->bank[best].level_use++;
446 led->bank = best;
447 set_select(tca, led->num, bank_source[best]);
448 led->led_cdev.brightness = TO_BRIGHT(tca->bank[best].level);
449 return 0;
450 }
451
452
453
454
455
456
457 if (choose_times(led->ontime, &c1, &c2) < 0)
458 return -EINVAL;
459 if (choose_times(led->offtime, &c1, &c2) < 0)
460 return -EINVAL;
461
462 for (i = BANK0; i <= BANK1; i++) {
463 if (tca->bank[i].level_use == 0)
464
465 break;
466 if (tca->bank[i].level != level)
467
468
469
470
471 continue;
472
473 if (tca->bank[i].time_use == 0)
474
475 break;
476
477 if (!(tca->bank[i].on_dflt ||
478 led->on_dflt ||
479 tca->bank[i].ontime == led->ontime))
480
481 continue;
482
483 if (!(tca->bank[i].off_dflt ||
484 led->off_dflt ||
485 tca->bank[i].offtime == led->offtime))
486
487 continue;
488
489
490 break;
491 }
492
493 if (i > BANK1)
494
495 return -EINVAL;
496
497 b = &tca->bank[i];
498 if (b->level_use == 0)
499 set_level(tca, i, level);
500 b->level_use++;
501 led->bank = i;
502
503 if (b->on_dflt ||
504 !led->on_dflt ||
505 b->time_use == 0) {
506 b->ontime = led->ontime;
507 b->on_dflt = led->on_dflt;
508 need_init = 1;
509 }
510
511 if (b->off_dflt ||
512 !led->off_dflt ||
513 b->time_use == 0) {
514 b->offtime = led->offtime;
515 b->off_dflt = led->off_dflt;
516 need_init = 1;
517 }
518
519 if (need_init)
520 set_times(tca, i);
521
522 led->ontime = b->ontime;
523 led->offtime = b->offtime;
524
525 b->time_use++;
526 led->blink = 1;
527 led->led_cdev.brightness = TO_BRIGHT(b->level);
528 set_select(tca, led->num, blink_source[i]);
529 return 0;
530 }
531
532 static int led_assign(struct tca6507_led *led)
533 {
534 struct tca6507_chip *tca = led->chip;
535 int err;
536 unsigned long flags;
537
538 spin_lock_irqsave(&tca->lock, flags);
539 led_release(led);
540 err = led_prepare(led);
541 if (err) {
542
543
544
545
546 led->ontime = 0;
547 led->offtime = 0;
548 led_prepare(led);
549 }
550 spin_unlock_irqrestore(&tca->lock, flags);
551
552 if (tca->reg_set)
553 schedule_work(&tca->work);
554 return err;
555 }
556
557 static void tca6507_brightness_set(struct led_classdev *led_cdev,
558 enum led_brightness brightness)
559 {
560 struct tca6507_led *led = container_of(led_cdev, struct tca6507_led,
561 led_cdev);
562 led->led_cdev.brightness = brightness;
563 led->ontime = 0;
564 led->offtime = 0;
565 led_assign(led);
566 }
567
568 static int tca6507_blink_set(struct led_classdev *led_cdev,
569 unsigned long *delay_on,
570 unsigned long *delay_off)
571 {
572 struct tca6507_led *led = container_of(led_cdev, struct tca6507_led,
573 led_cdev);
574
575 if (*delay_on == 0)
576 led->on_dflt = 1;
577 else if (delay_on != &led_cdev->blink_delay_on)
578 led->on_dflt = 0;
579 led->ontime = *delay_on;
580
581 if (*delay_off == 0)
582 led->off_dflt = 1;
583 else if (delay_off != &led_cdev->blink_delay_off)
584 led->off_dflt = 0;
585 led->offtime = *delay_off;
586
587 if (led->ontime == 0)
588 led->ontime = 512;
589 if (led->offtime == 0)
590 led->offtime = 512;
591
592 if (led->led_cdev.brightness == LED_OFF)
593 led->led_cdev.brightness = LED_FULL;
594 if (led_assign(led) < 0) {
595 led->ontime = 0;
596 led->offtime = 0;
597 led->led_cdev.brightness = LED_OFF;
598 return -EINVAL;
599 }
600 *delay_on = led->ontime;
601 *delay_off = led->offtime;
602 return 0;
603 }
604
605 #ifdef CONFIG_GPIOLIB
606 static void tca6507_gpio_set_value(struct gpio_chip *gc,
607 unsigned offset, int val)
608 {
609 struct tca6507_chip *tca = gpiochip_get_data(gc);
610 unsigned long flags;
611
612 spin_lock_irqsave(&tca->lock, flags);
613
614
615
616
617 set_select(tca, tca->gpio_map[offset],
618 val ? TCA6507_LS_LED_OFF : TCA6507_LS_LED_ON);
619 spin_unlock_irqrestore(&tca->lock, flags);
620 if (tca->reg_set)
621 schedule_work(&tca->work);
622 }
623
624 static int tca6507_gpio_direction_output(struct gpio_chip *gc,
625 unsigned offset, int val)
626 {
627 tca6507_gpio_set_value(gc, offset, val);
628 return 0;
629 }
630
631 static int tca6507_probe_gpios(struct i2c_client *client,
632 struct tca6507_chip *tca,
633 struct tca6507_platform_data *pdata)
634 {
635 int err;
636 int i = 0;
637 int gpios = 0;
638
639 for (i = 0; i < NUM_LEDS; i++)
640 if (pdata->leds.leds[i].name && pdata->leds.leds[i].flags) {
641
642 tca->gpio_name[gpios] = pdata->leds.leds[i].name;
643 tca->gpio_map[gpios] = i;
644 gpios++;
645 }
646
647 if (!gpios)
648 return 0;
649
650 tca->gpio.label = "gpio-tca6507";
651 tca->gpio.names = tca->gpio_name;
652 tca->gpio.ngpio = gpios;
653 tca->gpio.base = pdata->gpio_base;
654 tca->gpio.owner = THIS_MODULE;
655 tca->gpio.direction_output = tca6507_gpio_direction_output;
656 tca->gpio.set = tca6507_gpio_set_value;
657 tca->gpio.parent = &client->dev;
658 #ifdef CONFIG_OF_GPIO
659 tca->gpio.of_node = of_node_get(client->dev.of_node);
660 #endif
661 err = gpiochip_add_data(&tca->gpio, tca);
662 if (err) {
663 tca->gpio.ngpio = 0;
664 return err;
665 }
666 if (pdata->setup)
667 pdata->setup(tca->gpio.base, tca->gpio.ngpio);
668 return 0;
669 }
670
671 static void tca6507_remove_gpio(struct tca6507_chip *tca)
672 {
673 if (tca->gpio.ngpio)
674 gpiochip_remove(&tca->gpio);
675 }
676 #else
677 static int tca6507_probe_gpios(struct i2c_client *client,
678 struct tca6507_chip *tca,
679 struct tca6507_platform_data *pdata)
680 {
681 return 0;
682 }
683 static void tca6507_remove_gpio(struct tca6507_chip *tca)
684 {
685 }
686 #endif
687
688 #ifdef CONFIG_OF
689 static struct tca6507_platform_data *
690 tca6507_led_dt_init(struct i2c_client *client)
691 {
692 struct device_node *np = client->dev.of_node, *child;
693 struct tca6507_platform_data *pdata;
694 struct led_info *tca_leds;
695 int count;
696
697 count = of_get_child_count(np);
698 if (!count || count > NUM_LEDS)
699 return ERR_PTR(-ENODEV);
700
701 tca_leds = devm_kcalloc(&client->dev,
702 NUM_LEDS, sizeof(struct led_info), GFP_KERNEL);
703 if (!tca_leds)
704 return ERR_PTR(-ENOMEM);
705
706 for_each_child_of_node(np, child) {
707 struct led_info led;
708 u32 reg;
709 int ret;
710
711 led.name =
712 of_get_property(child, "label", NULL) ? : child->name;
713 led.default_trigger =
714 of_get_property(child, "linux,default-trigger", NULL);
715 led.flags = 0;
716 if (of_property_match_string(child, "compatible", "gpio") >= 0)
717 led.flags |= TCA6507_MAKE_GPIO;
718 ret = of_property_read_u32(child, "reg", ®);
719 if (ret != 0 || reg >= NUM_LEDS)
720 continue;
721
722 tca_leds[reg] = led;
723 }
724 pdata = devm_kzalloc(&client->dev,
725 sizeof(struct tca6507_platform_data), GFP_KERNEL);
726 if (!pdata)
727 return ERR_PTR(-ENOMEM);
728
729 pdata->leds.leds = tca_leds;
730 pdata->leds.num_leds = NUM_LEDS;
731 #ifdef CONFIG_GPIOLIB
732 pdata->gpio_base = -1;
733 #endif
734 return pdata;
735 }
736
737 static const struct of_device_id of_tca6507_leds_match[] = {
738 { .compatible = "ti,tca6507", },
739 {},
740 };
741 MODULE_DEVICE_TABLE(of, of_tca6507_leds_match);
742
743 #else
744 static struct tca6507_platform_data *
745 tca6507_led_dt_init(struct i2c_client *client)
746 {
747 return ERR_PTR(-ENODEV);
748 }
749
750 #endif
751
752 static int tca6507_probe(struct i2c_client *client,
753 const struct i2c_device_id *id)
754 {
755 struct tca6507_chip *tca;
756 struct i2c_adapter *adapter;
757 struct tca6507_platform_data *pdata;
758 int err;
759 int i = 0;
760
761 adapter = client->adapter;
762 pdata = dev_get_platdata(&client->dev);
763
764 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C))
765 return -EIO;
766
767 if (!pdata || pdata->leds.num_leds != NUM_LEDS) {
768 pdata = tca6507_led_dt_init(client);
769 if (IS_ERR(pdata)) {
770 dev_err(&client->dev, "Need %d entries in platform-data list\n",
771 NUM_LEDS);
772 return PTR_ERR(pdata);
773 }
774 }
775 tca = devm_kzalloc(&client->dev, sizeof(*tca), GFP_KERNEL);
776 if (!tca)
777 return -ENOMEM;
778
779 tca->client = client;
780 INIT_WORK(&tca->work, tca6507_work);
781 spin_lock_init(&tca->lock);
782 i2c_set_clientdata(client, tca);
783
784 for (i = 0; i < NUM_LEDS; i++) {
785 struct tca6507_led *l = tca->leds + i;
786
787 l->chip = tca;
788 l->num = i;
789 if (pdata->leds.leds[i].name && !pdata->leds.leds[i].flags) {
790 l->led_cdev.name = pdata->leds.leds[i].name;
791 l->led_cdev.default_trigger
792 = pdata->leds.leds[i].default_trigger;
793 l->led_cdev.brightness_set = tca6507_brightness_set;
794 l->led_cdev.blink_set = tca6507_blink_set;
795 l->bank = -1;
796 err = led_classdev_register(&client->dev,
797 &l->led_cdev);
798 if (err < 0)
799 goto exit;
800 }
801 }
802 err = tca6507_probe_gpios(client, tca, pdata);
803 if (err)
804 goto exit;
805
806 tca->reg_set = 0x7f;
807 schedule_work(&tca->work);
808
809 return 0;
810 exit:
811 while (i--) {
812 if (tca->leds[i].led_cdev.name)
813 led_classdev_unregister(&tca->leds[i].led_cdev);
814 }
815 return err;
816 }
817
818 static int tca6507_remove(struct i2c_client *client)
819 {
820 int i;
821 struct tca6507_chip *tca = i2c_get_clientdata(client);
822 struct tca6507_led *tca_leds = tca->leds;
823
824 for (i = 0; i < NUM_LEDS; i++) {
825 if (tca_leds[i].led_cdev.name)
826 led_classdev_unregister(&tca_leds[i].led_cdev);
827 }
828 tca6507_remove_gpio(tca);
829 cancel_work_sync(&tca->work);
830
831 return 0;
832 }
833
834 static struct i2c_driver tca6507_driver = {
835 .driver = {
836 .name = "leds-tca6507",
837 .of_match_table = of_match_ptr(of_tca6507_leds_match),
838 },
839 .probe = tca6507_probe,
840 .remove = tca6507_remove,
841 .id_table = tca6507_id,
842 };
843
844 module_i2c_driver(tca6507_driver);
845
846 MODULE_AUTHOR("NeilBrown <neilb@suse.de>");
847 MODULE_DESCRIPTION("TCA6507 LED/GPO driver");
848 MODULE_LICENSE("GPL v2");