blob: b2a0d877cdc1fef37f45424f943298ad5c1de67a [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
23#include <linux/module.h>
24#include <linux/init.h>
25#include <linux/i2c.h>
26#include <linux/mutex.h>
27#include <linux/gpio.h>
28#include <linux/interrupt.h>
29#include <linux/delay.h>
30#include <linux/ctype.h>
31#include <linux/spinlock.h>
32#include <linux/wait.h>
33#include <linux/leds.h>
34#include <linux/leds-lp5521.h>
35#include <linux/workqueue.h>
36#include <linux/slab.h>
Milo(Woogyom) Kim6a0c9a42013-02-05 18:03:08 +090037#include <linux/platform_data/leds-lp55xx.h>
Milo(Woogyom) Kim9ce7cb12013-02-05 19:18:10 +090038#include <linux/firmware.h>
Milo(Woogyom) Kim6a0c9a42013-02-05 18:03:08 +090039
40#include "leds-lp55xx-common.h"
Samu Onkalo500fe142010-11-11 14:05:22 -080041
Milo(Woogyom) Kim12f022d2013-02-05 19:25:26 +090042#define LP5521_PROGRAM_LENGTH 32
43#define LP5521_MAX_LEDS 3
44#define LP5521_CMD_DIRECT 0x3F
Samu Onkalo500fe142010-11-11 14:05:22 -080045
46/* Registers */
47#define LP5521_REG_ENABLE 0x00
48#define LP5521_REG_OP_MODE 0x01
49#define LP5521_REG_R_PWM 0x02
50#define LP5521_REG_G_PWM 0x03
51#define LP5521_REG_B_PWM 0x04
52#define LP5521_REG_R_CURRENT 0x05
53#define LP5521_REG_G_CURRENT 0x06
54#define LP5521_REG_B_CURRENT 0x07
55#define LP5521_REG_CONFIG 0x08
Samu Onkalo500fe142010-11-11 14:05:22 -080056#define LP5521_REG_STATUS 0x0C
57#define LP5521_REG_RESET 0x0D
Samu Onkalo500fe142010-11-11 14:05:22 -080058#define LP5521_REG_R_PROG_MEM 0x10
59#define LP5521_REG_G_PROG_MEM 0x30
60#define LP5521_REG_B_PROG_MEM 0x50
61
Samu Onkalo500fe142010-11-11 14:05:22 -080062/* Base register to set LED current */
63#define LP5521_REG_LED_CURRENT_BASE LP5521_REG_R_CURRENT
Samu Onkalo500fe142010-11-11 14:05:22 -080064/* Base register to set the brightness */
65#define LP5521_REG_LED_PWM_BASE LP5521_REG_R_PWM
66
67/* Bits in ENABLE register */
68#define LP5521_MASTER_ENABLE 0x40 /* Chip master enable */
69#define LP5521_LOGARITHMIC_PWM 0x80 /* Logarithmic PWM adjustment */
70#define LP5521_EXEC_RUN 0x2A
Kim, Milo32a2f742012-03-23 15:02:09 -070071#define LP5521_ENABLE_DEFAULT \
72 (LP5521_MASTER_ENABLE | LP5521_LOGARITHMIC_PWM)
73#define LP5521_ENABLE_RUN_PROGRAM \
74 (LP5521_ENABLE_DEFAULT | LP5521_EXEC_RUN)
Samu Onkalo500fe142010-11-11 14:05:22 -080075
Samu Onkalo500fe142010-11-11 14:05:22 -080076/* Status */
77#define LP5521_EXT_CLK_USED 0x08
78
Srinidhi KASAGARb3c49c02011-10-31 17:12:24 -070079/* default R channel current register value */
80#define LP5521_REG_R_CURR_DEFAULT 0xAF
81
Milo(Woogyom) Kim48068d52013-02-05 18:08:49 +090082/* Reset register value */
83#define LP5521_RESET 0xFF
84
Milo(Woogyom) Kim9ce7cb12013-02-05 19:18:10 +090085/* Program Memory Operations */
86#define LP5521_MODE_R_M 0x30 /* Operation Mode Register */
87#define LP5521_MODE_G_M 0x0C
88#define LP5521_MODE_B_M 0x03
89#define LP5521_LOAD_R 0x10
90#define LP5521_LOAD_G 0x04
91#define LP5521_LOAD_B 0x01
92
93#define LP5521_R_IS_LOADING(mode) \
94 ((mode & LP5521_MODE_R_M) == LP5521_LOAD_R)
95#define LP5521_G_IS_LOADING(mode) \
96 ((mode & LP5521_MODE_G_M) == LP5521_LOAD_G)
97#define LP5521_B_IS_LOADING(mode) \
98 ((mode & LP5521_MODE_B_M) == LP5521_LOAD_B)
99
100#define LP5521_EXEC_R_M 0x30 /* Enable Register */
101#define LP5521_EXEC_G_M 0x0C
102#define LP5521_EXEC_B_M 0x03
103#define LP5521_EXEC_M 0x3F
104#define LP5521_RUN_R 0x20
105#define LP5521_RUN_G 0x08
106#define LP5521_RUN_B 0x02
Samu Onkalo500fe142010-11-11 14:05:22 -0800107
Milo(Woogyom) Kim9ce7cb12013-02-05 19:18:10 +0900108static inline void lp5521_wait_opmode_done(void)
109{
110 /* operation mode change needs to be longer than 153 us */
111 usleep_range(200, 300);
112}
113
Milo(Woogyom) Kim94482172013-02-05 18:03:55 +0900114static inline void lp5521_wait_enable_done(void)
115{
116 /* it takes more 488 us to update ENABLE register */
117 usleep_range(500, 600);
118}
119
Milo(Woogyom) Kima96bfa12013-02-05 19:09:32 +0900120static void lp5521_set_led_current(struct lp55xx_led *led, u8 led_current)
121{
122 led->led_current = led_current;
123 lp55xx_write(led->chip, LP5521_REG_LED_CURRENT_BASE + led->chan_nr,
124 led_current);
125}
126
Milo(Woogyom) Kim9ce7cb12013-02-05 19:18:10 +0900127static void lp5521_load_engine(struct lp55xx_chip *chip)
Samu Onkalo500fe142010-11-11 14:05:22 -0800128{
Milo(Woogyom) Kim9ce7cb12013-02-05 19:18:10 +0900129 enum lp55xx_engine_index idx = chip->engine_idx;
130 u8 mask[] = {
131 [LP55XX_ENGINE_1] = LP5521_MODE_R_M,
132 [LP55XX_ENGINE_2] = LP5521_MODE_G_M,
133 [LP55XX_ENGINE_3] = LP5521_MODE_B_M,
134 };
Samu Onkalo500fe142010-11-11 14:05:22 -0800135
Milo(Woogyom) Kim9ce7cb12013-02-05 19:18:10 +0900136 u8 val[] = {
137 [LP55XX_ENGINE_1] = LP5521_LOAD_R,
138 [LP55XX_ENGINE_2] = LP5521_LOAD_G,
139 [LP55XX_ENGINE_3] = LP5521_LOAD_B,
140 };
Samu Onkalo500fe142010-11-11 14:05:22 -0800141
Milo(Woogyom) Kim9ce7cb12013-02-05 19:18:10 +0900142 lp55xx_update_bits(chip, LP5521_REG_OP_MODE, mask[idx], val[idx]);
Samu Onkalo500fe142010-11-11 14:05:22 -0800143
Milo(Woogyom) Kim9ce7cb12013-02-05 19:18:10 +0900144 lp5521_wait_opmode_done();
Samu Onkalo500fe142010-11-11 14:05:22 -0800145}
146
Milo(Woogyom) Kim9ce7cb12013-02-05 19:18:10 +0900147static void lp5521_stop_engine(struct lp55xx_chip *chip)
Samu Onkalo500fe142010-11-11 14:05:22 -0800148{
Milo(Woogyom) Kim9ce7cb12013-02-05 19:18:10 +0900149 lp55xx_write(chip, LP5521_REG_OP_MODE, 0);
150 lp5521_wait_opmode_done();
151}
152
153static void lp5521_run_engine(struct lp55xx_chip *chip, bool start)
154{
Samu Onkalo500fe142010-11-11 14:05:22 -0800155 int ret;
Samu Onkalo500fe142010-11-11 14:05:22 -0800156 u8 mode;
Milo(Woogyom) Kim9ce7cb12013-02-05 19:18:10 +0900157 u8 exec;
Samu Onkalo500fe142010-11-11 14:05:22 -0800158
Milo(Woogyom) Kim9ce7cb12013-02-05 19:18:10 +0900159 /* stop engine */
160 if (!start) {
161 lp5521_stop_engine(chip);
162 lp55xx_write(chip, LP5521_REG_OP_MODE, LP5521_CMD_DIRECT);
163 lp5521_wait_opmode_done();
164 return;
165 }
166
167 /*
168 * To run the engine,
169 * operation mode and enable register should updated at the same time
170 */
171
172 ret = lp55xx_read(chip, LP5521_REG_OP_MODE, &mode);
Dan Carpenter5bc9ad72012-05-29 15:07:26 -0700173 if (ret)
Milo(Woogyom) Kim9ce7cb12013-02-05 19:18:10 +0900174 return;
Dan Carpenter5bc9ad72012-05-29 15:07:26 -0700175
Milo(Woogyom) Kim9ce7cb12013-02-05 19:18:10 +0900176 ret = lp55xx_read(chip, LP5521_REG_ENABLE, &exec);
Dan Carpenter5bc9ad72012-05-29 15:07:26 -0700177 if (ret)
Milo(Woogyom) Kim9ce7cb12013-02-05 19:18:10 +0900178 return;
Samu Onkalo500fe142010-11-11 14:05:22 -0800179
Milo(Woogyom) Kim9ce7cb12013-02-05 19:18:10 +0900180 /* change operation mode to RUN only when each engine is loading */
181 if (LP5521_R_IS_LOADING(mode)) {
182 mode = (mode & ~LP5521_MODE_R_M) | LP5521_RUN_R;
183 exec = (exec & ~LP5521_EXEC_R_M) | LP5521_RUN_R;
184 }
Samu Onkalo500fe142010-11-11 14:05:22 -0800185
Milo(Woogyom) Kim9ce7cb12013-02-05 19:18:10 +0900186 if (LP5521_G_IS_LOADING(mode)) {
187 mode = (mode & ~LP5521_MODE_G_M) | LP5521_RUN_G;
188 exec = (exec & ~LP5521_EXEC_G_M) | LP5521_RUN_G;
189 }
Samu Onkalo500fe142010-11-11 14:05:22 -0800190
Milo(Woogyom) Kim9ce7cb12013-02-05 19:18:10 +0900191 if (LP5521_B_IS_LOADING(mode)) {
192 mode = (mode & ~LP5521_MODE_B_M) | LP5521_RUN_B;
193 exec = (exec & ~LP5521_EXEC_B_M) | LP5521_RUN_B;
194 }
195
196 lp55xx_write(chip, LP5521_REG_OP_MODE, mode);
197 lp5521_wait_opmode_done();
198
199 lp55xx_update_bits(chip, LP5521_REG_ENABLE, LP5521_EXEC_M, exec);
200 lp5521_wait_enable_done();
201}
202
203static int lp5521_update_program_memory(struct lp55xx_chip *chip,
204 const u8 *data, size_t size)
205{
206 enum lp55xx_engine_index idx = chip->engine_idx;
207 u8 pattern[LP5521_PROGRAM_LENGTH] = {0};
208 u8 addr[] = {
209 [LP55XX_ENGINE_1] = LP5521_REG_R_PROG_MEM,
210 [LP55XX_ENGINE_2] = LP5521_REG_G_PROG_MEM,
211 [LP55XX_ENGINE_3] = LP5521_REG_B_PROG_MEM,
212 };
213 unsigned cmd;
214 char c[3];
215 int program_size;
216 int nrchars;
217 int offset = 0;
218 int ret;
219 int i;
220
221 /* clear program memory before updating */
222 for (i = 0; i < LP5521_PROGRAM_LENGTH; i++)
223 lp55xx_write(chip, addr[idx] + i, 0);
224
225 i = 0;
226 while ((offset < size - 1) && (i < LP5521_PROGRAM_LENGTH)) {
227 /* separate sscanfs because length is working only for %s */
228 ret = sscanf(data + offset, "%2s%n ", c, &nrchars);
229 if (ret != 1)
230 goto err;
231
232 ret = sscanf(c, "%2x", &cmd);
233 if (ret != 1)
234 goto err;
235
236 pattern[i] = (u8)cmd;
237 offset += nrchars;
238 i++;
239 }
240
241 /* Each instruction is 16bit long. Check that length is even */
242 if (i % 2)
243 goto err;
244
245 program_size = i;
246 for (i = 0; i < program_size; i++)
247 lp55xx_write(chip, addr[idx] + i, pattern[i]);
248
249 return 0;
250
251err:
252 dev_err(&chip->cl->dev, "wrong pattern format\n");
253 return -EINVAL;
254}
255
256static void lp5521_firmware_loaded(struct lp55xx_chip *chip)
257{
258 const struct firmware *fw = chip->fw;
259
260 if (fw->size > LP5521_PROGRAM_LENGTH) {
261 dev_err(&chip->cl->dev, "firmware data size overflow: %zu\n",
262 fw->size);
263 return;
264 }
265
266 /*
267 * Program momery sequence
268 * 1) set engine mode to "LOAD"
269 * 2) write firmware data into program memory
270 */
271
272 lp5521_load_engine(chip);
273 lp5521_update_program_memory(chip, fw->data, fw->size);
Samu Onkalo500fe142010-11-11 14:05:22 -0800274}
275
Milo(Woogyom) Kimffbdccd2013-02-05 18:57:36 +0900276static int lp5521_post_init_device(struct lp55xx_chip *chip)
Samu Onkalo500fe142010-11-11 14:05:22 -0800277{
Samu Onkalo500fe142010-11-11 14:05:22 -0800278 int ret;
Milo(Woogyom) Kim94482172013-02-05 18:03:55 +0900279 u8 val;
Samu Onkalo500fe142010-11-11 14:05:22 -0800280
Milo(Woogyom) Kim94482172013-02-05 18:03:55 +0900281 /*
282 * Make sure that the chip is reset by reading back the r channel
283 * current reg. This is dummy read is required on some platforms -
284 * otherwise further access to the R G B channels in the
285 * LP5521_REG_ENABLE register will not have any effect - strange!
286 */
Milo(Woogyom) Kimffbdccd2013-02-05 18:57:36 +0900287 ret = lp55xx_read(chip, LP5521_REG_R_CURRENT, &val);
Milo(Woogyom) Kim94482172013-02-05 18:03:55 +0900288 if (ret) {
Milo(Woogyom) Kimffbdccd2013-02-05 18:57:36 +0900289 dev_err(&chip->cl->dev, "error in resetting chip\n");
Milo(Woogyom) Kim94482172013-02-05 18:03:55 +0900290 return ret;
291 }
292 if (val != LP5521_REG_R_CURR_DEFAULT) {
Milo(Woogyom) Kimffbdccd2013-02-05 18:57:36 +0900293 dev_err(&chip->cl->dev,
Milo(Woogyom) Kim94482172013-02-05 18:03:55 +0900294 "unexpected data in register (expected 0x%x got 0x%x)\n",
295 LP5521_REG_R_CURR_DEFAULT, val);
296 ret = -EINVAL;
297 return ret;
298 }
299 usleep_range(10000, 20000);
Samu Onkalo500fe142010-11-11 14:05:22 -0800300
Samu Onkalo500fe142010-11-11 14:05:22 -0800301 /* Set all PWMs to direct control mode */
Milo(Woogyom) Kimffbdccd2013-02-05 18:57:36 +0900302 ret = lp55xx_write(chip, LP5521_REG_OP_MODE, LP5521_CMD_DIRECT);
Samu Onkalo500fe142010-11-11 14:05:22 -0800303
Milo(Woogyom) Kimffbdccd2013-02-05 18:57:36 +0900304 val = chip->pdata->update_config ?
Kim, Milo3b49aac2012-03-23 15:02:08 -0700305 : (LP5521_PWRSAVE_EN | LP5521_CP_MODE_AUTO | LP5521_R_TO_BATT);
Milo(Woogyom) Kimffbdccd2013-02-05 18:57:36 +0900306 ret = lp55xx_write(chip, LP5521_REG_CONFIG, val);
Milo(Woogyom) Kim94482172013-02-05 18:03:55 +0900307 if (ret)
308 return ret;
Samu Onkalo500fe142010-11-11 14:05:22 -0800309
310 /* Initialize all channels PWM to zero -> leds off */
Milo(Woogyom) Kimffbdccd2013-02-05 18:57:36 +0900311 lp55xx_write(chip, LP5521_REG_R_PWM, 0);
312 lp55xx_write(chip, LP5521_REG_G_PWM, 0);
313 lp55xx_write(chip, LP5521_REG_B_PWM, 0);
Samu Onkalo500fe142010-11-11 14:05:22 -0800314
315 /* Set engines are set to run state when OP_MODE enables engines */
Milo(Woogyom) Kimffbdccd2013-02-05 18:57:36 +0900316 ret = lp55xx_write(chip, LP5521_REG_ENABLE, LP5521_ENABLE_RUN_PROGRAM);
Milo(Woogyom) Kim94482172013-02-05 18:03:55 +0900317 if (ret)
318 return ret;
Samu Onkalo500fe142010-11-11 14:05:22 -0800319
Milo(Woogyom) Kim94482172013-02-05 18:03:55 +0900320 lp5521_wait_enable_done();
321
322 return 0;
Samu Onkalo500fe142010-11-11 14:05:22 -0800323}
324
Milo(Woogyom) Kim9ca3bd82013-02-05 19:21:43 +0900325static int lp5521_run_selftest(struct lp55xx_chip *chip, char *buf)
Samu Onkalo500fe142010-11-11 14:05:22 -0800326{
Milo(Woogyom) Kim9ca3bd82013-02-05 19:21:43 +0900327 struct lp55xx_platform_data *pdata = chip->pdata;
Samu Onkalo500fe142010-11-11 14:05:22 -0800328 int ret;
329 u8 status;
330
Milo(Woogyom) Kim9ca3bd82013-02-05 19:21:43 +0900331 ret = lp55xx_read(chip, LP5521_REG_STATUS, &status);
Samu Onkalo500fe142010-11-11 14:05:22 -0800332 if (ret < 0)
333 return ret;
334
Milo(Woogyom) Kim9ca3bd82013-02-05 19:21:43 +0900335 if (pdata->clock_mode != LP55XX_CLOCK_EXT)
336 return 0;
337
Samu Onkalo500fe142010-11-11 14:05:22 -0800338 /* Check that ext clock is really in use if requested */
Milo(Woogyom) Kim9ca3bd82013-02-05 19:21:43 +0900339 if ((status & LP5521_EXT_CLK_USED) == 0)
340 return -EIO;
341
Samu Onkalo500fe142010-11-11 14:05:22 -0800342 return 0;
343}
344
Samu Onkalo500fe142010-11-11 14:05:22 -0800345static void lp5521_led_brightness_work(struct work_struct *work)
346{
Milo(Woogyom) Kima6e46792013-02-05 19:08:40 +0900347 struct lp55xx_led *led = container_of(work, struct lp55xx_led,
Samu Onkalo500fe142010-11-11 14:05:22 -0800348 brightness_work);
Milo(Woogyom) Kima6e46792013-02-05 19:08:40 +0900349 struct lp55xx_chip *chip = led->chip;
Samu Onkalo500fe142010-11-11 14:05:22 -0800350
351 mutex_lock(&chip->lock);
Milo(Woogyom) Kima6e46792013-02-05 19:08:40 +0900352 lp55xx_write(chip, LP5521_REG_LED_PWM_BASE + led->chan_nr,
Samu Onkalo500fe142010-11-11 14:05:22 -0800353 led->brightness);
354 mutex_unlock(&chip->lock);
355}
356
Samu Onkalo500fe142010-11-11 14:05:22 -0800357static ssize_t lp5521_selftest(struct device *dev,
358 struct device_attribute *attr,
359 char *buf)
360{
Milo(Woogyom) Kim9ca3bd82013-02-05 19:21:43 +0900361 struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
362 struct lp55xx_chip *chip = led->chip;
Samu Onkalo500fe142010-11-11 14:05:22 -0800363 int ret;
364
365 mutex_lock(&chip->lock);
366 ret = lp5521_run_selftest(chip, buf);
367 mutex_unlock(&chip->lock);
368 return sprintf(buf, "%s\n", ret ? "FAIL" : "OK");
369}
370
Samu Onkalo500fe142010-11-11 14:05:22 -0800371/* device attributes */
Samu Onkalo500fe142010-11-11 14:05:22 -0800372static DEVICE_ATTR(selftest, S_IRUGO, lp5521_selftest, NULL);
373
374static struct attribute *lp5521_attributes[] = {
Samu Onkalo500fe142010-11-11 14:05:22 -0800375 &dev_attr_selftest.attr,
Samu Onkalo500fe142010-11-11 14:05:22 -0800376 NULL
377};
378
379static const struct attribute_group lp5521_group = {
380 .attrs = lp5521_attributes,
381};
382
Milo(Woogyom) Kim48068d52013-02-05 18:08:49 +0900383/* Chip specific configurations */
384static struct lp55xx_device_config lp5521_cfg = {
385 .reset = {
386 .addr = LP5521_REG_RESET,
387 .val = LP5521_RESET,
388 },
Milo(Woogyom) Kime3a700d2013-02-05 18:09:56 +0900389 .enable = {
390 .addr = LP5521_REG_ENABLE,
391 .val = LP5521_ENABLE_DEFAULT,
392 },
Milo(Woogyom) Kim0e202342013-02-05 19:07:34 +0900393 .max_channel = LP5521_MAX_LEDS,
Milo(Woogyom) Kimffbdccd2013-02-05 18:57:36 +0900394 .post_init_device = lp5521_post_init_device,
Milo(Woogyom) Kima6e46792013-02-05 19:08:40 +0900395 .brightness_work_fn = lp5521_led_brightness_work,
Milo(Woogyom) Kima96bfa12013-02-05 19:09:32 +0900396 .set_led_current = lp5521_set_led_current,
Milo(Woogyom) Kim9ce7cb12013-02-05 19:18:10 +0900397 .firmware_cb = lp5521_firmware_loaded,
398 .run_engine = lp5521_run_engine,
Milo(Woogyom) Kime73c0ce2013-02-05 19:20:45 +0900399 .dev_attr_group = &lp5521_group,
Milo(Woogyom) Kim48068d52013-02-05 18:08:49 +0900400};
401
Bill Pemberton98ea1ea2012-11-19 13:23:02 -0500402static int lp5521_probe(struct i2c_client *client,
Samu Onkalo500fe142010-11-11 14:05:22 -0800403 const struct i2c_device_id *id)
404{
Milo(Woogyom) Kim1904f832013-02-05 17:56:23 +0900405 int ret;
Milo(Woogyom) Kim6a0c9a42013-02-05 18:03:08 +0900406 struct lp55xx_chip *chip;
407 struct lp55xx_led *led;
408 struct lp55xx_platform_data *pdata = client->dev.platform_data;
Samu Onkalo500fe142010-11-11 14:05:22 -0800409
Milo(Woogyom) Kim6a0c9a42013-02-05 18:03:08 +0900410 if (!pdata) {
Samu Onkalo500fe142010-11-11 14:05:22 -0800411 dev_err(&client->dev, "no platform data\n");
Bryan Wue430dc02012-07-04 11:16:09 +0800412 return -EINVAL;
Samu Onkalo500fe142010-11-11 14:05:22 -0800413 }
414
Milo(Woogyom) Kim6a0c9a42013-02-05 18:03:08 +0900415 chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
416 if (!chip)
417 return -ENOMEM;
Samu Onkalo500fe142010-11-11 14:05:22 -0800418
Milo(Woogyom) Kim6a0c9a42013-02-05 18:03:08 +0900419 led = devm_kzalloc(&client->dev,
420 sizeof(*led) * pdata->num_channels, GFP_KERNEL);
421 if (!led)
422 return -ENOMEM;
423
424 chip->cl = client;
425 chip->pdata = pdata;
Milo(Woogyom) Kim48068d52013-02-05 18:08:49 +0900426 chip->cfg = &lp5521_cfg;
Milo(Woogyom) Kim6a0c9a42013-02-05 18:03:08 +0900427
428 mutex_init(&chip->lock);
429
430 i2c_set_clientdata(client, led);
Samu Onkalo500fe142010-11-11 14:05:22 -0800431
Milo(Woogyom) Kim22ebeb42013-02-05 18:58:35 +0900432 ret = lp55xx_init_device(chip);
Milo(Woogyom) Kim944f7b12013-02-05 17:49:46 +0900433 if (ret)
Milo(Woogyom) Kimf6c64c62013-02-05 17:58:01 +0900434 goto err_init;
Samu Onkalo500fe142010-11-11 14:05:22 -0800435
436 dev_info(&client->dev, "%s programmable led chip found\n", id->name);
437
Milo(Woogyom) Kim9e9b3db2013-02-05 19:06:27 +0900438 ret = lp55xx_register_leds(led, chip);
Milo(Woogyom) Kimf6524802013-02-05 17:53:40 +0900439 if (ret)
Milo(Woogyom) Kim9e9b3db2013-02-05 19:06:27 +0900440 goto err_register_leds;
Samu Onkalo500fe142010-11-11 14:05:22 -0800441
Milo(Woogyom) Kime73c0ce2013-02-05 19:20:45 +0900442 ret = lp55xx_register_sysfs(chip);
Samu Onkalo500fe142010-11-11 14:05:22 -0800443 if (ret) {
444 dev_err(&client->dev, "registering sysfs failed\n");
Milo(Woogyom) Kime73c0ce2013-02-05 19:20:45 +0900445 goto err_register_sysfs;
Samu Onkalo500fe142010-11-11 14:05:22 -0800446 }
Milo(Woogyom) Kime73c0ce2013-02-05 19:20:45 +0900447
448 return 0;
449
450err_register_sysfs:
Milo(Woogyom) Kimc3a68eb2013-02-05 19:11:18 +0900451 lp55xx_unregister_leds(led, chip);
Milo(Woogyom) Kim9e9b3db2013-02-05 19:06:27 +0900452err_register_leds:
Milo(Woogyom) Kim6ce61762013-02-05 19:03:02 +0900453 lp55xx_deinit_device(chip);
Milo(Woogyom) Kimf6c64c62013-02-05 17:58:01 +0900454err_init:
Samu Onkalo500fe142010-11-11 14:05:22 -0800455 return ret;
456}
457
Bill Pemberton678e8a62012-11-19 13:26:00 -0500458static int lp5521_remove(struct i2c_client *client)
Samu Onkalo500fe142010-11-11 14:05:22 -0800459{
Milo(Woogyom) Kim6ce61762013-02-05 19:03:02 +0900460 struct lp55xx_led *led = i2c_get_clientdata(client);
461 struct lp55xx_chip *chip = led->chip;
Samu Onkalo500fe142010-11-11 14:05:22 -0800462
Milo(Woogyom) Kim87cc4bd2013-02-05 19:23:51 +0900463 lp5521_stop_engine(chip);
464 lp55xx_unregister_sysfs(chip);
Milo(Woogyom) Kimc3a68eb2013-02-05 19:11:18 +0900465 lp55xx_unregister_leds(led, chip);
Milo(Woogyom) Kim6ce61762013-02-05 19:03:02 +0900466 lp55xx_deinit_device(chip);
Samu Onkalo500fe142010-11-11 14:05:22 -0800467
Samu Onkalo500fe142010-11-11 14:05:22 -0800468 return 0;
469}
470
471static const struct i2c_device_id lp5521_id[] = {
472 { "lp5521", 0 }, /* Three channel chip */
473 { }
474};
475MODULE_DEVICE_TABLE(i2c, lp5521_id);
476
477static struct i2c_driver lp5521_driver = {
478 .driver = {
479 .name = "lp5521",
480 },
481 .probe = lp5521_probe,
Bill Pembertondf07cf82012-11-19 13:20:20 -0500482 .remove = lp5521_remove,
Samu Onkalo500fe142010-11-11 14:05:22 -0800483 .id_table = lp5521_id,
484};
485
Axel Lin09a0d182012-01-10 15:09:27 -0800486module_i2c_driver(lp5521_driver);
Samu Onkalo500fe142010-11-11 14:05:22 -0800487
488MODULE_AUTHOR("Mathias Nyman, Yuri Zaporozhets, Samu Onkalo");
489MODULE_DESCRIPTION("LP5521 LED engine");
490MODULE_LICENSE("GPL v2");