coda-common.c 61 KB

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