leds-lp55xx: clean up definitions
[linux-2.6-block.git] / drivers / leds / leds-lp5523.c
CommitLineData
0efba16c
SO
1/*
2 * lp5523.c - LP5523 LED Driver
3 *
4 * Copyright (C) 2010 Nokia Corporation
5 *
6 * Contact: Samu Onkalo <samu.p.onkalo@nokia.com>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * version 2 as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA
21 */
22
23#include <linux/module.h>
24#include <linux/init.h>
25#include <linux/i2c.h>
26#include <linux/mutex.h>
27#include <linux/gpio.h>
28#include <linux/interrupt.h>
29#include <linux/delay.h>
30#include <linux/ctype.h>
31#include <linux/spinlock.h>
32#include <linux/wait.h>
33#include <linux/leds.h>
34#include <linux/leds-lp5523.h>
35#include <linux/workqueue.h>
36#include <linux/slab.h>
6a0c9a47 37#include <linux/platform_data/leds-lp55xx.h>
db6eaf83 38#include <linux/firmware.h>
6a0c9a47
MWK
39
40#include "leds-lp55xx-common.h"
0efba16c 41
12f022d2
MWK
42#define LP5523_PROGRAM_LENGTH 32
43#define LP5523_MAX_LEDS 9
44
45/* Registers */
0efba16c
SO
46#define LP5523_REG_ENABLE 0x00
47#define LP5523_REG_OP_MODE 0x01
0efba16c
SO
48#define LP5523_REG_ENABLE_LEDS_MSB 0x04
49#define LP5523_REG_ENABLE_LEDS_LSB 0x05
0efba16c
SO
50#define LP5523_REG_LED_PWM_BASE 0x16
51#define LP5523_REG_LED_CURRENT_BASE 0x26
52#define LP5523_REG_CONFIG 0x36
12f022d2
MWK
53#define LP5523_REG_STATUS 0x3A
54#define LP5523_REG_RESET 0x3D
0efba16c
SO
55#define LP5523_REG_LED_TEST_CTRL 0x41
56#define LP5523_REG_LED_TEST_ADC 0x42
12f022d2 57#define LP5523_REG_PROG_PAGE_SEL 0x4F
0efba16c
SO
58#define LP5523_REG_PROG_MEM 0x50
59
12f022d2 60/* Bit description in registers */
0efba16c
SO
61#define LP5523_ENABLE 0x40
62#define LP5523_AUTO_INC 0x40
63#define LP5523_PWR_SAVE 0x20
64#define LP5523_PWM_PWR_SAVE 0x04
0efba16c 65#define LP5523_CP_AUTO 0x18
0efba16c 66#define LP5523_AUTO_CLK 0x02
12f022d2 67
0efba16c
SO
68#define LP5523_EN_LEDTEST 0x80
69#define LP5523_LEDTEST_DONE 0x80
48068d5d 70#define LP5523_RESET 0xFF
0efba16c 71#define LP5523_ADC_SHORTCIRC_LIM 80
0efba16c
SO
72#define LP5523_EXT_CLK_USED 0x08
73
db6eaf83
MWK
74/* Memory Page Selection */
75#define LP5523_PAGE_ENG1 0
76#define LP5523_PAGE_ENG2 1
77#define LP5523_PAGE_ENG3 2
78
79/* Program Memory Operations */
80#define LP5523_MODE_ENG1_M 0x30 /* Operation Mode Register */
81#define LP5523_MODE_ENG2_M 0x0C
82#define LP5523_MODE_ENG3_M 0x03
83#define LP5523_LOAD_ENG1 0x10
84#define LP5523_LOAD_ENG2 0x04
85#define LP5523_LOAD_ENG3 0x01
86
87#define LP5523_ENG1_IS_LOADING(mode) \
88 ((mode & LP5523_MODE_ENG1_M) == LP5523_LOAD_ENG1)
89#define LP5523_ENG2_IS_LOADING(mode) \
90 ((mode & LP5523_MODE_ENG2_M) == LP5523_LOAD_ENG2)
91#define LP5523_ENG3_IS_LOADING(mode) \
92 ((mode & LP5523_MODE_ENG3_M) == LP5523_LOAD_ENG3)
93
94#define LP5523_EXEC_ENG1_M 0x30 /* Enable Register */
95#define LP5523_EXEC_ENG2_M 0x0C
96#define LP5523_EXEC_ENG3_M 0x03
97#define LP5523_EXEC_M 0x3F
98#define LP5523_RUN_ENG1 0x20
99#define LP5523_RUN_ENG2 0x08
100#define LP5523_RUN_ENG3 0x02
101
27d7704e
KM
102enum lp5523_chip_id {
103 LP5523,
104 LP55231,
105};
106
db6eaf83
MWK
107static inline void lp5523_wait_opmode_done(void)
108{
109 usleep_range(1000, 2000);
110}
111
a96bfa13
MWK
112static void lp5523_set_led_current(struct lp55xx_led *led, u8 led_current)
113{
114 led->led_current = led_current;
115 lp55xx_write(led->chip, LP5523_REG_LED_CURRENT_BASE + led->chan_nr,
116 led_current);
117}
118
ffbdccdb 119static int lp5523_post_init_device(struct lp55xx_chip *chip)
0efba16c 120{
ffbdccdb 121 int ret;
0efba16c 122
ffbdccdb 123 ret = lp55xx_write(chip, LP5523_REG_ENABLE, LP5523_ENABLE);
632418bf
MWK
124 if (ret)
125 return ret;
0efba16c 126
2e4840ed
SO
127 /* Chip startup time is 500 us, 1 - 2 ms gives some margin */
128 usleep_range(1000, 2000);
0efba16c 129
ffbdccdb 130 ret = lp55xx_write(chip, LP5523_REG_CONFIG,
0efba16c
SO
131 LP5523_AUTO_INC | LP5523_PWR_SAVE |
132 LP5523_CP_AUTO | LP5523_AUTO_CLK |
133 LP5523_PWM_PWR_SAVE);
632418bf
MWK
134 if (ret)
135 return ret;
0efba16c
SO
136
137 /* turn on all leds */
ffbdccdb 138 ret = lp55xx_write(chip, LP5523_REG_ENABLE_LEDS_MSB, 0x01);
632418bf 139 if (ret)
1b21ec5a
JH
140 return ret;
141
ffbdccdb 142 return lp55xx_write(chip, LP5523_REG_ENABLE_LEDS_LSB, 0xff);
0efba16c
SO
143}
144
db6eaf83 145static void lp5523_load_engine(struct lp55xx_chip *chip)
0efba16c 146{
db6eaf83
MWK
147 enum lp55xx_engine_index idx = chip->engine_idx;
148 u8 mask[] = {
149 [LP55XX_ENGINE_1] = LP5523_MODE_ENG1_M,
150 [LP55XX_ENGINE_2] = LP5523_MODE_ENG2_M,
151 [LP55XX_ENGINE_3] = LP5523_MODE_ENG3_M,
152 };
153
154 u8 val[] = {
155 [LP55XX_ENGINE_1] = LP5523_LOAD_ENG1,
156 [LP55XX_ENGINE_2] = LP5523_LOAD_ENG2,
157 [LP55XX_ENGINE_3] = LP5523_LOAD_ENG3,
158 };
159
160 u8 page_sel[] = {
161 [LP55XX_ENGINE_1] = LP5523_PAGE_ENG1,
162 [LP55XX_ENGINE_2] = LP5523_PAGE_ENG2,
163 [LP55XX_ENGINE_3] = LP5523_PAGE_ENG3,
164 };
165
166 lp55xx_update_bits(chip, LP5523_REG_OP_MODE, mask[idx], val[idx]);
167
168 lp5523_wait_opmode_done();
169
170 lp55xx_write(chip, LP5523_REG_PROG_PAGE_SEL, page_sel[idx]);
0efba16c
SO
171}
172
db6eaf83 173static void lp5523_stop_engine(struct lp55xx_chip *chip)
0efba16c 174{
db6eaf83
MWK
175 lp55xx_write(chip, LP5523_REG_OP_MODE, 0);
176 lp5523_wait_opmode_done();
177}
0efba16c 178
db6eaf83
MWK
179static void lp5523_turn_off_channels(struct lp55xx_chip *chip)
180{
181 int i;
0efba16c 182
db6eaf83
MWK
183 for (i = 0; i < LP5523_MAX_LEDS; i++)
184 lp55xx_write(chip, LP5523_REG_LED_PWM_BASE + i, 0);
0efba16c
SO
185}
186
db6eaf83 187static void lp5523_run_engine(struct lp55xx_chip *chip, bool start)
0efba16c 188{
db6eaf83
MWK
189 int ret;
190 u8 mode;
191 u8 exec;
0efba16c 192
db6eaf83
MWK
193 /* stop engine */
194 if (!start) {
195 lp5523_stop_engine(chip);
196 lp5523_turn_off_channels(chip);
197 return;
198 }
0efba16c 199
db6eaf83
MWK
200 /*
201 * To run the engine,
202 * operation mode and enable register should updated at the same time
203 */
0efba16c 204
db6eaf83
MWK
205 ret = lp55xx_read(chip, LP5523_REG_OP_MODE, &mode);
206 if (ret)
207 return;
0efba16c 208
db6eaf83
MWK
209 ret = lp55xx_read(chip, LP5523_REG_ENABLE, &exec);
210 if (ret)
211 return;
0efba16c 212
db6eaf83
MWK
213 /* change operation mode to RUN only when each engine is loading */
214 if (LP5523_ENG1_IS_LOADING(mode)) {
215 mode = (mode & ~LP5523_MODE_ENG1_M) | LP5523_RUN_ENG1;
216 exec = (exec & ~LP5523_EXEC_ENG1_M) | LP5523_RUN_ENG1;
217 }
0efba16c 218
db6eaf83
MWK
219 if (LP5523_ENG2_IS_LOADING(mode)) {
220 mode = (mode & ~LP5523_MODE_ENG2_M) | LP5523_RUN_ENG2;
221 exec = (exec & ~LP5523_EXEC_ENG2_M) | LP5523_RUN_ENG2;
222 }
0efba16c 223
db6eaf83
MWK
224 if (LP5523_ENG3_IS_LOADING(mode)) {
225 mode = (mode & ~LP5523_MODE_ENG3_M) | LP5523_RUN_ENG3;
226 exec = (exec & ~LP5523_EXEC_ENG3_M) | LP5523_RUN_ENG3;
227 }
228
229 lp55xx_write(chip, LP5523_REG_OP_MODE, mode);
230 lp5523_wait_opmode_done();
231
232 lp55xx_update_bits(chip, LP5523_REG_ENABLE, LP5523_EXEC_M, exec);
0efba16c
SO
233}
234
db6eaf83
MWK
235static int lp5523_update_program_memory(struct lp55xx_chip *chip,
236 const u8 *data, size_t size)
0efba16c 237{
db6eaf83
MWK
238 u8 pattern[LP5523_PROGRAM_LENGTH] = {0};
239 unsigned cmd;
240 char c[3];
241 int update_size;
242 int nrchars;
243 int offset = 0;
244 int ret;
0efba16c 245 int i;
0efba16c 246
db6eaf83
MWK
247 /* clear program memory before updating */
248 for (i = 0; i < LP5523_PROGRAM_LENGTH; i++)
249 lp55xx_write(chip, LP5523_REG_PROG_MEM + i, 0);
0efba16c 250
db6eaf83
MWK
251 i = 0;
252 while ((offset < size - 1) && (i < LP5523_PROGRAM_LENGTH)) {
253 /* separate sscanfs because length is working only for %s */
254 ret = sscanf(data + offset, "%2s%n ", c, &nrchars);
255 if (ret != 1)
256 goto err;
0efba16c 257
db6eaf83
MWK
258 ret = sscanf(c, "%2x", &cmd);
259 if (ret != 1)
260 goto err;
0efba16c 261
db6eaf83
MWK
262 pattern[i] = (u8)cmd;
263 offset += nrchars;
264 i++;
265 }
0efba16c 266
db6eaf83
MWK
267 /* Each instruction is 16bit long. Check that length is even */
268 if (i % 2)
269 goto err;
0efba16c 270
db6eaf83
MWK
271 update_size = i;
272 for (i = 0; i < update_size; i++)
273 lp55xx_write(chip, LP5523_REG_PROG_MEM + i, pattern[i]);
0efba16c 274
db6eaf83 275 return 0;
0efba16c 276
db6eaf83
MWK
277err:
278 dev_err(&chip->cl->dev, "wrong pattern format\n");
279 return -EINVAL;
0efba16c 280}
0efba16c 281
db6eaf83 282static void lp5523_firmware_loaded(struct lp55xx_chip *chip)
0efba16c 283{
db6eaf83 284 const struct firmware *fw = chip->fw;
fbac0812 285
db6eaf83
MWK
286 if (fw->size > LP5523_PROGRAM_LENGTH) {
287 dev_err(&chip->cl->dev, "firmware data size overflow: %zu\n",
288 fw->size);
289 return;
290 }
0efba16c 291
db6eaf83
MWK
292 /*
293 * Program momery sequence
294 * 1) set engine mode to "LOAD"
295 * 2) write firmware data into program memory
296 */
0efba16c 297
db6eaf83
MWK
298 lp5523_load_engine(chip);
299 lp5523_update_program_memory(chip, fw->data, fw->size);
0efba16c 300}
0efba16c
SO
301
302static ssize_t lp5523_selftest(struct device *dev,
303 struct device_attribute *attr,
304 char *buf)
305{
9ca3bd80
MWK
306 struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
307 struct lp55xx_chip *chip = led->chip;
308 struct lp55xx_platform_data *pdata = chip->pdata;
0efba16c 309 int i, ret, pos = 0;
0efba16c
SO
310 u8 status, adc, vdd;
311
312 mutex_lock(&chip->lock);
313
9ca3bd80 314 ret = lp55xx_read(chip, LP5523_REG_STATUS, &status);
0efba16c
SO
315 if (ret < 0)
316 goto fail;
317
318 /* Check that ext clock is really in use if requested */
9ca3bd80 319 if (pdata->clock_mode == LP55XX_CLOCK_EXT) {
0efba16c
SO
320 if ((status & LP5523_EXT_CLK_USED) == 0)
321 goto fail;
9ca3bd80 322 }
0efba16c
SO
323
324 /* Measure VDD (i.e. VBAT) first (channel 16 corresponds to VDD) */
9ca3bd80 325 lp55xx_write(chip, LP5523_REG_LED_TEST_CTRL, LP5523_EN_LEDTEST | 16);
2e4840ed 326 usleep_range(3000, 6000); /* ADC conversion time is typically 2.7 ms */
9ca3bd80 327 ret = lp55xx_read(chip, LP5523_REG_STATUS, &status);
1b21ec5a
JH
328 if (ret < 0)
329 goto fail;
330
0efba16c 331 if (!(status & LP5523_LEDTEST_DONE))
2e4840ed 332 usleep_range(3000, 6000); /* Was not ready. Wait little bit */
0efba16c 333
9ca3bd80 334 ret = lp55xx_read(chip, LP5523_REG_LED_TEST_ADC, &vdd);
1b21ec5a
JH
335 if (ret < 0)
336 goto fail;
337
0efba16c
SO
338 vdd--; /* There may be some fluctuation in measurement */
339
0e202346 340 for (i = 0; i < LP5523_MAX_LEDS; i++) {
0efba16c 341 /* Skip non-existing channels */
9ca3bd80 342 if (pdata->led_config[i].led_current == 0)
0efba16c
SO
343 continue;
344
345 /* Set default current */
9ca3bd80
MWK
346 lp55xx_write(chip, LP5523_REG_LED_CURRENT_BASE + i,
347 pdata->led_config[i].led_current);
0efba16c 348
9ca3bd80 349 lp55xx_write(chip, LP5523_REG_LED_PWM_BASE + i, 0xff);
2e4840ed
SO
350 /* let current stabilize 2 - 4ms before measurements start */
351 usleep_range(2000, 4000);
9ca3bd80 352 lp55xx_write(chip, LP5523_REG_LED_TEST_CTRL,
0efba16c 353 LP5523_EN_LEDTEST | i);
2e4840ed
SO
354 /* ADC conversion time is 2.7 ms typically */
355 usleep_range(3000, 6000);
9ca3bd80 356 ret = lp55xx_read(chip, LP5523_REG_STATUS, &status);
1b21ec5a
JH
357 if (ret < 0)
358 goto fail;
359
0efba16c 360 if (!(status & LP5523_LEDTEST_DONE))
2e4840ed 361 usleep_range(3000, 6000);/* Was not ready. Wait. */
9ca3bd80
MWK
362
363 ret = lp55xx_read(chip, LP5523_REG_LED_TEST_ADC, &adc);
1b21ec5a
JH
364 if (ret < 0)
365 goto fail;
0efba16c
SO
366
367 if (adc >= vdd || adc < LP5523_ADC_SHORTCIRC_LIM)
368 pos += sprintf(buf + pos, "LED %d FAIL\n", i);
369
9ca3bd80 370 lp55xx_write(chip, LP5523_REG_LED_PWM_BASE + i, 0x00);
0efba16c
SO
371
372 /* Restore current */
9ca3bd80
MWK
373 lp55xx_write(chip, LP5523_REG_LED_CURRENT_BASE + i,
374 led->led_current);
0efba16c
SO
375 led++;
376 }
377 if (pos == 0)
378 pos = sprintf(buf, "OK\n");
379 goto release_lock;
380fail:
381 pos = sprintf(buf, "FAIL\n");
382
383release_lock:
384 mutex_unlock(&chip->lock);
385
386 return pos;
387}
388
0efba16c
SO
389static void lp5523_led_brightness_work(struct work_struct *work)
390{
a6e4679a 391 struct lp55xx_led *led = container_of(work, struct lp55xx_led,
0efba16c 392 brightness_work);
a6e4679a 393 struct lp55xx_chip *chip = led->chip;
0efba16c
SO
394
395 mutex_lock(&chip->lock);
a6e4679a 396 lp55xx_write(chip, LP5523_REG_LED_PWM_BASE + led->chan_nr,
0efba16c 397 led->brightness);
0efba16c
SO
398 mutex_unlock(&chip->lock);
399}
400
0efba16c
SO
401static DEVICE_ATTR(selftest, S_IRUGO, lp5523_selftest, NULL);
402
403static struct attribute *lp5523_attributes[] = {
0efba16c 404 &dev_attr_selftest.attr,
f1625842 405 NULL,
0efba16c
SO
406};
407
408static const struct attribute_group lp5523_group = {
409 .attrs = lp5523_attributes,
410};
411
48068d5d
MWK
412/* Chip specific configurations */
413static struct lp55xx_device_config lp5523_cfg = {
414 .reset = {
415 .addr = LP5523_REG_RESET,
416 .val = LP5523_RESET,
417 },
e3a700d8
MWK
418 .enable = {
419 .addr = LP5523_REG_ENABLE,
420 .val = LP5523_ENABLE,
421 },
0e202346 422 .max_channel = LP5523_MAX_LEDS,
ffbdccdb 423 .post_init_device = lp5523_post_init_device,
a6e4679a 424 .brightness_work_fn = lp5523_led_brightness_work,
a96bfa13 425 .set_led_current = lp5523_set_led_current,
db6eaf83
MWK
426 .firmware_cb = lp5523_firmware_loaded,
427 .run_engine = lp5523_run_engine,
e73c0ce6 428 .dev_attr_group = &lp5523_group,
48068d5d
MWK
429};
430
98ea1ea2 431static int lp5523_probe(struct i2c_client *client,
0efba16c
SO
432 const struct i2c_device_id *id)
433{
22ebeb48 434 int ret;
6a0c9a47
MWK
435 struct lp55xx_chip *chip;
436 struct lp55xx_led *led;
437 struct lp55xx_platform_data *pdata = client->dev.platform_data;
0efba16c 438
6a0c9a47 439 if (!pdata) {
0efba16c 440 dev_err(&client->dev, "no platform data\n");
94ca4bcc 441 return -EINVAL;
0efba16c
SO
442 }
443
6a0c9a47
MWK
444 chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
445 if (!chip)
446 return -ENOMEM;
447
448 led = devm_kzalloc(&client->dev,
449 sizeof(*led) * pdata->num_channels, GFP_KERNEL);
450 if (!led)
451 return -ENOMEM;
452
453 chip->cl = client;
454 chip->pdata = pdata;
48068d5d 455 chip->cfg = &lp5523_cfg;
6a0c9a47
MWK
456
457 mutex_init(&chip->lock);
0efba16c 458
6a0c9a47 459 i2c_set_clientdata(client, led);
0efba16c 460
22ebeb48 461 ret = lp55xx_init_device(chip);
0efba16c 462 if (ret)
f6c64c6f 463 goto err_init;
0efba16c 464
56a1e9ad 465 dev_info(&client->dev, "%s Programmable led chip found\n", id->name);
0efba16c 466
9e9b3db1 467 ret = lp55xx_register_leds(led, chip);
f6524808 468 if (ret)
9e9b3db1 469 goto err_register_leds;
0efba16c 470
e73c0ce6 471 ret = lp55xx_register_sysfs(chip);
0efba16c
SO
472 if (ret) {
473 dev_err(&client->dev, "registering sysfs failed\n");
e73c0ce6 474 goto err_register_sysfs;
0efba16c 475 }
e73c0ce6
MWK
476
477 return 0;
478
479err_register_sysfs:
c3a68ebf 480 lp55xx_unregister_leds(led, chip);
9e9b3db1 481err_register_leds:
6ce61762 482 lp55xx_deinit_device(chip);
f6c64c6f 483err_init:
0efba16c
SO
484 return ret;
485}
486
487static int lp5523_remove(struct i2c_client *client)
488{
6ce61762
MWK
489 struct lp55xx_led *led = i2c_get_clientdata(client);
490 struct lp55xx_chip *chip = led->chip;
0efba16c 491
87cc4bde
MWK
492 lp5523_stop_engine(chip);
493 lp55xx_unregister_sysfs(chip);
c3a68ebf 494 lp55xx_unregister_leds(led, chip);
6ce61762 495 lp55xx_deinit_device(chip);
0efba16c 496
0efba16c
SO
497 return 0;
498}
499
500static const struct i2c_device_id lp5523_id[] = {
27d7704e
KM
501 { "lp5523", LP5523 },
502 { "lp55231", LP55231 },
0efba16c
SO
503 { }
504};
505
506MODULE_DEVICE_TABLE(i2c, lp5523_id);
507
508static struct i2c_driver lp5523_driver = {
509 .driver = {
27d7704e 510 .name = "lp5523x",
0efba16c
SO
511 },
512 .probe = lp5523_probe,
513 .remove = lp5523_remove,
514 .id_table = lp5523_id,
515};
516
09a0d183 517module_i2c_driver(lp5523_driver);
0efba16c
SO
518
519MODULE_AUTHOR("Mathias Nyman <mathias.nyman@nokia.com>");
520MODULE_DESCRIPTION("LP5523 LED engine");
521MODULE_LICENSE("GPL");