vpe.c 59 KB

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