mx2_emmaprp.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011
  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 <linux/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. void __iomem *base_emma;
  184. struct clk *clk_emma_ahb, *clk_emma_ipg;
  185. struct v4l2_m2m_dev *m2m_dev;
  186. struct vb2_alloc_ctx *alloc_ctx;
  187. };
  188. struct emmaprp_ctx {
  189. struct emmaprp_dev *dev;
  190. /* Abort requested by m2m */
  191. int aborting;
  192. struct emmaprp_q_data q_data[2];
  193. struct v4l2_m2m_ctx *m2m_ctx;
  194. };
  195. static struct emmaprp_q_data *get_q_data(struct emmaprp_ctx *ctx,
  196. enum v4l2_buf_type type)
  197. {
  198. switch (type) {
  199. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  200. return &(ctx->q_data[V4L2_M2M_SRC]);
  201. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  202. return &(ctx->q_data[V4L2_M2M_DST]);
  203. default:
  204. BUG();
  205. }
  206. return NULL;
  207. }
  208. /*
  209. * mem2mem callbacks
  210. */
  211. static void emmaprp_job_abort(void *priv)
  212. {
  213. struct emmaprp_ctx *ctx = priv;
  214. struct emmaprp_dev *pcdev = ctx->dev;
  215. ctx->aborting = 1;
  216. dprintk(pcdev, "Aborting task\n");
  217. v4l2_m2m_job_finish(pcdev->m2m_dev, ctx->m2m_ctx);
  218. }
  219. static void emmaprp_lock(void *priv)
  220. {
  221. struct emmaprp_ctx *ctx = priv;
  222. struct emmaprp_dev *pcdev = ctx->dev;
  223. mutex_lock(&pcdev->dev_mutex);
  224. }
  225. static void emmaprp_unlock(void *priv)
  226. {
  227. struct emmaprp_ctx *ctx = priv;
  228. struct emmaprp_dev *pcdev = ctx->dev;
  229. mutex_unlock(&pcdev->dev_mutex);
  230. }
  231. static inline void emmaprp_dump_regs(struct emmaprp_dev *pcdev)
  232. {
  233. dprintk(pcdev,
  234. "eMMa-PrP Registers:\n"
  235. " SOURCE_Y_PTR = 0x%08X\n"
  236. " SRC_FRAME_SIZE = 0x%08X\n"
  237. " DEST_Y_PTR = 0x%08X\n"
  238. " DEST_CR_PTR = 0x%08X\n"
  239. " DEST_CB_PTR = 0x%08X\n"
  240. " CH2_OUT_IMAGE_SIZE = 0x%08X\n"
  241. " CNTL = 0x%08X\n",
  242. readl(pcdev->base_emma + PRP_SOURCE_Y_PTR),
  243. readl(pcdev->base_emma + PRP_SRC_FRAME_SIZE),
  244. readl(pcdev->base_emma + PRP_DEST_Y_PTR),
  245. readl(pcdev->base_emma + PRP_DEST_CR_PTR),
  246. readl(pcdev->base_emma + PRP_DEST_CB_PTR),
  247. readl(pcdev->base_emma + PRP_CH2_OUT_IMAGE_SIZE),
  248. readl(pcdev->base_emma + PRP_CNTL));
  249. }
  250. static void emmaprp_device_run(void *priv)
  251. {
  252. struct emmaprp_ctx *ctx = priv;
  253. struct emmaprp_q_data *s_q_data, *d_q_data;
  254. struct vb2_buffer *src_buf, *dst_buf;
  255. struct emmaprp_dev *pcdev = ctx->dev;
  256. unsigned int s_width, s_height;
  257. unsigned int d_width, d_height;
  258. unsigned int d_size;
  259. dma_addr_t p_in, p_out;
  260. u32 tmp;
  261. src_buf = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
  262. dst_buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
  263. s_q_data = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
  264. s_width = s_q_data->width;
  265. s_height = s_q_data->height;
  266. d_q_data = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
  267. d_width = d_q_data->width;
  268. d_height = d_q_data->height;
  269. d_size = d_width * d_height;
  270. p_in = vb2_dma_contig_plane_dma_addr(src_buf, 0);
  271. p_out = vb2_dma_contig_plane_dma_addr(dst_buf, 0);
  272. if (!p_in || !p_out) {
  273. v4l2_err(&pcdev->v4l2_dev,
  274. "Acquiring kernel pointers to buffers failed\n");
  275. return;
  276. }
  277. /* Input frame parameters */
  278. writel(p_in, pcdev->base_emma + PRP_SOURCE_Y_PTR);
  279. writel(PRP_SIZE_WIDTH(s_width) | PRP_SIZE_HEIGHT(s_height),
  280. pcdev->base_emma + PRP_SRC_FRAME_SIZE);
  281. /* Output frame parameters */
  282. writel(p_out, pcdev->base_emma + PRP_DEST_Y_PTR);
  283. writel(p_out + d_size, pcdev->base_emma + PRP_DEST_CB_PTR);
  284. writel(p_out + d_size + (d_size >> 2),
  285. pcdev->base_emma + PRP_DEST_CR_PTR);
  286. writel(PRP_SIZE_WIDTH(d_width) | PRP_SIZE_HEIGHT(d_height),
  287. pcdev->base_emma + PRP_CH2_OUT_IMAGE_SIZE);
  288. /* IRQ configuration */
  289. tmp = readl(pcdev->base_emma + PRP_INTR_CNTL);
  290. writel(tmp | PRP_INTR_RDERR |
  291. PRP_INTR_CH2WERR |
  292. PRP_INTR_CH2FC,
  293. pcdev->base_emma + PRP_INTR_CNTL);
  294. emmaprp_dump_regs(pcdev);
  295. /* Enable transfer */
  296. tmp = readl(pcdev->base_emma + PRP_CNTL);
  297. writel(tmp | PRP_CNTL_CH2_OUT_YUV420 |
  298. PRP_CNTL_DATA_IN_YUV422 |
  299. PRP_CNTL_CH2EN,
  300. pcdev->base_emma + PRP_CNTL);
  301. }
  302. static irqreturn_t emmaprp_irq(int irq_emma, void *data)
  303. {
  304. struct emmaprp_dev *pcdev = data;
  305. struct emmaprp_ctx *curr_ctx;
  306. struct vb2_buffer *src_vb, *dst_vb;
  307. unsigned long flags;
  308. u32 irqst;
  309. /* Check irq flags and clear irq */
  310. irqst = readl(pcdev->base_emma + PRP_INTRSTATUS);
  311. writel(irqst, pcdev->base_emma + PRP_INTRSTATUS);
  312. dprintk(pcdev, "irqst = 0x%08x\n", irqst);
  313. curr_ctx = v4l2_m2m_get_curr_priv(pcdev->m2m_dev);
  314. if (curr_ctx == NULL) {
  315. pr_err("Instance released before the end of transaction\n");
  316. return IRQ_HANDLED;
  317. }
  318. if (!curr_ctx->aborting) {
  319. if ((irqst & PRP_INTR_ST_RDERR) ||
  320. (irqst & PRP_INTR_ST_CH2WERR)) {
  321. pr_err("PrP bus error occurred, this transfer is probably corrupted\n");
  322. writel(PRP_CNTL_SWRST, pcdev->base_emma + PRP_CNTL);
  323. } else if (irqst & PRP_INTR_ST_CH2B1CI) { /* buffer ready */
  324. src_vb = v4l2_m2m_src_buf_remove(curr_ctx->m2m_ctx);
  325. dst_vb = v4l2_m2m_dst_buf_remove(curr_ctx->m2m_ctx);
  326. dst_vb->v4l2_buf.timestamp = src_vb->v4l2_buf.timestamp;
  327. dst_vb->v4l2_buf.flags &=
  328. ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
  329. dst_vb->v4l2_buf.flags |=
  330. src_vb->v4l2_buf.flags
  331. & V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
  332. dst_vb->v4l2_buf.timecode = src_vb->v4l2_buf.timecode;
  333. spin_lock_irqsave(&pcdev->irqlock, flags);
  334. v4l2_m2m_buf_done(src_vb, VB2_BUF_STATE_DONE);
  335. v4l2_m2m_buf_done(dst_vb, VB2_BUF_STATE_DONE);
  336. spin_unlock_irqrestore(&pcdev->irqlock, flags);
  337. }
  338. }
  339. v4l2_m2m_job_finish(pcdev->m2m_dev, curr_ctx->m2m_ctx);
  340. return IRQ_HANDLED;
  341. }
  342. /*
  343. * video ioctls
  344. */
  345. static int vidioc_querycap(struct file *file, void *priv,
  346. struct v4l2_capability *cap)
  347. {
  348. strncpy(cap->driver, MEM2MEM_NAME, sizeof(cap->driver) - 1);
  349. strncpy(cap->card, MEM2MEM_NAME, sizeof(cap->card) - 1);
  350. cap->device_caps = V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING;
  351. cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
  352. return 0;
  353. }
  354. static int enum_fmt(struct v4l2_fmtdesc *f, u32 type)
  355. {
  356. int i, num;
  357. struct emmaprp_fmt *fmt;
  358. num = 0;
  359. for (i = 0; i < NUM_FORMATS; ++i) {
  360. if (formats[i].types & type) {
  361. /* index-th format of type type found ? */
  362. if (num == f->index)
  363. break;
  364. /* Correct type but haven't reached our index yet,
  365. * just increment per-type index */
  366. ++num;
  367. }
  368. }
  369. if (i < NUM_FORMATS) {
  370. /* Format found */
  371. fmt = &formats[i];
  372. strlcpy(f->description, fmt->name, sizeof(f->description) - 1);
  373. f->pixelformat = fmt->fourcc;
  374. return 0;
  375. }
  376. /* Format not found */
  377. return -EINVAL;
  378. }
  379. static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  380. struct v4l2_fmtdesc *f)
  381. {
  382. return enum_fmt(f, MEM2MEM_CAPTURE);
  383. }
  384. static int vidioc_enum_fmt_vid_out(struct file *file, void *priv,
  385. struct v4l2_fmtdesc *f)
  386. {
  387. return enum_fmt(f, MEM2MEM_OUTPUT);
  388. }
  389. static int vidioc_g_fmt(struct emmaprp_ctx *ctx, struct v4l2_format *f)
  390. {
  391. struct vb2_queue *vq;
  392. struct emmaprp_q_data *q_data;
  393. vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
  394. if (!vq)
  395. return -EINVAL;
  396. q_data = get_q_data(ctx, f->type);
  397. f->fmt.pix.width = q_data->width;
  398. f->fmt.pix.height = q_data->height;
  399. f->fmt.pix.field = V4L2_FIELD_NONE;
  400. f->fmt.pix.pixelformat = q_data->fmt->fourcc;
  401. if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_YUV420)
  402. f->fmt.pix.bytesperline = q_data->width * 3 / 2;
  403. else /* YUYV */
  404. f->fmt.pix.bytesperline = q_data->width * 2;
  405. f->fmt.pix.sizeimage = q_data->sizeimage;
  406. return 0;
  407. }
  408. static int vidioc_g_fmt_vid_out(struct file *file, void *priv,
  409. struct v4l2_format *f)
  410. {
  411. return vidioc_g_fmt(priv, f);
  412. }
  413. static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  414. struct v4l2_format *f)
  415. {
  416. return vidioc_g_fmt(priv, f);
  417. }
  418. static int vidioc_try_fmt(struct v4l2_format *f)
  419. {
  420. enum v4l2_field field;
  421. if (!find_format(f))
  422. return -EINVAL;
  423. field = f->fmt.pix.field;
  424. if (field == V4L2_FIELD_ANY)
  425. field = V4L2_FIELD_NONE;
  426. else if (V4L2_FIELD_NONE != field)
  427. return -EINVAL;
  428. /* V4L2 specification suggests the driver corrects the format struct
  429. * if any of the dimensions is unsupported */
  430. f->fmt.pix.field = field;
  431. if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_YUV420) {
  432. v4l_bound_align_image(&f->fmt.pix.width, MIN_W, MAX_W,
  433. W_ALIGN_YUV420, &f->fmt.pix.height,
  434. MIN_H, MAX_H, H_ALIGN, S_ALIGN);
  435. f->fmt.pix.bytesperline = f->fmt.pix.width * 3 / 2;
  436. } else {
  437. v4l_bound_align_image(&f->fmt.pix.width, MIN_W, MAX_W,
  438. W_ALIGN_OTHERS, &f->fmt.pix.height,
  439. MIN_H, MAX_H, H_ALIGN, S_ALIGN);
  440. f->fmt.pix.bytesperline = f->fmt.pix.width * 2;
  441. }
  442. f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
  443. return 0;
  444. }
  445. static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
  446. struct v4l2_format *f)
  447. {
  448. struct emmaprp_fmt *fmt;
  449. struct emmaprp_ctx *ctx = priv;
  450. fmt = find_format(f);
  451. if (!fmt || !(fmt->types & MEM2MEM_CAPTURE)) {
  452. v4l2_err(&ctx->dev->v4l2_dev,
  453. "Fourcc format (0x%08x) invalid.\n",
  454. f->fmt.pix.pixelformat);
  455. return -EINVAL;
  456. }
  457. return vidioc_try_fmt(f);
  458. }
  459. static int vidioc_try_fmt_vid_out(struct file *file, void *priv,
  460. struct v4l2_format *f)
  461. {
  462. struct emmaprp_fmt *fmt;
  463. struct emmaprp_ctx *ctx = priv;
  464. fmt = find_format(f);
  465. if (!fmt || !(fmt->types & MEM2MEM_OUTPUT)) {
  466. v4l2_err(&ctx->dev->v4l2_dev,
  467. "Fourcc format (0x%08x) invalid.\n",
  468. f->fmt.pix.pixelformat);
  469. return -EINVAL;
  470. }
  471. return vidioc_try_fmt(f);
  472. }
  473. static int vidioc_s_fmt(struct emmaprp_ctx *ctx, struct v4l2_format *f)
  474. {
  475. struct emmaprp_q_data *q_data;
  476. struct vb2_queue *vq;
  477. int ret;
  478. vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
  479. if (!vq)
  480. return -EINVAL;
  481. q_data = get_q_data(ctx, f->type);
  482. if (!q_data)
  483. return -EINVAL;
  484. if (vb2_is_busy(vq)) {
  485. v4l2_err(&ctx->dev->v4l2_dev, "%s queue busy\n", __func__);
  486. return -EBUSY;
  487. }
  488. ret = vidioc_try_fmt(f);
  489. if (ret)
  490. return ret;
  491. q_data->fmt = find_format(f);
  492. q_data->width = f->fmt.pix.width;
  493. q_data->height = f->fmt.pix.height;
  494. if (q_data->fmt->fourcc == V4L2_PIX_FMT_YUV420)
  495. q_data->sizeimage = q_data->width * q_data->height * 3 / 2;
  496. else /* YUYV */
  497. q_data->sizeimage = q_data->width * q_data->height * 2;
  498. dprintk(ctx->dev,
  499. "Setting format for type %d, wxh: %dx%d, fmt: %d\n",
  500. f->type, q_data->width, q_data->height, q_data->fmt->fourcc);
  501. return 0;
  502. }
  503. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  504. struct v4l2_format *f)
  505. {
  506. int ret;
  507. ret = vidioc_try_fmt_vid_cap(file, priv, f);
  508. if (ret)
  509. return ret;
  510. return vidioc_s_fmt(priv, f);
  511. }
  512. static int vidioc_s_fmt_vid_out(struct file *file, void *priv,
  513. struct v4l2_format *f)
  514. {
  515. int ret;
  516. ret = vidioc_try_fmt_vid_out(file, priv, f);
  517. if (ret)
  518. return ret;
  519. return vidioc_s_fmt(priv, f);
  520. }
  521. static int vidioc_reqbufs(struct file *file, void *priv,
  522. struct v4l2_requestbuffers *reqbufs)
  523. {
  524. struct emmaprp_ctx *ctx = priv;
  525. return v4l2_m2m_reqbufs(file, ctx->m2m_ctx, reqbufs);
  526. }
  527. static int vidioc_querybuf(struct file *file, void *priv,
  528. struct v4l2_buffer *buf)
  529. {
  530. struct emmaprp_ctx *ctx = priv;
  531. return v4l2_m2m_querybuf(file, ctx->m2m_ctx, buf);
  532. }
  533. static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
  534. {
  535. struct emmaprp_ctx *ctx = priv;
  536. return v4l2_m2m_qbuf(file, ctx->m2m_ctx, buf);
  537. }
  538. static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
  539. {
  540. struct emmaprp_ctx *ctx = priv;
  541. return v4l2_m2m_dqbuf(file, ctx->m2m_ctx, buf);
  542. }
  543. static int vidioc_streamon(struct file *file, void *priv,
  544. enum v4l2_buf_type type)
  545. {
  546. struct emmaprp_ctx *ctx = priv;
  547. return v4l2_m2m_streamon(file, ctx->m2m_ctx, type);
  548. }
  549. static int vidioc_streamoff(struct file *file, void *priv,
  550. enum v4l2_buf_type type)
  551. {
  552. struct emmaprp_ctx *ctx = priv;
  553. return v4l2_m2m_streamoff(file, ctx->m2m_ctx, type);
  554. }
  555. static const struct v4l2_ioctl_ops emmaprp_ioctl_ops = {
  556. .vidioc_querycap = vidioc_querycap,
  557. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  558. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  559. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  560. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  561. .vidioc_enum_fmt_vid_out = vidioc_enum_fmt_vid_out,
  562. .vidioc_g_fmt_vid_out = vidioc_g_fmt_vid_out,
  563. .vidioc_try_fmt_vid_out = vidioc_try_fmt_vid_out,
  564. .vidioc_s_fmt_vid_out = vidioc_s_fmt_vid_out,
  565. .vidioc_reqbufs = vidioc_reqbufs,
  566. .vidioc_querybuf = vidioc_querybuf,
  567. .vidioc_qbuf = vidioc_qbuf,
  568. .vidioc_dqbuf = vidioc_dqbuf,
  569. .vidioc_streamon = vidioc_streamon,
  570. .vidioc_streamoff = vidioc_streamoff,
  571. };
  572. /*
  573. * Queue operations
  574. */
  575. static int emmaprp_queue_setup(struct vb2_queue *vq,
  576. const struct v4l2_format *fmt,
  577. unsigned int *nbuffers, unsigned int *nplanes,
  578. unsigned int sizes[], void *alloc_ctxs[])
  579. {
  580. struct emmaprp_ctx *ctx = vb2_get_drv_priv(vq);
  581. struct emmaprp_q_data *q_data;
  582. unsigned int size, count = *nbuffers;
  583. q_data = get_q_data(ctx, vq->type);
  584. if (q_data->fmt->fourcc == V4L2_PIX_FMT_YUV420)
  585. size = q_data->width * q_data->height * 3 / 2;
  586. else
  587. size = q_data->width * q_data->height * 2;
  588. while (size * count > MEM2MEM_VID_MEM_LIMIT)
  589. (count)--;
  590. *nplanes = 1;
  591. *nbuffers = count;
  592. sizes[0] = size;
  593. alloc_ctxs[0] = ctx->dev->alloc_ctx;
  594. dprintk(ctx->dev, "get %d buffer(s) of size %d each.\n", count, size);
  595. return 0;
  596. }
  597. static int emmaprp_buf_prepare(struct vb2_buffer *vb)
  598. {
  599. struct emmaprp_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  600. struct emmaprp_q_data *q_data;
  601. dprintk(ctx->dev, "type: %d\n", vb->vb2_queue->type);
  602. q_data = get_q_data(ctx, vb->vb2_queue->type);
  603. if (vb2_plane_size(vb, 0) < q_data->sizeimage) {
  604. dprintk(ctx->dev, "%s data will not fit into plane"
  605. "(%lu < %lu)\n", __func__,
  606. vb2_plane_size(vb, 0),
  607. (long)q_data->sizeimage);
  608. return -EINVAL;
  609. }
  610. vb2_set_plane_payload(vb, 0, q_data->sizeimage);
  611. return 0;
  612. }
  613. static void emmaprp_buf_queue(struct vb2_buffer *vb)
  614. {
  615. struct emmaprp_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  616. v4l2_m2m_buf_queue(ctx->m2m_ctx, vb);
  617. }
  618. static struct vb2_ops emmaprp_qops = {
  619. .queue_setup = emmaprp_queue_setup,
  620. .buf_prepare = emmaprp_buf_prepare,
  621. .buf_queue = emmaprp_buf_queue,
  622. };
  623. static int queue_init(void *priv, struct vb2_queue *src_vq,
  624. struct vb2_queue *dst_vq)
  625. {
  626. struct emmaprp_ctx *ctx = priv;
  627. int ret;
  628. src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  629. src_vq->io_modes = VB2_MMAP | VB2_USERPTR;
  630. src_vq->drv_priv = ctx;
  631. src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
  632. src_vq->ops = &emmaprp_qops;
  633. src_vq->mem_ops = &vb2_dma_contig_memops;
  634. src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
  635. ret = vb2_queue_init(src_vq);
  636. if (ret)
  637. return ret;
  638. dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  639. dst_vq->io_modes = VB2_MMAP | VB2_USERPTR;
  640. dst_vq->drv_priv = ctx;
  641. dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
  642. dst_vq->ops = &emmaprp_qops;
  643. dst_vq->mem_ops = &vb2_dma_contig_memops;
  644. dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
  645. return vb2_queue_init(dst_vq);
  646. }
  647. /*
  648. * File operations
  649. */
  650. static int emmaprp_open(struct file *file)
  651. {
  652. struct emmaprp_dev *pcdev = video_drvdata(file);
  653. struct emmaprp_ctx *ctx;
  654. ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
  655. if (!ctx)
  656. return -ENOMEM;
  657. file->private_data = ctx;
  658. ctx->dev = pcdev;
  659. if (mutex_lock_interruptible(&pcdev->dev_mutex)) {
  660. kfree(ctx);
  661. return -ERESTARTSYS;
  662. }
  663. ctx->m2m_ctx = v4l2_m2m_ctx_init(pcdev->m2m_dev, ctx, &queue_init);
  664. if (IS_ERR(ctx->m2m_ctx)) {
  665. int ret = PTR_ERR(ctx->m2m_ctx);
  666. mutex_unlock(&pcdev->dev_mutex);
  667. kfree(ctx);
  668. return ret;
  669. }
  670. clk_prepare_enable(pcdev->clk_emma_ipg);
  671. clk_prepare_enable(pcdev->clk_emma_ahb);
  672. ctx->q_data[V4L2_M2M_SRC].fmt = &formats[1];
  673. ctx->q_data[V4L2_M2M_DST].fmt = &formats[0];
  674. mutex_unlock(&pcdev->dev_mutex);
  675. dprintk(pcdev, "Created instance %p, m2m_ctx: %p\n", ctx, ctx->m2m_ctx);
  676. return 0;
  677. }
  678. static int emmaprp_release(struct file *file)
  679. {
  680. struct emmaprp_dev *pcdev = video_drvdata(file);
  681. struct emmaprp_ctx *ctx = file->private_data;
  682. dprintk(pcdev, "Releasing instance %p\n", ctx);
  683. mutex_lock(&pcdev->dev_mutex);
  684. clk_disable_unprepare(pcdev->clk_emma_ahb);
  685. clk_disable_unprepare(pcdev->clk_emma_ipg);
  686. v4l2_m2m_ctx_release(ctx->m2m_ctx);
  687. mutex_unlock(&pcdev->dev_mutex);
  688. kfree(ctx);
  689. return 0;
  690. }
  691. static unsigned int emmaprp_poll(struct file *file,
  692. struct poll_table_struct *wait)
  693. {
  694. struct emmaprp_dev *pcdev = video_drvdata(file);
  695. struct emmaprp_ctx *ctx = file->private_data;
  696. unsigned int res;
  697. mutex_lock(&pcdev->dev_mutex);
  698. res = v4l2_m2m_poll(file, ctx->m2m_ctx, wait);
  699. mutex_unlock(&pcdev->dev_mutex);
  700. return res;
  701. }
  702. static int emmaprp_mmap(struct file *file, struct vm_area_struct *vma)
  703. {
  704. struct emmaprp_dev *pcdev = video_drvdata(file);
  705. struct emmaprp_ctx *ctx = file->private_data;
  706. int ret;
  707. if (mutex_lock_interruptible(&pcdev->dev_mutex))
  708. return -ERESTARTSYS;
  709. ret = v4l2_m2m_mmap(file, ctx->m2m_ctx, vma);
  710. mutex_unlock(&pcdev->dev_mutex);
  711. return ret;
  712. }
  713. static const struct v4l2_file_operations emmaprp_fops = {
  714. .owner = THIS_MODULE,
  715. .open = emmaprp_open,
  716. .release = emmaprp_release,
  717. .poll = emmaprp_poll,
  718. .unlocked_ioctl = video_ioctl2,
  719. .mmap = emmaprp_mmap,
  720. };
  721. static struct video_device emmaprp_videodev = {
  722. .name = MEM2MEM_NAME,
  723. .fops = &emmaprp_fops,
  724. .ioctl_ops = &emmaprp_ioctl_ops,
  725. .minor = -1,
  726. .release = video_device_release,
  727. .vfl_dir = VFL_DIR_M2M,
  728. };
  729. static struct v4l2_m2m_ops m2m_ops = {
  730. .device_run = emmaprp_device_run,
  731. .job_abort = emmaprp_job_abort,
  732. .lock = emmaprp_lock,
  733. .unlock = emmaprp_unlock,
  734. };
  735. static int emmaprp_probe(struct platform_device *pdev)
  736. {
  737. struct emmaprp_dev *pcdev;
  738. struct video_device *vfd;
  739. struct resource *res;
  740. int irq, ret;
  741. pcdev = devm_kzalloc(&pdev->dev, sizeof(*pcdev), GFP_KERNEL);
  742. if (!pcdev)
  743. return -ENOMEM;
  744. spin_lock_init(&pcdev->irqlock);
  745. pcdev->clk_emma_ipg = devm_clk_get(&pdev->dev, "ipg");
  746. if (IS_ERR(pcdev->clk_emma_ipg)) {
  747. return PTR_ERR(pcdev->clk_emma_ipg);
  748. }
  749. pcdev->clk_emma_ahb = devm_clk_get(&pdev->dev, "ahb");
  750. if (IS_ERR(pcdev->clk_emma_ahb))
  751. return PTR_ERR(pcdev->clk_emma_ahb);
  752. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  753. pcdev->base_emma = devm_ioremap_resource(&pdev->dev, res);
  754. if (IS_ERR(pcdev->base_emma))
  755. return PTR_ERR(pcdev->base_emma);
  756. ret = v4l2_device_register(&pdev->dev, &pcdev->v4l2_dev);
  757. if (ret)
  758. return ret;
  759. mutex_init(&pcdev->dev_mutex);
  760. vfd = video_device_alloc();
  761. if (!vfd) {
  762. v4l2_err(&pcdev->v4l2_dev, "Failed to allocate video device\n");
  763. ret = -ENOMEM;
  764. goto unreg_dev;
  765. }
  766. *vfd = emmaprp_videodev;
  767. vfd->lock = &pcdev->dev_mutex;
  768. vfd->v4l2_dev = &pcdev->v4l2_dev;
  769. video_set_drvdata(vfd, pcdev);
  770. snprintf(vfd->name, sizeof(vfd->name), "%s", emmaprp_videodev.name);
  771. pcdev->vfd = vfd;
  772. v4l2_info(&pcdev->v4l2_dev, EMMAPRP_MODULE_NAME
  773. " Device registered as /dev/video%d\n", vfd->num);
  774. platform_set_drvdata(pdev, pcdev);
  775. irq = platform_get_irq(pdev, 0);
  776. ret = devm_request_irq(&pdev->dev, irq, emmaprp_irq, 0,
  777. dev_name(&pdev->dev), pcdev);
  778. if (ret)
  779. goto rel_vdev;
  780. pcdev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
  781. if (IS_ERR(pcdev->alloc_ctx)) {
  782. v4l2_err(&pcdev->v4l2_dev, "Failed to alloc vb2 context\n");
  783. ret = PTR_ERR(pcdev->alloc_ctx);
  784. goto rel_vdev;
  785. }
  786. pcdev->m2m_dev = v4l2_m2m_init(&m2m_ops);
  787. if (IS_ERR(pcdev->m2m_dev)) {
  788. v4l2_err(&pcdev->v4l2_dev, "Failed to init mem2mem device\n");
  789. ret = PTR_ERR(pcdev->m2m_dev);
  790. goto rel_ctx;
  791. }
  792. ret = video_register_device(vfd, VFL_TYPE_GRABBER, 0);
  793. if (ret) {
  794. v4l2_err(&pcdev->v4l2_dev, "Failed to register video device\n");
  795. goto rel_m2m;
  796. }
  797. return 0;
  798. rel_m2m:
  799. v4l2_m2m_release(pcdev->m2m_dev);
  800. rel_ctx:
  801. vb2_dma_contig_cleanup_ctx(pcdev->alloc_ctx);
  802. rel_vdev:
  803. video_device_release(vfd);
  804. unreg_dev:
  805. v4l2_device_unregister(&pcdev->v4l2_dev);
  806. mutex_destroy(&pcdev->dev_mutex);
  807. return ret;
  808. }
  809. static int emmaprp_remove(struct platform_device *pdev)
  810. {
  811. struct emmaprp_dev *pcdev = platform_get_drvdata(pdev);
  812. v4l2_info(&pcdev->v4l2_dev, "Removing " EMMAPRP_MODULE_NAME);
  813. video_unregister_device(pcdev->vfd);
  814. v4l2_m2m_release(pcdev->m2m_dev);
  815. vb2_dma_contig_cleanup_ctx(pcdev->alloc_ctx);
  816. v4l2_device_unregister(&pcdev->v4l2_dev);
  817. mutex_destroy(&pcdev->dev_mutex);
  818. return 0;
  819. }
  820. static struct platform_driver emmaprp_pdrv = {
  821. .probe = emmaprp_probe,
  822. .remove = emmaprp_remove,
  823. .driver = {
  824. .name = MEM2MEM_NAME,
  825. },
  826. };
  827. module_platform_driver(emmaprp_pdrv);