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