1
2
3
4
5
6
7
8
9
10
11
12
13
14
15#include <linux/clk.h>
16#include <linux/export.h>
17#include <linux/kernel.h>
18#include <linux/of.h>
19#include <linux/platform_device.h>
20
21#include <dt-bindings/media/xilinx-vip.h>
22
23#include "xilinx-vip.h"
24
25
26
27
28
29static const struct xvip_video_format xvip_video_formats[] = {
30 { XVIP_VF_YUV_422, 8, NULL, MEDIA_BUS_FMT_UYVY8_1X16,
31 2, V4L2_PIX_FMT_YUYV, "4:2:2, packed, YUYV" },
32 { XVIP_VF_YUV_444, 8, NULL, MEDIA_BUS_FMT_VUY8_1X24,
33 3, V4L2_PIX_FMT_YUV444, "4:4:4, packed, YUYV" },
34 { XVIP_VF_RBG, 8, NULL, MEDIA_BUS_FMT_RBG888_1X24,
35 3, 0, NULL },
36 { XVIP_VF_MONO_SENSOR, 8, "mono", MEDIA_BUS_FMT_Y8_1X8,
37 1, V4L2_PIX_FMT_GREY, "Greyscale 8-bit" },
38 { XVIP_VF_MONO_SENSOR, 8, "rggb", MEDIA_BUS_FMT_SRGGB8_1X8,
39 1, V4L2_PIX_FMT_SGRBG8, "Bayer 8-bit RGGB" },
40 { XVIP_VF_MONO_SENSOR, 8, "grbg", MEDIA_BUS_FMT_SGRBG8_1X8,
41 1, V4L2_PIX_FMT_SGRBG8, "Bayer 8-bit GRBG" },
42 { XVIP_VF_MONO_SENSOR, 8, "gbrg", MEDIA_BUS_FMT_SGBRG8_1X8,
43 1, V4L2_PIX_FMT_SGBRG8, "Bayer 8-bit GBRG" },
44 { XVIP_VF_MONO_SENSOR, 8, "bggr", MEDIA_BUS_FMT_SBGGR8_1X8,
45 1, V4L2_PIX_FMT_SBGGR8, "Bayer 8-bit BGGR" },
46};
47
48
49
50
51
52
53
54
55
56const struct xvip_video_format *xvip_get_format_by_code(unsigned int code)
57{
58 unsigned int i;
59
60 for (i = 0; i < ARRAY_SIZE(xvip_video_formats); ++i) {
61 const struct xvip_video_format *format = &xvip_video_formats[i];
62
63 if (format->code == code)
64 return format;
65 }
66
67 return ERR_PTR(-EINVAL);
68}
69EXPORT_SYMBOL_GPL(xvip_get_format_by_code);
70
71
72
73
74
75
76
77
78
79const struct xvip_video_format *xvip_get_format_by_fourcc(u32 fourcc)
80{
81 unsigned int i;
82
83 for (i = 0; i < ARRAY_SIZE(xvip_video_formats); ++i) {
84 const struct xvip_video_format *format = &xvip_video_formats[i];
85
86 if (format->fourcc == fourcc)
87 return format;
88 }
89
90 return ERR_PTR(-EINVAL);
91}
92EXPORT_SYMBOL_GPL(xvip_get_format_by_fourcc);
93
94
95
96
97
98
99
100
101
102
103
104
105const struct xvip_video_format *xvip_of_get_format(struct device_node *node)
106{
107 const char *pattern = "mono";
108 unsigned int vf_code;
109 unsigned int i;
110 u32 width;
111 int ret;
112
113 ret = of_property_read_u32(node, "xlnx,video-format", &vf_code);
114 if (ret < 0)
115 return ERR_PTR(ret);
116
117 ret = of_property_read_u32(node, "xlnx,video-width", &width);
118 if (ret < 0)
119 return ERR_PTR(ret);
120
121 if (vf_code == XVIP_VF_MONO_SENSOR)
122 of_property_read_string(node, "xlnx,cfa-pattern", &pattern);
123
124 for (i = 0; i < ARRAY_SIZE(xvip_video_formats); ++i) {
125 const struct xvip_video_format *format = &xvip_video_formats[i];
126
127 if (format->vf_code != vf_code || format->width != width)
128 continue;
129
130 if (vf_code == XVIP_VF_MONO_SENSOR &&
131 strcmp(pattern, format->pattern))
132 continue;
133
134 return format;
135 }
136
137 return ERR_PTR(-EINVAL);
138}
139EXPORT_SYMBOL_GPL(xvip_of_get_format);
140
141
142
143
144
145
146
147
148
149
150void xvip_set_format_size(struct v4l2_mbus_framefmt *format,
151 const struct v4l2_subdev_format *fmt)
152{
153 format->width = clamp_t(unsigned int, fmt->format.width,
154 XVIP_MIN_WIDTH, XVIP_MAX_WIDTH);
155 format->height = clamp_t(unsigned int, fmt->format.height,
156 XVIP_MIN_HEIGHT, XVIP_MAX_HEIGHT);
157}
158EXPORT_SYMBOL_GPL(xvip_set_format_size);
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177void xvip_clr_or_set(struct xvip_device *xvip, u32 addr, u32 mask, bool set)
178{
179 u32 reg;
180
181 reg = xvip_read(xvip, addr);
182 reg = set ? reg | mask : reg & ~mask;
183 xvip_write(xvip, addr, reg);
184}
185EXPORT_SYMBOL_GPL(xvip_clr_or_set);
186
187
188
189
190
191
192
193
194
195
196
197void xvip_clr_and_set(struct xvip_device *xvip, u32 addr, u32 clr, u32 set)
198{
199 u32 reg;
200
201 reg = xvip_read(xvip, addr);
202 reg &= ~clr;
203 reg |= set;
204 xvip_write(xvip, addr, reg);
205}
206EXPORT_SYMBOL_GPL(xvip_clr_and_set);
207
208int xvip_init_resources(struct xvip_device *xvip)
209{
210 struct platform_device *pdev = to_platform_device(xvip->dev);
211 struct resource *res;
212
213 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
214 xvip->iomem = devm_ioremap_resource(xvip->dev, res);
215 if (IS_ERR(xvip->iomem))
216 return PTR_ERR(xvip->iomem);
217
218 xvip->clk = devm_clk_get(xvip->dev, NULL);
219 if (IS_ERR(xvip->clk))
220 return PTR_ERR(xvip->clk);
221
222 clk_prepare_enable(xvip->clk);
223 return 0;
224}
225EXPORT_SYMBOL_GPL(xvip_init_resources);
226
227void xvip_cleanup_resources(struct xvip_device *xvip)
228{
229 clk_disable_unprepare(xvip->clk);
230}
231EXPORT_SYMBOL_GPL(xvip_cleanup_resources);
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251int xvip_enum_mbus_code(struct v4l2_subdev *subdev,
252 struct v4l2_subdev_pad_config *cfg,
253 struct v4l2_subdev_mbus_code_enum *code)
254{
255 struct v4l2_mbus_framefmt *format;
256
257
258
259
260 if (code->which == V4L2_SUBDEV_FORMAT_ACTIVE)
261 return -EINVAL;
262
263 if (code->index)
264 return -EINVAL;
265
266 format = v4l2_subdev_get_try_format(subdev, cfg, code->pad);
267
268 code->code = format->code;
269
270 return 0;
271}
272EXPORT_SYMBOL_GPL(xvip_enum_mbus_code);
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289int xvip_enum_frame_size(struct v4l2_subdev *subdev,
290 struct v4l2_subdev_pad_config *cfg,
291 struct v4l2_subdev_frame_size_enum *fse)
292{
293 struct v4l2_mbus_framefmt *format;
294
295
296
297
298 if (fse->which == V4L2_SUBDEV_FORMAT_ACTIVE)
299 return -EINVAL;
300
301 format = v4l2_subdev_get_try_format(subdev, cfg, fse->pad);
302
303 if (fse->index || fse->code != format->code)
304 return -EINVAL;
305
306 if (fse->pad == XVIP_PAD_SINK) {
307 fse->min_width = XVIP_MIN_WIDTH;
308 fse->max_width = XVIP_MAX_WIDTH;
309 fse->min_height = XVIP_MIN_HEIGHT;
310 fse->max_height = XVIP_MAX_HEIGHT;
311 } else {
312
313
314
315 fse->min_width = format->width;
316 fse->max_width = format->width;
317 fse->min_height = format->height;
318 fse->max_height = format->height;
319 }
320
321 return 0;
322}
323EXPORT_SYMBOL_GPL(xvip_enum_frame_size);
324