xilinx-vipp.c 15 KB

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