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