coda-common.c 76 KB

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