blob: 007c7c921e7e0cfff4989422942c59a7db42e169 [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>
37
38#define LP5521_PROGRAM_LENGTH 32 /* in bytes */
39
40#define LP5521_MAX_LEDS 3 /* Maximum number of LEDs */
41#define LP5521_MAX_ENGINES 3 /* Maximum number of engines */
42
43#define LP5521_ENG_MASK_BASE 0x30 /* 00110000 */
44#define LP5521_ENG_STATUS_MASK 0x07 /* 00000111 */
45
46#define LP5521_CMD_LOAD 0x15 /* 00010101 */
47#define LP5521_CMD_RUN 0x2a /* 00101010 */
48#define LP5521_CMD_DIRECT 0x3f /* 00111111 */
49#define LP5521_CMD_DISABLED 0x00 /* 00000000 */
50
51/* Registers */
52#define LP5521_REG_ENABLE 0x00
53#define LP5521_REG_OP_MODE 0x01
54#define LP5521_REG_R_PWM 0x02
55#define LP5521_REG_G_PWM 0x03
56#define LP5521_REG_B_PWM 0x04
57#define LP5521_REG_R_CURRENT 0x05
58#define LP5521_REG_G_CURRENT 0x06
59#define LP5521_REG_B_CURRENT 0x07
60#define LP5521_REG_CONFIG 0x08
61#define LP5521_REG_R_CHANNEL_PC 0x09
62#define LP5521_REG_G_CHANNEL_PC 0x0A
63#define LP5521_REG_B_CHANNEL_PC 0x0B
64#define LP5521_REG_STATUS 0x0C
65#define LP5521_REG_RESET 0x0D
66#define LP5521_REG_GPO 0x0E
67#define LP5521_REG_R_PROG_MEM 0x10
68#define LP5521_REG_G_PROG_MEM 0x30
69#define LP5521_REG_B_PROG_MEM 0x50
70
71#define LP5521_PROG_MEM_BASE LP5521_REG_R_PROG_MEM
72#define LP5521_PROG_MEM_SIZE 0x20
73
74/* Base register to set LED current */
75#define LP5521_REG_LED_CURRENT_BASE LP5521_REG_R_CURRENT
76
77/* Base register to set the brightness */
78#define LP5521_REG_LED_PWM_BASE LP5521_REG_R_PWM
79
80/* Bits in ENABLE register */
81#define LP5521_MASTER_ENABLE 0x40 /* Chip master enable */
82#define LP5521_LOGARITHMIC_PWM 0x80 /* Logarithmic PWM adjustment */
83#define LP5521_EXEC_RUN 0x2A
84
Samu Onkalo500fe142010-11-11 14:05:22 -080085/* Status */
86#define LP5521_EXT_CLK_USED 0x08
87
Srinidhi KASAGARb3c49c02011-10-31 17:12:24 -070088/* default R channel current register value */
89#define LP5521_REG_R_CURR_DEFAULT 0xAF
90
Kim, Milo011af7b2012-03-23 15:02:09 -070091/* Pattern Mode */
92#define PATTERN_OFF 0
93
Samu Onkalo500fe142010-11-11 14:05:22 -080094struct lp5521_engine {
Samu Onkalo500fe142010-11-11 14:05:22 -080095 int id;
96 u8 mode;
97 u8 prog_page;
98 u8 engine_mask;
99};
100
101struct lp5521_led {
102 int id;
103 u8 chan_nr;
104 u8 led_current;
105 u8 max_current;
106 struct led_classdev cdev;
107 struct work_struct brightness_work;
108 u8 brightness;
109};
110
111struct lp5521_chip {
112 struct lp5521_platform_data *pdata;
113 struct mutex lock; /* Serialize control */
114 struct i2c_client *client;
115 struct lp5521_engine engines[LP5521_MAX_ENGINES];
116 struct lp5521_led leds[LP5521_MAX_LEDS];
117 u8 num_channels;
118 u8 num_leds;
119};
120
Samu Onkalo9fdb18b2010-11-24 12:57:02 -0800121static inline struct lp5521_led *cdev_to_led(struct led_classdev *cdev)
122{
123 return container_of(cdev, struct lp5521_led, cdev);
124}
125
126static inline struct lp5521_chip *engine_to_lp5521(struct lp5521_engine *engine)
127{
128 return container_of(engine, struct lp5521_chip,
129 engines[engine->id - 1]);
130}
131
132static inline struct lp5521_chip *led_to_lp5521(struct lp5521_led *led)
133{
134 return container_of(led, struct lp5521_chip,
135 leds[led->id]);
136}
Samu Onkalo500fe142010-11-11 14:05:22 -0800137
138static void lp5521_led_brightness_work(struct work_struct *work);
139
140static inline int lp5521_write(struct i2c_client *client, u8 reg, u8 value)
141{
142 return i2c_smbus_write_byte_data(client, reg, value);
143}
144
145static int lp5521_read(struct i2c_client *client, u8 reg, u8 *buf)
146{
147 s32 ret;
148
149 ret = i2c_smbus_read_byte_data(client, reg);
150 if (ret < 0)
151 return -EIO;
152
153 *buf = ret;
154 return 0;
155}
156
157static int lp5521_set_engine_mode(struct lp5521_engine *engine, u8 mode)
158{
159 struct lp5521_chip *chip = engine_to_lp5521(engine);
160 struct i2c_client *client = chip->client;
161 int ret;
162 u8 engine_state;
163
164 /* Only transition between RUN and DIRECT mode are handled here */
165 if (mode == LP5521_CMD_LOAD)
166 return 0;
167
168 if (mode == LP5521_CMD_DISABLED)
169 mode = LP5521_CMD_DIRECT;
170
171 ret = lp5521_read(client, LP5521_REG_OP_MODE, &engine_state);
Axel Linfa0ea0e2011-10-31 17:12:12 -0700172 if (ret < 0)
173 return ret;
Samu Onkalo500fe142010-11-11 14:05:22 -0800174
175 /* set mode only for this engine */
176 engine_state &= ~(engine->engine_mask);
177 mode &= engine->engine_mask;
178 engine_state |= mode;
Axel Linfa0ea0e2011-10-31 17:12:12 -0700179 return lp5521_write(client, LP5521_REG_OP_MODE, engine_state);
Samu Onkalo500fe142010-11-11 14:05:22 -0800180}
181
182static int lp5521_load_program(struct lp5521_engine *eng, const u8 *pattern)
183{
184 struct lp5521_chip *chip = engine_to_lp5521(eng);
185 struct i2c_client *client = chip->client;
186 int ret;
187 int addr;
188 u8 mode;
189
190 /* move current engine to direct mode and remember the state */
191 ret = lp5521_set_engine_mode(eng, LP5521_CMD_DIRECT);
Samu Onkalo09c76b02010-11-24 12:57:03 -0800192 /* Mode change requires min 500 us delay. 1 - 2 ms with margin */
193 usleep_range(1000, 2000);
Samu Onkalo500fe142010-11-11 14:05:22 -0800194 ret |= lp5521_read(client, LP5521_REG_OP_MODE, &mode);
195
196 /* For loading, all the engines to load mode */
197 lp5521_write(client, LP5521_REG_OP_MODE, LP5521_CMD_DIRECT);
Samu Onkalo09c76b02010-11-24 12:57:03 -0800198 /* Mode change requires min 500 us delay. 1 - 2 ms with margin */
199 usleep_range(1000, 2000);
Samu Onkalo500fe142010-11-11 14:05:22 -0800200 lp5521_write(client, LP5521_REG_OP_MODE, LP5521_CMD_LOAD);
Samu Onkalo09c76b02010-11-24 12:57:03 -0800201 /* Mode change requires min 500 us delay. 1 - 2 ms with margin */
202 usleep_range(1000, 2000);
Samu Onkalo500fe142010-11-11 14:05:22 -0800203
204 addr = LP5521_PROG_MEM_BASE + eng->prog_page * LP5521_PROG_MEM_SIZE;
205 i2c_smbus_write_i2c_block_data(client,
206 addr,
207 LP5521_PROG_MEM_SIZE,
208 pattern);
209
210 ret |= lp5521_write(client, LP5521_REG_OP_MODE, mode);
211 return ret;
212}
213
214static int lp5521_set_led_current(struct lp5521_chip *chip, int led, u8 curr)
215{
216 return lp5521_write(chip->client,
217 LP5521_REG_LED_CURRENT_BASE + chip->leds[led].chan_nr,
218 curr);
219}
220
Samu Onkalod4e7ad02011-01-12 16:59:19 -0800221static void lp5521_init_engine(struct lp5521_chip *chip)
Samu Onkalo500fe142010-11-11 14:05:22 -0800222{
223 int i;
224 for (i = 0; i < ARRAY_SIZE(chip->engines); i++) {
225 chip->engines[i].id = i + 1;
226 chip->engines[i].engine_mask = LP5521_ENG_MASK_BASE >> (i * 2);
227 chip->engines[i].prog_page = i;
Samu Onkalo500fe142010-11-11 14:05:22 -0800228 }
229}
230
Samu Onkalod4e7ad02011-01-12 16:59:19 -0800231static int lp5521_configure(struct i2c_client *client)
Samu Onkalo500fe142010-11-11 14:05:22 -0800232{
233 struct lp5521_chip *chip = i2c_get_clientdata(client);
234 int ret;
Kim, Milo3b49aac2012-03-23 15:02:08 -0700235 u8 cfg;
Samu Onkalo500fe142010-11-11 14:05:22 -0800236
Samu Onkalod4e7ad02011-01-12 16:59:19 -0800237 lp5521_init_engine(chip);
Samu Onkalo500fe142010-11-11 14:05:22 -0800238
Samu Onkalo500fe142010-11-11 14:05:22 -0800239 /* Set all PWMs to direct control mode */
240 ret = lp5521_write(client, LP5521_REG_OP_MODE, 0x3F);
241
Kim, Milo3b49aac2012-03-23 15:02:08 -0700242 cfg = chip->pdata->update_config ?
243 : (LP5521_PWRSAVE_EN | LP5521_CP_MODE_AUTO | LP5521_R_TO_BATT);
244 ret |= lp5521_write(client, LP5521_REG_CONFIG, cfg);
Samu Onkalo500fe142010-11-11 14:05:22 -0800245
246 /* Initialize all channels PWM to zero -> leds off */
247 ret |= lp5521_write(client, LP5521_REG_R_PWM, 0);
248 ret |= lp5521_write(client, LP5521_REG_G_PWM, 0);
249 ret |= lp5521_write(client, LP5521_REG_B_PWM, 0);
250
251 /* Set engines are set to run state when OP_MODE enables engines */
252 ret |= lp5521_write(client, LP5521_REG_ENABLE,
253 LP5521_MASTER_ENABLE | LP5521_LOGARITHMIC_PWM |
254 LP5521_EXEC_RUN);
Samu Onkalo09c76b02010-11-24 12:57:03 -0800255 /* enable takes 500us. 1 - 2 ms leaves some margin */
256 usleep_range(1000, 2000);
Samu Onkalo500fe142010-11-11 14:05:22 -0800257
258 return ret;
259}
260
261static int lp5521_run_selftest(struct lp5521_chip *chip, char *buf)
262{
263 int ret;
264 u8 status;
265
266 ret = lp5521_read(chip->client, LP5521_REG_STATUS, &status);
267 if (ret < 0)
268 return ret;
269
270 /* Check that ext clock is really in use if requested */
271 if (chip->pdata && chip->pdata->clock_mode == LP5521_CLOCK_EXT)
272 if ((status & LP5521_EXT_CLK_USED) == 0)
273 return -EIO;
274 return 0;
275}
276
277static void lp5521_set_brightness(struct led_classdev *cdev,
278 enum led_brightness brightness)
279{
280 struct lp5521_led *led = cdev_to_led(cdev);
281 led->brightness = (u8)brightness;
282 schedule_work(&led->brightness_work);
283}
284
285static void lp5521_led_brightness_work(struct work_struct *work)
286{
287 struct lp5521_led *led = container_of(work,
288 struct lp5521_led,
289 brightness_work);
290 struct lp5521_chip *chip = led_to_lp5521(led);
291 struct i2c_client *client = chip->client;
292
293 mutex_lock(&chip->lock);
294 lp5521_write(client, LP5521_REG_LED_PWM_BASE + led->chan_nr,
295 led->brightness);
296 mutex_unlock(&chip->lock);
297}
298
299/* Detect the chip by setting its ENABLE register and reading it back. */
300static int lp5521_detect(struct i2c_client *client)
301{
302 int ret;
303 u8 buf;
304
305 ret = lp5521_write(client, LP5521_REG_ENABLE,
306 LP5521_MASTER_ENABLE | LP5521_LOGARITHMIC_PWM);
307 if (ret)
308 return ret;
Samu Onkalo09c76b02010-11-24 12:57:03 -0800309 /* enable takes 500us. 1 - 2 ms leaves some margin */
310 usleep_range(1000, 2000);
Samu Onkalo500fe142010-11-11 14:05:22 -0800311 ret = lp5521_read(client, LP5521_REG_ENABLE, &buf);
312 if (ret)
313 return ret;
314 if (buf != (LP5521_MASTER_ENABLE | LP5521_LOGARITHMIC_PWM))
315 return -ENODEV;
316
317 return 0;
318}
319
320/* Set engine mode and create appropriate sysfs attributes, if required. */
321static int lp5521_set_mode(struct lp5521_engine *engine, u8 mode)
322{
Samu Onkalo500fe142010-11-11 14:05:22 -0800323 int ret = 0;
324
325 /* if in that mode already do nothing, except for run */
326 if (mode == engine->mode && mode != LP5521_CMD_RUN)
327 return 0;
328
329 if (mode == LP5521_CMD_RUN) {
330 ret = lp5521_set_engine_mode(engine, LP5521_CMD_RUN);
331 } else if (mode == LP5521_CMD_LOAD) {
332 lp5521_set_engine_mode(engine, LP5521_CMD_DISABLED);
333 lp5521_set_engine_mode(engine, LP5521_CMD_LOAD);
Samu Onkalo500fe142010-11-11 14:05:22 -0800334 } else if (mode == LP5521_CMD_DISABLED) {
335 lp5521_set_engine_mode(engine, LP5521_CMD_DISABLED);
336 }
337
Samu Onkalo500fe142010-11-11 14:05:22 -0800338 engine->mode = mode;
339
340 return ret;
341}
342
343static int lp5521_do_store_load(struct lp5521_engine *engine,
344 const char *buf, size_t len)
345{
346 struct lp5521_chip *chip = engine_to_lp5521(engine);
347 struct i2c_client *client = chip->client;
348 int ret, nrchars, offset = 0, i = 0;
349 char c[3];
350 unsigned cmd;
351 u8 pattern[LP5521_PROGRAM_LENGTH] = {0};
352
353 while ((offset < len - 1) && (i < LP5521_PROGRAM_LENGTH)) {
354 /* separate sscanfs because length is working only for %s */
355 ret = sscanf(buf + offset, "%2s%n ", c, &nrchars);
Vasiliy Kulikov22602092011-01-12 16:59:14 -0800356 if (ret != 2)
357 goto fail;
Samu Onkalo500fe142010-11-11 14:05:22 -0800358 ret = sscanf(c, "%2x", &cmd);
359 if (ret != 1)
360 goto fail;
361 pattern[i] = (u8)cmd;
362
363 offset += nrchars;
364 i++;
365 }
366
367 /* Each instruction is 16bit long. Check that length is even */
368 if (i % 2)
369 goto fail;
370
371 mutex_lock(&chip->lock);
Samu Onkalod4e7ad02011-01-12 16:59:19 -0800372 if (engine->mode == LP5521_CMD_LOAD)
373 ret = lp5521_load_program(engine, pattern);
374 else
375 ret = -EINVAL;
Samu Onkalo500fe142010-11-11 14:05:22 -0800376 mutex_unlock(&chip->lock);
377
378 if (ret) {
379 dev_err(&client->dev, "failed loading pattern\n");
380 return ret;
381 }
382
383 return len;
384fail:
385 dev_err(&client->dev, "wrong pattern format\n");
386 return -EINVAL;
387}
388
389static ssize_t store_engine_load(struct device *dev,
390 struct device_attribute *attr,
391 const char *buf, size_t len, int nr)
392{
393 struct i2c_client *client = to_i2c_client(dev);
394 struct lp5521_chip *chip = i2c_get_clientdata(client);
395 return lp5521_do_store_load(&chip->engines[nr - 1], buf, len);
396}
397
398#define store_load(nr) \
399static ssize_t store_engine##nr##_load(struct device *dev, \
400 struct device_attribute *attr, \
401 const char *buf, size_t len) \
402{ \
403 return store_engine_load(dev, attr, buf, len, nr); \
404}
405store_load(1)
406store_load(2)
407store_load(3)
408
409static ssize_t show_engine_mode(struct device *dev,
410 struct device_attribute *attr,
411 char *buf, int nr)
412{
413 struct i2c_client *client = to_i2c_client(dev);
414 struct lp5521_chip *chip = i2c_get_clientdata(client);
415 switch (chip->engines[nr - 1].mode) {
416 case LP5521_CMD_RUN:
417 return sprintf(buf, "run\n");
418 case LP5521_CMD_LOAD:
419 return sprintf(buf, "load\n");
420 case LP5521_CMD_DISABLED:
421 return sprintf(buf, "disabled\n");
422 default:
423 return sprintf(buf, "disabled\n");
424 }
425}
426
427#define show_mode(nr) \
428static ssize_t show_engine##nr##_mode(struct device *dev, \
429 struct device_attribute *attr, \
430 char *buf) \
431{ \
432 return show_engine_mode(dev, attr, buf, nr); \
433}
434show_mode(1)
435show_mode(2)
436show_mode(3)
437
438static ssize_t store_engine_mode(struct device *dev,
439 struct device_attribute *attr,
440 const char *buf, size_t len, int nr)
441{
442 struct i2c_client *client = to_i2c_client(dev);
443 struct lp5521_chip *chip = i2c_get_clientdata(client);
444 struct lp5521_engine *engine = &chip->engines[nr - 1];
445 mutex_lock(&chip->lock);
446
447 if (!strncmp(buf, "run", 3))
448 lp5521_set_mode(engine, LP5521_CMD_RUN);
449 else if (!strncmp(buf, "load", 4))
450 lp5521_set_mode(engine, LP5521_CMD_LOAD);
451 else if (!strncmp(buf, "disabled", 8))
452 lp5521_set_mode(engine, LP5521_CMD_DISABLED);
453
454 mutex_unlock(&chip->lock);
455 return len;
456}
457
458#define store_mode(nr) \
459static ssize_t store_engine##nr##_mode(struct device *dev, \
460 struct device_attribute *attr, \
461 const char *buf, size_t len) \
462{ \
463 return store_engine_mode(dev, attr, buf, len, nr); \
464}
465store_mode(1)
466store_mode(2)
467store_mode(3)
468
469static ssize_t show_max_current(struct device *dev,
470 struct device_attribute *attr,
471 char *buf)
472{
473 struct led_classdev *led_cdev = dev_get_drvdata(dev);
474 struct lp5521_led *led = cdev_to_led(led_cdev);
475
476 return sprintf(buf, "%d\n", led->max_current);
477}
478
479static ssize_t show_current(struct device *dev,
480 struct device_attribute *attr,
481 char *buf)
482{
483 struct led_classdev *led_cdev = dev_get_drvdata(dev);
484 struct lp5521_led *led = cdev_to_led(led_cdev);
485
486 return sprintf(buf, "%d\n", led->led_current);
487}
488
489static ssize_t store_current(struct device *dev,
490 struct device_attribute *attr,
491 const char *buf, size_t len)
492{
493 struct led_classdev *led_cdev = dev_get_drvdata(dev);
494 struct lp5521_led *led = cdev_to_led(led_cdev);
495 struct lp5521_chip *chip = led_to_lp5521(led);
496 ssize_t ret;
497 unsigned long curr;
498
Kim, Milo011af7b2012-03-23 15:02:09 -0700499 if (kstrtoul(buf, 0, &curr))
Samu Onkalo500fe142010-11-11 14:05:22 -0800500 return -EINVAL;
501
502 if (curr > led->max_current)
503 return -EINVAL;
504
505 mutex_lock(&chip->lock);
506 ret = lp5521_set_led_current(chip, led->id, curr);
507 mutex_unlock(&chip->lock);
508
509 if (ret < 0)
510 return ret;
511
512 led->led_current = (u8)curr;
513
514 return len;
515}
516
517static ssize_t lp5521_selftest(struct device *dev,
518 struct device_attribute *attr,
519 char *buf)
520{
521 struct i2c_client *client = to_i2c_client(dev);
522 struct lp5521_chip *chip = i2c_get_clientdata(client);
523 int ret;
524
525 mutex_lock(&chip->lock);
526 ret = lp5521_run_selftest(chip, buf);
527 mutex_unlock(&chip->lock);
528 return sprintf(buf, "%s\n", ret ? "FAIL" : "OK");
529}
530
Kim, Milo011af7b2012-03-23 15:02:09 -0700531static void lp5521_clear_program_memory(struct i2c_client *cl)
532{
533 int i;
534 u8 rgb_mem[] = {
535 LP5521_REG_R_PROG_MEM,
536 LP5521_REG_G_PROG_MEM,
537 LP5521_REG_B_PROG_MEM,
538 };
539
540 for (i = 0; i < ARRAY_SIZE(rgb_mem); i++) {
541 lp5521_write(cl, rgb_mem[i], 0);
542 lp5521_write(cl, rgb_mem[i] + 1, 0);
543 }
544}
545
546static void lp5521_write_program_memory(struct i2c_client *cl,
547 u8 base, u8 *rgb, int size)
548{
549 int i;
550
551 if (!rgb || size <= 0)
552 return;
553
554 for (i = 0; i < size; i++)
555 lp5521_write(cl, base + i, *(rgb + i));
556
557 lp5521_write(cl, base + i, 0);
558 lp5521_write(cl, base + i + 1, 0);
559}
560
561static inline struct lp5521_led_pattern *lp5521_get_pattern
562 (struct lp5521_chip *chip, u8 offset)
563{
564 struct lp5521_led_pattern *ptn;
565 ptn = chip->pdata->patterns + (offset - 1);
566 return ptn;
567}
568
569static void lp5521_run_led_pattern(int mode, struct lp5521_chip *chip)
570{
571 struct lp5521_led_pattern *ptn;
572 struct i2c_client *cl = chip->client;
573 int num_patterns = chip->pdata->num_patterns;
574
575 if (mode > num_patterns || !(chip->pdata->patterns))
576 return;
577
578 if (mode == PATTERN_OFF) {
579 lp5521_write(cl, LP5521_REG_ENABLE,
580 LP5521_MASTER_ENABLE | LP5521_LOGARITHMIC_PWM);
581 usleep_range(1000, 2000);
582 lp5521_write(cl, LP5521_REG_OP_MODE, LP5521_CMD_DIRECT);
583 } else {
584 ptn = lp5521_get_pattern(chip, mode);
585 if (!ptn)
586 return;
587
588 lp5521_write(cl, LP5521_REG_OP_MODE, LP5521_CMD_LOAD);
589 usleep_range(1000, 2000);
590
591 lp5521_clear_program_memory(cl);
592
593 lp5521_write_program_memory(cl, LP5521_REG_R_PROG_MEM,
594 ptn->r, ptn->size_r);
595 lp5521_write_program_memory(cl, LP5521_REG_G_PROG_MEM,
596 ptn->g, ptn->size_g);
597 lp5521_write_program_memory(cl, LP5521_REG_B_PROG_MEM,
598 ptn->b, ptn->size_b);
599
600 lp5521_write(cl, LP5521_REG_OP_MODE, LP5521_CMD_RUN);
601 usleep_range(1000, 2000);
602 lp5521_write(cl, LP5521_REG_ENABLE,
603 LP5521_MASTER_ENABLE | LP5521_LOGARITHMIC_PWM |
604 LP5521_EXEC_RUN);
605 }
606}
607
608static ssize_t store_led_pattern(struct device *dev,
609 struct device_attribute *attr,
610 const char *buf, size_t len)
611{
612 struct lp5521_chip *chip = i2c_get_clientdata(to_i2c_client(dev));
613 unsigned long val;
614 int ret;
615
616 ret = strict_strtoul(buf, 16, &val);
617 if (ret)
618 return ret;
619
620 lp5521_run_led_pattern(val, chip);
621
622 return len;
623}
624
Samu Onkalo500fe142010-11-11 14:05:22 -0800625/* led class device attributes */
Vasiliy Kulikov67d1da72011-03-22 16:30:19 -0700626static DEVICE_ATTR(led_current, S_IRUGO | S_IWUSR, show_current, store_current);
Samu Onkalo500fe142010-11-11 14:05:22 -0800627static DEVICE_ATTR(max_current, S_IRUGO , show_max_current, NULL);
628
629static struct attribute *lp5521_led_attributes[] = {
630 &dev_attr_led_current.attr,
631 &dev_attr_max_current.attr,
632 NULL,
633};
634
635static struct attribute_group lp5521_led_attribute_group = {
636 .attrs = lp5521_led_attributes
637};
638
639/* device attributes */
Vasiliy Kulikov67d1da72011-03-22 16:30:19 -0700640static DEVICE_ATTR(engine1_mode, S_IRUGO | S_IWUSR,
Samu Onkalo500fe142010-11-11 14:05:22 -0800641 show_engine1_mode, store_engine1_mode);
Vasiliy Kulikov67d1da72011-03-22 16:30:19 -0700642static DEVICE_ATTR(engine2_mode, S_IRUGO | S_IWUSR,
Samu Onkalo500fe142010-11-11 14:05:22 -0800643 show_engine2_mode, store_engine2_mode);
Vasiliy Kulikov67d1da72011-03-22 16:30:19 -0700644static DEVICE_ATTR(engine3_mode, S_IRUGO | S_IWUSR,
Samu Onkalo500fe142010-11-11 14:05:22 -0800645 show_engine3_mode, store_engine3_mode);
Vasiliy Kulikov67d1da72011-03-22 16:30:19 -0700646static DEVICE_ATTR(engine1_load, S_IWUSR, NULL, store_engine1_load);
647static DEVICE_ATTR(engine2_load, S_IWUSR, NULL, store_engine2_load);
648static DEVICE_ATTR(engine3_load, S_IWUSR, NULL, store_engine3_load);
Samu Onkalo500fe142010-11-11 14:05:22 -0800649static DEVICE_ATTR(selftest, S_IRUGO, lp5521_selftest, NULL);
Kim, Milo011af7b2012-03-23 15:02:09 -0700650static DEVICE_ATTR(led_pattern, S_IWUSR, NULL, store_led_pattern);
Samu Onkalo500fe142010-11-11 14:05:22 -0800651
652static struct attribute *lp5521_attributes[] = {
653 &dev_attr_engine1_mode.attr,
654 &dev_attr_engine2_mode.attr,
655 &dev_attr_engine3_mode.attr,
656 &dev_attr_selftest.attr,
Samu Onkalo500fe142010-11-11 14:05:22 -0800657 &dev_attr_engine1_load.attr,
Samu Onkalo500fe142010-11-11 14:05:22 -0800658 &dev_attr_engine2_load.attr,
Samu Onkalo500fe142010-11-11 14:05:22 -0800659 &dev_attr_engine3_load.attr,
Kim, Milo011af7b2012-03-23 15:02:09 -0700660 &dev_attr_led_pattern.attr,
Samu Onkalo500fe142010-11-11 14:05:22 -0800661 NULL
662};
663
664static const struct attribute_group lp5521_group = {
665 .attrs = lp5521_attributes,
666};
667
Samu Onkalo500fe142010-11-11 14:05:22 -0800668static int lp5521_register_sysfs(struct i2c_client *client)
669{
670 struct device *dev = &client->dev;
671 return sysfs_create_group(&dev->kobj, &lp5521_group);
672}
673
674static void lp5521_unregister_sysfs(struct i2c_client *client)
675{
676 struct lp5521_chip *chip = i2c_get_clientdata(client);
677 struct device *dev = &client->dev;
678 int i;
679
680 sysfs_remove_group(&dev->kobj, &lp5521_group);
681
Samu Onkalo500fe142010-11-11 14:05:22 -0800682 for (i = 0; i < chip->num_leds; i++)
683 sysfs_remove_group(&chip->leds[i].cdev.dev->kobj,
684 &lp5521_led_attribute_group);
685}
686
Ralf Baechle5286bd92011-06-27 16:18:13 -0700687static int __devinit lp5521_init_led(struct lp5521_led *led,
Samu Onkalo500fe142010-11-11 14:05:22 -0800688 struct i2c_client *client,
689 int chan, struct lp5521_platform_data *pdata)
690{
691 struct device *dev = &client->dev;
692 char name[32];
693 int res;
694
695 if (chan >= LP5521_MAX_LEDS)
696 return -EINVAL;
697
698 if (pdata->led_config[chan].led_current == 0)
699 return 0;
700
701 led->led_current = pdata->led_config[chan].led_current;
702 led->max_current = pdata->led_config[chan].max_current;
703 led->chan_nr = pdata->led_config[chan].chan_nr;
704
705 if (led->chan_nr >= LP5521_MAX_LEDS) {
706 dev_err(dev, "Use channel numbers between 0 and %d\n",
707 LP5521_MAX_LEDS - 1);
708 return -EINVAL;
709 }
710
Samu Onkalo500fe142010-11-11 14:05:22 -0800711 led->cdev.brightness_set = lp5521_set_brightness;
Kim, Milo5ae4e8a2012-03-23 15:02:08 -0700712 if (pdata->led_config[chan].name) {
713 led->cdev.name = pdata->led_config[chan].name;
714 } else {
715 snprintf(name, sizeof(name), "%s:channel%d",
716 pdata->label ?: client->name, chan);
717 led->cdev.name = name;
718 }
719
Samu Onkalo500fe142010-11-11 14:05:22 -0800720 res = led_classdev_register(dev, &led->cdev);
721 if (res < 0) {
722 dev_err(dev, "couldn't register led on channel %d\n", chan);
723 return res;
724 }
725
726 res = sysfs_create_group(&led->cdev.dev->kobj,
727 &lp5521_led_attribute_group);
728 if (res < 0) {
729 dev_err(dev, "couldn't register current attribute\n");
730 led_classdev_unregister(&led->cdev);
731 return res;
732 }
733 return 0;
734}
735
Ralf Baechle5286bd92011-06-27 16:18:13 -0700736static int __devinit lp5521_probe(struct i2c_client *client,
Samu Onkalo500fe142010-11-11 14:05:22 -0800737 const struct i2c_device_id *id)
738{
739 struct lp5521_chip *chip;
740 struct lp5521_platform_data *pdata;
741 int ret, i, led;
Srinidhi KASAGARb3c49c02011-10-31 17:12:24 -0700742 u8 buf;
Samu Onkalo500fe142010-11-11 14:05:22 -0800743
744 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
745 if (!chip)
746 return -ENOMEM;
747
748 i2c_set_clientdata(client, chip);
749 chip->client = client;
750
751 pdata = client->dev.platform_data;
752
753 if (!pdata) {
754 dev_err(&client->dev, "no platform data\n");
755 ret = -EINVAL;
756 goto fail1;
757 }
758
759 mutex_init(&chip->lock);
760
761 chip->pdata = pdata;
762
763 if (pdata->setup_resources) {
764 ret = pdata->setup_resources();
765 if (ret < 0)
766 goto fail1;
767 }
768
769 if (pdata->enable) {
770 pdata->enable(0);
Samu Onkalo09c76b02010-11-24 12:57:03 -0800771 usleep_range(1000, 2000); /* Keep enable down at least 1ms */
Samu Onkalo500fe142010-11-11 14:05:22 -0800772 pdata->enable(1);
Samu Onkalo09c76b02010-11-24 12:57:03 -0800773 usleep_range(1000, 2000); /* 500us abs min. */
Samu Onkalo500fe142010-11-11 14:05:22 -0800774 }
775
Samu Onkalo95ea8ee2010-11-24 12:57:05 -0800776 lp5521_write(client, LP5521_REG_RESET, 0xff);
777 usleep_range(10000, 20000); /*
778 * Exact value is not available. 10 - 20ms
779 * appears to be enough for reset.
780 */
Srinidhi KASAGARb3c49c02011-10-31 17:12:24 -0700781
782 /*
783 * Make sure that the chip is reset by reading back the r channel
784 * current reg. This is dummy read is required on some platforms -
785 * otherwise further access to the R G B channels in the
786 * LP5521_REG_ENABLE register will not have any effect - strange!
787 */
788 lp5521_read(client, LP5521_REG_R_CURRENT, &buf);
789 if (buf != LP5521_REG_R_CURR_DEFAULT) {
Masanari Iida3a2fd4a2012-03-23 15:02:06 -0700790 dev_err(&client->dev, "error in resetting chip\n");
Srinidhi KASAGARb3c49c02011-10-31 17:12:24 -0700791 goto fail2;
792 }
793 usleep_range(10000, 20000);
794
Samu Onkalo500fe142010-11-11 14:05:22 -0800795 ret = lp5521_detect(client);
796
797 if (ret) {
798 dev_err(&client->dev, "Chip not found\n");
799 goto fail2;
800 }
801
802 dev_info(&client->dev, "%s programmable led chip found\n", id->name);
803
Samu Onkalod4e7ad02011-01-12 16:59:19 -0800804 ret = lp5521_configure(client);
Samu Onkalo500fe142010-11-11 14:05:22 -0800805 if (ret < 0) {
806 dev_err(&client->dev, "error configuring chip\n");
807 goto fail2;
808 }
809
810 /* Initialize leds */
811 chip->num_channels = pdata->num_channels;
812 chip->num_leds = 0;
813 led = 0;
814 for (i = 0; i < pdata->num_channels; i++) {
815 /* Do not initialize channels that are not connected */
816 if (pdata->led_config[i].led_current == 0)
817 continue;
818
819 ret = lp5521_init_led(&chip->leds[led], client, i, pdata);
820 if (ret) {
821 dev_err(&client->dev, "error initializing leds\n");
822 goto fail3;
823 }
824 chip->num_leds++;
825
826 chip->leds[led].id = led;
827 /* Set initial LED current */
828 lp5521_set_led_current(chip, led,
829 chip->leds[led].led_current);
830
831 INIT_WORK(&(chip->leds[led].brightness_work),
832 lp5521_led_brightness_work);
833
834 led++;
835 }
836
837 ret = lp5521_register_sysfs(client);
838 if (ret) {
839 dev_err(&client->dev, "registering sysfs failed\n");
840 goto fail3;
841 }
842 return ret;
843fail3:
844 for (i = 0; i < chip->num_leds; i++) {
845 led_classdev_unregister(&chip->leds[i].cdev);
846 cancel_work_sync(&chip->leds[i].brightness_work);
847 }
848fail2:
849 if (pdata->enable)
850 pdata->enable(0);
851 if (pdata->release_resources)
852 pdata->release_resources();
853fail1:
854 kfree(chip);
855 return ret;
856}
857
Linus Walleij19ab5cb2011-07-25 17:13:15 -0700858static int __devexit lp5521_remove(struct i2c_client *client)
Samu Onkalo500fe142010-11-11 14:05:22 -0800859{
860 struct lp5521_chip *chip = i2c_get_clientdata(client);
861 int i;
862
Kim, Milo011af7b2012-03-23 15:02:09 -0700863 lp5521_run_led_pattern(PATTERN_OFF, chip);
Samu Onkalo500fe142010-11-11 14:05:22 -0800864 lp5521_unregister_sysfs(client);
865
866 for (i = 0; i < chip->num_leds; i++) {
867 led_classdev_unregister(&chip->leds[i].cdev);
868 cancel_work_sync(&chip->leds[i].brightness_work);
869 }
870
871 if (chip->pdata->enable)
872 chip->pdata->enable(0);
873 if (chip->pdata->release_resources)
874 chip->pdata->release_resources();
875 kfree(chip);
876 return 0;
877}
878
879static const struct i2c_device_id lp5521_id[] = {
880 { "lp5521", 0 }, /* Three channel chip */
881 { }
882};
883MODULE_DEVICE_TABLE(i2c, lp5521_id);
884
885static struct i2c_driver lp5521_driver = {
886 .driver = {
887 .name = "lp5521",
888 },
889 .probe = lp5521_probe,
Linus Walleij19ab5cb2011-07-25 17:13:15 -0700890 .remove = __devexit_p(lp5521_remove),
Samu Onkalo500fe142010-11-11 14:05:22 -0800891 .id_table = lp5521_id,
892};
893
Axel Lin09a0d182012-01-10 15:09:27 -0800894module_i2c_driver(lp5521_driver);
Samu Onkalo500fe142010-11-11 14:05:22 -0800895
896MODULE_AUTHOR("Mathias Nyman, Yuri Zaporozhets, Samu Onkalo");
897MODULE_DESCRIPTION("LP5521 LED engine");
898MODULE_LICENSE("GPL v2");