1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright (C) 2013 Texas Instruments
7 * Author: Milo(Woogyom) Kim <milo.kim@ti.com>
10 #include <linux/delay.h>
11 #include <linux/firmware.h>
12 #include <linux/i2c.h>
13 #include <linux/leds.h>
14 #include <linux/module.h>
15 #include <linux/mutex.h>
17 #include <linux/platform_data/leds-lp55xx.h>
18 #include <linux/slab.h>
20 #include "leds-lp55xx-common.h"
22 #define LP5562_PROGRAM_LENGTH 32
23 #define LP5562_MAX_LEDS 4
25 /* ENABLE Register 00h */
26 #define LP5562_REG_ENABLE 0x00
27 #define LP5562_EXEC_ENG1_M 0x30
28 #define LP5562_EXEC_ENG2_M 0x0C
29 #define LP5562_EXEC_ENG3_M 0x03
30 #define LP5562_EXEC_M 0x3F
31 #define LP5562_MASTER_ENABLE 0x40 /* Chip master enable */
32 #define LP5562_LOGARITHMIC_PWM 0x80 /* Logarithmic PWM adjustment */
33 #define LP5562_EXEC_RUN 0x2A
34 #define LP5562_ENABLE_DEFAULT \
35 (LP5562_MASTER_ENABLE | LP5562_LOGARITHMIC_PWM)
36 #define LP5562_ENABLE_RUN_PROGRAM \
37 (LP5562_ENABLE_DEFAULT | LP5562_EXEC_RUN)
39 /* OPMODE Register 01h */
40 #define LP5562_REG_OP_MODE 0x01
41 #define LP5562_MODE_ENG1_M 0x30
42 #define LP5562_MODE_ENG2_M 0x0C
43 #define LP5562_MODE_ENG3_M 0x03
44 #define LP5562_LOAD_ENG1 0x10
45 #define LP5562_LOAD_ENG2 0x04
46 #define LP5562_LOAD_ENG3 0x01
47 #define LP5562_RUN_ENG1 0x20
48 #define LP5562_RUN_ENG2 0x08
49 #define LP5562_RUN_ENG3 0x02
50 #define LP5562_ENG1_IS_LOADING(mode) \
51 ((mode & LP5562_MODE_ENG1_M) == LP5562_LOAD_ENG1)
52 #define LP5562_ENG2_IS_LOADING(mode) \
53 ((mode & LP5562_MODE_ENG2_M) == LP5562_LOAD_ENG2)
54 #define LP5562_ENG3_IS_LOADING(mode) \
55 ((mode & LP5562_MODE_ENG3_M) == LP5562_LOAD_ENG3)
57 /* BRIGHTNESS Registers */
58 #define LP5562_REG_R_PWM 0x04
59 #define LP5562_REG_G_PWM 0x03
60 #define LP5562_REG_B_PWM 0x02
61 #define LP5562_REG_W_PWM 0x0E
63 /* CURRENT Registers */
64 #define LP5562_REG_R_CURRENT 0x07
65 #define LP5562_REG_G_CURRENT 0x06
66 #define LP5562_REG_B_CURRENT 0x05
67 #define LP5562_REG_W_CURRENT 0x0F
69 /* CONFIG Register 08h */
70 #define LP5562_REG_CONFIG 0x08
71 #define LP5562_PWM_HF 0x40
72 #define LP5562_PWRSAVE_EN 0x20
73 #define LP5562_CLK_INT 0x01 /* Internal clock */
74 #define LP5562_DEFAULT_CFG (LP5562_PWM_HF | LP5562_PWRSAVE_EN)
76 /* RESET Register 0Dh */
77 #define LP5562_REG_RESET 0x0D
78 #define LP5562_RESET 0xFF
80 /* PROGRAM ENGINE Registers */
81 #define LP5562_REG_PROG_MEM_ENG1 0x10
82 #define LP5562_REG_PROG_MEM_ENG2 0x30
83 #define LP5562_REG_PROG_MEM_ENG3 0x50
85 /* LEDMAP Register 70h */
86 #define LP5562_REG_ENG_SEL 0x70
87 #define LP5562_ENG_SEL_PWM 0
88 #define LP5562_ENG_FOR_RGB_M 0x3F
89 #define LP5562_ENG_SEL_RGB 0x1B /* R:ENG1, G:ENG2, B:ENG3 */
90 #define LP5562_ENG_FOR_W_M 0xC0
91 #define LP5562_ENG1_FOR_W 0x40 /* W:ENG1 */
92 #define LP5562_ENG2_FOR_W 0x80 /* W:ENG2 */
93 #define LP5562_ENG3_FOR_W 0xC0 /* W:ENG3 */
95 /* Program Commands */
96 #define LP5562_CMD_DISABLE 0x00
97 #define LP5562_CMD_LOAD 0x15
98 #define LP5562_CMD_RUN 0x2A
99 #define LP5562_CMD_DIRECT 0x3F
100 #define LP5562_PATTERN_OFF 0
102 static inline void lp5562_wait_opmode_done(void)
104 /* operation mode change needs to be longer than 153 us */
105 usleep_range(200, 300);
108 static inline void lp5562_wait_enable_done(void)
110 /* it takes more 488 us to update ENABLE register */
111 usleep_range(500, 600);
114 static void lp5562_set_led_current(struct lp55xx_led *led, u8 led_current)
116 static const u8 addr[] = {
117 LP5562_REG_R_CURRENT,
118 LP5562_REG_G_CURRENT,
119 LP5562_REG_B_CURRENT,
120 LP5562_REG_W_CURRENT,
123 led->led_current = led_current;
124 lp55xx_write(led->chip, addr[led->chan_nr], led_current);
127 static void lp5562_load_engine(struct lp55xx_chip *chip)
129 enum lp55xx_engine_index idx = chip->engine_idx;
130 static const u8 mask[] = {
131 [LP55XX_ENGINE_1] = LP5562_MODE_ENG1_M,
132 [LP55XX_ENGINE_2] = LP5562_MODE_ENG2_M,
133 [LP55XX_ENGINE_3] = LP5562_MODE_ENG3_M,
136 static const u8 val[] = {
137 [LP55XX_ENGINE_1] = LP5562_LOAD_ENG1,
138 [LP55XX_ENGINE_2] = LP5562_LOAD_ENG2,
139 [LP55XX_ENGINE_3] = LP5562_LOAD_ENG3,
142 lp55xx_update_bits(chip, LP5562_REG_OP_MODE, mask[idx], val[idx]);
144 lp5562_wait_opmode_done();
147 static void lp5562_stop_engine(struct lp55xx_chip *chip)
149 lp55xx_write(chip, LP5562_REG_OP_MODE, LP5562_CMD_DISABLE);
150 lp5562_wait_opmode_done();
153 static void lp5562_run_engine(struct lp55xx_chip *chip, bool start)
161 lp55xx_write(chip, LP5562_REG_ENABLE, LP5562_ENABLE_DEFAULT);
162 lp5562_wait_enable_done();
163 lp5562_stop_engine(chip);
164 lp55xx_write(chip, LP5562_REG_ENG_SEL, LP5562_ENG_SEL_PWM);
165 lp55xx_write(chip, LP5562_REG_OP_MODE, LP5562_CMD_DIRECT);
166 lp5562_wait_opmode_done();
172 * operation mode and enable register should updated at the same time
175 ret = lp55xx_read(chip, LP5562_REG_OP_MODE, &mode);
179 ret = lp55xx_read(chip, LP5562_REG_ENABLE, &exec);
183 /* change operation mode to RUN only when each engine is loading */
184 if (LP5562_ENG1_IS_LOADING(mode)) {
185 mode = (mode & ~LP5562_MODE_ENG1_M) | LP5562_RUN_ENG1;
186 exec = (exec & ~LP5562_EXEC_ENG1_M) | LP5562_RUN_ENG1;
189 if (LP5562_ENG2_IS_LOADING(mode)) {
190 mode = (mode & ~LP5562_MODE_ENG2_M) | LP5562_RUN_ENG2;
191 exec = (exec & ~LP5562_EXEC_ENG2_M) | LP5562_RUN_ENG2;
194 if (LP5562_ENG3_IS_LOADING(mode)) {
195 mode = (mode & ~LP5562_MODE_ENG3_M) | LP5562_RUN_ENG3;
196 exec = (exec & ~LP5562_EXEC_ENG3_M) | LP5562_RUN_ENG3;
199 lp55xx_write(chip, LP5562_REG_OP_MODE, mode);
200 lp5562_wait_opmode_done();
202 lp55xx_update_bits(chip, LP5562_REG_ENABLE, LP5562_EXEC_M, exec);
203 lp5562_wait_enable_done();
206 static int lp5562_update_firmware(struct lp55xx_chip *chip,
207 const u8 *data, size_t size)
209 enum lp55xx_engine_index idx = chip->engine_idx;
210 u8 pattern[LP5562_PROGRAM_LENGTH] = {0};
211 static const u8 addr[] = {
212 [LP55XX_ENGINE_1] = LP5562_REG_PROG_MEM_ENG1,
213 [LP55XX_ENGINE_2] = LP5562_REG_PROG_MEM_ENG2,
214 [LP55XX_ENGINE_3] = LP5562_REG_PROG_MEM_ENG3,
224 /* clear program memory before updating */
225 for (i = 0; i < LP5562_PROGRAM_LENGTH; i++)
226 lp55xx_write(chip, addr[idx] + i, 0);
229 while ((offset < size - 1) && (i < LP5562_PROGRAM_LENGTH)) {
230 /* separate sscanfs because length is working only for %s */
231 ret = sscanf(data + offset, "%2s%n ", c, &nrchars);
235 ret = sscanf(c, "%2x", &cmd);
239 pattern[i] = (u8)cmd;
244 /* Each instruction is 16bit long. Check that length is even */
249 for (i = 0; i < program_size; i++)
250 lp55xx_write(chip, addr[idx] + i, pattern[i]);
255 dev_err(&chip->cl->dev, "wrong pattern format\n");
259 static void lp5562_firmware_loaded(struct lp55xx_chip *chip)
261 const struct firmware *fw = chip->fw;
264 * the firmware is encoded in ascii hex character, with 2 chars
267 if (fw->size > (LP5562_PROGRAM_LENGTH * 2)) {
268 dev_err(&chip->cl->dev, "firmware data size overflow: %zu\n",
274 * Program memory sequence
275 * 1) set engine mode to "LOAD"
276 * 2) write firmware data into program memory
279 lp5562_load_engine(chip);
280 lp5562_update_firmware(chip, fw->data, fw->size);
283 static int lp5562_post_init_device(struct lp55xx_chip *chip)
286 u8 cfg = LP5562_DEFAULT_CFG;
288 /* Set all PWMs to direct control mode */
289 ret = lp55xx_write(chip, LP5562_REG_OP_MODE, LP5562_CMD_DIRECT);
293 lp5562_wait_opmode_done();
295 /* Update configuration for the clock setting */
296 if (!lp55xx_is_extclk_used(chip))
297 cfg |= LP5562_CLK_INT;
299 ret = lp55xx_write(chip, LP5562_REG_CONFIG, cfg);
303 /* Initialize all channels PWM to zero -> leds off */
304 lp55xx_write(chip, LP5562_REG_R_PWM, 0);
305 lp55xx_write(chip, LP5562_REG_G_PWM, 0);
306 lp55xx_write(chip, LP5562_REG_B_PWM, 0);
307 lp55xx_write(chip, LP5562_REG_W_PWM, 0);
309 /* Set LED map as register PWM by default */
310 lp55xx_write(chip, LP5562_REG_ENG_SEL, LP5562_ENG_SEL_PWM);
315 static int lp5562_led_brightness(struct lp55xx_led *led)
317 struct lp55xx_chip *chip = led->chip;
318 static const u8 addr[] = {
326 mutex_lock(&chip->lock);
327 ret = lp55xx_write(chip, addr[led->chan_nr], led->brightness);
328 mutex_unlock(&chip->lock);
333 static void lp5562_write_program_memory(struct lp55xx_chip *chip,
334 u8 base, const u8 *rgb, int size)
338 if (!rgb || size <= 0)
341 for (i = 0; i < size; i++)
342 lp55xx_write(chip, base + i, *(rgb + i));
344 lp55xx_write(chip, base + i, 0);
345 lp55xx_write(chip, base + i + 1, 0);
348 /* check the size of program count */
349 static inline bool _is_pc_overflow(struct lp55xx_predef_pattern *ptn)
351 return ptn->size_r >= LP5562_PROGRAM_LENGTH ||
352 ptn->size_g >= LP5562_PROGRAM_LENGTH ||
353 ptn->size_b >= LP5562_PROGRAM_LENGTH;
356 static int lp5562_run_predef_led_pattern(struct lp55xx_chip *chip, int mode)
358 struct lp55xx_predef_pattern *ptn;
361 if (mode == LP5562_PATTERN_OFF) {
362 lp5562_run_engine(chip, false);
366 ptn = chip->pdata->patterns + (mode - 1);
367 if (!ptn || _is_pc_overflow(ptn)) {
368 dev_err(&chip->cl->dev, "invalid pattern data\n");
372 lp5562_stop_engine(chip);
374 /* Set LED map as RGB */
375 lp55xx_write(chip, LP5562_REG_ENG_SEL, LP5562_ENG_SEL_RGB);
378 for (i = LP55XX_ENGINE_1; i <= LP55XX_ENGINE_3; i++) {
379 chip->engine_idx = i;
380 lp5562_load_engine(chip);
383 /* Clear program registers */
384 lp55xx_write(chip, LP5562_REG_PROG_MEM_ENG1, 0);
385 lp55xx_write(chip, LP5562_REG_PROG_MEM_ENG1 + 1, 0);
386 lp55xx_write(chip, LP5562_REG_PROG_MEM_ENG2, 0);
387 lp55xx_write(chip, LP5562_REG_PROG_MEM_ENG2 + 1, 0);
388 lp55xx_write(chip, LP5562_REG_PROG_MEM_ENG3, 0);
389 lp55xx_write(chip, LP5562_REG_PROG_MEM_ENG3 + 1, 0);
391 /* Program engines */
392 lp5562_write_program_memory(chip, LP5562_REG_PROG_MEM_ENG1,
393 ptn->r, ptn->size_r);
394 lp5562_write_program_memory(chip, LP5562_REG_PROG_MEM_ENG2,
395 ptn->g, ptn->size_g);
396 lp5562_write_program_memory(chip, LP5562_REG_PROG_MEM_ENG3,
397 ptn->b, ptn->size_b);
400 lp5562_run_engine(chip, true);
405 static ssize_t lp5562_store_pattern(struct device *dev,
406 struct device_attribute *attr,
407 const char *buf, size_t len)
409 struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
410 struct lp55xx_chip *chip = led->chip;
411 struct lp55xx_predef_pattern *ptn = chip->pdata->patterns;
412 int num_patterns = chip->pdata->num_patterns;
416 ret = kstrtoul(buf, 0, &mode);
420 if (mode > num_patterns || !ptn)
423 mutex_lock(&chip->lock);
424 ret = lp5562_run_predef_led_pattern(chip, mode);
425 mutex_unlock(&chip->lock);
433 static ssize_t lp5562_store_engine_mux(struct device *dev,
434 struct device_attribute *attr,
435 const char *buf, size_t len)
437 struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
438 struct lp55xx_chip *chip = led->chip;
443 * R ... Engine 1 (fixed)
444 * G ... Engine 2 (fixed)
445 * B ... Engine 3 (fixed)
446 * W ... Engine 1 or 2 or 3
449 if (sysfs_streq(buf, "RGB")) {
450 mask = LP5562_ENG_FOR_RGB_M;
451 val = LP5562_ENG_SEL_RGB;
452 } else if (sysfs_streq(buf, "W")) {
453 enum lp55xx_engine_index idx = chip->engine_idx;
455 mask = LP5562_ENG_FOR_W_M;
457 case LP55XX_ENGINE_1:
458 val = LP5562_ENG1_FOR_W;
460 case LP55XX_ENGINE_2:
461 val = LP5562_ENG2_FOR_W;
463 case LP55XX_ENGINE_3:
464 val = LP5562_ENG3_FOR_W;
471 dev_err(dev, "choose RGB or W\n");
475 mutex_lock(&chip->lock);
476 lp55xx_update_bits(chip, LP5562_REG_ENG_SEL, mask, val);
477 mutex_unlock(&chip->lock);
482 static LP55XX_DEV_ATTR_WO(led_pattern, lp5562_store_pattern);
483 static LP55XX_DEV_ATTR_WO(engine_mux, lp5562_store_engine_mux);
485 static struct attribute *lp5562_attributes[] = {
486 &dev_attr_led_pattern.attr,
487 &dev_attr_engine_mux.attr,
491 static const struct attribute_group lp5562_group = {
492 .attrs = lp5562_attributes,
495 /* Chip specific configurations */
496 static struct lp55xx_device_config lp5562_cfg = {
497 .max_channel = LP5562_MAX_LEDS,
499 .addr = LP5562_REG_RESET,
503 .addr = LP5562_REG_ENABLE,
504 .val = LP5562_ENABLE_DEFAULT,
506 .post_init_device = lp5562_post_init_device,
507 .set_led_current = lp5562_set_led_current,
508 .brightness_fn = lp5562_led_brightness,
509 .run_engine = lp5562_run_engine,
510 .firmware_cb = lp5562_firmware_loaded,
511 .dev_attr_group = &lp5562_group,
514 static int lp5562_probe(struct i2c_client *client,
515 const struct i2c_device_id *id)
518 struct lp55xx_chip *chip;
519 struct lp55xx_led *led;
520 struct lp55xx_platform_data *pdata = dev_get_platdata(&client->dev);
521 struct device_node *np = dev_of_node(&client->dev);
523 chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
527 chip->cfg = &lp5562_cfg;
531 pdata = lp55xx_of_populate_pdata(&client->dev, np,
534 return PTR_ERR(pdata);
536 dev_err(&client->dev, "no platform data\n");
542 led = devm_kcalloc(&client->dev,
543 pdata->num_channels, sizeof(*led), GFP_KERNEL);
550 mutex_init(&chip->lock);
552 i2c_set_clientdata(client, led);
554 ret = lp55xx_init_device(chip);
558 ret = lp55xx_register_leds(led, chip);
562 ret = lp55xx_register_sysfs(chip);
564 dev_err(&client->dev, "registering sysfs failed\n");
571 lp55xx_deinit_device(chip);
576 static void lp5562_remove(struct i2c_client *client)
578 struct lp55xx_led *led = i2c_get_clientdata(client);
579 struct lp55xx_chip *chip = led->chip;
581 lp5562_stop_engine(chip);
583 lp55xx_unregister_sysfs(chip);
584 lp55xx_deinit_device(chip);
587 static const struct i2c_device_id lp5562_id[] = {
591 MODULE_DEVICE_TABLE(i2c, lp5562_id);
594 static const struct of_device_id of_lp5562_leds_match[] = {
595 { .compatible = "ti,lp5562", },
599 MODULE_DEVICE_TABLE(of, of_lp5562_leds_match);
602 static struct i2c_driver lp5562_driver = {
605 .of_match_table = of_match_ptr(of_lp5562_leds_match),
607 .probe = lp5562_probe,
608 .remove = lp5562_remove,
609 .id_table = lp5562_id,
612 module_i2c_driver(lp5562_driver);
614 MODULE_DESCRIPTION("Texas Instruments LP5562 LED Driver");
615 MODULE_AUTHOR("Milo Kim");
616 MODULE_LICENSE("GPL");