vimc-core.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. /*
  2. * vimc-core.c Virtual Media Controller Driver
  3. *
  4. * Copyright (C) 2015-2017 Helen Koike <helen.fornazier@gmail.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  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. */
  17. #include <linux/component.h>
  18. #include <linux/init.h>
  19. #include <linux/module.h>
  20. #include <linux/platform_device.h>
  21. #include <media/media-device.h>
  22. #include <media/v4l2-device.h>
  23. #include "vimc-common.h"
  24. #define VIMC_PDEV_NAME "vimc"
  25. #define VIMC_MDEV_MODEL_NAME "VIMC MDEV"
  26. #define VIMC_ENT_LINK(src, srcpad, sink, sinkpad, link_flags) { \
  27. .src_ent = src, \
  28. .src_pad = srcpad, \
  29. .sink_ent = sink, \
  30. .sink_pad = sinkpad, \
  31. .flags = link_flags, \
  32. }
  33. struct vimc_device {
  34. /* The platform device */
  35. struct platform_device pdev;
  36. /* The pipeline configuration */
  37. const struct vimc_pipeline_config *pipe_cfg;
  38. /* The Associated media_device parent */
  39. struct media_device mdev;
  40. /* Internal v4l2 parent device*/
  41. struct v4l2_device v4l2_dev;
  42. /* Subdevices */
  43. struct platform_device **subdevs;
  44. };
  45. /* Structure which describes individual configuration for each entity */
  46. struct vimc_ent_config {
  47. const char *name;
  48. const char *drv;
  49. };
  50. /* Structure which describes links between entities */
  51. struct vimc_ent_link {
  52. unsigned int src_ent;
  53. u16 src_pad;
  54. unsigned int sink_ent;
  55. u16 sink_pad;
  56. u32 flags;
  57. };
  58. /* Structure which describes the whole topology */
  59. struct vimc_pipeline_config {
  60. const struct vimc_ent_config *ents;
  61. size_t num_ents;
  62. const struct vimc_ent_link *links;
  63. size_t num_links;
  64. };
  65. /* --------------------------------------------------------------------------
  66. * Topology Configuration
  67. */
  68. static const struct vimc_ent_config ent_config[] = {
  69. {
  70. .name = "Sensor A",
  71. .drv = "vimc-sensor",
  72. },
  73. {
  74. .name = "Sensor B",
  75. .drv = "vimc-sensor",
  76. },
  77. {
  78. .name = "Debayer A",
  79. .drv = "vimc-debayer",
  80. },
  81. {
  82. .name = "Debayer B",
  83. .drv = "vimc-debayer",
  84. },
  85. {
  86. .name = "Raw Capture 0",
  87. .drv = "vimc-capture",
  88. },
  89. {
  90. .name = "Raw Capture 1",
  91. .drv = "vimc-capture",
  92. },
  93. {
  94. .name = "RGB/YUV Input",
  95. /* TODO: change this to vimc-input when it is implemented */
  96. .drv = "vimc-sensor",
  97. },
  98. {
  99. .name = "Scaler",
  100. .drv = "vimc-scaler",
  101. },
  102. {
  103. .name = "RGB/YUV Capture",
  104. .drv = "vimc-capture",
  105. },
  106. };
  107. static const struct vimc_ent_link ent_links[] = {
  108. /* Link: Sensor A (Pad 0)->(Pad 0) Debayer A */
  109. VIMC_ENT_LINK(0, 0, 2, 0, MEDIA_LNK_FL_ENABLED | MEDIA_LNK_FL_IMMUTABLE),
  110. /* Link: Sensor A (Pad 0)->(Pad 0) Raw Capture 0 */
  111. VIMC_ENT_LINK(0, 0, 4, 0, MEDIA_LNK_FL_ENABLED | MEDIA_LNK_FL_IMMUTABLE),
  112. /* Link: Sensor B (Pad 0)->(Pad 0) Debayer B */
  113. VIMC_ENT_LINK(1, 0, 3, 0, MEDIA_LNK_FL_ENABLED | MEDIA_LNK_FL_IMMUTABLE),
  114. /* Link: Sensor B (Pad 0)->(Pad 0) Raw Capture 1 */
  115. VIMC_ENT_LINK(1, 0, 5, 0, MEDIA_LNK_FL_ENABLED | MEDIA_LNK_FL_IMMUTABLE),
  116. /* Link: Debayer A (Pad 1)->(Pad 0) Scaler */
  117. VIMC_ENT_LINK(2, 1, 7, 0, MEDIA_LNK_FL_ENABLED),
  118. /* Link: Debayer B (Pad 1)->(Pad 0) Scaler */
  119. VIMC_ENT_LINK(3, 1, 7, 0, 0),
  120. /* Link: RGB/YUV Input (Pad 0)->(Pad 0) Scaler */
  121. VIMC_ENT_LINK(6, 0, 7, 0, 0),
  122. /* Link: Scaler (Pad 1)->(Pad 0) RGB/YUV Capture */
  123. VIMC_ENT_LINK(7, 1, 8, 0, MEDIA_LNK_FL_ENABLED | MEDIA_LNK_FL_IMMUTABLE),
  124. };
  125. static const struct vimc_pipeline_config pipe_cfg = {
  126. .ents = ent_config,
  127. .num_ents = ARRAY_SIZE(ent_config),
  128. .links = ent_links,
  129. .num_links = ARRAY_SIZE(ent_links)
  130. };
  131. /* -------------------------------------------------------------------------- */
  132. static int vimc_create_links(struct vimc_device *vimc)
  133. {
  134. unsigned int i;
  135. int ret;
  136. /* Initialize the links between entities */
  137. for (i = 0; i < vimc->pipe_cfg->num_links; i++) {
  138. const struct vimc_ent_link *link = &vimc->pipe_cfg->links[i];
  139. /*
  140. * TODO: Check another way of retrieving ved struct without
  141. * relying on platform_get_drvdata
  142. */
  143. struct vimc_ent_device *ved_src =
  144. platform_get_drvdata(vimc->subdevs[link->src_ent]);
  145. struct vimc_ent_device *ved_sink =
  146. platform_get_drvdata(vimc->subdevs[link->sink_ent]);
  147. ret = media_create_pad_link(ved_src->ent, link->src_pad,
  148. ved_sink->ent, link->sink_pad,
  149. link->flags);
  150. if (ret)
  151. return ret;
  152. }
  153. return 0;
  154. }
  155. static int vimc_comp_bind(struct device *master)
  156. {
  157. struct vimc_device *vimc = container_of(to_platform_device(master),
  158. struct vimc_device, pdev);
  159. int ret;
  160. dev_dbg(master, "bind");
  161. /* Register the v4l2 struct */
  162. ret = v4l2_device_register(vimc->mdev.dev, &vimc->v4l2_dev);
  163. if (ret) {
  164. dev_err(vimc->mdev.dev,
  165. "v4l2 device register failed (err=%d)\n", ret);
  166. return ret;
  167. }
  168. /* Bind subdevices */
  169. ret = component_bind_all(master, &vimc->v4l2_dev);
  170. if (ret)
  171. goto err_v4l2_unregister;
  172. /* Initialize links */
  173. ret = vimc_create_links(vimc);
  174. if (ret)
  175. goto err_comp_unbind_all;
  176. /* Register the media device */
  177. ret = media_device_register(&vimc->mdev);
  178. if (ret) {
  179. dev_err(vimc->mdev.dev,
  180. "media device register failed (err=%d)\n", ret);
  181. goto err_comp_unbind_all;
  182. }
  183. /* Expose all subdev's nodes*/
  184. ret = v4l2_device_register_subdev_nodes(&vimc->v4l2_dev);
  185. if (ret) {
  186. dev_err(vimc->mdev.dev,
  187. "vimc subdev nodes registration failed (err=%d)\n",
  188. ret);
  189. goto err_mdev_unregister;
  190. }
  191. return 0;
  192. err_mdev_unregister:
  193. media_device_unregister(&vimc->mdev);
  194. err_comp_unbind_all:
  195. component_unbind_all(master, NULL);
  196. err_v4l2_unregister:
  197. v4l2_device_unregister(&vimc->v4l2_dev);
  198. return ret;
  199. }
  200. static void vimc_comp_unbind(struct device *master)
  201. {
  202. struct vimc_device *vimc = container_of(to_platform_device(master),
  203. struct vimc_device, pdev);
  204. dev_dbg(master, "unbind");
  205. media_device_unregister(&vimc->mdev);
  206. component_unbind_all(master, NULL);
  207. v4l2_device_unregister(&vimc->v4l2_dev);
  208. }
  209. static int vimc_comp_compare(struct device *comp, void *data)
  210. {
  211. const struct platform_device *pdev = to_platform_device(comp);
  212. const char *name = data;
  213. return !strcmp(pdev->dev.platform_data, name);
  214. }
  215. static struct component_match *vimc_add_subdevs(struct vimc_device *vimc)
  216. {
  217. struct component_match *match = NULL;
  218. struct vimc_platform_data pdata;
  219. int i;
  220. for (i = 0; i < vimc->pipe_cfg->num_ents; i++) {
  221. dev_dbg(&vimc->pdev.dev, "new pdev for %s\n",
  222. vimc->pipe_cfg->ents[i].drv);
  223. strlcpy(pdata.entity_name, vimc->pipe_cfg->ents[i].name,
  224. sizeof(pdata.entity_name));
  225. vimc->subdevs[i] = platform_device_register_data(&vimc->pdev.dev,
  226. vimc->pipe_cfg->ents[i].drv,
  227. PLATFORM_DEVID_AUTO,
  228. &pdata,
  229. sizeof(pdata));
  230. if (IS_ERR(vimc->subdevs[i])) {
  231. match = ERR_CAST(vimc->subdevs[i]);
  232. while (--i >= 0)
  233. platform_device_unregister(vimc->subdevs[i]);
  234. return match;
  235. }
  236. component_match_add(&vimc->pdev.dev, &match, vimc_comp_compare,
  237. (void *)vimc->pipe_cfg->ents[i].name);
  238. }
  239. return match;
  240. }
  241. static void vimc_rm_subdevs(struct vimc_device *vimc)
  242. {
  243. unsigned int i;
  244. for (i = 0; i < vimc->pipe_cfg->num_ents; i++)
  245. platform_device_unregister(vimc->subdevs[i]);
  246. }
  247. static const struct component_master_ops vimc_comp_ops = {
  248. .bind = vimc_comp_bind,
  249. .unbind = vimc_comp_unbind,
  250. };
  251. static int vimc_probe(struct platform_device *pdev)
  252. {
  253. struct vimc_device *vimc = container_of(pdev, struct vimc_device, pdev);
  254. struct component_match *match = NULL;
  255. int ret;
  256. dev_dbg(&pdev->dev, "probe");
  257. /* Create platform_device for each entity in the topology*/
  258. vimc->subdevs = devm_kcalloc(&vimc->pdev.dev, vimc->pipe_cfg->num_ents,
  259. sizeof(*vimc->subdevs), GFP_KERNEL);
  260. if (!vimc->subdevs)
  261. return -ENOMEM;
  262. match = vimc_add_subdevs(vimc);
  263. if (IS_ERR(match))
  264. return PTR_ERR(match);
  265. /* Link the media device within the v4l2_device */
  266. vimc->v4l2_dev.mdev = &vimc->mdev;
  267. /* Initialize media device */
  268. strlcpy(vimc->mdev.model, VIMC_MDEV_MODEL_NAME,
  269. sizeof(vimc->mdev.model));
  270. vimc->mdev.dev = &pdev->dev;
  271. media_device_init(&vimc->mdev);
  272. /* Add self to the component system */
  273. ret = component_master_add_with_match(&pdev->dev, &vimc_comp_ops,
  274. match);
  275. if (ret) {
  276. media_device_cleanup(&vimc->mdev);
  277. vimc_rm_subdevs(vimc);
  278. kfree(vimc);
  279. return ret;
  280. }
  281. return 0;
  282. }
  283. static int vimc_remove(struct platform_device *pdev)
  284. {
  285. struct vimc_device *vimc = container_of(pdev, struct vimc_device, pdev);
  286. dev_dbg(&pdev->dev, "remove");
  287. component_master_del(&pdev->dev, &vimc_comp_ops);
  288. vimc_rm_subdevs(vimc);
  289. return 0;
  290. }
  291. static void vimc_dev_release(struct device *dev)
  292. {
  293. }
  294. static struct vimc_device vimc_dev = {
  295. .pipe_cfg = &pipe_cfg,
  296. .pdev = {
  297. .name = VIMC_PDEV_NAME,
  298. .dev.release = vimc_dev_release,
  299. }
  300. };
  301. static struct platform_driver vimc_pdrv = {
  302. .probe = vimc_probe,
  303. .remove = vimc_remove,
  304. .driver = {
  305. .name = VIMC_PDEV_NAME,
  306. },
  307. };
  308. static int __init vimc_init(void)
  309. {
  310. int ret;
  311. ret = platform_device_register(&vimc_dev.pdev);
  312. if (ret) {
  313. dev_err(&vimc_dev.pdev.dev,
  314. "platform device registration failed (err=%d)\n", ret);
  315. return ret;
  316. }
  317. ret = platform_driver_register(&vimc_pdrv);
  318. if (ret) {
  319. dev_err(&vimc_dev.pdev.dev,
  320. "platform driver registration failed (err=%d)\n", ret);
  321. platform_driver_unregister(&vimc_pdrv);
  322. return ret;
  323. }
  324. return 0;
  325. }
  326. static void __exit vimc_exit(void)
  327. {
  328. platform_driver_unregister(&vimc_pdrv);
  329. platform_device_unregister(&vimc_dev.pdev);
  330. }
  331. module_init(vimc_init);
  332. module_exit(vimc_exit);
  333. MODULE_DESCRIPTION("Virtual Media Controller Driver (VIMC)");
  334. MODULE_AUTHOR("Helen Fornazier <helen.fornazier@gmail.com>");
  335. MODULE_LICENSE("GPL");