coda-common.c 53 KB

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