1 // SPDX-License-Identifier: GPL-2.0-only
3 * LED Flash class driver for the flash cell of max77693 mfd.
5 * Copyright (C) 2015, Samsung Electronics Co., Ltd.
7 * Authors: Jacek Anaszewski <j.anaszewski@samsung.com>
8 * Andrzej Hajda <a.hajda@samsung.com>
11 #include <linux/led-class-flash.h>
12 #include <linux/mfd/max77693.h>
13 #include <linux/mfd/max77693-common.h>
14 #include <linux/mfd/max77693-private.h>
15 #include <linux/module.h>
16 #include <linux/mutex.h>
17 #include <linux/platform_device.h>
18 #include <linux/regmap.h>
19 #include <linux/slab.h>
20 #include <media/v4l2-flash-led-class.h>
23 #define MODE_FLASH(a) (1 << (a))
24 #define MODE_TORCH(a) (1 << (2 + (a)))
25 #define MODE_FLASH_EXTERNAL(a) (1 << (4 + (a)))
27 #define MODE_FLASH_MASK (MODE_FLASH(FLED1) | MODE_FLASH(FLED2) | \
28 MODE_FLASH_EXTERNAL(FLED1) | \
29 MODE_FLASH_EXTERNAL(FLED2))
30 #define MODE_TORCH_MASK (MODE_TORCH(FLED1) | MODE_TORCH(FLED2))
32 #define FLED1_IOUT (1 << 0)
33 #define FLED2_IOUT (1 << 1)
40 enum max77693_led_mode
{
45 struct max77693_led_config_data
{
47 u32 iout_torch_max
[2];
48 u32 iout_flash_max
[2];
49 u32 flash_timeout_max
[2];
56 struct max77693_sub_led
{
57 /* corresponding FLED output identifier */
59 /* corresponding LED Flash class device */
60 struct led_classdev_flash fled_cdev
;
61 /* V4L2 Flash device */
62 struct v4l2_flash
*v4l2_flash
;
64 /* brightness cache */
65 unsigned int torch_brightness
;
66 /* flash timeout cache */
67 unsigned int flash_timeout
;
68 /* flash faults that may have occurred */
72 struct max77693_led_device
{
73 /* parent mfd regmap */
74 struct regmap
*regmap
;
75 /* platform device data */
76 struct platform_device
*pdev
;
77 /* secures access to the device */
81 struct max77693_sub_led sub_leds
[2];
83 /* maximum torch current values for FLED outputs */
84 u32 iout_torch_max
[2];
85 /* maximum flash current values for FLED outputs */
86 u32 iout_flash_max
[2];
88 /* current flash timeout cache */
89 unsigned int current_flash_timeout
;
90 /* ITORCH register cache */
92 /* mode of fled outputs */
93 unsigned int mode_flags
;
94 /* recently strobed fled */
95 int strobing_sub_led_id
;
96 /* bitmask of FLED outputs use state (bit 0. - FLED1, bit 1. - FLED2) */
98 /* FLED modes that can be set */
101 /* arrangement of current outputs */
105 static u8
max77693_led_iout_to_reg(u32 ua
)
107 if (ua
< FLASH_IOUT_MIN
)
109 return (ua
- FLASH_IOUT_MIN
) / FLASH_IOUT_STEP
;
112 static u8
max77693_flash_timeout_to_reg(u32 us
)
114 return (us
- FLASH_TIMEOUT_MIN
) / FLASH_TIMEOUT_STEP
;
117 static inline struct max77693_sub_led
*flcdev_to_sub_led(
118 struct led_classdev_flash
*fled_cdev
)
120 return container_of(fled_cdev
, struct max77693_sub_led
, fled_cdev
);
123 static inline struct max77693_led_device
*sub_led_to_led(
124 struct max77693_sub_led
*sub_led
)
126 return container_of(sub_led
, struct max77693_led_device
,
127 sub_leds
[sub_led
->fled_id
]);
130 static inline u8
max77693_led_vsys_to_reg(u32 mv
)
132 return ((mv
- MAX_FLASH1_VSYS_MIN
) / MAX_FLASH1_VSYS_STEP
) << 2;
135 static inline u8
max77693_led_vout_to_reg(u32 mv
)
137 return (mv
- FLASH_VOUT_MIN
) / FLASH_VOUT_STEP
+ FLASH_VOUT_RMIN
;
140 static inline bool max77693_fled_used(struct max77693_led_device
*led
,
143 u8 fled_bit
= (fled_id
== FLED1
) ? FLED1_IOUT
: FLED2_IOUT
;
145 return led
->fled_mask
& fled_bit
;
148 static int max77693_set_mode_reg(struct max77693_led_device
*led
, u8 mode
)
150 struct regmap
*rmap
= led
->regmap
;
153 for (i
= FLED1
; i
<= FLED2
; ++i
) {
154 if (mode
& MODE_TORCH(i
))
155 v
|= FLASH_EN_ON
<< TORCH_EN_SHIFT(i
);
157 if (mode
& MODE_FLASH(i
)) {
158 v
|= FLASH_EN_ON
<< FLASH_EN_SHIFT(i
);
159 } else if (mode
& MODE_FLASH_EXTERNAL(i
)) {
160 v
|= FLASH_EN_FLASH
<< FLASH_EN_SHIFT(i
);
162 * Enable hw triggering also for torch mode, as some
163 * camera sensors use torch led to fathom ambient light
164 * conditions before strobing the flash.
166 v
|= FLASH_EN_TORCH
<< TORCH_EN_SHIFT(i
);
170 /* Reset the register only prior setting flash modes */
171 if (mode
& ~(MODE_TORCH(FLED1
) | MODE_TORCH(FLED2
))) {
172 ret
= regmap_write(rmap
, MAX77693_LED_REG_FLASH_EN
, 0);
177 return regmap_write(rmap
, MAX77693_LED_REG_FLASH_EN
, v
);
180 static int max77693_add_mode(struct max77693_led_device
*led
, u8 mode
)
186 /* Span the mode on FLED2 for joint iouts case */
190 * FLASH_EXTERNAL mode activates FLASHEN and TORCHEN pins in the device.
191 * Corresponding register bit fields interfere with SW triggered modes,
192 * thus clear them to ensure proper device configuration.
194 for (i
= FLED1
; i
<= FLED2
; ++i
)
195 if (mode
& MODE_FLASH_EXTERNAL(i
))
196 led
->mode_flags
&= (~MODE_TORCH(i
) & ~MODE_FLASH(i
));
198 new_mode_flags
= mode
| led
->mode_flags
;
199 new_mode_flags
&= led
->allowed_modes
;
201 if (new_mode_flags
^ led
->mode_flags
)
202 led
->mode_flags
= new_mode_flags
;
206 ret
= max77693_set_mode_reg(led
, led
->mode_flags
);
211 * Clear flash mode flag after setting the mode to avoid spurious flash
212 * strobing on each subsequent torch mode setting.
214 if (mode
& MODE_FLASH_MASK
)
215 led
->mode_flags
&= ~mode
;
220 static int max77693_clear_mode(struct max77693_led_device
*led
,
224 /* Clear mode also on FLED2 for joint iouts case */
227 led
->mode_flags
&= ~mode
;
229 return max77693_set_mode_reg(led
, led
->mode_flags
);
232 static void max77693_add_allowed_modes(struct max77693_led_device
*led
,
233 int fled_id
, enum max77693_led_mode mode
)
236 led
->allowed_modes
|= (MODE_FLASH(fled_id
) |
237 MODE_FLASH_EXTERNAL(fled_id
));
239 led
->allowed_modes
|= MODE_TORCH(fled_id
);
242 static void max77693_distribute_currents(struct max77693_led_device
*led
,
243 int fled_id
, enum max77693_led_mode mode
,
244 u32 micro_amp
, u32 iout_max
[2], u32 iout
[2])
246 if (!led
->iout_joint
) {
247 iout
[fled_id
] = micro_amp
;
248 max77693_add_allowed_modes(led
, fled_id
, mode
);
252 iout
[FLED1
] = min(micro_amp
, iout_max
[FLED1
]);
253 iout
[FLED2
] = micro_amp
- iout
[FLED1
];
256 led
->allowed_modes
&= ~MODE_FLASH_MASK
;
258 led
->allowed_modes
&= ~MODE_TORCH_MASK
;
260 max77693_add_allowed_modes(led
, FLED1
, mode
);
263 max77693_add_allowed_modes(led
, FLED2
, mode
);
266 static int max77693_set_torch_current(struct max77693_led_device
*led
,
267 int fled_id
, u32 micro_amp
)
269 struct regmap
*rmap
= led
->regmap
;
270 u8 iout1_reg
= 0, iout2_reg
= 0;
273 max77693_distribute_currents(led
, fled_id
, TORCH
, micro_amp
,
274 led
->iout_torch_max
, iout
);
276 if (fled_id
== FLED1
|| led
->iout_joint
) {
277 iout1_reg
= max77693_led_iout_to_reg(iout
[FLED1
]);
278 led
->torch_iout_reg
&= TORCH_IOUT_MASK(TORCH_IOUT2_SHIFT
);
280 if (fled_id
== FLED2
|| led
->iout_joint
) {
281 iout2_reg
= max77693_led_iout_to_reg(iout
[FLED2
]);
282 led
->torch_iout_reg
&= TORCH_IOUT_MASK(TORCH_IOUT1_SHIFT
);
285 led
->torch_iout_reg
|= ((iout1_reg
<< TORCH_IOUT1_SHIFT
) |
286 (iout2_reg
<< TORCH_IOUT2_SHIFT
));
288 return regmap_write(rmap
, MAX77693_LED_REG_ITORCH
,
289 led
->torch_iout_reg
);
292 static int max77693_set_flash_current(struct max77693_led_device
*led
,
296 struct regmap
*rmap
= led
->regmap
;
297 u8 iout1_reg
, iout2_reg
;
301 max77693_distribute_currents(led
, fled_id
, FLASH
, micro_amp
,
302 led
->iout_flash_max
, iout
);
304 if (fled_id
== FLED1
|| led
->iout_joint
) {
305 iout1_reg
= max77693_led_iout_to_reg(iout
[FLED1
]);
306 ret
= regmap_write(rmap
, MAX77693_LED_REG_IFLASH1
,
311 if (fled_id
== FLED2
|| led
->iout_joint
) {
312 iout2_reg
= max77693_led_iout_to_reg(iout
[FLED2
]);
313 ret
= regmap_write(rmap
, MAX77693_LED_REG_IFLASH2
,
320 static int max77693_set_timeout(struct max77693_led_device
*led
, u32 microsec
)
322 struct regmap
*rmap
= led
->regmap
;
326 v
= max77693_flash_timeout_to_reg(microsec
) | FLASH_TMR_LEVEL
;
328 ret
= regmap_write(rmap
, MAX77693_LED_REG_FLASH_TIMER
, v
);
332 led
->current_flash_timeout
= microsec
;
337 static int max77693_get_strobe_status(struct max77693_led_device
*led
,
340 struct regmap
*rmap
= led
->regmap
;
344 ret
= regmap_read(rmap
, MAX77693_LED_REG_FLASH_STATUS
, &v
);
348 *state
= v
& FLASH_STATUS_FLASH_ON
;
353 static int max77693_get_flash_faults(struct max77693_sub_led
*sub_led
)
355 struct max77693_led_device
*led
= sub_led_to_led(sub_led
);
356 struct regmap
*rmap
= led
->regmap
;
358 u8 fault_open_mask
, fault_short_mask
;
361 sub_led
->flash_faults
= 0;
363 if (led
->iout_joint
) {
364 fault_open_mask
= FLASH_INT_FLED1_OPEN
| FLASH_INT_FLED2_OPEN
;
365 fault_short_mask
= FLASH_INT_FLED1_SHORT
|
366 FLASH_INT_FLED2_SHORT
;
368 fault_open_mask
= (sub_led
->fled_id
== FLED1
) ?
369 FLASH_INT_FLED1_OPEN
:
370 FLASH_INT_FLED2_OPEN
;
371 fault_short_mask
= (sub_led
->fled_id
== FLED1
) ?
372 FLASH_INT_FLED1_SHORT
:
373 FLASH_INT_FLED2_SHORT
;
376 ret
= regmap_read(rmap
, MAX77693_LED_REG_FLASH_INT
, &v
);
380 if (v
& fault_open_mask
)
381 sub_led
->flash_faults
|= LED_FAULT_OVER_VOLTAGE
;
382 if (v
& fault_short_mask
)
383 sub_led
->flash_faults
|= LED_FAULT_SHORT_CIRCUIT
;
384 if (v
& FLASH_INT_OVER_CURRENT
)
385 sub_led
->flash_faults
|= LED_FAULT_OVER_CURRENT
;
390 static int max77693_setup(struct max77693_led_device
*led
,
391 struct max77693_led_config_data
*led_cfg
)
393 struct regmap
*rmap
= led
->regmap
;
394 int i
, first_led
, last_led
, ret
;
395 u32 max_flash_curr
[2];
399 * Initialize only flash current. Torch current doesn't
400 * require initialization as ITORCH register is written with
401 * new value each time brightness_set op is called.
403 if (led
->iout_joint
) {
406 max_flash_curr
[FLED1
] = led_cfg
->iout_flash_max
[FLED1
] +
407 led_cfg
->iout_flash_max
[FLED2
];
409 first_led
= max77693_fled_used(led
, FLED1
) ? FLED1
: FLED2
;
410 last_led
= max77693_fled_used(led
, FLED2
) ? FLED2
: FLED1
;
411 max_flash_curr
[FLED1
] = led_cfg
->iout_flash_max
[FLED1
];
412 max_flash_curr
[FLED2
] = led_cfg
->iout_flash_max
[FLED2
];
415 for (i
= first_led
; i
<= last_led
; ++i
) {
416 ret
= max77693_set_flash_current(led
, i
,
422 v
= TORCH_TMR_NO_TIMER
| MAX77693_LED_TRIG_TYPE_LEVEL
;
423 ret
= regmap_write(rmap
, MAX77693_LED_REG_ITORCHTIMER
, v
);
427 if (led_cfg
->low_vsys
> 0)
428 v
= max77693_led_vsys_to_reg(led_cfg
->low_vsys
) |
429 MAX_FLASH1_MAX_FL_EN
;
433 ret
= regmap_write(rmap
, MAX77693_LED_REG_MAX_FLASH1
, v
);
436 ret
= regmap_write(rmap
, MAX77693_LED_REG_MAX_FLASH2
, 0);
440 if (led_cfg
->boost_mode
== MAX77693_LED_BOOST_FIXED
)
441 v
= FLASH_BOOST_FIXED
;
443 v
= led_cfg
->boost_mode
| led_cfg
->boost_mode
<< 1;
445 if (max77693_fled_used(led
, FLED1
) && max77693_fled_used(led
, FLED2
))
446 v
|= FLASH_BOOST_LEDNUM_2
;
448 ret
= regmap_write(rmap
, MAX77693_LED_REG_VOUT_CNTL
, v
);
452 v
= max77693_led_vout_to_reg(led_cfg
->boost_vout
);
453 ret
= regmap_write(rmap
, MAX77693_LED_REG_VOUT_FLASH1
, v
);
457 return max77693_set_mode_reg(led
, MODE_OFF
);
460 /* LED subsystem callbacks */
461 static int max77693_led_brightness_set(struct led_classdev
*led_cdev
,
462 enum led_brightness value
)
464 struct led_classdev_flash
*fled_cdev
= lcdev_to_flcdev(led_cdev
);
465 struct max77693_sub_led
*sub_led
= flcdev_to_sub_led(fled_cdev
);
466 struct max77693_led_device
*led
= sub_led_to_led(sub_led
);
467 int fled_id
= sub_led
->fled_id
, ret
;
469 mutex_lock(&led
->lock
);
472 ret
= max77693_clear_mode(led
, MODE_TORCH(fled_id
));
474 dev_dbg(&led
->pdev
->dev
,
475 "Failed to clear torch mode (%d)\n",
480 ret
= max77693_set_torch_current(led
, fled_id
, value
* TORCH_IOUT_STEP
);
482 dev_dbg(&led
->pdev
->dev
,
483 "Failed to set torch current (%d)\n",
488 ret
= max77693_add_mode(led
, MODE_TORCH(fled_id
));
490 dev_dbg(&led
->pdev
->dev
,
491 "Failed to set torch mode (%d)\n",
494 mutex_unlock(&led
->lock
);
499 static int max77693_led_flash_brightness_set(
500 struct led_classdev_flash
*fled_cdev
,
503 struct max77693_sub_led
*sub_led
= flcdev_to_sub_led(fled_cdev
);
504 struct max77693_led_device
*led
= sub_led_to_led(sub_led
);
507 mutex_lock(&led
->lock
);
508 ret
= max77693_set_flash_current(led
, sub_led
->fled_id
, brightness
);
509 mutex_unlock(&led
->lock
);
514 static int max77693_led_flash_strobe_set(
515 struct led_classdev_flash
*fled_cdev
,
518 struct max77693_sub_led
*sub_led
= flcdev_to_sub_led(fled_cdev
);
519 struct max77693_led_device
*led
= sub_led_to_led(sub_led
);
520 int fled_id
= sub_led
->fled_id
;
523 mutex_lock(&led
->lock
);
526 ret
= max77693_clear_mode(led
, MODE_FLASH(fled_id
));
530 if (sub_led
->flash_timeout
!= led
->current_flash_timeout
) {
531 ret
= max77693_set_timeout(led
, sub_led
->flash_timeout
);
536 led
->strobing_sub_led_id
= fled_id
;
538 ret
= max77693_add_mode(led
, MODE_FLASH(fled_id
));
542 ret
= max77693_get_flash_faults(sub_led
);
545 mutex_unlock(&led
->lock
);
549 static int max77693_led_flash_fault_get(
550 struct led_classdev_flash
*fled_cdev
,
553 struct max77693_sub_led
*sub_led
= flcdev_to_sub_led(fled_cdev
);
555 *fault
= sub_led
->flash_faults
;
560 static int max77693_led_flash_strobe_get(
561 struct led_classdev_flash
*fled_cdev
,
564 struct max77693_sub_led
*sub_led
= flcdev_to_sub_led(fled_cdev
);
565 struct max77693_led_device
*led
= sub_led_to_led(sub_led
);
571 mutex_lock(&led
->lock
);
573 ret
= max77693_get_strobe_status(led
, state
);
575 *state
= !!(*state
&& (led
->strobing_sub_led_id
== sub_led
->fled_id
));
577 mutex_unlock(&led
->lock
);
582 static int max77693_led_flash_timeout_set(
583 struct led_classdev_flash
*fled_cdev
,
586 struct max77693_sub_led
*sub_led
= flcdev_to_sub_led(fled_cdev
);
587 struct max77693_led_device
*led
= sub_led_to_led(sub_led
);
589 mutex_lock(&led
->lock
);
590 sub_led
->flash_timeout
= timeout
;
591 mutex_unlock(&led
->lock
);
596 static int max77693_led_parse_dt(struct max77693_led_device
*led
,
597 struct max77693_led_config_data
*cfg
,
598 struct device_node
**sub_nodes
)
600 struct device
*dev
= &led
->pdev
->dev
;
601 struct max77693_sub_led
*sub_leds
= led
->sub_leds
;
602 struct device_node
*node
= dev
->of_node
, *child_node
;
603 struct property
*prop
;
607 of_property_read_u32(node
, "maxim,boost-mode", &cfg
->boost_mode
);
608 of_property_read_u32(node
, "maxim,boost-mvout", &cfg
->boost_vout
);
609 of_property_read_u32(node
, "maxim,mvsys-min", &cfg
->low_vsys
);
611 for_each_available_child_of_node(node
, child_node
) {
612 prop
= of_find_property(child_node
, "led-sources", NULL
);
614 const __be32
*srcs
= NULL
;
616 for (i
= 0; i
< ARRAY_SIZE(led_sources
); ++i
) {
617 srcs
= of_prop_next_u32(prop
, srcs
,
624 "led-sources DT property missing\n");
625 of_node_put(child_node
);
631 led
->fled_mask
= FLED1_IOUT
| FLED2_IOUT
;
632 } else if (led_sources
[0] == FLED1
) {
634 led
->fled_mask
|= FLED1_IOUT
;
635 } else if (led_sources
[0] == FLED2
) {
637 led
->fled_mask
|= FLED2_IOUT
;
640 "Wrong led-sources DT property value.\n");
641 of_node_put(child_node
);
645 if (sub_nodes
[fled_id
]) {
647 "Conflicting \"led-sources\" DT properties\n");
648 of_node_put(child_node
);
652 sub_nodes
[fled_id
] = child_node
;
653 sub_leds
[fled_id
].fled_id
= fled_id
;
655 cfg
->label
[fled_id
] =
656 of_get_property(child_node
, "label", NULL
) ? :
659 ret
= of_property_read_u32(child_node
, "led-max-microamp",
660 &cfg
->iout_torch_max
[fled_id
]);
662 cfg
->iout_torch_max
[fled_id
] = TORCH_IOUT_MIN
;
663 dev_warn(dev
, "led-max-microamp DT property missing\n");
666 ret
= of_property_read_u32(child_node
, "flash-max-microamp",
667 &cfg
->iout_flash_max
[fled_id
]);
669 cfg
->iout_flash_max
[fled_id
] = FLASH_IOUT_MIN
;
671 "flash-max-microamp DT property missing\n");
674 ret
= of_property_read_u32(child_node
, "flash-max-timeout-us",
675 &cfg
->flash_timeout_max
[fled_id
]);
677 cfg
->flash_timeout_max
[fled_id
] = FLASH_TIMEOUT_MIN
;
679 "flash-max-timeout-us DT property missing\n");
682 if (++cfg
->num_leds
== 2 ||
683 (max77693_fled_used(led
, FLED1
) &&
684 max77693_fled_used(led
, FLED2
))) {
685 of_node_put(child_node
);
690 if (cfg
->num_leds
== 0) {
691 dev_err(dev
, "No DT child node found for connected LED(s).\n");
698 static void clamp_align(u32
*v
, u32 min
, u32 max
, u32 step
)
700 *v
= clamp_val(*v
, min
, max
);
702 *v
= (*v
- min
) / step
* step
+ min
;
705 static void max77693_align_iout_current(struct max77693_led_device
*led
,
706 u32
*iout
, u32 min
, u32 max
, u32 step
)
710 if (led
->iout_joint
) {
711 if (iout
[FLED1
] > min
) {
713 iout
[FLED2
] = iout
[FLED1
];
721 for (i
= FLED1
; i
<= FLED2
; ++i
)
722 if (max77693_fled_used(led
, i
))
723 clamp_align(&iout
[i
], min
, max
, step
);
728 static void max77693_led_validate_configuration(struct max77693_led_device
*led
,
729 struct max77693_led_config_data
*cfg
)
731 u32 flash_iout_max
= cfg
->boost_mode
? FLASH_IOUT_MAX_2LEDS
:
735 if (cfg
->num_leds
== 1 &&
736 max77693_fled_used(led
, FLED1
) && max77693_fled_used(led
, FLED2
))
737 led
->iout_joint
= true;
739 cfg
->boost_mode
= clamp_val(cfg
->boost_mode
, MAX77693_LED_BOOST_NONE
,
740 MAX77693_LED_BOOST_FIXED
);
742 /* Boost must be enabled if both current outputs are used */
743 if ((cfg
->boost_mode
== MAX77693_LED_BOOST_NONE
) && led
->iout_joint
)
744 cfg
->boost_mode
= MAX77693_LED_BOOST_FIXED
;
746 max77693_align_iout_current(led
, cfg
->iout_torch_max
,
747 TORCH_IOUT_MIN
, TORCH_IOUT_MAX
, TORCH_IOUT_STEP
);
749 max77693_align_iout_current(led
, cfg
->iout_flash_max
,
750 FLASH_IOUT_MIN
, flash_iout_max
, FLASH_IOUT_STEP
);
752 for (i
= 0; i
< ARRAY_SIZE(cfg
->flash_timeout_max
); ++i
)
753 clamp_align(&cfg
->flash_timeout_max
[i
], FLASH_TIMEOUT_MIN
,
754 FLASH_TIMEOUT_MAX
, FLASH_TIMEOUT_STEP
);
756 clamp_align(&cfg
->boost_vout
, FLASH_VOUT_MIN
, FLASH_VOUT_MAX
,
760 clamp_align(&cfg
->low_vsys
, MAX_FLASH1_VSYS_MIN
,
761 MAX_FLASH1_VSYS_MAX
, MAX_FLASH1_VSYS_STEP
);
764 static int max77693_led_get_configuration(struct max77693_led_device
*led
,
765 struct max77693_led_config_data
*cfg
,
766 struct device_node
**sub_nodes
)
770 ret
= max77693_led_parse_dt(led
, cfg
, sub_nodes
);
774 max77693_led_validate_configuration(led
, cfg
);
776 memcpy(led
->iout_torch_max
, cfg
->iout_torch_max
,
777 sizeof(led
->iout_torch_max
));
778 memcpy(led
->iout_flash_max
, cfg
->iout_flash_max
,
779 sizeof(led
->iout_flash_max
));
784 static const struct led_flash_ops flash_ops
= {
785 .flash_brightness_set
= max77693_led_flash_brightness_set
,
786 .strobe_set
= max77693_led_flash_strobe_set
,
787 .strobe_get
= max77693_led_flash_strobe_get
,
788 .timeout_set
= max77693_led_flash_timeout_set
,
789 .fault_get
= max77693_led_flash_fault_get
,
792 static void max77693_init_flash_settings(struct max77693_sub_led
*sub_led
,
793 struct max77693_led_config_data
*led_cfg
)
795 struct led_classdev_flash
*fled_cdev
= &sub_led
->fled_cdev
;
796 struct max77693_led_device
*led
= sub_led_to_led(sub_led
);
797 int fled_id
= sub_led
->fled_id
;
798 struct led_flash_setting
*setting
;
800 /* Init flash intensity setting */
801 setting
= &fled_cdev
->brightness
;
802 setting
->min
= FLASH_IOUT_MIN
;
803 setting
->max
= led
->iout_joint
?
804 led_cfg
->iout_flash_max
[FLED1
] +
805 led_cfg
->iout_flash_max
[FLED2
] :
806 led_cfg
->iout_flash_max
[fled_id
];
807 setting
->step
= FLASH_IOUT_STEP
;
808 setting
->val
= setting
->max
;
810 /* Init flash timeout setting */
811 setting
= &fled_cdev
->timeout
;
812 setting
->min
= FLASH_TIMEOUT_MIN
;
813 setting
->max
= led_cfg
->flash_timeout_max
[fled_id
];
814 setting
->step
= FLASH_TIMEOUT_STEP
;
815 setting
->val
= setting
->max
;
818 #if IS_ENABLED(CONFIG_V4L2_FLASH_LED_CLASS)
820 static int max77693_led_external_strobe_set(
821 struct v4l2_flash
*v4l2_flash
,
824 struct max77693_sub_led
*sub_led
=
825 flcdev_to_sub_led(v4l2_flash
->fled_cdev
);
826 struct max77693_led_device
*led
= sub_led_to_led(sub_led
);
827 int fled_id
= sub_led
->fled_id
;
830 mutex_lock(&led
->lock
);
833 ret
= max77693_add_mode(led
, MODE_FLASH_EXTERNAL(fled_id
));
835 ret
= max77693_clear_mode(led
, MODE_FLASH_EXTERNAL(fled_id
));
837 mutex_unlock(&led
->lock
);
842 static void max77693_init_v4l2_flash_config(struct max77693_sub_led
*sub_led
,
843 struct max77693_led_config_data
*led_cfg
,
844 struct v4l2_flash_config
*v4l2_sd_cfg
)
846 struct max77693_led_device
*led
= sub_led_to_led(sub_led
);
847 struct device
*dev
= &led
->pdev
->dev
;
848 struct max77693_dev
*iodev
= dev_get_drvdata(dev
->parent
);
849 struct i2c_client
*i2c
= iodev
->i2c
;
850 struct led_flash_setting
*s
;
852 snprintf(v4l2_sd_cfg
->dev_name
, sizeof(v4l2_sd_cfg
->dev_name
),
853 "%s %d-%04x", sub_led
->fled_cdev
.led_cdev
.name
,
854 i2c_adapter_id(i2c
->adapter
), i2c
->addr
);
856 s
= &v4l2_sd_cfg
->intensity
;
857 s
->min
= TORCH_IOUT_MIN
;
858 s
->max
= sub_led
->fled_cdev
.led_cdev
.max_brightness
* TORCH_IOUT_STEP
;
859 s
->step
= TORCH_IOUT_STEP
;
862 /* Init flash faults config */
863 v4l2_sd_cfg
->flash_faults
= LED_FAULT_OVER_VOLTAGE
|
864 LED_FAULT_SHORT_CIRCUIT
|
865 LED_FAULT_OVER_CURRENT
;
867 v4l2_sd_cfg
->has_external_strobe
= true;
870 static const struct v4l2_flash_ops v4l2_flash_ops
= {
871 .external_strobe_set
= max77693_led_external_strobe_set
,
874 static inline void max77693_init_v4l2_flash_config(
875 struct max77693_sub_led
*sub_led
,
876 struct max77693_led_config_data
*led_cfg
,
877 struct v4l2_flash_config
*v4l2_sd_cfg
)
880 static const struct v4l2_flash_ops v4l2_flash_ops
;
883 static void max77693_init_fled_cdev(struct max77693_sub_led
*sub_led
,
884 struct max77693_led_config_data
*led_cfg
)
886 struct max77693_led_device
*led
= sub_led_to_led(sub_led
);
887 int fled_id
= sub_led
->fled_id
;
888 struct led_classdev_flash
*fled_cdev
;
889 struct led_classdev
*led_cdev
;
891 /* Initialize LED Flash class device */
892 fled_cdev
= &sub_led
->fled_cdev
;
893 fled_cdev
->ops
= &flash_ops
;
894 led_cdev
= &fled_cdev
->led_cdev
;
896 led_cdev
->name
= led_cfg
->label
[fled_id
];
898 led_cdev
->brightness_set_blocking
= max77693_led_brightness_set
;
899 led_cdev
->max_brightness
= (led
->iout_joint
?
900 led_cfg
->iout_torch_max
[FLED1
] +
901 led_cfg
->iout_torch_max
[FLED2
] :
902 led_cfg
->iout_torch_max
[fled_id
]) /
904 led_cdev
->flags
|= LED_DEV_CAP_FLASH
;
906 max77693_init_flash_settings(sub_led
, led_cfg
);
908 /* Init flash timeout cache */
909 sub_led
->flash_timeout
= fled_cdev
->timeout
.val
;
912 static int max77693_register_led(struct max77693_sub_led
*sub_led
,
913 struct max77693_led_config_data
*led_cfg
,
914 struct device_node
*sub_node
)
916 struct max77693_led_device
*led
= sub_led_to_led(sub_led
);
917 struct led_classdev_flash
*fled_cdev
= &sub_led
->fled_cdev
;
918 struct device
*dev
= &led
->pdev
->dev
;
919 struct v4l2_flash_config v4l2_sd_cfg
= {};
922 /* Register in the LED subsystem */
923 ret
= led_classdev_flash_register(dev
, fled_cdev
);
927 max77693_init_v4l2_flash_config(sub_led
, led_cfg
, &v4l2_sd_cfg
);
929 /* Register in the V4L2 subsystem. */
930 sub_led
->v4l2_flash
= v4l2_flash_init(dev
, of_fwnode_handle(sub_node
),
931 fled_cdev
, &v4l2_flash_ops
,
933 if (IS_ERR(sub_led
->v4l2_flash
)) {
934 ret
= PTR_ERR(sub_led
->v4l2_flash
);
935 goto err_v4l2_flash_init
;
941 led_classdev_flash_unregister(fled_cdev
);
945 static int max77693_led_probe(struct platform_device
*pdev
)
947 struct device
*dev
= &pdev
->dev
;
948 struct max77693_dev
*iodev
= dev_get_drvdata(dev
->parent
);
949 struct max77693_led_device
*led
;
950 struct max77693_sub_led
*sub_leds
;
951 struct device_node
*sub_nodes
[2] = {};
952 struct max77693_led_config_data led_cfg
= {};
953 int init_fled_cdev
[2], i
, ret
;
955 led
= devm_kzalloc(dev
, sizeof(*led
), GFP_KERNEL
);
960 led
->regmap
= iodev
->regmap
;
961 led
->allowed_modes
= MODE_FLASH_MASK
;
962 sub_leds
= led
->sub_leds
;
964 platform_set_drvdata(pdev
, led
);
965 ret
= max77693_led_get_configuration(led
, &led_cfg
, sub_nodes
);
969 ret
= max77693_setup(led
, &led_cfg
);
973 mutex_init(&led
->lock
);
975 init_fled_cdev
[FLED1
] =
976 led
->iout_joint
|| max77693_fled_used(led
, FLED1
);
977 init_fled_cdev
[FLED2
] =
978 !led
->iout_joint
&& max77693_fled_used(led
, FLED2
);
980 for (i
= FLED1
; i
<= FLED2
; ++i
) {
981 if (!init_fled_cdev
[i
])
984 /* Initialize LED Flash class device */
985 max77693_init_fled_cdev(&sub_leds
[i
], &led_cfg
);
988 * Register LED Flash class device and corresponding
991 ret
= max77693_register_led(&sub_leds
[i
], &led_cfg
,
995 * At this moment FLED1 might have been already
996 * registered and it needs to be released.
999 goto err_register_led2
;
1001 goto err_register_led1
;
1008 /* It is possible than only FLED2 was to be registered */
1009 if (!init_fled_cdev
[FLED1
])
1010 goto err_register_led1
;
1011 v4l2_flash_release(sub_leds
[FLED1
].v4l2_flash
);
1012 led_classdev_flash_unregister(&sub_leds
[FLED1
].fled_cdev
);
1014 mutex_destroy(&led
->lock
);
1019 static int max77693_led_remove(struct platform_device
*pdev
)
1021 struct max77693_led_device
*led
= platform_get_drvdata(pdev
);
1022 struct max77693_sub_led
*sub_leds
= led
->sub_leds
;
1024 if (led
->iout_joint
|| max77693_fled_used(led
, FLED1
)) {
1025 v4l2_flash_release(sub_leds
[FLED1
].v4l2_flash
);
1026 led_classdev_flash_unregister(&sub_leds
[FLED1
].fled_cdev
);
1029 if (!led
->iout_joint
&& max77693_fled_used(led
, FLED2
)) {
1030 v4l2_flash_release(sub_leds
[FLED2
].v4l2_flash
);
1031 led_classdev_flash_unregister(&sub_leds
[FLED2
].fled_cdev
);
1034 mutex_destroy(&led
->lock
);
1039 static const struct of_device_id max77693_led_dt_match
[] = {
1040 { .compatible
= "maxim,max77693-led" },
1043 MODULE_DEVICE_TABLE(of
, max77693_led_dt_match
);
1045 static struct platform_driver max77693_led_driver
= {
1046 .probe
= max77693_led_probe
,
1047 .remove
= max77693_led_remove
,
1049 .name
= "max77693-led",
1050 .of_match_table
= max77693_led_dt_match
,
1054 module_platform_driver(max77693_led_driver
);
1056 MODULE_AUTHOR("Jacek Anaszewski <j.anaszewski@samsung.com>");
1057 MODULE_AUTHOR("Andrzej Hajda <a.hajda@samsung.com>");
1058 MODULE_DESCRIPTION("Maxim MAX77693 led flash driver");
1059 MODULE_LICENSE("GPL v2");