fimc-m2m.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  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 = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_M2M_MPLANE;
  201. __fimc_vidioc_querycap(&fimc->pdev->dev, cap, caps);
  202. return 0;
  203. }
  204. static int fimc_m2m_enum_fmt_mplane(struct file *file, void *priv,
  205. struct v4l2_fmtdesc *f)
  206. {
  207. struct fimc_fmt *fmt;
  208. fmt = fimc_find_format(NULL, NULL, get_m2m_fmt_flags(f->type),
  209. f->index);
  210. if (!fmt)
  211. return -EINVAL;
  212. strncpy(f->description, fmt->name, sizeof(f->description) - 1);
  213. f->pixelformat = fmt->fourcc;
  214. return 0;
  215. }
  216. static int fimc_m2m_g_fmt_mplane(struct file *file, void *fh,
  217. struct v4l2_format *f)
  218. {
  219. struct fimc_ctx *ctx = fh_to_ctx(fh);
  220. struct fimc_frame *frame = ctx_get_frame(ctx, f->type);
  221. if (IS_ERR(frame))
  222. return PTR_ERR(frame);
  223. __fimc_get_format(frame, f);
  224. return 0;
  225. }
  226. static int fimc_try_fmt_mplane(struct fimc_ctx *ctx, struct v4l2_format *f)
  227. {
  228. struct fimc_dev *fimc = ctx->fimc_dev;
  229. const struct fimc_variant *variant = fimc->variant;
  230. struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp;
  231. struct fimc_fmt *fmt;
  232. u32 max_w, mod_x, mod_y;
  233. if (!IS_M2M(f->type))
  234. return -EINVAL;
  235. fmt = fimc_find_format(&pix->pixelformat, NULL,
  236. get_m2m_fmt_flags(f->type), 0);
  237. if (WARN(fmt == NULL, "Pixel format lookup failed"))
  238. return -EINVAL;
  239. if (pix->field == V4L2_FIELD_ANY)
  240. pix->field = V4L2_FIELD_NONE;
  241. else if (pix->field != V4L2_FIELD_NONE)
  242. return -EINVAL;
  243. if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  244. max_w = variant->pix_limit->scaler_dis_w;
  245. mod_x = ffs(variant->min_inp_pixsize) - 1;
  246. } else {
  247. max_w = variant->pix_limit->out_rot_dis_w;
  248. mod_x = ffs(variant->min_out_pixsize) - 1;
  249. }
  250. if (tiled_fmt(fmt)) {
  251. mod_x = 6; /* 64 x 32 pixels tile */
  252. mod_y = 5;
  253. } else {
  254. if (variant->min_vsize_align == 1)
  255. mod_y = fimc_fmt_is_rgb(fmt->color) ? 0 : 1;
  256. else
  257. mod_y = ffs(variant->min_vsize_align) - 1;
  258. }
  259. v4l_bound_align_image(&pix->width, 16, max_w, mod_x,
  260. &pix->height, 8, variant->pix_limit->scaler_dis_w, mod_y, 0);
  261. fimc_adjust_mplane_format(fmt, pix->width, pix->height, &f->fmt.pix_mp);
  262. return 0;
  263. }
  264. static int fimc_m2m_try_fmt_mplane(struct file *file, void *fh,
  265. struct v4l2_format *f)
  266. {
  267. struct fimc_ctx *ctx = fh_to_ctx(fh);
  268. return fimc_try_fmt_mplane(ctx, f);
  269. }
  270. static void __set_frame_format(struct fimc_frame *frame, struct fimc_fmt *fmt,
  271. struct v4l2_pix_format_mplane *pixm)
  272. {
  273. int i;
  274. for (i = 0; i < fmt->memplanes; i++) {
  275. frame->bytesperline[i] = pixm->plane_fmt[i].bytesperline;
  276. frame->payload[i] = pixm->plane_fmt[i].sizeimage;
  277. }
  278. frame->f_width = pixm->width;
  279. frame->f_height = pixm->height;
  280. frame->o_width = pixm->width;
  281. frame->o_height = pixm->height;
  282. frame->width = pixm->width;
  283. frame->height = pixm->height;
  284. frame->offs_h = 0;
  285. frame->offs_v = 0;
  286. frame->fmt = fmt;
  287. }
  288. static int fimc_m2m_s_fmt_mplane(struct file *file, void *fh,
  289. struct v4l2_format *f)
  290. {
  291. struct fimc_ctx *ctx = fh_to_ctx(fh);
  292. struct fimc_dev *fimc = ctx->fimc_dev;
  293. struct fimc_fmt *fmt;
  294. struct vb2_queue *vq;
  295. struct fimc_frame *frame;
  296. int ret;
  297. ret = fimc_try_fmt_mplane(ctx, f);
  298. if (ret)
  299. return ret;
  300. vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
  301. if (vb2_is_busy(vq)) {
  302. v4l2_err(&fimc->m2m.vfd, "queue (%d) busy\n", f->type);
  303. return -EBUSY;
  304. }
  305. if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
  306. frame = &ctx->s_frame;
  307. else
  308. frame = &ctx->d_frame;
  309. fmt = fimc_find_format(&f->fmt.pix_mp.pixelformat, NULL,
  310. get_m2m_fmt_flags(f->type), 0);
  311. if (!fmt)
  312. return -EINVAL;
  313. __set_frame_format(frame, fmt, &f->fmt.pix_mp);
  314. /* Update RGB Alpha control state and value range */
  315. fimc_alpha_ctrl_update(ctx);
  316. return 0;
  317. }
  318. static int fimc_m2m_cropcap(struct file *file, void *fh,
  319. struct v4l2_cropcap *cr)
  320. {
  321. struct fimc_ctx *ctx = fh_to_ctx(fh);
  322. struct fimc_frame *frame;
  323. frame = ctx_get_frame(ctx, cr->type);
  324. if (IS_ERR(frame))
  325. return PTR_ERR(frame);
  326. cr->bounds.left = 0;
  327. cr->bounds.top = 0;
  328. cr->bounds.width = frame->o_width;
  329. cr->bounds.height = frame->o_height;
  330. cr->defrect = cr->bounds;
  331. return 0;
  332. }
  333. static int fimc_m2m_g_crop(struct file *file, void *fh, struct v4l2_crop *cr)
  334. {
  335. struct fimc_ctx *ctx = fh_to_ctx(fh);
  336. struct fimc_frame *frame;
  337. frame = ctx_get_frame(ctx, cr->type);
  338. if (IS_ERR(frame))
  339. return PTR_ERR(frame);
  340. cr->c.left = frame->offs_h;
  341. cr->c.top = frame->offs_v;
  342. cr->c.width = frame->width;
  343. cr->c.height = frame->height;
  344. return 0;
  345. }
  346. static int fimc_m2m_try_crop(struct fimc_ctx *ctx, struct v4l2_crop *cr)
  347. {
  348. struct fimc_dev *fimc = ctx->fimc_dev;
  349. struct fimc_frame *f;
  350. u32 min_size, halign, depth = 0;
  351. int i;
  352. if (cr->c.top < 0 || cr->c.left < 0) {
  353. v4l2_err(&fimc->m2m.vfd,
  354. "doesn't support negative values for top & left\n");
  355. return -EINVAL;
  356. }
  357. if (cr->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
  358. f = &ctx->d_frame;
  359. else if (cr->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
  360. f = &ctx->s_frame;
  361. else
  362. return -EINVAL;
  363. min_size = (f == &ctx->s_frame) ?
  364. fimc->variant->min_inp_pixsize : fimc->variant->min_out_pixsize;
  365. /* Get pixel alignment constraints. */
  366. if (fimc->variant->min_vsize_align == 1)
  367. halign = fimc_fmt_is_rgb(f->fmt->color) ? 0 : 1;
  368. else
  369. halign = ffs(fimc->variant->min_vsize_align) - 1;
  370. for (i = 0; i < f->fmt->memplanes; i++)
  371. depth += f->fmt->depth[i];
  372. v4l_bound_align_image(&cr->c.width, min_size, f->o_width,
  373. ffs(min_size) - 1,
  374. &cr->c.height, min_size, f->o_height,
  375. halign, 64/(ALIGN(depth, 8)));
  376. /* adjust left/top if cropping rectangle is out of bounds */
  377. if (cr->c.left + cr->c.width > f->o_width)
  378. cr->c.left = f->o_width - cr->c.width;
  379. if (cr->c.top + cr->c.height > f->o_height)
  380. cr->c.top = f->o_height - cr->c.height;
  381. cr->c.left = round_down(cr->c.left, min_size);
  382. cr->c.top = round_down(cr->c.top, fimc->variant->hor_offs_align);
  383. dbg("l:%d, t:%d, w:%d, h:%d, f_w: %d, f_h: %d",
  384. cr->c.left, cr->c.top, cr->c.width, cr->c.height,
  385. f->f_width, f->f_height);
  386. return 0;
  387. }
  388. static int fimc_m2m_s_crop(struct file *file, void *fh, const struct v4l2_crop *crop)
  389. {
  390. struct fimc_ctx *ctx = fh_to_ctx(fh);
  391. struct fimc_dev *fimc = ctx->fimc_dev;
  392. struct v4l2_crop cr = *crop;
  393. struct fimc_frame *f;
  394. int ret;
  395. ret = fimc_m2m_try_crop(ctx, &cr);
  396. if (ret)
  397. return ret;
  398. f = (cr.type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) ?
  399. &ctx->s_frame : &ctx->d_frame;
  400. /* Check to see if scaling ratio is within supported range */
  401. if (cr.type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  402. ret = fimc_check_scaler_ratio(ctx, cr.c.width,
  403. cr.c.height, ctx->d_frame.width,
  404. ctx->d_frame.height, ctx->rotation);
  405. } else {
  406. ret = fimc_check_scaler_ratio(ctx, ctx->s_frame.width,
  407. ctx->s_frame.height, cr.c.width,
  408. cr.c.height, ctx->rotation);
  409. }
  410. if (ret) {
  411. v4l2_err(&fimc->m2m.vfd, "Out of scaler range\n");
  412. return -EINVAL;
  413. }
  414. f->offs_h = cr.c.left;
  415. f->offs_v = cr.c.top;
  416. f->width = cr.c.width;
  417. f->height = cr.c.height;
  418. fimc_ctx_state_set(FIMC_PARAMS, ctx);
  419. return 0;
  420. }
  421. static const struct v4l2_ioctl_ops fimc_m2m_ioctl_ops = {
  422. .vidioc_querycap = fimc_m2m_querycap,
  423. .vidioc_enum_fmt_vid_cap_mplane = fimc_m2m_enum_fmt_mplane,
  424. .vidioc_enum_fmt_vid_out_mplane = fimc_m2m_enum_fmt_mplane,
  425. .vidioc_g_fmt_vid_cap_mplane = fimc_m2m_g_fmt_mplane,
  426. .vidioc_g_fmt_vid_out_mplane = fimc_m2m_g_fmt_mplane,
  427. .vidioc_try_fmt_vid_cap_mplane = fimc_m2m_try_fmt_mplane,
  428. .vidioc_try_fmt_vid_out_mplane = fimc_m2m_try_fmt_mplane,
  429. .vidioc_s_fmt_vid_cap_mplane = fimc_m2m_s_fmt_mplane,
  430. .vidioc_s_fmt_vid_out_mplane = fimc_m2m_s_fmt_mplane,
  431. .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
  432. .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
  433. .vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
  434. .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
  435. .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
  436. .vidioc_streamon = v4l2_m2m_ioctl_streamon,
  437. .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
  438. .vidioc_g_crop = fimc_m2m_g_crop,
  439. .vidioc_s_crop = fimc_m2m_s_crop,
  440. .vidioc_cropcap = fimc_m2m_cropcap
  441. };
  442. static int queue_init(void *priv, struct vb2_queue *src_vq,
  443. struct vb2_queue *dst_vq)
  444. {
  445. struct fimc_ctx *ctx = priv;
  446. int ret;
  447. src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
  448. src_vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
  449. src_vq->drv_priv = ctx;
  450. src_vq->ops = &fimc_qops;
  451. src_vq->mem_ops = &vb2_dma_contig_memops;
  452. src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
  453. src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
  454. src_vq->lock = &ctx->fimc_dev->lock;
  455. src_vq->dev = &ctx->fimc_dev->pdev->dev;
  456. ret = vb2_queue_init(src_vq);
  457. if (ret)
  458. return ret;
  459. dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
  460. dst_vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
  461. dst_vq->drv_priv = ctx;
  462. dst_vq->ops = &fimc_qops;
  463. dst_vq->mem_ops = &vb2_dma_contig_memops;
  464. dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
  465. dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
  466. dst_vq->lock = &ctx->fimc_dev->lock;
  467. dst_vq->dev = &ctx->fimc_dev->pdev->dev;
  468. return vb2_queue_init(dst_vq);
  469. }
  470. static int fimc_m2m_set_default_format(struct fimc_ctx *ctx)
  471. {
  472. struct v4l2_pix_format_mplane pixm = {
  473. .pixelformat = V4L2_PIX_FMT_RGB32,
  474. .width = 800,
  475. .height = 600,
  476. .plane_fmt[0] = {
  477. .bytesperline = 800 * 4,
  478. .sizeimage = 800 * 4 * 600,
  479. },
  480. };
  481. struct fimc_fmt *fmt;
  482. fmt = fimc_find_format(&pixm.pixelformat, NULL, FMT_FLAGS_M2M, 0);
  483. if (!fmt)
  484. return -EINVAL;
  485. __set_frame_format(&ctx->s_frame, fmt, &pixm);
  486. __set_frame_format(&ctx->d_frame, fmt, &pixm);
  487. return 0;
  488. }
  489. static int fimc_m2m_open(struct file *file)
  490. {
  491. struct fimc_dev *fimc = video_drvdata(file);
  492. struct fimc_ctx *ctx;
  493. int ret = -EBUSY;
  494. pr_debug("pid: %d, state: %#lx\n", task_pid_nr(current), fimc->state);
  495. if (mutex_lock_interruptible(&fimc->lock))
  496. return -ERESTARTSYS;
  497. /*
  498. * Don't allow simultaneous open() of the mem-to-mem and the
  499. * capture video node that belong to same FIMC IP instance.
  500. */
  501. if (test_bit(ST_CAPT_BUSY, &fimc->state))
  502. goto unlock;
  503. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  504. if (!ctx) {
  505. ret = -ENOMEM;
  506. goto unlock;
  507. }
  508. v4l2_fh_init(&ctx->fh, &fimc->m2m.vfd);
  509. ctx->fimc_dev = fimc;
  510. /* Default color format */
  511. ctx->s_frame.fmt = fimc_get_format(0);
  512. ctx->d_frame.fmt = fimc_get_format(0);
  513. ret = fimc_ctrls_create(ctx);
  514. if (ret)
  515. goto error_fh;
  516. /* Use separate control handler per file handle */
  517. ctx->fh.ctrl_handler = &ctx->ctrls.handler;
  518. file->private_data = &ctx->fh;
  519. v4l2_fh_add(&ctx->fh);
  520. /* Setup the device context for memory-to-memory mode */
  521. ctx->state = FIMC_CTX_M2M;
  522. ctx->flags = 0;
  523. ctx->in_path = FIMC_IO_DMA;
  524. ctx->out_path = FIMC_IO_DMA;
  525. ctx->scaler.enabled = 1;
  526. ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(fimc->m2m.m2m_dev, ctx, queue_init);
  527. if (IS_ERR(ctx->fh.m2m_ctx)) {
  528. ret = PTR_ERR(ctx->fh.m2m_ctx);
  529. goto error_c;
  530. }
  531. if (fimc->m2m.refcnt++ == 0)
  532. set_bit(ST_M2M_RUN, &fimc->state);
  533. ret = fimc_m2m_set_default_format(ctx);
  534. if (ret < 0)
  535. goto error_m2m_ctx;
  536. mutex_unlock(&fimc->lock);
  537. return 0;
  538. error_m2m_ctx:
  539. v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
  540. error_c:
  541. fimc_ctrls_delete(ctx);
  542. v4l2_fh_del(&ctx->fh);
  543. error_fh:
  544. v4l2_fh_exit(&ctx->fh);
  545. kfree(ctx);
  546. unlock:
  547. mutex_unlock(&fimc->lock);
  548. return ret;
  549. }
  550. static int fimc_m2m_release(struct file *file)
  551. {
  552. struct fimc_ctx *ctx = fh_to_ctx(file->private_data);
  553. struct fimc_dev *fimc = ctx->fimc_dev;
  554. dbg("pid: %d, state: 0x%lx, refcnt= %d",
  555. task_pid_nr(current), fimc->state, fimc->m2m.refcnt);
  556. mutex_lock(&fimc->lock);
  557. v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
  558. fimc_ctrls_delete(ctx);
  559. v4l2_fh_del(&ctx->fh);
  560. v4l2_fh_exit(&ctx->fh);
  561. if (--fimc->m2m.refcnt <= 0)
  562. clear_bit(ST_M2M_RUN, &fimc->state);
  563. kfree(ctx);
  564. mutex_unlock(&fimc->lock);
  565. return 0;
  566. }
  567. static const struct v4l2_file_operations fimc_m2m_fops = {
  568. .owner = THIS_MODULE,
  569. .open = fimc_m2m_open,
  570. .release = fimc_m2m_release,
  571. .poll = v4l2_m2m_fop_poll,
  572. .unlocked_ioctl = video_ioctl2,
  573. .mmap = v4l2_m2m_fop_mmap,
  574. };
  575. static const struct v4l2_m2m_ops m2m_ops = {
  576. .device_run = fimc_device_run,
  577. .job_abort = fimc_job_abort,
  578. };
  579. int fimc_register_m2m_device(struct fimc_dev *fimc,
  580. struct v4l2_device *v4l2_dev)
  581. {
  582. struct video_device *vfd = &fimc->m2m.vfd;
  583. int ret;
  584. fimc->v4l2_dev = v4l2_dev;
  585. memset(vfd, 0, sizeof(*vfd));
  586. vfd->fops = &fimc_m2m_fops;
  587. vfd->ioctl_ops = &fimc_m2m_ioctl_ops;
  588. vfd->v4l2_dev = v4l2_dev;
  589. vfd->minor = -1;
  590. vfd->release = video_device_release_empty;
  591. vfd->lock = &fimc->lock;
  592. vfd->vfl_dir = VFL_DIR_M2M;
  593. snprintf(vfd->name, sizeof(vfd->name), "fimc.%d.m2m", fimc->id);
  594. video_set_drvdata(vfd, fimc);
  595. fimc->m2m.m2m_dev = v4l2_m2m_init(&m2m_ops);
  596. if (IS_ERR(fimc->m2m.m2m_dev)) {
  597. v4l2_err(v4l2_dev, "failed to initialize v4l2-m2m device\n");
  598. return PTR_ERR(fimc->m2m.m2m_dev);
  599. }
  600. ret = media_entity_pads_init(&vfd->entity, 0, NULL);
  601. if (ret)
  602. goto err_me;
  603. ret = video_register_device(vfd, VFL_TYPE_GRABBER, -1);
  604. if (ret)
  605. goto err_vd;
  606. v4l2_info(v4l2_dev, "Registered %s as /dev/%s\n",
  607. vfd->name, video_device_node_name(vfd));
  608. return 0;
  609. err_vd:
  610. media_entity_cleanup(&vfd->entity);
  611. err_me:
  612. v4l2_m2m_release(fimc->m2m.m2m_dev);
  613. return ret;
  614. }
  615. void fimc_unregister_m2m_device(struct fimc_dev *fimc)
  616. {
  617. if (!fimc)
  618. return;
  619. if (fimc->m2m.m2m_dev)
  620. v4l2_m2m_release(fimc->m2m.m2m_dev);
  621. if (video_is_registered(&fimc->m2m.vfd)) {
  622. video_unregister_device(&fimc->m2m.vfd);
  623. media_entity_cleanup(&fimc->m2m.vfd.entity);
  624. }
  625. }