mtk_mdp_m2m.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286
  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_ctx_lock(struct vb2_queue *vq)
  346. {
  347. struct mtk_mdp_ctx *ctx = vb2_get_drv_priv(vq);
  348. mutex_lock(&ctx->mdp_dev->lock);
  349. }
  350. static void mtk_mdp_ctx_unlock(struct vb2_queue *vq)
  351. {
  352. struct mtk_mdp_ctx *ctx = vb2_get_drv_priv(vq);
  353. mutex_unlock(&ctx->mdp_dev->lock);
  354. }
  355. static void mtk_mdp_set_frame_size(struct mtk_mdp_frame *frame, int width,
  356. int height)
  357. {
  358. frame->width = width;
  359. frame->height = height;
  360. frame->crop.width = width;
  361. frame->crop.height = height;
  362. frame->crop.left = 0;
  363. frame->crop.top = 0;
  364. }
  365. static int mtk_mdp_m2m_start_streaming(struct vb2_queue *q, unsigned int count)
  366. {
  367. struct mtk_mdp_ctx *ctx = q->drv_priv;
  368. int ret;
  369. ret = pm_runtime_get_sync(&ctx->mdp_dev->pdev->dev);
  370. if (ret < 0)
  371. mtk_mdp_dbg(1, "[%d] pm_runtime_get_sync failed:%d",
  372. ctx->id, ret);
  373. return 0;
  374. }
  375. static void *mtk_mdp_m2m_buf_remove(struct mtk_mdp_ctx *ctx,
  376. enum v4l2_buf_type type)
  377. {
  378. if (V4L2_TYPE_IS_OUTPUT(type))
  379. return v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
  380. else
  381. return v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
  382. }
  383. static void mtk_mdp_m2m_stop_streaming(struct vb2_queue *q)
  384. {
  385. struct mtk_mdp_ctx *ctx = q->drv_priv;
  386. struct vb2_buffer *vb;
  387. vb = mtk_mdp_m2m_buf_remove(ctx, q->type);
  388. while (vb != NULL) {
  389. v4l2_m2m_buf_done(to_vb2_v4l2_buffer(vb), VB2_BUF_STATE_ERROR);
  390. vb = mtk_mdp_m2m_buf_remove(ctx, q->type);
  391. }
  392. pm_runtime_put(&ctx->mdp_dev->pdev->dev);
  393. }
  394. static void mtk_mdp_m2m_job_abort(void *priv)
  395. {
  396. }
  397. /* The color format (num_planes) must be already configured. */
  398. static void mtk_mdp_prepare_addr(struct mtk_mdp_ctx *ctx,
  399. struct vb2_buffer *vb,
  400. struct mtk_mdp_frame *frame,
  401. struct mtk_mdp_addr *addr)
  402. {
  403. u32 pix_size, planes, i;
  404. pix_size = frame->width * frame->height;
  405. planes = min_t(u32, frame->fmt->num_planes, ARRAY_SIZE(addr->addr));
  406. for (i = 0; i < planes; i++)
  407. addr->addr[i] = vb2_dma_contig_plane_dma_addr(vb, i);
  408. if (planes == 1) {
  409. if (frame->fmt->pixelformat == V4L2_PIX_FMT_YVU420) {
  410. addr->addr[1] = (dma_addr_t)(addr->addr[0] + pix_size);
  411. addr->addr[2] = (dma_addr_t)(addr->addr[1] +
  412. (pix_size >> 2));
  413. } else {
  414. dev_err(&ctx->mdp_dev->pdev->dev,
  415. "Invalid pixelformat:0x%x\n",
  416. frame->fmt->pixelformat);
  417. }
  418. }
  419. mtk_mdp_dbg(3, "[%d] planes:%d, size:%d, addr:%p,%p,%p",
  420. ctx->id, planes, pix_size, (void *)addr->addr[0],
  421. (void *)addr->addr[1], (void *)addr->addr[2]);
  422. }
  423. static void mtk_mdp_m2m_get_bufs(struct mtk_mdp_ctx *ctx)
  424. {
  425. struct mtk_mdp_frame *s_frame, *d_frame;
  426. struct vb2_buffer *src_vb, *dst_vb;
  427. struct vb2_v4l2_buffer *src_vbuf, *dst_vbuf;
  428. s_frame = &ctx->s_frame;
  429. d_frame = &ctx->d_frame;
  430. src_vb = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
  431. mtk_mdp_prepare_addr(ctx, src_vb, s_frame, &s_frame->addr);
  432. dst_vb = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
  433. mtk_mdp_prepare_addr(ctx, dst_vb, d_frame, &d_frame->addr);
  434. src_vbuf = to_vb2_v4l2_buffer(src_vb);
  435. dst_vbuf = to_vb2_v4l2_buffer(dst_vb);
  436. dst_vbuf->vb2_buf.timestamp = src_vbuf->vb2_buf.timestamp;
  437. }
  438. static void mtk_mdp_process_done(void *priv, int vb_state)
  439. {
  440. struct mtk_mdp_dev *mdp = priv;
  441. struct mtk_mdp_ctx *ctx;
  442. struct vb2_buffer *src_vb, *dst_vb;
  443. struct vb2_v4l2_buffer *src_vbuf = NULL, *dst_vbuf = NULL;
  444. ctx = v4l2_m2m_get_curr_priv(mdp->m2m_dev);
  445. if (!ctx)
  446. return;
  447. src_vb = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
  448. src_vbuf = to_vb2_v4l2_buffer(src_vb);
  449. dst_vb = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
  450. dst_vbuf = to_vb2_v4l2_buffer(dst_vb);
  451. dst_vbuf->vb2_buf.timestamp = src_vbuf->vb2_buf.timestamp;
  452. dst_vbuf->timecode = src_vbuf->timecode;
  453. dst_vbuf->flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
  454. dst_vbuf->flags |= src_vbuf->flags & V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
  455. v4l2_m2m_buf_done(src_vbuf, vb_state);
  456. v4l2_m2m_buf_done(dst_vbuf, vb_state);
  457. v4l2_m2m_job_finish(ctx->mdp_dev->m2m_dev, ctx->m2m_ctx);
  458. }
  459. static void mtk_mdp_m2m_worker(struct work_struct *work)
  460. {
  461. struct mtk_mdp_ctx *ctx =
  462. container_of(work, struct mtk_mdp_ctx, work);
  463. struct mtk_mdp_dev *mdp = ctx->mdp_dev;
  464. enum vb2_buffer_state buf_state = VB2_BUF_STATE_ERROR;
  465. int ret;
  466. if (mtk_mdp_ctx_state_is_set(ctx, MTK_MDP_CTX_ERROR)) {
  467. dev_err(&mdp->pdev->dev, "ctx is in error state");
  468. goto worker_end;
  469. }
  470. mtk_mdp_m2m_get_bufs(ctx);
  471. mtk_mdp_hw_set_input_addr(ctx, &ctx->s_frame.addr);
  472. mtk_mdp_hw_set_output_addr(ctx, &ctx->d_frame.addr);
  473. mtk_mdp_hw_set_in_size(ctx);
  474. mtk_mdp_hw_set_in_image_format(ctx);
  475. mtk_mdp_hw_set_out_size(ctx);
  476. mtk_mdp_hw_set_out_image_format(ctx);
  477. mtk_mdp_hw_set_rotation(ctx);
  478. mtk_mdp_hw_set_global_alpha(ctx);
  479. ret = mtk_mdp_vpu_process(&ctx->vpu);
  480. if (ret) {
  481. dev_err(&mdp->pdev->dev, "processing failed: %d", ret);
  482. goto worker_end;
  483. }
  484. buf_state = VB2_BUF_STATE_DONE;
  485. worker_end:
  486. mtk_mdp_process_done(mdp, buf_state);
  487. }
  488. static void mtk_mdp_m2m_device_run(void *priv)
  489. {
  490. struct mtk_mdp_ctx *ctx = priv;
  491. queue_work(ctx->mdp_dev->job_wq, &ctx->work);
  492. }
  493. static int mtk_mdp_m2m_queue_setup(struct vb2_queue *vq,
  494. unsigned int *num_buffers, unsigned int *num_planes,
  495. unsigned int sizes[], struct device *alloc_devs[])
  496. {
  497. struct mtk_mdp_ctx *ctx = vb2_get_drv_priv(vq);
  498. struct mtk_mdp_frame *frame;
  499. int i;
  500. frame = mtk_mdp_ctx_get_frame(ctx, vq->type);
  501. *num_planes = frame->fmt->num_planes;
  502. for (i = 0; i < frame->fmt->num_planes; i++)
  503. sizes[i] = frame->payload[i];
  504. mtk_mdp_dbg(2, "[%d] type:%d, planes:%d, buffers:%d, size:%u,%u",
  505. ctx->id, vq->type, *num_planes, *num_buffers,
  506. sizes[0], sizes[1]);
  507. return 0;
  508. }
  509. static int mtk_mdp_m2m_buf_prepare(struct vb2_buffer *vb)
  510. {
  511. struct mtk_mdp_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  512. struct mtk_mdp_frame *frame;
  513. int i;
  514. frame = mtk_mdp_ctx_get_frame(ctx, vb->vb2_queue->type);
  515. if (!V4L2_TYPE_IS_OUTPUT(vb->vb2_queue->type)) {
  516. for (i = 0; i < frame->fmt->num_planes; i++)
  517. vb2_set_plane_payload(vb, i, frame->payload[i]);
  518. }
  519. return 0;
  520. }
  521. static void mtk_mdp_m2m_buf_queue(struct vb2_buffer *vb)
  522. {
  523. struct mtk_mdp_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  524. v4l2_m2m_buf_queue(ctx->m2m_ctx, to_vb2_v4l2_buffer(vb));
  525. }
  526. static struct vb2_ops mtk_mdp_m2m_qops = {
  527. .queue_setup = mtk_mdp_m2m_queue_setup,
  528. .buf_prepare = mtk_mdp_m2m_buf_prepare,
  529. .buf_queue = mtk_mdp_m2m_buf_queue,
  530. .wait_prepare = mtk_mdp_ctx_unlock,
  531. .wait_finish = mtk_mdp_ctx_lock,
  532. .stop_streaming = mtk_mdp_m2m_stop_streaming,
  533. .start_streaming = mtk_mdp_m2m_start_streaming,
  534. };
  535. static int mtk_mdp_m2m_querycap(struct file *file, void *fh,
  536. struct v4l2_capability *cap)
  537. {
  538. struct mtk_mdp_ctx *ctx = fh_to_ctx(fh);
  539. struct mtk_mdp_dev *mdp = ctx->mdp_dev;
  540. strlcpy(cap->driver, MTK_MDP_MODULE_NAME, sizeof(cap->driver));
  541. strlcpy(cap->card, mdp->pdev->name, sizeof(cap->card));
  542. strlcpy(cap->bus_info, "platform:mt8173", sizeof(cap->bus_info));
  543. return 0;
  544. }
  545. static int mtk_mdp_enum_fmt_mplane(struct v4l2_fmtdesc *f, u32 type)
  546. {
  547. const struct mtk_mdp_fmt *fmt;
  548. fmt = mtk_mdp_find_fmt_by_index(f->index, type);
  549. if (!fmt)
  550. return -EINVAL;
  551. f->pixelformat = fmt->pixelformat;
  552. return 0;
  553. }
  554. static int mtk_mdp_m2m_enum_fmt_mplane_vid_cap(struct file *file, void *priv,
  555. struct v4l2_fmtdesc *f)
  556. {
  557. return mtk_mdp_enum_fmt_mplane(f, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
  558. }
  559. static int mtk_mdp_m2m_enum_fmt_mplane_vid_out(struct file *file, void *priv,
  560. struct v4l2_fmtdesc *f)
  561. {
  562. return mtk_mdp_enum_fmt_mplane(f, V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
  563. }
  564. static int mtk_mdp_m2m_g_fmt_mplane(struct file *file, void *fh,
  565. struct v4l2_format *f)
  566. {
  567. struct mtk_mdp_ctx *ctx = fh_to_ctx(fh);
  568. struct mtk_mdp_frame *frame;
  569. struct v4l2_pix_format_mplane *pix_mp;
  570. int i;
  571. mtk_mdp_dbg(2, "[%d] type:%d", ctx->id, f->type);
  572. frame = mtk_mdp_ctx_get_frame(ctx, f->type);
  573. pix_mp = &f->fmt.pix_mp;
  574. pix_mp->width = frame->width;
  575. pix_mp->height = frame->height;
  576. pix_mp->field = V4L2_FIELD_NONE;
  577. pix_mp->pixelformat = frame->fmt->pixelformat;
  578. pix_mp->num_planes = frame->fmt->num_planes;
  579. pix_mp->colorspace = ctx->colorspace;
  580. pix_mp->xfer_func = ctx->xfer_func;
  581. pix_mp->ycbcr_enc = ctx->ycbcr_enc;
  582. pix_mp->quantization = ctx->quant;
  583. mtk_mdp_dbg(2, "[%d] wxh:%dx%d", ctx->id,
  584. pix_mp->width, pix_mp->height);
  585. for (i = 0; i < pix_mp->num_planes; ++i) {
  586. pix_mp->plane_fmt[i].bytesperline = (frame->width *
  587. frame->fmt->row_depth[i]) / 8;
  588. pix_mp->plane_fmt[i].sizeimage = (frame->width *
  589. frame->height * frame->fmt->depth[i]) / 8;
  590. mtk_mdp_dbg(2, "[%d] p%d, bpl:%d, sizeimage:%d", ctx->id, i,
  591. pix_mp->plane_fmt[i].bytesperline,
  592. pix_mp->plane_fmt[i].sizeimage);
  593. }
  594. return 0;
  595. }
  596. static int mtk_mdp_m2m_try_fmt_mplane(struct file *file, void *fh,
  597. struct v4l2_format *f)
  598. {
  599. struct mtk_mdp_ctx *ctx = fh_to_ctx(fh);
  600. if (!mtk_mdp_try_fmt_mplane(ctx, f))
  601. return -EINVAL;
  602. return 0;
  603. }
  604. static int mtk_mdp_m2m_s_fmt_mplane(struct file *file, void *fh,
  605. struct v4l2_format *f)
  606. {
  607. struct mtk_mdp_ctx *ctx = fh_to_ctx(fh);
  608. struct vb2_queue *vq;
  609. struct mtk_mdp_frame *frame;
  610. struct v4l2_pix_format_mplane *pix_mp;
  611. const struct mtk_mdp_fmt *fmt;
  612. int i;
  613. mtk_mdp_dbg(2, "[%d] type:%d", ctx->id, f->type);
  614. frame = mtk_mdp_ctx_get_frame(ctx, f->type);
  615. fmt = mtk_mdp_try_fmt_mplane(ctx, f);
  616. if (!fmt) {
  617. mtk_mdp_err("[%d] try_fmt failed, type:%d", ctx->id, f->type);
  618. return -EINVAL;
  619. }
  620. frame->fmt = fmt;
  621. vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
  622. if (vb2_is_streaming(vq)) {
  623. dev_info(&ctx->mdp_dev->pdev->dev, "queue %d busy", f->type);
  624. return -EBUSY;
  625. }
  626. pix_mp = &f->fmt.pix_mp;
  627. for (i = 0; i < frame->fmt->num_planes; i++) {
  628. frame->payload[i] = pix_mp->plane_fmt[i].sizeimage;
  629. frame->pitch[i] = pix_mp->plane_fmt[i].bytesperline;
  630. }
  631. mtk_mdp_set_frame_size(frame, pix_mp->width, pix_mp->height);
  632. if (V4L2_TYPE_IS_OUTPUT(f->type)) {
  633. ctx->colorspace = pix_mp->colorspace;
  634. ctx->xfer_func = pix_mp->xfer_func;
  635. ctx->ycbcr_enc = pix_mp->ycbcr_enc;
  636. ctx->quant = pix_mp->quantization;
  637. }
  638. if (V4L2_TYPE_IS_OUTPUT(f->type))
  639. mtk_mdp_ctx_state_lock_set(ctx, MTK_MDP_SRC_FMT);
  640. else
  641. mtk_mdp_ctx_state_lock_set(ctx, MTK_MDP_DST_FMT);
  642. mtk_mdp_dbg(2, "[%d] type:%d, frame:%dx%d", ctx->id, f->type,
  643. frame->width, frame->height);
  644. return 0;
  645. }
  646. static int mtk_mdp_m2m_reqbufs(struct file *file, void *fh,
  647. struct v4l2_requestbuffers *reqbufs)
  648. {
  649. struct mtk_mdp_ctx *ctx = fh_to_ctx(fh);
  650. if (reqbufs->count == 0) {
  651. if (reqbufs->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
  652. mtk_mdp_ctx_state_lock_clear(ctx, MTK_MDP_SRC_FMT);
  653. else
  654. mtk_mdp_ctx_state_lock_clear(ctx, MTK_MDP_DST_FMT);
  655. }
  656. return v4l2_m2m_reqbufs(file, ctx->m2m_ctx, reqbufs);
  657. }
  658. static int mtk_mdp_m2m_streamon(struct file *file, void *fh,
  659. enum v4l2_buf_type type)
  660. {
  661. struct mtk_mdp_ctx *ctx = fh_to_ctx(fh);
  662. int ret;
  663. /* The source and target color format need to be set */
  664. if (V4L2_TYPE_IS_OUTPUT(type)) {
  665. if (!mtk_mdp_ctx_state_is_set(ctx, MTK_MDP_SRC_FMT))
  666. return -EINVAL;
  667. } else if (!mtk_mdp_ctx_state_is_set(ctx, MTK_MDP_DST_FMT)) {
  668. return -EINVAL;
  669. }
  670. if (!mtk_mdp_ctx_state_is_set(ctx, MTK_MDP_VPU_INIT)) {
  671. ret = mtk_mdp_vpu_init(&ctx->vpu);
  672. if (ret < 0) {
  673. dev_err(&ctx->mdp_dev->pdev->dev,
  674. "vpu init failed %d\n",
  675. ret);
  676. return -EINVAL;
  677. }
  678. mtk_mdp_ctx_state_lock_set(ctx, MTK_MDP_VPU_INIT);
  679. }
  680. return v4l2_m2m_streamon(file, ctx->m2m_ctx, type);
  681. }
  682. static inline bool mtk_mdp_is_target_compose(u32 target)
  683. {
  684. if (target == V4L2_SEL_TGT_COMPOSE_DEFAULT
  685. || target == V4L2_SEL_TGT_COMPOSE_BOUNDS
  686. || target == V4L2_SEL_TGT_COMPOSE)
  687. return true;
  688. return false;
  689. }
  690. static inline bool mtk_mdp_is_target_crop(u32 target)
  691. {
  692. if (target == V4L2_SEL_TGT_CROP_DEFAULT
  693. || target == V4L2_SEL_TGT_CROP_BOUNDS
  694. || target == V4L2_SEL_TGT_CROP)
  695. return true;
  696. return false;
  697. }
  698. static int mtk_mdp_m2m_g_selection(struct file *file, void *fh,
  699. struct v4l2_selection *s)
  700. {
  701. struct mtk_mdp_frame *frame;
  702. struct mtk_mdp_ctx *ctx = fh_to_ctx(fh);
  703. bool valid = false;
  704. if (s->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  705. if (mtk_mdp_is_target_compose(s->target))
  706. valid = true;
  707. } else if (s->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
  708. if (mtk_mdp_is_target_crop(s->target))
  709. valid = true;
  710. }
  711. if (!valid) {
  712. mtk_mdp_dbg(1, "[%d] invalid type:%d,%u", ctx->id, s->type,
  713. s->target);
  714. return -EINVAL;
  715. }
  716. frame = mtk_mdp_ctx_get_frame(ctx, s->type);
  717. switch (s->target) {
  718. case V4L2_SEL_TGT_COMPOSE_DEFAULT:
  719. case V4L2_SEL_TGT_COMPOSE_BOUNDS:
  720. case V4L2_SEL_TGT_CROP_BOUNDS:
  721. case V4L2_SEL_TGT_CROP_DEFAULT:
  722. s->r.left = 0;
  723. s->r.top = 0;
  724. s->r.width = frame->width;
  725. s->r.height = frame->height;
  726. return 0;
  727. case V4L2_SEL_TGT_COMPOSE:
  728. case V4L2_SEL_TGT_CROP:
  729. s->r.left = frame->crop.left;
  730. s->r.top = frame->crop.top;
  731. s->r.width = frame->crop.width;
  732. s->r.height = frame->crop.height;
  733. return 0;
  734. }
  735. return -EINVAL;
  736. }
  737. static int mtk_mdp_check_scaler_ratio(struct mtk_mdp_variant *var, int src_w,
  738. int src_h, int dst_w, int dst_h, int rot)
  739. {
  740. int tmp_w, tmp_h;
  741. if (rot == 90 || rot == 270) {
  742. tmp_w = dst_h;
  743. tmp_h = dst_w;
  744. } else {
  745. tmp_w = dst_w;
  746. tmp_h = dst_h;
  747. }
  748. if ((src_w / tmp_w) > var->h_scale_down_max ||
  749. (src_h / tmp_h) > var->v_scale_down_max ||
  750. (tmp_w / src_w) > var->h_scale_up_max ||
  751. (tmp_h / src_h) > var->v_scale_up_max)
  752. return -EINVAL;
  753. return 0;
  754. }
  755. static int mtk_mdp_m2m_s_selection(struct file *file, void *fh,
  756. struct v4l2_selection *s)
  757. {
  758. struct mtk_mdp_frame *frame;
  759. struct mtk_mdp_ctx *ctx = fh_to_ctx(fh);
  760. struct v4l2_rect new_r;
  761. struct mtk_mdp_variant *variant = ctx->mdp_dev->variant;
  762. int ret;
  763. bool valid = false;
  764. if (s->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  765. if (s->target == V4L2_SEL_TGT_COMPOSE)
  766. valid = true;
  767. } else if (s->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
  768. if (s->target == V4L2_SEL_TGT_CROP)
  769. valid = true;
  770. }
  771. if (!valid) {
  772. mtk_mdp_dbg(1, "[%d] invalid type:%d,%u", ctx->id, s->type,
  773. s->target);
  774. return -EINVAL;
  775. }
  776. new_r = s->r;
  777. ret = mtk_mdp_try_crop(ctx, s->type, &new_r);
  778. if (ret)
  779. return ret;
  780. if (mtk_mdp_is_target_crop(s->target))
  781. frame = &ctx->s_frame;
  782. else
  783. frame = &ctx->d_frame;
  784. /* Check to see if scaling ratio is within supported range */
  785. if (mtk_mdp_ctx_state_is_set(ctx, MTK_MDP_DST_FMT | MTK_MDP_SRC_FMT)) {
  786. if (V4L2_TYPE_IS_OUTPUT(s->type)) {
  787. ret = mtk_mdp_check_scaler_ratio(variant, new_r.width,
  788. new_r.height, ctx->d_frame.crop.width,
  789. ctx->d_frame.crop.height,
  790. ctx->ctrls.rotate->val);
  791. } else {
  792. ret = mtk_mdp_check_scaler_ratio(variant,
  793. ctx->s_frame.crop.width,
  794. ctx->s_frame.crop.height, new_r.width,
  795. new_r.height, ctx->ctrls.rotate->val);
  796. }
  797. if (ret) {
  798. dev_info(&ctx->mdp_dev->pdev->dev,
  799. "Out of scaler range");
  800. return -EINVAL;
  801. }
  802. }
  803. s->r = new_r;
  804. frame->crop = new_r;
  805. return 0;
  806. }
  807. static const struct v4l2_ioctl_ops mtk_mdp_m2m_ioctl_ops = {
  808. .vidioc_querycap = mtk_mdp_m2m_querycap,
  809. .vidioc_enum_fmt_vid_cap_mplane = mtk_mdp_m2m_enum_fmt_mplane_vid_cap,
  810. .vidioc_enum_fmt_vid_out_mplane = mtk_mdp_m2m_enum_fmt_mplane_vid_out,
  811. .vidioc_g_fmt_vid_cap_mplane = mtk_mdp_m2m_g_fmt_mplane,
  812. .vidioc_g_fmt_vid_out_mplane = mtk_mdp_m2m_g_fmt_mplane,
  813. .vidioc_try_fmt_vid_cap_mplane = mtk_mdp_m2m_try_fmt_mplane,
  814. .vidioc_try_fmt_vid_out_mplane = mtk_mdp_m2m_try_fmt_mplane,
  815. .vidioc_s_fmt_vid_cap_mplane = mtk_mdp_m2m_s_fmt_mplane,
  816. .vidioc_s_fmt_vid_out_mplane = mtk_mdp_m2m_s_fmt_mplane,
  817. .vidioc_reqbufs = mtk_mdp_m2m_reqbufs,
  818. .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
  819. .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
  820. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  821. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  822. .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
  823. .vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
  824. .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
  825. .vidioc_streamon = mtk_mdp_m2m_streamon,
  826. .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
  827. .vidioc_g_selection = mtk_mdp_m2m_g_selection,
  828. .vidioc_s_selection = mtk_mdp_m2m_s_selection
  829. };
  830. static int mtk_mdp_m2m_queue_init(void *priv, struct vb2_queue *src_vq,
  831. struct vb2_queue *dst_vq)
  832. {
  833. struct mtk_mdp_ctx *ctx = priv;
  834. int ret;
  835. memset(src_vq, 0, sizeof(*src_vq));
  836. src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
  837. src_vq->io_modes = VB2_MMAP | VB2_DMABUF;
  838. src_vq->drv_priv = ctx;
  839. src_vq->ops = &mtk_mdp_m2m_qops;
  840. src_vq->mem_ops = &vb2_dma_contig_memops;
  841. src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
  842. src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
  843. src_vq->dev = &ctx->mdp_dev->pdev->dev;
  844. ret = vb2_queue_init(src_vq);
  845. if (ret)
  846. return ret;
  847. memset(dst_vq, 0, sizeof(*dst_vq));
  848. dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
  849. dst_vq->io_modes = VB2_MMAP | VB2_DMABUF;
  850. dst_vq->drv_priv = ctx;
  851. dst_vq->ops = &mtk_mdp_m2m_qops;
  852. dst_vq->mem_ops = &vb2_dma_contig_memops;
  853. dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
  854. dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
  855. dst_vq->dev = &ctx->mdp_dev->pdev->dev;
  856. return vb2_queue_init(dst_vq);
  857. }
  858. static int mtk_mdp_s_ctrl(struct v4l2_ctrl *ctrl)
  859. {
  860. struct mtk_mdp_ctx *ctx = ctrl_to_ctx(ctrl);
  861. struct mtk_mdp_dev *mdp = ctx->mdp_dev;
  862. struct mtk_mdp_variant *variant = mdp->variant;
  863. u32 state = MTK_MDP_DST_FMT | MTK_MDP_SRC_FMT;
  864. int ret = 0;
  865. if (ctrl->flags & V4L2_CTRL_FLAG_INACTIVE)
  866. return 0;
  867. switch (ctrl->id) {
  868. case V4L2_CID_HFLIP:
  869. ctx->hflip = ctrl->val;
  870. break;
  871. case V4L2_CID_VFLIP:
  872. ctx->vflip = ctrl->val;
  873. break;
  874. case V4L2_CID_ROTATE:
  875. if (mtk_mdp_ctx_state_is_set(ctx, state)) {
  876. ret = mtk_mdp_check_scaler_ratio(variant,
  877. ctx->s_frame.crop.width,
  878. ctx->s_frame.crop.height,
  879. ctx->d_frame.crop.width,
  880. ctx->d_frame.crop.height,
  881. ctx->ctrls.rotate->val);
  882. if (ret)
  883. return -EINVAL;
  884. }
  885. ctx->rotation = ctrl->val;
  886. break;
  887. case V4L2_CID_ALPHA_COMPONENT:
  888. ctx->d_frame.alpha = ctrl->val;
  889. break;
  890. }
  891. return 0;
  892. }
  893. static const struct v4l2_ctrl_ops mtk_mdp_ctrl_ops = {
  894. .s_ctrl = mtk_mdp_s_ctrl,
  895. };
  896. static int mtk_mdp_ctrls_create(struct mtk_mdp_ctx *ctx)
  897. {
  898. v4l2_ctrl_handler_init(&ctx->ctrl_handler, MTK_MDP_MAX_CTRL_NUM);
  899. ctx->ctrls.rotate = v4l2_ctrl_new_std(&ctx->ctrl_handler,
  900. &mtk_mdp_ctrl_ops, V4L2_CID_ROTATE, 0, 270, 90, 0);
  901. ctx->ctrls.hflip = v4l2_ctrl_new_std(&ctx->ctrl_handler,
  902. &mtk_mdp_ctrl_ops,
  903. V4L2_CID_HFLIP,
  904. 0, 1, 1, 0);
  905. ctx->ctrls.vflip = v4l2_ctrl_new_std(&ctx->ctrl_handler,
  906. &mtk_mdp_ctrl_ops,
  907. V4L2_CID_VFLIP,
  908. 0, 1, 1, 0);
  909. ctx->ctrls.global_alpha = v4l2_ctrl_new_std(&ctx->ctrl_handler,
  910. &mtk_mdp_ctrl_ops,
  911. V4L2_CID_ALPHA_COMPONENT,
  912. 0, 255, 1, 0);
  913. ctx->ctrls_rdy = ctx->ctrl_handler.error == 0;
  914. if (ctx->ctrl_handler.error) {
  915. int err = ctx->ctrl_handler.error;
  916. v4l2_ctrl_handler_free(&ctx->ctrl_handler);
  917. dev_err(&ctx->mdp_dev->pdev->dev,
  918. "Failed to create control handlers\n");
  919. return err;
  920. }
  921. return 0;
  922. }
  923. static void mtk_mdp_set_default_params(struct mtk_mdp_ctx *ctx)
  924. {
  925. struct mtk_mdp_dev *mdp = ctx->mdp_dev;
  926. struct mtk_mdp_frame *frame;
  927. frame = mtk_mdp_ctx_get_frame(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
  928. frame->fmt = mtk_mdp_find_fmt_by_index(0,
  929. V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
  930. frame->width = mdp->variant->pix_min->org_w;
  931. frame->height = mdp->variant->pix_min->org_h;
  932. frame->payload[0] = frame->width * frame->height;
  933. frame->payload[1] = frame->payload[0] / 2;
  934. frame = mtk_mdp_ctx_get_frame(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
  935. frame->fmt = mtk_mdp_find_fmt_by_index(0,
  936. V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
  937. frame->width = mdp->variant->pix_min->target_rot_dis_w;
  938. frame->height = mdp->variant->pix_min->target_rot_dis_h;
  939. frame->payload[0] = frame->width * frame->height;
  940. frame->payload[1] = frame->payload[0] / 2;
  941. }
  942. static int mtk_mdp_m2m_open(struct file *file)
  943. {
  944. struct mtk_mdp_dev *mdp = video_drvdata(file);
  945. struct video_device *vfd = video_devdata(file);
  946. struct mtk_mdp_ctx *ctx = NULL;
  947. int ret;
  948. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  949. if (!ctx)
  950. return -ENOMEM;
  951. if (mutex_lock_interruptible(&mdp->lock)) {
  952. ret = -ERESTARTSYS;
  953. goto err_lock;
  954. }
  955. mutex_init(&ctx->slock);
  956. ctx->id = mdp->id_counter++;
  957. v4l2_fh_init(&ctx->fh, vfd);
  958. file->private_data = &ctx->fh;
  959. ret = mtk_mdp_ctrls_create(ctx);
  960. if (ret)
  961. goto error_ctrls;
  962. /* Use separate control handler per file handle */
  963. ctx->fh.ctrl_handler = &ctx->ctrl_handler;
  964. v4l2_fh_add(&ctx->fh);
  965. INIT_LIST_HEAD(&ctx->list);
  966. ctx->mdp_dev = mdp;
  967. mtk_mdp_set_default_params(ctx);
  968. INIT_WORK(&ctx->work, mtk_mdp_m2m_worker);
  969. ctx->m2m_ctx = v4l2_m2m_ctx_init(mdp->m2m_dev, ctx,
  970. mtk_mdp_m2m_queue_init);
  971. if (IS_ERR(ctx->m2m_ctx)) {
  972. dev_err(&mdp->pdev->dev, "Failed to initialize m2m context");
  973. ret = PTR_ERR(ctx->m2m_ctx);
  974. goto error_m2m_ctx;
  975. }
  976. ctx->fh.m2m_ctx = ctx->m2m_ctx;
  977. if (mdp->ctx_num++ == 0) {
  978. ret = vpu_load_firmware(mdp->vpu_dev);
  979. if (ret < 0) {
  980. dev_err(&mdp->pdev->dev,
  981. "vpu_load_firmware failed %d\n", ret);
  982. goto err_load_vpu;
  983. }
  984. ret = mtk_mdp_vpu_register(mdp->pdev);
  985. if (ret < 0) {
  986. dev_err(&mdp->pdev->dev,
  987. "mdp_vpu register failed %d\n", ret);
  988. goto err_load_vpu;
  989. }
  990. }
  991. list_add(&ctx->list, &mdp->ctx_list);
  992. mutex_unlock(&mdp->lock);
  993. mtk_mdp_dbg(0, "%s [%d]", dev_name(&mdp->pdev->dev), ctx->id);
  994. return 0;
  995. err_load_vpu:
  996. mdp->ctx_num--;
  997. v4l2_m2m_ctx_release(ctx->m2m_ctx);
  998. error_m2m_ctx:
  999. v4l2_ctrl_handler_free(&ctx->ctrl_handler);
  1000. error_ctrls:
  1001. v4l2_fh_del(&ctx->fh);
  1002. v4l2_fh_exit(&ctx->fh);
  1003. mutex_unlock(&mdp->lock);
  1004. err_lock:
  1005. kfree(ctx);
  1006. return ret;
  1007. }
  1008. static int mtk_mdp_m2m_release(struct file *file)
  1009. {
  1010. struct mtk_mdp_ctx *ctx = fh_to_ctx(file->private_data);
  1011. struct mtk_mdp_dev *mdp = ctx->mdp_dev;
  1012. flush_workqueue(mdp->job_wq);
  1013. mutex_lock(&mdp->lock);
  1014. v4l2_m2m_ctx_release(ctx->m2m_ctx);
  1015. v4l2_ctrl_handler_free(&ctx->ctrl_handler);
  1016. v4l2_fh_del(&ctx->fh);
  1017. v4l2_fh_exit(&ctx->fh);
  1018. mtk_mdp_vpu_deinit(&ctx->vpu);
  1019. mdp->ctx_num--;
  1020. list_del_init(&ctx->list);
  1021. mtk_mdp_dbg(0, "%s [%d]", dev_name(&mdp->pdev->dev), ctx->id);
  1022. mutex_unlock(&mdp->lock);
  1023. kfree(ctx);
  1024. return 0;
  1025. }
  1026. static const struct v4l2_file_operations mtk_mdp_m2m_fops = {
  1027. .owner = THIS_MODULE,
  1028. .open = mtk_mdp_m2m_open,
  1029. .release = mtk_mdp_m2m_release,
  1030. .poll = v4l2_m2m_fop_poll,
  1031. .unlocked_ioctl = video_ioctl2,
  1032. .mmap = v4l2_m2m_fop_mmap,
  1033. };
  1034. static struct v4l2_m2m_ops mtk_mdp_m2m_ops = {
  1035. .device_run = mtk_mdp_m2m_device_run,
  1036. .job_abort = mtk_mdp_m2m_job_abort,
  1037. };
  1038. int mtk_mdp_register_m2m_device(struct mtk_mdp_dev *mdp)
  1039. {
  1040. struct device *dev = &mdp->pdev->dev;
  1041. int ret;
  1042. mdp->variant = &mtk_mdp_default_variant;
  1043. mdp->vdev = video_device_alloc();
  1044. if (!mdp->vdev) {
  1045. dev_err(dev, "failed to allocate video device\n");
  1046. ret = -ENOMEM;
  1047. goto err_video_alloc;
  1048. }
  1049. mdp->vdev->device_caps = V4L2_CAP_VIDEO_M2M_MPLANE | V4L2_CAP_STREAMING;
  1050. mdp->vdev->fops = &mtk_mdp_m2m_fops;
  1051. mdp->vdev->ioctl_ops = &mtk_mdp_m2m_ioctl_ops;
  1052. mdp->vdev->release = video_device_release;
  1053. mdp->vdev->lock = &mdp->lock;
  1054. mdp->vdev->vfl_dir = VFL_DIR_M2M;
  1055. mdp->vdev->v4l2_dev = &mdp->v4l2_dev;
  1056. snprintf(mdp->vdev->name, sizeof(mdp->vdev->name), "%s:m2m",
  1057. MTK_MDP_MODULE_NAME);
  1058. video_set_drvdata(mdp->vdev, mdp);
  1059. mdp->m2m_dev = v4l2_m2m_init(&mtk_mdp_m2m_ops);
  1060. if (IS_ERR(mdp->m2m_dev)) {
  1061. dev_err(dev, "failed to initialize v4l2-m2m device\n");
  1062. ret = PTR_ERR(mdp->m2m_dev);
  1063. goto err_m2m_init;
  1064. }
  1065. ret = video_register_device(mdp->vdev, VFL_TYPE_GRABBER, 2);
  1066. if (ret) {
  1067. dev_err(dev, "failed to register video device\n");
  1068. goto err_vdev_register;
  1069. }
  1070. v4l2_info(&mdp->v4l2_dev, "driver registered as /dev/video%d",
  1071. mdp->vdev->num);
  1072. return 0;
  1073. err_vdev_register:
  1074. v4l2_m2m_release(mdp->m2m_dev);
  1075. err_m2m_init:
  1076. video_device_release(mdp->vdev);
  1077. err_video_alloc:
  1078. return ret;
  1079. }
  1080. void mtk_mdp_unregister_m2m_device(struct mtk_mdp_dev *mdp)
  1081. {
  1082. video_unregister_device(mdp->vdev);
  1083. v4l2_m2m_release(mdp->m2m_dev);
  1084. }