vpe.c 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309
  1. /*
  2. * TI VPE mem2mem driver, based on the virtual v4l2-mem2mem example driver
  3. *
  4. * Copyright (c) 2013 Texas Instruments Inc.
  5. * David Griego, <dagriego@biglakesoftware.com>
  6. * Dale Farnsworth, <dale@farnsworth.org>
  7. * Archit Taneja, <archit@ti.com>
  8. *
  9. * Copyright (c) 2009-2010 Samsung Electronics Co., Ltd.
  10. * Pawel Osciak, <pawel@osciak.com>
  11. * Marek Szyprowski, <m.szyprowski@samsung.com>
  12. *
  13. * Based on the virtual v4l2-mem2mem example device
  14. *
  15. * This program is free software; you can redistribute it and/or modify it
  16. * under the terms of the GNU General Public License version 2 as published by
  17. * the Free Software Foundation
  18. */
  19. #include <linux/delay.h>
  20. #include <linux/dma-mapping.h>
  21. #include <linux/err.h>
  22. #include <linux/fs.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/io.h>
  25. #include <linux/ioctl.h>
  26. #include <linux/module.h>
  27. #include <linux/of.h>
  28. #include <linux/platform_device.h>
  29. #include <linux/pm_runtime.h>
  30. #include <linux/sched.h>
  31. #include <linux/slab.h>
  32. #include <linux/videodev2.h>
  33. #include <linux/log2.h>
  34. #include <linux/sizes.h>
  35. #include <media/v4l2-common.h>
  36. #include <media/v4l2-ctrls.h>
  37. #include <media/v4l2-device.h>
  38. #include <media/v4l2-event.h>
  39. #include <media/v4l2-ioctl.h>
  40. #include <media/v4l2-mem2mem.h>
  41. #include <media/videobuf2-v4l2.h>
  42. #include <media/videobuf2-dma-contig.h>
  43. #include "vpdma.h"
  44. #include "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 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_ctrl_handler hdl;
  344. unsigned int field; /* current field */
  345. unsigned int sequence; /* current frame/field seq */
  346. unsigned int aborting; /* abort after next irq */
  347. unsigned int bufs_per_job; /* input buffers per batch */
  348. unsigned int bufs_completed; /* bufs done in this batch */
  349. struct vpe_q_data q_data[2]; /* src & dst queue data */
  350. struct vb2_v4l2_buffer *src_vbs[VPE_MAX_SRC_BUFS];
  351. struct vb2_v4l2_buffer *dst_vb;
  352. dma_addr_t mv_buf_dma[2]; /* dma addrs of motion vector in/out bufs */
  353. void *mv_buf[2]; /* virtual addrs of motion vector bufs */
  354. size_t mv_buf_size; /* current motion vector buffer size */
  355. struct vpdma_buf mmr_adb; /* shadow reg addr/data block */
  356. struct vpdma_buf sc_coeff_h; /* h coeff buffer */
  357. struct vpdma_buf sc_coeff_v; /* v coeff buffer */
  358. struct vpdma_desc_list desc_list; /* DMA descriptor list */
  359. bool deinterlacing; /* using de-interlacer */
  360. bool load_mmrs; /* have new shadow reg values */
  361. unsigned int src_mv_buf_selector;
  362. };
  363. /*
  364. * M2M devices get 2 queues.
  365. * Return the queue given the type.
  366. */
  367. static struct vpe_q_data *get_q_data(struct vpe_ctx *ctx,
  368. enum v4l2_buf_type type)
  369. {
  370. switch (type) {
  371. case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
  372. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  373. return &ctx->q_data[Q_DATA_SRC];
  374. case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
  375. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  376. return &ctx->q_data[Q_DATA_DST];
  377. default:
  378. BUG();
  379. }
  380. return NULL;
  381. }
  382. static u32 read_reg(struct vpe_dev *dev, int offset)
  383. {
  384. return ioread32(dev->base + offset);
  385. }
  386. static void write_reg(struct vpe_dev *dev, int offset, u32 value)
  387. {
  388. iowrite32(value, dev->base + offset);
  389. }
  390. /* register field read/write helpers */
  391. static int get_field(u32 value, u32 mask, int shift)
  392. {
  393. return (value & (mask << shift)) >> shift;
  394. }
  395. static int read_field_reg(struct vpe_dev *dev, int offset, u32 mask, int shift)
  396. {
  397. return get_field(read_reg(dev, offset), mask, shift);
  398. }
  399. static void write_field(u32 *valp, u32 field, u32 mask, int shift)
  400. {
  401. u32 val = *valp;
  402. val &= ~(mask << shift);
  403. val |= (field & mask) << shift;
  404. *valp = val;
  405. }
  406. static void write_field_reg(struct vpe_dev *dev, int offset, u32 field,
  407. u32 mask, int shift)
  408. {
  409. u32 val = read_reg(dev, offset);
  410. write_field(&val, field, mask, shift);
  411. write_reg(dev, offset, val);
  412. }
  413. /*
  414. * DMA address/data block for the shadow registers
  415. */
  416. struct vpe_mmr_adb {
  417. struct vpdma_adb_hdr out_fmt_hdr;
  418. u32 out_fmt_reg[1];
  419. u32 out_fmt_pad[3];
  420. struct vpdma_adb_hdr us1_hdr;
  421. u32 us1_regs[8];
  422. struct vpdma_adb_hdr us2_hdr;
  423. u32 us2_regs[8];
  424. struct vpdma_adb_hdr us3_hdr;
  425. u32 us3_regs[8];
  426. struct vpdma_adb_hdr dei_hdr;
  427. u32 dei_regs[8];
  428. struct vpdma_adb_hdr sc_hdr0;
  429. u32 sc_regs0[7];
  430. u32 sc_pad0[1];
  431. struct vpdma_adb_hdr sc_hdr8;
  432. u32 sc_regs8[6];
  433. u32 sc_pad8[2];
  434. struct vpdma_adb_hdr sc_hdr17;
  435. u32 sc_regs17[9];
  436. u32 sc_pad17[3];
  437. struct vpdma_adb_hdr csc_hdr;
  438. u32 csc_regs[6];
  439. u32 csc_pad[2];
  440. };
  441. #define GET_OFFSET_TOP(ctx, obj, reg) \
  442. ((obj)->res->start - ctx->dev->res->start + reg)
  443. #define VPE_SET_MMR_ADB_HDR(ctx, hdr, regs, offset_a) \
  444. VPDMA_SET_MMR_ADB_HDR(ctx->mmr_adb, vpe_mmr_adb, hdr, regs, offset_a)
  445. /*
  446. * Set the headers for all of the address/data block structures.
  447. */
  448. static void init_adb_hdrs(struct vpe_ctx *ctx)
  449. {
  450. VPE_SET_MMR_ADB_HDR(ctx, out_fmt_hdr, out_fmt_reg, VPE_CLK_FORMAT_SELECT);
  451. VPE_SET_MMR_ADB_HDR(ctx, us1_hdr, us1_regs, VPE_US1_R0);
  452. VPE_SET_MMR_ADB_HDR(ctx, us2_hdr, us2_regs, VPE_US2_R0);
  453. VPE_SET_MMR_ADB_HDR(ctx, us3_hdr, us3_regs, VPE_US3_R0);
  454. VPE_SET_MMR_ADB_HDR(ctx, dei_hdr, dei_regs, VPE_DEI_FRAME_SIZE);
  455. VPE_SET_MMR_ADB_HDR(ctx, sc_hdr0, sc_regs0,
  456. GET_OFFSET_TOP(ctx, ctx->dev->sc, CFG_SC0));
  457. VPE_SET_MMR_ADB_HDR(ctx, sc_hdr8, sc_regs8,
  458. GET_OFFSET_TOP(ctx, ctx->dev->sc, CFG_SC8));
  459. VPE_SET_MMR_ADB_HDR(ctx, sc_hdr17, sc_regs17,
  460. GET_OFFSET_TOP(ctx, ctx->dev->sc, CFG_SC17));
  461. VPE_SET_MMR_ADB_HDR(ctx, csc_hdr, csc_regs,
  462. GET_OFFSET_TOP(ctx, ctx->dev->csc, CSC_CSC00));
  463. };
  464. /*
  465. * Allocate or re-allocate the motion vector DMA buffers
  466. * There are two buffers, one for input and one for output.
  467. * However, the roles are reversed after each field is processed.
  468. * In other words, after each field is processed, the previous
  469. * output (dst) MV buffer becomes the new input (src) MV buffer.
  470. */
  471. static int realloc_mv_buffers(struct vpe_ctx *ctx, size_t size)
  472. {
  473. struct device *dev = ctx->dev->v4l2_dev.dev;
  474. if (ctx->mv_buf_size == size)
  475. return 0;
  476. if (ctx->mv_buf[0])
  477. dma_free_coherent(dev, ctx->mv_buf_size, ctx->mv_buf[0],
  478. ctx->mv_buf_dma[0]);
  479. if (ctx->mv_buf[1])
  480. dma_free_coherent(dev, ctx->mv_buf_size, ctx->mv_buf[1],
  481. ctx->mv_buf_dma[1]);
  482. if (size == 0)
  483. return 0;
  484. ctx->mv_buf[0] = dma_alloc_coherent(dev, size, &ctx->mv_buf_dma[0],
  485. GFP_KERNEL);
  486. if (!ctx->mv_buf[0]) {
  487. vpe_err(ctx->dev, "failed to allocate motion vector buffer\n");
  488. return -ENOMEM;
  489. }
  490. ctx->mv_buf[1] = dma_alloc_coherent(dev, size, &ctx->mv_buf_dma[1],
  491. GFP_KERNEL);
  492. if (!ctx->mv_buf[1]) {
  493. vpe_err(ctx->dev, "failed to allocate motion vector buffer\n");
  494. dma_free_coherent(dev, size, ctx->mv_buf[0],
  495. ctx->mv_buf_dma[0]);
  496. return -ENOMEM;
  497. }
  498. ctx->mv_buf_size = size;
  499. ctx->src_mv_buf_selector = 0;
  500. return 0;
  501. }
  502. static void free_mv_buffers(struct vpe_ctx *ctx)
  503. {
  504. realloc_mv_buffers(ctx, 0);
  505. }
  506. /*
  507. * While de-interlacing, we keep the two most recent input buffers
  508. * around. This function frees those two buffers when we have
  509. * finished processing the current stream.
  510. */
  511. static void free_vbs(struct vpe_ctx *ctx)
  512. {
  513. struct vpe_dev *dev = ctx->dev;
  514. unsigned long flags;
  515. if (ctx->src_vbs[2] == NULL)
  516. return;
  517. spin_lock_irqsave(&dev->lock, flags);
  518. if (ctx->src_vbs[2]) {
  519. v4l2_m2m_buf_done(ctx->src_vbs[2], VB2_BUF_STATE_DONE);
  520. v4l2_m2m_buf_done(ctx->src_vbs[1], VB2_BUF_STATE_DONE);
  521. }
  522. spin_unlock_irqrestore(&dev->lock, flags);
  523. }
  524. /*
  525. * Enable or disable the VPE clocks
  526. */
  527. static void vpe_set_clock_enable(struct vpe_dev *dev, bool on)
  528. {
  529. u32 val = 0;
  530. if (on)
  531. val = VPE_DATA_PATH_CLK_ENABLE | VPE_VPEDMA_CLK_ENABLE;
  532. write_reg(dev, VPE_CLK_ENABLE, val);
  533. }
  534. static void vpe_top_reset(struct vpe_dev *dev)
  535. {
  536. write_field_reg(dev, VPE_CLK_RESET, 1, VPE_DATA_PATH_CLK_RESET_MASK,
  537. VPE_DATA_PATH_CLK_RESET_SHIFT);
  538. usleep_range(100, 150);
  539. write_field_reg(dev, VPE_CLK_RESET, 0, VPE_DATA_PATH_CLK_RESET_MASK,
  540. VPE_DATA_PATH_CLK_RESET_SHIFT);
  541. }
  542. static void vpe_top_vpdma_reset(struct vpe_dev *dev)
  543. {
  544. write_field_reg(dev, VPE_CLK_RESET, 1, VPE_VPDMA_CLK_RESET_MASK,
  545. VPE_VPDMA_CLK_RESET_SHIFT);
  546. usleep_range(100, 150);
  547. write_field_reg(dev, VPE_CLK_RESET, 0, VPE_VPDMA_CLK_RESET_MASK,
  548. VPE_VPDMA_CLK_RESET_SHIFT);
  549. }
  550. /*
  551. * Load the correct of upsampler coefficients into the shadow MMRs
  552. */
  553. static void set_us_coefficients(struct vpe_ctx *ctx)
  554. {
  555. struct vpe_mmr_adb *mmr_adb = ctx->mmr_adb.addr;
  556. struct vpe_q_data *s_q_data = &ctx->q_data[Q_DATA_SRC];
  557. u32 *us1_reg = &mmr_adb->us1_regs[0];
  558. u32 *us2_reg = &mmr_adb->us2_regs[0];
  559. u32 *us3_reg = &mmr_adb->us3_regs[0];
  560. const unsigned short *cp, *end_cp;
  561. cp = &us_coeffs[0].anchor_fid0_c0;
  562. if (s_q_data->flags & Q_DATA_INTERLACED) /* interlaced */
  563. cp += sizeof(us_coeffs[0]) / sizeof(*cp);
  564. end_cp = cp + sizeof(us_coeffs[0]) / sizeof(*cp);
  565. while (cp < end_cp) {
  566. write_field(us1_reg, *cp++, VPE_US_C0_MASK, VPE_US_C0_SHIFT);
  567. write_field(us1_reg, *cp++, VPE_US_C1_MASK, VPE_US_C1_SHIFT);
  568. *us2_reg++ = *us1_reg;
  569. *us3_reg++ = *us1_reg++;
  570. }
  571. ctx->load_mmrs = true;
  572. }
  573. /*
  574. * Set the upsampler config mode and the VPDMA line mode in the shadow MMRs.
  575. */
  576. static void set_cfg_and_line_modes(struct vpe_ctx *ctx)
  577. {
  578. struct vpe_fmt *fmt = ctx->q_data[Q_DATA_SRC].fmt;
  579. struct vpe_mmr_adb *mmr_adb = ctx->mmr_adb.addr;
  580. u32 *us1_reg0 = &mmr_adb->us1_regs[0];
  581. u32 *us2_reg0 = &mmr_adb->us2_regs[0];
  582. u32 *us3_reg0 = &mmr_adb->us3_regs[0];
  583. int line_mode = 1;
  584. int cfg_mode = 1;
  585. /*
  586. * Cfg Mode 0: YUV420 source, enable upsampler, DEI is de-interlacing.
  587. * Cfg Mode 1: YUV422 source, disable upsampler, DEI is de-interlacing.
  588. */
  589. if (fmt->fourcc == V4L2_PIX_FMT_NV12) {
  590. cfg_mode = 0;
  591. line_mode = 0; /* double lines to line buffer */
  592. }
  593. write_field(us1_reg0, cfg_mode, VPE_US_MODE_MASK, VPE_US_MODE_SHIFT);
  594. write_field(us2_reg0, cfg_mode, VPE_US_MODE_MASK, VPE_US_MODE_SHIFT);
  595. write_field(us3_reg0, cfg_mode, VPE_US_MODE_MASK, VPE_US_MODE_SHIFT);
  596. /* regs for now */
  597. vpdma_set_line_mode(ctx->dev->vpdma, line_mode, VPE_CHAN_CHROMA1_IN);
  598. vpdma_set_line_mode(ctx->dev->vpdma, line_mode, VPE_CHAN_CHROMA2_IN);
  599. vpdma_set_line_mode(ctx->dev->vpdma, line_mode, VPE_CHAN_CHROMA3_IN);
  600. /* frame start for input luma */
  601. vpdma_set_frame_start_event(ctx->dev->vpdma, VPDMA_FSEVENT_CHANNEL_ACTIVE,
  602. VPE_CHAN_LUMA1_IN);
  603. vpdma_set_frame_start_event(ctx->dev->vpdma, VPDMA_FSEVENT_CHANNEL_ACTIVE,
  604. VPE_CHAN_LUMA2_IN);
  605. vpdma_set_frame_start_event(ctx->dev->vpdma, VPDMA_FSEVENT_CHANNEL_ACTIVE,
  606. VPE_CHAN_LUMA3_IN);
  607. /* frame start for input chroma */
  608. vpdma_set_frame_start_event(ctx->dev->vpdma, VPDMA_FSEVENT_CHANNEL_ACTIVE,
  609. VPE_CHAN_CHROMA1_IN);
  610. vpdma_set_frame_start_event(ctx->dev->vpdma, VPDMA_FSEVENT_CHANNEL_ACTIVE,
  611. VPE_CHAN_CHROMA2_IN);
  612. vpdma_set_frame_start_event(ctx->dev->vpdma, VPDMA_FSEVENT_CHANNEL_ACTIVE,
  613. VPE_CHAN_CHROMA3_IN);
  614. /* frame start for MV in client */
  615. vpdma_set_frame_start_event(ctx->dev->vpdma, VPDMA_FSEVENT_CHANNEL_ACTIVE,
  616. VPE_CHAN_MV_IN);
  617. ctx->load_mmrs = true;
  618. }
  619. /*
  620. * Set the shadow registers that are modified when the source
  621. * format changes.
  622. */
  623. static void set_src_registers(struct vpe_ctx *ctx)
  624. {
  625. set_us_coefficients(ctx);
  626. }
  627. /*
  628. * Set the shadow registers that are modified when the destination
  629. * format changes.
  630. */
  631. static void set_dst_registers(struct vpe_ctx *ctx)
  632. {
  633. struct vpe_mmr_adb *mmr_adb = ctx->mmr_adb.addr;
  634. enum v4l2_colorspace clrspc = ctx->q_data[Q_DATA_DST].colorspace;
  635. struct vpe_fmt *fmt = ctx->q_data[Q_DATA_DST].fmt;
  636. u32 val = 0;
  637. if (clrspc == V4L2_COLORSPACE_SRGB)
  638. val |= VPE_RGB_OUT_SELECT;
  639. else if (fmt->fourcc == V4L2_PIX_FMT_NV16)
  640. val |= VPE_COLOR_SEPARATE_422;
  641. /*
  642. * the source of CHR_DS and CSC is always the scaler, irrespective of
  643. * whether it's used or not
  644. */
  645. val |= VPE_DS_SRC_DEI_SCALER | VPE_CSC_SRC_DEI_SCALER;
  646. if (fmt->fourcc != V4L2_PIX_FMT_NV12)
  647. val |= VPE_DS_BYPASS;
  648. mmr_adb->out_fmt_reg[0] = val;
  649. ctx->load_mmrs = true;
  650. }
  651. /*
  652. * Set the de-interlacer shadow register values
  653. */
  654. static void set_dei_regs(struct vpe_ctx *ctx)
  655. {
  656. struct vpe_mmr_adb *mmr_adb = ctx->mmr_adb.addr;
  657. struct vpe_q_data *s_q_data = &ctx->q_data[Q_DATA_SRC];
  658. unsigned int src_h = s_q_data->c_rect.height;
  659. unsigned int src_w = s_q_data->c_rect.width;
  660. u32 *dei_mmr0 = &mmr_adb->dei_regs[0];
  661. bool deinterlace = true;
  662. u32 val = 0;
  663. /*
  664. * according to TRM, we should set DEI in progressive bypass mode when
  665. * the input content is progressive, however, DEI is bypassed correctly
  666. * for both progressive and interlace content in interlace bypass mode.
  667. * It has been recommended not to use progressive bypass mode.
  668. */
  669. if ((!ctx->deinterlacing && (s_q_data->flags & Q_DATA_INTERLACED)) ||
  670. !(s_q_data->flags & Q_DATA_INTERLACED)) {
  671. deinterlace = false;
  672. val = VPE_DEI_INTERLACE_BYPASS;
  673. }
  674. src_h = deinterlace ? src_h * 2 : src_h;
  675. val |= (src_h << VPE_DEI_HEIGHT_SHIFT) |
  676. (src_w << VPE_DEI_WIDTH_SHIFT) |
  677. VPE_DEI_FIELD_FLUSH;
  678. *dei_mmr0 = val;
  679. ctx->load_mmrs = true;
  680. }
  681. static void set_dei_shadow_registers(struct vpe_ctx *ctx)
  682. {
  683. struct vpe_mmr_adb *mmr_adb = ctx->mmr_adb.addr;
  684. u32 *dei_mmr = &mmr_adb->dei_regs[0];
  685. const struct vpe_dei_regs *cur = &dei_regs;
  686. dei_mmr[2] = cur->mdt_spacial_freq_thr_reg;
  687. dei_mmr[3] = cur->edi_config_reg;
  688. dei_mmr[4] = cur->edi_lut_reg0;
  689. dei_mmr[5] = cur->edi_lut_reg1;
  690. dei_mmr[6] = cur->edi_lut_reg2;
  691. dei_mmr[7] = cur->edi_lut_reg3;
  692. ctx->load_mmrs = true;
  693. }
  694. /*
  695. * Set the shadow registers whose values are modified when either the
  696. * source or destination format is changed.
  697. */
  698. static int set_srcdst_params(struct vpe_ctx *ctx)
  699. {
  700. struct vpe_q_data *s_q_data = &ctx->q_data[Q_DATA_SRC];
  701. struct vpe_q_data *d_q_data = &ctx->q_data[Q_DATA_DST];
  702. struct vpe_mmr_adb *mmr_adb = ctx->mmr_adb.addr;
  703. unsigned int src_w = s_q_data->c_rect.width;
  704. unsigned int src_h = s_q_data->c_rect.height;
  705. unsigned int dst_w = d_q_data->c_rect.width;
  706. unsigned int dst_h = d_q_data->c_rect.height;
  707. size_t mv_buf_size;
  708. int ret;
  709. ctx->sequence = 0;
  710. ctx->field = V4L2_FIELD_TOP;
  711. if ((s_q_data->flags & Q_DATA_INTERLACED) &&
  712. !(d_q_data->flags & Q_DATA_INTERLACED)) {
  713. int bytes_per_line;
  714. const struct vpdma_data_format *mv =
  715. &vpdma_misc_fmts[VPDMA_DATA_FMT_MV];
  716. /*
  717. * we make sure that the source image has a 16 byte aligned
  718. * stride, we need to do the same for the motion vector buffer
  719. * by aligning it's stride to the next 16 byte boundry. this
  720. * extra space will not be used by the de-interlacer, but will
  721. * ensure that vpdma operates correctly
  722. */
  723. bytes_per_line = ALIGN((s_q_data->width * mv->depth) >> 3,
  724. VPDMA_STRIDE_ALIGN);
  725. mv_buf_size = bytes_per_line * s_q_data->height;
  726. ctx->deinterlacing = true;
  727. src_h <<= 1;
  728. } else {
  729. ctx->deinterlacing = false;
  730. mv_buf_size = 0;
  731. }
  732. free_vbs(ctx);
  733. ret = realloc_mv_buffers(ctx, mv_buf_size);
  734. if (ret)
  735. return ret;
  736. set_cfg_and_line_modes(ctx);
  737. set_dei_regs(ctx);
  738. csc_set_coeff(ctx->dev->csc, &mmr_adb->csc_regs[0],
  739. s_q_data->colorspace, d_q_data->colorspace);
  740. sc_set_hs_coeffs(ctx->dev->sc, ctx->sc_coeff_h.addr, src_w, dst_w);
  741. sc_set_vs_coeffs(ctx->dev->sc, ctx->sc_coeff_v.addr, src_h, dst_h);
  742. sc_config_scaler(ctx->dev->sc, &mmr_adb->sc_regs0[0],
  743. &mmr_adb->sc_regs8[0], &mmr_adb->sc_regs17[0],
  744. src_w, src_h, dst_w, dst_h);
  745. return 0;
  746. }
  747. /*
  748. * Return the vpe_ctx structure for a given struct file
  749. */
  750. static struct vpe_ctx *file2ctx(struct file *file)
  751. {
  752. return container_of(file->private_data, struct vpe_ctx, fh);
  753. }
  754. /*
  755. * mem2mem callbacks
  756. */
  757. /**
  758. * job_ready() - check whether an instance is ready to be scheduled to run
  759. */
  760. static int job_ready(void *priv)
  761. {
  762. struct vpe_ctx *ctx = priv;
  763. int needed = ctx->bufs_per_job;
  764. if (ctx->deinterlacing && ctx->src_vbs[2] == NULL)
  765. needed += 2; /* need additional two most recent fields */
  766. if (v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx) < needed)
  767. return 0;
  768. if (v4l2_m2m_num_dst_bufs_ready(ctx->fh.m2m_ctx) < needed)
  769. return 0;
  770. return 1;
  771. }
  772. static void job_abort(void *priv)
  773. {
  774. struct vpe_ctx *ctx = priv;
  775. /* Will cancel the transaction in the next interrupt handler */
  776. ctx->aborting = 1;
  777. }
  778. /*
  779. * Lock access to the device
  780. */
  781. static void vpe_lock(void *priv)
  782. {
  783. struct vpe_ctx *ctx = priv;
  784. struct vpe_dev *dev = ctx->dev;
  785. mutex_lock(&dev->dev_mutex);
  786. }
  787. static void vpe_unlock(void *priv)
  788. {
  789. struct vpe_ctx *ctx = priv;
  790. struct vpe_dev *dev = ctx->dev;
  791. mutex_unlock(&dev->dev_mutex);
  792. }
  793. static void vpe_dump_regs(struct vpe_dev *dev)
  794. {
  795. #define DUMPREG(r) vpe_dbg(dev, "%-35s %08x\n", #r, read_reg(dev, VPE_##r))
  796. vpe_dbg(dev, "VPE Registers:\n");
  797. DUMPREG(PID);
  798. DUMPREG(SYSCONFIG);
  799. DUMPREG(INT0_STATUS0_RAW);
  800. DUMPREG(INT0_STATUS0);
  801. DUMPREG(INT0_ENABLE0);
  802. DUMPREG(INT0_STATUS1_RAW);
  803. DUMPREG(INT0_STATUS1);
  804. DUMPREG(INT0_ENABLE1);
  805. DUMPREG(CLK_ENABLE);
  806. DUMPREG(CLK_RESET);
  807. DUMPREG(CLK_FORMAT_SELECT);
  808. DUMPREG(CLK_RANGE_MAP);
  809. DUMPREG(US1_R0);
  810. DUMPREG(US1_R1);
  811. DUMPREG(US1_R2);
  812. DUMPREG(US1_R3);
  813. DUMPREG(US1_R4);
  814. DUMPREG(US1_R5);
  815. DUMPREG(US1_R6);
  816. DUMPREG(US1_R7);
  817. DUMPREG(US2_R0);
  818. DUMPREG(US2_R1);
  819. DUMPREG(US2_R2);
  820. DUMPREG(US2_R3);
  821. DUMPREG(US2_R4);
  822. DUMPREG(US2_R5);
  823. DUMPREG(US2_R6);
  824. DUMPREG(US2_R7);
  825. DUMPREG(US3_R0);
  826. DUMPREG(US3_R1);
  827. DUMPREG(US3_R2);
  828. DUMPREG(US3_R3);
  829. DUMPREG(US3_R4);
  830. DUMPREG(US3_R5);
  831. DUMPREG(US3_R6);
  832. DUMPREG(US3_R7);
  833. DUMPREG(DEI_FRAME_SIZE);
  834. DUMPREG(MDT_BYPASS);
  835. DUMPREG(MDT_SF_THRESHOLD);
  836. DUMPREG(EDI_CONFIG);
  837. DUMPREG(DEI_EDI_LUT_R0);
  838. DUMPREG(DEI_EDI_LUT_R1);
  839. DUMPREG(DEI_EDI_LUT_R2);
  840. DUMPREG(DEI_EDI_LUT_R3);
  841. DUMPREG(DEI_FMD_WINDOW_R0);
  842. DUMPREG(DEI_FMD_WINDOW_R1);
  843. DUMPREG(DEI_FMD_CONTROL_R0);
  844. DUMPREG(DEI_FMD_CONTROL_R1);
  845. DUMPREG(DEI_FMD_STATUS_R0);
  846. DUMPREG(DEI_FMD_STATUS_R1);
  847. DUMPREG(DEI_FMD_STATUS_R2);
  848. #undef DUMPREG
  849. sc_dump_regs(dev->sc);
  850. csc_dump_regs(dev->csc);
  851. }
  852. static void add_out_dtd(struct vpe_ctx *ctx, int port)
  853. {
  854. struct vpe_q_data *q_data = &ctx->q_data[Q_DATA_DST];
  855. const struct vpe_port_data *p_data = &port_data[port];
  856. struct vb2_buffer *vb = &ctx->dst_vb->vb2_buf;
  857. struct vpe_fmt *fmt = q_data->fmt;
  858. const struct vpdma_data_format *vpdma_fmt;
  859. int mv_buf_selector = !ctx->src_mv_buf_selector;
  860. dma_addr_t dma_addr;
  861. u32 flags = 0;
  862. if (port == VPE_PORT_MV_OUT) {
  863. vpdma_fmt = &vpdma_misc_fmts[VPDMA_DATA_FMT_MV];
  864. dma_addr = ctx->mv_buf_dma[mv_buf_selector];
  865. } else {
  866. /* to incorporate interleaved formats */
  867. int plane = fmt->coplanar ? p_data->vb_part : 0;
  868. vpdma_fmt = fmt->vpdma_fmt[plane];
  869. dma_addr = vb2_dma_contig_plane_dma_addr(vb, plane);
  870. if (!dma_addr) {
  871. vpe_err(ctx->dev,
  872. "acquiring output buffer(%d) dma_addr failed\n",
  873. port);
  874. return;
  875. }
  876. }
  877. if (q_data->flags & Q_DATA_FRAME_1D)
  878. flags |= VPDMA_DATA_FRAME_1D;
  879. if (q_data->flags & Q_DATA_MODE_TILED)
  880. flags |= VPDMA_DATA_MODE_TILED;
  881. vpdma_add_out_dtd(&ctx->desc_list, q_data->width, &q_data->c_rect,
  882. vpdma_fmt, dma_addr, p_data->channel, flags);
  883. }
  884. static void add_in_dtd(struct vpe_ctx *ctx, int port)
  885. {
  886. struct vpe_q_data *q_data = &ctx->q_data[Q_DATA_SRC];
  887. const struct vpe_port_data *p_data = &port_data[port];
  888. struct vb2_buffer *vb = &ctx->src_vbs[p_data->vb_index]->vb2_buf;
  889. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  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 = vbuf->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_v4l2_buffer *s_vb, *d_vb;
  1051. unsigned long flags;
  1052. u32 irqst0, irqst1;
  1053. irqst0 = read_reg(dev, VPE_INT0_STATUS0);
  1054. if (irqst0) {
  1055. write_reg(dev, VPE_INT0_STATUS0_CLR, irqst0);
  1056. vpe_dbg(dev, "INT0_STATUS0 = 0x%08x\n", irqst0);
  1057. }
  1058. irqst1 = read_reg(dev, VPE_INT0_STATUS1);
  1059. if (irqst1) {
  1060. write_reg(dev, VPE_INT0_STATUS1_CLR, irqst1);
  1061. vpe_dbg(dev, "INT0_STATUS1 = 0x%08x\n", irqst1);
  1062. }
  1063. ctx = v4l2_m2m_get_curr_priv(dev->m2m_dev);
  1064. if (!ctx) {
  1065. vpe_err(dev, "instance released before end of transaction\n");
  1066. goto handled;
  1067. }
  1068. if (irqst1) {
  1069. if (irqst1 & VPE_DEI_ERROR_INT) {
  1070. irqst1 &= ~VPE_DEI_ERROR_INT;
  1071. dei_error(ctx);
  1072. }
  1073. if (irqst1 & VPE_DS1_UV_ERROR_INT) {
  1074. irqst1 &= ~VPE_DS1_UV_ERROR_INT;
  1075. ds1_uv_error(ctx);
  1076. }
  1077. }
  1078. if (irqst0) {
  1079. if (irqst0 & VPE_INT0_LIST0_COMPLETE)
  1080. vpdma_clear_list_stat(ctx->dev->vpdma);
  1081. irqst0 &= ~(VPE_INT0_LIST0_COMPLETE);
  1082. }
  1083. if (irqst0 | irqst1) {
  1084. dev_warn(dev->v4l2_dev.dev, "Unexpected interrupt: "
  1085. "INT0_STATUS0 = 0x%08x, INT0_STATUS1 = 0x%08x\n",
  1086. irqst0, irqst1);
  1087. }
  1088. disable_irqs(ctx);
  1089. vpdma_unmap_desc_buf(dev->vpdma, &ctx->desc_list.buf);
  1090. vpdma_unmap_desc_buf(dev->vpdma, &ctx->mmr_adb);
  1091. vpdma_unmap_desc_buf(dev->vpdma, &ctx->sc_coeff_h);
  1092. vpdma_unmap_desc_buf(dev->vpdma, &ctx->sc_coeff_v);
  1093. vpdma_reset_desc_list(&ctx->desc_list);
  1094. /* the previous dst mv buffer becomes the next src mv buffer */
  1095. ctx->src_mv_buf_selector = !ctx->src_mv_buf_selector;
  1096. if (ctx->aborting)
  1097. goto finished;
  1098. s_vb = ctx->src_vbs[0];
  1099. d_vb = ctx->dst_vb;
  1100. d_vb->flags = s_vb->flags;
  1101. d_vb->vb2_buf.timestamp = s_vb->vb2_buf.timestamp;
  1102. if (s_vb->flags & V4L2_BUF_FLAG_TIMECODE)
  1103. d_vb->timecode = s_vb->timecode;
  1104. d_vb->sequence = ctx->sequence;
  1105. d_q_data = &ctx->q_data[Q_DATA_DST];
  1106. if (d_q_data->flags & Q_DATA_INTERLACED) {
  1107. d_vb->field = ctx->field;
  1108. if (ctx->field == V4L2_FIELD_BOTTOM) {
  1109. ctx->sequence++;
  1110. ctx->field = V4L2_FIELD_TOP;
  1111. } else {
  1112. WARN_ON(ctx->field != V4L2_FIELD_TOP);
  1113. ctx->field = V4L2_FIELD_BOTTOM;
  1114. }
  1115. } else {
  1116. d_vb->field = V4L2_FIELD_NONE;
  1117. ctx->sequence++;
  1118. }
  1119. if (ctx->deinterlacing)
  1120. s_vb = ctx->src_vbs[2];
  1121. spin_lock_irqsave(&dev->lock, flags);
  1122. v4l2_m2m_buf_done(s_vb, VB2_BUF_STATE_DONE);
  1123. v4l2_m2m_buf_done(d_vb, VB2_BUF_STATE_DONE);
  1124. spin_unlock_irqrestore(&dev->lock, flags);
  1125. if (ctx->deinterlacing) {
  1126. ctx->src_vbs[2] = ctx->src_vbs[1];
  1127. ctx->src_vbs[1] = ctx->src_vbs[0];
  1128. }
  1129. ctx->bufs_completed++;
  1130. if (ctx->bufs_completed < ctx->bufs_per_job) {
  1131. device_run(ctx);
  1132. goto handled;
  1133. }
  1134. finished:
  1135. vpe_dbg(ctx->dev, "finishing transaction\n");
  1136. ctx->bufs_completed = 0;
  1137. v4l2_m2m_job_finish(dev->m2m_dev, ctx->fh.m2m_ctx);
  1138. handled:
  1139. return IRQ_HANDLED;
  1140. }
  1141. /*
  1142. * video ioctls
  1143. */
  1144. static int vpe_querycap(struct file *file, void *priv,
  1145. struct v4l2_capability *cap)
  1146. {
  1147. strncpy(cap->driver, VPE_MODULE_NAME, sizeof(cap->driver) - 1);
  1148. strncpy(cap->card, VPE_MODULE_NAME, sizeof(cap->card) - 1);
  1149. snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
  1150. VPE_MODULE_NAME);
  1151. cap->device_caps = V4L2_CAP_VIDEO_M2M_MPLANE | V4L2_CAP_STREAMING;
  1152. cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
  1153. return 0;
  1154. }
  1155. static int __enum_fmt(struct v4l2_fmtdesc *f, u32 type)
  1156. {
  1157. int i, index;
  1158. struct vpe_fmt *fmt = NULL;
  1159. index = 0;
  1160. for (i = 0; i < ARRAY_SIZE(vpe_formats); ++i) {
  1161. if (vpe_formats[i].types & type) {
  1162. if (index == f->index) {
  1163. fmt = &vpe_formats[i];
  1164. break;
  1165. }
  1166. index++;
  1167. }
  1168. }
  1169. if (!fmt)
  1170. return -EINVAL;
  1171. strncpy(f->description, fmt->name, sizeof(f->description) - 1);
  1172. f->pixelformat = fmt->fourcc;
  1173. return 0;
  1174. }
  1175. static int vpe_enum_fmt(struct file *file, void *priv,
  1176. struct v4l2_fmtdesc *f)
  1177. {
  1178. if (V4L2_TYPE_IS_OUTPUT(f->type))
  1179. return __enum_fmt(f, VPE_FMT_TYPE_OUTPUT);
  1180. return __enum_fmt(f, VPE_FMT_TYPE_CAPTURE);
  1181. }
  1182. static int vpe_g_fmt(struct file *file, void *priv, struct v4l2_format *f)
  1183. {
  1184. struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp;
  1185. struct vpe_ctx *ctx = file2ctx(file);
  1186. struct vb2_queue *vq;
  1187. struct vpe_q_data *q_data;
  1188. int i;
  1189. vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
  1190. if (!vq)
  1191. return -EINVAL;
  1192. q_data = get_q_data(ctx, f->type);
  1193. pix->width = q_data->width;
  1194. pix->height = q_data->height;
  1195. pix->pixelformat = q_data->fmt->fourcc;
  1196. pix->field = q_data->field;
  1197. if (V4L2_TYPE_IS_OUTPUT(f->type)) {
  1198. pix->colorspace = q_data->colorspace;
  1199. } else {
  1200. struct vpe_q_data *s_q_data;
  1201. /* get colorspace from the source queue */
  1202. s_q_data = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
  1203. pix->colorspace = s_q_data->colorspace;
  1204. }
  1205. pix->num_planes = q_data->fmt->coplanar ? 2 : 1;
  1206. for (i = 0; i < pix->num_planes; i++) {
  1207. pix->plane_fmt[i].bytesperline = q_data->bytesperline[i];
  1208. pix->plane_fmt[i].sizeimage = q_data->sizeimage[i];
  1209. }
  1210. return 0;
  1211. }
  1212. static int __vpe_try_fmt(struct vpe_ctx *ctx, struct v4l2_format *f,
  1213. struct vpe_fmt *fmt, int type)
  1214. {
  1215. struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp;
  1216. struct v4l2_plane_pix_format *plane_fmt;
  1217. unsigned int w_align;
  1218. int i, depth, depth_bytes;
  1219. if (!fmt || !(fmt->types & type)) {
  1220. vpe_err(ctx->dev, "Fourcc format (0x%08x) invalid.\n",
  1221. pix->pixelformat);
  1222. return -EINVAL;
  1223. }
  1224. if (pix->field != V4L2_FIELD_NONE && pix->field != V4L2_FIELD_ALTERNATE)
  1225. pix->field = V4L2_FIELD_NONE;
  1226. depth = fmt->vpdma_fmt[VPE_LUMA]->depth;
  1227. /*
  1228. * the line stride should 16 byte aligned for VPDMA to work, based on
  1229. * the bytes per pixel, figure out how much the width should be aligned
  1230. * to make sure line stride is 16 byte aligned
  1231. */
  1232. depth_bytes = depth >> 3;
  1233. if (depth_bytes == 3)
  1234. /*
  1235. * if bpp is 3(as in some RGB formats), the pixel width doesn't
  1236. * really help in ensuring line stride is 16 byte aligned
  1237. */
  1238. w_align = 4;
  1239. else
  1240. /*
  1241. * for the remainder bpp(4, 2 and 1), the pixel width alignment
  1242. * can ensure a line stride alignment of 16 bytes. For example,
  1243. * if bpp is 2, then the line stride can be 16 byte aligned if
  1244. * the width is 8 byte aligned
  1245. */
  1246. w_align = order_base_2(VPDMA_DESC_ALIGN / depth_bytes);
  1247. v4l_bound_align_image(&pix->width, MIN_W, MAX_W, w_align,
  1248. &pix->height, MIN_H, MAX_H, H_ALIGN,
  1249. S_ALIGN);
  1250. pix->num_planes = fmt->coplanar ? 2 : 1;
  1251. pix->pixelformat = fmt->fourcc;
  1252. if (!pix->colorspace) {
  1253. if (fmt->fourcc == V4L2_PIX_FMT_RGB24 ||
  1254. fmt->fourcc == V4L2_PIX_FMT_BGR24 ||
  1255. fmt->fourcc == V4L2_PIX_FMT_RGB32 ||
  1256. fmt->fourcc == V4L2_PIX_FMT_BGR32) {
  1257. pix->colorspace = V4L2_COLORSPACE_SRGB;
  1258. } else {
  1259. if (pix->height > 1280) /* HD */
  1260. pix->colorspace = V4L2_COLORSPACE_REC709;
  1261. else /* SD */
  1262. pix->colorspace = V4L2_COLORSPACE_SMPTE170M;
  1263. }
  1264. }
  1265. memset(pix->reserved, 0, sizeof(pix->reserved));
  1266. for (i = 0; i < pix->num_planes; i++) {
  1267. plane_fmt = &pix->plane_fmt[i];
  1268. depth = fmt->vpdma_fmt[i]->depth;
  1269. if (i == VPE_LUMA)
  1270. plane_fmt->bytesperline = (pix->width * depth) >> 3;
  1271. else
  1272. plane_fmt->bytesperline = pix->width;
  1273. plane_fmt->sizeimage =
  1274. (pix->height * pix->width * depth) >> 3;
  1275. memset(plane_fmt->reserved, 0, sizeof(plane_fmt->reserved));
  1276. }
  1277. return 0;
  1278. }
  1279. static int vpe_try_fmt(struct file *file, void *priv, struct v4l2_format *f)
  1280. {
  1281. struct vpe_ctx *ctx = file2ctx(file);
  1282. struct vpe_fmt *fmt = find_format(f);
  1283. if (V4L2_TYPE_IS_OUTPUT(f->type))
  1284. return __vpe_try_fmt(ctx, f, fmt, VPE_FMT_TYPE_OUTPUT);
  1285. else
  1286. return __vpe_try_fmt(ctx, f, fmt, VPE_FMT_TYPE_CAPTURE);
  1287. }
  1288. static int __vpe_s_fmt(struct vpe_ctx *ctx, struct v4l2_format *f)
  1289. {
  1290. struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp;
  1291. struct v4l2_plane_pix_format *plane_fmt;
  1292. struct vpe_q_data *q_data;
  1293. struct vb2_queue *vq;
  1294. int i;
  1295. vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
  1296. if (!vq)
  1297. return -EINVAL;
  1298. if (vb2_is_busy(vq)) {
  1299. vpe_err(ctx->dev, "queue busy\n");
  1300. return -EBUSY;
  1301. }
  1302. q_data = get_q_data(ctx, f->type);
  1303. if (!q_data)
  1304. return -EINVAL;
  1305. q_data->fmt = find_format(f);
  1306. q_data->width = pix->width;
  1307. q_data->height = pix->height;
  1308. q_data->colorspace = pix->colorspace;
  1309. q_data->field = pix->field;
  1310. for (i = 0; i < pix->num_planes; i++) {
  1311. plane_fmt = &pix->plane_fmt[i];
  1312. q_data->bytesperline[i] = plane_fmt->bytesperline;
  1313. q_data->sizeimage[i] = plane_fmt->sizeimage;
  1314. }
  1315. q_data->c_rect.left = 0;
  1316. q_data->c_rect.top = 0;
  1317. q_data->c_rect.width = q_data->width;
  1318. q_data->c_rect.height = q_data->height;
  1319. if (q_data->field == V4L2_FIELD_ALTERNATE)
  1320. q_data->flags |= Q_DATA_INTERLACED;
  1321. else
  1322. q_data->flags &= ~Q_DATA_INTERLACED;
  1323. vpe_dbg(ctx->dev, "Setting format for type %d, wxh: %dx%d, fmt: %d bpl_y %d",
  1324. f->type, q_data->width, q_data->height, q_data->fmt->fourcc,
  1325. q_data->bytesperline[VPE_LUMA]);
  1326. if (q_data->fmt->coplanar)
  1327. vpe_dbg(ctx->dev, " bpl_uv %d\n",
  1328. q_data->bytesperline[VPE_CHROMA]);
  1329. return 0;
  1330. }
  1331. static int vpe_s_fmt(struct file *file, void *priv, struct v4l2_format *f)
  1332. {
  1333. int ret;
  1334. struct vpe_ctx *ctx = file2ctx(file);
  1335. ret = vpe_try_fmt(file, priv, f);
  1336. if (ret)
  1337. return ret;
  1338. ret = __vpe_s_fmt(ctx, f);
  1339. if (ret)
  1340. return ret;
  1341. if (V4L2_TYPE_IS_OUTPUT(f->type))
  1342. set_src_registers(ctx);
  1343. else
  1344. set_dst_registers(ctx);
  1345. return set_srcdst_params(ctx);
  1346. }
  1347. static int __vpe_try_selection(struct vpe_ctx *ctx, struct v4l2_selection *s)
  1348. {
  1349. struct vpe_q_data *q_data;
  1350. if ((s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
  1351. (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT))
  1352. return -EINVAL;
  1353. q_data = get_q_data(ctx, s->type);
  1354. if (!q_data)
  1355. return -EINVAL;
  1356. switch (s->target) {
  1357. case V4L2_SEL_TGT_COMPOSE:
  1358. /*
  1359. * COMPOSE target is only valid for capture buffer type, return
  1360. * error for output buffer type
  1361. */
  1362. if (s->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
  1363. return -EINVAL;
  1364. break;
  1365. case V4L2_SEL_TGT_CROP:
  1366. /*
  1367. * CROP target is only valid for output buffer type, return
  1368. * error for capture buffer type
  1369. */
  1370. if (s->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1371. return -EINVAL;
  1372. break;
  1373. /*
  1374. * bound and default crop/compose targets are invalid targets to
  1375. * try/set
  1376. */
  1377. default:
  1378. return -EINVAL;
  1379. }
  1380. if (s->r.top < 0 || s->r.left < 0) {
  1381. vpe_err(ctx->dev, "negative values for top and left\n");
  1382. s->r.top = s->r.left = 0;
  1383. }
  1384. v4l_bound_align_image(&s->r.width, MIN_W, q_data->width, 1,
  1385. &s->r.height, MIN_H, q_data->height, H_ALIGN, S_ALIGN);
  1386. /* adjust left/top if cropping rectangle is out of bounds */
  1387. if (s->r.left + s->r.width > q_data->width)
  1388. s->r.left = q_data->width - s->r.width;
  1389. if (s->r.top + s->r.height > q_data->height)
  1390. s->r.top = q_data->height - s->r.height;
  1391. return 0;
  1392. }
  1393. static int vpe_g_selection(struct file *file, void *fh,
  1394. struct v4l2_selection *s)
  1395. {
  1396. struct vpe_ctx *ctx = file2ctx(file);
  1397. struct vpe_q_data *q_data;
  1398. bool use_c_rect = false;
  1399. if ((s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
  1400. (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT))
  1401. return -EINVAL;
  1402. q_data = get_q_data(ctx, s->type);
  1403. if (!q_data)
  1404. return -EINVAL;
  1405. switch (s->target) {
  1406. case V4L2_SEL_TGT_COMPOSE_DEFAULT:
  1407. case V4L2_SEL_TGT_COMPOSE_BOUNDS:
  1408. if (s->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
  1409. return -EINVAL;
  1410. break;
  1411. case V4L2_SEL_TGT_CROP_BOUNDS:
  1412. case V4L2_SEL_TGT_CROP_DEFAULT:
  1413. if (s->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1414. return -EINVAL;
  1415. break;
  1416. case V4L2_SEL_TGT_COMPOSE:
  1417. if (s->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
  1418. return -EINVAL;
  1419. use_c_rect = true;
  1420. break;
  1421. case V4L2_SEL_TGT_CROP:
  1422. if (s->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1423. return -EINVAL;
  1424. use_c_rect = true;
  1425. break;
  1426. default:
  1427. return -EINVAL;
  1428. }
  1429. if (use_c_rect) {
  1430. /*
  1431. * for CROP/COMPOSE target type, return c_rect params from the
  1432. * respective buffer type
  1433. */
  1434. s->r = q_data->c_rect;
  1435. } else {
  1436. /*
  1437. * for DEFAULT/BOUNDS target type, return width and height from
  1438. * S_FMT of the respective buffer type
  1439. */
  1440. s->r.left = 0;
  1441. s->r.top = 0;
  1442. s->r.width = q_data->width;
  1443. s->r.height = q_data->height;
  1444. }
  1445. return 0;
  1446. }
  1447. static int vpe_s_selection(struct file *file, void *fh,
  1448. struct v4l2_selection *s)
  1449. {
  1450. struct vpe_ctx *ctx = file2ctx(file);
  1451. struct vpe_q_data *q_data;
  1452. struct v4l2_selection sel = *s;
  1453. int ret;
  1454. ret = __vpe_try_selection(ctx, &sel);
  1455. if (ret)
  1456. return ret;
  1457. q_data = get_q_data(ctx, sel.type);
  1458. if (!q_data)
  1459. return -EINVAL;
  1460. if ((q_data->c_rect.left == sel.r.left) &&
  1461. (q_data->c_rect.top == sel.r.top) &&
  1462. (q_data->c_rect.width == sel.r.width) &&
  1463. (q_data->c_rect.height == sel.r.height)) {
  1464. vpe_dbg(ctx->dev,
  1465. "requested crop/compose values are already set\n");
  1466. return 0;
  1467. }
  1468. q_data->c_rect = sel.r;
  1469. return set_srcdst_params(ctx);
  1470. }
  1471. /*
  1472. * defines number of buffers/frames a context can process with VPE before
  1473. * switching to a different context. default value is 1 buffer per context
  1474. */
  1475. #define V4L2_CID_VPE_BUFS_PER_JOB (V4L2_CID_USER_TI_VPE_BASE + 0)
  1476. static int vpe_s_ctrl(struct v4l2_ctrl *ctrl)
  1477. {
  1478. struct vpe_ctx *ctx =
  1479. container_of(ctrl->handler, struct vpe_ctx, hdl);
  1480. switch (ctrl->id) {
  1481. case V4L2_CID_VPE_BUFS_PER_JOB:
  1482. ctx->bufs_per_job = ctrl->val;
  1483. break;
  1484. default:
  1485. vpe_err(ctx->dev, "Invalid control\n");
  1486. return -EINVAL;
  1487. }
  1488. return 0;
  1489. }
  1490. static const struct v4l2_ctrl_ops vpe_ctrl_ops = {
  1491. .s_ctrl = vpe_s_ctrl,
  1492. };
  1493. static const struct v4l2_ioctl_ops vpe_ioctl_ops = {
  1494. .vidioc_querycap = vpe_querycap,
  1495. .vidioc_enum_fmt_vid_cap_mplane = vpe_enum_fmt,
  1496. .vidioc_g_fmt_vid_cap_mplane = vpe_g_fmt,
  1497. .vidioc_try_fmt_vid_cap_mplane = vpe_try_fmt,
  1498. .vidioc_s_fmt_vid_cap_mplane = vpe_s_fmt,
  1499. .vidioc_enum_fmt_vid_out_mplane = vpe_enum_fmt,
  1500. .vidioc_g_fmt_vid_out_mplane = vpe_g_fmt,
  1501. .vidioc_try_fmt_vid_out_mplane = vpe_try_fmt,
  1502. .vidioc_s_fmt_vid_out_mplane = vpe_s_fmt,
  1503. .vidioc_g_selection = vpe_g_selection,
  1504. .vidioc_s_selection = vpe_s_selection,
  1505. .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
  1506. .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
  1507. .vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
  1508. .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
  1509. .vidioc_streamon = v4l2_m2m_ioctl_streamon,
  1510. .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
  1511. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  1512. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  1513. };
  1514. /*
  1515. * Queue operations
  1516. */
  1517. static int vpe_queue_setup(struct vb2_queue *vq,
  1518. unsigned int *nbuffers, unsigned int *nplanes,
  1519. unsigned int sizes[], struct device *alloc_devs[])
  1520. {
  1521. int i;
  1522. struct vpe_ctx *ctx = vb2_get_drv_priv(vq);
  1523. struct vpe_q_data *q_data;
  1524. q_data = get_q_data(ctx, vq->type);
  1525. *nplanes = q_data->fmt->coplanar ? 2 : 1;
  1526. for (i = 0; i < *nplanes; i++)
  1527. sizes[i] = q_data->sizeimage[i];
  1528. vpe_dbg(ctx->dev, "get %d buffer(s) of size %d", *nbuffers,
  1529. sizes[VPE_LUMA]);
  1530. if (q_data->fmt->coplanar)
  1531. vpe_dbg(ctx->dev, " and %d\n", sizes[VPE_CHROMA]);
  1532. return 0;
  1533. }
  1534. static int vpe_buf_prepare(struct vb2_buffer *vb)
  1535. {
  1536. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  1537. struct vpe_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  1538. struct vpe_q_data *q_data;
  1539. int i, num_planes;
  1540. vpe_dbg(ctx->dev, "type: %d\n", vb->vb2_queue->type);
  1541. q_data = get_q_data(ctx, vb->vb2_queue->type);
  1542. num_planes = q_data->fmt->coplanar ? 2 : 1;
  1543. if (vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  1544. if (!(q_data->flags & Q_DATA_INTERLACED)) {
  1545. vbuf->field = V4L2_FIELD_NONE;
  1546. } else {
  1547. if (vbuf->field != V4L2_FIELD_TOP &&
  1548. vbuf->field != V4L2_FIELD_BOTTOM)
  1549. return -EINVAL;
  1550. }
  1551. }
  1552. for (i = 0; i < num_planes; i++) {
  1553. if (vb2_plane_size(vb, i) < q_data->sizeimage[i]) {
  1554. vpe_err(ctx->dev,
  1555. "data will not fit into plane (%lu < %lu)\n",
  1556. vb2_plane_size(vb, i),
  1557. (long) q_data->sizeimage[i]);
  1558. return -EINVAL;
  1559. }
  1560. }
  1561. for (i = 0; i < num_planes; i++)
  1562. vb2_set_plane_payload(vb, i, q_data->sizeimage[i]);
  1563. return 0;
  1564. }
  1565. static void vpe_buf_queue(struct vb2_buffer *vb)
  1566. {
  1567. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  1568. struct vpe_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  1569. v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
  1570. }
  1571. static int vpe_start_streaming(struct vb2_queue *q, unsigned int count)
  1572. {
  1573. /* currently we do nothing here */
  1574. return 0;
  1575. }
  1576. static void vpe_stop_streaming(struct vb2_queue *q)
  1577. {
  1578. struct vpe_ctx *ctx = vb2_get_drv_priv(q);
  1579. vpe_dump_regs(ctx->dev);
  1580. vpdma_dump_regs(ctx->dev->vpdma);
  1581. }
  1582. static const struct vb2_ops vpe_qops = {
  1583. .queue_setup = vpe_queue_setup,
  1584. .buf_prepare = vpe_buf_prepare,
  1585. .buf_queue = vpe_buf_queue,
  1586. .wait_prepare = vb2_ops_wait_prepare,
  1587. .wait_finish = vb2_ops_wait_finish,
  1588. .start_streaming = vpe_start_streaming,
  1589. .stop_streaming = vpe_stop_streaming,
  1590. };
  1591. static int queue_init(void *priv, struct vb2_queue *src_vq,
  1592. struct vb2_queue *dst_vq)
  1593. {
  1594. struct vpe_ctx *ctx = priv;
  1595. struct vpe_dev *dev = ctx->dev;
  1596. int ret;
  1597. memset(src_vq, 0, sizeof(*src_vq));
  1598. src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
  1599. src_vq->io_modes = VB2_MMAP | VB2_DMABUF;
  1600. src_vq->drv_priv = ctx;
  1601. src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
  1602. src_vq->ops = &vpe_qops;
  1603. src_vq->mem_ops = &vb2_dma_contig_memops;
  1604. src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
  1605. src_vq->lock = &dev->dev_mutex;
  1606. src_vq->dev = dev->v4l2_dev.dev;
  1607. ret = vb2_queue_init(src_vq);
  1608. if (ret)
  1609. return ret;
  1610. memset(dst_vq, 0, sizeof(*dst_vq));
  1611. dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
  1612. dst_vq->io_modes = VB2_MMAP | VB2_DMABUF;
  1613. dst_vq->drv_priv = ctx;
  1614. dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
  1615. dst_vq->ops = &vpe_qops;
  1616. dst_vq->mem_ops = &vb2_dma_contig_memops;
  1617. dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
  1618. dst_vq->lock = &dev->dev_mutex;
  1619. dst_vq->dev = dev->v4l2_dev.dev;
  1620. return vb2_queue_init(dst_vq);
  1621. }
  1622. static const struct v4l2_ctrl_config vpe_bufs_per_job = {
  1623. .ops = &vpe_ctrl_ops,
  1624. .id = V4L2_CID_VPE_BUFS_PER_JOB,
  1625. .name = "Buffers Per Transaction",
  1626. .type = V4L2_CTRL_TYPE_INTEGER,
  1627. .def = VPE_DEF_BUFS_PER_JOB,
  1628. .min = 1,
  1629. .max = VIDEO_MAX_FRAME,
  1630. .step = 1,
  1631. };
  1632. /*
  1633. * File operations
  1634. */
  1635. static int vpe_open(struct file *file)
  1636. {
  1637. struct vpe_dev *dev = video_drvdata(file);
  1638. struct vpe_q_data *s_q_data;
  1639. struct v4l2_ctrl_handler *hdl;
  1640. struct vpe_ctx *ctx;
  1641. int ret;
  1642. vpe_dbg(dev, "vpe_open\n");
  1643. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  1644. if (!ctx)
  1645. return -ENOMEM;
  1646. ctx->dev = dev;
  1647. if (mutex_lock_interruptible(&dev->dev_mutex)) {
  1648. ret = -ERESTARTSYS;
  1649. goto free_ctx;
  1650. }
  1651. ret = vpdma_create_desc_list(&ctx->desc_list, VPE_DESC_LIST_SIZE,
  1652. VPDMA_LIST_TYPE_NORMAL);
  1653. if (ret != 0)
  1654. goto unlock;
  1655. ret = vpdma_alloc_desc_buf(&ctx->mmr_adb, sizeof(struct vpe_mmr_adb));
  1656. if (ret != 0)
  1657. goto free_desc_list;
  1658. ret = vpdma_alloc_desc_buf(&ctx->sc_coeff_h, SC_COEF_SRAM_SIZE);
  1659. if (ret != 0)
  1660. goto free_mmr_adb;
  1661. ret = vpdma_alloc_desc_buf(&ctx->sc_coeff_v, SC_COEF_SRAM_SIZE);
  1662. if (ret != 0)
  1663. goto free_sc_h;
  1664. init_adb_hdrs(ctx);
  1665. v4l2_fh_init(&ctx->fh, video_devdata(file));
  1666. file->private_data = &ctx->fh;
  1667. hdl = &ctx->hdl;
  1668. v4l2_ctrl_handler_init(hdl, 1);
  1669. v4l2_ctrl_new_custom(hdl, &vpe_bufs_per_job, NULL);
  1670. if (hdl->error) {
  1671. ret = hdl->error;
  1672. goto exit_fh;
  1673. }
  1674. ctx->fh.ctrl_handler = hdl;
  1675. v4l2_ctrl_handler_setup(hdl);
  1676. s_q_data = &ctx->q_data[Q_DATA_SRC];
  1677. s_q_data->fmt = &vpe_formats[2];
  1678. s_q_data->width = 1920;
  1679. s_q_data->height = 1080;
  1680. s_q_data->bytesperline[VPE_LUMA] = (s_q_data->width *
  1681. s_q_data->fmt->vpdma_fmt[VPE_LUMA]->depth) >> 3;
  1682. s_q_data->sizeimage[VPE_LUMA] = (s_q_data->bytesperline[VPE_LUMA] *
  1683. s_q_data->height);
  1684. s_q_data->colorspace = V4L2_COLORSPACE_REC709;
  1685. s_q_data->field = V4L2_FIELD_NONE;
  1686. s_q_data->c_rect.left = 0;
  1687. s_q_data->c_rect.top = 0;
  1688. s_q_data->c_rect.width = s_q_data->width;
  1689. s_q_data->c_rect.height = s_q_data->height;
  1690. s_q_data->flags = 0;
  1691. ctx->q_data[Q_DATA_DST] = *s_q_data;
  1692. set_dei_shadow_registers(ctx);
  1693. set_src_registers(ctx);
  1694. set_dst_registers(ctx);
  1695. ret = set_srcdst_params(ctx);
  1696. if (ret)
  1697. goto exit_fh;
  1698. ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx, &queue_init);
  1699. if (IS_ERR(ctx->fh.m2m_ctx)) {
  1700. ret = PTR_ERR(ctx->fh.m2m_ctx);
  1701. goto exit_fh;
  1702. }
  1703. v4l2_fh_add(&ctx->fh);
  1704. /*
  1705. * for now, just report the creation of the first instance, we can later
  1706. * optimize the driver to enable or disable clocks when the first
  1707. * instance is created or the last instance released
  1708. */
  1709. if (atomic_inc_return(&dev->num_instances) == 1)
  1710. vpe_dbg(dev, "first instance created\n");
  1711. ctx->bufs_per_job = VPE_DEF_BUFS_PER_JOB;
  1712. ctx->load_mmrs = true;
  1713. vpe_dbg(dev, "created instance %p, m2m_ctx: %p\n",
  1714. ctx, ctx->fh.m2m_ctx);
  1715. mutex_unlock(&dev->dev_mutex);
  1716. return 0;
  1717. exit_fh:
  1718. v4l2_ctrl_handler_free(hdl);
  1719. v4l2_fh_exit(&ctx->fh);
  1720. vpdma_free_desc_buf(&ctx->sc_coeff_v);
  1721. free_sc_h:
  1722. vpdma_free_desc_buf(&ctx->sc_coeff_h);
  1723. free_mmr_adb:
  1724. vpdma_free_desc_buf(&ctx->mmr_adb);
  1725. free_desc_list:
  1726. vpdma_free_desc_list(&ctx->desc_list);
  1727. unlock:
  1728. mutex_unlock(&dev->dev_mutex);
  1729. free_ctx:
  1730. kfree(ctx);
  1731. return ret;
  1732. }
  1733. static int vpe_release(struct file *file)
  1734. {
  1735. struct vpe_dev *dev = video_drvdata(file);
  1736. struct vpe_ctx *ctx = file2ctx(file);
  1737. vpe_dbg(dev, "releasing instance %p\n", ctx);
  1738. mutex_lock(&dev->dev_mutex);
  1739. free_vbs(ctx);
  1740. free_mv_buffers(ctx);
  1741. vpdma_free_desc_list(&ctx->desc_list);
  1742. vpdma_free_desc_buf(&ctx->mmr_adb);
  1743. v4l2_fh_del(&ctx->fh);
  1744. v4l2_fh_exit(&ctx->fh);
  1745. v4l2_ctrl_handler_free(&ctx->hdl);
  1746. v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
  1747. kfree(ctx);
  1748. /*
  1749. * for now, just report the release of the last instance, we can later
  1750. * optimize the driver to enable or disable clocks when the first
  1751. * instance is created or the last instance released
  1752. */
  1753. if (atomic_dec_return(&dev->num_instances) == 0)
  1754. vpe_dbg(dev, "last instance released\n");
  1755. mutex_unlock(&dev->dev_mutex);
  1756. return 0;
  1757. }
  1758. static const struct v4l2_file_operations vpe_fops = {
  1759. .owner = THIS_MODULE,
  1760. .open = vpe_open,
  1761. .release = vpe_release,
  1762. .poll = v4l2_m2m_fop_poll,
  1763. .unlocked_ioctl = video_ioctl2,
  1764. .mmap = v4l2_m2m_fop_mmap,
  1765. };
  1766. static struct video_device vpe_videodev = {
  1767. .name = VPE_MODULE_NAME,
  1768. .fops = &vpe_fops,
  1769. .ioctl_ops = &vpe_ioctl_ops,
  1770. .minor = -1,
  1771. .release = video_device_release_empty,
  1772. .vfl_dir = VFL_DIR_M2M,
  1773. };
  1774. static struct v4l2_m2m_ops m2m_ops = {
  1775. .device_run = device_run,
  1776. .job_ready = job_ready,
  1777. .job_abort = job_abort,
  1778. .lock = vpe_lock,
  1779. .unlock = vpe_unlock,
  1780. };
  1781. static int vpe_runtime_get(struct platform_device *pdev)
  1782. {
  1783. int r;
  1784. dev_dbg(&pdev->dev, "vpe_runtime_get\n");
  1785. r = pm_runtime_get_sync(&pdev->dev);
  1786. WARN_ON(r < 0);
  1787. return r < 0 ? r : 0;
  1788. }
  1789. static void vpe_runtime_put(struct platform_device *pdev)
  1790. {
  1791. int r;
  1792. dev_dbg(&pdev->dev, "vpe_runtime_put\n");
  1793. r = pm_runtime_put_sync(&pdev->dev);
  1794. WARN_ON(r < 0 && r != -ENOSYS);
  1795. }
  1796. static void vpe_fw_cb(struct platform_device *pdev)
  1797. {
  1798. struct vpe_dev *dev = platform_get_drvdata(pdev);
  1799. struct video_device *vfd;
  1800. int ret;
  1801. vfd = &dev->vfd;
  1802. *vfd = vpe_videodev;
  1803. vfd->lock = &dev->dev_mutex;
  1804. vfd->v4l2_dev = &dev->v4l2_dev;
  1805. ret = video_register_device(vfd, VFL_TYPE_GRABBER, 0);
  1806. if (ret) {
  1807. vpe_err(dev, "Failed to register video device\n");
  1808. vpe_set_clock_enable(dev, 0);
  1809. vpe_runtime_put(pdev);
  1810. pm_runtime_disable(&pdev->dev);
  1811. v4l2_m2m_release(dev->m2m_dev);
  1812. v4l2_device_unregister(&dev->v4l2_dev);
  1813. return;
  1814. }
  1815. video_set_drvdata(vfd, dev);
  1816. snprintf(vfd->name, sizeof(vfd->name), "%s", vpe_videodev.name);
  1817. dev_info(dev->v4l2_dev.dev, "Device registered as /dev/video%d\n",
  1818. vfd->num);
  1819. }
  1820. static int vpe_probe(struct platform_device *pdev)
  1821. {
  1822. struct vpe_dev *dev;
  1823. int ret, irq, func;
  1824. dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
  1825. if (!dev)
  1826. return -ENOMEM;
  1827. spin_lock_init(&dev->lock);
  1828. ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
  1829. if (ret)
  1830. return ret;
  1831. atomic_set(&dev->num_instances, 0);
  1832. mutex_init(&dev->dev_mutex);
  1833. dev->res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  1834. "vpe_top");
  1835. /*
  1836. * HACK: we get resource info from device tree in the form of a list of
  1837. * VPE sub blocks, the driver currently uses only the base of vpe_top
  1838. * for register access, the driver should be changed later to access
  1839. * registers based on the sub block base addresses
  1840. */
  1841. dev->base = devm_ioremap(&pdev->dev, dev->res->start, SZ_32K);
  1842. if (!dev->base) {
  1843. ret = -ENOMEM;
  1844. goto v4l2_dev_unreg;
  1845. }
  1846. irq = platform_get_irq(pdev, 0);
  1847. ret = devm_request_irq(&pdev->dev, irq, vpe_irq, 0, VPE_MODULE_NAME,
  1848. dev);
  1849. if (ret)
  1850. goto v4l2_dev_unreg;
  1851. platform_set_drvdata(pdev, dev);
  1852. dev->m2m_dev = v4l2_m2m_init(&m2m_ops);
  1853. if (IS_ERR(dev->m2m_dev)) {
  1854. vpe_err(dev, "Failed to init mem2mem device\n");
  1855. ret = PTR_ERR(dev->m2m_dev);
  1856. goto v4l2_dev_unreg;
  1857. }
  1858. pm_runtime_enable(&pdev->dev);
  1859. ret = vpe_runtime_get(pdev);
  1860. if (ret)
  1861. goto rel_m2m;
  1862. /* Perform clk enable followed by reset */
  1863. vpe_set_clock_enable(dev, 1);
  1864. vpe_top_reset(dev);
  1865. func = read_field_reg(dev, VPE_PID, VPE_PID_FUNC_MASK,
  1866. VPE_PID_FUNC_SHIFT);
  1867. vpe_dbg(dev, "VPE PID function %x\n", func);
  1868. vpe_top_vpdma_reset(dev);
  1869. dev->sc = sc_create(pdev);
  1870. if (IS_ERR(dev->sc)) {
  1871. ret = PTR_ERR(dev->sc);
  1872. goto runtime_put;
  1873. }
  1874. dev->csc = csc_create(pdev);
  1875. if (IS_ERR(dev->csc)) {
  1876. ret = PTR_ERR(dev->csc);
  1877. goto runtime_put;
  1878. }
  1879. dev->vpdma = vpdma_create(pdev, vpe_fw_cb);
  1880. if (IS_ERR(dev->vpdma)) {
  1881. ret = PTR_ERR(dev->vpdma);
  1882. goto runtime_put;
  1883. }
  1884. return 0;
  1885. runtime_put:
  1886. vpe_runtime_put(pdev);
  1887. rel_m2m:
  1888. pm_runtime_disable(&pdev->dev);
  1889. v4l2_m2m_release(dev->m2m_dev);
  1890. v4l2_dev_unreg:
  1891. v4l2_device_unregister(&dev->v4l2_dev);
  1892. return ret;
  1893. }
  1894. static int vpe_remove(struct platform_device *pdev)
  1895. {
  1896. struct vpe_dev *dev = platform_get_drvdata(pdev);
  1897. v4l2_info(&dev->v4l2_dev, "Removing " VPE_MODULE_NAME);
  1898. v4l2_m2m_release(dev->m2m_dev);
  1899. video_unregister_device(&dev->vfd);
  1900. v4l2_device_unregister(&dev->v4l2_dev);
  1901. vpe_set_clock_enable(dev, 0);
  1902. vpe_runtime_put(pdev);
  1903. pm_runtime_disable(&pdev->dev);
  1904. return 0;
  1905. }
  1906. #if defined(CONFIG_OF)
  1907. static const struct of_device_id vpe_of_match[] = {
  1908. {
  1909. .compatible = "ti,vpe",
  1910. },
  1911. {},
  1912. };
  1913. #endif
  1914. static struct platform_driver vpe_pdrv = {
  1915. .probe = vpe_probe,
  1916. .remove = vpe_remove,
  1917. .driver = {
  1918. .name = VPE_MODULE_NAME,
  1919. .of_match_table = of_match_ptr(vpe_of_match),
  1920. },
  1921. };
  1922. module_platform_driver(vpe_pdrv);
  1923. MODULE_DESCRIPTION("TI VPE driver");
  1924. MODULE_AUTHOR("Dale Farnsworth, <dale@farnsworth.org>");
  1925. MODULE_LICENSE("GPL");