vdec.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267
  1. /*
  2. * Copyright (c) 2012-2016, The Linux Foundation. All rights reserved.
  3. * Copyright (C) 2017 Linaro Ltd.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 and
  7. * only version 2 as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. */
  15. #include <linux/clk.h>
  16. #include <linux/module.h>
  17. #include <linux/mod_devicetable.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/pm_runtime.h>
  20. #include <linux/slab.h>
  21. #include <media/v4l2-ioctl.h>
  22. #include <media/v4l2-event.h>
  23. #include <media/v4l2-ctrls.h>
  24. #include <media/v4l2-mem2mem.h>
  25. #include <media/videobuf2-dma-sg.h>
  26. #include "hfi_venus_io.h"
  27. #include "hfi_parser.h"
  28. #include "core.h"
  29. #include "helpers.h"
  30. #include "vdec.h"
  31. /*
  32. * Three resons to keep MPLANE formats (despite that the number of planes
  33. * currently is one):
  34. * - the MPLANE formats allow only one plane to be used
  35. * - the downstream driver use MPLANE formats too
  36. * - future firmware versions could add support for >1 planes
  37. */
  38. static const struct venus_format vdec_formats[] = {
  39. {
  40. .pixfmt = V4L2_PIX_FMT_NV12,
  41. .num_planes = 1,
  42. .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
  43. }, {
  44. .pixfmt = V4L2_PIX_FMT_MPEG4,
  45. .num_planes = 1,
  46. .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
  47. }, {
  48. .pixfmt = V4L2_PIX_FMT_MPEG2,
  49. .num_planes = 1,
  50. .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
  51. }, {
  52. .pixfmt = V4L2_PIX_FMT_H263,
  53. .num_planes = 1,
  54. .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
  55. }, {
  56. .pixfmt = V4L2_PIX_FMT_VC1_ANNEX_G,
  57. .num_planes = 1,
  58. .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
  59. }, {
  60. .pixfmt = V4L2_PIX_FMT_VC1_ANNEX_L,
  61. .num_planes = 1,
  62. .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
  63. }, {
  64. .pixfmt = V4L2_PIX_FMT_H264,
  65. .num_planes = 1,
  66. .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
  67. }, {
  68. .pixfmt = V4L2_PIX_FMT_VP8,
  69. .num_planes = 1,
  70. .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
  71. }, {
  72. .pixfmt = V4L2_PIX_FMT_VP9,
  73. .num_planes = 1,
  74. .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
  75. }, {
  76. .pixfmt = V4L2_PIX_FMT_XVID,
  77. .num_planes = 1,
  78. .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
  79. }, {
  80. .pixfmt = V4L2_PIX_FMT_HEVC,
  81. .num_planes = 1,
  82. .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
  83. },
  84. };
  85. static const struct venus_format *
  86. find_format(struct venus_inst *inst, u32 pixfmt, u32 type)
  87. {
  88. const struct venus_format *fmt = vdec_formats;
  89. unsigned int size = ARRAY_SIZE(vdec_formats);
  90. unsigned int i;
  91. for (i = 0; i < size; i++) {
  92. if (fmt[i].pixfmt == pixfmt)
  93. break;
  94. }
  95. if (i == size || fmt[i].type != type)
  96. return NULL;
  97. if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE &&
  98. !venus_helper_check_codec(inst, fmt[i].pixfmt))
  99. return NULL;
  100. return &fmt[i];
  101. }
  102. static const struct venus_format *
  103. find_format_by_index(struct venus_inst *inst, unsigned int index, u32 type)
  104. {
  105. const struct venus_format *fmt = vdec_formats;
  106. unsigned int size = ARRAY_SIZE(vdec_formats);
  107. unsigned int i, k = 0;
  108. if (index > size)
  109. return NULL;
  110. for (i = 0; i < size; i++) {
  111. bool valid;
  112. if (fmt[i].type != type)
  113. continue;
  114. valid = type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE ||
  115. venus_helper_check_codec(inst, fmt[i].pixfmt);
  116. if (k == index && valid)
  117. break;
  118. if (valid)
  119. k++;
  120. }
  121. if (i == size)
  122. return NULL;
  123. return &fmt[i];
  124. }
  125. static const struct venus_format *
  126. vdec_try_fmt_common(struct venus_inst *inst, struct v4l2_format *f)
  127. {
  128. struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp;
  129. struct v4l2_plane_pix_format *pfmt = pixmp->plane_fmt;
  130. const struct venus_format *fmt;
  131. memset(pfmt[0].reserved, 0, sizeof(pfmt[0].reserved));
  132. memset(pixmp->reserved, 0, sizeof(pixmp->reserved));
  133. fmt = find_format(inst, pixmp->pixelformat, f->type);
  134. if (!fmt) {
  135. if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
  136. pixmp->pixelformat = V4L2_PIX_FMT_NV12;
  137. else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
  138. pixmp->pixelformat = V4L2_PIX_FMT_H264;
  139. else
  140. return NULL;
  141. fmt = find_format(inst, pixmp->pixelformat, f->type);
  142. }
  143. pixmp->width = clamp(pixmp->width, frame_width_min(inst),
  144. frame_width_max(inst));
  145. pixmp->height = clamp(pixmp->height, frame_height_min(inst),
  146. frame_height_max(inst));
  147. if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
  148. pixmp->height = ALIGN(pixmp->height, 32);
  149. if (pixmp->field == V4L2_FIELD_ANY)
  150. pixmp->field = V4L2_FIELD_NONE;
  151. pixmp->num_planes = fmt->num_planes;
  152. pixmp->flags = 0;
  153. pfmt[0].sizeimage = venus_helper_get_framesz(pixmp->pixelformat,
  154. pixmp->width,
  155. pixmp->height);
  156. if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
  157. pfmt[0].bytesperline = ALIGN(pixmp->width, 128);
  158. else
  159. pfmt[0].bytesperline = 0;
  160. return fmt;
  161. }
  162. static int vdec_try_fmt(struct file *file, void *fh, struct v4l2_format *f)
  163. {
  164. struct venus_inst *inst = to_inst(file);
  165. vdec_try_fmt_common(inst, f);
  166. return 0;
  167. }
  168. static int vdec_g_fmt(struct file *file, void *fh, struct v4l2_format *f)
  169. {
  170. struct venus_inst *inst = to_inst(file);
  171. const struct venus_format *fmt = NULL;
  172. struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp;
  173. if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
  174. fmt = inst->fmt_cap;
  175. else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
  176. fmt = inst->fmt_out;
  177. if (inst->reconfig) {
  178. struct v4l2_format format = {};
  179. inst->out_width = inst->reconfig_width;
  180. inst->out_height = inst->reconfig_height;
  181. inst->reconfig = false;
  182. format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
  183. format.fmt.pix_mp.pixelformat = inst->fmt_cap->pixfmt;
  184. format.fmt.pix_mp.width = inst->out_width;
  185. format.fmt.pix_mp.height = inst->out_height;
  186. vdec_try_fmt_common(inst, &format);
  187. inst->width = format.fmt.pix_mp.width;
  188. inst->height = format.fmt.pix_mp.height;
  189. }
  190. pixmp->pixelformat = fmt->pixfmt;
  191. if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
  192. pixmp->width = inst->width;
  193. pixmp->height = inst->height;
  194. pixmp->colorspace = inst->colorspace;
  195. pixmp->ycbcr_enc = inst->ycbcr_enc;
  196. pixmp->quantization = inst->quantization;
  197. pixmp->xfer_func = inst->xfer_func;
  198. } else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  199. pixmp->width = inst->out_width;
  200. pixmp->height = inst->out_height;
  201. }
  202. vdec_try_fmt_common(inst, f);
  203. return 0;
  204. }
  205. static int vdec_s_fmt(struct file *file, void *fh, struct v4l2_format *f)
  206. {
  207. struct venus_inst *inst = to_inst(file);
  208. struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp;
  209. struct v4l2_pix_format_mplane orig_pixmp;
  210. const struct venus_format *fmt;
  211. struct v4l2_format format;
  212. u32 pixfmt_out = 0, pixfmt_cap = 0;
  213. orig_pixmp = *pixmp;
  214. fmt = vdec_try_fmt_common(inst, f);
  215. if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  216. pixfmt_out = pixmp->pixelformat;
  217. pixfmt_cap = inst->fmt_cap->pixfmt;
  218. } else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
  219. pixfmt_cap = pixmp->pixelformat;
  220. pixfmt_out = inst->fmt_out->pixfmt;
  221. }
  222. memset(&format, 0, sizeof(format));
  223. format.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
  224. format.fmt.pix_mp.pixelformat = pixfmt_out;
  225. format.fmt.pix_mp.width = orig_pixmp.width;
  226. format.fmt.pix_mp.height = orig_pixmp.height;
  227. vdec_try_fmt_common(inst, &format);
  228. if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  229. inst->out_width = format.fmt.pix_mp.width;
  230. inst->out_height = format.fmt.pix_mp.height;
  231. inst->colorspace = pixmp->colorspace;
  232. inst->ycbcr_enc = pixmp->ycbcr_enc;
  233. inst->quantization = pixmp->quantization;
  234. inst->xfer_func = pixmp->xfer_func;
  235. }
  236. memset(&format, 0, sizeof(format));
  237. format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
  238. format.fmt.pix_mp.pixelformat = pixfmt_cap;
  239. format.fmt.pix_mp.width = orig_pixmp.width;
  240. format.fmt.pix_mp.height = orig_pixmp.height;
  241. vdec_try_fmt_common(inst, &format);
  242. inst->width = format.fmt.pix_mp.width;
  243. inst->height = format.fmt.pix_mp.height;
  244. if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
  245. inst->fmt_out = fmt;
  246. else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
  247. inst->fmt_cap = fmt;
  248. return 0;
  249. }
  250. static int
  251. vdec_g_selection(struct file *file, void *fh, struct v4l2_selection *s)
  252. {
  253. struct venus_inst *inst = to_inst(file);
  254. if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
  255. s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
  256. return -EINVAL;
  257. switch (s->target) {
  258. case V4L2_SEL_TGT_CROP_BOUNDS:
  259. case V4L2_SEL_TGT_CROP_DEFAULT:
  260. case V4L2_SEL_TGT_CROP:
  261. if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
  262. return -EINVAL;
  263. s->r.width = inst->out_width;
  264. s->r.height = inst->out_height;
  265. break;
  266. case V4L2_SEL_TGT_COMPOSE_BOUNDS:
  267. case V4L2_SEL_TGT_COMPOSE_PADDED:
  268. if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  269. return -EINVAL;
  270. s->r.width = inst->width;
  271. s->r.height = inst->height;
  272. break;
  273. case V4L2_SEL_TGT_COMPOSE_DEFAULT:
  274. case V4L2_SEL_TGT_COMPOSE:
  275. if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  276. return -EINVAL;
  277. s->r.width = inst->out_width;
  278. s->r.height = inst->out_height;
  279. break;
  280. default:
  281. return -EINVAL;
  282. }
  283. s->r.top = 0;
  284. s->r.left = 0;
  285. return 0;
  286. }
  287. static int
  288. vdec_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
  289. {
  290. strlcpy(cap->driver, "qcom-venus", sizeof(cap->driver));
  291. strlcpy(cap->card, "Qualcomm Venus video decoder", sizeof(cap->card));
  292. strlcpy(cap->bus_info, "platform:qcom-venus", sizeof(cap->bus_info));
  293. return 0;
  294. }
  295. static int vdec_enum_fmt(struct file *file, void *fh, struct v4l2_fmtdesc *f)
  296. {
  297. struct venus_inst *inst = to_inst(file);
  298. const struct venus_format *fmt;
  299. memset(f->reserved, 0, sizeof(f->reserved));
  300. fmt = find_format_by_index(inst, f->index, f->type);
  301. if (!fmt)
  302. return -EINVAL;
  303. f->pixelformat = fmt->pixfmt;
  304. return 0;
  305. }
  306. static int vdec_s_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
  307. {
  308. struct venus_inst *inst = to_inst(file);
  309. struct v4l2_captureparm *cap = &a->parm.capture;
  310. struct v4l2_fract *timeperframe = &cap->timeperframe;
  311. u64 us_per_frame, fps;
  312. if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
  313. a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
  314. return -EINVAL;
  315. memset(cap->reserved, 0, sizeof(cap->reserved));
  316. if (!timeperframe->denominator)
  317. timeperframe->denominator = inst->timeperframe.denominator;
  318. if (!timeperframe->numerator)
  319. timeperframe->numerator = inst->timeperframe.numerator;
  320. cap->readbuffers = 0;
  321. cap->extendedmode = 0;
  322. cap->capability = V4L2_CAP_TIMEPERFRAME;
  323. us_per_frame = timeperframe->numerator * (u64)USEC_PER_SEC;
  324. do_div(us_per_frame, timeperframe->denominator);
  325. if (!us_per_frame)
  326. return -EINVAL;
  327. fps = (u64)USEC_PER_SEC;
  328. do_div(fps, us_per_frame);
  329. inst->fps = fps;
  330. inst->timeperframe = *timeperframe;
  331. return 0;
  332. }
  333. static int vdec_enum_framesizes(struct file *file, void *fh,
  334. struct v4l2_frmsizeenum *fsize)
  335. {
  336. struct venus_inst *inst = to_inst(file);
  337. const struct venus_format *fmt;
  338. fmt = find_format(inst, fsize->pixel_format,
  339. V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
  340. if (!fmt) {
  341. fmt = find_format(inst, fsize->pixel_format,
  342. V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
  343. if (!fmt)
  344. return -EINVAL;
  345. }
  346. if (fsize->index)
  347. return -EINVAL;
  348. fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
  349. fsize->stepwise.min_width = frame_width_min(inst);
  350. fsize->stepwise.max_width = frame_width_max(inst);
  351. fsize->stepwise.step_width = frame_width_step(inst);
  352. fsize->stepwise.min_height = frame_height_min(inst);
  353. fsize->stepwise.max_height = frame_height_max(inst);
  354. fsize->stepwise.step_height = frame_height_step(inst);
  355. return 0;
  356. }
  357. static int vdec_subscribe_event(struct v4l2_fh *fh,
  358. const struct v4l2_event_subscription *sub)
  359. {
  360. switch (sub->type) {
  361. case V4L2_EVENT_EOS:
  362. return v4l2_event_subscribe(fh, sub, 2, NULL);
  363. case V4L2_EVENT_SOURCE_CHANGE:
  364. return v4l2_src_change_event_subscribe(fh, sub);
  365. case V4L2_EVENT_CTRL:
  366. return v4l2_ctrl_subscribe_event(fh, sub);
  367. default:
  368. return -EINVAL;
  369. }
  370. }
  371. static int
  372. vdec_try_decoder_cmd(struct file *file, void *fh, struct v4l2_decoder_cmd *cmd)
  373. {
  374. switch (cmd->cmd) {
  375. case V4L2_DEC_CMD_STOP:
  376. if (cmd->flags & V4L2_DEC_CMD_STOP_TO_BLACK)
  377. return -EINVAL;
  378. break;
  379. default:
  380. return -EINVAL;
  381. }
  382. return 0;
  383. }
  384. static int
  385. vdec_decoder_cmd(struct file *file, void *fh, struct v4l2_decoder_cmd *cmd)
  386. {
  387. struct venus_inst *inst = to_inst(file);
  388. struct hfi_frame_data fdata = {0};
  389. int ret;
  390. ret = vdec_try_decoder_cmd(file, fh, cmd);
  391. if (ret)
  392. return ret;
  393. mutex_lock(&inst->lock);
  394. /*
  395. * Implement V4L2_DEC_CMD_STOP by enqueue an empty buffer on decoder
  396. * input to signal EOS.
  397. */
  398. if (!(inst->streamon_out & inst->streamon_cap))
  399. goto unlock;
  400. fdata.buffer_type = HFI_BUFFER_INPUT;
  401. fdata.flags |= HFI_BUFFERFLAG_EOS;
  402. fdata.device_addr = 0xdeadbeef;
  403. ret = hfi_session_process_buf(inst, &fdata);
  404. unlock:
  405. mutex_unlock(&inst->lock);
  406. return ret;
  407. }
  408. static const struct v4l2_ioctl_ops vdec_ioctl_ops = {
  409. .vidioc_querycap = vdec_querycap,
  410. .vidioc_enum_fmt_vid_cap_mplane = vdec_enum_fmt,
  411. .vidioc_enum_fmt_vid_out_mplane = vdec_enum_fmt,
  412. .vidioc_s_fmt_vid_cap_mplane = vdec_s_fmt,
  413. .vidioc_s_fmt_vid_out_mplane = vdec_s_fmt,
  414. .vidioc_g_fmt_vid_cap_mplane = vdec_g_fmt,
  415. .vidioc_g_fmt_vid_out_mplane = vdec_g_fmt,
  416. .vidioc_try_fmt_vid_cap_mplane = vdec_try_fmt,
  417. .vidioc_try_fmt_vid_out_mplane = vdec_try_fmt,
  418. .vidioc_g_selection = vdec_g_selection,
  419. .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
  420. .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
  421. .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
  422. .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
  423. .vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
  424. .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
  425. .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
  426. .vidioc_streamon = v4l2_m2m_ioctl_streamon,
  427. .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
  428. .vidioc_s_parm = vdec_s_parm,
  429. .vidioc_enum_framesizes = vdec_enum_framesizes,
  430. .vidioc_subscribe_event = vdec_subscribe_event,
  431. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  432. .vidioc_try_decoder_cmd = vdec_try_decoder_cmd,
  433. .vidioc_decoder_cmd = vdec_decoder_cmd,
  434. };
  435. static int vdec_set_properties(struct venus_inst *inst)
  436. {
  437. struct vdec_controls *ctr = &inst->controls.dec;
  438. struct hfi_enable en = { .enable = 1 };
  439. u32 ptype;
  440. int ret;
  441. if (ctr->post_loop_deb_mode) {
  442. ptype = HFI_PROPERTY_CONFIG_VDEC_POST_LOOP_DEBLOCKER;
  443. ret = hfi_session_set_property(inst, ptype, &en);
  444. if (ret)
  445. return ret;
  446. }
  447. return 0;
  448. }
  449. #define is_ubwc_fmt(fmt) (!!((fmt) & HFI_COLOR_FORMAT_UBWC_BASE))
  450. static int vdec_output_conf(struct venus_inst *inst)
  451. {
  452. struct venus_core *core = inst->core;
  453. struct hfi_enable en = { .enable = 1 };
  454. u32 width = inst->out_width;
  455. u32 height = inst->out_height;
  456. u32 out_fmt, out2_fmt;
  457. bool ubwc = false;
  458. u32 ptype;
  459. int ret;
  460. ret = venus_helper_set_work_mode(inst, VIDC_WORK_MODE_2);
  461. if (ret)
  462. return ret;
  463. ret = venus_helper_set_core_usage(inst, VIDC_CORE_ID_1);
  464. if (ret)
  465. return ret;
  466. if (core->res->hfi_version == HFI_VERSION_1XX) {
  467. ptype = HFI_PROPERTY_PARAM_VDEC_CONTINUE_DATA_TRANSFER;
  468. ret = hfi_session_set_property(inst, ptype, &en);
  469. if (ret)
  470. return ret;
  471. }
  472. /* Force searching UBWC formats for bigger then HD resolutions */
  473. if (width > 1920 && height > ALIGN(1080, 32))
  474. ubwc = true;
  475. /* For Venus v4 UBWC format is mandatory */
  476. if (IS_V4(core))
  477. ubwc = true;
  478. ret = venus_helper_get_out_fmts(inst, inst->fmt_cap->pixfmt, &out_fmt,
  479. &out2_fmt, ubwc);
  480. if (ret)
  481. return ret;
  482. inst->output_buf_size =
  483. venus_helper_get_framesz_raw(out_fmt, width, height);
  484. inst->output2_buf_size =
  485. venus_helper_get_framesz_raw(out2_fmt, width, height);
  486. if (is_ubwc_fmt(out_fmt)) {
  487. inst->opb_buftype = HFI_BUFFER_OUTPUT2;
  488. inst->opb_fmt = out2_fmt;
  489. inst->dpb_buftype = HFI_BUFFER_OUTPUT;
  490. inst->dpb_fmt = out_fmt;
  491. } else if (is_ubwc_fmt(out2_fmt)) {
  492. inst->opb_buftype = HFI_BUFFER_OUTPUT;
  493. inst->opb_fmt = out_fmt;
  494. inst->dpb_buftype = HFI_BUFFER_OUTPUT2;
  495. inst->dpb_fmt = out2_fmt;
  496. } else {
  497. inst->opb_buftype = HFI_BUFFER_OUTPUT;
  498. inst->opb_fmt = out_fmt;
  499. inst->dpb_buftype = 0;
  500. inst->dpb_fmt = 0;
  501. }
  502. ret = venus_helper_set_raw_format(inst, inst->opb_fmt,
  503. inst->opb_buftype);
  504. if (ret)
  505. return ret;
  506. if (inst->dpb_fmt) {
  507. ret = venus_helper_set_multistream(inst, false, true);
  508. if (ret)
  509. return ret;
  510. ret = venus_helper_set_raw_format(inst, inst->dpb_fmt,
  511. inst->dpb_buftype);
  512. if (ret)
  513. return ret;
  514. ret = venus_helper_set_output_resolution(inst, width, height,
  515. HFI_BUFFER_OUTPUT2);
  516. if (ret)
  517. return ret;
  518. }
  519. if (IS_V3(core) || IS_V4(core)) {
  520. if (inst->output2_buf_size) {
  521. ret = venus_helper_set_bufsize(inst,
  522. inst->output2_buf_size,
  523. HFI_BUFFER_OUTPUT2);
  524. if (ret)
  525. return ret;
  526. }
  527. if (inst->output_buf_size) {
  528. ret = venus_helper_set_bufsize(inst,
  529. inst->output_buf_size,
  530. HFI_BUFFER_OUTPUT);
  531. if (ret)
  532. return ret;
  533. }
  534. }
  535. ret = venus_helper_set_dyn_bufmode(inst);
  536. if (ret)
  537. return ret;
  538. return 0;
  539. }
  540. static int vdec_init_session(struct venus_inst *inst)
  541. {
  542. int ret;
  543. ret = hfi_session_init(inst, inst->fmt_out->pixfmt);
  544. if (ret)
  545. return ret;
  546. ret = venus_helper_set_input_resolution(inst, inst->out_width,
  547. inst->out_height);
  548. if (ret)
  549. goto deinit;
  550. ret = venus_helper_set_color_format(inst, inst->fmt_cap->pixfmt);
  551. if (ret)
  552. goto deinit;
  553. return 0;
  554. deinit:
  555. hfi_session_deinit(inst);
  556. return ret;
  557. }
  558. static int vdec_num_buffers(struct venus_inst *inst, unsigned int *in_num,
  559. unsigned int *out_num)
  560. {
  561. enum hfi_version ver = inst->core->res->hfi_version;
  562. struct hfi_buffer_requirements bufreq;
  563. int ret;
  564. *in_num = *out_num = 0;
  565. ret = vdec_init_session(inst);
  566. if (ret)
  567. return ret;
  568. ret = venus_helper_get_bufreq(inst, HFI_BUFFER_INPUT, &bufreq);
  569. if (ret)
  570. goto deinit;
  571. *in_num = HFI_BUFREQ_COUNT_MIN(&bufreq, ver);
  572. ret = venus_helper_get_bufreq(inst, HFI_BUFFER_OUTPUT, &bufreq);
  573. if (ret)
  574. goto deinit;
  575. *out_num = HFI_BUFREQ_COUNT_MIN(&bufreq, ver);
  576. deinit:
  577. hfi_session_deinit(inst);
  578. return ret;
  579. }
  580. static int vdec_queue_setup(struct vb2_queue *q,
  581. unsigned int *num_buffers, unsigned int *num_planes,
  582. unsigned int sizes[], struct device *alloc_devs[])
  583. {
  584. struct venus_inst *inst = vb2_get_drv_priv(q);
  585. unsigned int in_num, out_num;
  586. int ret = 0;
  587. if (*num_planes) {
  588. unsigned int output_buf_size = venus_helper_get_opb_size(inst);
  589. if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE &&
  590. *num_planes != inst->fmt_out->num_planes)
  591. return -EINVAL;
  592. if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
  593. *num_planes != inst->fmt_cap->num_planes)
  594. return -EINVAL;
  595. if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE &&
  596. sizes[0] < inst->input_buf_size)
  597. return -EINVAL;
  598. if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
  599. sizes[0] < output_buf_size)
  600. return -EINVAL;
  601. return 0;
  602. }
  603. ret = vdec_num_buffers(inst, &in_num, &out_num);
  604. if (ret)
  605. return ret;
  606. switch (q->type) {
  607. case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
  608. *num_planes = inst->fmt_out->num_planes;
  609. sizes[0] = venus_helper_get_framesz(inst->fmt_out->pixfmt,
  610. inst->out_width,
  611. inst->out_height);
  612. inst->input_buf_size = sizes[0];
  613. *num_buffers = max(*num_buffers, in_num);
  614. inst->num_input_bufs = *num_buffers;
  615. inst->num_output_bufs = out_num;
  616. break;
  617. case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
  618. *num_planes = inst->fmt_cap->num_planes;
  619. sizes[0] = venus_helper_get_framesz(inst->fmt_cap->pixfmt,
  620. inst->width,
  621. inst->height);
  622. inst->output_buf_size = sizes[0];
  623. *num_buffers = max(*num_buffers, out_num);
  624. inst->num_output_bufs = *num_buffers;
  625. break;
  626. default:
  627. ret = -EINVAL;
  628. break;
  629. }
  630. return ret;
  631. }
  632. static int vdec_verify_conf(struct venus_inst *inst)
  633. {
  634. enum hfi_version ver = inst->core->res->hfi_version;
  635. struct hfi_buffer_requirements bufreq;
  636. int ret;
  637. if (!inst->num_input_bufs || !inst->num_output_bufs)
  638. return -EINVAL;
  639. ret = venus_helper_get_bufreq(inst, HFI_BUFFER_OUTPUT, &bufreq);
  640. if (ret)
  641. return ret;
  642. if (inst->num_output_bufs < bufreq.count_actual ||
  643. inst->num_output_bufs < HFI_BUFREQ_COUNT_MIN(&bufreq, ver))
  644. return -EINVAL;
  645. ret = venus_helper_get_bufreq(inst, HFI_BUFFER_INPUT, &bufreq);
  646. if (ret)
  647. return ret;
  648. if (inst->num_input_bufs < HFI_BUFREQ_COUNT_MIN(&bufreq, ver))
  649. return -EINVAL;
  650. return 0;
  651. }
  652. static int vdec_start_streaming(struct vb2_queue *q, unsigned int count)
  653. {
  654. struct venus_inst *inst = vb2_get_drv_priv(q);
  655. int ret;
  656. mutex_lock(&inst->lock);
  657. if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
  658. inst->streamon_out = 1;
  659. else
  660. inst->streamon_cap = 1;
  661. if (!(inst->streamon_out & inst->streamon_cap)) {
  662. mutex_unlock(&inst->lock);
  663. return 0;
  664. }
  665. venus_helper_init_instance(inst);
  666. inst->reconfig = false;
  667. inst->sequence_cap = 0;
  668. inst->sequence_out = 0;
  669. ret = vdec_init_session(inst);
  670. if (ret)
  671. goto bufs_done;
  672. ret = vdec_set_properties(inst);
  673. if (ret)
  674. goto deinit_sess;
  675. ret = vdec_output_conf(inst);
  676. if (ret)
  677. goto deinit_sess;
  678. ret = vdec_verify_conf(inst);
  679. if (ret)
  680. goto deinit_sess;
  681. ret = venus_helper_set_num_bufs(inst, inst->num_input_bufs,
  682. VB2_MAX_FRAME, VB2_MAX_FRAME);
  683. if (ret)
  684. goto deinit_sess;
  685. ret = venus_helper_alloc_dpb_bufs(inst);
  686. if (ret)
  687. goto deinit_sess;
  688. ret = venus_helper_vb2_start_streaming(inst);
  689. if (ret)
  690. goto deinit_sess;
  691. mutex_unlock(&inst->lock);
  692. return 0;
  693. deinit_sess:
  694. hfi_session_deinit(inst);
  695. bufs_done:
  696. venus_helper_buffers_done(inst, VB2_BUF_STATE_QUEUED);
  697. if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
  698. inst->streamon_out = 0;
  699. else
  700. inst->streamon_cap = 0;
  701. mutex_unlock(&inst->lock);
  702. return ret;
  703. }
  704. static const struct vb2_ops vdec_vb2_ops = {
  705. .queue_setup = vdec_queue_setup,
  706. .buf_init = venus_helper_vb2_buf_init,
  707. .buf_prepare = venus_helper_vb2_buf_prepare,
  708. .start_streaming = vdec_start_streaming,
  709. .stop_streaming = venus_helper_vb2_stop_streaming,
  710. .buf_queue = venus_helper_vb2_buf_queue,
  711. };
  712. static void vdec_buf_done(struct venus_inst *inst, unsigned int buf_type,
  713. u32 tag, u32 bytesused, u32 data_offset, u32 flags,
  714. u32 hfi_flags, u64 timestamp_us)
  715. {
  716. enum vb2_buffer_state state = VB2_BUF_STATE_DONE;
  717. struct vb2_v4l2_buffer *vbuf;
  718. struct vb2_buffer *vb;
  719. unsigned int type;
  720. if (buf_type == HFI_BUFFER_INPUT)
  721. type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
  722. else
  723. type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
  724. vbuf = venus_helper_find_buf(inst, type, tag);
  725. if (!vbuf)
  726. return;
  727. vbuf->flags = flags;
  728. vbuf->field = V4L2_FIELD_NONE;
  729. if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
  730. unsigned int opb_sz = venus_helper_get_opb_size(inst);
  731. vb = &vbuf->vb2_buf;
  732. vb->planes[0].bytesused =
  733. max_t(unsigned int, opb_sz, bytesused);
  734. vb->planes[0].data_offset = data_offset;
  735. vb->timestamp = timestamp_us * NSEC_PER_USEC;
  736. vbuf->sequence = inst->sequence_cap++;
  737. if (vbuf->flags & V4L2_BUF_FLAG_LAST) {
  738. const struct v4l2_event ev = { .type = V4L2_EVENT_EOS };
  739. v4l2_event_queue_fh(&inst->fh, &ev);
  740. }
  741. } else {
  742. vbuf->sequence = inst->sequence_out++;
  743. }
  744. if (hfi_flags & HFI_BUFFERFLAG_READONLY)
  745. venus_helper_acquire_buf_ref(vbuf);
  746. if (hfi_flags & HFI_BUFFERFLAG_DATACORRUPT)
  747. state = VB2_BUF_STATE_ERROR;
  748. v4l2_m2m_buf_done(vbuf, state);
  749. }
  750. static void vdec_event_notify(struct venus_inst *inst, u32 event,
  751. struct hfi_event_data *data)
  752. {
  753. struct venus_core *core = inst->core;
  754. struct device *dev = core->dev_dec;
  755. static const struct v4l2_event ev = {
  756. .type = V4L2_EVENT_SOURCE_CHANGE,
  757. .u.src_change.changes = V4L2_EVENT_SRC_CH_RESOLUTION };
  758. switch (event) {
  759. case EVT_SESSION_ERROR:
  760. inst->session_error = true;
  761. dev_err(dev, "dec: event session error %x\n", inst->error);
  762. break;
  763. case EVT_SYS_EVENT_CHANGE:
  764. switch (data->event_type) {
  765. case HFI_EVENT_DATA_SEQUENCE_CHANGED_SUFFICIENT_BUF_RESOURCES:
  766. hfi_session_continue(inst);
  767. dev_dbg(dev, "event sufficient resources\n");
  768. break;
  769. case HFI_EVENT_DATA_SEQUENCE_CHANGED_INSUFFICIENT_BUF_RESOURCES:
  770. inst->reconfig_height = data->height;
  771. inst->reconfig_width = data->width;
  772. inst->reconfig = true;
  773. v4l2_event_queue_fh(&inst->fh, &ev);
  774. dev_dbg(dev, "event not sufficient resources (%ux%u)\n",
  775. data->width, data->height);
  776. break;
  777. case HFI_EVENT_RELEASE_BUFFER_REFERENCE:
  778. venus_helper_release_buf_ref(inst, data->tag);
  779. break;
  780. default:
  781. break;
  782. }
  783. break;
  784. default:
  785. break;
  786. }
  787. }
  788. static const struct hfi_inst_ops vdec_hfi_ops = {
  789. .buf_done = vdec_buf_done,
  790. .event_notify = vdec_event_notify,
  791. };
  792. static void vdec_inst_init(struct venus_inst *inst)
  793. {
  794. inst->fmt_out = &vdec_formats[6];
  795. inst->fmt_cap = &vdec_formats[0];
  796. inst->width = 1280;
  797. inst->height = ALIGN(720, 32);
  798. inst->out_width = 1280;
  799. inst->out_height = 720;
  800. inst->fps = 30;
  801. inst->timeperframe.numerator = 1;
  802. inst->timeperframe.denominator = 30;
  803. inst->hfi_codec = HFI_VIDEO_CODEC_H264;
  804. }
  805. static const struct v4l2_m2m_ops vdec_m2m_ops = {
  806. .device_run = venus_helper_m2m_device_run,
  807. .job_abort = venus_helper_m2m_job_abort,
  808. };
  809. static int m2m_queue_init(void *priv, struct vb2_queue *src_vq,
  810. struct vb2_queue *dst_vq)
  811. {
  812. struct venus_inst *inst = priv;
  813. int ret;
  814. src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
  815. src_vq->io_modes = VB2_MMAP | VB2_DMABUF;
  816. src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
  817. src_vq->ops = &vdec_vb2_ops;
  818. src_vq->mem_ops = &vb2_dma_sg_memops;
  819. src_vq->drv_priv = inst;
  820. src_vq->buf_struct_size = sizeof(struct venus_buffer);
  821. src_vq->allow_zero_bytesused = 1;
  822. src_vq->min_buffers_needed = 1;
  823. src_vq->dev = inst->core->dev;
  824. ret = vb2_queue_init(src_vq);
  825. if (ret)
  826. return ret;
  827. dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
  828. dst_vq->io_modes = VB2_MMAP | VB2_DMABUF;
  829. dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
  830. dst_vq->ops = &vdec_vb2_ops;
  831. dst_vq->mem_ops = &vb2_dma_sg_memops;
  832. dst_vq->drv_priv = inst;
  833. dst_vq->buf_struct_size = sizeof(struct venus_buffer);
  834. dst_vq->allow_zero_bytesused = 1;
  835. dst_vq->min_buffers_needed = 1;
  836. dst_vq->dev = inst->core->dev;
  837. ret = vb2_queue_init(dst_vq);
  838. if (ret) {
  839. vb2_queue_release(src_vq);
  840. return ret;
  841. }
  842. return 0;
  843. }
  844. static int vdec_open(struct file *file)
  845. {
  846. struct venus_core *core = video_drvdata(file);
  847. struct venus_inst *inst;
  848. int ret;
  849. inst = kzalloc(sizeof(*inst), GFP_KERNEL);
  850. if (!inst)
  851. return -ENOMEM;
  852. INIT_LIST_HEAD(&inst->dpbbufs);
  853. INIT_LIST_HEAD(&inst->registeredbufs);
  854. INIT_LIST_HEAD(&inst->internalbufs);
  855. INIT_LIST_HEAD(&inst->list);
  856. mutex_init(&inst->lock);
  857. inst->core = core;
  858. inst->session_type = VIDC_SESSION_TYPE_DEC;
  859. inst->num_output_bufs = 1;
  860. venus_helper_init_instance(inst);
  861. ret = pm_runtime_get_sync(core->dev_dec);
  862. if (ret < 0)
  863. goto err_free_inst;
  864. ret = vdec_ctrl_init(inst);
  865. if (ret)
  866. goto err_put_sync;
  867. ret = hfi_session_create(inst, &vdec_hfi_ops);
  868. if (ret)
  869. goto err_ctrl_deinit;
  870. vdec_inst_init(inst);
  871. /*
  872. * create m2m device for every instance, the m2m context scheduling
  873. * is made by firmware side so we do not need to care about.
  874. */
  875. inst->m2m_dev = v4l2_m2m_init(&vdec_m2m_ops);
  876. if (IS_ERR(inst->m2m_dev)) {
  877. ret = PTR_ERR(inst->m2m_dev);
  878. goto err_session_destroy;
  879. }
  880. inst->m2m_ctx = v4l2_m2m_ctx_init(inst->m2m_dev, inst, m2m_queue_init);
  881. if (IS_ERR(inst->m2m_ctx)) {
  882. ret = PTR_ERR(inst->m2m_ctx);
  883. goto err_m2m_release;
  884. }
  885. v4l2_fh_init(&inst->fh, core->vdev_dec);
  886. inst->fh.ctrl_handler = &inst->ctrl_handler;
  887. v4l2_fh_add(&inst->fh);
  888. inst->fh.m2m_ctx = inst->m2m_ctx;
  889. file->private_data = &inst->fh;
  890. return 0;
  891. err_m2m_release:
  892. v4l2_m2m_release(inst->m2m_dev);
  893. err_session_destroy:
  894. hfi_session_destroy(inst);
  895. err_ctrl_deinit:
  896. vdec_ctrl_deinit(inst);
  897. err_put_sync:
  898. pm_runtime_put_sync(core->dev_dec);
  899. err_free_inst:
  900. kfree(inst);
  901. return ret;
  902. }
  903. static int vdec_close(struct file *file)
  904. {
  905. struct venus_inst *inst = to_inst(file);
  906. v4l2_m2m_ctx_release(inst->m2m_ctx);
  907. v4l2_m2m_release(inst->m2m_dev);
  908. vdec_ctrl_deinit(inst);
  909. hfi_session_destroy(inst);
  910. mutex_destroy(&inst->lock);
  911. v4l2_fh_del(&inst->fh);
  912. v4l2_fh_exit(&inst->fh);
  913. pm_runtime_put_sync(inst->core->dev_dec);
  914. kfree(inst);
  915. return 0;
  916. }
  917. static const struct v4l2_file_operations vdec_fops = {
  918. .owner = THIS_MODULE,
  919. .open = vdec_open,
  920. .release = vdec_close,
  921. .unlocked_ioctl = video_ioctl2,
  922. .poll = v4l2_m2m_fop_poll,
  923. .mmap = v4l2_m2m_fop_mmap,
  924. #ifdef CONFIG_COMPAT
  925. .compat_ioctl32 = v4l2_compat_ioctl32,
  926. #endif
  927. };
  928. static int vdec_probe(struct platform_device *pdev)
  929. {
  930. struct device *dev = &pdev->dev;
  931. struct video_device *vdev;
  932. struct venus_core *core;
  933. int ret;
  934. if (!dev->parent)
  935. return -EPROBE_DEFER;
  936. core = dev_get_drvdata(dev->parent);
  937. if (!core)
  938. return -EPROBE_DEFER;
  939. if (IS_V3(core) || IS_V4(core)) {
  940. core->core0_clk = devm_clk_get(dev, "core");
  941. if (IS_ERR(core->core0_clk))
  942. return PTR_ERR(core->core0_clk);
  943. }
  944. if (IS_V4(core)) {
  945. core->core0_bus_clk = devm_clk_get(dev, "bus");
  946. if (IS_ERR(core->core0_bus_clk))
  947. return PTR_ERR(core->core0_bus_clk);
  948. }
  949. platform_set_drvdata(pdev, core);
  950. vdev = video_device_alloc();
  951. if (!vdev)
  952. return -ENOMEM;
  953. strlcpy(vdev->name, "qcom-venus-decoder", sizeof(vdev->name));
  954. vdev->release = video_device_release;
  955. vdev->fops = &vdec_fops;
  956. vdev->ioctl_ops = &vdec_ioctl_ops;
  957. vdev->vfl_dir = VFL_DIR_M2M;
  958. vdev->v4l2_dev = &core->v4l2_dev;
  959. vdev->device_caps = V4L2_CAP_VIDEO_M2M_MPLANE | V4L2_CAP_STREAMING;
  960. ret = video_register_device(vdev, VFL_TYPE_GRABBER, -1);
  961. if (ret)
  962. goto err_vdev_release;
  963. core->vdev_dec = vdev;
  964. core->dev_dec = dev;
  965. video_set_drvdata(vdev, core);
  966. pm_runtime_enable(dev);
  967. return 0;
  968. err_vdev_release:
  969. video_device_release(vdev);
  970. return ret;
  971. }
  972. static int vdec_remove(struct platform_device *pdev)
  973. {
  974. struct venus_core *core = dev_get_drvdata(pdev->dev.parent);
  975. video_unregister_device(core->vdev_dec);
  976. pm_runtime_disable(core->dev_dec);
  977. return 0;
  978. }
  979. static __maybe_unused int vdec_runtime_suspend(struct device *dev)
  980. {
  981. struct venus_core *core = dev_get_drvdata(dev);
  982. int ret;
  983. if (IS_V1(core))
  984. return 0;
  985. ret = venus_helper_power_enable(core, VIDC_SESSION_TYPE_DEC, true);
  986. if (ret)
  987. return ret;
  988. if (IS_V4(core))
  989. clk_disable_unprepare(core->core0_bus_clk);
  990. clk_disable_unprepare(core->core0_clk);
  991. return venus_helper_power_enable(core, VIDC_SESSION_TYPE_DEC, false);
  992. }
  993. static __maybe_unused int vdec_runtime_resume(struct device *dev)
  994. {
  995. struct venus_core *core = dev_get_drvdata(dev);
  996. int ret;
  997. if (IS_V1(core))
  998. return 0;
  999. ret = venus_helper_power_enable(core, VIDC_SESSION_TYPE_DEC, true);
  1000. if (ret)
  1001. return ret;
  1002. ret = clk_prepare_enable(core->core0_clk);
  1003. if (ret)
  1004. goto err_power_disable;
  1005. if (IS_V4(core))
  1006. ret = clk_prepare_enable(core->core0_bus_clk);
  1007. if (ret)
  1008. goto err_unprepare_core0;
  1009. return venus_helper_power_enable(core, VIDC_SESSION_TYPE_DEC, false);
  1010. err_unprepare_core0:
  1011. clk_disable_unprepare(core->core0_clk);
  1012. err_power_disable:
  1013. venus_helper_power_enable(core, VIDC_SESSION_TYPE_DEC, false);
  1014. return ret;
  1015. }
  1016. static const struct dev_pm_ops vdec_pm_ops = {
  1017. SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
  1018. pm_runtime_force_resume)
  1019. SET_RUNTIME_PM_OPS(vdec_runtime_suspend, vdec_runtime_resume, NULL)
  1020. };
  1021. static const struct of_device_id vdec_dt_match[] = {
  1022. { .compatible = "venus-decoder" },
  1023. { }
  1024. };
  1025. MODULE_DEVICE_TABLE(of, vdec_dt_match);
  1026. static struct platform_driver qcom_venus_dec_driver = {
  1027. .probe = vdec_probe,
  1028. .remove = vdec_remove,
  1029. .driver = {
  1030. .name = "qcom-venus-decoder",
  1031. .of_match_table = vdec_dt_match,
  1032. .pm = &vdec_pm_ops,
  1033. },
  1034. };
  1035. module_platform_driver(qcom_venus_dec_driver);
  1036. MODULE_ALIAS("platform:qcom-venus-decoder");
  1037. MODULE_DESCRIPTION("Qualcomm Venus video decoder driver");
  1038. MODULE_LICENSE("GPL v2");