vimc-core.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * vimc-core.c Virtual Media Controller Driver
  4. *
  5. * Copyright (C) 2015-2017 Helen Koike <helen.fornazier@gmail.com>
  6. */
  7. #include <linux/dma-mapping.h>
  8. #include <linux/font.h>
  9. #include <linux/init.h>
  10. #include <linux/module.h>
  11. #include <linux/platform_device.h>
  12. #include <media/media-device.h>
  13. #include <media/tpg/v4l2-tpg.h>
  14. #include <media/v4l2-device.h>
  15. #include "vimc-common.h"
  16. unsigned int vimc_allocator;
  17. module_param_named(allocator, vimc_allocator, uint, 0444);
  18. MODULE_PARM_DESC(allocator, " memory allocator selection, default is 0.\n"
  19. "\t\t 0 == vmalloc\n"
  20. "\t\t 1 == dma-contig");
  21. #define VIMC_MDEV_MODEL_NAME "VIMC MDEV"
  22. #define VIMC_DATA_LINK(src, srcpad, sink, sinkpad, link_flags) { \
  23. .src_ent = src, \
  24. .src_pad = srcpad, \
  25. .sink_ent = sink, \
  26. .sink_pad = sinkpad, \
  27. .flags = link_flags, \
  28. }
  29. #define VIMC_ANCILLARY_LINK(primary, ancillary) { \
  30. .primary_ent = primary, \
  31. .ancillary_ent = ancillary \
  32. }
  33. /* Structure which describes data links between entities */
  34. struct vimc_data_link {
  35. unsigned int src_ent;
  36. u16 src_pad;
  37. unsigned int sink_ent;
  38. u16 sink_pad;
  39. u32 flags;
  40. };
  41. /* Enum to improve clarity when defining vimc_data_links */
  42. enum vimc_data_link_ents {
  43. SENSOR_A,
  44. SENSOR_B,
  45. DEBAYER_A,
  46. DEBAYER_B,
  47. RAW_CAPTURE_0,
  48. RAW_CAPTURE_1,
  49. RGB_YUV_INPUT,
  50. SCALER,
  51. RGB_YUV_CAPTURE,
  52. LENS_A,
  53. LENS_B,
  54. };
  55. /* Structure which describes ancillary links between entities */
  56. struct vimc_ancillary_link {
  57. unsigned int primary_ent;
  58. unsigned int ancillary_ent;
  59. };
  60. /* Structure which describes the whole topology */
  61. struct vimc_pipeline_config {
  62. const struct vimc_ent_config *ents;
  63. size_t num_ents;
  64. const struct vimc_data_link *data_links;
  65. size_t num_data_links;
  66. const struct vimc_ancillary_link *ancillary_links;
  67. size_t num_ancillary_links;
  68. };
  69. /* --------------------------------------------------------------------------
  70. * Topology Configuration
  71. */
  72. static const struct vimc_ent_config ent_config[] = {
  73. [SENSOR_A] = {
  74. .name = "Sensor A",
  75. .type = &vimc_sensor_type
  76. },
  77. [SENSOR_B] = {
  78. .name = "Sensor B",
  79. .type = &vimc_sensor_type
  80. },
  81. [DEBAYER_A] = {
  82. .name = "Debayer A",
  83. .type = &vimc_debayer_type
  84. },
  85. [DEBAYER_B] = {
  86. .name = "Debayer B",
  87. .type = &vimc_debayer_type
  88. },
  89. [RAW_CAPTURE_0] = {
  90. .name = "Raw Capture 0",
  91. .type = &vimc_capture_type
  92. },
  93. [RAW_CAPTURE_1] = {
  94. .name = "Raw Capture 1",
  95. .type = &vimc_capture_type
  96. },
  97. [RGB_YUV_INPUT] = {
  98. /* TODO: change this to vimc-input when it is implemented */
  99. .name = "RGB/YUV Input",
  100. .type = &vimc_sensor_type
  101. },
  102. [SCALER] = {
  103. .name = "Scaler",
  104. .type = &vimc_scaler_type
  105. },
  106. [RGB_YUV_CAPTURE] = {
  107. .name = "RGB/YUV Capture",
  108. .type = &vimc_capture_type
  109. },
  110. [LENS_A] = {
  111. .name = "Lens A",
  112. .type = &vimc_lens_type
  113. },
  114. [LENS_B] = {
  115. .name = "Lens B",
  116. .type = &vimc_lens_type
  117. },
  118. };
  119. static const struct vimc_data_link data_links[] = {
  120. /* Link: Sensor A (Pad 0)->(Pad 0) Debayer A */
  121. VIMC_DATA_LINK(SENSOR_A, 0, DEBAYER_A, 0,
  122. MEDIA_LNK_FL_ENABLED | MEDIA_LNK_FL_IMMUTABLE),
  123. /* Link: Sensor A (Pad 0)->(Pad 0) Raw Capture 0 */
  124. VIMC_DATA_LINK(SENSOR_A, 0, RAW_CAPTURE_0, 0,
  125. MEDIA_LNK_FL_ENABLED | MEDIA_LNK_FL_IMMUTABLE),
  126. /* Link: Sensor B (Pad 0)->(Pad 0) Debayer B */
  127. VIMC_DATA_LINK(SENSOR_B, 0, DEBAYER_B, 0,
  128. MEDIA_LNK_FL_ENABLED | MEDIA_LNK_FL_IMMUTABLE),
  129. /* Link: Sensor B (Pad 0)->(Pad 0) Raw Capture 1 */
  130. VIMC_DATA_LINK(SENSOR_B, 0, RAW_CAPTURE_1, 0,
  131. MEDIA_LNK_FL_ENABLED | MEDIA_LNK_FL_IMMUTABLE),
  132. /* Link: Debayer A (Pad 1)->(Pad 0) Scaler */
  133. VIMC_DATA_LINK(DEBAYER_A, 1, SCALER, 0, MEDIA_LNK_FL_ENABLED),
  134. /* Link: Debayer B (Pad 1)->(Pad 0) Scaler */
  135. VIMC_DATA_LINK(DEBAYER_B, 1, SCALER, 0, 0),
  136. /* Link: RGB/YUV Input (Pad 0)->(Pad 0) Scaler */
  137. VIMC_DATA_LINK(RGB_YUV_INPUT, 0, SCALER, 0, 0),
  138. /* Link: Scaler (Pad 1)->(Pad 0) RGB/YUV Capture */
  139. VIMC_DATA_LINK(SCALER, 1, RGB_YUV_CAPTURE, 0,
  140. MEDIA_LNK_FL_ENABLED | MEDIA_LNK_FL_IMMUTABLE),
  141. };
  142. static const struct vimc_ancillary_link ancillary_links[] = {
  143. /* Link: Sensor A -> Lens A */
  144. VIMC_ANCILLARY_LINK(0, 9),
  145. /* Link: Sensor B -> Lens B */
  146. VIMC_ANCILLARY_LINK(1, 10),
  147. };
  148. static struct vimc_pipeline_config pipe_cfg = {
  149. .ents = ent_config,
  150. .num_ents = ARRAY_SIZE(ent_config),
  151. .data_links = data_links,
  152. .num_data_links = ARRAY_SIZE(data_links),
  153. .ancillary_links = ancillary_links,
  154. .num_ancillary_links = ARRAY_SIZE(ancillary_links),
  155. };
  156. /* -------------------------------------------------------------------------- */
  157. static void vimc_rm_links(struct vimc_device *vimc)
  158. {
  159. unsigned int i;
  160. for (i = 0; i < vimc->pipe_cfg->num_ents; i++)
  161. media_entity_remove_links(vimc->ent_devs[i]->ent);
  162. }
  163. static int vimc_create_links(struct vimc_device *vimc)
  164. {
  165. unsigned int i;
  166. int ret;
  167. /* Initialize the links between entities */
  168. for (i = 0; i < vimc->pipe_cfg->num_data_links; i++) {
  169. const struct vimc_data_link *link = &vimc->pipe_cfg->data_links[i];
  170. struct vimc_ent_device *ved_src =
  171. vimc->ent_devs[link->src_ent];
  172. struct vimc_ent_device *ved_sink =
  173. vimc->ent_devs[link->sink_ent];
  174. ret = media_create_pad_link(ved_src->ent, link->src_pad,
  175. ved_sink->ent, link->sink_pad,
  176. link->flags);
  177. if (ret)
  178. goto err_rm_links;
  179. }
  180. for (i = 0; i < vimc->pipe_cfg->num_ancillary_links; i++) {
  181. const struct vimc_ancillary_link *link = &vimc->pipe_cfg->ancillary_links[i];
  182. struct vimc_ent_device *ved_primary =
  183. vimc->ent_devs[link->primary_ent];
  184. struct vimc_ent_device *ved_ancillary =
  185. vimc->ent_devs[link->ancillary_ent];
  186. struct media_link *ret_link =
  187. media_create_ancillary_link(ved_primary->ent, ved_ancillary->ent);
  188. if (IS_ERR(ret_link)) {
  189. ret = PTR_ERR(ret_link);
  190. goto err_rm_links;
  191. }
  192. }
  193. return 0;
  194. err_rm_links:
  195. vimc_rm_links(vimc);
  196. return ret;
  197. }
  198. static void vimc_release_subdevs(struct vimc_device *vimc)
  199. {
  200. unsigned int i;
  201. for (i = 0; i < vimc->pipe_cfg->num_ents; i++)
  202. if (vimc->ent_devs[i])
  203. vimc->pipe_cfg->ents[i].type->release(vimc->ent_devs[i]);
  204. }
  205. static void vimc_unregister_subdevs(struct vimc_device *vimc)
  206. {
  207. unsigned int i;
  208. for (i = 0; i < vimc->pipe_cfg->num_ents; i++)
  209. if (vimc->ent_devs[i] && vimc->pipe_cfg->ents[i].type->unregister)
  210. vimc->pipe_cfg->ents[i].type->unregister(vimc->ent_devs[i]);
  211. }
  212. static int vimc_add_subdevs(struct vimc_device *vimc)
  213. {
  214. unsigned int i;
  215. for (i = 0; i < vimc->pipe_cfg->num_ents; i++) {
  216. dev_dbg(vimc->mdev.dev, "new entity for %s\n",
  217. vimc->pipe_cfg->ents[i].name);
  218. vimc->ent_devs[i] = vimc->pipe_cfg->ents[i].type->add(vimc,
  219. vimc->pipe_cfg->ents[i].name);
  220. if (IS_ERR(vimc->ent_devs[i])) {
  221. int err = PTR_ERR(vimc->ent_devs[i]);
  222. dev_err(vimc->mdev.dev, "adding entity %s failed (%d)\n",
  223. vimc->pipe_cfg->ents[i].name, err);
  224. vimc->ent_devs[i] = NULL;
  225. vimc_unregister_subdevs(vimc);
  226. vimc_release_subdevs(vimc);
  227. return err;
  228. }
  229. }
  230. return 0;
  231. }
  232. static void vimc_v4l2_dev_release(struct v4l2_device *v4l2_dev)
  233. {
  234. struct vimc_device *vimc =
  235. container_of(v4l2_dev, struct vimc_device, v4l2_dev);
  236. vimc_release_subdevs(vimc);
  237. media_device_cleanup(&vimc->mdev);
  238. kfree(vimc->ent_devs);
  239. kfree(vimc);
  240. }
  241. static int vimc_register_devices(struct vimc_device *vimc)
  242. {
  243. int ret;
  244. /* Register the v4l2 struct */
  245. ret = v4l2_device_register(vimc->mdev.dev, &vimc->v4l2_dev);
  246. if (ret) {
  247. dev_err(vimc->mdev.dev,
  248. "v4l2 device register failed (err=%d)\n", ret);
  249. return ret;
  250. }
  251. /* allocate ent_devs */
  252. vimc->ent_devs = kcalloc(vimc->pipe_cfg->num_ents,
  253. sizeof(*vimc->ent_devs), GFP_KERNEL);
  254. if (!vimc->ent_devs) {
  255. ret = -ENOMEM;
  256. goto err_v4l2_unregister;
  257. }
  258. /* Invoke entity config hooks to initialize and register subdevs */
  259. ret = vimc_add_subdevs(vimc);
  260. if (ret)
  261. goto err_free_ent_devs;
  262. /* Initialize links */
  263. ret = vimc_create_links(vimc);
  264. if (ret)
  265. goto err_rm_subdevs;
  266. /* Register the media device */
  267. ret = media_device_register(&vimc->mdev);
  268. if (ret) {
  269. dev_err(vimc->mdev.dev,
  270. "media device register failed (err=%d)\n", ret);
  271. goto err_rm_subdevs;
  272. }
  273. /* Expose all subdev's nodes*/
  274. ret = v4l2_device_register_subdev_nodes(&vimc->v4l2_dev);
  275. if (ret) {
  276. dev_err(vimc->mdev.dev,
  277. "vimc subdev nodes registration failed (err=%d)\n",
  278. ret);
  279. goto err_mdev_unregister;
  280. }
  281. return 0;
  282. err_mdev_unregister:
  283. media_device_unregister(&vimc->mdev);
  284. err_rm_subdevs:
  285. vimc_unregister_subdevs(vimc);
  286. vimc_release_subdevs(vimc);
  287. err_free_ent_devs:
  288. kfree(vimc->ent_devs);
  289. err_v4l2_unregister:
  290. v4l2_device_unregister(&vimc->v4l2_dev);
  291. return ret;
  292. }
  293. static int vimc_probe(struct platform_device *pdev)
  294. {
  295. const struct font_desc *font = find_font("VGA8x16");
  296. struct vimc_device *vimc;
  297. int ret;
  298. dev_dbg(&pdev->dev, "probe");
  299. if (!font) {
  300. dev_err(&pdev->dev, "could not find font\n");
  301. return -ENODEV;
  302. }
  303. tpg_set_font(font->data);
  304. if (vimc_allocator == VIMC_ALLOCATOR_DMA_CONTIG)
  305. dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  306. vimc = kzalloc(sizeof(*vimc), GFP_KERNEL);
  307. if (!vimc)
  308. return -ENOMEM;
  309. vimc->pipe_cfg = &pipe_cfg;
  310. /* Link the media device within the v4l2_device */
  311. vimc->v4l2_dev.mdev = &vimc->mdev;
  312. /* Initialize media device */
  313. strscpy(vimc->mdev.model, VIMC_MDEV_MODEL_NAME,
  314. sizeof(vimc->mdev.model));
  315. snprintf(vimc->mdev.bus_info, sizeof(vimc->mdev.bus_info),
  316. "platform:%s", VIMC_PDEV_NAME);
  317. vimc->mdev.dev = &pdev->dev;
  318. media_device_init(&vimc->mdev);
  319. ret = vimc_register_devices(vimc);
  320. if (ret) {
  321. media_device_cleanup(&vimc->mdev);
  322. kfree(vimc);
  323. return ret;
  324. }
  325. /*
  326. * the release cb is set only after successful registration.
  327. * if the registration fails, we release directly from probe
  328. */
  329. vimc->v4l2_dev.release = vimc_v4l2_dev_release;
  330. platform_set_drvdata(pdev, vimc);
  331. return 0;
  332. }
  333. static void vimc_remove(struct platform_device *pdev)
  334. {
  335. struct vimc_device *vimc = platform_get_drvdata(pdev);
  336. dev_dbg(&pdev->dev, "remove");
  337. vimc_unregister_subdevs(vimc);
  338. media_device_unregister(&vimc->mdev);
  339. v4l2_device_unregister(&vimc->v4l2_dev);
  340. v4l2_device_put(&vimc->v4l2_dev);
  341. }
  342. static void vimc_dev_release(struct device *dev)
  343. {
  344. }
  345. static struct platform_device vimc_pdev = {
  346. .name = VIMC_PDEV_NAME,
  347. .dev.release = vimc_dev_release,
  348. };
  349. static struct platform_driver vimc_pdrv = {
  350. .probe = vimc_probe,
  351. .remove_new = vimc_remove,
  352. .driver = {
  353. .name = VIMC_PDEV_NAME,
  354. },
  355. };
  356. static int __init vimc_init(void)
  357. {
  358. int ret;
  359. ret = platform_device_register(&vimc_pdev);
  360. if (ret) {
  361. dev_err(&vimc_pdev.dev,
  362. "platform device registration failed (err=%d)\n", ret);
  363. return ret;
  364. }
  365. ret = platform_driver_register(&vimc_pdrv);
  366. if (ret) {
  367. dev_err(&vimc_pdev.dev,
  368. "platform driver registration failed (err=%d)\n", ret);
  369. platform_device_unregister(&vimc_pdev);
  370. return ret;
  371. }
  372. return 0;
  373. }
  374. static void __exit vimc_exit(void)
  375. {
  376. platform_driver_unregister(&vimc_pdrv);
  377. platform_device_unregister(&vimc_pdev);
  378. }
  379. module_init(vimc_init);
  380. module_exit(vimc_exit);
  381. MODULE_DESCRIPTION("Virtual Media Controller Driver (VIMC)");
  382. MODULE_AUTHOR("Helen Fornazier <helen.fornazier@gmail.com>");
  383. MODULE_LICENSE("GPL");