xref: /OK3568_Linux_fs/kernel/drivers/media/platform/xilinx/xilinx-vip.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Xilinx Video IP Core
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (C) 2013-2015 Ideas on Board
6*4882a593Smuzhiyun  * Copyright (C) 2013-2015 Xilinx, Inc.
7*4882a593Smuzhiyun  *
8*4882a593Smuzhiyun  * Contacts: Hyun Kwon <hyun.kwon@xilinx.com>
9*4882a593Smuzhiyun  *           Laurent Pinchart <laurent.pinchart@ideasonboard.com>
10*4882a593Smuzhiyun  */
11*4882a593Smuzhiyun 
12*4882a593Smuzhiyun #include <linux/clk.h>
13*4882a593Smuzhiyun #include <linux/export.h>
14*4882a593Smuzhiyun #include <linux/kernel.h>
15*4882a593Smuzhiyun #include <linux/of.h>
16*4882a593Smuzhiyun #include <linux/platform_device.h>
17*4882a593Smuzhiyun 
18*4882a593Smuzhiyun #include <dt-bindings/media/xilinx-vip.h>
19*4882a593Smuzhiyun 
20*4882a593Smuzhiyun #include "xilinx-vip.h"
21*4882a593Smuzhiyun 
22*4882a593Smuzhiyun /* -----------------------------------------------------------------------------
23*4882a593Smuzhiyun  * Helper functions
24*4882a593Smuzhiyun  */
25*4882a593Smuzhiyun 
26*4882a593Smuzhiyun static const struct xvip_video_format xvip_video_formats[] = {
27*4882a593Smuzhiyun 	{ XVIP_VF_YUV_422, 8, NULL, MEDIA_BUS_FMT_UYVY8_1X16,
28*4882a593Smuzhiyun 	  2, V4L2_PIX_FMT_YUYV },
29*4882a593Smuzhiyun 	{ XVIP_VF_YUV_444, 8, NULL, MEDIA_BUS_FMT_VUY8_1X24,
30*4882a593Smuzhiyun 	  3, V4L2_PIX_FMT_YUV444 },
31*4882a593Smuzhiyun 	{ XVIP_VF_RBG, 8, NULL, MEDIA_BUS_FMT_RBG888_1X24,
32*4882a593Smuzhiyun 	  3, 0 },
33*4882a593Smuzhiyun 	{ XVIP_VF_MONO_SENSOR, 8, "mono", MEDIA_BUS_FMT_Y8_1X8,
34*4882a593Smuzhiyun 	  1, V4L2_PIX_FMT_GREY },
35*4882a593Smuzhiyun 	{ XVIP_VF_MONO_SENSOR, 8, "rggb", MEDIA_BUS_FMT_SRGGB8_1X8,
36*4882a593Smuzhiyun 	  1, V4L2_PIX_FMT_SRGGB8 },
37*4882a593Smuzhiyun 	{ XVIP_VF_MONO_SENSOR, 8, "grbg", MEDIA_BUS_FMT_SGRBG8_1X8,
38*4882a593Smuzhiyun 	  1, V4L2_PIX_FMT_SGRBG8 },
39*4882a593Smuzhiyun 	{ XVIP_VF_MONO_SENSOR, 8, "gbrg", MEDIA_BUS_FMT_SGBRG8_1X8,
40*4882a593Smuzhiyun 	  1, V4L2_PIX_FMT_SGBRG8 },
41*4882a593Smuzhiyun 	{ XVIP_VF_MONO_SENSOR, 8, "bggr", MEDIA_BUS_FMT_SBGGR8_1X8,
42*4882a593Smuzhiyun 	  1, V4L2_PIX_FMT_SBGGR8 },
43*4882a593Smuzhiyun };
44*4882a593Smuzhiyun 
45*4882a593Smuzhiyun /**
46*4882a593Smuzhiyun  * xvip_get_format_by_code - Retrieve format information for a media bus code
47*4882a593Smuzhiyun  * @code: the format media bus code
48*4882a593Smuzhiyun  *
49*4882a593Smuzhiyun  * Return: a pointer to the format information structure corresponding to the
50*4882a593Smuzhiyun  * given V4L2 media bus format @code, or ERR_PTR if no corresponding format can
51*4882a593Smuzhiyun  * be found.
52*4882a593Smuzhiyun  */
xvip_get_format_by_code(unsigned int code)53*4882a593Smuzhiyun const struct xvip_video_format *xvip_get_format_by_code(unsigned int code)
54*4882a593Smuzhiyun {
55*4882a593Smuzhiyun 	unsigned int i;
56*4882a593Smuzhiyun 
57*4882a593Smuzhiyun 	for (i = 0; i < ARRAY_SIZE(xvip_video_formats); ++i) {
58*4882a593Smuzhiyun 		const struct xvip_video_format *format = &xvip_video_formats[i];
59*4882a593Smuzhiyun 
60*4882a593Smuzhiyun 		if (format->code == code)
61*4882a593Smuzhiyun 			return format;
62*4882a593Smuzhiyun 	}
63*4882a593Smuzhiyun 
64*4882a593Smuzhiyun 	return ERR_PTR(-EINVAL);
65*4882a593Smuzhiyun }
66*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(xvip_get_format_by_code);
67*4882a593Smuzhiyun 
68*4882a593Smuzhiyun /**
69*4882a593Smuzhiyun  * xvip_get_format_by_fourcc - Retrieve format information for a 4CC
70*4882a593Smuzhiyun  * @fourcc: the format 4CC
71*4882a593Smuzhiyun  *
72*4882a593Smuzhiyun  * Return: a pointer to the format information structure corresponding to the
73*4882a593Smuzhiyun  * given V4L2 format @fourcc, or ERR_PTR if no corresponding format can be
74*4882a593Smuzhiyun  * found.
75*4882a593Smuzhiyun  */
xvip_get_format_by_fourcc(u32 fourcc)76*4882a593Smuzhiyun const struct xvip_video_format *xvip_get_format_by_fourcc(u32 fourcc)
77*4882a593Smuzhiyun {
78*4882a593Smuzhiyun 	unsigned int i;
79*4882a593Smuzhiyun 
80*4882a593Smuzhiyun 	for (i = 0; i < ARRAY_SIZE(xvip_video_formats); ++i) {
81*4882a593Smuzhiyun 		const struct xvip_video_format *format = &xvip_video_formats[i];
82*4882a593Smuzhiyun 
83*4882a593Smuzhiyun 		if (format->fourcc == fourcc)
84*4882a593Smuzhiyun 			return format;
85*4882a593Smuzhiyun 	}
86*4882a593Smuzhiyun 
87*4882a593Smuzhiyun 	return ERR_PTR(-EINVAL);
88*4882a593Smuzhiyun }
89*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(xvip_get_format_by_fourcc);
90*4882a593Smuzhiyun 
91*4882a593Smuzhiyun /**
92*4882a593Smuzhiyun  * xvip_of_get_format - Parse a device tree node and return format information
93*4882a593Smuzhiyun  * @node: the device tree node
94*4882a593Smuzhiyun  *
95*4882a593Smuzhiyun  * Read the xlnx,video-format, xlnx,video-width and xlnx,cfa-pattern properties
96*4882a593Smuzhiyun  * from the device tree @node passed as an argument and return the corresponding
97*4882a593Smuzhiyun  * format information.
98*4882a593Smuzhiyun  *
99*4882a593Smuzhiyun  * Return: a pointer to the format information structure corresponding to the
100*4882a593Smuzhiyun  * format name and width, or ERR_PTR if no corresponding format can be found.
101*4882a593Smuzhiyun  */
xvip_of_get_format(struct device_node * node)102*4882a593Smuzhiyun const struct xvip_video_format *xvip_of_get_format(struct device_node *node)
103*4882a593Smuzhiyun {
104*4882a593Smuzhiyun 	const char *pattern = "mono";
105*4882a593Smuzhiyun 	unsigned int vf_code;
106*4882a593Smuzhiyun 	unsigned int i;
107*4882a593Smuzhiyun 	u32 width;
108*4882a593Smuzhiyun 	int ret;
109*4882a593Smuzhiyun 
110*4882a593Smuzhiyun 	ret = of_property_read_u32(node, "xlnx,video-format", &vf_code);
111*4882a593Smuzhiyun 	if (ret < 0)
112*4882a593Smuzhiyun 		return ERR_PTR(ret);
113*4882a593Smuzhiyun 
114*4882a593Smuzhiyun 	ret = of_property_read_u32(node, "xlnx,video-width", &width);
115*4882a593Smuzhiyun 	if (ret < 0)
116*4882a593Smuzhiyun 		return ERR_PTR(ret);
117*4882a593Smuzhiyun 
118*4882a593Smuzhiyun 	if (vf_code == XVIP_VF_MONO_SENSOR)
119*4882a593Smuzhiyun 		of_property_read_string(node, "xlnx,cfa-pattern", &pattern);
120*4882a593Smuzhiyun 
121*4882a593Smuzhiyun 	for (i = 0; i < ARRAY_SIZE(xvip_video_formats); ++i) {
122*4882a593Smuzhiyun 		const struct xvip_video_format *format = &xvip_video_formats[i];
123*4882a593Smuzhiyun 
124*4882a593Smuzhiyun 		if (format->vf_code != vf_code || format->width != width)
125*4882a593Smuzhiyun 			continue;
126*4882a593Smuzhiyun 
127*4882a593Smuzhiyun 		if (vf_code == XVIP_VF_MONO_SENSOR &&
128*4882a593Smuzhiyun 		    strcmp(pattern, format->pattern))
129*4882a593Smuzhiyun 			continue;
130*4882a593Smuzhiyun 
131*4882a593Smuzhiyun 		return format;
132*4882a593Smuzhiyun 	}
133*4882a593Smuzhiyun 
134*4882a593Smuzhiyun 	return ERR_PTR(-EINVAL);
135*4882a593Smuzhiyun }
136*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(xvip_of_get_format);
137*4882a593Smuzhiyun 
138*4882a593Smuzhiyun /**
139*4882a593Smuzhiyun  * xvip_set_format_size - Set the media bus frame format size
140*4882a593Smuzhiyun  * @format: V4L2 frame format on media bus
141*4882a593Smuzhiyun  * @fmt: media bus format
142*4882a593Smuzhiyun  *
143*4882a593Smuzhiyun  * Set the media bus frame format size. The width / height from the subdevice
144*4882a593Smuzhiyun  * format are set to the given media bus format. The new format size is stored
145*4882a593Smuzhiyun  * in @format. The width and height are clamped using default min / max values.
146*4882a593Smuzhiyun  */
xvip_set_format_size(struct v4l2_mbus_framefmt * format,const struct v4l2_subdev_format * fmt)147*4882a593Smuzhiyun void xvip_set_format_size(struct v4l2_mbus_framefmt *format,
148*4882a593Smuzhiyun 			  const struct v4l2_subdev_format *fmt)
149*4882a593Smuzhiyun {
150*4882a593Smuzhiyun 	format->width = clamp_t(unsigned int, fmt->format.width,
151*4882a593Smuzhiyun 				XVIP_MIN_WIDTH, XVIP_MAX_WIDTH);
152*4882a593Smuzhiyun 	format->height = clamp_t(unsigned int, fmt->format.height,
153*4882a593Smuzhiyun 			 XVIP_MIN_HEIGHT, XVIP_MAX_HEIGHT);
154*4882a593Smuzhiyun }
155*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(xvip_set_format_size);
156*4882a593Smuzhiyun 
157*4882a593Smuzhiyun /**
158*4882a593Smuzhiyun  * xvip_clr_or_set - Clear or set the register with a bitmask
159*4882a593Smuzhiyun  * @xvip: Xilinx Video IP device
160*4882a593Smuzhiyun  * @addr: address of register
161*4882a593Smuzhiyun  * @mask: bitmask to be set or cleared
162*4882a593Smuzhiyun  * @set: boolean flag indicating whether to set or clear
163*4882a593Smuzhiyun  *
164*4882a593Smuzhiyun  * Clear or set the register at address @addr with a bitmask @mask depending on
165*4882a593Smuzhiyun  * the boolean flag @set. When the flag @set is true, the bitmask is set in
166*4882a593Smuzhiyun  * the register, otherwise the bitmask is cleared from the register
167*4882a593Smuzhiyun  * when the flag @set is false.
168*4882a593Smuzhiyun  *
169*4882a593Smuzhiyun  * Fox example, this function can be used to set a control with a boolean value
170*4882a593Smuzhiyun  * requested by users. If the caller knows whether to set or clear in the first
171*4882a593Smuzhiyun  * place, the caller should call xvip_clr() or xvip_set() directly instead of
172*4882a593Smuzhiyun  * using this function.
173*4882a593Smuzhiyun  */
xvip_clr_or_set(struct xvip_device * xvip,u32 addr,u32 mask,bool set)174*4882a593Smuzhiyun void xvip_clr_or_set(struct xvip_device *xvip, u32 addr, u32 mask, bool set)
175*4882a593Smuzhiyun {
176*4882a593Smuzhiyun 	u32 reg;
177*4882a593Smuzhiyun 
178*4882a593Smuzhiyun 	reg = xvip_read(xvip, addr);
179*4882a593Smuzhiyun 	reg = set ? reg | mask : reg & ~mask;
180*4882a593Smuzhiyun 	xvip_write(xvip, addr, reg);
181*4882a593Smuzhiyun }
182*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(xvip_clr_or_set);
183*4882a593Smuzhiyun 
184*4882a593Smuzhiyun /**
185*4882a593Smuzhiyun  * xvip_clr_and_set - Clear and set the register with a bitmask
186*4882a593Smuzhiyun  * @xvip: Xilinx Video IP device
187*4882a593Smuzhiyun  * @addr: address of register
188*4882a593Smuzhiyun  * @clr: bitmask to be cleared
189*4882a593Smuzhiyun  * @set: bitmask to be set
190*4882a593Smuzhiyun  *
191*4882a593Smuzhiyun  * Clear a bit(s) of mask @clr in the register at address @addr, then set
192*4882a593Smuzhiyun  * a bit(s) of mask @set in the register after.
193*4882a593Smuzhiyun  */
xvip_clr_and_set(struct xvip_device * xvip,u32 addr,u32 clr,u32 set)194*4882a593Smuzhiyun void xvip_clr_and_set(struct xvip_device *xvip, u32 addr, u32 clr, u32 set)
195*4882a593Smuzhiyun {
196*4882a593Smuzhiyun 	u32 reg;
197*4882a593Smuzhiyun 
198*4882a593Smuzhiyun 	reg = xvip_read(xvip, addr);
199*4882a593Smuzhiyun 	reg &= ~clr;
200*4882a593Smuzhiyun 	reg |= set;
201*4882a593Smuzhiyun 	xvip_write(xvip, addr, reg);
202*4882a593Smuzhiyun }
203*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(xvip_clr_and_set);
204*4882a593Smuzhiyun 
xvip_init_resources(struct xvip_device * xvip)205*4882a593Smuzhiyun int xvip_init_resources(struct xvip_device *xvip)
206*4882a593Smuzhiyun {
207*4882a593Smuzhiyun 	struct platform_device *pdev = to_platform_device(xvip->dev);
208*4882a593Smuzhiyun 	struct resource *res;
209*4882a593Smuzhiyun 
210*4882a593Smuzhiyun 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
211*4882a593Smuzhiyun 	xvip->iomem = devm_ioremap_resource(xvip->dev, res);
212*4882a593Smuzhiyun 	if (IS_ERR(xvip->iomem))
213*4882a593Smuzhiyun 		return PTR_ERR(xvip->iomem);
214*4882a593Smuzhiyun 
215*4882a593Smuzhiyun 	xvip->clk = devm_clk_get(xvip->dev, NULL);
216*4882a593Smuzhiyun 	if (IS_ERR(xvip->clk))
217*4882a593Smuzhiyun 		return PTR_ERR(xvip->clk);
218*4882a593Smuzhiyun 
219*4882a593Smuzhiyun 	clk_prepare_enable(xvip->clk);
220*4882a593Smuzhiyun 	return 0;
221*4882a593Smuzhiyun }
222*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(xvip_init_resources);
223*4882a593Smuzhiyun 
xvip_cleanup_resources(struct xvip_device * xvip)224*4882a593Smuzhiyun void xvip_cleanup_resources(struct xvip_device *xvip)
225*4882a593Smuzhiyun {
226*4882a593Smuzhiyun 	clk_disable_unprepare(xvip->clk);
227*4882a593Smuzhiyun }
228*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(xvip_cleanup_resources);
229*4882a593Smuzhiyun 
230*4882a593Smuzhiyun /* -----------------------------------------------------------------------------
231*4882a593Smuzhiyun  * Subdev operations handlers
232*4882a593Smuzhiyun  */
233*4882a593Smuzhiyun 
234*4882a593Smuzhiyun /**
235*4882a593Smuzhiyun  * xvip_enum_mbus_code - Enumerate the media format code
236*4882a593Smuzhiyun  * @subdev: V4L2 subdevice
237*4882a593Smuzhiyun  * @cfg: V4L2 subdev pad configuration
238*4882a593Smuzhiyun  * @code: returning media bus code
239*4882a593Smuzhiyun  *
240*4882a593Smuzhiyun  * Enumerate the media bus code of the subdevice. Return the corresponding
241*4882a593Smuzhiyun  * pad format code. This function only works for subdevices with fixed format
242*4882a593Smuzhiyun  * on all pads. Subdevices with multiple format should have their own
243*4882a593Smuzhiyun  * function to enumerate mbus codes.
244*4882a593Smuzhiyun  *
245*4882a593Smuzhiyun  * Return: 0 if the media bus code is found, or -EINVAL if the format index
246*4882a593Smuzhiyun  * is not valid.
247*4882a593Smuzhiyun  */
xvip_enum_mbus_code(struct v4l2_subdev * subdev,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_mbus_code_enum * code)248*4882a593Smuzhiyun int xvip_enum_mbus_code(struct v4l2_subdev *subdev,
249*4882a593Smuzhiyun 			struct v4l2_subdev_pad_config *cfg,
250*4882a593Smuzhiyun 			struct v4l2_subdev_mbus_code_enum *code)
251*4882a593Smuzhiyun {
252*4882a593Smuzhiyun 	struct v4l2_mbus_framefmt *format;
253*4882a593Smuzhiyun 
254*4882a593Smuzhiyun 	/* Enumerating frame sizes based on the active configuration isn't
255*4882a593Smuzhiyun 	 * supported yet.
256*4882a593Smuzhiyun 	 */
257*4882a593Smuzhiyun 	if (code->which == V4L2_SUBDEV_FORMAT_ACTIVE)
258*4882a593Smuzhiyun 		return -EINVAL;
259*4882a593Smuzhiyun 
260*4882a593Smuzhiyun 	if (code->index)
261*4882a593Smuzhiyun 		return -EINVAL;
262*4882a593Smuzhiyun 
263*4882a593Smuzhiyun 	format = v4l2_subdev_get_try_format(subdev, cfg, code->pad);
264*4882a593Smuzhiyun 
265*4882a593Smuzhiyun 	code->code = format->code;
266*4882a593Smuzhiyun 
267*4882a593Smuzhiyun 	return 0;
268*4882a593Smuzhiyun }
269*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(xvip_enum_mbus_code);
270*4882a593Smuzhiyun 
271*4882a593Smuzhiyun /**
272*4882a593Smuzhiyun  * xvip_enum_frame_size - Enumerate the media bus frame size
273*4882a593Smuzhiyun  * @subdev: V4L2 subdevice
274*4882a593Smuzhiyun  * @cfg: V4L2 subdev pad configuration
275*4882a593Smuzhiyun  * @fse: returning media bus frame size
276*4882a593Smuzhiyun  *
277*4882a593Smuzhiyun  * This function is a drop-in implementation of the subdev enum_frame_size pad
278*4882a593Smuzhiyun  * operation. It assumes that the subdevice has one sink pad and one source
279*4882a593Smuzhiyun  * pad, and that the format on the source pad is always identical to the
280*4882a593Smuzhiyun  * format on the sink pad. Entities with different requirements need to
281*4882a593Smuzhiyun  * implement their own enum_frame_size handlers.
282*4882a593Smuzhiyun  *
283*4882a593Smuzhiyun  * Return: 0 if the media bus frame size is found, or -EINVAL
284*4882a593Smuzhiyun  * if the index or the code is not valid.
285*4882a593Smuzhiyun  */
xvip_enum_frame_size(struct v4l2_subdev * subdev,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_frame_size_enum * fse)286*4882a593Smuzhiyun int xvip_enum_frame_size(struct v4l2_subdev *subdev,
287*4882a593Smuzhiyun 			 struct v4l2_subdev_pad_config *cfg,
288*4882a593Smuzhiyun 			 struct v4l2_subdev_frame_size_enum *fse)
289*4882a593Smuzhiyun {
290*4882a593Smuzhiyun 	struct v4l2_mbus_framefmt *format;
291*4882a593Smuzhiyun 
292*4882a593Smuzhiyun 	/* Enumerating frame sizes based on the active configuration isn't
293*4882a593Smuzhiyun 	 * supported yet.
294*4882a593Smuzhiyun 	 */
295*4882a593Smuzhiyun 	if (fse->which == V4L2_SUBDEV_FORMAT_ACTIVE)
296*4882a593Smuzhiyun 		return -EINVAL;
297*4882a593Smuzhiyun 
298*4882a593Smuzhiyun 	format = v4l2_subdev_get_try_format(subdev, cfg, fse->pad);
299*4882a593Smuzhiyun 
300*4882a593Smuzhiyun 	if (fse->index || fse->code != format->code)
301*4882a593Smuzhiyun 		return -EINVAL;
302*4882a593Smuzhiyun 
303*4882a593Smuzhiyun 	if (fse->pad == XVIP_PAD_SINK) {
304*4882a593Smuzhiyun 		fse->min_width = XVIP_MIN_WIDTH;
305*4882a593Smuzhiyun 		fse->max_width = XVIP_MAX_WIDTH;
306*4882a593Smuzhiyun 		fse->min_height = XVIP_MIN_HEIGHT;
307*4882a593Smuzhiyun 		fse->max_height = XVIP_MAX_HEIGHT;
308*4882a593Smuzhiyun 	} else {
309*4882a593Smuzhiyun 		/* The size on the source pad is fixed and always identical to
310*4882a593Smuzhiyun 		 * the size on the sink pad.
311*4882a593Smuzhiyun 		 */
312*4882a593Smuzhiyun 		fse->min_width = format->width;
313*4882a593Smuzhiyun 		fse->max_width = format->width;
314*4882a593Smuzhiyun 		fse->min_height = format->height;
315*4882a593Smuzhiyun 		fse->max_height = format->height;
316*4882a593Smuzhiyun 	}
317*4882a593Smuzhiyun 
318*4882a593Smuzhiyun 	return 0;
319*4882a593Smuzhiyun }
320*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(xvip_enum_frame_size);
321