coda-common.c 72 KB

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