fimc-m2m.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849
  1. /*
  2. * Samsung S5P/EXYNOS4 SoC series FIMC (video postprocessor) driver
  3. *
  4. * Copyright (C) 2012 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-core.h>
  27. #include <media/videobuf2-dma-contig.h>
  28. #include "fimc-core.h"
  29. #include "fimc-reg.h"
  30. #include "fimc-mdevice.h"
  31. static unsigned int get_m2m_fmt_flags(unsigned int stream_type)
  32. {
  33. if (stream_type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
  34. return FMT_FLAGS_M2M_IN;
  35. else
  36. return FMT_FLAGS_M2M_OUT;
  37. }
  38. void fimc_m2m_job_finish(struct fimc_ctx *ctx, int vb_state)
  39. {
  40. struct vb2_buffer *src_vb, *dst_vb;
  41. if (!ctx || !ctx->m2m_ctx)
  42. return;
  43. src_vb = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
  44. dst_vb = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
  45. if (src_vb && dst_vb) {
  46. v4l2_m2m_buf_done(src_vb, vb_state);
  47. v4l2_m2m_buf_done(dst_vb, vb_state);
  48. v4l2_m2m_job_finish(ctx->fimc_dev->m2m.m2m_dev,
  49. ctx->m2m_ctx);
  50. }
  51. }
  52. /* Complete the transaction which has been scheduled for execution. */
  53. static int fimc_m2m_shutdown(struct fimc_ctx *ctx)
  54. {
  55. struct fimc_dev *fimc = ctx->fimc_dev;
  56. int ret;
  57. if (!fimc_m2m_pending(fimc))
  58. return 0;
  59. fimc_ctx_state_set(FIMC_CTX_SHUT, ctx);
  60. ret = wait_event_timeout(fimc->irq_queue,
  61. !fimc_ctx_state_is_set(FIMC_CTX_SHUT, ctx),
  62. FIMC_SHUTDOWN_TIMEOUT);
  63. return ret == 0 ? -ETIMEDOUT : ret;
  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 int stop_streaming(struct vb2_queue *q)
  73. {
  74. struct fimc_ctx *ctx = q->drv_priv;
  75. int ret;
  76. ret = fimc_m2m_shutdown(ctx);
  77. if (ret == -ETIMEDOUT)
  78. fimc_m2m_job_finish(ctx, VB2_BUF_STATE_ERROR);
  79. pm_runtime_put(&ctx->fimc_dev->pdev->dev);
  80. return 0;
  81. }
  82. static void fimc_device_run(void *priv)
  83. {
  84. struct vb2_buffer *vb = NULL;
  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. vb = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
  103. ret = fimc_prepare_addr(ctx, vb, sf, &sf->paddr);
  104. if (ret)
  105. goto dma_unlock;
  106. vb = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
  107. ret = fimc_prepare_addr(ctx, vb, df, &df->paddr);
  108. if (ret)
  109. goto dma_unlock;
  110. /* Reconfigure hardware if the context has changed. */
  111. if (fimc->m2m.ctx != ctx) {
  112. ctx->state |= FIMC_PARAMS;
  113. fimc->m2m.ctx = ctx;
  114. }
  115. if (ctx->state & FIMC_PARAMS) {
  116. fimc_set_yuv_order(ctx);
  117. fimc_hw_set_input_path(ctx);
  118. fimc_hw_set_in_dma(ctx);
  119. ret = fimc_set_scaler_info(ctx);
  120. if (ret)
  121. goto dma_unlock;
  122. fimc_hw_set_prescaler(ctx);
  123. fimc_hw_set_mainscaler(ctx);
  124. fimc_hw_set_target_format(ctx);
  125. fimc_hw_set_rotation(ctx);
  126. fimc_hw_set_effect(ctx);
  127. fimc_hw_set_out_dma(ctx);
  128. if (fimc->variant->has_alpha)
  129. fimc_hw_set_rgb_alpha(ctx);
  130. fimc_hw_set_output_path(ctx);
  131. }
  132. fimc_hw_set_input_addr(fimc, &sf->paddr);
  133. fimc_hw_set_output_addr(fimc, &df->paddr, -1);
  134. fimc_activate_capture(ctx);
  135. ctx->state &= (FIMC_CTX_M2M | FIMC_CTX_CAP |
  136. FIMC_SRC_FMT | FIMC_DST_FMT);
  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, const struct v4l2_format *fmt,
  146. unsigned int *num_buffers, unsigned int *num_planes,
  147. unsigned int sizes[], void *allocators[])
  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-contigous 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->f_width * f->f_height * f->fmt->depth[i]) / 8;
  164. allocators[i] = ctx->fimc_dev->alloc_ctx;
  165. }
  166. return 0;
  167. }
  168. static int fimc_buf_prepare(struct vb2_buffer *vb)
  169. {
  170. struct fimc_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  171. struct fimc_frame *frame;
  172. int i;
  173. frame = ctx_get_frame(ctx, vb->vb2_queue->type);
  174. if (IS_ERR(frame))
  175. return PTR_ERR(frame);
  176. for (i = 0; i < frame->fmt->memplanes; i++)
  177. vb2_set_plane_payload(vb, i, frame->payload[i]);
  178. return 0;
  179. }
  180. static void fimc_buf_queue(struct vb2_buffer *vb)
  181. {
  182. struct fimc_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  183. dbg("ctx: %p, ctx->state: 0x%x", ctx, ctx->state);
  184. if (ctx->m2m_ctx)
  185. v4l2_m2m_buf_queue(ctx->m2m_ctx, vb);
  186. }
  187. static void fimc_lock(struct vb2_queue *vq)
  188. {
  189. struct fimc_ctx *ctx = vb2_get_drv_priv(vq);
  190. mutex_lock(&ctx->fimc_dev->lock);
  191. }
  192. static void fimc_unlock(struct vb2_queue *vq)
  193. {
  194. struct fimc_ctx *ctx = vb2_get_drv_priv(vq);
  195. mutex_unlock(&ctx->fimc_dev->lock);
  196. }
  197. static struct vb2_ops fimc_qops = {
  198. .queue_setup = fimc_queue_setup,
  199. .buf_prepare = fimc_buf_prepare,
  200. .buf_queue = fimc_buf_queue,
  201. .wait_prepare = fimc_unlock,
  202. .wait_finish = fimc_lock,
  203. .stop_streaming = stop_streaming,
  204. .start_streaming = start_streaming,
  205. };
  206. /*
  207. * V4L2 ioctl handlers
  208. */
  209. static int fimc_m2m_querycap(struct file *file, void *fh,
  210. struct v4l2_capability *cap)
  211. {
  212. struct fimc_ctx *ctx = fh_to_ctx(fh);
  213. struct fimc_dev *fimc = ctx->fimc_dev;
  214. strncpy(cap->driver, fimc->pdev->name, sizeof(cap->driver) - 1);
  215. strncpy(cap->card, fimc->pdev->name, sizeof(cap->card) - 1);
  216. cap->bus_info[0] = 0;
  217. /*
  218. * This is only a mem-to-mem video device. The capture and output
  219. * device capability flags are left only for backward compatibility
  220. * and are scheduled for removal.
  221. */
  222. cap->capabilities = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_M2M_MPLANE |
  223. V4L2_CAP_VIDEO_CAPTURE_MPLANE | V4L2_CAP_VIDEO_OUTPUT_MPLANE;
  224. return 0;
  225. }
  226. static int fimc_m2m_enum_fmt_mplane(struct file *file, void *priv,
  227. struct v4l2_fmtdesc *f)
  228. {
  229. struct fimc_fmt *fmt;
  230. fmt = fimc_find_format(NULL, NULL, get_m2m_fmt_flags(f->type),
  231. f->index);
  232. if (!fmt)
  233. return -EINVAL;
  234. strncpy(f->description, fmt->name, sizeof(f->description) - 1);
  235. f->pixelformat = fmt->fourcc;
  236. return 0;
  237. }
  238. static int fimc_m2m_g_fmt_mplane(struct file *file, void *fh,
  239. struct v4l2_format *f)
  240. {
  241. struct fimc_ctx *ctx = fh_to_ctx(fh);
  242. struct fimc_frame *frame = ctx_get_frame(ctx, f->type);
  243. if (IS_ERR(frame))
  244. return PTR_ERR(frame);
  245. return fimc_fill_format(frame, f);
  246. }
  247. static int fimc_try_fmt_mplane(struct fimc_ctx *ctx, struct v4l2_format *f)
  248. {
  249. struct fimc_dev *fimc = ctx->fimc_dev;
  250. struct fimc_variant *variant = fimc->variant;
  251. struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp;
  252. struct fimc_fmt *fmt;
  253. u32 max_w, mod_x, mod_y;
  254. if (!IS_M2M(f->type))
  255. return -EINVAL;
  256. dbg("w: %d, h: %d", pix->width, pix->height);
  257. fmt = fimc_find_format(&pix->pixelformat, NULL,
  258. get_m2m_fmt_flags(f->type), 0);
  259. if (WARN(fmt == NULL, "Pixel format lookup failed"))
  260. return -EINVAL;
  261. if (pix->field == V4L2_FIELD_ANY)
  262. pix->field = V4L2_FIELD_NONE;
  263. else if (pix->field != V4L2_FIELD_NONE)
  264. return -EINVAL;
  265. if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  266. max_w = variant->pix_limit->scaler_dis_w;
  267. mod_x = ffs(variant->min_inp_pixsize) - 1;
  268. } else {
  269. max_w = variant->pix_limit->out_rot_dis_w;
  270. mod_x = ffs(variant->min_out_pixsize) - 1;
  271. }
  272. if (tiled_fmt(fmt)) {
  273. mod_x = 6; /* 64 x 32 pixels tile */
  274. mod_y = 5;
  275. } else {
  276. if (variant->min_vsize_align == 1)
  277. mod_y = fimc_fmt_is_rgb(fmt->color) ? 0 : 1;
  278. else
  279. mod_y = ffs(variant->min_vsize_align) - 1;
  280. }
  281. v4l_bound_align_image(&pix->width, 16, max_w, mod_x,
  282. &pix->height, 8, variant->pix_limit->scaler_dis_w, mod_y, 0);
  283. fimc_adjust_mplane_format(fmt, pix->width, pix->height, &f->fmt.pix_mp);
  284. return 0;
  285. }
  286. static int fimc_m2m_try_fmt_mplane(struct file *file, void *fh,
  287. struct v4l2_format *f)
  288. {
  289. struct fimc_ctx *ctx = fh_to_ctx(fh);
  290. return fimc_try_fmt_mplane(ctx, f);
  291. }
  292. static int fimc_m2m_s_fmt_mplane(struct file *file, void *fh,
  293. struct v4l2_format *f)
  294. {
  295. struct fimc_ctx *ctx = fh_to_ctx(fh);
  296. struct fimc_dev *fimc = ctx->fimc_dev;
  297. struct vb2_queue *vq;
  298. struct fimc_frame *frame;
  299. struct v4l2_pix_format_mplane *pix;
  300. int i, ret = 0;
  301. ret = fimc_try_fmt_mplane(ctx, f);
  302. if (ret)
  303. return ret;
  304. vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
  305. if (vb2_is_busy(vq)) {
  306. v4l2_err(&fimc->m2m.vfd, "queue (%d) busy\n", f->type);
  307. return -EBUSY;
  308. }
  309. if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
  310. frame = &ctx->s_frame;
  311. else
  312. frame = &ctx->d_frame;
  313. pix = &f->fmt.pix_mp;
  314. frame->fmt = fimc_find_format(&pix->pixelformat, NULL,
  315. get_m2m_fmt_flags(f->type), 0);
  316. if (!frame->fmt)
  317. return -EINVAL;
  318. /* Update RGB Alpha control state and value range */
  319. fimc_alpha_ctrl_update(ctx);
  320. for (i = 0; i < frame->fmt->colplanes; i++) {
  321. frame->payload[i] =
  322. (pix->width * pix->height * frame->fmt->depth[i]) / 8;
  323. }
  324. fimc_fill_frame(frame, f);
  325. ctx->scaler.enabled = 1;
  326. if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
  327. fimc_ctx_state_set(FIMC_PARAMS | FIMC_DST_FMT, ctx);
  328. else
  329. fimc_ctx_state_set(FIMC_PARAMS | FIMC_SRC_FMT, ctx);
  330. dbg("f_w: %d, f_h: %d", frame->f_width, frame->f_height);
  331. return 0;
  332. }
  333. static int fimc_m2m_reqbufs(struct file *file, void *fh,
  334. struct v4l2_requestbuffers *reqbufs)
  335. {
  336. struct fimc_ctx *ctx = fh_to_ctx(fh);
  337. return v4l2_m2m_reqbufs(file, ctx->m2m_ctx, reqbufs);
  338. }
  339. static int fimc_m2m_querybuf(struct file *file, void *fh,
  340. struct v4l2_buffer *buf)
  341. {
  342. struct fimc_ctx *ctx = fh_to_ctx(fh);
  343. return v4l2_m2m_querybuf(file, ctx->m2m_ctx, buf);
  344. }
  345. static int fimc_m2m_qbuf(struct file *file, void *fh,
  346. struct v4l2_buffer *buf)
  347. {
  348. struct fimc_ctx *ctx = fh_to_ctx(fh);
  349. return v4l2_m2m_qbuf(file, ctx->m2m_ctx, buf);
  350. }
  351. static int fimc_m2m_dqbuf(struct file *file, void *fh,
  352. struct v4l2_buffer *buf)
  353. {
  354. struct fimc_ctx *ctx = fh_to_ctx(fh);
  355. return v4l2_m2m_dqbuf(file, ctx->m2m_ctx, buf);
  356. }
  357. static int fimc_m2m_expbuf(struct file *file, void *fh,
  358. struct v4l2_exportbuffer *eb)
  359. {
  360. struct fimc_ctx *ctx = fh_to_ctx(fh);
  361. return v4l2_m2m_expbuf(file, ctx->m2m_ctx, eb);
  362. }
  363. static int fimc_m2m_streamon(struct file *file, void *fh,
  364. enum v4l2_buf_type type)
  365. {
  366. struct fimc_ctx *ctx = fh_to_ctx(fh);
  367. /* The source and target color format need to be set */
  368. if (V4L2_TYPE_IS_OUTPUT(type)) {
  369. if (!fimc_ctx_state_is_set(FIMC_SRC_FMT, ctx))
  370. return -EINVAL;
  371. } else if (!fimc_ctx_state_is_set(FIMC_DST_FMT, ctx)) {
  372. return -EINVAL;
  373. }
  374. return v4l2_m2m_streamon(file, ctx->m2m_ctx, type);
  375. }
  376. static int fimc_m2m_streamoff(struct file *file, void *fh,
  377. enum v4l2_buf_type type)
  378. {
  379. struct fimc_ctx *ctx = fh_to_ctx(fh);
  380. return v4l2_m2m_streamoff(file, ctx->m2m_ctx, type);
  381. }
  382. static int fimc_m2m_cropcap(struct file *file, void *fh,
  383. struct v4l2_cropcap *cr)
  384. {
  385. struct fimc_ctx *ctx = fh_to_ctx(fh);
  386. struct fimc_frame *frame;
  387. frame = ctx_get_frame(ctx, cr->type);
  388. if (IS_ERR(frame))
  389. return PTR_ERR(frame);
  390. cr->bounds.left = 0;
  391. cr->bounds.top = 0;
  392. cr->bounds.width = frame->o_width;
  393. cr->bounds.height = frame->o_height;
  394. cr->defrect = cr->bounds;
  395. return 0;
  396. }
  397. static int fimc_m2m_g_crop(struct file *file, void *fh, struct v4l2_crop *cr)
  398. {
  399. struct fimc_ctx *ctx = fh_to_ctx(fh);
  400. struct fimc_frame *frame;
  401. frame = ctx_get_frame(ctx, cr->type);
  402. if (IS_ERR(frame))
  403. return PTR_ERR(frame);
  404. cr->c.left = frame->offs_h;
  405. cr->c.top = frame->offs_v;
  406. cr->c.width = frame->width;
  407. cr->c.height = frame->height;
  408. return 0;
  409. }
  410. static int fimc_m2m_try_crop(struct fimc_ctx *ctx, struct v4l2_crop *cr)
  411. {
  412. struct fimc_dev *fimc = ctx->fimc_dev;
  413. struct fimc_frame *f;
  414. u32 min_size, halign, depth = 0;
  415. int i;
  416. if (cr->c.top < 0 || cr->c.left < 0) {
  417. v4l2_err(&fimc->m2m.vfd,
  418. "doesn't support negative values for top & left\n");
  419. return -EINVAL;
  420. }
  421. if (cr->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
  422. f = &ctx->d_frame;
  423. else if (cr->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
  424. f = &ctx->s_frame;
  425. else
  426. return -EINVAL;
  427. min_size = (f == &ctx->s_frame) ?
  428. fimc->variant->min_inp_pixsize : fimc->variant->min_out_pixsize;
  429. /* Get pixel alignment constraints. */
  430. if (fimc->variant->min_vsize_align == 1)
  431. halign = fimc_fmt_is_rgb(f->fmt->color) ? 0 : 1;
  432. else
  433. halign = ffs(fimc->variant->min_vsize_align) - 1;
  434. for (i = 0; i < f->fmt->colplanes; i++)
  435. depth += f->fmt->depth[i];
  436. v4l_bound_align_image(&cr->c.width, min_size, f->o_width,
  437. ffs(min_size) - 1,
  438. &cr->c.height, min_size, f->o_height,
  439. halign, 64/(ALIGN(depth, 8)));
  440. /* adjust left/top if cropping rectangle is out of bounds */
  441. if (cr->c.left + cr->c.width > f->o_width)
  442. cr->c.left = f->o_width - cr->c.width;
  443. if (cr->c.top + cr->c.height > f->o_height)
  444. cr->c.top = f->o_height - cr->c.height;
  445. cr->c.left = round_down(cr->c.left, min_size);
  446. cr->c.top = round_down(cr->c.top, fimc->variant->hor_offs_align);
  447. dbg("l:%d, t:%d, w:%d, h:%d, f_w: %d, f_h: %d",
  448. cr->c.left, cr->c.top, cr->c.width, cr->c.height,
  449. f->f_width, f->f_height);
  450. return 0;
  451. }
  452. static int fimc_m2m_s_crop(struct file *file, void *fh, const struct v4l2_crop *crop)
  453. {
  454. struct fimc_ctx *ctx = fh_to_ctx(fh);
  455. struct fimc_dev *fimc = ctx->fimc_dev;
  456. struct v4l2_crop cr = *crop;
  457. struct fimc_frame *f;
  458. int ret;
  459. ret = fimc_m2m_try_crop(ctx, &cr);
  460. if (ret)
  461. return ret;
  462. f = (cr.type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) ?
  463. &ctx->s_frame : &ctx->d_frame;
  464. /* Check to see if scaling ratio is within supported range */
  465. if (fimc_ctx_state_is_set(FIMC_DST_FMT | FIMC_SRC_FMT, ctx)) {
  466. if (cr.type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  467. ret = fimc_check_scaler_ratio(ctx, cr.c.width,
  468. cr.c.height, ctx->d_frame.width,
  469. ctx->d_frame.height, ctx->rotation);
  470. } else {
  471. ret = fimc_check_scaler_ratio(ctx, ctx->s_frame.width,
  472. ctx->s_frame.height, cr.c.width,
  473. cr.c.height, ctx->rotation);
  474. }
  475. if (ret) {
  476. v4l2_err(&fimc->m2m.vfd, "Out of scaler range\n");
  477. return -EINVAL;
  478. }
  479. }
  480. f->offs_h = cr.c.left;
  481. f->offs_v = cr.c.top;
  482. f->width = cr.c.width;
  483. f->height = cr.c.height;
  484. fimc_ctx_state_set(FIMC_PARAMS, ctx);
  485. return 0;
  486. }
  487. static const struct v4l2_ioctl_ops fimc_m2m_ioctl_ops = {
  488. .vidioc_querycap = fimc_m2m_querycap,
  489. .vidioc_enum_fmt_vid_cap_mplane = fimc_m2m_enum_fmt_mplane,
  490. .vidioc_enum_fmt_vid_out_mplane = fimc_m2m_enum_fmt_mplane,
  491. .vidioc_g_fmt_vid_cap_mplane = fimc_m2m_g_fmt_mplane,
  492. .vidioc_g_fmt_vid_out_mplane = fimc_m2m_g_fmt_mplane,
  493. .vidioc_try_fmt_vid_cap_mplane = fimc_m2m_try_fmt_mplane,
  494. .vidioc_try_fmt_vid_out_mplane = fimc_m2m_try_fmt_mplane,
  495. .vidioc_s_fmt_vid_cap_mplane = fimc_m2m_s_fmt_mplane,
  496. .vidioc_s_fmt_vid_out_mplane = fimc_m2m_s_fmt_mplane,
  497. .vidioc_reqbufs = fimc_m2m_reqbufs,
  498. .vidioc_querybuf = fimc_m2m_querybuf,
  499. .vidioc_qbuf = fimc_m2m_qbuf,
  500. .vidioc_dqbuf = fimc_m2m_dqbuf,
  501. .vidioc_expbuf = fimc_m2m_expbuf,
  502. .vidioc_streamon = fimc_m2m_streamon,
  503. .vidioc_streamoff = fimc_m2m_streamoff,
  504. .vidioc_g_crop = fimc_m2m_g_crop,
  505. .vidioc_s_crop = fimc_m2m_s_crop,
  506. .vidioc_cropcap = fimc_m2m_cropcap
  507. };
  508. static int queue_init(void *priv, struct vb2_queue *src_vq,
  509. struct vb2_queue *dst_vq)
  510. {
  511. struct fimc_ctx *ctx = priv;
  512. int ret;
  513. src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
  514. src_vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
  515. src_vq->drv_priv = ctx;
  516. src_vq->ops = &fimc_qops;
  517. src_vq->mem_ops = &vb2_dma_contig_memops;
  518. src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
  519. ret = vb2_queue_init(src_vq);
  520. if (ret)
  521. return ret;
  522. dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
  523. dst_vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
  524. dst_vq->drv_priv = ctx;
  525. dst_vq->ops = &fimc_qops;
  526. dst_vq->mem_ops = &vb2_dma_contig_memops;
  527. dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
  528. return vb2_queue_init(dst_vq);
  529. }
  530. static int fimc_m2m_open(struct file *file)
  531. {
  532. struct fimc_dev *fimc = video_drvdata(file);
  533. struct fimc_ctx *ctx;
  534. int ret = -EBUSY;
  535. dbg("pid: %d, state: 0x%lx, refcnt: %d",
  536. task_pid_nr(current), fimc->state, fimc->vid_cap.refcnt);
  537. if (mutex_lock_interruptible(&fimc->lock))
  538. return -ERESTARTSYS;
  539. /*
  540. * Return if the corresponding video capture node
  541. * is already opened.
  542. */
  543. if (fimc->vid_cap.refcnt > 0)
  544. goto unlock;
  545. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  546. if (!ctx) {
  547. ret = -ENOMEM;
  548. goto unlock;
  549. }
  550. v4l2_fh_init(&ctx->fh, &fimc->m2m.vfd);
  551. ctx->fimc_dev = fimc;
  552. /* Default color format */
  553. ctx->s_frame.fmt = fimc_get_format(0);
  554. ctx->d_frame.fmt = fimc_get_format(0);
  555. ret = fimc_ctrls_create(ctx);
  556. if (ret)
  557. goto error_fh;
  558. /* Use separate control handler per file handle */
  559. ctx->fh.ctrl_handler = &ctx->ctrls.handler;
  560. file->private_data = &ctx->fh;
  561. v4l2_fh_add(&ctx->fh);
  562. /* Setup the device context for memory-to-memory mode */
  563. ctx->state = FIMC_CTX_M2M;
  564. ctx->flags = 0;
  565. ctx->in_path = FIMC_IO_DMA;
  566. ctx->out_path = FIMC_IO_DMA;
  567. ctx->m2m_ctx = v4l2_m2m_ctx_init(fimc->m2m.m2m_dev, ctx, queue_init);
  568. if (IS_ERR(ctx->m2m_ctx)) {
  569. ret = PTR_ERR(ctx->m2m_ctx);
  570. goto error_c;
  571. }
  572. if (fimc->m2m.refcnt++ == 0)
  573. set_bit(ST_M2M_RUN, &fimc->state);
  574. mutex_unlock(&fimc->lock);
  575. return 0;
  576. error_c:
  577. fimc_ctrls_delete(ctx);
  578. error_fh:
  579. v4l2_fh_del(&ctx->fh);
  580. v4l2_fh_exit(&ctx->fh);
  581. kfree(ctx);
  582. unlock:
  583. mutex_unlock(&fimc->lock);
  584. return ret;
  585. }
  586. static int fimc_m2m_release(struct file *file)
  587. {
  588. struct fimc_ctx *ctx = fh_to_ctx(file->private_data);
  589. struct fimc_dev *fimc = ctx->fimc_dev;
  590. dbg("pid: %d, state: 0x%lx, refcnt= %d",
  591. task_pid_nr(current), fimc->state, fimc->m2m.refcnt);
  592. mutex_lock(&fimc->lock);
  593. v4l2_m2m_ctx_release(ctx->m2m_ctx);
  594. fimc_ctrls_delete(ctx);
  595. v4l2_fh_del(&ctx->fh);
  596. v4l2_fh_exit(&ctx->fh);
  597. if (--fimc->m2m.refcnt <= 0)
  598. clear_bit(ST_M2M_RUN, &fimc->state);
  599. kfree(ctx);
  600. mutex_unlock(&fimc->lock);
  601. return 0;
  602. }
  603. static unsigned int fimc_m2m_poll(struct file *file,
  604. struct poll_table_struct *wait)
  605. {
  606. struct fimc_ctx *ctx = fh_to_ctx(file->private_data);
  607. struct fimc_dev *fimc = ctx->fimc_dev;
  608. int ret;
  609. if (mutex_lock_interruptible(&fimc->lock))
  610. return -ERESTARTSYS;
  611. ret = v4l2_m2m_poll(file, ctx->m2m_ctx, wait);
  612. mutex_unlock(&fimc->lock);
  613. return ret;
  614. }
  615. static int fimc_m2m_mmap(struct file *file, struct vm_area_struct *vma)
  616. {
  617. struct fimc_ctx *ctx = fh_to_ctx(file->private_data);
  618. struct fimc_dev *fimc = ctx->fimc_dev;
  619. int ret;
  620. if (mutex_lock_interruptible(&fimc->lock))
  621. return -ERESTARTSYS;
  622. ret = v4l2_m2m_mmap(file, ctx->m2m_ctx, vma);
  623. mutex_unlock(&fimc->lock);
  624. return ret;
  625. }
  626. static const struct v4l2_file_operations fimc_m2m_fops = {
  627. .owner = THIS_MODULE,
  628. .open = fimc_m2m_open,
  629. .release = fimc_m2m_release,
  630. .poll = fimc_m2m_poll,
  631. .unlocked_ioctl = video_ioctl2,
  632. .mmap = fimc_m2m_mmap,
  633. };
  634. static struct v4l2_m2m_ops m2m_ops = {
  635. .device_run = fimc_device_run,
  636. .job_abort = fimc_job_abort,
  637. };
  638. int fimc_register_m2m_device(struct fimc_dev *fimc,
  639. struct v4l2_device *v4l2_dev)
  640. {
  641. struct video_device *vfd = &fimc->m2m.vfd;
  642. int ret;
  643. fimc->v4l2_dev = v4l2_dev;
  644. memset(vfd, 0, sizeof(*vfd));
  645. vfd->fops = &fimc_m2m_fops;
  646. vfd->ioctl_ops = &fimc_m2m_ioctl_ops;
  647. vfd->v4l2_dev = v4l2_dev;
  648. vfd->minor = -1;
  649. vfd->release = video_device_release_empty;
  650. vfd->lock = &fimc->lock;
  651. vfd->vfl_dir = VFL_DIR_M2M;
  652. snprintf(vfd->name, sizeof(vfd->name), "fimc.%d.m2m", fimc->id);
  653. video_set_drvdata(vfd, fimc);
  654. fimc->m2m.m2m_dev = v4l2_m2m_init(&m2m_ops);
  655. if (IS_ERR(fimc->m2m.m2m_dev)) {
  656. v4l2_err(v4l2_dev, "failed to initialize v4l2-m2m device\n");
  657. return PTR_ERR(fimc->m2m.m2m_dev);
  658. }
  659. ret = media_entity_init(&vfd->entity, 0, NULL, 0);
  660. if (ret)
  661. goto err_me;
  662. ret = video_register_device(vfd, VFL_TYPE_GRABBER, -1);
  663. if (ret)
  664. goto err_vd;
  665. v4l2_info(v4l2_dev, "Registered %s as /dev/%s\n",
  666. vfd->name, video_device_node_name(vfd));
  667. return 0;
  668. err_vd:
  669. media_entity_cleanup(&vfd->entity);
  670. err_me:
  671. v4l2_m2m_release(fimc->m2m.m2m_dev);
  672. return ret;
  673. }
  674. void fimc_unregister_m2m_device(struct fimc_dev *fimc)
  675. {
  676. if (!fimc)
  677. return;
  678. if (fimc->m2m.m2m_dev)
  679. v4l2_m2m_release(fimc->m2m.m2m_dev);
  680. if (video_is_registered(&fimc->m2m.vfd)) {
  681. video_unregister_device(&fimc->m2m.vfd);
  682. media_entity_cleanup(&fimc->m2m.vfd.entity);
  683. }
  684. }