1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 2013-2015 Ideas on Board
6 * Copyright (C) 2013-2015 Xilinx, Inc.
8 * Contacts: Hyun Kwon <hyun.kwon@xilinx.com>
9 * Laurent Pinchart <laurent.pinchart@ideasonboard.com>
12 #include <linux/clk.h>
13 #include <linux/export.h>
14 #include <linux/kernel.h>
16 #include <linux/platform_device.h>
18 #include <dt-bindings/media/xilinx-vip.h>
20 #include "xilinx-vip.h"
22 /* -----------------------------------------------------------------------------
26 static const struct xvip_video_format xvip_video_formats[] = {
27 { XVIP_VF_YUV_422, 8, NULL, MEDIA_BUS_FMT_UYVY8_1X16,
28 2, V4L2_PIX_FMT_YUYV },
29 { XVIP_VF_YUV_444, 8, NULL, MEDIA_BUS_FMT_VUY8_1X24,
30 3, V4L2_PIX_FMT_YUV444 },
31 { XVIP_VF_RBG, 8, NULL, MEDIA_BUS_FMT_RBG888_1X24,
33 { XVIP_VF_MONO_SENSOR, 8, "mono", MEDIA_BUS_FMT_Y8_1X8,
34 1, V4L2_PIX_FMT_GREY },
35 { XVIP_VF_MONO_SENSOR, 8, "rggb", MEDIA_BUS_FMT_SRGGB8_1X8,
36 1, V4L2_PIX_FMT_SRGGB8 },
37 { XVIP_VF_MONO_SENSOR, 8, "grbg", MEDIA_BUS_FMT_SGRBG8_1X8,
38 1, V4L2_PIX_FMT_SGRBG8 },
39 { XVIP_VF_MONO_SENSOR, 8, "gbrg", MEDIA_BUS_FMT_SGBRG8_1X8,
40 1, V4L2_PIX_FMT_SGBRG8 },
41 { XVIP_VF_MONO_SENSOR, 8, "bggr", MEDIA_BUS_FMT_SBGGR8_1X8,
42 1, V4L2_PIX_FMT_SBGGR8 },
46 * xvip_get_format_by_code - Retrieve format information for a media bus code
47 * @code: the format media bus code
49 * Return: a pointer to the format information structure corresponding to the
50 * given V4L2 media bus format @code, or ERR_PTR if no corresponding format can
53 const struct xvip_video_format *xvip_get_format_by_code(unsigned int code)
57 for (i = 0; i < ARRAY_SIZE(xvip_video_formats); ++i) {
58 const struct xvip_video_format *format = &xvip_video_formats[i];
60 if (format->code == code)
64 return ERR_PTR(-EINVAL);
66 EXPORT_SYMBOL_GPL(xvip_get_format_by_code);
69 * xvip_get_format_by_fourcc - Retrieve format information for a 4CC
70 * @fourcc: the format 4CC
72 * Return: a pointer to the format information structure corresponding to the
73 * given V4L2 format @fourcc. If not found, return a pointer to the first
74 * available format (V4L2_PIX_FMT_YUYV).
76 const struct xvip_video_format *xvip_get_format_by_fourcc(u32 fourcc)
80 for (i = 0; i < ARRAY_SIZE(xvip_video_formats); ++i) {
81 const struct xvip_video_format *format = &xvip_video_formats[i];
83 if (format->fourcc == fourcc)
87 return &xvip_video_formats[0];
89 EXPORT_SYMBOL_GPL(xvip_get_format_by_fourcc);
92 * xvip_of_get_format - Parse a device tree node and return format information
93 * @node: the device tree node
95 * Read the xlnx,video-format, xlnx,video-width and xlnx,cfa-pattern properties
96 * from the device tree @node passed as an argument and return the corresponding
99 * Return: a pointer to the format information structure corresponding to the
100 * format name and width, or ERR_PTR if no corresponding format can be found.
102 const struct xvip_video_format *xvip_of_get_format(struct device_node *node)
104 const char *pattern = "mono";
105 unsigned int vf_code;
110 ret = of_property_read_u32(node, "xlnx,video-format", &vf_code);
114 ret = of_property_read_u32(node, "xlnx,video-width", &width);
118 if (vf_code == XVIP_VF_MONO_SENSOR)
119 of_property_read_string(node, "xlnx,cfa-pattern", &pattern);
121 for (i = 0; i < ARRAY_SIZE(xvip_video_formats); ++i) {
122 const struct xvip_video_format *format = &xvip_video_formats[i];
124 if (format->vf_code != vf_code || format->width != width)
127 if (vf_code == XVIP_VF_MONO_SENSOR &&
128 strcmp(pattern, format->pattern))
134 return ERR_PTR(-EINVAL);
136 EXPORT_SYMBOL_GPL(xvip_of_get_format);
139 * xvip_set_format_size - Set the media bus frame format size
140 * @format: V4L2 frame format on media bus
141 * @fmt: media bus format
143 * Set the media bus frame format size. The width / height from the subdevice
144 * format are set to the given media bus format. The new format size is stored
145 * in @format. The width and height are clamped using default min / max values.
147 void xvip_set_format_size(struct v4l2_mbus_framefmt *format,
148 const struct v4l2_subdev_format *fmt)
150 format->width = clamp_t(unsigned int, fmt->format.width,
151 XVIP_MIN_WIDTH, XVIP_MAX_WIDTH);
152 format->height = clamp_t(unsigned int, fmt->format.height,
153 XVIP_MIN_HEIGHT, XVIP_MAX_HEIGHT);
155 EXPORT_SYMBOL_GPL(xvip_set_format_size);
158 * xvip_clr_or_set - Clear or set the register with a bitmask
159 * @xvip: Xilinx Video IP device
160 * @addr: address of register
161 * @mask: bitmask to be set or cleared
162 * @set: boolean flag indicating whether to set or clear
164 * Clear or set the register at address @addr with a bitmask @mask depending on
165 * the boolean flag @set. When the flag @set is true, the bitmask is set in
166 * the register, otherwise the bitmask is cleared from the register
167 * when the flag @set is false.
169 * Fox example, this function can be used to set a control with a boolean value
170 * requested by users. If the caller knows whether to set or clear in the first
171 * place, the caller should call xvip_clr() or xvip_set() directly instead of
172 * using this function.
174 void xvip_clr_or_set(struct xvip_device *xvip, u32 addr, u32 mask, bool set)
178 reg = xvip_read(xvip, addr);
179 reg = set ? reg | mask : reg & ~mask;
180 xvip_write(xvip, addr, reg);
182 EXPORT_SYMBOL_GPL(xvip_clr_or_set);
185 * xvip_clr_and_set - Clear and set the register with a bitmask
186 * @xvip: Xilinx Video IP device
187 * @addr: address of register
188 * @clr: bitmask to be cleared
189 * @set: bitmask to be set
191 * Clear a bit(s) of mask @clr in the register at address @addr, then set
192 * a bit(s) of mask @set in the register after.
194 void xvip_clr_and_set(struct xvip_device *xvip, u32 addr, u32 clr, u32 set)
198 reg = xvip_read(xvip, addr);
201 xvip_write(xvip, addr, reg);
203 EXPORT_SYMBOL_GPL(xvip_clr_and_set);
205 int xvip_init_resources(struct xvip_device *xvip)
207 struct platform_device *pdev = to_platform_device(xvip->dev);
209 xvip->iomem = devm_platform_ioremap_resource(pdev, 0);
210 if (IS_ERR(xvip->iomem))
211 return PTR_ERR(xvip->iomem);
213 xvip->clk = devm_clk_get(xvip->dev, NULL);
214 if (IS_ERR(xvip->clk))
215 return PTR_ERR(xvip->clk);
217 clk_prepare_enable(xvip->clk);
220 EXPORT_SYMBOL_GPL(xvip_init_resources);
222 void xvip_cleanup_resources(struct xvip_device *xvip)
224 clk_disable_unprepare(xvip->clk);
226 EXPORT_SYMBOL_GPL(xvip_cleanup_resources);
228 /* -----------------------------------------------------------------------------
229 * Subdev operations handlers
233 * xvip_enum_mbus_code - Enumerate the media format code
234 * @subdev: V4L2 subdevice
235 * @sd_state: V4L2 subdev state
236 * @code: returning media bus code
238 * Enumerate the media bus code of the subdevice. Return the corresponding
239 * pad format code. This function only works for subdevices with fixed format
240 * on all pads. Subdevices with multiple format should have their own
241 * function to enumerate mbus codes.
243 * Return: 0 if the media bus code is found, or -EINVAL if the format index
246 int xvip_enum_mbus_code(struct v4l2_subdev *subdev,
247 struct v4l2_subdev_state *sd_state,
248 struct v4l2_subdev_mbus_code_enum *code)
250 struct v4l2_mbus_framefmt *format;
252 /* Enumerating frame sizes based on the active configuration isn't
255 if (code->which == V4L2_SUBDEV_FORMAT_ACTIVE)
261 format = v4l2_subdev_get_try_format(subdev, sd_state, code->pad);
263 code->code = format->code;
267 EXPORT_SYMBOL_GPL(xvip_enum_mbus_code);
270 * xvip_enum_frame_size - Enumerate the media bus frame size
271 * @subdev: V4L2 subdevice
272 * @sd_state: V4L2 subdev state
273 * @fse: returning media bus frame size
275 * This function is a drop-in implementation of the subdev enum_frame_size pad
276 * operation. It assumes that the subdevice has one sink pad and one source
277 * pad, and that the format on the source pad is always identical to the
278 * format on the sink pad. Entities with different requirements need to
279 * implement their own enum_frame_size handlers.
281 * Return: 0 if the media bus frame size is found, or -EINVAL
282 * if the index or the code is not valid.
284 int xvip_enum_frame_size(struct v4l2_subdev *subdev,
285 struct v4l2_subdev_state *sd_state,
286 struct v4l2_subdev_frame_size_enum *fse)
288 struct v4l2_mbus_framefmt *format;
290 /* Enumerating frame sizes based on the active configuration isn't
293 if (fse->which == V4L2_SUBDEV_FORMAT_ACTIVE)
296 format = v4l2_subdev_get_try_format(subdev, sd_state, fse->pad);
298 if (fse->index || fse->code != format->code)
301 if (fse->pad == XVIP_PAD_SINK) {
302 fse->min_width = XVIP_MIN_WIDTH;
303 fse->max_width = XVIP_MAX_WIDTH;
304 fse->min_height = XVIP_MIN_HEIGHT;
305 fse->max_height = XVIP_MAX_HEIGHT;
307 /* The size on the source pad is fixed and always identical to
308 * the size on the sink pad.
310 fse->min_width = format->width;
311 fse->max_width = format->width;
312 fse->min_height = format->height;
313 fse->max_height = format->height;
318 EXPORT_SYMBOL_GPL(xvip_enum_frame_size);