coda-common.c 60 KB

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