leds-lp55xx: clean up unused data and functions
[GitHub/mt8127/android_kernel_alcatel_ttab.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 37#include <linux/platform_data/leds-lp55xx.h>
9ce7cb17 38#include <linux/firmware.h>
6a0c9a47
MWK
39
40#include "leds-lp55xx-common.h"
500fe141
SO
41
42#define LP5521_PROGRAM_LENGTH 32 /* in bytes */
43
44#define LP5521_MAX_LEDS 3 /* Maximum number of LEDs */
45#define LP5521_MAX_ENGINES 3 /* Maximum number of engines */
46
47#define LP5521_ENG_MASK_BASE 0x30 /* 00110000 */
48#define LP5521_ENG_STATUS_MASK 0x07 /* 00000111 */
49
50#define LP5521_CMD_LOAD 0x15 /* 00010101 */
51#define LP5521_CMD_RUN 0x2a /* 00101010 */
52#define LP5521_CMD_DIRECT 0x3f /* 00111111 */
53#define LP5521_CMD_DISABLED 0x00 /* 00000000 */
54
55/* Registers */
56#define LP5521_REG_ENABLE 0x00
57#define LP5521_REG_OP_MODE 0x01
58#define LP5521_REG_R_PWM 0x02
59#define LP5521_REG_G_PWM 0x03
60#define LP5521_REG_B_PWM 0x04
61#define LP5521_REG_R_CURRENT 0x05
62#define LP5521_REG_G_CURRENT 0x06
63#define LP5521_REG_B_CURRENT 0x07
64#define LP5521_REG_CONFIG 0x08
65#define LP5521_REG_R_CHANNEL_PC 0x09
66#define LP5521_REG_G_CHANNEL_PC 0x0A
67#define LP5521_REG_B_CHANNEL_PC 0x0B
68#define LP5521_REG_STATUS 0x0C
69#define LP5521_REG_RESET 0x0D
70#define LP5521_REG_GPO 0x0E
71#define LP5521_REG_R_PROG_MEM 0x10
72#define LP5521_REG_G_PROG_MEM 0x30
73#define LP5521_REG_B_PROG_MEM 0x50
74
75#define LP5521_PROG_MEM_BASE LP5521_REG_R_PROG_MEM
76#define LP5521_PROG_MEM_SIZE 0x20
77
78/* Base register to set LED current */
79#define LP5521_REG_LED_CURRENT_BASE LP5521_REG_R_CURRENT
80
81/* Base register to set the brightness */
82#define LP5521_REG_LED_PWM_BASE LP5521_REG_R_PWM
83
84/* Bits in ENABLE register */
85#define LP5521_MASTER_ENABLE 0x40 /* Chip master enable */
86#define LP5521_LOGARITHMIC_PWM 0x80 /* Logarithmic PWM adjustment */
87#define LP5521_EXEC_RUN 0x2A
32a2f747
KM
88#define LP5521_ENABLE_DEFAULT \
89 (LP5521_MASTER_ENABLE | LP5521_LOGARITHMIC_PWM)
90#define LP5521_ENABLE_RUN_PROGRAM \
91 (LP5521_ENABLE_DEFAULT | LP5521_EXEC_RUN)
500fe141 92
500fe141
SO
93/* Status */
94#define LP5521_EXT_CLK_USED 0x08
95
b3c49c05
SK
96/* default R channel current register value */
97#define LP5521_REG_R_CURR_DEFAULT 0xAF
98
011af7bc
KM
99/* Pattern Mode */
100#define PATTERN_OFF 0
101
48068d5d
MWK
102/* Reset register value */
103#define LP5521_RESET 0xFF
104
9ce7cb17
MWK
105/* Program Memory Operations */
106#define LP5521_MODE_R_M 0x30 /* Operation Mode Register */
107#define LP5521_MODE_G_M 0x0C
108#define LP5521_MODE_B_M 0x03
109#define LP5521_LOAD_R 0x10
110#define LP5521_LOAD_G 0x04
111#define LP5521_LOAD_B 0x01
112
113#define LP5521_R_IS_LOADING(mode) \
114 ((mode & LP5521_MODE_R_M) == LP5521_LOAD_R)
115#define LP5521_G_IS_LOADING(mode) \
116 ((mode & LP5521_MODE_G_M) == LP5521_LOAD_G)
117#define LP5521_B_IS_LOADING(mode) \
118 ((mode & LP5521_MODE_B_M) == LP5521_LOAD_B)
119
120#define LP5521_EXEC_R_M 0x30 /* Enable Register */
121#define LP5521_EXEC_G_M 0x0C
122#define LP5521_EXEC_B_M 0x03
123#define LP5521_EXEC_M 0x3F
124#define LP5521_RUN_R 0x20
125#define LP5521_RUN_G 0x08
126#define LP5521_RUN_B 0x02
500fe141 127
9ce7cb17
MWK
128static inline void lp5521_wait_opmode_done(void)
129{
130 /* operation mode change needs to be longer than 153 us */
131 usleep_range(200, 300);
132}
133
94482174
MWK
134static inline void lp5521_wait_enable_done(void)
135{
136 /* it takes more 488 us to update ENABLE register */
137 usleep_range(500, 600);
138}
139
a96bfa13
MWK
140static void lp5521_set_led_current(struct lp55xx_led *led, u8 led_current)
141{
142 led->led_current = led_current;
143 lp55xx_write(led->chip, LP5521_REG_LED_CURRENT_BASE + led->chan_nr,
144 led_current);
145}
146
9ce7cb17 147static void lp5521_load_engine(struct lp55xx_chip *chip)
500fe141 148{
9ce7cb17
MWK
149 enum lp55xx_engine_index idx = chip->engine_idx;
150 u8 mask[] = {
151 [LP55XX_ENGINE_1] = LP5521_MODE_R_M,
152 [LP55XX_ENGINE_2] = LP5521_MODE_G_M,
153 [LP55XX_ENGINE_3] = LP5521_MODE_B_M,
154 };
500fe141 155
9ce7cb17
MWK
156 u8 val[] = {
157 [LP55XX_ENGINE_1] = LP5521_LOAD_R,
158 [LP55XX_ENGINE_2] = LP5521_LOAD_G,
159 [LP55XX_ENGINE_3] = LP5521_LOAD_B,
160 };
500fe141 161
9ce7cb17 162 lp55xx_update_bits(chip, LP5521_REG_OP_MODE, mask[idx], val[idx]);
500fe141 163
9ce7cb17
MWK
164 lp5521_wait_opmode_done();
165}
500fe141 166
9ce7cb17
MWK
167static void lp5521_stop_engine(struct lp55xx_chip *chip)
168{
169 lp55xx_write(chip, LP5521_REG_OP_MODE, 0);
170 lp5521_wait_opmode_done();
500fe141
SO
171}
172
9ce7cb17 173static void lp5521_run_engine(struct lp55xx_chip *chip, bool start)
500fe141 174{
500fe141 175 int ret;
500fe141 176 u8 mode;
9ce7cb17 177 u8 exec;
500fe141 178
9ce7cb17
MWK
179 /* stop engine */
180 if (!start) {
181 lp5521_stop_engine(chip);
182 lp55xx_write(chip, LP5521_REG_OP_MODE, LP5521_CMD_DIRECT);
183 lp5521_wait_opmode_done();
184 return;
185 }
186
187 /*
188 * To run the engine,
189 * operation mode and enable register should updated at the same time
190 */
191
192 ret = lp55xx_read(chip, LP5521_REG_OP_MODE, &mode);
5bc9ad77 193 if (ret)
9ce7cb17 194 return;
5bc9ad77 195
9ce7cb17 196 ret = lp55xx_read(chip, LP5521_REG_ENABLE, &exec);
5bc9ad77 197 if (ret)
9ce7cb17
MWK
198 return;
199
200 /* change operation mode to RUN only when each engine is loading */
201 if (LP5521_R_IS_LOADING(mode)) {
202 mode = (mode & ~LP5521_MODE_R_M) | LP5521_RUN_R;
203 exec = (exec & ~LP5521_EXEC_R_M) | LP5521_RUN_R;
204 }
205
206 if (LP5521_G_IS_LOADING(mode)) {
207 mode = (mode & ~LP5521_MODE_G_M) | LP5521_RUN_G;
208 exec = (exec & ~LP5521_EXEC_G_M) | LP5521_RUN_G;
209 }
210
211 if (LP5521_B_IS_LOADING(mode)) {
212 mode = (mode & ~LP5521_MODE_B_M) | LP5521_RUN_B;
213 exec = (exec & ~LP5521_EXEC_B_M) | LP5521_RUN_B;
214 }
215
216 lp55xx_write(chip, LP5521_REG_OP_MODE, mode);
217 lp5521_wait_opmode_done();
218
219 lp55xx_update_bits(chip, LP5521_REG_ENABLE, LP5521_EXEC_M, exec);
220 lp5521_wait_enable_done();
221}
222
223static int lp5521_update_program_memory(struct lp55xx_chip *chip,
224 const u8 *data, size_t size)
225{
226 enum lp55xx_engine_index idx = chip->engine_idx;
227 u8 pattern[LP5521_PROGRAM_LENGTH] = {0};
228 u8 addr[] = {
229 [LP55XX_ENGINE_1] = LP5521_REG_R_PROG_MEM,
230 [LP55XX_ENGINE_2] = LP5521_REG_G_PROG_MEM,
231 [LP55XX_ENGINE_3] = LP5521_REG_B_PROG_MEM,
232 };
233 unsigned cmd;
234 char c[3];
235 int program_size;
236 int nrchars;
237 int offset = 0;
238 int ret;
239 int i;
240
241 /* clear program memory before updating */
242 for (i = 0; i < LP5521_PROGRAM_LENGTH; i++)
243 lp55xx_write(chip, addr[idx] + i, 0);
244
245 i = 0;
246 while ((offset < size - 1) && (i < LP5521_PROGRAM_LENGTH)) {
247 /* separate sscanfs because length is working only for %s */
248 ret = sscanf(data + offset, "%2s%n ", c, &nrchars);
249 if (ret != 1)
250 goto err;
251
252 ret = sscanf(c, "%2x", &cmd);
253 if (ret != 1)
254 goto err;
255
256 pattern[i] = (u8)cmd;
257 offset += nrchars;
258 i++;
259 }
260
261 /* Each instruction is 16bit long. Check that length is even */
262 if (i % 2)
263 goto err;
264
265 program_size = i;
266 for (i = 0; i < program_size; i++)
267 lp55xx_write(chip, addr[idx] + i, pattern[i]);
268
269 return 0;
270
271err:
272 dev_err(&chip->cl->dev, "wrong pattern format\n");
273 return -EINVAL;
274}
275
276static void lp5521_firmware_loaded(struct lp55xx_chip *chip)
277{
278 const struct firmware *fw = chip->fw;
279
280 if (fw->size > LP5521_PROGRAM_LENGTH) {
281 dev_err(&chip->cl->dev, "firmware data size overflow: %zu\n",
282 fw->size);
283 return;
284 }
500fe141 285
9ce7cb17
MWK
286 /*
287 * Program momery sequence
288 * 1) set engine mode to "LOAD"
289 * 2) write firmware data into program memory
290 */
291
292 lp5521_load_engine(chip);
293 lp5521_update_program_memory(chip, fw->data, fw->size);
500fe141
SO
294}
295
ffbdccdb 296static int lp5521_post_init_device(struct lp55xx_chip *chip)
500fe141 297{
500fe141 298 int ret;
94482174 299 u8 val;
500fe141 300
94482174
MWK
301 /*
302 * Make sure that the chip is reset by reading back the r channel
303 * current reg. This is dummy read is required on some platforms -
304 * otherwise further access to the R G B channels in the
305 * LP5521_REG_ENABLE register will not have any effect - strange!
306 */
ffbdccdb 307 ret = lp55xx_read(chip, LP5521_REG_R_CURRENT, &val);
94482174 308 if (ret) {
ffbdccdb 309 dev_err(&chip->cl->dev, "error in resetting chip\n");
94482174
MWK
310 return ret;
311 }
312 if (val != LP5521_REG_R_CURR_DEFAULT) {
ffbdccdb 313 dev_err(&chip->cl->dev,
94482174
MWK
314 "unexpected data in register (expected 0x%x got 0x%x)\n",
315 LP5521_REG_R_CURR_DEFAULT, val);
316 ret = -EINVAL;
317 return ret;
318 }
319 usleep_range(10000, 20000);
500fe141 320
500fe141 321 /* Set all PWMs to direct control mode */
ffbdccdb 322 ret = lp55xx_write(chip, LP5521_REG_OP_MODE, LP5521_CMD_DIRECT);
500fe141 323
ffbdccdb 324 val = chip->pdata->update_config ?
3b49aacd 325 : (LP5521_PWRSAVE_EN | LP5521_CP_MODE_AUTO | LP5521_R_TO_BATT);
ffbdccdb 326 ret = lp55xx_write(chip, LP5521_REG_CONFIG, val);
94482174
MWK
327 if (ret)
328 return ret;
500fe141
SO
329
330 /* Initialize all channels PWM to zero -> leds off */
ffbdccdb
MWK
331 lp55xx_write(chip, LP5521_REG_R_PWM, 0);
332 lp55xx_write(chip, LP5521_REG_G_PWM, 0);
333 lp55xx_write(chip, LP5521_REG_B_PWM, 0);
500fe141
SO
334
335 /* Set engines are set to run state when OP_MODE enables engines */
ffbdccdb 336 ret = lp55xx_write(chip, LP5521_REG_ENABLE, LP5521_ENABLE_RUN_PROGRAM);
94482174
MWK
337 if (ret)
338 return ret;
500fe141 339
94482174
MWK
340 lp5521_wait_enable_done();
341
342 return 0;
500fe141
SO
343}
344
9ca3bd80 345static int lp5521_run_selftest(struct lp55xx_chip *chip, char *buf)
500fe141 346{
9ca3bd80 347 struct lp55xx_platform_data *pdata = chip->pdata;
500fe141
SO
348 int ret;
349 u8 status;
350
9ca3bd80 351 ret = lp55xx_read(chip, LP5521_REG_STATUS, &status);
500fe141
SO
352 if (ret < 0)
353 return ret;
354
9ca3bd80
MWK
355 if (pdata->clock_mode != LP55XX_CLOCK_EXT)
356 return 0;
357
500fe141 358 /* Check that ext clock is really in use if requested */
9ca3bd80
MWK
359 if ((status & LP5521_EXT_CLK_USED) == 0)
360 return -EIO;
361
500fe141
SO
362 return 0;
363}
364
500fe141
SO
365static void lp5521_led_brightness_work(struct work_struct *work)
366{
a6e4679a 367 struct lp55xx_led *led = container_of(work, struct lp55xx_led,
500fe141 368 brightness_work);
a6e4679a 369 struct lp55xx_chip *chip = led->chip;
500fe141
SO
370
371 mutex_lock(&chip->lock);
a6e4679a 372 lp55xx_write(chip, LP5521_REG_LED_PWM_BASE + led->chan_nr,
500fe141
SO
373 led->brightness);
374 mutex_unlock(&chip->lock);
375}
376
500fe141
SO
377static ssize_t lp5521_selftest(struct device *dev,
378 struct device_attribute *attr,
379 char *buf)
380{
9ca3bd80
MWK
381 struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
382 struct lp55xx_chip *chip = led->chip;
500fe141
SO
383 int ret;
384
385 mutex_lock(&chip->lock);
386 ret = lp5521_run_selftest(chip, buf);
387 mutex_unlock(&chip->lock);
388 return sprintf(buf, "%s\n", ret ? "FAIL" : "OK");
389}
390
500fe141 391/* device attributes */
500fe141
SO
392static DEVICE_ATTR(selftest, S_IRUGO, lp5521_selftest, NULL);
393
394static struct attribute *lp5521_attributes[] = {
500fe141 395 &dev_attr_selftest.attr,
500fe141
SO
396 NULL
397};
398
399static const struct attribute_group lp5521_group = {
400 .attrs = lp5521_attributes,
401};
402
48068d5d
MWK
403/* Chip specific configurations */
404static struct lp55xx_device_config lp5521_cfg = {
405 .reset = {
406 .addr = LP5521_REG_RESET,
407 .val = LP5521_RESET,
408 },
e3a700d8
MWK
409 .enable = {
410 .addr = LP5521_REG_ENABLE,
411 .val = LP5521_ENABLE_DEFAULT,
412 },
0e202346 413 .max_channel = LP5521_MAX_LEDS,
ffbdccdb 414 .post_init_device = lp5521_post_init_device,
a6e4679a 415 .brightness_work_fn = lp5521_led_brightness_work,
a96bfa13 416 .set_led_current = lp5521_set_led_current,
9ce7cb17
MWK
417 .firmware_cb = lp5521_firmware_loaded,
418 .run_engine = lp5521_run_engine,
e73c0ce6 419 .dev_attr_group = &lp5521_group,
48068d5d
MWK
420};
421
98ea1ea2 422static int lp5521_probe(struct i2c_client *client,
500fe141
SO
423 const struct i2c_device_id *id)
424{
1904f83d 425 int ret;
6a0c9a47
MWK
426 struct lp55xx_chip *chip;
427 struct lp55xx_led *led;
428 struct lp55xx_platform_data *pdata = client->dev.platform_data;
500fe141 429
6a0c9a47 430 if (!pdata) {
500fe141 431 dev_err(&client->dev, "no platform data\n");
e430dc00 432 return -EINVAL;
500fe141
SO
433 }
434
6a0c9a47
MWK
435 chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
436 if (!chip)
437 return -ENOMEM;
438
439 led = devm_kzalloc(&client->dev,
440 sizeof(*led) * pdata->num_channels, GFP_KERNEL);
441 if (!led)
442 return -ENOMEM;
443
444 chip->cl = client;
445 chip->pdata = pdata;
48068d5d 446 chip->cfg = &lp5521_cfg;
6a0c9a47
MWK
447
448 mutex_init(&chip->lock);
500fe141 449
6a0c9a47 450 i2c_set_clientdata(client, led);
500fe141 451
22ebeb48 452 ret = lp55xx_init_device(chip);
944f7b1d 453 if (ret)
f6c64c6f 454 goto err_init;
500fe141
SO
455
456 dev_info(&client->dev, "%s programmable led chip found\n", id->name);
457
9e9b3db1 458 ret = lp55xx_register_leds(led, chip);
f6524808 459 if (ret)
9e9b3db1 460 goto err_register_leds;
500fe141 461
e73c0ce6 462 ret = lp55xx_register_sysfs(chip);
500fe141
SO
463 if (ret) {
464 dev_err(&client->dev, "registering sysfs failed\n");
e73c0ce6 465 goto err_register_sysfs;
500fe141 466 }
e73c0ce6
MWK
467
468 return 0;
469
470err_register_sysfs:
c3a68ebf 471 lp55xx_unregister_leds(led, chip);
9e9b3db1 472err_register_leds:
6ce61762 473 lp55xx_deinit_device(chip);
f6c64c6f 474err_init:
500fe141
SO
475 return ret;
476}
477
678e8a6b 478static int lp5521_remove(struct i2c_client *client)
500fe141 479{
6ce61762
MWK
480 struct lp55xx_led *led = i2c_get_clientdata(client);
481 struct lp55xx_chip *chip = led->chip;
500fe141 482
87cc4bde
MWK
483 lp5521_stop_engine(chip);
484 lp55xx_unregister_sysfs(chip);
c3a68ebf 485 lp55xx_unregister_leds(led, chip);
6ce61762 486 lp55xx_deinit_device(chip);
500fe141 487
500fe141
SO
488 return 0;
489}
490
491static const struct i2c_device_id lp5521_id[] = {
492 { "lp5521", 0 }, /* Three channel chip */
493 { }
494};
495MODULE_DEVICE_TABLE(i2c, lp5521_id);
496
497static struct i2c_driver lp5521_driver = {
498 .driver = {
499 .name = "lp5521",
500 },
501 .probe = lp5521_probe,
df07cf81 502 .remove = lp5521_remove,
500fe141
SO
503 .id_table = lp5521_id,
504};
505
09a0d183 506module_i2c_driver(lp5521_driver);
500fe141
SO
507
508MODULE_AUTHOR("Mathias Nyman, Yuri Zaporozhets, Samu Onkalo");
509MODULE_DESCRIPTION("LP5521 LED engine");
510MODULE_LICENSE("GPL v2");