blob: 3b6dcb0be7e8d6f9f808831240c112c765d77551 [file] [log] [blame]
Samu Onkalo500fe142010-11-11 14:05:22 -08001/*
2 * LP5521 LED chip driver.
3 *
4 * Copyright (C) 2010 Nokia Corporation
5 *
6 * Contact: Samu Onkalo <samu.p.onkalo@nokia.com>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * version 2 as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA
21 */
22
Samu Onkalo500fe142010-11-11 14:05:22 -080023#include <linux/delay.h>
Milo(Woogyom) Kim9ce7cb12013-02-05 19:18:10 +090024#include <linux/firmware.h>
Milo(Woogyom) Kim79bcc102013-02-05 19:26:14 +090025#include <linux/i2c.h>
26#include <linux/init.h>
27#include <linux/leds.h>
28#include <linux/module.h>
29#include <linux/mutex.h>
30#include <linux/platform_data/leds-lp55xx.h>
31#include <linux/slab.h>
Milo(Woogyom) Kim6a0c9a42013-02-05 18:03:08 +090032
33#include "leds-lp55xx-common.h"
Samu Onkalo500fe142010-11-11 14:05:22 -080034
Milo(Woogyom) Kim12f022d2013-02-05 19:25:26 +090035#define LP5521_PROGRAM_LENGTH 32
36#define LP5521_MAX_LEDS 3
37#define LP5521_CMD_DIRECT 0x3F
Samu Onkalo500fe142010-11-11 14:05:22 -080038
39/* Registers */
40#define LP5521_REG_ENABLE 0x00
41#define LP5521_REG_OP_MODE 0x01
42#define LP5521_REG_R_PWM 0x02
43#define LP5521_REG_G_PWM 0x03
44#define LP5521_REG_B_PWM 0x04
45#define LP5521_REG_R_CURRENT 0x05
46#define LP5521_REG_G_CURRENT 0x06
47#define LP5521_REG_B_CURRENT 0x07
48#define LP5521_REG_CONFIG 0x08
Samu Onkalo500fe142010-11-11 14:05:22 -080049#define LP5521_REG_STATUS 0x0C
50#define LP5521_REG_RESET 0x0D
Samu Onkalo500fe142010-11-11 14:05:22 -080051#define LP5521_REG_R_PROG_MEM 0x10
52#define LP5521_REG_G_PROG_MEM 0x30
53#define LP5521_REG_B_PROG_MEM 0x50
54
Samu Onkalo500fe142010-11-11 14:05:22 -080055/* Base register to set LED current */
56#define LP5521_REG_LED_CURRENT_BASE LP5521_REG_R_CURRENT
Samu Onkalo500fe142010-11-11 14:05:22 -080057/* Base register to set the brightness */
58#define LP5521_REG_LED_PWM_BASE LP5521_REG_R_PWM
59
60/* Bits in ENABLE register */
61#define LP5521_MASTER_ENABLE 0x40 /* Chip master enable */
62#define LP5521_LOGARITHMIC_PWM 0x80 /* Logarithmic PWM adjustment */
63#define LP5521_EXEC_RUN 0x2A
Kim, Milo32a2f742012-03-23 15:02:09 -070064#define LP5521_ENABLE_DEFAULT \
65 (LP5521_MASTER_ENABLE | LP5521_LOGARITHMIC_PWM)
66#define LP5521_ENABLE_RUN_PROGRAM \
67 (LP5521_ENABLE_DEFAULT | LP5521_EXEC_RUN)
Samu Onkalo500fe142010-11-11 14:05:22 -080068
Samu Onkalo500fe142010-11-11 14:05:22 -080069/* Status */
70#define LP5521_EXT_CLK_USED 0x08
71
Srinidhi KASAGARb3c49c02011-10-31 17:12:24 -070072/* default R channel current register value */
73#define LP5521_REG_R_CURR_DEFAULT 0xAF
74
Milo(Woogyom) Kim48068d52013-02-05 18:08:49 +090075/* Reset register value */
76#define LP5521_RESET 0xFF
77
Milo(Woogyom) Kim9ce7cb12013-02-05 19:18:10 +090078/* Program Memory Operations */
79#define LP5521_MODE_R_M 0x30 /* Operation Mode Register */
80#define LP5521_MODE_G_M 0x0C
81#define LP5521_MODE_B_M 0x03
82#define LP5521_LOAD_R 0x10
83#define LP5521_LOAD_G 0x04
84#define LP5521_LOAD_B 0x01
85
86#define LP5521_R_IS_LOADING(mode) \
87 ((mode & LP5521_MODE_R_M) == LP5521_LOAD_R)
88#define LP5521_G_IS_LOADING(mode) \
89 ((mode & LP5521_MODE_G_M) == LP5521_LOAD_G)
90#define LP5521_B_IS_LOADING(mode) \
91 ((mode & LP5521_MODE_B_M) == LP5521_LOAD_B)
92
93#define LP5521_EXEC_R_M 0x30 /* Enable Register */
94#define LP5521_EXEC_G_M 0x0C
95#define LP5521_EXEC_B_M 0x03
96#define LP5521_EXEC_M 0x3F
97#define LP5521_RUN_R 0x20
98#define LP5521_RUN_G 0x08
99#define LP5521_RUN_B 0x02
Samu Onkalo500fe142010-11-11 14:05:22 -0800100
Milo(Woogyom) Kim9ce7cb12013-02-05 19:18:10 +0900101static inline void lp5521_wait_opmode_done(void)
102{
103 /* operation mode change needs to be longer than 153 us */
104 usleep_range(200, 300);
105}
106
Milo(Woogyom) Kim94482172013-02-05 18:03:55 +0900107static inline void lp5521_wait_enable_done(void)
108{
109 /* it takes more 488 us to update ENABLE register */
110 usleep_range(500, 600);
111}
112
Milo(Woogyom) Kima96bfa12013-02-05 19:09:32 +0900113static void lp5521_set_led_current(struct lp55xx_led *led, u8 led_current)
114{
115 led->led_current = led_current;
116 lp55xx_write(led->chip, LP5521_REG_LED_CURRENT_BASE + led->chan_nr,
117 led_current);
118}
119
Milo(Woogyom) Kim9ce7cb12013-02-05 19:18:10 +0900120static void lp5521_load_engine(struct lp55xx_chip *chip)
Samu Onkalo500fe142010-11-11 14:05:22 -0800121{
Milo(Woogyom) Kim9ce7cb12013-02-05 19:18:10 +0900122 enum lp55xx_engine_index idx = chip->engine_idx;
123 u8 mask[] = {
124 [LP55XX_ENGINE_1] = LP5521_MODE_R_M,
125 [LP55XX_ENGINE_2] = LP5521_MODE_G_M,
126 [LP55XX_ENGINE_3] = LP5521_MODE_B_M,
127 };
Samu Onkalo500fe142010-11-11 14:05:22 -0800128
Milo(Woogyom) Kim9ce7cb12013-02-05 19:18:10 +0900129 u8 val[] = {
130 [LP55XX_ENGINE_1] = LP5521_LOAD_R,
131 [LP55XX_ENGINE_2] = LP5521_LOAD_G,
132 [LP55XX_ENGINE_3] = LP5521_LOAD_B,
133 };
Samu Onkalo500fe142010-11-11 14:05:22 -0800134
Milo(Woogyom) Kim9ce7cb12013-02-05 19:18:10 +0900135 lp55xx_update_bits(chip, LP5521_REG_OP_MODE, mask[idx], val[idx]);
Samu Onkalo500fe142010-11-11 14:05:22 -0800136
Milo(Woogyom) Kim9ce7cb12013-02-05 19:18:10 +0900137 lp5521_wait_opmode_done();
Samu Onkalo500fe142010-11-11 14:05:22 -0800138}
139
Milo(Woogyom) Kim9ce7cb12013-02-05 19:18:10 +0900140static void lp5521_stop_engine(struct lp55xx_chip *chip)
Samu Onkalo500fe142010-11-11 14:05:22 -0800141{
Milo(Woogyom) Kim9ce7cb12013-02-05 19:18:10 +0900142 lp55xx_write(chip, LP5521_REG_OP_MODE, 0);
143 lp5521_wait_opmode_done();
144}
145
146static void lp5521_run_engine(struct lp55xx_chip *chip, bool start)
147{
Samu Onkalo500fe142010-11-11 14:05:22 -0800148 int ret;
Samu Onkalo500fe142010-11-11 14:05:22 -0800149 u8 mode;
Milo(Woogyom) Kim9ce7cb12013-02-05 19:18:10 +0900150 u8 exec;
Samu Onkalo500fe142010-11-11 14:05:22 -0800151
Milo(Woogyom) Kim9ce7cb12013-02-05 19:18:10 +0900152 /* stop engine */
153 if (!start) {
154 lp5521_stop_engine(chip);
155 lp55xx_write(chip, LP5521_REG_OP_MODE, LP5521_CMD_DIRECT);
156 lp5521_wait_opmode_done();
157 return;
158 }
159
160 /*
161 * To run the engine,
162 * operation mode and enable register should updated at the same time
163 */
164
165 ret = lp55xx_read(chip, LP5521_REG_OP_MODE, &mode);
Dan Carpenter5bc9ad72012-05-29 15:07:26 -0700166 if (ret)
Milo(Woogyom) Kim9ce7cb12013-02-05 19:18:10 +0900167 return;
Dan Carpenter5bc9ad72012-05-29 15:07:26 -0700168
Milo(Woogyom) Kim9ce7cb12013-02-05 19:18:10 +0900169 ret = lp55xx_read(chip, LP5521_REG_ENABLE, &exec);
Dan Carpenter5bc9ad72012-05-29 15:07:26 -0700170 if (ret)
Milo(Woogyom) Kim9ce7cb12013-02-05 19:18:10 +0900171 return;
Samu Onkalo500fe142010-11-11 14:05:22 -0800172
Milo(Woogyom) Kim9ce7cb12013-02-05 19:18:10 +0900173 /* change operation mode to RUN only when each engine is loading */
174 if (LP5521_R_IS_LOADING(mode)) {
175 mode = (mode & ~LP5521_MODE_R_M) | LP5521_RUN_R;
176 exec = (exec & ~LP5521_EXEC_R_M) | LP5521_RUN_R;
177 }
Samu Onkalo500fe142010-11-11 14:05:22 -0800178
Milo(Woogyom) Kim9ce7cb12013-02-05 19:18:10 +0900179 if (LP5521_G_IS_LOADING(mode)) {
180 mode = (mode & ~LP5521_MODE_G_M) | LP5521_RUN_G;
181 exec = (exec & ~LP5521_EXEC_G_M) | LP5521_RUN_G;
182 }
Samu Onkalo500fe142010-11-11 14:05:22 -0800183
Milo(Woogyom) Kim9ce7cb12013-02-05 19:18:10 +0900184 if (LP5521_B_IS_LOADING(mode)) {
185 mode = (mode & ~LP5521_MODE_B_M) | LP5521_RUN_B;
186 exec = (exec & ~LP5521_EXEC_B_M) | LP5521_RUN_B;
187 }
188
189 lp55xx_write(chip, LP5521_REG_OP_MODE, mode);
190 lp5521_wait_opmode_done();
191
192 lp55xx_update_bits(chip, LP5521_REG_ENABLE, LP5521_EXEC_M, exec);
193 lp5521_wait_enable_done();
194}
195
196static int lp5521_update_program_memory(struct lp55xx_chip *chip,
197 const u8 *data, size_t size)
198{
199 enum lp55xx_engine_index idx = chip->engine_idx;
200 u8 pattern[LP5521_PROGRAM_LENGTH] = {0};
201 u8 addr[] = {
202 [LP55XX_ENGINE_1] = LP5521_REG_R_PROG_MEM,
203 [LP55XX_ENGINE_2] = LP5521_REG_G_PROG_MEM,
204 [LP55XX_ENGINE_3] = LP5521_REG_B_PROG_MEM,
205 };
206 unsigned cmd;
207 char c[3];
208 int program_size;
209 int nrchars;
210 int offset = 0;
211 int ret;
212 int i;
213
214 /* clear program memory before updating */
215 for (i = 0; i < LP5521_PROGRAM_LENGTH; i++)
216 lp55xx_write(chip, addr[idx] + i, 0);
217
218 i = 0;
219 while ((offset < size - 1) && (i < LP5521_PROGRAM_LENGTH)) {
220 /* separate sscanfs because length is working only for %s */
221 ret = sscanf(data + offset, "%2s%n ", c, &nrchars);
222 if (ret != 1)
223 goto err;
224
225 ret = sscanf(c, "%2x", &cmd);
226 if (ret != 1)
227 goto err;
228
229 pattern[i] = (u8)cmd;
230 offset += nrchars;
231 i++;
232 }
233
234 /* Each instruction is 16bit long. Check that length is even */
235 if (i % 2)
236 goto err;
237
238 program_size = i;
239 for (i = 0; i < program_size; i++)
240 lp55xx_write(chip, addr[idx] + i, pattern[i]);
241
242 return 0;
243
244err:
245 dev_err(&chip->cl->dev, "wrong pattern format\n");
246 return -EINVAL;
247}
248
249static void lp5521_firmware_loaded(struct lp55xx_chip *chip)
250{
251 const struct firmware *fw = chip->fw;
252
253 if (fw->size > LP5521_PROGRAM_LENGTH) {
254 dev_err(&chip->cl->dev, "firmware data size overflow: %zu\n",
255 fw->size);
256 return;
257 }
258
259 /*
260 * Program momery sequence
261 * 1) set engine mode to "LOAD"
262 * 2) write firmware data into program memory
263 */
264
265 lp5521_load_engine(chip);
266 lp5521_update_program_memory(chip, fw->data, fw->size);
Samu Onkalo500fe142010-11-11 14:05:22 -0800267}
268
Milo(Woogyom) Kimffbdccd2013-02-05 18:57:36 +0900269static int lp5521_post_init_device(struct lp55xx_chip *chip)
Samu Onkalo500fe142010-11-11 14:05:22 -0800270{
Samu Onkalo500fe142010-11-11 14:05:22 -0800271 int ret;
Milo(Woogyom) Kim94482172013-02-05 18:03:55 +0900272 u8 val;
Samu Onkalo500fe142010-11-11 14:05:22 -0800273
Milo(Woogyom) Kim94482172013-02-05 18:03:55 +0900274 /*
275 * Make sure that the chip is reset by reading back the r channel
276 * current reg. This is dummy read is required on some platforms -
277 * otherwise further access to the R G B channels in the
278 * LP5521_REG_ENABLE register will not have any effect - strange!
279 */
Milo(Woogyom) Kimffbdccd2013-02-05 18:57:36 +0900280 ret = lp55xx_read(chip, LP5521_REG_R_CURRENT, &val);
Milo(Woogyom) Kim94482172013-02-05 18:03:55 +0900281 if (ret) {
Milo(Woogyom) Kimffbdccd2013-02-05 18:57:36 +0900282 dev_err(&chip->cl->dev, "error in resetting chip\n");
Milo(Woogyom) Kim94482172013-02-05 18:03:55 +0900283 return ret;
284 }
285 if (val != LP5521_REG_R_CURR_DEFAULT) {
Milo(Woogyom) Kimffbdccd2013-02-05 18:57:36 +0900286 dev_err(&chip->cl->dev,
Milo(Woogyom) Kim94482172013-02-05 18:03:55 +0900287 "unexpected data in register (expected 0x%x got 0x%x)\n",
288 LP5521_REG_R_CURR_DEFAULT, val);
289 ret = -EINVAL;
290 return ret;
291 }
292 usleep_range(10000, 20000);
Samu Onkalo500fe142010-11-11 14:05:22 -0800293
Samu Onkalo500fe142010-11-11 14:05:22 -0800294 /* Set all PWMs to direct control mode */
Milo(Woogyom) Kimffbdccd2013-02-05 18:57:36 +0900295 ret = lp55xx_write(chip, LP5521_REG_OP_MODE, LP5521_CMD_DIRECT);
Samu Onkalo500fe142010-11-11 14:05:22 -0800296
Milo(Woogyom) Kimffbdccd2013-02-05 18:57:36 +0900297 val = chip->pdata->update_config ?
Kim, Milo3b49aac2012-03-23 15:02:08 -0700298 : (LP5521_PWRSAVE_EN | LP5521_CP_MODE_AUTO | LP5521_R_TO_BATT);
Milo(Woogyom) Kimffbdccd2013-02-05 18:57:36 +0900299 ret = lp55xx_write(chip, LP5521_REG_CONFIG, val);
Milo(Woogyom) Kim94482172013-02-05 18:03:55 +0900300 if (ret)
301 return ret;
Samu Onkalo500fe142010-11-11 14:05:22 -0800302
303 /* Initialize all channels PWM to zero -> leds off */
Milo(Woogyom) Kimffbdccd2013-02-05 18:57:36 +0900304 lp55xx_write(chip, LP5521_REG_R_PWM, 0);
305 lp55xx_write(chip, LP5521_REG_G_PWM, 0);
306 lp55xx_write(chip, LP5521_REG_B_PWM, 0);
Samu Onkalo500fe142010-11-11 14:05:22 -0800307
308 /* Set engines are set to run state when OP_MODE enables engines */
Milo(Woogyom) Kimffbdccd2013-02-05 18:57:36 +0900309 ret = lp55xx_write(chip, LP5521_REG_ENABLE, LP5521_ENABLE_RUN_PROGRAM);
Milo(Woogyom) Kim94482172013-02-05 18:03:55 +0900310 if (ret)
311 return ret;
Samu Onkalo500fe142010-11-11 14:05:22 -0800312
Milo(Woogyom) Kim94482172013-02-05 18:03:55 +0900313 lp5521_wait_enable_done();
314
315 return 0;
Samu Onkalo500fe142010-11-11 14:05:22 -0800316}
317
Milo(Woogyom) Kim9ca3bd82013-02-05 19:21:43 +0900318static int lp5521_run_selftest(struct lp55xx_chip *chip, char *buf)
Samu Onkalo500fe142010-11-11 14:05:22 -0800319{
Milo(Woogyom) Kim9ca3bd82013-02-05 19:21:43 +0900320 struct lp55xx_platform_data *pdata = chip->pdata;
Samu Onkalo500fe142010-11-11 14:05:22 -0800321 int ret;
322 u8 status;
323
Milo(Woogyom) Kim9ca3bd82013-02-05 19:21:43 +0900324 ret = lp55xx_read(chip, LP5521_REG_STATUS, &status);
Samu Onkalo500fe142010-11-11 14:05:22 -0800325 if (ret < 0)
326 return ret;
327
Milo(Woogyom) Kim9ca3bd82013-02-05 19:21:43 +0900328 if (pdata->clock_mode != LP55XX_CLOCK_EXT)
329 return 0;
330
Samu Onkalo500fe142010-11-11 14:05:22 -0800331 /* Check that ext clock is really in use if requested */
Milo(Woogyom) Kim9ca3bd82013-02-05 19:21:43 +0900332 if ((status & LP5521_EXT_CLK_USED) == 0)
333 return -EIO;
334
Samu Onkalo500fe142010-11-11 14:05:22 -0800335 return 0;
336}
337
Samu Onkalo500fe142010-11-11 14:05:22 -0800338static void lp5521_led_brightness_work(struct work_struct *work)
339{
Milo(Woogyom) Kima6e46792013-02-05 19:08:40 +0900340 struct lp55xx_led *led = container_of(work, struct lp55xx_led,
Samu Onkalo500fe142010-11-11 14:05:22 -0800341 brightness_work);
Milo(Woogyom) Kima6e46792013-02-05 19:08:40 +0900342 struct lp55xx_chip *chip = led->chip;
Samu Onkalo500fe142010-11-11 14:05:22 -0800343
344 mutex_lock(&chip->lock);
Milo(Woogyom) Kima6e46792013-02-05 19:08:40 +0900345 lp55xx_write(chip, LP5521_REG_LED_PWM_BASE + led->chan_nr,
Samu Onkalo500fe142010-11-11 14:05:22 -0800346 led->brightness);
347 mutex_unlock(&chip->lock);
348}
349
Samu Onkalo500fe142010-11-11 14:05:22 -0800350static ssize_t lp5521_selftest(struct device *dev,
351 struct device_attribute *attr,
352 char *buf)
353{
Milo(Woogyom) Kim9ca3bd82013-02-05 19:21:43 +0900354 struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
355 struct lp55xx_chip *chip = led->chip;
Samu Onkalo500fe142010-11-11 14:05:22 -0800356 int ret;
357
358 mutex_lock(&chip->lock);
359 ret = lp5521_run_selftest(chip, buf);
360 mutex_unlock(&chip->lock);
361 return sprintf(buf, "%s\n", ret ? "FAIL" : "OK");
362}
363
Samu Onkalo500fe142010-11-11 14:05:22 -0800364/* device attributes */
Samu Onkalo500fe142010-11-11 14:05:22 -0800365static DEVICE_ATTR(selftest, S_IRUGO, lp5521_selftest, NULL);
366
367static struct attribute *lp5521_attributes[] = {
Samu Onkalo500fe142010-11-11 14:05:22 -0800368 &dev_attr_selftest.attr,
Samu Onkalo500fe142010-11-11 14:05:22 -0800369 NULL
370};
371
372static const struct attribute_group lp5521_group = {
373 .attrs = lp5521_attributes,
374};
375
Milo(Woogyom) Kim48068d52013-02-05 18:08:49 +0900376/* Chip specific configurations */
377static struct lp55xx_device_config lp5521_cfg = {
378 .reset = {
379 .addr = LP5521_REG_RESET,
380 .val = LP5521_RESET,
381 },
Milo(Woogyom) Kime3a700d2013-02-05 18:09:56 +0900382 .enable = {
383 .addr = LP5521_REG_ENABLE,
384 .val = LP5521_ENABLE_DEFAULT,
385 },
Milo(Woogyom) Kim0e202342013-02-05 19:07:34 +0900386 .max_channel = LP5521_MAX_LEDS,
Milo(Woogyom) Kimffbdccd2013-02-05 18:57:36 +0900387 .post_init_device = lp5521_post_init_device,
Milo(Woogyom) Kima6e46792013-02-05 19:08:40 +0900388 .brightness_work_fn = lp5521_led_brightness_work,
Milo(Woogyom) Kima96bfa12013-02-05 19:09:32 +0900389 .set_led_current = lp5521_set_led_current,
Milo(Woogyom) Kim9ce7cb12013-02-05 19:18:10 +0900390 .firmware_cb = lp5521_firmware_loaded,
391 .run_engine = lp5521_run_engine,
Milo(Woogyom) Kime73c0ce2013-02-05 19:20:45 +0900392 .dev_attr_group = &lp5521_group,
Milo(Woogyom) Kim48068d52013-02-05 18:08:49 +0900393};
394
Bill Pemberton98ea1ea2012-11-19 13:23:02 -0500395static int lp5521_probe(struct i2c_client *client,
Samu Onkalo500fe142010-11-11 14:05:22 -0800396 const struct i2c_device_id *id)
397{
Milo(Woogyom) Kim1904f832013-02-05 17:56:23 +0900398 int ret;
Milo(Woogyom) Kim6a0c9a42013-02-05 18:03:08 +0900399 struct lp55xx_chip *chip;
400 struct lp55xx_led *led;
401 struct lp55xx_platform_data *pdata = client->dev.platform_data;
Samu Onkalo500fe142010-11-11 14:05:22 -0800402
Milo(Woogyom) Kim6a0c9a42013-02-05 18:03:08 +0900403 if (!pdata) {
Samu Onkalo500fe142010-11-11 14:05:22 -0800404 dev_err(&client->dev, "no platform data\n");
Bryan Wue430dc02012-07-04 11:16:09 +0800405 return -EINVAL;
Samu Onkalo500fe142010-11-11 14:05:22 -0800406 }
407
Milo(Woogyom) Kim6a0c9a42013-02-05 18:03:08 +0900408 chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
409 if (!chip)
410 return -ENOMEM;
Samu Onkalo500fe142010-11-11 14:05:22 -0800411
Milo(Woogyom) Kim6a0c9a42013-02-05 18:03:08 +0900412 led = devm_kzalloc(&client->dev,
413 sizeof(*led) * pdata->num_channels, GFP_KERNEL);
414 if (!led)
415 return -ENOMEM;
416
417 chip->cl = client;
418 chip->pdata = pdata;
Milo(Woogyom) Kim48068d52013-02-05 18:08:49 +0900419 chip->cfg = &lp5521_cfg;
Milo(Woogyom) Kim6a0c9a42013-02-05 18:03:08 +0900420
421 mutex_init(&chip->lock);
422
423 i2c_set_clientdata(client, led);
Samu Onkalo500fe142010-11-11 14:05:22 -0800424
Milo(Woogyom) Kim22ebeb42013-02-05 18:58:35 +0900425 ret = lp55xx_init_device(chip);
Milo(Woogyom) Kim944f7b12013-02-05 17:49:46 +0900426 if (ret)
Milo(Woogyom) Kimf6c64c62013-02-05 17:58:01 +0900427 goto err_init;
Samu Onkalo500fe142010-11-11 14:05:22 -0800428
429 dev_info(&client->dev, "%s programmable led chip found\n", id->name);
430
Milo(Woogyom) Kim9e9b3db2013-02-05 19:06:27 +0900431 ret = lp55xx_register_leds(led, chip);
Milo(Woogyom) Kimf6524802013-02-05 17:53:40 +0900432 if (ret)
Milo(Woogyom) Kim9e9b3db2013-02-05 19:06:27 +0900433 goto err_register_leds;
Samu Onkalo500fe142010-11-11 14:05:22 -0800434
Milo(Woogyom) Kime73c0ce2013-02-05 19:20:45 +0900435 ret = lp55xx_register_sysfs(chip);
Samu Onkalo500fe142010-11-11 14:05:22 -0800436 if (ret) {
437 dev_err(&client->dev, "registering sysfs failed\n");
Milo(Woogyom) Kime73c0ce2013-02-05 19:20:45 +0900438 goto err_register_sysfs;
Samu Onkalo500fe142010-11-11 14:05:22 -0800439 }
Milo(Woogyom) Kime73c0ce2013-02-05 19:20:45 +0900440
441 return 0;
442
443err_register_sysfs:
Milo(Woogyom) Kimc3a68eb2013-02-05 19:11:18 +0900444 lp55xx_unregister_leds(led, chip);
Milo(Woogyom) Kim9e9b3db2013-02-05 19:06:27 +0900445err_register_leds:
Milo(Woogyom) Kim6ce61762013-02-05 19:03:02 +0900446 lp55xx_deinit_device(chip);
Milo(Woogyom) Kimf6c64c62013-02-05 17:58:01 +0900447err_init:
Samu Onkalo500fe142010-11-11 14:05:22 -0800448 return ret;
449}
450
Bill Pemberton678e8a62012-11-19 13:26:00 -0500451static int lp5521_remove(struct i2c_client *client)
Samu Onkalo500fe142010-11-11 14:05:22 -0800452{
Milo(Woogyom) Kim6ce61762013-02-05 19:03:02 +0900453 struct lp55xx_led *led = i2c_get_clientdata(client);
454 struct lp55xx_chip *chip = led->chip;
Samu Onkalo500fe142010-11-11 14:05:22 -0800455
Milo(Woogyom) Kim87cc4bd2013-02-05 19:23:51 +0900456 lp5521_stop_engine(chip);
457 lp55xx_unregister_sysfs(chip);
Milo(Woogyom) Kimc3a68eb2013-02-05 19:11:18 +0900458 lp55xx_unregister_leds(led, chip);
Milo(Woogyom) Kim6ce61762013-02-05 19:03:02 +0900459 lp55xx_deinit_device(chip);
Samu Onkalo500fe142010-11-11 14:05:22 -0800460
Samu Onkalo500fe142010-11-11 14:05:22 -0800461 return 0;
462}
463
464static const struct i2c_device_id lp5521_id[] = {
465 { "lp5521", 0 }, /* Three channel chip */
466 { }
467};
468MODULE_DEVICE_TABLE(i2c, lp5521_id);
469
470static struct i2c_driver lp5521_driver = {
471 .driver = {
472 .name = "lp5521",
473 },
474 .probe = lp5521_probe,
Bill Pembertondf07cf82012-11-19 13:20:20 -0500475 .remove = lp5521_remove,
Samu Onkalo500fe142010-11-11 14:05:22 -0800476 .id_table = lp5521_id,
477};
478
Axel Lin09a0d182012-01-10 15:09:27 -0800479module_i2c_driver(lp5521_driver);
Samu Onkalo500fe142010-11-11 14:05:22 -0800480
481MODULE_AUTHOR("Mathias Nyman, Yuri Zaporozhets, Samu Onkalo");
482MODULE_DESCRIPTION("LP5521 LED engine");
483MODULE_LICENSE("GPL v2");