xilinx-dma.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  1. /*
  2. * Xilinx Video DMA
  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/dma/xilinx_dma.h>
  15. #include <linux/lcm.h>
  16. #include <linux/list.h>
  17. #include <linux/module.h>
  18. #include <linux/of.h>
  19. #include <linux/slab.h>
  20. #include <media/v4l2-dev.h>
  21. #include <media/v4l2-fh.h>
  22. #include <media/v4l2-ioctl.h>
  23. #include <media/videobuf2-v4l2.h>
  24. #include <media/videobuf2-dma-contig.h>
  25. #include "xilinx-dma.h"
  26. #include "xilinx-vip.h"
  27. #include "xilinx-vipp.h"
  28. #define XVIP_DMA_DEF_FORMAT V4L2_PIX_FMT_YUYV
  29. #define XVIP_DMA_DEF_WIDTH 1920
  30. #define XVIP_DMA_DEF_HEIGHT 1080
  31. /* Minimum and maximum widths are expressed in bytes */
  32. #define XVIP_DMA_MIN_WIDTH 1U
  33. #define XVIP_DMA_MAX_WIDTH 65535U
  34. #define XVIP_DMA_MIN_HEIGHT 1U
  35. #define XVIP_DMA_MAX_HEIGHT 8191U
  36. /* -----------------------------------------------------------------------------
  37. * Helper functions
  38. */
  39. static struct v4l2_subdev *
  40. xvip_dma_remote_subdev(struct media_pad *local, u32 *pad)
  41. {
  42. struct media_pad *remote;
  43. remote = media_entity_remote_pad(local);
  44. if (!remote || !is_media_entity_v4l2_subdev(remote->entity))
  45. return NULL;
  46. if (pad)
  47. *pad = remote->index;
  48. return media_entity_to_v4l2_subdev(remote->entity);
  49. }
  50. static int xvip_dma_verify_format(struct xvip_dma *dma)
  51. {
  52. struct v4l2_subdev_format fmt;
  53. struct v4l2_subdev *subdev;
  54. int ret;
  55. subdev = xvip_dma_remote_subdev(&dma->pad, &fmt.pad);
  56. if (subdev == NULL)
  57. return -EPIPE;
  58. fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
  59. ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt);
  60. if (ret < 0)
  61. return ret == -ENOIOCTLCMD ? -EINVAL : ret;
  62. if (dma->fmtinfo->code != fmt.format.code ||
  63. dma->format.height != fmt.format.height ||
  64. dma->format.width != fmt.format.width ||
  65. dma->format.colorspace != fmt.format.colorspace)
  66. return -EINVAL;
  67. return 0;
  68. }
  69. /* -----------------------------------------------------------------------------
  70. * Pipeline Stream Management
  71. */
  72. /**
  73. * xvip_pipeline_start_stop - Start ot stop streaming on a pipeline
  74. * @pipe: The pipeline
  75. * @start: Start (when true) or stop (when false) the pipeline
  76. *
  77. * Walk the entities chain starting at the pipeline output video node and start
  78. * or stop all of them.
  79. *
  80. * Return: 0 if successful, or the return value of the failed video::s_stream
  81. * operation otherwise.
  82. */
  83. static int xvip_pipeline_start_stop(struct xvip_pipeline *pipe, bool start)
  84. {
  85. struct xvip_dma *dma = pipe->output;
  86. struct media_entity *entity;
  87. struct media_pad *pad;
  88. struct v4l2_subdev *subdev;
  89. int ret;
  90. entity = &dma->video.entity;
  91. while (1) {
  92. pad = &entity->pads[0];
  93. if (!(pad->flags & MEDIA_PAD_FL_SINK))
  94. break;
  95. pad = media_entity_remote_pad(pad);
  96. if (!pad || !is_media_entity_v4l2_subdev(pad->entity))
  97. break;
  98. entity = pad->entity;
  99. subdev = media_entity_to_v4l2_subdev(entity);
  100. ret = v4l2_subdev_call(subdev, video, s_stream, start);
  101. if (start && ret < 0 && ret != -ENOIOCTLCMD)
  102. return ret;
  103. }
  104. return 0;
  105. }
  106. /**
  107. * xvip_pipeline_set_stream - Enable/disable streaming on a pipeline
  108. * @pipe: The pipeline
  109. * @on: Turn the stream on when true or off when false
  110. *
  111. * The pipeline is shared between all DMA engines connect at its input and
  112. * output. While the stream state of DMA engines can be controlled
  113. * independently, pipelines have a shared stream state that enable or disable
  114. * all entities in the pipeline. For this reason the pipeline uses a streaming
  115. * counter that tracks the number of DMA engines that have requested the stream
  116. * to be enabled.
  117. *
  118. * When called with the @on argument set to true, this function will increment
  119. * the pipeline streaming count. If the streaming count reaches the number of
  120. * DMA engines in the pipeline it will enable all entities that belong to the
  121. * pipeline.
  122. *
  123. * Similarly, when called with the @on argument set to false, this function will
  124. * decrement the pipeline streaming count and disable all entities in the
  125. * pipeline when the streaming count reaches zero.
  126. *
  127. * Return: 0 if successful, or the return value of the failed video::s_stream
  128. * operation otherwise. Stopping the pipeline never fails. The pipeline state is
  129. * not updated when the operation fails.
  130. */
  131. static int xvip_pipeline_set_stream(struct xvip_pipeline *pipe, bool on)
  132. {
  133. int ret = 0;
  134. mutex_lock(&pipe->lock);
  135. if (on) {
  136. if (pipe->stream_count == pipe->num_dmas - 1) {
  137. ret = xvip_pipeline_start_stop(pipe, true);
  138. if (ret < 0)
  139. goto done;
  140. }
  141. pipe->stream_count++;
  142. } else {
  143. if (--pipe->stream_count == 0)
  144. xvip_pipeline_start_stop(pipe, false);
  145. }
  146. done:
  147. mutex_unlock(&pipe->lock);
  148. return ret;
  149. }
  150. static int xvip_pipeline_validate(struct xvip_pipeline *pipe,
  151. struct xvip_dma *start)
  152. {
  153. struct media_entity_graph graph;
  154. struct media_entity *entity = &start->video.entity;
  155. struct media_device *mdev = entity->graph_obj.mdev;
  156. unsigned int num_inputs = 0;
  157. unsigned int num_outputs = 0;
  158. mutex_lock(&mdev->graph_mutex);
  159. /* Walk the graph to locate the video nodes. */
  160. media_entity_graph_walk_start(&graph, entity);
  161. while ((entity = media_entity_graph_walk_next(&graph))) {
  162. struct xvip_dma *dma;
  163. if (entity->type != MEDIA_ENT_T_V4L2_VIDEO)
  164. continue;
  165. dma = to_xvip_dma(media_entity_to_video_device(entity));
  166. if (dma->pad.flags & MEDIA_PAD_FL_SINK) {
  167. pipe->output = dma;
  168. num_outputs++;
  169. } else {
  170. num_inputs++;
  171. }
  172. }
  173. mutex_unlock(&mdev->graph_mutex);
  174. /* We need exactly one output and zero or one input. */
  175. if (num_outputs != 1 || num_inputs > 1)
  176. return -EPIPE;
  177. pipe->num_dmas = num_inputs + num_outputs;
  178. return 0;
  179. }
  180. static void __xvip_pipeline_cleanup(struct xvip_pipeline *pipe)
  181. {
  182. pipe->num_dmas = 0;
  183. pipe->output = NULL;
  184. }
  185. /**
  186. * xvip_pipeline_cleanup - Cleanup the pipeline after streaming
  187. * @pipe: the pipeline
  188. *
  189. * Decrease the pipeline use count and clean it up if we were the last user.
  190. */
  191. static void xvip_pipeline_cleanup(struct xvip_pipeline *pipe)
  192. {
  193. mutex_lock(&pipe->lock);
  194. /* If we're the last user clean up the pipeline. */
  195. if (--pipe->use_count == 0)
  196. __xvip_pipeline_cleanup(pipe);
  197. mutex_unlock(&pipe->lock);
  198. }
  199. /**
  200. * xvip_pipeline_prepare - Prepare the pipeline for streaming
  201. * @pipe: the pipeline
  202. * @dma: DMA engine at one end of the pipeline
  203. *
  204. * Validate the pipeline if no user exists yet, otherwise just increase the use
  205. * count.
  206. *
  207. * Return: 0 if successful or -EPIPE if the pipeline is not valid.
  208. */
  209. static int xvip_pipeline_prepare(struct xvip_pipeline *pipe,
  210. struct xvip_dma *dma)
  211. {
  212. int ret;
  213. mutex_lock(&pipe->lock);
  214. /* If we're the first user validate and initialize the pipeline. */
  215. if (pipe->use_count == 0) {
  216. ret = xvip_pipeline_validate(pipe, dma);
  217. if (ret < 0) {
  218. __xvip_pipeline_cleanup(pipe);
  219. goto done;
  220. }
  221. }
  222. pipe->use_count++;
  223. ret = 0;
  224. done:
  225. mutex_unlock(&pipe->lock);
  226. return ret;
  227. }
  228. /* -----------------------------------------------------------------------------
  229. * videobuf2 queue operations
  230. */
  231. /**
  232. * struct xvip_dma_buffer - Video DMA buffer
  233. * @buf: vb2 buffer base object
  234. * @queue: buffer list entry in the DMA engine queued buffers list
  235. * @dma: DMA channel that uses the buffer
  236. */
  237. struct xvip_dma_buffer {
  238. struct vb2_v4l2_buffer buf;
  239. struct list_head queue;
  240. struct xvip_dma *dma;
  241. };
  242. #define to_xvip_dma_buffer(vb) container_of(vb, struct xvip_dma_buffer, buf)
  243. static void xvip_dma_complete(void *param)
  244. {
  245. struct xvip_dma_buffer *buf = param;
  246. struct xvip_dma *dma = buf->dma;
  247. spin_lock(&dma->queued_lock);
  248. list_del(&buf->queue);
  249. spin_unlock(&dma->queued_lock);
  250. buf->buf.field = V4L2_FIELD_NONE;
  251. buf->buf.sequence = dma->sequence++;
  252. buf->buf.vb2_buf.timestamp = ktime_get_ns();
  253. vb2_set_plane_payload(&buf->buf.vb2_buf, 0, dma->format.sizeimage);
  254. vb2_buffer_done(&buf->buf.vb2_buf, VB2_BUF_STATE_DONE);
  255. }
  256. static int
  257. xvip_dma_queue_setup(struct vb2_queue *vq,
  258. unsigned int *nbuffers, unsigned int *nplanes,
  259. unsigned int sizes[], void *alloc_ctxs[])
  260. {
  261. struct xvip_dma *dma = vb2_get_drv_priv(vq);
  262. alloc_ctxs[0] = dma->alloc_ctx;
  263. /* Make sure the image size is large enough. */
  264. if (*nplanes)
  265. return sizes[0] < dma->format.sizeimage ? -EINVAL : 0;
  266. *nplanes = 1;
  267. sizes[0] = dma->format.sizeimage;
  268. return 0;
  269. }
  270. static int xvip_dma_buffer_prepare(struct vb2_buffer *vb)
  271. {
  272. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  273. struct xvip_dma *dma = vb2_get_drv_priv(vb->vb2_queue);
  274. struct xvip_dma_buffer *buf = to_xvip_dma_buffer(vbuf);
  275. buf->dma = dma;
  276. return 0;
  277. }
  278. static void xvip_dma_buffer_queue(struct vb2_buffer *vb)
  279. {
  280. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  281. struct xvip_dma *dma = vb2_get_drv_priv(vb->vb2_queue);
  282. struct xvip_dma_buffer *buf = to_xvip_dma_buffer(vbuf);
  283. struct dma_async_tx_descriptor *desc;
  284. dma_addr_t addr = vb2_dma_contig_plane_dma_addr(vb, 0);
  285. u32 flags;
  286. if (dma->queue.type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  287. flags = DMA_PREP_INTERRUPT | DMA_CTRL_ACK;
  288. dma->xt.dir = DMA_DEV_TO_MEM;
  289. dma->xt.src_sgl = false;
  290. dma->xt.dst_sgl = true;
  291. dma->xt.dst_start = addr;
  292. } else {
  293. flags = DMA_PREP_INTERRUPT | DMA_CTRL_ACK;
  294. dma->xt.dir = DMA_MEM_TO_DEV;
  295. dma->xt.src_sgl = true;
  296. dma->xt.dst_sgl = false;
  297. dma->xt.src_start = addr;
  298. }
  299. dma->xt.frame_size = 1;
  300. dma->sgl[0].size = dma->format.width * dma->fmtinfo->bpp;
  301. dma->sgl[0].icg = dma->format.bytesperline - dma->sgl[0].size;
  302. dma->xt.numf = dma->format.height;
  303. desc = dmaengine_prep_interleaved_dma(dma->dma, &dma->xt, flags);
  304. if (!desc) {
  305. dev_err(dma->xdev->dev, "Failed to prepare DMA transfer\n");
  306. vb2_buffer_done(&buf->buf.vb2_buf, VB2_BUF_STATE_ERROR);
  307. return;
  308. }
  309. desc->callback = xvip_dma_complete;
  310. desc->callback_param = buf;
  311. spin_lock_irq(&dma->queued_lock);
  312. list_add_tail(&buf->queue, &dma->queued_bufs);
  313. spin_unlock_irq(&dma->queued_lock);
  314. dmaengine_submit(desc);
  315. if (vb2_is_streaming(&dma->queue))
  316. dma_async_issue_pending(dma->dma);
  317. }
  318. static int xvip_dma_start_streaming(struct vb2_queue *vq, unsigned int count)
  319. {
  320. struct xvip_dma *dma = vb2_get_drv_priv(vq);
  321. struct xvip_dma_buffer *buf, *nbuf;
  322. struct xvip_pipeline *pipe;
  323. int ret;
  324. dma->sequence = 0;
  325. /*
  326. * Start streaming on the pipeline. No link touching an entity in the
  327. * pipeline can be activated or deactivated once streaming is started.
  328. *
  329. * Use the pipeline object embedded in the first DMA object that starts
  330. * streaming.
  331. */
  332. pipe = dma->video.entity.pipe
  333. ? to_xvip_pipeline(&dma->video.entity) : &dma->pipe;
  334. ret = media_entity_pipeline_start(&dma->video.entity, &pipe->pipe);
  335. if (ret < 0)
  336. goto error;
  337. /* Verify that the configured format matches the output of the
  338. * connected subdev.
  339. */
  340. ret = xvip_dma_verify_format(dma);
  341. if (ret < 0)
  342. goto error_stop;
  343. ret = xvip_pipeline_prepare(pipe, dma);
  344. if (ret < 0)
  345. goto error_stop;
  346. /* Start the DMA engine. This must be done before starting the blocks
  347. * in the pipeline to avoid DMA synchronization issues.
  348. */
  349. dma_async_issue_pending(dma->dma);
  350. /* Start the pipeline. */
  351. xvip_pipeline_set_stream(pipe, true);
  352. return 0;
  353. error_stop:
  354. media_entity_pipeline_stop(&dma->video.entity);
  355. error:
  356. /* Give back all queued buffers to videobuf2. */
  357. spin_lock_irq(&dma->queued_lock);
  358. list_for_each_entry_safe(buf, nbuf, &dma->queued_bufs, queue) {
  359. vb2_buffer_done(&buf->buf.vb2_buf, VB2_BUF_STATE_QUEUED);
  360. list_del(&buf->queue);
  361. }
  362. spin_unlock_irq(&dma->queued_lock);
  363. return ret;
  364. }
  365. static void xvip_dma_stop_streaming(struct vb2_queue *vq)
  366. {
  367. struct xvip_dma *dma = vb2_get_drv_priv(vq);
  368. struct xvip_pipeline *pipe = to_xvip_pipeline(&dma->video.entity);
  369. struct xvip_dma_buffer *buf, *nbuf;
  370. /* Stop the pipeline. */
  371. xvip_pipeline_set_stream(pipe, false);
  372. /* Stop and reset the DMA engine. */
  373. dmaengine_terminate_all(dma->dma);
  374. /* Cleanup the pipeline and mark it as being stopped. */
  375. xvip_pipeline_cleanup(pipe);
  376. media_entity_pipeline_stop(&dma->video.entity);
  377. /* Give back all queued buffers to videobuf2. */
  378. spin_lock_irq(&dma->queued_lock);
  379. list_for_each_entry_safe(buf, nbuf, &dma->queued_bufs, queue) {
  380. vb2_buffer_done(&buf->buf.vb2_buf, VB2_BUF_STATE_ERROR);
  381. list_del(&buf->queue);
  382. }
  383. spin_unlock_irq(&dma->queued_lock);
  384. }
  385. static struct vb2_ops xvip_dma_queue_qops = {
  386. .queue_setup = xvip_dma_queue_setup,
  387. .buf_prepare = xvip_dma_buffer_prepare,
  388. .buf_queue = xvip_dma_buffer_queue,
  389. .wait_prepare = vb2_ops_wait_prepare,
  390. .wait_finish = vb2_ops_wait_finish,
  391. .start_streaming = xvip_dma_start_streaming,
  392. .stop_streaming = xvip_dma_stop_streaming,
  393. };
  394. /* -----------------------------------------------------------------------------
  395. * V4L2 ioctls
  396. */
  397. static int
  398. xvip_dma_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
  399. {
  400. struct v4l2_fh *vfh = file->private_data;
  401. struct xvip_dma *dma = to_xvip_dma(vfh->vdev);
  402. cap->capabilities = V4L2_CAP_DEVICE_CAPS | V4L2_CAP_STREAMING
  403. | dma->xdev->v4l2_caps;
  404. if (dma->queue.type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  405. cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
  406. else
  407. cap->device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
  408. strlcpy(cap->driver, "xilinx-vipp", sizeof(cap->driver));
  409. strlcpy(cap->card, dma->video.name, sizeof(cap->card));
  410. snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s:%u",
  411. dma->xdev->dev->of_node->name, dma->port);
  412. return 0;
  413. }
  414. /* FIXME: without this callback function, some applications are not configured
  415. * with correct formats, and it results in frames in wrong format. Whether this
  416. * callback needs to be required is not clearly defined, so it should be
  417. * clarified through the mailing list.
  418. */
  419. static int
  420. xvip_dma_enum_format(struct file *file, void *fh, struct v4l2_fmtdesc *f)
  421. {
  422. struct v4l2_fh *vfh = file->private_data;
  423. struct xvip_dma *dma = to_xvip_dma(vfh->vdev);
  424. if (f->index > 0)
  425. return -EINVAL;
  426. f->pixelformat = dma->format.pixelformat;
  427. strlcpy(f->description, dma->fmtinfo->description,
  428. sizeof(f->description));
  429. return 0;
  430. }
  431. static int
  432. xvip_dma_get_format(struct file *file, void *fh, struct v4l2_format *format)
  433. {
  434. struct v4l2_fh *vfh = file->private_data;
  435. struct xvip_dma *dma = to_xvip_dma(vfh->vdev);
  436. format->fmt.pix = dma->format;
  437. return 0;
  438. }
  439. static void
  440. __xvip_dma_try_format(struct xvip_dma *dma, struct v4l2_pix_format *pix,
  441. const struct xvip_video_format **fmtinfo)
  442. {
  443. const struct xvip_video_format *info;
  444. unsigned int min_width;
  445. unsigned int max_width;
  446. unsigned int min_bpl;
  447. unsigned int max_bpl;
  448. unsigned int width;
  449. unsigned int align;
  450. unsigned int bpl;
  451. /* Retrieve format information and select the default format if the
  452. * requested format isn't supported.
  453. */
  454. info = xvip_get_format_by_fourcc(pix->pixelformat);
  455. if (IS_ERR(info))
  456. info = xvip_get_format_by_fourcc(XVIP_DMA_DEF_FORMAT);
  457. pix->pixelformat = info->fourcc;
  458. pix->field = V4L2_FIELD_NONE;
  459. /* The transfer alignment requirements are expressed in bytes. Compute
  460. * the minimum and maximum values, clamp the requested width and convert
  461. * it back to pixels.
  462. */
  463. align = lcm(dma->align, info->bpp);
  464. min_width = roundup(XVIP_DMA_MIN_WIDTH, align);
  465. max_width = rounddown(XVIP_DMA_MAX_WIDTH, align);
  466. width = rounddown(pix->width * info->bpp, align);
  467. pix->width = clamp(width, min_width, max_width) / info->bpp;
  468. pix->height = clamp(pix->height, XVIP_DMA_MIN_HEIGHT,
  469. XVIP_DMA_MAX_HEIGHT);
  470. /* Clamp the requested bytes per line value. If the maximum bytes per
  471. * line value is zero, the module doesn't support user configurable line
  472. * sizes. Override the requested value with the minimum in that case.
  473. */
  474. min_bpl = pix->width * info->bpp;
  475. max_bpl = rounddown(XVIP_DMA_MAX_WIDTH, dma->align);
  476. bpl = rounddown(pix->bytesperline, dma->align);
  477. pix->bytesperline = clamp(bpl, min_bpl, max_bpl);
  478. pix->sizeimage = pix->bytesperline * pix->height;
  479. if (fmtinfo)
  480. *fmtinfo = info;
  481. }
  482. static int
  483. xvip_dma_try_format(struct file *file, void *fh, struct v4l2_format *format)
  484. {
  485. struct v4l2_fh *vfh = file->private_data;
  486. struct xvip_dma *dma = to_xvip_dma(vfh->vdev);
  487. __xvip_dma_try_format(dma, &format->fmt.pix, NULL);
  488. return 0;
  489. }
  490. static int
  491. xvip_dma_set_format(struct file *file, void *fh, struct v4l2_format *format)
  492. {
  493. struct v4l2_fh *vfh = file->private_data;
  494. struct xvip_dma *dma = to_xvip_dma(vfh->vdev);
  495. const struct xvip_video_format *info;
  496. __xvip_dma_try_format(dma, &format->fmt.pix, &info);
  497. if (vb2_is_busy(&dma->queue))
  498. return -EBUSY;
  499. dma->format = format->fmt.pix;
  500. dma->fmtinfo = info;
  501. return 0;
  502. }
  503. static const struct v4l2_ioctl_ops xvip_dma_ioctl_ops = {
  504. .vidioc_querycap = xvip_dma_querycap,
  505. .vidioc_enum_fmt_vid_cap = xvip_dma_enum_format,
  506. .vidioc_g_fmt_vid_cap = xvip_dma_get_format,
  507. .vidioc_g_fmt_vid_out = xvip_dma_get_format,
  508. .vidioc_s_fmt_vid_cap = xvip_dma_set_format,
  509. .vidioc_s_fmt_vid_out = xvip_dma_set_format,
  510. .vidioc_try_fmt_vid_cap = xvip_dma_try_format,
  511. .vidioc_try_fmt_vid_out = xvip_dma_try_format,
  512. .vidioc_reqbufs = vb2_ioctl_reqbufs,
  513. .vidioc_querybuf = vb2_ioctl_querybuf,
  514. .vidioc_qbuf = vb2_ioctl_qbuf,
  515. .vidioc_dqbuf = vb2_ioctl_dqbuf,
  516. .vidioc_create_bufs = vb2_ioctl_create_bufs,
  517. .vidioc_expbuf = vb2_ioctl_expbuf,
  518. .vidioc_streamon = vb2_ioctl_streamon,
  519. .vidioc_streamoff = vb2_ioctl_streamoff,
  520. };
  521. /* -----------------------------------------------------------------------------
  522. * V4L2 file operations
  523. */
  524. static const struct v4l2_file_operations xvip_dma_fops = {
  525. .owner = THIS_MODULE,
  526. .unlocked_ioctl = video_ioctl2,
  527. .open = v4l2_fh_open,
  528. .release = vb2_fop_release,
  529. .poll = vb2_fop_poll,
  530. .mmap = vb2_fop_mmap,
  531. };
  532. /* -----------------------------------------------------------------------------
  533. * Xilinx Video DMA Core
  534. */
  535. int xvip_dma_init(struct xvip_composite_device *xdev, struct xvip_dma *dma,
  536. enum v4l2_buf_type type, unsigned int port)
  537. {
  538. char name[16];
  539. int ret;
  540. dma->xdev = xdev;
  541. dma->port = port;
  542. mutex_init(&dma->lock);
  543. mutex_init(&dma->pipe.lock);
  544. INIT_LIST_HEAD(&dma->queued_bufs);
  545. spin_lock_init(&dma->queued_lock);
  546. dma->fmtinfo = xvip_get_format_by_fourcc(XVIP_DMA_DEF_FORMAT);
  547. dma->format.pixelformat = dma->fmtinfo->fourcc;
  548. dma->format.colorspace = V4L2_COLORSPACE_SRGB;
  549. dma->format.field = V4L2_FIELD_NONE;
  550. dma->format.width = XVIP_DMA_DEF_WIDTH;
  551. dma->format.height = XVIP_DMA_DEF_HEIGHT;
  552. dma->format.bytesperline = dma->format.width * dma->fmtinfo->bpp;
  553. dma->format.sizeimage = dma->format.bytesperline * dma->format.height;
  554. /* Initialize the media entity... */
  555. dma->pad.flags = type == V4L2_BUF_TYPE_VIDEO_CAPTURE
  556. ? MEDIA_PAD_FL_SINK : MEDIA_PAD_FL_SOURCE;
  557. ret = media_entity_init(&dma->video.entity, 1, &dma->pad);
  558. if (ret < 0)
  559. goto error;
  560. /* ... and the video node... */
  561. dma->video.fops = &xvip_dma_fops;
  562. dma->video.v4l2_dev = &xdev->v4l2_dev;
  563. dma->video.queue = &dma->queue;
  564. snprintf(dma->video.name, sizeof(dma->video.name), "%s %s %u",
  565. xdev->dev->of_node->name,
  566. type == V4L2_BUF_TYPE_VIDEO_CAPTURE ? "output" : "input",
  567. port);
  568. dma->video.vfl_type = VFL_TYPE_GRABBER;
  569. dma->video.vfl_dir = type == V4L2_BUF_TYPE_VIDEO_CAPTURE
  570. ? VFL_DIR_RX : VFL_DIR_TX;
  571. dma->video.release = video_device_release_empty;
  572. dma->video.ioctl_ops = &xvip_dma_ioctl_ops;
  573. dma->video.lock = &dma->lock;
  574. video_set_drvdata(&dma->video, dma);
  575. /* ... and the buffers queue... */
  576. dma->alloc_ctx = vb2_dma_contig_init_ctx(dma->xdev->dev);
  577. if (IS_ERR(dma->alloc_ctx)) {
  578. ret = PTR_ERR(dma->alloc_ctx);
  579. goto error;
  580. }
  581. /* Don't enable VB2_READ and VB2_WRITE, as using the read() and write()
  582. * V4L2 APIs would be inefficient. Testing on the command line with a
  583. * 'cat /dev/video?' thus won't be possible, but given that the driver
  584. * anyway requires a test tool to setup the pipeline before any video
  585. * stream can be started, requiring a specific V4L2 test tool as well
  586. * instead of 'cat' isn't really a drawback.
  587. */
  588. dma->queue.type = type;
  589. dma->queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
  590. dma->queue.lock = &dma->lock;
  591. dma->queue.drv_priv = dma;
  592. dma->queue.buf_struct_size = sizeof(struct xvip_dma_buffer);
  593. dma->queue.ops = &xvip_dma_queue_qops;
  594. dma->queue.mem_ops = &vb2_dma_contig_memops;
  595. dma->queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC
  596. | V4L2_BUF_FLAG_TSTAMP_SRC_EOF;
  597. ret = vb2_queue_init(&dma->queue);
  598. if (ret < 0) {
  599. dev_err(dma->xdev->dev, "failed to initialize VB2 queue\n");
  600. goto error;
  601. }
  602. /* ... and the DMA channel. */
  603. snprintf(name, sizeof(name), "port%u", port);
  604. dma->dma = dma_request_slave_channel(dma->xdev->dev, name);
  605. if (dma->dma == NULL) {
  606. dev_err(dma->xdev->dev, "no VDMA channel found\n");
  607. ret = -ENODEV;
  608. goto error;
  609. }
  610. dma->align = 1 << dma->dma->device->copy_align;
  611. ret = video_register_device(&dma->video, VFL_TYPE_GRABBER, -1);
  612. if (ret < 0) {
  613. dev_err(dma->xdev->dev, "failed to register video device\n");
  614. goto error;
  615. }
  616. return 0;
  617. error:
  618. xvip_dma_cleanup(dma);
  619. return ret;
  620. }
  621. void xvip_dma_cleanup(struct xvip_dma *dma)
  622. {
  623. if (video_is_registered(&dma->video))
  624. video_unregister_device(&dma->video);
  625. if (dma->dma)
  626. dma_release_channel(dma->dma);
  627. if (!IS_ERR_OR_NULL(dma->alloc_ctx))
  628. vb2_dma_contig_cleanup_ctx(dma->alloc_ctx);
  629. media_entity_cleanup(&dma->video.entity);
  630. mutex_destroy(&dma->lock);
  631. mutex_destroy(&dma->pipe.lock);
  632. }