leds-lp5521/5523: use new lp55xx common header
[linux-2.6-block.git] / drivers / leds / leds-lp5523.c
CommitLineData
0efba16c
SO
1/*
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
0efba16c 23#include <linux/delay.h>
79bcc10b
MWK
24#include <linux/firmware.h>
25#include <linux/i2c.h>
26#include <linux/init.h>
0efba16c 27#include <linux/leds.h>
79bcc10b
MWK
28#include <linux/module.h>
29#include <linux/mutex.h>
6a0c9a47 30#include <linux/platform_data/leds-lp55xx.h>
79bcc10b 31#include <linux/slab.h>
6a0c9a47
MWK
32
33#include "leds-lp55xx-common.h"
0efba16c 34
12f022d2
MWK
35#define LP5523_PROGRAM_LENGTH 32
36#define LP5523_MAX_LEDS 9
37
38/* Registers */
0efba16c
SO
39#define LP5523_REG_ENABLE 0x00
40#define LP5523_REG_OP_MODE 0x01
0efba16c
SO
41#define LP5523_REG_ENABLE_LEDS_MSB 0x04
42#define LP5523_REG_ENABLE_LEDS_LSB 0x05
0efba16c
SO
43#define LP5523_REG_LED_PWM_BASE 0x16
44#define LP5523_REG_LED_CURRENT_BASE 0x26
45#define LP5523_REG_CONFIG 0x36
12f022d2
MWK
46#define LP5523_REG_STATUS 0x3A
47#define LP5523_REG_RESET 0x3D
0efba16c
SO
48#define LP5523_REG_LED_TEST_CTRL 0x41
49#define LP5523_REG_LED_TEST_ADC 0x42
12f022d2 50#define LP5523_REG_PROG_PAGE_SEL 0x4F
0efba16c
SO
51#define LP5523_REG_PROG_MEM 0x50
52
12f022d2 53/* Bit description in registers */
0efba16c
SO
54#define LP5523_ENABLE 0x40
55#define LP5523_AUTO_INC 0x40
56#define LP5523_PWR_SAVE 0x20
57#define LP5523_PWM_PWR_SAVE 0x04
0efba16c 58#define LP5523_CP_AUTO 0x18
0efba16c 59#define LP5523_AUTO_CLK 0x02
12f022d2 60
0efba16c
SO
61#define LP5523_EN_LEDTEST 0x80
62#define LP5523_LEDTEST_DONE 0x80
48068d5d 63#define LP5523_RESET 0xFF
0efba16c 64#define LP5523_ADC_SHORTCIRC_LIM 80
0efba16c
SO
65#define LP5523_EXT_CLK_USED 0x08
66
db6eaf83
MWK
67/* 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
27d7704e
KM
95enum lp5523_chip_id {
96 LP5523,
97 LP55231,
98};
99
db6eaf83
MWK
100static inline void lp5523_wait_opmode_done(void)
101{
102 usleep_range(1000, 2000);
103}
104
a96bfa13
MWK
105static 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
ffbdccdb 112static int lp5523_post_init_device(struct lp55xx_chip *chip)
0efba16c 113{
ffbdccdb 114 int ret;
0efba16c 115
ffbdccdb 116 ret = lp55xx_write(chip, LP5523_REG_ENABLE, LP5523_ENABLE);
632418bf
MWK
117 if (ret)
118 return ret;
0efba16c 119
2e4840ed
SO
120 /* Chip startup time is 500 us, 1 - 2 ms gives some margin */
121 usleep_range(1000, 2000);
0efba16c 122
ffbdccdb 123 ret = lp55xx_write(chip, LP5523_REG_CONFIG,
0efba16c
SO
124 LP5523_AUTO_INC | LP5523_PWR_SAVE |
125 LP5523_CP_AUTO | LP5523_AUTO_CLK |
126 LP5523_PWM_PWR_SAVE);
632418bf
MWK
127 if (ret)
128 return ret;
0efba16c
SO
129
130 /* turn on all leds */
ffbdccdb 131 ret = lp55xx_write(chip, LP5523_REG_ENABLE_LEDS_MSB, 0x01);
632418bf 132 if (ret)
1b21ec5a
JH
133 return ret;
134
ffbdccdb 135 return lp55xx_write(chip, LP5523_REG_ENABLE_LEDS_LSB, 0xff);
0efba16c
SO
136}
137
db6eaf83 138static void lp5523_load_engine(struct lp55xx_chip *chip)
0efba16c 139{
db6eaf83
MWK
140 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 };
146
147 u8 val[] = {
148 [LP55XX_ENGINE_1] = LP5523_LOAD_ENG1,
149 [LP55XX_ENGINE_2] = LP5523_LOAD_ENG2,
150 [LP55XX_ENGINE_3] = LP5523_LOAD_ENG3,
151 };
152
153 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 };
158
159 lp55xx_update_bits(chip, LP5523_REG_OP_MODE, mask[idx], val[idx]);
160
161 lp5523_wait_opmode_done();
162
163 lp55xx_write(chip, LP5523_REG_PROG_PAGE_SEL, page_sel[idx]);
0efba16c
SO
164}
165
db6eaf83 166static void lp5523_stop_engine(struct lp55xx_chip *chip)
0efba16c 167{
db6eaf83
MWK
168 lp55xx_write(chip, LP5523_REG_OP_MODE, 0);
169 lp5523_wait_opmode_done();
170}
0efba16c 171
db6eaf83
MWK
172static void lp5523_turn_off_channels(struct lp55xx_chip *chip)
173{
174 int i;
0efba16c 175
db6eaf83
MWK
176 for (i = 0; i < LP5523_MAX_LEDS; i++)
177 lp55xx_write(chip, LP5523_REG_LED_PWM_BASE + i, 0);
0efba16c
SO
178}
179
db6eaf83 180static void lp5523_run_engine(struct lp55xx_chip *chip, bool start)
0efba16c 181{
db6eaf83
MWK
182 int ret;
183 u8 mode;
184 u8 exec;
0efba16c 185
db6eaf83
MWK
186 /* stop engine */
187 if (!start) {
188 lp5523_stop_engine(chip);
189 lp5523_turn_off_channels(chip);
190 return;
191 }
0efba16c 192
db6eaf83
MWK
193 /*
194 * To run the engine,
195 * operation mode and enable register should updated at the same time
196 */
0efba16c 197
db6eaf83
MWK
198 ret = lp55xx_read(chip, LP5523_REG_OP_MODE, &mode);
199 if (ret)
200 return;
0efba16c 201
db6eaf83
MWK
202 ret = lp55xx_read(chip, LP5523_REG_ENABLE, &exec);
203 if (ret)
204 return;
0efba16c 205
db6eaf83
MWK
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 }
0efba16c 211
db6eaf83
MWK
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 }
0efba16c 216
db6eaf83
MWK
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);
0efba16c
SO
226}
227
db6eaf83
MWK
228static int lp5523_update_program_memory(struct lp55xx_chip *chip,
229 const u8 *data, size_t size)
0efba16c 230{
db6eaf83
MWK
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;
0efba16c 238 int i;
0efba16c 239
db6eaf83
MWK
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);
0efba16c 243
db6eaf83
MWK
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;
0efba16c 250
db6eaf83
MWK
251 ret = sscanf(c, "%2x", &cmd);
252 if (ret != 1)
253 goto err;
0efba16c 254
db6eaf83
MWK
255 pattern[i] = (u8)cmd;
256 offset += nrchars;
257 i++;
258 }
0efba16c 259
db6eaf83
MWK
260 /* Each instruction is 16bit long. Check that length is even */
261 if (i % 2)
262 goto err;
0efba16c 263
db6eaf83
MWK
264 update_size = i;
265 for (i = 0; i < update_size; i++)
266 lp55xx_write(chip, LP5523_REG_PROG_MEM + i, pattern[i]);
0efba16c 267
db6eaf83 268 return 0;
0efba16c 269
db6eaf83
MWK
270err:
271 dev_err(&chip->cl->dev, "wrong pattern format\n");
272 return -EINVAL;
0efba16c 273}
0efba16c 274
db6eaf83 275static void lp5523_firmware_loaded(struct lp55xx_chip *chip)
0efba16c 276{
db6eaf83 277 const struct firmware *fw = chip->fw;
fbac0812 278
db6eaf83
MWK
279 if (fw->size > LP5523_PROGRAM_LENGTH) {
280 dev_err(&chip->cl->dev, "firmware data size overflow: %zu\n",
281 fw->size);
282 return;
283 }
0efba16c 284
db6eaf83
MWK
285 /*
286 * Program momery sequence
287 * 1) set engine mode to "LOAD"
288 * 2) write firmware data into program memory
289 */
0efba16c 290
db6eaf83
MWK
291 lp5523_load_engine(chip);
292 lp5523_update_program_memory(chip, fw->data, fw->size);
0efba16c 293}
0efba16c
SO
294
295static ssize_t lp5523_selftest(struct device *dev,
296 struct device_attribute *attr,
297 char *buf)
298{
9ca3bd80
MWK
299 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;
0efba16c 302 int i, ret, pos = 0;
0efba16c
SO
303 u8 status, adc, vdd;
304
305 mutex_lock(&chip->lock);
306
9ca3bd80 307 ret = lp55xx_read(chip, LP5523_REG_STATUS, &status);
0efba16c
SO
308 if (ret < 0)
309 goto fail;
310
311 /* Check that ext clock is really in use if requested */
9ca3bd80 312 if (pdata->clock_mode == LP55XX_CLOCK_EXT) {
0efba16c
SO
313 if ((status & LP5523_EXT_CLK_USED) == 0)
314 goto fail;
9ca3bd80 315 }
0efba16c
SO
316
317 /* Measure VDD (i.e. VBAT) first (channel 16 corresponds to VDD) */
9ca3bd80 318 lp55xx_write(chip, LP5523_REG_LED_TEST_CTRL, LP5523_EN_LEDTEST | 16);
2e4840ed 319 usleep_range(3000, 6000); /* ADC conversion time is typically 2.7 ms */
9ca3bd80 320 ret = lp55xx_read(chip, LP5523_REG_STATUS, &status);
1b21ec5a
JH
321 if (ret < 0)
322 goto fail;
323
0efba16c 324 if (!(status & LP5523_LEDTEST_DONE))
2e4840ed 325 usleep_range(3000, 6000); /* Was not ready. Wait little bit */
0efba16c 326
9ca3bd80 327 ret = lp55xx_read(chip, LP5523_REG_LED_TEST_ADC, &vdd);
1b21ec5a
JH
328 if (ret < 0)
329 goto fail;
330
0efba16c
SO
331 vdd--; /* There may be some fluctuation in measurement */
332
0e202346 333 for (i = 0; i < LP5523_MAX_LEDS; i++) {
0efba16c 334 /* Skip non-existing channels */
9ca3bd80 335 if (pdata->led_config[i].led_current == 0)
0efba16c
SO
336 continue;
337
338 /* Set default current */
9ca3bd80
MWK
339 lp55xx_write(chip, LP5523_REG_LED_CURRENT_BASE + i,
340 pdata->led_config[i].led_current);
0efba16c 341
9ca3bd80 342 lp55xx_write(chip, LP5523_REG_LED_PWM_BASE + i, 0xff);
2e4840ed
SO
343 /* let current stabilize 2 - 4ms before measurements start */
344 usleep_range(2000, 4000);
9ca3bd80 345 lp55xx_write(chip, LP5523_REG_LED_TEST_CTRL,
0efba16c 346 LP5523_EN_LEDTEST | i);
2e4840ed
SO
347 /* ADC conversion time is 2.7 ms typically */
348 usleep_range(3000, 6000);
9ca3bd80 349 ret = lp55xx_read(chip, LP5523_REG_STATUS, &status);
1b21ec5a
JH
350 if (ret < 0)
351 goto fail;
352
0efba16c 353 if (!(status & LP5523_LEDTEST_DONE))
2e4840ed 354 usleep_range(3000, 6000);/* Was not ready. Wait. */
9ca3bd80
MWK
355
356 ret = lp55xx_read(chip, LP5523_REG_LED_TEST_ADC, &adc);
1b21ec5a
JH
357 if (ret < 0)
358 goto fail;
0efba16c
SO
359
360 if (adc >= vdd || adc < LP5523_ADC_SHORTCIRC_LIM)
361 pos += sprintf(buf + pos, "LED %d FAIL\n", i);
362
9ca3bd80 363 lp55xx_write(chip, LP5523_REG_LED_PWM_BASE + i, 0x00);
0efba16c
SO
364
365 /* Restore current */
9ca3bd80
MWK
366 lp55xx_write(chip, LP5523_REG_LED_CURRENT_BASE + i,
367 led->led_current);
0efba16c
SO
368 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
0efba16c
SO
382static void lp5523_led_brightness_work(struct work_struct *work)
383{
a6e4679a 384 struct lp55xx_led *led = container_of(work, struct lp55xx_led,
0efba16c 385 brightness_work);
a6e4679a 386 struct lp55xx_chip *chip = led->chip;
0efba16c
SO
387
388 mutex_lock(&chip->lock);
a6e4679a 389 lp55xx_write(chip, LP5523_REG_LED_PWM_BASE + led->chan_nr,
0efba16c 390 led->brightness);
0efba16c
SO
391 mutex_unlock(&chip->lock);
392}
393
0efba16c
SO
394static DEVICE_ATTR(selftest, S_IRUGO, lp5523_selftest, NULL);
395
396static struct attribute *lp5523_attributes[] = {
0efba16c 397 &dev_attr_selftest.attr,
f1625842 398 NULL,
0efba16c
SO
399};
400
401static const struct attribute_group lp5523_group = {
402 .attrs = lp5523_attributes,
403};
404
48068d5d
MWK
405/* Chip specific configurations */
406static struct lp55xx_device_config lp5523_cfg = {
407 .reset = {
408 .addr = LP5523_REG_RESET,
409 .val = LP5523_RESET,
410 },
e3a700d8
MWK
411 .enable = {
412 .addr = LP5523_REG_ENABLE,
413 .val = LP5523_ENABLE,
414 },
0e202346 415 .max_channel = LP5523_MAX_LEDS,
ffbdccdb 416 .post_init_device = lp5523_post_init_device,
a6e4679a 417 .brightness_work_fn = lp5523_led_brightness_work,
a96bfa13 418 .set_led_current = lp5523_set_led_current,
db6eaf83
MWK
419 .firmware_cb = lp5523_firmware_loaded,
420 .run_engine = lp5523_run_engine,
e73c0ce6 421 .dev_attr_group = &lp5523_group,
48068d5d
MWK
422};
423
98ea1ea2 424static int lp5523_probe(struct i2c_client *client,
0efba16c
SO
425 const struct i2c_device_id *id)
426{
22ebeb48 427 int ret;
6a0c9a47
MWK
428 struct lp55xx_chip *chip;
429 struct lp55xx_led *led;
430 struct lp55xx_platform_data *pdata = client->dev.platform_data;
0efba16c 431
6a0c9a47 432 if (!pdata) {
0efba16c 433 dev_err(&client->dev, "no platform data\n");
94ca4bcc 434 return -EINVAL;
0efba16c
SO
435 }
436
6a0c9a47
MWK
437 chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
438 if (!chip)
439 return -ENOMEM;
440
441 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;
48068d5d 448 chip->cfg = &lp5523_cfg;
6a0c9a47
MWK
449
450 mutex_init(&chip->lock);
0efba16c 451
6a0c9a47 452 i2c_set_clientdata(client, led);
0efba16c 453
22ebeb48 454 ret = lp55xx_init_device(chip);
0efba16c 455 if (ret)
f6c64c6f 456 goto err_init;
0efba16c 457
56a1e9ad 458 dev_info(&client->dev, "%s Programmable led chip found\n", id->name);
0efba16c 459
9e9b3db1 460 ret = lp55xx_register_leds(led, chip);
f6524808 461 if (ret)
9e9b3db1 462 goto err_register_leds;
0efba16c 463
e73c0ce6 464 ret = lp55xx_register_sysfs(chip);
0efba16c
SO
465 if (ret) {
466 dev_err(&client->dev, "registering sysfs failed\n");
e73c0ce6 467 goto err_register_sysfs;
0efba16c 468 }
e73c0ce6
MWK
469
470 return 0;
471
472err_register_sysfs:
c3a68ebf 473 lp55xx_unregister_leds(led, chip);
9e9b3db1 474err_register_leds:
6ce61762 475 lp55xx_deinit_device(chip);
f6c64c6f 476err_init:
0efba16c
SO
477 return ret;
478}
479
480static int lp5523_remove(struct i2c_client *client)
481{
6ce61762
MWK
482 struct lp55xx_led *led = i2c_get_clientdata(client);
483 struct lp55xx_chip *chip = led->chip;
0efba16c 484
87cc4bde
MWK
485 lp5523_stop_engine(chip);
486 lp55xx_unregister_sysfs(chip);
c3a68ebf 487 lp55xx_unregister_leds(led, chip);
6ce61762 488 lp55xx_deinit_device(chip);
0efba16c 489
0efba16c
SO
490 return 0;
491}
492
493static const struct i2c_device_id lp5523_id[] = {
27d7704e
KM
494 { "lp5523", LP5523 },
495 { "lp55231", LP55231 },
0efba16c
SO
496 { }
497};
498
499MODULE_DEVICE_TABLE(i2c, lp5523_id);
500
501static struct i2c_driver lp5523_driver = {
502 .driver = {
27d7704e 503 .name = "lp5523x",
0efba16c
SO
504 },
505 .probe = lp5523_probe,
506 .remove = lp5523_remove,
507 .id_table = lp5523_id,
508};
509
09a0d183 510module_i2c_driver(lp5523_driver);
0efba16c
SO
511
512MODULE_AUTHOR("Mathias Nyman <mathias.nyman@nokia.com>");
513MODULE_DESCRIPTION("LP5523 LED engine");
514MODULE_LICENSE("GPL");