2 * video stream multiplexer controlled via mux control
4 * Copyright (C) 2013 Pengutronix, Sascha Hauer <kernel@pengutronix.de>
5 * Copyright (C) 2016-2017 Pengutronix, Philipp Zabel <kernel@pengutronix.de>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
17 #include <linux/err.h>
18 #include <linux/module.h>
19 #include <linux/mutex.h>
20 #include <linux/mux/consumer.h>
22 #include <linux/of_graph.h>
23 #include <linux/platform_device.h>
24 #include <media/v4l2-async.h>
25 #include <media/v4l2-device.h>
26 #include <media/v4l2-subdev.h>
29 struct v4l2_subdev subdev
;
30 struct media_pad
*pads
;
31 struct v4l2_mbus_framefmt
*format_mbus
;
32 struct mux_control
*mux
;
37 static const struct v4l2_mbus_framefmt video_mux_format_mbus_default
= {
40 .code
= MEDIA_BUS_FMT_Y8_1X8
,
41 .field
= V4L2_FIELD_NONE
,
44 static inline struct video_mux
*v4l2_subdev_to_video_mux(struct v4l2_subdev
*sd
)
46 return container_of(sd
, struct video_mux
, subdev
);
49 static int video_mux_link_setup(struct media_entity
*entity
,
50 const struct media_pad
*local
,
51 const struct media_pad
*remote
, u32 flags
)
53 struct v4l2_subdev
*sd
= media_entity_to_v4l2_subdev(entity
);
54 struct video_mux
*vmux
= v4l2_subdev_to_video_mux(sd
);
55 u16 source_pad
= entity
->num_pads
- 1;
59 * The mux state is determined by the enabled sink pad link.
60 * Enabling or disabling the source pad link has no effect.
62 if (local
->flags
& MEDIA_PAD_FL_SOURCE
)
65 dev_dbg(sd
->dev
, "link setup '%s':%d->'%s':%d[%d]",
66 remote
->entity
->name
, remote
->index
, local
->entity
->name
,
67 local
->index
, flags
& MEDIA_LNK_FL_ENABLED
);
69 mutex_lock(&vmux
->lock
);
71 if (flags
& MEDIA_LNK_FL_ENABLED
) {
72 if (vmux
->active
== local
->index
)
75 if (vmux
->active
>= 0) {
80 dev_dbg(sd
->dev
, "setting %d active\n", local
->index
);
81 ret
= mux_control_try_select(vmux
->mux
, local
->index
);
84 vmux
->active
= local
->index
;
86 /* Propagate the active format to the source */
87 vmux
->format_mbus
[source_pad
] = vmux
->format_mbus
[vmux
->active
];
89 if (vmux
->active
!= local
->index
)
92 dev_dbg(sd
->dev
, "going inactive\n");
93 mux_control_deselect(vmux
->mux
);
98 mutex_unlock(&vmux
->lock
);
102 static const struct media_entity_operations video_mux_ops
= {
103 .link_setup
= video_mux_link_setup
,
104 .link_validate
= v4l2_subdev_link_validate
,
107 static int video_mux_s_stream(struct v4l2_subdev
*sd
, int enable
)
109 struct video_mux
*vmux
= v4l2_subdev_to_video_mux(sd
);
110 struct v4l2_subdev
*upstream_sd
;
111 struct media_pad
*pad
;
113 if (vmux
->active
== -1) {
114 dev_err(sd
->dev
, "Can not start streaming on inactive mux\n");
118 pad
= media_entity_remote_pad(&sd
->entity
.pads
[vmux
->active
]);
120 dev_err(sd
->dev
, "Failed to find remote source pad\n");
124 if (!is_media_entity_v4l2_subdev(pad
->entity
)) {
125 dev_err(sd
->dev
, "Upstream entity is not a v4l2 subdev\n");
129 upstream_sd
= media_entity_to_v4l2_subdev(pad
->entity
);
131 return v4l2_subdev_call(upstream_sd
, video
, s_stream
, enable
);
134 static const struct v4l2_subdev_video_ops video_mux_subdev_video_ops
= {
135 .s_stream
= video_mux_s_stream
,
138 static struct v4l2_mbus_framefmt
*
139 __video_mux_get_pad_format(struct v4l2_subdev
*sd
,
140 struct v4l2_subdev_pad_config
*cfg
,
141 unsigned int pad
, u32 which
)
143 struct video_mux
*vmux
= v4l2_subdev_to_video_mux(sd
);
146 case V4L2_SUBDEV_FORMAT_TRY
:
147 return v4l2_subdev_get_try_format(sd
, cfg
, pad
);
148 case V4L2_SUBDEV_FORMAT_ACTIVE
:
149 return &vmux
->format_mbus
[pad
];
155 static int video_mux_get_format(struct v4l2_subdev
*sd
,
156 struct v4l2_subdev_pad_config
*cfg
,
157 struct v4l2_subdev_format
*sdformat
)
159 struct video_mux
*vmux
= v4l2_subdev_to_video_mux(sd
);
161 mutex_lock(&vmux
->lock
);
163 sdformat
->format
= *__video_mux_get_pad_format(sd
, cfg
, sdformat
->pad
,
166 mutex_unlock(&vmux
->lock
);
171 static int video_mux_set_format(struct v4l2_subdev
*sd
,
172 struct v4l2_subdev_pad_config
*cfg
,
173 struct v4l2_subdev_format
*sdformat
)
175 struct video_mux
*vmux
= v4l2_subdev_to_video_mux(sd
);
176 struct v4l2_mbus_framefmt
*mbusformat
, *source_mbusformat
;
177 struct media_pad
*pad
= &vmux
->pads
[sdformat
->pad
];
178 u16 source_pad
= sd
->entity
.num_pads
- 1;
180 mbusformat
= __video_mux_get_pad_format(sd
, cfg
, sdformat
->pad
,
185 source_mbusformat
= __video_mux_get_pad_format(sd
, cfg
, source_pad
,
187 if (!source_mbusformat
)
190 /* No size limitations except V4L2 compliance requirements */
191 v4l_bound_align_image(&sdformat
->format
.width
, 1, 65536, 0,
192 &sdformat
->format
.height
, 1, 65536, 0, 0);
194 /* All formats except LVDS and vendor specific formats are acceptable */
195 switch (sdformat
->format
.code
) {
196 case MEDIA_BUS_FMT_RGB444_1X12
:
197 case MEDIA_BUS_FMT_RGB444_2X8_PADHI_BE
:
198 case MEDIA_BUS_FMT_RGB444_2X8_PADHI_LE
:
199 case MEDIA_BUS_FMT_RGB555_2X8_PADHI_BE
:
200 case MEDIA_BUS_FMT_RGB555_2X8_PADHI_LE
:
201 case MEDIA_BUS_FMT_RGB565_1X16
:
202 case MEDIA_BUS_FMT_BGR565_2X8_BE
:
203 case MEDIA_BUS_FMT_BGR565_2X8_LE
:
204 case MEDIA_BUS_FMT_RGB565_2X8_BE
:
205 case MEDIA_BUS_FMT_RGB565_2X8_LE
:
206 case MEDIA_BUS_FMT_RGB666_1X18
:
207 case MEDIA_BUS_FMT_RBG888_1X24
:
208 case MEDIA_BUS_FMT_RGB666_1X24_CPADHI
:
209 case MEDIA_BUS_FMT_BGR888_1X24
:
210 case MEDIA_BUS_FMT_GBR888_1X24
:
211 case MEDIA_BUS_FMT_RGB888_1X24
:
212 case MEDIA_BUS_FMT_RGB888_2X12_BE
:
213 case MEDIA_BUS_FMT_RGB888_2X12_LE
:
214 case MEDIA_BUS_FMT_ARGB8888_1X32
:
215 case MEDIA_BUS_FMT_RGB888_1X32_PADHI
:
216 case MEDIA_BUS_FMT_RGB101010_1X30
:
217 case MEDIA_BUS_FMT_RGB121212_1X36
:
218 case MEDIA_BUS_FMT_RGB161616_1X48
:
219 case MEDIA_BUS_FMT_Y8_1X8
:
220 case MEDIA_BUS_FMT_UV8_1X8
:
221 case MEDIA_BUS_FMT_UYVY8_1_5X8
:
222 case MEDIA_BUS_FMT_VYUY8_1_5X8
:
223 case MEDIA_BUS_FMT_YUYV8_1_5X8
:
224 case MEDIA_BUS_FMT_YVYU8_1_5X8
:
225 case MEDIA_BUS_FMT_UYVY8_2X8
:
226 case MEDIA_BUS_FMT_VYUY8_2X8
:
227 case MEDIA_BUS_FMT_YUYV8_2X8
:
228 case MEDIA_BUS_FMT_YVYU8_2X8
:
229 case MEDIA_BUS_FMT_Y10_1X10
:
230 case MEDIA_BUS_FMT_UYVY10_2X10
:
231 case MEDIA_BUS_FMT_VYUY10_2X10
:
232 case MEDIA_BUS_FMT_YUYV10_2X10
:
233 case MEDIA_BUS_FMT_YVYU10_2X10
:
234 case MEDIA_BUS_FMT_Y12_1X12
:
235 case MEDIA_BUS_FMT_UYVY12_2X12
:
236 case MEDIA_BUS_FMT_VYUY12_2X12
:
237 case MEDIA_BUS_FMT_YUYV12_2X12
:
238 case MEDIA_BUS_FMT_YVYU12_2X12
:
239 case MEDIA_BUS_FMT_UYVY8_1X16
:
240 case MEDIA_BUS_FMT_VYUY8_1X16
:
241 case MEDIA_BUS_FMT_YUYV8_1X16
:
242 case MEDIA_BUS_FMT_YVYU8_1X16
:
243 case MEDIA_BUS_FMT_YDYUYDYV8_1X16
:
244 case MEDIA_BUS_FMT_UYVY10_1X20
:
245 case MEDIA_BUS_FMT_VYUY10_1X20
:
246 case MEDIA_BUS_FMT_YUYV10_1X20
:
247 case MEDIA_BUS_FMT_YVYU10_1X20
:
248 case MEDIA_BUS_FMT_VUY8_1X24
:
249 case MEDIA_BUS_FMT_YUV8_1X24
:
250 case MEDIA_BUS_FMT_UYYVYY8_0_5X24
:
251 case MEDIA_BUS_FMT_UYVY12_1X24
:
252 case MEDIA_BUS_FMT_VYUY12_1X24
:
253 case MEDIA_BUS_FMT_YUYV12_1X24
:
254 case MEDIA_BUS_FMT_YVYU12_1X24
:
255 case MEDIA_BUS_FMT_YUV10_1X30
:
256 case MEDIA_BUS_FMT_UYYVYY10_0_5X30
:
257 case MEDIA_BUS_FMT_AYUV8_1X32
:
258 case MEDIA_BUS_FMT_UYYVYY12_0_5X36
:
259 case MEDIA_BUS_FMT_YUV12_1X36
:
260 case MEDIA_BUS_FMT_YUV16_1X48
:
261 case MEDIA_BUS_FMT_UYYVYY16_0_5X48
:
262 case MEDIA_BUS_FMT_JPEG_1X8
:
263 case MEDIA_BUS_FMT_AHSV8888_1X32
:
266 sdformat
->format
.code
= MEDIA_BUS_FMT_Y8_1X8
;
269 if (sdformat
->format
.field
== V4L2_FIELD_ANY
)
270 sdformat
->format
.field
= V4L2_FIELD_NONE
;
272 mutex_lock(&vmux
->lock
);
274 /* Source pad mirrors active sink pad, no limitations on sink pads */
275 if ((pad
->flags
& MEDIA_PAD_FL_SOURCE
) && vmux
->active
>= 0)
276 sdformat
->format
= vmux
->format_mbus
[vmux
->active
];
278 *mbusformat
= sdformat
->format
;
280 /* Propagate the format from an active sink to source */
281 if ((pad
->flags
& MEDIA_PAD_FL_SINK
) && (pad
->index
== vmux
->active
))
282 *source_mbusformat
= sdformat
->format
;
284 mutex_unlock(&vmux
->lock
);
289 static int video_mux_init_cfg(struct v4l2_subdev
*sd
,
290 struct v4l2_subdev_pad_config
*cfg
)
292 struct video_mux
*vmux
= v4l2_subdev_to_video_mux(sd
);
293 struct v4l2_mbus_framefmt
*mbusformat
;
296 mutex_lock(&vmux
->lock
);
298 for (i
= 0; i
< sd
->entity
.num_pads
; i
++) {
299 mbusformat
= v4l2_subdev_get_try_format(sd
, cfg
, i
);
300 *mbusformat
= video_mux_format_mbus_default
;
303 mutex_unlock(&vmux
->lock
);
308 static const struct v4l2_subdev_pad_ops video_mux_pad_ops
= {
309 .init_cfg
= video_mux_init_cfg
,
310 .get_fmt
= video_mux_get_format
,
311 .set_fmt
= video_mux_set_format
,
314 static const struct v4l2_subdev_ops video_mux_subdev_ops
= {
315 .pad
= &video_mux_pad_ops
,
316 .video
= &video_mux_subdev_video_ops
,
319 static int video_mux_probe(struct platform_device
*pdev
)
321 struct device_node
*np
= pdev
->dev
.of_node
;
322 struct device
*dev
= &pdev
->dev
;
323 struct device_node
*ep
;
324 struct video_mux
*vmux
;
325 unsigned int num_pads
= 0;
329 vmux
= devm_kzalloc(dev
, sizeof(*vmux
), GFP_KERNEL
);
333 platform_set_drvdata(pdev
, vmux
);
335 v4l2_subdev_init(&vmux
->subdev
, &video_mux_subdev_ops
);
336 snprintf(vmux
->subdev
.name
, sizeof(vmux
->subdev
.name
), "%s", np
->name
);
337 vmux
->subdev
.flags
|= V4L2_SUBDEV_FL_HAS_DEVNODE
;
338 vmux
->subdev
.dev
= dev
;
341 * The largest numbered port is the output port. It determines
342 * total number of pads.
344 for_each_endpoint_of_node(np
, ep
) {
345 struct of_endpoint endpoint
;
347 of_graph_parse_endpoint(ep
, &endpoint
);
348 num_pads
= max(num_pads
, endpoint
.port
+ 1);
352 dev_err(dev
, "Not enough ports %d\n", num_pads
);
356 vmux
->mux
= devm_mux_control_get(dev
, NULL
);
357 if (IS_ERR(vmux
->mux
)) {
358 ret
= PTR_ERR(vmux
->mux
);
359 if (ret
!= -EPROBE_DEFER
)
360 dev_err(dev
, "Failed to get mux: %d\n", ret
);
364 mutex_init(&vmux
->lock
);
366 vmux
->pads
= devm_kcalloc(dev
, num_pads
, sizeof(*vmux
->pads
),
368 vmux
->format_mbus
= devm_kcalloc(dev
, num_pads
,
369 sizeof(*vmux
->format_mbus
),
372 for (i
= 0; i
< num_pads
; i
++) {
373 vmux
->pads
[i
].flags
= (i
< num_pads
- 1) ? MEDIA_PAD_FL_SINK
374 : MEDIA_PAD_FL_SOURCE
;
375 vmux
->format_mbus
[i
] = video_mux_format_mbus_default
;
378 vmux
->subdev
.entity
.function
= MEDIA_ENT_F_VID_MUX
;
379 ret
= media_entity_pads_init(&vmux
->subdev
.entity
, num_pads
,
384 vmux
->subdev
.entity
.ops
= &video_mux_ops
;
386 return v4l2_async_register_subdev(&vmux
->subdev
);
389 static int video_mux_remove(struct platform_device
*pdev
)
391 struct video_mux
*vmux
= platform_get_drvdata(pdev
);
392 struct v4l2_subdev
*sd
= &vmux
->subdev
;
394 v4l2_async_unregister_subdev(sd
);
395 media_entity_cleanup(&sd
->entity
);
400 static const struct of_device_id video_mux_dt_ids
[] = {
401 { .compatible
= "video-mux", },
404 MODULE_DEVICE_TABLE(of
, video_mux_dt_ids
);
406 static struct platform_driver video_mux_driver
= {
407 .probe
= video_mux_probe
,
408 .remove
= video_mux_remove
,
410 .of_match_table
= video_mux_dt_ids
,
415 module_platform_driver(video_mux_driver
);
417 MODULE_DESCRIPTION("video stream multiplexer");
418 MODULE_AUTHOR("Sascha Hauer, Pengutronix");
419 MODULE_AUTHOR("Philipp Zabel, Pengutronix");
420 MODULE_LICENSE("GPL");