]>
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 | ||
0efba16c SO |
145 | struct lp5523_led { |
146 | int id; | |
147 | u8 chan_nr; | |
148 | u8 led_current; | |
149 | u8 max_current; | |
150 | struct led_classdev cdev; | |
151 | struct work_struct brightness_work; | |
152 | u8 brightness; | |
153 | }; | |
154 | ||
155 | struct lp5523_chip { | |
156 | struct mutex lock; /* Serialize control */ | |
157 | struct i2c_client *client; | |
0e202346 | 158 | struct lp5523_led leds[LP5523_MAX_LEDS]; |
0efba16c SO |
159 | struct lp5523_platform_data *pdata; |
160 | u8 num_channels; | |
161 | u8 num_leds; | |
162 | }; | |
163 | ||
db6eaf83 MWK |
164 | static inline void lp5523_wait_opmode_done(void) |
165 | { | |
166 | usleep_range(1000, 2000); | |
167 | } | |
168 | ||
a96bfa13 MWK |
169 | static void lp5523_set_led_current(struct lp55xx_led *led, u8 led_current) |
170 | { | |
171 | led->led_current = led_current; | |
172 | lp55xx_write(led->chip, LP5523_REG_LED_CURRENT_BASE + led->chan_nr, | |
173 | led_current); | |
174 | } | |
175 | ||
0efba16c SO |
176 | static int lp5523_write(struct i2c_client *client, u8 reg, u8 value) |
177 | { | |
178 | return i2c_smbus_write_byte_data(client, reg, value); | |
179 | } | |
180 | ||
ffbdccdb | 181 | static int lp5523_post_init_device(struct lp55xx_chip *chip) |
0efba16c | 182 | { |
ffbdccdb | 183 | int ret; |
0efba16c | 184 | |
ffbdccdb | 185 | ret = lp55xx_write(chip, LP5523_REG_ENABLE, LP5523_ENABLE); |
632418bf MWK |
186 | if (ret) |
187 | return ret; | |
0efba16c | 188 | |
2e4840ed SO |
189 | /* Chip startup time is 500 us, 1 - 2 ms gives some margin */ |
190 | usleep_range(1000, 2000); | |
0efba16c | 191 | |
ffbdccdb | 192 | ret = lp55xx_write(chip, LP5523_REG_CONFIG, |
0efba16c SO |
193 | LP5523_AUTO_INC | LP5523_PWR_SAVE | |
194 | LP5523_CP_AUTO | LP5523_AUTO_CLK | | |
195 | LP5523_PWM_PWR_SAVE); | |
632418bf MWK |
196 | if (ret) |
197 | return ret; | |
0efba16c SO |
198 | |
199 | /* turn on all leds */ | |
ffbdccdb | 200 | ret = lp55xx_write(chip, LP5523_REG_ENABLE_LEDS_MSB, 0x01); |
632418bf | 201 | if (ret) |
1b21ec5a JH |
202 | return ret; |
203 | ||
ffbdccdb | 204 | return lp55xx_write(chip, LP5523_REG_ENABLE_LEDS_LSB, 0xff); |
0efba16c SO |
205 | } |
206 | ||
db6eaf83 | 207 | static void lp5523_load_engine(struct lp55xx_chip *chip) |
0efba16c | 208 | { |
db6eaf83 MWK |
209 | enum lp55xx_engine_index idx = chip->engine_idx; |
210 | u8 mask[] = { | |
211 | [LP55XX_ENGINE_1] = LP5523_MODE_ENG1_M, | |
212 | [LP55XX_ENGINE_2] = LP5523_MODE_ENG2_M, | |
213 | [LP55XX_ENGINE_3] = LP5523_MODE_ENG3_M, | |
214 | }; | |
215 | ||
216 | u8 val[] = { | |
217 | [LP55XX_ENGINE_1] = LP5523_LOAD_ENG1, | |
218 | [LP55XX_ENGINE_2] = LP5523_LOAD_ENG2, | |
219 | [LP55XX_ENGINE_3] = LP5523_LOAD_ENG3, | |
220 | }; | |
221 | ||
222 | u8 page_sel[] = { | |
223 | [LP55XX_ENGINE_1] = LP5523_PAGE_ENG1, | |
224 | [LP55XX_ENGINE_2] = LP5523_PAGE_ENG2, | |
225 | [LP55XX_ENGINE_3] = LP5523_PAGE_ENG3, | |
226 | }; | |
227 | ||
228 | lp55xx_update_bits(chip, LP5523_REG_OP_MODE, mask[idx], val[idx]); | |
229 | ||
230 | lp5523_wait_opmode_done(); | |
231 | ||
232 | lp55xx_write(chip, LP5523_REG_PROG_PAGE_SEL, page_sel[idx]); | |
0efba16c SO |
233 | } |
234 | ||
db6eaf83 | 235 | static void lp5523_stop_engine(struct lp55xx_chip *chip) |
0efba16c | 236 | { |
db6eaf83 MWK |
237 | lp55xx_write(chip, LP5523_REG_OP_MODE, 0); |
238 | lp5523_wait_opmode_done(); | |
239 | } | |
0efba16c | 240 | |
db6eaf83 MWK |
241 | static void lp5523_turn_off_channels(struct lp55xx_chip *chip) |
242 | { | |
243 | int i; | |
0efba16c | 244 | |
db6eaf83 MWK |
245 | for (i = 0; i < LP5523_MAX_LEDS; i++) |
246 | lp55xx_write(chip, LP5523_REG_LED_PWM_BASE + i, 0); | |
0efba16c SO |
247 | } |
248 | ||
db6eaf83 | 249 | static void lp5523_run_engine(struct lp55xx_chip *chip, bool start) |
0efba16c | 250 | { |
db6eaf83 MWK |
251 | int ret; |
252 | u8 mode; | |
253 | u8 exec; | |
0efba16c | 254 | |
db6eaf83 MWK |
255 | /* stop engine */ |
256 | if (!start) { | |
257 | lp5523_stop_engine(chip); | |
258 | lp5523_turn_off_channels(chip); | |
259 | return; | |
260 | } | |
0efba16c | 261 | |
db6eaf83 MWK |
262 | /* |
263 | * To run the engine, | |
264 | * operation mode and enable register should updated at the same time | |
265 | */ | |
0efba16c | 266 | |
db6eaf83 MWK |
267 | ret = lp55xx_read(chip, LP5523_REG_OP_MODE, &mode); |
268 | if (ret) | |
269 | return; | |
0efba16c | 270 | |
db6eaf83 MWK |
271 | ret = lp55xx_read(chip, LP5523_REG_ENABLE, &exec); |
272 | if (ret) | |
273 | return; | |
0efba16c | 274 | |
db6eaf83 MWK |
275 | /* change operation mode to RUN only when each engine is loading */ |
276 | if (LP5523_ENG1_IS_LOADING(mode)) { | |
277 | mode = (mode & ~LP5523_MODE_ENG1_M) | LP5523_RUN_ENG1; | |
278 | exec = (exec & ~LP5523_EXEC_ENG1_M) | LP5523_RUN_ENG1; | |
279 | } | |
0efba16c | 280 | |
db6eaf83 MWK |
281 | if (LP5523_ENG2_IS_LOADING(mode)) { |
282 | mode = (mode & ~LP5523_MODE_ENG2_M) | LP5523_RUN_ENG2; | |
283 | exec = (exec & ~LP5523_EXEC_ENG2_M) | LP5523_RUN_ENG2; | |
284 | } | |
0efba16c | 285 | |
db6eaf83 MWK |
286 | if (LP5523_ENG3_IS_LOADING(mode)) { |
287 | mode = (mode & ~LP5523_MODE_ENG3_M) | LP5523_RUN_ENG3; | |
288 | exec = (exec & ~LP5523_EXEC_ENG3_M) | LP5523_RUN_ENG3; | |
289 | } | |
290 | ||
291 | lp55xx_write(chip, LP5523_REG_OP_MODE, mode); | |
292 | lp5523_wait_opmode_done(); | |
293 | ||
294 | lp55xx_update_bits(chip, LP5523_REG_ENABLE, LP5523_EXEC_M, exec); | |
0efba16c SO |
295 | } |
296 | ||
db6eaf83 MWK |
297 | static int lp5523_update_program_memory(struct lp55xx_chip *chip, |
298 | const u8 *data, size_t size) | |
0efba16c | 299 | { |
db6eaf83 MWK |
300 | u8 pattern[LP5523_PROGRAM_LENGTH] = {0}; |
301 | unsigned cmd; | |
302 | char c[3]; | |
303 | int update_size; | |
304 | int nrchars; | |
305 | int offset = 0; | |
306 | int ret; | |
0efba16c | 307 | int i; |
0efba16c | 308 | |
db6eaf83 MWK |
309 | /* clear program memory before updating */ |
310 | for (i = 0; i < LP5523_PROGRAM_LENGTH; i++) | |
311 | lp55xx_write(chip, LP5523_REG_PROG_MEM + i, 0); | |
0efba16c | 312 | |
db6eaf83 MWK |
313 | i = 0; |
314 | while ((offset < size - 1) && (i < LP5523_PROGRAM_LENGTH)) { | |
315 | /* separate sscanfs because length is working only for %s */ | |
316 | ret = sscanf(data + offset, "%2s%n ", c, &nrchars); | |
317 | if (ret != 1) | |
318 | goto err; | |
0efba16c | 319 | |
db6eaf83 MWK |
320 | ret = sscanf(c, "%2x", &cmd); |
321 | if (ret != 1) | |
322 | goto err; | |
0efba16c | 323 | |
db6eaf83 MWK |
324 | pattern[i] = (u8)cmd; |
325 | offset += nrchars; | |
326 | i++; | |
327 | } | |
0efba16c | 328 | |
db6eaf83 MWK |
329 | /* Each instruction is 16bit long. Check that length is even */ |
330 | if (i % 2) | |
331 | goto err; | |
0efba16c | 332 | |
db6eaf83 MWK |
333 | update_size = i; |
334 | for (i = 0; i < update_size; i++) | |
335 | lp55xx_write(chip, LP5523_REG_PROG_MEM + i, pattern[i]); | |
0efba16c | 336 | |
db6eaf83 | 337 | return 0; |
0efba16c | 338 | |
db6eaf83 MWK |
339 | err: |
340 | dev_err(&chip->cl->dev, "wrong pattern format\n"); | |
341 | return -EINVAL; | |
0efba16c | 342 | } |
0efba16c | 343 | |
db6eaf83 | 344 | static void lp5523_firmware_loaded(struct lp55xx_chip *chip) |
0efba16c | 345 | { |
db6eaf83 | 346 | const struct firmware *fw = chip->fw; |
fbac0812 | 347 | |
db6eaf83 MWK |
348 | if (fw->size > LP5523_PROGRAM_LENGTH) { |
349 | dev_err(&chip->cl->dev, "firmware data size overflow: %zu\n", | |
350 | fw->size); | |
351 | return; | |
352 | } | |
0efba16c | 353 | |
db6eaf83 MWK |
354 | /* |
355 | * Program momery sequence | |
356 | * 1) set engine mode to "LOAD" | |
357 | * 2) write firmware data into program memory | |
358 | */ | |
0efba16c | 359 | |
db6eaf83 MWK |
360 | lp5523_load_engine(chip); |
361 | lp5523_update_program_memory(chip, fw->data, fw->size); | |
0efba16c | 362 | } |
0efba16c SO |
363 | |
364 | static ssize_t lp5523_selftest(struct device *dev, | |
365 | struct device_attribute *attr, | |
366 | char *buf) | |
367 | { | |
9ca3bd80 MWK |
368 | struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev)); |
369 | struct lp55xx_chip *chip = led->chip; | |
370 | struct lp55xx_platform_data *pdata = chip->pdata; | |
0efba16c | 371 | int i, ret, pos = 0; |
0efba16c SO |
372 | u8 status, adc, vdd; |
373 | ||
374 | mutex_lock(&chip->lock); | |
375 | ||
9ca3bd80 | 376 | ret = lp55xx_read(chip, LP5523_REG_STATUS, &status); |
0efba16c SO |
377 | if (ret < 0) |
378 | goto fail; | |
379 | ||
380 | /* Check that ext clock is really in use if requested */ | |
9ca3bd80 | 381 | if (pdata->clock_mode == LP55XX_CLOCK_EXT) { |
0efba16c SO |
382 | if ((status & LP5523_EXT_CLK_USED) == 0) |
383 | goto fail; | |
9ca3bd80 | 384 | } |
0efba16c SO |
385 | |
386 | /* Measure VDD (i.e. VBAT) first (channel 16 corresponds to VDD) */ | |
9ca3bd80 | 387 | lp55xx_write(chip, LP5523_REG_LED_TEST_CTRL, LP5523_EN_LEDTEST | 16); |
2e4840ed | 388 | usleep_range(3000, 6000); /* ADC conversion time is typically 2.7 ms */ |
9ca3bd80 | 389 | ret = lp55xx_read(chip, LP5523_REG_STATUS, &status); |
1b21ec5a JH |
390 | if (ret < 0) |
391 | goto fail; | |
392 | ||
0efba16c | 393 | if (!(status & LP5523_LEDTEST_DONE)) |
2e4840ed | 394 | usleep_range(3000, 6000); /* Was not ready. Wait little bit */ |
0efba16c | 395 | |
9ca3bd80 | 396 | ret = lp55xx_read(chip, LP5523_REG_LED_TEST_ADC, &vdd); |
1b21ec5a JH |
397 | if (ret < 0) |
398 | goto fail; | |
399 | ||
0efba16c SO |
400 | vdd--; /* There may be some fluctuation in measurement */ |
401 | ||
0e202346 | 402 | for (i = 0; i < LP5523_MAX_LEDS; i++) { |
0efba16c | 403 | /* Skip non-existing channels */ |
9ca3bd80 | 404 | if (pdata->led_config[i].led_current == 0) |
0efba16c SO |
405 | continue; |
406 | ||
407 | /* Set default current */ | |
9ca3bd80 MWK |
408 | lp55xx_write(chip, LP5523_REG_LED_CURRENT_BASE + i, |
409 | pdata->led_config[i].led_current); | |
0efba16c | 410 | |
9ca3bd80 | 411 | lp55xx_write(chip, LP5523_REG_LED_PWM_BASE + i, 0xff); |
2e4840ed SO |
412 | /* let current stabilize 2 - 4ms before measurements start */ |
413 | usleep_range(2000, 4000); | |
9ca3bd80 | 414 | lp55xx_write(chip, LP5523_REG_LED_TEST_CTRL, |
0efba16c | 415 | LP5523_EN_LEDTEST | i); |
2e4840ed SO |
416 | /* ADC conversion time is 2.7 ms typically */ |
417 | usleep_range(3000, 6000); | |
9ca3bd80 | 418 | ret = lp55xx_read(chip, LP5523_REG_STATUS, &status); |
1b21ec5a JH |
419 | if (ret < 0) |
420 | goto fail; | |
421 | ||
0efba16c | 422 | if (!(status & LP5523_LEDTEST_DONE)) |
2e4840ed | 423 | usleep_range(3000, 6000);/* Was not ready. Wait. */ |
9ca3bd80 MWK |
424 | |
425 | ret = lp55xx_read(chip, LP5523_REG_LED_TEST_ADC, &adc); | |
1b21ec5a JH |
426 | if (ret < 0) |
427 | goto fail; | |
0efba16c SO |
428 | |
429 | if (adc >= vdd || adc < LP5523_ADC_SHORTCIRC_LIM) | |
430 | pos += sprintf(buf + pos, "LED %d FAIL\n", i); | |
431 | ||
9ca3bd80 | 432 | lp55xx_write(chip, LP5523_REG_LED_PWM_BASE + i, 0x00); |
0efba16c SO |
433 | |
434 | /* Restore current */ | |
9ca3bd80 MWK |
435 | lp55xx_write(chip, LP5523_REG_LED_CURRENT_BASE + i, |
436 | led->led_current); | |
0efba16c SO |
437 | led++; |
438 | } | |
439 | if (pos == 0) | |
440 | pos = sprintf(buf, "OK\n"); | |
441 | goto release_lock; | |
442 | fail: | |
443 | pos = sprintf(buf, "FAIL\n"); | |
444 | ||
445 | release_lock: | |
446 | mutex_unlock(&chip->lock); | |
447 | ||
448 | return pos; | |
449 | } | |
450 | ||
0efba16c SO |
451 | static void lp5523_led_brightness_work(struct work_struct *work) |
452 | { | |
a6e4679a | 453 | struct lp55xx_led *led = container_of(work, struct lp55xx_led, |
0efba16c | 454 | brightness_work); |
a6e4679a | 455 | struct lp55xx_chip *chip = led->chip; |
0efba16c SO |
456 | |
457 | mutex_lock(&chip->lock); | |
a6e4679a | 458 | lp55xx_write(chip, LP5523_REG_LED_PWM_BASE + led->chan_nr, |
0efba16c | 459 | led->brightness); |
0efba16c SO |
460 | mutex_unlock(&chip->lock); |
461 | } | |
462 | ||
0efba16c SO |
463 | static DEVICE_ATTR(selftest, S_IRUGO, lp5523_selftest, NULL); |
464 | ||
465 | static struct attribute *lp5523_attributes[] = { | |
0efba16c | 466 | &dev_attr_selftest.attr, |
f1625842 | 467 | NULL, |
0efba16c SO |
468 | }; |
469 | ||
470 | static const struct attribute_group lp5523_group = { | |
471 | .attrs = lp5523_attributes, | |
472 | }; | |
473 | ||
0efba16c SO |
474 | static void lp5523_unregister_sysfs(struct i2c_client *client) |
475 | { | |
0efba16c | 476 | struct device *dev = &client->dev; |
0efba16c SO |
477 | |
478 | sysfs_remove_group(&dev->kobj, &lp5523_group); | |
0efba16c SO |
479 | } |
480 | ||
48068d5d MWK |
481 | /* Chip specific configurations */ |
482 | static struct lp55xx_device_config lp5523_cfg = { | |
483 | .reset = { | |
484 | .addr = LP5523_REG_RESET, | |
485 | .val = LP5523_RESET, | |
486 | }, | |
e3a700d8 MWK |
487 | .enable = { |
488 | .addr = LP5523_REG_ENABLE, | |
489 | .val = LP5523_ENABLE, | |
490 | }, | |
0e202346 | 491 | .max_channel = LP5523_MAX_LEDS, |
ffbdccdb | 492 | .post_init_device = lp5523_post_init_device, |
a6e4679a | 493 | .brightness_work_fn = lp5523_led_brightness_work, |
a96bfa13 | 494 | .set_led_current = lp5523_set_led_current, |
db6eaf83 MWK |
495 | .firmware_cb = lp5523_firmware_loaded, |
496 | .run_engine = lp5523_run_engine, | |
e73c0ce6 | 497 | .dev_attr_group = &lp5523_group, |
48068d5d MWK |
498 | }; |
499 | ||
98ea1ea2 | 500 | static int lp5523_probe(struct i2c_client *client, |
0efba16c SO |
501 | const struct i2c_device_id *id) |
502 | { | |
22ebeb48 | 503 | int ret; |
6a0c9a47 MWK |
504 | struct lp55xx_chip *chip; |
505 | struct lp55xx_led *led; | |
506 | struct lp55xx_platform_data *pdata = client->dev.platform_data; | |
0efba16c | 507 | |
6a0c9a47 | 508 | if (!pdata) { |
0efba16c | 509 | dev_err(&client->dev, "no platform data\n"); |
94ca4bcc | 510 | return -EINVAL; |
0efba16c SO |
511 | } |
512 | ||
6a0c9a47 MWK |
513 | chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL); |
514 | if (!chip) | |
515 | return -ENOMEM; | |
516 | ||
517 | led = devm_kzalloc(&client->dev, | |
518 | sizeof(*led) * pdata->num_channels, GFP_KERNEL); | |
519 | if (!led) | |
520 | return -ENOMEM; | |
521 | ||
522 | chip->cl = client; | |
523 | chip->pdata = pdata; | |
48068d5d | 524 | chip->cfg = &lp5523_cfg; |
6a0c9a47 MWK |
525 | |
526 | mutex_init(&chip->lock); | |
0efba16c | 527 | |
6a0c9a47 | 528 | i2c_set_clientdata(client, led); |
0efba16c | 529 | |
22ebeb48 | 530 | ret = lp55xx_init_device(chip); |
0efba16c | 531 | if (ret) |
f6c64c6f | 532 | goto err_init; |
0efba16c | 533 | |
56a1e9ad | 534 | dev_info(&client->dev, "%s Programmable led chip found\n", id->name); |
0efba16c | 535 | |
9e9b3db1 | 536 | ret = lp55xx_register_leds(led, chip); |
f6524808 | 537 | if (ret) |
9e9b3db1 | 538 | goto err_register_leds; |
0efba16c | 539 | |
e73c0ce6 | 540 | ret = lp55xx_register_sysfs(chip); |
0efba16c SO |
541 | if (ret) { |
542 | dev_err(&client->dev, "registering sysfs failed\n"); | |
e73c0ce6 | 543 | goto err_register_sysfs; |
0efba16c | 544 | } |
e73c0ce6 MWK |
545 | |
546 | return 0; | |
547 | ||
548 | err_register_sysfs: | |
c3a68ebf | 549 | lp55xx_unregister_leds(led, chip); |
9e9b3db1 | 550 | err_register_leds: |
6ce61762 | 551 | lp55xx_deinit_device(chip); |
f6c64c6f | 552 | err_init: |
0efba16c SO |
553 | return ret; |
554 | } | |
555 | ||
556 | static int lp5523_remove(struct i2c_client *client) | |
557 | { | |
6ce61762 MWK |
558 | struct lp55xx_led *led = i2c_get_clientdata(client); |
559 | struct lp55xx_chip *chip = led->chip; | |
0efba16c | 560 | |
23301b7f KM |
561 | /* Disable engine mode */ |
562 | lp5523_write(client, LP5523_REG_OP_MODE, LP5523_CMD_DISABLED); | |
563 | ||
0efba16c SO |
564 | lp5523_unregister_sysfs(client); |
565 | ||
c3a68ebf | 566 | lp55xx_unregister_leds(led, chip); |
6ce61762 | 567 | lp55xx_deinit_device(chip); |
0efba16c | 568 | |
0efba16c SO |
569 | return 0; |
570 | } | |
571 | ||
572 | static const struct i2c_device_id lp5523_id[] = { | |
27d7704e KM |
573 | { "lp5523", LP5523 }, |
574 | { "lp55231", LP55231 }, | |
0efba16c SO |
575 | { } |
576 | }; | |
577 | ||
578 | MODULE_DEVICE_TABLE(i2c, lp5523_id); | |
579 | ||
580 | static struct i2c_driver lp5523_driver = { | |
581 | .driver = { | |
27d7704e | 582 | .name = "lp5523x", |
0efba16c SO |
583 | }, |
584 | .probe = lp5523_probe, | |
585 | .remove = lp5523_remove, | |
586 | .id_table = lp5523_id, | |
587 | }; | |
588 | ||
09a0d183 | 589 | module_i2c_driver(lp5523_driver); |
0efba16c SO |
590 | |
591 | MODULE_AUTHOR("Mathias Nyman <mathias.nyman@nokia.com>"); | |
592 | MODULE_DESCRIPTION("LP5523 LED engine"); | |
593 | MODULE_LICENSE("GPL"); |