xref: /linux/drivers/leds/flash/leds-qcom-flash.c (revision f9bff0e31881d03badf191d3b0005839391f5f2b)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved.
4  */
5 
6 #include <linux/bitfield.h>
7 #include <linux/bits.h>
8 #include <linux/leds.h>
9 #include <linux/led-class-flash.h>
10 #include <linux/module.h>
11 #include <linux/platform_device.h>
12 #include <linux/property.h>
13 #include <linux/regmap.h>
14 #include <media/v4l2-flash-led-class.h>
15 
16 /* registers definitions */
17 #define FLASH_TYPE_REG			0x04
18 #define FLASH_TYPE_VAL			0x18
19 
20 #define FLASH_SUBTYPE_REG		0x05
21 #define FLASH_SUBTYPE_3CH_PM8150_VAL	0x04
22 #define FLASH_SUBTYPE_3CH_PMI8998_VAL	0x03
23 #define FLASH_SUBTYPE_4CH_VAL		0x07
24 
25 #define FLASH_STS_3CH_OTST1		BIT(0)
26 #define FLASH_STS_3CH_OTST2		BIT(1)
27 #define FLASH_STS_3CH_OTST3		BIT(2)
28 #define FLASH_STS_3CH_BOB_THM_OVERLOAD	BIT(3)
29 #define FLASH_STS_3CH_VPH_DROOP		BIT(4)
30 #define FLASH_STS_3CH_BOB_ILIM_S1	BIT(5)
31 #define FLASH_STS_3CH_BOB_ILIM_S2	BIT(6)
32 #define FLASH_STS_3CH_BCL_IBAT		BIT(7)
33 
34 #define FLASH_STS_4CH_VPH_LOW		BIT(0)
35 #define FLASH_STS_4CH_BCL_IBAT		BIT(1)
36 #define FLASH_STS_4CH_BOB_ILIM_S1	BIT(2)
37 #define FLASH_STS_4CH_BOB_ILIM_S2	BIT(3)
38 #define FLASH_STS_4CH_OTST2		BIT(4)
39 #define FLASH_STS_4CH_OTST1		BIT(5)
40 #define FLASH_STS_4CHG_BOB_THM_OVERLOAD	BIT(6)
41 
42 #define FLASH_TIMER_EN_BIT		BIT(7)
43 #define FLASH_TIMER_VAL_MASK		GENMASK(6, 0)
44 #define FLASH_TIMER_STEP_MS		10
45 
46 #define FLASH_STROBE_HW_SW_SEL_BIT	BIT(2)
47 #define SW_STROBE_VAL			0
48 #define HW_STROBE_VAL			1
49 #define FLASH_HW_STROBE_TRIGGER_SEL_BIT	BIT(1)
50 #define STROBE_LEVEL_TRIGGER_VAL	0
51 #define STROBE_EDGE_TRIGGER_VAL		1
52 #define FLASH_STROBE_POLARITY_BIT	BIT(0)
53 #define STROBE_ACTIVE_HIGH_VAL		1
54 
55 #define FLASH_IRES_MASK_4CH		BIT(0)
56 #define FLASH_IRES_MASK_3CH		GENMASK(1, 0)
57 #define FLASH_IRES_12P5MA_VAL		0
58 #define FLASH_IRES_5MA_VAL_4CH		1
59 #define FLASH_IRES_5MA_VAL_3CH		3
60 
61 /* constants */
62 #define FLASH_CURRENT_MAX_UA		1500000
63 #define TORCH_CURRENT_MAX_UA		500000
64 #define FLASH_TOTAL_CURRENT_MAX_UA	2000000
65 #define FLASH_CURRENT_DEFAULT_UA	1000000
66 #define TORCH_CURRENT_DEFAULT_UA	200000
67 
68 #define TORCH_IRES_UA			5000
69 #define FLASH_IRES_UA			12500
70 
71 #define FLASH_TIMEOUT_MAX_US		1280000
72 #define FLASH_TIMEOUT_STEP_US		10000
73 
74 #define UA_PER_MA			1000
75 
76 enum hw_type {
77 	QCOM_MVFLASH_3CH,
78 	QCOM_MVFLASH_4CH,
79 };
80 
81 enum led_mode {
82 	FLASH_MODE,
83 	TORCH_MODE,
84 };
85 
86 enum led_strobe {
87 	SW_STROBE,
88 	HW_STROBE,
89 };
90 
91 enum {
92 	REG_STATUS1,
93 	REG_STATUS2,
94 	REG_STATUS3,
95 	REG_CHAN_TIMER,
96 	REG_ITARGET,
97 	REG_MODULE_EN,
98 	REG_IRESOLUTION,
99 	REG_CHAN_STROBE,
100 	REG_CHAN_EN,
101 	REG_MAX_COUNT,
102 };
103 
104 static struct reg_field mvflash_3ch_regs[REG_MAX_COUNT] = {
105 	REG_FIELD(0x08, 0, 7),			/* status1	*/
106 	REG_FIELD(0x09, 0, 7),                  /* status2	*/
107 	REG_FIELD(0x0a, 0, 7),                  /* status3	*/
108 	REG_FIELD_ID(0x40, 0, 7, 3, 1),         /* chan_timer	*/
109 	REG_FIELD_ID(0x43, 0, 6, 3, 1),         /* itarget	*/
110 	REG_FIELD(0x46, 7, 7),                  /* module_en	*/
111 	REG_FIELD(0x47, 0, 5),                  /* iresolution	*/
112 	REG_FIELD_ID(0x49, 0, 2, 3, 1),         /* chan_strobe	*/
113 	REG_FIELD(0x4c, 0, 2),                  /* chan_en	*/
114 };
115 
116 static struct reg_field mvflash_4ch_regs[REG_MAX_COUNT] = {
117 	REG_FIELD(0x06, 0, 7),			/* status1	*/
118 	REG_FIELD(0x07, 0, 6),			/* status2	*/
119 	REG_FIELD(0x09, 0, 7),			/* status3	*/
120 	REG_FIELD_ID(0x3e, 0, 7, 4, 1),		/* chan_timer	*/
121 	REG_FIELD_ID(0x42, 0, 6, 4, 1),		/* itarget	*/
122 	REG_FIELD(0x46, 7, 7),			/* module_en	*/
123 	REG_FIELD(0x49, 0, 3),			/* iresolution	*/
124 	REG_FIELD_ID(0x4a, 0, 6, 4, 1),		/* chan_strobe	*/
125 	REG_FIELD(0x4e, 0, 3),			/* chan_en	*/
126 };
127 
128 struct qcom_flash_data {
129 	struct v4l2_flash	**v4l2_flash;
130 	struct regmap_field     *r_fields[REG_MAX_COUNT];
131 	struct mutex		lock;
132 	enum hw_type		hw_type;
133 	u8			leds_count;
134 	u8			max_channels;
135 	u8			chan_en_bits;
136 };
137 
138 struct qcom_flash_led {
139 	struct qcom_flash_data		*flash_data;
140 	struct led_classdev_flash	flash;
141 	u32				max_flash_current_ma;
142 	u32				max_torch_current_ma;
143 	u32				max_timeout_ms;
144 	u32				flash_current_ma;
145 	u32				flash_timeout_ms;
146 	u8				*chan_id;
147 	u8				chan_count;
148 	bool				enabled;
149 };
150 
151 static int set_flash_module_en(struct qcom_flash_led *led, bool en)
152 {
153 	struct qcom_flash_data *flash_data = led->flash_data;
154 	u8 led_mask = 0, enable;
155 	int i, rc;
156 
157 	for (i = 0; i < led->chan_count; i++)
158 		led_mask |= BIT(led->chan_id[i]);
159 
160 	mutex_lock(&flash_data->lock);
161 	if (en)
162 		flash_data->chan_en_bits |= led_mask;
163 	else
164 		flash_data->chan_en_bits &= ~led_mask;
165 
166 	enable = !!flash_data->chan_en_bits;
167 	rc = regmap_field_write(flash_data->r_fields[REG_MODULE_EN], enable);
168 	if (rc)
169 		dev_err(led->flash.led_cdev.dev, "write module_en failed, rc=%d\n", rc);
170 	mutex_unlock(&flash_data->lock);
171 
172 	return rc;
173 }
174 
175 static int set_flash_current(struct qcom_flash_led *led, u32 current_ma, enum led_mode mode)
176 {
177 	struct qcom_flash_data *flash_data = led->flash_data;
178 	u32 itarg_ua, ires_ua;
179 	u8 shift, ires_mask = 0, ires_val = 0, chan_id;
180 	int i, rc;
181 
182 	/*
183 	 * Split the current across the channels and set the
184 	 * IRESOLUTION and ITARGET registers accordingly.
185 	 */
186 	itarg_ua = (current_ma * UA_PER_MA) / led->chan_count + 1;
187 	ires_ua = (mode == FLASH_MODE) ? FLASH_IRES_UA : TORCH_IRES_UA;
188 
189 	for (i = 0; i < led->chan_count; i++) {
190 		u8 itarget = 0;
191 
192 		if (itarg_ua > ires_ua)
193 			itarget = itarg_ua / ires_ua - 1;
194 
195 		chan_id = led->chan_id[i];
196 
197 		rc = regmap_fields_write(flash_data->r_fields[REG_ITARGET], chan_id, itarget);
198 		if (rc)
199 			return rc;
200 
201 		if (flash_data->hw_type == QCOM_MVFLASH_3CH) {
202 			shift = chan_id * 2;
203 			ires_mask |= FLASH_IRES_MASK_3CH << shift;
204 			ires_val |= ((mode == FLASH_MODE) ?
205 				(FLASH_IRES_12P5MA_VAL << shift) :
206 				(FLASH_IRES_5MA_VAL_3CH << shift));
207 		} else if (flash_data->hw_type == QCOM_MVFLASH_4CH) {
208 			shift = chan_id;
209 			ires_mask |= FLASH_IRES_MASK_4CH << shift;
210 			ires_val |= ((mode == FLASH_MODE) ?
211 				(FLASH_IRES_12P5MA_VAL << shift) :
212 				(FLASH_IRES_5MA_VAL_4CH << shift));
213 		} else {
214 			dev_err(led->flash.led_cdev.dev,
215 					"HW type %d is not supported\n", flash_data->hw_type);
216 			return -EOPNOTSUPP;
217 		}
218 	}
219 
220 	return regmap_field_update_bits(flash_data->r_fields[REG_IRESOLUTION], ires_mask, ires_val);
221 }
222 
223 static int set_flash_timeout(struct qcom_flash_led *led, u32 timeout_ms)
224 {
225 	struct qcom_flash_data *flash_data = led->flash_data;
226 	u8 timer, chan_id;
227 	int rc, i;
228 
229 	/* set SAFETY_TIMER for all the channels connected to the same LED */
230 	timeout_ms = min_t(u32, timeout_ms, led->max_timeout_ms);
231 
232 	for (i = 0; i < led->chan_count; i++) {
233 		chan_id = led->chan_id[i];
234 
235 		timer = timeout_ms / FLASH_TIMER_STEP_MS;
236 		timer = clamp_t(u8, timer, 0, FLASH_TIMER_VAL_MASK);
237 
238 		if (timeout_ms)
239 			timer |= FLASH_TIMER_EN_BIT;
240 
241 		rc = regmap_fields_write(flash_data->r_fields[REG_CHAN_TIMER], chan_id, timer);
242 		if (rc)
243 			return rc;
244 	}
245 
246 	return 0;
247 }
248 
249 static int set_flash_strobe(struct qcom_flash_led *led, enum led_strobe strobe, bool state)
250 {
251 	struct qcom_flash_data *flash_data = led->flash_data;
252 	u8 strobe_sel, chan_en, chan_id, chan_mask = 0;
253 	int rc, i;
254 
255 	/* Set SW strobe config for all channels connected to the LED */
256 	for (i = 0; i < led->chan_count; i++) {
257 		chan_id = led->chan_id[i];
258 
259 		if (strobe == SW_STROBE)
260 			strobe_sel = FIELD_PREP(FLASH_STROBE_HW_SW_SEL_BIT, SW_STROBE_VAL);
261 		else
262 			strobe_sel = FIELD_PREP(FLASH_STROBE_HW_SW_SEL_BIT, HW_STROBE_VAL);
263 
264 		strobe_sel |=
265 			FIELD_PREP(FLASH_HW_STROBE_TRIGGER_SEL_BIT, STROBE_LEVEL_TRIGGER_VAL) |
266 			FIELD_PREP(FLASH_STROBE_POLARITY_BIT, STROBE_ACTIVE_HIGH_VAL);
267 
268 		rc = regmap_fields_write(
269 				flash_data->r_fields[REG_CHAN_STROBE], chan_id, strobe_sel);
270 		if (rc)
271 			return rc;
272 
273 		chan_mask |= BIT(chan_id);
274 	}
275 
276 	/* Enable/disable flash channels */
277 	chan_en = state ? chan_mask : 0;
278 	rc = regmap_field_update_bits(flash_data->r_fields[REG_CHAN_EN], chan_mask, chan_en);
279 	if (rc)
280 		return rc;
281 
282 	led->enabled = state;
283 	return 0;
284 }
285 
286 static inline struct qcom_flash_led *flcdev_to_qcom_fled(struct led_classdev_flash *flcdev)
287 {
288 	return container_of(flcdev, struct qcom_flash_led, flash);
289 }
290 
291 static int qcom_flash_brightness_set(struct led_classdev_flash *fled_cdev, u32 brightness)
292 {
293 	struct qcom_flash_led *led = flcdev_to_qcom_fled(fled_cdev);
294 
295 	led->flash_current_ma = min_t(u32, led->max_flash_current_ma, brightness / UA_PER_MA);
296 	return 0;
297 }
298 
299 static int qcom_flash_timeout_set(struct led_classdev_flash *fled_cdev, u32 timeout)
300 {
301 	struct qcom_flash_led *led = flcdev_to_qcom_fled(fled_cdev);
302 
303 	led->flash_timeout_ms = timeout / USEC_PER_MSEC;
304 	return 0;
305 }
306 
307 static int qcom_flash_strobe_set(struct led_classdev_flash *fled_cdev, bool state)
308 {
309 	struct qcom_flash_led *led = flcdev_to_qcom_fled(fled_cdev);
310 	int rc;
311 
312 	rc = set_flash_current(led, led->flash_current_ma, FLASH_MODE);
313 	if (rc)
314 		return rc;
315 
316 	rc = set_flash_timeout(led, led->flash_timeout_ms);
317 	if (rc)
318 		return rc;
319 
320 	rc = set_flash_module_en(led, state);
321 	if (rc)
322 		return rc;
323 
324 	return set_flash_strobe(led, SW_STROBE, state);
325 }
326 
327 static int qcom_flash_strobe_get(struct led_classdev_flash *fled_cdev, bool *state)
328 {
329 	struct qcom_flash_led *led = flcdev_to_qcom_fled(fled_cdev);
330 
331 	*state = led->enabled;
332 	return 0;
333 }
334 
335 static int qcom_flash_fault_get(struct led_classdev_flash *fled_cdev, u32 *fault)
336 {
337 	struct qcom_flash_led *led = flcdev_to_qcom_fled(fled_cdev);
338 	struct qcom_flash_data *flash_data = led->flash_data;
339 	u8 shift, chan_id, chan_mask = 0;
340 	u8 ot_mask = 0, oc_mask = 0, uv_mask = 0;
341 	u32 val, fault_sts = 0;
342 	int i, rc;
343 
344 	rc = regmap_field_read(flash_data->r_fields[REG_STATUS1], &val);
345 	if (rc)
346 		return rc;
347 
348 	for (i = 0; i < led->chan_count; i++) {
349 		chan_id = led->chan_id[i];
350 		shift = chan_id * 2;
351 
352 		if (val & BIT(shift))
353 			fault_sts |= LED_FAULT_SHORT_CIRCUIT;
354 
355 		chan_mask |= BIT(chan_id);
356 	}
357 
358 	rc = regmap_field_read(flash_data->r_fields[REG_STATUS2], &val);
359 	if (rc)
360 		return rc;
361 
362 	if (flash_data->hw_type == QCOM_MVFLASH_3CH) {
363 		ot_mask = FLASH_STS_3CH_OTST1 |
364 			  FLASH_STS_3CH_OTST2 |
365 			  FLASH_STS_3CH_OTST3 |
366 			  FLASH_STS_3CH_BOB_THM_OVERLOAD;
367 		oc_mask = FLASH_STS_3CH_BOB_ILIM_S1 |
368 			  FLASH_STS_3CH_BOB_ILIM_S2 |
369 			  FLASH_STS_3CH_BCL_IBAT;
370 		uv_mask = FLASH_STS_3CH_VPH_DROOP;
371 	} else if (flash_data->hw_type == QCOM_MVFLASH_4CH) {
372 		ot_mask = FLASH_STS_4CH_OTST2 |
373 			  FLASH_STS_4CH_OTST1 |
374 			  FLASH_STS_4CHG_BOB_THM_OVERLOAD;
375 		oc_mask = FLASH_STS_4CH_BCL_IBAT |
376 			  FLASH_STS_4CH_BOB_ILIM_S1 |
377 			  FLASH_STS_4CH_BOB_ILIM_S2;
378 		uv_mask = FLASH_STS_4CH_VPH_LOW;
379 	}
380 
381 	if (val & ot_mask)
382 		fault_sts |= LED_FAULT_OVER_TEMPERATURE;
383 
384 	if (val & oc_mask)
385 		fault_sts |= LED_FAULT_OVER_CURRENT;
386 
387 	if (val & uv_mask)
388 		fault_sts |= LED_FAULT_INPUT_VOLTAGE;
389 
390 	rc = regmap_field_read(flash_data->r_fields[REG_STATUS3], &val);
391 	if (rc)
392 		return rc;
393 
394 	if (flash_data->hw_type == QCOM_MVFLASH_3CH) {
395 		if (val & chan_mask)
396 			fault_sts |= LED_FAULT_TIMEOUT;
397 	} else if (flash_data->hw_type == QCOM_MVFLASH_4CH) {
398 		for (i = 0; i < led->chan_count; i++) {
399 			chan_id = led->chan_id[i];
400 			shift = chan_id * 2;
401 
402 			if (val & BIT(shift))
403 				fault_sts |= LED_FAULT_TIMEOUT;
404 		}
405 	}
406 
407 	*fault = fault_sts;
408 	return 0;
409 }
410 
411 static int qcom_flash_led_brightness_set(struct led_classdev *led_cdev,
412 					enum led_brightness brightness)
413 {
414 	struct led_classdev_flash *fled_cdev = lcdev_to_flcdev(led_cdev);
415 	struct qcom_flash_led *led = flcdev_to_qcom_fled(fled_cdev);
416 	u32 current_ma = brightness * led->max_torch_current_ma / LED_FULL;
417 	bool enable = !!brightness;
418 	int rc;
419 
420 	rc = set_flash_strobe(led, SW_STROBE, false);
421 	if (rc)
422 		return rc;
423 
424 	rc = set_flash_module_en(led, false);
425 	if (rc)
426 		return rc;
427 
428 	rc = set_flash_current(led, current_ma, TORCH_MODE);
429 	if (rc)
430 		return rc;
431 
432 	/* Disable flash timeout for torch LED */
433 	rc = set_flash_timeout(led, 0);
434 	if (rc)
435 		return rc;
436 
437 	rc = set_flash_module_en(led, enable);
438 	if (rc)
439 		return rc;
440 
441 	return set_flash_strobe(led, SW_STROBE, enable);
442 }
443 
444 static const struct led_flash_ops qcom_flash_ops = {
445 	.flash_brightness_set = qcom_flash_brightness_set,
446 	.strobe_set = qcom_flash_strobe_set,
447 	.strobe_get = qcom_flash_strobe_get,
448 	.timeout_set = qcom_flash_timeout_set,
449 	.fault_get = qcom_flash_fault_get,
450 };
451 
452 #if IS_ENABLED(CONFIG_V4L2_FLASH_LED_CLASS)
453 static int qcom_flash_external_strobe_set(struct v4l2_flash *v4l2_flash, bool enable)
454 {
455 	struct led_classdev_flash *fled_cdev = v4l2_flash->fled_cdev;
456 	struct qcom_flash_led *led = flcdev_to_qcom_fled(fled_cdev);
457 	int rc;
458 
459 	rc = set_flash_module_en(led, enable);
460 	if (rc)
461 		return rc;
462 
463 	if (enable)
464 		return set_flash_strobe(led, HW_STROBE, true);
465 	else
466 		return set_flash_strobe(led, SW_STROBE, false);
467 }
468 
469 static enum led_brightness
470 qcom_flash_intensity_to_led_brightness(struct v4l2_flash *v4l2_flash, s32 intensity)
471 {
472 	struct led_classdev_flash *fled_cdev = v4l2_flash->fled_cdev;
473 	struct qcom_flash_led *led = flcdev_to_qcom_fled(fled_cdev);
474 	u32 current_ma = intensity / UA_PER_MA;
475 
476 	current_ma = min_t(u32, current_ma, led->max_torch_current_ma);
477 	if (!current_ma)
478 		return LED_OFF;
479 
480 	return (current_ma * LED_FULL) / led->max_torch_current_ma;
481 }
482 
483 static s32 qcom_flash_brightness_to_led_intensity(struct v4l2_flash *v4l2_flash,
484 					enum led_brightness brightness)
485 {
486 	struct led_classdev_flash *fled_cdev = v4l2_flash->fled_cdev;
487 	struct qcom_flash_led *led = flcdev_to_qcom_fled(fled_cdev);
488 
489 	return (brightness * led->max_torch_current_ma * UA_PER_MA) / LED_FULL;
490 }
491 
492 static const struct v4l2_flash_ops qcom_v4l2_flash_ops = {
493 	.external_strobe_set = qcom_flash_external_strobe_set,
494 	.intensity_to_led_brightness = qcom_flash_intensity_to_led_brightness,
495 	.led_brightness_to_intensity = qcom_flash_brightness_to_led_intensity,
496 };
497 
498 static int
499 qcom_flash_v4l2_init(struct device *dev, struct qcom_flash_led *led, struct fwnode_handle *fwnode)
500 {
501 	struct qcom_flash_data *flash_data = led->flash_data;
502 	struct v4l2_flash_config v4l2_cfg = { 0 };
503 	struct led_flash_setting *intensity = &v4l2_cfg.intensity;
504 
505 	if (!(led->flash.led_cdev.flags & LED_DEV_CAP_FLASH))
506 		return 0;
507 
508 	intensity->min = intensity->step = TORCH_IRES_UA * led->chan_count;
509 	intensity->max = led->max_torch_current_ma * UA_PER_MA;
510 	intensity->val = min_t(u32, intensity->max, TORCH_CURRENT_DEFAULT_UA);
511 
512 	strscpy(v4l2_cfg.dev_name, led->flash.led_cdev.dev->kobj.name,
513 					sizeof(v4l2_cfg.dev_name));
514 
515 	v4l2_cfg.has_external_strobe = true;
516 	v4l2_cfg.flash_faults = LED_FAULT_INPUT_VOLTAGE |
517 				LED_FAULT_OVER_CURRENT |
518 				LED_FAULT_SHORT_CIRCUIT |
519 				LED_FAULT_OVER_TEMPERATURE |
520 				LED_FAULT_TIMEOUT;
521 
522 	flash_data->v4l2_flash[flash_data->leds_count] =
523 		v4l2_flash_init(dev, fwnode, &led->flash, &qcom_v4l2_flash_ops, &v4l2_cfg);
524 	return PTR_ERR_OR_ZERO(flash_data->v4l2_flash);
525 }
526 # else
527 static int
528 qcom_flash_v4l2_init(struct device *dev, struct qcom_flash_led *led, struct fwnode_handle *fwnode)
529 {
530 	return 0;
531 }
532 #endif
533 
534 static int qcom_flash_register_led_device(struct device *dev,
535 		struct fwnode_handle *node, struct qcom_flash_led *led)
536 {
537 	struct qcom_flash_data *flash_data = led->flash_data;
538 	struct led_init_data init_data;
539 	struct led_classdev_flash *flash = &led->flash;
540 	struct led_flash_setting *brightness, *timeout;
541 	u32 current_ua, timeout_us;
542 	u32 channels[4];
543 	int i, rc, count;
544 
545 	count = fwnode_property_count_u32(node, "led-sources");
546 	if (count <= 0) {
547 		dev_err(dev, "No led-sources specified\n");
548 		return -ENODEV;
549 	}
550 
551 	if (count > flash_data->max_channels) {
552 		dev_err(dev, "led-sources count %u exceeds maximum channel count %u\n",
553 				count, flash_data->max_channels);
554 		return -EINVAL;
555 	}
556 
557 	rc = fwnode_property_read_u32_array(node, "led-sources", channels, count);
558 	if (rc < 0) {
559 		dev_err(dev, "Failed to read led-sources property, rc=%d\n", rc);
560 		return rc;
561 	}
562 
563 	led->chan_count = count;
564 	led->chan_id = devm_kcalloc(dev, count, sizeof(u8), GFP_KERNEL);
565 	if (!led->chan_id)
566 		return -ENOMEM;
567 
568 	for (i = 0; i < count; i++) {
569 		if ((channels[i] == 0) || (channels[i] > flash_data->max_channels)) {
570 			dev_err(dev, "led-source out of HW support range [1-%u]\n",
571 					flash_data->max_channels);
572 			return -EINVAL;
573 		}
574 
575 		/* Make chan_id indexing from 0 */
576 		led->chan_id[i] = channels[i] - 1;
577 	}
578 
579 	rc = fwnode_property_read_u32(node, "led-max-microamp", &current_ua);
580 	if (rc < 0) {
581 		dev_err(dev, "Failed to read led-max-microamp property, rc=%d\n", rc);
582 		return rc;
583 	}
584 
585 	if (current_ua == 0) {
586 		dev_err(dev, "led-max-microamp shouldn't be 0\n");
587 		return -EINVAL;
588 	}
589 
590 	current_ua = min_t(u32, current_ua, TORCH_CURRENT_MAX_UA * led->chan_count);
591 	led->max_torch_current_ma = current_ua / UA_PER_MA;
592 
593 	if (fwnode_property_present(node, "flash-max-microamp")) {
594 		flash->led_cdev.flags |= LED_DEV_CAP_FLASH;
595 
596 		rc = fwnode_property_read_u32(node, "flash-max-microamp", &current_ua);
597 		if (rc < 0) {
598 			dev_err(dev, "Failed to read flash-max-microamp property, rc=%d\n",
599 					rc);
600 			return rc;
601 		}
602 
603 		current_ua = min_t(u32, current_ua, FLASH_CURRENT_MAX_UA * led->chan_count);
604 		current_ua = min_t(u32, current_ua, FLASH_TOTAL_CURRENT_MAX_UA);
605 
606 		/* Initialize flash class LED device brightness settings */
607 		brightness = &flash->brightness;
608 		brightness->min = brightness->step = FLASH_IRES_UA * led->chan_count;
609 		brightness->max = current_ua;
610 		brightness->val = min_t(u32, current_ua, FLASH_CURRENT_DEFAULT_UA);
611 
612 		led->max_flash_current_ma = current_ua / UA_PER_MA;
613 		led->flash_current_ma = brightness->val / UA_PER_MA;
614 
615 		rc = fwnode_property_read_u32(node, "flash-max-timeout-us", &timeout_us);
616 		if (rc < 0) {
617 			dev_err(dev, "Failed to read flash-max-timeout-us property, rc=%d\n",
618 					rc);
619 			return rc;
620 		}
621 
622 		timeout_us = min_t(u32, timeout_us, FLASH_TIMEOUT_MAX_US);
623 
624 		/* Initialize flash class LED device timeout settings */
625 		timeout = &flash->timeout;
626 		timeout->min = timeout->step = FLASH_TIMEOUT_STEP_US;
627 		timeout->val = timeout->max = timeout_us;
628 
629 		led->max_timeout_ms = led->flash_timeout_ms = timeout_us / USEC_PER_MSEC;
630 
631 		flash->ops = &qcom_flash_ops;
632 	}
633 
634 	flash->led_cdev.brightness_set_blocking = qcom_flash_led_brightness_set;
635 
636 	init_data.fwnode = node;
637 	init_data.devicename = NULL;
638 	init_data.default_label = NULL;
639 	init_data.devname_mandatory = false;
640 
641 	rc = devm_led_classdev_flash_register_ext(dev, flash, &init_data);
642 	if (rc < 0) {
643 		dev_err(dev, "Register flash LED classdev failed, rc=%d\n", rc);
644 		return rc;
645 	}
646 
647 	return qcom_flash_v4l2_init(dev, led, node);
648 }
649 
650 static int qcom_flash_led_probe(struct platform_device *pdev)
651 {
652 	struct qcom_flash_data *flash_data;
653 	struct qcom_flash_led *led;
654 	struct fwnode_handle *child;
655 	struct device *dev = &pdev->dev;
656 	struct regmap *regmap;
657 	struct reg_field *regs;
658 	int count, i, rc;
659 	u32 val, reg_base;
660 
661 	flash_data = devm_kzalloc(dev, sizeof(*flash_data), GFP_KERNEL);
662 	if (!flash_data)
663 		return -ENOMEM;
664 
665 	regmap = dev_get_regmap(dev->parent, NULL);
666 	if (!regmap) {
667 		dev_err(dev, "Failed to get parent regmap\n");
668 		return -EINVAL;
669 	}
670 
671 	rc = fwnode_property_read_u32(dev->fwnode, "reg", &reg_base);
672 	if (rc < 0) {
673 		dev_err(dev, "Failed to get register base address, rc=%d\n", rc);
674 		return rc;
675 	}
676 
677 	rc = regmap_read(regmap, reg_base + FLASH_TYPE_REG, &val);
678 	if (rc < 0) {
679 		dev_err(dev, "Read flash LED module type failed, rc=%d\n", rc);
680 		return rc;
681 	}
682 
683 	if (val != FLASH_TYPE_VAL) {
684 		dev_err(dev, "type %#x is not a flash LED module\n", val);
685 		return -ENODEV;
686 	}
687 
688 	rc = regmap_read(regmap, reg_base + FLASH_SUBTYPE_REG, &val);
689 	if (rc < 0) {
690 		dev_err(dev, "Read flash LED module subtype failed, rc=%d\n", rc);
691 		return rc;
692 	}
693 
694 	if (val == FLASH_SUBTYPE_3CH_PM8150_VAL || val == FLASH_SUBTYPE_3CH_PMI8998_VAL) {
695 		flash_data->hw_type = QCOM_MVFLASH_3CH;
696 		flash_data->max_channels = 3;
697 		regs = mvflash_3ch_regs;
698 	} else if (val == FLASH_SUBTYPE_4CH_VAL) {
699 		flash_data->hw_type = QCOM_MVFLASH_4CH;
700 		flash_data->max_channels = 4;
701 		regs = mvflash_4ch_regs;
702 	} else {
703 		dev_err(dev, "flash LED subtype %#x is not yet supported\n", val);
704 		return -ENODEV;
705 	}
706 
707 	for (i = 0; i < REG_MAX_COUNT; i++)
708 		regs[i].reg += reg_base;
709 
710 	rc = devm_regmap_field_bulk_alloc(dev, regmap, flash_data->r_fields, regs, REG_MAX_COUNT);
711 	if (rc < 0) {
712 		dev_err(dev, "Failed to allocate regmap field, rc=%d\n", rc);
713 		return rc;
714 	}
715 
716 	platform_set_drvdata(pdev, flash_data);
717 	mutex_init(&flash_data->lock);
718 
719 	count = device_get_child_node_count(dev);
720 	if (count == 0 || count > flash_data->max_channels) {
721 		dev_err(dev, "No child or child count exceeds %d\n", flash_data->max_channels);
722 		return -EINVAL;
723 	}
724 
725 	flash_data->v4l2_flash = devm_kcalloc(dev, count,
726 			sizeof(*flash_data->v4l2_flash), GFP_KERNEL);
727 	if (!flash_data->v4l2_flash)
728 		return -ENOMEM;
729 
730 	device_for_each_child_node(dev, child) {
731 		led = devm_kzalloc(dev, sizeof(*led), GFP_KERNEL);
732 		if (!led) {
733 			rc = -ENOMEM;
734 			goto release;
735 		}
736 
737 		led->flash_data = flash_data;
738 		rc = qcom_flash_register_led_device(dev, child, led);
739 		if (rc < 0)
740 			goto release;
741 
742 		flash_data->leds_count++;
743 	}
744 
745 	return 0;
746 
747 release:
748 	while (flash_data->v4l2_flash[flash_data->leds_count] && flash_data->leds_count)
749 		v4l2_flash_release(flash_data->v4l2_flash[flash_data->leds_count--]);
750 	return rc;
751 }
752 
753 static int qcom_flash_led_remove(struct platform_device *pdev)
754 {
755 	struct qcom_flash_data *flash_data = platform_get_drvdata(pdev);
756 
757 	while (flash_data->v4l2_flash[flash_data->leds_count] && flash_data->leds_count)
758 		v4l2_flash_release(flash_data->v4l2_flash[flash_data->leds_count--]);
759 
760 	mutex_destroy(&flash_data->lock);
761 	return 0;
762 }
763 
764 static const struct of_device_id qcom_flash_led_match_table[] = {
765 	{ .compatible = "qcom,spmi-flash-led" },
766 	{ }
767 };
768 
769 MODULE_DEVICE_TABLE(of, qcom_flash_led_match_table);
770 static struct platform_driver qcom_flash_led_driver = {
771 	.driver = {
772 		.name = "leds-qcom-flash",
773 		.of_match_table = qcom_flash_led_match_table,
774 	},
775 	.probe = qcom_flash_led_probe,
776 	.remove = qcom_flash_led_remove,
777 };
778 
779 module_platform_driver(qcom_flash_led_driver);
780 
781 MODULE_DESCRIPTION("QCOM Flash LED driver");
782 MODULE_LICENSE("GPL");
783