blob: fad7c7c6de936ae91779a6f9701dd002f41a62c0 [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
Helen Fornazierb6c61a62019-03-13 14:29:37 -040023static const __u32 vimc_mbus_list[] = {
24 MEDIA_BUS_FMT_FIXED,
25 MEDIA_BUS_FMT_RGB444_1X12,
26 MEDIA_BUS_FMT_RGB444_2X8_PADHI_BE,
27 MEDIA_BUS_FMT_RGB444_2X8_PADHI_LE,
28 MEDIA_BUS_FMT_RGB555_2X8_PADHI_BE,
29 MEDIA_BUS_FMT_RGB555_2X8_PADHI_LE,
30 MEDIA_BUS_FMT_RGB565_1X16,
31 MEDIA_BUS_FMT_BGR565_2X8_BE,
32 MEDIA_BUS_FMT_BGR565_2X8_LE,
33 MEDIA_BUS_FMT_RGB565_2X8_BE,
34 MEDIA_BUS_FMT_RGB565_2X8_LE,
35 MEDIA_BUS_FMT_RGB666_1X18,
36 MEDIA_BUS_FMT_RBG888_1X24,
37 MEDIA_BUS_FMT_RGB666_1X24_CPADHI,
38 MEDIA_BUS_FMT_RGB666_1X7X3_SPWG,
39 MEDIA_BUS_FMT_BGR888_1X24,
40 MEDIA_BUS_FMT_GBR888_1X24,
41 MEDIA_BUS_FMT_RGB888_1X24,
42 MEDIA_BUS_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MEDIA_BUS_FMT_SGRBG10_1X10,
114 MEDIA_BUS_FMT_SRGGB10_1X10,
115 MEDIA_BUS_FMT_SBGGR12_1X12,
116 MEDIA_BUS_FMT_SGBRG12_1X12,
117 MEDIA_BUS_FMT_SGRBG12_1X12,
118 MEDIA_BUS_FMT_SRGGB12_1X12,
119 MEDIA_BUS_FMT_SBGGR14_1X14,
120 MEDIA_BUS_FMT_SGBRG14_1X14,
121 MEDIA_BUS_FMT_SGRBG14_1X14,
122 MEDIA_BUS_FMT_SRGGB14_1X14,
123 MEDIA_BUS_FMT_SBGGR16_1X16,
124 MEDIA_BUS_FMT_SGBRG16_1X16,
125 MEDIA_BUS_FMT_SGRBG16_1X16,
126 MEDIA_BUS_FMT_SRGGB16_1X16,
127 MEDIA_BUS_FMT_JPEG_1X8,
128 MEDIA_BUS_FMT_S5C_UYVY_JPEG_1X8,
129 MEDIA_BUS_FMT_AHSV8888_1X32,
Helen Fornazier5ba0ae42017-06-19 14:00:11 -0300130};
131
Helen Fornazierb6c61a62019-03-13 14:29:37 -0400132/* Helper function to check mbus codes */
133bool vimc_mbus_code_supported(__u32 code)
Helen Fornazier5ba0ae42017-06-19 14:00:11 -0300134{
135 unsigned int i;
136
Helen Fornazierb6c61a62019-03-13 14:29:37 -0400137 for (i = 0; i < ARRAY_SIZE(vimc_mbus_list); i++)
138 if (code == vimc_mbus_list[i])
139 return true;
140 return false;
Helen Fornazier5ba0ae42017-06-19 14:00:11 -0300141}
Helen Fornazierb6c61a62019-03-13 14:29:37 -0400142EXPORT_SYMBOL_GPL(vimc_mbus_code_supported);
Helen Fornazier5ba0ae42017-06-19 14:00:11 -0300143
Helen Fornazierb6c61a62019-03-13 14:29:37 -0400144/* Helper function to enumerate mbus codes */
145int vimc_enum_mbus_code(struct v4l2_subdev *sd,
146 struct v4l2_subdev_pad_config *cfg,
147 struct v4l2_subdev_mbus_code_enum *code)
Helen Fornazier5ba0ae42017-06-19 14:00:11 -0300148{
Helen Fornazierb6c61a62019-03-13 14:29:37 -0400149 if (code->index >= ARRAY_SIZE(vimc_mbus_list))
150 return -EINVAL;
Helen Fornazier5ba0ae42017-06-19 14:00:11 -0300151
Helen Fornazierb6c61a62019-03-13 14:29:37 -0400152 code->code = vimc_mbus_list[code->index];
153 return 0;
Helen Fornazier5ba0ae42017-06-19 14:00:11 -0300154}
Helen Fornazierb6c61a62019-03-13 14:29:37 -0400155EXPORT_SYMBOL_GPL(vimc_enum_mbus_code);
Helen Fornazier5ba0ae42017-06-19 14:00:11 -0300156
Helen Fornazier5ba0ae42017-06-19 14:00:11 -0300157/* Helper function to allocate and initialize pads */
158struct media_pad *vimc_pads_init(u16 num_pads, const unsigned long *pads_flag)
159{
160 struct media_pad *pads;
161 unsigned int i;
162
163 /* Allocate memory for the pads */
164 pads = kcalloc(num_pads, sizeof(*pads), GFP_KERNEL);
165 if (!pads)
166 return ERR_PTR(-ENOMEM);
167
168 /* Initialize the pads */
169 for (i = 0; i < num_pads; i++) {
170 pads[i].index = i;
171 pads[i].flags = pads_flag[i];
172 }
173
174 return pads;
175}
Helen Fornazier4a29b702017-06-19 14:00:18 -0300176EXPORT_SYMBOL_GPL(vimc_pads_init);
Helen Fornazierc1495432017-06-19 14:00:12 -0300177
Helen Fornazierbf5fb952017-06-19 14:00:13 -0300178int vimc_pipeline_s_stream(struct media_entity *ent, int enable)
179{
180 struct v4l2_subdev *sd;
181 struct media_pad *pad;
182 unsigned int i;
183 int ret;
184
185 for (i = 0; i < ent->num_pads; i++) {
186 if (ent->pads[i].flags & MEDIA_PAD_FL_SOURCE)
187 continue;
188
189 /* Start the stream in the subdevice direct connected */
190 pad = media_entity_remote_pad(&ent->pads[i]);
Helen Fornaziere159b602018-12-07 12:56:41 -0500191 if (!pad)
192 continue;
Helen Fornazierbf5fb952017-06-19 14:00:13 -0300193
Helen Fornazier4a29b702017-06-19 14:00:18 -0300194 if (!is_media_entity_v4l2_subdev(pad->entity))
195 return -EINVAL;
Helen Fornazierbf5fb952017-06-19 14:00:13 -0300196
197 sd = media_entity_to_v4l2_subdev(pad->entity);
198 ret = v4l2_subdev_call(sd, video, s_stream, enable);
199 if (ret && ret != -ENOIOCTLCMD)
200 return ret;
201 }
202
203 return 0;
204}
Helen Fornazier4a29b702017-06-19 14:00:18 -0300205EXPORT_SYMBOL_GPL(vimc_pipeline_s_stream);
Helen Fornazierbf5fb952017-06-19 14:00:13 -0300206
Helen Fornazier288a22d2017-06-19 14:00:14 -0300207static int vimc_get_mbus_format(struct media_pad *pad,
208 struct v4l2_subdev_format *fmt)
209{
210 if (is_media_entity_v4l2_subdev(pad->entity)) {
211 struct v4l2_subdev *sd =
212 media_entity_to_v4l2_subdev(pad->entity);
213 int ret;
214
215 fmt->which = V4L2_SUBDEV_FORMAT_ACTIVE;
216 fmt->pad = pad->index;
217
218 ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, fmt);
219 if (ret)
220 return ret;
221
222 } else if (is_media_entity_v4l2_video_device(pad->entity)) {
223 struct video_device *vdev = container_of(pad->entity,
224 struct video_device,
225 entity);
226 struct vimc_ent_device *ved = video_get_drvdata(vdev);
Helen Fornazier288a22d2017-06-19 14:00:14 -0300227 struct v4l2_pix_format vdev_fmt;
228
229 if (!ved->vdev_get_format)
230 return -ENOIOCTLCMD;
231
232 ved->vdev_get_format(ved, &vdev_fmt);
Helen Fornazierb6c61a62019-03-13 14:29:37 -0400233 v4l2_fill_mbus_format(&fmt->format, &vdev_fmt, 0);
Helen Fornazier288a22d2017-06-19 14:00:14 -0300234 } else {
235 return -EINVAL;
236 }
237
238 return 0;
239}
240
241int vimc_link_validate(struct media_link *link)
242{
243 struct v4l2_subdev_format source_fmt, sink_fmt;
244 int ret;
245
Helen Fornazier288a22d2017-06-19 14:00:14 -0300246 ret = vimc_get_mbus_format(link->source, &source_fmt);
247 if (ret)
248 return ret;
249
250 ret = vimc_get_mbus_format(link->sink, &sink_fmt);
251 if (ret)
252 return ret;
253
254 pr_info("vimc link validate: "
255 "%s:src:%dx%d (0x%x, %d, %d, %d, %d) "
256 "%s:snk:%dx%d (0x%x, %d, %d, %d, %d)\n",
257 /* src */
258 link->source->entity->name,
259 source_fmt.format.width, source_fmt.format.height,
260 source_fmt.format.code, source_fmt.format.colorspace,
261 source_fmt.format.quantization, source_fmt.format.xfer_func,
262 source_fmt.format.ycbcr_enc,
263 /* sink */
264 link->sink->entity->name,
265 sink_fmt.format.width, sink_fmt.format.height,
266 sink_fmt.format.code, sink_fmt.format.colorspace,
267 sink_fmt.format.quantization, sink_fmt.format.xfer_func,
268 sink_fmt.format.ycbcr_enc);
269
270 /* The width, height and code must match. */
271 if (source_fmt.format.width != sink_fmt.format.width
272 || source_fmt.format.height != sink_fmt.format.height
Helen Fornazierb6c61a62019-03-13 14:29:37 -0400273 || (source_fmt.format.code && sink_fmt.format.code &&
274 source_fmt.format.code != sink_fmt.format.code)) {
275 pr_err("vimc: format doesn't match in link %s->%s\n",
276 link->source->entity->name, link->sink->entity->name);
Helen Fornazier288a22d2017-06-19 14:00:14 -0300277 return -EPIPE;
Helen Fornazierb6c61a62019-03-13 14:29:37 -0400278 }
Helen Fornazier288a22d2017-06-19 14:00:14 -0300279
280 /*
281 * The field order must match, or the sink field order must be NONE
282 * to support interlaced hardware connected to bridges that support
283 * progressive formats only.
284 */
285 if (source_fmt.format.field != sink_fmt.format.field &&
286 sink_fmt.format.field != V4L2_FIELD_NONE)
287 return -EPIPE;
288
289 /*
290 * If colorspace is DEFAULT, then assume all the colorimetry is also
291 * DEFAULT, return 0 to skip comparing the other colorimetry parameters
292 */
293 if (source_fmt.format.colorspace == V4L2_COLORSPACE_DEFAULT
294 || sink_fmt.format.colorspace == V4L2_COLORSPACE_DEFAULT)
295 return 0;
296
297 /* Colorspace must match. */
298 if (source_fmt.format.colorspace != sink_fmt.format.colorspace)
299 return -EPIPE;
300
301 /* Colorimetry must match if they are not set to DEFAULT */
302 if (source_fmt.format.ycbcr_enc != V4L2_YCBCR_ENC_DEFAULT
303 && sink_fmt.format.ycbcr_enc != V4L2_YCBCR_ENC_DEFAULT
304 && source_fmt.format.ycbcr_enc != sink_fmt.format.ycbcr_enc)
305 return -EPIPE;
306
307 if (source_fmt.format.quantization != V4L2_QUANTIZATION_DEFAULT
308 && sink_fmt.format.quantization != V4L2_QUANTIZATION_DEFAULT
309 && source_fmt.format.quantization != sink_fmt.format.quantization)
310 return -EPIPE;
311
312 if (source_fmt.format.xfer_func != V4L2_XFER_FUNC_DEFAULT
313 && sink_fmt.format.xfer_func != V4L2_XFER_FUNC_DEFAULT
314 && source_fmt.format.xfer_func != sink_fmt.format.xfer_func)
315 return -EPIPE;
316
317 return 0;
318}
Helen Fornazier4a29b702017-06-19 14:00:18 -0300319EXPORT_SYMBOL_GPL(vimc_link_validate);
Helen Fornazier288a22d2017-06-19 14:00:14 -0300320
Helen Fornazierc1495432017-06-19 14:00:12 -0300321static const struct media_entity_operations vimc_ent_sd_mops = {
Helen Fornazier288a22d2017-06-19 14:00:14 -0300322 .link_validate = vimc_link_validate,
Helen Fornazierc1495432017-06-19 14:00:12 -0300323};
324
325int vimc_ent_sd_register(struct vimc_ent_device *ved,
326 struct v4l2_subdev *sd,
327 struct v4l2_device *v4l2_dev,
328 const char *const name,
329 u32 function,
330 u16 num_pads,
331 const unsigned long *pads_flag,
Hans Verkuil2b177f22019-03-05 03:36:20 -0500332 const struct v4l2_subdev_internal_ops *sd_int_ops,
Helen Fornazier4a29b702017-06-19 14:00:18 -0300333 const struct v4l2_subdev_ops *sd_ops)
Helen Fornazierc1495432017-06-19 14:00:12 -0300334{
335 int ret;
336
337 /* Allocate the pads */
338 ved->pads = vimc_pads_init(num_pads, pads_flag);
339 if (IS_ERR(ved->pads))
340 return PTR_ERR(ved->pads);
341
342 /* Fill the vimc_ent_device struct */
Helen Fornazierc1495432017-06-19 14:00:12 -0300343 ved->ent = &sd->entity;
344
345 /* Initialize the subdev */
346 v4l2_subdev_init(sd, sd_ops);
Hans Verkuil2b177f22019-03-05 03:36:20 -0500347 sd->internal_ops = sd_int_ops;
Helen Fornazierc1495432017-06-19 14:00:12 -0300348 sd->entity.function = function;
349 sd->entity.ops = &vimc_ent_sd_mops;
350 sd->owner = THIS_MODULE;
Mauro Carvalho Chehabc0decac2018-09-10 08:19:14 -0400351 strscpy(sd->name, name, sizeof(sd->name));
Helen Fornazierc1495432017-06-19 14:00:12 -0300352 v4l2_set_subdevdata(sd, ved);
353
354 /* Expose this subdev to user space */
Hans Verkuil3da7ee92018-02-02 08:00:32 -0500355 sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
356 if (sd->ctrl_handler)
357 sd->flags |= V4L2_SUBDEV_FL_HAS_EVENTS;
Helen Fornazierc1495432017-06-19 14:00:12 -0300358
359 /* Initialize the media entity */
360 ret = media_entity_pads_init(&sd->entity, num_pads, ved->pads);
361 if (ret)
362 goto err_clean_pads;
363
364 /* Register the subdev with the v4l2 and the media framework */
365 ret = v4l2_device_register_subdev(v4l2_dev, sd);
366 if (ret) {
367 dev_err(v4l2_dev->dev,
368 "%s: subdev register failed (err=%d)\n",
369 name, ret);
370 goto err_clean_m_ent;
371 }
372
373 return 0;
374
375err_clean_m_ent:
376 media_entity_cleanup(&sd->entity);
377err_clean_pads:
378 vimc_pads_cleanup(ved->pads);
379 return ret;
380}
Helen Fornazier4a29b702017-06-19 14:00:18 -0300381EXPORT_SYMBOL_GPL(vimc_ent_sd_register);
Helen Fornazierc1495432017-06-19 14:00:12 -0300382
383void vimc_ent_sd_unregister(struct vimc_ent_device *ved, struct v4l2_subdev *sd)
384{
Helen Fornazierc1495432017-06-19 14:00:12 -0300385 media_entity_cleanup(ved->ent);
386 vimc_pads_cleanup(ved->pads);
Hans Verkuil2b177f22019-03-05 03:36:20 -0500387 v4l2_device_unregister_subdev(sd);
Helen Fornazierc1495432017-06-19 14:00:12 -0300388}
Helen Fornazier4a29b702017-06-19 14:00:18 -0300389EXPORT_SYMBOL_GPL(vimc_ent_sd_unregister);
390
391MODULE_DESCRIPTION("Virtual Media Controller Driver (VIMC) Common");
392MODULE_AUTHOR("Helen Koike <helen.fornazier@gmail.com>");
393MODULE_LICENSE("GPL");