gsc-m2m.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790
  1. /*
  2. * Copyright (c) 2011 - 2012 Samsung Electronics Co., Ltd.
  3. * http://www.samsung.com
  4. *
  5. * Samsung EXYNOS5 SoC series G-Scaler driver
  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/workqueue.h>
  19. #include <linux/device.h>
  20. #include <linux/platform_device.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 "gsc-core.h"
  27. static int gsc_m2m_ctx_stop_req(struct gsc_ctx *ctx)
  28. {
  29. struct gsc_ctx *curr_ctx;
  30. struct gsc_dev *gsc = ctx->gsc_dev;
  31. int ret;
  32. curr_ctx = v4l2_m2m_get_curr_priv(gsc->m2m.m2m_dev);
  33. if (!gsc_m2m_pending(gsc) || (curr_ctx != ctx))
  34. return 0;
  35. gsc_ctx_state_lock_set(GSC_CTX_STOP_REQ, ctx);
  36. ret = wait_event_timeout(gsc->irq_queue,
  37. !gsc_ctx_state_is_set(GSC_CTX_STOP_REQ, ctx),
  38. GSC_SHUTDOWN_TIMEOUT);
  39. return ret == 0 ? -ETIMEDOUT : ret;
  40. }
  41. static int gsc_m2m_start_streaming(struct vb2_queue *q, unsigned int count)
  42. {
  43. struct gsc_ctx *ctx = q->drv_priv;
  44. int ret;
  45. ret = pm_runtime_get_sync(&ctx->gsc_dev->pdev->dev);
  46. return ret > 0 ? 0 : ret;
  47. }
  48. static int gsc_m2m_stop_streaming(struct vb2_queue *q)
  49. {
  50. struct gsc_ctx *ctx = q->drv_priv;
  51. int ret;
  52. ret = gsc_m2m_ctx_stop_req(ctx);
  53. if (ret == -ETIMEDOUT)
  54. gsc_m2m_job_finish(ctx, VB2_BUF_STATE_ERROR);
  55. pm_runtime_put(&ctx->gsc_dev->pdev->dev);
  56. return 0;
  57. }
  58. void gsc_m2m_job_finish(struct gsc_ctx *ctx, int vb_state)
  59. {
  60. struct vb2_buffer *src_vb, *dst_vb;
  61. if (!ctx || !ctx->m2m_ctx)
  62. return;
  63. src_vb = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
  64. dst_vb = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
  65. if (src_vb && dst_vb) {
  66. src_vb->v4l2_buf.timestamp = dst_vb->v4l2_buf.timestamp;
  67. src_vb->v4l2_buf.timecode = dst_vb->v4l2_buf.timecode;
  68. v4l2_m2m_buf_done(src_vb, vb_state);
  69. v4l2_m2m_buf_done(dst_vb, vb_state);
  70. v4l2_m2m_job_finish(ctx->gsc_dev->m2m.m2m_dev,
  71. ctx->m2m_ctx);
  72. }
  73. }
  74. static void gsc_m2m_job_abort(void *priv)
  75. {
  76. struct gsc_ctx *ctx = priv;
  77. int ret;
  78. ret = gsc_m2m_ctx_stop_req(ctx);
  79. if (ret == -ETIMEDOUT)
  80. gsc_m2m_job_finish(ctx, VB2_BUF_STATE_ERROR);
  81. }
  82. static int gsc_get_bufs(struct gsc_ctx *ctx)
  83. {
  84. struct gsc_frame *s_frame, *d_frame;
  85. struct vb2_buffer *src_vb, *dst_vb;
  86. int ret;
  87. s_frame = &ctx->s_frame;
  88. d_frame = &ctx->d_frame;
  89. src_vb = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
  90. ret = gsc_prepare_addr(ctx, src_vb, s_frame, &s_frame->addr);
  91. if (ret)
  92. return ret;
  93. dst_vb = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
  94. ret = gsc_prepare_addr(ctx, dst_vb, d_frame, &d_frame->addr);
  95. if (ret)
  96. return ret;
  97. dst_vb->v4l2_buf.timestamp = src_vb->v4l2_buf.timestamp;
  98. return 0;
  99. }
  100. static void gsc_m2m_device_run(void *priv)
  101. {
  102. struct gsc_ctx *ctx = priv;
  103. struct gsc_dev *gsc;
  104. unsigned long flags;
  105. int ret;
  106. bool is_set = false;
  107. if (WARN(!ctx, "null hardware context\n"))
  108. return;
  109. gsc = ctx->gsc_dev;
  110. spin_lock_irqsave(&gsc->slock, flags);
  111. set_bit(ST_M2M_PEND, &gsc->state);
  112. /* Reconfigure hardware if the context has changed. */
  113. if (gsc->m2m.ctx != ctx) {
  114. pr_debug("gsc->m2m.ctx = 0x%p, current_ctx = 0x%p",
  115. gsc->m2m.ctx, ctx);
  116. ctx->state |= GSC_PARAMS;
  117. gsc->m2m.ctx = ctx;
  118. }
  119. is_set = (ctx->state & GSC_CTX_STOP_REQ) ? 1 : 0;
  120. ctx->state &= ~GSC_CTX_STOP_REQ;
  121. if (is_set) {
  122. wake_up(&gsc->irq_queue);
  123. goto put_device;
  124. }
  125. ret = gsc_get_bufs(ctx);
  126. if (ret) {
  127. pr_err("Wrong address");
  128. goto put_device;
  129. }
  130. gsc_set_prefbuf(gsc, &ctx->s_frame);
  131. gsc_hw_set_input_addr(gsc, &ctx->s_frame.addr, GSC_M2M_BUF_NUM);
  132. gsc_hw_set_output_addr(gsc, &ctx->d_frame.addr, GSC_M2M_BUF_NUM);
  133. if (ctx->state & GSC_PARAMS) {
  134. gsc_hw_set_input_buf_masking(gsc, GSC_M2M_BUF_NUM, false);
  135. gsc_hw_set_output_buf_masking(gsc, GSC_M2M_BUF_NUM, false);
  136. gsc_hw_set_frm_done_irq_mask(gsc, false);
  137. gsc_hw_set_gsc_irq_enable(gsc, true);
  138. if (gsc_set_scaler_info(ctx)) {
  139. pr_err("Scaler setup error");
  140. goto put_device;
  141. }
  142. gsc_hw_set_input_path(ctx);
  143. gsc_hw_set_in_size(ctx);
  144. gsc_hw_set_in_image_format(ctx);
  145. gsc_hw_set_output_path(ctx);
  146. gsc_hw_set_out_size(ctx);
  147. gsc_hw_set_out_image_format(ctx);
  148. gsc_hw_set_prescaler(ctx);
  149. gsc_hw_set_mainscaler(ctx);
  150. gsc_hw_set_rotation(ctx);
  151. gsc_hw_set_global_alpha(ctx);
  152. }
  153. /* update shadow registers */
  154. gsc_hw_set_sfr_update(ctx);
  155. ctx->state &= ~GSC_PARAMS;
  156. gsc_hw_enable_control(gsc, true);
  157. spin_unlock_irqrestore(&gsc->slock, flags);
  158. return;
  159. put_device:
  160. ctx->state &= ~GSC_PARAMS;
  161. spin_unlock_irqrestore(&gsc->slock, flags);
  162. }
  163. static int gsc_m2m_queue_setup(struct vb2_queue *vq,
  164. const struct v4l2_format *fmt,
  165. unsigned int *num_buffers, unsigned int *num_planes,
  166. unsigned int sizes[], void *allocators[])
  167. {
  168. struct gsc_ctx *ctx = vb2_get_drv_priv(vq);
  169. struct gsc_frame *frame;
  170. int i;
  171. frame = ctx_get_frame(ctx, vq->type);
  172. if (IS_ERR(frame))
  173. return PTR_ERR(frame);
  174. if (!frame->fmt)
  175. return -EINVAL;
  176. *num_planes = frame->fmt->num_planes;
  177. for (i = 0; i < frame->fmt->num_planes; i++) {
  178. sizes[i] = frame->payload[i];
  179. allocators[i] = ctx->gsc_dev->alloc_ctx;
  180. }
  181. return 0;
  182. }
  183. static int gsc_m2m_buf_prepare(struct vb2_buffer *vb)
  184. {
  185. struct gsc_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  186. struct gsc_frame *frame;
  187. int i;
  188. frame = ctx_get_frame(ctx, vb->vb2_queue->type);
  189. if (IS_ERR(frame))
  190. return PTR_ERR(frame);
  191. if (!V4L2_TYPE_IS_OUTPUT(vb->vb2_queue->type)) {
  192. for (i = 0; i < frame->fmt->num_planes; i++)
  193. vb2_set_plane_payload(vb, i, frame->payload[i]);
  194. }
  195. return 0;
  196. }
  197. static void gsc_m2m_buf_queue(struct vb2_buffer *vb)
  198. {
  199. struct gsc_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  200. pr_debug("ctx: %p, ctx->state: 0x%x", ctx, ctx->state);
  201. if (ctx->m2m_ctx)
  202. v4l2_m2m_buf_queue(ctx->m2m_ctx, vb);
  203. }
  204. static struct vb2_ops gsc_m2m_qops = {
  205. .queue_setup = gsc_m2m_queue_setup,
  206. .buf_prepare = gsc_m2m_buf_prepare,
  207. .buf_queue = gsc_m2m_buf_queue,
  208. .wait_prepare = gsc_unlock,
  209. .wait_finish = gsc_lock,
  210. .stop_streaming = gsc_m2m_stop_streaming,
  211. .start_streaming = gsc_m2m_start_streaming,
  212. };
  213. static int gsc_m2m_querycap(struct file *file, void *fh,
  214. struct v4l2_capability *cap)
  215. {
  216. struct gsc_ctx *ctx = fh_to_ctx(fh);
  217. struct gsc_dev *gsc = ctx->gsc_dev;
  218. strlcpy(cap->driver, gsc->pdev->name, sizeof(cap->driver));
  219. strlcpy(cap->card, gsc->pdev->name, sizeof(cap->card));
  220. strlcpy(cap->bus_info, "platform", sizeof(cap->bus_info));
  221. cap->device_caps = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_M2M_MPLANE |
  222. V4L2_CAP_VIDEO_CAPTURE_MPLANE | V4L2_CAP_VIDEO_OUTPUT_MPLANE;
  223. cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
  224. return 0;
  225. }
  226. static int gsc_m2m_enum_fmt_mplane(struct file *file, void *priv,
  227. struct v4l2_fmtdesc *f)
  228. {
  229. return gsc_enum_fmt_mplane(f);
  230. }
  231. static int gsc_m2m_g_fmt_mplane(struct file *file, void *fh,
  232. struct v4l2_format *f)
  233. {
  234. struct gsc_ctx *ctx = fh_to_ctx(fh);
  235. return gsc_g_fmt_mplane(ctx, f);
  236. }
  237. static int gsc_m2m_try_fmt_mplane(struct file *file, void *fh,
  238. struct v4l2_format *f)
  239. {
  240. struct gsc_ctx *ctx = fh_to_ctx(fh);
  241. return gsc_try_fmt_mplane(ctx, f);
  242. }
  243. static int gsc_m2m_s_fmt_mplane(struct file *file, void *fh,
  244. struct v4l2_format *f)
  245. {
  246. struct gsc_ctx *ctx = fh_to_ctx(fh);
  247. struct vb2_queue *vq;
  248. struct gsc_frame *frame;
  249. struct v4l2_pix_format_mplane *pix;
  250. int i, ret = 0;
  251. ret = gsc_m2m_try_fmt_mplane(file, fh, f);
  252. if (ret)
  253. return ret;
  254. vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
  255. if (vb2_is_streaming(vq)) {
  256. pr_err("queue (%d) busy", f->type);
  257. return -EBUSY;
  258. }
  259. if (V4L2_TYPE_IS_OUTPUT(f->type))
  260. frame = &ctx->s_frame;
  261. else
  262. frame = &ctx->d_frame;
  263. pix = &f->fmt.pix_mp;
  264. frame->fmt = find_fmt(&pix->pixelformat, NULL, 0);
  265. frame->colorspace = pix->colorspace;
  266. if (!frame->fmt)
  267. return -EINVAL;
  268. for (i = 0; i < frame->fmt->num_planes; i++)
  269. frame->payload[i] = pix->plane_fmt[i].sizeimage;
  270. gsc_set_frame_size(frame, pix->width, pix->height);
  271. if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
  272. gsc_ctx_state_lock_set(GSC_PARAMS | GSC_DST_FMT, ctx);
  273. else
  274. gsc_ctx_state_lock_set(GSC_PARAMS | GSC_SRC_FMT, ctx);
  275. pr_debug("f_w: %d, f_h: %d", frame->f_width, frame->f_height);
  276. return 0;
  277. }
  278. static int gsc_m2m_reqbufs(struct file *file, void *fh,
  279. struct v4l2_requestbuffers *reqbufs)
  280. {
  281. struct gsc_ctx *ctx = fh_to_ctx(fh);
  282. struct gsc_dev *gsc = ctx->gsc_dev;
  283. struct gsc_frame *frame;
  284. u32 max_cnt;
  285. max_cnt = (reqbufs->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) ?
  286. gsc->variant->in_buf_cnt : gsc->variant->out_buf_cnt;
  287. if (reqbufs->count > max_cnt) {
  288. return -EINVAL;
  289. } else if (reqbufs->count == 0) {
  290. if (reqbufs->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
  291. gsc_ctx_state_lock_clear(GSC_SRC_FMT, ctx);
  292. else
  293. gsc_ctx_state_lock_clear(GSC_DST_FMT, ctx);
  294. }
  295. frame = ctx_get_frame(ctx, reqbufs->type);
  296. return v4l2_m2m_reqbufs(file, ctx->m2m_ctx, reqbufs);
  297. }
  298. static int gsc_m2m_expbuf(struct file *file, void *fh,
  299. struct v4l2_exportbuffer *eb)
  300. {
  301. struct gsc_ctx *ctx = fh_to_ctx(fh);
  302. return v4l2_m2m_expbuf(file, ctx->m2m_ctx, eb);
  303. }
  304. static int gsc_m2m_querybuf(struct file *file, void *fh,
  305. struct v4l2_buffer *buf)
  306. {
  307. struct gsc_ctx *ctx = fh_to_ctx(fh);
  308. return v4l2_m2m_querybuf(file, ctx->m2m_ctx, buf);
  309. }
  310. static int gsc_m2m_qbuf(struct file *file, void *fh,
  311. struct v4l2_buffer *buf)
  312. {
  313. struct gsc_ctx *ctx = fh_to_ctx(fh);
  314. return v4l2_m2m_qbuf(file, ctx->m2m_ctx, buf);
  315. }
  316. static int gsc_m2m_dqbuf(struct file *file, void *fh,
  317. struct v4l2_buffer *buf)
  318. {
  319. struct gsc_ctx *ctx = fh_to_ctx(fh);
  320. return v4l2_m2m_dqbuf(file, ctx->m2m_ctx, buf);
  321. }
  322. static int gsc_m2m_streamon(struct file *file, void *fh,
  323. enum v4l2_buf_type type)
  324. {
  325. struct gsc_ctx *ctx = fh_to_ctx(fh);
  326. /* The source and target color format need to be set */
  327. if (V4L2_TYPE_IS_OUTPUT(type)) {
  328. if (!gsc_ctx_state_is_set(GSC_SRC_FMT, ctx))
  329. return -EINVAL;
  330. } else if (!gsc_ctx_state_is_set(GSC_DST_FMT, ctx)) {
  331. return -EINVAL;
  332. }
  333. return v4l2_m2m_streamon(file, ctx->m2m_ctx, type);
  334. }
  335. static int gsc_m2m_streamoff(struct file *file, void *fh,
  336. enum v4l2_buf_type type)
  337. {
  338. struct gsc_ctx *ctx = fh_to_ctx(fh);
  339. return v4l2_m2m_streamoff(file, ctx->m2m_ctx, type);
  340. }
  341. /* Return 1 if rectangle a is enclosed in rectangle b, or 0 otherwise. */
  342. static int is_rectangle_enclosed(struct v4l2_rect *a, struct v4l2_rect *b)
  343. {
  344. if (a->left < b->left || a->top < b->top)
  345. return 0;
  346. if (a->left + a->width > b->left + b->width)
  347. return 0;
  348. if (a->top + a->height > b->top + b->height)
  349. return 0;
  350. return 1;
  351. }
  352. static int gsc_m2m_g_selection(struct file *file, void *fh,
  353. struct v4l2_selection *s)
  354. {
  355. struct gsc_frame *frame;
  356. struct gsc_ctx *ctx = fh_to_ctx(fh);
  357. if ((s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) &&
  358. (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE))
  359. return -EINVAL;
  360. frame = ctx_get_frame(ctx, s->type);
  361. if (IS_ERR(frame))
  362. return PTR_ERR(frame);
  363. switch (s->target) {
  364. case V4L2_SEL_TGT_COMPOSE_DEFAULT:
  365. case V4L2_SEL_TGT_COMPOSE_BOUNDS:
  366. case V4L2_SEL_TGT_CROP_BOUNDS:
  367. case V4L2_SEL_TGT_CROP_DEFAULT:
  368. s->r.left = 0;
  369. s->r.top = 0;
  370. s->r.width = frame->f_width;
  371. s->r.height = frame->f_height;
  372. return 0;
  373. case V4L2_SEL_TGT_COMPOSE:
  374. case V4L2_SEL_TGT_CROP:
  375. s->r.left = frame->crop.left;
  376. s->r.top = frame->crop.top;
  377. s->r.width = frame->crop.width;
  378. s->r.height = frame->crop.height;
  379. return 0;
  380. }
  381. return -EINVAL;
  382. }
  383. static int gsc_m2m_s_selection(struct file *file, void *fh,
  384. struct v4l2_selection *s)
  385. {
  386. struct gsc_frame *frame;
  387. struct gsc_ctx *ctx = fh_to_ctx(fh);
  388. struct v4l2_crop cr;
  389. struct gsc_variant *variant = ctx->gsc_dev->variant;
  390. int ret;
  391. cr.type = s->type;
  392. cr.c = s->r;
  393. if ((s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) &&
  394. (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE))
  395. return -EINVAL;
  396. ret = gsc_try_crop(ctx, &cr);
  397. if (ret)
  398. return ret;
  399. if (s->flags & V4L2_SEL_FLAG_LE &&
  400. !is_rectangle_enclosed(&cr.c, &s->r))
  401. return -ERANGE;
  402. if (s->flags & V4L2_SEL_FLAG_GE &&
  403. !is_rectangle_enclosed(&s->r, &cr.c))
  404. return -ERANGE;
  405. s->r = cr.c;
  406. switch (s->target) {
  407. case V4L2_SEL_TGT_COMPOSE_BOUNDS:
  408. case V4L2_SEL_TGT_COMPOSE_DEFAULT:
  409. case V4L2_SEL_TGT_COMPOSE:
  410. frame = &ctx->s_frame;
  411. break;
  412. case V4L2_SEL_TGT_CROP_BOUNDS:
  413. case V4L2_SEL_TGT_CROP:
  414. case V4L2_SEL_TGT_CROP_DEFAULT:
  415. frame = &ctx->d_frame;
  416. break;
  417. default:
  418. return -EINVAL;
  419. }
  420. /* Check to see if scaling ratio is within supported range */
  421. if (gsc_ctx_state_is_set(GSC_DST_FMT | GSC_SRC_FMT, ctx)) {
  422. if (s->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  423. ret = gsc_check_scaler_ratio(variant, cr.c.width,
  424. cr.c.height, ctx->d_frame.crop.width,
  425. ctx->d_frame.crop.height,
  426. ctx->gsc_ctrls.rotate->val, ctx->out_path);
  427. } else {
  428. ret = gsc_check_scaler_ratio(variant,
  429. ctx->s_frame.crop.width,
  430. ctx->s_frame.crop.height, cr.c.width,
  431. cr.c.height, ctx->gsc_ctrls.rotate->val,
  432. ctx->out_path);
  433. }
  434. if (ret) {
  435. pr_err("Out of scaler range");
  436. return -EINVAL;
  437. }
  438. }
  439. frame->crop = cr.c;
  440. gsc_ctx_state_lock_set(GSC_PARAMS, ctx);
  441. return 0;
  442. }
  443. static const struct v4l2_ioctl_ops gsc_m2m_ioctl_ops = {
  444. .vidioc_querycap = gsc_m2m_querycap,
  445. .vidioc_enum_fmt_vid_cap_mplane = gsc_m2m_enum_fmt_mplane,
  446. .vidioc_enum_fmt_vid_out_mplane = gsc_m2m_enum_fmt_mplane,
  447. .vidioc_g_fmt_vid_cap_mplane = gsc_m2m_g_fmt_mplane,
  448. .vidioc_g_fmt_vid_out_mplane = gsc_m2m_g_fmt_mplane,
  449. .vidioc_try_fmt_vid_cap_mplane = gsc_m2m_try_fmt_mplane,
  450. .vidioc_try_fmt_vid_out_mplane = gsc_m2m_try_fmt_mplane,
  451. .vidioc_s_fmt_vid_cap_mplane = gsc_m2m_s_fmt_mplane,
  452. .vidioc_s_fmt_vid_out_mplane = gsc_m2m_s_fmt_mplane,
  453. .vidioc_reqbufs = gsc_m2m_reqbufs,
  454. .vidioc_expbuf = gsc_m2m_expbuf,
  455. .vidioc_querybuf = gsc_m2m_querybuf,
  456. .vidioc_qbuf = gsc_m2m_qbuf,
  457. .vidioc_dqbuf = gsc_m2m_dqbuf,
  458. .vidioc_streamon = gsc_m2m_streamon,
  459. .vidioc_streamoff = gsc_m2m_streamoff,
  460. .vidioc_g_selection = gsc_m2m_g_selection,
  461. .vidioc_s_selection = gsc_m2m_s_selection
  462. };
  463. static int queue_init(void *priv, struct vb2_queue *src_vq,
  464. struct vb2_queue *dst_vq)
  465. {
  466. struct gsc_ctx *ctx = priv;
  467. int ret;
  468. memset(src_vq, 0, sizeof(*src_vq));
  469. src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
  470. src_vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
  471. src_vq->drv_priv = ctx;
  472. src_vq->ops = &gsc_m2m_qops;
  473. src_vq->mem_ops = &vb2_dma_contig_memops;
  474. src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
  475. src_vq->timestamp_type = V4L2_BUF_FLAG_TIMESTAMP_COPY;
  476. ret = vb2_queue_init(src_vq);
  477. if (ret)
  478. return ret;
  479. memset(dst_vq, 0, sizeof(*dst_vq));
  480. dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
  481. dst_vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
  482. dst_vq->drv_priv = ctx;
  483. dst_vq->ops = &gsc_m2m_qops;
  484. dst_vq->mem_ops = &vb2_dma_contig_memops;
  485. dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
  486. dst_vq->timestamp_type = V4L2_BUF_FLAG_TIMESTAMP_COPY;
  487. return vb2_queue_init(dst_vq);
  488. }
  489. static int gsc_m2m_open(struct file *file)
  490. {
  491. struct gsc_dev *gsc = video_drvdata(file);
  492. struct gsc_ctx *ctx = NULL;
  493. int ret;
  494. pr_debug("pid: %d, state: 0x%lx", task_pid_nr(current), gsc->state);
  495. if (mutex_lock_interruptible(&gsc->lock))
  496. return -ERESTARTSYS;
  497. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  498. if (!ctx) {
  499. ret = -ENOMEM;
  500. goto unlock;
  501. }
  502. v4l2_fh_init(&ctx->fh, gsc->m2m.vfd);
  503. ret = gsc_ctrls_create(ctx);
  504. if (ret)
  505. goto error_fh;
  506. /* Use separate control handler per file handle */
  507. ctx->fh.ctrl_handler = &ctx->ctrl_handler;
  508. file->private_data = &ctx->fh;
  509. v4l2_fh_add(&ctx->fh);
  510. ctx->gsc_dev = gsc;
  511. /* Default color format */
  512. ctx->s_frame.fmt = get_format(0);
  513. ctx->d_frame.fmt = get_format(0);
  514. /* Setup the device context for mem2mem mode. */
  515. ctx->state = GSC_CTX_M2M;
  516. ctx->flags = 0;
  517. ctx->in_path = GSC_DMA;
  518. ctx->out_path = GSC_DMA;
  519. ctx->m2m_ctx = v4l2_m2m_ctx_init(gsc->m2m.m2m_dev, ctx, queue_init);
  520. if (IS_ERR(ctx->m2m_ctx)) {
  521. pr_err("Failed to initialize m2m context");
  522. ret = PTR_ERR(ctx->m2m_ctx);
  523. goto error_ctrls;
  524. }
  525. if (gsc->m2m.refcnt++ == 0)
  526. set_bit(ST_M2M_OPEN, &gsc->state);
  527. pr_debug("gsc m2m driver is opened, ctx(0x%p)", ctx);
  528. mutex_unlock(&gsc->lock);
  529. return 0;
  530. error_ctrls:
  531. gsc_ctrls_delete(ctx);
  532. error_fh:
  533. v4l2_fh_del(&ctx->fh);
  534. v4l2_fh_exit(&ctx->fh);
  535. kfree(ctx);
  536. unlock:
  537. mutex_unlock(&gsc->lock);
  538. return ret;
  539. }
  540. static int gsc_m2m_release(struct file *file)
  541. {
  542. struct gsc_ctx *ctx = fh_to_ctx(file->private_data);
  543. struct gsc_dev *gsc = ctx->gsc_dev;
  544. pr_debug("pid: %d, state: 0x%lx, refcnt= %d",
  545. task_pid_nr(current), gsc->state, gsc->m2m.refcnt);
  546. mutex_lock(&gsc->lock);
  547. v4l2_m2m_ctx_release(ctx->m2m_ctx);
  548. gsc_ctrls_delete(ctx);
  549. v4l2_fh_del(&ctx->fh);
  550. v4l2_fh_exit(&ctx->fh);
  551. if (--gsc->m2m.refcnt <= 0)
  552. clear_bit(ST_M2M_OPEN, &gsc->state);
  553. kfree(ctx);
  554. mutex_unlock(&gsc->lock);
  555. return 0;
  556. }
  557. static unsigned int gsc_m2m_poll(struct file *file,
  558. struct poll_table_struct *wait)
  559. {
  560. struct gsc_ctx *ctx = fh_to_ctx(file->private_data);
  561. struct gsc_dev *gsc = ctx->gsc_dev;
  562. int ret;
  563. if (mutex_lock_interruptible(&gsc->lock))
  564. return -ERESTARTSYS;
  565. ret = v4l2_m2m_poll(file, ctx->m2m_ctx, wait);
  566. mutex_unlock(&gsc->lock);
  567. return ret;
  568. }
  569. static int gsc_m2m_mmap(struct file *file, struct vm_area_struct *vma)
  570. {
  571. struct gsc_ctx *ctx = fh_to_ctx(file->private_data);
  572. struct gsc_dev *gsc = ctx->gsc_dev;
  573. int ret;
  574. if (mutex_lock_interruptible(&gsc->lock))
  575. return -ERESTARTSYS;
  576. ret = v4l2_m2m_mmap(file, ctx->m2m_ctx, vma);
  577. mutex_unlock(&gsc->lock);
  578. return ret;
  579. }
  580. static const struct v4l2_file_operations gsc_m2m_fops = {
  581. .owner = THIS_MODULE,
  582. .open = gsc_m2m_open,
  583. .release = gsc_m2m_release,
  584. .poll = gsc_m2m_poll,
  585. .unlocked_ioctl = video_ioctl2,
  586. .mmap = gsc_m2m_mmap,
  587. };
  588. static struct v4l2_m2m_ops gsc_m2m_ops = {
  589. .device_run = gsc_m2m_device_run,
  590. .job_abort = gsc_m2m_job_abort,
  591. };
  592. int gsc_register_m2m_device(struct gsc_dev *gsc)
  593. {
  594. struct platform_device *pdev;
  595. int ret;
  596. if (!gsc)
  597. return -ENODEV;
  598. pdev = gsc->pdev;
  599. gsc->vdev.fops = &gsc_m2m_fops;
  600. gsc->vdev.ioctl_ops = &gsc_m2m_ioctl_ops;
  601. gsc->vdev.release = video_device_release_empty;
  602. gsc->vdev.lock = &gsc->lock;
  603. gsc->vdev.vfl_dir = VFL_DIR_M2M;
  604. gsc->vdev.v4l2_dev = &gsc->v4l2_dev;
  605. snprintf(gsc->vdev.name, sizeof(gsc->vdev.name), "%s.%d:m2m",
  606. GSC_MODULE_NAME, gsc->id);
  607. video_set_drvdata(&gsc->vdev, gsc);
  608. gsc->m2m.vfd = &gsc->vdev;
  609. gsc->m2m.m2m_dev = v4l2_m2m_init(&gsc_m2m_ops);
  610. if (IS_ERR(gsc->m2m.m2m_dev)) {
  611. dev_err(&pdev->dev, "failed to initialize v4l2-m2m device\n");
  612. ret = PTR_ERR(gsc->m2m.m2m_dev);
  613. goto err_m2m_r1;
  614. }
  615. ret = video_register_device(&gsc->vdev, VFL_TYPE_GRABBER, -1);
  616. if (ret) {
  617. dev_err(&pdev->dev,
  618. "%s(): failed to register video device\n", __func__);
  619. goto err_m2m_r2;
  620. }
  621. pr_debug("gsc m2m driver registered as /dev/video%d", gsc->vdev.num);
  622. return 0;
  623. err_m2m_r2:
  624. v4l2_m2m_release(gsc->m2m.m2m_dev);
  625. err_m2m_r1:
  626. video_device_release(gsc->m2m.vfd);
  627. return ret;
  628. }
  629. void gsc_unregister_m2m_device(struct gsc_dev *gsc)
  630. {
  631. if (gsc)
  632. v4l2_m2m_release(gsc->m2m.m2m_dev);
  633. }