[media] media i.MX27 camera: migrate driver to videobuf2
[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
231/* buffer for one video frame */
232struct mx2_buffer {
233 /* common v4l buffer stuff -- must be first */
234 struct vb2_buffer vb;
235 struct list_head queue;
236 enum mx2_buffer_state state;
237
238 int bufnum;
239};
240
2066930d
BS
241struct mx2_camera_dev {
242 struct device *dev;
243 struct soc_camera_host soc_host;
244 struct soc_camera_device *icd;
245 struct clk *clk_csi, *clk_emma;
246
247 unsigned int irq_csi, irq_emma;
248 void __iomem *base_csi, *base_emma;
249 unsigned long base_dma;
250
251 struct mx2_camera_platform_data *pdata;
252 struct resource *res_csi, *res_emma;
253 unsigned long platform_flags;
254
255 struct list_head capture;
256 struct list_head active_bufs;
257
258 spinlock_t lock;
259
260 int dma;
261 struct mx2_buffer *active;
262 struct mx2_buffer *fb1_active;
263 struct mx2_buffer *fb2_active;
264
2066930d
BS
265 u32 csicr1;
266
79d3c2c2 267 void *discard_buffer;
2066930d
BS
268 dma_addr_t discard_buffer_dma;
269 size_t discard_size;
f410991d 270 struct mx2_fmt_cfg *emma_prp;
ccd1a499 271 u32 frame_count;
c6a41e32 272 struct vb2_alloc_ctx *alloc_ctx;
2066930d
BS
273};
274
f410991d
JM
275static struct mx2_fmt_cfg mx27_emma_prp_table[] = {
276 /*
277 * This is a generic configuration which is valid for most
278 * prp input-output format combinations.
279 * We set the incomming and outgoing pixelformat to a
280 * 16 Bit wide format and adjust the bytesperline
281 * accordingly. With this configuration the inputdata
282 * will not be changed by the emma and could be any type
283 * of 16 Bit Pixelformat.
284 */
285 {
286 .in_fmt = 0,
287 .out_fmt = 0,
288 .cfg = {
289 .channel = 1,
290 .in_fmt = PRP_CNTL_DATA_IN_RGB16,
291 .out_fmt = PRP_CNTL_CH1_OUT_RGB16,
292 .src_pixel = 0x2ca00565, /* RGB565 */
293 .ch1_pixel = 0x2ca00565, /* RGB565 */
294 .irq_flags = PRP_INTR_RDERR | PRP_INTR_CH1WERR |
295 PRP_INTR_CH1FC | PRP_INTR_LBOVF,
296 }
297 },
298 {
299 .in_fmt = V4L2_MBUS_FMT_YUYV8_2X8,
300 .out_fmt = V4L2_PIX_FMT_YUV420,
301 .cfg = {
302 .channel = 2,
303 .in_fmt = PRP_CNTL_DATA_IN_YUV422,
304 .out_fmt = PRP_CNTL_CH2_OUT_YUV420,
305 .src_pixel = 0x22000888, /* YUV422 (YUYV) */
306 .irq_flags = PRP_INTR_RDERR | PRP_INTR_CH2WERR |
307 PRP_INTR_CH2FC | PRP_INTR_LBOVF |
308 PRP_INTR_CH2OVF,
309 }
310 },
311};
312
313static struct mx2_fmt_cfg *mx27_emma_prp_get_format(
314 enum v4l2_mbus_pixelcode in_fmt,
315 u32 out_fmt)
316{
317 int i;
318
319 for (i = 1; i < ARRAY_SIZE(mx27_emma_prp_table); i++)
320 if ((mx27_emma_prp_table[i].in_fmt == in_fmt) &&
321 (mx27_emma_prp_table[i].out_fmt == out_fmt)) {
322 return &mx27_emma_prp_table[i];
323 }
324 /* If no match return the most generic configuration */
325 return &mx27_emma_prp_table[0];
326};
327
2066930d
BS
328static void mx2_camera_deactivate(struct mx2_camera_dev *pcdev)
329{
330 unsigned long flags;
331
332 clk_disable(pcdev->clk_csi);
333 writel(0, pcdev->base_csi + CSICR1);
2c9ba37d 334 if (cpu_is_mx27()) {
2066930d
BS
335 writel(0, pcdev->base_emma + PRP_CNTL);
336 } else if (cpu_is_mx25()) {
337 spin_lock_irqsave(&pcdev->lock, flags);
338 pcdev->fb1_active = NULL;
339 pcdev->fb2_active = NULL;
340 writel(0, pcdev->base_csi + CSIDMASA_FB1);
341 writel(0, pcdev->base_csi + CSIDMASA_FB2);
342 spin_unlock_irqrestore(&pcdev->lock, flags);
343 }
344}
345
346/*
347 * The following two functions absolutely depend on the fact, that
348 * there can be only one camera on mx2 camera sensor interface
349 */
350static int mx2_camera_add_device(struct soc_camera_device *icd)
351{
7dfff953 352 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
2066930d
BS
353 struct mx2_camera_dev *pcdev = ici->priv;
354 int ret;
355 u32 csicr1;
356
357 if (pcdev->icd)
358 return -EBUSY;
359
360 ret = clk_enable(pcdev->clk_csi);
361 if (ret < 0)
362 return ret;
363
364 csicr1 = CSICR1_MCLKEN;
365
2c9ba37d 366 if (cpu_is_mx27()) {
2066930d
BS
367 csicr1 |= CSICR1_PRP_IF_EN | CSICR1_FCC |
368 CSICR1_RXFF_LEVEL(0);
369 } else if (cpu_is_mx27())
370 csicr1 |= CSICR1_SOF_INTEN | CSICR1_RXFF_LEVEL(2);
371
372 pcdev->csicr1 = csicr1;
373 writel(pcdev->csicr1, pcdev->base_csi + CSICR1);
374
375 pcdev->icd = icd;
ccd1a499 376 pcdev->frame_count = 0;
2066930d 377
7dfff953 378 dev_info(icd->parent, "Camera driver attached to camera %d\n",
2066930d
BS
379 icd->devnum);
380
381 return 0;
382}
383
384static void mx2_camera_remove_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
389 BUG_ON(icd != pcdev->icd);
390
7dfff953 391 dev_info(icd->parent, "Camera driver detached from camera %d\n",
2066930d
BS
392 icd->devnum);
393
394 mx2_camera_deactivate(pcdev);
395
396 if (pcdev->discard_buffer) {
397 dma_free_coherent(ici->v4l2_dev.dev, pcdev->discard_size,
398 pcdev->discard_buffer,
399 pcdev->discard_buffer_dma);
400 pcdev->discard_buffer = NULL;
401 }
402
403 pcdev->icd = NULL;
404}
405
2066930d
BS
406static void mx25_camera_frame_done(struct mx2_camera_dev *pcdev, int fb,
407 int state)
408{
c6a41e32 409 struct vb2_buffer *vb;
2066930d
BS
410 struct mx2_buffer *buf;
411 struct mx2_buffer **fb_active = fb == 1 ? &pcdev->fb1_active :
412 &pcdev->fb2_active;
413 u32 fb_reg = fb == 1 ? CSIDMASA_FB1 : CSIDMASA_FB2;
414 unsigned long flags;
415
416 spin_lock_irqsave(&pcdev->lock, flags);
417
5384a12b
BS
418 if (*fb_active == NULL)
419 goto out;
420
2066930d 421 vb = &(*fb_active)->vb;
c6a41e32
JM
422 dev_dbg(pcdev->dev, "%s (vb=0x%p) 0x%p %lu\n", __func__,
423 vb, vb2_plane_vaddr(vb, 0), vb2_get_plane_payload(vb, 0));
2066930d 424
c6a41e32
JM
425 do_gettimeofday(&vb->v4l2_buf.timestamp);
426 vb->v4l2_buf.sequence++;
427 vb2_buffer_done(vb, VB2_BUF_STATE_DONE);
2066930d
BS
428
429 if (list_empty(&pcdev->capture)) {
430 buf = NULL;
431 writel(0, pcdev->base_csi + fb_reg);
432 } else {
433 buf = list_entry(pcdev->capture.next, struct mx2_buffer,
c6a41e32 434 queue);
2066930d 435 vb = &buf->vb;
c6a41e32
JM
436 list_del(&buf->queue);
437 buf->state = MX2_STATE_ACTIVE;
438 writel(vb2_dma_contig_plane_dma_addr(vb, 0),
439 pcdev->base_csi + fb_reg);
2066930d
BS
440 }
441
442 *fb_active = buf;
443
5384a12b 444out:
2066930d
BS
445 spin_unlock_irqrestore(&pcdev->lock, flags);
446}
447
448static irqreturn_t mx25_camera_irq(int irq_csi, void *data)
449{
450 struct mx2_camera_dev *pcdev = data;
451 u32 status = readl(pcdev->base_csi + CSISR);
452
453 if (status & CSISR_DMA_TSF_FB1_INT)
c6a41e32 454 mx25_camera_frame_done(pcdev, 1, MX2_STATE_DONE);
2066930d 455 else if (status & CSISR_DMA_TSF_FB2_INT)
c6a41e32 456 mx25_camera_frame_done(pcdev, 2, MX2_STATE_DONE);
2066930d
BS
457
458 /* FIXME: handle CSISR_RFF_OR_INT */
459
460 writel(status, pcdev->base_csi + CSISR);
461
462 return IRQ_HANDLED;
463}
464
465/*
466 * Videobuf operations
467 */
c6a41e32
JM
468static int mx2_videobuf_setup(struct vb2_queue *vq,
469 const struct v4l2_format *fmt,
470 unsigned int *count, unsigned int *num_planes,
471 unsigned int sizes[], void *alloc_ctxs[])
2066930d 472{
c6a41e32
JM
473 struct soc_camera_device *icd = soc_camera_from_vb2q(vq);
474 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
475 struct mx2_camera_dev *pcdev = ici->priv;
2066930d
BS
476 int bytes_per_line = soc_mbus_bytes_per_line(icd->user_width,
477 icd->current_fmt->host_fmt);
478
c6a41e32
JM
479 dev_dbg(icd->parent, "count=%d, size=%d\n", *count, sizes[0]);
480
481 /* TODO: support for VIDIOC_CREATE_BUFS not ready */
482 if (fmt != NULL)
483 return -ENOTTY;
2066930d
BS
484
485 if (bytes_per_line < 0)
486 return bytes_per_line;
487
c6a41e32
JM
488 alloc_ctxs[0] = pcdev->alloc_ctx;
489
490 sizes[0] = bytes_per_line * icd->user_height;
2066930d
BS
491
492 if (0 == *count)
493 *count = 32;
c6a41e32
JM
494 if (!*num_planes &&
495 sizes[0] * *count > MAX_VIDEO_MEM * 1024 * 1024)
496 *count = (MAX_VIDEO_MEM * 1024 * 1024) / sizes[0];
2066930d 497
c6a41e32 498 *num_planes = 1;
2066930d 499
c6a41e32 500 return 0;
2066930d
BS
501}
502
c6a41e32 503static int mx2_videobuf_prepare(struct vb2_buffer *vb)
2066930d 504{
c6a41e32 505 struct soc_camera_device *icd = soc_camera_from_vb2q(vb->vb2_queue);
2066930d
BS
506 int bytes_per_line = soc_mbus_bytes_per_line(icd->user_width,
507 icd->current_fmt->host_fmt);
508 int ret = 0;
509
c6a41e32
JM
510 dev_dbg(icd->parent, "%s (vb=0x%p) 0x%p %lu\n", __func__,
511 vb, vb2_plane_vaddr(vb, 0), vb2_get_plane_payload(vb, 0));
2066930d
BS
512
513 if (bytes_per_line < 0)
514 return bytes_per_line;
515
516#ifdef DEBUG
517 /*
518 * This can be useful if you want to see if we actually fill
519 * the buffer with something
520 */
c6a41e32
JM
521 memset((void *)vb2_plane_vaddr(vb, 0),
522 0xaa, vb2_get_plane_payload(vb, 0));
2066930d
BS
523#endif
524
c6a41e32
JM
525 vb2_set_plane_payload(vb, 0, bytes_per_line * icd->user_height);
526 if (vb2_plane_vaddr(vb, 0) &&
527 vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0)) {
2066930d
BS
528 ret = -EINVAL;
529 goto out;
530 }
531
2066930d
BS
532 return 0;
533
2066930d
BS
534out:
535 return ret;
536}
537
c6a41e32 538static void mx2_videobuf_queue(struct vb2_buffer *vb)
2066930d 539{
c6a41e32 540 struct soc_camera_device *icd = soc_camera_from_vb2q(vb->vb2_queue);
2066930d 541 struct soc_camera_host *ici =
7dfff953 542 to_soc_camera_host(icd->parent);
2066930d
BS
543 struct mx2_camera_dev *pcdev = ici->priv;
544 struct mx2_buffer *buf = container_of(vb, struct mx2_buffer, vb);
545 unsigned long flags;
546
c6a41e32
JM
547 dev_dbg(icd->parent, "%s (vb=0x%p) 0x%p %lu\n", __func__,
548 vb, vb2_plane_vaddr(vb, 0), vb2_get_plane_payload(vb, 0));
2066930d
BS
549
550 spin_lock_irqsave(&pcdev->lock, flags);
551
c6a41e32
JM
552 buf->state = MX2_STATE_QUEUED;
553 list_add_tail(&buf->queue, &pcdev->capture);
2066930d 554
2c9ba37d 555 if (cpu_is_mx25()) {
2066930d
BS
556 u32 csicr3, dma_inten = 0;
557
558 if (pcdev->fb1_active == NULL) {
c6a41e32 559 writel(vb2_dma_contig_plane_dma_addr(vb, 0),
2066930d
BS
560 pcdev->base_csi + CSIDMASA_FB1);
561 pcdev->fb1_active = buf;
562 dma_inten = CSICR1_FB1_DMA_INTEN;
563 } else if (pcdev->fb2_active == NULL) {
c6a41e32 564 writel(vb2_dma_contig_plane_dma_addr(vb, 0),
2066930d
BS
565 pcdev->base_csi + CSIDMASA_FB2);
566 pcdev->fb2_active = buf;
567 dma_inten = CSICR1_FB2_DMA_INTEN;
568 }
569
570 if (dma_inten) {
c6a41e32
JM
571 list_del(&buf->queue);
572 buf->state = MX2_STATE_ACTIVE;
2066930d
BS
573
574 csicr3 = readl(pcdev->base_csi + CSICR3);
575
576 /* Reflash DMA */
577 writel(csicr3 | CSICR3_DMA_REFLASH_RFF,
578 pcdev->base_csi + CSICR3);
579
580 /* clear & enable interrupts */
581 writel(dma_inten, pcdev->base_csi + CSISR);
582 pcdev->csicr1 |= dma_inten;
583 writel(pcdev->csicr1, pcdev->base_csi + CSICR1);
584
585 /* enable DMA */
586 csicr3 |= CSICR3_DMA_REQ_EN_RFF | CSICR3_RXFF_LEVEL(1);
587 writel(csicr3, pcdev->base_csi + CSICR3);
588 }
589 }
590
2066930d
BS
591 spin_unlock_irqrestore(&pcdev->lock, flags);
592}
593
c6a41e32 594static void mx2_videobuf_release(struct vb2_buffer *vb)
2066930d 595{
c6a41e32 596 struct soc_camera_device *icd = soc_camera_from_vb2q(vb->vb2_queue);
7dfff953 597 struct soc_camera_host *ici = 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
602#ifdef DEBUG
c6a41e32
JM
603 dev_dbg(icd->parent, "%s (vb=0x%p) 0x%p %lu\n", __func__,
604 vb, vb2_plane_vaddr(vb, 0), vb2_get_plane_payload(vb, 0));
2066930d 605
c6a41e32
JM
606 switch (buf->state) {
607 case MX2_STATE_ACTIVE:
7dfff953 608 dev_info(icd->parent, "%s (active)\n", __func__);
2066930d 609 break;
c6a41e32 610 case MX2_STATE_QUEUED:
7dfff953 611 dev_info(icd->parent, "%s (queued)\n", __func__);
2066930d 612 break;
2066930d 613 default:
7dfff953 614 dev_info(icd->parent, "%s (unknown) %d\n", __func__,
c6a41e32 615 buf->state);
2066930d
BS
616 break;
617 }
618#endif
619
620 /*
621 * Terminate only queued but inactive buffers. Active buffers are
622 * released when they become inactive after videobuf_waiton().
623 *
7c6b7319
BS
624 * FIXME: implement forced termination of active buffers for mx27 and
625 * mx27 eMMA, so that the user won't get stuck in an uninterruptible
626 * state. This requires a specific handling for each of the these DMA
627 * types.
2066930d 628 */
c6a41e32 629
2066930d 630 spin_lock_irqsave(&pcdev->lock, flags);
c6a41e32
JM
631 list_del_init(&buf->queue);
632 if (cpu_is_mx25() && buf->state == MX2_STATE_ACTIVE) {
7c6b7319
BS
633 if (pcdev->fb1_active == buf) {
634 pcdev->csicr1 &= ~CSICR1_FB1_DMA_INTEN;
635 writel(0, pcdev->base_csi + CSIDMASA_FB1);
636 pcdev->fb1_active = NULL;
637 } else if (pcdev->fb2_active == buf) {
638 pcdev->csicr1 &= ~CSICR1_FB2_DMA_INTEN;
639 writel(0, pcdev->base_csi + CSIDMASA_FB2);
640 pcdev->fb2_active = NULL;
641 }
642 writel(pcdev->csicr1, pcdev->base_csi + CSICR1);
2066930d
BS
643 }
644 spin_unlock_irqrestore(&pcdev->lock, flags);
2066930d
BS
645}
646
c6a41e32
JM
647static struct vb2_ops mx2_videobuf_ops = {
648 .queue_setup = mx2_videobuf_setup,
649 .buf_prepare = mx2_videobuf_prepare,
650 .buf_queue = mx2_videobuf_queue,
651 .buf_cleanup = mx2_videobuf_release,
2066930d
BS
652};
653
c6a41e32 654static int mx2_camera_init_videobuf(struct vb2_queue *q,
2066930d
BS
655 struct soc_camera_device *icd)
656{
c6a41e32
JM
657 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
658 q->io_modes = VB2_MMAP | VB2_USERPTR;
659 q->drv_priv = icd;
660 q->ops = &mx2_videobuf_ops;
661 q->mem_ops = &vb2_dma_contig_memops;
662 q->buf_struct_size = sizeof(struct mx2_buffer);
663
664 return vb2_queue_init(q);
2066930d
BS
665}
666
db592a24
GL
667#define MX2_BUS_FLAGS (V4L2_MBUS_MASTER | \
668 V4L2_MBUS_VSYNC_ACTIVE_HIGH | \
669 V4L2_MBUS_VSYNC_ACTIVE_LOW | \
670 V4L2_MBUS_HSYNC_ACTIVE_HIGH | \
671 V4L2_MBUS_HSYNC_ACTIVE_LOW | \
672 V4L2_MBUS_PCLK_SAMPLE_RISING | \
673 V4L2_MBUS_PCLK_SAMPLE_FALLING | \
674 V4L2_MBUS_DATA_ACTIVE_HIGH | \
675 V4L2_MBUS_DATA_ACTIVE_LOW)
2066930d
BS
676
677static int mx27_camera_emma_prp_reset(struct mx2_camera_dev *pcdev)
678{
679 u32 cntl;
680 int count = 0;
681
682 cntl = readl(pcdev->base_emma + PRP_CNTL);
683 writel(PRP_CNTL_SWRST, pcdev->base_emma + PRP_CNTL);
684 while (count++ < 100) {
685 if (!(readl(pcdev->base_emma + PRP_CNTL) & PRP_CNTL_SWRST))
686 return 0;
687 barrier();
688 udelay(1);
689 }
690
691 return -ETIMEDOUT;
692}
693
694static void mx27_camera_emma_buf_init(struct soc_camera_device *icd,
695 int bytesperline)
696{
697 struct soc_camera_host *ici =
7dfff953 698 to_soc_camera_host(icd->parent);
2066930d 699 struct mx2_camera_dev *pcdev = ici->priv;
f410991d
JM
700 struct mx2_fmt_cfg *prp = pcdev->emma_prp;
701 u32 imgsize = pcdev->icd->user_height * pcdev->icd->user_width;
702
703 if (prp->cfg.channel == 1) {
704 writel(pcdev->discard_buffer_dma,
705 pcdev->base_emma + PRP_DEST_RGB1_PTR);
706 writel(pcdev->discard_buffer_dma,
707 pcdev->base_emma + PRP_DEST_RGB2_PTR);
708
709 writel(PRP_CNTL_CH1EN |
710 PRP_CNTL_CSIEN |
711 prp->cfg.in_fmt |
712 prp->cfg.out_fmt |
713 PRP_CNTL_CH1_LEN |
714 PRP_CNTL_CH1BYP |
715 PRP_CNTL_CH1_TSKIP(0) |
716 PRP_CNTL_IN_TSKIP(0),
717 pcdev->base_emma + PRP_CNTL);
718
719 writel((icd->user_width << 16) | icd->user_height,
720 pcdev->base_emma + PRP_SRC_FRAME_SIZE);
721 writel((icd->user_width << 16) | icd->user_height,
722 pcdev->base_emma + PRP_CH1_OUT_IMAGE_SIZE);
723 writel(bytesperline,
724 pcdev->base_emma + PRP_DEST_CH1_LINE_STRIDE);
725 writel(prp->cfg.src_pixel,
726 pcdev->base_emma + PRP_SRC_PIXEL_FORMAT_CNTL);
727 writel(prp->cfg.ch1_pixel,
728 pcdev->base_emma + PRP_CH1_PIXEL_FORMAT_CNTL);
729 } else { /* channel 2 */
730 writel(pcdev->discard_buffer_dma,
731 pcdev->base_emma + PRP_DEST_Y_PTR);
732 writel(pcdev->discard_buffer_dma,
733 pcdev->base_emma + PRP_SOURCE_Y_PTR);
734
735 if (prp->cfg.out_fmt == PRP_CNTL_CH2_OUT_YUV420) {
736 writel(pcdev->discard_buffer_dma + imgsize,
737 pcdev->base_emma + PRP_DEST_CB_PTR);
738 writel(pcdev->discard_buffer_dma + ((5 * imgsize) / 4),
739 pcdev->base_emma + PRP_DEST_CR_PTR);
740 writel(pcdev->discard_buffer_dma + imgsize,
741 pcdev->base_emma + PRP_SOURCE_CB_PTR);
742 writel(pcdev->discard_buffer_dma + ((5 * imgsize) / 4),
743 pcdev->base_emma + PRP_SOURCE_CR_PTR);
744 }
2066930d 745
f410991d 746 writel(PRP_CNTL_CH2EN |
2066930d 747 PRP_CNTL_CSIEN |
f410991d
JM
748 prp->cfg.in_fmt |
749 prp->cfg.out_fmt |
750 PRP_CNTL_CH2_LEN |
751 PRP_CNTL_CH2_TSKIP(0) |
2066930d
BS
752 PRP_CNTL_IN_TSKIP(0),
753 pcdev->base_emma + PRP_CNTL);
754
f410991d 755 writel((icd->user_width << 16) | icd->user_height,
2066930d 756 pcdev->base_emma + PRP_SRC_FRAME_SIZE);
f410991d
JM
757
758 writel((icd->user_width << 16) | icd->user_height,
759 pcdev->base_emma + PRP_CH2_OUT_IMAGE_SIZE);
760
761 writel(prp->cfg.src_pixel,
2066930d 762 pcdev->base_emma + PRP_SRC_PIXEL_FORMAT_CNTL);
f410991d
JM
763
764 }
2066930d
BS
765
766 /* Enable interrupts */
f410991d 767 writel(prp->cfg.irq_flags, pcdev->base_emma + PRP_INTR_CNTL);
2066930d
BS
768}
769
8843d119 770static int mx2_camera_set_bus_param(struct soc_camera_device *icd)
2066930d 771{
db592a24
GL
772 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
773 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
2066930d 774 struct mx2_camera_dev *pcdev = ici->priv;
db592a24
GL
775 struct v4l2_mbus_config cfg = {.type = V4L2_MBUS_PARALLEL,};
776 unsigned long common_flags;
777 int ret;
2066930d
BS
778 int bytesperline;
779 u32 csicr1 = pcdev->csicr1;
780
db592a24
GL
781 ret = v4l2_subdev_call(sd, video, g_mbus_config, &cfg);
782 if (!ret) {
783 common_flags = soc_mbus_config_compatible(&cfg, MX2_BUS_FLAGS);
784 if (!common_flags) {
785 dev_warn(icd->parent,
786 "Flags incompatible: camera 0x%x, host 0x%x\n",
787 cfg.flags, MX2_BUS_FLAGS);
788 return -EINVAL;
789 }
790 } else if (ret != -ENOIOCTLCMD) {
791 return ret;
792 } else {
793 common_flags = MX2_BUS_FLAGS;
794 }
2066930d 795
db592a24
GL
796 if ((common_flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH) &&
797 (common_flags & V4L2_MBUS_HSYNC_ACTIVE_LOW)) {
2066930d 798 if (pcdev->platform_flags & MX2_CAMERA_HSYNC_HIGH)
db592a24 799 common_flags &= ~V4L2_MBUS_HSYNC_ACTIVE_LOW;
2066930d 800 else
db592a24 801 common_flags &= ~V4L2_MBUS_HSYNC_ACTIVE_HIGH;
2066930d
BS
802 }
803
db592a24
GL
804 if ((common_flags & V4L2_MBUS_PCLK_SAMPLE_RISING) &&
805 (common_flags & V4L2_MBUS_PCLK_SAMPLE_FALLING)) {
2066930d 806 if (pcdev->platform_flags & MX2_CAMERA_PCLK_SAMPLE_RISING)
db592a24 807 common_flags &= ~V4L2_MBUS_PCLK_SAMPLE_FALLING;
2066930d 808 else
db592a24 809 common_flags &= ~V4L2_MBUS_PCLK_SAMPLE_RISING;
2066930d
BS
810 }
811
db592a24
GL
812 cfg.flags = common_flags;
813 ret = v4l2_subdev_call(sd, video, s_mbus_config, &cfg);
814 if (ret < 0 && ret != -ENOIOCTLCMD) {
815 dev_dbg(icd->parent, "camera s_mbus_config(0x%lx) returned %d\n",
816 common_flags, ret);
2066930d 817 return ret;
db592a24 818 }
2066930d 819
db592a24 820 if (common_flags & V4L2_MBUS_PCLK_SAMPLE_RISING)
d86097e1 821 csicr1 |= CSICR1_REDGE;
db592a24 822 if (common_flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH)
2066930d 823 csicr1 |= CSICR1_SOF_POL;
db592a24 824 if (common_flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH)
2066930d
BS
825 csicr1 |= CSICR1_HSYNC_POL;
826 if (pcdev->platform_flags & MX2_CAMERA_SWAP16)
827 csicr1 |= CSICR1_SWAP16_EN;
828 if (pcdev->platform_flags & MX2_CAMERA_EXT_VSYNC)
829 csicr1 |= CSICR1_EXT_VSYNC;
830 if (pcdev->platform_flags & MX2_CAMERA_CCIR)
831 csicr1 |= CSICR1_CCIR_EN;
832 if (pcdev->platform_flags & MX2_CAMERA_CCIR_INTERLACE)
833 csicr1 |= CSICR1_CCIR_MODE;
834 if (pcdev->platform_flags & MX2_CAMERA_GATED_CLOCK)
835 csicr1 |= CSICR1_GCLK_MODE;
836 if (pcdev->platform_flags & MX2_CAMERA_INV_DATA)
837 csicr1 |= CSICR1_INV_DATA;
838 if (pcdev->platform_flags & MX2_CAMERA_PACK_DIR_MSB)
839 csicr1 |= CSICR1_PACK_DIR;
840
841 pcdev->csicr1 = csicr1;
842
843 bytesperline = soc_mbus_bytes_per_line(icd->user_width,
844 icd->current_fmt->host_fmt);
845 if (bytesperline < 0)
846 return bytesperline;
847
2c9ba37d 848 if (cpu_is_mx27()) {
2066930d
BS
849 ret = mx27_camera_emma_prp_reset(pcdev);
850 if (ret)
851 return ret;
852
853 if (pcdev->discard_buffer)
854 dma_free_coherent(ici->v4l2_dev.dev,
855 pcdev->discard_size, pcdev->discard_buffer,
856 pcdev->discard_buffer_dma);
857
858 /*
859 * I didn't manage to properly enable/disable the prp
860 * on a per frame basis during running transfers,
861 * thus we allocate a buffer here and use it to
862 * discard frames when no buffer is available.
863 * Feel free to work on this ;)
864 */
865 pcdev->discard_size = icd->user_height * bytesperline;
866 pcdev->discard_buffer = dma_alloc_coherent(ici->v4l2_dev.dev,
867 pcdev->discard_size, &pcdev->discard_buffer_dma,
868 GFP_KERNEL);
869 if (!pcdev->discard_buffer)
870 return -ENOMEM;
871
872 mx27_camera_emma_buf_init(icd, bytesperline);
873 } else if (cpu_is_mx25()) {
874 writel((bytesperline * icd->user_height) >> 2,
875 pcdev->base_csi + CSIRXCNT);
876 writel((bytesperline << 16) | icd->user_height,
877 pcdev->base_csi + CSIIMAG_PARA);
878 }
879
880 writel(pcdev->csicr1, pcdev->base_csi + CSICR1);
881
882 return 0;
883}
884
885static int mx2_camera_set_crop(struct soc_camera_device *icd,
886 struct v4l2_crop *a)
887{
888 struct v4l2_rect *rect = &a->c;
889 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
890 struct v4l2_mbus_framefmt mf;
891 int ret;
892
893 soc_camera_limit_side(&rect->left, &rect->width, 0, 2, 4096);
894 soc_camera_limit_side(&rect->top, &rect->height, 0, 2, 4096);
895
896 ret = v4l2_subdev_call(sd, video, s_crop, a);
897 if (ret < 0)
898 return ret;
899
900 /* The capture device might have changed its output */
901 ret = v4l2_subdev_call(sd, video, g_mbus_fmt, &mf);
902 if (ret < 0)
903 return ret;
904
7dfff953 905 dev_dbg(icd->parent, "Sensor cropped %dx%d\n",
2066930d
BS
906 mf.width, mf.height);
907
908 icd->user_width = mf.width;
909 icd->user_height = mf.height;
910
911 return ret;
912}
913
f410991d
JM
914static int mx2_camera_get_formats(struct soc_camera_device *icd,
915 unsigned int idx,
916 struct soc_camera_format_xlate *xlate)
917{
918 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
919 const struct soc_mbus_pixelfmt *fmt;
920 struct device *dev = icd->parent;
921 enum v4l2_mbus_pixelcode code;
922 int ret, formats = 0;
923
924 ret = v4l2_subdev_call(sd, video, enum_mbus_fmt, idx, &code);
925 if (ret < 0)
926 /* no more formats */
927 return 0;
928
929 fmt = soc_mbus_get_fmtdesc(code);
930 if (!fmt) {
931 dev_err(dev, "Invalid format code #%u: %d\n", idx, code);
932 return 0;
933 }
934
935 if (code == V4L2_MBUS_FMT_YUYV8_2X8) {
936 formats++;
937 if (xlate) {
938 /*
939 * CH2 can output YUV420 which is a standard format in
940 * soc_mediabus.c
941 */
942 xlate->host_fmt =
943 soc_mbus_get_fmtdesc(V4L2_MBUS_FMT_YUYV8_1_5X8);
944 xlate->code = code;
945 dev_dbg(dev, "Providing host format %s for sensor code %d\n",
946 xlate->host_fmt->name, code);
947 xlate++;
948 }
949 }
950
951 /* Generic pass-trough */
952 formats++;
953 if (xlate) {
954 xlate->host_fmt = fmt;
955 xlate->code = code;
956 xlate++;
957 }
958 return formats;
959}
960
2066930d
BS
961static int mx2_camera_set_fmt(struct soc_camera_device *icd,
962 struct v4l2_format *f)
963{
f410991d
JM
964 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
965 struct mx2_camera_dev *pcdev = ici->priv;
2066930d
BS
966 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
967 const struct soc_camera_format_xlate *xlate;
968 struct v4l2_pix_format *pix = &f->fmt.pix;
969 struct v4l2_mbus_framefmt mf;
970 int ret;
971
972 xlate = soc_camera_xlate_by_fourcc(icd, pix->pixelformat);
973 if (!xlate) {
7dfff953 974 dev_warn(icd->parent, "Format %x not found\n",
2066930d
BS
975 pix->pixelformat);
976 return -EINVAL;
977 }
978
2066930d
BS
979 mf.width = pix->width;
980 mf.height = pix->height;
981 mf.field = pix->field;
982 mf.colorspace = pix->colorspace;
983 mf.code = xlate->code;
984
985 ret = v4l2_subdev_call(sd, video, s_mbus_fmt, &mf);
986 if (ret < 0 && ret != -ENOIOCTLCMD)
987 return ret;
988
989 if (mf.code != xlate->code)
990 return -EINVAL;
991
992 pix->width = mf.width;
993 pix->height = mf.height;
994 pix->field = mf.field;
995 pix->colorspace = mf.colorspace;
996 icd->current_fmt = xlate;
997
2c9ba37d 998 if (cpu_is_mx27())
f410991d
JM
999 pcdev->emma_prp = mx27_emma_prp_get_format(xlate->code,
1000 xlate->host_fmt->fourcc);
1001
2066930d
BS
1002 return 0;
1003}
1004
1005static int mx2_camera_try_fmt(struct soc_camera_device *icd,
1006 struct v4l2_format *f)
1007{
2066930d
BS
1008 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1009 const struct soc_camera_format_xlate *xlate;
1010 struct v4l2_pix_format *pix = &f->fmt.pix;
1011 struct v4l2_mbus_framefmt mf;
1012 __u32 pixfmt = pix->pixelformat;
1013 unsigned int width_limit;
1014 int ret;
1015
1016 xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
1017 if (pixfmt && !xlate) {
7dfff953 1018 dev_warn(icd->parent, "Format %x not found\n", pixfmt);
2066930d
BS
1019 return -EINVAL;
1020 }
1021
1022 /* FIXME: implement MX27 limits */
1023
2066930d
BS
1024 /* limit to MX25 hardware capabilities */
1025 if (cpu_is_mx25()) {
1026 if (xlate->host_fmt->bits_per_sample <= 8)
1027 width_limit = 0xffff * 4;
1028 else
1029 width_limit = 0xffff * 2;
1030 /* CSIIMAG_PARA limit */
1031 if (pix->width > width_limit)
1032 pix->width = width_limit;
1033 if (pix->height > 0xffff)
1034 pix->height = 0xffff;
1035
1036 pix->bytesperline = soc_mbus_bytes_per_line(pix->width,
1037 xlate->host_fmt);
1038 if (pix->bytesperline < 0)
1039 return pix->bytesperline;
1040 pix->sizeimage = pix->height * pix->bytesperline;
28281a71
GL
1041 /* Check against the CSIRXCNT limit */
1042 if (pix->sizeimage > 4 * 0x3ffff) {
1043 /* Adjust geometry, preserve aspect ratio */
1044 unsigned int new_height = int_sqrt(4 * 0x3ffff *
1045 pix->height / pix->bytesperline);
1046 pix->width = new_height * pix->width / pix->height;
1047 pix->height = new_height;
1048 pix->bytesperline = soc_mbus_bytes_per_line(pix->width,
1049 xlate->host_fmt);
1050 BUG_ON(pix->bytesperline < 0);
2066930d
BS
1051 }
1052 }
1053
1054 /* limit to sensor capabilities */
1055 mf.width = pix->width;
1056 mf.height = pix->height;
1057 mf.field = pix->field;
1058 mf.colorspace = pix->colorspace;
1059 mf.code = xlate->code;
1060
1061 ret = v4l2_subdev_call(sd, video, try_mbus_fmt, &mf);
1062 if (ret < 0)
1063 return ret;
1064
1065 if (mf.field == V4L2_FIELD_ANY)
1066 mf.field = V4L2_FIELD_NONE;
f410991d
JM
1067 /*
1068 * Driver supports interlaced images provided they have
1069 * both fields so that they can be processed as if they
1070 * were progressive.
1071 */
1072 if (mf.field != V4L2_FIELD_NONE && !V4L2_FIELD_HAS_BOTH(mf.field)) {
7dfff953 1073 dev_err(icd->parent, "Field type %d unsupported.\n",
2066930d
BS
1074 mf.field);
1075 return -EINVAL;
1076 }
1077
1078 pix->width = mf.width;
1079 pix->height = mf.height;
1080 pix->field = mf.field;
1081 pix->colorspace = mf.colorspace;
1082
1083 return 0;
1084}
1085
1086static int mx2_camera_querycap(struct soc_camera_host *ici,
1087 struct v4l2_capability *cap)
1088{
1089 /* cap->name is set by the friendly caller:-> */
1090 strlcpy(cap->card, MX2_CAM_DRIVER_DESCRIPTION, sizeof(cap->card));
2066930d
BS
1091 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1092
1093 return 0;
1094}
1095
2066930d
BS
1096static unsigned int mx2_camera_poll(struct file *file, poll_table *pt)
1097{
6b101926 1098 struct soc_camera_device *icd = file->private_data;
2066930d 1099
c6a41e32 1100 return vb2_poll(&icd->vb2_vidq, file, pt);
2066930d
BS
1101}
1102
1103static struct soc_camera_host_ops mx2_soc_camera_host_ops = {
1104 .owner = THIS_MODULE,
1105 .add = mx2_camera_add_device,
1106 .remove = mx2_camera_remove_device,
1107 .set_fmt = mx2_camera_set_fmt,
1108 .set_crop = mx2_camera_set_crop,
f410991d 1109 .get_formats = mx2_camera_get_formats,
2066930d 1110 .try_fmt = mx2_camera_try_fmt,
c6a41e32 1111 .init_videobuf2 = mx2_camera_init_videobuf,
2066930d
BS
1112 .poll = mx2_camera_poll,
1113 .querycap = mx2_camera_querycap,
1114 .set_bus_param = mx2_camera_set_bus_param,
1115};
1116
1117static void mx27_camera_frame_done_emma(struct mx2_camera_dev *pcdev,
c6a41e32 1118 int bufnum)
2066930d 1119{
f410991d
JM
1120 u32 imgsize = pcdev->icd->user_height * pcdev->icd->user_width;
1121 struct mx2_fmt_cfg *prp = pcdev->emma_prp;
2066930d 1122 struct mx2_buffer *buf;
c6a41e32 1123 struct vb2_buffer *vb;
2066930d
BS
1124 unsigned long phys;
1125
1126 if (!list_empty(&pcdev->active_bufs)) {
1127 buf = list_entry(pcdev->active_bufs.next,
c6a41e32 1128 struct mx2_buffer, queue);
2066930d
BS
1129
1130 BUG_ON(buf->bufnum != bufnum);
1131
1132 vb = &buf->vb;
1133#ifdef DEBUG
c6a41e32 1134 phys = vb2_dma_contig_plane_dma_addr(vb, 0);
f410991d
JM
1135 if (prp->cfg.channel == 1) {
1136 if (readl(pcdev->base_emma + PRP_DEST_RGB1_PTR +
1137 4 * bufnum) != phys) {
1138 dev_err(pcdev->dev, "%p != %p\n", phys,
1139 readl(pcdev->base_emma +
1140 PRP_DEST_RGB1_PTR +
1141 4 * bufnum));
1142 }
1143 } else {
1144 if (readl(pcdev->base_emma + PRP_DEST_Y_PTR -
1145 0x14 * bufnum) != phys) {
1146 dev_err(pcdev->dev, "%p != %p\n", phys,
1147 readl(pcdev->base_emma +
1148 PRP_DEST_Y_PTR -
1149 0x14 * bufnum));
1150 }
2066930d
BS
1151 }
1152#endif
c6a41e32
JM
1153 dev_dbg(pcdev->dev, "%s (vb=0x%p) 0x%p %lu\n", __func__, vb,
1154 vb2_plane_vaddr(vb, 0),
1155 vb2_get_plane_payload(vb, 0));
2066930d 1156
c6a41e32
JM
1157 list_del_init(&buf->queue);
1158 do_gettimeofday(&vb->v4l2_buf.timestamp);
ccd1a499 1159 pcdev->frame_count++;
c6a41e32
JM
1160 vb->v4l2_buf.sequence = pcdev->frame_count;
1161 vb2_buffer_done(vb, VB2_BUF_STATE_DONE);
2066930d
BS
1162 }
1163
1164 if (list_empty(&pcdev->capture)) {
f410991d
JM
1165 if (prp->cfg.channel == 1) {
1166 writel(pcdev->discard_buffer_dma, pcdev->base_emma +
1167 PRP_DEST_RGB1_PTR + 4 * bufnum);
1168 } else {
1169 writel(pcdev->discard_buffer_dma, pcdev->base_emma +
1170 PRP_DEST_Y_PTR -
1171 0x14 * bufnum);
1172 if (prp->out_fmt == V4L2_PIX_FMT_YUV420) {
1173 writel(pcdev->discard_buffer_dma + imgsize,
1174 pcdev->base_emma + PRP_DEST_CB_PTR -
1175 0x14 * bufnum);
1176 writel(pcdev->discard_buffer_dma +
1177 ((5 * imgsize) / 4), pcdev->base_emma +
1178 PRP_DEST_CR_PTR - 0x14 * bufnum);
1179 }
1180 }
2066930d
BS
1181 return;
1182 }
1183
1184 buf = list_entry(pcdev->capture.next,
c6a41e32 1185 struct mx2_buffer, queue);
2066930d 1186
cd9ebdbc 1187 buf->bufnum = !bufnum;
2066930d
BS
1188
1189 list_move_tail(pcdev->capture.next, &pcdev->active_bufs);
1190
1191 vb = &buf->vb;
c6a41e32 1192 buf->state = MX2_STATE_ACTIVE;
2066930d 1193
c6a41e32 1194 phys = vb2_dma_contig_plane_dma_addr(vb, 0);
f410991d
JM
1195 if (prp->cfg.channel == 1) {
1196 writel(phys, pcdev->base_emma + PRP_DEST_RGB1_PTR + 4 * bufnum);
1197 } else {
1198 writel(phys, pcdev->base_emma +
1199 PRP_DEST_Y_PTR - 0x14 * bufnum);
1200 if (prp->cfg.out_fmt == PRP_CNTL_CH2_OUT_YUV420) {
1201 writel(phys + imgsize, pcdev->base_emma +
1202 PRP_DEST_CB_PTR - 0x14 * bufnum);
1203 writel(phys + ((5 * imgsize) / 4), pcdev->base_emma +
1204 PRP_DEST_CR_PTR - 0x14 * bufnum);
1205 }
1206 }
2066930d
BS
1207}
1208
1209static irqreturn_t mx27_camera_emma_irq(int irq_emma, void *data)
1210{
1211 struct mx2_camera_dev *pcdev = data;
1212 unsigned int status = readl(pcdev->base_emma + PRP_INTRSTATUS);
1213 struct mx2_buffer *buf;
1214
1215 if (status & (1 << 7)) { /* overflow */
1216 u32 cntl;
1217 /*
1218 * We only disable channel 1 here since this is the only
1219 * enabled channel
1220 *
1221 * FIXME: the correct DMA overflow handling should be resetting
1222 * the buffer, returning an error frame, and continuing with
1223 * the next one.
1224 */
1225 cntl = readl(pcdev->base_emma + PRP_CNTL);
f410991d
JM
1226 writel(cntl & ~(PRP_CNTL_CH1EN | PRP_CNTL_CH2EN),
1227 pcdev->base_emma + PRP_CNTL);
2066930d
BS
1228 writel(cntl, pcdev->base_emma + PRP_CNTL);
1229 }
f410991d
JM
1230 if ((((status & (3 << 5)) == (3 << 5)) ||
1231 ((status & (3 << 3)) == (3 << 3)))
2066930d
BS
1232 && !list_empty(&pcdev->active_bufs)) {
1233 /*
1234 * Both buffers have triggered, process the one we're expecting
1235 * to first
1236 */
1237 buf = list_entry(pcdev->active_bufs.next,
c6a41e32
JM
1238 struct mx2_buffer, queue);
1239 mx27_camera_frame_done_emma(pcdev, buf->bufnum);
2066930d
BS
1240 status &= ~(1 << (6 - buf->bufnum)); /* mark processed */
1241 }
f410991d 1242 if ((status & (1 << 6)) || (status & (1 << 4)))
c6a41e32 1243 mx27_camera_frame_done_emma(pcdev, 0);
f410991d 1244 if ((status & (1 << 5)) || (status & (1 << 3)))
c6a41e32 1245 mx27_camera_frame_done_emma(pcdev, 1);
2066930d
BS
1246
1247 writel(status, pcdev->base_emma + PRP_INTRSTATUS);
1248
1249 return IRQ_HANDLED;
1250}
1251
1252static int __devinit mx27_camera_emma_init(struct mx2_camera_dev *pcdev)
1253{
1254 struct resource *res_emma = pcdev->res_emma;
1255 int err = 0;
1256
1257 if (!request_mem_region(res_emma->start, resource_size(res_emma),
1258 MX2_CAM_DRV_NAME)) {
1259 err = -EBUSY;
1260 goto out;
1261 }
1262
1263 pcdev->base_emma = ioremap(res_emma->start, resource_size(res_emma));
1264 if (!pcdev->base_emma) {
1265 err = -ENOMEM;
1266 goto exit_release;
1267 }
1268
1269 err = request_irq(pcdev->irq_emma, mx27_camera_emma_irq, 0,
1270 MX2_CAM_DRV_NAME, pcdev);
1271 if (err) {
1272 dev_err(pcdev->dev, "Camera EMMA interrupt register failed \n");
1273 goto exit_iounmap;
1274 }
1275
1276 pcdev->clk_emma = clk_get(NULL, "emma");
1277 if (IS_ERR(pcdev->clk_emma)) {
1278 err = PTR_ERR(pcdev->clk_emma);
1279 goto exit_free_irq;
1280 }
1281
1282 clk_enable(pcdev->clk_emma);
1283
1284 err = mx27_camera_emma_prp_reset(pcdev);
1285 if (err)
1286 goto exit_clk_emma_put;
1287
1288 return err;
1289
1290exit_clk_emma_put:
1291 clk_disable(pcdev->clk_emma);
1292 clk_put(pcdev->clk_emma);
1293exit_free_irq:
1294 free_irq(pcdev->irq_emma, pcdev);
1295exit_iounmap:
1296 iounmap(pcdev->base_emma);
1297exit_release:
1298 release_mem_region(res_emma->start, resource_size(res_emma));
1299out:
1300 return err;
1301}
1302
1303static int __devinit mx2_camera_probe(struct platform_device *pdev)
1304{
1305 struct mx2_camera_dev *pcdev;
1306 struct resource *res_csi, *res_emma;
1307 void __iomem *base_csi;
1308 int irq_csi, irq_emma;
2066930d
BS
1309 int err = 0;
1310
1311 dev_dbg(&pdev->dev, "initialising\n");
1312
1313 res_csi = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1314 irq_csi = platform_get_irq(pdev, 0);
1315 if (res_csi == NULL || irq_csi < 0) {
1316 dev_err(&pdev->dev, "Missing platform resources data\n");
1317 err = -ENODEV;
1318 goto exit;
1319 }
1320
1321 pcdev = kzalloc(sizeof(*pcdev), GFP_KERNEL);
1322 if (!pcdev) {
1323 dev_err(&pdev->dev, "Could not allocate pcdev\n");
1324 err = -ENOMEM;
1325 goto exit;
1326 }
1327
1328 pcdev->clk_csi = clk_get(&pdev->dev, NULL);
1329 if (IS_ERR(pcdev->clk_csi)) {
1330 err = PTR_ERR(pcdev->clk_csi);
1331 goto exit_kfree;
1332 }
1333
1334 dev_dbg(&pdev->dev, "Camera clock frequency: %ld\n",
1335 clk_get_rate(pcdev->clk_csi));
1336
2066930d
BS
1337 pcdev->res_csi = res_csi;
1338 pcdev->pdata = pdev->dev.platform_data;
1339 if (pcdev->pdata) {
1340 long rate;
1341
1342 pcdev->platform_flags = pcdev->pdata->flags;
1343
1344 rate = clk_round_rate(pcdev->clk_csi, pcdev->pdata->clk * 2);
1345 if (rate <= 0) {
1346 err = -ENODEV;
1347 goto exit_dma_free;
1348 }
1349 err = clk_set_rate(pcdev->clk_csi, rate);
1350 if (err < 0)
1351 goto exit_dma_free;
1352 }
1353
1354 INIT_LIST_HEAD(&pcdev->capture);
1355 INIT_LIST_HEAD(&pcdev->active_bufs);
1356 spin_lock_init(&pcdev->lock);
1357
1358 /*
1359 * Request the regions.
1360 */
1361 if (!request_mem_region(res_csi->start, resource_size(res_csi),
1362 MX2_CAM_DRV_NAME)) {
1363 err = -EBUSY;
1364 goto exit_dma_free;
1365 }
1366
1367 base_csi = ioremap(res_csi->start, resource_size(res_csi));
1368 if (!base_csi) {
1369 err = -ENOMEM;
1370 goto exit_release;
1371 }
1372 pcdev->irq_csi = irq_csi;
1373 pcdev->base_csi = base_csi;
1374 pcdev->base_dma = res_csi->start;
1375 pcdev->dev = &pdev->dev;
1376
2c9ba37d
SH
1377 if (cpu_is_mx25()) {
1378 err = request_irq(pcdev->irq_csi, mx25_camera_irq, 0,
1379 MX2_CAM_DRV_NAME, pcdev);
1380 if (err) {
1381 dev_err(pcdev->dev, "Camera interrupt register failed \n");
1382 goto exit_iounmap;
1383 }
2066930d
BS
1384 }
1385
1386 if (cpu_is_mx27()) {
1387 /* EMMA support */
1388 res_emma = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1389 irq_emma = platform_get_irq(pdev, 1);
1390
2c9ba37d
SH
1391 if (!res_emma || !irq_emma) {
1392 dev_err(&pdev->dev, "no EMMA resources\n");
1393 goto exit_free_irq;
2066930d 1394 }
2c9ba37d
SH
1395
1396 pcdev->res_emma = res_emma;
1397 pcdev->irq_emma = irq_emma;
1398 if (mx27_camera_emma_init(pcdev))
1399 goto exit_free_irq;
2066930d
BS
1400 }
1401
1402 pcdev->soc_host.drv_name = MX2_CAM_DRV_NAME,
1403 pcdev->soc_host.ops = &mx2_soc_camera_host_ops,
1404 pcdev->soc_host.priv = pcdev;
1405 pcdev->soc_host.v4l2_dev.dev = &pdev->dev;
1406 pcdev->soc_host.nr = pdev->id;
c6a41e32
JM
1407
1408 pcdev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
1409 if (IS_ERR(pcdev->alloc_ctx)) {
1410 err = PTR_ERR(pcdev->alloc_ctx);
1411 goto eallocctx;
1412 }
2066930d
BS
1413 err = soc_camera_host_register(&pcdev->soc_host);
1414 if (err)
1415 goto exit_free_emma;
1416
45f4d4e8
MG
1417 dev_info(&pdev->dev, "MX2 Camera (CSI) driver probed, clock frequency: %ld\n",
1418 clk_get_rate(pcdev->clk_csi));
1419
2066930d
BS
1420 return 0;
1421
1422exit_free_emma:
c6a41e32
JM
1423 vb2_dma_contig_cleanup_ctx(pcdev->alloc_ctx);
1424eallocctx:
2c9ba37d 1425 if (cpu_is_mx27()) {
2066930d
BS
1426 free_irq(pcdev->irq_emma, pcdev);
1427 clk_disable(pcdev->clk_emma);
1428 clk_put(pcdev->clk_emma);
1429 iounmap(pcdev->base_emma);
2c9ba37d 1430 release_mem_region(pcdev->res_emma->start, resource_size(pcdev->res_emma));
2066930d
BS
1431 }
1432exit_free_irq:
2c9ba37d
SH
1433 if (cpu_is_mx25())
1434 free_irq(pcdev->irq_csi, pcdev);
2066930d
BS
1435exit_iounmap:
1436 iounmap(base_csi);
1437exit_release:
1438 release_mem_region(res_csi->start, resource_size(res_csi));
1439exit_dma_free:
2066930d 1440 clk_put(pcdev->clk_csi);
2066930d
BS
1441exit_kfree:
1442 kfree(pcdev);
1443exit:
1444 return err;
1445}
1446
1447static int __devexit mx2_camera_remove(struct platform_device *pdev)
1448{
1449 struct soc_camera_host *soc_host = to_soc_camera_host(&pdev->dev);
1450 struct mx2_camera_dev *pcdev = container_of(soc_host,
1451 struct mx2_camera_dev, soc_host);
1452 struct resource *res;
1453
1454 clk_put(pcdev->clk_csi);
2c9ba37d
SH
1455 if (cpu_is_mx25())
1456 free_irq(pcdev->irq_csi, pcdev);
2066930d 1457 if (cpu_is_mx27())
2066930d
BS
1458 free_irq(pcdev->irq_emma, pcdev);
1459
1460 soc_camera_host_unregister(&pcdev->soc_host);
1461
c6a41e32
JM
1462 vb2_dma_contig_cleanup_ctx(pcdev->alloc_ctx);
1463
2066930d
BS
1464 iounmap(pcdev->base_csi);
1465
2c9ba37d 1466 if (cpu_is_mx27()) {
2066930d
BS
1467 clk_disable(pcdev->clk_emma);
1468 clk_put(pcdev->clk_emma);
1469 iounmap(pcdev->base_emma);
1470 res = pcdev->res_emma;
1471 release_mem_region(res->start, resource_size(res));
1472 }
1473
1474 res = pcdev->res_csi;
1475 release_mem_region(res->start, resource_size(res));
1476
1477 kfree(pcdev);
1478
1479 dev_info(&pdev->dev, "MX2 Camera driver unloaded\n");
1480
1481 return 0;
1482}
1483
1484static struct platform_driver mx2_camera_driver = {
1485 .driver = {
1486 .name = MX2_CAM_DRV_NAME,
1487 },
1488 .remove = __devexit_p(mx2_camera_remove),
1489};
1490
1491
1492static int __init mx2_camera_init(void)
1493{
1494 return platform_driver_probe(&mx2_camera_driver, &mx2_camera_probe);
1495}
1496
1497static void __exit mx2_camera_exit(void)
1498{
1499 return platform_driver_unregister(&mx2_camera_driver);
1500}
1501
1502module_init(mx2_camera_init);
1503module_exit(mx2_camera_exit);
1504
1505MODULE_DESCRIPTION("i.MX27/i.MX25 SoC Camera Host driver");
1506MODULE_AUTHOR("Sascha Hauer <sha@pengutronix.de>");
1507MODULE_LICENSE("GPL");
64dc3c1a 1508MODULE_VERSION(MX2_CAM_VERSION);
This page took 0.233633 seconds and 5 git commands to generate.