leds: add new LP5562 LED driver
[deliverable/linux.git] / drivers / leds / leds-lp5562.c
CommitLineData
ff45262a
KM
1/*
2 * LP5562 LED driver
3 *
4 * Copyright (C) 2013 Texas Instruments
5 *
6 * Author: Milo(Woogyom) Kim <milo.kim@ti.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 */
12
13#include <linux/delay.h>
14#include <linux/firmware.h>
15#include <linux/i2c.h>
16#include <linux/init.h>
17#include <linux/leds.h>
18#include <linux/module.h>
19#include <linux/mutex.h>
20#include <linux/platform_data/leds-lp55xx.h>
21#include <linux/slab.h>
22
23#include "leds-lp55xx-common.h"
24
25#define LP5562_PROGRAM_LENGTH 32
26#define LP5562_MAX_LEDS 4
27
28/* ENABLE Register 00h */
29#define LP5562_REG_ENABLE 0x00
30#define LP5562_EXEC_ENG1_M 0x30
31#define LP5562_EXEC_ENG2_M 0x0C
32#define LP5562_EXEC_ENG3_M 0x03
33#define LP5562_EXEC_M 0x3F
34#define LP5562_MASTER_ENABLE 0x40 /* Chip master enable */
35#define LP5562_LOGARITHMIC_PWM 0x80 /* Logarithmic PWM adjustment */
36#define LP5562_EXEC_RUN 0x2A
37#define LP5562_ENABLE_DEFAULT \
38 (LP5562_MASTER_ENABLE | LP5562_LOGARITHMIC_PWM)
39#define LP5562_ENABLE_RUN_PROGRAM \
40 (LP5562_ENABLE_DEFAULT | LP5562_EXEC_RUN)
41
42/* OPMODE Register 01h */
43#define LP5562_REG_OP_MODE 0x01
44#define LP5562_MODE_ENG1_M 0x30
45#define LP5562_MODE_ENG2_M 0x0C
46#define LP5562_MODE_ENG3_M 0x03
47#define LP5562_LOAD_ENG1 0x10
48#define LP5562_LOAD_ENG2 0x04
49#define LP5562_LOAD_ENG3 0x01
50#define LP5562_RUN_ENG1 0x20
51#define LP5562_RUN_ENG2 0x08
52#define LP5562_RUN_ENG3 0x02
53#define LP5562_ENG1_IS_LOADING(mode) \
54 ((mode & LP5562_MODE_ENG1_M) == LP5562_LOAD_ENG1)
55#define LP5562_ENG2_IS_LOADING(mode) \
56 ((mode & LP5562_MODE_ENG2_M) == LP5562_LOAD_ENG2)
57#define LP5562_ENG3_IS_LOADING(mode) \
58 ((mode & LP5562_MODE_ENG3_M) == LP5562_LOAD_ENG3)
59
60/* BRIGHTNESS Registers */
61#define LP5562_REG_R_PWM 0x04
62#define LP5562_REG_G_PWM 0x03
63#define LP5562_REG_B_PWM 0x02
64#define LP5562_REG_W_PWM 0x0E
65
66/* CURRENT Registers */
67#define LP5562_REG_R_CURRENT 0x07
68#define LP5562_REG_G_CURRENT 0x06
69#define LP5562_REG_B_CURRENT 0x05
70#define LP5562_REG_W_CURRENT 0x0F
71
72/* CONFIG Register 08h */
73#define LP5562_REG_CONFIG 0x08
74#define LP5562_DEFAULT_CFG \
75 (LP5562_PWM_HF | LP5562_PWRSAVE_EN | LP5562_CLK_INT)
76
77/* RESET Register 0Dh */
78#define LP5562_REG_RESET 0x0D
79#define LP5562_RESET 0xFF
80
81/* PROGRAM ENGINE Registers */
82#define LP5562_REG_PROG_MEM_ENG1 0x10
83#define LP5562_REG_PROG_MEM_ENG2 0x30
84#define LP5562_REG_PROG_MEM_ENG3 0x50
85
86/* LEDMAP Register 70h */
87#define LP5562_REG_ENG_SEL 0x70
88#define LP5562_ENG_SEL_PWM 0
89#define LP5562_ENG_FOR_RGB_M 0x3F
90#define LP5562_ENG_SEL_RGB 0x1B /* R:ENG1, G:ENG2, B:ENG3 */
91#define LP5562_ENG_FOR_W_M 0xC0
92#define LP5562_ENG1_FOR_W 0x40 /* W:ENG1 */
93#define LP5562_ENG2_FOR_W 0x80 /* W:ENG2 */
94#define LP5562_ENG3_FOR_W 0xC0 /* W:ENG3 */
95
96/* Program Commands */
97#define LP5562_CMD_DISABLE 0x00
98#define LP5562_CMD_LOAD 0x15
99#define LP5562_CMD_RUN 0x2A
100#define LP5562_CMD_DIRECT 0x3F
101#define LP5562_PATTERN_OFF 0
102
103static inline void lp5562_wait_opmode_done(void)
104{
105 /* operation mode change needs to be longer than 153 us */
106 usleep_range(200, 300);
107}
108
109static inline void lp5562_wait_enable_done(void)
110{
111 /* it takes more 488 us to update ENABLE register */
112 usleep_range(500, 600);
113}
114
115static void lp5562_set_led_current(struct lp55xx_led *led, u8 led_current)
116{
117 u8 addr[] = {
118 LP5562_REG_R_CURRENT,
119 LP5562_REG_G_CURRENT,
120 LP5562_REG_B_CURRENT,
121 LP5562_REG_W_CURRENT,
122 };
123
124 led->led_current = led_current;
125 lp55xx_write(led->chip, addr[led->chan_nr], led_current);
126}
127
128static void lp5562_load_engine(struct lp55xx_chip *chip)
129{
130 enum lp55xx_engine_index idx = chip->engine_idx;
131 u8 mask[] = {
132 [LP55XX_ENGINE_1] = LP5562_MODE_ENG1_M,
133 [LP55XX_ENGINE_2] = LP5562_MODE_ENG2_M,
134 [LP55XX_ENGINE_3] = LP5562_MODE_ENG3_M,
135 };
136
137 u8 val[] = {
138 [LP55XX_ENGINE_1] = LP5562_LOAD_ENG1,
139 [LP55XX_ENGINE_2] = LP5562_LOAD_ENG2,
140 [LP55XX_ENGINE_3] = LP5562_LOAD_ENG3,
141 };
142
143 lp55xx_update_bits(chip, LP5562_REG_OP_MODE, mask[idx], val[idx]);
144
145 lp5562_wait_opmode_done();
146}
147
148static void lp5562_stop_engine(struct lp55xx_chip *chip)
149{
150 lp55xx_write(chip, LP5562_REG_OP_MODE, LP5562_CMD_DISABLE);
151 lp5562_wait_opmode_done();
152}
153
154static void lp5562_run_engine(struct lp55xx_chip *chip, bool start)
155{
156 int ret;
157 u8 mode;
158 u8 exec;
159
160 /* stop engine */
161 if (!start) {
162 lp55xx_write(chip, LP5562_REG_ENABLE, LP5562_ENABLE_DEFAULT);
163 lp5562_wait_enable_done();
164 lp5562_stop_engine(chip);
165 lp55xx_write(chip, LP5562_REG_ENG_SEL, LP5562_ENG_SEL_PWM);
166 lp55xx_write(chip, LP5562_REG_OP_MODE, LP5562_CMD_DIRECT);
167 lp5562_wait_opmode_done();
168 return;
169 }
170
171 /*
172 * To run the engine,
173 * operation mode and enable register should updated at the same time
174 */
175
176 ret = lp55xx_read(chip, LP5562_REG_OP_MODE, &mode);
177 if (ret)
178 return;
179
180 ret = lp55xx_read(chip, LP5562_REG_ENABLE, &exec);
181 if (ret)
182 return;
183
184 /* change operation mode to RUN only when each engine is loading */
185 if (LP5562_ENG1_IS_LOADING(mode)) {
186 mode = (mode & ~LP5562_MODE_ENG1_M) | LP5562_RUN_ENG1;
187 exec = (exec & ~LP5562_EXEC_ENG1_M) | LP5562_RUN_ENG1;
188 }
189
190 if (LP5562_ENG2_IS_LOADING(mode)) {
191 mode = (mode & ~LP5562_MODE_ENG2_M) | LP5562_RUN_ENG2;
192 exec = (exec & ~LP5562_EXEC_ENG2_M) | LP5562_RUN_ENG2;
193 }
194
195 if (LP5562_ENG3_IS_LOADING(mode)) {
196 mode = (mode & ~LP5562_MODE_ENG3_M) | LP5562_RUN_ENG3;
197 exec = (exec & ~LP5562_EXEC_ENG3_M) | LP5562_RUN_ENG3;
198 }
199
200 lp55xx_write(chip, LP5562_REG_OP_MODE, mode);
201 lp5562_wait_opmode_done();
202
203 lp55xx_update_bits(chip, LP5562_REG_ENABLE, LP5562_EXEC_M, exec);
204 lp5562_wait_enable_done();
205}
206
207static int lp5562_update_firmware(struct lp55xx_chip *chip,
208 const u8 *data, size_t size)
209{
210 enum lp55xx_engine_index idx = chip->engine_idx;
211 u8 pattern[LP5562_PROGRAM_LENGTH] = {0};
212 u8 addr[] = {
213 [LP55XX_ENGINE_1] = LP5562_REG_PROG_MEM_ENG1,
214 [LP55XX_ENGINE_2] = LP5562_REG_PROG_MEM_ENG2,
215 [LP55XX_ENGINE_3] = LP5562_REG_PROG_MEM_ENG3,
216 };
217 unsigned cmd;
218 char c[3];
219 int program_size;
220 int nrchars;
221 int offset = 0;
222 int ret;
223 int i;
224
225 /* clear program memory before updating */
226 for (i = 0; i < LP5562_PROGRAM_LENGTH; i++)
227 lp55xx_write(chip, addr[idx] + i, 0);
228
229 i = 0;
230 while ((offset < size - 1) && (i < LP5562_PROGRAM_LENGTH)) {
231 /* separate sscanfs because length is working only for %s */
232 ret = sscanf(data + offset, "%2s%n ", c, &nrchars);
233 if (ret != 1)
234 goto err;
235
236 ret = sscanf(c, "%2x", &cmd);
237 if (ret != 1)
238 goto err;
239
240 pattern[i] = (u8)cmd;
241 offset += nrchars;
242 i++;
243 }
244
245 /* Each instruction is 16bit long. Check that length is even */
246 if (i % 2)
247 goto err;
248
249 program_size = i;
250 for (i = 0; i < program_size; i++)
251 lp55xx_write(chip, addr[idx] + i, pattern[i]);
252
253 return 0;
254
255err:
256 dev_err(&chip->cl->dev, "wrong pattern format\n");
257 return -EINVAL;
258}
259
260static void lp5562_firmware_loaded(struct lp55xx_chip *chip)
261{
262 const struct firmware *fw = chip->fw;
263
264 if (fw->size > LP5562_PROGRAM_LENGTH) {
265 dev_err(&chip->cl->dev, "firmware data size overflow: %zu\n",
266 fw->size);
267 return;
268 }
269
270 /*
271 * Program momery sequence
272 * 1) set engine mode to "LOAD"
273 * 2) write firmware data into program memory
274 */
275
276 lp5562_load_engine(chip);
277 lp5562_update_firmware(chip, fw->data, fw->size);
278}
279
280static int lp5562_post_init_device(struct lp55xx_chip *chip)
281{
282 int ret;
283 u8 update_cfg = chip->pdata->update_config ? : LP5562_DEFAULT_CFG;
284
285 /* Set all PWMs to direct control mode */
286 ret = lp55xx_write(chip, LP5562_REG_OP_MODE, LP5562_CMD_DIRECT);
287 if (ret)
288 return ret;
289
290 lp5562_wait_opmode_done();
291
292 ret = lp55xx_write(chip, LP5562_REG_CONFIG, update_cfg);
293 if (ret)
294 return ret;
295
296 /* Initialize all channels PWM to zero -> leds off */
297 lp55xx_write(chip, LP5562_REG_R_PWM, 0);
298 lp55xx_write(chip, LP5562_REG_G_PWM, 0);
299 lp55xx_write(chip, LP5562_REG_B_PWM, 0);
300 lp55xx_write(chip, LP5562_REG_W_PWM, 0);
301
302 /* Set LED map as register PWM by default */
303 lp55xx_write(chip, LP5562_REG_ENG_SEL, LP5562_ENG_SEL_PWM);
304
305 return 0;
306}
307
308static void lp5562_led_brightness_work(struct work_struct *work)
309{
310 struct lp55xx_led *led = container_of(work, struct lp55xx_led,
311 brightness_work);
312 struct lp55xx_chip *chip = led->chip;
313 u8 addr[] = {
314 LP5562_REG_R_PWM,
315 LP5562_REG_G_PWM,
316 LP5562_REG_B_PWM,
317 LP5562_REG_W_PWM,
318 };
319
320 mutex_lock(&chip->lock);
321 lp55xx_write(chip, addr[led->chan_nr], led->brightness);
322 mutex_unlock(&chip->lock);
323}
324
325static void lp5562_write_program_memory(struct lp55xx_chip *chip,
326 u8 base, const u8 *rgb, int size)
327{
328 int i;
329
330 if (!rgb || size <= 0)
331 return;
332
333 for (i = 0; i < size; i++)
334 lp55xx_write(chip, base + i, *(rgb + i));
335
336 lp55xx_write(chip, base + i, 0);
337 lp55xx_write(chip, base + i + 1, 0);
338}
339
340/* check the size of program count */
341static inline bool _is_pc_overflow(struct lp55xx_predef_pattern *ptn)
342{
343 return (ptn->size_r >= LP5562_PROGRAM_LENGTH ||
344 ptn->size_g >= LP5562_PROGRAM_LENGTH ||
345 ptn->size_b >= LP5562_PROGRAM_LENGTH);
346}
347
348static int lp5562_run_predef_led_pattern(struct lp55xx_chip *chip, int mode)
349{
350 struct lp55xx_predef_pattern *ptn;
351 int i;
352
353 if (mode == LP5562_PATTERN_OFF) {
354 lp5562_run_engine(chip, false);
355 return 0;
356 }
357
358 ptn = chip->pdata->patterns + (mode - 1);
359 if (!ptn || _is_pc_overflow(ptn)) {
360 dev_err(&chip->cl->dev, "invalid pattern data\n");
361 return -EINVAL;
362 }
363
364 lp5562_stop_engine(chip);
365
366 /* Set LED map as RGB */
367 lp55xx_write(chip, LP5562_REG_ENG_SEL, LP5562_ENG_SEL_RGB);
368
369 /* Load engines */
370 for (i = LP55XX_ENGINE_1; i <= LP55XX_ENGINE_3; i++) {
371 chip->engine_idx = i;
372 lp5562_load_engine(chip);
373 }
374
375 /* Clear program registers */
376 lp55xx_write(chip, LP5562_REG_PROG_MEM_ENG1, 0);
377 lp55xx_write(chip, LP5562_REG_PROG_MEM_ENG1 + 1, 0);
378 lp55xx_write(chip, LP5562_REG_PROG_MEM_ENG2, 0);
379 lp55xx_write(chip, LP5562_REG_PROG_MEM_ENG2 + 1, 0);
380 lp55xx_write(chip, LP5562_REG_PROG_MEM_ENG3, 0);
381 lp55xx_write(chip, LP5562_REG_PROG_MEM_ENG3 + 1, 0);
382
383 /* Program engines */
384 lp5562_write_program_memory(chip, LP5562_REG_PROG_MEM_ENG1,
385 ptn->r, ptn->size_r);
386 lp5562_write_program_memory(chip, LP5562_REG_PROG_MEM_ENG2,
387 ptn->g, ptn->size_g);
388 lp5562_write_program_memory(chip, LP5562_REG_PROG_MEM_ENG3,
389 ptn->b, ptn->size_b);
390
391 /* Run engines */
392 lp5562_run_engine(chip, true);
393
394 return 0;
395}
396
397static ssize_t lp5562_store_pattern(struct device *dev,
398 struct device_attribute *attr,
399 const char *buf, size_t len)
400{
401 struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
402 struct lp55xx_chip *chip = led->chip;
403 struct lp55xx_predef_pattern *ptn = chip->pdata->patterns;
404 int num_patterns = chip->pdata->num_patterns;
405 unsigned long mode;
406 int ret;
407
408 ret = kstrtoul(buf, 0, &mode);
409 if (ret)
410 return ret;
411
412 if (mode > num_patterns || !ptn)
413 return -EINVAL;
414
415 mutex_lock(&chip->lock);
416 ret = lp5562_run_predef_led_pattern(chip, mode);
417 mutex_unlock(&chip->lock);
418
419 if (ret)
420 return ret;
421
422 return len;
423}
424
425static ssize_t lp5562_store_engine_mux(struct device *dev,
426 struct device_attribute *attr,
427 const char *buf, size_t len)
428{
429 struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
430 struct lp55xx_chip *chip = led->chip;
431 u8 mask;
432 u8 val;
433
434 /* LED map
435 * R ... Engine 1 (fixed)
436 * G ... Engine 2 (fixed)
437 * B ... Engine 3 (fixed)
438 * W ... Engine 1 or 2 or 3
439 */
440
441 if (sysfs_streq(buf, "RGB")) {
442 mask = LP5562_ENG_FOR_RGB_M;
443 val = LP5562_ENG_SEL_RGB;
444 } else if (sysfs_streq(buf, "W")) {
445 enum lp55xx_engine_index idx = chip->engine_idx;
446
447 mask = LP5562_ENG_FOR_W_M;
448 switch (idx) {
449 case LP55XX_ENGINE_1:
450 val = LP5562_ENG1_FOR_W;
451 break;
452 case LP55XX_ENGINE_2:
453 val = LP5562_ENG2_FOR_W;
454 break;
455 case LP55XX_ENGINE_3:
456 val = LP5562_ENG3_FOR_W;
457 break;
458 default:
459 return -EINVAL;
460 }
461
462 } else {
463 dev_err(dev, "choose RGB or W\n");
464 return -EINVAL;
465 }
466
467 mutex_lock(&chip->lock);
468 lp55xx_update_bits(chip, LP5562_REG_ENG_SEL, mask, val);
469 mutex_unlock(&chip->lock);
470
471 return len;
472}
473
474static DEVICE_ATTR(led_pattern, S_IWUSR, NULL, lp5562_store_pattern);
475static DEVICE_ATTR(engine_mux, S_IWUSR, NULL, lp5562_store_engine_mux);
476
477static struct attribute *lp5562_attributes[] = {
478 &dev_attr_led_pattern.attr,
479 &dev_attr_engine_mux.attr,
480 NULL,
481};
482
483static const struct attribute_group lp5562_group = {
484 .attrs = lp5562_attributes,
485};
486
487/* Chip specific configurations */
488static struct lp55xx_device_config lp5562_cfg = {
489 .max_channel = LP5562_MAX_LEDS,
490 .reset = {
491 .addr = LP5562_REG_RESET,
492 .val = LP5562_RESET,
493 },
494 .enable = {
495 .addr = LP5562_REG_ENABLE,
496 .val = LP5562_ENABLE_DEFAULT,
497 },
498 .post_init_device = lp5562_post_init_device,
499 .set_led_current = lp5562_set_led_current,
500 .brightness_work_fn = lp5562_led_brightness_work,
501 .run_engine = lp5562_run_engine,
502 .firmware_cb = lp5562_firmware_loaded,
503 .dev_attr_group = &lp5562_group,
504};
505
506static int lp5562_probe(struct i2c_client *client,
507 const struct i2c_device_id *id)
508{
509 int ret;
510 struct lp55xx_chip *chip;
511 struct lp55xx_led *led;
512 struct lp55xx_platform_data *pdata = client->dev.platform_data;
513
514 if (!pdata) {
515 dev_err(&client->dev, "no platform data\n");
516 return -EINVAL;
517 }
518
519 chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
520 if (!chip)
521 return -ENOMEM;
522
523 led = devm_kzalloc(&client->dev,
524 sizeof(*led) * pdata->num_channels, GFP_KERNEL);
525 if (!led)
526 return -ENOMEM;
527
528 chip->cl = client;
529 chip->pdata = pdata;
530 chip->cfg = &lp5562_cfg;
531
532 mutex_init(&chip->lock);
533
534 i2c_set_clientdata(client, led);
535
536 ret = lp55xx_init_device(chip);
537 if (ret)
538 goto err_init;
539
540 ret = lp55xx_register_leds(led, chip);
541 if (ret)
542 goto err_register_leds;
543
544 ret = lp55xx_register_sysfs(chip);
545 if (ret) {
546 dev_err(&client->dev, "registering sysfs failed\n");
547 goto err_register_sysfs;
548 }
549
550 return 0;
551
552err_register_sysfs:
553 lp55xx_unregister_leds(led, chip);
554err_register_leds:
555 lp55xx_deinit_device(chip);
556err_init:
557 return ret;
558}
559
560static int lp5562_remove(struct i2c_client *client)
561{
562 struct lp55xx_led *led = i2c_get_clientdata(client);
563 struct lp55xx_chip *chip = led->chip;
564
565 lp5562_stop_engine(chip);
566
567 lp55xx_unregister_sysfs(chip);
568 lp55xx_unregister_leds(led, chip);
569 lp55xx_deinit_device(chip);
570
571 return 0;
572}
573
574static const struct i2c_device_id lp5562_id[] = {
575 { "lp5562", 0 },
576 { }
577};
578MODULE_DEVICE_TABLE(i2c, lp5562_id);
579
580static struct i2c_driver lp5562_driver = {
581 .driver = {
582 .name = "lp5562",
583 },
584 .probe = lp5562_probe,
585 .remove = lp5562_remove,
586 .id_table = lp5562_id,
587};
588
589module_i2c_driver(lp5562_driver);
590
591MODULE_DESCRIPTION("Texas Instruments LP5562 LED Driver");
592MODULE_AUTHOR("Milo Kim");
593MODULE_LICENSE("GPL");
This page took 0.044978 seconds and 5 git commands to generate.