2 * lp5523.c - LP5523 LED Driver
4 * Copyright (C) 2010 Nokia Corporation
5 * Copyright (C) 2012 Texas Instruments
7 * Contact: Samu Onkalo <samu.p.onkalo@nokia.com>
8 * Milo(Woogyom) Kim <milo.kim@ti.com>
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * version 2 as published by the Free Software Foundation.
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
25 #include <linux/delay.h>
26 #include <linux/firmware.h>
27 #include <linux/i2c.h>
28 #include <linux/init.h>
29 #include <linux/leds.h>
30 #include <linux/module.h>
31 #include <linux/mutex.h>
32 #include <linux/platform_data/leds-lp55xx.h>
33 #include <linux/slab.h>
35 #include "leds-lp55xx-common.h"
37 #define LP5523_PROGRAM_LENGTH 32
38 #define LP5523_MAX_LEDS 9
41 #define LP5523_REG_ENABLE 0x00
42 #define LP5523_REG_OP_MODE 0x01
43 #define LP5523_REG_ENABLE_LEDS_MSB 0x04
44 #define LP5523_REG_ENABLE_LEDS_LSB 0x05
45 #define LP5523_REG_LED_PWM_BASE 0x16
46 #define LP5523_REG_LED_CURRENT_BASE 0x26
47 #define LP5523_REG_CONFIG 0x36
48 #define LP5523_REG_STATUS 0x3A
49 #define LP5523_REG_RESET 0x3D
50 #define LP5523_REG_LED_TEST_CTRL 0x41
51 #define LP5523_REG_LED_TEST_ADC 0x42
52 #define LP5523_REG_CH1_PROG_START 0x4C
53 #define LP5523_REG_CH2_PROG_START 0x4D
54 #define LP5523_REG_CH3_PROG_START 0x4E
55 #define LP5523_REG_PROG_PAGE_SEL 0x4F
56 #define LP5523_REG_PROG_MEM 0x50
58 /* Bit description in registers */
59 #define LP5523_ENABLE 0x40
60 #define LP5523_AUTO_INC 0x40
61 #define LP5523_PWR_SAVE 0x20
62 #define LP5523_PWM_PWR_SAVE 0x04
63 #define LP5523_CP_AUTO 0x18
64 #define LP5523_AUTO_CLK 0x02
66 #define LP5523_EN_LEDTEST 0x80
67 #define LP5523_LEDTEST_DONE 0x80
68 #define LP5523_RESET 0xFF
69 #define LP5523_ADC_SHORTCIRC_LIM 80
70 #define LP5523_EXT_CLK_USED 0x08
71 #define LP5523_ENG_STATUS_MASK 0x07
73 /* Memory Page Selection */
74 #define LP5523_PAGE_ENG1 0
75 #define LP5523_PAGE_ENG2 1
76 #define LP5523_PAGE_ENG3 2
78 /* Program Memory Operations */
79 #define LP5523_MODE_ENG1_M 0x30 /* Operation Mode Register */
80 #define LP5523_MODE_ENG2_M 0x0C
81 #define LP5523_MODE_ENG3_M 0x03
82 #define LP5523_LOAD_ENG1 0x10
83 #define LP5523_LOAD_ENG2 0x04
84 #define LP5523_LOAD_ENG3 0x01
86 #define LP5523_ENG1_IS_LOADING(mode) \
87 ((mode & LP5523_MODE_ENG1_M) == LP5523_LOAD_ENG1)
88 #define LP5523_ENG2_IS_LOADING(mode) \
89 ((mode & LP5523_MODE_ENG2_M) == LP5523_LOAD_ENG2)
90 #define LP5523_ENG3_IS_LOADING(mode) \
91 ((mode & LP5523_MODE_ENG3_M) == LP5523_LOAD_ENG3)
93 #define LP5523_EXEC_ENG1_M 0x30 /* Enable Register */
94 #define LP5523_EXEC_ENG2_M 0x0C
95 #define LP5523_EXEC_ENG3_M 0x03
96 #define LP5523_EXEC_M 0x3F
97 #define LP5523_RUN_ENG1 0x20
98 #define LP5523_RUN_ENG2 0x08
99 #define LP5523_RUN_ENG3 0x02
101 enum lp5523_chip_id {
106 static int lp5523_init_program_engine(struct lp55xx_chip *chip);
108 static inline void lp5523_wait_opmode_done(void)
110 usleep_range(1000, 2000);
113 static void lp5523_set_led_current(struct lp55xx_led *led, u8 led_current)
115 led->led_current = led_current;
116 lp55xx_write(led->chip, LP5523_REG_LED_CURRENT_BASE + led->chan_nr,
120 static int lp5523_post_init_device(struct lp55xx_chip *chip)
124 ret = lp55xx_write(chip, LP5523_REG_ENABLE, LP5523_ENABLE);
128 /* Chip startup time is 500 us, 1 - 2 ms gives some margin */
129 usleep_range(1000, 2000);
131 ret = lp55xx_write(chip, LP5523_REG_CONFIG,
132 LP5523_AUTO_INC | LP5523_PWR_SAVE |
133 LP5523_CP_AUTO | LP5523_AUTO_CLK |
134 LP5523_PWM_PWR_SAVE);
138 /* turn on all leds */
139 ret = lp55xx_write(chip, LP5523_REG_ENABLE_LEDS_MSB, 0x01);
143 ret = lp55xx_write(chip, LP5523_REG_ENABLE_LEDS_LSB, 0xff);
147 return lp5523_init_program_engine(chip);
150 static void lp5523_load_engine(struct lp55xx_chip *chip)
152 enum lp55xx_engine_index idx = chip->engine_idx;
154 [LP55XX_ENGINE_1] = LP5523_MODE_ENG1_M,
155 [LP55XX_ENGINE_2] = LP5523_MODE_ENG2_M,
156 [LP55XX_ENGINE_3] = LP5523_MODE_ENG3_M,
160 [LP55XX_ENGINE_1] = LP5523_LOAD_ENG1,
161 [LP55XX_ENGINE_2] = LP5523_LOAD_ENG2,
162 [LP55XX_ENGINE_3] = LP5523_LOAD_ENG3,
165 lp55xx_update_bits(chip, LP5523_REG_OP_MODE, mask[idx], val[idx]);
167 lp5523_wait_opmode_done();
170 static void lp5523_load_engine_and_select_page(struct lp55xx_chip *chip)
172 enum lp55xx_engine_index idx = chip->engine_idx;
174 [LP55XX_ENGINE_1] = LP5523_PAGE_ENG1,
175 [LP55XX_ENGINE_2] = LP5523_PAGE_ENG2,
176 [LP55XX_ENGINE_3] = LP5523_PAGE_ENG3,
179 lp5523_load_engine(chip);
181 lp55xx_write(chip, LP5523_REG_PROG_PAGE_SEL, page_sel[idx]);
184 static void lp5523_stop_engine(struct lp55xx_chip *chip)
186 lp55xx_write(chip, LP5523_REG_OP_MODE, 0);
187 lp5523_wait_opmode_done();
190 static void lp5523_turn_off_channels(struct lp55xx_chip *chip)
194 for (i = 0; i < LP5523_MAX_LEDS; i++)
195 lp55xx_write(chip, LP5523_REG_LED_PWM_BASE + i, 0);
198 static void lp5523_run_engine(struct lp55xx_chip *chip, bool start)
206 lp5523_stop_engine(chip);
207 lp5523_turn_off_channels(chip);
213 * operation mode and enable register should updated at the same time
216 ret = lp55xx_read(chip, LP5523_REG_OP_MODE, &mode);
220 ret = lp55xx_read(chip, LP5523_REG_ENABLE, &exec);
224 /* change operation mode to RUN only when each engine is loading */
225 if (LP5523_ENG1_IS_LOADING(mode)) {
226 mode = (mode & ~LP5523_MODE_ENG1_M) | LP5523_RUN_ENG1;
227 exec = (exec & ~LP5523_EXEC_ENG1_M) | LP5523_RUN_ENG1;
230 if (LP5523_ENG2_IS_LOADING(mode)) {
231 mode = (mode & ~LP5523_MODE_ENG2_M) | LP5523_RUN_ENG2;
232 exec = (exec & ~LP5523_EXEC_ENG2_M) | LP5523_RUN_ENG2;
235 if (LP5523_ENG3_IS_LOADING(mode)) {
236 mode = (mode & ~LP5523_MODE_ENG3_M) | LP5523_RUN_ENG3;
237 exec = (exec & ~LP5523_EXEC_ENG3_M) | LP5523_RUN_ENG3;
240 lp55xx_write(chip, LP5523_REG_OP_MODE, mode);
241 lp5523_wait_opmode_done();
243 lp55xx_update_bits(chip, LP5523_REG_ENABLE, LP5523_EXEC_M, exec);
246 static int lp5523_init_program_engine(struct lp55xx_chip *chip)
252 /* one pattern per engine setting LED MUX start and stop addresses */
253 static const u8 pattern[][LP5523_PROGRAM_LENGTH] = {
254 { 0x9c, 0x30, 0x9c, 0xb0, 0x9d, 0x80, 0xd8, 0x00, 0},
255 { 0x9c, 0x40, 0x9c, 0xc0, 0x9d, 0x80, 0xd8, 0x00, 0},
256 { 0x9c, 0x50, 0x9c, 0xd0, 0x9d, 0x80, 0xd8, 0x00, 0},
259 /* hardcode 32 bytes of memory for each engine from program memory */
260 ret = lp55xx_write(chip, LP5523_REG_CH1_PROG_START, 0x00);
264 ret = lp55xx_write(chip, LP5523_REG_CH2_PROG_START, 0x10);
268 ret = lp55xx_write(chip, LP5523_REG_CH3_PROG_START, 0x20);
272 /* write LED MUX address space for each engine */
273 for (i = LP55XX_ENGINE_1; i <= LP55XX_ENGINE_3; i++) {
274 chip->engine_idx = i;
275 lp5523_load_engine_and_select_page(chip);
277 for (j = 0; j < LP5523_PROGRAM_LENGTH; j++) {
278 ret = lp55xx_write(chip, LP5523_REG_PROG_MEM + j,
285 lp5523_run_engine(chip, true);
287 /* Let the programs run for couple of ms and check the engine status */
288 usleep_range(3000, 6000);
289 lp55xx_read(chip, LP5523_REG_STATUS, &status);
290 status &= LP5523_ENG_STATUS_MASK;
292 if (status != LP5523_ENG_STATUS_MASK) {
293 dev_err(&chip->cl->dev,
294 "cound not configure LED engine, status = 0x%.2x\n",
300 lp5523_stop_engine(chip);
304 static int lp5523_update_program_memory(struct lp55xx_chip *chip,
305 const u8 *data, size_t size)
307 u8 pattern[LP5523_PROGRAM_LENGTH] = {0};
316 /* clear program memory before updating */
317 for (i = 0; i < LP5523_PROGRAM_LENGTH; i++)
318 lp55xx_write(chip, LP5523_REG_PROG_MEM + i, 0);
321 while ((offset < size - 1) && (i < LP5523_PROGRAM_LENGTH)) {
322 /* separate sscanfs because length is working only for %s */
323 ret = sscanf(data + offset, "%2s%n ", c, &nrchars);
327 ret = sscanf(c, "%2x", &cmd);
331 pattern[i] = (u8)cmd;
336 /* Each instruction is 16bit long. Check that length is even */
341 for (i = 0; i < update_size; i++)
342 lp55xx_write(chip, LP5523_REG_PROG_MEM + i, pattern[i]);
347 dev_err(&chip->cl->dev, "wrong pattern format\n");
351 static void lp5523_firmware_loaded(struct lp55xx_chip *chip)
353 const struct firmware *fw = chip->fw;
355 if (fw->size > LP5523_PROGRAM_LENGTH) {
356 dev_err(&chip->cl->dev, "firmware data size overflow: %zu\n",
362 * Program momery sequence
363 * 1) set engine mode to "LOAD"
364 * 2) write firmware data into program memory
367 lp5523_load_engine_and_select_page(chip);
368 lp5523_update_program_memory(chip, fw->data, fw->size);
371 static ssize_t lp5523_selftest(struct device *dev,
372 struct device_attribute *attr,
375 struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
376 struct lp55xx_chip *chip = led->chip;
377 struct lp55xx_platform_data *pdata = chip->pdata;
381 mutex_lock(&chip->lock);
383 ret = lp55xx_read(chip, LP5523_REG_STATUS, &status);
387 /* Check that ext clock is really in use if requested */
388 if (pdata->clock_mode == LP55XX_CLOCK_EXT) {
389 if ((status & LP5523_EXT_CLK_USED) == 0)
393 /* Measure VDD (i.e. VBAT) first (channel 16 corresponds to VDD) */
394 lp55xx_write(chip, LP5523_REG_LED_TEST_CTRL, LP5523_EN_LEDTEST | 16);
395 usleep_range(3000, 6000); /* ADC conversion time is typically 2.7 ms */
396 ret = lp55xx_read(chip, LP5523_REG_STATUS, &status);
400 if (!(status & LP5523_LEDTEST_DONE))
401 usleep_range(3000, 6000); /* Was not ready. Wait little bit */
403 ret = lp55xx_read(chip, LP5523_REG_LED_TEST_ADC, &vdd);
407 vdd--; /* There may be some fluctuation in measurement */
409 for (i = 0; i < LP5523_MAX_LEDS; i++) {
410 /* Skip non-existing channels */
411 if (pdata->led_config[i].led_current == 0)
414 /* Set default current */
415 lp55xx_write(chip, LP5523_REG_LED_CURRENT_BASE + i,
416 pdata->led_config[i].led_current);
418 lp55xx_write(chip, LP5523_REG_LED_PWM_BASE + i, 0xff);
419 /* let current stabilize 2 - 4ms before measurements start */
420 usleep_range(2000, 4000);
421 lp55xx_write(chip, LP5523_REG_LED_TEST_CTRL,
422 LP5523_EN_LEDTEST | i);
423 /* ADC conversion time is 2.7 ms typically */
424 usleep_range(3000, 6000);
425 ret = lp55xx_read(chip, LP5523_REG_STATUS, &status);
429 if (!(status & LP5523_LEDTEST_DONE))
430 usleep_range(3000, 6000);/* Was not ready. Wait. */
432 ret = lp55xx_read(chip, LP5523_REG_LED_TEST_ADC, &adc);
436 if (adc >= vdd || adc < LP5523_ADC_SHORTCIRC_LIM)
437 pos += sprintf(buf + pos, "LED %d FAIL\n", i);
439 lp55xx_write(chip, LP5523_REG_LED_PWM_BASE + i, 0x00);
441 /* Restore current */
442 lp55xx_write(chip, LP5523_REG_LED_CURRENT_BASE + i,
447 pos = sprintf(buf, "OK\n");
450 pos = sprintf(buf, "FAIL\n");
453 mutex_unlock(&chip->lock);
458 static void lp5523_led_brightness_work(struct work_struct *work)
460 struct lp55xx_led *led = container_of(work, struct lp55xx_led,
462 struct lp55xx_chip *chip = led->chip;
464 mutex_lock(&chip->lock);
465 lp55xx_write(chip, LP5523_REG_LED_PWM_BASE + led->chan_nr,
467 mutex_unlock(&chip->lock);
470 static DEVICE_ATTR(selftest, S_IRUGO, lp5523_selftest, NULL);
472 static struct attribute *lp5523_attributes[] = {
473 &dev_attr_selftest.attr,
477 static const struct attribute_group lp5523_group = {
478 .attrs = lp5523_attributes,
481 /* Chip specific configurations */
482 static struct lp55xx_device_config lp5523_cfg = {
484 .addr = LP5523_REG_RESET,
488 .addr = LP5523_REG_ENABLE,
489 .val = LP5523_ENABLE,
491 .max_channel = LP5523_MAX_LEDS,
492 .post_init_device = lp5523_post_init_device,
493 .brightness_work_fn = lp5523_led_brightness_work,
494 .set_led_current = lp5523_set_led_current,
495 .firmware_cb = lp5523_firmware_loaded,
496 .run_engine = lp5523_run_engine,
497 .dev_attr_group = &lp5523_group,
500 static int lp5523_probe(struct i2c_client *client,
501 const struct i2c_device_id *id)
504 struct lp55xx_chip *chip;
505 struct lp55xx_led *led;
506 struct lp55xx_platform_data *pdata;
507 struct device_node *np = client->dev.of_node;
509 if (!dev_get_platdata(&client->dev)) {
511 ret = lp55xx_of_populate_pdata(&client->dev, np);
515 dev_err(&client->dev, "no platform data\n");
519 pdata = dev_get_platdata(&client->dev);
521 chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
525 led = devm_kzalloc(&client->dev,
526 sizeof(*led) * pdata->num_channels, GFP_KERNEL);
532 chip->cfg = &lp5523_cfg;
534 mutex_init(&chip->lock);
536 i2c_set_clientdata(client, led);
538 ret = lp55xx_init_device(chip);
542 dev_info(&client->dev, "%s Programmable led chip found\n", id->name);
544 ret = lp55xx_register_leds(led, chip);
546 goto err_register_leds;
548 ret = lp55xx_register_sysfs(chip);
550 dev_err(&client->dev, "registering sysfs failed\n");
551 goto err_register_sysfs;
557 lp55xx_unregister_leds(led, chip);
559 lp55xx_deinit_device(chip);
564 static int lp5523_remove(struct i2c_client *client)
566 struct lp55xx_led *led = i2c_get_clientdata(client);
567 struct lp55xx_chip *chip = led->chip;
569 lp5523_stop_engine(chip);
570 lp55xx_unregister_sysfs(chip);
571 lp55xx_unregister_leds(led, chip);
572 lp55xx_deinit_device(chip);
577 static const struct i2c_device_id lp5523_id[] = {
578 { "lp5523", LP5523 },
579 { "lp55231", LP55231 },
583 MODULE_DEVICE_TABLE(i2c, lp5523_id);
586 static const struct of_device_id of_lp5523_leds_match[] = {
587 { .compatible = "national,lp5523", },
591 MODULE_DEVICE_TABLE(of, of_lp5523_leds_match);
594 static struct i2c_driver lp5523_driver = {
597 .of_match_table = of_match_ptr(of_lp5523_leds_match),
599 .probe = lp5523_probe,
600 .remove = lp5523_remove,
601 .id_table = lp5523_id,
604 module_i2c_driver(lp5523_driver);
606 MODULE_AUTHOR("Mathias Nyman <mathias.nyman@nokia.com>");
607 MODULE_AUTHOR("Milo Kim <milo.kim@ti.com>");
608 MODULE_DESCRIPTION("LP5523 LED engine");
609 MODULE_LICENSE("GPL");