xilinx-vipp.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  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-fwnode.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_fwnode_link link;
  67. struct device_node *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 = of_graph_get_next_endpoint(entity->node, ep);
  73. if (ep == NULL)
  74. break;
  75. dev_dbg(xdev->dev, "processing endpoint %pOF\n", ep);
  76. ret = v4l2_fwnode_parse_link(of_fwnode_handle(ep), &link);
  77. if (ret < 0) {
  78. dev_err(xdev->dev, "failed to parse link for %pOF\n",
  79. ep);
  80. continue;
  81. }
  82. /* Skip sink ports, they will be processed from the other end of
  83. * the link.
  84. */
  85. if (link.local_port >= local->num_pads) {
  86. dev_err(xdev->dev, "invalid port number %u for %pOF\n",
  87. link.local_port,
  88. to_of_node(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 %pOF:%u\n",
  96. to_of_node(link.local_node),
  97. link.local_port);
  98. v4l2_fwnode_put_link(&link);
  99. continue;
  100. }
  101. /* Skip DMA engines, they will be processed separately. */
  102. if (link.remote_node == of_fwnode_handle(xdev->dev->of_node)) {
  103. dev_dbg(xdev->dev, "skipping DMA port %pOF:%u\n",
  104. to_of_node(link.local_node),
  105. link.local_port);
  106. v4l2_fwnode_put_link(&link);
  107. continue;
  108. }
  109. /* Find the remote entity. */
  110. ent = xvip_graph_find_entity(xdev,
  111. to_of_node(link.remote_node));
  112. if (ent == NULL) {
  113. dev_err(xdev->dev, "no entity found for %pOF\n",
  114. to_of_node(link.remote_node));
  115. v4l2_fwnode_put_link(&link);
  116. ret = -ENODEV;
  117. break;
  118. }
  119. remote = ent->entity;
  120. if (link.remote_port >= remote->num_pads) {
  121. dev_err(xdev->dev, "invalid port number %u on %pOF\n",
  122. link.remote_port, to_of_node(link.remote_node));
  123. v4l2_fwnode_put_link(&link);
  124. ret = -EINVAL;
  125. break;
  126. }
  127. remote_pad = &remote->pads[link.remote_port];
  128. v4l2_fwnode_put_link(&link);
  129. /* Create the media link. */
  130. dev_dbg(xdev->dev, "creating %s:%u -> %s:%u link\n",
  131. local->name, local_pad->index,
  132. remote->name, remote_pad->index);
  133. ret = media_create_pad_link(local, local_pad->index,
  134. remote, remote_pad->index,
  135. link_flags);
  136. if (ret < 0) {
  137. dev_err(xdev->dev,
  138. "failed to create %s:%u -> %s:%u link\n",
  139. local->name, local_pad->index,
  140. remote->name, remote_pad->index);
  141. break;
  142. }
  143. }
  144. of_node_put(ep);
  145. return ret;
  146. }
  147. static struct xvip_dma *
  148. xvip_graph_find_dma(struct xvip_composite_device *xdev, unsigned int port)
  149. {
  150. struct xvip_dma *dma;
  151. list_for_each_entry(dma, &xdev->dmas, list) {
  152. if (dma->port == port)
  153. return dma;
  154. }
  155. return NULL;
  156. }
  157. static int xvip_graph_build_dma(struct xvip_composite_device *xdev)
  158. {
  159. u32 link_flags = MEDIA_LNK_FL_ENABLED;
  160. struct device_node *node = xdev->dev->of_node;
  161. struct media_entity *source;
  162. struct media_entity *sink;
  163. struct media_pad *source_pad;
  164. struct media_pad *sink_pad;
  165. struct xvip_graph_entity *ent;
  166. struct v4l2_fwnode_link link;
  167. struct device_node *ep = NULL;
  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. ep = of_graph_get_next_endpoint(node, ep);
  174. if (ep == NULL)
  175. break;
  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,
  196. to_of_node(link.remote_node));
  197. if (ent == NULL) {
  198. dev_err(xdev->dev, "no entity found for %pOF\n",
  199. to_of_node(link.remote_node));
  200. v4l2_fwnode_put_link(&link);
  201. ret = -ENODEV;
  202. break;
  203. }
  204. if (link.remote_port >= ent->entity->num_pads) {
  205. dev_err(xdev->dev, "invalid port number %u on %pOF\n",
  206. link.remote_port,
  207. to_of_node(link.remote_node));
  208. v4l2_fwnode_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_fwnode_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 %pOF\n",
  279. entity->node);
  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 device_node *node)
  296. {
  297. struct xvip_graph_entity *entity;
  298. struct device_node *remote;
  299. struct device_node *ep = NULL;
  300. int ret = 0;
  301. dev_dbg(xdev->dev, "parsing node %pOF\n", node);
  302. while (1) {
  303. ep = of_graph_get_next_endpoint(node, ep);
  304. if (ep == NULL)
  305. break;
  306. dev_dbg(xdev->dev, "handling endpoint %pOF\n", ep);
  307. remote = of_graph_get_remote_port_parent(ep);
  308. if (remote == NULL) {
  309. ret = -EINVAL;
  310. break;
  311. }
  312. /* Skip entities that we have already processed. */
  313. if (remote == xdev->dev->of_node ||
  314. xvip_graph_find_entity(xdev, remote)) {
  315. of_node_put(remote);
  316. continue;
  317. }
  318. entity = devm_kzalloc(xdev->dev, sizeof(*entity), GFP_KERNEL);
  319. if (entity == NULL) {
  320. of_node_put(remote);
  321. ret = -ENOMEM;
  322. break;
  323. }
  324. entity->node = remote;
  325. entity->asd.match_type = V4L2_ASYNC_MATCH_FWNODE;
  326. entity->asd.match.fwnode = of_fwnode_handle(remote);
  327. list_add_tail(&entity->list, &xdev->entities);
  328. xdev->num_subdevs++;
  329. }
  330. of_node_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. int ret;
  337. /*
  338. * Walk the links to parse the full graph. Start by parsing the
  339. * composite node and then parse entities in turn. The list_for_each
  340. * loop will handle entities added at the end of the list while walking
  341. * the links.
  342. */
  343. ret = xvip_graph_parse_one(xdev, xdev->dev->of_node);
  344. if (ret < 0)
  345. return 0;
  346. list_for_each_entry(entity, &xdev->entities, list) {
  347. ret = xvip_graph_parse_one(xdev, entity->node);
  348. if (ret < 0)
  349. break;
  350. }
  351. return ret;
  352. }
  353. static int xvip_graph_dma_init_one(struct xvip_composite_device *xdev,
  354. struct device_node *node)
  355. {
  356. struct xvip_dma *dma;
  357. enum v4l2_buf_type type;
  358. const char *direction;
  359. unsigned int index;
  360. int ret;
  361. ret = of_property_read_string(node, "direction", &direction);
  362. if (ret < 0)
  363. return ret;
  364. if (strcmp(direction, "input") == 0)
  365. type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  366. else if (strcmp(direction, "output") == 0)
  367. type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  368. else
  369. return -EINVAL;
  370. of_property_read_u32(node, "reg", &index);
  371. dma = devm_kzalloc(xdev->dev, sizeof(*dma), GFP_KERNEL);
  372. if (dma == NULL)
  373. return -ENOMEM;
  374. ret = xvip_dma_init(xdev, dma, type, index);
  375. if (ret < 0) {
  376. dev_err(xdev->dev, "%pOF initialization failed\n", node);
  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. of_node_put(port);
  398. return ret;
  399. }
  400. }
  401. return 0;
  402. }
  403. static void xvip_graph_cleanup(struct xvip_composite_device *xdev)
  404. {
  405. struct xvip_graph_entity *entityp;
  406. struct xvip_graph_entity *entity;
  407. struct xvip_dma *dmap;
  408. struct xvip_dma *dma;
  409. v4l2_async_notifier_unregister(&xdev->notifier);
  410. list_for_each_entry_safe(entity, entityp, &xdev->entities, list) {
  411. of_node_put(entity->node);
  412. list_del(&entity->list);
  413. }
  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. struct xvip_graph_entity *entity;
  422. struct v4l2_async_subdev **subdevs = NULL;
  423. unsigned int num_subdevs;
  424. unsigned int i;
  425. int ret;
  426. /* Init the DMA channels. */
  427. ret = xvip_graph_dma_init(xdev);
  428. if (ret < 0) {
  429. dev_err(xdev->dev, "DMA initialization failed\n");
  430. goto done;
  431. }
  432. /* Parse the graph to extract a list of subdevice DT nodes. */
  433. ret = xvip_graph_parse(xdev);
  434. if (ret < 0) {
  435. dev_err(xdev->dev, "graph parsing failed\n");
  436. goto done;
  437. }
  438. if (!xdev->num_subdevs) {
  439. dev_err(xdev->dev, "no subdev found in graph\n");
  440. goto done;
  441. }
  442. /* Register the subdevices notifier. */
  443. num_subdevs = xdev->num_subdevs;
  444. subdevs = devm_kcalloc(xdev->dev, num_subdevs, sizeof(*subdevs),
  445. GFP_KERNEL);
  446. if (subdevs == NULL) {
  447. ret = -ENOMEM;
  448. goto done;
  449. }
  450. i = 0;
  451. list_for_each_entry(entity, &xdev->entities, list)
  452. subdevs[i++] = &entity->asd;
  453. xdev->notifier.subdevs = subdevs;
  454. xdev->notifier.num_subdevs = num_subdevs;
  455. xdev->notifier.ops = &xvip_graph_notify_ops;
  456. ret = v4l2_async_notifier_register(&xdev->v4l2_dev, &xdev->notifier);
  457. if (ret < 0) {
  458. dev_err(xdev->dev, "notifier registration failed\n");
  459. goto done;
  460. }
  461. ret = 0;
  462. done:
  463. if (ret < 0)
  464. xvip_graph_cleanup(xdev);
  465. return ret;
  466. }
  467. /* -----------------------------------------------------------------------------
  468. * Media Controller and V4L2
  469. */
  470. static void xvip_composite_v4l2_cleanup(struct xvip_composite_device *xdev)
  471. {
  472. v4l2_device_unregister(&xdev->v4l2_dev);
  473. media_device_unregister(&xdev->media_dev);
  474. media_device_cleanup(&xdev->media_dev);
  475. }
  476. static int xvip_composite_v4l2_init(struct xvip_composite_device *xdev)
  477. {
  478. int ret;
  479. xdev->media_dev.dev = xdev->dev;
  480. strlcpy(xdev->media_dev.model, "Xilinx Video Composite Device",
  481. sizeof(xdev->media_dev.model));
  482. xdev->media_dev.hw_revision = 0;
  483. media_device_init(&xdev->media_dev);
  484. xdev->v4l2_dev.mdev = &xdev->media_dev;
  485. ret = v4l2_device_register(xdev->dev, &xdev->v4l2_dev);
  486. if (ret < 0) {
  487. dev_err(xdev->dev, "V4L2 device registration failed (%d)\n",
  488. ret);
  489. media_device_cleanup(&xdev->media_dev);
  490. return ret;
  491. }
  492. return 0;
  493. }
  494. /* -----------------------------------------------------------------------------
  495. * Platform Device Driver
  496. */
  497. static int xvip_composite_probe(struct platform_device *pdev)
  498. {
  499. struct xvip_composite_device *xdev;
  500. int ret;
  501. xdev = devm_kzalloc(&pdev->dev, sizeof(*xdev), GFP_KERNEL);
  502. if (!xdev)
  503. return -ENOMEM;
  504. xdev->dev = &pdev->dev;
  505. INIT_LIST_HEAD(&xdev->entities);
  506. INIT_LIST_HEAD(&xdev->dmas);
  507. ret = xvip_composite_v4l2_init(xdev);
  508. if (ret < 0)
  509. return ret;
  510. ret = xvip_graph_init(xdev);
  511. if (ret < 0)
  512. goto error;
  513. platform_set_drvdata(pdev, xdev);
  514. dev_info(xdev->dev, "device registered\n");
  515. return 0;
  516. error:
  517. xvip_composite_v4l2_cleanup(xdev);
  518. return ret;
  519. }
  520. static int xvip_composite_remove(struct platform_device *pdev)
  521. {
  522. struct xvip_composite_device *xdev = platform_get_drvdata(pdev);
  523. xvip_graph_cleanup(xdev);
  524. xvip_composite_v4l2_cleanup(xdev);
  525. return 0;
  526. }
  527. static const struct of_device_id xvip_composite_of_id_table[] = {
  528. { .compatible = "xlnx,video" },
  529. { }
  530. };
  531. MODULE_DEVICE_TABLE(of, xvip_composite_of_id_table);
  532. static struct platform_driver xvip_composite_driver = {
  533. .driver = {
  534. .name = "xilinx-video",
  535. .of_match_table = xvip_composite_of_id_table,
  536. },
  537. .probe = xvip_composite_probe,
  538. .remove = xvip_composite_remove,
  539. };
  540. module_platform_driver(xvip_composite_driver);
  541. MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
  542. MODULE_DESCRIPTION("Xilinx Video IP Composite Driver");
  543. MODULE_LICENSE("GPL v2");