coda-common.c 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238
  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. { .name = "coda-imx53", .driver_data = CODA_IMX53 },
  1696. { /* sentinel */ }
  1697. };
  1698. MODULE_DEVICE_TABLE(platform, coda_platform_ids);
  1699. #ifdef CONFIG_OF
  1700. static const struct of_device_id coda_dt_ids[] = {
  1701. { .compatible = "fsl,imx27-vpu", .data = &coda_devdata[CODA_IMX27] },
  1702. { .compatible = "fsl,imx53-vpu", .data = &coda_devdata[CODA_IMX53] },
  1703. { .compatible = "fsl,imx6q-vpu", .data = &coda_devdata[CODA_IMX6Q] },
  1704. { .compatible = "fsl,imx6dl-vpu", .data = &coda_devdata[CODA_IMX6DL] },
  1705. { /* sentinel */ }
  1706. };
  1707. MODULE_DEVICE_TABLE(of, coda_dt_ids);
  1708. #endif
  1709. static int coda_probe(struct platform_device *pdev)
  1710. {
  1711. const struct of_device_id *of_id =
  1712. of_match_device(of_match_ptr(coda_dt_ids), &pdev->dev);
  1713. const struct platform_device_id *pdev_id;
  1714. struct coda_platform_data *pdata = pdev->dev.platform_data;
  1715. struct device_node *np = pdev->dev.of_node;
  1716. struct gen_pool *pool;
  1717. struct coda_dev *dev;
  1718. struct resource *res;
  1719. int ret, irq;
  1720. dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
  1721. if (!dev)
  1722. return -ENOMEM;
  1723. pdev_id = of_id ? of_id->data : platform_get_device_id(pdev);
  1724. if (of_id) {
  1725. dev->devtype = of_id->data;
  1726. } else if (pdev_id) {
  1727. dev->devtype = &coda_devdata[pdev_id->driver_data];
  1728. } else {
  1729. ret = -EINVAL;
  1730. goto err_v4l2_register;
  1731. }
  1732. spin_lock_init(&dev->irqlock);
  1733. INIT_LIST_HEAD(&dev->instances);
  1734. dev->plat_dev = pdev;
  1735. dev->clk_per = devm_clk_get(&pdev->dev, "per");
  1736. if (IS_ERR(dev->clk_per)) {
  1737. dev_err(&pdev->dev, "Could not get per clock\n");
  1738. return PTR_ERR(dev->clk_per);
  1739. }
  1740. dev->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
  1741. if (IS_ERR(dev->clk_ahb)) {
  1742. dev_err(&pdev->dev, "Could not get ahb clock\n");
  1743. return PTR_ERR(dev->clk_ahb);
  1744. }
  1745. /* Get memory for physical registers */
  1746. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1747. dev->regs_base = devm_ioremap_resource(&pdev->dev, res);
  1748. if (IS_ERR(dev->regs_base))
  1749. return PTR_ERR(dev->regs_base);
  1750. /* IRQ */
  1751. irq = platform_get_irq_byname(pdev, "bit");
  1752. if (irq < 0)
  1753. irq = platform_get_irq(pdev, 0);
  1754. if (irq < 0) {
  1755. dev_err(&pdev->dev, "failed to get irq resource\n");
  1756. return irq;
  1757. }
  1758. ret = devm_request_threaded_irq(&pdev->dev, irq, NULL, coda_irq_handler,
  1759. IRQF_ONESHOT, dev_name(&pdev->dev), dev);
  1760. if (ret < 0) {
  1761. dev_err(&pdev->dev, "failed to request irq: %d\n", ret);
  1762. return ret;
  1763. }
  1764. dev->rstc = devm_reset_control_get_optional(&pdev->dev, NULL);
  1765. if (IS_ERR(dev->rstc)) {
  1766. ret = PTR_ERR(dev->rstc);
  1767. if (ret == -ENOENT || ret == -ENOSYS) {
  1768. dev->rstc = NULL;
  1769. } else {
  1770. dev_err(&pdev->dev, "failed get reset control: %d\n",
  1771. ret);
  1772. return ret;
  1773. }
  1774. }
  1775. /* Get IRAM pool from device tree or platform data */
  1776. pool = of_get_named_gen_pool(np, "iram", 0);
  1777. if (!pool && pdata)
  1778. pool = dev_get_gen_pool(pdata->iram_dev);
  1779. if (!pool) {
  1780. dev_err(&pdev->dev, "iram pool not available\n");
  1781. return -ENOMEM;
  1782. }
  1783. dev->iram_pool = pool;
  1784. ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
  1785. if (ret)
  1786. return ret;
  1787. mutex_init(&dev->dev_mutex);
  1788. mutex_init(&dev->coda_mutex);
  1789. dev->debugfs_root = debugfs_create_dir("coda", NULL);
  1790. if (!dev->debugfs_root)
  1791. dev_warn(&pdev->dev, "failed to create debugfs root\n");
  1792. /* allocate auxiliary per-device buffers for the BIT processor */
  1793. if (dev->devtype->product == CODA_DX6) {
  1794. ret = coda_alloc_aux_buf(dev, &dev->workbuf,
  1795. dev->devtype->workbuf_size, "workbuf",
  1796. dev->debugfs_root);
  1797. if (ret < 0) {
  1798. dev_err(&pdev->dev, "failed to allocate work buffer\n");
  1799. goto err_v4l2_register;
  1800. }
  1801. }
  1802. if (dev->devtype->tempbuf_size) {
  1803. ret = coda_alloc_aux_buf(dev, &dev->tempbuf,
  1804. dev->devtype->tempbuf_size, "tempbuf",
  1805. dev->debugfs_root);
  1806. if (ret < 0) {
  1807. dev_err(&pdev->dev, "failed to allocate temp buffer\n");
  1808. goto err_v4l2_register;
  1809. }
  1810. }
  1811. dev->iram.size = dev->devtype->iram_size;
  1812. dev->iram.vaddr = gen_pool_dma_alloc(dev->iram_pool, dev->iram.size,
  1813. &dev->iram.paddr);
  1814. if (!dev->iram.vaddr) {
  1815. dev_warn(&pdev->dev, "unable to alloc iram\n");
  1816. } else {
  1817. dev->iram.blob.data = dev->iram.vaddr;
  1818. dev->iram.blob.size = dev->iram.size;
  1819. dev->iram.dentry = debugfs_create_blob("iram", 0644,
  1820. dev->debugfs_root,
  1821. &dev->iram.blob);
  1822. }
  1823. dev->workqueue = alloc_workqueue("coda", WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
  1824. if (!dev->workqueue) {
  1825. dev_err(&pdev->dev, "unable to alloc workqueue\n");
  1826. ret = -ENOMEM;
  1827. goto err_v4l2_register;
  1828. }
  1829. platform_set_drvdata(pdev, dev);
  1830. /*
  1831. * Start activated so we can directly call coda_hw_init in
  1832. * coda_fw_callback regardless of whether CONFIG_PM is
  1833. * enabled or whether the device is associated with a PM domain.
  1834. */
  1835. pm_runtime_get_noresume(&pdev->dev);
  1836. pm_runtime_set_active(&pdev->dev);
  1837. pm_runtime_enable(&pdev->dev);
  1838. return coda_firmware_request(dev);
  1839. err_v4l2_register:
  1840. v4l2_device_unregister(&dev->v4l2_dev);
  1841. return ret;
  1842. }
  1843. static int coda_remove(struct platform_device *pdev)
  1844. {
  1845. struct coda_dev *dev = platform_get_drvdata(pdev);
  1846. int i;
  1847. for (i = 0; i < ARRAY_SIZE(dev->vfd); i++) {
  1848. if (video_get_drvdata(&dev->vfd[i]))
  1849. video_unregister_device(&dev->vfd[i]);
  1850. }
  1851. if (dev->m2m_dev)
  1852. v4l2_m2m_release(dev->m2m_dev);
  1853. pm_runtime_disable(&pdev->dev);
  1854. if (dev->alloc_ctx)
  1855. vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
  1856. v4l2_device_unregister(&dev->v4l2_dev);
  1857. destroy_workqueue(dev->workqueue);
  1858. if (dev->iram.vaddr)
  1859. gen_pool_free(dev->iram_pool, (unsigned long)dev->iram.vaddr,
  1860. dev->iram.size);
  1861. coda_free_aux_buf(dev, &dev->codebuf);
  1862. coda_free_aux_buf(dev, &dev->tempbuf);
  1863. coda_free_aux_buf(dev, &dev->workbuf);
  1864. debugfs_remove_recursive(dev->debugfs_root);
  1865. return 0;
  1866. }
  1867. #ifdef CONFIG_PM
  1868. static int coda_runtime_resume(struct device *dev)
  1869. {
  1870. struct coda_dev *cdev = dev_get_drvdata(dev);
  1871. int ret = 0;
  1872. if (dev->pm_domain && cdev->codebuf.vaddr) {
  1873. ret = coda_hw_init(cdev);
  1874. if (ret)
  1875. v4l2_err(&cdev->v4l2_dev, "HW initialization failed\n");
  1876. }
  1877. return ret;
  1878. }
  1879. #endif
  1880. static const struct dev_pm_ops coda_pm_ops = {
  1881. SET_RUNTIME_PM_OPS(NULL, coda_runtime_resume, NULL)
  1882. };
  1883. static struct platform_driver coda_driver = {
  1884. .probe = coda_probe,
  1885. .remove = coda_remove,
  1886. .driver = {
  1887. .name = CODA_NAME,
  1888. .of_match_table = of_match_ptr(coda_dt_ids),
  1889. .pm = &coda_pm_ops,
  1890. },
  1891. .id_table = coda_platform_ids,
  1892. };
  1893. module_platform_driver(coda_driver);
  1894. MODULE_LICENSE("GPL");
  1895. MODULE_AUTHOR("Javier Martin <javier.martin@vista-silicon.com>");
  1896. MODULE_DESCRIPTION("Coda multi-standard codec V4L2 driver");