coda-common.c 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065
  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. ctx->aborting = 0;
  734. /* Default formats for output and input queues */
  735. ctx->q_data[V4L2_M2M_SRC].fourcc = ctx->codec->src_fourcc;
  736. ctx->q_data[V4L2_M2M_DST].fourcc = ctx->codec->dst_fourcc;
  737. ctx->q_data[V4L2_M2M_SRC].width = max_w;
  738. ctx->q_data[V4L2_M2M_SRC].height = max_h;
  739. ctx->q_data[V4L2_M2M_DST].width = max_w;
  740. ctx->q_data[V4L2_M2M_DST].height = max_h;
  741. if (ctx->codec->src_fourcc == V4L2_PIX_FMT_YUV420) {
  742. ctx->q_data[V4L2_M2M_SRC].bytesperline = max_w;
  743. ctx->q_data[V4L2_M2M_SRC].sizeimage = (max_w * max_h * 3) / 2;
  744. ctx->q_data[V4L2_M2M_DST].bytesperline = 0;
  745. ctx->q_data[V4L2_M2M_DST].sizeimage = CODA_MAX_FRAME_SIZE;
  746. } else {
  747. ctx->q_data[V4L2_M2M_SRC].bytesperline = 0;
  748. ctx->q_data[V4L2_M2M_SRC].sizeimage = CODA_MAX_FRAME_SIZE;
  749. ctx->q_data[V4L2_M2M_DST].bytesperline = max_w;
  750. ctx->q_data[V4L2_M2M_DST].sizeimage = (max_w * max_h * 3) / 2;
  751. }
  752. ctx->q_data[V4L2_M2M_SRC].rect.width = max_w;
  753. ctx->q_data[V4L2_M2M_SRC].rect.height = max_h;
  754. ctx->q_data[V4L2_M2M_DST].rect.width = max_w;
  755. ctx->q_data[V4L2_M2M_DST].rect.height = max_h;
  756. if (ctx->dev->devtype->product == CODA_960)
  757. coda_set_tiled_map_type(ctx, GDI_LINEAR_FRAME_MAP);
  758. }
  759. /*
  760. * Queue operations
  761. */
  762. static int coda_queue_setup(struct vb2_queue *vq,
  763. const struct v4l2_format *fmt,
  764. unsigned int *nbuffers, unsigned int *nplanes,
  765. unsigned int sizes[], void *alloc_ctxs[])
  766. {
  767. struct coda_ctx *ctx = vb2_get_drv_priv(vq);
  768. struct coda_q_data *q_data;
  769. unsigned int size;
  770. q_data = get_q_data(ctx, vq->type);
  771. size = q_data->sizeimage;
  772. *nplanes = 1;
  773. sizes[0] = size;
  774. alloc_ctxs[0] = ctx->dev->alloc_ctx;
  775. v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
  776. "get %d buffer(s) of size %d each.\n", *nbuffers, size);
  777. return 0;
  778. }
  779. static int coda_buf_prepare(struct vb2_buffer *vb)
  780. {
  781. struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  782. struct coda_q_data *q_data;
  783. q_data = get_q_data(ctx, vb->vb2_queue->type);
  784. if (vb2_plane_size(vb, 0) < q_data->sizeimage) {
  785. v4l2_warn(&ctx->dev->v4l2_dev,
  786. "%s data will not fit into plane (%lu < %lu)\n",
  787. __func__, vb2_plane_size(vb, 0),
  788. (long)q_data->sizeimage);
  789. return -EINVAL;
  790. }
  791. return 0;
  792. }
  793. static void coda_buf_queue(struct vb2_buffer *vb)
  794. {
  795. struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  796. struct coda_q_data *q_data;
  797. q_data = get_q_data(ctx, vb->vb2_queue->type);
  798. /*
  799. * In the decoder case, immediately try to copy the buffer into the
  800. * bitstream ringbuffer and mark it as ready to be dequeued.
  801. */
  802. if (q_data->fourcc == V4L2_PIX_FMT_H264 &&
  803. vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
  804. /*
  805. * For backwards compatibility, queuing an empty buffer marks
  806. * the stream end
  807. */
  808. if (vb2_get_plane_payload(vb, 0) == 0)
  809. coda_bit_stream_end_flag(ctx);
  810. mutex_lock(&ctx->bitstream_mutex);
  811. v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vb);
  812. if (vb2_is_streaming(vb->vb2_queue))
  813. coda_fill_bitstream(ctx);
  814. mutex_unlock(&ctx->bitstream_mutex);
  815. } else {
  816. v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vb);
  817. }
  818. }
  819. int coda_alloc_aux_buf(struct coda_dev *dev, struct coda_aux_buf *buf,
  820. size_t size, const char *name, struct dentry *parent)
  821. {
  822. buf->vaddr = dma_alloc_coherent(&dev->plat_dev->dev, size, &buf->paddr,
  823. GFP_KERNEL);
  824. if (!buf->vaddr) {
  825. v4l2_err(&dev->v4l2_dev,
  826. "Failed to allocate %s buffer of size %u\n",
  827. name, size);
  828. return -ENOMEM;
  829. }
  830. buf->size = size;
  831. if (name && parent) {
  832. buf->blob.data = buf->vaddr;
  833. buf->blob.size = size;
  834. buf->dentry = debugfs_create_blob(name, 0644, parent,
  835. &buf->blob);
  836. if (!buf->dentry)
  837. dev_warn(&dev->plat_dev->dev,
  838. "failed to create debugfs entry %s\n", name);
  839. }
  840. return 0;
  841. }
  842. void coda_free_aux_buf(struct coda_dev *dev,
  843. struct coda_aux_buf *buf)
  844. {
  845. if (buf->vaddr) {
  846. dma_free_coherent(&dev->plat_dev->dev, buf->size,
  847. buf->vaddr, buf->paddr);
  848. buf->vaddr = NULL;
  849. buf->size = 0;
  850. }
  851. debugfs_remove(buf->dentry);
  852. }
  853. static int coda_start_streaming(struct vb2_queue *q, unsigned int count)
  854. {
  855. struct coda_ctx *ctx = vb2_get_drv_priv(q);
  856. struct v4l2_device *v4l2_dev = &ctx->dev->v4l2_dev;
  857. struct coda_q_data *q_data_src, *q_data_dst;
  858. struct vb2_buffer *buf;
  859. u32 dst_fourcc;
  860. int ret = 0;
  861. q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
  862. if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
  863. if (q_data_src->fourcc == V4L2_PIX_FMT_H264) {
  864. /* copy the buffers that where queued before streamon */
  865. mutex_lock(&ctx->bitstream_mutex);
  866. coda_fill_bitstream(ctx);
  867. mutex_unlock(&ctx->bitstream_mutex);
  868. if (coda_get_bitstream_payload(ctx) < 512) {
  869. ret = -EINVAL;
  870. goto err;
  871. }
  872. } else {
  873. if (count < 1) {
  874. ret = -EINVAL;
  875. goto err;
  876. }
  877. }
  878. ctx->streamon_out = 1;
  879. } else {
  880. if (count < 1) {
  881. ret = -EINVAL;
  882. goto err;
  883. }
  884. ctx->streamon_cap = 1;
  885. }
  886. /* Don't start the coda unless both queues are on */
  887. if (!(ctx->streamon_out & ctx->streamon_cap))
  888. return 0;
  889. /* Allow decoder device_run with no new buffers queued */
  890. if (ctx->inst_type == CODA_INST_DECODER)
  891. v4l2_m2m_set_src_buffered(ctx->fh.m2m_ctx, true);
  892. ctx->gopcounter = ctx->params.gop_size - 1;
  893. q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
  894. dst_fourcc = q_data_dst->fourcc;
  895. ctx->codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
  896. q_data_dst->fourcc);
  897. if (!ctx->codec) {
  898. v4l2_err(v4l2_dev, "couldn't tell instance type.\n");
  899. ret = -EINVAL;
  900. goto err;
  901. }
  902. ret = ctx->ops->start_streaming(ctx);
  903. if (ctx->inst_type == CODA_INST_DECODER) {
  904. if (ret == -EAGAIN)
  905. return 0;
  906. else if (ret < 0)
  907. goto err;
  908. }
  909. ctx->initialized = 1;
  910. return ret;
  911. err:
  912. if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
  913. while ((buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx)))
  914. v4l2_m2m_buf_done(buf, VB2_BUF_STATE_DEQUEUED);
  915. } else {
  916. while ((buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx)))
  917. v4l2_m2m_buf_done(buf, VB2_BUF_STATE_DEQUEUED);
  918. }
  919. return ret;
  920. }
  921. static void coda_stop_streaming(struct vb2_queue *q)
  922. {
  923. struct coda_ctx *ctx = vb2_get_drv_priv(q);
  924. struct coda_dev *dev = ctx->dev;
  925. struct vb2_buffer *buf;
  926. if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
  927. v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
  928. "%s: output\n", __func__);
  929. ctx->streamon_out = 0;
  930. coda_bit_stream_end_flag(ctx);
  931. ctx->isequence = 0;
  932. while ((buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx)))
  933. v4l2_m2m_buf_done(buf, VB2_BUF_STATE_ERROR);
  934. } else {
  935. v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
  936. "%s: capture\n", __func__);
  937. ctx->streamon_cap = 0;
  938. ctx->osequence = 0;
  939. ctx->sequence_offset = 0;
  940. while ((buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx)))
  941. v4l2_m2m_buf_done(buf, VB2_BUF_STATE_ERROR);
  942. }
  943. if (!ctx->streamon_out && !ctx->streamon_cap) {
  944. struct coda_timestamp *ts;
  945. mutex_lock(&ctx->bitstream_mutex);
  946. while (!list_empty(&ctx->timestamp_list)) {
  947. ts = list_first_entry(&ctx->timestamp_list,
  948. struct coda_timestamp, list);
  949. list_del(&ts->list);
  950. kfree(ts);
  951. }
  952. mutex_unlock(&ctx->bitstream_mutex);
  953. kfifo_init(&ctx->bitstream_fifo,
  954. ctx->bitstream.vaddr, ctx->bitstream.size);
  955. ctx->runcounter = 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. return;
  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. return;
  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. if (pm_runtime_enabled(&pdev->dev) && pdev->dev.pm_domain) {
  1438. /*
  1439. * Enabling power temporarily will cause coda_hw_init to be
  1440. * called via coda_runtime_resume by the pm domain.
  1441. */
  1442. ret = pm_runtime_get_sync(&dev->plat_dev->dev);
  1443. if (ret < 0) {
  1444. v4l2_err(&dev->v4l2_dev, "failed to power on: %d\n",
  1445. ret);
  1446. return;
  1447. }
  1448. ret = coda_check_firmware(dev);
  1449. if (ret < 0)
  1450. return;
  1451. pm_runtime_put_sync(&dev->plat_dev->dev);
  1452. } else {
  1453. /*
  1454. * If runtime pm is disabled or pm_domain is not set,
  1455. * initialize once manually.
  1456. */
  1457. ret = coda_hw_init(dev);
  1458. if (ret < 0) {
  1459. v4l2_err(&dev->v4l2_dev, "HW initialization failed\n");
  1460. return;
  1461. }
  1462. ret = coda_check_firmware(dev);
  1463. if (ret < 0)
  1464. return;
  1465. }
  1466. dev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
  1467. if (IS_ERR(dev->alloc_ctx)) {
  1468. v4l2_err(&dev->v4l2_dev, "Failed to alloc vb2 context\n");
  1469. return;
  1470. }
  1471. dev->m2m_dev = v4l2_m2m_init(&coda_m2m_ops);
  1472. if (IS_ERR(dev->m2m_dev)) {
  1473. v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
  1474. goto rel_ctx;
  1475. }
  1476. dev->vfd[0].fops = &coda_encoder_fops,
  1477. dev->vfd[0].ioctl_ops = &coda_ioctl_ops;
  1478. snprintf(dev->vfd[0].name, sizeof(dev->vfd[0].name), "coda-encoder");
  1479. ret = coda_register_device(dev, &dev->vfd[0]);
  1480. if (ret) {
  1481. v4l2_err(&dev->v4l2_dev,
  1482. "Failed to register encoder video device\n");
  1483. goto rel_m2m;
  1484. }
  1485. dev->vfd[1].fops = &coda_decoder_fops,
  1486. dev->vfd[1].ioctl_ops = &coda_ioctl_ops;
  1487. snprintf(dev->vfd[1].name, sizeof(dev->vfd[1].name), "coda-decoder");
  1488. ret = coda_register_device(dev, &dev->vfd[1]);
  1489. if (ret) {
  1490. v4l2_err(&dev->v4l2_dev,
  1491. "Failed to register decoder video device\n");
  1492. goto rel_m2m;
  1493. }
  1494. v4l2_info(&dev->v4l2_dev, "codec registered as /dev/video[%d-%d]\n",
  1495. dev->vfd[0].num, dev->vfd[1].num);
  1496. return;
  1497. rel_m2m:
  1498. v4l2_m2m_release(dev->m2m_dev);
  1499. rel_ctx:
  1500. vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
  1501. }
  1502. static int coda_firmware_request(struct coda_dev *dev)
  1503. {
  1504. char *fw = dev->devtype->firmware;
  1505. dev_dbg(&dev->plat_dev->dev, "requesting firmware '%s' for %s\n", fw,
  1506. coda_product_name(dev->devtype->product));
  1507. return request_firmware_nowait(THIS_MODULE, true,
  1508. fw, &dev->plat_dev->dev, GFP_KERNEL, dev, coda_fw_callback);
  1509. }
  1510. enum coda_platform {
  1511. CODA_IMX27,
  1512. CODA_IMX53,
  1513. CODA_IMX6Q,
  1514. CODA_IMX6DL,
  1515. };
  1516. static const struct coda_devtype coda_devdata[] = {
  1517. [CODA_IMX27] = {
  1518. .firmware = "v4l-codadx6-imx27.bin",
  1519. .product = CODA_DX6,
  1520. .codecs = codadx6_codecs,
  1521. .num_codecs = ARRAY_SIZE(codadx6_codecs),
  1522. .workbuf_size = 288 * 1024 + FMO_SLICE_SAVE_BUF_SIZE * 8 * 1024,
  1523. .iram_size = 0xb000,
  1524. },
  1525. [CODA_IMX53] = {
  1526. .firmware = "v4l-coda7541-imx53.bin",
  1527. .product = CODA_7541,
  1528. .codecs = coda7_codecs,
  1529. .num_codecs = ARRAY_SIZE(coda7_codecs),
  1530. .workbuf_size = 128 * 1024,
  1531. .tempbuf_size = 304 * 1024,
  1532. .iram_size = 0x14000,
  1533. },
  1534. [CODA_IMX6Q] = {
  1535. .firmware = "v4l-coda960-imx6q.bin",
  1536. .product = CODA_960,
  1537. .codecs = coda9_codecs,
  1538. .num_codecs = ARRAY_SIZE(coda9_codecs),
  1539. .workbuf_size = 80 * 1024,
  1540. .tempbuf_size = 204 * 1024,
  1541. .iram_size = 0x21000,
  1542. },
  1543. [CODA_IMX6DL] = {
  1544. .firmware = "v4l-coda960-imx6dl.bin",
  1545. .product = CODA_960,
  1546. .codecs = coda9_codecs,
  1547. .num_codecs = ARRAY_SIZE(coda9_codecs),
  1548. .workbuf_size = 80 * 1024,
  1549. .tempbuf_size = 204 * 1024,
  1550. .iram_size = 0x20000,
  1551. },
  1552. };
  1553. static struct platform_device_id coda_platform_ids[] = {
  1554. { .name = "coda-imx27", .driver_data = CODA_IMX27 },
  1555. { .name = "coda-imx53", .driver_data = CODA_IMX53 },
  1556. { /* sentinel */ }
  1557. };
  1558. MODULE_DEVICE_TABLE(platform, coda_platform_ids);
  1559. #ifdef CONFIG_OF
  1560. static const struct of_device_id coda_dt_ids[] = {
  1561. { .compatible = "fsl,imx27-vpu", .data = &coda_devdata[CODA_IMX27] },
  1562. { .compatible = "fsl,imx53-vpu", .data = &coda_devdata[CODA_IMX53] },
  1563. { .compatible = "fsl,imx6q-vpu", .data = &coda_devdata[CODA_IMX6Q] },
  1564. { .compatible = "fsl,imx6dl-vpu", .data = &coda_devdata[CODA_IMX6DL] },
  1565. { /* sentinel */ }
  1566. };
  1567. MODULE_DEVICE_TABLE(of, coda_dt_ids);
  1568. #endif
  1569. static int coda_probe(struct platform_device *pdev)
  1570. {
  1571. const struct of_device_id *of_id =
  1572. of_match_device(of_match_ptr(coda_dt_ids), &pdev->dev);
  1573. const struct platform_device_id *pdev_id;
  1574. struct coda_platform_data *pdata = pdev->dev.platform_data;
  1575. struct device_node *np = pdev->dev.of_node;
  1576. struct gen_pool *pool;
  1577. struct coda_dev *dev;
  1578. struct resource *res;
  1579. int ret, irq;
  1580. dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
  1581. if (!dev) {
  1582. dev_err(&pdev->dev, "Not enough memory for %s\n",
  1583. CODA_NAME);
  1584. return -ENOMEM;
  1585. }
  1586. spin_lock_init(&dev->irqlock);
  1587. INIT_LIST_HEAD(&dev->instances);
  1588. dev->plat_dev = pdev;
  1589. dev->clk_per = devm_clk_get(&pdev->dev, "per");
  1590. if (IS_ERR(dev->clk_per)) {
  1591. dev_err(&pdev->dev, "Could not get per clock\n");
  1592. return PTR_ERR(dev->clk_per);
  1593. }
  1594. dev->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
  1595. if (IS_ERR(dev->clk_ahb)) {
  1596. dev_err(&pdev->dev, "Could not get ahb clock\n");
  1597. return PTR_ERR(dev->clk_ahb);
  1598. }
  1599. /* Get memory for physical registers */
  1600. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1601. dev->regs_base = devm_ioremap_resource(&pdev->dev, res);
  1602. if (IS_ERR(dev->regs_base))
  1603. return PTR_ERR(dev->regs_base);
  1604. /* IRQ */
  1605. irq = platform_get_irq_byname(pdev, "bit");
  1606. if (irq < 0)
  1607. irq = platform_get_irq(pdev, 0);
  1608. if (irq < 0) {
  1609. dev_err(&pdev->dev, "failed to get irq resource\n");
  1610. return irq;
  1611. }
  1612. ret = devm_request_threaded_irq(&pdev->dev, irq, NULL, coda_irq_handler,
  1613. IRQF_ONESHOT, dev_name(&pdev->dev), dev);
  1614. if (ret < 0) {
  1615. dev_err(&pdev->dev, "failed to request irq: %d\n", ret);
  1616. return ret;
  1617. }
  1618. dev->rstc = devm_reset_control_get_optional(&pdev->dev, NULL);
  1619. if (IS_ERR(dev->rstc)) {
  1620. ret = PTR_ERR(dev->rstc);
  1621. if (ret == -ENOENT || ret == -ENOSYS) {
  1622. dev->rstc = NULL;
  1623. } else {
  1624. dev_err(&pdev->dev, "failed get reset control: %d\n",
  1625. ret);
  1626. return ret;
  1627. }
  1628. }
  1629. /* Get IRAM pool from device tree or platform data */
  1630. pool = of_get_named_gen_pool(np, "iram", 0);
  1631. if (!pool && pdata)
  1632. pool = dev_get_gen_pool(pdata->iram_dev);
  1633. if (!pool) {
  1634. dev_err(&pdev->dev, "iram pool not available\n");
  1635. return -ENOMEM;
  1636. }
  1637. dev->iram_pool = pool;
  1638. ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
  1639. if (ret)
  1640. return ret;
  1641. mutex_init(&dev->dev_mutex);
  1642. mutex_init(&dev->coda_mutex);
  1643. pdev_id = of_id ? of_id->data : platform_get_device_id(pdev);
  1644. if (of_id) {
  1645. dev->devtype = of_id->data;
  1646. } else if (pdev_id) {
  1647. dev->devtype = &coda_devdata[pdev_id->driver_data];
  1648. } else {
  1649. v4l2_device_unregister(&dev->v4l2_dev);
  1650. return -EINVAL;
  1651. }
  1652. dev->debugfs_root = debugfs_create_dir("coda", NULL);
  1653. if (!dev->debugfs_root)
  1654. dev_warn(&pdev->dev, "failed to create debugfs root\n");
  1655. /* allocate auxiliary per-device buffers for the BIT processor */
  1656. if (dev->devtype->product == CODA_DX6) {
  1657. ret = coda_alloc_aux_buf(dev, &dev->workbuf,
  1658. dev->devtype->workbuf_size, "workbuf",
  1659. dev->debugfs_root);
  1660. if (ret < 0) {
  1661. dev_err(&pdev->dev, "failed to allocate work buffer\n");
  1662. v4l2_device_unregister(&dev->v4l2_dev);
  1663. return ret;
  1664. }
  1665. }
  1666. if (dev->devtype->tempbuf_size) {
  1667. ret = coda_alloc_aux_buf(dev, &dev->tempbuf,
  1668. dev->devtype->tempbuf_size, "tempbuf",
  1669. dev->debugfs_root);
  1670. if (ret < 0) {
  1671. dev_err(&pdev->dev, "failed to allocate temp buffer\n");
  1672. v4l2_device_unregister(&dev->v4l2_dev);
  1673. return ret;
  1674. }
  1675. }
  1676. dev->iram.size = dev->devtype->iram_size;
  1677. dev->iram.vaddr = gen_pool_dma_alloc(dev->iram_pool, dev->iram.size,
  1678. &dev->iram.paddr);
  1679. if (!dev->iram.vaddr) {
  1680. dev_warn(&pdev->dev, "unable to alloc iram\n");
  1681. } else {
  1682. dev->iram.blob.data = dev->iram.vaddr;
  1683. dev->iram.blob.size = dev->iram.size;
  1684. dev->iram.dentry = debugfs_create_blob("iram", 0644,
  1685. dev->debugfs_root,
  1686. &dev->iram.blob);
  1687. }
  1688. dev->workqueue = alloc_workqueue("coda", WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
  1689. if (!dev->workqueue) {
  1690. dev_err(&pdev->dev, "unable to alloc workqueue\n");
  1691. return -ENOMEM;
  1692. }
  1693. platform_set_drvdata(pdev, dev);
  1694. pm_runtime_enable(&pdev->dev);
  1695. return coda_firmware_request(dev);
  1696. }
  1697. static int coda_remove(struct platform_device *pdev)
  1698. {
  1699. struct coda_dev *dev = platform_get_drvdata(pdev);
  1700. video_unregister_device(&dev->vfd[0]);
  1701. video_unregister_device(&dev->vfd[1]);
  1702. if (dev->m2m_dev)
  1703. v4l2_m2m_release(dev->m2m_dev);
  1704. pm_runtime_disable(&pdev->dev);
  1705. if (dev->alloc_ctx)
  1706. vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
  1707. v4l2_device_unregister(&dev->v4l2_dev);
  1708. destroy_workqueue(dev->workqueue);
  1709. if (dev->iram.vaddr)
  1710. gen_pool_free(dev->iram_pool, (unsigned long)dev->iram.vaddr,
  1711. dev->iram.size);
  1712. coda_free_aux_buf(dev, &dev->codebuf);
  1713. coda_free_aux_buf(dev, &dev->tempbuf);
  1714. coda_free_aux_buf(dev, &dev->workbuf);
  1715. debugfs_remove_recursive(dev->debugfs_root);
  1716. return 0;
  1717. }
  1718. #ifdef CONFIG_PM_RUNTIME
  1719. static int coda_runtime_resume(struct device *dev)
  1720. {
  1721. struct coda_dev *cdev = dev_get_drvdata(dev);
  1722. int ret = 0;
  1723. if (dev->pm_domain && cdev->codebuf.vaddr) {
  1724. ret = coda_hw_init(cdev);
  1725. if (ret)
  1726. v4l2_err(&cdev->v4l2_dev, "HW initialization failed\n");
  1727. }
  1728. return ret;
  1729. }
  1730. #endif
  1731. static const struct dev_pm_ops coda_pm_ops = {
  1732. SET_RUNTIME_PM_OPS(NULL, coda_runtime_resume, NULL)
  1733. };
  1734. static struct platform_driver coda_driver = {
  1735. .probe = coda_probe,
  1736. .remove = coda_remove,
  1737. .driver = {
  1738. .name = CODA_NAME,
  1739. .owner = THIS_MODULE,
  1740. .of_match_table = of_match_ptr(coda_dt_ids),
  1741. .pm = &coda_pm_ops,
  1742. },
  1743. .id_table = coda_platform_ids,
  1744. };
  1745. module_platform_driver(coda_driver);
  1746. MODULE_LICENSE("GPL");
  1747. MODULE_AUTHOR("Javier Martin <javier.martin@vista-silicon.com>");
  1748. MODULE_DESCRIPTION("Coda multi-standard codec V4L2 driver");