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