xilinx-vipp.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  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_entity_create_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_entity_create_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 ret;
  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. struct device_node *next;
  297. int ret = 0;
  298. dev_dbg(xdev->dev, "parsing node %s\n", node->full_name);
  299. while (1) {
  300. next = of_graph_get_next_endpoint(node, ep);
  301. if (next == NULL)
  302. break;
  303. of_node_put(ep);
  304. ep = next;
  305. dev_dbg(xdev->dev, "handling endpoint %s\n", ep->full_name);
  306. remote = of_graph_get_remote_port_parent(ep);
  307. if (remote == NULL) {
  308. ret = -EINVAL;
  309. break;
  310. }
  311. /* Skip entities that we have already processed. */
  312. if (remote == xdev->dev->of_node ||
  313. xvip_graph_find_entity(xdev, remote)) {
  314. of_node_put(remote);
  315. continue;
  316. }
  317. entity = devm_kzalloc(xdev->dev, sizeof(*entity), GFP_KERNEL);
  318. if (entity == NULL) {
  319. of_node_put(remote);
  320. ret = -ENOMEM;
  321. break;
  322. }
  323. entity->node = remote;
  324. entity->asd.match_type = V4L2_ASYNC_MATCH_OF;
  325. entity->asd.match.of.node = remote;
  326. list_add_tail(&entity->list, &xdev->entities);
  327. xdev->num_subdevs++;
  328. }
  329. of_node_put(ep);
  330. return ret;
  331. }
  332. static int xvip_graph_parse(struct xvip_composite_device *xdev)
  333. {
  334. struct xvip_graph_entity *entity;
  335. int ret;
  336. /*
  337. * Walk the links to parse the full graph. Start by parsing the
  338. * composite node and then parse entities in turn. The list_for_each
  339. * loop will handle entities added at the end of the list while walking
  340. * the links.
  341. */
  342. ret = xvip_graph_parse_one(xdev, xdev->dev->of_node);
  343. if (ret < 0)
  344. return 0;
  345. list_for_each_entry(entity, &xdev->entities, list) {
  346. ret = xvip_graph_parse_one(xdev, entity->node);
  347. if (ret < 0)
  348. break;
  349. }
  350. return ret;
  351. }
  352. static int xvip_graph_dma_init_one(struct xvip_composite_device *xdev,
  353. struct device_node *node)
  354. {
  355. struct xvip_dma *dma;
  356. enum v4l2_buf_type type;
  357. const char *direction;
  358. unsigned int index;
  359. int ret;
  360. ret = of_property_read_string(node, "direction", &direction);
  361. if (ret < 0)
  362. return ret;
  363. if (strcmp(direction, "input") == 0)
  364. type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  365. else if (strcmp(direction, "output") == 0)
  366. type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  367. else
  368. return -EINVAL;
  369. of_property_read_u32(node, "reg", &index);
  370. dma = devm_kzalloc(xdev->dev, sizeof(*dma), GFP_KERNEL);
  371. if (dma == NULL)
  372. return -ENOMEM;
  373. ret = xvip_dma_init(xdev, dma, type, index);
  374. if (ret < 0) {
  375. dev_err(xdev->dev, "%s initialization failed\n",
  376. node->full_name);
  377. return ret;
  378. }
  379. list_add_tail(&dma->list, &xdev->dmas);
  380. xdev->v4l2_caps |= type == V4L2_BUF_TYPE_VIDEO_CAPTURE
  381. ? V4L2_CAP_VIDEO_CAPTURE : V4L2_CAP_VIDEO_OUTPUT;
  382. return 0;
  383. }
  384. static int xvip_graph_dma_init(struct xvip_composite_device *xdev)
  385. {
  386. struct device_node *ports;
  387. struct device_node *port;
  388. int ret;
  389. ports = of_get_child_by_name(xdev->dev->of_node, "ports");
  390. if (ports == NULL) {
  391. dev_err(xdev->dev, "ports node not present\n");
  392. return -EINVAL;
  393. }
  394. for_each_child_of_node(ports, port) {
  395. ret = xvip_graph_dma_init_one(xdev, port);
  396. if (ret < 0)
  397. return ret;
  398. }
  399. return 0;
  400. }
  401. static void xvip_graph_cleanup(struct xvip_composite_device *xdev)
  402. {
  403. struct xvip_graph_entity *entityp;
  404. struct xvip_graph_entity *entity;
  405. struct xvip_dma *dmap;
  406. struct xvip_dma *dma;
  407. v4l2_async_notifier_unregister(&xdev->notifier);
  408. list_for_each_entry_safe(entity, entityp, &xdev->entities, list) {
  409. of_node_put(entity->node);
  410. list_del(&entity->list);
  411. }
  412. list_for_each_entry_safe(dma, dmap, &xdev->dmas, list) {
  413. xvip_dma_cleanup(dma);
  414. list_del(&dma->list);
  415. }
  416. }
  417. static int xvip_graph_init(struct xvip_composite_device *xdev)
  418. {
  419. struct xvip_graph_entity *entity;
  420. struct v4l2_async_subdev **subdevs = NULL;
  421. unsigned int num_subdevs;
  422. unsigned int i;
  423. int ret;
  424. /* Init the DMA channels. */
  425. ret = xvip_graph_dma_init(xdev);
  426. if (ret < 0) {
  427. dev_err(xdev->dev, "DMA initialization failed\n");
  428. goto done;
  429. }
  430. /* Parse the graph to extract a list of subdevice DT nodes. */
  431. ret = xvip_graph_parse(xdev);
  432. if (ret < 0) {
  433. dev_err(xdev->dev, "graph parsing failed\n");
  434. goto done;
  435. }
  436. if (!xdev->num_subdevs) {
  437. dev_err(xdev->dev, "no subdev found in graph\n");
  438. goto done;
  439. }
  440. /* Register the subdevices notifier. */
  441. num_subdevs = xdev->num_subdevs;
  442. subdevs = devm_kzalloc(xdev->dev, sizeof(*subdevs) * num_subdevs,
  443. GFP_KERNEL);
  444. if (subdevs == NULL) {
  445. ret = -ENOMEM;
  446. goto done;
  447. }
  448. i = 0;
  449. list_for_each_entry(entity, &xdev->entities, list)
  450. subdevs[i++] = &entity->asd;
  451. xdev->notifier.subdevs = subdevs;
  452. xdev->notifier.num_subdevs = num_subdevs;
  453. xdev->notifier.bound = xvip_graph_notify_bound;
  454. xdev->notifier.complete = xvip_graph_notify_complete;
  455. ret = v4l2_async_notifier_register(&xdev->v4l2_dev, &xdev->notifier);
  456. if (ret < 0) {
  457. dev_err(xdev->dev, "notifier registration failed\n");
  458. goto done;
  459. }
  460. ret = 0;
  461. done:
  462. if (ret < 0)
  463. xvip_graph_cleanup(xdev);
  464. return ret;
  465. }
  466. /* -----------------------------------------------------------------------------
  467. * Media Controller and V4L2
  468. */
  469. static void xvip_composite_v4l2_cleanup(struct xvip_composite_device *xdev)
  470. {
  471. v4l2_device_unregister(&xdev->v4l2_dev);
  472. media_device_unregister(&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. ret = media_device_register(&xdev->media_dev);
  482. if (ret < 0) {
  483. dev_err(xdev->dev, "media device registration failed (%d)\n",
  484. ret);
  485. return ret;
  486. }
  487. xdev->v4l2_dev.mdev = &xdev->media_dev;
  488. ret = v4l2_device_register(xdev->dev, &xdev->v4l2_dev);
  489. if (ret < 0) {
  490. dev_err(xdev->dev, "V4L2 device registration failed (%d)\n",
  491. ret);
  492. media_device_unregister(&xdev->media_dev);
  493. return ret;
  494. }
  495. return 0;
  496. }
  497. /* -----------------------------------------------------------------------------
  498. * Platform Device Driver
  499. */
  500. static int xvip_composite_probe(struct platform_device *pdev)
  501. {
  502. struct xvip_composite_device *xdev;
  503. int ret;
  504. xdev = devm_kzalloc(&pdev->dev, sizeof(*xdev), GFP_KERNEL);
  505. if (!xdev)
  506. return -ENOMEM;
  507. xdev->dev = &pdev->dev;
  508. INIT_LIST_HEAD(&xdev->entities);
  509. INIT_LIST_HEAD(&xdev->dmas);
  510. ret = xvip_composite_v4l2_init(xdev);
  511. if (ret < 0)
  512. return ret;
  513. ret = xvip_graph_init(xdev);
  514. if (ret < 0)
  515. goto error;
  516. platform_set_drvdata(pdev, xdev);
  517. dev_info(xdev->dev, "device registered\n");
  518. return 0;
  519. error:
  520. xvip_composite_v4l2_cleanup(xdev);
  521. return ret;
  522. }
  523. static int xvip_composite_remove(struct platform_device *pdev)
  524. {
  525. struct xvip_composite_device *xdev = platform_get_drvdata(pdev);
  526. xvip_graph_cleanup(xdev);
  527. xvip_composite_v4l2_cleanup(xdev);
  528. return 0;
  529. }
  530. static const struct of_device_id xvip_composite_of_id_table[] = {
  531. { .compatible = "xlnx,video" },
  532. { }
  533. };
  534. MODULE_DEVICE_TABLE(of, xvip_composite_of_id_table);
  535. static struct platform_driver xvip_composite_driver = {
  536. .driver = {
  537. .name = "xilinx-video",
  538. .of_match_table = xvip_composite_of_id_table,
  539. },
  540. .probe = xvip_composite_probe,
  541. .remove = xvip_composite_remove,
  542. };
  543. module_platform_driver(xvip_composite_driver);
  544. MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
  545. MODULE_DESCRIPTION("Xilinx Video IP Composite Driver");
  546. MODULE_LICENSE("GPL v2");