fimc-m2m.c 19 KB

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