vpe.c 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387
  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. .of_match_table = vpe_of_match,
  1983. },
  1984. };
  1985. module_platform_driver(vpe_pdrv);
  1986. MODULE_DESCRIPTION("TI VPE driver");
  1987. MODULE_AUTHOR("Dale Farnsworth, <dale@farnsworth.org>");
  1988. MODULE_LICENSE("GPL");