video-mux.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * video stream multiplexer controlled via mux control
  4. *
  5. * Copyright (C) 2013 Pengutronix, Sascha Hauer <kernel@pengutronix.de>
  6. * Copyright (C) 2016-2017 Pengutronix, Philipp Zabel <kernel@pengutronix.de>
  7. */
  8. #include <linux/err.h>
  9. #include <linux/module.h>
  10. #include <linux/mutex.h>
  11. #include <linux/mux/consumer.h>
  12. #include <linux/of.h>
  13. #include <linux/of_graph.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/slab.h>
  16. #include <media/v4l2-async.h>
  17. #include <media/v4l2-device.h>
  18. #include <media/v4l2-fwnode.h>
  19. #include <media/v4l2-subdev.h>
  20. struct video_mux {
  21. struct v4l2_subdev subdev;
  22. struct media_pad *pads;
  23. struct v4l2_mbus_framefmt *format_mbus;
  24. struct mux_control *mux;
  25. struct mutex lock;
  26. int active;
  27. };
  28. static const struct v4l2_mbus_framefmt video_mux_format_mbus_default = {
  29. .width = 1,
  30. .height = 1,
  31. .code = MEDIA_BUS_FMT_Y8_1X8,
  32. .field = V4L2_FIELD_NONE,
  33. };
  34. static inline struct video_mux *v4l2_subdev_to_video_mux(struct v4l2_subdev *sd)
  35. {
  36. return container_of(sd, struct video_mux, subdev);
  37. }
  38. static int video_mux_link_setup(struct media_entity *entity,
  39. const struct media_pad *local,
  40. const struct media_pad *remote, u32 flags)
  41. {
  42. struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
  43. struct video_mux *vmux = v4l2_subdev_to_video_mux(sd);
  44. u16 source_pad = entity->num_pads - 1;
  45. int ret = 0;
  46. /*
  47. * The mux state is determined by the enabled sink pad link.
  48. * Enabling or disabling the source pad link has no effect.
  49. */
  50. if (local->flags & MEDIA_PAD_FL_SOURCE)
  51. return 0;
  52. dev_dbg(sd->dev, "link setup '%s':%d->'%s':%d[%d]",
  53. remote->entity->name, remote->index, local->entity->name,
  54. local->index, flags & MEDIA_LNK_FL_ENABLED);
  55. mutex_lock(&vmux->lock);
  56. if (flags & MEDIA_LNK_FL_ENABLED) {
  57. if (vmux->active == local->index)
  58. goto out;
  59. if (vmux->active >= 0) {
  60. ret = -EBUSY;
  61. goto out;
  62. }
  63. dev_dbg(sd->dev, "setting %d active\n", local->index);
  64. ret = mux_control_try_select(vmux->mux, local->index);
  65. if (ret < 0)
  66. goto out;
  67. vmux->active = local->index;
  68. /* Propagate the active format to the source */
  69. vmux->format_mbus[source_pad] = vmux->format_mbus[vmux->active];
  70. } else {
  71. if (vmux->active != local->index)
  72. goto out;
  73. dev_dbg(sd->dev, "going inactive\n");
  74. mux_control_deselect(vmux->mux);
  75. vmux->active = -1;
  76. }
  77. out:
  78. mutex_unlock(&vmux->lock);
  79. return ret;
  80. }
  81. static const struct media_entity_operations video_mux_ops = {
  82. .link_setup = video_mux_link_setup,
  83. .link_validate = v4l2_subdev_link_validate,
  84. };
  85. static int video_mux_s_stream(struct v4l2_subdev *sd, int enable)
  86. {
  87. struct video_mux *vmux = v4l2_subdev_to_video_mux(sd);
  88. struct v4l2_subdev *upstream_sd;
  89. struct media_pad *pad;
  90. if (vmux->active == -1) {
  91. dev_err(sd->dev, "Can not start streaming on inactive mux\n");
  92. return -EINVAL;
  93. }
  94. pad = media_entity_remote_pad(&sd->entity.pads[vmux->active]);
  95. if (!pad) {
  96. dev_err(sd->dev, "Failed to find remote source pad\n");
  97. return -ENOLINK;
  98. }
  99. if (!is_media_entity_v4l2_subdev(pad->entity)) {
  100. dev_err(sd->dev, "Upstream entity is not a v4l2 subdev\n");
  101. return -ENODEV;
  102. }
  103. upstream_sd = media_entity_to_v4l2_subdev(pad->entity);
  104. return v4l2_subdev_call(upstream_sd, video, s_stream, enable);
  105. }
  106. static const struct v4l2_subdev_video_ops video_mux_subdev_video_ops = {
  107. .s_stream = video_mux_s_stream,
  108. };
  109. static struct v4l2_mbus_framefmt *
  110. __video_mux_get_pad_format(struct v4l2_subdev *sd,
  111. struct v4l2_subdev_pad_config *cfg,
  112. unsigned int pad, u32 which)
  113. {
  114. struct video_mux *vmux = v4l2_subdev_to_video_mux(sd);
  115. switch (which) {
  116. case V4L2_SUBDEV_FORMAT_TRY:
  117. return v4l2_subdev_get_try_format(sd, cfg, pad);
  118. case V4L2_SUBDEV_FORMAT_ACTIVE:
  119. return &vmux->format_mbus[pad];
  120. default:
  121. return NULL;
  122. }
  123. }
  124. static int video_mux_get_format(struct v4l2_subdev *sd,
  125. struct v4l2_subdev_pad_config *cfg,
  126. struct v4l2_subdev_format *sdformat)
  127. {
  128. struct video_mux *vmux = v4l2_subdev_to_video_mux(sd);
  129. mutex_lock(&vmux->lock);
  130. sdformat->format = *__video_mux_get_pad_format(sd, cfg, sdformat->pad,
  131. sdformat->which);
  132. mutex_unlock(&vmux->lock);
  133. return 0;
  134. }
  135. static int video_mux_set_format(struct v4l2_subdev *sd,
  136. struct v4l2_subdev_pad_config *cfg,
  137. struct v4l2_subdev_format *sdformat)
  138. {
  139. struct video_mux *vmux = v4l2_subdev_to_video_mux(sd);
  140. struct v4l2_mbus_framefmt *mbusformat, *source_mbusformat;
  141. struct media_pad *pad = &vmux->pads[sdformat->pad];
  142. u16 source_pad = sd->entity.num_pads - 1;
  143. mbusformat = __video_mux_get_pad_format(sd, cfg, sdformat->pad,
  144. sdformat->which);
  145. if (!mbusformat)
  146. return -EINVAL;
  147. source_mbusformat = __video_mux_get_pad_format(sd, cfg, source_pad,
  148. sdformat->which);
  149. if (!source_mbusformat)
  150. return -EINVAL;
  151. /* No size limitations except V4L2 compliance requirements */
  152. v4l_bound_align_image(&sdformat->format.width, 1, 65536, 0,
  153. &sdformat->format.height, 1, 65536, 0, 0);
  154. /* All formats except LVDS and vendor specific formats are acceptable */
  155. switch (sdformat->format.code) {
  156. case MEDIA_BUS_FMT_RGB444_1X12:
  157. case MEDIA_BUS_FMT_RGB444_2X8_PADHI_BE:
  158. case MEDIA_BUS_FMT_RGB444_2X8_PADHI_LE:
  159. case MEDIA_BUS_FMT_RGB555_2X8_PADHI_BE:
  160. case MEDIA_BUS_FMT_RGB555_2X8_PADHI_LE:
  161. case MEDIA_BUS_FMT_RGB565_1X16:
  162. case MEDIA_BUS_FMT_BGR565_2X8_BE:
  163. case MEDIA_BUS_FMT_BGR565_2X8_LE:
  164. case MEDIA_BUS_FMT_RGB565_2X8_BE:
  165. case MEDIA_BUS_FMT_RGB565_2X8_LE:
  166. case MEDIA_BUS_FMT_RGB666_1X18:
  167. case MEDIA_BUS_FMT_RBG888_1X24:
  168. case MEDIA_BUS_FMT_RGB666_1X24_CPADHI:
  169. case MEDIA_BUS_FMT_BGR888_1X24:
  170. case MEDIA_BUS_FMT_GBR888_1X24:
  171. case MEDIA_BUS_FMT_RGB888_1X24:
  172. case MEDIA_BUS_FMT_RGB888_2X12_BE:
  173. case MEDIA_BUS_FMT_RGB888_2X12_LE:
  174. case MEDIA_BUS_FMT_ARGB8888_1X32:
  175. case MEDIA_BUS_FMT_RGB888_1X32_PADHI:
  176. case MEDIA_BUS_FMT_RGB101010_1X30:
  177. case MEDIA_BUS_FMT_RGB121212_1X36:
  178. case MEDIA_BUS_FMT_RGB161616_1X48:
  179. case MEDIA_BUS_FMT_Y8_1X8:
  180. case MEDIA_BUS_FMT_UV8_1X8:
  181. case MEDIA_BUS_FMT_UYVY8_1_5X8:
  182. case MEDIA_BUS_FMT_VYUY8_1_5X8:
  183. case MEDIA_BUS_FMT_YUYV8_1_5X8:
  184. case MEDIA_BUS_FMT_YVYU8_1_5X8:
  185. case MEDIA_BUS_FMT_UYVY8_2X8:
  186. case MEDIA_BUS_FMT_VYUY8_2X8:
  187. case MEDIA_BUS_FMT_YUYV8_2X8:
  188. case MEDIA_BUS_FMT_YVYU8_2X8:
  189. case MEDIA_BUS_FMT_Y10_1X10:
  190. case MEDIA_BUS_FMT_UYVY10_2X10:
  191. case MEDIA_BUS_FMT_VYUY10_2X10:
  192. case MEDIA_BUS_FMT_YUYV10_2X10:
  193. case MEDIA_BUS_FMT_YVYU10_2X10:
  194. case MEDIA_BUS_FMT_Y12_1X12:
  195. case MEDIA_BUS_FMT_UYVY12_2X12:
  196. case MEDIA_BUS_FMT_VYUY12_2X12:
  197. case MEDIA_BUS_FMT_YUYV12_2X12:
  198. case MEDIA_BUS_FMT_YVYU12_2X12:
  199. case MEDIA_BUS_FMT_UYVY8_1X16:
  200. case MEDIA_BUS_FMT_VYUY8_1X16:
  201. case MEDIA_BUS_FMT_YUYV8_1X16:
  202. case MEDIA_BUS_FMT_YVYU8_1X16:
  203. case MEDIA_BUS_FMT_YDYUYDYV8_1X16:
  204. case MEDIA_BUS_FMT_UYVY10_1X20:
  205. case MEDIA_BUS_FMT_VYUY10_1X20:
  206. case MEDIA_BUS_FMT_YUYV10_1X20:
  207. case MEDIA_BUS_FMT_YVYU10_1X20:
  208. case MEDIA_BUS_FMT_VUY8_1X24:
  209. case MEDIA_BUS_FMT_YUV8_1X24:
  210. case MEDIA_BUS_FMT_UYYVYY8_0_5X24:
  211. case MEDIA_BUS_FMT_UYVY12_1X24:
  212. case MEDIA_BUS_FMT_VYUY12_1X24:
  213. case MEDIA_BUS_FMT_YUYV12_1X24:
  214. case MEDIA_BUS_FMT_YVYU12_1X24:
  215. case MEDIA_BUS_FMT_YUV10_1X30:
  216. case MEDIA_BUS_FMT_UYYVYY10_0_5X30:
  217. case MEDIA_BUS_FMT_AYUV8_1X32:
  218. case MEDIA_BUS_FMT_UYYVYY12_0_5X36:
  219. case MEDIA_BUS_FMT_YUV12_1X36:
  220. case MEDIA_BUS_FMT_YUV16_1X48:
  221. case MEDIA_BUS_FMT_UYYVYY16_0_5X48:
  222. case MEDIA_BUS_FMT_JPEG_1X8:
  223. case MEDIA_BUS_FMT_AHSV8888_1X32:
  224. case MEDIA_BUS_FMT_SBGGR8_1X8:
  225. case MEDIA_BUS_FMT_SGBRG8_1X8:
  226. case MEDIA_BUS_FMT_SGRBG8_1X8:
  227. case MEDIA_BUS_FMT_SRGGB8_1X8:
  228. case MEDIA_BUS_FMT_SBGGR10_1X10:
  229. case MEDIA_BUS_FMT_SGBRG10_1X10:
  230. case MEDIA_BUS_FMT_SGRBG10_1X10:
  231. case MEDIA_BUS_FMT_SRGGB10_1X10:
  232. case MEDIA_BUS_FMT_SBGGR12_1X12:
  233. case MEDIA_BUS_FMT_SGBRG12_1X12:
  234. case MEDIA_BUS_FMT_SGRBG12_1X12:
  235. case MEDIA_BUS_FMT_SRGGB12_1X12:
  236. case MEDIA_BUS_FMT_SBGGR14_1X14:
  237. case MEDIA_BUS_FMT_SGBRG14_1X14:
  238. case MEDIA_BUS_FMT_SGRBG14_1X14:
  239. case MEDIA_BUS_FMT_SRGGB14_1X14:
  240. case MEDIA_BUS_FMT_SBGGR16_1X16:
  241. case MEDIA_BUS_FMT_SGBRG16_1X16:
  242. case MEDIA_BUS_FMT_SGRBG16_1X16:
  243. case MEDIA_BUS_FMT_SRGGB16_1X16:
  244. break;
  245. default:
  246. sdformat->format.code = MEDIA_BUS_FMT_Y8_1X8;
  247. break;
  248. }
  249. if (sdformat->format.field == V4L2_FIELD_ANY)
  250. sdformat->format.field = V4L2_FIELD_NONE;
  251. mutex_lock(&vmux->lock);
  252. /* Source pad mirrors active sink pad, no limitations on sink pads */
  253. if ((pad->flags & MEDIA_PAD_FL_SOURCE) && vmux->active >= 0)
  254. sdformat->format = vmux->format_mbus[vmux->active];
  255. *mbusformat = sdformat->format;
  256. /* Propagate the format from an active sink to source */
  257. if ((pad->flags & MEDIA_PAD_FL_SINK) && (pad->index == vmux->active))
  258. *source_mbusformat = sdformat->format;
  259. mutex_unlock(&vmux->lock);
  260. return 0;
  261. }
  262. static int video_mux_init_cfg(struct v4l2_subdev *sd,
  263. struct v4l2_subdev_pad_config *cfg)
  264. {
  265. struct video_mux *vmux = v4l2_subdev_to_video_mux(sd);
  266. struct v4l2_mbus_framefmt *mbusformat;
  267. unsigned int i;
  268. mutex_lock(&vmux->lock);
  269. for (i = 0; i < sd->entity.num_pads; i++) {
  270. mbusformat = v4l2_subdev_get_try_format(sd, cfg, i);
  271. *mbusformat = video_mux_format_mbus_default;
  272. }
  273. mutex_unlock(&vmux->lock);
  274. return 0;
  275. }
  276. static const struct v4l2_subdev_pad_ops video_mux_pad_ops = {
  277. .init_cfg = video_mux_init_cfg,
  278. .get_fmt = video_mux_get_format,
  279. .set_fmt = video_mux_set_format,
  280. };
  281. static const struct v4l2_subdev_ops video_mux_subdev_ops = {
  282. .pad = &video_mux_pad_ops,
  283. .video = &video_mux_subdev_video_ops,
  284. };
  285. static int video_mux_parse_endpoint(struct device *dev,
  286. struct v4l2_fwnode_endpoint *vep,
  287. struct v4l2_async_subdev *asd)
  288. {
  289. /*
  290. * it's not an error if remote is missing on a video-mux
  291. * input port, return -ENOTCONN to skip this endpoint with
  292. * no error.
  293. */
  294. return fwnode_device_is_available(asd->match.fwnode) ? 0 : -ENOTCONN;
  295. }
  296. static int video_mux_async_register(struct video_mux *vmux,
  297. unsigned int num_input_pads)
  298. {
  299. unsigned int i, *ports;
  300. int ret;
  301. ports = kcalloc(num_input_pads, sizeof(*ports), GFP_KERNEL);
  302. if (!ports)
  303. return -ENOMEM;
  304. for (i = 0; i < num_input_pads; i++)
  305. ports[i] = i;
  306. ret = v4l2_async_register_fwnode_subdev(
  307. &vmux->subdev, sizeof(struct v4l2_async_subdev),
  308. ports, num_input_pads, video_mux_parse_endpoint);
  309. kfree(ports);
  310. return ret;
  311. }
  312. static int video_mux_probe(struct platform_device *pdev)
  313. {
  314. struct device_node *np = pdev->dev.of_node;
  315. struct device *dev = &pdev->dev;
  316. struct device_node *ep;
  317. struct video_mux *vmux;
  318. unsigned int num_pads = 0;
  319. unsigned int i;
  320. int ret;
  321. vmux = devm_kzalloc(dev, sizeof(*vmux), GFP_KERNEL);
  322. if (!vmux)
  323. return -ENOMEM;
  324. platform_set_drvdata(pdev, vmux);
  325. v4l2_subdev_init(&vmux->subdev, &video_mux_subdev_ops);
  326. snprintf(vmux->subdev.name, sizeof(vmux->subdev.name), "%pOFn", np);
  327. vmux->subdev.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  328. vmux->subdev.dev = dev;
  329. /*
  330. * The largest numbered port is the output port. It determines
  331. * total number of pads.
  332. */
  333. for_each_endpoint_of_node(np, ep) {
  334. struct of_endpoint endpoint;
  335. of_graph_parse_endpoint(ep, &endpoint);
  336. num_pads = max(num_pads, endpoint.port + 1);
  337. }
  338. if (num_pads < 2) {
  339. dev_err(dev, "Not enough ports %d\n", num_pads);
  340. return -EINVAL;
  341. }
  342. vmux->mux = devm_mux_control_get(dev, NULL);
  343. if (IS_ERR(vmux->mux)) {
  344. ret = PTR_ERR(vmux->mux);
  345. if (ret != -EPROBE_DEFER)
  346. dev_err(dev, "Failed to get mux: %d\n", ret);
  347. return ret;
  348. }
  349. mutex_init(&vmux->lock);
  350. vmux->active = -1;
  351. vmux->pads = devm_kcalloc(dev, num_pads, sizeof(*vmux->pads),
  352. GFP_KERNEL);
  353. if (!vmux->pads)
  354. return -ENOMEM;
  355. vmux->format_mbus = devm_kcalloc(dev, num_pads,
  356. sizeof(*vmux->format_mbus),
  357. GFP_KERNEL);
  358. if (!vmux->format_mbus)
  359. return -ENOMEM;
  360. for (i = 0; i < num_pads; i++) {
  361. vmux->pads[i].flags = (i < num_pads - 1) ? MEDIA_PAD_FL_SINK
  362. : MEDIA_PAD_FL_SOURCE;
  363. vmux->format_mbus[i] = video_mux_format_mbus_default;
  364. }
  365. vmux->subdev.entity.function = MEDIA_ENT_F_VID_MUX;
  366. ret = media_entity_pads_init(&vmux->subdev.entity, num_pads,
  367. vmux->pads);
  368. if (ret < 0)
  369. return ret;
  370. vmux->subdev.entity.ops = &video_mux_ops;
  371. return video_mux_async_register(vmux, num_pads - 1);
  372. }
  373. static int video_mux_remove(struct platform_device *pdev)
  374. {
  375. struct video_mux *vmux = platform_get_drvdata(pdev);
  376. struct v4l2_subdev *sd = &vmux->subdev;
  377. v4l2_async_unregister_subdev(sd);
  378. media_entity_cleanup(&sd->entity);
  379. return 0;
  380. }
  381. static const struct of_device_id video_mux_dt_ids[] = {
  382. { .compatible = "video-mux", },
  383. { /* sentinel */ }
  384. };
  385. MODULE_DEVICE_TABLE(of, video_mux_dt_ids);
  386. static struct platform_driver video_mux_driver = {
  387. .probe = video_mux_probe,
  388. .remove = video_mux_remove,
  389. .driver = {
  390. .of_match_table = video_mux_dt_ids,
  391. .name = "video-mux",
  392. },
  393. };
  394. module_platform_driver(video_mux_driver);
  395. MODULE_DESCRIPTION("video stream multiplexer");
  396. MODULE_AUTHOR("Sascha Hauer, Pengutronix");
  397. MODULE_AUTHOR("Philipp Zabel, Pengutronix");
  398. MODULE_LICENSE("GPL");