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 v4l2_mbus_framefmt
*format_mbus
;
28 struct mux_control
*mux
;
33 static const struct v4l2_mbus_framefmt video_mux_format_mbus_default
= {
36 .code
= MEDIA_BUS_FMT_Y8_1X8
,
37 .field
= V4L2_FIELD_NONE
,
40 static inline struct video_mux
*
41 notifier_to_video_mux(struct v4l2_async_notifier
*n
)
43 return container_of(n
, struct video_mux
, notifier
);
46 static inline struct video_mux
*v4l2_subdev_to_video_mux(struct v4l2_subdev
*sd
)
48 return container_of(sd
, struct video_mux
, subdev
);
51 static int video_mux_link_setup(struct media_entity
*entity
,
52 const struct media_pad
*local
,
53 const struct media_pad
*remote
, u32 flags
)
55 struct v4l2_subdev
*sd
= media_entity_to_v4l2_subdev(entity
);
56 struct video_mux
*vmux
= v4l2_subdev_to_video_mux(sd
);
57 u16 source_pad
= entity
->num_pads
- 1;
61 * The mux state is determined by the enabled sink pad link.
62 * Enabling or disabling the source pad link has no effect.
64 if (local
->flags
& MEDIA_PAD_FL_SOURCE
)
67 dev_dbg(sd
->dev
, "link setup '%s':%d->'%s':%d[%d]",
68 remote
->entity
->name
, remote
->index
, local
->entity
->name
,
69 local
->index
, flags
& MEDIA_LNK_FL_ENABLED
);
71 mutex_lock(&vmux
->lock
);
73 if (flags
& MEDIA_LNK_FL_ENABLED
) {
74 if (vmux
->active
== local
->index
)
77 if (vmux
->active
>= 0) {
82 dev_dbg(sd
->dev
, "setting %d active\n", local
->index
);
83 ret
= mux_control_try_select(vmux
->mux
, local
->index
);
86 vmux
->active
= local
->index
;
88 /* Propagate the active format to the source */
89 vmux
->format_mbus
[source_pad
] = vmux
->format_mbus
[vmux
->active
];
91 if (vmux
->active
!= local
->index
)
94 dev_dbg(sd
->dev
, "going inactive\n");
95 mux_control_deselect(vmux
->mux
);
100 mutex_unlock(&vmux
->lock
);
104 static const struct media_entity_operations video_mux_ops
= {
105 .link_setup
= video_mux_link_setup
,
106 .link_validate
= v4l2_subdev_link_validate
,
107 .get_fwnode_pad
= v4l2_subdev_get_fwnode_pad_1_to_1
,
110 static int video_mux_s_stream(struct v4l2_subdev
*sd
, int enable
)
112 struct video_mux
*vmux
= v4l2_subdev_to_video_mux(sd
);
113 struct v4l2_subdev
*upstream_sd
;
114 struct media_pad
*pad
;
116 if (vmux
->active
== -1) {
117 dev_err(sd
->dev
, "Can not start streaming on inactive mux\n");
121 pad
= media_entity_remote_pad(&sd
->entity
.pads
[vmux
->active
]);
123 dev_err(sd
->dev
, "Failed to find remote source pad\n");
127 if (!is_media_entity_v4l2_subdev(pad
->entity
)) {
128 dev_err(sd
->dev
, "Upstream entity is not a v4l2 subdev\n");
132 upstream_sd
= media_entity_to_v4l2_subdev(pad
->entity
);
134 return v4l2_subdev_call(upstream_sd
, video
, s_stream
, enable
);
137 static const struct v4l2_subdev_video_ops video_mux_subdev_video_ops
= {
138 .s_stream
= video_mux_s_stream
,
141 static struct v4l2_mbus_framefmt
*
142 __video_mux_get_pad_format(struct v4l2_subdev
*sd
,
143 struct v4l2_subdev_pad_config
*cfg
,
144 unsigned int pad
, u32 which
)
146 struct video_mux
*vmux
= v4l2_subdev_to_video_mux(sd
);
149 case V4L2_SUBDEV_FORMAT_TRY
:
150 return v4l2_subdev_get_try_format(sd
, cfg
, pad
);
151 case V4L2_SUBDEV_FORMAT_ACTIVE
:
152 return &vmux
->format_mbus
[pad
];
158 static int video_mux_get_format(struct v4l2_subdev
*sd
,
159 struct v4l2_subdev_pad_config
*cfg
,
160 struct v4l2_subdev_format
*sdformat
)
162 struct video_mux
*vmux
= v4l2_subdev_to_video_mux(sd
);
164 mutex_lock(&vmux
->lock
);
166 sdformat
->format
= *__video_mux_get_pad_format(sd
, cfg
, sdformat
->pad
,
169 mutex_unlock(&vmux
->lock
);
174 static int video_mux_set_format(struct v4l2_subdev
*sd
,
175 struct v4l2_subdev_pad_config
*cfg
,
176 struct v4l2_subdev_format
*sdformat
)
178 struct video_mux
*vmux
= v4l2_subdev_to_video_mux(sd
);
179 struct v4l2_mbus_framefmt
*mbusformat
, *source_mbusformat
;
180 struct media_pad
*pad
= &vmux
->pads
[sdformat
->pad
];
181 u16 source_pad
= sd
->entity
.num_pads
- 1;
183 mbusformat
= __video_mux_get_pad_format(sd
, cfg
, sdformat
->pad
,
188 source_mbusformat
= __video_mux_get_pad_format(sd
, cfg
, source_pad
,
190 if (!source_mbusformat
)
193 /* No size limitations except V4L2 compliance requirements */
194 v4l_bound_align_image(&sdformat
->format
.width
, 1, 65536, 0,
195 &sdformat
->format
.height
, 1, 65536, 0, 0);
197 /* All formats except LVDS and vendor specific formats are acceptable */
198 switch (sdformat
->format
.code
) {
199 case MEDIA_BUS_FMT_RGB444_1X12
:
200 case MEDIA_BUS_FMT_RGB444_2X8_PADHI_BE
:
201 case MEDIA_BUS_FMT_RGB444_2X8_PADHI_LE
:
202 case MEDIA_BUS_FMT_RGB555_2X8_PADHI_BE
:
203 case MEDIA_BUS_FMT_RGB555_2X8_PADHI_LE
:
204 case MEDIA_BUS_FMT_RGB565_1X16
:
205 case MEDIA_BUS_FMT_BGR565_2X8_BE
:
206 case MEDIA_BUS_FMT_BGR565_2X8_LE
:
207 case MEDIA_BUS_FMT_RGB565_2X8_BE
:
208 case MEDIA_BUS_FMT_RGB565_2X8_LE
:
209 case MEDIA_BUS_FMT_RGB666_1X18
:
210 case MEDIA_BUS_FMT_RBG888_1X24
:
211 case MEDIA_BUS_FMT_RGB666_1X24_CPADHI
:
212 case MEDIA_BUS_FMT_BGR888_1X24
:
213 case MEDIA_BUS_FMT_GBR888_1X24
:
214 case MEDIA_BUS_FMT_RGB888_1X24
:
215 case MEDIA_BUS_FMT_RGB888_2X12_BE
:
216 case MEDIA_BUS_FMT_RGB888_2X12_LE
:
217 case MEDIA_BUS_FMT_ARGB8888_1X32
:
218 case MEDIA_BUS_FMT_RGB888_1X32_PADHI
:
219 case MEDIA_BUS_FMT_RGB101010_1X30
:
220 case MEDIA_BUS_FMT_RGB121212_1X36
:
221 case MEDIA_BUS_FMT_RGB161616_1X48
:
222 case MEDIA_BUS_FMT_Y8_1X8
:
223 case MEDIA_BUS_FMT_UV8_1X8
:
224 case MEDIA_BUS_FMT_UYVY8_1_5X8
:
225 case MEDIA_BUS_FMT_VYUY8_1_5X8
:
226 case MEDIA_BUS_FMT_YUYV8_1_5X8
:
227 case MEDIA_BUS_FMT_YVYU8_1_5X8
:
228 case MEDIA_BUS_FMT_UYVY8_2X8
:
229 case MEDIA_BUS_FMT_VYUY8_2X8
:
230 case MEDIA_BUS_FMT_YUYV8_2X8
:
231 case MEDIA_BUS_FMT_YVYU8_2X8
:
232 case MEDIA_BUS_FMT_Y10_1X10
:
233 case MEDIA_BUS_FMT_UYVY10_2X10
:
234 case MEDIA_BUS_FMT_VYUY10_2X10
:
235 case MEDIA_BUS_FMT_YUYV10_2X10
:
236 case MEDIA_BUS_FMT_YVYU10_2X10
:
237 case MEDIA_BUS_FMT_Y12_1X12
:
238 case MEDIA_BUS_FMT_UYVY12_2X12
:
239 case MEDIA_BUS_FMT_VYUY12_2X12
:
240 case MEDIA_BUS_FMT_YUYV12_2X12
:
241 case MEDIA_BUS_FMT_YVYU12_2X12
:
242 case MEDIA_BUS_FMT_UYVY8_1X16
:
243 case MEDIA_BUS_FMT_VYUY8_1X16
:
244 case MEDIA_BUS_FMT_YUYV8_1X16
:
245 case MEDIA_BUS_FMT_YVYU8_1X16
:
246 case MEDIA_BUS_FMT_YDYUYDYV8_1X16
:
247 case MEDIA_BUS_FMT_UYVY10_1X20
:
248 case MEDIA_BUS_FMT_VYUY10_1X20
:
249 case MEDIA_BUS_FMT_YUYV10_1X20
:
250 case MEDIA_BUS_FMT_YVYU10_1X20
:
251 case MEDIA_BUS_FMT_VUY8_1X24
:
252 case MEDIA_BUS_FMT_YUV8_1X24
:
253 case MEDIA_BUS_FMT_UYYVYY8_0_5X24
:
254 case MEDIA_BUS_FMT_UYVY12_1X24
:
255 case MEDIA_BUS_FMT_VYUY12_1X24
:
256 case MEDIA_BUS_FMT_YUYV12_1X24
:
257 case MEDIA_BUS_FMT_YVYU12_1X24
:
258 case MEDIA_BUS_FMT_YUV10_1X30
:
259 case MEDIA_BUS_FMT_UYYVYY10_0_5X30
:
260 case MEDIA_BUS_FMT_AYUV8_1X32
:
261 case MEDIA_BUS_FMT_UYYVYY12_0_5X36
:
262 case MEDIA_BUS_FMT_YUV12_1X36
:
263 case MEDIA_BUS_FMT_YUV16_1X48
:
264 case MEDIA_BUS_FMT_UYYVYY16_0_5X48
:
265 case MEDIA_BUS_FMT_JPEG_1X8
:
266 case MEDIA_BUS_FMT_AHSV8888_1X32
:
267 case MEDIA_BUS_FMT_SBGGR8_1X8
:
268 case MEDIA_BUS_FMT_SGBRG8_1X8
:
269 case MEDIA_BUS_FMT_SGRBG8_1X8
:
270 case MEDIA_BUS_FMT_SRGGB8_1X8
:
271 case MEDIA_BUS_FMT_SBGGR10_1X10
:
272 case MEDIA_BUS_FMT_SGBRG10_1X10
:
273 case MEDIA_BUS_FMT_SGRBG10_1X10
:
274 case MEDIA_BUS_FMT_SRGGB10_1X10
:
275 case MEDIA_BUS_FMT_SBGGR12_1X12
:
276 case MEDIA_BUS_FMT_SGBRG12_1X12
:
277 case MEDIA_BUS_FMT_SGRBG12_1X12
:
278 case MEDIA_BUS_FMT_SRGGB12_1X12
:
279 case MEDIA_BUS_FMT_SBGGR14_1X14
:
280 case MEDIA_BUS_FMT_SGBRG14_1X14
:
281 case MEDIA_BUS_FMT_SGRBG14_1X14
:
282 case MEDIA_BUS_FMT_SRGGB14_1X14
:
283 case MEDIA_BUS_FMT_SBGGR16_1X16
:
284 case MEDIA_BUS_FMT_SGBRG16_1X16
:
285 case MEDIA_BUS_FMT_SGRBG16_1X16
:
286 case MEDIA_BUS_FMT_SRGGB16_1X16
:
289 sdformat
->format
.code
= MEDIA_BUS_FMT_Y8_1X8
;
292 if (sdformat
->format
.field
== V4L2_FIELD_ANY
)
293 sdformat
->format
.field
= V4L2_FIELD_NONE
;
295 mutex_lock(&vmux
->lock
);
297 /* Source pad mirrors active sink pad, no limitations on sink pads */
298 if ((pad
->flags
& MEDIA_PAD_FL_SOURCE
) && vmux
->active
>= 0)
299 sdformat
->format
= vmux
->format_mbus
[vmux
->active
];
301 *mbusformat
= sdformat
->format
;
303 /* Propagate the format from an active sink to source */
304 if ((pad
->flags
& MEDIA_PAD_FL_SINK
) && (pad
->index
== vmux
->active
))
305 *source_mbusformat
= sdformat
->format
;
307 mutex_unlock(&vmux
->lock
);
312 static int video_mux_init_cfg(struct v4l2_subdev
*sd
,
313 struct v4l2_subdev_pad_config
*cfg
)
315 struct video_mux
*vmux
= v4l2_subdev_to_video_mux(sd
);
316 struct v4l2_mbus_framefmt
*mbusformat
;
319 mutex_lock(&vmux
->lock
);
321 for (i
= 0; i
< sd
->entity
.num_pads
; i
++) {
322 mbusformat
= v4l2_subdev_get_try_format(sd
, cfg
, i
);
323 *mbusformat
= video_mux_format_mbus_default
;
326 mutex_unlock(&vmux
->lock
);
331 static const struct v4l2_subdev_pad_ops video_mux_pad_ops
= {
332 .init_cfg
= video_mux_init_cfg
,
333 .get_fmt
= video_mux_get_format
,
334 .set_fmt
= video_mux_set_format
,
337 static const struct v4l2_subdev_ops video_mux_subdev_ops
= {
338 .pad
= &video_mux_pad_ops
,
339 .video
= &video_mux_subdev_video_ops
,
342 static int video_mux_notify_bound(struct v4l2_async_notifier
*notifier
,
343 struct v4l2_subdev
*sd
,
344 struct v4l2_async_subdev
*asd
)
346 struct video_mux
*vmux
= notifier_to_video_mux(notifier
);
348 return v4l2_create_fwnode_links(sd
, &vmux
->subdev
);
351 static const struct v4l2_async_notifier_operations video_mux_notify_ops
= {
352 .bound
= video_mux_notify_bound
,
355 static int video_mux_async_register(struct video_mux
*vmux
,
356 unsigned int num_input_pads
)
361 v4l2_async_notifier_init(&vmux
->notifier
);
363 for (i
= 0; i
< num_input_pads
; i
++) {
364 struct v4l2_async_subdev
*asd
;
365 struct fwnode_handle
*ep
;
367 ep
= fwnode_graph_get_endpoint_by_id(
368 dev_fwnode(vmux
->subdev
.dev
), i
, 0,
369 FWNODE_GRAPH_ENDPOINT_NEXT
);
373 asd
= kzalloc(sizeof(*asd
), GFP_KERNEL
);
375 fwnode_handle_put(ep
);
379 ret
= v4l2_async_notifier_add_fwnode_remote_subdev(
380 &vmux
->notifier
, ep
, asd
);
382 fwnode_handle_put(ep
);
386 /* OK if asd already exists */
392 vmux
->notifier
.ops
= &video_mux_notify_ops
;
394 ret
= v4l2_async_subdev_notifier_register(&vmux
->subdev
,
399 return v4l2_async_register_subdev(&vmux
->subdev
);
402 static int video_mux_probe(struct platform_device
*pdev
)
404 struct device_node
*np
= pdev
->dev
.of_node
;
405 struct device
*dev
= &pdev
->dev
;
406 struct device_node
*ep
;
407 struct video_mux
*vmux
;
408 unsigned int num_pads
= 0;
412 vmux
= devm_kzalloc(dev
, sizeof(*vmux
), GFP_KERNEL
);
416 platform_set_drvdata(pdev
, vmux
);
418 v4l2_subdev_init(&vmux
->subdev
, &video_mux_subdev_ops
);
419 snprintf(vmux
->subdev
.name
, sizeof(vmux
->subdev
.name
), "%pOFn", np
);
420 vmux
->subdev
.flags
|= V4L2_SUBDEV_FL_HAS_DEVNODE
;
421 vmux
->subdev
.dev
= dev
;
424 * The largest numbered port is the output port. It determines
425 * total number of pads.
427 for_each_endpoint_of_node(np
, ep
) {
428 struct of_endpoint endpoint
;
430 of_graph_parse_endpoint(ep
, &endpoint
);
431 num_pads
= max(num_pads
, endpoint
.port
+ 1);
435 dev_err(dev
, "Not enough ports %d\n", num_pads
);
439 vmux
->mux
= devm_mux_control_get(dev
, NULL
);
440 if (IS_ERR(vmux
->mux
)) {
441 ret
= PTR_ERR(vmux
->mux
);
442 if (ret
!= -EPROBE_DEFER
)
443 dev_err(dev
, "Failed to get mux: %d\n", ret
);
447 mutex_init(&vmux
->lock
);
449 vmux
->pads
= devm_kcalloc(dev
, num_pads
, sizeof(*vmux
->pads
),
454 vmux
->format_mbus
= devm_kcalloc(dev
, num_pads
,
455 sizeof(*vmux
->format_mbus
),
457 if (!vmux
->format_mbus
)
460 for (i
= 0; i
< num_pads
; i
++) {
461 vmux
->pads
[i
].flags
= (i
< num_pads
- 1) ? MEDIA_PAD_FL_SINK
462 : MEDIA_PAD_FL_SOURCE
;
463 vmux
->format_mbus
[i
] = video_mux_format_mbus_default
;
466 vmux
->subdev
.entity
.function
= MEDIA_ENT_F_VID_MUX
;
467 ret
= media_entity_pads_init(&vmux
->subdev
.entity
, num_pads
,
472 vmux
->subdev
.entity
.ops
= &video_mux_ops
;
474 ret
= video_mux_async_register(vmux
, num_pads
- 1);
476 v4l2_async_notifier_unregister(&vmux
->notifier
);
477 v4l2_async_notifier_cleanup(&vmux
->notifier
);
483 static int video_mux_remove(struct platform_device
*pdev
)
485 struct video_mux
*vmux
= platform_get_drvdata(pdev
);
486 struct v4l2_subdev
*sd
= &vmux
->subdev
;
488 v4l2_async_notifier_unregister(&vmux
->notifier
);
489 v4l2_async_notifier_cleanup(&vmux
->notifier
);
490 v4l2_async_unregister_subdev(sd
);
491 media_entity_cleanup(&sd
->entity
);
496 static const struct of_device_id video_mux_dt_ids
[] = {
497 { .compatible
= "video-mux", },
500 MODULE_DEVICE_TABLE(of
, video_mux_dt_ids
);
502 static struct platform_driver video_mux_driver
= {
503 .probe
= video_mux_probe
,
504 .remove
= video_mux_remove
,
506 .of_match_table
= video_mux_dt_ids
,
511 module_platform_driver(video_mux_driver
);
513 MODULE_DESCRIPTION("video stream multiplexer");
514 MODULE_AUTHOR("Sascha Hauer, Pengutronix");
515 MODULE_AUTHOR("Philipp Zabel, Pengutronix");
516 MODULE_LICENSE("GPL");