coda-common.c 91 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Coda multi-standard codec IP
  4. *
  5. * Copyright (C) 2012 Vista Silicon S.L.
  6. * Javier Martin, <[email protected]>
  7. * Xavier Duret
  8. */
  9. #include <linux/clk.h>
  10. #include <linux/debugfs.h>
  11. #include <linux/delay.h>
  12. #include <linux/firmware.h>
  13. #include <linux/gcd.h>
  14. #include <linux/genalloc.h>
  15. #include <linux/idr.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/io.h>
  18. #include <linux/irq.h>
  19. #include <linux/kfifo.h>
  20. #include <linux/module.h>
  21. #include <linux/of_device.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/pm_runtime.h>
  24. #include <linux/slab.h>
  25. #include <linux/videodev2.h>
  26. #include <linux/of.h>
  27. #include <linux/ratelimit.h>
  28. #include <linux/reset.h>
  29. #include <media/v4l2-ctrls.h>
  30. #include <media/v4l2-device.h>
  31. #include <media/v4l2-event.h>
  32. #include <media/v4l2-ioctl.h>
  33. #include <media/v4l2-mem2mem.h>
  34. #include <media/videobuf2-v4l2.h>
  35. #include <media/videobuf2-dma-contig.h>
  36. #include <media/videobuf2-vmalloc.h>
  37. #include "coda.h"
  38. #include "imx-vdoa.h"
  39. #define CODA_NAME "coda"
  40. #define CODADX6_MAX_INSTANCES 4
  41. #define CODA_MAX_FORMATS 5
  42. #define CODA_ISRAM_SIZE (2048 * 2)
  43. #define MIN_W 48
  44. #define MIN_H 16
  45. #define S_ALIGN 1 /* multiple of 2 */
  46. #define W_ALIGN 1 /* multiple of 2 */
  47. #define H_ALIGN 1 /* multiple of 2 */
  48. #define fh_to_ctx(__fh) container_of(__fh, struct coda_ctx, fh)
  49. int coda_debug;
  50. module_param(coda_debug, int, 0644);
  51. MODULE_PARM_DESC(coda_debug, "Debug level (0-2)");
  52. static int disable_tiling;
  53. module_param(disable_tiling, int, 0644);
  54. MODULE_PARM_DESC(disable_tiling, "Disable tiled frame buffers");
  55. static int disable_vdoa;
  56. module_param(disable_vdoa, int, 0644);
  57. MODULE_PARM_DESC(disable_vdoa, "Disable Video Data Order Adapter tiled to raster-scan conversion");
  58. static int enable_bwb = 0;
  59. module_param(enable_bwb, int, 0644);
  60. MODULE_PARM_DESC(enable_bwb, "Enable BWB unit for decoding, may crash on certain streams");
  61. void coda_write(struct coda_dev *dev, u32 data, u32 reg)
  62. {
  63. v4l2_dbg(3, coda_debug, &dev->v4l2_dev,
  64. "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
  65. writel(data, dev->regs_base + reg);
  66. }
  67. unsigned int coda_read(struct coda_dev *dev, u32 reg)
  68. {
  69. u32 data;
  70. data = readl(dev->regs_base + reg);
  71. v4l2_dbg(3, coda_debug, &dev->v4l2_dev,
  72. "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
  73. return data;
  74. }
  75. void coda_write_base(struct coda_ctx *ctx, struct coda_q_data *q_data,
  76. struct vb2_v4l2_buffer *buf, unsigned int reg_y)
  77. {
  78. u32 base_y = vb2_dma_contig_plane_dma_addr(&buf->vb2_buf, 0);
  79. u32 base_cb, base_cr;
  80. switch (q_data->fourcc) {
  81. case V4L2_PIX_FMT_YUYV:
  82. /* Fallthrough: IN -H264-> CODA -NV12 MB-> VDOA -YUYV-> OUT */
  83. case V4L2_PIX_FMT_NV12:
  84. case V4L2_PIX_FMT_YUV420:
  85. default:
  86. base_cb = base_y + q_data->bytesperline * q_data->height;
  87. base_cr = base_cb + q_data->bytesperline * q_data->height / 4;
  88. break;
  89. case V4L2_PIX_FMT_YVU420:
  90. /* Switch Cb and Cr for YVU420 format */
  91. base_cr = base_y + q_data->bytesperline * q_data->height;
  92. base_cb = base_cr + q_data->bytesperline * q_data->height / 4;
  93. break;
  94. case V4L2_PIX_FMT_YUV422P:
  95. base_cb = base_y + q_data->bytesperline * q_data->height;
  96. base_cr = base_cb + q_data->bytesperline * q_data->height / 2;
  97. }
  98. coda_write(ctx->dev, base_y, reg_y);
  99. coda_write(ctx->dev, base_cb, reg_y + 4);
  100. coda_write(ctx->dev, base_cr, reg_y + 8);
  101. }
  102. #define CODA_CODEC(mode, src_fourcc, dst_fourcc, max_w, max_h) \
  103. { mode, src_fourcc, dst_fourcc, max_w, max_h }
  104. /*
  105. * Arrays of codecs supported by each given version of Coda:
  106. * i.MX27 -> codadx6
  107. * i.MX51 -> codahx4
  108. * i.MX53 -> coda7
  109. * i.MX6 -> coda960
  110. * Use V4L2_PIX_FMT_YUV420 as placeholder for all supported YUV 4:2:0 variants
  111. */
  112. static const struct coda_codec codadx6_codecs[] = {
  113. CODA_CODEC(CODADX6_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 720, 576),
  114. CODA_CODEC(CODADX6_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 720, 576),
  115. };
  116. static const struct coda_codec codahx4_codecs[] = {
  117. CODA_CODEC(CODA7_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 720, 576),
  118. CODA_CODEC(CODA7_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1088),
  119. CODA_CODEC(CODA7_MODE_DECODE_MP2, V4L2_PIX_FMT_MPEG2, V4L2_PIX_FMT_YUV420, 1920, 1088),
  120. CODA_CODEC(CODA7_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1280, 720),
  121. };
  122. static const struct coda_codec coda7_codecs[] = {
  123. CODA_CODEC(CODA7_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 1280, 720),
  124. CODA_CODEC(CODA7_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 1280, 720),
  125. CODA_CODEC(CODA7_MODE_ENCODE_MJPG, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_JPEG, 8192, 8192),
  126. CODA_CODEC(CODA7_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1088),
  127. CODA_CODEC(CODA7_MODE_DECODE_MP2, V4L2_PIX_FMT_MPEG2, V4L2_PIX_FMT_YUV420, 1920, 1088),
  128. CODA_CODEC(CODA7_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1920, 1088),
  129. CODA_CODEC(CODA7_MODE_DECODE_MJPG, V4L2_PIX_FMT_JPEG, V4L2_PIX_FMT_YUV420, 8192, 8192),
  130. };
  131. static const struct coda_codec coda9_codecs[] = {
  132. CODA_CODEC(CODA9_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 1920, 1088),
  133. CODA_CODEC(CODA9_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 1920, 1088),
  134. CODA_CODEC(CODA9_MODE_ENCODE_MJPG, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_JPEG, 8192, 8192),
  135. CODA_CODEC(CODA9_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1088),
  136. CODA_CODEC(CODA9_MODE_DECODE_MP2, V4L2_PIX_FMT_MPEG2, V4L2_PIX_FMT_YUV420, 1920, 1088),
  137. CODA_CODEC(CODA9_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1920, 1088),
  138. CODA_CODEC(CODA9_MODE_DECODE_MJPG, V4L2_PIX_FMT_JPEG, V4L2_PIX_FMT_YUV420, 8192, 8192),
  139. };
  140. struct coda_video_device {
  141. const char *name;
  142. enum coda_inst_type type;
  143. const struct coda_context_ops *ops;
  144. bool direct;
  145. u32 src_formats[CODA_MAX_FORMATS];
  146. u32 dst_formats[CODA_MAX_FORMATS];
  147. };
  148. static const struct coda_video_device coda_bit_encoder = {
  149. .name = "coda-video-encoder",
  150. .type = CODA_INST_ENCODER,
  151. .ops = &coda_bit_encode_ops,
  152. .src_formats = {
  153. V4L2_PIX_FMT_NV12,
  154. V4L2_PIX_FMT_YUV420,
  155. V4L2_PIX_FMT_YVU420,
  156. },
  157. .dst_formats = {
  158. V4L2_PIX_FMT_H264,
  159. V4L2_PIX_FMT_MPEG4,
  160. },
  161. };
  162. static const struct coda_video_device coda_bit_jpeg_encoder = {
  163. .name = "coda-jpeg-encoder",
  164. .type = CODA_INST_ENCODER,
  165. .ops = &coda_bit_encode_ops,
  166. .src_formats = {
  167. V4L2_PIX_FMT_NV12,
  168. V4L2_PIX_FMT_YUV420,
  169. V4L2_PIX_FMT_YVU420,
  170. V4L2_PIX_FMT_YUV422P,
  171. },
  172. .dst_formats = {
  173. V4L2_PIX_FMT_JPEG,
  174. },
  175. };
  176. static const struct coda_video_device coda_bit_decoder = {
  177. .name = "coda-video-decoder",
  178. .type = CODA_INST_DECODER,
  179. .ops = &coda_bit_decode_ops,
  180. .src_formats = {
  181. V4L2_PIX_FMT_H264,
  182. V4L2_PIX_FMT_MPEG2,
  183. V4L2_PIX_FMT_MPEG4,
  184. },
  185. .dst_formats = {
  186. V4L2_PIX_FMT_NV12,
  187. V4L2_PIX_FMT_YUV420,
  188. V4L2_PIX_FMT_YVU420,
  189. /*
  190. * If V4L2_PIX_FMT_YUYV should be default,
  191. * set_default_params() must be adjusted.
  192. */
  193. V4L2_PIX_FMT_YUYV,
  194. },
  195. };
  196. static const struct coda_video_device coda_bit_jpeg_decoder = {
  197. .name = "coda-jpeg-decoder",
  198. .type = CODA_INST_DECODER,
  199. .ops = &coda_bit_decode_ops,
  200. .src_formats = {
  201. V4L2_PIX_FMT_JPEG,
  202. },
  203. .dst_formats = {
  204. V4L2_PIX_FMT_NV12,
  205. V4L2_PIX_FMT_YUV420,
  206. V4L2_PIX_FMT_YVU420,
  207. V4L2_PIX_FMT_YUV422P,
  208. },
  209. };
  210. static const struct coda_video_device coda9_jpeg_encoder = {
  211. .name = "coda-jpeg-encoder",
  212. .type = CODA_INST_ENCODER,
  213. .ops = &coda9_jpeg_encode_ops,
  214. .direct = true,
  215. .src_formats = {
  216. V4L2_PIX_FMT_NV12,
  217. V4L2_PIX_FMT_YUV420,
  218. V4L2_PIX_FMT_YVU420,
  219. V4L2_PIX_FMT_YUV422P,
  220. V4L2_PIX_FMT_GREY,
  221. },
  222. .dst_formats = {
  223. V4L2_PIX_FMT_JPEG,
  224. },
  225. };
  226. static const struct coda_video_device coda9_jpeg_decoder = {
  227. .name = "coda-jpeg-decoder",
  228. .type = CODA_INST_DECODER,
  229. .ops = &coda9_jpeg_decode_ops,
  230. .direct = true,
  231. .src_formats = {
  232. V4L2_PIX_FMT_JPEG,
  233. },
  234. .dst_formats = {
  235. V4L2_PIX_FMT_NV12,
  236. V4L2_PIX_FMT_YUV420,
  237. V4L2_PIX_FMT_YVU420,
  238. V4L2_PIX_FMT_YUV422P,
  239. },
  240. };
  241. static const struct coda_video_device *codadx6_video_devices[] = {
  242. &coda_bit_encoder,
  243. };
  244. static const struct coda_video_device *codahx4_video_devices[] = {
  245. &coda_bit_encoder,
  246. &coda_bit_decoder,
  247. };
  248. static const struct coda_video_device *coda7_video_devices[] = {
  249. &coda_bit_jpeg_encoder,
  250. &coda_bit_jpeg_decoder,
  251. &coda_bit_encoder,
  252. &coda_bit_decoder,
  253. };
  254. static const struct coda_video_device *coda9_video_devices[] = {
  255. &coda9_jpeg_encoder,
  256. &coda9_jpeg_decoder,
  257. &coda_bit_encoder,
  258. &coda_bit_decoder,
  259. };
  260. /*
  261. * Normalize all supported YUV 4:2:0 formats to the value used in the codec
  262. * tables.
  263. */
  264. static u32 coda_format_normalize_yuv(u32 fourcc)
  265. {
  266. switch (fourcc) {
  267. case V4L2_PIX_FMT_NV12:
  268. case V4L2_PIX_FMT_YUV420:
  269. case V4L2_PIX_FMT_YVU420:
  270. case V4L2_PIX_FMT_YUV422P:
  271. case V4L2_PIX_FMT_YUYV:
  272. return V4L2_PIX_FMT_YUV420;
  273. default:
  274. return fourcc;
  275. }
  276. }
  277. static const struct coda_codec *coda_find_codec(struct coda_dev *dev,
  278. int src_fourcc, int dst_fourcc)
  279. {
  280. const struct coda_codec *codecs = dev->devtype->codecs;
  281. int num_codecs = dev->devtype->num_codecs;
  282. int k;
  283. src_fourcc = coda_format_normalize_yuv(src_fourcc);
  284. dst_fourcc = coda_format_normalize_yuv(dst_fourcc);
  285. if (src_fourcc == dst_fourcc)
  286. return NULL;
  287. for (k = 0; k < num_codecs; k++) {
  288. if (codecs[k].src_fourcc == src_fourcc &&
  289. codecs[k].dst_fourcc == dst_fourcc)
  290. break;
  291. }
  292. if (k == num_codecs)
  293. return NULL;
  294. return &codecs[k];
  295. }
  296. static void coda_get_max_dimensions(struct coda_dev *dev,
  297. const struct coda_codec *codec,
  298. int *max_w, int *max_h)
  299. {
  300. const struct coda_codec *codecs = dev->devtype->codecs;
  301. int num_codecs = dev->devtype->num_codecs;
  302. unsigned int w, h;
  303. int k;
  304. if (codec) {
  305. w = codec->max_w;
  306. h = codec->max_h;
  307. } else {
  308. for (k = 0, w = 0, h = 0; k < num_codecs; k++) {
  309. w = max(w, codecs[k].max_w);
  310. h = max(h, codecs[k].max_h);
  311. }
  312. }
  313. if (max_w)
  314. *max_w = w;
  315. if (max_h)
  316. *max_h = h;
  317. }
  318. static const struct coda_video_device *to_coda_video_device(struct video_device
  319. *vdev)
  320. {
  321. struct coda_dev *dev = video_get_drvdata(vdev);
  322. unsigned int i = vdev - dev->vfd;
  323. if (i >= dev->devtype->num_vdevs)
  324. return NULL;
  325. return dev->devtype->vdevs[i];
  326. }
  327. const char *coda_product_name(int product)
  328. {
  329. static char buf[9];
  330. switch (product) {
  331. case CODA_DX6:
  332. return "CodaDx6";
  333. case CODA_HX4:
  334. return "CodaHx4";
  335. case CODA_7541:
  336. return "CODA7541";
  337. case CODA_960:
  338. return "CODA960";
  339. default:
  340. snprintf(buf, sizeof(buf), "(0x%04x)", product);
  341. return buf;
  342. }
  343. }
  344. static struct vdoa_data *coda_get_vdoa_data(void)
  345. {
  346. struct device_node *vdoa_node;
  347. struct platform_device *vdoa_pdev;
  348. struct vdoa_data *vdoa_data = NULL;
  349. vdoa_node = of_find_compatible_node(NULL, NULL, "fsl,imx6q-vdoa");
  350. if (!vdoa_node)
  351. return NULL;
  352. vdoa_pdev = of_find_device_by_node(vdoa_node);
  353. if (!vdoa_pdev)
  354. goto out;
  355. vdoa_data = platform_get_drvdata(vdoa_pdev);
  356. if (!vdoa_data)
  357. vdoa_data = ERR_PTR(-EPROBE_DEFER);
  358. put_device(&vdoa_pdev->dev);
  359. out:
  360. of_node_put(vdoa_node);
  361. return vdoa_data;
  362. }
  363. /*
  364. * V4L2 ioctl() operations.
  365. */
  366. static int coda_querycap(struct file *file, void *priv,
  367. struct v4l2_capability *cap)
  368. {
  369. struct coda_ctx *ctx = fh_to_ctx(priv);
  370. strscpy(cap->driver, CODA_NAME, sizeof(cap->driver));
  371. strscpy(cap->card, coda_product_name(ctx->dev->devtype->product),
  372. sizeof(cap->card));
  373. strscpy(cap->bus_info, "platform:" CODA_NAME, sizeof(cap->bus_info));
  374. return 0;
  375. }
  376. static const u32 coda_formats_420[CODA_MAX_FORMATS] = {
  377. V4L2_PIX_FMT_NV12,
  378. V4L2_PIX_FMT_YUV420,
  379. V4L2_PIX_FMT_YVU420,
  380. };
  381. static int coda_enum_fmt(struct file *file, void *priv,
  382. struct v4l2_fmtdesc *f)
  383. {
  384. struct video_device *vdev = video_devdata(file);
  385. const struct coda_video_device *cvd = to_coda_video_device(vdev);
  386. struct coda_ctx *ctx = fh_to_ctx(priv);
  387. const u32 *formats;
  388. if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
  389. formats = cvd->src_formats;
  390. else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  391. struct coda_q_data *q_data_src;
  392. struct vb2_queue *src_vq;
  393. formats = cvd->dst_formats;
  394. /*
  395. * If the source format is already fixed, only allow the same
  396. * chroma subsampling.
  397. */
  398. q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
  399. src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx,
  400. V4L2_BUF_TYPE_VIDEO_OUTPUT);
  401. if (q_data_src->fourcc == V4L2_PIX_FMT_JPEG &&
  402. vb2_is_streaming(src_vq)) {
  403. if (ctx->params.jpeg_chroma_subsampling ==
  404. V4L2_JPEG_CHROMA_SUBSAMPLING_420) {
  405. formats = coda_formats_420;
  406. } else if (ctx->params.jpeg_chroma_subsampling ==
  407. V4L2_JPEG_CHROMA_SUBSAMPLING_422) {
  408. f->pixelformat = V4L2_PIX_FMT_YUV422P;
  409. return f->index ? -EINVAL : 0;
  410. }
  411. }
  412. } else {
  413. return -EINVAL;
  414. }
  415. if (f->index >= CODA_MAX_FORMATS || formats[f->index] == 0)
  416. return -EINVAL;
  417. /* Skip YUYV if the vdoa is not available */
  418. if (!ctx->vdoa && f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
  419. formats[f->index] == V4L2_PIX_FMT_YUYV)
  420. return -EINVAL;
  421. f->pixelformat = formats[f->index];
  422. return 0;
  423. }
  424. static int coda_g_fmt(struct file *file, void *priv,
  425. struct v4l2_format *f)
  426. {
  427. struct coda_q_data *q_data;
  428. struct coda_ctx *ctx = fh_to_ctx(priv);
  429. q_data = get_q_data(ctx, f->type);
  430. if (!q_data)
  431. return -EINVAL;
  432. f->fmt.pix.field = V4L2_FIELD_NONE;
  433. f->fmt.pix.pixelformat = q_data->fourcc;
  434. f->fmt.pix.width = q_data->width;
  435. f->fmt.pix.height = q_data->height;
  436. f->fmt.pix.bytesperline = q_data->bytesperline;
  437. f->fmt.pix.sizeimage = q_data->sizeimage;
  438. f->fmt.pix.colorspace = ctx->colorspace;
  439. f->fmt.pix.xfer_func = ctx->xfer_func;
  440. f->fmt.pix.ycbcr_enc = ctx->ycbcr_enc;
  441. f->fmt.pix.quantization = ctx->quantization;
  442. return 0;
  443. }
  444. static int coda_try_pixelformat(struct coda_ctx *ctx, struct v4l2_format *f)
  445. {
  446. struct coda_q_data *q_data;
  447. const u32 *formats;
  448. int i;
  449. if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
  450. formats = ctx->cvd->src_formats;
  451. else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  452. formats = ctx->cvd->dst_formats;
  453. else
  454. return -EINVAL;
  455. for (i = 0; i < CODA_MAX_FORMATS; i++) {
  456. /* Skip YUYV if the vdoa is not available */
  457. if (!ctx->vdoa && f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
  458. formats[i] == V4L2_PIX_FMT_YUYV)
  459. continue;
  460. if (formats[i] == f->fmt.pix.pixelformat) {
  461. f->fmt.pix.pixelformat = formats[i];
  462. return 0;
  463. }
  464. }
  465. /* Fall back to currently set pixelformat */
  466. q_data = get_q_data(ctx, f->type);
  467. f->fmt.pix.pixelformat = q_data->fourcc;
  468. return 0;
  469. }
  470. static int coda_try_fmt_vdoa(struct coda_ctx *ctx, struct v4l2_format *f,
  471. bool *use_vdoa)
  472. {
  473. int err;
  474. if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  475. return -EINVAL;
  476. if (!use_vdoa)
  477. return -EINVAL;
  478. if (!ctx->vdoa) {
  479. *use_vdoa = false;
  480. return 0;
  481. }
  482. err = vdoa_context_configure(NULL, round_up(f->fmt.pix.width, 16),
  483. f->fmt.pix.height, f->fmt.pix.pixelformat);
  484. if (err) {
  485. *use_vdoa = false;
  486. return 0;
  487. }
  488. *use_vdoa = true;
  489. return 0;
  490. }
  491. static unsigned int coda_estimate_sizeimage(struct coda_ctx *ctx, u32 sizeimage,
  492. u32 width, u32 height)
  493. {
  494. /*
  495. * This is a rough estimate for sensible compressed buffer
  496. * sizes (between 1 and 16 bits per pixel). This could be
  497. * improved by better format specific worst case estimates.
  498. */
  499. return round_up(clamp(sizeimage, width * height / 8,
  500. width * height * 2), PAGE_SIZE);
  501. }
  502. static int coda_try_fmt(struct coda_ctx *ctx, const struct coda_codec *codec,
  503. struct v4l2_format *f)
  504. {
  505. struct coda_dev *dev = ctx->dev;
  506. unsigned int max_w, max_h;
  507. enum v4l2_field field;
  508. field = f->fmt.pix.field;
  509. if (field == V4L2_FIELD_ANY)
  510. field = V4L2_FIELD_NONE;
  511. else if (V4L2_FIELD_NONE != field)
  512. return -EINVAL;
  513. /* V4L2 specification suggests the driver corrects the format struct
  514. * if any of the dimensions is unsupported */
  515. f->fmt.pix.field = field;
  516. coda_get_max_dimensions(dev, codec, &max_w, &max_h);
  517. v4l_bound_align_image(&f->fmt.pix.width, MIN_W, max_w, W_ALIGN,
  518. &f->fmt.pix.height, MIN_H, max_h, H_ALIGN,
  519. S_ALIGN);
  520. switch (f->fmt.pix.pixelformat) {
  521. case V4L2_PIX_FMT_NV12:
  522. case V4L2_PIX_FMT_YUV420:
  523. case V4L2_PIX_FMT_YVU420:
  524. /*
  525. * Frame stride must be at least multiple of 8,
  526. * but multiple of 16 for h.264 or JPEG 4:2:x
  527. */
  528. f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
  529. f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
  530. f->fmt.pix.height * 3 / 2;
  531. break;
  532. case V4L2_PIX_FMT_YUYV:
  533. f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16) * 2;
  534. f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
  535. f->fmt.pix.height;
  536. break;
  537. case V4L2_PIX_FMT_YUV422P:
  538. f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
  539. f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
  540. f->fmt.pix.height * 2;
  541. break;
  542. case V4L2_PIX_FMT_GREY:
  543. /* keep 16 pixel alignment of 8-bit pixel data */
  544. f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
  545. f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * f->fmt.pix.height;
  546. break;
  547. case V4L2_PIX_FMT_JPEG:
  548. case V4L2_PIX_FMT_H264:
  549. case V4L2_PIX_FMT_MPEG4:
  550. case V4L2_PIX_FMT_MPEG2:
  551. f->fmt.pix.bytesperline = 0;
  552. f->fmt.pix.sizeimage = coda_estimate_sizeimage(ctx,
  553. f->fmt.pix.sizeimage,
  554. f->fmt.pix.width,
  555. f->fmt.pix.height);
  556. break;
  557. default:
  558. BUG();
  559. }
  560. return 0;
  561. }
  562. static int coda_try_fmt_vid_cap(struct file *file, void *priv,
  563. struct v4l2_format *f)
  564. {
  565. struct coda_ctx *ctx = fh_to_ctx(priv);
  566. const struct coda_q_data *q_data_src;
  567. const struct coda_codec *codec;
  568. struct vb2_queue *src_vq;
  569. int hscale = 0;
  570. int vscale = 0;
  571. int ret;
  572. bool use_vdoa;
  573. ret = coda_try_pixelformat(ctx, f);
  574. if (ret < 0)
  575. return ret;
  576. q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
  577. /*
  578. * If the source format is already fixed, only allow the same output
  579. * resolution. When decoding JPEG images, we also have to make sure to
  580. * use the same chroma subsampling.
  581. */
  582. src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
  583. if (vb2_is_streaming(src_vq)) {
  584. if (q_data_src->fourcc == V4L2_PIX_FMT_JPEG &&
  585. ctx->dev->devtype->product == CODA_960) {
  586. hscale = coda_jpeg_scale(q_data_src->width, f->fmt.pix.width);
  587. vscale = coda_jpeg_scale(q_data_src->height, f->fmt.pix.height);
  588. }
  589. f->fmt.pix.width = q_data_src->width >> hscale;
  590. f->fmt.pix.height = q_data_src->height >> vscale;
  591. if (q_data_src->fourcc == V4L2_PIX_FMT_JPEG) {
  592. if (ctx->params.jpeg_chroma_subsampling ==
  593. V4L2_JPEG_CHROMA_SUBSAMPLING_420 &&
  594. f->fmt.pix.pixelformat == V4L2_PIX_FMT_YUV422P)
  595. f->fmt.pix.pixelformat = V4L2_PIX_FMT_NV12;
  596. else if (ctx->params.jpeg_chroma_subsampling ==
  597. V4L2_JPEG_CHROMA_SUBSAMPLING_422)
  598. f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUV422P;
  599. }
  600. }
  601. f->fmt.pix.colorspace = ctx->colorspace;
  602. f->fmt.pix.xfer_func = ctx->xfer_func;
  603. f->fmt.pix.ycbcr_enc = ctx->ycbcr_enc;
  604. f->fmt.pix.quantization = ctx->quantization;
  605. q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
  606. codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
  607. f->fmt.pix.pixelformat);
  608. if (!codec)
  609. return -EINVAL;
  610. ret = coda_try_fmt(ctx, codec, f);
  611. if (ret < 0)
  612. return ret;
  613. /* The decoders always write complete macroblocks or MCUs */
  614. if (ctx->inst_type == CODA_INST_DECODER) {
  615. f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16 >> hscale);
  616. f->fmt.pix.height = round_up(f->fmt.pix.height, 16 >> vscale);
  617. if (codec->src_fourcc == V4L2_PIX_FMT_JPEG &&
  618. f->fmt.pix.pixelformat == V4L2_PIX_FMT_YUV422P) {
  619. f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
  620. f->fmt.pix.height * 2;
  621. } else {
  622. f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
  623. f->fmt.pix.height * 3 / 2;
  624. }
  625. ret = coda_try_fmt_vdoa(ctx, f, &use_vdoa);
  626. if (ret < 0)
  627. return ret;
  628. if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_YUYV) {
  629. if (!use_vdoa)
  630. return -EINVAL;
  631. f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16) * 2;
  632. f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
  633. f->fmt.pix.height;
  634. }
  635. }
  636. return 0;
  637. }
  638. static void coda_set_default_colorspace(struct v4l2_pix_format *fmt)
  639. {
  640. enum v4l2_colorspace colorspace;
  641. if (fmt->pixelformat == V4L2_PIX_FMT_JPEG)
  642. colorspace = V4L2_COLORSPACE_JPEG;
  643. else if (fmt->width <= 720 && fmt->height <= 576)
  644. colorspace = V4L2_COLORSPACE_SMPTE170M;
  645. else
  646. colorspace = V4L2_COLORSPACE_REC709;
  647. fmt->colorspace = colorspace;
  648. fmt->xfer_func = V4L2_XFER_FUNC_DEFAULT;
  649. fmt->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
  650. fmt->quantization = V4L2_QUANTIZATION_DEFAULT;
  651. }
  652. static int coda_try_fmt_vid_out(struct file *file, void *priv,
  653. struct v4l2_format *f)
  654. {
  655. struct coda_ctx *ctx = fh_to_ctx(priv);
  656. struct coda_dev *dev = ctx->dev;
  657. const struct coda_q_data *q_data_dst;
  658. const struct coda_codec *codec;
  659. int ret;
  660. ret = coda_try_pixelformat(ctx, f);
  661. if (ret < 0)
  662. return ret;
  663. if (f->fmt.pix.colorspace == V4L2_COLORSPACE_DEFAULT)
  664. coda_set_default_colorspace(&f->fmt.pix);
  665. q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
  666. codec = coda_find_codec(dev, f->fmt.pix.pixelformat, q_data_dst->fourcc);
  667. return coda_try_fmt(ctx, codec, f);
  668. }
  669. static int coda_s_fmt(struct coda_ctx *ctx, struct v4l2_format *f,
  670. struct v4l2_rect *r)
  671. {
  672. struct coda_q_data *q_data;
  673. struct vb2_queue *vq;
  674. vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
  675. if (!vq)
  676. return -EINVAL;
  677. q_data = get_q_data(ctx, f->type);
  678. if (!q_data)
  679. return -EINVAL;
  680. if (vb2_is_busy(vq)) {
  681. v4l2_err(&ctx->dev->v4l2_dev, "%s: %s queue busy: %d\n",
  682. __func__, v4l2_type_names[f->type], vq->num_buffers);
  683. return -EBUSY;
  684. }
  685. q_data->fourcc = f->fmt.pix.pixelformat;
  686. q_data->width = f->fmt.pix.width;
  687. q_data->height = f->fmt.pix.height;
  688. q_data->bytesperline = f->fmt.pix.bytesperline;
  689. q_data->sizeimage = f->fmt.pix.sizeimage;
  690. if (r) {
  691. q_data->rect = *r;
  692. } else {
  693. q_data->rect.left = 0;
  694. q_data->rect.top = 0;
  695. q_data->rect.width = f->fmt.pix.width;
  696. q_data->rect.height = f->fmt.pix.height;
  697. }
  698. switch (f->fmt.pix.pixelformat) {
  699. case V4L2_PIX_FMT_YUYV:
  700. ctx->tiled_map_type = GDI_TILED_FRAME_MB_RASTER_MAP;
  701. break;
  702. case V4L2_PIX_FMT_NV12:
  703. if (!disable_tiling && ctx->use_bit &&
  704. ctx->dev->devtype->product == CODA_960) {
  705. ctx->tiled_map_type = GDI_TILED_FRAME_MB_RASTER_MAP;
  706. break;
  707. }
  708. fallthrough;
  709. case V4L2_PIX_FMT_YUV420:
  710. case V4L2_PIX_FMT_YVU420:
  711. case V4L2_PIX_FMT_YUV422P:
  712. ctx->tiled_map_type = GDI_LINEAR_FRAME_MAP;
  713. break;
  714. default:
  715. break;
  716. }
  717. if (ctx->tiled_map_type == GDI_TILED_FRAME_MB_RASTER_MAP &&
  718. !coda_try_fmt_vdoa(ctx, f, &ctx->use_vdoa) &&
  719. ctx->use_vdoa)
  720. vdoa_context_configure(ctx->vdoa,
  721. round_up(f->fmt.pix.width, 16),
  722. f->fmt.pix.height,
  723. f->fmt.pix.pixelformat);
  724. else
  725. ctx->use_vdoa = false;
  726. coda_dbg(1, ctx, "Setting %s format, wxh: %dx%d, fmt: %4.4s %c\n",
  727. v4l2_type_names[f->type], q_data->width, q_data->height,
  728. (char *)&q_data->fourcc,
  729. (ctx->tiled_map_type == GDI_LINEAR_FRAME_MAP) ? 'L' : 'T');
  730. return 0;
  731. }
  732. static int coda_s_fmt_vid_cap(struct file *file, void *priv,
  733. struct v4l2_format *f)
  734. {
  735. struct coda_ctx *ctx = fh_to_ctx(priv);
  736. struct coda_q_data *q_data_src;
  737. const struct coda_codec *codec;
  738. struct v4l2_rect r;
  739. int hscale = 0;
  740. int vscale = 0;
  741. int ret;
  742. q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
  743. if (q_data_src->fourcc == V4L2_PIX_FMT_JPEG &&
  744. ctx->dev->devtype->product == CODA_960) {
  745. hscale = coda_jpeg_scale(q_data_src->width, f->fmt.pix.width);
  746. vscale = coda_jpeg_scale(q_data_src->height, f->fmt.pix.height);
  747. }
  748. ret = coda_try_fmt_vid_cap(file, priv, f);
  749. if (ret)
  750. return ret;
  751. r.left = 0;
  752. r.top = 0;
  753. r.width = q_data_src->width >> hscale;
  754. r.height = q_data_src->height >> vscale;
  755. ret = coda_s_fmt(ctx, f, &r);
  756. if (ret)
  757. return ret;
  758. if (ctx->inst_type != CODA_INST_ENCODER)
  759. return 0;
  760. /* Setting the coded format determines the selected codec */
  761. codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
  762. f->fmt.pix.pixelformat);
  763. if (!codec) {
  764. v4l2_err(&ctx->dev->v4l2_dev, "failed to determine codec\n");
  765. return -EINVAL;
  766. }
  767. ctx->codec = codec;
  768. ctx->colorspace = f->fmt.pix.colorspace;
  769. ctx->xfer_func = f->fmt.pix.xfer_func;
  770. ctx->ycbcr_enc = f->fmt.pix.ycbcr_enc;
  771. ctx->quantization = f->fmt.pix.quantization;
  772. return 0;
  773. }
  774. static int coda_s_fmt_vid_out(struct file *file, void *priv,
  775. struct v4l2_format *f)
  776. {
  777. struct coda_ctx *ctx = fh_to_ctx(priv);
  778. const struct coda_codec *codec;
  779. struct v4l2_format f_cap;
  780. struct vb2_queue *dst_vq;
  781. int ret;
  782. ret = coda_try_fmt_vid_out(file, priv, f);
  783. if (ret)
  784. return ret;
  785. ret = coda_s_fmt(ctx, f, NULL);
  786. if (ret)
  787. return ret;
  788. ctx->colorspace = f->fmt.pix.colorspace;
  789. ctx->xfer_func = f->fmt.pix.xfer_func;
  790. ctx->ycbcr_enc = f->fmt.pix.ycbcr_enc;
  791. ctx->quantization = f->fmt.pix.quantization;
  792. if (ctx->inst_type != CODA_INST_DECODER)
  793. return 0;
  794. /* Setting the coded format determines the selected codec */
  795. codec = coda_find_codec(ctx->dev, f->fmt.pix.pixelformat,
  796. V4L2_PIX_FMT_YUV420);
  797. if (!codec) {
  798. v4l2_err(&ctx->dev->v4l2_dev, "failed to determine codec\n");
  799. return -EINVAL;
  800. }
  801. ctx->codec = codec;
  802. dst_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
  803. if (!dst_vq)
  804. return -EINVAL;
  805. /*
  806. * Setting the capture queue format is not possible while the capture
  807. * queue is still busy. This is not an error, but the user will have to
  808. * make sure themselves that the capture format is set correctly before
  809. * starting the output queue again.
  810. */
  811. if (vb2_is_busy(dst_vq))
  812. return 0;
  813. memset(&f_cap, 0, sizeof(f_cap));
  814. f_cap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  815. coda_g_fmt(file, priv, &f_cap);
  816. f_cap.fmt.pix.width = f->fmt.pix.width;
  817. f_cap.fmt.pix.height = f->fmt.pix.height;
  818. return coda_s_fmt_vid_cap(file, priv, &f_cap);
  819. }
  820. static int coda_reqbufs(struct file *file, void *priv,
  821. struct v4l2_requestbuffers *rb)
  822. {
  823. struct coda_ctx *ctx = fh_to_ctx(priv);
  824. int ret;
  825. ret = v4l2_m2m_reqbufs(file, ctx->fh.m2m_ctx, rb);
  826. if (ret)
  827. return ret;
  828. /*
  829. * Allow to allocate instance specific per-context buffers, such as
  830. * bitstream ringbuffer, slice buffer, work buffer, etc. if needed.
  831. */
  832. if (rb->type == V4L2_BUF_TYPE_VIDEO_OUTPUT && ctx->ops->reqbufs)
  833. return ctx->ops->reqbufs(ctx, rb);
  834. return 0;
  835. }
  836. static int coda_qbuf(struct file *file, void *priv,
  837. struct v4l2_buffer *buf)
  838. {
  839. struct coda_ctx *ctx = fh_to_ctx(priv);
  840. if (ctx->inst_type == CODA_INST_DECODER &&
  841. buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
  842. buf->flags &= ~V4L2_BUF_FLAG_LAST;
  843. return v4l2_m2m_qbuf(file, ctx->fh.m2m_ctx, buf);
  844. }
  845. static int coda_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
  846. {
  847. struct coda_ctx *ctx = fh_to_ctx(priv);
  848. int ret;
  849. ret = v4l2_m2m_dqbuf(file, ctx->fh.m2m_ctx, buf);
  850. if (ctx->inst_type == CODA_INST_DECODER &&
  851. buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
  852. buf->flags &= ~V4L2_BUF_FLAG_LAST;
  853. return ret;
  854. }
  855. void coda_m2m_buf_done(struct coda_ctx *ctx, struct vb2_v4l2_buffer *buf,
  856. enum vb2_buffer_state state)
  857. {
  858. const struct v4l2_event eos_event = {
  859. .type = V4L2_EVENT_EOS
  860. };
  861. if (buf->flags & V4L2_BUF_FLAG_LAST)
  862. v4l2_event_queue_fh(&ctx->fh, &eos_event);
  863. v4l2_m2m_buf_done(buf, state);
  864. }
  865. static int coda_g_selection(struct file *file, void *fh,
  866. struct v4l2_selection *s)
  867. {
  868. struct coda_ctx *ctx = fh_to_ctx(fh);
  869. struct coda_q_data *q_data;
  870. struct v4l2_rect r, *rsel;
  871. q_data = get_q_data(ctx, s->type);
  872. if (!q_data)
  873. return -EINVAL;
  874. r.left = 0;
  875. r.top = 0;
  876. r.width = q_data->width;
  877. r.height = q_data->height;
  878. rsel = &q_data->rect;
  879. switch (s->target) {
  880. case V4L2_SEL_TGT_CROP_DEFAULT:
  881. case V4L2_SEL_TGT_CROP_BOUNDS:
  882. rsel = &r;
  883. fallthrough;
  884. case V4L2_SEL_TGT_CROP:
  885. if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT ||
  886. ctx->inst_type == CODA_INST_DECODER)
  887. return -EINVAL;
  888. break;
  889. case V4L2_SEL_TGT_COMPOSE_BOUNDS:
  890. case V4L2_SEL_TGT_COMPOSE_PADDED:
  891. rsel = &r;
  892. fallthrough;
  893. case V4L2_SEL_TGT_COMPOSE:
  894. case V4L2_SEL_TGT_COMPOSE_DEFAULT:
  895. if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  896. ctx->inst_type == CODA_INST_ENCODER)
  897. return -EINVAL;
  898. break;
  899. default:
  900. return -EINVAL;
  901. }
  902. s->r = *rsel;
  903. return 0;
  904. }
  905. static int coda_s_selection(struct file *file, void *fh,
  906. struct v4l2_selection *s)
  907. {
  908. struct coda_ctx *ctx = fh_to_ctx(fh);
  909. struct coda_q_data *q_data;
  910. switch (s->target) {
  911. case V4L2_SEL_TGT_CROP:
  912. if (ctx->inst_type == CODA_INST_ENCODER &&
  913. s->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
  914. q_data = get_q_data(ctx, s->type);
  915. if (!q_data)
  916. return -EINVAL;
  917. s->r.left = 0;
  918. s->r.top = 0;
  919. s->r.width = clamp(s->r.width, 2U, q_data->width);
  920. s->r.height = clamp(s->r.height, 2U, q_data->height);
  921. if (s->flags & V4L2_SEL_FLAG_LE) {
  922. s->r.width = round_up(s->r.width, 2);
  923. s->r.height = round_up(s->r.height, 2);
  924. } else {
  925. s->r.width = round_down(s->r.width, 2);
  926. s->r.height = round_down(s->r.height, 2);
  927. }
  928. q_data->rect = s->r;
  929. coda_dbg(1, ctx, "Setting crop rectangle: %dx%d\n",
  930. s->r.width, s->r.height);
  931. return 0;
  932. }
  933. fallthrough;
  934. case V4L2_SEL_TGT_NATIVE_SIZE:
  935. case V4L2_SEL_TGT_COMPOSE:
  936. return coda_g_selection(file, fh, s);
  937. default:
  938. /* v4l2-compliance expects this to fail for read-only targets */
  939. return -EINVAL;
  940. }
  941. }
  942. static void coda_wake_up_capture_queue(struct coda_ctx *ctx)
  943. {
  944. struct vb2_queue *dst_vq;
  945. coda_dbg(1, ctx, "waking up capture queue\n");
  946. dst_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
  947. dst_vq->last_buffer_dequeued = true;
  948. wake_up(&dst_vq->done_wq);
  949. }
  950. static int coda_encoder_cmd(struct file *file, void *fh,
  951. struct v4l2_encoder_cmd *ec)
  952. {
  953. struct coda_ctx *ctx = fh_to_ctx(fh);
  954. struct vb2_v4l2_buffer *buf;
  955. int ret;
  956. ret = v4l2_m2m_ioctl_try_encoder_cmd(file, fh, ec);
  957. if (ret < 0)
  958. return ret;
  959. mutex_lock(&ctx->wakeup_mutex);
  960. buf = v4l2_m2m_last_src_buf(ctx->fh.m2m_ctx);
  961. if (buf) {
  962. /*
  963. * If the last output buffer is still on the queue, make sure
  964. * that decoder finish_run will see the last flag and report it
  965. * to userspace.
  966. */
  967. buf->flags |= V4L2_BUF_FLAG_LAST;
  968. } else {
  969. /* Set the stream-end flag on this context */
  970. ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
  971. /*
  972. * If the last output buffer has already been taken from the
  973. * queue, wake up the capture queue and signal end of stream
  974. * via the -EPIPE mechanism.
  975. */
  976. coda_wake_up_capture_queue(ctx);
  977. }
  978. mutex_unlock(&ctx->wakeup_mutex);
  979. return 0;
  980. }
  981. static bool coda_mark_last_meta(struct coda_ctx *ctx)
  982. {
  983. struct coda_buffer_meta *meta;
  984. coda_dbg(1, ctx, "marking last meta\n");
  985. spin_lock(&ctx->buffer_meta_lock);
  986. if (list_empty(&ctx->buffer_meta_list)) {
  987. spin_unlock(&ctx->buffer_meta_lock);
  988. return false;
  989. }
  990. meta = list_last_entry(&ctx->buffer_meta_list, struct coda_buffer_meta,
  991. list);
  992. meta->last = true;
  993. spin_unlock(&ctx->buffer_meta_lock);
  994. return true;
  995. }
  996. static bool coda_mark_last_dst_buf(struct coda_ctx *ctx)
  997. {
  998. struct vb2_v4l2_buffer *buf;
  999. struct vb2_buffer *dst_vb;
  1000. struct vb2_queue *dst_vq;
  1001. unsigned long flags;
  1002. coda_dbg(1, ctx, "marking last capture buffer\n");
  1003. dst_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
  1004. spin_lock_irqsave(&dst_vq->done_lock, flags);
  1005. if (list_empty(&dst_vq->done_list)) {
  1006. spin_unlock_irqrestore(&dst_vq->done_lock, flags);
  1007. return false;
  1008. }
  1009. dst_vb = list_last_entry(&dst_vq->done_list, struct vb2_buffer,
  1010. done_entry);
  1011. buf = to_vb2_v4l2_buffer(dst_vb);
  1012. buf->flags |= V4L2_BUF_FLAG_LAST;
  1013. spin_unlock_irqrestore(&dst_vq->done_lock, flags);
  1014. return true;
  1015. }
  1016. static int coda_decoder_cmd(struct file *file, void *fh,
  1017. struct v4l2_decoder_cmd *dc)
  1018. {
  1019. struct coda_ctx *ctx = fh_to_ctx(fh);
  1020. struct coda_dev *dev = ctx->dev;
  1021. struct vb2_v4l2_buffer *buf;
  1022. struct vb2_queue *dst_vq;
  1023. bool stream_end;
  1024. bool wakeup;
  1025. int ret;
  1026. ret = v4l2_m2m_ioctl_try_decoder_cmd(file, fh, dc);
  1027. if (ret < 0)
  1028. return ret;
  1029. switch (dc->cmd) {
  1030. case V4L2_DEC_CMD_START:
  1031. mutex_lock(&dev->coda_mutex);
  1032. mutex_lock(&ctx->bitstream_mutex);
  1033. coda_bitstream_flush(ctx);
  1034. dst_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx,
  1035. V4L2_BUF_TYPE_VIDEO_CAPTURE);
  1036. vb2_clear_last_buffer_dequeued(dst_vq);
  1037. ctx->bit_stream_param &= ~CODA_BIT_STREAM_END_FLAG;
  1038. coda_fill_bitstream(ctx, NULL);
  1039. mutex_unlock(&ctx->bitstream_mutex);
  1040. mutex_unlock(&dev->coda_mutex);
  1041. break;
  1042. case V4L2_DEC_CMD_STOP:
  1043. stream_end = false;
  1044. wakeup = false;
  1045. mutex_lock(&ctx->wakeup_mutex);
  1046. buf = v4l2_m2m_last_src_buf(ctx->fh.m2m_ctx);
  1047. if (buf) {
  1048. coda_dbg(1, ctx, "marking last pending buffer\n");
  1049. /* Mark last buffer */
  1050. buf->flags |= V4L2_BUF_FLAG_LAST;
  1051. if (v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx) == 0) {
  1052. coda_dbg(1, ctx, "all remaining buffers queued\n");
  1053. stream_end = true;
  1054. }
  1055. } else {
  1056. if (ctx->use_bit)
  1057. if (coda_mark_last_meta(ctx))
  1058. stream_end = true;
  1059. else
  1060. wakeup = true;
  1061. else
  1062. if (!coda_mark_last_dst_buf(ctx))
  1063. wakeup = true;
  1064. }
  1065. if (stream_end) {
  1066. coda_dbg(1, ctx, "all remaining buffers queued\n");
  1067. /* Set the stream-end flag on this context */
  1068. coda_bit_stream_end_flag(ctx);
  1069. ctx->hold = false;
  1070. v4l2_m2m_try_schedule(ctx->fh.m2m_ctx);
  1071. }
  1072. if (wakeup) {
  1073. /* If there is no buffer in flight, wake up */
  1074. coda_wake_up_capture_queue(ctx);
  1075. }
  1076. mutex_unlock(&ctx->wakeup_mutex);
  1077. break;
  1078. default:
  1079. return -EINVAL;
  1080. }
  1081. return 0;
  1082. }
  1083. static int coda_enum_framesizes(struct file *file, void *fh,
  1084. struct v4l2_frmsizeenum *fsize)
  1085. {
  1086. struct coda_ctx *ctx = fh_to_ctx(fh);
  1087. struct coda_q_data *q_data_dst;
  1088. const struct coda_codec *codec;
  1089. if (fsize->index)
  1090. return -EINVAL;
  1091. if (coda_format_normalize_yuv(fsize->pixel_format) ==
  1092. V4L2_PIX_FMT_YUV420) {
  1093. q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
  1094. codec = coda_find_codec(ctx->dev, fsize->pixel_format,
  1095. q_data_dst->fourcc);
  1096. } else {
  1097. codec = coda_find_codec(ctx->dev, V4L2_PIX_FMT_YUV420,
  1098. fsize->pixel_format);
  1099. }
  1100. if (!codec)
  1101. return -EINVAL;
  1102. fsize->type = V4L2_FRMSIZE_TYPE_CONTINUOUS;
  1103. fsize->stepwise.min_width = MIN_W;
  1104. fsize->stepwise.max_width = codec->max_w;
  1105. fsize->stepwise.step_width = 1;
  1106. fsize->stepwise.min_height = MIN_H;
  1107. fsize->stepwise.max_height = codec->max_h;
  1108. fsize->stepwise.step_height = 1;
  1109. return 0;
  1110. }
  1111. static int coda_enum_frameintervals(struct file *file, void *fh,
  1112. struct v4l2_frmivalenum *f)
  1113. {
  1114. struct coda_ctx *ctx = fh_to_ctx(fh);
  1115. struct coda_q_data *q_data;
  1116. const struct coda_codec *codec;
  1117. if (f->index)
  1118. return -EINVAL;
  1119. /* Disallow YUYV if the vdoa is not available */
  1120. if (!ctx->vdoa && f->pixel_format == V4L2_PIX_FMT_YUYV)
  1121. return -EINVAL;
  1122. if (coda_format_normalize_yuv(f->pixel_format) == V4L2_PIX_FMT_YUV420) {
  1123. q_data = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
  1124. codec = coda_find_codec(ctx->dev, f->pixel_format,
  1125. q_data->fourcc);
  1126. } else {
  1127. codec = coda_find_codec(ctx->dev, V4L2_PIX_FMT_YUV420,
  1128. f->pixel_format);
  1129. }
  1130. if (!codec)
  1131. return -EINVAL;
  1132. if (f->width < MIN_W || f->width > codec->max_w ||
  1133. f->height < MIN_H || f->height > codec->max_h)
  1134. return -EINVAL;
  1135. f->type = V4L2_FRMIVAL_TYPE_CONTINUOUS;
  1136. f->stepwise.min.numerator = 1;
  1137. f->stepwise.min.denominator = 65535;
  1138. f->stepwise.max.numerator = 65536;
  1139. f->stepwise.max.denominator = 1;
  1140. f->stepwise.step.numerator = 1;
  1141. f->stepwise.step.denominator = 1;
  1142. return 0;
  1143. }
  1144. static int coda_g_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
  1145. {
  1146. struct coda_ctx *ctx = fh_to_ctx(fh);
  1147. struct v4l2_fract *tpf;
  1148. if (a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
  1149. return -EINVAL;
  1150. a->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
  1151. tpf = &a->parm.output.timeperframe;
  1152. tpf->denominator = ctx->params.framerate & CODA_FRATE_RES_MASK;
  1153. tpf->numerator = 1 + (ctx->params.framerate >>
  1154. CODA_FRATE_DIV_OFFSET);
  1155. return 0;
  1156. }
  1157. /*
  1158. * Approximate timeperframe v4l2_fract with values that can be written
  1159. * into the 16-bit CODA_FRATE_DIV and CODA_FRATE_RES fields.
  1160. */
  1161. static void coda_approximate_timeperframe(struct v4l2_fract *timeperframe)
  1162. {
  1163. struct v4l2_fract s = *timeperframe;
  1164. struct v4l2_fract f0;
  1165. struct v4l2_fract f1 = { 1, 0 };
  1166. struct v4l2_fract f2 = { 0, 1 };
  1167. unsigned int i, div, s_denominator;
  1168. /* Lower bound is 1/65535 */
  1169. if (s.numerator == 0 || s.denominator / s.numerator > 65535) {
  1170. timeperframe->numerator = 1;
  1171. timeperframe->denominator = 65535;
  1172. return;
  1173. }
  1174. /* Upper bound is 65536/1 */
  1175. if (s.denominator == 0 || s.numerator / s.denominator > 65536) {
  1176. timeperframe->numerator = 65536;
  1177. timeperframe->denominator = 1;
  1178. return;
  1179. }
  1180. /* Reduce fraction to lowest terms */
  1181. div = gcd(s.numerator, s.denominator);
  1182. if (div > 1) {
  1183. s.numerator /= div;
  1184. s.denominator /= div;
  1185. }
  1186. if (s.numerator <= 65536 && s.denominator < 65536) {
  1187. *timeperframe = s;
  1188. return;
  1189. }
  1190. /* Find successive convergents from continued fraction expansion */
  1191. while (f2.numerator <= 65536 && f2.denominator < 65536) {
  1192. f0 = f1;
  1193. f1 = f2;
  1194. /* Stop when f2 exactly equals timeperframe */
  1195. if (s.numerator == 0)
  1196. break;
  1197. i = s.denominator / s.numerator;
  1198. f2.numerator = f0.numerator + i * f1.numerator;
  1199. f2.denominator = f0.denominator + i * f2.denominator;
  1200. s_denominator = s.numerator;
  1201. s.numerator = s.denominator % s.numerator;
  1202. s.denominator = s_denominator;
  1203. }
  1204. *timeperframe = f1;
  1205. }
  1206. static uint32_t coda_timeperframe_to_frate(struct v4l2_fract *timeperframe)
  1207. {
  1208. return ((timeperframe->numerator - 1) << CODA_FRATE_DIV_OFFSET) |
  1209. timeperframe->denominator;
  1210. }
  1211. static int coda_s_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
  1212. {
  1213. struct coda_ctx *ctx = fh_to_ctx(fh);
  1214. struct v4l2_fract *tpf;
  1215. if (a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
  1216. return -EINVAL;
  1217. a->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
  1218. tpf = &a->parm.output.timeperframe;
  1219. coda_approximate_timeperframe(tpf);
  1220. ctx->params.framerate = coda_timeperframe_to_frate(tpf);
  1221. ctx->params.framerate_changed = true;
  1222. return 0;
  1223. }
  1224. static int coda_subscribe_event(struct v4l2_fh *fh,
  1225. const struct v4l2_event_subscription *sub)
  1226. {
  1227. struct coda_ctx *ctx = fh_to_ctx(fh);
  1228. switch (sub->type) {
  1229. case V4L2_EVENT_EOS:
  1230. return v4l2_event_subscribe(fh, sub, 0, NULL);
  1231. case V4L2_EVENT_SOURCE_CHANGE:
  1232. if (ctx->inst_type == CODA_INST_DECODER)
  1233. return v4l2_event_subscribe(fh, sub, 0, NULL);
  1234. else
  1235. return -EINVAL;
  1236. default:
  1237. return v4l2_ctrl_subscribe_event(fh, sub);
  1238. }
  1239. }
  1240. static const struct v4l2_ioctl_ops coda_ioctl_ops = {
  1241. .vidioc_querycap = coda_querycap,
  1242. .vidioc_enum_fmt_vid_cap = coda_enum_fmt,
  1243. .vidioc_g_fmt_vid_cap = coda_g_fmt,
  1244. .vidioc_try_fmt_vid_cap = coda_try_fmt_vid_cap,
  1245. .vidioc_s_fmt_vid_cap = coda_s_fmt_vid_cap,
  1246. .vidioc_enum_fmt_vid_out = coda_enum_fmt,
  1247. .vidioc_g_fmt_vid_out = coda_g_fmt,
  1248. .vidioc_try_fmt_vid_out = coda_try_fmt_vid_out,
  1249. .vidioc_s_fmt_vid_out = coda_s_fmt_vid_out,
  1250. .vidioc_reqbufs = coda_reqbufs,
  1251. .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
  1252. .vidioc_qbuf = coda_qbuf,
  1253. .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
  1254. .vidioc_dqbuf = coda_dqbuf,
  1255. .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
  1256. .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
  1257. .vidioc_streamon = v4l2_m2m_ioctl_streamon,
  1258. .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
  1259. .vidioc_g_selection = coda_g_selection,
  1260. .vidioc_s_selection = coda_s_selection,
  1261. .vidioc_try_encoder_cmd = v4l2_m2m_ioctl_try_encoder_cmd,
  1262. .vidioc_encoder_cmd = coda_encoder_cmd,
  1263. .vidioc_try_decoder_cmd = v4l2_m2m_ioctl_try_decoder_cmd,
  1264. .vidioc_decoder_cmd = coda_decoder_cmd,
  1265. .vidioc_g_parm = coda_g_parm,
  1266. .vidioc_s_parm = coda_s_parm,
  1267. .vidioc_enum_framesizes = coda_enum_framesizes,
  1268. .vidioc_enum_frameintervals = coda_enum_frameintervals,
  1269. .vidioc_subscribe_event = coda_subscribe_event,
  1270. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  1271. };
  1272. /*
  1273. * Mem-to-mem operations.
  1274. */
  1275. static void coda_device_run(void *m2m_priv)
  1276. {
  1277. struct coda_ctx *ctx = m2m_priv;
  1278. struct coda_dev *dev = ctx->dev;
  1279. queue_work(dev->workqueue, &ctx->pic_run_work);
  1280. }
  1281. static void coda_pic_run_work(struct work_struct *work)
  1282. {
  1283. struct coda_ctx *ctx = container_of(work, struct coda_ctx, pic_run_work);
  1284. struct coda_dev *dev = ctx->dev;
  1285. int ret;
  1286. mutex_lock(&ctx->buffer_mutex);
  1287. mutex_lock(&dev->coda_mutex);
  1288. ret = ctx->ops->prepare_run(ctx);
  1289. if (ret < 0 && ctx->inst_type == CODA_INST_DECODER)
  1290. goto out;
  1291. if (!wait_for_completion_timeout(&ctx->completion,
  1292. msecs_to_jiffies(1000))) {
  1293. if (ctx->use_bit) {
  1294. dev_err(dev->dev, "CODA PIC_RUN timeout\n");
  1295. ctx->hold = true;
  1296. coda_hw_reset(ctx);
  1297. }
  1298. if (ctx->ops->run_timeout)
  1299. ctx->ops->run_timeout(ctx);
  1300. } else {
  1301. ctx->ops->finish_run(ctx);
  1302. }
  1303. if ((ctx->aborting || (!ctx->streamon_cap && !ctx->streamon_out)) &&
  1304. ctx->ops->seq_end_work)
  1305. queue_work(dev->workqueue, &ctx->seq_end_work);
  1306. out:
  1307. mutex_unlock(&dev->coda_mutex);
  1308. mutex_unlock(&ctx->buffer_mutex);
  1309. v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx);
  1310. }
  1311. static int coda_job_ready(void *m2m_priv)
  1312. {
  1313. struct coda_ctx *ctx = m2m_priv;
  1314. int src_bufs = v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx);
  1315. /*
  1316. * For both 'P' and 'key' frame cases 1 picture
  1317. * and 1 frame are needed. In the decoder case,
  1318. * the compressed frame can be in the bitstream.
  1319. */
  1320. if (!src_bufs && ctx->inst_type != CODA_INST_DECODER) {
  1321. coda_dbg(1, ctx, "not ready: not enough vid-out buffers.\n");
  1322. return 0;
  1323. }
  1324. if (!v4l2_m2m_num_dst_bufs_ready(ctx->fh.m2m_ctx)) {
  1325. coda_dbg(1, ctx, "not ready: not enough vid-cap buffers.\n");
  1326. return 0;
  1327. }
  1328. if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit) {
  1329. bool stream_end = ctx->bit_stream_param &
  1330. CODA_BIT_STREAM_END_FLAG;
  1331. int num_metas = ctx->num_metas;
  1332. struct coda_buffer_meta *meta;
  1333. unsigned int count;
  1334. count = hweight32(ctx->frm_dis_flg);
  1335. if (ctx->use_vdoa && count >= (ctx->num_internal_frames - 1)) {
  1336. coda_dbg(1, ctx,
  1337. "not ready: all internal buffers in use: %d/%d (0x%x)",
  1338. count, ctx->num_internal_frames,
  1339. ctx->frm_dis_flg);
  1340. return 0;
  1341. }
  1342. if (ctx->hold && !src_bufs) {
  1343. coda_dbg(1, ctx,
  1344. "not ready: on hold for more buffers.\n");
  1345. return 0;
  1346. }
  1347. if (!stream_end && (num_metas + src_bufs) < 2) {
  1348. coda_dbg(1, ctx,
  1349. "not ready: need 2 buffers available (queue:%d + bitstream:%d)\n",
  1350. num_metas, src_bufs);
  1351. return 0;
  1352. }
  1353. meta = list_first_entry(&ctx->buffer_meta_list,
  1354. struct coda_buffer_meta, list);
  1355. if (!coda_bitstream_can_fetch_past(ctx, meta->end) &&
  1356. !stream_end) {
  1357. coda_dbg(1, ctx,
  1358. "not ready: not enough bitstream data to read past %u (%u)\n",
  1359. meta->end, ctx->bitstream_fifo.kfifo.in);
  1360. return 0;
  1361. }
  1362. }
  1363. if (ctx->aborting) {
  1364. coda_dbg(1, ctx, "not ready: aborting\n");
  1365. return 0;
  1366. }
  1367. coda_dbg(2, ctx, "job ready\n");
  1368. return 1;
  1369. }
  1370. static void coda_job_abort(void *priv)
  1371. {
  1372. struct coda_ctx *ctx = priv;
  1373. ctx->aborting = 1;
  1374. coda_dbg(1, ctx, "job abort\n");
  1375. }
  1376. static const struct v4l2_m2m_ops coda_m2m_ops = {
  1377. .device_run = coda_device_run,
  1378. .job_ready = coda_job_ready,
  1379. .job_abort = coda_job_abort,
  1380. };
  1381. static void set_default_params(struct coda_ctx *ctx)
  1382. {
  1383. unsigned int max_w, max_h, usize, csize;
  1384. ctx->codec = coda_find_codec(ctx->dev, ctx->cvd->src_formats[0],
  1385. ctx->cvd->dst_formats[0]);
  1386. max_w = min(ctx->codec->max_w, 1920U);
  1387. max_h = min(ctx->codec->max_h, 1088U);
  1388. usize = max_w * max_h * 3 / 2;
  1389. csize = coda_estimate_sizeimage(ctx, usize, max_w, max_h);
  1390. ctx->params.codec_mode = ctx->codec->mode;
  1391. if (ctx->cvd->src_formats[0] == V4L2_PIX_FMT_JPEG ||
  1392. ctx->cvd->dst_formats[0] == V4L2_PIX_FMT_JPEG) {
  1393. ctx->colorspace = V4L2_COLORSPACE_SRGB;
  1394. ctx->xfer_func = V4L2_XFER_FUNC_SRGB;
  1395. ctx->ycbcr_enc = V4L2_YCBCR_ENC_601;
  1396. ctx->quantization = V4L2_QUANTIZATION_FULL_RANGE;
  1397. } else {
  1398. ctx->colorspace = V4L2_COLORSPACE_REC709;
  1399. ctx->xfer_func = V4L2_XFER_FUNC_DEFAULT;
  1400. ctx->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
  1401. ctx->quantization = V4L2_QUANTIZATION_DEFAULT;
  1402. }
  1403. ctx->params.framerate = 30;
  1404. /* Default formats for output and input queues */
  1405. ctx->q_data[V4L2_M2M_SRC].fourcc = ctx->cvd->src_formats[0];
  1406. ctx->q_data[V4L2_M2M_DST].fourcc = ctx->cvd->dst_formats[0];
  1407. ctx->q_data[V4L2_M2M_SRC].width = max_w;
  1408. ctx->q_data[V4L2_M2M_SRC].height = max_h;
  1409. ctx->q_data[V4L2_M2M_DST].width = max_w;
  1410. ctx->q_data[V4L2_M2M_DST].height = max_h;
  1411. if (ctx->codec->src_fourcc == V4L2_PIX_FMT_YUV420) {
  1412. ctx->q_data[V4L2_M2M_SRC].bytesperline = max_w;
  1413. ctx->q_data[V4L2_M2M_SRC].sizeimage = usize;
  1414. ctx->q_data[V4L2_M2M_DST].bytesperline = 0;
  1415. ctx->q_data[V4L2_M2M_DST].sizeimage = csize;
  1416. } else {
  1417. ctx->q_data[V4L2_M2M_SRC].bytesperline = 0;
  1418. ctx->q_data[V4L2_M2M_SRC].sizeimage = csize;
  1419. ctx->q_data[V4L2_M2M_DST].bytesperline = max_w;
  1420. ctx->q_data[V4L2_M2M_DST].sizeimage = usize;
  1421. }
  1422. ctx->q_data[V4L2_M2M_SRC].rect.width = max_w;
  1423. ctx->q_data[V4L2_M2M_SRC].rect.height = max_h;
  1424. ctx->q_data[V4L2_M2M_DST].rect.width = max_w;
  1425. ctx->q_data[V4L2_M2M_DST].rect.height = max_h;
  1426. /*
  1427. * Since the RBC2AXI logic only supports a single chroma plane,
  1428. * macroblock tiling only works for to NV12 pixel format.
  1429. */
  1430. ctx->tiled_map_type = GDI_LINEAR_FRAME_MAP;
  1431. }
  1432. /*
  1433. * Queue operations
  1434. */
  1435. static int coda_queue_setup(struct vb2_queue *vq,
  1436. unsigned int *nbuffers, unsigned int *nplanes,
  1437. unsigned int sizes[], struct device *alloc_devs[])
  1438. {
  1439. struct coda_ctx *ctx = vb2_get_drv_priv(vq);
  1440. struct coda_q_data *q_data;
  1441. unsigned int size;
  1442. q_data = get_q_data(ctx, vq->type);
  1443. size = q_data->sizeimage;
  1444. if (*nplanes)
  1445. return sizes[0] < size ? -EINVAL : 0;
  1446. *nplanes = 1;
  1447. sizes[0] = size;
  1448. coda_dbg(1, ctx, "get %d buffer(s) of size %d each.\n", *nbuffers,
  1449. size);
  1450. return 0;
  1451. }
  1452. static int coda_buf_prepare(struct vb2_buffer *vb)
  1453. {
  1454. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  1455. struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  1456. struct coda_q_data *q_data;
  1457. q_data = get_q_data(ctx, vb->vb2_queue->type);
  1458. if (V4L2_TYPE_IS_OUTPUT(vb->vb2_queue->type)) {
  1459. if (vbuf->field == V4L2_FIELD_ANY)
  1460. vbuf->field = V4L2_FIELD_NONE;
  1461. if (vbuf->field != V4L2_FIELD_NONE) {
  1462. v4l2_warn(&ctx->dev->v4l2_dev,
  1463. "%s field isn't supported\n", __func__);
  1464. return -EINVAL;
  1465. }
  1466. }
  1467. if (vb2_plane_size(vb, 0) < q_data->sizeimage) {
  1468. v4l2_warn(&ctx->dev->v4l2_dev,
  1469. "%s data will not fit into plane (%lu < %lu)\n",
  1470. __func__, vb2_plane_size(vb, 0),
  1471. (long)q_data->sizeimage);
  1472. return -EINVAL;
  1473. }
  1474. return 0;
  1475. }
  1476. static void coda_update_menu_ctrl(struct v4l2_ctrl *ctrl, int value)
  1477. {
  1478. if (!ctrl)
  1479. return;
  1480. v4l2_ctrl_lock(ctrl);
  1481. /*
  1482. * Extend the control range if the parsed stream contains a known but
  1483. * unsupported value or level.
  1484. */
  1485. if (value > ctrl->maximum) {
  1486. __v4l2_ctrl_modify_range(ctrl, ctrl->minimum, value,
  1487. ctrl->menu_skip_mask & ~(1 << value),
  1488. ctrl->default_value);
  1489. } else if (value < ctrl->minimum) {
  1490. __v4l2_ctrl_modify_range(ctrl, value, ctrl->maximum,
  1491. ctrl->menu_skip_mask & ~(1 << value),
  1492. ctrl->default_value);
  1493. }
  1494. __v4l2_ctrl_s_ctrl(ctrl, value);
  1495. v4l2_ctrl_unlock(ctrl);
  1496. }
  1497. void coda_update_profile_level_ctrls(struct coda_ctx *ctx, u8 profile_idc,
  1498. u8 level_idc)
  1499. {
  1500. const char * const *profile_names;
  1501. const char * const *level_names;
  1502. struct v4l2_ctrl *profile_ctrl;
  1503. struct v4l2_ctrl *level_ctrl;
  1504. const char *codec_name;
  1505. u32 profile_cid;
  1506. u32 level_cid;
  1507. int profile;
  1508. int level;
  1509. switch (ctx->codec->src_fourcc) {
  1510. case V4L2_PIX_FMT_H264:
  1511. codec_name = "H264";
  1512. profile_cid = V4L2_CID_MPEG_VIDEO_H264_PROFILE;
  1513. level_cid = V4L2_CID_MPEG_VIDEO_H264_LEVEL;
  1514. profile_ctrl = ctx->h264_profile_ctrl;
  1515. level_ctrl = ctx->h264_level_ctrl;
  1516. profile = coda_h264_profile(profile_idc);
  1517. level = coda_h264_level(level_idc);
  1518. break;
  1519. case V4L2_PIX_FMT_MPEG2:
  1520. codec_name = "MPEG-2";
  1521. profile_cid = V4L2_CID_MPEG_VIDEO_MPEG2_PROFILE;
  1522. level_cid = V4L2_CID_MPEG_VIDEO_MPEG2_LEVEL;
  1523. profile_ctrl = ctx->mpeg2_profile_ctrl;
  1524. level_ctrl = ctx->mpeg2_level_ctrl;
  1525. profile = coda_mpeg2_profile(profile_idc);
  1526. level = coda_mpeg2_level(level_idc);
  1527. break;
  1528. case V4L2_PIX_FMT_MPEG4:
  1529. codec_name = "MPEG-4";
  1530. profile_cid = V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE;
  1531. level_cid = V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL;
  1532. profile_ctrl = ctx->mpeg4_profile_ctrl;
  1533. level_ctrl = ctx->mpeg4_level_ctrl;
  1534. profile = coda_mpeg4_profile(profile_idc);
  1535. level = coda_mpeg4_level(level_idc);
  1536. break;
  1537. default:
  1538. return;
  1539. }
  1540. profile_names = v4l2_ctrl_get_menu(profile_cid);
  1541. level_names = v4l2_ctrl_get_menu(level_cid);
  1542. if (profile < 0) {
  1543. v4l2_warn(&ctx->dev->v4l2_dev, "Invalid %s profile: %u\n",
  1544. codec_name, profile_idc);
  1545. } else {
  1546. coda_dbg(1, ctx, "Parsed %s profile: %s\n", codec_name,
  1547. profile_names[profile]);
  1548. coda_update_menu_ctrl(profile_ctrl, profile);
  1549. }
  1550. if (level < 0) {
  1551. v4l2_warn(&ctx->dev->v4l2_dev, "Invalid %s level: %u\n",
  1552. codec_name, level_idc);
  1553. } else {
  1554. coda_dbg(1, ctx, "Parsed %s level: %s\n", codec_name,
  1555. level_names[level]);
  1556. coda_update_menu_ctrl(level_ctrl, level);
  1557. }
  1558. }
  1559. static void coda_queue_source_change_event(struct coda_ctx *ctx)
  1560. {
  1561. static const struct v4l2_event source_change_event = {
  1562. .type = V4L2_EVENT_SOURCE_CHANGE,
  1563. .u.src_change.changes = V4L2_EVENT_SRC_CH_RESOLUTION,
  1564. };
  1565. v4l2_event_queue_fh(&ctx->fh, &source_change_event);
  1566. }
  1567. static void coda_buf_queue(struct vb2_buffer *vb)
  1568. {
  1569. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  1570. struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  1571. struct vb2_queue *vq = vb->vb2_queue;
  1572. struct coda_q_data *q_data;
  1573. q_data = get_q_data(ctx, vb->vb2_queue->type);
  1574. /*
  1575. * In the decoder case, immediately try to copy the buffer into the
  1576. * bitstream ringbuffer and mark it as ready to be dequeued.
  1577. */
  1578. if (ctx->bitstream.size && vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
  1579. /*
  1580. * For backwards compatibility, queuing an empty buffer marks
  1581. * the stream end
  1582. */
  1583. if (vb2_get_plane_payload(vb, 0) == 0)
  1584. coda_bit_stream_end_flag(ctx);
  1585. if (q_data->fourcc == V4L2_PIX_FMT_H264) {
  1586. /*
  1587. * Unless already done, try to obtain profile_idc and
  1588. * level_idc from the SPS header. This allows to decide
  1589. * whether to enable reordering during sequence
  1590. * initialization.
  1591. */
  1592. if (!ctx->params.h264_profile_idc) {
  1593. coda_sps_parse_profile(ctx, vb);
  1594. coda_update_profile_level_ctrls(ctx,
  1595. ctx->params.h264_profile_idc,
  1596. ctx->params.h264_level_idc);
  1597. }
  1598. }
  1599. mutex_lock(&ctx->bitstream_mutex);
  1600. v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
  1601. if (vb2_is_streaming(vb->vb2_queue))
  1602. /* This set buf->sequence = ctx->qsequence++ */
  1603. coda_fill_bitstream(ctx, NULL);
  1604. mutex_unlock(&ctx->bitstream_mutex);
  1605. if (!ctx->initialized) {
  1606. /*
  1607. * Run sequence initialization in case the queued
  1608. * buffer contained headers.
  1609. */
  1610. if (vb2_is_streaming(vb->vb2_queue) &&
  1611. ctx->ops->seq_init_work) {
  1612. queue_work(ctx->dev->workqueue,
  1613. &ctx->seq_init_work);
  1614. flush_work(&ctx->seq_init_work);
  1615. }
  1616. if (ctx->initialized)
  1617. coda_queue_source_change_event(ctx);
  1618. }
  1619. } else {
  1620. if ((ctx->inst_type == CODA_INST_ENCODER || !ctx->use_bit) &&
  1621. vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
  1622. vbuf->sequence = ctx->qsequence++;
  1623. v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
  1624. }
  1625. }
  1626. int coda_alloc_aux_buf(struct coda_dev *dev, struct coda_aux_buf *buf,
  1627. size_t size, const char *name, struct dentry *parent)
  1628. {
  1629. buf->vaddr = dma_alloc_coherent(dev->dev, size, &buf->paddr,
  1630. GFP_KERNEL);
  1631. if (!buf->vaddr) {
  1632. v4l2_err(&dev->v4l2_dev,
  1633. "Failed to allocate %s buffer of size %zu\n",
  1634. name, size);
  1635. return -ENOMEM;
  1636. }
  1637. buf->size = size;
  1638. if (name && parent) {
  1639. buf->blob.data = buf->vaddr;
  1640. buf->blob.size = size;
  1641. buf->dentry = debugfs_create_blob(name, 0444, parent,
  1642. &buf->blob);
  1643. }
  1644. return 0;
  1645. }
  1646. void coda_free_aux_buf(struct coda_dev *dev,
  1647. struct coda_aux_buf *buf)
  1648. {
  1649. if (buf->vaddr) {
  1650. dma_free_coherent(dev->dev, buf->size, buf->vaddr, buf->paddr);
  1651. buf->vaddr = NULL;
  1652. buf->size = 0;
  1653. debugfs_remove(buf->dentry);
  1654. buf->dentry = NULL;
  1655. }
  1656. }
  1657. static int coda_start_streaming(struct vb2_queue *q, unsigned int count)
  1658. {
  1659. struct coda_ctx *ctx = vb2_get_drv_priv(q);
  1660. struct v4l2_device *v4l2_dev = &ctx->dev->v4l2_dev;
  1661. struct coda_q_data *q_data_src, *q_data_dst;
  1662. struct v4l2_m2m_buffer *m2m_buf, *tmp;
  1663. struct vb2_v4l2_buffer *buf;
  1664. struct list_head list;
  1665. int ret = 0;
  1666. if (count < 1)
  1667. return -EINVAL;
  1668. coda_dbg(1, ctx, "start streaming %s\n", v4l2_type_names[q->type]);
  1669. INIT_LIST_HEAD(&list);
  1670. q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
  1671. if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
  1672. if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit) {
  1673. /* copy the buffers that were queued before streamon */
  1674. mutex_lock(&ctx->bitstream_mutex);
  1675. coda_fill_bitstream(ctx, &list);
  1676. mutex_unlock(&ctx->bitstream_mutex);
  1677. if (ctx->dev->devtype->product != CODA_960 &&
  1678. coda_get_bitstream_payload(ctx) < 512) {
  1679. v4l2_err(v4l2_dev, "start payload < 512\n");
  1680. ret = -EINVAL;
  1681. goto err;
  1682. }
  1683. if (!ctx->initialized) {
  1684. /* Run sequence initialization */
  1685. if (ctx->ops->seq_init_work) {
  1686. queue_work(ctx->dev->workqueue,
  1687. &ctx->seq_init_work);
  1688. flush_work(&ctx->seq_init_work);
  1689. }
  1690. }
  1691. }
  1692. /*
  1693. * Check the first input JPEG buffer to determine chroma
  1694. * subsampling.
  1695. */
  1696. if (q_data_src->fourcc == V4L2_PIX_FMT_JPEG) {
  1697. buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
  1698. coda_jpeg_decode_header(ctx, &buf->vb2_buf);
  1699. /*
  1700. * We have to start streaming even if the first buffer
  1701. * does not contain a valid JPEG image. The error will
  1702. * be caught during device run and will be signalled
  1703. * via the capture buffer error flag.
  1704. */
  1705. q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
  1706. q_data_dst->width = round_up(q_data_src->width, 16);
  1707. q_data_dst->height = round_up(q_data_src->height, 16);
  1708. q_data_dst->bytesperline = q_data_dst->width;
  1709. if (ctx->params.jpeg_chroma_subsampling ==
  1710. V4L2_JPEG_CHROMA_SUBSAMPLING_420) {
  1711. q_data_dst->sizeimage =
  1712. q_data_dst->bytesperline *
  1713. q_data_dst->height * 3 / 2;
  1714. if (q_data_dst->fourcc != V4L2_PIX_FMT_YUV420)
  1715. q_data_dst->fourcc = V4L2_PIX_FMT_NV12;
  1716. } else {
  1717. q_data_dst->sizeimage =
  1718. q_data_dst->bytesperline *
  1719. q_data_dst->height * 2;
  1720. q_data_dst->fourcc = V4L2_PIX_FMT_YUV422P;
  1721. }
  1722. q_data_dst->rect.left = 0;
  1723. q_data_dst->rect.top = 0;
  1724. q_data_dst->rect.width = q_data_src->width;
  1725. q_data_dst->rect.height = q_data_src->height;
  1726. }
  1727. ctx->streamon_out = 1;
  1728. } else {
  1729. ctx->streamon_cap = 1;
  1730. }
  1731. /* Don't start the coda unless both queues are on */
  1732. if (!(ctx->streamon_out && ctx->streamon_cap))
  1733. goto out;
  1734. q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
  1735. if ((q_data_src->rect.width != q_data_dst->width &&
  1736. round_up(q_data_src->rect.width, 16) != q_data_dst->width) ||
  1737. (q_data_src->rect.height != q_data_dst->height &&
  1738. round_up(q_data_src->rect.height, 16) != q_data_dst->height)) {
  1739. v4l2_err(v4l2_dev, "can't convert %dx%d to %dx%d\n",
  1740. q_data_src->rect.width, q_data_src->rect.height,
  1741. q_data_dst->width, q_data_dst->height);
  1742. ret = -EINVAL;
  1743. goto err;
  1744. }
  1745. /* Allow BIT decoder device_run with no new buffers queued */
  1746. if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit)
  1747. v4l2_m2m_set_src_buffered(ctx->fh.m2m_ctx, true);
  1748. ctx->gopcounter = ctx->params.gop_size - 1;
  1749. if (q_data_dst->fourcc == V4L2_PIX_FMT_JPEG)
  1750. ctx->params.gop_size = 1;
  1751. ctx->gopcounter = ctx->params.gop_size - 1;
  1752. /* Only decoders have this control */
  1753. if (ctx->mb_err_cnt_ctrl)
  1754. v4l2_ctrl_s_ctrl(ctx->mb_err_cnt_ctrl, 0);
  1755. ret = ctx->ops->start_streaming(ctx);
  1756. if (ctx->inst_type == CODA_INST_DECODER) {
  1757. if (ret == -EAGAIN)
  1758. goto out;
  1759. }
  1760. if (ret < 0)
  1761. goto err;
  1762. out:
  1763. if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
  1764. list_for_each_entry_safe(m2m_buf, tmp, &list, list) {
  1765. list_del(&m2m_buf->list);
  1766. v4l2_m2m_buf_done(&m2m_buf->vb, VB2_BUF_STATE_DONE);
  1767. }
  1768. }
  1769. return 0;
  1770. err:
  1771. if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
  1772. list_for_each_entry_safe(m2m_buf, tmp, &list, list) {
  1773. list_del(&m2m_buf->list);
  1774. v4l2_m2m_buf_done(&m2m_buf->vb, VB2_BUF_STATE_QUEUED);
  1775. }
  1776. while ((buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx)))
  1777. v4l2_m2m_buf_done(buf, VB2_BUF_STATE_QUEUED);
  1778. } else {
  1779. while ((buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx)))
  1780. v4l2_m2m_buf_done(buf, VB2_BUF_STATE_QUEUED);
  1781. }
  1782. return ret;
  1783. }
  1784. static void coda_stop_streaming(struct vb2_queue *q)
  1785. {
  1786. struct coda_ctx *ctx = vb2_get_drv_priv(q);
  1787. struct coda_dev *dev = ctx->dev;
  1788. struct vb2_v4l2_buffer *buf;
  1789. bool stop;
  1790. stop = ctx->streamon_out && ctx->streamon_cap;
  1791. coda_dbg(1, ctx, "stop streaming %s\n", v4l2_type_names[q->type]);
  1792. if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
  1793. ctx->streamon_out = 0;
  1794. coda_bit_stream_end_flag(ctx);
  1795. ctx->qsequence = 0;
  1796. while ((buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx)))
  1797. v4l2_m2m_buf_done(buf, VB2_BUF_STATE_ERROR);
  1798. } else {
  1799. ctx->streamon_cap = 0;
  1800. ctx->osequence = 0;
  1801. ctx->sequence_offset = 0;
  1802. while ((buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx)))
  1803. v4l2_m2m_buf_done(buf, VB2_BUF_STATE_ERROR);
  1804. }
  1805. if (stop) {
  1806. struct coda_buffer_meta *meta;
  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. spin_lock(&ctx->buffer_meta_lock);
  1812. while (!list_empty(&ctx->buffer_meta_list)) {
  1813. meta = list_first_entry(&ctx->buffer_meta_list,
  1814. struct coda_buffer_meta, list);
  1815. list_del(&meta->list);
  1816. kfree(meta);
  1817. }
  1818. ctx->num_metas = 0;
  1819. spin_unlock(&ctx->buffer_meta_lock);
  1820. kfifo_init(&ctx->bitstream_fifo,
  1821. ctx->bitstream.vaddr, ctx->bitstream.size);
  1822. ctx->runcounter = 0;
  1823. ctx->aborting = 0;
  1824. ctx->hold = false;
  1825. }
  1826. if (!ctx->streamon_out && !ctx->streamon_cap)
  1827. ctx->bit_stream_param &= ~CODA_BIT_STREAM_END_FLAG;
  1828. }
  1829. static const struct vb2_ops coda_qops = {
  1830. .queue_setup = coda_queue_setup,
  1831. .buf_prepare = coda_buf_prepare,
  1832. .buf_queue = coda_buf_queue,
  1833. .start_streaming = coda_start_streaming,
  1834. .stop_streaming = coda_stop_streaming,
  1835. .wait_prepare = vb2_ops_wait_prepare,
  1836. .wait_finish = vb2_ops_wait_finish,
  1837. };
  1838. static int coda_s_ctrl(struct v4l2_ctrl *ctrl)
  1839. {
  1840. const char * const *val_names = v4l2_ctrl_get_menu(ctrl->id);
  1841. struct coda_ctx *ctx =
  1842. container_of(ctrl->handler, struct coda_ctx, ctrls);
  1843. if (val_names)
  1844. coda_dbg(2, ctx, "s_ctrl: id = 0x%x, name = \"%s\", val = %d (\"%s\")\n",
  1845. ctrl->id, ctrl->name, ctrl->val, val_names[ctrl->val]);
  1846. else
  1847. coda_dbg(2, ctx, "s_ctrl: id = 0x%x, name = \"%s\", val = %d\n",
  1848. ctrl->id, ctrl->name, ctrl->val);
  1849. switch (ctrl->id) {
  1850. case V4L2_CID_HFLIP:
  1851. if (ctrl->val)
  1852. ctx->params.rot_mode |= CODA_MIR_HOR;
  1853. else
  1854. ctx->params.rot_mode &= ~CODA_MIR_HOR;
  1855. break;
  1856. case V4L2_CID_VFLIP:
  1857. if (ctrl->val)
  1858. ctx->params.rot_mode |= CODA_MIR_VER;
  1859. else
  1860. ctx->params.rot_mode &= ~CODA_MIR_VER;
  1861. break;
  1862. case V4L2_CID_MPEG_VIDEO_BITRATE:
  1863. ctx->params.bitrate = ctrl->val / 1000;
  1864. ctx->params.bitrate_changed = true;
  1865. break;
  1866. case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
  1867. ctx->params.gop_size = ctrl->val;
  1868. break;
  1869. case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP:
  1870. ctx->params.h264_intra_qp = ctrl->val;
  1871. ctx->params.h264_intra_qp_changed = true;
  1872. break;
  1873. case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP:
  1874. ctx->params.h264_inter_qp = ctrl->val;
  1875. break;
  1876. case V4L2_CID_MPEG_VIDEO_H264_MIN_QP:
  1877. ctx->params.h264_min_qp = ctrl->val;
  1878. break;
  1879. case V4L2_CID_MPEG_VIDEO_H264_MAX_QP:
  1880. ctx->params.h264_max_qp = ctrl->val;
  1881. break;
  1882. case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA:
  1883. ctx->params.h264_slice_alpha_c0_offset_div2 = ctrl->val;
  1884. break;
  1885. case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA:
  1886. ctx->params.h264_slice_beta_offset_div2 = ctrl->val;
  1887. break;
  1888. case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE:
  1889. ctx->params.h264_disable_deblocking_filter_idc = ctrl->val;
  1890. break;
  1891. case V4L2_CID_MPEG_VIDEO_H264_CONSTRAINED_INTRA_PREDICTION:
  1892. ctx->params.h264_constrained_intra_pred_flag = ctrl->val;
  1893. break;
  1894. case V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE:
  1895. ctx->params.frame_rc_enable = ctrl->val;
  1896. break;
  1897. case V4L2_CID_MPEG_VIDEO_MB_RC_ENABLE:
  1898. ctx->params.mb_rc_enable = ctrl->val;
  1899. break;
  1900. case V4L2_CID_MPEG_VIDEO_H264_CHROMA_QP_INDEX_OFFSET:
  1901. ctx->params.h264_chroma_qp_index_offset = ctrl->val;
  1902. break;
  1903. case V4L2_CID_MPEG_VIDEO_H264_PROFILE:
  1904. /* TODO: switch between baseline and constrained baseline */
  1905. if (ctx->inst_type == CODA_INST_ENCODER)
  1906. ctx->params.h264_profile_idc = 66;
  1907. break;
  1908. case V4L2_CID_MPEG_VIDEO_H264_LEVEL:
  1909. /* nothing to do, this is set by the encoder */
  1910. break;
  1911. case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP:
  1912. ctx->params.mpeg4_intra_qp = ctrl->val;
  1913. break;
  1914. case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP:
  1915. ctx->params.mpeg4_inter_qp = ctrl->val;
  1916. break;
  1917. case V4L2_CID_MPEG_VIDEO_MPEG2_PROFILE:
  1918. case V4L2_CID_MPEG_VIDEO_MPEG2_LEVEL:
  1919. case V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE:
  1920. case V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL:
  1921. /* nothing to do, these are fixed */
  1922. break;
  1923. case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
  1924. ctx->params.slice_mode = ctrl->val;
  1925. ctx->params.slice_mode_changed = true;
  1926. break;
  1927. case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB:
  1928. ctx->params.slice_max_mb = ctrl->val;
  1929. ctx->params.slice_mode_changed = true;
  1930. break;
  1931. case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES:
  1932. ctx->params.slice_max_bits = ctrl->val * 8;
  1933. ctx->params.slice_mode_changed = true;
  1934. break;
  1935. case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
  1936. break;
  1937. case V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB:
  1938. ctx->params.intra_refresh = ctrl->val;
  1939. ctx->params.intra_refresh_changed = true;
  1940. break;
  1941. case V4L2_CID_MPEG_VIDEO_FORCE_KEY_FRAME:
  1942. ctx->params.force_ipicture = true;
  1943. break;
  1944. case V4L2_CID_JPEG_COMPRESSION_QUALITY:
  1945. coda_set_jpeg_compression_quality(ctx, ctrl->val);
  1946. break;
  1947. case V4L2_CID_JPEG_RESTART_INTERVAL:
  1948. ctx->params.jpeg_restart_interval = ctrl->val;
  1949. break;
  1950. case V4L2_CID_MPEG_VIDEO_VBV_DELAY:
  1951. ctx->params.vbv_delay = ctrl->val;
  1952. break;
  1953. case V4L2_CID_MPEG_VIDEO_VBV_SIZE:
  1954. ctx->params.vbv_size = min(ctrl->val * 8192, 0x7fffffff);
  1955. break;
  1956. default:
  1957. coda_dbg(1, ctx, "Invalid control, id=%d, val=%d\n",
  1958. ctrl->id, ctrl->val);
  1959. return -EINVAL;
  1960. }
  1961. return 0;
  1962. }
  1963. static const struct v4l2_ctrl_ops coda_ctrl_ops = {
  1964. .s_ctrl = coda_s_ctrl,
  1965. };
  1966. static void coda_encode_ctrls(struct coda_ctx *ctx)
  1967. {
  1968. int max_gop_size = (ctx->dev->devtype->product == CODA_DX6) ? 60 : 99;
  1969. v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
  1970. V4L2_CID_MPEG_VIDEO_BITRATE, 0, 32767000, 1000, 0);
  1971. v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
  1972. V4L2_CID_MPEG_VIDEO_GOP_SIZE, 0, max_gop_size, 1, 16);
  1973. v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
  1974. V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP, 0, 51, 1, 25);
  1975. v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
  1976. V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP, 0, 51, 1, 25);
  1977. if (ctx->dev->devtype->product != CODA_960) {
  1978. v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
  1979. V4L2_CID_MPEG_VIDEO_H264_MIN_QP, 0, 51, 1, 12);
  1980. }
  1981. v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
  1982. V4L2_CID_MPEG_VIDEO_H264_MAX_QP, 0, 51, 1, 51);
  1983. v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
  1984. V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA, -6, 6, 1, 0);
  1985. v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
  1986. V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA, -6, 6, 1, 0);
  1987. v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
  1988. V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE,
  1989. V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_DISABLED_AT_SLICE_BOUNDARY,
  1990. 0x0, V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED);
  1991. v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
  1992. V4L2_CID_MPEG_VIDEO_H264_CONSTRAINED_INTRA_PREDICTION, 0, 1, 1,
  1993. 0);
  1994. v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
  1995. V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE, 0, 1, 1, 1);
  1996. v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
  1997. V4L2_CID_MPEG_VIDEO_MB_RC_ENABLE, 0, 1, 1, 1);
  1998. v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
  1999. V4L2_CID_MPEG_VIDEO_H264_CHROMA_QP_INDEX_OFFSET, -12, 12, 1, 0);
  2000. v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
  2001. V4L2_CID_MPEG_VIDEO_H264_PROFILE,
  2002. V4L2_MPEG_VIDEO_H264_PROFILE_CONSTRAINED_BASELINE, 0x0,
  2003. V4L2_MPEG_VIDEO_H264_PROFILE_CONSTRAINED_BASELINE);
  2004. if (ctx->dev->devtype->product == CODA_HX4 ||
  2005. ctx->dev->devtype->product == CODA_7541) {
  2006. v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
  2007. V4L2_CID_MPEG_VIDEO_H264_LEVEL,
  2008. V4L2_MPEG_VIDEO_H264_LEVEL_3_1,
  2009. ~((1 << V4L2_MPEG_VIDEO_H264_LEVEL_2_0) |
  2010. (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_0) |
  2011. (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_1)),
  2012. V4L2_MPEG_VIDEO_H264_LEVEL_3_1);
  2013. }
  2014. if (ctx->dev->devtype->product == CODA_960) {
  2015. v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
  2016. V4L2_CID_MPEG_VIDEO_H264_LEVEL,
  2017. V4L2_MPEG_VIDEO_H264_LEVEL_4_2,
  2018. ~((1 << V4L2_MPEG_VIDEO_H264_LEVEL_1_0) |
  2019. (1 << V4L2_MPEG_VIDEO_H264_LEVEL_2_0) |
  2020. (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_0) |
  2021. (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_1) |
  2022. (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_2) |
  2023. (1 << V4L2_MPEG_VIDEO_H264_LEVEL_4_0) |
  2024. (1 << V4L2_MPEG_VIDEO_H264_LEVEL_4_1) |
  2025. (1 << V4L2_MPEG_VIDEO_H264_LEVEL_4_2)),
  2026. V4L2_MPEG_VIDEO_H264_LEVEL_4_0);
  2027. }
  2028. v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
  2029. V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP, 1, 31, 1, 2);
  2030. v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
  2031. V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP, 1, 31, 1, 2);
  2032. v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
  2033. V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE,
  2034. V4L2_MPEG_VIDEO_MPEG4_PROFILE_SIMPLE, 0x0,
  2035. V4L2_MPEG_VIDEO_MPEG4_PROFILE_SIMPLE);
  2036. if (ctx->dev->devtype->product == CODA_HX4 ||
  2037. ctx->dev->devtype->product == CODA_7541 ||
  2038. ctx->dev->devtype->product == CODA_960) {
  2039. v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
  2040. V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL,
  2041. V4L2_MPEG_VIDEO_MPEG4_LEVEL_5,
  2042. ~(1 << V4L2_MPEG_VIDEO_MPEG4_LEVEL_5),
  2043. V4L2_MPEG_VIDEO_MPEG4_LEVEL_5);
  2044. }
  2045. v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
  2046. V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE,
  2047. V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_MAX_BYTES, 0x0,
  2048. V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE);
  2049. v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
  2050. V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB, 1, 0x3fffffff, 1, 1);
  2051. v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
  2052. V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES, 1, 0x3fffffff, 1,
  2053. 500);
  2054. v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
  2055. V4L2_CID_MPEG_VIDEO_HEADER_MODE,
  2056. V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME,
  2057. (1 << V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE),
  2058. V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME);
  2059. v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
  2060. V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB, 0,
  2061. 1920 * 1088 / 256, 1, 0);
  2062. v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
  2063. V4L2_CID_MPEG_VIDEO_VBV_DELAY, 0, 0x7fff, 1, 0);
  2064. /*
  2065. * The maximum VBV size value is 0x7fffffff bits,
  2066. * one bit less than 262144 KiB
  2067. */
  2068. v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
  2069. V4L2_CID_MPEG_VIDEO_VBV_SIZE, 0, 262144, 1, 0);
  2070. }
  2071. static void coda_jpeg_encode_ctrls(struct coda_ctx *ctx)
  2072. {
  2073. v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
  2074. V4L2_CID_JPEG_COMPRESSION_QUALITY, 5, 100, 1, 50);
  2075. v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
  2076. V4L2_CID_JPEG_RESTART_INTERVAL, 0, 100, 1, 0);
  2077. }
  2078. static void coda_decode_ctrls(struct coda_ctx *ctx)
  2079. {
  2080. u8 max;
  2081. ctx->h264_profile_ctrl = v4l2_ctrl_new_std_menu(&ctx->ctrls,
  2082. &coda_ctrl_ops, V4L2_CID_MPEG_VIDEO_H264_PROFILE,
  2083. V4L2_MPEG_VIDEO_H264_PROFILE_HIGH,
  2084. ~((1 << V4L2_MPEG_VIDEO_H264_PROFILE_CONSTRAINED_BASELINE) |
  2085. (1 << V4L2_MPEG_VIDEO_H264_PROFILE_MAIN) |
  2086. (1 << V4L2_MPEG_VIDEO_H264_PROFILE_HIGH)),
  2087. V4L2_MPEG_VIDEO_H264_PROFILE_HIGH);
  2088. if (ctx->h264_profile_ctrl)
  2089. ctx->h264_profile_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
  2090. if (ctx->dev->devtype->product == CODA_HX4 ||
  2091. ctx->dev->devtype->product == CODA_7541)
  2092. max = V4L2_MPEG_VIDEO_H264_LEVEL_4_0;
  2093. else if (ctx->dev->devtype->product == CODA_960)
  2094. max = V4L2_MPEG_VIDEO_H264_LEVEL_4_1;
  2095. else
  2096. return;
  2097. ctx->h264_level_ctrl = v4l2_ctrl_new_std_menu(&ctx->ctrls,
  2098. &coda_ctrl_ops, V4L2_CID_MPEG_VIDEO_H264_LEVEL, max, 0, max);
  2099. if (ctx->h264_level_ctrl)
  2100. ctx->h264_level_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
  2101. ctx->mpeg2_profile_ctrl = v4l2_ctrl_new_std_menu(&ctx->ctrls,
  2102. &coda_ctrl_ops, V4L2_CID_MPEG_VIDEO_MPEG2_PROFILE,
  2103. V4L2_MPEG_VIDEO_MPEG2_PROFILE_HIGH, 0,
  2104. V4L2_MPEG_VIDEO_MPEG2_PROFILE_HIGH);
  2105. if (ctx->mpeg2_profile_ctrl)
  2106. ctx->mpeg2_profile_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
  2107. ctx->mpeg2_level_ctrl = v4l2_ctrl_new_std_menu(&ctx->ctrls,
  2108. &coda_ctrl_ops, V4L2_CID_MPEG_VIDEO_MPEG2_LEVEL,
  2109. V4L2_MPEG_VIDEO_MPEG2_LEVEL_HIGH, 0,
  2110. V4L2_MPEG_VIDEO_MPEG2_LEVEL_HIGH);
  2111. if (ctx->mpeg2_level_ctrl)
  2112. ctx->mpeg2_level_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
  2113. ctx->mpeg4_profile_ctrl = v4l2_ctrl_new_std_menu(&ctx->ctrls,
  2114. &coda_ctrl_ops, V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE,
  2115. V4L2_MPEG_VIDEO_MPEG4_PROFILE_ADVANCED_CODING_EFFICIENCY, 0,
  2116. V4L2_MPEG_VIDEO_MPEG4_PROFILE_ADVANCED_CODING_EFFICIENCY);
  2117. if (ctx->mpeg4_profile_ctrl)
  2118. ctx->mpeg4_profile_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
  2119. ctx->mpeg4_level_ctrl = v4l2_ctrl_new_std_menu(&ctx->ctrls,
  2120. &coda_ctrl_ops, V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL,
  2121. V4L2_MPEG_VIDEO_MPEG4_LEVEL_5, 0,
  2122. V4L2_MPEG_VIDEO_MPEG4_LEVEL_5);
  2123. if (ctx->mpeg4_level_ctrl)
  2124. ctx->mpeg4_level_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
  2125. }
  2126. static const struct v4l2_ctrl_config coda_mb_err_cnt_ctrl_config = {
  2127. .id = V4L2_CID_CODA_MB_ERR_CNT,
  2128. .name = "Macroblocks Error Count",
  2129. .type = V4L2_CTRL_TYPE_INTEGER,
  2130. .min = 0,
  2131. .max = 0x7fffffff,
  2132. .step = 1,
  2133. };
  2134. static int coda_ctrls_setup(struct coda_ctx *ctx)
  2135. {
  2136. v4l2_ctrl_handler_init(&ctx->ctrls, 2);
  2137. v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
  2138. V4L2_CID_HFLIP, 0, 1, 1, 0);
  2139. v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
  2140. V4L2_CID_VFLIP, 0, 1, 1, 0);
  2141. if (ctx->inst_type == CODA_INST_ENCODER) {
  2142. v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
  2143. V4L2_CID_MIN_BUFFERS_FOR_OUTPUT,
  2144. 1, 1, 1, 1);
  2145. if (ctx->cvd->dst_formats[0] == V4L2_PIX_FMT_JPEG)
  2146. coda_jpeg_encode_ctrls(ctx);
  2147. else
  2148. coda_encode_ctrls(ctx);
  2149. } else {
  2150. v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
  2151. V4L2_CID_MIN_BUFFERS_FOR_CAPTURE,
  2152. 1, 1, 1, 1);
  2153. if (ctx->cvd->src_formats[0] == V4L2_PIX_FMT_H264)
  2154. coda_decode_ctrls(ctx);
  2155. ctx->mb_err_cnt_ctrl = v4l2_ctrl_new_custom(&ctx->ctrls,
  2156. &coda_mb_err_cnt_ctrl_config,
  2157. NULL);
  2158. if (ctx->mb_err_cnt_ctrl)
  2159. ctx->mb_err_cnt_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
  2160. }
  2161. if (ctx->ctrls.error) {
  2162. v4l2_err(&ctx->dev->v4l2_dev,
  2163. "control initialization error (%d)",
  2164. ctx->ctrls.error);
  2165. return -EINVAL;
  2166. }
  2167. return v4l2_ctrl_handler_setup(&ctx->ctrls);
  2168. }
  2169. static int coda_queue_init(struct coda_ctx *ctx, struct vb2_queue *vq)
  2170. {
  2171. vq->drv_priv = ctx;
  2172. vq->ops = &coda_qops;
  2173. vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
  2174. vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
  2175. vq->lock = &ctx->dev->dev_mutex;
  2176. /* One way to indicate end-of-stream for coda is to set the
  2177. * bytesused == 0. However by default videobuf2 handles bytesused
  2178. * equal to 0 as a special case and changes its value to the size
  2179. * of the buffer. Set the allow_zero_bytesused flag, so
  2180. * that videobuf2 will keep the value of bytesused intact.
  2181. */
  2182. vq->allow_zero_bytesused = 1;
  2183. /*
  2184. * We might be fine with no buffers on some of the queues, but that
  2185. * would need to be reflected in job_ready(). Currently we expect all
  2186. * queues to have at least one buffer queued.
  2187. */
  2188. vq->min_buffers_needed = 1;
  2189. vq->dev = ctx->dev->dev;
  2190. return vb2_queue_init(vq);
  2191. }
  2192. int coda_encoder_queue_init(void *priv, struct vb2_queue *src_vq,
  2193. struct vb2_queue *dst_vq)
  2194. {
  2195. int ret;
  2196. src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  2197. src_vq->io_modes = VB2_DMABUF | VB2_MMAP;
  2198. src_vq->mem_ops = &vb2_dma_contig_memops;
  2199. ret = coda_queue_init(priv, src_vq);
  2200. if (ret)
  2201. return ret;
  2202. dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  2203. dst_vq->io_modes = VB2_DMABUF | VB2_MMAP;
  2204. dst_vq->mem_ops = &vb2_dma_contig_memops;
  2205. return coda_queue_init(priv, dst_vq);
  2206. }
  2207. int coda_decoder_queue_init(void *priv, struct vb2_queue *src_vq,
  2208. struct vb2_queue *dst_vq)
  2209. {
  2210. int ret;
  2211. src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  2212. src_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
  2213. src_vq->mem_ops = &vb2_vmalloc_memops;
  2214. ret = coda_queue_init(priv, src_vq);
  2215. if (ret)
  2216. return ret;
  2217. dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  2218. dst_vq->io_modes = VB2_DMABUF | VB2_MMAP;
  2219. dst_vq->dma_attrs = DMA_ATTR_NO_KERNEL_MAPPING;
  2220. dst_vq->mem_ops = &vb2_dma_contig_memops;
  2221. return coda_queue_init(priv, dst_vq);
  2222. }
  2223. /*
  2224. * File operations
  2225. */
  2226. static int coda_open(struct file *file)
  2227. {
  2228. struct video_device *vdev = video_devdata(file);
  2229. struct coda_dev *dev = video_get_drvdata(vdev);
  2230. struct coda_ctx *ctx;
  2231. unsigned int max = ~0;
  2232. char *name;
  2233. int ret;
  2234. int idx;
  2235. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  2236. if (!ctx)
  2237. return -ENOMEM;
  2238. if (dev->devtype->product == CODA_DX6)
  2239. max = CODADX6_MAX_INSTANCES - 1;
  2240. idx = ida_alloc_max(&dev->ida, max, GFP_KERNEL);
  2241. if (idx < 0) {
  2242. ret = idx;
  2243. goto err_coda_max;
  2244. }
  2245. name = kasprintf(GFP_KERNEL, "context%d", idx);
  2246. if (!name) {
  2247. ret = -ENOMEM;
  2248. goto err_coda_name_init;
  2249. }
  2250. ctx->debugfs_entry = debugfs_create_dir(name, dev->debugfs_root);
  2251. kfree(name);
  2252. ctx->cvd = to_coda_video_device(vdev);
  2253. ctx->inst_type = ctx->cvd->type;
  2254. ctx->ops = ctx->cvd->ops;
  2255. ctx->use_bit = !ctx->cvd->direct;
  2256. init_completion(&ctx->completion);
  2257. INIT_WORK(&ctx->pic_run_work, coda_pic_run_work);
  2258. if (ctx->ops->seq_init_work)
  2259. INIT_WORK(&ctx->seq_init_work, ctx->ops->seq_init_work);
  2260. if (ctx->ops->seq_end_work)
  2261. INIT_WORK(&ctx->seq_end_work, ctx->ops->seq_end_work);
  2262. v4l2_fh_init(&ctx->fh, video_devdata(file));
  2263. file->private_data = &ctx->fh;
  2264. v4l2_fh_add(&ctx->fh);
  2265. ctx->dev = dev;
  2266. ctx->idx = idx;
  2267. coda_dbg(1, ctx, "open instance (%p)\n", ctx);
  2268. switch (dev->devtype->product) {
  2269. case CODA_960:
  2270. /*
  2271. * Enabling the BWB when decoding can hang the firmware with
  2272. * certain streams. The issue was tracked as ENGR00293425 by
  2273. * Freescale. As a workaround, disable BWB for all decoders.
  2274. * The enable_bwb module parameter allows to override this.
  2275. */
  2276. if (enable_bwb || ctx->inst_type == CODA_INST_ENCODER)
  2277. ctx->frame_mem_ctrl = CODA9_FRAME_ENABLE_BWB;
  2278. fallthrough;
  2279. case CODA_HX4:
  2280. case CODA_7541:
  2281. ctx->reg_idx = 0;
  2282. break;
  2283. default:
  2284. ctx->reg_idx = idx;
  2285. }
  2286. if (ctx->dev->vdoa && !disable_vdoa) {
  2287. ctx->vdoa = vdoa_context_create(dev->vdoa);
  2288. if (!ctx->vdoa)
  2289. v4l2_warn(&dev->v4l2_dev,
  2290. "Failed to create vdoa context: not using vdoa");
  2291. }
  2292. ctx->use_vdoa = false;
  2293. /* Power up and upload firmware if necessary */
  2294. ret = pm_runtime_resume_and_get(dev->dev);
  2295. if (ret < 0) {
  2296. v4l2_err(&dev->v4l2_dev, "failed to power up: %d\n", ret);
  2297. goto err_pm_get;
  2298. }
  2299. ret = clk_prepare_enable(dev->clk_per);
  2300. if (ret)
  2301. goto err_clk_enable;
  2302. ret = clk_prepare_enable(dev->clk_ahb);
  2303. if (ret)
  2304. goto err_clk_ahb;
  2305. set_default_params(ctx);
  2306. ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx,
  2307. ctx->ops->queue_init);
  2308. if (IS_ERR(ctx->fh.m2m_ctx)) {
  2309. ret = PTR_ERR(ctx->fh.m2m_ctx);
  2310. v4l2_err(&dev->v4l2_dev, "%s return error (%d)\n",
  2311. __func__, ret);
  2312. goto err_ctx_init;
  2313. }
  2314. ret = coda_ctrls_setup(ctx);
  2315. if (ret) {
  2316. v4l2_err(&dev->v4l2_dev, "failed to setup coda controls\n");
  2317. goto err_ctrls_setup;
  2318. }
  2319. ctx->fh.ctrl_handler = &ctx->ctrls;
  2320. mutex_init(&ctx->bitstream_mutex);
  2321. mutex_init(&ctx->buffer_mutex);
  2322. mutex_init(&ctx->wakeup_mutex);
  2323. INIT_LIST_HEAD(&ctx->buffer_meta_list);
  2324. spin_lock_init(&ctx->buffer_meta_lock);
  2325. return 0;
  2326. err_ctrls_setup:
  2327. v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
  2328. err_ctx_init:
  2329. clk_disable_unprepare(dev->clk_ahb);
  2330. err_clk_ahb:
  2331. clk_disable_unprepare(dev->clk_per);
  2332. err_clk_enable:
  2333. pm_runtime_put_sync(dev->dev);
  2334. err_pm_get:
  2335. v4l2_fh_del(&ctx->fh);
  2336. v4l2_fh_exit(&ctx->fh);
  2337. err_coda_name_init:
  2338. ida_free(&dev->ida, ctx->idx);
  2339. err_coda_max:
  2340. kfree(ctx);
  2341. return ret;
  2342. }
  2343. static int coda_release(struct file *file)
  2344. {
  2345. struct coda_dev *dev = video_drvdata(file);
  2346. struct coda_ctx *ctx = fh_to_ctx(file->private_data);
  2347. coda_dbg(1, ctx, "release instance (%p)\n", ctx);
  2348. if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit)
  2349. coda_bit_stream_end_flag(ctx);
  2350. /* If this instance is running, call .job_abort and wait for it to end */
  2351. v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
  2352. if (ctx->vdoa)
  2353. vdoa_context_destroy(ctx->vdoa);
  2354. /* In case the instance was not running, we still need to call SEQ_END */
  2355. if (ctx->ops->seq_end_work) {
  2356. queue_work(dev->workqueue, &ctx->seq_end_work);
  2357. flush_work(&ctx->seq_end_work);
  2358. }
  2359. if (ctx->dev->devtype->product == CODA_DX6)
  2360. coda_free_aux_buf(dev, &ctx->workbuf);
  2361. v4l2_ctrl_handler_free(&ctx->ctrls);
  2362. clk_disable_unprepare(dev->clk_ahb);
  2363. clk_disable_unprepare(dev->clk_per);
  2364. pm_runtime_put_sync(dev->dev);
  2365. v4l2_fh_del(&ctx->fh);
  2366. v4l2_fh_exit(&ctx->fh);
  2367. ida_free(&dev->ida, ctx->idx);
  2368. if (ctx->ops->release)
  2369. ctx->ops->release(ctx);
  2370. debugfs_remove_recursive(ctx->debugfs_entry);
  2371. kfree(ctx);
  2372. return 0;
  2373. }
  2374. static const struct v4l2_file_operations coda_fops = {
  2375. .owner = THIS_MODULE,
  2376. .open = coda_open,
  2377. .release = coda_release,
  2378. .poll = v4l2_m2m_fop_poll,
  2379. .unlocked_ioctl = video_ioctl2,
  2380. .mmap = v4l2_m2m_fop_mmap,
  2381. };
  2382. static int coda_hw_init(struct coda_dev *dev)
  2383. {
  2384. u32 data;
  2385. u16 *p;
  2386. int i, ret;
  2387. ret = clk_prepare_enable(dev->clk_per);
  2388. if (ret)
  2389. goto err_clk_per;
  2390. ret = clk_prepare_enable(dev->clk_ahb);
  2391. if (ret)
  2392. goto err_clk_ahb;
  2393. reset_control_reset(dev->rstc);
  2394. /*
  2395. * Copy the first CODA_ISRAM_SIZE in the internal SRAM.
  2396. * The 16-bit chars in the code buffer are in memory access
  2397. * order, re-sort them to CODA order for register download.
  2398. * Data in this SRAM survives a reboot.
  2399. */
  2400. p = (u16 *)dev->codebuf.vaddr;
  2401. if (dev->devtype->product == CODA_DX6) {
  2402. for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
  2403. data = CODA_DOWN_ADDRESS_SET(i) |
  2404. CODA_DOWN_DATA_SET(p[i ^ 1]);
  2405. coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
  2406. }
  2407. } else {
  2408. for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
  2409. data = CODA_DOWN_ADDRESS_SET(i) |
  2410. CODA_DOWN_DATA_SET(p[round_down(i, 4) +
  2411. 3 - (i % 4)]);
  2412. coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
  2413. }
  2414. }
  2415. /* Clear registers */
  2416. for (i = 0; i < 64; i++)
  2417. coda_write(dev, 0, CODA_REG_BIT_CODE_BUF_ADDR + i * 4);
  2418. /* Tell the BIT where to find everything it needs */
  2419. if (dev->devtype->product == CODA_960 ||
  2420. dev->devtype->product == CODA_7541 ||
  2421. dev->devtype->product == CODA_HX4) {
  2422. coda_write(dev, dev->tempbuf.paddr,
  2423. CODA_REG_BIT_TEMP_BUF_ADDR);
  2424. coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
  2425. } else {
  2426. coda_write(dev, dev->workbuf.paddr,
  2427. CODA_REG_BIT_WORK_BUF_ADDR);
  2428. }
  2429. coda_write(dev, dev->codebuf.paddr,
  2430. CODA_REG_BIT_CODE_BUF_ADDR);
  2431. coda_write(dev, 0, CODA_REG_BIT_CODE_RUN);
  2432. /* Set default values */
  2433. switch (dev->devtype->product) {
  2434. case CODA_DX6:
  2435. coda_write(dev, CODADX6_STREAM_BUF_PIC_FLUSH,
  2436. CODA_REG_BIT_STREAM_CTRL);
  2437. break;
  2438. default:
  2439. coda_write(dev, CODA7_STREAM_BUF_PIC_FLUSH,
  2440. CODA_REG_BIT_STREAM_CTRL);
  2441. }
  2442. if (dev->devtype->product == CODA_960)
  2443. coda_write(dev, CODA9_FRAME_ENABLE_BWB,
  2444. CODA_REG_BIT_FRAME_MEM_CTRL);
  2445. else
  2446. coda_write(dev, 0, CODA_REG_BIT_FRAME_MEM_CTRL);
  2447. if (dev->devtype->product != CODA_DX6)
  2448. coda_write(dev, 0, CODA7_REG_BIT_AXI_SRAM_USE);
  2449. coda_write(dev, CODA_INT_INTERRUPT_ENABLE,
  2450. CODA_REG_BIT_INT_ENABLE);
  2451. /* Reset VPU and start processor */
  2452. data = coda_read(dev, CODA_REG_BIT_CODE_RESET);
  2453. data |= CODA_REG_RESET_ENABLE;
  2454. coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
  2455. udelay(10);
  2456. data &= ~CODA_REG_RESET_ENABLE;
  2457. coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
  2458. coda_write(dev, CODA_REG_RUN_ENABLE, CODA_REG_BIT_CODE_RUN);
  2459. clk_disable_unprepare(dev->clk_ahb);
  2460. clk_disable_unprepare(dev->clk_per);
  2461. return 0;
  2462. err_clk_ahb:
  2463. clk_disable_unprepare(dev->clk_per);
  2464. err_clk_per:
  2465. return ret;
  2466. }
  2467. static int coda_register_device(struct coda_dev *dev, int i)
  2468. {
  2469. struct video_device *vfd = &dev->vfd[i];
  2470. const char *name;
  2471. int ret;
  2472. if (i >= dev->devtype->num_vdevs)
  2473. return -EINVAL;
  2474. name = dev->devtype->vdevs[i]->name;
  2475. strscpy(vfd->name, dev->devtype->vdevs[i]->name, sizeof(vfd->name));
  2476. vfd->fops = &coda_fops;
  2477. vfd->ioctl_ops = &coda_ioctl_ops;
  2478. vfd->release = video_device_release_empty;
  2479. vfd->lock = &dev->dev_mutex;
  2480. vfd->v4l2_dev = &dev->v4l2_dev;
  2481. vfd->vfl_dir = VFL_DIR_M2M;
  2482. vfd->device_caps = V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING;
  2483. video_set_drvdata(vfd, dev);
  2484. /* Not applicable, use the selection API instead */
  2485. v4l2_disable_ioctl(vfd, VIDIOC_CROPCAP);
  2486. v4l2_disable_ioctl(vfd, VIDIOC_G_CROP);
  2487. v4l2_disable_ioctl(vfd, VIDIOC_S_CROP);
  2488. if (dev->devtype->vdevs[i]->type == CODA_INST_ENCODER) {
  2489. v4l2_disable_ioctl(vfd, VIDIOC_DECODER_CMD);
  2490. v4l2_disable_ioctl(vfd, VIDIOC_TRY_DECODER_CMD);
  2491. if (dev->devtype->vdevs[i]->dst_formats[0] == V4L2_PIX_FMT_JPEG) {
  2492. v4l2_disable_ioctl(vfd, VIDIOC_ENUM_FRAMEINTERVALS);
  2493. v4l2_disable_ioctl(vfd, VIDIOC_G_PARM);
  2494. v4l2_disable_ioctl(vfd, VIDIOC_S_PARM);
  2495. }
  2496. } else {
  2497. v4l2_disable_ioctl(vfd, VIDIOC_ENCODER_CMD);
  2498. v4l2_disable_ioctl(vfd, VIDIOC_TRY_ENCODER_CMD);
  2499. v4l2_disable_ioctl(vfd, VIDIOC_ENUM_FRAMESIZES);
  2500. v4l2_disable_ioctl(vfd, VIDIOC_ENUM_FRAMEINTERVALS);
  2501. v4l2_disable_ioctl(vfd, VIDIOC_G_PARM);
  2502. v4l2_disable_ioctl(vfd, VIDIOC_S_PARM);
  2503. }
  2504. ret = video_register_device(vfd, VFL_TYPE_VIDEO, 0);
  2505. if (!ret)
  2506. v4l2_info(&dev->v4l2_dev, "%s registered as %s\n",
  2507. name, video_device_node_name(vfd));
  2508. return ret;
  2509. }
  2510. static void coda_copy_firmware(struct coda_dev *dev, const u8 * const buf,
  2511. size_t size)
  2512. {
  2513. u32 *src = (u32 *)buf;
  2514. /* Check if the firmware has a 16-byte Freescale header, skip it */
  2515. if (buf[0] == 'M' && buf[1] == 'X')
  2516. src += 4;
  2517. /*
  2518. * Check whether the firmware is in native order or pre-reordered for
  2519. * memory access. The first instruction opcode always is 0xe40e.
  2520. */
  2521. if (__le16_to_cpup((__le16 *)src) == 0xe40e) {
  2522. u32 *dst = dev->codebuf.vaddr;
  2523. int i;
  2524. /* Firmware in native order, reorder while copying */
  2525. if (dev->devtype->product == CODA_DX6) {
  2526. for (i = 0; i < (size - 16) / 4; i++)
  2527. dst[i] = (src[i] << 16) | (src[i] >> 16);
  2528. } else {
  2529. for (i = 0; i < (size - 16) / 4; i += 2) {
  2530. dst[i] = (src[i + 1] << 16) | (src[i + 1] >> 16);
  2531. dst[i + 1] = (src[i] << 16) | (src[i] >> 16);
  2532. }
  2533. }
  2534. } else {
  2535. /* Copy the already reordered firmware image */
  2536. memcpy(dev->codebuf.vaddr, src, size);
  2537. }
  2538. }
  2539. static void coda_fw_callback(const struct firmware *fw, void *context);
  2540. static int coda_firmware_request(struct coda_dev *dev)
  2541. {
  2542. char *fw;
  2543. if (dev->firmware >= ARRAY_SIZE(dev->devtype->firmware))
  2544. return -EINVAL;
  2545. fw = dev->devtype->firmware[dev->firmware];
  2546. dev_dbg(dev->dev, "requesting firmware '%s' for %s\n", fw,
  2547. coda_product_name(dev->devtype->product));
  2548. return request_firmware_nowait(THIS_MODULE, true, fw, dev->dev,
  2549. GFP_KERNEL, dev, coda_fw_callback);
  2550. }
  2551. static void coda_fw_callback(const struct firmware *fw, void *context)
  2552. {
  2553. struct coda_dev *dev = context;
  2554. int i, ret;
  2555. if (!fw) {
  2556. dev->firmware++;
  2557. ret = coda_firmware_request(dev);
  2558. if (ret < 0) {
  2559. v4l2_err(&dev->v4l2_dev, "firmware request failed\n");
  2560. goto put_pm;
  2561. }
  2562. return;
  2563. }
  2564. if (dev->firmware > 0) {
  2565. /*
  2566. * Since we can't suppress warnings for failed asynchronous
  2567. * firmware requests, report that the fallback firmware was
  2568. * found.
  2569. */
  2570. dev_info(dev->dev, "Using fallback firmware %s\n",
  2571. dev->devtype->firmware[dev->firmware]);
  2572. }
  2573. /* allocate auxiliary per-device code buffer for the BIT processor */
  2574. ret = coda_alloc_aux_buf(dev, &dev->codebuf, fw->size, "codebuf",
  2575. dev->debugfs_root);
  2576. if (ret < 0)
  2577. goto put_pm;
  2578. coda_copy_firmware(dev, fw->data, fw->size);
  2579. release_firmware(fw);
  2580. ret = coda_hw_init(dev);
  2581. if (ret < 0) {
  2582. v4l2_err(&dev->v4l2_dev, "HW initialization failed\n");
  2583. goto put_pm;
  2584. }
  2585. ret = coda_check_firmware(dev);
  2586. if (ret < 0)
  2587. goto put_pm;
  2588. dev->m2m_dev = v4l2_m2m_init(&coda_m2m_ops);
  2589. if (IS_ERR(dev->m2m_dev)) {
  2590. v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
  2591. goto put_pm;
  2592. }
  2593. for (i = 0; i < dev->devtype->num_vdevs; i++) {
  2594. ret = coda_register_device(dev, i);
  2595. if (ret) {
  2596. v4l2_err(&dev->v4l2_dev,
  2597. "Failed to register %s video device: %d\n",
  2598. dev->devtype->vdevs[i]->name, ret);
  2599. goto rel_vfd;
  2600. }
  2601. }
  2602. pm_runtime_put_sync(dev->dev);
  2603. return;
  2604. rel_vfd:
  2605. while (--i >= 0)
  2606. video_unregister_device(&dev->vfd[i]);
  2607. v4l2_m2m_release(dev->m2m_dev);
  2608. put_pm:
  2609. pm_runtime_put_sync(dev->dev);
  2610. }
  2611. enum coda_platform {
  2612. CODA_IMX27,
  2613. CODA_IMX51,
  2614. CODA_IMX53,
  2615. CODA_IMX6Q,
  2616. CODA_IMX6DL,
  2617. };
  2618. static const struct coda_devtype coda_devdata[] = {
  2619. [CODA_IMX27] = {
  2620. .firmware = {
  2621. "vpu_fw_imx27_TO2.bin",
  2622. "vpu/vpu_fw_imx27_TO2.bin",
  2623. "v4l-codadx6-imx27.bin"
  2624. },
  2625. .product = CODA_DX6,
  2626. .codecs = codadx6_codecs,
  2627. .num_codecs = ARRAY_SIZE(codadx6_codecs),
  2628. .vdevs = codadx6_video_devices,
  2629. .num_vdevs = ARRAY_SIZE(codadx6_video_devices),
  2630. .workbuf_size = 288 * 1024 + FMO_SLICE_SAVE_BUF_SIZE * 8 * 1024,
  2631. .iram_size = 0xb000,
  2632. },
  2633. [CODA_IMX51] = {
  2634. .firmware = {
  2635. "vpu_fw_imx51.bin",
  2636. "vpu/vpu_fw_imx51.bin",
  2637. "v4l-codahx4-imx51.bin"
  2638. },
  2639. .product = CODA_HX4,
  2640. .codecs = codahx4_codecs,
  2641. .num_codecs = ARRAY_SIZE(codahx4_codecs),
  2642. .vdevs = codahx4_video_devices,
  2643. .num_vdevs = ARRAY_SIZE(codahx4_video_devices),
  2644. .workbuf_size = 128 * 1024,
  2645. .tempbuf_size = 304 * 1024,
  2646. .iram_size = 0x14000,
  2647. },
  2648. [CODA_IMX53] = {
  2649. .firmware = {
  2650. "vpu_fw_imx53.bin",
  2651. "vpu/vpu_fw_imx53.bin",
  2652. "v4l-coda7541-imx53.bin"
  2653. },
  2654. .product = CODA_7541,
  2655. .codecs = coda7_codecs,
  2656. .num_codecs = ARRAY_SIZE(coda7_codecs),
  2657. .vdevs = coda7_video_devices,
  2658. .num_vdevs = ARRAY_SIZE(coda7_video_devices),
  2659. .workbuf_size = 128 * 1024,
  2660. .tempbuf_size = 304 * 1024,
  2661. .iram_size = 0x14000,
  2662. },
  2663. [CODA_IMX6Q] = {
  2664. .firmware = {
  2665. "vpu_fw_imx6q.bin",
  2666. "vpu/vpu_fw_imx6q.bin",
  2667. "v4l-coda960-imx6q.bin"
  2668. },
  2669. .product = CODA_960,
  2670. .codecs = coda9_codecs,
  2671. .num_codecs = ARRAY_SIZE(coda9_codecs),
  2672. .vdevs = coda9_video_devices,
  2673. .num_vdevs = ARRAY_SIZE(coda9_video_devices),
  2674. .workbuf_size = 80 * 1024,
  2675. .tempbuf_size = 204 * 1024,
  2676. .iram_size = 0x21000,
  2677. },
  2678. [CODA_IMX6DL] = {
  2679. .firmware = {
  2680. "vpu_fw_imx6d.bin",
  2681. "vpu/vpu_fw_imx6d.bin",
  2682. "v4l-coda960-imx6dl.bin"
  2683. },
  2684. .product = CODA_960,
  2685. .codecs = coda9_codecs,
  2686. .num_codecs = ARRAY_SIZE(coda9_codecs),
  2687. .vdevs = coda9_video_devices,
  2688. .num_vdevs = ARRAY_SIZE(coda9_video_devices),
  2689. .workbuf_size = 80 * 1024,
  2690. .tempbuf_size = 204 * 1024,
  2691. .iram_size = 0x1f000, /* leave 4k for suspend code */
  2692. },
  2693. };
  2694. static const struct of_device_id coda_dt_ids[] = {
  2695. { .compatible = "fsl,imx27-vpu", .data = &coda_devdata[CODA_IMX27] },
  2696. { .compatible = "fsl,imx51-vpu", .data = &coda_devdata[CODA_IMX51] },
  2697. { .compatible = "fsl,imx53-vpu", .data = &coda_devdata[CODA_IMX53] },
  2698. { .compatible = "fsl,imx6q-vpu", .data = &coda_devdata[CODA_IMX6Q] },
  2699. { .compatible = "fsl,imx6dl-vpu", .data = &coda_devdata[CODA_IMX6DL] },
  2700. { /* sentinel */ }
  2701. };
  2702. MODULE_DEVICE_TABLE(of, coda_dt_ids);
  2703. static int coda_probe(struct platform_device *pdev)
  2704. {
  2705. struct device_node *np = pdev->dev.of_node;
  2706. struct gen_pool *pool;
  2707. struct coda_dev *dev;
  2708. int ret, irq;
  2709. dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
  2710. if (!dev)
  2711. return -ENOMEM;
  2712. dev->devtype = of_device_get_match_data(&pdev->dev);
  2713. dev->dev = &pdev->dev;
  2714. dev->clk_per = devm_clk_get(&pdev->dev, "per");
  2715. if (IS_ERR(dev->clk_per)) {
  2716. dev_err(&pdev->dev, "Could not get per clock\n");
  2717. return PTR_ERR(dev->clk_per);
  2718. }
  2719. dev->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
  2720. if (IS_ERR(dev->clk_ahb)) {
  2721. dev_err(&pdev->dev, "Could not get ahb clock\n");
  2722. return PTR_ERR(dev->clk_ahb);
  2723. }
  2724. /* Get memory for physical registers */
  2725. dev->regs_base = devm_platform_ioremap_resource(pdev, 0);
  2726. if (IS_ERR(dev->regs_base))
  2727. return PTR_ERR(dev->regs_base);
  2728. /* IRQ */
  2729. irq = platform_get_irq_byname(pdev, "bit");
  2730. if (irq < 0)
  2731. irq = platform_get_irq(pdev, 0);
  2732. if (irq < 0)
  2733. return irq;
  2734. ret = devm_request_irq(&pdev->dev, irq, coda_irq_handler, 0,
  2735. CODA_NAME "-video", dev);
  2736. if (ret < 0) {
  2737. dev_err(&pdev->dev, "failed to request irq: %d\n", ret);
  2738. return ret;
  2739. }
  2740. /* JPEG IRQ */
  2741. if (dev->devtype->product == CODA_960) {
  2742. irq = platform_get_irq_byname(pdev, "jpeg");
  2743. if (irq < 0)
  2744. return irq;
  2745. ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
  2746. coda9_jpeg_irq_handler,
  2747. IRQF_ONESHOT, CODA_NAME "-jpeg",
  2748. dev);
  2749. if (ret < 0) {
  2750. dev_err(&pdev->dev, "failed to request jpeg irq\n");
  2751. return ret;
  2752. }
  2753. }
  2754. dev->rstc = devm_reset_control_get_optional_exclusive(&pdev->dev,
  2755. NULL);
  2756. if (IS_ERR(dev->rstc)) {
  2757. ret = PTR_ERR(dev->rstc);
  2758. dev_err(&pdev->dev, "failed get reset control: %d\n", ret);
  2759. return ret;
  2760. }
  2761. /* Get IRAM pool from device tree */
  2762. pool = of_gen_pool_get(np, "iram", 0);
  2763. if (!pool) {
  2764. dev_err(&pdev->dev, "iram pool not available\n");
  2765. return -ENOMEM;
  2766. }
  2767. dev->iram_pool = pool;
  2768. /* Get vdoa_data if supported by the platform */
  2769. dev->vdoa = coda_get_vdoa_data();
  2770. if (PTR_ERR(dev->vdoa) == -EPROBE_DEFER)
  2771. return -EPROBE_DEFER;
  2772. ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
  2773. if (ret)
  2774. return ret;
  2775. ratelimit_default_init(&dev->mb_err_rs);
  2776. mutex_init(&dev->dev_mutex);
  2777. mutex_init(&dev->coda_mutex);
  2778. ida_init(&dev->ida);
  2779. dev->debugfs_root = debugfs_create_dir("coda", NULL);
  2780. /* allocate auxiliary per-device buffers for the BIT processor */
  2781. if (dev->devtype->product == CODA_DX6) {
  2782. ret = coda_alloc_aux_buf(dev, &dev->workbuf,
  2783. dev->devtype->workbuf_size, "workbuf",
  2784. dev->debugfs_root);
  2785. if (ret < 0)
  2786. goto err_v4l2_register;
  2787. }
  2788. if (dev->devtype->tempbuf_size) {
  2789. ret = coda_alloc_aux_buf(dev, &dev->tempbuf,
  2790. dev->devtype->tempbuf_size, "tempbuf",
  2791. dev->debugfs_root);
  2792. if (ret < 0)
  2793. goto err_v4l2_register;
  2794. }
  2795. dev->iram.size = dev->devtype->iram_size;
  2796. dev->iram.vaddr = gen_pool_dma_alloc(dev->iram_pool, dev->iram.size,
  2797. &dev->iram.paddr);
  2798. if (!dev->iram.vaddr) {
  2799. dev_warn(&pdev->dev, "unable to alloc iram\n");
  2800. } else {
  2801. memset(dev->iram.vaddr, 0, dev->iram.size);
  2802. dev->iram.blob.data = dev->iram.vaddr;
  2803. dev->iram.blob.size = dev->iram.size;
  2804. dev->iram.dentry = debugfs_create_blob("iram", 0444,
  2805. dev->debugfs_root,
  2806. &dev->iram.blob);
  2807. }
  2808. dev->workqueue = alloc_workqueue("coda", WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
  2809. if (!dev->workqueue) {
  2810. dev_err(&pdev->dev, "unable to alloc workqueue\n");
  2811. ret = -ENOMEM;
  2812. goto err_v4l2_register;
  2813. }
  2814. platform_set_drvdata(pdev, dev);
  2815. /*
  2816. * Start activated so we can directly call coda_hw_init in
  2817. * coda_fw_callback regardless of whether CONFIG_PM is
  2818. * enabled or whether the device is associated with a PM domain.
  2819. */
  2820. pm_runtime_get_noresume(&pdev->dev);
  2821. pm_runtime_set_active(&pdev->dev);
  2822. pm_runtime_enable(&pdev->dev);
  2823. ret = coda_firmware_request(dev);
  2824. if (ret)
  2825. goto err_alloc_workqueue;
  2826. return 0;
  2827. err_alloc_workqueue:
  2828. pm_runtime_disable(&pdev->dev);
  2829. pm_runtime_put_noidle(&pdev->dev);
  2830. destroy_workqueue(dev->workqueue);
  2831. err_v4l2_register:
  2832. v4l2_device_unregister(&dev->v4l2_dev);
  2833. return ret;
  2834. }
  2835. static int coda_remove(struct platform_device *pdev)
  2836. {
  2837. struct coda_dev *dev = platform_get_drvdata(pdev);
  2838. int i;
  2839. for (i = 0; i < ARRAY_SIZE(dev->vfd); i++) {
  2840. if (video_get_drvdata(&dev->vfd[i]))
  2841. video_unregister_device(&dev->vfd[i]);
  2842. }
  2843. if (dev->m2m_dev)
  2844. v4l2_m2m_release(dev->m2m_dev);
  2845. pm_runtime_disable(&pdev->dev);
  2846. v4l2_device_unregister(&dev->v4l2_dev);
  2847. destroy_workqueue(dev->workqueue);
  2848. if (dev->iram.vaddr)
  2849. gen_pool_free(dev->iram_pool, (unsigned long)dev->iram.vaddr,
  2850. dev->iram.size);
  2851. coda_free_aux_buf(dev, &dev->codebuf);
  2852. coda_free_aux_buf(dev, &dev->tempbuf);
  2853. coda_free_aux_buf(dev, &dev->workbuf);
  2854. debugfs_remove_recursive(dev->debugfs_root);
  2855. ida_destroy(&dev->ida);
  2856. return 0;
  2857. }
  2858. #ifdef CONFIG_PM
  2859. static int coda_runtime_resume(struct device *dev)
  2860. {
  2861. struct coda_dev *cdev = dev_get_drvdata(dev);
  2862. int ret = 0;
  2863. if (dev->pm_domain && cdev->codebuf.vaddr) {
  2864. ret = coda_hw_init(cdev);
  2865. if (ret)
  2866. v4l2_err(&cdev->v4l2_dev, "HW initialization failed\n");
  2867. }
  2868. return ret;
  2869. }
  2870. #endif
  2871. static const struct dev_pm_ops coda_pm_ops = {
  2872. SET_RUNTIME_PM_OPS(NULL, coda_runtime_resume, NULL)
  2873. };
  2874. static struct platform_driver coda_driver = {
  2875. .probe = coda_probe,
  2876. .remove = coda_remove,
  2877. .driver = {
  2878. .name = CODA_NAME,
  2879. .of_match_table = coda_dt_ids,
  2880. .pm = &coda_pm_ops,
  2881. },
  2882. };
  2883. module_platform_driver(coda_driver);
  2884. MODULE_LICENSE("GPL");
  2885. MODULE_AUTHOR("Javier Martin <[email protected]>");
  2886. MODULE_DESCRIPTION("Coda multi-standard codec V4L2 driver");