1 // SPDX-License-Identifier: GPL-2.0
3 * V4L2 Media Controller Driver for Freescale common i.MX5/6/7 SOC
5 * Copyright (c) 2019 Linaro Ltd
6 * Copyright (c) 2016 Mentor Graphics Inc.
9 #include <linux/of_graph.h>
10 #include <linux/of_platform.h>
11 #include <media/v4l2-ctrls.h>
12 #include <media/v4l2-event.h>
13 #include <media/v4l2-ioctl.h>
14 #include <media/v4l2-mc.h>
15 #include "imx-media.h"
17 static inline struct imx_media_dev *notifier2dev(struct v4l2_async_notifier *n)
19 return container_of(n, struct imx_media_dev, notifier);
22 /* async subdev bound notifier */
23 static int imx_media_subdev_bound(struct v4l2_async_notifier *notifier,
24 struct v4l2_subdev *sd,
25 struct v4l2_async_subdev *asd)
27 struct imx_media_dev *imxmd = notifier2dev(notifier);
29 dev_dbg(imxmd->md.dev, "subdev %s bound\n", sd->name);
35 * Create the missing media links from the CSI-2 receiver.
36 * Called after all async subdevs have bound.
38 static void imx_media_create_csi2_links(struct imx_media_dev *imxmd)
40 struct v4l2_subdev *sd, *csi2 = NULL;
42 list_for_each_entry(sd, &imxmd->v4l2_dev.subdevs, list) {
43 if (sd->grp_id == IMX_MEDIA_GRP_ID_CSI2) {
51 list_for_each_entry(sd, &imxmd->v4l2_dev.subdevs, list) {
52 /* skip if not a CSI or a CSI mux */
53 if (!(sd->grp_id & IMX_MEDIA_GRP_ID_IPU_CSI) &&
54 !(sd->grp_id & IMX_MEDIA_GRP_ID_CSI) &&
55 !(sd->grp_id & IMX_MEDIA_GRP_ID_CSI_MUX))
58 v4l2_create_fwnode_links(csi2, sd);
63 * adds given video device to given imx-media source pad vdev list.
64 * Continues upstream from the pad entity's sink pads.
66 static int imx_media_add_vdev_to_pad(struct imx_media_dev *imxmd,
67 struct imx_media_video_dev *vdev,
68 struct media_pad *srcpad)
70 struct media_entity *entity = srcpad->entity;
71 struct imx_media_pad_vdev *pad_vdev;
72 struct list_head *pad_vdev_list;
73 struct media_link *link;
74 struct v4l2_subdev *sd;
77 /* skip this entity if not a v4l2_subdev */
78 if (!is_media_entity_v4l2_subdev(entity))
81 sd = media_entity_to_v4l2_subdev(entity);
83 pad_vdev_list = to_pad_vdev_list(sd, srcpad->index);
85 v4l2_warn(&imxmd->v4l2_dev, "%s:%u has no vdev list!\n",
86 entity->name, srcpad->index);
88 * shouldn't happen, but no reason to fail driver load,
89 * just skip this entity.
94 /* just return if we've been here before */
95 list_for_each_entry(pad_vdev, pad_vdev_list, list) {
96 if (pad_vdev->vdev == vdev)
100 dev_dbg(imxmd->md.dev, "adding %s to pad %s:%u\n",
101 vdev->vfd->entity.name, entity->name, srcpad->index);
103 pad_vdev = devm_kzalloc(imxmd->md.dev, sizeof(*pad_vdev), GFP_KERNEL);
107 /* attach this vdev to this pad */
108 pad_vdev->vdev = vdev;
109 list_add_tail(&pad_vdev->list, pad_vdev_list);
111 /* move upstream from this entity's sink pads */
112 for (i = 0; i < entity->num_pads; i++) {
113 struct media_pad *pad = &entity->pads[i];
115 if (!(pad->flags & MEDIA_PAD_FL_SINK))
118 list_for_each_entry(link, &entity->links, list) {
119 if (link->sink != pad)
121 ret = imx_media_add_vdev_to_pad(imxmd, vdev,
132 * For every subdevice, allocate an array of list_head's, one list_head
133 * for each pad, to hold the list of video devices reachable from that
136 static int imx_media_alloc_pad_vdev_lists(struct imx_media_dev *imxmd)
138 struct list_head *vdev_lists;
139 struct media_entity *entity;
140 struct v4l2_subdev *sd;
143 list_for_each_entry(sd, &imxmd->v4l2_dev.subdevs, list) {
144 entity = &sd->entity;
145 vdev_lists = devm_kcalloc(imxmd->md.dev,
146 entity->num_pads, sizeof(*vdev_lists),
151 /* attach to the subdev's host private pointer */
152 sd->host_priv = vdev_lists;
154 for (i = 0; i < entity->num_pads; i++)
155 INIT_LIST_HEAD(to_pad_vdev_list(sd, i));
161 /* form the vdev lists in all imx-media source pads */
162 static int imx_media_create_pad_vdev_lists(struct imx_media_dev *imxmd)
164 struct imx_media_video_dev *vdev;
165 struct media_link *link;
168 ret = imx_media_alloc_pad_vdev_lists(imxmd);
172 list_for_each_entry(vdev, &imxmd->vdev_list, list) {
173 link = list_first_entry(&vdev->vfd->entity.links,
174 struct media_link, list);
175 ret = imx_media_add_vdev_to_pad(imxmd, vdev, link->source);
183 /* async subdev complete notifier */
184 int imx_media_probe_complete(struct v4l2_async_notifier *notifier)
186 struct imx_media_dev *imxmd = notifier2dev(notifier);
189 mutex_lock(&imxmd->mutex);
191 imx_media_create_csi2_links(imxmd);
193 ret = imx_media_create_pad_vdev_lists(imxmd);
197 ret = v4l2_device_register_subdev_nodes(&imxmd->v4l2_dev);
199 mutex_unlock(&imxmd->mutex);
203 return media_device_register(&imxmd->md);
205 EXPORT_SYMBOL_GPL(imx_media_probe_complete);
208 * adds controls to a video device from an entity subdevice.
209 * Continues upstream from the entity's sink pads.
211 static int imx_media_inherit_controls(struct imx_media_dev *imxmd,
212 struct video_device *vfd,
213 struct media_entity *entity)
217 if (is_media_entity_v4l2_subdev(entity)) {
218 struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
220 dev_dbg(imxmd->md.dev,
221 "adding controls to %s from %s\n",
222 vfd->entity.name, sd->entity.name);
224 ret = v4l2_ctrl_add_handler(vfd->ctrl_handler,
232 for (i = 0; i < entity->num_pads; i++) {
233 struct media_pad *pad, *spad = &entity->pads[i];
235 if (!(spad->flags & MEDIA_PAD_FL_SINK))
238 pad = media_entity_remote_pad(spad);
239 if (!pad || !is_media_entity_v4l2_subdev(pad->entity))
242 ret = imx_media_inherit_controls(imxmd, vfd, pad->entity);
250 static int imx_media_link_notify(struct media_link *link, u32 flags,
251 unsigned int notification)
253 struct imx_media_dev *imxmd = container_of(link->graph_obj.mdev,
254 struct imx_media_dev, md);
255 struct media_entity *source = link->source->entity;
256 struct imx_media_pad_vdev *pad_vdev;
257 struct list_head *pad_vdev_list;
258 struct video_device *vfd;
259 struct v4l2_subdev *sd;
262 ret = v4l2_pipeline_link_notify(link, flags, notification);
266 /* don't bother if source is not a subdev */
267 if (!is_media_entity_v4l2_subdev(source))
270 sd = media_entity_to_v4l2_subdev(source);
271 pad_idx = link->source->index;
273 pad_vdev_list = to_pad_vdev_list(sd, pad_idx);
274 if (!pad_vdev_list) {
275 /* nothing to do if source sd has no pad vdev list */
280 * Before disabling a link, reset controls for all video
281 * devices reachable from this link.
283 * After enabling a link, refresh controls for all video
284 * devices reachable from this link.
286 if (notification == MEDIA_DEV_NOTIFY_PRE_LINK_CH &&
287 !(flags & MEDIA_LNK_FL_ENABLED)) {
288 list_for_each_entry(pad_vdev, pad_vdev_list, list) {
289 vfd = pad_vdev->vdev->vfd;
290 if (!vfd->ctrl_handler)
292 dev_dbg(imxmd->md.dev,
293 "reset controls for %s\n",
295 v4l2_ctrl_handler_free(vfd->ctrl_handler);
296 v4l2_ctrl_handler_init(vfd->ctrl_handler, 0);
298 } else if (notification == MEDIA_DEV_NOTIFY_POST_LINK_CH &&
299 (link->flags & MEDIA_LNK_FL_ENABLED)) {
300 list_for_each_entry(pad_vdev, pad_vdev_list, list) {
301 vfd = pad_vdev->vdev->vfd;
302 if (!vfd->ctrl_handler)
304 dev_dbg(imxmd->md.dev,
305 "refresh controls for %s\n",
307 ret = imx_media_inherit_controls(imxmd, vfd,
317 static void imx_media_notify(struct v4l2_subdev *sd, unsigned int notification,
320 struct media_entity *entity = &sd->entity;
323 if (notification != V4L2_DEVICE_NOTIFY_EVENT)
326 for (i = 0; i < entity->num_pads; i++) {
327 struct media_pad *pad = &entity->pads[i];
328 struct imx_media_pad_vdev *pad_vdev;
329 struct list_head *pad_vdev_list;
331 pad_vdev_list = to_pad_vdev_list(sd, pad->index);
334 list_for_each_entry(pad_vdev, pad_vdev_list, list)
335 v4l2_event_queue(pad_vdev->vdev->vfd, arg);
339 static const struct v4l2_async_notifier_operations imx_media_notifier_ops = {
340 .bound = imx_media_subdev_bound,
341 .complete = imx_media_probe_complete,
344 static const struct media_device_ops imx_media_md_ops = {
345 .link_notify = imx_media_link_notify,
348 struct imx_media_dev *imx_media_dev_init(struct device *dev,
349 const struct media_device_ops *ops)
351 struct imx_media_dev *imxmd;
354 imxmd = devm_kzalloc(dev, sizeof(*imxmd), GFP_KERNEL);
356 return ERR_PTR(-ENOMEM);
358 dev_set_drvdata(dev, imxmd);
360 strscpy(imxmd->md.model, "imx-media", sizeof(imxmd->md.model));
361 imxmd->md.ops = ops ? ops : &imx_media_md_ops;
364 mutex_init(&imxmd->mutex);
366 imxmd->v4l2_dev.mdev = &imxmd->md;
367 imxmd->v4l2_dev.notify = imx_media_notify;
368 strscpy(imxmd->v4l2_dev.name, "imx-media",
369 sizeof(imxmd->v4l2_dev.name));
370 snprintf(imxmd->md.bus_info, sizeof(imxmd->md.bus_info),
371 "platform:%s", dev_name(imxmd->md.dev));
373 media_device_init(&imxmd->md);
375 ret = v4l2_device_register(dev, &imxmd->v4l2_dev);
377 v4l2_err(&imxmd->v4l2_dev,
378 "Failed to register v4l2_device: %d\n", ret);
382 INIT_LIST_HEAD(&imxmd->vdev_list);
384 v4l2_async_nf_init(&imxmd->notifier);
389 media_device_cleanup(&imxmd->md);
393 EXPORT_SYMBOL_GPL(imx_media_dev_init);
395 int imx_media_dev_notifier_register(struct imx_media_dev *imxmd,
396 const struct v4l2_async_notifier_operations *ops)
400 /* no subdevs? just bail */
401 if (list_empty(&imxmd->notifier.asd_list)) {
402 v4l2_err(&imxmd->v4l2_dev, "no subdevs\n");
406 /* prepare the async subdev notifier and register it */
407 imxmd->notifier.ops = ops ? ops : &imx_media_notifier_ops;
408 ret = v4l2_async_nf_register(&imxmd->v4l2_dev, &imxmd->notifier);
410 v4l2_err(&imxmd->v4l2_dev,
411 "v4l2_async_nf_register failed with %d\n", ret);
417 EXPORT_SYMBOL_GPL(imx_media_dev_notifier_register);