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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
|
/*
* leds-tca6507
*
* The TCA6507 is a programmable LED controller that can drive 7
* separate lines either by holding them low, or by pulsing them
* with modulated width.
* The modulation can be varied in a simple pattern to produce a blink or
* double-blink.
*
* This driver can configure each line either as a 'GPIO' which is out-only
* (no pull-up) or as an LED with variable brightness and hardware-assisted
* blinking.
*
* Apart from OFF and ON there are three programmable brightness levels which
* can be programmed from 0 to 15 and indicate how many 500usec intervals in
* each 8msec that the led is 'on'. The levels are named MASTER, BANK0 and
* BANK1.
*
* There are two different blink rates that can be programmed, each with
* separate time for rise, on, fall, off and second-off. Thus if 3 or more
* different non-trivial rates are required, software must be used for the extra
* rates. The two different blink rates must align with the two levels BANK0 and
* BANK1.
* This driver does not support double-blink so 'second-off' always matches
* 'off'.
*
* Only 16 different times can be programmed in a roughly logarithmic scale from
* 64ms to 16320ms. To be precise the possible times are:
* 0, 64, 128, 192, 256, 384, 512, 768,
* 1024, 1536, 2048, 3072, 4096, 5760, 8128, 16320
*
* Times that cannot be closely matched with these must be
* handled in software. This driver allows 12.5% error in matching.
*
* This driver does not allow rise/fall rates to be set explicitly. When trying
* to match a given 'on' or 'off' period, an appropriate pair of 'change' and
* 'hold' times are chosen to get a close match. If the target delay is even,
* the 'change' number will be the smaller; if odd, the 'hold' number will be
* the smaller.
* Choosing pairs of delays with 12.5% errors allows us to match delays in the
* ranges: 56-72, 112-144, 168-216, 224-27504, 28560-36720.
* 26% of the achievable sums can be matched by multiple pairings. For example
* 1536 == 1536+0, 1024+512, or 768+768. This driver will always choose the
* pairing with the least maximum - 768+768 in this case. Other pairings are
* not available.
*
* Access to the 3 levels and 2 blinks are on a first-come, first-served basis.
* Access can be shared by multiple leds if they have the same level and
* either same blink rates, or some don't blink.
* When a led changes, it relinquishes access and tries again, so it might
* lose access to hardware blink.
* If a blink engine cannot be allocated, software blink is used.
* If the desired brightness cannot be allocated, the closest available non-zero
* brightness is used. As 'full' is always available, the worst case would be
* to have two different blink rates at '1', with Max at '2', then other leds
* will have to choose between '2' and '16'. Hopefully this is not likely.
*
* Each bank (BANK0 and BANK1) has two usage counts - LEDs using the brightness
* and LEDs using the blink. It can only be reprogrammed when the appropriate
* counter is zero. The MASTER level has a single usage count.
*
* Each Led has programmable 'on' and 'off' time as milliseconds. With each
* there is a flag saying if it was explicitly requested or defaulted.
* Similarly the banks know if each time was explicit or a default. Defaults
* are permitted to be changed freely - they are not recognised when matching.
*
*
* An led-tca6507 device must be provided with platform data. This data
* lists for each output: the name, default trigger, and whether the signal
* is being used as a GPiO rather than an led. 'struct led_plaform_data'
* is used for this. If 'name' is NULL, the output isn't used. If 'flags'
* is TCA6507_MAKE_CPIO, the output is a GPO.
* The "struct led_platform_data" can be embedded in a
* "struct tca6507_platform_data" which adds a 'gpio_base' for the GPiOs,
* and a 'setup' callback which is called once the GPiOs are available.
*
*/
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/leds.h>
#include <linux/err.h>
#include <linux/i2c.h>
#include <linux/gpio.h>
#include <linux/workqueue.h>
#include <linux/leds-tca6507.h>
/* LED select registers determine the source that drives LED outputs */
#define TCA6507_LS_LED_OFF 0x0 /* Output HI-Z (off) */
#define TCA6507_LS_LED_OFF1 0x1 /* Output HI-Z (off) - not used */
#define TCA6507_LS_LED_PWM0 0x2 /* Output LOW with Bank0 rate */
#define TCA6507_LS_LED_PWM1 0x3 /* Output LOW with Bank1 rate */
#define TCA6507_LS_LED_ON 0x4 /* Output LOW (on) */
#define TCA6507_LS_LED_MIR 0x5 /* Output LOW with Master Intensity */
#define TCA6507_LS_BLINK0 0x6 /* Blink at Bank0 rate */
#define TCA6507_LS_BLINK1 0x7 /* Blink at Bank1 rate */
enum {
BANK0,
BANK1,
MASTER,
};
static int bank_source[3] = {
TCA6507_LS_LED_PWM0,
TCA6507_LS_LED_PWM1,
TCA6507_LS_LED_MIR,
};
static int blink_source[2] = {
TCA6507_LS_BLINK0,
TCA6507_LS_BLINK1,
};
/* PWM registers */
#define TCA6507_REG_CNT 11
/*
* 0x00, 0x01, 0x02 encode the TCA6507_LS_* values, each output
* owns one bit in each register
*/
#define TCA6507_FADE_ON 0x03
#define TCA6507_FULL_ON 0x04
#define TCA6507_FADE_OFF 0x05
#define TCA6507_FIRST_OFF 0x06
#define TCA6507_SECOND_OFF 0x07
#define TCA6507_MAX_INTENSITY 0x08
#define TCA6507_MASTER_INTENSITY 0x09
#define TCA6507_INITIALIZE 0x0A
#define INIT_CODE 0x8
#define TIMECODES 16
static int time_codes[TIMECODES] = {
0, 64, 128, 192, 256, 384, 512, 768,
1024, 1536, 2048, 3072, 4096, 5760, 8128, 16320
};
/* Convert an led.brightness level (0..255) to a TCA6507 level (0..15) */
static inline int TO_LEVEL(int brightness)
{
return brightness >> 4;
}
/* ...and convert back */
static inline int TO_BRIGHT(int level)
{
if (level)
return (level << 4) | 0xf;
return 0;
}
#define NUM_LEDS 7
struct tca6507_chip {
int reg_set; /* One bit per register where
* a '1' means the register
* should be written */
u8 reg_file[TCA6507_REG_CNT];
/* Bank 2 is Master Intensity and doesn't use times */
struct bank {
int level;
int ontime, offtime;
int on_dflt, off_dflt;
int time_use, level_use;
} bank[3];
struct i2c_client *client;
struct work_struct work;
spinlock_t lock;
struct tca6507_led {
struct tca6507_chip *chip;
struct led_classdev led_cdev;
int num;
int ontime, offtime;
int on_dflt, off_dflt;
int bank; /* Bank used, or -1 */
int blink; /* Set if hardware-blinking */
} leds[NUM_LEDS];
#ifdef CONFIG_GPIOLIB
struct gpio_chip gpio;
const char *gpio_name[NUM_LEDS];
int gpio_map[NUM_LEDS];
#endif
};
static const struct i2c_device_id tca6507_id[] = {
{ "tca6507" },
{ }
};
MODULE_DEVICE_TABLE(i2c, tca6507_id);
static int choose_times(int msec, int *c1p, int *c2p)
{
/*
* Choose two timecodes which add to 'msec' as near as possible.
* The first returned is the 'on' or 'off' time. The second is to be
* used as a 'fade-on' or 'fade-off' time. If 'msec' is even,
* the first will not be smaller than the second. If 'msec' is odd,
* the first will not be larger than the second.
* If we cannot get a sum within 1/8 of 'msec' fail with -EINVAL,
* otherwise return the sum that was achieved, plus 1 if the first is
* smaller.
* If two possibilities are equally good (e.g. 512+0, 256+256), choose
* the first pair so there is more change-time visible (i.e. it is
* softer).
*/
int c1, c2;
int tmax = msec * 9 / 8;
int tmin = msec * 7 / 8;
int diff = 65536;
/* We start at '1' to ensure we never even think of choosing a
* total time of '0'.
*/
for (c1 = 1; c1 < TIMECODES; c1++) {
int t = time_codes[c1];
if (t*2 < tmin)
continue;
if (t > tmax)
break;
for (c2 = 0; c2 <= c1; c2++) {
int tt = t + time_codes[c2];
int d;
if (tt < tmin)
continue;
if (tt > tmax)
break;
/* This works! */
d = abs(msec - tt);
if (d >= diff)
continue;
/* Best yet */
*c1p = c1;
*c2p = c2;
diff = d;
if (d == 0)
return msec;
}
}
if (diff < 65536) {
int actual;
if (msec & 1) {
c1 = *c2p;
*c2p = *c1p;
*c1p = c1;
}
actual = time_codes[*c1p] + time_codes[*c2p];
if (*c1p < *c2p)
return actual + 1;
else
return actual;
}
/* No close match */
return -EINVAL;
}
/*
* Update the register file with the appropriate 3-bit state for
* the given led.
*/
static void set_select(struct tca6507_chip *tca, int led, int val)
|