fimc-m2m.c 20 KB

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