blob: da7f2b7a2e58d3a8c44eae8437e32a140fe47226 [file] [log] [blame]
Helen Fornazier5ba0ae42017-06-19 14:00:11 -03001/*
2 * vimc-common.c Virtual Media Controller Driver
3 *
4 * Copyright (C) 2015-2017 Helen Koike <helen.fornazier@gmail.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 */
17
Helen Fornazier4a29b702017-06-19 14:00:18 -030018#include <linux/init.h>
19#include <linux/module.h>
20
Helen Fornazier5ba0ae42017-06-19 14:00:11 -030021#include "vimc-common.h"
22
23static const struct vimc_pix_map vimc_pix_map_list[] = {
24 /* TODO: add all missing formats */
25
26 /* RGB formats */
27 {
28 .code = MEDIA_BUS_FMT_BGR888_1X24,
29 .pixelformat = V4L2_PIX_FMT_BGR24,
30 .bpp = 3,
31 },
32 {
33 .code = MEDIA_BUS_FMT_RGB888_1X24,
34 .pixelformat = V4L2_PIX_FMT_RGB24,
35 .bpp = 3,
36 },
37 {
38 .code = MEDIA_BUS_FMT_ARGB8888_1X32,
39 .pixelformat = V4L2_PIX_FMT_ARGB32,
40 .bpp = 4,
41 },
42
43 /* Bayer formats */
44 {
45 .code = MEDIA_BUS_FMT_SBGGR8_1X8,
46 .pixelformat = V4L2_PIX_FMT_SBGGR8,
47 .bpp = 1,
48 },
49 {
50 .code = MEDIA_BUS_FMT_SGBRG8_1X8,
51 .pixelformat = V4L2_PIX_FMT_SGBRG8,
52 .bpp = 1,
53 },
54 {
55 .code = MEDIA_BUS_FMT_SGRBG8_1X8,
56 .pixelformat = V4L2_PIX_FMT_SGRBG8,
57 .bpp = 1,
58 },
59 {
60 .code = MEDIA_BUS_FMT_SRGGB8_1X8,
61 .pixelformat = V4L2_PIX_FMT_SRGGB8,
62 .bpp = 1,
63 },
64 {
65 .code = MEDIA_BUS_FMT_SBGGR10_1X10,
66 .pixelformat = V4L2_PIX_FMT_SBGGR10,
67 .bpp = 2,
68 },
69 {
70 .code = MEDIA_BUS_FMT_SGBRG10_1X10,
71 .pixelformat = V4L2_PIX_FMT_SGBRG10,
72 .bpp = 2,
73 },
74 {
75 .code = MEDIA_BUS_FMT_SGRBG10_1X10,
76 .pixelformat = V4L2_PIX_FMT_SGRBG10,
77 .bpp = 2,
78 },
79 {
80 .code = MEDIA_BUS_FMT_SRGGB10_1X10,
81 .pixelformat = V4L2_PIX_FMT_SRGGB10,
82 .bpp = 2,
83 },
84
85 /* 10bit raw bayer a-law compressed to 8 bits */
86 {
87 .code = MEDIA_BUS_FMT_SBGGR10_ALAW8_1X8,
88 .pixelformat = V4L2_PIX_FMT_SBGGR10ALAW8,
89 .bpp = 1,
90 },
91 {
92 .code = MEDIA_BUS_FMT_SGBRG10_ALAW8_1X8,
93 .pixelformat = V4L2_PIX_FMT_SGBRG10ALAW8,
94 .bpp = 1,
95 },
96 {
97 .code = MEDIA_BUS_FMT_SGRBG10_ALAW8_1X8,
98 .pixelformat = V4L2_PIX_FMT_SGRBG10ALAW8,
99 .bpp = 1,
100 },
101 {
102 .code = MEDIA_BUS_FMT_SRGGB10_ALAW8_1X8,
103 .pixelformat = V4L2_PIX_FMT_SRGGB10ALAW8,
104 .bpp = 1,
105 },
106
107 /* 10bit raw bayer DPCM compressed to 8 bits */
108 {
109 .code = MEDIA_BUS_FMT_SBGGR10_DPCM8_1X8,
110 .pixelformat = V4L2_PIX_FMT_SBGGR10DPCM8,
111 .bpp = 1,
112 },
113 {
114 .code = MEDIA_BUS_FMT_SGBRG10_DPCM8_1X8,
115 .pixelformat = V4L2_PIX_FMT_SGBRG10DPCM8,
116 .bpp = 1,
117 },
118 {
119 .code = MEDIA_BUS_FMT_SGRBG10_DPCM8_1X8,
120 .pixelformat = V4L2_PIX_FMT_SGRBG10DPCM8,
121 .bpp = 1,
122 },
123 {
124 .code = MEDIA_BUS_FMT_SRGGB10_DPCM8_1X8,
125 .pixelformat = V4L2_PIX_FMT_SRGGB10DPCM8,
126 .bpp = 1,
127 },
128 {
129 .code = MEDIA_BUS_FMT_SBGGR12_1X12,
130 .pixelformat = V4L2_PIX_FMT_SBGGR12,
131 .bpp = 2,
132 },
133 {
134 .code = MEDIA_BUS_FMT_SGBRG12_1X12,
135 .pixelformat = V4L2_PIX_FMT_SGBRG12,
136 .bpp = 2,
137 },
138 {
139 .code = MEDIA_BUS_FMT_SGRBG12_1X12,
140 .pixelformat = V4L2_PIX_FMT_SGRBG12,
141 .bpp = 2,
142 },
143 {
144 .code = MEDIA_BUS_FMT_SRGGB12_1X12,
145 .pixelformat = V4L2_PIX_FMT_SRGGB12,
146 .bpp = 2,
147 },
148};
149
Helen Fornazier88ad71a2017-06-19 14:00:16 -0300150const struct vimc_pix_map *vimc_pix_map_by_index(unsigned int i)
151{
152 if (i >= ARRAY_SIZE(vimc_pix_map_list))
153 return NULL;
154
155 return &vimc_pix_map_list[i];
156}
Helen Fornazier4a29b702017-06-19 14:00:18 -0300157EXPORT_SYMBOL_GPL(vimc_pix_map_by_index);
Helen Fornazier88ad71a2017-06-19 14:00:16 -0300158
Helen Fornazier5ba0ae42017-06-19 14:00:11 -0300159const struct vimc_pix_map *vimc_pix_map_by_code(u32 code)
160{
161 unsigned int i;
162
163 for (i = 0; i < ARRAY_SIZE(vimc_pix_map_list); i++) {
164 if (vimc_pix_map_list[i].code == code)
165 return &vimc_pix_map_list[i];
166 }
167 return NULL;
168}
Helen Fornazier4a29b702017-06-19 14:00:18 -0300169EXPORT_SYMBOL_GPL(vimc_pix_map_by_code);
Helen Fornazier5ba0ae42017-06-19 14:00:11 -0300170
171const struct vimc_pix_map *vimc_pix_map_by_pixelformat(u32 pixelformat)
172{
173 unsigned int i;
174
175 for (i = 0; i < ARRAY_SIZE(vimc_pix_map_list); i++) {
176 if (vimc_pix_map_list[i].pixelformat == pixelformat)
177 return &vimc_pix_map_list[i];
178 }
179 return NULL;
180}
Helen Fornazier4a29b702017-06-19 14:00:18 -0300181EXPORT_SYMBOL_GPL(vimc_pix_map_by_pixelformat);
Helen Fornazier5ba0ae42017-06-19 14:00:11 -0300182
183int vimc_propagate_frame(struct media_pad *src, const void *frame)
184{
185 struct media_link *link;
186
187 if (!(src->flags & MEDIA_PAD_FL_SOURCE))
188 return -EINVAL;
189
190 /* Send this frame to all sink pads that are direct linked */
191 list_for_each_entry(link, &src->entity->links, list) {
192 if (link->source == src &&
193 (link->flags & MEDIA_LNK_FL_ENABLED)) {
194 struct vimc_ent_device *ved = NULL;
195 struct media_entity *entity = link->sink->entity;
196
197 if (is_media_entity_v4l2_subdev(entity)) {
198 struct v4l2_subdev *sd =
199 container_of(entity, struct v4l2_subdev,
200 entity);
201 ved = v4l2_get_subdevdata(sd);
202 } else if (is_media_entity_v4l2_video_device(entity)) {
203 struct video_device *vdev =
204 container_of(entity,
205 struct video_device,
206 entity);
207 ved = video_get_drvdata(vdev);
208 }
209 if (ved && ved->process_frame)
210 ved->process_frame(ved, link->sink, frame);
211 }
212 }
213
214 return 0;
215}
Helen Fornazier4a29b702017-06-19 14:00:18 -0300216EXPORT_SYMBOL_GPL(vimc_propagate_frame);
Helen Fornazier5ba0ae42017-06-19 14:00:11 -0300217
218/* Helper function to allocate and initialize pads */
219struct media_pad *vimc_pads_init(u16 num_pads, const unsigned long *pads_flag)
220{
221 struct media_pad *pads;
222 unsigned int i;
223
224 /* Allocate memory for the pads */
225 pads = kcalloc(num_pads, sizeof(*pads), GFP_KERNEL);
226 if (!pads)
227 return ERR_PTR(-ENOMEM);
228
229 /* Initialize the pads */
230 for (i = 0; i < num_pads; i++) {
231 pads[i].index = i;
232 pads[i].flags = pads_flag[i];
233 }
234
235 return pads;
236}
Helen Fornazier4a29b702017-06-19 14:00:18 -0300237EXPORT_SYMBOL_GPL(vimc_pads_init);
Helen Fornazierc1495432017-06-19 14:00:12 -0300238
Helen Fornazierbf5fb952017-06-19 14:00:13 -0300239int vimc_pipeline_s_stream(struct media_entity *ent, int enable)
240{
241 struct v4l2_subdev *sd;
242 struct media_pad *pad;
243 unsigned int i;
244 int ret;
245
246 for (i = 0; i < ent->num_pads; i++) {
247 if (ent->pads[i].flags & MEDIA_PAD_FL_SOURCE)
248 continue;
249
250 /* Start the stream in the subdevice direct connected */
251 pad = media_entity_remote_pad(&ent->pads[i]);
252
Helen Fornazier4a29b702017-06-19 14:00:18 -0300253 if (!is_media_entity_v4l2_subdev(pad->entity))
254 return -EINVAL;
Helen Fornazierbf5fb952017-06-19 14:00:13 -0300255
256 sd = media_entity_to_v4l2_subdev(pad->entity);
257 ret = v4l2_subdev_call(sd, video, s_stream, enable);
258 if (ret && ret != -ENOIOCTLCMD)
259 return ret;
260 }
261
262 return 0;
263}
Helen Fornazier4a29b702017-06-19 14:00:18 -0300264EXPORT_SYMBOL_GPL(vimc_pipeline_s_stream);
Helen Fornazierbf5fb952017-06-19 14:00:13 -0300265
Helen Fornazier288a22d2017-06-19 14:00:14 -0300266static int vimc_get_mbus_format(struct media_pad *pad,
267 struct v4l2_subdev_format *fmt)
268{
269 if (is_media_entity_v4l2_subdev(pad->entity)) {
270 struct v4l2_subdev *sd =
271 media_entity_to_v4l2_subdev(pad->entity);
272 int ret;
273
274 fmt->which = V4L2_SUBDEV_FORMAT_ACTIVE;
275 fmt->pad = pad->index;
276
277 ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, fmt);
278 if (ret)
279 return ret;
280
281 } else if (is_media_entity_v4l2_video_device(pad->entity)) {
282 struct video_device *vdev = container_of(pad->entity,
283 struct video_device,
284 entity);
285 struct vimc_ent_device *ved = video_get_drvdata(vdev);
286 const struct vimc_pix_map *vpix;
287 struct v4l2_pix_format vdev_fmt;
288
289 if (!ved->vdev_get_format)
290 return -ENOIOCTLCMD;
291
292 ved->vdev_get_format(ved, &vdev_fmt);
293 vpix = vimc_pix_map_by_pixelformat(vdev_fmt.pixelformat);
294 v4l2_fill_mbus_format(&fmt->format, &vdev_fmt, vpix->code);
295 } else {
296 return -EINVAL;
297 }
298
299 return 0;
300}
301
302int vimc_link_validate(struct media_link *link)
303{
304 struct v4l2_subdev_format source_fmt, sink_fmt;
305 int ret;
306
Helen Fornazier288a22d2017-06-19 14:00:14 -0300307 ret = vimc_get_mbus_format(link->source, &source_fmt);
308 if (ret)
309 return ret;
310
311 ret = vimc_get_mbus_format(link->sink, &sink_fmt);
312 if (ret)
313 return ret;
314
315 pr_info("vimc link validate: "
316 "%s:src:%dx%d (0x%x, %d, %d, %d, %d) "
317 "%s:snk:%dx%d (0x%x, %d, %d, %d, %d)\n",
318 /* src */
319 link->source->entity->name,
320 source_fmt.format.width, source_fmt.format.height,
321 source_fmt.format.code, source_fmt.format.colorspace,
322 source_fmt.format.quantization, source_fmt.format.xfer_func,
323 source_fmt.format.ycbcr_enc,
324 /* sink */
325 link->sink->entity->name,
326 sink_fmt.format.width, sink_fmt.format.height,
327 sink_fmt.format.code, sink_fmt.format.colorspace,
328 sink_fmt.format.quantization, sink_fmt.format.xfer_func,
329 sink_fmt.format.ycbcr_enc);
330
331 /* The width, height and code must match. */
332 if (source_fmt.format.width != sink_fmt.format.width
333 || source_fmt.format.height != sink_fmt.format.height
334 || source_fmt.format.code != sink_fmt.format.code)
335 return -EPIPE;
336
337 /*
338 * The field order must match, or the sink field order must be NONE
339 * to support interlaced hardware connected to bridges that support
340 * progressive formats only.
341 */
342 if (source_fmt.format.field != sink_fmt.format.field &&
343 sink_fmt.format.field != V4L2_FIELD_NONE)
344 return -EPIPE;
345
346 /*
347 * If colorspace is DEFAULT, then assume all the colorimetry is also
348 * DEFAULT, return 0 to skip comparing the other colorimetry parameters
349 */
350 if (source_fmt.format.colorspace == V4L2_COLORSPACE_DEFAULT
351 || sink_fmt.format.colorspace == V4L2_COLORSPACE_DEFAULT)
352 return 0;
353
354 /* Colorspace must match. */
355 if (source_fmt.format.colorspace != sink_fmt.format.colorspace)
356 return -EPIPE;
357
358 /* Colorimetry must match if they are not set to DEFAULT */
359 if (source_fmt.format.ycbcr_enc != V4L2_YCBCR_ENC_DEFAULT
360 && sink_fmt.format.ycbcr_enc != V4L2_YCBCR_ENC_DEFAULT
361 && source_fmt.format.ycbcr_enc != sink_fmt.format.ycbcr_enc)
362 return -EPIPE;
363
364 if (source_fmt.format.quantization != V4L2_QUANTIZATION_DEFAULT
365 && sink_fmt.format.quantization != V4L2_QUANTIZATION_DEFAULT
366 && source_fmt.format.quantization != sink_fmt.format.quantization)
367 return -EPIPE;
368
369 if (source_fmt.format.xfer_func != V4L2_XFER_FUNC_DEFAULT
370 && sink_fmt.format.xfer_func != V4L2_XFER_FUNC_DEFAULT
371 && source_fmt.format.xfer_func != sink_fmt.format.xfer_func)
372 return -EPIPE;
373
374 return 0;
375}
Helen Fornazier4a29b702017-06-19 14:00:18 -0300376EXPORT_SYMBOL_GPL(vimc_link_validate);
Helen Fornazier288a22d2017-06-19 14:00:14 -0300377
Helen Fornazierc1495432017-06-19 14:00:12 -0300378static const struct media_entity_operations vimc_ent_sd_mops = {
Helen Fornazier288a22d2017-06-19 14:00:14 -0300379 .link_validate = vimc_link_validate,
Helen Fornazierc1495432017-06-19 14:00:12 -0300380};
381
382int vimc_ent_sd_register(struct vimc_ent_device *ved,
383 struct v4l2_subdev *sd,
384 struct v4l2_device *v4l2_dev,
385 const char *const name,
386 u32 function,
387 u16 num_pads,
388 const unsigned long *pads_flag,
Helen Fornazier4a29b702017-06-19 14:00:18 -0300389 const struct v4l2_subdev_ops *sd_ops)
Helen Fornazierc1495432017-06-19 14:00:12 -0300390{
391 int ret;
392
393 /* Allocate the pads */
394 ved->pads = vimc_pads_init(num_pads, pads_flag);
395 if (IS_ERR(ved->pads))
396 return PTR_ERR(ved->pads);
397
398 /* Fill the vimc_ent_device struct */
Helen Fornazierc1495432017-06-19 14:00:12 -0300399 ved->ent = &sd->entity;
400
401 /* Initialize the subdev */
402 v4l2_subdev_init(sd, sd_ops);
403 sd->entity.function = function;
404 sd->entity.ops = &vimc_ent_sd_mops;
405 sd->owner = THIS_MODULE;
406 strlcpy(sd->name, name, sizeof(sd->name));
407 v4l2_set_subdevdata(sd, ved);
408
409 /* Expose this subdev to user space */
410 sd->flags = V4L2_SUBDEV_FL_HAS_DEVNODE;
411
412 /* Initialize the media entity */
413 ret = media_entity_pads_init(&sd->entity, num_pads, ved->pads);
414 if (ret)
415 goto err_clean_pads;
416
417 /* Register the subdev with the v4l2 and the media framework */
418 ret = v4l2_device_register_subdev(v4l2_dev, sd);
419 if (ret) {
420 dev_err(v4l2_dev->dev,
421 "%s: subdev register failed (err=%d)\n",
422 name, ret);
423 goto err_clean_m_ent;
424 }
425
426 return 0;
427
428err_clean_m_ent:
429 media_entity_cleanup(&sd->entity);
430err_clean_pads:
431 vimc_pads_cleanup(ved->pads);
432 return ret;
433}
Helen Fornazier4a29b702017-06-19 14:00:18 -0300434EXPORT_SYMBOL_GPL(vimc_ent_sd_register);
Helen Fornazierc1495432017-06-19 14:00:12 -0300435
436void vimc_ent_sd_unregister(struct vimc_ent_device *ved, struct v4l2_subdev *sd)
437{
438 v4l2_device_unregister_subdev(sd);
439 media_entity_cleanup(ved->ent);
440 vimc_pads_cleanup(ved->pads);
441}
Helen Fornazier4a29b702017-06-19 14:00:18 -0300442EXPORT_SYMBOL_GPL(vimc_ent_sd_unregister);
443
444MODULE_DESCRIPTION("Virtual Media Controller Driver (VIMC) Common");
445MODULE_AUTHOR("Helen Koike <helen.fornazier@gmail.com>");
446MODULE_LICENSE("GPL");