xref: /linux/drivers/media/v4l2-core/v4l2-flash-led-class.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
242bd6f59SJacek Anaszewski /*
342bd6f59SJacek Anaszewski  * V4L2 flash LED sub-device registration helpers.
442bd6f59SJacek Anaszewski  *
542bd6f59SJacek Anaszewski  *	Copyright (C) 2015 Samsung Electronics Co., Ltd
642bd6f59SJacek Anaszewski  *	Author: Jacek Anaszewski <j.anaszewski@samsung.com>
742bd6f59SJacek Anaszewski  */
842bd6f59SJacek Anaszewski 
942bd6f59SJacek Anaszewski #include <linux/led-class-flash.h>
1042bd6f59SJacek Anaszewski #include <linux/module.h>
1142bd6f59SJacek Anaszewski #include <linux/mutex.h>
12048ea05bSSakari Ailus #include <linux/property.h>
1342bd6f59SJacek Anaszewski #include <linux/slab.h>
1442bd6f59SJacek Anaszewski #include <linux/types.h>
1542bd6f59SJacek Anaszewski #include <media/v4l2-flash-led-class.h>
1642bd6f59SJacek Anaszewski 
1742bd6f59SJacek Anaszewski #define has_flash_op(v4l2_flash, op)				\
1852740975SSakari Ailus 	(v4l2_flash && v4l2_flash->ops && v4l2_flash->ops->op)
1942bd6f59SJacek Anaszewski 
2042bd6f59SJacek Anaszewski #define call_flash_op(v4l2_flash, op, arg)			\
2142bd6f59SJacek Anaszewski 		(has_flash_op(v4l2_flash, op) ?			\
2242bd6f59SJacek Anaszewski 			v4l2_flash->ops->op(v4l2_flash, arg) :	\
2342bd6f59SJacek Anaszewski 			-EINVAL)
2442bd6f59SJacek Anaszewski 
2542bd6f59SJacek Anaszewski enum ctrl_init_data_id {
2642bd6f59SJacek Anaszewski 	LED_MODE,
2742bd6f59SJacek Anaszewski 	TORCH_INTENSITY,
2842bd6f59SJacek Anaszewski 	FLASH_INTENSITY,
2942bd6f59SJacek Anaszewski 	INDICATOR_INTENSITY,
3042bd6f59SJacek Anaszewski 	FLASH_TIMEOUT,
3142bd6f59SJacek Anaszewski 	STROBE_SOURCE,
3242bd6f59SJacek Anaszewski 	/*
3342bd6f59SJacek Anaszewski 	 * Only above values are applicable to
3442bd6f59SJacek Anaszewski 	 * the 'ctrls' array in the struct v4l2_flash.
3542bd6f59SJacek Anaszewski 	 */
3642bd6f59SJacek Anaszewski 	FLASH_STROBE,
3742bd6f59SJacek Anaszewski 	STROBE_STOP,
3842bd6f59SJacek Anaszewski 	STROBE_STATUS,
3942bd6f59SJacek Anaszewski 	FLASH_FAULT,
4042bd6f59SJacek Anaszewski 	NUM_FLASH_CTRLS,
4142bd6f59SJacek Anaszewski };
4242bd6f59SJacek Anaszewski 
__intensity_to_led_brightness(struct v4l2_ctrl * ctrl,s32 intensity)4342bd6f59SJacek Anaszewski static enum led_brightness __intensity_to_led_brightness(
4442bd6f59SJacek Anaszewski 					struct v4l2_ctrl *ctrl, s32 intensity)
4542bd6f59SJacek Anaszewski {
4642bd6f59SJacek Anaszewski 	intensity -= ctrl->minimum;
4742bd6f59SJacek Anaszewski 	intensity /= (u32) ctrl->step;
4842bd6f59SJacek Anaszewski 
4942bd6f59SJacek Anaszewski 	/*
5042bd6f59SJacek Anaszewski 	 * Indicator LEDs, unlike torch LEDs, are turned on/off basing on
5142bd6f59SJacek Anaszewski 	 * the state of V4L2_CID_FLASH_INDICATOR_INTENSITY control only.
5242bd6f59SJacek Anaszewski 	 * Therefore it must be possible to set it to 0 level which in
5342bd6f59SJacek Anaszewski 	 * the LED subsystem reflects LED_OFF state.
5442bd6f59SJacek Anaszewski 	 */
5542bd6f59SJacek Anaszewski 	if (ctrl->minimum)
5642bd6f59SJacek Anaszewski 		++intensity;
5742bd6f59SJacek Anaszewski 
5842bd6f59SJacek Anaszewski 	return intensity;
5942bd6f59SJacek Anaszewski }
6042bd6f59SJacek Anaszewski 
__led_brightness_to_intensity(struct v4l2_ctrl * ctrl,enum led_brightness brightness)6142bd6f59SJacek Anaszewski static s32 __led_brightness_to_intensity(struct v4l2_ctrl *ctrl,
6242bd6f59SJacek Anaszewski 					 enum led_brightness brightness)
6342bd6f59SJacek Anaszewski {
6442bd6f59SJacek Anaszewski 	/*
6542bd6f59SJacek Anaszewski 	 * Indicator LEDs, unlike torch LEDs, are turned on/off basing on
6642bd6f59SJacek Anaszewski 	 * the state of V4L2_CID_FLASH_INDICATOR_INTENSITY control only.
6742bd6f59SJacek Anaszewski 	 * Do not decrement brightness read from the LED subsystem for
6842bd6f59SJacek Anaszewski 	 * indicator LED as it may equal 0. For torch LEDs this function
6942bd6f59SJacek Anaszewski 	 * is called only when V4L2_FLASH_LED_MODE_TORCH is set and the
7042bd6f59SJacek Anaszewski 	 * brightness read is guaranteed to be greater than 0. In the mode
7142bd6f59SJacek Anaszewski 	 * V4L2_FLASH_LED_MODE_NONE the cached torch intensity value is used.
7242bd6f59SJacek Anaszewski 	 */
7342bd6f59SJacek Anaszewski 	if (ctrl->id != V4L2_CID_FLASH_INDICATOR_INTENSITY)
7442bd6f59SJacek Anaszewski 		--brightness;
7542bd6f59SJacek Anaszewski 
7642bd6f59SJacek Anaszewski 	return (brightness * ctrl->step) + ctrl->minimum;
7742bd6f59SJacek Anaszewski }
7842bd6f59SJacek Anaszewski 
v4l2_flash_set_led_brightness(struct v4l2_flash * v4l2_flash,struct v4l2_ctrl * ctrl)794d7adf02SSakari Ailus static int v4l2_flash_set_led_brightness(struct v4l2_flash *v4l2_flash,
8042bd6f59SJacek Anaszewski 					 struct v4l2_ctrl *ctrl)
8142bd6f59SJacek Anaszewski {
8242bd6f59SJacek Anaszewski 	struct v4l2_ctrl **ctrls = v4l2_flash->ctrls;
83a40eba9bSSakari Ailus 	struct led_classdev *led_cdev;
8442bd6f59SJacek Anaszewski 	enum led_brightness brightness;
8542bd6f59SJacek Anaszewski 
8642bd6f59SJacek Anaszewski 	if (has_flash_op(v4l2_flash, intensity_to_led_brightness))
8742bd6f59SJacek Anaszewski 		brightness = call_flash_op(v4l2_flash,
8842bd6f59SJacek Anaszewski 					intensity_to_led_brightness,
8942bd6f59SJacek Anaszewski 					ctrl->val);
9042bd6f59SJacek Anaszewski 	else
9142bd6f59SJacek Anaszewski 		brightness = __intensity_to_led_brightness(ctrl, ctrl->val);
9242bd6f59SJacek Anaszewski 	/*
9342bd6f59SJacek Anaszewski 	 * In case a LED Flash class driver provides ops for custom
9442bd6f59SJacek Anaszewski 	 * brightness <-> intensity conversion, it also must have defined
9542bd6f59SJacek Anaszewski 	 * related v4l2 control step == 1. In such a case a backward conversion
9642bd6f59SJacek Anaszewski 	 * from led brightness to v4l2 intensity is required to find out the
97*54db159dSJason Wang 	 * aligned intensity value.
9842bd6f59SJacek Anaszewski 	 */
9942bd6f59SJacek Anaszewski 	if (has_flash_op(v4l2_flash, led_brightness_to_intensity))
10042bd6f59SJacek Anaszewski 		ctrl->val = call_flash_op(v4l2_flash,
10142bd6f59SJacek Anaszewski 					led_brightness_to_intensity,
10242bd6f59SJacek Anaszewski 					brightness);
10342bd6f59SJacek Anaszewski 
10442bd6f59SJacek Anaszewski 	if (ctrl == ctrls[TORCH_INTENSITY]) {
10542bd6f59SJacek Anaszewski 		if (ctrls[LED_MODE]->val != V4L2_FLASH_LED_MODE_TORCH)
1064d7adf02SSakari Ailus 			return 0;
10742bd6f59SJacek Anaszewski 
108a40eba9bSSakari Ailus 		if (WARN_ON_ONCE(!v4l2_flash->fled_cdev))
1094d7adf02SSakari Ailus 			return -EINVAL;
110a40eba9bSSakari Ailus 
111a40eba9bSSakari Ailus 		led_cdev = &v4l2_flash->fled_cdev->led_cdev;
11242bd6f59SJacek Anaszewski 	} else {
113a40eba9bSSakari Ailus 		if (WARN_ON_ONCE(!v4l2_flash->iled_cdev))
1144d7adf02SSakari Ailus 			return -EINVAL;
115a40eba9bSSakari Ailus 
116a40eba9bSSakari Ailus 		led_cdev = v4l2_flash->iled_cdev;
11742bd6f59SJacek Anaszewski 	}
118a40eba9bSSakari Ailus 
1194d7adf02SSakari Ailus 	return led_set_brightness_sync(led_cdev, brightness);
12042bd6f59SJacek Anaszewski }
12142bd6f59SJacek Anaszewski 
v4l2_flash_update_led_brightness(struct v4l2_flash * v4l2_flash,struct v4l2_ctrl * ctrl)12242bd6f59SJacek Anaszewski static int v4l2_flash_update_led_brightness(struct v4l2_flash *v4l2_flash,
12342bd6f59SJacek Anaszewski 					struct v4l2_ctrl *ctrl)
12442bd6f59SJacek Anaszewski {
12542bd6f59SJacek Anaszewski 	struct v4l2_ctrl **ctrls = v4l2_flash->ctrls;
12642bd6f59SJacek Anaszewski 	struct led_classdev *led_cdev;
12742bd6f59SJacek Anaszewski 	int ret;
12842bd6f59SJacek Anaszewski 
12942bd6f59SJacek Anaszewski 	if (ctrl == ctrls[TORCH_INTENSITY]) {
13042bd6f59SJacek Anaszewski 		/*
13142bd6f59SJacek Anaszewski 		 * Update torch brightness only if in TORCH_MODE. In other modes
13242bd6f59SJacek Anaszewski 		 * torch led is turned off, which would spuriously inform the
13342bd6f59SJacek Anaszewski 		 * user space that V4L2_CID_FLASH_TORCH_INTENSITY control value
13442bd6f59SJacek Anaszewski 		 * has changed to 0.
13542bd6f59SJacek Anaszewski 		 */
13642bd6f59SJacek Anaszewski 		if (ctrls[LED_MODE]->val != V4L2_FLASH_LED_MODE_TORCH)
13742bd6f59SJacek Anaszewski 			return 0;
138a40eba9bSSakari Ailus 
139a40eba9bSSakari Ailus 		if (WARN_ON_ONCE(!v4l2_flash->fled_cdev))
140a40eba9bSSakari Ailus 			return -EINVAL;
141a40eba9bSSakari Ailus 
14242bd6f59SJacek Anaszewski 		led_cdev = &v4l2_flash->fled_cdev->led_cdev;
14342bd6f59SJacek Anaszewski 	} else {
144a40eba9bSSakari Ailus 		if (WARN_ON_ONCE(!v4l2_flash->iled_cdev))
145a40eba9bSSakari Ailus 			return -EINVAL;
146a40eba9bSSakari Ailus 
14785f7ff97SSakari Ailus 		led_cdev = v4l2_flash->iled_cdev;
14842bd6f59SJacek Anaszewski 	}
14942bd6f59SJacek Anaszewski 
15042bd6f59SJacek Anaszewski 	ret = led_update_brightness(led_cdev);
15142bd6f59SJacek Anaszewski 	if (ret < 0)
15242bd6f59SJacek Anaszewski 		return ret;
15342bd6f59SJacek Anaszewski 
15442bd6f59SJacek Anaszewski 	if (has_flash_op(v4l2_flash, led_brightness_to_intensity))
15542bd6f59SJacek Anaszewski 		ctrl->val = call_flash_op(v4l2_flash,
15642bd6f59SJacek Anaszewski 						led_brightness_to_intensity,
15742bd6f59SJacek Anaszewski 						led_cdev->brightness);
15842bd6f59SJacek Anaszewski 	else
15942bd6f59SJacek Anaszewski 		ctrl->val = __led_brightness_to_intensity(ctrl,
16042bd6f59SJacek Anaszewski 						led_cdev->brightness);
16142bd6f59SJacek Anaszewski 
16242bd6f59SJacek Anaszewski 	return 0;
16342bd6f59SJacek Anaszewski }
16442bd6f59SJacek Anaszewski 
v4l2_flash_g_volatile_ctrl(struct v4l2_ctrl * c)16542bd6f59SJacek Anaszewski static int v4l2_flash_g_volatile_ctrl(struct v4l2_ctrl *c)
16642bd6f59SJacek Anaszewski {
16742bd6f59SJacek Anaszewski 	struct v4l2_flash *v4l2_flash = v4l2_ctrl_to_v4l2_flash(c);
16842bd6f59SJacek Anaszewski 	struct led_classdev_flash *fled_cdev = v4l2_flash->fled_cdev;
16942bd6f59SJacek Anaszewski 	bool is_strobing;
17042bd6f59SJacek Anaszewski 	int ret;
17142bd6f59SJacek Anaszewski 
17242bd6f59SJacek Anaszewski 	switch (c->id) {
17342bd6f59SJacek Anaszewski 	case V4L2_CID_FLASH_TORCH_INTENSITY:
17442bd6f59SJacek Anaszewski 	case V4L2_CID_FLASH_INDICATOR_INTENSITY:
17542bd6f59SJacek Anaszewski 		return v4l2_flash_update_led_brightness(v4l2_flash, c);
176a40eba9bSSakari Ailus 	}
177a40eba9bSSakari Ailus 
178a40eba9bSSakari Ailus 	if (!fled_cdev)
179a40eba9bSSakari Ailus 		return -EINVAL;
180a40eba9bSSakari Ailus 
181a40eba9bSSakari Ailus 	switch (c->id) {
18242bd6f59SJacek Anaszewski 	case V4L2_CID_FLASH_INTENSITY:
18342bd6f59SJacek Anaszewski 		ret = led_update_flash_brightness(fled_cdev);
18442bd6f59SJacek Anaszewski 		if (ret < 0)
18542bd6f59SJacek Anaszewski 			return ret;
18642bd6f59SJacek Anaszewski 		/*
18742bd6f59SJacek Anaszewski 		 * No conversion is needed as LED Flash class also uses
18842bd6f59SJacek Anaszewski 		 * microamperes for flash intensity units.
18942bd6f59SJacek Anaszewski 		 */
19042bd6f59SJacek Anaszewski 		c->val = fled_cdev->brightness.val;
19142bd6f59SJacek Anaszewski 		return 0;
19242bd6f59SJacek Anaszewski 	case V4L2_CID_FLASH_STROBE_STATUS:
19342bd6f59SJacek Anaszewski 		ret = led_get_flash_strobe(fled_cdev, &is_strobing);
19442bd6f59SJacek Anaszewski 		if (ret < 0)
19542bd6f59SJacek Anaszewski 			return ret;
19642bd6f59SJacek Anaszewski 		c->val = is_strobing;
19742bd6f59SJacek Anaszewski 		return 0;
19842bd6f59SJacek Anaszewski 	case V4L2_CID_FLASH_FAULT:
19942bd6f59SJacek Anaszewski 		/* LED faults map directly to V4L2 flash faults */
20042bd6f59SJacek Anaszewski 		return led_get_flash_fault(fled_cdev, &c->val);
20142bd6f59SJacek Anaszewski 	default:
20242bd6f59SJacek Anaszewski 		return -EINVAL;
20342bd6f59SJacek Anaszewski 	}
20442bd6f59SJacek Anaszewski }
20542bd6f59SJacek Anaszewski 
__software_strobe_mode_inactive(struct v4l2_ctrl ** ctrls)20642bd6f59SJacek Anaszewski static bool __software_strobe_mode_inactive(struct v4l2_ctrl **ctrls)
20742bd6f59SJacek Anaszewski {
20842bd6f59SJacek Anaszewski 	return ((ctrls[LED_MODE]->val != V4L2_FLASH_LED_MODE_FLASH) ||
20942bd6f59SJacek Anaszewski 		(ctrls[STROBE_SOURCE] && (ctrls[STROBE_SOURCE]->val !=
21042bd6f59SJacek Anaszewski 				V4L2_FLASH_STROBE_SOURCE_SOFTWARE)));
21142bd6f59SJacek Anaszewski }
21242bd6f59SJacek Anaszewski 
v4l2_flash_s_ctrl(struct v4l2_ctrl * c)21342bd6f59SJacek Anaszewski static int v4l2_flash_s_ctrl(struct v4l2_ctrl *c)
21442bd6f59SJacek Anaszewski {
21542bd6f59SJacek Anaszewski 	struct v4l2_flash *v4l2_flash = v4l2_ctrl_to_v4l2_flash(c);
21642bd6f59SJacek Anaszewski 	struct led_classdev_flash *fled_cdev = v4l2_flash->fled_cdev;
217a40eba9bSSakari Ailus 	struct led_classdev *led_cdev;
21842bd6f59SJacek Anaszewski 	struct v4l2_ctrl **ctrls = v4l2_flash->ctrls;
21942bd6f59SJacek Anaszewski 	bool external_strobe;
22042bd6f59SJacek Anaszewski 	int ret = 0;
22142bd6f59SJacek Anaszewski 
22242bd6f59SJacek Anaszewski 	switch (c->id) {
223a40eba9bSSakari Ailus 	case V4L2_CID_FLASH_TORCH_INTENSITY:
224a40eba9bSSakari Ailus 	case V4L2_CID_FLASH_INDICATOR_INTENSITY:
2254d7adf02SSakari Ailus 		return v4l2_flash_set_led_brightness(v4l2_flash, c);
226a40eba9bSSakari Ailus 	}
227a40eba9bSSakari Ailus 
228a40eba9bSSakari Ailus 	if (!fled_cdev)
229a40eba9bSSakari Ailus 		return -EINVAL;
230a40eba9bSSakari Ailus 
231a40eba9bSSakari Ailus 	led_cdev = &fled_cdev->led_cdev;
232a40eba9bSSakari Ailus 
233a40eba9bSSakari Ailus 	switch (c->id) {
23442bd6f59SJacek Anaszewski 	case V4L2_CID_FLASH_LED_MODE:
23542bd6f59SJacek Anaszewski 		switch (c->val) {
23642bd6f59SJacek Anaszewski 		case V4L2_FLASH_LED_MODE_NONE:
237fa1706c4SJacek Anaszewski 			led_set_brightness_sync(led_cdev, LED_OFF);
23842bd6f59SJacek Anaszewski 			return led_set_flash_strobe(fled_cdev, false);
23942bd6f59SJacek Anaszewski 		case V4L2_FLASH_LED_MODE_FLASH:
24042bd6f59SJacek Anaszewski 			/* Turn the torch LED off */
241fa1706c4SJacek Anaszewski 			led_set_brightness_sync(led_cdev, LED_OFF);
24242bd6f59SJacek Anaszewski 			if (ctrls[STROBE_SOURCE]) {
24342bd6f59SJacek Anaszewski 				external_strobe = (ctrls[STROBE_SOURCE]->val ==
24442bd6f59SJacek Anaszewski 					V4L2_FLASH_STROBE_SOURCE_EXTERNAL);
24542bd6f59SJacek Anaszewski 
24642bd6f59SJacek Anaszewski 				ret = call_flash_op(v4l2_flash,
24742bd6f59SJacek Anaszewski 						external_strobe_set,
24842bd6f59SJacek Anaszewski 						external_strobe);
24942bd6f59SJacek Anaszewski 			}
25042bd6f59SJacek Anaszewski 			return ret;
25142bd6f59SJacek Anaszewski 		case V4L2_FLASH_LED_MODE_TORCH:
25242bd6f59SJacek Anaszewski 			if (ctrls[STROBE_SOURCE]) {
25342bd6f59SJacek Anaszewski 				ret = call_flash_op(v4l2_flash,
25442bd6f59SJacek Anaszewski 						external_strobe_set,
25542bd6f59SJacek Anaszewski 						false);
25642bd6f59SJacek Anaszewski 				if (ret < 0)
25742bd6f59SJacek Anaszewski 					return ret;
25842bd6f59SJacek Anaszewski 			}
25942bd6f59SJacek Anaszewski 			/* Stop flash strobing */
26042bd6f59SJacek Anaszewski 			ret = led_set_flash_strobe(fled_cdev, false);
26142bd6f59SJacek Anaszewski 			if (ret < 0)
26242bd6f59SJacek Anaszewski 				return ret;
26342bd6f59SJacek Anaszewski 
2644d7adf02SSakari Ailus 			return v4l2_flash_set_led_brightness(v4l2_flash,
26542bd6f59SJacek Anaszewski 							     ctrls[TORCH_INTENSITY]);
26642bd6f59SJacek Anaszewski 		}
26742bd6f59SJacek Anaszewski 		break;
26842bd6f59SJacek Anaszewski 	case V4L2_CID_FLASH_STROBE_SOURCE:
26942bd6f59SJacek Anaszewski 		external_strobe = (c->val == V4L2_FLASH_STROBE_SOURCE_EXTERNAL);
27042bd6f59SJacek Anaszewski 		/*
27142bd6f59SJacek Anaszewski 		 * For some hardware arrangements setting strobe source may
27242bd6f59SJacek Anaszewski 		 * affect torch mode. Therefore, if not in the flash mode,
27342bd6f59SJacek Anaszewski 		 * cache only this setting. It will be applied upon switching
27442bd6f59SJacek Anaszewski 		 * to flash mode.
27542bd6f59SJacek Anaszewski 		 */
27642bd6f59SJacek Anaszewski 		if (ctrls[LED_MODE]->val != V4L2_FLASH_LED_MODE_FLASH)
27742bd6f59SJacek Anaszewski 			return 0;
27842bd6f59SJacek Anaszewski 
27942bd6f59SJacek Anaszewski 		return call_flash_op(v4l2_flash, external_strobe_set,
28042bd6f59SJacek Anaszewski 					external_strobe);
28142bd6f59SJacek Anaszewski 	case V4L2_CID_FLASH_STROBE:
28242bd6f59SJacek Anaszewski 		if (__software_strobe_mode_inactive(ctrls))
28342bd6f59SJacek Anaszewski 			return -EBUSY;
28442bd6f59SJacek Anaszewski 		return led_set_flash_strobe(fled_cdev, true);
28542bd6f59SJacek Anaszewski 	case V4L2_CID_FLASH_STROBE_STOP:
28642bd6f59SJacek Anaszewski 		if (__software_strobe_mode_inactive(ctrls))
28742bd6f59SJacek Anaszewski 			return -EBUSY;
28842bd6f59SJacek Anaszewski 		return led_set_flash_strobe(fled_cdev, false);
28942bd6f59SJacek Anaszewski 	case V4L2_CID_FLASH_TIMEOUT:
29042bd6f59SJacek Anaszewski 		/*
29142bd6f59SJacek Anaszewski 		 * No conversion is needed as LED Flash class also uses
29242bd6f59SJacek Anaszewski 		 * microseconds for flash timeout units.
29342bd6f59SJacek Anaszewski 		 */
29442bd6f59SJacek Anaszewski 		return led_set_flash_timeout(fled_cdev, c->val);
29542bd6f59SJacek Anaszewski 	case V4L2_CID_FLASH_INTENSITY:
29642bd6f59SJacek Anaszewski 		/*
29742bd6f59SJacek Anaszewski 		 * No conversion is needed as LED Flash class also uses
29842bd6f59SJacek Anaszewski 		 * microamperes for flash intensity units.
29942bd6f59SJacek Anaszewski 		 */
30042bd6f59SJacek Anaszewski 		return led_set_flash_brightness(fled_cdev, c->val);
30142bd6f59SJacek Anaszewski 	}
30242bd6f59SJacek Anaszewski 
30342bd6f59SJacek Anaszewski 	return -EINVAL;
30442bd6f59SJacek Anaszewski }
30542bd6f59SJacek Anaszewski 
30642bd6f59SJacek Anaszewski static const struct v4l2_ctrl_ops v4l2_flash_ctrl_ops = {
30742bd6f59SJacek Anaszewski 	.g_volatile_ctrl = v4l2_flash_g_volatile_ctrl,
30842bd6f59SJacek Anaszewski 	.s_ctrl = v4l2_flash_s_ctrl,
30942bd6f59SJacek Anaszewski };
31042bd6f59SJacek Anaszewski 
__lfs_to_v4l2_ctrl_config(struct led_flash_setting * s,struct v4l2_ctrl_config * c)31142bd6f59SJacek Anaszewski static void __lfs_to_v4l2_ctrl_config(struct led_flash_setting *s,
31242bd6f59SJacek Anaszewski 				struct v4l2_ctrl_config *c)
31342bd6f59SJacek Anaszewski {
31442bd6f59SJacek Anaszewski 	c->min = s->min;
31542bd6f59SJacek Anaszewski 	c->max = s->max;
31642bd6f59SJacek Anaszewski 	c->step = s->step;
31742bd6f59SJacek Anaszewski 	c->def = s->val;
31842bd6f59SJacek Anaszewski }
31942bd6f59SJacek Anaszewski 
__fill_ctrl_init_data(struct v4l2_flash * v4l2_flash,struct v4l2_flash_config * flash_cfg,struct v4l2_flash_ctrl_data * ctrl_init_data)32042bd6f59SJacek Anaszewski static void __fill_ctrl_init_data(struct v4l2_flash *v4l2_flash,
32142bd6f59SJacek Anaszewski 			  struct v4l2_flash_config *flash_cfg,
32242bd6f59SJacek Anaszewski 			  struct v4l2_flash_ctrl_data *ctrl_init_data)
32342bd6f59SJacek Anaszewski {
32442bd6f59SJacek Anaszewski 	struct led_classdev_flash *fled_cdev = v4l2_flash->fled_cdev;
325503dd28aSSakari Ailus 	struct led_classdev *led_cdev = fled_cdev ? &fled_cdev->led_cdev : NULL;
32642bd6f59SJacek Anaszewski 	struct v4l2_ctrl_config *ctrl_cfg;
32742bd6f59SJacek Anaszewski 	u32 mask;
32842bd6f59SJacek Anaszewski 
329503dd28aSSakari Ailus 	/* Init INDICATOR_INTENSITY ctrl data */
330503dd28aSSakari Ailus 	if (v4l2_flash->iled_cdev) {
331503dd28aSSakari Ailus 		ctrl_init_data[INDICATOR_INTENSITY].cid =
332503dd28aSSakari Ailus 					V4L2_CID_FLASH_INDICATOR_INTENSITY;
333503dd28aSSakari Ailus 		ctrl_cfg = &ctrl_init_data[INDICATOR_INTENSITY].config;
334503dd28aSSakari Ailus 		__lfs_to_v4l2_ctrl_config(&flash_cfg->intensity,
335503dd28aSSakari Ailus 					  ctrl_cfg);
336503dd28aSSakari Ailus 		ctrl_cfg->id = V4L2_CID_FLASH_INDICATOR_INTENSITY;
337503dd28aSSakari Ailus 		ctrl_cfg->min = 0;
338503dd28aSSakari Ailus 		ctrl_cfg->flags = V4L2_CTRL_FLAG_VOLATILE |
339503dd28aSSakari Ailus 				  V4L2_CTRL_FLAG_EXECUTE_ON_WRITE;
340503dd28aSSakari Ailus 	}
341503dd28aSSakari Ailus 
342503dd28aSSakari Ailus 	if (!led_cdev || WARN_ON(!(led_cdev->flags & LED_DEV_CAP_FLASH)))
343503dd28aSSakari Ailus 		return;
344503dd28aSSakari Ailus 
34542bd6f59SJacek Anaszewski 	/* Init FLASH_FAULT ctrl data */
34642bd6f59SJacek Anaszewski 	if (flash_cfg->flash_faults) {
34742bd6f59SJacek Anaszewski 		ctrl_init_data[FLASH_FAULT].cid = V4L2_CID_FLASH_FAULT;
34842bd6f59SJacek Anaszewski 		ctrl_cfg = &ctrl_init_data[FLASH_FAULT].config;
34942bd6f59SJacek Anaszewski 		ctrl_cfg->id = V4L2_CID_FLASH_FAULT;
35042bd6f59SJacek Anaszewski 		ctrl_cfg->max = flash_cfg->flash_faults;
35142bd6f59SJacek Anaszewski 		ctrl_cfg->flags = V4L2_CTRL_FLAG_VOLATILE |
35242bd6f59SJacek Anaszewski 				  V4L2_CTRL_FLAG_READ_ONLY;
35342bd6f59SJacek Anaszewski 	}
35442bd6f59SJacek Anaszewski 
35542bd6f59SJacek Anaszewski 	/* Init FLASH_LED_MODE ctrl data */
35642bd6f59SJacek Anaszewski 	mask = 1 << V4L2_FLASH_LED_MODE_NONE |
35742bd6f59SJacek Anaszewski 	       1 << V4L2_FLASH_LED_MODE_TORCH;
35842bd6f59SJacek Anaszewski 	if (led_cdev->flags & LED_DEV_CAP_FLASH)
35942bd6f59SJacek Anaszewski 		mask |= 1 << V4L2_FLASH_LED_MODE_FLASH;
36042bd6f59SJacek Anaszewski 
36142bd6f59SJacek Anaszewski 	ctrl_init_data[LED_MODE].cid = V4L2_CID_FLASH_LED_MODE;
36242bd6f59SJacek Anaszewski 	ctrl_cfg = &ctrl_init_data[LED_MODE].config;
36342bd6f59SJacek Anaszewski 	ctrl_cfg->id = V4L2_CID_FLASH_LED_MODE;
36442bd6f59SJacek Anaszewski 	ctrl_cfg->max = V4L2_FLASH_LED_MODE_TORCH;
36542bd6f59SJacek Anaszewski 	ctrl_cfg->menu_skip_mask = ~mask;
36642bd6f59SJacek Anaszewski 	ctrl_cfg->def = V4L2_FLASH_LED_MODE_NONE;
36742bd6f59SJacek Anaszewski 	ctrl_cfg->flags = 0;
36842bd6f59SJacek Anaszewski 
36942bd6f59SJacek Anaszewski 	/* Init TORCH_INTENSITY ctrl data */
37042bd6f59SJacek Anaszewski 	ctrl_init_data[TORCH_INTENSITY].cid = V4L2_CID_FLASH_TORCH_INTENSITY;
37142bd6f59SJacek Anaszewski 	ctrl_cfg = &ctrl_init_data[TORCH_INTENSITY].config;
372503dd28aSSakari Ailus 	__lfs_to_v4l2_ctrl_config(&flash_cfg->intensity, ctrl_cfg);
37342bd6f59SJacek Anaszewski 	ctrl_cfg->id = V4L2_CID_FLASH_TORCH_INTENSITY;
37442bd6f59SJacek Anaszewski 	ctrl_cfg->flags = V4L2_CTRL_FLAG_VOLATILE |
37542bd6f59SJacek Anaszewski 			  V4L2_CTRL_FLAG_EXECUTE_ON_WRITE;
37642bd6f59SJacek Anaszewski 
37742bd6f59SJacek Anaszewski 	/* Init FLASH_STROBE ctrl data */
37842bd6f59SJacek Anaszewski 	ctrl_init_data[FLASH_STROBE].cid = V4L2_CID_FLASH_STROBE;
37942bd6f59SJacek Anaszewski 	ctrl_cfg = &ctrl_init_data[FLASH_STROBE].config;
38042bd6f59SJacek Anaszewski 	ctrl_cfg->id = V4L2_CID_FLASH_STROBE;
38142bd6f59SJacek Anaszewski 
38242bd6f59SJacek Anaszewski 	/* Init STROBE_STOP ctrl data */
38342bd6f59SJacek Anaszewski 	ctrl_init_data[STROBE_STOP].cid = V4L2_CID_FLASH_STROBE_STOP;
38442bd6f59SJacek Anaszewski 	ctrl_cfg = &ctrl_init_data[STROBE_STOP].config;
38542bd6f59SJacek Anaszewski 	ctrl_cfg->id = V4L2_CID_FLASH_STROBE_STOP;
38642bd6f59SJacek Anaszewski 
38742bd6f59SJacek Anaszewski 	/* Init FLASH_STROBE_SOURCE ctrl data */
38842bd6f59SJacek Anaszewski 	if (flash_cfg->has_external_strobe) {
38942bd6f59SJacek Anaszewski 		mask = (1 << V4L2_FLASH_STROBE_SOURCE_SOFTWARE) |
39042bd6f59SJacek Anaszewski 		       (1 << V4L2_FLASH_STROBE_SOURCE_EXTERNAL);
39142bd6f59SJacek Anaszewski 		ctrl_init_data[STROBE_SOURCE].cid =
39242bd6f59SJacek Anaszewski 					V4L2_CID_FLASH_STROBE_SOURCE;
39342bd6f59SJacek Anaszewski 		ctrl_cfg = &ctrl_init_data[STROBE_SOURCE].config;
39442bd6f59SJacek Anaszewski 		ctrl_cfg->id = V4L2_CID_FLASH_STROBE_SOURCE;
39542bd6f59SJacek Anaszewski 		ctrl_cfg->max = V4L2_FLASH_STROBE_SOURCE_EXTERNAL;
39642bd6f59SJacek Anaszewski 		ctrl_cfg->menu_skip_mask = ~mask;
39742bd6f59SJacek Anaszewski 		ctrl_cfg->def = V4L2_FLASH_STROBE_SOURCE_SOFTWARE;
39842bd6f59SJacek Anaszewski 	}
39942bd6f59SJacek Anaszewski 
40042bd6f59SJacek Anaszewski 	/* Init STROBE_STATUS ctrl data */
40152740975SSakari Ailus 	if (has_flash_op(fled_cdev, strobe_get)) {
40242bd6f59SJacek Anaszewski 		ctrl_init_data[STROBE_STATUS].cid =
40342bd6f59SJacek Anaszewski 					V4L2_CID_FLASH_STROBE_STATUS;
40442bd6f59SJacek Anaszewski 		ctrl_cfg = &ctrl_init_data[STROBE_STATUS].config;
40542bd6f59SJacek Anaszewski 		ctrl_cfg->id = V4L2_CID_FLASH_STROBE_STATUS;
40642bd6f59SJacek Anaszewski 		ctrl_cfg->flags = V4L2_CTRL_FLAG_VOLATILE |
40742bd6f59SJacek Anaszewski 				  V4L2_CTRL_FLAG_READ_ONLY;
40842bd6f59SJacek Anaszewski 	}
40942bd6f59SJacek Anaszewski 
41042bd6f59SJacek Anaszewski 	/* Init FLASH_TIMEOUT ctrl data */
41152740975SSakari Ailus 	if (has_flash_op(fled_cdev, timeout_set)) {
41242bd6f59SJacek Anaszewski 		ctrl_init_data[FLASH_TIMEOUT].cid = V4L2_CID_FLASH_TIMEOUT;
41342bd6f59SJacek Anaszewski 		ctrl_cfg = &ctrl_init_data[FLASH_TIMEOUT].config;
41442bd6f59SJacek Anaszewski 		__lfs_to_v4l2_ctrl_config(&fled_cdev->timeout, ctrl_cfg);
41542bd6f59SJacek Anaszewski 		ctrl_cfg->id = V4L2_CID_FLASH_TIMEOUT;
41642bd6f59SJacek Anaszewski 	}
41742bd6f59SJacek Anaszewski 
41842bd6f59SJacek Anaszewski 	/* Init FLASH_INTENSITY ctrl data */
41952740975SSakari Ailus 	if (has_flash_op(fled_cdev, flash_brightness_set)) {
42042bd6f59SJacek Anaszewski 		ctrl_init_data[FLASH_INTENSITY].cid = V4L2_CID_FLASH_INTENSITY;
42142bd6f59SJacek Anaszewski 		ctrl_cfg = &ctrl_init_data[FLASH_INTENSITY].config;
42242bd6f59SJacek Anaszewski 		__lfs_to_v4l2_ctrl_config(&fled_cdev->brightness, ctrl_cfg);
42342bd6f59SJacek Anaszewski 		ctrl_cfg->id = V4L2_CID_FLASH_INTENSITY;
42442bd6f59SJacek Anaszewski 		ctrl_cfg->flags = V4L2_CTRL_FLAG_VOLATILE |
42542bd6f59SJacek Anaszewski 				  V4L2_CTRL_FLAG_EXECUTE_ON_WRITE;
42642bd6f59SJacek Anaszewski 	}
42742bd6f59SJacek Anaszewski }
42842bd6f59SJacek Anaszewski 
v4l2_flash_init_controls(struct v4l2_flash * v4l2_flash,struct v4l2_flash_config * flash_cfg)42942bd6f59SJacek Anaszewski static int v4l2_flash_init_controls(struct v4l2_flash *v4l2_flash,
43042bd6f59SJacek Anaszewski 				struct v4l2_flash_config *flash_cfg)
43142bd6f59SJacek Anaszewski 
43242bd6f59SJacek Anaszewski {
43342bd6f59SJacek Anaszewski 	struct v4l2_flash_ctrl_data *ctrl_init_data;
43442bd6f59SJacek Anaszewski 	struct v4l2_ctrl *ctrl;
43542bd6f59SJacek Anaszewski 	struct v4l2_ctrl_config *ctrl_cfg;
43642bd6f59SJacek Anaszewski 	int i, ret, num_ctrls = 0;
43742bd6f59SJacek Anaszewski 
438a86854d0SKees Cook 	v4l2_flash->ctrls = devm_kcalloc(v4l2_flash->sd.dev,
439a86854d0SKees Cook 					STROBE_SOURCE + 1,
440a86854d0SKees Cook 					sizeof(*v4l2_flash->ctrls),
441a86854d0SKees Cook 					GFP_KERNEL);
44242bd6f59SJacek Anaszewski 	if (!v4l2_flash->ctrls)
44342bd6f59SJacek Anaszewski 		return -ENOMEM;
44442bd6f59SJacek Anaszewski 
44542bd6f59SJacek Anaszewski 	/* allocate memory dynamically so as not to exceed stack frame size */
44642bd6f59SJacek Anaszewski 	ctrl_init_data = kcalloc(NUM_FLASH_CTRLS, sizeof(*ctrl_init_data),
44742bd6f59SJacek Anaszewski 					GFP_KERNEL);
44842bd6f59SJacek Anaszewski 	if (!ctrl_init_data)
44942bd6f59SJacek Anaszewski 		return -ENOMEM;
45042bd6f59SJacek Anaszewski 
45142bd6f59SJacek Anaszewski 	__fill_ctrl_init_data(v4l2_flash, flash_cfg, ctrl_init_data);
45242bd6f59SJacek Anaszewski 
45342bd6f59SJacek Anaszewski 	for (i = 0; i < NUM_FLASH_CTRLS; ++i)
45442bd6f59SJacek Anaszewski 		if (ctrl_init_data[i].cid)
45542bd6f59SJacek Anaszewski 			++num_ctrls;
45642bd6f59SJacek Anaszewski 
45742bd6f59SJacek Anaszewski 	v4l2_ctrl_handler_init(&v4l2_flash->hdl, num_ctrls);
45842bd6f59SJacek Anaszewski 
45942bd6f59SJacek Anaszewski 	for (i = 0; i < NUM_FLASH_CTRLS; ++i) {
46042bd6f59SJacek Anaszewski 		ctrl_cfg = &ctrl_init_data[i].config;
46142bd6f59SJacek Anaszewski 		if (!ctrl_init_data[i].cid)
46242bd6f59SJacek Anaszewski 			continue;
46342bd6f59SJacek Anaszewski 
46442bd6f59SJacek Anaszewski 		if (ctrl_cfg->id == V4L2_CID_FLASH_LED_MODE ||
46542bd6f59SJacek Anaszewski 		    ctrl_cfg->id == V4L2_CID_FLASH_STROBE_SOURCE)
46642bd6f59SJacek Anaszewski 			ctrl = v4l2_ctrl_new_std_menu(&v4l2_flash->hdl,
46742bd6f59SJacek Anaszewski 						&v4l2_flash_ctrl_ops,
46842bd6f59SJacek Anaszewski 						ctrl_cfg->id,
46942bd6f59SJacek Anaszewski 						ctrl_cfg->max,
47042bd6f59SJacek Anaszewski 						ctrl_cfg->menu_skip_mask,
47142bd6f59SJacek Anaszewski 						ctrl_cfg->def);
47242bd6f59SJacek Anaszewski 		else
47342bd6f59SJacek Anaszewski 			ctrl = v4l2_ctrl_new_std(&v4l2_flash->hdl,
47442bd6f59SJacek Anaszewski 						&v4l2_flash_ctrl_ops,
47542bd6f59SJacek Anaszewski 						ctrl_cfg->id,
47642bd6f59SJacek Anaszewski 						ctrl_cfg->min,
47742bd6f59SJacek Anaszewski 						ctrl_cfg->max,
47842bd6f59SJacek Anaszewski 						ctrl_cfg->step,
47942bd6f59SJacek Anaszewski 						ctrl_cfg->def);
48042bd6f59SJacek Anaszewski 
48142bd6f59SJacek Anaszewski 		if (ctrl)
48242bd6f59SJacek Anaszewski 			ctrl->flags |= ctrl_cfg->flags;
48342bd6f59SJacek Anaszewski 
48442bd6f59SJacek Anaszewski 		if (i <= STROBE_SOURCE)
48542bd6f59SJacek Anaszewski 			v4l2_flash->ctrls[i] = ctrl;
48642bd6f59SJacek Anaszewski 	}
48742bd6f59SJacek Anaszewski 
48842bd6f59SJacek Anaszewski 	kfree(ctrl_init_data);
48942bd6f59SJacek Anaszewski 
49042bd6f59SJacek Anaszewski 	if (v4l2_flash->hdl.error) {
49142bd6f59SJacek Anaszewski 		ret = v4l2_flash->hdl.error;
49242bd6f59SJacek Anaszewski 		goto error_free_handler;
49342bd6f59SJacek Anaszewski 	}
49442bd6f59SJacek Anaszewski 
49542bd6f59SJacek Anaszewski 	v4l2_ctrl_handler_setup(&v4l2_flash->hdl);
49642bd6f59SJacek Anaszewski 
49742bd6f59SJacek Anaszewski 	v4l2_flash->sd.ctrl_handler = &v4l2_flash->hdl;
49842bd6f59SJacek Anaszewski 
49942bd6f59SJacek Anaszewski 	return 0;
50042bd6f59SJacek Anaszewski 
50142bd6f59SJacek Anaszewski error_free_handler:
50242bd6f59SJacek Anaszewski 	v4l2_ctrl_handler_free(&v4l2_flash->hdl);
50342bd6f59SJacek Anaszewski 	return ret;
50442bd6f59SJacek Anaszewski }
50542bd6f59SJacek Anaszewski 
__sync_device_with_v4l2_controls(struct v4l2_flash * v4l2_flash)50642bd6f59SJacek Anaszewski static int __sync_device_with_v4l2_controls(struct v4l2_flash *v4l2_flash)
50742bd6f59SJacek Anaszewski {
50842bd6f59SJacek Anaszewski 	struct led_classdev_flash *fled_cdev = v4l2_flash->fled_cdev;
50942bd6f59SJacek Anaszewski 	struct v4l2_ctrl **ctrls = v4l2_flash->ctrls;
51042bd6f59SJacek Anaszewski 	int ret = 0;
51142bd6f59SJacek Anaszewski 
5124d7adf02SSakari Ailus 	if (ctrls[TORCH_INTENSITY]) {
5134d7adf02SSakari Ailus 		ret = v4l2_flash_set_led_brightness(v4l2_flash,
514503dd28aSSakari Ailus 						    ctrls[TORCH_INTENSITY]);
5154d7adf02SSakari Ailus 		if (ret < 0)
5164d7adf02SSakari Ailus 			return ret;
5174d7adf02SSakari Ailus 	}
51842bd6f59SJacek Anaszewski 
5194d7adf02SSakari Ailus 	if (ctrls[INDICATOR_INTENSITY]) {
5204d7adf02SSakari Ailus 		ret = v4l2_flash_set_led_brightness(v4l2_flash,
52142bd6f59SJacek Anaszewski 						    ctrls[INDICATOR_INTENSITY]);
5224d7adf02SSakari Ailus 		if (ret < 0)
5234d7adf02SSakari Ailus 			return ret;
5244d7adf02SSakari Ailus 	}
52542bd6f59SJacek Anaszewski 
52642bd6f59SJacek Anaszewski 	if (ctrls[FLASH_TIMEOUT]) {
527a40eba9bSSakari Ailus 		if (WARN_ON_ONCE(!fled_cdev))
528a40eba9bSSakari Ailus 			return -EINVAL;
529a40eba9bSSakari Ailus 
53042bd6f59SJacek Anaszewski 		ret = led_set_flash_timeout(fled_cdev,
53142bd6f59SJacek Anaszewski 					ctrls[FLASH_TIMEOUT]->val);
53242bd6f59SJacek Anaszewski 		if (ret < 0)
53342bd6f59SJacek Anaszewski 			return ret;
53442bd6f59SJacek Anaszewski 	}
53542bd6f59SJacek Anaszewski 
53642bd6f59SJacek Anaszewski 	if (ctrls[FLASH_INTENSITY]) {
537a40eba9bSSakari Ailus 		if (WARN_ON_ONCE(!fled_cdev))
538a40eba9bSSakari Ailus 			return -EINVAL;
539a40eba9bSSakari Ailus 
54042bd6f59SJacek Anaszewski 		ret = led_set_flash_brightness(fled_cdev,
54142bd6f59SJacek Anaszewski 					ctrls[FLASH_INTENSITY]->val);
54242bd6f59SJacek Anaszewski 		if (ret < 0)
54342bd6f59SJacek Anaszewski 			return ret;
54442bd6f59SJacek Anaszewski 	}
54542bd6f59SJacek Anaszewski 
54642bd6f59SJacek Anaszewski 	/*
54742bd6f59SJacek Anaszewski 	 * For some hardware arrangements setting strobe source may affect
54842bd6f59SJacek Anaszewski 	 * torch mode. Synchronize strobe source setting only if not in torch
54942bd6f59SJacek Anaszewski 	 * mode. For torch mode case it will get synchronized upon switching
55042bd6f59SJacek Anaszewski 	 * to flash mode.
55142bd6f59SJacek Anaszewski 	 */
55242bd6f59SJacek Anaszewski 	if (ctrls[STROBE_SOURCE] &&
55342bd6f59SJacek Anaszewski 	    ctrls[LED_MODE]->val != V4L2_FLASH_LED_MODE_TORCH)
55442bd6f59SJacek Anaszewski 		ret = call_flash_op(v4l2_flash, external_strobe_set,
55542bd6f59SJacek Anaszewski 					ctrls[STROBE_SOURCE]->val);
55642bd6f59SJacek Anaszewski 
55742bd6f59SJacek Anaszewski 	return ret;
55842bd6f59SJacek Anaszewski }
55942bd6f59SJacek Anaszewski 
56042bd6f59SJacek Anaszewski /*
56142bd6f59SJacek Anaszewski  * V4L2 subdev internal operations
56242bd6f59SJacek Anaszewski  */
56342bd6f59SJacek Anaszewski 
v4l2_flash_open(struct v4l2_subdev * sd,struct v4l2_subdev_fh * fh)56442bd6f59SJacek Anaszewski static int v4l2_flash_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
56542bd6f59SJacek Anaszewski {
56642bd6f59SJacek Anaszewski 	struct v4l2_flash *v4l2_flash = v4l2_subdev_to_v4l2_flash(sd);
56742bd6f59SJacek Anaszewski 	struct led_classdev_flash *fled_cdev = v4l2_flash->fled_cdev;
568503dd28aSSakari Ailus 	struct led_classdev *led_cdev = fled_cdev ? &fled_cdev->led_cdev : NULL;
56985f7ff97SSakari Ailus 	struct led_classdev *led_cdev_ind = v4l2_flash->iled_cdev;
57042bd6f59SJacek Anaszewski 	int ret = 0;
57142bd6f59SJacek Anaszewski 
57242bd6f59SJacek Anaszewski 	if (!v4l2_fh_is_singular(&fh->vfh))
57342bd6f59SJacek Anaszewski 		return 0;
57442bd6f59SJacek Anaszewski 
575503dd28aSSakari Ailus 	if (led_cdev) {
57642bd6f59SJacek Anaszewski 		mutex_lock(&led_cdev->led_access);
57742bd6f59SJacek Anaszewski 
57842bd6f59SJacek Anaszewski 		led_sysfs_disable(led_cdev);
57942bd6f59SJacek Anaszewski 		led_trigger_remove(led_cdev);
58042bd6f59SJacek Anaszewski 
58142bd6f59SJacek Anaszewski 		mutex_unlock(&led_cdev->led_access);
582503dd28aSSakari Ailus 	}
58342bd6f59SJacek Anaszewski 
58485f7ff97SSakari Ailus 	if (led_cdev_ind) {
58542bd6f59SJacek Anaszewski 		mutex_lock(&led_cdev_ind->led_access);
58642bd6f59SJacek Anaszewski 
58742bd6f59SJacek Anaszewski 		led_sysfs_disable(led_cdev_ind);
58842bd6f59SJacek Anaszewski 		led_trigger_remove(led_cdev_ind);
58942bd6f59SJacek Anaszewski 
59042bd6f59SJacek Anaszewski 		mutex_unlock(&led_cdev_ind->led_access);
59142bd6f59SJacek Anaszewski 	}
59242bd6f59SJacek Anaszewski 
59342bd6f59SJacek Anaszewski 	ret = __sync_device_with_v4l2_controls(v4l2_flash);
59442bd6f59SJacek Anaszewski 	if (ret < 0)
59542bd6f59SJacek Anaszewski 		goto out_sync_device;
59642bd6f59SJacek Anaszewski 
59742bd6f59SJacek Anaszewski 	return 0;
59842bd6f59SJacek Anaszewski out_sync_device:
599503dd28aSSakari Ailus 	if (led_cdev) {
60042bd6f59SJacek Anaszewski 		mutex_lock(&led_cdev->led_access);
60142bd6f59SJacek Anaszewski 		led_sysfs_enable(led_cdev);
60242bd6f59SJacek Anaszewski 		mutex_unlock(&led_cdev->led_access);
603503dd28aSSakari Ailus 	}
60442bd6f59SJacek Anaszewski 
60542bd6f59SJacek Anaszewski 	if (led_cdev_ind) {
60642bd6f59SJacek Anaszewski 		mutex_lock(&led_cdev_ind->led_access);
60742bd6f59SJacek Anaszewski 		led_sysfs_enable(led_cdev_ind);
60842bd6f59SJacek Anaszewski 		mutex_unlock(&led_cdev_ind->led_access);
60942bd6f59SJacek Anaszewski 	}
61042bd6f59SJacek Anaszewski 
61142bd6f59SJacek Anaszewski 	return ret;
61242bd6f59SJacek Anaszewski }
61342bd6f59SJacek Anaszewski 
v4l2_flash_close(struct v4l2_subdev * sd,struct v4l2_subdev_fh * fh)61442bd6f59SJacek Anaszewski static int v4l2_flash_close(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
61542bd6f59SJacek Anaszewski {
61642bd6f59SJacek Anaszewski 	struct v4l2_flash *v4l2_flash = v4l2_subdev_to_v4l2_flash(sd);
61742bd6f59SJacek Anaszewski 	struct led_classdev_flash *fled_cdev = v4l2_flash->fled_cdev;
618503dd28aSSakari Ailus 	struct led_classdev *led_cdev = fled_cdev ? &fled_cdev->led_cdev : NULL;
61985f7ff97SSakari Ailus 	struct led_classdev *led_cdev_ind = v4l2_flash->iled_cdev;
62042bd6f59SJacek Anaszewski 	int ret = 0;
62142bd6f59SJacek Anaszewski 
62242bd6f59SJacek Anaszewski 	if (!v4l2_fh_is_singular(&fh->vfh))
62342bd6f59SJacek Anaszewski 		return 0;
62442bd6f59SJacek Anaszewski 
625503dd28aSSakari Ailus 	if (led_cdev) {
62642bd6f59SJacek Anaszewski 		mutex_lock(&led_cdev->led_access);
62742bd6f59SJacek Anaszewski 
62842bd6f59SJacek Anaszewski 		if (v4l2_flash->ctrls[STROBE_SOURCE])
629503dd28aSSakari Ailus 			ret = v4l2_ctrl_s_ctrl(
630503dd28aSSakari Ailus 				v4l2_flash->ctrls[STROBE_SOURCE],
63142bd6f59SJacek Anaszewski 				V4L2_FLASH_STROBE_SOURCE_SOFTWARE);
63242bd6f59SJacek Anaszewski 		led_sysfs_enable(led_cdev);
63342bd6f59SJacek Anaszewski 
63442bd6f59SJacek Anaszewski 		mutex_unlock(&led_cdev->led_access);
635503dd28aSSakari Ailus 	}
63642bd6f59SJacek Anaszewski 
63785f7ff97SSakari Ailus 	if (led_cdev_ind) {
63842bd6f59SJacek Anaszewski 		mutex_lock(&led_cdev_ind->led_access);
63942bd6f59SJacek Anaszewski 		led_sysfs_enable(led_cdev_ind);
64042bd6f59SJacek Anaszewski 		mutex_unlock(&led_cdev_ind->led_access);
64142bd6f59SJacek Anaszewski 	}
64242bd6f59SJacek Anaszewski 
64342bd6f59SJacek Anaszewski 	return ret;
64442bd6f59SJacek Anaszewski }
64542bd6f59SJacek Anaszewski 
64642bd6f59SJacek Anaszewski static const struct v4l2_subdev_internal_ops v4l2_flash_subdev_internal_ops = {
64742bd6f59SJacek Anaszewski 	.open = v4l2_flash_open,
64842bd6f59SJacek Anaszewski 	.close = v4l2_flash_close,
64942bd6f59SJacek Anaszewski };
65042bd6f59SJacek Anaszewski 
6517853c237SHans Verkuil static const struct v4l2_subdev_ops v4l2_flash_subdev_ops;
65242bd6f59SJacek Anaszewski 
__v4l2_flash_init(struct device * dev,struct fwnode_handle * fwn,struct led_classdev_flash * fled_cdev,struct led_classdev * iled_cdev,const struct v4l2_flash_ops * ops,struct v4l2_flash_config * config)653503dd28aSSakari Ailus static struct v4l2_flash *__v4l2_flash_init(
654048ea05bSSakari Ailus 	struct device *dev, struct fwnode_handle *fwn,
655503dd28aSSakari Ailus 	struct led_classdev_flash *fled_cdev, struct led_classdev *iled_cdev,
656503dd28aSSakari Ailus 	const struct v4l2_flash_ops *ops, struct v4l2_flash_config *config)
65742bd6f59SJacek Anaszewski {
65842bd6f59SJacek Anaszewski 	struct v4l2_flash *v4l2_flash;
65942bd6f59SJacek Anaszewski 	struct v4l2_subdev *sd;
66042bd6f59SJacek Anaszewski 	int ret;
66142bd6f59SJacek Anaszewski 
662503dd28aSSakari Ailus 	if (!config)
66342bd6f59SJacek Anaszewski 		return ERR_PTR(-EINVAL);
66442bd6f59SJacek Anaszewski 
665503dd28aSSakari Ailus 	v4l2_flash = devm_kzalloc(dev, sizeof(*v4l2_flash), GFP_KERNEL);
66642bd6f59SJacek Anaszewski 	if (!v4l2_flash)
66742bd6f59SJacek Anaszewski 		return ERR_PTR(-ENOMEM);
66842bd6f59SJacek Anaszewski 
66942bd6f59SJacek Anaszewski 	sd = &v4l2_flash->sd;
67042bd6f59SJacek Anaszewski 	v4l2_flash->fled_cdev = fled_cdev;
67142bd6f59SJacek Anaszewski 	v4l2_flash->iled_cdev = iled_cdev;
67242bd6f59SJacek Anaszewski 	v4l2_flash->ops = ops;
67342bd6f59SJacek Anaszewski 	sd->dev = dev;
674503dd28aSSakari Ailus 	sd->fwnode = fwn ? fwn : dev_fwnode(dev);
67542bd6f59SJacek Anaszewski 	v4l2_subdev_init(sd, &v4l2_flash_subdev_ops);
67642bd6f59SJacek Anaszewski 	sd->internal_ops = &v4l2_flash_subdev_internal_ops;
67742bd6f59SJacek Anaszewski 	sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
678c0decac1SMauro Carvalho Chehab 	strscpy(sd->name, config->dev_name, sizeof(sd->name));
67942bd6f59SJacek Anaszewski 
680ab22e77cSMauro Carvalho Chehab 	ret = media_entity_pads_init(&sd->entity, 0, NULL);
68142bd6f59SJacek Anaszewski 	if (ret < 0)
68242bd6f59SJacek Anaszewski 		return ERR_PTR(ret);
68342bd6f59SJacek Anaszewski 
6844ca72efaSMauro Carvalho Chehab 	sd->entity.function = MEDIA_ENT_F_FLASH;
68542bd6f59SJacek Anaszewski 
68642bd6f59SJacek Anaszewski 	ret = v4l2_flash_init_controls(v4l2_flash, config);
68742bd6f59SJacek Anaszewski 	if (ret < 0)
68842bd6f59SJacek Anaszewski 		goto err_init_controls;
68942bd6f59SJacek Anaszewski 
690048ea05bSSakari Ailus 	fwnode_handle_get(sd->fwnode);
69142bd6f59SJacek Anaszewski 
69242bd6f59SJacek Anaszewski 	ret = v4l2_async_register_subdev(sd);
69342bd6f59SJacek Anaszewski 	if (ret < 0)
69442bd6f59SJacek Anaszewski 		goto err_async_register_sd;
69542bd6f59SJacek Anaszewski 
69642bd6f59SJacek Anaszewski 	return v4l2_flash;
69742bd6f59SJacek Anaszewski 
69842bd6f59SJacek Anaszewski err_async_register_sd:
699048ea05bSSakari Ailus 	fwnode_handle_put(sd->fwnode);
70042bd6f59SJacek Anaszewski 	v4l2_ctrl_handler_free(sd->ctrl_handler);
70142bd6f59SJacek Anaszewski err_init_controls:
70242bd6f59SJacek Anaszewski 	media_entity_cleanup(&sd->entity);
70342bd6f59SJacek Anaszewski 
70442bd6f59SJacek Anaszewski 	return ERR_PTR(ret);
70542bd6f59SJacek Anaszewski }
706503dd28aSSakari Ailus 
v4l2_flash_init(struct device * dev,struct fwnode_handle * fwn,struct led_classdev_flash * fled_cdev,const struct v4l2_flash_ops * ops,struct v4l2_flash_config * config)707503dd28aSSakari Ailus struct v4l2_flash *v4l2_flash_init(
708503dd28aSSakari Ailus 	struct device *dev, struct fwnode_handle *fwn,
709503dd28aSSakari Ailus 	struct led_classdev_flash *fled_cdev,
710503dd28aSSakari Ailus 	const struct v4l2_flash_ops *ops,
711503dd28aSSakari Ailus 	struct v4l2_flash_config *config)
712503dd28aSSakari Ailus {
713503dd28aSSakari Ailus 	return __v4l2_flash_init(dev, fwn, fled_cdev, NULL, ops, config);
714503dd28aSSakari Ailus }
71542bd6f59SJacek Anaszewski EXPORT_SYMBOL_GPL(v4l2_flash_init);
71642bd6f59SJacek Anaszewski 
v4l2_flash_indicator_init(struct device * dev,struct fwnode_handle * fwn,struct led_classdev * iled_cdev,struct v4l2_flash_config * config)717503dd28aSSakari Ailus struct v4l2_flash *v4l2_flash_indicator_init(
718503dd28aSSakari Ailus 	struct device *dev, struct fwnode_handle *fwn,
719503dd28aSSakari Ailus 	struct led_classdev *iled_cdev,
720503dd28aSSakari Ailus 	struct v4l2_flash_config *config)
721503dd28aSSakari Ailus {
722503dd28aSSakari Ailus 	return __v4l2_flash_init(dev, fwn, NULL, iled_cdev, NULL, config);
723503dd28aSSakari Ailus }
724503dd28aSSakari Ailus EXPORT_SYMBOL_GPL(v4l2_flash_indicator_init);
725503dd28aSSakari Ailus 
v4l2_flash_release(struct v4l2_flash * v4l2_flash)72642bd6f59SJacek Anaszewski void v4l2_flash_release(struct v4l2_flash *v4l2_flash)
72742bd6f59SJacek Anaszewski {
72842bd6f59SJacek Anaszewski 	struct v4l2_subdev *sd;
72942bd6f59SJacek Anaszewski 
73042bd6f59SJacek Anaszewski 	if (IS_ERR_OR_NULL(v4l2_flash))
73142bd6f59SJacek Anaszewski 		return;
73242bd6f59SJacek Anaszewski 
73342bd6f59SJacek Anaszewski 	sd = &v4l2_flash->sd;
73442bd6f59SJacek Anaszewski 
73542bd6f59SJacek Anaszewski 	v4l2_async_unregister_subdev(sd);
73642bd6f59SJacek Anaszewski 
737048ea05bSSakari Ailus 	fwnode_handle_put(sd->fwnode);
73842bd6f59SJacek Anaszewski 
73942bd6f59SJacek Anaszewski 	v4l2_ctrl_handler_free(sd->ctrl_handler);
74042bd6f59SJacek Anaszewski 	media_entity_cleanup(&sd->entity);
74142bd6f59SJacek Anaszewski }
74242bd6f59SJacek Anaszewski EXPORT_SYMBOL_GPL(v4l2_flash_release);
74342bd6f59SJacek Anaszewski 
74442bd6f59SJacek Anaszewski MODULE_AUTHOR("Jacek Anaszewski <j.anaszewski@samsung.com>");
74542bd6f59SJacek Anaszewski MODULE_DESCRIPTION("V4L2 Flash sub-device helpers");
74642bd6f59SJacek Anaszewski MODULE_LICENSE("GPL v2");
747