coda-common.c 59 KB

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