coda-common.c 60 KB

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