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)) {
93 dev_dbg(imxmd->md.dev, "%s: already added %pOFn\n",
96 dev_dbg(imxmd->md.dev, "%s: already added %s\n",
102 imxasd = devm_kzalloc(imxmd->md.dev, sizeof(*imxasd), GFP_KERNEL);
110 asd->match_type = V4L2_ASYNC_MATCH_FWNODE;
111 asd->match.fwnode = fwnode;
112 dev_dbg(imxmd->md.dev, "%s: added %pOFn, match type FWNODE\n",
115 asd->match_type = V4L2_ASYNC_MATCH_DEVNAME;
116 asd->match.device_name = devname;
118 dev_dbg(imxmd->md.dev, "%s: added %s, match type DEVNAME\n",
122 list_add_tail(&imxasd->list, &imxmd->asd_list);
124 imxmd->subdev_notifier.num_subdevs++;
127 mutex_unlock(&imxmd->mutex);
132 * get IPU from this CSI and add it to the list of IPUs
133 * the media driver will control.
135 static int imx_media_get_ipu(struct imx_media_dev *imxmd,
136 struct v4l2_subdev *csi_sd)
141 ipu = dev_get_drvdata(csi_sd->dev->parent);
143 v4l2_err(&imxmd->v4l2_dev,
144 "CSI %s has no parent IPU!\n", csi_sd->name);
148 ipu_id = ipu_get_num(ipu);
150 v4l2_err(&imxmd->v4l2_dev, "invalid IPU id %d!\n", ipu_id);
154 if (!imxmd->ipu[ipu_id])
155 imxmd->ipu[ipu_id] = ipu;
160 /* async subdev bound notifier */
161 static int imx_media_subdev_bound(struct v4l2_async_notifier *notifier,
162 struct v4l2_subdev *sd,
163 struct v4l2_async_subdev *asd)
165 struct imx_media_dev *imxmd = notifier2dev(notifier);
168 mutex_lock(&imxmd->mutex);
170 if (sd->grp_id & IMX_MEDIA_GRP_ID_CSI) {
171 ret = imx_media_get_ipu(imxmd, sd);
176 v4l2_info(&imxmd->v4l2_dev, "subdev %s bound\n", sd->name);
178 mutex_unlock(&imxmd->mutex);
183 * create the media links for all subdevs that registered async.
184 * Called after all async subdevs have bound.
186 static int imx_media_create_links(struct v4l2_async_notifier *notifier)
188 struct imx_media_dev *imxmd = notifier2dev(notifier);
189 struct v4l2_subdev *sd;
193 * Only links are created between subdevices that are known
194 * to the async notifier. If there are other non-async subdevices,
195 * they were created internally by some subdevice (smiapp is one
196 * example). In those cases it is expected the subdevice is
197 * responsible for creating those internal links.
199 list_for_each_entry(sd, ¬ifier->done, async_list) {
200 switch (sd->grp_id) {
201 case IMX_MEDIA_GRP_ID_VDIC:
202 case IMX_MEDIA_GRP_ID_IC_PRP:
203 case IMX_MEDIA_GRP_ID_IC_PRPENC:
204 case IMX_MEDIA_GRP_ID_IC_PRPVF:
205 case IMX_MEDIA_GRP_ID_CSI0:
206 case IMX_MEDIA_GRP_ID_CSI1:
207 ret = imx_media_create_internal_links(imxmd, sd);
211 * the CSIs straddle between the external and the IPU
212 * internal entities, so create the external links
213 * to the CSI sink pads.
215 if (sd->grp_id & IMX_MEDIA_GRP_ID_CSI)
216 imx_media_create_csi_of_links(imxmd, sd);
219 /* this is an external fwnode subdev */
220 imx_media_create_of_links(imxmd, sd);
229 * adds given video device to given imx-media source pad vdev list.
230 * Continues upstream from the pad entity's sink pads.
232 static int imx_media_add_vdev_to_pad(struct imx_media_dev *imxmd,
233 struct imx_media_video_dev *vdev,
234 struct media_pad *srcpad)
236 struct media_entity *entity = srcpad->entity;
237 struct imx_media_pad_vdev *pad_vdev;
238 struct list_head *pad_vdev_list;
239 struct media_link *link;
240 struct v4l2_subdev *sd;
243 /* skip this entity if not a v4l2_subdev */
244 if (!is_media_entity_v4l2_subdev(entity))
247 sd = media_entity_to_v4l2_subdev(entity);
249 pad_vdev_list = to_pad_vdev_list(sd, srcpad->index);
250 if (!pad_vdev_list) {
251 v4l2_warn(&imxmd->v4l2_dev, "%s:%u has no vdev list!\n",
252 entity->name, srcpad->index);
254 * shouldn't happen, but no reason to fail driver load,
255 * just skip this entity.
260 /* just return if we've been here before */
261 list_for_each_entry(pad_vdev, pad_vdev_list, list) {
262 if (pad_vdev->vdev == vdev)
266 dev_dbg(imxmd->md.dev, "adding %s to pad %s:%u\n",
267 vdev->vfd->entity.name, entity->name, srcpad->index);
269 pad_vdev = devm_kzalloc(imxmd->md.dev, sizeof(*pad_vdev), GFP_KERNEL);
273 /* attach this vdev to this pad */
274 pad_vdev->vdev = vdev;
275 list_add_tail(&pad_vdev->list, pad_vdev_list);
277 /* move upstream from this entity's sink pads */
278 for (i = 0; i < entity->num_pads; i++) {
279 struct media_pad *pad = &entity->pads[i];
281 if (!(pad->flags & MEDIA_PAD_FL_SINK))
284 list_for_each_entry(link, &entity->links, list) {
285 if (link->sink != pad)
287 ret = imx_media_add_vdev_to_pad(imxmd, vdev,
298 * For every subdevice, allocate an array of list_head's, one list_head
299 * for each pad, to hold the list of video devices reachable from that
302 static int imx_media_alloc_pad_vdev_lists(struct imx_media_dev *imxmd)
304 struct list_head *vdev_lists;
305 struct media_entity *entity;
306 struct v4l2_subdev *sd;
309 list_for_each_entry(sd, &imxmd->v4l2_dev.subdevs, list) {
310 entity = &sd->entity;
311 vdev_lists = devm_kcalloc(
313 entity->num_pads, sizeof(*vdev_lists),
318 /* attach to the subdev's host private pointer */
319 sd->host_priv = vdev_lists;
321 for (i = 0; i < entity->num_pads; i++)
322 INIT_LIST_HEAD(to_pad_vdev_list(sd, i));
328 /* form the vdev lists in all imx-media source pads */
329 static int imx_media_create_pad_vdev_lists(struct imx_media_dev *imxmd)
331 struct imx_media_video_dev *vdev;
332 struct media_link *link;
335 ret = imx_media_alloc_pad_vdev_lists(imxmd);
339 list_for_each_entry(vdev, &imxmd->vdev_list, list) {
340 link = list_first_entry(&vdev->vfd->entity.links,
341 struct media_link, list);
342 ret = imx_media_add_vdev_to_pad(imxmd, vdev, link->source);
350 /* async subdev complete notifier */
351 static int imx_media_probe_complete(struct v4l2_async_notifier *notifier)
353 struct imx_media_dev *imxmd = notifier2dev(notifier);
356 mutex_lock(&imxmd->mutex);
358 ret = imx_media_create_links(notifier);
362 ret = imx_media_create_pad_vdev_lists(imxmd);
366 ret = v4l2_device_register_subdev_nodes(&imxmd->v4l2_dev);
368 mutex_unlock(&imxmd->mutex);
372 return media_device_register(&imxmd->md);
375 static const struct v4l2_async_notifier_operations imx_media_subdev_ops = {
376 .bound = imx_media_subdev_bound,
377 .complete = imx_media_probe_complete,
381 * adds controls to a video device from an entity subdevice.
382 * Continues upstream from the entity's sink pads.
384 static int imx_media_inherit_controls(struct imx_media_dev *imxmd,
385 struct video_device *vfd,
386 struct media_entity *entity)
390 if (is_media_entity_v4l2_subdev(entity)) {
391 struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
393 dev_dbg(imxmd->md.dev,
394 "adding controls to %s from %s\n",
395 vfd->entity.name, sd->entity.name);
397 ret = v4l2_ctrl_add_handler(vfd->ctrl_handler,
405 for (i = 0; i < entity->num_pads; i++) {
406 struct media_pad *pad, *spad = &entity->pads[i];
408 if (!(spad->flags & MEDIA_PAD_FL_SINK))
411 pad = media_entity_remote_pad(spad);
412 if (!pad || !is_media_entity_v4l2_subdev(pad->entity))
415 ret = imx_media_inherit_controls(imxmd, vfd, pad->entity);
423 static int imx_media_link_notify(struct media_link *link, u32 flags,
424 unsigned int notification)
426 struct media_entity *source = link->source->entity;
427 struct imx_media_pad_vdev *pad_vdev;
428 struct list_head *pad_vdev_list;
429 struct imx_media_dev *imxmd;
430 struct video_device *vfd;
431 struct v4l2_subdev *sd;
434 ret = v4l2_pipeline_link_notify(link, flags, notification);
438 /* don't bother if source is not a subdev */
439 if (!is_media_entity_v4l2_subdev(source))
442 sd = media_entity_to_v4l2_subdev(source);
443 pad_idx = link->source->index;
445 imxmd = dev_get_drvdata(sd->v4l2_dev->dev);
447 pad_vdev_list = to_pad_vdev_list(sd, pad_idx);
448 if (!pad_vdev_list) {
449 /* shouldn't happen, but no reason to fail link setup */
454 * Before disabling a link, reset controls for all video
455 * devices reachable from this link.
457 * After enabling a link, refresh controls for all video
458 * devices reachable from this link.
460 if (notification == MEDIA_DEV_NOTIFY_PRE_LINK_CH &&
461 !(flags & MEDIA_LNK_FL_ENABLED)) {
462 list_for_each_entry(pad_vdev, pad_vdev_list, list) {
463 vfd = pad_vdev->vdev->vfd;
464 dev_dbg(imxmd->md.dev,
465 "reset controls for %s\n",
467 v4l2_ctrl_handler_free(vfd->ctrl_handler);
468 v4l2_ctrl_handler_init(vfd->ctrl_handler, 0);
470 } else if (notification == MEDIA_DEV_NOTIFY_POST_LINK_CH &&
471 (link->flags & MEDIA_LNK_FL_ENABLED)) {
472 list_for_each_entry(pad_vdev, pad_vdev_list, list) {
473 vfd = pad_vdev->vdev->vfd;
474 dev_dbg(imxmd->md.dev,
475 "refresh controls for %s\n",
477 ret = imx_media_inherit_controls(imxmd, vfd,
487 static const struct media_device_ops imx_media_md_ops = {
488 .link_notify = imx_media_link_notify,
491 static int imx_media_probe(struct platform_device *pdev)
493 struct device *dev = &pdev->dev;
494 struct device_node *node = dev->of_node;
495 struct imx_media_async_subdev *imxasd;
496 struct v4l2_async_subdev **subdevs;
497 struct imx_media_dev *imxmd;
498 int num_subdevs, i, ret;
500 imxmd = devm_kzalloc(dev, sizeof(*imxmd), GFP_KERNEL);
504 dev_set_drvdata(dev, imxmd);
506 strlcpy(imxmd->md.model, "imx-media", sizeof(imxmd->md.model));
507 imxmd->md.ops = &imx_media_md_ops;
510 mutex_init(&imxmd->mutex);
512 imxmd->v4l2_dev.mdev = &imxmd->md;
513 strlcpy(imxmd->v4l2_dev.name, "imx-media",
514 sizeof(imxmd->v4l2_dev.name));
516 media_device_init(&imxmd->md);
518 ret = v4l2_device_register(dev, &imxmd->v4l2_dev);
520 v4l2_err(&imxmd->v4l2_dev,
521 "Failed to register v4l2_device: %d\n", ret);
525 dev_set_drvdata(imxmd->v4l2_dev.dev, imxmd);
527 INIT_LIST_HEAD(&imxmd->asd_list);
528 INIT_LIST_HEAD(&imxmd->vdev_list);
530 ret = imx_media_add_of_subdevs(imxmd, node);
532 v4l2_err(&imxmd->v4l2_dev,
533 "add_of_subdevs failed with %d\n", ret);
537 ret = imx_media_add_internal_subdevs(imxmd);
539 v4l2_err(&imxmd->v4l2_dev,
540 "add_internal_subdevs failed with %d\n", ret);
544 num_subdevs = imxmd->subdev_notifier.num_subdevs;
546 /* no subdevs? just bail */
547 if (num_subdevs == 0) {
552 subdevs = devm_kcalloc(imxmd->md.dev, num_subdevs, sizeof(*subdevs),
560 list_for_each_entry(imxasd, &imxmd->asd_list, list)
561 subdevs[i++] = &imxasd->asd;
563 /* prepare the async subdev notifier and register it */
564 imxmd->subdev_notifier.subdevs = subdevs;
565 imxmd->subdev_notifier.ops = &imx_media_subdev_ops;
566 ret = v4l2_async_notifier_register(&imxmd->v4l2_dev,
567 &imxmd->subdev_notifier);
569 v4l2_err(&imxmd->v4l2_dev,
570 "v4l2_async_notifier_register failed with %d\n", ret);
577 imx_media_remove_internal_subdevs(imxmd);
579 v4l2_device_unregister(&imxmd->v4l2_dev);
581 media_device_cleanup(&imxmd->md);
585 static int imx_media_remove(struct platform_device *pdev)
587 struct imx_media_dev *imxmd =
588 (struct imx_media_dev *)platform_get_drvdata(pdev);
590 v4l2_info(&imxmd->v4l2_dev, "Removing imx-media\n");
592 v4l2_async_notifier_unregister(&imxmd->subdev_notifier);
593 imx_media_remove_internal_subdevs(imxmd);
594 v4l2_device_unregister(&imxmd->v4l2_dev);
595 media_device_unregister(&imxmd->md);
596 media_device_cleanup(&imxmd->md);
601 static const struct of_device_id imx_media_dt_ids[] = {
602 { .compatible = "fsl,imx-capture-subsystem" },
605 MODULE_DEVICE_TABLE(of, imx_media_dt_ids);
607 static struct platform_driver imx_media_pdrv = {
608 .probe = imx_media_probe,
609 .remove = imx_media_remove,
612 .of_match_table = imx_media_dt_ids,
616 module_platform_driver(imx_media_pdrv);
618 MODULE_DESCRIPTION("i.MX5/6 v4l2 media controller driver");
619 MODULE_AUTHOR("Steve Longerbeam <steve_longerbeam@mentor.com>");
620 MODULE_LICENSE("GPL");