xilinx-vipp.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Xilinx Video IP Composite Device
  4. *
  5. * Copyright (C) 2013-2015 Ideas on Board
  6. * Copyright (C) 2013-2015 Xilinx, Inc.
  7. *
  8. * Contacts: Hyun Kwon <hyun.kwon@xilinx.com>
  9. * Laurent Pinchart <laurent.pinchart@ideasonboard.com>
  10. */
  11. #include <linux/list.h>
  12. #include <linux/module.h>
  13. #include <linux/of.h>
  14. #include <linux/of_graph.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/slab.h>
  17. #include <media/v4l2-async.h>
  18. #include <media/v4l2-common.h>
  19. #include <media/v4l2-device.h>
  20. #include <media/v4l2-fwnode.h>
  21. #include "xilinx-dma.h"
  22. #include "xilinx-vipp.h"
  23. #define XVIPP_DMA_S2MM 0
  24. #define XVIPP_DMA_MM2S 1
  25. /**
  26. * struct xvip_graph_entity - Entity in the video graph
  27. * @asd: subdev asynchronous registration information
  28. * @entity: media entity, from the corresponding V4L2 subdev
  29. * @subdev: V4L2 subdev
  30. */
  31. struct xvip_graph_entity {
  32. struct v4l2_async_connection asd; /* must be first */
  33. struct media_entity *entity;
  34. struct v4l2_subdev *subdev;
  35. };
  36. static inline struct xvip_graph_entity *
  37. to_xvip_entity(struct v4l2_async_connection *asd)
  38. {
  39. return container_of(asd, struct xvip_graph_entity, asd);
  40. }
  41. /* -----------------------------------------------------------------------------
  42. * Graph Management
  43. */
  44. static struct xvip_graph_entity *
  45. xvip_graph_find_entity(struct xvip_composite_device *xdev,
  46. const struct fwnode_handle *fwnode)
  47. {
  48. struct xvip_graph_entity *entity;
  49. struct v4l2_async_connection *asd;
  50. struct list_head *lists[] = {
  51. &xdev->notifier.done_list,
  52. &xdev->notifier.waiting_list
  53. };
  54. unsigned int i;
  55. for (i = 0; i < ARRAY_SIZE(lists); i++) {
  56. list_for_each_entry(asd, lists[i], asc_entry) {
  57. entity = to_xvip_entity(asd);
  58. if (entity->asd.match.fwnode == fwnode)
  59. return entity;
  60. }
  61. }
  62. return NULL;
  63. }
  64. static int xvip_graph_build_one(struct xvip_composite_device *xdev,
  65. struct xvip_graph_entity *entity)
  66. {
  67. u32 link_flags = MEDIA_LNK_FL_ENABLED;
  68. struct media_entity *local = entity->entity;
  69. struct media_entity *remote;
  70. struct media_pad *local_pad;
  71. struct media_pad *remote_pad;
  72. struct xvip_graph_entity *ent;
  73. struct v4l2_fwnode_link link;
  74. struct fwnode_handle *ep = NULL;
  75. int ret = 0;
  76. dev_dbg(xdev->dev, "creating links for entity %s\n", local->name);
  77. while (1) {
  78. /* Get the next endpoint and parse its link. */
  79. ep = fwnode_graph_get_next_endpoint(entity->asd.match.fwnode,
  80. ep);
  81. if (ep == NULL)
  82. break;
  83. dev_dbg(xdev->dev, "processing endpoint %p\n", ep);
  84. ret = v4l2_fwnode_parse_link(ep, &link);
  85. if (ret < 0) {
  86. dev_err(xdev->dev, "failed to parse link for %p\n",
  87. ep);
  88. continue;
  89. }
  90. /* Skip sink ports, they will be processed from the other end of
  91. * the link.
  92. */
  93. if (link.local_port >= local->num_pads) {
  94. dev_err(xdev->dev, "invalid port number %u for %p\n",
  95. link.local_port, link.local_node);
  96. v4l2_fwnode_put_link(&link);
  97. ret = -EINVAL;
  98. break;
  99. }
  100. local_pad = &local->pads[link.local_port];
  101. if (local_pad->flags & MEDIA_PAD_FL_SINK) {
  102. dev_dbg(xdev->dev, "skipping sink port %p:%u\n",
  103. link.local_node, link.local_port);
  104. v4l2_fwnode_put_link(&link);
  105. continue;
  106. }
  107. /* Skip DMA engines, they will be processed separately. */
  108. if (link.remote_node == of_fwnode_handle(xdev->dev->of_node)) {
  109. dev_dbg(xdev->dev, "skipping DMA port %p:%u\n",
  110. link.local_node, link.local_port);
  111. v4l2_fwnode_put_link(&link);
  112. continue;
  113. }
  114. /* Find the remote entity. */
  115. ent = xvip_graph_find_entity(xdev, link.remote_node);
  116. if (ent == NULL) {
  117. dev_err(xdev->dev, "no entity found for %p\n",
  118. link.remote_node);
  119. v4l2_fwnode_put_link(&link);
  120. ret = -ENODEV;
  121. break;
  122. }
  123. remote = ent->entity;
  124. if (link.remote_port >= remote->num_pads) {
  125. dev_err(xdev->dev, "invalid port number %u on %p\n",
  126. link.remote_port, link.remote_node);
  127. v4l2_fwnode_put_link(&link);
  128. ret = -EINVAL;
  129. break;
  130. }
  131. remote_pad = &remote->pads[link.remote_port];
  132. v4l2_fwnode_put_link(&link);
  133. /* Create the media link. */
  134. dev_dbg(xdev->dev, "creating %s:%u -> %s:%u link\n",
  135. local->name, local_pad->index,
  136. remote->name, remote_pad->index);
  137. ret = media_create_pad_link(local, local_pad->index,
  138. remote, remote_pad->index,
  139. link_flags);
  140. if (ret < 0) {
  141. dev_err(xdev->dev,
  142. "failed to create %s:%u -> %s:%u link\n",
  143. local->name, local_pad->index,
  144. remote->name, remote_pad->index);
  145. break;
  146. }
  147. }
  148. fwnode_handle_put(ep);
  149. return ret;
  150. }
  151. static struct xvip_dma *
  152. xvip_graph_find_dma(struct xvip_composite_device *xdev, unsigned int port)
  153. {
  154. struct xvip_dma *dma;
  155. list_for_each_entry(dma, &xdev->dmas, list) {
  156. if (dma->port == port)
  157. return dma;
  158. }
  159. return NULL;
  160. }
  161. static int xvip_graph_build_dma(struct xvip_composite_device *xdev)
  162. {
  163. u32 link_flags = MEDIA_LNK_FL_ENABLED;
  164. struct device_node *node = xdev->dev->of_node;
  165. struct media_entity *source;
  166. struct media_entity *sink;
  167. struct media_pad *source_pad;
  168. struct media_pad *sink_pad;
  169. struct xvip_graph_entity *ent;
  170. struct v4l2_fwnode_link link;
  171. struct device_node *ep;
  172. struct xvip_dma *dma;
  173. int ret = 0;
  174. dev_dbg(xdev->dev, "creating links for DMA engines\n");
  175. for_each_endpoint_of_node(node, ep) {
  176. dev_dbg(xdev->dev, "processing endpoint %pOF\n", ep);
  177. ret = v4l2_fwnode_parse_link(of_fwnode_handle(ep), &link);
  178. if (ret < 0) {
  179. dev_err(xdev->dev, "failed to parse link for %pOF\n",
  180. ep);
  181. continue;
  182. }
  183. /* Find the DMA engine. */
  184. dma = xvip_graph_find_dma(xdev, link.local_port);
  185. if (dma == NULL) {
  186. dev_err(xdev->dev, "no DMA engine found for port %u\n",
  187. link.local_port);
  188. v4l2_fwnode_put_link(&link);
  189. ret = -EINVAL;
  190. break;
  191. }
  192. dev_dbg(xdev->dev, "creating link for DMA engine %s\n",
  193. dma->video.name);
  194. /* Find the remote entity. */
  195. ent = xvip_graph_find_entity(xdev, link.remote_node);
  196. if (ent == NULL) {
  197. dev_err(xdev->dev, "no entity found for %pOF\n",
  198. to_of_node(link.remote_node));
  199. v4l2_fwnode_put_link(&link);
  200. ret = -ENODEV;
  201. break;
  202. }
  203. if (link.remote_port >= ent->entity->num_pads) {
  204. dev_err(xdev->dev, "invalid port number %u on %pOF\n",
  205. link.remote_port,
  206. to_of_node(link.remote_node));
  207. v4l2_fwnode_put_link(&link);
  208. ret = -EINVAL;
  209. break;
  210. }
  211. if (dma->pad.flags & MEDIA_PAD_FL_SOURCE) {
  212. source = &dma->video.entity;
  213. source_pad = &dma->pad;
  214. sink = ent->entity;
  215. sink_pad = &sink->pads[link.remote_port];
  216. } else {
  217. source = ent->entity;
  218. source_pad = &source->pads[link.remote_port];
  219. sink = &dma->video.entity;
  220. sink_pad = &dma->pad;
  221. }
  222. v4l2_fwnode_put_link(&link);
  223. /* Create the media link. */
  224. dev_dbg(xdev->dev, "creating %s:%u -> %s:%u link\n",
  225. source->name, source_pad->index,
  226. sink->name, sink_pad->index);
  227. ret = media_create_pad_link(source, source_pad->index,
  228. sink, sink_pad->index,
  229. link_flags);
  230. if (ret < 0) {
  231. dev_err(xdev->dev,
  232. "failed to create %s:%u -> %s:%u link\n",
  233. source->name, source_pad->index,
  234. sink->name, sink_pad->index);
  235. break;
  236. }
  237. }
  238. of_node_put(ep);
  239. return ret;
  240. }
  241. static int xvip_graph_notify_complete(struct v4l2_async_notifier *notifier)
  242. {
  243. struct xvip_composite_device *xdev =
  244. container_of(notifier, struct xvip_composite_device, notifier);
  245. struct xvip_graph_entity *entity;
  246. struct v4l2_async_connection *asd;
  247. int ret;
  248. dev_dbg(xdev->dev, "notify complete, all subdevs registered\n");
  249. /* Create links for every entity. */
  250. list_for_each_entry(asd, &xdev->notifier.done_list, asc_entry) {
  251. entity = to_xvip_entity(asd);
  252. ret = xvip_graph_build_one(xdev, entity);
  253. if (ret < 0)
  254. return ret;
  255. }
  256. /* Create links for DMA channels. */
  257. ret = xvip_graph_build_dma(xdev);
  258. if (ret < 0)
  259. return ret;
  260. ret = v4l2_device_register_subdev_nodes(&xdev->v4l2_dev);
  261. if (ret < 0)
  262. dev_err(xdev->dev, "failed to register subdev nodes\n");
  263. return media_device_register(&xdev->media_dev);
  264. }
  265. static int xvip_graph_notify_bound(struct v4l2_async_notifier *notifier,
  266. struct v4l2_subdev *subdev,
  267. struct v4l2_async_connection *asc)
  268. {
  269. struct xvip_graph_entity *entity = to_xvip_entity(asc);
  270. entity->entity = &subdev->entity;
  271. entity->subdev = subdev;
  272. return 0;
  273. }
  274. static const struct v4l2_async_notifier_operations xvip_graph_notify_ops = {
  275. .bound = xvip_graph_notify_bound,
  276. .complete = xvip_graph_notify_complete,
  277. };
  278. static int xvip_graph_parse_one(struct xvip_composite_device *xdev,
  279. struct fwnode_handle *fwnode)
  280. {
  281. struct fwnode_handle *remote;
  282. struct fwnode_handle *ep = NULL;
  283. int ret = 0;
  284. dev_dbg(xdev->dev, "parsing node %p\n", fwnode);
  285. while (1) {
  286. struct xvip_graph_entity *xge;
  287. ep = fwnode_graph_get_next_endpoint(fwnode, ep);
  288. if (ep == NULL)
  289. break;
  290. dev_dbg(xdev->dev, "handling endpoint %p\n", ep);
  291. remote = fwnode_graph_get_remote_port_parent(ep);
  292. if (remote == NULL) {
  293. ret = -EINVAL;
  294. goto err_notifier_cleanup;
  295. }
  296. fwnode_handle_put(ep);
  297. /* Skip entities that we have already processed. */
  298. if (remote == of_fwnode_handle(xdev->dev->of_node) ||
  299. xvip_graph_find_entity(xdev, remote)) {
  300. fwnode_handle_put(remote);
  301. continue;
  302. }
  303. xge = v4l2_async_nf_add_fwnode(&xdev->notifier, remote,
  304. struct xvip_graph_entity);
  305. fwnode_handle_put(remote);
  306. if (IS_ERR(xge)) {
  307. ret = PTR_ERR(xge);
  308. goto err_notifier_cleanup;
  309. }
  310. }
  311. return 0;
  312. err_notifier_cleanup:
  313. v4l2_async_nf_cleanup(&xdev->notifier);
  314. fwnode_handle_put(ep);
  315. return ret;
  316. }
  317. static int xvip_graph_parse(struct xvip_composite_device *xdev)
  318. {
  319. struct xvip_graph_entity *entity;
  320. struct v4l2_async_connection *asd;
  321. int ret;
  322. /*
  323. * Walk the links to parse the full graph. Start by parsing the
  324. * composite node and then parse entities in turn. The list_for_each
  325. * loop will handle entities added at the end of the list while walking
  326. * the links.
  327. */
  328. ret = xvip_graph_parse_one(xdev, of_fwnode_handle(xdev->dev->of_node));
  329. if (ret < 0)
  330. return 0;
  331. list_for_each_entry(asd, &xdev->notifier.waiting_list, asc_entry) {
  332. entity = to_xvip_entity(asd);
  333. ret = xvip_graph_parse_one(xdev, entity->asd.match.fwnode);
  334. if (ret < 0) {
  335. v4l2_async_nf_cleanup(&xdev->notifier);
  336. break;
  337. }
  338. }
  339. return ret;
  340. }
  341. static int xvip_graph_dma_init_one(struct xvip_composite_device *xdev,
  342. struct device_node *node)
  343. {
  344. struct xvip_dma *dma;
  345. enum v4l2_buf_type type;
  346. const char *direction;
  347. unsigned int index;
  348. int ret;
  349. ret = of_property_read_string(node, "direction", &direction);
  350. if (ret < 0)
  351. return ret;
  352. if (strcmp(direction, "input") == 0)
  353. type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  354. else if (strcmp(direction, "output") == 0)
  355. type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  356. else
  357. return -EINVAL;
  358. of_property_read_u32(node, "reg", &index);
  359. dma = devm_kzalloc(xdev->dev, sizeof(*dma), GFP_KERNEL);
  360. if (dma == NULL)
  361. return -ENOMEM;
  362. ret = xvip_dma_init(xdev, dma, type, index);
  363. if (ret < 0) {
  364. dev_err(xdev->dev, "%pOF initialization failed\n", node);
  365. return ret;
  366. }
  367. list_add_tail(&dma->list, &xdev->dmas);
  368. xdev->v4l2_caps |= type == V4L2_BUF_TYPE_VIDEO_CAPTURE
  369. ? V4L2_CAP_VIDEO_CAPTURE : V4L2_CAP_VIDEO_OUTPUT;
  370. return 0;
  371. }
  372. static int xvip_graph_dma_init(struct xvip_composite_device *xdev)
  373. {
  374. struct device_node *ports;
  375. struct device_node *port;
  376. int ret = 0;
  377. ports = of_get_child_by_name(xdev->dev->of_node, "ports");
  378. if (ports == NULL) {
  379. dev_err(xdev->dev, "ports node not present\n");
  380. return -EINVAL;
  381. }
  382. for_each_child_of_node(ports, port) {
  383. ret = xvip_graph_dma_init_one(xdev, port);
  384. if (ret) {
  385. of_node_put(port);
  386. break;
  387. }
  388. }
  389. of_node_put(ports);
  390. return ret;
  391. }
  392. static void xvip_graph_cleanup(struct xvip_composite_device *xdev)
  393. {
  394. struct xvip_dma *dmap;
  395. struct xvip_dma *dma;
  396. v4l2_async_nf_unregister(&xdev->notifier);
  397. v4l2_async_nf_cleanup(&xdev->notifier);
  398. list_for_each_entry_safe(dma, dmap, &xdev->dmas, list) {
  399. xvip_dma_cleanup(dma);
  400. list_del(&dma->list);
  401. }
  402. }
  403. static int xvip_graph_init(struct xvip_composite_device *xdev)
  404. {
  405. int ret;
  406. /* Init the DMA channels. */
  407. ret = xvip_graph_dma_init(xdev);
  408. if (ret < 0) {
  409. dev_err(xdev->dev, "DMA initialization failed\n");
  410. goto done;
  411. }
  412. v4l2_async_nf_init(&xdev->notifier, &xdev->v4l2_dev);
  413. /* Parse the graph to extract a list of subdevice DT nodes. */
  414. ret = xvip_graph_parse(xdev);
  415. if (ret < 0) {
  416. dev_err(xdev->dev, "graph parsing failed\n");
  417. goto done;
  418. }
  419. if (list_empty(&xdev->notifier.waiting_list)) {
  420. dev_err(xdev->dev, "no subdev found in graph\n");
  421. ret = -ENOENT;
  422. goto done;
  423. }
  424. /* Register the subdevices notifier. */
  425. xdev->notifier.ops = &xvip_graph_notify_ops;
  426. ret = v4l2_async_nf_register(&xdev->notifier);
  427. if (ret < 0) {
  428. dev_err(xdev->dev, "notifier registration failed\n");
  429. goto done;
  430. }
  431. ret = 0;
  432. done:
  433. if (ret < 0)
  434. xvip_graph_cleanup(xdev);
  435. return ret;
  436. }
  437. /* -----------------------------------------------------------------------------
  438. * Media Controller and V4L2
  439. */
  440. static void xvip_composite_v4l2_cleanup(struct xvip_composite_device *xdev)
  441. {
  442. v4l2_device_unregister(&xdev->v4l2_dev);
  443. media_device_unregister(&xdev->media_dev);
  444. media_device_cleanup(&xdev->media_dev);
  445. }
  446. static int xvip_composite_v4l2_init(struct xvip_composite_device *xdev)
  447. {
  448. int ret;
  449. xdev->media_dev.dev = xdev->dev;
  450. strscpy(xdev->media_dev.model, "Xilinx Video Composite Device",
  451. sizeof(xdev->media_dev.model));
  452. xdev->media_dev.hw_revision = 0;
  453. media_device_init(&xdev->media_dev);
  454. xdev->v4l2_dev.mdev = &xdev->media_dev;
  455. ret = v4l2_device_register(xdev->dev, &xdev->v4l2_dev);
  456. if (ret < 0) {
  457. dev_err(xdev->dev, "V4L2 device registration failed (%d)\n",
  458. ret);
  459. media_device_cleanup(&xdev->media_dev);
  460. return ret;
  461. }
  462. return 0;
  463. }
  464. /* -----------------------------------------------------------------------------
  465. * Platform Device Driver
  466. */
  467. static int xvip_composite_probe(struct platform_device *pdev)
  468. {
  469. struct xvip_composite_device *xdev;
  470. int ret;
  471. xdev = devm_kzalloc(&pdev->dev, sizeof(*xdev), GFP_KERNEL);
  472. if (!xdev)
  473. return -ENOMEM;
  474. xdev->dev = &pdev->dev;
  475. INIT_LIST_HEAD(&xdev->dmas);
  476. ret = xvip_composite_v4l2_init(xdev);
  477. if (ret < 0)
  478. return ret;
  479. ret = xvip_graph_init(xdev);
  480. if (ret < 0)
  481. goto error;
  482. platform_set_drvdata(pdev, xdev);
  483. dev_info(xdev->dev, "device registered\n");
  484. return 0;
  485. error:
  486. xvip_composite_v4l2_cleanup(xdev);
  487. return ret;
  488. }
  489. static void xvip_composite_remove(struct platform_device *pdev)
  490. {
  491. struct xvip_composite_device *xdev = platform_get_drvdata(pdev);
  492. xvip_graph_cleanup(xdev);
  493. xvip_composite_v4l2_cleanup(xdev);
  494. }
  495. static const struct of_device_id xvip_composite_of_id_table[] = {
  496. { .compatible = "xlnx,video" },
  497. { }
  498. };
  499. MODULE_DEVICE_TABLE(of, xvip_composite_of_id_table);
  500. static struct platform_driver xvip_composite_driver = {
  501. .driver = {
  502. .name = "xilinx-video",
  503. .of_match_table = xvip_composite_of_id_table,
  504. },
  505. .probe = xvip_composite_probe,
  506. .remove_new = xvip_composite_remove,
  507. };
  508. module_platform_driver(xvip_composite_driver);
  509. MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
  510. MODULE_DESCRIPTION("Xilinx Video IP Composite Driver");
  511. MODULE_LICENSE("GPL v2");