xref: /openbmc/linux/drivers/leds/leds-lp5521.c (revision 48068d5de16c23c256c085b2cd3ff03bec393900)
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 
101*48068d5dSMilo(Woogyom) Kim /* Reset register value */
102*48068d5dSMilo(Woogyom) Kim #define LP5521_RESET			0xFF
103*48068d5dSMilo(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 
241d4e7ad03SSamu Onkalo static int lp5521_configure(struct i2c_client *client)
242500fe141SSamu Onkalo {
243500fe141SSamu Onkalo 	struct lp5521_chip *chip = i2c_get_clientdata(client);
244500fe141SSamu Onkalo 	int ret;
2453b49aacdSKim, Milo 	u8 cfg;
24694482174SMilo(Woogyom) Kim 	u8 val;
247500fe141SSamu Onkalo 
24894482174SMilo(Woogyom) Kim 	/*
24994482174SMilo(Woogyom) Kim 	 * Make sure that the chip is reset by reading back the r channel
25094482174SMilo(Woogyom) Kim 	 * current reg. This is dummy read is required on some platforms -
25194482174SMilo(Woogyom) Kim 	 * otherwise further access to the R G B channels in the
25294482174SMilo(Woogyom) Kim 	 * LP5521_REG_ENABLE register will not have any effect - strange!
25394482174SMilo(Woogyom) Kim 	 */
25494482174SMilo(Woogyom) Kim 	ret = lp5521_read(client, LP5521_REG_R_CURRENT, &val);
25594482174SMilo(Woogyom) Kim 	if (ret) {
25694482174SMilo(Woogyom) Kim 		dev_err(&client->dev, "error in resetting chip\n");
25794482174SMilo(Woogyom) Kim 		return ret;
25894482174SMilo(Woogyom) Kim 	}
25994482174SMilo(Woogyom) Kim 	if (val != LP5521_REG_R_CURR_DEFAULT) {
26094482174SMilo(Woogyom) Kim 		dev_err(&client->dev,
26194482174SMilo(Woogyom) Kim 			"unexpected data in register (expected 0x%x got 0x%x)\n",
26294482174SMilo(Woogyom) Kim 			LP5521_REG_R_CURR_DEFAULT, val);
26394482174SMilo(Woogyom) Kim 		ret = -EINVAL;
26494482174SMilo(Woogyom) Kim 		return ret;
26594482174SMilo(Woogyom) Kim 	}
26694482174SMilo(Woogyom) Kim 	usleep_range(10000, 20000);
267500fe141SSamu Onkalo 
268500fe141SSamu Onkalo 	/* Set all PWMs to direct control mode */
26932a2f747SKim, Milo 	ret = lp5521_write(client, LP5521_REG_OP_MODE, LP5521_CMD_DIRECT);
270500fe141SSamu Onkalo 
2713b49aacdSKim, Milo 	cfg = chip->pdata->update_config ?
2723b49aacdSKim, Milo 		: (LP5521_PWRSAVE_EN | LP5521_CP_MODE_AUTO | LP5521_R_TO_BATT);
27394482174SMilo(Woogyom) Kim 	ret = lp5521_write(client, LP5521_REG_CONFIG, cfg);
27494482174SMilo(Woogyom) Kim 	if (ret)
27594482174SMilo(Woogyom) Kim 		return ret;
276500fe141SSamu Onkalo 
277500fe141SSamu Onkalo 	/* Initialize all channels PWM to zero -> leds off */
27894482174SMilo(Woogyom) Kim 	lp5521_write(client, LP5521_REG_R_PWM, 0);
27994482174SMilo(Woogyom) Kim 	lp5521_write(client, LP5521_REG_G_PWM, 0);
28094482174SMilo(Woogyom) Kim 	lp5521_write(client, LP5521_REG_B_PWM, 0);
281500fe141SSamu Onkalo 
282500fe141SSamu Onkalo 	/* Set engines are set to run state when OP_MODE enables engines */
28394482174SMilo(Woogyom) Kim 	ret = lp5521_write(client, LP5521_REG_ENABLE,
28432a2f747SKim, Milo 			LP5521_ENABLE_RUN_PROGRAM);
28594482174SMilo(Woogyom) Kim 	if (ret)
286500fe141SSamu Onkalo 		return ret;
28794482174SMilo(Woogyom) Kim 
28894482174SMilo(Woogyom) Kim 	lp5521_wait_enable_done();
28994482174SMilo(Woogyom) Kim 
29094482174SMilo(Woogyom) Kim 	return 0;
291500fe141SSamu Onkalo }
292500fe141SSamu Onkalo 
293500fe141SSamu Onkalo static int lp5521_run_selftest(struct lp5521_chip *chip, char *buf)
294500fe141SSamu Onkalo {
295500fe141SSamu Onkalo 	int ret;
296500fe141SSamu Onkalo 	u8 status;
297500fe141SSamu Onkalo 
298500fe141SSamu Onkalo 	ret = lp5521_read(chip->client, LP5521_REG_STATUS, &status);
299500fe141SSamu Onkalo 	if (ret < 0)
300500fe141SSamu Onkalo 		return ret;
301500fe141SSamu Onkalo 
302500fe141SSamu Onkalo 	/* Check that ext clock is really in use if requested */
303500fe141SSamu Onkalo 	if (chip->pdata && chip->pdata->clock_mode == LP5521_CLOCK_EXT)
304500fe141SSamu Onkalo 		if  ((status & LP5521_EXT_CLK_USED) == 0)
305500fe141SSamu Onkalo 			return -EIO;
306500fe141SSamu Onkalo 	return 0;
307500fe141SSamu Onkalo }
308500fe141SSamu Onkalo 
309500fe141SSamu Onkalo static void lp5521_set_brightness(struct led_classdev *cdev,
310500fe141SSamu Onkalo 			     enum led_brightness brightness)
311500fe141SSamu Onkalo {
312500fe141SSamu Onkalo 	struct lp5521_led *led = cdev_to_led(cdev);
313500fe141SSamu Onkalo 	led->brightness = (u8)brightness;
314500fe141SSamu Onkalo 	schedule_work(&led->brightness_work);
315500fe141SSamu Onkalo }
316500fe141SSamu Onkalo 
317500fe141SSamu Onkalo static void lp5521_led_brightness_work(struct work_struct *work)
318500fe141SSamu Onkalo {
319500fe141SSamu Onkalo 	struct lp5521_led *led = container_of(work,
320500fe141SSamu Onkalo 					      struct lp5521_led,
321500fe141SSamu Onkalo 					      brightness_work);
322500fe141SSamu Onkalo 	struct lp5521_chip *chip = led_to_lp5521(led);
323500fe141SSamu Onkalo 	struct i2c_client *client = chip->client;
324500fe141SSamu Onkalo 
325500fe141SSamu Onkalo 	mutex_lock(&chip->lock);
326500fe141SSamu Onkalo 	lp5521_write(client, LP5521_REG_LED_PWM_BASE + led->chan_nr,
327500fe141SSamu Onkalo 		led->brightness);
328500fe141SSamu Onkalo 	mutex_unlock(&chip->lock);
329500fe141SSamu Onkalo }
330500fe141SSamu Onkalo 
331500fe141SSamu Onkalo /* Detect the chip by setting its ENABLE register and reading it back. */
332500fe141SSamu Onkalo static int lp5521_detect(struct i2c_client *client)
333500fe141SSamu Onkalo {
334500fe141SSamu Onkalo 	int ret;
335500fe141SSamu Onkalo 	u8 buf;
336500fe141SSamu Onkalo 
33732a2f747SKim, Milo 	ret = lp5521_write(client, LP5521_REG_ENABLE, LP5521_ENABLE_DEFAULT);
338500fe141SSamu Onkalo 	if (ret)
339500fe141SSamu Onkalo 		return ret;
34009c76b0fSSamu Onkalo 	/* enable takes 500us. 1 - 2 ms leaves some margin */
34109c76b0fSSamu Onkalo 	usleep_range(1000, 2000);
342500fe141SSamu Onkalo 	ret = lp5521_read(client, LP5521_REG_ENABLE, &buf);
343500fe141SSamu Onkalo 	if (ret)
344500fe141SSamu Onkalo 		return ret;
34532a2f747SKim, Milo 	if (buf != LP5521_ENABLE_DEFAULT)
346500fe141SSamu Onkalo 		return -ENODEV;
347500fe141SSamu Onkalo 
348500fe141SSamu Onkalo 	return 0;
349500fe141SSamu Onkalo }
350500fe141SSamu Onkalo 
351500fe141SSamu Onkalo /* Set engine mode and create appropriate sysfs attributes, if required. */
352500fe141SSamu Onkalo static int lp5521_set_mode(struct lp5521_engine *engine, u8 mode)
353500fe141SSamu Onkalo {
354500fe141SSamu Onkalo 	int ret = 0;
355500fe141SSamu Onkalo 
356500fe141SSamu Onkalo 	/* if in that mode already do nothing, except for run */
357500fe141SSamu Onkalo 	if (mode == engine->mode && mode != LP5521_CMD_RUN)
358500fe141SSamu Onkalo 		return 0;
359500fe141SSamu Onkalo 
360500fe141SSamu Onkalo 	if (mode == LP5521_CMD_RUN) {
361500fe141SSamu Onkalo 		ret = lp5521_set_engine_mode(engine, LP5521_CMD_RUN);
362500fe141SSamu Onkalo 	} else if (mode == LP5521_CMD_LOAD) {
363500fe141SSamu Onkalo 		lp5521_set_engine_mode(engine, LP5521_CMD_DISABLED);
364500fe141SSamu Onkalo 		lp5521_set_engine_mode(engine, LP5521_CMD_LOAD);
365500fe141SSamu Onkalo 	} else if (mode == LP5521_CMD_DISABLED) {
366500fe141SSamu Onkalo 		lp5521_set_engine_mode(engine, LP5521_CMD_DISABLED);
367500fe141SSamu Onkalo 	}
368500fe141SSamu Onkalo 
369500fe141SSamu Onkalo 	engine->mode = mode;
370500fe141SSamu Onkalo 
371500fe141SSamu Onkalo 	return ret;
372500fe141SSamu Onkalo }
373500fe141SSamu Onkalo 
374500fe141SSamu Onkalo static int lp5521_do_store_load(struct lp5521_engine *engine,
375500fe141SSamu Onkalo 				const char *buf, size_t len)
376500fe141SSamu Onkalo {
377500fe141SSamu Onkalo 	struct lp5521_chip *chip = engine_to_lp5521(engine);
378500fe141SSamu Onkalo 	struct i2c_client *client = chip->client;
379500fe141SSamu Onkalo 	int  ret, nrchars, offset = 0, i = 0;
380500fe141SSamu Onkalo 	char c[3];
381500fe141SSamu Onkalo 	unsigned cmd;
382500fe141SSamu Onkalo 	u8 pattern[LP5521_PROGRAM_LENGTH] = {0};
383500fe141SSamu Onkalo 
384500fe141SSamu Onkalo 	while ((offset < len - 1) && (i < LP5521_PROGRAM_LENGTH)) {
385500fe141SSamu Onkalo 		/* separate sscanfs because length is working only for %s */
386500fe141SSamu Onkalo 		ret = sscanf(buf + offset, "%2s%n ", c, &nrchars);
3872260209cSVasiliy Kulikov 		if (ret != 2)
3882260209cSVasiliy Kulikov 			goto fail;
389500fe141SSamu Onkalo 		ret = sscanf(c, "%2x", &cmd);
390500fe141SSamu Onkalo 		if (ret != 1)
391500fe141SSamu Onkalo 			goto fail;
392500fe141SSamu Onkalo 		pattern[i] = (u8)cmd;
393500fe141SSamu Onkalo 
394500fe141SSamu Onkalo 		offset += nrchars;
395500fe141SSamu Onkalo 		i++;
396500fe141SSamu Onkalo 	}
397500fe141SSamu Onkalo 
398500fe141SSamu Onkalo 	/* Each instruction is 16bit long. Check that length is even */
399500fe141SSamu Onkalo 	if (i % 2)
400500fe141SSamu Onkalo 		goto fail;
401500fe141SSamu Onkalo 
402500fe141SSamu Onkalo 	mutex_lock(&chip->lock);
403d4e7ad03SSamu Onkalo 	if (engine->mode == LP5521_CMD_LOAD)
404500fe141SSamu Onkalo 		ret = lp5521_load_program(engine, pattern);
405d4e7ad03SSamu Onkalo 	else
406d4e7ad03SSamu Onkalo 		ret = -EINVAL;
407500fe141SSamu Onkalo 	mutex_unlock(&chip->lock);
408500fe141SSamu Onkalo 
409500fe141SSamu Onkalo 	if (ret) {
410500fe141SSamu Onkalo 		dev_err(&client->dev, "failed loading pattern\n");
411500fe141SSamu Onkalo 		return ret;
412500fe141SSamu Onkalo 	}
413500fe141SSamu Onkalo 
414500fe141SSamu Onkalo 	return len;
415500fe141SSamu Onkalo fail:
416500fe141SSamu Onkalo 	dev_err(&client->dev, "wrong pattern format\n");
417500fe141SSamu Onkalo 	return -EINVAL;
418500fe141SSamu Onkalo }
419500fe141SSamu Onkalo 
420500fe141SSamu Onkalo static ssize_t store_engine_load(struct device *dev,
421500fe141SSamu Onkalo 				     struct device_attribute *attr,
422500fe141SSamu Onkalo 				     const char *buf, size_t len, int nr)
423500fe141SSamu Onkalo {
424500fe141SSamu Onkalo 	struct i2c_client *client = to_i2c_client(dev);
425500fe141SSamu Onkalo 	struct lp5521_chip *chip = i2c_get_clientdata(client);
426500fe141SSamu Onkalo 	return lp5521_do_store_load(&chip->engines[nr - 1], buf, len);
427500fe141SSamu Onkalo }
428500fe141SSamu Onkalo 
429500fe141SSamu Onkalo #define store_load(nr)							\
430500fe141SSamu Onkalo static ssize_t store_engine##nr##_load(struct device *dev,		\
431500fe141SSamu Onkalo 				     struct device_attribute *attr,	\
432500fe141SSamu Onkalo 				     const char *buf, size_t len)	\
433500fe141SSamu Onkalo {									\
434500fe141SSamu Onkalo 	return store_engine_load(dev, attr, buf, len, nr);		\
435500fe141SSamu Onkalo }
436500fe141SSamu Onkalo store_load(1)
437500fe141SSamu Onkalo store_load(2)
438500fe141SSamu Onkalo store_load(3)
439500fe141SSamu Onkalo 
440500fe141SSamu Onkalo static ssize_t show_engine_mode(struct device *dev,
441500fe141SSamu Onkalo 				struct device_attribute *attr,
442500fe141SSamu Onkalo 				char *buf, int nr)
443500fe141SSamu Onkalo {
444500fe141SSamu Onkalo 	struct i2c_client *client = to_i2c_client(dev);
445500fe141SSamu Onkalo 	struct lp5521_chip *chip = i2c_get_clientdata(client);
446500fe141SSamu Onkalo 	switch (chip->engines[nr - 1].mode) {
447500fe141SSamu Onkalo 	case LP5521_CMD_RUN:
448500fe141SSamu Onkalo 		return sprintf(buf, "run\n");
449500fe141SSamu Onkalo 	case LP5521_CMD_LOAD:
450500fe141SSamu Onkalo 		return sprintf(buf, "load\n");
451500fe141SSamu Onkalo 	case LP5521_CMD_DISABLED:
452500fe141SSamu Onkalo 		return sprintf(buf, "disabled\n");
453500fe141SSamu Onkalo 	default:
454500fe141SSamu Onkalo 		return sprintf(buf, "disabled\n");
455500fe141SSamu Onkalo 	}
456500fe141SSamu Onkalo }
457500fe141SSamu Onkalo 
458500fe141SSamu Onkalo #define show_mode(nr)							\
459500fe141SSamu Onkalo static ssize_t show_engine##nr##_mode(struct device *dev,		\
460500fe141SSamu Onkalo 				    struct device_attribute *attr,	\
461500fe141SSamu Onkalo 				    char *buf)				\
462500fe141SSamu Onkalo {									\
463500fe141SSamu Onkalo 	return show_engine_mode(dev, attr, buf, nr);			\
464500fe141SSamu Onkalo }
465500fe141SSamu Onkalo show_mode(1)
466500fe141SSamu Onkalo show_mode(2)
467500fe141SSamu Onkalo show_mode(3)
468500fe141SSamu Onkalo 
469500fe141SSamu Onkalo static ssize_t store_engine_mode(struct device *dev,
470500fe141SSamu Onkalo 				 struct device_attribute *attr,
471500fe141SSamu Onkalo 				 const char *buf, size_t len, int nr)
472500fe141SSamu Onkalo {
473500fe141SSamu Onkalo 	struct i2c_client *client = to_i2c_client(dev);
474500fe141SSamu Onkalo 	struct lp5521_chip *chip = i2c_get_clientdata(client);
475500fe141SSamu Onkalo 	struct lp5521_engine *engine = &chip->engines[nr - 1];
476500fe141SSamu Onkalo 	mutex_lock(&chip->lock);
477500fe141SSamu Onkalo 
478500fe141SSamu Onkalo 	if (!strncmp(buf, "run", 3))
479500fe141SSamu Onkalo 		lp5521_set_mode(engine, LP5521_CMD_RUN);
480500fe141SSamu Onkalo 	else if (!strncmp(buf, "load", 4))
481500fe141SSamu Onkalo 		lp5521_set_mode(engine, LP5521_CMD_LOAD);
482500fe141SSamu Onkalo 	else if (!strncmp(buf, "disabled", 8))
483500fe141SSamu Onkalo 		lp5521_set_mode(engine, LP5521_CMD_DISABLED);
484500fe141SSamu Onkalo 
485500fe141SSamu Onkalo 	mutex_unlock(&chip->lock);
486500fe141SSamu Onkalo 	return len;
487500fe141SSamu Onkalo }
488500fe141SSamu Onkalo 
489500fe141SSamu Onkalo #define store_mode(nr)							\
490500fe141SSamu Onkalo static ssize_t store_engine##nr##_mode(struct device *dev,		\
491500fe141SSamu Onkalo 				     struct device_attribute *attr,	\
492500fe141SSamu Onkalo 				     const char *buf, size_t len)	\
493500fe141SSamu Onkalo {									\
494500fe141SSamu Onkalo 	return store_engine_mode(dev, attr, buf, len, nr);		\
495500fe141SSamu Onkalo }
496500fe141SSamu Onkalo store_mode(1)
497500fe141SSamu Onkalo store_mode(2)
498500fe141SSamu Onkalo store_mode(3)
499500fe141SSamu Onkalo 
500500fe141SSamu Onkalo static ssize_t show_max_current(struct device *dev,
501500fe141SSamu Onkalo 			    struct device_attribute *attr,
502500fe141SSamu Onkalo 			    char *buf)
503500fe141SSamu Onkalo {
504500fe141SSamu Onkalo 	struct led_classdev *led_cdev = dev_get_drvdata(dev);
505500fe141SSamu Onkalo 	struct lp5521_led *led = cdev_to_led(led_cdev);
506500fe141SSamu Onkalo 
507500fe141SSamu Onkalo 	return sprintf(buf, "%d\n", led->max_current);
508500fe141SSamu Onkalo }
509500fe141SSamu Onkalo 
510500fe141SSamu Onkalo static ssize_t show_current(struct device *dev,
511500fe141SSamu Onkalo 			    struct device_attribute *attr,
512500fe141SSamu Onkalo 			    char *buf)
513500fe141SSamu Onkalo {
514500fe141SSamu Onkalo 	struct led_classdev *led_cdev = dev_get_drvdata(dev);
515500fe141SSamu Onkalo 	struct lp5521_led *led = cdev_to_led(led_cdev);
516500fe141SSamu Onkalo 
517500fe141SSamu Onkalo 	return sprintf(buf, "%d\n", led->led_current);
518500fe141SSamu Onkalo }
519500fe141SSamu Onkalo 
520500fe141SSamu Onkalo static ssize_t store_current(struct device *dev,
521500fe141SSamu Onkalo 			     struct device_attribute *attr,
522500fe141SSamu Onkalo 			     const char *buf, size_t len)
523500fe141SSamu Onkalo {
524500fe141SSamu Onkalo 	struct led_classdev *led_cdev = dev_get_drvdata(dev);
525500fe141SSamu Onkalo 	struct lp5521_led *led = cdev_to_led(led_cdev);
526500fe141SSamu Onkalo 	struct lp5521_chip *chip = led_to_lp5521(led);
527500fe141SSamu Onkalo 	ssize_t ret;
528500fe141SSamu Onkalo 	unsigned long curr;
529500fe141SSamu Onkalo 
530011af7bcSKim, Milo 	if (kstrtoul(buf, 0, &curr))
531500fe141SSamu Onkalo 		return -EINVAL;
532500fe141SSamu Onkalo 
533500fe141SSamu Onkalo 	if (curr > led->max_current)
534500fe141SSamu Onkalo 		return -EINVAL;
535500fe141SSamu Onkalo 
536500fe141SSamu Onkalo 	mutex_lock(&chip->lock);
537500fe141SSamu Onkalo 	ret = lp5521_set_led_current(chip, led->id, curr);
538500fe141SSamu Onkalo 	mutex_unlock(&chip->lock);
539500fe141SSamu Onkalo 
540500fe141SSamu Onkalo 	if (ret < 0)
541500fe141SSamu Onkalo 		return ret;
542500fe141SSamu Onkalo 
543500fe141SSamu Onkalo 	led->led_current = (u8)curr;
544500fe141SSamu Onkalo 
545500fe141SSamu Onkalo 	return len;
546500fe141SSamu Onkalo }
547500fe141SSamu Onkalo 
548500fe141SSamu Onkalo static ssize_t lp5521_selftest(struct device *dev,
549500fe141SSamu Onkalo 			       struct device_attribute *attr,
550500fe141SSamu Onkalo 			       char *buf)
551500fe141SSamu Onkalo {
552500fe141SSamu Onkalo 	struct i2c_client *client = to_i2c_client(dev);
553500fe141SSamu Onkalo 	struct lp5521_chip *chip = i2c_get_clientdata(client);
554500fe141SSamu Onkalo 	int ret;
555500fe141SSamu Onkalo 
556500fe141SSamu Onkalo 	mutex_lock(&chip->lock);
557500fe141SSamu Onkalo 	ret = lp5521_run_selftest(chip, buf);
558500fe141SSamu Onkalo 	mutex_unlock(&chip->lock);
559500fe141SSamu Onkalo 	return sprintf(buf, "%s\n", ret ? "FAIL" : "OK");
560500fe141SSamu Onkalo }
561500fe141SSamu Onkalo 
562011af7bcSKim, Milo static void lp5521_clear_program_memory(struct i2c_client *cl)
563011af7bcSKim, Milo {
564011af7bcSKim, Milo 	int i;
565011af7bcSKim, Milo 	u8 rgb_mem[] = {
566011af7bcSKim, Milo 		LP5521_REG_R_PROG_MEM,
567011af7bcSKim, Milo 		LP5521_REG_G_PROG_MEM,
568011af7bcSKim, Milo 		LP5521_REG_B_PROG_MEM,
569011af7bcSKim, Milo 	};
570011af7bcSKim, Milo 
571011af7bcSKim, Milo 	for (i = 0; i < ARRAY_SIZE(rgb_mem); i++) {
572011af7bcSKim, Milo 		lp5521_write(cl, rgb_mem[i], 0);
573011af7bcSKim, Milo 		lp5521_write(cl, rgb_mem[i] + 1, 0);
574011af7bcSKim, Milo 	}
575011af7bcSKim, Milo }
576011af7bcSKim, Milo 
577011af7bcSKim, Milo static void lp5521_write_program_memory(struct i2c_client *cl,
578011af7bcSKim, Milo 				u8 base, u8 *rgb, int size)
579011af7bcSKim, Milo {
580011af7bcSKim, Milo 	int i;
581011af7bcSKim, Milo 
582011af7bcSKim, Milo 	if (!rgb || size <= 0)
583011af7bcSKim, Milo 		return;
584011af7bcSKim, Milo 
585011af7bcSKim, Milo 	for (i = 0; i < size; i++)
586011af7bcSKim, Milo 		lp5521_write(cl, base + i, *(rgb + i));
587011af7bcSKim, Milo 
588011af7bcSKim, Milo 	lp5521_write(cl, base + i, 0);
589011af7bcSKim, Milo 	lp5521_write(cl, base + i + 1, 0);
590011af7bcSKim, Milo }
591011af7bcSKim, Milo 
592011af7bcSKim, Milo static inline struct lp5521_led_pattern *lp5521_get_pattern
593011af7bcSKim, Milo 					(struct lp5521_chip *chip, u8 offset)
594011af7bcSKim, Milo {
595011af7bcSKim, Milo 	struct lp5521_led_pattern *ptn;
596011af7bcSKim, Milo 	ptn = chip->pdata->patterns + (offset - 1);
597011af7bcSKim, Milo 	return ptn;
598011af7bcSKim, Milo }
599011af7bcSKim, Milo 
600011af7bcSKim, Milo static void lp5521_run_led_pattern(int mode, struct lp5521_chip *chip)
601011af7bcSKim, Milo {
602011af7bcSKim, Milo 	struct lp5521_led_pattern *ptn;
603011af7bcSKim, Milo 	struct i2c_client *cl = chip->client;
604011af7bcSKim, Milo 	int num_patterns = chip->pdata->num_patterns;
605011af7bcSKim, Milo 
606011af7bcSKim, Milo 	if (mode > num_patterns || !(chip->pdata->patterns))
607011af7bcSKim, Milo 		return;
608011af7bcSKim, Milo 
609011af7bcSKim, Milo 	if (mode == PATTERN_OFF) {
61032a2f747SKim, Milo 		lp5521_write(cl, LP5521_REG_ENABLE, LP5521_ENABLE_DEFAULT);
611011af7bcSKim, Milo 		usleep_range(1000, 2000);
612011af7bcSKim, Milo 		lp5521_write(cl, LP5521_REG_OP_MODE, LP5521_CMD_DIRECT);
613011af7bcSKim, Milo 	} else {
614011af7bcSKim, Milo 		ptn = lp5521_get_pattern(chip, mode);
615011af7bcSKim, Milo 		if (!ptn)
616011af7bcSKim, Milo 			return;
617011af7bcSKim, Milo 
618011af7bcSKim, Milo 		lp5521_write(cl, LP5521_REG_OP_MODE, LP5521_CMD_LOAD);
619011af7bcSKim, Milo 		usleep_range(1000, 2000);
620011af7bcSKim, Milo 
621011af7bcSKim, Milo 		lp5521_clear_program_memory(cl);
622011af7bcSKim, Milo 
623011af7bcSKim, Milo 		lp5521_write_program_memory(cl, LP5521_REG_R_PROG_MEM,
624011af7bcSKim, Milo 					ptn->r, ptn->size_r);
625011af7bcSKim, Milo 		lp5521_write_program_memory(cl, LP5521_REG_G_PROG_MEM,
626011af7bcSKim, Milo 					ptn->g, ptn->size_g);
627011af7bcSKim, Milo 		lp5521_write_program_memory(cl, LP5521_REG_B_PROG_MEM,
628011af7bcSKim, Milo 					ptn->b, ptn->size_b);
629011af7bcSKim, Milo 
630011af7bcSKim, Milo 		lp5521_write(cl, LP5521_REG_OP_MODE, LP5521_CMD_RUN);
631011af7bcSKim, Milo 		usleep_range(1000, 2000);
63232a2f747SKim, Milo 		lp5521_write(cl, LP5521_REG_ENABLE, LP5521_ENABLE_RUN_PROGRAM);
633011af7bcSKim, Milo 	}
634011af7bcSKim, Milo }
635011af7bcSKim, Milo 
636011af7bcSKim, Milo static ssize_t store_led_pattern(struct device *dev,
637011af7bcSKim, Milo 				struct device_attribute *attr,
638011af7bcSKim, Milo 				const char *buf, size_t len)
639011af7bcSKim, Milo {
640011af7bcSKim, Milo 	struct lp5521_chip *chip = i2c_get_clientdata(to_i2c_client(dev));
641011af7bcSKim, Milo 	unsigned long val;
642011af7bcSKim, Milo 	int ret;
643011af7bcSKim, Milo 
64469b44c16SJingoo Han 	ret = kstrtoul(buf, 16, &val);
645011af7bcSKim, Milo 	if (ret)
646011af7bcSKim, Milo 		return ret;
647011af7bcSKim, Milo 
648011af7bcSKim, Milo 	lp5521_run_led_pattern(val, chip);
649011af7bcSKim, Milo 
650011af7bcSKim, Milo 	return len;
651011af7bcSKim, Milo }
652011af7bcSKim, Milo 
653500fe141SSamu Onkalo /* led class device attributes */
65467d1da79SVasiliy Kulikov static DEVICE_ATTR(led_current, S_IRUGO | S_IWUSR, show_current, store_current);
655500fe141SSamu Onkalo static DEVICE_ATTR(max_current, S_IRUGO , show_max_current, NULL);
656500fe141SSamu Onkalo 
657500fe141SSamu Onkalo static struct attribute *lp5521_led_attributes[] = {
658500fe141SSamu Onkalo 	&dev_attr_led_current.attr,
659500fe141SSamu Onkalo 	&dev_attr_max_current.attr,
660500fe141SSamu Onkalo 	NULL,
661500fe141SSamu Onkalo };
662500fe141SSamu Onkalo 
663500fe141SSamu Onkalo static struct attribute_group lp5521_led_attribute_group = {
664500fe141SSamu Onkalo 	.attrs = lp5521_led_attributes
665500fe141SSamu Onkalo };
666500fe141SSamu Onkalo 
667500fe141SSamu Onkalo /* device attributes */
66867d1da79SVasiliy Kulikov static DEVICE_ATTR(engine1_mode, S_IRUGO | S_IWUSR,
669500fe141SSamu Onkalo 		   show_engine1_mode, store_engine1_mode);
67067d1da79SVasiliy Kulikov static DEVICE_ATTR(engine2_mode, S_IRUGO | S_IWUSR,
671500fe141SSamu Onkalo 		   show_engine2_mode, store_engine2_mode);
67267d1da79SVasiliy Kulikov static DEVICE_ATTR(engine3_mode, S_IRUGO | S_IWUSR,
673500fe141SSamu Onkalo 		   show_engine3_mode, store_engine3_mode);
67467d1da79SVasiliy Kulikov static DEVICE_ATTR(engine1_load, S_IWUSR, NULL, store_engine1_load);
67567d1da79SVasiliy Kulikov static DEVICE_ATTR(engine2_load, S_IWUSR, NULL, store_engine2_load);
67667d1da79SVasiliy Kulikov static DEVICE_ATTR(engine3_load, S_IWUSR, NULL, store_engine3_load);
677500fe141SSamu Onkalo static DEVICE_ATTR(selftest, S_IRUGO, lp5521_selftest, NULL);
678011af7bcSKim, Milo static DEVICE_ATTR(led_pattern, S_IWUSR, NULL, store_led_pattern);
679500fe141SSamu Onkalo 
680500fe141SSamu Onkalo static struct attribute *lp5521_attributes[] = {
681500fe141SSamu Onkalo 	&dev_attr_engine1_mode.attr,
682500fe141SSamu Onkalo 	&dev_attr_engine2_mode.attr,
683500fe141SSamu Onkalo 	&dev_attr_engine3_mode.attr,
684500fe141SSamu Onkalo 	&dev_attr_selftest.attr,
685500fe141SSamu Onkalo 	&dev_attr_engine1_load.attr,
686500fe141SSamu Onkalo 	&dev_attr_engine2_load.attr,
687500fe141SSamu Onkalo 	&dev_attr_engine3_load.attr,
688011af7bcSKim, Milo 	&dev_attr_led_pattern.attr,
689500fe141SSamu Onkalo 	NULL
690500fe141SSamu Onkalo };
691500fe141SSamu Onkalo 
692500fe141SSamu Onkalo static const struct attribute_group lp5521_group = {
693500fe141SSamu Onkalo 	.attrs = lp5521_attributes,
694500fe141SSamu Onkalo };
695500fe141SSamu Onkalo 
696500fe141SSamu Onkalo static int lp5521_register_sysfs(struct i2c_client *client)
697500fe141SSamu Onkalo {
698500fe141SSamu Onkalo 	struct device *dev = &client->dev;
699500fe141SSamu Onkalo 	return sysfs_create_group(&dev->kobj, &lp5521_group);
700500fe141SSamu Onkalo }
701500fe141SSamu Onkalo 
702500fe141SSamu Onkalo static void lp5521_unregister_sysfs(struct i2c_client *client)
703500fe141SSamu Onkalo {
704500fe141SSamu Onkalo 	struct lp5521_chip *chip = i2c_get_clientdata(client);
705500fe141SSamu Onkalo 	struct device *dev = &client->dev;
706500fe141SSamu Onkalo 	int i;
707500fe141SSamu Onkalo 
708500fe141SSamu Onkalo 	sysfs_remove_group(&dev->kobj, &lp5521_group);
709500fe141SSamu Onkalo 
710500fe141SSamu Onkalo 	for (i = 0; i < chip->num_leds; i++)
711500fe141SSamu Onkalo 		sysfs_remove_group(&chip->leds[i].cdev.dev->kobj,
712500fe141SSamu Onkalo 				&lp5521_led_attribute_group);
713500fe141SSamu Onkalo }
714500fe141SSamu Onkalo 
715f6c64c6fSMilo(Woogyom) Kim static void lp5521_deinit_device(struct lp5521_chip *chip);
716944f7b1dSMilo(Woogyom) Kim static int lp5521_init_device(struct lp5521_chip *chip)
717944f7b1dSMilo(Woogyom) Kim {
718944f7b1dSMilo(Woogyom) Kim 	struct i2c_client *client = chip->client;
719944f7b1dSMilo(Woogyom) Kim 	int ret;
720944f7b1dSMilo(Woogyom) Kim 
721944f7b1dSMilo(Woogyom) Kim 	ret = lp5521_detect(client);
722f6c64c6fSMilo(Woogyom) Kim 	if (ret) {
723944f7b1dSMilo(Woogyom) Kim 		dev_err(&client->dev, "Chip not found\n");
724f6c64c6fSMilo(Woogyom) Kim 		goto err;
725f6c64c6fSMilo(Woogyom) Kim 	}
726944f7b1dSMilo(Woogyom) Kim 
727f6c64c6fSMilo(Woogyom) Kim 	ret = lp5521_configure(client);
728f6c64c6fSMilo(Woogyom) Kim 	if (ret < 0) {
729f6c64c6fSMilo(Woogyom) Kim 		dev_err(&client->dev, "error configuring chip\n");
730f6c64c6fSMilo(Woogyom) Kim 		goto err_config;
731f6c64c6fSMilo(Woogyom) Kim 	}
732f6c64c6fSMilo(Woogyom) Kim 
733f6c64c6fSMilo(Woogyom) Kim 	return 0;
734f6c64c6fSMilo(Woogyom) Kim 
735f6c64c6fSMilo(Woogyom) Kim err_config:
736f6c64c6fSMilo(Woogyom) Kim 	lp5521_deinit_device(chip);
737f6c64c6fSMilo(Woogyom) Kim err:
738944f7b1dSMilo(Woogyom) Kim 	return ret;
739944f7b1dSMilo(Woogyom) Kim }
740944f7b1dSMilo(Woogyom) Kim 
7411a991485SMilo(Woogyom) Kim static void lp5521_deinit_device(struct lp5521_chip *chip)
7421a991485SMilo(Woogyom) Kim {
7431a991485SMilo(Woogyom) Kim 	struct lp5521_platform_data *pdata = chip->pdata;
7441a991485SMilo(Woogyom) Kim 
7451a991485SMilo(Woogyom) Kim 	if (pdata->enable)
7461a991485SMilo(Woogyom) Kim 		pdata->enable(0);
7471a991485SMilo(Woogyom) Kim 	if (pdata->release_resources)
7481a991485SMilo(Woogyom) Kim 		pdata->release_resources();
7491a991485SMilo(Woogyom) Kim }
7501a991485SMilo(Woogyom) Kim 
75198ea1ea2SBill Pemberton static int lp5521_init_led(struct lp5521_led *led,
752500fe141SSamu Onkalo 				struct i2c_client *client,
753500fe141SSamu Onkalo 				int chan, struct lp5521_platform_data *pdata)
754500fe141SSamu Onkalo {
755500fe141SSamu Onkalo 	struct device *dev = &client->dev;
756500fe141SSamu Onkalo 	char name[32];
757500fe141SSamu Onkalo 	int res;
758500fe141SSamu Onkalo 
759500fe141SSamu Onkalo 	if (chan >= LP5521_MAX_LEDS)
760500fe141SSamu Onkalo 		return -EINVAL;
761500fe141SSamu Onkalo 
762500fe141SSamu Onkalo 	if (pdata->led_config[chan].led_current == 0)
763500fe141SSamu Onkalo 		return 0;
764500fe141SSamu Onkalo 
765500fe141SSamu Onkalo 	led->led_current = pdata->led_config[chan].led_current;
766500fe141SSamu Onkalo 	led->max_current = pdata->led_config[chan].max_current;
767500fe141SSamu Onkalo 	led->chan_nr = pdata->led_config[chan].chan_nr;
768500fe141SSamu Onkalo 
769500fe141SSamu Onkalo 	if (led->chan_nr >= LP5521_MAX_LEDS) {
770500fe141SSamu Onkalo 		dev_err(dev, "Use channel numbers between 0 and %d\n",
771500fe141SSamu Onkalo 			LP5521_MAX_LEDS - 1);
772500fe141SSamu Onkalo 		return -EINVAL;
773500fe141SSamu Onkalo 	}
774500fe141SSamu Onkalo 
7755ae4e8a7SKim, Milo 	led->cdev.brightness_set = lp5521_set_brightness;
7765ae4e8a7SKim, Milo 	if (pdata->led_config[chan].name) {
7775ae4e8a7SKim, Milo 		led->cdev.name = pdata->led_config[chan].name;
7785ae4e8a7SKim, Milo 	} else {
77961a83932SArun Murthy 		snprintf(name, sizeof(name), "%s:channel%d",
78061a83932SArun Murthy 			pdata->label ?: client->name, chan);
781500fe141SSamu Onkalo 		led->cdev.name = name;
7825ae4e8a7SKim, Milo 	}
7835ae4e8a7SKim, Milo 
784500fe141SSamu Onkalo 	res = led_classdev_register(dev, &led->cdev);
785500fe141SSamu Onkalo 	if (res < 0) {
786500fe141SSamu Onkalo 		dev_err(dev, "couldn't register led on channel %d\n", chan);
787500fe141SSamu Onkalo 		return res;
788500fe141SSamu Onkalo 	}
789500fe141SSamu Onkalo 
790500fe141SSamu Onkalo 	res = sysfs_create_group(&led->cdev.dev->kobj,
791500fe141SSamu Onkalo 			&lp5521_led_attribute_group);
792500fe141SSamu Onkalo 	if (res < 0) {
793500fe141SSamu Onkalo 		dev_err(dev, "couldn't register current attribute\n");
794500fe141SSamu Onkalo 		led_classdev_unregister(&led->cdev);
795500fe141SSamu Onkalo 		return res;
796500fe141SSamu Onkalo 	}
797500fe141SSamu Onkalo 	return 0;
798500fe141SSamu Onkalo }
799500fe141SSamu Onkalo 
800f6524808SMilo(Woogyom) Kim static int lp5521_register_leds(struct lp5521_chip *chip)
801f6524808SMilo(Woogyom) Kim {
802f6524808SMilo(Woogyom) Kim 	struct lp5521_platform_data *pdata = chip->pdata;
803f6524808SMilo(Woogyom) Kim 	struct i2c_client *client = chip->client;
804f6524808SMilo(Woogyom) Kim 	int i;
805f6524808SMilo(Woogyom) Kim 	int led;
806f6524808SMilo(Woogyom) Kim 	int ret;
807f6524808SMilo(Woogyom) Kim 
808f6524808SMilo(Woogyom) Kim 	/* Initialize leds */
809f6524808SMilo(Woogyom) Kim 	chip->num_channels = pdata->num_channels;
810f6524808SMilo(Woogyom) Kim 	chip->num_leds = 0;
811f6524808SMilo(Woogyom) Kim 	led = 0;
812f6524808SMilo(Woogyom) Kim 	for (i = 0; i < pdata->num_channels; i++) {
813f6524808SMilo(Woogyom) Kim 		/* Do not initialize channels that are not connected */
814f6524808SMilo(Woogyom) Kim 		if (pdata->led_config[i].led_current == 0)
815f6524808SMilo(Woogyom) Kim 			continue;
816f6524808SMilo(Woogyom) Kim 
817f6524808SMilo(Woogyom) Kim 		ret = lp5521_init_led(&chip->leds[led], client, i, pdata);
818f6524808SMilo(Woogyom) Kim 		if (ret) {
819f6524808SMilo(Woogyom) Kim 			dev_err(&client->dev, "error initializing leds\n");
820f6524808SMilo(Woogyom) Kim 			return ret;
821f6524808SMilo(Woogyom) Kim 		}
822f6524808SMilo(Woogyom) Kim 		chip->num_leds++;
823f6524808SMilo(Woogyom) Kim 
824f6524808SMilo(Woogyom) Kim 		chip->leds[led].id = led;
825f6524808SMilo(Woogyom) Kim 		/* Set initial LED current */
826f6524808SMilo(Woogyom) Kim 		lp5521_set_led_current(chip, led,
827f6524808SMilo(Woogyom) Kim 				chip->leds[led].led_current);
828f6524808SMilo(Woogyom) Kim 
829f6524808SMilo(Woogyom) Kim 		INIT_WORK(&(chip->leds[led].brightness_work),
830f6524808SMilo(Woogyom) Kim 			lp5521_led_brightness_work);
831f6524808SMilo(Woogyom) Kim 
832f6524808SMilo(Woogyom) Kim 		led++;
833f6524808SMilo(Woogyom) Kim 	}
834f6524808SMilo(Woogyom) Kim 
835f6524808SMilo(Woogyom) Kim 	return 0;
836f6524808SMilo(Woogyom) Kim }
837f6524808SMilo(Woogyom) Kim 
8381904f83dSMilo(Woogyom) Kim static void lp5521_unregister_leds(struct lp5521_chip *chip)
8391904f83dSMilo(Woogyom) Kim {
8401904f83dSMilo(Woogyom) Kim 	int i;
8411904f83dSMilo(Woogyom) Kim 
8421904f83dSMilo(Woogyom) Kim 	for (i = 0; i < chip->num_leds; i++) {
8431904f83dSMilo(Woogyom) Kim 		led_classdev_unregister(&chip->leds[i].cdev);
8441904f83dSMilo(Woogyom) Kim 		cancel_work_sync(&chip->leds[i].brightness_work);
8451904f83dSMilo(Woogyom) Kim 	}
8461904f83dSMilo(Woogyom) Kim }
8471904f83dSMilo(Woogyom) Kim 
848*48068d5dSMilo(Woogyom) Kim /* Chip specific configurations */
849*48068d5dSMilo(Woogyom) Kim static struct lp55xx_device_config lp5521_cfg = {
850*48068d5dSMilo(Woogyom) Kim 	.reset = {
851*48068d5dSMilo(Woogyom) Kim 		.addr = LP5521_REG_RESET,
852*48068d5dSMilo(Woogyom) Kim 		.val  = LP5521_RESET,
853*48068d5dSMilo(Woogyom) Kim 	},
854*48068d5dSMilo(Woogyom) Kim };
855*48068d5dSMilo(Woogyom) Kim 
85698ea1ea2SBill Pemberton static int lp5521_probe(struct i2c_client *client,
857500fe141SSamu Onkalo 			const struct i2c_device_id *id)
858500fe141SSamu Onkalo {
8596a0c9a47SMilo(Woogyom) Kim 	struct lp5521_chip		*old_chip = NULL;
8601904f83dSMilo(Woogyom) Kim 	int ret;
8616a0c9a47SMilo(Woogyom) Kim 	struct lp55xx_chip *chip;
8626a0c9a47SMilo(Woogyom) Kim 	struct lp55xx_led *led;
8636a0c9a47SMilo(Woogyom) Kim 	struct lp55xx_platform_data *pdata = client->dev.platform_data;
864500fe141SSamu Onkalo 
8656a0c9a47SMilo(Woogyom) Kim 	if (!pdata) {
866500fe141SSamu Onkalo 		dev_err(&client->dev, "no platform data\n");
867e430dc00SBryan Wu 		return -EINVAL;
868500fe141SSamu Onkalo 	}
869500fe141SSamu Onkalo 
8706a0c9a47SMilo(Woogyom) Kim 	chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
8716a0c9a47SMilo(Woogyom) Kim 	if (!chip)
8726a0c9a47SMilo(Woogyom) Kim 		return -ENOMEM;
873500fe141SSamu Onkalo 
8746a0c9a47SMilo(Woogyom) Kim 	led = devm_kzalloc(&client->dev,
8756a0c9a47SMilo(Woogyom) Kim 			sizeof(*led) * pdata->num_channels, GFP_KERNEL);
8766a0c9a47SMilo(Woogyom) Kim 	if (!led)
8776a0c9a47SMilo(Woogyom) Kim 		return -ENOMEM;
8786a0c9a47SMilo(Woogyom) Kim 
8796a0c9a47SMilo(Woogyom) Kim 	chip->cl = client;
8806a0c9a47SMilo(Woogyom) Kim 	chip->pdata = pdata;
881*48068d5dSMilo(Woogyom) Kim 	chip->cfg = &lp5521_cfg;
8826a0c9a47SMilo(Woogyom) Kim 
8836a0c9a47SMilo(Woogyom) Kim 	mutex_init(&chip->lock);
8846a0c9a47SMilo(Woogyom) Kim 
8856a0c9a47SMilo(Woogyom) Kim 	i2c_set_clientdata(client, led);
886500fe141SSamu Onkalo 
887945c7007SMilo(Woogyom) Kim 	ret = lp5521_init_device(old_chip);
888944f7b1dSMilo(Woogyom) Kim 	if (ret)
889f6c64c6fSMilo(Woogyom) Kim 		goto err_init;
890500fe141SSamu Onkalo 
891500fe141SSamu Onkalo 	dev_info(&client->dev, "%s programmable led chip found\n", id->name);
892500fe141SSamu Onkalo 
893945c7007SMilo(Woogyom) Kim 	ret = lp5521_register_leds(old_chip);
894f6524808SMilo(Woogyom) Kim 	if (ret)
895e430dc00SBryan Wu 		goto fail2;
896500fe141SSamu Onkalo 
897500fe141SSamu Onkalo 	ret = lp5521_register_sysfs(client);
898500fe141SSamu Onkalo 	if (ret) {
899500fe141SSamu Onkalo 		dev_err(&client->dev, "registering sysfs failed\n");
900e430dc00SBryan Wu 		goto fail2;
901500fe141SSamu Onkalo 	}
902500fe141SSamu Onkalo 	return ret;
903e430dc00SBryan Wu fail2:
904945c7007SMilo(Woogyom) Kim 	lp5521_unregister_leds(old_chip);
905945c7007SMilo(Woogyom) Kim 	lp5521_deinit_device(old_chip);
906f6c64c6fSMilo(Woogyom) Kim err_init:
907500fe141SSamu Onkalo 	return ret;
908500fe141SSamu Onkalo }
909500fe141SSamu Onkalo 
910678e8a6bSBill Pemberton static int lp5521_remove(struct i2c_client *client)
911500fe141SSamu Onkalo {
912945c7007SMilo(Woogyom) Kim 	struct lp5521_chip *old_chip = i2c_get_clientdata(client);
913500fe141SSamu Onkalo 
914945c7007SMilo(Woogyom) Kim 	lp5521_run_led_pattern(PATTERN_OFF, old_chip);
915500fe141SSamu Onkalo 	lp5521_unregister_sysfs(client);
916500fe141SSamu Onkalo 
917945c7007SMilo(Woogyom) Kim 	lp5521_unregister_leds(old_chip);
918500fe141SSamu Onkalo 
919945c7007SMilo(Woogyom) Kim 	lp5521_deinit_device(old_chip);
920500fe141SSamu Onkalo 	return 0;
921500fe141SSamu Onkalo }
922500fe141SSamu Onkalo 
923500fe141SSamu Onkalo static const struct i2c_device_id lp5521_id[] = {
924500fe141SSamu Onkalo 	{ "lp5521", 0 }, /* Three channel chip */
925500fe141SSamu Onkalo 	{ }
926500fe141SSamu Onkalo };
927500fe141SSamu Onkalo MODULE_DEVICE_TABLE(i2c, lp5521_id);
928500fe141SSamu Onkalo 
929500fe141SSamu Onkalo static struct i2c_driver lp5521_driver = {
930500fe141SSamu Onkalo 	.driver = {
931500fe141SSamu Onkalo 		.name	= "lp5521",
932500fe141SSamu Onkalo 	},
933500fe141SSamu Onkalo 	.probe		= lp5521_probe,
934df07cf81SBill Pemberton 	.remove		= lp5521_remove,
935500fe141SSamu Onkalo 	.id_table	= lp5521_id,
936500fe141SSamu Onkalo };
937500fe141SSamu Onkalo 
93809a0d183SAxel Lin module_i2c_driver(lp5521_driver);
939500fe141SSamu Onkalo 
940500fe141SSamu Onkalo MODULE_AUTHOR("Mathias Nyman, Yuri Zaporozhets, Samu Onkalo");
941500fe141SSamu Onkalo MODULE_DESCRIPTION("LP5521 LED engine");
942500fe141SSamu Onkalo MODULE_LICENSE("GPL v2");
943