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
;
263 if (fw
->size
> LP5562_PROGRAM_LENGTH
) {
264 dev_err(&chip
->cl
->dev
, "firmware data size overflow: %zu\n",
270 * Program memory sequence
271 * 1) set engine mode to "LOAD"
272 * 2) write firmware data into program memory
275 lp5562_load_engine(chip
);
276 lp5562_update_firmware(chip
, fw
->data
, fw
->size
);
279 static int lp5562_post_init_device(struct lp55xx_chip
*chip
)
282 u8 cfg
= LP5562_DEFAULT_CFG
;
284 /* Set all PWMs to direct control mode */
285 ret
= lp55xx_write(chip
, LP5562_REG_OP_MODE
, LP5562_CMD_DIRECT
);
289 lp5562_wait_opmode_done();
291 /* Update configuration for the clock setting */
292 if (!lp55xx_is_extclk_used(chip
))
293 cfg
|= LP5562_CLK_INT
;
295 ret
= lp55xx_write(chip
, LP5562_REG_CONFIG
, cfg
);
299 /* Initialize all channels PWM to zero -> leds off */
300 lp55xx_write(chip
, LP5562_REG_R_PWM
, 0);
301 lp55xx_write(chip
, LP5562_REG_G_PWM
, 0);
302 lp55xx_write(chip
, LP5562_REG_B_PWM
, 0);
303 lp55xx_write(chip
, LP5562_REG_W_PWM
, 0);
305 /* Set LED map as register PWM by default */
306 lp55xx_write(chip
, LP5562_REG_ENG_SEL
, LP5562_ENG_SEL_PWM
);
311 static int lp5562_led_brightness(struct lp55xx_led
*led
)
313 struct lp55xx_chip
*chip
= led
->chip
;
314 static const u8 addr
[] = {
322 mutex_lock(&chip
->lock
);
323 ret
= lp55xx_write(chip
, addr
[led
->chan_nr
], led
->brightness
);
324 mutex_unlock(&chip
->lock
);
329 static void lp5562_write_program_memory(struct lp55xx_chip
*chip
,
330 u8 base
, const u8
*rgb
, int size
)
334 if (!rgb
|| size
<= 0)
337 for (i
= 0; i
< size
; i
++)
338 lp55xx_write(chip
, base
+ i
, *(rgb
+ i
));
340 lp55xx_write(chip
, base
+ i
, 0);
341 lp55xx_write(chip
, base
+ i
+ 1, 0);
344 /* check the size of program count */
345 static inline bool _is_pc_overflow(struct lp55xx_predef_pattern
*ptn
)
347 return ptn
->size_r
>= LP5562_PROGRAM_LENGTH
||
348 ptn
->size_g
>= LP5562_PROGRAM_LENGTH
||
349 ptn
->size_b
>= LP5562_PROGRAM_LENGTH
;
352 static int lp5562_run_predef_led_pattern(struct lp55xx_chip
*chip
, int mode
)
354 struct lp55xx_predef_pattern
*ptn
;
357 if (mode
== LP5562_PATTERN_OFF
) {
358 lp5562_run_engine(chip
, false);
362 ptn
= chip
->pdata
->patterns
+ (mode
- 1);
363 if (!ptn
|| _is_pc_overflow(ptn
)) {
364 dev_err(&chip
->cl
->dev
, "invalid pattern data\n");
368 lp5562_stop_engine(chip
);
370 /* Set LED map as RGB */
371 lp55xx_write(chip
, LP5562_REG_ENG_SEL
, LP5562_ENG_SEL_RGB
);
374 for (i
= LP55XX_ENGINE_1
; i
<= LP55XX_ENGINE_3
; i
++) {
375 chip
->engine_idx
= i
;
376 lp5562_load_engine(chip
);
379 /* Clear program registers */
380 lp55xx_write(chip
, LP5562_REG_PROG_MEM_ENG1
, 0);
381 lp55xx_write(chip
, LP5562_REG_PROG_MEM_ENG1
+ 1, 0);
382 lp55xx_write(chip
, LP5562_REG_PROG_MEM_ENG2
, 0);
383 lp55xx_write(chip
, LP5562_REG_PROG_MEM_ENG2
+ 1, 0);
384 lp55xx_write(chip
, LP5562_REG_PROG_MEM_ENG3
, 0);
385 lp55xx_write(chip
, LP5562_REG_PROG_MEM_ENG3
+ 1, 0);
387 /* Program engines */
388 lp5562_write_program_memory(chip
, LP5562_REG_PROG_MEM_ENG1
,
389 ptn
->r
, ptn
->size_r
);
390 lp5562_write_program_memory(chip
, LP5562_REG_PROG_MEM_ENG2
,
391 ptn
->g
, ptn
->size_g
);
392 lp5562_write_program_memory(chip
, LP5562_REG_PROG_MEM_ENG3
,
393 ptn
->b
, ptn
->size_b
);
396 lp5562_run_engine(chip
, true);
401 static ssize_t
lp5562_store_pattern(struct device
*dev
,
402 struct device_attribute
*attr
,
403 const char *buf
, size_t len
)
405 struct lp55xx_led
*led
= i2c_get_clientdata(to_i2c_client(dev
));
406 struct lp55xx_chip
*chip
= led
->chip
;
407 struct lp55xx_predef_pattern
*ptn
= chip
->pdata
->patterns
;
408 int num_patterns
= chip
->pdata
->num_patterns
;
412 ret
= kstrtoul(buf
, 0, &mode
);
416 if (mode
> num_patterns
|| !ptn
)
419 mutex_lock(&chip
->lock
);
420 ret
= lp5562_run_predef_led_pattern(chip
, mode
);
421 mutex_unlock(&chip
->lock
);
429 static ssize_t
lp5562_store_engine_mux(struct device
*dev
,
430 struct device_attribute
*attr
,
431 const char *buf
, size_t len
)
433 struct lp55xx_led
*led
= i2c_get_clientdata(to_i2c_client(dev
));
434 struct lp55xx_chip
*chip
= led
->chip
;
439 * R ... Engine 1 (fixed)
440 * G ... Engine 2 (fixed)
441 * B ... Engine 3 (fixed)
442 * W ... Engine 1 or 2 or 3
445 if (sysfs_streq(buf
, "RGB")) {
446 mask
= LP5562_ENG_FOR_RGB_M
;
447 val
= LP5562_ENG_SEL_RGB
;
448 } else if (sysfs_streq(buf
, "W")) {
449 enum lp55xx_engine_index idx
= chip
->engine_idx
;
451 mask
= LP5562_ENG_FOR_W_M
;
453 case LP55XX_ENGINE_1
:
454 val
= LP5562_ENG1_FOR_W
;
456 case LP55XX_ENGINE_2
:
457 val
= LP5562_ENG2_FOR_W
;
459 case LP55XX_ENGINE_3
:
460 val
= LP5562_ENG3_FOR_W
;
467 dev_err(dev
, "choose RGB or W\n");
471 mutex_lock(&chip
->lock
);
472 lp55xx_update_bits(chip
, LP5562_REG_ENG_SEL
, mask
, val
);
473 mutex_unlock(&chip
->lock
);
478 static LP55XX_DEV_ATTR_WO(led_pattern
, lp5562_store_pattern
);
479 static LP55XX_DEV_ATTR_WO(engine_mux
, lp5562_store_engine_mux
);
481 static struct attribute
*lp5562_attributes
[] = {
482 &dev_attr_led_pattern
.attr
,
483 &dev_attr_engine_mux
.attr
,
487 static const struct attribute_group lp5562_group
= {
488 .attrs
= lp5562_attributes
,
491 /* Chip specific configurations */
492 static struct lp55xx_device_config lp5562_cfg
= {
493 .max_channel
= LP5562_MAX_LEDS
,
495 .addr
= LP5562_REG_RESET
,
499 .addr
= LP5562_REG_ENABLE
,
500 .val
= LP5562_ENABLE_DEFAULT
,
502 .post_init_device
= lp5562_post_init_device
,
503 .set_led_current
= lp5562_set_led_current
,
504 .brightness_fn
= lp5562_led_brightness
,
505 .run_engine
= lp5562_run_engine
,
506 .firmware_cb
= lp5562_firmware_loaded
,
507 .dev_attr_group
= &lp5562_group
,
510 static int lp5562_probe(struct i2c_client
*client
,
511 const struct i2c_device_id
*id
)
514 struct lp55xx_chip
*chip
;
515 struct lp55xx_led
*led
;
516 struct lp55xx_platform_data
*pdata
= dev_get_platdata(&client
->dev
);
517 struct device_node
*np
= client
->dev
.of_node
;
521 pdata
= lp55xx_of_populate_pdata(&client
->dev
, np
);
523 return PTR_ERR(pdata
);
525 dev_err(&client
->dev
, "no platform data\n");
530 chip
= devm_kzalloc(&client
->dev
, sizeof(*chip
), GFP_KERNEL
);
534 led
= devm_kcalloc(&client
->dev
,
535 pdata
->num_channels
, sizeof(*led
), GFP_KERNEL
);
541 chip
->cfg
= &lp5562_cfg
;
543 mutex_init(&chip
->lock
);
545 i2c_set_clientdata(client
, led
);
547 ret
= lp55xx_init_device(chip
);
551 ret
= lp55xx_register_leds(led
, chip
);
553 goto err_register_leds
;
555 ret
= lp55xx_register_sysfs(chip
);
557 dev_err(&client
->dev
, "registering sysfs failed\n");
558 goto err_register_sysfs
;
564 lp55xx_unregister_leds(led
, chip
);
566 lp55xx_deinit_device(chip
);
571 static int lp5562_remove(struct i2c_client
*client
)
573 struct lp55xx_led
*led
= i2c_get_clientdata(client
);
574 struct lp55xx_chip
*chip
= led
->chip
;
576 lp5562_stop_engine(chip
);
578 lp55xx_unregister_sysfs(chip
);
579 lp55xx_unregister_leds(led
, chip
);
580 lp55xx_deinit_device(chip
);
585 static const struct i2c_device_id lp5562_id
[] = {
589 MODULE_DEVICE_TABLE(i2c
, lp5562_id
);
592 static const struct of_device_id of_lp5562_leds_match
[] = {
593 { .compatible
= "ti,lp5562", },
597 MODULE_DEVICE_TABLE(of
, of_lp5562_leds_match
);
600 static struct i2c_driver lp5562_driver
= {
603 .of_match_table
= of_match_ptr(of_lp5562_leds_match
),
605 .probe
= lp5562_probe
,
606 .remove
= lp5562_remove
,
607 .id_table
= lp5562_id
,
610 module_i2c_driver(lp5562_driver
);
612 MODULE_DESCRIPTION("Texas Instruments LP5562 LED Driver");
613 MODULE_AUTHOR("Milo Kim");
614 MODULE_LICENSE("GPL");