mx2_emmaprp.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009
  1. /*
  2. * Support eMMa-PrP through mem2mem framework.
  3. *
  4. * eMMa-PrP is a piece of HW that allows fetching buffers
  5. * from one memory location and do several operations on
  6. * them such as scaling or format conversion giving, as a result
  7. * a new processed buffer in another memory location.
  8. *
  9. * Based on mem2mem_testdev.c by Pawel Osciak.
  10. *
  11. * Copyright (c) 2011 Vista Silicon S.L.
  12. * Javier Martin <javier.martin@vista-silicon.com>
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by the
  16. * Free Software Foundation; either version 2 of the
  17. * License, or (at your option) any later version
  18. */
  19. #include <linux/module.h>
  20. #include <linux/clk.h>
  21. #include <linux/slab.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/io.h>
  24. #include <linux/platform_device.h>
  25. #include <media/v4l2-mem2mem.h>
  26. #include <media/v4l2-device.h>
  27. #include <media/v4l2-ioctl.h>
  28. #include <media/videobuf2-dma-contig.h>
  29. #include <asm/sizes.h>
  30. #define EMMAPRP_MODULE_NAME "mem2mem-emmaprp"
  31. MODULE_DESCRIPTION("Mem-to-mem device which supports eMMa-PrP present in mx2 SoCs");
  32. MODULE_AUTHOR("Javier Martin <javier.martin@vista-silicon.com");
  33. MODULE_LICENSE("GPL");
  34. MODULE_VERSION("0.0.1");
  35. static bool debug;
  36. module_param(debug, bool, 0644);
  37. #define MIN_W 32
  38. #define MIN_H 32
  39. #define MAX_W 2040
  40. #define MAX_H 2046
  41. #define S_ALIGN 1 /* multiple of 2 */
  42. #define W_ALIGN_YUV420 3 /* multiple of 8 */
  43. #define W_ALIGN_OTHERS 2 /* multiple of 4 */
  44. #define H_ALIGN 1 /* multiple of 2 */
  45. /* Flags that indicate a format can be used for capture/output */
  46. #define MEM2MEM_CAPTURE (1 << 0)
  47. #define MEM2MEM_OUTPUT (1 << 1)
  48. #define MEM2MEM_NAME "m2m-emmaprp"
  49. /* In bytes, per queue */
  50. #define MEM2MEM_VID_MEM_LIMIT SZ_16M
  51. #define dprintk(dev, fmt, arg...) \
  52. v4l2_dbg(1, debug, &dev->v4l2_dev, "%s: " fmt, __func__, ## arg)
  53. /* EMMA PrP */
  54. #define PRP_CNTL 0x00
  55. #define PRP_INTR_CNTL 0x04
  56. #define PRP_INTRSTATUS 0x08
  57. #define PRP_SOURCE_Y_PTR 0x0c
  58. #define PRP_SOURCE_CB_PTR 0x10
  59. #define PRP_SOURCE_CR_PTR 0x14
  60. #define PRP_DEST_RGB1_PTR 0x18
  61. #define PRP_DEST_RGB2_PTR 0x1c
  62. #define PRP_DEST_Y_PTR 0x20
  63. #define PRP_DEST_CB_PTR 0x24
  64. #define PRP_DEST_CR_PTR 0x28
  65. #define PRP_SRC_FRAME_SIZE 0x2c
  66. #define PRP_DEST_CH1_LINE_STRIDE 0x30
  67. #define PRP_SRC_PIXEL_FORMAT_CNTL 0x34
  68. #define PRP_CH1_PIXEL_FORMAT_CNTL 0x38
  69. #define PRP_CH1_OUT_IMAGE_SIZE 0x3c
  70. #define PRP_CH2_OUT_IMAGE_SIZE 0x40
  71. #define PRP_SRC_LINE_STRIDE 0x44
  72. #define PRP_CSC_COEF_012 0x48
  73. #define PRP_CSC_COEF_345 0x4c
  74. #define PRP_CSC_COEF_678 0x50
  75. #define PRP_CH1_RZ_HORI_COEF1 0x54
  76. #define PRP_CH1_RZ_HORI_COEF2 0x58
  77. #define PRP_CH1_RZ_HORI_VALID 0x5c
  78. #define PRP_CH1_RZ_VERT_COEF1 0x60
  79. #define PRP_CH1_RZ_VERT_COEF2 0x64
  80. #define PRP_CH1_RZ_VERT_VALID 0x68
  81. #define PRP_CH2_RZ_HORI_COEF1 0x6c
  82. #define PRP_CH2_RZ_HORI_COEF2 0x70
  83. #define PRP_CH2_RZ_HORI_VALID 0x74
  84. #define PRP_CH2_RZ_VERT_COEF1 0x78
  85. #define PRP_CH2_RZ_VERT_COEF2 0x7c
  86. #define PRP_CH2_RZ_VERT_VALID 0x80
  87. #define PRP_CNTL_CH1EN (1 << 0)
  88. #define PRP_CNTL_CH2EN (1 << 1)
  89. #define PRP_CNTL_CSIEN (1 << 2)
  90. #define PRP_CNTL_DATA_IN_YUV420 (0 << 3)
  91. #define PRP_CNTL_DATA_IN_YUV422 (1 << 3)
  92. #define PRP_CNTL_DATA_IN_RGB16 (2 << 3)
  93. #define PRP_CNTL_DATA_IN_RGB32 (3 << 3)
  94. #define PRP_CNTL_CH1_OUT_RGB8 (0 << 5)
  95. #define PRP_CNTL_CH1_OUT_RGB16 (1 << 5)
  96. #define PRP_CNTL_CH1_OUT_RGB32 (2 << 5)
  97. #define PRP_CNTL_CH1_OUT_YUV422 (3 << 5)
  98. #define PRP_CNTL_CH2_OUT_YUV420 (0 << 7)
  99. #define PRP_CNTL_CH2_OUT_YUV422 (1 << 7)
  100. #define PRP_CNTL_CH2_OUT_YUV444 (2 << 7)
  101. #define PRP_CNTL_CH1_LEN (1 << 9)
  102. #define PRP_CNTL_CH2_LEN (1 << 10)
  103. #define PRP_CNTL_SKIP_FRAME (1 << 11)
  104. #define PRP_CNTL_SWRST (1 << 12)
  105. #define PRP_CNTL_CLKEN (1 << 13)
  106. #define PRP_CNTL_WEN (1 << 14)
  107. #define PRP_CNTL_CH1BYP (1 << 15)
  108. #define PRP_CNTL_IN_TSKIP(x) ((x) << 16)
  109. #define PRP_CNTL_CH1_TSKIP(x) ((x) << 19)
  110. #define PRP_CNTL_CH2_TSKIP(x) ((x) << 22)
  111. #define PRP_CNTL_INPUT_FIFO_LEVEL(x) ((x) << 25)
  112. #define PRP_CNTL_RZ_FIFO_LEVEL(x) ((x) << 27)
  113. #define PRP_CNTL_CH2B1EN (1 << 29)
  114. #define PRP_CNTL_CH2B2EN (1 << 30)
  115. #define PRP_CNTL_CH2FEN (1 << 31)
  116. #define PRP_SIZE_HEIGHT(x) (x)
  117. #define PRP_SIZE_WIDTH(x) ((x) << 16)
  118. /* IRQ Enable and status register */
  119. #define PRP_INTR_RDERR (1 << 0)
  120. #define PRP_INTR_CH1WERR (1 << 1)
  121. #define PRP_INTR_CH2WERR (1 << 2)
  122. #define PRP_INTR_CH1FC (1 << 3)
  123. #define PRP_INTR_CH2FC (1 << 5)
  124. #define PRP_INTR_LBOVF (1 << 7)
  125. #define PRP_INTR_CH2OVF (1 << 8)
  126. #define PRP_INTR_ST_RDERR (1 << 0)
  127. #define PRP_INTR_ST_CH1WERR (1 << 1)
  128. #define PRP_INTR_ST_CH2WERR (1 << 2)
  129. #define PRP_INTR_ST_CH2B2CI (1 << 3)
  130. #define PRP_INTR_ST_CH2B1CI (1 << 4)
  131. #define PRP_INTR_ST_CH1B2CI (1 << 5)
  132. #define PRP_INTR_ST_CH1B1CI (1 << 6)
  133. #define PRP_INTR_ST_LBOVF (1 << 7)
  134. #define PRP_INTR_ST_CH2OVF (1 << 8)
  135. struct emmaprp_fmt {
  136. char *name;
  137. u32 fourcc;
  138. /* Types the format can be used for */
  139. u32 types;
  140. };
  141. static struct emmaprp_fmt formats[] = {
  142. {
  143. .name = "YUV 4:2:0 Planar",
  144. .fourcc = V4L2_PIX_FMT_YUV420,
  145. .types = MEM2MEM_CAPTURE,
  146. },
  147. {
  148. .name = "4:2:2, packed, YUYV",
  149. .fourcc = V4L2_PIX_FMT_YUYV,
  150. .types = MEM2MEM_OUTPUT,
  151. },
  152. };
  153. /* Per-queue, driver-specific private data */
  154. struct emmaprp_q_data {
  155. unsigned int width;
  156. unsigned int height;
  157. unsigned int sizeimage;
  158. struct emmaprp_fmt *fmt;
  159. };
  160. enum {
  161. V4L2_M2M_SRC = 0,
  162. V4L2_M2M_DST = 1,
  163. };
  164. #define NUM_FORMATS ARRAY_SIZE(formats)
  165. static struct emmaprp_fmt *find_format(struct v4l2_format *f)
  166. {
  167. struct emmaprp_fmt *fmt;
  168. unsigned int k;
  169. for (k = 0; k < NUM_FORMATS; k++) {
  170. fmt = &formats[k];
  171. if (fmt->fourcc == f->fmt.pix.pixelformat)
  172. break;
  173. }
  174. if (k == NUM_FORMATS)
  175. return NULL;
  176. return &formats[k];
  177. }
  178. struct emmaprp_dev {
  179. struct v4l2_device v4l2_dev;
  180. struct video_device *vfd;
  181. struct mutex dev_mutex;
  182. spinlock_t irqlock;
  183. int irq_emma;
  184. void __iomem *base_emma;
  185. struct clk *clk_emma;
  186. struct resource *res_emma;
  187. struct v4l2_m2m_dev *m2m_dev;
  188. struct vb2_alloc_ctx *alloc_ctx;
  189. };
  190. struct emmaprp_ctx {
  191. struct emmaprp_dev *dev;
  192. /* Abort requested by m2m */
  193. int aborting;
  194. struct emmaprp_q_data q_data[2];
  195. struct v4l2_m2m_ctx *m2m_ctx;
  196. };
  197. static struct emmaprp_q_data *get_q_data(struct emmaprp_ctx *ctx,
  198. enum v4l2_buf_type type)
  199. {
  200. switch (type) {
  201. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  202. return &(ctx->q_data[V4L2_M2M_SRC]);
  203. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  204. return &(ctx->q_data[V4L2_M2M_DST]);
  205. default:
  206. BUG();
  207. }
  208. return NULL;
  209. }
  210. /*
  211. * mem2mem callbacks
  212. */
  213. static void emmaprp_job_abort(void *priv)
  214. {
  215. struct emmaprp_ctx *ctx = priv;
  216. struct emmaprp_dev *pcdev = ctx->dev;
  217. ctx->aborting = 1;
  218. dprintk(pcdev, "Aborting task\n");
  219. v4l2_m2m_job_finish(pcdev->m2m_dev, ctx->m2m_ctx);
  220. }
  221. static void emmaprp_lock(void *priv)
  222. {
  223. struct emmaprp_ctx *ctx = priv;
  224. struct emmaprp_dev *pcdev = ctx->dev;
  225. mutex_lock(&pcdev->dev_mutex);
  226. }
  227. static void emmaprp_unlock(void *priv)
  228. {
  229. struct emmaprp_ctx *ctx = priv;
  230. struct emmaprp_dev *pcdev = ctx->dev;
  231. mutex_unlock(&pcdev->dev_mutex);
  232. }
  233. static inline void emmaprp_dump_regs(struct emmaprp_dev *pcdev)
  234. {
  235. dprintk(pcdev,
  236. "eMMa-PrP Registers:\n"
  237. " SOURCE_Y_PTR = 0x%08X\n"
  238. " SRC_FRAME_SIZE = 0x%08X\n"
  239. " DEST_Y_PTR = 0x%08X\n"
  240. " DEST_CR_PTR = 0x%08X\n"
  241. " DEST_CB_PTR = 0x%08X\n"
  242. " CH2_OUT_IMAGE_SIZE = 0x%08X\n"
  243. " CNTL = 0x%08X\n",
  244. readl(pcdev->base_emma + PRP_SOURCE_Y_PTR),
  245. readl(pcdev->base_emma + PRP_SRC_FRAME_SIZE),
  246. readl(pcdev->base_emma + PRP_DEST_Y_PTR),
  247. readl(pcdev->base_emma + PRP_DEST_CR_PTR),
  248. readl(pcdev->base_emma + PRP_DEST_CB_PTR),
  249. readl(pcdev->base_emma + PRP_CH2_OUT_IMAGE_SIZE),
  250. readl(pcdev->base_emma + PRP_CNTL));
  251. }
  252. static void emmaprp_device_run(void *priv)
  253. {
  254. struct emmaprp_ctx *ctx = priv;
  255. struct emmaprp_q_data *s_q_data, *d_q_data;
  256. struct vb2_buffer *src_buf, *dst_buf;
  257. struct emmaprp_dev *pcdev = ctx->dev;
  258. unsigned int s_width, s_height;
  259. unsigned int d_width, d_height;
  260. unsigned int d_size;
  261. dma_addr_t p_in, p_out;
  262. u32 tmp;
  263. src_buf = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
  264. dst_buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
  265. s_q_data = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
  266. s_width = s_q_data->width;
  267. s_height = s_q_data->height;
  268. d_q_data = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
  269. d_width = d_q_data->width;
  270. d_height = d_q_data->height;
  271. d_size = d_width * d_height;
  272. p_in = vb2_dma_contig_plane_dma_addr(src_buf, 0);
  273. p_out = vb2_dma_contig_plane_dma_addr(dst_buf, 0);
  274. if (!p_in || !p_out) {
  275. v4l2_err(&pcdev->v4l2_dev,
  276. "Acquiring kernel pointers to buffers failed\n");
  277. return;
  278. }
  279. /* Input frame parameters */
  280. writel(p_in, pcdev->base_emma + PRP_SOURCE_Y_PTR);
  281. writel(PRP_SIZE_WIDTH(s_width) | PRP_SIZE_HEIGHT(s_height),
  282. pcdev->base_emma + PRP_SRC_FRAME_SIZE);
  283. /* Output frame parameters */
  284. writel(p_out, pcdev->base_emma + PRP_DEST_Y_PTR);
  285. writel(p_out + d_size, pcdev->base_emma + PRP_DEST_CB_PTR);
  286. writel(p_out + d_size + (d_size >> 2),
  287. pcdev->base_emma + PRP_DEST_CR_PTR);
  288. writel(PRP_SIZE_WIDTH(d_width) | PRP_SIZE_HEIGHT(d_height),
  289. pcdev->base_emma + PRP_CH2_OUT_IMAGE_SIZE);
  290. /* IRQ configuration */
  291. tmp = readl(pcdev->base_emma + PRP_INTR_CNTL);
  292. writel(tmp | PRP_INTR_RDERR |
  293. PRP_INTR_CH2WERR |
  294. PRP_INTR_CH2FC,
  295. pcdev->base_emma + PRP_INTR_CNTL);
  296. emmaprp_dump_regs(pcdev);
  297. /* Enable transfer */
  298. tmp = readl(pcdev->base_emma + PRP_CNTL);
  299. writel(tmp | PRP_CNTL_CH2_OUT_YUV420 |
  300. PRP_CNTL_DATA_IN_YUV422 |
  301. PRP_CNTL_CH2EN,
  302. pcdev->base_emma + PRP_CNTL);
  303. }
  304. static irqreturn_t emmaprp_irq(int irq_emma, void *data)
  305. {
  306. struct emmaprp_dev *pcdev = data;
  307. struct emmaprp_ctx *curr_ctx;
  308. struct vb2_buffer *src_vb, *dst_vb;
  309. unsigned long flags;
  310. u32 irqst;
  311. /* Check irq flags and clear irq */
  312. irqst = readl(pcdev->base_emma + PRP_INTRSTATUS);
  313. writel(irqst, pcdev->base_emma + PRP_INTRSTATUS);
  314. dprintk(pcdev, "irqst = 0x%08x\n", irqst);
  315. curr_ctx = v4l2_m2m_get_curr_priv(pcdev->m2m_dev);
  316. if (curr_ctx == NULL) {
  317. pr_err("Instance released before the end of transaction\n");
  318. return IRQ_HANDLED;
  319. }
  320. if (!curr_ctx->aborting) {
  321. if ((irqst & PRP_INTR_ST_RDERR) ||
  322. (irqst & PRP_INTR_ST_CH2WERR)) {
  323. pr_err("PrP bus error ocurred, this transfer is probably corrupted\n");
  324. writel(PRP_CNTL_SWRST, pcdev->base_emma + PRP_CNTL);
  325. } else if (irqst & PRP_INTR_ST_CH2B1CI) { /* buffer ready */
  326. src_vb = v4l2_m2m_src_buf_remove(curr_ctx->m2m_ctx);
  327. dst_vb = v4l2_m2m_dst_buf_remove(curr_ctx->m2m_ctx);
  328. spin_lock_irqsave(&pcdev->irqlock, flags);
  329. v4l2_m2m_buf_done(src_vb, VB2_BUF_STATE_DONE);
  330. v4l2_m2m_buf_done(dst_vb, VB2_BUF_STATE_DONE);
  331. spin_unlock_irqrestore(&pcdev->irqlock, flags);
  332. }
  333. }
  334. v4l2_m2m_job_finish(pcdev->m2m_dev, curr_ctx->m2m_ctx);
  335. return IRQ_HANDLED;
  336. }
  337. /*
  338. * video ioctls
  339. */
  340. static int vidioc_querycap(struct file *file, void *priv,
  341. struct v4l2_capability *cap)
  342. {
  343. strncpy(cap->driver, MEM2MEM_NAME, sizeof(cap->driver) - 1);
  344. strncpy(cap->card, MEM2MEM_NAME, sizeof(cap->card) - 1);
  345. cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OUTPUT
  346. | V4L2_CAP_STREAMING;
  347. return 0;
  348. }
  349. static int enum_fmt(struct v4l2_fmtdesc *f, u32 type)
  350. {
  351. int i, num;
  352. struct emmaprp_fmt *fmt;
  353. num = 0;
  354. for (i = 0; i < NUM_FORMATS; ++i) {
  355. if (formats[i].types & type) {
  356. /* index-th format of type type found ? */
  357. if (num == f->index)
  358. break;
  359. /* Correct type but haven't reached our index yet,
  360. * just increment per-type index */
  361. ++num;
  362. }
  363. }
  364. if (i < NUM_FORMATS) {
  365. /* Format found */
  366. fmt = &formats[i];
  367. strlcpy(f->description, fmt->name, sizeof(f->description) - 1);
  368. f->pixelformat = fmt->fourcc;
  369. return 0;
  370. }
  371. /* Format not found */
  372. return -EINVAL;
  373. }
  374. static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  375. struct v4l2_fmtdesc *f)
  376. {
  377. return enum_fmt(f, MEM2MEM_CAPTURE);
  378. }
  379. static int vidioc_enum_fmt_vid_out(struct file *file, void *priv,
  380. struct v4l2_fmtdesc *f)
  381. {
  382. return enum_fmt(f, MEM2MEM_OUTPUT);
  383. }
  384. static int vidioc_g_fmt(struct emmaprp_ctx *ctx, struct v4l2_format *f)
  385. {
  386. struct vb2_queue *vq;
  387. struct emmaprp_q_data *q_data;
  388. vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
  389. if (!vq)
  390. return -EINVAL;
  391. q_data = get_q_data(ctx, f->type);
  392. f->fmt.pix.width = q_data->width;
  393. f->fmt.pix.height = q_data->height;
  394. f->fmt.pix.field = V4L2_FIELD_NONE;
  395. f->fmt.pix.pixelformat = q_data->fmt->fourcc;
  396. if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_YUV420)
  397. f->fmt.pix.bytesperline = q_data->width * 3 / 2;
  398. else /* YUYV */
  399. f->fmt.pix.bytesperline = q_data->width * 2;
  400. f->fmt.pix.sizeimage = q_data->sizeimage;
  401. return 0;
  402. }
  403. static int vidioc_g_fmt_vid_out(struct file *file, void *priv,
  404. struct v4l2_format *f)
  405. {
  406. return vidioc_g_fmt(priv, f);
  407. }
  408. static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  409. struct v4l2_format *f)
  410. {
  411. return vidioc_g_fmt(priv, f);
  412. }
  413. static int vidioc_try_fmt(struct v4l2_format *f)
  414. {
  415. enum v4l2_field field;
  416. if (!find_format(f))
  417. return -EINVAL;
  418. field = f->fmt.pix.field;
  419. if (field == V4L2_FIELD_ANY)
  420. field = V4L2_FIELD_NONE;
  421. else if (V4L2_FIELD_NONE != field)
  422. return -EINVAL;
  423. /* V4L2 specification suggests the driver corrects the format struct
  424. * if any of the dimensions is unsupported */
  425. f->fmt.pix.field = field;
  426. if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_YUV420) {
  427. v4l_bound_align_image(&f->fmt.pix.width, MIN_W, MAX_W,
  428. W_ALIGN_YUV420, &f->fmt.pix.height,
  429. MIN_H, MAX_H, H_ALIGN, S_ALIGN);
  430. f->fmt.pix.bytesperline = f->fmt.pix.width * 3 / 2;
  431. } else {
  432. v4l_bound_align_image(&f->fmt.pix.width, MIN_W, MAX_W,
  433. W_ALIGN_OTHERS, &f->fmt.pix.height,
  434. MIN_H, MAX_H, H_ALIGN, S_ALIGN);
  435. f->fmt.pix.bytesperline = f->fmt.pix.width * 2;
  436. }
  437. f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
  438. return 0;
  439. }
  440. static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
  441. struct v4l2_format *f)
  442. {
  443. struct emmaprp_fmt *fmt;
  444. struct emmaprp_ctx *ctx = priv;
  445. fmt = find_format(f);
  446. if (!fmt || !(fmt->types & MEM2MEM_CAPTURE)) {
  447. v4l2_err(&ctx->dev->v4l2_dev,
  448. "Fourcc format (0x%08x) invalid.\n",
  449. f->fmt.pix.pixelformat);
  450. return -EINVAL;
  451. }
  452. return vidioc_try_fmt(f);
  453. }
  454. static int vidioc_try_fmt_vid_out(struct file *file, void *priv,
  455. struct v4l2_format *f)
  456. {
  457. struct emmaprp_fmt *fmt;
  458. struct emmaprp_ctx *ctx = priv;
  459. fmt = find_format(f);
  460. if (!fmt || !(fmt->types & MEM2MEM_OUTPUT)) {
  461. v4l2_err(&ctx->dev->v4l2_dev,
  462. "Fourcc format (0x%08x) invalid.\n",
  463. f->fmt.pix.pixelformat);
  464. return -EINVAL;
  465. }
  466. return vidioc_try_fmt(f);
  467. }
  468. static int vidioc_s_fmt(struct emmaprp_ctx *ctx, struct v4l2_format *f)
  469. {
  470. struct emmaprp_q_data *q_data;
  471. struct vb2_queue *vq;
  472. int ret;
  473. vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
  474. if (!vq)
  475. return -EINVAL;
  476. q_data = get_q_data(ctx, f->type);
  477. if (!q_data)
  478. return -EINVAL;
  479. if (vb2_is_busy(vq)) {
  480. v4l2_err(&ctx->dev->v4l2_dev, "%s queue busy\n", __func__);
  481. return -EBUSY;
  482. }
  483. ret = vidioc_try_fmt(f);
  484. if (ret)
  485. return ret;
  486. q_data->fmt = find_format(f);
  487. q_data->width = f->fmt.pix.width;
  488. q_data->height = f->fmt.pix.height;
  489. if (q_data->fmt->fourcc == V4L2_PIX_FMT_YUV420)
  490. q_data->sizeimage = q_data->width * q_data->height * 3 / 2;
  491. else /* YUYV */
  492. q_data->sizeimage = q_data->width * q_data->height * 2;
  493. dprintk(ctx->dev,
  494. "Setting format for type %d, wxh: %dx%d, fmt: %d\n",
  495. f->type, q_data->width, q_data->height, q_data->fmt->fourcc);
  496. return 0;
  497. }
  498. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  499. struct v4l2_format *f)
  500. {
  501. int ret;
  502. ret = vidioc_try_fmt_vid_cap(file, priv, f);
  503. if (ret)
  504. return ret;
  505. return vidioc_s_fmt(priv, f);
  506. }
  507. static int vidioc_s_fmt_vid_out(struct file *file, void *priv,
  508. struct v4l2_format *f)
  509. {
  510. int ret;
  511. ret = vidioc_try_fmt_vid_out(file, priv, f);
  512. if (ret)
  513. return ret;
  514. return vidioc_s_fmt(priv, f);
  515. }
  516. static int vidioc_reqbufs(struct file *file, void *priv,
  517. struct v4l2_requestbuffers *reqbufs)
  518. {
  519. struct emmaprp_ctx *ctx = priv;
  520. return v4l2_m2m_reqbufs(file, ctx->m2m_ctx, reqbufs);
  521. }
  522. static int vidioc_querybuf(struct file *file, void *priv,
  523. struct v4l2_buffer *buf)
  524. {
  525. struct emmaprp_ctx *ctx = priv;
  526. return v4l2_m2m_querybuf(file, ctx->m2m_ctx, buf);
  527. }
  528. static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
  529. {
  530. struct emmaprp_ctx *ctx = priv;
  531. return v4l2_m2m_qbuf(file, ctx->m2m_ctx, buf);
  532. }
  533. static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
  534. {
  535. struct emmaprp_ctx *ctx = priv;
  536. return v4l2_m2m_dqbuf(file, ctx->m2m_ctx, buf);
  537. }
  538. static int vidioc_streamon(struct file *file, void *priv,
  539. enum v4l2_buf_type type)
  540. {
  541. struct emmaprp_ctx *ctx = priv;
  542. return v4l2_m2m_streamon(file, ctx->m2m_ctx, type);
  543. }
  544. static int vidioc_streamoff(struct file *file, void *priv,
  545. enum v4l2_buf_type type)
  546. {
  547. struct emmaprp_ctx *ctx = priv;
  548. return v4l2_m2m_streamoff(file, ctx->m2m_ctx, type);
  549. }
  550. static const struct v4l2_ioctl_ops emmaprp_ioctl_ops = {
  551. .vidioc_querycap = vidioc_querycap,
  552. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  553. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  554. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  555. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  556. .vidioc_enum_fmt_vid_out = vidioc_enum_fmt_vid_out,
  557. .vidioc_g_fmt_vid_out = vidioc_g_fmt_vid_out,
  558. .vidioc_try_fmt_vid_out = vidioc_try_fmt_vid_out,
  559. .vidioc_s_fmt_vid_out = vidioc_s_fmt_vid_out,
  560. .vidioc_reqbufs = vidioc_reqbufs,
  561. .vidioc_querybuf = vidioc_querybuf,
  562. .vidioc_qbuf = vidioc_qbuf,
  563. .vidioc_dqbuf = vidioc_dqbuf,
  564. .vidioc_streamon = vidioc_streamon,
  565. .vidioc_streamoff = vidioc_streamoff,
  566. };
  567. /*
  568. * Queue operations
  569. */
  570. static int emmaprp_queue_setup(struct vb2_queue *vq,
  571. const struct v4l2_format *fmt,
  572. unsigned int *nbuffers, unsigned int *nplanes,
  573. unsigned int sizes[], void *alloc_ctxs[])
  574. {
  575. struct emmaprp_ctx *ctx = vb2_get_drv_priv(vq);
  576. struct emmaprp_q_data *q_data;
  577. unsigned int size, count = *nbuffers;
  578. q_data = get_q_data(ctx, vq->type);
  579. if (q_data->fmt->fourcc == V4L2_PIX_FMT_YUV420)
  580. size = q_data->width * q_data->height * 3 / 2;
  581. else
  582. size = q_data->width * q_data->height * 2;
  583. while (size * count > MEM2MEM_VID_MEM_LIMIT)
  584. (count)--;
  585. *nplanes = 1;
  586. *nbuffers = count;
  587. sizes[0] = size;
  588. alloc_ctxs[0] = ctx->dev->alloc_ctx;
  589. dprintk(ctx->dev, "get %d buffer(s) of size %d each.\n", count, size);
  590. return 0;
  591. }
  592. static int emmaprp_buf_prepare(struct vb2_buffer *vb)
  593. {
  594. struct emmaprp_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  595. struct emmaprp_q_data *q_data;
  596. dprintk(ctx->dev, "type: %d\n", vb->vb2_queue->type);
  597. q_data = get_q_data(ctx, vb->vb2_queue->type);
  598. if (vb2_plane_size(vb, 0) < q_data->sizeimage) {
  599. dprintk(ctx->dev, "%s data will not fit into plane"
  600. "(%lu < %lu)\n", __func__,
  601. vb2_plane_size(vb, 0),
  602. (long)q_data->sizeimage);
  603. return -EINVAL;
  604. }
  605. vb2_set_plane_payload(vb, 0, q_data->sizeimage);
  606. return 0;
  607. }
  608. static void emmaprp_buf_queue(struct vb2_buffer *vb)
  609. {
  610. struct emmaprp_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  611. v4l2_m2m_buf_queue(ctx->m2m_ctx, vb);
  612. }
  613. static struct vb2_ops emmaprp_qops = {
  614. .queue_setup = emmaprp_queue_setup,
  615. .buf_prepare = emmaprp_buf_prepare,
  616. .buf_queue = emmaprp_buf_queue,
  617. };
  618. static int queue_init(void *priv, struct vb2_queue *src_vq,
  619. struct vb2_queue *dst_vq)
  620. {
  621. struct emmaprp_ctx *ctx = priv;
  622. int ret;
  623. memset(src_vq, 0, sizeof(*src_vq));
  624. src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  625. src_vq->io_modes = VB2_MMAP;
  626. src_vq->drv_priv = ctx;
  627. src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
  628. src_vq->ops = &emmaprp_qops;
  629. src_vq->mem_ops = &vb2_dma_contig_memops;
  630. ret = vb2_queue_init(src_vq);
  631. if (ret)
  632. return ret;
  633. memset(dst_vq, 0, sizeof(*dst_vq));
  634. dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  635. dst_vq->io_modes = VB2_MMAP;
  636. dst_vq->drv_priv = ctx;
  637. dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
  638. dst_vq->ops = &emmaprp_qops;
  639. dst_vq->mem_ops = &vb2_dma_contig_memops;
  640. return vb2_queue_init(dst_vq);
  641. }
  642. /*
  643. * File operations
  644. */
  645. static int emmaprp_open(struct file *file)
  646. {
  647. struct emmaprp_dev *pcdev = video_drvdata(file);
  648. struct emmaprp_ctx *ctx;
  649. ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
  650. if (!ctx)
  651. return -ENOMEM;
  652. file->private_data = ctx;
  653. ctx->dev = pcdev;
  654. ctx->m2m_ctx = v4l2_m2m_ctx_init(pcdev->m2m_dev, ctx, &queue_init);
  655. if (IS_ERR(ctx->m2m_ctx)) {
  656. int ret = PTR_ERR(ctx->m2m_ctx);
  657. kfree(ctx);
  658. return ret;
  659. }
  660. clk_enable(pcdev->clk_emma);
  661. ctx->q_data[V4L2_M2M_SRC].fmt = &formats[1];
  662. ctx->q_data[V4L2_M2M_DST].fmt = &formats[0];
  663. dprintk(pcdev, "Created instance %p, m2m_ctx: %p\n", ctx, ctx->m2m_ctx);
  664. return 0;
  665. }
  666. static int emmaprp_release(struct file *file)
  667. {
  668. struct emmaprp_dev *pcdev = video_drvdata(file);
  669. struct emmaprp_ctx *ctx = file->private_data;
  670. dprintk(pcdev, "Releasing instance %p\n", ctx);
  671. clk_disable(pcdev->clk_emma);
  672. v4l2_m2m_ctx_release(ctx->m2m_ctx);
  673. kfree(ctx);
  674. return 0;
  675. }
  676. static unsigned int emmaprp_poll(struct file *file,
  677. struct poll_table_struct *wait)
  678. {
  679. struct emmaprp_ctx *ctx = file->private_data;
  680. return v4l2_m2m_poll(file, ctx->m2m_ctx, wait);
  681. }
  682. static int emmaprp_mmap(struct file *file, struct vm_area_struct *vma)
  683. {
  684. struct emmaprp_ctx *ctx = file->private_data;
  685. return v4l2_m2m_mmap(file, ctx->m2m_ctx, vma);
  686. }
  687. static const struct v4l2_file_operations emmaprp_fops = {
  688. .owner = THIS_MODULE,
  689. .open = emmaprp_open,
  690. .release = emmaprp_release,
  691. .poll = emmaprp_poll,
  692. .unlocked_ioctl = video_ioctl2,
  693. .mmap = emmaprp_mmap,
  694. };
  695. static struct video_device emmaprp_videodev = {
  696. .name = MEM2MEM_NAME,
  697. .fops = &emmaprp_fops,
  698. .ioctl_ops = &emmaprp_ioctl_ops,
  699. .minor = -1,
  700. .release = video_device_release,
  701. };
  702. static struct v4l2_m2m_ops m2m_ops = {
  703. .device_run = emmaprp_device_run,
  704. .job_abort = emmaprp_job_abort,
  705. .lock = emmaprp_lock,
  706. .unlock = emmaprp_unlock,
  707. };
  708. static int emmaprp_probe(struct platform_device *pdev)
  709. {
  710. struct emmaprp_dev *pcdev;
  711. struct video_device *vfd;
  712. struct resource *res_emma;
  713. int irq_emma;
  714. int ret;
  715. pcdev = kzalloc(sizeof *pcdev, GFP_KERNEL);
  716. if (!pcdev)
  717. return -ENOMEM;
  718. spin_lock_init(&pcdev->irqlock);
  719. pcdev->clk_emma = clk_get(&pdev->dev, NULL);
  720. if (IS_ERR(pcdev->clk_emma)) {
  721. ret = PTR_ERR(pcdev->clk_emma);
  722. goto free_dev;
  723. }
  724. irq_emma = platform_get_irq(pdev, 0);
  725. res_emma = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  726. if (irq_emma < 0 || res_emma == NULL) {
  727. dev_err(&pdev->dev, "Missing platform resources data\n");
  728. ret = -ENODEV;
  729. goto free_clk;
  730. }
  731. ret = v4l2_device_register(&pdev->dev, &pcdev->v4l2_dev);
  732. if (ret)
  733. goto free_clk;
  734. mutex_init(&pcdev->dev_mutex);
  735. vfd = video_device_alloc();
  736. if (!vfd) {
  737. v4l2_err(&pcdev->v4l2_dev, "Failed to allocate video device\n");
  738. ret = -ENOMEM;
  739. goto unreg_dev;
  740. }
  741. *vfd = emmaprp_videodev;
  742. vfd->lock = &pcdev->dev_mutex;
  743. video_set_drvdata(vfd, pcdev);
  744. snprintf(vfd->name, sizeof(vfd->name), "%s", emmaprp_videodev.name);
  745. pcdev->vfd = vfd;
  746. v4l2_info(&pcdev->v4l2_dev, EMMAPRP_MODULE_NAME
  747. " Device registered as /dev/video%d\n", vfd->num);
  748. platform_set_drvdata(pdev, pcdev);
  749. if (devm_request_mem_region(&pdev->dev, res_emma->start,
  750. resource_size(res_emma), MEM2MEM_NAME) == NULL)
  751. goto rel_vdev;
  752. pcdev->base_emma = devm_ioremap(&pdev->dev, res_emma->start,
  753. resource_size(res_emma));
  754. if (!pcdev->base_emma)
  755. goto rel_vdev;
  756. pcdev->irq_emma = irq_emma;
  757. pcdev->res_emma = res_emma;
  758. if (devm_request_irq(&pdev->dev, pcdev->irq_emma, emmaprp_irq,
  759. 0, MEM2MEM_NAME, pcdev) < 0)
  760. goto rel_vdev;
  761. pcdev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
  762. if (IS_ERR(pcdev->alloc_ctx)) {
  763. v4l2_err(&pcdev->v4l2_dev, "Failed to alloc vb2 context\n");
  764. ret = PTR_ERR(pcdev->alloc_ctx);
  765. goto rel_vdev;
  766. }
  767. pcdev->m2m_dev = v4l2_m2m_init(&m2m_ops);
  768. if (IS_ERR(pcdev->m2m_dev)) {
  769. v4l2_err(&pcdev->v4l2_dev, "Failed to init mem2mem device\n");
  770. ret = PTR_ERR(pcdev->m2m_dev);
  771. goto rel_ctx;
  772. }
  773. ret = video_register_device(vfd, VFL_TYPE_GRABBER, 0);
  774. if (ret) {
  775. v4l2_err(&pcdev->v4l2_dev, "Failed to register video device\n");
  776. goto rel_m2m;
  777. }
  778. return 0;
  779. rel_m2m:
  780. v4l2_m2m_release(pcdev->m2m_dev);
  781. rel_ctx:
  782. vb2_dma_contig_cleanup_ctx(pcdev->alloc_ctx);
  783. rel_vdev:
  784. video_device_release(vfd);
  785. unreg_dev:
  786. v4l2_device_unregister(&pcdev->v4l2_dev);
  787. free_clk:
  788. clk_put(pcdev->clk_emma);
  789. free_dev:
  790. kfree(pcdev);
  791. return ret;
  792. }
  793. static int emmaprp_remove(struct platform_device *pdev)
  794. {
  795. struct emmaprp_dev *pcdev = platform_get_drvdata(pdev);
  796. v4l2_info(&pcdev->v4l2_dev, "Removing " EMMAPRP_MODULE_NAME);
  797. video_unregister_device(pcdev->vfd);
  798. v4l2_m2m_release(pcdev->m2m_dev);
  799. vb2_dma_contig_cleanup_ctx(pcdev->alloc_ctx);
  800. v4l2_device_unregister(&pcdev->v4l2_dev);
  801. clk_put(pcdev->clk_emma);
  802. kfree(pcdev);
  803. return 0;
  804. }
  805. static struct platform_driver emmaprp_pdrv = {
  806. .probe = emmaprp_probe,
  807. .remove = emmaprp_remove,
  808. .driver = {
  809. .name = MEM2MEM_NAME,
  810. .owner = THIS_MODULE,
  811. },
  812. };
  813. static void __exit emmaprp_exit(void)
  814. {
  815. platform_driver_unregister(&emmaprp_pdrv);
  816. }
  817. static int __init emmaprp_init(void)
  818. {
  819. return platform_driver_register(&emmaprp_pdrv);
  820. }
  821. module_init(emmaprp_init);
  822. module_exit(emmaprp_exit);