blob: 161bef65c6b7b88f19b5f2340a0a34cca336c4c7 [file] [log] [blame]
Thomas Gleixner09c434b2019-05-19 13:08:20 +01001// SPDX-License-Identifier: GPL-2.0-only
NeilBrowna6d511e2012-01-10 15:09:40 -08002/*
3 * leds-tca6507
4 *
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.
NeilBrown1f431af2013-11-12 21:52:43 -08008 * The modulation can be varied in a simple pattern to produce a
9 * blink or double-blink.
NeilBrowna6d511e2012-01-10 15:09:40 -080010 *
NeilBrown1f431af2013-11-12 21:52:43 -080011 * 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.
NeilBrowna6d511e2012-01-10 15:09:40 -080014 *
NeilBrown1f431af2013-11-12 21:52:43 -080015 * 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.
NeilBrowna6d511e2012-01-10 15:09:40 -080019 *
NeilBrown1f431af2013-11-12 21:52:43 -080020 * 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'.
NeilBrowna6d511e2012-01-10 15:09:40 -080026 *
NeilBrown1f431af2013-11-12 21:52:43 -080027 * Only 16 different times can be programmed in a roughly logarithmic
28 * scale from 64ms to 16320ms. To be precise the possible times are:
NeilBrowna6d511e2012-01-10 15:09:40 -080029 * 0, 64, 128, 192, 256, 384, 512, 768,
30 * 1024, 1536, 2048, 3072, 4096, 5760, 8128, 16320
31 *
NeilBrown1f431af2013-11-12 21:52:43 -080032 * Times that cannot be closely matched with these must be handled in
33 * software. This driver allows 12.5% error in matching.
NeilBrowna6d511e2012-01-10 15:09:40 -080034 *
NeilBrown1f431af2013-11-12 21:52:43 -080035 * 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.
NeilBrowna6d511e2012-01-10 15:09:40 -080040
NeilBrown1f431af2013-11-12 21:52:43 -080041 * 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,
43 * 28560-36720.
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.
NeilBrowna6d511e2012-01-10 15:09:40 -080048 *
NeilBrown1f431af2013-11-12 21:52:43 -080049 * 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.
NeilBrowna6d511e2012-01-10 15:09:40 -080054 *
NeilBrown1f431af2013-11-12 21:52:43 -080055 * 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.
NeilBrowna6d511e2012-01-10 15:09:40 -080061 *
NeilBrown1f431af2013-11-12 21:52:43 -080062 * 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
65 * single usage count.
66 *
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.
NeilBrowna6d511e2012-01-10 15:09:40 -080072 */
73
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>
Linus Walleijca995222020-04-15 17:08:37 +020079#include <linux/gpio/driver.h>
Marek Behún96f52412020-09-20 00:15:37 +020080#include <linux/property.h>
NeilBrowna6d511e2012-01-10 15:09:40 -080081#include <linux/workqueue.h>
NeilBrowna6d511e2012-01-10 15:09:40 -080082
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 */
92
Marek Behún38b393f2020-09-20 00:15:36 +020093struct tca6507_platform_data {
94 struct led_platform_data leds;
95#ifdef CONFIG_GPIOLIB
96 int gpio_base;
Marek Behún38b393f2020-09-20 00:15:36 +020097#endif
98};
99
100#define TCA6507_MAKE_GPIO 1
101
NeilBrowna6d511e2012-01-10 15:09:40 -0800102enum {
103 BANK0,
104 BANK1,
105 MASTER,
106};
107static int bank_source[3] = {
108 TCA6507_LS_LED_PWM0,
109 TCA6507_LS_LED_PWM1,
110 TCA6507_LS_LED_MIR,
111};
112static int blink_source[2] = {
113 TCA6507_LS_BLINK0,
114 TCA6507_LS_BLINK1,
115};
116
117/* PWM registers */
118#define TCA6507_REG_CNT 11
119
120/*
121 * 0x00, 0x01, 0x02 encode the TCA6507_LS_* values, each output
122 * owns one bit in each register
123 */
124#define TCA6507_FADE_ON 0x03
125#define TCA6507_FULL_ON 0x04
126#define TCA6507_FADE_OFF 0x05
127#define TCA6507_FIRST_OFF 0x06
128#define TCA6507_SECOND_OFF 0x07
129#define TCA6507_MAX_INTENSITY 0x08
130#define TCA6507_MASTER_INTENSITY 0x09
131#define TCA6507_INITIALIZE 0x0A
132
133#define INIT_CODE 0x8
134
135#define TIMECODES 16
136static int time_codes[TIMECODES] = {
137 0, 64, 128, 192, 256, 384, 512, 768,
138 1024, 1536, 2048, 3072, 4096, 5760, 8128, 16320
139};
140
141/* Convert an led.brightness level (0..255) to a TCA6507 level (0..15) */
142static inline int TO_LEVEL(int brightness)
143{
144 return brightness >> 4;
145}
146
147/* ...and convert back */
148static inline int TO_BRIGHT(int level)
149{
150 if (level)
151 return (level << 4) | 0xf;
152 return 0;
153}
154
155#define NUM_LEDS 7
156struct tca6507_chip {
157 int reg_set; /* One bit per register where
158 * a '1' means the register
159 * should be written */
160 u8 reg_file[TCA6507_REG_CNT];
161 /* Bank 2 is Master Intensity and doesn't use times */
162 struct bank {
163 int level;
164 int ontime, offtime;
165 int on_dflt, off_dflt;
166 int time_use, level_use;
167 } bank[3];
168 struct i2c_client *client;
169 struct work_struct work;
170 spinlock_t lock;
171
172 struct tca6507_led {
173 struct tca6507_chip *chip;
174 struct led_classdev led_cdev;
175 int num;
176 int ontime, offtime;
177 int on_dflt, off_dflt;
178 int bank; /* Bank used, or -1 */
179 int blink; /* Set if hardware-blinking */
180 } leds[NUM_LEDS];
181#ifdef CONFIG_GPIOLIB
182 struct gpio_chip gpio;
NeilBrowna6d511e2012-01-10 15:09:40 -0800183 int gpio_map[NUM_LEDS];
184#endif
185};
186
187static const struct i2c_device_id tca6507_id[] = {
188 { "tca6507" },
189 { }
190};
191MODULE_DEVICE_TABLE(i2c, tca6507_id);
192
193static int choose_times(int msec, int *c1p, int *c2p)
194{
195 /*
NeilBrown1f431af2013-11-12 21:52:43 -0800196 * Choose two timecodes which add to 'msec' as near as
197 * possible. The first returned is the 'on' or 'off' time.
198 * The second is to be used as a 'fade-on' or 'fade-off' time.
199 * If 'msec' is even, the first will not be smaller than the
200 * second. If 'msec' is odd, the first will not be larger
201 * than the second.
202 * If we cannot get a sum within 1/8 of 'msec' fail with
203 * -EINVAL, otherwise return the sum that was achieved, plus 1
204 * if the first is smaller.
205 * If two possibilities are equally good (e.g. 512+0,
206 * 256+256), choose the first pair so there is more
207 * change-time visible (i.e. it is softer).
NeilBrowna6d511e2012-01-10 15:09:40 -0800208 */
209 int c1, c2;
210 int tmax = msec * 9 / 8;
211 int tmin = msec * 7 / 8;
212 int diff = 65536;
213
214 /* We start at '1' to ensure we never even think of choosing a
215 * total time of '0'.
216 */
217 for (c1 = 1; c1 < TIMECODES; c1++) {
218 int t = time_codes[c1];
219 if (t*2 < tmin)
220 continue;
221 if (t > tmax)
222 break;
223 for (c2 = 0; c2 <= c1; c2++) {
224 int tt = t + time_codes[c2];
225 int d;
226 if (tt < tmin)
227 continue;
228 if (tt > tmax)
229 break;
230 /* This works! */
231 d = abs(msec - tt);
232 if (d >= diff)
233 continue;
234 /* Best yet */
235 *c1p = c1;
236 *c2p = c2;
237 diff = d;
238 if (d == 0)
239 return msec;
240 }
241 }
242 if (diff < 65536) {
243 int actual;
244 if (msec & 1) {
Yihao Hanfa019ba2021-11-14 19:24:28 -0800245 swap(*c2p, *c1p);
NeilBrowna6d511e2012-01-10 15:09:40 -0800246 }
247 actual = time_codes[*c1p] + time_codes[*c2p];
248 if (*c1p < *c2p)
249 return actual + 1;
250 else
251 return actual;
252 }
253 /* No close match */
254 return -EINVAL;
255}
256
257/*
NeilBrown1f431af2013-11-12 21:52:43 -0800258 * Update the register file with the appropriate 3-bit state for the
259 * given led.
NeilBrowna6d511e2012-01-10 15:09:40 -0800260 */
261static void set_select(struct tca6507_chip *tca, int led, int val)
262{
263 int mask = (1 << led);
264 int bit;
265
266 for (bit = 0; bit < 3; bit++) {
267 int n = tca->reg_file[bit] & ~mask;
268 if (val & (1 << bit))
269 n |= mask;
270 if (tca->reg_file[bit] != n) {
271 tca->reg_file[bit] = n;
272 tca->reg_set |= (1 << bit);
273 }
274 }
275}
276
NeilBrown1f431af2013-11-12 21:52:43 -0800277/* Update the register file with the appropriate 4-bit code for one
278 * bank or other. This can be used for timers, for levels, or for
279 * initialization.
NeilBrowna6d511e2012-01-10 15:09:40 -0800280 */
281static void set_code(struct tca6507_chip *tca, int reg, int bank, int new)
282{
283 int mask = 0xF;
284 int n;
285 if (bank) {
286 mask <<= 4;
287 new <<= 4;
288 }
289 n = tca->reg_file[reg] & ~mask;
290 n |= new;
291 if (tca->reg_file[reg] != n) {
292 tca->reg_file[reg] = n;
293 tca->reg_set |= 1 << reg;
294 }
295}
296
297/* Update brightness level. */
298static void set_level(struct tca6507_chip *tca, int bank, int level)
299{
300 switch (bank) {
301 case BANK0:
302 case BANK1:
303 set_code(tca, TCA6507_MAX_INTENSITY, bank, level);
304 break;
305 case MASTER:
306 set_code(tca, TCA6507_MASTER_INTENSITY, 0, level);
307 break;
308 }
309 tca->bank[bank].level = level;
310}
311
NeilBrown1f431af2013-11-12 21:52:43 -0800312/* Record all relevant time codes for a given bank */
NeilBrowna6d511e2012-01-10 15:09:40 -0800313static void set_times(struct tca6507_chip *tca, int bank)
314{
315 int c1, c2;
316 int result;
317
318 result = choose_times(tca->bank[bank].ontime, &c1, &c2);
Dan Carpenter4a91c452016-04-14 12:36:16 +0300319 if (result < 0)
320 return;
NeilBrowna6d511e2012-01-10 15:09:40 -0800321 dev_dbg(&tca->client->dev,
NeilBrown1f431af2013-11-12 21:52:43 -0800322 "Chose on times %d(%d) %d(%d) for %dms\n",
323 c1, time_codes[c1],
NeilBrowna6d511e2012-01-10 15:09:40 -0800324 c2, time_codes[c2], tca->bank[bank].ontime);
325 set_code(tca, TCA6507_FADE_ON, bank, c2);
326 set_code(tca, TCA6507_FULL_ON, bank, c1);
327 tca->bank[bank].ontime = result;
328
329 result = choose_times(tca->bank[bank].offtime, &c1, &c2);
330 dev_dbg(&tca->client->dev,
NeilBrown1f431af2013-11-12 21:52:43 -0800331 "Chose off times %d(%d) %d(%d) for %dms\n",
332 c1, time_codes[c1],
NeilBrowna6d511e2012-01-10 15:09:40 -0800333 c2, time_codes[c2], tca->bank[bank].offtime);
334 set_code(tca, TCA6507_FADE_OFF, bank, c2);
335 set_code(tca, TCA6507_FIRST_OFF, bank, c1);
336 set_code(tca, TCA6507_SECOND_OFF, bank, c1);
337 tca->bank[bank].offtime = result;
338
339 set_code(tca, TCA6507_INITIALIZE, bank, INIT_CODE);
340}
341
342/* Write all needed register of tca6507 */
343
344static void tca6507_work(struct work_struct *work)
345{
346 struct tca6507_chip *tca = container_of(work, struct tca6507_chip,
347 work);
348 struct i2c_client *cl = tca->client;
349 int set;
350 u8 file[TCA6507_REG_CNT];
351 int r;
352
353 spin_lock_irq(&tca->lock);
354 set = tca->reg_set;
355 memcpy(file, tca->reg_file, TCA6507_REG_CNT);
356 tca->reg_set = 0;
357 spin_unlock_irq(&tca->lock);
358
359 for (r = 0; r < TCA6507_REG_CNT; r++)
360 if (set & (1<<r))
361 i2c_smbus_write_byte_data(cl, r, file[r]);
362}
363
364static void led_release(struct tca6507_led *led)
365{
366 /* If led owns any resource, release it. */
367 struct tca6507_chip *tca = led->chip;
368 if (led->bank >= 0) {
369 struct bank *b = tca->bank + led->bank;
370 if (led->blink)
371 b->time_use--;
372 b->level_use--;
373 }
374 led->blink = 0;
375 led->bank = -1;
376}
377
378static int led_prepare(struct tca6507_led *led)
379{
NeilBrown1f431af2013-11-12 21:52:43 -0800380 /* Assign this led to a bank, configuring that bank if
381 * necessary. */
NeilBrowna6d511e2012-01-10 15:09:40 -0800382 int level = TO_LEVEL(led->led_cdev.brightness);
383 struct tca6507_chip *tca = led->chip;
384 int c1, c2;
385 int i;
386 struct bank *b;
387 int need_init = 0;
388
389 led->led_cdev.brightness = TO_BRIGHT(level);
390 if (level == 0) {
391 set_select(tca, led->num, TCA6507_LS_LED_OFF);
392 return 0;
393 }
394
395 if (led->ontime == 0 || led->offtime == 0) {
396 /*
NeilBrown1f431af2013-11-12 21:52:43 -0800397 * Just set the brightness, choosing first usable
398 * bank. If none perfect, choose best. Count
399 * backwards so we check MASTER bank first to avoid
400 * wasting a timer.
NeilBrowna6d511e2012-01-10 15:09:40 -0800401 */
402 int best = -1;/* full-on */
403 int diff = 15-level;
404
405 if (level == 15) {
406 set_select(tca, led->num, TCA6507_LS_LED_ON);
407 return 0;
408 }
409
410 for (i = MASTER; i >= BANK0; i--) {
411 int d;
412 if (tca->bank[i].level == level ||
413 tca->bank[i].level_use == 0) {
414 best = i;
415 break;
416 }
417 d = abs(level - tca->bank[i].level);
418 if (d < diff) {
419 diff = d;
420 best = i;
421 }
422 }
423 if (best == -1) {
424 /* Best brightness is full-on */
425 set_select(tca, led->num, TCA6507_LS_LED_ON);
426 led->led_cdev.brightness = LED_FULL;
427 return 0;
428 }
429
430 if (!tca->bank[best].level_use)
431 set_level(tca, best, level);
432
433 tca->bank[best].level_use++;
434 led->bank = best;
435 set_select(tca, led->num, bank_source[best]);
436 led->led_cdev.brightness = TO_BRIGHT(tca->bank[best].level);
437 return 0;
438 }
439
440 /*
NeilBrown1f431af2013-11-12 21:52:43 -0800441 * We have on/off time so we need to try to allocate a timing
442 * bank. First check if times are compatible with hardware
443 * and give up if not.
NeilBrowna6d511e2012-01-10 15:09:40 -0800444 */
445 if (choose_times(led->ontime, &c1, &c2) < 0)
446 return -EINVAL;
447 if (choose_times(led->offtime, &c1, &c2) < 0)
448 return -EINVAL;
449
450 for (i = BANK0; i <= BANK1; i++) {
451 if (tca->bank[i].level_use == 0)
452 /* not in use - it is ours! */
453 break;
454 if (tca->bank[i].level != level)
455 /* Incompatible level - skip */
456 /* FIX: if timer matches we maybe should consider
457 * this anyway...
458 */
459 continue;
460
461 if (tca->bank[i].time_use == 0)
462 /* Timer not in use, and level matches - use it */
463 break;
464
465 if (!(tca->bank[i].on_dflt ||
466 led->on_dflt ||
467 tca->bank[i].ontime == led->ontime))
468 /* on time is incompatible */
469 continue;
470
471 if (!(tca->bank[i].off_dflt ||
472 led->off_dflt ||
473 tca->bank[i].offtime == led->offtime))
474 /* off time is incompatible */
475 continue;
476
477 /* looks like a suitable match */
478 break;
479 }
480
481 if (i > BANK1)
482 /* Nothing matches - how sad */
483 return -EINVAL;
484
485 b = &tca->bank[i];
486 if (b->level_use == 0)
487 set_level(tca, i, level);
488 b->level_use++;
489 led->bank = i;
490
491 if (b->on_dflt ||
492 !led->on_dflt ||
493 b->time_use == 0) {
494 b->ontime = led->ontime;
495 b->on_dflt = led->on_dflt;
496 need_init = 1;
497 }
498
499 if (b->off_dflt ||
500 !led->off_dflt ||
501 b->time_use == 0) {
502 b->offtime = led->offtime;
503 b->off_dflt = led->off_dflt;
504 need_init = 1;
505 }
506
507 if (need_init)
508 set_times(tca, i);
509
510 led->ontime = b->ontime;
511 led->offtime = b->offtime;
512
513 b->time_use++;
514 led->blink = 1;
515 led->led_cdev.brightness = TO_BRIGHT(b->level);
516 set_select(tca, led->num, blink_source[i]);
517 return 0;
518}
519
520static int led_assign(struct tca6507_led *led)
521{
522 struct tca6507_chip *tca = led->chip;
523 int err;
524 unsigned long flags;
525
526 spin_lock_irqsave(&tca->lock, flags);
527 led_release(led);
528 err = led_prepare(led);
529 if (err) {
530 /*
NeilBrown1f431af2013-11-12 21:52:43 -0800531 * Can only fail on timer setup. In that case we need
532 * to re-establish as steady level.
NeilBrowna6d511e2012-01-10 15:09:40 -0800533 */
534 led->ontime = 0;
535 led->offtime = 0;
536 led_prepare(led);
537 }
538 spin_unlock_irqrestore(&tca->lock, flags);
539
540 if (tca->reg_set)
541 schedule_work(&tca->work);
542 return err;
543}
544
545static void tca6507_brightness_set(struct led_classdev *led_cdev,
546 enum led_brightness brightness)
547{
548 struct tca6507_led *led = container_of(led_cdev, struct tca6507_led,
549 led_cdev);
550 led->led_cdev.brightness = brightness;
551 led->ontime = 0;
552 led->offtime = 0;
553 led_assign(led);
554}
555
556static int tca6507_blink_set(struct led_classdev *led_cdev,
557 unsigned long *delay_on,
558 unsigned long *delay_off)
559{
560 struct tca6507_led *led = container_of(led_cdev, struct tca6507_led,
561 led_cdev);
562
563 if (*delay_on == 0)
564 led->on_dflt = 1;
565 else if (delay_on != &led_cdev->blink_delay_on)
566 led->on_dflt = 0;
567 led->ontime = *delay_on;
568
569 if (*delay_off == 0)
570 led->off_dflt = 1;
571 else if (delay_off != &led_cdev->blink_delay_off)
572 led->off_dflt = 0;
573 led->offtime = *delay_off;
574
575 if (led->ontime == 0)
576 led->ontime = 512;
577 if (led->offtime == 0)
578 led->offtime = 512;
579
580 if (led->led_cdev.brightness == LED_OFF)
581 led->led_cdev.brightness = LED_FULL;
582 if (led_assign(led) < 0) {
583 led->ontime = 0;
584 led->offtime = 0;
585 led->led_cdev.brightness = LED_OFF;
586 return -EINVAL;
587 }
588 *delay_on = led->ontime;
589 *delay_off = led->offtime;
590 return 0;
591}
592
593#ifdef CONFIG_GPIOLIB
594static void tca6507_gpio_set_value(struct gpio_chip *gc,
595 unsigned offset, int val)
596{
Linus Walleij2a664342015-12-08 16:42:58 +0100597 struct tca6507_chip *tca = gpiochip_get_data(gc);
NeilBrowna6d511e2012-01-10 15:09:40 -0800598 unsigned long flags;
599
600 spin_lock_irqsave(&tca->lock, flags);
601 /*
NeilBrown1f431af2013-11-12 21:52:43 -0800602 * 'OFF' is floating high, and 'ON' is pulled down, so it has
603 * the inverse sense of 'val'.
NeilBrowna6d511e2012-01-10 15:09:40 -0800604 */
605 set_select(tca, tca->gpio_map[offset],
606 val ? TCA6507_LS_LED_OFF : TCA6507_LS_LED_ON);
607 spin_unlock_irqrestore(&tca->lock, flags);
608 if (tca->reg_set)
609 schedule_work(&tca->work);
610}
611
612static int tca6507_gpio_direction_output(struct gpio_chip *gc,
613 unsigned offset, int val)
614{
615 tca6507_gpio_set_value(gc, offset, val);
616 return 0;
617}
618
Marek Behúnc1ff1a12020-09-20 00:15:41 +0200619static int tca6507_probe_gpios(struct device *dev,
NeilBrowna6d511e2012-01-10 15:09:40 -0800620 struct tca6507_chip *tca,
621 struct tca6507_platform_data *pdata)
622{
623 int err;
624 int i = 0;
625 int gpios = 0;
626
627 for (i = 0; i < NUM_LEDS; i++)
628 if (pdata->leds.leds[i].name && pdata->leds.leds[i].flags) {
629 /* Configure as a gpio */
NeilBrowna6d511e2012-01-10 15:09:40 -0800630 tca->gpio_map[gpios] = i;
631 gpios++;
632 }
633
634 if (!gpios)
635 return 0;
636
637 tca->gpio.label = "gpio-tca6507";
NeilBrowna6d511e2012-01-10 15:09:40 -0800638 tca->gpio.ngpio = gpios;
639 tca->gpio.base = pdata->gpio_base;
640 tca->gpio.owner = THIS_MODULE;
641 tca->gpio.direction_output = tca6507_gpio_direction_output;
642 tca->gpio.set = tca6507_gpio_set_value;
Marek Behúnc1ff1a12020-09-20 00:15:41 +0200643 tca->gpio.parent = dev;
Linus Walleij2a664342015-12-08 16:42:58 +0100644 err = gpiochip_add_data(&tca->gpio, tca);
NeilBrowna6d511e2012-01-10 15:09:40 -0800645 if (err) {
646 tca->gpio.ngpio = 0;
647 return err;
648 }
NeilBrowna6d511e2012-01-10 15:09:40 -0800649 return 0;
650}
651
652static void tca6507_remove_gpio(struct tca6507_chip *tca)
653{
abdoulaye berthe88d5e522014-07-12 22:30:14 +0200654 if (tca->gpio.ngpio)
655 gpiochip_remove(&tca->gpio);
NeilBrowna6d511e2012-01-10 15:09:40 -0800656}
657#else /* CONFIG_GPIOLIB */
Marek Behúnc1ff1a12020-09-20 00:15:41 +0200658static int tca6507_probe_gpios(struct device *dev,
NeilBrowna6d511e2012-01-10 15:09:40 -0800659 struct tca6507_chip *tca,
660 struct tca6507_platform_data *pdata)
661{
662 return 0;
663}
664static void tca6507_remove_gpio(struct tca6507_chip *tca)
665{
666}
667#endif /* CONFIG_GPIOLIB */
668
Marek Belisko0c596a72012-11-30 14:00:49 -0800669static struct tca6507_platform_data *
Marek Behúnc1ff1a12020-09-20 00:15:41 +0200670tca6507_led_dt_init(struct device *dev)
Marek Belisko0c596a72012-11-30 14:00:49 -0800671{
Marek Belisko0c596a72012-11-30 14:00:49 -0800672 struct tca6507_platform_data *pdata;
Marek Behún96f52412020-09-20 00:15:37 +0200673 struct fwnode_handle *child;
Marek Belisko0c596a72012-11-30 14:00:49 -0800674 struct led_info *tca_leds;
Axel Linef754e82013-01-27 01:14:14 -0800675 int count;
Marek Belisko0c596a72012-11-30 14:00:49 -0800676
Marek Behúnc1ff1a12020-09-20 00:15:41 +0200677 count = device_get_child_node_count(dev);
Axel Linef754e82013-01-27 01:14:14 -0800678 if (!count || count > NUM_LEDS)
Marek Belisko0c596a72012-11-30 14:00:49 -0800679 return ERR_PTR(-ENODEV);
680
Marek Behúnc1ff1a12020-09-20 00:15:41 +0200681 tca_leds = devm_kcalloc(dev, NUM_LEDS, sizeof(struct led_info),
Marek Behún96f52412020-09-20 00:15:37 +0200682 GFP_KERNEL);
Marek Belisko0c596a72012-11-30 14:00:49 -0800683 if (!tca_leds)
684 return ERR_PTR(-ENOMEM);
685
Marek Behúnc1ff1a12020-09-20 00:15:41 +0200686 device_for_each_child_node(dev, child) {
Marek Belisko0c596a72012-11-30 14:00:49 -0800687 struct led_info led;
688 u32 reg;
689 int ret;
690
Marek Behún96f52412020-09-20 00:15:37 +0200691 if (fwnode_property_read_string(child, "label", &led.name))
692 led.name = fwnode_get_name(child);
693
694 fwnode_property_read_string(child, "linux,default-trigger",
695 &led.default_trigger);
696
NeilBrown93341292013-10-31 19:33:45 -0700697 led.flags = 0;
Marek Behún96f52412020-09-20 00:15:37 +0200698 if (fwnode_property_match_string(child, "compatible",
699 "gpio") >= 0)
NeilBrown10ead6e2013-10-31 19:41:20 -0700700 led.flags |= TCA6507_MAKE_GPIO;
Marek Behún96f52412020-09-20 00:15:37 +0200701
702 ret = fwnode_property_read_u32(child, "reg", &reg);
703 if (ret || reg >= NUM_LEDS) {
704 fwnode_handle_put(child);
Marek Behúna2380982020-09-26 21:43:02 +0200705 return ERR_PTR(ret ? : -EINVAL);
Marek Behún96f52412020-09-20 00:15:37 +0200706 }
Marek Belisko0c596a72012-11-30 14:00:49 -0800707
708 tca_leds[reg] = led;
709 }
Marek Behún96f52412020-09-20 00:15:37 +0200710
Marek Behúnc1ff1a12020-09-20 00:15:41 +0200711 pdata = devm_kzalloc(dev, sizeof(struct tca6507_platform_data),
Marek Behún96f52412020-09-20 00:15:37 +0200712 GFP_KERNEL);
Marek Belisko0c596a72012-11-30 14:00:49 -0800713 if (!pdata)
714 return ERR_PTR(-ENOMEM);
715
716 pdata->leds.leds = tca_leds;
NeilBrown93341292013-10-31 19:33:45 -0700717 pdata->leds.num_leds = NUM_LEDS;
Chen Gang33ca1532013-12-08 20:41:37 -0800718#ifdef CONFIG_GPIOLIB
NeilBrown10ead6e2013-10-31 19:41:20 -0700719 pdata->gpio_base = -1;
Chen Gang33ca1532013-12-08 20:41:37 -0800720#endif
Marek Behún96f52412020-09-20 00:15:37 +0200721
Marek Belisko0c596a72012-11-30 14:00:49 -0800722 return pdata;
723}
724
Pavel Macheke8b7dab2020-09-25 11:24:12 +0200725static const struct of_device_id __maybe_unused of_tca6507_leds_match[] = {
Marek Belisko0c596a72012-11-30 14:00:49 -0800726 { .compatible = "ti,tca6507", },
727 {},
728};
Javier Martinez Canillas4d59ed82015-08-25 08:31:16 +0200729MODULE_DEVICE_TABLE(of, of_tca6507_leds_match);
Marek Belisko0c596a72012-11-30 14:00:49 -0800730
Bill Pemberton98ea1ea2012-11-19 13:23:02 -0500731static int tca6507_probe(struct i2c_client *client,
Marek Belisko0c596a72012-11-30 14:00:49 -0800732 const struct i2c_device_id *id)
NeilBrowna6d511e2012-01-10 15:09:40 -0800733{
Marek Behúnc1ff1a12020-09-20 00:15:41 +0200734 struct device *dev = &client->dev;
NeilBrowna6d511e2012-01-10 15:09:40 -0800735 struct i2c_adapter *adapter;
Marek Behúnc1ff1a12020-09-20 00:15:41 +0200736 struct tca6507_chip *tca;
NeilBrowna6d511e2012-01-10 15:09:40 -0800737 struct tca6507_platform_data *pdata;
738 int err;
739 int i = 0;
740
Wolfram Sang09bfa5f2019-06-08 12:55:44 +0200741 adapter = client->adapter;
NeilBrowna6d511e2012-01-10 15:09:40 -0800742
743 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C))
744 return -EIO;
745
Marek Behúnc1ff1a12020-09-20 00:15:41 +0200746 pdata = tca6507_led_dt_init(dev);
Marek Behún96f52412020-09-20 00:15:37 +0200747 if (IS_ERR(pdata)) {
Marek Behúnc1ff1a12020-09-20 00:15:41 +0200748 dev_err(dev, "Need %d entries in platform-data list\n", NUM_LEDS);
Marek Behún96f52412020-09-20 00:15:37 +0200749 return PTR_ERR(pdata);
NeilBrowna6d511e2012-01-10 15:09:40 -0800750 }
Marek Behúnc1ff1a12020-09-20 00:15:41 +0200751 tca = devm_kzalloc(dev, sizeof(*tca), GFP_KERNEL);
NeilBrowna6d511e2012-01-10 15:09:40 -0800752 if (!tca)
Dan Carpenter920c4f42012-03-23 15:02:07 -0700753 return -ENOMEM;
NeilBrowna6d511e2012-01-10 15:09:40 -0800754
755 tca->client = client;
756 INIT_WORK(&tca->work, tca6507_work);
757 spin_lock_init(&tca->lock);
758 i2c_set_clientdata(client, tca);
759
760 for (i = 0; i < NUM_LEDS; i++) {
761 struct tca6507_led *l = tca->leds + i;
762
763 l->chip = tca;
764 l->num = i;
765 if (pdata->leds.leds[i].name && !pdata->leds.leds[i].flags) {
766 l->led_cdev.name = pdata->leds.leds[i].name;
767 l->led_cdev.default_trigger
768 = pdata->leds.leds[i].default_trigger;
769 l->led_cdev.brightness_set = tca6507_brightness_set;
770 l->led_cdev.blink_set = tca6507_blink_set;
771 l->bank = -1;
Marek Behúnc1ff1a12020-09-20 00:15:41 +0200772 err = led_classdev_register(dev, &l->led_cdev);
NeilBrowna6d511e2012-01-10 15:09:40 -0800773 if (err < 0)
774 goto exit;
775 }
776 }
Marek Behúnc1ff1a12020-09-20 00:15:41 +0200777 err = tca6507_probe_gpios(dev, tca, pdata);
NeilBrowna6d511e2012-01-10 15:09:40 -0800778 if (err)
779 goto exit;
780 /* set all registers to known state - zero */
781 tca->reg_set = 0x7f;
782 schedule_work(&tca->work);
783
784 return 0;
785exit:
Dan Carpenter920c4f42012-03-23 15:02:07 -0700786 while (i--) {
NeilBrowna6d511e2012-01-10 15:09:40 -0800787 if (tca->leds[i].led_cdev.name)
788 led_classdev_unregister(&tca->leds[i].led_cdev);
Dan Carpenter920c4f42012-03-23 15:02:07 -0700789 }
NeilBrowna6d511e2012-01-10 15:09:40 -0800790 return err;
791}
792
Uwe Kleine-Königed5c2f52022-08-15 10:02:30 +0200793static void tca6507_remove(struct i2c_client *client)
NeilBrowna6d511e2012-01-10 15:09:40 -0800794{
795 int i;
796 struct tca6507_chip *tca = i2c_get_clientdata(client);
797 struct tca6507_led *tca_leds = tca->leds;
798
799 for (i = 0; i < NUM_LEDS; i++) {
800 if (tca_leds[i].led_cdev.name)
801 led_classdev_unregister(&tca_leds[i].led_cdev);
802 }
803 tca6507_remove_gpio(tca);
804 cancel_work_sync(&tca->work);
NeilBrowna6d511e2012-01-10 15:09:40 -0800805}
806
807static struct i2c_driver tca6507_driver = {
808 .driver = {
809 .name = "leds-tca6507",
Sachin Kamatfbd9df22013-03-14 03:09:49 -0700810 .of_match_table = of_match_ptr(of_tca6507_leds_match),
NeilBrowna6d511e2012-01-10 15:09:40 -0800811 },
812 .probe = tca6507_probe,
Bill Pembertondf07cf82012-11-19 13:20:20 -0500813 .remove = tca6507_remove,
NeilBrowna6d511e2012-01-10 15:09:40 -0800814 .id_table = tca6507_id,
815};
816
Peter Meerwald2e911762012-07-03 16:03:38 +0800817module_i2c_driver(tca6507_driver);
NeilBrowna6d511e2012-01-10 15:09:40 -0800818
819MODULE_AUTHOR("NeilBrown <neilb@suse.de>");
820MODULE_DESCRIPTION("TCA6507 LED/GPO driver");
821MODULE_LICENSE("GPL v2");