x86/xen: resume timer irqs early
[linux/fpc-iii.git] / drivers / leds / leds-lp8501.c
blob8d55a780ca4606e30ee2fd66ec3ec47417aea35d
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/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
29 /* Registers */
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,
90 led_current);
93 static int lp8501_post_init_device(struct lp55xx_chip *chip)
95 int ret;
96 u8 val = LP8501_DEFAULT_CFG;
98 ret = lp55xx_write(chip, LP8501_REG_ENABLE, LP8501_ENABLE);
99 if (ret)
100 return ret;
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);
109 if (ret)
110 return ret;
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;
120 u8 mask[] = {
121 [LP55XX_ENGINE_1] = LP8501_MODE_ENG1_M,
122 [LP55XX_ENGINE_2] = LP8501_MODE_ENG2_M,
123 [LP55XX_ENGINE_3] = LP8501_MODE_ENG3_M,
126 u8 val[] = {
127 [LP55XX_ENGINE_1] = LP8501_LOAD_ENG1,
128 [LP55XX_ENGINE_2] = LP8501_LOAD_ENG2,
129 [LP55XX_ENGINE_3] = LP8501_LOAD_ENG3,
132 u8 page_sel[] = {
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)
153 int i;
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)
161 int ret;
162 u8 mode;
163 u8 exec;
165 /* stop engine */
166 if (!start) {
167 lp8501_stop_engine(chip);
168 lp8501_turn_off_channels(chip);
169 return;
173 * To run the engine,
174 * operation mode and enable register should updated at the same time
177 ret = lp55xx_read(chip, LP8501_REG_OP_MODE, &mode);
178 if (ret)
179 return;
181 ret = lp55xx_read(chip, LP8501_REG_ENABLE, &exec);
182 if (ret)
183 return;
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};
211 unsigned cmd;
212 char c[3];
213 int update_size;
214 int nrchars;
215 int offset = 0;
216 int ret;
217 int i;
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);
223 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);
227 if (ret != 1)
228 goto err;
230 ret = sscanf(c, "%2x", &cmd);
231 if (ret != 1)
232 goto err;
234 pattern[i] = (u8)cmd;
235 offset += nrchars;
236 i++;
239 /* Each instruction is 16bit long. Check that length is even */
240 if (i % 2)
241 goto err;
243 update_size = i;
244 for (i = 0; i < update_size; i++)
245 lp55xx_write(chip, LP8501_REG_PROG_MEM + i, pattern[i]);
247 return 0;
249 err:
250 dev_err(&chip->cl->dev, "wrong pattern format\n");
251 return -EINVAL;
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",
260 fw->size);
261 return;
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,
277 brightness_work);
278 struct lp55xx_chip *chip = led->chip;
280 mutex_lock(&chip->lock);
281 lp55xx_write(chip, LP8501_REG_LED_PWM_BASE + led->chan_nr,
282 led->brightness);
283 mutex_unlock(&chip->lock);
286 /* Chip specific configurations */
287 static struct lp55xx_device_config lp8501_cfg = {
288 .reset = {
289 .addr = LP8501_REG_RESET,
290 .val = LP8501_RESET,
292 .enable = {
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)
307 int ret;
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)) {
314 if (np) {
315 ret = lp55xx_of_populate_pdata(&client->dev, np);
316 if (ret < 0)
317 return ret;
318 } else {
319 dev_err(&client->dev, "no platform data\n");
320 return -EINVAL;
323 pdata = dev_get_platdata(&client->dev);
325 chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
326 if (!chip)
327 return -ENOMEM;
329 led = devm_kzalloc(&client->dev,
330 sizeof(*led) * pdata->num_channels, GFP_KERNEL);
331 if (!led)
332 return -ENOMEM;
334 chip->cl = client;
335 chip->pdata = pdata;
336 chip->cfg = &lp8501_cfg;
338 mutex_init(&chip->lock);
340 i2c_set_clientdata(client, led);
342 ret = lp55xx_init_device(chip);
343 if (ret)
344 goto err_init;
346 dev_info(&client->dev, "%s Programmable led chip found\n", id->name);
348 ret = lp55xx_register_leds(led, chip);
349 if (ret)
350 goto err_register_leds;
352 ret = lp55xx_register_sysfs(chip);
353 if (ret) {
354 dev_err(&client->dev, "registering sysfs failed\n");
355 goto err_register_sysfs;
358 return 0;
360 err_register_sysfs:
361 lp55xx_unregister_leds(led, chip);
362 err_register_leds:
363 lp55xx_deinit_device(chip);
364 err_init:
365 return ret;
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);
378 return 0;
381 static const struct i2c_device_id lp8501_id[] = {
382 { "lp8501", 0 },
385 MODULE_DEVICE_TABLE(i2c, lp8501_id);
387 #ifdef CONFIG_OF
388 static const struct of_device_id of_lp8501_leds_match[] = {
389 { .compatible = "ti,lp8501", },
393 MODULE_DEVICE_TABLE(of, of_lp8501_leds_match);
394 #endif
396 static struct i2c_driver lp8501_driver = {
397 .driver = {
398 .name = "lp8501",
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");