hva-v4l2.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) STMicroelectronics SA 2015
  4. * Authors: Yannick Fertre <yannick.fertre@st.com>
  5. * Hugues Fruchet <hugues.fruchet@st.com>
  6. */
  7. #include <linux/module.h>
  8. #include <linux/mod_devicetable.h>
  9. #include <linux/platform_device.h>
  10. #include <linux/slab.h>
  11. #include <media/v4l2-event.h>
  12. #include <media/v4l2-ioctl.h>
  13. #include <media/videobuf2-dma-contig.h>
  14. #include "hva.h"
  15. #include "hva-hw.h"
  16. #define MIN_FRAMES 1
  17. #define MIN_STREAMS 1
  18. #define HVA_MIN_WIDTH 32
  19. #define HVA_MAX_WIDTH 1920
  20. #define HVA_MIN_HEIGHT 32
  21. #define HVA_MAX_HEIGHT 1920
  22. /* HVA requires a 16x16 pixels alignment for frames */
  23. #define HVA_WIDTH_ALIGNMENT 16
  24. #define HVA_HEIGHT_ALIGNMENT 16
  25. #define HVA_DEFAULT_WIDTH HVA_MIN_WIDTH
  26. #define HVA_DEFAULT_HEIGHT HVA_MIN_HEIGHT
  27. #define HVA_DEFAULT_FRAME_NUM 1
  28. #define HVA_DEFAULT_FRAME_DEN 30
  29. #define to_type_str(type) (type == V4L2_BUF_TYPE_VIDEO_OUTPUT ? \
  30. "frame" : "stream")
  31. #define fh_to_ctx(f) (container_of(f, struct hva_ctx, fh))
  32. /* registry of available encoders */
  33. static const struct hva_enc *hva_encoders[] = {
  34. &nv12h264enc,
  35. &nv21h264enc,
  36. };
  37. static inline int frame_size(u32 w, u32 h, u32 fmt)
  38. {
  39. switch (fmt) {
  40. case V4L2_PIX_FMT_NV12:
  41. case V4L2_PIX_FMT_NV21:
  42. return (w * h * 3) / 2;
  43. default:
  44. return 0;
  45. }
  46. }
  47. static inline int frame_stride(u32 w, u32 fmt)
  48. {
  49. switch (fmt) {
  50. case V4L2_PIX_FMT_NV12:
  51. case V4L2_PIX_FMT_NV21:
  52. return w;
  53. default:
  54. return 0;
  55. }
  56. }
  57. static inline int frame_alignment(u32 fmt)
  58. {
  59. switch (fmt) {
  60. case V4L2_PIX_FMT_NV12:
  61. case V4L2_PIX_FMT_NV21:
  62. /* multiple of 2 */
  63. return 2;
  64. default:
  65. return 1;
  66. }
  67. }
  68. static inline int estimated_stream_size(u32 w, u32 h)
  69. {
  70. /*
  71. * HVA only encodes in YUV420 format, whatever the frame format.
  72. * A compression ratio of 2 is assumed: thus, the maximum size
  73. * of a stream is estimated to ((width x height x 3 / 2) / 2)
  74. */
  75. return (w * h * 3) / 4;
  76. }
  77. static void set_default_params(struct hva_ctx *ctx)
  78. {
  79. struct hva_frameinfo *frameinfo = &ctx->frameinfo;
  80. struct hva_streaminfo *streaminfo = &ctx->streaminfo;
  81. frameinfo->pixelformat = V4L2_PIX_FMT_NV12;
  82. frameinfo->width = HVA_DEFAULT_WIDTH;
  83. frameinfo->height = HVA_DEFAULT_HEIGHT;
  84. frameinfo->aligned_width = ALIGN(frameinfo->width,
  85. HVA_WIDTH_ALIGNMENT);
  86. frameinfo->aligned_height = ALIGN(frameinfo->height,
  87. HVA_HEIGHT_ALIGNMENT);
  88. frameinfo->size = frame_size(frameinfo->aligned_width,
  89. frameinfo->aligned_height,
  90. frameinfo->pixelformat);
  91. streaminfo->streamformat = V4L2_PIX_FMT_H264;
  92. streaminfo->width = HVA_DEFAULT_WIDTH;
  93. streaminfo->height = HVA_DEFAULT_HEIGHT;
  94. ctx->colorspace = V4L2_COLORSPACE_REC709;
  95. ctx->xfer_func = V4L2_XFER_FUNC_DEFAULT;
  96. ctx->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
  97. ctx->quantization = V4L2_QUANTIZATION_DEFAULT;
  98. ctx->max_stream_size = estimated_stream_size(streaminfo->width,
  99. streaminfo->height);
  100. }
  101. static const struct hva_enc *hva_find_encoder(struct hva_ctx *ctx,
  102. u32 pixelformat,
  103. u32 streamformat)
  104. {
  105. struct hva_dev *hva = ctx_to_hdev(ctx);
  106. const struct hva_enc *enc;
  107. unsigned int i;
  108. for (i = 0; i < hva->nb_of_encoders; i++) {
  109. enc = hva->encoders[i];
  110. if ((enc->pixelformat == pixelformat) &&
  111. (enc->streamformat == streamformat))
  112. return enc;
  113. }
  114. return NULL;
  115. }
  116. static void register_format(u32 format, u32 formats[], u32 *nb_of_formats)
  117. {
  118. u32 i;
  119. bool found = false;
  120. for (i = 0; i < *nb_of_formats; i++) {
  121. if (format == formats[i]) {
  122. found = true;
  123. break;
  124. }
  125. }
  126. if (!found)
  127. formats[(*nb_of_formats)++] = format;
  128. }
  129. static void register_formats(struct hva_dev *hva)
  130. {
  131. unsigned int i;
  132. for (i = 0; i < hva->nb_of_encoders; i++) {
  133. register_format(hva->encoders[i]->pixelformat,
  134. hva->pixelformats,
  135. &hva->nb_of_pixelformats);
  136. register_format(hva->encoders[i]->streamformat,
  137. hva->streamformats,
  138. &hva->nb_of_streamformats);
  139. }
  140. }
  141. static void register_encoders(struct hva_dev *hva)
  142. {
  143. struct device *dev = hva_to_dev(hva);
  144. unsigned int i;
  145. for (i = 0; i < ARRAY_SIZE(hva_encoders); i++) {
  146. if (hva->nb_of_encoders >= HVA_MAX_ENCODERS) {
  147. dev_dbg(dev,
  148. "%s failed to register %s encoder (%d maximum reached)\n",
  149. HVA_PREFIX, hva_encoders[i]->name,
  150. HVA_MAX_ENCODERS);
  151. return;
  152. }
  153. hva->encoders[hva->nb_of_encoders++] = hva_encoders[i];
  154. dev_info(dev, "%s %s encoder registered\n", HVA_PREFIX,
  155. hva_encoders[i]->name);
  156. }
  157. }
  158. static int hva_open_encoder(struct hva_ctx *ctx, u32 streamformat,
  159. u32 pixelformat, struct hva_enc **penc)
  160. {
  161. struct hva_dev *hva = ctx_to_hdev(ctx);
  162. struct device *dev = ctx_to_dev(ctx);
  163. struct hva_enc *enc;
  164. int ret;
  165. /* find an encoder which can deal with these formats */
  166. enc = (struct hva_enc *)hva_find_encoder(ctx, pixelformat,
  167. streamformat);
  168. if (!enc) {
  169. dev_err(dev, "%s no encoder found matching %4.4s => %4.4s\n",
  170. ctx->name, (char *)&pixelformat, (char *)&streamformat);
  171. return -EINVAL;
  172. }
  173. dev_dbg(dev, "%s one encoder matching %4.4s => %4.4s\n",
  174. ctx->name, (char *)&pixelformat, (char *)&streamformat);
  175. /* update instance name */
  176. snprintf(ctx->name, sizeof(ctx->name), "[%3d:%4.4s]",
  177. hva->instance_id, (char *)&streamformat);
  178. /* open encoder instance */
  179. ret = enc->open(ctx);
  180. if (ret) {
  181. dev_err(dev, "%s failed to open encoder instance (%d)\n",
  182. ctx->name, ret);
  183. return ret;
  184. }
  185. dev_dbg(dev, "%s %s encoder opened\n", ctx->name, enc->name);
  186. *penc = enc;
  187. return ret;
  188. }
  189. static void hva_dbg_summary(struct hva_ctx *ctx)
  190. {
  191. struct device *dev = ctx_to_dev(ctx);
  192. struct hva_streaminfo *stream = &ctx->streaminfo;
  193. struct hva_frameinfo *frame = &ctx->frameinfo;
  194. if (!(ctx->flags & HVA_FLAG_STREAMINFO))
  195. return;
  196. dev_dbg(dev, "%s %4.4s %dx%d > %4.4s %dx%d %s %s: %d frames encoded, %d system errors, %d encoding errors, %d frame errors\n",
  197. ctx->name,
  198. (char *)&frame->pixelformat,
  199. frame->aligned_width, frame->aligned_height,
  200. (char *)&stream->streamformat,
  201. stream->width, stream->height,
  202. stream->profile, stream->level,
  203. ctx->encoded_frames,
  204. ctx->sys_errors,
  205. ctx->encode_errors,
  206. ctx->frame_errors);
  207. }
  208. /*
  209. * V4L2 ioctl operations
  210. */
  211. static int hva_querycap(struct file *file, void *priv,
  212. struct v4l2_capability *cap)
  213. {
  214. struct hva_ctx *ctx = fh_to_ctx(file->private_data);
  215. struct hva_dev *hva = ctx_to_hdev(ctx);
  216. strlcpy(cap->driver, HVA_NAME, sizeof(cap->driver));
  217. strlcpy(cap->card, hva->vdev->name, sizeof(cap->card));
  218. snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
  219. hva->pdev->name);
  220. return 0;
  221. }
  222. static int hva_enum_fmt_stream(struct file *file, void *priv,
  223. struct v4l2_fmtdesc *f)
  224. {
  225. struct hva_ctx *ctx = fh_to_ctx(file->private_data);
  226. struct hva_dev *hva = ctx_to_hdev(ctx);
  227. if (unlikely(f->index >= hva->nb_of_streamformats))
  228. return -EINVAL;
  229. f->pixelformat = hva->streamformats[f->index];
  230. return 0;
  231. }
  232. static int hva_enum_fmt_frame(struct file *file, void *priv,
  233. struct v4l2_fmtdesc *f)
  234. {
  235. struct hva_ctx *ctx = fh_to_ctx(file->private_data);
  236. struct hva_dev *hva = ctx_to_hdev(ctx);
  237. if (unlikely(f->index >= hva->nb_of_pixelformats))
  238. return -EINVAL;
  239. f->pixelformat = hva->pixelformats[f->index];
  240. return 0;
  241. }
  242. static int hva_g_fmt_stream(struct file *file, void *fh, struct v4l2_format *f)
  243. {
  244. struct hva_ctx *ctx = fh_to_ctx(file->private_data);
  245. struct hva_streaminfo *streaminfo = &ctx->streaminfo;
  246. f->fmt.pix.width = streaminfo->width;
  247. f->fmt.pix.height = streaminfo->height;
  248. f->fmt.pix.field = V4L2_FIELD_NONE;
  249. f->fmt.pix.colorspace = ctx->colorspace;
  250. f->fmt.pix.xfer_func = ctx->xfer_func;
  251. f->fmt.pix.ycbcr_enc = ctx->ycbcr_enc;
  252. f->fmt.pix.quantization = ctx->quantization;
  253. f->fmt.pix.pixelformat = streaminfo->streamformat;
  254. f->fmt.pix.bytesperline = 0;
  255. f->fmt.pix.sizeimage = ctx->max_stream_size;
  256. return 0;
  257. }
  258. static int hva_g_fmt_frame(struct file *file, void *fh, struct v4l2_format *f)
  259. {
  260. struct hva_ctx *ctx = fh_to_ctx(file->private_data);
  261. struct hva_frameinfo *frameinfo = &ctx->frameinfo;
  262. f->fmt.pix.width = frameinfo->width;
  263. f->fmt.pix.height = frameinfo->height;
  264. f->fmt.pix.field = V4L2_FIELD_NONE;
  265. f->fmt.pix.colorspace = ctx->colorspace;
  266. f->fmt.pix.xfer_func = ctx->xfer_func;
  267. f->fmt.pix.ycbcr_enc = ctx->ycbcr_enc;
  268. f->fmt.pix.quantization = ctx->quantization;
  269. f->fmt.pix.pixelformat = frameinfo->pixelformat;
  270. f->fmt.pix.bytesperline = frame_stride(frameinfo->aligned_width,
  271. frameinfo->pixelformat);
  272. f->fmt.pix.sizeimage = frameinfo->size;
  273. return 0;
  274. }
  275. static int hva_try_fmt_stream(struct file *file, void *priv,
  276. struct v4l2_format *f)
  277. {
  278. struct hva_ctx *ctx = fh_to_ctx(file->private_data);
  279. struct device *dev = ctx_to_dev(ctx);
  280. struct v4l2_pix_format *pix = &f->fmt.pix;
  281. u32 streamformat = pix->pixelformat;
  282. const struct hva_enc *enc;
  283. u32 width, height;
  284. u32 stream_size;
  285. enc = hva_find_encoder(ctx, ctx->frameinfo.pixelformat, streamformat);
  286. if (!enc) {
  287. dev_dbg(dev,
  288. "%s V4L2 TRY_FMT (CAPTURE): unsupported format %.4s\n",
  289. ctx->name, (char *)&pix->pixelformat);
  290. return -EINVAL;
  291. }
  292. width = pix->width;
  293. height = pix->height;
  294. if (ctx->flags & HVA_FLAG_FRAMEINFO) {
  295. /*
  296. * if the frame resolution is already fixed, only allow the
  297. * same stream resolution
  298. */
  299. pix->width = ctx->frameinfo.width;
  300. pix->height = ctx->frameinfo.height;
  301. if ((pix->width != width) || (pix->height != height))
  302. dev_dbg(dev,
  303. "%s V4L2 TRY_FMT (CAPTURE): resolution updated %dx%d -> %dx%d to fit frame resolution\n",
  304. ctx->name, width, height,
  305. pix->width, pix->height);
  306. } else {
  307. /* adjust width & height */
  308. v4l_bound_align_image(&pix->width,
  309. HVA_MIN_WIDTH, enc->max_width,
  310. 0,
  311. &pix->height,
  312. HVA_MIN_HEIGHT, enc->max_height,
  313. 0,
  314. 0);
  315. if ((pix->width != width) || (pix->height != height))
  316. dev_dbg(dev,
  317. "%s V4L2 TRY_FMT (CAPTURE): resolution updated %dx%d -> %dx%d to fit min/max/alignment\n",
  318. ctx->name, width, height,
  319. pix->width, pix->height);
  320. }
  321. stream_size = estimated_stream_size(pix->width, pix->height);
  322. if (pix->sizeimage < stream_size)
  323. pix->sizeimage = stream_size;
  324. pix->bytesperline = 0;
  325. pix->colorspace = ctx->colorspace;
  326. pix->xfer_func = ctx->xfer_func;
  327. pix->ycbcr_enc = ctx->ycbcr_enc;
  328. pix->quantization = ctx->quantization;
  329. pix->field = V4L2_FIELD_NONE;
  330. return 0;
  331. }
  332. static int hva_try_fmt_frame(struct file *file, void *priv,
  333. struct v4l2_format *f)
  334. {
  335. struct hva_ctx *ctx = fh_to_ctx(file->private_data);
  336. struct device *dev = ctx_to_dev(ctx);
  337. struct v4l2_pix_format *pix = &f->fmt.pix;
  338. u32 pixelformat = pix->pixelformat;
  339. const struct hva_enc *enc;
  340. u32 width, height;
  341. enc = hva_find_encoder(ctx, pixelformat, ctx->streaminfo.streamformat);
  342. if (!enc) {
  343. dev_dbg(dev,
  344. "%s V4L2 TRY_FMT (OUTPUT): unsupported format %.4s\n",
  345. ctx->name, (char *)&pixelformat);
  346. return -EINVAL;
  347. }
  348. /* adjust width & height */
  349. width = pix->width;
  350. height = pix->height;
  351. v4l_bound_align_image(&pix->width,
  352. HVA_MIN_WIDTH, HVA_MAX_WIDTH,
  353. frame_alignment(pixelformat) - 1,
  354. &pix->height,
  355. HVA_MIN_HEIGHT, HVA_MAX_HEIGHT,
  356. frame_alignment(pixelformat) - 1,
  357. 0);
  358. if ((pix->width != width) || (pix->height != height))
  359. dev_dbg(dev,
  360. "%s V4L2 TRY_FMT (OUTPUT): resolution updated %dx%d -> %dx%d to fit min/max/alignment\n",
  361. ctx->name, width, height, pix->width, pix->height);
  362. width = ALIGN(pix->width, HVA_WIDTH_ALIGNMENT);
  363. height = ALIGN(pix->height, HVA_HEIGHT_ALIGNMENT);
  364. if (!pix->colorspace) {
  365. pix->colorspace = V4L2_COLORSPACE_REC709;
  366. pix->xfer_func = V4L2_XFER_FUNC_DEFAULT;
  367. pix->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
  368. pix->quantization = V4L2_QUANTIZATION_DEFAULT;
  369. }
  370. pix->bytesperline = frame_stride(width, pixelformat);
  371. pix->sizeimage = frame_size(width, height, pixelformat);
  372. pix->field = V4L2_FIELD_NONE;
  373. return 0;
  374. }
  375. static int hva_s_fmt_stream(struct file *file, void *fh, struct v4l2_format *f)
  376. {
  377. struct hva_ctx *ctx = fh_to_ctx(file->private_data);
  378. struct device *dev = ctx_to_dev(ctx);
  379. struct vb2_queue *vq;
  380. int ret;
  381. ret = hva_try_fmt_stream(file, fh, f);
  382. if (ret) {
  383. dev_dbg(dev, "%s V4L2 S_FMT (CAPTURE): unsupported format %.4s\n",
  384. ctx->name, (char *)&f->fmt.pix.pixelformat);
  385. return ret;
  386. }
  387. vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
  388. if (vb2_is_streaming(vq)) {
  389. dev_dbg(dev, "%s V4L2 S_FMT (CAPTURE): queue busy\n",
  390. ctx->name);
  391. return -EBUSY;
  392. }
  393. ctx->max_stream_size = f->fmt.pix.sizeimage;
  394. ctx->streaminfo.width = f->fmt.pix.width;
  395. ctx->streaminfo.height = f->fmt.pix.height;
  396. ctx->streaminfo.streamformat = f->fmt.pix.pixelformat;
  397. ctx->flags |= HVA_FLAG_STREAMINFO;
  398. return 0;
  399. }
  400. static int hva_s_fmt_frame(struct file *file, void *fh, struct v4l2_format *f)
  401. {
  402. struct hva_ctx *ctx = fh_to_ctx(file->private_data);
  403. struct device *dev = ctx_to_dev(ctx);
  404. struct v4l2_pix_format *pix = &f->fmt.pix;
  405. struct vb2_queue *vq;
  406. int ret;
  407. ret = hva_try_fmt_frame(file, fh, f);
  408. if (ret) {
  409. dev_dbg(dev, "%s V4L2 S_FMT (OUTPUT): unsupported format %.4s\n",
  410. ctx->name, (char *)&pix->pixelformat);
  411. return ret;
  412. }
  413. vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
  414. if (vb2_is_streaming(vq)) {
  415. dev_dbg(dev, "%s V4L2 S_FMT (OUTPUT): queue busy\n", ctx->name);
  416. return -EBUSY;
  417. }
  418. ctx->colorspace = pix->colorspace;
  419. ctx->xfer_func = pix->xfer_func;
  420. ctx->ycbcr_enc = pix->ycbcr_enc;
  421. ctx->quantization = pix->quantization;
  422. ctx->frameinfo.aligned_width = ALIGN(pix->width, HVA_WIDTH_ALIGNMENT);
  423. ctx->frameinfo.aligned_height = ALIGN(pix->height,
  424. HVA_HEIGHT_ALIGNMENT);
  425. ctx->frameinfo.size = pix->sizeimage;
  426. ctx->frameinfo.pixelformat = pix->pixelformat;
  427. ctx->frameinfo.width = pix->width;
  428. ctx->frameinfo.height = pix->height;
  429. ctx->flags |= HVA_FLAG_FRAMEINFO;
  430. return 0;
  431. }
  432. static int hva_g_parm(struct file *file, void *fh, struct v4l2_streamparm *sp)
  433. {
  434. struct hva_ctx *ctx = fh_to_ctx(file->private_data);
  435. struct v4l2_fract *time_per_frame = &ctx->ctrls.time_per_frame;
  436. if (sp->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
  437. return -EINVAL;
  438. sp->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
  439. sp->parm.output.timeperframe.numerator = time_per_frame->numerator;
  440. sp->parm.output.timeperframe.denominator =
  441. time_per_frame->denominator;
  442. return 0;
  443. }
  444. static int hva_s_parm(struct file *file, void *fh, struct v4l2_streamparm *sp)
  445. {
  446. struct hva_ctx *ctx = fh_to_ctx(file->private_data);
  447. struct v4l2_fract *time_per_frame = &ctx->ctrls.time_per_frame;
  448. if (sp->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
  449. return -EINVAL;
  450. if (!sp->parm.output.timeperframe.numerator ||
  451. !sp->parm.output.timeperframe.denominator)
  452. return hva_g_parm(file, fh, sp);
  453. sp->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
  454. time_per_frame->numerator = sp->parm.output.timeperframe.numerator;
  455. time_per_frame->denominator =
  456. sp->parm.output.timeperframe.denominator;
  457. return 0;
  458. }
  459. static int hva_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
  460. {
  461. struct hva_ctx *ctx = fh_to_ctx(file->private_data);
  462. struct device *dev = ctx_to_dev(ctx);
  463. if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  464. /*
  465. * depending on the targeted compressed video format, the
  466. * capture buffer might contain headers (e.g. H.264 SPS/PPS)
  467. * filled in by the driver client; the size of these data is
  468. * copied from the bytesused field of the V4L2 buffer in the
  469. * payload field of the hva stream buffer
  470. */
  471. struct vb2_queue *vq;
  472. struct hva_stream *stream;
  473. vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, buf->type);
  474. if (buf->index >= vq->num_buffers) {
  475. dev_dbg(dev, "%s buffer index %d out of range (%d)\n",
  476. ctx->name, buf->index, vq->num_buffers);
  477. return -EINVAL;
  478. }
  479. stream = (struct hva_stream *)vq->bufs[buf->index];
  480. stream->bytesused = buf->bytesused;
  481. }
  482. return v4l2_m2m_qbuf(file, ctx->fh.m2m_ctx, buf);
  483. }
  484. /* V4L2 ioctl ops */
  485. static const struct v4l2_ioctl_ops hva_ioctl_ops = {
  486. .vidioc_querycap = hva_querycap,
  487. .vidioc_enum_fmt_vid_cap = hva_enum_fmt_stream,
  488. .vidioc_enum_fmt_vid_out = hva_enum_fmt_frame,
  489. .vidioc_g_fmt_vid_cap = hva_g_fmt_stream,
  490. .vidioc_g_fmt_vid_out = hva_g_fmt_frame,
  491. .vidioc_try_fmt_vid_cap = hva_try_fmt_stream,
  492. .vidioc_try_fmt_vid_out = hva_try_fmt_frame,
  493. .vidioc_s_fmt_vid_cap = hva_s_fmt_stream,
  494. .vidioc_s_fmt_vid_out = hva_s_fmt_frame,
  495. .vidioc_g_parm = hva_g_parm,
  496. .vidioc_s_parm = hva_s_parm,
  497. .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
  498. .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
  499. .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
  500. .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
  501. .vidioc_qbuf = hva_qbuf,
  502. .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
  503. .vidioc_streamon = v4l2_m2m_ioctl_streamon,
  504. .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
  505. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  506. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  507. };
  508. /*
  509. * V4L2 control operations
  510. */
  511. static int hva_s_ctrl(struct v4l2_ctrl *ctrl)
  512. {
  513. struct hva_ctx *ctx = container_of(ctrl->handler, struct hva_ctx,
  514. ctrl_handler);
  515. struct device *dev = ctx_to_dev(ctx);
  516. dev_dbg(dev, "%s S_CTRL: id = %d, val = %d\n", ctx->name,
  517. ctrl->id, ctrl->val);
  518. switch (ctrl->id) {
  519. case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
  520. ctx->ctrls.bitrate_mode = ctrl->val;
  521. break;
  522. case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
  523. ctx->ctrls.gop_size = ctrl->val;
  524. break;
  525. case V4L2_CID_MPEG_VIDEO_BITRATE:
  526. ctx->ctrls.bitrate = ctrl->val;
  527. break;
  528. case V4L2_CID_MPEG_VIDEO_ASPECT:
  529. ctx->ctrls.aspect = ctrl->val;
  530. break;
  531. case V4L2_CID_MPEG_VIDEO_H264_PROFILE:
  532. ctx->ctrls.profile = ctrl->val;
  533. snprintf(ctx->streaminfo.profile,
  534. sizeof(ctx->streaminfo.profile),
  535. "%s profile",
  536. v4l2_ctrl_get_menu(ctrl->id)[ctrl->val]);
  537. break;
  538. case V4L2_CID_MPEG_VIDEO_H264_LEVEL:
  539. ctx->ctrls.level = ctrl->val;
  540. snprintf(ctx->streaminfo.level,
  541. sizeof(ctx->streaminfo.level),
  542. "level %s",
  543. v4l2_ctrl_get_menu(ctrl->id)[ctrl->val]);
  544. break;
  545. case V4L2_CID_MPEG_VIDEO_H264_ENTROPY_MODE:
  546. ctx->ctrls.entropy_mode = ctrl->val;
  547. break;
  548. case V4L2_CID_MPEG_VIDEO_H264_CPB_SIZE:
  549. ctx->ctrls.cpb_size = ctrl->val;
  550. break;
  551. case V4L2_CID_MPEG_VIDEO_H264_8X8_TRANSFORM:
  552. ctx->ctrls.dct8x8 = ctrl->val;
  553. break;
  554. case V4L2_CID_MPEG_VIDEO_H264_MIN_QP:
  555. ctx->ctrls.qpmin = ctrl->val;
  556. break;
  557. case V4L2_CID_MPEG_VIDEO_H264_MAX_QP:
  558. ctx->ctrls.qpmax = ctrl->val;
  559. break;
  560. case V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_ENABLE:
  561. ctx->ctrls.vui_sar = ctrl->val;
  562. break;
  563. case V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_IDC:
  564. ctx->ctrls.vui_sar_idc = ctrl->val;
  565. break;
  566. case V4L2_CID_MPEG_VIDEO_H264_SEI_FRAME_PACKING:
  567. ctx->ctrls.sei_fp = ctrl->val;
  568. break;
  569. case V4L2_CID_MPEG_VIDEO_H264_SEI_FP_ARRANGEMENT_TYPE:
  570. ctx->ctrls.sei_fp_type = ctrl->val;
  571. break;
  572. default:
  573. dev_dbg(dev, "%s S_CTRL: invalid control (id = %d)\n",
  574. ctx->name, ctrl->id);
  575. return -EINVAL;
  576. }
  577. return 0;
  578. }
  579. /* V4L2 control ops */
  580. static const struct v4l2_ctrl_ops hva_ctrl_ops = {
  581. .s_ctrl = hva_s_ctrl,
  582. };
  583. static int hva_ctrls_setup(struct hva_ctx *ctx)
  584. {
  585. struct device *dev = ctx_to_dev(ctx);
  586. u64 mask;
  587. enum v4l2_mpeg_video_h264_sei_fp_arrangement_type sei_fp_type =
  588. V4L2_MPEG_VIDEO_H264_SEI_FP_ARRANGEMENT_TYPE_TOP_BOTTOM;
  589. v4l2_ctrl_handler_init(&ctx->ctrl_handler, 15);
  590. v4l2_ctrl_new_std_menu(&ctx->ctrl_handler, &hva_ctrl_ops,
  591. V4L2_CID_MPEG_VIDEO_BITRATE_MODE,
  592. V4L2_MPEG_VIDEO_BITRATE_MODE_CBR,
  593. 0,
  594. V4L2_MPEG_VIDEO_BITRATE_MODE_CBR);
  595. v4l2_ctrl_new_std(&ctx->ctrl_handler, &hva_ctrl_ops,
  596. V4L2_CID_MPEG_VIDEO_GOP_SIZE,
  597. 1, 60, 1, 16);
  598. v4l2_ctrl_new_std(&ctx->ctrl_handler, &hva_ctrl_ops,
  599. V4L2_CID_MPEG_VIDEO_BITRATE,
  600. 1000, 60000000, 1000, 20000000);
  601. mask = ~(1 << V4L2_MPEG_VIDEO_ASPECT_1x1);
  602. v4l2_ctrl_new_std_menu(&ctx->ctrl_handler, &hva_ctrl_ops,
  603. V4L2_CID_MPEG_VIDEO_ASPECT,
  604. V4L2_MPEG_VIDEO_ASPECT_1x1,
  605. mask,
  606. V4L2_MPEG_VIDEO_ASPECT_1x1);
  607. mask = ~((1 << V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE) |
  608. (1 << V4L2_MPEG_VIDEO_H264_PROFILE_MAIN) |
  609. (1 << V4L2_MPEG_VIDEO_H264_PROFILE_HIGH) |
  610. (1 << V4L2_MPEG_VIDEO_H264_PROFILE_STEREO_HIGH));
  611. v4l2_ctrl_new_std_menu(&ctx->ctrl_handler, &hva_ctrl_ops,
  612. V4L2_CID_MPEG_VIDEO_H264_PROFILE,
  613. V4L2_MPEG_VIDEO_H264_PROFILE_STEREO_HIGH,
  614. mask,
  615. V4L2_MPEG_VIDEO_H264_PROFILE_HIGH);
  616. v4l2_ctrl_new_std_menu(&ctx->ctrl_handler, &hva_ctrl_ops,
  617. V4L2_CID_MPEG_VIDEO_H264_LEVEL,
  618. V4L2_MPEG_VIDEO_H264_LEVEL_4_2,
  619. 0,
  620. V4L2_MPEG_VIDEO_H264_LEVEL_4_0);
  621. v4l2_ctrl_new_std_menu(&ctx->ctrl_handler, &hva_ctrl_ops,
  622. V4L2_CID_MPEG_VIDEO_H264_ENTROPY_MODE,
  623. V4L2_MPEG_VIDEO_H264_ENTROPY_MODE_CABAC,
  624. 0,
  625. V4L2_MPEG_VIDEO_H264_ENTROPY_MODE_CAVLC);
  626. v4l2_ctrl_new_std(&ctx->ctrl_handler, &hva_ctrl_ops,
  627. V4L2_CID_MPEG_VIDEO_H264_CPB_SIZE,
  628. 1, 10000, 1, 3000);
  629. v4l2_ctrl_new_std(&ctx->ctrl_handler, &hva_ctrl_ops,
  630. V4L2_CID_MPEG_VIDEO_H264_8X8_TRANSFORM,
  631. 0, 1, 1, 0);
  632. v4l2_ctrl_new_std(&ctx->ctrl_handler, &hva_ctrl_ops,
  633. V4L2_CID_MPEG_VIDEO_H264_MIN_QP,
  634. 0, 51, 1, 5);
  635. v4l2_ctrl_new_std(&ctx->ctrl_handler, &hva_ctrl_ops,
  636. V4L2_CID_MPEG_VIDEO_H264_MAX_QP,
  637. 0, 51, 1, 51);
  638. v4l2_ctrl_new_std(&ctx->ctrl_handler, &hva_ctrl_ops,
  639. V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_ENABLE,
  640. 0, 1, 1, 1);
  641. mask = ~(1 << V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_1x1);
  642. v4l2_ctrl_new_std_menu(&ctx->ctrl_handler, &hva_ctrl_ops,
  643. V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_IDC,
  644. V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_1x1,
  645. mask,
  646. V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_1x1);
  647. v4l2_ctrl_new_std(&ctx->ctrl_handler, &hva_ctrl_ops,
  648. V4L2_CID_MPEG_VIDEO_H264_SEI_FRAME_PACKING,
  649. 0, 1, 1, 0);
  650. mask = ~(1 << sei_fp_type);
  651. v4l2_ctrl_new_std_menu(&ctx->ctrl_handler, &hva_ctrl_ops,
  652. V4L2_CID_MPEG_VIDEO_H264_SEI_FP_ARRANGEMENT_TYPE,
  653. sei_fp_type,
  654. mask,
  655. sei_fp_type);
  656. if (ctx->ctrl_handler.error) {
  657. int err = ctx->ctrl_handler.error;
  658. dev_dbg(dev, "%s controls setup failed (%d)\n",
  659. ctx->name, err);
  660. v4l2_ctrl_handler_free(&ctx->ctrl_handler);
  661. return err;
  662. }
  663. v4l2_ctrl_handler_setup(&ctx->ctrl_handler);
  664. /* set default time per frame */
  665. ctx->ctrls.time_per_frame.numerator = HVA_DEFAULT_FRAME_NUM;
  666. ctx->ctrls.time_per_frame.denominator = HVA_DEFAULT_FRAME_DEN;
  667. return 0;
  668. }
  669. /*
  670. * mem-to-mem operations
  671. */
  672. static void hva_run_work(struct work_struct *work)
  673. {
  674. struct hva_ctx *ctx = container_of(work, struct hva_ctx, run_work);
  675. struct vb2_v4l2_buffer *src_buf, *dst_buf;
  676. const struct hva_enc *enc = ctx->enc;
  677. struct hva_frame *frame;
  678. struct hva_stream *stream;
  679. int ret;
  680. /* protect instance against reentrancy */
  681. mutex_lock(&ctx->lock);
  682. #ifdef CONFIG_VIDEO_STI_HVA_DEBUGFS
  683. hva_dbg_perf_begin(ctx);
  684. #endif
  685. src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
  686. dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
  687. frame = to_hva_frame(src_buf);
  688. stream = to_hva_stream(dst_buf);
  689. frame->vbuf.sequence = ctx->frame_num++;
  690. ret = enc->encode(ctx, frame, stream);
  691. vb2_set_plane_payload(&dst_buf->vb2_buf, 0, stream->bytesused);
  692. if (ret) {
  693. v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_ERROR);
  694. v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_ERROR);
  695. } else {
  696. /* propagate frame timestamp */
  697. dst_buf->vb2_buf.timestamp = src_buf->vb2_buf.timestamp;
  698. dst_buf->field = V4L2_FIELD_NONE;
  699. dst_buf->sequence = ctx->stream_num - 1;
  700. ctx->encoded_frames++;
  701. #ifdef CONFIG_VIDEO_STI_HVA_DEBUGFS
  702. hva_dbg_perf_end(ctx, stream);
  703. #endif
  704. v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
  705. v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_DONE);
  706. }
  707. mutex_unlock(&ctx->lock);
  708. v4l2_m2m_job_finish(ctx->hva_dev->m2m_dev, ctx->fh.m2m_ctx);
  709. }
  710. static void hva_device_run(void *priv)
  711. {
  712. struct hva_ctx *ctx = priv;
  713. struct hva_dev *hva = ctx_to_hdev(ctx);
  714. queue_work(hva->work_queue, &ctx->run_work);
  715. }
  716. static void hva_job_abort(void *priv)
  717. {
  718. struct hva_ctx *ctx = priv;
  719. struct device *dev = ctx_to_dev(ctx);
  720. dev_dbg(dev, "%s aborting job\n", ctx->name);
  721. ctx->aborting = true;
  722. }
  723. static int hva_job_ready(void *priv)
  724. {
  725. struct hva_ctx *ctx = priv;
  726. struct device *dev = ctx_to_dev(ctx);
  727. if (!v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx)) {
  728. dev_dbg(dev, "%s job not ready: no frame buffers\n",
  729. ctx->name);
  730. return 0;
  731. }
  732. if (!v4l2_m2m_num_dst_bufs_ready(ctx->fh.m2m_ctx)) {
  733. dev_dbg(dev, "%s job not ready: no stream buffers\n",
  734. ctx->name);
  735. return 0;
  736. }
  737. if (ctx->aborting) {
  738. dev_dbg(dev, "%s job not ready: aborting\n", ctx->name);
  739. return 0;
  740. }
  741. return 1;
  742. }
  743. /* mem-to-mem ops */
  744. static const struct v4l2_m2m_ops hva_m2m_ops = {
  745. .device_run = hva_device_run,
  746. .job_abort = hva_job_abort,
  747. .job_ready = hva_job_ready,
  748. };
  749. /*
  750. * VB2 queue operations
  751. */
  752. static int hva_queue_setup(struct vb2_queue *vq,
  753. unsigned int *num_buffers, unsigned int *num_planes,
  754. unsigned int sizes[], struct device *alloc_devs[])
  755. {
  756. struct hva_ctx *ctx = vb2_get_drv_priv(vq);
  757. struct device *dev = ctx_to_dev(ctx);
  758. unsigned int size;
  759. dev_dbg(dev, "%s %s queue setup: num_buffers %d\n", ctx->name,
  760. to_type_str(vq->type), *num_buffers);
  761. size = vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT ?
  762. ctx->frameinfo.size : ctx->max_stream_size;
  763. if (*num_planes)
  764. return sizes[0] < size ? -EINVAL : 0;
  765. /* only one plane supported */
  766. *num_planes = 1;
  767. sizes[0] = size;
  768. return 0;
  769. }
  770. static int hva_buf_prepare(struct vb2_buffer *vb)
  771. {
  772. struct hva_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  773. struct device *dev = ctx_to_dev(ctx);
  774. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  775. if (vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
  776. struct hva_frame *frame = to_hva_frame(vbuf);
  777. if (vbuf->field == V4L2_FIELD_ANY)
  778. vbuf->field = V4L2_FIELD_NONE;
  779. if (vbuf->field != V4L2_FIELD_NONE) {
  780. dev_dbg(dev,
  781. "%s frame[%d] prepare: %d field not supported\n",
  782. ctx->name, vb->index, vbuf->field);
  783. return -EINVAL;
  784. }
  785. if (!frame->prepared) {
  786. /* get memory addresses */
  787. frame->vaddr = vb2_plane_vaddr(&vbuf->vb2_buf, 0);
  788. frame->paddr = vb2_dma_contig_plane_dma_addr(
  789. &vbuf->vb2_buf, 0);
  790. frame->info = ctx->frameinfo;
  791. frame->prepared = true;
  792. dev_dbg(dev,
  793. "%s frame[%d] prepared; virt=%p, phy=%pad\n",
  794. ctx->name, vb->index,
  795. frame->vaddr, &frame->paddr);
  796. }
  797. } else {
  798. struct hva_stream *stream = to_hva_stream(vbuf);
  799. if (!stream->prepared) {
  800. /* get memory addresses */
  801. stream->vaddr = vb2_plane_vaddr(&vbuf->vb2_buf, 0);
  802. stream->paddr = vb2_dma_contig_plane_dma_addr(
  803. &vbuf->vb2_buf, 0);
  804. stream->size = vb2_plane_size(&vbuf->vb2_buf, 0);
  805. stream->prepared = true;
  806. dev_dbg(dev,
  807. "%s stream[%d] prepared; virt=%p, phy=%pad\n",
  808. ctx->name, vb->index,
  809. stream->vaddr, &stream->paddr);
  810. }
  811. }
  812. return 0;
  813. }
  814. static void hva_buf_queue(struct vb2_buffer *vb)
  815. {
  816. struct hva_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  817. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  818. if (ctx->fh.m2m_ctx)
  819. v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
  820. }
  821. static int hva_start_streaming(struct vb2_queue *vq, unsigned int count)
  822. {
  823. struct hva_ctx *ctx = vb2_get_drv_priv(vq);
  824. struct hva_dev *hva = ctx_to_hdev(ctx);
  825. struct device *dev = ctx_to_dev(ctx);
  826. struct vb2_v4l2_buffer *vbuf;
  827. int ret;
  828. unsigned int i;
  829. bool found = false;
  830. dev_dbg(dev, "%s %s start streaming\n", ctx->name,
  831. to_type_str(vq->type));
  832. /* open encoder when both start_streaming have been called */
  833. if (V4L2_TYPE_IS_OUTPUT(vq->type)) {
  834. if (!vb2_start_streaming_called(&ctx->fh.m2m_ctx->cap_q_ctx.q))
  835. return 0;
  836. } else {
  837. if (!vb2_start_streaming_called(&ctx->fh.m2m_ctx->out_q_ctx.q))
  838. return 0;
  839. }
  840. /* store the instance context in the instances array */
  841. for (i = 0; i < HVA_MAX_INSTANCES; i++) {
  842. if (!hva->instances[i]) {
  843. hva->instances[i] = ctx;
  844. /* save the context identifier in the context */
  845. ctx->id = i;
  846. found = true;
  847. break;
  848. }
  849. }
  850. if (!found) {
  851. dev_err(dev, "%s maximum instances reached\n", ctx->name);
  852. ret = -ENOMEM;
  853. goto err;
  854. }
  855. hva->nb_of_instances++;
  856. if (!ctx->enc) {
  857. ret = hva_open_encoder(ctx,
  858. ctx->streaminfo.streamformat,
  859. ctx->frameinfo.pixelformat,
  860. &ctx->enc);
  861. if (ret < 0)
  862. goto err_ctx;
  863. }
  864. return 0;
  865. err_ctx:
  866. hva->instances[ctx->id] = NULL;
  867. hva->nb_of_instances--;
  868. err:
  869. if (vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
  870. /* return of all pending buffers to vb2 (in queued state) */
  871. while ((vbuf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx)))
  872. v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_QUEUED);
  873. } else {
  874. /* return of all pending buffers to vb2 (in queued state) */
  875. while ((vbuf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx)))
  876. v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_QUEUED);
  877. }
  878. ctx->sys_errors++;
  879. return ret;
  880. }
  881. static void hva_stop_streaming(struct vb2_queue *vq)
  882. {
  883. struct hva_ctx *ctx = vb2_get_drv_priv(vq);
  884. struct hva_dev *hva = ctx_to_hdev(ctx);
  885. struct device *dev = ctx_to_dev(ctx);
  886. const struct hva_enc *enc = ctx->enc;
  887. struct vb2_v4l2_buffer *vbuf;
  888. dev_dbg(dev, "%s %s stop streaming\n", ctx->name,
  889. to_type_str(vq->type));
  890. if (vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
  891. /* return of all pending buffers to vb2 (in error state) */
  892. ctx->frame_num = 0;
  893. while ((vbuf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx)))
  894. v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_ERROR);
  895. } else {
  896. /* return of all pending buffers to vb2 (in error state) */
  897. ctx->stream_num = 0;
  898. while ((vbuf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx)))
  899. v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_ERROR);
  900. }
  901. if ((V4L2_TYPE_IS_OUTPUT(vq->type) &&
  902. vb2_is_streaming(&ctx->fh.m2m_ctx->cap_q_ctx.q)) ||
  903. (!V4L2_TYPE_IS_OUTPUT(vq->type) &&
  904. vb2_is_streaming(&ctx->fh.m2m_ctx->out_q_ctx.q))) {
  905. dev_dbg(dev, "%s %s out=%d cap=%d\n",
  906. ctx->name, to_type_str(vq->type),
  907. vb2_is_streaming(&ctx->fh.m2m_ctx->out_q_ctx.q),
  908. vb2_is_streaming(&ctx->fh.m2m_ctx->cap_q_ctx.q));
  909. return;
  910. }
  911. /* close encoder when both stop_streaming have been called */
  912. if (enc) {
  913. dev_dbg(dev, "%s %s encoder closed\n", ctx->name, enc->name);
  914. enc->close(ctx);
  915. ctx->enc = NULL;
  916. /* clear instance context in instances array */
  917. hva->instances[ctx->id] = NULL;
  918. hva->nb_of_instances--;
  919. }
  920. ctx->aborting = false;
  921. }
  922. /* VB2 queue ops */
  923. static const struct vb2_ops hva_qops = {
  924. .queue_setup = hva_queue_setup,
  925. .buf_prepare = hva_buf_prepare,
  926. .buf_queue = hva_buf_queue,
  927. .start_streaming = hva_start_streaming,
  928. .stop_streaming = hva_stop_streaming,
  929. .wait_prepare = vb2_ops_wait_prepare,
  930. .wait_finish = vb2_ops_wait_finish,
  931. };
  932. /*
  933. * V4L2 file operations
  934. */
  935. static int queue_init(struct hva_ctx *ctx, struct vb2_queue *vq)
  936. {
  937. vq->io_modes = VB2_MMAP | VB2_DMABUF;
  938. vq->drv_priv = ctx;
  939. vq->ops = &hva_qops;
  940. vq->mem_ops = &vb2_dma_contig_memops;
  941. vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
  942. vq->lock = &ctx->hva_dev->lock;
  943. return vb2_queue_init(vq);
  944. }
  945. static int hva_queue_init(void *priv, struct vb2_queue *src_vq,
  946. struct vb2_queue *dst_vq)
  947. {
  948. struct hva_ctx *ctx = priv;
  949. int ret;
  950. src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  951. src_vq->buf_struct_size = sizeof(struct hva_frame);
  952. src_vq->min_buffers_needed = MIN_FRAMES;
  953. src_vq->dev = ctx->hva_dev->dev;
  954. ret = queue_init(ctx, src_vq);
  955. if (ret)
  956. return ret;
  957. dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  958. dst_vq->buf_struct_size = sizeof(struct hva_stream);
  959. dst_vq->min_buffers_needed = MIN_STREAMS;
  960. dst_vq->dev = ctx->hva_dev->dev;
  961. return queue_init(ctx, dst_vq);
  962. }
  963. static int hva_open(struct file *file)
  964. {
  965. struct hva_dev *hva = video_drvdata(file);
  966. struct device *dev = hva_to_dev(hva);
  967. struct hva_ctx *ctx;
  968. int ret;
  969. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  970. if (!ctx) {
  971. ret = -ENOMEM;
  972. goto out;
  973. }
  974. ctx->hva_dev = hva;
  975. INIT_WORK(&ctx->run_work, hva_run_work);
  976. v4l2_fh_init(&ctx->fh, video_devdata(file));
  977. file->private_data = &ctx->fh;
  978. v4l2_fh_add(&ctx->fh);
  979. ret = hva_ctrls_setup(ctx);
  980. if (ret) {
  981. dev_err(dev, "%s [x:x] failed to setup controls\n",
  982. HVA_PREFIX);
  983. ctx->sys_errors++;
  984. goto err_fh;
  985. }
  986. ctx->fh.ctrl_handler = &ctx->ctrl_handler;
  987. mutex_init(&ctx->lock);
  988. ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(hva->m2m_dev, ctx,
  989. &hva_queue_init);
  990. if (IS_ERR(ctx->fh.m2m_ctx)) {
  991. ret = PTR_ERR(ctx->fh.m2m_ctx);
  992. dev_err(dev, "%s failed to initialize m2m context (%d)\n",
  993. HVA_PREFIX, ret);
  994. ctx->sys_errors++;
  995. goto err_ctrls;
  996. }
  997. /* set the instance name */
  998. mutex_lock(&hva->lock);
  999. hva->instance_id++;
  1000. snprintf(ctx->name, sizeof(ctx->name), "[%3d:----]",
  1001. hva->instance_id);
  1002. mutex_unlock(&hva->lock);
  1003. /* default parameters for frame and stream */
  1004. set_default_params(ctx);
  1005. #ifdef CONFIG_VIDEO_STI_HVA_DEBUGFS
  1006. hva_dbg_ctx_create(ctx);
  1007. #endif
  1008. dev_info(dev, "%s encoder instance created\n", ctx->name);
  1009. return 0;
  1010. err_ctrls:
  1011. v4l2_ctrl_handler_free(&ctx->ctrl_handler);
  1012. err_fh:
  1013. v4l2_fh_del(&ctx->fh);
  1014. v4l2_fh_exit(&ctx->fh);
  1015. kfree(ctx);
  1016. out:
  1017. return ret;
  1018. }
  1019. static int hva_release(struct file *file)
  1020. {
  1021. struct hva_ctx *ctx = fh_to_ctx(file->private_data);
  1022. struct hva_dev *hva = ctx_to_hdev(ctx);
  1023. struct device *dev = ctx_to_dev(ctx);
  1024. const struct hva_enc *enc = ctx->enc;
  1025. if (enc) {
  1026. dev_dbg(dev, "%s %s encoder closed\n", ctx->name, enc->name);
  1027. enc->close(ctx);
  1028. ctx->enc = NULL;
  1029. /* clear instance context in instances array */
  1030. hva->instances[ctx->id] = NULL;
  1031. hva->nb_of_instances--;
  1032. }
  1033. /* trace a summary of instance before closing (debug purpose) */
  1034. hva_dbg_summary(ctx);
  1035. v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
  1036. v4l2_ctrl_handler_free(&ctx->ctrl_handler);
  1037. v4l2_fh_del(&ctx->fh);
  1038. v4l2_fh_exit(&ctx->fh);
  1039. #ifdef CONFIG_VIDEO_STI_HVA_DEBUGFS
  1040. hva_dbg_ctx_remove(ctx);
  1041. #endif
  1042. dev_info(dev, "%s encoder instance released\n", ctx->name);
  1043. kfree(ctx);
  1044. return 0;
  1045. }
  1046. /* V4L2 file ops */
  1047. static const struct v4l2_file_operations hva_fops = {
  1048. .owner = THIS_MODULE,
  1049. .open = hva_open,
  1050. .release = hva_release,
  1051. .unlocked_ioctl = video_ioctl2,
  1052. .mmap = v4l2_m2m_fop_mmap,
  1053. .poll = v4l2_m2m_fop_poll,
  1054. };
  1055. /*
  1056. * Platform device operations
  1057. */
  1058. static int hva_register_device(struct hva_dev *hva)
  1059. {
  1060. int ret;
  1061. struct video_device *vdev;
  1062. struct device *dev;
  1063. if (!hva)
  1064. return -ENODEV;
  1065. dev = hva_to_dev(hva);
  1066. hva->m2m_dev = v4l2_m2m_init(&hva_m2m_ops);
  1067. if (IS_ERR(hva->m2m_dev)) {
  1068. dev_err(dev, "%s failed to initialize v4l2-m2m device\n",
  1069. HVA_PREFIX);
  1070. ret = PTR_ERR(hva->m2m_dev);
  1071. goto err;
  1072. }
  1073. vdev = video_device_alloc();
  1074. if (!vdev) {
  1075. dev_err(dev, "%s failed to allocate video device\n",
  1076. HVA_PREFIX);
  1077. ret = -ENOMEM;
  1078. goto err_m2m_release;
  1079. }
  1080. vdev->fops = &hva_fops;
  1081. vdev->ioctl_ops = &hva_ioctl_ops;
  1082. vdev->release = video_device_release;
  1083. vdev->lock = &hva->lock;
  1084. vdev->vfl_dir = VFL_DIR_M2M;
  1085. vdev->device_caps = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_M2M;
  1086. vdev->v4l2_dev = &hva->v4l2_dev;
  1087. snprintf(vdev->name, sizeof(vdev->name), "%s%lx", HVA_NAME,
  1088. hva->ip_version);
  1089. ret = video_register_device(vdev, VFL_TYPE_GRABBER, -1);
  1090. if (ret) {
  1091. dev_err(dev, "%s failed to register video device\n",
  1092. HVA_PREFIX);
  1093. goto err_vdev_release;
  1094. }
  1095. hva->vdev = vdev;
  1096. video_set_drvdata(vdev, hva);
  1097. return 0;
  1098. err_vdev_release:
  1099. video_device_release(vdev);
  1100. err_m2m_release:
  1101. v4l2_m2m_release(hva->m2m_dev);
  1102. err:
  1103. return ret;
  1104. }
  1105. static void hva_unregister_device(struct hva_dev *hva)
  1106. {
  1107. if (!hva)
  1108. return;
  1109. if (hva->m2m_dev)
  1110. v4l2_m2m_release(hva->m2m_dev);
  1111. video_unregister_device(hva->vdev);
  1112. }
  1113. static int hva_probe(struct platform_device *pdev)
  1114. {
  1115. struct hva_dev *hva;
  1116. struct device *dev = &pdev->dev;
  1117. int ret;
  1118. hva = devm_kzalloc(dev, sizeof(*hva), GFP_KERNEL);
  1119. if (!hva) {
  1120. ret = -ENOMEM;
  1121. goto err;
  1122. }
  1123. ret = dma_coerce_mask_and_coherent(dev, DMA_BIT_MASK(32));
  1124. if (ret)
  1125. return ret;
  1126. hva->dev = dev;
  1127. hva->pdev = pdev;
  1128. platform_set_drvdata(pdev, hva);
  1129. mutex_init(&hva->lock);
  1130. /* probe hardware */
  1131. ret = hva_hw_probe(pdev, hva);
  1132. if (ret)
  1133. goto err;
  1134. /* register all available encoders */
  1135. register_encoders(hva);
  1136. /* register all supported formats */
  1137. register_formats(hva);
  1138. /* register on V4L2 */
  1139. ret = v4l2_device_register(dev, &hva->v4l2_dev);
  1140. if (ret) {
  1141. dev_err(dev, "%s %s failed to register V4L2 device\n",
  1142. HVA_PREFIX, HVA_NAME);
  1143. goto err_hw;
  1144. }
  1145. #ifdef CONFIG_VIDEO_STI_HVA_DEBUGFS
  1146. hva_debugfs_create(hva);
  1147. #endif
  1148. hva->work_queue = create_workqueue(HVA_NAME);
  1149. if (!hva->work_queue) {
  1150. dev_err(dev, "%s %s failed to allocate work queue\n",
  1151. HVA_PREFIX, HVA_NAME);
  1152. ret = -ENOMEM;
  1153. goto err_v4l2;
  1154. }
  1155. /* register device */
  1156. ret = hva_register_device(hva);
  1157. if (ret)
  1158. goto err_work_queue;
  1159. dev_info(dev, "%s %s registered as /dev/video%d\n", HVA_PREFIX,
  1160. HVA_NAME, hva->vdev->num);
  1161. return 0;
  1162. err_work_queue:
  1163. destroy_workqueue(hva->work_queue);
  1164. err_v4l2:
  1165. #ifdef CONFIG_VIDEO_STI_HVA_DEBUGFS
  1166. hva_debugfs_remove(hva);
  1167. #endif
  1168. v4l2_device_unregister(&hva->v4l2_dev);
  1169. err_hw:
  1170. hva_hw_remove(hva);
  1171. err:
  1172. return ret;
  1173. }
  1174. static int hva_remove(struct platform_device *pdev)
  1175. {
  1176. struct hva_dev *hva = platform_get_drvdata(pdev);
  1177. struct device *dev = hva_to_dev(hva);
  1178. hva_unregister_device(hva);
  1179. destroy_workqueue(hva->work_queue);
  1180. hva_hw_remove(hva);
  1181. #ifdef CONFIG_VIDEO_STI_HVA_DEBUGFS
  1182. hva_debugfs_remove(hva);
  1183. #endif
  1184. v4l2_device_unregister(&hva->v4l2_dev);
  1185. dev_info(dev, "%s %s removed\n", HVA_PREFIX, pdev->name);
  1186. return 0;
  1187. }
  1188. /* PM ops */
  1189. static const struct dev_pm_ops hva_pm_ops = {
  1190. .runtime_suspend = hva_hw_runtime_suspend,
  1191. .runtime_resume = hva_hw_runtime_resume,
  1192. };
  1193. static const struct of_device_id hva_match_types[] = {
  1194. {
  1195. .compatible = "st,st-hva",
  1196. },
  1197. { /* end node */ }
  1198. };
  1199. MODULE_DEVICE_TABLE(of, hva_match_types);
  1200. static struct platform_driver hva_driver = {
  1201. .probe = hva_probe,
  1202. .remove = hva_remove,
  1203. .driver = {
  1204. .name = HVA_NAME,
  1205. .of_match_table = hva_match_types,
  1206. .pm = &hva_pm_ops,
  1207. },
  1208. };
  1209. module_platform_driver(hva_driver);
  1210. MODULE_LICENSE("GPL");
  1211. MODULE_AUTHOR("Yannick Fertre <yannick.fertre@st.com>");
  1212. MODULE_DESCRIPTION("STMicroelectronics HVA video encoder V4L2 driver");