• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * vimc-common.c Virtual Media Controller Driver
4  *
5  * Copyright (C) 2015-2017 Helen Koike <helen.fornazier@gmail.com>
6  */
7 
8 #include <linux/init.h>
9 #include <linux/module.h>
10 
11 #include "vimc-common.h"
12 
13 /*
14  * NOTE: non-bayer formats need to come first (necessary for enum_mbus_code
15  * in the scaler)
16  */
17 static const struct vimc_pix_map vimc_pix_map_list[] = {
18 	/* TODO: add all missing formats */
19 
20 	/* RGB formats */
21 	{
22 		.code = MEDIA_BUS_FMT_BGR888_1X24,
23 		.pixelformat = V4L2_PIX_FMT_BGR24,
24 		.bpp = 3,
25 		.bayer = false,
26 	},
27 	{
28 		.code = MEDIA_BUS_FMT_RGB888_1X24,
29 		.pixelformat = V4L2_PIX_FMT_RGB24,
30 		.bpp = 3,
31 		.bayer = false,
32 	},
33 	{
34 		.code = MEDIA_BUS_FMT_ARGB8888_1X32,
35 		.pixelformat = V4L2_PIX_FMT_ARGB32,
36 		.bpp = 4,
37 		.bayer = false,
38 	},
39 
40 	/* Bayer formats */
41 	{
42 		.code = MEDIA_BUS_FMT_SBGGR8_1X8,
43 		.pixelformat = V4L2_PIX_FMT_SBGGR8,
44 		.bpp = 1,
45 		.bayer = true,
46 	},
47 	{
48 		.code = MEDIA_BUS_FMT_SGBRG8_1X8,
49 		.pixelformat = V4L2_PIX_FMT_SGBRG8,
50 		.bpp = 1,
51 		.bayer = true,
52 	},
53 	{
54 		.code = MEDIA_BUS_FMT_SGRBG8_1X8,
55 		.pixelformat = V4L2_PIX_FMT_SGRBG8,
56 		.bpp = 1,
57 		.bayer = true,
58 	},
59 	{
60 		.code = MEDIA_BUS_FMT_SRGGB8_1X8,
61 		.pixelformat = V4L2_PIX_FMT_SRGGB8,
62 		.bpp = 1,
63 		.bayer = true,
64 	},
65 	{
66 		.code = MEDIA_BUS_FMT_SBGGR10_1X10,
67 		.pixelformat = V4L2_PIX_FMT_SBGGR10,
68 		.bpp = 2,
69 		.bayer = true,
70 	},
71 	{
72 		.code = MEDIA_BUS_FMT_SGBRG10_1X10,
73 		.pixelformat = V4L2_PIX_FMT_SGBRG10,
74 		.bpp = 2,
75 		.bayer = true,
76 	},
77 	{
78 		.code = MEDIA_BUS_FMT_SGRBG10_1X10,
79 		.pixelformat = V4L2_PIX_FMT_SGRBG10,
80 		.bpp = 2,
81 		.bayer = true,
82 	},
83 	{
84 		.code = MEDIA_BUS_FMT_SRGGB10_1X10,
85 		.pixelformat = V4L2_PIX_FMT_SRGGB10,
86 		.bpp = 2,
87 		.bayer = true,
88 	},
89 
90 	/* 10bit raw bayer a-law compressed to 8 bits */
91 	{
92 		.code = MEDIA_BUS_FMT_SBGGR10_ALAW8_1X8,
93 		.pixelformat = V4L2_PIX_FMT_SBGGR10ALAW8,
94 		.bpp = 1,
95 		.bayer = true,
96 	},
97 	{
98 		.code = MEDIA_BUS_FMT_SGBRG10_ALAW8_1X8,
99 		.pixelformat = V4L2_PIX_FMT_SGBRG10ALAW8,
100 		.bpp = 1,
101 		.bayer = true,
102 	},
103 	{
104 		.code = MEDIA_BUS_FMT_SGRBG10_ALAW8_1X8,
105 		.pixelformat = V4L2_PIX_FMT_SGRBG10ALAW8,
106 		.bpp = 1,
107 		.bayer = true,
108 	},
109 	{
110 		.code = MEDIA_BUS_FMT_SRGGB10_ALAW8_1X8,
111 		.pixelformat = V4L2_PIX_FMT_SRGGB10ALAW8,
112 		.bpp = 1,
113 		.bayer = true,
114 	},
115 
116 	/* 10bit raw bayer DPCM compressed to 8 bits */
117 	{
118 		.code = MEDIA_BUS_FMT_SBGGR10_DPCM8_1X8,
119 		.pixelformat = V4L2_PIX_FMT_SBGGR10DPCM8,
120 		.bpp = 1,
121 		.bayer = true,
122 	},
123 	{
124 		.code = MEDIA_BUS_FMT_SGBRG10_DPCM8_1X8,
125 		.pixelformat = V4L2_PIX_FMT_SGBRG10DPCM8,
126 		.bpp = 1,
127 		.bayer = true,
128 	},
129 	{
130 		.code = MEDIA_BUS_FMT_SGRBG10_DPCM8_1X8,
131 		.pixelformat = V4L2_PIX_FMT_SGRBG10DPCM8,
132 		.bpp = 1,
133 		.bayer = true,
134 	},
135 	{
136 		.code = MEDIA_BUS_FMT_SRGGB10_DPCM8_1X8,
137 		.pixelformat = V4L2_PIX_FMT_SRGGB10DPCM8,
138 		.bpp = 1,
139 		.bayer = true,
140 	},
141 	{
142 		.code = MEDIA_BUS_FMT_SBGGR12_1X12,
143 		.pixelformat = V4L2_PIX_FMT_SBGGR12,
144 		.bpp = 2,
145 		.bayer = true,
146 	},
147 	{
148 		.code = MEDIA_BUS_FMT_SGBRG12_1X12,
149 		.pixelformat = V4L2_PIX_FMT_SGBRG12,
150 		.bpp = 2,
151 		.bayer = true,
152 	},
153 	{
154 		.code = MEDIA_BUS_FMT_SGRBG12_1X12,
155 		.pixelformat = V4L2_PIX_FMT_SGRBG12,
156 		.bpp = 2,
157 		.bayer = true,
158 	},
159 	{
160 		.code = MEDIA_BUS_FMT_SRGGB12_1X12,
161 		.pixelformat = V4L2_PIX_FMT_SRGGB12,
162 		.bpp = 2,
163 		.bayer = true,
164 	},
165 };
166 
vimc_pix_map_by_index(unsigned int i)167 const struct vimc_pix_map *vimc_pix_map_by_index(unsigned int i)
168 {
169 	if (i >= ARRAY_SIZE(vimc_pix_map_list))
170 		return NULL;
171 
172 	return &vimc_pix_map_list[i];
173 }
174 EXPORT_SYMBOL_GPL(vimc_pix_map_by_index);
175 
vimc_pix_map_by_code(u32 code)176 const struct vimc_pix_map *vimc_pix_map_by_code(u32 code)
177 {
178 	unsigned int i;
179 
180 	for (i = 0; i < ARRAY_SIZE(vimc_pix_map_list); i++) {
181 		if (vimc_pix_map_list[i].code == code)
182 			return &vimc_pix_map_list[i];
183 	}
184 	return NULL;
185 }
186 EXPORT_SYMBOL_GPL(vimc_pix_map_by_code);
187 
vimc_pix_map_by_pixelformat(u32 pixelformat)188 const struct vimc_pix_map *vimc_pix_map_by_pixelformat(u32 pixelformat)
189 {
190 	unsigned int i;
191 
192 	for (i = 0; i < ARRAY_SIZE(vimc_pix_map_list); i++) {
193 		if (vimc_pix_map_list[i].pixelformat == pixelformat)
194 			return &vimc_pix_map_list[i];
195 	}
196 	return NULL;
197 }
198 EXPORT_SYMBOL_GPL(vimc_pix_map_by_pixelformat);
199 
200 /* Helper function to allocate and initialize pads */
vimc_pads_init(u16 num_pads,const unsigned long * pads_flag)201 struct media_pad *vimc_pads_init(u16 num_pads, const unsigned long *pads_flag)
202 {
203 	struct media_pad *pads;
204 	unsigned int i;
205 
206 	/* Allocate memory for the pads */
207 	pads = kcalloc(num_pads, sizeof(*pads), GFP_KERNEL);
208 	if (!pads)
209 		return ERR_PTR(-ENOMEM);
210 
211 	/* Initialize the pads */
212 	for (i = 0; i < num_pads; i++) {
213 		pads[i].index = i;
214 		pads[i].flags = pads_flag[i];
215 	}
216 
217 	return pads;
218 }
219 EXPORT_SYMBOL_GPL(vimc_pads_init);
220 
vimc_pipeline_s_stream(struct media_entity * ent,int enable)221 int vimc_pipeline_s_stream(struct media_entity *ent, int enable)
222 {
223 	struct v4l2_subdev *sd;
224 	struct media_pad *pad;
225 	unsigned int i;
226 	int ret;
227 
228 	for (i = 0; i < ent->num_pads; i++) {
229 		if (ent->pads[i].flags & MEDIA_PAD_FL_SOURCE)
230 			continue;
231 
232 		/* Start the stream in the subdevice direct connected */
233 		pad = media_entity_remote_pad(&ent->pads[i]);
234 		if (!pad)
235 			continue;
236 
237 		if (!is_media_entity_v4l2_subdev(pad->entity))
238 			return -EINVAL;
239 
240 		sd = media_entity_to_v4l2_subdev(pad->entity);
241 		ret = v4l2_subdev_call(sd, video, s_stream, enable);
242 		if (ret && ret != -ENOIOCTLCMD)
243 			return ret;
244 	}
245 
246 	return 0;
247 }
248 EXPORT_SYMBOL_GPL(vimc_pipeline_s_stream);
249 
vimc_get_mbus_format(struct media_pad * pad,struct v4l2_subdev_format * fmt)250 static int vimc_get_mbus_format(struct media_pad *pad,
251 				struct v4l2_subdev_format *fmt)
252 {
253 	if (is_media_entity_v4l2_subdev(pad->entity)) {
254 		struct v4l2_subdev *sd =
255 			media_entity_to_v4l2_subdev(pad->entity);
256 		int ret;
257 
258 		fmt->which = V4L2_SUBDEV_FORMAT_ACTIVE;
259 		fmt->pad = pad->index;
260 
261 		ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, fmt);
262 		if (ret)
263 			return ret;
264 
265 	} else if (is_media_entity_v4l2_video_device(pad->entity)) {
266 		struct video_device *vdev = container_of(pad->entity,
267 							 struct video_device,
268 							 entity);
269 		struct vimc_ent_device *ved = video_get_drvdata(vdev);
270 		const struct vimc_pix_map *vpix;
271 		struct v4l2_pix_format vdev_fmt;
272 
273 		if (!ved->vdev_get_format)
274 			return -ENOIOCTLCMD;
275 
276 		ved->vdev_get_format(ved, &vdev_fmt);
277 		vpix = vimc_pix_map_by_pixelformat(vdev_fmt.pixelformat);
278 		v4l2_fill_mbus_format(&fmt->format, &vdev_fmt, vpix->code);
279 	} else {
280 		return -EINVAL;
281 	}
282 
283 	return 0;
284 }
285 
vimc_link_validate(struct media_link * link)286 int vimc_link_validate(struct media_link *link)
287 {
288 	struct v4l2_subdev_format source_fmt, sink_fmt;
289 	int ret;
290 
291 	ret = vimc_get_mbus_format(link->source, &source_fmt);
292 	if (ret)
293 		return ret;
294 
295 	ret = vimc_get_mbus_format(link->sink, &sink_fmt);
296 	if (ret)
297 		return ret;
298 
299 	pr_info("vimc link validate: "
300 		"%s:src:%dx%d (0x%x, %d, %d, %d, %d) "
301 		"%s:snk:%dx%d (0x%x, %d, %d, %d, %d)\n",
302 		/* src */
303 		link->source->entity->name,
304 		source_fmt.format.width, source_fmt.format.height,
305 		source_fmt.format.code, source_fmt.format.colorspace,
306 		source_fmt.format.quantization, source_fmt.format.xfer_func,
307 		source_fmt.format.ycbcr_enc,
308 		/* sink */
309 		link->sink->entity->name,
310 		sink_fmt.format.width, sink_fmt.format.height,
311 		sink_fmt.format.code, sink_fmt.format.colorspace,
312 		sink_fmt.format.quantization, sink_fmt.format.xfer_func,
313 		sink_fmt.format.ycbcr_enc);
314 
315 	/* The width, height and code must match. */
316 	if (source_fmt.format.width != sink_fmt.format.width
317 	    || source_fmt.format.height != sink_fmt.format.height
318 	    || source_fmt.format.code != sink_fmt.format.code)
319 		return -EPIPE;
320 
321 	/*
322 	 * The field order must match, or the sink field order must be NONE
323 	 * to support interlaced hardware connected to bridges that support
324 	 * progressive formats only.
325 	 */
326 	if (source_fmt.format.field != sink_fmt.format.field &&
327 	    sink_fmt.format.field != V4L2_FIELD_NONE)
328 		return -EPIPE;
329 
330 	/*
331 	 * If colorspace is DEFAULT, then assume all the colorimetry is also
332 	 * DEFAULT, return 0 to skip comparing the other colorimetry parameters
333 	 */
334 	if (source_fmt.format.colorspace == V4L2_COLORSPACE_DEFAULT
335 	    || sink_fmt.format.colorspace == V4L2_COLORSPACE_DEFAULT)
336 		return 0;
337 
338 	/* Colorspace must match. */
339 	if (source_fmt.format.colorspace != sink_fmt.format.colorspace)
340 		return -EPIPE;
341 
342 	/* Colorimetry must match if they are not set to DEFAULT */
343 	if (source_fmt.format.ycbcr_enc != V4L2_YCBCR_ENC_DEFAULT
344 	    && sink_fmt.format.ycbcr_enc != V4L2_YCBCR_ENC_DEFAULT
345 	    && source_fmt.format.ycbcr_enc != sink_fmt.format.ycbcr_enc)
346 		return -EPIPE;
347 
348 	if (source_fmt.format.quantization != V4L2_QUANTIZATION_DEFAULT
349 	    && sink_fmt.format.quantization != V4L2_QUANTIZATION_DEFAULT
350 	    && source_fmt.format.quantization != sink_fmt.format.quantization)
351 		return -EPIPE;
352 
353 	if (source_fmt.format.xfer_func != V4L2_XFER_FUNC_DEFAULT
354 	    && sink_fmt.format.xfer_func != V4L2_XFER_FUNC_DEFAULT
355 	    && source_fmt.format.xfer_func != sink_fmt.format.xfer_func)
356 		return -EPIPE;
357 
358 	return 0;
359 }
360 EXPORT_SYMBOL_GPL(vimc_link_validate);
361 
362 static const struct media_entity_operations vimc_ent_sd_mops = {
363 	.link_validate = vimc_link_validate,
364 };
365 
vimc_ent_sd_register(struct vimc_ent_device * ved,struct v4l2_subdev * sd,struct v4l2_device * v4l2_dev,const char * const name,u32 function,u16 num_pads,const unsigned long * pads_flag,const struct v4l2_subdev_internal_ops * sd_int_ops,const struct v4l2_subdev_ops * sd_ops)366 int vimc_ent_sd_register(struct vimc_ent_device *ved,
367 			 struct v4l2_subdev *sd,
368 			 struct v4l2_device *v4l2_dev,
369 			 const char *const name,
370 			 u32 function,
371 			 u16 num_pads,
372 			 const unsigned long *pads_flag,
373 			 const struct v4l2_subdev_internal_ops *sd_int_ops,
374 			 const struct v4l2_subdev_ops *sd_ops)
375 {
376 	int ret;
377 
378 	/* Allocate the pads. Should be released from the sd_int_op release */
379 	ved->pads = vimc_pads_init(num_pads, pads_flag);
380 	if (IS_ERR(ved->pads))
381 		return PTR_ERR(ved->pads);
382 
383 	/* Fill the vimc_ent_device struct */
384 	ved->ent = &sd->entity;
385 
386 	/* Initialize the subdev */
387 	v4l2_subdev_init(sd, sd_ops);
388 	sd->internal_ops = sd_int_ops;
389 	sd->entity.function = function;
390 	sd->entity.ops = &vimc_ent_sd_mops;
391 	sd->owner = THIS_MODULE;
392 	strscpy(sd->name, name, sizeof(sd->name));
393 	v4l2_set_subdevdata(sd, ved);
394 
395 	/* Expose this subdev to user space */
396 	sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
397 	if (sd->ctrl_handler)
398 		sd->flags |= V4L2_SUBDEV_FL_HAS_EVENTS;
399 
400 	/* Initialize the media entity */
401 	ret = media_entity_pads_init(&sd->entity, num_pads, ved->pads);
402 	if (ret)
403 		goto err_clean_pads;
404 
405 	/* Register the subdev with the v4l2 and the media framework */
406 	ret = v4l2_device_register_subdev(v4l2_dev, sd);
407 	if (ret) {
408 		dev_err(v4l2_dev->dev,
409 			"%s: subdev register failed (err=%d)\n",
410 			name, ret);
411 		goto err_clean_m_ent;
412 	}
413 
414 	return 0;
415 
416 err_clean_m_ent:
417 	media_entity_cleanup(&sd->entity);
418 err_clean_pads:
419 	vimc_pads_cleanup(ved->pads);
420 	return ret;
421 }
422 EXPORT_SYMBOL_GPL(vimc_ent_sd_register);
423 
vimc_ent_sd_unregister(struct vimc_ent_device * ved,struct v4l2_subdev * sd)424 void vimc_ent_sd_unregister(struct vimc_ent_device *ved, struct v4l2_subdev *sd)
425 {
426 	media_entity_cleanup(ved->ent);
427 	v4l2_device_unregister_subdev(sd);
428 }
429 EXPORT_SYMBOL_GPL(vimc_ent_sd_unregister);
430