1 // SPDX-License-Identifier: GPL-2.0-only
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.
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.
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.
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'.
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
32 * Times that cannot be closely matched with these must be handled in
33 * software. This driver allows 12.5% error in matching.
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.
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,
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.
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.
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.
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
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.
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>
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 */
93 struct tca6507_platform_data
{
94 struct led_platform_data leds
;
97 #define TCA6507_MAKE_GPIO 1
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 int gpio_map
[NUM_LEDS
];
184 static const struct i2c_device_id tca6507_id
[] = {
188 MODULE_DEVICE_TABLE(i2c
, tca6507_id
);
190 static int choose_times(int msec
, int *c1p
, int *c2p
)
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
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).
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
];
244 actual
= time_codes
[*c1p
] + time_codes
[*c2p
];
255 * Update the register file with the appropriate 3-bit state for the
258 static void set_select(struct tca6507_chip
*tca
, int led
, int val
)
260 int mask
= (1 << led
);
263 for (bit
= 0; bit
< 3; bit
++) {
264 int n
= tca
->reg_file
[bit
] & ~mask
;
265 if (val
& (1 << bit
))
267 if (tca
->reg_file
[bit
] != n
) {
268 tca
->reg_file
[bit
] = n
;
269 tca
->reg_set
|= (1 << bit
);
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
278 static void set_code(struct tca6507_chip
*tca
, int reg
, int bank
, int new)
286 n
= tca
->reg_file
[reg
] & ~mask
;
288 if (tca
->reg_file
[reg
] != n
) {
289 tca
->reg_file
[reg
] = n
;
290 tca
->reg_set
|= 1 << reg
;
294 /* Update brightness level. */
295 static void set_level(struct tca6507_chip
*tca
, int bank
, int level
)
300 set_code(tca
, TCA6507_MAX_INTENSITY
, bank
, level
);
303 set_code(tca
, TCA6507_MASTER_INTENSITY
, 0, level
);
306 tca
->bank
[bank
].level
= level
;
309 /* Record all relevant time codes for a given bank */
310 static void set_times(struct tca6507_chip
*tca
, int bank
)
315 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",
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
;
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",
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
;
336 set_code(tca
, TCA6507_INITIALIZE
, bank
, INIT_CODE
);
339 /* Write all needed register of tca6507 */
341 static void tca6507_work(struct work_struct
*work
)
343 struct tca6507_chip
*tca
= container_of(work
, struct tca6507_chip
,
345 struct i2c_client
*cl
= tca
->client
;
347 u8 file
[TCA6507_REG_CNT
];
350 spin_lock_irq(&tca
->lock
);
352 memcpy(file
, tca
->reg_file
, TCA6507_REG_CNT
);
354 spin_unlock_irq(&tca
->lock
);
356 for (r
= 0; r
< TCA6507_REG_CNT
; r
++)
358 i2c_smbus_write_byte_data(cl
, r
, file
[r
]);
361 static void led_release(struct tca6507_led
*led
)
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
;
375 static int led_prepare(struct tca6507_led
*led
)
377 /* Assign this led to a bank, configuring that bank if
379 int level
= TO_LEVEL(led
->led_cdev
.brightness
);
380 struct tca6507_chip
*tca
= led
->chip
;
386 led
->led_cdev
.brightness
= TO_BRIGHT(level
);
388 set_select(tca
, led
->num
, TCA6507_LS_LED_OFF
);
392 if (led
->ontime
== 0 || led
->offtime
== 0) {
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
399 int best
= -1;/* full-on */
403 set_select(tca
, led
->num
, TCA6507_LS_LED_ON
);
407 for (i
= MASTER
; i
>= BANK0
; i
--) {
409 if (tca
->bank
[i
].level
== level
||
410 tca
->bank
[i
].level_use
== 0) {
414 d
= abs(level
- tca
->bank
[i
].level
);
421 /* Best brightness is full-on */
422 set_select(tca
, led
->num
, TCA6507_LS_LED_ON
);
423 led
->led_cdev
.brightness
= LED_FULL
;
427 if (!tca
->bank
[best
].level_use
)
428 set_level(tca
, best
, level
);
430 tca
->bank
[best
].level_use
++;
432 set_select(tca
, led
->num
, bank_source
[best
]);
433 led
->led_cdev
.brightness
= TO_BRIGHT(tca
->bank
[best
].level
);
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.
442 if (choose_times(led
->ontime
, &c1
, &c2
) < 0)
444 if (choose_times(led
->offtime
, &c1
, &c2
) < 0)
447 for (i
= BANK0
; i
<= BANK1
; i
++) {
448 if (tca
->bank
[i
].level_use
== 0)
449 /* not in use - it is ours! */
451 if (tca
->bank
[i
].level
!= level
)
452 /* Incompatible level - skip */
453 /* FIX: if timer matches we maybe should consider
458 if (tca
->bank
[i
].time_use
== 0)
459 /* Timer not in use, and level matches - use it */
462 if (!(tca
->bank
[i
].on_dflt
||
464 tca
->bank
[i
].ontime
== led
->ontime
))
465 /* on time is incompatible */
468 if (!(tca
->bank
[i
].off_dflt
||
470 tca
->bank
[i
].offtime
== led
->offtime
))
471 /* off time is incompatible */
474 /* looks like a suitable match */
479 /* Nothing matches - how sad */
483 if (b
->level_use
== 0)
484 set_level(tca
, i
, level
);
491 b
->ontime
= led
->ontime
;
492 b
->on_dflt
= led
->on_dflt
;
499 b
->offtime
= led
->offtime
;
500 b
->off_dflt
= led
->off_dflt
;
507 led
->ontime
= b
->ontime
;
508 led
->offtime
= b
->offtime
;
512 led
->led_cdev
.brightness
= TO_BRIGHT(b
->level
);
513 set_select(tca
, led
->num
, blink_source
[i
]);
517 static int led_assign(struct tca6507_led
*led
)
519 struct tca6507_chip
*tca
= led
->chip
;
523 spin_lock_irqsave(&tca
->lock
, flags
);
525 err
= led_prepare(led
);
528 * Can only fail on timer setup. In that case we need
529 * to re-establish as steady level.
535 spin_unlock_irqrestore(&tca
->lock
, flags
);
538 schedule_work(&tca
->work
);
542 static void tca6507_brightness_set(struct led_classdev
*led_cdev
,
543 enum led_brightness brightness
)
545 struct tca6507_led
*led
= container_of(led_cdev
, struct tca6507_led
,
547 led
->led_cdev
.brightness
= brightness
;
553 static int tca6507_blink_set(struct led_classdev
*led_cdev
,
554 unsigned long *delay_on
,
555 unsigned long *delay_off
)
557 struct tca6507_led
*led
= container_of(led_cdev
, struct tca6507_led
,
562 else if (delay_on
!= &led_cdev
->blink_delay_on
)
564 led
->ontime
= *delay_on
;
568 else if (delay_off
!= &led_cdev
->blink_delay_off
)
570 led
->offtime
= *delay_off
;
572 if (led
->ontime
== 0)
574 if (led
->offtime
== 0)
577 if (led
->led_cdev
.brightness
== LED_OFF
)
578 led
->led_cdev
.brightness
= LED_FULL
;
579 if (led_assign(led
) < 0) {
582 led
->led_cdev
.brightness
= LED_OFF
;
585 *delay_on
= led
->ontime
;
586 *delay_off
= led
->offtime
;
590 #ifdef CONFIG_GPIOLIB
591 static void tca6507_gpio_set_value(struct gpio_chip
*gc
,
592 unsigned offset
, int val
)
594 struct tca6507_chip
*tca
= gpiochip_get_data(gc
);
597 spin_lock_irqsave(&tca
->lock
, flags
);
599 * 'OFF' is floating high, and 'ON' is pulled down, so it has
600 * the inverse sense of 'val'.
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
);
606 schedule_work(&tca
->work
);
609 static int tca6507_gpio_direction_output(struct gpio_chip
*gc
,
610 unsigned offset
, int val
)
612 tca6507_gpio_set_value(gc
, offset
, val
);
616 static int tca6507_probe_gpios(struct device
*dev
,
617 struct tca6507_chip
*tca
,
618 struct tca6507_platform_data
*pdata
)
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
;
634 tca
->gpio
.label
= "gpio-tca6507";
635 tca
->gpio
.ngpio
= gpios
;
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
);
648 #else /* CONFIG_GPIOLIB */
649 static int tca6507_probe_gpios(struct device
*dev
,
650 struct tca6507_chip
*tca
,
651 struct tca6507_platform_data
*pdata
)
655 #endif /* CONFIG_GPIOLIB */
657 static struct tca6507_platform_data
*
658 tca6507_led_dt_init(struct device
*dev
)
660 struct tca6507_platform_data
*pdata
;
661 struct fwnode_handle
*child
;
662 struct led_info
*tca_leds
;
665 count
= device_get_child_node_count(dev
);
666 if (!count
|| count
> NUM_LEDS
)
667 return ERR_PTR(-ENODEV
);
669 tca_leds
= devm_kcalloc(dev
, NUM_LEDS
, sizeof(struct led_info
),
672 return ERR_PTR(-ENOMEM
);
674 device_for_each_child_node(dev
, child
) {
679 if (fwnode_property_read_string(child
, "label", &led
.name
))
680 led
.name
= fwnode_get_name(child
);
682 if (fwnode_property_read_string(child
, "linux,default-trigger",
683 &led
.default_trigger
))
684 led
.default_trigger
= NULL
;
687 if (fwnode_device_is_compatible(child
, "gpio"))
688 led
.flags
|= TCA6507_MAKE_GPIO
;
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
);
699 pdata
= devm_kzalloc(dev
, sizeof(struct tca6507_platform_data
),
702 return ERR_PTR(-ENOMEM
);
704 pdata
->leds
.leds
= tca_leds
;
705 pdata
->leds
.num_leds
= NUM_LEDS
;
710 static const struct of_device_id __maybe_unused of_tca6507_leds_match
[] = {
711 { .compatible
= "ti,tca6507", },
714 MODULE_DEVICE_TABLE(of
, of_tca6507_leds_match
);
716 static int tca6507_probe(struct i2c_client
*client
)
718 struct device
*dev
= &client
->dev
;
719 struct i2c_adapter
*adapter
;
720 struct tca6507_chip
*tca
;
721 struct tca6507_platform_data
*pdata
;
725 adapter
= client
->adapter
;
727 if (!i2c_check_functionality(adapter
, I2C_FUNC_I2C
))
730 pdata
= tca6507_led_dt_init(dev
);
732 dev_err(dev
, "Need %d entries in platform-data list\n", NUM_LEDS
);
733 return PTR_ERR(pdata
);
735 tca
= devm_kzalloc(dev
, sizeof(*tca
), GFP_KERNEL
);
739 tca
->client
= client
;
740 INIT_WORK(&tca
->work
, tca6507_work
);
741 spin_lock_init(&tca
->lock
);
742 i2c_set_clientdata(client
, tca
);
744 for (i
= 0; i
< NUM_LEDS
; i
++) {
745 struct tca6507_led
*l
= tca
->leds
+ 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
;
756 err
= devm_led_classdev_register(dev
, &l
->led_cdev
);
761 err
= tca6507_probe_gpios(dev
, tca
, pdata
);
764 /* set all registers to known state - zero */
766 schedule_work(&tca
->work
);
771 static void tca6507_remove(struct i2c_client
*client
)
773 struct tca6507_chip
*tca
= i2c_get_clientdata(client
);
775 cancel_work_sync(&tca
->work
);
778 static struct i2c_driver tca6507_driver
= {
780 .name
= "leds-tca6507",
781 .of_match_table
= of_match_ptr(of_tca6507_leds_match
),
783 .probe
= tca6507_probe
,
784 .remove
= tca6507_remove
,
785 .id_table
= tca6507_id
,
788 module_i2c_driver(tca6507_driver
);
790 MODULE_AUTHOR("NeilBrown <neilb@suse.de>");
791 MODULE_DESCRIPTION("TCA6507 LED/GPO driver");
792 MODULE_LICENSE("GPL v2");