blob: cf587c1b2c41cfd75e1510fcb7ecc1b6a20d52ea [file] [log] [blame]
Samu Onkalo0efba162010-11-11 14:05:22 -08001/*
2 * lp5523.c - LP5523 LED 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-lp5523.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) Kimdb6eaf82013-02-05 19:18:51 +090038#include <linux/firmware.h>
Milo(Woogyom) Kim6a0c9a42013-02-05 18:03:08 +090039
40#include "leds-lp55xx-common.h"
Samu Onkalo0efba162010-11-11 14:05:22 -080041
42#define LP5523_REG_ENABLE 0x00
43#define LP5523_REG_OP_MODE 0x01
44#define LP5523_REG_RATIOMETRIC_MSB 0x02
45#define LP5523_REG_RATIOMETRIC_LSB 0x03
46#define LP5523_REG_ENABLE_LEDS_MSB 0x04
47#define LP5523_REG_ENABLE_LEDS_LSB 0x05
48#define LP5523_REG_LED_CNTRL_BASE 0x06
49#define LP5523_REG_LED_PWM_BASE 0x16
50#define LP5523_REG_LED_CURRENT_BASE 0x26
51#define LP5523_REG_CONFIG 0x36
52#define LP5523_REG_CHANNEL1_PC 0x37
53#define LP5523_REG_CHANNEL2_PC 0x38
54#define LP5523_REG_CHANNEL3_PC 0x39
55#define LP5523_REG_STATUS 0x3a
56#define LP5523_REG_GPO 0x3b
57#define LP5523_REG_VARIABLE 0x3c
58#define LP5523_REG_RESET 0x3d
59#define LP5523_REG_TEMP_CTRL 0x3e
60#define LP5523_REG_TEMP_READ 0x3f
61#define LP5523_REG_TEMP_WRITE 0x40
62#define LP5523_REG_LED_TEST_CTRL 0x41
63#define LP5523_REG_LED_TEST_ADC 0x42
64#define LP5523_REG_ENG1_VARIABLE 0x45
65#define LP5523_REG_ENG2_VARIABLE 0x46
66#define LP5523_REG_ENG3_VARIABLE 0x47
67#define LP5523_REG_MASTER_FADER1 0x48
68#define LP5523_REG_MASTER_FADER2 0x49
69#define LP5523_REG_MASTER_FADER3 0x4a
70#define LP5523_REG_CH1_PROG_START 0x4c
71#define LP5523_REG_CH2_PROG_START 0x4d
72#define LP5523_REG_CH3_PROG_START 0x4e
73#define LP5523_REG_PROG_PAGE_SEL 0x4f
74#define LP5523_REG_PROG_MEM 0x50
75
76#define LP5523_CMD_LOAD 0x15 /* 00010101 */
77#define LP5523_CMD_RUN 0x2a /* 00101010 */
78#define LP5523_CMD_DISABLED 0x00 /* 00000000 */
79
80#define LP5523_ENABLE 0x40
81#define LP5523_AUTO_INC 0x40
82#define LP5523_PWR_SAVE 0x20
83#define LP5523_PWM_PWR_SAVE 0x04
84#define LP5523_CP_1 0x08
85#define LP5523_CP_1_5 0x10
86#define LP5523_CP_AUTO 0x18
87#define LP5523_INT_CLK 0x01
88#define LP5523_AUTO_CLK 0x02
89#define LP5523_EN_LEDTEST 0x80
90#define LP5523_LEDTEST_DONE 0x80
Milo(Woogyom) Kim48068d52013-02-05 18:08:49 +090091#define LP5523_RESET 0xFF
Samu Onkalo0efba162010-11-11 14:05:22 -080092
93#define LP5523_DEFAULT_CURRENT 50 /* microAmps */
94#define LP5523_PROGRAM_LENGTH 32 /* in bytes */
95#define LP5523_PROGRAM_PAGES 6
96#define LP5523_ADC_SHORTCIRC_LIM 80
97
Milo(Woogyom) Kim0e202342013-02-05 19:07:34 +090098#define LP5523_MAX_LEDS 9
Samu Onkalo0efba162010-11-11 14:05:22 -080099#define LP5523_ENGINES 3
100
101#define LP5523_ENG_MASK_BASE 0x30 /* 00110000 */
102
103#define LP5523_ENG_STATUS_MASK 0x07 /* 00000111 */
104
105#define LP5523_IRQ_FLAGS IRQF_TRIGGER_FALLING
106
107#define LP5523_EXT_CLK_USED 0x08
108
109#define LED_ACTIVE(mux, led) (!!(mux & (0x0001 << led)))
110#define SHIFT_MASK(id) (((id) - 1) * 2)
111
Milo(Woogyom) Kimdb6eaf82013-02-05 19:18:51 +0900112/* Memory Page Selection */
113#define LP5523_PAGE_ENG1 0
114#define LP5523_PAGE_ENG2 1
115#define LP5523_PAGE_ENG3 2
116
117/* Program Memory Operations */
118#define LP5523_MODE_ENG1_M 0x30 /* Operation Mode Register */
119#define LP5523_MODE_ENG2_M 0x0C
120#define LP5523_MODE_ENG3_M 0x03
121#define LP5523_LOAD_ENG1 0x10
122#define LP5523_LOAD_ENG2 0x04
123#define LP5523_LOAD_ENG3 0x01
124
125#define LP5523_ENG1_IS_LOADING(mode) \
126 ((mode & LP5523_MODE_ENG1_M) == LP5523_LOAD_ENG1)
127#define LP5523_ENG2_IS_LOADING(mode) \
128 ((mode & LP5523_MODE_ENG2_M) == LP5523_LOAD_ENG2)
129#define LP5523_ENG3_IS_LOADING(mode) \
130 ((mode & LP5523_MODE_ENG3_M) == LP5523_LOAD_ENG3)
131
132#define LP5523_EXEC_ENG1_M 0x30 /* Enable Register */
133#define LP5523_EXEC_ENG2_M 0x0C
134#define LP5523_EXEC_ENG3_M 0x03
135#define LP5523_EXEC_M 0x3F
136#define LP5523_RUN_ENG1 0x20
137#define LP5523_RUN_ENG2 0x08
138#define LP5523_RUN_ENG3 0x02
139
Kim, Milo27d77042012-09-04 15:06:18 +0800140enum lp5523_chip_id {
141 LP5523,
142 LP55231,
143};
144
Samu Onkalo0efba162010-11-11 14:05:22 -0800145struct lp5523_led {
146 int id;
147 u8 chan_nr;
148 u8 led_current;
149 u8 max_current;
150 struct led_classdev cdev;
151 struct work_struct brightness_work;
152 u8 brightness;
153};
154
155struct lp5523_chip {
156 struct mutex lock; /* Serialize control */
157 struct i2c_client *client;
Milo(Woogyom) Kim0e202342013-02-05 19:07:34 +0900158 struct lp5523_led leds[LP5523_MAX_LEDS];
Samu Onkalo0efba162010-11-11 14:05:22 -0800159 struct lp5523_platform_data *pdata;
160 u8 num_channels;
161 u8 num_leds;
162};
163
Milo(Woogyom) Kimdb6eaf82013-02-05 19:18:51 +0900164static inline void lp5523_wait_opmode_done(void)
165{
166 usleep_range(1000, 2000);
167}
168
Milo(Woogyom) Kima96bfa12013-02-05 19:09:32 +0900169static void lp5523_set_led_current(struct lp55xx_led *led, u8 led_current)
170{
171 led->led_current = led_current;
172 lp55xx_write(led->chip, LP5523_REG_LED_CURRENT_BASE + led->chan_nr,
173 led_current);
174}
175
Milo(Woogyom) Kimffbdccd2013-02-05 18:57:36 +0900176static int lp5523_post_init_device(struct lp55xx_chip *chip)
Samu Onkalo0efba162010-11-11 14:05:22 -0800177{
Milo(Woogyom) Kimffbdccd2013-02-05 18:57:36 +0900178 int ret;
Samu Onkalo0efba162010-11-11 14:05:22 -0800179
Milo(Woogyom) Kimffbdccd2013-02-05 18:57:36 +0900180 ret = lp55xx_write(chip, LP5523_REG_ENABLE, LP5523_ENABLE);
Milo(Woogyom) Kim632418b2013-02-05 18:04:50 +0900181 if (ret)
182 return ret;
Samu Onkalo0efba162010-11-11 14:05:22 -0800183
Samu Onkalo2e4840e2010-11-24 12:57:04 -0800184 /* Chip startup time is 500 us, 1 - 2 ms gives some margin */
185 usleep_range(1000, 2000);
Samu Onkalo0efba162010-11-11 14:05:22 -0800186
Milo(Woogyom) Kimffbdccd2013-02-05 18:57:36 +0900187 ret = lp55xx_write(chip, LP5523_REG_CONFIG,
Samu Onkalo0efba162010-11-11 14:05:22 -0800188 LP5523_AUTO_INC | LP5523_PWR_SAVE |
189 LP5523_CP_AUTO | LP5523_AUTO_CLK |
190 LP5523_PWM_PWR_SAVE);
Milo(Woogyom) Kim632418b2013-02-05 18:04:50 +0900191 if (ret)
Jingoo Han1b21ec52012-10-28 18:45:11 -0700192 return ret;
193
Milo(Woogyom) Kim632418b2013-02-05 18:04:50 +0900194 /* turn on all leds */
Milo(Woogyom) Kimffbdccd2013-02-05 18:57:36 +0900195 ret = lp55xx_write(chip, LP5523_REG_ENABLE_LEDS_MSB, 0x01);
Milo(Woogyom) Kim632418b2013-02-05 18:04:50 +0900196 if (ret)
197 return ret;
Samu Onkalo0efba162010-11-11 14:05:22 -0800198
Milo(Woogyom) Kimffbdccd2013-02-05 18:57:36 +0900199 return lp55xx_write(chip, LP5523_REG_ENABLE_LEDS_LSB, 0xff);
Samu Onkalo0efba162010-11-11 14:05:22 -0800200}
201
Milo(Woogyom) Kimdb6eaf82013-02-05 19:18:51 +0900202static void lp5523_load_engine(struct lp55xx_chip *chip)
Samu Onkalo0efba162010-11-11 14:05:22 -0800203{
Milo(Woogyom) Kimdb6eaf82013-02-05 19:18:51 +0900204 enum lp55xx_engine_index idx = chip->engine_idx;
205 u8 mask[] = {
206 [LP55XX_ENGINE_1] = LP5523_MODE_ENG1_M,
207 [LP55XX_ENGINE_2] = LP5523_MODE_ENG2_M,
208 [LP55XX_ENGINE_3] = LP5523_MODE_ENG3_M,
209 };
Samu Onkalo0efba162010-11-11 14:05:22 -0800210
Milo(Woogyom) Kimdb6eaf82013-02-05 19:18:51 +0900211 u8 val[] = {
212 [LP55XX_ENGINE_1] = LP5523_LOAD_ENG1,
213 [LP55XX_ENGINE_2] = LP5523_LOAD_ENG2,
214 [LP55XX_ENGINE_3] = LP5523_LOAD_ENG3,
215 };
Samu Onkalo0efba162010-11-11 14:05:22 -0800216
Milo(Woogyom) Kimdb6eaf82013-02-05 19:18:51 +0900217 u8 page_sel[] = {
218 [LP55XX_ENGINE_1] = LP5523_PAGE_ENG1,
219 [LP55XX_ENGINE_2] = LP5523_PAGE_ENG2,
220 [LP55XX_ENGINE_3] = LP5523_PAGE_ENG3,
221 };
Samu Onkalo0efba162010-11-11 14:05:22 -0800222
Milo(Woogyom) Kimdb6eaf82013-02-05 19:18:51 +0900223 lp55xx_update_bits(chip, LP5523_REG_OP_MODE, mask[idx], val[idx]);
Samu Onkalo0efba162010-11-11 14:05:22 -0800224
Milo(Woogyom) Kimdb6eaf82013-02-05 19:18:51 +0900225 lp5523_wait_opmode_done();
Samu Onkalo0efba162010-11-11 14:05:22 -0800226
Milo(Woogyom) Kimdb6eaf82013-02-05 19:18:51 +0900227 lp55xx_write(chip, LP5523_REG_PROG_PAGE_SEL, page_sel[idx]);
Samu Onkalo0efba162010-11-11 14:05:22 -0800228}
229
Milo(Woogyom) Kimdb6eaf82013-02-05 19:18:51 +0900230static void lp5523_stop_engine(struct lp55xx_chip *chip)
Samu Onkalo0efba162010-11-11 14:05:22 -0800231{
Milo(Woogyom) Kimdb6eaf82013-02-05 19:18:51 +0900232 lp55xx_write(chip, LP5523_REG_OP_MODE, 0);
233 lp5523_wait_opmode_done();
Samu Onkalo0efba162010-11-11 14:05:22 -0800234}
235
Milo(Woogyom) Kimdb6eaf82013-02-05 19:18:51 +0900236static void lp5523_turn_off_channels(struct lp55xx_chip *chip)
Samu Onkalo0efba162010-11-11 14:05:22 -0800237{
238 int i;
Kim, Milo469eba02012-08-21 17:04:02 +0800239
Milo(Woogyom) Kimdb6eaf82013-02-05 19:18:51 +0900240 for (i = 0; i < LP5523_MAX_LEDS; i++)
241 lp55xx_write(chip, LP5523_REG_LED_PWM_BASE + i, 0);
242}
243
244static void lp5523_run_engine(struct lp55xx_chip *chip, bool start)
245{
246 int ret;
247 u8 mode;
248 u8 exec;
249
250 /* stop engine */
251 if (!start) {
252 lp5523_stop_engine(chip);
253 lp5523_turn_off_channels(chip);
254 return;
Samu Onkalo0efba162010-11-11 14:05:22 -0800255 }
Milo(Woogyom) Kimdb6eaf82013-02-05 19:18:51 +0900256
257 /*
258 * To run the engine,
259 * operation mode and enable register should updated at the same time
260 */
261
262 ret = lp55xx_read(chip, LP5523_REG_OP_MODE, &mode);
263 if (ret)
264 return;
265
266 ret = lp55xx_read(chip, LP5523_REG_ENABLE, &exec);
267 if (ret)
268 return;
269
270 /* change operation mode to RUN only when each engine is loading */
271 if (LP5523_ENG1_IS_LOADING(mode)) {
272 mode = (mode & ~LP5523_MODE_ENG1_M) | LP5523_RUN_ENG1;
273 exec = (exec & ~LP5523_EXEC_ENG1_M) | LP5523_RUN_ENG1;
274 }
275
276 if (LP5523_ENG2_IS_LOADING(mode)) {
277 mode = (mode & ~LP5523_MODE_ENG2_M) | LP5523_RUN_ENG2;
278 exec = (exec & ~LP5523_EXEC_ENG2_M) | LP5523_RUN_ENG2;
279 }
280
281 if (LP5523_ENG3_IS_LOADING(mode)) {
282 mode = (mode & ~LP5523_MODE_ENG3_M) | LP5523_RUN_ENG3;
283 exec = (exec & ~LP5523_EXEC_ENG3_M) | LP5523_RUN_ENG3;
284 }
285
286 lp55xx_write(chip, LP5523_REG_OP_MODE, mode);
287 lp5523_wait_opmode_done();
288
289 lp55xx_update_bits(chip, LP5523_REG_ENABLE, LP5523_EXEC_M, exec);
290}
291
292static int lp5523_update_program_memory(struct lp55xx_chip *chip,
293 const u8 *data, size_t size)
294{
295 u8 pattern[LP5523_PROGRAM_LENGTH] = {0};
296 unsigned cmd;
297 char c[3];
298 int update_size;
299 int nrchars;
300 int offset = 0;
301 int ret;
302 int i;
303
304 /* clear program memory before updating */
305 for (i = 0; i < LP5523_PROGRAM_LENGTH; i++)
306 lp55xx_write(chip, LP5523_REG_PROG_MEM + i, 0);
307
308 i = 0;
309 while ((offset < size - 1) && (i < LP5523_PROGRAM_LENGTH)) {
310 /* separate sscanfs because length is working only for %s */
311 ret = sscanf(data + offset, "%2s%n ", c, &nrchars);
312 if (ret != 1)
313 goto err;
314
315 ret = sscanf(c, "%2x", &cmd);
316 if (ret != 1)
317 goto err;
318
319 pattern[i] = (u8)cmd;
320 offset += nrchars;
321 i++;
322 }
323
324 /* Each instruction is 16bit long. Check that length is even */
325 if (i % 2)
326 goto err;
327
328 update_size = i;
329 for (i = 0; i < update_size; i++)
330 lp55xx_write(chip, LP5523_REG_PROG_MEM + i, pattern[i]);
Samu Onkalo0efba162010-11-11 14:05:22 -0800331
332 return 0;
Milo(Woogyom) Kimdb6eaf82013-02-05 19:18:51 +0900333
334err:
335 dev_err(&chip->cl->dev, "wrong pattern format\n");
336 return -EINVAL;
Samu Onkalo0efba162010-11-11 14:05:22 -0800337}
338
Milo(Woogyom) Kimdb6eaf82013-02-05 19:18:51 +0900339static void lp5523_firmware_loaded(struct lp55xx_chip *chip)
Samu Onkalo0efba162010-11-11 14:05:22 -0800340{
Milo(Woogyom) Kimdb6eaf82013-02-05 19:18:51 +0900341 const struct firmware *fw = chip->fw;
Samu Onkalo0efba162010-11-11 14:05:22 -0800342
Milo(Woogyom) Kimdb6eaf82013-02-05 19:18:51 +0900343 if (fw->size > LP5523_PROGRAM_LENGTH) {
344 dev_err(&chip->cl->dev, "firmware data size overflow: %zu\n",
345 fw->size);
346 return;
347 }
348
349 /*
350 * Program momery sequence
351 * 1) set engine mode to "LOAD"
352 * 2) write firmware data into program memory
353 */
354
355 lp5523_load_engine(chip);
356 lp5523_update_program_memory(chip, fw->data, fw->size);
Samu Onkalo0efba162010-11-11 14:05:22 -0800357}
358
Samu Onkalo0efba162010-11-11 14:05:22 -0800359static ssize_t lp5523_selftest(struct device *dev,
360 struct device_attribute *attr,
361 char *buf)
362{
Milo(Woogyom) Kim9ca3bd82013-02-05 19:21:43 +0900363 struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
364 struct lp55xx_chip *chip = led->chip;
365 struct lp55xx_platform_data *pdata = chip->pdata;
Samu Onkalo0efba162010-11-11 14:05:22 -0800366 int i, ret, pos = 0;
Samu Onkalo0efba162010-11-11 14:05:22 -0800367 u8 status, adc, vdd;
368
369 mutex_lock(&chip->lock);
370
Milo(Woogyom) Kim9ca3bd82013-02-05 19:21:43 +0900371 ret = lp55xx_read(chip, LP5523_REG_STATUS, &status);
Samu Onkalo0efba162010-11-11 14:05:22 -0800372 if (ret < 0)
373 goto fail;
374
375 /* Check that ext clock is really in use if requested */
Milo(Woogyom) Kim9ca3bd82013-02-05 19:21:43 +0900376 if (pdata->clock_mode == LP55XX_CLOCK_EXT) {
Samu Onkalo0efba162010-11-11 14:05:22 -0800377 if ((status & LP5523_EXT_CLK_USED) == 0)
378 goto fail;
Milo(Woogyom) Kim9ca3bd82013-02-05 19:21:43 +0900379 }
Samu Onkalo0efba162010-11-11 14:05:22 -0800380
381 /* Measure VDD (i.e. VBAT) first (channel 16 corresponds to VDD) */
Milo(Woogyom) Kim9ca3bd82013-02-05 19:21:43 +0900382 lp55xx_write(chip, LP5523_REG_LED_TEST_CTRL, LP5523_EN_LEDTEST | 16);
Samu Onkalo2e4840e2010-11-24 12:57:04 -0800383 usleep_range(3000, 6000); /* ADC conversion time is typically 2.7 ms */
Milo(Woogyom) Kim9ca3bd82013-02-05 19:21:43 +0900384 ret = lp55xx_read(chip, LP5523_REG_STATUS, &status);
Jingoo Han1b21ec52012-10-28 18:45:11 -0700385 if (ret < 0)
386 goto fail;
387
Samu Onkalo0efba162010-11-11 14:05:22 -0800388 if (!(status & LP5523_LEDTEST_DONE))
Samu Onkalo2e4840e2010-11-24 12:57:04 -0800389 usleep_range(3000, 6000); /* Was not ready. Wait little bit */
Samu Onkalo0efba162010-11-11 14:05:22 -0800390
Milo(Woogyom) Kim9ca3bd82013-02-05 19:21:43 +0900391 ret = lp55xx_read(chip, LP5523_REG_LED_TEST_ADC, &vdd);
Jingoo Han1b21ec52012-10-28 18:45:11 -0700392 if (ret < 0)
393 goto fail;
394
Samu Onkalo0efba162010-11-11 14:05:22 -0800395 vdd--; /* There may be some fluctuation in measurement */
396
Milo(Woogyom) Kim0e202342013-02-05 19:07:34 +0900397 for (i = 0; i < LP5523_MAX_LEDS; i++) {
Samu Onkalo0efba162010-11-11 14:05:22 -0800398 /* Skip non-existing channels */
Milo(Woogyom) Kim9ca3bd82013-02-05 19:21:43 +0900399 if (pdata->led_config[i].led_current == 0)
Samu Onkalo0efba162010-11-11 14:05:22 -0800400 continue;
401
402 /* Set default current */
Milo(Woogyom) Kim9ca3bd82013-02-05 19:21:43 +0900403 lp55xx_write(chip, LP5523_REG_LED_CURRENT_BASE + i,
404 pdata->led_config[i].led_current);
Samu Onkalo0efba162010-11-11 14:05:22 -0800405
Milo(Woogyom) Kim9ca3bd82013-02-05 19:21:43 +0900406 lp55xx_write(chip, LP5523_REG_LED_PWM_BASE + i, 0xff);
Samu Onkalo2e4840e2010-11-24 12:57:04 -0800407 /* let current stabilize 2 - 4ms before measurements start */
408 usleep_range(2000, 4000);
Milo(Woogyom) Kim9ca3bd82013-02-05 19:21:43 +0900409 lp55xx_write(chip, LP5523_REG_LED_TEST_CTRL,
Samu Onkalo0efba162010-11-11 14:05:22 -0800410 LP5523_EN_LEDTEST | i);
Samu Onkalo2e4840e2010-11-24 12:57:04 -0800411 /* ADC conversion time is 2.7 ms typically */
412 usleep_range(3000, 6000);
Milo(Woogyom) Kim9ca3bd82013-02-05 19:21:43 +0900413 ret = lp55xx_read(chip, LP5523_REG_STATUS, &status);
Jingoo Han1b21ec52012-10-28 18:45:11 -0700414 if (ret < 0)
415 goto fail;
416
Samu Onkalo0efba162010-11-11 14:05:22 -0800417 if (!(status & LP5523_LEDTEST_DONE))
Samu Onkalo2e4840e2010-11-24 12:57:04 -0800418 usleep_range(3000, 6000);/* Was not ready. Wait. */
Milo(Woogyom) Kim9ca3bd82013-02-05 19:21:43 +0900419
420 ret = lp55xx_read(chip, LP5523_REG_LED_TEST_ADC, &adc);
Jingoo Han1b21ec52012-10-28 18:45:11 -0700421 if (ret < 0)
422 goto fail;
Samu Onkalo0efba162010-11-11 14:05:22 -0800423
424 if (adc >= vdd || adc < LP5523_ADC_SHORTCIRC_LIM)
425 pos += sprintf(buf + pos, "LED %d FAIL\n", i);
426
Milo(Woogyom) Kim9ca3bd82013-02-05 19:21:43 +0900427 lp55xx_write(chip, LP5523_REG_LED_PWM_BASE + i, 0x00);
Samu Onkalo0efba162010-11-11 14:05:22 -0800428
429 /* Restore current */
Milo(Woogyom) Kim9ca3bd82013-02-05 19:21:43 +0900430 lp55xx_write(chip, LP5523_REG_LED_CURRENT_BASE + i,
431 led->led_current);
Samu Onkalo0efba162010-11-11 14:05:22 -0800432 led++;
433 }
434 if (pos == 0)
435 pos = sprintf(buf, "OK\n");
436 goto release_lock;
437fail:
438 pos = sprintf(buf, "FAIL\n");
439
440release_lock:
441 mutex_unlock(&chip->lock);
442
443 return pos;
444}
445
Samu Onkalo0efba162010-11-11 14:05:22 -0800446static void lp5523_led_brightness_work(struct work_struct *work)
447{
Milo(Woogyom) Kima6e46792013-02-05 19:08:40 +0900448 struct lp55xx_led *led = container_of(work, struct lp55xx_led,
Samu Onkalo0efba162010-11-11 14:05:22 -0800449 brightness_work);
Milo(Woogyom) Kima6e46792013-02-05 19:08:40 +0900450 struct lp55xx_chip *chip = led->chip;
Samu Onkalo0efba162010-11-11 14:05:22 -0800451
452 mutex_lock(&chip->lock);
Milo(Woogyom) Kima6e46792013-02-05 19:08:40 +0900453 lp55xx_write(chip, LP5523_REG_LED_PWM_BASE + led->chan_nr,
Samu Onkalo0efba162010-11-11 14:05:22 -0800454 led->brightness);
Samu Onkalo0efba162010-11-11 14:05:22 -0800455 mutex_unlock(&chip->lock);
456}
457
Samu Onkalo0efba162010-11-11 14:05:22 -0800458static DEVICE_ATTR(selftest, S_IRUGO, lp5523_selftest, NULL);
459
460static struct attribute *lp5523_attributes[] = {
Samu Onkalo0efba162010-11-11 14:05:22 -0800461 &dev_attr_selftest.attr,
Kim, Milof1625842012-09-12 20:16:03 +0800462 NULL,
Samu Onkalo0efba162010-11-11 14:05:22 -0800463};
464
465static const struct attribute_group lp5523_group = {
466 .attrs = lp5523_attributes,
467};
468
Milo(Woogyom) Kim48068d52013-02-05 18:08:49 +0900469/* Chip specific configurations */
470static struct lp55xx_device_config lp5523_cfg = {
471 .reset = {
472 .addr = LP5523_REG_RESET,
473 .val = LP5523_RESET,
474 },
Milo(Woogyom) Kime3a700d2013-02-05 18:09:56 +0900475 .enable = {
476 .addr = LP5523_REG_ENABLE,
477 .val = LP5523_ENABLE,
478 },
Milo(Woogyom) Kim0e202342013-02-05 19:07:34 +0900479 .max_channel = LP5523_MAX_LEDS,
Milo(Woogyom) Kimffbdccd2013-02-05 18:57:36 +0900480 .post_init_device = lp5523_post_init_device,
Milo(Woogyom) Kima6e46792013-02-05 19:08:40 +0900481 .brightness_work_fn = lp5523_led_brightness_work,
Milo(Woogyom) Kima96bfa12013-02-05 19:09:32 +0900482 .set_led_current = lp5523_set_led_current,
Milo(Woogyom) Kimdb6eaf82013-02-05 19:18:51 +0900483 .firmware_cb = lp5523_firmware_loaded,
484 .run_engine = lp5523_run_engine,
Milo(Woogyom) Kime73c0ce2013-02-05 19:20:45 +0900485 .dev_attr_group = &lp5523_group,
Milo(Woogyom) Kim48068d52013-02-05 18:08:49 +0900486};
487
Bill Pemberton98ea1ea2012-11-19 13:23:02 -0500488static int lp5523_probe(struct i2c_client *client,
Samu Onkalo0efba162010-11-11 14:05:22 -0800489 const struct i2c_device_id *id)
490{
Milo(Woogyom) Kim22ebeb42013-02-05 18:58:35 +0900491 int ret;
Milo(Woogyom) Kim6a0c9a42013-02-05 18:03:08 +0900492 struct lp55xx_chip *chip;
493 struct lp55xx_led *led;
494 struct lp55xx_platform_data *pdata = client->dev.platform_data;
Samu Onkalo0efba162010-11-11 14:05:22 -0800495
Milo(Woogyom) Kim6a0c9a42013-02-05 18:03:08 +0900496 if (!pdata) {
Samu Onkalo0efba162010-11-11 14:05:22 -0800497 dev_err(&client->dev, "no platform data\n");
Bryan Wu94ca4bc2012-07-04 11:44:18 +0800498 return -EINVAL;
Samu Onkalo0efba162010-11-11 14:05:22 -0800499 }
500
Milo(Woogyom) Kim6a0c9a42013-02-05 18:03:08 +0900501 chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
502 if (!chip)
503 return -ENOMEM;
Samu Onkalo0efba162010-11-11 14:05:22 -0800504
Milo(Woogyom) Kim6a0c9a42013-02-05 18:03:08 +0900505 led = devm_kzalloc(&client->dev,
506 sizeof(*led) * pdata->num_channels, GFP_KERNEL);
507 if (!led)
508 return -ENOMEM;
509
510 chip->cl = client;
511 chip->pdata = pdata;
Milo(Woogyom) Kim48068d52013-02-05 18:08:49 +0900512 chip->cfg = &lp5523_cfg;
Milo(Woogyom) Kim6a0c9a42013-02-05 18:03:08 +0900513
514 mutex_init(&chip->lock);
515
516 i2c_set_clientdata(client, led);
Samu Onkalo0efba162010-11-11 14:05:22 -0800517
Milo(Woogyom) Kim22ebeb42013-02-05 18:58:35 +0900518 ret = lp55xx_init_device(chip);
Samu Onkalo0efba162010-11-11 14:05:22 -0800519 if (ret)
Milo(Woogyom) Kimf6c64c62013-02-05 17:58:01 +0900520 goto err_init;
Samu Onkalo0efba162010-11-11 14:05:22 -0800521
Kim, Milo56a1e9a2012-09-04 15:06:26 +0800522 dev_info(&client->dev, "%s Programmable led chip found\n", id->name);
Samu Onkalo0efba162010-11-11 14:05:22 -0800523
Milo(Woogyom) Kim9e9b3db2013-02-05 19:06:27 +0900524 ret = lp55xx_register_leds(led, chip);
Milo(Woogyom) Kimf6524802013-02-05 17:53:40 +0900525 if (ret)
Milo(Woogyom) Kim9e9b3db2013-02-05 19:06:27 +0900526 goto err_register_leds;
Samu Onkalo0efba162010-11-11 14:05:22 -0800527
Milo(Woogyom) Kime73c0ce2013-02-05 19:20:45 +0900528 ret = lp55xx_register_sysfs(chip);
Samu Onkalo0efba162010-11-11 14:05:22 -0800529 if (ret) {
530 dev_err(&client->dev, "registering sysfs failed\n");
Milo(Woogyom) Kime73c0ce2013-02-05 19:20:45 +0900531 goto err_register_sysfs;
Samu Onkalo0efba162010-11-11 14:05:22 -0800532 }
Milo(Woogyom) Kime73c0ce2013-02-05 19:20:45 +0900533
534 return 0;
535
536err_register_sysfs:
Milo(Woogyom) Kimc3a68eb2013-02-05 19:11:18 +0900537 lp55xx_unregister_leds(led, chip);
Milo(Woogyom) Kim9e9b3db2013-02-05 19:06:27 +0900538err_register_leds:
Milo(Woogyom) Kim6ce61762013-02-05 19:03:02 +0900539 lp55xx_deinit_device(chip);
Milo(Woogyom) Kimf6c64c62013-02-05 17:58:01 +0900540err_init:
Samu Onkalo0efba162010-11-11 14:05:22 -0800541 return ret;
542}
543
544static int lp5523_remove(struct i2c_client *client)
545{
Milo(Woogyom) Kim6ce61762013-02-05 19:03:02 +0900546 struct lp55xx_led *led = i2c_get_clientdata(client);
547 struct lp55xx_chip *chip = led->chip;
Samu Onkalo0efba162010-11-11 14:05:22 -0800548
Milo(Woogyom) Kim87cc4bd2013-02-05 19:23:51 +0900549 lp5523_stop_engine(chip);
550 lp55xx_unregister_sysfs(chip);
Milo(Woogyom) Kimc3a68eb2013-02-05 19:11:18 +0900551 lp55xx_unregister_leds(led, chip);
Milo(Woogyom) Kim6ce61762013-02-05 19:03:02 +0900552 lp55xx_deinit_device(chip);
Samu Onkalo0efba162010-11-11 14:05:22 -0800553
Samu Onkalo0efba162010-11-11 14:05:22 -0800554 return 0;
555}
556
557static const struct i2c_device_id lp5523_id[] = {
Kim, Milo27d77042012-09-04 15:06:18 +0800558 { "lp5523", LP5523 },
559 { "lp55231", LP55231 },
Samu Onkalo0efba162010-11-11 14:05:22 -0800560 { }
561};
562
563MODULE_DEVICE_TABLE(i2c, lp5523_id);
564
565static struct i2c_driver lp5523_driver = {
566 .driver = {
Kim, Milo27d77042012-09-04 15:06:18 +0800567 .name = "lp5523x",
Samu Onkalo0efba162010-11-11 14:05:22 -0800568 },
569 .probe = lp5523_probe,
570 .remove = lp5523_remove,
571 .id_table = lp5523_id,
572};
573
Axel Lin09a0d182012-01-10 15:09:27 -0800574module_i2c_driver(lp5523_driver);
Samu Onkalo0efba162010-11-11 14:05:22 -0800575
576MODULE_AUTHOR("Mathias Nyman <mathias.nyman@nokia.com>");
577MODULE_DESCRIPTION("LP5523 LED engine");
578MODULE_LICENSE("GPL");