coda-common.c 57 KB

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