blob: d44c001e5e73204f5ecf8bad0de3e857bb431a19 [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
Samu Onkalo0efba162010-11-11 14:05:22 -080023#include <linux/delay.h>
Milo(Woogyom) Kimdb6eaf82013-02-05 19:18:51 +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 Onkalo0efba162010-11-11 14:05:22 -080034
Milo(Woogyom) Kim12f022d2013-02-05 19:25:26 +090035#define LP5523_PROGRAM_LENGTH 32
36#define LP5523_MAX_LEDS 9
37
38/* Registers */
Samu Onkalo0efba162010-11-11 14:05:22 -080039#define LP5523_REG_ENABLE 0x00
40#define LP5523_REG_OP_MODE 0x01
Samu Onkalo0efba162010-11-11 14:05:22 -080041#define LP5523_REG_ENABLE_LEDS_MSB 0x04
42#define LP5523_REG_ENABLE_LEDS_LSB 0x05
Samu Onkalo0efba162010-11-11 14:05:22 -080043#define LP5523_REG_LED_PWM_BASE 0x16
44#define LP5523_REG_LED_CURRENT_BASE 0x26
45#define LP5523_REG_CONFIG 0x36
Milo(Woogyom) Kim12f022d2013-02-05 19:25:26 +090046#define LP5523_REG_STATUS 0x3A
47#define LP5523_REG_RESET 0x3D
Samu Onkalo0efba162010-11-11 14:05:22 -080048#define LP5523_REG_LED_TEST_CTRL 0x41
49#define LP5523_REG_LED_TEST_ADC 0x42
Milo(Woogyom) Kim12f022d2013-02-05 19:25:26 +090050#define LP5523_REG_PROG_PAGE_SEL 0x4F
Samu Onkalo0efba162010-11-11 14:05:22 -080051#define LP5523_REG_PROG_MEM 0x50
52
Milo(Woogyom) Kim12f022d2013-02-05 19:25:26 +090053/* Bit description in registers */
Samu Onkalo0efba162010-11-11 14:05:22 -080054#define LP5523_ENABLE 0x40
55#define LP5523_AUTO_INC 0x40
56#define LP5523_PWR_SAVE 0x20
57#define LP5523_PWM_PWR_SAVE 0x04
Samu Onkalo0efba162010-11-11 14:05:22 -080058#define LP5523_CP_AUTO 0x18
Samu Onkalo0efba162010-11-11 14:05:22 -080059#define LP5523_AUTO_CLK 0x02
Milo(Woogyom) Kim12f022d2013-02-05 19:25:26 +090060
Samu Onkalo0efba162010-11-11 14:05:22 -080061#define LP5523_EN_LEDTEST 0x80
62#define LP5523_LEDTEST_DONE 0x80
Milo(Woogyom) Kim48068d52013-02-05 18:08:49 +090063#define LP5523_RESET 0xFF
Samu Onkalo0efba162010-11-11 14:05:22 -080064#define LP5523_ADC_SHORTCIRC_LIM 80
Samu Onkalo0efba162010-11-11 14:05:22 -080065#define LP5523_EXT_CLK_USED 0x08
66
Milo(Woogyom) Kimdb6eaf82013-02-05 19:18:51 +090067/* Memory Page Selection */
68#define LP5523_PAGE_ENG1 0
69#define LP5523_PAGE_ENG2 1
70#define LP5523_PAGE_ENG3 2
71
72/* Program Memory Operations */
73#define LP5523_MODE_ENG1_M 0x30 /* Operation Mode Register */
74#define LP5523_MODE_ENG2_M 0x0C
75#define LP5523_MODE_ENG3_M 0x03
76#define LP5523_LOAD_ENG1 0x10
77#define LP5523_LOAD_ENG2 0x04
78#define LP5523_LOAD_ENG3 0x01
79
80#define LP5523_ENG1_IS_LOADING(mode) \
81 ((mode & LP5523_MODE_ENG1_M) == LP5523_LOAD_ENG1)
82#define LP5523_ENG2_IS_LOADING(mode) \
83 ((mode & LP5523_MODE_ENG2_M) == LP5523_LOAD_ENG2)
84#define LP5523_ENG3_IS_LOADING(mode) \
85 ((mode & LP5523_MODE_ENG3_M) == LP5523_LOAD_ENG3)
86
87#define LP5523_EXEC_ENG1_M 0x30 /* Enable Register */
88#define LP5523_EXEC_ENG2_M 0x0C
89#define LP5523_EXEC_ENG3_M 0x03
90#define LP5523_EXEC_M 0x3F
91#define LP5523_RUN_ENG1 0x20
92#define LP5523_RUN_ENG2 0x08
93#define LP5523_RUN_ENG3 0x02
94
Kim, Milo27d77042012-09-04 15:06:18 +080095enum lp5523_chip_id {
96 LP5523,
97 LP55231,
98};
99
Milo(Woogyom) Kimdb6eaf82013-02-05 19:18:51 +0900100static inline void lp5523_wait_opmode_done(void)
101{
102 usleep_range(1000, 2000);
103}
104
Milo(Woogyom) Kima96bfa12013-02-05 19:09:32 +0900105static void lp5523_set_led_current(struct lp55xx_led *led, u8 led_current)
106{
107 led->led_current = led_current;
108 lp55xx_write(led->chip, LP5523_REG_LED_CURRENT_BASE + led->chan_nr,
109 led_current);
110}
111
Milo(Woogyom) Kimffbdccd2013-02-05 18:57:36 +0900112static int lp5523_post_init_device(struct lp55xx_chip *chip)
Samu Onkalo0efba162010-11-11 14:05:22 -0800113{
Milo(Woogyom) Kimffbdccd2013-02-05 18:57:36 +0900114 int ret;
Samu Onkalo0efba162010-11-11 14:05:22 -0800115
Milo(Woogyom) Kimffbdccd2013-02-05 18:57:36 +0900116 ret = lp55xx_write(chip, LP5523_REG_ENABLE, LP5523_ENABLE);
Milo(Woogyom) Kim632418b2013-02-05 18:04:50 +0900117 if (ret)
118 return ret;
Samu Onkalo0efba162010-11-11 14:05:22 -0800119
Samu Onkalo2e4840e2010-11-24 12:57:04 -0800120 /* Chip startup time is 500 us, 1 - 2 ms gives some margin */
121 usleep_range(1000, 2000);
Samu Onkalo0efba162010-11-11 14:05:22 -0800122
Milo(Woogyom) Kimffbdccd2013-02-05 18:57:36 +0900123 ret = lp55xx_write(chip, LP5523_REG_CONFIG,
Samu Onkalo0efba162010-11-11 14:05:22 -0800124 LP5523_AUTO_INC | LP5523_PWR_SAVE |
125 LP5523_CP_AUTO | LP5523_AUTO_CLK |
126 LP5523_PWM_PWR_SAVE);
Milo(Woogyom) Kim632418b2013-02-05 18:04:50 +0900127 if (ret)
Jingoo Han1b21ec52012-10-28 18:45:11 -0700128 return ret;
129
Milo(Woogyom) Kim632418b2013-02-05 18:04:50 +0900130 /* turn on all leds */
Milo(Woogyom) Kimffbdccd2013-02-05 18:57:36 +0900131 ret = lp55xx_write(chip, LP5523_REG_ENABLE_LEDS_MSB, 0x01);
Milo(Woogyom) Kim632418b2013-02-05 18:04:50 +0900132 if (ret)
133 return ret;
Samu Onkalo0efba162010-11-11 14:05:22 -0800134
Milo(Woogyom) Kimffbdccd2013-02-05 18:57:36 +0900135 return lp55xx_write(chip, LP5523_REG_ENABLE_LEDS_LSB, 0xff);
Samu Onkalo0efba162010-11-11 14:05:22 -0800136}
137
Milo(Woogyom) Kimdb6eaf82013-02-05 19:18:51 +0900138static void lp5523_load_engine(struct lp55xx_chip *chip)
Samu Onkalo0efba162010-11-11 14:05:22 -0800139{
Milo(Woogyom) Kimdb6eaf82013-02-05 19:18:51 +0900140 enum lp55xx_engine_index idx = chip->engine_idx;
141 u8 mask[] = {
142 [LP55XX_ENGINE_1] = LP5523_MODE_ENG1_M,
143 [LP55XX_ENGINE_2] = LP5523_MODE_ENG2_M,
144 [LP55XX_ENGINE_3] = LP5523_MODE_ENG3_M,
145 };
Samu Onkalo0efba162010-11-11 14:05:22 -0800146
Milo(Woogyom) Kimdb6eaf82013-02-05 19:18:51 +0900147 u8 val[] = {
148 [LP55XX_ENGINE_1] = LP5523_LOAD_ENG1,
149 [LP55XX_ENGINE_2] = LP5523_LOAD_ENG2,
150 [LP55XX_ENGINE_3] = LP5523_LOAD_ENG3,
151 };
Samu Onkalo0efba162010-11-11 14:05:22 -0800152
Milo(Woogyom) Kimdb6eaf82013-02-05 19:18:51 +0900153 u8 page_sel[] = {
154 [LP55XX_ENGINE_1] = LP5523_PAGE_ENG1,
155 [LP55XX_ENGINE_2] = LP5523_PAGE_ENG2,
156 [LP55XX_ENGINE_3] = LP5523_PAGE_ENG3,
157 };
Samu Onkalo0efba162010-11-11 14:05:22 -0800158
Milo(Woogyom) Kimdb6eaf82013-02-05 19:18:51 +0900159 lp55xx_update_bits(chip, LP5523_REG_OP_MODE, mask[idx], val[idx]);
Samu Onkalo0efba162010-11-11 14:05:22 -0800160
Milo(Woogyom) Kimdb6eaf82013-02-05 19:18:51 +0900161 lp5523_wait_opmode_done();
Samu Onkalo0efba162010-11-11 14:05:22 -0800162
Milo(Woogyom) Kimdb6eaf82013-02-05 19:18:51 +0900163 lp55xx_write(chip, LP5523_REG_PROG_PAGE_SEL, page_sel[idx]);
Samu Onkalo0efba162010-11-11 14:05:22 -0800164}
165
Milo(Woogyom) Kimdb6eaf82013-02-05 19:18:51 +0900166static void lp5523_stop_engine(struct lp55xx_chip *chip)
Samu Onkalo0efba162010-11-11 14:05:22 -0800167{
Milo(Woogyom) Kimdb6eaf82013-02-05 19:18:51 +0900168 lp55xx_write(chip, LP5523_REG_OP_MODE, 0);
169 lp5523_wait_opmode_done();
Samu Onkalo0efba162010-11-11 14:05:22 -0800170}
171
Milo(Woogyom) Kimdb6eaf82013-02-05 19:18:51 +0900172static void lp5523_turn_off_channels(struct lp55xx_chip *chip)
Samu Onkalo0efba162010-11-11 14:05:22 -0800173{
174 int i;
Kim, Milo469eba02012-08-21 17:04:02 +0800175
Milo(Woogyom) Kimdb6eaf82013-02-05 19:18:51 +0900176 for (i = 0; i < LP5523_MAX_LEDS; i++)
177 lp55xx_write(chip, LP5523_REG_LED_PWM_BASE + i, 0);
178}
179
180static void lp5523_run_engine(struct lp55xx_chip *chip, bool start)
181{
182 int ret;
183 u8 mode;
184 u8 exec;
185
186 /* stop engine */
187 if (!start) {
188 lp5523_stop_engine(chip);
189 lp5523_turn_off_channels(chip);
190 return;
Samu Onkalo0efba162010-11-11 14:05:22 -0800191 }
Milo(Woogyom) Kimdb6eaf82013-02-05 19:18:51 +0900192
193 /*
194 * To run the engine,
195 * operation mode and enable register should updated at the same time
196 */
197
198 ret = lp55xx_read(chip, LP5523_REG_OP_MODE, &mode);
199 if (ret)
200 return;
201
202 ret = lp55xx_read(chip, LP5523_REG_ENABLE, &exec);
203 if (ret)
204 return;
205
206 /* change operation mode to RUN only when each engine is loading */
207 if (LP5523_ENG1_IS_LOADING(mode)) {
208 mode = (mode & ~LP5523_MODE_ENG1_M) | LP5523_RUN_ENG1;
209 exec = (exec & ~LP5523_EXEC_ENG1_M) | LP5523_RUN_ENG1;
210 }
211
212 if (LP5523_ENG2_IS_LOADING(mode)) {
213 mode = (mode & ~LP5523_MODE_ENG2_M) | LP5523_RUN_ENG2;
214 exec = (exec & ~LP5523_EXEC_ENG2_M) | LP5523_RUN_ENG2;
215 }
216
217 if (LP5523_ENG3_IS_LOADING(mode)) {
218 mode = (mode & ~LP5523_MODE_ENG3_M) | LP5523_RUN_ENG3;
219 exec = (exec & ~LP5523_EXEC_ENG3_M) | LP5523_RUN_ENG3;
220 }
221
222 lp55xx_write(chip, LP5523_REG_OP_MODE, mode);
223 lp5523_wait_opmode_done();
224
225 lp55xx_update_bits(chip, LP5523_REG_ENABLE, LP5523_EXEC_M, exec);
226}
227
228static int lp5523_update_program_memory(struct lp55xx_chip *chip,
229 const u8 *data, size_t size)
230{
231 u8 pattern[LP5523_PROGRAM_LENGTH] = {0};
232 unsigned cmd;
233 char c[3];
234 int update_size;
235 int nrchars;
236 int offset = 0;
237 int ret;
238 int i;
239
240 /* clear program memory before updating */
241 for (i = 0; i < LP5523_PROGRAM_LENGTH; i++)
242 lp55xx_write(chip, LP5523_REG_PROG_MEM + i, 0);
243
244 i = 0;
245 while ((offset < size - 1) && (i < LP5523_PROGRAM_LENGTH)) {
246 /* separate sscanfs because length is working only for %s */
247 ret = sscanf(data + offset, "%2s%n ", c, &nrchars);
248 if (ret != 1)
249 goto err;
250
251 ret = sscanf(c, "%2x", &cmd);
252 if (ret != 1)
253 goto err;
254
255 pattern[i] = (u8)cmd;
256 offset += nrchars;
257 i++;
258 }
259
260 /* Each instruction is 16bit long. Check that length is even */
261 if (i % 2)
262 goto err;
263
264 update_size = i;
265 for (i = 0; i < update_size; i++)
266 lp55xx_write(chip, LP5523_REG_PROG_MEM + i, pattern[i]);
Samu Onkalo0efba162010-11-11 14:05:22 -0800267
268 return 0;
Milo(Woogyom) Kimdb6eaf82013-02-05 19:18:51 +0900269
270err:
271 dev_err(&chip->cl->dev, "wrong pattern format\n");
272 return -EINVAL;
Samu Onkalo0efba162010-11-11 14:05:22 -0800273}
274
Milo(Woogyom) Kimdb6eaf82013-02-05 19:18:51 +0900275static void lp5523_firmware_loaded(struct lp55xx_chip *chip)
Samu Onkalo0efba162010-11-11 14:05:22 -0800276{
Milo(Woogyom) Kimdb6eaf82013-02-05 19:18:51 +0900277 const struct firmware *fw = chip->fw;
Samu Onkalo0efba162010-11-11 14:05:22 -0800278
Milo(Woogyom) Kimdb6eaf82013-02-05 19:18:51 +0900279 if (fw->size > LP5523_PROGRAM_LENGTH) {
280 dev_err(&chip->cl->dev, "firmware data size overflow: %zu\n",
281 fw->size);
282 return;
283 }
284
285 /*
286 * Program momery sequence
287 * 1) set engine mode to "LOAD"
288 * 2) write firmware data into program memory
289 */
290
291 lp5523_load_engine(chip);
292 lp5523_update_program_memory(chip, fw->data, fw->size);
Samu Onkalo0efba162010-11-11 14:05:22 -0800293}
294
Samu Onkalo0efba162010-11-11 14:05:22 -0800295static ssize_t lp5523_selftest(struct device *dev,
296 struct device_attribute *attr,
297 char *buf)
298{
Milo(Woogyom) Kim9ca3bd82013-02-05 19:21:43 +0900299 struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
300 struct lp55xx_chip *chip = led->chip;
301 struct lp55xx_platform_data *pdata = chip->pdata;
Samu Onkalo0efba162010-11-11 14:05:22 -0800302 int i, ret, pos = 0;
Samu Onkalo0efba162010-11-11 14:05:22 -0800303 u8 status, adc, vdd;
304
305 mutex_lock(&chip->lock);
306
Milo(Woogyom) Kim9ca3bd82013-02-05 19:21:43 +0900307 ret = lp55xx_read(chip, LP5523_REG_STATUS, &status);
Samu Onkalo0efba162010-11-11 14:05:22 -0800308 if (ret < 0)
309 goto fail;
310
311 /* Check that ext clock is really in use if requested */
Milo(Woogyom) Kim9ca3bd82013-02-05 19:21:43 +0900312 if (pdata->clock_mode == LP55XX_CLOCK_EXT) {
Samu Onkalo0efba162010-11-11 14:05:22 -0800313 if ((status & LP5523_EXT_CLK_USED) == 0)
314 goto fail;
Milo(Woogyom) Kim9ca3bd82013-02-05 19:21:43 +0900315 }
Samu Onkalo0efba162010-11-11 14:05:22 -0800316
317 /* Measure VDD (i.e. VBAT) first (channel 16 corresponds to VDD) */
Milo(Woogyom) Kim9ca3bd82013-02-05 19:21:43 +0900318 lp55xx_write(chip, LP5523_REG_LED_TEST_CTRL, LP5523_EN_LEDTEST | 16);
Samu Onkalo2e4840e2010-11-24 12:57:04 -0800319 usleep_range(3000, 6000); /* ADC conversion time is typically 2.7 ms */
Milo(Woogyom) Kim9ca3bd82013-02-05 19:21:43 +0900320 ret = lp55xx_read(chip, LP5523_REG_STATUS, &status);
Jingoo Han1b21ec52012-10-28 18:45:11 -0700321 if (ret < 0)
322 goto fail;
323
Samu Onkalo0efba162010-11-11 14:05:22 -0800324 if (!(status & LP5523_LEDTEST_DONE))
Samu Onkalo2e4840e2010-11-24 12:57:04 -0800325 usleep_range(3000, 6000); /* Was not ready. Wait little bit */
Samu Onkalo0efba162010-11-11 14:05:22 -0800326
Milo(Woogyom) Kim9ca3bd82013-02-05 19:21:43 +0900327 ret = lp55xx_read(chip, LP5523_REG_LED_TEST_ADC, &vdd);
Jingoo Han1b21ec52012-10-28 18:45:11 -0700328 if (ret < 0)
329 goto fail;
330
Samu Onkalo0efba162010-11-11 14:05:22 -0800331 vdd--; /* There may be some fluctuation in measurement */
332
Milo(Woogyom) Kim0e202342013-02-05 19:07:34 +0900333 for (i = 0; i < LP5523_MAX_LEDS; i++) {
Samu Onkalo0efba162010-11-11 14:05:22 -0800334 /* Skip non-existing channels */
Milo(Woogyom) Kim9ca3bd82013-02-05 19:21:43 +0900335 if (pdata->led_config[i].led_current == 0)
Samu Onkalo0efba162010-11-11 14:05:22 -0800336 continue;
337
338 /* Set default current */
Milo(Woogyom) Kim9ca3bd82013-02-05 19:21:43 +0900339 lp55xx_write(chip, LP5523_REG_LED_CURRENT_BASE + i,
340 pdata->led_config[i].led_current);
Samu Onkalo0efba162010-11-11 14:05:22 -0800341
Milo(Woogyom) Kim9ca3bd82013-02-05 19:21:43 +0900342 lp55xx_write(chip, LP5523_REG_LED_PWM_BASE + i, 0xff);
Samu Onkalo2e4840e2010-11-24 12:57:04 -0800343 /* let current stabilize 2 - 4ms before measurements start */
344 usleep_range(2000, 4000);
Milo(Woogyom) Kim9ca3bd82013-02-05 19:21:43 +0900345 lp55xx_write(chip, LP5523_REG_LED_TEST_CTRL,
Samu Onkalo0efba162010-11-11 14:05:22 -0800346 LP5523_EN_LEDTEST | i);
Samu Onkalo2e4840e2010-11-24 12:57:04 -0800347 /* ADC conversion time is 2.7 ms typically */
348 usleep_range(3000, 6000);
Milo(Woogyom) Kim9ca3bd82013-02-05 19:21:43 +0900349 ret = lp55xx_read(chip, LP5523_REG_STATUS, &status);
Jingoo Han1b21ec52012-10-28 18:45:11 -0700350 if (ret < 0)
351 goto fail;
352
Samu Onkalo0efba162010-11-11 14:05:22 -0800353 if (!(status & LP5523_LEDTEST_DONE))
Samu Onkalo2e4840e2010-11-24 12:57:04 -0800354 usleep_range(3000, 6000);/* Was not ready. Wait. */
Milo(Woogyom) Kim9ca3bd82013-02-05 19:21:43 +0900355
356 ret = lp55xx_read(chip, LP5523_REG_LED_TEST_ADC, &adc);
Jingoo Han1b21ec52012-10-28 18:45:11 -0700357 if (ret < 0)
358 goto fail;
Samu Onkalo0efba162010-11-11 14:05:22 -0800359
360 if (adc >= vdd || adc < LP5523_ADC_SHORTCIRC_LIM)
361 pos += sprintf(buf + pos, "LED %d FAIL\n", i);
362
Milo(Woogyom) Kim9ca3bd82013-02-05 19:21:43 +0900363 lp55xx_write(chip, LP5523_REG_LED_PWM_BASE + i, 0x00);
Samu Onkalo0efba162010-11-11 14:05:22 -0800364
365 /* Restore current */
Milo(Woogyom) Kim9ca3bd82013-02-05 19:21:43 +0900366 lp55xx_write(chip, LP5523_REG_LED_CURRENT_BASE + i,
367 led->led_current);
Samu Onkalo0efba162010-11-11 14:05:22 -0800368 led++;
369 }
370 if (pos == 0)
371 pos = sprintf(buf, "OK\n");
372 goto release_lock;
373fail:
374 pos = sprintf(buf, "FAIL\n");
375
376release_lock:
377 mutex_unlock(&chip->lock);
378
379 return pos;
380}
381
Samu Onkalo0efba162010-11-11 14:05:22 -0800382static void lp5523_led_brightness_work(struct work_struct *work)
383{
Milo(Woogyom) Kima6e46792013-02-05 19:08:40 +0900384 struct lp55xx_led *led = container_of(work, struct lp55xx_led,
Samu Onkalo0efba162010-11-11 14:05:22 -0800385 brightness_work);
Milo(Woogyom) Kima6e46792013-02-05 19:08:40 +0900386 struct lp55xx_chip *chip = led->chip;
Samu Onkalo0efba162010-11-11 14:05:22 -0800387
388 mutex_lock(&chip->lock);
Milo(Woogyom) Kima6e46792013-02-05 19:08:40 +0900389 lp55xx_write(chip, LP5523_REG_LED_PWM_BASE + led->chan_nr,
Samu Onkalo0efba162010-11-11 14:05:22 -0800390 led->brightness);
Samu Onkalo0efba162010-11-11 14:05:22 -0800391 mutex_unlock(&chip->lock);
392}
393
Samu Onkalo0efba162010-11-11 14:05:22 -0800394static DEVICE_ATTR(selftest, S_IRUGO, lp5523_selftest, NULL);
395
396static struct attribute *lp5523_attributes[] = {
Samu Onkalo0efba162010-11-11 14:05:22 -0800397 &dev_attr_selftest.attr,
Kim, Milof1625842012-09-12 20:16:03 +0800398 NULL,
Samu Onkalo0efba162010-11-11 14:05:22 -0800399};
400
401static const struct attribute_group lp5523_group = {
402 .attrs = lp5523_attributes,
403};
404
Milo(Woogyom) Kim48068d52013-02-05 18:08:49 +0900405/* Chip specific configurations */
406static struct lp55xx_device_config lp5523_cfg = {
407 .reset = {
408 .addr = LP5523_REG_RESET,
409 .val = LP5523_RESET,
410 },
Milo(Woogyom) Kime3a700d2013-02-05 18:09:56 +0900411 .enable = {
412 .addr = LP5523_REG_ENABLE,
413 .val = LP5523_ENABLE,
414 },
Milo(Woogyom) Kim0e202342013-02-05 19:07:34 +0900415 .max_channel = LP5523_MAX_LEDS,
Milo(Woogyom) Kimffbdccd2013-02-05 18:57:36 +0900416 .post_init_device = lp5523_post_init_device,
Milo(Woogyom) Kima6e46792013-02-05 19:08:40 +0900417 .brightness_work_fn = lp5523_led_brightness_work,
Milo(Woogyom) Kima96bfa12013-02-05 19:09:32 +0900418 .set_led_current = lp5523_set_led_current,
Milo(Woogyom) Kimdb6eaf82013-02-05 19:18:51 +0900419 .firmware_cb = lp5523_firmware_loaded,
420 .run_engine = lp5523_run_engine,
Milo(Woogyom) Kime73c0ce2013-02-05 19:20:45 +0900421 .dev_attr_group = &lp5523_group,
Milo(Woogyom) Kim48068d52013-02-05 18:08:49 +0900422};
423
Bill Pemberton98ea1ea2012-11-19 13:23:02 -0500424static int lp5523_probe(struct i2c_client *client,
Samu Onkalo0efba162010-11-11 14:05:22 -0800425 const struct i2c_device_id *id)
426{
Milo(Woogyom) Kim22ebeb42013-02-05 18:58:35 +0900427 int ret;
Milo(Woogyom) Kim6a0c9a42013-02-05 18:03:08 +0900428 struct lp55xx_chip *chip;
429 struct lp55xx_led *led;
430 struct lp55xx_platform_data *pdata = client->dev.platform_data;
Samu Onkalo0efba162010-11-11 14:05:22 -0800431
Milo(Woogyom) Kim6a0c9a42013-02-05 18:03:08 +0900432 if (!pdata) {
Samu Onkalo0efba162010-11-11 14:05:22 -0800433 dev_err(&client->dev, "no platform data\n");
Bryan Wu94ca4bc2012-07-04 11:44:18 +0800434 return -EINVAL;
Samu Onkalo0efba162010-11-11 14:05:22 -0800435 }
436
Milo(Woogyom) Kim6a0c9a42013-02-05 18:03:08 +0900437 chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
438 if (!chip)
439 return -ENOMEM;
Samu Onkalo0efba162010-11-11 14:05:22 -0800440
Milo(Woogyom) Kim6a0c9a42013-02-05 18:03:08 +0900441 led = devm_kzalloc(&client->dev,
442 sizeof(*led) * pdata->num_channels, GFP_KERNEL);
443 if (!led)
444 return -ENOMEM;
445
446 chip->cl = client;
447 chip->pdata = pdata;
Milo(Woogyom) Kim48068d52013-02-05 18:08:49 +0900448 chip->cfg = &lp5523_cfg;
Milo(Woogyom) Kim6a0c9a42013-02-05 18:03:08 +0900449
450 mutex_init(&chip->lock);
451
452 i2c_set_clientdata(client, led);
Samu Onkalo0efba162010-11-11 14:05:22 -0800453
Milo(Woogyom) Kim22ebeb42013-02-05 18:58:35 +0900454 ret = lp55xx_init_device(chip);
Samu Onkalo0efba162010-11-11 14:05:22 -0800455 if (ret)
Milo(Woogyom) Kimf6c64c62013-02-05 17:58:01 +0900456 goto err_init;
Samu Onkalo0efba162010-11-11 14:05:22 -0800457
Kim, Milo56a1e9a2012-09-04 15:06:26 +0800458 dev_info(&client->dev, "%s Programmable led chip found\n", id->name);
Samu Onkalo0efba162010-11-11 14:05:22 -0800459
Milo(Woogyom) Kim9e9b3db2013-02-05 19:06:27 +0900460 ret = lp55xx_register_leds(led, chip);
Milo(Woogyom) Kimf6524802013-02-05 17:53:40 +0900461 if (ret)
Milo(Woogyom) Kim9e9b3db2013-02-05 19:06:27 +0900462 goto err_register_leds;
Samu Onkalo0efba162010-11-11 14:05:22 -0800463
Milo(Woogyom) Kime73c0ce2013-02-05 19:20:45 +0900464 ret = lp55xx_register_sysfs(chip);
Samu Onkalo0efba162010-11-11 14:05:22 -0800465 if (ret) {
466 dev_err(&client->dev, "registering sysfs failed\n");
Milo(Woogyom) Kime73c0ce2013-02-05 19:20:45 +0900467 goto err_register_sysfs;
Samu Onkalo0efba162010-11-11 14:05:22 -0800468 }
Milo(Woogyom) Kime73c0ce2013-02-05 19:20:45 +0900469
470 return 0;
471
472err_register_sysfs:
Milo(Woogyom) Kimc3a68eb2013-02-05 19:11:18 +0900473 lp55xx_unregister_leds(led, chip);
Milo(Woogyom) Kim9e9b3db2013-02-05 19:06:27 +0900474err_register_leds:
Milo(Woogyom) Kim6ce61762013-02-05 19:03:02 +0900475 lp55xx_deinit_device(chip);
Milo(Woogyom) Kimf6c64c62013-02-05 17:58:01 +0900476err_init:
Samu Onkalo0efba162010-11-11 14:05:22 -0800477 return ret;
478}
479
480static int lp5523_remove(struct i2c_client *client)
481{
Milo(Woogyom) Kim6ce61762013-02-05 19:03:02 +0900482 struct lp55xx_led *led = i2c_get_clientdata(client);
483 struct lp55xx_chip *chip = led->chip;
Samu Onkalo0efba162010-11-11 14:05:22 -0800484
Milo(Woogyom) Kim87cc4bd2013-02-05 19:23:51 +0900485 lp5523_stop_engine(chip);
486 lp55xx_unregister_sysfs(chip);
Milo(Woogyom) Kimc3a68eb2013-02-05 19:11:18 +0900487 lp55xx_unregister_leds(led, chip);
Milo(Woogyom) Kim6ce61762013-02-05 19:03:02 +0900488 lp55xx_deinit_device(chip);
Samu Onkalo0efba162010-11-11 14:05:22 -0800489
Samu Onkalo0efba162010-11-11 14:05:22 -0800490 return 0;
491}
492
493static const struct i2c_device_id lp5523_id[] = {
Kim, Milo27d77042012-09-04 15:06:18 +0800494 { "lp5523", LP5523 },
495 { "lp55231", LP55231 },
Samu Onkalo0efba162010-11-11 14:05:22 -0800496 { }
497};
498
499MODULE_DEVICE_TABLE(i2c, lp5523_id);
500
501static struct i2c_driver lp5523_driver = {
502 .driver = {
Kim, Milo27d77042012-09-04 15:06:18 +0800503 .name = "lp5523x",
Samu Onkalo0efba162010-11-11 14:05:22 -0800504 },
505 .probe = lp5523_probe,
506 .remove = lp5523_remove,
507 .id_table = lp5523_id,
508};
509
Axel Lin09a0d182012-01-10 15:09:27 -0800510module_i2c_driver(lp5523_driver);
Samu Onkalo0efba162010-11-11 14:05:22 -0800511
512MODULE_AUTHOR("Mathias Nyman <mathias.nyman@nokia.com>");
513MODULE_DESCRIPTION("LP5523 LED engine");
514MODULE_LICENSE("GPL");