blob: 8cc304f36728a4e217cbf6716f893a3e6f5aed0c [file] [log] [blame]
NeilBrowna6d511e2012-01-10 15:09:40 -08001/*
2 * leds-tca6507
3 *
4 * The TCA6507 is a programmable LED controller that can drive 7
5 * separate lines either by holding them low, or by pulsing them
6 * with modulated width.
7 * The modulation can be varied in a simple pattern to produce a blink or
8 * double-blink.
9 *
10 * This driver can configure each line either as a 'GPIO' which is out-only
11 * (no pull-up) or as an LED with variable brightness and hardware-assisted
12 * blinking.
13 *
14 * Apart from OFF and ON there are three programmable brightness levels which
15 * can be programmed from 0 to 15 and indicate how many 500usec intervals in
16 * each 8msec that the led is 'on'. The levels are named MASTER, BANK0 and
17 * BANK1.
18 *
19 * There are two different blink rates that can be programmed, each with
20 * separate time for rise, on, fall, off and second-off. Thus if 3 or more
21 * different non-trivial rates are required, software must be used for the extra
22 * rates. The two different blink rates must align with the two levels BANK0 and
23 * BANK1.
24 * This driver does not support double-blink so 'second-off' always matches
25 * 'off'.
26 *
27 * Only 16 different times can be programmed in a roughly logarithmic scale from
28 * 64ms to 16320ms. To be precise the possible times are:
29 * 0, 64, 128, 192, 256, 384, 512, 768,
30 * 1024, 1536, 2048, 3072, 4096, 5760, 8128, 16320
31 *
32 * Times that cannot be closely matched with these must be
33 * handled in software. This driver allows 12.5% error in matching.
34 *
35 * This driver does not allow rise/fall rates to be set explicitly. When trying
36 * to match a given 'on' or 'off' period, an appropriate pair of 'change' and
37 * 'hold' times are chosen to get a close match. If the target delay is even,
38 * the 'change' number will be the smaller; if odd, the 'hold' number will be
39 * the smaller.
40
41 * Choosing pairs of delays with 12.5% errors allows us to match delays in the
42 * ranges: 56-72, 112-144, 168-216, 224-27504, 28560-36720.
43 * 26% of the achievable sums can be matched by multiple pairings. For example
44 * 1536 == 1536+0, 1024+512, or 768+768. This driver will always choose the
45 * pairing with the least maximum - 768+768 in this case. Other pairings are
46 * not available.
47 *
48 * Access to the 3 levels and 2 blinks are on a first-come, first-served basis.
49 * Access can be shared by multiple leds if they have the same level and
50 * either same blink rates, or some don't blink.
51 * When a led changes, it relinquishes access and tries again, so it might
52 * lose access to hardware blink.
53 * If a blink engine cannot be allocated, software blink is used.
54 * If the desired brightness cannot be allocated, the closest available non-zero
55 * brightness is used. As 'full' is always available, the worst case would be
56 * to have two different blink rates at '1', with Max at '2', then other leds
57 * will have to choose between '2' and '16'. Hopefully this is not likely.
58 *
59 * Each bank (BANK0 and BANK1) has two usage counts - LEDs using the brightness
60 * and LEDs using the blink. It can only be reprogrammed when the appropriate
61 * counter is zero. The MASTER level has a single usage count.
62 *
63 * Each Led has programmable 'on' and 'off' time as milliseconds. With each
64 * there is a flag saying if it was explicitly requested or defaulted.
65 * Similarly the banks know if each time was explicit or a default. Defaults
66 * are permitted to be changed freely - they are not recognised when matching.
67 *
68 *
69 * An led-tca6507 device must be provided with platform data. This data
70 * lists for each output: the name, default trigger, and whether the signal
71 * is being used as a GPiO rather than an led. 'struct led_plaform_data'
72 * is used for this. If 'name' is NULL, the output isn't used. If 'flags'
73 * is TCA6507_MAKE_CPIO, the output is a GPO.
74 * The "struct led_platform_data" can be embedded in a
75 * "struct tca6507_platform_data" which adds a 'gpio_base' for the GPiOs,
76 * and a 'setup' callback which is called once the GPiOs are available.
77 *
78 */
79
80#include <linux/module.h>
81#include <linux/slab.h>
82#include <linux/leds.h>
83#include <linux/err.h>
84#include <linux/i2c.h>
85#include <linux/gpio.h>
86#include <linux/workqueue.h>
87#include <linux/leds-tca6507.h>
Sachin Kamatfbd9df22013-03-14 03:09:49 -070088#include <linux/of.h>
NeilBrowna6d511e2012-01-10 15:09:40 -080089
90/* LED select registers determine the source that drives LED outputs */
91#define TCA6507_LS_LED_OFF 0x0 /* Output HI-Z (off) */
92#define TCA6507_LS_LED_OFF1 0x1 /* Output HI-Z (off) - not used */
93#define TCA6507_LS_LED_PWM0 0x2 /* Output LOW with Bank0 rate */
94#define TCA6507_LS_LED_PWM1 0x3 /* Output LOW with Bank1 rate */
95#define TCA6507_LS_LED_ON 0x4 /* Output LOW (on) */
96#define TCA6507_LS_LED_MIR 0x5 /* Output LOW with Master Intensity */
97#define TCA6507_LS_BLINK0 0x6 /* Blink at Bank0 rate */
98#define TCA6507_LS_BLINK1 0x7 /* Blink at Bank1 rate */
99
100enum {
101 BANK0,
102 BANK1,
103 MASTER,
104};
105static int bank_source[3] = {
106 TCA6507_LS_LED_PWM0,
107 TCA6507_LS_LED_PWM1,
108 TCA6507_LS_LED_MIR,
109};
110static int blink_source[2] = {
111 TCA6507_LS_BLINK0,
112 TCA6507_LS_BLINK1,
113};
114
115/* PWM registers */
116#define TCA6507_REG_CNT 11
117
118/*
119 * 0x00, 0x01, 0x02 encode the TCA6507_LS_* values, each output
120 * owns one bit in each register
121 */
122#define TCA6507_FADE_ON 0x03
123#define TCA6507_FULL_ON 0x04
124#define TCA6507_FADE_OFF 0x05
125#define TCA6507_FIRST_OFF 0x06
126#define TCA6507_SECOND_OFF 0x07
127#define TCA6507_MAX_INTENSITY 0x08
128#define TCA6507_MASTER_INTENSITY 0x09
129#define TCA6507_INITIALIZE 0x0A
130
131#define INIT_CODE 0x8
132
133#define TIMECODES 16
134static int time_codes[TIMECODES] = {
135 0, 64, 128, 192, 256, 384, 512, 768,
136 1024, 1536, 2048, 3072, 4096, 5760, 8128, 16320
137};
138
139/* Convert an led.brightness level (0..255) to a TCA6507 level (0..15) */
140static inline int TO_LEVEL(int brightness)
141{
142 return brightness >> 4;
143}
144
145/* ...and convert back */
146static inline int TO_BRIGHT(int level)
147{
148 if (level)
149 return (level << 4) | 0xf;
150 return 0;
151}
152
153#define NUM_LEDS 7
154struct tca6507_chip {
155 int reg_set; /* One bit per register where
156 * a '1' means the register
157 * should be written */
158 u8 reg_file[TCA6507_REG_CNT];
159 /* Bank 2 is Master Intensity and doesn't use times */
160 struct bank {
161 int level;
162 int ontime, offtime;
163 int on_dflt, off_dflt;
164 int time_use, level_use;
165 } bank[3];
166 struct i2c_client *client;
167 struct work_struct work;
168 spinlock_t lock;
169
170 struct tca6507_led {
171 struct tca6507_chip *chip;
172 struct led_classdev led_cdev;
173 int num;
174 int ontime, offtime;
175 int on_dflt, off_dflt;
176 int bank; /* Bank used, or -1 */
177 int blink; /* Set if hardware-blinking */
178 } leds[NUM_LEDS];
179#ifdef CONFIG_GPIOLIB
180 struct gpio_chip gpio;
181 const char *gpio_name[NUM_LEDS];
182 int gpio_map[NUM_LEDS];
183#endif
184};
185
186static const struct i2c_device_id tca6507_id[] = {
187 { "tca6507" },
188 { }
189};
190MODULE_DEVICE_TABLE(i2c, tca6507_id);
191
192static int choose_times(int msec, int *c1p, int *c2p)
193{
194 /*
195 * Choose two timecodes which add to 'msec' as near as possible.
196 * The first returned is the 'on' or 'off' time. The second is to be
197 * used as a 'fade-on' or 'fade-off' time. If 'msec' is even,
198 * the first will not be smaller than the second. If 'msec' is odd,
199 * the first will not be larger than the second.
200 * If we cannot get a sum within 1/8 of 'msec' fail with -EINVAL,
201 * otherwise return the sum that was achieved, plus 1 if the first is
202 * smaller.
203 * If two possibilities are equally good (e.g. 512+0, 256+256), choose
204 * the first pair so there is more change-time visible (i.e. it is
205 * softer).
206 */
207 int c1, c2;
208 int tmax = msec * 9 / 8;
209 int tmin = msec * 7 / 8;
210 int diff = 65536;
211
212 /* We start at '1' to ensure we never even think of choosing a
213 * total time of '0'.
214 */
215 for (c1 = 1; c1 < TIMECODES; c1++) {
216 int t = time_codes[c1];
217 if (t*2 < tmin)
218 continue;
219 if (t > tmax)
220 break;
221 for (c2 = 0; c2 <= c1; c2++) {
222 int tt = t + time_codes[c2];
223 int d;
224 if (tt < tmin)
225 continue;
226 if (tt > tmax)
227 break;
228 /* This works! */
229 d = abs(msec - tt);
230 if (d >= diff)
231 continue;
232 /* Best yet */
233 *c1p = c1;
234 *c2p = c2;
235 diff = d;
236 if (d == 0)
237 return msec;
238 }
239 }
240 if (diff < 65536) {
241 int actual;
242 if (msec & 1) {
243 c1 = *c2p;
244 *c2p = *c1p;
245 *c1p = c1;
246 }
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/*
258 * Update the register file with the appropriate 3-bit state for
259 * the given led.
260 */
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
277/* Update the register file with the appropriate 4-bit code for
278 * one bank or other. This can be used for timers, for levels, or
279 * for initialisation.
280 */
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
312/* Record all relevant time code for a given bank */
313static 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);
319 dev_dbg(&tca->client->dev,
320 "Chose on times %d(%d) %d(%d) for %dms\n", c1, time_codes[c1],
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;
325
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", c1, time_codes[c1],
329 c2, time_codes[c2], tca->bank[bank].offtime);
330 set_code(tca, TCA6507_FADE_OFF, bank, c2);
331 set_code(tca, TCA6507_FIRST_OFF, bank, c1);
332 set_code(tca, TCA6507_SECOND_OFF, bank, c1);
333 tca->bank[bank].offtime = result;
334
335 set_code(tca, TCA6507_INITIALIZE, bank, INIT_CODE);
336}
337
338/* Write all needed register of tca6507 */
339
340static void tca6507_work(struct work_struct *work)
341{
342 struct tca6507_chip *tca = container_of(work, struct tca6507_chip,
343 work);
344 struct i2c_client *cl = tca->client;
345 int set;
346 u8 file[TCA6507_REG_CNT];
347 int r;
348
349 spin_lock_irq(&tca->lock);
350 set = tca->reg_set;
351 memcpy(file, tca->reg_file, TCA6507_REG_CNT);
352 tca->reg_set = 0;
353 spin_unlock_irq(&tca->lock);
354
355 for (r = 0; r < TCA6507_REG_CNT; r++)
356 if (set & (1<<r))
357 i2c_smbus_write_byte_data(cl, r, file[r]);
358}
359
360static void led_release(struct tca6507_led *led)
361{
362 /* If led owns any resource, release it. */
363 struct tca6507_chip *tca = led->chip;
364 if (led->bank >= 0) {
365 struct bank *b = tca->bank + led->bank;
366 if (led->blink)
367 b->time_use--;
368 b->level_use--;
369 }
370 led->blink = 0;
371 led->bank = -1;
372}
373
374static int led_prepare(struct tca6507_led *led)
375{
376 /* Assign this led to a bank, configuring that bank if necessary. */
377 int level = TO_LEVEL(led->led_cdev.brightness);
378 struct tca6507_chip *tca = led->chip;
379 int c1, c2;
380 int i;
381 struct bank *b;
382 int need_init = 0;
383
384 led->led_cdev.brightness = TO_BRIGHT(level);
385 if (level == 0) {
386 set_select(tca, led->num, TCA6507_LS_LED_OFF);
387 return 0;
388 }
389
390 if (led->ontime == 0 || led->offtime == 0) {
391 /*
392 * Just set the brightness, choosing first usable bank.
393 * If none perfect, choose best.
394 * Count backwards so we check MASTER bank first
395 * to avoid wasting a timer.
396 */
397 int best = -1;/* full-on */
398 int diff = 15-level;
399
400 if (level == 15) {
401 set_select(tca, led->num, TCA6507_LS_LED_ON);
402 return 0;
403 }
404
405 for (i = MASTER; i >= BANK0; i--) {
406 int d;
407 if (tca->bank[i].level == level ||
408 tca->bank[i].level_use == 0) {
409 best = i;
410 break;
411 }
412 d = abs(level - tca->bank[i].level);
413 if (d < diff) {
414 diff = d;
415 best = i;
416 }
417 }
418 if (best == -1) {
419 /* Best brightness is full-on */
420 set_select(tca, led->num, TCA6507_LS_LED_ON);
421 led->led_cdev.brightness = LED_FULL;
422 return 0;
423 }
424
425 if (!tca->bank[best].level_use)
426 set_level(tca, best, level);
427
428 tca->bank[best].level_use++;
429 led->bank = best;
430 set_select(tca, led->num, bank_source[best]);
431 led->led_cdev.brightness = TO_BRIGHT(tca->bank[best].level);
432 return 0;
433 }
434
435 /*
436 * We have on/off time so we need to try to allocate a timing bank.
437 * First check if times are compatible with hardware and give up if
438 * not.
439 */
440 if (choose_times(led->ontime, &c1, &c2) < 0)
441 return -EINVAL;
442 if (choose_times(led->offtime, &c1, &c2) < 0)
443 return -EINVAL;
444
445 for (i = BANK0; i <= BANK1; i++) {
446 if (tca->bank[i].level_use == 0)
447 /* not in use - it is ours! */
448 break;
449 if (tca->bank[i].level != level)
450 /* Incompatible level - skip */
451 /* FIX: if timer matches we maybe should consider
452 * this anyway...
453 */
454 continue;
455
456 if (tca->bank[i].time_use == 0)
457 /* Timer not in use, and level matches - use it */
458 break;
459
460 if (!(tca->bank[i].on_dflt ||
461 led->on_dflt ||
462 tca->bank[i].ontime == led->ontime))
463 /* on time is incompatible */
464 continue;
465
466 if (!(tca->bank[i].off_dflt ||
467 led->off_dflt ||
468 tca->bank[i].offtime == led->offtime))
469 /* off time is incompatible */
470 continue;
471
472 /* looks like a suitable match */
473 break;
474 }
475
476 if (i > BANK1)
477 /* Nothing matches - how sad */
478 return -EINVAL;
479
480 b = &tca->bank[i];
481 if (b->level_use == 0)
482 set_level(tca, i, level);
483 b->level_use++;
484 led->bank = i;
485
486 if (b->on_dflt ||
487 !led->on_dflt ||
488 b->time_use == 0) {
489 b->ontime = led->ontime;
490 b->on_dflt = led->on_dflt;
491 need_init = 1;
492 }
493
494 if (b->off_dflt ||
495 !led->off_dflt ||
496 b->time_use == 0) {
497 b->offtime = led->offtime;
498 b->off_dflt = led->off_dflt;
499 need_init = 1;
500 }
501
502 if (need_init)
503 set_times(tca, i);
504
505 led->ontime = b->ontime;
506 led->offtime = b->offtime;
507
508 b->time_use++;
509 led->blink = 1;
510 led->led_cdev.brightness = TO_BRIGHT(b->level);
511 set_select(tca, led->num, blink_source[i]);
512 return 0;
513}
514
515static int led_assign(struct tca6507_led *led)
516{
517 struct tca6507_chip *tca = led->chip;
518 int err;
519 unsigned long flags;
520
521 spin_lock_irqsave(&tca->lock, flags);
522 led_release(led);
523 err = led_prepare(led);
524 if (err) {
525 /*
526 * Can only fail on timer setup. In that case we need to
527 * re-establish as steady level.
528 */
529 led->ontime = 0;
530 led->offtime = 0;
531 led_prepare(led);
532 }
533 spin_unlock_irqrestore(&tca->lock, flags);
534
535 if (tca->reg_set)
536 schedule_work(&tca->work);
537 return err;
538}
539
540static void tca6507_brightness_set(struct led_classdev *led_cdev,
541 enum led_brightness brightness)
542{
543 struct tca6507_led *led = container_of(led_cdev, struct tca6507_led,
544 led_cdev);
545 led->led_cdev.brightness = brightness;
546 led->ontime = 0;
547 led->offtime = 0;
548 led_assign(led);
549}
550
551static int tca6507_blink_set(struct led_classdev *led_cdev,
552 unsigned long *delay_on,
553 unsigned long *delay_off)
554{
555 struct tca6507_led *led = container_of(led_cdev, struct tca6507_led,
556 led_cdev);
557
558 if (*delay_on == 0)
559 led->on_dflt = 1;
560 else if (delay_on != &led_cdev->blink_delay_on)
561 led->on_dflt = 0;
562 led->ontime = *delay_on;
563
564 if (*delay_off == 0)
565 led->off_dflt = 1;
566 else if (delay_off != &led_cdev->blink_delay_off)
567 led->off_dflt = 0;
568 led->offtime = *delay_off;
569
570 if (led->ontime == 0)
571 led->ontime = 512;
572 if (led->offtime == 0)
573 led->offtime = 512;
574
575 if (led->led_cdev.brightness == LED_OFF)
576 led->led_cdev.brightness = LED_FULL;
577 if (led_assign(led) < 0) {
578 led->ontime = 0;
579 led->offtime = 0;
580 led->led_cdev.brightness = LED_OFF;
581 return -EINVAL;
582 }
583 *delay_on = led->ontime;
584 *delay_off = led->offtime;
585 return 0;
586}
587
588#ifdef CONFIG_GPIOLIB
589static void tca6507_gpio_set_value(struct gpio_chip *gc,
590 unsigned offset, int val)
591{
592 struct tca6507_chip *tca = container_of(gc, struct tca6507_chip, gpio);
593 unsigned long flags;
594
595 spin_lock_irqsave(&tca->lock, flags);
596 /*
597 * 'OFF' is floating high, and 'ON' is pulled down, so it has the
598 * inverse sense of 'val'.
599 */
600 set_select(tca, tca->gpio_map[offset],
601 val ? TCA6507_LS_LED_OFF : TCA6507_LS_LED_ON);
602 spin_unlock_irqrestore(&tca->lock, flags);
603 if (tca->reg_set)
604 schedule_work(&tca->work);
605}
606
607static int tca6507_gpio_direction_output(struct gpio_chip *gc,
608 unsigned offset, int val)
609{
610 tca6507_gpio_set_value(gc, offset, val);
611 return 0;
612}
613
614static int tca6507_probe_gpios(struct i2c_client *client,
615 struct tca6507_chip *tca,
616 struct tca6507_platform_data *pdata)
617{
618 int err;
619 int i = 0;
620 int gpios = 0;
621
622 for (i = 0; i < NUM_LEDS; i++)
623 if (pdata->leds.leds[i].name && pdata->leds.leds[i].flags) {
624 /* Configure as a gpio */
625 tca->gpio_name[gpios] = pdata->leds.leds[i].name;
626 tca->gpio_map[gpios] = i;
627 gpios++;
628 }
629
630 if (!gpios)
631 return 0;
632
633 tca->gpio.label = "gpio-tca6507";
634 tca->gpio.names = tca->gpio_name;
635 tca->gpio.ngpio = gpios;
636 tca->gpio.base = pdata->gpio_base;
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.dev = &client->dev;
641 err = gpiochip_add(&tca->gpio);
642 if (err) {
643 tca->gpio.ngpio = 0;
644 return err;
645 }
646 if (pdata->setup)
647 pdata->setup(tca->gpio.base, tca->gpio.ngpio);
648 return 0;
649}
650
651static void tca6507_remove_gpio(struct tca6507_chip *tca)
652{
653 if (tca->gpio.ngpio) {
654 int err = gpiochip_remove(&tca->gpio);
655 dev_err(&tca->client->dev, "%s failed, %d\n",
656 "gpiochip_remove()", err);
657 }
658}
659#else /* CONFIG_GPIOLIB */
660static int tca6507_probe_gpios(struct i2c_client *client,
661 struct tca6507_chip *tca,
662 struct tca6507_platform_data *pdata)
663{
664 return 0;
665}
666static void tca6507_remove_gpio(struct tca6507_chip *tca)
667{
668}
669#endif /* CONFIG_GPIOLIB */
670
Marek Belisko0c596a72012-11-30 14:00:49 -0800671#ifdef CONFIG_OF
672static struct tca6507_platform_data *
673tca6507_led_dt_init(struct i2c_client *client)
674{
675 struct device_node *np = client->dev.of_node, *child;
676 struct tca6507_platform_data *pdata;
677 struct led_info *tca_leds;
Axel Linef754e82013-01-27 01:14:14 -0800678 int count;
Marek Belisko0c596a72012-11-30 14:00:49 -0800679
Axel Linef754e82013-01-27 01:14:14 -0800680 count = of_get_child_count(np);
681 if (!count || count > NUM_LEDS)
Marek Belisko0c596a72012-11-30 14:00:49 -0800682 return ERR_PTR(-ENODEV);
683
684 tca_leds = devm_kzalloc(&client->dev,
685 sizeof(struct led_info) * count, GFP_KERNEL);
686 if (!tca_leds)
687 return ERR_PTR(-ENOMEM);
688
689 for_each_child_of_node(np, child) {
690 struct led_info led;
691 u32 reg;
692 int ret;
693
694 led.name =
695 of_get_property(child, "label", NULL) ? : child->name;
696 led.default_trigger =
697 of_get_property(child, "linux,default-trigger", NULL);
698
699 ret = of_property_read_u32(child, "reg", &reg);
700 if (ret != 0)
701 continue;
702
703 tca_leds[reg] = led;
704 }
705 pdata = devm_kzalloc(&client->dev,
706 sizeof(struct tca6507_platform_data), GFP_KERNEL);
707 if (!pdata)
708 return ERR_PTR(-ENOMEM);
709
710 pdata->leds.leds = tca_leds;
711 pdata->leds.num_leds = count;
712
713 return pdata;
714}
715
716static const struct of_device_id of_tca6507_leds_match[] = {
717 { .compatible = "ti,tca6507", },
718 {},
719};
720
721#else
722static struct tca6507_platform_data *
723tca6507_led_dt_init(struct i2c_client *client)
724{
725 return ERR_PTR(-ENODEV);
726}
727
Marek Belisko0c596a72012-11-30 14:00:49 -0800728#endif
729
Bill Pemberton98ea1ea2012-11-19 13:23:02 -0500730static int tca6507_probe(struct i2c_client *client,
Marek Belisko0c596a72012-11-30 14:00:49 -0800731 const struct i2c_device_id *id)
NeilBrowna6d511e2012-01-10 15:09:40 -0800732{
733 struct tca6507_chip *tca;
734 struct i2c_adapter *adapter;
735 struct tca6507_platform_data *pdata;
736 int err;
737 int i = 0;
738
739 adapter = to_i2c_adapter(client->dev.parent);
Jingoo Han87aae1e2013-07-30 01:07:35 -0700740 pdata = dev_get_platdata(&client->dev);
NeilBrowna6d511e2012-01-10 15:09:40 -0800741
742 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C))
743 return -EIO;
744
745 if (!pdata || pdata->leds.num_leds != NUM_LEDS) {
Marek Belisko0c596a72012-11-30 14:00:49 -0800746 pdata = tca6507_led_dt_init(client);
747 if (IS_ERR(pdata)) {
748 dev_err(&client->dev, "Need %d entries in platform-data list\n",
749 NUM_LEDS);
750 return PTR_ERR(pdata);
751 }
NeilBrowna6d511e2012-01-10 15:09:40 -0800752 }
Bryan Wu6770fc62012-07-04 12:20:41 +0800753 tca = devm_kzalloc(&client->dev, sizeof(*tca), GFP_KERNEL);
NeilBrowna6d511e2012-01-10 15:09:40 -0800754 if (!tca)
Dan Carpenter920c4f42012-03-23 15:02:07 -0700755 return -ENOMEM;
NeilBrowna6d511e2012-01-10 15:09:40 -0800756
757 tca->client = client;
758 INIT_WORK(&tca->work, tca6507_work);
759 spin_lock_init(&tca->lock);
760 i2c_set_clientdata(client, tca);
761
762 for (i = 0; i < NUM_LEDS; i++) {
763 struct tca6507_led *l = tca->leds + i;
764
765 l->chip = tca;
766 l->num = i;
767 if (pdata->leds.leds[i].name && !pdata->leds.leds[i].flags) {
768 l->led_cdev.name = pdata->leds.leds[i].name;
769 l->led_cdev.default_trigger
770 = pdata->leds.leds[i].default_trigger;
771 l->led_cdev.brightness_set = tca6507_brightness_set;
772 l->led_cdev.blink_set = tca6507_blink_set;
773 l->bank = -1;
774 err = led_classdev_register(&client->dev,
775 &l->led_cdev);
776 if (err < 0)
777 goto exit;
778 }
779 }
780 err = tca6507_probe_gpios(client, tca, pdata);
781 if (err)
782 goto exit;
783 /* set all registers to known state - zero */
784 tca->reg_set = 0x7f;
785 schedule_work(&tca->work);
786
787 return 0;
788exit:
Dan Carpenter920c4f42012-03-23 15:02:07 -0700789 while (i--) {
NeilBrowna6d511e2012-01-10 15:09:40 -0800790 if (tca->leds[i].led_cdev.name)
791 led_classdev_unregister(&tca->leds[i].led_cdev);
Dan Carpenter920c4f42012-03-23 15:02:07 -0700792 }
NeilBrowna6d511e2012-01-10 15:09:40 -0800793 return err;
794}
795
Bill Pemberton678e8a62012-11-19 13:26:00 -0500796static int tca6507_remove(struct i2c_client *client)
NeilBrowna6d511e2012-01-10 15:09:40 -0800797{
798 int i;
799 struct tca6507_chip *tca = i2c_get_clientdata(client);
800 struct tca6507_led *tca_leds = tca->leds;
801
802 for (i = 0; i < NUM_LEDS; i++) {
803 if (tca_leds[i].led_cdev.name)
804 led_classdev_unregister(&tca_leds[i].led_cdev);
805 }
806 tca6507_remove_gpio(tca);
807 cancel_work_sync(&tca->work);
NeilBrowna6d511e2012-01-10 15:09:40 -0800808
809 return 0;
810}
811
812static struct i2c_driver tca6507_driver = {
813 .driver = {
814 .name = "leds-tca6507",
815 .owner = THIS_MODULE,
Sachin Kamatfbd9df22013-03-14 03:09:49 -0700816 .of_match_table = of_match_ptr(of_tca6507_leds_match),
NeilBrowna6d511e2012-01-10 15:09:40 -0800817 },
818 .probe = tca6507_probe,
Bill Pembertondf07cf82012-11-19 13:20:20 -0500819 .remove = tca6507_remove,
NeilBrowna6d511e2012-01-10 15:09:40 -0800820 .id_table = tca6507_id,
821};
822
Peter Meerwald2e911762012-07-03 16:03:38 +0800823module_i2c_driver(tca6507_driver);
NeilBrowna6d511e2012-01-10 15:09:40 -0800824
825MODULE_AUTHOR("NeilBrown <neilb@suse.de>");
826MODULE_DESCRIPTION("TCA6507 LED/GPO driver");
827MODULE_LICENSE("GPL v2");