coda-common.c 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392
  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. struct v4l2_rect *r)
  488. {
  489. struct coda_q_data *q_data;
  490. struct vb2_queue *vq;
  491. vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
  492. if (!vq)
  493. return -EINVAL;
  494. q_data = get_q_data(ctx, f->type);
  495. if (!q_data)
  496. return -EINVAL;
  497. if (vb2_is_busy(vq)) {
  498. v4l2_err(&ctx->dev->v4l2_dev, "%s queue busy\n", __func__);
  499. return -EBUSY;
  500. }
  501. q_data->fourcc = f->fmt.pix.pixelformat;
  502. q_data->width = f->fmt.pix.width;
  503. q_data->height = f->fmt.pix.height;
  504. q_data->bytesperline = f->fmt.pix.bytesperline;
  505. q_data->sizeimage = f->fmt.pix.sizeimage;
  506. if (r) {
  507. q_data->rect = *r;
  508. } else {
  509. q_data->rect.left = 0;
  510. q_data->rect.top = 0;
  511. q_data->rect.width = f->fmt.pix.width;
  512. q_data->rect.height = f->fmt.pix.height;
  513. }
  514. switch (f->fmt.pix.pixelformat) {
  515. case V4L2_PIX_FMT_NV12:
  516. if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
  517. ctx->tiled_map_type = GDI_TILED_FRAME_MB_RASTER_MAP;
  518. if (!disable_tiling)
  519. break;
  520. }
  521. /* else fall through */
  522. case V4L2_PIX_FMT_YUV420:
  523. case V4L2_PIX_FMT_YVU420:
  524. ctx->tiled_map_type = GDI_LINEAR_FRAME_MAP;
  525. break;
  526. default:
  527. break;
  528. }
  529. v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
  530. "Setting format for type %d, wxh: %dx%d, fmt: %4.4s %c\n",
  531. f->type, q_data->width, q_data->height,
  532. (char *)&q_data->fourcc,
  533. (ctx->tiled_map_type == GDI_LINEAR_FRAME_MAP) ? 'L' : 'T');
  534. return 0;
  535. }
  536. static int coda_s_fmt_vid_cap(struct file *file, void *priv,
  537. struct v4l2_format *f)
  538. {
  539. struct coda_ctx *ctx = fh_to_ctx(priv);
  540. struct coda_q_data *q_data_src;
  541. struct v4l2_rect r;
  542. int ret;
  543. ret = coda_try_fmt_vid_cap(file, priv, f);
  544. if (ret)
  545. return ret;
  546. q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
  547. r.left = 0;
  548. r.top = 0;
  549. r.width = q_data_src->width;
  550. r.height = q_data_src->height;
  551. return coda_s_fmt(ctx, f, &r);
  552. }
  553. static int coda_s_fmt_vid_out(struct file *file, void *priv,
  554. struct v4l2_format *f)
  555. {
  556. struct coda_ctx *ctx = fh_to_ctx(priv);
  557. struct coda_q_data *q_data_src;
  558. struct v4l2_format f_cap;
  559. struct v4l2_rect r;
  560. int ret;
  561. ret = coda_try_fmt_vid_out(file, priv, f);
  562. if (ret)
  563. return ret;
  564. ret = coda_s_fmt(ctx, f, NULL);
  565. if (ret)
  566. return ret;
  567. ctx->colorspace = f->fmt.pix.colorspace;
  568. memset(&f_cap, 0, sizeof(f_cap));
  569. f_cap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  570. coda_g_fmt(file, priv, &f_cap);
  571. f_cap.fmt.pix.width = f->fmt.pix.width;
  572. f_cap.fmt.pix.height = f->fmt.pix.height;
  573. ret = coda_try_fmt_vid_cap(file, priv, &f_cap);
  574. if (ret)
  575. return ret;
  576. q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
  577. r.left = 0;
  578. r.top = 0;
  579. r.width = q_data_src->width;
  580. r.height = q_data_src->height;
  581. return coda_s_fmt(ctx, &f_cap, &r);
  582. }
  583. static int coda_reqbufs(struct file *file, void *priv,
  584. struct v4l2_requestbuffers *rb)
  585. {
  586. struct coda_ctx *ctx = fh_to_ctx(priv);
  587. int ret;
  588. ret = v4l2_m2m_reqbufs(file, ctx->fh.m2m_ctx, rb);
  589. if (ret)
  590. return ret;
  591. /*
  592. * Allow to allocate instance specific per-context buffers, such as
  593. * bitstream ringbuffer, slice buffer, work buffer, etc. if needed.
  594. */
  595. if (rb->type == V4L2_BUF_TYPE_VIDEO_OUTPUT && ctx->ops->reqbufs)
  596. return ctx->ops->reqbufs(ctx, rb);
  597. return 0;
  598. }
  599. static int coda_qbuf(struct file *file, void *priv,
  600. struct v4l2_buffer *buf)
  601. {
  602. struct coda_ctx *ctx = fh_to_ctx(priv);
  603. return v4l2_m2m_qbuf(file, ctx->fh.m2m_ctx, buf);
  604. }
  605. static bool coda_buf_is_end_of_stream(struct coda_ctx *ctx,
  606. struct vb2_v4l2_buffer *buf)
  607. {
  608. struct vb2_queue *src_vq;
  609. src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
  610. return ((ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG) &&
  611. (buf->sequence == (ctx->qsequence - 1)));
  612. }
  613. void coda_m2m_buf_done(struct coda_ctx *ctx, struct vb2_v4l2_buffer *buf,
  614. enum vb2_buffer_state state)
  615. {
  616. const struct v4l2_event eos_event = {
  617. .type = V4L2_EVENT_EOS
  618. };
  619. if (coda_buf_is_end_of_stream(ctx, buf)) {
  620. buf->flags |= V4L2_BUF_FLAG_LAST;
  621. v4l2_event_queue_fh(&ctx->fh, &eos_event);
  622. }
  623. v4l2_m2m_buf_done(buf, state);
  624. }
  625. static int coda_g_selection(struct file *file, void *fh,
  626. struct v4l2_selection *s)
  627. {
  628. struct coda_ctx *ctx = fh_to_ctx(fh);
  629. struct coda_q_data *q_data;
  630. struct v4l2_rect r, *rsel;
  631. q_data = get_q_data(ctx, s->type);
  632. if (!q_data)
  633. return -EINVAL;
  634. r.left = 0;
  635. r.top = 0;
  636. r.width = q_data->width;
  637. r.height = q_data->height;
  638. rsel = &q_data->rect;
  639. switch (s->target) {
  640. case V4L2_SEL_TGT_CROP_DEFAULT:
  641. case V4L2_SEL_TGT_CROP_BOUNDS:
  642. rsel = &r;
  643. /* fallthrough */
  644. case V4L2_SEL_TGT_CROP:
  645. if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
  646. return -EINVAL;
  647. break;
  648. case V4L2_SEL_TGT_COMPOSE_BOUNDS:
  649. case V4L2_SEL_TGT_COMPOSE_PADDED:
  650. rsel = &r;
  651. /* fallthrough */
  652. case V4L2_SEL_TGT_COMPOSE:
  653. case V4L2_SEL_TGT_COMPOSE_DEFAULT:
  654. if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  655. return -EINVAL;
  656. break;
  657. default:
  658. return -EINVAL;
  659. }
  660. s->r = *rsel;
  661. return 0;
  662. }
  663. static int coda_try_decoder_cmd(struct file *file, void *fh,
  664. struct v4l2_decoder_cmd *dc)
  665. {
  666. if (dc->cmd != V4L2_DEC_CMD_STOP)
  667. return -EINVAL;
  668. if (dc->flags & V4L2_DEC_CMD_STOP_TO_BLACK)
  669. return -EINVAL;
  670. if (!(dc->flags & V4L2_DEC_CMD_STOP_IMMEDIATELY) && (dc->stop.pts != 0))
  671. return -EINVAL;
  672. return 0;
  673. }
  674. static int coda_decoder_cmd(struct file *file, void *fh,
  675. struct v4l2_decoder_cmd *dc)
  676. {
  677. struct coda_ctx *ctx = fh_to_ctx(fh);
  678. int ret;
  679. ret = coda_try_decoder_cmd(file, fh, dc);
  680. if (ret < 0)
  681. return ret;
  682. /* Ignore decoder stop command silently in encoder context */
  683. if (ctx->inst_type != CODA_INST_DECODER)
  684. return 0;
  685. /* Set the stream-end flag on this context */
  686. coda_bit_stream_end_flag(ctx);
  687. ctx->hold = false;
  688. v4l2_m2m_try_schedule(ctx->fh.m2m_ctx);
  689. return 0;
  690. }
  691. static int coda_g_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
  692. {
  693. struct coda_ctx *ctx = fh_to_ctx(fh);
  694. struct v4l2_fract *tpf;
  695. if (a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
  696. return -EINVAL;
  697. a->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
  698. tpf = &a->parm.output.timeperframe;
  699. tpf->denominator = ctx->params.framerate & CODA_FRATE_RES_MASK;
  700. tpf->numerator = 1 + (ctx->params.framerate >>
  701. CODA_FRATE_DIV_OFFSET);
  702. return 0;
  703. }
  704. /*
  705. * Approximate timeperframe v4l2_fract with values that can be written
  706. * into the 16-bit CODA_FRATE_DIV and CODA_FRATE_RES fields.
  707. */
  708. static void coda_approximate_timeperframe(struct v4l2_fract *timeperframe)
  709. {
  710. struct v4l2_fract s = *timeperframe;
  711. struct v4l2_fract f0;
  712. struct v4l2_fract f1 = { 1, 0 };
  713. struct v4l2_fract f2 = { 0, 1 };
  714. unsigned int i, div, s_denominator;
  715. /* Lower bound is 1/65535 */
  716. if (s.numerator == 0 || s.denominator / s.numerator > 65535) {
  717. timeperframe->numerator = 1;
  718. timeperframe->denominator = 65535;
  719. return;
  720. }
  721. /* Upper bound is 65536/1, map everything above to infinity */
  722. if (s.denominator == 0 || s.numerator / s.denominator > 65536) {
  723. timeperframe->numerator = 1;
  724. timeperframe->denominator = 0;
  725. return;
  726. }
  727. /* Reduce fraction to lowest terms */
  728. div = gcd(s.numerator, s.denominator);
  729. if (div > 1) {
  730. s.numerator /= div;
  731. s.denominator /= div;
  732. }
  733. if (s.numerator <= 65536 && s.denominator < 65536) {
  734. *timeperframe = s;
  735. return;
  736. }
  737. /* Find successive convergents from continued fraction expansion */
  738. while (f2.numerator <= 65536 && f2.denominator < 65536) {
  739. f0 = f1;
  740. f1 = f2;
  741. /* Stop when f2 exactly equals timeperframe */
  742. if (s.numerator == 0)
  743. break;
  744. i = s.denominator / s.numerator;
  745. f2.numerator = f0.numerator + i * f1.numerator;
  746. f2.denominator = f0.denominator + i * f2.denominator;
  747. s_denominator = s.numerator;
  748. s.numerator = s.denominator % s.numerator;
  749. s.denominator = s_denominator;
  750. }
  751. *timeperframe = f1;
  752. }
  753. static uint32_t coda_timeperframe_to_frate(struct v4l2_fract *timeperframe)
  754. {
  755. return ((timeperframe->numerator - 1) << CODA_FRATE_DIV_OFFSET) |
  756. timeperframe->denominator;
  757. }
  758. static int coda_s_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
  759. {
  760. struct coda_ctx *ctx = fh_to_ctx(fh);
  761. struct v4l2_fract *tpf;
  762. if (a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
  763. return -EINVAL;
  764. tpf = &a->parm.output.timeperframe;
  765. coda_approximate_timeperframe(tpf);
  766. ctx->params.framerate = coda_timeperframe_to_frate(tpf);
  767. return 0;
  768. }
  769. static int coda_subscribe_event(struct v4l2_fh *fh,
  770. const struct v4l2_event_subscription *sub)
  771. {
  772. switch (sub->type) {
  773. case V4L2_EVENT_EOS:
  774. return v4l2_event_subscribe(fh, sub, 0, NULL);
  775. default:
  776. return v4l2_ctrl_subscribe_event(fh, sub);
  777. }
  778. }
  779. static const struct v4l2_ioctl_ops coda_ioctl_ops = {
  780. .vidioc_querycap = coda_querycap,
  781. .vidioc_enum_fmt_vid_cap = coda_enum_fmt,
  782. .vidioc_g_fmt_vid_cap = coda_g_fmt,
  783. .vidioc_try_fmt_vid_cap = coda_try_fmt_vid_cap,
  784. .vidioc_s_fmt_vid_cap = coda_s_fmt_vid_cap,
  785. .vidioc_enum_fmt_vid_out = coda_enum_fmt,
  786. .vidioc_g_fmt_vid_out = coda_g_fmt,
  787. .vidioc_try_fmt_vid_out = coda_try_fmt_vid_out,
  788. .vidioc_s_fmt_vid_out = coda_s_fmt_vid_out,
  789. .vidioc_reqbufs = coda_reqbufs,
  790. .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
  791. .vidioc_qbuf = coda_qbuf,
  792. .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
  793. .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
  794. .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
  795. .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
  796. .vidioc_streamon = v4l2_m2m_ioctl_streamon,
  797. .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
  798. .vidioc_g_selection = coda_g_selection,
  799. .vidioc_try_decoder_cmd = coda_try_decoder_cmd,
  800. .vidioc_decoder_cmd = coda_decoder_cmd,
  801. .vidioc_g_parm = coda_g_parm,
  802. .vidioc_s_parm = coda_s_parm,
  803. .vidioc_subscribe_event = coda_subscribe_event,
  804. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  805. };
  806. /*
  807. * Mem-to-mem operations.
  808. */
  809. static void coda_device_run(void *m2m_priv)
  810. {
  811. struct coda_ctx *ctx = m2m_priv;
  812. struct coda_dev *dev = ctx->dev;
  813. queue_work(dev->workqueue, &ctx->pic_run_work);
  814. }
  815. static void coda_pic_run_work(struct work_struct *work)
  816. {
  817. struct coda_ctx *ctx = container_of(work, struct coda_ctx, pic_run_work);
  818. struct coda_dev *dev = ctx->dev;
  819. int ret;
  820. mutex_lock(&ctx->buffer_mutex);
  821. mutex_lock(&dev->coda_mutex);
  822. ret = ctx->ops->prepare_run(ctx);
  823. if (ret < 0 && ctx->inst_type == CODA_INST_DECODER) {
  824. mutex_unlock(&dev->coda_mutex);
  825. mutex_unlock(&ctx->buffer_mutex);
  826. /* job_finish scheduled by prepare_decode */
  827. return;
  828. }
  829. if (!wait_for_completion_timeout(&ctx->completion,
  830. msecs_to_jiffies(1000))) {
  831. dev_err(&dev->plat_dev->dev, "CODA PIC_RUN timeout\n");
  832. ctx->hold = true;
  833. coda_hw_reset(ctx);
  834. } else if (!ctx->aborting) {
  835. ctx->ops->finish_run(ctx);
  836. }
  837. if ((ctx->aborting || (!ctx->streamon_cap && !ctx->streamon_out)) &&
  838. ctx->ops->seq_end_work)
  839. queue_work(dev->workqueue, &ctx->seq_end_work);
  840. mutex_unlock(&dev->coda_mutex);
  841. mutex_unlock(&ctx->buffer_mutex);
  842. v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx);
  843. }
  844. static int coda_job_ready(void *m2m_priv)
  845. {
  846. struct coda_ctx *ctx = m2m_priv;
  847. int src_bufs = v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx);
  848. /*
  849. * For both 'P' and 'key' frame cases 1 picture
  850. * and 1 frame are needed. In the decoder case,
  851. * the compressed frame can be in the bitstream.
  852. */
  853. if (!src_bufs && ctx->inst_type != CODA_INST_DECODER) {
  854. v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
  855. "not ready: not enough video buffers.\n");
  856. return 0;
  857. }
  858. if (!v4l2_m2m_num_dst_bufs_ready(ctx->fh.m2m_ctx)) {
  859. v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
  860. "not ready: not enough video capture buffers.\n");
  861. return 0;
  862. }
  863. if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit) {
  864. bool stream_end = ctx->bit_stream_param &
  865. CODA_BIT_STREAM_END_FLAG;
  866. int num_metas = ctx->num_metas;
  867. if (ctx->hold && !src_bufs) {
  868. v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
  869. "%d: not ready: on hold for more buffers.\n",
  870. ctx->idx);
  871. return 0;
  872. }
  873. if (!stream_end && (num_metas + src_bufs) < 2) {
  874. v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
  875. "%d: not ready: need 2 buffers available (%d, %d)\n",
  876. ctx->idx, num_metas, src_bufs);
  877. return 0;
  878. }
  879. if (!src_bufs && !stream_end &&
  880. (coda_get_bitstream_payload(ctx) < 512)) {
  881. v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
  882. "%d: not ready: not enough bitstream data (%d).\n",
  883. ctx->idx, coda_get_bitstream_payload(ctx));
  884. return 0;
  885. }
  886. }
  887. if (ctx->aborting) {
  888. v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
  889. "not ready: aborting\n");
  890. return 0;
  891. }
  892. v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
  893. "job ready\n");
  894. return 1;
  895. }
  896. static void coda_job_abort(void *priv)
  897. {
  898. struct coda_ctx *ctx = priv;
  899. ctx->aborting = 1;
  900. v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
  901. "Aborting task\n");
  902. }
  903. static void coda_lock(void *m2m_priv)
  904. {
  905. struct coda_ctx *ctx = m2m_priv;
  906. struct coda_dev *pcdev = ctx->dev;
  907. mutex_lock(&pcdev->dev_mutex);
  908. }
  909. static void coda_unlock(void *m2m_priv)
  910. {
  911. struct coda_ctx *ctx = m2m_priv;
  912. struct coda_dev *pcdev = ctx->dev;
  913. mutex_unlock(&pcdev->dev_mutex);
  914. }
  915. static const struct v4l2_m2m_ops coda_m2m_ops = {
  916. .device_run = coda_device_run,
  917. .job_ready = coda_job_ready,
  918. .job_abort = coda_job_abort,
  919. .lock = coda_lock,
  920. .unlock = coda_unlock,
  921. };
  922. static void set_default_params(struct coda_ctx *ctx)
  923. {
  924. unsigned int max_w, max_h, usize, csize;
  925. ctx->codec = coda_find_codec(ctx->dev, ctx->cvd->src_formats[0],
  926. ctx->cvd->dst_formats[0]);
  927. max_w = min(ctx->codec->max_w, 1920U);
  928. max_h = min(ctx->codec->max_h, 1088U);
  929. usize = max_w * max_h * 3 / 2;
  930. csize = coda_estimate_sizeimage(ctx, usize, max_w, max_h);
  931. ctx->params.codec_mode = ctx->codec->mode;
  932. ctx->colorspace = V4L2_COLORSPACE_REC709;
  933. ctx->params.framerate = 30;
  934. /* Default formats for output and input queues */
  935. ctx->q_data[V4L2_M2M_SRC].fourcc = ctx->cvd->src_formats[0];
  936. ctx->q_data[V4L2_M2M_DST].fourcc = ctx->cvd->dst_formats[0];
  937. ctx->q_data[V4L2_M2M_SRC].width = max_w;
  938. ctx->q_data[V4L2_M2M_SRC].height = max_h;
  939. ctx->q_data[V4L2_M2M_DST].width = max_w;
  940. ctx->q_data[V4L2_M2M_DST].height = max_h;
  941. if (ctx->codec->src_fourcc == V4L2_PIX_FMT_YUV420) {
  942. ctx->q_data[V4L2_M2M_SRC].bytesperline = max_w;
  943. ctx->q_data[V4L2_M2M_SRC].sizeimage = usize;
  944. ctx->q_data[V4L2_M2M_DST].bytesperline = 0;
  945. ctx->q_data[V4L2_M2M_DST].sizeimage = csize;
  946. } else {
  947. ctx->q_data[V4L2_M2M_SRC].bytesperline = 0;
  948. ctx->q_data[V4L2_M2M_SRC].sizeimage = csize;
  949. ctx->q_data[V4L2_M2M_DST].bytesperline = max_w;
  950. ctx->q_data[V4L2_M2M_DST].sizeimage = usize;
  951. }
  952. ctx->q_data[V4L2_M2M_SRC].rect.width = max_w;
  953. ctx->q_data[V4L2_M2M_SRC].rect.height = max_h;
  954. ctx->q_data[V4L2_M2M_DST].rect.width = max_w;
  955. ctx->q_data[V4L2_M2M_DST].rect.height = max_h;
  956. /*
  957. * Since the RBC2AXI logic only supports a single chroma plane,
  958. * macroblock tiling only works for to NV12 pixel format.
  959. */
  960. ctx->tiled_map_type = GDI_LINEAR_FRAME_MAP;
  961. }
  962. /*
  963. * Queue operations
  964. */
  965. static int coda_queue_setup(struct vb2_queue *vq,
  966. unsigned int *nbuffers, unsigned int *nplanes,
  967. unsigned int sizes[], struct device *alloc_devs[])
  968. {
  969. struct coda_ctx *ctx = vb2_get_drv_priv(vq);
  970. struct coda_q_data *q_data;
  971. unsigned int size;
  972. q_data = get_q_data(ctx, vq->type);
  973. size = q_data->sizeimage;
  974. *nplanes = 1;
  975. sizes[0] = size;
  976. v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
  977. "get %d buffer(s) of size %d each.\n", *nbuffers, size);
  978. return 0;
  979. }
  980. static int coda_buf_prepare(struct vb2_buffer *vb)
  981. {
  982. struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  983. struct coda_q_data *q_data;
  984. q_data = get_q_data(ctx, vb->vb2_queue->type);
  985. if (vb2_plane_size(vb, 0) < q_data->sizeimage) {
  986. v4l2_warn(&ctx->dev->v4l2_dev,
  987. "%s data will not fit into plane (%lu < %lu)\n",
  988. __func__, vb2_plane_size(vb, 0),
  989. (long)q_data->sizeimage);
  990. return -EINVAL;
  991. }
  992. return 0;
  993. }
  994. static void coda_buf_queue(struct vb2_buffer *vb)
  995. {
  996. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  997. struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  998. struct vb2_queue *vq = vb->vb2_queue;
  999. struct coda_q_data *q_data;
  1000. q_data = get_q_data(ctx, vb->vb2_queue->type);
  1001. /*
  1002. * In the decoder case, immediately try to copy the buffer into the
  1003. * bitstream ringbuffer and mark it as ready to be dequeued.
  1004. */
  1005. if (ctx->bitstream.size && vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
  1006. /*
  1007. * For backwards compatibility, queuing an empty buffer marks
  1008. * the stream end
  1009. */
  1010. if (vb2_get_plane_payload(vb, 0) == 0)
  1011. coda_bit_stream_end_flag(ctx);
  1012. mutex_lock(&ctx->bitstream_mutex);
  1013. v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
  1014. if (vb2_is_streaming(vb->vb2_queue))
  1015. coda_fill_bitstream(ctx, true);
  1016. mutex_unlock(&ctx->bitstream_mutex);
  1017. } else {
  1018. v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
  1019. }
  1020. }
  1021. int coda_alloc_aux_buf(struct coda_dev *dev, struct coda_aux_buf *buf,
  1022. size_t size, const char *name, struct dentry *parent)
  1023. {
  1024. buf->vaddr = dma_alloc_coherent(&dev->plat_dev->dev, size, &buf->paddr,
  1025. GFP_KERNEL);
  1026. if (!buf->vaddr) {
  1027. v4l2_err(&dev->v4l2_dev,
  1028. "Failed to allocate %s buffer of size %u\n",
  1029. name, size);
  1030. return -ENOMEM;
  1031. }
  1032. buf->size = size;
  1033. if (name && parent) {
  1034. buf->blob.data = buf->vaddr;
  1035. buf->blob.size = size;
  1036. buf->dentry = debugfs_create_blob(name, 0644, parent,
  1037. &buf->blob);
  1038. if (!buf->dentry)
  1039. dev_warn(&dev->plat_dev->dev,
  1040. "failed to create debugfs entry %s\n", name);
  1041. }
  1042. return 0;
  1043. }
  1044. void coda_free_aux_buf(struct coda_dev *dev,
  1045. struct coda_aux_buf *buf)
  1046. {
  1047. if (buf->vaddr) {
  1048. dma_free_coherent(&dev->plat_dev->dev, buf->size,
  1049. buf->vaddr, buf->paddr);
  1050. buf->vaddr = NULL;
  1051. buf->size = 0;
  1052. debugfs_remove(buf->dentry);
  1053. buf->dentry = NULL;
  1054. }
  1055. }
  1056. static int coda_start_streaming(struct vb2_queue *q, unsigned int count)
  1057. {
  1058. struct coda_ctx *ctx = vb2_get_drv_priv(q);
  1059. struct v4l2_device *v4l2_dev = &ctx->dev->v4l2_dev;
  1060. struct coda_q_data *q_data_src, *q_data_dst;
  1061. struct vb2_v4l2_buffer *buf;
  1062. int ret = 0;
  1063. if (count < 1)
  1064. return -EINVAL;
  1065. q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
  1066. if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
  1067. if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit) {
  1068. /* copy the buffers that were queued before streamon */
  1069. mutex_lock(&ctx->bitstream_mutex);
  1070. coda_fill_bitstream(ctx, false);
  1071. mutex_unlock(&ctx->bitstream_mutex);
  1072. if (coda_get_bitstream_payload(ctx) < 512) {
  1073. ret = -EINVAL;
  1074. goto err;
  1075. }
  1076. }
  1077. ctx->streamon_out = 1;
  1078. } else {
  1079. ctx->streamon_cap = 1;
  1080. }
  1081. /* Don't start the coda unless both queues are on */
  1082. if (!(ctx->streamon_out & ctx->streamon_cap))
  1083. return 0;
  1084. q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
  1085. if ((q_data_src->width != q_data_dst->width &&
  1086. round_up(q_data_src->width, 16) != q_data_dst->width) ||
  1087. (q_data_src->height != q_data_dst->height &&
  1088. round_up(q_data_src->height, 16) != q_data_dst->height)) {
  1089. v4l2_err(v4l2_dev, "can't convert %dx%d to %dx%d\n",
  1090. q_data_src->width, q_data_src->height,
  1091. q_data_dst->width, q_data_dst->height);
  1092. ret = -EINVAL;
  1093. goto err;
  1094. }
  1095. /* Allow BIT decoder device_run with no new buffers queued */
  1096. if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit)
  1097. v4l2_m2m_set_src_buffered(ctx->fh.m2m_ctx, true);
  1098. ctx->gopcounter = ctx->params.gop_size - 1;
  1099. ctx->codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
  1100. q_data_dst->fourcc);
  1101. if (!ctx->codec) {
  1102. v4l2_err(v4l2_dev, "couldn't tell instance type.\n");
  1103. ret = -EINVAL;
  1104. goto err;
  1105. }
  1106. if (q_data_dst->fourcc == V4L2_PIX_FMT_JPEG)
  1107. ctx->params.gop_size = 1;
  1108. ctx->gopcounter = ctx->params.gop_size - 1;
  1109. ret = ctx->ops->start_streaming(ctx);
  1110. if (ctx->inst_type == CODA_INST_DECODER) {
  1111. if (ret == -EAGAIN)
  1112. return 0;
  1113. else if (ret < 0)
  1114. goto err;
  1115. }
  1116. return ret;
  1117. err:
  1118. if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
  1119. while ((buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx)))
  1120. v4l2_m2m_buf_done(buf, VB2_BUF_STATE_QUEUED);
  1121. } else {
  1122. while ((buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx)))
  1123. v4l2_m2m_buf_done(buf, VB2_BUF_STATE_QUEUED);
  1124. }
  1125. return ret;
  1126. }
  1127. static void coda_stop_streaming(struct vb2_queue *q)
  1128. {
  1129. struct coda_ctx *ctx = vb2_get_drv_priv(q);
  1130. struct coda_dev *dev = ctx->dev;
  1131. struct vb2_v4l2_buffer *buf;
  1132. unsigned long flags;
  1133. bool stop;
  1134. stop = ctx->streamon_out && ctx->streamon_cap;
  1135. if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
  1136. v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
  1137. "%s: output\n", __func__);
  1138. ctx->streamon_out = 0;
  1139. coda_bit_stream_end_flag(ctx);
  1140. ctx->qsequence = 0;
  1141. while ((buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx)))
  1142. v4l2_m2m_buf_done(buf, VB2_BUF_STATE_ERROR);
  1143. } else {
  1144. v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
  1145. "%s: capture\n", __func__);
  1146. ctx->streamon_cap = 0;
  1147. ctx->osequence = 0;
  1148. ctx->sequence_offset = 0;
  1149. while ((buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx)))
  1150. v4l2_m2m_buf_done(buf, VB2_BUF_STATE_ERROR);
  1151. }
  1152. if (stop) {
  1153. struct coda_buffer_meta *meta;
  1154. if (ctx->ops->seq_end_work) {
  1155. queue_work(dev->workqueue, &ctx->seq_end_work);
  1156. flush_work(&ctx->seq_end_work);
  1157. }
  1158. spin_lock_irqsave(&ctx->buffer_meta_lock, flags);
  1159. while (!list_empty(&ctx->buffer_meta_list)) {
  1160. meta = list_first_entry(&ctx->buffer_meta_list,
  1161. struct coda_buffer_meta, list);
  1162. list_del(&meta->list);
  1163. kfree(meta);
  1164. }
  1165. ctx->num_metas = 0;
  1166. spin_unlock_irqrestore(&ctx->buffer_meta_lock, flags);
  1167. kfifo_init(&ctx->bitstream_fifo,
  1168. ctx->bitstream.vaddr, ctx->bitstream.size);
  1169. ctx->runcounter = 0;
  1170. ctx->aborting = 0;
  1171. }
  1172. if (!ctx->streamon_out && !ctx->streamon_cap)
  1173. ctx->bit_stream_param &= ~CODA_BIT_STREAM_END_FLAG;
  1174. }
  1175. static const struct vb2_ops coda_qops = {
  1176. .queue_setup = coda_queue_setup,
  1177. .buf_prepare = coda_buf_prepare,
  1178. .buf_queue = coda_buf_queue,
  1179. .start_streaming = coda_start_streaming,
  1180. .stop_streaming = coda_stop_streaming,
  1181. .wait_prepare = vb2_ops_wait_prepare,
  1182. .wait_finish = vb2_ops_wait_finish,
  1183. };
  1184. static int coda_s_ctrl(struct v4l2_ctrl *ctrl)
  1185. {
  1186. struct coda_ctx *ctx =
  1187. container_of(ctrl->handler, struct coda_ctx, ctrls);
  1188. v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
  1189. "s_ctrl: id = %d, val = %d\n", ctrl->id, ctrl->val);
  1190. switch (ctrl->id) {
  1191. case V4L2_CID_HFLIP:
  1192. if (ctrl->val)
  1193. ctx->params.rot_mode |= CODA_MIR_HOR;
  1194. else
  1195. ctx->params.rot_mode &= ~CODA_MIR_HOR;
  1196. break;
  1197. case V4L2_CID_VFLIP:
  1198. if (ctrl->val)
  1199. ctx->params.rot_mode |= CODA_MIR_VER;
  1200. else
  1201. ctx->params.rot_mode &= ~CODA_MIR_VER;
  1202. break;
  1203. case V4L2_CID_MPEG_VIDEO_BITRATE:
  1204. ctx->params.bitrate = ctrl->val / 1000;
  1205. break;
  1206. case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
  1207. ctx->params.gop_size = ctrl->val;
  1208. break;
  1209. case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP:
  1210. ctx->params.h264_intra_qp = ctrl->val;
  1211. break;
  1212. case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP:
  1213. ctx->params.h264_inter_qp = ctrl->val;
  1214. break;
  1215. case V4L2_CID_MPEG_VIDEO_H264_MIN_QP:
  1216. ctx->params.h264_min_qp = ctrl->val;
  1217. break;
  1218. case V4L2_CID_MPEG_VIDEO_H264_MAX_QP:
  1219. ctx->params.h264_max_qp = ctrl->val;
  1220. break;
  1221. case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA:
  1222. ctx->params.h264_deblk_alpha = ctrl->val;
  1223. break;
  1224. case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA:
  1225. ctx->params.h264_deblk_beta = ctrl->val;
  1226. break;
  1227. case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE:
  1228. ctx->params.h264_deblk_enabled = (ctrl->val ==
  1229. V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED);
  1230. break;
  1231. case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP:
  1232. ctx->params.mpeg4_intra_qp = ctrl->val;
  1233. break;
  1234. case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP:
  1235. ctx->params.mpeg4_inter_qp = ctrl->val;
  1236. break;
  1237. case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
  1238. ctx->params.slice_mode = ctrl->val;
  1239. break;
  1240. case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB:
  1241. ctx->params.slice_max_mb = ctrl->val;
  1242. break;
  1243. case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES:
  1244. ctx->params.slice_max_bits = ctrl->val * 8;
  1245. break;
  1246. case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
  1247. break;
  1248. case V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB:
  1249. ctx->params.intra_refresh = ctrl->val;
  1250. break;
  1251. case V4L2_CID_JPEG_COMPRESSION_QUALITY:
  1252. coda_set_jpeg_compression_quality(ctx, ctrl->val);
  1253. break;
  1254. case V4L2_CID_JPEG_RESTART_INTERVAL:
  1255. ctx->params.jpeg_restart_interval = ctrl->val;
  1256. break;
  1257. case V4L2_CID_MPEG_VIDEO_VBV_DELAY:
  1258. ctx->params.vbv_delay = ctrl->val;
  1259. break;
  1260. case V4L2_CID_MPEG_VIDEO_VBV_SIZE:
  1261. ctx->params.vbv_size = min(ctrl->val * 8192, 0x7fffffff);
  1262. break;
  1263. default:
  1264. v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
  1265. "Invalid control, id=%d, val=%d\n",
  1266. ctrl->id, ctrl->val);
  1267. return -EINVAL;
  1268. }
  1269. return 0;
  1270. }
  1271. static const struct v4l2_ctrl_ops coda_ctrl_ops = {
  1272. .s_ctrl = coda_s_ctrl,
  1273. };
  1274. static void coda_encode_ctrls(struct coda_ctx *ctx)
  1275. {
  1276. v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
  1277. V4L2_CID_MPEG_VIDEO_BITRATE, 0, 32767000, 1000, 0);
  1278. v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
  1279. V4L2_CID_MPEG_VIDEO_GOP_SIZE, 1, 60, 1, 16);
  1280. v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
  1281. V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP, 0, 51, 1, 25);
  1282. v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
  1283. V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP, 0, 51, 1, 25);
  1284. if (ctx->dev->devtype->product != CODA_960) {
  1285. v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
  1286. V4L2_CID_MPEG_VIDEO_H264_MIN_QP, 0, 51, 1, 12);
  1287. }
  1288. v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
  1289. V4L2_CID_MPEG_VIDEO_H264_MAX_QP, 0, 51, 1, 51);
  1290. v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
  1291. V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA, 0, 15, 1, 0);
  1292. v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
  1293. V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA, 0, 15, 1, 0);
  1294. v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
  1295. V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE,
  1296. V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_DISABLED, 0x0,
  1297. V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED);
  1298. v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
  1299. V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP, 1, 31, 1, 2);
  1300. v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
  1301. V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP, 1, 31, 1, 2);
  1302. v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
  1303. V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE,
  1304. V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES, 0x0,
  1305. V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE);
  1306. v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
  1307. V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB, 1, 0x3fffffff, 1, 1);
  1308. v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
  1309. V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES, 1, 0x3fffffff, 1,
  1310. 500);
  1311. v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
  1312. V4L2_CID_MPEG_VIDEO_HEADER_MODE,
  1313. V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME,
  1314. (1 << V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE),
  1315. V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME);
  1316. v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
  1317. V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB, 0,
  1318. 1920 * 1088 / 256, 1, 0);
  1319. v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
  1320. V4L2_CID_MPEG_VIDEO_VBV_DELAY, 0, 0x7fff, 1, 0);
  1321. /*
  1322. * The maximum VBV size value is 0x7fffffff bits,
  1323. * one bit less than 262144 KiB
  1324. */
  1325. v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
  1326. V4L2_CID_MPEG_VIDEO_VBV_SIZE, 0, 262144, 1, 0);
  1327. }
  1328. static void coda_jpeg_encode_ctrls(struct coda_ctx *ctx)
  1329. {
  1330. v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
  1331. V4L2_CID_JPEG_COMPRESSION_QUALITY, 5, 100, 1, 50);
  1332. v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
  1333. V4L2_CID_JPEG_RESTART_INTERVAL, 0, 100, 1, 0);
  1334. }
  1335. static int coda_ctrls_setup(struct coda_ctx *ctx)
  1336. {
  1337. v4l2_ctrl_handler_init(&ctx->ctrls, 2);
  1338. v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
  1339. V4L2_CID_HFLIP, 0, 1, 1, 0);
  1340. v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
  1341. V4L2_CID_VFLIP, 0, 1, 1, 0);
  1342. if (ctx->inst_type == CODA_INST_ENCODER) {
  1343. if (ctx->cvd->dst_formats[0] == V4L2_PIX_FMT_JPEG)
  1344. coda_jpeg_encode_ctrls(ctx);
  1345. else
  1346. coda_encode_ctrls(ctx);
  1347. }
  1348. if (ctx->ctrls.error) {
  1349. v4l2_err(&ctx->dev->v4l2_dev,
  1350. "control initialization error (%d)",
  1351. ctx->ctrls.error);
  1352. return -EINVAL;
  1353. }
  1354. return v4l2_ctrl_handler_setup(&ctx->ctrls);
  1355. }
  1356. static int coda_queue_init(struct coda_ctx *ctx, struct vb2_queue *vq)
  1357. {
  1358. vq->drv_priv = ctx;
  1359. vq->ops = &coda_qops;
  1360. vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
  1361. vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
  1362. vq->lock = &ctx->dev->dev_mutex;
  1363. /* One way to indicate end-of-stream for coda is to set the
  1364. * bytesused == 0. However by default videobuf2 handles bytesused
  1365. * equal to 0 as a special case and changes its value to the size
  1366. * of the buffer. Set the allow_zero_bytesused flag, so
  1367. * that videobuf2 will keep the value of bytesused intact.
  1368. */
  1369. vq->allow_zero_bytesused = 1;
  1370. vq->dev = &ctx->dev->plat_dev->dev;
  1371. return vb2_queue_init(vq);
  1372. }
  1373. int coda_encoder_queue_init(void *priv, struct vb2_queue *src_vq,
  1374. struct vb2_queue *dst_vq)
  1375. {
  1376. int ret;
  1377. src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  1378. src_vq->io_modes = VB2_DMABUF | VB2_MMAP;
  1379. src_vq->mem_ops = &vb2_dma_contig_memops;
  1380. ret = coda_queue_init(priv, src_vq);
  1381. if (ret)
  1382. return ret;
  1383. dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1384. dst_vq->io_modes = VB2_DMABUF | VB2_MMAP;
  1385. dst_vq->mem_ops = &vb2_dma_contig_memops;
  1386. return coda_queue_init(priv, dst_vq);
  1387. }
  1388. int coda_decoder_queue_init(void *priv, struct vb2_queue *src_vq,
  1389. struct vb2_queue *dst_vq)
  1390. {
  1391. int ret;
  1392. src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  1393. src_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
  1394. src_vq->mem_ops = &vb2_vmalloc_memops;
  1395. ret = coda_queue_init(priv, src_vq);
  1396. if (ret)
  1397. return ret;
  1398. dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1399. dst_vq->io_modes = VB2_DMABUF | VB2_MMAP;
  1400. dst_vq->mem_ops = &vb2_dma_contig_memops;
  1401. return coda_queue_init(priv, dst_vq);
  1402. }
  1403. static int coda_next_free_instance(struct coda_dev *dev)
  1404. {
  1405. int idx = ffz(dev->instance_mask);
  1406. if ((idx < 0) ||
  1407. (dev->devtype->product == CODA_DX6 && idx > CODADX6_MAX_INSTANCES))
  1408. return -EBUSY;
  1409. return idx;
  1410. }
  1411. /*
  1412. * File operations
  1413. */
  1414. static int coda_open(struct file *file)
  1415. {
  1416. struct video_device *vdev = video_devdata(file);
  1417. struct coda_dev *dev = video_get_drvdata(vdev);
  1418. struct coda_ctx *ctx = NULL;
  1419. char *name;
  1420. int ret;
  1421. int idx;
  1422. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  1423. if (!ctx)
  1424. return -ENOMEM;
  1425. idx = coda_next_free_instance(dev);
  1426. if (idx < 0) {
  1427. ret = idx;
  1428. goto err_coda_max;
  1429. }
  1430. set_bit(idx, &dev->instance_mask);
  1431. name = kasprintf(GFP_KERNEL, "context%d", idx);
  1432. if (!name) {
  1433. ret = -ENOMEM;
  1434. goto err_coda_name_init;
  1435. }
  1436. ctx->debugfs_entry = debugfs_create_dir(name, dev->debugfs_root);
  1437. kfree(name);
  1438. ctx->cvd = to_coda_video_device(vdev);
  1439. ctx->inst_type = ctx->cvd->type;
  1440. ctx->ops = ctx->cvd->ops;
  1441. ctx->use_bit = !ctx->cvd->direct;
  1442. init_completion(&ctx->completion);
  1443. INIT_WORK(&ctx->pic_run_work, coda_pic_run_work);
  1444. if (ctx->ops->seq_end_work)
  1445. INIT_WORK(&ctx->seq_end_work, ctx->ops->seq_end_work);
  1446. v4l2_fh_init(&ctx->fh, video_devdata(file));
  1447. file->private_data = &ctx->fh;
  1448. v4l2_fh_add(&ctx->fh);
  1449. ctx->dev = dev;
  1450. ctx->idx = idx;
  1451. switch (dev->devtype->product) {
  1452. case CODA_960:
  1453. ctx->frame_mem_ctrl = 1 << 12;
  1454. /* fallthrough */
  1455. case CODA_7541:
  1456. ctx->reg_idx = 0;
  1457. break;
  1458. default:
  1459. ctx->reg_idx = idx;
  1460. }
  1461. /* Power up and upload firmware if necessary */
  1462. ret = pm_runtime_get_sync(&dev->plat_dev->dev);
  1463. if (ret < 0) {
  1464. v4l2_err(&dev->v4l2_dev, "failed to power up: %d\n", ret);
  1465. goto err_pm_get;
  1466. }
  1467. ret = clk_prepare_enable(dev->clk_per);
  1468. if (ret)
  1469. goto err_clk_per;
  1470. ret = clk_prepare_enable(dev->clk_ahb);
  1471. if (ret)
  1472. goto err_clk_ahb;
  1473. set_default_params(ctx);
  1474. ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx,
  1475. ctx->ops->queue_init);
  1476. if (IS_ERR(ctx->fh.m2m_ctx)) {
  1477. ret = PTR_ERR(ctx->fh.m2m_ctx);
  1478. v4l2_err(&dev->v4l2_dev, "%s return error (%d)\n",
  1479. __func__, ret);
  1480. goto err_ctx_init;
  1481. }
  1482. ret = coda_ctrls_setup(ctx);
  1483. if (ret) {
  1484. v4l2_err(&dev->v4l2_dev, "failed to setup coda controls\n");
  1485. goto err_ctrls_setup;
  1486. }
  1487. ctx->fh.ctrl_handler = &ctx->ctrls;
  1488. mutex_init(&ctx->bitstream_mutex);
  1489. mutex_init(&ctx->buffer_mutex);
  1490. INIT_LIST_HEAD(&ctx->buffer_meta_list);
  1491. spin_lock_init(&ctx->buffer_meta_lock);
  1492. coda_lock(ctx);
  1493. list_add(&ctx->list, &dev->instances);
  1494. coda_unlock(ctx);
  1495. v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Created instance %d (%p)\n",
  1496. ctx->idx, ctx);
  1497. return 0;
  1498. err_ctrls_setup:
  1499. v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
  1500. err_ctx_init:
  1501. clk_disable_unprepare(dev->clk_ahb);
  1502. err_clk_ahb:
  1503. clk_disable_unprepare(dev->clk_per);
  1504. err_clk_per:
  1505. pm_runtime_put_sync(&dev->plat_dev->dev);
  1506. err_pm_get:
  1507. v4l2_fh_del(&ctx->fh);
  1508. v4l2_fh_exit(&ctx->fh);
  1509. clear_bit(ctx->idx, &dev->instance_mask);
  1510. err_coda_name_init:
  1511. err_coda_max:
  1512. kfree(ctx);
  1513. return ret;
  1514. }
  1515. static int coda_release(struct file *file)
  1516. {
  1517. struct coda_dev *dev = video_drvdata(file);
  1518. struct coda_ctx *ctx = fh_to_ctx(file->private_data);
  1519. v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Releasing instance %p\n",
  1520. ctx);
  1521. if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit)
  1522. coda_bit_stream_end_flag(ctx);
  1523. /* If this instance is running, call .job_abort and wait for it to end */
  1524. v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
  1525. /* In case the instance was not running, we still need to call SEQ_END */
  1526. if (ctx->ops->seq_end_work) {
  1527. queue_work(dev->workqueue, &ctx->seq_end_work);
  1528. flush_work(&ctx->seq_end_work);
  1529. }
  1530. coda_lock(ctx);
  1531. list_del(&ctx->list);
  1532. coda_unlock(ctx);
  1533. if (ctx->dev->devtype->product == CODA_DX6)
  1534. coda_free_aux_buf(dev, &ctx->workbuf);
  1535. v4l2_ctrl_handler_free(&ctx->ctrls);
  1536. clk_disable_unprepare(dev->clk_ahb);
  1537. clk_disable_unprepare(dev->clk_per);
  1538. pm_runtime_put_sync(&dev->plat_dev->dev);
  1539. v4l2_fh_del(&ctx->fh);
  1540. v4l2_fh_exit(&ctx->fh);
  1541. clear_bit(ctx->idx, &dev->instance_mask);
  1542. if (ctx->ops->release)
  1543. ctx->ops->release(ctx);
  1544. debugfs_remove_recursive(ctx->debugfs_entry);
  1545. kfree(ctx);
  1546. return 0;
  1547. }
  1548. static const struct v4l2_file_operations coda_fops = {
  1549. .owner = THIS_MODULE,
  1550. .open = coda_open,
  1551. .release = coda_release,
  1552. .poll = v4l2_m2m_fop_poll,
  1553. .unlocked_ioctl = video_ioctl2,
  1554. .mmap = v4l2_m2m_fop_mmap,
  1555. };
  1556. static int coda_hw_init(struct coda_dev *dev)
  1557. {
  1558. u32 data;
  1559. u16 *p;
  1560. int i, ret;
  1561. ret = clk_prepare_enable(dev->clk_per);
  1562. if (ret)
  1563. goto err_clk_per;
  1564. ret = clk_prepare_enable(dev->clk_ahb);
  1565. if (ret)
  1566. goto err_clk_ahb;
  1567. if (dev->rstc)
  1568. reset_control_reset(dev->rstc);
  1569. /*
  1570. * Copy the first CODA_ISRAM_SIZE in the internal SRAM.
  1571. * The 16-bit chars in the code buffer are in memory access
  1572. * order, re-sort them to CODA order for register download.
  1573. * Data in this SRAM survives a reboot.
  1574. */
  1575. p = (u16 *)dev->codebuf.vaddr;
  1576. if (dev->devtype->product == CODA_DX6) {
  1577. for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
  1578. data = CODA_DOWN_ADDRESS_SET(i) |
  1579. CODA_DOWN_DATA_SET(p[i ^ 1]);
  1580. coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
  1581. }
  1582. } else {
  1583. for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
  1584. data = CODA_DOWN_ADDRESS_SET(i) |
  1585. CODA_DOWN_DATA_SET(p[round_down(i, 4) +
  1586. 3 - (i % 4)]);
  1587. coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
  1588. }
  1589. }
  1590. /* Clear registers */
  1591. for (i = 0; i < 64; i++)
  1592. coda_write(dev, 0, CODA_REG_BIT_CODE_BUF_ADDR + i * 4);
  1593. /* Tell the BIT where to find everything it needs */
  1594. if (dev->devtype->product == CODA_960 ||
  1595. dev->devtype->product == CODA_7541) {
  1596. coda_write(dev, dev->tempbuf.paddr,
  1597. CODA_REG_BIT_TEMP_BUF_ADDR);
  1598. coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
  1599. } else {
  1600. coda_write(dev, dev->workbuf.paddr,
  1601. CODA_REG_BIT_WORK_BUF_ADDR);
  1602. }
  1603. coda_write(dev, dev->codebuf.paddr,
  1604. CODA_REG_BIT_CODE_BUF_ADDR);
  1605. coda_write(dev, 0, CODA_REG_BIT_CODE_RUN);
  1606. /* Set default values */
  1607. switch (dev->devtype->product) {
  1608. case CODA_DX6:
  1609. coda_write(dev, CODADX6_STREAM_BUF_PIC_FLUSH,
  1610. CODA_REG_BIT_STREAM_CTRL);
  1611. break;
  1612. default:
  1613. coda_write(dev, CODA7_STREAM_BUF_PIC_FLUSH,
  1614. CODA_REG_BIT_STREAM_CTRL);
  1615. }
  1616. if (dev->devtype->product == CODA_960)
  1617. coda_write(dev, 1 << 12, CODA_REG_BIT_FRAME_MEM_CTRL);
  1618. else
  1619. coda_write(dev, 0, CODA_REG_BIT_FRAME_MEM_CTRL);
  1620. if (dev->devtype->product != CODA_DX6)
  1621. coda_write(dev, 0, CODA7_REG_BIT_AXI_SRAM_USE);
  1622. coda_write(dev, CODA_INT_INTERRUPT_ENABLE,
  1623. CODA_REG_BIT_INT_ENABLE);
  1624. /* Reset VPU and start processor */
  1625. data = coda_read(dev, CODA_REG_BIT_CODE_RESET);
  1626. data |= CODA_REG_RESET_ENABLE;
  1627. coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
  1628. udelay(10);
  1629. data &= ~CODA_REG_RESET_ENABLE;
  1630. coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
  1631. coda_write(dev, CODA_REG_RUN_ENABLE, CODA_REG_BIT_CODE_RUN);
  1632. clk_disable_unprepare(dev->clk_ahb);
  1633. clk_disable_unprepare(dev->clk_per);
  1634. return 0;
  1635. err_clk_ahb:
  1636. clk_disable_unprepare(dev->clk_per);
  1637. err_clk_per:
  1638. return ret;
  1639. }
  1640. static int coda_register_device(struct coda_dev *dev, int i)
  1641. {
  1642. struct video_device *vfd = &dev->vfd[i];
  1643. if (i >= dev->devtype->num_vdevs)
  1644. return -EINVAL;
  1645. strlcpy(vfd->name, dev->devtype->vdevs[i]->name, sizeof(vfd->name));
  1646. vfd->fops = &coda_fops;
  1647. vfd->ioctl_ops = &coda_ioctl_ops;
  1648. vfd->release = video_device_release_empty,
  1649. vfd->lock = &dev->dev_mutex;
  1650. vfd->v4l2_dev = &dev->v4l2_dev;
  1651. vfd->vfl_dir = VFL_DIR_M2M;
  1652. video_set_drvdata(vfd, dev);
  1653. /* Not applicable, use the selection API instead */
  1654. v4l2_disable_ioctl(vfd, VIDIOC_CROPCAP);
  1655. v4l2_disable_ioctl(vfd, VIDIOC_G_CROP);
  1656. v4l2_disable_ioctl(vfd, VIDIOC_S_CROP);
  1657. return video_register_device(vfd, VFL_TYPE_GRABBER, 0);
  1658. }
  1659. static void coda_copy_firmware(struct coda_dev *dev, const u8 * const buf,
  1660. size_t size)
  1661. {
  1662. u32 *src = (u32 *)buf;
  1663. /* Check if the firmware has a 16-byte Freescale header, skip it */
  1664. if (buf[0] == 'M' && buf[1] == 'X')
  1665. src += 4;
  1666. /*
  1667. * Check whether the firmware is in native order or pre-reordered for
  1668. * memory access. The first instruction opcode always is 0xe40e.
  1669. */
  1670. if (__le16_to_cpup((__le16 *)src) == 0xe40e) {
  1671. u32 *dst = dev->codebuf.vaddr;
  1672. int i;
  1673. /* Firmware in native order, reorder while copying */
  1674. if (dev->devtype->product == CODA_DX6) {
  1675. for (i = 0; i < (size - 16) / 4; i++)
  1676. dst[i] = (src[i] << 16) | (src[i] >> 16);
  1677. } else {
  1678. for (i = 0; i < (size - 16) / 4; i += 2) {
  1679. dst[i] = (src[i + 1] << 16) | (src[i + 1] >> 16);
  1680. dst[i + 1] = (src[i] << 16) | (src[i] >> 16);
  1681. }
  1682. }
  1683. } else {
  1684. /* Copy the already reordered firmware image */
  1685. memcpy(dev->codebuf.vaddr, src, size);
  1686. }
  1687. }
  1688. static void coda_fw_callback(const struct firmware *fw, void *context);
  1689. static int coda_firmware_request(struct coda_dev *dev)
  1690. {
  1691. char *fw = dev->devtype->firmware[dev->firmware];
  1692. dev_dbg(&dev->plat_dev->dev, "requesting firmware '%s' for %s\n", fw,
  1693. coda_product_name(dev->devtype->product));
  1694. return request_firmware_nowait(THIS_MODULE, true, fw,
  1695. &dev->plat_dev->dev, GFP_KERNEL, dev,
  1696. coda_fw_callback);
  1697. }
  1698. static void coda_fw_callback(const struct firmware *fw, void *context)
  1699. {
  1700. struct coda_dev *dev = context;
  1701. struct platform_device *pdev = dev->plat_dev;
  1702. int i, ret;
  1703. if (!fw && dev->firmware == 1) {
  1704. v4l2_err(&dev->v4l2_dev, "firmware request failed\n");
  1705. goto put_pm;
  1706. }
  1707. if (!fw) {
  1708. dev->firmware = 1;
  1709. coda_firmware_request(dev);
  1710. return;
  1711. }
  1712. if (dev->firmware == 1) {
  1713. /*
  1714. * Since we can't suppress warnings for failed asynchronous
  1715. * firmware requests, report that the fallback firmware was
  1716. * found.
  1717. */
  1718. dev_info(&pdev->dev, "Using fallback firmware %s\n",
  1719. dev->devtype->firmware[dev->firmware]);
  1720. }
  1721. /* allocate auxiliary per-device code buffer for the BIT processor */
  1722. ret = coda_alloc_aux_buf(dev, &dev->codebuf, fw->size, "codebuf",
  1723. dev->debugfs_root);
  1724. if (ret < 0)
  1725. goto put_pm;
  1726. coda_copy_firmware(dev, fw->data, fw->size);
  1727. release_firmware(fw);
  1728. ret = coda_hw_init(dev);
  1729. if (ret < 0) {
  1730. v4l2_err(&dev->v4l2_dev, "HW initialization failed\n");
  1731. goto put_pm;
  1732. }
  1733. ret = coda_check_firmware(dev);
  1734. if (ret < 0)
  1735. goto put_pm;
  1736. dev->m2m_dev = v4l2_m2m_init(&coda_m2m_ops);
  1737. if (IS_ERR(dev->m2m_dev)) {
  1738. v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
  1739. goto put_pm;
  1740. }
  1741. for (i = 0; i < dev->devtype->num_vdevs; i++) {
  1742. ret = coda_register_device(dev, i);
  1743. if (ret) {
  1744. v4l2_err(&dev->v4l2_dev,
  1745. "Failed to register %s video device: %d\n",
  1746. dev->devtype->vdevs[i]->name, ret);
  1747. goto rel_vfd;
  1748. }
  1749. }
  1750. v4l2_info(&dev->v4l2_dev, "codec registered as /dev/video[%d-%d]\n",
  1751. dev->vfd[0].num, dev->vfd[i - 1].num);
  1752. pm_runtime_put_sync(&pdev->dev);
  1753. return;
  1754. rel_vfd:
  1755. while (--i >= 0)
  1756. video_unregister_device(&dev->vfd[i]);
  1757. v4l2_m2m_release(dev->m2m_dev);
  1758. put_pm:
  1759. pm_runtime_put_sync(&pdev->dev);
  1760. }
  1761. enum coda_platform {
  1762. CODA_IMX27,
  1763. CODA_IMX53,
  1764. CODA_IMX6Q,
  1765. CODA_IMX6DL,
  1766. };
  1767. static const struct coda_devtype coda_devdata[] = {
  1768. [CODA_IMX27] = {
  1769. .firmware = {
  1770. "vpu_fw_imx27_TO2.bin",
  1771. "v4l-codadx6-imx27.bin"
  1772. },
  1773. .product = CODA_DX6,
  1774. .codecs = codadx6_codecs,
  1775. .num_codecs = ARRAY_SIZE(codadx6_codecs),
  1776. .vdevs = codadx6_video_devices,
  1777. .num_vdevs = ARRAY_SIZE(codadx6_video_devices),
  1778. .workbuf_size = 288 * 1024 + FMO_SLICE_SAVE_BUF_SIZE * 8 * 1024,
  1779. .iram_size = 0xb000,
  1780. },
  1781. [CODA_IMX53] = {
  1782. .firmware = {
  1783. "vpu_fw_imx53.bin",
  1784. "v4l-coda7541-imx53.bin"
  1785. },
  1786. .product = CODA_7541,
  1787. .codecs = coda7_codecs,
  1788. .num_codecs = ARRAY_SIZE(coda7_codecs),
  1789. .vdevs = coda7_video_devices,
  1790. .num_vdevs = ARRAY_SIZE(coda7_video_devices),
  1791. .workbuf_size = 128 * 1024,
  1792. .tempbuf_size = 304 * 1024,
  1793. .iram_size = 0x14000,
  1794. },
  1795. [CODA_IMX6Q] = {
  1796. .firmware = {
  1797. "vpu_fw_imx6q.bin",
  1798. "v4l-coda960-imx6q.bin"
  1799. },
  1800. .product = CODA_960,
  1801. .codecs = coda9_codecs,
  1802. .num_codecs = ARRAY_SIZE(coda9_codecs),
  1803. .vdevs = coda9_video_devices,
  1804. .num_vdevs = ARRAY_SIZE(coda9_video_devices),
  1805. .workbuf_size = 80 * 1024,
  1806. .tempbuf_size = 204 * 1024,
  1807. .iram_size = 0x21000,
  1808. },
  1809. [CODA_IMX6DL] = {
  1810. .firmware = {
  1811. "vpu_fw_imx6d.bin",
  1812. "v4l-coda960-imx6dl.bin"
  1813. },
  1814. .product = CODA_960,
  1815. .codecs = coda9_codecs,
  1816. .num_codecs = ARRAY_SIZE(coda9_codecs),
  1817. .vdevs = coda9_video_devices,
  1818. .num_vdevs = ARRAY_SIZE(coda9_video_devices),
  1819. .workbuf_size = 80 * 1024,
  1820. .tempbuf_size = 204 * 1024,
  1821. .iram_size = 0x20000,
  1822. },
  1823. };
  1824. static struct platform_device_id coda_platform_ids[] = {
  1825. { .name = "coda-imx27", .driver_data = CODA_IMX27 },
  1826. { /* sentinel */ }
  1827. };
  1828. MODULE_DEVICE_TABLE(platform, coda_platform_ids);
  1829. #ifdef CONFIG_OF
  1830. static const struct of_device_id coda_dt_ids[] = {
  1831. { .compatible = "fsl,imx27-vpu", .data = &coda_devdata[CODA_IMX27] },
  1832. { .compatible = "fsl,imx53-vpu", .data = &coda_devdata[CODA_IMX53] },
  1833. { .compatible = "fsl,imx6q-vpu", .data = &coda_devdata[CODA_IMX6Q] },
  1834. { .compatible = "fsl,imx6dl-vpu", .data = &coda_devdata[CODA_IMX6DL] },
  1835. { /* sentinel */ }
  1836. };
  1837. MODULE_DEVICE_TABLE(of, coda_dt_ids);
  1838. #endif
  1839. static int coda_probe(struct platform_device *pdev)
  1840. {
  1841. const struct of_device_id *of_id =
  1842. of_match_device(of_match_ptr(coda_dt_ids), &pdev->dev);
  1843. const struct platform_device_id *pdev_id;
  1844. struct coda_platform_data *pdata = pdev->dev.platform_data;
  1845. struct device_node *np = pdev->dev.of_node;
  1846. struct gen_pool *pool;
  1847. struct coda_dev *dev;
  1848. struct resource *res;
  1849. int ret, irq;
  1850. dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
  1851. if (!dev)
  1852. return -ENOMEM;
  1853. pdev_id = of_id ? of_id->data : platform_get_device_id(pdev);
  1854. if (of_id)
  1855. dev->devtype = of_id->data;
  1856. else if (pdev_id)
  1857. dev->devtype = &coda_devdata[pdev_id->driver_data];
  1858. else
  1859. return -EINVAL;
  1860. spin_lock_init(&dev->irqlock);
  1861. INIT_LIST_HEAD(&dev->instances);
  1862. dev->plat_dev = pdev;
  1863. dev->clk_per = devm_clk_get(&pdev->dev, "per");
  1864. if (IS_ERR(dev->clk_per)) {
  1865. dev_err(&pdev->dev, "Could not get per clock\n");
  1866. return PTR_ERR(dev->clk_per);
  1867. }
  1868. dev->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
  1869. if (IS_ERR(dev->clk_ahb)) {
  1870. dev_err(&pdev->dev, "Could not get ahb clock\n");
  1871. return PTR_ERR(dev->clk_ahb);
  1872. }
  1873. /* Get memory for physical registers */
  1874. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1875. dev->regs_base = devm_ioremap_resource(&pdev->dev, res);
  1876. if (IS_ERR(dev->regs_base))
  1877. return PTR_ERR(dev->regs_base);
  1878. /* IRQ */
  1879. irq = platform_get_irq_byname(pdev, "bit");
  1880. if (irq < 0)
  1881. irq = platform_get_irq(pdev, 0);
  1882. if (irq < 0) {
  1883. dev_err(&pdev->dev, "failed to get irq resource\n");
  1884. return irq;
  1885. }
  1886. ret = devm_request_threaded_irq(&pdev->dev, irq, NULL, coda_irq_handler,
  1887. IRQF_ONESHOT, dev_name(&pdev->dev), dev);
  1888. if (ret < 0) {
  1889. dev_err(&pdev->dev, "failed to request irq: %d\n", ret);
  1890. return ret;
  1891. }
  1892. dev->rstc = devm_reset_control_get_optional(&pdev->dev, NULL);
  1893. if (IS_ERR(dev->rstc)) {
  1894. ret = PTR_ERR(dev->rstc);
  1895. if (ret == -ENOENT || ret == -ENOTSUPP) {
  1896. dev->rstc = NULL;
  1897. } else {
  1898. dev_err(&pdev->dev, "failed get reset control: %d\n",
  1899. ret);
  1900. return ret;
  1901. }
  1902. }
  1903. /* Get IRAM pool from device tree or platform data */
  1904. pool = of_gen_pool_get(np, "iram", 0);
  1905. if (!pool && pdata)
  1906. pool = gen_pool_get(pdata->iram_dev, NULL);
  1907. if (!pool) {
  1908. dev_err(&pdev->dev, "iram pool not available\n");
  1909. return -ENOMEM;
  1910. }
  1911. dev->iram_pool = pool;
  1912. ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
  1913. if (ret)
  1914. return ret;
  1915. mutex_init(&dev->dev_mutex);
  1916. mutex_init(&dev->coda_mutex);
  1917. dev->debugfs_root = debugfs_create_dir("coda", NULL);
  1918. if (!dev->debugfs_root)
  1919. dev_warn(&pdev->dev, "failed to create debugfs root\n");
  1920. /* allocate auxiliary per-device buffers for the BIT processor */
  1921. if (dev->devtype->product == CODA_DX6) {
  1922. ret = coda_alloc_aux_buf(dev, &dev->workbuf,
  1923. dev->devtype->workbuf_size, "workbuf",
  1924. dev->debugfs_root);
  1925. if (ret < 0)
  1926. goto err_v4l2_register;
  1927. }
  1928. if (dev->devtype->tempbuf_size) {
  1929. ret = coda_alloc_aux_buf(dev, &dev->tempbuf,
  1930. dev->devtype->tempbuf_size, "tempbuf",
  1931. dev->debugfs_root);
  1932. if (ret < 0)
  1933. goto err_v4l2_register;
  1934. }
  1935. dev->iram.size = dev->devtype->iram_size;
  1936. dev->iram.vaddr = gen_pool_dma_alloc(dev->iram_pool, dev->iram.size,
  1937. &dev->iram.paddr);
  1938. if (!dev->iram.vaddr) {
  1939. dev_warn(&pdev->dev, "unable to alloc iram\n");
  1940. } else {
  1941. memset(dev->iram.vaddr, 0, dev->iram.size);
  1942. dev->iram.blob.data = dev->iram.vaddr;
  1943. dev->iram.blob.size = dev->iram.size;
  1944. dev->iram.dentry = debugfs_create_blob("iram", 0644,
  1945. dev->debugfs_root,
  1946. &dev->iram.blob);
  1947. }
  1948. dev->workqueue = alloc_workqueue("coda", WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
  1949. if (!dev->workqueue) {
  1950. dev_err(&pdev->dev, "unable to alloc workqueue\n");
  1951. ret = -ENOMEM;
  1952. goto err_v4l2_register;
  1953. }
  1954. platform_set_drvdata(pdev, dev);
  1955. /*
  1956. * Start activated so we can directly call coda_hw_init in
  1957. * coda_fw_callback regardless of whether CONFIG_PM is
  1958. * enabled or whether the device is associated with a PM domain.
  1959. */
  1960. pm_runtime_get_noresume(&pdev->dev);
  1961. pm_runtime_set_active(&pdev->dev);
  1962. pm_runtime_enable(&pdev->dev);
  1963. ret = coda_firmware_request(dev);
  1964. if (ret)
  1965. goto err_alloc_workqueue;
  1966. return 0;
  1967. err_alloc_workqueue:
  1968. destroy_workqueue(dev->workqueue);
  1969. err_v4l2_register:
  1970. v4l2_device_unregister(&dev->v4l2_dev);
  1971. return ret;
  1972. }
  1973. static int coda_remove(struct platform_device *pdev)
  1974. {
  1975. struct coda_dev *dev = platform_get_drvdata(pdev);
  1976. int i;
  1977. for (i = 0; i < ARRAY_SIZE(dev->vfd); i++) {
  1978. if (video_get_drvdata(&dev->vfd[i]))
  1979. video_unregister_device(&dev->vfd[i]);
  1980. }
  1981. if (dev->m2m_dev)
  1982. v4l2_m2m_release(dev->m2m_dev);
  1983. pm_runtime_disable(&pdev->dev);
  1984. v4l2_device_unregister(&dev->v4l2_dev);
  1985. destroy_workqueue(dev->workqueue);
  1986. if (dev->iram.vaddr)
  1987. gen_pool_free(dev->iram_pool, (unsigned long)dev->iram.vaddr,
  1988. dev->iram.size);
  1989. coda_free_aux_buf(dev, &dev->codebuf);
  1990. coda_free_aux_buf(dev, &dev->tempbuf);
  1991. coda_free_aux_buf(dev, &dev->workbuf);
  1992. debugfs_remove_recursive(dev->debugfs_root);
  1993. return 0;
  1994. }
  1995. #ifdef CONFIG_PM
  1996. static int coda_runtime_resume(struct device *dev)
  1997. {
  1998. struct coda_dev *cdev = dev_get_drvdata(dev);
  1999. int ret = 0;
  2000. if (dev->pm_domain && cdev->codebuf.vaddr) {
  2001. ret = coda_hw_init(cdev);
  2002. if (ret)
  2003. v4l2_err(&cdev->v4l2_dev, "HW initialization failed\n");
  2004. }
  2005. return ret;
  2006. }
  2007. #endif
  2008. static const struct dev_pm_ops coda_pm_ops = {
  2009. SET_RUNTIME_PM_OPS(NULL, coda_runtime_resume, NULL)
  2010. };
  2011. static struct platform_driver coda_driver = {
  2012. .probe = coda_probe,
  2013. .remove = coda_remove,
  2014. .driver = {
  2015. .name = CODA_NAME,
  2016. .of_match_table = of_match_ptr(coda_dt_ids),
  2017. .pm = &coda_pm_ops,
  2018. },
  2019. .id_table = coda_platform_ids,
  2020. };
  2021. module_platform_driver(coda_driver);
  2022. MODULE_LICENSE("GPL");
  2023. MODULE_AUTHOR("Javier Martin <javier.martin@vista-silicon.com>");
  2024. MODULE_DESCRIPTION("Coda multi-standard codec V4L2 driver");