vpe.c 67 KB

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