1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright (C) 2013 Texas Instruments
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_run_engine(struct lp55xx_chip *chip, bool start)
133 lp55xx_write(chip, LP5562_REG_ENABLE, LP5562_ENABLE_DEFAULT);
134 lp5562_wait_enable_done();
135 lp55xx_stop_all_engine(chip);
136 lp55xx_write(chip, LP5562_REG_ENG_SEL, LP5562_ENG_SEL_PWM);
137 lp55xx_write(chip, LP5562_REG_OP_MODE, LP5562_CMD_DIRECT);
138 lp5562_wait_opmode_done();
142 ret = lp55xx_run_engine_common(chip);
144 lp5562_wait_enable_done();
147 static void lp5562_firmware_loaded(struct lp55xx_chip *chip)
149 const struct firmware *fw = chip->fw;
152 * the firmware is encoded in ascii hex character, with 2 chars
155 if (fw->size > (LP5562_PROGRAM_LENGTH * 2)) {
156 dev_err(&chip->cl->dev, "firmware data size overflow: %zu\n",
162 * Program memory sequence
163 * 1) set engine mode to "LOAD"
164 * 2) write firmware data into program memory
167 lp55xx_load_engine(chip);
168 lp55xx_update_program_memory(chip, fw->data, fw->size);
171 static int lp5562_post_init_device(struct lp55xx_chip *chip)
174 u8 cfg = LP5562_DEFAULT_CFG;
176 /* Set all PWMs to direct control mode */
177 ret = lp55xx_write(chip, LP5562_REG_OP_MODE, LP5562_CMD_DIRECT);
181 lp5562_wait_opmode_done();
183 /* Update configuration for the clock setting */
184 if (!lp55xx_is_extclk_used(chip))
185 cfg |= LP5562_CLK_INT;
187 ret = lp55xx_write(chip, LP5562_REG_CONFIG, cfg);
191 /* Initialize all channels PWM to zero -> leds off */
192 lp55xx_write(chip, LP5562_REG_R_PWM, 0);
193 lp55xx_write(chip, LP5562_REG_G_PWM, 0);
194 lp55xx_write(chip, LP5562_REG_B_PWM, 0);
195 lp55xx_write(chip, LP5562_REG_W_PWM, 0);
197 /* Set LED map as register PWM by default */
198 lp55xx_write(chip, LP5562_REG_ENG_SEL, LP5562_ENG_SEL_PWM);
203 static int lp5562_led_brightness(struct lp55xx_led *led)
205 struct lp55xx_chip *chip = led->chip;
206 static const u8 addr[] = {
214 mutex_lock(&chip->lock);
215 ret = lp55xx_write(chip, addr[led->chan_nr], led->brightness);
216 mutex_unlock(&chip->lock);
221 static void lp5562_write_program_memory(struct lp55xx_chip *chip,
222 u8 base, const u8 *rgb, int size)
226 if (!rgb || size <= 0)
229 for (i = 0; i < size; i++)
230 lp55xx_write(chip, base + i, *(rgb + i));
232 lp55xx_write(chip, base + i, 0);
233 lp55xx_write(chip, base + i + 1, 0);
236 /* check the size of program count */
237 static inline bool _is_pc_overflow(struct lp55xx_predef_pattern *ptn)
239 return ptn->size_r >= LP5562_PROGRAM_LENGTH ||
240 ptn->size_g >= LP5562_PROGRAM_LENGTH ||
241 ptn->size_b >= LP5562_PROGRAM_LENGTH;
244 static int lp5562_run_predef_led_pattern(struct lp55xx_chip *chip, int mode)
246 struct lp55xx_predef_pattern *ptn;
249 if (mode == LP5562_PATTERN_OFF) {
250 lp5562_run_engine(chip, false);
254 ptn = chip->pdata->patterns + (mode - 1);
255 if (!ptn || _is_pc_overflow(ptn)) {
256 dev_err(&chip->cl->dev, "invalid pattern data\n");
260 lp55xx_stop_all_engine(chip);
262 /* Set LED map as RGB */
263 lp55xx_write(chip, LP5562_REG_ENG_SEL, LP5562_ENG_SEL_RGB);
266 for (i = LP55XX_ENGINE_1; i <= LP55XX_ENGINE_3; i++) {
267 chip->engine_idx = i;
268 lp55xx_load_engine(chip);
271 /* Clear program registers */
272 lp55xx_write(chip, LP5562_REG_PROG_MEM_ENG1, 0);
273 lp55xx_write(chip, LP5562_REG_PROG_MEM_ENG1 + 1, 0);
274 lp55xx_write(chip, LP5562_REG_PROG_MEM_ENG2, 0);
275 lp55xx_write(chip, LP5562_REG_PROG_MEM_ENG2 + 1, 0);
276 lp55xx_write(chip, LP5562_REG_PROG_MEM_ENG3, 0);
277 lp55xx_write(chip, LP5562_REG_PROG_MEM_ENG3 + 1, 0);
279 /* Program engines */
280 lp5562_write_program_memory(chip, LP5562_REG_PROG_MEM_ENG1,
281 ptn->r, ptn->size_r);
282 lp5562_write_program_memory(chip, LP5562_REG_PROG_MEM_ENG2,
283 ptn->g, ptn->size_g);
284 lp5562_write_program_memory(chip, LP5562_REG_PROG_MEM_ENG3,
285 ptn->b, ptn->size_b);
288 lp5562_run_engine(chip, true);
293 static ssize_t lp5562_store_pattern(struct device *dev,
294 struct device_attribute *attr,
295 const char *buf, size_t len)
297 struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
298 struct lp55xx_chip *chip = led->chip;
299 struct lp55xx_predef_pattern *ptn = chip->pdata->patterns;
300 int num_patterns = chip->pdata->num_patterns;
304 ret = kstrtoul(buf, 0, &mode);
308 if (mode > num_patterns || !ptn)
311 mutex_lock(&chip->lock);
312 ret = lp5562_run_predef_led_pattern(chip, mode);
313 mutex_unlock(&chip->lock);
321 static ssize_t lp5562_store_engine_mux(struct device *dev,
322 struct device_attribute *attr,
323 const char *buf, size_t len)
325 struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
326 struct lp55xx_chip *chip = led->chip;
331 * R ... Engine 1 (fixed)
332 * G ... Engine 2 (fixed)
333 * B ... Engine 3 (fixed)
334 * W ... Engine 1 or 2 or 3
337 if (sysfs_streq(buf, "RGB")) {
338 mask = LP5562_ENG_FOR_RGB_M;
339 val = LP5562_ENG_SEL_RGB;
340 } else if (sysfs_streq(buf, "W")) {
341 enum lp55xx_engine_index idx = chip->engine_idx;
343 mask = LP5562_ENG_FOR_W_M;
345 case LP55XX_ENGINE_1:
346 val = LP5562_ENG1_FOR_W;
348 case LP55XX_ENGINE_2:
349 val = LP5562_ENG2_FOR_W;
351 case LP55XX_ENGINE_3:
352 val = LP5562_ENG3_FOR_W;
359 dev_err(dev, "choose RGB or W\n");
363 mutex_lock(&chip->lock);
364 lp55xx_update_bits(chip, LP5562_REG_ENG_SEL, mask, val);
365 mutex_unlock(&chip->lock);
370 static LP55XX_DEV_ATTR_WO(led_pattern, lp5562_store_pattern);
371 static LP55XX_DEV_ATTR_WO(engine_mux, lp5562_store_engine_mux);
373 static struct attribute *lp5562_attributes[] = {
374 &dev_attr_led_pattern.attr,
375 &dev_attr_engine_mux.attr,
379 static const struct attribute_group lp5562_group = {
380 .attrs = lp5562_attributes,
383 /* Chip specific configurations */
384 static struct lp55xx_device_config lp5562_cfg = {
385 .max_channel = LP5562_MAX_LEDS,
387 .addr = LP5562_REG_OP_MODE,
390 .addr = LP5562_REG_ENABLE,
393 .addr = LP5562_REG_RESET,
397 .addr = LP5562_REG_ENABLE,
398 .val = LP5562_ENABLE_DEFAULT,
401 .addr = LP5562_REG_PROG_MEM_ENG1,
403 .post_init_device = lp5562_post_init_device,
404 .set_led_current = lp5562_set_led_current,
405 .brightness_fn = lp5562_led_brightness,
406 .run_engine = lp5562_run_engine,
407 .firmware_cb = lp5562_firmware_loaded,
408 .dev_attr_group = &lp5562_group,
411 static const struct i2c_device_id lp5562_id[] = {
412 { "lp5562", .driver_data = (kernel_ulong_t)&lp5562_cfg, },
415 MODULE_DEVICE_TABLE(i2c, lp5562_id);
417 static const struct of_device_id of_lp5562_leds_match[] = {
418 { .compatible = "ti,lp5562", .data = &lp5562_cfg, },
422 MODULE_DEVICE_TABLE(of, of_lp5562_leds_match);
424 static struct i2c_driver lp5562_driver = {
427 .of_match_table = of_lp5562_leds_match,
429 .probe = lp55xx_probe,
430 .remove = lp55xx_remove,
431 .id_table = lp5562_id,
434 module_i2c_driver(lp5562_driver);
436 MODULE_DESCRIPTION("Texas Instruments LP5562 LED Driver");
437 MODULE_AUTHOR("Milo Kim");
438 MODULE_LICENSE("GPL");