vpe.c 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321
  1. /*
  2. * TI VPE mem2mem driver, based on the virtual v4l2-mem2mem example driver
  3. *
  4. * Copyright (c) 2013 Texas Instruments Inc.
  5. * David Griego, <dagriego@biglakesoftware.com>
  6. * Dale Farnsworth, <dale@farnsworth.org>
  7. * Archit Taneja, <archit@ti.com>
  8. *
  9. * Copyright (c) 2009-2010 Samsung Electronics Co., Ltd.
  10. * Pawel Osciak, <pawel@osciak.com>
  11. * Marek Szyprowski, <m.szyprowski@samsung.com>
  12. *
  13. * Based on the virtual v4l2-mem2mem example device
  14. *
  15. * This program is free software; you can redistribute it and/or modify it
  16. * under the terms of the GNU General Public License version 2 as published by
  17. * the Free Software Foundation
  18. */
  19. #include <linux/delay.h>
  20. #include <linux/dma-mapping.h>
  21. #include <linux/err.h>
  22. #include <linux/fs.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/io.h>
  25. #include <linux/ioctl.h>
  26. #include <linux/module.h>
  27. #include <linux/of.h>
  28. #include <linux/platform_device.h>
  29. #include <linux/pm_runtime.h>
  30. #include <linux/sched.h>
  31. #include <linux/slab.h>
  32. #include <linux/videodev2.h>
  33. #include <linux/log2.h>
  34. #include <linux/sizes.h>
  35. #include <media/v4l2-common.h>
  36. #include <media/v4l2-ctrls.h>
  37. #include <media/v4l2-device.h>
  38. #include <media/v4l2-event.h>
  39. #include <media/v4l2-ioctl.h>
  40. #include <media/v4l2-mem2mem.h>
  41. #include <media/videobuf2-core.h>
  42. #include <media/videobuf2-dma-contig.h>
  43. #include "vpdma.h"
  44. #include "vpe_regs.h"
  45. #include "sc.h"
  46. #include "csc.h"
  47. #define VPE_MODULE_NAME "vpe"
  48. /* minimum and maximum frame sizes */
  49. #define MIN_W 32
  50. #define MIN_H 32
  51. #define MAX_W 1920
  52. #define MAX_H 1080
  53. /* required alignments */
  54. #define S_ALIGN 0 /* multiple of 1 */
  55. #define H_ALIGN 1 /* multiple of 2 */
  56. /* flags that indicate a format can be used for capture/output */
  57. #define VPE_FMT_TYPE_CAPTURE (1 << 0)
  58. #define VPE_FMT_TYPE_OUTPUT (1 << 1)
  59. /* used as plane indices */
  60. #define VPE_MAX_PLANES 2
  61. #define VPE_LUMA 0
  62. #define VPE_CHROMA 1
  63. /* per m2m context info */
  64. #define VPE_MAX_SRC_BUFS 3 /* need 3 src fields to de-interlace */
  65. #define VPE_DEF_BUFS_PER_JOB 1 /* default one buffer per batch job */
  66. /*
  67. * each VPE context can need up to 3 config descriptors, 7 input descriptors,
  68. * 3 output descriptors, and 10 control descriptors
  69. */
  70. #define VPE_DESC_LIST_SIZE (10 * VPDMA_DTD_DESC_SIZE + \
  71. 13 * VPDMA_CFD_CTD_DESC_SIZE)
  72. #define vpe_dbg(vpedev, fmt, arg...) \
  73. dev_dbg((vpedev)->v4l2_dev.dev, fmt, ##arg)
  74. #define vpe_err(vpedev, fmt, arg...) \
  75. dev_err((vpedev)->v4l2_dev.dev, fmt, ##arg)
  76. struct vpe_us_coeffs {
  77. unsigned short anchor_fid0_c0;
  78. unsigned short anchor_fid0_c1;
  79. unsigned short anchor_fid0_c2;
  80. unsigned short anchor_fid0_c3;
  81. unsigned short interp_fid0_c0;
  82. unsigned short interp_fid0_c1;
  83. unsigned short interp_fid0_c2;
  84. unsigned short interp_fid0_c3;
  85. unsigned short anchor_fid1_c0;
  86. unsigned short anchor_fid1_c1;
  87. unsigned short anchor_fid1_c2;
  88. unsigned short anchor_fid1_c3;
  89. unsigned short interp_fid1_c0;
  90. unsigned short interp_fid1_c1;
  91. unsigned short interp_fid1_c2;
  92. unsigned short interp_fid1_c3;
  93. };
  94. /*
  95. * Default upsampler coefficients
  96. */
  97. static const struct vpe_us_coeffs us_coeffs[] = {
  98. {
  99. /* Coefficients for progressive input */
  100. 0x00C8, 0x0348, 0x0018, 0x3FD8, 0x3FB8, 0x0378, 0x00E8, 0x3FE8,
  101. 0x00C8, 0x0348, 0x0018, 0x3FD8, 0x3FB8, 0x0378, 0x00E8, 0x3FE8,
  102. },
  103. {
  104. /* Coefficients for Top Field Interlaced input */
  105. 0x0051, 0x03D5, 0x3FE3, 0x3FF7, 0x3FB5, 0x02E9, 0x018F, 0x3FD3,
  106. /* Coefficients for Bottom Field Interlaced input */
  107. 0x016B, 0x0247, 0x00B1, 0x3F9D, 0x3FCF, 0x03DB, 0x005D, 0x3FF9,
  108. },
  109. };
  110. /*
  111. * the following registers are for configuring some of the parameters of the
  112. * motion and edge detection blocks inside DEI, these generally remain the same,
  113. * these could be passed later via userspace if some one needs to tweak these.
  114. */
  115. struct vpe_dei_regs {
  116. unsigned long mdt_spacial_freq_thr_reg; /* VPE_DEI_REG2 */
  117. unsigned long edi_config_reg; /* VPE_DEI_REG3 */
  118. unsigned long edi_lut_reg0; /* VPE_DEI_REG4 */
  119. unsigned long edi_lut_reg1; /* VPE_DEI_REG5 */
  120. unsigned long edi_lut_reg2; /* VPE_DEI_REG6 */
  121. unsigned long edi_lut_reg3; /* VPE_DEI_REG7 */
  122. };
  123. /*
  124. * default expert DEI register values, unlikely to be modified.
  125. */
  126. static const struct vpe_dei_regs dei_regs = {
  127. .mdt_spacial_freq_thr_reg = 0x020C0804u,
  128. .edi_config_reg = 0x0118100Fu,
  129. .edi_lut_reg0 = 0x08040200u,
  130. .edi_lut_reg1 = 0x1010100Cu,
  131. .edi_lut_reg2 = 0x10101010u,
  132. .edi_lut_reg3 = 0x10101010u,
  133. };
  134. /*
  135. * The port_data structure contains per-port data.
  136. */
  137. struct vpe_port_data {
  138. enum vpdma_channel channel; /* VPDMA channel */
  139. u8 vb_index; /* input frame f, f-1, f-2 index */
  140. u8 vb_part; /* plane index for co-panar formats */
  141. };
  142. /*
  143. * Define indices into the port_data tables
  144. */
  145. #define VPE_PORT_LUMA1_IN 0
  146. #define VPE_PORT_CHROMA1_IN 1
  147. #define VPE_PORT_LUMA2_IN 2
  148. #define VPE_PORT_CHROMA2_IN 3
  149. #define VPE_PORT_LUMA3_IN 4
  150. #define VPE_PORT_CHROMA3_IN 5
  151. #define VPE_PORT_MV_IN 6
  152. #define VPE_PORT_MV_OUT 7
  153. #define VPE_PORT_LUMA_OUT 8
  154. #define VPE_PORT_CHROMA_OUT 9
  155. #define VPE_PORT_RGB_OUT 10
  156. static const struct vpe_port_data port_data[11] = {
  157. [VPE_PORT_LUMA1_IN] = {
  158. .channel = VPE_CHAN_LUMA1_IN,
  159. .vb_index = 0,
  160. .vb_part = VPE_LUMA,
  161. },
  162. [VPE_PORT_CHROMA1_IN] = {
  163. .channel = VPE_CHAN_CHROMA1_IN,
  164. .vb_index = 0,
  165. .vb_part = VPE_CHROMA,
  166. },
  167. [VPE_PORT_LUMA2_IN] = {
  168. .channel = VPE_CHAN_LUMA2_IN,
  169. .vb_index = 1,
  170. .vb_part = VPE_LUMA,
  171. },
  172. [VPE_PORT_CHROMA2_IN] = {
  173. .channel = VPE_CHAN_CHROMA2_IN,
  174. .vb_index = 1,
  175. .vb_part = VPE_CHROMA,
  176. },
  177. [VPE_PORT_LUMA3_IN] = {
  178. .channel = VPE_CHAN_LUMA3_IN,
  179. .vb_index = 2,
  180. .vb_part = VPE_LUMA,
  181. },
  182. [VPE_PORT_CHROMA3_IN] = {
  183. .channel = VPE_CHAN_CHROMA3_IN,
  184. .vb_index = 2,
  185. .vb_part = VPE_CHROMA,
  186. },
  187. [VPE_PORT_MV_IN] = {
  188. .channel = VPE_CHAN_MV_IN,
  189. },
  190. [VPE_PORT_MV_OUT] = {
  191. .channel = VPE_CHAN_MV_OUT,
  192. },
  193. [VPE_PORT_LUMA_OUT] = {
  194. .channel = VPE_CHAN_LUMA_OUT,
  195. .vb_part = VPE_LUMA,
  196. },
  197. [VPE_PORT_CHROMA_OUT] = {
  198. .channel = VPE_CHAN_CHROMA_OUT,
  199. .vb_part = VPE_CHROMA,
  200. },
  201. [VPE_PORT_RGB_OUT] = {
  202. .channel = VPE_CHAN_RGB_OUT,
  203. .vb_part = VPE_LUMA,
  204. },
  205. };
  206. /* driver info for each of the supported video formats */
  207. struct vpe_fmt {
  208. char *name; /* human-readable name */
  209. u32 fourcc; /* standard format identifier */
  210. u8 types; /* CAPTURE and/or OUTPUT */
  211. u8 coplanar; /* set for unpacked Luma and Chroma */
  212. /* vpdma format info for each plane */
  213. struct vpdma_data_format const *vpdma_fmt[VPE_MAX_PLANES];
  214. };
  215. static struct vpe_fmt vpe_formats[] = {
  216. {
  217. .name = "YUV 422 co-planar",
  218. .fourcc = V4L2_PIX_FMT_NV16,
  219. .types = VPE_FMT_TYPE_CAPTURE | VPE_FMT_TYPE_OUTPUT,
  220. .coplanar = 1,
  221. .vpdma_fmt = { &vpdma_yuv_fmts[VPDMA_DATA_FMT_Y444],
  222. &vpdma_yuv_fmts[VPDMA_DATA_FMT_C444],
  223. },
  224. },
  225. {
  226. .name = "YUV 420 co-planar",
  227. .fourcc = V4L2_PIX_FMT_NV12,
  228. .types = VPE_FMT_TYPE_CAPTURE | VPE_FMT_TYPE_OUTPUT,
  229. .coplanar = 1,
  230. .vpdma_fmt = { &vpdma_yuv_fmts[VPDMA_DATA_FMT_Y420],
  231. &vpdma_yuv_fmts[VPDMA_DATA_FMT_C420],
  232. },
  233. },
  234. {
  235. .name = "YUYV 422 packed",
  236. .fourcc = V4L2_PIX_FMT_YUYV,
  237. .types = VPE_FMT_TYPE_CAPTURE | VPE_FMT_TYPE_OUTPUT,
  238. .coplanar = 0,
  239. .vpdma_fmt = { &vpdma_yuv_fmts[VPDMA_DATA_FMT_YC422],
  240. },
  241. },
  242. {
  243. .name = "UYVY 422 packed",
  244. .fourcc = V4L2_PIX_FMT_UYVY,
  245. .types = VPE_FMT_TYPE_CAPTURE | VPE_FMT_TYPE_OUTPUT,
  246. .coplanar = 0,
  247. .vpdma_fmt = { &vpdma_yuv_fmts[VPDMA_DATA_FMT_CY422],
  248. },
  249. },
  250. {
  251. .name = "RGB888 packed",
  252. .fourcc = V4L2_PIX_FMT_RGB24,
  253. .types = VPE_FMT_TYPE_CAPTURE,
  254. .coplanar = 0,
  255. .vpdma_fmt = { &vpdma_rgb_fmts[VPDMA_DATA_FMT_RGB24],
  256. },
  257. },
  258. {
  259. .name = "ARGB32",
  260. .fourcc = V4L2_PIX_FMT_RGB32,
  261. .types = VPE_FMT_TYPE_CAPTURE,
  262. .coplanar = 0,
  263. .vpdma_fmt = { &vpdma_rgb_fmts[VPDMA_DATA_FMT_ARGB32],
  264. },
  265. },
  266. {
  267. .name = "BGR888 packed",
  268. .fourcc = V4L2_PIX_FMT_BGR24,
  269. .types = VPE_FMT_TYPE_CAPTURE,
  270. .coplanar = 0,
  271. .vpdma_fmt = { &vpdma_rgb_fmts[VPDMA_DATA_FMT_BGR24],
  272. },
  273. },
  274. {
  275. .name = "ABGR32",
  276. .fourcc = V4L2_PIX_FMT_BGR32,
  277. .types = VPE_FMT_TYPE_CAPTURE,
  278. .coplanar = 0,
  279. .vpdma_fmt = { &vpdma_rgb_fmts[VPDMA_DATA_FMT_ABGR32],
  280. },
  281. },
  282. };
  283. /*
  284. * per-queue, driver-specific private data.
  285. * there is one source queue and one destination queue for each m2m context.
  286. */
  287. struct vpe_q_data {
  288. unsigned int width; /* frame width */
  289. unsigned int height; /* frame height */
  290. unsigned int bytesperline[VPE_MAX_PLANES]; /* bytes per line in memory */
  291. enum v4l2_colorspace colorspace;
  292. enum v4l2_field field; /* supported field value */
  293. unsigned int flags;
  294. unsigned int sizeimage[VPE_MAX_PLANES]; /* image size in memory */
  295. struct v4l2_rect c_rect; /* crop/compose rectangle */
  296. struct vpe_fmt *fmt; /* format info */
  297. };
  298. /* vpe_q_data flag bits */
  299. #define Q_DATA_FRAME_1D (1 << 0)
  300. #define Q_DATA_MODE_TILED (1 << 1)
  301. #define Q_DATA_INTERLACED (1 << 2)
  302. enum {
  303. Q_DATA_SRC = 0,
  304. Q_DATA_DST = 1,
  305. };
  306. /* find our format description corresponding to the passed v4l2_format */
  307. static struct vpe_fmt *find_format(struct v4l2_format *f)
  308. {
  309. struct vpe_fmt *fmt;
  310. unsigned int k;
  311. for (k = 0; k < ARRAY_SIZE(vpe_formats); k++) {
  312. fmt = &vpe_formats[k];
  313. if (fmt->fourcc == f->fmt.pix.pixelformat)
  314. return fmt;
  315. }
  316. return NULL;
  317. }
  318. /*
  319. * there is one vpe_dev structure in the driver, it is shared by
  320. * all instances.
  321. */
  322. struct vpe_dev {
  323. struct v4l2_device v4l2_dev;
  324. struct video_device vfd;
  325. struct v4l2_m2m_dev *m2m_dev;
  326. atomic_t num_instances; /* count of driver instances */
  327. dma_addr_t loaded_mmrs; /* shadow mmrs in device */
  328. struct mutex dev_mutex;
  329. spinlock_t lock;
  330. int irq;
  331. void __iomem *base;
  332. struct resource *res;
  333. struct vb2_alloc_ctx *alloc_ctx;
  334. struct vpdma_data *vpdma; /* vpdma data handle */
  335. struct sc_data *sc; /* scaler data handle */
  336. struct csc_data *csc; /* csc data handle */
  337. };
  338. /*
  339. * There is one vpe_ctx structure for each m2m context.
  340. */
  341. struct vpe_ctx {
  342. struct v4l2_fh fh;
  343. struct vpe_dev *dev;
  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->fh.m2m_ctx) < needed)
  768. return 0;
  769. if (v4l2_m2m_num_dst_bufs_ready(ctx->fh.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->fh.m2m_ctx);
  952. WARN_ON(ctx->src_vbs[2] == NULL);
  953. ctx->src_vbs[1] = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
  954. WARN_ON(ctx->src_vbs[1] == NULL);
  955. }
  956. ctx->src_vbs[0] = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
  957. WARN_ON(ctx->src_vbs[0] == NULL);
  958. ctx->dst_vb = v4l2_m2m_dst_buf_remove(ctx->fh.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->fh.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->fh.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->fh.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. /*
  1475. * defines number of buffers/frames a context can process with VPE before
  1476. * switching to a different context. default value is 1 buffer per context
  1477. */
  1478. #define V4L2_CID_VPE_BUFS_PER_JOB (V4L2_CID_USER_TI_VPE_BASE + 0)
  1479. static int vpe_s_ctrl(struct v4l2_ctrl *ctrl)
  1480. {
  1481. struct vpe_ctx *ctx =
  1482. container_of(ctrl->handler, struct vpe_ctx, hdl);
  1483. switch (ctrl->id) {
  1484. case V4L2_CID_VPE_BUFS_PER_JOB:
  1485. ctx->bufs_per_job = ctrl->val;
  1486. break;
  1487. default:
  1488. vpe_err(ctx->dev, "Invalid control\n");
  1489. return -EINVAL;
  1490. }
  1491. return 0;
  1492. }
  1493. static const struct v4l2_ctrl_ops vpe_ctrl_ops = {
  1494. .s_ctrl = vpe_s_ctrl,
  1495. };
  1496. static const struct v4l2_ioctl_ops vpe_ioctl_ops = {
  1497. .vidioc_querycap = vpe_querycap,
  1498. .vidioc_enum_fmt_vid_cap_mplane = vpe_enum_fmt,
  1499. .vidioc_g_fmt_vid_cap_mplane = vpe_g_fmt,
  1500. .vidioc_try_fmt_vid_cap_mplane = vpe_try_fmt,
  1501. .vidioc_s_fmt_vid_cap_mplane = vpe_s_fmt,
  1502. .vidioc_enum_fmt_vid_out_mplane = vpe_enum_fmt,
  1503. .vidioc_g_fmt_vid_out_mplane = vpe_g_fmt,
  1504. .vidioc_try_fmt_vid_out_mplane = vpe_try_fmt,
  1505. .vidioc_s_fmt_vid_out_mplane = vpe_s_fmt,
  1506. .vidioc_g_selection = vpe_g_selection,
  1507. .vidioc_s_selection = vpe_s_selection,
  1508. .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
  1509. .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
  1510. .vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
  1511. .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
  1512. .vidioc_streamon = v4l2_m2m_ioctl_streamon,
  1513. .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
  1514. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  1515. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  1516. };
  1517. /*
  1518. * Queue operations
  1519. */
  1520. static int vpe_queue_setup(struct vb2_queue *vq,
  1521. const struct v4l2_format *fmt,
  1522. unsigned int *nbuffers, unsigned int *nplanes,
  1523. unsigned int sizes[], void *alloc_ctxs[])
  1524. {
  1525. int i;
  1526. struct vpe_ctx *ctx = vb2_get_drv_priv(vq);
  1527. struct vpe_q_data *q_data;
  1528. q_data = get_q_data(ctx, vq->type);
  1529. *nplanes = q_data->fmt->coplanar ? 2 : 1;
  1530. for (i = 0; i < *nplanes; i++) {
  1531. sizes[i] = q_data->sizeimage[i];
  1532. alloc_ctxs[i] = ctx->dev->alloc_ctx;
  1533. }
  1534. vpe_dbg(ctx->dev, "get %d buffer(s) of size %d", *nbuffers,
  1535. sizes[VPE_LUMA]);
  1536. if (q_data->fmt->coplanar)
  1537. vpe_dbg(ctx->dev, " and %d\n", sizes[VPE_CHROMA]);
  1538. return 0;
  1539. }
  1540. static int vpe_buf_prepare(struct vb2_buffer *vb)
  1541. {
  1542. struct vpe_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  1543. struct vpe_q_data *q_data;
  1544. int i, num_planes;
  1545. vpe_dbg(ctx->dev, "type: %d\n", vb->vb2_queue->type);
  1546. q_data = get_q_data(ctx, vb->vb2_queue->type);
  1547. num_planes = q_data->fmt->coplanar ? 2 : 1;
  1548. if (vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  1549. if (!(q_data->flags & Q_DATA_INTERLACED)) {
  1550. vb->v4l2_buf.field = V4L2_FIELD_NONE;
  1551. } else {
  1552. if (vb->v4l2_buf.field != V4L2_FIELD_TOP &&
  1553. vb->v4l2_buf.field != V4L2_FIELD_BOTTOM)
  1554. return -EINVAL;
  1555. }
  1556. }
  1557. for (i = 0; i < num_planes; i++) {
  1558. if (vb2_plane_size(vb, i) < q_data->sizeimage[i]) {
  1559. vpe_err(ctx->dev,
  1560. "data will not fit into plane (%lu < %lu)\n",
  1561. vb2_plane_size(vb, i),
  1562. (long) q_data->sizeimage[i]);
  1563. return -EINVAL;
  1564. }
  1565. }
  1566. for (i = 0; i < num_planes; i++)
  1567. vb2_set_plane_payload(vb, i, q_data->sizeimage[i]);
  1568. return 0;
  1569. }
  1570. static void vpe_buf_queue(struct vb2_buffer *vb)
  1571. {
  1572. struct vpe_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  1573. v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vb);
  1574. }
  1575. static int vpe_start_streaming(struct vb2_queue *q, unsigned int count)
  1576. {
  1577. /* currently we do nothing here */
  1578. return 0;
  1579. }
  1580. static void vpe_stop_streaming(struct vb2_queue *q)
  1581. {
  1582. struct vpe_ctx *ctx = vb2_get_drv_priv(q);
  1583. vpe_dump_regs(ctx->dev);
  1584. vpdma_dump_regs(ctx->dev->vpdma);
  1585. }
  1586. static struct vb2_ops vpe_qops = {
  1587. .queue_setup = vpe_queue_setup,
  1588. .buf_prepare = vpe_buf_prepare,
  1589. .buf_queue = vpe_buf_queue,
  1590. .wait_prepare = vb2_ops_wait_prepare,
  1591. .wait_finish = vb2_ops_wait_finish,
  1592. .start_streaming = vpe_start_streaming,
  1593. .stop_streaming = vpe_stop_streaming,
  1594. };
  1595. static int queue_init(void *priv, struct vb2_queue *src_vq,
  1596. struct vb2_queue *dst_vq)
  1597. {
  1598. struct vpe_ctx *ctx = priv;
  1599. struct vpe_dev *dev = ctx->dev;
  1600. int ret;
  1601. memset(src_vq, 0, sizeof(*src_vq));
  1602. src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
  1603. src_vq->io_modes = VB2_MMAP | VB2_DMABUF;
  1604. src_vq->drv_priv = ctx;
  1605. src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
  1606. src_vq->ops = &vpe_qops;
  1607. src_vq->mem_ops = &vb2_dma_contig_memops;
  1608. src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
  1609. src_vq->lock = &dev->dev_mutex;
  1610. ret = vb2_queue_init(src_vq);
  1611. if (ret)
  1612. return ret;
  1613. memset(dst_vq, 0, sizeof(*dst_vq));
  1614. dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
  1615. dst_vq->io_modes = VB2_MMAP | VB2_DMABUF;
  1616. dst_vq->drv_priv = ctx;
  1617. dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
  1618. dst_vq->ops = &vpe_qops;
  1619. dst_vq->mem_ops = &vb2_dma_contig_memops;
  1620. dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
  1621. dst_vq->lock = &dev->dev_mutex;
  1622. return vb2_queue_init(dst_vq);
  1623. }
  1624. static const struct v4l2_ctrl_config vpe_bufs_per_job = {
  1625. .ops = &vpe_ctrl_ops,
  1626. .id = V4L2_CID_VPE_BUFS_PER_JOB,
  1627. .name = "Buffers Per Transaction",
  1628. .type = V4L2_CTRL_TYPE_INTEGER,
  1629. .def = VPE_DEF_BUFS_PER_JOB,
  1630. .min = 1,
  1631. .max = VIDEO_MAX_FRAME,
  1632. .step = 1,
  1633. };
  1634. /*
  1635. * File operations
  1636. */
  1637. static int vpe_open(struct file *file)
  1638. {
  1639. struct vpe_dev *dev = video_drvdata(file);
  1640. struct vpe_q_data *s_q_data;
  1641. struct v4l2_ctrl_handler *hdl;
  1642. struct vpe_ctx *ctx;
  1643. int ret;
  1644. vpe_dbg(dev, "vpe_open\n");
  1645. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  1646. if (!ctx)
  1647. return -ENOMEM;
  1648. ctx->dev = dev;
  1649. if (mutex_lock_interruptible(&dev->dev_mutex)) {
  1650. ret = -ERESTARTSYS;
  1651. goto free_ctx;
  1652. }
  1653. ret = vpdma_create_desc_list(&ctx->desc_list, VPE_DESC_LIST_SIZE,
  1654. VPDMA_LIST_TYPE_NORMAL);
  1655. if (ret != 0)
  1656. goto unlock;
  1657. ret = vpdma_alloc_desc_buf(&ctx->mmr_adb, sizeof(struct vpe_mmr_adb));
  1658. if (ret != 0)
  1659. goto free_desc_list;
  1660. ret = vpdma_alloc_desc_buf(&ctx->sc_coeff_h, SC_COEF_SRAM_SIZE);
  1661. if (ret != 0)
  1662. goto free_mmr_adb;
  1663. ret = vpdma_alloc_desc_buf(&ctx->sc_coeff_v, SC_COEF_SRAM_SIZE);
  1664. if (ret != 0)
  1665. goto free_sc_h;
  1666. init_adb_hdrs(ctx);
  1667. v4l2_fh_init(&ctx->fh, video_devdata(file));
  1668. file->private_data = &ctx->fh;
  1669. hdl = &ctx->hdl;
  1670. v4l2_ctrl_handler_init(hdl, 1);
  1671. v4l2_ctrl_new_custom(hdl, &vpe_bufs_per_job, NULL);
  1672. if (hdl->error) {
  1673. ret = hdl->error;
  1674. goto exit_fh;
  1675. }
  1676. ctx->fh.ctrl_handler = hdl;
  1677. v4l2_ctrl_handler_setup(hdl);
  1678. s_q_data = &ctx->q_data[Q_DATA_SRC];
  1679. s_q_data->fmt = &vpe_formats[2];
  1680. s_q_data->width = 1920;
  1681. s_q_data->height = 1080;
  1682. s_q_data->bytesperline[VPE_LUMA] = (s_q_data->width *
  1683. s_q_data->fmt->vpdma_fmt[VPE_LUMA]->depth) >> 3;
  1684. s_q_data->sizeimage[VPE_LUMA] = (s_q_data->bytesperline[VPE_LUMA] *
  1685. s_q_data->height);
  1686. s_q_data->colorspace = V4L2_COLORSPACE_REC709;
  1687. s_q_data->field = V4L2_FIELD_NONE;
  1688. s_q_data->c_rect.left = 0;
  1689. s_q_data->c_rect.top = 0;
  1690. s_q_data->c_rect.width = s_q_data->width;
  1691. s_q_data->c_rect.height = s_q_data->height;
  1692. s_q_data->flags = 0;
  1693. ctx->q_data[Q_DATA_DST] = *s_q_data;
  1694. set_dei_shadow_registers(ctx);
  1695. set_src_registers(ctx);
  1696. set_dst_registers(ctx);
  1697. ret = set_srcdst_params(ctx);
  1698. if (ret)
  1699. goto exit_fh;
  1700. ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx, &queue_init);
  1701. if (IS_ERR(ctx->fh.m2m_ctx)) {
  1702. ret = PTR_ERR(ctx->fh.m2m_ctx);
  1703. goto exit_fh;
  1704. }
  1705. v4l2_fh_add(&ctx->fh);
  1706. /*
  1707. * for now, just report the creation of the first instance, we can later
  1708. * optimize the driver to enable or disable clocks when the first
  1709. * instance is created or the last instance released
  1710. */
  1711. if (atomic_inc_return(&dev->num_instances) == 1)
  1712. vpe_dbg(dev, "first instance created\n");
  1713. ctx->bufs_per_job = VPE_DEF_BUFS_PER_JOB;
  1714. ctx->load_mmrs = true;
  1715. vpe_dbg(dev, "created instance %p, m2m_ctx: %p\n",
  1716. ctx, ctx->fh.m2m_ctx);
  1717. mutex_unlock(&dev->dev_mutex);
  1718. return 0;
  1719. exit_fh:
  1720. v4l2_ctrl_handler_free(hdl);
  1721. v4l2_fh_exit(&ctx->fh);
  1722. vpdma_free_desc_buf(&ctx->sc_coeff_v);
  1723. free_sc_h:
  1724. vpdma_free_desc_buf(&ctx->sc_coeff_h);
  1725. free_mmr_adb:
  1726. vpdma_free_desc_buf(&ctx->mmr_adb);
  1727. free_desc_list:
  1728. vpdma_free_desc_list(&ctx->desc_list);
  1729. unlock:
  1730. mutex_unlock(&dev->dev_mutex);
  1731. free_ctx:
  1732. kfree(ctx);
  1733. return ret;
  1734. }
  1735. static int vpe_release(struct file *file)
  1736. {
  1737. struct vpe_dev *dev = video_drvdata(file);
  1738. struct vpe_ctx *ctx = file2ctx(file);
  1739. vpe_dbg(dev, "releasing instance %p\n", ctx);
  1740. mutex_lock(&dev->dev_mutex);
  1741. free_vbs(ctx);
  1742. free_mv_buffers(ctx);
  1743. vpdma_free_desc_list(&ctx->desc_list);
  1744. vpdma_free_desc_buf(&ctx->mmr_adb);
  1745. v4l2_fh_del(&ctx->fh);
  1746. v4l2_fh_exit(&ctx->fh);
  1747. v4l2_ctrl_handler_free(&ctx->hdl);
  1748. v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
  1749. kfree(ctx);
  1750. /*
  1751. * for now, just report the release of the last instance, we can later
  1752. * optimize the driver to enable or disable clocks when the first
  1753. * instance is created or the last instance released
  1754. */
  1755. if (atomic_dec_return(&dev->num_instances) == 0)
  1756. vpe_dbg(dev, "last instance released\n");
  1757. mutex_unlock(&dev->dev_mutex);
  1758. return 0;
  1759. }
  1760. static const struct v4l2_file_operations vpe_fops = {
  1761. .owner = THIS_MODULE,
  1762. .open = vpe_open,
  1763. .release = vpe_release,
  1764. .poll = v4l2_m2m_fop_poll,
  1765. .unlocked_ioctl = video_ioctl2,
  1766. .mmap = v4l2_m2m_fop_mmap,
  1767. };
  1768. static struct video_device vpe_videodev = {
  1769. .name = VPE_MODULE_NAME,
  1770. .fops = &vpe_fops,
  1771. .ioctl_ops = &vpe_ioctl_ops,
  1772. .minor = -1,
  1773. .release = video_device_release_empty,
  1774. .vfl_dir = VFL_DIR_M2M,
  1775. };
  1776. static struct v4l2_m2m_ops m2m_ops = {
  1777. .device_run = device_run,
  1778. .job_ready = job_ready,
  1779. .job_abort = job_abort,
  1780. .lock = vpe_lock,
  1781. .unlock = vpe_unlock,
  1782. };
  1783. static int vpe_runtime_get(struct platform_device *pdev)
  1784. {
  1785. int r;
  1786. dev_dbg(&pdev->dev, "vpe_runtime_get\n");
  1787. r = pm_runtime_get_sync(&pdev->dev);
  1788. WARN_ON(r < 0);
  1789. return r < 0 ? r : 0;
  1790. }
  1791. static void vpe_runtime_put(struct platform_device *pdev)
  1792. {
  1793. int r;
  1794. dev_dbg(&pdev->dev, "vpe_runtime_put\n");
  1795. r = pm_runtime_put_sync(&pdev->dev);
  1796. WARN_ON(r < 0 && r != -ENOSYS);
  1797. }
  1798. static void vpe_fw_cb(struct platform_device *pdev)
  1799. {
  1800. struct vpe_dev *dev = platform_get_drvdata(pdev);
  1801. struct video_device *vfd;
  1802. int ret;
  1803. vfd = &dev->vfd;
  1804. *vfd = vpe_videodev;
  1805. vfd->lock = &dev->dev_mutex;
  1806. vfd->v4l2_dev = &dev->v4l2_dev;
  1807. ret = video_register_device(vfd, VFL_TYPE_GRABBER, 0);
  1808. if (ret) {
  1809. vpe_err(dev, "Failed to register video device\n");
  1810. vpe_set_clock_enable(dev, 0);
  1811. vpe_runtime_put(pdev);
  1812. pm_runtime_disable(&pdev->dev);
  1813. v4l2_m2m_release(dev->m2m_dev);
  1814. vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
  1815. v4l2_device_unregister(&dev->v4l2_dev);
  1816. return;
  1817. }
  1818. video_set_drvdata(vfd, dev);
  1819. snprintf(vfd->name, sizeof(vfd->name), "%s", vpe_videodev.name);
  1820. dev_info(dev->v4l2_dev.dev, "Device registered as /dev/video%d\n",
  1821. vfd->num);
  1822. }
  1823. static int vpe_probe(struct platform_device *pdev)
  1824. {
  1825. struct vpe_dev *dev;
  1826. int ret, irq, func;
  1827. dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
  1828. if (!dev)
  1829. return -ENOMEM;
  1830. spin_lock_init(&dev->lock);
  1831. ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
  1832. if (ret)
  1833. return ret;
  1834. atomic_set(&dev->num_instances, 0);
  1835. mutex_init(&dev->dev_mutex);
  1836. dev->res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  1837. "vpe_top");
  1838. /*
  1839. * HACK: we get resource info from device tree in the form of a list of
  1840. * VPE sub blocks, the driver currently uses only the base of vpe_top
  1841. * for register access, the driver should be changed later to access
  1842. * registers based on the sub block base addresses
  1843. */
  1844. dev->base = devm_ioremap(&pdev->dev, dev->res->start, SZ_32K);
  1845. if (!dev->base) {
  1846. ret = -ENOMEM;
  1847. goto v4l2_dev_unreg;
  1848. }
  1849. irq = platform_get_irq(pdev, 0);
  1850. ret = devm_request_irq(&pdev->dev, irq, vpe_irq, 0, VPE_MODULE_NAME,
  1851. dev);
  1852. if (ret)
  1853. goto v4l2_dev_unreg;
  1854. platform_set_drvdata(pdev, dev);
  1855. dev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
  1856. if (IS_ERR(dev->alloc_ctx)) {
  1857. vpe_err(dev, "Failed to alloc vb2 context\n");
  1858. ret = PTR_ERR(dev->alloc_ctx);
  1859. goto v4l2_dev_unreg;
  1860. }
  1861. dev->m2m_dev = v4l2_m2m_init(&m2m_ops);
  1862. if (IS_ERR(dev->m2m_dev)) {
  1863. vpe_err(dev, "Failed to init mem2mem device\n");
  1864. ret = PTR_ERR(dev->m2m_dev);
  1865. goto rel_ctx;
  1866. }
  1867. pm_runtime_enable(&pdev->dev);
  1868. ret = vpe_runtime_get(pdev);
  1869. if (ret)
  1870. goto rel_m2m;
  1871. /* Perform clk enable followed by reset */
  1872. vpe_set_clock_enable(dev, 1);
  1873. vpe_top_reset(dev);
  1874. func = read_field_reg(dev, VPE_PID, VPE_PID_FUNC_MASK,
  1875. VPE_PID_FUNC_SHIFT);
  1876. vpe_dbg(dev, "VPE PID function %x\n", func);
  1877. vpe_top_vpdma_reset(dev);
  1878. dev->sc = sc_create(pdev);
  1879. if (IS_ERR(dev->sc)) {
  1880. ret = PTR_ERR(dev->sc);
  1881. goto runtime_put;
  1882. }
  1883. dev->csc = csc_create(pdev);
  1884. if (IS_ERR(dev->csc)) {
  1885. ret = PTR_ERR(dev->csc);
  1886. goto runtime_put;
  1887. }
  1888. dev->vpdma = vpdma_create(pdev, vpe_fw_cb);
  1889. if (IS_ERR(dev->vpdma)) {
  1890. ret = PTR_ERR(dev->vpdma);
  1891. goto runtime_put;
  1892. }
  1893. return 0;
  1894. runtime_put:
  1895. vpe_runtime_put(pdev);
  1896. rel_m2m:
  1897. pm_runtime_disable(&pdev->dev);
  1898. v4l2_m2m_release(dev->m2m_dev);
  1899. rel_ctx:
  1900. vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
  1901. v4l2_dev_unreg:
  1902. v4l2_device_unregister(&dev->v4l2_dev);
  1903. return ret;
  1904. }
  1905. static int vpe_remove(struct platform_device *pdev)
  1906. {
  1907. struct vpe_dev *dev = platform_get_drvdata(pdev);
  1908. v4l2_info(&dev->v4l2_dev, "Removing " VPE_MODULE_NAME);
  1909. v4l2_m2m_release(dev->m2m_dev);
  1910. video_unregister_device(&dev->vfd);
  1911. v4l2_device_unregister(&dev->v4l2_dev);
  1912. vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
  1913. vpe_set_clock_enable(dev, 0);
  1914. vpe_runtime_put(pdev);
  1915. pm_runtime_disable(&pdev->dev);
  1916. return 0;
  1917. }
  1918. #if defined(CONFIG_OF)
  1919. static const struct of_device_id vpe_of_match[] = {
  1920. {
  1921. .compatible = "ti,vpe",
  1922. },
  1923. {},
  1924. };
  1925. #endif
  1926. static struct platform_driver vpe_pdrv = {
  1927. .probe = vpe_probe,
  1928. .remove = vpe_remove,
  1929. .driver = {
  1930. .name = VPE_MODULE_NAME,
  1931. .of_match_table = of_match_ptr(vpe_of_match),
  1932. },
  1933. };
  1934. module_platform_driver(vpe_pdrv);
  1935. MODULE_DESCRIPTION("TI VPE driver");
  1936. MODULE_AUTHOR("Dale Farnsworth, <dale@farnsworth.org>");
  1937. MODULE_LICENSE("GPL");