]>
Commit | Line | Data |
---|---|---|
1802d0be | 1 | // SPDX-License-Identifier: GPL-2.0-only |
a56ba8fb SA |
2 | /* |
3 | * drivers/leds/leds-as3645a.c - AS3645A and LM3555 flash controllers driver | |
4 | * | |
5 | * Copyright (C) 2008-2011 Nokia Corporation | |
6 | * Copyright (c) 2011, 2017 Intel Corporation. | |
7 | * | |
8 | * Based on drivers/media/i2c/as3645a.c. | |
9 | * | |
10 | * Contact: Sakari Ailus <sakari.ailus@iki.fi> | |
a56ba8fb SA |
11 | */ |
12 | ||
13 | #include <linux/delay.h> | |
14 | #include <linux/gpio/consumer.h> | |
15 | #include <linux/i2c.h> | |
16 | #include <linux/led-class-flash.h> | |
17 | #include <linux/leds.h> | |
18 | #include <linux/module.h> | |
19 | #include <linux/mutex.h> | |
88b7e9ff | 20 | #include <linux/property.h> |
a56ba8fb SA |
21 | #include <linux/slab.h> |
22 | ||
23 | #include <media/v4l2-flash-led-class.h> | |
24 | ||
25 | #define AS_TIMER_US_TO_CODE(t) (((t) / 1000 - 100) / 50) | |
26 | #define AS_TIMER_CODE_TO_US(c) ((50 * (c) + 100) * 1000) | |
27 | ||
28 | /* Register definitions */ | |
29 | ||
30 | /* Read-only Design info register: Reset state: xxxx 0001 */ | |
31 | #define AS_DESIGN_INFO_REG 0x00 | |
32 | #define AS_DESIGN_INFO_FACTORY(x) (((x) >> 4)) | |
33 | #define AS_DESIGN_INFO_MODEL(x) ((x) & 0x0f) | |
34 | ||
35 | /* Read-only Version control register: Reset state: 0000 0000 | |
36 | * for first engineering samples | |
37 | */ | |
38 | #define AS_VERSION_CONTROL_REG 0x01 | |
39 | #define AS_VERSION_CONTROL_RFU(x) (((x) >> 4)) | |
40 | #define AS_VERSION_CONTROL_VERSION(x) ((x) & 0x0f) | |
41 | ||
42 | /* Read / Write (Indicator and timer register): Reset state: 0000 1111 */ | |
43 | #define AS_INDICATOR_AND_TIMER_REG 0x02 | |
44 | #define AS_INDICATOR_AND_TIMER_TIMEOUT_SHIFT 0 | |
45 | #define AS_INDICATOR_AND_TIMER_VREF_SHIFT 4 | |
46 | #define AS_INDICATOR_AND_TIMER_INDICATOR_SHIFT 6 | |
47 | ||
48 | /* Read / Write (Current set register): Reset state: 0110 1001 */ | |
49 | #define AS_CURRENT_SET_REG 0x03 | |
50 | #define AS_CURRENT_ASSIST_LIGHT_SHIFT 0 | |
51 | #define AS_CURRENT_LED_DET_ON (1 << 3) | |
52 | #define AS_CURRENT_FLASH_CURRENT_SHIFT 4 | |
53 | ||
54 | /* Read / Write (Control register): Reset state: 1011 0100 */ | |
55 | #define AS_CONTROL_REG 0x04 | |
56 | #define AS_CONTROL_MODE_SETTING_SHIFT 0 | |
57 | #define AS_CONTROL_STROBE_ON (1 << 2) | |
58 | #define AS_CONTROL_OUT_ON (1 << 3) | |
59 | #define AS_CONTROL_EXT_TORCH_ON (1 << 4) | |
60 | #define AS_CONTROL_STROBE_TYPE_EDGE (0 << 5) | |
61 | #define AS_CONTROL_STROBE_TYPE_LEVEL (1 << 5) | |
62 | #define AS_CONTROL_COIL_PEAK_SHIFT 6 | |
63 | ||
64 | /* Read only (D3 is read / write) (Fault and info): Reset state: 0000 x000 */ | |
65 | #define AS_FAULT_INFO_REG 0x05 | |
66 | #define AS_FAULT_INFO_INDUCTOR_PEAK_LIMIT (1 << 1) | |
67 | #define AS_FAULT_INFO_INDICATOR_LED (1 << 2) | |
68 | #define AS_FAULT_INFO_LED_AMOUNT (1 << 3) | |
69 | #define AS_FAULT_INFO_TIMEOUT (1 << 4) | |
70 | #define AS_FAULT_INFO_OVER_TEMPERATURE (1 << 5) | |
71 | #define AS_FAULT_INFO_SHORT_CIRCUIT (1 << 6) | |
72 | #define AS_FAULT_INFO_OVER_VOLTAGE (1 << 7) | |
73 | ||
74 | /* Boost register */ | |
75 | #define AS_BOOST_REG 0x0d | |
76 | #define AS_BOOST_CURRENT_DISABLE (0 << 0) | |
77 | #define AS_BOOST_CURRENT_ENABLE (1 << 0) | |
78 | ||
79 | /* Password register is used to unlock boost register writing */ | |
80 | #define AS_PASSWORD_REG 0x0f | |
81 | #define AS_PASSWORD_UNLOCK_VALUE 0x55 | |
82 | ||
83 | #define AS_NAME "as3645a" | |
84 | #define AS_I2C_ADDR (0x60 >> 1) /* W:0x60, R:0x61 */ | |
85 | ||
86 | #define AS_FLASH_TIMEOUT_MIN 100000 /* us */ | |
87 | #define AS_FLASH_TIMEOUT_MAX 850000 | |
88 | #define AS_FLASH_TIMEOUT_STEP 50000 | |
89 | ||
90 | #define AS_FLASH_INTENSITY_MIN 200000 /* uA */ | |
91 | #define AS_FLASH_INTENSITY_MAX_1LED 500000 | |
92 | #define AS_FLASH_INTENSITY_MAX_2LEDS 400000 | |
93 | #define AS_FLASH_INTENSITY_STEP 20000 | |
94 | ||
95 | #define AS_TORCH_INTENSITY_MIN 20000 /* uA */ | |
96 | #define AS_TORCH_INTENSITY_MAX 160000 | |
97 | #define AS_TORCH_INTENSITY_STEP 20000 | |
98 | ||
99 | #define AS_INDICATOR_INTENSITY_MIN 0 /* uA */ | |
100 | #define AS_INDICATOR_INTENSITY_MAX 10000 | |
101 | #define AS_INDICATOR_INTENSITY_STEP 2500 | |
102 | ||
103 | #define AS_PEAK_mA_MAX 2000 | |
104 | #define AS_PEAK_mA_TO_REG(a) \ | |
105 | ((min_t(u32, AS_PEAK_mA_MAX, a) - 1250) / 250) | |
106 | ||
e626c325 SA |
107 | /* LED numbers for Devicetree */ |
108 | #define AS_LED_FLASH 0 | |
109 | #define AS_LED_INDICATOR 1 | |
110 | ||
a56ba8fb SA |
111 | enum as_mode { |
112 | AS_MODE_EXT_TORCH = 0 << AS_CONTROL_MODE_SETTING_SHIFT, | |
113 | AS_MODE_INDICATOR = 1 << AS_CONTROL_MODE_SETTING_SHIFT, | |
114 | AS_MODE_ASSIST = 2 << AS_CONTROL_MODE_SETTING_SHIFT, | |
115 | AS_MODE_FLASH = 3 << AS_CONTROL_MODE_SETTING_SHIFT, | |
116 | }; | |
117 | ||
118 | struct as3645a_config { | |
119 | u32 flash_timeout_us; | |
120 | u32 flash_max_ua; | |
121 | u32 assist_max_ua; | |
122 | u32 indicator_max_ua; | |
123 | u32 voltage_reference; | |
124 | u32 peak; | |
125 | }; | |
126 | ||
a56ba8fb SA |
127 | struct as3645a { |
128 | struct i2c_client *client; | |
129 | ||
130 | struct mutex mutex; | |
131 | ||
132 | struct led_classdev_flash fled; | |
133 | struct led_classdev iled_cdev; | |
134 | ||
135 | struct v4l2_flash *vf; | |
136 | struct v4l2_flash *vfind; | |
137 | ||
88b7e9ff SA |
138 | struct fwnode_handle *flash_node; |
139 | struct fwnode_handle *indicator_node; | |
a56ba8fb SA |
140 | |
141 | struct as3645a_config cfg; | |
142 | ||
143 | enum as_mode mode; | |
144 | unsigned int timeout; | |
145 | unsigned int flash_current; | |
146 | unsigned int assist_current; | |
147 | unsigned int indicator_current; | |
148 | enum v4l2_flash_strobe_source strobe_source; | |
149 | }; | |
150 | ||
151 | #define fled_to_as3645a(__fled) container_of(__fled, struct as3645a, fled) | |
152 | #define iled_cdev_to_as3645a(__iled_cdev) \ | |
153 | container_of(__iled_cdev, struct as3645a, iled_cdev) | |
154 | ||
155 | /* Return negative errno else zero on success */ | |
156 | static int as3645a_write(struct as3645a *flash, u8 addr, u8 val) | |
157 | { | |
158 | struct i2c_client *client = flash->client; | |
159 | int rval; | |
160 | ||
161 | rval = i2c_smbus_write_byte_data(client, addr, val); | |
162 | ||
163 | dev_dbg(&client->dev, "Write Addr:%02X Val:%02X %s\n", addr, val, | |
164 | rval < 0 ? "fail" : "ok"); | |
165 | ||
166 | return rval; | |
167 | } | |
168 | ||
169 | /* Return negative errno else a data byte received from the device. */ | |
170 | static int as3645a_read(struct as3645a *flash, u8 addr) | |
171 | { | |
172 | struct i2c_client *client = flash->client; | |
173 | int rval; | |
174 | ||
175 | rval = i2c_smbus_read_byte_data(client, addr); | |
176 | ||
177 | dev_dbg(&client->dev, "Read Addr:%02X Val:%02X %s\n", addr, rval, | |
178 | rval < 0 ? "fail" : "ok"); | |
179 | ||
180 | return rval; | |
181 | } | |
182 | ||
183 | /* ----------------------------------------------------------------------------- | |
184 | * Hardware configuration and trigger | |
185 | */ | |
186 | ||
187 | /** | |
188 | * as3645a_set_config - Set flash configuration registers | |
189 | * @flash: The flash | |
190 | * | |
191 | * Configure the hardware with flash, assist and indicator currents, as well as | |
192 | * flash timeout. | |
193 | * | |
194 | * Return 0 on success, or a negative error code if an I2C communication error | |
195 | * occurred. | |
196 | */ | |
197 | static int as3645a_set_current(struct as3645a *flash) | |
198 | { | |
199 | u8 val; | |
200 | ||
201 | val = (flash->flash_current << AS_CURRENT_FLASH_CURRENT_SHIFT) | |
202 | | (flash->assist_current << AS_CURRENT_ASSIST_LIGHT_SHIFT) | |
203 | | AS_CURRENT_LED_DET_ON; | |
204 | ||
205 | return as3645a_write(flash, AS_CURRENT_SET_REG, val); | |
206 | } | |
207 | ||
208 | static int as3645a_set_timeout(struct as3645a *flash) | |
209 | { | |
210 | u8 val; | |
211 | ||
212 | val = flash->timeout << AS_INDICATOR_AND_TIMER_TIMEOUT_SHIFT; | |
213 | ||
214 | val |= (flash->cfg.voltage_reference | |
215 | << AS_INDICATOR_AND_TIMER_VREF_SHIFT) | |
216 | | ((flash->indicator_current ? flash->indicator_current - 1 : 0) | |
217 | << AS_INDICATOR_AND_TIMER_INDICATOR_SHIFT); | |
218 | ||
219 | return as3645a_write(flash, AS_INDICATOR_AND_TIMER_REG, val); | |
220 | } | |
221 | ||
222 | /** | |
223 | * as3645a_set_control - Set flash control register | |
224 | * @flash: The flash | |
225 | * @mode: Desired output mode | |
226 | * @on: Desired output state | |
227 | * | |
228 | * Configure the hardware with output mode and state. | |
229 | * | |
230 | * Return 0 on success, or a negative error code if an I2C communication error | |
231 | * occurred. | |
232 | */ | |
233 | static int | |
234 | as3645a_set_control(struct as3645a *flash, enum as_mode mode, bool on) | |
235 | { | |
236 | u8 reg; | |
237 | ||
238 | /* Configure output parameters and operation mode. */ | |
239 | reg = (flash->cfg.peak << AS_CONTROL_COIL_PEAK_SHIFT) | |
240 | | (on ? AS_CONTROL_OUT_ON : 0) | |
241 | | mode; | |
242 | ||
243 | if (mode == AS_MODE_FLASH && | |
244 | flash->strobe_source == V4L2_FLASH_STROBE_SOURCE_EXTERNAL) | |
245 | reg |= AS_CONTROL_STROBE_TYPE_LEVEL | |
246 | | AS_CONTROL_STROBE_ON; | |
247 | ||
248 | return as3645a_write(flash, AS_CONTROL_REG, reg); | |
249 | } | |
250 | ||
251 | static int as3645a_get_fault(struct led_classdev_flash *fled, u32 *fault) | |
252 | { | |
253 | struct as3645a *flash = fled_to_as3645a(fled); | |
254 | int rval; | |
255 | ||
256 | /* NOTE: reading register clears fault status */ | |
257 | rval = as3645a_read(flash, AS_FAULT_INFO_REG); | |
258 | if (rval < 0) | |
259 | return rval; | |
260 | ||
261 | if (rval & AS_FAULT_INFO_INDUCTOR_PEAK_LIMIT) | |
262 | *fault |= LED_FAULT_OVER_CURRENT; | |
263 | ||
264 | if (rval & AS_FAULT_INFO_INDICATOR_LED) | |
265 | *fault |= LED_FAULT_INDICATOR; | |
266 | ||
267 | dev_dbg(&flash->client->dev, "%u connected LEDs\n", | |
268 | rval & AS_FAULT_INFO_LED_AMOUNT ? 2 : 1); | |
269 | ||
270 | if (rval & AS_FAULT_INFO_TIMEOUT) | |
271 | *fault |= LED_FAULT_TIMEOUT; | |
272 | ||
273 | if (rval & AS_FAULT_INFO_OVER_TEMPERATURE) | |
274 | *fault |= LED_FAULT_OVER_TEMPERATURE; | |
275 | ||
276 | if (rval & AS_FAULT_INFO_SHORT_CIRCUIT) | |
277 | *fault |= LED_FAULT_OVER_CURRENT; | |
278 | ||
279 | if (rval & AS_FAULT_INFO_OVER_VOLTAGE) | |
280 | *fault |= LED_FAULT_INPUT_VOLTAGE; | |
281 | ||
282 | return rval; | |
283 | } | |
284 | ||
285 | static unsigned int __as3645a_current_to_reg(unsigned int min, unsigned int max, | |
286 | unsigned int step, | |
287 | unsigned int val) | |
288 | { | |
289 | if (val < min) | |
290 | val = min; | |
291 | ||
292 | if (val > max) | |
293 | val = max; | |
294 | ||
295 | return (val - min) / step; | |
296 | } | |
297 | ||
298 | static unsigned int as3645a_current_to_reg(struct as3645a *flash, bool is_flash, | |
299 | unsigned int ua) | |
300 | { | |
301 | if (is_flash) | |
302 | return __as3645a_current_to_reg(AS_TORCH_INTENSITY_MIN, | |
303 | flash->cfg.assist_max_ua, | |
304 | AS_TORCH_INTENSITY_STEP, ua); | |
305 | else | |
306 | return __as3645a_current_to_reg(AS_FLASH_INTENSITY_MIN, | |
307 | flash->cfg.flash_max_ua, | |
308 | AS_FLASH_INTENSITY_STEP, ua); | |
309 | } | |
310 | ||
311 | static int as3645a_set_indicator_brightness(struct led_classdev *iled_cdev, | |
312 | enum led_brightness brightness) | |
313 | { | |
314 | struct as3645a *flash = iled_cdev_to_as3645a(iled_cdev); | |
315 | int rval; | |
316 | ||
317 | flash->indicator_current = brightness; | |
318 | ||
319 | rval = as3645a_set_timeout(flash); | |
320 | if (rval) | |
321 | return rval; | |
322 | ||
323 | return as3645a_set_control(flash, AS_MODE_INDICATOR, brightness); | |
324 | } | |
325 | ||
326 | static int as3645a_set_assist_brightness(struct led_classdev *fled_cdev, | |
327 | enum led_brightness brightness) | |
328 | { | |
329 | struct led_classdev_flash *fled = lcdev_to_flcdev(fled_cdev); | |
330 | struct as3645a *flash = fled_to_as3645a(fled); | |
331 | int rval; | |
332 | ||
333 | if (brightness) { | |
334 | /* Register value 0 is 20 mA. */ | |
335 | flash->assist_current = brightness - 1; | |
336 | ||
337 | rval = as3645a_set_current(flash); | |
338 | if (rval) | |
339 | return rval; | |
340 | } | |
341 | ||
342 | return as3645a_set_control(flash, AS_MODE_ASSIST, brightness); | |
343 | } | |
344 | ||
345 | static int as3645a_set_flash_brightness(struct led_classdev_flash *fled, | |
346 | u32 brightness_ua) | |
347 | { | |
348 | struct as3645a *flash = fled_to_as3645a(fled); | |
349 | ||
6b68441b DM |
350 | flash->flash_current = as3645a_current_to_reg(flash, true, |
351 | brightness_ua); | |
a56ba8fb SA |
352 | |
353 | return as3645a_set_current(flash); | |
354 | } | |
355 | ||
356 | static int as3645a_set_flash_timeout(struct led_classdev_flash *fled, | |
357 | u32 timeout_us) | |
358 | { | |
359 | struct as3645a *flash = fled_to_as3645a(fled); | |
360 | ||
361 | flash->timeout = AS_TIMER_US_TO_CODE(timeout_us); | |
362 | ||
363 | return as3645a_set_timeout(flash); | |
364 | } | |
365 | ||
366 | static int as3645a_set_strobe(struct led_classdev_flash *fled, bool state) | |
367 | { | |
368 | struct as3645a *flash = fled_to_as3645a(fled); | |
369 | ||
370 | return as3645a_set_control(flash, AS_MODE_FLASH, state); | |
371 | } | |
372 | ||
373 | static const struct led_flash_ops as3645a_led_flash_ops = { | |
374 | .flash_brightness_set = as3645a_set_flash_brightness, | |
375 | .timeout_set = as3645a_set_flash_timeout, | |
376 | .strobe_set = as3645a_set_strobe, | |
377 | .fault_get = as3645a_get_fault, | |
378 | }; | |
379 | ||
380 | static int as3645a_setup(struct as3645a *flash) | |
381 | { | |
382 | struct device *dev = &flash->client->dev; | |
383 | u32 fault = 0; | |
384 | int rval; | |
385 | ||
386 | /* clear errors */ | |
387 | rval = as3645a_read(flash, AS_FAULT_INFO_REG); | |
388 | if (rval < 0) | |
389 | return rval; | |
390 | ||
391 | dev_dbg(dev, "Fault info: %02x\n", rval); | |
392 | ||
393 | rval = as3645a_set_current(flash); | |
394 | if (rval < 0) | |
395 | return rval; | |
396 | ||
397 | rval = as3645a_set_timeout(flash); | |
398 | if (rval < 0) | |
399 | return rval; | |
400 | ||
401 | rval = as3645a_set_control(flash, AS_MODE_INDICATOR, false); | |
402 | if (rval < 0) | |
403 | return rval; | |
404 | ||
405 | /* read status */ | |
406 | rval = as3645a_get_fault(&flash->fled, &fault); | |
407 | if (rval < 0) | |
408 | return rval; | |
409 | ||
410 | dev_dbg(dev, "AS_INDICATOR_AND_TIMER_REG: %02x\n", | |
411 | as3645a_read(flash, AS_INDICATOR_AND_TIMER_REG)); | |
412 | dev_dbg(dev, "AS_CURRENT_SET_REG: %02x\n", | |
413 | as3645a_read(flash, AS_CURRENT_SET_REG)); | |
414 | dev_dbg(dev, "AS_CONTROL_REG: %02x\n", | |
415 | as3645a_read(flash, AS_CONTROL_REG)); | |
416 | ||
417 | return rval & ~AS_FAULT_INFO_LED_AMOUNT ? -EIO : 0; | |
418 | } | |
419 | ||
420 | static int as3645a_detect(struct as3645a *flash) | |
421 | { | |
422 | struct device *dev = &flash->client->dev; | |
423 | int rval, man, model, rfu, version; | |
424 | const char *vendor; | |
425 | ||
426 | rval = as3645a_read(flash, AS_DESIGN_INFO_REG); | |
427 | if (rval < 0) { | |
428 | dev_err(dev, "can't read design info reg\n"); | |
429 | return rval; | |
430 | } | |
431 | ||
432 | man = AS_DESIGN_INFO_FACTORY(rval); | |
433 | model = AS_DESIGN_INFO_MODEL(rval); | |
434 | ||
435 | rval = as3645a_read(flash, AS_VERSION_CONTROL_REG); | |
436 | if (rval < 0) { | |
437 | dev_err(dev, "can't read version control reg\n"); | |
438 | return rval; | |
439 | } | |
440 | ||
441 | rfu = AS_VERSION_CONTROL_RFU(rval); | |
442 | version = AS_VERSION_CONTROL_VERSION(rval); | |
443 | ||
444 | /* Verify the chip model and version. */ | |
445 | if (model != 0x01 || rfu != 0x00) { | |
faa91a7b DM |
446 | dev_err(dev, "AS3645A not detected (model %d rfu %d)\n", |
447 | model, rfu); | |
a56ba8fb SA |
448 | return -ENODEV; |
449 | } | |
450 | ||
451 | switch (man) { | |
452 | case 1: | |
453 | vendor = "AMS, Austria Micro Systems"; | |
454 | break; | |
455 | case 2: | |
456 | vendor = "ADI, Analog Devices Inc."; | |
457 | break; | |
458 | case 3: | |
459 | vendor = "NSC, National Semiconductor"; | |
460 | break; | |
461 | case 4: | |
462 | vendor = "NXP"; | |
463 | break; | |
464 | case 5: | |
465 | vendor = "TI, Texas Instrument"; | |
466 | break; | |
467 | default: | |
468 | vendor = "Unknown"; | |
469 | } | |
470 | ||
471 | dev_info(dev, "Chip vendor: %s (%d) Version: %d\n", vendor, | |
472 | man, version); | |
473 | ||
474 | rval = as3645a_write(flash, AS_PASSWORD_REG, AS_PASSWORD_UNLOCK_VALUE); | |
475 | if (rval < 0) | |
476 | return rval; | |
477 | ||
478 | return as3645a_write(flash, AS_BOOST_REG, AS_BOOST_CURRENT_DISABLE); | |
479 | } | |
480 | ||
481 | static int as3645a_parse_node(struct as3645a *flash, | |
88b7e9ff | 482 | struct fwnode_handle *fwnode) |
a56ba8fb SA |
483 | { |
484 | struct as3645a_config *cfg = &flash->cfg; | |
88b7e9ff | 485 | struct fwnode_handle *child; |
a56ba8fb SA |
486 | int rval; |
487 | ||
88b7e9ff | 488 | fwnode_for_each_child_node(fwnode, child) { |
e626c325 SA |
489 | u32 id = 0; |
490 | ||
88b7e9ff | 491 | fwnode_property_read_u32(child, "reg", &id); |
e626c325 SA |
492 | |
493 | switch (id) { | |
494 | case AS_LED_FLASH: | |
88b7e9ff | 495 | flash->flash_node = child; |
22cb0a76 | 496 | fwnode_handle_get(child); |
e626c325 SA |
497 | break; |
498 | case AS_LED_INDICATOR: | |
88b7e9ff | 499 | flash->indicator_node = child; |
22cb0a76 | 500 | fwnode_handle_get(child); |
e626c325 SA |
501 | break; |
502 | default: | |
503 | dev_warn(&flash->client->dev, | |
504 | "unknown LED %u encountered, ignoring\n", id); | |
505 | break; | |
506 | } | |
507 | } | |
508 | ||
a56ba8fb SA |
509 | if (!flash->flash_node) { |
510 | dev_err(&flash->client->dev, "can't find flash node\n"); | |
511 | return -ENODEV; | |
512 | } | |
513 | ||
88b7e9ff SA |
514 | rval = fwnode_property_read_u32(flash->flash_node, "flash-timeout-us", |
515 | &cfg->flash_timeout_us); | |
a56ba8fb SA |
516 | if (rval < 0) { |
517 | dev_err(&flash->client->dev, | |
518 | "can't read flash-timeout-us property for flash\n"); | |
519 | goto out_err; | |
520 | } | |
521 | ||
88b7e9ff SA |
522 | rval = fwnode_property_read_u32(flash->flash_node, "flash-max-microamp", |
523 | &cfg->flash_max_ua); | |
a56ba8fb SA |
524 | if (rval < 0) { |
525 | dev_err(&flash->client->dev, | |
526 | "can't read flash-max-microamp property for flash\n"); | |
527 | goto out_err; | |
528 | } | |
529 | ||
88b7e9ff SA |
530 | rval = fwnode_property_read_u32(flash->flash_node, "led-max-microamp", |
531 | &cfg->assist_max_ua); | |
a56ba8fb SA |
532 | if (rval < 0) { |
533 | dev_err(&flash->client->dev, | |
534 | "can't read led-max-microamp property for flash\n"); | |
535 | goto out_err; | |
536 | } | |
537 | ||
88b7e9ff SA |
538 | fwnode_property_read_u32(flash->flash_node, "voltage-reference", |
539 | &cfg->voltage_reference); | |
a56ba8fb | 540 | |
88b7e9ff SA |
541 | fwnode_property_read_u32(flash->flash_node, "ams,input-max-microamp", |
542 | &cfg->peak); | |
a56ba8fb SA |
543 | cfg->peak = AS_PEAK_mA_TO_REG(cfg->peak); |
544 | ||
a56ba8fb SA |
545 | if (!flash->indicator_node) { |
546 | dev_warn(&flash->client->dev, | |
547 | "can't find indicator node\n"); | |
548 | goto out_err; | |
549 | } | |
550 | ||
a56ba8fb | 551 | |
88b7e9ff SA |
552 | rval = fwnode_property_read_u32(flash->indicator_node, |
553 | "led-max-microamp", | |
554 | &cfg->indicator_max_ua); | |
a56ba8fb SA |
555 | if (rval < 0) { |
556 | dev_err(&flash->client->dev, | |
557 | "can't read led-max-microamp property for indicator\n"); | |
558 | goto out_err; | |
559 | } | |
560 | ||
561 | return 0; | |
562 | ||
563 | out_err: | |
88b7e9ff SA |
564 | fwnode_handle_put(flash->flash_node); |
565 | fwnode_handle_put(flash->indicator_node); | |
a56ba8fb SA |
566 | |
567 | return rval; | |
568 | } | |
569 | ||
b1f24498 | 570 | static int as3645a_led_class_setup(struct as3645a *flash) |
a56ba8fb SA |
571 | { |
572 | struct led_classdev *fled_cdev = &flash->fled.led_cdev; | |
573 | struct led_classdev *iled_cdev = &flash->iled_cdev; | |
b1f24498 | 574 | struct led_init_data init_data = {}; |
a56ba8fb SA |
575 | struct led_flash_setting *cfg; |
576 | int rval; | |
577 | ||
a56ba8fb SA |
578 | iled_cdev->brightness_set_blocking = as3645a_set_indicator_brightness; |
579 | iled_cdev->max_brightness = | |
580 | flash->cfg.indicator_max_ua / AS_INDICATOR_INTENSITY_STEP; | |
581 | iled_cdev->flags = LED_CORE_SUSPENDRESUME; | |
582 | ||
b1f24498 JA |
583 | init_data.fwnode = flash->indicator_node; |
584 | init_data.devicename = AS_NAME; | |
585 | init_data.default_label = "indicator"; | |
586 | ||
587 | rval = led_classdev_register_ext(&flash->client->dev, iled_cdev, | |
588 | &init_data); | |
a56ba8fb SA |
589 | if (rval < 0) |
590 | return rval; | |
591 | ||
592 | cfg = &flash->fled.brightness; | |
593 | cfg->min = AS_FLASH_INTENSITY_MIN; | |
594 | cfg->max = flash->cfg.flash_max_ua; | |
595 | cfg->step = AS_FLASH_INTENSITY_STEP; | |
596 | cfg->val = flash->cfg.flash_max_ua; | |
597 | ||
598 | cfg = &flash->fled.timeout; | |
599 | cfg->min = AS_FLASH_TIMEOUT_MIN; | |
600 | cfg->max = flash->cfg.flash_timeout_us; | |
601 | cfg->step = AS_FLASH_TIMEOUT_STEP; | |
602 | cfg->val = flash->cfg.flash_timeout_us; | |
603 | ||
604 | flash->fled.ops = &as3645a_led_flash_ops; | |
605 | ||
a56ba8fb SA |
606 | fled_cdev->brightness_set_blocking = as3645a_set_assist_brightness; |
607 | /* Value 0 is off in LED class. */ | |
608 | fled_cdev->max_brightness = | |
609 | as3645a_current_to_reg(flash, false, | |
610 | flash->cfg.assist_max_ua) + 1; | |
611 | fled_cdev->flags = LED_DEV_CAP_FLASH | LED_CORE_SUSPENDRESUME; | |
612 | ||
b1f24498 JA |
613 | init_data.fwnode = flash->flash_node; |
614 | init_data.devicename = AS_NAME; | |
615 | init_data.default_label = "flash"; | |
616 | ||
617 | rval = led_classdev_flash_register_ext(&flash->client->dev, | |
618 | &flash->fled, &init_data); | |
619 | if (rval) | |
620 | goto out_err; | |
a56ba8fb SA |
621 | |
622 | return rval; | |
b1f24498 JA |
623 | |
624 | out_err: | |
625 | led_classdev_unregister(iled_cdev); | |
626 | dev_err(&flash->client->dev, | |
627 | "led_classdev_flash_register() failed, error %d\n", | |
628 | rval); | |
629 | return rval; | |
a56ba8fb SA |
630 | } |
631 | ||
632 | static int as3645a_v4l2_setup(struct as3645a *flash) | |
633 | { | |
634 | struct led_classdev_flash *fled = &flash->fled; | |
635 | struct led_classdev *led = &fled->led_cdev; | |
636 | struct v4l2_flash_config cfg = { | |
637 | .intensity = { | |
638 | .min = AS_TORCH_INTENSITY_MIN, | |
639 | .max = flash->cfg.assist_max_ua, | |
640 | .step = AS_TORCH_INTENSITY_STEP, | |
641 | .val = flash->cfg.assist_max_ua, | |
642 | }, | |
643 | }; | |
644 | struct v4l2_flash_config cfgind = { | |
645 | .intensity = { | |
646 | .min = AS_INDICATOR_INTENSITY_MIN, | |
647 | .max = flash->cfg.indicator_max_ua, | |
648 | .step = AS_INDICATOR_INTENSITY_STEP, | |
649 | .val = flash->cfg.indicator_max_ua, | |
650 | }, | |
651 | }; | |
652 | ||
b1f24498 JA |
653 | strlcpy(cfg.dev_name, led->dev->kobj.name, sizeof(cfg.dev_name)); |
654 | strlcpy(cfgind.dev_name, flash->iled_cdev.dev->kobj.name, | |
655 | sizeof(cfgind.dev_name)); | |
a56ba8fb SA |
656 | |
657 | flash->vf = v4l2_flash_init( | |
88b7e9ff SA |
658 | &flash->client->dev, flash->flash_node, &flash->fled, NULL, |
659 | &cfg); | |
a56ba8fb SA |
660 | if (IS_ERR(flash->vf)) |
661 | return PTR_ERR(flash->vf); | |
662 | ||
663 | flash->vfind = v4l2_flash_indicator_init( | |
88b7e9ff SA |
664 | &flash->client->dev, flash->indicator_node, &flash->iled_cdev, |
665 | &cfgind); | |
a56ba8fb SA |
666 | if (IS_ERR(flash->vfind)) { |
667 | v4l2_flash_release(flash->vf); | |
668 | return PTR_ERR(flash->vfind); | |
669 | } | |
670 | ||
671 | return 0; | |
672 | } | |
673 | ||
674 | static int as3645a_probe(struct i2c_client *client) | |
675 | { | |
a56ba8fb SA |
676 | struct as3645a *flash; |
677 | int rval; | |
678 | ||
88b7e9ff | 679 | if (!dev_fwnode(&client->dev)) |
a56ba8fb SA |
680 | return -ENODEV; |
681 | ||
682 | flash = devm_kzalloc(&client->dev, sizeof(*flash), GFP_KERNEL); | |
683 | if (flash == NULL) | |
684 | return -ENOMEM; | |
685 | ||
686 | flash->client = client; | |
687 | ||
b1f24498 | 688 | rval = as3645a_parse_node(flash, dev_fwnode(&client->dev)); |
a56ba8fb SA |
689 | if (rval < 0) |
690 | return rval; | |
691 | ||
692 | rval = as3645a_detect(flash); | |
693 | if (rval < 0) | |
694 | goto out_put_nodes; | |
695 | ||
696 | mutex_init(&flash->mutex); | |
697 | i2c_set_clientdata(client, flash); | |
698 | ||
699 | rval = as3645a_setup(flash); | |
700 | if (rval) | |
701 | goto out_mutex_destroy; | |
702 | ||
b1f24498 | 703 | rval = as3645a_led_class_setup(flash); |
a56ba8fb SA |
704 | if (rval) |
705 | goto out_mutex_destroy; | |
706 | ||
707 | rval = as3645a_v4l2_setup(flash); | |
708 | if (rval) | |
709 | goto out_led_classdev_flash_unregister; | |
710 | ||
711 | return 0; | |
712 | ||
713 | out_led_classdev_flash_unregister: | |
714 | led_classdev_flash_unregister(&flash->fled); | |
715 | ||
716 | out_mutex_destroy: | |
717 | mutex_destroy(&flash->mutex); | |
718 | ||
719 | out_put_nodes: | |
88b7e9ff SA |
720 | fwnode_handle_put(flash->flash_node); |
721 | fwnode_handle_put(flash->indicator_node); | |
a56ba8fb SA |
722 | |
723 | return rval; | |
724 | } | |
725 | ||
726 | static int as3645a_remove(struct i2c_client *client) | |
727 | { | |
728 | struct as3645a *flash = i2c_get_clientdata(client); | |
729 | ||
730 | as3645a_set_control(flash, AS_MODE_EXT_TORCH, false); | |
731 | ||
732 | v4l2_flash_release(flash->vf); | |
12c4b878 | 733 | v4l2_flash_release(flash->vfind); |
a56ba8fb SA |
734 | |
735 | led_classdev_flash_unregister(&flash->fled); | |
736 | led_classdev_unregister(&flash->iled_cdev); | |
737 | ||
738 | mutex_destroy(&flash->mutex); | |
739 | ||
88b7e9ff SA |
740 | fwnode_handle_put(flash->flash_node); |
741 | fwnode_handle_put(flash->indicator_node); | |
a56ba8fb SA |
742 | |
743 | return 0; | |
744 | } | |
745 | ||
746 | static const struct i2c_device_id as3645a_id_table[] = { | |
747 | { AS_NAME, 0 }, | |
748 | { }, | |
749 | }; | |
750 | MODULE_DEVICE_TABLE(i2c, as3645a_id_table); | |
751 | ||
752 | static const struct of_device_id as3645a_of_table[] = { | |
753 | { .compatible = "ams,as3645a" }, | |
754 | { }, | |
755 | }; | |
756 | MODULE_DEVICE_TABLE(of, as3645a_of_table); | |
757 | ||
758 | static struct i2c_driver as3645a_i2c_driver = { | |
759 | .driver = { | |
760 | .of_match_table = as3645a_of_table, | |
761 | .name = AS_NAME, | |
762 | }, | |
763 | .probe_new = as3645a_probe, | |
764 | .remove = as3645a_remove, | |
765 | .id_table = as3645a_id_table, | |
766 | }; | |
767 | ||
768 | module_i2c_driver(as3645a_i2c_driver); | |
769 | ||
770 | MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>"); | |
771 | MODULE_AUTHOR("Sakari Ailus <sakari.ailus@iki.fi>"); | |
772 | MODULE_DESCRIPTION("LED flash driver for AS3645A, LM3555 and their clones"); | |
773 | MODULE_LICENSE("GPL v2"); |