blob: 4248d78cec37258e4a0df293ebd6db1737802194 [file] [log] [blame]
Samu Onkalo0efba162010-11-11 14:05:22 -08001/*
2 * lp5523.c - LP5523 LED 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-lp5523.h>
35#include <linux/workqueue.h>
36#include <linux/slab.h>
37
38#define LP5523_REG_ENABLE 0x00
39#define LP5523_REG_OP_MODE 0x01
40#define LP5523_REG_RATIOMETRIC_MSB 0x02
41#define LP5523_REG_RATIOMETRIC_LSB 0x03
42#define LP5523_REG_ENABLE_LEDS_MSB 0x04
43#define LP5523_REG_ENABLE_LEDS_LSB 0x05
44#define LP5523_REG_LED_CNTRL_BASE 0x06
45#define LP5523_REG_LED_PWM_BASE 0x16
46#define LP5523_REG_LED_CURRENT_BASE 0x26
47#define LP5523_REG_CONFIG 0x36
48#define LP5523_REG_CHANNEL1_PC 0x37
49#define LP5523_REG_CHANNEL2_PC 0x38
50#define LP5523_REG_CHANNEL3_PC 0x39
51#define LP5523_REG_STATUS 0x3a
52#define LP5523_REG_GPO 0x3b
53#define LP5523_REG_VARIABLE 0x3c
54#define LP5523_REG_RESET 0x3d
55#define LP5523_REG_TEMP_CTRL 0x3e
56#define LP5523_REG_TEMP_READ 0x3f
57#define LP5523_REG_TEMP_WRITE 0x40
58#define LP5523_REG_LED_TEST_CTRL 0x41
59#define LP5523_REG_LED_TEST_ADC 0x42
60#define LP5523_REG_ENG1_VARIABLE 0x45
61#define LP5523_REG_ENG2_VARIABLE 0x46
62#define LP5523_REG_ENG3_VARIABLE 0x47
63#define LP5523_REG_MASTER_FADER1 0x48
64#define LP5523_REG_MASTER_FADER2 0x49
65#define LP5523_REG_MASTER_FADER3 0x4a
66#define LP5523_REG_CH1_PROG_START 0x4c
67#define LP5523_REG_CH2_PROG_START 0x4d
68#define LP5523_REG_CH3_PROG_START 0x4e
69#define LP5523_REG_PROG_PAGE_SEL 0x4f
70#define LP5523_REG_PROG_MEM 0x50
71
72#define LP5523_CMD_LOAD 0x15 /* 00010101 */
73#define LP5523_CMD_RUN 0x2a /* 00101010 */
74#define LP5523_CMD_DISABLED 0x00 /* 00000000 */
75
76#define LP5523_ENABLE 0x40
77#define LP5523_AUTO_INC 0x40
78#define LP5523_PWR_SAVE 0x20
79#define LP5523_PWM_PWR_SAVE 0x04
80#define LP5523_CP_1 0x08
81#define LP5523_CP_1_5 0x10
82#define LP5523_CP_AUTO 0x18
83#define LP5523_INT_CLK 0x01
84#define LP5523_AUTO_CLK 0x02
85#define LP5523_EN_LEDTEST 0x80
86#define LP5523_LEDTEST_DONE 0x80
87
88#define LP5523_DEFAULT_CURRENT 50 /* microAmps */
89#define LP5523_PROGRAM_LENGTH 32 /* in bytes */
90#define LP5523_PROGRAM_PAGES 6
91#define LP5523_ADC_SHORTCIRC_LIM 80
92
93#define LP5523_LEDS 9
94#define LP5523_ENGINES 3
95
96#define LP5523_ENG_MASK_BASE 0x30 /* 00110000 */
97
98#define LP5523_ENG_STATUS_MASK 0x07 /* 00000111 */
99
100#define LP5523_IRQ_FLAGS IRQF_TRIGGER_FALLING
101
102#define LP5523_EXT_CLK_USED 0x08
103
104#define LED_ACTIVE(mux, led) (!!(mux & (0x0001 << led)))
105#define SHIFT_MASK(id) (((id) - 1) * 2)
106
107struct lp5523_engine {
108 const struct attribute_group *attributes;
109 int id;
110 u8 mode;
111 u8 prog_page;
112 u8 mux_page;
113 u16 led_mux;
114 u8 engine_mask;
115};
116
117struct lp5523_led {
118 int id;
119 u8 chan_nr;
120 u8 led_current;
121 u8 max_current;
122 struct led_classdev cdev;
123 struct work_struct brightness_work;
124 u8 brightness;
125};
126
127struct lp5523_chip {
128 struct mutex lock; /* Serialize control */
129 struct i2c_client *client;
130 struct lp5523_engine engines[LP5523_ENGINES];
131 struct lp5523_led leds[LP5523_LEDS];
132 struct lp5523_platform_data *pdata;
133 u8 num_channels;
134 u8 num_leds;
135};
136
Samu Onkalo87dbf622010-11-24 12:57:03 -0800137static inline struct lp5523_led *cdev_to_led(struct led_classdev *cdev)
138{
139 return container_of(cdev, struct lp5523_led, cdev);
140}
Samu Onkalo0efba162010-11-11 14:05:22 -0800141
Samu Onkalo87dbf622010-11-24 12:57:03 -0800142static inline struct lp5523_chip *engine_to_lp5523(struct lp5523_engine *engine)
Samu Onkalo0efba162010-11-11 14:05:22 -0800143{
144 return container_of(engine, struct lp5523_chip,
145 engines[engine->id - 1]);
146}
147
Samu Onkalo87dbf622010-11-24 12:57:03 -0800148static inline struct lp5523_chip *led_to_lp5523(struct lp5523_led *led)
Samu Onkalo0efba162010-11-11 14:05:22 -0800149{
150 return container_of(led, struct lp5523_chip,
151 leds[led->id]);
152}
153
154static int lp5523_set_mode(struct lp5523_engine *engine, u8 mode);
155static int lp5523_set_engine_mode(struct lp5523_engine *engine, u8 mode);
156static int lp5523_load_program(struct lp5523_engine *engine, u8 *pattern);
157
158static void lp5523_led_brightness_work(struct work_struct *work);
159
160static int lp5523_write(struct i2c_client *client, u8 reg, u8 value)
161{
162 return i2c_smbus_write_byte_data(client, reg, value);
163}
164
165static int lp5523_read(struct i2c_client *client, u8 reg, u8 *buf)
166{
167 s32 ret = i2c_smbus_read_byte_data(client, reg);
168
169 if (ret < 0)
170 return -EIO;
171
172 *buf = ret;
173 return 0;
174}
175
176static int lp5523_detect(struct i2c_client *client)
177{
178 int ret;
179 u8 buf;
180
181 ret = lp5523_write(client, LP5523_REG_ENABLE, 0x40);
182 if (ret)
183 return ret;
184 ret = lp5523_read(client, LP5523_REG_ENABLE, &buf);
185 if (ret)
186 return ret;
187 if (buf == 0x40)
188 return 0;
189 else
190 return -ENODEV;
191}
192
193static int lp5523_configure(struct i2c_client *client)
194{
195 struct lp5523_chip *chip = i2c_get_clientdata(client);
196 int ret = 0;
197 u8 status;
198
199 /* one pattern per engine setting led mux start and stop addresses */
200 u8 pattern[][LP5523_PROGRAM_LENGTH] = {
201 { 0x9c, 0x30, 0x9c, 0xb0, 0x9d, 0x80, 0xd8, 0x00, 0},
202 { 0x9c, 0x40, 0x9c, 0xc0, 0x9d, 0x80, 0xd8, 0x00, 0},
203 { 0x9c, 0x50, 0x9c, 0xd0, 0x9d, 0x80, 0xd8, 0x00, 0},
204 };
205
206 lp5523_write(client, LP5523_REG_RESET, 0xff);
207
Samu Onkalo2e4840e2010-11-24 12:57:04 -0800208 usleep_range(10000, 20000); /*
209 * Exact value is not available. 10 - 20ms
210 * appears to be enough for reset.
211 */
Samu Onkalo0efba162010-11-11 14:05:22 -0800212
213 ret |= lp5523_write(client, LP5523_REG_ENABLE, LP5523_ENABLE);
Samu Onkalo2e4840e2010-11-24 12:57:04 -0800214 /* Chip startup time is 500 us, 1 - 2 ms gives some margin */
215 usleep_range(1000, 2000);
Samu Onkalo0efba162010-11-11 14:05:22 -0800216
217 ret |= lp5523_write(client, LP5523_REG_CONFIG,
218 LP5523_AUTO_INC | LP5523_PWR_SAVE |
219 LP5523_CP_AUTO | LP5523_AUTO_CLK |
220 LP5523_PWM_PWR_SAVE);
221
222 /* turn on all leds */
223 ret |= lp5523_write(client, LP5523_REG_ENABLE_LEDS_MSB, 0x01);
224 ret |= lp5523_write(client, LP5523_REG_ENABLE_LEDS_LSB, 0xff);
225
226 /* hardcode 32 bytes of memory for each engine from program memory */
227 ret |= lp5523_write(client, LP5523_REG_CH1_PROG_START, 0x00);
228 ret |= lp5523_write(client, LP5523_REG_CH2_PROG_START, 0x10);
229 ret |= lp5523_write(client, LP5523_REG_CH3_PROG_START, 0x20);
230
231 /* write led mux address space for each channel */
232 ret |= lp5523_load_program(&chip->engines[0], pattern[0]);
233 ret |= lp5523_load_program(&chip->engines[1], pattern[1]);
234 ret |= lp5523_load_program(&chip->engines[2], pattern[2]);
235
236 if (ret) {
237 dev_err(&client->dev, "could not load mux programs\n");
238 return -1;
239 }
240
241 /* set all engines exec state and mode to run 00101010 */
242 ret |= lp5523_write(client, LP5523_REG_ENABLE,
243 (LP5523_CMD_RUN | LP5523_ENABLE));
244
245 ret |= lp5523_write(client, LP5523_REG_OP_MODE, LP5523_CMD_RUN);
246
247 if (ret) {
248 dev_err(&client->dev, "could not start mux programs\n");
249 return -1;
250 }
251
Samu Onkalo2e4840e2010-11-24 12:57:04 -0800252 /* Let the programs run for couple of ms and check the engine status */
253 usleep_range(3000, 6000);
Samu Onkalo0efba162010-11-11 14:05:22 -0800254 lp5523_read(client, LP5523_REG_STATUS, &status);
255 status &= LP5523_ENG_STATUS_MASK;
256
257 if (status == LP5523_ENG_STATUS_MASK) {
258 dev_dbg(&client->dev, "all engines configured\n");
259 } else {
260 dev_info(&client->dev, "status == %x\n", status);
261 dev_err(&client->dev, "cound not configure LED engine\n");
262 return -1;
263 }
264
265 dev_info(&client->dev, "disabling engines\n");
266
267 ret |= lp5523_write(client, LP5523_REG_OP_MODE, LP5523_CMD_DISABLED);
268
269 return ret;
270}
271
272static int lp5523_set_engine_mode(struct lp5523_engine *engine, u8 mode)
273{
274 struct lp5523_chip *chip = engine_to_lp5523(engine);
275 struct i2c_client *client = chip->client;
276 int ret;
277 u8 engine_state;
278
279 ret = lp5523_read(client, LP5523_REG_OP_MODE, &engine_state);
280 if (ret)
281 goto fail;
282
283 engine_state &= ~(engine->engine_mask);
284
285 /* set mode only for this engine */
286 mode &= engine->engine_mask;
287
288 engine_state |= mode;
289
290 ret |= lp5523_write(client, LP5523_REG_OP_MODE, engine_state);
291fail:
292 return ret;
293}
294
295static int lp5523_load_mux(struct lp5523_engine *engine, u16 mux)
296{
297 struct lp5523_chip *chip = engine_to_lp5523(engine);
298 struct i2c_client *client = chip->client;
299 int ret = 0;
300
301 ret |= lp5523_set_engine_mode(engine, LP5523_CMD_LOAD);
302
303 ret |= lp5523_write(client, LP5523_REG_PROG_PAGE_SEL, engine->mux_page);
304 ret |= lp5523_write(client, LP5523_REG_PROG_MEM,
305 (u8)(mux >> 8));
306 ret |= lp5523_write(client, LP5523_REG_PROG_MEM + 1, (u8)(mux));
307 engine->led_mux = mux;
308
309 return ret;
310}
311
312static int lp5523_load_program(struct lp5523_engine *engine, u8 *pattern)
313{
314 struct lp5523_chip *chip = engine_to_lp5523(engine);
315 struct i2c_client *client = chip->client;
316
317 int ret = 0;
318
319 ret |= lp5523_set_engine_mode(engine, LP5523_CMD_LOAD);
320
321 ret |= lp5523_write(client, LP5523_REG_PROG_PAGE_SEL,
322 engine->prog_page);
323 ret |= i2c_smbus_write_i2c_block_data(client, LP5523_REG_PROG_MEM,
324 LP5523_PROGRAM_LENGTH, pattern);
325
326 return ret;
327}
328
329static int lp5523_run_program(struct lp5523_engine *engine)
330{
331 struct lp5523_chip *chip = engine_to_lp5523(engine);
332 struct i2c_client *client = chip->client;
333 int ret;
334
335 ret = lp5523_write(client, LP5523_REG_ENABLE,
336 LP5523_CMD_RUN | LP5523_ENABLE);
337 if (ret)
338 goto fail;
339
340 ret = lp5523_set_engine_mode(engine, LP5523_CMD_RUN);
341fail:
342 return ret;
343}
344
345static int lp5523_mux_parse(const char *buf, u16 *mux, size_t len)
346{
347 int i;
348 u16 tmp_mux = 0;
349 len = len < LP5523_LEDS ? len : LP5523_LEDS;
350 for (i = 0; i < len; i++) {
351 switch (buf[i]) {
352 case '1':
353 tmp_mux |= (1 << i);
354 break;
355 case '0':
356 break;
357 case '\n':
358 i = len;
359 break;
360 default:
361 return -1;
362 }
363 }
364 *mux = tmp_mux;
365
366 return 0;
367}
368
369static void lp5523_mux_to_array(u16 led_mux, char *array)
370{
371 int i, pos = 0;
372 for (i = 0; i < LP5523_LEDS; i++)
373 pos += sprintf(array + pos, "%x", LED_ACTIVE(led_mux, i));
374
375 array[pos] = '\0';
376}
377
378/*--------------------------------------------------------------*/
379/* Sysfs interface */
380/*--------------------------------------------------------------*/
381
382static ssize_t show_engine_leds(struct device *dev,
383 struct device_attribute *attr,
384 char *buf, int nr)
385{
386 struct i2c_client *client = to_i2c_client(dev);
387 struct lp5523_chip *chip = i2c_get_clientdata(client);
388 char mux[LP5523_LEDS + 1];
389
390 lp5523_mux_to_array(chip->engines[nr - 1].led_mux, mux);
391
392 return sprintf(buf, "%s\n", mux);
393}
394
395#define show_leds(nr) \
396static ssize_t show_engine##nr##_leds(struct device *dev, \
397 struct device_attribute *attr, \
398 char *buf) \
399{ \
400 return show_engine_leds(dev, attr, buf, nr); \
401}
402show_leds(1)
403show_leds(2)
404show_leds(3)
405
406static ssize_t store_engine_leds(struct device *dev,
407 struct device_attribute *attr,
408 const char *buf, size_t len, int nr)
409{
410 struct i2c_client *client = to_i2c_client(dev);
411 struct lp5523_chip *chip = i2c_get_clientdata(client);
412 u16 mux = 0;
413
414 if (lp5523_mux_parse(buf, &mux, len))
415 return -EINVAL;
416
417 if (lp5523_load_mux(&chip->engines[nr - 1], mux))
418 return -EINVAL;
419
420 return len;
421}
422
423#define store_leds(nr) \
424static ssize_t store_engine##nr##_leds(struct device *dev, \
425 struct device_attribute *attr, \
426 const char *buf, size_t len) \
427{ \
428 return store_engine_leds(dev, attr, buf, len, nr); \
429}
430store_leds(1)
431store_leds(2)
432store_leds(3)
433
434static ssize_t lp5523_selftest(struct device *dev,
435 struct device_attribute *attr,
436 char *buf)
437{
438 struct i2c_client *client = to_i2c_client(dev);
439 struct lp5523_chip *chip = i2c_get_clientdata(client);
440 int i, ret, pos = 0;
441 int led = 0;
442 u8 status, adc, vdd;
443
444 mutex_lock(&chip->lock);
445
446 ret = lp5523_read(chip->client, LP5523_REG_STATUS, &status);
447 if (ret < 0)
448 goto fail;
449
450 /* Check that ext clock is really in use if requested */
451 if ((chip->pdata) && (chip->pdata->clock_mode == LP5523_CLOCK_EXT))
452 if ((status & LP5523_EXT_CLK_USED) == 0)
453 goto fail;
454
455 /* Measure VDD (i.e. VBAT) first (channel 16 corresponds to VDD) */
456 lp5523_write(chip->client, LP5523_REG_LED_TEST_CTRL,
457 LP5523_EN_LEDTEST | 16);
Samu Onkalo2e4840e2010-11-24 12:57:04 -0800458 usleep_range(3000, 6000); /* ADC conversion time is typically 2.7 ms */
Samu Onkalo0efba162010-11-11 14:05:22 -0800459 ret = lp5523_read(chip->client, LP5523_REG_STATUS, &status);
460 if (!(status & LP5523_LEDTEST_DONE))
Samu Onkalo2e4840e2010-11-24 12:57:04 -0800461 usleep_range(3000, 6000); /* Was not ready. Wait little bit */
Samu Onkalo0efba162010-11-11 14:05:22 -0800462
463 ret |= lp5523_read(chip->client, LP5523_REG_LED_TEST_ADC, &vdd);
464 vdd--; /* There may be some fluctuation in measurement */
465
466 for (i = 0; i < LP5523_LEDS; i++) {
467 /* Skip non-existing channels */
468 if (chip->pdata->led_config[i].led_current == 0)
469 continue;
470
471 /* Set default current */
472 lp5523_write(chip->client,
473 LP5523_REG_LED_CURRENT_BASE + i,
474 chip->pdata->led_config[i].led_current);
475
476 lp5523_write(chip->client, LP5523_REG_LED_PWM_BASE + i, 0xff);
Samu Onkalo2e4840e2010-11-24 12:57:04 -0800477 /* let current stabilize 2 - 4ms before measurements start */
478 usleep_range(2000, 4000);
Samu Onkalo0efba162010-11-11 14:05:22 -0800479 lp5523_write(chip->client,
480 LP5523_REG_LED_TEST_CTRL,
481 LP5523_EN_LEDTEST | i);
Samu Onkalo2e4840e2010-11-24 12:57:04 -0800482 /* ADC conversion time is 2.7 ms typically */
483 usleep_range(3000, 6000);
Samu Onkalo0efba162010-11-11 14:05:22 -0800484 ret = lp5523_read(chip->client, LP5523_REG_STATUS, &status);
485 if (!(status & LP5523_LEDTEST_DONE))
Samu Onkalo2e4840e2010-11-24 12:57:04 -0800486 usleep_range(3000, 6000);/* Was not ready. Wait. */
Samu Onkalo0efba162010-11-11 14:05:22 -0800487 ret |= lp5523_read(chip->client, LP5523_REG_LED_TEST_ADC, &adc);
488
489 if (adc >= vdd || adc < LP5523_ADC_SHORTCIRC_LIM)
490 pos += sprintf(buf + pos, "LED %d FAIL\n", i);
491
492 lp5523_write(chip->client, LP5523_REG_LED_PWM_BASE + i, 0x00);
493
494 /* Restore current */
495 lp5523_write(chip->client,
496 LP5523_REG_LED_CURRENT_BASE + i,
497 chip->leds[led].led_current);
498 led++;
499 }
500 if (pos == 0)
501 pos = sprintf(buf, "OK\n");
502 goto release_lock;
503fail:
504 pos = sprintf(buf, "FAIL\n");
505
506release_lock:
507 mutex_unlock(&chip->lock);
508
509 return pos;
510}
511
512static void lp5523_set_brightness(struct led_classdev *cdev,
513 enum led_brightness brightness)
514{
515 struct lp5523_led *led = cdev_to_led(cdev);
516
517 led->brightness = (u8)brightness;
518
519 schedule_work(&led->brightness_work);
520}
521
522static void lp5523_led_brightness_work(struct work_struct *work)
523{
524 struct lp5523_led *led = container_of(work,
525 struct lp5523_led,
526 brightness_work);
527 struct lp5523_chip *chip = led_to_lp5523(led);
528 struct i2c_client *client = chip->client;
529
530 mutex_lock(&chip->lock);
531
532 lp5523_write(client, LP5523_REG_LED_PWM_BASE + led->chan_nr,
533 led->brightness);
534
535 mutex_unlock(&chip->lock);
536}
537
538static int lp5523_do_store_load(struct lp5523_engine *engine,
539 const char *buf, size_t len)
540{
541 struct lp5523_chip *chip = engine_to_lp5523(engine);
542 struct i2c_client *client = chip->client;
543 int ret, nrchars, offset = 0, i = 0;
544 char c[3];
545 unsigned cmd;
546 u8 pattern[LP5523_PROGRAM_LENGTH] = {0};
547
548 while ((offset < len - 1) && (i < LP5523_PROGRAM_LENGTH)) {
549 /* separate sscanfs because length is working only for %s */
550 ret = sscanf(buf + offset, "%2s%n ", c, &nrchars);
551 ret = sscanf(c, "%2x", &cmd);
552 if (ret != 1)
553 goto fail;
554 pattern[i] = (u8)cmd;
555
556 offset += nrchars;
557 i++;
558 }
559
560 /* Each instruction is 16bit long. Check that length is even */
561 if (i % 2)
562 goto fail;
563
564 mutex_lock(&chip->lock);
565
566 ret = lp5523_load_program(engine, pattern);
567 mutex_unlock(&chip->lock);
568
569 if (ret) {
570 dev_err(&client->dev, "failed loading pattern\n");
571 return ret;
572 }
573
574 return len;
575fail:
576 dev_err(&client->dev, "wrong pattern format\n");
577 return -EINVAL;
578}
579
580static ssize_t store_engine_load(struct device *dev,
581 struct device_attribute *attr,
582 const char *buf, size_t len, int nr)
583{
584 struct i2c_client *client = to_i2c_client(dev);
585 struct lp5523_chip *chip = i2c_get_clientdata(client);
586 return lp5523_do_store_load(&chip->engines[nr - 1], buf, len);
587}
588
589#define store_load(nr) \
590static ssize_t store_engine##nr##_load(struct device *dev, \
591 struct device_attribute *attr, \
592 const char *buf, size_t len) \
593{ \
594 return store_engine_load(dev, attr, buf, len, nr); \
595}
596store_load(1)
597store_load(2)
598store_load(3)
599
600static ssize_t show_engine_mode(struct device *dev,
601 struct device_attribute *attr,
602 char *buf, int nr)
603{
604 struct i2c_client *client = to_i2c_client(dev);
605 struct lp5523_chip *chip = i2c_get_clientdata(client);
606 switch (chip->engines[nr - 1].mode) {
607 case LP5523_CMD_RUN:
608 return sprintf(buf, "run\n");
609 case LP5523_CMD_LOAD:
610 return sprintf(buf, "load\n");
611 case LP5523_CMD_DISABLED:
612 return sprintf(buf, "disabled\n");
613 default:
614 return sprintf(buf, "disabled\n");
615 }
616}
617
618#define show_mode(nr) \
619static ssize_t show_engine##nr##_mode(struct device *dev, \
620 struct device_attribute *attr, \
621 char *buf) \
622{ \
623 return show_engine_mode(dev, attr, buf, nr); \
624}
625show_mode(1)
626show_mode(2)
627show_mode(3)
628
629static ssize_t store_engine_mode(struct device *dev,
630 struct device_attribute *attr,
631 const char *buf, size_t len, int nr)
632{
633 struct i2c_client *client = to_i2c_client(dev);
634 struct lp5523_chip *chip = i2c_get_clientdata(client);
635 struct lp5523_engine *engine = &chip->engines[nr - 1];
636 mutex_lock(&chip->lock);
637
638 if (!strncmp(buf, "run", 3))
639 lp5523_set_mode(engine, LP5523_CMD_RUN);
640 else if (!strncmp(buf, "load", 4))
641 lp5523_set_mode(engine, LP5523_CMD_LOAD);
642 else if (!strncmp(buf, "disabled", 8))
643 lp5523_set_mode(engine, LP5523_CMD_DISABLED);
644
645 mutex_unlock(&chip->lock);
646 return len;
647}
648
649#define store_mode(nr) \
650static ssize_t store_engine##nr##_mode(struct device *dev, \
651 struct device_attribute *attr, \
652 const char *buf, size_t len) \
653{ \
654 return store_engine_mode(dev, attr, buf, len, nr); \
655}
656store_mode(1)
657store_mode(2)
658store_mode(3)
659
660static ssize_t show_max_current(struct device *dev,
661 struct device_attribute *attr,
662 char *buf)
663{
664 struct led_classdev *led_cdev = dev_get_drvdata(dev);
665 struct lp5523_led *led = cdev_to_led(led_cdev);
666
667 return sprintf(buf, "%d\n", led->max_current);
668}
669
670static ssize_t show_current(struct device *dev,
671 struct device_attribute *attr,
672 char *buf)
673{
674 struct led_classdev *led_cdev = dev_get_drvdata(dev);
675 struct lp5523_led *led = cdev_to_led(led_cdev);
676
677 return sprintf(buf, "%d\n", led->led_current);
678}
679
680static ssize_t store_current(struct device *dev,
681 struct device_attribute *attr,
682 const char *buf, size_t len)
683{
684 struct led_classdev *led_cdev = dev_get_drvdata(dev);
685 struct lp5523_led *led = cdev_to_led(led_cdev);
686 struct lp5523_chip *chip = led_to_lp5523(led);
687 ssize_t ret;
688 unsigned long curr;
689
690 if (strict_strtoul(buf, 0, &curr))
691 return -EINVAL;
692
693 if (curr > led->max_current)
694 return -EINVAL;
695
696 mutex_lock(&chip->lock);
697 ret = lp5523_write(chip->client,
698 LP5523_REG_LED_CURRENT_BASE + led->chan_nr,
699 (u8)curr);
700 mutex_unlock(&chip->lock);
701
702 if (ret < 0)
703 return ret;
704
705 led->led_current = (u8)curr;
706
707 return len;
708}
709
710/* led class device attributes */
711static DEVICE_ATTR(led_current, S_IRUGO | S_IWUGO, show_current, store_current);
712static DEVICE_ATTR(max_current, S_IRUGO , show_max_current, NULL);
713
714static struct attribute *lp5523_led_attributes[] = {
715 &dev_attr_led_current.attr,
716 &dev_attr_max_current.attr,
717 NULL,
718};
719
720static struct attribute_group lp5523_led_attribute_group = {
721 .attrs = lp5523_led_attributes
722};
723
724/* device attributes */
725static DEVICE_ATTR(engine1_mode, S_IRUGO | S_IWUGO,
726 show_engine1_mode, store_engine1_mode);
727static DEVICE_ATTR(engine2_mode, S_IRUGO | S_IWUGO,
728 show_engine2_mode, store_engine2_mode);
729static DEVICE_ATTR(engine3_mode, S_IRUGO | S_IWUGO,
730 show_engine3_mode, store_engine3_mode);
731static DEVICE_ATTR(engine1_leds, S_IRUGO | S_IWUGO,
732 show_engine1_leds, store_engine1_leds);
733static DEVICE_ATTR(engine2_leds, S_IRUGO | S_IWUGO,
734 show_engine2_leds, store_engine2_leds);
735static DEVICE_ATTR(engine3_leds, S_IRUGO | S_IWUGO,
736 show_engine3_leds, store_engine3_leds);
737static DEVICE_ATTR(engine1_load, S_IWUGO, NULL, store_engine1_load);
738static DEVICE_ATTR(engine2_load, S_IWUGO, NULL, store_engine2_load);
739static DEVICE_ATTR(engine3_load, S_IWUGO, NULL, store_engine3_load);
740static DEVICE_ATTR(selftest, S_IRUGO, lp5523_selftest, NULL);
741
742static struct attribute *lp5523_attributes[] = {
743 &dev_attr_engine1_mode.attr,
744 &dev_attr_engine2_mode.attr,
745 &dev_attr_engine3_mode.attr,
746 &dev_attr_selftest.attr,
747 NULL
748};
749
750static struct attribute *lp5523_engine1_attributes[] = {
751 &dev_attr_engine1_load.attr,
752 &dev_attr_engine1_leds.attr,
753 NULL
754};
755
756static struct attribute *lp5523_engine2_attributes[] = {
757 &dev_attr_engine2_load.attr,
758 &dev_attr_engine2_leds.attr,
759 NULL
760};
761
762static struct attribute *lp5523_engine3_attributes[] = {
763 &dev_attr_engine3_load.attr,
764 &dev_attr_engine3_leds.attr,
765 NULL
766};
767
768static const struct attribute_group lp5523_group = {
769 .attrs = lp5523_attributes,
770};
771
772static const struct attribute_group lp5523_engine_group[] = {
773 {.attrs = lp5523_engine1_attributes },
774 {.attrs = lp5523_engine2_attributes },
775 {.attrs = lp5523_engine3_attributes },
776};
777
778static int lp5523_register_sysfs(struct i2c_client *client)
779{
780 struct device *dev = &client->dev;
781 int ret;
782
783 ret = sysfs_create_group(&dev->kobj, &lp5523_group);
784 if (ret < 0)
785 return ret;
786
787 return 0;
788}
789
790static void lp5523_unregister_sysfs(struct i2c_client *client)
791{
792 struct lp5523_chip *chip = i2c_get_clientdata(client);
793 struct device *dev = &client->dev;
794 int i;
795
796 sysfs_remove_group(&dev->kobj, &lp5523_group);
797
798 for (i = 0; i < ARRAY_SIZE(chip->engines); i++)
799 if (chip->engines[i].mode == LP5523_CMD_LOAD)
800 sysfs_remove_group(&dev->kobj, &lp5523_engine_group[i]);
801
802 for (i = 0; i < chip->num_leds; i++)
803 sysfs_remove_group(&chip->leds[i].cdev.dev->kobj,
804 &lp5523_led_attribute_group);
805}
806
807/*--------------------------------------------------------------*/
808/* Set chip operating mode */
809/*--------------------------------------------------------------*/
810static int lp5523_set_mode(struct lp5523_engine *engine, u8 mode)
811{
812 /* engine to chip */
813 struct lp5523_chip *chip = engine_to_lp5523(engine);
814 struct i2c_client *client = chip->client;
815 struct device *dev = &client->dev;
816 int ret = 0;
817
818 /* if in that mode already do nothing, except for run */
819 if (mode == engine->mode && mode != LP5523_CMD_RUN)
820 return 0;
821
822 if (mode == LP5523_CMD_RUN) {
823 ret = lp5523_run_program(engine);
824 } else if (mode == LP5523_CMD_LOAD) {
825 lp5523_set_engine_mode(engine, LP5523_CMD_DISABLED);
826 lp5523_set_engine_mode(engine, LP5523_CMD_LOAD);
827
828 ret = sysfs_create_group(&dev->kobj, engine->attributes);
829 if (ret)
830 return ret;
831 } else if (mode == LP5523_CMD_DISABLED) {
832 lp5523_set_engine_mode(engine, LP5523_CMD_DISABLED);
833 }
834
835 /* remove load attribute from sysfs if not in load mode */
836 if (engine->mode == LP5523_CMD_LOAD && mode != LP5523_CMD_LOAD)
837 sysfs_remove_group(&dev->kobj, engine->attributes);
838
839 engine->mode = mode;
840
841 return ret;
842}
843
844/*--------------------------------------------------------------*/
845/* Probe, Attach, Remove */
846/*--------------------------------------------------------------*/
847static int __init lp5523_init_engine(struct lp5523_engine *engine, int id)
848{
849 if (id < 1 || id > LP5523_ENGINES)
850 return -1;
851 engine->id = id;
852 engine->engine_mask = LP5523_ENG_MASK_BASE >> SHIFT_MASK(id);
853 engine->prog_page = id - 1;
854 engine->mux_page = id + 2;
855 engine->attributes = &lp5523_engine_group[id - 1];
856
857 return 0;
858}
859
860static int __init lp5523_init_led(struct lp5523_led *led, struct device *dev,
861 int chan, struct lp5523_platform_data *pdata)
862{
863 char name[32];
864 int res;
865
866 if (chan >= LP5523_LEDS)
867 return -EINVAL;
868
869 if (pdata->led_config[chan].led_current) {
870 led->led_current = pdata->led_config[chan].led_current;
871 led->max_current = pdata->led_config[chan].max_current;
872 led->chan_nr = pdata->led_config[chan].chan_nr;
873
874 if (led->chan_nr >= LP5523_LEDS) {
875 dev_err(dev, "Use channel numbers between 0 and %d\n",
876 LP5523_LEDS - 1);
877 return -EINVAL;
878 }
879
880 snprintf(name, 32, "lp5523:channel%d", chan);
881
882 led->cdev.name = name;
883 led->cdev.brightness_set = lp5523_set_brightness;
884 res = led_classdev_register(dev, &led->cdev);
885 if (res < 0) {
886 dev_err(dev, "couldn't register led on channel %d\n",
887 chan);
888 return res;
889 }
890 res = sysfs_create_group(&led->cdev.dev->kobj,
891 &lp5523_led_attribute_group);
892 if (res < 0) {
893 dev_err(dev, "couldn't register current attribute\n");
894 led_classdev_unregister(&led->cdev);
895 return res;
896 }
897 } else {
898 led->led_current = 0;
899 }
900 return 0;
901}
902
903static struct i2c_driver lp5523_driver;
904
905static int lp5523_probe(struct i2c_client *client,
906 const struct i2c_device_id *id)
907{
908 struct lp5523_chip *chip;
909 struct lp5523_platform_data *pdata;
910 int ret, i, led;
911
912 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
913 if (!chip)
914 return -ENOMEM;
915
916 i2c_set_clientdata(client, chip);
917 chip->client = client;
918
919 pdata = client->dev.platform_data;
920
921 if (!pdata) {
922 dev_err(&client->dev, "no platform data\n");
923 ret = -EINVAL;
924 goto fail1;
925 }
926
927 mutex_init(&chip->lock);
928
929 chip->pdata = pdata;
930
931 if (pdata->setup_resources) {
932 ret = pdata->setup_resources();
933 if (ret < 0)
934 goto fail1;
935 }
936
937 if (pdata->enable) {
938 pdata->enable(0);
Samu Onkalo2e4840e2010-11-24 12:57:04 -0800939 usleep_range(1000, 2000); /* Keep enable down at least 1ms */
Samu Onkalo0efba162010-11-11 14:05:22 -0800940 pdata->enable(1);
Samu Onkalo2e4840e2010-11-24 12:57:04 -0800941 usleep_range(1000, 2000); /* 500us abs min. */
Samu Onkalo0efba162010-11-11 14:05:22 -0800942 }
943
944 ret = lp5523_detect(client);
945 if (ret)
946 goto fail2;
947
948 dev_info(&client->dev, "LP5523 Programmable led chip found\n");
949
950 /* Initialize engines */
951 for (i = 0; i < ARRAY_SIZE(chip->engines); i++) {
952 ret = lp5523_init_engine(&chip->engines[i], i + 1);
953 if (ret) {
954 dev_err(&client->dev, "error initializing engine\n");
955 goto fail2;
956 }
957 }
958 ret = lp5523_configure(client);
959 if (ret < 0) {
960 dev_err(&client->dev, "error configuring chip\n");
961 goto fail2;
962 }
963
964 /* Initialize leds */
965 chip->num_channels = pdata->num_channels;
966 chip->num_leds = 0;
967 led = 0;
968 for (i = 0; i < pdata->num_channels; i++) {
969 /* Do not initialize channels that are not connected */
970 if (pdata->led_config[i].led_current == 0)
971 continue;
972
973 ret = lp5523_init_led(&chip->leds[led], &client->dev, i, pdata);
974 if (ret) {
975 dev_err(&client->dev, "error initializing leds\n");
976 goto fail3;
977 }
978 chip->num_leds++;
979
980 chip->leds[led].id = led;
981 /* Set LED current */
982 lp5523_write(client,
983 LP5523_REG_LED_CURRENT_BASE + chip->leds[led].chan_nr,
984 chip->leds[led].led_current);
985
986 INIT_WORK(&(chip->leds[led].brightness_work),
987 lp5523_led_brightness_work);
988
989 led++;
990 }
991
992 ret = lp5523_register_sysfs(client);
993 if (ret) {
994 dev_err(&client->dev, "registering sysfs failed\n");
995 goto fail3;
996 }
997 return ret;
998fail3:
999 for (i = 0; i < chip->num_leds; i++) {
1000 led_classdev_unregister(&chip->leds[i].cdev);
1001 cancel_work_sync(&chip->leds[i].brightness_work);
1002 }
1003fail2:
1004 if (pdata->enable)
1005 pdata->enable(0);
1006 if (pdata->release_resources)
1007 pdata->release_resources();
1008fail1:
1009 kfree(chip);
1010 return ret;
1011}
1012
1013static int lp5523_remove(struct i2c_client *client)
1014{
1015 struct lp5523_chip *chip = i2c_get_clientdata(client);
1016 int i;
1017
1018 lp5523_unregister_sysfs(client);
1019
1020 for (i = 0; i < chip->num_leds; i++) {
1021 led_classdev_unregister(&chip->leds[i].cdev);
1022 cancel_work_sync(&chip->leds[i].brightness_work);
1023 }
1024
1025 if (chip->pdata->enable)
1026 chip->pdata->enable(0);
1027 if (chip->pdata->release_resources)
1028 chip->pdata->release_resources();
1029 kfree(chip);
1030 return 0;
1031}
1032
1033static const struct i2c_device_id lp5523_id[] = {
1034 { "lp5523", 0 },
1035 { }
1036};
1037
1038MODULE_DEVICE_TABLE(i2c, lp5523_id);
1039
1040static struct i2c_driver lp5523_driver = {
1041 .driver = {
1042 .name = "lp5523",
1043 },
1044 .probe = lp5523_probe,
1045 .remove = lp5523_remove,
1046 .id_table = lp5523_id,
1047};
1048
1049static int __init lp5523_init(void)
1050{
1051 int ret;
1052
1053 ret = i2c_add_driver(&lp5523_driver);
1054
1055 if (ret < 0)
1056 printk(KERN_ALERT "Adding lp5523 driver failed\n");
1057
1058 return ret;
1059}
1060
1061static void __exit lp5523_exit(void)
1062{
1063 i2c_del_driver(&lp5523_driver);
1064}
1065
1066module_init(lp5523_init);
1067module_exit(lp5523_exit);
1068
1069MODULE_AUTHOR("Mathias Nyman <mathias.nyman@nokia.com>");
1070MODULE_DESCRIPTION("LP5523 LED engine");
1071MODULE_LICENSE("GPL");