xref: /openbmc/linux/drivers/leds/leds-lp5562.c (revision 0d07cf5e)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * LP5562 LED driver
4  *
5  * Copyright (C) 2013 Texas Instruments
6  *
7  * Author: Milo(Woogyom) Kim <milo.kim@ti.com>
8  */
9 
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>
16 #include <linux/of.h>
17 #include <linux/platform_data/leds-lp55xx.h>
18 #include <linux/slab.h>
19 
20 #include "leds-lp55xx-common.h"
21 
22 #define LP5562_PROGRAM_LENGTH		32
23 #define LP5562_MAX_LEDS			4
24 
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)
38 
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)
56 
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
62 
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
68 
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)
75 
76 /* RESET Register 0Dh */
77 #define LP5562_REG_RESET		0x0D
78 #define LP5562_RESET			0xFF
79 
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
84 
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 */
94 
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
101 
102 static inline void lp5562_wait_opmode_done(void)
103 {
104 	/* operation mode change needs to be longer than 153 us */
105 	usleep_range(200, 300);
106 }
107 
108 static inline void lp5562_wait_enable_done(void)
109 {
110 	/* it takes more 488 us to update ENABLE register */
111 	usleep_range(500, 600);
112 }
113 
114 static void lp5562_set_led_current(struct lp55xx_led *led, u8 led_current)
115 {
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,
121 	};
122 
123 	led->led_current = led_current;
124 	lp55xx_write(led->chip, addr[led->chan_nr], led_current);
125 }
126 
127 static void lp5562_load_engine(struct lp55xx_chip *chip)
128 {
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,
134 	};
135 
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,
140 	};
141 
142 	lp55xx_update_bits(chip, LP5562_REG_OP_MODE, mask[idx], val[idx]);
143 
144 	lp5562_wait_opmode_done();
145 }
146 
147 static void lp5562_stop_engine(struct lp55xx_chip *chip)
148 {
149 	lp55xx_write(chip, LP5562_REG_OP_MODE, LP5562_CMD_DISABLE);
150 	lp5562_wait_opmode_done();
151 }
152 
153 static void lp5562_run_engine(struct lp55xx_chip *chip, bool start)
154 {
155 	int ret;
156 	u8 mode;
157 	u8 exec;
158 
159 	/* stop engine */
160 	if (!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();
167 		return;
168 	}
169 
170 	/*
171 	 * To run the engine,
172 	 * operation mode and enable register should updated at the same time
173 	 */
174 
175 	ret = lp55xx_read(chip, LP5562_REG_OP_MODE, &mode);
176 	if (ret)
177 		return;
178 
179 	ret = lp55xx_read(chip, LP5562_REG_ENABLE, &exec);
180 	if (ret)
181 		return;
182 
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;
187 	}
188 
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;
192 	}
193 
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;
197 	}
198 
199 	lp55xx_write(chip, LP5562_REG_OP_MODE, mode);
200 	lp5562_wait_opmode_done();
201 
202 	lp55xx_update_bits(chip, LP5562_REG_ENABLE, LP5562_EXEC_M, exec);
203 	lp5562_wait_enable_done();
204 }
205 
206 static int lp5562_update_firmware(struct lp55xx_chip *chip,
207 					const u8 *data, size_t size)
208 {
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,
215 	};
216 	unsigned cmd;
217 	char c[3];
218 	int program_size;
219 	int nrchars;
220 	int offset = 0;
221 	int ret;
222 	int i;
223 
224 	/* clear program memory before updating */
225 	for (i = 0; i < LP5562_PROGRAM_LENGTH; i++)
226 		lp55xx_write(chip, addr[idx] + i, 0);
227 
228 	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);
232 		if (ret != 1)
233 			goto err;
234 
235 		ret = sscanf(c, "%2x", &cmd);
236 		if (ret != 1)
237 			goto err;
238 
239 		pattern[i] = (u8)cmd;
240 		offset += nrchars;
241 		i++;
242 	}
243 
244 	/* Each instruction is 16bit long. Check that length is even */
245 	if (i % 2)
246 		goto err;
247 
248 	program_size = i;
249 	for (i = 0; i < program_size; i++)
250 		lp55xx_write(chip, addr[idx] + i, pattern[i]);
251 
252 	return 0;
253 
254 err:
255 	dev_err(&chip->cl->dev, "wrong pattern format\n");
256 	return -EINVAL;
257 }
258 
259 static void lp5562_firmware_loaded(struct lp55xx_chip *chip)
260 {
261 	const struct firmware *fw = chip->fw;
262 
263 	if (fw->size > LP5562_PROGRAM_LENGTH) {
264 		dev_err(&chip->cl->dev, "firmware data size overflow: %zu\n",
265 			fw->size);
266 		return;
267 	}
268 
269 	/*
270 	 * Program memory sequence
271 	 *  1) set engine mode to "LOAD"
272 	 *  2) write firmware data into program memory
273 	 */
274 
275 	lp5562_load_engine(chip);
276 	lp5562_update_firmware(chip, fw->data, fw->size);
277 }
278 
279 static int lp5562_post_init_device(struct lp55xx_chip *chip)
280 {
281 	int ret;
282 	u8 cfg = LP5562_DEFAULT_CFG;
283 
284 	/* Set all PWMs to direct control mode */
285 	ret = lp55xx_write(chip, LP5562_REG_OP_MODE, LP5562_CMD_DIRECT);
286 	if (ret)
287 		return ret;
288 
289 	lp5562_wait_opmode_done();
290 
291 	/* Update configuration for the clock setting */
292 	if (!lp55xx_is_extclk_used(chip))
293 		cfg |= LP5562_CLK_INT;
294 
295 	ret = lp55xx_write(chip, LP5562_REG_CONFIG, cfg);
296 	if (ret)
297 		return ret;
298 
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);
304 
305 	/* Set LED map as register PWM by default */
306 	lp55xx_write(chip, LP5562_REG_ENG_SEL, LP5562_ENG_SEL_PWM);
307 
308 	return 0;
309 }
310 
311 static int lp5562_led_brightness(struct lp55xx_led *led)
312 {
313 	struct lp55xx_chip *chip = led->chip;
314 	static const u8 addr[] = {
315 		LP5562_REG_R_PWM,
316 		LP5562_REG_G_PWM,
317 		LP5562_REG_B_PWM,
318 		LP5562_REG_W_PWM,
319 	};
320 	int ret;
321 
322 	mutex_lock(&chip->lock);
323 	ret = lp55xx_write(chip, addr[led->chan_nr], led->brightness);
324 	mutex_unlock(&chip->lock);
325 
326 	return ret;
327 }
328 
329 static void lp5562_write_program_memory(struct lp55xx_chip *chip,
330 					u8 base, const u8 *rgb, int size)
331 {
332 	int i;
333 
334 	if (!rgb || size <= 0)
335 		return;
336 
337 	for (i = 0; i < size; i++)
338 		lp55xx_write(chip, base + i, *(rgb + i));
339 
340 	lp55xx_write(chip, base + i, 0);
341 	lp55xx_write(chip, base + i + 1, 0);
342 }
343 
344 /* check the size of program count */
345 static inline bool _is_pc_overflow(struct lp55xx_predef_pattern *ptn)
346 {
347 	return ptn->size_r >= LP5562_PROGRAM_LENGTH ||
348 	       ptn->size_g >= LP5562_PROGRAM_LENGTH ||
349 	       ptn->size_b >= LP5562_PROGRAM_LENGTH;
350 }
351 
352 static int lp5562_run_predef_led_pattern(struct lp55xx_chip *chip, int mode)
353 {
354 	struct lp55xx_predef_pattern *ptn;
355 	int i;
356 
357 	if (mode == LP5562_PATTERN_OFF) {
358 		lp5562_run_engine(chip, false);
359 		return 0;
360 	}
361 
362 	ptn = chip->pdata->patterns + (mode - 1);
363 	if (!ptn || _is_pc_overflow(ptn)) {
364 		dev_err(&chip->cl->dev, "invalid pattern data\n");
365 		return -EINVAL;
366 	}
367 
368 	lp5562_stop_engine(chip);
369 
370 	/* Set LED map as RGB */
371 	lp55xx_write(chip, LP5562_REG_ENG_SEL, LP5562_ENG_SEL_RGB);
372 
373 	/* Load engines */
374 	for (i = LP55XX_ENGINE_1; i <= LP55XX_ENGINE_3; i++) {
375 		chip->engine_idx = i;
376 		lp5562_load_engine(chip);
377 	}
378 
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);
386 
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);
394 
395 	/* Run engines */
396 	lp5562_run_engine(chip, true);
397 
398 	return 0;
399 }
400 
401 static ssize_t lp5562_store_pattern(struct device *dev,
402 				struct device_attribute *attr,
403 				const char *buf, size_t len)
404 {
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;
409 	unsigned long mode;
410 	int ret;
411 
412 	ret = kstrtoul(buf, 0, &mode);
413 	if (ret)
414 		return ret;
415 
416 	if (mode > num_patterns || !ptn)
417 		return -EINVAL;
418 
419 	mutex_lock(&chip->lock);
420 	ret = lp5562_run_predef_led_pattern(chip, mode);
421 	mutex_unlock(&chip->lock);
422 
423 	if (ret)
424 		return ret;
425 
426 	return len;
427 }
428 
429 static ssize_t lp5562_store_engine_mux(struct device *dev,
430 				     struct device_attribute *attr,
431 				     const char *buf, size_t len)
432 {
433 	struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
434 	struct lp55xx_chip *chip = led->chip;
435 	u8 mask;
436 	u8 val;
437 
438 	/* LED map
439 	 * R ... Engine 1 (fixed)
440 	 * G ... Engine 2 (fixed)
441 	 * B ... Engine 3 (fixed)
442 	 * W ... Engine 1 or 2 or 3
443 	 */
444 
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;
450 
451 		mask = LP5562_ENG_FOR_W_M;
452 		switch (idx) {
453 		case LP55XX_ENGINE_1:
454 			val = LP5562_ENG1_FOR_W;
455 			break;
456 		case LP55XX_ENGINE_2:
457 			val = LP5562_ENG2_FOR_W;
458 			break;
459 		case LP55XX_ENGINE_3:
460 			val = LP5562_ENG3_FOR_W;
461 			break;
462 		default:
463 			return -EINVAL;
464 		}
465 
466 	} else {
467 		dev_err(dev, "choose RGB or W\n");
468 		return -EINVAL;
469 	}
470 
471 	mutex_lock(&chip->lock);
472 	lp55xx_update_bits(chip, LP5562_REG_ENG_SEL, mask, val);
473 	mutex_unlock(&chip->lock);
474 
475 	return len;
476 }
477 
478 static LP55XX_DEV_ATTR_WO(led_pattern, lp5562_store_pattern);
479 static LP55XX_DEV_ATTR_WO(engine_mux, lp5562_store_engine_mux);
480 
481 static struct attribute *lp5562_attributes[] = {
482 	&dev_attr_led_pattern.attr,
483 	&dev_attr_engine_mux.attr,
484 	NULL,
485 };
486 
487 static const struct attribute_group lp5562_group = {
488 	.attrs = lp5562_attributes,
489 };
490 
491 /* Chip specific configurations */
492 static struct lp55xx_device_config lp5562_cfg = {
493 	.max_channel  = LP5562_MAX_LEDS,
494 	.reset = {
495 		.addr = LP5562_REG_RESET,
496 		.val  = LP5562_RESET,
497 	},
498 	.enable = {
499 		.addr = LP5562_REG_ENABLE,
500 		.val  = LP5562_ENABLE_DEFAULT,
501 	},
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,
508 };
509 
510 static int lp5562_probe(struct i2c_client *client,
511 			const struct i2c_device_id *id)
512 {
513 	int ret;
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;
518 
519 	if (!pdata) {
520 		if (np) {
521 			pdata = lp55xx_of_populate_pdata(&client->dev, np);
522 			if (IS_ERR(pdata))
523 				return PTR_ERR(pdata);
524 		} else {
525 			dev_err(&client->dev, "no platform data\n");
526 			return -EINVAL;
527 		}
528 	}
529 
530 	chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
531 	if (!chip)
532 		return -ENOMEM;
533 
534 	led = devm_kcalloc(&client->dev,
535 			pdata->num_channels, sizeof(*led), GFP_KERNEL);
536 	if (!led)
537 		return -ENOMEM;
538 
539 	chip->cl = client;
540 	chip->pdata = pdata;
541 	chip->cfg = &lp5562_cfg;
542 
543 	mutex_init(&chip->lock);
544 
545 	i2c_set_clientdata(client, led);
546 
547 	ret = lp55xx_init_device(chip);
548 	if (ret)
549 		goto err_init;
550 
551 	ret = lp55xx_register_leds(led, chip);
552 	if (ret)
553 		goto err_register_leds;
554 
555 	ret = lp55xx_register_sysfs(chip);
556 	if (ret) {
557 		dev_err(&client->dev, "registering sysfs failed\n");
558 		goto err_register_sysfs;
559 	}
560 
561 	return 0;
562 
563 err_register_sysfs:
564 	lp55xx_unregister_leds(led, chip);
565 err_register_leds:
566 	lp55xx_deinit_device(chip);
567 err_init:
568 	return ret;
569 }
570 
571 static int lp5562_remove(struct i2c_client *client)
572 {
573 	struct lp55xx_led *led = i2c_get_clientdata(client);
574 	struct lp55xx_chip *chip = led->chip;
575 
576 	lp5562_stop_engine(chip);
577 
578 	lp55xx_unregister_sysfs(chip);
579 	lp55xx_unregister_leds(led, chip);
580 	lp55xx_deinit_device(chip);
581 
582 	return 0;
583 }
584 
585 static const struct i2c_device_id lp5562_id[] = {
586 	{ "lp5562", 0 },
587 	{ }
588 };
589 MODULE_DEVICE_TABLE(i2c, lp5562_id);
590 
591 #ifdef CONFIG_OF
592 static const struct of_device_id of_lp5562_leds_match[] = {
593 	{ .compatible = "ti,lp5562", },
594 	{},
595 };
596 
597 MODULE_DEVICE_TABLE(of, of_lp5562_leds_match);
598 #endif
599 
600 static struct i2c_driver lp5562_driver = {
601 	.driver = {
602 		.name	= "lp5562",
603 		.of_match_table = of_match_ptr(of_lp5562_leds_match),
604 	},
605 	.probe		= lp5562_probe,
606 	.remove		= lp5562_remove,
607 	.id_table	= lp5562_id,
608 };
609 
610 module_i2c_driver(lp5562_driver);
611 
612 MODULE_DESCRIPTION("Texas Instruments LP5562 LED Driver");
613 MODULE_AUTHOR("Milo Kim");
614 MODULE_LICENSE("GPL");
615