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 | 37 | #include <linux/platform_data/leds-lp55xx.h> |
db6eaf83 | 38 | #include <linux/firmware.h> |
6a0c9a47 MWK |
39 | |
40 | #include "leds-lp55xx-common.h" | |
0efba16c SO |
41 | |
42 | #define LP5523_REG_ENABLE 0x00 | |
43 | #define LP5523_REG_OP_MODE 0x01 | |
44 | #define LP5523_REG_RATIOMETRIC_MSB 0x02 | |
45 | #define LP5523_REG_RATIOMETRIC_LSB 0x03 | |
46 | #define LP5523_REG_ENABLE_LEDS_MSB 0x04 | |
47 | #define LP5523_REG_ENABLE_LEDS_LSB 0x05 | |
48 | #define LP5523_REG_LED_CNTRL_BASE 0x06 | |
49 | #define LP5523_REG_LED_PWM_BASE 0x16 | |
50 | #define LP5523_REG_LED_CURRENT_BASE 0x26 | |
51 | #define LP5523_REG_CONFIG 0x36 | |
52 | #define LP5523_REG_CHANNEL1_PC 0x37 | |
53 | #define LP5523_REG_CHANNEL2_PC 0x38 | |
54 | #define LP5523_REG_CHANNEL3_PC 0x39 | |
55 | #define LP5523_REG_STATUS 0x3a | |
56 | #define LP5523_REG_GPO 0x3b | |
57 | #define LP5523_REG_VARIABLE 0x3c | |
58 | #define LP5523_REG_RESET 0x3d | |
59 | #define LP5523_REG_TEMP_CTRL 0x3e | |
60 | #define LP5523_REG_TEMP_READ 0x3f | |
61 | #define LP5523_REG_TEMP_WRITE 0x40 | |
62 | #define LP5523_REG_LED_TEST_CTRL 0x41 | |
63 | #define LP5523_REG_LED_TEST_ADC 0x42 | |
64 | #define LP5523_REG_ENG1_VARIABLE 0x45 | |
65 | #define LP5523_REG_ENG2_VARIABLE 0x46 | |
66 | #define LP5523_REG_ENG3_VARIABLE 0x47 | |
67 | #define LP5523_REG_MASTER_FADER1 0x48 | |
68 | #define LP5523_REG_MASTER_FADER2 0x49 | |
69 | #define LP5523_REG_MASTER_FADER3 0x4a | |
70 | #define LP5523_REG_CH1_PROG_START 0x4c | |
71 | #define LP5523_REG_CH2_PROG_START 0x4d | |
72 | #define LP5523_REG_CH3_PROG_START 0x4e | |
73 | #define LP5523_REG_PROG_PAGE_SEL 0x4f | |
74 | #define LP5523_REG_PROG_MEM 0x50 | |
75 | ||
76 | #define LP5523_CMD_LOAD 0x15 /* 00010101 */ | |
77 | #define LP5523_CMD_RUN 0x2a /* 00101010 */ | |
78 | #define LP5523_CMD_DISABLED 0x00 /* 00000000 */ | |
79 | ||
80 | #define LP5523_ENABLE 0x40 | |
81 | #define LP5523_AUTO_INC 0x40 | |
82 | #define LP5523_PWR_SAVE 0x20 | |
83 | #define LP5523_PWM_PWR_SAVE 0x04 | |
84 | #define LP5523_CP_1 0x08 | |
85 | #define LP5523_CP_1_5 0x10 | |
86 | #define LP5523_CP_AUTO 0x18 | |
87 | #define LP5523_INT_CLK 0x01 | |
88 | #define LP5523_AUTO_CLK 0x02 | |
89 | #define LP5523_EN_LEDTEST 0x80 | |
90 | #define LP5523_LEDTEST_DONE 0x80 | |
48068d5d | 91 | #define LP5523_RESET 0xFF |
0efba16c SO |
92 | |
93 | #define LP5523_DEFAULT_CURRENT 50 /* microAmps */ | |
94 | #define LP5523_PROGRAM_LENGTH 32 /* in bytes */ | |
95 | #define LP5523_PROGRAM_PAGES 6 | |
96 | #define LP5523_ADC_SHORTCIRC_LIM 80 | |
97 | ||
0e202346 | 98 | #define LP5523_MAX_LEDS 9 |
0efba16c SO |
99 | #define LP5523_ENGINES 3 |
100 | ||
101 | #define LP5523_ENG_MASK_BASE 0x30 /* 00110000 */ | |
102 | ||
103 | #define LP5523_ENG_STATUS_MASK 0x07 /* 00000111 */ | |
104 | ||
105 | #define LP5523_IRQ_FLAGS IRQF_TRIGGER_FALLING | |
106 | ||
107 | #define LP5523_EXT_CLK_USED 0x08 | |
108 | ||
109 | #define LED_ACTIVE(mux, led) (!!(mux & (0x0001 << led))) | |
110 | #define SHIFT_MASK(id) (((id) - 1) * 2) | |
111 | ||
db6eaf83 MWK |
112 | /* Memory Page Selection */ |
113 | #define LP5523_PAGE_ENG1 0 | |
114 | #define LP5523_PAGE_ENG2 1 | |
115 | #define LP5523_PAGE_ENG3 2 | |
116 | ||
117 | /* Program Memory Operations */ | |
118 | #define LP5523_MODE_ENG1_M 0x30 /* Operation Mode Register */ | |
119 | #define LP5523_MODE_ENG2_M 0x0C | |
120 | #define LP5523_MODE_ENG3_M 0x03 | |
121 | #define LP5523_LOAD_ENG1 0x10 | |
122 | #define LP5523_LOAD_ENG2 0x04 | |
123 | #define LP5523_LOAD_ENG3 0x01 | |
124 | ||
125 | #define LP5523_ENG1_IS_LOADING(mode) \ | |
126 | ((mode & LP5523_MODE_ENG1_M) == LP5523_LOAD_ENG1) | |
127 | #define LP5523_ENG2_IS_LOADING(mode) \ | |
128 | ((mode & LP5523_MODE_ENG2_M) == LP5523_LOAD_ENG2) | |
129 | #define LP5523_ENG3_IS_LOADING(mode) \ | |
130 | ((mode & LP5523_MODE_ENG3_M) == LP5523_LOAD_ENG3) | |
131 | ||
132 | #define LP5523_EXEC_ENG1_M 0x30 /* Enable Register */ | |
133 | #define LP5523_EXEC_ENG2_M 0x0C | |
134 | #define LP5523_EXEC_ENG3_M 0x03 | |
135 | #define LP5523_EXEC_M 0x3F | |
136 | #define LP5523_RUN_ENG1 0x20 | |
137 | #define LP5523_RUN_ENG2 0x08 | |
138 | #define LP5523_RUN_ENG3 0x02 | |
139 | ||
27d7704e KM |
140 | enum lp5523_chip_id { |
141 | LP5523, | |
142 | LP55231, | |
143 | }; | |
144 | ||
db6eaf83 MWK |
145 | static inline void lp5523_wait_opmode_done(void) |
146 | { | |
147 | usleep_range(1000, 2000); | |
148 | } | |
149 | ||
a96bfa13 MWK |
150 | static void lp5523_set_led_current(struct lp55xx_led *led, u8 led_current) |
151 | { | |
152 | led->led_current = led_current; | |
153 | lp55xx_write(led->chip, LP5523_REG_LED_CURRENT_BASE + led->chan_nr, | |
154 | led_current); | |
155 | } | |
156 | ||
ffbdccdb | 157 | static int lp5523_post_init_device(struct lp55xx_chip *chip) |
0efba16c | 158 | { |
ffbdccdb | 159 | int ret; |
0efba16c | 160 | |
ffbdccdb | 161 | ret = lp55xx_write(chip, LP5523_REG_ENABLE, LP5523_ENABLE); |
632418bf MWK |
162 | if (ret) |
163 | return ret; | |
0efba16c | 164 | |
2e4840ed SO |
165 | /* Chip startup time is 500 us, 1 - 2 ms gives some margin */ |
166 | usleep_range(1000, 2000); | |
0efba16c | 167 | |
ffbdccdb | 168 | ret = lp55xx_write(chip, LP5523_REG_CONFIG, |
0efba16c SO |
169 | LP5523_AUTO_INC | LP5523_PWR_SAVE | |
170 | LP5523_CP_AUTO | LP5523_AUTO_CLK | | |
171 | LP5523_PWM_PWR_SAVE); | |
632418bf MWK |
172 | if (ret) |
173 | return ret; | |
0efba16c SO |
174 | |
175 | /* turn on all leds */ | |
ffbdccdb | 176 | ret = lp55xx_write(chip, LP5523_REG_ENABLE_LEDS_MSB, 0x01); |
632418bf | 177 | if (ret) |
1b21ec5a JH |
178 | return ret; |
179 | ||
ffbdccdb | 180 | return lp55xx_write(chip, LP5523_REG_ENABLE_LEDS_LSB, 0xff); |
0efba16c SO |
181 | } |
182 | ||
db6eaf83 | 183 | static void lp5523_load_engine(struct lp55xx_chip *chip) |
0efba16c | 184 | { |
db6eaf83 MWK |
185 | enum lp55xx_engine_index idx = chip->engine_idx; |
186 | u8 mask[] = { | |
187 | [LP55XX_ENGINE_1] = LP5523_MODE_ENG1_M, | |
188 | [LP55XX_ENGINE_2] = LP5523_MODE_ENG2_M, | |
189 | [LP55XX_ENGINE_3] = LP5523_MODE_ENG3_M, | |
190 | }; | |
191 | ||
192 | u8 val[] = { | |
193 | [LP55XX_ENGINE_1] = LP5523_LOAD_ENG1, | |
194 | [LP55XX_ENGINE_2] = LP5523_LOAD_ENG2, | |
195 | [LP55XX_ENGINE_3] = LP5523_LOAD_ENG3, | |
196 | }; | |
197 | ||
198 | u8 page_sel[] = { | |
199 | [LP55XX_ENGINE_1] = LP5523_PAGE_ENG1, | |
200 | [LP55XX_ENGINE_2] = LP5523_PAGE_ENG2, | |
201 | [LP55XX_ENGINE_3] = LP5523_PAGE_ENG3, | |
202 | }; | |
203 | ||
204 | lp55xx_update_bits(chip, LP5523_REG_OP_MODE, mask[idx], val[idx]); | |
205 | ||
206 | lp5523_wait_opmode_done(); | |
207 | ||
208 | lp55xx_write(chip, LP5523_REG_PROG_PAGE_SEL, page_sel[idx]); | |
0efba16c SO |
209 | } |
210 | ||
db6eaf83 | 211 | static void lp5523_stop_engine(struct lp55xx_chip *chip) |
0efba16c | 212 | { |
db6eaf83 MWK |
213 | lp55xx_write(chip, LP5523_REG_OP_MODE, 0); |
214 | lp5523_wait_opmode_done(); | |
215 | } | |
0efba16c | 216 | |
db6eaf83 MWK |
217 | static void lp5523_turn_off_channels(struct lp55xx_chip *chip) |
218 | { | |
219 | int i; | |
0efba16c | 220 | |
db6eaf83 MWK |
221 | for (i = 0; i < LP5523_MAX_LEDS; i++) |
222 | lp55xx_write(chip, LP5523_REG_LED_PWM_BASE + i, 0); | |
0efba16c SO |
223 | } |
224 | ||
db6eaf83 | 225 | static void lp5523_run_engine(struct lp55xx_chip *chip, bool start) |
0efba16c | 226 | { |
db6eaf83 MWK |
227 | int ret; |
228 | u8 mode; | |
229 | u8 exec; | |
0efba16c | 230 | |
db6eaf83 MWK |
231 | /* stop engine */ |
232 | if (!start) { | |
233 | lp5523_stop_engine(chip); | |
234 | lp5523_turn_off_channels(chip); | |
235 | return; | |
236 | } | |
0efba16c | 237 | |
db6eaf83 MWK |
238 | /* |
239 | * To run the engine, | |
240 | * operation mode and enable register should updated at the same time | |
241 | */ | |
0efba16c | 242 | |
db6eaf83 MWK |
243 | ret = lp55xx_read(chip, LP5523_REG_OP_MODE, &mode); |
244 | if (ret) | |
245 | return; | |
0efba16c | 246 | |
db6eaf83 MWK |
247 | ret = lp55xx_read(chip, LP5523_REG_ENABLE, &exec); |
248 | if (ret) | |
249 | return; | |
0efba16c | 250 | |
db6eaf83 MWK |
251 | /* change operation mode to RUN only when each engine is loading */ |
252 | if (LP5523_ENG1_IS_LOADING(mode)) { | |
253 | mode = (mode & ~LP5523_MODE_ENG1_M) | LP5523_RUN_ENG1; | |
254 | exec = (exec & ~LP5523_EXEC_ENG1_M) | LP5523_RUN_ENG1; | |
255 | } | |
0efba16c | 256 | |
db6eaf83 MWK |
257 | if (LP5523_ENG2_IS_LOADING(mode)) { |
258 | mode = (mode & ~LP5523_MODE_ENG2_M) | LP5523_RUN_ENG2; | |
259 | exec = (exec & ~LP5523_EXEC_ENG2_M) | LP5523_RUN_ENG2; | |
260 | } | |
0efba16c | 261 | |
db6eaf83 MWK |
262 | if (LP5523_ENG3_IS_LOADING(mode)) { |
263 | mode = (mode & ~LP5523_MODE_ENG3_M) | LP5523_RUN_ENG3; | |
264 | exec = (exec & ~LP5523_EXEC_ENG3_M) | LP5523_RUN_ENG3; | |
265 | } | |
266 | ||
267 | lp55xx_write(chip, LP5523_REG_OP_MODE, mode); | |
268 | lp5523_wait_opmode_done(); | |
269 | ||
270 | lp55xx_update_bits(chip, LP5523_REG_ENABLE, LP5523_EXEC_M, exec); | |
0efba16c SO |
271 | } |
272 | ||
db6eaf83 MWK |
273 | static int lp5523_update_program_memory(struct lp55xx_chip *chip, |
274 | const u8 *data, size_t size) | |
0efba16c | 275 | { |
db6eaf83 MWK |
276 | u8 pattern[LP5523_PROGRAM_LENGTH] = {0}; |
277 | unsigned cmd; | |
278 | char c[3]; | |
279 | int update_size; | |
280 | int nrchars; | |
281 | int offset = 0; | |
282 | int ret; | |
0efba16c | 283 | int i; |
0efba16c | 284 | |
db6eaf83 MWK |
285 | /* clear program memory before updating */ |
286 | for (i = 0; i < LP5523_PROGRAM_LENGTH; i++) | |
287 | lp55xx_write(chip, LP5523_REG_PROG_MEM + i, 0); | |
0efba16c | 288 | |
db6eaf83 MWK |
289 | i = 0; |
290 | while ((offset < size - 1) && (i < LP5523_PROGRAM_LENGTH)) { | |
291 | /* separate sscanfs because length is working only for %s */ | |
292 | ret = sscanf(data + offset, "%2s%n ", c, &nrchars); | |
293 | if (ret != 1) | |
294 | goto err; | |
0efba16c | 295 | |
db6eaf83 MWK |
296 | ret = sscanf(c, "%2x", &cmd); |
297 | if (ret != 1) | |
298 | goto err; | |
0efba16c | 299 | |
db6eaf83 MWK |
300 | pattern[i] = (u8)cmd; |
301 | offset += nrchars; | |
302 | i++; | |
303 | } | |
0efba16c | 304 | |
db6eaf83 MWK |
305 | /* Each instruction is 16bit long. Check that length is even */ |
306 | if (i % 2) | |
307 | goto err; | |
0efba16c | 308 | |
db6eaf83 MWK |
309 | update_size = i; |
310 | for (i = 0; i < update_size; i++) | |
311 | lp55xx_write(chip, LP5523_REG_PROG_MEM + i, pattern[i]); | |
0efba16c | 312 | |
db6eaf83 | 313 | return 0; |
0efba16c | 314 | |
db6eaf83 MWK |
315 | err: |
316 | dev_err(&chip->cl->dev, "wrong pattern format\n"); | |
317 | return -EINVAL; | |
0efba16c | 318 | } |
0efba16c | 319 | |
db6eaf83 | 320 | static void lp5523_firmware_loaded(struct lp55xx_chip *chip) |
0efba16c | 321 | { |
db6eaf83 | 322 | const struct firmware *fw = chip->fw; |
fbac0812 | 323 | |
db6eaf83 MWK |
324 | if (fw->size > LP5523_PROGRAM_LENGTH) { |
325 | dev_err(&chip->cl->dev, "firmware data size overflow: %zu\n", | |
326 | fw->size); | |
327 | return; | |
328 | } | |
0efba16c | 329 | |
db6eaf83 MWK |
330 | /* |
331 | * Program momery sequence | |
332 | * 1) set engine mode to "LOAD" | |
333 | * 2) write firmware data into program memory | |
334 | */ | |
0efba16c | 335 | |
db6eaf83 MWK |
336 | lp5523_load_engine(chip); |
337 | lp5523_update_program_memory(chip, fw->data, fw->size); | |
0efba16c | 338 | } |
0efba16c SO |
339 | |
340 | static ssize_t lp5523_selftest(struct device *dev, | |
341 | struct device_attribute *attr, | |
342 | char *buf) | |
343 | { | |
9ca3bd80 MWK |
344 | struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev)); |
345 | struct lp55xx_chip *chip = led->chip; | |
346 | struct lp55xx_platform_data *pdata = chip->pdata; | |
0efba16c | 347 | int i, ret, pos = 0; |
0efba16c SO |
348 | u8 status, adc, vdd; |
349 | ||
350 | mutex_lock(&chip->lock); | |
351 | ||
9ca3bd80 | 352 | ret = lp55xx_read(chip, LP5523_REG_STATUS, &status); |
0efba16c SO |
353 | if (ret < 0) |
354 | goto fail; | |
355 | ||
356 | /* Check that ext clock is really in use if requested */ | |
9ca3bd80 | 357 | if (pdata->clock_mode == LP55XX_CLOCK_EXT) { |
0efba16c SO |
358 | if ((status & LP5523_EXT_CLK_USED) == 0) |
359 | goto fail; | |
9ca3bd80 | 360 | } |
0efba16c SO |
361 | |
362 | /* Measure VDD (i.e. VBAT) first (channel 16 corresponds to VDD) */ | |
9ca3bd80 | 363 | lp55xx_write(chip, LP5523_REG_LED_TEST_CTRL, LP5523_EN_LEDTEST | 16); |
2e4840ed | 364 | usleep_range(3000, 6000); /* ADC conversion time is typically 2.7 ms */ |
9ca3bd80 | 365 | ret = lp55xx_read(chip, LP5523_REG_STATUS, &status); |
1b21ec5a JH |
366 | if (ret < 0) |
367 | goto fail; | |
368 | ||
0efba16c | 369 | if (!(status & LP5523_LEDTEST_DONE)) |
2e4840ed | 370 | usleep_range(3000, 6000); /* Was not ready. Wait little bit */ |
0efba16c | 371 | |
9ca3bd80 | 372 | ret = lp55xx_read(chip, LP5523_REG_LED_TEST_ADC, &vdd); |
1b21ec5a JH |
373 | if (ret < 0) |
374 | goto fail; | |
375 | ||
0efba16c SO |
376 | vdd--; /* There may be some fluctuation in measurement */ |
377 | ||
0e202346 | 378 | for (i = 0; i < LP5523_MAX_LEDS; i++) { |
0efba16c | 379 | /* Skip non-existing channels */ |
9ca3bd80 | 380 | if (pdata->led_config[i].led_current == 0) |
0efba16c SO |
381 | continue; |
382 | ||
383 | /* Set default current */ | |
9ca3bd80 MWK |
384 | lp55xx_write(chip, LP5523_REG_LED_CURRENT_BASE + i, |
385 | pdata->led_config[i].led_current); | |
0efba16c | 386 | |
9ca3bd80 | 387 | lp55xx_write(chip, LP5523_REG_LED_PWM_BASE + i, 0xff); |
2e4840ed SO |
388 | /* let current stabilize 2 - 4ms before measurements start */ |
389 | usleep_range(2000, 4000); | |
9ca3bd80 | 390 | lp55xx_write(chip, LP5523_REG_LED_TEST_CTRL, |
0efba16c | 391 | LP5523_EN_LEDTEST | i); |
2e4840ed SO |
392 | /* ADC conversion time is 2.7 ms typically */ |
393 | usleep_range(3000, 6000); | |
9ca3bd80 | 394 | ret = lp55xx_read(chip, LP5523_REG_STATUS, &status); |
1b21ec5a JH |
395 | if (ret < 0) |
396 | goto fail; | |
397 | ||
0efba16c | 398 | if (!(status & LP5523_LEDTEST_DONE)) |
2e4840ed | 399 | usleep_range(3000, 6000);/* Was not ready. Wait. */ |
9ca3bd80 MWK |
400 | |
401 | ret = lp55xx_read(chip, LP5523_REG_LED_TEST_ADC, &adc); | |
1b21ec5a JH |
402 | if (ret < 0) |
403 | goto fail; | |
0efba16c SO |
404 | |
405 | if (adc >= vdd || adc < LP5523_ADC_SHORTCIRC_LIM) | |
406 | pos += sprintf(buf + pos, "LED %d FAIL\n", i); | |
407 | ||
9ca3bd80 | 408 | lp55xx_write(chip, LP5523_REG_LED_PWM_BASE + i, 0x00); |
0efba16c SO |
409 | |
410 | /* Restore current */ | |
9ca3bd80 MWK |
411 | lp55xx_write(chip, LP5523_REG_LED_CURRENT_BASE + i, |
412 | led->led_current); | |
0efba16c SO |
413 | led++; |
414 | } | |
415 | if (pos == 0) | |
416 | pos = sprintf(buf, "OK\n"); | |
417 | goto release_lock; | |
418 | fail: | |
419 | pos = sprintf(buf, "FAIL\n"); | |
420 | ||
421 | release_lock: | |
422 | mutex_unlock(&chip->lock); | |
423 | ||
424 | return pos; | |
425 | } | |
426 | ||
0efba16c SO |
427 | static void lp5523_led_brightness_work(struct work_struct *work) |
428 | { | |
a6e4679a | 429 | struct lp55xx_led *led = container_of(work, struct lp55xx_led, |
0efba16c | 430 | brightness_work); |
a6e4679a | 431 | struct lp55xx_chip *chip = led->chip; |
0efba16c SO |
432 | |
433 | mutex_lock(&chip->lock); | |
a6e4679a | 434 | lp55xx_write(chip, LP5523_REG_LED_PWM_BASE + led->chan_nr, |
0efba16c | 435 | led->brightness); |
0efba16c SO |
436 | mutex_unlock(&chip->lock); |
437 | } | |
438 | ||
0efba16c SO |
439 | static DEVICE_ATTR(selftest, S_IRUGO, lp5523_selftest, NULL); |
440 | ||
441 | static struct attribute *lp5523_attributes[] = { | |
0efba16c | 442 | &dev_attr_selftest.attr, |
f1625842 | 443 | NULL, |
0efba16c SO |
444 | }; |
445 | ||
446 | static const struct attribute_group lp5523_group = { | |
447 | .attrs = lp5523_attributes, | |
448 | }; | |
449 | ||
48068d5d MWK |
450 | /* Chip specific configurations */ |
451 | static struct lp55xx_device_config lp5523_cfg = { | |
452 | .reset = { | |
453 | .addr = LP5523_REG_RESET, | |
454 | .val = LP5523_RESET, | |
455 | }, | |
e3a700d8 MWK |
456 | .enable = { |
457 | .addr = LP5523_REG_ENABLE, | |
458 | .val = LP5523_ENABLE, | |
459 | }, | |
0e202346 | 460 | .max_channel = LP5523_MAX_LEDS, |
ffbdccdb | 461 | .post_init_device = lp5523_post_init_device, |
a6e4679a | 462 | .brightness_work_fn = lp5523_led_brightness_work, |
a96bfa13 | 463 | .set_led_current = lp5523_set_led_current, |
db6eaf83 MWK |
464 | .firmware_cb = lp5523_firmware_loaded, |
465 | .run_engine = lp5523_run_engine, | |
e73c0ce6 | 466 | .dev_attr_group = &lp5523_group, |
48068d5d MWK |
467 | }; |
468 | ||
98ea1ea2 | 469 | static int lp5523_probe(struct i2c_client *client, |
0efba16c SO |
470 | const struct i2c_device_id *id) |
471 | { | |
22ebeb48 | 472 | int ret; |
6a0c9a47 MWK |
473 | struct lp55xx_chip *chip; |
474 | struct lp55xx_led *led; | |
475 | struct lp55xx_platform_data *pdata = client->dev.platform_data; | |
0efba16c | 476 | |
6a0c9a47 | 477 | if (!pdata) { |
0efba16c | 478 | dev_err(&client->dev, "no platform data\n"); |
94ca4bcc | 479 | return -EINVAL; |
0efba16c SO |
480 | } |
481 | ||
6a0c9a47 MWK |
482 | chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL); |
483 | if (!chip) | |
484 | return -ENOMEM; | |
485 | ||
486 | led = devm_kzalloc(&client->dev, | |
487 | sizeof(*led) * pdata->num_channels, GFP_KERNEL); | |
488 | if (!led) | |
489 | return -ENOMEM; | |
490 | ||
491 | chip->cl = client; | |
492 | chip->pdata = pdata; | |
48068d5d | 493 | chip->cfg = &lp5523_cfg; |
6a0c9a47 MWK |
494 | |
495 | mutex_init(&chip->lock); | |
0efba16c | 496 | |
6a0c9a47 | 497 | i2c_set_clientdata(client, led); |
0efba16c | 498 | |
22ebeb48 | 499 | ret = lp55xx_init_device(chip); |
0efba16c | 500 | if (ret) |
f6c64c6f | 501 | goto err_init; |
0efba16c | 502 | |
56a1e9ad | 503 | dev_info(&client->dev, "%s Programmable led chip found\n", id->name); |
0efba16c | 504 | |
9e9b3db1 | 505 | ret = lp55xx_register_leds(led, chip); |
f6524808 | 506 | if (ret) |
9e9b3db1 | 507 | goto err_register_leds; |
0efba16c | 508 | |
e73c0ce6 | 509 | ret = lp55xx_register_sysfs(chip); |
0efba16c SO |
510 | if (ret) { |
511 | dev_err(&client->dev, "registering sysfs failed\n"); | |
e73c0ce6 | 512 | goto err_register_sysfs; |
0efba16c | 513 | } |
e73c0ce6 MWK |
514 | |
515 | return 0; | |
516 | ||
517 | err_register_sysfs: | |
c3a68ebf | 518 | lp55xx_unregister_leds(led, chip); |
9e9b3db1 | 519 | err_register_leds: |
6ce61762 | 520 | lp55xx_deinit_device(chip); |
f6c64c6f | 521 | err_init: |
0efba16c SO |
522 | return ret; |
523 | } | |
524 | ||
525 | static int lp5523_remove(struct i2c_client *client) | |
526 | { | |
6ce61762 MWK |
527 | struct lp55xx_led *led = i2c_get_clientdata(client); |
528 | struct lp55xx_chip *chip = led->chip; | |
0efba16c | 529 | |
87cc4bde MWK |
530 | lp5523_stop_engine(chip); |
531 | lp55xx_unregister_sysfs(chip); | |
c3a68ebf | 532 | lp55xx_unregister_leds(led, chip); |
6ce61762 | 533 | lp55xx_deinit_device(chip); |
0efba16c | 534 | |
0efba16c SO |
535 | return 0; |
536 | } | |
537 | ||
538 | static const struct i2c_device_id lp5523_id[] = { | |
27d7704e KM |
539 | { "lp5523", LP5523 }, |
540 | { "lp55231", LP55231 }, | |
0efba16c SO |
541 | { } |
542 | }; | |
543 | ||
544 | MODULE_DEVICE_TABLE(i2c, lp5523_id); | |
545 | ||
546 | static struct i2c_driver lp5523_driver = { | |
547 | .driver = { | |
27d7704e | 548 | .name = "lp5523x", |
0efba16c SO |
549 | }, |
550 | .probe = lp5523_probe, | |
551 | .remove = lp5523_remove, | |
552 | .id_table = lp5523_id, | |
553 | }; | |
554 | ||
09a0d183 | 555 | module_i2c_driver(lp5523_driver); |
0efba16c SO |
556 | |
557 | MODULE_AUTHOR("Mathias Nyman <mathias.nyman@nokia.com>"); | |
558 | MODULE_DESCRIPTION("LP5523 LED engine"); | |
559 | MODULE_LICENSE("GPL"); |