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(dev
);
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_scoped(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");
630 led
->fled_mask
= FLED1_IOUT
| FLED2_IOUT
;
631 } else if (led_sources
[0] == FLED1
) {
633 led
->fled_mask
|= FLED1_IOUT
;
634 } else if (led_sources
[0] == FLED2
) {
636 led
->fled_mask
|= FLED2_IOUT
;
639 "Wrong led-sources DT property value.\n");
643 if (sub_nodes
[fled_id
]) {
645 "Conflicting \"led-sources\" DT properties\n");
649 sub_nodes
[fled_id
] = of_node_get(child_node
);
650 sub_leds
[fled_id
].fled_id
= fled_id
;
652 cfg
->label
[fled_id
] =
653 of_get_property(child_node
, "label", NULL
) ? :
656 ret
= of_property_read_u32(child_node
, "led-max-microamp",
657 &cfg
->iout_torch_max
[fled_id
]);
659 cfg
->iout_torch_max
[fled_id
] = TORCH_IOUT_MIN
;
660 dev_warn(dev
, "led-max-microamp DT property missing\n");
663 ret
= of_property_read_u32(child_node
, "flash-max-microamp",
664 &cfg
->iout_flash_max
[fled_id
]);
666 cfg
->iout_flash_max
[fled_id
] = FLASH_IOUT_MIN
;
668 "flash-max-microamp DT property missing\n");
671 ret
= of_property_read_u32(child_node
, "flash-max-timeout-us",
672 &cfg
->flash_timeout_max
[fled_id
]);
674 cfg
->flash_timeout_max
[fled_id
] = FLASH_TIMEOUT_MIN
;
676 "flash-max-timeout-us DT property missing\n");
679 if (++cfg
->num_leds
== 2 ||
680 (max77693_fled_used(led
, FLED1
) &&
681 max77693_fled_used(led
, FLED2
)))
685 if (cfg
->num_leds
== 0) {
686 dev_err(dev
, "No DT child node found for connected LED(s).\n");
693 static void clamp_align(u32
*v
, u32 min
, u32 max
, u32 step
)
695 *v
= clamp_val(*v
, min
, max
);
697 *v
= (*v
- min
) / step
* step
+ min
;
700 static void max77693_align_iout_current(struct max77693_led_device
*led
,
701 u32
*iout
, u32 min
, u32 max
, u32 step
)
705 if (led
->iout_joint
) {
706 if (iout
[FLED1
] > min
) {
708 iout
[FLED2
] = iout
[FLED1
];
716 for (i
= FLED1
; i
<= FLED2
; ++i
)
717 if (max77693_fled_used(led
, i
))
718 clamp_align(&iout
[i
], min
, max
, step
);
723 static void max77693_led_validate_configuration(struct max77693_led_device
*led
,
724 struct max77693_led_config_data
*cfg
)
726 u32 flash_iout_max
= cfg
->boost_mode
? FLASH_IOUT_MAX_2LEDS
:
730 if (cfg
->num_leds
== 1 &&
731 max77693_fled_used(led
, FLED1
) && max77693_fled_used(led
, FLED2
))
732 led
->iout_joint
= true;
734 cfg
->boost_mode
= clamp_val(cfg
->boost_mode
, MAX77693_LED_BOOST_NONE
,
735 MAX77693_LED_BOOST_FIXED
);
737 /* Boost must be enabled if both current outputs are used */
738 if ((cfg
->boost_mode
== MAX77693_LED_BOOST_NONE
) && led
->iout_joint
)
739 cfg
->boost_mode
= MAX77693_LED_BOOST_FIXED
;
741 max77693_align_iout_current(led
, cfg
->iout_torch_max
,
742 TORCH_IOUT_MIN
, TORCH_IOUT_MAX
, TORCH_IOUT_STEP
);
744 max77693_align_iout_current(led
, cfg
->iout_flash_max
,
745 FLASH_IOUT_MIN
, flash_iout_max
, FLASH_IOUT_STEP
);
747 for (i
= 0; i
< ARRAY_SIZE(cfg
->flash_timeout_max
); ++i
)
748 clamp_align(&cfg
->flash_timeout_max
[i
], FLASH_TIMEOUT_MIN
,
749 FLASH_TIMEOUT_MAX
, FLASH_TIMEOUT_STEP
);
751 clamp_align(&cfg
->boost_vout
, FLASH_VOUT_MIN
, FLASH_VOUT_MAX
,
755 clamp_align(&cfg
->low_vsys
, MAX_FLASH1_VSYS_MIN
,
756 MAX_FLASH1_VSYS_MAX
, MAX_FLASH1_VSYS_STEP
);
759 static int max77693_led_get_configuration(struct max77693_led_device
*led
,
760 struct max77693_led_config_data
*cfg
,
761 struct device_node
**sub_nodes
)
765 ret
= max77693_led_parse_dt(led
, cfg
, sub_nodes
);
769 max77693_led_validate_configuration(led
, cfg
);
771 memcpy(led
->iout_torch_max
, cfg
->iout_torch_max
,
772 sizeof(led
->iout_torch_max
));
773 memcpy(led
->iout_flash_max
, cfg
->iout_flash_max
,
774 sizeof(led
->iout_flash_max
));
779 static const struct led_flash_ops flash_ops
= {
780 .flash_brightness_set
= max77693_led_flash_brightness_set
,
781 .strobe_set
= max77693_led_flash_strobe_set
,
782 .strobe_get
= max77693_led_flash_strobe_get
,
783 .timeout_set
= max77693_led_flash_timeout_set
,
784 .fault_get
= max77693_led_flash_fault_get
,
787 static void max77693_init_flash_settings(struct max77693_sub_led
*sub_led
,
788 struct max77693_led_config_data
*led_cfg
)
790 struct led_classdev_flash
*fled_cdev
= &sub_led
->fled_cdev
;
791 struct max77693_led_device
*led
= sub_led_to_led(sub_led
);
792 int fled_id
= sub_led
->fled_id
;
793 struct led_flash_setting
*setting
;
795 /* Init flash intensity setting */
796 setting
= &fled_cdev
->brightness
;
797 setting
->min
= FLASH_IOUT_MIN
;
798 setting
->max
= led
->iout_joint
?
799 led_cfg
->iout_flash_max
[FLED1
] +
800 led_cfg
->iout_flash_max
[FLED2
] :
801 led_cfg
->iout_flash_max
[fled_id
];
802 setting
->step
= FLASH_IOUT_STEP
;
803 setting
->val
= setting
->max
;
805 /* Init flash timeout setting */
806 setting
= &fled_cdev
->timeout
;
807 setting
->min
= FLASH_TIMEOUT_MIN
;
808 setting
->max
= led_cfg
->flash_timeout_max
[fled_id
];
809 setting
->step
= FLASH_TIMEOUT_STEP
;
810 setting
->val
= setting
->max
;
813 #if IS_ENABLED(CONFIG_V4L2_FLASH_LED_CLASS)
815 static int max77693_led_external_strobe_set(
816 struct v4l2_flash
*v4l2_flash
,
819 struct max77693_sub_led
*sub_led
=
820 flcdev_to_sub_led(v4l2_flash
->fled_cdev
);
821 struct max77693_led_device
*led
= sub_led_to_led(sub_led
);
822 int fled_id
= sub_led
->fled_id
;
825 mutex_lock(&led
->lock
);
828 ret
= max77693_add_mode(led
, MODE_FLASH_EXTERNAL(fled_id
));
830 ret
= max77693_clear_mode(led
, MODE_FLASH_EXTERNAL(fled_id
));
832 mutex_unlock(&led
->lock
);
837 static void max77693_init_v4l2_flash_config(struct max77693_sub_led
*sub_led
,
838 struct max77693_led_config_data
*led_cfg
,
839 struct v4l2_flash_config
*v4l2_sd_cfg
)
841 struct max77693_led_device
*led
= sub_led_to_led(sub_led
);
842 struct device
*dev
= &led
->pdev
->dev
;
843 struct max77693_dev
*iodev
= dev_get_drvdata(dev
->parent
);
844 struct i2c_client
*i2c
= iodev
->i2c
;
845 struct led_flash_setting
*s
;
847 snprintf(v4l2_sd_cfg
->dev_name
, sizeof(v4l2_sd_cfg
->dev_name
),
848 "%s %d-%04x", sub_led
->fled_cdev
.led_cdev
.name
,
849 i2c_adapter_id(i2c
->adapter
), i2c
->addr
);
851 s
= &v4l2_sd_cfg
->intensity
;
852 s
->min
= TORCH_IOUT_MIN
;
853 s
->max
= sub_led
->fled_cdev
.led_cdev
.max_brightness
* TORCH_IOUT_STEP
;
854 s
->step
= TORCH_IOUT_STEP
;
857 /* Init flash faults config */
858 v4l2_sd_cfg
->flash_faults
= LED_FAULT_OVER_VOLTAGE
|
859 LED_FAULT_SHORT_CIRCUIT
|
860 LED_FAULT_OVER_CURRENT
;
862 v4l2_sd_cfg
->has_external_strobe
= true;
865 static const struct v4l2_flash_ops v4l2_flash_ops
= {
866 .external_strobe_set
= max77693_led_external_strobe_set
,
869 static inline void max77693_init_v4l2_flash_config(
870 struct max77693_sub_led
*sub_led
,
871 struct max77693_led_config_data
*led_cfg
,
872 struct v4l2_flash_config
*v4l2_sd_cfg
)
875 static const struct v4l2_flash_ops v4l2_flash_ops
;
878 static void max77693_init_fled_cdev(struct max77693_sub_led
*sub_led
,
879 struct max77693_led_config_data
*led_cfg
)
881 struct max77693_led_device
*led
= sub_led_to_led(sub_led
);
882 int fled_id
= sub_led
->fled_id
;
883 struct led_classdev_flash
*fled_cdev
;
884 struct led_classdev
*led_cdev
;
886 /* Initialize LED Flash class device */
887 fled_cdev
= &sub_led
->fled_cdev
;
888 fled_cdev
->ops
= &flash_ops
;
889 led_cdev
= &fled_cdev
->led_cdev
;
891 led_cdev
->name
= led_cfg
->label
[fled_id
];
893 led_cdev
->brightness_set_blocking
= max77693_led_brightness_set
;
894 led_cdev
->max_brightness
= (led
->iout_joint
?
895 led_cfg
->iout_torch_max
[FLED1
] +
896 led_cfg
->iout_torch_max
[FLED2
] :
897 led_cfg
->iout_torch_max
[fled_id
]) /
899 led_cdev
->flags
|= LED_DEV_CAP_FLASH
;
901 max77693_init_flash_settings(sub_led
, led_cfg
);
903 /* Init flash timeout cache */
904 sub_led
->flash_timeout
= fled_cdev
->timeout
.val
;
907 static int max77693_register_led(struct max77693_sub_led
*sub_led
,
908 struct max77693_led_config_data
*led_cfg
,
909 struct device_node
*sub_node
)
911 struct max77693_led_device
*led
= sub_led_to_led(sub_led
);
912 struct led_classdev_flash
*fled_cdev
= &sub_led
->fled_cdev
;
913 struct device
*dev
= &led
->pdev
->dev
;
914 struct v4l2_flash_config v4l2_sd_cfg
= {};
917 /* Register in the LED subsystem */
918 ret
= led_classdev_flash_register(dev
, fled_cdev
);
922 max77693_init_v4l2_flash_config(sub_led
, led_cfg
, &v4l2_sd_cfg
);
924 /* Register in the V4L2 subsystem. */
925 sub_led
->v4l2_flash
= v4l2_flash_init(dev
, of_fwnode_handle(sub_node
),
926 fled_cdev
, &v4l2_flash_ops
,
928 if (IS_ERR(sub_led
->v4l2_flash
)) {
929 ret
= PTR_ERR(sub_led
->v4l2_flash
);
930 goto err_v4l2_flash_init
;
936 led_classdev_flash_unregister(fled_cdev
);
940 static int max77693_led_probe(struct platform_device
*pdev
)
942 struct device
*dev
= &pdev
->dev
;
943 struct max77693_dev
*iodev
= dev_get_drvdata(dev
->parent
);
944 struct max77693_led_device
*led
;
945 struct max77693_sub_led
*sub_leds
;
946 struct device_node
*sub_nodes
[2] = {};
947 struct max77693_led_config_data led_cfg
= {};
948 int init_fled_cdev
[2], i
, ret
;
950 led
= devm_kzalloc(dev
, sizeof(*led
), GFP_KERNEL
);
955 led
->regmap
= iodev
->regmap
;
956 led
->allowed_modes
= MODE_FLASH_MASK
;
957 sub_leds
= led
->sub_leds
;
959 platform_set_drvdata(pdev
, led
);
960 ret
= max77693_led_get_configuration(led
, &led_cfg
, sub_nodes
);
964 ret
= max77693_setup(led
, &led_cfg
);
968 mutex_init(&led
->lock
);
970 init_fled_cdev
[FLED1
] =
971 led
->iout_joint
|| max77693_fled_used(led
, FLED1
);
972 init_fled_cdev
[FLED2
] =
973 !led
->iout_joint
&& max77693_fled_used(led
, FLED2
);
975 for (i
= FLED1
; i
<= FLED2
; ++i
) {
976 if (!init_fled_cdev
[i
])
979 /* Initialize LED Flash class device */
980 max77693_init_fled_cdev(&sub_leds
[i
], &led_cfg
);
983 * Register LED Flash class device and corresponding
986 ret
= max77693_register_led(&sub_leds
[i
], &led_cfg
,
990 * At this moment FLED1 might have been already
991 * registered and it needs to be released.
994 goto err_register_led2
;
996 goto err_register_led1
;
998 of_node_put(sub_nodes
[i
]);
1005 /* It is possible than only FLED2 was to be registered */
1006 if (!init_fled_cdev
[FLED1
])
1007 goto err_register_led1
;
1008 v4l2_flash_release(sub_leds
[FLED1
].v4l2_flash
);
1009 led_classdev_flash_unregister(&sub_leds
[FLED1
].fled_cdev
);
1011 mutex_destroy(&led
->lock
);
1014 for (i
= FLED1
; i
<= FLED2
; i
++)
1015 of_node_put(sub_nodes
[i
]);
1019 static void 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
);
1037 static const struct of_device_id max77693_led_dt_match
[] = {
1038 { .compatible
= "maxim,max77693-led" },
1041 MODULE_DEVICE_TABLE(of
, max77693_led_dt_match
);
1043 static struct platform_driver max77693_led_driver
= {
1044 .probe
= max77693_led_probe
,
1045 .remove_new
= max77693_led_remove
,
1047 .name
= "max77693-led",
1048 .of_match_table
= max77693_led_dt_match
,
1052 module_platform_driver(max77693_led_driver
);
1054 MODULE_AUTHOR("Jacek Anaszewski <j.anaszewski@samsung.com>");
1055 MODULE_AUTHOR("Andrzej Hajda <a.hajda@samsung.com>");
1056 MODULE_DESCRIPTION("Maxim MAX77693 led flash driver");
1057 MODULE_LICENSE("GPL v2");