coda-common.c 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606
  1. /*
  2. * Coda multi-standard codec IP
  3. *
  4. * Copyright (C) 2012 Vista Silicon S.L.
  5. * Javier Martin, <javier.martin@vista-silicon.com>
  6. * Xavier Duret
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. */
  13. #include <linux/clk.h>
  14. #include <linux/debugfs.h>
  15. #include <linux/delay.h>
  16. #include <linux/firmware.h>
  17. #include <linux/gcd.h>
  18. #include <linux/genalloc.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/io.h>
  21. #include <linux/irq.h>
  22. #include <linux/kfifo.h>
  23. #include <linux/module.h>
  24. #include <linux/of_device.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/pm_runtime.h>
  27. #include <linux/slab.h>
  28. #include <linux/videodev2.h>
  29. #include <linux/of.h>
  30. #include <linux/platform_data/media/coda.h>
  31. #include <linux/reset.h>
  32. #include <media/v4l2-ctrls.h>
  33. #include <media/v4l2-device.h>
  34. #include <media/v4l2-event.h>
  35. #include <media/v4l2-ioctl.h>
  36. #include <media/v4l2-mem2mem.h>
  37. #include <media/videobuf2-v4l2.h>
  38. #include <media/videobuf2-dma-contig.h>
  39. #include <media/videobuf2-vmalloc.h>
  40. #include "coda.h"
  41. #include "imx-vdoa.h"
  42. #define CODA_NAME "coda"
  43. #define CODADX6_MAX_INSTANCES 4
  44. #define CODA_MAX_FORMATS 4
  45. #define CODA_ISRAM_SIZE (2048 * 2)
  46. #define MIN_W 176
  47. #define MIN_H 144
  48. #define S_ALIGN 1 /* multiple of 2 */
  49. #define W_ALIGN 1 /* multiple of 2 */
  50. #define H_ALIGN 1 /* multiple of 2 */
  51. #define fh_to_ctx(__fh) container_of(__fh, struct coda_ctx, fh)
  52. int coda_debug;
  53. module_param(coda_debug, int, 0644);
  54. MODULE_PARM_DESC(coda_debug, "Debug level (0-2)");
  55. static int disable_tiling;
  56. module_param(disable_tiling, int, 0644);
  57. MODULE_PARM_DESC(disable_tiling, "Disable tiled frame buffers");
  58. static int disable_vdoa;
  59. module_param(disable_vdoa, int, 0644);
  60. MODULE_PARM_DESC(disable_vdoa, "Disable Video Data Order Adapter tiled to raster-scan conversion");
  61. static int enable_bwb = 0;
  62. module_param(enable_bwb, int, 0644);
  63. MODULE_PARM_DESC(enable_bwb, "Enable BWB unit, may crash on certain streams");
  64. void coda_write(struct coda_dev *dev, u32 data, u32 reg)
  65. {
  66. v4l2_dbg(2, coda_debug, &dev->v4l2_dev,
  67. "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
  68. writel(data, dev->regs_base + reg);
  69. }
  70. unsigned int coda_read(struct coda_dev *dev, u32 reg)
  71. {
  72. u32 data;
  73. data = readl(dev->regs_base + reg);
  74. v4l2_dbg(2, coda_debug, &dev->v4l2_dev,
  75. "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
  76. return data;
  77. }
  78. void coda_write_base(struct coda_ctx *ctx, struct coda_q_data *q_data,
  79. struct vb2_v4l2_buffer *buf, unsigned int reg_y)
  80. {
  81. u32 base_y = vb2_dma_contig_plane_dma_addr(&buf->vb2_buf, 0);
  82. u32 base_cb, base_cr;
  83. switch (q_data->fourcc) {
  84. case V4L2_PIX_FMT_YUYV:
  85. /* Fallthrough: IN -H264-> CODA -NV12 MB-> VDOA -YUYV-> OUT */
  86. case V4L2_PIX_FMT_NV12:
  87. case V4L2_PIX_FMT_YUV420:
  88. default:
  89. base_cb = base_y + q_data->bytesperline * q_data->height;
  90. base_cr = base_cb + q_data->bytesperline * q_data->height / 4;
  91. break;
  92. case V4L2_PIX_FMT_YVU420:
  93. /* Switch Cb and Cr for YVU420 format */
  94. base_cr = base_y + q_data->bytesperline * q_data->height;
  95. base_cb = base_cr + q_data->bytesperline * q_data->height / 4;
  96. break;
  97. case V4L2_PIX_FMT_YUV422P:
  98. base_cb = base_y + q_data->bytesperline * q_data->height;
  99. base_cr = base_cb + q_data->bytesperline * q_data->height / 2;
  100. }
  101. coda_write(ctx->dev, base_y, reg_y);
  102. coda_write(ctx->dev, base_cb, reg_y + 4);
  103. coda_write(ctx->dev, base_cr, reg_y + 8);
  104. }
  105. #define CODA_CODEC(mode, src_fourcc, dst_fourcc, max_w, max_h) \
  106. { mode, src_fourcc, dst_fourcc, max_w, max_h }
  107. /*
  108. * Arrays of codecs supported by each given version of Coda:
  109. * i.MX27 -> codadx6
  110. * i.MX5x -> coda7
  111. * i.MX6 -> coda960
  112. * Use V4L2_PIX_FMT_YUV420 as placeholder for all supported YUV 4:2:0 variants
  113. */
  114. static const struct coda_codec codadx6_codecs[] = {
  115. CODA_CODEC(CODADX6_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 720, 576),
  116. CODA_CODEC(CODADX6_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 720, 576),
  117. };
  118. static const struct coda_codec coda7_codecs[] = {
  119. CODA_CODEC(CODA7_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 1280, 720),
  120. CODA_CODEC(CODA7_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 1280, 720),
  121. CODA_CODEC(CODA7_MODE_ENCODE_MJPG, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_JPEG, 8192, 8192),
  122. CODA_CODEC(CODA7_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1088),
  123. CODA_CODEC(CODA7_MODE_DECODE_MP2, V4L2_PIX_FMT_MPEG2, V4L2_PIX_FMT_YUV420, 1920, 1088),
  124. CODA_CODEC(CODA7_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1920, 1088),
  125. CODA_CODEC(CODA7_MODE_DECODE_MJPG, V4L2_PIX_FMT_JPEG, V4L2_PIX_FMT_YUV420, 8192, 8192),
  126. };
  127. static const struct coda_codec coda9_codecs[] = {
  128. CODA_CODEC(CODA9_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 1920, 1088),
  129. CODA_CODEC(CODA9_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 1920, 1088),
  130. CODA_CODEC(CODA9_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1088),
  131. CODA_CODEC(CODA9_MODE_DECODE_MP2, V4L2_PIX_FMT_MPEG2, V4L2_PIX_FMT_YUV420, 1920, 1088),
  132. CODA_CODEC(CODA9_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1920, 1088),
  133. };
  134. struct coda_video_device {
  135. const char *name;
  136. enum coda_inst_type type;
  137. const struct coda_context_ops *ops;
  138. bool direct;
  139. u32 src_formats[CODA_MAX_FORMATS];
  140. u32 dst_formats[CODA_MAX_FORMATS];
  141. };
  142. static const struct coda_video_device coda_bit_encoder = {
  143. .name = "coda-encoder",
  144. .type = CODA_INST_ENCODER,
  145. .ops = &coda_bit_encode_ops,
  146. .src_formats = {
  147. V4L2_PIX_FMT_NV12,
  148. V4L2_PIX_FMT_YUV420,
  149. V4L2_PIX_FMT_YVU420,
  150. },
  151. .dst_formats = {
  152. V4L2_PIX_FMT_H264,
  153. V4L2_PIX_FMT_MPEG4,
  154. },
  155. };
  156. static const struct coda_video_device coda_bit_jpeg_encoder = {
  157. .name = "coda-jpeg-encoder",
  158. .type = CODA_INST_ENCODER,
  159. .ops = &coda_bit_encode_ops,
  160. .src_formats = {
  161. V4L2_PIX_FMT_NV12,
  162. V4L2_PIX_FMT_YUV420,
  163. V4L2_PIX_FMT_YVU420,
  164. V4L2_PIX_FMT_YUV422P,
  165. },
  166. .dst_formats = {
  167. V4L2_PIX_FMT_JPEG,
  168. },
  169. };
  170. static const struct coda_video_device coda_bit_decoder = {
  171. .name = "coda-decoder",
  172. .type = CODA_INST_DECODER,
  173. .ops = &coda_bit_decode_ops,
  174. .src_formats = {
  175. V4L2_PIX_FMT_H264,
  176. V4L2_PIX_FMT_MPEG2,
  177. V4L2_PIX_FMT_MPEG4,
  178. },
  179. .dst_formats = {
  180. V4L2_PIX_FMT_NV12,
  181. V4L2_PIX_FMT_YUV420,
  182. V4L2_PIX_FMT_YVU420,
  183. /*
  184. * If V4L2_PIX_FMT_YUYV should be default,
  185. * set_default_params() must be adjusted.
  186. */
  187. V4L2_PIX_FMT_YUYV,
  188. },
  189. };
  190. static const struct coda_video_device coda_bit_jpeg_decoder = {
  191. .name = "coda-jpeg-decoder",
  192. .type = CODA_INST_DECODER,
  193. .ops = &coda_bit_decode_ops,
  194. .src_formats = {
  195. V4L2_PIX_FMT_JPEG,
  196. },
  197. .dst_formats = {
  198. V4L2_PIX_FMT_NV12,
  199. V4L2_PIX_FMT_YUV420,
  200. V4L2_PIX_FMT_YVU420,
  201. V4L2_PIX_FMT_YUV422P,
  202. },
  203. };
  204. static const struct coda_video_device *codadx6_video_devices[] = {
  205. &coda_bit_encoder,
  206. };
  207. static const struct coda_video_device *coda7_video_devices[] = {
  208. &coda_bit_jpeg_encoder,
  209. &coda_bit_jpeg_decoder,
  210. &coda_bit_encoder,
  211. &coda_bit_decoder,
  212. };
  213. static const struct coda_video_device *coda9_video_devices[] = {
  214. &coda_bit_encoder,
  215. &coda_bit_decoder,
  216. };
  217. /*
  218. * Normalize all supported YUV 4:2:0 formats to the value used in the codec
  219. * tables.
  220. */
  221. static u32 coda_format_normalize_yuv(u32 fourcc)
  222. {
  223. switch (fourcc) {
  224. case V4L2_PIX_FMT_NV12:
  225. case V4L2_PIX_FMT_YUV420:
  226. case V4L2_PIX_FMT_YVU420:
  227. case V4L2_PIX_FMT_YUV422P:
  228. case V4L2_PIX_FMT_YUYV:
  229. return V4L2_PIX_FMT_YUV420;
  230. default:
  231. return fourcc;
  232. }
  233. }
  234. static const struct coda_codec *coda_find_codec(struct coda_dev *dev,
  235. int src_fourcc, int dst_fourcc)
  236. {
  237. const struct coda_codec *codecs = dev->devtype->codecs;
  238. int num_codecs = dev->devtype->num_codecs;
  239. int k;
  240. src_fourcc = coda_format_normalize_yuv(src_fourcc);
  241. dst_fourcc = coda_format_normalize_yuv(dst_fourcc);
  242. if (src_fourcc == dst_fourcc)
  243. return NULL;
  244. for (k = 0; k < num_codecs; k++) {
  245. if (codecs[k].src_fourcc == src_fourcc &&
  246. codecs[k].dst_fourcc == dst_fourcc)
  247. break;
  248. }
  249. if (k == num_codecs)
  250. return NULL;
  251. return &codecs[k];
  252. }
  253. static void coda_get_max_dimensions(struct coda_dev *dev,
  254. const struct coda_codec *codec,
  255. int *max_w, int *max_h)
  256. {
  257. const struct coda_codec *codecs = dev->devtype->codecs;
  258. int num_codecs = dev->devtype->num_codecs;
  259. unsigned int w, h;
  260. int k;
  261. if (codec) {
  262. w = codec->max_w;
  263. h = codec->max_h;
  264. } else {
  265. for (k = 0, w = 0, h = 0; k < num_codecs; k++) {
  266. w = max(w, codecs[k].max_w);
  267. h = max(h, codecs[k].max_h);
  268. }
  269. }
  270. if (max_w)
  271. *max_w = w;
  272. if (max_h)
  273. *max_h = h;
  274. }
  275. static const struct coda_video_device *to_coda_video_device(struct video_device
  276. *vdev)
  277. {
  278. struct coda_dev *dev = video_get_drvdata(vdev);
  279. unsigned int i = vdev - dev->vfd;
  280. if (i >= dev->devtype->num_vdevs)
  281. return NULL;
  282. return dev->devtype->vdevs[i];
  283. }
  284. const char *coda_product_name(int product)
  285. {
  286. static char buf[9];
  287. switch (product) {
  288. case CODA_DX6:
  289. return "CodaDx6";
  290. case CODA_7541:
  291. return "CODA7541";
  292. case CODA_960:
  293. return "CODA960";
  294. default:
  295. snprintf(buf, sizeof(buf), "(0x%04x)", product);
  296. return buf;
  297. }
  298. }
  299. static struct vdoa_data *coda_get_vdoa_data(void)
  300. {
  301. struct device_node *vdoa_node;
  302. struct platform_device *vdoa_pdev;
  303. struct vdoa_data *vdoa_data = NULL;
  304. vdoa_node = of_find_compatible_node(NULL, NULL, "fsl,imx6q-vdoa");
  305. if (!vdoa_node)
  306. return NULL;
  307. vdoa_pdev = of_find_device_by_node(vdoa_node);
  308. if (!vdoa_pdev)
  309. goto out;
  310. vdoa_data = platform_get_drvdata(vdoa_pdev);
  311. if (!vdoa_data)
  312. vdoa_data = ERR_PTR(-EPROBE_DEFER);
  313. out:
  314. if (vdoa_node)
  315. of_node_put(vdoa_node);
  316. return vdoa_data;
  317. }
  318. /*
  319. * V4L2 ioctl() operations.
  320. */
  321. static int coda_querycap(struct file *file, void *priv,
  322. struct v4l2_capability *cap)
  323. {
  324. struct coda_ctx *ctx = fh_to_ctx(priv);
  325. strlcpy(cap->driver, CODA_NAME, sizeof(cap->driver));
  326. strlcpy(cap->card, coda_product_name(ctx->dev->devtype->product),
  327. sizeof(cap->card));
  328. strlcpy(cap->bus_info, "platform:" CODA_NAME, sizeof(cap->bus_info));
  329. cap->device_caps = V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING;
  330. cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
  331. return 0;
  332. }
  333. static int coda_enum_fmt(struct file *file, void *priv,
  334. struct v4l2_fmtdesc *f)
  335. {
  336. struct video_device *vdev = video_devdata(file);
  337. const struct coda_video_device *cvd = to_coda_video_device(vdev);
  338. struct coda_ctx *ctx = fh_to_ctx(priv);
  339. const u32 *formats;
  340. if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
  341. formats = cvd->src_formats;
  342. else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  343. formats = cvd->dst_formats;
  344. else
  345. return -EINVAL;
  346. if (f->index >= CODA_MAX_FORMATS || formats[f->index] == 0)
  347. return -EINVAL;
  348. /* Skip YUYV if the vdoa is not available */
  349. if (!ctx->vdoa && f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
  350. formats[f->index] == V4L2_PIX_FMT_YUYV)
  351. return -EINVAL;
  352. f->pixelformat = formats[f->index];
  353. return 0;
  354. }
  355. static int coda_g_fmt(struct file *file, void *priv,
  356. struct v4l2_format *f)
  357. {
  358. struct coda_q_data *q_data;
  359. struct coda_ctx *ctx = fh_to_ctx(priv);
  360. q_data = get_q_data(ctx, f->type);
  361. if (!q_data)
  362. return -EINVAL;
  363. f->fmt.pix.field = V4L2_FIELD_NONE;
  364. f->fmt.pix.pixelformat = q_data->fourcc;
  365. f->fmt.pix.width = q_data->width;
  366. f->fmt.pix.height = q_data->height;
  367. f->fmt.pix.bytesperline = q_data->bytesperline;
  368. f->fmt.pix.sizeimage = q_data->sizeimage;
  369. if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_JPEG)
  370. f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
  371. else
  372. f->fmt.pix.colorspace = ctx->colorspace;
  373. return 0;
  374. }
  375. static int coda_try_pixelformat(struct coda_ctx *ctx, struct v4l2_format *f)
  376. {
  377. struct coda_q_data *q_data;
  378. const u32 *formats;
  379. int i;
  380. if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
  381. formats = ctx->cvd->src_formats;
  382. else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  383. formats = ctx->cvd->dst_formats;
  384. else
  385. return -EINVAL;
  386. for (i = 0; i < CODA_MAX_FORMATS; i++) {
  387. /* Skip YUYV if the vdoa is not available */
  388. if (!ctx->vdoa && f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
  389. formats[i] == V4L2_PIX_FMT_YUYV)
  390. continue;
  391. if (formats[i] == f->fmt.pix.pixelformat) {
  392. f->fmt.pix.pixelformat = formats[i];
  393. return 0;
  394. }
  395. }
  396. /* Fall back to currently set pixelformat */
  397. q_data = get_q_data(ctx, f->type);
  398. f->fmt.pix.pixelformat = q_data->fourcc;
  399. return 0;
  400. }
  401. static int coda_try_fmt_vdoa(struct coda_ctx *ctx, struct v4l2_format *f,
  402. bool *use_vdoa)
  403. {
  404. int err;
  405. if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  406. return -EINVAL;
  407. if (!use_vdoa)
  408. return -EINVAL;
  409. if (!ctx->vdoa) {
  410. *use_vdoa = false;
  411. return 0;
  412. }
  413. err = vdoa_context_configure(NULL, f->fmt.pix.width, f->fmt.pix.height,
  414. f->fmt.pix.pixelformat);
  415. if (err) {
  416. *use_vdoa = false;
  417. return 0;
  418. }
  419. *use_vdoa = true;
  420. return 0;
  421. }
  422. static unsigned int coda_estimate_sizeimage(struct coda_ctx *ctx, u32 sizeimage,
  423. u32 width, u32 height)
  424. {
  425. /*
  426. * This is a rough estimate for sensible compressed buffer
  427. * sizes (between 1 and 16 bits per pixel). This could be
  428. * improved by better format specific worst case estimates.
  429. */
  430. return round_up(clamp(sizeimage, width * height / 8,
  431. width * height * 2), PAGE_SIZE);
  432. }
  433. static int coda_try_fmt(struct coda_ctx *ctx, const struct coda_codec *codec,
  434. struct v4l2_format *f)
  435. {
  436. struct coda_dev *dev = ctx->dev;
  437. unsigned int max_w, max_h;
  438. enum v4l2_field field;
  439. field = f->fmt.pix.field;
  440. if (field == V4L2_FIELD_ANY)
  441. field = V4L2_FIELD_NONE;
  442. else if (V4L2_FIELD_NONE != field)
  443. return -EINVAL;
  444. /* V4L2 specification suggests the driver corrects the format struct
  445. * if any of the dimensions is unsupported */
  446. f->fmt.pix.field = field;
  447. coda_get_max_dimensions(dev, codec, &max_w, &max_h);
  448. v4l_bound_align_image(&f->fmt.pix.width, MIN_W, max_w, W_ALIGN,
  449. &f->fmt.pix.height, MIN_H, max_h, H_ALIGN,
  450. S_ALIGN);
  451. switch (f->fmt.pix.pixelformat) {
  452. case V4L2_PIX_FMT_NV12:
  453. case V4L2_PIX_FMT_YUV420:
  454. case V4L2_PIX_FMT_YVU420:
  455. /*
  456. * Frame stride must be at least multiple of 8,
  457. * but multiple of 16 for h.264 or JPEG 4:2:x
  458. */
  459. f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
  460. f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
  461. f->fmt.pix.height * 3 / 2;
  462. break;
  463. case V4L2_PIX_FMT_YUYV:
  464. f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16) * 2;
  465. f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
  466. f->fmt.pix.height;
  467. break;
  468. case V4L2_PIX_FMT_YUV422P:
  469. f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
  470. f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
  471. f->fmt.pix.height * 2;
  472. break;
  473. case V4L2_PIX_FMT_JPEG:
  474. f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
  475. /* fallthrough */
  476. case V4L2_PIX_FMT_H264:
  477. case V4L2_PIX_FMT_MPEG4:
  478. case V4L2_PIX_FMT_MPEG2:
  479. f->fmt.pix.bytesperline = 0;
  480. f->fmt.pix.sizeimage = coda_estimate_sizeimage(ctx,
  481. f->fmt.pix.sizeimage,
  482. f->fmt.pix.width,
  483. f->fmt.pix.height);
  484. break;
  485. default:
  486. BUG();
  487. }
  488. return 0;
  489. }
  490. static int coda_try_fmt_vid_cap(struct file *file, void *priv,
  491. struct v4l2_format *f)
  492. {
  493. struct coda_ctx *ctx = fh_to_ctx(priv);
  494. const struct coda_q_data *q_data_src;
  495. const struct coda_codec *codec;
  496. struct vb2_queue *src_vq;
  497. int ret;
  498. bool use_vdoa;
  499. ret = coda_try_pixelformat(ctx, f);
  500. if (ret < 0)
  501. return ret;
  502. q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
  503. /*
  504. * If the source format is already fixed, only allow the same output
  505. * resolution
  506. */
  507. src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
  508. if (vb2_is_streaming(src_vq)) {
  509. f->fmt.pix.width = q_data_src->width;
  510. f->fmt.pix.height = q_data_src->height;
  511. }
  512. f->fmt.pix.colorspace = ctx->colorspace;
  513. q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
  514. codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
  515. f->fmt.pix.pixelformat);
  516. if (!codec)
  517. return -EINVAL;
  518. ret = coda_try_fmt(ctx, codec, f);
  519. if (ret < 0)
  520. return ret;
  521. /* The h.264 decoder only returns complete 16x16 macroblocks */
  522. if (codec && codec->src_fourcc == V4L2_PIX_FMT_H264) {
  523. f->fmt.pix.width = f->fmt.pix.width;
  524. f->fmt.pix.height = round_up(f->fmt.pix.height, 16);
  525. f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
  526. f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
  527. f->fmt.pix.height * 3 / 2;
  528. ret = coda_try_fmt_vdoa(ctx, f, &use_vdoa);
  529. if (ret < 0)
  530. return ret;
  531. if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_YUYV) {
  532. if (!use_vdoa)
  533. return -EINVAL;
  534. f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16) * 2;
  535. f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
  536. f->fmt.pix.height;
  537. }
  538. }
  539. return 0;
  540. }
  541. static int coda_try_fmt_vid_out(struct file *file, void *priv,
  542. struct v4l2_format *f)
  543. {
  544. struct coda_ctx *ctx = fh_to_ctx(priv);
  545. struct coda_dev *dev = ctx->dev;
  546. const struct coda_q_data *q_data_dst;
  547. const struct coda_codec *codec;
  548. int ret;
  549. ret = coda_try_pixelformat(ctx, f);
  550. if (ret < 0)
  551. return ret;
  552. switch (f->fmt.pix.colorspace) {
  553. case V4L2_COLORSPACE_REC709:
  554. case V4L2_COLORSPACE_JPEG:
  555. break;
  556. default:
  557. if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_JPEG)
  558. f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
  559. else
  560. f->fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
  561. }
  562. q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
  563. codec = coda_find_codec(dev, f->fmt.pix.pixelformat, q_data_dst->fourcc);
  564. return coda_try_fmt(ctx, codec, f);
  565. }
  566. static int coda_s_fmt(struct coda_ctx *ctx, struct v4l2_format *f,
  567. struct v4l2_rect *r)
  568. {
  569. struct coda_q_data *q_data;
  570. struct vb2_queue *vq;
  571. vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
  572. if (!vq)
  573. return -EINVAL;
  574. q_data = get_q_data(ctx, f->type);
  575. if (!q_data)
  576. return -EINVAL;
  577. if (vb2_is_busy(vq)) {
  578. v4l2_err(&ctx->dev->v4l2_dev, "%s queue busy\n", __func__);
  579. return -EBUSY;
  580. }
  581. q_data->fourcc = f->fmt.pix.pixelformat;
  582. q_data->width = f->fmt.pix.width;
  583. q_data->height = f->fmt.pix.height;
  584. q_data->bytesperline = f->fmt.pix.bytesperline;
  585. q_data->sizeimage = f->fmt.pix.sizeimage;
  586. if (r) {
  587. q_data->rect = *r;
  588. } else {
  589. q_data->rect.left = 0;
  590. q_data->rect.top = 0;
  591. q_data->rect.width = f->fmt.pix.width;
  592. q_data->rect.height = f->fmt.pix.height;
  593. }
  594. switch (f->fmt.pix.pixelformat) {
  595. case V4L2_PIX_FMT_YUYV:
  596. ctx->tiled_map_type = GDI_TILED_FRAME_MB_RASTER_MAP;
  597. break;
  598. case V4L2_PIX_FMT_NV12:
  599. ctx->tiled_map_type = GDI_TILED_FRAME_MB_RASTER_MAP;
  600. if (!disable_tiling)
  601. break;
  602. /* else fall through */
  603. case V4L2_PIX_FMT_YUV420:
  604. case V4L2_PIX_FMT_YVU420:
  605. ctx->tiled_map_type = GDI_LINEAR_FRAME_MAP;
  606. break;
  607. default:
  608. break;
  609. }
  610. if (ctx->tiled_map_type == GDI_TILED_FRAME_MB_RASTER_MAP &&
  611. !coda_try_fmt_vdoa(ctx, f, &ctx->use_vdoa) &&
  612. ctx->use_vdoa)
  613. vdoa_context_configure(ctx->vdoa, f->fmt.pix.width,
  614. f->fmt.pix.height,
  615. f->fmt.pix.pixelformat);
  616. else
  617. ctx->use_vdoa = false;
  618. v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
  619. "Setting format for type %d, wxh: %dx%d, fmt: %4.4s %c\n",
  620. f->type, q_data->width, q_data->height,
  621. (char *)&q_data->fourcc,
  622. (ctx->tiled_map_type == GDI_LINEAR_FRAME_MAP) ? 'L' : 'T');
  623. return 0;
  624. }
  625. static int coda_s_fmt_vid_cap(struct file *file, void *priv,
  626. struct v4l2_format *f)
  627. {
  628. struct coda_ctx *ctx = fh_to_ctx(priv);
  629. struct coda_q_data *q_data_src;
  630. struct v4l2_rect r;
  631. int ret;
  632. ret = coda_try_fmt_vid_cap(file, priv, f);
  633. if (ret)
  634. return ret;
  635. q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
  636. r.left = 0;
  637. r.top = 0;
  638. r.width = q_data_src->width;
  639. r.height = q_data_src->height;
  640. return coda_s_fmt(ctx, f, &r);
  641. }
  642. static int coda_s_fmt_vid_out(struct file *file, void *priv,
  643. struct v4l2_format *f)
  644. {
  645. struct coda_ctx *ctx = fh_to_ctx(priv);
  646. struct coda_q_data *q_data_src;
  647. struct v4l2_format f_cap;
  648. struct v4l2_rect r;
  649. int ret;
  650. ret = coda_try_fmt_vid_out(file, priv, f);
  651. if (ret)
  652. return ret;
  653. ret = coda_s_fmt(ctx, f, NULL);
  654. if (ret)
  655. return ret;
  656. ctx->colorspace = f->fmt.pix.colorspace;
  657. memset(&f_cap, 0, sizeof(f_cap));
  658. f_cap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  659. coda_g_fmt(file, priv, &f_cap);
  660. f_cap.fmt.pix.width = f->fmt.pix.width;
  661. f_cap.fmt.pix.height = f->fmt.pix.height;
  662. ret = coda_try_fmt_vid_cap(file, priv, &f_cap);
  663. if (ret)
  664. return ret;
  665. q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
  666. r.left = 0;
  667. r.top = 0;
  668. r.width = q_data_src->width;
  669. r.height = q_data_src->height;
  670. return coda_s_fmt(ctx, &f_cap, &r);
  671. }
  672. static int coda_reqbufs(struct file *file, void *priv,
  673. struct v4l2_requestbuffers *rb)
  674. {
  675. struct coda_ctx *ctx = fh_to_ctx(priv);
  676. int ret;
  677. ret = v4l2_m2m_reqbufs(file, ctx->fh.m2m_ctx, rb);
  678. if (ret)
  679. return ret;
  680. /*
  681. * Allow to allocate instance specific per-context buffers, such as
  682. * bitstream ringbuffer, slice buffer, work buffer, etc. if needed.
  683. */
  684. if (rb->type == V4L2_BUF_TYPE_VIDEO_OUTPUT && ctx->ops->reqbufs)
  685. return ctx->ops->reqbufs(ctx, rb);
  686. return 0;
  687. }
  688. static int coda_qbuf(struct file *file, void *priv,
  689. struct v4l2_buffer *buf)
  690. {
  691. struct coda_ctx *ctx = fh_to_ctx(priv);
  692. return v4l2_m2m_qbuf(file, ctx->fh.m2m_ctx, buf);
  693. }
  694. static bool coda_buf_is_end_of_stream(struct coda_ctx *ctx,
  695. struct vb2_v4l2_buffer *buf)
  696. {
  697. return ((ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG) &&
  698. (buf->sequence == (ctx->qsequence - 1)));
  699. }
  700. void coda_m2m_buf_done(struct coda_ctx *ctx, struct vb2_v4l2_buffer *buf,
  701. enum vb2_buffer_state state)
  702. {
  703. const struct v4l2_event eos_event = {
  704. .type = V4L2_EVENT_EOS
  705. };
  706. if (coda_buf_is_end_of_stream(ctx, buf)) {
  707. buf->flags |= V4L2_BUF_FLAG_LAST;
  708. v4l2_event_queue_fh(&ctx->fh, &eos_event);
  709. }
  710. v4l2_m2m_buf_done(buf, state);
  711. }
  712. static int coda_g_selection(struct file *file, void *fh,
  713. struct v4l2_selection *s)
  714. {
  715. struct coda_ctx *ctx = fh_to_ctx(fh);
  716. struct coda_q_data *q_data;
  717. struct v4l2_rect r, *rsel;
  718. q_data = get_q_data(ctx, s->type);
  719. if (!q_data)
  720. return -EINVAL;
  721. r.left = 0;
  722. r.top = 0;
  723. r.width = q_data->width;
  724. r.height = q_data->height;
  725. rsel = &q_data->rect;
  726. switch (s->target) {
  727. case V4L2_SEL_TGT_CROP_DEFAULT:
  728. case V4L2_SEL_TGT_CROP_BOUNDS:
  729. rsel = &r;
  730. /* fallthrough */
  731. case V4L2_SEL_TGT_CROP:
  732. if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
  733. return -EINVAL;
  734. break;
  735. case V4L2_SEL_TGT_COMPOSE_BOUNDS:
  736. case V4L2_SEL_TGT_COMPOSE_PADDED:
  737. rsel = &r;
  738. /* fallthrough */
  739. case V4L2_SEL_TGT_COMPOSE:
  740. case V4L2_SEL_TGT_COMPOSE_DEFAULT:
  741. if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  742. return -EINVAL;
  743. break;
  744. default:
  745. return -EINVAL;
  746. }
  747. s->r = *rsel;
  748. return 0;
  749. }
  750. static int coda_try_encoder_cmd(struct file *file, void *fh,
  751. struct v4l2_encoder_cmd *ec)
  752. {
  753. if (ec->cmd != V4L2_ENC_CMD_STOP)
  754. return -EINVAL;
  755. if (ec->flags & V4L2_ENC_CMD_STOP_AT_GOP_END)
  756. return -EINVAL;
  757. return 0;
  758. }
  759. static int coda_encoder_cmd(struct file *file, void *fh,
  760. struct v4l2_encoder_cmd *ec)
  761. {
  762. struct coda_ctx *ctx = fh_to_ctx(fh);
  763. struct vb2_queue *dst_vq;
  764. int ret;
  765. ret = coda_try_encoder_cmd(file, fh, ec);
  766. if (ret < 0)
  767. return ret;
  768. /* Ignore encoder stop command silently in decoder context */
  769. if (ctx->inst_type != CODA_INST_ENCODER)
  770. return 0;
  771. /* Set the stream-end flag on this context */
  772. ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
  773. /* If there is no buffer in flight, wake up */
  774. if (ctx->qsequence == ctx->osequence) {
  775. dst_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx,
  776. V4L2_BUF_TYPE_VIDEO_CAPTURE);
  777. dst_vq->last_buffer_dequeued = true;
  778. wake_up(&dst_vq->done_wq);
  779. }
  780. return 0;
  781. }
  782. static int coda_try_decoder_cmd(struct file *file, void *fh,
  783. struct v4l2_decoder_cmd *dc)
  784. {
  785. if (dc->cmd != V4L2_DEC_CMD_STOP)
  786. return -EINVAL;
  787. if (dc->flags & V4L2_DEC_CMD_STOP_TO_BLACK)
  788. return -EINVAL;
  789. if (!(dc->flags & V4L2_DEC_CMD_STOP_IMMEDIATELY) && (dc->stop.pts != 0))
  790. return -EINVAL;
  791. return 0;
  792. }
  793. static int coda_decoder_cmd(struct file *file, void *fh,
  794. struct v4l2_decoder_cmd *dc)
  795. {
  796. struct coda_ctx *ctx = fh_to_ctx(fh);
  797. int ret;
  798. ret = coda_try_decoder_cmd(file, fh, dc);
  799. if (ret < 0)
  800. return ret;
  801. /* Ignore decoder stop command silently in encoder context */
  802. if (ctx->inst_type != CODA_INST_DECODER)
  803. return 0;
  804. /* Set the stream-end flag on this context */
  805. coda_bit_stream_end_flag(ctx);
  806. ctx->hold = false;
  807. v4l2_m2m_try_schedule(ctx->fh.m2m_ctx);
  808. return 0;
  809. }
  810. static int coda_g_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
  811. {
  812. struct coda_ctx *ctx = fh_to_ctx(fh);
  813. struct v4l2_fract *tpf;
  814. if (a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
  815. return -EINVAL;
  816. a->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
  817. tpf = &a->parm.output.timeperframe;
  818. tpf->denominator = ctx->params.framerate & CODA_FRATE_RES_MASK;
  819. tpf->numerator = 1 + (ctx->params.framerate >>
  820. CODA_FRATE_DIV_OFFSET);
  821. return 0;
  822. }
  823. /*
  824. * Approximate timeperframe v4l2_fract with values that can be written
  825. * into the 16-bit CODA_FRATE_DIV and CODA_FRATE_RES fields.
  826. */
  827. static void coda_approximate_timeperframe(struct v4l2_fract *timeperframe)
  828. {
  829. struct v4l2_fract s = *timeperframe;
  830. struct v4l2_fract f0;
  831. struct v4l2_fract f1 = { 1, 0 };
  832. struct v4l2_fract f2 = { 0, 1 };
  833. unsigned int i, div, s_denominator;
  834. /* Lower bound is 1/65535 */
  835. if (s.numerator == 0 || s.denominator / s.numerator > 65535) {
  836. timeperframe->numerator = 1;
  837. timeperframe->denominator = 65535;
  838. return;
  839. }
  840. /* Upper bound is 65536/1, map everything above to infinity */
  841. if (s.denominator == 0 || s.numerator / s.denominator > 65536) {
  842. timeperframe->numerator = 1;
  843. timeperframe->denominator = 0;
  844. return;
  845. }
  846. /* Reduce fraction to lowest terms */
  847. div = gcd(s.numerator, s.denominator);
  848. if (div > 1) {
  849. s.numerator /= div;
  850. s.denominator /= div;
  851. }
  852. if (s.numerator <= 65536 && s.denominator < 65536) {
  853. *timeperframe = s;
  854. return;
  855. }
  856. /* Find successive convergents from continued fraction expansion */
  857. while (f2.numerator <= 65536 && f2.denominator < 65536) {
  858. f0 = f1;
  859. f1 = f2;
  860. /* Stop when f2 exactly equals timeperframe */
  861. if (s.numerator == 0)
  862. break;
  863. i = s.denominator / s.numerator;
  864. f2.numerator = f0.numerator + i * f1.numerator;
  865. f2.denominator = f0.denominator + i * f2.denominator;
  866. s_denominator = s.numerator;
  867. s.numerator = s.denominator % s.numerator;
  868. s.denominator = s_denominator;
  869. }
  870. *timeperframe = f1;
  871. }
  872. static uint32_t coda_timeperframe_to_frate(struct v4l2_fract *timeperframe)
  873. {
  874. return ((timeperframe->numerator - 1) << CODA_FRATE_DIV_OFFSET) |
  875. timeperframe->denominator;
  876. }
  877. static int coda_s_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
  878. {
  879. struct coda_ctx *ctx = fh_to_ctx(fh);
  880. struct v4l2_fract *tpf;
  881. if (a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
  882. return -EINVAL;
  883. tpf = &a->parm.output.timeperframe;
  884. coda_approximate_timeperframe(tpf);
  885. ctx->params.framerate = coda_timeperframe_to_frate(tpf);
  886. return 0;
  887. }
  888. static int coda_subscribe_event(struct v4l2_fh *fh,
  889. const struct v4l2_event_subscription *sub)
  890. {
  891. switch (sub->type) {
  892. case V4L2_EVENT_EOS:
  893. return v4l2_event_subscribe(fh, sub, 0, NULL);
  894. default:
  895. return v4l2_ctrl_subscribe_event(fh, sub);
  896. }
  897. }
  898. static const struct v4l2_ioctl_ops coda_ioctl_ops = {
  899. .vidioc_querycap = coda_querycap,
  900. .vidioc_enum_fmt_vid_cap = coda_enum_fmt,
  901. .vidioc_g_fmt_vid_cap = coda_g_fmt,
  902. .vidioc_try_fmt_vid_cap = coda_try_fmt_vid_cap,
  903. .vidioc_s_fmt_vid_cap = coda_s_fmt_vid_cap,
  904. .vidioc_enum_fmt_vid_out = coda_enum_fmt,
  905. .vidioc_g_fmt_vid_out = coda_g_fmt,
  906. .vidioc_try_fmt_vid_out = coda_try_fmt_vid_out,
  907. .vidioc_s_fmt_vid_out = coda_s_fmt_vid_out,
  908. .vidioc_reqbufs = coda_reqbufs,
  909. .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
  910. .vidioc_qbuf = coda_qbuf,
  911. .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
  912. .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
  913. .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
  914. .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
  915. .vidioc_streamon = v4l2_m2m_ioctl_streamon,
  916. .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
  917. .vidioc_g_selection = coda_g_selection,
  918. .vidioc_try_encoder_cmd = coda_try_encoder_cmd,
  919. .vidioc_encoder_cmd = coda_encoder_cmd,
  920. .vidioc_try_decoder_cmd = coda_try_decoder_cmd,
  921. .vidioc_decoder_cmd = coda_decoder_cmd,
  922. .vidioc_g_parm = coda_g_parm,
  923. .vidioc_s_parm = coda_s_parm,
  924. .vidioc_subscribe_event = coda_subscribe_event,
  925. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  926. };
  927. /*
  928. * Mem-to-mem operations.
  929. */
  930. static void coda_device_run(void *m2m_priv)
  931. {
  932. struct coda_ctx *ctx = m2m_priv;
  933. struct coda_dev *dev = ctx->dev;
  934. queue_work(dev->workqueue, &ctx->pic_run_work);
  935. }
  936. static void coda_pic_run_work(struct work_struct *work)
  937. {
  938. struct coda_ctx *ctx = container_of(work, struct coda_ctx, pic_run_work);
  939. struct coda_dev *dev = ctx->dev;
  940. int ret;
  941. mutex_lock(&ctx->buffer_mutex);
  942. mutex_lock(&dev->coda_mutex);
  943. ret = ctx->ops->prepare_run(ctx);
  944. if (ret < 0 && ctx->inst_type == CODA_INST_DECODER) {
  945. mutex_unlock(&dev->coda_mutex);
  946. mutex_unlock(&ctx->buffer_mutex);
  947. /* job_finish scheduled by prepare_decode */
  948. return;
  949. }
  950. if (!wait_for_completion_timeout(&ctx->completion,
  951. msecs_to_jiffies(1000))) {
  952. dev_err(&dev->plat_dev->dev, "CODA PIC_RUN timeout\n");
  953. ctx->hold = true;
  954. coda_hw_reset(ctx);
  955. } else if (!ctx->aborting) {
  956. ctx->ops->finish_run(ctx);
  957. }
  958. if ((ctx->aborting || (!ctx->streamon_cap && !ctx->streamon_out)) &&
  959. ctx->ops->seq_end_work)
  960. queue_work(dev->workqueue, &ctx->seq_end_work);
  961. mutex_unlock(&dev->coda_mutex);
  962. mutex_unlock(&ctx->buffer_mutex);
  963. v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx);
  964. }
  965. static int coda_job_ready(void *m2m_priv)
  966. {
  967. struct coda_ctx *ctx = m2m_priv;
  968. int src_bufs = v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx);
  969. /*
  970. * For both 'P' and 'key' frame cases 1 picture
  971. * and 1 frame are needed. In the decoder case,
  972. * the compressed frame can be in the bitstream.
  973. */
  974. if (!src_bufs && ctx->inst_type != CODA_INST_DECODER) {
  975. v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
  976. "not ready: not enough video buffers.\n");
  977. return 0;
  978. }
  979. if (!v4l2_m2m_num_dst_bufs_ready(ctx->fh.m2m_ctx)) {
  980. v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
  981. "not ready: not enough video capture buffers.\n");
  982. return 0;
  983. }
  984. if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit) {
  985. bool stream_end = ctx->bit_stream_param &
  986. CODA_BIT_STREAM_END_FLAG;
  987. int num_metas = ctx->num_metas;
  988. unsigned int count;
  989. count = hweight32(ctx->frm_dis_flg);
  990. if (ctx->use_vdoa && count >= (ctx->num_internal_frames - 1)) {
  991. v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
  992. "%d: not ready: all internal buffers in use: %d/%d (0x%x)",
  993. ctx->idx, count, ctx->num_internal_frames,
  994. ctx->frm_dis_flg);
  995. return 0;
  996. }
  997. if (ctx->hold && !src_bufs) {
  998. v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
  999. "%d: not ready: on hold for more buffers.\n",
  1000. ctx->idx);
  1001. return 0;
  1002. }
  1003. if (!stream_end && (num_metas + src_bufs) < 2) {
  1004. v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
  1005. "%d: not ready: need 2 buffers available (%d, %d)\n",
  1006. ctx->idx, num_metas, src_bufs);
  1007. return 0;
  1008. }
  1009. if (!src_bufs && !stream_end &&
  1010. (coda_get_bitstream_payload(ctx) < 512)) {
  1011. v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
  1012. "%d: not ready: not enough bitstream data (%d).\n",
  1013. ctx->idx, coda_get_bitstream_payload(ctx));
  1014. return 0;
  1015. }
  1016. }
  1017. if (ctx->aborting) {
  1018. v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
  1019. "not ready: aborting\n");
  1020. return 0;
  1021. }
  1022. v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
  1023. "job ready\n");
  1024. return 1;
  1025. }
  1026. static void coda_job_abort(void *priv)
  1027. {
  1028. struct coda_ctx *ctx = priv;
  1029. ctx->aborting = 1;
  1030. v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
  1031. "Aborting task\n");
  1032. }
  1033. static void coda_lock(void *m2m_priv)
  1034. {
  1035. struct coda_ctx *ctx = m2m_priv;
  1036. struct coda_dev *pcdev = ctx->dev;
  1037. mutex_lock(&pcdev->dev_mutex);
  1038. }
  1039. static void coda_unlock(void *m2m_priv)
  1040. {
  1041. struct coda_ctx *ctx = m2m_priv;
  1042. struct coda_dev *pcdev = ctx->dev;
  1043. mutex_unlock(&pcdev->dev_mutex);
  1044. }
  1045. static const struct v4l2_m2m_ops coda_m2m_ops = {
  1046. .device_run = coda_device_run,
  1047. .job_ready = coda_job_ready,
  1048. .job_abort = coda_job_abort,
  1049. .lock = coda_lock,
  1050. .unlock = coda_unlock,
  1051. };
  1052. static void set_default_params(struct coda_ctx *ctx)
  1053. {
  1054. unsigned int max_w, max_h, usize, csize;
  1055. ctx->codec = coda_find_codec(ctx->dev, ctx->cvd->src_formats[0],
  1056. ctx->cvd->dst_formats[0]);
  1057. max_w = min(ctx->codec->max_w, 1920U);
  1058. max_h = min(ctx->codec->max_h, 1088U);
  1059. usize = max_w * max_h * 3 / 2;
  1060. csize = coda_estimate_sizeimage(ctx, usize, max_w, max_h);
  1061. ctx->params.codec_mode = ctx->codec->mode;
  1062. ctx->colorspace = V4L2_COLORSPACE_REC709;
  1063. ctx->params.framerate = 30;
  1064. /* Default formats for output and input queues */
  1065. ctx->q_data[V4L2_M2M_SRC].fourcc = ctx->cvd->src_formats[0];
  1066. ctx->q_data[V4L2_M2M_DST].fourcc = ctx->cvd->dst_formats[0];
  1067. ctx->q_data[V4L2_M2M_SRC].width = max_w;
  1068. ctx->q_data[V4L2_M2M_SRC].height = max_h;
  1069. ctx->q_data[V4L2_M2M_DST].width = max_w;
  1070. ctx->q_data[V4L2_M2M_DST].height = max_h;
  1071. if (ctx->codec->src_fourcc == V4L2_PIX_FMT_YUV420) {
  1072. ctx->q_data[V4L2_M2M_SRC].bytesperline = max_w;
  1073. ctx->q_data[V4L2_M2M_SRC].sizeimage = usize;
  1074. ctx->q_data[V4L2_M2M_DST].bytesperline = 0;
  1075. ctx->q_data[V4L2_M2M_DST].sizeimage = csize;
  1076. } else {
  1077. ctx->q_data[V4L2_M2M_SRC].bytesperline = 0;
  1078. ctx->q_data[V4L2_M2M_SRC].sizeimage = csize;
  1079. ctx->q_data[V4L2_M2M_DST].bytesperline = max_w;
  1080. ctx->q_data[V4L2_M2M_DST].sizeimage = usize;
  1081. }
  1082. ctx->q_data[V4L2_M2M_SRC].rect.width = max_w;
  1083. ctx->q_data[V4L2_M2M_SRC].rect.height = max_h;
  1084. ctx->q_data[V4L2_M2M_DST].rect.width = max_w;
  1085. ctx->q_data[V4L2_M2M_DST].rect.height = max_h;
  1086. /*
  1087. * Since the RBC2AXI logic only supports a single chroma plane,
  1088. * macroblock tiling only works for to NV12 pixel format.
  1089. */
  1090. ctx->tiled_map_type = GDI_LINEAR_FRAME_MAP;
  1091. }
  1092. /*
  1093. * Queue operations
  1094. */
  1095. static int coda_queue_setup(struct vb2_queue *vq,
  1096. unsigned int *nbuffers, unsigned int *nplanes,
  1097. unsigned int sizes[], struct device *alloc_devs[])
  1098. {
  1099. struct coda_ctx *ctx = vb2_get_drv_priv(vq);
  1100. struct coda_q_data *q_data;
  1101. unsigned int size;
  1102. q_data = get_q_data(ctx, vq->type);
  1103. size = q_data->sizeimage;
  1104. *nplanes = 1;
  1105. sizes[0] = size;
  1106. v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
  1107. "get %d buffer(s) of size %d each.\n", *nbuffers, size);
  1108. return 0;
  1109. }
  1110. static int coda_buf_prepare(struct vb2_buffer *vb)
  1111. {
  1112. struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  1113. struct coda_q_data *q_data;
  1114. q_data = get_q_data(ctx, vb->vb2_queue->type);
  1115. if (vb2_plane_size(vb, 0) < q_data->sizeimage) {
  1116. v4l2_warn(&ctx->dev->v4l2_dev,
  1117. "%s data will not fit into plane (%lu < %lu)\n",
  1118. __func__, vb2_plane_size(vb, 0),
  1119. (long)q_data->sizeimage);
  1120. return -EINVAL;
  1121. }
  1122. return 0;
  1123. }
  1124. static void coda_buf_queue(struct vb2_buffer *vb)
  1125. {
  1126. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  1127. struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  1128. struct vb2_queue *vq = vb->vb2_queue;
  1129. struct coda_q_data *q_data;
  1130. q_data = get_q_data(ctx, vb->vb2_queue->type);
  1131. /*
  1132. * In the decoder case, immediately try to copy the buffer into the
  1133. * bitstream ringbuffer and mark it as ready to be dequeued.
  1134. */
  1135. if (ctx->bitstream.size && vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
  1136. /*
  1137. * For backwards compatibility, queuing an empty buffer marks
  1138. * the stream end
  1139. */
  1140. if (vb2_get_plane_payload(vb, 0) == 0)
  1141. coda_bit_stream_end_flag(ctx);
  1142. if (q_data->fourcc == V4L2_PIX_FMT_H264) {
  1143. /*
  1144. * Unless already done, try to obtain profile_idc and
  1145. * level_idc from the SPS header. This allows to decide
  1146. * whether to enable reordering during sequence
  1147. * initialization.
  1148. */
  1149. if (!ctx->params.h264_profile_idc)
  1150. coda_sps_parse_profile(ctx, vb);
  1151. }
  1152. mutex_lock(&ctx->bitstream_mutex);
  1153. v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
  1154. if (vb2_is_streaming(vb->vb2_queue))
  1155. /* This set buf->sequence = ctx->qsequence++ */
  1156. coda_fill_bitstream(ctx, NULL);
  1157. mutex_unlock(&ctx->bitstream_mutex);
  1158. } else {
  1159. if (ctx->inst_type == CODA_INST_ENCODER &&
  1160. vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
  1161. vbuf->sequence = ctx->qsequence++;
  1162. v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
  1163. }
  1164. }
  1165. int coda_alloc_aux_buf(struct coda_dev *dev, struct coda_aux_buf *buf,
  1166. size_t size, const char *name, struct dentry *parent)
  1167. {
  1168. buf->vaddr = dma_alloc_coherent(&dev->plat_dev->dev, size, &buf->paddr,
  1169. GFP_KERNEL);
  1170. if (!buf->vaddr) {
  1171. v4l2_err(&dev->v4l2_dev,
  1172. "Failed to allocate %s buffer of size %zu\n",
  1173. name, size);
  1174. return -ENOMEM;
  1175. }
  1176. buf->size = size;
  1177. if (name && parent) {
  1178. buf->blob.data = buf->vaddr;
  1179. buf->blob.size = size;
  1180. buf->dentry = debugfs_create_blob(name, 0644, parent,
  1181. &buf->blob);
  1182. if (!buf->dentry)
  1183. dev_warn(&dev->plat_dev->dev,
  1184. "failed to create debugfs entry %s\n", name);
  1185. }
  1186. return 0;
  1187. }
  1188. void coda_free_aux_buf(struct coda_dev *dev,
  1189. struct coda_aux_buf *buf)
  1190. {
  1191. if (buf->vaddr) {
  1192. dma_free_coherent(&dev->plat_dev->dev, buf->size,
  1193. buf->vaddr, buf->paddr);
  1194. buf->vaddr = NULL;
  1195. buf->size = 0;
  1196. debugfs_remove(buf->dentry);
  1197. buf->dentry = NULL;
  1198. }
  1199. }
  1200. static int coda_start_streaming(struct vb2_queue *q, unsigned int count)
  1201. {
  1202. struct coda_ctx *ctx = vb2_get_drv_priv(q);
  1203. struct v4l2_device *v4l2_dev = &ctx->dev->v4l2_dev;
  1204. struct coda_q_data *q_data_src, *q_data_dst;
  1205. struct v4l2_m2m_buffer *m2m_buf, *tmp;
  1206. struct vb2_v4l2_buffer *buf;
  1207. struct list_head list;
  1208. int ret = 0;
  1209. if (count < 1)
  1210. return -EINVAL;
  1211. INIT_LIST_HEAD(&list);
  1212. q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
  1213. if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
  1214. if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit) {
  1215. /* copy the buffers that were queued before streamon */
  1216. mutex_lock(&ctx->bitstream_mutex);
  1217. coda_fill_bitstream(ctx, &list);
  1218. mutex_unlock(&ctx->bitstream_mutex);
  1219. if (coda_get_bitstream_payload(ctx) < 512) {
  1220. ret = -EINVAL;
  1221. goto err;
  1222. }
  1223. }
  1224. ctx->streamon_out = 1;
  1225. } else {
  1226. ctx->streamon_cap = 1;
  1227. }
  1228. /* Don't start the coda unless both queues are on */
  1229. if (!(ctx->streamon_out && ctx->streamon_cap))
  1230. goto out;
  1231. q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
  1232. if ((q_data_src->width != q_data_dst->width &&
  1233. round_up(q_data_src->width, 16) != q_data_dst->width) ||
  1234. (q_data_src->height != q_data_dst->height &&
  1235. round_up(q_data_src->height, 16) != q_data_dst->height)) {
  1236. v4l2_err(v4l2_dev, "can't convert %dx%d to %dx%d\n",
  1237. q_data_src->width, q_data_src->height,
  1238. q_data_dst->width, q_data_dst->height);
  1239. ret = -EINVAL;
  1240. goto err;
  1241. }
  1242. /* Allow BIT decoder device_run with no new buffers queued */
  1243. if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit)
  1244. v4l2_m2m_set_src_buffered(ctx->fh.m2m_ctx, true);
  1245. ctx->gopcounter = ctx->params.gop_size - 1;
  1246. ctx->codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
  1247. q_data_dst->fourcc);
  1248. if (!ctx->codec) {
  1249. v4l2_err(v4l2_dev, "couldn't tell instance type.\n");
  1250. ret = -EINVAL;
  1251. goto err;
  1252. }
  1253. if (q_data_dst->fourcc == V4L2_PIX_FMT_JPEG)
  1254. ctx->params.gop_size = 1;
  1255. ctx->gopcounter = ctx->params.gop_size - 1;
  1256. ret = ctx->ops->start_streaming(ctx);
  1257. if (ctx->inst_type == CODA_INST_DECODER) {
  1258. if (ret == -EAGAIN)
  1259. goto out;
  1260. }
  1261. if (ret < 0)
  1262. goto err;
  1263. out:
  1264. if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
  1265. list_for_each_entry_safe(m2m_buf, tmp, &list, list) {
  1266. list_del(&m2m_buf->list);
  1267. v4l2_m2m_buf_done(&m2m_buf->vb, VB2_BUF_STATE_DONE);
  1268. }
  1269. }
  1270. return 0;
  1271. err:
  1272. if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
  1273. list_for_each_entry_safe(m2m_buf, tmp, &list, list) {
  1274. list_del(&m2m_buf->list);
  1275. v4l2_m2m_buf_done(&m2m_buf->vb, VB2_BUF_STATE_QUEUED);
  1276. }
  1277. while ((buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx)))
  1278. v4l2_m2m_buf_done(buf, VB2_BUF_STATE_QUEUED);
  1279. } else {
  1280. while ((buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx)))
  1281. v4l2_m2m_buf_done(buf, VB2_BUF_STATE_QUEUED);
  1282. }
  1283. return ret;
  1284. }
  1285. static void coda_stop_streaming(struct vb2_queue *q)
  1286. {
  1287. struct coda_ctx *ctx = vb2_get_drv_priv(q);
  1288. struct coda_dev *dev = ctx->dev;
  1289. struct vb2_v4l2_buffer *buf;
  1290. unsigned long flags;
  1291. bool stop;
  1292. stop = ctx->streamon_out && ctx->streamon_cap;
  1293. if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
  1294. v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
  1295. "%s: output\n", __func__);
  1296. ctx->streamon_out = 0;
  1297. coda_bit_stream_end_flag(ctx);
  1298. ctx->qsequence = 0;
  1299. while ((buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx)))
  1300. v4l2_m2m_buf_done(buf, VB2_BUF_STATE_ERROR);
  1301. } else {
  1302. v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
  1303. "%s: capture\n", __func__);
  1304. ctx->streamon_cap = 0;
  1305. ctx->osequence = 0;
  1306. ctx->sequence_offset = 0;
  1307. while ((buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx)))
  1308. v4l2_m2m_buf_done(buf, VB2_BUF_STATE_ERROR);
  1309. }
  1310. if (stop) {
  1311. struct coda_buffer_meta *meta;
  1312. if (ctx->ops->seq_end_work) {
  1313. queue_work(dev->workqueue, &ctx->seq_end_work);
  1314. flush_work(&ctx->seq_end_work);
  1315. }
  1316. spin_lock_irqsave(&ctx->buffer_meta_lock, flags);
  1317. while (!list_empty(&ctx->buffer_meta_list)) {
  1318. meta = list_first_entry(&ctx->buffer_meta_list,
  1319. struct coda_buffer_meta, list);
  1320. list_del(&meta->list);
  1321. kfree(meta);
  1322. }
  1323. ctx->num_metas = 0;
  1324. spin_unlock_irqrestore(&ctx->buffer_meta_lock, flags);
  1325. kfifo_init(&ctx->bitstream_fifo,
  1326. ctx->bitstream.vaddr, ctx->bitstream.size);
  1327. ctx->runcounter = 0;
  1328. ctx->aborting = 0;
  1329. }
  1330. if (!ctx->streamon_out && !ctx->streamon_cap)
  1331. ctx->bit_stream_param &= ~CODA_BIT_STREAM_END_FLAG;
  1332. }
  1333. static const struct vb2_ops coda_qops = {
  1334. .queue_setup = coda_queue_setup,
  1335. .buf_prepare = coda_buf_prepare,
  1336. .buf_queue = coda_buf_queue,
  1337. .start_streaming = coda_start_streaming,
  1338. .stop_streaming = coda_stop_streaming,
  1339. .wait_prepare = vb2_ops_wait_prepare,
  1340. .wait_finish = vb2_ops_wait_finish,
  1341. };
  1342. static int coda_s_ctrl(struct v4l2_ctrl *ctrl)
  1343. {
  1344. struct coda_ctx *ctx =
  1345. container_of(ctrl->handler, struct coda_ctx, ctrls);
  1346. v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
  1347. "s_ctrl: id = %d, val = %d\n", ctrl->id, ctrl->val);
  1348. switch (ctrl->id) {
  1349. case V4L2_CID_HFLIP:
  1350. if (ctrl->val)
  1351. ctx->params.rot_mode |= CODA_MIR_HOR;
  1352. else
  1353. ctx->params.rot_mode &= ~CODA_MIR_HOR;
  1354. break;
  1355. case V4L2_CID_VFLIP:
  1356. if (ctrl->val)
  1357. ctx->params.rot_mode |= CODA_MIR_VER;
  1358. else
  1359. ctx->params.rot_mode &= ~CODA_MIR_VER;
  1360. break;
  1361. case V4L2_CID_MPEG_VIDEO_BITRATE:
  1362. ctx->params.bitrate = ctrl->val / 1000;
  1363. break;
  1364. case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
  1365. ctx->params.gop_size = ctrl->val;
  1366. break;
  1367. case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP:
  1368. ctx->params.h264_intra_qp = ctrl->val;
  1369. break;
  1370. case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP:
  1371. ctx->params.h264_inter_qp = ctrl->val;
  1372. break;
  1373. case V4L2_CID_MPEG_VIDEO_H264_MIN_QP:
  1374. ctx->params.h264_min_qp = ctrl->val;
  1375. break;
  1376. case V4L2_CID_MPEG_VIDEO_H264_MAX_QP:
  1377. ctx->params.h264_max_qp = ctrl->val;
  1378. break;
  1379. case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA:
  1380. ctx->params.h264_deblk_alpha = ctrl->val;
  1381. break;
  1382. case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA:
  1383. ctx->params.h264_deblk_beta = ctrl->val;
  1384. break;
  1385. case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE:
  1386. ctx->params.h264_deblk_enabled = (ctrl->val ==
  1387. V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED);
  1388. break;
  1389. case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP:
  1390. ctx->params.mpeg4_intra_qp = ctrl->val;
  1391. break;
  1392. case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP:
  1393. ctx->params.mpeg4_inter_qp = ctrl->val;
  1394. break;
  1395. case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
  1396. ctx->params.slice_mode = ctrl->val;
  1397. break;
  1398. case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB:
  1399. ctx->params.slice_max_mb = ctrl->val;
  1400. break;
  1401. case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES:
  1402. ctx->params.slice_max_bits = ctrl->val * 8;
  1403. break;
  1404. case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
  1405. break;
  1406. case V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB:
  1407. ctx->params.intra_refresh = ctrl->val;
  1408. break;
  1409. case V4L2_CID_JPEG_COMPRESSION_QUALITY:
  1410. coda_set_jpeg_compression_quality(ctx, ctrl->val);
  1411. break;
  1412. case V4L2_CID_JPEG_RESTART_INTERVAL:
  1413. ctx->params.jpeg_restart_interval = ctrl->val;
  1414. break;
  1415. case V4L2_CID_MPEG_VIDEO_VBV_DELAY:
  1416. ctx->params.vbv_delay = ctrl->val;
  1417. break;
  1418. case V4L2_CID_MPEG_VIDEO_VBV_SIZE:
  1419. ctx->params.vbv_size = min(ctrl->val * 8192, 0x7fffffff);
  1420. break;
  1421. default:
  1422. v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
  1423. "Invalid control, id=%d, val=%d\n",
  1424. ctrl->id, ctrl->val);
  1425. return -EINVAL;
  1426. }
  1427. return 0;
  1428. }
  1429. static const struct v4l2_ctrl_ops coda_ctrl_ops = {
  1430. .s_ctrl = coda_s_ctrl,
  1431. };
  1432. static void coda_encode_ctrls(struct coda_ctx *ctx)
  1433. {
  1434. v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
  1435. V4L2_CID_MPEG_VIDEO_BITRATE, 0, 32767000, 1000, 0);
  1436. v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
  1437. V4L2_CID_MPEG_VIDEO_GOP_SIZE, 1, 60, 1, 16);
  1438. v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
  1439. V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP, 0, 51, 1, 25);
  1440. v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
  1441. V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP, 0, 51, 1, 25);
  1442. if (ctx->dev->devtype->product != CODA_960) {
  1443. v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
  1444. V4L2_CID_MPEG_VIDEO_H264_MIN_QP, 0, 51, 1, 12);
  1445. }
  1446. v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
  1447. V4L2_CID_MPEG_VIDEO_H264_MAX_QP, 0, 51, 1, 51);
  1448. v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
  1449. V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA, 0, 15, 1, 0);
  1450. v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
  1451. V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA, 0, 15, 1, 0);
  1452. v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
  1453. V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE,
  1454. V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_DISABLED, 0x0,
  1455. V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED);
  1456. v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
  1457. V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP, 1, 31, 1, 2);
  1458. v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
  1459. V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP, 1, 31, 1, 2);
  1460. v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
  1461. V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE,
  1462. V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES, 0x0,
  1463. V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE);
  1464. v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
  1465. V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB, 1, 0x3fffffff, 1, 1);
  1466. v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
  1467. V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES, 1, 0x3fffffff, 1,
  1468. 500);
  1469. v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
  1470. V4L2_CID_MPEG_VIDEO_HEADER_MODE,
  1471. V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME,
  1472. (1 << V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE),
  1473. V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME);
  1474. v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
  1475. V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB, 0,
  1476. 1920 * 1088 / 256, 1, 0);
  1477. v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
  1478. V4L2_CID_MPEG_VIDEO_VBV_DELAY, 0, 0x7fff, 1, 0);
  1479. /*
  1480. * The maximum VBV size value is 0x7fffffff bits,
  1481. * one bit less than 262144 KiB
  1482. */
  1483. v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
  1484. V4L2_CID_MPEG_VIDEO_VBV_SIZE, 0, 262144, 1, 0);
  1485. }
  1486. static void coda_jpeg_encode_ctrls(struct coda_ctx *ctx)
  1487. {
  1488. v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
  1489. V4L2_CID_JPEG_COMPRESSION_QUALITY, 5, 100, 1, 50);
  1490. v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
  1491. V4L2_CID_JPEG_RESTART_INTERVAL, 0, 100, 1, 0);
  1492. }
  1493. static int coda_ctrls_setup(struct coda_ctx *ctx)
  1494. {
  1495. v4l2_ctrl_handler_init(&ctx->ctrls, 2);
  1496. v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
  1497. V4L2_CID_HFLIP, 0, 1, 1, 0);
  1498. v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
  1499. V4L2_CID_VFLIP, 0, 1, 1, 0);
  1500. if (ctx->inst_type == CODA_INST_ENCODER) {
  1501. if (ctx->cvd->dst_formats[0] == V4L2_PIX_FMT_JPEG)
  1502. coda_jpeg_encode_ctrls(ctx);
  1503. else
  1504. coda_encode_ctrls(ctx);
  1505. }
  1506. if (ctx->ctrls.error) {
  1507. v4l2_err(&ctx->dev->v4l2_dev,
  1508. "control initialization error (%d)",
  1509. ctx->ctrls.error);
  1510. return -EINVAL;
  1511. }
  1512. return v4l2_ctrl_handler_setup(&ctx->ctrls);
  1513. }
  1514. static int coda_queue_init(struct coda_ctx *ctx, struct vb2_queue *vq)
  1515. {
  1516. vq->drv_priv = ctx;
  1517. vq->ops = &coda_qops;
  1518. vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
  1519. vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
  1520. vq->lock = &ctx->dev->dev_mutex;
  1521. /* One way to indicate end-of-stream for coda is to set the
  1522. * bytesused == 0. However by default videobuf2 handles bytesused
  1523. * equal to 0 as a special case and changes its value to the size
  1524. * of the buffer. Set the allow_zero_bytesused flag, so
  1525. * that videobuf2 will keep the value of bytesused intact.
  1526. */
  1527. vq->allow_zero_bytesused = 1;
  1528. vq->dev = &ctx->dev->plat_dev->dev;
  1529. return vb2_queue_init(vq);
  1530. }
  1531. int coda_encoder_queue_init(void *priv, struct vb2_queue *src_vq,
  1532. struct vb2_queue *dst_vq)
  1533. {
  1534. int ret;
  1535. src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  1536. src_vq->io_modes = VB2_DMABUF | VB2_MMAP;
  1537. src_vq->mem_ops = &vb2_dma_contig_memops;
  1538. ret = coda_queue_init(priv, src_vq);
  1539. if (ret)
  1540. return ret;
  1541. dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1542. dst_vq->io_modes = VB2_DMABUF | VB2_MMAP;
  1543. dst_vq->mem_ops = &vb2_dma_contig_memops;
  1544. return coda_queue_init(priv, dst_vq);
  1545. }
  1546. int coda_decoder_queue_init(void *priv, struct vb2_queue *src_vq,
  1547. struct vb2_queue *dst_vq)
  1548. {
  1549. int ret;
  1550. src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  1551. src_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
  1552. src_vq->mem_ops = &vb2_vmalloc_memops;
  1553. ret = coda_queue_init(priv, src_vq);
  1554. if (ret)
  1555. return ret;
  1556. dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1557. dst_vq->io_modes = VB2_DMABUF | VB2_MMAP;
  1558. dst_vq->mem_ops = &vb2_dma_contig_memops;
  1559. return coda_queue_init(priv, dst_vq);
  1560. }
  1561. static int coda_next_free_instance(struct coda_dev *dev)
  1562. {
  1563. int idx = ffz(dev->instance_mask);
  1564. if ((idx < 0) ||
  1565. (dev->devtype->product == CODA_DX6 && idx > CODADX6_MAX_INSTANCES))
  1566. return -EBUSY;
  1567. return idx;
  1568. }
  1569. /*
  1570. * File operations
  1571. */
  1572. static int coda_open(struct file *file)
  1573. {
  1574. struct video_device *vdev = video_devdata(file);
  1575. struct coda_dev *dev = video_get_drvdata(vdev);
  1576. struct coda_ctx *ctx = NULL;
  1577. char *name;
  1578. int ret;
  1579. int idx;
  1580. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  1581. if (!ctx)
  1582. return -ENOMEM;
  1583. idx = coda_next_free_instance(dev);
  1584. if (idx < 0) {
  1585. ret = idx;
  1586. goto err_coda_max;
  1587. }
  1588. set_bit(idx, &dev->instance_mask);
  1589. name = kasprintf(GFP_KERNEL, "context%d", idx);
  1590. if (!name) {
  1591. ret = -ENOMEM;
  1592. goto err_coda_name_init;
  1593. }
  1594. ctx->debugfs_entry = debugfs_create_dir(name, dev->debugfs_root);
  1595. kfree(name);
  1596. ctx->cvd = to_coda_video_device(vdev);
  1597. ctx->inst_type = ctx->cvd->type;
  1598. ctx->ops = ctx->cvd->ops;
  1599. ctx->use_bit = !ctx->cvd->direct;
  1600. init_completion(&ctx->completion);
  1601. INIT_WORK(&ctx->pic_run_work, coda_pic_run_work);
  1602. if (ctx->ops->seq_end_work)
  1603. INIT_WORK(&ctx->seq_end_work, ctx->ops->seq_end_work);
  1604. v4l2_fh_init(&ctx->fh, video_devdata(file));
  1605. file->private_data = &ctx->fh;
  1606. v4l2_fh_add(&ctx->fh);
  1607. ctx->dev = dev;
  1608. ctx->idx = idx;
  1609. switch (dev->devtype->product) {
  1610. case CODA_960:
  1611. if (enable_bwb)
  1612. ctx->frame_mem_ctrl = CODA9_FRAME_ENABLE_BWB;
  1613. /* fallthrough */
  1614. case CODA_7541:
  1615. ctx->reg_idx = 0;
  1616. break;
  1617. default:
  1618. ctx->reg_idx = idx;
  1619. }
  1620. if (ctx->dev->vdoa && !disable_vdoa) {
  1621. ctx->vdoa = vdoa_context_create(dev->vdoa);
  1622. if (!ctx->vdoa)
  1623. v4l2_warn(&dev->v4l2_dev,
  1624. "Failed to create vdoa context: not using vdoa");
  1625. }
  1626. ctx->use_vdoa = false;
  1627. /* Power up and upload firmware if necessary */
  1628. ret = pm_runtime_get_sync(&dev->plat_dev->dev);
  1629. if (ret < 0) {
  1630. v4l2_err(&dev->v4l2_dev, "failed to power up: %d\n", ret);
  1631. goto err_pm_get;
  1632. }
  1633. ret = clk_prepare_enable(dev->clk_per);
  1634. if (ret)
  1635. goto err_clk_per;
  1636. ret = clk_prepare_enable(dev->clk_ahb);
  1637. if (ret)
  1638. goto err_clk_ahb;
  1639. set_default_params(ctx);
  1640. ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx,
  1641. ctx->ops->queue_init);
  1642. if (IS_ERR(ctx->fh.m2m_ctx)) {
  1643. ret = PTR_ERR(ctx->fh.m2m_ctx);
  1644. v4l2_err(&dev->v4l2_dev, "%s return error (%d)\n",
  1645. __func__, ret);
  1646. goto err_ctx_init;
  1647. }
  1648. ret = coda_ctrls_setup(ctx);
  1649. if (ret) {
  1650. v4l2_err(&dev->v4l2_dev, "failed to setup coda controls\n");
  1651. goto err_ctrls_setup;
  1652. }
  1653. ctx->fh.ctrl_handler = &ctx->ctrls;
  1654. mutex_init(&ctx->bitstream_mutex);
  1655. mutex_init(&ctx->buffer_mutex);
  1656. INIT_LIST_HEAD(&ctx->buffer_meta_list);
  1657. spin_lock_init(&ctx->buffer_meta_lock);
  1658. coda_lock(ctx);
  1659. list_add(&ctx->list, &dev->instances);
  1660. coda_unlock(ctx);
  1661. v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Created instance %d (%p)\n",
  1662. ctx->idx, ctx);
  1663. return 0;
  1664. err_ctrls_setup:
  1665. v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
  1666. err_ctx_init:
  1667. clk_disable_unprepare(dev->clk_ahb);
  1668. err_clk_ahb:
  1669. clk_disable_unprepare(dev->clk_per);
  1670. err_clk_per:
  1671. pm_runtime_put_sync(&dev->plat_dev->dev);
  1672. err_pm_get:
  1673. v4l2_fh_del(&ctx->fh);
  1674. v4l2_fh_exit(&ctx->fh);
  1675. clear_bit(ctx->idx, &dev->instance_mask);
  1676. err_coda_name_init:
  1677. err_coda_max:
  1678. kfree(ctx);
  1679. return ret;
  1680. }
  1681. static int coda_release(struct file *file)
  1682. {
  1683. struct coda_dev *dev = video_drvdata(file);
  1684. struct coda_ctx *ctx = fh_to_ctx(file->private_data);
  1685. v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Releasing instance %p\n",
  1686. ctx);
  1687. if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit)
  1688. coda_bit_stream_end_flag(ctx);
  1689. /* If this instance is running, call .job_abort and wait for it to end */
  1690. v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
  1691. if (ctx->vdoa)
  1692. vdoa_context_destroy(ctx->vdoa);
  1693. /* In case the instance was not running, we still need to call SEQ_END */
  1694. if (ctx->ops->seq_end_work) {
  1695. queue_work(dev->workqueue, &ctx->seq_end_work);
  1696. flush_work(&ctx->seq_end_work);
  1697. }
  1698. coda_lock(ctx);
  1699. list_del(&ctx->list);
  1700. coda_unlock(ctx);
  1701. if (ctx->dev->devtype->product == CODA_DX6)
  1702. coda_free_aux_buf(dev, &ctx->workbuf);
  1703. v4l2_ctrl_handler_free(&ctx->ctrls);
  1704. clk_disable_unprepare(dev->clk_ahb);
  1705. clk_disable_unprepare(dev->clk_per);
  1706. pm_runtime_put_sync(&dev->plat_dev->dev);
  1707. v4l2_fh_del(&ctx->fh);
  1708. v4l2_fh_exit(&ctx->fh);
  1709. clear_bit(ctx->idx, &dev->instance_mask);
  1710. if (ctx->ops->release)
  1711. ctx->ops->release(ctx);
  1712. debugfs_remove_recursive(ctx->debugfs_entry);
  1713. kfree(ctx);
  1714. return 0;
  1715. }
  1716. static const struct v4l2_file_operations coda_fops = {
  1717. .owner = THIS_MODULE,
  1718. .open = coda_open,
  1719. .release = coda_release,
  1720. .poll = v4l2_m2m_fop_poll,
  1721. .unlocked_ioctl = video_ioctl2,
  1722. .mmap = v4l2_m2m_fop_mmap,
  1723. };
  1724. static int coda_hw_init(struct coda_dev *dev)
  1725. {
  1726. u32 data;
  1727. u16 *p;
  1728. int i, ret;
  1729. ret = clk_prepare_enable(dev->clk_per);
  1730. if (ret)
  1731. goto err_clk_per;
  1732. ret = clk_prepare_enable(dev->clk_ahb);
  1733. if (ret)
  1734. goto err_clk_ahb;
  1735. if (dev->rstc)
  1736. reset_control_reset(dev->rstc);
  1737. /*
  1738. * Copy the first CODA_ISRAM_SIZE in the internal SRAM.
  1739. * The 16-bit chars in the code buffer are in memory access
  1740. * order, re-sort them to CODA order for register download.
  1741. * Data in this SRAM survives a reboot.
  1742. */
  1743. p = (u16 *)dev->codebuf.vaddr;
  1744. if (dev->devtype->product == CODA_DX6) {
  1745. for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
  1746. data = CODA_DOWN_ADDRESS_SET(i) |
  1747. CODA_DOWN_DATA_SET(p[i ^ 1]);
  1748. coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
  1749. }
  1750. } else {
  1751. for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
  1752. data = CODA_DOWN_ADDRESS_SET(i) |
  1753. CODA_DOWN_DATA_SET(p[round_down(i, 4) +
  1754. 3 - (i % 4)]);
  1755. coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
  1756. }
  1757. }
  1758. /* Clear registers */
  1759. for (i = 0; i < 64; i++)
  1760. coda_write(dev, 0, CODA_REG_BIT_CODE_BUF_ADDR + i * 4);
  1761. /* Tell the BIT where to find everything it needs */
  1762. if (dev->devtype->product == CODA_960 ||
  1763. dev->devtype->product == CODA_7541) {
  1764. coda_write(dev, dev->tempbuf.paddr,
  1765. CODA_REG_BIT_TEMP_BUF_ADDR);
  1766. coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
  1767. } else {
  1768. coda_write(dev, dev->workbuf.paddr,
  1769. CODA_REG_BIT_WORK_BUF_ADDR);
  1770. }
  1771. coda_write(dev, dev->codebuf.paddr,
  1772. CODA_REG_BIT_CODE_BUF_ADDR);
  1773. coda_write(dev, 0, CODA_REG_BIT_CODE_RUN);
  1774. /* Set default values */
  1775. switch (dev->devtype->product) {
  1776. case CODA_DX6:
  1777. coda_write(dev, CODADX6_STREAM_BUF_PIC_FLUSH,
  1778. CODA_REG_BIT_STREAM_CTRL);
  1779. break;
  1780. default:
  1781. coda_write(dev, CODA7_STREAM_BUF_PIC_FLUSH,
  1782. CODA_REG_BIT_STREAM_CTRL);
  1783. }
  1784. if (dev->devtype->product == CODA_960)
  1785. coda_write(dev, 1 << 12, CODA_REG_BIT_FRAME_MEM_CTRL);
  1786. else
  1787. coda_write(dev, 0, CODA_REG_BIT_FRAME_MEM_CTRL);
  1788. if (dev->devtype->product != CODA_DX6)
  1789. coda_write(dev, 0, CODA7_REG_BIT_AXI_SRAM_USE);
  1790. coda_write(dev, CODA_INT_INTERRUPT_ENABLE,
  1791. CODA_REG_BIT_INT_ENABLE);
  1792. /* Reset VPU and start processor */
  1793. data = coda_read(dev, CODA_REG_BIT_CODE_RESET);
  1794. data |= CODA_REG_RESET_ENABLE;
  1795. coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
  1796. udelay(10);
  1797. data &= ~CODA_REG_RESET_ENABLE;
  1798. coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
  1799. coda_write(dev, CODA_REG_RUN_ENABLE, CODA_REG_BIT_CODE_RUN);
  1800. clk_disable_unprepare(dev->clk_ahb);
  1801. clk_disable_unprepare(dev->clk_per);
  1802. return 0;
  1803. err_clk_ahb:
  1804. clk_disable_unprepare(dev->clk_per);
  1805. err_clk_per:
  1806. return ret;
  1807. }
  1808. static int coda_register_device(struct coda_dev *dev, int i)
  1809. {
  1810. struct video_device *vfd = &dev->vfd[i];
  1811. if (i >= dev->devtype->num_vdevs)
  1812. return -EINVAL;
  1813. strlcpy(vfd->name, dev->devtype->vdevs[i]->name, sizeof(vfd->name));
  1814. vfd->fops = &coda_fops;
  1815. vfd->ioctl_ops = &coda_ioctl_ops;
  1816. vfd->release = video_device_release_empty,
  1817. vfd->lock = &dev->dev_mutex;
  1818. vfd->v4l2_dev = &dev->v4l2_dev;
  1819. vfd->vfl_dir = VFL_DIR_M2M;
  1820. video_set_drvdata(vfd, dev);
  1821. /* Not applicable, use the selection API instead */
  1822. v4l2_disable_ioctl(vfd, VIDIOC_CROPCAP);
  1823. v4l2_disable_ioctl(vfd, VIDIOC_G_CROP);
  1824. v4l2_disable_ioctl(vfd, VIDIOC_S_CROP);
  1825. return video_register_device(vfd, VFL_TYPE_GRABBER, 0);
  1826. }
  1827. static void coda_copy_firmware(struct coda_dev *dev, const u8 * const buf,
  1828. size_t size)
  1829. {
  1830. u32 *src = (u32 *)buf;
  1831. /* Check if the firmware has a 16-byte Freescale header, skip it */
  1832. if (buf[0] == 'M' && buf[1] == 'X')
  1833. src += 4;
  1834. /*
  1835. * Check whether the firmware is in native order or pre-reordered for
  1836. * memory access. The first instruction opcode always is 0xe40e.
  1837. */
  1838. if (__le16_to_cpup((__le16 *)src) == 0xe40e) {
  1839. u32 *dst = dev->codebuf.vaddr;
  1840. int i;
  1841. /* Firmware in native order, reorder while copying */
  1842. if (dev->devtype->product == CODA_DX6) {
  1843. for (i = 0; i < (size - 16) / 4; i++)
  1844. dst[i] = (src[i] << 16) | (src[i] >> 16);
  1845. } else {
  1846. for (i = 0; i < (size - 16) / 4; i += 2) {
  1847. dst[i] = (src[i + 1] << 16) | (src[i + 1] >> 16);
  1848. dst[i + 1] = (src[i] << 16) | (src[i] >> 16);
  1849. }
  1850. }
  1851. } else {
  1852. /* Copy the already reordered firmware image */
  1853. memcpy(dev->codebuf.vaddr, src, size);
  1854. }
  1855. }
  1856. static void coda_fw_callback(const struct firmware *fw, void *context);
  1857. static int coda_firmware_request(struct coda_dev *dev)
  1858. {
  1859. char *fw;
  1860. if (dev->firmware >= ARRAY_SIZE(dev->devtype->firmware))
  1861. return -EINVAL;
  1862. fw = dev->devtype->firmware[dev->firmware];
  1863. dev_dbg(&dev->plat_dev->dev, "requesting firmware '%s' for %s\n", fw,
  1864. coda_product_name(dev->devtype->product));
  1865. return request_firmware_nowait(THIS_MODULE, true, fw,
  1866. &dev->plat_dev->dev, GFP_KERNEL, dev,
  1867. coda_fw_callback);
  1868. }
  1869. static void coda_fw_callback(const struct firmware *fw, void *context)
  1870. {
  1871. struct coda_dev *dev = context;
  1872. struct platform_device *pdev = dev->plat_dev;
  1873. int i, ret;
  1874. if (!fw) {
  1875. dev->firmware++;
  1876. ret = coda_firmware_request(dev);
  1877. if (ret < 0) {
  1878. v4l2_err(&dev->v4l2_dev, "firmware request failed\n");
  1879. goto put_pm;
  1880. }
  1881. return;
  1882. }
  1883. if (dev->firmware > 0) {
  1884. /*
  1885. * Since we can't suppress warnings for failed asynchronous
  1886. * firmware requests, report that the fallback firmware was
  1887. * found.
  1888. */
  1889. dev_info(&pdev->dev, "Using fallback firmware %s\n",
  1890. dev->devtype->firmware[dev->firmware]);
  1891. }
  1892. /* allocate auxiliary per-device code buffer for the BIT processor */
  1893. ret = coda_alloc_aux_buf(dev, &dev->codebuf, fw->size, "codebuf",
  1894. dev->debugfs_root);
  1895. if (ret < 0)
  1896. goto put_pm;
  1897. coda_copy_firmware(dev, fw->data, fw->size);
  1898. release_firmware(fw);
  1899. ret = coda_hw_init(dev);
  1900. if (ret < 0) {
  1901. v4l2_err(&dev->v4l2_dev, "HW initialization failed\n");
  1902. goto put_pm;
  1903. }
  1904. ret = coda_check_firmware(dev);
  1905. if (ret < 0)
  1906. goto put_pm;
  1907. dev->m2m_dev = v4l2_m2m_init(&coda_m2m_ops);
  1908. if (IS_ERR(dev->m2m_dev)) {
  1909. v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
  1910. goto put_pm;
  1911. }
  1912. for (i = 0; i < dev->devtype->num_vdevs; i++) {
  1913. ret = coda_register_device(dev, i);
  1914. if (ret) {
  1915. v4l2_err(&dev->v4l2_dev,
  1916. "Failed to register %s video device: %d\n",
  1917. dev->devtype->vdevs[i]->name, ret);
  1918. goto rel_vfd;
  1919. }
  1920. }
  1921. v4l2_info(&dev->v4l2_dev, "codec registered as /dev/video[%d-%d]\n",
  1922. dev->vfd[0].num, dev->vfd[i - 1].num);
  1923. pm_runtime_put_sync(&pdev->dev);
  1924. return;
  1925. rel_vfd:
  1926. while (--i >= 0)
  1927. video_unregister_device(&dev->vfd[i]);
  1928. v4l2_m2m_release(dev->m2m_dev);
  1929. put_pm:
  1930. pm_runtime_put_sync(&pdev->dev);
  1931. }
  1932. enum coda_platform {
  1933. CODA_IMX27,
  1934. CODA_IMX53,
  1935. CODA_IMX6Q,
  1936. CODA_IMX6DL,
  1937. };
  1938. static const struct coda_devtype coda_devdata[] = {
  1939. [CODA_IMX27] = {
  1940. .firmware = {
  1941. "vpu_fw_imx27_TO2.bin",
  1942. "vpu/vpu_fw_imx27_TO2.bin",
  1943. "v4l-codadx6-imx27.bin"
  1944. },
  1945. .product = CODA_DX6,
  1946. .codecs = codadx6_codecs,
  1947. .num_codecs = ARRAY_SIZE(codadx6_codecs),
  1948. .vdevs = codadx6_video_devices,
  1949. .num_vdevs = ARRAY_SIZE(codadx6_video_devices),
  1950. .workbuf_size = 288 * 1024 + FMO_SLICE_SAVE_BUF_SIZE * 8 * 1024,
  1951. .iram_size = 0xb000,
  1952. },
  1953. [CODA_IMX53] = {
  1954. .firmware = {
  1955. "vpu_fw_imx53.bin",
  1956. "vpu/vpu_fw_imx53.bin",
  1957. "v4l-coda7541-imx53.bin"
  1958. },
  1959. .product = CODA_7541,
  1960. .codecs = coda7_codecs,
  1961. .num_codecs = ARRAY_SIZE(coda7_codecs),
  1962. .vdevs = coda7_video_devices,
  1963. .num_vdevs = ARRAY_SIZE(coda7_video_devices),
  1964. .workbuf_size = 128 * 1024,
  1965. .tempbuf_size = 304 * 1024,
  1966. .iram_size = 0x14000,
  1967. },
  1968. [CODA_IMX6Q] = {
  1969. .firmware = {
  1970. "vpu_fw_imx6q.bin",
  1971. "vpu/vpu_fw_imx6q.bin",
  1972. "v4l-coda960-imx6q.bin"
  1973. },
  1974. .product = CODA_960,
  1975. .codecs = coda9_codecs,
  1976. .num_codecs = ARRAY_SIZE(coda9_codecs),
  1977. .vdevs = coda9_video_devices,
  1978. .num_vdevs = ARRAY_SIZE(coda9_video_devices),
  1979. .workbuf_size = 80 * 1024,
  1980. .tempbuf_size = 204 * 1024,
  1981. .iram_size = 0x21000,
  1982. },
  1983. [CODA_IMX6DL] = {
  1984. .firmware = {
  1985. "vpu_fw_imx6d.bin",
  1986. "vpu/vpu_fw_imx6d.bin",
  1987. "v4l-coda960-imx6dl.bin"
  1988. },
  1989. .product = CODA_960,
  1990. .codecs = coda9_codecs,
  1991. .num_codecs = ARRAY_SIZE(coda9_codecs),
  1992. .vdevs = coda9_video_devices,
  1993. .num_vdevs = ARRAY_SIZE(coda9_video_devices),
  1994. .workbuf_size = 80 * 1024,
  1995. .tempbuf_size = 204 * 1024,
  1996. .iram_size = 0x20000,
  1997. },
  1998. };
  1999. static struct platform_device_id coda_platform_ids[] = {
  2000. { .name = "coda-imx27", .driver_data = CODA_IMX27 },
  2001. { /* sentinel */ }
  2002. };
  2003. MODULE_DEVICE_TABLE(platform, coda_platform_ids);
  2004. #ifdef CONFIG_OF
  2005. static const struct of_device_id coda_dt_ids[] = {
  2006. { .compatible = "fsl,imx27-vpu", .data = &coda_devdata[CODA_IMX27] },
  2007. { .compatible = "fsl,imx53-vpu", .data = &coda_devdata[CODA_IMX53] },
  2008. { .compatible = "fsl,imx6q-vpu", .data = &coda_devdata[CODA_IMX6Q] },
  2009. { .compatible = "fsl,imx6dl-vpu", .data = &coda_devdata[CODA_IMX6DL] },
  2010. { /* sentinel */ }
  2011. };
  2012. MODULE_DEVICE_TABLE(of, coda_dt_ids);
  2013. #endif
  2014. static int coda_probe(struct platform_device *pdev)
  2015. {
  2016. const struct of_device_id *of_id =
  2017. of_match_device(of_match_ptr(coda_dt_ids), &pdev->dev);
  2018. const struct platform_device_id *pdev_id;
  2019. struct coda_platform_data *pdata = pdev->dev.platform_data;
  2020. struct device_node *np = pdev->dev.of_node;
  2021. struct gen_pool *pool;
  2022. struct coda_dev *dev;
  2023. struct resource *res;
  2024. int ret, irq;
  2025. dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
  2026. if (!dev)
  2027. return -ENOMEM;
  2028. pdev_id = of_id ? of_id->data : platform_get_device_id(pdev);
  2029. if (of_id)
  2030. dev->devtype = of_id->data;
  2031. else if (pdev_id)
  2032. dev->devtype = &coda_devdata[pdev_id->driver_data];
  2033. else
  2034. return -EINVAL;
  2035. spin_lock_init(&dev->irqlock);
  2036. INIT_LIST_HEAD(&dev->instances);
  2037. dev->plat_dev = pdev;
  2038. dev->clk_per = devm_clk_get(&pdev->dev, "per");
  2039. if (IS_ERR(dev->clk_per)) {
  2040. dev_err(&pdev->dev, "Could not get per clock\n");
  2041. return PTR_ERR(dev->clk_per);
  2042. }
  2043. dev->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
  2044. if (IS_ERR(dev->clk_ahb)) {
  2045. dev_err(&pdev->dev, "Could not get ahb clock\n");
  2046. return PTR_ERR(dev->clk_ahb);
  2047. }
  2048. /* Get memory for physical registers */
  2049. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  2050. dev->regs_base = devm_ioremap_resource(&pdev->dev, res);
  2051. if (IS_ERR(dev->regs_base))
  2052. return PTR_ERR(dev->regs_base);
  2053. /* IRQ */
  2054. irq = platform_get_irq_byname(pdev, "bit");
  2055. if (irq < 0)
  2056. irq = platform_get_irq(pdev, 0);
  2057. if (irq < 0) {
  2058. dev_err(&pdev->dev, "failed to get irq resource\n");
  2059. return irq;
  2060. }
  2061. ret = devm_request_threaded_irq(&pdev->dev, irq, NULL, coda_irq_handler,
  2062. IRQF_ONESHOT, dev_name(&pdev->dev), dev);
  2063. if (ret < 0) {
  2064. dev_err(&pdev->dev, "failed to request irq: %d\n", ret);
  2065. return ret;
  2066. }
  2067. dev->rstc = devm_reset_control_get_optional(&pdev->dev, NULL);
  2068. if (IS_ERR(dev->rstc)) {
  2069. ret = PTR_ERR(dev->rstc);
  2070. if (ret == -ENOENT || ret == -ENOTSUPP) {
  2071. dev->rstc = NULL;
  2072. } else {
  2073. dev_err(&pdev->dev, "failed get reset control: %d\n",
  2074. ret);
  2075. return ret;
  2076. }
  2077. }
  2078. /* Get IRAM pool from device tree or platform data */
  2079. pool = of_gen_pool_get(np, "iram", 0);
  2080. if (!pool && pdata)
  2081. pool = gen_pool_get(pdata->iram_dev, NULL);
  2082. if (!pool) {
  2083. dev_err(&pdev->dev, "iram pool not available\n");
  2084. return -ENOMEM;
  2085. }
  2086. dev->iram_pool = pool;
  2087. /* Get vdoa_data if supported by the platform */
  2088. dev->vdoa = coda_get_vdoa_data();
  2089. if (PTR_ERR(dev->vdoa) == -EPROBE_DEFER)
  2090. return -EPROBE_DEFER;
  2091. ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
  2092. if (ret)
  2093. return ret;
  2094. mutex_init(&dev->dev_mutex);
  2095. mutex_init(&dev->coda_mutex);
  2096. dev->debugfs_root = debugfs_create_dir("coda", NULL);
  2097. if (!dev->debugfs_root)
  2098. dev_warn(&pdev->dev, "failed to create debugfs root\n");
  2099. /* allocate auxiliary per-device buffers for the BIT processor */
  2100. if (dev->devtype->product == CODA_DX6) {
  2101. ret = coda_alloc_aux_buf(dev, &dev->workbuf,
  2102. dev->devtype->workbuf_size, "workbuf",
  2103. dev->debugfs_root);
  2104. if (ret < 0)
  2105. goto err_v4l2_register;
  2106. }
  2107. if (dev->devtype->tempbuf_size) {
  2108. ret = coda_alloc_aux_buf(dev, &dev->tempbuf,
  2109. dev->devtype->tempbuf_size, "tempbuf",
  2110. dev->debugfs_root);
  2111. if (ret < 0)
  2112. goto err_v4l2_register;
  2113. }
  2114. dev->iram.size = dev->devtype->iram_size;
  2115. dev->iram.vaddr = gen_pool_dma_alloc(dev->iram_pool, dev->iram.size,
  2116. &dev->iram.paddr);
  2117. if (!dev->iram.vaddr) {
  2118. dev_warn(&pdev->dev, "unable to alloc iram\n");
  2119. } else {
  2120. memset(dev->iram.vaddr, 0, dev->iram.size);
  2121. dev->iram.blob.data = dev->iram.vaddr;
  2122. dev->iram.blob.size = dev->iram.size;
  2123. dev->iram.dentry = debugfs_create_blob("iram", 0644,
  2124. dev->debugfs_root,
  2125. &dev->iram.blob);
  2126. }
  2127. dev->workqueue = alloc_workqueue("coda", WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
  2128. if (!dev->workqueue) {
  2129. dev_err(&pdev->dev, "unable to alloc workqueue\n");
  2130. ret = -ENOMEM;
  2131. goto err_v4l2_register;
  2132. }
  2133. platform_set_drvdata(pdev, dev);
  2134. /*
  2135. * Start activated so we can directly call coda_hw_init in
  2136. * coda_fw_callback regardless of whether CONFIG_PM is
  2137. * enabled or whether the device is associated with a PM domain.
  2138. */
  2139. pm_runtime_get_noresume(&pdev->dev);
  2140. pm_runtime_set_active(&pdev->dev);
  2141. pm_runtime_enable(&pdev->dev);
  2142. ret = coda_firmware_request(dev);
  2143. if (ret)
  2144. goto err_alloc_workqueue;
  2145. return 0;
  2146. err_alloc_workqueue:
  2147. destroy_workqueue(dev->workqueue);
  2148. err_v4l2_register:
  2149. v4l2_device_unregister(&dev->v4l2_dev);
  2150. return ret;
  2151. }
  2152. static int coda_remove(struct platform_device *pdev)
  2153. {
  2154. struct coda_dev *dev = platform_get_drvdata(pdev);
  2155. int i;
  2156. for (i = 0; i < ARRAY_SIZE(dev->vfd); i++) {
  2157. if (video_get_drvdata(&dev->vfd[i]))
  2158. video_unregister_device(&dev->vfd[i]);
  2159. }
  2160. if (dev->m2m_dev)
  2161. v4l2_m2m_release(dev->m2m_dev);
  2162. pm_runtime_disable(&pdev->dev);
  2163. v4l2_device_unregister(&dev->v4l2_dev);
  2164. destroy_workqueue(dev->workqueue);
  2165. if (dev->iram.vaddr)
  2166. gen_pool_free(dev->iram_pool, (unsigned long)dev->iram.vaddr,
  2167. dev->iram.size);
  2168. coda_free_aux_buf(dev, &dev->codebuf);
  2169. coda_free_aux_buf(dev, &dev->tempbuf);
  2170. coda_free_aux_buf(dev, &dev->workbuf);
  2171. debugfs_remove_recursive(dev->debugfs_root);
  2172. return 0;
  2173. }
  2174. #ifdef CONFIG_PM
  2175. static int coda_runtime_resume(struct device *dev)
  2176. {
  2177. struct coda_dev *cdev = dev_get_drvdata(dev);
  2178. int ret = 0;
  2179. if (dev->pm_domain && cdev->codebuf.vaddr) {
  2180. ret = coda_hw_init(cdev);
  2181. if (ret)
  2182. v4l2_err(&cdev->v4l2_dev, "HW initialization failed\n");
  2183. }
  2184. return ret;
  2185. }
  2186. #endif
  2187. static const struct dev_pm_ops coda_pm_ops = {
  2188. SET_RUNTIME_PM_OPS(NULL, coda_runtime_resume, NULL)
  2189. };
  2190. static struct platform_driver coda_driver = {
  2191. .probe = coda_probe,
  2192. .remove = coda_remove,
  2193. .driver = {
  2194. .name = CODA_NAME,
  2195. .of_match_table = of_match_ptr(coda_dt_ids),
  2196. .pm = &coda_pm_ops,
  2197. },
  2198. .id_table = coda_platform_ids,
  2199. };
  2200. module_platform_driver(coda_driver);
  2201. MODULE_LICENSE("GPL");
  2202. MODULE_AUTHOR("Javier Martin <javier.martin@vista-silicon.com>");
  2203. MODULE_DESCRIPTION("Coda multi-standard codec V4L2 driver");