xref: /openbmc/linux/drivers/leds/leds-tca6507.c (revision 1ac731c529cd4d6adbce134754b51ff7d822b145)
109c434b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2a6d511e5SNeilBrown /*
3a6d511e5SNeilBrown  * leds-tca6507
4a6d511e5SNeilBrown  *
5a6d511e5SNeilBrown  * The TCA6507 is a programmable LED controller that can drive 7
6a6d511e5SNeilBrown  * separate lines either by holding them low, or by pulsing them
7a6d511e5SNeilBrown  * with modulated width.
81f431afdSNeilBrown  * The modulation can be varied in a simple pattern to produce a
91f431afdSNeilBrown  * blink or double-blink.
10a6d511e5SNeilBrown  *
111f431afdSNeilBrown  * This driver can configure each line either as a 'GPIO' which is
121f431afdSNeilBrown  * out-only (pull-up resistor required) or as an LED with variable
131f431afdSNeilBrown  * brightness and hardware-assisted blinking.
14a6d511e5SNeilBrown  *
151f431afdSNeilBrown  * Apart from OFF and ON there are three programmable brightness
161f431afdSNeilBrown  * levels which can be programmed from 0 to 15 and indicate how many
171f431afdSNeilBrown  * 500usec intervals in each 8msec that the led is 'on'.  The levels
181f431afdSNeilBrown  * are named MASTER, BANK0 and BANK1.
19a6d511e5SNeilBrown  *
201f431afdSNeilBrown  * There are two different blink rates that can be programmed, each
211f431afdSNeilBrown  * with separate time for rise, on, fall, off and second-off.  Thus if
221f431afdSNeilBrown  * 3 or more different non-trivial rates are required, software must
231f431afdSNeilBrown  * be used for the extra rates. The two different blink rates must
241f431afdSNeilBrown  * align with the two levels BANK0 and BANK1.  This driver does not
251f431afdSNeilBrown  * support double-blink so 'second-off' always matches 'off'.
26a6d511e5SNeilBrown  *
271f431afdSNeilBrown  * Only 16 different times can be programmed in a roughly logarithmic
281f431afdSNeilBrown  * scale from 64ms to 16320ms.  To be precise the possible times are:
29a6d511e5SNeilBrown  *    0, 64, 128, 192, 256, 384, 512, 768,
30a6d511e5SNeilBrown  *    1024, 1536, 2048, 3072, 4096, 5760, 8128, 16320
31a6d511e5SNeilBrown  *
321f431afdSNeilBrown  * Times that cannot be closely matched with these must be handled in
331f431afdSNeilBrown  * software.  This driver allows 12.5% error in matching.
34a6d511e5SNeilBrown  *
351f431afdSNeilBrown  * This driver does not allow rise/fall rates to be set explicitly.
361f431afdSNeilBrown  * When trying to match a given 'on' or 'off' period, an appropriate
371f431afdSNeilBrown  * pair of 'change' and 'hold' times are chosen to get a close match.
381f431afdSNeilBrown  * If the target delay is even, the 'change' number will be the
391f431afdSNeilBrown  * smaller; if odd, the 'hold' number will be the smaller.
40a6d511e5SNeilBrown 
411f431afdSNeilBrown  * Choosing pairs of delays with 12.5% errors allows us to match
421f431afdSNeilBrown  * delays in the ranges: 56-72, 112-144, 168-216, 224-27504,
431f431afdSNeilBrown  * 28560-36720.
441f431afdSNeilBrown  * 26% of the achievable sums can be matched by multiple pairings.
451f431afdSNeilBrown  * For example 1536 == 1536+0, 1024+512, or 768+768.
461f431afdSNeilBrown  * This driver will always choose the pairing with the least
471f431afdSNeilBrown  * maximum - 768+768 in this case.  Other pairings are not available.
48a6d511e5SNeilBrown  *
491f431afdSNeilBrown  * Access to the 3 levels and 2 blinks are on a first-come,
501f431afdSNeilBrown  * first-served basis.  Access can be shared by multiple leds if they
511f431afdSNeilBrown  * have the same level and either same blink rates, or some don't
521f431afdSNeilBrown  * blink.  When a led changes, it relinquishes access and tries again,
531f431afdSNeilBrown  * so it might lose access to hardware blink.
54a6d511e5SNeilBrown  *
551f431afdSNeilBrown  * If a blink engine cannot be allocated, software blink is used.  If
561f431afdSNeilBrown  * the desired brightness cannot be allocated, the closest available
571f431afdSNeilBrown  * non-zero brightness is used.  As 'full' is always available, the
581f431afdSNeilBrown  * worst case would be to have two different blink rates at '1', with
591f431afdSNeilBrown  * Max at '2', then other leds will have to choose between '2' and
601f431afdSNeilBrown  * '16'.  Hopefully this is not likely.
61a6d511e5SNeilBrown  *
621f431afdSNeilBrown  * Each bank (BANK0 and BANK1) has two usage counts - LEDs using the
631f431afdSNeilBrown  * brightness and LEDs using the blink.  It can only be reprogrammed
641f431afdSNeilBrown  * when the appropriate counter is zero.  The MASTER level has a
651f431afdSNeilBrown  * single usage count.
661f431afdSNeilBrown  *
671f431afdSNeilBrown  * Each LED has programmable 'on' and 'off' time as milliseconds.
681f431afdSNeilBrown  * With each there is a flag saying if it was explicitly requested or
691f431afdSNeilBrown  * defaulted.  Similarly the banks know if each time was explicit or a
701f431afdSNeilBrown  * default.  Defaults are permitted to be changed freely - they are
711f431afdSNeilBrown  * not recognised when matching.
72a6d511e5SNeilBrown  */
73a6d511e5SNeilBrown 
74a6d511e5SNeilBrown #include <linux/module.h>
75a6d511e5SNeilBrown #include <linux/slab.h>
76a6d511e5SNeilBrown #include <linux/leds.h>
77a6d511e5SNeilBrown #include <linux/err.h>
78a6d511e5SNeilBrown #include <linux/i2c.h>
79ca995226SLinus Walleij #include <linux/gpio/driver.h>
8096f52410SMarek Behún #include <linux/property.h>
81a6d511e5SNeilBrown #include <linux/workqueue.h>
82a6d511e5SNeilBrown 
83a6d511e5SNeilBrown /* LED select registers determine the source that drives LED outputs */
84a6d511e5SNeilBrown #define TCA6507_LS_LED_OFF	0x0	/* Output HI-Z (off) */
85a6d511e5SNeilBrown #define TCA6507_LS_LED_OFF1	0x1	/* Output HI-Z (off) - not used */
86a6d511e5SNeilBrown #define TCA6507_LS_LED_PWM0	0x2	/* Output LOW with Bank0 rate */
87a6d511e5SNeilBrown #define TCA6507_LS_LED_PWM1	0x3	/* Output LOW with Bank1 rate */
88a6d511e5SNeilBrown #define TCA6507_LS_LED_ON	0x4	/* Output LOW (on) */
89a6d511e5SNeilBrown #define TCA6507_LS_LED_MIR	0x5	/* Output LOW with Master Intensity */
90a6d511e5SNeilBrown #define TCA6507_LS_BLINK0	0x6	/* Blink at Bank0 rate */
91a6d511e5SNeilBrown #define TCA6507_LS_BLINK1	0x7	/* Blink at Bank1 rate */
92a6d511e5SNeilBrown 
9338b393feSMarek Behún struct tca6507_platform_data {
9438b393feSMarek Behún 	struct led_platform_data leds;
9538b393feSMarek Behún #ifdef CONFIG_GPIOLIB
9638b393feSMarek Behún 	int gpio_base;
9738b393feSMarek Behún #endif
9838b393feSMarek Behún };
9938b393feSMarek Behún 
10038b393feSMarek Behún #define	TCA6507_MAKE_GPIO 1
10138b393feSMarek Behún 
102a6d511e5SNeilBrown enum {
103a6d511e5SNeilBrown 	BANK0,
104a6d511e5SNeilBrown 	BANK1,
105a6d511e5SNeilBrown 	MASTER,
106a6d511e5SNeilBrown };
107a6d511e5SNeilBrown static int bank_source[3] = {
108a6d511e5SNeilBrown 	TCA6507_LS_LED_PWM0,
109a6d511e5SNeilBrown 	TCA6507_LS_LED_PWM1,
110a6d511e5SNeilBrown 	TCA6507_LS_LED_MIR,
111a6d511e5SNeilBrown };
112a6d511e5SNeilBrown static int blink_source[2] = {
113a6d511e5SNeilBrown 	TCA6507_LS_BLINK0,
114a6d511e5SNeilBrown 	TCA6507_LS_BLINK1,
115a6d511e5SNeilBrown };
116a6d511e5SNeilBrown 
117a6d511e5SNeilBrown /* PWM registers */
118a6d511e5SNeilBrown #define	TCA6507_REG_CNT			11
119a6d511e5SNeilBrown 
120a6d511e5SNeilBrown /*
121a6d511e5SNeilBrown  * 0x00, 0x01, 0x02 encode the TCA6507_LS_* values, each output
122a6d511e5SNeilBrown  * owns one bit in each register
123a6d511e5SNeilBrown  */
124a6d511e5SNeilBrown #define	TCA6507_FADE_ON			0x03
125a6d511e5SNeilBrown #define	TCA6507_FULL_ON			0x04
126a6d511e5SNeilBrown #define	TCA6507_FADE_OFF		0x05
127a6d511e5SNeilBrown #define	TCA6507_FIRST_OFF		0x06
128a6d511e5SNeilBrown #define	TCA6507_SECOND_OFF		0x07
129a6d511e5SNeilBrown #define	TCA6507_MAX_INTENSITY		0x08
130a6d511e5SNeilBrown #define	TCA6507_MASTER_INTENSITY	0x09
131a6d511e5SNeilBrown #define	TCA6507_INITIALIZE		0x0A
132a6d511e5SNeilBrown 
133a6d511e5SNeilBrown #define	INIT_CODE			0x8
134a6d511e5SNeilBrown 
135a6d511e5SNeilBrown #define TIMECODES 16
136a6d511e5SNeilBrown static int time_codes[TIMECODES] = {
137a6d511e5SNeilBrown 	0, 64, 128, 192, 256, 384, 512, 768,
138a6d511e5SNeilBrown 	1024, 1536, 2048, 3072, 4096, 5760, 8128, 16320
139a6d511e5SNeilBrown };
140a6d511e5SNeilBrown 
141a6d511e5SNeilBrown /* Convert an led.brightness level (0..255) to a TCA6507 level (0..15) */
TO_LEVEL(int brightness)142a6d511e5SNeilBrown static inline int TO_LEVEL(int brightness)
143a6d511e5SNeilBrown {
144a6d511e5SNeilBrown 	return brightness >> 4;
145a6d511e5SNeilBrown }
146a6d511e5SNeilBrown 
147a6d511e5SNeilBrown /* ...and convert back */
TO_BRIGHT(int level)148a6d511e5SNeilBrown static inline int TO_BRIGHT(int level)
149a6d511e5SNeilBrown {
150a6d511e5SNeilBrown 	if (level)
151a6d511e5SNeilBrown 		return (level << 4) | 0xf;
152a6d511e5SNeilBrown 	return 0;
153a6d511e5SNeilBrown }
154a6d511e5SNeilBrown 
155a6d511e5SNeilBrown #define NUM_LEDS 7
156a6d511e5SNeilBrown struct tca6507_chip {
157a6d511e5SNeilBrown 	int			reg_set;	/* One bit per register where
158a6d511e5SNeilBrown 						 * a '1' means the register
159a6d511e5SNeilBrown 						 * should be written */
160a6d511e5SNeilBrown 	u8			reg_file[TCA6507_REG_CNT];
161a6d511e5SNeilBrown 	/* Bank 2 is Master Intensity and doesn't use times */
162a6d511e5SNeilBrown 	struct bank {
163a6d511e5SNeilBrown 		int level;
164a6d511e5SNeilBrown 		int ontime, offtime;
165a6d511e5SNeilBrown 		int on_dflt, off_dflt;
166a6d511e5SNeilBrown 		int time_use, level_use;
167a6d511e5SNeilBrown 	} bank[3];
168a6d511e5SNeilBrown 	struct i2c_client	*client;
169a6d511e5SNeilBrown 	struct work_struct	work;
170a6d511e5SNeilBrown 	spinlock_t		lock;
171a6d511e5SNeilBrown 
172a6d511e5SNeilBrown 	struct tca6507_led {
173a6d511e5SNeilBrown 		struct tca6507_chip	*chip;
174a6d511e5SNeilBrown 		struct led_classdev	led_cdev;
175a6d511e5SNeilBrown 		int			num;
176a6d511e5SNeilBrown 		int			ontime, offtime;
177a6d511e5SNeilBrown 		int			on_dflt, off_dflt;
178a6d511e5SNeilBrown 		int			bank;	/* Bank used, or -1 */
179a6d511e5SNeilBrown 		int			blink;	/* Set if hardware-blinking */
180a6d511e5SNeilBrown 	} leds[NUM_LEDS];
181a6d511e5SNeilBrown #ifdef CONFIG_GPIOLIB
182a6d511e5SNeilBrown 	struct gpio_chip		gpio;
183a6d511e5SNeilBrown 	int				gpio_map[NUM_LEDS];
184a6d511e5SNeilBrown #endif
185a6d511e5SNeilBrown };
186a6d511e5SNeilBrown 
187a6d511e5SNeilBrown static const struct i2c_device_id tca6507_id[] = {
188a6d511e5SNeilBrown 	{ "tca6507" },
189a6d511e5SNeilBrown 	{ }
190a6d511e5SNeilBrown };
191a6d511e5SNeilBrown MODULE_DEVICE_TABLE(i2c, tca6507_id);
192a6d511e5SNeilBrown 
choose_times(int msec,int * c1p,int * c2p)193a6d511e5SNeilBrown static int choose_times(int msec, int *c1p, int *c2p)
194a6d511e5SNeilBrown {
195a6d511e5SNeilBrown 	/*
1961f431afdSNeilBrown 	 * Choose two timecodes which add to 'msec' as near as
1971f431afdSNeilBrown 	 * possible.  The first returned is the 'on' or 'off' time.
1981f431afdSNeilBrown 	 * The second is to be used as a 'fade-on' or 'fade-off' time.
1991f431afdSNeilBrown 	 * If 'msec' is even, the first will not be smaller than the
2001f431afdSNeilBrown 	 * second.  If 'msec' is odd, the first will not be larger
2011f431afdSNeilBrown 	 * than the second.
2021f431afdSNeilBrown 	 * If we cannot get a sum within 1/8 of 'msec' fail with
2031f431afdSNeilBrown 	 * -EINVAL, otherwise return the sum that was achieved, plus 1
2041f431afdSNeilBrown 	 * if the first is smaller.
2051f431afdSNeilBrown 	 * If two possibilities are equally good (e.g. 512+0,
2061f431afdSNeilBrown 	 * 256+256), choose the first pair so there is more
2071f431afdSNeilBrown 	 * change-time visible (i.e. it is softer).
208a6d511e5SNeilBrown 	 */
209a6d511e5SNeilBrown 	int c1, c2;
210a6d511e5SNeilBrown 	int tmax = msec * 9 / 8;
211a6d511e5SNeilBrown 	int tmin = msec * 7 / 8;
212a6d511e5SNeilBrown 	int diff = 65536;
213a6d511e5SNeilBrown 
214a6d511e5SNeilBrown 	/* We start at '1' to ensure we never even think of choosing a
215a6d511e5SNeilBrown 	 * total time of '0'.
216a6d511e5SNeilBrown 	 */
217a6d511e5SNeilBrown 	for (c1 = 1; c1 < TIMECODES; c1++) {
218a6d511e5SNeilBrown 		int t = time_codes[c1];
219a6d511e5SNeilBrown 		if (t*2 < tmin)
220a6d511e5SNeilBrown 			continue;
221a6d511e5SNeilBrown 		if (t > tmax)
222a6d511e5SNeilBrown 			break;
223a6d511e5SNeilBrown 		for (c2 = 0; c2 <= c1; c2++) {
224a6d511e5SNeilBrown 			int tt = t + time_codes[c2];
225a6d511e5SNeilBrown 			int d;
226a6d511e5SNeilBrown 			if (tt < tmin)
227a6d511e5SNeilBrown 				continue;
228a6d511e5SNeilBrown 			if (tt > tmax)
229a6d511e5SNeilBrown 				break;
230a6d511e5SNeilBrown 			/* This works! */
231a6d511e5SNeilBrown 			d = abs(msec - tt);
232a6d511e5SNeilBrown 			if (d >= diff)
233a6d511e5SNeilBrown 				continue;
234a6d511e5SNeilBrown 			/* Best yet */
235a6d511e5SNeilBrown 			*c1p = c1;
236a6d511e5SNeilBrown 			*c2p = c2;
237a6d511e5SNeilBrown 			diff = d;
238a6d511e5SNeilBrown 			if (d == 0)
239a6d511e5SNeilBrown 				return msec;
240a6d511e5SNeilBrown 		}
241a6d511e5SNeilBrown 	}
242a6d511e5SNeilBrown 	if (diff < 65536) {
243a6d511e5SNeilBrown 		int actual;
244a6d511e5SNeilBrown 		if (msec & 1) {
245fa019ba4SYihao Han 			swap(*c2p, *c1p);
246a6d511e5SNeilBrown 		}
247a6d511e5SNeilBrown 		actual = time_codes[*c1p] + time_codes[*c2p];
248a6d511e5SNeilBrown 		if (*c1p < *c2p)
249a6d511e5SNeilBrown 			return actual + 1;
250a6d511e5SNeilBrown 		else
251a6d511e5SNeilBrown 			return actual;
252a6d511e5SNeilBrown 	}
253a6d511e5SNeilBrown 	/* No close match */
254a6d511e5SNeilBrown 	return -EINVAL;
255a6d511e5SNeilBrown }
256a6d511e5SNeilBrown 
257a6d511e5SNeilBrown /*
2581f431afdSNeilBrown  * Update the register file with the appropriate 3-bit state for the
2591f431afdSNeilBrown  * given led.
260a6d511e5SNeilBrown  */
set_select(struct tca6507_chip * tca,int led,int val)261a6d511e5SNeilBrown static void set_select(struct tca6507_chip *tca, int led, int val)
262a6d511e5SNeilBrown {
263a6d511e5SNeilBrown 	int mask = (1 << led);
264a6d511e5SNeilBrown 	int bit;
265a6d511e5SNeilBrown 
266a6d511e5SNeilBrown 	for (bit = 0; bit < 3; bit++) {
267a6d511e5SNeilBrown 		int n = tca->reg_file[bit] & ~mask;
268a6d511e5SNeilBrown 		if (val & (1 << bit))
269a6d511e5SNeilBrown 			n |= mask;
270a6d511e5SNeilBrown 		if (tca->reg_file[bit] != n) {
271a6d511e5SNeilBrown 			tca->reg_file[bit] = n;
272a6d511e5SNeilBrown 			tca->reg_set |= (1 << bit);
273a6d511e5SNeilBrown 		}
274a6d511e5SNeilBrown 	}
275a6d511e5SNeilBrown }
276a6d511e5SNeilBrown 
2771f431afdSNeilBrown /* Update the register file with the appropriate 4-bit code for one
2781f431afdSNeilBrown  * bank or other.  This can be used for timers, for levels, or for
2791f431afdSNeilBrown  * initialization.
280a6d511e5SNeilBrown  */
set_code(struct tca6507_chip * tca,int reg,int bank,int new)281a6d511e5SNeilBrown static void set_code(struct tca6507_chip *tca, int reg, int bank, int new)
282a6d511e5SNeilBrown {
283a6d511e5SNeilBrown 	int mask = 0xF;
284a6d511e5SNeilBrown 	int n;
285a6d511e5SNeilBrown 	if (bank) {
286a6d511e5SNeilBrown 		mask <<= 4;
287a6d511e5SNeilBrown 		new <<= 4;
288a6d511e5SNeilBrown 	}
289a6d511e5SNeilBrown 	n = tca->reg_file[reg] & ~mask;
290a6d511e5SNeilBrown 	n |= new;
291a6d511e5SNeilBrown 	if (tca->reg_file[reg] != n) {
292a6d511e5SNeilBrown 		tca->reg_file[reg] = n;
293a6d511e5SNeilBrown 		tca->reg_set |= 1 << reg;
294a6d511e5SNeilBrown 	}
295a6d511e5SNeilBrown }
296a6d511e5SNeilBrown 
297a6d511e5SNeilBrown /* Update brightness level. */
set_level(struct tca6507_chip * tca,int bank,int level)298a6d511e5SNeilBrown static void set_level(struct tca6507_chip *tca, int bank, int level)
299a6d511e5SNeilBrown {
300a6d511e5SNeilBrown 	switch (bank) {
301a6d511e5SNeilBrown 	case BANK0:
302a6d511e5SNeilBrown 	case BANK1:
303a6d511e5SNeilBrown 		set_code(tca, TCA6507_MAX_INTENSITY, bank, level);
304a6d511e5SNeilBrown 		break;
305a6d511e5SNeilBrown 	case MASTER:
306a6d511e5SNeilBrown 		set_code(tca, TCA6507_MASTER_INTENSITY, 0, level);
307a6d511e5SNeilBrown 		break;
308a6d511e5SNeilBrown 	}
309a6d511e5SNeilBrown 	tca->bank[bank].level = level;
310a6d511e5SNeilBrown }
311a6d511e5SNeilBrown 
3121f431afdSNeilBrown /* Record all relevant time codes for a given bank */
set_times(struct tca6507_chip * tca,int bank)313a6d511e5SNeilBrown static void set_times(struct tca6507_chip *tca, int bank)
314a6d511e5SNeilBrown {
315a6d511e5SNeilBrown 	int c1, c2;
316a6d511e5SNeilBrown 	int result;
317a6d511e5SNeilBrown 
318a6d511e5SNeilBrown 	result = choose_times(tca->bank[bank].ontime, &c1, &c2);
3194a91c45eSDan Carpenter 	if (result < 0)
3204a91c45eSDan Carpenter 		return;
321a6d511e5SNeilBrown 	dev_dbg(&tca->client->dev,
3221f431afdSNeilBrown 		"Chose on  times %d(%d) %d(%d) for %dms\n",
3231f431afdSNeilBrown 		c1, time_codes[c1],
324a6d511e5SNeilBrown 		c2, time_codes[c2], tca->bank[bank].ontime);
325a6d511e5SNeilBrown 	set_code(tca, TCA6507_FADE_ON, bank, c2);
326a6d511e5SNeilBrown 	set_code(tca, TCA6507_FULL_ON, bank, c1);
327a6d511e5SNeilBrown 	tca->bank[bank].ontime = result;
328a6d511e5SNeilBrown 
329a6d511e5SNeilBrown 	result = choose_times(tca->bank[bank].offtime, &c1, &c2);
330a6d511e5SNeilBrown 	dev_dbg(&tca->client->dev,
3311f431afdSNeilBrown 		"Chose off times %d(%d) %d(%d) for %dms\n",
3321f431afdSNeilBrown 		c1, time_codes[c1],
333a6d511e5SNeilBrown 		c2, time_codes[c2], tca->bank[bank].offtime);
334a6d511e5SNeilBrown 	set_code(tca, TCA6507_FADE_OFF, bank, c2);
335a6d511e5SNeilBrown 	set_code(tca, TCA6507_FIRST_OFF, bank, c1);
336a6d511e5SNeilBrown 	set_code(tca, TCA6507_SECOND_OFF, bank, c1);
337a6d511e5SNeilBrown 	tca->bank[bank].offtime = result;
338a6d511e5SNeilBrown 
339a6d511e5SNeilBrown 	set_code(tca, TCA6507_INITIALIZE, bank, INIT_CODE);
340a6d511e5SNeilBrown }
341a6d511e5SNeilBrown 
342a6d511e5SNeilBrown /* Write all needed register of tca6507 */
343a6d511e5SNeilBrown 
tca6507_work(struct work_struct * work)344a6d511e5SNeilBrown static void tca6507_work(struct work_struct *work)
345a6d511e5SNeilBrown {
346a6d511e5SNeilBrown 	struct tca6507_chip *tca = container_of(work, struct tca6507_chip,
347a6d511e5SNeilBrown 						work);
348a6d511e5SNeilBrown 	struct i2c_client *cl = tca->client;
349a6d511e5SNeilBrown 	int set;
350a6d511e5SNeilBrown 	u8 file[TCA6507_REG_CNT];
351a6d511e5SNeilBrown 	int r;
352a6d511e5SNeilBrown 
353a6d511e5SNeilBrown 	spin_lock_irq(&tca->lock);
354a6d511e5SNeilBrown 	set = tca->reg_set;
355a6d511e5SNeilBrown 	memcpy(file, tca->reg_file, TCA6507_REG_CNT);
356a6d511e5SNeilBrown 	tca->reg_set = 0;
357a6d511e5SNeilBrown 	spin_unlock_irq(&tca->lock);
358a6d511e5SNeilBrown 
359a6d511e5SNeilBrown 	for (r = 0; r < TCA6507_REG_CNT; r++)
360a6d511e5SNeilBrown 		if (set & (1<<r))
361a6d511e5SNeilBrown 			i2c_smbus_write_byte_data(cl, r, file[r]);
362a6d511e5SNeilBrown }
363a6d511e5SNeilBrown 
led_release(struct tca6507_led * led)364a6d511e5SNeilBrown static void led_release(struct tca6507_led *led)
365a6d511e5SNeilBrown {
366a6d511e5SNeilBrown 	/* If led owns any resource, release it. */
367a6d511e5SNeilBrown 	struct tca6507_chip *tca = led->chip;
368a6d511e5SNeilBrown 	if (led->bank >= 0) {
369a6d511e5SNeilBrown 		struct bank *b = tca->bank + led->bank;
370a6d511e5SNeilBrown 		if (led->blink)
371a6d511e5SNeilBrown 			b->time_use--;
372a6d511e5SNeilBrown 		b->level_use--;
373a6d511e5SNeilBrown 	}
374a6d511e5SNeilBrown 	led->blink = 0;
375a6d511e5SNeilBrown 	led->bank = -1;
376a6d511e5SNeilBrown }
377a6d511e5SNeilBrown 
led_prepare(struct tca6507_led * led)378a6d511e5SNeilBrown static int led_prepare(struct tca6507_led *led)
379a6d511e5SNeilBrown {
3801f431afdSNeilBrown 	/* Assign this led to a bank, configuring that bank if
3811f431afdSNeilBrown 	 * necessary. */
382a6d511e5SNeilBrown 	int level = TO_LEVEL(led->led_cdev.brightness);
383a6d511e5SNeilBrown 	struct tca6507_chip *tca = led->chip;
384a6d511e5SNeilBrown 	int c1, c2;
385a6d511e5SNeilBrown 	int i;
386a6d511e5SNeilBrown 	struct bank *b;
387a6d511e5SNeilBrown 	int need_init = 0;
388a6d511e5SNeilBrown 
389a6d511e5SNeilBrown 	led->led_cdev.brightness = TO_BRIGHT(level);
390a6d511e5SNeilBrown 	if (level == 0) {
391a6d511e5SNeilBrown 		set_select(tca, led->num, TCA6507_LS_LED_OFF);
392a6d511e5SNeilBrown 		return 0;
393a6d511e5SNeilBrown 	}
394a6d511e5SNeilBrown 
395a6d511e5SNeilBrown 	if (led->ontime == 0 || led->offtime == 0) {
396a6d511e5SNeilBrown 		/*
3971f431afdSNeilBrown 		 * Just set the brightness, choosing first usable
3981f431afdSNeilBrown 		 * bank.  If none perfect, choose best.  Count
3991f431afdSNeilBrown 		 * backwards so we check MASTER bank first to avoid
4001f431afdSNeilBrown 		 * wasting a timer.
401a6d511e5SNeilBrown 		 */
402a6d511e5SNeilBrown 		int best = -1;/* full-on */
403a6d511e5SNeilBrown 		int diff = 15-level;
404a6d511e5SNeilBrown 
405a6d511e5SNeilBrown 		if (level == 15) {
406a6d511e5SNeilBrown 			set_select(tca, led->num, TCA6507_LS_LED_ON);
407a6d511e5SNeilBrown 			return 0;
408a6d511e5SNeilBrown 		}
409a6d511e5SNeilBrown 
410a6d511e5SNeilBrown 		for (i = MASTER; i >= BANK0; i--) {
411a6d511e5SNeilBrown 			int d;
412a6d511e5SNeilBrown 			if (tca->bank[i].level == level ||
413a6d511e5SNeilBrown 			    tca->bank[i].level_use == 0) {
414a6d511e5SNeilBrown 				best = i;
415a6d511e5SNeilBrown 				break;
416a6d511e5SNeilBrown 			}
417a6d511e5SNeilBrown 			d = abs(level - tca->bank[i].level);
418a6d511e5SNeilBrown 			if (d < diff) {
419a6d511e5SNeilBrown 				diff = d;
420a6d511e5SNeilBrown 				best = i;
421a6d511e5SNeilBrown 			}
422a6d511e5SNeilBrown 		}
423a6d511e5SNeilBrown 		if (best == -1) {
424a6d511e5SNeilBrown 			/* Best brightness is full-on */
425a6d511e5SNeilBrown 			set_select(tca, led->num, TCA6507_LS_LED_ON);
426a6d511e5SNeilBrown 			led->led_cdev.brightness = LED_FULL;
427a6d511e5SNeilBrown 			return 0;
428a6d511e5SNeilBrown 		}
429a6d511e5SNeilBrown 
430a6d511e5SNeilBrown 		if (!tca->bank[best].level_use)
431a6d511e5SNeilBrown 			set_level(tca, best, level);
432a6d511e5SNeilBrown 
433a6d511e5SNeilBrown 		tca->bank[best].level_use++;
434a6d511e5SNeilBrown 		led->bank = best;
435a6d511e5SNeilBrown 		set_select(tca, led->num, bank_source[best]);
436a6d511e5SNeilBrown 		led->led_cdev.brightness = TO_BRIGHT(tca->bank[best].level);
437a6d511e5SNeilBrown 		return 0;
438a6d511e5SNeilBrown 	}
439a6d511e5SNeilBrown 
440a6d511e5SNeilBrown 	/*
4411f431afdSNeilBrown 	 * We have on/off time so we need to try to allocate a timing
4421f431afdSNeilBrown 	 * bank.  First check if times are compatible with hardware
4431f431afdSNeilBrown 	 * and give up if not.
444a6d511e5SNeilBrown 	 */
445a6d511e5SNeilBrown 	if (choose_times(led->ontime, &c1, &c2) < 0)
446a6d511e5SNeilBrown 		return -EINVAL;
447a6d511e5SNeilBrown 	if (choose_times(led->offtime, &c1, &c2) < 0)
448a6d511e5SNeilBrown 		return -EINVAL;
449a6d511e5SNeilBrown 
450a6d511e5SNeilBrown 	for (i = BANK0; i <= BANK1; i++) {
451a6d511e5SNeilBrown 		if (tca->bank[i].level_use == 0)
452a6d511e5SNeilBrown 			/* not in use - it is ours! */
453a6d511e5SNeilBrown 			break;
454a6d511e5SNeilBrown 		if (tca->bank[i].level != level)
455a6d511e5SNeilBrown 			/* Incompatible level - skip */
456a6d511e5SNeilBrown 			/* FIX: if timer matches we maybe should consider
457a6d511e5SNeilBrown 			 * this anyway...
458a6d511e5SNeilBrown 			 */
459a6d511e5SNeilBrown 			continue;
460a6d511e5SNeilBrown 
461a6d511e5SNeilBrown 		if (tca->bank[i].time_use == 0)
462a6d511e5SNeilBrown 			/* Timer not in use, and level matches - use it */
463a6d511e5SNeilBrown 			break;
464a6d511e5SNeilBrown 
465a6d511e5SNeilBrown 		if (!(tca->bank[i].on_dflt ||
466a6d511e5SNeilBrown 		      led->on_dflt ||
467a6d511e5SNeilBrown 		      tca->bank[i].ontime == led->ontime))
468a6d511e5SNeilBrown 			/* on time is incompatible */
469a6d511e5SNeilBrown 			continue;
470a6d511e5SNeilBrown 
471a6d511e5SNeilBrown 		if (!(tca->bank[i].off_dflt ||
472a6d511e5SNeilBrown 		      led->off_dflt ||
473a6d511e5SNeilBrown 		      tca->bank[i].offtime == led->offtime))
474a6d511e5SNeilBrown 			/* off time is incompatible */
475a6d511e5SNeilBrown 			continue;
476a6d511e5SNeilBrown 
477a6d511e5SNeilBrown 		/* looks like a suitable match */
478a6d511e5SNeilBrown 		break;
479a6d511e5SNeilBrown 	}
480a6d511e5SNeilBrown 
481a6d511e5SNeilBrown 	if (i > BANK1)
482a6d511e5SNeilBrown 		/* Nothing matches - how sad */
483a6d511e5SNeilBrown 		return -EINVAL;
484a6d511e5SNeilBrown 
485a6d511e5SNeilBrown 	b = &tca->bank[i];
486a6d511e5SNeilBrown 	if (b->level_use == 0)
487a6d511e5SNeilBrown 		set_level(tca, i, level);
488a6d511e5SNeilBrown 	b->level_use++;
489a6d511e5SNeilBrown 	led->bank = i;
490a6d511e5SNeilBrown 
491a6d511e5SNeilBrown 	if (b->on_dflt ||
492a6d511e5SNeilBrown 	    !led->on_dflt ||
493a6d511e5SNeilBrown 	    b->time_use == 0) {
494a6d511e5SNeilBrown 		b->ontime = led->ontime;
495a6d511e5SNeilBrown 		b->on_dflt = led->on_dflt;
496a6d511e5SNeilBrown 		need_init = 1;
497a6d511e5SNeilBrown 	}
498a6d511e5SNeilBrown 
499a6d511e5SNeilBrown 	if (b->off_dflt ||
500a6d511e5SNeilBrown 	    !led->off_dflt ||
501a6d511e5SNeilBrown 	    b->time_use == 0) {
502a6d511e5SNeilBrown 		b->offtime = led->offtime;
503a6d511e5SNeilBrown 		b->off_dflt = led->off_dflt;
504a6d511e5SNeilBrown 		need_init = 1;
505a6d511e5SNeilBrown 	}
506a6d511e5SNeilBrown 
507a6d511e5SNeilBrown 	if (need_init)
508a6d511e5SNeilBrown 		set_times(tca, i);
509a6d511e5SNeilBrown 
510a6d511e5SNeilBrown 	led->ontime = b->ontime;
511a6d511e5SNeilBrown 	led->offtime = b->offtime;
512a6d511e5SNeilBrown 
513a6d511e5SNeilBrown 	b->time_use++;
514a6d511e5SNeilBrown 	led->blink = 1;
515a6d511e5SNeilBrown 	led->led_cdev.brightness = TO_BRIGHT(b->level);
516a6d511e5SNeilBrown 	set_select(tca, led->num, blink_source[i]);
517a6d511e5SNeilBrown 	return 0;
518a6d511e5SNeilBrown }
519a6d511e5SNeilBrown 
led_assign(struct tca6507_led * led)520a6d511e5SNeilBrown static int led_assign(struct tca6507_led *led)
521a6d511e5SNeilBrown {
522a6d511e5SNeilBrown 	struct tca6507_chip *tca = led->chip;
523a6d511e5SNeilBrown 	int err;
524a6d511e5SNeilBrown 	unsigned long flags;
525a6d511e5SNeilBrown 
526a6d511e5SNeilBrown 	spin_lock_irqsave(&tca->lock, flags);
527a6d511e5SNeilBrown 	led_release(led);
528a6d511e5SNeilBrown 	err = led_prepare(led);
529a6d511e5SNeilBrown 	if (err) {
530a6d511e5SNeilBrown 		/*
5311f431afdSNeilBrown 		 * Can only fail on timer setup.  In that case we need
5321f431afdSNeilBrown 		 * to re-establish as steady level.
533a6d511e5SNeilBrown 		 */
534a6d511e5SNeilBrown 		led->ontime = 0;
535a6d511e5SNeilBrown 		led->offtime = 0;
536a6d511e5SNeilBrown 		led_prepare(led);
537a6d511e5SNeilBrown 	}
538a6d511e5SNeilBrown 	spin_unlock_irqrestore(&tca->lock, flags);
539a6d511e5SNeilBrown 
540a6d511e5SNeilBrown 	if (tca->reg_set)
541a6d511e5SNeilBrown 		schedule_work(&tca->work);
542a6d511e5SNeilBrown 	return err;
543a6d511e5SNeilBrown }
544a6d511e5SNeilBrown 
tca6507_brightness_set(struct led_classdev * led_cdev,enum led_brightness brightness)545a6d511e5SNeilBrown static void tca6507_brightness_set(struct led_classdev *led_cdev,
546a6d511e5SNeilBrown 				   enum led_brightness brightness)
547a6d511e5SNeilBrown {
548a6d511e5SNeilBrown 	struct tca6507_led *led = container_of(led_cdev, struct tca6507_led,
549a6d511e5SNeilBrown 					       led_cdev);
550a6d511e5SNeilBrown 	led->led_cdev.brightness = brightness;
551a6d511e5SNeilBrown 	led->ontime = 0;
552a6d511e5SNeilBrown 	led->offtime = 0;
553a6d511e5SNeilBrown 	led_assign(led);
554a6d511e5SNeilBrown }
555a6d511e5SNeilBrown 
tca6507_blink_set(struct led_classdev * led_cdev,unsigned long * delay_on,unsigned long * delay_off)556a6d511e5SNeilBrown static int tca6507_blink_set(struct led_classdev *led_cdev,
557a6d511e5SNeilBrown 			     unsigned long *delay_on,
558a6d511e5SNeilBrown 			     unsigned long *delay_off)
559a6d511e5SNeilBrown {
560a6d511e5SNeilBrown 	struct tca6507_led *led = container_of(led_cdev, struct tca6507_led,
561a6d511e5SNeilBrown 					       led_cdev);
562a6d511e5SNeilBrown 
563a6d511e5SNeilBrown 	if (*delay_on == 0)
564a6d511e5SNeilBrown 		led->on_dflt = 1;
565a6d511e5SNeilBrown 	else if (delay_on != &led_cdev->blink_delay_on)
566a6d511e5SNeilBrown 		led->on_dflt = 0;
567a6d511e5SNeilBrown 	led->ontime = *delay_on;
568a6d511e5SNeilBrown 
569a6d511e5SNeilBrown 	if (*delay_off == 0)
570a6d511e5SNeilBrown 		led->off_dflt = 1;
571a6d511e5SNeilBrown 	else if (delay_off != &led_cdev->blink_delay_off)
572a6d511e5SNeilBrown 		led->off_dflt = 0;
573a6d511e5SNeilBrown 	led->offtime = *delay_off;
574a6d511e5SNeilBrown 
575a6d511e5SNeilBrown 	if (led->ontime == 0)
576a6d511e5SNeilBrown 		led->ontime = 512;
577a6d511e5SNeilBrown 	if (led->offtime == 0)
578a6d511e5SNeilBrown 		led->offtime = 512;
579a6d511e5SNeilBrown 
580a6d511e5SNeilBrown 	if (led->led_cdev.brightness == LED_OFF)
581a6d511e5SNeilBrown 		led->led_cdev.brightness = LED_FULL;
582a6d511e5SNeilBrown 	if (led_assign(led) < 0) {
583a6d511e5SNeilBrown 		led->ontime = 0;
584a6d511e5SNeilBrown 		led->offtime = 0;
585a6d511e5SNeilBrown 		led->led_cdev.brightness = LED_OFF;
586a6d511e5SNeilBrown 		return -EINVAL;
587a6d511e5SNeilBrown 	}
588a6d511e5SNeilBrown 	*delay_on = led->ontime;
589a6d511e5SNeilBrown 	*delay_off = led->offtime;
590a6d511e5SNeilBrown 	return 0;
591a6d511e5SNeilBrown }
592a6d511e5SNeilBrown 
593a6d511e5SNeilBrown #ifdef CONFIG_GPIOLIB
tca6507_gpio_set_value(struct gpio_chip * gc,unsigned offset,int val)594a6d511e5SNeilBrown static void tca6507_gpio_set_value(struct gpio_chip *gc,
595a6d511e5SNeilBrown 				   unsigned offset, int val)
596a6d511e5SNeilBrown {
5972a664343SLinus Walleij 	struct tca6507_chip *tca = gpiochip_get_data(gc);
598a6d511e5SNeilBrown 	unsigned long flags;
599a6d511e5SNeilBrown 
600a6d511e5SNeilBrown 	spin_lock_irqsave(&tca->lock, flags);
601a6d511e5SNeilBrown 	/*
6021f431afdSNeilBrown 	 * 'OFF' is floating high, and 'ON' is pulled down, so it has
6031f431afdSNeilBrown 	 * the inverse sense of 'val'.
604a6d511e5SNeilBrown 	 */
605a6d511e5SNeilBrown 	set_select(tca, tca->gpio_map[offset],
606a6d511e5SNeilBrown 		   val ? TCA6507_LS_LED_OFF : TCA6507_LS_LED_ON);
607a6d511e5SNeilBrown 	spin_unlock_irqrestore(&tca->lock, flags);
608a6d511e5SNeilBrown 	if (tca->reg_set)
609a6d511e5SNeilBrown 		schedule_work(&tca->work);
610a6d511e5SNeilBrown }
611a6d511e5SNeilBrown 
tca6507_gpio_direction_output(struct gpio_chip * gc,unsigned offset,int val)612a6d511e5SNeilBrown static int tca6507_gpio_direction_output(struct gpio_chip *gc,
613a6d511e5SNeilBrown 					  unsigned offset, int val)
614a6d511e5SNeilBrown {
615a6d511e5SNeilBrown 	tca6507_gpio_set_value(gc, offset, val);
616a6d511e5SNeilBrown 	return 0;
617a6d511e5SNeilBrown }
618a6d511e5SNeilBrown 
tca6507_probe_gpios(struct device * dev,struct tca6507_chip * tca,struct tca6507_platform_data * pdata)619c1ff1a1dSMarek Behún static int tca6507_probe_gpios(struct device *dev,
620a6d511e5SNeilBrown 			       struct tca6507_chip *tca,
621a6d511e5SNeilBrown 			       struct tca6507_platform_data *pdata)
622a6d511e5SNeilBrown {
623a6d511e5SNeilBrown 	int err;
624a6d511e5SNeilBrown 	int i = 0;
625a6d511e5SNeilBrown 	int gpios = 0;
626a6d511e5SNeilBrown 
627a6d511e5SNeilBrown 	for (i = 0; i < NUM_LEDS; i++)
628a6d511e5SNeilBrown 		if (pdata->leds.leds[i].name && pdata->leds.leds[i].flags) {
629a6d511e5SNeilBrown 			/* Configure as a gpio */
630a6d511e5SNeilBrown 			tca->gpio_map[gpios] = i;
631a6d511e5SNeilBrown 			gpios++;
632a6d511e5SNeilBrown 		}
633a6d511e5SNeilBrown 
634a6d511e5SNeilBrown 	if (!gpios)
635a6d511e5SNeilBrown 		return 0;
636a6d511e5SNeilBrown 
637a6d511e5SNeilBrown 	tca->gpio.label = "gpio-tca6507";
638a6d511e5SNeilBrown 	tca->gpio.ngpio = gpios;
639a6d511e5SNeilBrown 	tca->gpio.base = pdata->gpio_base;
640a6d511e5SNeilBrown 	tca->gpio.owner = THIS_MODULE;
641a6d511e5SNeilBrown 	tca->gpio.direction_output = tca6507_gpio_direction_output;
642a6d511e5SNeilBrown 	tca->gpio.set = tca6507_gpio_set_value;
643c1ff1a1dSMarek Behún 	tca->gpio.parent = dev;
6442a664343SLinus Walleij 	err = gpiochip_add_data(&tca->gpio, tca);
645a6d511e5SNeilBrown 	if (err) {
646a6d511e5SNeilBrown 		tca->gpio.ngpio = 0;
647a6d511e5SNeilBrown 		return err;
648a6d511e5SNeilBrown 	}
649a6d511e5SNeilBrown 	return 0;
650a6d511e5SNeilBrown }
651a6d511e5SNeilBrown 
tca6507_remove_gpio(struct tca6507_chip * tca)652a6d511e5SNeilBrown static void tca6507_remove_gpio(struct tca6507_chip *tca)
653a6d511e5SNeilBrown {
65488d5e520Sabdoulaye berthe 	if (tca->gpio.ngpio)
65588d5e520Sabdoulaye berthe 		gpiochip_remove(&tca->gpio);
656a6d511e5SNeilBrown }
657a6d511e5SNeilBrown #else /* CONFIG_GPIOLIB */
tca6507_probe_gpios(struct device * dev,struct tca6507_chip * tca,struct tca6507_platform_data * pdata)658c1ff1a1dSMarek Behún static int tca6507_probe_gpios(struct device *dev,
659a6d511e5SNeilBrown 			       struct tca6507_chip *tca,
660a6d511e5SNeilBrown 			       struct tca6507_platform_data *pdata)
661a6d511e5SNeilBrown {
662a6d511e5SNeilBrown 	return 0;
663a6d511e5SNeilBrown }
tca6507_remove_gpio(struct tca6507_chip * tca)664a6d511e5SNeilBrown static void tca6507_remove_gpio(struct tca6507_chip *tca)
665a6d511e5SNeilBrown {
666a6d511e5SNeilBrown }
667a6d511e5SNeilBrown #endif /* CONFIG_GPIOLIB */
668a6d511e5SNeilBrown 
6690c596a7eSMarek Belisko static struct tca6507_platform_data *
tca6507_led_dt_init(struct device * dev)670c1ff1a1dSMarek Behún tca6507_led_dt_init(struct device *dev)
6710c596a7eSMarek Belisko {
6720c596a7eSMarek Belisko 	struct tca6507_platform_data *pdata;
67396f52410SMarek Behún 	struct fwnode_handle *child;
6740c596a7eSMarek Belisko 	struct led_info *tca_leds;
675ef754e88SAxel Lin 	int count;
6760c596a7eSMarek Belisko 
677c1ff1a1dSMarek Behún 	count = device_get_child_node_count(dev);
678ef754e88SAxel Lin 	if (!count || count > NUM_LEDS)
6790c596a7eSMarek Belisko 		return ERR_PTR(-ENODEV);
6800c596a7eSMarek Belisko 
681c1ff1a1dSMarek Behún 	tca_leds = devm_kcalloc(dev, NUM_LEDS, sizeof(struct led_info),
68296f52410SMarek Behún 				GFP_KERNEL);
6830c596a7eSMarek Belisko 	if (!tca_leds)
6840c596a7eSMarek Belisko 		return ERR_PTR(-ENOMEM);
6850c596a7eSMarek Belisko 
686c1ff1a1dSMarek Behún 	device_for_each_child_node(dev, child) {
6870c596a7eSMarek Belisko 		struct led_info led;
6880c596a7eSMarek Belisko 		u32 reg;
6890c596a7eSMarek Belisko 		int ret;
6900c596a7eSMarek Belisko 
69196f52410SMarek Behún 		if (fwnode_property_read_string(child, "label", &led.name))
69296f52410SMarek Behún 			led.name = fwnode_get_name(child);
69396f52410SMarek Behún 
694*c1087c29SH. Nikolaus Schaller 		if (fwnode_property_read_string(child, "linux,default-trigger",
695*c1087c29SH. Nikolaus Schaller 						&led.default_trigger))
696*c1087c29SH. Nikolaus Schaller 			led.default_trigger = NULL;
69796f52410SMarek Behún 
6989334129eSNeilBrown 		led.flags = 0;
6998f47707aSAndy Shevchenko 		if (fwnode_device_is_compatible(child, "gpio"))
70010ead6e5SNeilBrown 			led.flags |= TCA6507_MAKE_GPIO;
70196f52410SMarek Behún 
70296f52410SMarek Behún 		ret = fwnode_property_read_u32(child, "reg", &reg);
70396f52410SMarek Behún 		if (ret || reg >= NUM_LEDS) {
70496f52410SMarek Behún 			fwnode_handle_put(child);
705a2380982SMarek Behún 			return ERR_PTR(ret ? : -EINVAL);
70696f52410SMarek Behún 		}
7070c596a7eSMarek Belisko 
7080c596a7eSMarek Belisko 		tca_leds[reg] = led;
7090c596a7eSMarek Belisko 	}
71096f52410SMarek Behún 
711c1ff1a1dSMarek Behún 	pdata = devm_kzalloc(dev, sizeof(struct tca6507_platform_data),
71296f52410SMarek Behún 			     GFP_KERNEL);
7130c596a7eSMarek Belisko 	if (!pdata)
7140c596a7eSMarek Belisko 		return ERR_PTR(-ENOMEM);
7150c596a7eSMarek Belisko 
7160c596a7eSMarek Belisko 	pdata->leds.leds = tca_leds;
7179334129eSNeilBrown 	pdata->leds.num_leds = NUM_LEDS;
71833ca1532SChen Gang #ifdef CONFIG_GPIOLIB
71910ead6e5SNeilBrown 	pdata->gpio_base = -1;
72033ca1532SChen Gang #endif
72196f52410SMarek Behún 
7220c596a7eSMarek Belisko 	return pdata;
7230c596a7eSMarek Belisko }
7240c596a7eSMarek Belisko 
725e8b7dabcSPavel Machek static const struct of_device_id __maybe_unused of_tca6507_leds_match[] = {
7260c596a7eSMarek Belisko 	{ .compatible = "ti,tca6507", },
7270c596a7eSMarek Belisko 	{},
7280c596a7eSMarek Belisko };
7294d59ed85SJavier Martinez Canillas MODULE_DEVICE_TABLE(of, of_tca6507_leds_match);
7300c596a7eSMarek Belisko 
tca6507_probe(struct i2c_client * client)731e916e052SUwe Kleine-König static int tca6507_probe(struct i2c_client *client)
732a6d511e5SNeilBrown {
733c1ff1a1dSMarek Behún 	struct device *dev = &client->dev;
734a6d511e5SNeilBrown 	struct i2c_adapter *adapter;
735c1ff1a1dSMarek Behún 	struct tca6507_chip *tca;
736a6d511e5SNeilBrown 	struct tca6507_platform_data *pdata;
737a6d511e5SNeilBrown 	int err;
738a6d511e5SNeilBrown 	int i = 0;
739a6d511e5SNeilBrown 
74009bfa5f6SWolfram Sang 	adapter = client->adapter;
741a6d511e5SNeilBrown 
742a6d511e5SNeilBrown 	if (!i2c_check_functionality(adapter, I2C_FUNC_I2C))
743a6d511e5SNeilBrown 		return -EIO;
744a6d511e5SNeilBrown 
745c1ff1a1dSMarek Behún 	pdata = tca6507_led_dt_init(dev);
7460c596a7eSMarek Belisko 	if (IS_ERR(pdata)) {
747c1ff1a1dSMarek Behún 		dev_err(dev, "Need %d entries in platform-data list\n", NUM_LEDS);
7480c596a7eSMarek Belisko 		return PTR_ERR(pdata);
7490c596a7eSMarek Belisko 	}
750c1ff1a1dSMarek Behún 	tca = devm_kzalloc(dev, sizeof(*tca), GFP_KERNEL);
751a6d511e5SNeilBrown 	if (!tca)
752920c4f4cSDan Carpenter 		return -ENOMEM;
753a6d511e5SNeilBrown 
754a6d511e5SNeilBrown 	tca->client = client;
755a6d511e5SNeilBrown 	INIT_WORK(&tca->work, tca6507_work);
756a6d511e5SNeilBrown 	spin_lock_init(&tca->lock);
757a6d511e5SNeilBrown 	i2c_set_clientdata(client, tca);
758a6d511e5SNeilBrown 
759a6d511e5SNeilBrown 	for (i = 0; i < NUM_LEDS; i++) {
760a6d511e5SNeilBrown 		struct tca6507_led *l = tca->leds + i;
761a6d511e5SNeilBrown 
762a6d511e5SNeilBrown 		l->chip = tca;
763a6d511e5SNeilBrown 		l->num = i;
764a6d511e5SNeilBrown 		if (pdata->leds.leds[i].name && !pdata->leds.leds[i].flags) {
765a6d511e5SNeilBrown 			l->led_cdev.name = pdata->leds.leds[i].name;
766a6d511e5SNeilBrown 			l->led_cdev.default_trigger
767a6d511e5SNeilBrown 				= pdata->leds.leds[i].default_trigger;
768a6d511e5SNeilBrown 			l->led_cdev.brightness_set = tca6507_brightness_set;
769a6d511e5SNeilBrown 			l->led_cdev.blink_set = tca6507_blink_set;
770a6d511e5SNeilBrown 			l->bank = -1;
771c1ff1a1dSMarek Behún 			err = led_classdev_register(dev, &l->led_cdev);
772a6d511e5SNeilBrown 			if (err < 0)
773a6d511e5SNeilBrown 				goto exit;
774a6d511e5SNeilBrown 		}
775a6d511e5SNeilBrown 	}
776c1ff1a1dSMarek Behún 	err = tca6507_probe_gpios(dev, tca, pdata);
777a6d511e5SNeilBrown 	if (err)
778a6d511e5SNeilBrown 		goto exit;
779a6d511e5SNeilBrown 	/* set all registers to known state - zero */
780a6d511e5SNeilBrown 	tca->reg_set = 0x7f;
781a6d511e5SNeilBrown 	schedule_work(&tca->work);
782a6d511e5SNeilBrown 
783a6d511e5SNeilBrown 	return 0;
784a6d511e5SNeilBrown exit:
785920c4f4cSDan Carpenter 	while (i--) {
786a6d511e5SNeilBrown 		if (tca->leds[i].led_cdev.name)
787a6d511e5SNeilBrown 			led_classdev_unregister(&tca->leds[i].led_cdev);
788920c4f4cSDan Carpenter 	}
789a6d511e5SNeilBrown 	return err;
790a6d511e5SNeilBrown }
791a6d511e5SNeilBrown 
tca6507_remove(struct i2c_client * client)792ed5c2f5fSUwe Kleine-König static void tca6507_remove(struct i2c_client *client)
793a6d511e5SNeilBrown {
794a6d511e5SNeilBrown 	int i;
795a6d511e5SNeilBrown 	struct tca6507_chip *tca = i2c_get_clientdata(client);
796a6d511e5SNeilBrown 	struct tca6507_led *tca_leds = tca->leds;
797a6d511e5SNeilBrown 
798a6d511e5SNeilBrown 	for (i = 0; i < NUM_LEDS; i++) {
799a6d511e5SNeilBrown 		if (tca_leds[i].led_cdev.name)
800a6d511e5SNeilBrown 			led_classdev_unregister(&tca_leds[i].led_cdev);
801a6d511e5SNeilBrown 	}
802a6d511e5SNeilBrown 	tca6507_remove_gpio(tca);
803a6d511e5SNeilBrown 	cancel_work_sync(&tca->work);
804a6d511e5SNeilBrown }
805a6d511e5SNeilBrown 
806a6d511e5SNeilBrown static struct i2c_driver tca6507_driver = {
807a6d511e5SNeilBrown 	.driver   = {
808a6d511e5SNeilBrown 		.name    = "leds-tca6507",
809fbd9df28SSachin Kamat 		.of_match_table = of_match_ptr(of_tca6507_leds_match),
810a6d511e5SNeilBrown 	},
811e916e052SUwe Kleine-König 	.probe    = tca6507_probe,
812df07cf81SBill Pemberton 	.remove   = tca6507_remove,
813a6d511e5SNeilBrown 	.id_table = tca6507_id,
814a6d511e5SNeilBrown };
815a6d511e5SNeilBrown 
8162e911767SPeter Meerwald module_i2c_driver(tca6507_driver);
817a6d511e5SNeilBrown 
818a6d511e5SNeilBrown MODULE_AUTHOR("NeilBrown <neilb@suse.de>");
819a6d511e5SNeilBrown MODULE_DESCRIPTION("TCA6507 LED/GPO driver");
820a6d511e5SNeilBrown MODULE_LICENSE("GPL v2");
821