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_PROG_PAGE_SEL 0x4F
53 #define LP5523_REG_PROG_MEM 0x50
55 /* Bit description in registers */
56 #define LP5523_ENABLE 0x40
57 #define LP5523_AUTO_INC 0x40
58 #define LP5523_PWR_SAVE 0x20
59 #define LP5523_PWM_PWR_SAVE 0x04
60 #define LP5523_CP_AUTO 0x18
61 #define LP5523_AUTO_CLK 0x02
63 #define LP5523_EN_LEDTEST 0x80
64 #define LP5523_LEDTEST_DONE 0x80
65 #define LP5523_RESET 0xFF
66 #define LP5523_ADC_SHORTCIRC_LIM 80
67 #define LP5523_EXT_CLK_USED 0x08
69 /* Memory Page Selection */
70 #define LP5523_PAGE_ENG1 0
71 #define LP5523_PAGE_ENG2 1
72 #define LP5523_PAGE_ENG3 2
74 /* Program Memory Operations */
75 #define LP5523_MODE_ENG1_M 0x30 /* Operation Mode Register */
76 #define LP5523_MODE_ENG2_M 0x0C
77 #define LP5523_MODE_ENG3_M 0x03
78 #define LP5523_LOAD_ENG1 0x10
79 #define LP5523_LOAD_ENG2 0x04
80 #define LP5523_LOAD_ENG3 0x01
82 #define LP5523_ENG1_IS_LOADING(mode) \
83 ((mode & LP5523_MODE_ENG1_M) == LP5523_LOAD_ENG1)
84 #define LP5523_ENG2_IS_LOADING(mode) \
85 ((mode & LP5523_MODE_ENG2_M) == LP5523_LOAD_ENG2)
86 #define LP5523_ENG3_IS_LOADING(mode) \
87 ((mode & LP5523_MODE_ENG3_M) == LP5523_LOAD_ENG3)
89 #define LP5523_EXEC_ENG1_M 0x30 /* Enable Register */
90 #define LP5523_EXEC_ENG2_M 0x0C
91 #define LP5523_EXEC_ENG3_M 0x03
92 #define LP5523_EXEC_M 0x3F
93 #define LP5523_RUN_ENG1 0x20
94 #define LP5523_RUN_ENG2 0x08
95 #define LP5523_RUN_ENG3 0x02
102 static inline void lp5523_wait_opmode_done(void)
104 usleep_range(1000, 2000);
107 static void lp5523_set_led_current(struct lp55xx_led
*led
, u8 led_current
)
109 led
->led_current
= led_current
;
110 lp55xx_write(led
->chip
, LP5523_REG_LED_CURRENT_BASE
+ led
->chan_nr
,
114 static int lp5523_post_init_device(struct lp55xx_chip
*chip
)
118 ret
= lp55xx_write(chip
, LP5523_REG_ENABLE
, LP5523_ENABLE
);
122 /* Chip startup time is 500 us, 1 - 2 ms gives some margin */
123 usleep_range(1000, 2000);
125 ret
= lp55xx_write(chip
, LP5523_REG_CONFIG
,
126 LP5523_AUTO_INC
| LP5523_PWR_SAVE
|
127 LP5523_CP_AUTO
| LP5523_AUTO_CLK
|
128 LP5523_PWM_PWR_SAVE
);
132 /* turn on all leds */
133 ret
= lp55xx_write(chip
, LP5523_REG_ENABLE_LEDS_MSB
, 0x01);
137 return lp55xx_write(chip
, LP5523_REG_ENABLE_LEDS_LSB
, 0xff);
140 static void lp5523_load_engine(struct lp55xx_chip
*chip
)
142 enum lp55xx_engine_index idx
= chip
->engine_idx
;
144 [LP55XX_ENGINE_1
] = LP5523_MODE_ENG1_M
,
145 [LP55XX_ENGINE_2
] = LP5523_MODE_ENG2_M
,
146 [LP55XX_ENGINE_3
] = LP5523_MODE_ENG3_M
,
150 [LP55XX_ENGINE_1
] = LP5523_LOAD_ENG1
,
151 [LP55XX_ENGINE_2
] = LP5523_LOAD_ENG2
,
152 [LP55XX_ENGINE_3
] = LP5523_LOAD_ENG3
,
155 lp55xx_update_bits(chip
, LP5523_REG_OP_MODE
, mask
[idx
], val
[idx
]);
157 lp5523_wait_opmode_done();
160 static void lp5523_load_engine_and_select_page(struct lp55xx_chip
*chip
)
162 enum lp55xx_engine_index idx
= chip
->engine_idx
;
164 [LP55XX_ENGINE_1
] = LP5523_PAGE_ENG1
,
165 [LP55XX_ENGINE_2
] = LP5523_PAGE_ENG2
,
166 [LP55XX_ENGINE_3
] = LP5523_PAGE_ENG3
,
169 lp5523_load_engine(chip
);
171 lp55xx_write(chip
, LP5523_REG_PROG_PAGE_SEL
, page_sel
[idx
]);
174 static void lp5523_stop_engine(struct lp55xx_chip
*chip
)
176 lp55xx_write(chip
, LP5523_REG_OP_MODE
, 0);
177 lp5523_wait_opmode_done();
180 static void lp5523_turn_off_channels(struct lp55xx_chip
*chip
)
184 for (i
= 0; i
< LP5523_MAX_LEDS
; i
++)
185 lp55xx_write(chip
, LP5523_REG_LED_PWM_BASE
+ i
, 0);
188 static void lp5523_run_engine(struct lp55xx_chip
*chip
, bool start
)
196 lp5523_stop_engine(chip
);
197 lp5523_turn_off_channels(chip
);
203 * operation mode and enable register should updated at the same time
206 ret
= lp55xx_read(chip
, LP5523_REG_OP_MODE
, &mode
);
210 ret
= lp55xx_read(chip
, LP5523_REG_ENABLE
, &exec
);
214 /* change operation mode to RUN only when each engine is loading */
215 if (LP5523_ENG1_IS_LOADING(mode
)) {
216 mode
= (mode
& ~LP5523_MODE_ENG1_M
) | LP5523_RUN_ENG1
;
217 exec
= (exec
& ~LP5523_EXEC_ENG1_M
) | LP5523_RUN_ENG1
;
220 if (LP5523_ENG2_IS_LOADING(mode
)) {
221 mode
= (mode
& ~LP5523_MODE_ENG2_M
) | LP5523_RUN_ENG2
;
222 exec
= (exec
& ~LP5523_EXEC_ENG2_M
) | LP5523_RUN_ENG2
;
225 if (LP5523_ENG3_IS_LOADING(mode
)) {
226 mode
= (mode
& ~LP5523_MODE_ENG3_M
) | LP5523_RUN_ENG3
;
227 exec
= (exec
& ~LP5523_EXEC_ENG3_M
) | LP5523_RUN_ENG3
;
230 lp55xx_write(chip
, LP5523_REG_OP_MODE
, mode
);
231 lp5523_wait_opmode_done();
233 lp55xx_update_bits(chip
, LP5523_REG_ENABLE
, LP5523_EXEC_M
, exec
);
236 static int lp5523_update_program_memory(struct lp55xx_chip
*chip
,
237 const u8
*data
, size_t size
)
239 u8 pattern
[LP5523_PROGRAM_LENGTH
] = {0};
248 /* clear program memory before updating */
249 for (i
= 0; i
< LP5523_PROGRAM_LENGTH
; i
++)
250 lp55xx_write(chip
, LP5523_REG_PROG_MEM
+ i
, 0);
253 while ((offset
< size
- 1) && (i
< LP5523_PROGRAM_LENGTH
)) {
254 /* separate sscanfs because length is working only for %s */
255 ret
= sscanf(data
+ offset
, "%2s%n ", c
, &nrchars
);
259 ret
= sscanf(c
, "%2x", &cmd
);
263 pattern
[i
] = (u8
)cmd
;
268 /* Each instruction is 16bit long. Check that length is even */
273 for (i
= 0; i
< update_size
; i
++)
274 lp55xx_write(chip
, LP5523_REG_PROG_MEM
+ i
, pattern
[i
]);
279 dev_err(&chip
->cl
->dev
, "wrong pattern format\n");
283 static void lp5523_firmware_loaded(struct lp55xx_chip
*chip
)
285 const struct firmware
*fw
= chip
->fw
;
287 if (fw
->size
> LP5523_PROGRAM_LENGTH
) {
288 dev_err(&chip
->cl
->dev
, "firmware data size overflow: %zu\n",
294 * Program momery sequence
295 * 1) set engine mode to "LOAD"
296 * 2) write firmware data into program memory
299 lp5523_load_engine_and_select_page(chip
);
300 lp5523_update_program_memory(chip
, fw
->data
, fw
->size
);
303 static ssize_t
lp5523_selftest(struct device
*dev
,
304 struct device_attribute
*attr
,
307 struct lp55xx_led
*led
= i2c_get_clientdata(to_i2c_client(dev
));
308 struct lp55xx_chip
*chip
= led
->chip
;
309 struct lp55xx_platform_data
*pdata
= chip
->pdata
;
313 mutex_lock(&chip
->lock
);
315 ret
= lp55xx_read(chip
, LP5523_REG_STATUS
, &status
);
319 /* Check that ext clock is really in use if requested */
320 if (pdata
->clock_mode
== LP55XX_CLOCK_EXT
) {
321 if ((status
& LP5523_EXT_CLK_USED
) == 0)
325 /* Measure VDD (i.e. VBAT) first (channel 16 corresponds to VDD) */
326 lp55xx_write(chip
, LP5523_REG_LED_TEST_CTRL
, LP5523_EN_LEDTEST
| 16);
327 usleep_range(3000, 6000); /* ADC conversion time is typically 2.7 ms */
328 ret
= lp55xx_read(chip
, LP5523_REG_STATUS
, &status
);
332 if (!(status
& LP5523_LEDTEST_DONE
))
333 usleep_range(3000, 6000); /* Was not ready. Wait little bit */
335 ret
= lp55xx_read(chip
, LP5523_REG_LED_TEST_ADC
, &vdd
);
339 vdd
--; /* There may be some fluctuation in measurement */
341 for (i
= 0; i
< LP5523_MAX_LEDS
; i
++) {
342 /* Skip non-existing channels */
343 if (pdata
->led_config
[i
].led_current
== 0)
346 /* Set default current */
347 lp55xx_write(chip
, LP5523_REG_LED_CURRENT_BASE
+ i
,
348 pdata
->led_config
[i
].led_current
);
350 lp55xx_write(chip
, LP5523_REG_LED_PWM_BASE
+ i
, 0xff);
351 /* let current stabilize 2 - 4ms before measurements start */
352 usleep_range(2000, 4000);
353 lp55xx_write(chip
, LP5523_REG_LED_TEST_CTRL
,
354 LP5523_EN_LEDTEST
| i
);
355 /* ADC conversion time is 2.7 ms typically */
356 usleep_range(3000, 6000);
357 ret
= lp55xx_read(chip
, LP5523_REG_STATUS
, &status
);
361 if (!(status
& LP5523_LEDTEST_DONE
))
362 usleep_range(3000, 6000);/* Was not ready. Wait. */
364 ret
= lp55xx_read(chip
, LP5523_REG_LED_TEST_ADC
, &adc
);
368 if (adc
>= vdd
|| adc
< LP5523_ADC_SHORTCIRC_LIM
)
369 pos
+= sprintf(buf
+ pos
, "LED %d FAIL\n", i
);
371 lp55xx_write(chip
, LP5523_REG_LED_PWM_BASE
+ i
, 0x00);
373 /* Restore current */
374 lp55xx_write(chip
, LP5523_REG_LED_CURRENT_BASE
+ i
,
379 pos
= sprintf(buf
, "OK\n");
382 pos
= sprintf(buf
, "FAIL\n");
385 mutex_unlock(&chip
->lock
);
390 static void lp5523_led_brightness_work(struct work_struct
*work
)
392 struct lp55xx_led
*led
= container_of(work
, struct lp55xx_led
,
394 struct lp55xx_chip
*chip
= led
->chip
;
396 mutex_lock(&chip
->lock
);
397 lp55xx_write(chip
, LP5523_REG_LED_PWM_BASE
+ led
->chan_nr
,
399 mutex_unlock(&chip
->lock
);
402 static DEVICE_ATTR(selftest
, S_IRUGO
, lp5523_selftest
, NULL
);
404 static struct attribute
*lp5523_attributes
[] = {
405 &dev_attr_selftest
.attr
,
409 static const struct attribute_group lp5523_group
= {
410 .attrs
= lp5523_attributes
,
413 /* Chip specific configurations */
414 static struct lp55xx_device_config lp5523_cfg
= {
416 .addr
= LP5523_REG_RESET
,
420 .addr
= LP5523_REG_ENABLE
,
421 .val
= LP5523_ENABLE
,
423 .max_channel
= LP5523_MAX_LEDS
,
424 .post_init_device
= lp5523_post_init_device
,
425 .brightness_work_fn
= lp5523_led_brightness_work
,
426 .set_led_current
= lp5523_set_led_current
,
427 .firmware_cb
= lp5523_firmware_loaded
,
428 .run_engine
= lp5523_run_engine
,
429 .dev_attr_group
= &lp5523_group
,
432 static int lp5523_probe(struct i2c_client
*client
,
433 const struct i2c_device_id
*id
)
436 struct lp55xx_chip
*chip
;
437 struct lp55xx_led
*led
;
438 struct lp55xx_platform_data
*pdata
;
439 struct device_node
*np
= client
->dev
.of_node
;
441 if (!dev_get_platdata(&client
->dev
)) {
443 ret
= lp55xx_of_populate_pdata(&client
->dev
, np
);
447 dev_err(&client
->dev
, "no platform data\n");
451 pdata
= dev_get_platdata(&client
->dev
);
453 chip
= devm_kzalloc(&client
->dev
, sizeof(*chip
), GFP_KERNEL
);
457 led
= devm_kzalloc(&client
->dev
,
458 sizeof(*led
) * pdata
->num_channels
, GFP_KERNEL
);
464 chip
->cfg
= &lp5523_cfg
;
466 mutex_init(&chip
->lock
);
468 i2c_set_clientdata(client
, led
);
470 ret
= lp55xx_init_device(chip
);
474 dev_info(&client
->dev
, "%s Programmable led chip found\n", id
->name
);
476 ret
= lp55xx_register_leds(led
, chip
);
478 goto err_register_leds
;
480 ret
= lp55xx_register_sysfs(chip
);
482 dev_err(&client
->dev
, "registering sysfs failed\n");
483 goto err_register_sysfs
;
489 lp55xx_unregister_leds(led
, chip
);
491 lp55xx_deinit_device(chip
);
496 static int lp5523_remove(struct i2c_client
*client
)
498 struct lp55xx_led
*led
= i2c_get_clientdata(client
);
499 struct lp55xx_chip
*chip
= led
->chip
;
501 lp5523_stop_engine(chip
);
502 lp55xx_unregister_sysfs(chip
);
503 lp55xx_unregister_leds(led
, chip
);
504 lp55xx_deinit_device(chip
);
509 static const struct i2c_device_id lp5523_id
[] = {
510 { "lp5523", LP5523
},
511 { "lp55231", LP55231
},
515 MODULE_DEVICE_TABLE(i2c
, lp5523_id
);
518 static const struct of_device_id of_lp5523_leds_match
[] = {
519 { .compatible
= "national,lp5523", },
523 MODULE_DEVICE_TABLE(of
, of_lp5523_leds_match
);
526 static struct i2c_driver lp5523_driver
= {
529 .of_match_table
= of_match_ptr(of_lp5523_leds_match
),
531 .probe
= lp5523_probe
,
532 .remove
= lp5523_remove
,
533 .id_table
= lp5523_id
,
536 module_i2c_driver(lp5523_driver
);
538 MODULE_AUTHOR("Mathias Nyman <mathias.nyman@nokia.com>");
539 MODULE_AUTHOR("Milo Kim <milo.kim@ti.com>");
540 MODULE_DESCRIPTION("LP5523 LED engine");
541 MODULE_LICENSE("GPL");