2 * LED Flash class driver for the flash cell of max77693 mfd.
4 * Copyright (C) 2015, Samsung Electronics Co., Ltd.
6 * Authors: Jacek Anaszewski <j.anaszewski@samsung.com>
7 * Andrzej Hajda <a.hajda@samsung.com>
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * version 2 as published by the Free Software Foundation.
14 #include <linux/led-class-flash.h>
15 #include <linux/mfd/max77693.h>
16 #include <linux/mfd/max77693-common.h>
17 #include <linux/mfd/max77693-private.h>
18 #include <linux/module.h>
19 #include <linux/mutex.h>
20 #include <linux/platform_device.h>
21 #include <linux/regmap.h>
22 #include <linux/slab.h>
23 #include <linux/workqueue.h>
24 #include <media/v4l2-flash-led-class.h>
27 #define MODE_FLASH(a) (1 << (a))
28 #define MODE_TORCH(a) (1 << (2 + (a)))
29 #define MODE_FLASH_EXTERNAL(a) (1 << (4 + (a)))
31 #define MODE_FLASH_MASK (MODE_FLASH(FLED1) | MODE_FLASH(FLED2) | \
32 MODE_FLASH_EXTERNAL(FLED1) | \
33 MODE_FLASH_EXTERNAL(FLED2))
34 #define MODE_TORCH_MASK (MODE_TORCH(FLED1) | MODE_TORCH(FLED2))
36 #define FLED1_IOUT (1 << 0)
37 #define FLED2_IOUT (1 << 1)
44 enum max77693_led_mode {
49 struct max77693_led_config_data {
51 u32 iout_torch_max[2];
52 u32 iout_flash_max[2];
53 u32 flash_timeout_max[2];
60 struct max77693_sub_led {
61 /* corresponding FLED output identifier */
63 /* corresponding LED Flash class device */
64 struct led_classdev_flash fled_cdev;
65 /* assures led-triggers compatibility */
66 struct work_struct work_brightness_set;
67 /* V4L2 Flash device */
68 struct v4l2_flash *v4l2_flash;
70 /* brightness cache */
71 unsigned int torch_brightness;
72 /* flash timeout cache */
73 unsigned int flash_timeout;
74 /* flash faults that may have occurred */
78 struct max77693_led_device {
79 /* parent mfd regmap */
80 struct regmap *regmap;
81 /* platform device data */
82 struct platform_device *pdev;
83 /* secures access to the device */
87 struct max77693_sub_led sub_leds[2];
89 /* maximum torch current values for FLED outputs */
90 u32 iout_torch_max[2];
91 /* maximum flash current values for FLED outputs */
92 u32 iout_flash_max[2];
94 /* current flash timeout cache */
95 unsigned int current_flash_timeout;
96 /* ITORCH register cache */
98 /* mode of fled outputs */
99 unsigned int mode_flags;
100 /* recently strobed fled */
101 int strobing_sub_led_id;
102 /* bitmask of FLED outputs use state (bit 0. - FLED1, bit 1. - FLED2) */
104 /* FLED modes that can be set */
107 /* arrangement of current outputs */
111 static u8 max77693_led_iout_to_reg(u32 ua)
113 if (ua < FLASH_IOUT_MIN)
115 return (ua - FLASH_IOUT_MIN) / FLASH_IOUT_STEP;
118 static u8 max77693_flash_timeout_to_reg(u32 us)
120 return (us - FLASH_TIMEOUT_MIN) / FLASH_TIMEOUT_STEP;
123 static inline struct max77693_sub_led *flcdev_to_sub_led(
124 struct led_classdev_flash *fled_cdev)
126 return container_of(fled_cdev, struct max77693_sub_led, fled_cdev);
129 static inline struct max77693_led_device *sub_led_to_led(
130 struct max77693_sub_led *sub_led)
132 return container_of(sub_led, struct max77693_led_device,
133 sub_leds[sub_led->fled_id]);
136 static inline u8 max77693_led_vsys_to_reg(u32 mv)
138 return ((mv - MAX_FLASH1_VSYS_MIN) / MAX_FLASH1_VSYS_STEP) << 2;
141 static inline u8 max77693_led_vout_to_reg(u32 mv)
143 return (mv - FLASH_VOUT_MIN) / FLASH_VOUT_STEP + FLASH_VOUT_RMIN;
146 static inline bool max77693_fled_used(struct max77693_led_device *led,
149 u8 fled_bit = (fled_id == FLED1) ? FLED1_IOUT : FLED2_IOUT;
151 return led->fled_mask & fled_bit;
154 static int max77693_set_mode_reg(struct max77693_led_device *led, u8 mode)
156 struct regmap *rmap = led->regmap;
159 for (i = FLED1; i <= FLED2; ++i) {
160 if (mode & MODE_TORCH(i))
161 v |= FLASH_EN_ON << TORCH_EN_SHIFT(i);
163 if (mode & MODE_FLASH(i)) {
164 v |= FLASH_EN_ON << FLASH_EN_SHIFT(i);
165 } else if (mode & MODE_FLASH_EXTERNAL(i)) {
166 v |= FLASH_EN_FLASH << FLASH_EN_SHIFT(i);
168 * Enable hw triggering also for torch mode, as some
169 * camera sensors use torch led to fathom ambient light
170 * conditions before strobing the flash.
172 v |= FLASH_EN_TORCH << TORCH_EN_SHIFT(i);
176 /* Reset the register only prior setting flash modes */
177 if (mode & ~(MODE_TORCH(FLED1) | MODE_TORCH(FLED2))) {
178 ret = regmap_write(rmap, MAX77693_LED_REG_FLASH_EN, 0);
183 return regmap_write(rmap, MAX77693_LED_REG_FLASH_EN, v);
186 static int max77693_add_mode(struct max77693_led_device *led, u8 mode)
192 /* Span the mode on FLED2 for joint iouts case */
196 * FLASH_EXTERNAL mode activates FLASHEN and TORCHEN pins in the device.
197 * Corresponding register bit fields interfere with SW triggered modes,
198 * thus clear them to ensure proper device configuration.
200 for (i = FLED1; i <= FLED2; ++i)
201 if (mode & MODE_FLASH_EXTERNAL(i))
202 led->mode_flags &= (~MODE_TORCH(i) & ~MODE_FLASH(i));
204 new_mode_flags = mode | led->mode_flags;
205 new_mode_flags &= led->allowed_modes;
207 if (new_mode_flags ^ led->mode_flags)
208 led->mode_flags = new_mode_flags;
212 ret = max77693_set_mode_reg(led, led->mode_flags);
217 * Clear flash mode flag after setting the mode to avoid spurious flash
218 * strobing on each subsequent torch mode setting.
220 if (mode & MODE_FLASH_MASK)
221 led->mode_flags &= ~mode;
226 static int max77693_clear_mode(struct max77693_led_device *led,
230 /* Clear mode also on FLED2 for joint iouts case */
233 led->mode_flags &= ~mode;
235 return max77693_set_mode_reg(led, led->mode_flags);
238 static void max77693_add_allowed_modes(struct max77693_led_device *led,
239 int fled_id, enum max77693_led_mode mode)
242 led->allowed_modes |= (MODE_FLASH(fled_id) |
243 MODE_FLASH_EXTERNAL(fled_id));
245 led->allowed_modes |= MODE_TORCH(fled_id);
248 static void max77693_distribute_currents(struct max77693_led_device *led,
249 int fled_id, enum max77693_led_mode mode,
250 u32 micro_amp, u32 iout_max[2], u32 iout[2])
252 if (!led->iout_joint) {
253 iout[fled_id] = micro_amp;
254 max77693_add_allowed_modes(led, fled_id, mode);
258 iout[FLED1] = min(micro_amp, iout_max[FLED1]);
259 iout[FLED2] = micro_amp - iout[FLED1];
262 led->allowed_modes &= ~MODE_FLASH_MASK;
264 led->allowed_modes &= ~MODE_TORCH_MASK;
266 max77693_add_allowed_modes(led, FLED1, mode);
269 max77693_add_allowed_modes(led, FLED2, mode);
272 static int max77693_set_torch_current(struct max77693_led_device *led,
273 int fled_id, u32 micro_amp)
275 struct regmap *rmap = led->regmap;
276 u8 iout1_reg = 0, iout2_reg = 0;
279 max77693_distribute_currents(led, fled_id, TORCH, micro_amp,
280 led->iout_torch_max, iout);
282 if (fled_id == FLED1 || led->iout_joint) {
283 iout1_reg = max77693_led_iout_to_reg(iout[FLED1]);
284 led->torch_iout_reg &= TORCH_IOUT_MASK(TORCH_IOUT2_SHIFT);
286 if (fled_id == FLED2 || led->iout_joint) {
287 iout2_reg = max77693_led_iout_to_reg(iout[FLED2]);
288 led->torch_iout_reg &= TORCH_IOUT_MASK(TORCH_IOUT1_SHIFT);
291 led->torch_iout_reg |= ((iout1_reg << TORCH_IOUT1_SHIFT) |
292 (iout2_reg << TORCH_IOUT2_SHIFT));
294 return regmap_write(rmap, MAX77693_LED_REG_ITORCH,
295 led->torch_iout_reg);
298 static int max77693_set_flash_current(struct max77693_led_device *led,
302 struct regmap *rmap = led->regmap;
303 u8 iout1_reg, iout2_reg;
307 max77693_distribute_currents(led, fled_id, FLASH, micro_amp,
308 led->iout_flash_max, iout);
310 if (fled_id == FLED1 || led->iout_joint) {
311 iout1_reg = max77693_led_iout_to_reg(iout[FLED1]);
312 ret = regmap_write(rmap, MAX77693_LED_REG_IFLASH1,
317 if (fled_id == FLED2 || led->iout_joint) {
318 iout2_reg = max77693_led_iout_to_reg(iout[FLED2]);
319 ret = regmap_write(rmap, MAX77693_LED_REG_IFLASH2,
326 static int max77693_set_timeout(struct max77693_led_device *led, u32 microsec)
328 struct regmap *rmap = led->regmap;
332 v = max77693_flash_timeout_to_reg(microsec) | FLASH_TMR_LEVEL;
334 ret = regmap_write(rmap, MAX77693_LED_REG_FLASH_TIMER, v);
338 led->current_flash_timeout = microsec;
343 static int max77693_get_strobe_status(struct max77693_led_device *led,
346 struct regmap *rmap = led->regmap;
350 ret = regmap_read(rmap, MAX77693_LED_REG_FLASH_STATUS, &v);
354 *state = v & FLASH_STATUS_FLASH_ON;
359 static int max77693_get_flash_faults(struct max77693_sub_led *sub_led)
361 struct max77693_led_device *led = sub_led_to_led(sub_led);
362 struct regmap *rmap = led->regmap;
364 u8 fault_open_mask, fault_short_mask;
367 sub_led->flash_faults = 0;
369 if (led->iout_joint) {
370 fault_open_mask = FLASH_INT_FLED1_OPEN | FLASH_INT_FLED2_OPEN;
371 fault_short_mask = FLASH_INT_FLED1_SHORT |
372 FLASH_INT_FLED2_SHORT;
374 fault_open_mask = (sub_led->fled_id == FLED1) ?
375 FLASH_INT_FLED1_OPEN :
376 FLASH_INT_FLED2_OPEN;
377 fault_short_mask = (sub_led->fled_id == FLED1) ?
378 FLASH_INT_FLED1_SHORT :
379 FLASH_INT_FLED2_SHORT;
382 ret = regmap_read(rmap, MAX77693_LED_REG_FLASH_INT, &v);
386 if (v & fault_open_mask)
387 sub_led->flash_faults |= LED_FAULT_OVER_VOLTAGE;
388 if (v & fault_short_mask)
389 sub_led->flash_faults |= LED_FAULT_SHORT_CIRCUIT;
390 if (v & FLASH_INT_OVER_CURRENT)
391 sub_led->flash_faults |= LED_FAULT_OVER_CURRENT;
396 static int max77693_setup(struct max77693_led_device *led,
397 struct max77693_led_config_data *led_cfg)
399 struct regmap *rmap = led->regmap;
400 int i, first_led, last_led, ret;
401 u32 max_flash_curr[2];
405 * Initialize only flash current. Torch current doesn't
406 * require initialization as ITORCH register is written with
407 * new value each time brightness_set op is called.
409 if (led->iout_joint) {
412 max_flash_curr[FLED1] = led_cfg->iout_flash_max[FLED1] +
413 led_cfg->iout_flash_max[FLED2];
415 first_led = max77693_fled_used(led, FLED1) ? FLED1 : FLED2;
416 last_led = max77693_fled_used(led, FLED2) ? FLED2 : FLED1;
417 max_flash_curr[FLED1] = led_cfg->iout_flash_max[FLED1];
418 max_flash_curr[FLED2] = led_cfg->iout_flash_max[FLED2];
421 for (i = first_led; i <= last_led; ++i) {
422 ret = max77693_set_flash_current(led, i,
428 v = TORCH_TMR_NO_TIMER | MAX77693_LED_TRIG_TYPE_LEVEL;
429 ret = regmap_write(rmap, MAX77693_LED_REG_ITORCHTIMER, v);
433 if (led_cfg->low_vsys > 0)
434 v = max77693_led_vsys_to_reg(led_cfg->low_vsys) |
435 MAX_FLASH1_MAX_FL_EN;
439 ret = regmap_write(rmap, MAX77693_LED_REG_MAX_FLASH1, v);
442 ret = regmap_write(rmap, MAX77693_LED_REG_MAX_FLASH2, 0);
446 if (led_cfg->boost_mode == MAX77693_LED_BOOST_FIXED)
447 v = FLASH_BOOST_FIXED;
449 v = led_cfg->boost_mode | led_cfg->boost_mode << 1;
451 if (max77693_fled_used(led, FLED1) && max77693_fled_used(led, FLED2))
452 v |= FLASH_BOOST_LEDNUM_2;
454 ret = regmap_write(rmap, MAX77693_LED_REG_VOUT_CNTL, v);
458 v = max77693_led_vout_to_reg(led_cfg->boost_vout);
459 ret = regmap_write(rmap, MAX77693_LED_REG_VOUT_FLASH1, v);
463 return max77693_set_mode_reg(led, MODE_OFF);
466 static int __max77693_led_brightness_set(struct max77693_led_device *led,
467 int fled_id, enum led_brightness value)
471 mutex_lock(&led->lock);
474 ret = max77693_clear_mode(led, MODE_TORCH(fled_id));
476 dev_dbg(&led->pdev->dev,
477 "Failed to clear torch mode (%d)\n",
482 ret = max77693_set_torch_current(led, fled_id, value * TORCH_IOUT_STEP);
484 dev_dbg(&led->pdev->dev,
485 "Failed to set torch current (%d)\n",
490 ret = max77693_add_mode(led, MODE_TORCH(fled_id));
492 dev_dbg(&led->pdev->dev,
493 "Failed to set torch mode (%d)\n",
496 mutex_unlock(&led->lock);
500 static void max77693_led_brightness_set_work(
501 struct work_struct *work)
503 struct max77693_sub_led *sub_led =
504 container_of(work, struct max77693_sub_led,
505 work_brightness_set);
506 struct max77693_led_device *led = sub_led_to_led(sub_led);
508 __max77693_led_brightness_set(led, sub_led->fled_id,
509 sub_led->torch_brightness);
512 /* LED subsystem callbacks */
514 static int max77693_led_brightness_set_sync(
515 struct led_classdev *led_cdev,
516 enum led_brightness value)
518 struct led_classdev_flash *fled_cdev = lcdev_to_flcdev(led_cdev);
519 struct max77693_sub_led *sub_led = flcdev_to_sub_led(fled_cdev);
520 struct max77693_led_device *led = sub_led_to_led(sub_led);
522 return __max77693_led_brightness_set(led, sub_led->fled_id, value);
525 static void max77693_led_brightness_set(
526 struct led_classdev *led_cdev,
527 enum led_brightness value)
529 struct led_classdev_flash *fled_cdev = lcdev_to_flcdev(led_cdev);
530 struct max77693_sub_led *sub_led = flcdev_to_sub_led(fled_cdev);
532 sub_led->torch_brightness = value;
533 schedule_work(&sub_led->work_brightness_set);
536 static int max77693_led_flash_brightness_set(
537 struct led_classdev_flash *fled_cdev,
540 struct max77693_sub_led *sub_led = flcdev_to_sub_led(fled_cdev);
541 struct max77693_led_device *led = sub_led_to_led(sub_led);
544 mutex_lock(&led->lock);
545 ret = max77693_set_flash_current(led, sub_led->fled_id, brightness);
546 mutex_unlock(&led->lock);
551 static int max77693_led_flash_strobe_set(
552 struct led_classdev_flash *fled_cdev,
555 struct max77693_sub_led *sub_led = flcdev_to_sub_led(fled_cdev);
556 struct max77693_led_device *led = sub_led_to_led(sub_led);
557 int fled_id = sub_led->fled_id;
560 mutex_lock(&led->lock);
563 ret = max77693_clear_mode(led, MODE_FLASH(fled_id));
567 if (sub_led->flash_timeout != led->current_flash_timeout) {
568 ret = max77693_set_timeout(led, sub_led->flash_timeout);
573 led->strobing_sub_led_id = fled_id;
575 ret = max77693_add_mode(led, MODE_FLASH(fled_id));
579 ret = max77693_get_flash_faults(sub_led);
582 mutex_unlock(&led->lock);
586 static int max77693_led_flash_fault_get(
587 struct led_classdev_flash *fled_cdev,
590 struct max77693_sub_led *sub_led = flcdev_to_sub_led(fled_cdev);
592 *fault = sub_led->flash_faults;
597 static int max77693_led_flash_strobe_get(
598 struct led_classdev_flash *fled_cdev,
601 struct max77693_sub_led *sub_led = flcdev_to_sub_led(fled_cdev);
602 struct max77693_led_device *led = sub_led_to_led(sub_led);
608 mutex_lock(&led->lock);
610 ret = max77693_get_strobe_status(led, state);
612 *state = !!(*state && (led->strobing_sub_led_id == sub_led->fled_id));
614 mutex_unlock(&led->lock);
619 static int max77693_led_flash_timeout_set(
620 struct led_classdev_flash *fled_cdev,
623 struct max77693_sub_led *sub_led = flcdev_to_sub_led(fled_cdev);
624 struct max77693_led_device *led = sub_led_to_led(sub_led);
626 mutex_lock(&led->lock);
627 sub_led->flash_timeout = timeout;
628 mutex_unlock(&led->lock);
633 static int max77693_led_parse_dt(struct max77693_led_device *led,
634 struct max77693_led_config_data *cfg,
635 struct device_node **sub_nodes)
637 struct device *dev = &led->pdev->dev;
638 struct max77693_sub_led *sub_leds = led->sub_leds;
639 struct device_node *node = dev->of_node, *child_node;
640 struct property *prop;
644 of_property_read_u32(node, "maxim,boost-mode", &cfg->boost_mode);
645 of_property_read_u32(node, "maxim,boost-mvout", &cfg->boost_vout);
646 of_property_read_u32(node, "maxim,mvsys-min", &cfg->low_vsys);
648 for_each_available_child_of_node(node, child_node) {
649 prop = of_find_property(child_node, "led-sources", NULL);
651 const __be32 *srcs = NULL;
653 for (i = 0; i < ARRAY_SIZE(led_sources); ++i) {
654 srcs = of_prop_next_u32(prop, srcs,
661 "led-sources DT property missing\n");
662 of_node_put(child_node);
668 led->fled_mask = FLED1_IOUT | FLED2_IOUT;
669 } else if (led_sources[0] == FLED1) {
671 led->fled_mask |= FLED1_IOUT;
672 } else if (led_sources[0] == FLED2) {
674 led->fled_mask |= FLED2_IOUT;
677 "Wrong led-sources DT property value.\n");
678 of_node_put(child_node);
682 if (sub_nodes[fled_id]) {
684 "Conflicting \"led-sources\" DT properties\n");
688 sub_nodes[fled_id] = child_node;
689 sub_leds[fled_id].fled_id = fled_id;
691 cfg->label[fled_id] =
692 of_get_property(child_node, "label", NULL) ? :
695 ret = of_property_read_u32(child_node, "led-max-microamp",
696 &cfg->iout_torch_max[fled_id]);
698 cfg->iout_torch_max[fled_id] = TORCH_IOUT_MIN;
699 dev_warn(dev, "led-max-microamp DT property missing\n");
702 ret = of_property_read_u32(child_node, "flash-max-microamp",
703 &cfg->iout_flash_max[fled_id]);
705 cfg->iout_flash_max[fled_id] = FLASH_IOUT_MIN;
707 "flash-max-microamp DT property missing\n");
710 ret = of_property_read_u32(child_node, "flash-max-timeout-us",
711 &cfg->flash_timeout_max[fled_id]);
713 cfg->flash_timeout_max[fled_id] = FLASH_TIMEOUT_MIN;
715 "flash-max-timeout-us DT property missing\n");
718 if (++cfg->num_leds == 2 ||
719 (max77693_fled_used(led, FLED1) &&
720 max77693_fled_used(led, FLED2))) {
721 of_node_put(child_node);
726 if (cfg->num_leds == 0) {
727 dev_err(dev, "No DT child node found for connected LED(s).\n");
734 static void clamp_align(u32 *v, u32 min, u32 max, u32 step)
736 *v = clamp_val(*v, min, max);
738 *v = (*v - min) / step * step + min;
741 static void max77693_align_iout_current(struct max77693_led_device *led,
742 u32 *iout, u32 min, u32 max, u32 step)
746 if (led->iout_joint) {
747 if (iout[FLED1] > min) {
749 iout[FLED2] = iout[FLED1];
757 for (i = FLED1; i <= FLED2; ++i)
758 if (max77693_fled_used(led, i))
759 clamp_align(&iout[i], min, max, step);
764 static void max77693_led_validate_configuration(struct max77693_led_device *led,
765 struct max77693_led_config_data *cfg)
767 u32 flash_iout_max = cfg->boost_mode ? FLASH_IOUT_MAX_2LEDS :
771 if (cfg->num_leds == 1 &&
772 max77693_fled_used(led, FLED1) && max77693_fled_used(led, FLED2))
773 led->iout_joint = true;
775 cfg->boost_mode = clamp_val(cfg->boost_mode, MAX77693_LED_BOOST_NONE,
776 MAX77693_LED_BOOST_FIXED);
778 /* Boost must be enabled if both current outputs are used */
779 if ((cfg->boost_mode == MAX77693_LED_BOOST_NONE) && led->iout_joint)
780 cfg->boost_mode = MAX77693_LED_BOOST_FIXED;
782 max77693_align_iout_current(led, cfg->iout_torch_max,
783 TORCH_IOUT_MIN, TORCH_IOUT_MAX, TORCH_IOUT_STEP);
785 max77693_align_iout_current(led, cfg->iout_flash_max,
786 FLASH_IOUT_MIN, flash_iout_max, FLASH_IOUT_STEP);
788 for (i = 0; i < ARRAY_SIZE(cfg->flash_timeout_max); ++i)
789 clamp_align(&cfg->flash_timeout_max[i], FLASH_TIMEOUT_MIN,
790 FLASH_TIMEOUT_MAX, FLASH_TIMEOUT_STEP);
792 clamp_align(&cfg->boost_vout, FLASH_VOUT_MIN, FLASH_VOUT_MAX,
796 clamp_align(&cfg->low_vsys, MAX_FLASH1_VSYS_MIN,
797 MAX_FLASH1_VSYS_MAX, MAX_FLASH1_VSYS_STEP);
800 static int max77693_led_get_configuration(struct max77693_led_device *led,
801 struct max77693_led_config_data *cfg,
802 struct device_node **sub_nodes)
806 ret = max77693_led_parse_dt(led, cfg, sub_nodes);
810 max77693_led_validate_configuration(led, cfg);
812 memcpy(led->iout_torch_max, cfg->iout_torch_max,
813 sizeof(led->iout_torch_max));
814 memcpy(led->iout_flash_max, cfg->iout_flash_max,
815 sizeof(led->iout_flash_max));
820 static const struct led_flash_ops flash_ops = {
821 .flash_brightness_set = max77693_led_flash_brightness_set,
822 .strobe_set = max77693_led_flash_strobe_set,
823 .strobe_get = max77693_led_flash_strobe_get,
824 .timeout_set = max77693_led_flash_timeout_set,
825 .fault_get = max77693_led_flash_fault_get,
828 static void max77693_init_flash_settings(struct max77693_sub_led *sub_led,
829 struct max77693_led_config_data *led_cfg)
831 struct led_classdev_flash *fled_cdev = &sub_led->fled_cdev;
832 struct max77693_led_device *led = sub_led_to_led(sub_led);
833 int fled_id = sub_led->fled_id;
834 struct led_flash_setting *setting;
836 /* Init flash intensity setting */
837 setting = &fled_cdev->brightness;
838 setting->min = FLASH_IOUT_MIN;
839 setting->max = led->iout_joint ?
840 led_cfg->iout_flash_max[FLED1] +
841 led_cfg->iout_flash_max[FLED2] :
842 led_cfg->iout_flash_max[fled_id];
843 setting->step = FLASH_IOUT_STEP;
844 setting->val = setting->max;
846 /* Init flash timeout setting */
847 setting = &fled_cdev->timeout;
848 setting->min = FLASH_TIMEOUT_MIN;
849 setting->max = led_cfg->flash_timeout_max[fled_id];
850 setting->step = FLASH_TIMEOUT_STEP;
851 setting->val = setting->max;
854 #if IS_ENABLED(CONFIG_V4L2_FLASH_LED_CLASS)
856 static int max77693_led_external_strobe_set(
857 struct v4l2_flash *v4l2_flash,
860 struct max77693_sub_led *sub_led =
861 flcdev_to_sub_led(v4l2_flash->fled_cdev);
862 struct max77693_led_device *led = sub_led_to_led(sub_led);
863 int fled_id = sub_led->fled_id;
866 mutex_lock(&led->lock);
869 ret = max77693_add_mode(led, MODE_FLASH_EXTERNAL(fled_id));
871 ret = max77693_clear_mode(led, MODE_FLASH_EXTERNAL(fled_id));
873 mutex_unlock(&led->lock);
878 static void max77693_init_v4l2_flash_config(struct max77693_sub_led *sub_led,
879 struct max77693_led_config_data *led_cfg,
880 struct v4l2_flash_config *v4l2_sd_cfg)
882 struct max77693_led_device *led = sub_led_to_led(sub_led);
883 struct device *dev = &led->pdev->dev;
884 struct max77693_dev *iodev = dev_get_drvdata(dev->parent);
885 struct i2c_client *i2c = iodev->i2c;
886 struct led_flash_setting *s;
888 snprintf(v4l2_sd_cfg->dev_name, sizeof(v4l2_sd_cfg->dev_name),
889 "%s %d-%04x", sub_led->fled_cdev.led_cdev.name,
890 i2c_adapter_id(i2c->adapter), i2c->addr);
892 s = &v4l2_sd_cfg->torch_intensity;
893 s->min = TORCH_IOUT_MIN;
894 s->max = sub_led->fled_cdev.led_cdev.max_brightness * TORCH_IOUT_STEP;
895 s->step = TORCH_IOUT_STEP;
898 /* Init flash faults config */
899 v4l2_sd_cfg->flash_faults = LED_FAULT_OVER_VOLTAGE |
900 LED_FAULT_SHORT_CIRCUIT |
901 LED_FAULT_OVER_CURRENT;
903 v4l2_sd_cfg->has_external_strobe = true;
906 static const struct v4l2_flash_ops v4l2_flash_ops = {
907 .external_strobe_set = max77693_led_external_strobe_set,
910 static inline void max77693_init_v4l2_flash_config(
911 struct max77693_sub_led *sub_led,
912 struct max77693_led_config_data *led_cfg,
913 struct v4l2_flash_config *v4l2_sd_cfg)
916 static const struct v4l2_flash_ops v4l2_flash_ops;
919 static void max77693_init_fled_cdev(struct max77693_sub_led *sub_led,
920 struct max77693_led_config_data *led_cfg)
922 struct max77693_led_device *led = sub_led_to_led(sub_led);
923 int fled_id = sub_led->fled_id;
924 struct led_classdev_flash *fled_cdev;
925 struct led_classdev *led_cdev;
927 /* Initialize LED Flash class device */
928 fled_cdev = &sub_led->fled_cdev;
929 fled_cdev->ops = &flash_ops;
930 led_cdev = &fled_cdev->led_cdev;
932 led_cdev->name = led_cfg->label[fled_id];
934 led_cdev->brightness_set = max77693_led_brightness_set;
935 led_cdev->brightness_set_sync = max77693_led_brightness_set_sync;
936 led_cdev->max_brightness = (led->iout_joint ?
937 led_cfg->iout_torch_max[FLED1] +
938 led_cfg->iout_torch_max[FLED2] :
939 led_cfg->iout_torch_max[fled_id]) /
941 led_cdev->flags |= LED_DEV_CAP_FLASH;
942 INIT_WORK(&sub_led->work_brightness_set,
943 max77693_led_brightness_set_work);
945 max77693_init_flash_settings(sub_led, led_cfg);
947 /* Init flash timeout cache */
948 sub_led->flash_timeout = fled_cdev->timeout.val;
951 static int max77693_register_led(struct max77693_sub_led *sub_led,
952 struct max77693_led_config_data *led_cfg,
953 struct device_node *sub_node)
955 struct max77693_led_device *led = sub_led_to_led(sub_led);
956 struct led_classdev_flash *fled_cdev = &sub_led->fled_cdev;
957 struct device *dev = &led->pdev->dev;
958 struct v4l2_flash_config v4l2_sd_cfg = {};
961 /* Register in the LED subsystem */
962 ret = led_classdev_flash_register(dev, fled_cdev);
966 max77693_init_v4l2_flash_config(sub_led, led_cfg, &v4l2_sd_cfg);
968 /* Register in the V4L2 subsystem. */
969 sub_led->v4l2_flash = v4l2_flash_init(dev, sub_node, fled_cdev, NULL,
970 &v4l2_flash_ops, &v4l2_sd_cfg);
971 if (IS_ERR(sub_led->v4l2_flash)) {
972 ret = PTR_ERR(sub_led->v4l2_flash);
973 goto err_v4l2_flash_init;
979 led_classdev_flash_unregister(fled_cdev);
983 static int max77693_led_probe(struct platform_device *pdev)
985 struct device *dev = &pdev->dev;
986 struct max77693_dev *iodev = dev_get_drvdata(dev->parent);
987 struct max77693_led_device *led;
988 struct max77693_sub_led *sub_leds;
989 struct device_node *sub_nodes[2] = {};
990 struct max77693_led_config_data led_cfg = {};
991 int init_fled_cdev[2], i, ret;
993 led = devm_kzalloc(dev, sizeof(*led), GFP_KERNEL);
998 led->regmap = iodev->regmap;
999 led->allowed_modes = MODE_FLASH_MASK;
1000 sub_leds = led->sub_leds;
1002 platform_set_drvdata(pdev, led);
1003 ret = max77693_led_get_configuration(led, &led_cfg, sub_nodes);
1007 ret = max77693_setup(led, &led_cfg);
1011 mutex_init(&led->lock);
1013 init_fled_cdev[FLED1] =
1014 led->iout_joint || max77693_fled_used(led, FLED1);
1015 init_fled_cdev[FLED2] =
1016 !led->iout_joint && max77693_fled_used(led, FLED2);
1018 for (i = FLED1; i <= FLED2; ++i) {
1019 if (!init_fled_cdev[i])
1022 /* Initialize LED Flash class device */
1023 max77693_init_fled_cdev(&sub_leds[i], &led_cfg);
1026 * Register LED Flash class device and corresponding
1027 * V4L2 Flash device.
1029 ret = max77693_register_led(&sub_leds[i], &led_cfg,
1033 * At this moment FLED1 might have been already
1034 * registered and it needs to be released.
1037 goto err_register_led2;
1039 goto err_register_led1;
1046 /* It is possible than only FLED2 was to be registered */
1047 if (!init_fled_cdev[FLED1])
1048 goto err_register_led1;
1049 v4l2_flash_release(sub_leds[FLED1].v4l2_flash);
1050 led_classdev_flash_unregister(&sub_leds[FLED1].fled_cdev);
1052 mutex_destroy(&led->lock);
1057 static int max77693_led_remove(struct platform_device *pdev)
1059 struct max77693_led_device *led = platform_get_drvdata(pdev);
1060 struct max77693_sub_led *sub_leds = led->sub_leds;
1062 if (led->iout_joint || max77693_fled_used(led, FLED1)) {
1063 v4l2_flash_release(sub_leds[FLED1].v4l2_flash);
1064 led_classdev_flash_unregister(&sub_leds[FLED1].fled_cdev);
1065 cancel_work_sync(&sub_leds[FLED1].work_brightness_set);
1068 if (!led->iout_joint && max77693_fled_used(led, FLED2)) {
1069 v4l2_flash_release(sub_leds[FLED2].v4l2_flash);
1070 led_classdev_flash_unregister(&sub_leds[FLED2].fled_cdev);
1071 cancel_work_sync(&sub_leds[FLED2].work_brightness_set);
1074 mutex_destroy(&led->lock);
1079 static const struct of_device_id max77693_led_dt_match[] = {
1080 { .compatible = "maxim,max77693-led" },
1083 MODULE_DEVICE_TABLE(of, max77693_led_dt_match);
1085 static struct platform_driver max77693_led_driver = {
1086 .probe = max77693_led_probe,
1087 .remove = max77693_led_remove,
1089 .name = "max77693-led",
1090 .of_match_table = max77693_led_dt_match,
1094 module_platform_driver(max77693_led_driver);
1096 MODULE_AUTHOR("Jacek Anaszewski <j.anaszewski@samsung.com>");
1097 MODULE_AUTHOR("Andrzej Hajda <a.hajda@samsung.com>");
1098 MODULE_DESCRIPTION("Maxim MAX77693 led flash driver");
1099 MODULE_LICENSE("GPL v2");