vpe.c 66 KB

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