80 #include <linux/module.h>
81 #include <linux/slab.h>
84 #include <linux/i2c.h>
90 #define TCA6507_LS_LED_OFF 0x0
91 #define TCA6507_LS_LED_OFF1 0x1
92 #define TCA6507_LS_LED_PWM0 0x2
93 #define TCA6507_LS_LED_PWM1 0x3
94 #define TCA6507_LS_LED_ON 0x4
95 #define TCA6507_LS_LED_MIR 0x5
96 #define TCA6507_LS_BLINK0 0x6
97 #define TCA6507_LS_BLINK1 0x7
104 static int bank_source[3] = {
109 static int blink_source[2] = {
115 #define TCA6507_REG_CNT 11
121 #define TCA6507_FADE_ON 0x03
122 #define TCA6507_FULL_ON 0x04
123 #define TCA6507_FADE_OFF 0x05
124 #define TCA6507_FIRST_OFF 0x06
125 #define TCA6507_SECOND_OFF 0x07
126 #define TCA6507_MAX_INTENSITY 0x08
127 #define TCA6507_MASTER_INTENSITY 0x09
128 #define TCA6507_INITIALIZE 0x0A
130 #define INIT_CODE 0x8
134 0, 64, 128, 192, 256, 384, 512, 768,
135 1024, 1536, 2048, 3072, 4096, 5760, 8128, 16320
141 return brightness >> 4;
145 static inline int TO_BRIGHT(
int level)
148 return (level << 4) | 0xf;
178 #ifdef CONFIG_GPIOLIB
179 struct gpio_chip
gpio;
191 static int choose_times(
int msec,
int *c1p,
int *c2p)
207 int tmax = msec * 9 / 8;
208 int tmin = msec * 7 / 8;
215 int t = time_codes[c1];
220 for (c2 = 0; c2 <= c1; c2++) {
221 int tt = t + time_codes[c2];
246 actual = time_codes[*c1p] + time_codes[*c2p];
265 for (bit = 0; bit < 3; bit++) {
267 if (val & (1 << bit))
280 static void set_code(
struct tca6507_chip *tca,
int reg,
int bank,
int new)
297 static void set_level(
struct tca6507_chip *tca,
int bank,
int level)
312 static void set_times(
struct tca6507_chip *tca,
int bank)
317 result = choose_times(tca->
bank[bank].ontime, &c1, &c2);
319 "Chose on times %d(%d) %d(%d) for %dms\n", c1, time_codes[c1],
320 c2, time_codes[c2], tca->
bank[bank].ontime);
325 result = choose_times(tca->
bank[bank].offtime, &c1, &c2);
327 "Chose off times %d(%d) %d(%d) for %dms\n", c1, time_codes[c1],
328 c2, time_codes[c2], tca->
bank[bank].offtime);
348 spin_lock_irq(&tca->
lock);
352 spin_unlock_irq(&tca->
lock);
359 static void led_release(
struct tca6507_led *led)
363 if (led->bank >= 0) {
364 struct bank *
b = tca->
bank + led->bank;
373 static int led_prepare(
struct tca6507_led *led)
376 int level = TO_LEVEL(led->led_cdev.brightness);
383 led->led_cdev.brightness = TO_BRIGHT(level);
389 if (led->ontime == 0 || led->offtime == 0) {
406 if (tca->
bank[i].level == level ||
407 tca->
bank[i].level_use == 0) {
411 d =
abs(level - tca->
bank[i].level);
420 led->led_cdev.brightness =
LED_FULL;
424 if (!tca->
bank[best].level_use)
425 set_level(tca, best, level);
427 tca->
bank[best].level_use++;
429 set_select(tca, led->num, bank_source[best]);
430 led->led_cdev.brightness = TO_BRIGHT(tca->
bank[best].level);
439 if (choose_times(led->ontime, &c1, &c2) < 0)
441 if (choose_times(led->offtime, &c1, &c2) < 0)
445 if (tca->
bank[i].level_use == 0)
448 if (tca->
bank[i].level != level)
455 if (tca->
bank[i].time_use == 0)
459 if (!(tca->
bank[i].on_dflt ||
461 tca->
bank[i].ontime == led->ontime))
465 if (!(tca->
bank[i].off_dflt ||
467 tca->
bank[i].offtime == led->offtime))
480 if (b->level_use == 0)
481 set_level(tca, i, level);
488 b->ontime = led->ontime;
489 b->on_dflt = led->on_dflt;
496 b->offtime = led->offtime;
497 b->off_dflt = led->off_dflt;
504 led->ontime = b->ontime;
505 led->offtime = b->offtime;
509 led->led_cdev.brightness = TO_BRIGHT(b->level);
510 set_select(tca, led->num, blink_source[i]);
514 static int led_assign(
struct tca6507_led *led)
522 err = led_prepare(led);
532 spin_unlock_irqrestore(&tca->
lock, flags);
539 static void tca6507_brightness_set(
struct led_classdev *led_cdev,
542 struct tca6507_led *led =
container_of(led_cdev,
struct tca6507_led,
550 static int tca6507_blink_set(
struct led_classdev *led_cdev,
551 unsigned long *delay_on,
552 unsigned long *delay_off)
554 struct tca6507_led *led =
container_of(led_cdev,
struct tca6507_led,
561 led->ontime = *delay_on;
567 led->offtime = *delay_off;
569 if (led->ontime == 0)
571 if (led->offtime == 0)
574 if (led->led_cdev.brightness ==
LED_OFF)
575 led->led_cdev.brightness =
LED_FULL;
576 if (led_assign(led) < 0) {
579 led->led_cdev.brightness =
LED_OFF;
582 *delay_on = led->ontime;
583 *delay_off = led->offtime;
587 #ifdef CONFIG_GPIOLIB
588 static void tca6507_gpio_set_value(
struct gpio_chip *
gc,
599 set_select(tca, tca->gpio_map[offset],
601 spin_unlock_irqrestore(&tca->
lock, flags);
606 static int tca6507_gpio_direction_output(
struct gpio_chip *gc,
607 unsigned offset,
int val)
609 tca6507_gpio_set_value(gc, offset, val);
622 if (pdata->
leds.leds[i].name && pdata->
leds.leds[i].flags) {
624 tca->gpio_name[gpios] = pdata->
leds.leds[
i].name;
625 tca->gpio_map[gpios] =
i;
632 tca->gpio.label =
"gpio-tca6507";
633 tca->gpio.names = tca->gpio_name;
634 tca->gpio.ngpio = gpios;
635 tca->gpio.base = pdata->gpio_base;
637 tca->gpio.direction_output = tca6507_gpio_direction_output;
638 tca->gpio.set = tca6507_gpio_set_value;
639 tca->gpio.dev = &client->
dev;
646 pdata->setup(tca->gpio.base, tca->gpio.ngpio);
650 static void tca6507_remove_gpio(
struct tca6507_chip *tca)
652 if (tca->gpio.ngpio) {
655 "gpiochip_remove()", err);
659 static int tca6507_probe_gpios(
struct i2c_client *client,
665 static void tca6507_remove_gpio(
struct tca6507_chip *tca)
680 pdata = client->
dev.platform_data;
685 if (!pdata || pdata->
leds.num_leds != NUM_LEDS) {
686 dev_err(&client->
dev,
"Need %d entries in platform-data list\n",
697 i2c_set_clientdata(client, tca);
700 struct tca6507_led *
l = tca->
leds +
i;
704 if (pdata->
leds.leds[i].name && !pdata->
leds.leds[i].flags) {
705 l->led_cdev.name = pdata->
leds.leds[
i].name;
706 l->led_cdev.default_trigger
707 = pdata->
leds.leds[
i].default_trigger;
708 l->led_cdev.brightness_set = tca6507_brightness_set;
709 l->led_cdev.blink_set = tca6507_blink_set;
717 err = tca6507_probe_gpios(client, tca, pdata);
727 if (tca->
leds[i].led_cdev.name)
737 struct tca6507_led *tca_leds = tca->
leds;
740 if (tca_leds[i].led_cdev.
name)
743 tca6507_remove_gpio(tca);
751 .name =
"leds-tca6507",
754 .probe = tca6507_probe,
756 .id_table = tca6507_id,