vpe.c 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526
  1. /*
  2. * TI VPE mem2mem driver, based on the virtual v4l2-mem2mem example driver
  3. *
  4. * Copyright (c) 2013 Texas Instruments Inc.
  5. * David Griego, <dagriego@biglakesoftware.com>
  6. * Dale Farnsworth, <dale@farnsworth.org>
  7. * Archit Taneja, <archit@ti.com>
  8. *
  9. * Copyright (c) 2009-2010 Samsung Electronics Co., Ltd.
  10. * Pawel Osciak, <pawel@osciak.com>
  11. * Marek Szyprowski, <m.szyprowski@samsung.com>
  12. *
  13. * Based on the virtual v4l2-mem2mem example device
  14. *
  15. * This program is free software; you can redistribute it and/or modify it
  16. * under the terms of the GNU General Public License version 2 as published by
  17. * the Free Software Foundation
  18. */
  19. #include <linux/delay.h>
  20. #include <linux/dma-mapping.h>
  21. #include <linux/err.h>
  22. #include <linux/fs.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/io.h>
  25. #include <linux/ioctl.h>
  26. #include <linux/module.h>
  27. #include <linux/of.h>
  28. #include <linux/platform_device.h>
  29. #include <linux/pm_runtime.h>
  30. #include <linux/sched.h>
  31. #include <linux/slab.h>
  32. #include <linux/videodev2.h>
  33. #include <linux/log2.h>
  34. #include <linux/sizes.h>
  35. #include <media/v4l2-common.h>
  36. #include <media/v4l2-ctrls.h>
  37. #include <media/v4l2-device.h>
  38. #include <media/v4l2-event.h>
  39. #include <media/v4l2-ioctl.h>
  40. #include <media/v4l2-mem2mem.h>
  41. #include <media/videobuf2-v4l2.h>
  42. #include <media/videobuf2-dma-contig.h>
  43. #include "vpdma.h"
  44. #include "vpdma_priv.h"
  45. #include "vpe_regs.h"
  46. #include "sc.h"
  47. #include "csc.h"
  48. #define VPE_MODULE_NAME "vpe"
  49. /* minimum and maximum frame sizes */
  50. #define MIN_W 32
  51. #define MIN_H 32
  52. #define MAX_W 2048
  53. #define MAX_H 1184
  54. /* required alignments */
  55. #define S_ALIGN 0 /* multiple of 1 */
  56. #define H_ALIGN 1 /* multiple of 2 */
  57. /* flags that indicate a format can be used for capture/output */
  58. #define VPE_FMT_TYPE_CAPTURE (1 << 0)
  59. #define VPE_FMT_TYPE_OUTPUT (1 << 1)
  60. /* used as plane indices */
  61. #define VPE_MAX_PLANES 2
  62. #define VPE_LUMA 0
  63. #define VPE_CHROMA 1
  64. /* per m2m context info */
  65. #define VPE_MAX_SRC_BUFS 3 /* need 3 src fields to de-interlace */
  66. #define VPE_DEF_BUFS_PER_JOB 1 /* default one buffer per batch job */
  67. /*
  68. * each VPE context can need up to 3 config descriptors, 7 input descriptors,
  69. * 3 output descriptors, and 10 control descriptors
  70. */
  71. #define VPE_DESC_LIST_SIZE (10 * VPDMA_DTD_DESC_SIZE + \
  72. 13 * VPDMA_CFD_CTD_DESC_SIZE)
  73. #define vpe_dbg(vpedev, fmt, arg...) \
  74. dev_dbg((vpedev)->v4l2_dev.dev, fmt, ##arg)
  75. #define vpe_err(vpedev, fmt, arg...) \
  76. dev_err((vpedev)->v4l2_dev.dev, fmt, ##arg)
  77. struct vpe_us_coeffs {
  78. unsigned short anchor_fid0_c0;
  79. unsigned short anchor_fid0_c1;
  80. unsigned short anchor_fid0_c2;
  81. unsigned short anchor_fid0_c3;
  82. unsigned short interp_fid0_c0;
  83. unsigned short interp_fid0_c1;
  84. unsigned short interp_fid0_c2;
  85. unsigned short interp_fid0_c3;
  86. unsigned short anchor_fid1_c0;
  87. unsigned short anchor_fid1_c1;
  88. unsigned short anchor_fid1_c2;
  89. unsigned short anchor_fid1_c3;
  90. unsigned short interp_fid1_c0;
  91. unsigned short interp_fid1_c1;
  92. unsigned short interp_fid1_c2;
  93. unsigned short interp_fid1_c3;
  94. };
  95. /*
  96. * Default upsampler coefficients
  97. */
  98. static const struct vpe_us_coeffs us_coeffs[] = {
  99. {
  100. /* Coefficients for progressive input */
  101. 0x00C8, 0x0348, 0x0018, 0x3FD8, 0x3FB8, 0x0378, 0x00E8, 0x3FE8,
  102. 0x00C8, 0x0348, 0x0018, 0x3FD8, 0x3FB8, 0x0378, 0x00E8, 0x3FE8,
  103. },
  104. {
  105. /* Coefficients for Top Field Interlaced input */
  106. 0x0051, 0x03D5, 0x3FE3, 0x3FF7, 0x3FB5, 0x02E9, 0x018F, 0x3FD3,
  107. /* Coefficients for Bottom Field Interlaced input */
  108. 0x016B, 0x0247, 0x00B1, 0x3F9D, 0x3FCF, 0x03DB, 0x005D, 0x3FF9,
  109. },
  110. };
  111. /*
  112. * the following registers are for configuring some of the parameters of the
  113. * motion and edge detection blocks inside DEI, these generally remain the same,
  114. * these could be passed later via userspace if some one needs to tweak these.
  115. */
  116. struct vpe_dei_regs {
  117. unsigned long mdt_spacial_freq_thr_reg; /* VPE_DEI_REG2 */
  118. unsigned long edi_config_reg; /* VPE_DEI_REG3 */
  119. unsigned long edi_lut_reg0; /* VPE_DEI_REG4 */
  120. unsigned long edi_lut_reg1; /* VPE_DEI_REG5 */
  121. unsigned long edi_lut_reg2; /* VPE_DEI_REG6 */
  122. unsigned long edi_lut_reg3; /* VPE_DEI_REG7 */
  123. };
  124. /*
  125. * default expert DEI register values, unlikely to be modified.
  126. */
  127. static const struct vpe_dei_regs dei_regs = {
  128. .mdt_spacial_freq_thr_reg = 0x020C0804u,
  129. .edi_config_reg = 0x0118100Cu,
  130. .edi_lut_reg0 = 0x08040200u,
  131. .edi_lut_reg1 = 0x1010100Cu,
  132. .edi_lut_reg2 = 0x10101010u,
  133. .edi_lut_reg3 = 0x10101010u,
  134. };
  135. /*
  136. * The port_data structure contains per-port data.
  137. */
  138. struct vpe_port_data {
  139. enum vpdma_channel channel; /* VPDMA channel */
  140. u8 vb_index; /* input frame f, f-1, f-2 index */
  141. u8 vb_part; /* plane index for co-panar formats */
  142. };
  143. /*
  144. * Define indices into the port_data tables
  145. */
  146. #define VPE_PORT_LUMA1_IN 0
  147. #define VPE_PORT_CHROMA1_IN 1
  148. #define VPE_PORT_LUMA2_IN 2
  149. #define VPE_PORT_CHROMA2_IN 3
  150. #define VPE_PORT_LUMA3_IN 4
  151. #define VPE_PORT_CHROMA3_IN 5
  152. #define VPE_PORT_MV_IN 6
  153. #define VPE_PORT_MV_OUT 7
  154. #define VPE_PORT_LUMA_OUT 8
  155. #define VPE_PORT_CHROMA_OUT 9
  156. #define VPE_PORT_RGB_OUT 10
  157. static const struct vpe_port_data port_data[11] = {
  158. [VPE_PORT_LUMA1_IN] = {
  159. .channel = VPE_CHAN_LUMA1_IN,
  160. .vb_index = 0,
  161. .vb_part = VPE_LUMA,
  162. },
  163. [VPE_PORT_CHROMA1_IN] = {
  164. .channel = VPE_CHAN_CHROMA1_IN,
  165. .vb_index = 0,
  166. .vb_part = VPE_CHROMA,
  167. },
  168. [VPE_PORT_LUMA2_IN] = {
  169. .channel = VPE_CHAN_LUMA2_IN,
  170. .vb_index = 1,
  171. .vb_part = VPE_LUMA,
  172. },
  173. [VPE_PORT_CHROMA2_IN] = {
  174. .channel = VPE_CHAN_CHROMA2_IN,
  175. .vb_index = 1,
  176. .vb_part = VPE_CHROMA,
  177. },
  178. [VPE_PORT_LUMA3_IN] = {
  179. .channel = VPE_CHAN_LUMA3_IN,
  180. .vb_index = 2,
  181. .vb_part = VPE_LUMA,
  182. },
  183. [VPE_PORT_CHROMA3_IN] = {
  184. .channel = VPE_CHAN_CHROMA3_IN,
  185. .vb_index = 2,
  186. .vb_part = VPE_CHROMA,
  187. },
  188. [VPE_PORT_MV_IN] = {
  189. .channel = VPE_CHAN_MV_IN,
  190. },
  191. [VPE_PORT_MV_OUT] = {
  192. .channel = VPE_CHAN_MV_OUT,
  193. },
  194. [VPE_PORT_LUMA_OUT] = {
  195. .channel = VPE_CHAN_LUMA_OUT,
  196. .vb_part = VPE_LUMA,
  197. },
  198. [VPE_PORT_CHROMA_OUT] = {
  199. .channel = VPE_CHAN_CHROMA_OUT,
  200. .vb_part = VPE_CHROMA,
  201. },
  202. [VPE_PORT_RGB_OUT] = {
  203. .channel = VPE_CHAN_RGB_OUT,
  204. .vb_part = VPE_LUMA,
  205. },
  206. };
  207. /* driver info for each of the supported video formats */
  208. struct vpe_fmt {
  209. char *name; /* human-readable name */
  210. u32 fourcc; /* standard format identifier */
  211. u8 types; /* CAPTURE and/or OUTPUT */
  212. u8 coplanar; /* set for unpacked Luma and Chroma */
  213. /* vpdma format info for each plane */
  214. struct vpdma_data_format const *vpdma_fmt[VPE_MAX_PLANES];
  215. };
  216. static struct vpe_fmt vpe_formats[] = {
  217. {
  218. .name = "NV16 YUV 422 co-planar",
  219. .fourcc = V4L2_PIX_FMT_NV16,
  220. .types = VPE_FMT_TYPE_CAPTURE | VPE_FMT_TYPE_OUTPUT,
  221. .coplanar = 1,
  222. .vpdma_fmt = { &vpdma_yuv_fmts[VPDMA_DATA_FMT_Y444],
  223. &vpdma_yuv_fmts[VPDMA_DATA_FMT_C444],
  224. },
  225. },
  226. {
  227. .name = "NV12 YUV 420 co-planar",
  228. .fourcc = V4L2_PIX_FMT_NV12,
  229. .types = VPE_FMT_TYPE_CAPTURE | VPE_FMT_TYPE_OUTPUT,
  230. .coplanar = 1,
  231. .vpdma_fmt = { &vpdma_yuv_fmts[VPDMA_DATA_FMT_Y420],
  232. &vpdma_yuv_fmts[VPDMA_DATA_FMT_C420],
  233. },
  234. },
  235. {
  236. .name = "YUYV 422 packed",
  237. .fourcc = V4L2_PIX_FMT_YUYV,
  238. .types = VPE_FMT_TYPE_CAPTURE | VPE_FMT_TYPE_OUTPUT,
  239. .coplanar = 0,
  240. .vpdma_fmt = { &vpdma_yuv_fmts[VPDMA_DATA_FMT_YCB422],
  241. },
  242. },
  243. {
  244. .name = "UYVY 422 packed",
  245. .fourcc = V4L2_PIX_FMT_UYVY,
  246. .types = VPE_FMT_TYPE_CAPTURE | VPE_FMT_TYPE_OUTPUT,
  247. .coplanar = 0,
  248. .vpdma_fmt = { &vpdma_yuv_fmts[VPDMA_DATA_FMT_CBY422],
  249. },
  250. },
  251. {
  252. .name = "RGB888 packed",
  253. .fourcc = V4L2_PIX_FMT_RGB24,
  254. .types = VPE_FMT_TYPE_CAPTURE,
  255. .coplanar = 0,
  256. .vpdma_fmt = { &vpdma_rgb_fmts[VPDMA_DATA_FMT_RGB24],
  257. },
  258. },
  259. {
  260. .name = "ARGB32",
  261. .fourcc = V4L2_PIX_FMT_RGB32,
  262. .types = VPE_FMT_TYPE_CAPTURE,
  263. .coplanar = 0,
  264. .vpdma_fmt = { &vpdma_rgb_fmts[VPDMA_DATA_FMT_ARGB32],
  265. },
  266. },
  267. {
  268. .name = "BGR888 packed",
  269. .fourcc = V4L2_PIX_FMT_BGR24,
  270. .types = VPE_FMT_TYPE_CAPTURE,
  271. .coplanar = 0,
  272. .vpdma_fmt = { &vpdma_rgb_fmts[VPDMA_DATA_FMT_BGR24],
  273. },
  274. },
  275. {
  276. .name = "ABGR32",
  277. .fourcc = V4L2_PIX_FMT_BGR32,
  278. .types = VPE_FMT_TYPE_CAPTURE,
  279. .coplanar = 0,
  280. .vpdma_fmt = { &vpdma_rgb_fmts[VPDMA_DATA_FMT_ABGR32],
  281. },
  282. },
  283. {
  284. .name = "RGB565",
  285. .fourcc = V4L2_PIX_FMT_RGB565,
  286. .types = VPE_FMT_TYPE_CAPTURE,
  287. .coplanar = 0,
  288. .vpdma_fmt = { &vpdma_rgb_fmts[VPDMA_DATA_FMT_RGB565],
  289. },
  290. },
  291. {
  292. .name = "RGB5551",
  293. .fourcc = V4L2_PIX_FMT_RGB555,
  294. .types = VPE_FMT_TYPE_CAPTURE,
  295. .coplanar = 0,
  296. .vpdma_fmt = { &vpdma_rgb_fmts[VPDMA_DATA_FMT_RGBA16_5551],
  297. },
  298. },
  299. };
  300. /*
  301. * per-queue, driver-specific private data.
  302. * there is one source queue and one destination queue for each m2m context.
  303. */
  304. struct vpe_q_data {
  305. unsigned int width; /* frame width */
  306. unsigned int height; /* frame height */
  307. unsigned int bytesperline[VPE_MAX_PLANES]; /* bytes per line in memory */
  308. enum v4l2_colorspace colorspace;
  309. enum v4l2_field field; /* supported field value */
  310. unsigned int flags;
  311. unsigned int sizeimage[VPE_MAX_PLANES]; /* image size in memory */
  312. struct v4l2_rect c_rect; /* crop/compose rectangle */
  313. struct vpe_fmt *fmt; /* format info */
  314. };
  315. /* vpe_q_data flag bits */
  316. #define Q_DATA_FRAME_1D BIT(0)
  317. #define Q_DATA_MODE_TILED BIT(1)
  318. #define Q_DATA_INTERLACED_ALTERNATE BIT(2)
  319. #define Q_DATA_INTERLACED_SEQ_TB BIT(3)
  320. #define Q_IS_INTERLACED (Q_DATA_INTERLACED_ALTERNATE | \
  321. Q_DATA_INTERLACED_SEQ_TB)
  322. enum {
  323. Q_DATA_SRC = 0,
  324. Q_DATA_DST = 1,
  325. };
  326. /* find our format description corresponding to the passed v4l2_format */
  327. static struct vpe_fmt *find_format(struct v4l2_format *f)
  328. {
  329. struct vpe_fmt *fmt;
  330. unsigned int k;
  331. for (k = 0; k < ARRAY_SIZE(vpe_formats); k++) {
  332. fmt = &vpe_formats[k];
  333. if (fmt->fourcc == f->fmt.pix.pixelformat)
  334. return fmt;
  335. }
  336. return NULL;
  337. }
  338. /*
  339. * there is one vpe_dev structure in the driver, it is shared by
  340. * all instances.
  341. */
  342. struct vpe_dev {
  343. struct v4l2_device v4l2_dev;
  344. struct video_device vfd;
  345. struct v4l2_m2m_dev *m2m_dev;
  346. atomic_t num_instances; /* count of driver instances */
  347. dma_addr_t loaded_mmrs; /* shadow mmrs in device */
  348. struct mutex dev_mutex;
  349. spinlock_t lock;
  350. int irq;
  351. void __iomem *base;
  352. struct resource *res;
  353. struct vpdma_data vpdma_data;
  354. struct vpdma_data *vpdma; /* vpdma data handle */
  355. struct sc_data *sc; /* scaler data handle */
  356. struct csc_data *csc; /* csc data handle */
  357. };
  358. /*
  359. * There is one vpe_ctx structure for each m2m context.
  360. */
  361. struct vpe_ctx {
  362. struct v4l2_fh fh;
  363. struct vpe_dev *dev;
  364. struct v4l2_ctrl_handler hdl;
  365. unsigned int field; /* current field */
  366. unsigned int sequence; /* current frame/field seq */
  367. unsigned int aborting; /* abort after next irq */
  368. unsigned int bufs_per_job; /* input buffers per batch */
  369. unsigned int bufs_completed; /* bufs done in this batch */
  370. struct vpe_q_data q_data[2]; /* src & dst queue data */
  371. struct vb2_v4l2_buffer *src_vbs[VPE_MAX_SRC_BUFS];
  372. struct vb2_v4l2_buffer *dst_vb;
  373. dma_addr_t mv_buf_dma[2]; /* dma addrs of motion vector in/out bufs */
  374. void *mv_buf[2]; /* virtual addrs of motion vector bufs */
  375. size_t mv_buf_size; /* current motion vector buffer size */
  376. struct vpdma_buf mmr_adb; /* shadow reg addr/data block */
  377. struct vpdma_buf sc_coeff_h; /* h coeff buffer */
  378. struct vpdma_buf sc_coeff_v; /* v coeff buffer */
  379. struct vpdma_desc_list desc_list; /* DMA descriptor list */
  380. bool deinterlacing; /* using de-interlacer */
  381. bool load_mmrs; /* have new shadow reg values */
  382. unsigned int src_mv_buf_selector;
  383. };
  384. /*
  385. * M2M devices get 2 queues.
  386. * Return the queue given the type.
  387. */
  388. static struct vpe_q_data *get_q_data(struct vpe_ctx *ctx,
  389. enum v4l2_buf_type type)
  390. {
  391. switch (type) {
  392. case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
  393. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  394. return &ctx->q_data[Q_DATA_SRC];
  395. case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
  396. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  397. return &ctx->q_data[Q_DATA_DST];
  398. default:
  399. return NULL;
  400. }
  401. return NULL;
  402. }
  403. static u32 read_reg(struct vpe_dev *dev, int offset)
  404. {
  405. return ioread32(dev->base + offset);
  406. }
  407. static void write_reg(struct vpe_dev *dev, int offset, u32 value)
  408. {
  409. iowrite32(value, dev->base + offset);
  410. }
  411. /* register field read/write helpers */
  412. static int get_field(u32 value, u32 mask, int shift)
  413. {
  414. return (value & (mask << shift)) >> shift;
  415. }
  416. static int read_field_reg(struct vpe_dev *dev, int offset, u32 mask, int shift)
  417. {
  418. return get_field(read_reg(dev, offset), mask, shift);
  419. }
  420. static void write_field(u32 *valp, u32 field, u32 mask, int shift)
  421. {
  422. u32 val = *valp;
  423. val &= ~(mask << shift);
  424. val |= (field & mask) << shift;
  425. *valp = val;
  426. }
  427. static void write_field_reg(struct vpe_dev *dev, int offset, u32 field,
  428. u32 mask, int shift)
  429. {
  430. u32 val = read_reg(dev, offset);
  431. write_field(&val, field, mask, shift);
  432. write_reg(dev, offset, val);
  433. }
  434. /*
  435. * DMA address/data block for the shadow registers
  436. */
  437. struct vpe_mmr_adb {
  438. struct vpdma_adb_hdr out_fmt_hdr;
  439. u32 out_fmt_reg[1];
  440. u32 out_fmt_pad[3];
  441. struct vpdma_adb_hdr us1_hdr;
  442. u32 us1_regs[8];
  443. struct vpdma_adb_hdr us2_hdr;
  444. u32 us2_regs[8];
  445. struct vpdma_adb_hdr us3_hdr;
  446. u32 us3_regs[8];
  447. struct vpdma_adb_hdr dei_hdr;
  448. u32 dei_regs[8];
  449. struct vpdma_adb_hdr sc_hdr0;
  450. u32 sc_regs0[7];
  451. u32 sc_pad0[1];
  452. struct vpdma_adb_hdr sc_hdr8;
  453. u32 sc_regs8[6];
  454. u32 sc_pad8[2];
  455. struct vpdma_adb_hdr sc_hdr17;
  456. u32 sc_regs17[9];
  457. u32 sc_pad17[3];
  458. struct vpdma_adb_hdr csc_hdr;
  459. u32 csc_regs[6];
  460. u32 csc_pad[2];
  461. };
  462. #define GET_OFFSET_TOP(ctx, obj, reg) \
  463. ((obj)->res->start - ctx->dev->res->start + reg)
  464. #define VPE_SET_MMR_ADB_HDR(ctx, hdr, regs, offset_a) \
  465. VPDMA_SET_MMR_ADB_HDR(ctx->mmr_adb, vpe_mmr_adb, hdr, regs, offset_a)
  466. /*
  467. * Set the headers for all of the address/data block structures.
  468. */
  469. static void init_adb_hdrs(struct vpe_ctx *ctx)
  470. {
  471. VPE_SET_MMR_ADB_HDR(ctx, out_fmt_hdr, out_fmt_reg, VPE_CLK_FORMAT_SELECT);
  472. VPE_SET_MMR_ADB_HDR(ctx, us1_hdr, us1_regs, VPE_US1_R0);
  473. VPE_SET_MMR_ADB_HDR(ctx, us2_hdr, us2_regs, VPE_US2_R0);
  474. VPE_SET_MMR_ADB_HDR(ctx, us3_hdr, us3_regs, VPE_US3_R0);
  475. VPE_SET_MMR_ADB_HDR(ctx, dei_hdr, dei_regs, VPE_DEI_FRAME_SIZE);
  476. VPE_SET_MMR_ADB_HDR(ctx, sc_hdr0, sc_regs0,
  477. GET_OFFSET_TOP(ctx, ctx->dev->sc, CFG_SC0));
  478. VPE_SET_MMR_ADB_HDR(ctx, sc_hdr8, sc_regs8,
  479. GET_OFFSET_TOP(ctx, ctx->dev->sc, CFG_SC8));
  480. VPE_SET_MMR_ADB_HDR(ctx, sc_hdr17, sc_regs17,
  481. GET_OFFSET_TOP(ctx, ctx->dev->sc, CFG_SC17));
  482. VPE_SET_MMR_ADB_HDR(ctx, csc_hdr, csc_regs,
  483. GET_OFFSET_TOP(ctx, ctx->dev->csc, CSC_CSC00));
  484. };
  485. /*
  486. * Allocate or re-allocate the motion vector DMA buffers
  487. * There are two buffers, one for input and one for output.
  488. * However, the roles are reversed after each field is processed.
  489. * In other words, after each field is processed, the previous
  490. * output (dst) MV buffer becomes the new input (src) MV buffer.
  491. */
  492. static int realloc_mv_buffers(struct vpe_ctx *ctx, size_t size)
  493. {
  494. struct device *dev = ctx->dev->v4l2_dev.dev;
  495. if (ctx->mv_buf_size == size)
  496. return 0;
  497. if (ctx->mv_buf[0])
  498. dma_free_coherent(dev, ctx->mv_buf_size, ctx->mv_buf[0],
  499. ctx->mv_buf_dma[0]);
  500. if (ctx->mv_buf[1])
  501. dma_free_coherent(dev, ctx->mv_buf_size, ctx->mv_buf[1],
  502. ctx->mv_buf_dma[1]);
  503. if (size == 0)
  504. return 0;
  505. ctx->mv_buf[0] = dma_alloc_coherent(dev, size, &ctx->mv_buf_dma[0],
  506. GFP_KERNEL);
  507. if (!ctx->mv_buf[0]) {
  508. vpe_err(ctx->dev, "failed to allocate motion vector buffer\n");
  509. return -ENOMEM;
  510. }
  511. ctx->mv_buf[1] = dma_alloc_coherent(dev, size, &ctx->mv_buf_dma[1],
  512. GFP_KERNEL);
  513. if (!ctx->mv_buf[1]) {
  514. vpe_err(ctx->dev, "failed to allocate motion vector buffer\n");
  515. dma_free_coherent(dev, size, ctx->mv_buf[0],
  516. ctx->mv_buf_dma[0]);
  517. return -ENOMEM;
  518. }
  519. ctx->mv_buf_size = size;
  520. ctx->src_mv_buf_selector = 0;
  521. return 0;
  522. }
  523. static void free_mv_buffers(struct vpe_ctx *ctx)
  524. {
  525. realloc_mv_buffers(ctx, 0);
  526. }
  527. /*
  528. * While de-interlacing, we keep the two most recent input buffers
  529. * around. This function frees those two buffers when we have
  530. * finished processing the current stream.
  531. */
  532. static void free_vbs(struct vpe_ctx *ctx)
  533. {
  534. struct vpe_dev *dev = ctx->dev;
  535. unsigned long flags;
  536. if (ctx->src_vbs[2] == NULL)
  537. return;
  538. spin_lock_irqsave(&dev->lock, flags);
  539. if (ctx->src_vbs[2]) {
  540. v4l2_m2m_buf_done(ctx->src_vbs[2], VB2_BUF_STATE_DONE);
  541. if (ctx->src_vbs[1] && (ctx->src_vbs[1] != ctx->src_vbs[2]))
  542. v4l2_m2m_buf_done(ctx->src_vbs[1], VB2_BUF_STATE_DONE);
  543. ctx->src_vbs[2] = NULL;
  544. ctx->src_vbs[1] = NULL;
  545. }
  546. spin_unlock_irqrestore(&dev->lock, flags);
  547. }
  548. /*
  549. * Enable or disable the VPE clocks
  550. */
  551. static void vpe_set_clock_enable(struct vpe_dev *dev, bool on)
  552. {
  553. u32 val = 0;
  554. if (on)
  555. val = VPE_DATA_PATH_CLK_ENABLE | VPE_VPEDMA_CLK_ENABLE;
  556. write_reg(dev, VPE_CLK_ENABLE, val);
  557. }
  558. static void vpe_top_reset(struct vpe_dev *dev)
  559. {
  560. write_field_reg(dev, VPE_CLK_RESET, 1, VPE_DATA_PATH_CLK_RESET_MASK,
  561. VPE_DATA_PATH_CLK_RESET_SHIFT);
  562. usleep_range(100, 150);
  563. write_field_reg(dev, VPE_CLK_RESET, 0, VPE_DATA_PATH_CLK_RESET_MASK,
  564. VPE_DATA_PATH_CLK_RESET_SHIFT);
  565. }
  566. static void vpe_top_vpdma_reset(struct vpe_dev *dev)
  567. {
  568. write_field_reg(dev, VPE_CLK_RESET, 1, VPE_VPDMA_CLK_RESET_MASK,
  569. VPE_VPDMA_CLK_RESET_SHIFT);
  570. usleep_range(100, 150);
  571. write_field_reg(dev, VPE_CLK_RESET, 0, VPE_VPDMA_CLK_RESET_MASK,
  572. VPE_VPDMA_CLK_RESET_SHIFT);
  573. }
  574. /*
  575. * Load the correct of upsampler coefficients into the shadow MMRs
  576. */
  577. static void set_us_coefficients(struct vpe_ctx *ctx)
  578. {
  579. struct vpe_mmr_adb *mmr_adb = ctx->mmr_adb.addr;
  580. struct vpe_q_data *s_q_data = &ctx->q_data[Q_DATA_SRC];
  581. u32 *us1_reg = &mmr_adb->us1_regs[0];
  582. u32 *us2_reg = &mmr_adb->us2_regs[0];
  583. u32 *us3_reg = &mmr_adb->us3_regs[0];
  584. const unsigned short *cp, *end_cp;
  585. cp = &us_coeffs[0].anchor_fid0_c0;
  586. if (s_q_data->flags & Q_IS_INTERLACED) /* interlaced */
  587. cp += sizeof(us_coeffs[0]) / sizeof(*cp);
  588. end_cp = cp + sizeof(us_coeffs[0]) / sizeof(*cp);
  589. while (cp < end_cp) {
  590. write_field(us1_reg, *cp++, VPE_US_C0_MASK, VPE_US_C0_SHIFT);
  591. write_field(us1_reg, *cp++, VPE_US_C1_MASK, VPE_US_C1_SHIFT);
  592. *us2_reg++ = *us1_reg;
  593. *us3_reg++ = *us1_reg++;
  594. }
  595. ctx->load_mmrs = true;
  596. }
  597. /*
  598. * Set the upsampler config mode and the VPDMA line mode in the shadow MMRs.
  599. */
  600. static void set_cfg_modes(struct vpe_ctx *ctx)
  601. {
  602. struct vpe_fmt *fmt = ctx->q_data[Q_DATA_SRC].fmt;
  603. struct vpe_mmr_adb *mmr_adb = ctx->mmr_adb.addr;
  604. u32 *us1_reg0 = &mmr_adb->us1_regs[0];
  605. u32 *us2_reg0 = &mmr_adb->us2_regs[0];
  606. u32 *us3_reg0 = &mmr_adb->us3_regs[0];
  607. int cfg_mode = 1;
  608. /*
  609. * Cfg Mode 0: YUV420 source, enable upsampler, DEI is de-interlacing.
  610. * Cfg Mode 1: YUV422 source, disable upsampler, DEI is de-interlacing.
  611. */
  612. if (fmt->fourcc == V4L2_PIX_FMT_NV12)
  613. cfg_mode = 0;
  614. write_field(us1_reg0, cfg_mode, VPE_US_MODE_MASK, VPE_US_MODE_SHIFT);
  615. write_field(us2_reg0, cfg_mode, VPE_US_MODE_MASK, VPE_US_MODE_SHIFT);
  616. write_field(us3_reg0, cfg_mode, VPE_US_MODE_MASK, VPE_US_MODE_SHIFT);
  617. ctx->load_mmrs = true;
  618. }
  619. static void set_line_modes(struct vpe_ctx *ctx)
  620. {
  621. struct vpe_fmt *fmt = ctx->q_data[Q_DATA_SRC].fmt;
  622. int line_mode = 1;
  623. if (fmt->fourcc == V4L2_PIX_FMT_NV12)
  624. line_mode = 0; /* double lines to line buffer */
  625. /* regs for now */
  626. vpdma_set_line_mode(ctx->dev->vpdma, line_mode, VPE_CHAN_CHROMA1_IN);
  627. vpdma_set_line_mode(ctx->dev->vpdma, line_mode, VPE_CHAN_CHROMA2_IN);
  628. vpdma_set_line_mode(ctx->dev->vpdma, line_mode, VPE_CHAN_CHROMA3_IN);
  629. /* frame start for input luma */
  630. vpdma_set_frame_start_event(ctx->dev->vpdma, VPDMA_FSEVENT_CHANNEL_ACTIVE,
  631. VPE_CHAN_LUMA1_IN);
  632. vpdma_set_frame_start_event(ctx->dev->vpdma, VPDMA_FSEVENT_CHANNEL_ACTIVE,
  633. VPE_CHAN_LUMA2_IN);
  634. vpdma_set_frame_start_event(ctx->dev->vpdma, VPDMA_FSEVENT_CHANNEL_ACTIVE,
  635. VPE_CHAN_LUMA3_IN);
  636. /* frame start for input chroma */
  637. vpdma_set_frame_start_event(ctx->dev->vpdma, VPDMA_FSEVENT_CHANNEL_ACTIVE,
  638. VPE_CHAN_CHROMA1_IN);
  639. vpdma_set_frame_start_event(ctx->dev->vpdma, VPDMA_FSEVENT_CHANNEL_ACTIVE,
  640. VPE_CHAN_CHROMA2_IN);
  641. vpdma_set_frame_start_event(ctx->dev->vpdma, VPDMA_FSEVENT_CHANNEL_ACTIVE,
  642. VPE_CHAN_CHROMA3_IN);
  643. /* frame start for MV in client */
  644. vpdma_set_frame_start_event(ctx->dev->vpdma, VPDMA_FSEVENT_CHANNEL_ACTIVE,
  645. VPE_CHAN_MV_IN);
  646. }
  647. /*
  648. * Set the shadow registers that are modified when the source
  649. * format changes.
  650. */
  651. static void set_src_registers(struct vpe_ctx *ctx)
  652. {
  653. set_us_coefficients(ctx);
  654. }
  655. /*
  656. * Set the shadow registers that are modified when the destination
  657. * format changes.
  658. */
  659. static void set_dst_registers(struct vpe_ctx *ctx)
  660. {
  661. struct vpe_mmr_adb *mmr_adb = ctx->mmr_adb.addr;
  662. enum v4l2_colorspace clrspc = ctx->q_data[Q_DATA_DST].colorspace;
  663. struct vpe_fmt *fmt = ctx->q_data[Q_DATA_DST].fmt;
  664. u32 val = 0;
  665. if (clrspc == V4L2_COLORSPACE_SRGB) {
  666. val |= VPE_RGB_OUT_SELECT;
  667. vpdma_set_bg_color(ctx->dev->vpdma,
  668. (struct vpdma_data_format *)fmt->vpdma_fmt[0], 0xff);
  669. } else if (fmt->fourcc == V4L2_PIX_FMT_NV16)
  670. val |= VPE_COLOR_SEPARATE_422;
  671. /*
  672. * the source of CHR_DS and CSC is always the scaler, irrespective of
  673. * whether it's used or not
  674. */
  675. val |= VPE_DS_SRC_DEI_SCALER | VPE_CSC_SRC_DEI_SCALER;
  676. if (fmt->fourcc != V4L2_PIX_FMT_NV12)
  677. val |= VPE_DS_BYPASS;
  678. mmr_adb->out_fmt_reg[0] = val;
  679. ctx->load_mmrs = true;
  680. }
  681. /*
  682. * Set the de-interlacer shadow register values
  683. */
  684. static void set_dei_regs(struct vpe_ctx *ctx)
  685. {
  686. struct vpe_mmr_adb *mmr_adb = ctx->mmr_adb.addr;
  687. struct vpe_q_data *s_q_data = &ctx->q_data[Q_DATA_SRC];
  688. unsigned int src_h = s_q_data->c_rect.height;
  689. unsigned int src_w = s_q_data->c_rect.width;
  690. u32 *dei_mmr0 = &mmr_adb->dei_regs[0];
  691. bool deinterlace = true;
  692. u32 val = 0;
  693. /*
  694. * according to TRM, we should set DEI in progressive bypass mode when
  695. * the input content is progressive, however, DEI is bypassed correctly
  696. * for both progressive and interlace content in interlace bypass mode.
  697. * It has been recommended not to use progressive bypass mode.
  698. */
  699. if (!(s_q_data->flags & Q_IS_INTERLACED) || !ctx->deinterlacing) {
  700. deinterlace = false;
  701. val = VPE_DEI_INTERLACE_BYPASS;
  702. }
  703. src_h = deinterlace ? src_h * 2 : src_h;
  704. val |= (src_h << VPE_DEI_HEIGHT_SHIFT) |
  705. (src_w << VPE_DEI_WIDTH_SHIFT) |
  706. VPE_DEI_FIELD_FLUSH;
  707. *dei_mmr0 = val;
  708. ctx->load_mmrs = true;
  709. }
  710. static void set_dei_shadow_registers(struct vpe_ctx *ctx)
  711. {
  712. struct vpe_mmr_adb *mmr_adb = ctx->mmr_adb.addr;
  713. u32 *dei_mmr = &mmr_adb->dei_regs[0];
  714. const struct vpe_dei_regs *cur = &dei_regs;
  715. dei_mmr[2] = cur->mdt_spacial_freq_thr_reg;
  716. dei_mmr[3] = cur->edi_config_reg;
  717. dei_mmr[4] = cur->edi_lut_reg0;
  718. dei_mmr[5] = cur->edi_lut_reg1;
  719. dei_mmr[6] = cur->edi_lut_reg2;
  720. dei_mmr[7] = cur->edi_lut_reg3;
  721. ctx->load_mmrs = true;
  722. }
  723. static void config_edi_input_mode(struct vpe_ctx *ctx, int mode)
  724. {
  725. struct vpe_mmr_adb *mmr_adb = ctx->mmr_adb.addr;
  726. u32 *edi_config_reg = &mmr_adb->dei_regs[3];
  727. if (mode & 0x2)
  728. write_field(edi_config_reg, 1, 1, 2); /* EDI_ENABLE_3D */
  729. if (mode & 0x3)
  730. write_field(edi_config_reg, 1, 1, 3); /* EDI_CHROMA_3D */
  731. write_field(edi_config_reg, mode, VPE_EDI_INP_MODE_MASK,
  732. VPE_EDI_INP_MODE_SHIFT);
  733. ctx->load_mmrs = true;
  734. }
  735. /*
  736. * Set the shadow registers whose values are modified when either the
  737. * source or destination format is changed.
  738. */
  739. static int set_srcdst_params(struct vpe_ctx *ctx)
  740. {
  741. struct vpe_q_data *s_q_data = &ctx->q_data[Q_DATA_SRC];
  742. struct vpe_q_data *d_q_data = &ctx->q_data[Q_DATA_DST];
  743. struct vpe_mmr_adb *mmr_adb = ctx->mmr_adb.addr;
  744. unsigned int src_w = s_q_data->c_rect.width;
  745. unsigned int src_h = s_q_data->c_rect.height;
  746. unsigned int dst_w = d_q_data->c_rect.width;
  747. unsigned int dst_h = d_q_data->c_rect.height;
  748. size_t mv_buf_size;
  749. int ret;
  750. ctx->sequence = 0;
  751. ctx->field = V4L2_FIELD_TOP;
  752. if ((s_q_data->flags & Q_IS_INTERLACED) &&
  753. !(d_q_data->flags & Q_IS_INTERLACED)) {
  754. int bytes_per_line;
  755. const struct vpdma_data_format *mv =
  756. &vpdma_misc_fmts[VPDMA_DATA_FMT_MV];
  757. /*
  758. * we make sure that the source image has a 16 byte aligned
  759. * stride, we need to do the same for the motion vector buffer
  760. * by aligning it's stride to the next 16 byte boundry. this
  761. * extra space will not be used by the de-interlacer, but will
  762. * ensure that vpdma operates correctly
  763. */
  764. bytes_per_line = ALIGN((s_q_data->width * mv->depth) >> 3,
  765. VPDMA_STRIDE_ALIGN);
  766. mv_buf_size = bytes_per_line * s_q_data->height;
  767. ctx->deinterlacing = true;
  768. src_h <<= 1;
  769. } else {
  770. ctx->deinterlacing = false;
  771. mv_buf_size = 0;
  772. }
  773. free_vbs(ctx);
  774. ctx->src_vbs[2] = ctx->src_vbs[1] = ctx->src_vbs[0] = NULL;
  775. ret = realloc_mv_buffers(ctx, mv_buf_size);
  776. if (ret)
  777. return ret;
  778. set_cfg_modes(ctx);
  779. set_dei_regs(ctx);
  780. csc_set_coeff(ctx->dev->csc, &mmr_adb->csc_regs[0],
  781. s_q_data->colorspace, d_q_data->colorspace);
  782. sc_set_hs_coeffs(ctx->dev->sc, ctx->sc_coeff_h.addr, src_w, dst_w);
  783. sc_set_vs_coeffs(ctx->dev->sc, ctx->sc_coeff_v.addr, src_h, dst_h);
  784. sc_config_scaler(ctx->dev->sc, &mmr_adb->sc_regs0[0],
  785. &mmr_adb->sc_regs8[0], &mmr_adb->sc_regs17[0],
  786. src_w, src_h, dst_w, dst_h);
  787. return 0;
  788. }
  789. /*
  790. * Return the vpe_ctx structure for a given struct file
  791. */
  792. static struct vpe_ctx *file2ctx(struct file *file)
  793. {
  794. return container_of(file->private_data, struct vpe_ctx, fh);
  795. }
  796. /*
  797. * mem2mem callbacks
  798. */
  799. /**
  800. * job_ready() - check whether an instance is ready to be scheduled to run
  801. */
  802. static int job_ready(void *priv)
  803. {
  804. struct vpe_ctx *ctx = priv;
  805. /*
  806. * This check is needed as this might be called directly from driver
  807. * When called by m2m framework, this will always satisfy, but when
  808. * called from vpe_irq, this might fail. (src stream with zero buffers)
  809. */
  810. if (v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx) <= 0 ||
  811. v4l2_m2m_num_dst_bufs_ready(ctx->fh.m2m_ctx) <= 0)
  812. return 0;
  813. return 1;
  814. }
  815. static void job_abort(void *priv)
  816. {
  817. struct vpe_ctx *ctx = priv;
  818. /* Will cancel the transaction in the next interrupt handler */
  819. ctx->aborting = 1;
  820. }
  821. /*
  822. * Lock access to the device
  823. */
  824. static void vpe_lock(void *priv)
  825. {
  826. struct vpe_ctx *ctx = priv;
  827. struct vpe_dev *dev = ctx->dev;
  828. mutex_lock(&dev->dev_mutex);
  829. }
  830. static void vpe_unlock(void *priv)
  831. {
  832. struct vpe_ctx *ctx = priv;
  833. struct vpe_dev *dev = ctx->dev;
  834. mutex_unlock(&dev->dev_mutex);
  835. }
  836. static void vpe_dump_regs(struct vpe_dev *dev)
  837. {
  838. #define DUMPREG(r) vpe_dbg(dev, "%-35s %08x\n", #r, read_reg(dev, VPE_##r))
  839. vpe_dbg(dev, "VPE Registers:\n");
  840. DUMPREG(PID);
  841. DUMPREG(SYSCONFIG);
  842. DUMPREG(INT0_STATUS0_RAW);
  843. DUMPREG(INT0_STATUS0);
  844. DUMPREG(INT0_ENABLE0);
  845. DUMPREG(INT0_STATUS1_RAW);
  846. DUMPREG(INT0_STATUS1);
  847. DUMPREG(INT0_ENABLE1);
  848. DUMPREG(CLK_ENABLE);
  849. DUMPREG(CLK_RESET);
  850. DUMPREG(CLK_FORMAT_SELECT);
  851. DUMPREG(CLK_RANGE_MAP);
  852. DUMPREG(US1_R0);
  853. DUMPREG(US1_R1);
  854. DUMPREG(US1_R2);
  855. DUMPREG(US1_R3);
  856. DUMPREG(US1_R4);
  857. DUMPREG(US1_R5);
  858. DUMPREG(US1_R6);
  859. DUMPREG(US1_R7);
  860. DUMPREG(US2_R0);
  861. DUMPREG(US2_R1);
  862. DUMPREG(US2_R2);
  863. DUMPREG(US2_R3);
  864. DUMPREG(US2_R4);
  865. DUMPREG(US2_R5);
  866. DUMPREG(US2_R6);
  867. DUMPREG(US2_R7);
  868. DUMPREG(US3_R0);
  869. DUMPREG(US3_R1);
  870. DUMPREG(US3_R2);
  871. DUMPREG(US3_R3);
  872. DUMPREG(US3_R4);
  873. DUMPREG(US3_R5);
  874. DUMPREG(US3_R6);
  875. DUMPREG(US3_R7);
  876. DUMPREG(DEI_FRAME_SIZE);
  877. DUMPREG(MDT_BYPASS);
  878. DUMPREG(MDT_SF_THRESHOLD);
  879. DUMPREG(EDI_CONFIG);
  880. DUMPREG(DEI_EDI_LUT_R0);
  881. DUMPREG(DEI_EDI_LUT_R1);
  882. DUMPREG(DEI_EDI_LUT_R2);
  883. DUMPREG(DEI_EDI_LUT_R3);
  884. DUMPREG(DEI_FMD_WINDOW_R0);
  885. DUMPREG(DEI_FMD_WINDOW_R1);
  886. DUMPREG(DEI_FMD_CONTROL_R0);
  887. DUMPREG(DEI_FMD_CONTROL_R1);
  888. DUMPREG(DEI_FMD_STATUS_R0);
  889. DUMPREG(DEI_FMD_STATUS_R1);
  890. DUMPREG(DEI_FMD_STATUS_R2);
  891. #undef DUMPREG
  892. sc_dump_regs(dev->sc);
  893. csc_dump_regs(dev->csc);
  894. }
  895. static void add_out_dtd(struct vpe_ctx *ctx, int port)
  896. {
  897. struct vpe_q_data *q_data = &ctx->q_data[Q_DATA_DST];
  898. const struct vpe_port_data *p_data = &port_data[port];
  899. struct vb2_buffer *vb = &ctx->dst_vb->vb2_buf;
  900. struct vpe_fmt *fmt = q_data->fmt;
  901. const struct vpdma_data_format *vpdma_fmt;
  902. int mv_buf_selector = !ctx->src_mv_buf_selector;
  903. dma_addr_t dma_addr;
  904. u32 flags = 0;
  905. if (port == VPE_PORT_MV_OUT) {
  906. vpdma_fmt = &vpdma_misc_fmts[VPDMA_DATA_FMT_MV];
  907. dma_addr = ctx->mv_buf_dma[mv_buf_selector];
  908. q_data = &ctx->q_data[Q_DATA_SRC];
  909. } else {
  910. /* to incorporate interleaved formats */
  911. int plane = fmt->coplanar ? p_data->vb_part : 0;
  912. vpdma_fmt = fmt->vpdma_fmt[plane];
  913. dma_addr = vb2_dma_contig_plane_dma_addr(vb, plane);
  914. if (!dma_addr) {
  915. vpe_err(ctx->dev,
  916. "acquiring output buffer(%d) dma_addr failed\n",
  917. port);
  918. return;
  919. }
  920. }
  921. if (q_data->flags & Q_DATA_FRAME_1D)
  922. flags |= VPDMA_DATA_FRAME_1D;
  923. if (q_data->flags & Q_DATA_MODE_TILED)
  924. flags |= VPDMA_DATA_MODE_TILED;
  925. vpdma_set_max_size(ctx->dev->vpdma, VPDMA_MAX_SIZE1,
  926. MAX_W, MAX_H);
  927. vpdma_add_out_dtd(&ctx->desc_list, q_data->width, &q_data->c_rect,
  928. vpdma_fmt, dma_addr, MAX_OUT_WIDTH_REG1,
  929. MAX_OUT_HEIGHT_REG1, p_data->channel, flags);
  930. }
  931. static void add_in_dtd(struct vpe_ctx *ctx, int port)
  932. {
  933. struct vpe_q_data *q_data = &ctx->q_data[Q_DATA_SRC];
  934. const struct vpe_port_data *p_data = &port_data[port];
  935. struct vb2_buffer *vb = &ctx->src_vbs[p_data->vb_index]->vb2_buf;
  936. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  937. struct vpe_fmt *fmt = q_data->fmt;
  938. const struct vpdma_data_format *vpdma_fmt;
  939. int mv_buf_selector = ctx->src_mv_buf_selector;
  940. int field = vbuf->field == V4L2_FIELD_BOTTOM;
  941. int frame_width, frame_height;
  942. dma_addr_t dma_addr;
  943. u32 flags = 0;
  944. if (port == VPE_PORT_MV_IN) {
  945. vpdma_fmt = &vpdma_misc_fmts[VPDMA_DATA_FMT_MV];
  946. dma_addr = ctx->mv_buf_dma[mv_buf_selector];
  947. } else {
  948. /* to incorporate interleaved formats */
  949. int plane = fmt->coplanar ? p_data->vb_part : 0;
  950. vpdma_fmt = fmt->vpdma_fmt[plane];
  951. dma_addr = vb2_dma_contig_plane_dma_addr(vb, plane);
  952. if (!dma_addr) {
  953. vpe_err(ctx->dev,
  954. "acquiring input buffer(%d) dma_addr failed\n",
  955. port);
  956. return;
  957. }
  958. if (q_data->flags & Q_DATA_INTERLACED_SEQ_TB) {
  959. /*
  960. * Use top or bottom field from same vb alternately
  961. * f,f-1,f-2 = TBT when seq is even
  962. * f,f-1,f-2 = BTB when seq is odd
  963. */
  964. field = (p_data->vb_index + (ctx->sequence % 2)) % 2;
  965. if (field) {
  966. /*
  967. * bottom field of a SEQ_TB buffer
  968. * Skip the top field data by
  969. */
  970. int height = q_data->height / 2;
  971. int bpp = fmt->fourcc == V4L2_PIX_FMT_NV12 ?
  972. 1 : (vpdma_fmt->depth >> 3);
  973. if (plane)
  974. height /= 2;
  975. dma_addr += q_data->width * height * bpp;
  976. }
  977. }
  978. }
  979. if (q_data->flags & Q_DATA_FRAME_1D)
  980. flags |= VPDMA_DATA_FRAME_1D;
  981. if (q_data->flags & Q_DATA_MODE_TILED)
  982. flags |= VPDMA_DATA_MODE_TILED;
  983. frame_width = q_data->c_rect.width;
  984. frame_height = q_data->c_rect.height;
  985. if (p_data->vb_part && fmt->fourcc == V4L2_PIX_FMT_NV12)
  986. frame_height /= 2;
  987. vpdma_add_in_dtd(&ctx->desc_list, q_data->width, &q_data->c_rect,
  988. vpdma_fmt, dma_addr, p_data->channel, field, flags, frame_width,
  989. frame_height, 0, 0);
  990. }
  991. /*
  992. * Enable the expected IRQ sources
  993. */
  994. static void enable_irqs(struct vpe_ctx *ctx)
  995. {
  996. write_reg(ctx->dev, VPE_INT0_ENABLE0_SET, VPE_INT0_LIST0_COMPLETE);
  997. write_reg(ctx->dev, VPE_INT0_ENABLE1_SET, VPE_DEI_ERROR_INT |
  998. VPE_DS1_UV_ERROR_INT);
  999. vpdma_enable_list_complete_irq(ctx->dev->vpdma, 0, 0, true);
  1000. }
  1001. static void disable_irqs(struct vpe_ctx *ctx)
  1002. {
  1003. write_reg(ctx->dev, VPE_INT0_ENABLE0_CLR, 0xffffffff);
  1004. write_reg(ctx->dev, VPE_INT0_ENABLE1_CLR, 0xffffffff);
  1005. vpdma_enable_list_complete_irq(ctx->dev->vpdma, 0, 0, false);
  1006. }
  1007. /* device_run() - prepares and starts the device
  1008. *
  1009. * This function is only called when both the source and destination
  1010. * buffers are in place.
  1011. */
  1012. static void device_run(void *priv)
  1013. {
  1014. struct vpe_ctx *ctx = priv;
  1015. struct sc_data *sc = ctx->dev->sc;
  1016. struct vpe_q_data *d_q_data = &ctx->q_data[Q_DATA_DST];
  1017. struct vpe_q_data *s_q_data = &ctx->q_data[Q_DATA_SRC];
  1018. if (ctx->deinterlacing && s_q_data->flags & Q_DATA_INTERLACED_SEQ_TB &&
  1019. ctx->sequence % 2 == 0) {
  1020. /* When using SEQ_TB buffers, When using it first time,
  1021. * No need to remove the buffer as the next field is present
  1022. * in the same buffer. (so that job_ready won't fail)
  1023. * It will be removed when using bottom field
  1024. */
  1025. ctx->src_vbs[0] = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
  1026. WARN_ON(ctx->src_vbs[0] == NULL);
  1027. } else {
  1028. ctx->src_vbs[0] = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
  1029. WARN_ON(ctx->src_vbs[0] == NULL);
  1030. }
  1031. ctx->dst_vb = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
  1032. WARN_ON(ctx->dst_vb == NULL);
  1033. if (ctx->deinterlacing) {
  1034. if (ctx->src_vbs[2] == NULL) {
  1035. ctx->src_vbs[2] = ctx->src_vbs[0];
  1036. WARN_ON(ctx->src_vbs[2] == NULL);
  1037. ctx->src_vbs[1] = ctx->src_vbs[0];
  1038. WARN_ON(ctx->src_vbs[1] == NULL);
  1039. }
  1040. /*
  1041. * we have output the first 2 frames through line average, we
  1042. * now switch to EDI de-interlacer
  1043. */
  1044. if (ctx->sequence == 2)
  1045. config_edi_input_mode(ctx, 0x3); /* EDI (Y + UV) */
  1046. }
  1047. /* config descriptors */
  1048. if (ctx->dev->loaded_mmrs != ctx->mmr_adb.dma_addr || ctx->load_mmrs) {
  1049. vpdma_map_desc_buf(ctx->dev->vpdma, &ctx->mmr_adb);
  1050. vpdma_add_cfd_adb(&ctx->desc_list, CFD_MMR_CLIENT, &ctx->mmr_adb);
  1051. set_line_modes(ctx);
  1052. ctx->dev->loaded_mmrs = ctx->mmr_adb.dma_addr;
  1053. ctx->load_mmrs = false;
  1054. }
  1055. if (sc->loaded_coeff_h != ctx->sc_coeff_h.dma_addr ||
  1056. sc->load_coeff_h) {
  1057. vpdma_map_desc_buf(ctx->dev->vpdma, &ctx->sc_coeff_h);
  1058. vpdma_add_cfd_block(&ctx->desc_list, CFD_SC_CLIENT,
  1059. &ctx->sc_coeff_h, 0);
  1060. sc->loaded_coeff_h = ctx->sc_coeff_h.dma_addr;
  1061. sc->load_coeff_h = false;
  1062. }
  1063. if (sc->loaded_coeff_v != ctx->sc_coeff_v.dma_addr ||
  1064. sc->load_coeff_v) {
  1065. vpdma_map_desc_buf(ctx->dev->vpdma, &ctx->sc_coeff_v);
  1066. vpdma_add_cfd_block(&ctx->desc_list, CFD_SC_CLIENT,
  1067. &ctx->sc_coeff_v, SC_COEF_SRAM_SIZE >> 4);
  1068. sc->loaded_coeff_v = ctx->sc_coeff_v.dma_addr;
  1069. sc->load_coeff_v = false;
  1070. }
  1071. /* output data descriptors */
  1072. if (ctx->deinterlacing)
  1073. add_out_dtd(ctx, VPE_PORT_MV_OUT);
  1074. if (d_q_data->colorspace == V4L2_COLORSPACE_SRGB) {
  1075. add_out_dtd(ctx, VPE_PORT_RGB_OUT);
  1076. } else {
  1077. add_out_dtd(ctx, VPE_PORT_LUMA_OUT);
  1078. if (d_q_data->fmt->coplanar)
  1079. add_out_dtd(ctx, VPE_PORT_CHROMA_OUT);
  1080. }
  1081. /* input data descriptors */
  1082. if (ctx->deinterlacing) {
  1083. add_in_dtd(ctx, VPE_PORT_LUMA3_IN);
  1084. add_in_dtd(ctx, VPE_PORT_CHROMA3_IN);
  1085. add_in_dtd(ctx, VPE_PORT_LUMA2_IN);
  1086. add_in_dtd(ctx, VPE_PORT_CHROMA2_IN);
  1087. }
  1088. add_in_dtd(ctx, VPE_PORT_LUMA1_IN);
  1089. add_in_dtd(ctx, VPE_PORT_CHROMA1_IN);
  1090. if (ctx->deinterlacing)
  1091. add_in_dtd(ctx, VPE_PORT_MV_IN);
  1092. /* sync on channel control descriptors for input ports */
  1093. vpdma_add_sync_on_channel_ctd(&ctx->desc_list, VPE_CHAN_LUMA1_IN);
  1094. vpdma_add_sync_on_channel_ctd(&ctx->desc_list, VPE_CHAN_CHROMA1_IN);
  1095. if (ctx->deinterlacing) {
  1096. vpdma_add_sync_on_channel_ctd(&ctx->desc_list,
  1097. VPE_CHAN_LUMA2_IN);
  1098. vpdma_add_sync_on_channel_ctd(&ctx->desc_list,
  1099. VPE_CHAN_CHROMA2_IN);
  1100. vpdma_add_sync_on_channel_ctd(&ctx->desc_list,
  1101. VPE_CHAN_LUMA3_IN);
  1102. vpdma_add_sync_on_channel_ctd(&ctx->desc_list,
  1103. VPE_CHAN_CHROMA3_IN);
  1104. vpdma_add_sync_on_channel_ctd(&ctx->desc_list, VPE_CHAN_MV_IN);
  1105. }
  1106. /* sync on channel control descriptors for output ports */
  1107. if (d_q_data->colorspace == V4L2_COLORSPACE_SRGB) {
  1108. vpdma_add_sync_on_channel_ctd(&ctx->desc_list,
  1109. VPE_CHAN_RGB_OUT);
  1110. } else {
  1111. vpdma_add_sync_on_channel_ctd(&ctx->desc_list,
  1112. VPE_CHAN_LUMA_OUT);
  1113. if (d_q_data->fmt->coplanar)
  1114. vpdma_add_sync_on_channel_ctd(&ctx->desc_list,
  1115. VPE_CHAN_CHROMA_OUT);
  1116. }
  1117. if (ctx->deinterlacing)
  1118. vpdma_add_sync_on_channel_ctd(&ctx->desc_list, VPE_CHAN_MV_OUT);
  1119. enable_irqs(ctx);
  1120. vpdma_map_desc_buf(ctx->dev->vpdma, &ctx->desc_list.buf);
  1121. vpdma_submit_descs(ctx->dev->vpdma, &ctx->desc_list, 0);
  1122. }
  1123. static void dei_error(struct vpe_ctx *ctx)
  1124. {
  1125. dev_warn(ctx->dev->v4l2_dev.dev,
  1126. "received DEI error interrupt\n");
  1127. }
  1128. static void ds1_uv_error(struct vpe_ctx *ctx)
  1129. {
  1130. dev_warn(ctx->dev->v4l2_dev.dev,
  1131. "received downsampler error interrupt\n");
  1132. }
  1133. static irqreturn_t vpe_irq(int irq_vpe, void *data)
  1134. {
  1135. struct vpe_dev *dev = (struct vpe_dev *)data;
  1136. struct vpe_ctx *ctx;
  1137. struct vpe_q_data *d_q_data;
  1138. struct vb2_v4l2_buffer *s_vb, *d_vb;
  1139. unsigned long flags;
  1140. u32 irqst0, irqst1;
  1141. bool list_complete = false;
  1142. irqst0 = read_reg(dev, VPE_INT0_STATUS0);
  1143. if (irqst0) {
  1144. write_reg(dev, VPE_INT0_STATUS0_CLR, irqst0);
  1145. vpe_dbg(dev, "INT0_STATUS0 = 0x%08x\n", irqst0);
  1146. }
  1147. irqst1 = read_reg(dev, VPE_INT0_STATUS1);
  1148. if (irqst1) {
  1149. write_reg(dev, VPE_INT0_STATUS1_CLR, irqst1);
  1150. vpe_dbg(dev, "INT0_STATUS1 = 0x%08x\n", irqst1);
  1151. }
  1152. ctx = v4l2_m2m_get_curr_priv(dev->m2m_dev);
  1153. if (!ctx) {
  1154. vpe_err(dev, "instance released before end of transaction\n");
  1155. goto handled;
  1156. }
  1157. if (irqst1) {
  1158. if (irqst1 & VPE_DEI_ERROR_INT) {
  1159. irqst1 &= ~VPE_DEI_ERROR_INT;
  1160. dei_error(ctx);
  1161. }
  1162. if (irqst1 & VPE_DS1_UV_ERROR_INT) {
  1163. irqst1 &= ~VPE_DS1_UV_ERROR_INT;
  1164. ds1_uv_error(ctx);
  1165. }
  1166. }
  1167. if (irqst0) {
  1168. if (irqst0 & VPE_INT0_LIST0_COMPLETE)
  1169. vpdma_clear_list_stat(ctx->dev->vpdma, 0, 0);
  1170. irqst0 &= ~(VPE_INT0_LIST0_COMPLETE);
  1171. list_complete = true;
  1172. }
  1173. if (irqst0 | irqst1) {
  1174. dev_warn(dev->v4l2_dev.dev, "Unexpected interrupt: INT0_STATUS0 = 0x%08x, INT0_STATUS1 = 0x%08x\n",
  1175. irqst0, irqst1);
  1176. }
  1177. /*
  1178. * Setup next operation only when list complete IRQ occurs
  1179. * otherwise, skip the following code
  1180. */
  1181. if (!list_complete)
  1182. goto handled;
  1183. disable_irqs(ctx);
  1184. vpdma_unmap_desc_buf(dev->vpdma, &ctx->desc_list.buf);
  1185. vpdma_unmap_desc_buf(dev->vpdma, &ctx->mmr_adb);
  1186. vpdma_unmap_desc_buf(dev->vpdma, &ctx->sc_coeff_h);
  1187. vpdma_unmap_desc_buf(dev->vpdma, &ctx->sc_coeff_v);
  1188. vpdma_reset_desc_list(&ctx->desc_list);
  1189. /* the previous dst mv buffer becomes the next src mv buffer */
  1190. ctx->src_mv_buf_selector = !ctx->src_mv_buf_selector;
  1191. if (ctx->aborting)
  1192. goto finished;
  1193. s_vb = ctx->src_vbs[0];
  1194. d_vb = ctx->dst_vb;
  1195. d_vb->flags = s_vb->flags;
  1196. d_vb->vb2_buf.timestamp = s_vb->vb2_buf.timestamp;
  1197. if (s_vb->flags & V4L2_BUF_FLAG_TIMECODE)
  1198. d_vb->timecode = s_vb->timecode;
  1199. d_vb->sequence = ctx->sequence;
  1200. d_q_data = &ctx->q_data[Q_DATA_DST];
  1201. if (d_q_data->flags & Q_IS_INTERLACED) {
  1202. d_vb->field = ctx->field;
  1203. if (ctx->field == V4L2_FIELD_BOTTOM) {
  1204. ctx->sequence++;
  1205. ctx->field = V4L2_FIELD_TOP;
  1206. } else {
  1207. WARN_ON(ctx->field != V4L2_FIELD_TOP);
  1208. ctx->field = V4L2_FIELD_BOTTOM;
  1209. }
  1210. } else {
  1211. d_vb->field = V4L2_FIELD_NONE;
  1212. ctx->sequence++;
  1213. }
  1214. if (ctx->deinterlacing) {
  1215. /*
  1216. * Allow source buffer to be dequeued only if it won't be used
  1217. * in the next iteration. All vbs are initialized to first
  1218. * buffer and we are shifting buffers every iteration, for the
  1219. * first two iterations, no buffer will be dequeued.
  1220. * This ensures that driver will keep (n-2)th (n-1)th and (n)th
  1221. * field when deinterlacing is enabled
  1222. */
  1223. if (ctx->src_vbs[2] != ctx->src_vbs[1])
  1224. s_vb = ctx->src_vbs[2];
  1225. else
  1226. s_vb = NULL;
  1227. }
  1228. spin_lock_irqsave(&dev->lock, flags);
  1229. if (s_vb)
  1230. v4l2_m2m_buf_done(s_vb, VB2_BUF_STATE_DONE);
  1231. v4l2_m2m_buf_done(d_vb, VB2_BUF_STATE_DONE);
  1232. spin_unlock_irqrestore(&dev->lock, flags);
  1233. if (ctx->deinterlacing) {
  1234. ctx->src_vbs[2] = ctx->src_vbs[1];
  1235. ctx->src_vbs[1] = ctx->src_vbs[0];
  1236. }
  1237. /*
  1238. * Since the vb2_buf_done has already been called fir therse
  1239. * buffer we can now NULL them out so that we won't try
  1240. * to clean out stray pointer later on.
  1241. */
  1242. ctx->src_vbs[0] = NULL;
  1243. ctx->dst_vb = NULL;
  1244. ctx->bufs_completed++;
  1245. if (ctx->bufs_completed < ctx->bufs_per_job && job_ready(ctx)) {
  1246. device_run(ctx);
  1247. goto handled;
  1248. }
  1249. finished:
  1250. vpe_dbg(ctx->dev, "finishing transaction\n");
  1251. ctx->bufs_completed = 0;
  1252. v4l2_m2m_job_finish(dev->m2m_dev, ctx->fh.m2m_ctx);
  1253. handled:
  1254. return IRQ_HANDLED;
  1255. }
  1256. /*
  1257. * video ioctls
  1258. */
  1259. static int vpe_querycap(struct file *file, void *priv,
  1260. struct v4l2_capability *cap)
  1261. {
  1262. strncpy(cap->driver, VPE_MODULE_NAME, sizeof(cap->driver) - 1);
  1263. strncpy(cap->card, VPE_MODULE_NAME, sizeof(cap->card) - 1);
  1264. snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
  1265. VPE_MODULE_NAME);
  1266. cap->device_caps = V4L2_CAP_VIDEO_M2M_MPLANE | V4L2_CAP_STREAMING;
  1267. cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
  1268. return 0;
  1269. }
  1270. static int __enum_fmt(struct v4l2_fmtdesc *f, u32 type)
  1271. {
  1272. int i, index;
  1273. struct vpe_fmt *fmt = NULL;
  1274. index = 0;
  1275. for (i = 0; i < ARRAY_SIZE(vpe_formats); ++i) {
  1276. if (vpe_formats[i].types & type) {
  1277. if (index == f->index) {
  1278. fmt = &vpe_formats[i];
  1279. break;
  1280. }
  1281. index++;
  1282. }
  1283. }
  1284. if (!fmt)
  1285. return -EINVAL;
  1286. strncpy(f->description, fmt->name, sizeof(f->description) - 1);
  1287. f->pixelformat = fmt->fourcc;
  1288. return 0;
  1289. }
  1290. static int vpe_enum_fmt(struct file *file, void *priv,
  1291. struct v4l2_fmtdesc *f)
  1292. {
  1293. if (V4L2_TYPE_IS_OUTPUT(f->type))
  1294. return __enum_fmt(f, VPE_FMT_TYPE_OUTPUT);
  1295. return __enum_fmt(f, VPE_FMT_TYPE_CAPTURE);
  1296. }
  1297. static int vpe_g_fmt(struct file *file, void *priv, struct v4l2_format *f)
  1298. {
  1299. struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp;
  1300. struct vpe_ctx *ctx = file2ctx(file);
  1301. struct vb2_queue *vq;
  1302. struct vpe_q_data *q_data;
  1303. int i;
  1304. vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
  1305. if (!vq)
  1306. return -EINVAL;
  1307. q_data = get_q_data(ctx, f->type);
  1308. pix->width = q_data->width;
  1309. pix->height = q_data->height;
  1310. pix->pixelformat = q_data->fmt->fourcc;
  1311. pix->field = q_data->field;
  1312. if (V4L2_TYPE_IS_OUTPUT(f->type)) {
  1313. pix->colorspace = q_data->colorspace;
  1314. } else {
  1315. struct vpe_q_data *s_q_data;
  1316. /* get colorspace from the source queue */
  1317. s_q_data = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
  1318. pix->colorspace = s_q_data->colorspace;
  1319. }
  1320. pix->num_planes = q_data->fmt->coplanar ? 2 : 1;
  1321. for (i = 0; i < pix->num_planes; i++) {
  1322. pix->plane_fmt[i].bytesperline = q_data->bytesperline[i];
  1323. pix->plane_fmt[i].sizeimage = q_data->sizeimage[i];
  1324. }
  1325. return 0;
  1326. }
  1327. static int __vpe_try_fmt(struct vpe_ctx *ctx, struct v4l2_format *f,
  1328. struct vpe_fmt *fmt, int type)
  1329. {
  1330. struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp;
  1331. struct v4l2_plane_pix_format *plane_fmt;
  1332. unsigned int w_align;
  1333. int i, depth, depth_bytes, height;
  1334. if (!fmt || !(fmt->types & type)) {
  1335. vpe_err(ctx->dev, "Fourcc format (0x%08x) invalid.\n",
  1336. pix->pixelformat);
  1337. return -EINVAL;
  1338. }
  1339. if (pix->field != V4L2_FIELD_NONE && pix->field != V4L2_FIELD_ALTERNATE
  1340. && pix->field != V4L2_FIELD_SEQ_TB)
  1341. pix->field = V4L2_FIELD_NONE;
  1342. depth = fmt->vpdma_fmt[VPE_LUMA]->depth;
  1343. /*
  1344. * the line stride should 16 byte aligned for VPDMA to work, based on
  1345. * the bytes per pixel, figure out how much the width should be aligned
  1346. * to make sure line stride is 16 byte aligned
  1347. */
  1348. depth_bytes = depth >> 3;
  1349. if (depth_bytes == 3)
  1350. /*
  1351. * if bpp is 3(as in some RGB formats), the pixel width doesn't
  1352. * really help in ensuring line stride is 16 byte aligned
  1353. */
  1354. w_align = 4;
  1355. else
  1356. /*
  1357. * for the remainder bpp(4, 2 and 1), the pixel width alignment
  1358. * can ensure a line stride alignment of 16 bytes. For example,
  1359. * if bpp is 2, then the line stride can be 16 byte aligned if
  1360. * the width is 8 byte aligned
  1361. */
  1362. w_align = order_base_2(VPDMA_DESC_ALIGN / depth_bytes);
  1363. v4l_bound_align_image(&pix->width, MIN_W, MAX_W, w_align,
  1364. &pix->height, MIN_H, MAX_H, H_ALIGN,
  1365. S_ALIGN);
  1366. pix->num_planes = fmt->coplanar ? 2 : 1;
  1367. pix->pixelformat = fmt->fourcc;
  1368. /*
  1369. * For the actual image parameters, we need to consider the field
  1370. * height of the image for SEQ_TB buffers.
  1371. */
  1372. if (pix->field == V4L2_FIELD_SEQ_TB)
  1373. height = pix->height / 2;
  1374. else
  1375. height = pix->height;
  1376. if (!pix->colorspace) {
  1377. if (fmt->fourcc == V4L2_PIX_FMT_RGB24 ||
  1378. fmt->fourcc == V4L2_PIX_FMT_BGR24 ||
  1379. fmt->fourcc == V4L2_PIX_FMT_RGB32 ||
  1380. fmt->fourcc == V4L2_PIX_FMT_BGR32) {
  1381. pix->colorspace = V4L2_COLORSPACE_SRGB;
  1382. } else {
  1383. if (height > 1280) /* HD */
  1384. pix->colorspace = V4L2_COLORSPACE_REC709;
  1385. else /* SD */
  1386. pix->colorspace = V4L2_COLORSPACE_SMPTE170M;
  1387. }
  1388. }
  1389. memset(pix->reserved, 0, sizeof(pix->reserved));
  1390. for (i = 0; i < pix->num_planes; i++) {
  1391. plane_fmt = &pix->plane_fmt[i];
  1392. depth = fmt->vpdma_fmt[i]->depth;
  1393. if (i == VPE_LUMA)
  1394. plane_fmt->bytesperline = (pix->width * depth) >> 3;
  1395. else
  1396. plane_fmt->bytesperline = pix->width;
  1397. plane_fmt->sizeimage =
  1398. (pix->height * pix->width * depth) >> 3;
  1399. memset(plane_fmt->reserved, 0, sizeof(plane_fmt->reserved));
  1400. }
  1401. return 0;
  1402. }
  1403. static int vpe_try_fmt(struct file *file, void *priv, struct v4l2_format *f)
  1404. {
  1405. struct vpe_ctx *ctx = file2ctx(file);
  1406. struct vpe_fmt *fmt = find_format(f);
  1407. if (V4L2_TYPE_IS_OUTPUT(f->type))
  1408. return __vpe_try_fmt(ctx, f, fmt, VPE_FMT_TYPE_OUTPUT);
  1409. else
  1410. return __vpe_try_fmt(ctx, f, fmt, VPE_FMT_TYPE_CAPTURE);
  1411. }
  1412. static int __vpe_s_fmt(struct vpe_ctx *ctx, struct v4l2_format *f)
  1413. {
  1414. struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp;
  1415. struct v4l2_plane_pix_format *plane_fmt;
  1416. struct vpe_q_data *q_data;
  1417. struct vb2_queue *vq;
  1418. int i;
  1419. vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
  1420. if (!vq)
  1421. return -EINVAL;
  1422. if (vb2_is_busy(vq)) {
  1423. vpe_err(ctx->dev, "queue busy\n");
  1424. return -EBUSY;
  1425. }
  1426. q_data = get_q_data(ctx, f->type);
  1427. if (!q_data)
  1428. return -EINVAL;
  1429. q_data->fmt = find_format(f);
  1430. q_data->width = pix->width;
  1431. q_data->height = pix->height;
  1432. q_data->colorspace = pix->colorspace;
  1433. q_data->field = pix->field;
  1434. for (i = 0; i < pix->num_planes; i++) {
  1435. plane_fmt = &pix->plane_fmt[i];
  1436. q_data->bytesperline[i] = plane_fmt->bytesperline;
  1437. q_data->sizeimage[i] = plane_fmt->sizeimage;
  1438. }
  1439. q_data->c_rect.left = 0;
  1440. q_data->c_rect.top = 0;
  1441. q_data->c_rect.width = q_data->width;
  1442. q_data->c_rect.height = q_data->height;
  1443. if (q_data->field == V4L2_FIELD_ALTERNATE)
  1444. q_data->flags |= Q_DATA_INTERLACED_ALTERNATE;
  1445. else if (q_data->field == V4L2_FIELD_SEQ_TB)
  1446. q_data->flags |= Q_DATA_INTERLACED_SEQ_TB;
  1447. else
  1448. q_data->flags &= ~Q_IS_INTERLACED;
  1449. /* the crop height is halved for the case of SEQ_TB buffers */
  1450. if (q_data->flags & Q_DATA_INTERLACED_SEQ_TB)
  1451. q_data->c_rect.height /= 2;
  1452. vpe_dbg(ctx->dev, "Setting format for type %d, wxh: %dx%d, fmt: %d bpl_y %d",
  1453. f->type, q_data->width, q_data->height, q_data->fmt->fourcc,
  1454. q_data->bytesperline[VPE_LUMA]);
  1455. if (q_data->fmt->coplanar)
  1456. vpe_dbg(ctx->dev, " bpl_uv %d\n",
  1457. q_data->bytesperline[VPE_CHROMA]);
  1458. return 0;
  1459. }
  1460. static int vpe_s_fmt(struct file *file, void *priv, struct v4l2_format *f)
  1461. {
  1462. int ret;
  1463. struct vpe_ctx *ctx = file2ctx(file);
  1464. ret = vpe_try_fmt(file, priv, f);
  1465. if (ret)
  1466. return ret;
  1467. ret = __vpe_s_fmt(ctx, f);
  1468. if (ret)
  1469. return ret;
  1470. if (V4L2_TYPE_IS_OUTPUT(f->type))
  1471. set_src_registers(ctx);
  1472. else
  1473. set_dst_registers(ctx);
  1474. return set_srcdst_params(ctx);
  1475. }
  1476. static int __vpe_try_selection(struct vpe_ctx *ctx, struct v4l2_selection *s)
  1477. {
  1478. struct vpe_q_data *q_data;
  1479. int height;
  1480. if ((s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
  1481. (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT))
  1482. return -EINVAL;
  1483. q_data = get_q_data(ctx, s->type);
  1484. if (!q_data)
  1485. return -EINVAL;
  1486. switch (s->target) {
  1487. case V4L2_SEL_TGT_COMPOSE:
  1488. /*
  1489. * COMPOSE target is only valid for capture buffer type, return
  1490. * error for output buffer type
  1491. */
  1492. if (s->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
  1493. return -EINVAL;
  1494. break;
  1495. case V4L2_SEL_TGT_CROP:
  1496. /*
  1497. * CROP target is only valid for output buffer type, return
  1498. * error for capture buffer type
  1499. */
  1500. if (s->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1501. return -EINVAL;
  1502. break;
  1503. /*
  1504. * bound and default crop/compose targets are invalid targets to
  1505. * try/set
  1506. */
  1507. default:
  1508. return -EINVAL;
  1509. }
  1510. /*
  1511. * For SEQ_TB buffers, crop height should be less than the height of
  1512. * the field height, not the buffer height
  1513. */
  1514. if (q_data->flags & Q_DATA_INTERLACED_SEQ_TB)
  1515. height = q_data->height / 2;
  1516. else
  1517. height = q_data->height;
  1518. if (s->r.top < 0 || s->r.left < 0) {
  1519. vpe_err(ctx->dev, "negative values for top and left\n");
  1520. s->r.top = s->r.left = 0;
  1521. }
  1522. v4l_bound_align_image(&s->r.width, MIN_W, q_data->width, 1,
  1523. &s->r.height, MIN_H, height, H_ALIGN, S_ALIGN);
  1524. /* adjust left/top if cropping rectangle is out of bounds */
  1525. if (s->r.left + s->r.width > q_data->width)
  1526. s->r.left = q_data->width - s->r.width;
  1527. if (s->r.top + s->r.height > q_data->height)
  1528. s->r.top = q_data->height - s->r.height;
  1529. return 0;
  1530. }
  1531. static int vpe_g_selection(struct file *file, void *fh,
  1532. struct v4l2_selection *s)
  1533. {
  1534. struct vpe_ctx *ctx = file2ctx(file);
  1535. struct vpe_q_data *q_data;
  1536. bool use_c_rect = false;
  1537. if ((s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
  1538. (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT))
  1539. return -EINVAL;
  1540. q_data = get_q_data(ctx, s->type);
  1541. if (!q_data)
  1542. return -EINVAL;
  1543. switch (s->target) {
  1544. case V4L2_SEL_TGT_COMPOSE_DEFAULT:
  1545. case V4L2_SEL_TGT_COMPOSE_BOUNDS:
  1546. if (s->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
  1547. return -EINVAL;
  1548. break;
  1549. case V4L2_SEL_TGT_CROP_BOUNDS:
  1550. case V4L2_SEL_TGT_CROP_DEFAULT:
  1551. if (s->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1552. return -EINVAL;
  1553. break;
  1554. case V4L2_SEL_TGT_COMPOSE:
  1555. if (s->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
  1556. return -EINVAL;
  1557. use_c_rect = true;
  1558. break;
  1559. case V4L2_SEL_TGT_CROP:
  1560. if (s->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1561. return -EINVAL;
  1562. use_c_rect = true;
  1563. break;
  1564. default:
  1565. return -EINVAL;
  1566. }
  1567. if (use_c_rect) {
  1568. /*
  1569. * for CROP/COMPOSE target type, return c_rect params from the
  1570. * respective buffer type
  1571. */
  1572. s->r = q_data->c_rect;
  1573. } else {
  1574. /*
  1575. * for DEFAULT/BOUNDS target type, return width and height from
  1576. * S_FMT of the respective buffer type
  1577. */
  1578. s->r.left = 0;
  1579. s->r.top = 0;
  1580. s->r.width = q_data->width;
  1581. s->r.height = q_data->height;
  1582. }
  1583. return 0;
  1584. }
  1585. static int vpe_s_selection(struct file *file, void *fh,
  1586. struct v4l2_selection *s)
  1587. {
  1588. struct vpe_ctx *ctx = file2ctx(file);
  1589. struct vpe_q_data *q_data;
  1590. struct v4l2_selection sel = *s;
  1591. int ret;
  1592. ret = __vpe_try_selection(ctx, &sel);
  1593. if (ret)
  1594. return ret;
  1595. q_data = get_q_data(ctx, sel.type);
  1596. if (!q_data)
  1597. return -EINVAL;
  1598. if ((q_data->c_rect.left == sel.r.left) &&
  1599. (q_data->c_rect.top == sel.r.top) &&
  1600. (q_data->c_rect.width == sel.r.width) &&
  1601. (q_data->c_rect.height == sel.r.height)) {
  1602. vpe_dbg(ctx->dev,
  1603. "requested crop/compose values are already set\n");
  1604. return 0;
  1605. }
  1606. q_data->c_rect = sel.r;
  1607. return set_srcdst_params(ctx);
  1608. }
  1609. /*
  1610. * defines number of buffers/frames a context can process with VPE before
  1611. * switching to a different context. default value is 1 buffer per context
  1612. */
  1613. #define V4L2_CID_VPE_BUFS_PER_JOB (V4L2_CID_USER_TI_VPE_BASE + 0)
  1614. static int vpe_s_ctrl(struct v4l2_ctrl *ctrl)
  1615. {
  1616. struct vpe_ctx *ctx =
  1617. container_of(ctrl->handler, struct vpe_ctx, hdl);
  1618. switch (ctrl->id) {
  1619. case V4L2_CID_VPE_BUFS_PER_JOB:
  1620. ctx->bufs_per_job = ctrl->val;
  1621. break;
  1622. default:
  1623. vpe_err(ctx->dev, "Invalid control\n");
  1624. return -EINVAL;
  1625. }
  1626. return 0;
  1627. }
  1628. static const struct v4l2_ctrl_ops vpe_ctrl_ops = {
  1629. .s_ctrl = vpe_s_ctrl,
  1630. };
  1631. static const struct v4l2_ioctl_ops vpe_ioctl_ops = {
  1632. .vidioc_querycap = vpe_querycap,
  1633. .vidioc_enum_fmt_vid_cap_mplane = vpe_enum_fmt,
  1634. .vidioc_g_fmt_vid_cap_mplane = vpe_g_fmt,
  1635. .vidioc_try_fmt_vid_cap_mplane = vpe_try_fmt,
  1636. .vidioc_s_fmt_vid_cap_mplane = vpe_s_fmt,
  1637. .vidioc_enum_fmt_vid_out_mplane = vpe_enum_fmt,
  1638. .vidioc_g_fmt_vid_out_mplane = vpe_g_fmt,
  1639. .vidioc_try_fmt_vid_out_mplane = vpe_try_fmt,
  1640. .vidioc_s_fmt_vid_out_mplane = vpe_s_fmt,
  1641. .vidioc_g_selection = vpe_g_selection,
  1642. .vidioc_s_selection = vpe_s_selection,
  1643. .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
  1644. .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
  1645. .vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
  1646. .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
  1647. .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
  1648. .vidioc_streamon = v4l2_m2m_ioctl_streamon,
  1649. .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
  1650. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  1651. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  1652. };
  1653. /*
  1654. * Queue operations
  1655. */
  1656. static int vpe_queue_setup(struct vb2_queue *vq,
  1657. unsigned int *nbuffers, unsigned int *nplanes,
  1658. unsigned int sizes[], struct device *alloc_devs[])
  1659. {
  1660. int i;
  1661. struct vpe_ctx *ctx = vb2_get_drv_priv(vq);
  1662. struct vpe_q_data *q_data;
  1663. q_data = get_q_data(ctx, vq->type);
  1664. *nplanes = q_data->fmt->coplanar ? 2 : 1;
  1665. for (i = 0; i < *nplanes; i++)
  1666. sizes[i] = q_data->sizeimage[i];
  1667. vpe_dbg(ctx->dev, "get %d buffer(s) of size %d", *nbuffers,
  1668. sizes[VPE_LUMA]);
  1669. if (q_data->fmt->coplanar)
  1670. vpe_dbg(ctx->dev, " and %d\n", sizes[VPE_CHROMA]);
  1671. return 0;
  1672. }
  1673. static int vpe_buf_prepare(struct vb2_buffer *vb)
  1674. {
  1675. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  1676. struct vpe_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  1677. struct vpe_q_data *q_data;
  1678. int i, num_planes;
  1679. vpe_dbg(ctx->dev, "type: %d\n", vb->vb2_queue->type);
  1680. q_data = get_q_data(ctx, vb->vb2_queue->type);
  1681. num_planes = q_data->fmt->coplanar ? 2 : 1;
  1682. if (vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  1683. if (!(q_data->flags & Q_IS_INTERLACED)) {
  1684. vbuf->field = V4L2_FIELD_NONE;
  1685. } else {
  1686. if (vbuf->field != V4L2_FIELD_TOP &&
  1687. vbuf->field != V4L2_FIELD_BOTTOM &&
  1688. vbuf->field != V4L2_FIELD_SEQ_TB)
  1689. return -EINVAL;
  1690. }
  1691. }
  1692. for (i = 0; i < num_planes; i++) {
  1693. if (vb2_plane_size(vb, i) < q_data->sizeimage[i]) {
  1694. vpe_err(ctx->dev,
  1695. "data will not fit into plane (%lu < %lu)\n",
  1696. vb2_plane_size(vb, i),
  1697. (long) q_data->sizeimage[i]);
  1698. return -EINVAL;
  1699. }
  1700. }
  1701. for (i = 0; i < num_planes; i++)
  1702. vb2_set_plane_payload(vb, i, q_data->sizeimage[i]);
  1703. return 0;
  1704. }
  1705. static void vpe_buf_queue(struct vb2_buffer *vb)
  1706. {
  1707. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  1708. struct vpe_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  1709. v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
  1710. }
  1711. static int vpe_start_streaming(struct vb2_queue *q, unsigned int count)
  1712. {
  1713. struct vpe_ctx *ctx = vb2_get_drv_priv(q);
  1714. if (ctx->deinterlacing)
  1715. config_edi_input_mode(ctx, 0x0);
  1716. if (ctx->sequence != 0)
  1717. set_srcdst_params(ctx);
  1718. return 0;
  1719. }
  1720. static void vpe_stop_streaming(struct vb2_queue *q)
  1721. {
  1722. struct vpe_ctx *ctx = vb2_get_drv_priv(q);
  1723. struct vb2_v4l2_buffer *vb;
  1724. unsigned long flags;
  1725. vpe_dump_regs(ctx->dev);
  1726. vpdma_dump_regs(ctx->dev->vpdma);
  1727. for (;;) {
  1728. if (V4L2_TYPE_IS_OUTPUT(q->type))
  1729. vb = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
  1730. else
  1731. vb = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
  1732. if (!vb)
  1733. break;
  1734. spin_lock_irqsave(&ctx->dev->lock, flags);
  1735. v4l2_m2m_buf_done(vb, VB2_BUF_STATE_ERROR);
  1736. spin_unlock_irqrestore(&ctx->dev->lock, flags);
  1737. }
  1738. /*
  1739. * Cleanup the in-transit vb2 buffers that have been
  1740. * removed from their respective queue already but for
  1741. * which procecessing has not been completed yet.
  1742. */
  1743. if (V4L2_TYPE_IS_OUTPUT(q->type)) {
  1744. spin_lock_irqsave(&ctx->dev->lock, flags);
  1745. if (ctx->src_vbs[2])
  1746. v4l2_m2m_buf_done(ctx->src_vbs[2], VB2_BUF_STATE_ERROR);
  1747. if (ctx->src_vbs[1] && (ctx->src_vbs[1] != ctx->src_vbs[2]))
  1748. v4l2_m2m_buf_done(ctx->src_vbs[1], VB2_BUF_STATE_ERROR);
  1749. if (ctx->src_vbs[0] &&
  1750. (ctx->src_vbs[0] != ctx->src_vbs[1]) &&
  1751. (ctx->src_vbs[0] != ctx->src_vbs[2]))
  1752. v4l2_m2m_buf_done(ctx->src_vbs[0], VB2_BUF_STATE_ERROR);
  1753. ctx->src_vbs[2] = NULL;
  1754. ctx->src_vbs[1] = NULL;
  1755. ctx->src_vbs[0] = NULL;
  1756. spin_unlock_irqrestore(&ctx->dev->lock, flags);
  1757. } else {
  1758. if (ctx->dst_vb) {
  1759. spin_lock_irqsave(&ctx->dev->lock, flags);
  1760. v4l2_m2m_buf_done(ctx->dst_vb, VB2_BUF_STATE_ERROR);
  1761. ctx->dst_vb = NULL;
  1762. spin_unlock_irqrestore(&ctx->dev->lock, flags);
  1763. }
  1764. }
  1765. }
  1766. static const struct vb2_ops vpe_qops = {
  1767. .queue_setup = vpe_queue_setup,
  1768. .buf_prepare = vpe_buf_prepare,
  1769. .buf_queue = vpe_buf_queue,
  1770. .wait_prepare = vb2_ops_wait_prepare,
  1771. .wait_finish = vb2_ops_wait_finish,
  1772. .start_streaming = vpe_start_streaming,
  1773. .stop_streaming = vpe_stop_streaming,
  1774. };
  1775. static int queue_init(void *priv, struct vb2_queue *src_vq,
  1776. struct vb2_queue *dst_vq)
  1777. {
  1778. struct vpe_ctx *ctx = priv;
  1779. struct vpe_dev *dev = ctx->dev;
  1780. int ret;
  1781. memset(src_vq, 0, sizeof(*src_vq));
  1782. src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
  1783. src_vq->io_modes = VB2_MMAP | VB2_DMABUF;
  1784. src_vq->drv_priv = ctx;
  1785. src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
  1786. src_vq->ops = &vpe_qops;
  1787. src_vq->mem_ops = &vb2_dma_contig_memops;
  1788. src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
  1789. src_vq->lock = &dev->dev_mutex;
  1790. src_vq->dev = dev->v4l2_dev.dev;
  1791. ret = vb2_queue_init(src_vq);
  1792. if (ret)
  1793. return ret;
  1794. memset(dst_vq, 0, sizeof(*dst_vq));
  1795. dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
  1796. dst_vq->io_modes = VB2_MMAP | VB2_DMABUF;
  1797. dst_vq->drv_priv = ctx;
  1798. dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
  1799. dst_vq->ops = &vpe_qops;
  1800. dst_vq->mem_ops = &vb2_dma_contig_memops;
  1801. dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
  1802. dst_vq->lock = &dev->dev_mutex;
  1803. dst_vq->dev = dev->v4l2_dev.dev;
  1804. return vb2_queue_init(dst_vq);
  1805. }
  1806. static const struct v4l2_ctrl_config vpe_bufs_per_job = {
  1807. .ops = &vpe_ctrl_ops,
  1808. .id = V4L2_CID_VPE_BUFS_PER_JOB,
  1809. .name = "Buffers Per Transaction",
  1810. .type = V4L2_CTRL_TYPE_INTEGER,
  1811. .def = VPE_DEF_BUFS_PER_JOB,
  1812. .min = 1,
  1813. .max = VIDEO_MAX_FRAME,
  1814. .step = 1,
  1815. };
  1816. /*
  1817. * File operations
  1818. */
  1819. static int vpe_open(struct file *file)
  1820. {
  1821. struct vpe_dev *dev = video_drvdata(file);
  1822. struct vpe_q_data *s_q_data;
  1823. struct v4l2_ctrl_handler *hdl;
  1824. struct vpe_ctx *ctx;
  1825. int ret;
  1826. vpe_dbg(dev, "vpe_open\n");
  1827. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  1828. if (!ctx)
  1829. return -ENOMEM;
  1830. ctx->dev = dev;
  1831. if (mutex_lock_interruptible(&dev->dev_mutex)) {
  1832. ret = -ERESTARTSYS;
  1833. goto free_ctx;
  1834. }
  1835. ret = vpdma_create_desc_list(&ctx->desc_list, VPE_DESC_LIST_SIZE,
  1836. VPDMA_LIST_TYPE_NORMAL);
  1837. if (ret != 0)
  1838. goto unlock;
  1839. ret = vpdma_alloc_desc_buf(&ctx->mmr_adb, sizeof(struct vpe_mmr_adb));
  1840. if (ret != 0)
  1841. goto free_desc_list;
  1842. ret = vpdma_alloc_desc_buf(&ctx->sc_coeff_h, SC_COEF_SRAM_SIZE);
  1843. if (ret != 0)
  1844. goto free_mmr_adb;
  1845. ret = vpdma_alloc_desc_buf(&ctx->sc_coeff_v, SC_COEF_SRAM_SIZE);
  1846. if (ret != 0)
  1847. goto free_sc_h;
  1848. init_adb_hdrs(ctx);
  1849. v4l2_fh_init(&ctx->fh, video_devdata(file));
  1850. file->private_data = &ctx->fh;
  1851. hdl = &ctx->hdl;
  1852. v4l2_ctrl_handler_init(hdl, 1);
  1853. v4l2_ctrl_new_custom(hdl, &vpe_bufs_per_job, NULL);
  1854. if (hdl->error) {
  1855. ret = hdl->error;
  1856. goto exit_fh;
  1857. }
  1858. ctx->fh.ctrl_handler = hdl;
  1859. v4l2_ctrl_handler_setup(hdl);
  1860. s_q_data = &ctx->q_data[Q_DATA_SRC];
  1861. s_q_data->fmt = &vpe_formats[2];
  1862. s_q_data->width = 1920;
  1863. s_q_data->height = 1080;
  1864. s_q_data->bytesperline[VPE_LUMA] = (s_q_data->width *
  1865. s_q_data->fmt->vpdma_fmt[VPE_LUMA]->depth) >> 3;
  1866. s_q_data->sizeimage[VPE_LUMA] = (s_q_data->bytesperline[VPE_LUMA] *
  1867. s_q_data->height);
  1868. s_q_data->colorspace = V4L2_COLORSPACE_REC709;
  1869. s_q_data->field = V4L2_FIELD_NONE;
  1870. s_q_data->c_rect.left = 0;
  1871. s_q_data->c_rect.top = 0;
  1872. s_q_data->c_rect.width = s_q_data->width;
  1873. s_q_data->c_rect.height = s_q_data->height;
  1874. s_q_data->flags = 0;
  1875. ctx->q_data[Q_DATA_DST] = *s_q_data;
  1876. set_dei_shadow_registers(ctx);
  1877. set_src_registers(ctx);
  1878. set_dst_registers(ctx);
  1879. ret = set_srcdst_params(ctx);
  1880. if (ret)
  1881. goto exit_fh;
  1882. ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx, &queue_init);
  1883. if (IS_ERR(ctx->fh.m2m_ctx)) {
  1884. ret = PTR_ERR(ctx->fh.m2m_ctx);
  1885. goto exit_fh;
  1886. }
  1887. v4l2_fh_add(&ctx->fh);
  1888. /*
  1889. * for now, just report the creation of the first instance, we can later
  1890. * optimize the driver to enable or disable clocks when the first
  1891. * instance is created or the last instance released
  1892. */
  1893. if (atomic_inc_return(&dev->num_instances) == 1)
  1894. vpe_dbg(dev, "first instance created\n");
  1895. ctx->bufs_per_job = VPE_DEF_BUFS_PER_JOB;
  1896. ctx->load_mmrs = true;
  1897. vpe_dbg(dev, "created instance %p, m2m_ctx: %p\n",
  1898. ctx, ctx->fh.m2m_ctx);
  1899. mutex_unlock(&dev->dev_mutex);
  1900. return 0;
  1901. exit_fh:
  1902. v4l2_ctrl_handler_free(hdl);
  1903. v4l2_fh_exit(&ctx->fh);
  1904. vpdma_free_desc_buf(&ctx->sc_coeff_v);
  1905. free_sc_h:
  1906. vpdma_free_desc_buf(&ctx->sc_coeff_h);
  1907. free_mmr_adb:
  1908. vpdma_free_desc_buf(&ctx->mmr_adb);
  1909. free_desc_list:
  1910. vpdma_free_desc_list(&ctx->desc_list);
  1911. unlock:
  1912. mutex_unlock(&dev->dev_mutex);
  1913. free_ctx:
  1914. kfree(ctx);
  1915. return ret;
  1916. }
  1917. static int vpe_release(struct file *file)
  1918. {
  1919. struct vpe_dev *dev = video_drvdata(file);
  1920. struct vpe_ctx *ctx = file2ctx(file);
  1921. vpe_dbg(dev, "releasing instance %p\n", ctx);
  1922. mutex_lock(&dev->dev_mutex);
  1923. free_mv_buffers(ctx);
  1924. vpdma_free_desc_list(&ctx->desc_list);
  1925. vpdma_free_desc_buf(&ctx->mmr_adb);
  1926. vpdma_free_desc_buf(&ctx->sc_coeff_v);
  1927. vpdma_free_desc_buf(&ctx->sc_coeff_h);
  1928. v4l2_fh_del(&ctx->fh);
  1929. v4l2_fh_exit(&ctx->fh);
  1930. v4l2_ctrl_handler_free(&ctx->hdl);
  1931. v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
  1932. kfree(ctx);
  1933. /*
  1934. * for now, just report the release of the last instance, we can later
  1935. * optimize the driver to enable or disable clocks when the first
  1936. * instance is created or the last instance released
  1937. */
  1938. if (atomic_dec_return(&dev->num_instances) == 0)
  1939. vpe_dbg(dev, "last instance released\n");
  1940. mutex_unlock(&dev->dev_mutex);
  1941. return 0;
  1942. }
  1943. static const struct v4l2_file_operations vpe_fops = {
  1944. .owner = THIS_MODULE,
  1945. .open = vpe_open,
  1946. .release = vpe_release,
  1947. .poll = v4l2_m2m_fop_poll,
  1948. .unlocked_ioctl = video_ioctl2,
  1949. .mmap = v4l2_m2m_fop_mmap,
  1950. };
  1951. static struct video_device vpe_videodev = {
  1952. .name = VPE_MODULE_NAME,
  1953. .fops = &vpe_fops,
  1954. .ioctl_ops = &vpe_ioctl_ops,
  1955. .minor = -1,
  1956. .release = video_device_release_empty,
  1957. .vfl_dir = VFL_DIR_M2M,
  1958. };
  1959. static struct v4l2_m2m_ops m2m_ops = {
  1960. .device_run = device_run,
  1961. .job_ready = job_ready,
  1962. .job_abort = job_abort,
  1963. .lock = vpe_lock,
  1964. .unlock = vpe_unlock,
  1965. };
  1966. static int vpe_runtime_get(struct platform_device *pdev)
  1967. {
  1968. int r;
  1969. dev_dbg(&pdev->dev, "vpe_runtime_get\n");
  1970. r = pm_runtime_get_sync(&pdev->dev);
  1971. WARN_ON(r < 0);
  1972. return r < 0 ? r : 0;
  1973. }
  1974. static void vpe_runtime_put(struct platform_device *pdev)
  1975. {
  1976. int r;
  1977. dev_dbg(&pdev->dev, "vpe_runtime_put\n");
  1978. r = pm_runtime_put_sync(&pdev->dev);
  1979. WARN_ON(r < 0 && r != -ENOSYS);
  1980. }
  1981. static void vpe_fw_cb(struct platform_device *pdev)
  1982. {
  1983. struct vpe_dev *dev = platform_get_drvdata(pdev);
  1984. struct video_device *vfd;
  1985. int ret;
  1986. vfd = &dev->vfd;
  1987. *vfd = vpe_videodev;
  1988. vfd->lock = &dev->dev_mutex;
  1989. vfd->v4l2_dev = &dev->v4l2_dev;
  1990. ret = video_register_device(vfd, VFL_TYPE_GRABBER, 0);
  1991. if (ret) {
  1992. vpe_err(dev, "Failed to register video device\n");
  1993. vpe_set_clock_enable(dev, 0);
  1994. vpe_runtime_put(pdev);
  1995. pm_runtime_disable(&pdev->dev);
  1996. v4l2_m2m_release(dev->m2m_dev);
  1997. v4l2_device_unregister(&dev->v4l2_dev);
  1998. return;
  1999. }
  2000. video_set_drvdata(vfd, dev);
  2001. snprintf(vfd->name, sizeof(vfd->name), "%s", vpe_videodev.name);
  2002. dev_info(dev->v4l2_dev.dev, "Device registered as /dev/video%d\n",
  2003. vfd->num);
  2004. }
  2005. static int vpe_probe(struct platform_device *pdev)
  2006. {
  2007. struct vpe_dev *dev;
  2008. int ret, irq, func;
  2009. dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
  2010. if (!dev)
  2011. return -ENOMEM;
  2012. spin_lock_init(&dev->lock);
  2013. ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
  2014. if (ret)
  2015. return ret;
  2016. atomic_set(&dev->num_instances, 0);
  2017. mutex_init(&dev->dev_mutex);
  2018. dev->res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  2019. "vpe_top");
  2020. /*
  2021. * HACK: we get resource info from device tree in the form of a list of
  2022. * VPE sub blocks, the driver currently uses only the base of vpe_top
  2023. * for register access, the driver should be changed later to access
  2024. * registers based on the sub block base addresses
  2025. */
  2026. dev->base = devm_ioremap(&pdev->dev, dev->res->start, SZ_32K);
  2027. if (!dev->base) {
  2028. ret = -ENOMEM;
  2029. goto v4l2_dev_unreg;
  2030. }
  2031. irq = platform_get_irq(pdev, 0);
  2032. ret = devm_request_irq(&pdev->dev, irq, vpe_irq, 0, VPE_MODULE_NAME,
  2033. dev);
  2034. if (ret)
  2035. goto v4l2_dev_unreg;
  2036. platform_set_drvdata(pdev, dev);
  2037. dev->m2m_dev = v4l2_m2m_init(&m2m_ops);
  2038. if (IS_ERR(dev->m2m_dev)) {
  2039. vpe_err(dev, "Failed to init mem2mem device\n");
  2040. ret = PTR_ERR(dev->m2m_dev);
  2041. goto v4l2_dev_unreg;
  2042. }
  2043. pm_runtime_enable(&pdev->dev);
  2044. ret = vpe_runtime_get(pdev);
  2045. if (ret)
  2046. goto rel_m2m;
  2047. /* Perform clk enable followed by reset */
  2048. vpe_set_clock_enable(dev, 1);
  2049. vpe_top_reset(dev);
  2050. func = read_field_reg(dev, VPE_PID, VPE_PID_FUNC_MASK,
  2051. VPE_PID_FUNC_SHIFT);
  2052. vpe_dbg(dev, "VPE PID function %x\n", func);
  2053. vpe_top_vpdma_reset(dev);
  2054. dev->sc = sc_create(pdev);
  2055. if (IS_ERR(dev->sc)) {
  2056. ret = PTR_ERR(dev->sc);
  2057. goto runtime_put;
  2058. }
  2059. dev->csc = csc_create(pdev);
  2060. if (IS_ERR(dev->csc)) {
  2061. ret = PTR_ERR(dev->csc);
  2062. goto runtime_put;
  2063. }
  2064. dev->vpdma = &dev->vpdma_data;
  2065. ret = vpdma_create(pdev, dev->vpdma, vpe_fw_cb);
  2066. if (ret)
  2067. goto runtime_put;
  2068. return 0;
  2069. runtime_put:
  2070. vpe_runtime_put(pdev);
  2071. rel_m2m:
  2072. pm_runtime_disable(&pdev->dev);
  2073. v4l2_m2m_release(dev->m2m_dev);
  2074. v4l2_dev_unreg:
  2075. v4l2_device_unregister(&dev->v4l2_dev);
  2076. return ret;
  2077. }
  2078. static int vpe_remove(struct platform_device *pdev)
  2079. {
  2080. struct vpe_dev *dev = platform_get_drvdata(pdev);
  2081. v4l2_info(&dev->v4l2_dev, "Removing " VPE_MODULE_NAME);
  2082. v4l2_m2m_release(dev->m2m_dev);
  2083. video_unregister_device(&dev->vfd);
  2084. v4l2_device_unregister(&dev->v4l2_dev);
  2085. vpe_set_clock_enable(dev, 0);
  2086. vpe_runtime_put(pdev);
  2087. pm_runtime_disable(&pdev->dev);
  2088. return 0;
  2089. }
  2090. #if defined(CONFIG_OF)
  2091. static const struct of_device_id vpe_of_match[] = {
  2092. {
  2093. .compatible = "ti,vpe",
  2094. },
  2095. {},
  2096. };
  2097. MODULE_DEVICE_TABLE(of, vpe_of_match);
  2098. #endif
  2099. static struct platform_driver vpe_pdrv = {
  2100. .probe = vpe_probe,
  2101. .remove = vpe_remove,
  2102. .driver = {
  2103. .name = VPE_MODULE_NAME,
  2104. .of_match_table = of_match_ptr(vpe_of_match),
  2105. },
  2106. };
  2107. module_platform_driver(vpe_pdrv);
  2108. MODULE_DESCRIPTION("TI VPE driver");
  2109. MODULE_AUTHOR("Dale Farnsworth, <dale@farnsworth.org>");
  2110. MODULE_LICENSE("GPL");