Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / leds / leds-as3645a.c
blobe8922fa03379620f4ee15896d224bd81535a589f
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * drivers/leds/leds-as3645a.c - AS3645A and LM3555 flash controllers driver
5 * Copyright (C) 2008-2011 Nokia Corporation
6 * Copyright (c) 2011, 2017 Intel Corporation.
8 * Based on drivers/media/i2c/as3645a.c.
10 * Contact: Sakari Ailus <sakari.ailus@iki.fi>
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>
20 #include <linux/property.h>
21 #include <linux/slab.h>
23 #include <media/v4l2-flash-led-class.h>
25 #define AS_TIMER_US_TO_CODE(t) (((t) / 1000 - 100) / 50)
26 #define AS_TIMER_CODE_TO_US(c) ((50 * (c) + 100) * 1000)
28 /* Register definitions */
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)
35 /* Read-only Version control register: Reset state: 0000 0000
36 * for first engineering samples
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)
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
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
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
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)
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)
79 /* Password register is used to unlock boost register writing */
80 #define AS_PASSWORD_REG 0x0f
81 #define AS_PASSWORD_UNLOCK_VALUE 0x55
83 #define AS_NAME "as3645a"
84 #define AS_I2C_ADDR (0x60 >> 1) /* W:0x60, R:0x61 */
86 #define AS_FLASH_TIMEOUT_MIN 100000 /* us */
87 #define AS_FLASH_TIMEOUT_MAX 850000
88 #define AS_FLASH_TIMEOUT_STEP 50000
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
95 #define AS_TORCH_INTENSITY_MIN 20000 /* uA */
96 #define AS_TORCH_INTENSITY_MAX 160000
97 #define AS_TORCH_INTENSITY_STEP 20000
99 #define AS_INDICATOR_INTENSITY_MIN 0 /* uA */
100 #define AS_INDICATOR_INTENSITY_MAX 10000
101 #define AS_INDICATOR_INTENSITY_STEP 2500
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)
107 /* LED numbers for Devicetree */
108 #define AS_LED_FLASH 0
109 #define AS_LED_INDICATOR 1
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,
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;
127 struct as3645a {
128 struct i2c_client *client;
130 struct mutex mutex;
132 struct led_classdev_flash fled;
133 struct led_classdev iled_cdev;
135 struct v4l2_flash *vf;
136 struct v4l2_flash *vfind;
138 struct fwnode_handle *flash_node;
139 struct fwnode_handle *indicator_node;
141 struct as3645a_config cfg;
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;
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)
155 /* Return negative errno else zero on success */
156 static int as3645a_write(struct as3645a *flash, u8 addr, u8 val)
158 struct i2c_client *client = flash->client;
159 int rval;
161 rval = i2c_smbus_write_byte_data(client, addr, val);
163 dev_dbg(&client->dev, "Write Addr:%02X Val:%02X %s\n", addr, val,
164 rval < 0 ? "fail" : "ok");
166 return rval;
169 /* Return negative errno else a data byte received from the device. */
170 static int as3645a_read(struct as3645a *flash, u8 addr)
172 struct i2c_client *client = flash->client;
173 int rval;
175 rval = i2c_smbus_read_byte_data(client, addr);
177 dev_dbg(&client->dev, "Read Addr:%02X Val:%02X %s\n", addr, rval,
178 rval < 0 ? "fail" : "ok");
180 return rval;
183 /* -----------------------------------------------------------------------------
184 * Hardware configuration and trigger
188 * as3645a_set_config - Set flash configuration registers
189 * @flash: The flash
191 * Configure the hardware with flash, assist and indicator currents, as well as
192 * flash timeout.
194 * Return 0 on success, or a negative error code if an I2C communication error
195 * occurred.
197 static int as3645a_set_current(struct as3645a *flash)
199 u8 val;
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;
205 return as3645a_write(flash, AS_CURRENT_SET_REG, val);
208 static int as3645a_set_timeout(struct as3645a *flash)
210 u8 val;
212 val = flash->timeout << AS_INDICATOR_AND_TIMER_TIMEOUT_SHIFT;
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);
219 return as3645a_write(flash, AS_INDICATOR_AND_TIMER_REG, val);
223 * as3645a_set_control - Set flash control register
224 * @flash: The flash
225 * @mode: Desired output mode
226 * @on: Desired output state
228 * Configure the hardware with output mode and state.
230 * Return 0 on success, or a negative error code if an I2C communication error
231 * occurred.
233 static int
234 as3645a_set_control(struct as3645a *flash, enum as_mode mode, bool on)
236 u8 reg;
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;
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;
248 return as3645a_write(flash, AS_CONTROL_REG, reg);
251 static int as3645a_get_fault(struct led_classdev_flash *fled, u32 *fault)
253 struct as3645a *flash = fled_to_as3645a(fled);
254 int rval;
256 /* NOTE: reading register clears fault status */
257 rval = as3645a_read(flash, AS_FAULT_INFO_REG);
258 if (rval < 0)
259 return rval;
261 if (rval & AS_FAULT_INFO_INDUCTOR_PEAK_LIMIT)
262 *fault |= LED_FAULT_OVER_CURRENT;
264 if (rval & AS_FAULT_INFO_INDICATOR_LED)
265 *fault |= LED_FAULT_INDICATOR;
267 dev_dbg(&flash->client->dev, "%u connected LEDs\n",
268 rval & AS_FAULT_INFO_LED_AMOUNT ? 2 : 1);
270 if (rval & AS_FAULT_INFO_TIMEOUT)
271 *fault |= LED_FAULT_TIMEOUT;
273 if (rval & AS_FAULT_INFO_OVER_TEMPERATURE)
274 *fault |= LED_FAULT_OVER_TEMPERATURE;
276 if (rval & AS_FAULT_INFO_SHORT_CIRCUIT)
277 *fault |= LED_FAULT_OVER_CURRENT;
279 if (rval & AS_FAULT_INFO_OVER_VOLTAGE)
280 *fault |= LED_FAULT_INPUT_VOLTAGE;
282 return rval;
285 static unsigned int __as3645a_current_to_reg(unsigned int min, unsigned int max,
286 unsigned int step,
287 unsigned int val)
289 if (val < min)
290 val = min;
292 if (val > max)
293 val = max;
295 return (val - min) / step;
298 static unsigned int as3645a_current_to_reg(struct as3645a *flash, bool is_flash,
299 unsigned int ua)
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);
311 static int as3645a_set_indicator_brightness(struct led_classdev *iled_cdev,
312 enum led_brightness brightness)
314 struct as3645a *flash = iled_cdev_to_as3645a(iled_cdev);
315 int rval;
317 flash->indicator_current = brightness;
319 rval = as3645a_set_timeout(flash);
320 if (rval)
321 return rval;
323 return as3645a_set_control(flash, AS_MODE_INDICATOR, brightness);
326 static int as3645a_set_assist_brightness(struct led_classdev *fled_cdev,
327 enum led_brightness brightness)
329 struct led_classdev_flash *fled = lcdev_to_flcdev(fled_cdev);
330 struct as3645a *flash = fled_to_as3645a(fled);
331 int rval;
333 if (brightness) {
334 /* Register value 0 is 20 mA. */
335 flash->assist_current = brightness - 1;
337 rval = as3645a_set_current(flash);
338 if (rval)
339 return rval;
342 return as3645a_set_control(flash, AS_MODE_ASSIST, brightness);
345 static int as3645a_set_flash_brightness(struct led_classdev_flash *fled,
346 u32 brightness_ua)
348 struct as3645a *flash = fled_to_as3645a(fled);
350 flash->flash_current = as3645a_current_to_reg(flash, true,
351 brightness_ua);
353 return as3645a_set_current(flash);
356 static int as3645a_set_flash_timeout(struct led_classdev_flash *fled,
357 u32 timeout_us)
359 struct as3645a *flash = fled_to_as3645a(fled);
361 flash->timeout = AS_TIMER_US_TO_CODE(timeout_us);
363 return as3645a_set_timeout(flash);
366 static int as3645a_set_strobe(struct led_classdev_flash *fled, bool state)
368 struct as3645a *flash = fled_to_as3645a(fled);
370 return as3645a_set_control(flash, AS_MODE_FLASH, state);
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,
380 static int as3645a_setup(struct as3645a *flash)
382 struct device *dev = &flash->client->dev;
383 u32 fault = 0;
384 int rval;
386 /* clear errors */
387 rval = as3645a_read(flash, AS_FAULT_INFO_REG);
388 if (rval < 0)
389 return rval;
391 dev_dbg(dev, "Fault info: %02x\n", rval);
393 rval = as3645a_set_current(flash);
394 if (rval < 0)
395 return rval;
397 rval = as3645a_set_timeout(flash);
398 if (rval < 0)
399 return rval;
401 rval = as3645a_set_control(flash, AS_MODE_INDICATOR, false);
402 if (rval < 0)
403 return rval;
405 /* read status */
406 rval = as3645a_get_fault(&flash->fled, &fault);
407 if (rval < 0)
408 return rval;
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));
417 return rval & ~AS_FAULT_INFO_LED_AMOUNT ? -EIO : 0;
420 static int as3645a_detect(struct as3645a *flash)
422 struct device *dev = &flash->client->dev;
423 int rval, man, model, rfu, version;
424 const char *vendor;
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;
432 man = AS_DESIGN_INFO_FACTORY(rval);
433 model = AS_DESIGN_INFO_MODEL(rval);
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;
441 rfu = AS_VERSION_CONTROL_RFU(rval);
442 version = AS_VERSION_CONTROL_VERSION(rval);
444 /* Verify the chip model and version. */
445 if (model != 0x01 || rfu != 0x00) {
446 dev_err(dev, "AS3645A not detected (model %d rfu %d)\n",
447 model, rfu);
448 return -ENODEV;
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";
471 dev_info(dev, "Chip vendor: %s (%d) Version: %d\n", vendor,
472 man, version);
474 rval = as3645a_write(flash, AS_PASSWORD_REG, AS_PASSWORD_UNLOCK_VALUE);
475 if (rval < 0)
476 return rval;
478 return as3645a_write(flash, AS_BOOST_REG, AS_BOOST_CURRENT_DISABLE);
481 static int as3645a_parse_node(struct as3645a *flash,
482 struct fwnode_handle *fwnode)
484 struct as3645a_config *cfg = &flash->cfg;
485 struct fwnode_handle *child;
486 int rval;
488 fwnode_for_each_child_node(fwnode, child) {
489 u32 id = 0;
491 fwnode_property_read_u32(child, "reg", &id);
493 switch (id) {
494 case AS_LED_FLASH:
495 flash->flash_node = child;
496 fwnode_handle_get(child);
497 break;
498 case AS_LED_INDICATOR:
499 flash->indicator_node = child;
500 fwnode_handle_get(child);
501 break;
502 default:
503 dev_warn(&flash->client->dev,
504 "unknown LED %u encountered, ignoring\n", id);
505 break;
509 if (!flash->flash_node) {
510 dev_err(&flash->client->dev, "can't find flash node\n");
511 return -ENODEV;
514 rval = fwnode_property_read_u32(flash->flash_node, "flash-timeout-us",
515 &cfg->flash_timeout_us);
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;
522 rval = fwnode_property_read_u32(flash->flash_node, "flash-max-microamp",
523 &cfg->flash_max_ua);
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;
530 rval = fwnode_property_read_u32(flash->flash_node, "led-max-microamp",
531 &cfg->assist_max_ua);
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;
538 fwnode_property_read_u32(flash->flash_node, "voltage-reference",
539 &cfg->voltage_reference);
541 fwnode_property_read_u32(flash->flash_node, "ams,input-max-microamp",
542 &cfg->peak);
543 cfg->peak = AS_PEAK_mA_TO_REG(cfg->peak);
545 if (!flash->indicator_node) {
546 dev_warn(&flash->client->dev,
547 "can't find indicator node\n");
548 goto out_err;
552 rval = fwnode_property_read_u32(flash->indicator_node,
553 "led-max-microamp",
554 &cfg->indicator_max_ua);
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;
561 return 0;
563 out_err:
564 fwnode_handle_put(flash->flash_node);
565 fwnode_handle_put(flash->indicator_node);
567 return rval;
570 static int as3645a_led_class_setup(struct as3645a *flash)
572 struct led_classdev *fled_cdev = &flash->fled.led_cdev;
573 struct led_classdev *iled_cdev = &flash->iled_cdev;
574 struct led_init_data init_data = {};
575 struct led_flash_setting *cfg;
576 int rval;
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;
583 init_data.fwnode = flash->indicator_node;
584 init_data.devicename = AS_NAME;
585 init_data.default_label = "indicator";
587 rval = led_classdev_register_ext(&flash->client->dev, iled_cdev,
588 &init_data);
589 if (rval < 0)
590 return rval;
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;
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;
604 flash->fled.ops = &as3645a_led_flash_ops;
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;
613 init_data.fwnode = flash->flash_node;
614 init_data.devicename = AS_NAME;
615 init_data.default_label = "flash";
617 rval = led_classdev_flash_register_ext(&flash->client->dev,
618 &flash->fled, &init_data);
619 if (rval)
620 goto out_err;
622 return rval;
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;
632 static int as3645a_v4l2_setup(struct as3645a *flash)
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,
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,
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));
657 flash->vf = v4l2_flash_init(
658 &flash->client->dev, flash->flash_node, &flash->fled, NULL,
659 &cfg);
660 if (IS_ERR(flash->vf))
661 return PTR_ERR(flash->vf);
663 flash->vfind = v4l2_flash_indicator_init(
664 &flash->client->dev, flash->indicator_node, &flash->iled_cdev,
665 &cfgind);
666 if (IS_ERR(flash->vfind)) {
667 v4l2_flash_release(flash->vf);
668 return PTR_ERR(flash->vfind);
671 return 0;
674 static int as3645a_probe(struct i2c_client *client)
676 struct as3645a *flash;
677 int rval;
679 if (!dev_fwnode(&client->dev))
680 return -ENODEV;
682 flash = devm_kzalloc(&client->dev, sizeof(*flash), GFP_KERNEL);
683 if (flash == NULL)
684 return -ENOMEM;
686 flash->client = client;
688 rval = as3645a_parse_node(flash, dev_fwnode(&client->dev));
689 if (rval < 0)
690 return rval;
692 rval = as3645a_detect(flash);
693 if (rval < 0)
694 goto out_put_nodes;
696 mutex_init(&flash->mutex);
697 i2c_set_clientdata(client, flash);
699 rval = as3645a_setup(flash);
700 if (rval)
701 goto out_mutex_destroy;
703 rval = as3645a_led_class_setup(flash);
704 if (rval)
705 goto out_mutex_destroy;
707 rval = as3645a_v4l2_setup(flash);
708 if (rval)
709 goto out_led_classdev_flash_unregister;
711 return 0;
713 out_led_classdev_flash_unregister:
714 led_classdev_flash_unregister(&flash->fled);
716 out_mutex_destroy:
717 mutex_destroy(&flash->mutex);
719 out_put_nodes:
720 fwnode_handle_put(flash->flash_node);
721 fwnode_handle_put(flash->indicator_node);
723 return rval;
726 static int as3645a_remove(struct i2c_client *client)
728 struct as3645a *flash = i2c_get_clientdata(client);
730 as3645a_set_control(flash, AS_MODE_EXT_TORCH, false);
732 v4l2_flash_release(flash->vf);
733 v4l2_flash_release(flash->vfind);
735 led_classdev_flash_unregister(&flash->fled);
736 led_classdev_unregister(&flash->iled_cdev);
738 mutex_destroy(&flash->mutex);
740 fwnode_handle_put(flash->flash_node);
741 fwnode_handle_put(flash->indicator_node);
743 return 0;
746 static const struct i2c_device_id as3645a_id_table[] = {
747 { AS_NAME, 0 },
748 { },
750 MODULE_DEVICE_TABLE(i2c, as3645a_id_table);
752 static const struct of_device_id as3645a_of_table[] = {
753 { .compatible = "ams,as3645a" },
754 { },
756 MODULE_DEVICE_TABLE(of, as3645a_of_table);
758 static struct i2c_driver as3645a_i2c_driver = {
759 .driver = {
760 .of_match_table = as3645a_of_table,
761 .name = AS_NAME,
763 .probe_new = as3645a_probe,
764 .remove = as3645a_remove,
765 .id_table = as3645a_id_table,
768 module_i2c_driver(as3645a_i2c_driver);
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");