blob: 341a41030fd859aa3b7ce797dfad91f3c3f277e5 [file] [log] [blame]
Samu Onkalo500fe142010-11-11 14:05:22 -08001/*
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>
Milo(Woogyom) Kim6a0c9a42013-02-05 18:03:08 +090037#include <linux/platform_data/leds-lp55xx.h>
38
39#include "leds-lp55xx-common.h"
Samu Onkalo500fe142010-11-11 14:05:22 -080040
41#define LP5521_PROGRAM_LENGTH 32 /* in bytes */
42
43#define LP5521_MAX_LEDS 3 /* Maximum number of LEDs */
44#define LP5521_MAX_ENGINES 3 /* Maximum number of engines */
45
46#define LP5521_ENG_MASK_BASE 0x30 /* 00110000 */
47#define LP5521_ENG_STATUS_MASK 0x07 /* 00000111 */
48
49#define LP5521_CMD_LOAD 0x15 /* 00010101 */
50#define LP5521_CMD_RUN 0x2a /* 00101010 */
51#define LP5521_CMD_DIRECT 0x3f /* 00111111 */
52#define LP5521_CMD_DISABLED 0x00 /* 00000000 */
53
54/* Registers */
55#define LP5521_REG_ENABLE 0x00
56#define LP5521_REG_OP_MODE 0x01
57#define LP5521_REG_R_PWM 0x02
58#define LP5521_REG_G_PWM 0x03
59#define LP5521_REG_B_PWM 0x04
60#define LP5521_REG_R_CURRENT 0x05
61#define LP5521_REG_G_CURRENT 0x06
62#define LP5521_REG_B_CURRENT 0x07
63#define LP5521_REG_CONFIG 0x08
64#define LP5521_REG_R_CHANNEL_PC 0x09
65#define LP5521_REG_G_CHANNEL_PC 0x0A
66#define LP5521_REG_B_CHANNEL_PC 0x0B
67#define LP5521_REG_STATUS 0x0C
68#define LP5521_REG_RESET 0x0D
69#define LP5521_REG_GPO 0x0E
70#define LP5521_REG_R_PROG_MEM 0x10
71#define LP5521_REG_G_PROG_MEM 0x30
72#define LP5521_REG_B_PROG_MEM 0x50
73
74#define LP5521_PROG_MEM_BASE LP5521_REG_R_PROG_MEM
75#define LP5521_PROG_MEM_SIZE 0x20
76
77/* Base register to set LED current */
78#define LP5521_REG_LED_CURRENT_BASE LP5521_REG_R_CURRENT
79
80/* Base register to set the brightness */
81#define LP5521_REG_LED_PWM_BASE LP5521_REG_R_PWM
82
83/* Bits in ENABLE register */
84#define LP5521_MASTER_ENABLE 0x40 /* Chip master enable */
85#define LP5521_LOGARITHMIC_PWM 0x80 /* Logarithmic PWM adjustment */
86#define LP5521_EXEC_RUN 0x2A
Kim, Milo32a2f742012-03-23 15:02:09 -070087#define LP5521_ENABLE_DEFAULT \
88 (LP5521_MASTER_ENABLE | LP5521_LOGARITHMIC_PWM)
89#define LP5521_ENABLE_RUN_PROGRAM \
90 (LP5521_ENABLE_DEFAULT | LP5521_EXEC_RUN)
Samu Onkalo500fe142010-11-11 14:05:22 -080091
Samu Onkalo500fe142010-11-11 14:05:22 -080092/* Status */
93#define LP5521_EXT_CLK_USED 0x08
94
Srinidhi KASAGARb3c49c02011-10-31 17:12:24 -070095/* default R channel current register value */
96#define LP5521_REG_R_CURR_DEFAULT 0xAF
97
Kim, Milo011af7b2012-03-23 15:02:09 -070098/* Pattern Mode */
99#define PATTERN_OFF 0
100
Samu Onkalo500fe142010-11-11 14:05:22 -0800101struct lp5521_engine {
Samu Onkalo500fe142010-11-11 14:05:22 -0800102 int id;
103 u8 mode;
104 u8 prog_page;
105 u8 engine_mask;
106};
107
108struct lp5521_led {
109 int id;
110 u8 chan_nr;
111 u8 led_current;
112 u8 max_current;
113 struct led_classdev cdev;
114 struct work_struct brightness_work;
115 u8 brightness;
116};
117
118struct lp5521_chip {
119 struct lp5521_platform_data *pdata;
120 struct mutex lock; /* Serialize control */
121 struct i2c_client *client;
122 struct lp5521_engine engines[LP5521_MAX_ENGINES];
123 struct lp5521_led leds[LP5521_MAX_LEDS];
124 u8 num_channels;
125 u8 num_leds;
126};
127
Milo(Woogyom) Kim94482172013-02-05 18:03:55 +0900128static inline void lp5521_wait_enable_done(void)
129{
130 /* it takes more 488 us to update ENABLE register */
131 usleep_range(500, 600);
132}
133
Samu Onkalo9fdb18b2010-11-24 12:57:02 -0800134static inline struct lp5521_led *cdev_to_led(struct led_classdev *cdev)
135{
136 return container_of(cdev, struct lp5521_led, cdev);
137}
138
139static inline struct lp5521_chip *engine_to_lp5521(struct lp5521_engine *engine)
140{
141 return container_of(engine, struct lp5521_chip,
142 engines[engine->id - 1]);
143}
144
145static inline struct lp5521_chip *led_to_lp5521(struct lp5521_led *led)
146{
147 return container_of(led, struct lp5521_chip,
148 leds[led->id]);
149}
Samu Onkalo500fe142010-11-11 14:05:22 -0800150
151static void lp5521_led_brightness_work(struct work_struct *work);
152
153static inline int lp5521_write(struct i2c_client *client, u8 reg, u8 value)
154{
155 return i2c_smbus_write_byte_data(client, reg, value);
156}
157
158static int lp5521_read(struct i2c_client *client, u8 reg, u8 *buf)
159{
160 s32 ret;
161
162 ret = i2c_smbus_read_byte_data(client, reg);
163 if (ret < 0)
Sachin Kamat313e8b52012-11-20 01:59:00 -0800164 return ret;
Samu Onkalo500fe142010-11-11 14:05:22 -0800165
166 *buf = ret;
167 return 0;
168}
169
170static int lp5521_set_engine_mode(struct lp5521_engine *engine, u8 mode)
171{
172 struct lp5521_chip *chip = engine_to_lp5521(engine);
173 struct i2c_client *client = chip->client;
174 int ret;
175 u8 engine_state;
176
177 /* Only transition between RUN and DIRECT mode are handled here */
178 if (mode == LP5521_CMD_LOAD)
179 return 0;
180
181 if (mode == LP5521_CMD_DISABLED)
182 mode = LP5521_CMD_DIRECT;
183
184 ret = lp5521_read(client, LP5521_REG_OP_MODE, &engine_state);
Axel Linfa0ea0e2011-10-31 17:12:12 -0700185 if (ret < 0)
186 return ret;
Samu Onkalo500fe142010-11-11 14:05:22 -0800187
188 /* set mode only for this engine */
189 engine_state &= ~(engine->engine_mask);
190 mode &= engine->engine_mask;
191 engine_state |= mode;
Axel Linfa0ea0e2011-10-31 17:12:12 -0700192 return lp5521_write(client, LP5521_REG_OP_MODE, engine_state);
Samu Onkalo500fe142010-11-11 14:05:22 -0800193}
194
195static int lp5521_load_program(struct lp5521_engine *eng, const u8 *pattern)
196{
197 struct lp5521_chip *chip = engine_to_lp5521(eng);
198 struct i2c_client *client = chip->client;
199 int ret;
200 int addr;
201 u8 mode;
202
203 /* move current engine to direct mode and remember the state */
204 ret = lp5521_set_engine_mode(eng, LP5521_CMD_DIRECT);
Dan Carpenter5bc9ad72012-05-29 15:07:26 -0700205 if (ret)
206 return ret;
207
Samu Onkalo09c76b02010-11-24 12:57:03 -0800208 /* Mode change requires min 500 us delay. 1 - 2 ms with margin */
209 usleep_range(1000, 2000);
Dan Carpenter5bc9ad72012-05-29 15:07:26 -0700210 ret = lp5521_read(client, LP5521_REG_OP_MODE, &mode);
211 if (ret)
212 return ret;
Samu Onkalo500fe142010-11-11 14:05:22 -0800213
214 /* For loading, all the engines to load mode */
215 lp5521_write(client, LP5521_REG_OP_MODE, LP5521_CMD_DIRECT);
Samu Onkalo09c76b02010-11-24 12:57:03 -0800216 /* Mode change requires min 500 us delay. 1 - 2 ms with margin */
217 usleep_range(1000, 2000);
Samu Onkalo500fe142010-11-11 14:05:22 -0800218 lp5521_write(client, LP5521_REG_OP_MODE, LP5521_CMD_LOAD);
Samu Onkalo09c76b02010-11-24 12:57:03 -0800219 /* Mode change requires min 500 us delay. 1 - 2 ms with margin */
220 usleep_range(1000, 2000);
Samu Onkalo500fe142010-11-11 14:05:22 -0800221
222 addr = LP5521_PROG_MEM_BASE + eng->prog_page * LP5521_PROG_MEM_SIZE;
223 i2c_smbus_write_i2c_block_data(client,
224 addr,
225 LP5521_PROG_MEM_SIZE,
226 pattern);
227
Dan Carpenter5bc9ad72012-05-29 15:07:26 -0700228 return lp5521_write(client, LP5521_REG_OP_MODE, mode);
Samu Onkalo500fe142010-11-11 14:05:22 -0800229}
230
231static int lp5521_set_led_current(struct lp5521_chip *chip, int led, u8 curr)
232{
233 return lp5521_write(chip->client,
234 LP5521_REG_LED_CURRENT_BASE + chip->leds[led].chan_nr,
235 curr);
236}
237
Samu Onkalod4e7ad02011-01-12 16:59:19 -0800238static int lp5521_configure(struct i2c_client *client)
Samu Onkalo500fe142010-11-11 14:05:22 -0800239{
240 struct lp5521_chip *chip = i2c_get_clientdata(client);
241 int ret;
Kim, Milo3b49aac2012-03-23 15:02:08 -0700242 u8 cfg;
Milo(Woogyom) Kim94482172013-02-05 18:03:55 +0900243 u8 val;
Samu Onkalo500fe142010-11-11 14:05:22 -0800244
Milo(Woogyom) Kim94482172013-02-05 18:03:55 +0900245 /*
246 * Make sure that the chip is reset by reading back the r channel
247 * current reg. This is dummy read is required on some platforms -
248 * otherwise further access to the R G B channels in the
249 * LP5521_REG_ENABLE register will not have any effect - strange!
250 */
251 ret = lp5521_read(client, LP5521_REG_R_CURRENT, &val);
252 if (ret) {
253 dev_err(&client->dev, "error in resetting chip\n");
254 return ret;
255 }
256 if (val != LP5521_REG_R_CURR_DEFAULT) {
257 dev_err(&client->dev,
258 "unexpected data in register (expected 0x%x got 0x%x)\n",
259 LP5521_REG_R_CURR_DEFAULT, val);
260 ret = -EINVAL;
261 return ret;
262 }
263 usleep_range(10000, 20000);
Samu Onkalo500fe142010-11-11 14:05:22 -0800264
Samu Onkalo500fe142010-11-11 14:05:22 -0800265 /* Set all PWMs to direct control mode */
Kim, Milo32a2f742012-03-23 15:02:09 -0700266 ret = lp5521_write(client, LP5521_REG_OP_MODE, LP5521_CMD_DIRECT);
Samu Onkalo500fe142010-11-11 14:05:22 -0800267
Kim, Milo3b49aac2012-03-23 15:02:08 -0700268 cfg = chip->pdata->update_config ?
269 : (LP5521_PWRSAVE_EN | LP5521_CP_MODE_AUTO | LP5521_R_TO_BATT);
Milo(Woogyom) Kim94482172013-02-05 18:03:55 +0900270 ret = lp5521_write(client, LP5521_REG_CONFIG, cfg);
271 if (ret)
272 return ret;
Samu Onkalo500fe142010-11-11 14:05:22 -0800273
274 /* Initialize all channels PWM to zero -> leds off */
Milo(Woogyom) Kim94482172013-02-05 18:03:55 +0900275 lp5521_write(client, LP5521_REG_R_PWM, 0);
276 lp5521_write(client, LP5521_REG_G_PWM, 0);
277 lp5521_write(client, LP5521_REG_B_PWM, 0);
Samu Onkalo500fe142010-11-11 14:05:22 -0800278
279 /* Set engines are set to run state when OP_MODE enables engines */
Milo(Woogyom) Kim94482172013-02-05 18:03:55 +0900280 ret = lp5521_write(client, LP5521_REG_ENABLE,
Kim, Milo32a2f742012-03-23 15:02:09 -0700281 LP5521_ENABLE_RUN_PROGRAM);
Milo(Woogyom) Kim94482172013-02-05 18:03:55 +0900282 if (ret)
283 return ret;
Samu Onkalo500fe142010-11-11 14:05:22 -0800284
Milo(Woogyom) Kim94482172013-02-05 18:03:55 +0900285 lp5521_wait_enable_done();
286
287 return 0;
Samu Onkalo500fe142010-11-11 14:05:22 -0800288}
289
290static int lp5521_run_selftest(struct lp5521_chip *chip, char *buf)
291{
292 int ret;
293 u8 status;
294
295 ret = lp5521_read(chip->client, LP5521_REG_STATUS, &status);
296 if (ret < 0)
297 return ret;
298
299 /* Check that ext clock is really in use if requested */
300 if (chip->pdata && chip->pdata->clock_mode == LP5521_CLOCK_EXT)
301 if ((status & LP5521_EXT_CLK_USED) == 0)
302 return -EIO;
303 return 0;
304}
305
306static void lp5521_set_brightness(struct led_classdev *cdev,
307 enum led_brightness brightness)
308{
309 struct lp5521_led *led = cdev_to_led(cdev);
310 led->brightness = (u8)brightness;
311 schedule_work(&led->brightness_work);
312}
313
314static void lp5521_led_brightness_work(struct work_struct *work)
315{
316 struct lp5521_led *led = container_of(work,
317 struct lp5521_led,
318 brightness_work);
319 struct lp5521_chip *chip = led_to_lp5521(led);
320 struct i2c_client *client = chip->client;
321
322 mutex_lock(&chip->lock);
323 lp5521_write(client, LP5521_REG_LED_PWM_BASE + led->chan_nr,
324 led->brightness);
325 mutex_unlock(&chip->lock);
326}
327
328/* Detect the chip by setting its ENABLE register and reading it back. */
329static int lp5521_detect(struct i2c_client *client)
330{
331 int ret;
332 u8 buf;
333
Kim, Milo32a2f742012-03-23 15:02:09 -0700334 ret = lp5521_write(client, LP5521_REG_ENABLE, LP5521_ENABLE_DEFAULT);
Samu Onkalo500fe142010-11-11 14:05:22 -0800335 if (ret)
336 return ret;
Samu Onkalo09c76b02010-11-24 12:57:03 -0800337 /* enable takes 500us. 1 - 2 ms leaves some margin */
338 usleep_range(1000, 2000);
Samu Onkalo500fe142010-11-11 14:05:22 -0800339 ret = lp5521_read(client, LP5521_REG_ENABLE, &buf);
340 if (ret)
341 return ret;
Kim, Milo32a2f742012-03-23 15:02:09 -0700342 if (buf != LP5521_ENABLE_DEFAULT)
Samu Onkalo500fe142010-11-11 14:05:22 -0800343 return -ENODEV;
344
345 return 0;
346}
347
348/* Set engine mode and create appropriate sysfs attributes, if required. */
349static int lp5521_set_mode(struct lp5521_engine *engine, u8 mode)
350{
Samu Onkalo500fe142010-11-11 14:05:22 -0800351 int ret = 0;
352
353 /* if in that mode already do nothing, except for run */
354 if (mode == engine->mode && mode != LP5521_CMD_RUN)
355 return 0;
356
357 if (mode == LP5521_CMD_RUN) {
358 ret = lp5521_set_engine_mode(engine, LP5521_CMD_RUN);
359 } else if (mode == LP5521_CMD_LOAD) {
360 lp5521_set_engine_mode(engine, LP5521_CMD_DISABLED);
361 lp5521_set_engine_mode(engine, LP5521_CMD_LOAD);
Samu Onkalo500fe142010-11-11 14:05:22 -0800362 } else if (mode == LP5521_CMD_DISABLED) {
363 lp5521_set_engine_mode(engine, LP5521_CMD_DISABLED);
364 }
365
Samu Onkalo500fe142010-11-11 14:05:22 -0800366 engine->mode = mode;
367
368 return ret;
369}
370
371static int lp5521_do_store_load(struct lp5521_engine *engine,
372 const char *buf, size_t len)
373{
374 struct lp5521_chip *chip = engine_to_lp5521(engine);
375 struct i2c_client *client = chip->client;
376 int ret, nrchars, offset = 0, i = 0;
377 char c[3];
378 unsigned cmd;
379 u8 pattern[LP5521_PROGRAM_LENGTH] = {0};
380
381 while ((offset < len - 1) && (i < LP5521_PROGRAM_LENGTH)) {
382 /* separate sscanfs because length is working only for %s */
383 ret = sscanf(buf + offset, "%2s%n ", c, &nrchars);
Vasiliy Kulikov22602092011-01-12 16:59:14 -0800384 if (ret != 2)
385 goto fail;
Samu Onkalo500fe142010-11-11 14:05:22 -0800386 ret = sscanf(c, "%2x", &cmd);
387 if (ret != 1)
388 goto fail;
389 pattern[i] = (u8)cmd;
390
391 offset += nrchars;
392 i++;
393 }
394
395 /* Each instruction is 16bit long. Check that length is even */
396 if (i % 2)
397 goto fail;
398
399 mutex_lock(&chip->lock);
Samu Onkalod4e7ad02011-01-12 16:59:19 -0800400 if (engine->mode == LP5521_CMD_LOAD)
401 ret = lp5521_load_program(engine, pattern);
402 else
403 ret = -EINVAL;
Samu Onkalo500fe142010-11-11 14:05:22 -0800404 mutex_unlock(&chip->lock);
405
406 if (ret) {
407 dev_err(&client->dev, "failed loading pattern\n");
408 return ret;
409 }
410
411 return len;
412fail:
413 dev_err(&client->dev, "wrong pattern format\n");
414 return -EINVAL;
415}
416
417static ssize_t store_engine_load(struct device *dev,
418 struct device_attribute *attr,
419 const char *buf, size_t len, int nr)
420{
421 struct i2c_client *client = to_i2c_client(dev);
422 struct lp5521_chip *chip = i2c_get_clientdata(client);
423 return lp5521_do_store_load(&chip->engines[nr - 1], buf, len);
424}
425
426#define store_load(nr) \
427static ssize_t store_engine##nr##_load(struct device *dev, \
428 struct device_attribute *attr, \
429 const char *buf, size_t len) \
430{ \
431 return store_engine_load(dev, attr, buf, len, nr); \
432}
433store_load(1)
434store_load(2)
435store_load(3)
436
437static ssize_t show_engine_mode(struct device *dev,
438 struct device_attribute *attr,
439 char *buf, int nr)
440{
441 struct i2c_client *client = to_i2c_client(dev);
442 struct lp5521_chip *chip = i2c_get_clientdata(client);
443 switch (chip->engines[nr - 1].mode) {
444 case LP5521_CMD_RUN:
445 return sprintf(buf, "run\n");
446 case LP5521_CMD_LOAD:
447 return sprintf(buf, "load\n");
448 case LP5521_CMD_DISABLED:
449 return sprintf(buf, "disabled\n");
450 default:
451 return sprintf(buf, "disabled\n");
452 }
453}
454
455#define show_mode(nr) \
456static ssize_t show_engine##nr##_mode(struct device *dev, \
457 struct device_attribute *attr, \
458 char *buf) \
459{ \
460 return show_engine_mode(dev, attr, buf, nr); \
461}
462show_mode(1)
463show_mode(2)
464show_mode(3)
465
466static ssize_t store_engine_mode(struct device *dev,
467 struct device_attribute *attr,
468 const char *buf, size_t len, int nr)
469{
470 struct i2c_client *client = to_i2c_client(dev);
471 struct lp5521_chip *chip = i2c_get_clientdata(client);
472 struct lp5521_engine *engine = &chip->engines[nr - 1];
473 mutex_lock(&chip->lock);
474
475 if (!strncmp(buf, "run", 3))
476 lp5521_set_mode(engine, LP5521_CMD_RUN);
477 else if (!strncmp(buf, "load", 4))
478 lp5521_set_mode(engine, LP5521_CMD_LOAD);
479 else if (!strncmp(buf, "disabled", 8))
480 lp5521_set_mode(engine, LP5521_CMD_DISABLED);
481
482 mutex_unlock(&chip->lock);
483 return len;
484}
485
486#define store_mode(nr) \
487static ssize_t store_engine##nr##_mode(struct device *dev, \
488 struct device_attribute *attr, \
489 const char *buf, size_t len) \
490{ \
491 return store_engine_mode(dev, attr, buf, len, nr); \
492}
493store_mode(1)
494store_mode(2)
495store_mode(3)
496
497static ssize_t show_max_current(struct device *dev,
498 struct device_attribute *attr,
499 char *buf)
500{
501 struct led_classdev *led_cdev = dev_get_drvdata(dev);
502 struct lp5521_led *led = cdev_to_led(led_cdev);
503
504 return sprintf(buf, "%d\n", led->max_current);
505}
506
507static ssize_t show_current(struct device *dev,
508 struct device_attribute *attr,
509 char *buf)
510{
511 struct led_classdev *led_cdev = dev_get_drvdata(dev);
512 struct lp5521_led *led = cdev_to_led(led_cdev);
513
514 return sprintf(buf, "%d\n", led->led_current);
515}
516
517static ssize_t store_current(struct device *dev,
518 struct device_attribute *attr,
519 const char *buf, size_t len)
520{
521 struct led_classdev *led_cdev = dev_get_drvdata(dev);
522 struct lp5521_led *led = cdev_to_led(led_cdev);
523 struct lp5521_chip *chip = led_to_lp5521(led);
524 ssize_t ret;
525 unsigned long curr;
526
Kim, Milo011af7b2012-03-23 15:02:09 -0700527 if (kstrtoul(buf, 0, &curr))
Samu Onkalo500fe142010-11-11 14:05:22 -0800528 return -EINVAL;
529
530 if (curr > led->max_current)
531 return -EINVAL;
532
533 mutex_lock(&chip->lock);
534 ret = lp5521_set_led_current(chip, led->id, curr);
535 mutex_unlock(&chip->lock);
536
537 if (ret < 0)
538 return ret;
539
540 led->led_current = (u8)curr;
541
542 return len;
543}
544
545static ssize_t lp5521_selftest(struct device *dev,
546 struct device_attribute *attr,
547 char *buf)
548{
549 struct i2c_client *client = to_i2c_client(dev);
550 struct lp5521_chip *chip = i2c_get_clientdata(client);
551 int ret;
552
553 mutex_lock(&chip->lock);
554 ret = lp5521_run_selftest(chip, buf);
555 mutex_unlock(&chip->lock);
556 return sprintf(buf, "%s\n", ret ? "FAIL" : "OK");
557}
558
Kim, Milo011af7b2012-03-23 15:02:09 -0700559static void lp5521_clear_program_memory(struct i2c_client *cl)
560{
561 int i;
562 u8 rgb_mem[] = {
563 LP5521_REG_R_PROG_MEM,
564 LP5521_REG_G_PROG_MEM,
565 LP5521_REG_B_PROG_MEM,
566 };
567
568 for (i = 0; i < ARRAY_SIZE(rgb_mem); i++) {
569 lp5521_write(cl, rgb_mem[i], 0);
570 lp5521_write(cl, rgb_mem[i] + 1, 0);
571 }
572}
573
574static void lp5521_write_program_memory(struct i2c_client *cl,
575 u8 base, u8 *rgb, int size)
576{
577 int i;
578
579 if (!rgb || size <= 0)
580 return;
581
582 for (i = 0; i < size; i++)
583 lp5521_write(cl, base + i, *(rgb + i));
584
585 lp5521_write(cl, base + i, 0);
586 lp5521_write(cl, base + i + 1, 0);
587}
588
589static inline struct lp5521_led_pattern *lp5521_get_pattern
590 (struct lp5521_chip *chip, u8 offset)
591{
592 struct lp5521_led_pattern *ptn;
593 ptn = chip->pdata->patterns + (offset - 1);
594 return ptn;
595}
596
597static void lp5521_run_led_pattern(int mode, struct lp5521_chip *chip)
598{
599 struct lp5521_led_pattern *ptn;
600 struct i2c_client *cl = chip->client;
601 int num_patterns = chip->pdata->num_patterns;
602
603 if (mode > num_patterns || !(chip->pdata->patterns))
604 return;
605
606 if (mode == PATTERN_OFF) {
Kim, Milo32a2f742012-03-23 15:02:09 -0700607 lp5521_write(cl, LP5521_REG_ENABLE, LP5521_ENABLE_DEFAULT);
Kim, Milo011af7b2012-03-23 15:02:09 -0700608 usleep_range(1000, 2000);
609 lp5521_write(cl, LP5521_REG_OP_MODE, LP5521_CMD_DIRECT);
610 } else {
611 ptn = lp5521_get_pattern(chip, mode);
612 if (!ptn)
613 return;
614
615 lp5521_write(cl, LP5521_REG_OP_MODE, LP5521_CMD_LOAD);
616 usleep_range(1000, 2000);
617
618 lp5521_clear_program_memory(cl);
619
620 lp5521_write_program_memory(cl, LP5521_REG_R_PROG_MEM,
621 ptn->r, ptn->size_r);
622 lp5521_write_program_memory(cl, LP5521_REG_G_PROG_MEM,
623 ptn->g, ptn->size_g);
624 lp5521_write_program_memory(cl, LP5521_REG_B_PROG_MEM,
625 ptn->b, ptn->size_b);
626
627 lp5521_write(cl, LP5521_REG_OP_MODE, LP5521_CMD_RUN);
628 usleep_range(1000, 2000);
Kim, Milo32a2f742012-03-23 15:02:09 -0700629 lp5521_write(cl, LP5521_REG_ENABLE, LP5521_ENABLE_RUN_PROGRAM);
Kim, Milo011af7b2012-03-23 15:02:09 -0700630 }
631}
632
633static ssize_t store_led_pattern(struct device *dev,
634 struct device_attribute *attr,
635 const char *buf, size_t len)
636{
637 struct lp5521_chip *chip = i2c_get_clientdata(to_i2c_client(dev));
638 unsigned long val;
639 int ret;
640
Jingoo Han69b44c12012-11-18 20:51:20 -0800641 ret = kstrtoul(buf, 16, &val);
Kim, Milo011af7b2012-03-23 15:02:09 -0700642 if (ret)
643 return ret;
644
645 lp5521_run_led_pattern(val, chip);
646
647 return len;
648}
649
Samu Onkalo500fe142010-11-11 14:05:22 -0800650/* led class device attributes */
Vasiliy Kulikov67d1da72011-03-22 16:30:19 -0700651static DEVICE_ATTR(led_current, S_IRUGO | S_IWUSR, show_current, store_current);
Samu Onkalo500fe142010-11-11 14:05:22 -0800652static DEVICE_ATTR(max_current, S_IRUGO , show_max_current, NULL);
653
654static struct attribute *lp5521_led_attributes[] = {
655 &dev_attr_led_current.attr,
656 &dev_attr_max_current.attr,
657 NULL,
658};
659
660static struct attribute_group lp5521_led_attribute_group = {
661 .attrs = lp5521_led_attributes
662};
663
664/* device attributes */
Vasiliy Kulikov67d1da72011-03-22 16:30:19 -0700665static DEVICE_ATTR(engine1_mode, S_IRUGO | S_IWUSR,
Samu Onkalo500fe142010-11-11 14:05:22 -0800666 show_engine1_mode, store_engine1_mode);
Vasiliy Kulikov67d1da72011-03-22 16:30:19 -0700667static DEVICE_ATTR(engine2_mode, S_IRUGO | S_IWUSR,
Samu Onkalo500fe142010-11-11 14:05:22 -0800668 show_engine2_mode, store_engine2_mode);
Vasiliy Kulikov67d1da72011-03-22 16:30:19 -0700669static DEVICE_ATTR(engine3_mode, S_IRUGO | S_IWUSR,
Samu Onkalo500fe142010-11-11 14:05:22 -0800670 show_engine3_mode, store_engine3_mode);
Vasiliy Kulikov67d1da72011-03-22 16:30:19 -0700671static DEVICE_ATTR(engine1_load, S_IWUSR, NULL, store_engine1_load);
672static DEVICE_ATTR(engine2_load, S_IWUSR, NULL, store_engine2_load);
673static DEVICE_ATTR(engine3_load, S_IWUSR, NULL, store_engine3_load);
Samu Onkalo500fe142010-11-11 14:05:22 -0800674static DEVICE_ATTR(selftest, S_IRUGO, lp5521_selftest, NULL);
Kim, Milo011af7b2012-03-23 15:02:09 -0700675static DEVICE_ATTR(led_pattern, S_IWUSR, NULL, store_led_pattern);
Samu Onkalo500fe142010-11-11 14:05:22 -0800676
677static struct attribute *lp5521_attributes[] = {
678 &dev_attr_engine1_mode.attr,
679 &dev_attr_engine2_mode.attr,
680 &dev_attr_engine3_mode.attr,
681 &dev_attr_selftest.attr,
Samu Onkalo500fe142010-11-11 14:05:22 -0800682 &dev_attr_engine1_load.attr,
Samu Onkalo500fe142010-11-11 14:05:22 -0800683 &dev_attr_engine2_load.attr,
Samu Onkalo500fe142010-11-11 14:05:22 -0800684 &dev_attr_engine3_load.attr,
Kim, Milo011af7b2012-03-23 15:02:09 -0700685 &dev_attr_led_pattern.attr,
Samu Onkalo500fe142010-11-11 14:05:22 -0800686 NULL
687};
688
689static const struct attribute_group lp5521_group = {
690 .attrs = lp5521_attributes,
691};
692
Samu Onkalo500fe142010-11-11 14:05:22 -0800693static int lp5521_register_sysfs(struct i2c_client *client)
694{
695 struct device *dev = &client->dev;
696 return sysfs_create_group(&dev->kobj, &lp5521_group);
697}
698
699static void lp5521_unregister_sysfs(struct i2c_client *client)
700{
701 struct lp5521_chip *chip = i2c_get_clientdata(client);
702 struct device *dev = &client->dev;
703 int i;
704
705 sysfs_remove_group(&dev->kobj, &lp5521_group);
706
Samu Onkalo500fe142010-11-11 14:05:22 -0800707 for (i = 0; i < chip->num_leds; i++)
708 sysfs_remove_group(&chip->leds[i].cdev.dev->kobj,
709 &lp5521_led_attribute_group);
710}
711
Milo(Woogyom) Kim86eb7742013-02-05 17:57:02 +0900712static void lp5521_reset_device(struct lp5521_chip *chip)
713{
714 struct i2c_client *client = chip->client;
715
716 lp5521_write(client, LP5521_REG_RESET, 0xff);
717}
718
Milo(Woogyom) Kimf6c64c62013-02-05 17:58:01 +0900719static void lp5521_deinit_device(struct lp5521_chip *chip);
Milo(Woogyom) Kim944f7b12013-02-05 17:49:46 +0900720static int lp5521_init_device(struct lp5521_chip *chip)
721{
722 struct lp5521_platform_data *pdata = chip->pdata;
723 struct i2c_client *client = chip->client;
724 int ret;
Milo(Woogyom) Kim944f7b12013-02-05 17:49:46 +0900725
726 if (pdata->setup_resources) {
727 ret = pdata->setup_resources();
728 if (ret < 0)
729 return ret;
730 }
731
732 if (pdata->enable) {
733 pdata->enable(0);
734 usleep_range(1000, 2000); /* Keep enable down at least 1ms */
735 pdata->enable(1);
736 usleep_range(1000, 2000); /* 500us abs min. */
737 }
738
Milo(Woogyom) Kim86eb7742013-02-05 17:57:02 +0900739 lp5521_reset_device(chip);
740
Milo(Woogyom) Kim944f7b12013-02-05 17:49:46 +0900741 usleep_range(10000, 20000); /*
742 * Exact value is not available. 10 - 20ms
743 * appears to be enough for reset.
744 */
745
Milo(Woogyom) Kim944f7b12013-02-05 17:49:46 +0900746 ret = lp5521_detect(client);
Milo(Woogyom) Kimf6c64c62013-02-05 17:58:01 +0900747 if (ret) {
Milo(Woogyom) Kim944f7b12013-02-05 17:49:46 +0900748 dev_err(&client->dev, "Chip not found\n");
Milo(Woogyom) Kimf6c64c62013-02-05 17:58:01 +0900749 goto err;
750 }
Milo(Woogyom) Kim944f7b12013-02-05 17:49:46 +0900751
Milo(Woogyom) Kimf6c64c62013-02-05 17:58:01 +0900752 ret = lp5521_configure(client);
753 if (ret < 0) {
754 dev_err(&client->dev, "error configuring chip\n");
755 goto err_config;
756 }
757
758 return 0;
759
760err_config:
761 lp5521_deinit_device(chip);
762err:
Milo(Woogyom) Kim944f7b12013-02-05 17:49:46 +0900763 return ret;
764}
765
Milo(Woogyom) Kim1a991482013-02-05 17:50:36 +0900766static void lp5521_deinit_device(struct lp5521_chip *chip)
767{
768 struct lp5521_platform_data *pdata = chip->pdata;
769
770 if (pdata->enable)
771 pdata->enable(0);
772 if (pdata->release_resources)
773 pdata->release_resources();
774}
775
Bill Pemberton98ea1ea2012-11-19 13:23:02 -0500776static int lp5521_init_led(struct lp5521_led *led,
Samu Onkalo500fe142010-11-11 14:05:22 -0800777 struct i2c_client *client,
778 int chan, struct lp5521_platform_data *pdata)
779{
780 struct device *dev = &client->dev;
781 char name[32];
782 int res;
783
784 if (chan >= LP5521_MAX_LEDS)
785 return -EINVAL;
786
787 if (pdata->led_config[chan].led_current == 0)
788 return 0;
789
790 led->led_current = pdata->led_config[chan].led_current;
791 led->max_current = pdata->led_config[chan].max_current;
792 led->chan_nr = pdata->led_config[chan].chan_nr;
793
794 if (led->chan_nr >= LP5521_MAX_LEDS) {
795 dev_err(dev, "Use channel numbers between 0 and %d\n",
796 LP5521_MAX_LEDS - 1);
797 return -EINVAL;
798 }
799
Samu Onkalo500fe142010-11-11 14:05:22 -0800800 led->cdev.brightness_set = lp5521_set_brightness;
Kim, Milo5ae4e8a2012-03-23 15:02:08 -0700801 if (pdata->led_config[chan].name) {
802 led->cdev.name = pdata->led_config[chan].name;
803 } else {
804 snprintf(name, sizeof(name), "%s:channel%d",
805 pdata->label ?: client->name, chan);
806 led->cdev.name = name;
807 }
808
Samu Onkalo500fe142010-11-11 14:05:22 -0800809 res = led_classdev_register(dev, &led->cdev);
810 if (res < 0) {
811 dev_err(dev, "couldn't register led on channel %d\n", chan);
812 return res;
813 }
814
815 res = sysfs_create_group(&led->cdev.dev->kobj,
816 &lp5521_led_attribute_group);
817 if (res < 0) {
818 dev_err(dev, "couldn't register current attribute\n");
819 led_classdev_unregister(&led->cdev);
820 return res;
821 }
822 return 0;
823}
824
Milo(Woogyom) Kimf6524802013-02-05 17:53:40 +0900825static int lp5521_register_leds(struct lp5521_chip *chip)
826{
827 struct lp5521_platform_data *pdata = chip->pdata;
828 struct i2c_client *client = chip->client;
829 int i;
830 int led;
831 int ret;
832
833 /* Initialize leds */
834 chip->num_channels = pdata->num_channels;
835 chip->num_leds = 0;
836 led = 0;
837 for (i = 0; i < pdata->num_channels; i++) {
838 /* Do not initialize channels that are not connected */
839 if (pdata->led_config[i].led_current == 0)
840 continue;
841
842 ret = lp5521_init_led(&chip->leds[led], client, i, pdata);
843 if (ret) {
844 dev_err(&client->dev, "error initializing leds\n");
845 return ret;
846 }
847 chip->num_leds++;
848
849 chip->leds[led].id = led;
850 /* Set initial LED current */
851 lp5521_set_led_current(chip, led,
852 chip->leds[led].led_current);
853
854 INIT_WORK(&(chip->leds[led].brightness_work),
855 lp5521_led_brightness_work);
856
857 led++;
858 }
859
860 return 0;
861}
862
Milo(Woogyom) Kim1904f832013-02-05 17:56:23 +0900863static void lp5521_unregister_leds(struct lp5521_chip *chip)
864{
865 int i;
866
867 for (i = 0; i < chip->num_leds; i++) {
868 led_classdev_unregister(&chip->leds[i].cdev);
869 cancel_work_sync(&chip->leds[i].brightness_work);
870 }
871}
872
Bill Pemberton98ea1ea2012-11-19 13:23:02 -0500873static int lp5521_probe(struct i2c_client *client,
Samu Onkalo500fe142010-11-11 14:05:22 -0800874 const struct i2c_device_id *id)
875{
Milo(Woogyom) Kim6a0c9a42013-02-05 18:03:08 +0900876 struct lp5521_chip *old_chip = NULL;
Milo(Woogyom) Kim1904f832013-02-05 17:56:23 +0900877 int ret;
Milo(Woogyom) Kim6a0c9a42013-02-05 18:03:08 +0900878 struct lp55xx_chip *chip;
879 struct lp55xx_led *led;
880 struct lp55xx_platform_data *pdata = client->dev.platform_data;
Samu Onkalo500fe142010-11-11 14:05:22 -0800881
Milo(Woogyom) Kim6a0c9a42013-02-05 18:03:08 +0900882 if (!pdata) {
Samu Onkalo500fe142010-11-11 14:05:22 -0800883 dev_err(&client->dev, "no platform data\n");
Bryan Wue430dc02012-07-04 11:16:09 +0800884 return -EINVAL;
Samu Onkalo500fe142010-11-11 14:05:22 -0800885 }
886
Milo(Woogyom) Kim6a0c9a42013-02-05 18:03:08 +0900887 chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
888 if (!chip)
889 return -ENOMEM;
Samu Onkalo500fe142010-11-11 14:05:22 -0800890
Milo(Woogyom) Kim6a0c9a42013-02-05 18:03:08 +0900891 led = devm_kzalloc(&client->dev,
892 sizeof(*led) * pdata->num_channels, GFP_KERNEL);
893 if (!led)
894 return -ENOMEM;
895
896 chip->cl = client;
897 chip->pdata = pdata;
898
899 mutex_init(&chip->lock);
900
901 i2c_set_clientdata(client, led);
Samu Onkalo500fe142010-11-11 14:05:22 -0800902
Milo(Woogyom) Kim945c7002013-02-05 18:02:26 +0900903 ret = lp5521_init_device(old_chip);
Milo(Woogyom) Kim944f7b12013-02-05 17:49:46 +0900904 if (ret)
Milo(Woogyom) Kimf6c64c62013-02-05 17:58:01 +0900905 goto err_init;
Samu Onkalo500fe142010-11-11 14:05:22 -0800906
907 dev_info(&client->dev, "%s programmable led chip found\n", id->name);
908
Milo(Woogyom) Kim945c7002013-02-05 18:02:26 +0900909 ret = lp5521_register_leds(old_chip);
Milo(Woogyom) Kimf6524802013-02-05 17:53:40 +0900910 if (ret)
911 goto fail2;
Samu Onkalo500fe142010-11-11 14:05:22 -0800912
913 ret = lp5521_register_sysfs(client);
914 if (ret) {
915 dev_err(&client->dev, "registering sysfs failed\n");
Bryan Wue430dc02012-07-04 11:16:09 +0800916 goto fail2;
Samu Onkalo500fe142010-11-11 14:05:22 -0800917 }
918 return ret;
Bryan Wue430dc02012-07-04 11:16:09 +0800919fail2:
Milo(Woogyom) Kim945c7002013-02-05 18:02:26 +0900920 lp5521_unregister_leds(old_chip);
921 lp5521_deinit_device(old_chip);
Milo(Woogyom) Kimf6c64c62013-02-05 17:58:01 +0900922err_init:
Samu Onkalo500fe142010-11-11 14:05:22 -0800923 return ret;
924}
925
Bill Pemberton678e8a62012-11-19 13:26:00 -0500926static int lp5521_remove(struct i2c_client *client)
Samu Onkalo500fe142010-11-11 14:05:22 -0800927{
Milo(Woogyom) Kim945c7002013-02-05 18:02:26 +0900928 struct lp5521_chip *old_chip = i2c_get_clientdata(client);
Samu Onkalo500fe142010-11-11 14:05:22 -0800929
Milo(Woogyom) Kim945c7002013-02-05 18:02:26 +0900930 lp5521_run_led_pattern(PATTERN_OFF, old_chip);
Samu Onkalo500fe142010-11-11 14:05:22 -0800931 lp5521_unregister_sysfs(client);
932
Milo(Woogyom) Kim945c7002013-02-05 18:02:26 +0900933 lp5521_unregister_leds(old_chip);
Samu Onkalo500fe142010-11-11 14:05:22 -0800934
Milo(Woogyom) Kim945c7002013-02-05 18:02:26 +0900935 lp5521_deinit_device(old_chip);
Samu Onkalo500fe142010-11-11 14:05:22 -0800936 return 0;
937}
938
939static const struct i2c_device_id lp5521_id[] = {
940 { "lp5521", 0 }, /* Three channel chip */
941 { }
942};
943MODULE_DEVICE_TABLE(i2c, lp5521_id);
944
945static struct i2c_driver lp5521_driver = {
946 .driver = {
947 .name = "lp5521",
948 },
949 .probe = lp5521_probe,
Bill Pembertondf07cf82012-11-19 13:20:20 -0500950 .remove = lp5521_remove,
Samu Onkalo500fe142010-11-11 14:05:22 -0800951 .id_table = lp5521_id,
952};
953
Axel Lin09a0d182012-01-10 15:09:27 -0800954module_i2c_driver(lp5521_driver);
Samu Onkalo500fe142010-11-11 14:05:22 -0800955
956MODULE_AUTHOR("Mathias Nyman, Yuri Zaporozhets, Samu Onkalo");
957MODULE_DESCRIPTION("LP5521 LED engine");
958MODULE_LICENSE("GPL v2");