mtk_mdp_m2m.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269
  1. /*
  2. * Copyright (c) 2015-2016 MediaTek Inc.
  3. * Author: Houlong Wei <houlong.wei@mediatek.com>
  4. * Ming Hsiu Tsai <minghsiu.tsai@mediatek.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <linux/device.h>
  16. #include <linux/errno.h>
  17. #include <linux/kernel.h>
  18. #include <linux/pm_runtime.h>
  19. #include <linux/slab.h>
  20. #include <linux/workqueue.h>
  21. #include <media/v4l2-event.h>
  22. #include <media/v4l2-ioctl.h>
  23. #include "mtk_mdp_core.h"
  24. #include "mtk_mdp_m2m.h"
  25. #include "mtk_mdp_regs.h"
  26. #include "mtk_vpu.h"
  27. /**
  28. * struct mtk_mdp_pix_limit - image pixel size limits
  29. * @org_w: source pixel width
  30. * @org_h: source pixel height
  31. * @target_rot_dis_w: pixel dst scaled width with the rotator is off
  32. * @target_rot_dis_h: pixel dst scaled height with the rotator is off
  33. * @target_rot_en_w: pixel dst scaled width with the rotator is on
  34. * @target_rot_en_h: pixel dst scaled height with the rotator is on
  35. */
  36. struct mtk_mdp_pix_limit {
  37. u16 org_w;
  38. u16 org_h;
  39. u16 target_rot_dis_w;
  40. u16 target_rot_dis_h;
  41. u16 target_rot_en_w;
  42. u16 target_rot_en_h;
  43. };
  44. static struct mtk_mdp_pix_align mtk_mdp_size_align = {
  45. .org_w = 16,
  46. .org_h = 16,
  47. .target_w = 2,
  48. .target_h = 2,
  49. };
  50. static const struct mtk_mdp_fmt mtk_mdp_formats[] = {
  51. {
  52. .pixelformat = V4L2_PIX_FMT_MT21C,
  53. .depth = { 8, 4 },
  54. .row_depth = { 8, 8 },
  55. .num_planes = 2,
  56. .num_comp = 2,
  57. .align = &mtk_mdp_size_align,
  58. .flags = MTK_MDP_FMT_FLAG_OUTPUT,
  59. }, {
  60. .pixelformat = V4L2_PIX_FMT_NV12M,
  61. .depth = { 8, 4 },
  62. .row_depth = { 8, 8 },
  63. .num_planes = 2,
  64. .num_comp = 2,
  65. .flags = MTK_MDP_FMT_FLAG_OUTPUT |
  66. MTK_MDP_FMT_FLAG_CAPTURE,
  67. }, {
  68. .pixelformat = V4L2_PIX_FMT_YUV420M,
  69. .depth = { 8, 2, 2 },
  70. .row_depth = { 8, 4, 4 },
  71. .num_planes = 3,
  72. .num_comp = 3,
  73. .flags = MTK_MDP_FMT_FLAG_OUTPUT |
  74. MTK_MDP_FMT_FLAG_CAPTURE,
  75. }, {
  76. .pixelformat = V4L2_PIX_FMT_YVU420,
  77. .depth = { 12 },
  78. .row_depth = { 8 },
  79. .num_planes = 1,
  80. .num_comp = 3,
  81. .flags = MTK_MDP_FMT_FLAG_OUTPUT |
  82. MTK_MDP_FMT_FLAG_CAPTURE,
  83. }
  84. };
  85. static struct mtk_mdp_pix_limit mtk_mdp_size_max = {
  86. .target_rot_dis_w = 4096,
  87. .target_rot_dis_h = 4096,
  88. .target_rot_en_w = 4096,
  89. .target_rot_en_h = 4096,
  90. };
  91. static struct mtk_mdp_pix_limit mtk_mdp_size_min = {
  92. .org_w = 16,
  93. .org_h = 16,
  94. .target_rot_dis_w = 16,
  95. .target_rot_dis_h = 16,
  96. .target_rot_en_w = 16,
  97. .target_rot_en_h = 16,
  98. };
  99. /* align size for normal raster scan pixel format */
  100. static struct mtk_mdp_pix_align mtk_mdp_rs_align = {
  101. .org_w = 2,
  102. .org_h = 2,
  103. .target_w = 2,
  104. .target_h = 2,
  105. };
  106. static struct mtk_mdp_variant mtk_mdp_default_variant = {
  107. .pix_max = &mtk_mdp_size_max,
  108. .pix_min = &mtk_mdp_size_min,
  109. .pix_align = &mtk_mdp_rs_align,
  110. .h_scale_up_max = 32,
  111. .v_scale_up_max = 32,
  112. .h_scale_down_max = 32,
  113. .v_scale_down_max = 128,
  114. };
  115. static const struct mtk_mdp_fmt *mtk_mdp_find_fmt(u32 pixelformat, u32 type)
  116. {
  117. u32 i, flag;
  118. flag = V4L2_TYPE_IS_OUTPUT(type) ? MTK_MDP_FMT_FLAG_OUTPUT :
  119. MTK_MDP_FMT_FLAG_CAPTURE;
  120. for (i = 0; i < ARRAY_SIZE(mtk_mdp_formats); ++i) {
  121. if (!(mtk_mdp_formats[i].flags & flag))
  122. continue;
  123. if (mtk_mdp_formats[i].pixelformat == pixelformat)
  124. return &mtk_mdp_formats[i];
  125. }
  126. return NULL;
  127. }
  128. static const struct mtk_mdp_fmt *mtk_mdp_find_fmt_by_index(u32 index, u32 type)
  129. {
  130. u32 i, flag, num = 0;
  131. flag = V4L2_TYPE_IS_OUTPUT(type) ? MTK_MDP_FMT_FLAG_OUTPUT :
  132. MTK_MDP_FMT_FLAG_CAPTURE;
  133. for (i = 0; i < ARRAY_SIZE(mtk_mdp_formats); ++i) {
  134. if (!(mtk_mdp_formats[i].flags & flag))
  135. continue;
  136. if (index == num)
  137. return &mtk_mdp_formats[i];
  138. num++;
  139. }
  140. return NULL;
  141. }
  142. static void mtk_mdp_bound_align_image(u32 *w, unsigned int wmin,
  143. unsigned int wmax, unsigned int align_w,
  144. u32 *h, unsigned int hmin,
  145. unsigned int hmax, unsigned int align_h)
  146. {
  147. int org_w, org_h, step_w, step_h;
  148. int walign, halign;
  149. org_w = *w;
  150. org_h = *h;
  151. walign = ffs(align_w) - 1;
  152. halign = ffs(align_h) - 1;
  153. v4l_bound_align_image(w, wmin, wmax, walign, h, hmin, hmax, halign, 0);
  154. step_w = 1 << walign;
  155. step_h = 1 << halign;
  156. if (*w < org_w && (*w + step_w) <= wmax)
  157. *w += step_w;
  158. if (*h < org_h && (*h + step_h) <= hmax)
  159. *h += step_h;
  160. }
  161. static const struct mtk_mdp_fmt *mtk_mdp_try_fmt_mplane(struct mtk_mdp_ctx *ctx,
  162. struct v4l2_format *f)
  163. {
  164. struct mtk_mdp_dev *mdp = ctx->mdp_dev;
  165. struct mtk_mdp_variant *variant = mdp->variant;
  166. struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
  167. const struct mtk_mdp_fmt *fmt;
  168. u32 max_w, max_h, align_w, align_h;
  169. u32 min_w, min_h, org_w, org_h;
  170. int i;
  171. fmt = mtk_mdp_find_fmt(pix_mp->pixelformat, f->type);
  172. if (!fmt)
  173. fmt = mtk_mdp_find_fmt_by_index(0, f->type);
  174. if (!fmt) {
  175. dev_dbg(&ctx->mdp_dev->pdev->dev,
  176. "pixelformat format 0x%X invalid\n",
  177. pix_mp->pixelformat);
  178. return NULL;
  179. }
  180. pix_mp->field = V4L2_FIELD_NONE;
  181. pix_mp->pixelformat = fmt->pixelformat;
  182. if (!V4L2_TYPE_IS_OUTPUT(f->type)) {
  183. pix_mp->colorspace = ctx->colorspace;
  184. pix_mp->xfer_func = ctx->xfer_func;
  185. pix_mp->ycbcr_enc = ctx->ycbcr_enc;
  186. pix_mp->quantization = ctx->quant;
  187. }
  188. memset(pix_mp->reserved, 0, sizeof(pix_mp->reserved));
  189. max_w = variant->pix_max->target_rot_dis_w;
  190. max_h = variant->pix_max->target_rot_dis_h;
  191. if (fmt->align == NULL) {
  192. /* use default alignment */
  193. align_w = variant->pix_align->org_w;
  194. align_h = variant->pix_align->org_h;
  195. } else {
  196. align_w = fmt->align->org_w;
  197. align_h = fmt->align->org_h;
  198. }
  199. if (V4L2_TYPE_IS_OUTPUT(f->type)) {
  200. min_w = variant->pix_min->org_w;
  201. min_h = variant->pix_min->org_h;
  202. } else {
  203. min_w = variant->pix_min->target_rot_dis_w;
  204. min_h = variant->pix_min->target_rot_dis_h;
  205. }
  206. mtk_mdp_dbg(2, "[%d] type:%d, wxh:%ux%u, align:%ux%u, max:%ux%u",
  207. ctx->id, f->type, pix_mp->width, pix_mp->height,
  208. align_w, align_h, max_w, max_h);
  209. /*
  210. * To check if image size is modified to adjust parameter against
  211. * hardware abilities
  212. */
  213. org_w = pix_mp->width;
  214. org_h = pix_mp->height;
  215. mtk_mdp_bound_align_image(&pix_mp->width, min_w, max_w, align_w,
  216. &pix_mp->height, min_h, max_h, align_h);
  217. if (org_w != pix_mp->width || org_h != pix_mp->height)
  218. mtk_mdp_dbg(1, "[%d] size change:%ux%u to %ux%u", ctx->id,
  219. org_w, org_h, pix_mp->width, pix_mp->height);
  220. pix_mp->num_planes = fmt->num_planes;
  221. for (i = 0; i < pix_mp->num_planes; ++i) {
  222. int bpl = (pix_mp->width * fmt->row_depth[i]) / 8;
  223. int sizeimage = (pix_mp->width * pix_mp->height *
  224. fmt->depth[i]) / 8;
  225. pix_mp->plane_fmt[i].bytesperline = bpl;
  226. if (pix_mp->plane_fmt[i].sizeimage < sizeimage)
  227. pix_mp->plane_fmt[i].sizeimage = sizeimage;
  228. memset(pix_mp->plane_fmt[i].reserved, 0,
  229. sizeof(pix_mp->plane_fmt[i].reserved));
  230. mtk_mdp_dbg(2, "[%d] p%d, bpl:%d, sizeimage:%u (%u)", ctx->id,
  231. i, bpl, pix_mp->plane_fmt[i].sizeimage, sizeimage);
  232. }
  233. return fmt;
  234. }
  235. static struct mtk_mdp_frame *mtk_mdp_ctx_get_frame(struct mtk_mdp_ctx *ctx,
  236. enum v4l2_buf_type type)
  237. {
  238. if (V4L2_TYPE_IS_OUTPUT(type))
  239. return &ctx->s_frame;
  240. return &ctx->d_frame;
  241. }
  242. static void mtk_mdp_check_crop_change(u32 new_w, u32 new_h, u32 *w, u32 *h)
  243. {
  244. if (new_w != *w || new_h != *h) {
  245. mtk_mdp_dbg(1, "size change:%dx%d to %dx%d",
  246. *w, *h, new_w, new_h);
  247. *w = new_w;
  248. *h = new_h;
  249. }
  250. }
  251. static int mtk_mdp_try_crop(struct mtk_mdp_ctx *ctx, u32 type,
  252. struct v4l2_rect *r)
  253. {
  254. struct mtk_mdp_frame *frame;
  255. struct mtk_mdp_dev *mdp = ctx->mdp_dev;
  256. struct mtk_mdp_variant *variant = mdp->variant;
  257. u32 align_w, align_h, new_w, new_h;
  258. u32 min_w, min_h, max_w, max_h;
  259. if (r->top < 0 || r->left < 0) {
  260. dev_err(&ctx->mdp_dev->pdev->dev,
  261. "doesn't support negative values for top & left\n");
  262. return -EINVAL;
  263. }
  264. mtk_mdp_dbg(2, "[%d] type:%d, set wxh:%dx%d", ctx->id, type,
  265. r->width, r->height);
  266. frame = mtk_mdp_ctx_get_frame(ctx, type);
  267. max_w = frame->width;
  268. max_h = frame->height;
  269. new_w = r->width;
  270. new_h = r->height;
  271. if (V4L2_TYPE_IS_OUTPUT(type)) {
  272. align_w = 1;
  273. align_h = 1;
  274. min_w = 64;
  275. min_h = 32;
  276. } else {
  277. align_w = variant->pix_align->target_w;
  278. align_h = variant->pix_align->target_h;
  279. if (ctx->ctrls.rotate->val == 90 ||
  280. ctx->ctrls.rotate->val == 270) {
  281. max_w = frame->height;
  282. max_h = frame->width;
  283. min_w = variant->pix_min->target_rot_en_w;
  284. min_h = variant->pix_min->target_rot_en_h;
  285. new_w = r->height;
  286. new_h = r->width;
  287. } else {
  288. min_w = variant->pix_min->target_rot_dis_w;
  289. min_h = variant->pix_min->target_rot_dis_h;
  290. }
  291. }
  292. mtk_mdp_dbg(2, "[%d] align:%dx%d, min:%dx%d, new:%dx%d", ctx->id,
  293. align_w, align_h, min_w, min_h, new_w, new_h);
  294. mtk_mdp_bound_align_image(&new_w, min_w, max_w, align_w,
  295. &new_h, min_h, max_h, align_h);
  296. if (!V4L2_TYPE_IS_OUTPUT(type) &&
  297. (ctx->ctrls.rotate->val == 90 ||
  298. ctx->ctrls.rotate->val == 270))
  299. mtk_mdp_check_crop_change(new_h, new_w,
  300. &r->width, &r->height);
  301. else
  302. mtk_mdp_check_crop_change(new_w, new_h,
  303. &r->width, &r->height);
  304. /* adjust left/top if cropping rectangle is out of bounds */
  305. /* Need to add code to algin left value with 2's multiple */
  306. if (r->left + new_w > max_w)
  307. r->left = max_w - new_w;
  308. if (r->top + new_h > max_h)
  309. r->top = max_h - new_h;
  310. if (r->left & 1)
  311. r->left -= 1;
  312. mtk_mdp_dbg(2, "[%d] crop l,t,w,h:%d,%d,%d,%d, max:%dx%d", ctx->id,
  313. r->left, r->top, r->width,
  314. r->height, max_w, max_h);
  315. return 0;
  316. }
  317. static inline struct mtk_mdp_ctx *fh_to_ctx(struct v4l2_fh *fh)
  318. {
  319. return container_of(fh, struct mtk_mdp_ctx, fh);
  320. }
  321. static inline struct mtk_mdp_ctx *ctrl_to_ctx(struct v4l2_ctrl *ctrl)
  322. {
  323. return container_of(ctrl->handler, struct mtk_mdp_ctx, ctrl_handler);
  324. }
  325. void mtk_mdp_ctx_state_lock_set(struct mtk_mdp_ctx *ctx, u32 state)
  326. {
  327. mutex_lock(&ctx->slock);
  328. ctx->state |= state;
  329. mutex_unlock(&ctx->slock);
  330. }
  331. static void mtk_mdp_ctx_state_lock_clear(struct mtk_mdp_ctx *ctx, u32 state)
  332. {
  333. mutex_lock(&ctx->slock);
  334. ctx->state &= ~state;
  335. mutex_unlock(&ctx->slock);
  336. }
  337. static bool mtk_mdp_ctx_state_is_set(struct mtk_mdp_ctx *ctx, u32 mask)
  338. {
  339. bool ret;
  340. mutex_lock(&ctx->slock);
  341. ret = (ctx->state & mask) == mask;
  342. mutex_unlock(&ctx->slock);
  343. return ret;
  344. }
  345. static void mtk_mdp_set_frame_size(struct mtk_mdp_frame *frame, int width,
  346. int height)
  347. {
  348. frame->width = width;
  349. frame->height = height;
  350. frame->crop.width = width;
  351. frame->crop.height = height;
  352. frame->crop.left = 0;
  353. frame->crop.top = 0;
  354. }
  355. static int mtk_mdp_m2m_start_streaming(struct vb2_queue *q, unsigned int count)
  356. {
  357. struct mtk_mdp_ctx *ctx = q->drv_priv;
  358. int ret;
  359. ret = pm_runtime_get_sync(&ctx->mdp_dev->pdev->dev);
  360. if (ret < 0)
  361. mtk_mdp_dbg(1, "[%d] pm_runtime_get_sync failed:%d",
  362. ctx->id, ret);
  363. return 0;
  364. }
  365. static void *mtk_mdp_m2m_buf_remove(struct mtk_mdp_ctx *ctx,
  366. enum v4l2_buf_type type)
  367. {
  368. if (V4L2_TYPE_IS_OUTPUT(type))
  369. return v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
  370. else
  371. return v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
  372. }
  373. static void mtk_mdp_m2m_stop_streaming(struct vb2_queue *q)
  374. {
  375. struct mtk_mdp_ctx *ctx = q->drv_priv;
  376. struct vb2_buffer *vb;
  377. vb = mtk_mdp_m2m_buf_remove(ctx, q->type);
  378. while (vb != NULL) {
  379. v4l2_m2m_buf_done(to_vb2_v4l2_buffer(vb), VB2_BUF_STATE_ERROR);
  380. vb = mtk_mdp_m2m_buf_remove(ctx, q->type);
  381. }
  382. pm_runtime_put(&ctx->mdp_dev->pdev->dev);
  383. }
  384. /* The color format (num_planes) must be already configured. */
  385. static void mtk_mdp_prepare_addr(struct mtk_mdp_ctx *ctx,
  386. struct vb2_buffer *vb,
  387. struct mtk_mdp_frame *frame,
  388. struct mtk_mdp_addr *addr)
  389. {
  390. u32 pix_size, planes, i;
  391. pix_size = frame->width * frame->height;
  392. planes = min_t(u32, frame->fmt->num_planes, ARRAY_SIZE(addr->addr));
  393. for (i = 0; i < planes; i++)
  394. addr->addr[i] = vb2_dma_contig_plane_dma_addr(vb, i);
  395. if (planes == 1) {
  396. if (frame->fmt->pixelformat == V4L2_PIX_FMT_YVU420) {
  397. addr->addr[1] = (dma_addr_t)(addr->addr[0] + pix_size);
  398. addr->addr[2] = (dma_addr_t)(addr->addr[1] +
  399. (pix_size >> 2));
  400. } else {
  401. dev_err(&ctx->mdp_dev->pdev->dev,
  402. "Invalid pixelformat:0x%x\n",
  403. frame->fmt->pixelformat);
  404. }
  405. }
  406. mtk_mdp_dbg(3, "[%d] planes:%d, size:%d, addr:%p,%p,%p",
  407. ctx->id, planes, pix_size, (void *)addr->addr[0],
  408. (void *)addr->addr[1], (void *)addr->addr[2]);
  409. }
  410. static void mtk_mdp_m2m_get_bufs(struct mtk_mdp_ctx *ctx)
  411. {
  412. struct mtk_mdp_frame *s_frame, *d_frame;
  413. struct vb2_buffer *src_vb, *dst_vb;
  414. struct vb2_v4l2_buffer *src_vbuf, *dst_vbuf;
  415. s_frame = &ctx->s_frame;
  416. d_frame = &ctx->d_frame;
  417. src_vb = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
  418. mtk_mdp_prepare_addr(ctx, src_vb, s_frame, &s_frame->addr);
  419. dst_vb = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
  420. mtk_mdp_prepare_addr(ctx, dst_vb, d_frame, &d_frame->addr);
  421. src_vbuf = to_vb2_v4l2_buffer(src_vb);
  422. dst_vbuf = to_vb2_v4l2_buffer(dst_vb);
  423. dst_vbuf->vb2_buf.timestamp = src_vbuf->vb2_buf.timestamp;
  424. }
  425. static void mtk_mdp_process_done(void *priv, int vb_state)
  426. {
  427. struct mtk_mdp_dev *mdp = priv;
  428. struct mtk_mdp_ctx *ctx;
  429. struct vb2_buffer *src_vb, *dst_vb;
  430. struct vb2_v4l2_buffer *src_vbuf = NULL, *dst_vbuf = NULL;
  431. ctx = v4l2_m2m_get_curr_priv(mdp->m2m_dev);
  432. if (!ctx)
  433. return;
  434. src_vb = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
  435. src_vbuf = to_vb2_v4l2_buffer(src_vb);
  436. dst_vb = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
  437. dst_vbuf = to_vb2_v4l2_buffer(dst_vb);
  438. dst_vbuf->vb2_buf.timestamp = src_vbuf->vb2_buf.timestamp;
  439. dst_vbuf->timecode = src_vbuf->timecode;
  440. dst_vbuf->flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
  441. dst_vbuf->flags |= src_vbuf->flags & V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
  442. v4l2_m2m_buf_done(src_vbuf, vb_state);
  443. v4l2_m2m_buf_done(dst_vbuf, vb_state);
  444. v4l2_m2m_job_finish(ctx->mdp_dev->m2m_dev, ctx->m2m_ctx);
  445. }
  446. static void mtk_mdp_m2m_worker(struct work_struct *work)
  447. {
  448. struct mtk_mdp_ctx *ctx =
  449. container_of(work, struct mtk_mdp_ctx, work);
  450. struct mtk_mdp_dev *mdp = ctx->mdp_dev;
  451. enum vb2_buffer_state buf_state = VB2_BUF_STATE_ERROR;
  452. int ret;
  453. if (mtk_mdp_ctx_state_is_set(ctx, MTK_MDP_CTX_ERROR)) {
  454. dev_err(&mdp->pdev->dev, "ctx is in error state");
  455. goto worker_end;
  456. }
  457. mtk_mdp_m2m_get_bufs(ctx);
  458. mtk_mdp_hw_set_input_addr(ctx, &ctx->s_frame.addr);
  459. mtk_mdp_hw_set_output_addr(ctx, &ctx->d_frame.addr);
  460. mtk_mdp_hw_set_in_size(ctx);
  461. mtk_mdp_hw_set_in_image_format(ctx);
  462. mtk_mdp_hw_set_out_size(ctx);
  463. mtk_mdp_hw_set_out_image_format(ctx);
  464. mtk_mdp_hw_set_rotation(ctx);
  465. mtk_mdp_hw_set_global_alpha(ctx);
  466. ret = mtk_mdp_vpu_process(&ctx->vpu);
  467. if (ret) {
  468. dev_err(&mdp->pdev->dev, "processing failed: %d", ret);
  469. goto worker_end;
  470. }
  471. buf_state = VB2_BUF_STATE_DONE;
  472. worker_end:
  473. mtk_mdp_process_done(mdp, buf_state);
  474. }
  475. static void mtk_mdp_m2m_device_run(void *priv)
  476. {
  477. struct mtk_mdp_ctx *ctx = priv;
  478. queue_work(ctx->mdp_dev->job_wq, &ctx->work);
  479. }
  480. static int mtk_mdp_m2m_queue_setup(struct vb2_queue *vq,
  481. unsigned int *num_buffers, unsigned int *num_planes,
  482. unsigned int sizes[], struct device *alloc_devs[])
  483. {
  484. struct mtk_mdp_ctx *ctx = vb2_get_drv_priv(vq);
  485. struct mtk_mdp_frame *frame;
  486. int i;
  487. frame = mtk_mdp_ctx_get_frame(ctx, vq->type);
  488. *num_planes = frame->fmt->num_planes;
  489. for (i = 0; i < frame->fmt->num_planes; i++)
  490. sizes[i] = frame->payload[i];
  491. mtk_mdp_dbg(2, "[%d] type:%d, planes:%d, buffers:%d, size:%u,%u",
  492. ctx->id, vq->type, *num_planes, *num_buffers,
  493. sizes[0], sizes[1]);
  494. return 0;
  495. }
  496. static int mtk_mdp_m2m_buf_prepare(struct vb2_buffer *vb)
  497. {
  498. struct mtk_mdp_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  499. struct mtk_mdp_frame *frame;
  500. int i;
  501. frame = mtk_mdp_ctx_get_frame(ctx, vb->vb2_queue->type);
  502. if (!V4L2_TYPE_IS_OUTPUT(vb->vb2_queue->type)) {
  503. for (i = 0; i < frame->fmt->num_planes; i++)
  504. vb2_set_plane_payload(vb, i, frame->payload[i]);
  505. }
  506. return 0;
  507. }
  508. static void mtk_mdp_m2m_buf_queue(struct vb2_buffer *vb)
  509. {
  510. struct mtk_mdp_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  511. v4l2_m2m_buf_queue(ctx->m2m_ctx, to_vb2_v4l2_buffer(vb));
  512. }
  513. static const struct vb2_ops mtk_mdp_m2m_qops = {
  514. .queue_setup = mtk_mdp_m2m_queue_setup,
  515. .buf_prepare = mtk_mdp_m2m_buf_prepare,
  516. .buf_queue = mtk_mdp_m2m_buf_queue,
  517. .stop_streaming = mtk_mdp_m2m_stop_streaming,
  518. .start_streaming = mtk_mdp_m2m_start_streaming,
  519. .wait_prepare = vb2_ops_wait_prepare,
  520. .wait_finish = vb2_ops_wait_finish,
  521. };
  522. static int mtk_mdp_m2m_querycap(struct file *file, void *fh,
  523. struct v4l2_capability *cap)
  524. {
  525. struct mtk_mdp_ctx *ctx = fh_to_ctx(fh);
  526. struct mtk_mdp_dev *mdp = ctx->mdp_dev;
  527. strlcpy(cap->driver, MTK_MDP_MODULE_NAME, sizeof(cap->driver));
  528. strlcpy(cap->card, mdp->pdev->name, sizeof(cap->card));
  529. strlcpy(cap->bus_info, "platform:mt8173", sizeof(cap->bus_info));
  530. return 0;
  531. }
  532. static int mtk_mdp_enum_fmt_mplane(struct v4l2_fmtdesc *f, u32 type)
  533. {
  534. const struct mtk_mdp_fmt *fmt;
  535. fmt = mtk_mdp_find_fmt_by_index(f->index, type);
  536. if (!fmt)
  537. return -EINVAL;
  538. f->pixelformat = fmt->pixelformat;
  539. return 0;
  540. }
  541. static int mtk_mdp_m2m_enum_fmt_mplane_vid_cap(struct file *file, void *priv,
  542. struct v4l2_fmtdesc *f)
  543. {
  544. return mtk_mdp_enum_fmt_mplane(f, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
  545. }
  546. static int mtk_mdp_m2m_enum_fmt_mplane_vid_out(struct file *file, void *priv,
  547. struct v4l2_fmtdesc *f)
  548. {
  549. return mtk_mdp_enum_fmt_mplane(f, V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
  550. }
  551. static int mtk_mdp_m2m_g_fmt_mplane(struct file *file, void *fh,
  552. struct v4l2_format *f)
  553. {
  554. struct mtk_mdp_ctx *ctx = fh_to_ctx(fh);
  555. struct mtk_mdp_frame *frame;
  556. struct v4l2_pix_format_mplane *pix_mp;
  557. int i;
  558. mtk_mdp_dbg(2, "[%d] type:%d", ctx->id, f->type);
  559. frame = mtk_mdp_ctx_get_frame(ctx, f->type);
  560. pix_mp = &f->fmt.pix_mp;
  561. pix_mp->width = frame->width;
  562. pix_mp->height = frame->height;
  563. pix_mp->field = V4L2_FIELD_NONE;
  564. pix_mp->pixelformat = frame->fmt->pixelformat;
  565. pix_mp->num_planes = frame->fmt->num_planes;
  566. pix_mp->colorspace = ctx->colorspace;
  567. pix_mp->xfer_func = ctx->xfer_func;
  568. pix_mp->ycbcr_enc = ctx->ycbcr_enc;
  569. pix_mp->quantization = ctx->quant;
  570. mtk_mdp_dbg(2, "[%d] wxh:%dx%d", ctx->id,
  571. pix_mp->width, pix_mp->height);
  572. for (i = 0; i < pix_mp->num_planes; ++i) {
  573. pix_mp->plane_fmt[i].bytesperline = (frame->width *
  574. frame->fmt->row_depth[i]) / 8;
  575. pix_mp->plane_fmt[i].sizeimage = (frame->width *
  576. frame->height * frame->fmt->depth[i]) / 8;
  577. mtk_mdp_dbg(2, "[%d] p%d, bpl:%d, sizeimage:%d", ctx->id, i,
  578. pix_mp->plane_fmt[i].bytesperline,
  579. pix_mp->plane_fmt[i].sizeimage);
  580. }
  581. return 0;
  582. }
  583. static int mtk_mdp_m2m_try_fmt_mplane(struct file *file, void *fh,
  584. struct v4l2_format *f)
  585. {
  586. struct mtk_mdp_ctx *ctx = fh_to_ctx(fh);
  587. if (!mtk_mdp_try_fmt_mplane(ctx, f))
  588. return -EINVAL;
  589. return 0;
  590. }
  591. static int mtk_mdp_m2m_s_fmt_mplane(struct file *file, void *fh,
  592. struct v4l2_format *f)
  593. {
  594. struct mtk_mdp_ctx *ctx = fh_to_ctx(fh);
  595. struct vb2_queue *vq;
  596. struct mtk_mdp_frame *frame;
  597. struct v4l2_pix_format_mplane *pix_mp;
  598. const struct mtk_mdp_fmt *fmt;
  599. int i;
  600. mtk_mdp_dbg(2, "[%d] type:%d", ctx->id, f->type);
  601. frame = mtk_mdp_ctx_get_frame(ctx, f->type);
  602. fmt = mtk_mdp_try_fmt_mplane(ctx, f);
  603. if (!fmt) {
  604. mtk_mdp_err("[%d] try_fmt failed, type:%d", ctx->id, f->type);
  605. return -EINVAL;
  606. }
  607. frame->fmt = fmt;
  608. vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
  609. if (vb2_is_streaming(vq)) {
  610. dev_info(&ctx->mdp_dev->pdev->dev, "queue %d busy", f->type);
  611. return -EBUSY;
  612. }
  613. pix_mp = &f->fmt.pix_mp;
  614. for (i = 0; i < frame->fmt->num_planes; i++) {
  615. frame->payload[i] = pix_mp->plane_fmt[i].sizeimage;
  616. frame->pitch[i] = pix_mp->plane_fmt[i].bytesperline;
  617. }
  618. mtk_mdp_set_frame_size(frame, pix_mp->width, pix_mp->height);
  619. if (V4L2_TYPE_IS_OUTPUT(f->type)) {
  620. ctx->colorspace = pix_mp->colorspace;
  621. ctx->xfer_func = pix_mp->xfer_func;
  622. ctx->ycbcr_enc = pix_mp->ycbcr_enc;
  623. ctx->quant = pix_mp->quantization;
  624. }
  625. if (V4L2_TYPE_IS_OUTPUT(f->type))
  626. mtk_mdp_ctx_state_lock_set(ctx, MTK_MDP_SRC_FMT);
  627. else
  628. mtk_mdp_ctx_state_lock_set(ctx, MTK_MDP_DST_FMT);
  629. mtk_mdp_dbg(2, "[%d] type:%d, frame:%dx%d", ctx->id, f->type,
  630. frame->width, frame->height);
  631. return 0;
  632. }
  633. static int mtk_mdp_m2m_reqbufs(struct file *file, void *fh,
  634. struct v4l2_requestbuffers *reqbufs)
  635. {
  636. struct mtk_mdp_ctx *ctx = fh_to_ctx(fh);
  637. if (reqbufs->count == 0) {
  638. if (reqbufs->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
  639. mtk_mdp_ctx_state_lock_clear(ctx, MTK_MDP_SRC_FMT);
  640. else
  641. mtk_mdp_ctx_state_lock_clear(ctx, MTK_MDP_DST_FMT);
  642. }
  643. return v4l2_m2m_reqbufs(file, ctx->m2m_ctx, reqbufs);
  644. }
  645. static int mtk_mdp_m2m_streamon(struct file *file, void *fh,
  646. enum v4l2_buf_type type)
  647. {
  648. struct mtk_mdp_ctx *ctx = fh_to_ctx(fh);
  649. int ret;
  650. /* The source and target color format need to be set */
  651. if (V4L2_TYPE_IS_OUTPUT(type)) {
  652. if (!mtk_mdp_ctx_state_is_set(ctx, MTK_MDP_SRC_FMT))
  653. return -EINVAL;
  654. } else if (!mtk_mdp_ctx_state_is_set(ctx, MTK_MDP_DST_FMT)) {
  655. return -EINVAL;
  656. }
  657. if (!mtk_mdp_ctx_state_is_set(ctx, MTK_MDP_VPU_INIT)) {
  658. ret = mtk_mdp_vpu_init(&ctx->vpu);
  659. if (ret < 0) {
  660. dev_err(&ctx->mdp_dev->pdev->dev,
  661. "vpu init failed %d\n",
  662. ret);
  663. return -EINVAL;
  664. }
  665. mtk_mdp_ctx_state_lock_set(ctx, MTK_MDP_VPU_INIT);
  666. }
  667. return v4l2_m2m_streamon(file, ctx->m2m_ctx, type);
  668. }
  669. static inline bool mtk_mdp_is_target_compose(u32 target)
  670. {
  671. if (target == V4L2_SEL_TGT_COMPOSE_DEFAULT
  672. || target == V4L2_SEL_TGT_COMPOSE_BOUNDS
  673. || target == V4L2_SEL_TGT_COMPOSE)
  674. return true;
  675. return false;
  676. }
  677. static inline bool mtk_mdp_is_target_crop(u32 target)
  678. {
  679. if (target == V4L2_SEL_TGT_CROP_DEFAULT
  680. || target == V4L2_SEL_TGT_CROP_BOUNDS
  681. || target == V4L2_SEL_TGT_CROP)
  682. return true;
  683. return false;
  684. }
  685. static int mtk_mdp_m2m_g_selection(struct file *file, void *fh,
  686. struct v4l2_selection *s)
  687. {
  688. struct mtk_mdp_frame *frame;
  689. struct mtk_mdp_ctx *ctx = fh_to_ctx(fh);
  690. bool valid = false;
  691. if (s->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  692. if (mtk_mdp_is_target_compose(s->target))
  693. valid = true;
  694. } else if (s->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
  695. if (mtk_mdp_is_target_crop(s->target))
  696. valid = true;
  697. }
  698. if (!valid) {
  699. mtk_mdp_dbg(1, "[%d] invalid type:%d,%u", ctx->id, s->type,
  700. s->target);
  701. return -EINVAL;
  702. }
  703. frame = mtk_mdp_ctx_get_frame(ctx, s->type);
  704. switch (s->target) {
  705. case V4L2_SEL_TGT_COMPOSE_DEFAULT:
  706. case V4L2_SEL_TGT_COMPOSE_BOUNDS:
  707. case V4L2_SEL_TGT_CROP_BOUNDS:
  708. case V4L2_SEL_TGT_CROP_DEFAULT:
  709. s->r.left = 0;
  710. s->r.top = 0;
  711. s->r.width = frame->width;
  712. s->r.height = frame->height;
  713. return 0;
  714. case V4L2_SEL_TGT_COMPOSE:
  715. case V4L2_SEL_TGT_CROP:
  716. s->r.left = frame->crop.left;
  717. s->r.top = frame->crop.top;
  718. s->r.width = frame->crop.width;
  719. s->r.height = frame->crop.height;
  720. return 0;
  721. }
  722. return -EINVAL;
  723. }
  724. static int mtk_mdp_check_scaler_ratio(struct mtk_mdp_variant *var, int src_w,
  725. int src_h, int dst_w, int dst_h, int rot)
  726. {
  727. int tmp_w, tmp_h;
  728. if (rot == 90 || rot == 270) {
  729. tmp_w = dst_h;
  730. tmp_h = dst_w;
  731. } else {
  732. tmp_w = dst_w;
  733. tmp_h = dst_h;
  734. }
  735. if ((src_w / tmp_w) > var->h_scale_down_max ||
  736. (src_h / tmp_h) > var->v_scale_down_max ||
  737. (tmp_w / src_w) > var->h_scale_up_max ||
  738. (tmp_h / src_h) > var->v_scale_up_max)
  739. return -EINVAL;
  740. return 0;
  741. }
  742. static int mtk_mdp_m2m_s_selection(struct file *file, void *fh,
  743. struct v4l2_selection *s)
  744. {
  745. struct mtk_mdp_frame *frame;
  746. struct mtk_mdp_ctx *ctx = fh_to_ctx(fh);
  747. struct v4l2_rect new_r;
  748. struct mtk_mdp_variant *variant = ctx->mdp_dev->variant;
  749. int ret;
  750. bool valid = false;
  751. if (s->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  752. if (s->target == V4L2_SEL_TGT_COMPOSE)
  753. valid = true;
  754. } else if (s->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
  755. if (s->target == V4L2_SEL_TGT_CROP)
  756. valid = true;
  757. }
  758. if (!valid) {
  759. mtk_mdp_dbg(1, "[%d] invalid type:%d,%u", ctx->id, s->type,
  760. s->target);
  761. return -EINVAL;
  762. }
  763. new_r = s->r;
  764. ret = mtk_mdp_try_crop(ctx, s->type, &new_r);
  765. if (ret)
  766. return ret;
  767. if (mtk_mdp_is_target_crop(s->target))
  768. frame = &ctx->s_frame;
  769. else
  770. frame = &ctx->d_frame;
  771. /* Check to see if scaling ratio is within supported range */
  772. if (mtk_mdp_ctx_state_is_set(ctx, MTK_MDP_DST_FMT | MTK_MDP_SRC_FMT)) {
  773. if (V4L2_TYPE_IS_OUTPUT(s->type)) {
  774. ret = mtk_mdp_check_scaler_ratio(variant, new_r.width,
  775. new_r.height, ctx->d_frame.crop.width,
  776. ctx->d_frame.crop.height,
  777. ctx->ctrls.rotate->val);
  778. } else {
  779. ret = mtk_mdp_check_scaler_ratio(variant,
  780. ctx->s_frame.crop.width,
  781. ctx->s_frame.crop.height, new_r.width,
  782. new_r.height, ctx->ctrls.rotate->val);
  783. }
  784. if (ret) {
  785. dev_info(&ctx->mdp_dev->pdev->dev,
  786. "Out of scaler range");
  787. return -EINVAL;
  788. }
  789. }
  790. s->r = new_r;
  791. frame->crop = new_r;
  792. return 0;
  793. }
  794. static const struct v4l2_ioctl_ops mtk_mdp_m2m_ioctl_ops = {
  795. .vidioc_querycap = mtk_mdp_m2m_querycap,
  796. .vidioc_enum_fmt_vid_cap_mplane = mtk_mdp_m2m_enum_fmt_mplane_vid_cap,
  797. .vidioc_enum_fmt_vid_out_mplane = mtk_mdp_m2m_enum_fmt_mplane_vid_out,
  798. .vidioc_g_fmt_vid_cap_mplane = mtk_mdp_m2m_g_fmt_mplane,
  799. .vidioc_g_fmt_vid_out_mplane = mtk_mdp_m2m_g_fmt_mplane,
  800. .vidioc_try_fmt_vid_cap_mplane = mtk_mdp_m2m_try_fmt_mplane,
  801. .vidioc_try_fmt_vid_out_mplane = mtk_mdp_m2m_try_fmt_mplane,
  802. .vidioc_s_fmt_vid_cap_mplane = mtk_mdp_m2m_s_fmt_mplane,
  803. .vidioc_s_fmt_vid_out_mplane = mtk_mdp_m2m_s_fmt_mplane,
  804. .vidioc_reqbufs = mtk_mdp_m2m_reqbufs,
  805. .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
  806. .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
  807. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  808. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  809. .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
  810. .vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
  811. .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
  812. .vidioc_streamon = mtk_mdp_m2m_streamon,
  813. .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
  814. .vidioc_g_selection = mtk_mdp_m2m_g_selection,
  815. .vidioc_s_selection = mtk_mdp_m2m_s_selection
  816. };
  817. static int mtk_mdp_m2m_queue_init(void *priv, struct vb2_queue *src_vq,
  818. struct vb2_queue *dst_vq)
  819. {
  820. struct mtk_mdp_ctx *ctx = priv;
  821. int ret;
  822. memset(src_vq, 0, sizeof(*src_vq));
  823. src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
  824. src_vq->io_modes = VB2_MMAP | VB2_DMABUF;
  825. src_vq->drv_priv = ctx;
  826. src_vq->ops = &mtk_mdp_m2m_qops;
  827. src_vq->mem_ops = &vb2_dma_contig_memops;
  828. src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
  829. src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
  830. src_vq->dev = &ctx->mdp_dev->pdev->dev;
  831. src_vq->lock = &ctx->mdp_dev->lock;
  832. ret = vb2_queue_init(src_vq);
  833. if (ret)
  834. return ret;
  835. memset(dst_vq, 0, sizeof(*dst_vq));
  836. dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
  837. dst_vq->io_modes = VB2_MMAP | VB2_DMABUF;
  838. dst_vq->drv_priv = ctx;
  839. dst_vq->ops = &mtk_mdp_m2m_qops;
  840. dst_vq->mem_ops = &vb2_dma_contig_memops;
  841. dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
  842. dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
  843. dst_vq->dev = &ctx->mdp_dev->pdev->dev;
  844. dst_vq->lock = &ctx->mdp_dev->lock;
  845. return vb2_queue_init(dst_vq);
  846. }
  847. static int mtk_mdp_s_ctrl(struct v4l2_ctrl *ctrl)
  848. {
  849. struct mtk_mdp_ctx *ctx = ctrl_to_ctx(ctrl);
  850. struct mtk_mdp_dev *mdp = ctx->mdp_dev;
  851. struct mtk_mdp_variant *variant = mdp->variant;
  852. u32 state = MTK_MDP_DST_FMT | MTK_MDP_SRC_FMT;
  853. int ret = 0;
  854. if (ctrl->flags & V4L2_CTRL_FLAG_INACTIVE)
  855. return 0;
  856. switch (ctrl->id) {
  857. case V4L2_CID_HFLIP:
  858. ctx->hflip = ctrl->val;
  859. break;
  860. case V4L2_CID_VFLIP:
  861. ctx->vflip = ctrl->val;
  862. break;
  863. case V4L2_CID_ROTATE:
  864. if (mtk_mdp_ctx_state_is_set(ctx, state)) {
  865. ret = mtk_mdp_check_scaler_ratio(variant,
  866. ctx->s_frame.crop.width,
  867. ctx->s_frame.crop.height,
  868. ctx->d_frame.crop.width,
  869. ctx->d_frame.crop.height,
  870. ctx->ctrls.rotate->val);
  871. if (ret)
  872. return -EINVAL;
  873. }
  874. ctx->rotation = ctrl->val;
  875. break;
  876. case V4L2_CID_ALPHA_COMPONENT:
  877. ctx->d_frame.alpha = ctrl->val;
  878. break;
  879. }
  880. return 0;
  881. }
  882. static const struct v4l2_ctrl_ops mtk_mdp_ctrl_ops = {
  883. .s_ctrl = mtk_mdp_s_ctrl,
  884. };
  885. static int mtk_mdp_ctrls_create(struct mtk_mdp_ctx *ctx)
  886. {
  887. v4l2_ctrl_handler_init(&ctx->ctrl_handler, MTK_MDP_MAX_CTRL_NUM);
  888. ctx->ctrls.rotate = v4l2_ctrl_new_std(&ctx->ctrl_handler,
  889. &mtk_mdp_ctrl_ops, V4L2_CID_ROTATE, 0, 270, 90, 0);
  890. ctx->ctrls.hflip = v4l2_ctrl_new_std(&ctx->ctrl_handler,
  891. &mtk_mdp_ctrl_ops,
  892. V4L2_CID_HFLIP,
  893. 0, 1, 1, 0);
  894. ctx->ctrls.vflip = v4l2_ctrl_new_std(&ctx->ctrl_handler,
  895. &mtk_mdp_ctrl_ops,
  896. V4L2_CID_VFLIP,
  897. 0, 1, 1, 0);
  898. ctx->ctrls.global_alpha = v4l2_ctrl_new_std(&ctx->ctrl_handler,
  899. &mtk_mdp_ctrl_ops,
  900. V4L2_CID_ALPHA_COMPONENT,
  901. 0, 255, 1, 0);
  902. ctx->ctrls_rdy = ctx->ctrl_handler.error == 0;
  903. if (ctx->ctrl_handler.error) {
  904. int err = ctx->ctrl_handler.error;
  905. v4l2_ctrl_handler_free(&ctx->ctrl_handler);
  906. dev_err(&ctx->mdp_dev->pdev->dev,
  907. "Failed to create control handlers\n");
  908. return err;
  909. }
  910. return 0;
  911. }
  912. static void mtk_mdp_set_default_params(struct mtk_mdp_ctx *ctx)
  913. {
  914. struct mtk_mdp_dev *mdp = ctx->mdp_dev;
  915. struct mtk_mdp_frame *frame;
  916. frame = mtk_mdp_ctx_get_frame(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
  917. frame->fmt = mtk_mdp_find_fmt_by_index(0,
  918. V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
  919. frame->width = mdp->variant->pix_min->org_w;
  920. frame->height = mdp->variant->pix_min->org_h;
  921. frame->payload[0] = frame->width * frame->height;
  922. frame->payload[1] = frame->payload[0] / 2;
  923. frame = mtk_mdp_ctx_get_frame(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
  924. frame->fmt = mtk_mdp_find_fmt_by_index(0,
  925. V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
  926. frame->width = mdp->variant->pix_min->target_rot_dis_w;
  927. frame->height = mdp->variant->pix_min->target_rot_dis_h;
  928. frame->payload[0] = frame->width * frame->height;
  929. frame->payload[1] = frame->payload[0] / 2;
  930. }
  931. static int mtk_mdp_m2m_open(struct file *file)
  932. {
  933. struct mtk_mdp_dev *mdp = video_drvdata(file);
  934. struct video_device *vfd = video_devdata(file);
  935. struct mtk_mdp_ctx *ctx = NULL;
  936. int ret;
  937. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  938. if (!ctx)
  939. return -ENOMEM;
  940. if (mutex_lock_interruptible(&mdp->lock)) {
  941. ret = -ERESTARTSYS;
  942. goto err_lock;
  943. }
  944. mutex_init(&ctx->slock);
  945. ctx->id = mdp->id_counter++;
  946. v4l2_fh_init(&ctx->fh, vfd);
  947. file->private_data = &ctx->fh;
  948. ret = mtk_mdp_ctrls_create(ctx);
  949. if (ret)
  950. goto error_ctrls;
  951. /* Use separate control handler per file handle */
  952. ctx->fh.ctrl_handler = &ctx->ctrl_handler;
  953. v4l2_fh_add(&ctx->fh);
  954. INIT_LIST_HEAD(&ctx->list);
  955. ctx->mdp_dev = mdp;
  956. mtk_mdp_set_default_params(ctx);
  957. INIT_WORK(&ctx->work, mtk_mdp_m2m_worker);
  958. ctx->m2m_ctx = v4l2_m2m_ctx_init(mdp->m2m_dev, ctx,
  959. mtk_mdp_m2m_queue_init);
  960. if (IS_ERR(ctx->m2m_ctx)) {
  961. dev_err(&mdp->pdev->dev, "Failed to initialize m2m context");
  962. ret = PTR_ERR(ctx->m2m_ctx);
  963. goto error_m2m_ctx;
  964. }
  965. ctx->fh.m2m_ctx = ctx->m2m_ctx;
  966. if (mdp->ctx_num++ == 0) {
  967. ret = vpu_load_firmware(mdp->vpu_dev);
  968. if (ret < 0) {
  969. dev_err(&mdp->pdev->dev,
  970. "vpu_load_firmware failed %d\n", ret);
  971. goto err_load_vpu;
  972. }
  973. ret = mtk_mdp_vpu_register(mdp->pdev);
  974. if (ret < 0) {
  975. dev_err(&mdp->pdev->dev,
  976. "mdp_vpu register failed %d\n", ret);
  977. goto err_load_vpu;
  978. }
  979. }
  980. list_add(&ctx->list, &mdp->ctx_list);
  981. mutex_unlock(&mdp->lock);
  982. mtk_mdp_dbg(0, "%s [%d]", dev_name(&mdp->pdev->dev), ctx->id);
  983. return 0;
  984. err_load_vpu:
  985. mdp->ctx_num--;
  986. v4l2_m2m_ctx_release(ctx->m2m_ctx);
  987. error_m2m_ctx:
  988. v4l2_ctrl_handler_free(&ctx->ctrl_handler);
  989. error_ctrls:
  990. v4l2_fh_del(&ctx->fh);
  991. v4l2_fh_exit(&ctx->fh);
  992. mutex_unlock(&mdp->lock);
  993. err_lock:
  994. kfree(ctx);
  995. return ret;
  996. }
  997. static int mtk_mdp_m2m_release(struct file *file)
  998. {
  999. struct mtk_mdp_ctx *ctx = fh_to_ctx(file->private_data);
  1000. struct mtk_mdp_dev *mdp = ctx->mdp_dev;
  1001. flush_workqueue(mdp->job_wq);
  1002. mutex_lock(&mdp->lock);
  1003. v4l2_m2m_ctx_release(ctx->m2m_ctx);
  1004. v4l2_ctrl_handler_free(&ctx->ctrl_handler);
  1005. v4l2_fh_del(&ctx->fh);
  1006. v4l2_fh_exit(&ctx->fh);
  1007. mtk_mdp_vpu_deinit(&ctx->vpu);
  1008. mdp->ctx_num--;
  1009. list_del_init(&ctx->list);
  1010. mtk_mdp_dbg(0, "%s [%d]", dev_name(&mdp->pdev->dev), ctx->id);
  1011. mutex_unlock(&mdp->lock);
  1012. kfree(ctx);
  1013. return 0;
  1014. }
  1015. static const struct v4l2_file_operations mtk_mdp_m2m_fops = {
  1016. .owner = THIS_MODULE,
  1017. .open = mtk_mdp_m2m_open,
  1018. .release = mtk_mdp_m2m_release,
  1019. .poll = v4l2_m2m_fop_poll,
  1020. .unlocked_ioctl = video_ioctl2,
  1021. .mmap = v4l2_m2m_fop_mmap,
  1022. };
  1023. static const struct v4l2_m2m_ops mtk_mdp_m2m_ops = {
  1024. .device_run = mtk_mdp_m2m_device_run,
  1025. };
  1026. int mtk_mdp_register_m2m_device(struct mtk_mdp_dev *mdp)
  1027. {
  1028. struct device *dev = &mdp->pdev->dev;
  1029. int ret;
  1030. mdp->variant = &mtk_mdp_default_variant;
  1031. mdp->vdev = video_device_alloc();
  1032. if (!mdp->vdev) {
  1033. dev_err(dev, "failed to allocate video device\n");
  1034. ret = -ENOMEM;
  1035. goto err_video_alloc;
  1036. }
  1037. mdp->vdev->device_caps = V4L2_CAP_VIDEO_M2M_MPLANE | V4L2_CAP_STREAMING;
  1038. mdp->vdev->fops = &mtk_mdp_m2m_fops;
  1039. mdp->vdev->ioctl_ops = &mtk_mdp_m2m_ioctl_ops;
  1040. mdp->vdev->release = video_device_release;
  1041. mdp->vdev->lock = &mdp->lock;
  1042. mdp->vdev->vfl_dir = VFL_DIR_M2M;
  1043. mdp->vdev->v4l2_dev = &mdp->v4l2_dev;
  1044. snprintf(mdp->vdev->name, sizeof(mdp->vdev->name), "%s:m2m",
  1045. MTK_MDP_MODULE_NAME);
  1046. video_set_drvdata(mdp->vdev, mdp);
  1047. mdp->m2m_dev = v4l2_m2m_init(&mtk_mdp_m2m_ops);
  1048. if (IS_ERR(mdp->m2m_dev)) {
  1049. dev_err(dev, "failed to initialize v4l2-m2m device\n");
  1050. ret = PTR_ERR(mdp->m2m_dev);
  1051. goto err_m2m_init;
  1052. }
  1053. ret = video_register_device(mdp->vdev, VFL_TYPE_GRABBER, 2);
  1054. if (ret) {
  1055. dev_err(dev, "failed to register video device\n");
  1056. goto err_vdev_register;
  1057. }
  1058. v4l2_info(&mdp->v4l2_dev, "driver registered as /dev/video%d",
  1059. mdp->vdev->num);
  1060. return 0;
  1061. err_vdev_register:
  1062. v4l2_m2m_release(mdp->m2m_dev);
  1063. err_m2m_init:
  1064. video_device_release(mdp->vdev);
  1065. err_video_alloc:
  1066. return ret;
  1067. }
  1068. void mtk_mdp_unregister_m2m_device(struct mtk_mdp_dev *mdp)
  1069. {
  1070. video_unregister_device(mdp->vdev);
  1071. v4l2_m2m_release(mdp->m2m_dev);
  1072. }