xref: /openbmc/linux/drivers/leds/flash/leds-max77693.c (revision 762f99f4f3cb41a775b5157dd761217beba65873)
1*eb5a4422SLinus Walleij // SPDX-License-Identifier: GPL-2.0-only
2*eb5a4422SLinus Walleij /*
3*eb5a4422SLinus Walleij  * LED Flash class driver for the flash cell of max77693 mfd.
4*eb5a4422SLinus Walleij  *
5*eb5a4422SLinus Walleij  *	Copyright (C) 2015, Samsung Electronics Co., Ltd.
6*eb5a4422SLinus Walleij  *
7*eb5a4422SLinus Walleij  *	Authors: Jacek Anaszewski <j.anaszewski@samsung.com>
8*eb5a4422SLinus Walleij  *		 Andrzej Hajda <a.hajda@samsung.com>
9*eb5a4422SLinus Walleij  */
10*eb5a4422SLinus Walleij 
11*eb5a4422SLinus Walleij #include <linux/led-class-flash.h>
12*eb5a4422SLinus Walleij #include <linux/mfd/max77693.h>
13*eb5a4422SLinus Walleij #include <linux/mfd/max77693-common.h>
14*eb5a4422SLinus Walleij #include <linux/mfd/max77693-private.h>
15*eb5a4422SLinus Walleij #include <linux/module.h>
16*eb5a4422SLinus Walleij #include <linux/mutex.h>
17*eb5a4422SLinus Walleij #include <linux/platform_device.h>
18*eb5a4422SLinus Walleij #include <linux/regmap.h>
19*eb5a4422SLinus Walleij #include <linux/slab.h>
20*eb5a4422SLinus Walleij #include <media/v4l2-flash-led-class.h>
21*eb5a4422SLinus Walleij 
22*eb5a4422SLinus Walleij #define MODE_OFF		0
23*eb5a4422SLinus Walleij #define MODE_FLASH(a)		(1 << (a))
24*eb5a4422SLinus Walleij #define MODE_TORCH(a)		(1 << (2 + (a)))
25*eb5a4422SLinus Walleij #define MODE_FLASH_EXTERNAL(a)	(1 << (4 + (a)))
26*eb5a4422SLinus Walleij 
27*eb5a4422SLinus Walleij #define MODE_FLASH_MASK		(MODE_FLASH(FLED1) | MODE_FLASH(FLED2) | \
28*eb5a4422SLinus Walleij 				 MODE_FLASH_EXTERNAL(FLED1) | \
29*eb5a4422SLinus Walleij 				 MODE_FLASH_EXTERNAL(FLED2))
30*eb5a4422SLinus Walleij #define MODE_TORCH_MASK		(MODE_TORCH(FLED1) | MODE_TORCH(FLED2))
31*eb5a4422SLinus Walleij 
32*eb5a4422SLinus Walleij #define FLED1_IOUT		(1 << 0)
33*eb5a4422SLinus Walleij #define FLED2_IOUT		(1 << 1)
34*eb5a4422SLinus Walleij 
35*eb5a4422SLinus Walleij enum max77693_fled {
36*eb5a4422SLinus Walleij 	FLED1,
37*eb5a4422SLinus Walleij 	FLED2,
38*eb5a4422SLinus Walleij };
39*eb5a4422SLinus Walleij 
40*eb5a4422SLinus Walleij enum max77693_led_mode {
41*eb5a4422SLinus Walleij 	FLASH,
42*eb5a4422SLinus Walleij 	TORCH,
43*eb5a4422SLinus Walleij };
44*eb5a4422SLinus Walleij 
45*eb5a4422SLinus Walleij struct max77693_led_config_data {
46*eb5a4422SLinus Walleij 	const char *label[2];
47*eb5a4422SLinus Walleij 	u32 iout_torch_max[2];
48*eb5a4422SLinus Walleij 	u32 iout_flash_max[2];
49*eb5a4422SLinus Walleij 	u32 flash_timeout_max[2];
50*eb5a4422SLinus Walleij 	u32 num_leds;
51*eb5a4422SLinus Walleij 	u32 boost_mode;
52*eb5a4422SLinus Walleij 	u32 boost_vout;
53*eb5a4422SLinus Walleij 	u32 low_vsys;
54*eb5a4422SLinus Walleij };
55*eb5a4422SLinus Walleij 
56*eb5a4422SLinus Walleij struct max77693_sub_led {
57*eb5a4422SLinus Walleij 	/* corresponding FLED output identifier */
58*eb5a4422SLinus Walleij 	int fled_id;
59*eb5a4422SLinus Walleij 	/* corresponding LED Flash class device */
60*eb5a4422SLinus Walleij 	struct led_classdev_flash fled_cdev;
61*eb5a4422SLinus Walleij 	/* V4L2 Flash device */
62*eb5a4422SLinus Walleij 	struct v4l2_flash *v4l2_flash;
63*eb5a4422SLinus Walleij 
64*eb5a4422SLinus Walleij 	/* brightness cache */
65*eb5a4422SLinus Walleij 	unsigned int torch_brightness;
66*eb5a4422SLinus Walleij 	/* flash timeout cache */
67*eb5a4422SLinus Walleij 	unsigned int flash_timeout;
68*eb5a4422SLinus Walleij 	/* flash faults that may have occurred */
69*eb5a4422SLinus Walleij 	u32 flash_faults;
70*eb5a4422SLinus Walleij };
71*eb5a4422SLinus Walleij 
72*eb5a4422SLinus Walleij struct max77693_led_device {
73*eb5a4422SLinus Walleij 	/* parent mfd regmap */
74*eb5a4422SLinus Walleij 	struct regmap *regmap;
75*eb5a4422SLinus Walleij 	/* platform device data */
76*eb5a4422SLinus Walleij 	struct platform_device *pdev;
77*eb5a4422SLinus Walleij 	/* secures access to the device */
78*eb5a4422SLinus Walleij 	struct mutex lock;
79*eb5a4422SLinus Walleij 
80*eb5a4422SLinus Walleij 	/* sub led data */
81*eb5a4422SLinus Walleij 	struct max77693_sub_led sub_leds[2];
82*eb5a4422SLinus Walleij 
83*eb5a4422SLinus Walleij 	/* maximum torch current values for FLED outputs */
84*eb5a4422SLinus Walleij 	u32 iout_torch_max[2];
85*eb5a4422SLinus Walleij 	/* maximum flash current values for FLED outputs */
86*eb5a4422SLinus Walleij 	u32 iout_flash_max[2];
87*eb5a4422SLinus Walleij 
88*eb5a4422SLinus Walleij 	/* current flash timeout cache */
89*eb5a4422SLinus Walleij 	unsigned int current_flash_timeout;
90*eb5a4422SLinus Walleij 	/* ITORCH register cache */
91*eb5a4422SLinus Walleij 	u8 torch_iout_reg;
92*eb5a4422SLinus Walleij 	/* mode of fled outputs */
93*eb5a4422SLinus Walleij 	unsigned int mode_flags;
94*eb5a4422SLinus Walleij 	/* recently strobed fled */
95*eb5a4422SLinus Walleij 	int strobing_sub_led_id;
96*eb5a4422SLinus Walleij 	/* bitmask of FLED outputs use state (bit 0. - FLED1, bit 1. - FLED2) */
97*eb5a4422SLinus Walleij 	u8 fled_mask;
98*eb5a4422SLinus Walleij 	/* FLED modes that can be set */
99*eb5a4422SLinus Walleij 	u8 allowed_modes;
100*eb5a4422SLinus Walleij 
101*eb5a4422SLinus Walleij 	/* arrangement of current outputs */
102*eb5a4422SLinus Walleij 	bool iout_joint;
103*eb5a4422SLinus Walleij };
104*eb5a4422SLinus Walleij 
max77693_led_iout_to_reg(u32 ua)105*eb5a4422SLinus Walleij static u8 max77693_led_iout_to_reg(u32 ua)
106*eb5a4422SLinus Walleij {
107*eb5a4422SLinus Walleij 	if (ua < FLASH_IOUT_MIN)
108*eb5a4422SLinus Walleij 		ua = FLASH_IOUT_MIN;
109*eb5a4422SLinus Walleij 	return (ua - FLASH_IOUT_MIN) / FLASH_IOUT_STEP;
110*eb5a4422SLinus Walleij }
111*eb5a4422SLinus Walleij 
max77693_flash_timeout_to_reg(u32 us)112*eb5a4422SLinus Walleij static u8 max77693_flash_timeout_to_reg(u32 us)
113*eb5a4422SLinus Walleij {
114*eb5a4422SLinus Walleij 	return (us - FLASH_TIMEOUT_MIN) / FLASH_TIMEOUT_STEP;
115*eb5a4422SLinus Walleij }
116*eb5a4422SLinus Walleij 
flcdev_to_sub_led(struct led_classdev_flash * fled_cdev)117*eb5a4422SLinus Walleij static inline struct max77693_sub_led *flcdev_to_sub_led(
118*eb5a4422SLinus Walleij 					struct led_classdev_flash *fled_cdev)
119*eb5a4422SLinus Walleij {
120*eb5a4422SLinus Walleij 	return container_of(fled_cdev, struct max77693_sub_led, fled_cdev);
121*eb5a4422SLinus Walleij }
122*eb5a4422SLinus Walleij 
sub_led_to_led(struct max77693_sub_led * sub_led)123*eb5a4422SLinus Walleij static inline struct max77693_led_device *sub_led_to_led(
124*eb5a4422SLinus Walleij 					struct max77693_sub_led *sub_led)
125*eb5a4422SLinus Walleij {
126*eb5a4422SLinus Walleij 	return container_of(sub_led, struct max77693_led_device,
127*eb5a4422SLinus Walleij 				sub_leds[sub_led->fled_id]);
128*eb5a4422SLinus Walleij }
129*eb5a4422SLinus Walleij 
max77693_led_vsys_to_reg(u32 mv)130*eb5a4422SLinus Walleij static inline u8 max77693_led_vsys_to_reg(u32 mv)
131*eb5a4422SLinus Walleij {
132*eb5a4422SLinus Walleij 	return ((mv - MAX_FLASH1_VSYS_MIN) / MAX_FLASH1_VSYS_STEP) << 2;
133*eb5a4422SLinus Walleij }
134*eb5a4422SLinus Walleij 
max77693_led_vout_to_reg(u32 mv)135*eb5a4422SLinus Walleij static inline u8 max77693_led_vout_to_reg(u32 mv)
136*eb5a4422SLinus Walleij {
137*eb5a4422SLinus Walleij 	return (mv - FLASH_VOUT_MIN) / FLASH_VOUT_STEP + FLASH_VOUT_RMIN;
138*eb5a4422SLinus Walleij }
139*eb5a4422SLinus Walleij 
max77693_fled_used(struct max77693_led_device * led,int fled_id)140*eb5a4422SLinus Walleij static inline bool max77693_fled_used(struct max77693_led_device *led,
141*eb5a4422SLinus Walleij 					 int fled_id)
142*eb5a4422SLinus Walleij {
143*eb5a4422SLinus Walleij 	u8 fled_bit = (fled_id == FLED1) ? FLED1_IOUT : FLED2_IOUT;
144*eb5a4422SLinus Walleij 
145*eb5a4422SLinus Walleij 	return led->fled_mask & fled_bit;
146*eb5a4422SLinus Walleij }
147*eb5a4422SLinus Walleij 
max77693_set_mode_reg(struct max77693_led_device * led,u8 mode)148*eb5a4422SLinus Walleij static int max77693_set_mode_reg(struct max77693_led_device *led, u8 mode)
149*eb5a4422SLinus Walleij {
150*eb5a4422SLinus Walleij 	struct regmap *rmap = led->regmap;
151*eb5a4422SLinus Walleij 	int ret, v = 0, i;
152*eb5a4422SLinus Walleij 
153*eb5a4422SLinus Walleij 	for (i = FLED1; i <= FLED2; ++i) {
154*eb5a4422SLinus Walleij 		if (mode & MODE_TORCH(i))
155*eb5a4422SLinus Walleij 			v |= FLASH_EN_ON << TORCH_EN_SHIFT(i);
156*eb5a4422SLinus Walleij 
157*eb5a4422SLinus Walleij 		if (mode & MODE_FLASH(i)) {
158*eb5a4422SLinus Walleij 			v |= FLASH_EN_ON << FLASH_EN_SHIFT(i);
159*eb5a4422SLinus Walleij 		} else if (mode & MODE_FLASH_EXTERNAL(i)) {
160*eb5a4422SLinus Walleij 			v |= FLASH_EN_FLASH << FLASH_EN_SHIFT(i);
161*eb5a4422SLinus Walleij 			/*
162*eb5a4422SLinus Walleij 			 * Enable hw triggering also for torch mode, as some
163*eb5a4422SLinus Walleij 			 * camera sensors use torch led to fathom ambient light
164*eb5a4422SLinus Walleij 			 * conditions before strobing the flash.
165*eb5a4422SLinus Walleij 			 */
166*eb5a4422SLinus Walleij 			v |= FLASH_EN_TORCH << TORCH_EN_SHIFT(i);
167*eb5a4422SLinus Walleij 		}
168*eb5a4422SLinus Walleij 	}
169*eb5a4422SLinus Walleij 
170*eb5a4422SLinus Walleij 	/* Reset the register only prior setting flash modes */
171*eb5a4422SLinus Walleij 	if (mode & ~(MODE_TORCH(FLED1) | MODE_TORCH(FLED2))) {
172*eb5a4422SLinus Walleij 		ret = regmap_write(rmap, MAX77693_LED_REG_FLASH_EN, 0);
173*eb5a4422SLinus Walleij 		if (ret < 0)
174*eb5a4422SLinus Walleij 			return ret;
175*eb5a4422SLinus Walleij 	}
176*eb5a4422SLinus Walleij 
177*eb5a4422SLinus Walleij 	return regmap_write(rmap, MAX77693_LED_REG_FLASH_EN, v);
178*eb5a4422SLinus Walleij }
179*eb5a4422SLinus Walleij 
max77693_add_mode(struct max77693_led_device * led,u8 mode)180*eb5a4422SLinus Walleij static int max77693_add_mode(struct max77693_led_device *led, u8 mode)
181*eb5a4422SLinus Walleij {
182*eb5a4422SLinus Walleij 	u8 new_mode_flags;
183*eb5a4422SLinus Walleij 	int i, ret;
184*eb5a4422SLinus Walleij 
185*eb5a4422SLinus Walleij 	if (led->iout_joint)
186*eb5a4422SLinus Walleij 		/* Span the mode on FLED2 for joint iouts case */
187*eb5a4422SLinus Walleij 		mode |= (mode << 1);
188*eb5a4422SLinus Walleij 
189*eb5a4422SLinus Walleij 	/*
190*eb5a4422SLinus Walleij 	 * FLASH_EXTERNAL mode activates FLASHEN and TORCHEN pins in the device.
191*eb5a4422SLinus Walleij 	 * Corresponding register bit fields interfere with SW triggered modes,
192*eb5a4422SLinus Walleij 	 * thus clear them to ensure proper device configuration.
193*eb5a4422SLinus Walleij 	 */
194*eb5a4422SLinus Walleij 	for (i = FLED1; i <= FLED2; ++i)
195*eb5a4422SLinus Walleij 		if (mode & MODE_FLASH_EXTERNAL(i))
196*eb5a4422SLinus Walleij 			led->mode_flags &= (~MODE_TORCH(i) & ~MODE_FLASH(i));
197*eb5a4422SLinus Walleij 
198*eb5a4422SLinus Walleij 	new_mode_flags = mode | led->mode_flags;
199*eb5a4422SLinus Walleij 	new_mode_flags &= led->allowed_modes;
200*eb5a4422SLinus Walleij 
201*eb5a4422SLinus Walleij 	if (new_mode_flags ^ led->mode_flags)
202*eb5a4422SLinus Walleij 		led->mode_flags = new_mode_flags;
203*eb5a4422SLinus Walleij 	else
204*eb5a4422SLinus Walleij 		return 0;
205*eb5a4422SLinus Walleij 
206*eb5a4422SLinus Walleij 	ret = max77693_set_mode_reg(led, led->mode_flags);
207*eb5a4422SLinus Walleij 	if (ret < 0)
208*eb5a4422SLinus Walleij 		return ret;
209*eb5a4422SLinus Walleij 
210*eb5a4422SLinus Walleij 	/*
211*eb5a4422SLinus Walleij 	 * Clear flash mode flag after setting the mode to avoid spurious flash
212*eb5a4422SLinus Walleij 	 * strobing on each subsequent torch mode setting.
213*eb5a4422SLinus Walleij 	 */
214*eb5a4422SLinus Walleij 	if (mode & MODE_FLASH_MASK)
215*eb5a4422SLinus Walleij 		led->mode_flags &= ~mode;
216*eb5a4422SLinus Walleij 
217*eb5a4422SLinus Walleij 	return ret;
218*eb5a4422SLinus Walleij }
219*eb5a4422SLinus Walleij 
max77693_clear_mode(struct max77693_led_device * led,u8 mode)220*eb5a4422SLinus Walleij static int max77693_clear_mode(struct max77693_led_device *led,
221*eb5a4422SLinus Walleij 				u8 mode)
222*eb5a4422SLinus Walleij {
223*eb5a4422SLinus Walleij 	if (led->iout_joint)
224*eb5a4422SLinus Walleij 		/* Clear mode also on FLED2 for joint iouts case */
225*eb5a4422SLinus Walleij 		mode |= (mode << 1);
226*eb5a4422SLinus Walleij 
227*eb5a4422SLinus Walleij 	led->mode_flags &= ~mode;
228*eb5a4422SLinus Walleij 
229*eb5a4422SLinus Walleij 	return max77693_set_mode_reg(led, led->mode_flags);
230*eb5a4422SLinus Walleij }
231*eb5a4422SLinus Walleij 
max77693_add_allowed_modes(struct max77693_led_device * led,int fled_id,enum max77693_led_mode mode)232*eb5a4422SLinus Walleij static void max77693_add_allowed_modes(struct max77693_led_device *led,
233*eb5a4422SLinus Walleij 				int fled_id, enum max77693_led_mode mode)
234*eb5a4422SLinus Walleij {
235*eb5a4422SLinus Walleij 	if (mode == FLASH)
236*eb5a4422SLinus Walleij 		led->allowed_modes |= (MODE_FLASH(fled_id) |
237*eb5a4422SLinus Walleij 				       MODE_FLASH_EXTERNAL(fled_id));
238*eb5a4422SLinus Walleij 	else
239*eb5a4422SLinus Walleij 		led->allowed_modes |= MODE_TORCH(fled_id);
240*eb5a4422SLinus Walleij }
241*eb5a4422SLinus Walleij 
max77693_distribute_currents(struct max77693_led_device * led,int fled_id,enum max77693_led_mode mode,u32 micro_amp,u32 iout_max[2],u32 iout[2])242*eb5a4422SLinus Walleij static void max77693_distribute_currents(struct max77693_led_device *led,
243*eb5a4422SLinus Walleij 				int fled_id, enum max77693_led_mode mode,
244*eb5a4422SLinus Walleij 				u32 micro_amp, u32 iout_max[2], u32 iout[2])
245*eb5a4422SLinus Walleij {
246*eb5a4422SLinus Walleij 	if (!led->iout_joint) {
247*eb5a4422SLinus Walleij 		iout[fled_id] = micro_amp;
248*eb5a4422SLinus Walleij 		max77693_add_allowed_modes(led, fled_id, mode);
249*eb5a4422SLinus Walleij 		return;
250*eb5a4422SLinus Walleij 	}
251*eb5a4422SLinus Walleij 
252*eb5a4422SLinus Walleij 	iout[FLED1] = min(micro_amp, iout_max[FLED1]);
253*eb5a4422SLinus Walleij 	iout[FLED2] = micro_amp - iout[FLED1];
254*eb5a4422SLinus Walleij 
255*eb5a4422SLinus Walleij 	if (mode == FLASH)
256*eb5a4422SLinus Walleij 		led->allowed_modes &= ~MODE_FLASH_MASK;
257*eb5a4422SLinus Walleij 	else
258*eb5a4422SLinus Walleij 		led->allowed_modes &= ~MODE_TORCH_MASK;
259*eb5a4422SLinus Walleij 
260*eb5a4422SLinus Walleij 	max77693_add_allowed_modes(led, FLED1, mode);
261*eb5a4422SLinus Walleij 
262*eb5a4422SLinus Walleij 	if (iout[FLED2])
263*eb5a4422SLinus Walleij 		max77693_add_allowed_modes(led, FLED2, mode);
264*eb5a4422SLinus Walleij }
265*eb5a4422SLinus Walleij 
max77693_set_torch_current(struct max77693_led_device * led,int fled_id,u32 micro_amp)266*eb5a4422SLinus Walleij static int max77693_set_torch_current(struct max77693_led_device *led,
267*eb5a4422SLinus Walleij 				int fled_id, u32 micro_amp)
268*eb5a4422SLinus Walleij {
269*eb5a4422SLinus Walleij 	struct regmap *rmap = led->regmap;
270*eb5a4422SLinus Walleij 	u8 iout1_reg = 0, iout2_reg = 0;
271*eb5a4422SLinus Walleij 	u32 iout[2];
272*eb5a4422SLinus Walleij 
273*eb5a4422SLinus Walleij 	max77693_distribute_currents(led, fled_id, TORCH, micro_amp,
274*eb5a4422SLinus Walleij 					led->iout_torch_max, iout);
275*eb5a4422SLinus Walleij 
276*eb5a4422SLinus Walleij 	if (fled_id == FLED1 || led->iout_joint) {
277*eb5a4422SLinus Walleij 		iout1_reg = max77693_led_iout_to_reg(iout[FLED1]);
278*eb5a4422SLinus Walleij 		led->torch_iout_reg &= TORCH_IOUT_MASK(TORCH_IOUT2_SHIFT);
279*eb5a4422SLinus Walleij 	}
280*eb5a4422SLinus Walleij 	if (fled_id == FLED2 || led->iout_joint) {
281*eb5a4422SLinus Walleij 		iout2_reg = max77693_led_iout_to_reg(iout[FLED2]);
282*eb5a4422SLinus Walleij 		led->torch_iout_reg &= TORCH_IOUT_MASK(TORCH_IOUT1_SHIFT);
283*eb5a4422SLinus Walleij 	}
284*eb5a4422SLinus Walleij 
285*eb5a4422SLinus Walleij 	led->torch_iout_reg |= ((iout1_reg << TORCH_IOUT1_SHIFT) |
286*eb5a4422SLinus Walleij 				(iout2_reg << TORCH_IOUT2_SHIFT));
287*eb5a4422SLinus Walleij 
288*eb5a4422SLinus Walleij 	return regmap_write(rmap, MAX77693_LED_REG_ITORCH,
289*eb5a4422SLinus Walleij 						led->torch_iout_reg);
290*eb5a4422SLinus Walleij }
291*eb5a4422SLinus Walleij 
max77693_set_flash_current(struct max77693_led_device * led,int fled_id,u32 micro_amp)292*eb5a4422SLinus Walleij static int max77693_set_flash_current(struct max77693_led_device *led,
293*eb5a4422SLinus Walleij 					int fled_id,
294*eb5a4422SLinus Walleij 					u32 micro_amp)
295*eb5a4422SLinus Walleij {
296*eb5a4422SLinus Walleij 	struct regmap *rmap = led->regmap;
297*eb5a4422SLinus Walleij 	u8 iout1_reg, iout2_reg;
298*eb5a4422SLinus Walleij 	u32 iout[2];
299*eb5a4422SLinus Walleij 	int ret = -EINVAL;
300*eb5a4422SLinus Walleij 
301*eb5a4422SLinus Walleij 	max77693_distribute_currents(led, fled_id, FLASH, micro_amp,
302*eb5a4422SLinus Walleij 					led->iout_flash_max, iout);
303*eb5a4422SLinus Walleij 
304*eb5a4422SLinus Walleij 	if (fled_id == FLED1 || led->iout_joint) {
305*eb5a4422SLinus Walleij 		iout1_reg = max77693_led_iout_to_reg(iout[FLED1]);
306*eb5a4422SLinus Walleij 		ret = regmap_write(rmap, MAX77693_LED_REG_IFLASH1,
307*eb5a4422SLinus Walleij 							iout1_reg);
308*eb5a4422SLinus Walleij 		if (ret < 0)
309*eb5a4422SLinus Walleij 			return ret;
310*eb5a4422SLinus Walleij 	}
311*eb5a4422SLinus Walleij 	if (fled_id == FLED2 || led->iout_joint) {
312*eb5a4422SLinus Walleij 		iout2_reg = max77693_led_iout_to_reg(iout[FLED2]);
313*eb5a4422SLinus Walleij 		ret = regmap_write(rmap, MAX77693_LED_REG_IFLASH2,
314*eb5a4422SLinus Walleij 							iout2_reg);
315*eb5a4422SLinus Walleij 	}
316*eb5a4422SLinus Walleij 
317*eb5a4422SLinus Walleij 	return ret;
318*eb5a4422SLinus Walleij }
319*eb5a4422SLinus Walleij 
max77693_set_timeout(struct max77693_led_device * led,u32 microsec)320*eb5a4422SLinus Walleij static int max77693_set_timeout(struct max77693_led_device *led, u32 microsec)
321*eb5a4422SLinus Walleij {
322*eb5a4422SLinus Walleij 	struct regmap *rmap = led->regmap;
323*eb5a4422SLinus Walleij 	u8 v;
324*eb5a4422SLinus Walleij 	int ret;
325*eb5a4422SLinus Walleij 
326*eb5a4422SLinus Walleij 	v = max77693_flash_timeout_to_reg(microsec) | FLASH_TMR_LEVEL;
327*eb5a4422SLinus Walleij 
328*eb5a4422SLinus Walleij 	ret = regmap_write(rmap, MAX77693_LED_REG_FLASH_TIMER, v);
329*eb5a4422SLinus Walleij 	if (ret < 0)
330*eb5a4422SLinus Walleij 		return ret;
331*eb5a4422SLinus Walleij 
332*eb5a4422SLinus Walleij 	led->current_flash_timeout = microsec;
333*eb5a4422SLinus Walleij 
334*eb5a4422SLinus Walleij 	return 0;
335*eb5a4422SLinus Walleij }
336*eb5a4422SLinus Walleij 
max77693_get_strobe_status(struct max77693_led_device * led,bool * state)337*eb5a4422SLinus Walleij static int max77693_get_strobe_status(struct max77693_led_device *led,
338*eb5a4422SLinus Walleij 					bool *state)
339*eb5a4422SLinus Walleij {
340*eb5a4422SLinus Walleij 	struct regmap *rmap = led->regmap;
341*eb5a4422SLinus Walleij 	unsigned int v;
342*eb5a4422SLinus Walleij 	int ret;
343*eb5a4422SLinus Walleij 
344*eb5a4422SLinus Walleij 	ret = regmap_read(rmap, MAX77693_LED_REG_FLASH_STATUS, &v);
345*eb5a4422SLinus Walleij 	if (ret < 0)
346*eb5a4422SLinus Walleij 		return ret;
347*eb5a4422SLinus Walleij 
348*eb5a4422SLinus Walleij 	*state = v & FLASH_STATUS_FLASH_ON;
349*eb5a4422SLinus Walleij 
350*eb5a4422SLinus Walleij 	return ret;
351*eb5a4422SLinus Walleij }
352*eb5a4422SLinus Walleij 
max77693_get_flash_faults(struct max77693_sub_led * sub_led)353*eb5a4422SLinus Walleij static int max77693_get_flash_faults(struct max77693_sub_led *sub_led)
354*eb5a4422SLinus Walleij {
355*eb5a4422SLinus Walleij 	struct max77693_led_device *led = sub_led_to_led(sub_led);
356*eb5a4422SLinus Walleij 	struct regmap *rmap = led->regmap;
357*eb5a4422SLinus Walleij 	unsigned int v;
358*eb5a4422SLinus Walleij 	u8 fault_open_mask, fault_short_mask;
359*eb5a4422SLinus Walleij 	int ret;
360*eb5a4422SLinus Walleij 
361*eb5a4422SLinus Walleij 	sub_led->flash_faults = 0;
362*eb5a4422SLinus Walleij 
363*eb5a4422SLinus Walleij 	if (led->iout_joint) {
364*eb5a4422SLinus Walleij 		fault_open_mask = FLASH_INT_FLED1_OPEN | FLASH_INT_FLED2_OPEN;
365*eb5a4422SLinus Walleij 		fault_short_mask = FLASH_INT_FLED1_SHORT |
366*eb5a4422SLinus Walleij 							FLASH_INT_FLED2_SHORT;
367*eb5a4422SLinus Walleij 	} else {
368*eb5a4422SLinus Walleij 		fault_open_mask = (sub_led->fled_id == FLED1) ?
369*eb5a4422SLinus Walleij 						FLASH_INT_FLED1_OPEN :
370*eb5a4422SLinus Walleij 						FLASH_INT_FLED2_OPEN;
371*eb5a4422SLinus Walleij 		fault_short_mask = (sub_led->fled_id == FLED1) ?
372*eb5a4422SLinus Walleij 						FLASH_INT_FLED1_SHORT :
373*eb5a4422SLinus Walleij 						FLASH_INT_FLED2_SHORT;
374*eb5a4422SLinus Walleij 	}
375*eb5a4422SLinus Walleij 
376*eb5a4422SLinus Walleij 	ret = regmap_read(rmap, MAX77693_LED_REG_FLASH_INT, &v);
377*eb5a4422SLinus Walleij 	if (ret < 0)
378*eb5a4422SLinus Walleij 		return ret;
379*eb5a4422SLinus Walleij 
380*eb5a4422SLinus Walleij 	if (v & fault_open_mask)
381*eb5a4422SLinus Walleij 		sub_led->flash_faults |= LED_FAULT_OVER_VOLTAGE;
382*eb5a4422SLinus Walleij 	if (v & fault_short_mask)
383*eb5a4422SLinus Walleij 		sub_led->flash_faults |= LED_FAULT_SHORT_CIRCUIT;
384*eb5a4422SLinus Walleij 	if (v & FLASH_INT_OVER_CURRENT)
385*eb5a4422SLinus Walleij 		sub_led->flash_faults |= LED_FAULT_OVER_CURRENT;
386*eb5a4422SLinus Walleij 
387*eb5a4422SLinus Walleij 	return 0;
388*eb5a4422SLinus Walleij }
389*eb5a4422SLinus Walleij 
max77693_setup(struct max77693_led_device * led,struct max77693_led_config_data * led_cfg)390*eb5a4422SLinus Walleij static int max77693_setup(struct max77693_led_device *led,
391*eb5a4422SLinus Walleij 			 struct max77693_led_config_data *led_cfg)
392*eb5a4422SLinus Walleij {
393*eb5a4422SLinus Walleij 	struct regmap *rmap = led->regmap;
394*eb5a4422SLinus Walleij 	int i, first_led, last_led, ret;
395*eb5a4422SLinus Walleij 	u32 max_flash_curr[2];
396*eb5a4422SLinus Walleij 	u8 v;
397*eb5a4422SLinus Walleij 
398*eb5a4422SLinus Walleij 	/*
399*eb5a4422SLinus Walleij 	 * Initialize only flash current. Torch current doesn't
400*eb5a4422SLinus Walleij 	 * require initialization as ITORCH register is written with
401*eb5a4422SLinus Walleij 	 * new value each time brightness_set op is called.
402*eb5a4422SLinus Walleij 	 */
403*eb5a4422SLinus Walleij 	if (led->iout_joint) {
404*eb5a4422SLinus Walleij 		first_led = FLED1;
405*eb5a4422SLinus Walleij 		last_led = FLED1;
406*eb5a4422SLinus Walleij 		max_flash_curr[FLED1] = led_cfg->iout_flash_max[FLED1] +
407*eb5a4422SLinus Walleij 					led_cfg->iout_flash_max[FLED2];
408*eb5a4422SLinus Walleij 	} else {
409*eb5a4422SLinus Walleij 		first_led = max77693_fled_used(led, FLED1) ? FLED1 : FLED2;
410*eb5a4422SLinus Walleij 		last_led = max77693_fled_used(led, FLED2) ? FLED2 : FLED1;
411*eb5a4422SLinus Walleij 		max_flash_curr[FLED1] = led_cfg->iout_flash_max[FLED1];
412*eb5a4422SLinus Walleij 		max_flash_curr[FLED2] = led_cfg->iout_flash_max[FLED2];
413*eb5a4422SLinus Walleij 	}
414*eb5a4422SLinus Walleij 
415*eb5a4422SLinus Walleij 	for (i = first_led; i <= last_led; ++i) {
416*eb5a4422SLinus Walleij 		ret = max77693_set_flash_current(led, i,
417*eb5a4422SLinus Walleij 					max_flash_curr[i]);
418*eb5a4422SLinus Walleij 		if (ret < 0)
419*eb5a4422SLinus Walleij 			return ret;
420*eb5a4422SLinus Walleij 	}
421*eb5a4422SLinus Walleij 
422*eb5a4422SLinus Walleij 	v = TORCH_TMR_NO_TIMER | MAX77693_LED_TRIG_TYPE_LEVEL;
423*eb5a4422SLinus Walleij 	ret = regmap_write(rmap, MAX77693_LED_REG_ITORCHTIMER, v);
424*eb5a4422SLinus Walleij 	if (ret < 0)
425*eb5a4422SLinus Walleij 		return ret;
426*eb5a4422SLinus Walleij 
427*eb5a4422SLinus Walleij 	if (led_cfg->low_vsys > 0)
428*eb5a4422SLinus Walleij 		v = max77693_led_vsys_to_reg(led_cfg->low_vsys) |
429*eb5a4422SLinus Walleij 						MAX_FLASH1_MAX_FL_EN;
430*eb5a4422SLinus Walleij 	else
431*eb5a4422SLinus Walleij 		v = 0;
432*eb5a4422SLinus Walleij 
433*eb5a4422SLinus Walleij 	ret = regmap_write(rmap, MAX77693_LED_REG_MAX_FLASH1, v);
434*eb5a4422SLinus Walleij 	if (ret < 0)
435*eb5a4422SLinus Walleij 		return ret;
436*eb5a4422SLinus Walleij 	ret = regmap_write(rmap, MAX77693_LED_REG_MAX_FLASH2, 0);
437*eb5a4422SLinus Walleij 	if (ret < 0)
438*eb5a4422SLinus Walleij 		return ret;
439*eb5a4422SLinus Walleij 
440*eb5a4422SLinus Walleij 	if (led_cfg->boost_mode == MAX77693_LED_BOOST_FIXED)
441*eb5a4422SLinus Walleij 		v = FLASH_BOOST_FIXED;
442*eb5a4422SLinus Walleij 	else
443*eb5a4422SLinus Walleij 		v = led_cfg->boost_mode | led_cfg->boost_mode << 1;
444*eb5a4422SLinus Walleij 
445*eb5a4422SLinus Walleij 	if (max77693_fled_used(led, FLED1) && max77693_fled_used(led, FLED2))
446*eb5a4422SLinus Walleij 		v |= FLASH_BOOST_LEDNUM_2;
447*eb5a4422SLinus Walleij 
448*eb5a4422SLinus Walleij 	ret = regmap_write(rmap, MAX77693_LED_REG_VOUT_CNTL, v);
449*eb5a4422SLinus Walleij 	if (ret < 0)
450*eb5a4422SLinus Walleij 		return ret;
451*eb5a4422SLinus Walleij 
452*eb5a4422SLinus Walleij 	v = max77693_led_vout_to_reg(led_cfg->boost_vout);
453*eb5a4422SLinus Walleij 	ret = regmap_write(rmap, MAX77693_LED_REG_VOUT_FLASH1, v);
454*eb5a4422SLinus Walleij 	if (ret < 0)
455*eb5a4422SLinus Walleij 		return ret;
456*eb5a4422SLinus Walleij 
457*eb5a4422SLinus Walleij 	return max77693_set_mode_reg(led, MODE_OFF);
458*eb5a4422SLinus Walleij }
459*eb5a4422SLinus Walleij 
460*eb5a4422SLinus Walleij /* LED subsystem callbacks */
max77693_led_brightness_set(struct led_classdev * led_cdev,enum led_brightness value)461*eb5a4422SLinus Walleij static int max77693_led_brightness_set(struct led_classdev *led_cdev,
462*eb5a4422SLinus Walleij 					enum led_brightness value)
463*eb5a4422SLinus Walleij {
464*eb5a4422SLinus Walleij 	struct led_classdev_flash *fled_cdev = lcdev_to_flcdev(led_cdev);
465*eb5a4422SLinus Walleij 	struct max77693_sub_led *sub_led = flcdev_to_sub_led(fled_cdev);
466*eb5a4422SLinus Walleij 	struct max77693_led_device *led = sub_led_to_led(sub_led);
467*eb5a4422SLinus Walleij 	int fled_id = sub_led->fled_id, ret;
468*eb5a4422SLinus Walleij 
469*eb5a4422SLinus Walleij 	mutex_lock(&led->lock);
470*eb5a4422SLinus Walleij 
471*eb5a4422SLinus Walleij 	if (value == 0) {
472*eb5a4422SLinus Walleij 		ret = max77693_clear_mode(led, MODE_TORCH(fled_id));
473*eb5a4422SLinus Walleij 		if (ret < 0)
474*eb5a4422SLinus Walleij 			dev_dbg(&led->pdev->dev,
475*eb5a4422SLinus Walleij 				"Failed to clear torch mode (%d)\n",
476*eb5a4422SLinus Walleij 				ret);
477*eb5a4422SLinus Walleij 		goto unlock;
478*eb5a4422SLinus Walleij 	}
479*eb5a4422SLinus Walleij 
480*eb5a4422SLinus Walleij 	ret = max77693_set_torch_current(led, fled_id, value * TORCH_IOUT_STEP);
481*eb5a4422SLinus Walleij 	if (ret < 0) {
482*eb5a4422SLinus Walleij 		dev_dbg(&led->pdev->dev,
483*eb5a4422SLinus Walleij 			"Failed to set torch current (%d)\n",
484*eb5a4422SLinus Walleij 			ret);
485*eb5a4422SLinus Walleij 		goto unlock;
486*eb5a4422SLinus Walleij 	}
487*eb5a4422SLinus Walleij 
488*eb5a4422SLinus Walleij 	ret = max77693_add_mode(led, MODE_TORCH(fled_id));
489*eb5a4422SLinus Walleij 	if (ret < 0)
490*eb5a4422SLinus Walleij 		dev_dbg(&led->pdev->dev,
491*eb5a4422SLinus Walleij 			"Failed to set torch mode (%d)\n",
492*eb5a4422SLinus Walleij 			ret);
493*eb5a4422SLinus Walleij unlock:
494*eb5a4422SLinus Walleij 	mutex_unlock(&led->lock);
495*eb5a4422SLinus Walleij 
496*eb5a4422SLinus Walleij 	return ret;
497*eb5a4422SLinus Walleij }
498*eb5a4422SLinus Walleij 
max77693_led_flash_brightness_set(struct led_classdev_flash * fled_cdev,u32 brightness)499*eb5a4422SLinus Walleij static int max77693_led_flash_brightness_set(
500*eb5a4422SLinus Walleij 				struct led_classdev_flash *fled_cdev,
501*eb5a4422SLinus Walleij 				u32 brightness)
502*eb5a4422SLinus Walleij {
503*eb5a4422SLinus Walleij 	struct max77693_sub_led *sub_led = flcdev_to_sub_led(fled_cdev);
504*eb5a4422SLinus Walleij 	struct max77693_led_device *led = sub_led_to_led(sub_led);
505*eb5a4422SLinus Walleij 	int ret;
506*eb5a4422SLinus Walleij 
507*eb5a4422SLinus Walleij 	mutex_lock(&led->lock);
508*eb5a4422SLinus Walleij 	ret = max77693_set_flash_current(led, sub_led->fled_id, brightness);
509*eb5a4422SLinus Walleij 	mutex_unlock(&led->lock);
510*eb5a4422SLinus Walleij 
511*eb5a4422SLinus Walleij 	return ret;
512*eb5a4422SLinus Walleij }
513*eb5a4422SLinus Walleij 
max77693_led_flash_strobe_set(struct led_classdev_flash * fled_cdev,bool state)514*eb5a4422SLinus Walleij static int max77693_led_flash_strobe_set(
515*eb5a4422SLinus Walleij 				struct led_classdev_flash *fled_cdev,
516*eb5a4422SLinus Walleij 				bool state)
517*eb5a4422SLinus Walleij {
518*eb5a4422SLinus Walleij 	struct max77693_sub_led *sub_led = flcdev_to_sub_led(fled_cdev);
519*eb5a4422SLinus Walleij 	struct max77693_led_device *led = sub_led_to_led(sub_led);
520*eb5a4422SLinus Walleij 	int fled_id = sub_led->fled_id;
521*eb5a4422SLinus Walleij 	int ret;
522*eb5a4422SLinus Walleij 
523*eb5a4422SLinus Walleij 	mutex_lock(&led->lock);
524*eb5a4422SLinus Walleij 
525*eb5a4422SLinus Walleij 	if (!state) {
526*eb5a4422SLinus Walleij 		ret = max77693_clear_mode(led, MODE_FLASH(fled_id));
527*eb5a4422SLinus Walleij 		goto unlock;
528*eb5a4422SLinus Walleij 	}
529*eb5a4422SLinus Walleij 
530*eb5a4422SLinus Walleij 	if (sub_led->flash_timeout != led->current_flash_timeout) {
531*eb5a4422SLinus Walleij 		ret = max77693_set_timeout(led, sub_led->flash_timeout);
532*eb5a4422SLinus Walleij 		if (ret < 0)
533*eb5a4422SLinus Walleij 			goto unlock;
534*eb5a4422SLinus Walleij 	}
535*eb5a4422SLinus Walleij 
536*eb5a4422SLinus Walleij 	led->strobing_sub_led_id = fled_id;
537*eb5a4422SLinus Walleij 
538*eb5a4422SLinus Walleij 	ret = max77693_add_mode(led, MODE_FLASH(fled_id));
539*eb5a4422SLinus Walleij 	if (ret < 0)
540*eb5a4422SLinus Walleij 		goto unlock;
541*eb5a4422SLinus Walleij 
542*eb5a4422SLinus Walleij 	ret = max77693_get_flash_faults(sub_led);
543*eb5a4422SLinus Walleij 
544*eb5a4422SLinus Walleij unlock:
545*eb5a4422SLinus Walleij 	mutex_unlock(&led->lock);
546*eb5a4422SLinus Walleij 	return ret;
547*eb5a4422SLinus Walleij }
548*eb5a4422SLinus Walleij 
max77693_led_flash_fault_get(struct led_classdev_flash * fled_cdev,u32 * fault)549*eb5a4422SLinus Walleij static int max77693_led_flash_fault_get(
550*eb5a4422SLinus Walleij 				struct led_classdev_flash *fled_cdev,
551*eb5a4422SLinus Walleij 				u32 *fault)
552*eb5a4422SLinus Walleij {
553*eb5a4422SLinus Walleij 	struct max77693_sub_led *sub_led = flcdev_to_sub_led(fled_cdev);
554*eb5a4422SLinus Walleij 
555*eb5a4422SLinus Walleij 	*fault = sub_led->flash_faults;
556*eb5a4422SLinus Walleij 
557*eb5a4422SLinus Walleij 	return 0;
558*eb5a4422SLinus Walleij }
559*eb5a4422SLinus Walleij 
max77693_led_flash_strobe_get(struct led_classdev_flash * fled_cdev,bool * state)560*eb5a4422SLinus Walleij static int max77693_led_flash_strobe_get(
561*eb5a4422SLinus Walleij 				struct led_classdev_flash *fled_cdev,
562*eb5a4422SLinus Walleij 				bool *state)
563*eb5a4422SLinus Walleij {
564*eb5a4422SLinus Walleij 	struct max77693_sub_led *sub_led = flcdev_to_sub_led(fled_cdev);
565*eb5a4422SLinus Walleij 	struct max77693_led_device *led = sub_led_to_led(sub_led);
566*eb5a4422SLinus Walleij 	int ret;
567*eb5a4422SLinus Walleij 
568*eb5a4422SLinus Walleij 	if (!state)
569*eb5a4422SLinus Walleij 		return -EINVAL;
570*eb5a4422SLinus Walleij 
571*eb5a4422SLinus Walleij 	mutex_lock(&led->lock);
572*eb5a4422SLinus Walleij 
573*eb5a4422SLinus Walleij 	ret = max77693_get_strobe_status(led, state);
574*eb5a4422SLinus Walleij 
575*eb5a4422SLinus Walleij 	*state = !!(*state && (led->strobing_sub_led_id == sub_led->fled_id));
576*eb5a4422SLinus Walleij 
577*eb5a4422SLinus Walleij 	mutex_unlock(&led->lock);
578*eb5a4422SLinus Walleij 
579*eb5a4422SLinus Walleij 	return ret;
580*eb5a4422SLinus Walleij }
581*eb5a4422SLinus Walleij 
max77693_led_flash_timeout_set(struct led_classdev_flash * fled_cdev,u32 timeout)582*eb5a4422SLinus Walleij static int max77693_led_flash_timeout_set(
583*eb5a4422SLinus Walleij 				struct led_classdev_flash *fled_cdev,
584*eb5a4422SLinus Walleij 				u32 timeout)
585*eb5a4422SLinus Walleij {
586*eb5a4422SLinus Walleij 	struct max77693_sub_led *sub_led = flcdev_to_sub_led(fled_cdev);
587*eb5a4422SLinus Walleij 	struct max77693_led_device *led = sub_led_to_led(sub_led);
588*eb5a4422SLinus Walleij 
589*eb5a4422SLinus Walleij 	mutex_lock(&led->lock);
590*eb5a4422SLinus Walleij 	sub_led->flash_timeout = timeout;
591*eb5a4422SLinus Walleij 	mutex_unlock(&led->lock);
592*eb5a4422SLinus Walleij 
593*eb5a4422SLinus Walleij 	return 0;
594*eb5a4422SLinus Walleij }
595*eb5a4422SLinus Walleij 
max77693_led_parse_dt(struct max77693_led_device * led,struct max77693_led_config_data * cfg,struct device_node ** sub_nodes)596*eb5a4422SLinus Walleij static int max77693_led_parse_dt(struct max77693_led_device *led,
597*eb5a4422SLinus Walleij 				struct max77693_led_config_data *cfg,
598*eb5a4422SLinus Walleij 				struct device_node **sub_nodes)
599*eb5a4422SLinus Walleij {
600*eb5a4422SLinus Walleij 	struct device *dev = &led->pdev->dev;
601*eb5a4422SLinus Walleij 	struct max77693_sub_led *sub_leds = led->sub_leds;
602*eb5a4422SLinus Walleij 	struct device_node *node = dev_of_node(dev), *child_node;
603*eb5a4422SLinus Walleij 	struct property *prop;
604*eb5a4422SLinus Walleij 	u32 led_sources[2];
605*eb5a4422SLinus Walleij 	int i, ret, fled_id;
606*eb5a4422SLinus Walleij 
607*eb5a4422SLinus Walleij 	of_property_read_u32(node, "maxim,boost-mode", &cfg->boost_mode);
608*eb5a4422SLinus Walleij 	of_property_read_u32(node, "maxim,boost-mvout", &cfg->boost_vout);
609*eb5a4422SLinus Walleij 	of_property_read_u32(node, "maxim,mvsys-min", &cfg->low_vsys);
610*eb5a4422SLinus Walleij 
611*eb5a4422SLinus Walleij 	for_each_available_child_of_node(node, child_node) {
612*eb5a4422SLinus Walleij 		prop = of_find_property(child_node, "led-sources", NULL);
613*eb5a4422SLinus Walleij 		if (prop) {
614*eb5a4422SLinus Walleij 			const __be32 *srcs = NULL;
615*eb5a4422SLinus Walleij 
616*eb5a4422SLinus Walleij 			for (i = 0; i < ARRAY_SIZE(led_sources); ++i) {
617*eb5a4422SLinus Walleij 				srcs = of_prop_next_u32(prop, srcs,
618*eb5a4422SLinus Walleij 							&led_sources[i]);
619*eb5a4422SLinus Walleij 				if (!srcs)
620*eb5a4422SLinus Walleij 					break;
621*eb5a4422SLinus Walleij 			}
622*eb5a4422SLinus Walleij 		} else {
623*eb5a4422SLinus Walleij 			dev_err(dev,
624*eb5a4422SLinus Walleij 				"led-sources DT property missing\n");
625*eb5a4422SLinus Walleij 			of_node_put(child_node);
626*eb5a4422SLinus Walleij 			return -EINVAL;
627*eb5a4422SLinus Walleij 		}
628*eb5a4422SLinus Walleij 
629*eb5a4422SLinus Walleij 		if (i == 2) {
630*eb5a4422SLinus Walleij 			fled_id = FLED1;
631*eb5a4422SLinus Walleij 			led->fled_mask = FLED1_IOUT | FLED2_IOUT;
632*eb5a4422SLinus Walleij 		} else if (led_sources[0] == FLED1) {
633*eb5a4422SLinus Walleij 			fled_id = FLED1;
634*eb5a4422SLinus Walleij 			led->fled_mask |= FLED1_IOUT;
635*eb5a4422SLinus Walleij 		} else if (led_sources[0] == FLED2) {
636*eb5a4422SLinus Walleij 			fled_id = FLED2;
637*eb5a4422SLinus Walleij 			led->fled_mask |= FLED2_IOUT;
638*eb5a4422SLinus Walleij 		} else {
639*eb5a4422SLinus Walleij 			dev_err(dev,
640*eb5a4422SLinus Walleij 				"Wrong led-sources DT property value.\n");
641*eb5a4422SLinus Walleij 			of_node_put(child_node);
642*eb5a4422SLinus Walleij 			return -EINVAL;
643*eb5a4422SLinus Walleij 		}
644*eb5a4422SLinus Walleij 
645*eb5a4422SLinus Walleij 		if (sub_nodes[fled_id]) {
646*eb5a4422SLinus Walleij 			dev_err(dev,
647*eb5a4422SLinus Walleij 				"Conflicting \"led-sources\" DT properties\n");
648*eb5a4422SLinus Walleij 			of_node_put(child_node);
649*eb5a4422SLinus Walleij 			return -EINVAL;
650*eb5a4422SLinus Walleij 		}
651*eb5a4422SLinus Walleij 
652*eb5a4422SLinus Walleij 		sub_nodes[fled_id] = child_node;
653*eb5a4422SLinus Walleij 		sub_leds[fled_id].fled_id = fled_id;
654*eb5a4422SLinus Walleij 
655*eb5a4422SLinus Walleij 		cfg->label[fled_id] =
656*eb5a4422SLinus Walleij 			of_get_property(child_node, "label", NULL) ? :
657*eb5a4422SLinus Walleij 						child_node->name;
658*eb5a4422SLinus Walleij 
659*eb5a4422SLinus Walleij 		ret = of_property_read_u32(child_node, "led-max-microamp",
660*eb5a4422SLinus Walleij 					&cfg->iout_torch_max[fled_id]);
661*eb5a4422SLinus Walleij 		if (ret < 0) {
662*eb5a4422SLinus Walleij 			cfg->iout_torch_max[fled_id] = TORCH_IOUT_MIN;
663*eb5a4422SLinus Walleij 			dev_warn(dev, "led-max-microamp DT property missing\n");
664*eb5a4422SLinus Walleij 		}
665*eb5a4422SLinus Walleij 
666*eb5a4422SLinus Walleij 		ret = of_property_read_u32(child_node, "flash-max-microamp",
667*eb5a4422SLinus Walleij 					&cfg->iout_flash_max[fled_id]);
668*eb5a4422SLinus Walleij 		if (ret < 0) {
669*eb5a4422SLinus Walleij 			cfg->iout_flash_max[fled_id] = FLASH_IOUT_MIN;
670*eb5a4422SLinus Walleij 			dev_warn(dev,
671*eb5a4422SLinus Walleij 				 "flash-max-microamp DT property missing\n");
672*eb5a4422SLinus Walleij 		}
673*eb5a4422SLinus Walleij 
674*eb5a4422SLinus Walleij 		ret = of_property_read_u32(child_node, "flash-max-timeout-us",
675*eb5a4422SLinus Walleij 					&cfg->flash_timeout_max[fled_id]);
676*eb5a4422SLinus Walleij 		if (ret < 0) {
677*eb5a4422SLinus Walleij 			cfg->flash_timeout_max[fled_id] = FLASH_TIMEOUT_MIN;
678*eb5a4422SLinus Walleij 			dev_warn(dev,
679*eb5a4422SLinus Walleij 				 "flash-max-timeout-us DT property missing\n");
680*eb5a4422SLinus Walleij 		}
681*eb5a4422SLinus Walleij 
682*eb5a4422SLinus Walleij 		if (++cfg->num_leds == 2 ||
683*eb5a4422SLinus Walleij 		    (max77693_fled_used(led, FLED1) &&
684*eb5a4422SLinus Walleij 		     max77693_fled_used(led, FLED2))) {
685*eb5a4422SLinus Walleij 			of_node_put(child_node);
686*eb5a4422SLinus Walleij 			break;
687*eb5a4422SLinus Walleij 		}
688*eb5a4422SLinus Walleij 	}
689*eb5a4422SLinus Walleij 
690*eb5a4422SLinus Walleij 	if (cfg->num_leds == 0) {
691*eb5a4422SLinus Walleij 		dev_err(dev, "No DT child node found for connected LED(s).\n");
692*eb5a4422SLinus Walleij 		return -EINVAL;
693*eb5a4422SLinus Walleij 	}
694*eb5a4422SLinus Walleij 
695*eb5a4422SLinus Walleij 	return 0;
696*eb5a4422SLinus Walleij }
697*eb5a4422SLinus Walleij 
clamp_align(u32 * v,u32 min,u32 max,u32 step)698*eb5a4422SLinus Walleij static void clamp_align(u32 *v, u32 min, u32 max, u32 step)
699*eb5a4422SLinus Walleij {
700*eb5a4422SLinus Walleij 	*v = clamp_val(*v, min, max);
701*eb5a4422SLinus Walleij 	if (step > 1)
702*eb5a4422SLinus Walleij 		*v = (*v - min) / step * step + min;
703*eb5a4422SLinus Walleij }
704*eb5a4422SLinus Walleij 
max77693_align_iout_current(struct max77693_led_device * led,u32 * iout,u32 min,u32 max,u32 step)705*eb5a4422SLinus Walleij static void max77693_align_iout_current(struct max77693_led_device *led,
706*eb5a4422SLinus Walleij 					u32 *iout, u32 min, u32 max, u32 step)
707*eb5a4422SLinus Walleij {
708*eb5a4422SLinus Walleij 	int i;
709*eb5a4422SLinus Walleij 
710*eb5a4422SLinus Walleij 	if (led->iout_joint) {
711*eb5a4422SLinus Walleij 		if (iout[FLED1] > min) {
712*eb5a4422SLinus Walleij 			iout[FLED1] /= 2;
713*eb5a4422SLinus Walleij 			iout[FLED2] = iout[FLED1];
714*eb5a4422SLinus Walleij 		} else {
715*eb5a4422SLinus Walleij 			iout[FLED1] = min;
716*eb5a4422SLinus Walleij 			iout[FLED2] = 0;
717*eb5a4422SLinus Walleij 			return;
718*eb5a4422SLinus Walleij 		}
719*eb5a4422SLinus Walleij 	}
720*eb5a4422SLinus Walleij 
721*eb5a4422SLinus Walleij 	for (i = FLED1; i <= FLED2; ++i)
722*eb5a4422SLinus Walleij 		if (max77693_fled_used(led, i))
723*eb5a4422SLinus Walleij 			clamp_align(&iout[i], min, max, step);
724*eb5a4422SLinus Walleij 		else
725*eb5a4422SLinus Walleij 			iout[i] = 0;
726*eb5a4422SLinus Walleij }
727*eb5a4422SLinus Walleij 
max77693_led_validate_configuration(struct max77693_led_device * led,struct max77693_led_config_data * cfg)728*eb5a4422SLinus Walleij static void max77693_led_validate_configuration(struct max77693_led_device *led,
729*eb5a4422SLinus Walleij 					struct max77693_led_config_data *cfg)
730*eb5a4422SLinus Walleij {
731*eb5a4422SLinus Walleij 	u32 flash_iout_max = cfg->boost_mode ? FLASH_IOUT_MAX_2LEDS :
732*eb5a4422SLinus Walleij 					       FLASH_IOUT_MAX_1LED;
733*eb5a4422SLinus Walleij 	int i;
734*eb5a4422SLinus Walleij 
735*eb5a4422SLinus Walleij 	if (cfg->num_leds == 1 &&
736*eb5a4422SLinus Walleij 	    max77693_fled_used(led, FLED1) && max77693_fled_used(led, FLED2))
737*eb5a4422SLinus Walleij 		led->iout_joint = true;
738*eb5a4422SLinus Walleij 
739*eb5a4422SLinus Walleij 	cfg->boost_mode = clamp_val(cfg->boost_mode, MAX77693_LED_BOOST_NONE,
740*eb5a4422SLinus Walleij 			    MAX77693_LED_BOOST_FIXED);
741*eb5a4422SLinus Walleij 
742*eb5a4422SLinus Walleij 	/* Boost must be enabled if both current outputs are used */
743*eb5a4422SLinus Walleij 	if ((cfg->boost_mode == MAX77693_LED_BOOST_NONE) && led->iout_joint)
744*eb5a4422SLinus Walleij 		cfg->boost_mode = MAX77693_LED_BOOST_FIXED;
745*eb5a4422SLinus Walleij 
746*eb5a4422SLinus Walleij 	max77693_align_iout_current(led, cfg->iout_torch_max,
747*eb5a4422SLinus Walleij 			TORCH_IOUT_MIN, TORCH_IOUT_MAX, TORCH_IOUT_STEP);
748*eb5a4422SLinus Walleij 
749*eb5a4422SLinus Walleij 	max77693_align_iout_current(led, cfg->iout_flash_max,
750*eb5a4422SLinus Walleij 			FLASH_IOUT_MIN, flash_iout_max, FLASH_IOUT_STEP);
751*eb5a4422SLinus Walleij 
752*eb5a4422SLinus Walleij 	for (i = 0; i < ARRAY_SIZE(cfg->flash_timeout_max); ++i)
753*eb5a4422SLinus Walleij 		clamp_align(&cfg->flash_timeout_max[i], FLASH_TIMEOUT_MIN,
754*eb5a4422SLinus Walleij 				FLASH_TIMEOUT_MAX, FLASH_TIMEOUT_STEP);
755*eb5a4422SLinus Walleij 
756*eb5a4422SLinus Walleij 	clamp_align(&cfg->boost_vout, FLASH_VOUT_MIN, FLASH_VOUT_MAX,
757*eb5a4422SLinus Walleij 							FLASH_VOUT_STEP);
758*eb5a4422SLinus Walleij 
759*eb5a4422SLinus Walleij 	if (cfg->low_vsys)
760*eb5a4422SLinus Walleij 		clamp_align(&cfg->low_vsys, MAX_FLASH1_VSYS_MIN,
761*eb5a4422SLinus Walleij 				MAX_FLASH1_VSYS_MAX, MAX_FLASH1_VSYS_STEP);
762*eb5a4422SLinus Walleij }
763*eb5a4422SLinus Walleij 
max77693_led_get_configuration(struct max77693_led_device * led,struct max77693_led_config_data * cfg,struct device_node ** sub_nodes)764*eb5a4422SLinus Walleij static int max77693_led_get_configuration(struct max77693_led_device *led,
765*eb5a4422SLinus Walleij 				struct max77693_led_config_data *cfg,
766*eb5a4422SLinus Walleij 				struct device_node **sub_nodes)
767*eb5a4422SLinus Walleij {
768*eb5a4422SLinus Walleij 	int ret;
769*eb5a4422SLinus Walleij 
770*eb5a4422SLinus Walleij 	ret = max77693_led_parse_dt(led, cfg, sub_nodes);
771*eb5a4422SLinus Walleij 	if (ret < 0)
772*eb5a4422SLinus Walleij 		return ret;
773*eb5a4422SLinus Walleij 
774*eb5a4422SLinus Walleij 	max77693_led_validate_configuration(led, cfg);
775*eb5a4422SLinus Walleij 
776*eb5a4422SLinus Walleij 	memcpy(led->iout_torch_max, cfg->iout_torch_max,
777*eb5a4422SLinus Walleij 				sizeof(led->iout_torch_max));
778*eb5a4422SLinus Walleij 	memcpy(led->iout_flash_max, cfg->iout_flash_max,
779*eb5a4422SLinus Walleij 				sizeof(led->iout_flash_max));
780*eb5a4422SLinus Walleij 
781*eb5a4422SLinus Walleij 	return 0;
782*eb5a4422SLinus Walleij }
783*eb5a4422SLinus Walleij 
784*eb5a4422SLinus Walleij static const struct led_flash_ops flash_ops = {
785*eb5a4422SLinus Walleij 	.flash_brightness_set	= max77693_led_flash_brightness_set,
786*eb5a4422SLinus Walleij 	.strobe_set		= max77693_led_flash_strobe_set,
787*eb5a4422SLinus Walleij 	.strobe_get		= max77693_led_flash_strobe_get,
788*eb5a4422SLinus Walleij 	.timeout_set		= max77693_led_flash_timeout_set,
789*eb5a4422SLinus Walleij 	.fault_get		= max77693_led_flash_fault_get,
790*eb5a4422SLinus Walleij };
791*eb5a4422SLinus Walleij 
max77693_init_flash_settings(struct max77693_sub_led * sub_led,struct max77693_led_config_data * led_cfg)792*eb5a4422SLinus Walleij static void max77693_init_flash_settings(struct max77693_sub_led *sub_led,
793*eb5a4422SLinus Walleij 				 struct max77693_led_config_data *led_cfg)
794*eb5a4422SLinus Walleij {
795*eb5a4422SLinus Walleij 	struct led_classdev_flash *fled_cdev = &sub_led->fled_cdev;
796*eb5a4422SLinus Walleij 	struct max77693_led_device *led = sub_led_to_led(sub_led);
797*eb5a4422SLinus Walleij 	int fled_id = sub_led->fled_id;
798*eb5a4422SLinus Walleij 	struct led_flash_setting *setting;
799*eb5a4422SLinus Walleij 
800*eb5a4422SLinus Walleij 	/* Init flash intensity setting */
801*eb5a4422SLinus Walleij 	setting = &fled_cdev->brightness;
802*eb5a4422SLinus Walleij 	setting->min = FLASH_IOUT_MIN;
803*eb5a4422SLinus Walleij 	setting->max = led->iout_joint ?
804*eb5a4422SLinus Walleij 		led_cfg->iout_flash_max[FLED1] +
805*eb5a4422SLinus Walleij 		led_cfg->iout_flash_max[FLED2] :
806*eb5a4422SLinus Walleij 		led_cfg->iout_flash_max[fled_id];
807*eb5a4422SLinus Walleij 	setting->step = FLASH_IOUT_STEP;
808*eb5a4422SLinus Walleij 	setting->val = setting->max;
809*eb5a4422SLinus Walleij 
810*eb5a4422SLinus Walleij 	/* Init flash timeout setting */
811*eb5a4422SLinus Walleij 	setting = &fled_cdev->timeout;
812*eb5a4422SLinus Walleij 	setting->min = FLASH_TIMEOUT_MIN;
813*eb5a4422SLinus Walleij 	setting->max = led_cfg->flash_timeout_max[fled_id];
814*eb5a4422SLinus Walleij 	setting->step = FLASH_TIMEOUT_STEP;
815*eb5a4422SLinus Walleij 	setting->val = setting->max;
816*eb5a4422SLinus Walleij }
817*eb5a4422SLinus Walleij 
818*eb5a4422SLinus Walleij #if IS_ENABLED(CONFIG_V4L2_FLASH_LED_CLASS)
819*eb5a4422SLinus Walleij 
max77693_led_external_strobe_set(struct v4l2_flash * v4l2_flash,bool enable)820*eb5a4422SLinus Walleij static int max77693_led_external_strobe_set(
821*eb5a4422SLinus Walleij 				struct v4l2_flash *v4l2_flash,
822*eb5a4422SLinus Walleij 				bool enable)
823*eb5a4422SLinus Walleij {
824*eb5a4422SLinus Walleij 	struct max77693_sub_led *sub_led =
825*eb5a4422SLinus Walleij 				flcdev_to_sub_led(v4l2_flash->fled_cdev);
826*eb5a4422SLinus Walleij 	struct max77693_led_device *led = sub_led_to_led(sub_led);
827*eb5a4422SLinus Walleij 	int fled_id = sub_led->fled_id;
828*eb5a4422SLinus Walleij 	int ret;
829*eb5a4422SLinus Walleij 
830*eb5a4422SLinus Walleij 	mutex_lock(&led->lock);
831*eb5a4422SLinus Walleij 
832*eb5a4422SLinus Walleij 	if (enable)
833*eb5a4422SLinus Walleij 		ret = max77693_add_mode(led, MODE_FLASH_EXTERNAL(fled_id));
834*eb5a4422SLinus Walleij 	else
835*eb5a4422SLinus Walleij 		ret = max77693_clear_mode(led, MODE_FLASH_EXTERNAL(fled_id));
836*eb5a4422SLinus Walleij 
837*eb5a4422SLinus Walleij 	mutex_unlock(&led->lock);
838*eb5a4422SLinus Walleij 
839*eb5a4422SLinus Walleij 	return ret;
840*eb5a4422SLinus Walleij }
841*eb5a4422SLinus Walleij 
max77693_init_v4l2_flash_config(struct max77693_sub_led * sub_led,struct max77693_led_config_data * led_cfg,struct v4l2_flash_config * v4l2_sd_cfg)842*eb5a4422SLinus Walleij static void max77693_init_v4l2_flash_config(struct max77693_sub_led *sub_led,
843*eb5a4422SLinus Walleij 				struct max77693_led_config_data *led_cfg,
844*eb5a4422SLinus Walleij 				struct v4l2_flash_config *v4l2_sd_cfg)
845*eb5a4422SLinus Walleij {
846*eb5a4422SLinus Walleij 	struct max77693_led_device *led = sub_led_to_led(sub_led);
847*eb5a4422SLinus Walleij 	struct device *dev = &led->pdev->dev;
848*eb5a4422SLinus Walleij 	struct max77693_dev *iodev = dev_get_drvdata(dev->parent);
849*eb5a4422SLinus Walleij 	struct i2c_client *i2c = iodev->i2c;
850*eb5a4422SLinus Walleij 	struct led_flash_setting *s;
851*eb5a4422SLinus Walleij 
852*eb5a4422SLinus Walleij 	snprintf(v4l2_sd_cfg->dev_name, sizeof(v4l2_sd_cfg->dev_name),
853*eb5a4422SLinus Walleij 		 "%s %d-%04x", sub_led->fled_cdev.led_cdev.name,
854*eb5a4422SLinus Walleij 		 i2c_adapter_id(i2c->adapter), i2c->addr);
855*eb5a4422SLinus Walleij 
856*eb5a4422SLinus Walleij 	s = &v4l2_sd_cfg->intensity;
857*eb5a4422SLinus Walleij 	s->min = TORCH_IOUT_MIN;
858*eb5a4422SLinus Walleij 	s->max = sub_led->fled_cdev.led_cdev.max_brightness * TORCH_IOUT_STEP;
859*eb5a4422SLinus Walleij 	s->step = TORCH_IOUT_STEP;
860*eb5a4422SLinus Walleij 	s->val = s->max;
861*eb5a4422SLinus Walleij 
862*eb5a4422SLinus Walleij 	/* Init flash faults config */
863*eb5a4422SLinus Walleij 	v4l2_sd_cfg->flash_faults = LED_FAULT_OVER_VOLTAGE |
864*eb5a4422SLinus Walleij 				LED_FAULT_SHORT_CIRCUIT |
865*eb5a4422SLinus Walleij 				LED_FAULT_OVER_CURRENT;
866*eb5a4422SLinus Walleij 
867*eb5a4422SLinus Walleij 	v4l2_sd_cfg->has_external_strobe = true;
868*eb5a4422SLinus Walleij }
869*eb5a4422SLinus Walleij 
870*eb5a4422SLinus Walleij static const struct v4l2_flash_ops v4l2_flash_ops = {
871*eb5a4422SLinus Walleij 	.external_strobe_set = max77693_led_external_strobe_set,
872*eb5a4422SLinus Walleij };
873*eb5a4422SLinus Walleij #else
max77693_init_v4l2_flash_config(struct max77693_sub_led * sub_led,struct max77693_led_config_data * led_cfg,struct v4l2_flash_config * v4l2_sd_cfg)874*eb5a4422SLinus Walleij static inline void max77693_init_v4l2_flash_config(
875*eb5a4422SLinus Walleij 				struct max77693_sub_led *sub_led,
876*eb5a4422SLinus Walleij 				struct max77693_led_config_data *led_cfg,
877*eb5a4422SLinus Walleij 				struct v4l2_flash_config *v4l2_sd_cfg)
878*eb5a4422SLinus Walleij {
879*eb5a4422SLinus Walleij }
880*eb5a4422SLinus Walleij static const struct v4l2_flash_ops v4l2_flash_ops;
881*eb5a4422SLinus Walleij #endif
882*eb5a4422SLinus Walleij 
max77693_init_fled_cdev(struct max77693_sub_led * sub_led,struct max77693_led_config_data * led_cfg)883*eb5a4422SLinus Walleij static void max77693_init_fled_cdev(struct max77693_sub_led *sub_led,
884*eb5a4422SLinus Walleij 				struct max77693_led_config_data *led_cfg)
885*eb5a4422SLinus Walleij {
886*eb5a4422SLinus Walleij 	struct max77693_led_device *led = sub_led_to_led(sub_led);
887*eb5a4422SLinus Walleij 	int fled_id = sub_led->fled_id;
888*eb5a4422SLinus Walleij 	struct led_classdev_flash *fled_cdev;
889*eb5a4422SLinus Walleij 	struct led_classdev *led_cdev;
890*eb5a4422SLinus Walleij 
891*eb5a4422SLinus Walleij 	/* Initialize LED Flash class device */
892*eb5a4422SLinus Walleij 	fled_cdev = &sub_led->fled_cdev;
893*eb5a4422SLinus Walleij 	fled_cdev->ops = &flash_ops;
894*eb5a4422SLinus Walleij 	led_cdev = &fled_cdev->led_cdev;
895*eb5a4422SLinus Walleij 
896*eb5a4422SLinus Walleij 	led_cdev->name = led_cfg->label[fled_id];
897*eb5a4422SLinus Walleij 
898*eb5a4422SLinus Walleij 	led_cdev->brightness_set_blocking = max77693_led_brightness_set;
899*eb5a4422SLinus Walleij 	led_cdev->max_brightness = (led->iout_joint ?
900*eb5a4422SLinus Walleij 					led_cfg->iout_torch_max[FLED1] +
901*eb5a4422SLinus Walleij 					led_cfg->iout_torch_max[FLED2] :
902*eb5a4422SLinus Walleij 					led_cfg->iout_torch_max[fled_id]) /
903*eb5a4422SLinus Walleij 				   TORCH_IOUT_STEP;
904*eb5a4422SLinus Walleij 	led_cdev->flags |= LED_DEV_CAP_FLASH;
905*eb5a4422SLinus Walleij 
906*eb5a4422SLinus Walleij 	max77693_init_flash_settings(sub_led, led_cfg);
907*eb5a4422SLinus Walleij 
908*eb5a4422SLinus Walleij 	/* Init flash timeout cache */
909*eb5a4422SLinus Walleij 	sub_led->flash_timeout = fled_cdev->timeout.val;
910*eb5a4422SLinus Walleij }
911*eb5a4422SLinus Walleij 
max77693_register_led(struct max77693_sub_led * sub_led,struct max77693_led_config_data * led_cfg,struct device_node * sub_node)912*eb5a4422SLinus Walleij static int max77693_register_led(struct max77693_sub_led *sub_led,
913*eb5a4422SLinus Walleij 				 struct max77693_led_config_data *led_cfg,
914*eb5a4422SLinus Walleij 				 struct device_node *sub_node)
915*eb5a4422SLinus Walleij {
916*eb5a4422SLinus Walleij 	struct max77693_led_device *led = sub_led_to_led(sub_led);
917*eb5a4422SLinus Walleij 	struct led_classdev_flash *fled_cdev = &sub_led->fled_cdev;
918*eb5a4422SLinus Walleij 	struct device *dev = &led->pdev->dev;
919*eb5a4422SLinus Walleij 	struct v4l2_flash_config v4l2_sd_cfg = {};
920*eb5a4422SLinus Walleij 	int ret;
921*eb5a4422SLinus Walleij 
922*eb5a4422SLinus Walleij 	/* Register in the LED subsystem */
923*eb5a4422SLinus Walleij 	ret = led_classdev_flash_register(dev, fled_cdev);
924*eb5a4422SLinus Walleij 	if (ret < 0)
925*eb5a4422SLinus Walleij 		return ret;
926*eb5a4422SLinus Walleij 
927*eb5a4422SLinus Walleij 	max77693_init_v4l2_flash_config(sub_led, led_cfg, &v4l2_sd_cfg);
928*eb5a4422SLinus Walleij 
929*eb5a4422SLinus Walleij 	/* Register in the V4L2 subsystem. */
930*eb5a4422SLinus Walleij 	sub_led->v4l2_flash = v4l2_flash_init(dev, of_fwnode_handle(sub_node),
931*eb5a4422SLinus Walleij 					      fled_cdev, &v4l2_flash_ops,
932*eb5a4422SLinus Walleij 					      &v4l2_sd_cfg);
933*eb5a4422SLinus Walleij 	if (IS_ERR(sub_led->v4l2_flash)) {
934*eb5a4422SLinus Walleij 		ret = PTR_ERR(sub_led->v4l2_flash);
935*eb5a4422SLinus Walleij 		goto err_v4l2_flash_init;
936*eb5a4422SLinus Walleij 	}
937*eb5a4422SLinus Walleij 
938*eb5a4422SLinus Walleij 	return 0;
939*eb5a4422SLinus Walleij 
940*eb5a4422SLinus Walleij err_v4l2_flash_init:
941*eb5a4422SLinus Walleij 	led_classdev_flash_unregister(fled_cdev);
942*eb5a4422SLinus Walleij 	return ret;
943*eb5a4422SLinus Walleij }
944*eb5a4422SLinus Walleij 
max77693_led_probe(struct platform_device * pdev)945*eb5a4422SLinus Walleij static int max77693_led_probe(struct platform_device *pdev)
946*eb5a4422SLinus Walleij {
947*eb5a4422SLinus Walleij 	struct device *dev = &pdev->dev;
948*eb5a4422SLinus Walleij 	struct max77693_dev *iodev = dev_get_drvdata(dev->parent);
949*eb5a4422SLinus Walleij 	struct max77693_led_device *led;
950*eb5a4422SLinus Walleij 	struct max77693_sub_led *sub_leds;
951*eb5a4422SLinus Walleij 	struct device_node *sub_nodes[2] = {};
952*eb5a4422SLinus Walleij 	struct max77693_led_config_data led_cfg = {};
953*eb5a4422SLinus Walleij 	int init_fled_cdev[2], i, ret;
954*eb5a4422SLinus Walleij 
955*eb5a4422SLinus Walleij 	led = devm_kzalloc(dev, sizeof(*led), GFP_KERNEL);
956*eb5a4422SLinus Walleij 	if (!led)
957*eb5a4422SLinus Walleij 		return -ENOMEM;
958*eb5a4422SLinus Walleij 
959*eb5a4422SLinus Walleij 	led->pdev = pdev;
960*eb5a4422SLinus Walleij 	led->regmap = iodev->regmap;
961*eb5a4422SLinus Walleij 	led->allowed_modes = MODE_FLASH_MASK;
962*eb5a4422SLinus Walleij 	sub_leds = led->sub_leds;
963*eb5a4422SLinus Walleij 
964*eb5a4422SLinus Walleij 	platform_set_drvdata(pdev, led);
965*eb5a4422SLinus Walleij 	ret = max77693_led_get_configuration(led, &led_cfg, sub_nodes);
966*eb5a4422SLinus Walleij 	if (ret < 0)
967*eb5a4422SLinus Walleij 		return ret;
968*eb5a4422SLinus Walleij 
969*eb5a4422SLinus Walleij 	ret = max77693_setup(led, &led_cfg);
970*eb5a4422SLinus Walleij 	if (ret < 0)
971*eb5a4422SLinus Walleij 		return ret;
972*eb5a4422SLinus Walleij 
973*eb5a4422SLinus Walleij 	mutex_init(&led->lock);
974*eb5a4422SLinus Walleij 
975*eb5a4422SLinus Walleij 	init_fled_cdev[FLED1] =
976*eb5a4422SLinus Walleij 			led->iout_joint || max77693_fled_used(led, FLED1);
977*eb5a4422SLinus Walleij 	init_fled_cdev[FLED2] =
978*eb5a4422SLinus Walleij 			!led->iout_joint && max77693_fled_used(led, FLED2);
979*eb5a4422SLinus Walleij 
980*eb5a4422SLinus Walleij 	for (i = FLED1; i <= FLED2; ++i) {
981*eb5a4422SLinus Walleij 		if (!init_fled_cdev[i])
982*eb5a4422SLinus Walleij 			continue;
983*eb5a4422SLinus Walleij 
984*eb5a4422SLinus Walleij 		/* Initialize LED Flash class device */
985*eb5a4422SLinus Walleij 		max77693_init_fled_cdev(&sub_leds[i], &led_cfg);
986*eb5a4422SLinus Walleij 
987*eb5a4422SLinus Walleij 		/*
988*eb5a4422SLinus Walleij 		 * Register LED Flash class device and corresponding
989*eb5a4422SLinus Walleij 		 * V4L2 Flash device.
990*eb5a4422SLinus Walleij 		 */
991*eb5a4422SLinus Walleij 		ret = max77693_register_led(&sub_leds[i], &led_cfg,
992*eb5a4422SLinus Walleij 						sub_nodes[i]);
993*eb5a4422SLinus Walleij 		if (ret < 0) {
994*eb5a4422SLinus Walleij 			/*
995*eb5a4422SLinus Walleij 			 * At this moment FLED1 might have been already
996*eb5a4422SLinus Walleij 			 * registered and it needs to be released.
997*eb5a4422SLinus Walleij 			 */
998*eb5a4422SLinus Walleij 			if (i == FLED2)
999*eb5a4422SLinus Walleij 				goto err_register_led2;
1000*eb5a4422SLinus Walleij 			else
1001*eb5a4422SLinus Walleij 				goto err_register_led1;
1002*eb5a4422SLinus Walleij 		}
1003*eb5a4422SLinus Walleij 	}
1004*eb5a4422SLinus Walleij 
1005*eb5a4422SLinus Walleij 	return 0;
1006*eb5a4422SLinus Walleij 
1007*eb5a4422SLinus Walleij err_register_led2:
1008*eb5a4422SLinus Walleij 	/* It is possible than only FLED2 was to be registered */
1009*eb5a4422SLinus Walleij 	if (!init_fled_cdev[FLED1])
1010*eb5a4422SLinus Walleij 		goto err_register_led1;
1011*eb5a4422SLinus Walleij 	v4l2_flash_release(sub_leds[FLED1].v4l2_flash);
1012*eb5a4422SLinus Walleij 	led_classdev_flash_unregister(&sub_leds[FLED1].fled_cdev);
1013*eb5a4422SLinus Walleij err_register_led1:
1014*eb5a4422SLinus Walleij 	mutex_destroy(&led->lock);
1015*eb5a4422SLinus Walleij 
1016*eb5a4422SLinus Walleij 	return ret;
1017*eb5a4422SLinus Walleij }
1018*eb5a4422SLinus Walleij 
max77693_led_remove(struct platform_device * pdev)1019*eb5a4422SLinus Walleij static int max77693_led_remove(struct platform_device *pdev)
1020*eb5a4422SLinus Walleij {
1021*eb5a4422SLinus Walleij 	struct max77693_led_device *led = platform_get_drvdata(pdev);
1022*eb5a4422SLinus Walleij 	struct max77693_sub_led *sub_leds = led->sub_leds;
1023*eb5a4422SLinus Walleij 
1024*eb5a4422SLinus Walleij 	if (led->iout_joint || max77693_fled_used(led, FLED1)) {
1025*eb5a4422SLinus Walleij 		v4l2_flash_release(sub_leds[FLED1].v4l2_flash);
1026*eb5a4422SLinus Walleij 		led_classdev_flash_unregister(&sub_leds[FLED1].fled_cdev);
1027*eb5a4422SLinus Walleij 	}
1028*eb5a4422SLinus Walleij 
1029*eb5a4422SLinus Walleij 	if (!led->iout_joint && max77693_fled_used(led, FLED2)) {
1030*eb5a4422SLinus Walleij 		v4l2_flash_release(sub_leds[FLED2].v4l2_flash);
1031*eb5a4422SLinus Walleij 		led_classdev_flash_unregister(&sub_leds[FLED2].fled_cdev);
1032*eb5a4422SLinus Walleij 	}
1033*eb5a4422SLinus Walleij 
1034*eb5a4422SLinus Walleij 	mutex_destroy(&led->lock);
1035*eb5a4422SLinus Walleij 
1036*eb5a4422SLinus Walleij 	return 0;
1037*eb5a4422SLinus Walleij }
1038*eb5a4422SLinus Walleij 
1039*eb5a4422SLinus Walleij static const struct of_device_id max77693_led_dt_match[] = {
1040*eb5a4422SLinus Walleij 	{ .compatible = "maxim,max77693-led" },
1041*eb5a4422SLinus Walleij 	{},
1042*eb5a4422SLinus Walleij };
1043*eb5a4422SLinus Walleij MODULE_DEVICE_TABLE(of, max77693_led_dt_match);
1044*eb5a4422SLinus Walleij 
1045*eb5a4422SLinus Walleij static struct platform_driver max77693_led_driver = {
1046*eb5a4422SLinus Walleij 	.probe		= max77693_led_probe,
1047*eb5a4422SLinus Walleij 	.remove		= max77693_led_remove,
1048*eb5a4422SLinus Walleij 	.driver		= {
1049*eb5a4422SLinus Walleij 		.name	= "max77693-led",
1050*eb5a4422SLinus Walleij 		.of_match_table = max77693_led_dt_match,
1051*eb5a4422SLinus Walleij 	},
1052*eb5a4422SLinus Walleij };
1053*eb5a4422SLinus Walleij 
1054*eb5a4422SLinus Walleij module_platform_driver(max77693_led_driver);
1055*eb5a4422SLinus Walleij 
1056*eb5a4422SLinus Walleij MODULE_AUTHOR("Jacek Anaszewski <j.anaszewski@samsung.com>");
1057*eb5a4422SLinus Walleij MODULE_AUTHOR("Andrzej Hajda <a.hajda@samsung.com>");
1058*eb5a4422SLinus Walleij MODULE_DESCRIPTION("Maxim MAX77693 led flash driver");
1059*eb5a4422SLinus Walleij MODULE_LICENSE("GPL v2");
1060