xref: /linux/drivers/media/platform/samsung/exynos4-is/fimc-isp.c (revision 06d07429858317ded2db7986113a9e0129cd599b)
1238c84f7SMauro Carvalho Chehab // SPDX-License-Identifier: GPL-2.0-only
2238c84f7SMauro Carvalho Chehab /*
3238c84f7SMauro Carvalho Chehab  * Samsung EXYNOS4x12 FIMC-IS (Imaging Subsystem) driver
4238c84f7SMauro Carvalho Chehab  *
5238c84f7SMauro Carvalho Chehab  * Copyright (C) 2013 Samsung Electronics Co., Ltd.
6238c84f7SMauro Carvalho Chehab  *
7238c84f7SMauro Carvalho Chehab  * Authors: Sylwester Nawrocki <s.nawrocki@samsung.com>
8238c84f7SMauro Carvalho Chehab  *          Younghwan Joo <yhwan.joo@samsung.com>
9238c84f7SMauro Carvalho Chehab  */
10238c84f7SMauro Carvalho Chehab #define pr_fmt(fmt) "%s:%d " fmt, __func__, __LINE__
11238c84f7SMauro Carvalho Chehab 
12238c84f7SMauro Carvalho Chehab #include <linux/device.h>
13238c84f7SMauro Carvalho Chehab #include <linux/errno.h>
14238c84f7SMauro Carvalho Chehab #include <linux/kernel.h>
15238c84f7SMauro Carvalho Chehab #include <linux/list.h>
16238c84f7SMauro Carvalho Chehab #include <linux/module.h>
17238c84f7SMauro Carvalho Chehab #include <linux/platform_device.h>
18238c84f7SMauro Carvalho Chehab #include <linux/printk.h>
19238c84f7SMauro Carvalho Chehab #include <linux/pm_runtime.h>
20238c84f7SMauro Carvalho Chehab #include <linux/slab.h>
21238c84f7SMauro Carvalho Chehab #include <linux/types.h>
22238c84f7SMauro Carvalho Chehab #include <media/v4l2-device.h>
23238c84f7SMauro Carvalho Chehab 
24238c84f7SMauro Carvalho Chehab #include "media-dev.h"
25238c84f7SMauro Carvalho Chehab #include "fimc-isp-video.h"
26238c84f7SMauro Carvalho Chehab #include "fimc-is-command.h"
27238c84f7SMauro Carvalho Chehab #include "fimc-is-param.h"
28238c84f7SMauro Carvalho Chehab #include "fimc-is-regs.h"
29238c84f7SMauro Carvalho Chehab #include "fimc-is.h"
30238c84f7SMauro Carvalho Chehab 
31238c84f7SMauro Carvalho Chehab int fimc_isp_debug;
32238c84f7SMauro Carvalho Chehab module_param_named(debug_isp, fimc_isp_debug, int, S_IRUGO | S_IWUSR);
33238c84f7SMauro Carvalho Chehab 
34238c84f7SMauro Carvalho Chehab static const struct fimc_fmt fimc_isp_formats[FIMC_ISP_NUM_FORMATS] = {
35238c84f7SMauro Carvalho Chehab 	{
36238c84f7SMauro Carvalho Chehab 		.fourcc		= V4L2_PIX_FMT_SGRBG8,
37238c84f7SMauro Carvalho Chehab 		.depth		= { 8 },
38238c84f7SMauro Carvalho Chehab 		.color		= FIMC_FMT_RAW8,
39238c84f7SMauro Carvalho Chehab 		.memplanes	= 1,
40238c84f7SMauro Carvalho Chehab 		.mbus_code	= MEDIA_BUS_FMT_SGRBG8_1X8,
41238c84f7SMauro Carvalho Chehab 	}, {
42238c84f7SMauro Carvalho Chehab 		.fourcc		= V4L2_PIX_FMT_SGRBG10,
43238c84f7SMauro Carvalho Chehab 		.depth		= { 10 },
44238c84f7SMauro Carvalho Chehab 		.color		= FIMC_FMT_RAW10,
45238c84f7SMauro Carvalho Chehab 		.memplanes	= 1,
46238c84f7SMauro Carvalho Chehab 		.mbus_code	= MEDIA_BUS_FMT_SGRBG10_1X10,
47238c84f7SMauro Carvalho Chehab 	}, {
48238c84f7SMauro Carvalho Chehab 		.fourcc		= V4L2_PIX_FMT_SGRBG12,
49238c84f7SMauro Carvalho Chehab 		.depth		= { 12 },
50238c84f7SMauro Carvalho Chehab 		.color		= FIMC_FMT_RAW12,
51238c84f7SMauro Carvalho Chehab 		.memplanes	= 1,
52238c84f7SMauro Carvalho Chehab 		.mbus_code	= MEDIA_BUS_FMT_SGRBG12_1X12,
53238c84f7SMauro Carvalho Chehab 	},
54238c84f7SMauro Carvalho Chehab };
55238c84f7SMauro Carvalho Chehab 
56238c84f7SMauro Carvalho Chehab /**
57238c84f7SMauro Carvalho Chehab  * fimc_isp_find_format - lookup color format by fourcc or media bus code
58238c84f7SMauro Carvalho Chehab  * @pixelformat: fourcc to match, ignored if null
59238c84f7SMauro Carvalho Chehab  * @mbus_code: media bus code to match, ignored if null
60238c84f7SMauro Carvalho Chehab  * @index: index to the fimc_isp_formats array, ignored if negative
61238c84f7SMauro Carvalho Chehab  */
fimc_isp_find_format(const u32 * pixelformat,const u32 * mbus_code,int index)62238c84f7SMauro Carvalho Chehab const struct fimc_fmt *fimc_isp_find_format(const u32 *pixelformat,
63238c84f7SMauro Carvalho Chehab 					const u32 *mbus_code, int index)
64238c84f7SMauro Carvalho Chehab {
65238c84f7SMauro Carvalho Chehab 	const struct fimc_fmt *fmt, *def_fmt = NULL;
66238c84f7SMauro Carvalho Chehab 	unsigned int i;
67238c84f7SMauro Carvalho Chehab 	int id = 0;
68238c84f7SMauro Carvalho Chehab 
69238c84f7SMauro Carvalho Chehab 	if (index >= (int)ARRAY_SIZE(fimc_isp_formats))
70238c84f7SMauro Carvalho Chehab 		return NULL;
71238c84f7SMauro Carvalho Chehab 
72238c84f7SMauro Carvalho Chehab 	for (i = 0; i < ARRAY_SIZE(fimc_isp_formats); ++i) {
73238c84f7SMauro Carvalho Chehab 		fmt = &fimc_isp_formats[i];
74238c84f7SMauro Carvalho Chehab 		if (pixelformat && fmt->fourcc == *pixelformat)
75238c84f7SMauro Carvalho Chehab 			return fmt;
76238c84f7SMauro Carvalho Chehab 		if (mbus_code && fmt->mbus_code == *mbus_code)
77238c84f7SMauro Carvalho Chehab 			return fmt;
78238c84f7SMauro Carvalho Chehab 		if (index == id)
79238c84f7SMauro Carvalho Chehab 			def_fmt = fmt;
80238c84f7SMauro Carvalho Chehab 		id++;
81238c84f7SMauro Carvalho Chehab 	}
82238c84f7SMauro Carvalho Chehab 	return def_fmt;
83238c84f7SMauro Carvalho Chehab }
84238c84f7SMauro Carvalho Chehab 
fimc_isp_irq_handler(struct fimc_is * is)85238c84f7SMauro Carvalho Chehab void fimc_isp_irq_handler(struct fimc_is *is)
86238c84f7SMauro Carvalho Chehab {
87238c84f7SMauro Carvalho Chehab 	is->i2h_cmd.args[0] = mcuctl_read(is, MCUCTL_REG_ISSR(20));
88238c84f7SMauro Carvalho Chehab 	is->i2h_cmd.args[1] = mcuctl_read(is, MCUCTL_REG_ISSR(21));
89238c84f7SMauro Carvalho Chehab 
90238c84f7SMauro Carvalho Chehab 	fimc_is_fw_clear_irq1(is, FIMC_IS_INT_FRAME_DONE_ISP);
91238c84f7SMauro Carvalho Chehab 	fimc_isp_video_irq_handler(is);
92238c84f7SMauro Carvalho Chehab 
93238c84f7SMauro Carvalho Chehab 	wake_up(&is->irq_queue);
94238c84f7SMauro Carvalho Chehab }
95238c84f7SMauro Carvalho Chehab 
96238c84f7SMauro Carvalho Chehab /* Capture subdev media entity operations */
fimc_is_link_setup(struct media_entity * entity,const struct media_pad * local,const struct media_pad * remote,u32 flags)97238c84f7SMauro Carvalho Chehab static int fimc_is_link_setup(struct media_entity *entity,
98238c84f7SMauro Carvalho Chehab 				const struct media_pad *local,
99238c84f7SMauro Carvalho Chehab 				const struct media_pad *remote, u32 flags)
100238c84f7SMauro Carvalho Chehab {
101238c84f7SMauro Carvalho Chehab 	return 0;
102238c84f7SMauro Carvalho Chehab }
103238c84f7SMauro Carvalho Chehab 
104238c84f7SMauro Carvalho Chehab static const struct media_entity_operations fimc_is_subdev_media_ops = {
105238c84f7SMauro Carvalho Chehab 	.link_setup = fimc_is_link_setup,
106238c84f7SMauro Carvalho Chehab };
107238c84f7SMauro Carvalho Chehab 
fimc_is_subdev_enum_mbus_code(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_mbus_code_enum * code)108238c84f7SMauro Carvalho Chehab static int fimc_is_subdev_enum_mbus_code(struct v4l2_subdev *sd,
109238c84f7SMauro Carvalho Chehab 				struct v4l2_subdev_state *sd_state,
110238c84f7SMauro Carvalho Chehab 				struct v4l2_subdev_mbus_code_enum *code)
111238c84f7SMauro Carvalho Chehab {
112238c84f7SMauro Carvalho Chehab 	const struct fimc_fmt *fmt;
113238c84f7SMauro Carvalho Chehab 
114238c84f7SMauro Carvalho Chehab 	fmt = fimc_isp_find_format(NULL, NULL, code->index);
115238c84f7SMauro Carvalho Chehab 	if (!fmt)
116238c84f7SMauro Carvalho Chehab 		return -EINVAL;
117238c84f7SMauro Carvalho Chehab 	code->code = fmt->mbus_code;
118238c84f7SMauro Carvalho Chehab 	return 0;
119238c84f7SMauro Carvalho Chehab }
120238c84f7SMauro Carvalho Chehab 
fimc_isp_subdev_get_fmt(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_format * fmt)121238c84f7SMauro Carvalho Chehab static int fimc_isp_subdev_get_fmt(struct v4l2_subdev *sd,
122238c84f7SMauro Carvalho Chehab 				   struct v4l2_subdev_state *sd_state,
123238c84f7SMauro Carvalho Chehab 				   struct v4l2_subdev_format *fmt)
124238c84f7SMauro Carvalho Chehab {
125238c84f7SMauro Carvalho Chehab 	struct fimc_isp *isp = v4l2_get_subdevdata(sd);
126238c84f7SMauro Carvalho Chehab 	struct v4l2_mbus_framefmt *mf = &fmt->format;
127238c84f7SMauro Carvalho Chehab 
128238c84f7SMauro Carvalho Chehab 	if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
129*bc0e8d91SSakari Ailus 		*mf = *v4l2_subdev_state_get_format(sd_state, fmt->pad);
130238c84f7SMauro Carvalho Chehab 		return 0;
131238c84f7SMauro Carvalho Chehab 	}
132238c84f7SMauro Carvalho Chehab 
133238c84f7SMauro Carvalho Chehab 	mf->colorspace = V4L2_COLORSPACE_SRGB;
134238c84f7SMauro Carvalho Chehab 
135238c84f7SMauro Carvalho Chehab 	mutex_lock(&isp->subdev_lock);
136238c84f7SMauro Carvalho Chehab 
137238c84f7SMauro Carvalho Chehab 	if (fmt->pad == FIMC_ISP_SD_PAD_SINK) {
138238c84f7SMauro Carvalho Chehab 		/* ISP OTF input image format */
139238c84f7SMauro Carvalho Chehab 		*mf = isp->sink_fmt;
140238c84f7SMauro Carvalho Chehab 	} else {
141238c84f7SMauro Carvalho Chehab 		/* ISP OTF output image format */
142238c84f7SMauro Carvalho Chehab 		*mf = isp->src_fmt;
143238c84f7SMauro Carvalho Chehab 
144238c84f7SMauro Carvalho Chehab 		if (fmt->pad == FIMC_ISP_SD_PAD_SRC_FIFO) {
145238c84f7SMauro Carvalho Chehab 			mf->colorspace = V4L2_COLORSPACE_JPEG;
146238c84f7SMauro Carvalho Chehab 			mf->code = MEDIA_BUS_FMT_YUV10_1X30;
147238c84f7SMauro Carvalho Chehab 		}
148238c84f7SMauro Carvalho Chehab 	}
149238c84f7SMauro Carvalho Chehab 
150238c84f7SMauro Carvalho Chehab 	mutex_unlock(&isp->subdev_lock);
151238c84f7SMauro Carvalho Chehab 
152238c84f7SMauro Carvalho Chehab 	isp_dbg(1, sd, "%s: pad%d: fmt: 0x%x, %dx%d\n", __func__,
153238c84f7SMauro Carvalho Chehab 		fmt->pad, mf->code, mf->width, mf->height);
154238c84f7SMauro Carvalho Chehab 
155238c84f7SMauro Carvalho Chehab 	return 0;
156238c84f7SMauro Carvalho Chehab }
157238c84f7SMauro Carvalho Chehab 
__isp_subdev_try_format(struct fimc_isp * isp,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_format * fmt)158238c84f7SMauro Carvalho Chehab static void __isp_subdev_try_format(struct fimc_isp *isp,
159238c84f7SMauro Carvalho Chehab 				    struct v4l2_subdev_state *sd_state,
160238c84f7SMauro Carvalho Chehab 				    struct v4l2_subdev_format *fmt)
161238c84f7SMauro Carvalho Chehab {
162238c84f7SMauro Carvalho Chehab 	struct v4l2_mbus_framefmt *mf = &fmt->format;
163238c84f7SMauro Carvalho Chehab 	struct v4l2_mbus_framefmt *format;
164238c84f7SMauro Carvalho Chehab 
165238c84f7SMauro Carvalho Chehab 	mf->colorspace = V4L2_COLORSPACE_SRGB;
166238c84f7SMauro Carvalho Chehab 
167238c84f7SMauro Carvalho Chehab 	if (fmt->pad == FIMC_ISP_SD_PAD_SINK) {
168238c84f7SMauro Carvalho Chehab 		v4l_bound_align_image(&mf->width, FIMC_ISP_SINK_WIDTH_MIN,
169238c84f7SMauro Carvalho Chehab 				FIMC_ISP_SINK_WIDTH_MAX, 0,
170238c84f7SMauro Carvalho Chehab 				&mf->height, FIMC_ISP_SINK_HEIGHT_MIN,
171238c84f7SMauro Carvalho Chehab 				FIMC_ISP_SINK_HEIGHT_MAX, 0, 0);
172238c84f7SMauro Carvalho Chehab 		mf->code = MEDIA_BUS_FMT_SGRBG10_1X10;
173238c84f7SMauro Carvalho Chehab 	} else {
174238c84f7SMauro Carvalho Chehab 		if (fmt->which == V4L2_SUBDEV_FORMAT_TRY)
175*bc0e8d91SSakari Ailus 			format = v4l2_subdev_state_get_format(sd_state,
176238c84f7SMauro Carvalho Chehab 							      FIMC_ISP_SD_PAD_SINK);
177238c84f7SMauro Carvalho Chehab 		else
178238c84f7SMauro Carvalho Chehab 			format = &isp->sink_fmt;
179238c84f7SMauro Carvalho Chehab 
180238c84f7SMauro Carvalho Chehab 		/* Allow changing format only on sink pad */
181238c84f7SMauro Carvalho Chehab 		mf->width = format->width - FIMC_ISP_CAC_MARGIN_WIDTH;
182238c84f7SMauro Carvalho Chehab 		mf->height = format->height - FIMC_ISP_CAC_MARGIN_HEIGHT;
183238c84f7SMauro Carvalho Chehab 
184238c84f7SMauro Carvalho Chehab 		if (fmt->pad == FIMC_ISP_SD_PAD_SRC_FIFO) {
185238c84f7SMauro Carvalho Chehab 			mf->code = MEDIA_BUS_FMT_YUV10_1X30;
186238c84f7SMauro Carvalho Chehab 			mf->colorspace = V4L2_COLORSPACE_JPEG;
187238c84f7SMauro Carvalho Chehab 		} else {
188238c84f7SMauro Carvalho Chehab 			mf->code = format->code;
189238c84f7SMauro Carvalho Chehab 		}
190238c84f7SMauro Carvalho Chehab 	}
191238c84f7SMauro Carvalho Chehab }
192238c84f7SMauro Carvalho Chehab 
fimc_isp_subdev_set_fmt(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_format * fmt)193238c84f7SMauro Carvalho Chehab static int fimc_isp_subdev_set_fmt(struct v4l2_subdev *sd,
194238c84f7SMauro Carvalho Chehab 				   struct v4l2_subdev_state *sd_state,
195238c84f7SMauro Carvalho Chehab 				   struct v4l2_subdev_format *fmt)
196238c84f7SMauro Carvalho Chehab {
197238c84f7SMauro Carvalho Chehab 	struct fimc_isp *isp = v4l2_get_subdevdata(sd);
198238c84f7SMauro Carvalho Chehab 	struct fimc_is *is = fimc_isp_to_is(isp);
199238c84f7SMauro Carvalho Chehab 	struct v4l2_mbus_framefmt *mf = &fmt->format;
200238c84f7SMauro Carvalho Chehab 	int ret = 0;
201238c84f7SMauro Carvalho Chehab 
202238c84f7SMauro Carvalho Chehab 	isp_dbg(1, sd, "%s: pad%d: code: 0x%x, %dx%d\n",
203238c84f7SMauro Carvalho Chehab 		 __func__, fmt->pad, mf->code, mf->width, mf->height);
204238c84f7SMauro Carvalho Chehab 
205238c84f7SMauro Carvalho Chehab 	mutex_lock(&isp->subdev_lock);
206238c84f7SMauro Carvalho Chehab 	__isp_subdev_try_format(isp, sd_state, fmt);
207238c84f7SMauro Carvalho Chehab 
208238c84f7SMauro Carvalho Chehab 	if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
209*bc0e8d91SSakari Ailus 		mf = v4l2_subdev_state_get_format(sd_state, fmt->pad);
210238c84f7SMauro Carvalho Chehab 		*mf = fmt->format;
211238c84f7SMauro Carvalho Chehab 
212238c84f7SMauro Carvalho Chehab 		/* Propagate format to the source pads */
213238c84f7SMauro Carvalho Chehab 		if (fmt->pad == FIMC_ISP_SD_PAD_SINK) {
214238c84f7SMauro Carvalho Chehab 			struct v4l2_subdev_format format = *fmt;
215238c84f7SMauro Carvalho Chehab 			unsigned int pad;
216238c84f7SMauro Carvalho Chehab 
217238c84f7SMauro Carvalho Chehab 			for (pad = FIMC_ISP_SD_PAD_SRC_FIFO;
218238c84f7SMauro Carvalho Chehab 					pad < FIMC_ISP_SD_PADS_NUM; pad++) {
219238c84f7SMauro Carvalho Chehab 				format.pad = pad;
220238c84f7SMauro Carvalho Chehab 				__isp_subdev_try_format(isp, sd_state,
221238c84f7SMauro Carvalho Chehab 							&format);
222*bc0e8d91SSakari Ailus 				mf = v4l2_subdev_state_get_format(sd_state,
223238c84f7SMauro Carvalho Chehab 								  pad);
224238c84f7SMauro Carvalho Chehab 				*mf = format.format;
225238c84f7SMauro Carvalho Chehab 			}
226238c84f7SMauro Carvalho Chehab 		}
227238c84f7SMauro Carvalho Chehab 	} else {
228238c84f7SMauro Carvalho Chehab 		if (!media_entity_is_streaming(&sd->entity)) {
229238c84f7SMauro Carvalho Chehab 			if (fmt->pad == FIMC_ISP_SD_PAD_SINK) {
230238c84f7SMauro Carvalho Chehab 				struct v4l2_subdev_format format = *fmt;
231238c84f7SMauro Carvalho Chehab 
232238c84f7SMauro Carvalho Chehab 				isp->sink_fmt = *mf;
233238c84f7SMauro Carvalho Chehab 
234238c84f7SMauro Carvalho Chehab 				format.pad = FIMC_ISP_SD_PAD_SRC_DMA;
235238c84f7SMauro Carvalho Chehab 				__isp_subdev_try_format(isp, sd_state,
236238c84f7SMauro Carvalho Chehab 							&format);
237238c84f7SMauro Carvalho Chehab 
238238c84f7SMauro Carvalho Chehab 				isp->src_fmt = format.format;
239238c84f7SMauro Carvalho Chehab 				__is_set_frame_size(is, &isp->src_fmt);
240238c84f7SMauro Carvalho Chehab 			} else {
241238c84f7SMauro Carvalho Chehab 				isp->src_fmt = *mf;
242238c84f7SMauro Carvalho Chehab 			}
243238c84f7SMauro Carvalho Chehab 		} else {
244238c84f7SMauro Carvalho Chehab 			ret = -EBUSY;
245238c84f7SMauro Carvalho Chehab 		}
246238c84f7SMauro Carvalho Chehab 	}
247238c84f7SMauro Carvalho Chehab 
248238c84f7SMauro Carvalho Chehab 	mutex_unlock(&isp->subdev_lock);
249238c84f7SMauro Carvalho Chehab 	return ret;
250238c84f7SMauro Carvalho Chehab }
251238c84f7SMauro Carvalho Chehab 
fimc_isp_subdev_s_stream(struct v4l2_subdev * sd,int on)252238c84f7SMauro Carvalho Chehab static int fimc_isp_subdev_s_stream(struct v4l2_subdev *sd, int on)
253238c84f7SMauro Carvalho Chehab {
254238c84f7SMauro Carvalho Chehab 	struct fimc_isp *isp = v4l2_get_subdevdata(sd);
255238c84f7SMauro Carvalho Chehab 	struct fimc_is *is = fimc_isp_to_is(isp);
256238c84f7SMauro Carvalho Chehab 	int ret;
257238c84f7SMauro Carvalho Chehab 
258238c84f7SMauro Carvalho Chehab 	isp_dbg(1, sd, "%s: on: %d\n", __func__, on);
259238c84f7SMauro Carvalho Chehab 
260238c84f7SMauro Carvalho Chehab 	if (!test_bit(IS_ST_INIT_DONE, &is->state))
261238c84f7SMauro Carvalho Chehab 		return -EBUSY;
262238c84f7SMauro Carvalho Chehab 
263238c84f7SMauro Carvalho Chehab 	fimc_is_mem_barrier();
264238c84f7SMauro Carvalho Chehab 
265238c84f7SMauro Carvalho Chehab 	if (on) {
266238c84f7SMauro Carvalho Chehab 		if (__get_pending_param_count(is)) {
267238c84f7SMauro Carvalho Chehab 			ret = fimc_is_itf_s_param(is, true);
268238c84f7SMauro Carvalho Chehab 			if (ret < 0)
269238c84f7SMauro Carvalho Chehab 				return ret;
270238c84f7SMauro Carvalho Chehab 		}
271238c84f7SMauro Carvalho Chehab 
272238c84f7SMauro Carvalho Chehab 		isp_dbg(1, sd, "changing mode to %d\n", is->config_index);
273238c84f7SMauro Carvalho Chehab 
274238c84f7SMauro Carvalho Chehab 		ret = fimc_is_itf_mode_change(is);
275238c84f7SMauro Carvalho Chehab 		if (ret)
276238c84f7SMauro Carvalho Chehab 			return -EINVAL;
277238c84f7SMauro Carvalho Chehab 
278238c84f7SMauro Carvalho Chehab 		clear_bit(IS_ST_STREAM_ON, &is->state);
279238c84f7SMauro Carvalho Chehab 		fimc_is_hw_stream_on(is);
280238c84f7SMauro Carvalho Chehab 		ret = fimc_is_wait_event(is, IS_ST_STREAM_ON, 1,
281238c84f7SMauro Carvalho Chehab 					 FIMC_IS_CONFIG_TIMEOUT);
282238c84f7SMauro Carvalho Chehab 		if (ret < 0) {
283238c84f7SMauro Carvalho Chehab 			v4l2_err(sd, "stream on timeout\n");
284238c84f7SMauro Carvalho Chehab 			return ret;
285238c84f7SMauro Carvalho Chehab 		}
286238c84f7SMauro Carvalho Chehab 	} else {
287238c84f7SMauro Carvalho Chehab 		clear_bit(IS_ST_STREAM_OFF, &is->state);
288238c84f7SMauro Carvalho Chehab 		fimc_is_hw_stream_off(is);
289238c84f7SMauro Carvalho Chehab 		ret = fimc_is_wait_event(is, IS_ST_STREAM_OFF, 1,
290238c84f7SMauro Carvalho Chehab 					 FIMC_IS_CONFIG_TIMEOUT);
291238c84f7SMauro Carvalho Chehab 		if (ret < 0) {
292238c84f7SMauro Carvalho Chehab 			v4l2_err(sd, "stream off timeout\n");
293238c84f7SMauro Carvalho Chehab 			return ret;
294238c84f7SMauro Carvalho Chehab 		}
295238c84f7SMauro Carvalho Chehab 		is->setfile.sub_index = 0;
296238c84f7SMauro Carvalho Chehab 	}
297238c84f7SMauro Carvalho Chehab 
298238c84f7SMauro Carvalho Chehab 	return 0;
299238c84f7SMauro Carvalho Chehab }
300238c84f7SMauro Carvalho Chehab 
fimc_isp_subdev_s_power(struct v4l2_subdev * sd,int on)301238c84f7SMauro Carvalho Chehab static int fimc_isp_subdev_s_power(struct v4l2_subdev *sd, int on)
302238c84f7SMauro Carvalho Chehab {
303238c84f7SMauro Carvalho Chehab 	struct fimc_isp *isp = v4l2_get_subdevdata(sd);
304238c84f7SMauro Carvalho Chehab 	struct fimc_is *is = fimc_isp_to_is(isp);
305238c84f7SMauro Carvalho Chehab 	int ret = 0;
306238c84f7SMauro Carvalho Chehab 
307238c84f7SMauro Carvalho Chehab 	pr_debug("on: %d\n", on);
308238c84f7SMauro Carvalho Chehab 
309238c84f7SMauro Carvalho Chehab 	if (on) {
310238c84f7SMauro Carvalho Chehab 		ret = pm_runtime_resume_and_get(&is->pdev->dev);
311238c84f7SMauro Carvalho Chehab 		if (ret < 0)
312238c84f7SMauro Carvalho Chehab 			return ret;
313238c84f7SMauro Carvalho Chehab 
314238c84f7SMauro Carvalho Chehab 		set_bit(IS_ST_PWR_ON, &is->state);
315238c84f7SMauro Carvalho Chehab 
316238c84f7SMauro Carvalho Chehab 		ret = fimc_is_start_firmware(is);
317238c84f7SMauro Carvalho Chehab 		if (ret < 0) {
318238c84f7SMauro Carvalho Chehab 			v4l2_err(sd, "firmware booting failed\n");
319238c84f7SMauro Carvalho Chehab 			pm_runtime_put(&is->pdev->dev);
320238c84f7SMauro Carvalho Chehab 			return ret;
321238c84f7SMauro Carvalho Chehab 		}
322238c84f7SMauro Carvalho Chehab 		set_bit(IS_ST_PWR_SUBIP_ON, &is->state);
323238c84f7SMauro Carvalho Chehab 
324238c84f7SMauro Carvalho Chehab 		ret = fimc_is_hw_initialize(is);
325238c84f7SMauro Carvalho Chehab 	} else {
326238c84f7SMauro Carvalho Chehab 		/* Close sensor */
327238c84f7SMauro Carvalho Chehab 		if (!test_bit(IS_ST_PWR_ON, &is->state)) {
328238c84f7SMauro Carvalho Chehab 			fimc_is_hw_close_sensor(is, 0);
329238c84f7SMauro Carvalho Chehab 
330238c84f7SMauro Carvalho Chehab 			ret = fimc_is_wait_event(is, IS_ST_OPEN_SENSOR, 0,
331238c84f7SMauro Carvalho Chehab 						 FIMC_IS_CONFIG_TIMEOUT);
332238c84f7SMauro Carvalho Chehab 			if (ret < 0) {
333238c84f7SMauro Carvalho Chehab 				v4l2_err(sd, "sensor close timeout\n");
334238c84f7SMauro Carvalho Chehab 				return ret;
335238c84f7SMauro Carvalho Chehab 			}
336238c84f7SMauro Carvalho Chehab 		}
337238c84f7SMauro Carvalho Chehab 
338238c84f7SMauro Carvalho Chehab 		/* SUB IP power off */
339238c84f7SMauro Carvalho Chehab 		if (test_bit(IS_ST_PWR_SUBIP_ON, &is->state)) {
340238c84f7SMauro Carvalho Chehab 			fimc_is_hw_subip_power_off(is);
341238c84f7SMauro Carvalho Chehab 			ret = fimc_is_wait_event(is, IS_ST_PWR_SUBIP_ON, 0,
342238c84f7SMauro Carvalho Chehab 						 FIMC_IS_CONFIG_TIMEOUT);
343238c84f7SMauro Carvalho Chehab 			if (ret < 0) {
344238c84f7SMauro Carvalho Chehab 				v4l2_err(sd, "sub-IP power off timeout\n");
345238c84f7SMauro Carvalho Chehab 				return ret;
346238c84f7SMauro Carvalho Chehab 			}
347238c84f7SMauro Carvalho Chehab 		}
348238c84f7SMauro Carvalho Chehab 
349238c84f7SMauro Carvalho Chehab 		fimc_is_cpu_set_power(is, 0);
350238c84f7SMauro Carvalho Chehab 		pm_runtime_put_sync(&is->pdev->dev);
351238c84f7SMauro Carvalho Chehab 
352238c84f7SMauro Carvalho Chehab 		clear_bit(IS_ST_PWR_ON, &is->state);
353238c84f7SMauro Carvalho Chehab 		clear_bit(IS_ST_INIT_DONE, &is->state);
354238c84f7SMauro Carvalho Chehab 		is->state = 0;
355238c84f7SMauro Carvalho Chehab 		is->config[is->config_index].p_region_index[0] = 0;
356238c84f7SMauro Carvalho Chehab 		is->config[is->config_index].p_region_index[1] = 0;
357238c84f7SMauro Carvalho Chehab 		set_bit(IS_ST_IDLE, &is->state);
358238c84f7SMauro Carvalho Chehab 		wmb();
359238c84f7SMauro Carvalho Chehab 	}
360238c84f7SMauro Carvalho Chehab 
361238c84f7SMauro Carvalho Chehab 	return ret;
362238c84f7SMauro Carvalho Chehab }
363238c84f7SMauro Carvalho Chehab 
fimc_isp_subdev_open(struct v4l2_subdev * sd,struct v4l2_subdev_fh * fh)364238c84f7SMauro Carvalho Chehab static int fimc_isp_subdev_open(struct v4l2_subdev *sd,
365238c84f7SMauro Carvalho Chehab 				struct v4l2_subdev_fh *fh)
366238c84f7SMauro Carvalho Chehab {
367238c84f7SMauro Carvalho Chehab 	struct v4l2_mbus_framefmt *format;
368238c84f7SMauro Carvalho Chehab 	struct v4l2_mbus_framefmt fmt = {
369238c84f7SMauro Carvalho Chehab 		.colorspace = V4L2_COLORSPACE_SRGB,
370238c84f7SMauro Carvalho Chehab 		.code = fimc_isp_formats[0].mbus_code,
371238c84f7SMauro Carvalho Chehab 		.width = DEFAULT_PREVIEW_STILL_WIDTH + FIMC_ISP_CAC_MARGIN_WIDTH,
372238c84f7SMauro Carvalho Chehab 		.height = DEFAULT_PREVIEW_STILL_HEIGHT + FIMC_ISP_CAC_MARGIN_HEIGHT,
373238c84f7SMauro Carvalho Chehab 		.field = V4L2_FIELD_NONE,
374238c84f7SMauro Carvalho Chehab 	};
375238c84f7SMauro Carvalho Chehab 
376*bc0e8d91SSakari Ailus 	format = v4l2_subdev_state_get_format(fh->state, FIMC_ISP_SD_PAD_SINK);
377238c84f7SMauro Carvalho Chehab 	*format = fmt;
378238c84f7SMauro Carvalho Chehab 
379*bc0e8d91SSakari Ailus 	format = v4l2_subdev_state_get_format(fh->state,
380238c84f7SMauro Carvalho Chehab 					      FIMC_ISP_SD_PAD_SRC_FIFO);
381238c84f7SMauro Carvalho Chehab 	fmt.width = DEFAULT_PREVIEW_STILL_WIDTH;
382238c84f7SMauro Carvalho Chehab 	fmt.height = DEFAULT_PREVIEW_STILL_HEIGHT;
383238c84f7SMauro Carvalho Chehab 	*format = fmt;
384238c84f7SMauro Carvalho Chehab 
385*bc0e8d91SSakari Ailus 	format = v4l2_subdev_state_get_format(fh->state,
386238c84f7SMauro Carvalho Chehab 					      FIMC_ISP_SD_PAD_SRC_DMA);
387238c84f7SMauro Carvalho Chehab 	*format = fmt;
388238c84f7SMauro Carvalho Chehab 
389238c84f7SMauro Carvalho Chehab 	return 0;
390238c84f7SMauro Carvalho Chehab }
391238c84f7SMauro Carvalho Chehab 
fimc_isp_subdev_registered(struct v4l2_subdev * sd)392238c84f7SMauro Carvalho Chehab static int fimc_isp_subdev_registered(struct v4l2_subdev *sd)
393238c84f7SMauro Carvalho Chehab {
394238c84f7SMauro Carvalho Chehab 	struct fimc_isp *isp = v4l2_get_subdevdata(sd);
395238c84f7SMauro Carvalho Chehab 	int ret;
396238c84f7SMauro Carvalho Chehab 
397238c84f7SMauro Carvalho Chehab 	/* Use pipeline object allocated by the media device. */
398238c84f7SMauro Carvalho Chehab 	isp->video_capture.ve.pipe = v4l2_get_subdev_hostdata(sd);
399238c84f7SMauro Carvalho Chehab 
400238c84f7SMauro Carvalho Chehab 	ret = fimc_isp_video_device_register(isp, sd->v4l2_dev,
401238c84f7SMauro Carvalho Chehab 			V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
402238c84f7SMauro Carvalho Chehab 	if (ret < 0)
403238c84f7SMauro Carvalho Chehab 		isp->video_capture.ve.pipe = NULL;
404238c84f7SMauro Carvalho Chehab 
405238c84f7SMauro Carvalho Chehab 	return ret;
406238c84f7SMauro Carvalho Chehab }
407238c84f7SMauro Carvalho Chehab 
fimc_isp_subdev_unregistered(struct v4l2_subdev * sd)408238c84f7SMauro Carvalho Chehab static void fimc_isp_subdev_unregistered(struct v4l2_subdev *sd)
409238c84f7SMauro Carvalho Chehab {
410238c84f7SMauro Carvalho Chehab 	struct fimc_isp *isp = v4l2_get_subdevdata(sd);
411238c84f7SMauro Carvalho Chehab 
412238c84f7SMauro Carvalho Chehab 	fimc_isp_video_device_unregister(isp,
413238c84f7SMauro Carvalho Chehab 			V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
414238c84f7SMauro Carvalho Chehab }
415238c84f7SMauro Carvalho Chehab 
416238c84f7SMauro Carvalho Chehab static const struct v4l2_subdev_internal_ops fimc_is_subdev_internal_ops = {
417238c84f7SMauro Carvalho Chehab 	.registered = fimc_isp_subdev_registered,
418238c84f7SMauro Carvalho Chehab 	.unregistered = fimc_isp_subdev_unregistered,
419238c84f7SMauro Carvalho Chehab 	.open = fimc_isp_subdev_open,
420238c84f7SMauro Carvalho Chehab };
421238c84f7SMauro Carvalho Chehab 
422238c84f7SMauro Carvalho Chehab static const struct v4l2_subdev_pad_ops fimc_is_subdev_pad_ops = {
423238c84f7SMauro Carvalho Chehab 	.enum_mbus_code = fimc_is_subdev_enum_mbus_code,
424238c84f7SMauro Carvalho Chehab 	.get_fmt = fimc_isp_subdev_get_fmt,
425238c84f7SMauro Carvalho Chehab 	.set_fmt = fimc_isp_subdev_set_fmt,
426238c84f7SMauro Carvalho Chehab };
427238c84f7SMauro Carvalho Chehab 
428238c84f7SMauro Carvalho Chehab static const struct v4l2_subdev_video_ops fimc_is_subdev_video_ops = {
429238c84f7SMauro Carvalho Chehab 	.s_stream = fimc_isp_subdev_s_stream,
430238c84f7SMauro Carvalho Chehab };
431238c84f7SMauro Carvalho Chehab 
432238c84f7SMauro Carvalho Chehab static const struct v4l2_subdev_core_ops fimc_is_core_ops = {
433238c84f7SMauro Carvalho Chehab 	.s_power = fimc_isp_subdev_s_power,
434238c84f7SMauro Carvalho Chehab };
435238c84f7SMauro Carvalho Chehab 
436238c84f7SMauro Carvalho Chehab static const struct v4l2_subdev_ops fimc_is_subdev_ops = {
437238c84f7SMauro Carvalho Chehab 	.core = &fimc_is_core_ops,
438238c84f7SMauro Carvalho Chehab 	.video = &fimc_is_subdev_video_ops,
439238c84f7SMauro Carvalho Chehab 	.pad = &fimc_is_subdev_pad_ops,
440238c84f7SMauro Carvalho Chehab };
441238c84f7SMauro Carvalho Chehab 
__ctrl_set_white_balance(struct fimc_is * is,int value)442238c84f7SMauro Carvalho Chehab static int __ctrl_set_white_balance(struct fimc_is *is, int value)
443238c84f7SMauro Carvalho Chehab {
444238c84f7SMauro Carvalho Chehab 	switch (value) {
445238c84f7SMauro Carvalho Chehab 	case V4L2_WHITE_BALANCE_AUTO:
446238c84f7SMauro Carvalho Chehab 		__is_set_isp_awb(is, ISP_AWB_COMMAND_AUTO, 0);
447238c84f7SMauro Carvalho Chehab 		break;
448238c84f7SMauro Carvalho Chehab 	case V4L2_WHITE_BALANCE_DAYLIGHT:
449238c84f7SMauro Carvalho Chehab 		__is_set_isp_awb(is, ISP_AWB_COMMAND_ILLUMINATION,
450238c84f7SMauro Carvalho Chehab 					ISP_AWB_ILLUMINATION_DAYLIGHT);
451238c84f7SMauro Carvalho Chehab 		break;
452238c84f7SMauro Carvalho Chehab 	case V4L2_WHITE_BALANCE_CLOUDY:
453238c84f7SMauro Carvalho Chehab 		__is_set_isp_awb(is, ISP_AWB_COMMAND_ILLUMINATION,
454238c84f7SMauro Carvalho Chehab 					ISP_AWB_ILLUMINATION_CLOUDY);
455238c84f7SMauro Carvalho Chehab 		break;
456238c84f7SMauro Carvalho Chehab 	case V4L2_WHITE_BALANCE_INCANDESCENT:
457238c84f7SMauro Carvalho Chehab 		__is_set_isp_awb(is, ISP_AWB_COMMAND_ILLUMINATION,
458238c84f7SMauro Carvalho Chehab 					ISP_AWB_ILLUMINATION_TUNGSTEN);
459238c84f7SMauro Carvalho Chehab 		break;
460238c84f7SMauro Carvalho Chehab 	case V4L2_WHITE_BALANCE_FLUORESCENT:
461238c84f7SMauro Carvalho Chehab 		__is_set_isp_awb(is, ISP_AWB_COMMAND_ILLUMINATION,
462238c84f7SMauro Carvalho Chehab 					ISP_AWB_ILLUMINATION_FLUORESCENT);
463238c84f7SMauro Carvalho Chehab 		break;
464238c84f7SMauro Carvalho Chehab 	default:
465238c84f7SMauro Carvalho Chehab 		return -EINVAL;
466238c84f7SMauro Carvalho Chehab 	}
467238c84f7SMauro Carvalho Chehab 
468238c84f7SMauro Carvalho Chehab 	return 0;
469238c84f7SMauro Carvalho Chehab }
470238c84f7SMauro Carvalho Chehab 
__ctrl_set_aewb_lock(struct fimc_is * is,struct v4l2_ctrl * ctrl)471238c84f7SMauro Carvalho Chehab static int __ctrl_set_aewb_lock(struct fimc_is *is,
472238c84f7SMauro Carvalho Chehab 				      struct v4l2_ctrl *ctrl)
473238c84f7SMauro Carvalho Chehab {
474238c84f7SMauro Carvalho Chehab 	bool awb_lock = ctrl->val & V4L2_LOCK_WHITE_BALANCE;
475238c84f7SMauro Carvalho Chehab 	bool ae_lock = ctrl->val & V4L2_LOCK_EXPOSURE;
476238c84f7SMauro Carvalho Chehab 	struct isp_param *isp = &is->is_p_region->parameter.isp;
477238c84f7SMauro Carvalho Chehab 	int cmd, ret;
478238c84f7SMauro Carvalho Chehab 
479238c84f7SMauro Carvalho Chehab 	cmd = ae_lock ? ISP_AA_COMMAND_STOP : ISP_AA_COMMAND_START;
480238c84f7SMauro Carvalho Chehab 	isp->aa.cmd = cmd;
481238c84f7SMauro Carvalho Chehab 	isp->aa.target = ISP_AA_TARGET_AE;
482238c84f7SMauro Carvalho Chehab 	fimc_is_set_param_bit(is, PARAM_ISP_AA);
483238c84f7SMauro Carvalho Chehab 	is->af.ae_lock_state = ae_lock;
484238c84f7SMauro Carvalho Chehab 	wmb();
485238c84f7SMauro Carvalho Chehab 
486238c84f7SMauro Carvalho Chehab 	ret = fimc_is_itf_s_param(is, false);
487238c84f7SMauro Carvalho Chehab 	if (ret < 0)
488238c84f7SMauro Carvalho Chehab 		return ret;
489238c84f7SMauro Carvalho Chehab 
490238c84f7SMauro Carvalho Chehab 	cmd = awb_lock ? ISP_AA_COMMAND_STOP : ISP_AA_COMMAND_START;
491238c84f7SMauro Carvalho Chehab 	isp->aa.cmd = cmd;
492238c84f7SMauro Carvalho Chehab 	isp->aa.target = ISP_AA_TARGET_AE;
493238c84f7SMauro Carvalho Chehab 	fimc_is_set_param_bit(is, PARAM_ISP_AA);
494238c84f7SMauro Carvalho Chehab 	is->af.awb_lock_state = awb_lock;
495238c84f7SMauro Carvalho Chehab 	wmb();
496238c84f7SMauro Carvalho Chehab 
497238c84f7SMauro Carvalho Chehab 	return fimc_is_itf_s_param(is, false);
498238c84f7SMauro Carvalho Chehab }
499238c84f7SMauro Carvalho Chehab 
500238c84f7SMauro Carvalho Chehab /* Supported manual ISO values */
501238c84f7SMauro Carvalho Chehab static const s64 iso_qmenu[] = {
502238c84f7SMauro Carvalho Chehab 	50, 100, 200, 400, 800,
503238c84f7SMauro Carvalho Chehab };
504238c84f7SMauro Carvalho Chehab 
__ctrl_set_iso(struct fimc_is * is,int value)505238c84f7SMauro Carvalho Chehab static int __ctrl_set_iso(struct fimc_is *is, int value)
506238c84f7SMauro Carvalho Chehab {
507238c84f7SMauro Carvalho Chehab 	unsigned int idx, iso;
508238c84f7SMauro Carvalho Chehab 
509238c84f7SMauro Carvalho Chehab 	if (value == V4L2_ISO_SENSITIVITY_AUTO) {
510238c84f7SMauro Carvalho Chehab 		__is_set_isp_iso(is, ISP_ISO_COMMAND_AUTO, 0);
511238c84f7SMauro Carvalho Chehab 		return 0;
512238c84f7SMauro Carvalho Chehab 	}
513238c84f7SMauro Carvalho Chehab 	idx = is->isp.ctrls.iso->val;
514238c84f7SMauro Carvalho Chehab 	if (idx >= ARRAY_SIZE(iso_qmenu))
515238c84f7SMauro Carvalho Chehab 		return -EINVAL;
516238c84f7SMauro Carvalho Chehab 
517238c84f7SMauro Carvalho Chehab 	iso = iso_qmenu[idx];
518238c84f7SMauro Carvalho Chehab 	__is_set_isp_iso(is, ISP_ISO_COMMAND_MANUAL, iso);
519238c84f7SMauro Carvalho Chehab 	return 0;
520238c84f7SMauro Carvalho Chehab }
521238c84f7SMauro Carvalho Chehab 
__ctrl_set_metering(struct fimc_is * is,unsigned int value)522238c84f7SMauro Carvalho Chehab static int __ctrl_set_metering(struct fimc_is *is, unsigned int value)
523238c84f7SMauro Carvalho Chehab {
524238c84f7SMauro Carvalho Chehab 	unsigned int val;
525238c84f7SMauro Carvalho Chehab 
526238c84f7SMauro Carvalho Chehab 	switch (value) {
527238c84f7SMauro Carvalho Chehab 	case V4L2_EXPOSURE_METERING_AVERAGE:
528238c84f7SMauro Carvalho Chehab 		val = ISP_METERING_COMMAND_AVERAGE;
529238c84f7SMauro Carvalho Chehab 		break;
530238c84f7SMauro Carvalho Chehab 	case V4L2_EXPOSURE_METERING_CENTER_WEIGHTED:
531238c84f7SMauro Carvalho Chehab 		val = ISP_METERING_COMMAND_CENTER;
532238c84f7SMauro Carvalho Chehab 		break;
533238c84f7SMauro Carvalho Chehab 	case V4L2_EXPOSURE_METERING_SPOT:
534238c84f7SMauro Carvalho Chehab 		val = ISP_METERING_COMMAND_SPOT;
535238c84f7SMauro Carvalho Chehab 		break;
536238c84f7SMauro Carvalho Chehab 	case V4L2_EXPOSURE_METERING_MATRIX:
537238c84f7SMauro Carvalho Chehab 		val = ISP_METERING_COMMAND_MATRIX;
538238c84f7SMauro Carvalho Chehab 		break;
539238c84f7SMauro Carvalho Chehab 	default:
540238c84f7SMauro Carvalho Chehab 		return -EINVAL;
541238c84f7SMauro Carvalho Chehab 	}
542238c84f7SMauro Carvalho Chehab 
543238c84f7SMauro Carvalho Chehab 	__is_set_isp_metering(is, IS_METERING_CONFIG_CMD, val);
544238c84f7SMauro Carvalho Chehab 	return 0;
545238c84f7SMauro Carvalho Chehab }
546238c84f7SMauro Carvalho Chehab 
__ctrl_set_afc(struct fimc_is * is,int value)547238c84f7SMauro Carvalho Chehab static int __ctrl_set_afc(struct fimc_is *is, int value)
548238c84f7SMauro Carvalho Chehab {
549238c84f7SMauro Carvalho Chehab 	switch (value) {
550238c84f7SMauro Carvalho Chehab 	case V4L2_CID_POWER_LINE_FREQUENCY_DISABLED:
551238c84f7SMauro Carvalho Chehab 		__is_set_isp_afc(is, ISP_AFC_COMMAND_DISABLE, 0);
552238c84f7SMauro Carvalho Chehab 		break;
553238c84f7SMauro Carvalho Chehab 	case V4L2_CID_POWER_LINE_FREQUENCY_50HZ:
554238c84f7SMauro Carvalho Chehab 		__is_set_isp_afc(is, ISP_AFC_COMMAND_MANUAL, 50);
555238c84f7SMauro Carvalho Chehab 		break;
556238c84f7SMauro Carvalho Chehab 	case V4L2_CID_POWER_LINE_FREQUENCY_60HZ:
557238c84f7SMauro Carvalho Chehab 		__is_set_isp_afc(is, ISP_AFC_COMMAND_MANUAL, 60);
558238c84f7SMauro Carvalho Chehab 		break;
559238c84f7SMauro Carvalho Chehab 	case V4L2_CID_POWER_LINE_FREQUENCY_AUTO:
560238c84f7SMauro Carvalho Chehab 		__is_set_isp_afc(is, ISP_AFC_COMMAND_AUTO, 0);
561238c84f7SMauro Carvalho Chehab 		break;
562238c84f7SMauro Carvalho Chehab 	default:
563238c84f7SMauro Carvalho Chehab 		return -EINVAL;
564238c84f7SMauro Carvalho Chehab 	}
565238c84f7SMauro Carvalho Chehab 
566238c84f7SMauro Carvalho Chehab 	return 0;
567238c84f7SMauro Carvalho Chehab }
568238c84f7SMauro Carvalho Chehab 
__ctrl_set_image_effect(struct fimc_is * is,int value)569238c84f7SMauro Carvalho Chehab static int __ctrl_set_image_effect(struct fimc_is *is, int value)
570238c84f7SMauro Carvalho Chehab {
571238c84f7SMauro Carvalho Chehab 	static const u8 effects[][2] = {
572238c84f7SMauro Carvalho Chehab 		{ V4L2_COLORFX_NONE,	 ISP_IMAGE_EFFECT_DISABLE },
573238c84f7SMauro Carvalho Chehab 		{ V4L2_COLORFX_BW,	 ISP_IMAGE_EFFECT_MONOCHROME },
574238c84f7SMauro Carvalho Chehab 		{ V4L2_COLORFX_SEPIA,	 ISP_IMAGE_EFFECT_SEPIA },
575238c84f7SMauro Carvalho Chehab 		{ V4L2_COLORFX_NEGATIVE, ISP_IMAGE_EFFECT_NEGATIVE_MONO },
576238c84f7SMauro Carvalho Chehab 		{ 16 /* TODO */,	 ISP_IMAGE_EFFECT_NEGATIVE_COLOR },
577238c84f7SMauro Carvalho Chehab 	};
578238c84f7SMauro Carvalho Chehab 	int i;
579238c84f7SMauro Carvalho Chehab 
580238c84f7SMauro Carvalho Chehab 	for (i = 0; i < ARRAY_SIZE(effects); i++) {
581238c84f7SMauro Carvalho Chehab 		if (effects[i][0] != value)
582238c84f7SMauro Carvalho Chehab 			continue;
583238c84f7SMauro Carvalho Chehab 
584238c84f7SMauro Carvalho Chehab 		__is_set_isp_effect(is, effects[i][1]);
585238c84f7SMauro Carvalho Chehab 		return 0;
586238c84f7SMauro Carvalho Chehab 	}
587238c84f7SMauro Carvalho Chehab 
588238c84f7SMauro Carvalho Chehab 	return -EINVAL;
589238c84f7SMauro Carvalho Chehab }
590238c84f7SMauro Carvalho Chehab 
fimc_is_s_ctrl(struct v4l2_ctrl * ctrl)591238c84f7SMauro Carvalho Chehab static int fimc_is_s_ctrl(struct v4l2_ctrl *ctrl)
592238c84f7SMauro Carvalho Chehab {
593238c84f7SMauro Carvalho Chehab 	struct fimc_isp *isp = ctrl_to_fimc_isp(ctrl);
594238c84f7SMauro Carvalho Chehab 	struct fimc_is *is = fimc_isp_to_is(isp);
595238c84f7SMauro Carvalho Chehab 	bool set_param = true;
596238c84f7SMauro Carvalho Chehab 	int ret = 0;
597238c84f7SMauro Carvalho Chehab 
598238c84f7SMauro Carvalho Chehab 	switch (ctrl->id) {
599238c84f7SMauro Carvalho Chehab 	case V4L2_CID_CONTRAST:
600238c84f7SMauro Carvalho Chehab 		__is_set_isp_adjust(is, ISP_ADJUST_COMMAND_MANUAL_CONTRAST,
601238c84f7SMauro Carvalho Chehab 				    ctrl->val);
602238c84f7SMauro Carvalho Chehab 		break;
603238c84f7SMauro Carvalho Chehab 
604238c84f7SMauro Carvalho Chehab 	case V4L2_CID_SATURATION:
605238c84f7SMauro Carvalho Chehab 		__is_set_isp_adjust(is, ISP_ADJUST_COMMAND_MANUAL_SATURATION,
606238c84f7SMauro Carvalho Chehab 				    ctrl->val);
607238c84f7SMauro Carvalho Chehab 		break;
608238c84f7SMauro Carvalho Chehab 
609238c84f7SMauro Carvalho Chehab 	case V4L2_CID_SHARPNESS:
610238c84f7SMauro Carvalho Chehab 		__is_set_isp_adjust(is, ISP_ADJUST_COMMAND_MANUAL_SHARPNESS,
611238c84f7SMauro Carvalho Chehab 				    ctrl->val);
612238c84f7SMauro Carvalho Chehab 		break;
613238c84f7SMauro Carvalho Chehab 
614238c84f7SMauro Carvalho Chehab 	case V4L2_CID_EXPOSURE_ABSOLUTE:
615238c84f7SMauro Carvalho Chehab 		__is_set_isp_adjust(is, ISP_ADJUST_COMMAND_MANUAL_EXPOSURE,
616238c84f7SMauro Carvalho Chehab 				    ctrl->val);
617238c84f7SMauro Carvalho Chehab 		break;
618238c84f7SMauro Carvalho Chehab 
619238c84f7SMauro Carvalho Chehab 	case V4L2_CID_BRIGHTNESS:
620238c84f7SMauro Carvalho Chehab 		__is_set_isp_adjust(is, ISP_ADJUST_COMMAND_MANUAL_BRIGHTNESS,
621238c84f7SMauro Carvalho Chehab 				    ctrl->val);
622238c84f7SMauro Carvalho Chehab 		break;
623238c84f7SMauro Carvalho Chehab 
624238c84f7SMauro Carvalho Chehab 	case V4L2_CID_HUE:
625238c84f7SMauro Carvalho Chehab 		__is_set_isp_adjust(is, ISP_ADJUST_COMMAND_MANUAL_HUE,
626238c84f7SMauro Carvalho Chehab 				    ctrl->val);
627238c84f7SMauro Carvalho Chehab 		break;
628238c84f7SMauro Carvalho Chehab 
629238c84f7SMauro Carvalho Chehab 	case V4L2_CID_EXPOSURE_METERING:
630238c84f7SMauro Carvalho Chehab 		ret = __ctrl_set_metering(is, ctrl->val);
631238c84f7SMauro Carvalho Chehab 		break;
632238c84f7SMauro Carvalho Chehab 
633238c84f7SMauro Carvalho Chehab 	case V4L2_CID_AUTO_N_PRESET_WHITE_BALANCE:
634238c84f7SMauro Carvalho Chehab 		ret = __ctrl_set_white_balance(is, ctrl->val);
635238c84f7SMauro Carvalho Chehab 		break;
636238c84f7SMauro Carvalho Chehab 
637238c84f7SMauro Carvalho Chehab 	case V4L2_CID_3A_LOCK:
638238c84f7SMauro Carvalho Chehab 		ret = __ctrl_set_aewb_lock(is, ctrl);
639238c84f7SMauro Carvalho Chehab 		set_param = false;
640238c84f7SMauro Carvalho Chehab 		break;
641238c84f7SMauro Carvalho Chehab 
642238c84f7SMauro Carvalho Chehab 	case V4L2_CID_ISO_SENSITIVITY_AUTO:
643238c84f7SMauro Carvalho Chehab 		ret = __ctrl_set_iso(is, ctrl->val);
644238c84f7SMauro Carvalho Chehab 		break;
645238c84f7SMauro Carvalho Chehab 
646238c84f7SMauro Carvalho Chehab 	case V4L2_CID_POWER_LINE_FREQUENCY:
647238c84f7SMauro Carvalho Chehab 		ret = __ctrl_set_afc(is, ctrl->val);
648238c84f7SMauro Carvalho Chehab 		break;
649238c84f7SMauro Carvalho Chehab 
650238c84f7SMauro Carvalho Chehab 	case V4L2_CID_COLORFX:
651238c84f7SMauro Carvalho Chehab 		__ctrl_set_image_effect(is, ctrl->val);
652238c84f7SMauro Carvalho Chehab 		break;
653238c84f7SMauro Carvalho Chehab 
654238c84f7SMauro Carvalho Chehab 	default:
655238c84f7SMauro Carvalho Chehab 		ret = -EINVAL;
656238c84f7SMauro Carvalho Chehab 		break;
657238c84f7SMauro Carvalho Chehab 	}
658238c84f7SMauro Carvalho Chehab 
659238c84f7SMauro Carvalho Chehab 	if (ret < 0) {
660238c84f7SMauro Carvalho Chehab 		v4l2_err(&isp->subdev, "Failed to set control: %s (%d)\n",
661238c84f7SMauro Carvalho Chehab 						ctrl->name, ctrl->val);
662238c84f7SMauro Carvalho Chehab 		return ret;
663238c84f7SMauro Carvalho Chehab 	}
664238c84f7SMauro Carvalho Chehab 
665238c84f7SMauro Carvalho Chehab 	if (set_param && test_bit(IS_ST_STREAM_ON, &is->state))
666238c84f7SMauro Carvalho Chehab 		return fimc_is_itf_s_param(is, true);
667238c84f7SMauro Carvalho Chehab 
668238c84f7SMauro Carvalho Chehab 	return 0;
669238c84f7SMauro Carvalho Chehab }
670238c84f7SMauro Carvalho Chehab 
671238c84f7SMauro Carvalho Chehab static const struct v4l2_ctrl_ops fimc_isp_ctrl_ops = {
672238c84f7SMauro Carvalho Chehab 	.s_ctrl	= fimc_is_s_ctrl,
673238c84f7SMauro Carvalho Chehab };
674238c84f7SMauro Carvalho Chehab 
__isp_subdev_set_default_format(struct fimc_isp * isp)675238c84f7SMauro Carvalho Chehab static void __isp_subdev_set_default_format(struct fimc_isp *isp)
676238c84f7SMauro Carvalho Chehab {
677238c84f7SMauro Carvalho Chehab 	struct fimc_is *is = fimc_isp_to_is(isp);
678238c84f7SMauro Carvalho Chehab 
679238c84f7SMauro Carvalho Chehab 	isp->sink_fmt.width = DEFAULT_PREVIEW_STILL_WIDTH +
680238c84f7SMauro Carvalho Chehab 				FIMC_ISP_CAC_MARGIN_WIDTH;
681238c84f7SMauro Carvalho Chehab 	isp->sink_fmt.height = DEFAULT_PREVIEW_STILL_HEIGHT +
682238c84f7SMauro Carvalho Chehab 				FIMC_ISP_CAC_MARGIN_HEIGHT;
683238c84f7SMauro Carvalho Chehab 	isp->sink_fmt.code = MEDIA_BUS_FMT_SGRBG10_1X10;
684238c84f7SMauro Carvalho Chehab 
685238c84f7SMauro Carvalho Chehab 	isp->src_fmt.width = DEFAULT_PREVIEW_STILL_WIDTH;
686238c84f7SMauro Carvalho Chehab 	isp->src_fmt.height = DEFAULT_PREVIEW_STILL_HEIGHT;
687238c84f7SMauro Carvalho Chehab 	isp->src_fmt.code = MEDIA_BUS_FMT_SGRBG10_1X10;
688238c84f7SMauro Carvalho Chehab 	__is_set_frame_size(is, &isp->src_fmt);
689238c84f7SMauro Carvalho Chehab }
690238c84f7SMauro Carvalho Chehab 
fimc_isp_subdev_create(struct fimc_isp * isp)691238c84f7SMauro Carvalho Chehab int fimc_isp_subdev_create(struct fimc_isp *isp)
692238c84f7SMauro Carvalho Chehab {
693238c84f7SMauro Carvalho Chehab 	const struct v4l2_ctrl_ops *ops = &fimc_isp_ctrl_ops;
694238c84f7SMauro Carvalho Chehab 	struct v4l2_ctrl_handler *handler = &isp->ctrls.handler;
695238c84f7SMauro Carvalho Chehab 	struct v4l2_subdev *sd = &isp->subdev;
696238c84f7SMauro Carvalho Chehab 	struct fimc_isp_ctrls *ctrls = &isp->ctrls;
697238c84f7SMauro Carvalho Chehab 	int ret;
698238c84f7SMauro Carvalho Chehab 
699238c84f7SMauro Carvalho Chehab 	mutex_init(&isp->subdev_lock);
700238c84f7SMauro Carvalho Chehab 
701238c84f7SMauro Carvalho Chehab 	v4l2_subdev_init(sd, &fimc_is_subdev_ops);
702238c84f7SMauro Carvalho Chehab 
703238c84f7SMauro Carvalho Chehab 	sd->owner = THIS_MODULE;
704238c84f7SMauro Carvalho Chehab 	sd->grp_id = GRP_ID_FIMC_IS;
705238c84f7SMauro Carvalho Chehab 	sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
706238c84f7SMauro Carvalho Chehab 	snprintf(sd->name, sizeof(sd->name), "FIMC-IS-ISP");
707238c84f7SMauro Carvalho Chehab 
708238c84f7SMauro Carvalho Chehab 	sd->entity.function = MEDIA_ENT_F_PROC_VIDEO_PIXEL_FORMATTER;
709238c84f7SMauro Carvalho Chehab 	isp->subdev_pads[FIMC_ISP_SD_PAD_SINK].flags = MEDIA_PAD_FL_SINK;
710238c84f7SMauro Carvalho Chehab 	isp->subdev_pads[FIMC_ISP_SD_PAD_SRC_FIFO].flags = MEDIA_PAD_FL_SOURCE;
711238c84f7SMauro Carvalho Chehab 	isp->subdev_pads[FIMC_ISP_SD_PAD_SRC_DMA].flags = MEDIA_PAD_FL_SOURCE;
712238c84f7SMauro Carvalho Chehab 	ret = media_entity_pads_init(&sd->entity, FIMC_ISP_SD_PADS_NUM,
713238c84f7SMauro Carvalho Chehab 				isp->subdev_pads);
714238c84f7SMauro Carvalho Chehab 	if (ret)
715238c84f7SMauro Carvalho Chehab 		return ret;
716238c84f7SMauro Carvalho Chehab 
717238c84f7SMauro Carvalho Chehab 	v4l2_ctrl_handler_init(handler, 20);
718238c84f7SMauro Carvalho Chehab 
719238c84f7SMauro Carvalho Chehab 	ctrls->saturation = v4l2_ctrl_new_std(handler, ops, V4L2_CID_SATURATION,
720238c84f7SMauro Carvalho Chehab 						-2, 2, 1, 0);
721238c84f7SMauro Carvalho Chehab 	ctrls->brightness = v4l2_ctrl_new_std(handler, ops, V4L2_CID_BRIGHTNESS,
722238c84f7SMauro Carvalho Chehab 						-4, 4, 1, 0);
723238c84f7SMauro Carvalho Chehab 	ctrls->contrast = v4l2_ctrl_new_std(handler, ops, V4L2_CID_CONTRAST,
724238c84f7SMauro Carvalho Chehab 						-2, 2, 1, 0);
725238c84f7SMauro Carvalho Chehab 	ctrls->sharpness = v4l2_ctrl_new_std(handler, ops, V4L2_CID_SHARPNESS,
726238c84f7SMauro Carvalho Chehab 						-2, 2, 1, 0);
727238c84f7SMauro Carvalho Chehab 	ctrls->hue = v4l2_ctrl_new_std(handler, ops, V4L2_CID_HUE,
728238c84f7SMauro Carvalho Chehab 						-2, 2, 1, 0);
729238c84f7SMauro Carvalho Chehab 
730238c84f7SMauro Carvalho Chehab 	ctrls->auto_wb = v4l2_ctrl_new_std_menu(handler, ops,
731238c84f7SMauro Carvalho Chehab 					V4L2_CID_AUTO_N_PRESET_WHITE_BALANCE,
732238c84f7SMauro Carvalho Chehab 					8, ~0x14e, V4L2_WHITE_BALANCE_AUTO);
733238c84f7SMauro Carvalho Chehab 
734238c84f7SMauro Carvalho Chehab 	ctrls->exposure = v4l2_ctrl_new_std(handler, ops,
735238c84f7SMauro Carvalho Chehab 					V4L2_CID_EXPOSURE_ABSOLUTE,
736238c84f7SMauro Carvalho Chehab 					-4, 4, 1, 0);
737238c84f7SMauro Carvalho Chehab 
738238c84f7SMauro Carvalho Chehab 	ctrls->exp_metering = v4l2_ctrl_new_std_menu(handler, ops,
739238c84f7SMauro Carvalho Chehab 					V4L2_CID_EXPOSURE_METERING, 3,
740238c84f7SMauro Carvalho Chehab 					~0xf, V4L2_EXPOSURE_METERING_AVERAGE);
741238c84f7SMauro Carvalho Chehab 
742238c84f7SMauro Carvalho Chehab 	v4l2_ctrl_new_std_menu(handler, ops, V4L2_CID_POWER_LINE_FREQUENCY,
743238c84f7SMauro Carvalho Chehab 					V4L2_CID_POWER_LINE_FREQUENCY_AUTO, 0,
744238c84f7SMauro Carvalho Chehab 					V4L2_CID_POWER_LINE_FREQUENCY_AUTO);
745238c84f7SMauro Carvalho Chehab 	/* ISO sensitivity */
746238c84f7SMauro Carvalho Chehab 	ctrls->auto_iso = v4l2_ctrl_new_std_menu(handler, ops,
747238c84f7SMauro Carvalho Chehab 			V4L2_CID_ISO_SENSITIVITY_AUTO, 1, 0,
748238c84f7SMauro Carvalho Chehab 			V4L2_ISO_SENSITIVITY_AUTO);
749238c84f7SMauro Carvalho Chehab 
750238c84f7SMauro Carvalho Chehab 	ctrls->iso = v4l2_ctrl_new_int_menu(handler, ops,
751238c84f7SMauro Carvalho Chehab 			V4L2_CID_ISO_SENSITIVITY, ARRAY_SIZE(iso_qmenu) - 1,
752238c84f7SMauro Carvalho Chehab 			ARRAY_SIZE(iso_qmenu)/2 - 1, iso_qmenu);
753238c84f7SMauro Carvalho Chehab 
754238c84f7SMauro Carvalho Chehab 	ctrls->aewb_lock = v4l2_ctrl_new_std(handler, ops,
755238c84f7SMauro Carvalho Chehab 					V4L2_CID_3A_LOCK, 0, 0x3, 0, 0);
756238c84f7SMauro Carvalho Chehab 
757238c84f7SMauro Carvalho Chehab 	/* TODO: Add support for NEGATIVE_COLOR option */
758238c84f7SMauro Carvalho Chehab 	ctrls->colorfx = v4l2_ctrl_new_std_menu(handler, ops, V4L2_CID_COLORFX,
759238c84f7SMauro Carvalho Chehab 			V4L2_COLORFX_SET_CBCR + 1, ~0x1000f, V4L2_COLORFX_NONE);
760238c84f7SMauro Carvalho Chehab 
761238c84f7SMauro Carvalho Chehab 	if (handler->error) {
762238c84f7SMauro Carvalho Chehab 		media_entity_cleanup(&sd->entity);
763238c84f7SMauro Carvalho Chehab 		return handler->error;
764238c84f7SMauro Carvalho Chehab 	}
765238c84f7SMauro Carvalho Chehab 
766238c84f7SMauro Carvalho Chehab 	v4l2_ctrl_auto_cluster(2, &ctrls->auto_iso,
767238c84f7SMauro Carvalho Chehab 			V4L2_ISO_SENSITIVITY_MANUAL, false);
768238c84f7SMauro Carvalho Chehab 
769238c84f7SMauro Carvalho Chehab 	sd->ctrl_handler = handler;
770238c84f7SMauro Carvalho Chehab 	sd->internal_ops = &fimc_is_subdev_internal_ops;
771238c84f7SMauro Carvalho Chehab 	sd->entity.ops = &fimc_is_subdev_media_ops;
772238c84f7SMauro Carvalho Chehab 	v4l2_set_subdevdata(sd, isp);
773238c84f7SMauro Carvalho Chehab 
774238c84f7SMauro Carvalho Chehab 	__isp_subdev_set_default_format(isp);
775238c84f7SMauro Carvalho Chehab 
776238c84f7SMauro Carvalho Chehab 	return 0;
777238c84f7SMauro Carvalho Chehab }
778238c84f7SMauro Carvalho Chehab 
fimc_isp_subdev_destroy(struct fimc_isp * isp)779238c84f7SMauro Carvalho Chehab void fimc_isp_subdev_destroy(struct fimc_isp *isp)
780238c84f7SMauro Carvalho Chehab {
781238c84f7SMauro Carvalho Chehab 	struct v4l2_subdev *sd = &isp->subdev;
782238c84f7SMauro Carvalho Chehab 
783238c84f7SMauro Carvalho Chehab 	v4l2_device_unregister_subdev(sd);
784238c84f7SMauro Carvalho Chehab 	media_entity_cleanup(&sd->entity);
785238c84f7SMauro Carvalho Chehab 	v4l2_ctrl_handler_free(&isp->ctrls.handler);
786238c84f7SMauro Carvalho Chehab 	v4l2_set_subdevdata(sd, NULL);
787238c84f7SMauro Carvalho Chehab }
788