]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/leds/leds-lp5521.c
leds-lp55xx: use lp55xx common init function - platform data
[mirror_ubuntu-bionic-kernel.git] / drivers / leds / leds-lp5521.c
CommitLineData
500fe141
SO
1/*
2 * LP5521 LED chip 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-lp5521.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"
500fe141
SO
40
41#define LP5521_PROGRAM_LENGTH 32 /* in bytes */
42
43#define LP5521_MAX_LEDS 3 /* Maximum number of LEDs */
44#define LP5521_MAX_ENGINES 3 /* Maximum number of engines */
45
46#define LP5521_ENG_MASK_BASE 0x30 /* 00110000 */
47#define LP5521_ENG_STATUS_MASK 0x07 /* 00000111 */
48
49#define LP5521_CMD_LOAD 0x15 /* 00010101 */
50#define LP5521_CMD_RUN 0x2a /* 00101010 */
51#define LP5521_CMD_DIRECT 0x3f /* 00111111 */
52#define LP5521_CMD_DISABLED 0x00 /* 00000000 */
53
54/* Registers */
55#define LP5521_REG_ENABLE 0x00
56#define LP5521_REG_OP_MODE 0x01
57#define LP5521_REG_R_PWM 0x02
58#define LP5521_REG_G_PWM 0x03
59#define LP5521_REG_B_PWM 0x04
60#define LP5521_REG_R_CURRENT 0x05
61#define LP5521_REG_G_CURRENT 0x06
62#define LP5521_REG_B_CURRENT 0x07
63#define LP5521_REG_CONFIG 0x08
64#define LP5521_REG_R_CHANNEL_PC 0x09
65#define LP5521_REG_G_CHANNEL_PC 0x0A
66#define LP5521_REG_B_CHANNEL_PC 0x0B
67#define LP5521_REG_STATUS 0x0C
68#define LP5521_REG_RESET 0x0D
69#define LP5521_REG_GPO 0x0E
70#define LP5521_REG_R_PROG_MEM 0x10
71#define LP5521_REG_G_PROG_MEM 0x30
72#define LP5521_REG_B_PROG_MEM 0x50
73
74#define LP5521_PROG_MEM_BASE LP5521_REG_R_PROG_MEM
75#define LP5521_PROG_MEM_SIZE 0x20
76
77/* Base register to set LED current */
78#define LP5521_REG_LED_CURRENT_BASE LP5521_REG_R_CURRENT
79
80/* Base register to set the brightness */
81#define LP5521_REG_LED_PWM_BASE LP5521_REG_R_PWM
82
83/* Bits in ENABLE register */
84#define LP5521_MASTER_ENABLE 0x40 /* Chip master enable */
85#define LP5521_LOGARITHMIC_PWM 0x80 /* Logarithmic PWM adjustment */
86#define LP5521_EXEC_RUN 0x2A
32a2f747
KM
87#define LP5521_ENABLE_DEFAULT \
88 (LP5521_MASTER_ENABLE | LP5521_LOGARITHMIC_PWM)
89#define LP5521_ENABLE_RUN_PROGRAM \
90 (LP5521_ENABLE_DEFAULT | LP5521_EXEC_RUN)
500fe141 91
500fe141
SO
92/* Status */
93#define LP5521_EXT_CLK_USED 0x08
94
b3c49c05
SK
95/* default R channel current register value */
96#define LP5521_REG_R_CURR_DEFAULT 0xAF
97
011af7bc
KM
98/* Pattern Mode */
99#define PATTERN_OFF 0
100
500fe141 101struct lp5521_engine {
500fe141
SO
102 int id;
103 u8 mode;
104 u8 prog_page;
105 u8 engine_mask;
106};
107
108struct lp5521_led {
109 int id;
110 u8 chan_nr;
111 u8 led_current;
112 u8 max_current;
113 struct led_classdev cdev;
114 struct work_struct brightness_work;
115 u8 brightness;
116};
117
118struct lp5521_chip {
119 struct lp5521_platform_data *pdata;
120 struct mutex lock; /* Serialize control */
121 struct i2c_client *client;
122 struct lp5521_engine engines[LP5521_MAX_ENGINES];
123 struct lp5521_led leds[LP5521_MAX_LEDS];
124 u8 num_channels;
125 u8 num_leds;
126};
127
94482174
MWK
128static inline void lp5521_wait_enable_done(void)
129{
130 /* it takes more 488 us to update ENABLE register */
131 usleep_range(500, 600);
132}
133
9fdb18b6
SO
134static inline struct lp5521_led *cdev_to_led(struct led_classdev *cdev)
135{
136 return container_of(cdev, struct lp5521_led, cdev);
137}
138
139static inline struct lp5521_chip *engine_to_lp5521(struct lp5521_engine *engine)
140{
141 return container_of(engine, struct lp5521_chip,
142 engines[engine->id - 1]);
143}
144
145static inline struct lp5521_chip *led_to_lp5521(struct lp5521_led *led)
146{
147 return container_of(led, struct lp5521_chip,
148 leds[led->id]);
149}
500fe141
SO
150
151static void lp5521_led_brightness_work(struct work_struct *work);
152
153static inline int lp5521_write(struct i2c_client *client, u8 reg, u8 value)
154{
155 return i2c_smbus_write_byte_data(client, reg, value);
156}
157
158static int lp5521_read(struct i2c_client *client, u8 reg, u8 *buf)
159{
160 s32 ret;
161
162 ret = i2c_smbus_read_byte_data(client, reg);
163 if (ret < 0)
313e8b50 164 return ret;
500fe141
SO
165
166 *buf = ret;
167 return 0;
168}
169
170static int lp5521_set_engine_mode(struct lp5521_engine *engine, u8 mode)
171{
172 struct lp5521_chip *chip = engine_to_lp5521(engine);
173 struct i2c_client *client = chip->client;
174 int ret;
175 u8 engine_state;
176
177 /* Only transition between RUN and DIRECT mode are handled here */
178 if (mode == LP5521_CMD_LOAD)
179 return 0;
180
181 if (mode == LP5521_CMD_DISABLED)
182 mode = LP5521_CMD_DIRECT;
183
184 ret = lp5521_read(client, LP5521_REG_OP_MODE, &engine_state);
fa0ea0e1
AL
185 if (ret < 0)
186 return ret;
500fe141
SO
187
188 /* set mode only for this engine */
189 engine_state &= ~(engine->engine_mask);
190 mode &= engine->engine_mask;
191 engine_state |= mode;
fa0ea0e1 192 return lp5521_write(client, LP5521_REG_OP_MODE, engine_state);
500fe141
SO
193}
194
195static int lp5521_load_program(struct lp5521_engine *eng, const u8 *pattern)
196{
197 struct lp5521_chip *chip = engine_to_lp5521(eng);
198 struct i2c_client *client = chip->client;
199 int ret;
200 int addr;
201 u8 mode;
202
203 /* move current engine to direct mode and remember the state */
204 ret = lp5521_set_engine_mode(eng, LP5521_CMD_DIRECT);
5bc9ad77
DC
205 if (ret)
206 return ret;
207
09c76b0f
SO
208 /* Mode change requires min 500 us delay. 1 - 2 ms with margin */
209 usleep_range(1000, 2000);
5bc9ad77
DC
210 ret = lp5521_read(client, LP5521_REG_OP_MODE, &mode);
211 if (ret)
212 return ret;
500fe141
SO
213
214 /* For loading, all the engines to load mode */
215 lp5521_write(client, LP5521_REG_OP_MODE, LP5521_CMD_DIRECT);
09c76b0f
SO
216 /* Mode change requires min 500 us delay. 1 - 2 ms with margin */
217 usleep_range(1000, 2000);
500fe141 218 lp5521_write(client, LP5521_REG_OP_MODE, LP5521_CMD_LOAD);
09c76b0f
SO
219 /* Mode change requires min 500 us delay. 1 - 2 ms with margin */
220 usleep_range(1000, 2000);
500fe141
SO
221
222 addr = LP5521_PROG_MEM_BASE + eng->prog_page * LP5521_PROG_MEM_SIZE;
223 i2c_smbus_write_i2c_block_data(client,
224 addr,
225 LP5521_PROG_MEM_SIZE,
226 pattern);
227
5bc9ad77 228 return lp5521_write(client, LP5521_REG_OP_MODE, mode);
500fe141
SO
229}
230
231static int lp5521_set_led_current(struct lp5521_chip *chip, int led, u8 curr)
232{
233 return lp5521_write(chip->client,
234 LP5521_REG_LED_CURRENT_BASE + chip->leds[led].chan_nr,
235 curr);
236}
237
d4e7ad03 238static int lp5521_configure(struct i2c_client *client)
500fe141
SO
239{
240 struct lp5521_chip *chip = i2c_get_clientdata(client);
241 int ret;
3b49aacd 242 u8 cfg;
94482174 243 u8 val;
500fe141 244
94482174
MWK
245 /*
246 * Make sure that the chip is reset by reading back the r channel
247 * current reg. This is dummy read is required on some platforms -
248 * otherwise further access to the R G B channels in the
249 * LP5521_REG_ENABLE register will not have any effect - strange!
250 */
251 ret = lp5521_read(client, LP5521_REG_R_CURRENT, &val);
252 if (ret) {
253 dev_err(&client->dev, "error in resetting chip\n");
254 return ret;
255 }
256 if (val != LP5521_REG_R_CURR_DEFAULT) {
257 dev_err(&client->dev,
258 "unexpected data in register (expected 0x%x got 0x%x)\n",
259 LP5521_REG_R_CURR_DEFAULT, val);
260 ret = -EINVAL;
261 return ret;
262 }
263 usleep_range(10000, 20000);
500fe141 264
500fe141 265 /* Set all PWMs to direct control mode */
32a2f747 266 ret = lp5521_write(client, LP5521_REG_OP_MODE, LP5521_CMD_DIRECT);
500fe141 267
3b49aacd
KM
268 cfg = chip->pdata->update_config ?
269 : (LP5521_PWRSAVE_EN | LP5521_CP_MODE_AUTO | LP5521_R_TO_BATT);
94482174
MWK
270 ret = lp5521_write(client, LP5521_REG_CONFIG, cfg);
271 if (ret)
272 return ret;
500fe141
SO
273
274 /* Initialize all channels PWM to zero -> leds off */
94482174
MWK
275 lp5521_write(client, LP5521_REG_R_PWM, 0);
276 lp5521_write(client, LP5521_REG_G_PWM, 0);
277 lp5521_write(client, LP5521_REG_B_PWM, 0);
500fe141
SO
278
279 /* Set engines are set to run state when OP_MODE enables engines */
94482174 280 ret = lp5521_write(client, LP5521_REG_ENABLE,
32a2f747 281 LP5521_ENABLE_RUN_PROGRAM);
94482174
MWK
282 if (ret)
283 return ret;
500fe141 284
94482174
MWK
285 lp5521_wait_enable_done();
286
287 return 0;
500fe141
SO
288}
289
290static int lp5521_run_selftest(struct lp5521_chip *chip, char *buf)
291{
292 int ret;
293 u8 status;
294
295 ret = lp5521_read(chip->client, LP5521_REG_STATUS, &status);
296 if (ret < 0)
297 return ret;
298
299 /* Check that ext clock is really in use if requested */
300 if (chip->pdata && chip->pdata->clock_mode == LP5521_CLOCK_EXT)
301 if ((status & LP5521_EXT_CLK_USED) == 0)
302 return -EIO;
303 return 0;
304}
305
306static void lp5521_set_brightness(struct led_classdev *cdev,
307 enum led_brightness brightness)
308{
309 struct lp5521_led *led = cdev_to_led(cdev);
310 led->brightness = (u8)brightness;
311 schedule_work(&led->brightness_work);
312}
313
314static void lp5521_led_brightness_work(struct work_struct *work)
315{
316 struct lp5521_led *led = container_of(work,
317 struct lp5521_led,
318 brightness_work);
319 struct lp5521_chip *chip = led_to_lp5521(led);
320 struct i2c_client *client = chip->client;
321
322 mutex_lock(&chip->lock);
323 lp5521_write(client, LP5521_REG_LED_PWM_BASE + led->chan_nr,
324 led->brightness);
325 mutex_unlock(&chip->lock);
326}
327
328/* Detect the chip by setting its ENABLE register and reading it back. */
329static int lp5521_detect(struct i2c_client *client)
330{
331 int ret;
332 u8 buf;
333
32a2f747 334 ret = lp5521_write(client, LP5521_REG_ENABLE, LP5521_ENABLE_DEFAULT);
500fe141
SO
335 if (ret)
336 return ret;
09c76b0f
SO
337 /* enable takes 500us. 1 - 2 ms leaves some margin */
338 usleep_range(1000, 2000);
500fe141
SO
339 ret = lp5521_read(client, LP5521_REG_ENABLE, &buf);
340 if (ret)
341 return ret;
32a2f747 342 if (buf != LP5521_ENABLE_DEFAULT)
500fe141
SO
343 return -ENODEV;
344
345 return 0;
346}
347
348/* Set engine mode and create appropriate sysfs attributes, if required. */
349static int lp5521_set_mode(struct lp5521_engine *engine, u8 mode)
350{
500fe141
SO
351 int ret = 0;
352
353 /* if in that mode already do nothing, except for run */
354 if (mode == engine->mode && mode != LP5521_CMD_RUN)
355 return 0;
356
357 if (mode == LP5521_CMD_RUN) {
358 ret = lp5521_set_engine_mode(engine, LP5521_CMD_RUN);
359 } else if (mode == LP5521_CMD_LOAD) {
360 lp5521_set_engine_mode(engine, LP5521_CMD_DISABLED);
361 lp5521_set_engine_mode(engine, LP5521_CMD_LOAD);
500fe141
SO
362 } else if (mode == LP5521_CMD_DISABLED) {
363 lp5521_set_engine_mode(engine, LP5521_CMD_DISABLED);
364 }
365
500fe141
SO
366 engine->mode = mode;
367
368 return ret;
369}
370
371static int lp5521_do_store_load(struct lp5521_engine *engine,
372 const char *buf, size_t len)
373{
374 struct lp5521_chip *chip = engine_to_lp5521(engine);
375 struct i2c_client *client = chip->client;
376 int ret, nrchars, offset = 0, i = 0;
377 char c[3];
378 unsigned cmd;
379 u8 pattern[LP5521_PROGRAM_LENGTH] = {0};
380
381 while ((offset < len - 1) && (i < LP5521_PROGRAM_LENGTH)) {
382 /* separate sscanfs because length is working only for %s */
383 ret = sscanf(buf + offset, "%2s%n ", c, &nrchars);
2260209c
VK
384 if (ret != 2)
385 goto fail;
500fe141
SO
386 ret = sscanf(c, "%2x", &cmd);
387 if (ret != 1)
388 goto fail;
389 pattern[i] = (u8)cmd;
390
391 offset += nrchars;
392 i++;
393 }
394
395 /* Each instruction is 16bit long. Check that length is even */
396 if (i % 2)
397 goto fail;
398
399 mutex_lock(&chip->lock);
d4e7ad03
SO
400 if (engine->mode == LP5521_CMD_LOAD)
401 ret = lp5521_load_program(engine, pattern);
402 else
403 ret = -EINVAL;
500fe141
SO
404 mutex_unlock(&chip->lock);
405
406 if (ret) {
407 dev_err(&client->dev, "failed loading pattern\n");
408 return ret;
409 }
410
411 return len;
412fail:
413 dev_err(&client->dev, "wrong pattern format\n");
414 return -EINVAL;
415}
416
417static ssize_t store_engine_load(struct device *dev,
418 struct device_attribute *attr,
419 const char *buf, size_t len, int nr)
420{
421 struct i2c_client *client = to_i2c_client(dev);
422 struct lp5521_chip *chip = i2c_get_clientdata(client);
423 return lp5521_do_store_load(&chip->engines[nr - 1], buf, len);
424}
425
426#define store_load(nr) \
427static ssize_t store_engine##nr##_load(struct device *dev, \
428 struct device_attribute *attr, \
429 const char *buf, size_t len) \
430{ \
431 return store_engine_load(dev, attr, buf, len, nr); \
432}
433store_load(1)
434store_load(2)
435store_load(3)
436
437static ssize_t show_engine_mode(struct device *dev,
438 struct device_attribute *attr,
439 char *buf, int nr)
440{
441 struct i2c_client *client = to_i2c_client(dev);
442 struct lp5521_chip *chip = i2c_get_clientdata(client);
443 switch (chip->engines[nr - 1].mode) {
444 case LP5521_CMD_RUN:
445 return sprintf(buf, "run\n");
446 case LP5521_CMD_LOAD:
447 return sprintf(buf, "load\n");
448 case LP5521_CMD_DISABLED:
449 return sprintf(buf, "disabled\n");
450 default:
451 return sprintf(buf, "disabled\n");
452 }
453}
454
455#define show_mode(nr) \
456static ssize_t show_engine##nr##_mode(struct device *dev, \
457 struct device_attribute *attr, \
458 char *buf) \
459{ \
460 return show_engine_mode(dev, attr, buf, nr); \
461}
462show_mode(1)
463show_mode(2)
464show_mode(3)
465
466static ssize_t store_engine_mode(struct device *dev,
467 struct device_attribute *attr,
468 const char *buf, size_t len, int nr)
469{
470 struct i2c_client *client = to_i2c_client(dev);
471 struct lp5521_chip *chip = i2c_get_clientdata(client);
472 struct lp5521_engine *engine = &chip->engines[nr - 1];
473 mutex_lock(&chip->lock);
474
475 if (!strncmp(buf, "run", 3))
476 lp5521_set_mode(engine, LP5521_CMD_RUN);
477 else if (!strncmp(buf, "load", 4))
478 lp5521_set_mode(engine, LP5521_CMD_LOAD);
479 else if (!strncmp(buf, "disabled", 8))
480 lp5521_set_mode(engine, LP5521_CMD_DISABLED);
481
482 mutex_unlock(&chip->lock);
483 return len;
484}
485
486#define store_mode(nr) \
487static ssize_t store_engine##nr##_mode(struct device *dev, \
488 struct device_attribute *attr, \
489 const char *buf, size_t len) \
490{ \
491 return store_engine_mode(dev, attr, buf, len, nr); \
492}
493store_mode(1)
494store_mode(2)
495store_mode(3)
496
497static ssize_t show_max_current(struct device *dev,
498 struct device_attribute *attr,
499 char *buf)
500{
501 struct led_classdev *led_cdev = dev_get_drvdata(dev);
502 struct lp5521_led *led = cdev_to_led(led_cdev);
503
504 return sprintf(buf, "%d\n", led->max_current);
505}
506
507static ssize_t show_current(struct device *dev,
508 struct device_attribute *attr,
509 char *buf)
510{
511 struct led_classdev *led_cdev = dev_get_drvdata(dev);
512 struct lp5521_led *led = cdev_to_led(led_cdev);
513
514 return sprintf(buf, "%d\n", led->led_current);
515}
516
517static ssize_t store_current(struct device *dev,
518 struct device_attribute *attr,
519 const char *buf, size_t len)
520{
521 struct led_classdev *led_cdev = dev_get_drvdata(dev);
522 struct lp5521_led *led = cdev_to_led(led_cdev);
523 struct lp5521_chip *chip = led_to_lp5521(led);
524 ssize_t ret;
525 unsigned long curr;
526
011af7bc 527 if (kstrtoul(buf, 0, &curr))
500fe141
SO
528 return -EINVAL;
529
530 if (curr > led->max_current)
531 return -EINVAL;
532
533 mutex_lock(&chip->lock);
534 ret = lp5521_set_led_current(chip, led->id, curr);
535 mutex_unlock(&chip->lock);
536
537 if (ret < 0)
538 return ret;
539
540 led->led_current = (u8)curr;
541
542 return len;
543}
544
545static ssize_t lp5521_selftest(struct device *dev,
546 struct device_attribute *attr,
547 char *buf)
548{
549 struct i2c_client *client = to_i2c_client(dev);
550 struct lp5521_chip *chip = i2c_get_clientdata(client);
551 int ret;
552
553 mutex_lock(&chip->lock);
554 ret = lp5521_run_selftest(chip, buf);
555 mutex_unlock(&chip->lock);
556 return sprintf(buf, "%s\n", ret ? "FAIL" : "OK");
557}
558
011af7bc
KM
559static void lp5521_clear_program_memory(struct i2c_client *cl)
560{
561 int i;
562 u8 rgb_mem[] = {
563 LP5521_REG_R_PROG_MEM,
564 LP5521_REG_G_PROG_MEM,
565 LP5521_REG_B_PROG_MEM,
566 };
567
568 for (i = 0; i < ARRAY_SIZE(rgb_mem); i++) {
569 lp5521_write(cl, rgb_mem[i], 0);
570 lp5521_write(cl, rgb_mem[i] + 1, 0);
571 }
572}
573
574static void lp5521_write_program_memory(struct i2c_client *cl,
575 u8 base, u8 *rgb, int size)
576{
577 int i;
578
579 if (!rgb || size <= 0)
580 return;
581
582 for (i = 0; i < size; i++)
583 lp5521_write(cl, base + i, *(rgb + i));
584
585 lp5521_write(cl, base + i, 0);
586 lp5521_write(cl, base + i + 1, 0);
587}
588
589static inline struct lp5521_led_pattern *lp5521_get_pattern
590 (struct lp5521_chip *chip, u8 offset)
591{
592 struct lp5521_led_pattern *ptn;
593 ptn = chip->pdata->patterns + (offset - 1);
594 return ptn;
595}
596
597static void lp5521_run_led_pattern(int mode, struct lp5521_chip *chip)
598{
599 struct lp5521_led_pattern *ptn;
600 struct i2c_client *cl = chip->client;
601 int num_patterns = chip->pdata->num_patterns;
602
603 if (mode > num_patterns || !(chip->pdata->patterns))
604 return;
605
606 if (mode == PATTERN_OFF) {
32a2f747 607 lp5521_write(cl, LP5521_REG_ENABLE, LP5521_ENABLE_DEFAULT);
011af7bc
KM
608 usleep_range(1000, 2000);
609 lp5521_write(cl, LP5521_REG_OP_MODE, LP5521_CMD_DIRECT);
610 } else {
611 ptn = lp5521_get_pattern(chip, mode);
612 if (!ptn)
613 return;
614
615 lp5521_write(cl, LP5521_REG_OP_MODE, LP5521_CMD_LOAD);
616 usleep_range(1000, 2000);
617
618 lp5521_clear_program_memory(cl);
619
620 lp5521_write_program_memory(cl, LP5521_REG_R_PROG_MEM,
621 ptn->r, ptn->size_r);
622 lp5521_write_program_memory(cl, LP5521_REG_G_PROG_MEM,
623 ptn->g, ptn->size_g);
624 lp5521_write_program_memory(cl, LP5521_REG_B_PROG_MEM,
625 ptn->b, ptn->size_b);
626
627 lp5521_write(cl, LP5521_REG_OP_MODE, LP5521_CMD_RUN);
628 usleep_range(1000, 2000);
32a2f747 629 lp5521_write(cl, LP5521_REG_ENABLE, LP5521_ENABLE_RUN_PROGRAM);
011af7bc
KM
630 }
631}
632
633static ssize_t store_led_pattern(struct device *dev,
634 struct device_attribute *attr,
635 const char *buf, size_t len)
636{
637 struct lp5521_chip *chip = i2c_get_clientdata(to_i2c_client(dev));
638 unsigned long val;
639 int ret;
640
69b44c16 641 ret = kstrtoul(buf, 16, &val);
011af7bc
KM
642 if (ret)
643 return ret;
644
645 lp5521_run_led_pattern(val, chip);
646
647 return len;
648}
649
500fe141 650/* led class device attributes */
67d1da79 651static DEVICE_ATTR(led_current, S_IRUGO | S_IWUSR, show_current, store_current);
500fe141
SO
652static DEVICE_ATTR(max_current, S_IRUGO , show_max_current, NULL);
653
654static struct attribute *lp5521_led_attributes[] = {
655 &dev_attr_led_current.attr,
656 &dev_attr_max_current.attr,
657 NULL,
658};
659
660static struct attribute_group lp5521_led_attribute_group = {
661 .attrs = lp5521_led_attributes
662};
663
664/* device attributes */
67d1da79 665static DEVICE_ATTR(engine1_mode, S_IRUGO | S_IWUSR,
500fe141 666 show_engine1_mode, store_engine1_mode);
67d1da79 667static DEVICE_ATTR(engine2_mode, S_IRUGO | S_IWUSR,
500fe141 668 show_engine2_mode, store_engine2_mode);
67d1da79 669static DEVICE_ATTR(engine3_mode, S_IRUGO | S_IWUSR,
500fe141 670 show_engine3_mode, store_engine3_mode);
67d1da79
VK
671static DEVICE_ATTR(engine1_load, S_IWUSR, NULL, store_engine1_load);
672static DEVICE_ATTR(engine2_load, S_IWUSR, NULL, store_engine2_load);
673static DEVICE_ATTR(engine3_load, S_IWUSR, NULL, store_engine3_load);
500fe141 674static DEVICE_ATTR(selftest, S_IRUGO, lp5521_selftest, NULL);
011af7bc 675static DEVICE_ATTR(led_pattern, S_IWUSR, NULL, store_led_pattern);
500fe141
SO
676
677static struct attribute *lp5521_attributes[] = {
678 &dev_attr_engine1_mode.attr,
679 &dev_attr_engine2_mode.attr,
680 &dev_attr_engine3_mode.attr,
681 &dev_attr_selftest.attr,
500fe141 682 &dev_attr_engine1_load.attr,
500fe141 683 &dev_attr_engine2_load.attr,
500fe141 684 &dev_attr_engine3_load.attr,
011af7bc 685 &dev_attr_led_pattern.attr,
500fe141
SO
686 NULL
687};
688
689static const struct attribute_group lp5521_group = {
690 .attrs = lp5521_attributes,
691};
692
500fe141
SO
693static int lp5521_register_sysfs(struct i2c_client *client)
694{
695 struct device *dev = &client->dev;
696 return sysfs_create_group(&dev->kobj, &lp5521_group);
697}
698
699static void lp5521_unregister_sysfs(struct i2c_client *client)
700{
701 struct lp5521_chip *chip = i2c_get_clientdata(client);
702 struct device *dev = &client->dev;
703 int i;
704
705 sysfs_remove_group(&dev->kobj, &lp5521_group);
706
500fe141
SO
707 for (i = 0; i < chip->num_leds; i++)
708 sysfs_remove_group(&chip->leds[i].cdev.dev->kobj,
709 &lp5521_led_attribute_group);
710}
711
86eb7748
MWK
712static void lp5521_reset_device(struct lp5521_chip *chip)
713{
714 struct i2c_client *client = chip->client;
715
716 lp5521_write(client, LP5521_REG_RESET, 0xff);
717}
718
f6c64c6f 719static void lp5521_deinit_device(struct lp5521_chip *chip);
944f7b1d
MWK
720static int lp5521_init_device(struct lp5521_chip *chip)
721{
944f7b1d
MWK
722 struct i2c_client *client = chip->client;
723 int ret;
944f7b1d 724
86eb7748
MWK
725 lp5521_reset_device(chip);
726
944f7b1d
MWK
727 usleep_range(10000, 20000); /*
728 * Exact value is not available. 10 - 20ms
729 * appears to be enough for reset.
730 */
731
944f7b1d 732 ret = lp5521_detect(client);
f6c64c6f 733 if (ret) {
944f7b1d 734 dev_err(&client->dev, "Chip not found\n");
f6c64c6f
MWK
735 goto err;
736 }
737
738 ret = lp5521_configure(client);
739 if (ret < 0) {
740 dev_err(&client->dev, "error configuring chip\n");
741 goto err_config;
742 }
944f7b1d 743
f6c64c6f
MWK
744 return 0;
745
746err_config:
747 lp5521_deinit_device(chip);
748err:
944f7b1d
MWK
749 return ret;
750}
751
1a991485
MWK
752static void lp5521_deinit_device(struct lp5521_chip *chip)
753{
754 struct lp5521_platform_data *pdata = chip->pdata;
755
756 if (pdata->enable)
757 pdata->enable(0);
758 if (pdata->release_resources)
759 pdata->release_resources();
760}
761
98ea1ea2 762static int lp5521_init_led(struct lp5521_led *led,
500fe141
SO
763 struct i2c_client *client,
764 int chan, struct lp5521_platform_data *pdata)
765{
766 struct device *dev = &client->dev;
767 char name[32];
768 int res;
769
770 if (chan >= LP5521_MAX_LEDS)
771 return -EINVAL;
772
773 if (pdata->led_config[chan].led_current == 0)
774 return 0;
775
776 led->led_current = pdata->led_config[chan].led_current;
777 led->max_current = pdata->led_config[chan].max_current;
778 led->chan_nr = pdata->led_config[chan].chan_nr;
779
780 if (led->chan_nr >= LP5521_MAX_LEDS) {
781 dev_err(dev, "Use channel numbers between 0 and %d\n",
782 LP5521_MAX_LEDS - 1);
783 return -EINVAL;
784 }
785
500fe141 786 led->cdev.brightness_set = lp5521_set_brightness;
5ae4e8a7
KM
787 if (pdata->led_config[chan].name) {
788 led->cdev.name = pdata->led_config[chan].name;
789 } else {
790 snprintf(name, sizeof(name), "%s:channel%d",
791 pdata->label ?: client->name, chan);
792 led->cdev.name = name;
793 }
794
500fe141
SO
795 res = led_classdev_register(dev, &led->cdev);
796 if (res < 0) {
797 dev_err(dev, "couldn't register led on channel %d\n", chan);
798 return res;
799 }
800
801 res = sysfs_create_group(&led->cdev.dev->kobj,
802 &lp5521_led_attribute_group);
803 if (res < 0) {
804 dev_err(dev, "couldn't register current attribute\n");
805 led_classdev_unregister(&led->cdev);
806 return res;
807 }
808 return 0;
809}
810
f6524808
MWK
811static int lp5521_register_leds(struct lp5521_chip *chip)
812{
813 struct lp5521_platform_data *pdata = chip->pdata;
814 struct i2c_client *client = chip->client;
815 int i;
816 int led;
817 int ret;
818
819 /* Initialize leds */
820 chip->num_channels = pdata->num_channels;
821 chip->num_leds = 0;
822 led = 0;
823 for (i = 0; i < pdata->num_channels; i++) {
824 /* Do not initialize channels that are not connected */
825 if (pdata->led_config[i].led_current == 0)
826 continue;
827
828 ret = lp5521_init_led(&chip->leds[led], client, i, pdata);
829 if (ret) {
830 dev_err(&client->dev, "error initializing leds\n");
831 return ret;
832 }
833 chip->num_leds++;
834
835 chip->leds[led].id = led;
836 /* Set initial LED current */
837 lp5521_set_led_current(chip, led,
838 chip->leds[led].led_current);
839
840 INIT_WORK(&(chip->leds[led].brightness_work),
841 lp5521_led_brightness_work);
842
843 led++;
844 }
845
846 return 0;
847}
848
1904f83d
MWK
849static void lp5521_unregister_leds(struct lp5521_chip *chip)
850{
851 int i;
852
853 for (i = 0; i < chip->num_leds; i++) {
854 led_classdev_unregister(&chip->leds[i].cdev);
855 cancel_work_sync(&chip->leds[i].brightness_work);
856 }
857}
858
98ea1ea2 859static int lp5521_probe(struct i2c_client *client,
500fe141
SO
860 const struct i2c_device_id *id)
861{
6a0c9a47 862 struct lp5521_chip *old_chip = NULL;
1904f83d 863 int ret;
6a0c9a47
MWK
864 struct lp55xx_chip *chip;
865 struct lp55xx_led *led;
866 struct lp55xx_platform_data *pdata = client->dev.platform_data;
500fe141 867
6a0c9a47 868 if (!pdata) {
500fe141 869 dev_err(&client->dev, "no platform data\n");
e430dc00 870 return -EINVAL;
500fe141
SO
871 }
872
6a0c9a47
MWK
873 chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
874 if (!chip)
875 return -ENOMEM;
876
877 led = devm_kzalloc(&client->dev,
878 sizeof(*led) * pdata->num_channels, GFP_KERNEL);
879 if (!led)
880 return -ENOMEM;
881
882 chip->cl = client;
883 chip->pdata = pdata;
884
885 mutex_init(&chip->lock);
500fe141 886
6a0c9a47 887 i2c_set_clientdata(client, led);
500fe141 888
945c7007 889 ret = lp5521_init_device(old_chip);
944f7b1d 890 if (ret)
f6c64c6f 891 goto err_init;
500fe141
SO
892
893 dev_info(&client->dev, "%s programmable led chip found\n", id->name);
894
945c7007 895 ret = lp5521_register_leds(old_chip);
f6524808
MWK
896 if (ret)
897 goto fail2;
500fe141
SO
898
899 ret = lp5521_register_sysfs(client);
900 if (ret) {
901 dev_err(&client->dev, "registering sysfs failed\n");
e430dc00 902 goto fail2;
500fe141
SO
903 }
904 return ret;
e430dc00 905fail2:
945c7007
MWK
906 lp5521_unregister_leds(old_chip);
907 lp5521_deinit_device(old_chip);
f6c64c6f 908err_init:
500fe141
SO
909 return ret;
910}
911
678e8a6b 912static int lp5521_remove(struct i2c_client *client)
500fe141 913{
945c7007 914 struct lp5521_chip *old_chip = i2c_get_clientdata(client);
500fe141 915
945c7007 916 lp5521_run_led_pattern(PATTERN_OFF, old_chip);
500fe141
SO
917 lp5521_unregister_sysfs(client);
918
945c7007 919 lp5521_unregister_leds(old_chip);
500fe141 920
945c7007 921 lp5521_deinit_device(old_chip);
500fe141
SO
922 return 0;
923}
924
925static const struct i2c_device_id lp5521_id[] = {
926 { "lp5521", 0 }, /* Three channel chip */
927 { }
928};
929MODULE_DEVICE_TABLE(i2c, lp5521_id);
930
931static struct i2c_driver lp5521_driver = {
932 .driver = {
933 .name = "lp5521",
934 },
935 .probe = lp5521_probe,
df07cf81 936 .remove = lp5521_remove,
500fe141
SO
937 .id_table = lp5521_id,
938};
939
09a0d183 940module_i2c_driver(lp5521_driver);
500fe141
SO
941
942MODULE_AUTHOR("Mathias Nyman, Yuri Zaporozhets, Samu Onkalo");
943MODULE_DESCRIPTION("LP5521 LED engine");
944MODULE_LICENSE("GPL v2");