vpe.c 67 KB

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