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 blink or
10 * This driver can configure each line either as a 'GPIO' which is out-only
11 * (no pull-up) or as an LED with variable brightness and hardware-assisted
14 * Apart from OFF and ON there are three programmable brightness levels which
15 * can be programmed from 0 to 15 and indicate how many 500usec intervals in
16 * each 8msec that the led is 'on'. The levels are named MASTER, BANK0 and
19 * There are two different blink rates that can be programmed, each with
20 * separate time for rise, on, fall, off and second-off. Thus if 3 or more
21 * different non-trivial rates are required, software must be used for the extra
22 * rates. The two different blink rates must align with the two levels BANK0 and
24 * This driver does not support double-blink so 'second-off' always matches
27 * Only 16 different times can be programmed in a roughly logarithmic scale from
28 * 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
32 * Times that cannot be closely matched with these must be
33 * handled in software. This driver allows 12.5% error in matching.
35 * This driver does not allow rise/fall rates to be set explicitly. When trying
36 * to match a given 'on' or 'off' period, an appropriate pair of 'change' and
37 * 'hold' times are chosen to get a close match. If the target delay is even,
38 * the 'change' number will be the smaller; if odd, the 'hold' number will be
41 * Choosing pairs of delays with 12.5% errors allows us to match delays in the
42 * ranges: 56-72, 112-144, 168-216, 224-27504, 28560-36720.
43 * 26% of the achievable sums can be matched by multiple pairings. For example
44 * 1536 == 1536+0, 1024+512, or 768+768. This driver will always choose the
45 * pairing with the least maximum - 768+768 in this case. Other pairings are
48 * Access to the 3 levels and 2 blinks are on a first-come, first-served basis.
49 * Access can be shared by multiple leds if they have the same level and
50 * either same blink rates, or some don't blink.
51 * When a led changes, it relinquishes access and tries again, so it might
52 * lose access to hardware blink.
53 * If a blink engine cannot be allocated, software blink is used.
54 * If the desired brightness cannot be allocated, the closest available non-zero
55 * brightness is used. As 'full' is always available, the worst case would be
56 * to have two different blink rates at '1', with Max at '2', then other leds
57 * will have to choose between '2' and '16'. Hopefully this is not likely.
59 * Each bank (BANK0 and BANK1) has two usage counts - LEDs using the brightness
60 * and LEDs using the blink. It can only be reprogrammed when the appropriate
61 * counter is zero. The MASTER level has a single usage count.
63 * Each Led has programmable 'on' and 'off' time as milliseconds. With each
64 * there is a flag saying if it was explicitly requested or defaulted.
65 * Similarly the banks know if each time was explicit or a default. Defaults
66 * are permitted to be changed freely - they are not recognised when matching.
69 * An led-tca6507 device must be provided with platform data. This data
70 * lists for each output: the name, default trigger, and whether the signal
71 * is being used as a GPiO rather than an led. 'struct led_plaform_data'
72 * is used for this. If 'name' is NULL, the output isn't used. If 'flags'
73 * is TCA6507_MAKE_CPIO, the output is a GPO.
74 * The "struct led_platform_data" can be embedded in a
75 * "struct tca6507_platform_data" which adds a 'gpio_base' for the GPiOs,
76 * and a 'setup' callback which is called once the GPiOs are available.
80 #include <linux/module.h>
81 #include <linux/slab.h>
82 #include <linux/leds.h>
83 #include <linux/err.h>
84 #include <linux/i2c.h>
85 #include <linux/gpio.h>
86 #include <linux/workqueue.h>
87 #include <linux/leds-tca6507.h>
89 /* LED select registers determine the source that drives LED outputs */
90 #define TCA6507_LS_LED_OFF 0x0 /* Output HI-Z (off) */
91 #define TCA6507_LS_LED_OFF1 0x1 /* Output HI-Z (off) - not used */
92 #define TCA6507_LS_LED_PWM0 0x2 /* Output LOW with Bank0 rate */
93 #define TCA6507_LS_LED_PWM1 0x3 /* Output LOW with Bank1 rate */
94 #define TCA6507_LS_LED_ON 0x4 /* Output LOW (on) */
95 #define TCA6507_LS_LED_MIR 0x5 /* Output LOW with Master Intensity */
96 #define TCA6507_LS_BLINK0 0x6 /* Blink at Bank0 rate */
97 #define TCA6507_LS_BLINK1 0x7 /* Blink at Bank1 rate */
104 static int bank_source
[3] = {
109 static int blink_source
[2] = {
115 #define TCA6507_REG_CNT 11
118 * 0x00, 0x01, 0x02 encode the TCA6507_LS_* values, each output
119 * owns one bit in each register
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
130 #define INIT_CODE 0x8
133 static int time_codes
[TIMECODES
] = {
134 0, 64, 128, 192, 256, 384, 512, 768,
135 1024, 1536, 2048, 3072, 4096, 5760, 8128, 16320
138 /* Convert an led.brightness level (0..255) to a TCA6507 level (0..15) */
139 static inline int TO_LEVEL(int brightness
)
141 return brightness
>> 4;
144 /* ...and convert back */
145 static inline int TO_BRIGHT(int level
)
148 return (level
<< 4) | 0xf;
153 struct 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 */
162 int on_dflt
, off_dflt
;
163 int time_use
, level_use
;
165 struct i2c_client
*client
;
166 struct work_struct work
;
170 struct tca6507_chip
*chip
;
171 struct led_classdev led_cdev
;
174 int on_dflt
, off_dflt
;
175 int bank
; /* Bank used, or -1 */
176 int blink
; /* Set if hardware-blinking */
178 #ifdef CONFIG_GPIOLIB
179 struct gpio_chip gpio
;
180 const char *gpio_name
[NUM_LEDS
];
181 int gpio_map
[NUM_LEDS
];
185 static const struct i2c_device_id tca6507_id
[] = {
189 MODULE_DEVICE_TABLE(i2c
, tca6507_id
);
191 static int choose_times(int msec
, int *c1p
, int *c2p
)
194 * Choose two timecodes which add to 'msec' as near as possible.
195 * The first returned is the 'on' or 'off' time. The second is to be
196 * used as a 'fade-on' or 'fade-off' time. If 'msec' is even,
197 * the first will not be smaller than the second. If 'msec' is odd,
198 * the first will not be larger than the second.
199 * If we cannot get a sum within 1/8 of 'msec' fail with -EINVAL,
200 * otherwise return the sum that was achieved, plus 1 if the first is
202 * If two possibilities are equally good (e.g. 512+0, 256+256), choose
203 * the first pair so there is more change-time visible (i.e. it is
207 int tmax
= msec
* 9 / 8;
208 int tmin
= msec
* 7 / 8;
211 /* We start at '1' to ensure we never even think of choosing a
214 for (c1
= 1; c1
< TIMECODES
; c1
++) {
215 int t
= time_codes
[c1
];
220 for (c2
= 0; c2
<= c1
; c2
++) {
221 int tt
= t
+ time_codes
[c2
];
246 actual
= time_codes
[*c1p
] + time_codes
[*c2p
];
257 * Update the register file with the appropriate 3-bit state for
260 static void set_select(struct tca6507_chip
*tca
, int led
, int val
)
262 int mask
= (1 << led
);
265 for (bit
= 0; bit
< 3; bit
++) {
266 int n
= tca
->reg_file
[bit
] & ~mask
;
267 if (val
& (1 << bit
))
269 if (tca
->reg_file
[bit
] != n
) {
270 tca
->reg_file
[bit
] = n
;
271 tca
->reg_set
|= (1 << bit
);
276 /* Update the register file with the appropriate 4-bit code for
277 * one bank or other. This can be used for timers, for levels, or
278 * for initialisation.
280 static void set_code(struct tca6507_chip
*tca
, int reg
, int bank
, int new)
288 n
= tca
->reg_file
[reg
] & ~mask
;
290 if (tca
->reg_file
[reg
] != n
) {
291 tca
->reg_file
[reg
] = n
;
292 tca
->reg_set
|= 1 << reg
;
296 /* Update brightness level. */
297 static void set_level(struct tca6507_chip
*tca
, int bank
, int level
)
302 set_code(tca
, TCA6507_MAX_INTENSITY
, bank
, level
);
305 set_code(tca
, TCA6507_MASTER_INTENSITY
, 0, level
);
308 tca
->bank
[bank
].level
= level
;
311 /* Record all relevant time code for a given bank */
312 static void set_times(struct tca6507_chip
*tca
, int bank
)
317 result
= choose_times(tca
->bank
[bank
].ontime
, &c1
, &c2
);
318 dev_dbg(&tca
->client
->dev
,
319 "Chose on times %d(%d) %d(%d) for %dms\n", c1
, time_codes
[c1
],
320 c2
, time_codes
[c2
], tca
->bank
[bank
].ontime
);
321 set_code(tca
, TCA6507_FADE_ON
, bank
, c2
);
322 set_code(tca
, TCA6507_FULL_ON
, bank
, c1
);
323 tca
->bank
[bank
].ontime
= result
;
325 result
= choose_times(tca
->bank
[bank
].offtime
, &c1
, &c2
);
326 dev_dbg(&tca
->client
->dev
,
327 "Chose off times %d(%d) %d(%d) for %dms\n", c1
, time_codes
[c1
],
328 c2
, time_codes
[c2
], tca
->bank
[bank
].offtime
);
329 set_code(tca
, TCA6507_FADE_OFF
, bank
, c2
);
330 set_code(tca
, TCA6507_FIRST_OFF
, bank
, c1
);
331 set_code(tca
, TCA6507_SECOND_OFF
, bank
, c1
);
332 tca
->bank
[bank
].offtime
= result
;
334 set_code(tca
, TCA6507_INITIALIZE
, bank
, INIT_CODE
);
337 /* Write all needed register of tca6507 */
339 static void tca6507_work(struct work_struct
*work
)
341 struct tca6507_chip
*tca
= container_of(work
, struct tca6507_chip
,
343 struct i2c_client
*cl
= tca
->client
;
345 u8 file
[TCA6507_REG_CNT
];
348 spin_lock_irq(&tca
->lock
);
350 memcpy(file
, tca
->reg_file
, TCA6507_REG_CNT
);
352 spin_unlock_irq(&tca
->lock
);
354 for (r
= 0; r
< TCA6507_REG_CNT
; r
++)
356 i2c_smbus_write_byte_data(cl
, r
, file
[r
]);
359 static void led_release(struct tca6507_led
*led
)
361 /* If led owns any resource, release it. */
362 struct tca6507_chip
*tca
= led
->chip
;
363 if (led
->bank
>= 0) {
364 struct bank
*b
= tca
->bank
+ led
->bank
;
373 static int led_prepare(struct tca6507_led
*led
)
375 /* Assign this led to a bank, configuring that bank if necessary. */
376 int level
= TO_LEVEL(led
->led_cdev
.brightness
);
377 struct tca6507_chip
*tca
= led
->chip
;
383 led
->led_cdev
.brightness
= TO_BRIGHT(level
);
385 set_select(tca
, led
->num
, TCA6507_LS_LED_OFF
);
389 if (led
->ontime
== 0 || led
->offtime
== 0) {
391 * Just set the brightness, choosing first usable bank.
392 * If none perfect, choose best.
393 * Count backwards so we check MASTER bank first
394 * to avoid wasting a timer.
396 int best
= -1;/* full-on */
400 set_select(tca
, led
->num
, TCA6507_LS_LED_ON
);
404 for (i
= MASTER
; i
>= BANK0
; i
--) {
406 if (tca
->bank
[i
].level
== level
||
407 tca
->bank
[i
].level_use
== 0) {
411 d
= abs(level
- tca
->bank
[i
].level
);
418 /* Best brightness is full-on */
419 set_select(tca
, led
->num
, TCA6507_LS_LED_ON
);
420 led
->led_cdev
.brightness
= LED_FULL
;
424 if (!tca
->bank
[best
].level_use
)
425 set_level(tca
, best
, level
);
427 tca
->bank
[best
].level_use
++;
429 set_select(tca
, led
->num
, bank_source
[best
]);
430 led
->led_cdev
.brightness
= TO_BRIGHT(tca
->bank
[best
].level
);
435 * We have on/off time so we need to try to allocate a timing bank.
436 * First check if times are compatible with hardware and give up if
439 if (choose_times(led
->ontime
, &c1
, &c2
) < 0)
441 if (choose_times(led
->offtime
, &c1
, &c2
) < 0)
444 for (i
= BANK0
; i
<= BANK1
; i
++) {
445 if (tca
->bank
[i
].level_use
== 0)
446 /* not in use - it is ours! */
448 if (tca
->bank
[i
].level
!= level
)
449 /* Incompatible level - skip */
450 /* FIX: if timer matches we maybe should consider
455 if (tca
->bank
[i
].time_use
== 0)
456 /* Timer not in use, and level matches - use it */
459 if (!(tca
->bank
[i
].on_dflt
||
461 tca
->bank
[i
].ontime
== led
->ontime
))
462 /* on time is incompatible */
465 if (!(tca
->bank
[i
].off_dflt
||
467 tca
->bank
[i
].offtime
== led
->offtime
))
468 /* off time is incompatible */
471 /* looks like a suitable match */
476 /* Nothing matches - how sad */
480 if (b
->level_use
== 0)
481 set_level(tca
, i
, level
);
488 b
->ontime
= led
->ontime
;
489 b
->on_dflt
= led
->on_dflt
;
496 b
->offtime
= led
->offtime
;
497 b
->off_dflt
= led
->off_dflt
;
504 led
->ontime
= b
->ontime
;
505 led
->offtime
= b
->offtime
;
509 led
->led_cdev
.brightness
= TO_BRIGHT(b
->level
);
510 set_select(tca
, led
->num
, blink_source
[i
]);
514 static int led_assign(struct tca6507_led
*led
)
516 struct tca6507_chip
*tca
= led
->chip
;
520 spin_lock_irqsave(&tca
->lock
, flags
);
522 err
= led_prepare(led
);
525 * Can only fail on timer setup. In that case we need to
526 * re-establish as steady level.
532 spin_unlock_irqrestore(&tca
->lock
, flags
);
535 schedule_work(&tca
->work
);
539 static void tca6507_brightness_set(struct led_classdev
*led_cdev
,
540 enum led_brightness brightness
)
542 struct tca6507_led
*led
= container_of(led_cdev
, struct tca6507_led
,
544 led
->led_cdev
.brightness
= brightness
;
550 static int tca6507_blink_set(struct led_classdev
*led_cdev
,
551 unsigned long *delay_on
,
552 unsigned long *delay_off
)
554 struct tca6507_led
*led
= container_of(led_cdev
, struct tca6507_led
,
559 else if (delay_on
!= &led_cdev
->blink_delay_on
)
561 led
->ontime
= *delay_on
;
565 else if (delay_off
!= &led_cdev
->blink_delay_off
)
567 led
->offtime
= *delay_off
;
569 if (led
->ontime
== 0)
571 if (led
->offtime
== 0)
574 if (led
->led_cdev
.brightness
== LED_OFF
)
575 led
->led_cdev
.brightness
= LED_FULL
;
576 if (led_assign(led
) < 0) {
579 led
->led_cdev
.brightness
= LED_OFF
;
582 *delay_on
= led
->ontime
;
583 *delay_off
= led
->offtime
;
587 #ifdef CONFIG_GPIOLIB
588 static void tca6507_gpio_set_value(struct gpio_chip
*gc
,
589 unsigned offset
, int val
)
591 struct tca6507_chip
*tca
= container_of(gc
, struct tca6507_chip
, gpio
);
594 spin_lock_irqsave(&tca
->lock
, flags
);
596 * 'OFF' is floating high, and 'ON' is pulled down, so it has the
597 * inverse sense of 'val'.
599 set_select(tca
, tca
->gpio_map
[offset
],
600 val
? TCA6507_LS_LED_OFF
: TCA6507_LS_LED_ON
);
601 spin_unlock_irqrestore(&tca
->lock
, flags
);
603 schedule_work(&tca
->work
);
606 static int tca6507_gpio_direction_output(struct gpio_chip
*gc
,
607 unsigned offset
, int val
)
609 tca6507_gpio_set_value(gc
, offset
, val
);
613 static int tca6507_probe_gpios(struct i2c_client
*client
,
614 struct tca6507_chip
*tca
,
615 struct tca6507_platform_data
*pdata
)
621 for (i
= 0; i
< NUM_LEDS
; i
++)
622 if (pdata
->leds
.leds
[i
].name
&& pdata
->leds
.leds
[i
].flags
) {
623 /* Configure as a gpio */
624 tca
->gpio_name
[gpios
] = pdata
->leds
.leds
[i
].name
;
625 tca
->gpio_map
[gpios
] = i
;
632 tca
->gpio
.label
= "gpio-tca6507";
633 tca
->gpio
.names
= tca
->gpio_name
;
634 tca
->gpio
.ngpio
= gpios
;
635 tca
->gpio
.base
= pdata
->gpio_base
;
636 tca
->gpio
.owner
= THIS_MODULE
;
637 tca
->gpio
.direction_output
= tca6507_gpio_direction_output
;
638 tca
->gpio
.set
= tca6507_gpio_set_value
;
639 tca
->gpio
.dev
= &client
->dev
;
640 err
= gpiochip_add(&tca
->gpio
);
646 pdata
->setup(tca
->gpio
.base
, tca
->gpio
.ngpio
);
650 static void tca6507_remove_gpio(struct tca6507_chip
*tca
)
652 if (tca
->gpio
.ngpio
) {
653 int err
= gpiochip_remove(&tca
->gpio
);
654 dev_err(&tca
->client
->dev
, "%s failed, %d\n",
655 "gpiochip_remove()", err
);
658 #else /* CONFIG_GPIOLIB */
659 static int tca6507_probe_gpios(struct i2c_client
*client
,
660 struct tca6507_chip
*tca
,
661 struct tca6507_platform_data
*pdata
)
665 static void tca6507_remove_gpio(struct tca6507_chip
*tca
)
668 #endif /* CONFIG_GPIOLIB */
670 static int __devinit
tca6507_probe(struct i2c_client
*client
,
671 const struct i2c_device_id
*id
)
673 struct tca6507_chip
*tca
;
674 struct i2c_adapter
*adapter
;
675 struct tca6507_platform_data
*pdata
;
679 adapter
= to_i2c_adapter(client
->dev
.parent
);
680 pdata
= client
->dev
.platform_data
;
682 if (!i2c_check_functionality(adapter
, I2C_FUNC_I2C
))
685 if (!pdata
|| pdata
->leds
.num_leds
!= NUM_LEDS
) {
686 dev_err(&client
->dev
, "Need %d entries in platform-data list\n",
690 tca
= devm_kzalloc(&client
->dev
, sizeof(*tca
), GFP_KERNEL
);
694 tca
->client
= client
;
695 INIT_WORK(&tca
->work
, tca6507_work
);
696 spin_lock_init(&tca
->lock
);
697 i2c_set_clientdata(client
, tca
);
699 for (i
= 0; i
< NUM_LEDS
; i
++) {
700 struct tca6507_led
*l
= tca
->leds
+ i
;
704 if (pdata
->leds
.leds
[i
].name
&& !pdata
->leds
.leds
[i
].flags
) {
705 l
->led_cdev
.name
= pdata
->leds
.leds
[i
].name
;
706 l
->led_cdev
.default_trigger
707 = pdata
->leds
.leds
[i
].default_trigger
;
708 l
->led_cdev
.brightness_set
= tca6507_brightness_set
;
709 l
->led_cdev
.blink_set
= tca6507_blink_set
;
711 err
= led_classdev_register(&client
->dev
,
717 err
= tca6507_probe_gpios(client
, tca
, pdata
);
720 /* set all registers to known state - zero */
722 schedule_work(&tca
->work
);
727 if (tca
->leds
[i
].led_cdev
.name
)
728 led_classdev_unregister(&tca
->leds
[i
].led_cdev
);
733 static int __devexit
tca6507_remove(struct i2c_client
*client
)
736 struct tca6507_chip
*tca
= i2c_get_clientdata(client
);
737 struct tca6507_led
*tca_leds
= tca
->leds
;
739 for (i
= 0; i
< NUM_LEDS
; i
++) {
740 if (tca_leds
[i
].led_cdev
.name
)
741 led_classdev_unregister(&tca_leds
[i
].led_cdev
);
743 tca6507_remove_gpio(tca
);
744 cancel_work_sync(&tca
->work
);
749 static struct i2c_driver tca6507_driver
= {
751 .name
= "leds-tca6507",
752 .owner
= THIS_MODULE
,
754 .probe
= tca6507_probe
,
755 .remove
= __devexit_p(tca6507_remove
),
756 .id_table
= tca6507_id
,
759 module_i2c_driver(tca6507_driver
);
761 MODULE_AUTHOR("NeilBrown <neilb@suse.de>");
762 MODULE_DESCRIPTION("TCA6507 LED/GPO driver");
763 MODULE_LICENSE("GPL v2");