blob: 2ca41c5af7191dc1946567717351fca9b5b412e0 [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>
Milo(Woogyom) Kim6a0c9a42013-02-05 18:03:08 +090037#include <linux/platform_data/leds-lp55xx.h>
38
39#include "leds-lp55xx-common.h"
Samu Onkalo0efba162010-11-11 14:05:22 -080040
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
Kim, Milo27d77042012-09-04 15:06:18 +0800110enum lp5523_chip_id {
111 LP5523,
112 LP55231,
113};
114
Samu Onkalo0efba162010-11-11 14:05:22 -0800115struct lp5523_engine {
Samu Onkalo0efba162010-11-11 14:05:22 -0800116 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
Samu Onkalo87dbf622010-11-24 12:57:03 -0800144static inline struct lp5523_led *cdev_to_led(struct led_classdev *cdev)
145{
146 return container_of(cdev, struct lp5523_led, cdev);
147}
Samu Onkalo0efba162010-11-11 14:05:22 -0800148
Samu Onkalo87dbf622010-11-24 12:57:03 -0800149static inline struct lp5523_chip *engine_to_lp5523(struct lp5523_engine *engine)
Samu Onkalo0efba162010-11-11 14:05:22 -0800150{
151 return container_of(engine, struct lp5523_chip,
152 engines[engine->id - 1]);
153}
154
Samu Onkalo87dbf622010-11-24 12:57:03 -0800155static inline struct lp5523_chip *led_to_lp5523(struct lp5523_led *led)
Samu Onkalo0efba162010-11-11 14:05:22 -0800156{
157 return container_of(led, struct lp5523_chip,
158 leds[led->id]);
159}
160
Kim, Milo6f6365f2012-08-21 17:03:58 +0800161static void lp5523_set_mode(struct lp5523_engine *engine, u8 mode);
Samu Onkalo0efba162010-11-11 14:05:22 -0800162static int lp5523_set_engine_mode(struct lp5523_engine *engine, u8 mode);
Andrew Mortond06cb462012-03-23 15:02:10 -0700163static int lp5523_load_program(struct lp5523_engine *engine, const u8 *pattern);
Samu Onkalo0efba162010-11-11 14:05:22 -0800164
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)
Sachin Kamat5ebab742012-11-20 01:59:01 -0800177 return ret;
Samu Onkalo0efba162010-11-11 14:05:22 -0800178
179 *buf = ret;
180 return 0;
181}
182
183static int lp5523_detect(struct i2c_client *client)
184{
185 int ret;
186 u8 buf;
187
Kim, Milo469eba02012-08-21 17:04:02 +0800188 ret = lp5523_write(client, LP5523_REG_ENABLE, LP5523_ENABLE);
Samu Onkalo0efba162010-11-11 14:05:22 -0800189 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{
Samu Onkalo0efba162010-11-11 14:05:22 -0800202 int ret = 0;
Samu Onkalo0efba162010-11-11 14:05:22 -0800203
Milo(Woogyom) Kim632418b2013-02-05 18:04:50 +0900204 ret = lp5523_write(client, LP5523_REG_ENABLE, LP5523_ENABLE);
205 if (ret)
206 return ret;
Samu Onkalo0efba162010-11-11 14:05:22 -0800207
Samu Onkalo2e4840e2010-11-24 12:57:04 -0800208 /* Chip startup time is 500 us, 1 - 2 ms gives some margin */
209 usleep_range(1000, 2000);
Samu Onkalo0efba162010-11-11 14:05:22 -0800210
Milo(Woogyom) Kim632418b2013-02-05 18:04:50 +0900211 ret = lp5523_write(client, LP5523_REG_CONFIG,
Samu Onkalo0efba162010-11-11 14:05:22 -0800212 LP5523_AUTO_INC | LP5523_PWR_SAVE |
213 LP5523_CP_AUTO | LP5523_AUTO_CLK |
214 LP5523_PWM_PWR_SAVE);
Milo(Woogyom) Kim632418b2013-02-05 18:04:50 +0900215 if (ret)
Jingoo Han1b21ec52012-10-28 18:45:11 -0700216 return ret;
217
Milo(Woogyom) Kim632418b2013-02-05 18:04:50 +0900218 /* turn on all leds */
219 ret = lp5523_write(client, LP5523_REG_ENABLE_LEDS_MSB, 0x01);
220 if (ret)
221 return ret;
Samu Onkalo0efba162010-11-11 14:05:22 -0800222
Milo(Woogyom) Kim632418b2013-02-05 18:04:50 +0900223 return lp5523_write(client, LP5523_REG_ENABLE_LEDS_LSB, 0xff);
Samu Onkalo0efba162010-11-11 14:05:22 -0800224}
225
226static int lp5523_set_engine_mode(struct lp5523_engine *engine, u8 mode)
227{
228 struct lp5523_chip *chip = engine_to_lp5523(engine);
229 struct i2c_client *client = chip->client;
230 int ret;
231 u8 engine_state;
232
233 ret = lp5523_read(client, LP5523_REG_OP_MODE, &engine_state);
234 if (ret)
235 goto fail;
236
237 engine_state &= ~(engine->engine_mask);
238
239 /* set mode only for this engine */
240 mode &= engine->engine_mask;
241
242 engine_state |= mode;
243
244 ret |= lp5523_write(client, LP5523_REG_OP_MODE, engine_state);
245fail:
246 return ret;
247}
248
249static int lp5523_load_mux(struct lp5523_engine *engine, u16 mux)
250{
251 struct lp5523_chip *chip = engine_to_lp5523(engine);
252 struct i2c_client *client = chip->client;
253 int ret = 0;
254
255 ret |= lp5523_set_engine_mode(engine, LP5523_CMD_LOAD);
256
257 ret |= lp5523_write(client, LP5523_REG_PROG_PAGE_SEL, engine->mux_page);
258 ret |= lp5523_write(client, LP5523_REG_PROG_MEM,
259 (u8)(mux >> 8));
260 ret |= lp5523_write(client, LP5523_REG_PROG_MEM + 1, (u8)(mux));
261 engine->led_mux = mux;
262
263 return ret;
264}
265
Andrew Mortond06cb462012-03-23 15:02:10 -0700266static int lp5523_load_program(struct lp5523_engine *engine, const u8 *pattern)
Samu Onkalo0efba162010-11-11 14:05:22 -0800267{
268 struct lp5523_chip *chip = engine_to_lp5523(engine);
269 struct i2c_client *client = chip->client;
270
271 int ret = 0;
272
273 ret |= lp5523_set_engine_mode(engine, LP5523_CMD_LOAD);
274
275 ret |= lp5523_write(client, LP5523_REG_PROG_PAGE_SEL,
276 engine->prog_page);
277 ret |= i2c_smbus_write_i2c_block_data(client, LP5523_REG_PROG_MEM,
278 LP5523_PROGRAM_LENGTH, pattern);
279
280 return ret;
281}
282
283static int lp5523_run_program(struct lp5523_engine *engine)
284{
285 struct lp5523_chip *chip = engine_to_lp5523(engine);
286 struct i2c_client *client = chip->client;
287 int ret;
288
289 ret = lp5523_write(client, LP5523_REG_ENABLE,
290 LP5523_CMD_RUN | LP5523_ENABLE);
291 if (ret)
292 goto fail;
293
294 ret = lp5523_set_engine_mode(engine, LP5523_CMD_RUN);
295fail:
296 return ret;
297}
298
299static int lp5523_mux_parse(const char *buf, u16 *mux, size_t len)
300{
301 int i;
302 u16 tmp_mux = 0;
Kim, Milo469eba02012-08-21 17:04:02 +0800303
304 len = min_t(int, len, LP5523_LEDS);
Samu Onkalo0efba162010-11-11 14:05:22 -0800305 for (i = 0; i < len; i++) {
306 switch (buf[i]) {
307 case '1':
308 tmp_mux |= (1 << i);
309 break;
310 case '0':
311 break;
312 case '\n':
313 i = len;
314 break;
315 default:
316 return -1;
317 }
318 }
319 *mux = tmp_mux;
320
321 return 0;
322}
323
324static void lp5523_mux_to_array(u16 led_mux, char *array)
325{
326 int i, pos = 0;
327 for (i = 0; i < LP5523_LEDS; i++)
328 pos += sprintf(array + pos, "%x", LED_ACTIVE(led_mux, i));
329
330 array[pos] = '\0';
331}
332
333/*--------------------------------------------------------------*/
334/* Sysfs interface */
335/*--------------------------------------------------------------*/
336
337static ssize_t show_engine_leds(struct device *dev,
338 struct device_attribute *attr,
339 char *buf, int nr)
340{
341 struct i2c_client *client = to_i2c_client(dev);
342 struct lp5523_chip *chip = i2c_get_clientdata(client);
343 char mux[LP5523_LEDS + 1];
344
345 lp5523_mux_to_array(chip->engines[nr - 1].led_mux, mux);
346
347 return sprintf(buf, "%s\n", mux);
348}
349
350#define show_leds(nr) \
351static ssize_t show_engine##nr##_leds(struct device *dev, \
352 struct device_attribute *attr, \
353 char *buf) \
354{ \
355 return show_engine_leds(dev, attr, buf, nr); \
356}
357show_leds(1)
358show_leds(2)
359show_leds(3)
360
361static ssize_t store_engine_leds(struct device *dev,
362 struct device_attribute *attr,
363 const char *buf, size_t len, int nr)
364{
365 struct i2c_client *client = to_i2c_client(dev);
366 struct lp5523_chip *chip = i2c_get_clientdata(client);
367 u16 mux = 0;
Samu Onkalofbac0812011-01-12 16:59:18 -0800368 ssize_t ret;
Samu Onkalo0efba162010-11-11 14:05:22 -0800369
370 if (lp5523_mux_parse(buf, &mux, len))
371 return -EINVAL;
372
Samu Onkalofbac0812011-01-12 16:59:18 -0800373 mutex_lock(&chip->lock);
374 ret = -EINVAL;
375 if (chip->engines[nr - 1].mode != LP5523_CMD_LOAD)
376 goto leave;
Samu Onkalo0efba162010-11-11 14:05:22 -0800377
Samu Onkalofbac0812011-01-12 16:59:18 -0800378 if (lp5523_load_mux(&chip->engines[nr - 1], mux))
379 goto leave;
380
381 ret = len;
382leave:
383 mutex_unlock(&chip->lock);
384 return ret;
Samu Onkalo0efba162010-11-11 14:05:22 -0800385}
386
387#define store_leds(nr) \
388static ssize_t store_engine##nr##_leds(struct device *dev, \
389 struct device_attribute *attr, \
390 const char *buf, size_t len) \
391{ \
392 return store_engine_leds(dev, attr, buf, len, nr); \
393}
394store_leds(1)
395store_leds(2)
396store_leds(3)
397
398static ssize_t lp5523_selftest(struct device *dev,
399 struct device_attribute *attr,
400 char *buf)
401{
402 struct i2c_client *client = to_i2c_client(dev);
403 struct lp5523_chip *chip = i2c_get_clientdata(client);
404 int i, ret, pos = 0;
405 int led = 0;
406 u8 status, adc, vdd;
407
408 mutex_lock(&chip->lock);
409
410 ret = lp5523_read(chip->client, LP5523_REG_STATUS, &status);
411 if (ret < 0)
412 goto fail;
413
414 /* Check that ext clock is really in use if requested */
415 if ((chip->pdata) && (chip->pdata->clock_mode == LP5523_CLOCK_EXT))
416 if ((status & LP5523_EXT_CLK_USED) == 0)
417 goto fail;
418
419 /* Measure VDD (i.e. VBAT) first (channel 16 corresponds to VDD) */
420 lp5523_write(chip->client, LP5523_REG_LED_TEST_CTRL,
421 LP5523_EN_LEDTEST | 16);
Samu Onkalo2e4840e2010-11-24 12:57:04 -0800422 usleep_range(3000, 6000); /* ADC conversion time is typically 2.7 ms */
Samu Onkalo0efba162010-11-11 14:05:22 -0800423 ret = lp5523_read(chip->client, LP5523_REG_STATUS, &status);
Jingoo Han1b21ec52012-10-28 18:45:11 -0700424 if (ret < 0)
425 goto fail;
426
Samu Onkalo0efba162010-11-11 14:05:22 -0800427 if (!(status & LP5523_LEDTEST_DONE))
Samu Onkalo2e4840e2010-11-24 12:57:04 -0800428 usleep_range(3000, 6000); /* Was not ready. Wait little bit */
Samu Onkalo0efba162010-11-11 14:05:22 -0800429
Jingoo Han1b21ec52012-10-28 18:45:11 -0700430 ret = lp5523_read(chip->client, LP5523_REG_LED_TEST_ADC, &vdd);
431 if (ret < 0)
432 goto fail;
433
Samu Onkalo0efba162010-11-11 14:05:22 -0800434 vdd--; /* There may be some fluctuation in measurement */
435
436 for (i = 0; i < LP5523_LEDS; i++) {
437 /* Skip non-existing channels */
438 if (chip->pdata->led_config[i].led_current == 0)
439 continue;
440
441 /* Set default current */
442 lp5523_write(chip->client,
443 LP5523_REG_LED_CURRENT_BASE + i,
444 chip->pdata->led_config[i].led_current);
445
446 lp5523_write(chip->client, LP5523_REG_LED_PWM_BASE + i, 0xff);
Samu Onkalo2e4840e2010-11-24 12:57:04 -0800447 /* let current stabilize 2 - 4ms before measurements start */
448 usleep_range(2000, 4000);
Samu Onkalo0efba162010-11-11 14:05:22 -0800449 lp5523_write(chip->client,
450 LP5523_REG_LED_TEST_CTRL,
451 LP5523_EN_LEDTEST | i);
Samu Onkalo2e4840e2010-11-24 12:57:04 -0800452 /* ADC conversion time is 2.7 ms typically */
453 usleep_range(3000, 6000);
Samu Onkalo0efba162010-11-11 14:05:22 -0800454 ret = lp5523_read(chip->client, LP5523_REG_STATUS, &status);
Jingoo Han1b21ec52012-10-28 18:45:11 -0700455 if (ret < 0)
456 goto fail;
457
Samu Onkalo0efba162010-11-11 14:05:22 -0800458 if (!(status & LP5523_LEDTEST_DONE))
Samu Onkalo2e4840e2010-11-24 12:57:04 -0800459 usleep_range(3000, 6000);/* Was not ready. Wait. */
Jingoo Han1b21ec52012-10-28 18:45:11 -0700460 ret = lp5523_read(chip->client, LP5523_REG_LED_TEST_ADC, &adc);
461 if (ret < 0)
462 goto fail;
Samu Onkalo0efba162010-11-11 14:05:22 -0800463
464 if (adc >= vdd || adc < LP5523_ADC_SHORTCIRC_LIM)
465 pos += sprintf(buf + pos, "LED %d FAIL\n", i);
466
467 lp5523_write(chip->client, LP5523_REG_LED_PWM_BASE + i, 0x00);
468
469 /* Restore current */
470 lp5523_write(chip->client,
471 LP5523_REG_LED_CURRENT_BASE + i,
472 chip->leds[led].led_current);
473 led++;
474 }
475 if (pos == 0)
476 pos = sprintf(buf, "OK\n");
477 goto release_lock;
478fail:
479 pos = sprintf(buf, "FAIL\n");
480
481release_lock:
482 mutex_unlock(&chip->lock);
483
484 return pos;
485}
486
487static void lp5523_set_brightness(struct led_classdev *cdev,
488 enum led_brightness brightness)
489{
490 struct lp5523_led *led = cdev_to_led(cdev);
491
492 led->brightness = (u8)brightness;
493
494 schedule_work(&led->brightness_work);
495}
496
497static void lp5523_led_brightness_work(struct work_struct *work)
498{
499 struct lp5523_led *led = container_of(work,
500 struct lp5523_led,
501 brightness_work);
502 struct lp5523_chip *chip = led_to_lp5523(led);
503 struct i2c_client *client = chip->client;
504
505 mutex_lock(&chip->lock);
506
507 lp5523_write(client, LP5523_REG_LED_PWM_BASE + led->chan_nr,
508 led->brightness);
509
510 mutex_unlock(&chip->lock);
511}
512
513static int lp5523_do_store_load(struct lp5523_engine *engine,
514 const char *buf, size_t len)
515{
516 struct lp5523_chip *chip = engine_to_lp5523(engine);
517 struct i2c_client *client = chip->client;
518 int ret, nrchars, offset = 0, i = 0;
519 char c[3];
520 unsigned cmd;
521 u8 pattern[LP5523_PROGRAM_LENGTH] = {0};
522
Kim, Milo469eba02012-08-21 17:04:02 +0800523 if (engine->mode != LP5523_CMD_LOAD)
524 return -EINVAL;
525
Samu Onkalo0efba162010-11-11 14:05:22 -0800526 while ((offset < len - 1) && (i < LP5523_PROGRAM_LENGTH)) {
527 /* separate sscanfs because length is working only for %s */
528 ret = sscanf(buf + offset, "%2s%n ", c, &nrchars);
529 ret = sscanf(c, "%2x", &cmd);
530 if (ret != 1)
531 goto fail;
532 pattern[i] = (u8)cmd;
533
534 offset += nrchars;
535 i++;
536 }
537
538 /* Each instruction is 16bit long. Check that length is even */
539 if (i % 2)
540 goto fail;
541
542 mutex_lock(&chip->lock);
Kim, Milo469eba02012-08-21 17:04:02 +0800543 ret = lp5523_load_program(engine, pattern);
Samu Onkalo0efba162010-11-11 14:05:22 -0800544 mutex_unlock(&chip->lock);
545
546 if (ret) {
547 dev_err(&client->dev, "failed loading pattern\n");
548 return ret;
549 }
550
551 return len;
552fail:
553 dev_err(&client->dev, "wrong pattern format\n");
554 return -EINVAL;
555}
556
557static ssize_t store_engine_load(struct device *dev,
558 struct device_attribute *attr,
559 const char *buf, size_t len, int nr)
560{
561 struct i2c_client *client = to_i2c_client(dev);
562 struct lp5523_chip *chip = i2c_get_clientdata(client);
563 return lp5523_do_store_load(&chip->engines[nr - 1], buf, len);
564}
565
566#define store_load(nr) \
567static ssize_t store_engine##nr##_load(struct device *dev, \
568 struct device_attribute *attr, \
569 const char *buf, size_t len) \
570{ \
571 return store_engine_load(dev, attr, buf, len, nr); \
572}
573store_load(1)
574store_load(2)
575store_load(3)
576
577static ssize_t show_engine_mode(struct device *dev,
578 struct device_attribute *attr,
579 char *buf, int nr)
580{
581 struct i2c_client *client = to_i2c_client(dev);
582 struct lp5523_chip *chip = i2c_get_clientdata(client);
583 switch (chip->engines[nr - 1].mode) {
584 case LP5523_CMD_RUN:
585 return sprintf(buf, "run\n");
586 case LP5523_CMD_LOAD:
587 return sprintf(buf, "load\n");
588 case LP5523_CMD_DISABLED:
589 return sprintf(buf, "disabled\n");
590 default:
591 return sprintf(buf, "disabled\n");
592 }
593}
594
595#define show_mode(nr) \
596static ssize_t show_engine##nr##_mode(struct device *dev, \
597 struct device_attribute *attr, \
598 char *buf) \
599{ \
600 return show_engine_mode(dev, attr, buf, nr); \
601}
602show_mode(1)
603show_mode(2)
604show_mode(3)
605
606static ssize_t store_engine_mode(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 struct lp5523_engine *engine = &chip->engines[nr - 1];
613 mutex_lock(&chip->lock);
614
615 if (!strncmp(buf, "run", 3))
616 lp5523_set_mode(engine, LP5523_CMD_RUN);
617 else if (!strncmp(buf, "load", 4))
618 lp5523_set_mode(engine, LP5523_CMD_LOAD);
619 else if (!strncmp(buf, "disabled", 8))
620 lp5523_set_mode(engine, LP5523_CMD_DISABLED);
621
622 mutex_unlock(&chip->lock);
623 return len;
624}
625
626#define store_mode(nr) \
627static ssize_t store_engine##nr##_mode(struct device *dev, \
628 struct device_attribute *attr, \
629 const char *buf, size_t len) \
630{ \
631 return store_engine_mode(dev, attr, buf, len, nr); \
632}
633store_mode(1)
634store_mode(2)
635store_mode(3)
636
637static ssize_t show_max_current(struct device *dev,
638 struct device_attribute *attr,
639 char *buf)
640{
641 struct led_classdev *led_cdev = dev_get_drvdata(dev);
642 struct lp5523_led *led = cdev_to_led(led_cdev);
643
644 return sprintf(buf, "%d\n", led->max_current);
645}
646
647static ssize_t show_current(struct device *dev,
648 struct device_attribute *attr,
649 char *buf)
650{
651 struct led_classdev *led_cdev = dev_get_drvdata(dev);
652 struct lp5523_led *led = cdev_to_led(led_cdev);
653
654 return sprintf(buf, "%d\n", led->led_current);
655}
656
657static ssize_t store_current(struct device *dev,
658 struct device_attribute *attr,
659 const char *buf, size_t len)
660{
661 struct led_classdev *led_cdev = dev_get_drvdata(dev);
662 struct lp5523_led *led = cdev_to_led(led_cdev);
663 struct lp5523_chip *chip = led_to_lp5523(led);
664 ssize_t ret;
665 unsigned long curr;
666
Jingoo Haneef4aa62012-11-18 20:51:47 -0800667 if (kstrtoul(buf, 0, &curr))
Samu Onkalo0efba162010-11-11 14:05:22 -0800668 return -EINVAL;
669
670 if (curr > led->max_current)
671 return -EINVAL;
672
673 mutex_lock(&chip->lock);
674 ret = lp5523_write(chip->client,
675 LP5523_REG_LED_CURRENT_BASE + led->chan_nr,
676 (u8)curr);
677 mutex_unlock(&chip->lock);
678
679 if (ret < 0)
680 return ret;
681
682 led->led_current = (u8)curr;
683
684 return len;
685}
686
687/* led class device attributes */
Vasiliy Kulikovccd75102011-03-22 16:30:20 -0700688static DEVICE_ATTR(led_current, S_IRUGO | S_IWUSR, show_current, store_current);
Samu Onkalo0efba162010-11-11 14:05:22 -0800689static DEVICE_ATTR(max_current, S_IRUGO , show_max_current, NULL);
690
691static struct attribute *lp5523_led_attributes[] = {
692 &dev_attr_led_current.attr,
693 &dev_attr_max_current.attr,
694 NULL,
695};
696
697static struct attribute_group lp5523_led_attribute_group = {
698 .attrs = lp5523_led_attributes
699};
700
701/* device attributes */
Vasiliy Kulikovccd75102011-03-22 16:30:20 -0700702static DEVICE_ATTR(engine1_mode, S_IRUGO | S_IWUSR,
Samu Onkalo0efba162010-11-11 14:05:22 -0800703 show_engine1_mode, store_engine1_mode);
Vasiliy Kulikovccd75102011-03-22 16:30:20 -0700704static DEVICE_ATTR(engine2_mode, S_IRUGO | S_IWUSR,
Samu Onkalo0efba162010-11-11 14:05:22 -0800705 show_engine2_mode, store_engine2_mode);
Vasiliy Kulikovccd75102011-03-22 16:30:20 -0700706static DEVICE_ATTR(engine3_mode, S_IRUGO | S_IWUSR,
Samu Onkalo0efba162010-11-11 14:05:22 -0800707 show_engine3_mode, store_engine3_mode);
Vasiliy Kulikovccd75102011-03-22 16:30:20 -0700708static DEVICE_ATTR(engine1_leds, S_IRUGO | S_IWUSR,
Samu Onkalo0efba162010-11-11 14:05:22 -0800709 show_engine1_leds, store_engine1_leds);
Vasiliy Kulikovccd75102011-03-22 16:30:20 -0700710static DEVICE_ATTR(engine2_leds, S_IRUGO | S_IWUSR,
Samu Onkalo0efba162010-11-11 14:05:22 -0800711 show_engine2_leds, store_engine2_leds);
Vasiliy Kulikovccd75102011-03-22 16:30:20 -0700712static DEVICE_ATTR(engine3_leds, S_IRUGO | S_IWUSR,
Samu Onkalo0efba162010-11-11 14:05:22 -0800713 show_engine3_leds, store_engine3_leds);
Vasiliy Kulikovccd75102011-03-22 16:30:20 -0700714static DEVICE_ATTR(engine1_load, S_IWUSR, NULL, store_engine1_load);
715static DEVICE_ATTR(engine2_load, S_IWUSR, NULL, store_engine2_load);
716static DEVICE_ATTR(engine3_load, S_IWUSR, NULL, store_engine3_load);
Samu Onkalo0efba162010-11-11 14:05:22 -0800717static DEVICE_ATTR(selftest, S_IRUGO, lp5523_selftest, NULL);
718
719static struct attribute *lp5523_attributes[] = {
720 &dev_attr_engine1_mode.attr,
721 &dev_attr_engine2_mode.attr,
722 &dev_attr_engine3_mode.attr,
723 &dev_attr_selftest.attr,
Samu Onkalo0efba162010-11-11 14:05:22 -0800724 &dev_attr_engine1_load.attr,
725 &dev_attr_engine1_leds.attr,
Samu Onkalo0efba162010-11-11 14:05:22 -0800726 &dev_attr_engine2_load.attr,
727 &dev_attr_engine2_leds.attr,
Samu Onkalo0efba162010-11-11 14:05:22 -0800728 &dev_attr_engine3_load.attr,
729 &dev_attr_engine3_leds.attr,
Kim, Milof1625842012-09-12 20:16:03 +0800730 NULL,
Samu Onkalo0efba162010-11-11 14:05:22 -0800731};
732
733static const struct attribute_group lp5523_group = {
734 .attrs = lp5523_attributes,
735};
736
Samu Onkalo0efba162010-11-11 14:05:22 -0800737static int lp5523_register_sysfs(struct i2c_client *client)
738{
739 struct device *dev = &client->dev;
740 int ret;
741
742 ret = sysfs_create_group(&dev->kobj, &lp5523_group);
743 if (ret < 0)
744 return ret;
745
746 return 0;
747}
748
749static void lp5523_unregister_sysfs(struct i2c_client *client)
750{
751 struct lp5523_chip *chip = i2c_get_clientdata(client);
752 struct device *dev = &client->dev;
753 int i;
754
755 sysfs_remove_group(&dev->kobj, &lp5523_group);
756
Samu Onkalo0efba162010-11-11 14:05:22 -0800757 for (i = 0; i < chip->num_leds; i++)
758 sysfs_remove_group(&chip->leds[i].cdev.dev->kobj,
759 &lp5523_led_attribute_group);
760}
761
762/*--------------------------------------------------------------*/
763/* Set chip operating mode */
764/*--------------------------------------------------------------*/
Kim, Milo6f6365f2012-08-21 17:03:58 +0800765static void lp5523_set_mode(struct lp5523_engine *engine, u8 mode)
Samu Onkalo0efba162010-11-11 14:05:22 -0800766{
Samu Onkalo0efba162010-11-11 14:05:22 -0800767 /* if in that mode already do nothing, except for run */
768 if (mode == engine->mode && mode != LP5523_CMD_RUN)
Kim, Milo6f6365f2012-08-21 17:03:58 +0800769 return;
Samu Onkalo0efba162010-11-11 14:05:22 -0800770
Kim, Milo6f6365f2012-08-21 17:03:58 +0800771 switch (mode) {
772 case LP5523_CMD_RUN:
773 lp5523_run_program(engine);
774 break;
775 case LP5523_CMD_LOAD:
Samu Onkalo0efba162010-11-11 14:05:22 -0800776 lp5523_set_engine_mode(engine, LP5523_CMD_DISABLED);
777 lp5523_set_engine_mode(engine, LP5523_CMD_LOAD);
Kim, Milo6f6365f2012-08-21 17:03:58 +0800778 break;
779 case LP5523_CMD_DISABLED:
Samu Onkalo0efba162010-11-11 14:05:22 -0800780 lp5523_set_engine_mode(engine, LP5523_CMD_DISABLED);
Kim, Milo6f6365f2012-08-21 17:03:58 +0800781 break;
782 default:
783 return;
Samu Onkalo0efba162010-11-11 14:05:22 -0800784 }
785
Samu Onkalo0efba162010-11-11 14:05:22 -0800786 engine->mode = mode;
Samu Onkalo0efba162010-11-11 14:05:22 -0800787}
788
789/*--------------------------------------------------------------*/
790/* Probe, Attach, Remove */
791/*--------------------------------------------------------------*/
792static int __init lp5523_init_engine(struct lp5523_engine *engine, int id)
793{
794 if (id < 1 || id > LP5523_ENGINES)
795 return -1;
796 engine->id = id;
797 engine->engine_mask = LP5523_ENG_MASK_BASE >> SHIFT_MASK(id);
798 engine->prog_page = id - 1;
799 engine->mux_page = id + 2;
Samu Onkalo0efba162010-11-11 14:05:22 -0800800
801 return 0;
802}
803
Bill Pemberton98ea1ea2012-11-19 13:23:02 -0500804static int lp5523_init_led(struct lp5523_led *led, struct device *dev,
Kim, Milo56a1e9a2012-09-04 15:06:26 +0800805 int chan, struct lp5523_platform_data *pdata,
806 const char *chip_name)
Samu Onkalo0efba162010-11-11 14:05:22 -0800807{
808 char name[32];
809 int res;
810
811 if (chan >= LP5523_LEDS)
812 return -EINVAL;
813
814 if (pdata->led_config[chan].led_current) {
815 led->led_current = pdata->led_config[chan].led_current;
816 led->max_current = pdata->led_config[chan].max_current;
817 led->chan_nr = pdata->led_config[chan].chan_nr;
818
819 if (led->chan_nr >= LP5523_LEDS) {
820 dev_err(dev, "Use channel numbers between 0 and %d\n",
821 LP5523_LEDS - 1);
822 return -EINVAL;
823 }
824
Kim, Milo94b43b62012-08-22 15:32:29 +0800825 if (pdata->led_config[chan].name) {
826 led->cdev.name = pdata->led_config[chan].name;
827 } else {
828 snprintf(name, sizeof(name), "%s:channel%d",
Kim, Milo56a1e9a2012-09-04 15:06:26 +0800829 pdata->label ? : chip_name, chan);
Kim, Milo94b43b62012-08-22 15:32:29 +0800830 led->cdev.name = name;
831 }
Samu Onkalo0efba162010-11-11 14:05:22 -0800832
Samu Onkalo0efba162010-11-11 14:05:22 -0800833 led->cdev.brightness_set = lp5523_set_brightness;
834 res = led_classdev_register(dev, &led->cdev);
835 if (res < 0) {
836 dev_err(dev, "couldn't register led on channel %d\n",
837 chan);
838 return res;
839 }
840 res = sysfs_create_group(&led->cdev.dev->kobj,
841 &lp5523_led_attribute_group);
842 if (res < 0) {
843 dev_err(dev, "couldn't register current attribute\n");
844 led_classdev_unregister(&led->cdev);
845 return res;
846 }
847 } else {
848 led->led_current = 0;
849 }
850 return 0;
851}
852
Milo(Woogyom) Kimf6524802013-02-05 17:53:40 +0900853static int lp5523_register_leds(struct lp5523_chip *chip, const char *name)
854{
855 struct lp5523_platform_data *pdata = chip->pdata;
856 struct i2c_client *client = chip->client;
857 int i;
858 int led;
859 int ret;
860
861 /* Initialize leds */
862 chip->num_channels = pdata->num_channels;
863 chip->num_leds = 0;
864 led = 0;
865 for (i = 0; i < pdata->num_channels; i++) {
866 /* Do not initialize channels that are not connected */
867 if (pdata->led_config[i].led_current == 0)
868 continue;
869
870 INIT_WORK(&chip->leds[led].brightness_work,
871 lp5523_led_brightness_work);
872
873 ret = lp5523_init_led(&chip->leds[led], &client->dev, i, pdata,
874 name);
875 if (ret) {
876 dev_err(&client->dev, "error initializing leds\n");
877 return ret;
878 }
879 chip->num_leds++;
880
881 chip->leds[led].id = led;
882 /* Set LED current */
883 lp5523_write(client,
884 LP5523_REG_LED_CURRENT_BASE + chip->leds[led].chan_nr,
885 chip->leds[led].led_current);
886
887 led++;
888 }
889
890 return 0;
891}
892
Milo(Woogyom) Kim1904f832013-02-05 17:56:23 +0900893static void lp5523_unregister_leds(struct lp5523_chip *chip)
894{
895 int i;
896
897 for (i = 0; i < chip->num_leds; i++) {
898 led_classdev_unregister(&chip->leds[i].cdev);
899 flush_work(&chip->leds[i].brightness_work);
900 }
901}
902
Milo(Woogyom) Kim86eb7742013-02-05 17:57:02 +0900903static void lp5523_reset_device(struct lp5523_chip *chip)
904{
905 struct i2c_client *client = chip->client;
906
907 lp5523_write(client, LP5523_REG_RESET, 0xff);
908}
909
Milo(Woogyom) Kimf6c64c62013-02-05 17:58:01 +0900910static void lp5523_deinit_device(struct lp5523_chip *chip);
Milo(Woogyom) Kim944f7b12013-02-05 17:49:46 +0900911static int lp5523_init_device(struct lp5523_chip *chip)
912{
913 struct lp5523_platform_data *pdata = chip->pdata;
914 struct i2c_client *client = chip->client;
915 int ret;
916
917 if (pdata->setup_resources) {
918 ret = pdata->setup_resources();
919 if (ret < 0)
920 return ret;
921 }
922
923 if (pdata->enable) {
924 pdata->enable(0);
925 usleep_range(1000, 2000); /* Keep enable down at least 1ms */
926 pdata->enable(1);
927 usleep_range(1000, 2000); /* 500us abs min. */
928 }
929
Milo(Woogyom) Kim86eb7742013-02-05 17:57:02 +0900930 lp5523_reset_device(chip);
931
Milo(Woogyom) Kim944f7b12013-02-05 17:49:46 +0900932 usleep_range(10000, 20000); /*
933 * Exact value is not available. 10 - 20ms
934 * appears to be enough for reset.
935 */
Milo(Woogyom) Kimf6c64c62013-02-05 17:58:01 +0900936 ret = lp5523_detect(client);
937 if (ret)
938 goto err;
939
940 ret = lp5523_configure(client);
941 if (ret < 0) {
942 dev_err(&client->dev, "error configuring chip\n");
943 goto err_config;
944 }
945
946 return 0;
947
948err_config:
949 lp5523_deinit_device(chip);
950err:
951 return ret;
Milo(Woogyom) Kim944f7b12013-02-05 17:49:46 +0900952}
953
Milo(Woogyom) Kim1a9914852013-02-05 17:50:36 +0900954static void lp5523_deinit_device(struct lp5523_chip *chip)
955{
956 struct lp5523_platform_data *pdata = chip->pdata;
957
958 if (pdata->enable)
959 pdata->enable(0);
960 if (pdata->release_resources)
961 pdata->release_resources();
962}
963
Bill Pemberton98ea1ea2012-11-19 13:23:02 -0500964static int lp5523_probe(struct i2c_client *client,
Samu Onkalo0efba162010-11-11 14:05:22 -0800965 const struct i2c_device_id *id)
966{
Milo(Woogyom) Kim6a0c9a42013-02-05 18:03:08 +0900967 struct lp5523_chip *old_chip = NULL;
Milo(Woogyom) Kimf6524802013-02-05 17:53:40 +0900968 int ret, i;
Milo(Woogyom) Kim6a0c9a42013-02-05 18:03:08 +0900969 struct lp55xx_chip *chip;
970 struct lp55xx_led *led;
971 struct lp55xx_platform_data *pdata = client->dev.platform_data;
Samu Onkalo0efba162010-11-11 14:05:22 -0800972
Milo(Woogyom) Kim6a0c9a42013-02-05 18:03:08 +0900973 if (!pdata) {
Samu Onkalo0efba162010-11-11 14:05:22 -0800974 dev_err(&client->dev, "no platform data\n");
Bryan Wu94ca4bc2012-07-04 11:44:18 +0800975 return -EINVAL;
Samu Onkalo0efba162010-11-11 14:05:22 -0800976 }
977
Milo(Woogyom) Kim6a0c9a42013-02-05 18:03:08 +0900978 chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
979 if (!chip)
980 return -ENOMEM;
Samu Onkalo0efba162010-11-11 14:05:22 -0800981
Milo(Woogyom) Kim6a0c9a42013-02-05 18:03:08 +0900982 led = devm_kzalloc(&client->dev,
983 sizeof(*led) * pdata->num_channels, GFP_KERNEL);
984 if (!led)
985 return -ENOMEM;
986
987 chip->cl = client;
988 chip->pdata = pdata;
989
990 mutex_init(&chip->lock);
991
992 i2c_set_clientdata(client, led);
Samu Onkalo0efba162010-11-11 14:05:22 -0800993
Milo(Woogyom) Kim945c7002013-02-05 18:02:26 +0900994 ret = lp5523_init_device(old_chip);
Samu Onkalo0efba162010-11-11 14:05:22 -0800995 if (ret)
Milo(Woogyom) Kimf6c64c62013-02-05 17:58:01 +0900996 goto err_init;
Samu Onkalo0efba162010-11-11 14:05:22 -0800997
Kim, Milo56a1e9a2012-09-04 15:06:26 +0800998 dev_info(&client->dev, "%s Programmable led chip found\n", id->name);
Samu Onkalo0efba162010-11-11 14:05:22 -0800999
1000 /* Initialize engines */
Milo(Woogyom) Kim945c7002013-02-05 18:02:26 +09001001 for (i = 0; i < ARRAY_SIZE(old_chip->engines); i++) {
1002 ret = lp5523_init_engine(&old_chip->engines[i], i + 1);
Samu Onkalo0efba162010-11-11 14:05:22 -08001003 if (ret) {
1004 dev_err(&client->dev, "error initializing engine\n");
Bryan Wu94ca4bc2012-07-04 11:44:18 +08001005 goto fail1;
Samu Onkalo0efba162010-11-11 14:05:22 -08001006 }
1007 }
Samu Onkalo0efba162010-11-11 14:05:22 -08001008
Milo(Woogyom) Kim945c7002013-02-05 18:02:26 +09001009 ret = lp5523_register_leds(old_chip, id->name);
Milo(Woogyom) Kimf6524802013-02-05 17:53:40 +09001010 if (ret)
1011 goto fail2;
Samu Onkalo0efba162010-11-11 14:05:22 -08001012
1013 ret = lp5523_register_sysfs(client);
1014 if (ret) {
1015 dev_err(&client->dev, "registering sysfs failed\n");
Bryan Wu94ca4bc2012-07-04 11:44:18 +08001016 goto fail2;
Samu Onkalo0efba162010-11-11 14:05:22 -08001017 }
1018 return ret;
Bryan Wu94ca4bc2012-07-04 11:44:18 +08001019fail2:
Milo(Woogyom) Kim945c7002013-02-05 18:02:26 +09001020 lp5523_unregister_leds(old_chip);
Bryan Wu94ca4bc2012-07-04 11:44:18 +08001021fail1:
Milo(Woogyom) Kim945c7002013-02-05 18:02:26 +09001022 lp5523_deinit_device(old_chip);
Milo(Woogyom) Kimf6c64c62013-02-05 17:58:01 +09001023err_init:
Samu Onkalo0efba162010-11-11 14:05:22 -08001024 return ret;
1025}
1026
1027static int lp5523_remove(struct i2c_client *client)
1028{
Milo(Woogyom) Kim945c7002013-02-05 18:02:26 +09001029 struct lp5523_chip *old_chip = i2c_get_clientdata(client);
Samu Onkalo0efba162010-11-11 14:05:22 -08001030
Kim, Milo23301b72012-09-12 20:16:00 +08001031 /* Disable engine mode */
1032 lp5523_write(client, LP5523_REG_OP_MODE, LP5523_CMD_DISABLED);
1033
Samu Onkalo0efba162010-11-11 14:05:22 -08001034 lp5523_unregister_sysfs(client);
1035
Milo(Woogyom) Kim945c7002013-02-05 18:02:26 +09001036 lp5523_unregister_leds(old_chip);
Samu Onkalo0efba162010-11-11 14:05:22 -08001037
Milo(Woogyom) Kim945c7002013-02-05 18:02:26 +09001038 lp5523_deinit_device(old_chip);
Samu Onkalo0efba162010-11-11 14:05:22 -08001039 return 0;
1040}
1041
1042static const struct i2c_device_id lp5523_id[] = {
Kim, Milo27d77042012-09-04 15:06:18 +08001043 { "lp5523", LP5523 },
1044 { "lp55231", LP55231 },
Samu Onkalo0efba162010-11-11 14:05:22 -08001045 { }
1046};
1047
1048MODULE_DEVICE_TABLE(i2c, lp5523_id);
1049
1050static struct i2c_driver lp5523_driver = {
1051 .driver = {
Kim, Milo27d77042012-09-04 15:06:18 +08001052 .name = "lp5523x",
Samu Onkalo0efba162010-11-11 14:05:22 -08001053 },
1054 .probe = lp5523_probe,
1055 .remove = lp5523_remove,
1056 .id_table = lp5523_id,
1057};
1058
Axel Lin09a0d182012-01-10 15:09:27 -08001059module_i2c_driver(lp5523_driver);
Samu Onkalo0efba162010-11-11 14:05:22 -08001060
1061MODULE_AUTHOR("Mathias Nyman <mathias.nyman@nokia.com>");
1062MODULE_DESCRIPTION("LP5523 LED engine");
1063MODULE_LICENSE("GPL");