fimc-m2m.c 19 KB

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