2 * TI LP8501 9 channel LED Driver
4 * Copyright (C) 2013 Texas Instruments
6 * Author: Milo(Woogyom) Kim <milo.kim@ti.com>
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.
14 #include <linux/delay.h>
15 #include <linux/firmware.h>
16 #include <linux/i2c.h>
17 #include <linux/init.h>
18 #include <linux/leds.h>
19 #include <linux/module.h>
20 #include <linux/mutex.h>
21 #include <linux/platform_data/leds-lp55xx.h>
22 #include <linux/slab.h>
24 #include "leds-lp55xx-common.h"
26 #define LP8501_PROGRAM_LENGTH 32
27 #define LP8501_MAX_LEDS 9
30 #define LP8501_REG_ENABLE 0x00
31 #define LP8501_ENABLE BIT(6)
32 #define LP8501_EXEC_M 0x3F
33 #define LP8501_EXEC_ENG1_M 0x30
34 #define LP8501_EXEC_ENG2_M 0x0C
35 #define LP8501_EXEC_ENG3_M 0x03
36 #define LP8501_RUN_ENG1 0x20
37 #define LP8501_RUN_ENG2 0x08
38 #define LP8501_RUN_ENG3 0x02
40 #define LP8501_REG_OP_MODE 0x01
41 #define LP8501_MODE_ENG1_M 0x30
42 #define LP8501_MODE_ENG2_M 0x0C
43 #define LP8501_MODE_ENG3_M 0x03
44 #define LP8501_LOAD_ENG1 0x10
45 #define LP8501_LOAD_ENG2 0x04
46 #define LP8501_LOAD_ENG3 0x01
48 #define LP8501_REG_PWR_CONFIG 0x05
49 #define LP8501_PWR_CONFIG_M 0x03
51 #define LP8501_REG_LED_PWM_BASE 0x16
53 #define LP8501_REG_LED_CURRENT_BASE 0x26
55 #define LP8501_REG_CONFIG 0x36
56 #define LP8501_PWM_PSAVE BIT(7)
57 #define LP8501_AUTO_INC BIT(6)
58 #define LP8501_PWR_SAVE BIT(5)
59 #define LP8501_CP_AUTO 0x18
60 #define LP8501_INT_CLK BIT(0)
61 #define LP8501_DEFAULT_CFG \
62 (LP8501_PWM_PSAVE | LP8501_AUTO_INC | LP8501_PWR_SAVE | LP8501_CP_AUTO)
64 #define LP8501_REG_RESET 0x3D
65 #define LP8501_RESET 0xFF
67 #define LP8501_REG_PROG_PAGE_SEL 0x4F
68 #define LP8501_PAGE_ENG1 0
69 #define LP8501_PAGE_ENG2 1
70 #define LP8501_PAGE_ENG3 2
72 #define LP8501_REG_PROG_MEM 0x50
74 #define LP8501_ENG1_IS_LOADING(mode) \
75 ((mode & LP8501_MODE_ENG1_M) == LP8501_LOAD_ENG1)
76 #define LP8501_ENG2_IS_LOADING(mode) \
77 ((mode & LP8501_MODE_ENG2_M) == LP8501_LOAD_ENG2)
78 #define LP8501_ENG3_IS_LOADING(mode) \
79 ((mode & LP8501_MODE_ENG3_M) == LP8501_LOAD_ENG3)
81 static inline void lp8501_wait_opmode_done(void)
83 usleep_range(1000, 2000);
86 static void lp8501_set_led_current(struct lp55xx_led
*led
, u8 led_current
)
88 led
->led_current
= led_current
;
89 lp55xx_write(led
->chip
, LP8501_REG_LED_CURRENT_BASE
+ led
->chan_nr
,
93 static int lp8501_post_init_device(struct lp55xx_chip
*chip
)
96 u8 val
= LP8501_DEFAULT_CFG
;
98 ret
= lp55xx_write(chip
, LP8501_REG_ENABLE
, LP8501_ENABLE
);
102 /* Chip startup time is 500 us, 1 - 2 ms gives some margin */
103 usleep_range(1000, 2000);
105 if (chip
->pdata
->clock_mode
!= LP55XX_CLOCK_EXT
)
106 val
|= LP8501_INT_CLK
;
108 ret
= lp55xx_write(chip
, LP8501_REG_CONFIG
, val
);
112 /* Power selection for each output */
113 return lp55xx_update_bits(chip
, LP8501_REG_PWR_CONFIG
,
114 LP8501_PWR_CONFIG_M
, chip
->pdata
->pwr_sel
);
117 static void lp8501_load_engine(struct lp55xx_chip
*chip
)
119 enum lp55xx_engine_index idx
= chip
->engine_idx
;
121 [LP55XX_ENGINE_1
] = LP8501_MODE_ENG1_M
,
122 [LP55XX_ENGINE_2
] = LP8501_MODE_ENG2_M
,
123 [LP55XX_ENGINE_3
] = LP8501_MODE_ENG3_M
,
127 [LP55XX_ENGINE_1
] = LP8501_LOAD_ENG1
,
128 [LP55XX_ENGINE_2
] = LP8501_LOAD_ENG2
,
129 [LP55XX_ENGINE_3
] = LP8501_LOAD_ENG3
,
133 [LP55XX_ENGINE_1
] = LP8501_PAGE_ENG1
,
134 [LP55XX_ENGINE_2
] = LP8501_PAGE_ENG2
,
135 [LP55XX_ENGINE_3
] = LP8501_PAGE_ENG3
,
138 lp55xx_update_bits(chip
, LP8501_REG_OP_MODE
, mask
[idx
], val
[idx
]);
140 lp8501_wait_opmode_done();
142 lp55xx_write(chip
, LP8501_REG_PROG_PAGE_SEL
, page_sel
[idx
]);
145 static void lp8501_stop_engine(struct lp55xx_chip
*chip
)
147 lp55xx_write(chip
, LP8501_REG_OP_MODE
, 0);
148 lp8501_wait_opmode_done();
151 static void lp8501_turn_off_channels(struct lp55xx_chip
*chip
)
155 for (i
= 0; i
< LP8501_MAX_LEDS
; i
++)
156 lp55xx_write(chip
, LP8501_REG_LED_PWM_BASE
+ i
, 0);
159 static void lp8501_run_engine(struct lp55xx_chip
*chip
, bool start
)
167 lp8501_stop_engine(chip
);
168 lp8501_turn_off_channels(chip
);
174 * operation mode and enable register should updated at the same time
177 ret
= lp55xx_read(chip
, LP8501_REG_OP_MODE
, &mode
);
181 ret
= lp55xx_read(chip
, LP8501_REG_ENABLE
, &exec
);
185 /* change operation mode to RUN only when each engine is loading */
186 if (LP8501_ENG1_IS_LOADING(mode
)) {
187 mode
= (mode
& ~LP8501_MODE_ENG1_M
) | LP8501_RUN_ENG1
;
188 exec
= (exec
& ~LP8501_EXEC_ENG1_M
) | LP8501_RUN_ENG1
;
191 if (LP8501_ENG2_IS_LOADING(mode
)) {
192 mode
= (mode
& ~LP8501_MODE_ENG2_M
) | LP8501_RUN_ENG2
;
193 exec
= (exec
& ~LP8501_EXEC_ENG2_M
) | LP8501_RUN_ENG2
;
196 if (LP8501_ENG3_IS_LOADING(mode
)) {
197 mode
= (mode
& ~LP8501_MODE_ENG3_M
) | LP8501_RUN_ENG3
;
198 exec
= (exec
& ~LP8501_EXEC_ENG3_M
) | LP8501_RUN_ENG3
;
201 lp55xx_write(chip
, LP8501_REG_OP_MODE
, mode
);
202 lp8501_wait_opmode_done();
204 lp55xx_update_bits(chip
, LP8501_REG_ENABLE
, LP8501_EXEC_M
, exec
);
207 static int lp8501_update_program_memory(struct lp55xx_chip
*chip
,
208 const u8
*data
, size_t size
)
210 u8 pattern
[LP8501_PROGRAM_LENGTH
] = {0};
219 /* clear program memory before updating */
220 for (i
= 0; i
< LP8501_PROGRAM_LENGTH
; i
++)
221 lp55xx_write(chip
, LP8501_REG_PROG_MEM
+ i
, 0);
224 while ((offset
< size
- 1) && (i
< LP8501_PROGRAM_LENGTH
)) {
225 /* separate sscanfs because length is working only for %s */
226 ret
= sscanf(data
+ offset
, "%2s%n ", c
, &nrchars
);
230 ret
= sscanf(c
, "%2x", &cmd
);
234 pattern
[i
] = (u8
)cmd
;
239 /* Each instruction is 16bit long. Check that length is even */
244 for (i
= 0; i
< update_size
; i
++)
245 lp55xx_write(chip
, LP8501_REG_PROG_MEM
+ i
, pattern
[i
]);
250 dev_err(&chip
->cl
->dev
, "wrong pattern format\n");
254 static void lp8501_firmware_loaded(struct lp55xx_chip
*chip
)
256 const struct firmware
*fw
= chip
->fw
;
258 if (fw
->size
> LP8501_PROGRAM_LENGTH
) {
259 dev_err(&chip
->cl
->dev
, "firmware data size overflow: %zu\n",
265 * Program momery sequence
266 * 1) set engine mode to "LOAD"
267 * 2) write firmware data into program memory
270 lp8501_load_engine(chip
);
271 lp8501_update_program_memory(chip
, fw
->data
, fw
->size
);
274 static void lp8501_led_brightness_work(struct work_struct
*work
)
276 struct lp55xx_led
*led
= container_of(work
, struct lp55xx_led
,
278 struct lp55xx_chip
*chip
= led
->chip
;
280 mutex_lock(&chip
->lock
);
281 lp55xx_write(chip
, LP8501_REG_LED_PWM_BASE
+ led
->chan_nr
,
283 mutex_unlock(&chip
->lock
);
286 /* Chip specific configurations */
287 static struct lp55xx_device_config lp8501_cfg
= {
289 .addr
= LP8501_REG_RESET
,
293 .addr
= LP8501_REG_ENABLE
,
294 .val
= LP8501_ENABLE
,
296 .max_channel
= LP8501_MAX_LEDS
,
297 .post_init_device
= lp8501_post_init_device
,
298 .brightness_work_fn
= lp8501_led_brightness_work
,
299 .set_led_current
= lp8501_set_led_current
,
300 .firmware_cb
= lp8501_firmware_loaded
,
301 .run_engine
= lp8501_run_engine
,
304 static int lp8501_probe(struct i2c_client
*client
,
305 const struct i2c_device_id
*id
)
308 struct lp55xx_chip
*chip
;
309 struct lp55xx_led
*led
;
310 struct lp55xx_platform_data
*pdata
;
311 struct device_node
*np
= client
->dev
.of_node
;
313 if (!dev_get_platdata(&client
->dev
)) {
315 ret
= lp55xx_of_populate_pdata(&client
->dev
, np
);
319 dev_err(&client
->dev
, "no platform data\n");
323 pdata
= dev_get_platdata(&client
->dev
);
325 chip
= devm_kzalloc(&client
->dev
, sizeof(*chip
), GFP_KERNEL
);
329 led
= devm_kzalloc(&client
->dev
,
330 sizeof(*led
) * pdata
->num_channels
, GFP_KERNEL
);
336 chip
->cfg
= &lp8501_cfg
;
338 mutex_init(&chip
->lock
);
340 i2c_set_clientdata(client
, led
);
342 ret
= lp55xx_init_device(chip
);
346 dev_info(&client
->dev
, "%s Programmable led chip found\n", id
->name
);
348 ret
= lp55xx_register_leds(led
, chip
);
350 goto err_register_leds
;
352 ret
= lp55xx_register_sysfs(chip
);
354 dev_err(&client
->dev
, "registering sysfs failed\n");
355 goto err_register_sysfs
;
361 lp55xx_unregister_leds(led
, chip
);
363 lp55xx_deinit_device(chip
);
368 static int lp8501_remove(struct i2c_client
*client
)
370 struct lp55xx_led
*led
= i2c_get_clientdata(client
);
371 struct lp55xx_chip
*chip
= led
->chip
;
373 lp8501_stop_engine(chip
);
374 lp55xx_unregister_sysfs(chip
);
375 lp55xx_unregister_leds(led
, chip
);
376 lp55xx_deinit_device(chip
);
381 static const struct i2c_device_id lp8501_id
[] = {
385 MODULE_DEVICE_TABLE(i2c
, lp8501_id
);
388 static const struct of_device_id of_lp8501_leds_match
[] = {
389 { .compatible
= "ti,lp8501", },
393 MODULE_DEVICE_TABLE(of
, of_lp8501_leds_match
);
396 static struct i2c_driver lp8501_driver
= {
399 .of_match_table
= of_match_ptr(of_lp8501_leds_match
),
401 .probe
= lp8501_probe
,
402 .remove
= lp8501_remove
,
403 .id_table
= lp8501_id
,
406 module_i2c_driver(lp8501_driver
);
408 MODULE_DESCRIPTION("Texas Instruments LP8501 LED drvier");
409 MODULE_AUTHOR("Milo Kim");
410 MODULE_LICENSE("GPL");