Commit | Line | Data |
---|---|---|
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 MWK |
37 | #include <linux/platform_data/leds-lp55xx.h> |
38 | ||
39 | #include "leds-lp55xx-common.h" | |
0efba16c SO |
40 | |
41 | #define LP5523_REG_ENABLE 0x00 | |
42 | #define LP5523_REG_OP_MODE 0x01 | |
43 | #define LP5523_REG_RATIOMETRIC_MSB 0x02 | |
44 | #define LP5523_REG_RATIOMETRIC_LSB 0x03 | |
45 | #define LP5523_REG_ENABLE_LEDS_MSB 0x04 | |
46 | #define LP5523_REG_ENABLE_LEDS_LSB 0x05 | |
47 | #define LP5523_REG_LED_CNTRL_BASE 0x06 | |
48 | #define LP5523_REG_LED_PWM_BASE 0x16 | |
49 | #define LP5523_REG_LED_CURRENT_BASE 0x26 | |
50 | #define LP5523_REG_CONFIG 0x36 | |
51 | #define LP5523_REG_CHANNEL1_PC 0x37 | |
52 | #define LP5523_REG_CHANNEL2_PC 0x38 | |
53 | #define LP5523_REG_CHANNEL3_PC 0x39 | |
54 | #define LP5523_REG_STATUS 0x3a | |
55 | #define LP5523_REG_GPO 0x3b | |
56 | #define LP5523_REG_VARIABLE 0x3c | |
57 | #define LP5523_REG_RESET 0x3d | |
58 | #define LP5523_REG_TEMP_CTRL 0x3e | |
59 | #define LP5523_REG_TEMP_READ 0x3f | |
60 | #define LP5523_REG_TEMP_WRITE 0x40 | |
61 | #define LP5523_REG_LED_TEST_CTRL 0x41 | |
62 | #define LP5523_REG_LED_TEST_ADC 0x42 | |
63 | #define LP5523_REG_ENG1_VARIABLE 0x45 | |
64 | #define LP5523_REG_ENG2_VARIABLE 0x46 | |
65 | #define LP5523_REG_ENG3_VARIABLE 0x47 | |
66 | #define LP5523_REG_MASTER_FADER1 0x48 | |
67 | #define LP5523_REG_MASTER_FADER2 0x49 | |
68 | #define LP5523_REG_MASTER_FADER3 0x4a | |
69 | #define LP5523_REG_CH1_PROG_START 0x4c | |
70 | #define LP5523_REG_CH2_PROG_START 0x4d | |
71 | #define LP5523_REG_CH3_PROG_START 0x4e | |
72 | #define LP5523_REG_PROG_PAGE_SEL 0x4f | |
73 | #define LP5523_REG_PROG_MEM 0x50 | |
74 | ||
75 | #define LP5523_CMD_LOAD 0x15 /* 00010101 */ | |
76 | #define LP5523_CMD_RUN 0x2a /* 00101010 */ | |
77 | #define LP5523_CMD_DISABLED 0x00 /* 00000000 */ | |
78 | ||
79 | #define LP5523_ENABLE 0x40 | |
80 | #define LP5523_AUTO_INC 0x40 | |
81 | #define LP5523_PWR_SAVE 0x20 | |
82 | #define LP5523_PWM_PWR_SAVE 0x04 | |
83 | #define LP5523_CP_1 0x08 | |
84 | #define LP5523_CP_1_5 0x10 | |
85 | #define LP5523_CP_AUTO 0x18 | |
86 | #define LP5523_INT_CLK 0x01 | |
87 | #define LP5523_AUTO_CLK 0x02 | |
88 | #define LP5523_EN_LEDTEST 0x80 | |
89 | #define LP5523_LEDTEST_DONE 0x80 | |
48068d5d | 90 | #define LP5523_RESET 0xFF |
0efba16c SO |
91 | |
92 | #define LP5523_DEFAULT_CURRENT 50 /* microAmps */ | |
93 | #define LP5523_PROGRAM_LENGTH 32 /* in bytes */ | |
94 | #define LP5523_PROGRAM_PAGES 6 | |
95 | #define LP5523_ADC_SHORTCIRC_LIM 80 | |
96 | ||
0e202346 | 97 | #define LP5523_MAX_LEDS 9 |
0efba16c SO |
98 | #define LP5523_ENGINES 3 |
99 | ||
100 | #define LP5523_ENG_MASK_BASE 0x30 /* 00110000 */ | |
101 | ||
102 | #define LP5523_ENG_STATUS_MASK 0x07 /* 00000111 */ | |
103 | ||
104 | #define LP5523_IRQ_FLAGS IRQF_TRIGGER_FALLING | |
105 | ||
106 | #define LP5523_EXT_CLK_USED 0x08 | |
107 | ||
108 | #define LED_ACTIVE(mux, led) (!!(mux & (0x0001 << led))) | |
109 | #define SHIFT_MASK(id) (((id) - 1) * 2) | |
110 | ||
27d7704e KM |
111 | enum lp5523_chip_id { |
112 | LP5523, | |
113 | LP55231, | |
114 | }; | |
115 | ||
0efba16c | 116 | struct lp5523_engine { |
0efba16c SO |
117 | int id; |
118 | u8 mode; | |
119 | u8 prog_page; | |
120 | u8 mux_page; | |
121 | u16 led_mux; | |
122 | u8 engine_mask; | |
123 | }; | |
124 | ||
125 | struct lp5523_led { | |
126 | int id; | |
127 | u8 chan_nr; | |
128 | u8 led_current; | |
129 | u8 max_current; | |
130 | struct led_classdev cdev; | |
131 | struct work_struct brightness_work; | |
132 | u8 brightness; | |
133 | }; | |
134 | ||
135 | struct lp5523_chip { | |
136 | struct mutex lock; /* Serialize control */ | |
137 | struct i2c_client *client; | |
138 | struct lp5523_engine engines[LP5523_ENGINES]; | |
0e202346 | 139 | struct lp5523_led leds[LP5523_MAX_LEDS]; |
0efba16c SO |
140 | struct lp5523_platform_data *pdata; |
141 | u8 num_channels; | |
142 | u8 num_leds; | |
143 | }; | |
144 | ||
a96bfa13 MWK |
145 | static void lp5523_set_led_current(struct lp55xx_led *led, u8 led_current) |
146 | { | |
147 | led->led_current = led_current; | |
148 | lp55xx_write(led->chip, LP5523_REG_LED_CURRENT_BASE + led->chan_nr, | |
149 | led_current); | |
150 | } | |
151 | ||
87dbf623 SO |
152 | static inline struct lp5523_led *cdev_to_led(struct led_classdev *cdev) |
153 | { | |
154 | return container_of(cdev, struct lp5523_led, cdev); | |
155 | } | |
0efba16c | 156 | |
87dbf623 | 157 | static inline struct lp5523_chip *engine_to_lp5523(struct lp5523_engine *engine) |
0efba16c SO |
158 | { |
159 | return container_of(engine, struct lp5523_chip, | |
160 | engines[engine->id - 1]); | |
161 | } | |
162 | ||
87dbf623 | 163 | static inline struct lp5523_chip *led_to_lp5523(struct lp5523_led *led) |
0efba16c SO |
164 | { |
165 | return container_of(led, struct lp5523_chip, | |
166 | leds[led->id]); | |
167 | } | |
168 | ||
6f6365fb | 169 | static void lp5523_set_mode(struct lp5523_engine *engine, u8 mode); |
0efba16c | 170 | static int lp5523_set_engine_mode(struct lp5523_engine *engine, u8 mode); |
d06cb46c | 171 | static int lp5523_load_program(struct lp5523_engine *engine, const u8 *pattern); |
0efba16c | 172 | |
0efba16c SO |
173 | static int lp5523_write(struct i2c_client *client, u8 reg, u8 value) |
174 | { | |
175 | return i2c_smbus_write_byte_data(client, reg, value); | |
176 | } | |
177 | ||
178 | static int lp5523_read(struct i2c_client *client, u8 reg, u8 *buf) | |
179 | { | |
180 | s32 ret = i2c_smbus_read_byte_data(client, reg); | |
181 | ||
182 | if (ret < 0) | |
5ebab74a | 183 | return ret; |
0efba16c SO |
184 | |
185 | *buf = ret; | |
186 | return 0; | |
187 | } | |
188 | ||
ffbdccdb | 189 | static int lp5523_post_init_device(struct lp55xx_chip *chip) |
0efba16c | 190 | { |
ffbdccdb | 191 | int ret; |
0efba16c | 192 | |
ffbdccdb | 193 | ret = lp55xx_write(chip, LP5523_REG_ENABLE, LP5523_ENABLE); |
632418bf MWK |
194 | if (ret) |
195 | return ret; | |
0efba16c | 196 | |
2e4840ed SO |
197 | /* Chip startup time is 500 us, 1 - 2 ms gives some margin */ |
198 | usleep_range(1000, 2000); | |
0efba16c | 199 | |
ffbdccdb | 200 | ret = lp55xx_write(chip, LP5523_REG_CONFIG, |
0efba16c SO |
201 | LP5523_AUTO_INC | LP5523_PWR_SAVE | |
202 | LP5523_CP_AUTO | LP5523_AUTO_CLK | | |
203 | LP5523_PWM_PWR_SAVE); | |
632418bf MWK |
204 | if (ret) |
205 | return ret; | |
0efba16c SO |
206 | |
207 | /* turn on all leds */ | |
ffbdccdb | 208 | ret = lp55xx_write(chip, LP5523_REG_ENABLE_LEDS_MSB, 0x01); |
632418bf | 209 | if (ret) |
1b21ec5a JH |
210 | return ret; |
211 | ||
ffbdccdb | 212 | return lp55xx_write(chip, LP5523_REG_ENABLE_LEDS_LSB, 0xff); |
0efba16c SO |
213 | } |
214 | ||
215 | static int lp5523_set_engine_mode(struct lp5523_engine *engine, u8 mode) | |
216 | { | |
217 | struct lp5523_chip *chip = engine_to_lp5523(engine); | |
218 | struct i2c_client *client = chip->client; | |
219 | int ret; | |
220 | u8 engine_state; | |
221 | ||
222 | ret = lp5523_read(client, LP5523_REG_OP_MODE, &engine_state); | |
223 | if (ret) | |
224 | goto fail; | |
225 | ||
226 | engine_state &= ~(engine->engine_mask); | |
227 | ||
228 | /* set mode only for this engine */ | |
229 | mode &= engine->engine_mask; | |
230 | ||
231 | engine_state |= mode; | |
232 | ||
233 | ret |= lp5523_write(client, LP5523_REG_OP_MODE, engine_state); | |
234 | fail: | |
235 | return ret; | |
236 | } | |
237 | ||
238 | static int lp5523_load_mux(struct lp5523_engine *engine, u16 mux) | |
239 | { | |
240 | struct lp5523_chip *chip = engine_to_lp5523(engine); | |
241 | struct i2c_client *client = chip->client; | |
242 | int ret = 0; | |
243 | ||
244 | ret |= lp5523_set_engine_mode(engine, LP5523_CMD_LOAD); | |
245 | ||
246 | ret |= lp5523_write(client, LP5523_REG_PROG_PAGE_SEL, engine->mux_page); | |
247 | ret |= lp5523_write(client, LP5523_REG_PROG_MEM, | |
248 | (u8)(mux >> 8)); | |
249 | ret |= lp5523_write(client, LP5523_REG_PROG_MEM + 1, (u8)(mux)); | |
250 | engine->led_mux = mux; | |
251 | ||
252 | return ret; | |
253 | } | |
254 | ||
d06cb46c | 255 | static int lp5523_load_program(struct lp5523_engine *engine, const u8 *pattern) |
0efba16c SO |
256 | { |
257 | struct lp5523_chip *chip = engine_to_lp5523(engine); | |
258 | struct i2c_client *client = chip->client; | |
259 | ||
260 | int ret = 0; | |
261 | ||
262 | ret |= lp5523_set_engine_mode(engine, LP5523_CMD_LOAD); | |
263 | ||
264 | ret |= lp5523_write(client, LP5523_REG_PROG_PAGE_SEL, | |
265 | engine->prog_page); | |
266 | ret |= i2c_smbus_write_i2c_block_data(client, LP5523_REG_PROG_MEM, | |
267 | LP5523_PROGRAM_LENGTH, pattern); | |
268 | ||
269 | return ret; | |
270 | } | |
271 | ||
272 | static int lp5523_run_program(struct lp5523_engine *engine) | |
273 | { | |
274 | struct lp5523_chip *chip = engine_to_lp5523(engine); | |
275 | struct i2c_client *client = chip->client; | |
276 | int ret; | |
277 | ||
278 | ret = lp5523_write(client, LP5523_REG_ENABLE, | |
279 | LP5523_CMD_RUN | LP5523_ENABLE); | |
280 | if (ret) | |
281 | goto fail; | |
282 | ||
283 | ret = lp5523_set_engine_mode(engine, LP5523_CMD_RUN); | |
284 | fail: | |
285 | return ret; | |
286 | } | |
287 | ||
288 | static int lp5523_mux_parse(const char *buf, u16 *mux, size_t len) | |
289 | { | |
290 | int i; | |
291 | u16 tmp_mux = 0; | |
469eba02 | 292 | |
0e202346 | 293 | len = min_t(int, len, LP5523_MAX_LEDS); |
0efba16c SO |
294 | for (i = 0; i < len; i++) { |
295 | switch (buf[i]) { | |
296 | case '1': | |
297 | tmp_mux |= (1 << i); | |
298 | break; | |
299 | case '0': | |
300 | break; | |
301 | case '\n': | |
302 | i = len; | |
303 | break; | |
304 | default: | |
305 | return -1; | |
306 | } | |
307 | } | |
308 | *mux = tmp_mux; | |
309 | ||
310 | return 0; | |
311 | } | |
312 | ||
313 | static void lp5523_mux_to_array(u16 led_mux, char *array) | |
314 | { | |
315 | int i, pos = 0; | |
0e202346 | 316 | for (i = 0; i < LP5523_MAX_LEDS; i++) |
0efba16c SO |
317 | pos += sprintf(array + pos, "%x", LED_ACTIVE(led_mux, i)); |
318 | ||
319 | array[pos] = '\0'; | |
320 | } | |
321 | ||
322 | /*--------------------------------------------------------------*/ | |
323 | /* Sysfs interface */ | |
324 | /*--------------------------------------------------------------*/ | |
325 | ||
326 | static ssize_t show_engine_leds(struct device *dev, | |
327 | struct device_attribute *attr, | |
328 | char *buf, int nr) | |
329 | { | |
330 | struct i2c_client *client = to_i2c_client(dev); | |
331 | struct lp5523_chip *chip = i2c_get_clientdata(client); | |
0e202346 | 332 | char mux[LP5523_MAX_LEDS + 1]; |
0efba16c SO |
333 | |
334 | lp5523_mux_to_array(chip->engines[nr - 1].led_mux, mux); | |
335 | ||
336 | return sprintf(buf, "%s\n", mux); | |
337 | } | |
338 | ||
339 | #define show_leds(nr) \ | |
340 | static ssize_t show_engine##nr##_leds(struct device *dev, \ | |
341 | struct device_attribute *attr, \ | |
342 | char *buf) \ | |
343 | { \ | |
344 | return show_engine_leds(dev, attr, buf, nr); \ | |
345 | } | |
346 | show_leds(1) | |
347 | show_leds(2) | |
348 | show_leds(3) | |
349 | ||
350 | static ssize_t store_engine_leds(struct device *dev, | |
351 | struct device_attribute *attr, | |
352 | const char *buf, size_t len, int nr) | |
353 | { | |
354 | struct i2c_client *client = to_i2c_client(dev); | |
355 | struct lp5523_chip *chip = i2c_get_clientdata(client); | |
356 | u16 mux = 0; | |
fbac0812 | 357 | ssize_t ret; |
0efba16c SO |
358 | |
359 | if (lp5523_mux_parse(buf, &mux, len)) | |
360 | return -EINVAL; | |
361 | ||
fbac0812 SO |
362 | mutex_lock(&chip->lock); |
363 | ret = -EINVAL; | |
364 | if (chip->engines[nr - 1].mode != LP5523_CMD_LOAD) | |
365 | goto leave; | |
366 | ||
0efba16c | 367 | if (lp5523_load_mux(&chip->engines[nr - 1], mux)) |
fbac0812 | 368 | goto leave; |
0efba16c | 369 | |
fbac0812 SO |
370 | ret = len; |
371 | leave: | |
372 | mutex_unlock(&chip->lock); | |
373 | return ret; | |
0efba16c SO |
374 | } |
375 | ||
376 | #define store_leds(nr) \ | |
377 | static ssize_t store_engine##nr##_leds(struct device *dev, \ | |
378 | struct device_attribute *attr, \ | |
379 | const char *buf, size_t len) \ | |
380 | { \ | |
381 | return store_engine_leds(dev, attr, buf, len, nr); \ | |
382 | } | |
383 | store_leds(1) | |
384 | store_leds(2) | |
385 | store_leds(3) | |
386 | ||
387 | static ssize_t lp5523_selftest(struct device *dev, | |
388 | struct device_attribute *attr, | |
389 | char *buf) | |
390 | { | |
391 | struct i2c_client *client = to_i2c_client(dev); | |
392 | struct lp5523_chip *chip = i2c_get_clientdata(client); | |
393 | int i, ret, pos = 0; | |
394 | int led = 0; | |
395 | u8 status, adc, vdd; | |
396 | ||
397 | mutex_lock(&chip->lock); | |
398 | ||
399 | ret = lp5523_read(chip->client, LP5523_REG_STATUS, &status); | |
400 | if (ret < 0) | |
401 | goto fail; | |
402 | ||
403 | /* Check that ext clock is really in use if requested */ | |
404 | if ((chip->pdata) && (chip->pdata->clock_mode == LP5523_CLOCK_EXT)) | |
405 | if ((status & LP5523_EXT_CLK_USED) == 0) | |
406 | goto fail; | |
407 | ||
408 | /* Measure VDD (i.e. VBAT) first (channel 16 corresponds to VDD) */ | |
409 | lp5523_write(chip->client, LP5523_REG_LED_TEST_CTRL, | |
410 | LP5523_EN_LEDTEST | 16); | |
2e4840ed | 411 | usleep_range(3000, 6000); /* ADC conversion time is typically 2.7 ms */ |
0efba16c | 412 | ret = lp5523_read(chip->client, LP5523_REG_STATUS, &status); |
1b21ec5a JH |
413 | if (ret < 0) |
414 | goto fail; | |
415 | ||
0efba16c | 416 | if (!(status & LP5523_LEDTEST_DONE)) |
2e4840ed | 417 | usleep_range(3000, 6000); /* Was not ready. Wait little bit */ |
0efba16c | 418 | |
1b21ec5a JH |
419 | ret = lp5523_read(chip->client, LP5523_REG_LED_TEST_ADC, &vdd); |
420 | if (ret < 0) | |
421 | goto fail; | |
422 | ||
0efba16c SO |
423 | vdd--; /* There may be some fluctuation in measurement */ |
424 | ||
0e202346 | 425 | for (i = 0; i < LP5523_MAX_LEDS; i++) { |
0efba16c SO |
426 | /* Skip non-existing channels */ |
427 | if (chip->pdata->led_config[i].led_current == 0) | |
428 | continue; | |
429 | ||
430 | /* Set default current */ | |
431 | lp5523_write(chip->client, | |
432 | LP5523_REG_LED_CURRENT_BASE + i, | |
433 | chip->pdata->led_config[i].led_current); | |
434 | ||
435 | lp5523_write(chip->client, LP5523_REG_LED_PWM_BASE + i, 0xff); | |
2e4840ed SO |
436 | /* let current stabilize 2 - 4ms before measurements start */ |
437 | usleep_range(2000, 4000); | |
0efba16c SO |
438 | lp5523_write(chip->client, |
439 | LP5523_REG_LED_TEST_CTRL, | |
440 | LP5523_EN_LEDTEST | i); | |
2e4840ed SO |
441 | /* ADC conversion time is 2.7 ms typically */ |
442 | usleep_range(3000, 6000); | |
0efba16c | 443 | ret = lp5523_read(chip->client, LP5523_REG_STATUS, &status); |
1b21ec5a JH |
444 | if (ret < 0) |
445 | goto fail; | |
446 | ||
0efba16c | 447 | if (!(status & LP5523_LEDTEST_DONE)) |
2e4840ed | 448 | usleep_range(3000, 6000);/* Was not ready. Wait. */ |
1b21ec5a JH |
449 | ret = lp5523_read(chip->client, LP5523_REG_LED_TEST_ADC, &adc); |
450 | if (ret < 0) | |
451 | goto fail; | |
0efba16c SO |
452 | |
453 | if (adc >= vdd || adc < LP5523_ADC_SHORTCIRC_LIM) | |
454 | pos += sprintf(buf + pos, "LED %d FAIL\n", i); | |
455 | ||
456 | lp5523_write(chip->client, LP5523_REG_LED_PWM_BASE + i, 0x00); | |
457 | ||
458 | /* Restore current */ | |
459 | lp5523_write(chip->client, | |
460 | LP5523_REG_LED_CURRENT_BASE + i, | |
461 | chip->leds[led].led_current); | |
462 | led++; | |
463 | } | |
464 | if (pos == 0) | |
465 | pos = sprintf(buf, "OK\n"); | |
466 | goto release_lock; | |
467 | fail: | |
468 | pos = sprintf(buf, "FAIL\n"); | |
469 | ||
470 | release_lock: | |
471 | mutex_unlock(&chip->lock); | |
472 | ||
473 | return pos; | |
474 | } | |
475 | ||
0efba16c SO |
476 | static void lp5523_led_brightness_work(struct work_struct *work) |
477 | { | |
a6e4679a | 478 | struct lp55xx_led *led = container_of(work, struct lp55xx_led, |
0efba16c | 479 | brightness_work); |
a6e4679a | 480 | struct lp55xx_chip *chip = led->chip; |
0efba16c SO |
481 | |
482 | mutex_lock(&chip->lock); | |
a6e4679a | 483 | lp55xx_write(chip, LP5523_REG_LED_PWM_BASE + led->chan_nr, |
0efba16c | 484 | led->brightness); |
0efba16c SO |
485 | mutex_unlock(&chip->lock); |
486 | } | |
487 | ||
488 | static int lp5523_do_store_load(struct lp5523_engine *engine, | |
489 | const char *buf, size_t len) | |
490 | { | |
491 | struct lp5523_chip *chip = engine_to_lp5523(engine); | |
492 | struct i2c_client *client = chip->client; | |
493 | int ret, nrchars, offset = 0, i = 0; | |
494 | char c[3]; | |
495 | unsigned cmd; | |
496 | u8 pattern[LP5523_PROGRAM_LENGTH] = {0}; | |
497 | ||
469eba02 KM |
498 | if (engine->mode != LP5523_CMD_LOAD) |
499 | return -EINVAL; | |
500 | ||
0efba16c SO |
501 | while ((offset < len - 1) && (i < LP5523_PROGRAM_LENGTH)) { |
502 | /* separate sscanfs because length is working only for %s */ | |
503 | ret = sscanf(buf + offset, "%2s%n ", c, &nrchars); | |
504 | ret = sscanf(c, "%2x", &cmd); | |
505 | if (ret != 1) | |
506 | goto fail; | |
507 | pattern[i] = (u8)cmd; | |
508 | ||
509 | offset += nrchars; | |
510 | i++; | |
511 | } | |
512 | ||
513 | /* Each instruction is 16bit long. Check that length is even */ | |
514 | if (i % 2) | |
515 | goto fail; | |
516 | ||
517 | mutex_lock(&chip->lock); | |
469eba02 | 518 | ret = lp5523_load_program(engine, pattern); |
0efba16c SO |
519 | mutex_unlock(&chip->lock); |
520 | ||
521 | if (ret) { | |
522 | dev_err(&client->dev, "failed loading pattern\n"); | |
523 | return ret; | |
524 | } | |
525 | ||
526 | return len; | |
527 | fail: | |
528 | dev_err(&client->dev, "wrong pattern format\n"); | |
529 | return -EINVAL; | |
530 | } | |
531 | ||
532 | static ssize_t store_engine_load(struct device *dev, | |
533 | struct device_attribute *attr, | |
534 | const char *buf, size_t len, int nr) | |
535 | { | |
536 | struct i2c_client *client = to_i2c_client(dev); | |
537 | struct lp5523_chip *chip = i2c_get_clientdata(client); | |
538 | return lp5523_do_store_load(&chip->engines[nr - 1], buf, len); | |
539 | } | |
540 | ||
541 | #define store_load(nr) \ | |
542 | static ssize_t store_engine##nr##_load(struct device *dev, \ | |
543 | struct device_attribute *attr, \ | |
544 | const char *buf, size_t len) \ | |
545 | { \ | |
546 | return store_engine_load(dev, attr, buf, len, nr); \ | |
547 | } | |
548 | store_load(1) | |
549 | store_load(2) | |
550 | store_load(3) | |
551 | ||
552 | static ssize_t show_engine_mode(struct device *dev, | |
553 | struct device_attribute *attr, | |
554 | char *buf, int nr) | |
555 | { | |
556 | struct i2c_client *client = to_i2c_client(dev); | |
557 | struct lp5523_chip *chip = i2c_get_clientdata(client); | |
558 | switch (chip->engines[nr - 1].mode) { | |
559 | case LP5523_CMD_RUN: | |
560 | return sprintf(buf, "run\n"); | |
561 | case LP5523_CMD_LOAD: | |
562 | return sprintf(buf, "load\n"); | |
563 | case LP5523_CMD_DISABLED: | |
564 | return sprintf(buf, "disabled\n"); | |
565 | default: | |
566 | return sprintf(buf, "disabled\n"); | |
567 | } | |
568 | } | |
569 | ||
570 | #define show_mode(nr) \ | |
571 | static ssize_t show_engine##nr##_mode(struct device *dev, \ | |
572 | struct device_attribute *attr, \ | |
573 | char *buf) \ | |
574 | { \ | |
575 | return show_engine_mode(dev, attr, buf, nr); \ | |
576 | } | |
577 | show_mode(1) | |
578 | show_mode(2) | |
579 | show_mode(3) | |
580 | ||
581 | static ssize_t store_engine_mode(struct device *dev, | |
582 | struct device_attribute *attr, | |
583 | const char *buf, size_t len, int nr) | |
584 | { | |
585 | struct i2c_client *client = to_i2c_client(dev); | |
586 | struct lp5523_chip *chip = i2c_get_clientdata(client); | |
587 | struct lp5523_engine *engine = &chip->engines[nr - 1]; | |
588 | mutex_lock(&chip->lock); | |
589 | ||
590 | if (!strncmp(buf, "run", 3)) | |
591 | lp5523_set_mode(engine, LP5523_CMD_RUN); | |
592 | else if (!strncmp(buf, "load", 4)) | |
593 | lp5523_set_mode(engine, LP5523_CMD_LOAD); | |
594 | else if (!strncmp(buf, "disabled", 8)) | |
595 | lp5523_set_mode(engine, LP5523_CMD_DISABLED); | |
596 | ||
597 | mutex_unlock(&chip->lock); | |
598 | return len; | |
599 | } | |
600 | ||
601 | #define store_mode(nr) \ | |
602 | static ssize_t store_engine##nr##_mode(struct device *dev, \ | |
603 | struct device_attribute *attr, \ | |
604 | const char *buf, size_t len) \ | |
605 | { \ | |
606 | return store_engine_mode(dev, attr, buf, len, nr); \ | |
607 | } | |
608 | store_mode(1) | |
609 | store_mode(2) | |
610 | store_mode(3) | |
611 | ||
0efba16c | 612 | static struct attribute_group lp5523_led_attribute_group = { |
0efba16c SO |
613 | }; |
614 | ||
615 | /* device attributes */ | |
ccd7510f | 616 | static DEVICE_ATTR(engine1_mode, S_IRUGO | S_IWUSR, |
0efba16c | 617 | show_engine1_mode, store_engine1_mode); |
ccd7510f | 618 | static DEVICE_ATTR(engine2_mode, S_IRUGO | S_IWUSR, |
0efba16c | 619 | show_engine2_mode, store_engine2_mode); |
ccd7510f | 620 | static DEVICE_ATTR(engine3_mode, S_IRUGO | S_IWUSR, |
0efba16c | 621 | show_engine3_mode, store_engine3_mode); |
ccd7510f | 622 | static DEVICE_ATTR(engine1_leds, S_IRUGO | S_IWUSR, |
0efba16c | 623 | show_engine1_leds, store_engine1_leds); |
ccd7510f | 624 | static DEVICE_ATTR(engine2_leds, S_IRUGO | S_IWUSR, |
0efba16c | 625 | show_engine2_leds, store_engine2_leds); |
ccd7510f | 626 | static DEVICE_ATTR(engine3_leds, S_IRUGO | S_IWUSR, |
0efba16c | 627 | show_engine3_leds, store_engine3_leds); |
ccd7510f VK |
628 | static DEVICE_ATTR(engine1_load, S_IWUSR, NULL, store_engine1_load); |
629 | static DEVICE_ATTR(engine2_load, S_IWUSR, NULL, store_engine2_load); | |
630 | static DEVICE_ATTR(engine3_load, S_IWUSR, NULL, store_engine3_load); | |
0efba16c SO |
631 | static DEVICE_ATTR(selftest, S_IRUGO, lp5523_selftest, NULL); |
632 | ||
633 | static struct attribute *lp5523_attributes[] = { | |
634 | &dev_attr_engine1_mode.attr, | |
635 | &dev_attr_engine2_mode.attr, | |
636 | &dev_attr_engine3_mode.attr, | |
637 | &dev_attr_selftest.attr, | |
0efba16c SO |
638 | &dev_attr_engine1_load.attr, |
639 | &dev_attr_engine1_leds.attr, | |
0efba16c SO |
640 | &dev_attr_engine2_load.attr, |
641 | &dev_attr_engine2_leds.attr, | |
0efba16c SO |
642 | &dev_attr_engine3_load.attr, |
643 | &dev_attr_engine3_leds.attr, | |
f1625842 | 644 | NULL, |
0efba16c SO |
645 | }; |
646 | ||
647 | static const struct attribute_group lp5523_group = { | |
648 | .attrs = lp5523_attributes, | |
649 | }; | |
650 | ||
0efba16c SO |
651 | static int lp5523_register_sysfs(struct i2c_client *client) |
652 | { | |
653 | struct device *dev = &client->dev; | |
654 | int ret; | |
655 | ||
656 | ret = sysfs_create_group(&dev->kobj, &lp5523_group); | |
657 | if (ret < 0) | |
658 | return ret; | |
659 | ||
660 | return 0; | |
661 | } | |
662 | ||
663 | static void lp5523_unregister_sysfs(struct i2c_client *client) | |
664 | { | |
665 | struct lp5523_chip *chip = i2c_get_clientdata(client); | |
666 | struct device *dev = &client->dev; | |
667 | int i; | |
668 | ||
669 | sysfs_remove_group(&dev->kobj, &lp5523_group); | |
670 | ||
0efba16c SO |
671 | for (i = 0; i < chip->num_leds; i++) |
672 | sysfs_remove_group(&chip->leds[i].cdev.dev->kobj, | |
673 | &lp5523_led_attribute_group); | |
674 | } | |
675 | ||
676 | /*--------------------------------------------------------------*/ | |
677 | /* Set chip operating mode */ | |
678 | /*--------------------------------------------------------------*/ | |
6f6365fb | 679 | static void lp5523_set_mode(struct lp5523_engine *engine, u8 mode) |
0efba16c | 680 | { |
0efba16c SO |
681 | /* if in that mode already do nothing, except for run */ |
682 | if (mode == engine->mode && mode != LP5523_CMD_RUN) | |
6f6365fb | 683 | return; |
0efba16c | 684 | |
6f6365fb KM |
685 | switch (mode) { |
686 | case LP5523_CMD_RUN: | |
687 | lp5523_run_program(engine); | |
688 | break; | |
689 | case LP5523_CMD_LOAD: | |
0efba16c SO |
690 | lp5523_set_engine_mode(engine, LP5523_CMD_DISABLED); |
691 | lp5523_set_engine_mode(engine, LP5523_CMD_LOAD); | |
6f6365fb KM |
692 | break; |
693 | case LP5523_CMD_DISABLED: | |
0efba16c | 694 | lp5523_set_engine_mode(engine, LP5523_CMD_DISABLED); |
6f6365fb KM |
695 | break; |
696 | default: | |
697 | return; | |
0efba16c SO |
698 | } |
699 | ||
0efba16c | 700 | engine->mode = mode; |
0efba16c SO |
701 | } |
702 | ||
703 | /*--------------------------------------------------------------*/ | |
704 | /* Probe, Attach, Remove */ | |
705 | /*--------------------------------------------------------------*/ | |
1904f83d MWK |
706 | static void lp5523_unregister_leds(struct lp5523_chip *chip) |
707 | { | |
708 | int i; | |
709 | ||
710 | for (i = 0; i < chip->num_leds; i++) { | |
711 | led_classdev_unregister(&chip->leds[i].cdev); | |
712 | flush_work(&chip->leds[i].brightness_work); | |
713 | } | |
714 | } | |
715 | ||
48068d5d MWK |
716 | /* Chip specific configurations */ |
717 | static struct lp55xx_device_config lp5523_cfg = { | |
718 | .reset = { | |
719 | .addr = LP5523_REG_RESET, | |
720 | .val = LP5523_RESET, | |
721 | }, | |
e3a700d8 MWK |
722 | .enable = { |
723 | .addr = LP5523_REG_ENABLE, | |
724 | .val = LP5523_ENABLE, | |
725 | }, | |
0e202346 | 726 | .max_channel = LP5523_MAX_LEDS, |
ffbdccdb | 727 | .post_init_device = lp5523_post_init_device, |
a6e4679a | 728 | .brightness_work_fn = lp5523_led_brightness_work, |
a96bfa13 | 729 | .set_led_current = lp5523_set_led_current, |
48068d5d MWK |
730 | }; |
731 | ||
98ea1ea2 | 732 | static int lp5523_probe(struct i2c_client *client, |
0efba16c SO |
733 | const struct i2c_device_id *id) |
734 | { | |
6a0c9a47 | 735 | struct lp5523_chip *old_chip = NULL; |
22ebeb48 | 736 | int ret; |
6a0c9a47 MWK |
737 | struct lp55xx_chip *chip; |
738 | struct lp55xx_led *led; | |
739 | struct lp55xx_platform_data *pdata = client->dev.platform_data; | |
0efba16c | 740 | |
6a0c9a47 | 741 | if (!pdata) { |
0efba16c | 742 | dev_err(&client->dev, "no platform data\n"); |
94ca4bcc | 743 | return -EINVAL; |
0efba16c SO |
744 | } |
745 | ||
6a0c9a47 MWK |
746 | chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL); |
747 | if (!chip) | |
748 | return -ENOMEM; | |
749 | ||
750 | led = devm_kzalloc(&client->dev, | |
751 | sizeof(*led) * pdata->num_channels, GFP_KERNEL); | |
752 | if (!led) | |
753 | return -ENOMEM; | |
754 | ||
755 | chip->cl = client; | |
756 | chip->pdata = pdata; | |
48068d5d | 757 | chip->cfg = &lp5523_cfg; |
6a0c9a47 MWK |
758 | |
759 | mutex_init(&chip->lock); | |
0efba16c | 760 | |
6a0c9a47 | 761 | i2c_set_clientdata(client, led); |
0efba16c | 762 | |
22ebeb48 | 763 | ret = lp55xx_init_device(chip); |
0efba16c | 764 | if (ret) |
f6c64c6f | 765 | goto err_init; |
0efba16c | 766 | |
56a1e9ad | 767 | dev_info(&client->dev, "%s Programmable led chip found\n", id->name); |
0efba16c | 768 | |
9e9b3db1 | 769 | ret = lp55xx_register_leds(led, chip); |
f6524808 | 770 | if (ret) |
9e9b3db1 | 771 | goto err_register_leds; |
0efba16c SO |
772 | |
773 | ret = lp5523_register_sysfs(client); | |
774 | if (ret) { | |
775 | dev_err(&client->dev, "registering sysfs failed\n"); | |
94ca4bcc | 776 | goto fail2; |
0efba16c SO |
777 | } |
778 | return ret; | |
94ca4bcc | 779 | fail2: |
945c7007 | 780 | lp5523_unregister_leds(old_chip); |
9e9b3db1 | 781 | err_register_leds: |
6ce61762 | 782 | lp55xx_deinit_device(chip); |
f6c64c6f | 783 | err_init: |
0efba16c SO |
784 | return ret; |
785 | } | |
786 | ||
787 | static int lp5523_remove(struct i2c_client *client) | |
788 | { | |
945c7007 | 789 | struct lp5523_chip *old_chip = i2c_get_clientdata(client); |
6ce61762 MWK |
790 | struct lp55xx_led *led = i2c_get_clientdata(client); |
791 | struct lp55xx_chip *chip = led->chip; | |
0efba16c | 792 | |
23301b7f KM |
793 | /* Disable engine mode */ |
794 | lp5523_write(client, LP5523_REG_OP_MODE, LP5523_CMD_DISABLED); | |
795 | ||
0efba16c SO |
796 | lp5523_unregister_sysfs(client); |
797 | ||
945c7007 | 798 | lp5523_unregister_leds(old_chip); |
6ce61762 | 799 | lp55xx_deinit_device(chip); |
0efba16c | 800 | |
0efba16c SO |
801 | return 0; |
802 | } | |
803 | ||
804 | static const struct i2c_device_id lp5523_id[] = { | |
27d7704e KM |
805 | { "lp5523", LP5523 }, |
806 | { "lp55231", LP55231 }, | |
0efba16c SO |
807 | { } |
808 | }; | |
809 | ||
810 | MODULE_DEVICE_TABLE(i2c, lp5523_id); | |
811 | ||
812 | static struct i2c_driver lp5523_driver = { | |
813 | .driver = { | |
27d7704e | 814 | .name = "lp5523x", |
0efba16c SO |
815 | }, |
816 | .probe = lp5523_probe, | |
817 | .remove = lp5523_remove, | |
818 | .id_table = lp5523_id, | |
819 | }; | |
820 | ||
09a0d183 | 821 | module_i2c_driver(lp5523_driver); |
0efba16c SO |
822 | |
823 | MODULE_AUTHOR("Mathias Nyman <mathias.nyman@nokia.com>"); | |
824 | MODULE_DESCRIPTION("LP5523 LED engine"); | |
825 | MODULE_LICENSE("GPL"); |