2 * V4L2 Media Controller Driver for Freescale i.MX5/6 SOC
4 * Copyright (c) 2016 Mentor Graphics Inc.
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.
11 #include <linux/delay.h>
13 #include <linux/module.h>
14 #include <linux/of_graph.h>
15 #include <linux/of_platform.h>
16 #include <linux/pinctrl/consumer.h>
17 #include <linux/platform_device.h>
18 #include <linux/sched.h>
19 #include <linux/slab.h>
20 #include <linux/spinlock.h>
21 #include <linux/timer.h>
22 #include <media/v4l2-ctrls.h>
23 #include <media/v4l2-event.h>
24 #include <media/v4l2-ioctl.h>
25 #include <media/v4l2-mc.h>
26 #include <video/imx-ipu-v3.h>
27 #include <media/imx.h>
28 #include "imx-media.h"
30 static inline struct imx_media_dev *notifier2dev(struct v4l2_async_notifier *n)
32 return container_of(n, struct imx_media_dev, subdev_notifier);
36 * Find an asd by fwnode or device name. This is called during
37 * driver load to form the async subdev list and bind them.
39 static struct v4l2_async_subdev *
40 find_async_subdev(struct imx_media_dev *imxmd,
41 struct fwnode_handle *fwnode,
44 struct imx_media_async_subdev *imxasd;
45 struct v4l2_async_subdev *asd;
47 list_for_each_entry(imxasd, &imxmd->asd_list, list) {
49 switch (asd->match_type) {
50 case V4L2_ASYNC_MATCH_FWNODE:
51 if (fwnode && asd->match.fwnode == fwnode)
54 case V4L2_ASYNC_MATCH_DEVNAME:
55 if (devname && !strcmp(asd->match.device_name,
69 * Adds a subdev to the async subdev list. If fwnode is non-NULL, adds
70 * the async as a V4L2_ASYNC_MATCH_FWNODE match type, otherwise as
71 * a V4L2_ASYNC_MATCH_DEVNAME match type using the dev_name of the
72 * given platform_device. This is called during driver load when
73 * forming the async subdev list.
75 int imx_media_add_async_subdev(struct imx_media_dev *imxmd,
76 struct fwnode_handle *fwnode,
77 struct platform_device *pdev)
79 struct device_node *np = to_of_node(fwnode);
80 struct imx_media_async_subdev *imxasd;
81 struct v4l2_async_subdev *asd;
82 const char *devname = NULL;
85 mutex_lock(&imxmd->mutex);
88 devname = dev_name(&pdev->dev);
90 /* return -EEXIST if this asd already added */
91 if (find_async_subdev(imxmd, fwnode, devname)) {
92 dev_dbg(imxmd->md.dev, "%s: already added %s\n",
93 __func__, np ? np->name : devname);
98 imxasd = devm_kzalloc(imxmd->md.dev, sizeof(*imxasd), GFP_KERNEL);
106 asd->match_type = V4L2_ASYNC_MATCH_FWNODE;
107 asd->match.fwnode = fwnode;
109 asd->match_type = V4L2_ASYNC_MATCH_DEVNAME;
110 asd->match.device_name = devname;
114 list_add_tail(&imxasd->list, &imxmd->asd_list);
116 imxmd->subdev_notifier.num_subdevs++;
118 dev_dbg(imxmd->md.dev, "%s: added %s, match type %s\n",
119 __func__, np ? np->name : devname, np ? "FWNODE" : "DEVNAME");
122 mutex_unlock(&imxmd->mutex);
127 * get IPU from this CSI and add it to the list of IPUs
128 * the media driver will control.
130 static int imx_media_get_ipu(struct imx_media_dev *imxmd,
131 struct v4l2_subdev *csi_sd)
136 ipu = dev_get_drvdata(csi_sd->dev->parent);
138 v4l2_err(&imxmd->v4l2_dev,
139 "CSI %s has no parent IPU!\n", csi_sd->name);
143 ipu_id = ipu_get_num(ipu);
145 v4l2_err(&imxmd->v4l2_dev, "invalid IPU id %d!\n", ipu_id);
149 if (!imxmd->ipu[ipu_id])
150 imxmd->ipu[ipu_id] = ipu;
155 /* async subdev bound notifier */
156 static int imx_media_subdev_bound(struct v4l2_async_notifier *notifier,
157 struct v4l2_subdev *sd,
158 struct v4l2_async_subdev *asd)
160 struct imx_media_dev *imxmd = notifier2dev(notifier);
163 mutex_lock(&imxmd->mutex);
165 if (sd->grp_id & IMX_MEDIA_GRP_ID_CSI) {
166 ret = imx_media_get_ipu(imxmd, sd);
171 v4l2_info(&imxmd->v4l2_dev, "subdev %s bound\n", sd->name);
173 mutex_unlock(&imxmd->mutex);
178 * create the media links for all subdevs that registered async.
179 * Called after all async subdevs have bound.
181 static int imx_media_create_links(struct v4l2_async_notifier *notifier)
183 struct imx_media_dev *imxmd = notifier2dev(notifier);
184 struct v4l2_subdev *sd;
188 * Only links are created between subdevices that are known
189 * to the async notifier. If there are other non-async subdevices,
190 * they were created internally by some subdevice (smiapp is one
191 * example). In those cases it is expected the subdevice is
192 * responsible for creating those internal links.
194 list_for_each_entry(sd, ¬ifier->done, async_list) {
195 switch (sd->grp_id) {
196 case IMX_MEDIA_GRP_ID_VDIC:
197 case IMX_MEDIA_GRP_ID_IC_PRP:
198 case IMX_MEDIA_GRP_ID_IC_PRPENC:
199 case IMX_MEDIA_GRP_ID_IC_PRPVF:
200 case IMX_MEDIA_GRP_ID_CSI0:
201 case IMX_MEDIA_GRP_ID_CSI1:
202 ret = imx_media_create_internal_links(imxmd, sd);
206 * the CSIs straddle between the external and the IPU
207 * internal entities, so create the external links
208 * to the CSI sink pads.
210 if (sd->grp_id & IMX_MEDIA_GRP_ID_CSI)
211 imx_media_create_csi_of_links(imxmd, sd);
214 /* this is an external fwnode subdev */
215 imx_media_create_of_links(imxmd, sd);
224 * adds given video device to given imx-media source pad vdev list.
225 * Continues upstream from the pad entity's sink pads.
227 static int imx_media_add_vdev_to_pad(struct imx_media_dev *imxmd,
228 struct imx_media_video_dev *vdev,
229 struct media_pad *srcpad)
231 struct media_entity *entity = srcpad->entity;
232 struct imx_media_pad_vdev *pad_vdev;
233 struct list_head *pad_vdev_list;
234 struct media_link *link;
235 struct v4l2_subdev *sd;
238 /* skip this entity if not a v4l2_subdev */
239 if (!is_media_entity_v4l2_subdev(entity))
242 sd = media_entity_to_v4l2_subdev(entity);
244 pad_vdev_list = to_pad_vdev_list(sd, srcpad->index);
245 if (!pad_vdev_list) {
246 v4l2_warn(&imxmd->v4l2_dev, "%s:%u has no vdev list!\n",
247 entity->name, srcpad->index);
249 * shouldn't happen, but no reason to fail driver load,
250 * just skip this entity.
255 /* just return if we've been here before */
256 list_for_each_entry(pad_vdev, pad_vdev_list, list) {
257 if (pad_vdev->vdev == vdev)
261 dev_dbg(imxmd->md.dev, "adding %s to pad %s:%u\n",
262 vdev->vfd->entity.name, entity->name, srcpad->index);
264 pad_vdev = devm_kzalloc(imxmd->md.dev, sizeof(*pad_vdev), GFP_KERNEL);
268 /* attach this vdev to this pad */
269 pad_vdev->vdev = vdev;
270 list_add_tail(&pad_vdev->list, pad_vdev_list);
272 /* move upstream from this entity's sink pads */
273 for (i = 0; i < entity->num_pads; i++) {
274 struct media_pad *pad = &entity->pads[i];
276 if (!(pad->flags & MEDIA_PAD_FL_SINK))
279 list_for_each_entry(link, &entity->links, list) {
280 if (link->sink != pad)
282 ret = imx_media_add_vdev_to_pad(imxmd, vdev,
293 * For every subdevice, allocate an array of list_head's, one list_head
294 * for each pad, to hold the list of video devices reachable from that
297 static int imx_media_alloc_pad_vdev_lists(struct imx_media_dev *imxmd)
299 struct list_head *vdev_lists;
300 struct media_entity *entity;
301 struct v4l2_subdev *sd;
304 list_for_each_entry(sd, &imxmd->v4l2_dev.subdevs, list) {
305 entity = &sd->entity;
306 vdev_lists = devm_kcalloc(
308 entity->num_pads, sizeof(*vdev_lists),
313 /* attach to the subdev's host private pointer */
314 sd->host_priv = vdev_lists;
316 for (i = 0; i < entity->num_pads; i++)
317 INIT_LIST_HEAD(to_pad_vdev_list(sd, i));
323 /* form the vdev lists in all imx-media source pads */
324 static int imx_media_create_pad_vdev_lists(struct imx_media_dev *imxmd)
326 struct imx_media_video_dev *vdev;
327 struct media_link *link;
330 ret = imx_media_alloc_pad_vdev_lists(imxmd);
334 list_for_each_entry(vdev, &imxmd->vdev_list, list) {
335 link = list_first_entry(&vdev->vfd->entity.links,
336 struct media_link, list);
337 ret = imx_media_add_vdev_to_pad(imxmd, vdev, link->source);
345 /* async subdev complete notifier */
346 static int imx_media_probe_complete(struct v4l2_async_notifier *notifier)
348 struct imx_media_dev *imxmd = notifier2dev(notifier);
351 mutex_lock(&imxmd->mutex);
353 ret = imx_media_create_links(notifier);
357 ret = imx_media_create_pad_vdev_lists(imxmd);
361 ret = v4l2_device_register_subdev_nodes(&imxmd->v4l2_dev);
363 mutex_unlock(&imxmd->mutex);
367 return media_device_register(&imxmd->md);
370 static const struct v4l2_async_notifier_operations imx_media_subdev_ops = {
371 .bound = imx_media_subdev_bound,
372 .complete = imx_media_probe_complete,
376 * adds controls to a video device from an entity subdevice.
377 * Continues upstream from the entity's sink pads.
379 static int imx_media_inherit_controls(struct imx_media_dev *imxmd,
380 struct video_device *vfd,
381 struct media_entity *entity)
385 if (is_media_entity_v4l2_subdev(entity)) {
386 struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
388 dev_dbg(imxmd->md.dev,
389 "adding controls to %s from %s\n",
390 vfd->entity.name, sd->entity.name);
392 ret = v4l2_ctrl_add_handler(vfd->ctrl_handler,
400 for (i = 0; i < entity->num_pads; i++) {
401 struct media_pad *pad, *spad = &entity->pads[i];
403 if (!(spad->flags & MEDIA_PAD_FL_SINK))
406 pad = media_entity_remote_pad(spad);
407 if (!pad || !is_media_entity_v4l2_subdev(pad->entity))
410 ret = imx_media_inherit_controls(imxmd, vfd, pad->entity);
418 static int imx_media_link_notify(struct media_link *link, u32 flags,
419 unsigned int notification)
421 struct media_entity *source = link->source->entity;
422 struct imx_media_pad_vdev *pad_vdev;
423 struct list_head *pad_vdev_list;
424 struct imx_media_dev *imxmd;
425 struct video_device *vfd;
426 struct v4l2_subdev *sd;
429 ret = v4l2_pipeline_link_notify(link, flags, notification);
433 /* don't bother if source is not a subdev */
434 if (!is_media_entity_v4l2_subdev(source))
437 sd = media_entity_to_v4l2_subdev(source);
438 pad_idx = link->source->index;
440 imxmd = dev_get_drvdata(sd->v4l2_dev->dev);
442 pad_vdev_list = to_pad_vdev_list(sd, pad_idx);
443 if (!pad_vdev_list) {
444 /* shouldn't happen, but no reason to fail link setup */
449 * Before disabling a link, reset controls for all video
450 * devices reachable from this link.
452 * After enabling a link, refresh controls for all video
453 * devices reachable from this link.
455 if (notification == MEDIA_DEV_NOTIFY_PRE_LINK_CH &&
456 !(flags & MEDIA_LNK_FL_ENABLED)) {
457 list_for_each_entry(pad_vdev, pad_vdev_list, list) {
458 vfd = pad_vdev->vdev->vfd;
459 dev_dbg(imxmd->md.dev,
460 "reset controls for %s\n",
462 v4l2_ctrl_handler_free(vfd->ctrl_handler);
463 v4l2_ctrl_handler_init(vfd->ctrl_handler, 0);
465 } else if (notification == MEDIA_DEV_NOTIFY_POST_LINK_CH &&
466 (link->flags & MEDIA_LNK_FL_ENABLED)) {
467 list_for_each_entry(pad_vdev, pad_vdev_list, list) {
468 vfd = pad_vdev->vdev->vfd;
469 dev_dbg(imxmd->md.dev,
470 "refresh controls for %s\n",
472 ret = imx_media_inherit_controls(imxmd, vfd,
482 static const struct media_device_ops imx_media_md_ops = {
483 .link_notify = imx_media_link_notify,
486 static int imx_media_probe(struct platform_device *pdev)
488 struct device *dev = &pdev->dev;
489 struct device_node *node = dev->of_node;
490 struct imx_media_async_subdev *imxasd;
491 struct v4l2_async_subdev **subdevs;
492 struct imx_media_dev *imxmd;
493 int num_subdevs, i, ret;
495 imxmd = devm_kzalloc(dev, sizeof(*imxmd), GFP_KERNEL);
499 dev_set_drvdata(dev, imxmd);
501 strlcpy(imxmd->md.model, "imx-media", sizeof(imxmd->md.model));
502 imxmd->md.ops = &imx_media_md_ops;
505 mutex_init(&imxmd->mutex);
507 imxmd->v4l2_dev.mdev = &imxmd->md;
508 strlcpy(imxmd->v4l2_dev.name, "imx-media",
509 sizeof(imxmd->v4l2_dev.name));
511 media_device_init(&imxmd->md);
513 ret = v4l2_device_register(dev, &imxmd->v4l2_dev);
515 v4l2_err(&imxmd->v4l2_dev,
516 "Failed to register v4l2_device: %d\n", ret);
520 dev_set_drvdata(imxmd->v4l2_dev.dev, imxmd);
522 INIT_LIST_HEAD(&imxmd->asd_list);
523 INIT_LIST_HEAD(&imxmd->vdev_list);
525 ret = imx_media_add_of_subdevs(imxmd, node);
527 v4l2_err(&imxmd->v4l2_dev,
528 "add_of_subdevs failed with %d\n", ret);
532 ret = imx_media_add_internal_subdevs(imxmd);
534 v4l2_err(&imxmd->v4l2_dev,
535 "add_internal_subdevs failed with %d\n", ret);
539 num_subdevs = imxmd->subdev_notifier.num_subdevs;
541 /* no subdevs? just bail */
542 if (num_subdevs == 0) {
547 subdevs = devm_kcalloc(imxmd->md.dev, num_subdevs, sizeof(*subdevs),
555 list_for_each_entry(imxasd, &imxmd->asd_list, list)
556 subdevs[i++] = &imxasd->asd;
558 /* prepare the async subdev notifier and register it */
559 imxmd->subdev_notifier.subdevs = subdevs;
560 imxmd->subdev_notifier.ops = &imx_media_subdev_ops;
561 ret = v4l2_async_notifier_register(&imxmd->v4l2_dev,
562 &imxmd->subdev_notifier);
564 v4l2_err(&imxmd->v4l2_dev,
565 "v4l2_async_notifier_register failed with %d\n", ret);
572 imx_media_remove_internal_subdevs(imxmd);
574 v4l2_device_unregister(&imxmd->v4l2_dev);
576 media_device_cleanup(&imxmd->md);
580 static int imx_media_remove(struct platform_device *pdev)
582 struct imx_media_dev *imxmd =
583 (struct imx_media_dev *)platform_get_drvdata(pdev);
585 v4l2_info(&imxmd->v4l2_dev, "Removing imx-media\n");
587 v4l2_async_notifier_unregister(&imxmd->subdev_notifier);
588 imx_media_remove_internal_subdevs(imxmd);
589 v4l2_device_unregister(&imxmd->v4l2_dev);
590 media_device_unregister(&imxmd->md);
591 media_device_cleanup(&imxmd->md);
596 static const struct of_device_id imx_media_dt_ids[] = {
597 { .compatible = "fsl,imx-capture-subsystem" },
600 MODULE_DEVICE_TABLE(of, imx_media_dt_ids);
602 static struct platform_driver imx_media_pdrv = {
603 .probe = imx_media_probe,
604 .remove = imx_media_remove,
607 .of_match_table = imx_media_dt_ids,
611 module_platform_driver(imx_media_pdrv);
613 MODULE_DESCRIPTION("i.MX5/6 v4l2 media controller driver");
614 MODULE_AUTHOR("Steve Longerbeam <steve_longerbeam@mentor.com>");
615 MODULE_LICENSE("GPL");