blob: 00547783db77256fa103b8b8e2c91d490239ddfa [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
Milo(Woogyom) Kim48068d52013-02-05 18:08:49 +090090#define LP5523_RESET 0xFF
Samu Onkalo0efba162010-11-11 14:05:22 -080091
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
Kim, Milo27d77042012-09-04 15:06:18 +0800111enum lp5523_chip_id {
112 LP5523,
113 LP55231,
114};
115
Samu Onkalo0efba162010-11-11 14:05:22 -0800116struct lp5523_engine {
Samu Onkalo0efba162010-11-11 14:05:22 -0800117 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
Samu Onkalo87dbf622010-11-24 12:57:03 -0800145static inline struct lp5523_led *cdev_to_led(struct led_classdev *cdev)
146{
147 return container_of(cdev, struct lp5523_led, cdev);
148}
Samu Onkalo0efba162010-11-11 14:05:22 -0800149
Samu Onkalo87dbf622010-11-24 12:57:03 -0800150static inline struct lp5523_chip *engine_to_lp5523(struct lp5523_engine *engine)
Samu Onkalo0efba162010-11-11 14:05:22 -0800151{
152 return container_of(engine, struct lp5523_chip,
153 engines[engine->id - 1]);
154}
155
Samu Onkalo87dbf622010-11-24 12:57:03 -0800156static inline struct lp5523_chip *led_to_lp5523(struct lp5523_led *led)
Samu Onkalo0efba162010-11-11 14:05:22 -0800157{
158 return container_of(led, struct lp5523_chip,
159 leds[led->id]);
160}
161
Kim, Milo6f6365f2012-08-21 17:03:58 +0800162static void lp5523_set_mode(struct lp5523_engine *engine, u8 mode);
Samu Onkalo0efba162010-11-11 14:05:22 -0800163static int lp5523_set_engine_mode(struct lp5523_engine *engine, u8 mode);
Andrew Mortond06cb462012-03-23 15:02:10 -0700164static int lp5523_load_program(struct lp5523_engine *engine, const u8 *pattern);
Samu Onkalo0efba162010-11-11 14:05:22 -0800165
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)
Sachin Kamat5ebab742012-11-20 01:59:01 -0800178 return ret;
Samu Onkalo0efba162010-11-11 14:05:22 -0800179
180 *buf = ret;
181 return 0;
182}
183
184static int lp5523_detect(struct i2c_client *client)
185{
186 int ret;
187 u8 buf;
188
Kim, Milo469eba02012-08-21 17:04:02 +0800189 ret = lp5523_write(client, LP5523_REG_ENABLE, LP5523_ENABLE);
Samu Onkalo0efba162010-11-11 14:05:22 -0800190 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{
Samu Onkalo0efba162010-11-11 14:05:22 -0800203 int ret = 0;
Samu Onkalo0efba162010-11-11 14:05:22 -0800204
Milo(Woogyom) Kim632418b2013-02-05 18:04:50 +0900205 ret = lp5523_write(client, LP5523_REG_ENABLE, LP5523_ENABLE);
206 if (ret)
207 return ret;
Samu Onkalo0efba162010-11-11 14:05:22 -0800208
Samu Onkalo2e4840e2010-11-24 12:57:04 -0800209 /* Chip startup time is 500 us, 1 - 2 ms gives some margin */
210 usleep_range(1000, 2000);
Samu Onkalo0efba162010-11-11 14:05:22 -0800211
Milo(Woogyom) Kim632418b2013-02-05 18:04:50 +0900212 ret = lp5523_write(client, LP5523_REG_CONFIG,
Samu Onkalo0efba162010-11-11 14:05:22 -0800213 LP5523_AUTO_INC | LP5523_PWR_SAVE |
214 LP5523_CP_AUTO | LP5523_AUTO_CLK |
215 LP5523_PWM_PWR_SAVE);
Milo(Woogyom) Kim632418b2013-02-05 18:04:50 +0900216 if (ret)
Jingoo Han1b21ec52012-10-28 18:45:11 -0700217 return ret;
218
Milo(Woogyom) Kim632418b2013-02-05 18:04:50 +0900219 /* turn on all leds */
220 ret = lp5523_write(client, LP5523_REG_ENABLE_LEDS_MSB, 0x01);
221 if (ret)
222 return ret;
Samu Onkalo0efba162010-11-11 14:05:22 -0800223
Milo(Woogyom) Kim632418b2013-02-05 18:04:50 +0900224 return lp5523_write(client, LP5523_REG_ENABLE_LEDS_LSB, 0xff);
Samu Onkalo0efba162010-11-11 14:05:22 -0800225}
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
Andrew Mortond06cb462012-03-23 15:02:10 -0700267static int lp5523_load_program(struct lp5523_engine *engine, const u8 *pattern)
Samu Onkalo0efba162010-11-11 14:05:22 -0800268{
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;
Kim, Milo469eba02012-08-21 17:04:02 +0800304
305 len = min_t(int, len, LP5523_LEDS);
Samu Onkalo0efba162010-11-11 14:05:22 -0800306 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;
Samu Onkalofbac0812011-01-12 16:59:18 -0800369 ssize_t ret;
Samu Onkalo0efba162010-11-11 14:05:22 -0800370
371 if (lp5523_mux_parse(buf, &mux, len))
372 return -EINVAL;
373
Samu Onkalofbac0812011-01-12 16:59:18 -0800374 mutex_lock(&chip->lock);
375 ret = -EINVAL;
376 if (chip->engines[nr - 1].mode != LP5523_CMD_LOAD)
377 goto leave;
Samu Onkalo0efba162010-11-11 14:05:22 -0800378
Samu Onkalofbac0812011-01-12 16:59:18 -0800379 if (lp5523_load_mux(&chip->engines[nr - 1], mux))
380 goto leave;
381
382 ret = len;
383leave:
384 mutex_unlock(&chip->lock);
385 return ret;
Samu Onkalo0efba162010-11-11 14:05:22 -0800386}
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);
Samu Onkalo2e4840e2010-11-24 12:57:04 -0800423 usleep_range(3000, 6000); /* ADC conversion time is typically 2.7 ms */
Samu Onkalo0efba162010-11-11 14:05:22 -0800424 ret = lp5523_read(chip->client, LP5523_REG_STATUS, &status);
Jingoo Han1b21ec52012-10-28 18:45:11 -0700425 if (ret < 0)
426 goto fail;
427
Samu Onkalo0efba162010-11-11 14:05:22 -0800428 if (!(status & LP5523_LEDTEST_DONE))
Samu Onkalo2e4840e2010-11-24 12:57:04 -0800429 usleep_range(3000, 6000); /* Was not ready. Wait little bit */
Samu Onkalo0efba162010-11-11 14:05:22 -0800430
Jingoo Han1b21ec52012-10-28 18:45:11 -0700431 ret = lp5523_read(chip->client, LP5523_REG_LED_TEST_ADC, &vdd);
432 if (ret < 0)
433 goto fail;
434
Samu Onkalo0efba162010-11-11 14:05:22 -0800435 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);
Samu Onkalo2e4840e2010-11-24 12:57:04 -0800448 /* let current stabilize 2 - 4ms before measurements start */
449 usleep_range(2000, 4000);
Samu Onkalo0efba162010-11-11 14:05:22 -0800450 lp5523_write(chip->client,
451 LP5523_REG_LED_TEST_CTRL,
452 LP5523_EN_LEDTEST | i);
Samu Onkalo2e4840e2010-11-24 12:57:04 -0800453 /* ADC conversion time is 2.7 ms typically */
454 usleep_range(3000, 6000);
Samu Onkalo0efba162010-11-11 14:05:22 -0800455 ret = lp5523_read(chip->client, LP5523_REG_STATUS, &status);
Jingoo Han1b21ec52012-10-28 18:45:11 -0700456 if (ret < 0)
457 goto fail;
458
Samu Onkalo0efba162010-11-11 14:05:22 -0800459 if (!(status & LP5523_LEDTEST_DONE))
Samu Onkalo2e4840e2010-11-24 12:57:04 -0800460 usleep_range(3000, 6000);/* Was not ready. Wait. */
Jingoo Han1b21ec52012-10-28 18:45:11 -0700461 ret = lp5523_read(chip->client, LP5523_REG_LED_TEST_ADC, &adc);
462 if (ret < 0)
463 goto fail;
Samu Onkalo0efba162010-11-11 14:05:22 -0800464
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
Kim, Milo469eba02012-08-21 17:04:02 +0800524 if (engine->mode != LP5523_CMD_LOAD)
525 return -EINVAL;
526
Samu Onkalo0efba162010-11-11 14:05:22 -0800527 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);
Kim, Milo469eba02012-08-21 17:04:02 +0800544 ret = lp5523_load_program(engine, pattern);
Samu Onkalo0efba162010-11-11 14:05:22 -0800545 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
Jingoo Haneef4aa62012-11-18 20:51:47 -0800668 if (kstrtoul(buf, 0, &curr))
Samu Onkalo0efba162010-11-11 14:05:22 -0800669 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 */
Vasiliy Kulikovccd75102011-03-22 16:30:20 -0700689static DEVICE_ATTR(led_current, S_IRUGO | S_IWUSR, show_current, store_current);
Samu Onkalo0efba162010-11-11 14:05:22 -0800690static 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 */
Vasiliy Kulikovccd75102011-03-22 16:30:20 -0700703static DEVICE_ATTR(engine1_mode, S_IRUGO | S_IWUSR,
Samu Onkalo0efba162010-11-11 14:05:22 -0800704 show_engine1_mode, store_engine1_mode);
Vasiliy Kulikovccd75102011-03-22 16:30:20 -0700705static DEVICE_ATTR(engine2_mode, S_IRUGO | S_IWUSR,
Samu Onkalo0efba162010-11-11 14:05:22 -0800706 show_engine2_mode, store_engine2_mode);
Vasiliy Kulikovccd75102011-03-22 16:30:20 -0700707static DEVICE_ATTR(engine3_mode, S_IRUGO | S_IWUSR,
Samu Onkalo0efba162010-11-11 14:05:22 -0800708 show_engine3_mode, store_engine3_mode);
Vasiliy Kulikovccd75102011-03-22 16:30:20 -0700709static DEVICE_ATTR(engine1_leds, S_IRUGO | S_IWUSR,
Samu Onkalo0efba162010-11-11 14:05:22 -0800710 show_engine1_leds, store_engine1_leds);
Vasiliy Kulikovccd75102011-03-22 16:30:20 -0700711static DEVICE_ATTR(engine2_leds, S_IRUGO | S_IWUSR,
Samu Onkalo0efba162010-11-11 14:05:22 -0800712 show_engine2_leds, store_engine2_leds);
Vasiliy Kulikovccd75102011-03-22 16:30:20 -0700713static DEVICE_ATTR(engine3_leds, S_IRUGO | S_IWUSR,
Samu Onkalo0efba162010-11-11 14:05:22 -0800714 show_engine3_leds, store_engine3_leds);
Vasiliy Kulikovccd75102011-03-22 16:30:20 -0700715static 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);
Samu Onkalo0efba162010-11-11 14:05:22 -0800718static 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,
Samu Onkalo0efba162010-11-11 14:05:22 -0800725 &dev_attr_engine1_load.attr,
726 &dev_attr_engine1_leds.attr,
Samu Onkalo0efba162010-11-11 14:05:22 -0800727 &dev_attr_engine2_load.attr,
728 &dev_attr_engine2_leds.attr,
Samu Onkalo0efba162010-11-11 14:05:22 -0800729 &dev_attr_engine3_load.attr,
730 &dev_attr_engine3_leds.attr,
Kim, Milof1625842012-09-12 20:16:03 +0800731 NULL,
Samu Onkalo0efba162010-11-11 14:05:22 -0800732};
733
734static const struct attribute_group lp5523_group = {
735 .attrs = lp5523_attributes,
736};
737
Samu Onkalo0efba162010-11-11 14:05:22 -0800738static 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
Samu Onkalo0efba162010-11-11 14:05:22 -0800758 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/*--------------------------------------------------------------*/
Kim, Milo6f6365f2012-08-21 17:03:58 +0800766static void lp5523_set_mode(struct lp5523_engine *engine, u8 mode)
Samu Onkalo0efba162010-11-11 14:05:22 -0800767{
Samu Onkalo0efba162010-11-11 14:05:22 -0800768 /* if in that mode already do nothing, except for run */
769 if (mode == engine->mode && mode != LP5523_CMD_RUN)
Kim, Milo6f6365f2012-08-21 17:03:58 +0800770 return;
Samu Onkalo0efba162010-11-11 14:05:22 -0800771
Kim, Milo6f6365f2012-08-21 17:03:58 +0800772 switch (mode) {
773 case LP5523_CMD_RUN:
774 lp5523_run_program(engine);
775 break;
776 case LP5523_CMD_LOAD:
Samu Onkalo0efba162010-11-11 14:05:22 -0800777 lp5523_set_engine_mode(engine, LP5523_CMD_DISABLED);
778 lp5523_set_engine_mode(engine, LP5523_CMD_LOAD);
Kim, Milo6f6365f2012-08-21 17:03:58 +0800779 break;
780 case LP5523_CMD_DISABLED:
Samu Onkalo0efba162010-11-11 14:05:22 -0800781 lp5523_set_engine_mode(engine, LP5523_CMD_DISABLED);
Kim, Milo6f6365f2012-08-21 17:03:58 +0800782 break;
783 default:
784 return;
Samu Onkalo0efba162010-11-11 14:05:22 -0800785 }
786
Samu Onkalo0efba162010-11-11 14:05:22 -0800787 engine->mode = mode;
Samu Onkalo0efba162010-11-11 14:05:22 -0800788}
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;
Samu Onkalo0efba162010-11-11 14:05:22 -0800801
802 return 0;
803}
804
Bill Pemberton98ea1ea2012-11-19 13:23:02 -0500805static int lp5523_init_led(struct lp5523_led *led, struct device *dev,
Kim, Milo56a1e9a2012-09-04 15:06:26 +0800806 int chan, struct lp5523_platform_data *pdata,
807 const char *chip_name)
Samu Onkalo0efba162010-11-11 14:05:22 -0800808{
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
Kim, Milo94b43b62012-08-22 15:32:29 +0800826 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",
Kim, Milo56a1e9a2012-09-04 15:06:26 +0800830 pdata->label ? : chip_name, chan);
Kim, Milo94b43b62012-08-22 15:32:29 +0800831 led->cdev.name = name;
832 }
Samu Onkalo0efba162010-11-11 14:05:22 -0800833
Samu Onkalo0efba162010-11-11 14:05:22 -0800834 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
Milo(Woogyom) Kimf6524802013-02-05 17:53:40 +0900854static 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
Milo(Woogyom) Kim1904f832013-02-05 17:56:23 +0900894static 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
Milo(Woogyom) Kimf6c64c62013-02-05 17:58:01 +0900904static void lp5523_deinit_device(struct lp5523_chip *chip);
Milo(Woogyom) Kim944f7b12013-02-05 17:49:46 +0900905static int lp5523_init_device(struct lp5523_chip *chip)
906{
Milo(Woogyom) Kim944f7b12013-02-05 17:49:46 +0900907 struct i2c_client *client = chip->client;
908 int ret;
909
Milo(Woogyom) Kimf6c64c62013-02-05 17:58:01 +0900910 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;
Milo(Woogyom) Kim944f7b12013-02-05 17:49:46 +0900926}
927
Milo(Woogyom) Kim1a9914852013-02-05 17:50:36 +0900928static 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
Milo(Woogyom) Kim48068d52013-02-05 18:08:49 +0900938/* Chip specific configurations */
939static struct lp55xx_device_config lp5523_cfg = {
940 .reset = {
941 .addr = LP5523_REG_RESET,
942 .val = LP5523_RESET,
943 },
944};
945
Bill Pemberton98ea1ea2012-11-19 13:23:02 -0500946static int lp5523_probe(struct i2c_client *client,
Samu Onkalo0efba162010-11-11 14:05:22 -0800947 const struct i2c_device_id *id)
948{
Milo(Woogyom) Kim6a0c9a42013-02-05 18:03:08 +0900949 struct lp5523_chip *old_chip = NULL;
Milo(Woogyom) Kimf6524802013-02-05 17:53:40 +0900950 int ret, i;
Milo(Woogyom) Kim6a0c9a42013-02-05 18:03:08 +0900951 struct lp55xx_chip *chip;
952 struct lp55xx_led *led;
953 struct lp55xx_platform_data *pdata = client->dev.platform_data;
Samu Onkalo0efba162010-11-11 14:05:22 -0800954
Milo(Woogyom) Kim6a0c9a42013-02-05 18:03:08 +0900955 if (!pdata) {
Samu Onkalo0efba162010-11-11 14:05:22 -0800956 dev_err(&client->dev, "no platform data\n");
Bryan Wu94ca4bc2012-07-04 11:44:18 +0800957 return -EINVAL;
Samu Onkalo0efba162010-11-11 14:05:22 -0800958 }
959
Milo(Woogyom) Kim6a0c9a42013-02-05 18:03:08 +0900960 chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
961 if (!chip)
962 return -ENOMEM;
Samu Onkalo0efba162010-11-11 14:05:22 -0800963
Milo(Woogyom) Kim6a0c9a42013-02-05 18:03:08 +0900964 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;
Milo(Woogyom) Kim48068d52013-02-05 18:08:49 +0900971 chip->cfg = &lp5523_cfg;
Milo(Woogyom) Kim6a0c9a42013-02-05 18:03:08 +0900972
973 mutex_init(&chip->lock);
974
975 i2c_set_clientdata(client, led);
Samu Onkalo0efba162010-11-11 14:05:22 -0800976
Milo(Woogyom) Kim945c7002013-02-05 18:02:26 +0900977 ret = lp5523_init_device(old_chip);
Samu Onkalo0efba162010-11-11 14:05:22 -0800978 if (ret)
Milo(Woogyom) Kimf6c64c62013-02-05 17:58:01 +0900979 goto err_init;
Samu Onkalo0efba162010-11-11 14:05:22 -0800980
Kim, Milo56a1e9a2012-09-04 15:06:26 +0800981 dev_info(&client->dev, "%s Programmable led chip found\n", id->name);
Samu Onkalo0efba162010-11-11 14:05:22 -0800982
983 /* Initialize engines */
Milo(Woogyom) Kim945c7002013-02-05 18:02:26 +0900984 for (i = 0; i < ARRAY_SIZE(old_chip->engines); i++) {
985 ret = lp5523_init_engine(&old_chip->engines[i], i + 1);
Samu Onkalo0efba162010-11-11 14:05:22 -0800986 if (ret) {
987 dev_err(&client->dev, "error initializing engine\n");
Bryan Wu94ca4bc2012-07-04 11:44:18 +0800988 goto fail1;
Samu Onkalo0efba162010-11-11 14:05:22 -0800989 }
990 }
Samu Onkalo0efba162010-11-11 14:05:22 -0800991
Milo(Woogyom) Kim945c7002013-02-05 18:02:26 +0900992 ret = lp5523_register_leds(old_chip, id->name);
Milo(Woogyom) Kimf6524802013-02-05 17:53:40 +0900993 if (ret)
994 goto fail2;
Samu Onkalo0efba162010-11-11 14:05:22 -0800995
996 ret = lp5523_register_sysfs(client);
997 if (ret) {
998 dev_err(&client->dev, "registering sysfs failed\n");
Bryan Wu94ca4bc2012-07-04 11:44:18 +0800999 goto fail2;
Samu Onkalo0efba162010-11-11 14:05:22 -08001000 }
1001 return ret;
Bryan Wu94ca4bc2012-07-04 11:44:18 +08001002fail2:
Milo(Woogyom) Kim945c7002013-02-05 18:02:26 +09001003 lp5523_unregister_leds(old_chip);
Bryan Wu94ca4bc2012-07-04 11:44:18 +08001004fail1:
Milo(Woogyom) Kim945c7002013-02-05 18:02:26 +09001005 lp5523_deinit_device(old_chip);
Milo(Woogyom) Kimf6c64c62013-02-05 17:58:01 +09001006err_init:
Samu Onkalo0efba162010-11-11 14:05:22 -08001007 return ret;
1008}
1009
1010static int lp5523_remove(struct i2c_client *client)
1011{
Milo(Woogyom) Kim945c7002013-02-05 18:02:26 +09001012 struct lp5523_chip *old_chip = i2c_get_clientdata(client);
Samu Onkalo0efba162010-11-11 14:05:22 -08001013
Kim, Milo23301b72012-09-12 20:16:00 +08001014 /* Disable engine mode */
1015 lp5523_write(client, LP5523_REG_OP_MODE, LP5523_CMD_DISABLED);
1016
Samu Onkalo0efba162010-11-11 14:05:22 -08001017 lp5523_unregister_sysfs(client);
1018
Milo(Woogyom) Kim945c7002013-02-05 18:02:26 +09001019 lp5523_unregister_leds(old_chip);
Samu Onkalo0efba162010-11-11 14:05:22 -08001020
Milo(Woogyom) Kim945c7002013-02-05 18:02:26 +09001021 lp5523_deinit_device(old_chip);
Samu Onkalo0efba162010-11-11 14:05:22 -08001022 return 0;
1023}
1024
1025static const struct i2c_device_id lp5523_id[] = {
Kim, Milo27d77042012-09-04 15:06:18 +08001026 { "lp5523", LP5523 },
1027 { "lp55231", LP55231 },
Samu Onkalo0efba162010-11-11 14:05:22 -08001028 { }
1029};
1030
1031MODULE_DEVICE_TABLE(i2c, lp5523_id);
1032
1033static struct i2c_driver lp5523_driver = {
1034 .driver = {
Kim, Milo27d77042012-09-04 15:06:18 +08001035 .name = "lp5523x",
Samu Onkalo0efba162010-11-11 14:05:22 -08001036 },
1037 .probe = lp5523_probe,
1038 .remove = lp5523_remove,
1039 .id_table = lp5523_id,
1040};
1041
Axel Lin09a0d182012-01-10 15:09:27 -08001042module_i2c_driver(lp5523_driver);
Samu Onkalo0efba162010-11-11 14:05:22 -08001043
1044MODULE_AUTHOR("Mathias Nyman <mathias.nyman@nokia.com>");
1045MODULE_DESCRIPTION("LP5523 LED engine");
1046MODULE_LICENSE("GPL");