xref: /openbmc/linux/drivers/leds/leds-lp5521.c (revision 6ce6176263393dd80b9a537c1e1462b8529f240b)
1500fe141SSamu Onkalo /*
2500fe141SSamu Onkalo  * LP5521 LED chip driver.
3500fe141SSamu Onkalo  *
4500fe141SSamu Onkalo  * Copyright (C) 2010 Nokia Corporation
5500fe141SSamu Onkalo  *
6500fe141SSamu Onkalo  * Contact: Samu Onkalo <samu.p.onkalo@nokia.com>
7500fe141SSamu Onkalo  *
8500fe141SSamu Onkalo  * This program is free software; you can redistribute it and/or
9500fe141SSamu Onkalo  * modify it under the terms of the GNU General Public License
10500fe141SSamu Onkalo  * version 2 as published by the Free Software Foundation.
11500fe141SSamu Onkalo  *
12500fe141SSamu Onkalo  * This program is distributed in the hope that it will be useful, but
13500fe141SSamu Onkalo  * WITHOUT ANY WARRANTY; without even the implied warranty of
14500fe141SSamu Onkalo  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
15500fe141SSamu Onkalo  * General Public License for more details.
16500fe141SSamu Onkalo  *
17500fe141SSamu Onkalo  * You should have received a copy of the GNU General Public License
18500fe141SSamu Onkalo  * along with this program; if not, write to the Free Software
19500fe141SSamu Onkalo  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20500fe141SSamu Onkalo  * 02110-1301 USA
21500fe141SSamu Onkalo  */
22500fe141SSamu Onkalo 
23500fe141SSamu Onkalo #include <linux/module.h>
24500fe141SSamu Onkalo #include <linux/init.h>
25500fe141SSamu Onkalo #include <linux/i2c.h>
26500fe141SSamu Onkalo #include <linux/mutex.h>
27500fe141SSamu Onkalo #include <linux/gpio.h>
28500fe141SSamu Onkalo #include <linux/interrupt.h>
29500fe141SSamu Onkalo #include <linux/delay.h>
30500fe141SSamu Onkalo #include <linux/ctype.h>
31500fe141SSamu Onkalo #include <linux/spinlock.h>
32500fe141SSamu Onkalo #include <linux/wait.h>
33500fe141SSamu Onkalo #include <linux/leds.h>
34500fe141SSamu Onkalo #include <linux/leds-lp5521.h>
35500fe141SSamu Onkalo #include <linux/workqueue.h>
36500fe141SSamu Onkalo #include <linux/slab.h>
376a0c9a47SMilo(Woogyom) Kim #include <linux/platform_data/leds-lp55xx.h>
386a0c9a47SMilo(Woogyom) Kim 
396a0c9a47SMilo(Woogyom) Kim #include "leds-lp55xx-common.h"
40500fe141SSamu Onkalo 
41500fe141SSamu Onkalo #define LP5521_PROGRAM_LENGTH		32	/* in bytes */
42500fe141SSamu Onkalo 
43500fe141SSamu Onkalo #define LP5521_MAX_LEDS			3	/* Maximum number of LEDs */
44500fe141SSamu Onkalo #define LP5521_MAX_ENGINES		3	/* Maximum number of engines */
45500fe141SSamu Onkalo 
46500fe141SSamu Onkalo #define LP5521_ENG_MASK_BASE		0x30	/* 00110000 */
47500fe141SSamu Onkalo #define LP5521_ENG_STATUS_MASK		0x07	/* 00000111 */
48500fe141SSamu Onkalo 
49500fe141SSamu Onkalo #define LP5521_CMD_LOAD			0x15	/* 00010101 */
50500fe141SSamu Onkalo #define LP5521_CMD_RUN			0x2a	/* 00101010 */
51500fe141SSamu Onkalo #define LP5521_CMD_DIRECT		0x3f	/* 00111111 */
52500fe141SSamu Onkalo #define LP5521_CMD_DISABLED		0x00	/* 00000000 */
53500fe141SSamu Onkalo 
54500fe141SSamu Onkalo /* Registers */
55500fe141SSamu Onkalo #define LP5521_REG_ENABLE		0x00
56500fe141SSamu Onkalo #define LP5521_REG_OP_MODE		0x01
57500fe141SSamu Onkalo #define LP5521_REG_R_PWM		0x02
58500fe141SSamu Onkalo #define LP5521_REG_G_PWM		0x03
59500fe141SSamu Onkalo #define LP5521_REG_B_PWM		0x04
60500fe141SSamu Onkalo #define LP5521_REG_R_CURRENT		0x05
61500fe141SSamu Onkalo #define LP5521_REG_G_CURRENT		0x06
62500fe141SSamu Onkalo #define LP5521_REG_B_CURRENT		0x07
63500fe141SSamu Onkalo #define LP5521_REG_CONFIG		0x08
64500fe141SSamu Onkalo #define LP5521_REG_R_CHANNEL_PC		0x09
65500fe141SSamu Onkalo #define LP5521_REG_G_CHANNEL_PC		0x0A
66500fe141SSamu Onkalo #define LP5521_REG_B_CHANNEL_PC		0x0B
67500fe141SSamu Onkalo #define LP5521_REG_STATUS		0x0C
68500fe141SSamu Onkalo #define LP5521_REG_RESET		0x0D
69500fe141SSamu Onkalo #define LP5521_REG_GPO			0x0E
70500fe141SSamu Onkalo #define LP5521_REG_R_PROG_MEM		0x10
71500fe141SSamu Onkalo #define LP5521_REG_G_PROG_MEM		0x30
72500fe141SSamu Onkalo #define LP5521_REG_B_PROG_MEM		0x50
73500fe141SSamu Onkalo 
74500fe141SSamu Onkalo #define LP5521_PROG_MEM_BASE		LP5521_REG_R_PROG_MEM
75500fe141SSamu Onkalo #define LP5521_PROG_MEM_SIZE		0x20
76500fe141SSamu Onkalo 
77500fe141SSamu Onkalo /* Base register to set LED current */
78500fe141SSamu Onkalo #define LP5521_REG_LED_CURRENT_BASE	LP5521_REG_R_CURRENT
79500fe141SSamu Onkalo 
80500fe141SSamu Onkalo /* Base register to set the brightness */
81500fe141SSamu Onkalo #define LP5521_REG_LED_PWM_BASE		LP5521_REG_R_PWM
82500fe141SSamu Onkalo 
83500fe141SSamu Onkalo /* Bits in ENABLE register */
84500fe141SSamu Onkalo #define LP5521_MASTER_ENABLE		0x40	/* Chip master enable */
85500fe141SSamu Onkalo #define LP5521_LOGARITHMIC_PWM		0x80	/* Logarithmic PWM adjustment */
86500fe141SSamu Onkalo #define LP5521_EXEC_RUN			0x2A
8732a2f747SKim, Milo #define LP5521_ENABLE_DEFAULT	\
8832a2f747SKim, Milo 	(LP5521_MASTER_ENABLE | LP5521_LOGARITHMIC_PWM)
8932a2f747SKim, Milo #define LP5521_ENABLE_RUN_PROGRAM	\
9032a2f747SKim, Milo 	(LP5521_ENABLE_DEFAULT | LP5521_EXEC_RUN)
91500fe141SSamu Onkalo 
92500fe141SSamu Onkalo /* Status */
93500fe141SSamu Onkalo #define LP5521_EXT_CLK_USED		0x08
94500fe141SSamu Onkalo 
95b3c49c05SSrinidhi KASAGAR /* default R channel current register value */
96b3c49c05SSrinidhi KASAGAR #define LP5521_REG_R_CURR_DEFAULT	0xAF
97b3c49c05SSrinidhi KASAGAR 
98011af7bcSKim, Milo /* Pattern Mode */
99011af7bcSKim, Milo #define PATTERN_OFF	0
100011af7bcSKim, Milo 
10148068d5dSMilo(Woogyom) Kim /* Reset register value */
10248068d5dSMilo(Woogyom) Kim #define LP5521_RESET			0xFF
10348068d5dSMilo(Woogyom) Kim 
104500fe141SSamu Onkalo struct lp5521_engine {
105500fe141SSamu Onkalo 	int		id;
106500fe141SSamu Onkalo 	u8		mode;
107500fe141SSamu Onkalo 	u8		prog_page;
108500fe141SSamu Onkalo 	u8		engine_mask;
109500fe141SSamu Onkalo };
110500fe141SSamu Onkalo 
111500fe141SSamu Onkalo struct lp5521_led {
112500fe141SSamu Onkalo 	int			id;
113500fe141SSamu Onkalo 	u8			chan_nr;
114500fe141SSamu Onkalo 	u8			led_current;
115500fe141SSamu Onkalo 	u8			max_current;
116500fe141SSamu Onkalo 	struct led_classdev	cdev;
117500fe141SSamu Onkalo 	struct work_struct	brightness_work;
118500fe141SSamu Onkalo 	u8			brightness;
119500fe141SSamu Onkalo };
120500fe141SSamu Onkalo 
121500fe141SSamu Onkalo struct lp5521_chip {
122500fe141SSamu Onkalo 	struct lp5521_platform_data *pdata;
123500fe141SSamu Onkalo 	struct mutex		lock; /* Serialize control */
124500fe141SSamu Onkalo 	struct i2c_client	*client;
125500fe141SSamu Onkalo 	struct lp5521_engine	engines[LP5521_MAX_ENGINES];
126500fe141SSamu Onkalo 	struct lp5521_led	leds[LP5521_MAX_LEDS];
127500fe141SSamu Onkalo 	u8			num_channels;
128500fe141SSamu Onkalo 	u8			num_leds;
129500fe141SSamu Onkalo };
130500fe141SSamu Onkalo 
13194482174SMilo(Woogyom) Kim static inline void lp5521_wait_enable_done(void)
13294482174SMilo(Woogyom) Kim {
13394482174SMilo(Woogyom) Kim 	/* it takes more 488 us to update ENABLE register */
13494482174SMilo(Woogyom) Kim 	usleep_range(500, 600);
13594482174SMilo(Woogyom) Kim }
13694482174SMilo(Woogyom) Kim 
1379fdb18b6SSamu Onkalo static inline struct lp5521_led *cdev_to_led(struct led_classdev *cdev)
1389fdb18b6SSamu Onkalo {
1399fdb18b6SSamu Onkalo 	return container_of(cdev, struct lp5521_led, cdev);
1409fdb18b6SSamu Onkalo }
1419fdb18b6SSamu Onkalo 
1429fdb18b6SSamu Onkalo static inline struct lp5521_chip *engine_to_lp5521(struct lp5521_engine *engine)
1439fdb18b6SSamu Onkalo {
1449fdb18b6SSamu Onkalo 	return container_of(engine, struct lp5521_chip,
1459fdb18b6SSamu Onkalo 			    engines[engine->id - 1]);
1469fdb18b6SSamu Onkalo }
1479fdb18b6SSamu Onkalo 
1489fdb18b6SSamu Onkalo static inline struct lp5521_chip *led_to_lp5521(struct lp5521_led *led)
1499fdb18b6SSamu Onkalo {
1509fdb18b6SSamu Onkalo 	return container_of(led, struct lp5521_chip,
1519fdb18b6SSamu Onkalo 			    leds[led->id]);
1529fdb18b6SSamu Onkalo }
153500fe141SSamu Onkalo 
154500fe141SSamu Onkalo static void lp5521_led_brightness_work(struct work_struct *work);
155500fe141SSamu Onkalo 
156500fe141SSamu Onkalo static inline int lp5521_write(struct i2c_client *client, u8 reg, u8 value)
157500fe141SSamu Onkalo {
158500fe141SSamu Onkalo 	return i2c_smbus_write_byte_data(client, reg, value);
159500fe141SSamu Onkalo }
160500fe141SSamu Onkalo 
161500fe141SSamu Onkalo static int lp5521_read(struct i2c_client *client, u8 reg, u8 *buf)
162500fe141SSamu Onkalo {
163500fe141SSamu Onkalo 	s32 ret;
164500fe141SSamu Onkalo 
165500fe141SSamu Onkalo 	ret = i2c_smbus_read_byte_data(client, reg);
166500fe141SSamu Onkalo 	if (ret < 0)
167313e8b50SSachin Kamat 		return ret;
168500fe141SSamu Onkalo 
169500fe141SSamu Onkalo 	*buf = ret;
170500fe141SSamu Onkalo 	return 0;
171500fe141SSamu Onkalo }
172500fe141SSamu Onkalo 
173500fe141SSamu Onkalo static int lp5521_set_engine_mode(struct lp5521_engine *engine, u8 mode)
174500fe141SSamu Onkalo {
175500fe141SSamu Onkalo 	struct lp5521_chip *chip = engine_to_lp5521(engine);
176500fe141SSamu Onkalo 	struct i2c_client *client = chip->client;
177500fe141SSamu Onkalo 	int ret;
178500fe141SSamu Onkalo 	u8 engine_state;
179500fe141SSamu Onkalo 
180500fe141SSamu Onkalo 	/* Only transition between RUN and DIRECT mode are handled here */
181500fe141SSamu Onkalo 	if (mode == LP5521_CMD_LOAD)
182500fe141SSamu Onkalo 		return 0;
183500fe141SSamu Onkalo 
184500fe141SSamu Onkalo 	if (mode == LP5521_CMD_DISABLED)
185500fe141SSamu Onkalo 		mode = LP5521_CMD_DIRECT;
186500fe141SSamu Onkalo 
187500fe141SSamu Onkalo 	ret = lp5521_read(client, LP5521_REG_OP_MODE, &engine_state);
188fa0ea0e1SAxel Lin 	if (ret < 0)
189fa0ea0e1SAxel Lin 		return ret;
190500fe141SSamu Onkalo 
191500fe141SSamu Onkalo 	/* set mode only for this engine */
192500fe141SSamu Onkalo 	engine_state &= ~(engine->engine_mask);
193500fe141SSamu Onkalo 	mode &= engine->engine_mask;
194500fe141SSamu Onkalo 	engine_state |= mode;
195fa0ea0e1SAxel Lin 	return lp5521_write(client, LP5521_REG_OP_MODE, engine_state);
196500fe141SSamu Onkalo }
197500fe141SSamu Onkalo 
198500fe141SSamu Onkalo static int lp5521_load_program(struct lp5521_engine *eng, const u8 *pattern)
199500fe141SSamu Onkalo {
200500fe141SSamu Onkalo 	struct lp5521_chip *chip = engine_to_lp5521(eng);
201500fe141SSamu Onkalo 	struct i2c_client *client = chip->client;
202500fe141SSamu Onkalo 	int ret;
203500fe141SSamu Onkalo 	int addr;
204500fe141SSamu Onkalo 	u8 mode;
205500fe141SSamu Onkalo 
206500fe141SSamu Onkalo 	/* move current engine to direct mode and remember the state */
207500fe141SSamu Onkalo 	ret = lp5521_set_engine_mode(eng, LP5521_CMD_DIRECT);
2085bc9ad77SDan Carpenter 	if (ret)
2095bc9ad77SDan Carpenter 		return ret;
2105bc9ad77SDan Carpenter 
21109c76b0fSSamu Onkalo 	/* Mode change requires min 500 us delay. 1 - 2 ms  with margin */
21209c76b0fSSamu Onkalo 	usleep_range(1000, 2000);
2135bc9ad77SDan Carpenter 	ret = lp5521_read(client, LP5521_REG_OP_MODE, &mode);
2145bc9ad77SDan Carpenter 	if (ret)
2155bc9ad77SDan Carpenter 		return ret;
216500fe141SSamu Onkalo 
217500fe141SSamu Onkalo 	/* For loading, all the engines to load mode */
218500fe141SSamu Onkalo 	lp5521_write(client, LP5521_REG_OP_MODE, LP5521_CMD_DIRECT);
21909c76b0fSSamu Onkalo 	/* Mode change requires min 500 us delay. 1 - 2 ms  with margin */
22009c76b0fSSamu Onkalo 	usleep_range(1000, 2000);
221500fe141SSamu Onkalo 	lp5521_write(client, LP5521_REG_OP_MODE, LP5521_CMD_LOAD);
22209c76b0fSSamu Onkalo 	/* Mode change requires min 500 us delay. 1 - 2 ms  with margin */
22309c76b0fSSamu Onkalo 	usleep_range(1000, 2000);
224500fe141SSamu Onkalo 
225500fe141SSamu Onkalo 	addr = LP5521_PROG_MEM_BASE + eng->prog_page * LP5521_PROG_MEM_SIZE;
226500fe141SSamu Onkalo 	i2c_smbus_write_i2c_block_data(client,
227500fe141SSamu Onkalo 				addr,
228500fe141SSamu Onkalo 				LP5521_PROG_MEM_SIZE,
229500fe141SSamu Onkalo 				pattern);
230500fe141SSamu Onkalo 
2315bc9ad77SDan Carpenter 	return lp5521_write(client, LP5521_REG_OP_MODE, mode);
232500fe141SSamu Onkalo }
233500fe141SSamu Onkalo 
234500fe141SSamu Onkalo static int lp5521_set_led_current(struct lp5521_chip *chip, int led, u8 curr)
235500fe141SSamu Onkalo {
236500fe141SSamu Onkalo 	return lp5521_write(chip->client,
237500fe141SSamu Onkalo 		    LP5521_REG_LED_CURRENT_BASE + chip->leds[led].chan_nr,
238500fe141SSamu Onkalo 		    curr);
239500fe141SSamu Onkalo }
240500fe141SSamu Onkalo 
241ffbdccdbSMilo(Woogyom) Kim static int lp5521_post_init_device(struct lp55xx_chip *chip)
242500fe141SSamu Onkalo {
243500fe141SSamu Onkalo 	int ret;
24494482174SMilo(Woogyom) Kim 	u8 val;
245500fe141SSamu Onkalo 
24694482174SMilo(Woogyom) Kim 	/*
24794482174SMilo(Woogyom) Kim 	 * Make sure that the chip is reset by reading back the r channel
24894482174SMilo(Woogyom) Kim 	 * current reg. This is dummy read is required on some platforms -
24994482174SMilo(Woogyom) Kim 	 * otherwise further access to the R G B channels in the
25094482174SMilo(Woogyom) Kim 	 * LP5521_REG_ENABLE register will not have any effect - strange!
25194482174SMilo(Woogyom) Kim 	 */
252ffbdccdbSMilo(Woogyom) Kim 	ret = lp55xx_read(chip, LP5521_REG_R_CURRENT, &val);
25394482174SMilo(Woogyom) Kim 	if (ret) {
254ffbdccdbSMilo(Woogyom) Kim 		dev_err(&chip->cl->dev, "error in resetting chip\n");
25594482174SMilo(Woogyom) Kim 		return ret;
25694482174SMilo(Woogyom) Kim 	}
25794482174SMilo(Woogyom) Kim 	if (val != LP5521_REG_R_CURR_DEFAULT) {
258ffbdccdbSMilo(Woogyom) Kim 		dev_err(&chip->cl->dev,
25994482174SMilo(Woogyom) Kim 			"unexpected data in register (expected 0x%x got 0x%x)\n",
26094482174SMilo(Woogyom) Kim 			LP5521_REG_R_CURR_DEFAULT, val);
26194482174SMilo(Woogyom) Kim 		ret = -EINVAL;
26294482174SMilo(Woogyom) Kim 		return ret;
26394482174SMilo(Woogyom) Kim 	}
26494482174SMilo(Woogyom) Kim 	usleep_range(10000, 20000);
265500fe141SSamu Onkalo 
266500fe141SSamu Onkalo 	/* Set all PWMs to direct control mode */
267ffbdccdbSMilo(Woogyom) Kim 	ret = lp55xx_write(chip, LP5521_REG_OP_MODE, LP5521_CMD_DIRECT);
268500fe141SSamu Onkalo 
269ffbdccdbSMilo(Woogyom) Kim 	val = chip->pdata->update_config ?
2703b49aacdSKim, Milo 		: (LP5521_PWRSAVE_EN | LP5521_CP_MODE_AUTO | LP5521_R_TO_BATT);
271ffbdccdbSMilo(Woogyom) Kim 	ret = lp55xx_write(chip, LP5521_REG_CONFIG, val);
27294482174SMilo(Woogyom) Kim 	if (ret)
27394482174SMilo(Woogyom) Kim 		return ret;
274500fe141SSamu Onkalo 
275500fe141SSamu Onkalo 	/* Initialize all channels PWM to zero -> leds off */
276ffbdccdbSMilo(Woogyom) Kim 	lp55xx_write(chip, LP5521_REG_R_PWM, 0);
277ffbdccdbSMilo(Woogyom) Kim 	lp55xx_write(chip, LP5521_REG_G_PWM, 0);
278ffbdccdbSMilo(Woogyom) Kim 	lp55xx_write(chip, LP5521_REG_B_PWM, 0);
279500fe141SSamu Onkalo 
280500fe141SSamu Onkalo 	/* Set engines are set to run state when OP_MODE enables engines */
281ffbdccdbSMilo(Woogyom) Kim 	ret = lp55xx_write(chip, LP5521_REG_ENABLE, LP5521_ENABLE_RUN_PROGRAM);
28294482174SMilo(Woogyom) Kim 	if (ret)
283500fe141SSamu Onkalo 		return ret;
28494482174SMilo(Woogyom) Kim 
28594482174SMilo(Woogyom) Kim 	lp5521_wait_enable_done();
28694482174SMilo(Woogyom) Kim 
28794482174SMilo(Woogyom) Kim 	return 0;
288500fe141SSamu Onkalo }
289500fe141SSamu Onkalo 
290500fe141SSamu Onkalo static int lp5521_run_selftest(struct lp5521_chip *chip, char *buf)
291500fe141SSamu Onkalo {
292500fe141SSamu Onkalo 	int ret;
293500fe141SSamu Onkalo 	u8 status;
294500fe141SSamu Onkalo 
295500fe141SSamu Onkalo 	ret = lp5521_read(chip->client, LP5521_REG_STATUS, &status);
296500fe141SSamu Onkalo 	if (ret < 0)
297500fe141SSamu Onkalo 		return ret;
298500fe141SSamu Onkalo 
299500fe141SSamu Onkalo 	/* Check that ext clock is really in use if requested */
300500fe141SSamu Onkalo 	if (chip->pdata && chip->pdata->clock_mode == LP5521_CLOCK_EXT)
301500fe141SSamu Onkalo 		if  ((status & LP5521_EXT_CLK_USED) == 0)
302500fe141SSamu Onkalo 			return -EIO;
303500fe141SSamu Onkalo 	return 0;
304500fe141SSamu Onkalo }
305500fe141SSamu Onkalo 
306500fe141SSamu Onkalo static void lp5521_set_brightness(struct led_classdev *cdev,
307500fe141SSamu Onkalo 			     enum led_brightness brightness)
308500fe141SSamu Onkalo {
309500fe141SSamu Onkalo 	struct lp5521_led *led = cdev_to_led(cdev);
310500fe141SSamu Onkalo 	led->brightness = (u8)brightness;
311500fe141SSamu Onkalo 	schedule_work(&led->brightness_work);
312500fe141SSamu Onkalo }
313500fe141SSamu Onkalo 
314500fe141SSamu Onkalo static void lp5521_led_brightness_work(struct work_struct *work)
315500fe141SSamu Onkalo {
316500fe141SSamu Onkalo 	struct lp5521_led *led = container_of(work,
317500fe141SSamu Onkalo 					      struct lp5521_led,
318500fe141SSamu Onkalo 					      brightness_work);
319500fe141SSamu Onkalo 	struct lp5521_chip *chip = led_to_lp5521(led);
320500fe141SSamu Onkalo 	struct i2c_client *client = chip->client;
321500fe141SSamu Onkalo 
322500fe141SSamu Onkalo 	mutex_lock(&chip->lock);
323500fe141SSamu Onkalo 	lp5521_write(client, LP5521_REG_LED_PWM_BASE + led->chan_nr,
324500fe141SSamu Onkalo 		led->brightness);
325500fe141SSamu Onkalo 	mutex_unlock(&chip->lock);
326500fe141SSamu Onkalo }
327500fe141SSamu Onkalo 
328500fe141SSamu Onkalo /* Set engine mode and create appropriate sysfs attributes, if required. */
329500fe141SSamu Onkalo static int lp5521_set_mode(struct lp5521_engine *engine, u8 mode)
330500fe141SSamu Onkalo {
331500fe141SSamu Onkalo 	int ret = 0;
332500fe141SSamu Onkalo 
333500fe141SSamu Onkalo 	/* if in that mode already do nothing, except for run */
334500fe141SSamu Onkalo 	if (mode == engine->mode && mode != LP5521_CMD_RUN)
335500fe141SSamu Onkalo 		return 0;
336500fe141SSamu Onkalo 
337500fe141SSamu Onkalo 	if (mode == LP5521_CMD_RUN) {
338500fe141SSamu Onkalo 		ret = lp5521_set_engine_mode(engine, LP5521_CMD_RUN);
339500fe141SSamu Onkalo 	} else if (mode == LP5521_CMD_LOAD) {
340500fe141SSamu Onkalo 		lp5521_set_engine_mode(engine, LP5521_CMD_DISABLED);
341500fe141SSamu Onkalo 		lp5521_set_engine_mode(engine, LP5521_CMD_LOAD);
342500fe141SSamu Onkalo 	} else if (mode == LP5521_CMD_DISABLED) {
343500fe141SSamu Onkalo 		lp5521_set_engine_mode(engine, LP5521_CMD_DISABLED);
344500fe141SSamu Onkalo 	}
345500fe141SSamu Onkalo 
346500fe141SSamu Onkalo 	engine->mode = mode;
347500fe141SSamu Onkalo 
348500fe141SSamu Onkalo 	return ret;
349500fe141SSamu Onkalo }
350500fe141SSamu Onkalo 
351500fe141SSamu Onkalo static int lp5521_do_store_load(struct lp5521_engine *engine,
352500fe141SSamu Onkalo 				const char *buf, size_t len)
353500fe141SSamu Onkalo {
354500fe141SSamu Onkalo 	struct lp5521_chip *chip = engine_to_lp5521(engine);
355500fe141SSamu Onkalo 	struct i2c_client *client = chip->client;
356500fe141SSamu Onkalo 	int  ret, nrchars, offset = 0, i = 0;
357500fe141SSamu Onkalo 	char c[3];
358500fe141SSamu Onkalo 	unsigned cmd;
359500fe141SSamu Onkalo 	u8 pattern[LP5521_PROGRAM_LENGTH] = {0};
360500fe141SSamu Onkalo 
361500fe141SSamu Onkalo 	while ((offset < len - 1) && (i < LP5521_PROGRAM_LENGTH)) {
362500fe141SSamu Onkalo 		/* separate sscanfs because length is working only for %s */
363500fe141SSamu Onkalo 		ret = sscanf(buf + offset, "%2s%n ", c, &nrchars);
3642260209cSVasiliy Kulikov 		if (ret != 2)
3652260209cSVasiliy Kulikov 			goto fail;
366500fe141SSamu Onkalo 		ret = sscanf(c, "%2x", &cmd);
367500fe141SSamu Onkalo 		if (ret != 1)
368500fe141SSamu Onkalo 			goto fail;
369500fe141SSamu Onkalo 		pattern[i] = (u8)cmd;
370500fe141SSamu Onkalo 
371500fe141SSamu Onkalo 		offset += nrchars;
372500fe141SSamu Onkalo 		i++;
373500fe141SSamu Onkalo 	}
374500fe141SSamu Onkalo 
375500fe141SSamu Onkalo 	/* Each instruction is 16bit long. Check that length is even */
376500fe141SSamu Onkalo 	if (i % 2)
377500fe141SSamu Onkalo 		goto fail;
378500fe141SSamu Onkalo 
379500fe141SSamu Onkalo 	mutex_lock(&chip->lock);
380d4e7ad03SSamu Onkalo 	if (engine->mode == LP5521_CMD_LOAD)
381500fe141SSamu Onkalo 		ret = lp5521_load_program(engine, pattern);
382d4e7ad03SSamu Onkalo 	else
383d4e7ad03SSamu Onkalo 		ret = -EINVAL;
384500fe141SSamu Onkalo 	mutex_unlock(&chip->lock);
385500fe141SSamu Onkalo 
386500fe141SSamu Onkalo 	if (ret) {
387500fe141SSamu Onkalo 		dev_err(&client->dev, "failed loading pattern\n");
388500fe141SSamu Onkalo 		return ret;
389500fe141SSamu Onkalo 	}
390500fe141SSamu Onkalo 
391500fe141SSamu Onkalo 	return len;
392500fe141SSamu Onkalo fail:
393500fe141SSamu Onkalo 	dev_err(&client->dev, "wrong pattern format\n");
394500fe141SSamu Onkalo 	return -EINVAL;
395500fe141SSamu Onkalo }
396500fe141SSamu Onkalo 
397500fe141SSamu Onkalo static ssize_t store_engine_load(struct device *dev,
398500fe141SSamu Onkalo 				     struct device_attribute *attr,
399500fe141SSamu Onkalo 				     const char *buf, size_t len, int nr)
400500fe141SSamu Onkalo {
401500fe141SSamu Onkalo 	struct i2c_client *client = to_i2c_client(dev);
402500fe141SSamu Onkalo 	struct lp5521_chip *chip = i2c_get_clientdata(client);
403500fe141SSamu Onkalo 	return lp5521_do_store_load(&chip->engines[nr - 1], buf, len);
404500fe141SSamu Onkalo }
405500fe141SSamu Onkalo 
406500fe141SSamu Onkalo #define store_load(nr)							\
407500fe141SSamu Onkalo static ssize_t store_engine##nr##_load(struct device *dev,		\
408500fe141SSamu Onkalo 				     struct device_attribute *attr,	\
409500fe141SSamu Onkalo 				     const char *buf, size_t len)	\
410500fe141SSamu Onkalo {									\
411500fe141SSamu Onkalo 	return store_engine_load(dev, attr, buf, len, nr);		\
412500fe141SSamu Onkalo }
413500fe141SSamu Onkalo store_load(1)
414500fe141SSamu Onkalo store_load(2)
415500fe141SSamu Onkalo store_load(3)
416500fe141SSamu Onkalo 
417500fe141SSamu Onkalo static ssize_t show_engine_mode(struct device *dev,
418500fe141SSamu Onkalo 				struct device_attribute *attr,
419500fe141SSamu Onkalo 				char *buf, int nr)
420500fe141SSamu Onkalo {
421500fe141SSamu Onkalo 	struct i2c_client *client = to_i2c_client(dev);
422500fe141SSamu Onkalo 	struct lp5521_chip *chip = i2c_get_clientdata(client);
423500fe141SSamu Onkalo 	switch (chip->engines[nr - 1].mode) {
424500fe141SSamu Onkalo 	case LP5521_CMD_RUN:
425500fe141SSamu Onkalo 		return sprintf(buf, "run\n");
426500fe141SSamu Onkalo 	case LP5521_CMD_LOAD:
427500fe141SSamu Onkalo 		return sprintf(buf, "load\n");
428500fe141SSamu Onkalo 	case LP5521_CMD_DISABLED:
429500fe141SSamu Onkalo 		return sprintf(buf, "disabled\n");
430500fe141SSamu Onkalo 	default:
431500fe141SSamu Onkalo 		return sprintf(buf, "disabled\n");
432500fe141SSamu Onkalo 	}
433500fe141SSamu Onkalo }
434500fe141SSamu Onkalo 
435500fe141SSamu Onkalo #define show_mode(nr)							\
436500fe141SSamu Onkalo static ssize_t show_engine##nr##_mode(struct device *dev,		\
437500fe141SSamu Onkalo 				    struct device_attribute *attr,	\
438500fe141SSamu Onkalo 				    char *buf)				\
439500fe141SSamu Onkalo {									\
440500fe141SSamu Onkalo 	return show_engine_mode(dev, attr, buf, nr);			\
441500fe141SSamu Onkalo }
442500fe141SSamu Onkalo show_mode(1)
443500fe141SSamu Onkalo show_mode(2)
444500fe141SSamu Onkalo show_mode(3)
445500fe141SSamu Onkalo 
446500fe141SSamu Onkalo static ssize_t store_engine_mode(struct device *dev,
447500fe141SSamu Onkalo 				 struct device_attribute *attr,
448500fe141SSamu Onkalo 				 const char *buf, size_t len, int nr)
449500fe141SSamu Onkalo {
450500fe141SSamu Onkalo 	struct i2c_client *client = to_i2c_client(dev);
451500fe141SSamu Onkalo 	struct lp5521_chip *chip = i2c_get_clientdata(client);
452500fe141SSamu Onkalo 	struct lp5521_engine *engine = &chip->engines[nr - 1];
453500fe141SSamu Onkalo 	mutex_lock(&chip->lock);
454500fe141SSamu Onkalo 
455500fe141SSamu Onkalo 	if (!strncmp(buf, "run", 3))
456500fe141SSamu Onkalo 		lp5521_set_mode(engine, LP5521_CMD_RUN);
457500fe141SSamu Onkalo 	else if (!strncmp(buf, "load", 4))
458500fe141SSamu Onkalo 		lp5521_set_mode(engine, LP5521_CMD_LOAD);
459500fe141SSamu Onkalo 	else if (!strncmp(buf, "disabled", 8))
460500fe141SSamu Onkalo 		lp5521_set_mode(engine, LP5521_CMD_DISABLED);
461500fe141SSamu Onkalo 
462500fe141SSamu Onkalo 	mutex_unlock(&chip->lock);
463500fe141SSamu Onkalo 	return len;
464500fe141SSamu Onkalo }
465500fe141SSamu Onkalo 
466500fe141SSamu Onkalo #define store_mode(nr)							\
467500fe141SSamu Onkalo static ssize_t store_engine##nr##_mode(struct device *dev,		\
468500fe141SSamu Onkalo 				     struct device_attribute *attr,	\
469500fe141SSamu Onkalo 				     const char *buf, size_t len)	\
470500fe141SSamu Onkalo {									\
471500fe141SSamu Onkalo 	return store_engine_mode(dev, attr, buf, len, nr);		\
472500fe141SSamu Onkalo }
473500fe141SSamu Onkalo store_mode(1)
474500fe141SSamu Onkalo store_mode(2)
475500fe141SSamu Onkalo store_mode(3)
476500fe141SSamu Onkalo 
477500fe141SSamu Onkalo static ssize_t show_max_current(struct device *dev,
478500fe141SSamu Onkalo 			    struct device_attribute *attr,
479500fe141SSamu Onkalo 			    char *buf)
480500fe141SSamu Onkalo {
481500fe141SSamu Onkalo 	struct led_classdev *led_cdev = dev_get_drvdata(dev);
482500fe141SSamu Onkalo 	struct lp5521_led *led = cdev_to_led(led_cdev);
483500fe141SSamu Onkalo 
484500fe141SSamu Onkalo 	return sprintf(buf, "%d\n", led->max_current);
485500fe141SSamu Onkalo }
486500fe141SSamu Onkalo 
487500fe141SSamu Onkalo static ssize_t show_current(struct device *dev,
488500fe141SSamu Onkalo 			    struct device_attribute *attr,
489500fe141SSamu Onkalo 			    char *buf)
490500fe141SSamu Onkalo {
491500fe141SSamu Onkalo 	struct led_classdev *led_cdev = dev_get_drvdata(dev);
492500fe141SSamu Onkalo 	struct lp5521_led *led = cdev_to_led(led_cdev);
493500fe141SSamu Onkalo 
494500fe141SSamu Onkalo 	return sprintf(buf, "%d\n", led->led_current);
495500fe141SSamu Onkalo }
496500fe141SSamu Onkalo 
497500fe141SSamu Onkalo static ssize_t store_current(struct device *dev,
498500fe141SSamu Onkalo 			     struct device_attribute *attr,
499500fe141SSamu Onkalo 			     const char *buf, size_t len)
500500fe141SSamu Onkalo {
501500fe141SSamu Onkalo 	struct led_classdev *led_cdev = dev_get_drvdata(dev);
502500fe141SSamu Onkalo 	struct lp5521_led *led = cdev_to_led(led_cdev);
503500fe141SSamu Onkalo 	struct lp5521_chip *chip = led_to_lp5521(led);
504500fe141SSamu Onkalo 	ssize_t ret;
505500fe141SSamu Onkalo 	unsigned long curr;
506500fe141SSamu Onkalo 
507011af7bcSKim, Milo 	if (kstrtoul(buf, 0, &curr))
508500fe141SSamu Onkalo 		return -EINVAL;
509500fe141SSamu Onkalo 
510500fe141SSamu Onkalo 	if (curr > led->max_current)
511500fe141SSamu Onkalo 		return -EINVAL;
512500fe141SSamu Onkalo 
513500fe141SSamu Onkalo 	mutex_lock(&chip->lock);
514500fe141SSamu Onkalo 	ret = lp5521_set_led_current(chip, led->id, curr);
515500fe141SSamu Onkalo 	mutex_unlock(&chip->lock);
516500fe141SSamu Onkalo 
517500fe141SSamu Onkalo 	if (ret < 0)
518500fe141SSamu Onkalo 		return ret;
519500fe141SSamu Onkalo 
520500fe141SSamu Onkalo 	led->led_current = (u8)curr;
521500fe141SSamu Onkalo 
522500fe141SSamu Onkalo 	return len;
523500fe141SSamu Onkalo }
524500fe141SSamu Onkalo 
525500fe141SSamu Onkalo static ssize_t lp5521_selftest(struct device *dev,
526500fe141SSamu Onkalo 			       struct device_attribute *attr,
527500fe141SSamu Onkalo 			       char *buf)
528500fe141SSamu Onkalo {
529500fe141SSamu Onkalo 	struct i2c_client *client = to_i2c_client(dev);
530500fe141SSamu Onkalo 	struct lp5521_chip *chip = i2c_get_clientdata(client);
531500fe141SSamu Onkalo 	int ret;
532500fe141SSamu Onkalo 
533500fe141SSamu Onkalo 	mutex_lock(&chip->lock);
534500fe141SSamu Onkalo 	ret = lp5521_run_selftest(chip, buf);
535500fe141SSamu Onkalo 	mutex_unlock(&chip->lock);
536500fe141SSamu Onkalo 	return sprintf(buf, "%s\n", ret ? "FAIL" : "OK");
537500fe141SSamu Onkalo }
538500fe141SSamu Onkalo 
539011af7bcSKim, Milo static void lp5521_clear_program_memory(struct i2c_client *cl)
540011af7bcSKim, Milo {
541011af7bcSKim, Milo 	int i;
542011af7bcSKim, Milo 	u8 rgb_mem[] = {
543011af7bcSKim, Milo 		LP5521_REG_R_PROG_MEM,
544011af7bcSKim, Milo 		LP5521_REG_G_PROG_MEM,
545011af7bcSKim, Milo 		LP5521_REG_B_PROG_MEM,
546011af7bcSKim, Milo 	};
547011af7bcSKim, Milo 
548011af7bcSKim, Milo 	for (i = 0; i < ARRAY_SIZE(rgb_mem); i++) {
549011af7bcSKim, Milo 		lp5521_write(cl, rgb_mem[i], 0);
550011af7bcSKim, Milo 		lp5521_write(cl, rgb_mem[i] + 1, 0);
551011af7bcSKim, Milo 	}
552011af7bcSKim, Milo }
553011af7bcSKim, Milo 
554011af7bcSKim, Milo static void lp5521_write_program_memory(struct i2c_client *cl,
555011af7bcSKim, Milo 				u8 base, u8 *rgb, int size)
556011af7bcSKim, Milo {
557011af7bcSKim, Milo 	int i;
558011af7bcSKim, Milo 
559011af7bcSKim, Milo 	if (!rgb || size <= 0)
560011af7bcSKim, Milo 		return;
561011af7bcSKim, Milo 
562011af7bcSKim, Milo 	for (i = 0; i < size; i++)
563011af7bcSKim, Milo 		lp5521_write(cl, base + i, *(rgb + i));
564011af7bcSKim, Milo 
565011af7bcSKim, Milo 	lp5521_write(cl, base + i, 0);
566011af7bcSKim, Milo 	lp5521_write(cl, base + i + 1, 0);
567011af7bcSKim, Milo }
568011af7bcSKim, Milo 
569011af7bcSKim, Milo static inline struct lp5521_led_pattern *lp5521_get_pattern
570011af7bcSKim, Milo 					(struct lp5521_chip *chip, u8 offset)
571011af7bcSKim, Milo {
572011af7bcSKim, Milo 	struct lp5521_led_pattern *ptn;
573011af7bcSKim, Milo 	ptn = chip->pdata->patterns + (offset - 1);
574011af7bcSKim, Milo 	return ptn;
575011af7bcSKim, Milo }
576011af7bcSKim, Milo 
577011af7bcSKim, Milo static void lp5521_run_led_pattern(int mode, struct lp5521_chip *chip)
578011af7bcSKim, Milo {
579011af7bcSKim, Milo 	struct lp5521_led_pattern *ptn;
580011af7bcSKim, Milo 	struct i2c_client *cl = chip->client;
581011af7bcSKim, Milo 	int num_patterns = chip->pdata->num_patterns;
582011af7bcSKim, Milo 
583011af7bcSKim, Milo 	if (mode > num_patterns || !(chip->pdata->patterns))
584011af7bcSKim, Milo 		return;
585011af7bcSKim, Milo 
586011af7bcSKim, Milo 	if (mode == PATTERN_OFF) {
58732a2f747SKim, Milo 		lp5521_write(cl, LP5521_REG_ENABLE, LP5521_ENABLE_DEFAULT);
588011af7bcSKim, Milo 		usleep_range(1000, 2000);
589011af7bcSKim, Milo 		lp5521_write(cl, LP5521_REG_OP_MODE, LP5521_CMD_DIRECT);
590011af7bcSKim, Milo 	} else {
591011af7bcSKim, Milo 		ptn = lp5521_get_pattern(chip, mode);
592011af7bcSKim, Milo 		if (!ptn)
593011af7bcSKim, Milo 			return;
594011af7bcSKim, Milo 
595011af7bcSKim, Milo 		lp5521_write(cl, LP5521_REG_OP_MODE, LP5521_CMD_LOAD);
596011af7bcSKim, Milo 		usleep_range(1000, 2000);
597011af7bcSKim, Milo 
598011af7bcSKim, Milo 		lp5521_clear_program_memory(cl);
599011af7bcSKim, Milo 
600011af7bcSKim, Milo 		lp5521_write_program_memory(cl, LP5521_REG_R_PROG_MEM,
601011af7bcSKim, Milo 					ptn->r, ptn->size_r);
602011af7bcSKim, Milo 		lp5521_write_program_memory(cl, LP5521_REG_G_PROG_MEM,
603011af7bcSKim, Milo 					ptn->g, ptn->size_g);
604011af7bcSKim, Milo 		lp5521_write_program_memory(cl, LP5521_REG_B_PROG_MEM,
605011af7bcSKim, Milo 					ptn->b, ptn->size_b);
606011af7bcSKim, Milo 
607011af7bcSKim, Milo 		lp5521_write(cl, LP5521_REG_OP_MODE, LP5521_CMD_RUN);
608011af7bcSKim, Milo 		usleep_range(1000, 2000);
60932a2f747SKim, Milo 		lp5521_write(cl, LP5521_REG_ENABLE, LP5521_ENABLE_RUN_PROGRAM);
610011af7bcSKim, Milo 	}
611011af7bcSKim, Milo }
612011af7bcSKim, Milo 
613011af7bcSKim, Milo static ssize_t store_led_pattern(struct device *dev,
614011af7bcSKim, Milo 				struct device_attribute *attr,
615011af7bcSKim, Milo 				const char *buf, size_t len)
616011af7bcSKim, Milo {
617011af7bcSKim, Milo 	struct lp5521_chip *chip = i2c_get_clientdata(to_i2c_client(dev));
618011af7bcSKim, Milo 	unsigned long val;
619011af7bcSKim, Milo 	int ret;
620011af7bcSKim, Milo 
62169b44c16SJingoo Han 	ret = kstrtoul(buf, 16, &val);
622011af7bcSKim, Milo 	if (ret)
623011af7bcSKim, Milo 		return ret;
624011af7bcSKim, Milo 
625011af7bcSKim, Milo 	lp5521_run_led_pattern(val, chip);
626011af7bcSKim, Milo 
627011af7bcSKim, Milo 	return len;
628011af7bcSKim, Milo }
629011af7bcSKim, Milo 
630500fe141SSamu Onkalo /* led class device attributes */
63167d1da79SVasiliy Kulikov static DEVICE_ATTR(led_current, S_IRUGO | S_IWUSR, show_current, store_current);
632500fe141SSamu Onkalo static DEVICE_ATTR(max_current, S_IRUGO , show_max_current, NULL);
633500fe141SSamu Onkalo 
634500fe141SSamu Onkalo static struct attribute *lp5521_led_attributes[] = {
635500fe141SSamu Onkalo 	&dev_attr_led_current.attr,
636500fe141SSamu Onkalo 	&dev_attr_max_current.attr,
637500fe141SSamu Onkalo 	NULL,
638500fe141SSamu Onkalo };
639500fe141SSamu Onkalo 
640500fe141SSamu Onkalo static struct attribute_group lp5521_led_attribute_group = {
641500fe141SSamu Onkalo 	.attrs = lp5521_led_attributes
642500fe141SSamu Onkalo };
643500fe141SSamu Onkalo 
644500fe141SSamu Onkalo /* device attributes */
64567d1da79SVasiliy Kulikov static DEVICE_ATTR(engine1_mode, S_IRUGO | S_IWUSR,
646500fe141SSamu Onkalo 		   show_engine1_mode, store_engine1_mode);
64767d1da79SVasiliy Kulikov static DEVICE_ATTR(engine2_mode, S_IRUGO | S_IWUSR,
648500fe141SSamu Onkalo 		   show_engine2_mode, store_engine2_mode);
64967d1da79SVasiliy Kulikov static DEVICE_ATTR(engine3_mode, S_IRUGO | S_IWUSR,
650500fe141SSamu Onkalo 		   show_engine3_mode, store_engine3_mode);
65167d1da79SVasiliy Kulikov static DEVICE_ATTR(engine1_load, S_IWUSR, NULL, store_engine1_load);
65267d1da79SVasiliy Kulikov static DEVICE_ATTR(engine2_load, S_IWUSR, NULL, store_engine2_load);
65367d1da79SVasiliy Kulikov static DEVICE_ATTR(engine3_load, S_IWUSR, NULL, store_engine3_load);
654500fe141SSamu Onkalo static DEVICE_ATTR(selftest, S_IRUGO, lp5521_selftest, NULL);
655011af7bcSKim, Milo static DEVICE_ATTR(led_pattern, S_IWUSR, NULL, store_led_pattern);
656500fe141SSamu Onkalo 
657500fe141SSamu Onkalo static struct attribute *lp5521_attributes[] = {
658500fe141SSamu Onkalo 	&dev_attr_engine1_mode.attr,
659500fe141SSamu Onkalo 	&dev_attr_engine2_mode.attr,
660500fe141SSamu Onkalo 	&dev_attr_engine3_mode.attr,
661500fe141SSamu Onkalo 	&dev_attr_selftest.attr,
662500fe141SSamu Onkalo 	&dev_attr_engine1_load.attr,
663500fe141SSamu Onkalo 	&dev_attr_engine2_load.attr,
664500fe141SSamu Onkalo 	&dev_attr_engine3_load.attr,
665011af7bcSKim, Milo 	&dev_attr_led_pattern.attr,
666500fe141SSamu Onkalo 	NULL
667500fe141SSamu Onkalo };
668500fe141SSamu Onkalo 
669500fe141SSamu Onkalo static const struct attribute_group lp5521_group = {
670500fe141SSamu Onkalo 	.attrs = lp5521_attributes,
671500fe141SSamu Onkalo };
672500fe141SSamu Onkalo 
673500fe141SSamu Onkalo static int lp5521_register_sysfs(struct i2c_client *client)
674500fe141SSamu Onkalo {
675500fe141SSamu Onkalo 	struct device *dev = &client->dev;
676500fe141SSamu Onkalo 	return sysfs_create_group(&dev->kobj, &lp5521_group);
677500fe141SSamu Onkalo }
678500fe141SSamu Onkalo 
679500fe141SSamu Onkalo static void lp5521_unregister_sysfs(struct i2c_client *client)
680500fe141SSamu Onkalo {
681500fe141SSamu Onkalo 	struct lp5521_chip *chip = i2c_get_clientdata(client);
682500fe141SSamu Onkalo 	struct device *dev = &client->dev;
683500fe141SSamu Onkalo 	int i;
684500fe141SSamu Onkalo 
685500fe141SSamu Onkalo 	sysfs_remove_group(&dev->kobj, &lp5521_group);
686500fe141SSamu Onkalo 
687500fe141SSamu Onkalo 	for (i = 0; i < chip->num_leds; i++)
688500fe141SSamu Onkalo 		sysfs_remove_group(&chip->leds[i].cdev.dev->kobj,
689500fe141SSamu Onkalo 				&lp5521_led_attribute_group);
690500fe141SSamu Onkalo }
691500fe141SSamu Onkalo 
69298ea1ea2SBill Pemberton static int lp5521_init_led(struct lp5521_led *led,
693500fe141SSamu Onkalo 				struct i2c_client *client,
694500fe141SSamu Onkalo 				int chan, struct lp5521_platform_data *pdata)
695500fe141SSamu Onkalo {
696500fe141SSamu Onkalo 	struct device *dev = &client->dev;
697500fe141SSamu Onkalo 	char name[32];
698500fe141SSamu Onkalo 	int res;
699500fe141SSamu Onkalo 
700500fe141SSamu Onkalo 	if (chan >= LP5521_MAX_LEDS)
701500fe141SSamu Onkalo 		return -EINVAL;
702500fe141SSamu Onkalo 
703500fe141SSamu Onkalo 	if (pdata->led_config[chan].led_current == 0)
704500fe141SSamu Onkalo 		return 0;
705500fe141SSamu Onkalo 
706500fe141SSamu Onkalo 	led->led_current = pdata->led_config[chan].led_current;
707500fe141SSamu Onkalo 	led->max_current = pdata->led_config[chan].max_current;
708500fe141SSamu Onkalo 	led->chan_nr = pdata->led_config[chan].chan_nr;
709500fe141SSamu Onkalo 
710500fe141SSamu Onkalo 	if (led->chan_nr >= LP5521_MAX_LEDS) {
711500fe141SSamu Onkalo 		dev_err(dev, "Use channel numbers between 0 and %d\n",
712500fe141SSamu Onkalo 			LP5521_MAX_LEDS - 1);
713500fe141SSamu Onkalo 		return -EINVAL;
714500fe141SSamu Onkalo 	}
715500fe141SSamu Onkalo 
7165ae4e8a7SKim, Milo 	led->cdev.brightness_set = lp5521_set_brightness;
7175ae4e8a7SKim, Milo 	if (pdata->led_config[chan].name) {
7185ae4e8a7SKim, Milo 		led->cdev.name = pdata->led_config[chan].name;
7195ae4e8a7SKim, Milo 	} else {
72061a83932SArun Murthy 		snprintf(name, sizeof(name), "%s:channel%d",
72161a83932SArun Murthy 			pdata->label ?: client->name, chan);
722500fe141SSamu Onkalo 		led->cdev.name = name;
7235ae4e8a7SKim, Milo 	}
7245ae4e8a7SKim, Milo 
725500fe141SSamu Onkalo 	res = led_classdev_register(dev, &led->cdev);
726500fe141SSamu Onkalo 	if (res < 0) {
727500fe141SSamu Onkalo 		dev_err(dev, "couldn't register led on channel %d\n", chan);
728500fe141SSamu Onkalo 		return res;
729500fe141SSamu Onkalo 	}
730500fe141SSamu Onkalo 
731500fe141SSamu Onkalo 	res = sysfs_create_group(&led->cdev.dev->kobj,
732500fe141SSamu Onkalo 			&lp5521_led_attribute_group);
733500fe141SSamu Onkalo 	if (res < 0) {
734500fe141SSamu Onkalo 		dev_err(dev, "couldn't register current attribute\n");
735500fe141SSamu Onkalo 		led_classdev_unregister(&led->cdev);
736500fe141SSamu Onkalo 		return res;
737500fe141SSamu Onkalo 	}
738500fe141SSamu Onkalo 	return 0;
739500fe141SSamu Onkalo }
740500fe141SSamu Onkalo 
741f6524808SMilo(Woogyom) Kim static int lp5521_register_leds(struct lp5521_chip *chip)
742f6524808SMilo(Woogyom) Kim {
743f6524808SMilo(Woogyom) Kim 	struct lp5521_platform_data *pdata = chip->pdata;
744f6524808SMilo(Woogyom) Kim 	struct i2c_client *client = chip->client;
745f6524808SMilo(Woogyom) Kim 	int i;
746f6524808SMilo(Woogyom) Kim 	int led;
747f6524808SMilo(Woogyom) Kim 	int ret;
748f6524808SMilo(Woogyom) Kim 
749f6524808SMilo(Woogyom) Kim 	/* Initialize leds */
750f6524808SMilo(Woogyom) Kim 	chip->num_channels = pdata->num_channels;
751f6524808SMilo(Woogyom) Kim 	chip->num_leds = 0;
752f6524808SMilo(Woogyom) Kim 	led = 0;
753f6524808SMilo(Woogyom) Kim 	for (i = 0; i < pdata->num_channels; i++) {
754f6524808SMilo(Woogyom) Kim 		/* Do not initialize channels that are not connected */
755f6524808SMilo(Woogyom) Kim 		if (pdata->led_config[i].led_current == 0)
756f6524808SMilo(Woogyom) Kim 			continue;
757f6524808SMilo(Woogyom) Kim 
758f6524808SMilo(Woogyom) Kim 		ret = lp5521_init_led(&chip->leds[led], client, i, pdata);
759f6524808SMilo(Woogyom) Kim 		if (ret) {
760f6524808SMilo(Woogyom) Kim 			dev_err(&client->dev, "error initializing leds\n");
761f6524808SMilo(Woogyom) Kim 			return ret;
762f6524808SMilo(Woogyom) Kim 		}
763f6524808SMilo(Woogyom) Kim 		chip->num_leds++;
764f6524808SMilo(Woogyom) Kim 
765f6524808SMilo(Woogyom) Kim 		chip->leds[led].id = led;
766f6524808SMilo(Woogyom) Kim 		/* Set initial LED current */
767f6524808SMilo(Woogyom) Kim 		lp5521_set_led_current(chip, led,
768f6524808SMilo(Woogyom) Kim 				chip->leds[led].led_current);
769f6524808SMilo(Woogyom) Kim 
770f6524808SMilo(Woogyom) Kim 		INIT_WORK(&(chip->leds[led].brightness_work),
771f6524808SMilo(Woogyom) Kim 			lp5521_led_brightness_work);
772f6524808SMilo(Woogyom) Kim 
773f6524808SMilo(Woogyom) Kim 		led++;
774f6524808SMilo(Woogyom) Kim 	}
775f6524808SMilo(Woogyom) Kim 
776f6524808SMilo(Woogyom) Kim 	return 0;
777f6524808SMilo(Woogyom) Kim }
778f6524808SMilo(Woogyom) Kim 
7791904f83dSMilo(Woogyom) Kim static void lp5521_unregister_leds(struct lp5521_chip *chip)
7801904f83dSMilo(Woogyom) Kim {
7811904f83dSMilo(Woogyom) Kim 	int i;
7821904f83dSMilo(Woogyom) Kim 
7831904f83dSMilo(Woogyom) Kim 	for (i = 0; i < chip->num_leds; i++) {
7841904f83dSMilo(Woogyom) Kim 		led_classdev_unregister(&chip->leds[i].cdev);
7851904f83dSMilo(Woogyom) Kim 		cancel_work_sync(&chip->leds[i].brightness_work);
7861904f83dSMilo(Woogyom) Kim 	}
7871904f83dSMilo(Woogyom) Kim }
7881904f83dSMilo(Woogyom) Kim 
78948068d5dSMilo(Woogyom) Kim /* Chip specific configurations */
79048068d5dSMilo(Woogyom) Kim static struct lp55xx_device_config lp5521_cfg = {
79148068d5dSMilo(Woogyom) Kim 	.reset = {
79248068d5dSMilo(Woogyom) Kim 		.addr = LP5521_REG_RESET,
79348068d5dSMilo(Woogyom) Kim 		.val  = LP5521_RESET,
79448068d5dSMilo(Woogyom) Kim 	},
795e3a700d8SMilo(Woogyom) Kim 	.enable = {
796e3a700d8SMilo(Woogyom) Kim 		.addr = LP5521_REG_ENABLE,
797e3a700d8SMilo(Woogyom) Kim 		.val  = LP5521_ENABLE_DEFAULT,
798e3a700d8SMilo(Woogyom) Kim 	},
799ffbdccdbSMilo(Woogyom) Kim 	.post_init_device   = lp5521_post_init_device,
80048068d5dSMilo(Woogyom) Kim };
80148068d5dSMilo(Woogyom) Kim 
80298ea1ea2SBill Pemberton static int lp5521_probe(struct i2c_client *client,
803500fe141SSamu Onkalo 			const struct i2c_device_id *id)
804500fe141SSamu Onkalo {
8056a0c9a47SMilo(Woogyom) Kim 	struct lp5521_chip		*old_chip = NULL;
8061904f83dSMilo(Woogyom) Kim 	int ret;
8076a0c9a47SMilo(Woogyom) Kim 	struct lp55xx_chip *chip;
8086a0c9a47SMilo(Woogyom) Kim 	struct lp55xx_led *led;
8096a0c9a47SMilo(Woogyom) Kim 	struct lp55xx_platform_data *pdata = client->dev.platform_data;
810500fe141SSamu Onkalo 
8116a0c9a47SMilo(Woogyom) Kim 	if (!pdata) {
812500fe141SSamu Onkalo 		dev_err(&client->dev, "no platform data\n");
813e430dc00SBryan Wu 		return -EINVAL;
814500fe141SSamu Onkalo 	}
815500fe141SSamu Onkalo 
8166a0c9a47SMilo(Woogyom) Kim 	chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
8176a0c9a47SMilo(Woogyom) Kim 	if (!chip)
8186a0c9a47SMilo(Woogyom) Kim 		return -ENOMEM;
819500fe141SSamu Onkalo 
8206a0c9a47SMilo(Woogyom) Kim 	led = devm_kzalloc(&client->dev,
8216a0c9a47SMilo(Woogyom) Kim 			sizeof(*led) * pdata->num_channels, GFP_KERNEL);
8226a0c9a47SMilo(Woogyom) Kim 	if (!led)
8236a0c9a47SMilo(Woogyom) Kim 		return -ENOMEM;
8246a0c9a47SMilo(Woogyom) Kim 
8256a0c9a47SMilo(Woogyom) Kim 	chip->cl = client;
8266a0c9a47SMilo(Woogyom) Kim 	chip->pdata = pdata;
82748068d5dSMilo(Woogyom) Kim 	chip->cfg = &lp5521_cfg;
8286a0c9a47SMilo(Woogyom) Kim 
8296a0c9a47SMilo(Woogyom) Kim 	mutex_init(&chip->lock);
8306a0c9a47SMilo(Woogyom) Kim 
8316a0c9a47SMilo(Woogyom) Kim 	i2c_set_clientdata(client, led);
832500fe141SSamu Onkalo 
83322ebeb48SMilo(Woogyom) Kim 	ret = lp55xx_init_device(chip);
834944f7b1dSMilo(Woogyom) Kim 	if (ret)
835f6c64c6fSMilo(Woogyom) Kim 		goto err_init;
836500fe141SSamu Onkalo 
837500fe141SSamu Onkalo 	dev_info(&client->dev, "%s programmable led chip found\n", id->name);
838500fe141SSamu Onkalo 
839945c7007SMilo(Woogyom) Kim 	ret = lp5521_register_leds(old_chip);
840f6524808SMilo(Woogyom) Kim 	if (ret)
841e430dc00SBryan Wu 		goto fail2;
842500fe141SSamu Onkalo 
843500fe141SSamu Onkalo 	ret = lp5521_register_sysfs(client);
844500fe141SSamu Onkalo 	if (ret) {
845500fe141SSamu Onkalo 		dev_err(&client->dev, "registering sysfs failed\n");
846e430dc00SBryan Wu 		goto fail2;
847500fe141SSamu Onkalo 	}
848500fe141SSamu Onkalo 	return ret;
849e430dc00SBryan Wu fail2:
850945c7007SMilo(Woogyom) Kim 	lp5521_unregister_leds(old_chip);
851*6ce61762SMilo(Woogyom) Kim 	lp55xx_deinit_device(chip);
852f6c64c6fSMilo(Woogyom) Kim err_init:
853500fe141SSamu Onkalo 	return ret;
854500fe141SSamu Onkalo }
855500fe141SSamu Onkalo 
856678e8a6bSBill Pemberton static int lp5521_remove(struct i2c_client *client)
857500fe141SSamu Onkalo {
858945c7007SMilo(Woogyom) Kim 	struct lp5521_chip *old_chip = i2c_get_clientdata(client);
859*6ce61762SMilo(Woogyom) Kim 	struct lp55xx_led *led = i2c_get_clientdata(client);
860*6ce61762SMilo(Woogyom) Kim 	struct lp55xx_chip *chip = led->chip;
861500fe141SSamu Onkalo 
862945c7007SMilo(Woogyom) Kim 	lp5521_run_led_pattern(PATTERN_OFF, old_chip);
863500fe141SSamu Onkalo 	lp5521_unregister_sysfs(client);
864500fe141SSamu Onkalo 
865945c7007SMilo(Woogyom) Kim 	lp5521_unregister_leds(old_chip);
866*6ce61762SMilo(Woogyom) Kim 	lp55xx_deinit_device(chip);
867500fe141SSamu Onkalo 
868500fe141SSamu Onkalo 	return 0;
869500fe141SSamu Onkalo }
870500fe141SSamu Onkalo 
871500fe141SSamu Onkalo static const struct i2c_device_id lp5521_id[] = {
872500fe141SSamu Onkalo 	{ "lp5521", 0 }, /* Three channel chip */
873500fe141SSamu Onkalo 	{ }
874500fe141SSamu Onkalo };
875500fe141SSamu Onkalo MODULE_DEVICE_TABLE(i2c, lp5521_id);
876500fe141SSamu Onkalo 
877500fe141SSamu Onkalo static struct i2c_driver lp5521_driver = {
878500fe141SSamu Onkalo 	.driver = {
879500fe141SSamu Onkalo 		.name	= "lp5521",
880500fe141SSamu Onkalo 	},
881500fe141SSamu Onkalo 	.probe		= lp5521_probe,
882df07cf81SBill Pemberton 	.remove		= lp5521_remove,
883500fe141SSamu Onkalo 	.id_table	= lp5521_id,
884500fe141SSamu Onkalo };
885500fe141SSamu Onkalo 
88609a0d183SAxel Lin module_i2c_driver(lp5521_driver);
887500fe141SSamu Onkalo 
888500fe141SSamu Onkalo MODULE_AUTHOR("Mathias Nyman, Yuri Zaporozhets, Samu Onkalo");
889500fe141SSamu Onkalo MODULE_DESCRIPTION("LP5521 LED engine");
890500fe141SSamu Onkalo MODULE_LICENSE("GPL v2");
891