Merge branch 'fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/evalenti/linux...
[linux/fpc-iii.git] / drivers / leds / leds-lp8501.c
blob3f9675bd214a404fcd5e771ed87bf4079dacf938
1 /*
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/of.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
30 /* Registers */
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,
91 led_current);
94 static int lp8501_post_init_device(struct lp55xx_chip *chip)
96 int ret;
97 u8 val = LP8501_DEFAULT_CFG;
99 ret = lp55xx_write(chip, LP8501_REG_ENABLE, LP8501_ENABLE);
100 if (ret)
101 return ret;
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);
110 if (ret)
111 return ret;
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;
121 u8 mask[] = {
122 [LP55XX_ENGINE_1] = LP8501_MODE_ENG1_M,
123 [LP55XX_ENGINE_2] = LP8501_MODE_ENG2_M,
124 [LP55XX_ENGINE_3] = LP8501_MODE_ENG3_M,
127 u8 val[] = {
128 [LP55XX_ENGINE_1] = LP8501_LOAD_ENG1,
129 [LP55XX_ENGINE_2] = LP8501_LOAD_ENG2,
130 [LP55XX_ENGINE_3] = LP8501_LOAD_ENG3,
133 u8 page_sel[] = {
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)
154 int i;
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)
162 int ret;
163 u8 mode;
164 u8 exec;
166 /* stop engine */
167 if (!start) {
168 lp8501_stop_engine(chip);
169 lp8501_turn_off_channels(chip);
170 return;
174 * To run the engine,
175 * operation mode and enable register should updated at the same time
178 ret = lp55xx_read(chip, LP8501_REG_OP_MODE, &mode);
179 if (ret)
180 return;
182 ret = lp55xx_read(chip, LP8501_REG_ENABLE, &exec);
183 if (ret)
184 return;
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};
212 unsigned cmd;
213 char c[3];
214 int update_size;
215 int nrchars;
216 int offset = 0;
217 int ret;
218 int i;
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);
224 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);
228 if (ret != 1)
229 goto err;
231 ret = sscanf(c, "%2x", &cmd);
232 if (ret != 1)
233 goto err;
235 pattern[i] = (u8)cmd;
236 offset += nrchars;
237 i++;
240 /* Each instruction is 16bit long. Check that length is even */
241 if (i % 2)
242 goto err;
244 update_size = i;
245 for (i = 0; i < update_size; i++)
246 lp55xx_write(chip, LP8501_REG_PROG_MEM + i, pattern[i]);
248 return 0;
250 err:
251 dev_err(&chip->cl->dev, "wrong pattern format\n");
252 return -EINVAL;
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",
261 fw->size);
262 return;
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;
278 int ret;
280 mutex_lock(&chip->lock);
281 ret = lp55xx_write(chip, LP8501_REG_LED_PWM_BASE + led->chan_nr,
282 led->brightness);
283 mutex_unlock(&chip->lock);
285 return ret;
288 /* Chip specific configurations */
289 static struct lp55xx_device_config lp8501_cfg = {
290 .reset = {
291 .addr = LP8501_REG_RESET,
292 .val = LP8501_RESET,
294 .enable = {
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)
309 int ret;
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;
315 if (!pdata) {
316 if (np) {
317 pdata = lp55xx_of_populate_pdata(&client->dev, np);
318 if (IS_ERR(pdata))
319 return PTR_ERR(pdata);
320 } else {
321 dev_err(&client->dev, "no platform data\n");
322 return -EINVAL;
326 chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
327 if (!chip)
328 return -ENOMEM;
330 led = devm_kzalloc(&client->dev,
331 sizeof(*led) * pdata->num_channels, GFP_KERNEL);
332 if (!led)
333 return -ENOMEM;
335 chip->cl = client;
336 chip->pdata = pdata;
337 chip->cfg = &lp8501_cfg;
339 mutex_init(&chip->lock);
341 i2c_set_clientdata(client, led);
343 ret = lp55xx_init_device(chip);
344 if (ret)
345 goto err_init;
347 dev_info(&client->dev, "%s Programmable led chip found\n", id->name);
349 ret = lp55xx_register_leds(led, chip);
350 if (ret)
351 goto err_register_leds;
353 ret = lp55xx_register_sysfs(chip);
354 if (ret) {
355 dev_err(&client->dev, "registering sysfs failed\n");
356 goto err_register_sysfs;
359 return 0;
361 err_register_sysfs:
362 lp55xx_unregister_leds(led, chip);
363 err_register_leds:
364 lp55xx_deinit_device(chip);
365 err_init:
366 return ret;
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);
379 return 0;
382 static const struct i2c_device_id lp8501_id[] = {
383 { "lp8501", 0 },
386 MODULE_DEVICE_TABLE(i2c, lp8501_id);
388 #ifdef CONFIG_OF
389 static const struct of_device_id of_lp8501_leds_match[] = {
390 { .compatible = "ti,lp8501", },
394 MODULE_DEVICE_TABLE(of, of_lp8501_leds_match);
395 #endif
397 static struct i2c_driver lp8501_driver = {
398 .driver = {
399 .name = "lp8501",
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");