xilinx-dma.c 21 KB

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