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 <media/v4l2-flash-led-class.h>
26 #define MODE_FLASH(a) (1 << (a))
27 #define MODE_TORCH(a) (1 << (2 + (a)))
28 #define MODE_FLASH_EXTERNAL(a) (1 << (4 + (a)))
30 #define MODE_FLASH_MASK (MODE_FLASH(FLED1) | MODE_FLASH(FLED2) | \
31 MODE_FLASH_EXTERNAL(FLED1) | \
32 MODE_FLASH_EXTERNAL(FLED2))
33 #define MODE_TORCH_MASK (MODE_TORCH(FLED1) | MODE_TORCH(FLED2))
35 #define FLED1_IOUT (1 << 0)
36 #define FLED2_IOUT (1 << 1)
43 enum max77693_led_mode {
48 struct max77693_led_config_data {
50 u32 iout_torch_max[2];
51 u32 iout_flash_max[2];
52 u32 flash_timeout_max[2];
59 struct max77693_sub_led {
60 /* corresponding FLED output identifier */
62 /* corresponding LED Flash class device */
63 struct led_classdev_flash fled_cdev;
64 /* V4L2 Flash device */
65 struct v4l2_flash *v4l2_flash;
67 /* brightness cache */
68 unsigned int torch_brightness;
69 /* flash timeout cache */
70 unsigned int flash_timeout;
71 /* flash faults that may have occurred */
75 struct max77693_led_device {
76 /* parent mfd regmap */
77 struct regmap *regmap;
78 /* platform device data */
79 struct platform_device *pdev;
80 /* secures access to the device */
84 struct max77693_sub_led sub_leds[2];
86 /* maximum torch current values for FLED outputs */
87 u32 iout_torch_max[2];
88 /* maximum flash current values for FLED outputs */
89 u32 iout_flash_max[2];
91 /* current flash timeout cache */
92 unsigned int current_flash_timeout;
93 /* ITORCH register cache */
95 /* mode of fled outputs */
96 unsigned int mode_flags;
97 /* recently strobed fled */
98 int strobing_sub_led_id;
99 /* bitmask of FLED outputs use state (bit 0. - FLED1, bit 1. - FLED2) */
101 /* FLED modes that can be set */
104 /* arrangement of current outputs */
108 static u8 max77693_led_iout_to_reg(u32 ua)
110 if (ua < FLASH_IOUT_MIN)
112 return (ua - FLASH_IOUT_MIN) / FLASH_IOUT_STEP;
115 static u8 max77693_flash_timeout_to_reg(u32 us)
117 return (us - FLASH_TIMEOUT_MIN) / FLASH_TIMEOUT_STEP;
120 static inline struct max77693_sub_led *flcdev_to_sub_led(
121 struct led_classdev_flash *fled_cdev)
123 return container_of(fled_cdev, struct max77693_sub_led, fled_cdev);
126 static inline struct max77693_led_device *sub_led_to_led(
127 struct max77693_sub_led *sub_led)
129 return container_of(sub_led, struct max77693_led_device,
130 sub_leds[sub_led->fled_id]);
133 static inline u8 max77693_led_vsys_to_reg(u32 mv)
135 return ((mv - MAX_FLASH1_VSYS_MIN) / MAX_FLASH1_VSYS_STEP) << 2;
138 static inline u8 max77693_led_vout_to_reg(u32 mv)
140 return (mv - FLASH_VOUT_MIN) / FLASH_VOUT_STEP + FLASH_VOUT_RMIN;
143 static inline bool max77693_fled_used(struct max77693_led_device *led,
146 u8 fled_bit = (fled_id == FLED1) ? FLED1_IOUT : FLED2_IOUT;
148 return led->fled_mask & fled_bit;
151 static int max77693_set_mode_reg(struct max77693_led_device *led, u8 mode)
153 struct regmap *rmap = led->regmap;
156 for (i = FLED1; i <= FLED2; ++i) {
157 if (mode & MODE_TORCH(i))
158 v |= FLASH_EN_ON << TORCH_EN_SHIFT(i);
160 if (mode & MODE_FLASH(i)) {
161 v |= FLASH_EN_ON << FLASH_EN_SHIFT(i);
162 } else if (mode & MODE_FLASH_EXTERNAL(i)) {
163 v |= FLASH_EN_FLASH << FLASH_EN_SHIFT(i);
165 * Enable hw triggering also for torch mode, as some
166 * camera sensors use torch led to fathom ambient light
167 * conditions before strobing the flash.
169 v |= FLASH_EN_TORCH << TORCH_EN_SHIFT(i);
173 /* Reset the register only prior setting flash modes */
174 if (mode & ~(MODE_TORCH(FLED1) | MODE_TORCH(FLED2))) {
175 ret = regmap_write(rmap, MAX77693_LED_REG_FLASH_EN, 0);
180 return regmap_write(rmap, MAX77693_LED_REG_FLASH_EN, v);
183 static int max77693_add_mode(struct max77693_led_device *led, u8 mode)
189 /* Span the mode on FLED2 for joint iouts case */
193 * FLASH_EXTERNAL mode activates FLASHEN and TORCHEN pins in the device.
194 * Corresponding register bit fields interfere with SW triggered modes,
195 * thus clear them to ensure proper device configuration.
197 for (i = FLED1; i <= FLED2; ++i)
198 if (mode & MODE_FLASH_EXTERNAL(i))
199 led->mode_flags &= (~MODE_TORCH(i) & ~MODE_FLASH(i));
201 new_mode_flags = mode | led->mode_flags;
202 new_mode_flags &= led->allowed_modes;
204 if (new_mode_flags ^ led->mode_flags)
205 led->mode_flags = new_mode_flags;
209 ret = max77693_set_mode_reg(led, led->mode_flags);
214 * Clear flash mode flag after setting the mode to avoid spurious flash
215 * strobing on each subsequent torch mode setting.
217 if (mode & MODE_FLASH_MASK)
218 led->mode_flags &= ~mode;
223 static int max77693_clear_mode(struct max77693_led_device *led,
227 /* Clear mode also on FLED2 for joint iouts case */
230 led->mode_flags &= ~mode;
232 return max77693_set_mode_reg(led, led->mode_flags);
235 static void max77693_add_allowed_modes(struct max77693_led_device *led,
236 int fled_id, enum max77693_led_mode mode)
239 led->allowed_modes |= (MODE_FLASH(fled_id) |
240 MODE_FLASH_EXTERNAL(fled_id));
242 led->allowed_modes |= MODE_TORCH(fled_id);
245 static void max77693_distribute_currents(struct max77693_led_device *led,
246 int fled_id, enum max77693_led_mode mode,
247 u32 micro_amp, u32 iout_max[2], u32 iout[2])
249 if (!led->iout_joint) {
250 iout[fled_id] = micro_amp;
251 max77693_add_allowed_modes(led, fled_id, mode);
255 iout[FLED1] = min(micro_amp, iout_max[FLED1]);
256 iout[FLED2] = micro_amp - iout[FLED1];
259 led->allowed_modes &= ~MODE_FLASH_MASK;
261 led->allowed_modes &= ~MODE_TORCH_MASK;
263 max77693_add_allowed_modes(led, FLED1, mode);
266 max77693_add_allowed_modes(led, FLED2, mode);
269 static int max77693_set_torch_current(struct max77693_led_device *led,
270 int fled_id, u32 micro_amp)
272 struct regmap *rmap = led->regmap;
273 u8 iout1_reg = 0, iout2_reg = 0;
276 max77693_distribute_currents(led, fled_id, TORCH, micro_amp,
277 led->iout_torch_max, iout);
279 if (fled_id == FLED1 || led->iout_joint) {
280 iout1_reg = max77693_led_iout_to_reg(iout[FLED1]);
281 led->torch_iout_reg &= TORCH_IOUT_MASK(TORCH_IOUT2_SHIFT);
283 if (fled_id == FLED2 || led->iout_joint) {
284 iout2_reg = max77693_led_iout_to_reg(iout[FLED2]);
285 led->torch_iout_reg &= TORCH_IOUT_MASK(TORCH_IOUT1_SHIFT);
288 led->torch_iout_reg |= ((iout1_reg << TORCH_IOUT1_SHIFT) |
289 (iout2_reg << TORCH_IOUT2_SHIFT));
291 return regmap_write(rmap, MAX77693_LED_REG_ITORCH,
292 led->torch_iout_reg);
295 static int max77693_set_flash_current(struct max77693_led_device *led,
299 struct regmap *rmap = led->regmap;
300 u8 iout1_reg, iout2_reg;
304 max77693_distribute_currents(led, fled_id, FLASH, micro_amp,
305 led->iout_flash_max, iout);
307 if (fled_id == FLED1 || led->iout_joint) {
308 iout1_reg = max77693_led_iout_to_reg(iout[FLED1]);
309 ret = regmap_write(rmap, MAX77693_LED_REG_IFLASH1,
314 if (fled_id == FLED2 || led->iout_joint) {
315 iout2_reg = max77693_led_iout_to_reg(iout[FLED2]);
316 ret = regmap_write(rmap, MAX77693_LED_REG_IFLASH2,
323 static int max77693_set_timeout(struct max77693_led_device *led, u32 microsec)
325 struct regmap *rmap = led->regmap;
329 v = max77693_flash_timeout_to_reg(microsec) | FLASH_TMR_LEVEL;
331 ret = regmap_write(rmap, MAX77693_LED_REG_FLASH_TIMER, v);
335 led->current_flash_timeout = microsec;
340 static int max77693_get_strobe_status(struct max77693_led_device *led,
343 struct regmap *rmap = led->regmap;
347 ret = regmap_read(rmap, MAX77693_LED_REG_FLASH_STATUS, &v);
351 *state = v & FLASH_STATUS_FLASH_ON;
356 static int max77693_get_flash_faults(struct max77693_sub_led *sub_led)
358 struct max77693_led_device *led = sub_led_to_led(sub_led);
359 struct regmap *rmap = led->regmap;
361 u8 fault_open_mask, fault_short_mask;
364 sub_led->flash_faults = 0;
366 if (led->iout_joint) {
367 fault_open_mask = FLASH_INT_FLED1_OPEN | FLASH_INT_FLED2_OPEN;
368 fault_short_mask = FLASH_INT_FLED1_SHORT |
369 FLASH_INT_FLED2_SHORT;
371 fault_open_mask = (sub_led->fled_id == FLED1) ?
372 FLASH_INT_FLED1_OPEN :
373 FLASH_INT_FLED2_OPEN;
374 fault_short_mask = (sub_led->fled_id == FLED1) ?
375 FLASH_INT_FLED1_SHORT :
376 FLASH_INT_FLED2_SHORT;
379 ret = regmap_read(rmap, MAX77693_LED_REG_FLASH_INT, &v);
383 if (v & fault_open_mask)
384 sub_led->flash_faults |= LED_FAULT_OVER_VOLTAGE;
385 if (v & fault_short_mask)
386 sub_led->flash_faults |= LED_FAULT_SHORT_CIRCUIT;
387 if (v & FLASH_INT_OVER_CURRENT)
388 sub_led->flash_faults |= LED_FAULT_OVER_CURRENT;
393 static int max77693_setup(struct max77693_led_device *led,
394 struct max77693_led_config_data *led_cfg)
396 struct regmap *rmap = led->regmap;
397 int i, first_led, last_led, ret;
398 u32 max_flash_curr[2];
402 * Initialize only flash current. Torch current doesn't
403 * require initialization as ITORCH register is written with
404 * new value each time brightness_set op is called.
406 if (led->iout_joint) {
409 max_flash_curr[FLED1] = led_cfg->iout_flash_max[FLED1] +
410 led_cfg->iout_flash_max[FLED2];
412 first_led = max77693_fled_used(led, FLED1) ? FLED1 : FLED2;
413 last_led = max77693_fled_used(led, FLED2) ? FLED2 : FLED1;
414 max_flash_curr[FLED1] = led_cfg->iout_flash_max[FLED1];
415 max_flash_curr[FLED2] = led_cfg->iout_flash_max[FLED2];
418 for (i = first_led; i <= last_led; ++i) {
419 ret = max77693_set_flash_current(led, i,
425 v = TORCH_TMR_NO_TIMER | MAX77693_LED_TRIG_TYPE_LEVEL;
426 ret = regmap_write(rmap, MAX77693_LED_REG_ITORCHTIMER, v);
430 if (led_cfg->low_vsys > 0)
431 v = max77693_led_vsys_to_reg(led_cfg->low_vsys) |
432 MAX_FLASH1_MAX_FL_EN;
436 ret = regmap_write(rmap, MAX77693_LED_REG_MAX_FLASH1, v);
439 ret = regmap_write(rmap, MAX77693_LED_REG_MAX_FLASH2, 0);
443 if (led_cfg->boost_mode == MAX77693_LED_BOOST_FIXED)
444 v = FLASH_BOOST_FIXED;
446 v = led_cfg->boost_mode | led_cfg->boost_mode << 1;
448 if (max77693_fled_used(led, FLED1) && max77693_fled_used(led, FLED2))
449 v |= FLASH_BOOST_LEDNUM_2;
451 ret = regmap_write(rmap, MAX77693_LED_REG_VOUT_CNTL, v);
455 v = max77693_led_vout_to_reg(led_cfg->boost_vout);
456 ret = regmap_write(rmap, MAX77693_LED_REG_VOUT_FLASH1, v);
460 return max77693_set_mode_reg(led, MODE_OFF);
463 /* LED subsystem callbacks */
464 static int max77693_led_brightness_set(struct led_classdev *led_cdev,
465 enum led_brightness value)
467 struct led_classdev_flash *fled_cdev = lcdev_to_flcdev(led_cdev);
468 struct max77693_sub_led *sub_led = flcdev_to_sub_led(fled_cdev);
469 struct max77693_led_device *led = sub_led_to_led(sub_led);
470 int fled_id = sub_led->fled_id, ret;
472 mutex_lock(&led->lock);
475 ret = max77693_clear_mode(led, MODE_TORCH(fled_id));
477 dev_dbg(&led->pdev->dev,
478 "Failed to clear torch mode (%d)\n",
483 ret = max77693_set_torch_current(led, fled_id, value * TORCH_IOUT_STEP);
485 dev_dbg(&led->pdev->dev,
486 "Failed to set torch current (%d)\n",
491 ret = max77693_add_mode(led, MODE_TORCH(fled_id));
493 dev_dbg(&led->pdev->dev,
494 "Failed to set torch mode (%d)\n",
497 mutex_unlock(&led->lock);
502 static int max77693_led_flash_brightness_set(
503 struct led_classdev_flash *fled_cdev,
506 struct max77693_sub_led *sub_led = flcdev_to_sub_led(fled_cdev);
507 struct max77693_led_device *led = sub_led_to_led(sub_led);
510 mutex_lock(&led->lock);
511 ret = max77693_set_flash_current(led, sub_led->fled_id, brightness);
512 mutex_unlock(&led->lock);
517 static int max77693_led_flash_strobe_set(
518 struct led_classdev_flash *fled_cdev,
521 struct max77693_sub_led *sub_led = flcdev_to_sub_led(fled_cdev);
522 struct max77693_led_device *led = sub_led_to_led(sub_led);
523 int fled_id = sub_led->fled_id;
526 mutex_lock(&led->lock);
529 ret = max77693_clear_mode(led, MODE_FLASH(fled_id));
533 if (sub_led->flash_timeout != led->current_flash_timeout) {
534 ret = max77693_set_timeout(led, sub_led->flash_timeout);
539 led->strobing_sub_led_id = fled_id;
541 ret = max77693_add_mode(led, MODE_FLASH(fled_id));
545 ret = max77693_get_flash_faults(sub_led);
548 mutex_unlock(&led->lock);
552 static int max77693_led_flash_fault_get(
553 struct led_classdev_flash *fled_cdev,
556 struct max77693_sub_led *sub_led = flcdev_to_sub_led(fled_cdev);
558 *fault = sub_led->flash_faults;
563 static int max77693_led_flash_strobe_get(
564 struct led_classdev_flash *fled_cdev,
567 struct max77693_sub_led *sub_led = flcdev_to_sub_led(fled_cdev);
568 struct max77693_led_device *led = sub_led_to_led(sub_led);
574 mutex_lock(&led->lock);
576 ret = max77693_get_strobe_status(led, state);
578 *state = !!(*state && (led->strobing_sub_led_id == sub_led->fled_id));
580 mutex_unlock(&led->lock);
585 static int max77693_led_flash_timeout_set(
586 struct led_classdev_flash *fled_cdev,
589 struct max77693_sub_led *sub_led = flcdev_to_sub_led(fled_cdev);
590 struct max77693_led_device *led = sub_led_to_led(sub_led);
592 mutex_lock(&led->lock);
593 sub_led->flash_timeout = timeout;
594 mutex_unlock(&led->lock);
599 static int max77693_led_parse_dt(struct max77693_led_device *led,
600 struct max77693_led_config_data *cfg,
601 struct device_node **sub_nodes)
603 struct device *dev = &led->pdev->dev;
604 struct max77693_sub_led *sub_leds = led->sub_leds;
605 struct device_node *node = dev->of_node, *child_node;
606 struct property *prop;
610 of_property_read_u32(node, "maxim,boost-mode", &cfg->boost_mode);
611 of_property_read_u32(node, "maxim,boost-mvout", &cfg->boost_vout);
612 of_property_read_u32(node, "maxim,mvsys-min", &cfg->low_vsys);
614 for_each_available_child_of_node(node, child_node) {
615 prop = of_find_property(child_node, "led-sources", NULL);
617 const __be32 *srcs = NULL;
619 for (i = 0; i < ARRAY_SIZE(led_sources); ++i) {
620 srcs = of_prop_next_u32(prop, srcs,
627 "led-sources DT property missing\n");
628 of_node_put(child_node);
634 led->fled_mask = FLED1_IOUT | FLED2_IOUT;
635 } else if (led_sources[0] == FLED1) {
637 led->fled_mask |= FLED1_IOUT;
638 } else if (led_sources[0] == FLED2) {
640 led->fled_mask |= FLED2_IOUT;
643 "Wrong led-sources DT property value.\n");
644 of_node_put(child_node);
648 if (sub_nodes[fled_id]) {
650 "Conflicting \"led-sources\" DT properties\n");
651 of_node_put(child_node);
655 sub_nodes[fled_id] = child_node;
656 sub_leds[fled_id].fled_id = fled_id;
658 cfg->label[fled_id] =
659 of_get_property(child_node, "label", NULL) ? :
662 ret = of_property_read_u32(child_node, "led-max-microamp",
663 &cfg->iout_torch_max[fled_id]);
665 cfg->iout_torch_max[fled_id] = TORCH_IOUT_MIN;
666 dev_warn(dev, "led-max-microamp DT property missing\n");
669 ret = of_property_read_u32(child_node, "flash-max-microamp",
670 &cfg->iout_flash_max[fled_id]);
672 cfg->iout_flash_max[fled_id] = FLASH_IOUT_MIN;
674 "flash-max-microamp DT property missing\n");
677 ret = of_property_read_u32(child_node, "flash-max-timeout-us",
678 &cfg->flash_timeout_max[fled_id]);
680 cfg->flash_timeout_max[fled_id] = FLASH_TIMEOUT_MIN;
682 "flash-max-timeout-us DT property missing\n");
685 if (++cfg->num_leds == 2 ||
686 (max77693_fled_used(led, FLED1) &&
687 max77693_fled_used(led, FLED2))) {
688 of_node_put(child_node);
693 if (cfg->num_leds == 0) {
694 dev_err(dev, "No DT child node found for connected LED(s).\n");
701 static void clamp_align(u32 *v, u32 min, u32 max, u32 step)
703 *v = clamp_val(*v, min, max);
705 *v = (*v - min) / step * step + min;
708 static void max77693_align_iout_current(struct max77693_led_device *led,
709 u32 *iout, u32 min, u32 max, u32 step)
713 if (led->iout_joint) {
714 if (iout[FLED1] > min) {
716 iout[FLED2] = iout[FLED1];
724 for (i = FLED1; i <= FLED2; ++i)
725 if (max77693_fled_used(led, i))
726 clamp_align(&iout[i], min, max, step);
731 static void max77693_led_validate_configuration(struct max77693_led_device *led,
732 struct max77693_led_config_data *cfg)
734 u32 flash_iout_max = cfg->boost_mode ? FLASH_IOUT_MAX_2LEDS :
738 if (cfg->num_leds == 1 &&
739 max77693_fled_used(led, FLED1) && max77693_fled_used(led, FLED2))
740 led->iout_joint = true;
742 cfg->boost_mode = clamp_val(cfg->boost_mode, MAX77693_LED_BOOST_NONE,
743 MAX77693_LED_BOOST_FIXED);
745 /* Boost must be enabled if both current outputs are used */
746 if ((cfg->boost_mode == MAX77693_LED_BOOST_NONE) && led->iout_joint)
747 cfg->boost_mode = MAX77693_LED_BOOST_FIXED;
749 max77693_align_iout_current(led, cfg->iout_torch_max,
750 TORCH_IOUT_MIN, TORCH_IOUT_MAX, TORCH_IOUT_STEP);
752 max77693_align_iout_current(led, cfg->iout_flash_max,
753 FLASH_IOUT_MIN, flash_iout_max, FLASH_IOUT_STEP);
755 for (i = 0; i < ARRAY_SIZE(cfg->flash_timeout_max); ++i)
756 clamp_align(&cfg->flash_timeout_max[i], FLASH_TIMEOUT_MIN,
757 FLASH_TIMEOUT_MAX, FLASH_TIMEOUT_STEP);
759 clamp_align(&cfg->boost_vout, FLASH_VOUT_MIN, FLASH_VOUT_MAX,
763 clamp_align(&cfg->low_vsys, MAX_FLASH1_VSYS_MIN,
764 MAX_FLASH1_VSYS_MAX, MAX_FLASH1_VSYS_STEP);
767 static int max77693_led_get_configuration(struct max77693_led_device *led,
768 struct max77693_led_config_data *cfg,
769 struct device_node **sub_nodes)
773 ret = max77693_led_parse_dt(led, cfg, sub_nodes);
777 max77693_led_validate_configuration(led, cfg);
779 memcpy(led->iout_torch_max, cfg->iout_torch_max,
780 sizeof(led->iout_torch_max));
781 memcpy(led->iout_flash_max, cfg->iout_flash_max,
782 sizeof(led->iout_flash_max));
787 static const struct led_flash_ops flash_ops = {
788 .flash_brightness_set = max77693_led_flash_brightness_set,
789 .strobe_set = max77693_led_flash_strobe_set,
790 .strobe_get = max77693_led_flash_strobe_get,
791 .timeout_set = max77693_led_flash_timeout_set,
792 .fault_get = max77693_led_flash_fault_get,
795 static void max77693_init_flash_settings(struct max77693_sub_led *sub_led,
796 struct max77693_led_config_data *led_cfg)
798 struct led_classdev_flash *fled_cdev = &sub_led->fled_cdev;
799 struct max77693_led_device *led = sub_led_to_led(sub_led);
800 int fled_id = sub_led->fled_id;
801 struct led_flash_setting *setting;
803 /* Init flash intensity setting */
804 setting = &fled_cdev->brightness;
805 setting->min = FLASH_IOUT_MIN;
806 setting->max = led->iout_joint ?
807 led_cfg->iout_flash_max[FLED1] +
808 led_cfg->iout_flash_max[FLED2] :
809 led_cfg->iout_flash_max[fled_id];
810 setting->step = FLASH_IOUT_STEP;
811 setting->val = setting->max;
813 /* Init flash timeout setting */
814 setting = &fled_cdev->timeout;
815 setting->min = FLASH_TIMEOUT_MIN;
816 setting->max = led_cfg->flash_timeout_max[fled_id];
817 setting->step = FLASH_TIMEOUT_STEP;
818 setting->val = setting->max;
821 #if IS_ENABLED(CONFIG_V4L2_FLASH_LED_CLASS)
823 static int max77693_led_external_strobe_set(
824 struct v4l2_flash *v4l2_flash,
827 struct max77693_sub_led *sub_led =
828 flcdev_to_sub_led(v4l2_flash->fled_cdev);
829 struct max77693_led_device *led = sub_led_to_led(sub_led);
830 int fled_id = sub_led->fled_id;
833 mutex_lock(&led->lock);
836 ret = max77693_add_mode(led, MODE_FLASH_EXTERNAL(fled_id));
838 ret = max77693_clear_mode(led, MODE_FLASH_EXTERNAL(fled_id));
840 mutex_unlock(&led->lock);
845 static void max77693_init_v4l2_flash_config(struct max77693_sub_led *sub_led,
846 struct max77693_led_config_data *led_cfg,
847 struct v4l2_flash_config *v4l2_sd_cfg)
849 struct max77693_led_device *led = sub_led_to_led(sub_led);
850 struct device *dev = &led->pdev->dev;
851 struct max77693_dev *iodev = dev_get_drvdata(dev->parent);
852 struct i2c_client *i2c = iodev->i2c;
853 struct led_flash_setting *s;
855 snprintf(v4l2_sd_cfg->dev_name, sizeof(v4l2_sd_cfg->dev_name),
856 "%s %d-%04x", sub_led->fled_cdev.led_cdev.name,
857 i2c_adapter_id(i2c->adapter), i2c->addr);
859 s = &v4l2_sd_cfg->torch_intensity;
860 s->min = TORCH_IOUT_MIN;
861 s->max = sub_led->fled_cdev.led_cdev.max_brightness * TORCH_IOUT_STEP;
862 s->step = TORCH_IOUT_STEP;
865 /* Init flash faults config */
866 v4l2_sd_cfg->flash_faults = LED_FAULT_OVER_VOLTAGE |
867 LED_FAULT_SHORT_CIRCUIT |
868 LED_FAULT_OVER_CURRENT;
870 v4l2_sd_cfg->has_external_strobe = true;
873 static const struct v4l2_flash_ops v4l2_flash_ops = {
874 .external_strobe_set = max77693_led_external_strobe_set,
877 static inline void max77693_init_v4l2_flash_config(
878 struct max77693_sub_led *sub_led,
879 struct max77693_led_config_data *led_cfg,
880 struct v4l2_flash_config *v4l2_sd_cfg)
883 static const struct v4l2_flash_ops v4l2_flash_ops;
886 static void max77693_init_fled_cdev(struct max77693_sub_led *sub_led,
887 struct max77693_led_config_data *led_cfg)
889 struct max77693_led_device *led = sub_led_to_led(sub_led);
890 int fled_id = sub_led->fled_id;
891 struct led_classdev_flash *fled_cdev;
892 struct led_classdev *led_cdev;
894 /* Initialize LED Flash class device */
895 fled_cdev = &sub_led->fled_cdev;
896 fled_cdev->ops = &flash_ops;
897 led_cdev = &fled_cdev->led_cdev;
899 led_cdev->name = led_cfg->label[fled_id];
901 led_cdev->brightness_set_blocking = max77693_led_brightness_set;
902 led_cdev->max_brightness = (led->iout_joint ?
903 led_cfg->iout_torch_max[FLED1] +
904 led_cfg->iout_torch_max[FLED2] :
905 led_cfg->iout_torch_max[fled_id]) /
907 led_cdev->flags |= LED_DEV_CAP_FLASH;
909 max77693_init_flash_settings(sub_led, led_cfg);
911 /* Init flash timeout cache */
912 sub_led->flash_timeout = fled_cdev->timeout.val;
915 static int max77693_register_led(struct max77693_sub_led *sub_led,
916 struct max77693_led_config_data *led_cfg,
917 struct device_node *sub_node)
919 struct max77693_led_device *led = sub_led_to_led(sub_led);
920 struct led_classdev_flash *fled_cdev = &sub_led->fled_cdev;
921 struct device *dev = &led->pdev->dev;
922 struct v4l2_flash_config v4l2_sd_cfg = {};
925 /* Register in the LED subsystem */
926 ret = led_classdev_flash_register(dev, fled_cdev);
930 max77693_init_v4l2_flash_config(sub_led, led_cfg, &v4l2_sd_cfg);
932 /* Register in the V4L2 subsystem. */
933 sub_led->v4l2_flash = v4l2_flash_init(dev, sub_node, fled_cdev, NULL,
934 &v4l2_flash_ops, &v4l2_sd_cfg);
935 if (IS_ERR(sub_led->v4l2_flash)) {
936 ret = PTR_ERR(sub_led->v4l2_flash);
937 goto err_v4l2_flash_init;
943 led_classdev_flash_unregister(fled_cdev);
947 static int max77693_led_probe(struct platform_device *pdev)
949 struct device *dev = &pdev->dev;
950 struct max77693_dev *iodev = dev_get_drvdata(dev->parent);
951 struct max77693_led_device *led;
952 struct max77693_sub_led *sub_leds;
953 struct device_node *sub_nodes[2] = {};
954 struct max77693_led_config_data led_cfg = {};
955 int init_fled_cdev[2], i, ret;
957 led = devm_kzalloc(dev, sizeof(*led), GFP_KERNEL);
962 led->regmap = iodev->regmap;
963 led->allowed_modes = MODE_FLASH_MASK;
964 sub_leds = led->sub_leds;
966 platform_set_drvdata(pdev, led);
967 ret = max77693_led_get_configuration(led, &led_cfg, sub_nodes);
971 ret = max77693_setup(led, &led_cfg);
975 mutex_init(&led->lock);
977 init_fled_cdev[FLED1] =
978 led->iout_joint || max77693_fled_used(led, FLED1);
979 init_fled_cdev[FLED2] =
980 !led->iout_joint && max77693_fled_used(led, FLED2);
982 for (i = FLED1; i <= FLED2; ++i) {
983 if (!init_fled_cdev[i])
986 /* Initialize LED Flash class device */
987 max77693_init_fled_cdev(&sub_leds[i], &led_cfg);
990 * Register LED Flash class device and corresponding
993 ret = max77693_register_led(&sub_leds[i], &led_cfg,
997 * At this moment FLED1 might have been already
998 * registered and it needs to be released.
1001 goto err_register_led2;
1003 goto err_register_led1;
1010 /* It is possible than only FLED2 was to be registered */
1011 if (!init_fled_cdev[FLED1])
1012 goto err_register_led1;
1013 v4l2_flash_release(sub_leds[FLED1].v4l2_flash);
1014 led_classdev_flash_unregister(&sub_leds[FLED1].fled_cdev);
1016 mutex_destroy(&led->lock);
1021 static int max77693_led_remove(struct platform_device *pdev)
1023 struct max77693_led_device *led = platform_get_drvdata(pdev);
1024 struct max77693_sub_led *sub_leds = led->sub_leds;
1026 if (led->iout_joint || max77693_fled_used(led, FLED1)) {
1027 v4l2_flash_release(sub_leds[FLED1].v4l2_flash);
1028 led_classdev_flash_unregister(&sub_leds[FLED1].fled_cdev);
1031 if (!led->iout_joint && max77693_fled_used(led, FLED2)) {
1032 v4l2_flash_release(sub_leds[FLED2].v4l2_flash);
1033 led_classdev_flash_unregister(&sub_leds[FLED2].fled_cdev);
1036 mutex_destroy(&led->lock);
1041 static const struct of_device_id max77693_led_dt_match[] = {
1042 { .compatible = "maxim,max77693-led" },
1045 MODULE_DEVICE_TABLE(of, max77693_led_dt_match);
1047 static struct platform_driver max77693_led_driver = {
1048 .probe = max77693_led_probe,
1049 .remove = max77693_led_remove,
1051 .name = "max77693-led",
1052 .of_match_table = max77693_led_dt_match,
1056 module_platform_driver(max77693_led_driver);
1058 MODULE_AUTHOR("Jacek Anaszewski <j.anaszewski@samsung.com>");
1059 MODULE_AUTHOR("Andrzej Hajda <a.hajda@samsung.com>");
1060 MODULE_DESCRIPTION("Maxim MAX77693 led flash driver");
1061 MODULE_LICENSE("GPL v2");