xref: /openbmc/linux/drivers/leds/leds-pca955x.c (revision d6da216ff5f093e377f063c4a3f577eebf0f665a)
136edc939SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2f46e9203SNate Case /*
3f46e9203SNate Case  * Copyright 2007-2008 Extreme Engineering Solutions, Inc.
4f46e9203SNate Case  *
5f46e9203SNate Case  * Author: Nate Case <ncase@xes-inc.com>
6f46e9203SNate Case  *
7f46e9203SNate Case  * LED driver for various PCA955x I2C LED drivers
8f46e9203SNate Case  *
9f46e9203SNate Case  * Supported devices:
10f46e9203SNate Case  *
11f46e9203SNate Case  *	Device		Description		7-bit slave address
12f46e9203SNate Case  *	------		-----------		-------------------
13f46e9203SNate Case  *	PCA9550		2-bit driver		0x60 .. 0x61
14f46e9203SNate Case  *	PCA9551		8-bit driver		0x60 .. 0x67
15f46e9203SNate Case  *	PCA9552		16-bit driver		0x60 .. 0x67
16f46e9203SNate Case  *	PCA9553/01	4-bit driver		0x62
17f46e9203SNate Case  *	PCA9553/02	4-bit driver		0x63
18f46e9203SNate Case  *
19f46e9203SNate Case  * Philips PCA955x LED driver chips follow a register map as shown below:
20f46e9203SNate Case  *
21f46e9203SNate Case  *	Control Register		Description
22f46e9203SNate Case  *	----------------		-----------
23f46e9203SNate Case  *	0x0				Input register 0
24f46e9203SNate Case  *					..
25f46e9203SNate Case  *	NUM_INPUT_REGS - 1		Last Input register X
26f46e9203SNate Case  *
27f46e9203SNate Case  *	NUM_INPUT_REGS			Frequency prescaler 0
28f46e9203SNate Case  *	NUM_INPUT_REGS + 1		PWM register 0
29f46e9203SNate Case  *	NUM_INPUT_REGS + 2		Frequency prescaler 1
30f46e9203SNate Case  *	NUM_INPUT_REGS + 3		PWM register 1
31f46e9203SNate Case  *
32f46e9203SNate Case  *	NUM_INPUT_REGS + 4		LED selector 0
33f46e9203SNate Case  *	NUM_INPUT_REGS + 4
34f46e9203SNate Case  *	    + NUM_LED_REGS - 1		Last LED selector
35f46e9203SNate Case  *
36f46e9203SNate Case  *  where NUM_INPUT_REGS and NUM_LED_REGS vary depending on how many
37f46e9203SNate Case  *  bits the chip supports.
38f46e9203SNate Case  */
39f46e9203SNate Case 
40ca386253SAndrew Jeffery #include <linux/bitops.h>
41f46e9203SNate Case #include <linux/ctype.h>
42ed1f4b96SCédric Le Goater #include <linux/delay.h>
43f46e9203SNate Case #include <linux/err.h>
440987c7dfSLinus Walleij #include <linux/gpio/driver.h>
45f46e9203SNate Case #include <linux/i2c.h>
46ed1f4b96SCédric Le Goater #include <linux/leds.h>
47ed1f4b96SCédric Le Goater #include <linux/module.h>
48ed1f4b96SCédric Le Goater #include <linux/of.h>
49967f69deSAndy Shevchenko #include <linux/property.h>
505a0e3ad6STejun Heo #include <linux/slab.h>
51ed1f4b96SCédric Le Goater #include <linux/string.h>
52f46e9203SNate Case 
53561099a1SCédric Le Goater #include <dt-bindings/leds/leds-pca955x.h>
54561099a1SCédric Le Goater 
55f46e9203SNate Case /* LED select registers determine the source that drives LED outputs */
56f46e9203SNate Case #define PCA955X_LS_LED_ON	0x0	/* Output LOW */
57f46e9203SNate Case #define PCA955X_LS_LED_OFF	0x1	/* Output HI-Z */
58f46e9203SNate Case #define PCA955X_LS_BLINK0	0x2	/* Blink at PWM0 rate */
59f46e9203SNate Case #define PCA955X_LS_BLINK1	0x3	/* Blink at PWM1 rate */
60f46e9203SNate Case 
6152ca7d0fSAndrew Jeffery #define PCA955X_GPIO_INPUT	LED_OFF
6252ca7d0fSAndrew Jeffery #define PCA955X_GPIO_HIGH	LED_OFF
6352ca7d0fSAndrew Jeffery #define PCA955X_GPIO_LOW	LED_FULL
6452ca7d0fSAndrew Jeffery 
65*d6da216fSEddie James #define PCA955X_BLINK_DEFAULT_MS	1000
66*d6da216fSEddie James 
67f46e9203SNate Case enum pca955x_type {
68f46e9203SNate Case 	pca9550,
69f46e9203SNate Case 	pca9551,
70f46e9203SNate Case 	pca9552,
7146de3adbSEddie James 	ibm_pca9552,
72f46e9203SNate Case 	pca9553,
73f46e9203SNate Case };
74f46e9203SNate Case 
75f46e9203SNate Case struct pca955x_chipdef {
76f46e9203SNate Case 	int			bits;
77f46e9203SNate Case 	u8			slv_addr;	/* 7-bit slave address mask */
78f46e9203SNate Case 	int			slv_addr_shift;	/* Number of bits to ignore */
79*d6da216fSEddie James 	int			blink_div;	/* PSC divider */
80f46e9203SNate Case };
81f46e9203SNate Case 
82f46e9203SNate Case static struct pca955x_chipdef pca955x_chipdefs[] = {
83f46e9203SNate Case 	[pca9550] = {
84f46e9203SNate Case 		.bits		= 2,
85f46e9203SNate Case 		.slv_addr	= /* 110000x */ 0x60,
86f46e9203SNate Case 		.slv_addr_shift	= 1,
87*d6da216fSEddie James 		.blink_div	= 44,
88f46e9203SNate Case 	},
89f46e9203SNate Case 	[pca9551] = {
90f46e9203SNate Case 		.bits		= 8,
91f46e9203SNate Case 		.slv_addr	= /* 1100xxx */ 0x60,
92f46e9203SNate Case 		.slv_addr_shift	= 3,
93*d6da216fSEddie James 		.blink_div	= 38,
94f46e9203SNate Case 	},
95f46e9203SNate Case 	[pca9552] = {
96f46e9203SNate Case 		.bits		= 16,
97f46e9203SNate Case 		.slv_addr	= /* 1100xxx */ 0x60,
98f46e9203SNate Case 		.slv_addr_shift	= 3,
99*d6da216fSEddie James 		.blink_div	= 44,
100f46e9203SNate Case 	},
10146de3adbSEddie James 	[ibm_pca9552] = {
10246de3adbSEddie James 		.bits		= 16,
10346de3adbSEddie James 		.slv_addr	= /* 0110xxx */ 0x30,
10446de3adbSEddie James 		.slv_addr_shift	= 3,
105*d6da216fSEddie James 		.blink_div	= 44,
10646de3adbSEddie James 	},
107f46e9203SNate Case 	[pca9553] = {
108f46e9203SNate Case 		.bits		= 4,
109f46e9203SNate Case 		.slv_addr	= /* 110001x */ 0x62,
110f46e9203SNate Case 		.slv_addr_shift	= 1,
111*d6da216fSEddie James 		.blink_div	= 44,
112f46e9203SNate Case 	},
113f46e9203SNate Case };
114f46e9203SNate Case 
115f46e9203SNate Case static const struct i2c_device_id pca955x_id[] = {
116f46e9203SNate Case 	{ "pca9550", pca9550 },
117f46e9203SNate Case 	{ "pca9551", pca9551 },
118f46e9203SNate Case 	{ "pca9552", pca9552 },
11946de3adbSEddie James 	{ "ibm-pca9552", ibm_pca9552 },
120f46e9203SNate Case 	{ "pca9553", pca9553 },
121f46e9203SNate Case 	{ }
122f46e9203SNate Case };
123f46e9203SNate Case MODULE_DEVICE_TABLE(i2c, pca955x_id);
124f46e9203SNate Case 
125e7e11d8bSAlexander Stein struct pca955x {
126e7e11d8bSAlexander Stein 	struct mutex lock;
127e7e11d8bSAlexander Stein 	struct pca955x_led *leds;
128f46e9203SNate Case 	struct pca955x_chipdef	*chipdef;
129f46e9203SNate Case 	struct i2c_client	*client;
130*d6da216fSEddie James 	unsigned long active_blink;
131ca386253SAndrew Jeffery 	unsigned long active_pins;
132*d6da216fSEddie James 	unsigned long blink_period;
133561099a1SCédric Le Goater #ifdef CONFIG_LEDS_PCA955X_GPIO
134561099a1SCédric Le Goater 	struct gpio_chip gpio;
135561099a1SCédric Le Goater #endif
136e7e11d8bSAlexander Stein };
137e7e11d8bSAlexander Stein 
138e7e11d8bSAlexander Stein struct pca955x_led {
139e7e11d8bSAlexander Stein 	struct pca955x	*pca955x;
140f46e9203SNate Case 	struct led_classdev	led_cdev;
141f46e9203SNate Case 	int			led_num;	/* 0 .. 15 potentially */
142561099a1SCédric Le Goater 	u32			type;
1430dd37b1cSAndy Shevchenko 	enum led_default_state	default_state;
1447c481592SEddie James 	struct fwnode_handle	*fwnode;
145ed1f4b96SCédric Le Goater };
146ed1f4b96SCédric Le Goater 
147f5058548SEddie James #define led_to_pca955x(l)	container_of(l, struct pca955x_led, led_cdev)
148f5058548SEddie James 
149ed1f4b96SCédric Le Goater struct pca955x_platform_data {
150ed1f4b96SCédric Le Goater 	struct pca955x_led	*leds;
151ed1f4b96SCédric Le Goater 	int			num_leds;
152f46e9203SNate Case };
153f46e9203SNate Case 
154f46e9203SNate Case /* 8 bits per input register */
pca955x_num_input_regs(int bits)155f5058548SEddie James static inline int pca955x_num_input_regs(int bits)
156f46e9203SNate Case {
157f46e9203SNate Case 	return (bits + 7) / 8;
158f46e9203SNate Case }
159f46e9203SNate Case 
16041f4cf59SJoel Stanley /* 4 bits per LED selector register */
pca955x_num_led_regs(int bits)16141f4cf59SJoel Stanley static inline int pca955x_num_led_regs(int bits)
16241f4cf59SJoel Stanley {
16341f4cf59SJoel Stanley 	return (bits + 3)  / 4;
16441f4cf59SJoel Stanley }
16541f4cf59SJoel Stanley 
166f46e9203SNate Case /*
167f46e9203SNate Case  * Return an LED selector register value based on an existing one, with
168f46e9203SNate Case  * the appropriate 2-bit state value set for the given LED number (0-3).
169f46e9203SNate Case  */
pca955x_ledsel(u8 oldval,int led_num,int state)170f46e9203SNate Case static inline u8 pca955x_ledsel(u8 oldval, int led_num, int state)
171f46e9203SNate Case {
172f46e9203SNate Case 	return (oldval & (~(0x3 << (led_num << 1)))) |
173f46e9203SNate Case 		((state & 0x3) << (led_num << 1));
174f46e9203SNate Case }
175f46e9203SNate Case 
pca955x_ledstate(u8 ls,int led_num)176f5058548SEddie James static inline int pca955x_ledstate(u8 ls, int led_num)
177f5058548SEddie James {
178f5058548SEddie James 	return (ls >> (led_num << 1)) & 0x3;
179f5058548SEddie James }
180f5058548SEddie James 
181f46e9203SNate Case /*
182f46e9203SNate Case  * Write to frequency prescaler register, used to program the
183*d6da216fSEddie James  * period of the PWM output.  period = (PSCx + 1) / coeff
184*d6da216fSEddie James  * Where for pca9551 chips coeff = 38 and for all other chips coeff = 44
185f46e9203SNate Case  */
pca955x_write_psc(struct pca955x * pca955x,int n,u8 val)186d451e3b4SEddie James static int pca955x_write_psc(struct pca955x *pca955x, int n, u8 val)
187f46e9203SNate Case {
188f5058548SEddie James 	u8 cmd = pca955x_num_input_regs(pca955x->chipdef->bits) + (2 * n);
1891591caf2SCédric Le Goater 	int ret;
190f46e9203SNate Case 
191d451e3b4SEddie James 	ret = i2c_smbus_write_byte_data(pca955x->client, cmd, val);
1921591caf2SCédric Le Goater 	if (ret < 0)
193d451e3b4SEddie James 		dev_err(&pca955x->client->dev, "%s: reg 0x%x, val 0x%x, err %d\n", __func__, n,
194d451e3b4SEddie James 			val, ret);
1951591caf2SCédric Le Goater 	return ret;
196f46e9203SNate Case }
197f46e9203SNate Case 
198f46e9203SNate Case /*
199f46e9203SNate Case  * Write to PWM register, which determines the duty cycle of the
200f46e9203SNate Case  * output.  LED is OFF when the count is less than the value of this
201f46e9203SNate Case  * register, and ON when it is greater.  If PWMx == 0, LED is always OFF.
202f46e9203SNate Case  *
203f46e9203SNate Case  * Duty cycle is (256 - PWMx) / 256
204f46e9203SNate Case  */
pca955x_write_pwm(struct pca955x * pca955x,int n,u8 val)205d451e3b4SEddie James static int pca955x_write_pwm(struct pca955x *pca955x, int n, u8 val)
206f46e9203SNate Case {
207f5058548SEddie James 	u8 cmd = pca955x_num_input_regs(pca955x->chipdef->bits) + 1 + (2 * n);
2081591caf2SCédric Le Goater 	int ret;
209f46e9203SNate Case 
210d451e3b4SEddie James 	ret = i2c_smbus_write_byte_data(pca955x->client, cmd, val);
2111591caf2SCédric Le Goater 	if (ret < 0)
212d451e3b4SEddie James 		dev_err(&pca955x->client->dev, "%s: reg 0x%x, val 0x%x, err %d\n", __func__, n,
213d451e3b4SEddie James 			val, ret);
2141591caf2SCédric Le Goater 	return ret;
215f46e9203SNate Case }
216f46e9203SNate Case 
217f46e9203SNate Case /*
218f46e9203SNate Case  * Write to LED selector register, which determines the source that
219f46e9203SNate Case  * drives the LED output.
220f46e9203SNate Case  */
pca955x_write_ls(struct pca955x * pca955x,int n,u8 val)221d451e3b4SEddie James static int pca955x_write_ls(struct pca955x *pca955x, int n, u8 val)
222f46e9203SNate Case {
223f5058548SEddie James 	u8 cmd = pca955x_num_input_regs(pca955x->chipdef->bits) + 4 + n;
2241591caf2SCédric Le Goater 	int ret;
225f46e9203SNate Case 
226d451e3b4SEddie James 	ret = i2c_smbus_write_byte_data(pca955x->client, cmd, val);
2271591caf2SCédric Le Goater 	if (ret < 0)
228d451e3b4SEddie James 		dev_err(&pca955x->client->dev, "%s: reg 0x%x, val 0x%x, err %d\n", __func__, n,
229d451e3b4SEddie James 			val, ret);
2301591caf2SCédric Le Goater 	return ret;
231f46e9203SNate Case }
232f46e9203SNate Case 
233f46e9203SNate Case /*
234f46e9203SNate Case  * Read the LED selector register, which determines the source that
235f46e9203SNate Case  * drives the LED output.
236f46e9203SNate Case  */
pca955x_read_ls(struct pca955x * pca955x,int n,u8 * val)237d451e3b4SEddie James static int pca955x_read_ls(struct pca955x *pca955x, int n, u8 *val)
238f46e9203SNate Case {
239f5058548SEddie James 	u8 cmd = pca955x_num_input_regs(pca955x->chipdef->bits) + 4 + n;
2401591caf2SCédric Le Goater 	int ret;
241f46e9203SNate Case 
242d451e3b4SEddie James 	ret = i2c_smbus_read_byte_data(pca955x->client, cmd);
2431591caf2SCédric Le Goater 	if (ret < 0) {
244d451e3b4SEddie James 		dev_err(&pca955x->client->dev, "%s: reg 0x%x, err %d\n", __func__, n, ret);
2451591caf2SCédric Le Goater 		return ret;
2461591caf2SCédric Le Goater 	}
2471591caf2SCédric Le Goater 	*val = (u8)ret;
2481591caf2SCédric Le Goater 	return 0;
249f46e9203SNate Case }
250f46e9203SNate Case 
pca955x_read_pwm(struct pca955x * pca955x,int n,u8 * val)251d451e3b4SEddie James static int pca955x_read_pwm(struct pca955x *pca955x, int n, u8 *val)
2527086625fSEddie James {
253f5058548SEddie James 	u8 cmd = pca955x_num_input_regs(pca955x->chipdef->bits) + 1 + (2 * n);
2547086625fSEddie James 	int ret;
2557086625fSEddie James 
256d451e3b4SEddie James 	ret = i2c_smbus_read_byte_data(pca955x->client, cmd);
2577086625fSEddie James 	if (ret < 0) {
258d451e3b4SEddie James 		dev_err(&pca955x->client->dev, "%s: reg 0x%x, err %d\n", __func__, n, ret);
2597086625fSEddie James 		return ret;
2607086625fSEddie James 	}
2617086625fSEddie James 	*val = (u8)ret;
2627086625fSEddie James 	return 0;
2637086625fSEddie James }
2647086625fSEddie James 
pca955x_read_psc(struct pca955x * pca955x,int n,u8 * val)265*d6da216fSEddie James static int pca955x_read_psc(struct pca955x *pca955x, int n, u8 *val)
266*d6da216fSEddie James {
267*d6da216fSEddie James 	u8 cmd = pca955x_num_input_regs(pca955x->chipdef->bits) + (2 * n);
268*d6da216fSEddie James 	int ret;
269*d6da216fSEddie James 
270*d6da216fSEddie James 	ret = i2c_smbus_read_byte_data(pca955x->client, cmd);
271*d6da216fSEddie James 	if (ret < 0) {
272*d6da216fSEddie James 		dev_err(&pca955x->client->dev, "%s: reg 0x%x, err %d\n", __func__, n, ret);
273*d6da216fSEddie James 		return ret;
274*d6da216fSEddie James 	}
275*d6da216fSEddie James 	*val = (u8)ret;
276*d6da216fSEddie James 	return 0;
277*d6da216fSEddie James }
278*d6da216fSEddie James 
pca955x_led_get(struct led_classdev * led_cdev)2797086625fSEddie James static enum led_brightness pca955x_led_get(struct led_classdev *led_cdev)
2807086625fSEddie James {
281f5058548SEddie James 	struct pca955x_led *pca955x_led = led_to_pca955x(led_cdev);
2827086625fSEddie James 	struct pca955x *pca955x = pca955x_led->pca955x;
2837086625fSEddie James 	u8 ls, pwm;
2847086625fSEddie James 	int ret;
2857086625fSEddie James 
286d451e3b4SEddie James 	ret = pca955x_read_ls(pca955x, pca955x_led->led_num / 4, &ls);
2877086625fSEddie James 	if (ret)
2887086625fSEddie James 		return ret;
2897086625fSEddie James 
290f5058548SEddie James 	switch (pca955x_ledstate(ls, pca955x_led->led_num % 4)) {
2917086625fSEddie James 	case PCA955X_LS_LED_ON:
292*d6da216fSEddie James 	case PCA955X_LS_BLINK0:
2937086625fSEddie James 		ret = LED_FULL;
2947086625fSEddie James 		break;
2957086625fSEddie James 	case PCA955X_LS_LED_OFF:
2967086625fSEddie James 		ret = LED_OFF;
2977086625fSEddie James 		break;
2987086625fSEddie James 	case PCA955X_LS_BLINK1:
299d451e3b4SEddie James 		ret = pca955x_read_pwm(pca955x, 1, &pwm);
3007086625fSEddie James 		if (ret)
3017086625fSEddie James 			return ret;
3027086625fSEddie James 		ret = 255 - pwm;
3037086625fSEddie James 		break;
3047086625fSEddie James 	}
3057086625fSEddie James 
3067086625fSEddie James 	return ret;
3077086625fSEddie James }
3087086625fSEddie James 
pca955x_led_set(struct led_classdev * led_cdev,enum led_brightness value)309c3482b82SAndrew Lunn static int pca955x_led_set(struct led_classdev *led_cdev,
310c3482b82SAndrew Lunn 			    enum led_brightness value)
311f46e9203SNate Case {
312f5058548SEddie James 	struct pca955x_led *pca955x_led = led_to_pca955x(led_cdev);
313f5058548SEddie James 	struct pca955x *pca955x = pca955x_led->pca955x;
314f5058548SEddie James 	int reg = pca955x_led->led_num / 4;
315f5058548SEddie James 	int bit = pca955x_led->led_num % 4;
316f46e9203SNate Case 	u8 ls;
3171591caf2SCédric Le Goater 	int ret;
318f46e9203SNate Case 
319e7e11d8bSAlexander Stein 	mutex_lock(&pca955x->lock);
320f46e9203SNate Case 
321d451e3b4SEddie James 	ret = pca955x_read_ls(pca955x, reg, &ls);
3221591caf2SCédric Le Goater 	if (ret)
3231591caf2SCédric Le Goater 		goto out;
324f46e9203SNate Case 
325*d6da216fSEddie James 	if (test_bit(pca955x_led->led_num, &pca955x->active_blink)) {
326*d6da216fSEddie James 		if (value == LED_OFF) {
327*d6da216fSEddie James 			clear_bit(pca955x_led->led_num, &pca955x->active_blink);
328*d6da216fSEddie James 			ls = pca955x_ledsel(ls, bit, PCA955X_LS_LED_OFF);
329*d6da216fSEddie James 		} else {
330*d6da216fSEddie James 			/* No variable brightness for blinking LEDs */
331*d6da216fSEddie James 			goto out;
332*d6da216fSEddie James 		}
333*d6da216fSEddie James 	} else {
334c3482b82SAndrew Lunn 		switch (value) {
335f46e9203SNate Case 		case LED_FULL:
336f5058548SEddie James 			ls = pca955x_ledsel(ls, bit, PCA955X_LS_LED_ON);
337f46e9203SNate Case 			break;
338f46e9203SNate Case 		case LED_OFF:
339f5058548SEddie James 			ls = pca955x_ledsel(ls, bit, PCA955X_LS_LED_OFF);
340f46e9203SNate Case 			break;
341f46e9203SNate Case 		default:
342f46e9203SNate Case 			/*
343f46e9203SNate Case 			 * Use PWM1 for all other values. This has the unwanted
344f46e9203SNate Case 			 * side effect of making all LEDs on the chip share the
345f46e9203SNate Case 			 * same brightness level if set to a value other than
346*d6da216fSEddie James 			 * OFF or FULL. But, this is probably better than just
347*d6da216fSEddie James 			 * turning off for all other values.
348f46e9203SNate Case 			 */
349d451e3b4SEddie James 			ret = pca955x_write_pwm(pca955x, 1, 255 - value);
3501591caf2SCédric Le Goater 			if (ret)
3511591caf2SCédric Le Goater 				goto out;
352f5058548SEddie James 			ls = pca955x_ledsel(ls, bit, PCA955X_LS_BLINK1);
353f46e9203SNate Case 			break;
354f46e9203SNate Case 		}
355*d6da216fSEddie James 	}
356f46e9203SNate Case 
357d451e3b4SEddie James 	ret = pca955x_write_ls(pca955x, reg, ls);
358e7e11d8bSAlexander Stein 
3591591caf2SCédric Le Goater out:
360e7e11d8bSAlexander Stein 	mutex_unlock(&pca955x->lock);
361f46e9203SNate Case 
3621591caf2SCédric Le Goater 	return ret;
363f46e9203SNate Case }
364f46e9203SNate Case 
pca955x_period_to_psc(struct pca955x * pca955x,unsigned long p)365*d6da216fSEddie James static u8 pca955x_period_to_psc(struct pca955x *pca955x, unsigned long p)
366*d6da216fSEddie James {
367*d6da216fSEddie James 	p *= pca955x->chipdef->blink_div;
368*d6da216fSEddie James 	p /= MSEC_PER_SEC;
369*d6da216fSEddie James 	p -= 1;
370*d6da216fSEddie James 
371*d6da216fSEddie James 	return p;
372*d6da216fSEddie James }
373*d6da216fSEddie James 
pca955x_psc_to_period(struct pca955x * pca955x,u8 psc)374*d6da216fSEddie James static unsigned long pca955x_psc_to_period(struct pca955x *pca955x, u8 psc)
375*d6da216fSEddie James {
376*d6da216fSEddie James 	unsigned long p = psc;
377*d6da216fSEddie James 
378*d6da216fSEddie James 	p += 1;
379*d6da216fSEddie James 	p *= MSEC_PER_SEC;
380*d6da216fSEddie James 	p /= pca955x->chipdef->blink_div;
381*d6da216fSEddie James 
382*d6da216fSEddie James 	return p;
383*d6da216fSEddie James }
384*d6da216fSEddie James 
pca955x_led_blink(struct led_classdev * led_cdev,unsigned long * delay_on,unsigned long * delay_off)385*d6da216fSEddie James static int pca955x_led_blink(struct led_classdev *led_cdev,
386*d6da216fSEddie James 			     unsigned long *delay_on, unsigned long *delay_off)
387*d6da216fSEddie James {
388*d6da216fSEddie James 	struct pca955x_led *pca955x_led = led_to_pca955x(led_cdev);
389*d6da216fSEddie James 	struct pca955x *pca955x = pca955x_led->pca955x;
390*d6da216fSEddie James 	unsigned long p = *delay_on + *delay_off;
391*d6da216fSEddie James 	int ret = 0;
392*d6da216fSEddie James 
393*d6da216fSEddie James 	mutex_lock(&pca955x->lock);
394*d6da216fSEddie James 
395*d6da216fSEddie James 	if (p) {
396*d6da216fSEddie James 		if (*delay_on != *delay_off) {
397*d6da216fSEddie James 			ret = -EINVAL;
398*d6da216fSEddie James 			goto out;
399*d6da216fSEddie James 		}
400*d6da216fSEddie James 
401*d6da216fSEddie James 		if (p < pca955x_psc_to_period(pca955x, 0) ||
402*d6da216fSEddie James 		    p > pca955x_psc_to_period(pca955x, 0xff)) {
403*d6da216fSEddie James 			ret = -EINVAL;
404*d6da216fSEddie James 			goto out;
405*d6da216fSEddie James 		}
406*d6da216fSEddie James 	} else {
407*d6da216fSEddie James 		p = pca955x->active_blink ? pca955x->blink_period :
408*d6da216fSEddie James 			PCA955X_BLINK_DEFAULT_MS;
409*d6da216fSEddie James 	}
410*d6da216fSEddie James 
411*d6da216fSEddie James 	if (!pca955x->active_blink ||
412*d6da216fSEddie James 	    pca955x->active_blink == BIT(pca955x_led->led_num) ||
413*d6da216fSEddie James 	    pca955x->blink_period == p) {
414*d6da216fSEddie James 		u8 psc = pca955x_period_to_psc(pca955x, p);
415*d6da216fSEddie James 
416*d6da216fSEddie James 		if (!test_and_set_bit(pca955x_led->led_num,
417*d6da216fSEddie James 				      &pca955x->active_blink)) {
418*d6da216fSEddie James 			u8 ls;
419*d6da216fSEddie James 			int reg = pca955x_led->led_num / 4;
420*d6da216fSEddie James 			int bit = pca955x_led->led_num % 4;
421*d6da216fSEddie James 
422*d6da216fSEddie James 			ret = pca955x_read_ls(pca955x, reg, &ls);
423*d6da216fSEddie James 			if (ret)
424*d6da216fSEddie James 				goto out;
425*d6da216fSEddie James 
426*d6da216fSEddie James 			ls = pca955x_ledsel(ls, bit, PCA955X_LS_BLINK0);
427*d6da216fSEddie James 			ret = pca955x_write_ls(pca955x, reg, ls);
428*d6da216fSEddie James 			if (ret)
429*d6da216fSEddie James 				goto out;
430*d6da216fSEddie James 
431*d6da216fSEddie James 			/*
432*d6da216fSEddie James 			 * Force 50% duty cycle to maintain the specified
433*d6da216fSEddie James 			 * blink rate.
434*d6da216fSEddie James 			 */
435*d6da216fSEddie James 			ret = pca955x_write_pwm(pca955x, 0, 128);
436*d6da216fSEddie James 			if (ret)
437*d6da216fSEddie James 				goto out;
438*d6da216fSEddie James 		}
439*d6da216fSEddie James 
440*d6da216fSEddie James 		if (pca955x->blink_period != p) {
441*d6da216fSEddie James 			pca955x->blink_period = p;
442*d6da216fSEddie James 			ret = pca955x_write_psc(pca955x, 0, psc);
443*d6da216fSEddie James 			if (ret)
444*d6da216fSEddie James 				goto out;
445*d6da216fSEddie James 		}
446*d6da216fSEddie James 
447*d6da216fSEddie James 		p = pca955x_psc_to_period(pca955x, psc);
448*d6da216fSEddie James 		p /= 2;
449*d6da216fSEddie James 		*delay_on = p;
450*d6da216fSEddie James 		*delay_off = p;
451*d6da216fSEddie James 	} else {
452*d6da216fSEddie James 		ret = -EBUSY;
453*d6da216fSEddie James 	}
454*d6da216fSEddie James 
455*d6da216fSEddie James out:
456*d6da216fSEddie James 	mutex_unlock(&pca955x->lock);
457*d6da216fSEddie James 
458*d6da216fSEddie James 	return ret;
459*d6da216fSEddie James }
460*d6da216fSEddie James 
461561099a1SCédric Le Goater #ifdef CONFIG_LEDS_PCA955X_GPIO
462561099a1SCédric Le Goater /*
463561099a1SCédric Le Goater  * Read the INPUT register, which contains the state of LEDs.
464561099a1SCédric Le Goater  */
pca955x_read_input(struct i2c_client * client,int n,u8 * val)4651591caf2SCédric Le Goater static int pca955x_read_input(struct i2c_client *client, int n, u8 *val)
466561099a1SCédric Le Goater {
4671591caf2SCédric Le Goater 	int ret = i2c_smbus_read_byte_data(client, n);
4681591caf2SCédric Le Goater 
4691591caf2SCédric Le Goater 	if (ret < 0) {
4701591caf2SCédric Le Goater 		dev_err(&client->dev, "%s: reg 0x%x, err %d\n",
4711591caf2SCédric Le Goater 			__func__, n, ret);
4721591caf2SCédric Le Goater 		return ret;
4731591caf2SCédric Le Goater 	}
4741591caf2SCédric Le Goater 	*val = (u8)ret;
4751591caf2SCédric Le Goater 	return 0;
4761591caf2SCédric Le Goater 
477561099a1SCédric Le Goater }
478561099a1SCédric Le Goater 
pca955x_gpio_request_pin(struct gpio_chip * gc,unsigned int offset)479561099a1SCédric Le Goater static int pca955x_gpio_request_pin(struct gpio_chip *gc, unsigned int offset)
480561099a1SCédric Le Goater {
481561099a1SCédric Le Goater 	struct pca955x *pca955x = gpiochip_get_data(gc);
482561099a1SCédric Le Goater 
483ca386253SAndrew Jeffery 	return test_and_set_bit(offset, &pca955x->active_pins) ? -EBUSY : 0;
484ca386253SAndrew Jeffery }
485561099a1SCédric Le Goater 
pca955x_gpio_free_pin(struct gpio_chip * gc,unsigned int offset)486ca386253SAndrew Jeffery static void pca955x_gpio_free_pin(struct gpio_chip *gc, unsigned int offset)
487ca386253SAndrew Jeffery {
488ca386253SAndrew Jeffery 	struct pca955x *pca955x = gpiochip_get_data(gc);
489ca386253SAndrew Jeffery 
490ca386253SAndrew Jeffery 	clear_bit(offset, &pca955x->active_pins);
491561099a1SCédric Le Goater }
492561099a1SCédric Le Goater 
pca955x_set_value(struct gpio_chip * gc,unsigned int offset,int val)4931591caf2SCédric Le Goater static int pca955x_set_value(struct gpio_chip *gc, unsigned int offset,
494561099a1SCédric Le Goater 			     int val)
495561099a1SCédric Le Goater {
496561099a1SCédric Le Goater 	struct pca955x *pca955x = gpiochip_get_data(gc);
497561099a1SCédric Le Goater 	struct pca955x_led *led = &pca955x->leds[offset];
498561099a1SCédric Le Goater 
499561099a1SCédric Le Goater 	if (val)
50052ca7d0fSAndrew Jeffery 		return pca955x_led_set(&led->led_cdev, PCA955X_GPIO_HIGH);
50152ca7d0fSAndrew Jeffery 
50252ca7d0fSAndrew Jeffery 	return pca955x_led_set(&led->led_cdev, PCA955X_GPIO_LOW);
5031591caf2SCédric Le Goater }
5041591caf2SCédric Le Goater 
pca955x_gpio_set_value(struct gpio_chip * gc,unsigned int offset,int val)5051591caf2SCédric Le Goater static void pca955x_gpio_set_value(struct gpio_chip *gc, unsigned int offset,
5061591caf2SCédric Le Goater 				   int val)
5071591caf2SCédric Le Goater {
5081591caf2SCédric Le Goater 	pca955x_set_value(gc, offset, val);
509561099a1SCédric Le Goater }
510561099a1SCédric Le Goater 
pca955x_gpio_get_value(struct gpio_chip * gc,unsigned int offset)511561099a1SCédric Le Goater static int pca955x_gpio_get_value(struct gpio_chip *gc, unsigned int offset)
512561099a1SCédric Le Goater {
513561099a1SCédric Le Goater 	struct pca955x *pca955x = gpiochip_get_data(gc);
514561099a1SCédric Le Goater 	struct pca955x_led *led = &pca955x->leds[offset];
5151591caf2SCédric Le Goater 	u8 reg = 0;
5161591caf2SCédric Le Goater 
5171591caf2SCédric Le Goater 	/* There is nothing we can do about errors */
5181591caf2SCédric Le Goater 	pca955x_read_input(pca955x->client, led->led_num / 8, &reg);
519561099a1SCédric Le Goater 
520561099a1SCédric Le Goater 	return !!(reg & (1 << (led->led_num % 8)));
521561099a1SCédric Le Goater }
522561099a1SCédric Le Goater 
pca955x_gpio_direction_input(struct gpio_chip * gc,unsigned int offset)523561099a1SCédric Le Goater static int pca955x_gpio_direction_input(struct gpio_chip *gc,
524561099a1SCédric Le Goater 					unsigned int offset)
525561099a1SCédric Le Goater {
52652ca7d0fSAndrew Jeffery 	struct pca955x *pca955x = gpiochip_get_data(gc);
52752ca7d0fSAndrew Jeffery 	struct pca955x_led *led = &pca955x->leds[offset];
52852ca7d0fSAndrew Jeffery 
52952ca7d0fSAndrew Jeffery 	/* To use as input ensure pin is not driven. */
53052ca7d0fSAndrew Jeffery 	return pca955x_led_set(&led->led_cdev, PCA955X_GPIO_INPUT);
531561099a1SCédric Le Goater }
532561099a1SCédric Le Goater 
pca955x_gpio_direction_output(struct gpio_chip * gc,unsigned int offset,int val)533561099a1SCédric Le Goater static int pca955x_gpio_direction_output(struct gpio_chip *gc,
534561099a1SCédric Le Goater 					 unsigned int offset, int val)
535561099a1SCédric Le Goater {
5361591caf2SCédric Le Goater 	return pca955x_set_value(gc, offset, val);
537561099a1SCédric Le Goater }
538561099a1SCédric Le Goater #endif /* CONFIG_LEDS_PCA955X_GPIO */
539561099a1SCédric Le Goater 
540ed1f4b96SCédric Le Goater static struct pca955x_platform_data *
pca955x_get_pdata(struct i2c_client * client,struct pca955x_chipdef * chip)541967f69deSAndy Shevchenko pca955x_get_pdata(struct i2c_client *client, struct pca955x_chipdef *chip)
542ed1f4b96SCédric Le Goater {
543ed1f4b96SCédric Le Goater 	struct pca955x_platform_data *pdata;
5442420ae02SEddie James 	struct pca955x_led *led;
545967f69deSAndy Shevchenko 	struct fwnode_handle *child;
546ed1f4b96SCédric Le Goater 	int count;
547ed1f4b96SCédric Le Goater 
548967f69deSAndy Shevchenko 	count = device_get_child_node_count(&client->dev);
549e26557a0SAndrew Jeffery 	if (count > chip->bits)
550ed1f4b96SCédric Le Goater 		return ERR_PTR(-ENODEV);
551ed1f4b96SCédric Le Goater 
552ed1f4b96SCédric Le Goater 	pdata = devm_kzalloc(&client->dev, sizeof(*pdata), GFP_KERNEL);
553ed1f4b96SCédric Le Goater 	if (!pdata)
554ed1f4b96SCédric Le Goater 		return ERR_PTR(-ENOMEM);
555ed1f4b96SCédric Le Goater 
556a86854d0SKees Cook 	pdata->leds = devm_kcalloc(&client->dev,
557a86854d0SKees Cook 				   chip->bits, sizeof(struct pca955x_led),
558ed1f4b96SCédric Le Goater 				   GFP_KERNEL);
559ed1f4b96SCédric Le Goater 	if (!pdata->leds)
560ed1f4b96SCédric Le Goater 		return ERR_PTR(-ENOMEM);
561ed1f4b96SCédric Le Goater 
562967f69deSAndy Shevchenko 	device_for_each_child_node(&client->dev, child) {
563ed1f4b96SCédric Le Goater 		u32 reg;
564ed1f4b96SCédric Le Goater 		int res;
565ed1f4b96SCédric Le Goater 
566967f69deSAndy Shevchenko 		res = fwnode_property_read_u32(child, "reg", &reg);
567ed1f4b96SCédric Le Goater 		if ((res != 0) || (reg >= chip->bits))
568ed1f4b96SCédric Le Goater 			continue;
569ed1f4b96SCédric Le Goater 
5702420ae02SEddie James 		led = &pdata->leds[reg];
5712420ae02SEddie James 		led->type = PCA955X_TYPE_LED;
5727c481592SEddie James 		led->fwnode = child;
5730dd37b1cSAndy Shevchenko 		led->default_state = led_init_default_state_get(child);
574e46cb6d0SEddie James 
5750dd37b1cSAndy Shevchenko 		fwnode_property_read_u32(child, "type", &led->type);
576ed1f4b96SCédric Le Goater 	}
577ed1f4b96SCédric Le Goater 
578ed1f4b96SCédric Le Goater 	pdata->num_leds = chip->bits;
579ed1f4b96SCédric Le Goater 
580ed1f4b96SCédric Le Goater 	return pdata;
581ed1f4b96SCédric Le Goater }
582ed1f4b96SCédric Le Goater 
583ed1f4b96SCédric Le Goater static const struct of_device_id of_pca955x_match[] = {
584ed1f4b96SCédric Le Goater 	{ .compatible = "nxp,pca9550", .data = (void *)pca9550 },
585ed1f4b96SCédric Le Goater 	{ .compatible = "nxp,pca9551", .data = (void *)pca9551 },
586ed1f4b96SCédric Le Goater 	{ .compatible = "nxp,pca9552", .data = (void *)pca9552 },
58746de3adbSEddie James 	{ .compatible = "ibm,pca9552", .data = (void *)ibm_pca9552 },
588ed1f4b96SCédric Le Goater 	{ .compatible = "nxp,pca9553", .data = (void *)pca9553 },
589ed1f4b96SCédric Le Goater 	{},
590ed1f4b96SCédric Le Goater };
591ed1f4b96SCédric Le Goater MODULE_DEVICE_TABLE(of, of_pca955x_match);
592ed1f4b96SCédric Le Goater 
pca955x_probe(struct i2c_client * client)593239f32b4SEddie James static int pca955x_probe(struct i2c_client *client)
594f46e9203SNate Case {
595e7e11d8bSAlexander Stein 	struct pca955x *pca955x;
596e7e11d8bSAlexander Stein 	struct pca955x_led *pca955x_led;
597f46e9203SNate Case 	struct pca955x_chipdef *chip;
5982420ae02SEddie James 	struct led_classdev *led;
5997c481592SEddie James 	struct led_init_data init_data;
600f46e9203SNate Case 	struct i2c_adapter *adapter;
6013368d344SEddie James 	int i, bit, err, nls, reg;
6023368d344SEddie James 	u8 ls1[4];
6033368d344SEddie James 	u8 ls2[4];
604ed1f4b96SCédric Le Goater 	struct pca955x_platform_data *pdata;
605*d6da216fSEddie James 	u8 psc0;
606*d6da216fSEddie James 	bool keep_psc0 = false;
6077c481592SEddie James 	bool set_default_label = false;
6087c481592SEddie James 	char default_label[8];
609239f32b4SEddie James 	enum pca955x_type chip_type;
610239f32b4SEddie James 	const void *md = device_get_match_data(&client->dev);
611f46e9203SNate Case 
612239f32b4SEddie James 	if (md) {
613239f32b4SEddie James 		chip_type = (enum pca955x_type)md;
614239f32b4SEddie James 	} else {
615239f32b4SEddie James 		const struct i2c_device_id *id = i2c_match_id(pca955x_id,
616239f32b4SEddie James 							      client);
617239f32b4SEddie James 
618239f32b4SEddie James 		if (id) {
619239f32b4SEddie James 			chip_type = (enum pca955x_type)id->driver_data;
620239f32b4SEddie James 		} else {
621239f32b4SEddie James 			dev_err(&client->dev, "unknown chip\n");
622239f32b4SEddie James 			return -ENODEV;
623239f32b4SEddie James 		}
624239f32b4SEddie James 	}
625239f32b4SEddie James 
626239f32b4SEddie James 	chip = &pca955x_chipdefs[chip_type];
6271c57d9bdSWolfram Sang 	adapter = client->adapter;
62887aae1eaSJingoo Han 	pdata = dev_get_platdata(&client->dev);
629ed1f4b96SCédric Le Goater 	if (!pdata) {
630967f69deSAndy Shevchenko 		pdata =	pca955x_get_pdata(client, chip);
631ed1f4b96SCédric Le Goater 		if (IS_ERR(pdata))
632ed1f4b96SCédric Le Goater 			return PTR_ERR(pdata);
633ed1f4b96SCédric Le Goater 	}
634f46e9203SNate Case 
635f46e9203SNate Case 	/* Make sure the slave address / chip type combo given is possible */
636f46e9203SNate Case 	if ((client->addr & ~((1 << chip->slv_addr_shift) - 1)) !=
637f46e9203SNate Case 	    chip->slv_addr) {
638f46e9203SNate Case 		dev_err(&client->dev, "invalid slave address %02x\n",
639f46e9203SNate Case 			client->addr);
640f46e9203SNate Case 		return -ENODEV;
641f46e9203SNate Case 	}
642f46e9203SNate Case 
643b40b0c17SSachin Kamat 	dev_info(&client->dev, "leds-pca955x: Using %s %d-bit LED driver at "
6442420ae02SEddie James 		 "slave address 0x%02x\n", client->name, chip->bits,
6452420ae02SEddie James 		 client->addr);
646f46e9203SNate Case 
647aace34c0STin Huynh 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
648f46e9203SNate Case 		return -EIO;
649f46e9203SNate Case 
650f46e9203SNate Case 	if (pdata->num_leds != chip->bits) {
651ed1f4b96SCédric Le Goater 		dev_err(&client->dev,
652ed1f4b96SCédric Le Goater 			"board info claims %d LEDs on a %d-bit chip\n",
653f46e9203SNate Case 			pdata->num_leds, chip->bits);
654f46e9203SNate Case 		return -ENODEV;
655f46e9203SNate Case 	}
656f46e9203SNate Case 
65773759f6aSBryan Wu 	pca955x = devm_kzalloc(&client->dev, sizeof(*pca955x), GFP_KERNEL);
65895bf14bfSSven Wegener 	if (!pca955x)
65995bf14bfSSven Wegener 		return -ENOMEM;
660f46e9203SNate Case 
6612420ae02SEddie James 	pca955x->leds = devm_kcalloc(&client->dev, chip->bits,
6622420ae02SEddie James 				     sizeof(*pca955x_led), GFP_KERNEL);
66373759f6aSBryan Wu 	if (!pca955x->leds)
66473759f6aSBryan Wu 		return -ENOMEM;
665e7e11d8bSAlexander Stein 
66695bf14bfSSven Wegener 	i2c_set_clientdata(client, pca955x);
66795bf14bfSSven Wegener 
668e7e11d8bSAlexander Stein 	mutex_init(&pca955x->lock);
669e7e11d8bSAlexander Stein 	pca955x->client = client;
670e7e11d8bSAlexander Stein 	pca955x->chipdef = chip;
671*d6da216fSEddie James 	pca955x->blink_period = PCA955X_BLINK_DEFAULT_MS;
672e7e11d8bSAlexander Stein 
6737c481592SEddie James 	init_data.devname_mandatory = false;
6747c481592SEddie James 	init_data.devicename = "pca955x";
6757c481592SEddie James 
6763368d344SEddie James 	nls = pca955x_num_led_regs(chip->bits);
6773368d344SEddie James 	/* use auto-increment feature to read all the led selectors at once */
6783368d344SEddie James 	err = i2c_smbus_read_i2c_block_data(client,
6793368d344SEddie James 					    0x10 | (pca955x_num_input_regs(chip->bits) + 4), nls,
6803368d344SEddie James 					    ls1);
6813368d344SEddie James 	if (err < 0)
6823368d344SEddie James 		return err;
6833368d344SEddie James 
6843368d344SEddie James 	for (i = 0; i < nls; ++i)
6853368d344SEddie James 		ls2[i] = ls1[i];
6863368d344SEddie James 
68795bf14bfSSven Wegener 	for (i = 0; i < chip->bits; i++) {
688e7e11d8bSAlexander Stein 		pca955x_led = &pca955x->leds[i];
689e7e11d8bSAlexander Stein 		pca955x_led->led_num = i;
690e7e11d8bSAlexander Stein 		pca955x_led->pca955x = pca955x;
691561099a1SCédric Le Goater 		pca955x_led->type = pdata->leds[i].type;
69295bf14bfSSven Wegener 
693561099a1SCédric Le Goater 		switch (pca955x_led->type) {
694561099a1SCédric Le Goater 		case PCA955X_TYPE_NONE:
695561099a1SCédric Le Goater 		case PCA955X_TYPE_GPIO:
696561099a1SCédric Le Goater 			break;
697561099a1SCédric Le Goater 		case PCA955X_TYPE_LED:
6983368d344SEddie James 			bit = i % 4;
6993368d344SEddie James 			reg = i / 4;
7002420ae02SEddie James 			led = &pca955x_led->led_cdev;
7012420ae02SEddie James 			led->brightness_set_blocking = pca955x_led_set;
7027086625fSEddie James 			led->brightness_get = pca955x_led_get;
703*d6da216fSEddie James 			led->blink_set = pca955x_led_blink;
704f46e9203SNate Case 
7053368d344SEddie James 			if (pdata->leds[i].default_state == LEDS_DEFSTATE_OFF)
7063368d344SEddie James 				ls2[reg] = pca955x_ledsel(ls2[reg], bit, PCA955X_LS_LED_OFF);
7073368d344SEddie James 			else if (pdata->leds[i].default_state == LEDS_DEFSTATE_ON)
7083368d344SEddie James 				ls2[reg] = pca955x_ledsel(ls2[reg], bit, PCA955X_LS_LED_ON);
709*d6da216fSEddie James 			else if (pca955x_ledstate(ls2[reg], bit) == PCA955X_LS_BLINK0) {
710*d6da216fSEddie James 				keep_psc0 = true;
711*d6da216fSEddie James 				set_bit(i, &pca955x->active_blink);
712*d6da216fSEddie James 			}
713e46cb6d0SEddie James 
7147c481592SEddie James 			init_data.fwnode = pdata->leds[i].fwnode;
7157c481592SEddie James 
7167c481592SEddie James 			if (is_of_node(init_data.fwnode)) {
7177c481592SEddie James 				if (to_of_node(init_data.fwnode)->name[0] ==
7187c481592SEddie James 				    '\0')
7197c481592SEddie James 					set_default_label = true;
7207c481592SEddie James 				else
7217c481592SEddie James 					set_default_label = false;
7227c481592SEddie James 			} else {
7237c481592SEddie James 				set_default_label = true;
7247c481592SEddie James 			}
7257c481592SEddie James 
7267c481592SEddie James 			if (set_default_label) {
7277c481592SEddie James 				snprintf(default_label, sizeof(default_label),
7287c481592SEddie James 					 "%d", i);
7297c481592SEddie James 				init_data.default_label = default_label;
7307c481592SEddie James 			} else {
7317c481592SEddie James 				init_data.default_label = NULL;
7327c481592SEddie James 			}
7337c481592SEddie James 
7347c481592SEddie James 			err = devm_led_classdev_register_ext(&client->dev, led,
7357c481592SEddie James 							     &init_data);
73691940bb4SCédric Le Goater 			if (err)
73791940bb4SCédric Le Goater 				return err;
738f46e9203SNate Case 
739ca386253SAndrew Jeffery 			set_bit(i, &pca955x->active_pins);
740561099a1SCédric Le Goater 		}
741561099a1SCédric Le Goater 	}
742f46e9203SNate Case 
7433368d344SEddie James 	for (i = 0; i < nls; ++i) {
7443368d344SEddie James 		if (ls1[i] != ls2[i]) {
7453368d344SEddie James 			err = pca955x_write_ls(pca955x, i, ls2[i]);
7463368d344SEddie James 			if (err)
7473368d344SEddie James 				return err;
7483368d344SEddie James 		}
7493368d344SEddie James 	}
7503368d344SEddie James 
751*d6da216fSEddie James 	if (keep_psc0) {
752*d6da216fSEddie James 		err = pca955x_read_psc(pca955x, 0, &psc0);
753*d6da216fSEddie James 	} else {
754*d6da216fSEddie James 		psc0 = pca955x_period_to_psc(pca955x, pca955x->blink_period);
755*d6da216fSEddie James 		err = pca955x_write_psc(pca955x, 0, psc0);
756e46cb6d0SEddie James 	}
757f46e9203SNate Case 
7581591caf2SCédric Le Goater 	if (err)
7591591caf2SCédric Le Goater 		return err;
760*d6da216fSEddie James 
761*d6da216fSEddie James 	pca955x->blink_period = pca955x_psc_to_period(pca955x, psc0);
762*d6da216fSEddie James 
763*d6da216fSEddie James 	/* Set PWM1 to fast frequency so we do not see flashing */
764d451e3b4SEddie James 	err = pca955x_write_psc(pca955x, 1, 0);
7651591caf2SCédric Le Goater 	if (err)
7661591caf2SCédric Le Goater 		return err;
767f46e9203SNate Case 
768561099a1SCédric Le Goater #ifdef CONFIG_LEDS_PCA955X_GPIO
769561099a1SCédric Le Goater 	pca955x->gpio.label = "gpio-pca955x";
770561099a1SCédric Le Goater 	pca955x->gpio.direction_input = pca955x_gpio_direction_input;
771561099a1SCédric Le Goater 	pca955x->gpio.direction_output = pca955x_gpio_direction_output;
772561099a1SCédric Le Goater 	pca955x->gpio.set = pca955x_gpio_set_value;
773561099a1SCédric Le Goater 	pca955x->gpio.get = pca955x_gpio_get_value;
774561099a1SCédric Le Goater 	pca955x->gpio.request = pca955x_gpio_request_pin;
775ca386253SAndrew Jeffery 	pca955x->gpio.free = pca955x_gpio_free_pin;
776561099a1SCédric Le Goater 	pca955x->gpio.can_sleep = 1;
777561099a1SCédric Le Goater 	pca955x->gpio.base = -1;
778ca386253SAndrew Jeffery 	pca955x->gpio.ngpio = chip->bits;
779561099a1SCédric Le Goater 	pca955x->gpio.parent = &client->dev;
780561099a1SCédric Le Goater 	pca955x->gpio.owner = THIS_MODULE;
781561099a1SCédric Le Goater 
782561099a1SCédric Le Goater 	err = devm_gpiochip_add_data(&client->dev, &pca955x->gpio,
783561099a1SCédric Le Goater 				     pca955x);
784561099a1SCédric Le Goater 	if (err) {
785561099a1SCédric Le Goater 		/* Use data->gpio.dev as a flag for freeing gpiochip */
786561099a1SCédric Le Goater 		pca955x->gpio.parent = NULL;
787561099a1SCédric Le Goater 		dev_warn(&client->dev, "could not add gpiochip\n");
788561099a1SCédric Le Goater 		return err;
789561099a1SCédric Le Goater 	}
790561099a1SCédric Le Goater 	dev_info(&client->dev, "gpios %i...%i\n",
791561099a1SCédric Le Goater 		 pca955x->gpio.base, pca955x->gpio.base +
792561099a1SCédric Le Goater 		 pca955x->gpio.ngpio - 1);
793561099a1SCédric Le Goater #endif
794561099a1SCédric Le Goater 
795f46e9203SNate Case 	return 0;
796f46e9203SNate Case }
797f46e9203SNate Case 
798f46e9203SNate Case static struct i2c_driver pca955x_driver = {
799f46e9203SNate Case 	.driver = {
800f46e9203SNate Case 		.name	= "leds-pca955x",
801967f69deSAndy Shevchenko 		.of_match_table = of_pca955x_match,
802f46e9203SNate Case 	},
803d9ff8a8eSUwe Kleine-König 	.probe = pca955x_probe,
804f46e9203SNate Case 	.id_table = pca955x_id,
805f46e9203SNate Case };
806f46e9203SNate Case 
80709a0d183SAxel Lin module_i2c_driver(pca955x_driver);
808f46e9203SNate Case 
809f46e9203SNate Case MODULE_AUTHOR("Nate Case <ncase@xes-inc.com>");
810f46e9203SNate Case MODULE_DESCRIPTION("PCA955x LED driver");
811f46e9203SNate Case MODULE_LICENSE("GPL v2");
812