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>
22 #include <linux/platform_data/leds-lp55xx.h>
23 #include <linux/slab.h>
25 #include "leds-lp55xx-common.h"
27 #define LP8501_PROGRAM_LENGTH 32
28 #define LP8501_MAX_LEDS 9
31 #define LP8501_REG_ENABLE 0x00
32 #define LP8501_ENABLE BIT(6)
33 #define LP8501_EXEC_M 0x3F
34 #define LP8501_EXEC_ENG1_M 0x30
35 #define LP8501_EXEC_ENG2_M 0x0C
36 #define LP8501_EXEC_ENG3_M 0x03
37 #define LP8501_RUN_ENG1 0x20
38 #define LP8501_RUN_ENG2 0x08
39 #define LP8501_RUN_ENG3 0x02
41 #define LP8501_REG_OP_MODE 0x01
42 #define LP8501_MODE_ENG1_M 0x30
43 #define LP8501_MODE_ENG2_M 0x0C
44 #define LP8501_MODE_ENG3_M 0x03
45 #define LP8501_LOAD_ENG1 0x10
46 #define LP8501_LOAD_ENG2 0x04
47 #define LP8501_LOAD_ENG3 0x01
49 #define LP8501_REG_PWR_CONFIG 0x05
50 #define LP8501_PWR_CONFIG_M 0x03
52 #define LP8501_REG_LED_PWM_BASE 0x16
54 #define LP8501_REG_LED_CURRENT_BASE 0x26
56 #define LP8501_REG_CONFIG 0x36
57 #define LP8501_PWM_PSAVE BIT(7)
58 #define LP8501_AUTO_INC BIT(6)
59 #define LP8501_PWR_SAVE BIT(5)
60 #define LP8501_CP_AUTO 0x18
61 #define LP8501_INT_CLK BIT(0)
62 #define LP8501_DEFAULT_CFG \
63 (LP8501_PWM_PSAVE | LP8501_AUTO_INC | LP8501_PWR_SAVE | LP8501_CP_AUTO)
65 #define LP8501_REG_RESET 0x3D
66 #define LP8501_RESET 0xFF
68 #define LP8501_REG_PROG_PAGE_SEL 0x4F
69 #define LP8501_PAGE_ENG1 0
70 #define LP8501_PAGE_ENG2 1
71 #define LP8501_PAGE_ENG3 2
73 #define LP8501_REG_PROG_MEM 0x50
75 #define LP8501_ENG1_IS_LOADING(mode) \
76 ((mode & LP8501_MODE_ENG1_M) == LP8501_LOAD_ENG1)
77 #define LP8501_ENG2_IS_LOADING(mode) \
78 ((mode & LP8501_MODE_ENG2_M) == LP8501_LOAD_ENG2)
79 #define LP8501_ENG3_IS_LOADING(mode) \
80 ((mode & LP8501_MODE_ENG3_M) == LP8501_LOAD_ENG3)
82 static inline void lp8501_wait_opmode_done(void)
84 usleep_range(1000, 2000);
87 static void lp8501_set_led_current(struct lp55xx_led *led, u8 led_current)
89 led->led_current = led_current;
90 lp55xx_write(led->chip, LP8501_REG_LED_CURRENT_BASE + led->chan_nr,
94 static int lp8501_post_init_device(struct lp55xx_chip *chip)
97 u8 val = LP8501_DEFAULT_CFG;
99 ret = lp55xx_write(chip, LP8501_REG_ENABLE, LP8501_ENABLE);
103 /* Chip startup time is 500 us, 1 - 2 ms gives some margin */
104 usleep_range(1000, 2000);
106 if (chip->pdata->clock_mode != LP55XX_CLOCK_EXT)
107 val |= LP8501_INT_CLK;
109 ret = lp55xx_write(chip, LP8501_REG_CONFIG, val);
113 /* Power selection for each output */
114 return lp55xx_update_bits(chip, LP8501_REG_PWR_CONFIG,
115 LP8501_PWR_CONFIG_M, chip->pdata->pwr_sel);
118 static void lp8501_load_engine(struct lp55xx_chip *chip)
120 enum lp55xx_engine_index idx = chip->engine_idx;
122 [LP55XX_ENGINE_1] = LP8501_MODE_ENG1_M,
123 [LP55XX_ENGINE_2] = LP8501_MODE_ENG2_M,
124 [LP55XX_ENGINE_3] = LP8501_MODE_ENG3_M,
128 [LP55XX_ENGINE_1] = LP8501_LOAD_ENG1,
129 [LP55XX_ENGINE_2] = LP8501_LOAD_ENG2,
130 [LP55XX_ENGINE_3] = LP8501_LOAD_ENG3,
134 [LP55XX_ENGINE_1] = LP8501_PAGE_ENG1,
135 [LP55XX_ENGINE_2] = LP8501_PAGE_ENG2,
136 [LP55XX_ENGINE_3] = LP8501_PAGE_ENG3,
139 lp55xx_update_bits(chip, LP8501_REG_OP_MODE, mask[idx], val[idx]);
141 lp8501_wait_opmode_done();
143 lp55xx_write(chip, LP8501_REG_PROG_PAGE_SEL, page_sel[idx]);
146 static void lp8501_stop_engine(struct lp55xx_chip *chip)
148 lp55xx_write(chip, LP8501_REG_OP_MODE, 0);
149 lp8501_wait_opmode_done();
152 static void lp8501_turn_off_channels(struct lp55xx_chip *chip)
156 for (i = 0; i < LP8501_MAX_LEDS; i++)
157 lp55xx_write(chip, LP8501_REG_LED_PWM_BASE + i, 0);
160 static void lp8501_run_engine(struct lp55xx_chip *chip, bool start)
168 lp8501_stop_engine(chip);
169 lp8501_turn_off_channels(chip);
175 * operation mode and enable register should updated at the same time
178 ret = lp55xx_read(chip, LP8501_REG_OP_MODE, &mode);
182 ret = lp55xx_read(chip, LP8501_REG_ENABLE, &exec);
186 /* change operation mode to RUN only when each engine is loading */
187 if (LP8501_ENG1_IS_LOADING(mode)) {
188 mode = (mode & ~LP8501_MODE_ENG1_M) | LP8501_RUN_ENG1;
189 exec = (exec & ~LP8501_EXEC_ENG1_M) | LP8501_RUN_ENG1;
192 if (LP8501_ENG2_IS_LOADING(mode)) {
193 mode = (mode & ~LP8501_MODE_ENG2_M) | LP8501_RUN_ENG2;
194 exec = (exec & ~LP8501_EXEC_ENG2_M) | LP8501_RUN_ENG2;
197 if (LP8501_ENG3_IS_LOADING(mode)) {
198 mode = (mode & ~LP8501_MODE_ENG3_M) | LP8501_RUN_ENG3;
199 exec = (exec & ~LP8501_EXEC_ENG3_M) | LP8501_RUN_ENG3;
202 lp55xx_write(chip, LP8501_REG_OP_MODE, mode);
203 lp8501_wait_opmode_done();
205 lp55xx_update_bits(chip, LP8501_REG_ENABLE, LP8501_EXEC_M, exec);
208 static int lp8501_update_program_memory(struct lp55xx_chip *chip,
209 const u8 *data, size_t size)
211 u8 pattern[LP8501_PROGRAM_LENGTH] = {0};
220 /* clear program memory before updating */
221 for (i = 0; i < LP8501_PROGRAM_LENGTH; i++)
222 lp55xx_write(chip, LP8501_REG_PROG_MEM + i, 0);
225 while ((offset < size - 1) && (i < LP8501_PROGRAM_LENGTH)) {
226 /* separate sscanfs because length is working only for %s */
227 ret = sscanf(data + offset, "%2s%n ", c, &nrchars);
231 ret = sscanf(c, "%2x", &cmd);
235 pattern[i] = (u8)cmd;
240 /* Each instruction is 16bit long. Check that length is even */
245 for (i = 0; i < update_size; i++)
246 lp55xx_write(chip, LP8501_REG_PROG_MEM + i, pattern[i]);
251 dev_err(&chip->cl->dev, "wrong pattern format\n");
255 static void lp8501_firmware_loaded(struct lp55xx_chip *chip)
257 const struct firmware *fw = chip->fw;
259 if (fw->size > LP8501_PROGRAM_LENGTH) {
260 dev_err(&chip->cl->dev, "firmware data size overflow: %zu\n",
266 * Program memory sequence
267 * 1) set engine mode to "LOAD"
268 * 2) write firmware data into program memory
271 lp8501_load_engine(chip);
272 lp8501_update_program_memory(chip, fw->data, fw->size);
275 static int lp8501_led_brightness(struct lp55xx_led *led)
277 struct lp55xx_chip *chip = led->chip;
280 mutex_lock(&chip->lock);
281 ret = lp55xx_write(chip, LP8501_REG_LED_PWM_BASE + led->chan_nr,
283 mutex_unlock(&chip->lock);
288 /* Chip specific configurations */
289 static struct lp55xx_device_config lp8501_cfg = {
291 .addr = LP8501_REG_RESET,
295 .addr = LP8501_REG_ENABLE,
296 .val = LP8501_ENABLE,
298 .max_channel = LP8501_MAX_LEDS,
299 .post_init_device = lp8501_post_init_device,
300 .brightness_fn = lp8501_led_brightness,
301 .set_led_current = lp8501_set_led_current,
302 .firmware_cb = lp8501_firmware_loaded,
303 .run_engine = lp8501_run_engine,
306 static int lp8501_probe(struct i2c_client *client,
307 const struct i2c_device_id *id)
310 struct lp55xx_chip *chip;
311 struct lp55xx_led *led;
312 struct lp55xx_platform_data *pdata = dev_get_platdata(&client->dev);
313 struct device_node *np = client->dev.of_node;
317 pdata = lp55xx_of_populate_pdata(&client->dev, np);
319 return PTR_ERR(pdata);
321 dev_err(&client->dev, "no platform data\n");
326 chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
330 led = devm_kzalloc(&client->dev,
331 sizeof(*led) * pdata->num_channels, GFP_KERNEL);
337 chip->cfg = &lp8501_cfg;
339 mutex_init(&chip->lock);
341 i2c_set_clientdata(client, led);
343 ret = lp55xx_init_device(chip);
347 dev_info(&client->dev, "%s Programmable led chip found\n", id->name);
349 ret = lp55xx_register_leds(led, chip);
351 goto err_register_leds;
353 ret = lp55xx_register_sysfs(chip);
355 dev_err(&client->dev, "registering sysfs failed\n");
356 goto err_register_sysfs;
362 lp55xx_unregister_leds(led, chip);
364 lp55xx_deinit_device(chip);
369 static int lp8501_remove(struct i2c_client *client)
371 struct lp55xx_led *led = i2c_get_clientdata(client);
372 struct lp55xx_chip *chip = led->chip;
374 lp8501_stop_engine(chip);
375 lp55xx_unregister_sysfs(chip);
376 lp55xx_unregister_leds(led, chip);
377 lp55xx_deinit_device(chip);
382 static const struct i2c_device_id lp8501_id[] = {
386 MODULE_DEVICE_TABLE(i2c, lp8501_id);
389 static const struct of_device_id of_lp8501_leds_match[] = {
390 { .compatible = "ti,lp8501", },
394 MODULE_DEVICE_TABLE(of, of_lp8501_leds_match);
397 static struct i2c_driver lp8501_driver = {
400 .of_match_table = of_match_ptr(of_lp8501_leds_match),
402 .probe = lp8501_probe,
403 .remove = lp8501_remove,
404 .id_table = lp8501_id,
407 module_i2c_driver(lp8501_driver);
409 MODULE_DESCRIPTION("Texas Instruments LP8501 LED driver");
410 MODULE_AUTHOR("Milo Kim");
411 MODULE_LICENSE("GPL");