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 * An led-tca6507 device must be provided with platform data or
75 * configured via devicetree.
77 * The platform-data lists for each output: the name, default trigger,
78 * and whether the signal is being used as a GPIO rather than an LED.
79 * 'struct led_plaform_data' is used for this. If 'name' is NULL, the
80 * output isn't used. If 'flags' is TCA6507_MAKE_GPIO, the output is
81 * a GPO. The "struct led_platform_data" can be embedded in a "struct
82 * tca6507_platform_data" which adds a 'gpio_base' for the GPIOs, and
83 * a 'setup' callback which is called once the GPIOs are available.
85 * When configured via devicetree there is one child for each output.
86 * The "reg" determines the output number and "compatible" determines
87 * whether it is an LED or a GPIO. "linux,default-trigger" can set a
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>
101 /* LED select registers determine the source that drives LED outputs */
102 #define TCA6507_LS_LED_OFF 0x0 /* Output HI-Z (off) */
103 #define TCA6507_LS_LED_OFF1 0x1 /* Output HI-Z (off) - not used */
104 #define TCA6507_LS_LED_PWM0 0x2 /* Output LOW with Bank0 rate */
105 #define TCA6507_LS_LED_PWM1 0x3 /* Output LOW with Bank1 rate */
106 #define TCA6507_LS_LED_ON 0x4 /* Output LOW (on) */
107 #define TCA6507_LS_LED_MIR 0x5 /* Output LOW with Master Intensity */
108 #define TCA6507_LS_BLINK0 0x6 /* Blink at Bank0 rate */
109 #define TCA6507_LS_BLINK1 0x7 /* Blink at Bank1 rate */
116 static int bank_source
[3] = {
121 static int blink_source
[2] = {
127 #define TCA6507_REG_CNT 11
130 * 0x00, 0x01, 0x02 encode the TCA6507_LS_* values, each output
131 * owns one bit in each register
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
142 #define INIT_CODE 0x8
145 static int time_codes
[TIMECODES
] = {
146 0, 64, 128, 192, 256, 384, 512, 768,
147 1024, 1536, 2048, 3072, 4096, 5760, 8128, 16320
150 /* Convert an led.brightness level (0..255) to a TCA6507 level (0..15) */
151 static inline int TO_LEVEL(int brightness
)
153 return brightness
>> 4;
156 /* ...and convert back */
157 static inline int TO_BRIGHT(int level
)
160 return (level
<< 4) | 0xf;
165 struct tca6507_chip
{
166 int reg_set
; /* One bit per register where
167 * a '1' means the register
168 * should be written */
169 u8 reg_file
[TCA6507_REG_CNT
];
170 /* Bank 2 is Master Intensity and doesn't use times */
174 int on_dflt
, off_dflt
;
175 int time_use
, level_use
;
177 struct i2c_client
*client
;
178 struct work_struct work
;
182 struct tca6507_chip
*chip
;
183 struct led_classdev led_cdev
;
186 int on_dflt
, off_dflt
;
187 int bank
; /* Bank used, or -1 */
188 int blink
; /* Set if hardware-blinking */
190 #ifdef CONFIG_GPIOLIB
191 struct gpio_chip gpio
;
192 const char *gpio_name
[NUM_LEDS
];
193 int gpio_map
[NUM_LEDS
];
197 static const struct i2c_device_id tca6507_id
[] = {
201 MODULE_DEVICE_TABLE(i2c
, tca6507_id
);
203 static int choose_times(int msec
, int *c1p
, int *c2p
)
206 * Choose two timecodes which add to 'msec' as near as
207 * possible. The first returned is the 'on' or 'off' time.
208 * The second is to be used as a 'fade-on' or 'fade-off' time.
209 * If 'msec' is even, the first will not be smaller than the
210 * second. If 'msec' is odd, the first will not be larger
212 * If we cannot get a sum within 1/8 of 'msec' fail with
213 * -EINVAL, otherwise return the sum that was achieved, plus 1
214 * if the first is smaller.
215 * If two possibilities are equally good (e.g. 512+0,
216 * 256+256), choose the first pair so there is more
217 * change-time visible (i.e. it is softer).
220 int tmax
= msec
* 9 / 8;
221 int tmin
= msec
* 7 / 8;
224 /* We start at '1' to ensure we never even think of choosing a
227 for (c1
= 1; c1
< TIMECODES
; c1
++) {
228 int t
= time_codes
[c1
];
233 for (c2
= 0; c2
<= c1
; c2
++) {
234 int tt
= t
+ time_codes
[c2
];
259 actual
= time_codes
[*c1p
] + time_codes
[*c2p
];
270 * Update the register file with the appropriate 3-bit state for the
273 static void set_select(struct tca6507_chip
*tca
, int led
, int val
)
275 int mask
= (1 << led
);
278 for (bit
= 0; bit
< 3; bit
++) {
279 int n
= tca
->reg_file
[bit
] & ~mask
;
280 if (val
& (1 << bit
))
282 if (tca
->reg_file
[bit
] != n
) {
283 tca
->reg_file
[bit
] = n
;
284 tca
->reg_set
|= (1 << bit
);
289 /* Update the register file with the appropriate 4-bit code for one
290 * bank or other. This can be used for timers, for levels, or for
293 static void set_code(struct tca6507_chip
*tca
, int reg
, int bank
, int new)
301 n
= tca
->reg_file
[reg
] & ~mask
;
303 if (tca
->reg_file
[reg
] != n
) {
304 tca
->reg_file
[reg
] = n
;
305 tca
->reg_set
|= 1 << reg
;
309 /* Update brightness level. */
310 static void set_level(struct tca6507_chip
*tca
, int bank
, int level
)
315 set_code(tca
, TCA6507_MAX_INTENSITY
, bank
, level
);
318 set_code(tca
, TCA6507_MASTER_INTENSITY
, 0, level
);
321 tca
->bank
[bank
].level
= level
;
324 /* Record all relevant time codes for a given bank */
325 static void set_times(struct tca6507_chip
*tca
, int bank
)
330 result
= choose_times(tca
->bank
[bank
].ontime
, &c1
, &c2
);
333 dev_dbg(&tca
->client
->dev
,
334 "Chose on times %d(%d) %d(%d) for %dms\n",
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
;
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",
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
;
351 set_code(tca
, TCA6507_INITIALIZE
, bank
, INIT_CODE
);
354 /* Write all needed register of tca6507 */
356 static void tca6507_work(struct work_struct
*work
)
358 struct tca6507_chip
*tca
= container_of(work
, struct tca6507_chip
,
360 struct i2c_client
*cl
= tca
->client
;
362 u8 file
[TCA6507_REG_CNT
];
365 spin_lock_irq(&tca
->lock
);
367 memcpy(file
, tca
->reg_file
, TCA6507_REG_CNT
);
369 spin_unlock_irq(&tca
->lock
);
371 for (r
= 0; r
< TCA6507_REG_CNT
; r
++)
373 i2c_smbus_write_byte_data(cl
, r
, file
[r
]);
376 static void led_release(struct tca6507_led
*led
)
378 /* If led owns any resource, release it. */
379 struct tca6507_chip
*tca
= led
->chip
;
380 if (led
->bank
>= 0) {
381 struct bank
*b
= tca
->bank
+ led
->bank
;
390 static int led_prepare(struct tca6507_led
*led
)
392 /* Assign this led to a bank, configuring that bank if
394 int level
= TO_LEVEL(led
->led_cdev
.brightness
);
395 struct tca6507_chip
*tca
= led
->chip
;
401 led
->led_cdev
.brightness
= TO_BRIGHT(level
);
403 set_select(tca
, led
->num
, TCA6507_LS_LED_OFF
);
407 if (led
->ontime
== 0 || led
->offtime
== 0) {
409 * Just set the brightness, choosing first usable
410 * bank. If none perfect, choose best. Count
411 * backwards so we check MASTER bank first to avoid
414 int best
= -1;/* full-on */
418 set_select(tca
, led
->num
, TCA6507_LS_LED_ON
);
422 for (i
= MASTER
; i
>= BANK0
; i
--) {
424 if (tca
->bank
[i
].level
== level
||
425 tca
->bank
[i
].level_use
== 0) {
429 d
= abs(level
- tca
->bank
[i
].level
);
436 /* Best brightness is full-on */
437 set_select(tca
, led
->num
, TCA6507_LS_LED_ON
);
438 led
->led_cdev
.brightness
= LED_FULL
;
442 if (!tca
->bank
[best
].level_use
)
443 set_level(tca
, best
, level
);
445 tca
->bank
[best
].level_use
++;
447 set_select(tca
, led
->num
, bank_source
[best
]);
448 led
->led_cdev
.brightness
= TO_BRIGHT(tca
->bank
[best
].level
);
453 * We have on/off time so we need to try to allocate a timing
454 * bank. First check if times are compatible with hardware
455 * and give up if not.
457 if (choose_times(led
->ontime
, &c1
, &c2
) < 0)
459 if (choose_times(led
->offtime
, &c1
, &c2
) < 0)
462 for (i
= BANK0
; i
<= BANK1
; i
++) {
463 if (tca
->bank
[i
].level_use
== 0)
464 /* not in use - it is ours! */
466 if (tca
->bank
[i
].level
!= level
)
467 /* Incompatible level - skip */
468 /* FIX: if timer matches we maybe should consider
473 if (tca
->bank
[i
].time_use
== 0)
474 /* Timer not in use, and level matches - use it */
477 if (!(tca
->bank
[i
].on_dflt
||
479 tca
->bank
[i
].ontime
== led
->ontime
))
480 /* on time is incompatible */
483 if (!(tca
->bank
[i
].off_dflt
||
485 tca
->bank
[i
].offtime
== led
->offtime
))
486 /* off time is incompatible */
489 /* looks like a suitable match */
494 /* Nothing matches - how sad */
498 if (b
->level_use
== 0)
499 set_level(tca
, i
, level
);
506 b
->ontime
= led
->ontime
;
507 b
->on_dflt
= led
->on_dflt
;
514 b
->offtime
= led
->offtime
;
515 b
->off_dflt
= led
->off_dflt
;
522 led
->ontime
= b
->ontime
;
523 led
->offtime
= b
->offtime
;
527 led
->led_cdev
.brightness
= TO_BRIGHT(b
->level
);
528 set_select(tca
, led
->num
, blink_source
[i
]);
532 static int led_assign(struct tca6507_led
*led
)
534 struct tca6507_chip
*tca
= led
->chip
;
538 spin_lock_irqsave(&tca
->lock
, flags
);
540 err
= led_prepare(led
);
543 * Can only fail on timer setup. In that case we need
544 * to re-establish as steady level.
550 spin_unlock_irqrestore(&tca
->lock
, flags
);
553 schedule_work(&tca
->work
);
557 static void tca6507_brightness_set(struct led_classdev
*led_cdev
,
558 enum led_brightness brightness
)
560 struct tca6507_led
*led
= container_of(led_cdev
, struct tca6507_led
,
562 led
->led_cdev
.brightness
= brightness
;
568 static int tca6507_blink_set(struct led_classdev
*led_cdev
,
569 unsigned long *delay_on
,
570 unsigned long *delay_off
)
572 struct tca6507_led
*led
= container_of(led_cdev
, struct tca6507_led
,
577 else if (delay_on
!= &led_cdev
->blink_delay_on
)
579 led
->ontime
= *delay_on
;
583 else if (delay_off
!= &led_cdev
->blink_delay_off
)
585 led
->offtime
= *delay_off
;
587 if (led
->ontime
== 0)
589 if (led
->offtime
== 0)
592 if (led
->led_cdev
.brightness
== LED_OFF
)
593 led
->led_cdev
.brightness
= LED_FULL
;
594 if (led_assign(led
) < 0) {
597 led
->led_cdev
.brightness
= LED_OFF
;
600 *delay_on
= led
->ontime
;
601 *delay_off
= led
->offtime
;
605 #ifdef CONFIG_GPIOLIB
606 static void tca6507_gpio_set_value(struct gpio_chip
*gc
,
607 unsigned offset
, int val
)
609 struct tca6507_chip
*tca
= gpiochip_get_data(gc
);
612 spin_lock_irqsave(&tca
->lock
, flags
);
614 * 'OFF' is floating high, and 'ON' is pulled down, so it has
615 * the inverse sense of 'val'.
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
);
621 schedule_work(&tca
->work
);
624 static int tca6507_gpio_direction_output(struct gpio_chip
*gc
,
625 unsigned offset
, int val
)
627 tca6507_gpio_set_value(gc
, offset
, val
);
631 static int tca6507_probe_gpios(struct i2c_client
*client
,
632 struct tca6507_chip
*tca
,
633 struct tca6507_platform_data
*pdata
)
639 for (i
= 0; i
< NUM_LEDS
; i
++)
640 if (pdata
->leds
.leds
[i
].name
&& pdata
->leds
.leds
[i
].flags
) {
641 /* Configure as a gpio */
642 tca
->gpio_name
[gpios
] = pdata
->leds
.leds
[i
].name
;
643 tca
->gpio_map
[gpios
] = i
;
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
);
661 err
= gpiochip_add_data(&tca
->gpio
, tca
);
667 pdata
->setup(tca
->gpio
.base
, tca
->gpio
.ngpio
);
671 static void tca6507_remove_gpio(struct tca6507_chip
*tca
)
674 gpiochip_remove(&tca
->gpio
);
676 #else /* CONFIG_GPIOLIB */
677 static int tca6507_probe_gpios(struct i2c_client
*client
,
678 struct tca6507_chip
*tca
,
679 struct tca6507_platform_data
*pdata
)
683 static void tca6507_remove_gpio(struct tca6507_chip
*tca
)
686 #endif /* CONFIG_GPIOLIB */
689 static struct tca6507_platform_data
*
690 tca6507_led_dt_init(struct i2c_client
*client
)
692 struct device_node
*np
= client
->dev
.of_node
, *child
;
693 struct tca6507_platform_data
*pdata
;
694 struct led_info
*tca_leds
;
697 count
= of_get_child_count(np
);
698 if (!count
|| count
> NUM_LEDS
)
699 return ERR_PTR(-ENODEV
);
701 tca_leds
= devm_kcalloc(&client
->dev
,
702 NUM_LEDS
, sizeof(struct led_info
), GFP_KERNEL
);
704 return ERR_PTR(-ENOMEM
);
706 for_each_child_of_node(np
, child
) {
712 of_get_property(child
, "label", NULL
) ? : child
->name
;
713 led
.default_trigger
=
714 of_get_property(child
, "linux,default-trigger", NULL
);
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
)
724 pdata
= devm_kzalloc(&client
->dev
,
725 sizeof(struct tca6507_platform_data
), GFP_KERNEL
);
727 return ERR_PTR(-ENOMEM
);
729 pdata
->leds
.leds
= tca_leds
;
730 pdata
->leds
.num_leds
= NUM_LEDS
;
731 #ifdef CONFIG_GPIOLIB
732 pdata
->gpio_base
= -1;
737 static const struct of_device_id of_tca6507_leds_match
[] = {
738 { .compatible
= "ti,tca6507", },
741 MODULE_DEVICE_TABLE(of
, of_tca6507_leds_match
);
744 static struct tca6507_platform_data
*
745 tca6507_led_dt_init(struct i2c_client
*client
)
747 return ERR_PTR(-ENODEV
);
752 static int tca6507_probe(struct i2c_client
*client
,
753 const struct i2c_device_id
*id
)
755 struct tca6507_chip
*tca
;
756 struct i2c_adapter
*adapter
;
757 struct tca6507_platform_data
*pdata
;
761 adapter
= client
->adapter
;
762 pdata
= dev_get_platdata(&client
->dev
);
764 if (!i2c_check_functionality(adapter
, I2C_FUNC_I2C
))
767 if (!pdata
|| pdata
->leds
.num_leds
!= NUM_LEDS
) {
768 pdata
= tca6507_led_dt_init(client
);
770 dev_err(&client
->dev
, "Need %d entries in platform-data list\n",
772 return PTR_ERR(pdata
);
775 tca
= devm_kzalloc(&client
->dev
, sizeof(*tca
), GFP_KERNEL
);
779 tca
->client
= client
;
780 INIT_WORK(&tca
->work
, tca6507_work
);
781 spin_lock_init(&tca
->lock
);
782 i2c_set_clientdata(client
, tca
);
784 for (i
= 0; i
< NUM_LEDS
; i
++) {
785 struct tca6507_led
*l
= tca
->leds
+ 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
;
796 err
= led_classdev_register(&client
->dev
,
802 err
= tca6507_probe_gpios(client
, tca
, pdata
);
805 /* set all registers to known state - zero */
807 schedule_work(&tca
->work
);
812 if (tca
->leds
[i
].led_cdev
.name
)
813 led_classdev_unregister(&tca
->leds
[i
].led_cdev
);
818 static int tca6507_remove(struct i2c_client
*client
)
821 struct tca6507_chip
*tca
= i2c_get_clientdata(client
);
822 struct tca6507_led
*tca_leds
= tca
->leds
;
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
);
828 tca6507_remove_gpio(tca
);
829 cancel_work_sync(&tca
->work
);
834 static struct i2c_driver tca6507_driver
= {
836 .name
= "leds-tca6507",
837 .of_match_table
= of_match_ptr(of_tca6507_leds_match
),
839 .probe
= tca6507_probe
,
840 .remove
= tca6507_remove
,
841 .id_table
= tca6507_id
,
844 module_i2c_driver(tca6507_driver
);
846 MODULE_AUTHOR("NeilBrown <neilb@suse.de>");
847 MODULE_DESCRIPTION("TCA6507 LED/GPO driver");
848 MODULE_LICENSE("GPL v2");