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