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");
654 sub_nodes
[fled_id
] = child_node
;
655 sub_leds
[fled_id
].fled_id
= fled_id
;
657 cfg
->label
[fled_id
] =
658 of_get_property(child_node
, "label", NULL
) ? :
661 ret
= of_property_read_u32(child_node
, "led-max-microamp",
662 &cfg
->iout_torch_max
[fled_id
]);
664 cfg
->iout_torch_max
[fled_id
] = TORCH_IOUT_MIN
;
665 dev_warn(dev
, "led-max-microamp DT property missing\n");
668 ret
= of_property_read_u32(child_node
, "flash-max-microamp",
669 &cfg
->iout_flash_max
[fled_id
]);
671 cfg
->iout_flash_max
[fled_id
] = FLASH_IOUT_MIN
;
673 "flash-max-microamp DT property missing\n");
676 ret
= of_property_read_u32(child_node
, "flash-max-timeout-us",
677 &cfg
->flash_timeout_max
[fled_id
]);
679 cfg
->flash_timeout_max
[fled_id
] = FLASH_TIMEOUT_MIN
;
681 "flash-max-timeout-us DT property missing\n");
684 if (++cfg
->num_leds
== 2 ||
685 (max77693_fled_used(led
, FLED1
) &&
686 max77693_fled_used(led
, FLED2
))) {
687 of_node_put(child_node
);
692 if (cfg
->num_leds
== 0) {
693 dev_err(dev
, "No DT child node found for connected LED(s).\n");
700 static void clamp_align(u32
*v
, u32 min
, u32 max
, u32 step
)
702 *v
= clamp_val(*v
, min
, max
);
704 *v
= (*v
- min
) / step
* step
+ min
;
707 static void max77693_align_iout_current(struct max77693_led_device
*led
,
708 u32
*iout
, u32 min
, u32 max
, u32 step
)
712 if (led
->iout_joint
) {
713 if (iout
[FLED1
] > min
) {
715 iout
[FLED2
] = iout
[FLED1
];
723 for (i
= FLED1
; i
<= FLED2
; ++i
)
724 if (max77693_fled_used(led
, i
))
725 clamp_align(&iout
[i
], min
, max
, step
);
730 static void max77693_led_validate_configuration(struct max77693_led_device
*led
,
731 struct max77693_led_config_data
*cfg
)
733 u32 flash_iout_max
= cfg
->boost_mode
? FLASH_IOUT_MAX_2LEDS
:
737 if (cfg
->num_leds
== 1 &&
738 max77693_fled_used(led
, FLED1
) && max77693_fled_used(led
, FLED2
))
739 led
->iout_joint
= true;
741 cfg
->boost_mode
= clamp_val(cfg
->boost_mode
, MAX77693_LED_BOOST_NONE
,
742 MAX77693_LED_BOOST_FIXED
);
744 /* Boost must be enabled if both current outputs are used */
745 if ((cfg
->boost_mode
== MAX77693_LED_BOOST_NONE
) && led
->iout_joint
)
746 cfg
->boost_mode
= MAX77693_LED_BOOST_FIXED
;
748 max77693_align_iout_current(led
, cfg
->iout_torch_max
,
749 TORCH_IOUT_MIN
, TORCH_IOUT_MAX
, TORCH_IOUT_STEP
);
751 max77693_align_iout_current(led
, cfg
->iout_flash_max
,
752 FLASH_IOUT_MIN
, flash_iout_max
, FLASH_IOUT_STEP
);
754 for (i
= 0; i
< ARRAY_SIZE(cfg
->flash_timeout_max
); ++i
)
755 clamp_align(&cfg
->flash_timeout_max
[i
], FLASH_TIMEOUT_MIN
,
756 FLASH_TIMEOUT_MAX
, FLASH_TIMEOUT_STEP
);
758 clamp_align(&cfg
->boost_vout
, FLASH_VOUT_MIN
, FLASH_VOUT_MAX
,
762 clamp_align(&cfg
->low_vsys
, MAX_FLASH1_VSYS_MIN
,
763 MAX_FLASH1_VSYS_MAX
, MAX_FLASH1_VSYS_STEP
);
766 static int max77693_led_get_configuration(struct max77693_led_device
*led
,
767 struct max77693_led_config_data
*cfg
,
768 struct device_node
**sub_nodes
)
772 ret
= max77693_led_parse_dt(led
, cfg
, sub_nodes
);
776 max77693_led_validate_configuration(led
, cfg
);
778 memcpy(led
->iout_torch_max
, cfg
->iout_torch_max
,
779 sizeof(led
->iout_torch_max
));
780 memcpy(led
->iout_flash_max
, cfg
->iout_flash_max
,
781 sizeof(led
->iout_flash_max
));
786 static const struct led_flash_ops flash_ops
= {
787 .flash_brightness_set
= max77693_led_flash_brightness_set
,
788 .strobe_set
= max77693_led_flash_strobe_set
,
789 .strobe_get
= max77693_led_flash_strobe_get
,
790 .timeout_set
= max77693_led_flash_timeout_set
,
791 .fault_get
= max77693_led_flash_fault_get
,
794 static void max77693_init_flash_settings(struct max77693_sub_led
*sub_led
,
795 struct max77693_led_config_data
*led_cfg
)
797 struct led_classdev_flash
*fled_cdev
= &sub_led
->fled_cdev
;
798 struct max77693_led_device
*led
= sub_led_to_led(sub_led
);
799 int fled_id
= sub_led
->fled_id
;
800 struct led_flash_setting
*setting
;
802 /* Init flash intensity setting */
803 setting
= &fled_cdev
->brightness
;
804 setting
->min
= FLASH_IOUT_MIN
;
805 setting
->max
= led
->iout_joint
?
806 led_cfg
->iout_flash_max
[FLED1
] +
807 led_cfg
->iout_flash_max
[FLED2
] :
808 led_cfg
->iout_flash_max
[fled_id
];
809 setting
->step
= FLASH_IOUT_STEP
;
810 setting
->val
= setting
->max
;
812 /* Init flash timeout setting */
813 setting
= &fled_cdev
->timeout
;
814 setting
->min
= FLASH_TIMEOUT_MIN
;
815 setting
->max
= led_cfg
->flash_timeout_max
[fled_id
];
816 setting
->step
= FLASH_TIMEOUT_STEP
;
817 setting
->val
= setting
->max
;
820 #if IS_ENABLED(CONFIG_V4L2_FLASH_LED_CLASS)
822 static int max77693_led_external_strobe_set(
823 struct v4l2_flash
*v4l2_flash
,
826 struct max77693_sub_led
*sub_led
=
827 flcdev_to_sub_led(v4l2_flash
->fled_cdev
);
828 struct max77693_led_device
*led
= sub_led_to_led(sub_led
);
829 int fled_id
= sub_led
->fled_id
;
832 mutex_lock(&led
->lock
);
835 ret
= max77693_add_mode(led
, MODE_FLASH_EXTERNAL(fled_id
));
837 ret
= max77693_clear_mode(led
, MODE_FLASH_EXTERNAL(fled_id
));
839 mutex_unlock(&led
->lock
);
844 static void max77693_init_v4l2_flash_config(struct max77693_sub_led
*sub_led
,
845 struct max77693_led_config_data
*led_cfg
,
846 struct v4l2_flash_config
*v4l2_sd_cfg
)
848 struct max77693_led_device
*led
= sub_led_to_led(sub_led
);
849 struct device
*dev
= &led
->pdev
->dev
;
850 struct max77693_dev
*iodev
= dev_get_drvdata(dev
->parent
);
851 struct i2c_client
*i2c
= iodev
->i2c
;
852 struct led_flash_setting
*s
;
854 snprintf(v4l2_sd_cfg
->dev_name
, sizeof(v4l2_sd_cfg
->dev_name
),
855 "%s %d-%04x", sub_led
->fled_cdev
.led_cdev
.name
,
856 i2c_adapter_id(i2c
->adapter
), i2c
->addr
);
858 s
= &v4l2_sd_cfg
->torch_intensity
;
859 s
->min
= TORCH_IOUT_MIN
;
860 s
->max
= sub_led
->fled_cdev
.led_cdev
.max_brightness
* TORCH_IOUT_STEP
;
861 s
->step
= TORCH_IOUT_STEP
;
864 /* Init flash faults config */
865 v4l2_sd_cfg
->flash_faults
= LED_FAULT_OVER_VOLTAGE
|
866 LED_FAULT_SHORT_CIRCUIT
|
867 LED_FAULT_OVER_CURRENT
;
869 v4l2_sd_cfg
->has_external_strobe
= true;
872 static const struct v4l2_flash_ops v4l2_flash_ops
= {
873 .external_strobe_set
= max77693_led_external_strobe_set
,
876 static inline void max77693_init_v4l2_flash_config(
877 struct max77693_sub_led
*sub_led
,
878 struct max77693_led_config_data
*led_cfg
,
879 struct v4l2_flash_config
*v4l2_sd_cfg
)
882 static const struct v4l2_flash_ops v4l2_flash_ops
;
885 static void max77693_init_fled_cdev(struct max77693_sub_led
*sub_led
,
886 struct max77693_led_config_data
*led_cfg
)
888 struct max77693_led_device
*led
= sub_led_to_led(sub_led
);
889 int fled_id
= sub_led
->fled_id
;
890 struct led_classdev_flash
*fled_cdev
;
891 struct led_classdev
*led_cdev
;
893 /* Initialize LED Flash class device */
894 fled_cdev
= &sub_led
->fled_cdev
;
895 fled_cdev
->ops
= &flash_ops
;
896 led_cdev
= &fled_cdev
->led_cdev
;
898 led_cdev
->name
= led_cfg
->label
[fled_id
];
900 led_cdev
->brightness_set_blocking
= max77693_led_brightness_set
;
901 led_cdev
->max_brightness
= (led
->iout_joint
?
902 led_cfg
->iout_torch_max
[FLED1
] +
903 led_cfg
->iout_torch_max
[FLED2
] :
904 led_cfg
->iout_torch_max
[fled_id
]) /
906 led_cdev
->flags
|= LED_DEV_CAP_FLASH
;
908 max77693_init_flash_settings(sub_led
, led_cfg
);
910 /* Init flash timeout cache */
911 sub_led
->flash_timeout
= fled_cdev
->timeout
.val
;
914 static int max77693_register_led(struct max77693_sub_led
*sub_led
,
915 struct max77693_led_config_data
*led_cfg
,
916 struct device_node
*sub_node
)
918 struct max77693_led_device
*led
= sub_led_to_led(sub_led
);
919 struct led_classdev_flash
*fled_cdev
= &sub_led
->fled_cdev
;
920 struct device
*dev
= &led
->pdev
->dev
;
921 struct v4l2_flash_config v4l2_sd_cfg
= {};
924 /* Register in the LED subsystem */
925 ret
= led_classdev_flash_register(dev
, fled_cdev
);
929 max77693_init_v4l2_flash_config(sub_led
, led_cfg
, &v4l2_sd_cfg
);
931 /* Register in the V4L2 subsystem. */
932 sub_led
->v4l2_flash
= v4l2_flash_init(dev
, sub_node
, fled_cdev
, NULL
,
933 &v4l2_flash_ops
, &v4l2_sd_cfg
);
934 if (IS_ERR(sub_led
->v4l2_flash
)) {
935 ret
= PTR_ERR(sub_led
->v4l2_flash
);
936 goto err_v4l2_flash_init
;
942 led_classdev_flash_unregister(fled_cdev
);
946 static int max77693_led_probe(struct platform_device
*pdev
)
948 struct device
*dev
= &pdev
->dev
;
949 struct max77693_dev
*iodev
= dev_get_drvdata(dev
->parent
);
950 struct max77693_led_device
*led
;
951 struct max77693_sub_led
*sub_leds
;
952 struct device_node
*sub_nodes
[2] = {};
953 struct max77693_led_config_data led_cfg
= {};
954 int init_fled_cdev
[2], i
, ret
;
956 led
= devm_kzalloc(dev
, sizeof(*led
), GFP_KERNEL
);
961 led
->regmap
= iodev
->regmap
;
962 led
->allowed_modes
= MODE_FLASH_MASK
;
963 sub_leds
= led
->sub_leds
;
965 platform_set_drvdata(pdev
, led
);
966 ret
= max77693_led_get_configuration(led
, &led_cfg
, sub_nodes
);
970 ret
= max77693_setup(led
, &led_cfg
);
974 mutex_init(&led
->lock
);
976 init_fled_cdev
[FLED1
] =
977 led
->iout_joint
|| max77693_fled_used(led
, FLED1
);
978 init_fled_cdev
[FLED2
] =
979 !led
->iout_joint
&& max77693_fled_used(led
, FLED2
);
981 for (i
= FLED1
; i
<= FLED2
; ++i
) {
982 if (!init_fled_cdev
[i
])
985 /* Initialize LED Flash class device */
986 max77693_init_fled_cdev(&sub_leds
[i
], &led_cfg
);
989 * Register LED Flash class device and corresponding
992 ret
= max77693_register_led(&sub_leds
[i
], &led_cfg
,
996 * At this moment FLED1 might have been already
997 * registered and it needs to be released.
1000 goto err_register_led2
;
1002 goto err_register_led1
;
1009 /* It is possible than only FLED2 was to be registered */
1010 if (!init_fled_cdev
[FLED1
])
1011 goto err_register_led1
;
1012 v4l2_flash_release(sub_leds
[FLED1
].v4l2_flash
);
1013 led_classdev_flash_unregister(&sub_leds
[FLED1
].fled_cdev
);
1015 mutex_destroy(&led
->lock
);
1020 static int max77693_led_remove(struct platform_device
*pdev
)
1022 struct max77693_led_device
*led
= platform_get_drvdata(pdev
);
1023 struct max77693_sub_led
*sub_leds
= led
->sub_leds
;
1025 if (led
->iout_joint
|| max77693_fled_used(led
, FLED1
)) {
1026 v4l2_flash_release(sub_leds
[FLED1
].v4l2_flash
);
1027 led_classdev_flash_unregister(&sub_leds
[FLED1
].fled_cdev
);
1030 if (!led
->iout_joint
&& max77693_fled_used(led
, FLED2
)) {
1031 v4l2_flash_release(sub_leds
[FLED2
].v4l2_flash
);
1032 led_classdev_flash_unregister(&sub_leds
[FLED2
].fled_cdev
);
1035 mutex_destroy(&led
->lock
);
1040 static const struct of_device_id max77693_led_dt_match
[] = {
1041 { .compatible
= "maxim,max77693-led" },
1044 MODULE_DEVICE_TABLE(of
, max77693_led_dt_match
);
1046 static struct platform_driver max77693_led_driver
= {
1047 .probe
= max77693_led_probe
,
1048 .remove
= max77693_led_remove
,
1050 .name
= "max77693-led",
1051 .of_match_table
= max77693_led_dt_match
,
1055 module_platform_driver(max77693_led_driver
);
1057 MODULE_AUTHOR("Jacek Anaszewski <j.anaszewski@samsung.com>");
1058 MODULE_AUTHOR("Andrzej Hajda <a.hajda@samsung.com>");
1059 MODULE_DESCRIPTION("Maxim MAX77693 led flash driver");
1060 MODULE_LICENSE("GPL v2");