video-mux.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*
  2. * video stream multiplexer controlled via mux control
  3. *
  4. * Copyright (C) 2013 Pengutronix, Sascha Hauer <kernel@pengutronix.de>
  5. * Copyright (C) 2016-2017 Pengutronix, Philipp Zabel <kernel@pengutronix.de>
  6. *
  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.
  15. */
  16. #include <linux/err.h>
  17. #include <linux/module.h>
  18. #include <linux/mutex.h>
  19. #include <linux/mux/consumer.h>
  20. #include <linux/of.h>
  21. #include <linux/of_graph.h>
  22. #include <linux/platform_device.h>
  23. #include <media/v4l2-async.h>
  24. #include <media/v4l2-device.h>
  25. #include <media/v4l2-subdev.h>
  26. struct video_mux {
  27. struct v4l2_subdev subdev;
  28. struct media_pad *pads;
  29. struct v4l2_mbus_framefmt *format_mbus;
  30. struct mux_control *mux;
  31. struct mutex lock;
  32. int active;
  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. mutex_lock(&vmux->lock);
  152. /* Source pad mirrors active sink pad, no limitations on sink pads */
  153. if ((pad->flags & MEDIA_PAD_FL_SOURCE) && vmux->active >= 0)
  154. sdformat->format = vmux->format_mbus[vmux->active];
  155. *mbusformat = sdformat->format;
  156. /* Propagate the format from an active sink to source */
  157. if ((pad->flags & MEDIA_PAD_FL_SINK) && (pad->index == vmux->active))
  158. *source_mbusformat = sdformat->format;
  159. mutex_unlock(&vmux->lock);
  160. return 0;
  161. }
  162. static const struct v4l2_subdev_pad_ops video_mux_pad_ops = {
  163. .get_fmt = video_mux_get_format,
  164. .set_fmt = video_mux_set_format,
  165. };
  166. static const struct v4l2_subdev_ops video_mux_subdev_ops = {
  167. .pad = &video_mux_pad_ops,
  168. .video = &video_mux_subdev_video_ops,
  169. };
  170. static int video_mux_probe(struct platform_device *pdev)
  171. {
  172. struct device_node *np = pdev->dev.of_node;
  173. struct device *dev = &pdev->dev;
  174. struct device_node *ep;
  175. struct video_mux *vmux;
  176. unsigned int num_pads = 0;
  177. int ret;
  178. int i;
  179. vmux = devm_kzalloc(dev, sizeof(*vmux), GFP_KERNEL);
  180. if (!vmux)
  181. return -ENOMEM;
  182. platform_set_drvdata(pdev, vmux);
  183. v4l2_subdev_init(&vmux->subdev, &video_mux_subdev_ops);
  184. snprintf(vmux->subdev.name, sizeof(vmux->subdev.name), "%s", np->name);
  185. vmux->subdev.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  186. vmux->subdev.dev = dev;
  187. /*
  188. * The largest numbered port is the output port. It determines
  189. * total number of pads.
  190. */
  191. for_each_endpoint_of_node(np, ep) {
  192. struct of_endpoint endpoint;
  193. of_graph_parse_endpoint(ep, &endpoint);
  194. num_pads = max(num_pads, endpoint.port + 1);
  195. }
  196. if (num_pads < 2) {
  197. dev_err(dev, "Not enough ports %d\n", num_pads);
  198. return -EINVAL;
  199. }
  200. vmux->mux = devm_mux_control_get(dev, NULL);
  201. if (IS_ERR(vmux->mux)) {
  202. ret = PTR_ERR(vmux->mux);
  203. if (ret != -EPROBE_DEFER)
  204. dev_err(dev, "Failed to get mux: %d\n", ret);
  205. return ret;
  206. }
  207. mutex_init(&vmux->lock);
  208. vmux->active = -1;
  209. vmux->pads = devm_kcalloc(dev, num_pads, sizeof(*vmux->pads),
  210. GFP_KERNEL);
  211. vmux->format_mbus = devm_kcalloc(dev, num_pads,
  212. sizeof(*vmux->format_mbus),
  213. GFP_KERNEL);
  214. for (i = 0; i < num_pads - 1; i++)
  215. vmux->pads[i].flags = MEDIA_PAD_FL_SINK;
  216. vmux->pads[num_pads - 1].flags = MEDIA_PAD_FL_SOURCE;
  217. vmux->subdev.entity.function = MEDIA_ENT_F_VID_MUX;
  218. ret = media_entity_pads_init(&vmux->subdev.entity, num_pads,
  219. vmux->pads);
  220. if (ret < 0)
  221. return ret;
  222. vmux->subdev.entity.ops = &video_mux_ops;
  223. return v4l2_async_register_subdev(&vmux->subdev);
  224. }
  225. static int video_mux_remove(struct platform_device *pdev)
  226. {
  227. struct video_mux *vmux = platform_get_drvdata(pdev);
  228. struct v4l2_subdev *sd = &vmux->subdev;
  229. v4l2_async_unregister_subdev(sd);
  230. media_entity_cleanup(&sd->entity);
  231. return 0;
  232. }
  233. static const struct of_device_id video_mux_dt_ids[] = {
  234. { .compatible = "video-mux", },
  235. { /* sentinel */ }
  236. };
  237. MODULE_DEVICE_TABLE(of, video_mux_dt_ids);
  238. static struct platform_driver video_mux_driver = {
  239. .probe = video_mux_probe,
  240. .remove = video_mux_remove,
  241. .driver = {
  242. .of_match_table = video_mux_dt_ids,
  243. .name = "video-mux",
  244. },
  245. };
  246. module_platform_driver(video_mux_driver);
  247. MODULE_DESCRIPTION("video stream multiplexer");
  248. MODULE_AUTHOR("Sascha Hauer, Pengutronix");
  249. MODULE_AUTHOR("Philipp Zabel, Pengutronix");
  250. MODULE_LICENSE("GPL");