[media] soc-camera: Honor user-requested bytesperline and sizeimage
[deliverable/linux.git] / drivers / media / video / mx2_camera.c
CommitLineData
2066930d
BS
1/*
2 * V4L2 Driver for i.MX27/i.MX25 camera host
3 *
4 * Copyright (C) 2008, Sascha Hauer, Pengutronix
5 * Copyright (C) 2010, Baruch Siach, Orex Computed Radiography
c6a41e32 6 * Copyright (C) 2012, Javier Martin, Vista Silicon S.L.
2066930d
BS
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 */
13
14#include <linux/init.h>
15#include <linux/module.h>
16#include <linux/io.h>
17#include <linux/delay.h>
18#include <linux/slab.h>
19#include <linux/dma-mapping.h>
20#include <linux/errno.h>
21#include <linux/fs.h>
750a6dff 22#include <linux/gcd.h>
2066930d
BS
23#include <linux/interrupt.h>
24#include <linux/kernel.h>
25#include <linux/mm.h>
26#include <linux/moduleparam.h>
27#include <linux/time.h>
2066930d
BS
28#include <linux/device.h>
29#include <linux/platform_device.h>
30#include <linux/mutex.h>
31#include <linux/clk.h>
32
33#include <media/v4l2-common.h>
34#include <media/v4l2-dev.h>
c6a41e32
JM
35#include <media/videobuf2-core.h>
36#include <media/videobuf2-dma-contig.h>
2066930d
BS
37#include <media/soc_camera.h>
38#include <media/soc_mediabus.h>
39
40#include <linux/videodev2.h>
41
42#include <mach/mx2_cam.h>
2066930d
BS
43#include <mach/hardware.h>
44
45#include <asm/dma.h>
46
47#define MX2_CAM_DRV_NAME "mx2-camera"
64dc3c1a 48#define MX2_CAM_VERSION "0.0.6"
2066930d
BS
49#define MX2_CAM_DRIVER_DESCRIPTION "i.MX2x_Camera"
50
51/* reset values */
52#define CSICR1_RESET_VAL 0x40000800
53#define CSICR2_RESET_VAL 0x0
54#define CSICR3_RESET_VAL 0x0
55
56/* csi control reg 1 */
57#define CSICR1_SWAP16_EN (1 << 31)
58#define CSICR1_EXT_VSYNC (1 << 30)
59#define CSICR1_EOF_INTEN (1 << 29)
60#define CSICR1_PRP_IF_EN (1 << 28)
61#define CSICR1_CCIR_MODE (1 << 27)
62#define CSICR1_COF_INTEN (1 << 26)
63#define CSICR1_SF_OR_INTEN (1 << 25)
64#define CSICR1_RF_OR_INTEN (1 << 24)
65#define CSICR1_STATFF_LEVEL (3 << 22)
66#define CSICR1_STATFF_INTEN (1 << 21)
67#define CSICR1_RXFF_LEVEL(l) (((l) & 3) << 19) /* MX27 */
68#define CSICR1_FB2_DMA_INTEN (1 << 20) /* MX25 */
69#define CSICR1_FB1_DMA_INTEN (1 << 19) /* MX25 */
70#define CSICR1_RXFF_INTEN (1 << 18)
71#define CSICR1_SOF_POL (1 << 17)
72#define CSICR1_SOF_INTEN (1 << 16)
73#define CSICR1_MCLKDIV(d) (((d) & 0xF) << 12)
74#define CSICR1_HSYNC_POL (1 << 11)
75#define CSICR1_CCIR_EN (1 << 10)
76#define CSICR1_MCLKEN (1 << 9)
77#define CSICR1_FCC (1 << 8)
78#define CSICR1_PACK_DIR (1 << 7)
79#define CSICR1_CLR_STATFIFO (1 << 6)
80#define CSICR1_CLR_RXFIFO (1 << 5)
81#define CSICR1_GCLK_MODE (1 << 4)
82#define CSICR1_INV_DATA (1 << 3)
83#define CSICR1_INV_PCLK (1 << 2)
84#define CSICR1_REDGE (1 << 1)
85
86#define SHIFT_STATFF_LEVEL 22
87#define SHIFT_RXFF_LEVEL 19
88#define SHIFT_MCLKDIV 12
89
90/* control reg 3 */
91#define CSICR3_FRMCNT (0xFFFF << 16)
92#define CSICR3_FRMCNT_RST (1 << 15)
93#define CSICR3_DMA_REFLASH_RFF (1 << 14)
94#define CSICR3_DMA_REFLASH_SFF (1 << 13)
95#define CSICR3_DMA_REQ_EN_RFF (1 << 12)
96#define CSICR3_DMA_REQ_EN_SFF (1 << 11)
97#define CSICR3_RXFF_LEVEL(l) (((l) & 7) << 4) /* MX25 */
98#define CSICR3_CSI_SUP (1 << 3)
99#define CSICR3_ZERO_PACK_EN (1 << 2)
100#define CSICR3_ECC_INT_EN (1 << 1)
101#define CSICR3_ECC_AUTO_EN (1 << 0)
102
103#define SHIFT_FRMCNT 16
104
105/* csi status reg */
106#define CSISR_SFF_OR_INT (1 << 25)
107#define CSISR_RFF_OR_INT (1 << 24)
108#define CSISR_STATFF_INT (1 << 21)
109#define CSISR_DMA_TSF_FB2_INT (1 << 20) /* MX25 */
110#define CSISR_DMA_TSF_FB1_INT (1 << 19) /* MX25 */
111#define CSISR_RXFF_INT (1 << 18)
112#define CSISR_EOF_INT (1 << 17)
113#define CSISR_SOF_INT (1 << 16)
114#define CSISR_F2_INT (1 << 15)
115#define CSISR_F1_INT (1 << 14)
116#define CSISR_COF_INT (1 << 13)
117#define CSISR_ECC_INT (1 << 1)
118#define CSISR_DRDY (1 << 0)
119
120#define CSICR1 0x00
121#define CSICR2 0x04
122#define CSISR (cpu_is_mx27() ? 0x08 : 0x18)
123#define CSISTATFIFO 0x0c
124#define CSIRFIFO 0x10
125#define CSIRXCNT 0x14
126#define CSICR3 (cpu_is_mx27() ? 0x1C : 0x08)
127#define CSIDMASA_STATFIFO 0x20
128#define CSIDMATA_STATFIFO 0x24
129#define CSIDMASA_FB1 0x28
130#define CSIDMASA_FB2 0x2c
131#define CSIFBUF_PARA 0x30
132#define CSIIMAG_PARA 0x34
133
134/* EMMA PrP */
135#define PRP_CNTL 0x00
136#define PRP_INTR_CNTL 0x04
137#define PRP_INTRSTATUS 0x08
138#define PRP_SOURCE_Y_PTR 0x0c
139#define PRP_SOURCE_CB_PTR 0x10
140#define PRP_SOURCE_CR_PTR 0x14
141#define PRP_DEST_RGB1_PTR 0x18
142#define PRP_DEST_RGB2_PTR 0x1c
143#define PRP_DEST_Y_PTR 0x20
144#define PRP_DEST_CB_PTR 0x24
145#define PRP_DEST_CR_PTR 0x28
146#define PRP_SRC_FRAME_SIZE 0x2c
147#define PRP_DEST_CH1_LINE_STRIDE 0x30
148#define PRP_SRC_PIXEL_FORMAT_CNTL 0x34
149#define PRP_CH1_PIXEL_FORMAT_CNTL 0x38
150#define PRP_CH1_OUT_IMAGE_SIZE 0x3c
151#define PRP_CH2_OUT_IMAGE_SIZE 0x40
152#define PRP_SRC_LINE_STRIDE 0x44
153#define PRP_CSC_COEF_012 0x48
154#define PRP_CSC_COEF_345 0x4c
155#define PRP_CSC_COEF_678 0x50
156#define PRP_CH1_RZ_HORI_COEF1 0x54
157#define PRP_CH1_RZ_HORI_COEF2 0x58
158#define PRP_CH1_RZ_HORI_VALID 0x5c
159#define PRP_CH1_RZ_VERT_COEF1 0x60
160#define PRP_CH1_RZ_VERT_COEF2 0x64
161#define PRP_CH1_RZ_VERT_VALID 0x68
162#define PRP_CH2_RZ_HORI_COEF1 0x6c
163#define PRP_CH2_RZ_HORI_COEF2 0x70
164#define PRP_CH2_RZ_HORI_VALID 0x74
165#define PRP_CH2_RZ_VERT_COEF1 0x78
166#define PRP_CH2_RZ_VERT_COEF2 0x7c
167#define PRP_CH2_RZ_VERT_VALID 0x80
168
169#define PRP_CNTL_CH1EN (1 << 0)
170#define PRP_CNTL_CH2EN (1 << 1)
171#define PRP_CNTL_CSIEN (1 << 2)
172#define PRP_CNTL_DATA_IN_YUV420 (0 << 3)
173#define PRP_CNTL_DATA_IN_YUV422 (1 << 3)
174#define PRP_CNTL_DATA_IN_RGB16 (2 << 3)
175#define PRP_CNTL_DATA_IN_RGB32 (3 << 3)
176#define PRP_CNTL_CH1_OUT_RGB8 (0 << 5)
177#define PRP_CNTL_CH1_OUT_RGB16 (1 << 5)
178#define PRP_CNTL_CH1_OUT_RGB32 (2 << 5)
179#define PRP_CNTL_CH1_OUT_YUV422 (3 << 5)
180#define PRP_CNTL_CH2_OUT_YUV420 (0 << 7)
181#define PRP_CNTL_CH2_OUT_YUV422 (1 << 7)
182#define PRP_CNTL_CH2_OUT_YUV444 (2 << 7)
183#define PRP_CNTL_CH1_LEN (1 << 9)
184#define PRP_CNTL_CH2_LEN (1 << 10)
185#define PRP_CNTL_SKIP_FRAME (1 << 11)
186#define PRP_CNTL_SWRST (1 << 12)
187#define PRP_CNTL_CLKEN (1 << 13)
188#define PRP_CNTL_WEN (1 << 14)
189#define PRP_CNTL_CH1BYP (1 << 15)
190#define PRP_CNTL_IN_TSKIP(x) ((x) << 16)
191#define PRP_CNTL_CH1_TSKIP(x) ((x) << 19)
192#define PRP_CNTL_CH2_TSKIP(x) ((x) << 22)
193#define PRP_CNTL_INPUT_FIFO_LEVEL(x) ((x) << 25)
194#define PRP_CNTL_RZ_FIFO_LEVEL(x) ((x) << 27)
195#define PRP_CNTL_CH2B1EN (1 << 29)
196#define PRP_CNTL_CH2B2EN (1 << 30)
197#define PRP_CNTL_CH2FEN (1 << 31)
198
199/* IRQ Enable and status register */
200#define PRP_INTR_RDERR (1 << 0)
201#define PRP_INTR_CH1WERR (1 << 1)
202#define PRP_INTR_CH2WERR (1 << 2)
203#define PRP_INTR_CH1FC (1 << 3)
204#define PRP_INTR_CH2FC (1 << 5)
205#define PRP_INTR_LBOVF (1 << 7)
206#define PRP_INTR_CH2OVF (1 << 8)
207
750a6dff
JM
208/* Resizing registers */
209#define PRP_RZ_VALID_TBL_LEN(x) ((x) << 24)
210#define PRP_RZ_VALID_BILINEAR (1 << 31)
211
2066930d
BS
212#define MAX_VIDEO_MEM 16
213
750a6dff
JM
214#define RESIZE_NUM_MIN 1
215#define RESIZE_NUM_MAX 20
216#define BC_COEF 3
217#define SZ_COEF (1 << BC_COEF)
218
219#define RESIZE_DIR_H 0
220#define RESIZE_DIR_V 1
221
222#define RESIZE_ALGO_BILINEAR 0
223#define RESIZE_ALGO_AVERAGING 1
224
f410991d
JM
225struct mx2_prp_cfg {
226 int channel;
227 u32 in_fmt;
228 u32 out_fmt;
229 u32 src_pixel;
230 u32 ch1_pixel;
231 u32 irq_flags;
232};
233
750a6dff
JM
234/* prp resizing parameters */
235struct emma_prp_resize {
236 int algo; /* type of algorithm used */
237 int len; /* number of coefficients */
238 unsigned char s[RESIZE_NUM_MAX]; /* table of coefficients */
239};
240
f410991d
JM
241/* prp configuration for a client-host fmt pair */
242struct mx2_fmt_cfg {
243 enum v4l2_mbus_pixelcode in_fmt;
244 u32 out_fmt;
245 struct mx2_prp_cfg cfg;
246};
247
c6a41e32
JM
248enum mx2_buffer_state {
249 MX2_STATE_QUEUED,
250 MX2_STATE_ACTIVE,
251 MX2_STATE_DONE,
252};
253
560fab62
JM
254struct mx2_buf_internal {
255 struct list_head queue;
256 int bufnum;
257 bool discard;
258};
259
c6a41e32
JM
260/* buffer for one video frame */
261struct mx2_buffer {
262 /* common v4l buffer stuff -- must be first */
263 struct vb2_buffer vb;
c6a41e32 264 enum mx2_buffer_state state;
560fab62 265 struct mx2_buf_internal internal;
c6a41e32
JM
266};
267
2066930d
BS
268struct mx2_camera_dev {
269 struct device *dev;
270 struct soc_camera_host soc_host;
271 struct soc_camera_device *icd;
272 struct clk *clk_csi, *clk_emma;
273
274 unsigned int irq_csi, irq_emma;
275 void __iomem *base_csi, *base_emma;
276 unsigned long base_dma;
277
278 struct mx2_camera_platform_data *pdata;
279 struct resource *res_csi, *res_emma;
280 unsigned long platform_flags;
281
282 struct list_head capture;
283 struct list_head active_bufs;
cdc9d6f1 284 struct list_head discard;
2066930d
BS
285
286 spinlock_t lock;
287
288 int dma;
289 struct mx2_buffer *active;
290 struct mx2_buffer *fb1_active;
291 struct mx2_buffer *fb2_active;
292
2066930d
BS
293 u32 csicr1;
294
560fab62 295 struct mx2_buf_internal buf_discard[2];
79d3c2c2 296 void *discard_buffer;
2066930d
BS
297 dma_addr_t discard_buffer_dma;
298 size_t discard_size;
f410991d 299 struct mx2_fmt_cfg *emma_prp;
750a6dff
JM
300 struct emma_prp_resize resizing[2];
301 unsigned int s_width, s_height;
ccd1a499 302 u32 frame_count;
c6a41e32 303 struct vb2_alloc_ctx *alloc_ctx;
2066930d
BS
304};
305
560fab62
JM
306static struct mx2_buffer *mx2_ibuf_to_buf(struct mx2_buf_internal *int_buf)
307{
308 return container_of(int_buf, struct mx2_buffer, internal);
309}
310
f410991d
JM
311static struct mx2_fmt_cfg mx27_emma_prp_table[] = {
312 /*
313 * This is a generic configuration which is valid for most
314 * prp input-output format combinations.
315 * We set the incomming and outgoing pixelformat to a
316 * 16 Bit wide format and adjust the bytesperline
317 * accordingly. With this configuration the inputdata
318 * will not be changed by the emma and could be any type
319 * of 16 Bit Pixelformat.
320 */
321 {
322 .in_fmt = 0,
323 .out_fmt = 0,
324 .cfg = {
325 .channel = 1,
326 .in_fmt = PRP_CNTL_DATA_IN_RGB16,
327 .out_fmt = PRP_CNTL_CH1_OUT_RGB16,
328 .src_pixel = 0x2ca00565, /* RGB565 */
329 .ch1_pixel = 0x2ca00565, /* RGB565 */
330 .irq_flags = PRP_INTR_RDERR | PRP_INTR_CH1WERR |
331 PRP_INTR_CH1FC | PRP_INTR_LBOVF,
332 }
333 },
334 {
335 .in_fmt = V4L2_MBUS_FMT_YUYV8_2X8,
336 .out_fmt = V4L2_PIX_FMT_YUV420,
337 .cfg = {
338 .channel = 2,
339 .in_fmt = PRP_CNTL_DATA_IN_YUV422,
340 .out_fmt = PRP_CNTL_CH2_OUT_YUV420,
341 .src_pixel = 0x22000888, /* YUV422 (YUYV) */
342 .irq_flags = PRP_INTR_RDERR | PRP_INTR_CH2WERR |
343 PRP_INTR_CH2FC | PRP_INTR_LBOVF |
344 PRP_INTR_CH2OVF,
345 }
346 },
d509835e
JM
347 {
348 .in_fmt = V4L2_MBUS_FMT_UYVY8_2X8,
349 .out_fmt = V4L2_PIX_FMT_YUV420,
350 .cfg = {
351 .channel = 2,
352 .in_fmt = PRP_CNTL_DATA_IN_YUV422,
353 .out_fmt = PRP_CNTL_CH2_OUT_YUV420,
354 .src_pixel = 0x22000888, /* YUV422 (YUYV) */
355 .irq_flags = PRP_INTR_RDERR | PRP_INTR_CH2WERR |
356 PRP_INTR_CH2FC | PRP_INTR_LBOVF |
357 PRP_INTR_CH2OVF,
358 }
359 },
f410991d
JM
360};
361
362static struct mx2_fmt_cfg *mx27_emma_prp_get_format(
363 enum v4l2_mbus_pixelcode in_fmt,
364 u32 out_fmt)
365{
366 int i;
367
368 for (i = 1; i < ARRAY_SIZE(mx27_emma_prp_table); i++)
369 if ((mx27_emma_prp_table[i].in_fmt == in_fmt) &&
370 (mx27_emma_prp_table[i].out_fmt == out_fmt)) {
371 return &mx27_emma_prp_table[i];
372 }
373 /* If no match return the most generic configuration */
374 return &mx27_emma_prp_table[0];
375};
376
cdc9d6f1
JM
377static void mx27_update_emma_buf(struct mx2_camera_dev *pcdev,
378 unsigned long phys, int bufnum)
379{
380 struct mx2_fmt_cfg *prp = pcdev->emma_prp;
381
382 if (prp->cfg.channel == 1) {
383 writel(phys, pcdev->base_emma +
384 PRP_DEST_RGB1_PTR + 4 * bufnum);
385 } else {
386 writel(phys, pcdev->base_emma +
387 PRP_DEST_Y_PTR - 0x14 * bufnum);
388 if (prp->out_fmt == V4L2_PIX_FMT_YUV420) {
389 u32 imgsize = pcdev->icd->user_height *
390 pcdev->icd->user_width;
391
392 writel(phys + imgsize, pcdev->base_emma +
393 PRP_DEST_CB_PTR - 0x14 * bufnum);
394 writel(phys + ((5 * imgsize) / 4), pcdev->base_emma +
395 PRP_DEST_CR_PTR - 0x14 * bufnum);
396 }
397 }
398}
399
2066930d
BS
400static void mx2_camera_deactivate(struct mx2_camera_dev *pcdev)
401{
402 unsigned long flags;
403
404 clk_disable(pcdev->clk_csi);
405 writel(0, pcdev->base_csi + CSICR1);
2c9ba37d 406 if (cpu_is_mx27()) {
2066930d
BS
407 writel(0, pcdev->base_emma + PRP_CNTL);
408 } else if (cpu_is_mx25()) {
409 spin_lock_irqsave(&pcdev->lock, flags);
410 pcdev->fb1_active = NULL;
411 pcdev->fb2_active = NULL;
412 writel(0, pcdev->base_csi + CSIDMASA_FB1);
413 writel(0, pcdev->base_csi + CSIDMASA_FB2);
414 spin_unlock_irqrestore(&pcdev->lock, flags);
415 }
416}
417
418/*
419 * The following two functions absolutely depend on the fact, that
420 * there can be only one camera on mx2 camera sensor interface
421 */
422static int mx2_camera_add_device(struct soc_camera_device *icd)
423{
7dfff953 424 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
2066930d
BS
425 struct mx2_camera_dev *pcdev = ici->priv;
426 int ret;
427 u32 csicr1;
428
429 if (pcdev->icd)
430 return -EBUSY;
431
432 ret = clk_enable(pcdev->clk_csi);
433 if (ret < 0)
434 return ret;
435
436 csicr1 = CSICR1_MCLKEN;
437
2c9ba37d 438 if (cpu_is_mx27()) {
2066930d
BS
439 csicr1 |= CSICR1_PRP_IF_EN | CSICR1_FCC |
440 CSICR1_RXFF_LEVEL(0);
441 } else if (cpu_is_mx27())
442 csicr1 |= CSICR1_SOF_INTEN | CSICR1_RXFF_LEVEL(2);
443
444 pcdev->csicr1 = csicr1;
445 writel(pcdev->csicr1, pcdev->base_csi + CSICR1);
446
447 pcdev->icd = icd;
cdc9d6f1 448 pcdev->frame_count = 0;
2066930d 449
7dfff953 450 dev_info(icd->parent, "Camera driver attached to camera %d\n",
2066930d
BS
451 icd->devnum);
452
453 return 0;
454}
455
456static void mx2_camera_remove_device(struct soc_camera_device *icd)
457{
7dfff953 458 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
2066930d
BS
459 struct mx2_camera_dev *pcdev = ici->priv;
460
461 BUG_ON(icd != pcdev->icd);
462
7dfff953 463 dev_info(icd->parent, "Camera driver detached from camera %d\n",
2066930d
BS
464 icd->devnum);
465
466 mx2_camera_deactivate(pcdev);
467
2066930d
BS
468 pcdev->icd = NULL;
469}
470
2066930d
BS
471static void mx25_camera_frame_done(struct mx2_camera_dev *pcdev, int fb,
472 int state)
473{
c6a41e32 474 struct vb2_buffer *vb;
2066930d
BS
475 struct mx2_buffer *buf;
476 struct mx2_buffer **fb_active = fb == 1 ? &pcdev->fb1_active :
477 &pcdev->fb2_active;
478 u32 fb_reg = fb == 1 ? CSIDMASA_FB1 : CSIDMASA_FB2;
479 unsigned long flags;
480
481 spin_lock_irqsave(&pcdev->lock, flags);
482
5384a12b
BS
483 if (*fb_active == NULL)
484 goto out;
485
2066930d 486 vb = &(*fb_active)->vb;
c6a41e32
JM
487 dev_dbg(pcdev->dev, "%s (vb=0x%p) 0x%p %lu\n", __func__,
488 vb, vb2_plane_vaddr(vb, 0), vb2_get_plane_payload(vb, 0));
2066930d 489
c6a41e32
JM
490 do_gettimeofday(&vb->v4l2_buf.timestamp);
491 vb->v4l2_buf.sequence++;
492 vb2_buffer_done(vb, VB2_BUF_STATE_DONE);
2066930d
BS
493
494 if (list_empty(&pcdev->capture)) {
495 buf = NULL;
496 writel(0, pcdev->base_csi + fb_reg);
497 } else {
a654ba16 498 buf = list_first_entry(&pcdev->capture, struct mx2_buffer,
560fab62 499 internal.queue);
2066930d 500 vb = &buf->vb;
560fab62 501 list_del(&buf->internal.queue);
c6a41e32
JM
502 buf->state = MX2_STATE_ACTIVE;
503 writel(vb2_dma_contig_plane_dma_addr(vb, 0),
504 pcdev->base_csi + fb_reg);
2066930d
BS
505 }
506
507 *fb_active = buf;
508
5384a12b 509out:
2066930d
BS
510 spin_unlock_irqrestore(&pcdev->lock, flags);
511}
512
513static irqreturn_t mx25_camera_irq(int irq_csi, void *data)
514{
515 struct mx2_camera_dev *pcdev = data;
516 u32 status = readl(pcdev->base_csi + CSISR);
517
518 if (status & CSISR_DMA_TSF_FB1_INT)
c6a41e32 519 mx25_camera_frame_done(pcdev, 1, MX2_STATE_DONE);
2066930d 520 else if (status & CSISR_DMA_TSF_FB2_INT)
c6a41e32 521 mx25_camera_frame_done(pcdev, 2, MX2_STATE_DONE);
2066930d
BS
522
523 /* FIXME: handle CSISR_RFF_OR_INT */
524
525 writel(status, pcdev->base_csi + CSISR);
526
527 return IRQ_HANDLED;
528}
529
530/*
531 * Videobuf operations
532 */
c6a41e32
JM
533static int mx2_videobuf_setup(struct vb2_queue *vq,
534 const struct v4l2_format *fmt,
535 unsigned int *count, unsigned int *num_planes,
536 unsigned int sizes[], void *alloc_ctxs[])
2066930d 537{
c6a41e32
JM
538 struct soc_camera_device *icd = soc_camera_from_vb2q(vq);
539 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
540 struct mx2_camera_dev *pcdev = ici->priv;
2066930d 541
c6a41e32
JM
542 dev_dbg(icd->parent, "count=%d, size=%d\n", *count, sizes[0]);
543
544 /* TODO: support for VIDIOC_CREATE_BUFS not ready */
545 if (fmt != NULL)
546 return -ENOTTY;
2066930d 547
c6a41e32
JM
548 alloc_ctxs[0] = pcdev->alloc_ctx;
549
2b61d46e 550 sizes[0] = icd->sizeimage;
2066930d
BS
551
552 if (0 == *count)
553 *count = 32;
c6a41e32
JM
554 if (!*num_planes &&
555 sizes[0] * *count > MAX_VIDEO_MEM * 1024 * 1024)
556 *count = (MAX_VIDEO_MEM * 1024 * 1024) / sizes[0];
2066930d 557
c6a41e32 558 *num_planes = 1;
2066930d 559
c6a41e32 560 return 0;
2066930d
BS
561}
562
c6a41e32 563static int mx2_videobuf_prepare(struct vb2_buffer *vb)
2066930d 564{
c6a41e32 565 struct soc_camera_device *icd = soc_camera_from_vb2q(vb->vb2_queue);
2066930d
BS
566 int ret = 0;
567
c6a41e32
JM
568 dev_dbg(icd->parent, "%s (vb=0x%p) 0x%p %lu\n", __func__,
569 vb, vb2_plane_vaddr(vb, 0), vb2_get_plane_payload(vb, 0));
2066930d 570
2066930d
BS
571#ifdef DEBUG
572 /*
573 * This can be useful if you want to see if we actually fill
574 * the buffer with something
575 */
c6a41e32
JM
576 memset((void *)vb2_plane_vaddr(vb, 0),
577 0xaa, vb2_get_plane_payload(vb, 0));
2066930d
BS
578#endif
579
2b61d46e 580 vb2_set_plane_payload(vb, 0, icd->sizeimage);
c6a41e32
JM
581 if (vb2_plane_vaddr(vb, 0) &&
582 vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0)) {
2066930d
BS
583 ret = -EINVAL;
584 goto out;
585 }
586
2066930d
BS
587 return 0;
588
2066930d
BS
589out:
590 return ret;
591}
592
c6a41e32 593static void mx2_videobuf_queue(struct vb2_buffer *vb)
2066930d 594{
c6a41e32 595 struct soc_camera_device *icd = soc_camera_from_vb2q(vb->vb2_queue);
2066930d 596 struct soc_camera_host *ici =
7dfff953 597 to_soc_camera_host(icd->parent);
2066930d
BS
598 struct mx2_camera_dev *pcdev = ici->priv;
599 struct mx2_buffer *buf = container_of(vb, struct mx2_buffer, vb);
600 unsigned long flags;
601
c6a41e32
JM
602 dev_dbg(icd->parent, "%s (vb=0x%p) 0x%p %lu\n", __func__,
603 vb, vb2_plane_vaddr(vb, 0), vb2_get_plane_payload(vb, 0));
2066930d
BS
604
605 spin_lock_irqsave(&pcdev->lock, flags);
606
c6a41e32 607 buf->state = MX2_STATE_QUEUED;
560fab62 608 list_add_tail(&buf->internal.queue, &pcdev->capture);
2066930d 609
2c9ba37d 610 if (cpu_is_mx25()) {
2066930d
BS
611 u32 csicr3, dma_inten = 0;
612
613 if (pcdev->fb1_active == NULL) {
c6a41e32 614 writel(vb2_dma_contig_plane_dma_addr(vb, 0),
2066930d
BS
615 pcdev->base_csi + CSIDMASA_FB1);
616 pcdev->fb1_active = buf;
617 dma_inten = CSICR1_FB1_DMA_INTEN;
618 } else if (pcdev->fb2_active == NULL) {
c6a41e32 619 writel(vb2_dma_contig_plane_dma_addr(vb, 0),
2066930d
BS
620 pcdev->base_csi + CSIDMASA_FB2);
621 pcdev->fb2_active = buf;
622 dma_inten = CSICR1_FB2_DMA_INTEN;
623 }
624
625 if (dma_inten) {
560fab62 626 list_del(&buf->internal.queue);
c6a41e32 627 buf->state = MX2_STATE_ACTIVE;
2066930d
BS
628
629 csicr3 = readl(pcdev->base_csi + CSICR3);
630
631 /* Reflash DMA */
632 writel(csicr3 | CSICR3_DMA_REFLASH_RFF,
633 pcdev->base_csi + CSICR3);
634
635 /* clear & enable interrupts */
636 writel(dma_inten, pcdev->base_csi + CSISR);
637 pcdev->csicr1 |= dma_inten;
638 writel(pcdev->csicr1, pcdev->base_csi + CSICR1);
639
640 /* enable DMA */
641 csicr3 |= CSICR3_DMA_REQ_EN_RFF | CSICR3_RXFF_LEVEL(1);
642 writel(csicr3, pcdev->base_csi + CSICR3);
643 }
644 }
645
2066930d
BS
646 spin_unlock_irqrestore(&pcdev->lock, flags);
647}
648
c6a41e32 649static void mx2_videobuf_release(struct vb2_buffer *vb)
2066930d 650{
c6a41e32 651 struct soc_camera_device *icd = soc_camera_from_vb2q(vb->vb2_queue);
7dfff953 652 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
2066930d
BS
653 struct mx2_camera_dev *pcdev = ici->priv;
654 struct mx2_buffer *buf = container_of(vb, struct mx2_buffer, vb);
655 unsigned long flags;
656
657#ifdef DEBUG
c6a41e32
JM
658 dev_dbg(icd->parent, "%s (vb=0x%p) 0x%p %lu\n", __func__,
659 vb, vb2_plane_vaddr(vb, 0), vb2_get_plane_payload(vb, 0));
2066930d 660
c6a41e32
JM
661 switch (buf->state) {
662 case MX2_STATE_ACTIVE:
7dfff953 663 dev_info(icd->parent, "%s (active)\n", __func__);
2066930d 664 break;
c6a41e32 665 case MX2_STATE_QUEUED:
7dfff953 666 dev_info(icd->parent, "%s (queued)\n", __func__);
2066930d 667 break;
2066930d 668 default:
7dfff953 669 dev_info(icd->parent, "%s (unknown) %d\n", __func__,
c6a41e32 670 buf->state);
2066930d
BS
671 break;
672 }
673#endif
674
675 /*
676 * Terminate only queued but inactive buffers. Active buffers are
677 * released when they become inactive after videobuf_waiton().
678 *
7c6b7319
BS
679 * FIXME: implement forced termination of active buffers for mx27 and
680 * mx27 eMMA, so that the user won't get stuck in an uninterruptible
681 * state. This requires a specific handling for each of the these DMA
682 * types.
2066930d 683 */
c6a41e32 684
2066930d 685 spin_lock_irqsave(&pcdev->lock, flags);
c6a41e32 686 if (cpu_is_mx25() && buf->state == MX2_STATE_ACTIVE) {
7c6b7319
BS
687 if (pcdev->fb1_active == buf) {
688 pcdev->csicr1 &= ~CSICR1_FB1_DMA_INTEN;
689 writel(0, pcdev->base_csi + CSIDMASA_FB1);
690 pcdev->fb1_active = NULL;
691 } else if (pcdev->fb2_active == buf) {
692 pcdev->csicr1 &= ~CSICR1_FB2_DMA_INTEN;
693 writel(0, pcdev->base_csi + CSIDMASA_FB2);
694 pcdev->fb2_active = NULL;
695 }
696 writel(pcdev->csicr1, pcdev->base_csi + CSICR1);
2066930d
BS
697 }
698 spin_unlock_irqrestore(&pcdev->lock, flags);
2066930d
BS
699}
700
cdc9d6f1
JM
701static void mx27_camera_emma_buf_init(struct soc_camera_device *icd,
702 int bytesperline)
703{
704 struct soc_camera_host *ici =
705 to_soc_camera_host(icd->parent);
706 struct mx2_camera_dev *pcdev = ici->priv;
707 struct mx2_fmt_cfg *prp = pcdev->emma_prp;
708
750a6dff 709 writel((pcdev->s_width << 16) | pcdev->s_height,
cdc9d6f1
JM
710 pcdev->base_emma + PRP_SRC_FRAME_SIZE);
711 writel(prp->cfg.src_pixel,
712 pcdev->base_emma + PRP_SRC_PIXEL_FORMAT_CNTL);
713 if (prp->cfg.channel == 1) {
714 writel((icd->user_width << 16) | icd->user_height,
715 pcdev->base_emma + PRP_CH1_OUT_IMAGE_SIZE);
716 writel(bytesperline,
717 pcdev->base_emma + PRP_DEST_CH1_LINE_STRIDE);
718 writel(prp->cfg.ch1_pixel,
719 pcdev->base_emma + PRP_CH1_PIXEL_FORMAT_CNTL);
720 } else { /* channel 2 */
721 writel((icd->user_width << 16) | icd->user_height,
722 pcdev->base_emma + PRP_CH2_OUT_IMAGE_SIZE);
723 }
724
725 /* Enable interrupts */
726 writel(prp->cfg.irq_flags, pcdev->base_emma + PRP_INTR_CNTL);
727}
728
750a6dff
JM
729static void mx2_prp_resize_commit(struct mx2_camera_dev *pcdev)
730{
731 int dir;
732
733 for (dir = RESIZE_DIR_H; dir <= RESIZE_DIR_V; dir++) {
734 unsigned char *s = pcdev->resizing[dir].s;
735 int len = pcdev->resizing[dir].len;
736 unsigned int coeff[2] = {0, 0};
737 unsigned int valid = 0;
738 int i;
739
740 if (len == 0)
741 continue;
742
743 for (i = RESIZE_NUM_MAX - 1; i >= 0; i--) {
744 int j;
745
746 j = i > 9 ? 1 : 0;
747 coeff[j] = (coeff[j] << BC_COEF) |
748 (s[i] & (SZ_COEF - 1));
749
750 if (i == 5 || i == 15)
751 coeff[j] <<= 1;
752
753 valid = (valid << 1) | (s[i] >> BC_COEF);
754 }
755
756 valid |= PRP_RZ_VALID_TBL_LEN(len);
757
758 if (pcdev->resizing[dir].algo == RESIZE_ALGO_BILINEAR)
759 valid |= PRP_RZ_VALID_BILINEAR;
760
761 if (pcdev->emma_prp->cfg.channel == 1) {
762 if (dir == RESIZE_DIR_H) {
763 writel(coeff[0], pcdev->base_emma +
764 PRP_CH1_RZ_HORI_COEF1);
765 writel(coeff[1], pcdev->base_emma +
766 PRP_CH1_RZ_HORI_COEF2);
767 writel(valid, pcdev->base_emma +
768 PRP_CH1_RZ_HORI_VALID);
769 } else {
770 writel(coeff[0], pcdev->base_emma +
771 PRP_CH1_RZ_VERT_COEF1);
772 writel(coeff[1], pcdev->base_emma +
773 PRP_CH1_RZ_VERT_COEF2);
774 writel(valid, pcdev->base_emma +
775 PRP_CH1_RZ_VERT_VALID);
776 }
777 } else {
778 if (dir == RESIZE_DIR_H) {
779 writel(coeff[0], pcdev->base_emma +
780 PRP_CH2_RZ_HORI_COEF1);
781 writel(coeff[1], pcdev->base_emma +
782 PRP_CH2_RZ_HORI_COEF2);
783 writel(valid, pcdev->base_emma +
784 PRP_CH2_RZ_HORI_VALID);
785 } else {
786 writel(coeff[0], pcdev->base_emma +
787 PRP_CH2_RZ_VERT_COEF1);
788 writel(coeff[1], pcdev->base_emma +
789 PRP_CH2_RZ_VERT_COEF2);
790 writel(valid, pcdev->base_emma +
791 PRP_CH2_RZ_VERT_VALID);
792 }
793 }
794 }
795}
796
fb51cbdd
JM
797static int mx2_start_streaming(struct vb2_queue *q, unsigned int count)
798{
799 struct soc_camera_device *icd = soc_camera_from_vb2q(q);
800 struct soc_camera_host *ici =
801 to_soc_camera_host(icd->parent);
802 struct mx2_camera_dev *pcdev = ici->priv;
803 struct mx2_fmt_cfg *prp = pcdev->emma_prp;
cdc9d6f1
JM
804 struct vb2_buffer *vb;
805 struct mx2_buffer *buf;
806 unsigned long phys;
807 int bytesperline;
fb51cbdd
JM
808
809 if (cpu_is_mx27()) {
810 unsigned long flags;
811 if (count < 2)
812 return -EINVAL;
813
814 spin_lock_irqsave(&pcdev->lock, flags);
cdc9d6f1 815
a654ba16 816 buf = list_first_entry(&pcdev->capture, struct mx2_buffer,
560fab62
JM
817 internal.queue);
818 buf->internal.bufnum = 0;
cdc9d6f1
JM
819 vb = &buf->vb;
820 buf->state = MX2_STATE_ACTIVE;
821
822 phys = vb2_dma_contig_plane_dma_addr(vb, 0);
560fab62 823 mx27_update_emma_buf(pcdev, phys, buf->internal.bufnum);
cdc9d6f1
JM
824 list_move_tail(pcdev->capture.next, &pcdev->active_bufs);
825
a654ba16 826 buf = list_first_entry(&pcdev->capture, struct mx2_buffer,
560fab62
JM
827 internal.queue);
828 buf->internal.bufnum = 1;
cdc9d6f1
JM
829 vb = &buf->vb;
830 buf->state = MX2_STATE_ACTIVE;
831
832 phys = vb2_dma_contig_plane_dma_addr(vb, 0);
560fab62 833 mx27_update_emma_buf(pcdev, phys, buf->internal.bufnum);
cdc9d6f1
JM
834 list_move_tail(pcdev->capture.next, &pcdev->active_bufs);
835
836 bytesperline = soc_mbus_bytes_per_line(icd->user_width,
837 icd->current_fmt->host_fmt);
838 if (bytesperline < 0)
839 return bytesperline;
840
841 /*
842 * I didn't manage to properly enable/disable the prp
843 * on a per frame basis during running transfers,
844 * thus we allocate a buffer here and use it to
845 * discard frames when no buffer is available.
846 * Feel free to work on this ;)
847 */
848 pcdev->discard_size = icd->user_height * bytesperline;
849 pcdev->discard_buffer = dma_alloc_coherent(ici->v4l2_dev.dev,
850 pcdev->discard_size, &pcdev->discard_buffer_dma,
851 GFP_KERNEL);
852 if (!pcdev->discard_buffer)
853 return -ENOMEM;
854
855 pcdev->buf_discard[0].discard = true;
856 list_add_tail(&pcdev->buf_discard[0].queue,
857 &pcdev->discard);
858
859 pcdev->buf_discard[1].discard = true;
860 list_add_tail(&pcdev->buf_discard[1].queue,
861 &pcdev->discard);
862
750a6dff
JM
863 mx2_prp_resize_commit(pcdev);
864
cdc9d6f1
JM
865 mx27_camera_emma_buf_init(icd, bytesperline);
866
fb51cbdd
JM
867 if (prp->cfg.channel == 1) {
868 writel(PRP_CNTL_CH1EN |
869 PRP_CNTL_CSIEN |
870 prp->cfg.in_fmt |
871 prp->cfg.out_fmt |
872 PRP_CNTL_CH1_LEN |
873 PRP_CNTL_CH1BYP |
874 PRP_CNTL_CH1_TSKIP(0) |
875 PRP_CNTL_IN_TSKIP(0),
876 pcdev->base_emma + PRP_CNTL);
877 } else {
878 writel(PRP_CNTL_CH2EN |
879 PRP_CNTL_CSIEN |
880 prp->cfg.in_fmt |
881 prp->cfg.out_fmt |
882 PRP_CNTL_CH2_LEN |
883 PRP_CNTL_CH2_TSKIP(0) |
884 PRP_CNTL_IN_TSKIP(0),
885 pcdev->base_emma + PRP_CNTL);
886 }
887 spin_unlock_irqrestore(&pcdev->lock, flags);
888 }
889
890 return 0;
891}
892
893static int mx2_stop_streaming(struct vb2_queue *q)
894{
895 struct soc_camera_device *icd = soc_camera_from_vb2q(q);
896 struct soc_camera_host *ici =
897 to_soc_camera_host(icd->parent);
898 struct mx2_camera_dev *pcdev = ici->priv;
899 struct mx2_fmt_cfg *prp = pcdev->emma_prp;
900 unsigned long flags;
cdc9d6f1 901 void *b;
fb51cbdd
JM
902 u32 cntl;
903
fb51cbdd 904 if (cpu_is_mx27()) {
cdc9d6f1
JM
905 spin_lock_irqsave(&pcdev->lock, flags);
906
fb51cbdd
JM
907 cntl = readl(pcdev->base_emma + PRP_CNTL);
908 if (prp->cfg.channel == 1) {
909 writel(cntl & ~PRP_CNTL_CH1EN,
910 pcdev->base_emma + PRP_CNTL);
911 } else {
912 writel(cntl & ~PRP_CNTL_CH2EN,
913 pcdev->base_emma + PRP_CNTL);
914 }
cdc9d6f1
JM
915 INIT_LIST_HEAD(&pcdev->capture);
916 INIT_LIST_HEAD(&pcdev->active_bufs);
917 INIT_LIST_HEAD(&pcdev->discard);
918
919 b = pcdev->discard_buffer;
920 pcdev->discard_buffer = NULL;
921
922 spin_unlock_irqrestore(&pcdev->lock, flags);
923
924 dma_free_coherent(ici->v4l2_dev.dev,
925 pcdev->discard_size, b, pcdev->discard_buffer_dma);
fb51cbdd 926 }
fb51cbdd
JM
927
928 return 0;
929}
930
c6a41e32 931static struct vb2_ops mx2_videobuf_ops = {
fb51cbdd
JM
932 .queue_setup = mx2_videobuf_setup,
933 .buf_prepare = mx2_videobuf_prepare,
934 .buf_queue = mx2_videobuf_queue,
935 .buf_cleanup = mx2_videobuf_release,
936 .start_streaming = mx2_start_streaming,
937 .stop_streaming = mx2_stop_streaming,
2066930d
BS
938};
939
c6a41e32 940static int mx2_camera_init_videobuf(struct vb2_queue *q,
2066930d
BS
941 struct soc_camera_device *icd)
942{
c6a41e32
JM
943 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
944 q->io_modes = VB2_MMAP | VB2_USERPTR;
945 q->drv_priv = icd;
946 q->ops = &mx2_videobuf_ops;
947 q->mem_ops = &vb2_dma_contig_memops;
948 q->buf_struct_size = sizeof(struct mx2_buffer);
949
950 return vb2_queue_init(q);
2066930d
BS
951}
952
db592a24
GL
953#define MX2_BUS_FLAGS (V4L2_MBUS_MASTER | \
954 V4L2_MBUS_VSYNC_ACTIVE_HIGH | \
955 V4L2_MBUS_VSYNC_ACTIVE_LOW | \
956 V4L2_MBUS_HSYNC_ACTIVE_HIGH | \
957 V4L2_MBUS_HSYNC_ACTIVE_LOW | \
958 V4L2_MBUS_PCLK_SAMPLE_RISING | \
959 V4L2_MBUS_PCLK_SAMPLE_FALLING | \
960 V4L2_MBUS_DATA_ACTIVE_HIGH | \
961 V4L2_MBUS_DATA_ACTIVE_LOW)
2066930d
BS
962
963static int mx27_camera_emma_prp_reset(struct mx2_camera_dev *pcdev)
964{
965 u32 cntl;
966 int count = 0;
967
968 cntl = readl(pcdev->base_emma + PRP_CNTL);
969 writel(PRP_CNTL_SWRST, pcdev->base_emma + PRP_CNTL);
970 while (count++ < 100) {
971 if (!(readl(pcdev->base_emma + PRP_CNTL) & PRP_CNTL_SWRST))
972 return 0;
973 barrier();
974 udelay(1);
975 }
976
977 return -ETIMEDOUT;
978}
979
8843d119 980static int mx2_camera_set_bus_param(struct soc_camera_device *icd)
2066930d 981{
db592a24
GL
982 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
983 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
2066930d 984 struct mx2_camera_dev *pcdev = ici->priv;
db592a24 985 struct v4l2_mbus_config cfg = {.type = V4L2_MBUS_PARALLEL,};
d509835e 986 const struct soc_camera_format_xlate *xlate;
db592a24
GL
987 unsigned long common_flags;
988 int ret;
2066930d
BS
989 int bytesperline;
990 u32 csicr1 = pcdev->csicr1;
991
db592a24
GL
992 ret = v4l2_subdev_call(sd, video, g_mbus_config, &cfg);
993 if (!ret) {
994 common_flags = soc_mbus_config_compatible(&cfg, MX2_BUS_FLAGS);
995 if (!common_flags) {
996 dev_warn(icd->parent,
997 "Flags incompatible: camera 0x%x, host 0x%x\n",
998 cfg.flags, MX2_BUS_FLAGS);
999 return -EINVAL;
1000 }
1001 } else if (ret != -ENOIOCTLCMD) {
1002 return ret;
1003 } else {
1004 common_flags = MX2_BUS_FLAGS;
1005 }
2066930d 1006
db592a24
GL
1007 if ((common_flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH) &&
1008 (common_flags & V4L2_MBUS_HSYNC_ACTIVE_LOW)) {
2066930d 1009 if (pcdev->platform_flags & MX2_CAMERA_HSYNC_HIGH)
db592a24 1010 common_flags &= ~V4L2_MBUS_HSYNC_ACTIVE_LOW;
2066930d 1011 else
db592a24 1012 common_flags &= ~V4L2_MBUS_HSYNC_ACTIVE_HIGH;
2066930d
BS
1013 }
1014
db592a24
GL
1015 if ((common_flags & V4L2_MBUS_PCLK_SAMPLE_RISING) &&
1016 (common_flags & V4L2_MBUS_PCLK_SAMPLE_FALLING)) {
2066930d 1017 if (pcdev->platform_flags & MX2_CAMERA_PCLK_SAMPLE_RISING)
db592a24 1018 common_flags &= ~V4L2_MBUS_PCLK_SAMPLE_FALLING;
2066930d 1019 else
db592a24 1020 common_flags &= ~V4L2_MBUS_PCLK_SAMPLE_RISING;
2066930d
BS
1021 }
1022
db592a24
GL
1023 cfg.flags = common_flags;
1024 ret = v4l2_subdev_call(sd, video, s_mbus_config, &cfg);
1025 if (ret < 0 && ret != -ENOIOCTLCMD) {
1026 dev_dbg(icd->parent, "camera s_mbus_config(0x%lx) returned %d\n",
1027 common_flags, ret);
2066930d 1028 return ret;
db592a24 1029 }
2066930d 1030
d509835e
JM
1031 xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
1032 if (!xlate) {
1033 dev_warn(icd->parent, "Format %x not found\n", pixfmt);
1034 return -EINVAL;
1035 }
1036
1037 if (xlate->code == V4L2_MBUS_FMT_YUYV8_2X8) {
1038 csicr1 |= CSICR1_PACK_DIR;
1039 csicr1 &= ~CSICR1_SWAP16_EN;
1040 dev_dbg(icd->parent, "already yuyv format, don't convert\n");
1041 } else if (xlate->code == V4L2_MBUS_FMT_UYVY8_2X8) {
1042 csicr1 &= ~CSICR1_PACK_DIR;
1043 csicr1 |= CSICR1_SWAP16_EN;
1044 dev_dbg(icd->parent, "convert uyvy mbus format into yuyv\n");
1045 } else {
1046 dev_warn(icd->parent, "mbus format not supported\n");
1047 return -EINVAL;
1048 }
1049
db592a24 1050 if (common_flags & V4L2_MBUS_PCLK_SAMPLE_RISING)
d86097e1 1051 csicr1 |= CSICR1_REDGE;
db592a24 1052 if (common_flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH)
2066930d 1053 csicr1 |= CSICR1_SOF_POL;
db592a24 1054 if (common_flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH)
2066930d 1055 csicr1 |= CSICR1_HSYNC_POL;
2066930d
BS
1056 if (pcdev->platform_flags & MX2_CAMERA_EXT_VSYNC)
1057 csicr1 |= CSICR1_EXT_VSYNC;
1058 if (pcdev->platform_flags & MX2_CAMERA_CCIR)
1059 csicr1 |= CSICR1_CCIR_EN;
1060 if (pcdev->platform_flags & MX2_CAMERA_CCIR_INTERLACE)
1061 csicr1 |= CSICR1_CCIR_MODE;
1062 if (pcdev->platform_flags & MX2_CAMERA_GATED_CLOCK)
1063 csicr1 |= CSICR1_GCLK_MODE;
1064 if (pcdev->platform_flags & MX2_CAMERA_INV_DATA)
1065 csicr1 |= CSICR1_INV_DATA;
2066930d
BS
1066
1067 pcdev->csicr1 = csicr1;
1068
1069 bytesperline = soc_mbus_bytes_per_line(icd->user_width,
1070 icd->current_fmt->host_fmt);
1071 if (bytesperline < 0)
1072 return bytesperline;
1073
2c9ba37d 1074 if (cpu_is_mx27()) {
2066930d
BS
1075 ret = mx27_camera_emma_prp_reset(pcdev);
1076 if (ret)
1077 return ret;
2066930d
BS
1078 } else if (cpu_is_mx25()) {
1079 writel((bytesperline * icd->user_height) >> 2,
1080 pcdev->base_csi + CSIRXCNT);
1081 writel((bytesperline << 16) | icd->user_height,
1082 pcdev->base_csi + CSIIMAG_PARA);
1083 }
1084
1085 writel(pcdev->csicr1, pcdev->base_csi + CSICR1);
1086
1087 return 0;
1088}
1089
1090static int mx2_camera_set_crop(struct soc_camera_device *icd,
1091 struct v4l2_crop *a)
1092{
1093 struct v4l2_rect *rect = &a->c;
1094 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1095 struct v4l2_mbus_framefmt mf;
1096 int ret;
1097
1098 soc_camera_limit_side(&rect->left, &rect->width, 0, 2, 4096);
1099 soc_camera_limit_side(&rect->top, &rect->height, 0, 2, 4096);
1100
1101 ret = v4l2_subdev_call(sd, video, s_crop, a);
1102 if (ret < 0)
1103 return ret;
1104
1105 /* The capture device might have changed its output */
1106 ret = v4l2_subdev_call(sd, video, g_mbus_fmt, &mf);
1107 if (ret < 0)
1108 return ret;
1109
7dfff953 1110 dev_dbg(icd->parent, "Sensor cropped %dx%d\n",
2066930d
BS
1111 mf.width, mf.height);
1112
1113 icd->user_width = mf.width;
1114 icd->user_height = mf.height;
1115
1116 return ret;
1117}
1118
f410991d
JM
1119static int mx2_camera_get_formats(struct soc_camera_device *icd,
1120 unsigned int idx,
1121 struct soc_camera_format_xlate *xlate)
1122{
1123 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1124 const struct soc_mbus_pixelfmt *fmt;
1125 struct device *dev = icd->parent;
1126 enum v4l2_mbus_pixelcode code;
1127 int ret, formats = 0;
1128
1129 ret = v4l2_subdev_call(sd, video, enum_mbus_fmt, idx, &code);
1130 if (ret < 0)
1131 /* no more formats */
1132 return 0;
1133
1134 fmt = soc_mbus_get_fmtdesc(code);
1135 if (!fmt) {
1136 dev_err(dev, "Invalid format code #%u: %d\n", idx, code);
1137 return 0;
1138 }
1139
d509835e
JM
1140 if (code == V4L2_MBUS_FMT_YUYV8_2X8 ||
1141 code == V4L2_MBUS_FMT_UYVY8_2X8) {
f410991d
JM
1142 formats++;
1143 if (xlate) {
1144 /*
1145 * CH2 can output YUV420 which is a standard format in
1146 * soc_mediabus.c
1147 */
1148 xlate->host_fmt =
1149 soc_mbus_get_fmtdesc(V4L2_MBUS_FMT_YUYV8_1_5X8);
1150 xlate->code = code;
1151 dev_dbg(dev, "Providing host format %s for sensor code %d\n",
1152 xlate->host_fmt->name, code);
1153 xlate++;
1154 }
1155 }
1156
d509835e
JM
1157 if (code == V4L2_MBUS_FMT_UYVY8_2X8) {
1158 formats++;
1159 if (xlate) {
1160 xlate->host_fmt =
1161 soc_mbus_get_fmtdesc(V4L2_MBUS_FMT_YUYV8_2X8);
1162 xlate->code = code;
1163 dev_dbg(dev, "Providing host format %s for sensor code %d\n",
1164 xlate->host_fmt->name, code);
1165 xlate++;
1166 }
1167 }
1168
f410991d
JM
1169 /* Generic pass-trough */
1170 formats++;
1171 if (xlate) {
1172 xlate->host_fmt = fmt;
1173 xlate->code = code;
1174 xlate++;
1175 }
1176 return formats;
1177}
1178
750a6dff
JM
1179static int mx2_emmaprp_resize(struct mx2_camera_dev *pcdev,
1180 struct v4l2_mbus_framefmt *mf_in,
1181 struct v4l2_pix_format *pix_out, bool apply)
1182{
1183 int num, den;
1184 unsigned long m;
1185 int i, dir;
1186
1187 for (dir = RESIZE_DIR_H; dir <= RESIZE_DIR_V; dir++) {
1188 struct emma_prp_resize tmprsz;
1189 unsigned char *s = tmprsz.s;
1190 int len = 0;
1191 int in, out;
1192
1193 if (dir == RESIZE_DIR_H) {
1194 in = mf_in->width;
1195 out = pix_out->width;
1196 } else {
1197 in = mf_in->height;
1198 out = pix_out->height;
1199 }
1200
1201 if (in < out)
1202 return -EINVAL;
1203 else if (in == out)
1204 continue;
1205
1206 /* Calculate ratio */
1207 m = gcd(in, out);
1208 num = in / m;
1209 den = out / m;
1210 if (num > RESIZE_NUM_MAX)
1211 return -EINVAL;
1212
1213 if ((num >= 2 * den) && (den == 1) &&
1214 (num < 9) && (!(num & 0x01))) {
1215 int sum = 0;
1216 int j;
1217
1218 /* Average scaling for >= 2:1 ratios */
1219 /* Support can be added for num >=9 and odd values */
1220
1221 tmprsz.algo = RESIZE_ALGO_AVERAGING;
1222 len = num;
1223
1224 for (i = 0; i < (len / 2); i++)
1225 s[i] = 8;
1226
1227 do {
1228 for (i = 0; i < (len / 2); i++) {
1229 s[i] = s[i] >> 1;
1230 sum = 0;
1231 for (j = 0; j < (len / 2); j++)
1232 sum += s[j];
1233 if (sum == 4)
1234 break;
1235 }
1236 } while (sum != 4);
1237
1238 for (i = (len / 2); i < len; i++)
1239 s[i] = s[len - i - 1];
1240
1241 s[len - 1] |= SZ_COEF;
1242 } else {
1243 /* bilinear scaling for < 2:1 ratios */
1244 int v; /* overflow counter */
1245 int coeff, nxt; /* table output */
1246 int in_pos_inc = 2 * den;
1247 int out_pos = num;
1248 int out_pos_inc = 2 * num;
1249 int init_carry = num - den;
1250 int carry = init_carry;
1251
1252 tmprsz.algo = RESIZE_ALGO_BILINEAR;
1253 v = den + in_pos_inc;
1254 do {
1255 coeff = v - out_pos;
1256 out_pos += out_pos_inc;
1257 carry += out_pos_inc;
1258 for (nxt = 0; v < out_pos; nxt++) {
1259 v += in_pos_inc;
1260 carry -= in_pos_inc;
1261 }
1262
1263 if (len > RESIZE_NUM_MAX)
1264 return -EINVAL;
1265
1266 coeff = ((coeff << BC_COEF) +
1267 (in_pos_inc >> 1)) / in_pos_inc;
1268
1269 if (coeff >= (SZ_COEF - 1))
1270 coeff--;
1271
1272 coeff |= SZ_COEF;
1273 s[len] = (unsigned char)coeff;
1274 len++;
1275
1276 for (i = 1; i < nxt; i++) {
1277 if (len >= RESIZE_NUM_MAX)
1278 return -EINVAL;
1279 s[len] = 0;
1280 len++;
1281 }
1282 } while (carry != init_carry);
1283 }
1284 tmprsz.len = len;
1285 if (dir == RESIZE_DIR_H)
1286 mf_in->width = pix_out->width;
1287 else
1288 mf_in->height = pix_out->height;
1289
1290 if (apply)
1291 memcpy(&pcdev->resizing[dir], &tmprsz, sizeof(tmprsz));
1292 }
1293 return 0;
1294}
1295
2066930d
BS
1296static int mx2_camera_set_fmt(struct soc_camera_device *icd,
1297 struct v4l2_format *f)
1298{
f410991d
JM
1299 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
1300 struct mx2_camera_dev *pcdev = ici->priv;
2066930d
BS
1301 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1302 const struct soc_camera_format_xlate *xlate;
1303 struct v4l2_pix_format *pix = &f->fmt.pix;
1304 struct v4l2_mbus_framefmt mf;
1305 int ret;
1306
750a6dff
JM
1307 dev_dbg(icd->parent, "%s: requested params: width = %d, height = %d\n",
1308 __func__, pix->width, pix->height);
1309
2066930d
BS
1310 xlate = soc_camera_xlate_by_fourcc(icd, pix->pixelformat);
1311 if (!xlate) {
7dfff953 1312 dev_warn(icd->parent, "Format %x not found\n",
2066930d
BS
1313 pix->pixelformat);
1314 return -EINVAL;
1315 }
1316
2066930d
BS
1317 mf.width = pix->width;
1318 mf.height = pix->height;
1319 mf.field = pix->field;
1320 mf.colorspace = pix->colorspace;
1321 mf.code = xlate->code;
1322
1323 ret = v4l2_subdev_call(sd, video, s_mbus_fmt, &mf);
1324 if (ret < 0 && ret != -ENOIOCTLCMD)
1325 return ret;
1326
750a6dff
JM
1327 /* Store width and height returned by the sensor for resizing */
1328 pcdev->s_width = mf.width;
1329 pcdev->s_height = mf.height;
1330 dev_dbg(icd->parent, "%s: sensor params: width = %d, height = %d\n",
1331 __func__, pcdev->s_width, pcdev->s_height);
1332
1333 pcdev->emma_prp = mx27_emma_prp_get_format(xlate->code,
1334 xlate->host_fmt->fourcc);
1335
1336 memset(pcdev->resizing, 0, sizeof(pcdev->resizing));
1337 if ((mf.width != pix->width || mf.height != pix->height) &&
1338 pcdev->emma_prp->cfg.in_fmt == PRP_CNTL_DATA_IN_YUV422) {
1339 if (mx2_emmaprp_resize(pcdev, &mf, pix, true) < 0)
1340 dev_dbg(icd->parent, "%s: can't resize\n", __func__);
1341 }
1342
2066930d
BS
1343 if (mf.code != xlate->code)
1344 return -EINVAL;
1345
1346 pix->width = mf.width;
1347 pix->height = mf.height;
1348 pix->field = mf.field;
1349 pix->colorspace = mf.colorspace;
1350 icd->current_fmt = xlate;
1351
750a6dff
JM
1352 dev_dbg(icd->parent, "%s: returned params: width = %d, height = %d\n",
1353 __func__, pix->width, pix->height);
f410991d 1354
2066930d
BS
1355 return 0;
1356}
1357
1358static int mx2_camera_try_fmt(struct soc_camera_device *icd,
1359 struct v4l2_format *f)
1360{
2066930d
BS
1361 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1362 const struct soc_camera_format_xlate *xlate;
1363 struct v4l2_pix_format *pix = &f->fmt.pix;
1364 struct v4l2_mbus_framefmt mf;
1365 __u32 pixfmt = pix->pixelformat;
750a6dff
JM
1366 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
1367 struct mx2_camera_dev *pcdev = ici->priv;
2066930d
BS
1368 unsigned int width_limit;
1369 int ret;
1370
750a6dff
JM
1371 dev_dbg(icd->parent, "%s: requested params: width = %d, height = %d\n",
1372 __func__, pix->width, pix->height);
1373
2066930d
BS
1374 xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
1375 if (pixfmt && !xlate) {
7dfff953 1376 dev_warn(icd->parent, "Format %x not found\n", pixfmt);
2066930d
BS
1377 return -EINVAL;
1378 }
1379
1380 /* FIXME: implement MX27 limits */
1381
2066930d
BS
1382 /* limit to MX25 hardware capabilities */
1383 if (cpu_is_mx25()) {
1384 if (xlate->host_fmt->bits_per_sample <= 8)
1385 width_limit = 0xffff * 4;
1386 else
1387 width_limit = 0xffff * 2;
1388 /* CSIIMAG_PARA limit */
1389 if (pix->width > width_limit)
1390 pix->width = width_limit;
1391 if (pix->height > 0xffff)
1392 pix->height = 0xffff;
1393
1394 pix->bytesperline = soc_mbus_bytes_per_line(pix->width,
1395 xlate->host_fmt);
1396 if (pix->bytesperline < 0)
1397 return pix->bytesperline;
1398 pix->sizeimage = pix->height * pix->bytesperline;
28281a71
GL
1399 /* Check against the CSIRXCNT limit */
1400 if (pix->sizeimage > 4 * 0x3ffff) {
1401 /* Adjust geometry, preserve aspect ratio */
1402 unsigned int new_height = int_sqrt(4 * 0x3ffff *
1403 pix->height / pix->bytesperline);
1404 pix->width = new_height * pix->width / pix->height;
1405 pix->height = new_height;
1406 pix->bytesperline = soc_mbus_bytes_per_line(pix->width,
1407 xlate->host_fmt);
1408 BUG_ON(pix->bytesperline < 0);
584943aa 1409 pix->sizeimage = pix->height * pix->bytesperline;
2066930d
BS
1410 }
1411 }
1412
1413 /* limit to sensor capabilities */
1414 mf.width = pix->width;
1415 mf.height = pix->height;
1416 mf.field = pix->field;
1417 mf.colorspace = pix->colorspace;
1418 mf.code = xlate->code;
1419
1420 ret = v4l2_subdev_call(sd, video, try_mbus_fmt, &mf);
1421 if (ret < 0)
1422 return ret;
1423
750a6dff
JM
1424 dev_dbg(icd->parent, "%s: sensor params: width = %d, height = %d\n",
1425 __func__, pcdev->s_width, pcdev->s_height);
1426
1427 /* If the sensor does not support image size try PrP resizing */
1428 pcdev->emma_prp = mx27_emma_prp_get_format(xlate->code,
1429 xlate->host_fmt->fourcc);
1430
1431 memset(pcdev->resizing, 0, sizeof(pcdev->resizing));
1432 if ((mf.width != pix->width || mf.height != pix->height) &&
1433 pcdev->emma_prp->cfg.in_fmt == PRP_CNTL_DATA_IN_YUV422) {
1434 if (mx2_emmaprp_resize(pcdev, &mf, pix, false) < 0)
1435 dev_dbg(icd->parent, "%s: can't resize\n", __func__);
1436 }
1437
2066930d
BS
1438 if (mf.field == V4L2_FIELD_ANY)
1439 mf.field = V4L2_FIELD_NONE;
f410991d
JM
1440 /*
1441 * Driver supports interlaced images provided they have
1442 * both fields so that they can be processed as if they
1443 * were progressive.
1444 */
1445 if (mf.field != V4L2_FIELD_NONE && !V4L2_FIELD_HAS_BOTH(mf.field)) {
7dfff953 1446 dev_err(icd->parent, "Field type %d unsupported.\n",
2066930d
BS
1447 mf.field);
1448 return -EINVAL;
1449 }
1450
1451 pix->width = mf.width;
1452 pix->height = mf.height;
1453 pix->field = mf.field;
1454 pix->colorspace = mf.colorspace;
1455
750a6dff
JM
1456 dev_dbg(icd->parent, "%s: returned params: width = %d, height = %d\n",
1457 __func__, pix->width, pix->height);
1458
2066930d
BS
1459 return 0;
1460}
1461
1462static int mx2_camera_querycap(struct soc_camera_host *ici,
1463 struct v4l2_capability *cap)
1464{
1465 /* cap->name is set by the friendly caller:-> */
1466 strlcpy(cap->card, MX2_CAM_DRIVER_DESCRIPTION, sizeof(cap->card));
2066930d
BS
1467 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1468
1469 return 0;
1470}
1471
2066930d
BS
1472static unsigned int mx2_camera_poll(struct file *file, poll_table *pt)
1473{
6b101926 1474 struct soc_camera_device *icd = file->private_data;
2066930d 1475
c6a41e32 1476 return vb2_poll(&icd->vb2_vidq, file, pt);
2066930d
BS
1477}
1478
1479static struct soc_camera_host_ops mx2_soc_camera_host_ops = {
1480 .owner = THIS_MODULE,
1481 .add = mx2_camera_add_device,
1482 .remove = mx2_camera_remove_device,
1483 .set_fmt = mx2_camera_set_fmt,
1484 .set_crop = mx2_camera_set_crop,
f410991d 1485 .get_formats = mx2_camera_get_formats,
2066930d 1486 .try_fmt = mx2_camera_try_fmt,
c6a41e32 1487 .init_videobuf2 = mx2_camera_init_videobuf,
2066930d
BS
1488 .poll = mx2_camera_poll,
1489 .querycap = mx2_camera_querycap,
1490 .set_bus_param = mx2_camera_set_bus_param,
1491};
1492
1493static void mx27_camera_frame_done_emma(struct mx2_camera_dev *pcdev,
d84279e6 1494 int bufnum, bool err)
2066930d 1495{
8886a881 1496#ifdef DEBUG
f410991d 1497 struct mx2_fmt_cfg *prp = pcdev->emma_prp;
8886a881 1498#endif
560fab62 1499 struct mx2_buf_internal *ibuf;
2066930d 1500 struct mx2_buffer *buf;
c6a41e32 1501 struct vb2_buffer *vb;
2066930d
BS
1502 unsigned long phys;
1503
560fab62
JM
1504 ibuf = list_first_entry(&pcdev->active_bufs, struct mx2_buf_internal,
1505 queue);
2066930d 1506
560fab62 1507 BUG_ON(ibuf->bufnum != bufnum);
2066930d 1508
560fab62 1509 if (ibuf->discard) {
cdc9d6f1
JM
1510 /*
1511 * Discard buffer must not be returned to user space.
1512 * Just return it to the discard queue.
1513 */
1514 list_move_tail(pcdev->active_bufs.next, &pcdev->discard);
1515 } else {
560fab62
JM
1516 buf = mx2_ibuf_to_buf(ibuf);
1517
2066930d
BS
1518 vb = &buf->vb;
1519#ifdef DEBUG
c6a41e32 1520 phys = vb2_dma_contig_plane_dma_addr(vb, 0);
f410991d
JM
1521 if (prp->cfg.channel == 1) {
1522 if (readl(pcdev->base_emma + PRP_DEST_RGB1_PTR +
1523 4 * bufnum) != phys) {
8886a881
JM
1524 dev_err(pcdev->dev, "%lx != %x\n", phys,
1525 readl(pcdev->base_emma +
1526 PRP_DEST_RGB1_PTR + 4 * bufnum));
f410991d
JM
1527 }
1528 } else {
1529 if (readl(pcdev->base_emma + PRP_DEST_Y_PTR -
1530 0x14 * bufnum) != phys) {
8886a881
JM
1531 dev_err(pcdev->dev, "%lx != %x\n", phys,
1532 readl(pcdev->base_emma +
1533 PRP_DEST_Y_PTR - 0x14 * bufnum));
f410991d 1534 }
2066930d
BS
1535 }
1536#endif
c6a41e32
JM
1537 dev_dbg(pcdev->dev, "%s (vb=0x%p) 0x%p %lu\n", __func__, vb,
1538 vb2_plane_vaddr(vb, 0),
1539 vb2_get_plane_payload(vb, 0));
2066930d 1540
560fab62 1541 list_del_init(&buf->internal.queue);
c6a41e32 1542 do_gettimeofday(&vb->v4l2_buf.timestamp);
c6a41e32 1543 vb->v4l2_buf.sequence = pcdev->frame_count;
d84279e6
JM
1544 if (err)
1545 vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
1546 else
1547 vb2_buffer_done(vb, VB2_BUF_STATE_DONE);
2066930d
BS
1548 }
1549
fb51cbdd
JM
1550 pcdev->frame_count++;
1551
2066930d 1552 if (list_empty(&pcdev->capture)) {
cdc9d6f1
JM
1553 if (list_empty(&pcdev->discard)) {
1554 dev_warn(pcdev->dev, "%s: trying to access empty discard list\n",
1555 __func__);
1556 return;
f410991d 1557 }
cdc9d6f1 1558
560fab62
JM
1559 ibuf = list_first_entry(&pcdev->discard,
1560 struct mx2_buf_internal, queue);
1561 ibuf->bufnum = bufnum;
cdc9d6f1
JM
1562
1563 list_move_tail(pcdev->discard.next, &pcdev->active_bufs);
1564 mx27_update_emma_buf(pcdev, pcdev->discard_buffer_dma, bufnum);
2066930d
BS
1565 return;
1566 }
1567
560fab62
JM
1568 buf = list_first_entry(&pcdev->capture, struct mx2_buffer,
1569 internal.queue);
2066930d 1570
560fab62 1571 buf->internal.bufnum = bufnum;
2066930d
BS
1572
1573 list_move_tail(pcdev->capture.next, &pcdev->active_bufs);
1574
1575 vb = &buf->vb;
c6a41e32 1576 buf->state = MX2_STATE_ACTIVE;
2066930d 1577
c6a41e32 1578 phys = vb2_dma_contig_plane_dma_addr(vb, 0);
cdc9d6f1 1579 mx27_update_emma_buf(pcdev, phys, bufnum);
2066930d
BS
1580}
1581
1582static irqreturn_t mx27_camera_emma_irq(int irq_emma, void *data)
1583{
1584 struct mx2_camera_dev *pcdev = data;
1585 unsigned int status = readl(pcdev->base_emma + PRP_INTRSTATUS);
560fab62 1586 struct mx2_buf_internal *ibuf;
cdc9d6f1 1587
8636ead8 1588 spin_lock(&pcdev->lock);
cdc9d6f1
JM
1589
1590 if (list_empty(&pcdev->active_bufs)) {
1591 dev_warn(pcdev->dev, "%s: called while active list is empty\n",
1592 __func__);
665ad8af
JM
1593
1594 if (!status) {
1595 spin_unlock(&pcdev->lock);
1596 return IRQ_NONE;
1597 }
cdc9d6f1 1598 }
2066930d
BS
1599
1600 if (status & (1 << 7)) { /* overflow */
d84279e6 1601 u32 cntl = readl(pcdev->base_emma + PRP_CNTL);
f410991d
JM
1602 writel(cntl & ~(PRP_CNTL_CH1EN | PRP_CNTL_CH2EN),
1603 pcdev->base_emma + PRP_CNTL);
2066930d 1604 writel(cntl, pcdev->base_emma + PRP_CNTL);
d84279e6 1605
560fab62
JM
1606 ibuf = list_first_entry(&pcdev->active_bufs,
1607 struct mx2_buf_internal, queue);
d84279e6 1608 mx27_camera_frame_done_emma(pcdev,
560fab62 1609 ibuf->bufnum, true);
d84279e6
JM
1610
1611 status &= ~(1 << 7);
1612 } else if (((status & (3 << 5)) == (3 << 5)) ||
cdc9d6f1 1613 ((status & (3 << 3)) == (3 << 3))) {
2066930d
BS
1614 /*
1615 * Both buffers have triggered, process the one we're expecting
1616 * to first
1617 */
560fab62
JM
1618 ibuf = list_first_entry(&pcdev->active_bufs,
1619 struct mx2_buf_internal, queue);
1620 mx27_camera_frame_done_emma(pcdev, ibuf->bufnum, false);
1621 status &= ~(1 << (6 - ibuf->bufnum)); /* mark processed */
d84279e6
JM
1622 } else if ((status & (1 << 6)) || (status & (1 << 4))) {
1623 mx27_camera_frame_done_emma(pcdev, 0, false);
1624 } else if ((status & (1 << 5)) || (status & (1 << 3))) {
1625 mx27_camera_frame_done_emma(pcdev, 1, false);
2066930d 1626 }
2066930d 1627
8636ead8 1628 spin_unlock(&pcdev->lock);
2066930d
BS
1629 writel(status, pcdev->base_emma + PRP_INTRSTATUS);
1630
1631 return IRQ_HANDLED;
1632}
1633
1634static int __devinit mx27_camera_emma_init(struct mx2_camera_dev *pcdev)
1635{
1636 struct resource *res_emma = pcdev->res_emma;
1637 int err = 0;
1638
1639 if (!request_mem_region(res_emma->start, resource_size(res_emma),
1640 MX2_CAM_DRV_NAME)) {
1641 err = -EBUSY;
1642 goto out;
1643 }
1644
1645 pcdev->base_emma = ioremap(res_emma->start, resource_size(res_emma));
1646 if (!pcdev->base_emma) {
1647 err = -ENOMEM;
1648 goto exit_release;
1649 }
1650
1651 err = request_irq(pcdev->irq_emma, mx27_camera_emma_irq, 0,
1652 MX2_CAM_DRV_NAME, pcdev);
1653 if (err) {
1654 dev_err(pcdev->dev, "Camera EMMA interrupt register failed \n");
1655 goto exit_iounmap;
1656 }
1657
1658 pcdev->clk_emma = clk_get(NULL, "emma");
1659 if (IS_ERR(pcdev->clk_emma)) {
1660 err = PTR_ERR(pcdev->clk_emma);
1661 goto exit_free_irq;
1662 }
1663
1664 clk_enable(pcdev->clk_emma);
1665
1666 err = mx27_camera_emma_prp_reset(pcdev);
1667 if (err)
1668 goto exit_clk_emma_put;
1669
1670 return err;
1671
1672exit_clk_emma_put:
1673 clk_disable(pcdev->clk_emma);
1674 clk_put(pcdev->clk_emma);
1675exit_free_irq:
1676 free_irq(pcdev->irq_emma, pcdev);
1677exit_iounmap:
1678 iounmap(pcdev->base_emma);
1679exit_release:
1680 release_mem_region(res_emma->start, resource_size(res_emma));
1681out:
1682 return err;
1683}
1684
1685static int __devinit mx2_camera_probe(struct platform_device *pdev)
1686{
1687 struct mx2_camera_dev *pcdev;
1688 struct resource *res_csi, *res_emma;
1689 void __iomem *base_csi;
1690 int irq_csi, irq_emma;
2066930d
BS
1691 int err = 0;
1692
1693 dev_dbg(&pdev->dev, "initialising\n");
1694
1695 res_csi = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1696 irq_csi = platform_get_irq(pdev, 0);
1697 if (res_csi == NULL || irq_csi < 0) {
1698 dev_err(&pdev->dev, "Missing platform resources data\n");
1699 err = -ENODEV;
1700 goto exit;
1701 }
1702
1703 pcdev = kzalloc(sizeof(*pcdev), GFP_KERNEL);
1704 if (!pcdev) {
1705 dev_err(&pdev->dev, "Could not allocate pcdev\n");
1706 err = -ENOMEM;
1707 goto exit;
1708 }
1709
1710 pcdev->clk_csi = clk_get(&pdev->dev, NULL);
1711 if (IS_ERR(pcdev->clk_csi)) {
d867be1c 1712 dev_err(&pdev->dev, "Could not get csi clock\n");
2066930d
BS
1713 err = PTR_ERR(pcdev->clk_csi);
1714 goto exit_kfree;
1715 }
1716
2066930d
BS
1717 pcdev->res_csi = res_csi;
1718 pcdev->pdata = pdev->dev.platform_data;
1719 if (pcdev->pdata) {
1720 long rate;
1721
1722 pcdev->platform_flags = pcdev->pdata->flags;
1723
1724 rate = clk_round_rate(pcdev->clk_csi, pcdev->pdata->clk * 2);
1725 if (rate <= 0) {
1726 err = -ENODEV;
1727 goto exit_dma_free;
1728 }
1729 err = clk_set_rate(pcdev->clk_csi, rate);
1730 if (err < 0)
1731 goto exit_dma_free;
1732 }
1733
1734 INIT_LIST_HEAD(&pcdev->capture);
1735 INIT_LIST_HEAD(&pcdev->active_bufs);
cdc9d6f1 1736 INIT_LIST_HEAD(&pcdev->discard);
2066930d
BS
1737 spin_lock_init(&pcdev->lock);
1738
1739 /*
1740 * Request the regions.
1741 */
1742 if (!request_mem_region(res_csi->start, resource_size(res_csi),
1743 MX2_CAM_DRV_NAME)) {
1744 err = -EBUSY;
1745 goto exit_dma_free;
1746 }
1747
1748 base_csi = ioremap(res_csi->start, resource_size(res_csi));
1749 if (!base_csi) {
1750 err = -ENOMEM;
1751 goto exit_release;
1752 }
1753 pcdev->irq_csi = irq_csi;
1754 pcdev->base_csi = base_csi;
1755 pcdev->base_dma = res_csi->start;
1756 pcdev->dev = &pdev->dev;
1757
2c9ba37d
SH
1758 if (cpu_is_mx25()) {
1759 err = request_irq(pcdev->irq_csi, mx25_camera_irq, 0,
1760 MX2_CAM_DRV_NAME, pcdev);
1761 if (err) {
1762 dev_err(pcdev->dev, "Camera interrupt register failed \n");
1763 goto exit_iounmap;
1764 }
2066930d
BS
1765 }
1766
1767 if (cpu_is_mx27()) {
1768 /* EMMA support */
1769 res_emma = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1770 irq_emma = platform_get_irq(pdev, 1);
1771
2c9ba37d
SH
1772 if (!res_emma || !irq_emma) {
1773 dev_err(&pdev->dev, "no EMMA resources\n");
1774 goto exit_free_irq;
2066930d 1775 }
2c9ba37d
SH
1776
1777 pcdev->res_emma = res_emma;
1778 pcdev->irq_emma = irq_emma;
1779 if (mx27_camera_emma_init(pcdev))
1780 goto exit_free_irq;
2066930d
BS
1781 }
1782
1783 pcdev->soc_host.drv_name = MX2_CAM_DRV_NAME,
1784 pcdev->soc_host.ops = &mx2_soc_camera_host_ops,
1785 pcdev->soc_host.priv = pcdev;
1786 pcdev->soc_host.v4l2_dev.dev = &pdev->dev;
1787 pcdev->soc_host.nr = pdev->id;
c6a41e32
JM
1788
1789 pcdev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
1790 if (IS_ERR(pcdev->alloc_ctx)) {
1791 err = PTR_ERR(pcdev->alloc_ctx);
1792 goto eallocctx;
1793 }
2066930d
BS
1794 err = soc_camera_host_register(&pcdev->soc_host);
1795 if (err)
1796 goto exit_free_emma;
1797
45f4d4e8
MG
1798 dev_info(&pdev->dev, "MX2 Camera (CSI) driver probed, clock frequency: %ld\n",
1799 clk_get_rate(pcdev->clk_csi));
1800
2066930d
BS
1801 return 0;
1802
1803exit_free_emma:
c6a41e32
JM
1804 vb2_dma_contig_cleanup_ctx(pcdev->alloc_ctx);
1805eallocctx:
2c9ba37d 1806 if (cpu_is_mx27()) {
2066930d
BS
1807 free_irq(pcdev->irq_emma, pcdev);
1808 clk_disable(pcdev->clk_emma);
1809 clk_put(pcdev->clk_emma);
1810 iounmap(pcdev->base_emma);
2c9ba37d 1811 release_mem_region(pcdev->res_emma->start, resource_size(pcdev->res_emma));
2066930d
BS
1812 }
1813exit_free_irq:
2c9ba37d
SH
1814 if (cpu_is_mx25())
1815 free_irq(pcdev->irq_csi, pcdev);
2066930d
BS
1816exit_iounmap:
1817 iounmap(base_csi);
1818exit_release:
1819 release_mem_region(res_csi->start, resource_size(res_csi));
1820exit_dma_free:
2066930d 1821 clk_put(pcdev->clk_csi);
2066930d
BS
1822exit_kfree:
1823 kfree(pcdev);
1824exit:
1825 return err;
1826}
1827
1828static int __devexit mx2_camera_remove(struct platform_device *pdev)
1829{
1830 struct soc_camera_host *soc_host = to_soc_camera_host(&pdev->dev);
1831 struct mx2_camera_dev *pcdev = container_of(soc_host,
1832 struct mx2_camera_dev, soc_host);
1833 struct resource *res;
1834
1835 clk_put(pcdev->clk_csi);
2c9ba37d
SH
1836 if (cpu_is_mx25())
1837 free_irq(pcdev->irq_csi, pcdev);
2066930d 1838 if (cpu_is_mx27())
2066930d
BS
1839 free_irq(pcdev->irq_emma, pcdev);
1840
1841 soc_camera_host_unregister(&pcdev->soc_host);
1842
c6a41e32
JM
1843 vb2_dma_contig_cleanup_ctx(pcdev->alloc_ctx);
1844
2066930d
BS
1845 iounmap(pcdev->base_csi);
1846
2c9ba37d 1847 if (cpu_is_mx27()) {
2066930d
BS
1848 clk_disable(pcdev->clk_emma);
1849 clk_put(pcdev->clk_emma);
1850 iounmap(pcdev->base_emma);
1851 res = pcdev->res_emma;
1852 release_mem_region(res->start, resource_size(res));
1853 }
1854
1855 res = pcdev->res_csi;
1856 release_mem_region(res->start, resource_size(res));
1857
1858 kfree(pcdev);
1859
1860 dev_info(&pdev->dev, "MX2 Camera driver unloaded\n");
1861
1862 return 0;
1863}
1864
1865static struct platform_driver mx2_camera_driver = {
1866 .driver = {
1867 .name = MX2_CAM_DRV_NAME,
1868 },
1869 .remove = __devexit_p(mx2_camera_remove),
1870};
1871
1872
1873static int __init mx2_camera_init(void)
1874{
1875 return platform_driver_probe(&mx2_camera_driver, &mx2_camera_probe);
1876}
1877
1878static void __exit mx2_camera_exit(void)
1879{
1880 return platform_driver_unregister(&mx2_camera_driver);
1881}
1882
1883module_init(mx2_camera_init);
1884module_exit(mx2_camera_exit);
1885
1886MODULE_DESCRIPTION("i.MX27/i.MX25 SoC Camera Host driver");
1887MODULE_AUTHOR("Sascha Hauer <sha@pengutronix.de>");
1888MODULE_LICENSE("GPL");
64dc3c1a 1889MODULE_VERSION(MX2_CAM_VERSION);
This page took 0.255931 seconds and 5 git commands to generate.