vpe.c 42 KB

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