blob: 4b10ef9ae22129d09300ae1cbf0cdae394749bf9 [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 *
NeilBrown1f431af2013-11-12 21:52:43 -080074 * An led-tca6507 device must be provided with platform data or
75 * configured via devicetree.
NeilBrowna6d511e2012-01-10 15:09:40 -080076 *
NeilBrown1f431af2013-11-12 21:52:43 -080077 * 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.
84 *
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
88 * default trigger.
NeilBrowna6d511e2012-01-10 15:09:40 -080089 */
90
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>
Linus Walleijca995222020-04-15 17:08:37 +020096#include <linux/gpio/driver.h>
Marek Behún96f52412020-09-20 00:15:37 +020097#include <linux/property.h>
NeilBrowna6d511e2012-01-10 15:09:40 -080098#include <linux/workqueue.h>
NeilBrowna6d511e2012-01-10 15:09:40 -080099
100/* LED select registers determine the source that drives LED outputs */
101#define TCA6507_LS_LED_OFF 0x0 /* Output HI-Z (off) */
102#define TCA6507_LS_LED_OFF1 0x1 /* Output HI-Z (off) - not used */
103#define TCA6507_LS_LED_PWM0 0x2 /* Output LOW with Bank0 rate */
104#define TCA6507_LS_LED_PWM1 0x3 /* Output LOW with Bank1 rate */
105#define TCA6507_LS_LED_ON 0x4 /* Output LOW (on) */
106#define TCA6507_LS_LED_MIR 0x5 /* Output LOW with Master Intensity */
107#define TCA6507_LS_BLINK0 0x6 /* Blink at Bank0 rate */
108#define TCA6507_LS_BLINK1 0x7 /* Blink at Bank1 rate */
109
Marek Behún38b393f2020-09-20 00:15:36 +0200110struct tca6507_platform_data {
111 struct led_platform_data leds;
112#ifdef CONFIG_GPIOLIB
113 int gpio_base;
Marek Behún38b393f2020-09-20 00:15:36 +0200114#endif
115};
116
117#define TCA6507_MAKE_GPIO 1
118
NeilBrowna6d511e2012-01-10 15:09:40 -0800119enum {
120 BANK0,
121 BANK1,
122 MASTER,
123};
124static int bank_source[3] = {
125 TCA6507_LS_LED_PWM0,
126 TCA6507_LS_LED_PWM1,
127 TCA6507_LS_LED_MIR,
128};
129static int blink_source[2] = {
130 TCA6507_LS_BLINK0,
131 TCA6507_LS_BLINK1,
132};
133
134/* PWM registers */
135#define TCA6507_REG_CNT 11
136
137/*
138 * 0x00, 0x01, 0x02 encode the TCA6507_LS_* values, each output
139 * owns one bit in each register
140 */
141#define TCA6507_FADE_ON 0x03
142#define TCA6507_FULL_ON 0x04
143#define TCA6507_FADE_OFF 0x05
144#define TCA6507_FIRST_OFF 0x06
145#define TCA6507_SECOND_OFF 0x07
146#define TCA6507_MAX_INTENSITY 0x08
147#define TCA6507_MASTER_INTENSITY 0x09
148#define TCA6507_INITIALIZE 0x0A
149
150#define INIT_CODE 0x8
151
152#define TIMECODES 16
153static int time_codes[TIMECODES] = {
154 0, 64, 128, 192, 256, 384, 512, 768,
155 1024, 1536, 2048, 3072, 4096, 5760, 8128, 16320
156};
157
158/* Convert an led.brightness level (0..255) to a TCA6507 level (0..15) */
159static inline int TO_LEVEL(int brightness)
160{
161 return brightness >> 4;
162}
163
164/* ...and convert back */
165static inline int TO_BRIGHT(int level)
166{
167 if (level)
168 return (level << 4) | 0xf;
169 return 0;
170}
171
172#define NUM_LEDS 7
173struct tca6507_chip {
174 int reg_set; /* One bit per register where
175 * a '1' means the register
176 * should be written */
177 u8 reg_file[TCA6507_REG_CNT];
178 /* Bank 2 is Master Intensity and doesn't use times */
179 struct bank {
180 int level;
181 int ontime, offtime;
182 int on_dflt, off_dflt;
183 int time_use, level_use;
184 } bank[3];
185 struct i2c_client *client;
186 struct work_struct work;
187 spinlock_t lock;
188
189 struct tca6507_led {
190 struct tca6507_chip *chip;
191 struct led_classdev led_cdev;
192 int num;
193 int ontime, offtime;
194 int on_dflt, off_dflt;
195 int bank; /* Bank used, or -1 */
196 int blink; /* Set if hardware-blinking */
197 } leds[NUM_LEDS];
198#ifdef CONFIG_GPIOLIB
199 struct gpio_chip gpio;
200 const char *gpio_name[NUM_LEDS];
201 int gpio_map[NUM_LEDS];
202#endif
203};
204
205static const struct i2c_device_id tca6507_id[] = {
206 { "tca6507" },
207 { }
208};
209MODULE_DEVICE_TABLE(i2c, tca6507_id);
210
211static int choose_times(int msec, int *c1p, int *c2p)
212{
213 /*
NeilBrown1f431af2013-11-12 21:52:43 -0800214 * Choose two timecodes which add to 'msec' as near as
215 * possible. The first returned is the 'on' or 'off' time.
216 * The second is to be used as a 'fade-on' or 'fade-off' time.
217 * If 'msec' is even, the first will not be smaller than the
218 * second. If 'msec' is odd, the first will not be larger
219 * than the second.
220 * If we cannot get a sum within 1/8 of 'msec' fail with
221 * -EINVAL, otherwise return the sum that was achieved, plus 1
222 * if the first is smaller.
223 * If two possibilities are equally good (e.g. 512+0,
224 * 256+256), choose the first pair so there is more
225 * change-time visible (i.e. it is softer).
NeilBrowna6d511e2012-01-10 15:09:40 -0800226 */
227 int c1, c2;
228 int tmax = msec * 9 / 8;
229 int tmin = msec * 7 / 8;
230 int diff = 65536;
231
232 /* We start at '1' to ensure we never even think of choosing a
233 * total time of '0'.
234 */
235 for (c1 = 1; c1 < TIMECODES; c1++) {
236 int t = time_codes[c1];
237 if (t*2 < tmin)
238 continue;
239 if (t > tmax)
240 break;
241 for (c2 = 0; c2 <= c1; c2++) {
242 int tt = t + time_codes[c2];
243 int d;
244 if (tt < tmin)
245 continue;
246 if (tt > tmax)
247 break;
248 /* This works! */
249 d = abs(msec - tt);
250 if (d >= diff)
251 continue;
252 /* Best yet */
253 *c1p = c1;
254 *c2p = c2;
255 diff = d;
256 if (d == 0)
257 return msec;
258 }
259 }
260 if (diff < 65536) {
261 int actual;
262 if (msec & 1) {
263 c1 = *c2p;
264 *c2p = *c1p;
265 *c1p = c1;
266 }
267 actual = time_codes[*c1p] + time_codes[*c2p];
268 if (*c1p < *c2p)
269 return actual + 1;
270 else
271 return actual;
272 }
273 /* No close match */
274 return -EINVAL;
275}
276
277/*
NeilBrown1f431af2013-11-12 21:52:43 -0800278 * Update the register file with the appropriate 3-bit state for the
279 * given led.
NeilBrowna6d511e2012-01-10 15:09:40 -0800280 */
281static void set_select(struct tca6507_chip *tca, int led, int val)
282{
283 int mask = (1 << led);
284 int bit;
285
286 for (bit = 0; bit < 3; bit++) {
287 int n = tca->reg_file[bit] & ~mask;
288 if (val & (1 << bit))
289 n |= mask;
290 if (tca->reg_file[bit] != n) {
291 tca->reg_file[bit] = n;
292 tca->reg_set |= (1 << bit);
293 }
294 }
295}
296
NeilBrown1f431af2013-11-12 21:52:43 -0800297/* Update the register file with the appropriate 4-bit code for one
298 * bank or other. This can be used for timers, for levels, or for
299 * initialization.
NeilBrowna6d511e2012-01-10 15:09:40 -0800300 */
301static void set_code(struct tca6507_chip *tca, int reg, int bank, int new)
302{
303 int mask = 0xF;
304 int n;
305 if (bank) {
306 mask <<= 4;
307 new <<= 4;
308 }
309 n = tca->reg_file[reg] & ~mask;
310 n |= new;
311 if (tca->reg_file[reg] != n) {
312 tca->reg_file[reg] = n;
313 tca->reg_set |= 1 << reg;
314 }
315}
316
317/* Update brightness level. */
318static void set_level(struct tca6507_chip *tca, int bank, int level)
319{
320 switch (bank) {
321 case BANK0:
322 case BANK1:
323 set_code(tca, TCA6507_MAX_INTENSITY, bank, level);
324 break;
325 case MASTER:
326 set_code(tca, TCA6507_MASTER_INTENSITY, 0, level);
327 break;
328 }
329 tca->bank[bank].level = level;
330}
331
NeilBrown1f431af2013-11-12 21:52:43 -0800332/* Record all relevant time codes for a given bank */
NeilBrowna6d511e2012-01-10 15:09:40 -0800333static void set_times(struct tca6507_chip *tca, int bank)
334{
335 int c1, c2;
336 int result;
337
338 result = choose_times(tca->bank[bank].ontime, &c1, &c2);
Dan Carpenter4a91c452016-04-14 12:36:16 +0300339 if (result < 0)
340 return;
NeilBrowna6d511e2012-01-10 15:09:40 -0800341 dev_dbg(&tca->client->dev,
NeilBrown1f431af2013-11-12 21:52:43 -0800342 "Chose on times %d(%d) %d(%d) for %dms\n",
343 c1, time_codes[c1],
NeilBrowna6d511e2012-01-10 15:09:40 -0800344 c2, time_codes[c2], tca->bank[bank].ontime);
345 set_code(tca, TCA6507_FADE_ON, bank, c2);
346 set_code(tca, TCA6507_FULL_ON, bank, c1);
347 tca->bank[bank].ontime = result;
348
349 result = choose_times(tca->bank[bank].offtime, &c1, &c2);
350 dev_dbg(&tca->client->dev,
NeilBrown1f431af2013-11-12 21:52:43 -0800351 "Chose off times %d(%d) %d(%d) for %dms\n",
352 c1, time_codes[c1],
NeilBrowna6d511e2012-01-10 15:09:40 -0800353 c2, time_codes[c2], tca->bank[bank].offtime);
354 set_code(tca, TCA6507_FADE_OFF, bank, c2);
355 set_code(tca, TCA6507_FIRST_OFF, bank, c1);
356 set_code(tca, TCA6507_SECOND_OFF, bank, c1);
357 tca->bank[bank].offtime = result;
358
359 set_code(tca, TCA6507_INITIALIZE, bank, INIT_CODE);
360}
361
362/* Write all needed register of tca6507 */
363
364static void tca6507_work(struct work_struct *work)
365{
366 struct tca6507_chip *tca = container_of(work, struct tca6507_chip,
367 work);
368 struct i2c_client *cl = tca->client;
369 int set;
370 u8 file[TCA6507_REG_CNT];
371 int r;
372
373 spin_lock_irq(&tca->lock);
374 set = tca->reg_set;
375 memcpy(file, tca->reg_file, TCA6507_REG_CNT);
376 tca->reg_set = 0;
377 spin_unlock_irq(&tca->lock);
378
379 for (r = 0; r < TCA6507_REG_CNT; r++)
380 if (set & (1<<r))
381 i2c_smbus_write_byte_data(cl, r, file[r]);
382}
383
384static void led_release(struct tca6507_led *led)
385{
386 /* If led owns any resource, release it. */
387 struct tca6507_chip *tca = led->chip;
388 if (led->bank >= 0) {
389 struct bank *b = tca->bank + led->bank;
390 if (led->blink)
391 b->time_use--;
392 b->level_use--;
393 }
394 led->blink = 0;
395 led->bank = -1;
396}
397
398static int led_prepare(struct tca6507_led *led)
399{
NeilBrown1f431af2013-11-12 21:52:43 -0800400 /* Assign this led to a bank, configuring that bank if
401 * necessary. */
NeilBrowna6d511e2012-01-10 15:09:40 -0800402 int level = TO_LEVEL(led->led_cdev.brightness);
403 struct tca6507_chip *tca = led->chip;
404 int c1, c2;
405 int i;
406 struct bank *b;
407 int need_init = 0;
408
409 led->led_cdev.brightness = TO_BRIGHT(level);
410 if (level == 0) {
411 set_select(tca, led->num, TCA6507_LS_LED_OFF);
412 return 0;
413 }
414
415 if (led->ontime == 0 || led->offtime == 0) {
416 /*
NeilBrown1f431af2013-11-12 21:52:43 -0800417 * Just set the brightness, choosing first usable
418 * bank. If none perfect, choose best. Count
419 * backwards so we check MASTER bank first to avoid
420 * wasting a timer.
NeilBrowna6d511e2012-01-10 15:09:40 -0800421 */
422 int best = -1;/* full-on */
423 int diff = 15-level;
424
425 if (level == 15) {
426 set_select(tca, led->num, TCA6507_LS_LED_ON);
427 return 0;
428 }
429
430 for (i = MASTER; i >= BANK0; i--) {
431 int d;
432 if (tca->bank[i].level == level ||
433 tca->bank[i].level_use == 0) {
434 best = i;
435 break;
436 }
437 d = abs(level - tca->bank[i].level);
438 if (d < diff) {
439 diff = d;
440 best = i;
441 }
442 }
443 if (best == -1) {
444 /* Best brightness is full-on */
445 set_select(tca, led->num, TCA6507_LS_LED_ON);
446 led->led_cdev.brightness = LED_FULL;
447 return 0;
448 }
449
450 if (!tca->bank[best].level_use)
451 set_level(tca, best, level);
452
453 tca->bank[best].level_use++;
454 led->bank = best;
455 set_select(tca, led->num, bank_source[best]);
456 led->led_cdev.brightness = TO_BRIGHT(tca->bank[best].level);
457 return 0;
458 }
459
460 /*
NeilBrown1f431af2013-11-12 21:52:43 -0800461 * We have on/off time so we need to try to allocate a timing
462 * bank. First check if times are compatible with hardware
463 * and give up if not.
NeilBrowna6d511e2012-01-10 15:09:40 -0800464 */
465 if (choose_times(led->ontime, &c1, &c2) < 0)
466 return -EINVAL;
467 if (choose_times(led->offtime, &c1, &c2) < 0)
468 return -EINVAL;
469
470 for (i = BANK0; i <= BANK1; i++) {
471 if (tca->bank[i].level_use == 0)
472 /* not in use - it is ours! */
473 break;
474 if (tca->bank[i].level != level)
475 /* Incompatible level - skip */
476 /* FIX: if timer matches we maybe should consider
477 * this anyway...
478 */
479 continue;
480
481 if (tca->bank[i].time_use == 0)
482 /* Timer not in use, and level matches - use it */
483 break;
484
485 if (!(tca->bank[i].on_dflt ||
486 led->on_dflt ||
487 tca->bank[i].ontime == led->ontime))
488 /* on time is incompatible */
489 continue;
490
491 if (!(tca->bank[i].off_dflt ||
492 led->off_dflt ||
493 tca->bank[i].offtime == led->offtime))
494 /* off time is incompatible */
495 continue;
496
497 /* looks like a suitable match */
498 break;
499 }
500
501 if (i > BANK1)
502 /* Nothing matches - how sad */
503 return -EINVAL;
504
505 b = &tca->bank[i];
506 if (b->level_use == 0)
507 set_level(tca, i, level);
508 b->level_use++;
509 led->bank = i;
510
511 if (b->on_dflt ||
512 !led->on_dflt ||
513 b->time_use == 0) {
514 b->ontime = led->ontime;
515 b->on_dflt = led->on_dflt;
516 need_init = 1;
517 }
518
519 if (b->off_dflt ||
520 !led->off_dflt ||
521 b->time_use == 0) {
522 b->offtime = led->offtime;
523 b->off_dflt = led->off_dflt;
524 need_init = 1;
525 }
526
527 if (need_init)
528 set_times(tca, i);
529
530 led->ontime = b->ontime;
531 led->offtime = b->offtime;
532
533 b->time_use++;
534 led->blink = 1;
535 led->led_cdev.brightness = TO_BRIGHT(b->level);
536 set_select(tca, led->num, blink_source[i]);
537 return 0;
538}
539
540static int led_assign(struct tca6507_led *led)
541{
542 struct tca6507_chip *tca = led->chip;
543 int err;
544 unsigned long flags;
545
546 spin_lock_irqsave(&tca->lock, flags);
547 led_release(led);
548 err = led_prepare(led);
549 if (err) {
550 /*
NeilBrown1f431af2013-11-12 21:52:43 -0800551 * Can only fail on timer setup. In that case we need
552 * to re-establish as steady level.
NeilBrowna6d511e2012-01-10 15:09:40 -0800553 */
554 led->ontime = 0;
555 led->offtime = 0;
556 led_prepare(led);
557 }
558 spin_unlock_irqrestore(&tca->lock, flags);
559
560 if (tca->reg_set)
561 schedule_work(&tca->work);
562 return err;
563}
564
565static void tca6507_brightness_set(struct led_classdev *led_cdev,
566 enum led_brightness brightness)
567{
568 struct tca6507_led *led = container_of(led_cdev, struct tca6507_led,
569 led_cdev);
570 led->led_cdev.brightness = brightness;
571 led->ontime = 0;
572 led->offtime = 0;
573 led_assign(led);
574}
575
576static int tca6507_blink_set(struct led_classdev *led_cdev,
577 unsigned long *delay_on,
578 unsigned long *delay_off)
579{
580 struct tca6507_led *led = container_of(led_cdev, struct tca6507_led,
581 led_cdev);
582
583 if (*delay_on == 0)
584 led->on_dflt = 1;
585 else if (delay_on != &led_cdev->blink_delay_on)
586 led->on_dflt = 0;
587 led->ontime = *delay_on;
588
589 if (*delay_off == 0)
590 led->off_dflt = 1;
591 else if (delay_off != &led_cdev->blink_delay_off)
592 led->off_dflt = 0;
593 led->offtime = *delay_off;
594
595 if (led->ontime == 0)
596 led->ontime = 512;
597 if (led->offtime == 0)
598 led->offtime = 512;
599
600 if (led->led_cdev.brightness == LED_OFF)
601 led->led_cdev.brightness = LED_FULL;
602 if (led_assign(led) < 0) {
603 led->ontime = 0;
604 led->offtime = 0;
605 led->led_cdev.brightness = LED_OFF;
606 return -EINVAL;
607 }
608 *delay_on = led->ontime;
609 *delay_off = led->offtime;
610 return 0;
611}
612
613#ifdef CONFIG_GPIOLIB
614static void tca6507_gpio_set_value(struct gpio_chip *gc,
615 unsigned offset, int val)
616{
Linus Walleij2a664342015-12-08 16:42:58 +0100617 struct tca6507_chip *tca = gpiochip_get_data(gc);
NeilBrowna6d511e2012-01-10 15:09:40 -0800618 unsigned long flags;
619
620 spin_lock_irqsave(&tca->lock, flags);
621 /*
NeilBrown1f431af2013-11-12 21:52:43 -0800622 * 'OFF' is floating high, and 'ON' is pulled down, so it has
623 * the inverse sense of 'val'.
NeilBrowna6d511e2012-01-10 15:09:40 -0800624 */
625 set_select(tca, tca->gpio_map[offset],
626 val ? TCA6507_LS_LED_OFF : TCA6507_LS_LED_ON);
627 spin_unlock_irqrestore(&tca->lock, flags);
628 if (tca->reg_set)
629 schedule_work(&tca->work);
630}
631
632static int tca6507_gpio_direction_output(struct gpio_chip *gc,
633 unsigned offset, int val)
634{
635 tca6507_gpio_set_value(gc, offset, val);
636 return 0;
637}
638
639static int tca6507_probe_gpios(struct i2c_client *client,
640 struct tca6507_chip *tca,
641 struct tca6507_platform_data *pdata)
642{
643 int err;
644 int i = 0;
645 int gpios = 0;
646
647 for (i = 0; i < NUM_LEDS; i++)
648 if (pdata->leds.leds[i].name && pdata->leds.leds[i].flags) {
649 /* Configure as a gpio */
650 tca->gpio_name[gpios] = pdata->leds.leds[i].name;
651 tca->gpio_map[gpios] = i;
652 gpios++;
653 }
654
655 if (!gpios)
656 return 0;
657
658 tca->gpio.label = "gpio-tca6507";
659 tca->gpio.names = tca->gpio_name;
660 tca->gpio.ngpio = gpios;
661 tca->gpio.base = pdata->gpio_base;
662 tca->gpio.owner = THIS_MODULE;
663 tca->gpio.direction_output = tca6507_gpio_direction_output;
664 tca->gpio.set = tca6507_gpio_set_value;
Linus Walleij58383c782015-11-04 09:56:26 +0100665 tca->gpio.parent = &client->dev;
NeilBrown10ead6e2013-10-31 19:41:20 -0700666#ifdef CONFIG_OF_GPIO
Marek Behún8853c95e92020-09-18 00:32:54 +0200667 tca->gpio.of_node = of_node_get(dev_of_node(&client->dev));
NeilBrown10ead6e2013-10-31 19:41:20 -0700668#endif
Linus Walleij2a664342015-12-08 16:42:58 +0100669 err = gpiochip_add_data(&tca->gpio, tca);
NeilBrowna6d511e2012-01-10 15:09:40 -0800670 if (err) {
671 tca->gpio.ngpio = 0;
672 return err;
673 }
NeilBrowna6d511e2012-01-10 15:09:40 -0800674 return 0;
675}
676
677static void tca6507_remove_gpio(struct tca6507_chip *tca)
678{
abdoulaye berthe88d5e522014-07-12 22:30:14 +0200679 if (tca->gpio.ngpio)
680 gpiochip_remove(&tca->gpio);
NeilBrowna6d511e2012-01-10 15:09:40 -0800681}
682#else /* CONFIG_GPIOLIB */
683static int tca6507_probe_gpios(struct i2c_client *client,
684 struct tca6507_chip *tca,
685 struct tca6507_platform_data *pdata)
686{
687 return 0;
688}
689static void tca6507_remove_gpio(struct tca6507_chip *tca)
690{
691}
692#endif /* CONFIG_GPIOLIB */
693
Marek Belisko0c596a72012-11-30 14:00:49 -0800694static struct tca6507_platform_data *
695tca6507_led_dt_init(struct i2c_client *client)
696{
Marek Belisko0c596a72012-11-30 14:00:49 -0800697 struct tca6507_platform_data *pdata;
Marek Behún96f52412020-09-20 00:15:37 +0200698 struct fwnode_handle *child;
Marek Belisko0c596a72012-11-30 14:00:49 -0800699 struct led_info *tca_leds;
Axel Linef754e82013-01-27 01:14:14 -0800700 int count;
Marek Belisko0c596a72012-11-30 14:00:49 -0800701
Marek Behún96f52412020-09-20 00:15:37 +0200702 count = device_get_child_node_count(&client->dev);
Axel Linef754e82013-01-27 01:14:14 -0800703 if (!count || count > NUM_LEDS)
Marek Belisko0c596a72012-11-30 14:00:49 -0800704 return ERR_PTR(-ENODEV);
705
Marek Behún96f52412020-09-20 00:15:37 +0200706 tca_leds = devm_kcalloc(&client->dev, NUM_LEDS, sizeof(struct led_info),
707 GFP_KERNEL);
Marek Belisko0c596a72012-11-30 14:00:49 -0800708 if (!tca_leds)
709 return ERR_PTR(-ENOMEM);
710
Marek Behún96f52412020-09-20 00:15:37 +0200711 device_for_each_child_node(&client->dev, child) {
Marek Belisko0c596a72012-11-30 14:00:49 -0800712 struct led_info led;
713 u32 reg;
714 int ret;
715
Marek Behún96f52412020-09-20 00:15:37 +0200716 if (fwnode_property_read_string(child, "label", &led.name))
717 led.name = fwnode_get_name(child);
718
719 fwnode_property_read_string(child, "linux,default-trigger",
720 &led.default_trigger);
721
NeilBrown93341292013-10-31 19:33:45 -0700722 led.flags = 0;
Marek Behún96f52412020-09-20 00:15:37 +0200723 if (fwnode_property_match_string(child, "compatible",
724 "gpio") >= 0)
NeilBrown10ead6e2013-10-31 19:41:20 -0700725 led.flags |= TCA6507_MAKE_GPIO;
Marek Behún96f52412020-09-20 00:15:37 +0200726
727 ret = fwnode_property_read_u32(child, "reg", &reg);
728 if (ret || reg >= NUM_LEDS) {
729 fwnode_handle_put(child);
730 return ERR_PTR(ret);
731 }
Marek Belisko0c596a72012-11-30 14:00:49 -0800732
733 tca_leds[reg] = led;
734 }
Marek Behún96f52412020-09-20 00:15:37 +0200735
736 pdata = devm_kzalloc(&client->dev, sizeof(struct tca6507_platform_data),
737 GFP_KERNEL);
Marek Belisko0c596a72012-11-30 14:00:49 -0800738 if (!pdata)
739 return ERR_PTR(-ENOMEM);
740
741 pdata->leds.leds = tca_leds;
NeilBrown93341292013-10-31 19:33:45 -0700742 pdata->leds.num_leds = NUM_LEDS;
Chen Gang33ca1532013-12-08 20:41:37 -0800743#ifdef CONFIG_GPIOLIB
NeilBrown10ead6e2013-10-31 19:41:20 -0700744 pdata->gpio_base = -1;
Chen Gang33ca1532013-12-08 20:41:37 -0800745#endif
Marek Behún96f52412020-09-20 00:15:37 +0200746
Marek Belisko0c596a72012-11-30 14:00:49 -0800747 return pdata;
748}
749
Pavel Macheke8b7dab2020-09-25 11:24:12 +0200750static const struct of_device_id __maybe_unused of_tca6507_leds_match[] = {
Marek Belisko0c596a72012-11-30 14:00:49 -0800751 { .compatible = "ti,tca6507", },
752 {},
753};
Javier Martinez Canillas4d59ed82015-08-25 08:31:16 +0200754MODULE_DEVICE_TABLE(of, of_tca6507_leds_match);
Marek Belisko0c596a72012-11-30 14:00:49 -0800755
Bill Pemberton98ea1ea2012-11-19 13:23:02 -0500756static int tca6507_probe(struct i2c_client *client,
Marek Belisko0c596a72012-11-30 14:00:49 -0800757 const struct i2c_device_id *id)
NeilBrowna6d511e2012-01-10 15:09:40 -0800758{
759 struct tca6507_chip *tca;
760 struct i2c_adapter *adapter;
761 struct tca6507_platform_data *pdata;
762 int err;
763 int i = 0;
764
Wolfram Sang09bfa5f2019-06-08 12:55:44 +0200765 adapter = client->adapter;
NeilBrowna6d511e2012-01-10 15:09:40 -0800766
767 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C))
768 return -EIO;
769
Marek Behún96f52412020-09-20 00:15:37 +0200770 pdata = tca6507_led_dt_init(client);
771 if (IS_ERR(pdata)) {
772 dev_err(&client->dev, "Need %d entries in platform-data list\n",
773 NUM_LEDS);
774 return PTR_ERR(pdata);
NeilBrowna6d511e2012-01-10 15:09:40 -0800775 }
Bryan Wu6770fc62012-07-04 12:20:41 +0800776 tca = devm_kzalloc(&client->dev, sizeof(*tca), GFP_KERNEL);
NeilBrowna6d511e2012-01-10 15:09:40 -0800777 if (!tca)
Dan Carpenter920c4f42012-03-23 15:02:07 -0700778 return -ENOMEM;
NeilBrowna6d511e2012-01-10 15:09:40 -0800779
780 tca->client = client;
781 INIT_WORK(&tca->work, tca6507_work);
782 spin_lock_init(&tca->lock);
783 i2c_set_clientdata(client, tca);
784
785 for (i = 0; i < NUM_LEDS; i++) {
786 struct tca6507_led *l = tca->leds + i;
787
788 l->chip = tca;
789 l->num = i;
790 if (pdata->leds.leds[i].name && !pdata->leds.leds[i].flags) {
791 l->led_cdev.name = pdata->leds.leds[i].name;
792 l->led_cdev.default_trigger
793 = pdata->leds.leds[i].default_trigger;
794 l->led_cdev.brightness_set = tca6507_brightness_set;
795 l->led_cdev.blink_set = tca6507_blink_set;
796 l->bank = -1;
797 err = led_classdev_register(&client->dev,
798 &l->led_cdev);
799 if (err < 0)
800 goto exit;
801 }
802 }
803 err = tca6507_probe_gpios(client, tca, pdata);
804 if (err)
805 goto exit;
806 /* set all registers to known state - zero */
807 tca->reg_set = 0x7f;
808 schedule_work(&tca->work);
809
810 return 0;
811exit:
Dan Carpenter920c4f42012-03-23 15:02:07 -0700812 while (i--) {
NeilBrowna6d511e2012-01-10 15:09:40 -0800813 if (tca->leds[i].led_cdev.name)
814 led_classdev_unregister(&tca->leds[i].led_cdev);
Dan Carpenter920c4f42012-03-23 15:02:07 -0700815 }
NeilBrowna6d511e2012-01-10 15:09:40 -0800816 return err;
817}
818
Bill Pemberton678e8a62012-11-19 13:26:00 -0500819static int tca6507_remove(struct i2c_client *client)
NeilBrowna6d511e2012-01-10 15:09:40 -0800820{
821 int i;
822 struct tca6507_chip *tca = i2c_get_clientdata(client);
823 struct tca6507_led *tca_leds = tca->leds;
824
825 for (i = 0; i < NUM_LEDS; i++) {
826 if (tca_leds[i].led_cdev.name)
827 led_classdev_unregister(&tca_leds[i].led_cdev);
828 }
829 tca6507_remove_gpio(tca);
830 cancel_work_sync(&tca->work);
NeilBrowna6d511e2012-01-10 15:09:40 -0800831
832 return 0;
833}
834
835static struct i2c_driver tca6507_driver = {
836 .driver = {
837 .name = "leds-tca6507",
Sachin Kamatfbd9df22013-03-14 03:09:49 -0700838 .of_match_table = of_match_ptr(of_tca6507_leds_match),
NeilBrowna6d511e2012-01-10 15:09:40 -0800839 },
840 .probe = tca6507_probe,
Bill Pembertondf07cf82012-11-19 13:20:20 -0500841 .remove = tca6507_remove,
NeilBrowna6d511e2012-01-10 15:09:40 -0800842 .id_table = tca6507_id,
843};
844
Peter Meerwald2e911762012-07-03 16:03:38 +0800845module_i2c_driver(tca6507_driver);
NeilBrowna6d511e2012-01-10 15:09:40 -0800846
847MODULE_AUTHOR("NeilBrown <neilb@suse.de>");
848MODULE_DESCRIPTION("TCA6507 LED/GPO driver");
849MODULE_LICENSE("GPL v2");