xref: /openbmc/linux/drivers/leds/leds-lp5521.c (revision 9ce7cb170f97f83a78dc948bf7d25690f15e1328)
1 /*
2  * LP5521 LED chip driver.
3  *
4  * Copyright (C) 2010 Nokia Corporation
5  *
6  * Contact: Samu Onkalo <samu.p.onkalo@nokia.com>
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * version 2 as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA
21  */
22 
23 #include <linux/module.h>
24 #include <linux/init.h>
25 #include <linux/i2c.h>
26 #include <linux/mutex.h>
27 #include <linux/gpio.h>
28 #include <linux/interrupt.h>
29 #include <linux/delay.h>
30 #include <linux/ctype.h>
31 #include <linux/spinlock.h>
32 #include <linux/wait.h>
33 #include <linux/leds.h>
34 #include <linux/leds-lp5521.h>
35 #include <linux/workqueue.h>
36 #include <linux/slab.h>
37 #include <linux/platform_data/leds-lp55xx.h>
38 #include <linux/firmware.h>
39 
40 #include "leds-lp55xx-common.h"
41 
42 #define LP5521_PROGRAM_LENGTH		32	/* in bytes */
43 
44 #define LP5521_MAX_LEDS			3	/* Maximum number of LEDs */
45 #define LP5521_MAX_ENGINES		3	/* Maximum number of engines */
46 
47 #define LP5521_ENG_MASK_BASE		0x30	/* 00110000 */
48 #define LP5521_ENG_STATUS_MASK		0x07	/* 00000111 */
49 
50 #define LP5521_CMD_LOAD			0x15	/* 00010101 */
51 #define LP5521_CMD_RUN			0x2a	/* 00101010 */
52 #define LP5521_CMD_DIRECT		0x3f	/* 00111111 */
53 #define LP5521_CMD_DISABLED		0x00	/* 00000000 */
54 
55 /* Registers */
56 #define LP5521_REG_ENABLE		0x00
57 #define LP5521_REG_OP_MODE		0x01
58 #define LP5521_REG_R_PWM		0x02
59 #define LP5521_REG_G_PWM		0x03
60 #define LP5521_REG_B_PWM		0x04
61 #define LP5521_REG_R_CURRENT		0x05
62 #define LP5521_REG_G_CURRENT		0x06
63 #define LP5521_REG_B_CURRENT		0x07
64 #define LP5521_REG_CONFIG		0x08
65 #define LP5521_REG_R_CHANNEL_PC		0x09
66 #define LP5521_REG_G_CHANNEL_PC		0x0A
67 #define LP5521_REG_B_CHANNEL_PC		0x0B
68 #define LP5521_REG_STATUS		0x0C
69 #define LP5521_REG_RESET		0x0D
70 #define LP5521_REG_GPO			0x0E
71 #define LP5521_REG_R_PROG_MEM		0x10
72 #define LP5521_REG_G_PROG_MEM		0x30
73 #define LP5521_REG_B_PROG_MEM		0x50
74 
75 #define LP5521_PROG_MEM_BASE		LP5521_REG_R_PROG_MEM
76 #define LP5521_PROG_MEM_SIZE		0x20
77 
78 /* Base register to set LED current */
79 #define LP5521_REG_LED_CURRENT_BASE	LP5521_REG_R_CURRENT
80 
81 /* Base register to set the brightness */
82 #define LP5521_REG_LED_PWM_BASE		LP5521_REG_R_PWM
83 
84 /* Bits in ENABLE register */
85 #define LP5521_MASTER_ENABLE		0x40	/* Chip master enable */
86 #define LP5521_LOGARITHMIC_PWM		0x80	/* Logarithmic PWM adjustment */
87 #define LP5521_EXEC_RUN			0x2A
88 #define LP5521_ENABLE_DEFAULT	\
89 	(LP5521_MASTER_ENABLE | LP5521_LOGARITHMIC_PWM)
90 #define LP5521_ENABLE_RUN_PROGRAM	\
91 	(LP5521_ENABLE_DEFAULT | LP5521_EXEC_RUN)
92 
93 /* Status */
94 #define LP5521_EXT_CLK_USED		0x08
95 
96 /* default R channel current register value */
97 #define LP5521_REG_R_CURR_DEFAULT	0xAF
98 
99 /* Pattern Mode */
100 #define PATTERN_OFF	0
101 
102 /* Reset register value */
103 #define LP5521_RESET			0xFF
104 
105 /* Program Memory Operations */
106 #define LP5521_MODE_R_M			0x30	/* Operation Mode Register */
107 #define LP5521_MODE_G_M			0x0C
108 #define LP5521_MODE_B_M			0x03
109 #define LP5521_LOAD_R			0x10
110 #define LP5521_LOAD_G			0x04
111 #define LP5521_LOAD_B			0x01
112 
113 #define LP5521_R_IS_LOADING(mode)	\
114 	((mode & LP5521_MODE_R_M) == LP5521_LOAD_R)
115 #define LP5521_G_IS_LOADING(mode)	\
116 	((mode & LP5521_MODE_G_M) == LP5521_LOAD_G)
117 #define LP5521_B_IS_LOADING(mode)	\
118 	((mode & LP5521_MODE_B_M) == LP5521_LOAD_B)
119 
120 #define LP5521_EXEC_R_M			0x30	/* Enable Register */
121 #define LP5521_EXEC_G_M			0x0C
122 #define LP5521_EXEC_B_M			0x03
123 #define LP5521_EXEC_M			0x3F
124 #define LP5521_RUN_R			0x20
125 #define LP5521_RUN_G			0x08
126 #define LP5521_RUN_B			0x02
127 
128 struct lp5521_led {
129 	int			id;
130 	u8			chan_nr;
131 	u8			led_current;
132 	u8			max_current;
133 	struct led_classdev	cdev;
134 	struct work_struct	brightness_work;
135 	u8			brightness;
136 };
137 
138 struct lp5521_chip {
139 	struct lp5521_platform_data *pdata;
140 	struct mutex		lock; /* Serialize control */
141 	struct i2c_client	*client;
142 	struct lp5521_led	leds[LP5521_MAX_LEDS];
143 	u8			num_channels;
144 	u8			num_leds;
145 };
146 
147 static inline void lp5521_wait_opmode_done(void)
148 {
149 	/* operation mode change needs to be longer than 153 us */
150 	usleep_range(200, 300);
151 }
152 
153 static inline void lp5521_wait_enable_done(void)
154 {
155 	/* it takes more 488 us to update ENABLE register */
156 	usleep_range(500, 600);
157 }
158 
159 static void lp5521_set_led_current(struct lp55xx_led *led, u8 led_current)
160 {
161 	led->led_current = led_current;
162 	lp55xx_write(led->chip, LP5521_REG_LED_CURRENT_BASE + led->chan_nr,
163 		led_current);
164 }
165 
166 static inline int lp5521_write(struct i2c_client *client, u8 reg, u8 value)
167 {
168 	return i2c_smbus_write_byte_data(client, reg, value);
169 }
170 
171 static int lp5521_read(struct i2c_client *client, u8 reg, u8 *buf)
172 {
173 	s32 ret;
174 
175 	ret = i2c_smbus_read_byte_data(client, reg);
176 	if (ret < 0)
177 		return ret;
178 
179 	*buf = ret;
180 	return 0;
181 }
182 
183 static void lp5521_load_engine(struct lp55xx_chip *chip)
184 {
185 	enum lp55xx_engine_index idx = chip->engine_idx;
186 	u8 mask[] = {
187 		[LP55XX_ENGINE_1] = LP5521_MODE_R_M,
188 		[LP55XX_ENGINE_2] = LP5521_MODE_G_M,
189 		[LP55XX_ENGINE_3] = LP5521_MODE_B_M,
190 	};
191 
192 	u8 val[] = {
193 		[LP55XX_ENGINE_1] = LP5521_LOAD_R,
194 		[LP55XX_ENGINE_2] = LP5521_LOAD_G,
195 		[LP55XX_ENGINE_3] = LP5521_LOAD_B,
196 	};
197 
198 	lp55xx_update_bits(chip, LP5521_REG_OP_MODE, mask[idx], val[idx]);
199 
200 	lp5521_wait_opmode_done();
201 }
202 
203 static void lp5521_stop_engine(struct lp55xx_chip *chip)
204 {
205 	lp55xx_write(chip, LP5521_REG_OP_MODE, 0);
206 	lp5521_wait_opmode_done();
207 }
208 
209 static void lp5521_run_engine(struct lp55xx_chip *chip, bool start)
210 {
211 	int ret;
212 	u8 mode;
213 	u8 exec;
214 
215 	/* stop engine */
216 	if (!start) {
217 		lp5521_stop_engine(chip);
218 		lp55xx_write(chip, LP5521_REG_OP_MODE, LP5521_CMD_DIRECT);
219 		lp5521_wait_opmode_done();
220 		return;
221 	}
222 
223 	/*
224 	 * To run the engine,
225 	 * operation mode and enable register should updated at the same time
226 	 */
227 
228 	ret = lp55xx_read(chip, LP5521_REG_OP_MODE, &mode);
229 	if (ret)
230 		return;
231 
232 	ret = lp55xx_read(chip, LP5521_REG_ENABLE, &exec);
233 	if (ret)
234 		return;
235 
236 	/* change operation mode to RUN only when each engine is loading */
237 	if (LP5521_R_IS_LOADING(mode)) {
238 		mode = (mode & ~LP5521_MODE_R_M) | LP5521_RUN_R;
239 		exec = (exec & ~LP5521_EXEC_R_M) | LP5521_RUN_R;
240 	}
241 
242 	if (LP5521_G_IS_LOADING(mode)) {
243 		mode = (mode & ~LP5521_MODE_G_M) | LP5521_RUN_G;
244 		exec = (exec & ~LP5521_EXEC_G_M) | LP5521_RUN_G;
245 	}
246 
247 	if (LP5521_B_IS_LOADING(mode)) {
248 		mode = (mode & ~LP5521_MODE_B_M) | LP5521_RUN_B;
249 		exec = (exec & ~LP5521_EXEC_B_M) | LP5521_RUN_B;
250 	}
251 
252 	lp55xx_write(chip, LP5521_REG_OP_MODE, mode);
253 	lp5521_wait_opmode_done();
254 
255 	lp55xx_update_bits(chip, LP5521_REG_ENABLE, LP5521_EXEC_M, exec);
256 	lp5521_wait_enable_done();
257 }
258 
259 static int lp5521_update_program_memory(struct lp55xx_chip *chip,
260 					const u8 *data, size_t size)
261 {
262 	enum lp55xx_engine_index idx = chip->engine_idx;
263 	u8 pattern[LP5521_PROGRAM_LENGTH] = {0};
264 	u8 addr[] = {
265 		[LP55XX_ENGINE_1] = LP5521_REG_R_PROG_MEM,
266 		[LP55XX_ENGINE_2] = LP5521_REG_G_PROG_MEM,
267 		[LP55XX_ENGINE_3] = LP5521_REG_B_PROG_MEM,
268 	};
269 	unsigned cmd;
270 	char c[3];
271 	int program_size;
272 	int nrchars;
273 	int offset = 0;
274 	int ret;
275 	int i;
276 
277 	/* clear program memory before updating */
278 	for (i = 0; i < LP5521_PROGRAM_LENGTH; i++)
279 		lp55xx_write(chip, addr[idx] + i, 0);
280 
281 	i = 0;
282 	while ((offset < size - 1) && (i < LP5521_PROGRAM_LENGTH)) {
283 		/* separate sscanfs because length is working only for %s */
284 		ret = sscanf(data + offset, "%2s%n ", c, &nrchars);
285 		if (ret != 1)
286 			goto err;
287 
288 		ret = sscanf(c, "%2x", &cmd);
289 		if (ret != 1)
290 			goto err;
291 
292 		pattern[i] = (u8)cmd;
293 		offset += nrchars;
294 		i++;
295 	}
296 
297 	/* Each instruction is 16bit long. Check that length is even */
298 	if (i % 2)
299 		goto err;
300 
301 	program_size = i;
302 	for (i = 0; i < program_size; i++)
303 		lp55xx_write(chip, addr[idx] + i, pattern[i]);
304 
305 	return 0;
306 
307 err:
308 	dev_err(&chip->cl->dev, "wrong pattern format\n");
309 	return -EINVAL;
310 }
311 
312 static void lp5521_firmware_loaded(struct lp55xx_chip *chip)
313 {
314 	const struct firmware *fw = chip->fw;
315 
316 	if (fw->size > LP5521_PROGRAM_LENGTH) {
317 		dev_err(&chip->cl->dev, "firmware data size overflow: %zu\n",
318 			fw->size);
319 		return;
320 	}
321 
322 	/*
323 	 * Program momery sequence
324 	 *  1) set engine mode to "LOAD"
325 	 *  2) write firmware data into program memory
326 	 */
327 
328 	lp5521_load_engine(chip);
329 	lp5521_update_program_memory(chip, fw->data, fw->size);
330 }
331 
332 static int lp5521_post_init_device(struct lp55xx_chip *chip)
333 {
334 	int ret;
335 	u8 val;
336 
337 	/*
338 	 * Make sure that the chip is reset by reading back the r channel
339 	 * current reg. This is dummy read is required on some platforms -
340 	 * otherwise further access to the R G B channels in the
341 	 * LP5521_REG_ENABLE register will not have any effect - strange!
342 	 */
343 	ret = lp55xx_read(chip, LP5521_REG_R_CURRENT, &val);
344 	if (ret) {
345 		dev_err(&chip->cl->dev, "error in resetting chip\n");
346 		return ret;
347 	}
348 	if (val != LP5521_REG_R_CURR_DEFAULT) {
349 		dev_err(&chip->cl->dev,
350 			"unexpected data in register (expected 0x%x got 0x%x)\n",
351 			LP5521_REG_R_CURR_DEFAULT, val);
352 		ret = -EINVAL;
353 		return ret;
354 	}
355 	usleep_range(10000, 20000);
356 
357 	/* Set all PWMs to direct control mode */
358 	ret = lp55xx_write(chip, LP5521_REG_OP_MODE, LP5521_CMD_DIRECT);
359 
360 	val = chip->pdata->update_config ?
361 		: (LP5521_PWRSAVE_EN | LP5521_CP_MODE_AUTO | LP5521_R_TO_BATT);
362 	ret = lp55xx_write(chip, LP5521_REG_CONFIG, val);
363 	if (ret)
364 		return ret;
365 
366 	/* Initialize all channels PWM to zero -> leds off */
367 	lp55xx_write(chip, LP5521_REG_R_PWM, 0);
368 	lp55xx_write(chip, LP5521_REG_G_PWM, 0);
369 	lp55xx_write(chip, LP5521_REG_B_PWM, 0);
370 
371 	/* Set engines are set to run state when OP_MODE enables engines */
372 	ret = lp55xx_write(chip, LP5521_REG_ENABLE, LP5521_ENABLE_RUN_PROGRAM);
373 	if (ret)
374 		return ret;
375 
376 	lp5521_wait_enable_done();
377 
378 	return 0;
379 }
380 
381 static int lp5521_run_selftest(struct lp5521_chip *chip, char *buf)
382 {
383 	int ret;
384 	u8 status;
385 
386 	ret = lp5521_read(chip->client, LP5521_REG_STATUS, &status);
387 	if (ret < 0)
388 		return ret;
389 
390 	/* Check that ext clock is really in use if requested */
391 	if (chip->pdata && chip->pdata->clock_mode == LP5521_CLOCK_EXT)
392 		if  ((status & LP5521_EXT_CLK_USED) == 0)
393 			return -EIO;
394 	return 0;
395 }
396 
397 static void lp5521_led_brightness_work(struct work_struct *work)
398 {
399 	struct lp55xx_led *led = container_of(work, struct lp55xx_led,
400 					      brightness_work);
401 	struct lp55xx_chip *chip = led->chip;
402 
403 	mutex_lock(&chip->lock);
404 	lp55xx_write(chip, LP5521_REG_LED_PWM_BASE + led->chan_nr,
405 		led->brightness);
406 	mutex_unlock(&chip->lock);
407 }
408 
409 static ssize_t lp5521_selftest(struct device *dev,
410 			       struct device_attribute *attr,
411 			       char *buf)
412 {
413 	struct i2c_client *client = to_i2c_client(dev);
414 	struct lp5521_chip *chip = i2c_get_clientdata(client);
415 	int ret;
416 
417 	mutex_lock(&chip->lock);
418 	ret = lp5521_run_selftest(chip, buf);
419 	mutex_unlock(&chip->lock);
420 	return sprintf(buf, "%s\n", ret ? "FAIL" : "OK");
421 }
422 
423 static void lp5521_clear_program_memory(struct i2c_client *cl)
424 {
425 	int i;
426 	u8 rgb_mem[] = {
427 		LP5521_REG_R_PROG_MEM,
428 		LP5521_REG_G_PROG_MEM,
429 		LP5521_REG_B_PROG_MEM,
430 	};
431 
432 	for (i = 0; i < ARRAY_SIZE(rgb_mem); i++) {
433 		lp5521_write(cl, rgb_mem[i], 0);
434 		lp5521_write(cl, rgb_mem[i] + 1, 0);
435 	}
436 }
437 
438 static void lp5521_write_program_memory(struct i2c_client *cl,
439 				u8 base, u8 *rgb, int size)
440 {
441 	int i;
442 
443 	if (!rgb || size <= 0)
444 		return;
445 
446 	for (i = 0; i < size; i++)
447 		lp5521_write(cl, base + i, *(rgb + i));
448 
449 	lp5521_write(cl, base + i, 0);
450 	lp5521_write(cl, base + i + 1, 0);
451 }
452 
453 static inline struct lp5521_led_pattern *lp5521_get_pattern
454 					(struct lp5521_chip *chip, u8 offset)
455 {
456 	struct lp5521_led_pattern *ptn;
457 	ptn = chip->pdata->patterns + (offset - 1);
458 	return ptn;
459 }
460 
461 static void lp5521_run_led_pattern(int mode, struct lp5521_chip *chip)
462 {
463 	struct lp5521_led_pattern *ptn;
464 	struct i2c_client *cl = chip->client;
465 	int num_patterns = chip->pdata->num_patterns;
466 
467 	if (mode > num_patterns || !(chip->pdata->patterns))
468 		return;
469 
470 	if (mode == PATTERN_OFF) {
471 		lp5521_write(cl, LP5521_REG_ENABLE, LP5521_ENABLE_DEFAULT);
472 		usleep_range(1000, 2000);
473 		lp5521_write(cl, LP5521_REG_OP_MODE, LP5521_CMD_DIRECT);
474 	} else {
475 		ptn = lp5521_get_pattern(chip, mode);
476 		if (!ptn)
477 			return;
478 
479 		lp5521_write(cl, LP5521_REG_OP_MODE, LP5521_CMD_LOAD);
480 		usleep_range(1000, 2000);
481 
482 		lp5521_clear_program_memory(cl);
483 
484 		lp5521_write_program_memory(cl, LP5521_REG_R_PROG_MEM,
485 					ptn->r, ptn->size_r);
486 		lp5521_write_program_memory(cl, LP5521_REG_G_PROG_MEM,
487 					ptn->g, ptn->size_g);
488 		lp5521_write_program_memory(cl, LP5521_REG_B_PROG_MEM,
489 					ptn->b, ptn->size_b);
490 
491 		lp5521_write(cl, LP5521_REG_OP_MODE, LP5521_CMD_RUN);
492 		usleep_range(1000, 2000);
493 		lp5521_write(cl, LP5521_REG_ENABLE, LP5521_ENABLE_RUN_PROGRAM);
494 	}
495 }
496 
497 /* device attributes */
498 static DEVICE_ATTR(selftest, S_IRUGO, lp5521_selftest, NULL);
499 
500 static struct attribute *lp5521_attributes[] = {
501 	&dev_attr_selftest.attr,
502 	NULL
503 };
504 
505 static const struct attribute_group lp5521_group = {
506 	.attrs = lp5521_attributes,
507 };
508 
509 static int lp5521_register_sysfs(struct i2c_client *client)
510 {
511 	struct device *dev = &client->dev;
512 	return sysfs_create_group(&dev->kobj, &lp5521_group);
513 }
514 
515 static void lp5521_unregister_sysfs(struct i2c_client *client)
516 {
517 	struct device *dev = &client->dev;
518 
519 	sysfs_remove_group(&dev->kobj, &lp5521_group);
520 }
521 
522 /* Chip specific configurations */
523 static struct lp55xx_device_config lp5521_cfg = {
524 	.reset = {
525 		.addr = LP5521_REG_RESET,
526 		.val  = LP5521_RESET,
527 	},
528 	.enable = {
529 		.addr = LP5521_REG_ENABLE,
530 		.val  = LP5521_ENABLE_DEFAULT,
531 	},
532 	.max_channel  = LP5521_MAX_LEDS,
533 	.post_init_device   = lp5521_post_init_device,
534 	.brightness_work_fn = lp5521_led_brightness_work,
535 	.set_led_current    = lp5521_set_led_current,
536 	.firmware_cb        = lp5521_firmware_loaded,
537 	.run_engine         = lp5521_run_engine,
538 };
539 
540 static int lp5521_probe(struct i2c_client *client,
541 			const struct i2c_device_id *id)
542 {
543 	int ret;
544 	struct lp55xx_chip *chip;
545 	struct lp55xx_led *led;
546 	struct lp55xx_platform_data *pdata = client->dev.platform_data;
547 
548 	if (!pdata) {
549 		dev_err(&client->dev, "no platform data\n");
550 		return -EINVAL;
551 	}
552 
553 	chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
554 	if (!chip)
555 		return -ENOMEM;
556 
557 	led = devm_kzalloc(&client->dev,
558 			sizeof(*led) * pdata->num_channels, GFP_KERNEL);
559 	if (!led)
560 		return -ENOMEM;
561 
562 	chip->cl = client;
563 	chip->pdata = pdata;
564 	chip->cfg = &lp5521_cfg;
565 
566 	mutex_init(&chip->lock);
567 
568 	i2c_set_clientdata(client, led);
569 
570 	ret = lp55xx_init_device(chip);
571 	if (ret)
572 		goto err_init;
573 
574 	dev_info(&client->dev, "%s programmable led chip found\n", id->name);
575 
576 	ret = lp55xx_register_leds(led, chip);
577 	if (ret)
578 		goto err_register_leds;
579 
580 	ret = lp5521_register_sysfs(client);
581 	if (ret) {
582 		dev_err(&client->dev, "registering sysfs failed\n");
583 		goto fail2;
584 	}
585 	return ret;
586 fail2:
587 	lp55xx_unregister_leds(led, chip);
588 err_register_leds:
589 	lp55xx_deinit_device(chip);
590 err_init:
591 	return ret;
592 }
593 
594 static int lp5521_remove(struct i2c_client *client)
595 {
596 	struct lp5521_chip *old_chip = i2c_get_clientdata(client);
597 	struct lp55xx_led *led = i2c_get_clientdata(client);
598 	struct lp55xx_chip *chip = led->chip;
599 
600 	lp5521_run_led_pattern(PATTERN_OFF, old_chip);
601 	lp5521_unregister_sysfs(client);
602 
603 	lp55xx_unregister_leds(led, chip);
604 	lp55xx_deinit_device(chip);
605 
606 	return 0;
607 }
608 
609 static const struct i2c_device_id lp5521_id[] = {
610 	{ "lp5521", 0 }, /* Three channel chip */
611 	{ }
612 };
613 MODULE_DEVICE_TABLE(i2c, lp5521_id);
614 
615 static struct i2c_driver lp5521_driver = {
616 	.driver = {
617 		.name	= "lp5521",
618 	},
619 	.probe		= lp5521_probe,
620 	.remove		= lp5521_remove,
621 	.id_table	= lp5521_id,
622 };
623 
624 module_i2c_driver(lp5521_driver);
625 
626 MODULE_AUTHOR("Mathias Nyman, Yuri Zaporozhets, Samu Onkalo");
627 MODULE_DESCRIPTION("LP5521 LED engine");
628 MODULE_LICENSE("GPL v2");
629