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