1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * video stream multiplexer controlled via mux control
5 * Copyright (C) 2013 Pengutronix, Sascha Hauer <kernel@pengutronix.de>
6 * Copyright (C) 2016-2017 Pengutronix, Philipp Zabel <kernel@pengutronix.de>
10 #include <linux/module.h>
11 #include <linux/mutex.h>
12 #include <linux/mux/consumer.h>
14 #include <linux/of_graph.h>
15 #include <linux/platform_device.h>
16 #include <linux/slab.h>
17 #include <media/v4l2-async.h>
18 #include <media/v4l2-device.h>
19 #include <media/v4l2-fwnode.h>
20 #include <media/v4l2-mc.h>
21 #include <media/v4l2-subdev.h>
24 struct v4l2_subdev subdev;
25 struct v4l2_async_notifier notifier;
26 struct media_pad *pads;
27 struct mux_control *mux;
32 static const struct v4l2_mbus_framefmt video_mux_format_mbus_default = {
35 .code = MEDIA_BUS_FMT_Y8_1X8,
36 .field = V4L2_FIELD_NONE,
39 static inline struct video_mux *
40 notifier_to_video_mux(struct v4l2_async_notifier *n)
42 return container_of(n, struct video_mux, notifier);
45 static inline struct video_mux *v4l2_subdev_to_video_mux(struct v4l2_subdev *sd)
47 return container_of(sd, struct video_mux, subdev);
50 static int video_mux_link_setup(struct media_entity *entity,
51 const struct media_pad *local,
52 const struct media_pad *remote, u32 flags)
54 struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
55 struct video_mux *vmux = v4l2_subdev_to_video_mux(sd);
56 u16 source_pad = entity->num_pads - 1;
60 * The mux state is determined by the enabled sink pad link.
61 * Enabling or disabling the source pad link has no effect.
63 if (local->flags & MEDIA_PAD_FL_SOURCE)
66 dev_dbg(sd->dev, "link setup '%s':%d->'%s':%d[%d]",
67 remote->entity->name, remote->index, local->entity->name,
68 local->index, flags & MEDIA_LNK_FL_ENABLED);
70 mutex_lock(&vmux->lock);
72 if (flags & MEDIA_LNK_FL_ENABLED) {
73 struct v4l2_subdev_state *sd_state;
74 struct v4l2_mbus_framefmt *source_mbusformat;
76 if (vmux->active == local->index)
79 if (vmux->active >= 0) {
84 dev_dbg(sd->dev, "setting %d active\n", local->index);
85 ret = mux_control_try_select(vmux->mux, local->index);
88 vmux->active = local->index;
90 /* Propagate the active format to the source */
91 sd_state = v4l2_subdev_lock_and_get_active_state(sd);
92 source_mbusformat = v4l2_subdev_get_pad_format(sd, sd_state,
94 *source_mbusformat = *v4l2_subdev_get_pad_format(sd, sd_state,
96 v4l2_subdev_unlock_state(sd_state);
98 if (vmux->active != local->index)
101 dev_dbg(sd->dev, "going inactive\n");
102 mux_control_deselect(vmux->mux);
107 mutex_unlock(&vmux->lock);
111 static const struct media_entity_operations video_mux_ops = {
112 .link_setup = video_mux_link_setup,
113 .link_validate = v4l2_subdev_link_validate,
114 .get_fwnode_pad = v4l2_subdev_get_fwnode_pad_1_to_1,
117 static int video_mux_s_stream(struct v4l2_subdev *sd, int enable)
119 struct video_mux *vmux = v4l2_subdev_to_video_mux(sd);
120 struct v4l2_subdev *upstream_sd;
121 struct media_pad *pad;
123 if (vmux->active == -1) {
124 dev_err(sd->dev, "Can not start streaming on inactive mux\n");
128 pad = media_pad_remote_pad_first(&sd->entity.pads[vmux->active]);
130 dev_err(sd->dev, "Failed to find remote source pad\n");
134 if (!is_media_entity_v4l2_subdev(pad->entity)) {
135 dev_err(sd->dev, "Upstream entity is not a v4l2 subdev\n");
139 upstream_sd = media_entity_to_v4l2_subdev(pad->entity);
141 return v4l2_subdev_call(upstream_sd, video, s_stream, enable);
144 static const struct v4l2_subdev_video_ops video_mux_subdev_video_ops = {
145 .s_stream = video_mux_s_stream,
148 static int video_mux_set_format(struct v4l2_subdev *sd,
149 struct v4l2_subdev_state *sd_state,
150 struct v4l2_subdev_format *sdformat)
152 struct video_mux *vmux = v4l2_subdev_to_video_mux(sd);
153 struct v4l2_mbus_framefmt *mbusformat, *source_mbusformat;
154 struct media_pad *pad = &vmux->pads[sdformat->pad];
155 u16 source_pad = sd->entity.num_pads - 1;
157 mbusformat = v4l2_subdev_get_pad_format(sd, sd_state, sdformat->pad);
161 source_mbusformat = v4l2_subdev_get_pad_format(sd, sd_state, source_pad);
162 if (!source_mbusformat)
165 /* No size limitations except V4L2 compliance requirements */
166 v4l_bound_align_image(&sdformat->format.width, 1, 65536, 0,
167 &sdformat->format.height, 1, 65536, 0, 0);
169 /* All formats except LVDS and vendor specific formats are acceptable */
170 switch (sdformat->format.code) {
171 case MEDIA_BUS_FMT_RGB444_1X12:
172 case MEDIA_BUS_FMT_RGB444_2X8_PADHI_BE:
173 case MEDIA_BUS_FMT_RGB444_2X8_PADHI_LE:
174 case MEDIA_BUS_FMT_RGB555_2X8_PADHI_BE:
175 case MEDIA_BUS_FMT_RGB555_2X8_PADHI_LE:
176 case MEDIA_BUS_FMT_RGB565_1X16:
177 case MEDIA_BUS_FMT_BGR565_2X8_BE:
178 case MEDIA_BUS_FMT_BGR565_2X8_LE:
179 case MEDIA_BUS_FMT_RGB565_2X8_BE:
180 case MEDIA_BUS_FMT_RGB565_2X8_LE:
181 case MEDIA_BUS_FMT_RGB666_1X18:
182 case MEDIA_BUS_FMT_RBG888_1X24:
183 case MEDIA_BUS_FMT_RGB666_1X24_CPADHI:
184 case MEDIA_BUS_FMT_BGR888_1X24:
185 case MEDIA_BUS_FMT_GBR888_1X24:
186 case MEDIA_BUS_FMT_RGB888_1X24:
187 case MEDIA_BUS_FMT_RGB888_2X12_BE:
188 case MEDIA_BUS_FMT_RGB888_2X12_LE:
189 case MEDIA_BUS_FMT_ARGB8888_1X32:
190 case MEDIA_BUS_FMT_RGB888_1X32_PADHI:
191 case MEDIA_BUS_FMT_RGB101010_1X30:
192 case MEDIA_BUS_FMT_RGB121212_1X36:
193 case MEDIA_BUS_FMT_RGB161616_1X48:
194 case MEDIA_BUS_FMT_Y8_1X8:
195 case MEDIA_BUS_FMT_UV8_1X8:
196 case MEDIA_BUS_FMT_UYVY8_1_5X8:
197 case MEDIA_BUS_FMT_VYUY8_1_5X8:
198 case MEDIA_BUS_FMT_YUYV8_1_5X8:
199 case MEDIA_BUS_FMT_YVYU8_1_5X8:
200 case MEDIA_BUS_FMT_UYVY8_2X8:
201 case MEDIA_BUS_FMT_VYUY8_2X8:
202 case MEDIA_BUS_FMT_YUYV8_2X8:
203 case MEDIA_BUS_FMT_YVYU8_2X8:
204 case MEDIA_BUS_FMT_Y10_1X10:
205 case MEDIA_BUS_FMT_UYVY10_2X10:
206 case MEDIA_BUS_FMT_VYUY10_2X10:
207 case MEDIA_BUS_FMT_YUYV10_2X10:
208 case MEDIA_BUS_FMT_YVYU10_2X10:
209 case MEDIA_BUS_FMT_Y12_1X12:
210 case MEDIA_BUS_FMT_UYVY12_2X12:
211 case MEDIA_BUS_FMT_VYUY12_2X12:
212 case MEDIA_BUS_FMT_YUYV12_2X12:
213 case MEDIA_BUS_FMT_YVYU12_2X12:
214 case MEDIA_BUS_FMT_UYVY8_1X16:
215 case MEDIA_BUS_FMT_VYUY8_1X16:
216 case MEDIA_BUS_FMT_YUYV8_1X16:
217 case MEDIA_BUS_FMT_YVYU8_1X16:
218 case MEDIA_BUS_FMT_YDYUYDYV8_1X16:
219 case MEDIA_BUS_FMT_UYVY10_1X20:
220 case MEDIA_BUS_FMT_VYUY10_1X20:
221 case MEDIA_BUS_FMT_YUYV10_1X20:
222 case MEDIA_BUS_FMT_YVYU10_1X20:
223 case MEDIA_BUS_FMT_VUY8_1X24:
224 case MEDIA_BUS_FMT_YUV8_1X24:
225 case MEDIA_BUS_FMT_UYYVYY8_0_5X24:
226 case MEDIA_BUS_FMT_UYVY12_1X24:
227 case MEDIA_BUS_FMT_VYUY12_1X24:
228 case MEDIA_BUS_FMT_YUYV12_1X24:
229 case MEDIA_BUS_FMT_YVYU12_1X24:
230 case MEDIA_BUS_FMT_YUV10_1X30:
231 case MEDIA_BUS_FMT_UYYVYY10_0_5X30:
232 case MEDIA_BUS_FMT_AYUV8_1X32:
233 case MEDIA_BUS_FMT_UYYVYY12_0_5X36:
234 case MEDIA_BUS_FMT_YUV12_1X36:
235 case MEDIA_BUS_FMT_YUV16_1X48:
236 case MEDIA_BUS_FMT_UYYVYY16_0_5X48:
237 case MEDIA_BUS_FMT_JPEG_1X8:
238 case MEDIA_BUS_FMT_AHSV8888_1X32:
239 case MEDIA_BUS_FMT_SBGGR8_1X8:
240 case MEDIA_BUS_FMT_SGBRG8_1X8:
241 case MEDIA_BUS_FMT_SGRBG8_1X8:
242 case MEDIA_BUS_FMT_SRGGB8_1X8:
243 case MEDIA_BUS_FMT_SBGGR10_1X10:
244 case MEDIA_BUS_FMT_SGBRG10_1X10:
245 case MEDIA_BUS_FMT_SGRBG10_1X10:
246 case MEDIA_BUS_FMT_SRGGB10_1X10:
247 case MEDIA_BUS_FMT_SBGGR12_1X12:
248 case MEDIA_BUS_FMT_SGBRG12_1X12:
249 case MEDIA_BUS_FMT_SGRBG12_1X12:
250 case MEDIA_BUS_FMT_SRGGB12_1X12:
251 case MEDIA_BUS_FMT_SBGGR14_1X14:
252 case MEDIA_BUS_FMT_SGBRG14_1X14:
253 case MEDIA_BUS_FMT_SGRBG14_1X14:
254 case MEDIA_BUS_FMT_SRGGB14_1X14:
255 case MEDIA_BUS_FMT_SBGGR16_1X16:
256 case MEDIA_BUS_FMT_SGBRG16_1X16:
257 case MEDIA_BUS_FMT_SGRBG16_1X16:
258 case MEDIA_BUS_FMT_SRGGB16_1X16:
261 sdformat->format.code = MEDIA_BUS_FMT_Y8_1X8;
264 if (sdformat->format.field == V4L2_FIELD_ANY)
265 sdformat->format.field = V4L2_FIELD_NONE;
267 mutex_lock(&vmux->lock);
269 /* Source pad mirrors active sink pad, no limitations on sink pads */
270 if ((pad->flags & MEDIA_PAD_FL_SOURCE) && vmux->active >= 0)
271 sdformat->format = *v4l2_subdev_get_pad_format(sd, sd_state,
274 *mbusformat = sdformat->format;
276 /* Propagate the format from an active sink to source */
277 if ((pad->flags & MEDIA_PAD_FL_SINK) && (pad->index == vmux->active))
278 *source_mbusformat = sdformat->format;
280 mutex_unlock(&vmux->lock);
285 static int video_mux_init_cfg(struct v4l2_subdev *sd,
286 struct v4l2_subdev_state *sd_state)
288 struct video_mux *vmux = v4l2_subdev_to_video_mux(sd);
289 struct v4l2_mbus_framefmt *mbusformat;
292 mutex_lock(&vmux->lock);
294 for (i = 0; i < sd->entity.num_pads; i++) {
295 mbusformat = v4l2_subdev_get_pad_format(sd, sd_state, i);
296 *mbusformat = video_mux_format_mbus_default;
299 mutex_unlock(&vmux->lock);
304 static const struct v4l2_subdev_pad_ops video_mux_pad_ops = {
305 .init_cfg = video_mux_init_cfg,
306 .get_fmt = v4l2_subdev_get_fmt,
307 .set_fmt = video_mux_set_format,
310 static const struct v4l2_subdev_ops video_mux_subdev_ops = {
311 .pad = &video_mux_pad_ops,
312 .video = &video_mux_subdev_video_ops,
315 static int video_mux_notify_bound(struct v4l2_async_notifier *notifier,
316 struct v4l2_subdev *sd,
317 struct v4l2_async_connection *asd)
319 struct video_mux *vmux = notifier_to_video_mux(notifier);
321 return v4l2_create_fwnode_links(sd, &vmux->subdev);
324 static const struct v4l2_async_notifier_operations video_mux_notify_ops = {
325 .bound = video_mux_notify_bound,
328 static int video_mux_async_register(struct video_mux *vmux,
329 unsigned int num_input_pads)
334 v4l2_async_nf_init(&vmux->notifier);
336 for (i = 0; i < num_input_pads; i++) {
337 struct v4l2_async_connection *asd;
338 struct fwnode_handle *ep, *remote_ep;
340 ep = fwnode_graph_get_endpoint_by_id(
341 dev_fwnode(vmux->subdev.dev), i, 0,
342 FWNODE_GRAPH_ENDPOINT_NEXT);
346 /* Skip dangling endpoints for backwards compatibility */
347 remote_ep = fwnode_graph_get_remote_endpoint(ep);
349 fwnode_handle_put(ep);
352 fwnode_handle_put(remote_ep);
354 asd = v4l2_async_nf_add_fwnode_remote(&vmux->notifier, ep,
355 struct v4l2_async_connection);
357 fwnode_handle_put(ep);
361 /* OK if asd already exists */
367 vmux->notifier.ops = &video_mux_notify_ops;
369 ret = v4l2_async_subdev_nf_register(&vmux->subdev, &vmux->notifier);
373 ret = v4l2_async_register_subdev(&vmux->subdev);
375 goto err_nf_unregister;
380 v4l2_async_nf_unregister(&vmux->notifier);
382 v4l2_async_nf_cleanup(&vmux->notifier);
386 static int video_mux_probe(struct platform_device *pdev)
388 struct device_node *np = pdev->dev.of_node;
389 struct device *dev = &pdev->dev;
390 struct device_node *ep;
391 struct video_mux *vmux;
392 unsigned int num_pads = 0;
396 vmux = devm_kzalloc(dev, sizeof(*vmux), GFP_KERNEL);
400 platform_set_drvdata(pdev, vmux);
402 v4l2_subdev_init(&vmux->subdev, &video_mux_subdev_ops);
403 snprintf(vmux->subdev.name, sizeof(vmux->subdev.name), "%pOFn", np);
404 vmux->subdev.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
405 vmux->subdev.dev = dev;
408 * The largest numbered port is the output port. It determines
409 * total number of pads.
411 for_each_endpoint_of_node(np, ep) {
412 struct of_endpoint endpoint;
414 of_graph_parse_endpoint(ep, &endpoint);
415 num_pads = max(num_pads, endpoint.port + 1);
419 dev_err(dev, "Not enough ports %d\n", num_pads);
423 vmux->mux = devm_mux_control_get(dev, NULL);
424 if (IS_ERR(vmux->mux)) {
425 ret = PTR_ERR(vmux->mux);
426 return dev_err_probe(dev, ret, "Failed to get mux\n");
429 mutex_init(&vmux->lock);
431 vmux->pads = devm_kcalloc(dev, num_pads, sizeof(*vmux->pads),
436 for (i = 0; i < num_pads; i++)
437 vmux->pads[i].flags = (i < num_pads - 1) ? MEDIA_PAD_FL_SINK
438 : MEDIA_PAD_FL_SOURCE;
440 vmux->subdev.entity.function = MEDIA_ENT_F_VID_MUX;
441 ret = media_entity_pads_init(&vmux->subdev.entity, num_pads,
446 vmux->subdev.entity.ops = &video_mux_ops;
448 ret = v4l2_subdev_init_finalize(&vmux->subdev);
450 goto err_entity_cleanup;
452 ret = video_mux_async_register(vmux, num_pads - 1);
454 goto err_subdev_cleanup;
459 v4l2_subdev_cleanup(&vmux->subdev);
461 media_entity_cleanup(&vmux->subdev.entity);
465 static void video_mux_remove(struct platform_device *pdev)
467 struct video_mux *vmux = platform_get_drvdata(pdev);
468 struct v4l2_subdev *sd = &vmux->subdev;
470 v4l2_async_nf_unregister(&vmux->notifier);
471 v4l2_async_nf_cleanup(&vmux->notifier);
472 v4l2_async_unregister_subdev(sd);
473 v4l2_subdev_cleanup(sd);
474 media_entity_cleanup(&sd->entity);
477 static const struct of_device_id video_mux_dt_ids[] = {
478 { .compatible = "video-mux", },
481 MODULE_DEVICE_TABLE(of, video_mux_dt_ids);
483 static struct platform_driver video_mux_driver = {
484 .probe = video_mux_probe,
485 .remove_new = video_mux_remove,
487 .of_match_table = video_mux_dt_ids,
492 module_platform_driver(video_mux_driver);
494 MODULE_DESCRIPTION("video stream multiplexer");
495 MODULE_AUTHOR("Sascha Hauer, Pengutronix");
496 MODULE_AUTHOR("Philipp Zabel, Pengutronix");
497 MODULE_LICENSE("GPL");