fimc-m2m.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  1. /*
  2. * Samsung S5P/EXYNOS4 SoC series FIMC (video postprocessor) driver
  3. *
  4. * Copyright (C) 2012 - 2013 Samsung Electronics Co., Ltd.
  5. * Sylwester Nawrocki <s.nawrocki@samsung.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published
  9. * by the Free Software Foundation, either version 2 of the License,
  10. * or (at your option) any later version.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/kernel.h>
  14. #include <linux/types.h>
  15. #include <linux/errno.h>
  16. #include <linux/bug.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/device.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/pm_runtime.h>
  21. #include <linux/list.h>
  22. #include <linux/io.h>
  23. #include <linux/slab.h>
  24. #include <linux/clk.h>
  25. #include <media/v4l2-ioctl.h>
  26. #include <media/videobuf2-v4l2.h>
  27. #include <media/videobuf2-dma-contig.h>
  28. #include "common.h"
  29. #include "fimc-core.h"
  30. #include "fimc-reg.h"
  31. #include "media-dev.h"
  32. static unsigned int get_m2m_fmt_flags(unsigned int stream_type)
  33. {
  34. if (stream_type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
  35. return FMT_FLAGS_M2M_IN;
  36. else
  37. return FMT_FLAGS_M2M_OUT;
  38. }
  39. void fimc_m2m_job_finish(struct fimc_ctx *ctx, int vb_state)
  40. {
  41. struct vb2_v4l2_buffer *src_vb, *dst_vb;
  42. if (!ctx || !ctx->fh.m2m_ctx)
  43. return;
  44. src_vb = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
  45. dst_vb = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
  46. if (src_vb)
  47. v4l2_m2m_buf_done(src_vb, vb_state);
  48. if (dst_vb)
  49. v4l2_m2m_buf_done(dst_vb, vb_state);
  50. if (src_vb && dst_vb)
  51. v4l2_m2m_job_finish(ctx->fimc_dev->m2m.m2m_dev,
  52. ctx->fh.m2m_ctx);
  53. }
  54. /* Complete the transaction which has been scheduled for execution. */
  55. static void fimc_m2m_shutdown(struct fimc_ctx *ctx)
  56. {
  57. struct fimc_dev *fimc = ctx->fimc_dev;
  58. if (!fimc_m2m_pending(fimc))
  59. return;
  60. fimc_ctx_state_set(FIMC_CTX_SHUT, ctx);
  61. wait_event_timeout(fimc->irq_queue,
  62. !fimc_ctx_state_is_set(FIMC_CTX_SHUT, ctx),
  63. FIMC_SHUTDOWN_TIMEOUT);
  64. }
  65. static int start_streaming(struct vb2_queue *q, unsigned int count)
  66. {
  67. struct fimc_ctx *ctx = q->drv_priv;
  68. int ret;
  69. ret = pm_runtime_get_sync(&ctx->fimc_dev->pdev->dev);
  70. return ret > 0 ? 0 : ret;
  71. }
  72. static void stop_streaming(struct vb2_queue *q)
  73. {
  74. struct fimc_ctx *ctx = q->drv_priv;
  75. fimc_m2m_shutdown(ctx);
  76. fimc_m2m_job_finish(ctx, VB2_BUF_STATE_ERROR);
  77. pm_runtime_put(&ctx->fimc_dev->pdev->dev);
  78. }
  79. static void fimc_device_run(void *priv)
  80. {
  81. struct vb2_v4l2_buffer *src_vb, *dst_vb;
  82. struct fimc_ctx *ctx = priv;
  83. struct fimc_frame *sf, *df;
  84. struct fimc_dev *fimc;
  85. unsigned long flags;
  86. int ret;
  87. if (WARN(!ctx, "Null context\n"))
  88. return;
  89. fimc = ctx->fimc_dev;
  90. spin_lock_irqsave(&fimc->slock, flags);
  91. set_bit(ST_M2M_PEND, &fimc->state);
  92. sf = &ctx->s_frame;
  93. df = &ctx->d_frame;
  94. if (ctx->state & FIMC_PARAMS) {
  95. /* Prepare the DMA offsets for scaler */
  96. fimc_prepare_dma_offset(ctx, sf);
  97. fimc_prepare_dma_offset(ctx, df);
  98. }
  99. src_vb = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
  100. ret = fimc_prepare_addr(ctx, &src_vb->vb2_buf, sf, &sf->paddr);
  101. if (ret)
  102. goto dma_unlock;
  103. dst_vb = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
  104. ret = fimc_prepare_addr(ctx, &dst_vb->vb2_buf, df, &df->paddr);
  105. if (ret)
  106. goto dma_unlock;
  107. dst_vb->vb2_buf.timestamp = src_vb->vb2_buf.timestamp;
  108. dst_vb->flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
  109. dst_vb->flags |=
  110. src_vb->flags & V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
  111. /* Reconfigure hardware if the context has changed. */
  112. if (fimc->m2m.ctx != ctx) {
  113. ctx->state |= FIMC_PARAMS;
  114. fimc->m2m.ctx = ctx;
  115. }
  116. if (ctx->state & FIMC_PARAMS) {
  117. fimc_set_yuv_order(ctx);
  118. fimc_hw_set_input_path(ctx);
  119. fimc_hw_set_in_dma(ctx);
  120. ret = fimc_set_scaler_info(ctx);
  121. if (ret)
  122. goto dma_unlock;
  123. fimc_hw_set_prescaler(ctx);
  124. fimc_hw_set_mainscaler(ctx);
  125. fimc_hw_set_target_format(ctx);
  126. fimc_hw_set_rotation(ctx);
  127. fimc_hw_set_effect(ctx);
  128. fimc_hw_set_out_dma(ctx);
  129. if (fimc->drv_data->alpha_color)
  130. fimc_hw_set_rgb_alpha(ctx);
  131. fimc_hw_set_output_path(ctx);
  132. }
  133. fimc_hw_set_input_addr(fimc, &sf->paddr);
  134. fimc_hw_set_output_addr(fimc, &df->paddr, -1);
  135. fimc_activate_capture(ctx);
  136. ctx->state &= (FIMC_CTX_M2M | FIMC_CTX_CAP);
  137. fimc_hw_activate_input_dma(fimc, true);
  138. dma_unlock:
  139. spin_unlock_irqrestore(&fimc->slock, flags);
  140. }
  141. static void fimc_job_abort(void *priv)
  142. {
  143. fimc_m2m_shutdown(priv);
  144. }
  145. static int fimc_queue_setup(struct vb2_queue *vq,
  146. unsigned int *num_buffers, unsigned int *num_planes,
  147. unsigned int sizes[], struct device *alloc_devs[])
  148. {
  149. struct fimc_ctx *ctx = vb2_get_drv_priv(vq);
  150. struct fimc_frame *f;
  151. int i;
  152. f = ctx_get_frame(ctx, vq->type);
  153. if (IS_ERR(f))
  154. return PTR_ERR(f);
  155. /*
  156. * Return number of non-contiguous planes (plane buffers)
  157. * depending on the configured color format.
  158. */
  159. if (!f->fmt)
  160. return -EINVAL;
  161. *num_planes = f->fmt->memplanes;
  162. for (i = 0; i < f->fmt->memplanes; i++)
  163. sizes[i] = f->payload[i];
  164. return 0;
  165. }
  166. static int fimc_buf_prepare(struct vb2_buffer *vb)
  167. {
  168. struct fimc_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  169. struct fimc_frame *frame;
  170. int i;
  171. frame = ctx_get_frame(ctx, vb->vb2_queue->type);
  172. if (IS_ERR(frame))
  173. return PTR_ERR(frame);
  174. for (i = 0; i < frame->fmt->memplanes; i++)
  175. vb2_set_plane_payload(vb, i, frame->payload[i]);
  176. return 0;
  177. }
  178. static void fimc_buf_queue(struct vb2_buffer *vb)
  179. {
  180. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  181. struct fimc_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  182. v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
  183. }
  184. static const struct vb2_ops fimc_qops = {
  185. .queue_setup = fimc_queue_setup,
  186. .buf_prepare = fimc_buf_prepare,
  187. .buf_queue = fimc_buf_queue,
  188. .wait_prepare = vb2_ops_wait_prepare,
  189. .wait_finish = vb2_ops_wait_finish,
  190. .stop_streaming = stop_streaming,
  191. .start_streaming = start_streaming,
  192. };
  193. /*
  194. * V4L2 ioctl handlers
  195. */
  196. static int fimc_m2m_querycap(struct file *file, void *fh,
  197. struct v4l2_capability *cap)
  198. {
  199. struct fimc_dev *fimc = video_drvdata(file);
  200. unsigned int caps;
  201. /*
  202. * This is only a mem-to-mem video device. The capture and output
  203. * device capability flags are left only for backward compatibility
  204. * and are scheduled for removal.
  205. */
  206. caps = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_M2M_MPLANE |
  207. V4L2_CAP_VIDEO_CAPTURE_MPLANE | V4L2_CAP_VIDEO_OUTPUT_MPLANE;
  208. __fimc_vidioc_querycap(&fimc->pdev->dev, cap, caps);
  209. return 0;
  210. }
  211. static int fimc_m2m_enum_fmt_mplane(struct file *file, void *priv,
  212. struct v4l2_fmtdesc *f)
  213. {
  214. struct fimc_fmt *fmt;
  215. fmt = fimc_find_format(NULL, NULL, get_m2m_fmt_flags(f->type),
  216. f->index);
  217. if (!fmt)
  218. return -EINVAL;
  219. strncpy(f->description, fmt->name, sizeof(f->description) - 1);
  220. f->pixelformat = fmt->fourcc;
  221. return 0;
  222. }
  223. static int fimc_m2m_g_fmt_mplane(struct file *file, void *fh,
  224. struct v4l2_format *f)
  225. {
  226. struct fimc_ctx *ctx = fh_to_ctx(fh);
  227. struct fimc_frame *frame = ctx_get_frame(ctx, f->type);
  228. if (IS_ERR(frame))
  229. return PTR_ERR(frame);
  230. __fimc_get_format(frame, f);
  231. return 0;
  232. }
  233. static int fimc_try_fmt_mplane(struct fimc_ctx *ctx, struct v4l2_format *f)
  234. {
  235. struct fimc_dev *fimc = ctx->fimc_dev;
  236. const struct fimc_variant *variant = fimc->variant;
  237. struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp;
  238. struct fimc_fmt *fmt;
  239. u32 max_w, mod_x, mod_y;
  240. if (!IS_M2M(f->type))
  241. return -EINVAL;
  242. fmt = fimc_find_format(&pix->pixelformat, NULL,
  243. get_m2m_fmt_flags(f->type), 0);
  244. if (WARN(fmt == NULL, "Pixel format lookup failed"))
  245. return -EINVAL;
  246. if (pix->field == V4L2_FIELD_ANY)
  247. pix->field = V4L2_FIELD_NONE;
  248. else if (pix->field != V4L2_FIELD_NONE)
  249. return -EINVAL;
  250. if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  251. max_w = variant->pix_limit->scaler_dis_w;
  252. mod_x = ffs(variant->min_inp_pixsize) - 1;
  253. } else {
  254. max_w = variant->pix_limit->out_rot_dis_w;
  255. mod_x = ffs(variant->min_out_pixsize) - 1;
  256. }
  257. if (tiled_fmt(fmt)) {
  258. mod_x = 6; /* 64 x 32 pixels tile */
  259. mod_y = 5;
  260. } else {
  261. if (variant->min_vsize_align == 1)
  262. mod_y = fimc_fmt_is_rgb(fmt->color) ? 0 : 1;
  263. else
  264. mod_y = ffs(variant->min_vsize_align) - 1;
  265. }
  266. v4l_bound_align_image(&pix->width, 16, max_w, mod_x,
  267. &pix->height, 8, variant->pix_limit->scaler_dis_w, mod_y, 0);
  268. fimc_adjust_mplane_format(fmt, pix->width, pix->height, &f->fmt.pix_mp);
  269. return 0;
  270. }
  271. static int fimc_m2m_try_fmt_mplane(struct file *file, void *fh,
  272. struct v4l2_format *f)
  273. {
  274. struct fimc_ctx *ctx = fh_to_ctx(fh);
  275. return fimc_try_fmt_mplane(ctx, f);
  276. }
  277. static void __set_frame_format(struct fimc_frame *frame, struct fimc_fmt *fmt,
  278. struct v4l2_pix_format_mplane *pixm)
  279. {
  280. int i;
  281. for (i = 0; i < fmt->memplanes; i++) {
  282. frame->bytesperline[i] = pixm->plane_fmt[i].bytesperline;
  283. frame->payload[i] = pixm->plane_fmt[i].sizeimage;
  284. }
  285. frame->f_width = pixm->width;
  286. frame->f_height = pixm->height;
  287. frame->o_width = pixm->width;
  288. frame->o_height = pixm->height;
  289. frame->width = pixm->width;
  290. frame->height = pixm->height;
  291. frame->offs_h = 0;
  292. frame->offs_v = 0;
  293. frame->fmt = fmt;
  294. }
  295. static int fimc_m2m_s_fmt_mplane(struct file *file, void *fh,
  296. struct v4l2_format *f)
  297. {
  298. struct fimc_ctx *ctx = fh_to_ctx(fh);
  299. struct fimc_dev *fimc = ctx->fimc_dev;
  300. struct fimc_fmt *fmt;
  301. struct vb2_queue *vq;
  302. struct fimc_frame *frame;
  303. int ret;
  304. ret = fimc_try_fmt_mplane(ctx, f);
  305. if (ret)
  306. return ret;
  307. vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
  308. if (vb2_is_busy(vq)) {
  309. v4l2_err(&fimc->m2m.vfd, "queue (%d) busy\n", f->type);
  310. return -EBUSY;
  311. }
  312. if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
  313. frame = &ctx->s_frame;
  314. else
  315. frame = &ctx->d_frame;
  316. fmt = fimc_find_format(&f->fmt.pix_mp.pixelformat, NULL,
  317. get_m2m_fmt_flags(f->type), 0);
  318. if (!fmt)
  319. return -EINVAL;
  320. __set_frame_format(frame, fmt, &f->fmt.pix_mp);
  321. /* Update RGB Alpha control state and value range */
  322. fimc_alpha_ctrl_update(ctx);
  323. return 0;
  324. }
  325. static int fimc_m2m_cropcap(struct file *file, void *fh,
  326. struct v4l2_cropcap *cr)
  327. {
  328. struct fimc_ctx *ctx = fh_to_ctx(fh);
  329. struct fimc_frame *frame;
  330. frame = ctx_get_frame(ctx, cr->type);
  331. if (IS_ERR(frame))
  332. return PTR_ERR(frame);
  333. cr->bounds.left = 0;
  334. cr->bounds.top = 0;
  335. cr->bounds.width = frame->o_width;
  336. cr->bounds.height = frame->o_height;
  337. cr->defrect = cr->bounds;
  338. return 0;
  339. }
  340. static int fimc_m2m_g_crop(struct file *file, void *fh, struct v4l2_crop *cr)
  341. {
  342. struct fimc_ctx *ctx = fh_to_ctx(fh);
  343. struct fimc_frame *frame;
  344. frame = ctx_get_frame(ctx, cr->type);
  345. if (IS_ERR(frame))
  346. return PTR_ERR(frame);
  347. cr->c.left = frame->offs_h;
  348. cr->c.top = frame->offs_v;
  349. cr->c.width = frame->width;
  350. cr->c.height = frame->height;
  351. return 0;
  352. }
  353. static int fimc_m2m_try_crop(struct fimc_ctx *ctx, struct v4l2_crop *cr)
  354. {
  355. struct fimc_dev *fimc = ctx->fimc_dev;
  356. struct fimc_frame *f;
  357. u32 min_size, halign, depth = 0;
  358. int i;
  359. if (cr->c.top < 0 || cr->c.left < 0) {
  360. v4l2_err(&fimc->m2m.vfd,
  361. "doesn't support negative values for top & left\n");
  362. return -EINVAL;
  363. }
  364. if (cr->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
  365. f = &ctx->d_frame;
  366. else if (cr->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
  367. f = &ctx->s_frame;
  368. else
  369. return -EINVAL;
  370. min_size = (f == &ctx->s_frame) ?
  371. fimc->variant->min_inp_pixsize : fimc->variant->min_out_pixsize;
  372. /* Get pixel alignment constraints. */
  373. if (fimc->variant->min_vsize_align == 1)
  374. halign = fimc_fmt_is_rgb(f->fmt->color) ? 0 : 1;
  375. else
  376. halign = ffs(fimc->variant->min_vsize_align) - 1;
  377. for (i = 0; i < f->fmt->memplanes; i++)
  378. depth += f->fmt->depth[i];
  379. v4l_bound_align_image(&cr->c.width, min_size, f->o_width,
  380. ffs(min_size) - 1,
  381. &cr->c.height, min_size, f->o_height,
  382. halign, 64/(ALIGN(depth, 8)));
  383. /* adjust left/top if cropping rectangle is out of bounds */
  384. if (cr->c.left + cr->c.width > f->o_width)
  385. cr->c.left = f->o_width - cr->c.width;
  386. if (cr->c.top + cr->c.height > f->o_height)
  387. cr->c.top = f->o_height - cr->c.height;
  388. cr->c.left = round_down(cr->c.left, min_size);
  389. cr->c.top = round_down(cr->c.top, fimc->variant->hor_offs_align);
  390. dbg("l:%d, t:%d, w:%d, h:%d, f_w: %d, f_h: %d",
  391. cr->c.left, cr->c.top, cr->c.width, cr->c.height,
  392. f->f_width, f->f_height);
  393. return 0;
  394. }
  395. static int fimc_m2m_s_crop(struct file *file, void *fh, const struct v4l2_crop *crop)
  396. {
  397. struct fimc_ctx *ctx = fh_to_ctx(fh);
  398. struct fimc_dev *fimc = ctx->fimc_dev;
  399. struct v4l2_crop cr = *crop;
  400. struct fimc_frame *f;
  401. int ret;
  402. ret = fimc_m2m_try_crop(ctx, &cr);
  403. if (ret)
  404. return ret;
  405. f = (cr.type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) ?
  406. &ctx->s_frame : &ctx->d_frame;
  407. /* Check to see if scaling ratio is within supported range */
  408. if (cr.type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  409. ret = fimc_check_scaler_ratio(ctx, cr.c.width,
  410. cr.c.height, ctx->d_frame.width,
  411. ctx->d_frame.height, ctx->rotation);
  412. } else {
  413. ret = fimc_check_scaler_ratio(ctx, ctx->s_frame.width,
  414. ctx->s_frame.height, cr.c.width,
  415. cr.c.height, ctx->rotation);
  416. }
  417. if (ret) {
  418. v4l2_err(&fimc->m2m.vfd, "Out of scaler range\n");
  419. return -EINVAL;
  420. }
  421. f->offs_h = cr.c.left;
  422. f->offs_v = cr.c.top;
  423. f->width = cr.c.width;
  424. f->height = cr.c.height;
  425. fimc_ctx_state_set(FIMC_PARAMS, ctx);
  426. return 0;
  427. }
  428. static const struct v4l2_ioctl_ops fimc_m2m_ioctl_ops = {
  429. .vidioc_querycap = fimc_m2m_querycap,
  430. .vidioc_enum_fmt_vid_cap_mplane = fimc_m2m_enum_fmt_mplane,
  431. .vidioc_enum_fmt_vid_out_mplane = fimc_m2m_enum_fmt_mplane,
  432. .vidioc_g_fmt_vid_cap_mplane = fimc_m2m_g_fmt_mplane,
  433. .vidioc_g_fmt_vid_out_mplane = fimc_m2m_g_fmt_mplane,
  434. .vidioc_try_fmt_vid_cap_mplane = fimc_m2m_try_fmt_mplane,
  435. .vidioc_try_fmt_vid_out_mplane = fimc_m2m_try_fmt_mplane,
  436. .vidioc_s_fmt_vid_cap_mplane = fimc_m2m_s_fmt_mplane,
  437. .vidioc_s_fmt_vid_out_mplane = fimc_m2m_s_fmt_mplane,
  438. .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
  439. .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
  440. .vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
  441. .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
  442. .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
  443. .vidioc_streamon = v4l2_m2m_ioctl_streamon,
  444. .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
  445. .vidioc_g_crop = fimc_m2m_g_crop,
  446. .vidioc_s_crop = fimc_m2m_s_crop,
  447. .vidioc_cropcap = fimc_m2m_cropcap
  448. };
  449. static int queue_init(void *priv, struct vb2_queue *src_vq,
  450. struct vb2_queue *dst_vq)
  451. {
  452. struct fimc_ctx *ctx = priv;
  453. int ret;
  454. src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
  455. src_vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
  456. src_vq->drv_priv = ctx;
  457. src_vq->ops = &fimc_qops;
  458. src_vq->mem_ops = &vb2_dma_contig_memops;
  459. src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
  460. src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
  461. src_vq->lock = &ctx->fimc_dev->lock;
  462. src_vq->dev = &ctx->fimc_dev->pdev->dev;
  463. ret = vb2_queue_init(src_vq);
  464. if (ret)
  465. return ret;
  466. dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
  467. dst_vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
  468. dst_vq->drv_priv = ctx;
  469. dst_vq->ops = &fimc_qops;
  470. dst_vq->mem_ops = &vb2_dma_contig_memops;
  471. dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
  472. dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
  473. dst_vq->lock = &ctx->fimc_dev->lock;
  474. dst_vq->dev = &ctx->fimc_dev->pdev->dev;
  475. return vb2_queue_init(dst_vq);
  476. }
  477. static int fimc_m2m_set_default_format(struct fimc_ctx *ctx)
  478. {
  479. struct v4l2_pix_format_mplane pixm = {
  480. .pixelformat = V4L2_PIX_FMT_RGB32,
  481. .width = 800,
  482. .height = 600,
  483. .plane_fmt[0] = {
  484. .bytesperline = 800 * 4,
  485. .sizeimage = 800 * 4 * 600,
  486. },
  487. };
  488. struct fimc_fmt *fmt;
  489. fmt = fimc_find_format(&pixm.pixelformat, NULL, FMT_FLAGS_M2M, 0);
  490. if (!fmt)
  491. return -EINVAL;
  492. __set_frame_format(&ctx->s_frame, fmt, &pixm);
  493. __set_frame_format(&ctx->d_frame, fmt, &pixm);
  494. return 0;
  495. }
  496. static int fimc_m2m_open(struct file *file)
  497. {
  498. struct fimc_dev *fimc = video_drvdata(file);
  499. struct fimc_ctx *ctx;
  500. int ret = -EBUSY;
  501. pr_debug("pid: %d, state: %#lx\n", task_pid_nr(current), fimc->state);
  502. if (mutex_lock_interruptible(&fimc->lock))
  503. return -ERESTARTSYS;
  504. /*
  505. * Don't allow simultaneous open() of the mem-to-mem and the
  506. * capture video node that belong to same FIMC IP instance.
  507. */
  508. if (test_bit(ST_CAPT_BUSY, &fimc->state))
  509. goto unlock;
  510. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  511. if (!ctx) {
  512. ret = -ENOMEM;
  513. goto unlock;
  514. }
  515. v4l2_fh_init(&ctx->fh, &fimc->m2m.vfd);
  516. ctx->fimc_dev = fimc;
  517. /* Default color format */
  518. ctx->s_frame.fmt = fimc_get_format(0);
  519. ctx->d_frame.fmt = fimc_get_format(0);
  520. ret = fimc_ctrls_create(ctx);
  521. if (ret)
  522. goto error_fh;
  523. /* Use separate control handler per file handle */
  524. ctx->fh.ctrl_handler = &ctx->ctrls.handler;
  525. file->private_data = &ctx->fh;
  526. v4l2_fh_add(&ctx->fh);
  527. /* Setup the device context for memory-to-memory mode */
  528. ctx->state = FIMC_CTX_M2M;
  529. ctx->flags = 0;
  530. ctx->in_path = FIMC_IO_DMA;
  531. ctx->out_path = FIMC_IO_DMA;
  532. ctx->scaler.enabled = 1;
  533. ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(fimc->m2m.m2m_dev, ctx, queue_init);
  534. if (IS_ERR(ctx->fh.m2m_ctx)) {
  535. ret = PTR_ERR(ctx->fh.m2m_ctx);
  536. goto error_c;
  537. }
  538. if (fimc->m2m.refcnt++ == 0)
  539. set_bit(ST_M2M_RUN, &fimc->state);
  540. ret = fimc_m2m_set_default_format(ctx);
  541. if (ret < 0)
  542. goto error_m2m_ctx;
  543. mutex_unlock(&fimc->lock);
  544. return 0;
  545. error_m2m_ctx:
  546. v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
  547. error_c:
  548. fimc_ctrls_delete(ctx);
  549. error_fh:
  550. v4l2_fh_del(&ctx->fh);
  551. v4l2_fh_exit(&ctx->fh);
  552. kfree(ctx);
  553. unlock:
  554. mutex_unlock(&fimc->lock);
  555. return ret;
  556. }
  557. static int fimc_m2m_release(struct file *file)
  558. {
  559. struct fimc_ctx *ctx = fh_to_ctx(file->private_data);
  560. struct fimc_dev *fimc = ctx->fimc_dev;
  561. dbg("pid: %d, state: 0x%lx, refcnt= %d",
  562. task_pid_nr(current), fimc->state, fimc->m2m.refcnt);
  563. mutex_lock(&fimc->lock);
  564. v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
  565. fimc_ctrls_delete(ctx);
  566. v4l2_fh_del(&ctx->fh);
  567. v4l2_fh_exit(&ctx->fh);
  568. if (--fimc->m2m.refcnt <= 0)
  569. clear_bit(ST_M2M_RUN, &fimc->state);
  570. kfree(ctx);
  571. mutex_unlock(&fimc->lock);
  572. return 0;
  573. }
  574. static const struct v4l2_file_operations fimc_m2m_fops = {
  575. .owner = THIS_MODULE,
  576. .open = fimc_m2m_open,
  577. .release = fimc_m2m_release,
  578. .poll = v4l2_m2m_fop_poll,
  579. .unlocked_ioctl = video_ioctl2,
  580. .mmap = v4l2_m2m_fop_mmap,
  581. };
  582. static struct v4l2_m2m_ops m2m_ops = {
  583. .device_run = fimc_device_run,
  584. .job_abort = fimc_job_abort,
  585. };
  586. int fimc_register_m2m_device(struct fimc_dev *fimc,
  587. struct v4l2_device *v4l2_dev)
  588. {
  589. struct video_device *vfd = &fimc->m2m.vfd;
  590. int ret;
  591. fimc->v4l2_dev = v4l2_dev;
  592. memset(vfd, 0, sizeof(*vfd));
  593. vfd->fops = &fimc_m2m_fops;
  594. vfd->ioctl_ops = &fimc_m2m_ioctl_ops;
  595. vfd->v4l2_dev = v4l2_dev;
  596. vfd->minor = -1;
  597. vfd->release = video_device_release_empty;
  598. vfd->lock = &fimc->lock;
  599. vfd->vfl_dir = VFL_DIR_M2M;
  600. snprintf(vfd->name, sizeof(vfd->name), "fimc.%d.m2m", fimc->id);
  601. video_set_drvdata(vfd, fimc);
  602. fimc->m2m.m2m_dev = v4l2_m2m_init(&m2m_ops);
  603. if (IS_ERR(fimc->m2m.m2m_dev)) {
  604. v4l2_err(v4l2_dev, "failed to initialize v4l2-m2m device\n");
  605. return PTR_ERR(fimc->m2m.m2m_dev);
  606. }
  607. ret = media_entity_pads_init(&vfd->entity, 0, NULL);
  608. if (ret)
  609. goto err_me;
  610. ret = video_register_device(vfd, VFL_TYPE_GRABBER, -1);
  611. if (ret)
  612. goto err_vd;
  613. v4l2_info(v4l2_dev, "Registered %s as /dev/%s\n",
  614. vfd->name, video_device_node_name(vfd));
  615. return 0;
  616. err_vd:
  617. media_entity_cleanup(&vfd->entity);
  618. err_me:
  619. v4l2_m2m_release(fimc->m2m.m2m_dev);
  620. return ret;
  621. }
  622. void fimc_unregister_m2m_device(struct fimc_dev *fimc)
  623. {
  624. if (!fimc)
  625. return;
  626. if (fimc->m2m.m2m_dev)
  627. v4l2_m2m_release(fimc->m2m.m2m_dev);
  628. if (video_is_registered(&fimc->m2m.vfd)) {
  629. video_unregister_device(&fimc->m2m.vfd);
  630. media_entity_cleanup(&fimc->m2m.vfd.entity);
  631. }
  632. }