1a6d511e5SNeilBrown /* 2a6d511e5SNeilBrown * leds-tca6507 3a6d511e5SNeilBrown * 4a6d511e5SNeilBrown * The TCA6507 is a programmable LED controller that can drive 7 5a6d511e5SNeilBrown * separate lines either by holding them low, or by pulsing them 6a6d511e5SNeilBrown * with modulated width. 71f431afdSNeilBrown * The modulation can be varied in a simple pattern to produce a 81f431afdSNeilBrown * blink or double-blink. 9a6d511e5SNeilBrown * 101f431afdSNeilBrown * This driver can configure each line either as a 'GPIO' which is 111f431afdSNeilBrown * out-only (pull-up resistor required) or as an LED with variable 121f431afdSNeilBrown * brightness and hardware-assisted blinking. 13a6d511e5SNeilBrown * 141f431afdSNeilBrown * Apart from OFF and ON there are three programmable brightness 151f431afdSNeilBrown * levels which can be programmed from 0 to 15 and indicate how many 161f431afdSNeilBrown * 500usec intervals in each 8msec that the led is 'on'. The levels 171f431afdSNeilBrown * are named MASTER, BANK0 and BANK1. 18a6d511e5SNeilBrown * 191f431afdSNeilBrown * There are two different blink rates that can be programmed, each 201f431afdSNeilBrown * with separate time for rise, on, fall, off and second-off. Thus if 211f431afdSNeilBrown * 3 or more different non-trivial rates are required, software must 221f431afdSNeilBrown * be used for the extra rates. The two different blink rates must 231f431afdSNeilBrown * align with the two levels BANK0 and BANK1. This driver does not 241f431afdSNeilBrown * support double-blink so 'second-off' always matches 'off'. 25a6d511e5SNeilBrown * 261f431afdSNeilBrown * Only 16 different times can be programmed in a roughly logarithmic 271f431afdSNeilBrown * scale from 64ms to 16320ms. To be precise the possible times are: 28a6d511e5SNeilBrown * 0, 64, 128, 192, 256, 384, 512, 768, 29a6d511e5SNeilBrown * 1024, 1536, 2048, 3072, 4096, 5760, 8128, 16320 30a6d511e5SNeilBrown * 311f431afdSNeilBrown * Times that cannot be closely matched with these must be handled in 321f431afdSNeilBrown * software. This driver allows 12.5% error in matching. 33a6d511e5SNeilBrown * 341f431afdSNeilBrown * This driver does not allow rise/fall rates to be set explicitly. 351f431afdSNeilBrown * When trying to match a given 'on' or 'off' period, an appropriate 361f431afdSNeilBrown * pair of 'change' and 'hold' times are chosen to get a close match. 371f431afdSNeilBrown * If the target delay is even, the 'change' number will be the 381f431afdSNeilBrown * smaller; if odd, the 'hold' number will be the smaller. 39a6d511e5SNeilBrown 401f431afdSNeilBrown * Choosing pairs of delays with 12.5% errors allows us to match 411f431afdSNeilBrown * delays in the ranges: 56-72, 112-144, 168-216, 224-27504, 421f431afdSNeilBrown * 28560-36720. 431f431afdSNeilBrown * 26% of the achievable sums can be matched by multiple pairings. 441f431afdSNeilBrown * For example 1536 == 1536+0, 1024+512, or 768+768. 451f431afdSNeilBrown * This driver will always choose the pairing with the least 461f431afdSNeilBrown * maximum - 768+768 in this case. Other pairings are not available. 47a6d511e5SNeilBrown * 481f431afdSNeilBrown * Access to the 3 levels and 2 blinks are on a first-come, 491f431afdSNeilBrown * first-served basis. Access can be shared by multiple leds if they 501f431afdSNeilBrown * have the same level and either same blink rates, or some don't 511f431afdSNeilBrown * blink. When a led changes, it relinquishes access and tries again, 521f431afdSNeilBrown * so it might lose access to hardware blink. 53a6d511e5SNeilBrown * 541f431afdSNeilBrown * If a blink engine cannot be allocated, software blink is used. If 551f431afdSNeilBrown * the desired brightness cannot be allocated, the closest available 561f431afdSNeilBrown * non-zero brightness is used. As 'full' is always available, the 571f431afdSNeilBrown * worst case would be to have two different blink rates at '1', with 581f431afdSNeilBrown * Max at '2', then other leds will have to choose between '2' and 591f431afdSNeilBrown * '16'. Hopefully this is not likely. 60a6d511e5SNeilBrown * 611f431afdSNeilBrown * Each bank (BANK0 and BANK1) has two usage counts - LEDs using the 621f431afdSNeilBrown * brightness and LEDs using the blink. It can only be reprogrammed 631f431afdSNeilBrown * when the appropriate counter is zero. The MASTER level has a 641f431afdSNeilBrown * single usage count. 651f431afdSNeilBrown * 661f431afdSNeilBrown * Each LED has programmable 'on' and 'off' time as milliseconds. 671f431afdSNeilBrown * With each there is a flag saying if it was explicitly requested or 681f431afdSNeilBrown * defaulted. Similarly the banks know if each time was explicit or a 691f431afdSNeilBrown * default. Defaults are permitted to be changed freely - they are 701f431afdSNeilBrown * not recognised when matching. 71a6d511e5SNeilBrown * 72a6d511e5SNeilBrown * 731f431afdSNeilBrown * An led-tca6507 device must be provided with platform data or 741f431afdSNeilBrown * configured via devicetree. 75a6d511e5SNeilBrown * 761f431afdSNeilBrown * The platform-data lists for each output: the name, default trigger, 771f431afdSNeilBrown * and whether the signal is being used as a GPIO rather than an LED. 781f431afdSNeilBrown * 'struct led_plaform_data' is used for this. If 'name' is NULL, the 791f431afdSNeilBrown * output isn't used. If 'flags' is TCA6507_MAKE_GPIO, the output is 801f431afdSNeilBrown * a GPO. The "struct led_platform_data" can be embedded in a "struct 811f431afdSNeilBrown * tca6507_platform_data" which adds a 'gpio_base' for the GPIOs, and 821f431afdSNeilBrown * a 'setup' callback which is called once the GPIOs are available. 831f431afdSNeilBrown * 841f431afdSNeilBrown * When configured via devicetree there is one child for each output. 851f431afdSNeilBrown * The "reg" determines the output number and "compatible" determines 861f431afdSNeilBrown * whether it is an LED or a GPIO. "linux,default-trigger" can set a 871f431afdSNeilBrown * default trigger. 88a6d511e5SNeilBrown */ 89a6d511e5SNeilBrown 90a6d511e5SNeilBrown #include <linux/module.h> 91a6d511e5SNeilBrown #include <linux/slab.h> 92a6d511e5SNeilBrown #include <linux/leds.h> 93a6d511e5SNeilBrown #include <linux/err.h> 94a6d511e5SNeilBrown #include <linux/i2c.h> 95a6d511e5SNeilBrown #include <linux/gpio.h> 96a6d511e5SNeilBrown #include <linux/workqueue.h> 97a6d511e5SNeilBrown #include <linux/leds-tca6507.h> 98fbd9df28SSachin Kamat #include <linux/of.h> 99a6d511e5SNeilBrown 100a6d511e5SNeilBrown /* LED select registers determine the source that drives LED outputs */ 101a6d511e5SNeilBrown #define TCA6507_LS_LED_OFF 0x0 /* Output HI-Z (off) */ 102a6d511e5SNeilBrown #define TCA6507_LS_LED_OFF1 0x1 /* Output HI-Z (off) - not used */ 103a6d511e5SNeilBrown #define TCA6507_LS_LED_PWM0 0x2 /* Output LOW with Bank0 rate */ 104a6d511e5SNeilBrown #define TCA6507_LS_LED_PWM1 0x3 /* Output LOW with Bank1 rate */ 105a6d511e5SNeilBrown #define TCA6507_LS_LED_ON 0x4 /* Output LOW (on) */ 106a6d511e5SNeilBrown #define TCA6507_LS_LED_MIR 0x5 /* Output LOW with Master Intensity */ 107a6d511e5SNeilBrown #define TCA6507_LS_BLINK0 0x6 /* Blink at Bank0 rate */ 108a6d511e5SNeilBrown #define TCA6507_LS_BLINK1 0x7 /* Blink at Bank1 rate */ 109a6d511e5SNeilBrown 110a6d511e5SNeilBrown enum { 111a6d511e5SNeilBrown BANK0, 112a6d511e5SNeilBrown BANK1, 113a6d511e5SNeilBrown MASTER, 114a6d511e5SNeilBrown }; 115a6d511e5SNeilBrown static int bank_source[3] = { 116a6d511e5SNeilBrown TCA6507_LS_LED_PWM0, 117a6d511e5SNeilBrown TCA6507_LS_LED_PWM1, 118a6d511e5SNeilBrown TCA6507_LS_LED_MIR, 119a6d511e5SNeilBrown }; 120a6d511e5SNeilBrown static int blink_source[2] = { 121a6d511e5SNeilBrown TCA6507_LS_BLINK0, 122a6d511e5SNeilBrown TCA6507_LS_BLINK1, 123a6d511e5SNeilBrown }; 124a6d511e5SNeilBrown 125a6d511e5SNeilBrown /* PWM registers */ 126a6d511e5SNeilBrown #define TCA6507_REG_CNT 11 127a6d511e5SNeilBrown 128a6d511e5SNeilBrown /* 129a6d511e5SNeilBrown * 0x00, 0x01, 0x02 encode the TCA6507_LS_* values, each output 130a6d511e5SNeilBrown * owns one bit in each register 131a6d511e5SNeilBrown */ 132a6d511e5SNeilBrown #define TCA6507_FADE_ON 0x03 133a6d511e5SNeilBrown #define TCA6507_FULL_ON 0x04 134a6d511e5SNeilBrown #define TCA6507_FADE_OFF 0x05 135a6d511e5SNeilBrown #define TCA6507_FIRST_OFF 0x06 136a6d511e5SNeilBrown #define TCA6507_SECOND_OFF 0x07 137a6d511e5SNeilBrown #define TCA6507_MAX_INTENSITY 0x08 138a6d511e5SNeilBrown #define TCA6507_MASTER_INTENSITY 0x09 139a6d511e5SNeilBrown #define TCA6507_INITIALIZE 0x0A 140a6d511e5SNeilBrown 141a6d511e5SNeilBrown #define INIT_CODE 0x8 142a6d511e5SNeilBrown 143a6d511e5SNeilBrown #define TIMECODES 16 144a6d511e5SNeilBrown static int time_codes[TIMECODES] = { 145a6d511e5SNeilBrown 0, 64, 128, 192, 256, 384, 512, 768, 146a6d511e5SNeilBrown 1024, 1536, 2048, 3072, 4096, 5760, 8128, 16320 147a6d511e5SNeilBrown }; 148a6d511e5SNeilBrown 149a6d511e5SNeilBrown /* Convert an led.brightness level (0..255) to a TCA6507 level (0..15) */ 150a6d511e5SNeilBrown static inline int TO_LEVEL(int brightness) 151a6d511e5SNeilBrown { 152a6d511e5SNeilBrown return brightness >> 4; 153a6d511e5SNeilBrown } 154a6d511e5SNeilBrown 155a6d511e5SNeilBrown /* ...and convert back */ 156a6d511e5SNeilBrown static inline int TO_BRIGHT(int level) 157a6d511e5SNeilBrown { 158a6d511e5SNeilBrown if (level) 159a6d511e5SNeilBrown return (level << 4) | 0xf; 160a6d511e5SNeilBrown return 0; 161a6d511e5SNeilBrown } 162a6d511e5SNeilBrown 163a6d511e5SNeilBrown #define NUM_LEDS 7 164a6d511e5SNeilBrown struct tca6507_chip { 165a6d511e5SNeilBrown int reg_set; /* One bit per register where 166a6d511e5SNeilBrown * a '1' means the register 167a6d511e5SNeilBrown * should be written */ 168a6d511e5SNeilBrown u8 reg_file[TCA6507_REG_CNT]; 169a6d511e5SNeilBrown /* Bank 2 is Master Intensity and doesn't use times */ 170a6d511e5SNeilBrown struct bank { 171a6d511e5SNeilBrown int level; 172a6d511e5SNeilBrown int ontime, offtime; 173a6d511e5SNeilBrown int on_dflt, off_dflt; 174a6d511e5SNeilBrown int time_use, level_use; 175a6d511e5SNeilBrown } bank[3]; 176a6d511e5SNeilBrown struct i2c_client *client; 177a6d511e5SNeilBrown struct work_struct work; 178a6d511e5SNeilBrown spinlock_t lock; 179a6d511e5SNeilBrown 180a6d511e5SNeilBrown struct tca6507_led { 181a6d511e5SNeilBrown struct tca6507_chip *chip; 182a6d511e5SNeilBrown struct led_classdev led_cdev; 183a6d511e5SNeilBrown int num; 184a6d511e5SNeilBrown int ontime, offtime; 185a6d511e5SNeilBrown int on_dflt, off_dflt; 186a6d511e5SNeilBrown int bank; /* Bank used, or -1 */ 187a6d511e5SNeilBrown int blink; /* Set if hardware-blinking */ 188a6d511e5SNeilBrown } leds[NUM_LEDS]; 189a6d511e5SNeilBrown #ifdef CONFIG_GPIOLIB 190a6d511e5SNeilBrown struct gpio_chip gpio; 191a6d511e5SNeilBrown const char *gpio_name[NUM_LEDS]; 192a6d511e5SNeilBrown int gpio_map[NUM_LEDS]; 193a6d511e5SNeilBrown #endif 194a6d511e5SNeilBrown }; 195a6d511e5SNeilBrown 196a6d511e5SNeilBrown static const struct i2c_device_id tca6507_id[] = { 197a6d511e5SNeilBrown { "tca6507" }, 198a6d511e5SNeilBrown { } 199a6d511e5SNeilBrown }; 200a6d511e5SNeilBrown MODULE_DEVICE_TABLE(i2c, tca6507_id); 201a6d511e5SNeilBrown 202a6d511e5SNeilBrown static int choose_times(int msec, int *c1p, int *c2p) 203a6d511e5SNeilBrown { 204a6d511e5SNeilBrown /* 2051f431afdSNeilBrown * Choose two timecodes which add to 'msec' as near as 2061f431afdSNeilBrown * possible. The first returned is the 'on' or 'off' time. 2071f431afdSNeilBrown * The second is to be used as a 'fade-on' or 'fade-off' time. 2081f431afdSNeilBrown * If 'msec' is even, the first will not be smaller than the 2091f431afdSNeilBrown * second. If 'msec' is odd, the first will not be larger 2101f431afdSNeilBrown * than the second. 2111f431afdSNeilBrown * If we cannot get a sum within 1/8 of 'msec' fail with 2121f431afdSNeilBrown * -EINVAL, otherwise return the sum that was achieved, plus 1 2131f431afdSNeilBrown * if the first is smaller. 2141f431afdSNeilBrown * If two possibilities are equally good (e.g. 512+0, 2151f431afdSNeilBrown * 256+256), choose the first pair so there is more 2161f431afdSNeilBrown * change-time visible (i.e. it is softer). 217a6d511e5SNeilBrown */ 218a6d511e5SNeilBrown int c1, c2; 219a6d511e5SNeilBrown int tmax = msec * 9 / 8; 220a6d511e5SNeilBrown int tmin = msec * 7 / 8; 221a6d511e5SNeilBrown int diff = 65536; 222a6d511e5SNeilBrown 223a6d511e5SNeilBrown /* We start at '1' to ensure we never even think of choosing a 224a6d511e5SNeilBrown * total time of '0'. 225a6d511e5SNeilBrown */ 226a6d511e5SNeilBrown for (c1 = 1; c1 < TIMECODES; c1++) { 227a6d511e5SNeilBrown int t = time_codes[c1]; 228a6d511e5SNeilBrown if (t*2 < tmin) 229a6d511e5SNeilBrown continue; 230a6d511e5SNeilBrown if (t > tmax) 231a6d511e5SNeilBrown break; 232a6d511e5SNeilBrown for (c2 = 0; c2 <= c1; c2++) { 233a6d511e5SNeilBrown int tt = t + time_codes[c2]; 234a6d511e5SNeilBrown int d; 235a6d511e5SNeilBrown if (tt < tmin) 236a6d511e5SNeilBrown continue; 237a6d511e5SNeilBrown if (tt > tmax) 238a6d511e5SNeilBrown break; 239a6d511e5SNeilBrown /* This works! */ 240a6d511e5SNeilBrown d = abs(msec - tt); 241a6d511e5SNeilBrown if (d >= diff) 242a6d511e5SNeilBrown continue; 243a6d511e5SNeilBrown /* Best yet */ 244a6d511e5SNeilBrown *c1p = c1; 245a6d511e5SNeilBrown *c2p = c2; 246a6d511e5SNeilBrown diff = d; 247a6d511e5SNeilBrown if (d == 0) 248a6d511e5SNeilBrown return msec; 249a6d511e5SNeilBrown } 250a6d511e5SNeilBrown } 251a6d511e5SNeilBrown if (diff < 65536) { 252a6d511e5SNeilBrown int actual; 253a6d511e5SNeilBrown if (msec & 1) { 254a6d511e5SNeilBrown c1 = *c2p; 255a6d511e5SNeilBrown *c2p = *c1p; 256a6d511e5SNeilBrown *c1p = c1; 257a6d511e5SNeilBrown } 258a6d511e5SNeilBrown actual = time_codes[*c1p] + time_codes[*c2p]; 259a6d511e5SNeilBrown if (*c1p < *c2p) 260a6d511e5SNeilBrown return actual + 1; 261a6d511e5SNeilBrown else 262a6d511e5SNeilBrown return actual; 263a6d511e5SNeilBrown } 264a6d511e5SNeilBrown /* No close match */ 265a6d511e5SNeilBrown return -EINVAL; 266a6d511e5SNeilBrown } 267a6d511e5SNeilBrown 268a6d511e5SNeilBrown /* 2691f431afdSNeilBrown * Update the register file with the appropriate 3-bit state for the 2701f431afdSNeilBrown * given led. 271a6d511e5SNeilBrown */ 272a6d511e5SNeilBrown static void set_select(struct tca6507_chip *tca, int led, int val) 273a6d511e5SNeilBrown { 274a6d511e5SNeilBrown int mask = (1 << led); 275a6d511e5SNeilBrown int bit; 276a6d511e5SNeilBrown 277a6d511e5SNeilBrown for (bit = 0; bit < 3; bit++) { 278a6d511e5SNeilBrown int n = tca->reg_file[bit] & ~mask; 279a6d511e5SNeilBrown if (val & (1 << bit)) 280a6d511e5SNeilBrown n |= mask; 281a6d511e5SNeilBrown if (tca->reg_file[bit] != n) { 282a6d511e5SNeilBrown tca->reg_file[bit] = n; 283a6d511e5SNeilBrown tca->reg_set |= (1 << bit); 284a6d511e5SNeilBrown } 285a6d511e5SNeilBrown } 286a6d511e5SNeilBrown } 287a6d511e5SNeilBrown 2881f431afdSNeilBrown /* Update the register file with the appropriate 4-bit code for one 2891f431afdSNeilBrown * bank or other. This can be used for timers, for levels, or for 2901f431afdSNeilBrown * initialization. 291a6d511e5SNeilBrown */ 292a6d511e5SNeilBrown static void set_code(struct tca6507_chip *tca, int reg, int bank, int new) 293a6d511e5SNeilBrown { 294a6d511e5SNeilBrown int mask = 0xF; 295a6d511e5SNeilBrown int n; 296a6d511e5SNeilBrown if (bank) { 297a6d511e5SNeilBrown mask <<= 4; 298a6d511e5SNeilBrown new <<= 4; 299a6d511e5SNeilBrown } 300a6d511e5SNeilBrown n = tca->reg_file[reg] & ~mask; 301a6d511e5SNeilBrown n |= new; 302a6d511e5SNeilBrown if (tca->reg_file[reg] != n) { 303a6d511e5SNeilBrown tca->reg_file[reg] = n; 304a6d511e5SNeilBrown tca->reg_set |= 1 << reg; 305a6d511e5SNeilBrown } 306a6d511e5SNeilBrown } 307a6d511e5SNeilBrown 308a6d511e5SNeilBrown /* Update brightness level. */ 309a6d511e5SNeilBrown static void set_level(struct tca6507_chip *tca, int bank, int level) 310a6d511e5SNeilBrown { 311a6d511e5SNeilBrown switch (bank) { 312a6d511e5SNeilBrown case BANK0: 313a6d511e5SNeilBrown case BANK1: 314a6d511e5SNeilBrown set_code(tca, TCA6507_MAX_INTENSITY, bank, level); 315a6d511e5SNeilBrown break; 316a6d511e5SNeilBrown case MASTER: 317a6d511e5SNeilBrown set_code(tca, TCA6507_MASTER_INTENSITY, 0, level); 318a6d511e5SNeilBrown break; 319a6d511e5SNeilBrown } 320a6d511e5SNeilBrown tca->bank[bank].level = level; 321a6d511e5SNeilBrown } 322a6d511e5SNeilBrown 3231f431afdSNeilBrown /* Record all relevant time codes for a given bank */ 324a6d511e5SNeilBrown static void set_times(struct tca6507_chip *tca, int bank) 325a6d511e5SNeilBrown { 326a6d511e5SNeilBrown int c1, c2; 327a6d511e5SNeilBrown int result; 328a6d511e5SNeilBrown 329a6d511e5SNeilBrown result = choose_times(tca->bank[bank].ontime, &c1, &c2); 330a6d511e5SNeilBrown dev_dbg(&tca->client->dev, 3311f431afdSNeilBrown "Chose on times %d(%d) %d(%d) for %dms\n", 3321f431afdSNeilBrown c1, time_codes[c1], 333a6d511e5SNeilBrown c2, time_codes[c2], tca->bank[bank].ontime); 334a6d511e5SNeilBrown set_code(tca, TCA6507_FADE_ON, bank, c2); 335a6d511e5SNeilBrown set_code(tca, TCA6507_FULL_ON, bank, c1); 336a6d511e5SNeilBrown tca->bank[bank].ontime = result; 337a6d511e5SNeilBrown 338a6d511e5SNeilBrown result = choose_times(tca->bank[bank].offtime, &c1, &c2); 339a6d511e5SNeilBrown dev_dbg(&tca->client->dev, 3401f431afdSNeilBrown "Chose off times %d(%d) %d(%d) for %dms\n", 3411f431afdSNeilBrown c1, time_codes[c1], 342a6d511e5SNeilBrown c2, time_codes[c2], tca->bank[bank].offtime); 343a6d511e5SNeilBrown set_code(tca, TCA6507_FADE_OFF, bank, c2); 344a6d511e5SNeilBrown set_code(tca, TCA6507_FIRST_OFF, bank, c1); 345a6d511e5SNeilBrown set_code(tca, TCA6507_SECOND_OFF, bank, c1); 346a6d511e5SNeilBrown tca->bank[bank].offtime = result; 347a6d511e5SNeilBrown 348a6d511e5SNeilBrown set_code(tca, TCA6507_INITIALIZE, bank, INIT_CODE); 349a6d511e5SNeilBrown } 350a6d511e5SNeilBrown 351a6d511e5SNeilBrown /* Write all needed register of tca6507 */ 352a6d511e5SNeilBrown 353a6d511e5SNeilBrown static void tca6507_work(struct work_struct *work) 354a6d511e5SNeilBrown { 355a6d511e5SNeilBrown struct tca6507_chip *tca = container_of(work, struct tca6507_chip, 356a6d511e5SNeilBrown work); 357a6d511e5SNeilBrown struct i2c_client *cl = tca->client; 358a6d511e5SNeilBrown int set; 359a6d511e5SNeilBrown u8 file[TCA6507_REG_CNT]; 360a6d511e5SNeilBrown int r; 361a6d511e5SNeilBrown 362a6d511e5SNeilBrown spin_lock_irq(&tca->lock); 363a6d511e5SNeilBrown set = tca->reg_set; 364a6d511e5SNeilBrown memcpy(file, tca->reg_file, TCA6507_REG_CNT); 365a6d511e5SNeilBrown tca->reg_set = 0; 366a6d511e5SNeilBrown spin_unlock_irq(&tca->lock); 367a6d511e5SNeilBrown 368a6d511e5SNeilBrown for (r = 0; r < TCA6507_REG_CNT; r++) 369a6d511e5SNeilBrown if (set & (1<<r)) 370a6d511e5SNeilBrown i2c_smbus_write_byte_data(cl, r, file[r]); 371a6d511e5SNeilBrown } 372a6d511e5SNeilBrown 373a6d511e5SNeilBrown static void led_release(struct tca6507_led *led) 374a6d511e5SNeilBrown { 375a6d511e5SNeilBrown /* If led owns any resource, release it. */ 376a6d511e5SNeilBrown struct tca6507_chip *tca = led->chip; 377a6d511e5SNeilBrown if (led->bank >= 0) { 378a6d511e5SNeilBrown struct bank *b = tca->bank + led->bank; 379a6d511e5SNeilBrown if (led->blink) 380a6d511e5SNeilBrown b->time_use--; 381a6d511e5SNeilBrown b->level_use--; 382a6d511e5SNeilBrown } 383a6d511e5SNeilBrown led->blink = 0; 384a6d511e5SNeilBrown led->bank = -1; 385a6d511e5SNeilBrown } 386a6d511e5SNeilBrown 387a6d511e5SNeilBrown static int led_prepare(struct tca6507_led *led) 388a6d511e5SNeilBrown { 3891f431afdSNeilBrown /* Assign this led to a bank, configuring that bank if 3901f431afdSNeilBrown * necessary. */ 391a6d511e5SNeilBrown int level = TO_LEVEL(led->led_cdev.brightness); 392a6d511e5SNeilBrown struct tca6507_chip *tca = led->chip; 393a6d511e5SNeilBrown int c1, c2; 394a6d511e5SNeilBrown int i; 395a6d511e5SNeilBrown struct bank *b; 396a6d511e5SNeilBrown int need_init = 0; 397a6d511e5SNeilBrown 398a6d511e5SNeilBrown led->led_cdev.brightness = TO_BRIGHT(level); 399a6d511e5SNeilBrown if (level == 0) { 400a6d511e5SNeilBrown set_select(tca, led->num, TCA6507_LS_LED_OFF); 401a6d511e5SNeilBrown return 0; 402a6d511e5SNeilBrown } 403a6d511e5SNeilBrown 404a6d511e5SNeilBrown if (led->ontime == 0 || led->offtime == 0) { 405a6d511e5SNeilBrown /* 4061f431afdSNeilBrown * Just set the brightness, choosing first usable 4071f431afdSNeilBrown * bank. If none perfect, choose best. Count 4081f431afdSNeilBrown * backwards so we check MASTER bank first to avoid 4091f431afdSNeilBrown * wasting a timer. 410a6d511e5SNeilBrown */ 411a6d511e5SNeilBrown int best = -1;/* full-on */ 412a6d511e5SNeilBrown int diff = 15-level; 413a6d511e5SNeilBrown 414a6d511e5SNeilBrown if (level == 15) { 415a6d511e5SNeilBrown set_select(tca, led->num, TCA6507_LS_LED_ON); 416a6d511e5SNeilBrown return 0; 417a6d511e5SNeilBrown } 418a6d511e5SNeilBrown 419a6d511e5SNeilBrown for (i = MASTER; i >= BANK0; i--) { 420a6d511e5SNeilBrown int d; 421a6d511e5SNeilBrown if (tca->bank[i].level == level || 422a6d511e5SNeilBrown tca->bank[i].level_use == 0) { 423a6d511e5SNeilBrown best = i; 424a6d511e5SNeilBrown break; 425a6d511e5SNeilBrown } 426a6d511e5SNeilBrown d = abs(level - tca->bank[i].level); 427a6d511e5SNeilBrown if (d < diff) { 428a6d511e5SNeilBrown diff = d; 429a6d511e5SNeilBrown best = i; 430a6d511e5SNeilBrown } 431a6d511e5SNeilBrown } 432a6d511e5SNeilBrown if (best == -1) { 433a6d511e5SNeilBrown /* Best brightness is full-on */ 434a6d511e5SNeilBrown set_select(tca, led->num, TCA6507_LS_LED_ON); 435a6d511e5SNeilBrown led->led_cdev.brightness = LED_FULL; 436a6d511e5SNeilBrown return 0; 437a6d511e5SNeilBrown } 438a6d511e5SNeilBrown 439a6d511e5SNeilBrown if (!tca->bank[best].level_use) 440a6d511e5SNeilBrown set_level(tca, best, level); 441a6d511e5SNeilBrown 442a6d511e5SNeilBrown tca->bank[best].level_use++; 443a6d511e5SNeilBrown led->bank = best; 444a6d511e5SNeilBrown set_select(tca, led->num, bank_source[best]); 445a6d511e5SNeilBrown led->led_cdev.brightness = TO_BRIGHT(tca->bank[best].level); 446a6d511e5SNeilBrown return 0; 447a6d511e5SNeilBrown } 448a6d511e5SNeilBrown 449a6d511e5SNeilBrown /* 4501f431afdSNeilBrown * We have on/off time so we need to try to allocate a timing 4511f431afdSNeilBrown * bank. First check if times are compatible with hardware 4521f431afdSNeilBrown * and give up if not. 453a6d511e5SNeilBrown */ 454a6d511e5SNeilBrown if (choose_times(led->ontime, &c1, &c2) < 0) 455a6d511e5SNeilBrown return -EINVAL; 456a6d511e5SNeilBrown if (choose_times(led->offtime, &c1, &c2) < 0) 457a6d511e5SNeilBrown return -EINVAL; 458a6d511e5SNeilBrown 459a6d511e5SNeilBrown for (i = BANK0; i <= BANK1; i++) { 460a6d511e5SNeilBrown if (tca->bank[i].level_use == 0) 461a6d511e5SNeilBrown /* not in use - it is ours! */ 462a6d511e5SNeilBrown break; 463a6d511e5SNeilBrown if (tca->bank[i].level != level) 464a6d511e5SNeilBrown /* Incompatible level - skip */ 465a6d511e5SNeilBrown /* FIX: if timer matches we maybe should consider 466a6d511e5SNeilBrown * this anyway... 467a6d511e5SNeilBrown */ 468a6d511e5SNeilBrown continue; 469a6d511e5SNeilBrown 470a6d511e5SNeilBrown if (tca->bank[i].time_use == 0) 471a6d511e5SNeilBrown /* Timer not in use, and level matches - use it */ 472a6d511e5SNeilBrown break; 473a6d511e5SNeilBrown 474a6d511e5SNeilBrown if (!(tca->bank[i].on_dflt || 475a6d511e5SNeilBrown led->on_dflt || 476a6d511e5SNeilBrown tca->bank[i].ontime == led->ontime)) 477a6d511e5SNeilBrown /* on time is incompatible */ 478a6d511e5SNeilBrown continue; 479a6d511e5SNeilBrown 480a6d511e5SNeilBrown if (!(tca->bank[i].off_dflt || 481a6d511e5SNeilBrown led->off_dflt || 482a6d511e5SNeilBrown tca->bank[i].offtime == led->offtime)) 483a6d511e5SNeilBrown /* off time is incompatible */ 484a6d511e5SNeilBrown continue; 485a6d511e5SNeilBrown 486a6d511e5SNeilBrown /* looks like a suitable match */ 487a6d511e5SNeilBrown break; 488a6d511e5SNeilBrown } 489a6d511e5SNeilBrown 490a6d511e5SNeilBrown if (i > BANK1) 491a6d511e5SNeilBrown /* Nothing matches - how sad */ 492a6d511e5SNeilBrown return -EINVAL; 493a6d511e5SNeilBrown 494a6d511e5SNeilBrown b = &tca->bank[i]; 495a6d511e5SNeilBrown if (b->level_use == 0) 496a6d511e5SNeilBrown set_level(tca, i, level); 497a6d511e5SNeilBrown b->level_use++; 498a6d511e5SNeilBrown led->bank = i; 499a6d511e5SNeilBrown 500a6d511e5SNeilBrown if (b->on_dflt || 501a6d511e5SNeilBrown !led->on_dflt || 502a6d511e5SNeilBrown b->time_use == 0) { 503a6d511e5SNeilBrown b->ontime = led->ontime; 504a6d511e5SNeilBrown b->on_dflt = led->on_dflt; 505a6d511e5SNeilBrown need_init = 1; 506a6d511e5SNeilBrown } 507a6d511e5SNeilBrown 508a6d511e5SNeilBrown if (b->off_dflt || 509a6d511e5SNeilBrown !led->off_dflt || 510a6d511e5SNeilBrown b->time_use == 0) { 511a6d511e5SNeilBrown b->offtime = led->offtime; 512a6d511e5SNeilBrown b->off_dflt = led->off_dflt; 513a6d511e5SNeilBrown need_init = 1; 514a6d511e5SNeilBrown } 515a6d511e5SNeilBrown 516a6d511e5SNeilBrown if (need_init) 517a6d511e5SNeilBrown set_times(tca, i); 518a6d511e5SNeilBrown 519a6d511e5SNeilBrown led->ontime = b->ontime; 520a6d511e5SNeilBrown led->offtime = b->offtime; 521a6d511e5SNeilBrown 522a6d511e5SNeilBrown b->time_use++; 523a6d511e5SNeilBrown led->blink = 1; 524a6d511e5SNeilBrown led->led_cdev.brightness = TO_BRIGHT(b->level); 525a6d511e5SNeilBrown set_select(tca, led->num, blink_source[i]); 526a6d511e5SNeilBrown return 0; 527a6d511e5SNeilBrown } 528a6d511e5SNeilBrown 529a6d511e5SNeilBrown static int led_assign(struct tca6507_led *led) 530a6d511e5SNeilBrown { 531a6d511e5SNeilBrown struct tca6507_chip *tca = led->chip; 532a6d511e5SNeilBrown int err; 533a6d511e5SNeilBrown unsigned long flags; 534a6d511e5SNeilBrown 535a6d511e5SNeilBrown spin_lock_irqsave(&tca->lock, flags); 536a6d511e5SNeilBrown led_release(led); 537a6d511e5SNeilBrown err = led_prepare(led); 538a6d511e5SNeilBrown if (err) { 539a6d511e5SNeilBrown /* 5401f431afdSNeilBrown * Can only fail on timer setup. In that case we need 5411f431afdSNeilBrown * to re-establish as steady level. 542a6d511e5SNeilBrown */ 543a6d511e5SNeilBrown led->ontime = 0; 544a6d511e5SNeilBrown led->offtime = 0; 545a6d511e5SNeilBrown led_prepare(led); 546a6d511e5SNeilBrown } 547a6d511e5SNeilBrown spin_unlock_irqrestore(&tca->lock, flags); 548a6d511e5SNeilBrown 549a6d511e5SNeilBrown if (tca->reg_set) 550a6d511e5SNeilBrown schedule_work(&tca->work); 551a6d511e5SNeilBrown return err; 552a6d511e5SNeilBrown } 553a6d511e5SNeilBrown 554a6d511e5SNeilBrown static void tca6507_brightness_set(struct led_classdev *led_cdev, 555a6d511e5SNeilBrown enum led_brightness brightness) 556a6d511e5SNeilBrown { 557a6d511e5SNeilBrown struct tca6507_led *led = container_of(led_cdev, struct tca6507_led, 558a6d511e5SNeilBrown led_cdev); 559a6d511e5SNeilBrown led->led_cdev.brightness = brightness; 560a6d511e5SNeilBrown led->ontime = 0; 561a6d511e5SNeilBrown led->offtime = 0; 562a6d511e5SNeilBrown led_assign(led); 563a6d511e5SNeilBrown } 564a6d511e5SNeilBrown 565a6d511e5SNeilBrown static int tca6507_blink_set(struct led_classdev *led_cdev, 566a6d511e5SNeilBrown unsigned long *delay_on, 567a6d511e5SNeilBrown unsigned long *delay_off) 568a6d511e5SNeilBrown { 569a6d511e5SNeilBrown struct tca6507_led *led = container_of(led_cdev, struct tca6507_led, 570a6d511e5SNeilBrown led_cdev); 571a6d511e5SNeilBrown 572a6d511e5SNeilBrown if (*delay_on == 0) 573a6d511e5SNeilBrown led->on_dflt = 1; 574a6d511e5SNeilBrown else if (delay_on != &led_cdev->blink_delay_on) 575a6d511e5SNeilBrown led->on_dflt = 0; 576a6d511e5SNeilBrown led->ontime = *delay_on; 577a6d511e5SNeilBrown 578a6d511e5SNeilBrown if (*delay_off == 0) 579a6d511e5SNeilBrown led->off_dflt = 1; 580a6d511e5SNeilBrown else if (delay_off != &led_cdev->blink_delay_off) 581a6d511e5SNeilBrown led->off_dflt = 0; 582a6d511e5SNeilBrown led->offtime = *delay_off; 583a6d511e5SNeilBrown 584a6d511e5SNeilBrown if (led->ontime == 0) 585a6d511e5SNeilBrown led->ontime = 512; 586a6d511e5SNeilBrown if (led->offtime == 0) 587a6d511e5SNeilBrown led->offtime = 512; 588a6d511e5SNeilBrown 589a6d511e5SNeilBrown if (led->led_cdev.brightness == LED_OFF) 590a6d511e5SNeilBrown led->led_cdev.brightness = LED_FULL; 591a6d511e5SNeilBrown if (led_assign(led) < 0) { 592a6d511e5SNeilBrown led->ontime = 0; 593a6d511e5SNeilBrown led->offtime = 0; 594a6d511e5SNeilBrown led->led_cdev.brightness = LED_OFF; 595a6d511e5SNeilBrown return -EINVAL; 596a6d511e5SNeilBrown } 597a6d511e5SNeilBrown *delay_on = led->ontime; 598a6d511e5SNeilBrown *delay_off = led->offtime; 599a6d511e5SNeilBrown return 0; 600a6d511e5SNeilBrown } 601a6d511e5SNeilBrown 602a6d511e5SNeilBrown #ifdef CONFIG_GPIOLIB 603a6d511e5SNeilBrown static void tca6507_gpio_set_value(struct gpio_chip *gc, 604a6d511e5SNeilBrown unsigned offset, int val) 605a6d511e5SNeilBrown { 606a6d511e5SNeilBrown struct tca6507_chip *tca = container_of(gc, struct tca6507_chip, gpio); 607a6d511e5SNeilBrown unsigned long flags; 608a6d511e5SNeilBrown 609a6d511e5SNeilBrown spin_lock_irqsave(&tca->lock, flags); 610a6d511e5SNeilBrown /* 6111f431afdSNeilBrown * 'OFF' is floating high, and 'ON' is pulled down, so it has 6121f431afdSNeilBrown * the inverse sense of 'val'. 613a6d511e5SNeilBrown */ 614a6d511e5SNeilBrown set_select(tca, tca->gpio_map[offset], 615a6d511e5SNeilBrown val ? TCA6507_LS_LED_OFF : TCA6507_LS_LED_ON); 616a6d511e5SNeilBrown spin_unlock_irqrestore(&tca->lock, flags); 617a6d511e5SNeilBrown if (tca->reg_set) 618a6d511e5SNeilBrown schedule_work(&tca->work); 619a6d511e5SNeilBrown } 620a6d511e5SNeilBrown 621a6d511e5SNeilBrown static int tca6507_gpio_direction_output(struct gpio_chip *gc, 622a6d511e5SNeilBrown unsigned offset, int val) 623a6d511e5SNeilBrown { 624a6d511e5SNeilBrown tca6507_gpio_set_value(gc, offset, val); 625a6d511e5SNeilBrown return 0; 626a6d511e5SNeilBrown } 627a6d511e5SNeilBrown 628a6d511e5SNeilBrown static int tca6507_probe_gpios(struct i2c_client *client, 629a6d511e5SNeilBrown struct tca6507_chip *tca, 630a6d511e5SNeilBrown struct tca6507_platform_data *pdata) 631a6d511e5SNeilBrown { 632a6d511e5SNeilBrown int err; 633a6d511e5SNeilBrown int i = 0; 634a6d511e5SNeilBrown int gpios = 0; 635a6d511e5SNeilBrown 636a6d511e5SNeilBrown for (i = 0; i < NUM_LEDS; i++) 637a6d511e5SNeilBrown if (pdata->leds.leds[i].name && pdata->leds.leds[i].flags) { 638a6d511e5SNeilBrown /* Configure as a gpio */ 639a6d511e5SNeilBrown tca->gpio_name[gpios] = pdata->leds.leds[i].name; 640a6d511e5SNeilBrown tca->gpio_map[gpios] = i; 641a6d511e5SNeilBrown gpios++; 642a6d511e5SNeilBrown } 643a6d511e5SNeilBrown 644a6d511e5SNeilBrown if (!gpios) 645a6d511e5SNeilBrown return 0; 646a6d511e5SNeilBrown 647a6d511e5SNeilBrown tca->gpio.label = "gpio-tca6507"; 648a6d511e5SNeilBrown tca->gpio.names = tca->gpio_name; 649a6d511e5SNeilBrown tca->gpio.ngpio = gpios; 650a6d511e5SNeilBrown tca->gpio.base = pdata->gpio_base; 651a6d511e5SNeilBrown tca->gpio.owner = THIS_MODULE; 652a6d511e5SNeilBrown tca->gpio.direction_output = tca6507_gpio_direction_output; 653a6d511e5SNeilBrown tca->gpio.set = tca6507_gpio_set_value; 654a6d511e5SNeilBrown tca->gpio.dev = &client->dev; 65510ead6e5SNeilBrown #ifdef CONFIG_OF_GPIO 65610ead6e5SNeilBrown tca->gpio.of_node = of_node_get(client->dev.of_node); 65710ead6e5SNeilBrown #endif 658a6d511e5SNeilBrown err = gpiochip_add(&tca->gpio); 659a6d511e5SNeilBrown if (err) { 660a6d511e5SNeilBrown tca->gpio.ngpio = 0; 661a6d511e5SNeilBrown return err; 662a6d511e5SNeilBrown } 663a6d511e5SNeilBrown if (pdata->setup) 664a6d511e5SNeilBrown pdata->setup(tca->gpio.base, tca->gpio.ngpio); 665a6d511e5SNeilBrown return 0; 666a6d511e5SNeilBrown } 667a6d511e5SNeilBrown 668a6d511e5SNeilBrown static void tca6507_remove_gpio(struct tca6507_chip *tca) 669a6d511e5SNeilBrown { 670a6d511e5SNeilBrown if (tca->gpio.ngpio) { 671a6d511e5SNeilBrown int err = gpiochip_remove(&tca->gpio); 672a6d511e5SNeilBrown dev_err(&tca->client->dev, "%s failed, %d\n", 673a6d511e5SNeilBrown "gpiochip_remove()", err); 674a6d511e5SNeilBrown } 675a6d511e5SNeilBrown } 676a6d511e5SNeilBrown #else /* CONFIG_GPIOLIB */ 677a6d511e5SNeilBrown static int tca6507_probe_gpios(struct i2c_client *client, 678a6d511e5SNeilBrown struct tca6507_chip *tca, 679a6d511e5SNeilBrown struct tca6507_platform_data *pdata) 680a6d511e5SNeilBrown { 681a6d511e5SNeilBrown return 0; 682a6d511e5SNeilBrown } 683a6d511e5SNeilBrown static void tca6507_remove_gpio(struct tca6507_chip *tca) 684a6d511e5SNeilBrown { 685a6d511e5SNeilBrown } 686a6d511e5SNeilBrown #endif /* CONFIG_GPIOLIB */ 687a6d511e5SNeilBrown 6880c596a7eSMarek Belisko #ifdef CONFIG_OF 6890c596a7eSMarek Belisko static struct tca6507_platform_data * 6900c596a7eSMarek Belisko tca6507_led_dt_init(struct i2c_client *client) 6910c596a7eSMarek Belisko { 6920c596a7eSMarek Belisko struct device_node *np = client->dev.of_node, *child; 6930c596a7eSMarek Belisko struct tca6507_platform_data *pdata; 6940c596a7eSMarek Belisko struct led_info *tca_leds; 695ef754e88SAxel Lin int count; 6960c596a7eSMarek Belisko 697ef754e88SAxel Lin count = of_get_child_count(np); 698ef754e88SAxel Lin if (!count || count > NUM_LEDS) 6990c596a7eSMarek Belisko return ERR_PTR(-ENODEV); 7000c596a7eSMarek Belisko 7010c596a7eSMarek Belisko tca_leds = devm_kzalloc(&client->dev, 7029334129eSNeilBrown sizeof(struct led_info) * NUM_LEDS, GFP_KERNEL); 7030c596a7eSMarek Belisko if (!tca_leds) 7040c596a7eSMarek Belisko return ERR_PTR(-ENOMEM); 7050c596a7eSMarek Belisko 7060c596a7eSMarek Belisko for_each_child_of_node(np, child) { 7070c596a7eSMarek Belisko struct led_info led; 7080c596a7eSMarek Belisko u32 reg; 7090c596a7eSMarek Belisko int ret; 7100c596a7eSMarek Belisko 7110c596a7eSMarek Belisko led.name = 7120c596a7eSMarek Belisko of_get_property(child, "label", NULL) ? : child->name; 7130c596a7eSMarek Belisko led.default_trigger = 7140c596a7eSMarek Belisko of_get_property(child, "linux,default-trigger", NULL); 7159334129eSNeilBrown led.flags = 0; 71610ead6e5SNeilBrown if (of_property_match_string(child, "compatible", "gpio") >= 0) 71710ead6e5SNeilBrown led.flags |= TCA6507_MAKE_GPIO; 7180c596a7eSMarek Belisko ret = of_property_read_u32(child, "reg", ®); 7199334129eSNeilBrown if (ret != 0 || reg < 0 || reg >= NUM_LEDS) 7200c596a7eSMarek Belisko continue; 7210c596a7eSMarek Belisko 7220c596a7eSMarek Belisko tca_leds[reg] = led; 7230c596a7eSMarek Belisko } 7240c596a7eSMarek Belisko pdata = devm_kzalloc(&client->dev, 7250c596a7eSMarek Belisko sizeof(struct tca6507_platform_data), GFP_KERNEL); 7260c596a7eSMarek Belisko if (!pdata) 7270c596a7eSMarek Belisko return ERR_PTR(-ENOMEM); 7280c596a7eSMarek Belisko 7290c596a7eSMarek Belisko pdata->leds.leds = tca_leds; 7309334129eSNeilBrown pdata->leds.num_leds = NUM_LEDS; 731*33ca1532SChen Gang #ifdef CONFIG_GPIOLIB 73210ead6e5SNeilBrown pdata->gpio_base = -1; 733*33ca1532SChen Gang #endif 7340c596a7eSMarek Belisko return pdata; 7350c596a7eSMarek Belisko } 7360c596a7eSMarek Belisko 7370c596a7eSMarek Belisko static const struct of_device_id of_tca6507_leds_match[] = { 7380c596a7eSMarek Belisko { .compatible = "ti,tca6507", }, 7390c596a7eSMarek Belisko {}, 7400c596a7eSMarek Belisko }; 7410c596a7eSMarek Belisko 7420c596a7eSMarek Belisko #else 7430c596a7eSMarek Belisko static struct tca6507_platform_data * 7440c596a7eSMarek Belisko tca6507_led_dt_init(struct i2c_client *client) 7450c596a7eSMarek Belisko { 7460c596a7eSMarek Belisko return ERR_PTR(-ENODEV); 7470c596a7eSMarek Belisko } 7480c596a7eSMarek Belisko 7490c596a7eSMarek Belisko #endif 7500c596a7eSMarek Belisko 75198ea1ea2SBill Pemberton static int tca6507_probe(struct i2c_client *client, 752a6d511e5SNeilBrown const struct i2c_device_id *id) 753a6d511e5SNeilBrown { 754a6d511e5SNeilBrown struct tca6507_chip *tca; 755a6d511e5SNeilBrown struct i2c_adapter *adapter; 756a6d511e5SNeilBrown struct tca6507_platform_data *pdata; 757a6d511e5SNeilBrown int err; 758a6d511e5SNeilBrown int i = 0; 759a6d511e5SNeilBrown 760a6d511e5SNeilBrown adapter = to_i2c_adapter(client->dev.parent); 76187aae1eaSJingoo Han pdata = dev_get_platdata(&client->dev); 762a6d511e5SNeilBrown 763a6d511e5SNeilBrown if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) 764a6d511e5SNeilBrown return -EIO; 765a6d511e5SNeilBrown 766a6d511e5SNeilBrown if (!pdata || pdata->leds.num_leds != NUM_LEDS) { 7670c596a7eSMarek Belisko pdata = tca6507_led_dt_init(client); 7680c596a7eSMarek Belisko if (IS_ERR(pdata)) { 769a6d511e5SNeilBrown dev_err(&client->dev, "Need %d entries in platform-data list\n", 770a6d511e5SNeilBrown NUM_LEDS); 7710c596a7eSMarek Belisko return PTR_ERR(pdata); 7720c596a7eSMarek Belisko } 773a6d511e5SNeilBrown } 7746770fc60SBryan Wu tca = devm_kzalloc(&client->dev, sizeof(*tca), GFP_KERNEL); 775a6d511e5SNeilBrown if (!tca) 776920c4f4cSDan Carpenter return -ENOMEM; 777a6d511e5SNeilBrown 778a6d511e5SNeilBrown tca->client = client; 779a6d511e5SNeilBrown INIT_WORK(&tca->work, tca6507_work); 780a6d511e5SNeilBrown spin_lock_init(&tca->lock); 781a6d511e5SNeilBrown i2c_set_clientdata(client, tca); 782a6d511e5SNeilBrown 783a6d511e5SNeilBrown for (i = 0; i < NUM_LEDS; i++) { 784a6d511e5SNeilBrown struct tca6507_led *l = tca->leds + i; 785a6d511e5SNeilBrown 786a6d511e5SNeilBrown l->chip = tca; 787a6d511e5SNeilBrown l->num = i; 788a6d511e5SNeilBrown if (pdata->leds.leds[i].name && !pdata->leds.leds[i].flags) { 789a6d511e5SNeilBrown l->led_cdev.name = pdata->leds.leds[i].name; 790a6d511e5SNeilBrown l->led_cdev.default_trigger 791a6d511e5SNeilBrown = pdata->leds.leds[i].default_trigger; 792a6d511e5SNeilBrown l->led_cdev.brightness_set = tca6507_brightness_set; 793a6d511e5SNeilBrown l->led_cdev.blink_set = tca6507_blink_set; 794a6d511e5SNeilBrown l->bank = -1; 795a6d511e5SNeilBrown err = led_classdev_register(&client->dev, 796a6d511e5SNeilBrown &l->led_cdev); 797a6d511e5SNeilBrown if (err < 0) 798a6d511e5SNeilBrown goto exit; 799a6d511e5SNeilBrown } 800a6d511e5SNeilBrown } 801a6d511e5SNeilBrown err = tca6507_probe_gpios(client, tca, pdata); 802a6d511e5SNeilBrown if (err) 803a6d511e5SNeilBrown goto exit; 804a6d511e5SNeilBrown /* set all registers to known state - zero */ 805a6d511e5SNeilBrown tca->reg_set = 0x7f; 806a6d511e5SNeilBrown schedule_work(&tca->work); 807a6d511e5SNeilBrown 808a6d511e5SNeilBrown return 0; 809a6d511e5SNeilBrown exit: 810920c4f4cSDan Carpenter while (i--) { 811a6d511e5SNeilBrown if (tca->leds[i].led_cdev.name) 812a6d511e5SNeilBrown led_classdev_unregister(&tca->leds[i].led_cdev); 813920c4f4cSDan Carpenter } 814a6d511e5SNeilBrown return err; 815a6d511e5SNeilBrown } 816a6d511e5SNeilBrown 817678e8a6bSBill Pemberton static int tca6507_remove(struct i2c_client *client) 818a6d511e5SNeilBrown { 819a6d511e5SNeilBrown int i; 820a6d511e5SNeilBrown struct tca6507_chip *tca = i2c_get_clientdata(client); 821a6d511e5SNeilBrown struct tca6507_led *tca_leds = tca->leds; 822a6d511e5SNeilBrown 823a6d511e5SNeilBrown for (i = 0; i < NUM_LEDS; i++) { 824a6d511e5SNeilBrown if (tca_leds[i].led_cdev.name) 825a6d511e5SNeilBrown led_classdev_unregister(&tca_leds[i].led_cdev); 826a6d511e5SNeilBrown } 827a6d511e5SNeilBrown tca6507_remove_gpio(tca); 828a6d511e5SNeilBrown cancel_work_sync(&tca->work); 829a6d511e5SNeilBrown 830a6d511e5SNeilBrown return 0; 831a6d511e5SNeilBrown } 832a6d511e5SNeilBrown 833a6d511e5SNeilBrown static struct i2c_driver tca6507_driver = { 834a6d511e5SNeilBrown .driver = { 835a6d511e5SNeilBrown .name = "leds-tca6507", 836a6d511e5SNeilBrown .owner = THIS_MODULE, 837fbd9df28SSachin Kamat .of_match_table = of_match_ptr(of_tca6507_leds_match), 838a6d511e5SNeilBrown }, 839a6d511e5SNeilBrown .probe = tca6507_probe, 840df07cf81SBill Pemberton .remove = tca6507_remove, 841a6d511e5SNeilBrown .id_table = tca6507_id, 842a6d511e5SNeilBrown }; 843a6d511e5SNeilBrown 8442e911767SPeter Meerwald module_i2c_driver(tca6507_driver); 845a6d511e5SNeilBrown 846a6d511e5SNeilBrown MODULE_AUTHOR("NeilBrown <neilb@suse.de>"); 847a6d511e5SNeilBrown MODULE_DESCRIPTION("TCA6507 LED/GPO driver"); 848a6d511e5SNeilBrown MODULE_LICENSE("GPL v2"); 849