xilinx-vipp.c 16 KB

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