vpe.c 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099
  1. /*
  2. * TI VPE mem2mem driver, based on the virtual v4l2-mem2mem example driver
  3. *
  4. * Copyright (c) 2013 Texas Instruments Inc.
  5. * David Griego, <dagriego@biglakesoftware.com>
  6. * Dale Farnsworth, <dale@farnsworth.org>
  7. * Archit Taneja, <archit@ti.com>
  8. *
  9. * Copyright (c) 2009-2010 Samsung Electronics Co., Ltd.
  10. * Pawel Osciak, <pawel@osciak.com>
  11. * Marek Szyprowski, <m.szyprowski@samsung.com>
  12. *
  13. * Based on the virtual v4l2-mem2mem example device
  14. *
  15. * This program is free software; you can redistribute it and/or modify it
  16. * under the terms of the GNU General Public License version 2 as published by
  17. * the Free Software Foundation
  18. */
  19. #include <linux/delay.h>
  20. #include <linux/dma-mapping.h>
  21. #include <linux/err.h>
  22. #include <linux/fs.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/io.h>
  25. #include <linux/ioctl.h>
  26. #include <linux/module.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/pm_runtime.h>
  29. #include <linux/sched.h>
  30. #include <linux/slab.h>
  31. #include <linux/videodev2.h>
  32. #include <media/v4l2-common.h>
  33. #include <media/v4l2-ctrls.h>
  34. #include <media/v4l2-device.h>
  35. #include <media/v4l2-event.h>
  36. #include <media/v4l2-ioctl.h>
  37. #include <media/v4l2-mem2mem.h>
  38. #include <media/videobuf2-core.h>
  39. #include <media/videobuf2-dma-contig.h>
  40. #include "vpdma.h"
  41. #include "vpe_regs.h"
  42. #define VPE_MODULE_NAME "vpe"
  43. /* minimum and maximum frame sizes */
  44. #define MIN_W 128
  45. #define MIN_H 128
  46. #define MAX_W 1920
  47. #define MAX_H 1080
  48. /* required alignments */
  49. #define S_ALIGN 0 /* multiple of 1 */
  50. #define H_ALIGN 1 /* multiple of 2 */
  51. #define W_ALIGN 1 /* multiple of 2 */
  52. /* multiple of 128 bits, line stride, 16 bytes */
  53. #define L_ALIGN 4
  54. /* flags that indicate a format can be used for capture/output */
  55. #define VPE_FMT_TYPE_CAPTURE (1 << 0)
  56. #define VPE_FMT_TYPE_OUTPUT (1 << 1)
  57. /* used as plane indices */
  58. #define VPE_MAX_PLANES 2
  59. #define VPE_LUMA 0
  60. #define VPE_CHROMA 1
  61. /* per m2m context info */
  62. #define VPE_MAX_SRC_BUFS 3 /* need 3 src fields to de-interlace */
  63. #define VPE_DEF_BUFS_PER_JOB 1 /* default one buffer per batch job */
  64. /*
  65. * each VPE context can need up to 3 config desciptors, 7 input descriptors,
  66. * 3 output descriptors, and 10 control descriptors
  67. */
  68. #define VPE_DESC_LIST_SIZE (10 * VPDMA_DTD_DESC_SIZE + \
  69. 13 * VPDMA_CFD_CTD_DESC_SIZE)
  70. #define vpe_dbg(vpedev, fmt, arg...) \
  71. dev_dbg((vpedev)->v4l2_dev.dev, fmt, ##arg)
  72. #define vpe_err(vpedev, fmt, arg...) \
  73. dev_err((vpedev)->v4l2_dev.dev, fmt, ##arg)
  74. struct vpe_us_coeffs {
  75. unsigned short anchor_fid0_c0;
  76. unsigned short anchor_fid0_c1;
  77. unsigned short anchor_fid0_c2;
  78. unsigned short anchor_fid0_c3;
  79. unsigned short interp_fid0_c0;
  80. unsigned short interp_fid0_c1;
  81. unsigned short interp_fid0_c2;
  82. unsigned short interp_fid0_c3;
  83. unsigned short anchor_fid1_c0;
  84. unsigned short anchor_fid1_c1;
  85. unsigned short anchor_fid1_c2;
  86. unsigned short anchor_fid1_c3;
  87. unsigned short interp_fid1_c0;
  88. unsigned short interp_fid1_c1;
  89. unsigned short interp_fid1_c2;
  90. unsigned short interp_fid1_c3;
  91. };
  92. /*
  93. * Default upsampler coefficients
  94. */
  95. static const struct vpe_us_coeffs us_coeffs[] = {
  96. {
  97. /* Coefficients for progressive input */
  98. 0x00C8, 0x0348, 0x0018, 0x3FD8, 0x3FB8, 0x0378, 0x00E8, 0x3FE8,
  99. 0x00C8, 0x0348, 0x0018, 0x3FD8, 0x3FB8, 0x0378, 0x00E8, 0x3FE8,
  100. },
  101. {
  102. /* Coefficients for Top Field Interlaced input */
  103. 0x0051, 0x03D5, 0x3FE3, 0x3FF7, 0x3FB5, 0x02E9, 0x018F, 0x3FD3,
  104. /* Coefficients for Bottom Field Interlaced input */
  105. 0x016B, 0x0247, 0x00B1, 0x3F9D, 0x3FCF, 0x03DB, 0x005D, 0x3FF9,
  106. },
  107. };
  108. /*
  109. * the following registers are for configuring some of the parameters of the
  110. * motion and edge detection blocks inside DEI, these generally remain the same,
  111. * these could be passed later via userspace if some one needs to tweak these.
  112. */
  113. struct vpe_dei_regs {
  114. unsigned long mdt_spacial_freq_thr_reg; /* VPE_DEI_REG2 */
  115. unsigned long edi_config_reg; /* VPE_DEI_REG3 */
  116. unsigned long edi_lut_reg0; /* VPE_DEI_REG4 */
  117. unsigned long edi_lut_reg1; /* VPE_DEI_REG5 */
  118. unsigned long edi_lut_reg2; /* VPE_DEI_REG6 */
  119. unsigned long edi_lut_reg3; /* VPE_DEI_REG7 */
  120. };
  121. /*
  122. * default expert DEI register values, unlikely to be modified.
  123. */
  124. static const struct vpe_dei_regs dei_regs = {
  125. 0x020C0804u,
  126. 0x0118100Fu,
  127. 0x08040200u,
  128. 0x1010100Cu,
  129. 0x10101010u,
  130. 0x10101010u,
  131. };
  132. /*
  133. * The port_data structure contains per-port data.
  134. */
  135. struct vpe_port_data {
  136. enum vpdma_channel channel; /* VPDMA channel */
  137. u8 vb_index; /* input frame f, f-1, f-2 index */
  138. u8 vb_part; /* plane index for co-panar formats */
  139. };
  140. /*
  141. * Define indices into the port_data tables
  142. */
  143. #define VPE_PORT_LUMA1_IN 0
  144. #define VPE_PORT_CHROMA1_IN 1
  145. #define VPE_PORT_LUMA2_IN 2
  146. #define VPE_PORT_CHROMA2_IN 3
  147. #define VPE_PORT_LUMA3_IN 4
  148. #define VPE_PORT_CHROMA3_IN 5
  149. #define VPE_PORT_MV_IN 6
  150. #define VPE_PORT_MV_OUT 7
  151. #define VPE_PORT_LUMA_OUT 8
  152. #define VPE_PORT_CHROMA_OUT 9
  153. #define VPE_PORT_RGB_OUT 10
  154. static const struct vpe_port_data port_data[11] = {
  155. [VPE_PORT_LUMA1_IN] = {
  156. .channel = VPE_CHAN_LUMA1_IN,
  157. .vb_index = 0,
  158. .vb_part = VPE_LUMA,
  159. },
  160. [VPE_PORT_CHROMA1_IN] = {
  161. .channel = VPE_CHAN_CHROMA1_IN,
  162. .vb_index = 0,
  163. .vb_part = VPE_CHROMA,
  164. },
  165. [VPE_PORT_LUMA2_IN] = {
  166. .channel = VPE_CHAN_LUMA2_IN,
  167. .vb_index = 1,
  168. .vb_part = VPE_LUMA,
  169. },
  170. [VPE_PORT_CHROMA2_IN] = {
  171. .channel = VPE_CHAN_CHROMA2_IN,
  172. .vb_index = 1,
  173. .vb_part = VPE_CHROMA,
  174. },
  175. [VPE_PORT_LUMA3_IN] = {
  176. .channel = VPE_CHAN_LUMA3_IN,
  177. .vb_index = 2,
  178. .vb_part = VPE_LUMA,
  179. },
  180. [VPE_PORT_CHROMA3_IN] = {
  181. .channel = VPE_CHAN_CHROMA3_IN,
  182. .vb_index = 2,
  183. .vb_part = VPE_CHROMA,
  184. },
  185. [VPE_PORT_MV_IN] = {
  186. .channel = VPE_CHAN_MV_IN,
  187. },
  188. [VPE_PORT_MV_OUT] = {
  189. .channel = VPE_CHAN_MV_OUT,
  190. },
  191. [VPE_PORT_LUMA_OUT] = {
  192. .channel = VPE_CHAN_LUMA_OUT,
  193. .vb_part = VPE_LUMA,
  194. },
  195. [VPE_PORT_CHROMA_OUT] = {
  196. .channel = VPE_CHAN_CHROMA_OUT,
  197. .vb_part = VPE_CHROMA,
  198. },
  199. [VPE_PORT_RGB_OUT] = {
  200. .channel = VPE_CHAN_RGB_OUT,
  201. .vb_part = VPE_LUMA,
  202. },
  203. };
  204. /* driver info for each of the supported video formats */
  205. struct vpe_fmt {
  206. char *name; /* human-readable name */
  207. u32 fourcc; /* standard format identifier */
  208. u8 types; /* CAPTURE and/or OUTPUT */
  209. u8 coplanar; /* set for unpacked Luma and Chroma */
  210. /* vpdma format info for each plane */
  211. struct vpdma_data_format const *vpdma_fmt[VPE_MAX_PLANES];
  212. };
  213. static struct vpe_fmt vpe_formats[] = {
  214. {
  215. .name = "YUV 422 co-planar",
  216. .fourcc = V4L2_PIX_FMT_NV16,
  217. .types = VPE_FMT_TYPE_CAPTURE | VPE_FMT_TYPE_OUTPUT,
  218. .coplanar = 1,
  219. .vpdma_fmt = { &vpdma_yuv_fmts[VPDMA_DATA_FMT_Y444],
  220. &vpdma_yuv_fmts[VPDMA_DATA_FMT_C444],
  221. },
  222. },
  223. {
  224. .name = "YUV 420 co-planar",
  225. .fourcc = V4L2_PIX_FMT_NV12,
  226. .types = VPE_FMT_TYPE_CAPTURE | VPE_FMT_TYPE_OUTPUT,
  227. .coplanar = 1,
  228. .vpdma_fmt = { &vpdma_yuv_fmts[VPDMA_DATA_FMT_Y420],
  229. &vpdma_yuv_fmts[VPDMA_DATA_FMT_C420],
  230. },
  231. },
  232. {
  233. .name = "YUYV 422 packed",
  234. .fourcc = V4L2_PIX_FMT_YUYV,
  235. .types = VPE_FMT_TYPE_CAPTURE | VPE_FMT_TYPE_OUTPUT,
  236. .coplanar = 0,
  237. .vpdma_fmt = { &vpdma_yuv_fmts[VPDMA_DATA_FMT_YC422],
  238. },
  239. },
  240. {
  241. .name = "UYVY 422 packed",
  242. .fourcc = V4L2_PIX_FMT_UYVY,
  243. .types = VPE_FMT_TYPE_CAPTURE | VPE_FMT_TYPE_OUTPUT,
  244. .coplanar = 0,
  245. .vpdma_fmt = { &vpdma_yuv_fmts[VPDMA_DATA_FMT_CY422],
  246. },
  247. },
  248. };
  249. /*
  250. * per-queue, driver-specific private data.
  251. * there is one source queue and one destination queue for each m2m context.
  252. */
  253. struct vpe_q_data {
  254. unsigned int width; /* frame width */
  255. unsigned int height; /* frame height */
  256. unsigned int bytesperline[VPE_MAX_PLANES]; /* bytes per line in memory */
  257. enum v4l2_colorspace colorspace;
  258. enum v4l2_field field; /* supported field value */
  259. unsigned int flags;
  260. unsigned int sizeimage[VPE_MAX_PLANES]; /* image size in memory */
  261. struct v4l2_rect c_rect; /* crop/compose rectangle */
  262. struct vpe_fmt *fmt; /* format info */
  263. };
  264. /* vpe_q_data flag bits */
  265. #define Q_DATA_FRAME_1D (1 << 0)
  266. #define Q_DATA_MODE_TILED (1 << 1)
  267. #define Q_DATA_INTERLACED (1 << 2)
  268. enum {
  269. Q_DATA_SRC = 0,
  270. Q_DATA_DST = 1,
  271. };
  272. /* find our format description corresponding to the passed v4l2_format */
  273. static struct vpe_fmt *find_format(struct v4l2_format *f)
  274. {
  275. struct vpe_fmt *fmt;
  276. unsigned int k;
  277. for (k = 0; k < ARRAY_SIZE(vpe_formats); k++) {
  278. fmt = &vpe_formats[k];
  279. if (fmt->fourcc == f->fmt.pix.pixelformat)
  280. return fmt;
  281. }
  282. return NULL;
  283. }
  284. /*
  285. * there is one vpe_dev structure in the driver, it is shared by
  286. * all instances.
  287. */
  288. struct vpe_dev {
  289. struct v4l2_device v4l2_dev;
  290. struct video_device vfd;
  291. struct v4l2_m2m_dev *m2m_dev;
  292. atomic_t num_instances; /* count of driver instances */
  293. dma_addr_t loaded_mmrs; /* shadow mmrs in device */
  294. struct mutex dev_mutex;
  295. spinlock_t lock;
  296. int irq;
  297. void __iomem *base;
  298. struct vb2_alloc_ctx *alloc_ctx;
  299. struct vpdma_data *vpdma; /* vpdma data handle */
  300. };
  301. /*
  302. * There is one vpe_ctx structure for each m2m context.
  303. */
  304. struct vpe_ctx {
  305. struct v4l2_fh fh;
  306. struct vpe_dev *dev;
  307. struct v4l2_m2m_ctx *m2m_ctx;
  308. struct v4l2_ctrl_handler hdl;
  309. unsigned int field; /* current field */
  310. unsigned int sequence; /* current frame/field seq */
  311. unsigned int aborting; /* abort after next irq */
  312. unsigned int bufs_per_job; /* input buffers per batch */
  313. unsigned int bufs_completed; /* bufs done in this batch */
  314. struct vpe_q_data q_data[2]; /* src & dst queue data */
  315. struct vb2_buffer *src_vbs[VPE_MAX_SRC_BUFS];
  316. struct vb2_buffer *dst_vb;
  317. dma_addr_t mv_buf_dma[2]; /* dma addrs of motion vector in/out bufs */
  318. void *mv_buf[2]; /* virtual addrs of motion vector bufs */
  319. size_t mv_buf_size; /* current motion vector buffer size */
  320. struct vpdma_buf mmr_adb; /* shadow reg addr/data block */
  321. struct vpdma_desc_list desc_list; /* DMA descriptor list */
  322. bool deinterlacing; /* using de-interlacer */
  323. bool load_mmrs; /* have new shadow reg values */
  324. unsigned int src_mv_buf_selector;
  325. };
  326. /*
  327. * M2M devices get 2 queues.
  328. * Return the queue given the type.
  329. */
  330. static struct vpe_q_data *get_q_data(struct vpe_ctx *ctx,
  331. enum v4l2_buf_type type)
  332. {
  333. switch (type) {
  334. case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
  335. return &ctx->q_data[Q_DATA_SRC];
  336. case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
  337. return &ctx->q_data[Q_DATA_DST];
  338. default:
  339. BUG();
  340. }
  341. return NULL;
  342. }
  343. static u32 read_reg(struct vpe_dev *dev, int offset)
  344. {
  345. return ioread32(dev->base + offset);
  346. }
  347. static void write_reg(struct vpe_dev *dev, int offset, u32 value)
  348. {
  349. iowrite32(value, dev->base + offset);
  350. }
  351. /* register field read/write helpers */
  352. static int get_field(u32 value, u32 mask, int shift)
  353. {
  354. return (value & (mask << shift)) >> shift;
  355. }
  356. static int read_field_reg(struct vpe_dev *dev, int offset, u32 mask, int shift)
  357. {
  358. return get_field(read_reg(dev, offset), mask, shift);
  359. }
  360. static void write_field(u32 *valp, u32 field, u32 mask, int shift)
  361. {
  362. u32 val = *valp;
  363. val &= ~(mask << shift);
  364. val |= (field & mask) << shift;
  365. *valp = val;
  366. }
  367. static void write_field_reg(struct vpe_dev *dev, int offset, u32 field,
  368. u32 mask, int shift)
  369. {
  370. u32 val = read_reg(dev, offset);
  371. write_field(&val, field, mask, shift);
  372. write_reg(dev, offset, val);
  373. }
  374. /*
  375. * DMA address/data block for the shadow registers
  376. */
  377. struct vpe_mmr_adb {
  378. struct vpdma_adb_hdr out_fmt_hdr;
  379. u32 out_fmt_reg[1];
  380. u32 out_fmt_pad[3];
  381. struct vpdma_adb_hdr us1_hdr;
  382. u32 us1_regs[8];
  383. struct vpdma_adb_hdr us2_hdr;
  384. u32 us2_regs[8];
  385. struct vpdma_adb_hdr us3_hdr;
  386. u32 us3_regs[8];
  387. struct vpdma_adb_hdr dei_hdr;
  388. u32 dei_regs[8];
  389. struct vpdma_adb_hdr sc_hdr;
  390. u32 sc_regs[1];
  391. u32 sc_pad[3];
  392. struct vpdma_adb_hdr csc_hdr;
  393. u32 csc_regs[6];
  394. u32 csc_pad[2];
  395. };
  396. #define VPE_SET_MMR_ADB_HDR(ctx, hdr, regs, offset_a) \
  397. VPDMA_SET_MMR_ADB_HDR(ctx->mmr_adb, vpe_mmr_adb, hdr, regs, offset_a)
  398. /*
  399. * Set the headers for all of the address/data block structures.
  400. */
  401. static void init_adb_hdrs(struct vpe_ctx *ctx)
  402. {
  403. VPE_SET_MMR_ADB_HDR(ctx, out_fmt_hdr, out_fmt_reg, VPE_CLK_FORMAT_SELECT);
  404. VPE_SET_MMR_ADB_HDR(ctx, us1_hdr, us1_regs, VPE_US1_R0);
  405. VPE_SET_MMR_ADB_HDR(ctx, us2_hdr, us2_regs, VPE_US2_R0);
  406. VPE_SET_MMR_ADB_HDR(ctx, us3_hdr, us3_regs, VPE_US3_R0);
  407. VPE_SET_MMR_ADB_HDR(ctx, dei_hdr, dei_regs, VPE_DEI_FRAME_SIZE);
  408. VPE_SET_MMR_ADB_HDR(ctx, sc_hdr, sc_regs, VPE_SC_MP_SC0);
  409. VPE_SET_MMR_ADB_HDR(ctx, csc_hdr, csc_regs, VPE_CSC_CSC00);
  410. };
  411. /*
  412. * Allocate or re-allocate the motion vector DMA buffers
  413. * There are two buffers, one for input and one for output.
  414. * However, the roles are reversed after each field is processed.
  415. * In other words, after each field is processed, the previous
  416. * output (dst) MV buffer becomes the new input (src) MV buffer.
  417. */
  418. static int realloc_mv_buffers(struct vpe_ctx *ctx, size_t size)
  419. {
  420. struct device *dev = ctx->dev->v4l2_dev.dev;
  421. if (ctx->mv_buf_size == size)
  422. return 0;
  423. if (ctx->mv_buf[0])
  424. dma_free_coherent(dev, ctx->mv_buf_size, ctx->mv_buf[0],
  425. ctx->mv_buf_dma[0]);
  426. if (ctx->mv_buf[1])
  427. dma_free_coherent(dev, ctx->mv_buf_size, ctx->mv_buf[1],
  428. ctx->mv_buf_dma[1]);
  429. if (size == 0)
  430. return 0;
  431. ctx->mv_buf[0] = dma_alloc_coherent(dev, size, &ctx->mv_buf_dma[0],
  432. GFP_KERNEL);
  433. if (!ctx->mv_buf[0]) {
  434. vpe_err(ctx->dev, "failed to allocate motion vector buffer\n");
  435. return -ENOMEM;
  436. }
  437. ctx->mv_buf[1] = dma_alloc_coherent(dev, size, &ctx->mv_buf_dma[1],
  438. GFP_KERNEL);
  439. if (!ctx->mv_buf[1]) {
  440. vpe_err(ctx->dev, "failed to allocate motion vector buffer\n");
  441. dma_free_coherent(dev, size, ctx->mv_buf[0],
  442. ctx->mv_buf_dma[0]);
  443. return -ENOMEM;
  444. }
  445. ctx->mv_buf_size = size;
  446. ctx->src_mv_buf_selector = 0;
  447. return 0;
  448. }
  449. static void free_mv_buffers(struct vpe_ctx *ctx)
  450. {
  451. realloc_mv_buffers(ctx, 0);
  452. }
  453. /*
  454. * While de-interlacing, we keep the two most recent input buffers
  455. * around. This function frees those two buffers when we have
  456. * finished processing the current stream.
  457. */
  458. static void free_vbs(struct vpe_ctx *ctx)
  459. {
  460. struct vpe_dev *dev = ctx->dev;
  461. unsigned long flags;
  462. if (ctx->src_vbs[2] == NULL)
  463. return;
  464. spin_lock_irqsave(&dev->lock, flags);
  465. if (ctx->src_vbs[2]) {
  466. v4l2_m2m_buf_done(ctx->src_vbs[2], VB2_BUF_STATE_DONE);
  467. v4l2_m2m_buf_done(ctx->src_vbs[1], VB2_BUF_STATE_DONE);
  468. }
  469. spin_unlock_irqrestore(&dev->lock, flags);
  470. }
  471. /*
  472. * Enable or disable the VPE clocks
  473. */
  474. static void vpe_set_clock_enable(struct vpe_dev *dev, bool on)
  475. {
  476. u32 val = 0;
  477. if (on)
  478. val = VPE_DATA_PATH_CLK_ENABLE | VPE_VPEDMA_CLK_ENABLE;
  479. write_reg(dev, VPE_CLK_ENABLE, val);
  480. }
  481. static void vpe_top_reset(struct vpe_dev *dev)
  482. {
  483. write_field_reg(dev, VPE_CLK_RESET, 1, VPE_DATA_PATH_CLK_RESET_MASK,
  484. VPE_DATA_PATH_CLK_RESET_SHIFT);
  485. usleep_range(100, 150);
  486. write_field_reg(dev, VPE_CLK_RESET, 0, VPE_DATA_PATH_CLK_RESET_MASK,
  487. VPE_DATA_PATH_CLK_RESET_SHIFT);
  488. }
  489. static void vpe_top_vpdma_reset(struct vpe_dev *dev)
  490. {
  491. write_field_reg(dev, VPE_CLK_RESET, 1, VPE_VPDMA_CLK_RESET_MASK,
  492. VPE_VPDMA_CLK_RESET_SHIFT);
  493. usleep_range(100, 150);
  494. write_field_reg(dev, VPE_CLK_RESET, 0, VPE_VPDMA_CLK_RESET_MASK,
  495. VPE_VPDMA_CLK_RESET_SHIFT);
  496. }
  497. /*
  498. * Load the correct of upsampler coefficients into the shadow MMRs
  499. */
  500. static void set_us_coefficients(struct vpe_ctx *ctx)
  501. {
  502. struct vpe_mmr_adb *mmr_adb = ctx->mmr_adb.addr;
  503. struct vpe_q_data *s_q_data = &ctx->q_data[Q_DATA_SRC];
  504. u32 *us1_reg = &mmr_adb->us1_regs[0];
  505. u32 *us2_reg = &mmr_adb->us2_regs[0];
  506. u32 *us3_reg = &mmr_adb->us3_regs[0];
  507. const unsigned short *cp, *end_cp;
  508. cp = &us_coeffs[0].anchor_fid0_c0;
  509. if (s_q_data->flags & Q_DATA_INTERLACED) /* interlaced */
  510. cp += sizeof(us_coeffs[0]) / sizeof(*cp);
  511. end_cp = cp + sizeof(us_coeffs[0]) / sizeof(*cp);
  512. while (cp < end_cp) {
  513. write_field(us1_reg, *cp++, VPE_US_C0_MASK, VPE_US_C0_SHIFT);
  514. write_field(us1_reg, *cp++, VPE_US_C1_MASK, VPE_US_C1_SHIFT);
  515. *us2_reg++ = *us1_reg;
  516. *us3_reg++ = *us1_reg++;
  517. }
  518. ctx->load_mmrs = true;
  519. }
  520. /*
  521. * Set the upsampler config mode and the VPDMA line mode in the shadow MMRs.
  522. */
  523. static void set_cfg_and_line_modes(struct vpe_ctx *ctx)
  524. {
  525. struct vpe_fmt *fmt = ctx->q_data[Q_DATA_SRC].fmt;
  526. struct vpe_mmr_adb *mmr_adb = ctx->mmr_adb.addr;
  527. u32 *us1_reg0 = &mmr_adb->us1_regs[0];
  528. u32 *us2_reg0 = &mmr_adb->us2_regs[0];
  529. u32 *us3_reg0 = &mmr_adb->us3_regs[0];
  530. int line_mode = 1;
  531. int cfg_mode = 1;
  532. /*
  533. * Cfg Mode 0: YUV420 source, enable upsampler, DEI is de-interlacing.
  534. * Cfg Mode 1: YUV422 source, disable upsampler, DEI is de-interlacing.
  535. */
  536. if (fmt->fourcc == V4L2_PIX_FMT_NV12) {
  537. cfg_mode = 0;
  538. line_mode = 0; /* double lines to line buffer */
  539. }
  540. write_field(us1_reg0, cfg_mode, VPE_US_MODE_MASK, VPE_US_MODE_SHIFT);
  541. write_field(us2_reg0, cfg_mode, VPE_US_MODE_MASK, VPE_US_MODE_SHIFT);
  542. write_field(us3_reg0, cfg_mode, VPE_US_MODE_MASK, VPE_US_MODE_SHIFT);
  543. /* regs for now */
  544. vpdma_set_line_mode(ctx->dev->vpdma, line_mode, VPE_CHAN_CHROMA1_IN);
  545. vpdma_set_line_mode(ctx->dev->vpdma, line_mode, VPE_CHAN_CHROMA2_IN);
  546. vpdma_set_line_mode(ctx->dev->vpdma, line_mode, VPE_CHAN_CHROMA3_IN);
  547. /* frame start for input luma */
  548. vpdma_set_frame_start_event(ctx->dev->vpdma, VPDMA_FSEVENT_CHANNEL_ACTIVE,
  549. VPE_CHAN_LUMA1_IN);
  550. vpdma_set_frame_start_event(ctx->dev->vpdma, VPDMA_FSEVENT_CHANNEL_ACTIVE,
  551. VPE_CHAN_LUMA2_IN);
  552. vpdma_set_frame_start_event(ctx->dev->vpdma, VPDMA_FSEVENT_CHANNEL_ACTIVE,
  553. VPE_CHAN_LUMA3_IN);
  554. /* frame start for input chroma */
  555. vpdma_set_frame_start_event(ctx->dev->vpdma, VPDMA_FSEVENT_CHANNEL_ACTIVE,
  556. VPE_CHAN_CHROMA1_IN);
  557. vpdma_set_frame_start_event(ctx->dev->vpdma, VPDMA_FSEVENT_CHANNEL_ACTIVE,
  558. VPE_CHAN_CHROMA2_IN);
  559. vpdma_set_frame_start_event(ctx->dev->vpdma, VPDMA_FSEVENT_CHANNEL_ACTIVE,
  560. VPE_CHAN_CHROMA3_IN);
  561. /* frame start for MV in client */
  562. vpdma_set_frame_start_event(ctx->dev->vpdma, VPDMA_FSEVENT_CHANNEL_ACTIVE,
  563. VPE_CHAN_MV_IN);
  564. ctx->load_mmrs = true;
  565. }
  566. /*
  567. * Set the shadow registers that are modified when the source
  568. * format changes.
  569. */
  570. static void set_src_registers(struct vpe_ctx *ctx)
  571. {
  572. set_us_coefficients(ctx);
  573. }
  574. /*
  575. * Set the shadow registers that are modified when the destination
  576. * format changes.
  577. */
  578. static void set_dst_registers(struct vpe_ctx *ctx)
  579. {
  580. struct vpe_mmr_adb *mmr_adb = ctx->mmr_adb.addr;
  581. struct vpe_fmt *fmt = ctx->q_data[Q_DATA_DST].fmt;
  582. u32 val = 0;
  583. /* select RGB path when color space conversion is supported in future */
  584. if (fmt->fourcc == V4L2_PIX_FMT_RGB24)
  585. val |= VPE_RGB_OUT_SELECT | VPE_CSC_SRC_DEI_SCALER;
  586. else if (fmt->fourcc == V4L2_PIX_FMT_NV16)
  587. val |= VPE_COLOR_SEPARATE_422;
  588. /* The source of CHR_DS is always the scaler, whether it's used or not */
  589. val |= VPE_DS_SRC_DEI_SCALER;
  590. if (fmt->fourcc != V4L2_PIX_FMT_NV12)
  591. val |= VPE_DS_BYPASS;
  592. mmr_adb->out_fmt_reg[0] = val;
  593. ctx->load_mmrs = true;
  594. }
  595. /*
  596. * Set the de-interlacer shadow register values
  597. */
  598. static void set_dei_regs(struct vpe_ctx *ctx)
  599. {
  600. struct vpe_mmr_adb *mmr_adb = ctx->mmr_adb.addr;
  601. struct vpe_q_data *s_q_data = &ctx->q_data[Q_DATA_SRC];
  602. unsigned int src_h = s_q_data->c_rect.height;
  603. unsigned int src_w = s_q_data->c_rect.width;
  604. u32 *dei_mmr0 = &mmr_adb->dei_regs[0];
  605. bool deinterlace = true;
  606. u32 val = 0;
  607. /*
  608. * according to TRM, we should set DEI in progressive bypass mode when
  609. * the input content is progressive, however, DEI is bypassed correctly
  610. * for both progressive and interlace content in interlace bypass mode.
  611. * It has been recommended not to use progressive bypass mode.
  612. */
  613. if ((!ctx->deinterlacing && (s_q_data->flags & Q_DATA_INTERLACED)) ||
  614. !(s_q_data->flags & Q_DATA_INTERLACED)) {
  615. deinterlace = false;
  616. val = VPE_DEI_INTERLACE_BYPASS;
  617. }
  618. src_h = deinterlace ? src_h * 2 : src_h;
  619. val |= (src_h << VPE_DEI_HEIGHT_SHIFT) |
  620. (src_w << VPE_DEI_WIDTH_SHIFT) |
  621. VPE_DEI_FIELD_FLUSH;
  622. *dei_mmr0 = val;
  623. ctx->load_mmrs = true;
  624. }
  625. static void set_dei_shadow_registers(struct vpe_ctx *ctx)
  626. {
  627. struct vpe_mmr_adb *mmr_adb = ctx->mmr_adb.addr;
  628. u32 *dei_mmr = &mmr_adb->dei_regs[0];
  629. const struct vpe_dei_regs *cur = &dei_regs;
  630. dei_mmr[2] = cur->mdt_spacial_freq_thr_reg;
  631. dei_mmr[3] = cur->edi_config_reg;
  632. dei_mmr[4] = cur->edi_lut_reg0;
  633. dei_mmr[5] = cur->edi_lut_reg1;
  634. dei_mmr[6] = cur->edi_lut_reg2;
  635. dei_mmr[7] = cur->edi_lut_reg3;
  636. ctx->load_mmrs = true;
  637. }
  638. static void set_csc_coeff_bypass(struct vpe_ctx *ctx)
  639. {
  640. struct vpe_mmr_adb *mmr_adb = ctx->mmr_adb.addr;
  641. u32 *shadow_csc_reg5 = &mmr_adb->csc_regs[5];
  642. *shadow_csc_reg5 |= VPE_CSC_BYPASS;
  643. ctx->load_mmrs = true;
  644. }
  645. static void set_sc_regs_bypass(struct vpe_ctx *ctx)
  646. {
  647. struct vpe_mmr_adb *mmr_adb = ctx->mmr_adb.addr;
  648. u32 *sc_reg0 = &mmr_adb->sc_regs[0];
  649. u32 val = 0;
  650. val |= VPE_SC_BYPASS;
  651. *sc_reg0 = val;
  652. ctx->load_mmrs = true;
  653. }
  654. /*
  655. * Set the shadow registers whose values are modified when either the
  656. * source or destination format is changed.
  657. */
  658. static int set_srcdst_params(struct vpe_ctx *ctx)
  659. {
  660. struct vpe_q_data *s_q_data = &ctx->q_data[Q_DATA_SRC];
  661. struct vpe_q_data *d_q_data = &ctx->q_data[Q_DATA_DST];
  662. size_t mv_buf_size;
  663. int ret;
  664. ctx->sequence = 0;
  665. ctx->field = V4L2_FIELD_TOP;
  666. if ((s_q_data->flags & Q_DATA_INTERLACED) &&
  667. !(d_q_data->flags & Q_DATA_INTERLACED)) {
  668. const struct vpdma_data_format *mv =
  669. &vpdma_misc_fmts[VPDMA_DATA_FMT_MV];
  670. ctx->deinterlacing = 1;
  671. mv_buf_size =
  672. (s_q_data->width * s_q_data->height * mv->depth) >> 3;
  673. } else {
  674. ctx->deinterlacing = 0;
  675. mv_buf_size = 0;
  676. }
  677. free_vbs(ctx);
  678. ret = realloc_mv_buffers(ctx, mv_buf_size);
  679. if (ret)
  680. return ret;
  681. set_cfg_and_line_modes(ctx);
  682. set_dei_regs(ctx);
  683. set_csc_coeff_bypass(ctx);
  684. set_sc_regs_bypass(ctx);
  685. return 0;
  686. }
  687. /*
  688. * Return the vpe_ctx structure for a given struct file
  689. */
  690. static struct vpe_ctx *file2ctx(struct file *file)
  691. {
  692. return container_of(file->private_data, struct vpe_ctx, fh);
  693. }
  694. /*
  695. * mem2mem callbacks
  696. */
  697. /**
  698. * job_ready() - check whether an instance is ready to be scheduled to run
  699. */
  700. static int job_ready(void *priv)
  701. {
  702. struct vpe_ctx *ctx = priv;
  703. int needed = ctx->bufs_per_job;
  704. if (ctx->deinterlacing && ctx->src_vbs[2] == NULL)
  705. needed += 2; /* need additional two most recent fields */
  706. if (v4l2_m2m_num_src_bufs_ready(ctx->m2m_ctx) < needed)
  707. return 0;
  708. return 1;
  709. }
  710. static void job_abort(void *priv)
  711. {
  712. struct vpe_ctx *ctx = priv;
  713. /* Will cancel the transaction in the next interrupt handler */
  714. ctx->aborting = 1;
  715. }
  716. /*
  717. * Lock access to the device
  718. */
  719. static void vpe_lock(void *priv)
  720. {
  721. struct vpe_ctx *ctx = priv;
  722. struct vpe_dev *dev = ctx->dev;
  723. mutex_lock(&dev->dev_mutex);
  724. }
  725. static void vpe_unlock(void *priv)
  726. {
  727. struct vpe_ctx *ctx = priv;
  728. struct vpe_dev *dev = ctx->dev;
  729. mutex_unlock(&dev->dev_mutex);
  730. }
  731. static void vpe_dump_regs(struct vpe_dev *dev)
  732. {
  733. #define DUMPREG(r) vpe_dbg(dev, "%-35s %08x\n", #r, read_reg(dev, VPE_##r))
  734. vpe_dbg(dev, "VPE Registers:\n");
  735. DUMPREG(PID);
  736. DUMPREG(SYSCONFIG);
  737. DUMPREG(INT0_STATUS0_RAW);
  738. DUMPREG(INT0_STATUS0);
  739. DUMPREG(INT0_ENABLE0);
  740. DUMPREG(INT0_STATUS1_RAW);
  741. DUMPREG(INT0_STATUS1);
  742. DUMPREG(INT0_ENABLE1);
  743. DUMPREG(CLK_ENABLE);
  744. DUMPREG(CLK_RESET);
  745. DUMPREG(CLK_FORMAT_SELECT);
  746. DUMPREG(CLK_RANGE_MAP);
  747. DUMPREG(US1_R0);
  748. DUMPREG(US1_R1);
  749. DUMPREG(US1_R2);
  750. DUMPREG(US1_R3);
  751. DUMPREG(US1_R4);
  752. DUMPREG(US1_R5);
  753. DUMPREG(US1_R6);
  754. DUMPREG(US1_R7);
  755. DUMPREG(US2_R0);
  756. DUMPREG(US2_R1);
  757. DUMPREG(US2_R2);
  758. DUMPREG(US2_R3);
  759. DUMPREG(US2_R4);
  760. DUMPREG(US2_R5);
  761. DUMPREG(US2_R6);
  762. DUMPREG(US2_R7);
  763. DUMPREG(US3_R0);
  764. DUMPREG(US3_R1);
  765. DUMPREG(US3_R2);
  766. DUMPREG(US3_R3);
  767. DUMPREG(US3_R4);
  768. DUMPREG(US3_R5);
  769. DUMPREG(US3_R6);
  770. DUMPREG(US3_R7);
  771. DUMPREG(DEI_FRAME_SIZE);
  772. DUMPREG(MDT_BYPASS);
  773. DUMPREG(MDT_SF_THRESHOLD);
  774. DUMPREG(EDI_CONFIG);
  775. DUMPREG(DEI_EDI_LUT_R0);
  776. DUMPREG(DEI_EDI_LUT_R1);
  777. DUMPREG(DEI_EDI_LUT_R2);
  778. DUMPREG(DEI_EDI_LUT_R3);
  779. DUMPREG(DEI_FMD_WINDOW_R0);
  780. DUMPREG(DEI_FMD_WINDOW_R1);
  781. DUMPREG(DEI_FMD_CONTROL_R0);
  782. DUMPREG(DEI_FMD_CONTROL_R1);
  783. DUMPREG(DEI_FMD_STATUS_R0);
  784. DUMPREG(DEI_FMD_STATUS_R1);
  785. DUMPREG(DEI_FMD_STATUS_R2);
  786. DUMPREG(SC_MP_SC0);
  787. DUMPREG(SC_MP_SC1);
  788. DUMPREG(SC_MP_SC2);
  789. DUMPREG(SC_MP_SC3);
  790. DUMPREG(SC_MP_SC4);
  791. DUMPREG(SC_MP_SC5);
  792. DUMPREG(SC_MP_SC6);
  793. DUMPREG(SC_MP_SC8);
  794. DUMPREG(SC_MP_SC9);
  795. DUMPREG(SC_MP_SC10);
  796. DUMPREG(SC_MP_SC11);
  797. DUMPREG(SC_MP_SC12);
  798. DUMPREG(SC_MP_SC13);
  799. DUMPREG(SC_MP_SC17);
  800. DUMPREG(SC_MP_SC18);
  801. DUMPREG(SC_MP_SC19);
  802. DUMPREG(SC_MP_SC20);
  803. DUMPREG(SC_MP_SC21);
  804. DUMPREG(SC_MP_SC22);
  805. DUMPREG(SC_MP_SC23);
  806. DUMPREG(SC_MP_SC24);
  807. DUMPREG(SC_MP_SC25);
  808. DUMPREG(CSC_CSC00);
  809. DUMPREG(CSC_CSC01);
  810. DUMPREG(CSC_CSC02);
  811. DUMPREG(CSC_CSC03);
  812. DUMPREG(CSC_CSC04);
  813. DUMPREG(CSC_CSC05);
  814. #undef DUMPREG
  815. }
  816. static void add_out_dtd(struct vpe_ctx *ctx, int port)
  817. {
  818. struct vpe_q_data *q_data = &ctx->q_data[Q_DATA_DST];
  819. const struct vpe_port_data *p_data = &port_data[port];
  820. struct vb2_buffer *vb = ctx->dst_vb;
  821. struct v4l2_rect *c_rect = &q_data->c_rect;
  822. struct vpe_fmt *fmt = q_data->fmt;
  823. const struct vpdma_data_format *vpdma_fmt;
  824. int mv_buf_selector = !ctx->src_mv_buf_selector;
  825. dma_addr_t dma_addr;
  826. u32 flags = 0;
  827. if (port == VPE_PORT_MV_OUT) {
  828. vpdma_fmt = &vpdma_misc_fmts[VPDMA_DATA_FMT_MV];
  829. dma_addr = ctx->mv_buf_dma[mv_buf_selector];
  830. } else {
  831. /* to incorporate interleaved formats */
  832. int plane = fmt->coplanar ? p_data->vb_part : 0;
  833. vpdma_fmt = fmt->vpdma_fmt[plane];
  834. dma_addr = vb2_dma_contig_plane_dma_addr(vb, plane);
  835. if (!dma_addr) {
  836. vpe_err(ctx->dev,
  837. "acquiring output buffer(%d) dma_addr failed\n",
  838. port);
  839. return;
  840. }
  841. }
  842. if (q_data->flags & Q_DATA_FRAME_1D)
  843. flags |= VPDMA_DATA_FRAME_1D;
  844. if (q_data->flags & Q_DATA_MODE_TILED)
  845. flags |= VPDMA_DATA_MODE_TILED;
  846. vpdma_add_out_dtd(&ctx->desc_list, c_rect, vpdma_fmt, dma_addr,
  847. p_data->channel, flags);
  848. }
  849. static void add_in_dtd(struct vpe_ctx *ctx, int port)
  850. {
  851. struct vpe_q_data *q_data = &ctx->q_data[Q_DATA_SRC];
  852. const struct vpe_port_data *p_data = &port_data[port];
  853. struct vb2_buffer *vb = ctx->src_vbs[p_data->vb_index];
  854. struct v4l2_rect *c_rect = &q_data->c_rect;
  855. struct vpe_fmt *fmt = q_data->fmt;
  856. const struct vpdma_data_format *vpdma_fmt;
  857. int mv_buf_selector = ctx->src_mv_buf_selector;
  858. int field = vb->v4l2_buf.field == V4L2_FIELD_BOTTOM;
  859. dma_addr_t dma_addr;
  860. u32 flags = 0;
  861. if (port == VPE_PORT_MV_IN) {
  862. vpdma_fmt = &vpdma_misc_fmts[VPDMA_DATA_FMT_MV];
  863. dma_addr = ctx->mv_buf_dma[mv_buf_selector];
  864. } else {
  865. /* to incorporate interleaved formats */
  866. int plane = fmt->coplanar ? p_data->vb_part : 0;
  867. vpdma_fmt = fmt->vpdma_fmt[plane];
  868. dma_addr = vb2_dma_contig_plane_dma_addr(vb, plane);
  869. if (!dma_addr) {
  870. vpe_err(ctx->dev,
  871. "acquiring input buffer(%d) dma_addr failed\n",
  872. port);
  873. return;
  874. }
  875. }
  876. if (q_data->flags & Q_DATA_FRAME_1D)
  877. flags |= VPDMA_DATA_FRAME_1D;
  878. if (q_data->flags & Q_DATA_MODE_TILED)
  879. flags |= VPDMA_DATA_MODE_TILED;
  880. vpdma_add_in_dtd(&ctx->desc_list, q_data->width, q_data->height,
  881. c_rect, vpdma_fmt, dma_addr, p_data->channel, field, flags);
  882. }
  883. /*
  884. * Enable the expected IRQ sources
  885. */
  886. static void enable_irqs(struct vpe_ctx *ctx)
  887. {
  888. write_reg(ctx->dev, VPE_INT0_ENABLE0_SET, VPE_INT0_LIST0_COMPLETE);
  889. write_reg(ctx->dev, VPE_INT0_ENABLE1_SET, VPE_DEI_ERROR_INT |
  890. VPE_DS1_UV_ERROR_INT);
  891. vpdma_enable_list_complete_irq(ctx->dev->vpdma, 0, true);
  892. }
  893. static void disable_irqs(struct vpe_ctx *ctx)
  894. {
  895. write_reg(ctx->dev, VPE_INT0_ENABLE0_CLR, 0xffffffff);
  896. write_reg(ctx->dev, VPE_INT0_ENABLE1_CLR, 0xffffffff);
  897. vpdma_enable_list_complete_irq(ctx->dev->vpdma, 0, false);
  898. }
  899. /* device_run() - prepares and starts the device
  900. *
  901. * This function is only called when both the source and destination
  902. * buffers are in place.
  903. */
  904. static void device_run(void *priv)
  905. {
  906. struct vpe_ctx *ctx = priv;
  907. struct vpe_q_data *d_q_data = &ctx->q_data[Q_DATA_DST];
  908. if (ctx->deinterlacing && ctx->src_vbs[2] == NULL) {
  909. ctx->src_vbs[2] = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
  910. WARN_ON(ctx->src_vbs[2] == NULL);
  911. ctx->src_vbs[1] = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
  912. WARN_ON(ctx->src_vbs[1] == NULL);
  913. }
  914. ctx->src_vbs[0] = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
  915. WARN_ON(ctx->src_vbs[0] == NULL);
  916. ctx->dst_vb = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
  917. WARN_ON(ctx->dst_vb == NULL);
  918. /* config descriptors */
  919. if (ctx->dev->loaded_mmrs != ctx->mmr_adb.dma_addr || ctx->load_mmrs) {
  920. vpdma_map_desc_buf(ctx->dev->vpdma, &ctx->mmr_adb);
  921. vpdma_add_cfd_adb(&ctx->desc_list, CFD_MMR_CLIENT, &ctx->mmr_adb);
  922. ctx->dev->loaded_mmrs = ctx->mmr_adb.dma_addr;
  923. ctx->load_mmrs = false;
  924. }
  925. /* output data descriptors */
  926. if (ctx->deinterlacing)
  927. add_out_dtd(ctx, VPE_PORT_MV_OUT);
  928. add_out_dtd(ctx, VPE_PORT_LUMA_OUT);
  929. if (d_q_data->fmt->coplanar)
  930. add_out_dtd(ctx, VPE_PORT_CHROMA_OUT);
  931. /* input data descriptors */
  932. if (ctx->deinterlacing) {
  933. add_in_dtd(ctx, VPE_PORT_LUMA3_IN);
  934. add_in_dtd(ctx, VPE_PORT_CHROMA3_IN);
  935. add_in_dtd(ctx, VPE_PORT_LUMA2_IN);
  936. add_in_dtd(ctx, VPE_PORT_CHROMA2_IN);
  937. }
  938. add_in_dtd(ctx, VPE_PORT_LUMA1_IN);
  939. add_in_dtd(ctx, VPE_PORT_CHROMA1_IN);
  940. if (ctx->deinterlacing)
  941. add_in_dtd(ctx, VPE_PORT_MV_IN);
  942. /* sync on channel control descriptors for input ports */
  943. vpdma_add_sync_on_channel_ctd(&ctx->desc_list, VPE_CHAN_LUMA1_IN);
  944. vpdma_add_sync_on_channel_ctd(&ctx->desc_list, VPE_CHAN_CHROMA1_IN);
  945. if (ctx->deinterlacing) {
  946. vpdma_add_sync_on_channel_ctd(&ctx->desc_list,
  947. VPE_CHAN_LUMA2_IN);
  948. vpdma_add_sync_on_channel_ctd(&ctx->desc_list,
  949. VPE_CHAN_CHROMA2_IN);
  950. vpdma_add_sync_on_channel_ctd(&ctx->desc_list,
  951. VPE_CHAN_LUMA3_IN);
  952. vpdma_add_sync_on_channel_ctd(&ctx->desc_list,
  953. VPE_CHAN_CHROMA3_IN);
  954. vpdma_add_sync_on_channel_ctd(&ctx->desc_list, VPE_CHAN_MV_IN);
  955. }
  956. /* sync on channel control descriptors for output ports */
  957. vpdma_add_sync_on_channel_ctd(&ctx->desc_list, VPE_CHAN_LUMA_OUT);
  958. if (d_q_data->fmt->coplanar)
  959. vpdma_add_sync_on_channel_ctd(&ctx->desc_list, VPE_CHAN_CHROMA_OUT);
  960. if (ctx->deinterlacing)
  961. vpdma_add_sync_on_channel_ctd(&ctx->desc_list, VPE_CHAN_MV_OUT);
  962. enable_irqs(ctx);
  963. vpdma_map_desc_buf(ctx->dev->vpdma, &ctx->desc_list.buf);
  964. vpdma_submit_descs(ctx->dev->vpdma, &ctx->desc_list);
  965. }
  966. static void dei_error(struct vpe_ctx *ctx)
  967. {
  968. dev_warn(ctx->dev->v4l2_dev.dev,
  969. "received DEI error interrupt\n");
  970. }
  971. static void ds1_uv_error(struct vpe_ctx *ctx)
  972. {
  973. dev_warn(ctx->dev->v4l2_dev.dev,
  974. "received downsampler error interrupt\n");
  975. }
  976. static irqreturn_t vpe_irq(int irq_vpe, void *data)
  977. {
  978. struct vpe_dev *dev = (struct vpe_dev *)data;
  979. struct vpe_ctx *ctx;
  980. struct vpe_q_data *d_q_data;
  981. struct vb2_buffer *s_vb, *d_vb;
  982. struct v4l2_buffer *s_buf, *d_buf;
  983. unsigned long flags;
  984. u32 irqst0, irqst1;
  985. irqst0 = read_reg(dev, VPE_INT0_STATUS0);
  986. if (irqst0) {
  987. write_reg(dev, VPE_INT0_STATUS0_CLR, irqst0);
  988. vpe_dbg(dev, "INT0_STATUS0 = 0x%08x\n", irqst0);
  989. }
  990. irqst1 = read_reg(dev, VPE_INT0_STATUS1);
  991. if (irqst1) {
  992. write_reg(dev, VPE_INT0_STATUS1_CLR, irqst1);
  993. vpe_dbg(dev, "INT0_STATUS1 = 0x%08x\n", irqst1);
  994. }
  995. ctx = v4l2_m2m_get_curr_priv(dev->m2m_dev);
  996. if (!ctx) {
  997. vpe_err(dev, "instance released before end of transaction\n");
  998. goto handled;
  999. }
  1000. if (irqst1) {
  1001. if (irqst1 & VPE_DEI_ERROR_INT) {
  1002. irqst1 &= ~VPE_DEI_ERROR_INT;
  1003. dei_error(ctx);
  1004. }
  1005. if (irqst1 & VPE_DS1_UV_ERROR_INT) {
  1006. irqst1 &= ~VPE_DS1_UV_ERROR_INT;
  1007. ds1_uv_error(ctx);
  1008. }
  1009. }
  1010. if (irqst0) {
  1011. if (irqst0 & VPE_INT0_LIST0_COMPLETE)
  1012. vpdma_clear_list_stat(ctx->dev->vpdma);
  1013. irqst0 &= ~(VPE_INT0_LIST0_COMPLETE);
  1014. }
  1015. if (irqst0 | irqst1) {
  1016. dev_warn(dev->v4l2_dev.dev, "Unexpected interrupt: "
  1017. "INT0_STATUS0 = 0x%08x, INT0_STATUS1 = 0x%08x\n",
  1018. irqst0, irqst1);
  1019. }
  1020. disable_irqs(ctx);
  1021. vpdma_unmap_desc_buf(dev->vpdma, &ctx->desc_list.buf);
  1022. vpdma_unmap_desc_buf(dev->vpdma, &ctx->mmr_adb);
  1023. vpdma_reset_desc_list(&ctx->desc_list);
  1024. /* the previous dst mv buffer becomes the next src mv buffer */
  1025. ctx->src_mv_buf_selector = !ctx->src_mv_buf_selector;
  1026. if (ctx->aborting)
  1027. goto finished;
  1028. s_vb = ctx->src_vbs[0];
  1029. d_vb = ctx->dst_vb;
  1030. s_buf = &s_vb->v4l2_buf;
  1031. d_buf = &d_vb->v4l2_buf;
  1032. d_buf->timestamp = s_buf->timestamp;
  1033. if (s_buf->flags & V4L2_BUF_FLAG_TIMECODE) {
  1034. d_buf->flags |= V4L2_BUF_FLAG_TIMECODE;
  1035. d_buf->timecode = s_buf->timecode;
  1036. }
  1037. d_buf->sequence = ctx->sequence;
  1038. d_buf->field = ctx->field;
  1039. d_q_data = &ctx->q_data[Q_DATA_DST];
  1040. if (d_q_data->flags & Q_DATA_INTERLACED) {
  1041. if (ctx->field == V4L2_FIELD_BOTTOM) {
  1042. ctx->sequence++;
  1043. ctx->field = V4L2_FIELD_TOP;
  1044. } else {
  1045. WARN_ON(ctx->field != V4L2_FIELD_TOP);
  1046. ctx->field = V4L2_FIELD_BOTTOM;
  1047. }
  1048. } else {
  1049. ctx->sequence++;
  1050. }
  1051. if (ctx->deinterlacing)
  1052. s_vb = ctx->src_vbs[2];
  1053. spin_lock_irqsave(&dev->lock, flags);
  1054. v4l2_m2m_buf_done(s_vb, VB2_BUF_STATE_DONE);
  1055. v4l2_m2m_buf_done(d_vb, VB2_BUF_STATE_DONE);
  1056. spin_unlock_irqrestore(&dev->lock, flags);
  1057. if (ctx->deinterlacing) {
  1058. ctx->src_vbs[2] = ctx->src_vbs[1];
  1059. ctx->src_vbs[1] = ctx->src_vbs[0];
  1060. }
  1061. ctx->bufs_completed++;
  1062. if (ctx->bufs_completed < ctx->bufs_per_job) {
  1063. device_run(ctx);
  1064. goto handled;
  1065. }
  1066. finished:
  1067. vpe_dbg(ctx->dev, "finishing transaction\n");
  1068. ctx->bufs_completed = 0;
  1069. v4l2_m2m_job_finish(dev->m2m_dev, ctx->m2m_ctx);
  1070. handled:
  1071. return IRQ_HANDLED;
  1072. }
  1073. /*
  1074. * video ioctls
  1075. */
  1076. static int vpe_querycap(struct file *file, void *priv,
  1077. struct v4l2_capability *cap)
  1078. {
  1079. strncpy(cap->driver, VPE_MODULE_NAME, sizeof(cap->driver) - 1);
  1080. strncpy(cap->card, VPE_MODULE_NAME, sizeof(cap->card) - 1);
  1081. strlcpy(cap->bus_info, VPE_MODULE_NAME, sizeof(cap->bus_info));
  1082. cap->device_caps = V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING;
  1083. cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
  1084. return 0;
  1085. }
  1086. static int __enum_fmt(struct v4l2_fmtdesc *f, u32 type)
  1087. {
  1088. int i, index;
  1089. struct vpe_fmt *fmt = NULL;
  1090. index = 0;
  1091. for (i = 0; i < ARRAY_SIZE(vpe_formats); ++i) {
  1092. if (vpe_formats[i].types & type) {
  1093. if (index == f->index) {
  1094. fmt = &vpe_formats[i];
  1095. break;
  1096. }
  1097. index++;
  1098. }
  1099. }
  1100. if (!fmt)
  1101. return -EINVAL;
  1102. strncpy(f->description, fmt->name, sizeof(f->description) - 1);
  1103. f->pixelformat = fmt->fourcc;
  1104. return 0;
  1105. }
  1106. static int vpe_enum_fmt(struct file *file, void *priv,
  1107. struct v4l2_fmtdesc *f)
  1108. {
  1109. if (V4L2_TYPE_IS_OUTPUT(f->type))
  1110. return __enum_fmt(f, VPE_FMT_TYPE_OUTPUT);
  1111. return __enum_fmt(f, VPE_FMT_TYPE_CAPTURE);
  1112. }
  1113. static int vpe_g_fmt(struct file *file, void *priv, struct v4l2_format *f)
  1114. {
  1115. struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp;
  1116. struct vpe_ctx *ctx = file2ctx(file);
  1117. struct vb2_queue *vq;
  1118. struct vpe_q_data *q_data;
  1119. int i;
  1120. vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
  1121. if (!vq)
  1122. return -EINVAL;
  1123. q_data = get_q_data(ctx, f->type);
  1124. pix->width = q_data->width;
  1125. pix->height = q_data->height;
  1126. pix->pixelformat = q_data->fmt->fourcc;
  1127. pix->field = q_data->field;
  1128. if (V4L2_TYPE_IS_OUTPUT(f->type)) {
  1129. pix->colorspace = q_data->colorspace;
  1130. } else {
  1131. struct vpe_q_data *s_q_data;
  1132. /* get colorspace from the source queue */
  1133. s_q_data = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
  1134. pix->colorspace = s_q_data->colorspace;
  1135. }
  1136. pix->num_planes = q_data->fmt->coplanar ? 2 : 1;
  1137. for (i = 0; i < pix->num_planes; i++) {
  1138. pix->plane_fmt[i].bytesperline = q_data->bytesperline[i];
  1139. pix->plane_fmt[i].sizeimage = q_data->sizeimage[i];
  1140. }
  1141. return 0;
  1142. }
  1143. static int __vpe_try_fmt(struct vpe_ctx *ctx, struct v4l2_format *f,
  1144. struct vpe_fmt *fmt, int type)
  1145. {
  1146. struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp;
  1147. struct v4l2_plane_pix_format *plane_fmt;
  1148. int i;
  1149. if (!fmt || !(fmt->types & type)) {
  1150. vpe_err(ctx->dev, "Fourcc format (0x%08x) invalid.\n",
  1151. pix->pixelformat);
  1152. return -EINVAL;
  1153. }
  1154. if (pix->field != V4L2_FIELD_NONE && pix->field != V4L2_FIELD_ALTERNATE)
  1155. pix->field = V4L2_FIELD_NONE;
  1156. v4l_bound_align_image(&pix->width, MIN_W, MAX_W, W_ALIGN,
  1157. &pix->height, MIN_H, MAX_H, H_ALIGN,
  1158. S_ALIGN);
  1159. pix->num_planes = fmt->coplanar ? 2 : 1;
  1160. pix->pixelformat = fmt->fourcc;
  1161. if (type == VPE_FMT_TYPE_CAPTURE) {
  1162. struct vpe_q_data *s_q_data;
  1163. /* get colorspace from the source queue */
  1164. s_q_data = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
  1165. pix->colorspace = s_q_data->colorspace;
  1166. } else {
  1167. if (!pix->colorspace)
  1168. pix->colorspace = V4L2_COLORSPACE_SMPTE240M;
  1169. }
  1170. for (i = 0; i < pix->num_planes; i++) {
  1171. int depth;
  1172. plane_fmt = &pix->plane_fmt[i];
  1173. depth = fmt->vpdma_fmt[i]->depth;
  1174. if (i == VPE_LUMA)
  1175. plane_fmt->bytesperline =
  1176. round_up((pix->width * depth) >> 3,
  1177. 1 << L_ALIGN);
  1178. else
  1179. plane_fmt->bytesperline = pix->width;
  1180. plane_fmt->sizeimage =
  1181. (pix->height * pix->width * depth) >> 3;
  1182. }
  1183. return 0;
  1184. }
  1185. static int vpe_try_fmt(struct file *file, void *priv, struct v4l2_format *f)
  1186. {
  1187. struct vpe_ctx *ctx = file2ctx(file);
  1188. struct vpe_fmt *fmt = find_format(f);
  1189. if (V4L2_TYPE_IS_OUTPUT(f->type))
  1190. return __vpe_try_fmt(ctx, f, fmt, VPE_FMT_TYPE_OUTPUT);
  1191. else
  1192. return __vpe_try_fmt(ctx, f, fmt, VPE_FMT_TYPE_CAPTURE);
  1193. }
  1194. static int __vpe_s_fmt(struct vpe_ctx *ctx, struct v4l2_format *f)
  1195. {
  1196. struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp;
  1197. struct v4l2_plane_pix_format *plane_fmt;
  1198. struct vpe_q_data *q_data;
  1199. struct vb2_queue *vq;
  1200. int i;
  1201. vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
  1202. if (!vq)
  1203. return -EINVAL;
  1204. if (vb2_is_busy(vq)) {
  1205. vpe_err(ctx->dev, "queue busy\n");
  1206. return -EBUSY;
  1207. }
  1208. q_data = get_q_data(ctx, f->type);
  1209. if (!q_data)
  1210. return -EINVAL;
  1211. q_data->fmt = find_format(f);
  1212. q_data->width = pix->width;
  1213. q_data->height = pix->height;
  1214. q_data->colorspace = pix->colorspace;
  1215. q_data->field = pix->field;
  1216. for (i = 0; i < pix->num_planes; i++) {
  1217. plane_fmt = &pix->plane_fmt[i];
  1218. q_data->bytesperline[i] = plane_fmt->bytesperline;
  1219. q_data->sizeimage[i] = plane_fmt->sizeimage;
  1220. }
  1221. q_data->c_rect.left = 0;
  1222. q_data->c_rect.top = 0;
  1223. q_data->c_rect.width = q_data->width;
  1224. q_data->c_rect.height = q_data->height;
  1225. if (q_data->field == V4L2_FIELD_ALTERNATE)
  1226. q_data->flags |= Q_DATA_INTERLACED;
  1227. else
  1228. q_data->flags &= ~Q_DATA_INTERLACED;
  1229. vpe_dbg(ctx->dev, "Setting format for type %d, wxh: %dx%d, fmt: %d bpl_y %d",
  1230. f->type, q_data->width, q_data->height, q_data->fmt->fourcc,
  1231. q_data->bytesperline[VPE_LUMA]);
  1232. if (q_data->fmt->coplanar)
  1233. vpe_dbg(ctx->dev, " bpl_uv %d\n",
  1234. q_data->bytesperline[VPE_CHROMA]);
  1235. return 0;
  1236. }
  1237. static int vpe_s_fmt(struct file *file, void *priv, struct v4l2_format *f)
  1238. {
  1239. int ret;
  1240. struct vpe_ctx *ctx = file2ctx(file);
  1241. ret = vpe_try_fmt(file, priv, f);
  1242. if (ret)
  1243. return ret;
  1244. ret = __vpe_s_fmt(ctx, f);
  1245. if (ret)
  1246. return ret;
  1247. if (V4L2_TYPE_IS_OUTPUT(f->type))
  1248. set_src_registers(ctx);
  1249. else
  1250. set_dst_registers(ctx);
  1251. return set_srcdst_params(ctx);
  1252. }
  1253. static int vpe_reqbufs(struct file *file, void *priv,
  1254. struct v4l2_requestbuffers *reqbufs)
  1255. {
  1256. struct vpe_ctx *ctx = file2ctx(file);
  1257. return v4l2_m2m_reqbufs(file, ctx->m2m_ctx, reqbufs);
  1258. }
  1259. static int vpe_querybuf(struct file *file, void *priv, struct v4l2_buffer *buf)
  1260. {
  1261. struct vpe_ctx *ctx = file2ctx(file);
  1262. return v4l2_m2m_querybuf(file, ctx->m2m_ctx, buf);
  1263. }
  1264. static int vpe_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
  1265. {
  1266. struct vpe_ctx *ctx = file2ctx(file);
  1267. return v4l2_m2m_qbuf(file, ctx->m2m_ctx, buf);
  1268. }
  1269. static int vpe_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
  1270. {
  1271. struct vpe_ctx *ctx = file2ctx(file);
  1272. return v4l2_m2m_dqbuf(file, ctx->m2m_ctx, buf);
  1273. }
  1274. static int vpe_streamon(struct file *file, void *priv, enum v4l2_buf_type type)
  1275. {
  1276. struct vpe_ctx *ctx = file2ctx(file);
  1277. return v4l2_m2m_streamon(file, ctx->m2m_ctx, type);
  1278. }
  1279. static int vpe_streamoff(struct file *file, void *priv, enum v4l2_buf_type type)
  1280. {
  1281. struct vpe_ctx *ctx = file2ctx(file);
  1282. vpe_dump_regs(ctx->dev);
  1283. vpdma_dump_regs(ctx->dev->vpdma);
  1284. return v4l2_m2m_streamoff(file, ctx->m2m_ctx, type);
  1285. }
  1286. /*
  1287. * defines number of buffers/frames a context can process with VPE before
  1288. * switching to a different context. default value is 1 buffer per context
  1289. */
  1290. #define V4L2_CID_VPE_BUFS_PER_JOB (V4L2_CID_USER_TI_VPE_BASE + 0)
  1291. static int vpe_s_ctrl(struct v4l2_ctrl *ctrl)
  1292. {
  1293. struct vpe_ctx *ctx =
  1294. container_of(ctrl->handler, struct vpe_ctx, hdl);
  1295. switch (ctrl->id) {
  1296. case V4L2_CID_VPE_BUFS_PER_JOB:
  1297. ctx->bufs_per_job = ctrl->val;
  1298. break;
  1299. default:
  1300. vpe_err(ctx->dev, "Invalid control\n");
  1301. return -EINVAL;
  1302. }
  1303. return 0;
  1304. }
  1305. static const struct v4l2_ctrl_ops vpe_ctrl_ops = {
  1306. .s_ctrl = vpe_s_ctrl,
  1307. };
  1308. static const struct v4l2_ioctl_ops vpe_ioctl_ops = {
  1309. .vidioc_querycap = vpe_querycap,
  1310. .vidioc_enum_fmt_vid_cap_mplane = vpe_enum_fmt,
  1311. .vidioc_g_fmt_vid_cap_mplane = vpe_g_fmt,
  1312. .vidioc_try_fmt_vid_cap_mplane = vpe_try_fmt,
  1313. .vidioc_s_fmt_vid_cap_mplane = vpe_s_fmt,
  1314. .vidioc_enum_fmt_vid_out_mplane = vpe_enum_fmt,
  1315. .vidioc_g_fmt_vid_out_mplane = vpe_g_fmt,
  1316. .vidioc_try_fmt_vid_out_mplane = vpe_try_fmt,
  1317. .vidioc_s_fmt_vid_out_mplane = vpe_s_fmt,
  1318. .vidioc_reqbufs = vpe_reqbufs,
  1319. .vidioc_querybuf = vpe_querybuf,
  1320. .vidioc_qbuf = vpe_qbuf,
  1321. .vidioc_dqbuf = vpe_dqbuf,
  1322. .vidioc_streamon = vpe_streamon,
  1323. .vidioc_streamoff = vpe_streamoff,
  1324. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  1325. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  1326. };
  1327. /*
  1328. * Queue operations
  1329. */
  1330. static int vpe_queue_setup(struct vb2_queue *vq,
  1331. const struct v4l2_format *fmt,
  1332. unsigned int *nbuffers, unsigned int *nplanes,
  1333. unsigned int sizes[], void *alloc_ctxs[])
  1334. {
  1335. int i;
  1336. struct vpe_ctx *ctx = vb2_get_drv_priv(vq);
  1337. struct vpe_q_data *q_data;
  1338. q_data = get_q_data(ctx, vq->type);
  1339. *nplanes = q_data->fmt->coplanar ? 2 : 1;
  1340. for (i = 0; i < *nplanes; i++) {
  1341. sizes[i] = q_data->sizeimage[i];
  1342. alloc_ctxs[i] = ctx->dev->alloc_ctx;
  1343. }
  1344. vpe_dbg(ctx->dev, "get %d buffer(s) of size %d", *nbuffers,
  1345. sizes[VPE_LUMA]);
  1346. if (q_data->fmt->coplanar)
  1347. vpe_dbg(ctx->dev, " and %d\n", sizes[VPE_CHROMA]);
  1348. return 0;
  1349. }
  1350. static int vpe_buf_prepare(struct vb2_buffer *vb)
  1351. {
  1352. struct vpe_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  1353. struct vpe_q_data *q_data;
  1354. int i, num_planes;
  1355. vpe_dbg(ctx->dev, "type: %d\n", vb->vb2_queue->type);
  1356. q_data = get_q_data(ctx, vb->vb2_queue->type);
  1357. num_planes = q_data->fmt->coplanar ? 2 : 1;
  1358. for (i = 0; i < num_planes; i++) {
  1359. if (vb2_plane_size(vb, i) < q_data->sizeimage[i]) {
  1360. vpe_err(ctx->dev,
  1361. "data will not fit into plane (%lu < %lu)\n",
  1362. vb2_plane_size(vb, i),
  1363. (long) q_data->sizeimage[i]);
  1364. return -EINVAL;
  1365. }
  1366. }
  1367. for (i = 0; i < num_planes; i++)
  1368. vb2_set_plane_payload(vb, i, q_data->sizeimage[i]);
  1369. return 0;
  1370. }
  1371. static void vpe_buf_queue(struct vb2_buffer *vb)
  1372. {
  1373. struct vpe_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  1374. v4l2_m2m_buf_queue(ctx->m2m_ctx, vb);
  1375. }
  1376. static void vpe_wait_prepare(struct vb2_queue *q)
  1377. {
  1378. struct vpe_ctx *ctx = vb2_get_drv_priv(q);
  1379. vpe_unlock(ctx);
  1380. }
  1381. static void vpe_wait_finish(struct vb2_queue *q)
  1382. {
  1383. struct vpe_ctx *ctx = vb2_get_drv_priv(q);
  1384. vpe_lock(ctx);
  1385. }
  1386. static struct vb2_ops vpe_qops = {
  1387. .queue_setup = vpe_queue_setup,
  1388. .buf_prepare = vpe_buf_prepare,
  1389. .buf_queue = vpe_buf_queue,
  1390. .wait_prepare = vpe_wait_prepare,
  1391. .wait_finish = vpe_wait_finish,
  1392. };
  1393. static int queue_init(void *priv, struct vb2_queue *src_vq,
  1394. struct vb2_queue *dst_vq)
  1395. {
  1396. struct vpe_ctx *ctx = priv;
  1397. int ret;
  1398. memset(src_vq, 0, sizeof(*src_vq));
  1399. src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
  1400. src_vq->io_modes = VB2_MMAP;
  1401. src_vq->drv_priv = ctx;
  1402. src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
  1403. src_vq->ops = &vpe_qops;
  1404. src_vq->mem_ops = &vb2_dma_contig_memops;
  1405. src_vq->timestamp_type = V4L2_BUF_FLAG_TIMESTAMP_COPY;
  1406. ret = vb2_queue_init(src_vq);
  1407. if (ret)
  1408. return ret;
  1409. memset(dst_vq, 0, sizeof(*dst_vq));
  1410. dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
  1411. dst_vq->io_modes = VB2_MMAP;
  1412. dst_vq->drv_priv = ctx;
  1413. dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
  1414. dst_vq->ops = &vpe_qops;
  1415. dst_vq->mem_ops = &vb2_dma_contig_memops;
  1416. dst_vq->timestamp_type = V4L2_BUF_FLAG_TIMESTAMP_COPY;
  1417. return vb2_queue_init(dst_vq);
  1418. }
  1419. static const struct v4l2_ctrl_config vpe_bufs_per_job = {
  1420. .ops = &vpe_ctrl_ops,
  1421. .id = V4L2_CID_VPE_BUFS_PER_JOB,
  1422. .name = "Buffers Per Transaction",
  1423. .type = V4L2_CTRL_TYPE_INTEGER,
  1424. .def = VPE_DEF_BUFS_PER_JOB,
  1425. .min = 1,
  1426. .max = VIDEO_MAX_FRAME,
  1427. .step = 1,
  1428. };
  1429. /*
  1430. * File operations
  1431. */
  1432. static int vpe_open(struct file *file)
  1433. {
  1434. struct vpe_dev *dev = video_drvdata(file);
  1435. struct vpe_ctx *ctx = NULL;
  1436. struct vpe_q_data *s_q_data;
  1437. struct v4l2_ctrl_handler *hdl;
  1438. int ret;
  1439. vpe_dbg(dev, "vpe_open\n");
  1440. if (!dev->vpdma->ready) {
  1441. vpe_err(dev, "vpdma firmware not loaded\n");
  1442. return -ENODEV;
  1443. }
  1444. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  1445. if (!ctx)
  1446. return -ENOMEM;
  1447. ctx->dev = dev;
  1448. if (mutex_lock_interruptible(&dev->dev_mutex)) {
  1449. ret = -ERESTARTSYS;
  1450. goto free_ctx;
  1451. }
  1452. ret = vpdma_create_desc_list(&ctx->desc_list, VPE_DESC_LIST_SIZE,
  1453. VPDMA_LIST_TYPE_NORMAL);
  1454. if (ret != 0)
  1455. goto unlock;
  1456. ret = vpdma_alloc_desc_buf(&ctx->mmr_adb, sizeof(struct vpe_mmr_adb));
  1457. if (ret != 0)
  1458. goto free_desc_list;
  1459. init_adb_hdrs(ctx);
  1460. v4l2_fh_init(&ctx->fh, video_devdata(file));
  1461. file->private_data = &ctx->fh;
  1462. hdl = &ctx->hdl;
  1463. v4l2_ctrl_handler_init(hdl, 1);
  1464. v4l2_ctrl_new_custom(hdl, &vpe_bufs_per_job, NULL);
  1465. if (hdl->error) {
  1466. ret = hdl->error;
  1467. goto exit_fh;
  1468. }
  1469. ctx->fh.ctrl_handler = hdl;
  1470. v4l2_ctrl_handler_setup(hdl);
  1471. s_q_data = &ctx->q_data[Q_DATA_SRC];
  1472. s_q_data->fmt = &vpe_formats[2];
  1473. s_q_data->width = 1920;
  1474. s_q_data->height = 1080;
  1475. s_q_data->sizeimage[VPE_LUMA] = (s_q_data->width * s_q_data->height *
  1476. s_q_data->fmt->vpdma_fmt[VPE_LUMA]->depth) >> 3;
  1477. s_q_data->colorspace = V4L2_COLORSPACE_SMPTE240M;
  1478. s_q_data->field = V4L2_FIELD_NONE;
  1479. s_q_data->c_rect.left = 0;
  1480. s_q_data->c_rect.top = 0;
  1481. s_q_data->c_rect.width = s_q_data->width;
  1482. s_q_data->c_rect.height = s_q_data->height;
  1483. s_q_data->flags = 0;
  1484. ctx->q_data[Q_DATA_DST] = *s_q_data;
  1485. set_dei_shadow_registers(ctx);
  1486. set_src_registers(ctx);
  1487. set_dst_registers(ctx);
  1488. ret = set_srcdst_params(ctx);
  1489. if (ret)
  1490. goto exit_fh;
  1491. ctx->m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx, &queue_init);
  1492. if (IS_ERR(ctx->m2m_ctx)) {
  1493. ret = PTR_ERR(ctx->m2m_ctx);
  1494. goto exit_fh;
  1495. }
  1496. v4l2_fh_add(&ctx->fh);
  1497. /*
  1498. * for now, just report the creation of the first instance, we can later
  1499. * optimize the driver to enable or disable clocks when the first
  1500. * instance is created or the last instance released
  1501. */
  1502. if (atomic_inc_return(&dev->num_instances) == 1)
  1503. vpe_dbg(dev, "first instance created\n");
  1504. ctx->bufs_per_job = VPE_DEF_BUFS_PER_JOB;
  1505. ctx->load_mmrs = true;
  1506. vpe_dbg(dev, "created instance %p, m2m_ctx: %p\n",
  1507. ctx, ctx->m2m_ctx);
  1508. mutex_unlock(&dev->dev_mutex);
  1509. return 0;
  1510. exit_fh:
  1511. v4l2_ctrl_handler_free(hdl);
  1512. v4l2_fh_exit(&ctx->fh);
  1513. vpdma_free_desc_buf(&ctx->mmr_adb);
  1514. free_desc_list:
  1515. vpdma_free_desc_list(&ctx->desc_list);
  1516. unlock:
  1517. mutex_unlock(&dev->dev_mutex);
  1518. free_ctx:
  1519. kfree(ctx);
  1520. return ret;
  1521. }
  1522. static int vpe_release(struct file *file)
  1523. {
  1524. struct vpe_dev *dev = video_drvdata(file);
  1525. struct vpe_ctx *ctx = file2ctx(file);
  1526. vpe_dbg(dev, "releasing instance %p\n", ctx);
  1527. mutex_lock(&dev->dev_mutex);
  1528. free_vbs(ctx);
  1529. free_mv_buffers(ctx);
  1530. vpdma_free_desc_list(&ctx->desc_list);
  1531. vpdma_free_desc_buf(&ctx->mmr_adb);
  1532. v4l2_fh_del(&ctx->fh);
  1533. v4l2_fh_exit(&ctx->fh);
  1534. v4l2_ctrl_handler_free(&ctx->hdl);
  1535. v4l2_m2m_ctx_release(ctx->m2m_ctx);
  1536. kfree(ctx);
  1537. /*
  1538. * for now, just report the release of the last instance, we can later
  1539. * optimize the driver to enable or disable clocks when the first
  1540. * instance is created or the last instance released
  1541. */
  1542. if (atomic_dec_return(&dev->num_instances) == 0)
  1543. vpe_dbg(dev, "last instance released\n");
  1544. mutex_unlock(&dev->dev_mutex);
  1545. return 0;
  1546. }
  1547. static unsigned int vpe_poll(struct file *file,
  1548. struct poll_table_struct *wait)
  1549. {
  1550. struct vpe_ctx *ctx = file2ctx(file);
  1551. struct vpe_dev *dev = ctx->dev;
  1552. int ret;
  1553. mutex_lock(&dev->dev_mutex);
  1554. ret = v4l2_m2m_poll(file, ctx->m2m_ctx, wait);
  1555. mutex_unlock(&dev->dev_mutex);
  1556. return ret;
  1557. }
  1558. static int vpe_mmap(struct file *file, struct vm_area_struct *vma)
  1559. {
  1560. struct vpe_ctx *ctx = file2ctx(file);
  1561. struct vpe_dev *dev = ctx->dev;
  1562. int ret;
  1563. if (mutex_lock_interruptible(&dev->dev_mutex))
  1564. return -ERESTARTSYS;
  1565. ret = v4l2_m2m_mmap(file, ctx->m2m_ctx, vma);
  1566. mutex_unlock(&dev->dev_mutex);
  1567. return ret;
  1568. }
  1569. static const struct v4l2_file_operations vpe_fops = {
  1570. .owner = THIS_MODULE,
  1571. .open = vpe_open,
  1572. .release = vpe_release,
  1573. .poll = vpe_poll,
  1574. .unlocked_ioctl = video_ioctl2,
  1575. .mmap = vpe_mmap,
  1576. };
  1577. static struct video_device vpe_videodev = {
  1578. .name = VPE_MODULE_NAME,
  1579. .fops = &vpe_fops,
  1580. .ioctl_ops = &vpe_ioctl_ops,
  1581. .minor = -1,
  1582. .release = video_device_release,
  1583. .vfl_dir = VFL_DIR_M2M,
  1584. };
  1585. static struct v4l2_m2m_ops m2m_ops = {
  1586. .device_run = device_run,
  1587. .job_ready = job_ready,
  1588. .job_abort = job_abort,
  1589. .lock = vpe_lock,
  1590. .unlock = vpe_unlock,
  1591. };
  1592. static int vpe_runtime_get(struct platform_device *pdev)
  1593. {
  1594. int r;
  1595. dev_dbg(&pdev->dev, "vpe_runtime_get\n");
  1596. r = pm_runtime_get_sync(&pdev->dev);
  1597. WARN_ON(r < 0);
  1598. return r < 0 ? r : 0;
  1599. }
  1600. static void vpe_runtime_put(struct platform_device *pdev)
  1601. {
  1602. int r;
  1603. dev_dbg(&pdev->dev, "vpe_runtime_put\n");
  1604. r = pm_runtime_put_sync(&pdev->dev);
  1605. WARN_ON(r < 0 && r != -ENOSYS);
  1606. }
  1607. static int vpe_probe(struct platform_device *pdev)
  1608. {
  1609. struct vpe_dev *dev;
  1610. struct video_device *vfd;
  1611. struct resource *res;
  1612. int ret, irq, func;
  1613. dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
  1614. if (IS_ERR(dev))
  1615. return PTR_ERR(dev);
  1616. spin_lock_init(&dev->lock);
  1617. ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
  1618. if (ret)
  1619. return ret;
  1620. atomic_set(&dev->num_instances, 0);
  1621. mutex_init(&dev->dev_mutex);
  1622. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "vpe_top");
  1623. /*
  1624. * HACK: we get resource info from device tree in the form of a list of
  1625. * VPE sub blocks, the driver currently uses only the base of vpe_top
  1626. * for register access, the driver should be changed later to access
  1627. * registers based on the sub block base addresses
  1628. */
  1629. dev->base = devm_ioremap(&pdev->dev, res->start, SZ_32K);
  1630. if (IS_ERR(dev->base)) {
  1631. ret = PTR_ERR(dev->base);
  1632. goto v4l2_dev_unreg;
  1633. }
  1634. irq = platform_get_irq(pdev, 0);
  1635. ret = devm_request_irq(&pdev->dev, irq, vpe_irq, 0, VPE_MODULE_NAME,
  1636. dev);
  1637. if (ret)
  1638. goto v4l2_dev_unreg;
  1639. platform_set_drvdata(pdev, dev);
  1640. dev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
  1641. if (IS_ERR(dev->alloc_ctx)) {
  1642. vpe_err(dev, "Failed to alloc vb2 context\n");
  1643. ret = PTR_ERR(dev->alloc_ctx);
  1644. goto v4l2_dev_unreg;
  1645. }
  1646. dev->m2m_dev = v4l2_m2m_init(&m2m_ops);
  1647. if (IS_ERR(dev->m2m_dev)) {
  1648. vpe_err(dev, "Failed to init mem2mem device\n");
  1649. ret = PTR_ERR(dev->m2m_dev);
  1650. goto rel_ctx;
  1651. }
  1652. pm_runtime_enable(&pdev->dev);
  1653. ret = vpe_runtime_get(pdev);
  1654. if (ret)
  1655. goto rel_m2m;
  1656. /* Perform clk enable followed by reset */
  1657. vpe_set_clock_enable(dev, 1);
  1658. vpe_top_reset(dev);
  1659. func = read_field_reg(dev, VPE_PID, VPE_PID_FUNC_MASK,
  1660. VPE_PID_FUNC_SHIFT);
  1661. vpe_dbg(dev, "VPE PID function %x\n", func);
  1662. vpe_top_vpdma_reset(dev);
  1663. dev->vpdma = vpdma_create(pdev);
  1664. if (IS_ERR(dev->vpdma))
  1665. goto runtime_put;
  1666. vfd = &dev->vfd;
  1667. *vfd = vpe_videodev;
  1668. vfd->lock = &dev->dev_mutex;
  1669. vfd->v4l2_dev = &dev->v4l2_dev;
  1670. ret = video_register_device(vfd, VFL_TYPE_GRABBER, 0);
  1671. if (ret) {
  1672. vpe_err(dev, "Failed to register video device\n");
  1673. goto runtime_put;
  1674. }
  1675. video_set_drvdata(vfd, dev);
  1676. snprintf(vfd->name, sizeof(vfd->name), "%s", vpe_videodev.name);
  1677. dev_info(dev->v4l2_dev.dev, "Device registered as /dev/video%d\n",
  1678. vfd->num);
  1679. return 0;
  1680. runtime_put:
  1681. vpe_runtime_put(pdev);
  1682. rel_m2m:
  1683. pm_runtime_disable(&pdev->dev);
  1684. v4l2_m2m_release(dev->m2m_dev);
  1685. rel_ctx:
  1686. vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
  1687. v4l2_dev_unreg:
  1688. v4l2_device_unregister(&dev->v4l2_dev);
  1689. return ret;
  1690. }
  1691. static int vpe_remove(struct platform_device *pdev)
  1692. {
  1693. struct vpe_dev *dev =
  1694. (struct vpe_dev *) platform_get_drvdata(pdev);
  1695. v4l2_info(&dev->v4l2_dev, "Removing " VPE_MODULE_NAME);
  1696. v4l2_m2m_release(dev->m2m_dev);
  1697. video_unregister_device(&dev->vfd);
  1698. v4l2_device_unregister(&dev->v4l2_dev);
  1699. vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
  1700. vpe_set_clock_enable(dev, 0);
  1701. vpe_runtime_put(pdev);
  1702. pm_runtime_disable(&pdev->dev);
  1703. return 0;
  1704. }
  1705. #if defined(CONFIG_OF)
  1706. static const struct of_device_id vpe_of_match[] = {
  1707. {
  1708. .compatible = "ti,vpe",
  1709. },
  1710. {},
  1711. };
  1712. #else
  1713. #define vpe_of_match NULL
  1714. #endif
  1715. static struct platform_driver vpe_pdrv = {
  1716. .probe = vpe_probe,
  1717. .remove = vpe_remove,
  1718. .driver = {
  1719. .name = VPE_MODULE_NAME,
  1720. .owner = THIS_MODULE,
  1721. .of_match_table = vpe_of_match,
  1722. },
  1723. };
  1724. static void __exit vpe_exit(void)
  1725. {
  1726. platform_driver_unregister(&vpe_pdrv);
  1727. }
  1728. static int __init vpe_init(void)
  1729. {
  1730. return platform_driver_register(&vpe_pdrv);
  1731. }
  1732. module_init(vpe_init);
  1733. module_exit(vpe_exit);
  1734. MODULE_DESCRIPTION("TI VPE driver");
  1735. MODULE_AUTHOR("Dale Farnsworth, <dale@farnsworth.org>");
  1736. MODULE_LICENSE("GPL");