xref: /linux/drivers/leds/leds-lp5521.c (revision d642ef71)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * LP5521 LED chip driver.
4  *
5  * Copyright (C) 2010 Nokia Corporation
6  * Copyright (C) 2012 Texas Instruments
7  *
8  * Contact: Samu Onkalo <samu.p.onkalo@nokia.com>
9  *          Milo(Woogyom) Kim <milo.kim@ti.com>
10  */
11 
12 #include <linux/delay.h>
13 #include <linux/firmware.h>
14 #include <linux/i2c.h>
15 #include <linux/leds.h>
16 #include <linux/module.h>
17 #include <linux/mutex.h>
18 #include <linux/platform_data/leds-lp55xx.h>
19 #include <linux/slab.h>
20 #include <linux/of.h>
21 
22 #include "leds-lp55xx-common.h"
23 
24 #define LP5521_PROGRAM_LENGTH		32
25 #define LP5521_MAX_LEDS			3
26 #define LP5521_CMD_DIRECT		0x3F
27 
28 /* Registers */
29 #define LP5521_REG_ENABLE		0x00
30 #define LP5521_REG_OP_MODE		0x01
31 #define LP5521_REG_R_PWM		0x02
32 #define LP5521_REG_G_PWM		0x03
33 #define LP5521_REG_B_PWM		0x04
34 #define LP5521_REG_R_CURRENT		0x05
35 #define LP5521_REG_G_CURRENT		0x06
36 #define LP5521_REG_B_CURRENT		0x07
37 #define LP5521_REG_CONFIG		0x08
38 #define LP5521_REG_STATUS		0x0C
39 #define LP5521_REG_RESET		0x0D
40 #define LP5521_REG_R_PROG_MEM		0x10
41 #define LP5521_REG_G_PROG_MEM		0x30
42 #define LP5521_REG_B_PROG_MEM		0x50
43 
44 /* Base register to set LED current */
45 #define LP5521_REG_LED_CURRENT_BASE	LP5521_REG_R_CURRENT
46 /* Base register to set the brightness */
47 #define LP5521_REG_LED_PWM_BASE		LP5521_REG_R_PWM
48 
49 /* Bits in ENABLE register */
50 #define LP5521_MASTER_ENABLE		0x40	/* Chip master enable */
51 #define LP5521_LOGARITHMIC_PWM		0x80	/* Logarithmic PWM adjustment */
52 #define LP5521_EXEC_RUN			0x2A
53 #define LP5521_ENABLE_DEFAULT	\
54 	(LP5521_MASTER_ENABLE | LP5521_LOGARITHMIC_PWM)
55 #define LP5521_ENABLE_RUN_PROGRAM	\
56 	(LP5521_ENABLE_DEFAULT | LP5521_EXEC_RUN)
57 
58 /* CONFIG register */
59 #define LP5521_PWM_HF			0x40	/* PWM: 0 = 256Hz, 1 = 558Hz */
60 #define LP5521_PWRSAVE_EN		0x20	/* 1 = Power save mode */
61 #define LP5521_CP_MODE_MASK		0x18	/* Charge pump mode */
62 #define LP5521_CP_MODE_SHIFT		3
63 #define LP5521_R_TO_BATT		0x04	/* R out: 0 = CP, 1 = Vbat */
64 #define LP5521_CLK_INT			0x01	/* Internal clock */
65 #define LP5521_DEFAULT_CFG		(LP5521_PWM_HF | LP5521_PWRSAVE_EN)
66 
67 /* Status */
68 #define LP5521_EXT_CLK_USED		0x08
69 
70 /* default R channel current register value */
71 #define LP5521_REG_R_CURR_DEFAULT	0xAF
72 
73 /* Reset register value */
74 #define LP5521_RESET			0xFF
75 
76 /* Program Memory Operations */
77 #define LP5521_MODE_R_M			0x30	/* Operation Mode Register */
78 #define LP5521_MODE_G_M			0x0C
79 #define LP5521_MODE_B_M			0x03
80 #define LP5521_LOAD_R			0x10
81 #define LP5521_LOAD_G			0x04
82 #define LP5521_LOAD_B			0x01
83 
84 #define LP5521_R_IS_LOADING(mode)	\
85 	((mode & LP5521_MODE_R_M) == LP5521_LOAD_R)
86 #define LP5521_G_IS_LOADING(mode)	\
87 	((mode & LP5521_MODE_G_M) == LP5521_LOAD_G)
88 #define LP5521_B_IS_LOADING(mode)	\
89 	((mode & LP5521_MODE_B_M) == LP5521_LOAD_B)
90 
91 #define LP5521_EXEC_R_M			0x30	/* Enable Register */
92 #define LP5521_EXEC_G_M			0x0C
93 #define LP5521_EXEC_B_M			0x03
94 #define LP5521_EXEC_M			0x3F
95 #define LP5521_RUN_R			0x20
96 #define LP5521_RUN_G			0x08
97 #define LP5521_RUN_B			0x02
98 
99 static inline void lp5521_wait_opmode_done(void)
100 {
101 	/* operation mode change needs to be longer than 153 us */
102 	usleep_range(200, 300);
103 }
104 
105 static inline void lp5521_wait_enable_done(void)
106 {
107 	/* it takes more 488 us to update ENABLE register */
108 	usleep_range(500, 600);
109 }
110 
111 static void lp5521_set_led_current(struct lp55xx_led *led, u8 led_current)
112 {
113 	led->led_current = led_current;
114 	lp55xx_write(led->chip, LP5521_REG_LED_CURRENT_BASE + led->chan_nr,
115 		led_current);
116 }
117 
118 static void lp5521_load_engine(struct lp55xx_chip *chip)
119 {
120 	enum lp55xx_engine_index idx = chip->engine_idx;
121 	static const u8 mask[] = {
122 		[LP55XX_ENGINE_1] = LP5521_MODE_R_M,
123 		[LP55XX_ENGINE_2] = LP5521_MODE_G_M,
124 		[LP55XX_ENGINE_3] = LP5521_MODE_B_M,
125 	};
126 
127 	static const u8 val[] = {
128 		[LP55XX_ENGINE_1] = LP5521_LOAD_R,
129 		[LP55XX_ENGINE_2] = LP5521_LOAD_G,
130 		[LP55XX_ENGINE_3] = LP5521_LOAD_B,
131 	};
132 
133 	lp55xx_update_bits(chip, LP5521_REG_OP_MODE, mask[idx], val[idx]);
134 
135 	lp5521_wait_opmode_done();
136 }
137 
138 static void lp5521_stop_all_engines(struct lp55xx_chip *chip)
139 {
140 	lp55xx_write(chip, LP5521_REG_OP_MODE, 0);
141 	lp5521_wait_opmode_done();
142 }
143 
144 static void lp5521_stop_engine(struct lp55xx_chip *chip)
145 {
146 	enum lp55xx_engine_index idx = chip->engine_idx;
147 	static const u8 mask[] = {
148 		[LP55XX_ENGINE_1] = LP5521_MODE_R_M,
149 		[LP55XX_ENGINE_2] = LP5521_MODE_G_M,
150 		[LP55XX_ENGINE_3] = LP5521_MODE_B_M,
151 	};
152 
153 	lp55xx_update_bits(chip, LP5521_REG_OP_MODE, mask[idx], 0);
154 
155 	lp5521_wait_opmode_done();
156 }
157 
158 static void lp5521_run_engine(struct lp55xx_chip *chip, bool start)
159 {
160 	int ret;
161 	u8 mode;
162 	u8 exec;
163 
164 	/* stop engine */
165 	if (!start) {
166 		lp5521_stop_engine(chip);
167 		lp55xx_write(chip, LP5521_REG_OP_MODE, LP5521_CMD_DIRECT);
168 		lp5521_wait_opmode_done();
169 		return;
170 	}
171 
172 	/*
173 	 * To run the engine,
174 	 * operation mode and enable register should updated at the same time
175 	 */
176 
177 	ret = lp55xx_read(chip, LP5521_REG_OP_MODE, &mode);
178 	if (ret)
179 		return;
180 
181 	ret = lp55xx_read(chip, LP5521_REG_ENABLE, &exec);
182 	if (ret)
183 		return;
184 
185 	/* change operation mode to RUN only when each engine is loading */
186 	if (LP5521_R_IS_LOADING(mode)) {
187 		mode = (mode & ~LP5521_MODE_R_M) | LP5521_RUN_R;
188 		exec = (exec & ~LP5521_EXEC_R_M) | LP5521_RUN_R;
189 	}
190 
191 	if (LP5521_G_IS_LOADING(mode)) {
192 		mode = (mode & ~LP5521_MODE_G_M) | LP5521_RUN_G;
193 		exec = (exec & ~LP5521_EXEC_G_M) | LP5521_RUN_G;
194 	}
195 
196 	if (LP5521_B_IS_LOADING(mode)) {
197 		mode = (mode & ~LP5521_MODE_B_M) | LP5521_RUN_B;
198 		exec = (exec & ~LP5521_EXEC_B_M) | LP5521_RUN_B;
199 	}
200 
201 	lp55xx_write(chip, LP5521_REG_OP_MODE, mode);
202 	lp5521_wait_opmode_done();
203 
204 	lp55xx_update_bits(chip, LP5521_REG_ENABLE, LP5521_EXEC_M, exec);
205 	lp5521_wait_enable_done();
206 }
207 
208 static int lp5521_update_program_memory(struct lp55xx_chip *chip,
209 					const u8 *data, size_t size)
210 {
211 	enum lp55xx_engine_index idx = chip->engine_idx;
212 	u8 pattern[LP5521_PROGRAM_LENGTH] = {0};
213 	static const u8 addr[] = {
214 		[LP55XX_ENGINE_1] = LP5521_REG_R_PROG_MEM,
215 		[LP55XX_ENGINE_2] = LP5521_REG_G_PROG_MEM,
216 		[LP55XX_ENGINE_3] = LP5521_REG_B_PROG_MEM,
217 	};
218 	unsigned cmd;
219 	char c[3];
220 	int nrchars;
221 	int ret;
222 	int offset = 0;
223 	int i = 0;
224 
225 	while ((offset < size - 1) && (i < LP5521_PROGRAM_LENGTH)) {
226 		/* separate sscanfs because length is working only for %s */
227 		ret = sscanf(data + offset, "%2s%n ", c, &nrchars);
228 		if (ret != 1)
229 			goto err;
230 
231 		ret = sscanf(c, "%2x", &cmd);
232 		if (ret != 1)
233 			goto err;
234 
235 		pattern[i] = (u8)cmd;
236 		offset += nrchars;
237 		i++;
238 	}
239 
240 	/* Each instruction is 16bit long. Check that length is even */
241 	if (i % 2)
242 		goto err;
243 
244 	for (i = 0; i < LP5521_PROGRAM_LENGTH; i++) {
245 		ret = lp55xx_write(chip, addr[idx] + i, pattern[i]);
246 		if (ret)
247 			return -EINVAL;
248 	}
249 
250 	return size;
251 
252 err:
253 	dev_err(&chip->cl->dev, "wrong pattern format\n");
254 	return -EINVAL;
255 }
256 
257 static void lp5521_firmware_loaded(struct lp55xx_chip *chip)
258 {
259 	const struct firmware *fw = chip->fw;
260 
261 	if (fw->size > LP5521_PROGRAM_LENGTH) {
262 		dev_err(&chip->cl->dev, "firmware data size overflow: %zu\n",
263 			fw->size);
264 		return;
265 	}
266 
267 	/*
268 	 * Program memory sequence
269 	 *  1) set engine mode to "LOAD"
270 	 *  2) write firmware data into program memory
271 	 */
272 
273 	lp5521_load_engine(chip);
274 	lp5521_update_program_memory(chip, fw->data, fw->size);
275 }
276 
277 static int lp5521_post_init_device(struct lp55xx_chip *chip)
278 {
279 	int ret;
280 	u8 val;
281 
282 	/*
283 	 * Make sure that the chip is reset by reading back the r channel
284 	 * current reg. This is dummy read is required on some platforms -
285 	 * otherwise further access to the R G B channels in the
286 	 * LP5521_REG_ENABLE register will not have any effect - strange!
287 	 */
288 	ret = lp55xx_read(chip, LP5521_REG_R_CURRENT, &val);
289 	if (ret) {
290 		dev_err(&chip->cl->dev, "error in resetting chip\n");
291 		return ret;
292 	}
293 	if (val != LP5521_REG_R_CURR_DEFAULT) {
294 		dev_err(&chip->cl->dev,
295 			"unexpected data in register (expected 0x%x got 0x%x)\n",
296 			LP5521_REG_R_CURR_DEFAULT, val);
297 		ret = -EINVAL;
298 		return ret;
299 	}
300 	usleep_range(10000, 20000);
301 
302 	/* Set all PWMs to direct control mode */
303 	ret = lp55xx_write(chip, LP5521_REG_OP_MODE, LP5521_CMD_DIRECT);
304 	if (ret)
305 		return ret;
306 
307 	/* Update configuration for the clock setting */
308 	val = LP5521_DEFAULT_CFG;
309 	if (!lp55xx_is_extclk_used(chip))
310 		val |= LP5521_CLK_INT;
311 
312 	val |= (chip->pdata->charge_pump_mode << LP5521_CP_MODE_SHIFT) & LP5521_CP_MODE_MASK;
313 
314 	ret = lp55xx_write(chip, LP5521_REG_CONFIG, val);
315 	if (ret)
316 		return ret;
317 
318 	/* Initialize all channels PWM to zero -> leds off */
319 	lp55xx_write(chip, LP5521_REG_R_PWM, 0);
320 	lp55xx_write(chip, LP5521_REG_G_PWM, 0);
321 	lp55xx_write(chip, LP5521_REG_B_PWM, 0);
322 
323 	/* Set engines are set to run state when OP_MODE enables engines */
324 	ret = lp55xx_write(chip, LP5521_REG_ENABLE, LP5521_ENABLE_RUN_PROGRAM);
325 	if (ret)
326 		return ret;
327 
328 	lp5521_wait_enable_done();
329 
330 	return 0;
331 }
332 
333 static int lp5521_run_selftest(struct lp55xx_chip *chip, char *buf)
334 {
335 	struct lp55xx_platform_data *pdata = chip->pdata;
336 	int ret;
337 	u8 status;
338 
339 	ret = lp55xx_read(chip, LP5521_REG_STATUS, &status);
340 	if (ret < 0)
341 		return ret;
342 
343 	if (pdata->clock_mode != LP55XX_CLOCK_EXT)
344 		return 0;
345 
346 	/* Check that ext clock is really in use if requested */
347 	if  ((status & LP5521_EXT_CLK_USED) == 0)
348 		return -EIO;
349 
350 	return 0;
351 }
352 
353 static int lp5521_multicolor_brightness(struct lp55xx_led *led)
354 {
355 	struct lp55xx_chip *chip = led->chip;
356 	int ret;
357 	int i;
358 
359 	mutex_lock(&chip->lock);
360 	for (i = 0; i < led->mc_cdev.num_colors; i++) {
361 		ret = lp55xx_write(chip,
362 				   LP5521_REG_LED_PWM_BASE +
363 				   led->mc_cdev.subled_info[i].channel,
364 				   led->mc_cdev.subled_info[i].brightness);
365 		if (ret)
366 			break;
367 	}
368 	mutex_unlock(&chip->lock);
369 	return ret;
370 }
371 
372 static int lp5521_led_brightness(struct lp55xx_led *led)
373 {
374 	struct lp55xx_chip *chip = led->chip;
375 	int ret;
376 
377 	mutex_lock(&chip->lock);
378 	ret = lp55xx_write(chip, LP5521_REG_LED_PWM_BASE + led->chan_nr,
379 		led->brightness);
380 	mutex_unlock(&chip->lock);
381 
382 	return ret;
383 }
384 
385 static ssize_t show_engine_mode(struct device *dev,
386 				struct device_attribute *attr,
387 				char *buf, int nr)
388 {
389 	struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
390 	struct lp55xx_chip *chip = led->chip;
391 	enum lp55xx_engine_mode mode = chip->engines[nr - 1].mode;
392 
393 	switch (mode) {
394 	case LP55XX_ENGINE_RUN:
395 		return sprintf(buf, "run\n");
396 	case LP55XX_ENGINE_LOAD:
397 		return sprintf(buf, "load\n");
398 	case LP55XX_ENGINE_DISABLED:
399 	default:
400 		return sprintf(buf, "disabled\n");
401 	}
402 }
403 show_mode(1)
404 show_mode(2)
405 show_mode(3)
406 
407 static ssize_t store_engine_mode(struct device *dev,
408 				 struct device_attribute *attr,
409 				 const char *buf, size_t len, int nr)
410 {
411 	struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
412 	struct lp55xx_chip *chip = led->chip;
413 	struct lp55xx_engine *engine = &chip->engines[nr - 1];
414 
415 	mutex_lock(&chip->lock);
416 
417 	chip->engine_idx = nr;
418 
419 	if (!strncmp(buf, "run", 3)) {
420 		lp5521_run_engine(chip, true);
421 		engine->mode = LP55XX_ENGINE_RUN;
422 	} else if (!strncmp(buf, "load", 4)) {
423 		lp5521_stop_engine(chip);
424 		lp5521_load_engine(chip);
425 		engine->mode = LP55XX_ENGINE_LOAD;
426 	} else if (!strncmp(buf, "disabled", 8)) {
427 		lp5521_stop_engine(chip);
428 		engine->mode = LP55XX_ENGINE_DISABLED;
429 	}
430 
431 	mutex_unlock(&chip->lock);
432 
433 	return len;
434 }
435 store_mode(1)
436 store_mode(2)
437 store_mode(3)
438 
439 static ssize_t store_engine_load(struct device *dev,
440 			     struct device_attribute *attr,
441 			     const char *buf, size_t len, int nr)
442 {
443 	struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
444 	struct lp55xx_chip *chip = led->chip;
445 	int ret;
446 
447 	mutex_lock(&chip->lock);
448 
449 	chip->engine_idx = nr;
450 	lp5521_load_engine(chip);
451 	ret = lp5521_update_program_memory(chip, buf, len);
452 
453 	mutex_unlock(&chip->lock);
454 
455 	return ret;
456 }
457 store_load(1)
458 store_load(2)
459 store_load(3)
460 
461 static ssize_t lp5521_selftest(struct device *dev,
462 			       struct device_attribute *attr,
463 			       char *buf)
464 {
465 	struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
466 	struct lp55xx_chip *chip = led->chip;
467 	int ret;
468 
469 	mutex_lock(&chip->lock);
470 	ret = lp5521_run_selftest(chip, buf);
471 	mutex_unlock(&chip->lock);
472 
473 	return sysfs_emit(buf, "%s\n", ret ? "FAIL" : "OK");
474 }
475 
476 /* device attributes */
477 static LP55XX_DEV_ATTR_RW(engine1_mode, show_engine1_mode, store_engine1_mode);
478 static LP55XX_DEV_ATTR_RW(engine2_mode, show_engine2_mode, store_engine2_mode);
479 static LP55XX_DEV_ATTR_RW(engine3_mode, show_engine3_mode, store_engine3_mode);
480 static LP55XX_DEV_ATTR_WO(engine1_load, store_engine1_load);
481 static LP55XX_DEV_ATTR_WO(engine2_load, store_engine2_load);
482 static LP55XX_DEV_ATTR_WO(engine3_load, store_engine3_load);
483 static LP55XX_DEV_ATTR_RO(selftest, lp5521_selftest);
484 
485 static struct attribute *lp5521_attributes[] = {
486 	&dev_attr_engine1_mode.attr,
487 	&dev_attr_engine2_mode.attr,
488 	&dev_attr_engine3_mode.attr,
489 	&dev_attr_engine1_load.attr,
490 	&dev_attr_engine2_load.attr,
491 	&dev_attr_engine3_load.attr,
492 	&dev_attr_selftest.attr,
493 	NULL
494 };
495 
496 static const struct attribute_group lp5521_group = {
497 	.attrs = lp5521_attributes,
498 };
499 
500 /* Chip specific configurations */
501 static struct lp55xx_device_config lp5521_cfg = {
502 	.reset = {
503 		.addr = LP5521_REG_RESET,
504 		.val  = LP5521_RESET,
505 	},
506 	.enable = {
507 		.addr = LP5521_REG_ENABLE,
508 		.val  = LP5521_ENABLE_DEFAULT,
509 	},
510 	.max_channel  = LP5521_MAX_LEDS,
511 	.post_init_device   = lp5521_post_init_device,
512 	.brightness_fn      = lp5521_led_brightness,
513 	.multicolor_brightness_fn = lp5521_multicolor_brightness,
514 	.set_led_current    = lp5521_set_led_current,
515 	.firmware_cb        = lp5521_firmware_loaded,
516 	.run_engine         = lp5521_run_engine,
517 	.dev_attr_group     = &lp5521_group,
518 };
519 
520 static int lp5521_probe(struct i2c_client *client)
521 {
522 	const struct i2c_device_id *id = i2c_client_get_device_id(client);
523 	int ret;
524 	struct lp55xx_chip *chip;
525 	struct lp55xx_led *led;
526 	struct lp55xx_platform_data *pdata = dev_get_platdata(&client->dev);
527 	struct device_node *np = dev_of_node(&client->dev);
528 
529 	chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
530 	if (!chip)
531 		return -ENOMEM;
532 
533 	chip->cfg = &lp5521_cfg;
534 
535 	if (!pdata) {
536 		if (np) {
537 			pdata = lp55xx_of_populate_pdata(&client->dev, np,
538 							 chip);
539 			if (IS_ERR(pdata))
540 				return PTR_ERR(pdata);
541 		} else {
542 			dev_err(&client->dev, "no platform data\n");
543 			return -EINVAL;
544 		}
545 	}
546 
547 	led = devm_kcalloc(&client->dev,
548 			pdata->num_channels, sizeof(*led), GFP_KERNEL);
549 	if (!led)
550 		return -ENOMEM;
551 
552 	chip->cl = client;
553 	chip->pdata = pdata;
554 
555 	mutex_init(&chip->lock);
556 
557 	i2c_set_clientdata(client, led);
558 
559 	ret = lp55xx_init_device(chip);
560 	if (ret)
561 		goto err_init;
562 
563 	dev_info(&client->dev, "%s programmable led chip found\n", id->name);
564 
565 	ret = lp55xx_register_leds(led, chip);
566 	if (ret)
567 		goto err_out;
568 
569 	ret = lp55xx_register_sysfs(chip);
570 	if (ret) {
571 		dev_err(&client->dev, "registering sysfs failed\n");
572 		goto err_out;
573 	}
574 
575 	return 0;
576 
577 err_out:
578 	lp55xx_deinit_device(chip);
579 err_init:
580 	return ret;
581 }
582 
583 static void lp5521_remove(struct i2c_client *client)
584 {
585 	struct lp55xx_led *led = i2c_get_clientdata(client);
586 	struct lp55xx_chip *chip = led->chip;
587 
588 	lp5521_stop_all_engines(chip);
589 	lp55xx_unregister_sysfs(chip);
590 	lp55xx_deinit_device(chip);
591 }
592 
593 static const struct i2c_device_id lp5521_id[] = {
594 	{ "lp5521", 0 }, /* Three channel chip */
595 	{ }
596 };
597 MODULE_DEVICE_TABLE(i2c, lp5521_id);
598 
599 static const struct of_device_id of_lp5521_leds_match[] = {
600 	{ .compatible = "national,lp5521", },
601 	{},
602 };
603 
604 MODULE_DEVICE_TABLE(of, of_lp5521_leds_match);
605 
606 static struct i2c_driver lp5521_driver = {
607 	.driver = {
608 		.name	= "lp5521",
609 		.of_match_table = of_lp5521_leds_match,
610 	},
611 	.probe		= lp5521_probe,
612 	.remove		= lp5521_remove,
613 	.id_table	= lp5521_id,
614 };
615 
616 module_i2c_driver(lp5521_driver);
617 
618 MODULE_AUTHOR("Mathias Nyman, Yuri Zaporozhets, Samu Onkalo");
619 MODULE_AUTHOR("Milo Kim <milo.kim@ti.com>");
620 MODULE_DESCRIPTION("LP5521 LED engine");
621 MODULE_LICENSE("GPL v2");
622