2 * LP5521 LED chip driver.
4 * Copyright (C) 2010 Nokia Corporation
6 * Contact: Samu Onkalo <samu.p.onkalo@nokia.com>
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.
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.
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
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>
40 #include "leds-lp55xx-common.h"
42 #define LP5521_PROGRAM_LENGTH 32 /* in bytes */
44 #define LP5521_MAX_LEDS 3 /* Maximum number of LEDs */
45 #define LP5521_MAX_ENGINES 3 /* Maximum number of engines */
47 #define LP5521_ENG_MASK_BASE 0x30 /* 00110000 */
48 #define LP5521_ENG_STATUS_MASK 0x07 /* 00000111 */
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 */
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
75 #define LP5521_PROG_MEM_BASE LP5521_REG_R_PROG_MEM
76 #define LP5521_PROG_MEM_SIZE 0x20
78 /* Base register to set LED current */
79 #define LP5521_REG_LED_CURRENT_BASE LP5521_REG_R_CURRENT
81 /* Base register to set the brightness */
82 #define LP5521_REG_LED_PWM_BASE LP5521_REG_R_PWM
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)
94 #define LP5521_EXT_CLK_USED 0x08
96 /* default R channel current register value */
97 #define LP5521_REG_R_CURR_DEFAULT 0xAF
100 #define PATTERN_OFF 0
102 /* Reset register value */
103 #define LP5521_RESET 0xFF
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
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)
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
133 struct led_classdev cdev;
134 struct work_struct brightness_work;
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];
147 static inline void lp5521_wait_opmode_done(void)
149 /* operation mode change needs to be longer than 153 us */
150 usleep_range(200, 300);
153 static inline void lp5521_wait_enable_done(void)
155 /* it takes more 488 us to update ENABLE register */
156 usleep_range(500, 600);
159 static void lp5521_set_led_current(struct lp55xx_led *led, u8 led_current)
161 led->led_current = led_current;
162 lp55xx_write(led->chip, LP5521_REG_LED_CURRENT_BASE + led->chan_nr,
166 static inline int lp5521_write(struct i2c_client *client, u8 reg, u8 value)
168 return i2c_smbus_write_byte_data(client, reg, value);
171 static int lp5521_read(struct i2c_client *client, u8 reg, u8 *buf)
175 ret = i2c_smbus_read_byte_data(client, reg);
183 static void lp5521_load_engine(struct lp55xx_chip *chip)
185 enum lp55xx_engine_index idx = chip->engine_idx;
187 [LP55XX_ENGINE_1] = LP5521_MODE_R_M,
188 [LP55XX_ENGINE_2] = LP5521_MODE_G_M,
189 [LP55XX_ENGINE_3] = LP5521_MODE_B_M,
193 [LP55XX_ENGINE_1] = LP5521_LOAD_R,
194 [LP55XX_ENGINE_2] = LP5521_LOAD_G,
195 [LP55XX_ENGINE_3] = LP5521_LOAD_B,
198 lp55xx_update_bits(chip, LP5521_REG_OP_MODE, mask[idx], val[idx]);
200 lp5521_wait_opmode_done();
203 static void lp5521_stop_engine(struct lp55xx_chip *chip)
205 lp55xx_write(chip, LP5521_REG_OP_MODE, 0);
206 lp5521_wait_opmode_done();
209 static void lp5521_run_engine(struct lp55xx_chip *chip, bool start)
217 lp5521_stop_engine(chip);
218 lp55xx_write(chip, LP5521_REG_OP_MODE, LP5521_CMD_DIRECT);
219 lp5521_wait_opmode_done();
225 * operation mode and enable register should updated at the same time
228 ret = lp55xx_read(chip, LP5521_REG_OP_MODE, &mode);
232 ret = lp55xx_read(chip, LP5521_REG_ENABLE, &exec);
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;
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;
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;
252 lp55xx_write(chip, LP5521_REG_OP_MODE, mode);
253 lp5521_wait_opmode_done();
255 lp55xx_update_bits(chip, LP5521_REG_ENABLE, LP5521_EXEC_M, exec);
256 lp5521_wait_enable_done();
259 static int lp5521_update_program_memory(struct lp55xx_chip *chip,
260 const u8 *data, size_t size)
262 enum lp55xx_engine_index idx = chip->engine_idx;
263 u8 pattern[LP5521_PROGRAM_LENGTH] = {0};
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,
277 /* clear program memory before updating */
278 for (i = 0; i < LP5521_PROGRAM_LENGTH; i++)
279 lp55xx_write(chip, addr[idx] + 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);
288 ret = sscanf(c, "%2x", &cmd);
292 pattern[i] = (u8)cmd;
297 /* Each instruction is 16bit long. Check that length is even */
302 for (i = 0; i < program_size; i++)
303 lp55xx_write(chip, addr[idx] + i, pattern[i]);
308 dev_err(&chip->cl->dev, "wrong pattern format\n");
312 static void lp5521_firmware_loaded(struct lp55xx_chip *chip)
314 const struct firmware *fw = chip->fw;
316 if (fw->size > LP5521_PROGRAM_LENGTH) {
317 dev_err(&chip->cl->dev, "firmware data size overflow: %zu\n",
323 * Program momery sequence
324 * 1) set engine mode to "LOAD"
325 * 2) write firmware data into program memory
328 lp5521_load_engine(chip);
329 lp5521_update_program_memory(chip, fw->data, fw->size);
332 static int lp5521_post_init_device(struct lp55xx_chip *chip)
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!
343 ret = lp55xx_read(chip, LP5521_REG_R_CURRENT, &val);
345 dev_err(&chip->cl->dev, "error in resetting chip\n");
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);
355 usleep_range(10000, 20000);
357 /* Set all PWMs to direct control mode */
358 ret = lp55xx_write(chip, LP5521_REG_OP_MODE, LP5521_CMD_DIRECT);
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);
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);
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);
376 lp5521_wait_enable_done();
381 static int lp5521_run_selftest(struct lp5521_chip *chip, char *buf)
386 ret = lp5521_read(chip->client, LP5521_REG_STATUS, &status);
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)
397 static void lp5521_led_brightness_work(struct work_struct *work)
399 struct lp55xx_led *led = container_of(work, struct lp55xx_led,
401 struct lp55xx_chip *chip = led->chip;
403 mutex_lock(&chip->lock);
404 lp55xx_write(chip, LP5521_REG_LED_PWM_BASE + led->chan_nr,
406 mutex_unlock(&chip->lock);
409 static ssize_t lp5521_selftest(struct device *dev,
410 struct device_attribute *attr,
413 struct i2c_client *client = to_i2c_client(dev);
414 struct lp5521_chip *chip = i2c_get_clientdata(client);
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");
423 static void lp5521_clear_program_memory(struct i2c_client *cl)
427 LP5521_REG_R_PROG_MEM,
428 LP5521_REG_G_PROG_MEM,
429 LP5521_REG_B_PROG_MEM,
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);
438 static void lp5521_write_program_memory(struct i2c_client *cl,
439 u8 base, u8 *rgb, int size)
443 if (!rgb || size <= 0)
446 for (i = 0; i < size; i++)
447 lp5521_write(cl, base + i, *(rgb + i));
449 lp5521_write(cl, base + i, 0);
450 lp5521_write(cl, base + i + 1, 0);
453 static inline struct lp5521_led_pattern *lp5521_get_pattern
454 (struct lp5521_chip *chip, u8 offset)
456 struct lp5521_led_pattern *ptn;
457 ptn = chip->pdata->patterns + (offset - 1);
461 static void lp5521_run_led_pattern(int mode, struct lp5521_chip *chip)
463 struct lp5521_led_pattern *ptn;
464 struct i2c_client *cl = chip->client;
465 int num_patterns = chip->pdata->num_patterns;
467 if (mode > num_patterns || !(chip->pdata->patterns))
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);
475 ptn = lp5521_get_pattern(chip, mode);
479 lp5521_write(cl, LP5521_REG_OP_MODE, LP5521_CMD_LOAD);
480 usleep_range(1000, 2000);
482 lp5521_clear_program_memory(cl);
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);
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);
497 /* device attributes */
498 static DEVICE_ATTR(selftest, S_IRUGO, lp5521_selftest, NULL);
500 static struct attribute *lp5521_attributes[] = {
501 &dev_attr_selftest.attr,
505 static const struct attribute_group lp5521_group = {
506 .attrs = lp5521_attributes,
509 static int lp5521_register_sysfs(struct i2c_client *client)
511 struct device *dev = &client->dev;
512 return sysfs_create_group(&dev->kobj, &lp5521_group);
515 static void lp5521_unregister_sysfs(struct i2c_client *client)
517 struct device *dev = &client->dev;
519 sysfs_remove_group(&dev->kobj, &lp5521_group);
522 /* Chip specific configurations */
523 static struct lp55xx_device_config lp5521_cfg = {
525 .addr = LP5521_REG_RESET,
529 .addr = LP5521_REG_ENABLE,
530 .val = LP5521_ENABLE_DEFAULT,
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,
540 static int lp5521_probe(struct i2c_client *client,
541 const struct i2c_device_id *id)
544 struct lp55xx_chip *chip;
545 struct lp55xx_led *led;
546 struct lp55xx_platform_data *pdata = client->dev.platform_data;
549 dev_err(&client->dev, "no platform data\n");
553 chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
557 led = devm_kzalloc(&client->dev,
558 sizeof(*led) * pdata->num_channels, GFP_KERNEL);
564 chip->cfg = &lp5521_cfg;
566 mutex_init(&chip->lock);
568 i2c_set_clientdata(client, led);
570 ret = lp55xx_init_device(chip);
574 dev_info(&client->dev, "%s programmable led chip found\n", id->name);
576 ret = lp55xx_register_leds(led, chip);
578 goto err_register_leds;
580 ret = lp5521_register_sysfs(client);
582 dev_err(&client->dev, "registering sysfs failed\n");
587 lp55xx_unregister_leds(led, chip);
589 lp55xx_deinit_device(chip);
594 static int lp5521_remove(struct i2c_client *client)
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;
600 lp5521_run_led_pattern(PATTERN_OFF, old_chip);
601 lp5521_unregister_sysfs(client);
603 lp55xx_unregister_leds(led, chip);
604 lp55xx_deinit_device(chip);
609 static const struct i2c_device_id lp5521_id[] = {
610 { "lp5521", 0 }, /* Three channel chip */
613 MODULE_DEVICE_TABLE(i2c, lp5521_id);
615 static struct i2c_driver lp5521_driver = {
619 .probe = lp5521_probe,
620 .remove = lp5521_remove,
621 .id_table = lp5521_id,
624 module_i2c_driver(lp5521_driver);
626 MODULE_AUTHOR("Mathias Nyman, Yuri Zaporozhets, Samu Onkalo");
627 MODULE_DESCRIPTION("LP5521 LED engine");
628 MODULE_LICENSE("GPL v2");