coda.c 88 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347
  1. /*
  2. * Coda multi-standard codec IP
  3. *
  4. * Copyright (C) 2012 Vista Silicon S.L.
  5. * Javier Martin, <javier.martin@vista-silicon.com>
  6. * Xavier Duret
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. */
  13. #include <linux/clk.h>
  14. #include <linux/delay.h>
  15. #include <linux/firmware.h>
  16. #include <linux/genalloc.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/io.h>
  19. #include <linux/irq.h>
  20. #include <linux/kfifo.h>
  21. #include <linux/module.h>
  22. #include <linux/of_device.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/slab.h>
  25. #include <linux/videodev2.h>
  26. #include <linux/of.h>
  27. #include <linux/platform_data/coda.h>
  28. #include <media/v4l2-ctrls.h>
  29. #include <media/v4l2-device.h>
  30. #include <media/v4l2-event.h>
  31. #include <media/v4l2-ioctl.h>
  32. #include <media/v4l2-mem2mem.h>
  33. #include <media/videobuf2-core.h>
  34. #include <media/videobuf2-dma-contig.h>
  35. #include "coda.h"
  36. #define CODA_NAME "coda"
  37. #define CODADX6_MAX_INSTANCES 4
  38. #define CODA_FMO_BUF_SIZE 32
  39. #define CODADX6_WORK_BUF_SIZE (288 * 1024 + CODA_FMO_BUF_SIZE * 8 * 1024)
  40. #define CODA7_WORK_BUF_SIZE (128 * 1024)
  41. #define CODA7_TEMP_BUF_SIZE (304 * 1024)
  42. #define CODA_PARA_BUF_SIZE (10 * 1024)
  43. #define CODA_ISRAM_SIZE (2048 * 2)
  44. #define CODADX6_IRAM_SIZE 0xb000
  45. #define CODA7_IRAM_SIZE 0x14000
  46. #define CODA7_PS_BUF_SIZE 0x28000
  47. #define CODA_MAX_FRAMEBUFFERS 8
  48. #define CODA_MAX_FRAME_SIZE 0x100000
  49. #define FMO_SLICE_SAVE_BUF_SIZE (32)
  50. #define CODA_DEFAULT_GAMMA 4096
  51. #define MIN_W 176
  52. #define MIN_H 144
  53. #define S_ALIGN 1 /* multiple of 2 */
  54. #define W_ALIGN 1 /* multiple of 2 */
  55. #define H_ALIGN 1 /* multiple of 2 */
  56. #define fh_to_ctx(__fh) container_of(__fh, struct coda_ctx, fh)
  57. static int coda_debug;
  58. module_param(coda_debug, int, 0644);
  59. MODULE_PARM_DESC(coda_debug, "Debug level (0-1)");
  60. enum {
  61. V4L2_M2M_SRC = 0,
  62. V4L2_M2M_DST = 1,
  63. };
  64. enum coda_inst_type {
  65. CODA_INST_ENCODER,
  66. CODA_INST_DECODER,
  67. };
  68. enum coda_product {
  69. CODA_DX6 = 0xf001,
  70. CODA_7541 = 0xf012,
  71. };
  72. struct coda_fmt {
  73. char *name;
  74. u32 fourcc;
  75. };
  76. struct coda_codec {
  77. u32 mode;
  78. u32 src_fourcc;
  79. u32 dst_fourcc;
  80. u32 max_w;
  81. u32 max_h;
  82. };
  83. struct coda_devtype {
  84. char *firmware;
  85. enum coda_product product;
  86. struct coda_codec *codecs;
  87. unsigned int num_codecs;
  88. size_t workbuf_size;
  89. };
  90. /* Per-queue, driver-specific private data */
  91. struct coda_q_data {
  92. unsigned int width;
  93. unsigned int height;
  94. unsigned int sizeimage;
  95. unsigned int fourcc;
  96. };
  97. struct coda_aux_buf {
  98. void *vaddr;
  99. dma_addr_t paddr;
  100. u32 size;
  101. };
  102. struct coda_dev {
  103. struct v4l2_device v4l2_dev;
  104. struct video_device vfd;
  105. struct platform_device *plat_dev;
  106. const struct coda_devtype *devtype;
  107. void __iomem *regs_base;
  108. struct clk *clk_per;
  109. struct clk *clk_ahb;
  110. struct coda_aux_buf codebuf;
  111. struct coda_aux_buf tempbuf;
  112. struct coda_aux_buf workbuf;
  113. struct gen_pool *iram_pool;
  114. long unsigned int iram_vaddr;
  115. long unsigned int iram_paddr;
  116. unsigned long iram_size;
  117. spinlock_t irqlock;
  118. struct mutex dev_mutex;
  119. struct mutex coda_mutex;
  120. struct v4l2_m2m_dev *m2m_dev;
  121. struct vb2_alloc_ctx *alloc_ctx;
  122. struct list_head instances;
  123. unsigned long instance_mask;
  124. struct delayed_work timeout;
  125. };
  126. struct coda_params {
  127. u8 rot_mode;
  128. u8 h264_intra_qp;
  129. u8 h264_inter_qp;
  130. u8 mpeg4_intra_qp;
  131. u8 mpeg4_inter_qp;
  132. u8 gop_size;
  133. int codec_mode;
  134. int codec_mode_aux;
  135. enum v4l2_mpeg_video_multi_slice_mode slice_mode;
  136. u32 framerate;
  137. u16 bitrate;
  138. u32 slice_max_bits;
  139. u32 slice_max_mb;
  140. };
  141. struct coda_iram_info {
  142. u32 axi_sram_use;
  143. phys_addr_t buf_bit_use;
  144. phys_addr_t buf_ip_ac_dc_use;
  145. phys_addr_t buf_dbk_y_use;
  146. phys_addr_t buf_dbk_c_use;
  147. phys_addr_t buf_ovl_use;
  148. phys_addr_t buf_btp_use;
  149. phys_addr_t search_ram_paddr;
  150. int search_ram_size;
  151. };
  152. struct coda_ctx {
  153. struct coda_dev *dev;
  154. struct mutex buffer_mutex;
  155. struct list_head list;
  156. struct work_struct skip_run;
  157. int aborting;
  158. int initialized;
  159. int streamon_out;
  160. int streamon_cap;
  161. u32 isequence;
  162. u32 qsequence;
  163. u32 osequence;
  164. struct coda_q_data q_data[2];
  165. enum coda_inst_type inst_type;
  166. struct coda_codec *codec;
  167. enum v4l2_colorspace colorspace;
  168. struct coda_params params;
  169. struct v4l2_m2m_ctx *m2m_ctx;
  170. struct v4l2_ctrl_handler ctrls;
  171. struct v4l2_fh fh;
  172. int gopcounter;
  173. int runcounter;
  174. char vpu_header[3][64];
  175. int vpu_header_size[3];
  176. struct kfifo bitstream_fifo;
  177. struct mutex bitstream_mutex;
  178. struct coda_aux_buf bitstream;
  179. bool prescan_failed;
  180. struct coda_aux_buf parabuf;
  181. struct coda_aux_buf psbuf;
  182. struct coda_aux_buf slicebuf;
  183. struct coda_aux_buf internal_frames[CODA_MAX_FRAMEBUFFERS];
  184. struct coda_aux_buf workbuf;
  185. int num_internal_frames;
  186. int idx;
  187. int reg_idx;
  188. struct coda_iram_info iram_info;
  189. u32 bit_stream_param;
  190. u32 frm_dis_flg;
  191. int display_idx;
  192. };
  193. static const u8 coda_filler_nal[14] = { 0x00, 0x00, 0x00, 0x01, 0x0c, 0xff,
  194. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80 };
  195. static const u8 coda_filler_size[8] = { 0, 7, 14, 13, 12, 11, 10, 9 };
  196. static inline void coda_write(struct coda_dev *dev, u32 data, u32 reg)
  197. {
  198. v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
  199. "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
  200. writel(data, dev->regs_base + reg);
  201. }
  202. static inline unsigned int coda_read(struct coda_dev *dev, u32 reg)
  203. {
  204. u32 data;
  205. data = readl(dev->regs_base + reg);
  206. v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
  207. "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
  208. return data;
  209. }
  210. static inline unsigned long coda_isbusy(struct coda_dev *dev)
  211. {
  212. return coda_read(dev, CODA_REG_BIT_BUSY);
  213. }
  214. static inline int coda_is_initialized(struct coda_dev *dev)
  215. {
  216. return (coda_read(dev, CODA_REG_BIT_CUR_PC) != 0);
  217. }
  218. static int coda_wait_timeout(struct coda_dev *dev)
  219. {
  220. unsigned long timeout = jiffies + msecs_to_jiffies(1000);
  221. while (coda_isbusy(dev)) {
  222. if (time_after(jiffies, timeout))
  223. return -ETIMEDOUT;
  224. }
  225. return 0;
  226. }
  227. static void coda_command_async(struct coda_ctx *ctx, int cmd)
  228. {
  229. struct coda_dev *dev = ctx->dev;
  230. if (dev->devtype->product == CODA_7541) {
  231. /* Restore context related registers to CODA */
  232. coda_write(dev, ctx->bit_stream_param,
  233. CODA_REG_BIT_BIT_STREAM_PARAM);
  234. coda_write(dev, ctx->frm_dis_flg,
  235. CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
  236. coda_write(dev, ctx->workbuf.paddr, CODA_REG_BIT_WORK_BUF_ADDR);
  237. }
  238. coda_write(dev, CODA_REG_BIT_BUSY_FLAG, CODA_REG_BIT_BUSY);
  239. coda_write(dev, ctx->idx, CODA_REG_BIT_RUN_INDEX);
  240. coda_write(dev, ctx->params.codec_mode, CODA_REG_BIT_RUN_COD_STD);
  241. coda_write(dev, ctx->params.codec_mode_aux, CODA7_REG_BIT_RUN_AUX_STD);
  242. coda_write(dev, cmd, CODA_REG_BIT_RUN_COMMAND);
  243. }
  244. static int coda_command_sync(struct coda_ctx *ctx, int cmd)
  245. {
  246. struct coda_dev *dev = ctx->dev;
  247. coda_command_async(ctx, cmd);
  248. return coda_wait_timeout(dev);
  249. }
  250. static struct coda_q_data *get_q_data(struct coda_ctx *ctx,
  251. enum v4l2_buf_type type)
  252. {
  253. switch (type) {
  254. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  255. return &(ctx->q_data[V4L2_M2M_SRC]);
  256. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  257. return &(ctx->q_data[V4L2_M2M_DST]);
  258. default:
  259. BUG();
  260. }
  261. return NULL;
  262. }
  263. /*
  264. * Array of all formats supported by any version of Coda:
  265. */
  266. static struct coda_fmt coda_formats[] = {
  267. {
  268. .name = "YUV 4:2:0 Planar, YCbCr",
  269. .fourcc = V4L2_PIX_FMT_YUV420,
  270. },
  271. {
  272. .name = "YUV 4:2:0 Planar, YCrCb",
  273. .fourcc = V4L2_PIX_FMT_YVU420,
  274. },
  275. {
  276. .name = "H264 Encoded Stream",
  277. .fourcc = V4L2_PIX_FMT_H264,
  278. },
  279. {
  280. .name = "MPEG4 Encoded Stream",
  281. .fourcc = V4L2_PIX_FMT_MPEG4,
  282. },
  283. };
  284. #define CODA_CODEC(mode, src_fourcc, dst_fourcc, max_w, max_h) \
  285. { mode, src_fourcc, dst_fourcc, max_w, max_h }
  286. /*
  287. * Arrays of codecs supported by each given version of Coda:
  288. * i.MX27 -> codadx6
  289. * i.MX5x -> coda7
  290. * i.MX6 -> coda960
  291. * Use V4L2_PIX_FMT_YUV420 as placeholder for all supported YUV 4:2:0 variants
  292. */
  293. static struct coda_codec codadx6_codecs[] = {
  294. CODA_CODEC(CODADX6_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 720, 576),
  295. CODA_CODEC(CODADX6_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 720, 576),
  296. };
  297. static struct coda_codec coda7_codecs[] = {
  298. CODA_CODEC(CODA7_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 1280, 720),
  299. CODA_CODEC(CODA7_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 1280, 720),
  300. CODA_CODEC(CODA7_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1080),
  301. CODA_CODEC(CODA7_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1920, 1080),
  302. };
  303. static bool coda_format_is_yuv(u32 fourcc)
  304. {
  305. switch (fourcc) {
  306. case V4L2_PIX_FMT_YUV420:
  307. case V4L2_PIX_FMT_YVU420:
  308. return true;
  309. default:
  310. return false;
  311. }
  312. }
  313. /*
  314. * Normalize all supported YUV 4:2:0 formats to the value used in the codec
  315. * tables.
  316. */
  317. static u32 coda_format_normalize_yuv(u32 fourcc)
  318. {
  319. return coda_format_is_yuv(fourcc) ? V4L2_PIX_FMT_YUV420 : fourcc;
  320. }
  321. static struct coda_codec *coda_find_codec(struct coda_dev *dev, int src_fourcc,
  322. int dst_fourcc)
  323. {
  324. struct coda_codec *codecs = dev->devtype->codecs;
  325. int num_codecs = dev->devtype->num_codecs;
  326. int k;
  327. src_fourcc = coda_format_normalize_yuv(src_fourcc);
  328. dst_fourcc = coda_format_normalize_yuv(dst_fourcc);
  329. if (src_fourcc == dst_fourcc)
  330. return NULL;
  331. for (k = 0; k < num_codecs; k++) {
  332. if (codecs[k].src_fourcc == src_fourcc &&
  333. codecs[k].dst_fourcc == dst_fourcc)
  334. break;
  335. }
  336. if (k == num_codecs)
  337. return NULL;
  338. return &codecs[k];
  339. }
  340. static void coda_get_max_dimensions(struct coda_dev *dev,
  341. struct coda_codec *codec,
  342. int *max_w, int *max_h)
  343. {
  344. struct coda_codec *codecs = dev->devtype->codecs;
  345. int num_codecs = dev->devtype->num_codecs;
  346. unsigned int w, h;
  347. int k;
  348. if (codec) {
  349. w = codec->max_w;
  350. h = codec->max_h;
  351. } else {
  352. for (k = 0, w = 0, h = 0; k < num_codecs; k++) {
  353. w = max(w, codecs[k].max_w);
  354. h = max(h, codecs[k].max_h);
  355. }
  356. }
  357. if (max_w)
  358. *max_w = w;
  359. if (max_h)
  360. *max_h = h;
  361. }
  362. static char *coda_product_name(int product)
  363. {
  364. static char buf[9];
  365. switch (product) {
  366. case CODA_DX6:
  367. return "CodaDx6";
  368. case CODA_7541:
  369. return "CODA7541";
  370. default:
  371. snprintf(buf, sizeof(buf), "(0x%04x)", product);
  372. return buf;
  373. }
  374. }
  375. /*
  376. * V4L2 ioctl() operations.
  377. */
  378. static int coda_querycap(struct file *file, void *priv,
  379. struct v4l2_capability *cap)
  380. {
  381. struct coda_ctx *ctx = fh_to_ctx(priv);
  382. strlcpy(cap->driver, CODA_NAME, sizeof(cap->driver));
  383. strlcpy(cap->card, coda_product_name(ctx->dev->devtype->product),
  384. sizeof(cap->card));
  385. strlcpy(cap->bus_info, "platform:" CODA_NAME, sizeof(cap->bus_info));
  386. /*
  387. * This is only a mem-to-mem video device. The capture and output
  388. * device capability flags are left only for backward compatibility
  389. * and are scheduled for removal.
  390. */
  391. cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OUTPUT |
  392. V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING;
  393. cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
  394. return 0;
  395. }
  396. static int enum_fmt(void *priv, struct v4l2_fmtdesc *f,
  397. enum v4l2_buf_type type, int src_fourcc)
  398. {
  399. struct coda_ctx *ctx = fh_to_ctx(priv);
  400. struct coda_codec *codecs = ctx->dev->devtype->codecs;
  401. struct coda_fmt *formats = coda_formats;
  402. struct coda_fmt *fmt;
  403. int num_codecs = ctx->dev->devtype->num_codecs;
  404. int num_formats = ARRAY_SIZE(coda_formats);
  405. int i, k, num = 0;
  406. for (i = 0; i < num_formats; i++) {
  407. /* Both uncompressed formats are always supported */
  408. if (coda_format_is_yuv(formats[i].fourcc) &&
  409. !coda_format_is_yuv(src_fourcc)) {
  410. if (num == f->index)
  411. break;
  412. ++num;
  413. continue;
  414. }
  415. /* Compressed formats may be supported, check the codec list */
  416. for (k = 0; k < num_codecs; k++) {
  417. /* if src_fourcc is set, only consider matching codecs */
  418. if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
  419. formats[i].fourcc == codecs[k].dst_fourcc &&
  420. (!src_fourcc || src_fourcc == codecs[k].src_fourcc))
  421. break;
  422. if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
  423. formats[i].fourcc == codecs[k].src_fourcc)
  424. break;
  425. }
  426. if (k < num_codecs) {
  427. if (num == f->index)
  428. break;
  429. ++num;
  430. }
  431. }
  432. if (i < num_formats) {
  433. fmt = &formats[i];
  434. strlcpy(f->description, fmt->name, sizeof(f->description));
  435. f->pixelformat = fmt->fourcc;
  436. if (!coda_format_is_yuv(fmt->fourcc))
  437. f->flags |= V4L2_FMT_FLAG_COMPRESSED;
  438. return 0;
  439. }
  440. /* Format not found */
  441. return -EINVAL;
  442. }
  443. static int coda_enum_fmt_vid_cap(struct file *file, void *priv,
  444. struct v4l2_fmtdesc *f)
  445. {
  446. struct coda_ctx *ctx = fh_to_ctx(priv);
  447. struct vb2_queue *src_vq;
  448. struct coda_q_data *q_data_src;
  449. /* If the source format is already fixed, only list matching formats */
  450. src_vq = v4l2_m2m_get_vq(ctx->m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
  451. if (vb2_is_streaming(src_vq)) {
  452. q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
  453. return enum_fmt(priv, f, V4L2_BUF_TYPE_VIDEO_CAPTURE,
  454. q_data_src->fourcc);
  455. }
  456. return enum_fmt(priv, f, V4L2_BUF_TYPE_VIDEO_CAPTURE, 0);
  457. }
  458. static int coda_enum_fmt_vid_out(struct file *file, void *priv,
  459. struct v4l2_fmtdesc *f)
  460. {
  461. return enum_fmt(priv, f, V4L2_BUF_TYPE_VIDEO_OUTPUT, 0);
  462. }
  463. static int coda_g_fmt(struct file *file, void *priv,
  464. struct v4l2_format *f)
  465. {
  466. struct vb2_queue *vq;
  467. struct coda_q_data *q_data;
  468. struct coda_ctx *ctx = fh_to_ctx(priv);
  469. vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
  470. if (!vq)
  471. return -EINVAL;
  472. q_data = get_q_data(ctx, f->type);
  473. f->fmt.pix.field = V4L2_FIELD_NONE;
  474. f->fmt.pix.pixelformat = q_data->fourcc;
  475. f->fmt.pix.width = q_data->width;
  476. f->fmt.pix.height = q_data->height;
  477. if (coda_format_is_yuv(f->fmt.pix.pixelformat))
  478. f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 2);
  479. else /* encoded formats h.264/mpeg4 */
  480. f->fmt.pix.bytesperline = 0;
  481. f->fmt.pix.sizeimage = q_data->sizeimage;
  482. f->fmt.pix.colorspace = ctx->colorspace;
  483. return 0;
  484. }
  485. static int coda_try_fmt(struct coda_ctx *ctx, struct coda_codec *codec,
  486. struct v4l2_format *f)
  487. {
  488. struct coda_dev *dev = ctx->dev;
  489. struct coda_q_data *q_data;
  490. unsigned int max_w, max_h;
  491. enum v4l2_field field;
  492. field = f->fmt.pix.field;
  493. if (field == V4L2_FIELD_ANY)
  494. field = V4L2_FIELD_NONE;
  495. else if (V4L2_FIELD_NONE != field)
  496. return -EINVAL;
  497. /* V4L2 specification suggests the driver corrects the format struct
  498. * if any of the dimensions is unsupported */
  499. f->fmt.pix.field = field;
  500. coda_get_max_dimensions(dev, codec, &max_w, &max_h);
  501. v4l_bound_align_image(&f->fmt.pix.width, MIN_W, max_w, W_ALIGN,
  502. &f->fmt.pix.height, MIN_H, max_h, H_ALIGN,
  503. S_ALIGN);
  504. switch (f->fmt.pix.pixelformat) {
  505. case V4L2_PIX_FMT_YUV420:
  506. case V4L2_PIX_FMT_YVU420:
  507. case V4L2_PIX_FMT_H264:
  508. case V4L2_PIX_FMT_MPEG4:
  509. case V4L2_PIX_FMT_JPEG:
  510. break;
  511. default:
  512. q_data = get_q_data(ctx, f->type);
  513. f->fmt.pix.pixelformat = q_data->fourcc;
  514. }
  515. switch (f->fmt.pix.pixelformat) {
  516. case V4L2_PIX_FMT_YUV420:
  517. case V4L2_PIX_FMT_YVU420:
  518. /* Frame stride must be multiple of 8 */
  519. f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 8);
  520. f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
  521. f->fmt.pix.height * 3 / 2;
  522. break;
  523. case V4L2_PIX_FMT_H264:
  524. case V4L2_PIX_FMT_MPEG4:
  525. case V4L2_PIX_FMT_JPEG:
  526. f->fmt.pix.bytesperline = 0;
  527. f->fmt.pix.sizeimage = CODA_MAX_FRAME_SIZE;
  528. break;
  529. default:
  530. BUG();
  531. }
  532. f->fmt.pix.priv = 0;
  533. return 0;
  534. }
  535. static int coda_try_fmt_vid_cap(struct file *file, void *priv,
  536. struct v4l2_format *f)
  537. {
  538. struct coda_ctx *ctx = fh_to_ctx(priv);
  539. struct coda_codec *codec;
  540. struct vb2_queue *src_vq;
  541. int ret;
  542. /*
  543. * If the source format is already fixed, try to find a codec that
  544. * converts to the given destination format
  545. */
  546. src_vq = v4l2_m2m_get_vq(ctx->m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
  547. if (vb2_is_streaming(src_vq)) {
  548. struct coda_q_data *q_data_src;
  549. q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
  550. codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
  551. f->fmt.pix.pixelformat);
  552. if (!codec)
  553. return -EINVAL;
  554. } else {
  555. /* Otherwise determine codec by encoded format, if possible */
  556. codec = coda_find_codec(ctx->dev, V4L2_PIX_FMT_YUV420,
  557. f->fmt.pix.pixelformat);
  558. }
  559. f->fmt.pix.colorspace = ctx->colorspace;
  560. ret = coda_try_fmt(ctx, codec, f);
  561. if (ret < 0)
  562. return ret;
  563. /* The h.264 decoder only returns complete 16x16 macroblocks */
  564. if (codec && codec->src_fourcc == V4L2_PIX_FMT_H264) {
  565. f->fmt.pix.width = round_up(f->fmt.pix.width, 16);
  566. f->fmt.pix.height = round_up(f->fmt.pix.height, 16);
  567. f->fmt.pix.bytesperline = f->fmt.pix.width;
  568. f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
  569. f->fmt.pix.height * 3 / 2;
  570. }
  571. return 0;
  572. }
  573. static int coda_try_fmt_vid_out(struct file *file, void *priv,
  574. struct v4l2_format *f)
  575. {
  576. struct coda_ctx *ctx = fh_to_ctx(priv);
  577. struct coda_codec *codec;
  578. /* Determine codec by encoded format, returns NULL if raw or invalid */
  579. codec = coda_find_codec(ctx->dev, f->fmt.pix.pixelformat,
  580. V4L2_PIX_FMT_YUV420);
  581. if (!f->fmt.pix.colorspace)
  582. f->fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
  583. return coda_try_fmt(ctx, codec, f);
  584. }
  585. static int coda_s_fmt(struct coda_ctx *ctx, struct v4l2_format *f)
  586. {
  587. struct coda_q_data *q_data;
  588. struct vb2_queue *vq;
  589. vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
  590. if (!vq)
  591. return -EINVAL;
  592. q_data = get_q_data(ctx, f->type);
  593. if (!q_data)
  594. return -EINVAL;
  595. if (vb2_is_busy(vq)) {
  596. v4l2_err(&ctx->dev->v4l2_dev, "%s queue busy\n", __func__);
  597. return -EBUSY;
  598. }
  599. q_data->fourcc = f->fmt.pix.pixelformat;
  600. q_data->width = f->fmt.pix.width;
  601. q_data->height = f->fmt.pix.height;
  602. q_data->sizeimage = f->fmt.pix.sizeimage;
  603. v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
  604. "Setting format for type %d, wxh: %dx%d, fmt: %d\n",
  605. f->type, q_data->width, q_data->height, q_data->fourcc);
  606. return 0;
  607. }
  608. static int coda_s_fmt_vid_cap(struct file *file, void *priv,
  609. struct v4l2_format *f)
  610. {
  611. struct coda_ctx *ctx = fh_to_ctx(priv);
  612. int ret;
  613. ret = coda_try_fmt_vid_cap(file, priv, f);
  614. if (ret)
  615. return ret;
  616. return coda_s_fmt(ctx, f);
  617. }
  618. static int coda_s_fmt_vid_out(struct file *file, void *priv,
  619. struct v4l2_format *f)
  620. {
  621. struct coda_ctx *ctx = fh_to_ctx(priv);
  622. int ret;
  623. ret = coda_try_fmt_vid_out(file, priv, f);
  624. if (ret)
  625. return ret;
  626. ret = coda_s_fmt(ctx, f);
  627. if (ret)
  628. ctx->colorspace = f->fmt.pix.colorspace;
  629. return ret;
  630. }
  631. static int coda_reqbufs(struct file *file, void *priv,
  632. struct v4l2_requestbuffers *reqbufs)
  633. {
  634. struct coda_ctx *ctx = fh_to_ctx(priv);
  635. return v4l2_m2m_reqbufs(file, ctx->m2m_ctx, reqbufs);
  636. }
  637. static int coda_querybuf(struct file *file, void *priv,
  638. struct v4l2_buffer *buf)
  639. {
  640. struct coda_ctx *ctx = fh_to_ctx(priv);
  641. return v4l2_m2m_querybuf(file, ctx->m2m_ctx, buf);
  642. }
  643. static int coda_qbuf(struct file *file, void *priv,
  644. struct v4l2_buffer *buf)
  645. {
  646. struct coda_ctx *ctx = fh_to_ctx(priv);
  647. return v4l2_m2m_qbuf(file, ctx->m2m_ctx, buf);
  648. }
  649. static int coda_expbuf(struct file *file, void *priv,
  650. struct v4l2_exportbuffer *eb)
  651. {
  652. struct coda_ctx *ctx = fh_to_ctx(priv);
  653. return v4l2_m2m_expbuf(file, ctx->m2m_ctx, eb);
  654. }
  655. static bool coda_buf_is_end_of_stream(struct coda_ctx *ctx,
  656. struct v4l2_buffer *buf)
  657. {
  658. struct vb2_queue *src_vq;
  659. src_vq = v4l2_m2m_get_vq(ctx->m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
  660. return ((ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG) &&
  661. (buf->sequence == (ctx->qsequence - 1)));
  662. }
  663. static int coda_dqbuf(struct file *file, void *priv,
  664. struct v4l2_buffer *buf)
  665. {
  666. struct coda_ctx *ctx = fh_to_ctx(priv);
  667. int ret;
  668. ret = v4l2_m2m_dqbuf(file, ctx->m2m_ctx, buf);
  669. /* If this is the last capture buffer, emit an end-of-stream event */
  670. if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
  671. coda_buf_is_end_of_stream(ctx, buf)) {
  672. const struct v4l2_event eos_event = {
  673. .type = V4L2_EVENT_EOS
  674. };
  675. v4l2_event_queue_fh(&ctx->fh, &eos_event);
  676. }
  677. return ret;
  678. }
  679. static int coda_create_bufs(struct file *file, void *priv,
  680. struct v4l2_create_buffers *create)
  681. {
  682. struct coda_ctx *ctx = fh_to_ctx(priv);
  683. return v4l2_m2m_create_bufs(file, ctx->m2m_ctx, create);
  684. }
  685. static int coda_streamon(struct file *file, void *priv,
  686. enum v4l2_buf_type type)
  687. {
  688. struct coda_ctx *ctx = fh_to_ctx(priv);
  689. return v4l2_m2m_streamon(file, ctx->m2m_ctx, type);
  690. }
  691. static int coda_streamoff(struct file *file, void *priv,
  692. enum v4l2_buf_type type)
  693. {
  694. struct coda_ctx *ctx = fh_to_ctx(priv);
  695. int ret;
  696. /*
  697. * This indirectly calls __vb2_queue_cancel, which dequeues all buffers.
  698. * We therefore have to lock it against running hardware in this context,
  699. * which still needs the buffers.
  700. */
  701. mutex_lock(&ctx->buffer_mutex);
  702. ret = v4l2_m2m_streamoff(file, ctx->m2m_ctx, type);
  703. mutex_unlock(&ctx->buffer_mutex);
  704. return ret;
  705. }
  706. static int coda_try_decoder_cmd(struct file *file, void *fh,
  707. struct v4l2_decoder_cmd *dc)
  708. {
  709. if (dc->cmd != V4L2_DEC_CMD_STOP)
  710. return -EINVAL;
  711. if (dc->flags & V4L2_DEC_CMD_STOP_TO_BLACK)
  712. return -EINVAL;
  713. if (!(dc->flags & V4L2_DEC_CMD_STOP_IMMEDIATELY) && (dc->stop.pts != 0))
  714. return -EINVAL;
  715. return 0;
  716. }
  717. static int coda_decoder_cmd(struct file *file, void *fh,
  718. struct v4l2_decoder_cmd *dc)
  719. {
  720. struct coda_ctx *ctx = fh_to_ctx(fh);
  721. int ret;
  722. ret = coda_try_decoder_cmd(file, fh, dc);
  723. if (ret < 0)
  724. return ret;
  725. /* Ignore decoder stop command silently in encoder context */
  726. if (ctx->inst_type != CODA_INST_DECODER)
  727. return 0;
  728. /* Set the strem-end flag on this context */
  729. ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
  730. return 0;
  731. }
  732. static int coda_subscribe_event(struct v4l2_fh *fh,
  733. const struct v4l2_event_subscription *sub)
  734. {
  735. switch (sub->type) {
  736. case V4L2_EVENT_EOS:
  737. return v4l2_event_subscribe(fh, sub, 0, NULL);
  738. default:
  739. return v4l2_ctrl_subscribe_event(fh, sub);
  740. }
  741. }
  742. static const struct v4l2_ioctl_ops coda_ioctl_ops = {
  743. .vidioc_querycap = coda_querycap,
  744. .vidioc_enum_fmt_vid_cap = coda_enum_fmt_vid_cap,
  745. .vidioc_g_fmt_vid_cap = coda_g_fmt,
  746. .vidioc_try_fmt_vid_cap = coda_try_fmt_vid_cap,
  747. .vidioc_s_fmt_vid_cap = coda_s_fmt_vid_cap,
  748. .vidioc_enum_fmt_vid_out = coda_enum_fmt_vid_out,
  749. .vidioc_g_fmt_vid_out = coda_g_fmt,
  750. .vidioc_try_fmt_vid_out = coda_try_fmt_vid_out,
  751. .vidioc_s_fmt_vid_out = coda_s_fmt_vid_out,
  752. .vidioc_reqbufs = coda_reqbufs,
  753. .vidioc_querybuf = coda_querybuf,
  754. .vidioc_qbuf = coda_qbuf,
  755. .vidioc_expbuf = coda_expbuf,
  756. .vidioc_dqbuf = coda_dqbuf,
  757. .vidioc_create_bufs = coda_create_bufs,
  758. .vidioc_streamon = coda_streamon,
  759. .vidioc_streamoff = coda_streamoff,
  760. .vidioc_try_decoder_cmd = coda_try_decoder_cmd,
  761. .vidioc_decoder_cmd = coda_decoder_cmd,
  762. .vidioc_subscribe_event = coda_subscribe_event,
  763. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  764. };
  765. static int coda_start_decoding(struct coda_ctx *ctx);
  766. static void coda_skip_run(struct work_struct *work)
  767. {
  768. struct coda_ctx *ctx = container_of(work, struct coda_ctx, skip_run);
  769. v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->m2m_ctx);
  770. }
  771. static inline int coda_get_bitstream_payload(struct coda_ctx *ctx)
  772. {
  773. return kfifo_len(&ctx->bitstream_fifo);
  774. }
  775. static void coda_kfifo_sync_from_device(struct coda_ctx *ctx)
  776. {
  777. struct __kfifo *kfifo = &ctx->bitstream_fifo.kfifo;
  778. struct coda_dev *dev = ctx->dev;
  779. u32 rd_ptr;
  780. rd_ptr = coda_read(dev, CODA_REG_BIT_RD_PTR(ctx->reg_idx));
  781. kfifo->out = (kfifo->in & ~kfifo->mask) |
  782. (rd_ptr - ctx->bitstream.paddr);
  783. if (kfifo->out > kfifo->in)
  784. kfifo->out -= kfifo->mask + 1;
  785. }
  786. static void coda_kfifo_sync_to_device_full(struct coda_ctx *ctx)
  787. {
  788. struct __kfifo *kfifo = &ctx->bitstream_fifo.kfifo;
  789. struct coda_dev *dev = ctx->dev;
  790. u32 rd_ptr, wr_ptr;
  791. rd_ptr = ctx->bitstream.paddr + (kfifo->out & kfifo->mask);
  792. coda_write(dev, rd_ptr, CODA_REG_BIT_RD_PTR(ctx->reg_idx));
  793. wr_ptr = ctx->bitstream.paddr + (kfifo->in & kfifo->mask);
  794. coda_write(dev, wr_ptr, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
  795. }
  796. static void coda_kfifo_sync_to_device_write(struct coda_ctx *ctx)
  797. {
  798. struct __kfifo *kfifo = &ctx->bitstream_fifo.kfifo;
  799. struct coda_dev *dev = ctx->dev;
  800. u32 wr_ptr;
  801. wr_ptr = ctx->bitstream.paddr + (kfifo->in & kfifo->mask);
  802. coda_write(dev, wr_ptr, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
  803. }
  804. static int coda_bitstream_queue(struct coda_ctx *ctx, struct vb2_buffer *src_buf)
  805. {
  806. u32 src_size = vb2_get_plane_payload(src_buf, 0);
  807. u32 n;
  808. n = kfifo_in(&ctx->bitstream_fifo, vb2_plane_vaddr(src_buf, 0), src_size);
  809. if (n < src_size)
  810. return -ENOSPC;
  811. dma_sync_single_for_device(&ctx->dev->plat_dev->dev, ctx->bitstream.paddr,
  812. ctx->bitstream.size, DMA_TO_DEVICE);
  813. ctx->qsequence++;
  814. return 0;
  815. }
  816. static bool coda_bitstream_try_queue(struct coda_ctx *ctx,
  817. struct vb2_buffer *src_buf)
  818. {
  819. int ret;
  820. if (coda_get_bitstream_payload(ctx) +
  821. vb2_get_plane_payload(src_buf, 0) + 512 >= ctx->bitstream.size)
  822. return false;
  823. if (vb2_plane_vaddr(src_buf, 0) == NULL) {
  824. v4l2_err(&ctx->dev->v4l2_dev, "trying to queue empty buffer\n");
  825. return true;
  826. }
  827. ret = coda_bitstream_queue(ctx, src_buf);
  828. if (ret < 0) {
  829. v4l2_err(&ctx->dev->v4l2_dev, "bitstream buffer overflow\n");
  830. return false;
  831. }
  832. /* Sync read pointer to device */
  833. if (ctx == v4l2_m2m_get_curr_priv(ctx->dev->m2m_dev))
  834. coda_kfifo_sync_to_device_write(ctx);
  835. ctx->prescan_failed = false;
  836. return true;
  837. }
  838. static void coda_fill_bitstream(struct coda_ctx *ctx)
  839. {
  840. struct vb2_buffer *src_buf;
  841. while (v4l2_m2m_num_src_bufs_ready(ctx->m2m_ctx) > 0) {
  842. src_buf = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
  843. if (coda_bitstream_try_queue(ctx, src_buf)) {
  844. src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
  845. v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
  846. } else {
  847. break;
  848. }
  849. }
  850. }
  851. /*
  852. * Mem-to-mem operations.
  853. */
  854. static int coda_prepare_decode(struct coda_ctx *ctx)
  855. {
  856. struct vb2_buffer *dst_buf;
  857. struct coda_dev *dev = ctx->dev;
  858. struct coda_q_data *q_data_dst;
  859. u32 stridey, height;
  860. u32 picture_y, picture_cb, picture_cr;
  861. dst_buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
  862. q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
  863. if (ctx->params.rot_mode & CODA_ROT_90) {
  864. stridey = q_data_dst->height;
  865. height = q_data_dst->width;
  866. } else {
  867. stridey = q_data_dst->width;
  868. height = q_data_dst->height;
  869. }
  870. /* Try to copy source buffer contents into the bitstream ringbuffer */
  871. mutex_lock(&ctx->bitstream_mutex);
  872. coda_fill_bitstream(ctx);
  873. mutex_unlock(&ctx->bitstream_mutex);
  874. if (coda_get_bitstream_payload(ctx) < 512 &&
  875. (!(ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG))) {
  876. v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
  877. "bitstream payload: %d, skipping\n",
  878. coda_get_bitstream_payload(ctx));
  879. schedule_work(&ctx->skip_run);
  880. return -EAGAIN;
  881. }
  882. /* Run coda_start_decoding (again) if not yet initialized */
  883. if (!ctx->initialized) {
  884. int ret = coda_start_decoding(ctx);
  885. if (ret < 0) {
  886. v4l2_err(&dev->v4l2_dev, "failed to start decoding\n");
  887. schedule_work(&ctx->skip_run);
  888. return -EAGAIN;
  889. } else {
  890. ctx->initialized = 1;
  891. }
  892. }
  893. /* Set rotator output */
  894. picture_y = vb2_dma_contig_plane_dma_addr(dst_buf, 0);
  895. if (q_data_dst->fourcc == V4L2_PIX_FMT_YVU420) {
  896. /* Switch Cr and Cb for YVU420 format */
  897. picture_cr = picture_y + stridey * height;
  898. picture_cb = picture_cr + stridey / 2 * height / 2;
  899. } else {
  900. picture_cb = picture_y + stridey * height;
  901. picture_cr = picture_cb + stridey / 2 * height / 2;
  902. }
  903. coda_write(dev, picture_y, CODA_CMD_DEC_PIC_ROT_ADDR_Y);
  904. coda_write(dev, picture_cb, CODA_CMD_DEC_PIC_ROT_ADDR_CB);
  905. coda_write(dev, picture_cr, CODA_CMD_DEC_PIC_ROT_ADDR_CR);
  906. coda_write(dev, stridey, CODA_CMD_DEC_PIC_ROT_STRIDE);
  907. coda_write(dev, CODA_ROT_MIR_ENABLE | ctx->params.rot_mode,
  908. CODA_CMD_DEC_PIC_ROT_MODE);
  909. switch (dev->devtype->product) {
  910. case CODA_DX6:
  911. /* TBD */
  912. case CODA_7541:
  913. coda_write(dev, CODA_PRE_SCAN_EN, CODA_CMD_DEC_PIC_OPTION);
  914. break;
  915. }
  916. coda_write(dev, 0, CODA_CMD_DEC_PIC_SKIP_NUM);
  917. coda_write(dev, 0, CODA_CMD_DEC_PIC_BB_START);
  918. coda_write(dev, 0, CODA_CMD_DEC_PIC_START_BYTE);
  919. return 0;
  920. }
  921. static void coda_prepare_encode(struct coda_ctx *ctx)
  922. {
  923. struct coda_q_data *q_data_src, *q_data_dst;
  924. struct vb2_buffer *src_buf, *dst_buf;
  925. struct coda_dev *dev = ctx->dev;
  926. int force_ipicture;
  927. int quant_param = 0;
  928. u32 picture_y, picture_cb, picture_cr;
  929. u32 pic_stream_buffer_addr, pic_stream_buffer_size;
  930. u32 dst_fourcc;
  931. src_buf = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
  932. dst_buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
  933. q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
  934. q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
  935. dst_fourcc = q_data_dst->fourcc;
  936. src_buf->v4l2_buf.sequence = ctx->osequence;
  937. dst_buf->v4l2_buf.sequence = ctx->osequence;
  938. ctx->osequence++;
  939. /*
  940. * Workaround coda firmware BUG that only marks the first
  941. * frame as IDR. This is a problem for some decoders that can't
  942. * recover when a frame is lost.
  943. */
  944. if (src_buf->v4l2_buf.sequence % ctx->params.gop_size) {
  945. src_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_PFRAME;
  946. src_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_KEYFRAME;
  947. } else {
  948. src_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_KEYFRAME;
  949. src_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_PFRAME;
  950. }
  951. /*
  952. * Copy headers at the beginning of the first frame for H.264 only.
  953. * In MPEG4 they are already copied by the coda.
  954. */
  955. if (src_buf->v4l2_buf.sequence == 0) {
  956. pic_stream_buffer_addr =
  957. vb2_dma_contig_plane_dma_addr(dst_buf, 0) +
  958. ctx->vpu_header_size[0] +
  959. ctx->vpu_header_size[1] +
  960. ctx->vpu_header_size[2];
  961. pic_stream_buffer_size = CODA_MAX_FRAME_SIZE -
  962. ctx->vpu_header_size[0] -
  963. ctx->vpu_header_size[1] -
  964. ctx->vpu_header_size[2];
  965. memcpy(vb2_plane_vaddr(dst_buf, 0),
  966. &ctx->vpu_header[0][0], ctx->vpu_header_size[0]);
  967. memcpy(vb2_plane_vaddr(dst_buf, 0) + ctx->vpu_header_size[0],
  968. &ctx->vpu_header[1][0], ctx->vpu_header_size[1]);
  969. memcpy(vb2_plane_vaddr(dst_buf, 0) + ctx->vpu_header_size[0] +
  970. ctx->vpu_header_size[1], &ctx->vpu_header[2][0],
  971. ctx->vpu_header_size[2]);
  972. } else {
  973. pic_stream_buffer_addr =
  974. vb2_dma_contig_plane_dma_addr(dst_buf, 0);
  975. pic_stream_buffer_size = CODA_MAX_FRAME_SIZE;
  976. }
  977. if (src_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) {
  978. force_ipicture = 1;
  979. switch (dst_fourcc) {
  980. case V4L2_PIX_FMT_H264:
  981. quant_param = ctx->params.h264_intra_qp;
  982. break;
  983. case V4L2_PIX_FMT_MPEG4:
  984. quant_param = ctx->params.mpeg4_intra_qp;
  985. break;
  986. default:
  987. v4l2_warn(&ctx->dev->v4l2_dev,
  988. "cannot set intra qp, fmt not supported\n");
  989. break;
  990. }
  991. } else {
  992. force_ipicture = 0;
  993. switch (dst_fourcc) {
  994. case V4L2_PIX_FMT_H264:
  995. quant_param = ctx->params.h264_inter_qp;
  996. break;
  997. case V4L2_PIX_FMT_MPEG4:
  998. quant_param = ctx->params.mpeg4_inter_qp;
  999. break;
  1000. default:
  1001. v4l2_warn(&ctx->dev->v4l2_dev,
  1002. "cannot set inter qp, fmt not supported\n");
  1003. break;
  1004. }
  1005. }
  1006. /* submit */
  1007. coda_write(dev, CODA_ROT_MIR_ENABLE | ctx->params.rot_mode, CODA_CMD_ENC_PIC_ROT_MODE);
  1008. coda_write(dev, quant_param, CODA_CMD_ENC_PIC_QS);
  1009. picture_y = vb2_dma_contig_plane_dma_addr(src_buf, 0);
  1010. switch (q_data_src->fourcc) {
  1011. case V4L2_PIX_FMT_YVU420:
  1012. /* Switch Cb and Cr for YVU420 format */
  1013. picture_cr = picture_y + q_data_src->width * q_data_src->height;
  1014. picture_cb = picture_cr + q_data_src->width / 2 *
  1015. q_data_src->height / 2;
  1016. break;
  1017. case V4L2_PIX_FMT_YUV420:
  1018. default:
  1019. picture_cb = picture_y + q_data_src->width * q_data_src->height;
  1020. picture_cr = picture_cb + q_data_src->width / 2 *
  1021. q_data_src->height / 2;
  1022. break;
  1023. }
  1024. coda_write(dev, picture_y, CODA_CMD_ENC_PIC_SRC_ADDR_Y);
  1025. coda_write(dev, picture_cb, CODA_CMD_ENC_PIC_SRC_ADDR_CB);
  1026. coda_write(dev, picture_cr, CODA_CMD_ENC_PIC_SRC_ADDR_CR);
  1027. coda_write(dev, force_ipicture << 1 & 0x2,
  1028. CODA_CMD_ENC_PIC_OPTION);
  1029. coda_write(dev, pic_stream_buffer_addr, CODA_CMD_ENC_PIC_BB_START);
  1030. coda_write(dev, pic_stream_buffer_size / 1024,
  1031. CODA_CMD_ENC_PIC_BB_SIZE);
  1032. }
  1033. static void coda_device_run(void *m2m_priv)
  1034. {
  1035. struct coda_ctx *ctx = m2m_priv;
  1036. struct coda_dev *dev = ctx->dev;
  1037. int ret;
  1038. mutex_lock(&ctx->buffer_mutex);
  1039. /*
  1040. * If streamoff dequeued all buffers before we could get the lock,
  1041. * just bail out immediately.
  1042. */
  1043. if ((!v4l2_m2m_num_src_bufs_ready(ctx->m2m_ctx) &&
  1044. ctx->inst_type != CODA_INST_DECODER) ||
  1045. !v4l2_m2m_num_dst_bufs_ready(ctx->m2m_ctx)) {
  1046. v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
  1047. "%d: device_run without buffers\n", ctx->idx);
  1048. mutex_unlock(&ctx->buffer_mutex);
  1049. schedule_work(&ctx->skip_run);
  1050. return;
  1051. }
  1052. mutex_lock(&dev->coda_mutex);
  1053. if (ctx->inst_type == CODA_INST_DECODER) {
  1054. ret = coda_prepare_decode(ctx);
  1055. if (ret < 0) {
  1056. mutex_unlock(&dev->coda_mutex);
  1057. mutex_unlock(&ctx->buffer_mutex);
  1058. /* job_finish scheduled by prepare_decode */
  1059. return;
  1060. }
  1061. } else {
  1062. coda_prepare_encode(ctx);
  1063. }
  1064. if (dev->devtype->product != CODA_DX6)
  1065. coda_write(dev, ctx->iram_info.axi_sram_use,
  1066. CODA7_REG_BIT_AXI_SRAM_USE);
  1067. /* 1 second timeout in case CODA locks up */
  1068. schedule_delayed_work(&dev->timeout, HZ);
  1069. if (ctx->inst_type == CODA_INST_DECODER)
  1070. coda_kfifo_sync_to_device_full(ctx);
  1071. coda_command_async(ctx, CODA_COMMAND_PIC_RUN);
  1072. }
  1073. static int coda_job_ready(void *m2m_priv)
  1074. {
  1075. struct coda_ctx *ctx = m2m_priv;
  1076. /*
  1077. * For both 'P' and 'key' frame cases 1 picture
  1078. * and 1 frame are needed. In the decoder case,
  1079. * the compressed frame can be in the bitstream.
  1080. */
  1081. if (!v4l2_m2m_num_src_bufs_ready(ctx->m2m_ctx) &&
  1082. ctx->inst_type != CODA_INST_DECODER) {
  1083. v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
  1084. "not ready: not enough video buffers.\n");
  1085. return 0;
  1086. }
  1087. if (!v4l2_m2m_num_dst_bufs_ready(ctx->m2m_ctx)) {
  1088. v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
  1089. "not ready: not enough video capture buffers.\n");
  1090. return 0;
  1091. }
  1092. if (ctx->prescan_failed ||
  1093. ((ctx->inst_type == CODA_INST_DECODER) &&
  1094. (coda_get_bitstream_payload(ctx) < 512) &&
  1095. !(ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG))) {
  1096. v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
  1097. "%d: not ready: not enough bitstream data.\n",
  1098. ctx->idx);
  1099. return 0;
  1100. }
  1101. if (ctx->aborting) {
  1102. v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
  1103. "not ready: aborting\n");
  1104. return 0;
  1105. }
  1106. v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
  1107. "job ready\n");
  1108. return 1;
  1109. }
  1110. static void coda_job_abort(void *priv)
  1111. {
  1112. struct coda_ctx *ctx = priv;
  1113. ctx->aborting = 1;
  1114. v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
  1115. "Aborting task\n");
  1116. }
  1117. static void coda_lock(void *m2m_priv)
  1118. {
  1119. struct coda_ctx *ctx = m2m_priv;
  1120. struct coda_dev *pcdev = ctx->dev;
  1121. mutex_lock(&pcdev->dev_mutex);
  1122. }
  1123. static void coda_unlock(void *m2m_priv)
  1124. {
  1125. struct coda_ctx *ctx = m2m_priv;
  1126. struct coda_dev *pcdev = ctx->dev;
  1127. mutex_unlock(&pcdev->dev_mutex);
  1128. }
  1129. static struct v4l2_m2m_ops coda_m2m_ops = {
  1130. .device_run = coda_device_run,
  1131. .job_ready = coda_job_ready,
  1132. .job_abort = coda_job_abort,
  1133. .lock = coda_lock,
  1134. .unlock = coda_unlock,
  1135. };
  1136. static void set_default_params(struct coda_ctx *ctx)
  1137. {
  1138. int max_w;
  1139. int max_h;
  1140. ctx->codec = &ctx->dev->devtype->codecs[0];
  1141. max_w = ctx->codec->max_w;
  1142. max_h = ctx->codec->max_h;
  1143. ctx->params.codec_mode = CODA_MODE_INVALID;
  1144. ctx->colorspace = V4L2_COLORSPACE_REC709;
  1145. ctx->params.framerate = 30;
  1146. ctx->aborting = 0;
  1147. /* Default formats for output and input queues */
  1148. ctx->q_data[V4L2_M2M_SRC].fourcc = ctx->codec->src_fourcc;
  1149. ctx->q_data[V4L2_M2M_DST].fourcc = ctx->codec->dst_fourcc;
  1150. ctx->q_data[V4L2_M2M_SRC].width = max_w;
  1151. ctx->q_data[V4L2_M2M_SRC].height = max_h;
  1152. ctx->q_data[V4L2_M2M_SRC].sizeimage = (max_w * max_h * 3) / 2;
  1153. ctx->q_data[V4L2_M2M_DST].width = max_w;
  1154. ctx->q_data[V4L2_M2M_DST].height = max_h;
  1155. ctx->q_data[V4L2_M2M_DST].sizeimage = CODA_MAX_FRAME_SIZE;
  1156. }
  1157. /*
  1158. * Queue operations
  1159. */
  1160. static int coda_queue_setup(struct vb2_queue *vq,
  1161. const struct v4l2_format *fmt,
  1162. unsigned int *nbuffers, unsigned int *nplanes,
  1163. unsigned int sizes[], void *alloc_ctxs[])
  1164. {
  1165. struct coda_ctx *ctx = vb2_get_drv_priv(vq);
  1166. struct coda_q_data *q_data;
  1167. unsigned int size;
  1168. q_data = get_q_data(ctx, vq->type);
  1169. size = q_data->sizeimage;
  1170. *nplanes = 1;
  1171. sizes[0] = size;
  1172. alloc_ctxs[0] = ctx->dev->alloc_ctx;
  1173. v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
  1174. "get %d buffer(s) of size %d each.\n", *nbuffers, size);
  1175. return 0;
  1176. }
  1177. static int coda_buf_prepare(struct vb2_buffer *vb)
  1178. {
  1179. struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  1180. struct coda_q_data *q_data;
  1181. q_data = get_q_data(ctx, vb->vb2_queue->type);
  1182. if (vb2_plane_size(vb, 0) < q_data->sizeimage) {
  1183. v4l2_warn(&ctx->dev->v4l2_dev,
  1184. "%s data will not fit into plane (%lu < %lu)\n",
  1185. __func__, vb2_plane_size(vb, 0),
  1186. (long)q_data->sizeimage);
  1187. return -EINVAL;
  1188. }
  1189. return 0;
  1190. }
  1191. static void coda_buf_queue(struct vb2_buffer *vb)
  1192. {
  1193. struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  1194. struct coda_q_data *q_data;
  1195. q_data = get_q_data(ctx, vb->vb2_queue->type);
  1196. /*
  1197. * In the decoder case, immediately try to copy the buffer into the
  1198. * bitstream ringbuffer and mark it as ready to be dequeued.
  1199. */
  1200. if (q_data->fourcc == V4L2_PIX_FMT_H264 &&
  1201. vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
  1202. /*
  1203. * For backwards compatiblity, queuing an empty buffer marks
  1204. * the stream end
  1205. */
  1206. if (vb2_get_plane_payload(vb, 0) == 0)
  1207. ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
  1208. mutex_lock(&ctx->bitstream_mutex);
  1209. v4l2_m2m_buf_queue(ctx->m2m_ctx, vb);
  1210. coda_fill_bitstream(ctx);
  1211. mutex_unlock(&ctx->bitstream_mutex);
  1212. } else {
  1213. v4l2_m2m_buf_queue(ctx->m2m_ctx, vb);
  1214. }
  1215. }
  1216. static void coda_wait_prepare(struct vb2_queue *q)
  1217. {
  1218. struct coda_ctx *ctx = vb2_get_drv_priv(q);
  1219. coda_unlock(ctx);
  1220. }
  1221. static void coda_wait_finish(struct vb2_queue *q)
  1222. {
  1223. struct coda_ctx *ctx = vb2_get_drv_priv(q);
  1224. coda_lock(ctx);
  1225. }
  1226. static void coda_parabuf_write(struct coda_ctx *ctx, int index, u32 value)
  1227. {
  1228. struct coda_dev *dev = ctx->dev;
  1229. u32 *p = ctx->parabuf.vaddr;
  1230. if (dev->devtype->product == CODA_DX6)
  1231. p[index] = value;
  1232. else
  1233. p[index ^ 1] = value;
  1234. }
  1235. static int coda_alloc_aux_buf(struct coda_dev *dev,
  1236. struct coda_aux_buf *buf, size_t size)
  1237. {
  1238. buf->vaddr = dma_alloc_coherent(&dev->plat_dev->dev, size, &buf->paddr,
  1239. GFP_KERNEL);
  1240. if (!buf->vaddr)
  1241. return -ENOMEM;
  1242. buf->size = size;
  1243. return 0;
  1244. }
  1245. static inline int coda_alloc_context_buf(struct coda_ctx *ctx,
  1246. struct coda_aux_buf *buf, size_t size)
  1247. {
  1248. return coda_alloc_aux_buf(ctx->dev, buf, size);
  1249. }
  1250. static void coda_free_aux_buf(struct coda_dev *dev,
  1251. struct coda_aux_buf *buf)
  1252. {
  1253. if (buf->vaddr) {
  1254. dma_free_coherent(&dev->plat_dev->dev, buf->size,
  1255. buf->vaddr, buf->paddr);
  1256. buf->vaddr = NULL;
  1257. buf->size = 0;
  1258. }
  1259. }
  1260. static void coda_free_framebuffers(struct coda_ctx *ctx)
  1261. {
  1262. int i;
  1263. for (i = 0; i < CODA_MAX_FRAMEBUFFERS; i++)
  1264. coda_free_aux_buf(ctx->dev, &ctx->internal_frames[i]);
  1265. }
  1266. static int coda_alloc_framebuffers(struct coda_ctx *ctx, struct coda_q_data *q_data, u32 fourcc)
  1267. {
  1268. struct coda_dev *dev = ctx->dev;
  1269. int height = q_data->height;
  1270. dma_addr_t paddr;
  1271. int ysize;
  1272. int ret;
  1273. int i;
  1274. if (ctx->codec && ctx->codec->src_fourcc == V4L2_PIX_FMT_H264)
  1275. height = round_up(height, 16);
  1276. ysize = round_up(q_data->width, 8) * height;
  1277. /* Allocate frame buffers */
  1278. for (i = 0; i < ctx->num_internal_frames; i++) {
  1279. size_t size;
  1280. size = q_data->sizeimage;
  1281. if (ctx->codec->src_fourcc == V4L2_PIX_FMT_H264 &&
  1282. dev->devtype->product != CODA_DX6)
  1283. ctx->internal_frames[i].size += ysize/4;
  1284. ret = coda_alloc_context_buf(ctx, &ctx->internal_frames[i], size);
  1285. if (ret < 0) {
  1286. coda_free_framebuffers(ctx);
  1287. return ret;
  1288. }
  1289. }
  1290. /* Register frame buffers in the parameter buffer */
  1291. for (i = 0; i < ctx->num_internal_frames; i++) {
  1292. paddr = ctx->internal_frames[i].paddr;
  1293. coda_parabuf_write(ctx, i * 3 + 0, paddr); /* Y */
  1294. coda_parabuf_write(ctx, i * 3 + 1, paddr + ysize); /* Cb */
  1295. coda_parabuf_write(ctx, i * 3 + 2, paddr + ysize + ysize/4); /* Cr */
  1296. /* mvcol buffer for h.264 */
  1297. if (ctx->codec->src_fourcc == V4L2_PIX_FMT_H264 &&
  1298. dev->devtype->product != CODA_DX6)
  1299. coda_parabuf_write(ctx, 96 + i,
  1300. ctx->internal_frames[i].paddr +
  1301. ysize + ysize/4 + ysize/4);
  1302. }
  1303. /* mvcol buffer for mpeg4 */
  1304. if ((dev->devtype->product != CODA_DX6) &&
  1305. (ctx->codec->src_fourcc == V4L2_PIX_FMT_MPEG4))
  1306. coda_parabuf_write(ctx, 97, ctx->internal_frames[i].paddr +
  1307. ysize + ysize/4 + ysize/4);
  1308. return 0;
  1309. }
  1310. static int coda_h264_padding(int size, char *p)
  1311. {
  1312. int nal_size;
  1313. int diff;
  1314. diff = size - (size & ~0x7);
  1315. if (diff == 0)
  1316. return 0;
  1317. nal_size = coda_filler_size[diff];
  1318. memcpy(p, coda_filler_nal, nal_size);
  1319. /* Add rbsp stop bit and trailing at the end */
  1320. *(p + nal_size - 1) = 0x80;
  1321. return nal_size;
  1322. }
  1323. static void coda_setup_iram(struct coda_ctx *ctx)
  1324. {
  1325. struct coda_iram_info *iram_info = &ctx->iram_info;
  1326. struct coda_dev *dev = ctx->dev;
  1327. int ipacdc_size;
  1328. int bitram_size;
  1329. int dbk_size;
  1330. int ovl_size;
  1331. int mb_width;
  1332. int me_size;
  1333. int size;
  1334. memset(iram_info, 0, sizeof(*iram_info));
  1335. size = dev->iram_size;
  1336. if (dev->devtype->product == CODA_DX6)
  1337. return;
  1338. if (ctx->inst_type == CODA_INST_ENCODER) {
  1339. struct coda_q_data *q_data_src;
  1340. q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
  1341. mb_width = DIV_ROUND_UP(q_data_src->width, 16);
  1342. /* Prioritize in case IRAM is too small for everything */
  1343. me_size = round_up(round_up(q_data_src->width, 16) * 36 + 2048,
  1344. 1024);
  1345. iram_info->search_ram_size = me_size;
  1346. if (size >= iram_info->search_ram_size) {
  1347. if (dev->devtype->product == CODA_7541)
  1348. iram_info->axi_sram_use |= CODA7_USE_HOST_ME_ENABLE;
  1349. iram_info->search_ram_paddr = dev->iram_paddr;
  1350. size -= iram_info->search_ram_size;
  1351. } else {
  1352. pr_err("IRAM is smaller than the search ram size\n");
  1353. goto out;
  1354. }
  1355. /* Only H.264BP and H.263P3 are considered */
  1356. dbk_size = round_up(128 * mb_width, 1024);
  1357. if (size >= dbk_size) {
  1358. iram_info->axi_sram_use |= CODA7_USE_HOST_DBK_ENABLE;
  1359. iram_info->buf_dbk_y_use = dev->iram_paddr +
  1360. iram_info->search_ram_size;
  1361. iram_info->buf_dbk_c_use = iram_info->buf_dbk_y_use +
  1362. dbk_size / 2;
  1363. size -= dbk_size;
  1364. } else {
  1365. goto out;
  1366. }
  1367. bitram_size = round_up(128 * mb_width, 1024);
  1368. if (size >= bitram_size) {
  1369. iram_info->axi_sram_use |= CODA7_USE_HOST_BIT_ENABLE;
  1370. iram_info->buf_bit_use = iram_info->buf_dbk_c_use +
  1371. dbk_size / 2;
  1372. size -= bitram_size;
  1373. } else {
  1374. goto out;
  1375. }
  1376. ipacdc_size = round_up(128 * mb_width, 1024);
  1377. if (size >= ipacdc_size) {
  1378. iram_info->axi_sram_use |= CODA7_USE_HOST_IP_ENABLE;
  1379. iram_info->buf_ip_ac_dc_use = iram_info->buf_bit_use +
  1380. bitram_size;
  1381. size -= ipacdc_size;
  1382. }
  1383. /* OVL and BTP disabled for encoder */
  1384. } else if (ctx->inst_type == CODA_INST_DECODER) {
  1385. struct coda_q_data *q_data_dst;
  1386. int mb_height;
  1387. q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
  1388. mb_width = DIV_ROUND_UP(q_data_dst->width, 16);
  1389. mb_height = DIV_ROUND_UP(q_data_dst->height, 16);
  1390. dbk_size = round_up(256 * mb_width, 1024);
  1391. if (size >= dbk_size) {
  1392. iram_info->axi_sram_use |= CODA7_USE_HOST_DBK_ENABLE;
  1393. iram_info->buf_dbk_y_use = dev->iram_paddr;
  1394. iram_info->buf_dbk_c_use = dev->iram_paddr +
  1395. dbk_size / 2;
  1396. size -= dbk_size;
  1397. } else {
  1398. goto out;
  1399. }
  1400. bitram_size = round_up(128 * mb_width, 1024);
  1401. if (size >= bitram_size) {
  1402. iram_info->axi_sram_use |= CODA7_USE_HOST_BIT_ENABLE;
  1403. iram_info->buf_bit_use = iram_info->buf_dbk_c_use +
  1404. dbk_size / 2;
  1405. size -= bitram_size;
  1406. } else {
  1407. goto out;
  1408. }
  1409. ipacdc_size = round_up(128 * mb_width, 1024);
  1410. if (size >= ipacdc_size) {
  1411. iram_info->axi_sram_use |= CODA7_USE_HOST_IP_ENABLE;
  1412. iram_info->buf_ip_ac_dc_use = iram_info->buf_bit_use +
  1413. bitram_size;
  1414. size -= ipacdc_size;
  1415. } else {
  1416. goto out;
  1417. }
  1418. ovl_size = round_up(80 * mb_width, 1024);
  1419. }
  1420. out:
  1421. switch (dev->devtype->product) {
  1422. case CODA_DX6:
  1423. break;
  1424. case CODA_7541:
  1425. /* i.MX53 uses secondary AXI for IRAM access */
  1426. if (iram_info->axi_sram_use & CODA7_USE_HOST_BIT_ENABLE)
  1427. iram_info->axi_sram_use |= CODA7_USE_BIT_ENABLE;
  1428. if (iram_info->axi_sram_use & CODA7_USE_HOST_IP_ENABLE)
  1429. iram_info->axi_sram_use |= CODA7_USE_IP_ENABLE;
  1430. if (iram_info->axi_sram_use & CODA7_USE_HOST_DBK_ENABLE)
  1431. iram_info->axi_sram_use |= CODA7_USE_DBK_ENABLE;
  1432. if (iram_info->axi_sram_use & CODA7_USE_HOST_OVL_ENABLE)
  1433. iram_info->axi_sram_use |= CODA7_USE_OVL_ENABLE;
  1434. if (iram_info->axi_sram_use & CODA7_USE_HOST_ME_ENABLE)
  1435. iram_info->axi_sram_use |= CODA7_USE_ME_ENABLE;
  1436. }
  1437. if (!(iram_info->axi_sram_use & CODA7_USE_HOST_IP_ENABLE))
  1438. v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
  1439. "IRAM smaller than needed\n");
  1440. if (dev->devtype->product == CODA_7541) {
  1441. /* TODO - Enabling these causes picture errors on CODA7541 */
  1442. if (ctx->inst_type == CODA_INST_DECODER) {
  1443. /* fw 1.4.50 */
  1444. iram_info->axi_sram_use &= ~(CODA7_USE_HOST_IP_ENABLE |
  1445. CODA7_USE_IP_ENABLE);
  1446. } else {
  1447. /* fw 13.4.29 */
  1448. iram_info->axi_sram_use &= ~(CODA7_USE_HOST_IP_ENABLE |
  1449. CODA7_USE_HOST_DBK_ENABLE |
  1450. CODA7_USE_IP_ENABLE |
  1451. CODA7_USE_DBK_ENABLE);
  1452. }
  1453. }
  1454. }
  1455. static void coda_free_context_buffers(struct coda_ctx *ctx)
  1456. {
  1457. struct coda_dev *dev = ctx->dev;
  1458. coda_free_aux_buf(dev, &ctx->slicebuf);
  1459. coda_free_aux_buf(dev, &ctx->psbuf);
  1460. if (dev->devtype->product != CODA_DX6)
  1461. coda_free_aux_buf(dev, &ctx->workbuf);
  1462. }
  1463. static int coda_alloc_context_buffers(struct coda_ctx *ctx,
  1464. struct coda_q_data *q_data)
  1465. {
  1466. struct coda_dev *dev = ctx->dev;
  1467. size_t size;
  1468. int ret;
  1469. switch (dev->devtype->product) {
  1470. case CODA_7541:
  1471. size = CODA7_WORK_BUF_SIZE;
  1472. break;
  1473. default:
  1474. return 0;
  1475. }
  1476. if (ctx->psbuf.vaddr) {
  1477. v4l2_err(&dev->v4l2_dev, "psmembuf still allocated\n");
  1478. return -EBUSY;
  1479. }
  1480. if (ctx->slicebuf.vaddr) {
  1481. v4l2_err(&dev->v4l2_dev, "slicebuf still allocated\n");
  1482. return -EBUSY;
  1483. }
  1484. if (ctx->workbuf.vaddr) {
  1485. v4l2_err(&dev->v4l2_dev, "context buffer still allocated\n");
  1486. ret = -EBUSY;
  1487. return -ENOMEM;
  1488. }
  1489. if (q_data->fourcc == V4L2_PIX_FMT_H264) {
  1490. /* worst case slice size */
  1491. size = (DIV_ROUND_UP(q_data->width, 16) *
  1492. DIV_ROUND_UP(q_data->height, 16)) * 3200 / 8 + 512;
  1493. ret = coda_alloc_context_buf(ctx, &ctx->slicebuf, size);
  1494. if (ret < 0) {
  1495. v4l2_err(&dev->v4l2_dev, "failed to allocate %d byte slice buffer",
  1496. ctx->slicebuf.size);
  1497. return ret;
  1498. }
  1499. }
  1500. if (dev->devtype->product == CODA_7541) {
  1501. ret = coda_alloc_context_buf(ctx, &ctx->psbuf, CODA7_PS_BUF_SIZE);
  1502. if (ret < 0) {
  1503. v4l2_err(&dev->v4l2_dev, "failed to allocate psmem buffer");
  1504. goto err;
  1505. }
  1506. }
  1507. ret = coda_alloc_context_buf(ctx, &ctx->workbuf, size);
  1508. if (ret < 0) {
  1509. v4l2_err(&dev->v4l2_dev, "failed to allocate %d byte context buffer",
  1510. ctx->workbuf.size);
  1511. goto err;
  1512. }
  1513. return 0;
  1514. err:
  1515. coda_free_context_buffers(ctx);
  1516. return ret;
  1517. }
  1518. static int coda_start_decoding(struct coda_ctx *ctx)
  1519. {
  1520. struct coda_q_data *q_data_src, *q_data_dst;
  1521. u32 bitstream_buf, bitstream_size;
  1522. struct coda_dev *dev = ctx->dev;
  1523. int width, height;
  1524. u32 src_fourcc;
  1525. u32 val;
  1526. int ret;
  1527. /* Start decoding */
  1528. q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
  1529. q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
  1530. bitstream_buf = ctx->bitstream.paddr;
  1531. bitstream_size = ctx->bitstream.size;
  1532. src_fourcc = q_data_src->fourcc;
  1533. coda_write(dev, ctx->parabuf.paddr, CODA_REG_BIT_PARA_BUF_ADDR);
  1534. /* Update coda bitstream read and write pointers from kfifo */
  1535. coda_kfifo_sync_to_device_full(ctx);
  1536. ctx->display_idx = -1;
  1537. ctx->frm_dis_flg = 0;
  1538. coda_write(dev, 0, CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
  1539. coda_write(dev, CODA_BIT_DEC_SEQ_INIT_ESCAPE,
  1540. CODA_REG_BIT_BIT_STREAM_PARAM);
  1541. coda_write(dev, bitstream_buf, CODA_CMD_DEC_SEQ_BB_START);
  1542. coda_write(dev, bitstream_size / 1024, CODA_CMD_DEC_SEQ_BB_SIZE);
  1543. val = 0;
  1544. if (dev->devtype->product == CODA_7541)
  1545. val |= CODA_REORDER_ENABLE;
  1546. coda_write(dev, val, CODA_CMD_DEC_SEQ_OPTION);
  1547. ctx->params.codec_mode = ctx->codec->mode;
  1548. ctx->params.codec_mode_aux = 0;
  1549. if (src_fourcc == V4L2_PIX_FMT_H264) {
  1550. if (dev->devtype->product == CODA_7541) {
  1551. coda_write(dev, ctx->psbuf.paddr,
  1552. CODA_CMD_DEC_SEQ_PS_BB_START);
  1553. coda_write(dev, (CODA7_PS_BUF_SIZE / 1024),
  1554. CODA_CMD_DEC_SEQ_PS_BB_SIZE);
  1555. }
  1556. }
  1557. if (coda_command_sync(ctx, CODA_COMMAND_SEQ_INIT)) {
  1558. v4l2_err(&dev->v4l2_dev, "CODA_COMMAND_SEQ_INIT timeout\n");
  1559. coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
  1560. return -ETIMEDOUT;
  1561. }
  1562. /* Update kfifo out pointer from coda bitstream read pointer */
  1563. coda_kfifo_sync_from_device(ctx);
  1564. coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
  1565. if (coda_read(dev, CODA_RET_DEC_SEQ_SUCCESS) == 0) {
  1566. v4l2_err(&dev->v4l2_dev,
  1567. "CODA_COMMAND_SEQ_INIT failed, error code = %d\n",
  1568. coda_read(dev, CODA_RET_DEC_SEQ_ERR_REASON));
  1569. return -EAGAIN;
  1570. }
  1571. val = coda_read(dev, CODA_RET_DEC_SEQ_SRC_SIZE);
  1572. if (dev->devtype->product == CODA_DX6) {
  1573. width = (val >> CODADX6_PICWIDTH_OFFSET) & CODADX6_PICWIDTH_MASK;
  1574. height = val & CODADX6_PICHEIGHT_MASK;
  1575. } else {
  1576. width = (val >> CODA7_PICWIDTH_OFFSET) & CODA7_PICWIDTH_MASK;
  1577. height = val & CODA7_PICHEIGHT_MASK;
  1578. }
  1579. if (width > q_data_dst->width || height > q_data_dst->height) {
  1580. v4l2_err(&dev->v4l2_dev, "stream is %dx%d, not %dx%d\n",
  1581. width, height, q_data_dst->width, q_data_dst->height);
  1582. return -EINVAL;
  1583. }
  1584. width = round_up(width, 16);
  1585. height = round_up(height, 16);
  1586. v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "%s instance %d now: %dx%d\n",
  1587. __func__, ctx->idx, width, height);
  1588. ctx->num_internal_frames = coda_read(dev, CODA_RET_DEC_SEQ_FRAME_NEED) + 1;
  1589. if (ctx->num_internal_frames > CODA_MAX_FRAMEBUFFERS) {
  1590. v4l2_err(&dev->v4l2_dev,
  1591. "not enough framebuffers to decode (%d < %d)\n",
  1592. CODA_MAX_FRAMEBUFFERS, ctx->num_internal_frames);
  1593. return -EINVAL;
  1594. }
  1595. ret = coda_alloc_framebuffers(ctx, q_data_dst, src_fourcc);
  1596. if (ret < 0)
  1597. return ret;
  1598. /* Tell the decoder how many frame buffers we allocated. */
  1599. coda_write(dev, ctx->num_internal_frames, CODA_CMD_SET_FRAME_BUF_NUM);
  1600. coda_write(dev, width, CODA_CMD_SET_FRAME_BUF_STRIDE);
  1601. if (dev->devtype->product != CODA_DX6) {
  1602. /* Set secondary AXI IRAM */
  1603. coda_setup_iram(ctx);
  1604. coda_write(dev, ctx->iram_info.buf_bit_use,
  1605. CODA7_CMD_SET_FRAME_AXI_BIT_ADDR);
  1606. coda_write(dev, ctx->iram_info.buf_ip_ac_dc_use,
  1607. CODA7_CMD_SET_FRAME_AXI_IPACDC_ADDR);
  1608. coda_write(dev, ctx->iram_info.buf_dbk_y_use,
  1609. CODA7_CMD_SET_FRAME_AXI_DBKY_ADDR);
  1610. coda_write(dev, ctx->iram_info.buf_dbk_c_use,
  1611. CODA7_CMD_SET_FRAME_AXI_DBKC_ADDR);
  1612. coda_write(dev, ctx->iram_info.buf_ovl_use,
  1613. CODA7_CMD_SET_FRAME_AXI_OVL_ADDR);
  1614. }
  1615. if (src_fourcc == V4L2_PIX_FMT_H264) {
  1616. coda_write(dev, ctx->slicebuf.paddr,
  1617. CODA_CMD_SET_FRAME_SLICE_BB_START);
  1618. coda_write(dev, ctx->slicebuf.size / 1024,
  1619. CODA_CMD_SET_FRAME_SLICE_BB_SIZE);
  1620. }
  1621. if (dev->devtype->product == CODA_7541) {
  1622. int max_mb_x = 1920 / 16;
  1623. int max_mb_y = 1088 / 16;
  1624. int max_mb_num = max_mb_x * max_mb_y;
  1625. coda_write(dev, max_mb_num << 16 | max_mb_x << 8 | max_mb_y,
  1626. CODA7_CMD_SET_FRAME_MAX_DEC_SIZE);
  1627. }
  1628. if (coda_command_sync(ctx, CODA_COMMAND_SET_FRAME_BUF)) {
  1629. v4l2_err(&ctx->dev->v4l2_dev,
  1630. "CODA_COMMAND_SET_FRAME_BUF timeout\n");
  1631. return -ETIMEDOUT;
  1632. }
  1633. return 0;
  1634. }
  1635. static int coda_encode_header(struct coda_ctx *ctx, struct vb2_buffer *buf,
  1636. int header_code, u8 *header, int *size)
  1637. {
  1638. struct coda_dev *dev = ctx->dev;
  1639. int ret;
  1640. coda_write(dev, vb2_dma_contig_plane_dma_addr(buf, 0),
  1641. CODA_CMD_ENC_HEADER_BB_START);
  1642. coda_write(dev, vb2_plane_size(buf, 0), CODA_CMD_ENC_HEADER_BB_SIZE);
  1643. coda_write(dev, header_code, CODA_CMD_ENC_HEADER_CODE);
  1644. ret = coda_command_sync(ctx, CODA_COMMAND_ENCODE_HEADER);
  1645. if (ret < 0) {
  1646. v4l2_err(&dev->v4l2_dev, "CODA_COMMAND_ENCODE_HEADER timeout\n");
  1647. return ret;
  1648. }
  1649. *size = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->reg_idx)) -
  1650. coda_read(dev, CODA_CMD_ENC_HEADER_BB_START);
  1651. memcpy(header, vb2_plane_vaddr(buf, 0), *size);
  1652. return 0;
  1653. }
  1654. static int coda_start_streaming(struct vb2_queue *q, unsigned int count)
  1655. {
  1656. struct coda_ctx *ctx = vb2_get_drv_priv(q);
  1657. struct v4l2_device *v4l2_dev = &ctx->dev->v4l2_dev;
  1658. u32 bitstream_buf, bitstream_size;
  1659. struct coda_dev *dev = ctx->dev;
  1660. struct coda_q_data *q_data_src, *q_data_dst;
  1661. struct vb2_buffer *buf;
  1662. u32 dst_fourcc;
  1663. u32 value;
  1664. int ret = 0;
  1665. q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
  1666. if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
  1667. if (q_data_src->fourcc == V4L2_PIX_FMT_H264) {
  1668. if (coda_get_bitstream_payload(ctx) < 512)
  1669. return -EINVAL;
  1670. } else {
  1671. if (count < 1)
  1672. return -EINVAL;
  1673. }
  1674. ctx->streamon_out = 1;
  1675. if (coda_format_is_yuv(q_data_src->fourcc))
  1676. ctx->inst_type = CODA_INST_ENCODER;
  1677. else
  1678. ctx->inst_type = CODA_INST_DECODER;
  1679. } else {
  1680. if (count < 1)
  1681. return -EINVAL;
  1682. ctx->streamon_cap = 1;
  1683. }
  1684. /* Don't start the coda unless both queues are on */
  1685. if (!(ctx->streamon_out & ctx->streamon_cap))
  1686. return 0;
  1687. /* Allow decoder device_run with no new buffers queued */
  1688. if (ctx->inst_type == CODA_INST_DECODER)
  1689. v4l2_m2m_set_src_buffered(ctx->m2m_ctx, true);
  1690. ctx->gopcounter = ctx->params.gop_size - 1;
  1691. buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
  1692. bitstream_buf = vb2_dma_contig_plane_dma_addr(buf, 0);
  1693. q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
  1694. bitstream_size = q_data_dst->sizeimage;
  1695. dst_fourcc = q_data_dst->fourcc;
  1696. ctx->codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
  1697. q_data_dst->fourcc);
  1698. if (!ctx->codec) {
  1699. v4l2_err(v4l2_dev, "couldn't tell instance type.\n");
  1700. return -EINVAL;
  1701. }
  1702. /* Allocate per-instance buffers */
  1703. ret = coda_alloc_context_buffers(ctx, q_data_src);
  1704. if (ret < 0)
  1705. return ret;
  1706. if (ctx->inst_type == CODA_INST_DECODER) {
  1707. mutex_lock(&dev->coda_mutex);
  1708. ret = coda_start_decoding(ctx);
  1709. mutex_unlock(&dev->coda_mutex);
  1710. if (ret == -EAGAIN) {
  1711. return 0;
  1712. } else if (ret < 0) {
  1713. return ret;
  1714. } else {
  1715. ctx->initialized = 1;
  1716. return 0;
  1717. }
  1718. }
  1719. if (!coda_is_initialized(dev)) {
  1720. v4l2_err(v4l2_dev, "coda is not initialized.\n");
  1721. return -EFAULT;
  1722. }
  1723. mutex_lock(&dev->coda_mutex);
  1724. coda_write(dev, ctx->parabuf.paddr, CODA_REG_BIT_PARA_BUF_ADDR);
  1725. coda_write(dev, bitstream_buf, CODA_REG_BIT_RD_PTR(ctx->reg_idx));
  1726. coda_write(dev, bitstream_buf, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
  1727. switch (dev->devtype->product) {
  1728. case CODA_DX6:
  1729. coda_write(dev, CODADX6_STREAM_BUF_DYNALLOC_EN |
  1730. CODADX6_STREAM_BUF_PIC_RESET, CODA_REG_BIT_STREAM_CTRL);
  1731. break;
  1732. default:
  1733. coda_write(dev, CODA7_STREAM_BUF_DYNALLOC_EN |
  1734. CODA7_STREAM_BUF_PIC_RESET, CODA_REG_BIT_STREAM_CTRL);
  1735. }
  1736. if (dev->devtype->product == CODA_DX6) {
  1737. /* Configure the coda */
  1738. coda_write(dev, dev->iram_paddr, CODADX6_REG_BIT_SEARCH_RAM_BASE_ADDR);
  1739. }
  1740. /* Could set rotation here if needed */
  1741. switch (dev->devtype->product) {
  1742. case CODA_DX6:
  1743. value = (q_data_src->width & CODADX6_PICWIDTH_MASK) << CODADX6_PICWIDTH_OFFSET;
  1744. value |= (q_data_src->height & CODADX6_PICHEIGHT_MASK) << CODA_PICHEIGHT_OFFSET;
  1745. break;
  1746. default:
  1747. value = (q_data_src->width & CODA7_PICWIDTH_MASK) << CODA7_PICWIDTH_OFFSET;
  1748. value |= (q_data_src->height & CODA7_PICHEIGHT_MASK) << CODA_PICHEIGHT_OFFSET;
  1749. }
  1750. coda_write(dev, value, CODA_CMD_ENC_SEQ_SRC_SIZE);
  1751. coda_write(dev, ctx->params.framerate,
  1752. CODA_CMD_ENC_SEQ_SRC_F_RATE);
  1753. ctx->params.codec_mode = ctx->codec->mode;
  1754. switch (dst_fourcc) {
  1755. case V4L2_PIX_FMT_MPEG4:
  1756. coda_write(dev, CODA_STD_MPEG4, CODA_CMD_ENC_SEQ_COD_STD);
  1757. coda_write(dev, 0, CODA_CMD_ENC_SEQ_MP4_PARA);
  1758. break;
  1759. case V4L2_PIX_FMT_H264:
  1760. coda_write(dev, CODA_STD_H264, CODA_CMD_ENC_SEQ_COD_STD);
  1761. coda_write(dev, 0, CODA_CMD_ENC_SEQ_264_PARA);
  1762. break;
  1763. default:
  1764. v4l2_err(v4l2_dev,
  1765. "dst format (0x%08x) invalid.\n", dst_fourcc);
  1766. ret = -EINVAL;
  1767. goto out;
  1768. }
  1769. switch (ctx->params.slice_mode) {
  1770. case V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE:
  1771. value = 0;
  1772. break;
  1773. case V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_MB:
  1774. value = (ctx->params.slice_max_mb & CODA_SLICING_SIZE_MASK) << CODA_SLICING_SIZE_OFFSET;
  1775. value |= (1 & CODA_SLICING_UNIT_MASK) << CODA_SLICING_UNIT_OFFSET;
  1776. value |= 1 & CODA_SLICING_MODE_MASK;
  1777. break;
  1778. case V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES:
  1779. value = (ctx->params.slice_max_bits & CODA_SLICING_SIZE_MASK) << CODA_SLICING_SIZE_OFFSET;
  1780. value |= (0 & CODA_SLICING_UNIT_MASK) << CODA_SLICING_UNIT_OFFSET;
  1781. value |= 1 & CODA_SLICING_MODE_MASK;
  1782. break;
  1783. }
  1784. coda_write(dev, value, CODA_CMD_ENC_SEQ_SLICE_MODE);
  1785. value = ctx->params.gop_size & CODA_GOP_SIZE_MASK;
  1786. coda_write(dev, value, CODA_CMD_ENC_SEQ_GOP_SIZE);
  1787. if (ctx->params.bitrate) {
  1788. /* Rate control enabled */
  1789. value = (ctx->params.bitrate & CODA_RATECONTROL_BITRATE_MASK) << CODA_RATECONTROL_BITRATE_OFFSET;
  1790. value |= 1 & CODA_RATECONTROL_ENABLE_MASK;
  1791. } else {
  1792. value = 0;
  1793. }
  1794. coda_write(dev, value, CODA_CMD_ENC_SEQ_RC_PARA);
  1795. coda_write(dev, 0, CODA_CMD_ENC_SEQ_RC_BUF_SIZE);
  1796. coda_write(dev, 0, CODA_CMD_ENC_SEQ_INTRA_REFRESH);
  1797. coda_write(dev, bitstream_buf, CODA_CMD_ENC_SEQ_BB_START);
  1798. coda_write(dev, bitstream_size / 1024, CODA_CMD_ENC_SEQ_BB_SIZE);
  1799. /* set default gamma */
  1800. value = (CODA_DEFAULT_GAMMA & CODA_GAMMA_MASK) << CODA_GAMMA_OFFSET;
  1801. coda_write(dev, value, CODA_CMD_ENC_SEQ_RC_GAMMA);
  1802. if (CODA_DEFAULT_GAMMA > 0) {
  1803. if (dev->devtype->product == CODA_DX6)
  1804. value = 1 << CODADX6_OPTION_GAMMA_OFFSET;
  1805. else
  1806. value = 1 << CODA7_OPTION_GAMMA_OFFSET;
  1807. } else {
  1808. value = 0;
  1809. }
  1810. coda_write(dev, value, CODA_CMD_ENC_SEQ_OPTION);
  1811. coda_setup_iram(ctx);
  1812. if (dst_fourcc == V4L2_PIX_FMT_H264) {
  1813. if (dev->devtype->product == CODA_DX6) {
  1814. value = FMO_SLICE_SAVE_BUF_SIZE << 7;
  1815. coda_write(dev, value, CODADX6_CMD_ENC_SEQ_FMO);
  1816. } else {
  1817. coda_write(dev, ctx->iram_info.search_ram_paddr,
  1818. CODA7_CMD_ENC_SEQ_SEARCH_BASE);
  1819. coda_write(dev, ctx->iram_info.search_ram_size,
  1820. CODA7_CMD_ENC_SEQ_SEARCH_SIZE);
  1821. }
  1822. }
  1823. ret = coda_command_sync(ctx, CODA_COMMAND_SEQ_INIT);
  1824. if (ret < 0) {
  1825. v4l2_err(v4l2_dev, "CODA_COMMAND_SEQ_INIT timeout\n");
  1826. goto out;
  1827. }
  1828. if (coda_read(dev, CODA_RET_ENC_SEQ_SUCCESS) == 0) {
  1829. v4l2_err(v4l2_dev, "CODA_COMMAND_SEQ_INIT failed\n");
  1830. ret = -EFAULT;
  1831. goto out;
  1832. }
  1833. ctx->num_internal_frames = 2;
  1834. ret = coda_alloc_framebuffers(ctx, q_data_src, dst_fourcc);
  1835. if (ret < 0) {
  1836. v4l2_err(v4l2_dev, "failed to allocate framebuffers\n");
  1837. goto out;
  1838. }
  1839. coda_write(dev, ctx->num_internal_frames, CODA_CMD_SET_FRAME_BUF_NUM);
  1840. coda_write(dev, round_up(q_data_src->width, 8), CODA_CMD_SET_FRAME_BUF_STRIDE);
  1841. if (dev->devtype->product == CODA_7541)
  1842. coda_write(dev, round_up(q_data_src->width, 8),
  1843. CODA7_CMD_SET_FRAME_SOURCE_BUF_STRIDE);
  1844. if (dev->devtype->product != CODA_DX6) {
  1845. coda_write(dev, ctx->iram_info.buf_bit_use,
  1846. CODA7_CMD_SET_FRAME_AXI_BIT_ADDR);
  1847. coda_write(dev, ctx->iram_info.buf_ip_ac_dc_use,
  1848. CODA7_CMD_SET_FRAME_AXI_IPACDC_ADDR);
  1849. coda_write(dev, ctx->iram_info.buf_dbk_y_use,
  1850. CODA7_CMD_SET_FRAME_AXI_DBKY_ADDR);
  1851. coda_write(dev, ctx->iram_info.buf_dbk_c_use,
  1852. CODA7_CMD_SET_FRAME_AXI_DBKC_ADDR);
  1853. coda_write(dev, ctx->iram_info.buf_ovl_use,
  1854. CODA7_CMD_SET_FRAME_AXI_OVL_ADDR);
  1855. }
  1856. ret = coda_command_sync(ctx, CODA_COMMAND_SET_FRAME_BUF);
  1857. if (ret < 0) {
  1858. v4l2_err(v4l2_dev, "CODA_COMMAND_SET_FRAME_BUF timeout\n");
  1859. goto out;
  1860. }
  1861. /* Save stream headers */
  1862. buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
  1863. switch (dst_fourcc) {
  1864. case V4L2_PIX_FMT_H264:
  1865. /*
  1866. * Get SPS in the first frame and copy it to an
  1867. * intermediate buffer.
  1868. */
  1869. ret = coda_encode_header(ctx, buf, CODA_HEADER_H264_SPS,
  1870. &ctx->vpu_header[0][0],
  1871. &ctx->vpu_header_size[0]);
  1872. if (ret < 0)
  1873. goto out;
  1874. /*
  1875. * Get PPS in the first frame and copy it to an
  1876. * intermediate buffer.
  1877. */
  1878. ret = coda_encode_header(ctx, buf, CODA_HEADER_H264_PPS,
  1879. &ctx->vpu_header[1][0],
  1880. &ctx->vpu_header_size[1]);
  1881. if (ret < 0)
  1882. goto out;
  1883. /*
  1884. * Length of H.264 headers is variable and thus it might not be
  1885. * aligned for the coda to append the encoded frame. In that is
  1886. * the case a filler NAL must be added to header 2.
  1887. */
  1888. ctx->vpu_header_size[2] = coda_h264_padding(
  1889. (ctx->vpu_header_size[0] +
  1890. ctx->vpu_header_size[1]),
  1891. ctx->vpu_header[2]);
  1892. break;
  1893. case V4L2_PIX_FMT_MPEG4:
  1894. /*
  1895. * Get VOS in the first frame and copy it to an
  1896. * intermediate buffer
  1897. */
  1898. ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VOS,
  1899. &ctx->vpu_header[0][0],
  1900. &ctx->vpu_header_size[0]);
  1901. if (ret < 0)
  1902. goto out;
  1903. ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VIS,
  1904. &ctx->vpu_header[1][0],
  1905. &ctx->vpu_header_size[1]);
  1906. if (ret < 0)
  1907. goto out;
  1908. ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VOL,
  1909. &ctx->vpu_header[2][0],
  1910. &ctx->vpu_header_size[2]);
  1911. if (ret < 0)
  1912. goto out;
  1913. break;
  1914. default:
  1915. /* No more formats need to save headers at the moment */
  1916. break;
  1917. }
  1918. out:
  1919. mutex_unlock(&dev->coda_mutex);
  1920. return ret;
  1921. }
  1922. static int coda_stop_streaming(struct vb2_queue *q)
  1923. {
  1924. struct coda_ctx *ctx = vb2_get_drv_priv(q);
  1925. struct coda_dev *dev = ctx->dev;
  1926. if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
  1927. v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
  1928. "%s: output\n", __func__);
  1929. ctx->streamon_out = 0;
  1930. ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
  1931. ctx->isequence = 0;
  1932. } else {
  1933. v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
  1934. "%s: capture\n", __func__);
  1935. ctx->streamon_cap = 0;
  1936. ctx->osequence = 0;
  1937. }
  1938. if (!ctx->streamon_out && !ctx->streamon_cap) {
  1939. kfifo_init(&ctx->bitstream_fifo,
  1940. ctx->bitstream.vaddr, ctx->bitstream.size);
  1941. ctx->runcounter = 0;
  1942. }
  1943. return 0;
  1944. }
  1945. static struct vb2_ops coda_qops = {
  1946. .queue_setup = coda_queue_setup,
  1947. .buf_prepare = coda_buf_prepare,
  1948. .buf_queue = coda_buf_queue,
  1949. .wait_prepare = coda_wait_prepare,
  1950. .wait_finish = coda_wait_finish,
  1951. .start_streaming = coda_start_streaming,
  1952. .stop_streaming = coda_stop_streaming,
  1953. };
  1954. static int coda_s_ctrl(struct v4l2_ctrl *ctrl)
  1955. {
  1956. struct coda_ctx *ctx =
  1957. container_of(ctrl->handler, struct coda_ctx, ctrls);
  1958. v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
  1959. "s_ctrl: id = %d, val = %d\n", ctrl->id, ctrl->val);
  1960. switch (ctrl->id) {
  1961. case V4L2_CID_HFLIP:
  1962. if (ctrl->val)
  1963. ctx->params.rot_mode |= CODA_MIR_HOR;
  1964. else
  1965. ctx->params.rot_mode &= ~CODA_MIR_HOR;
  1966. break;
  1967. case V4L2_CID_VFLIP:
  1968. if (ctrl->val)
  1969. ctx->params.rot_mode |= CODA_MIR_VER;
  1970. else
  1971. ctx->params.rot_mode &= ~CODA_MIR_VER;
  1972. break;
  1973. case V4L2_CID_MPEG_VIDEO_BITRATE:
  1974. ctx->params.bitrate = ctrl->val / 1000;
  1975. break;
  1976. case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
  1977. ctx->params.gop_size = ctrl->val;
  1978. break;
  1979. case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP:
  1980. ctx->params.h264_intra_qp = ctrl->val;
  1981. break;
  1982. case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP:
  1983. ctx->params.h264_inter_qp = ctrl->val;
  1984. break;
  1985. case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP:
  1986. ctx->params.mpeg4_intra_qp = ctrl->val;
  1987. break;
  1988. case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP:
  1989. ctx->params.mpeg4_inter_qp = ctrl->val;
  1990. break;
  1991. case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
  1992. ctx->params.slice_mode = ctrl->val;
  1993. break;
  1994. case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB:
  1995. ctx->params.slice_max_mb = ctrl->val;
  1996. break;
  1997. case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES:
  1998. ctx->params.slice_max_bits = ctrl->val * 8;
  1999. break;
  2000. case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
  2001. break;
  2002. default:
  2003. v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
  2004. "Invalid control, id=%d, val=%d\n",
  2005. ctrl->id, ctrl->val);
  2006. return -EINVAL;
  2007. }
  2008. return 0;
  2009. }
  2010. static struct v4l2_ctrl_ops coda_ctrl_ops = {
  2011. .s_ctrl = coda_s_ctrl,
  2012. };
  2013. static int coda_ctrls_setup(struct coda_ctx *ctx)
  2014. {
  2015. v4l2_ctrl_handler_init(&ctx->ctrls, 9);
  2016. v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
  2017. V4L2_CID_HFLIP, 0, 1, 1, 0);
  2018. v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
  2019. V4L2_CID_VFLIP, 0, 1, 1, 0);
  2020. v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
  2021. V4L2_CID_MPEG_VIDEO_BITRATE, 0, 32767000, 1, 0);
  2022. v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
  2023. V4L2_CID_MPEG_VIDEO_GOP_SIZE, 1, 60, 1, 16);
  2024. v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
  2025. V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP, 1, 51, 1, 25);
  2026. v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
  2027. V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP, 1, 51, 1, 25);
  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_MULTI_SLICE_MODE,
  2034. V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES, 0x0,
  2035. V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE);
  2036. v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
  2037. V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB, 1, 0x3fffffff, 1, 1);
  2038. v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
  2039. V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES, 1, 0x3fffffff, 1, 500);
  2040. v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
  2041. V4L2_CID_MPEG_VIDEO_HEADER_MODE,
  2042. V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME,
  2043. (1 << V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE),
  2044. V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME);
  2045. if (ctx->ctrls.error) {
  2046. v4l2_err(&ctx->dev->v4l2_dev, "control initialization error (%d)",
  2047. ctx->ctrls.error);
  2048. return -EINVAL;
  2049. }
  2050. return v4l2_ctrl_handler_setup(&ctx->ctrls);
  2051. }
  2052. static int coda_queue_init(void *priv, struct vb2_queue *src_vq,
  2053. struct vb2_queue *dst_vq)
  2054. {
  2055. struct coda_ctx *ctx = priv;
  2056. int ret;
  2057. src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  2058. src_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
  2059. src_vq->drv_priv = ctx;
  2060. src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
  2061. src_vq->ops = &coda_qops;
  2062. src_vq->mem_ops = &vb2_dma_contig_memops;
  2063. src_vq->timestamp_type = V4L2_BUF_FLAG_TIMESTAMP_COPY;
  2064. ret = vb2_queue_init(src_vq);
  2065. if (ret)
  2066. return ret;
  2067. dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  2068. dst_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
  2069. dst_vq->drv_priv = ctx;
  2070. dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
  2071. dst_vq->ops = &coda_qops;
  2072. dst_vq->mem_ops = &vb2_dma_contig_memops;
  2073. dst_vq->timestamp_type = V4L2_BUF_FLAG_TIMESTAMP_COPY;
  2074. return vb2_queue_init(dst_vq);
  2075. }
  2076. static int coda_next_free_instance(struct coda_dev *dev)
  2077. {
  2078. int idx = ffz(dev->instance_mask);
  2079. if ((idx < 0) ||
  2080. (dev->devtype->product == CODA_DX6 && idx > CODADX6_MAX_INSTANCES))
  2081. return -EBUSY;
  2082. return idx;
  2083. }
  2084. static int coda_open(struct file *file)
  2085. {
  2086. struct coda_dev *dev = video_drvdata(file);
  2087. struct coda_ctx *ctx = NULL;
  2088. int ret;
  2089. int idx;
  2090. ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
  2091. if (!ctx)
  2092. return -ENOMEM;
  2093. idx = coda_next_free_instance(dev);
  2094. if (idx < 0) {
  2095. ret = idx;
  2096. goto err_coda_max;
  2097. }
  2098. set_bit(idx, &dev->instance_mask);
  2099. INIT_WORK(&ctx->skip_run, coda_skip_run);
  2100. v4l2_fh_init(&ctx->fh, video_devdata(file));
  2101. file->private_data = &ctx->fh;
  2102. v4l2_fh_add(&ctx->fh);
  2103. ctx->dev = dev;
  2104. ctx->idx = idx;
  2105. switch (dev->devtype->product) {
  2106. case CODA_7541:
  2107. ctx->reg_idx = 0;
  2108. break;
  2109. default:
  2110. ctx->reg_idx = idx;
  2111. }
  2112. ret = clk_prepare_enable(dev->clk_per);
  2113. if (ret)
  2114. goto err_clk_per;
  2115. ret = clk_prepare_enable(dev->clk_ahb);
  2116. if (ret)
  2117. goto err_clk_ahb;
  2118. set_default_params(ctx);
  2119. ctx->m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx,
  2120. &coda_queue_init);
  2121. if (IS_ERR(ctx->m2m_ctx)) {
  2122. ret = PTR_ERR(ctx->m2m_ctx);
  2123. v4l2_err(&dev->v4l2_dev, "%s return error (%d)\n",
  2124. __func__, ret);
  2125. goto err_ctx_init;
  2126. }
  2127. ret = coda_ctrls_setup(ctx);
  2128. if (ret) {
  2129. v4l2_err(&dev->v4l2_dev, "failed to setup coda controls\n");
  2130. goto err_ctrls_setup;
  2131. }
  2132. ctx->fh.ctrl_handler = &ctx->ctrls;
  2133. ret = coda_alloc_context_buf(ctx, &ctx->parabuf, CODA_PARA_BUF_SIZE);
  2134. if (ret < 0) {
  2135. v4l2_err(&dev->v4l2_dev, "failed to allocate parabuf");
  2136. goto err_dma_alloc;
  2137. }
  2138. ctx->bitstream.size = CODA_MAX_FRAME_SIZE;
  2139. ctx->bitstream.vaddr = dma_alloc_writecombine(&dev->plat_dev->dev,
  2140. ctx->bitstream.size, &ctx->bitstream.paddr, GFP_KERNEL);
  2141. if (!ctx->bitstream.vaddr) {
  2142. v4l2_err(&dev->v4l2_dev, "failed to allocate bitstream ringbuffer");
  2143. ret = -ENOMEM;
  2144. goto err_dma_writecombine;
  2145. }
  2146. kfifo_init(&ctx->bitstream_fifo,
  2147. ctx->bitstream.vaddr, ctx->bitstream.size);
  2148. mutex_init(&ctx->bitstream_mutex);
  2149. mutex_init(&ctx->buffer_mutex);
  2150. coda_lock(ctx);
  2151. list_add(&ctx->list, &dev->instances);
  2152. coda_unlock(ctx);
  2153. v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Created instance %d (%p)\n",
  2154. ctx->idx, ctx);
  2155. return 0;
  2156. err_dma_writecombine:
  2157. coda_free_context_buffers(ctx);
  2158. if (ctx->dev->devtype->product == CODA_DX6)
  2159. coda_free_aux_buf(dev, &ctx->workbuf);
  2160. coda_free_aux_buf(dev, &ctx->parabuf);
  2161. err_dma_alloc:
  2162. v4l2_ctrl_handler_free(&ctx->ctrls);
  2163. err_ctrls_setup:
  2164. v4l2_m2m_ctx_release(ctx->m2m_ctx);
  2165. err_ctx_init:
  2166. clk_disable_unprepare(dev->clk_ahb);
  2167. err_clk_ahb:
  2168. clk_disable_unprepare(dev->clk_per);
  2169. err_clk_per:
  2170. v4l2_fh_del(&ctx->fh);
  2171. v4l2_fh_exit(&ctx->fh);
  2172. clear_bit(ctx->idx, &dev->instance_mask);
  2173. err_coda_max:
  2174. kfree(ctx);
  2175. return ret;
  2176. }
  2177. static int coda_release(struct file *file)
  2178. {
  2179. struct coda_dev *dev = video_drvdata(file);
  2180. struct coda_ctx *ctx = fh_to_ctx(file->private_data);
  2181. v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Releasing instance %p\n",
  2182. ctx);
  2183. /* If this instance is running, call .job_abort and wait for it to end */
  2184. v4l2_m2m_ctx_release(ctx->m2m_ctx);
  2185. /* In case the instance was not running, we still need to call SEQ_END */
  2186. mutex_lock(&dev->coda_mutex);
  2187. v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
  2188. "%s: sent command 'SEQ_END' to coda\n", __func__);
  2189. if (coda_command_sync(ctx, CODA_COMMAND_SEQ_END)) {
  2190. v4l2_err(&dev->v4l2_dev,
  2191. "CODA_COMMAND_SEQ_END failed\n");
  2192. mutex_unlock(&dev->coda_mutex);
  2193. return -ETIMEDOUT;
  2194. }
  2195. mutex_unlock(&dev->coda_mutex);
  2196. coda_free_framebuffers(ctx);
  2197. coda_lock(ctx);
  2198. list_del(&ctx->list);
  2199. coda_unlock(ctx);
  2200. dma_free_writecombine(&dev->plat_dev->dev, ctx->bitstream.size,
  2201. ctx->bitstream.vaddr, ctx->bitstream.paddr);
  2202. coda_free_context_buffers(ctx);
  2203. if (ctx->dev->devtype->product == CODA_DX6)
  2204. coda_free_aux_buf(dev, &ctx->workbuf);
  2205. coda_free_aux_buf(dev, &ctx->parabuf);
  2206. v4l2_ctrl_handler_free(&ctx->ctrls);
  2207. clk_disable_unprepare(dev->clk_ahb);
  2208. clk_disable_unprepare(dev->clk_per);
  2209. v4l2_fh_del(&ctx->fh);
  2210. v4l2_fh_exit(&ctx->fh);
  2211. clear_bit(ctx->idx, &dev->instance_mask);
  2212. kfree(ctx);
  2213. return 0;
  2214. }
  2215. static unsigned int coda_poll(struct file *file,
  2216. struct poll_table_struct *wait)
  2217. {
  2218. struct coda_ctx *ctx = fh_to_ctx(file->private_data);
  2219. int ret;
  2220. coda_lock(ctx);
  2221. ret = v4l2_m2m_poll(file, ctx->m2m_ctx, wait);
  2222. coda_unlock(ctx);
  2223. return ret;
  2224. }
  2225. static int coda_mmap(struct file *file, struct vm_area_struct *vma)
  2226. {
  2227. struct coda_ctx *ctx = fh_to_ctx(file->private_data);
  2228. return v4l2_m2m_mmap(file, ctx->m2m_ctx, vma);
  2229. }
  2230. static const struct v4l2_file_operations coda_fops = {
  2231. .owner = THIS_MODULE,
  2232. .open = coda_open,
  2233. .release = coda_release,
  2234. .poll = coda_poll,
  2235. .unlocked_ioctl = video_ioctl2,
  2236. .mmap = coda_mmap,
  2237. };
  2238. static void coda_finish_decode(struct coda_ctx *ctx)
  2239. {
  2240. struct coda_dev *dev = ctx->dev;
  2241. struct coda_q_data *q_data_src;
  2242. struct coda_q_data *q_data_dst;
  2243. struct vb2_buffer *dst_buf;
  2244. int width, height;
  2245. int decoded_idx;
  2246. int display_idx;
  2247. u32 src_fourcc;
  2248. int success;
  2249. u32 val;
  2250. dst_buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
  2251. /* Update kfifo out pointer from coda bitstream read pointer */
  2252. coda_kfifo_sync_from_device(ctx);
  2253. /*
  2254. * in stream-end mode, the read pointer can overshoot the write pointer
  2255. * by up to 512 bytes
  2256. */
  2257. if (ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG) {
  2258. if (coda_get_bitstream_payload(ctx) >= 0x100000 - 512)
  2259. kfifo_init(&ctx->bitstream_fifo,
  2260. ctx->bitstream.vaddr, ctx->bitstream.size);
  2261. }
  2262. q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
  2263. src_fourcc = q_data_src->fourcc;
  2264. val = coda_read(dev, CODA_RET_DEC_PIC_SUCCESS);
  2265. if (val != 1)
  2266. pr_err("DEC_PIC_SUCCESS = %d\n", val);
  2267. success = val & 0x1;
  2268. if (!success)
  2269. v4l2_err(&dev->v4l2_dev, "decode failed\n");
  2270. if (src_fourcc == V4L2_PIX_FMT_H264) {
  2271. if (val & (1 << 3))
  2272. v4l2_err(&dev->v4l2_dev,
  2273. "insufficient PS buffer space (%d bytes)\n",
  2274. ctx->psbuf.size);
  2275. if (val & (1 << 2))
  2276. v4l2_err(&dev->v4l2_dev,
  2277. "insufficient slice buffer space (%d bytes)\n",
  2278. ctx->slicebuf.size);
  2279. }
  2280. val = coda_read(dev, CODA_RET_DEC_PIC_SIZE);
  2281. width = (val >> 16) & 0xffff;
  2282. height = val & 0xffff;
  2283. q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
  2284. val = coda_read(dev, CODA_RET_DEC_PIC_TYPE);
  2285. if ((val & 0x7) == 0) {
  2286. dst_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_KEYFRAME;
  2287. dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_PFRAME;
  2288. } else {
  2289. dst_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_PFRAME;
  2290. dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_KEYFRAME;
  2291. }
  2292. val = coda_read(dev, CODA_RET_DEC_PIC_ERR_MB);
  2293. if (val > 0)
  2294. v4l2_err(&dev->v4l2_dev,
  2295. "errors in %d macroblocks\n", val);
  2296. if (dev->devtype->product == CODA_7541) {
  2297. val = coda_read(dev, CODA_RET_DEC_PIC_OPTION);
  2298. if (val == 0) {
  2299. /* not enough bitstream data */
  2300. v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
  2301. "prescan failed: %d\n", val);
  2302. ctx->prescan_failed = true;
  2303. return;
  2304. }
  2305. }
  2306. ctx->frm_dis_flg = coda_read(dev, CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
  2307. /*
  2308. * The previous display frame was copied out by the rotator,
  2309. * now it can be overwritten again
  2310. */
  2311. if (ctx->display_idx >= 0 &&
  2312. ctx->display_idx < ctx->num_internal_frames) {
  2313. ctx->frm_dis_flg &= ~(1 << ctx->display_idx);
  2314. coda_write(dev, ctx->frm_dis_flg,
  2315. CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
  2316. }
  2317. /*
  2318. * The index of the last decoded frame, not necessarily in
  2319. * display order, and the index of the next display frame.
  2320. * The latter could have been decoded in a previous run.
  2321. */
  2322. decoded_idx = coda_read(dev, CODA_RET_DEC_PIC_CUR_IDX);
  2323. display_idx = coda_read(dev, CODA_RET_DEC_PIC_FRAME_IDX);
  2324. if (decoded_idx == -1) {
  2325. /* no frame was decoded, but we might have a display frame */
  2326. if (display_idx < 0 && ctx->display_idx < 0)
  2327. ctx->prescan_failed = true;
  2328. } else if (decoded_idx == -2) {
  2329. /* no frame was decoded, we still return the remaining buffers */
  2330. } else if (decoded_idx < 0 || decoded_idx >= ctx->num_internal_frames) {
  2331. v4l2_err(&dev->v4l2_dev,
  2332. "decoded frame index out of range: %d\n", decoded_idx);
  2333. }
  2334. if (display_idx == -1) {
  2335. /*
  2336. * no more frames to be decoded, but there could still
  2337. * be rotator output to dequeue
  2338. */
  2339. ctx->prescan_failed = true;
  2340. } else if (display_idx == -3) {
  2341. /* possibly prescan failure */
  2342. } else if (display_idx < 0 || display_idx >= ctx->num_internal_frames) {
  2343. v4l2_err(&dev->v4l2_dev,
  2344. "presentation frame index out of range: %d\n",
  2345. display_idx);
  2346. }
  2347. /* If a frame was copied out, return it */
  2348. if (ctx->display_idx >= 0 &&
  2349. ctx->display_idx < ctx->num_internal_frames) {
  2350. dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
  2351. dst_buf->v4l2_buf.sequence = ctx->osequence++;
  2352. vb2_set_plane_payload(dst_buf, 0, width * height * 3 / 2);
  2353. v4l2_m2m_buf_done(dst_buf, success ? VB2_BUF_STATE_DONE :
  2354. VB2_BUF_STATE_ERROR);
  2355. v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
  2356. "job finished: decoding frame (%d) (%s)\n",
  2357. dst_buf->v4l2_buf.sequence,
  2358. (dst_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) ?
  2359. "KEYFRAME" : "PFRAME");
  2360. } else {
  2361. v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
  2362. "job finished: no frame decoded\n");
  2363. }
  2364. /* The rotator will copy the current display frame next time */
  2365. ctx->display_idx = display_idx;
  2366. }
  2367. static void coda_finish_encode(struct coda_ctx *ctx)
  2368. {
  2369. struct vb2_buffer *src_buf, *dst_buf;
  2370. struct coda_dev *dev = ctx->dev;
  2371. u32 wr_ptr, start_ptr;
  2372. src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
  2373. dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
  2374. /* Get results from the coda */
  2375. start_ptr = coda_read(dev, CODA_CMD_ENC_PIC_BB_START);
  2376. wr_ptr = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
  2377. /* Calculate bytesused field */
  2378. if (dst_buf->v4l2_buf.sequence == 0) {
  2379. vb2_set_plane_payload(dst_buf, 0, wr_ptr - start_ptr +
  2380. ctx->vpu_header_size[0] +
  2381. ctx->vpu_header_size[1] +
  2382. ctx->vpu_header_size[2]);
  2383. } else {
  2384. vb2_set_plane_payload(dst_buf, 0, wr_ptr - start_ptr);
  2385. }
  2386. v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, "frame size = %u\n",
  2387. wr_ptr - start_ptr);
  2388. coda_read(dev, CODA_RET_ENC_PIC_SLICE_NUM);
  2389. coda_read(dev, CODA_RET_ENC_PIC_FLAG);
  2390. if (coda_read(dev, CODA_RET_ENC_PIC_TYPE) == 0) {
  2391. dst_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_KEYFRAME;
  2392. dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_PFRAME;
  2393. } else {
  2394. dst_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_PFRAME;
  2395. dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_KEYFRAME;
  2396. }
  2397. dst_buf->v4l2_buf.timestamp = src_buf->v4l2_buf.timestamp;
  2398. dst_buf->v4l2_buf.timecode = src_buf->v4l2_buf.timecode;
  2399. v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
  2400. v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_DONE);
  2401. ctx->gopcounter--;
  2402. if (ctx->gopcounter < 0)
  2403. ctx->gopcounter = ctx->params.gop_size - 1;
  2404. v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
  2405. "job finished: encoding frame (%d) (%s)\n",
  2406. dst_buf->v4l2_buf.sequence,
  2407. (dst_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) ?
  2408. "KEYFRAME" : "PFRAME");
  2409. }
  2410. static irqreturn_t coda_irq_handler(int irq, void *data)
  2411. {
  2412. struct coda_dev *dev = data;
  2413. struct coda_ctx *ctx;
  2414. cancel_delayed_work(&dev->timeout);
  2415. /* read status register to attend the IRQ */
  2416. coda_read(dev, CODA_REG_BIT_INT_STATUS);
  2417. coda_write(dev, CODA_REG_BIT_INT_CLEAR_SET,
  2418. CODA_REG_BIT_INT_CLEAR);
  2419. ctx = v4l2_m2m_get_curr_priv(dev->m2m_dev);
  2420. if (ctx == NULL) {
  2421. v4l2_err(&dev->v4l2_dev, "Instance released before the end of transaction\n");
  2422. mutex_unlock(&dev->coda_mutex);
  2423. return IRQ_HANDLED;
  2424. }
  2425. if (ctx->aborting) {
  2426. v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
  2427. "task has been aborted\n");
  2428. goto out;
  2429. }
  2430. if (coda_isbusy(ctx->dev)) {
  2431. v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
  2432. "coda is still busy!!!!\n");
  2433. return IRQ_NONE;
  2434. }
  2435. if (ctx->inst_type == CODA_INST_DECODER)
  2436. coda_finish_decode(ctx);
  2437. else
  2438. coda_finish_encode(ctx);
  2439. out:
  2440. if (ctx->aborting || (!ctx->streamon_cap && !ctx->streamon_out)) {
  2441. v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
  2442. "%s: sent command 'SEQ_END' to coda\n", __func__);
  2443. if (coda_command_sync(ctx, CODA_COMMAND_SEQ_END)) {
  2444. v4l2_err(&dev->v4l2_dev,
  2445. "CODA_COMMAND_SEQ_END failed\n");
  2446. }
  2447. kfifo_init(&ctx->bitstream_fifo,
  2448. ctx->bitstream.vaddr, ctx->bitstream.size);
  2449. coda_free_framebuffers(ctx);
  2450. coda_free_context_buffers(ctx);
  2451. }
  2452. mutex_unlock(&dev->coda_mutex);
  2453. mutex_unlock(&ctx->buffer_mutex);
  2454. v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->m2m_ctx);
  2455. return IRQ_HANDLED;
  2456. }
  2457. static void coda_timeout(struct work_struct *work)
  2458. {
  2459. struct coda_ctx *ctx;
  2460. struct coda_dev *dev = container_of(to_delayed_work(work),
  2461. struct coda_dev, timeout);
  2462. dev_err(&dev->plat_dev->dev, "CODA PIC_RUN timeout, stopping all streams\n");
  2463. mutex_lock(&dev->dev_mutex);
  2464. list_for_each_entry(ctx, &dev->instances, list) {
  2465. if (mutex_is_locked(&ctx->buffer_mutex))
  2466. mutex_unlock(&ctx->buffer_mutex);
  2467. v4l2_m2m_streamoff(NULL, ctx->m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
  2468. v4l2_m2m_streamoff(NULL, ctx->m2m_ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
  2469. }
  2470. mutex_unlock(&dev->dev_mutex);
  2471. mutex_unlock(&dev->coda_mutex);
  2472. ctx = v4l2_m2m_get_curr_priv(dev->m2m_dev);
  2473. v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->m2m_ctx);
  2474. }
  2475. static u32 coda_supported_firmwares[] = {
  2476. CODA_FIRMWARE_VERNUM(CODA_DX6, 2, 2, 5),
  2477. CODA_FIRMWARE_VERNUM(CODA_7541, 1, 4, 50),
  2478. };
  2479. static bool coda_firmware_supported(u32 vernum)
  2480. {
  2481. int i;
  2482. for (i = 0; i < ARRAY_SIZE(coda_supported_firmwares); i++)
  2483. if (vernum == coda_supported_firmwares[i])
  2484. return true;
  2485. return false;
  2486. }
  2487. static int coda_hw_init(struct coda_dev *dev)
  2488. {
  2489. u16 product, major, minor, release;
  2490. u32 data;
  2491. u16 *p;
  2492. int i, ret;
  2493. ret = clk_prepare_enable(dev->clk_per);
  2494. if (ret)
  2495. return ret;
  2496. ret = clk_prepare_enable(dev->clk_ahb);
  2497. if (ret)
  2498. goto err_clk_ahb;
  2499. /*
  2500. * Copy the first CODA_ISRAM_SIZE in the internal SRAM.
  2501. * The 16-bit chars in the code buffer are in memory access
  2502. * order, re-sort them to CODA order for register download.
  2503. * Data in this SRAM survives a reboot.
  2504. */
  2505. p = (u16 *)dev->codebuf.vaddr;
  2506. if (dev->devtype->product == CODA_DX6) {
  2507. for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
  2508. data = CODA_DOWN_ADDRESS_SET(i) |
  2509. CODA_DOWN_DATA_SET(p[i ^ 1]);
  2510. coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
  2511. }
  2512. } else {
  2513. for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
  2514. data = CODA_DOWN_ADDRESS_SET(i) |
  2515. CODA_DOWN_DATA_SET(p[round_down(i, 4) +
  2516. 3 - (i % 4)]);
  2517. coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
  2518. }
  2519. }
  2520. /* Clear registers */
  2521. for (i = 0; i < 64; i++)
  2522. coda_write(dev, 0, CODA_REG_BIT_CODE_BUF_ADDR + i * 4);
  2523. /* Tell the BIT where to find everything it needs */
  2524. if (dev->devtype->product == CODA_7541) {
  2525. coda_write(dev, dev->tempbuf.paddr,
  2526. CODA_REG_BIT_TEMP_BUF_ADDR);
  2527. coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
  2528. } else {
  2529. coda_write(dev, dev->workbuf.paddr,
  2530. CODA_REG_BIT_WORK_BUF_ADDR);
  2531. }
  2532. coda_write(dev, dev->codebuf.paddr,
  2533. CODA_REG_BIT_CODE_BUF_ADDR);
  2534. coda_write(dev, 0, CODA_REG_BIT_CODE_RUN);
  2535. /* Set default values */
  2536. switch (dev->devtype->product) {
  2537. case CODA_DX6:
  2538. coda_write(dev, CODADX6_STREAM_BUF_PIC_FLUSH, CODA_REG_BIT_STREAM_CTRL);
  2539. break;
  2540. default:
  2541. coda_write(dev, CODA7_STREAM_BUF_PIC_FLUSH, CODA_REG_BIT_STREAM_CTRL);
  2542. }
  2543. coda_write(dev, 0, CODA_REG_BIT_FRAME_MEM_CTRL);
  2544. if (dev->devtype->product != CODA_DX6)
  2545. coda_write(dev, 0, CODA7_REG_BIT_AXI_SRAM_USE);
  2546. coda_write(dev, CODA_INT_INTERRUPT_ENABLE,
  2547. CODA_REG_BIT_INT_ENABLE);
  2548. /* Reset VPU and start processor */
  2549. data = coda_read(dev, CODA_REG_BIT_CODE_RESET);
  2550. data |= CODA_REG_RESET_ENABLE;
  2551. coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
  2552. udelay(10);
  2553. data &= ~CODA_REG_RESET_ENABLE;
  2554. coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
  2555. coda_write(dev, CODA_REG_RUN_ENABLE, CODA_REG_BIT_CODE_RUN);
  2556. /* Load firmware */
  2557. coda_write(dev, 0, CODA_CMD_FIRMWARE_VERNUM);
  2558. coda_write(dev, CODA_REG_BIT_BUSY_FLAG, CODA_REG_BIT_BUSY);
  2559. coda_write(dev, 0, CODA_REG_BIT_RUN_INDEX);
  2560. coda_write(dev, 0, CODA_REG_BIT_RUN_COD_STD);
  2561. coda_write(dev, CODA_COMMAND_FIRMWARE_GET, CODA_REG_BIT_RUN_COMMAND);
  2562. if (coda_wait_timeout(dev)) {
  2563. clk_disable_unprepare(dev->clk_per);
  2564. clk_disable_unprepare(dev->clk_ahb);
  2565. v4l2_err(&dev->v4l2_dev, "firmware get command error\n");
  2566. return -EIO;
  2567. }
  2568. /* Check we are compatible with the loaded firmware */
  2569. data = coda_read(dev, CODA_CMD_FIRMWARE_VERNUM);
  2570. product = CODA_FIRMWARE_PRODUCT(data);
  2571. major = CODA_FIRMWARE_MAJOR(data);
  2572. minor = CODA_FIRMWARE_MINOR(data);
  2573. release = CODA_FIRMWARE_RELEASE(data);
  2574. clk_disable_unprepare(dev->clk_per);
  2575. clk_disable_unprepare(dev->clk_ahb);
  2576. if (product != dev->devtype->product) {
  2577. v4l2_err(&dev->v4l2_dev, "Wrong firmware. Hw: %s, Fw: %s,"
  2578. " Version: %u.%u.%u\n",
  2579. coda_product_name(dev->devtype->product),
  2580. coda_product_name(product), major, minor, release);
  2581. return -EINVAL;
  2582. }
  2583. v4l2_info(&dev->v4l2_dev, "Initialized %s.\n",
  2584. coda_product_name(product));
  2585. if (coda_firmware_supported(data)) {
  2586. v4l2_info(&dev->v4l2_dev, "Firmware version: %u.%u.%u\n",
  2587. major, minor, release);
  2588. } else {
  2589. v4l2_warn(&dev->v4l2_dev, "Unsupported firmware version: "
  2590. "%u.%u.%u\n", major, minor, release);
  2591. }
  2592. return 0;
  2593. err_clk_ahb:
  2594. clk_disable_unprepare(dev->clk_per);
  2595. return ret;
  2596. }
  2597. static void coda_fw_callback(const struct firmware *fw, void *context)
  2598. {
  2599. struct coda_dev *dev = context;
  2600. struct platform_device *pdev = dev->plat_dev;
  2601. int ret;
  2602. if (!fw) {
  2603. v4l2_err(&dev->v4l2_dev, "firmware request failed\n");
  2604. return;
  2605. }
  2606. /* allocate auxiliary per-device code buffer for the BIT processor */
  2607. ret = coda_alloc_aux_buf(dev, &dev->codebuf, fw->size);
  2608. if (ret < 0) {
  2609. dev_err(&pdev->dev, "failed to allocate code buffer\n");
  2610. return;
  2611. }
  2612. /* Copy the whole firmware image to the code buffer */
  2613. memcpy(dev->codebuf.vaddr, fw->data, fw->size);
  2614. release_firmware(fw);
  2615. ret = coda_hw_init(dev);
  2616. if (ret) {
  2617. v4l2_err(&dev->v4l2_dev, "HW initialization failed\n");
  2618. return;
  2619. }
  2620. dev->vfd.fops = &coda_fops,
  2621. dev->vfd.ioctl_ops = &coda_ioctl_ops;
  2622. dev->vfd.release = video_device_release_empty,
  2623. dev->vfd.lock = &dev->dev_mutex;
  2624. dev->vfd.v4l2_dev = &dev->v4l2_dev;
  2625. dev->vfd.vfl_dir = VFL_DIR_M2M;
  2626. snprintf(dev->vfd.name, sizeof(dev->vfd.name), "%s", CODA_NAME);
  2627. video_set_drvdata(&dev->vfd, dev);
  2628. dev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
  2629. if (IS_ERR(dev->alloc_ctx)) {
  2630. v4l2_err(&dev->v4l2_dev, "Failed to alloc vb2 context\n");
  2631. return;
  2632. }
  2633. dev->m2m_dev = v4l2_m2m_init(&coda_m2m_ops);
  2634. if (IS_ERR(dev->m2m_dev)) {
  2635. v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
  2636. goto rel_ctx;
  2637. }
  2638. ret = video_register_device(&dev->vfd, VFL_TYPE_GRABBER, 0);
  2639. if (ret) {
  2640. v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
  2641. goto rel_m2m;
  2642. }
  2643. v4l2_info(&dev->v4l2_dev, "codec registered as /dev/video%d\n",
  2644. dev->vfd.num);
  2645. return;
  2646. rel_m2m:
  2647. v4l2_m2m_release(dev->m2m_dev);
  2648. rel_ctx:
  2649. vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
  2650. }
  2651. static int coda_firmware_request(struct coda_dev *dev)
  2652. {
  2653. char *fw = dev->devtype->firmware;
  2654. dev_dbg(&dev->plat_dev->dev, "requesting firmware '%s' for %s\n", fw,
  2655. coda_product_name(dev->devtype->product));
  2656. return request_firmware_nowait(THIS_MODULE, true,
  2657. fw, &dev->plat_dev->dev, GFP_KERNEL, dev, coda_fw_callback);
  2658. }
  2659. enum coda_platform {
  2660. CODA_IMX27,
  2661. CODA_IMX53,
  2662. };
  2663. static const struct coda_devtype coda_devdata[] = {
  2664. [CODA_IMX27] = {
  2665. .firmware = "v4l-codadx6-imx27.bin",
  2666. .product = CODA_DX6,
  2667. .codecs = codadx6_codecs,
  2668. .num_codecs = ARRAY_SIZE(codadx6_codecs),
  2669. },
  2670. [CODA_IMX53] = {
  2671. .firmware = "v4l-coda7541-imx53.bin",
  2672. .product = CODA_7541,
  2673. .codecs = coda7_codecs,
  2674. .num_codecs = ARRAY_SIZE(coda7_codecs),
  2675. },
  2676. };
  2677. static struct platform_device_id coda_platform_ids[] = {
  2678. { .name = "coda-imx27", .driver_data = CODA_IMX27 },
  2679. { .name = "coda-imx53", .driver_data = CODA_IMX53 },
  2680. { /* sentinel */ }
  2681. };
  2682. MODULE_DEVICE_TABLE(platform, coda_platform_ids);
  2683. #ifdef CONFIG_OF
  2684. static const struct of_device_id coda_dt_ids[] = {
  2685. { .compatible = "fsl,imx27-vpu", .data = &coda_devdata[CODA_IMX27] },
  2686. { .compatible = "fsl,imx53-vpu", .data = &coda_devdata[CODA_IMX53] },
  2687. { /* sentinel */ }
  2688. };
  2689. MODULE_DEVICE_TABLE(of, coda_dt_ids);
  2690. #endif
  2691. static int coda_probe(struct platform_device *pdev)
  2692. {
  2693. const struct of_device_id *of_id =
  2694. of_match_device(of_match_ptr(coda_dt_ids), &pdev->dev);
  2695. const struct platform_device_id *pdev_id;
  2696. struct coda_platform_data *pdata = pdev->dev.platform_data;
  2697. struct device_node *np = pdev->dev.of_node;
  2698. struct gen_pool *pool;
  2699. struct coda_dev *dev;
  2700. struct resource *res;
  2701. int ret, irq;
  2702. dev = devm_kzalloc(&pdev->dev, sizeof *dev, GFP_KERNEL);
  2703. if (!dev) {
  2704. dev_err(&pdev->dev, "Not enough memory for %s\n",
  2705. CODA_NAME);
  2706. return -ENOMEM;
  2707. }
  2708. spin_lock_init(&dev->irqlock);
  2709. INIT_LIST_HEAD(&dev->instances);
  2710. INIT_DELAYED_WORK(&dev->timeout, coda_timeout);
  2711. dev->plat_dev = pdev;
  2712. dev->clk_per = devm_clk_get(&pdev->dev, "per");
  2713. if (IS_ERR(dev->clk_per)) {
  2714. dev_err(&pdev->dev, "Could not get per clock\n");
  2715. return PTR_ERR(dev->clk_per);
  2716. }
  2717. dev->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
  2718. if (IS_ERR(dev->clk_ahb)) {
  2719. dev_err(&pdev->dev, "Could not get ahb clock\n");
  2720. return PTR_ERR(dev->clk_ahb);
  2721. }
  2722. /* Get memory for physical registers */
  2723. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  2724. dev->regs_base = devm_ioremap_resource(&pdev->dev, res);
  2725. if (IS_ERR(dev->regs_base))
  2726. return PTR_ERR(dev->regs_base);
  2727. /* IRQ */
  2728. irq = platform_get_irq(pdev, 0);
  2729. if (irq < 0) {
  2730. dev_err(&pdev->dev, "failed to get irq resource\n");
  2731. return -ENOENT;
  2732. }
  2733. if (devm_request_threaded_irq(&pdev->dev, irq, NULL, coda_irq_handler,
  2734. IRQF_ONESHOT, CODA_NAME, dev) < 0) {
  2735. dev_err(&pdev->dev, "failed to request irq\n");
  2736. return -ENOENT;
  2737. }
  2738. /* Get IRAM pool from device tree or platform data */
  2739. pool = of_get_named_gen_pool(np, "iram", 0);
  2740. if (!pool && pdata)
  2741. pool = dev_get_gen_pool(pdata->iram_dev);
  2742. if (!pool) {
  2743. dev_err(&pdev->dev, "iram pool not available\n");
  2744. return -ENOMEM;
  2745. }
  2746. dev->iram_pool = pool;
  2747. ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
  2748. if (ret)
  2749. return ret;
  2750. mutex_init(&dev->dev_mutex);
  2751. mutex_init(&dev->coda_mutex);
  2752. pdev_id = of_id ? of_id->data : platform_get_device_id(pdev);
  2753. if (of_id) {
  2754. dev->devtype = of_id->data;
  2755. } else if (pdev_id) {
  2756. dev->devtype = &coda_devdata[pdev_id->driver_data];
  2757. } else {
  2758. v4l2_device_unregister(&dev->v4l2_dev);
  2759. return -EINVAL;
  2760. }
  2761. /* allocate auxiliary per-device buffers for the BIT processor */
  2762. switch (dev->devtype->product) {
  2763. case CODA_DX6:
  2764. ret = coda_alloc_aux_buf(dev, &dev->workbuf,
  2765. CODADX6_WORK_BUF_SIZE);
  2766. if (ret < 0) {
  2767. dev_err(&pdev->dev, "failed to allocate work buffer\n");
  2768. v4l2_device_unregister(&dev->v4l2_dev);
  2769. return ret;
  2770. }
  2771. break;
  2772. case CODA_7541:
  2773. dev->tempbuf.size = CODA7_TEMP_BUF_SIZE;
  2774. break;
  2775. }
  2776. if (dev->tempbuf.size) {
  2777. ret = coda_alloc_aux_buf(dev, &dev->tempbuf,
  2778. dev->tempbuf.size);
  2779. if (ret < 0) {
  2780. dev_err(&pdev->dev, "failed to allocate temp buffer\n");
  2781. v4l2_device_unregister(&dev->v4l2_dev);
  2782. return ret;
  2783. }
  2784. }
  2785. switch (dev->devtype->product) {
  2786. case CODA_DX6:
  2787. dev->iram_size = CODADX6_IRAM_SIZE;
  2788. break;
  2789. case CODA_7541:
  2790. dev->iram_size = CODA7_IRAM_SIZE;
  2791. break;
  2792. }
  2793. dev->iram_vaddr = gen_pool_alloc(dev->iram_pool, dev->iram_size);
  2794. if (!dev->iram_vaddr) {
  2795. dev_err(&pdev->dev, "unable to alloc iram\n");
  2796. return -ENOMEM;
  2797. }
  2798. dev->iram_paddr = gen_pool_virt_to_phys(dev->iram_pool,
  2799. dev->iram_vaddr);
  2800. platform_set_drvdata(pdev, dev);
  2801. return coda_firmware_request(dev);
  2802. }
  2803. static int coda_remove(struct platform_device *pdev)
  2804. {
  2805. struct coda_dev *dev = platform_get_drvdata(pdev);
  2806. video_unregister_device(&dev->vfd);
  2807. if (dev->m2m_dev)
  2808. v4l2_m2m_release(dev->m2m_dev);
  2809. if (dev->alloc_ctx)
  2810. vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
  2811. v4l2_device_unregister(&dev->v4l2_dev);
  2812. if (dev->iram_vaddr)
  2813. gen_pool_free(dev->iram_pool, dev->iram_vaddr, dev->iram_size);
  2814. coda_free_aux_buf(dev, &dev->codebuf);
  2815. coda_free_aux_buf(dev, &dev->tempbuf);
  2816. coda_free_aux_buf(dev, &dev->workbuf);
  2817. return 0;
  2818. }
  2819. static struct platform_driver coda_driver = {
  2820. .probe = coda_probe,
  2821. .remove = coda_remove,
  2822. .driver = {
  2823. .name = CODA_NAME,
  2824. .owner = THIS_MODULE,
  2825. .of_match_table = of_match_ptr(coda_dt_ids),
  2826. },
  2827. .id_table = coda_platform_ids,
  2828. };
  2829. module_platform_driver(coda_driver);
  2830. MODULE_LICENSE("GPL");
  2831. MODULE_AUTHOR("Javier Martin <javier.martin@vista-silicon.com>");
  2832. MODULE_DESCRIPTION("Coda multi-standard codec V4L2 driver");