hva-v4l2.c 37 KB

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