blob: f771f53b2998891ec3d4a739b4d4de4bf4fbe966 [file] [log] [blame]
Baruch Siach20669302010-07-04 07:55:10 +03001/*
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
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 */
12
13#include <linux/init.h>
14#include <linux/module.h>
15#include <linux/io.h>
16#include <linux/delay.h>
17#include <linux/slab.h>
18#include <linux/dma-mapping.h>
19#include <linux/errno.h>
20#include <linux/fs.h>
21#include <linux/interrupt.h>
22#include <linux/kernel.h>
23#include <linux/mm.h>
24#include <linux/moduleparam.h>
25#include <linux/time.h>
Baruch Siach20669302010-07-04 07:55:10 +030026#include <linux/device.h>
27#include <linux/platform_device.h>
28#include <linux/mutex.h>
29#include <linux/clk.h>
30
31#include <media/v4l2-common.h>
32#include <media/v4l2-dev.h>
Sascha Hauer6b101922010-11-08 17:52:45 -030033#include <media/videobuf-core.h>
Baruch Siach20669302010-07-04 07:55:10 +030034#include <media/videobuf-dma-contig.h>
35#include <media/soc_camera.h>
36#include <media/soc_mediabus.h>
37
38#include <linux/videodev2.h>
39
40#include <mach/mx2_cam.h>
Baruch Siach20669302010-07-04 07:55:10 +030041#include <mach/hardware.h>
42
43#include <asm/dma.h>
44
45#define MX2_CAM_DRV_NAME "mx2-camera"
Mauro Carvalho Chehab64dc3c12011-06-25 11:28:37 -030046#define MX2_CAM_VERSION "0.0.6"
Baruch Siach20669302010-07-04 07:55:10 +030047#define MX2_CAM_DRIVER_DESCRIPTION "i.MX2x_Camera"
48
49/* reset values */
50#define CSICR1_RESET_VAL 0x40000800
51#define CSICR2_RESET_VAL 0x0
52#define CSICR3_RESET_VAL 0x0
53
54/* csi control reg 1 */
55#define CSICR1_SWAP16_EN (1 << 31)
56#define CSICR1_EXT_VSYNC (1 << 30)
57#define CSICR1_EOF_INTEN (1 << 29)
58#define CSICR1_PRP_IF_EN (1 << 28)
59#define CSICR1_CCIR_MODE (1 << 27)
60#define CSICR1_COF_INTEN (1 << 26)
61#define CSICR1_SF_OR_INTEN (1 << 25)
62#define CSICR1_RF_OR_INTEN (1 << 24)
63#define CSICR1_STATFF_LEVEL (3 << 22)
64#define CSICR1_STATFF_INTEN (1 << 21)
65#define CSICR1_RXFF_LEVEL(l) (((l) & 3) << 19) /* MX27 */
66#define CSICR1_FB2_DMA_INTEN (1 << 20) /* MX25 */
67#define CSICR1_FB1_DMA_INTEN (1 << 19) /* MX25 */
68#define CSICR1_RXFF_INTEN (1 << 18)
69#define CSICR1_SOF_POL (1 << 17)
70#define CSICR1_SOF_INTEN (1 << 16)
71#define CSICR1_MCLKDIV(d) (((d) & 0xF) << 12)
72#define CSICR1_HSYNC_POL (1 << 11)
73#define CSICR1_CCIR_EN (1 << 10)
74#define CSICR1_MCLKEN (1 << 9)
75#define CSICR1_FCC (1 << 8)
76#define CSICR1_PACK_DIR (1 << 7)
77#define CSICR1_CLR_STATFIFO (1 << 6)
78#define CSICR1_CLR_RXFIFO (1 << 5)
79#define CSICR1_GCLK_MODE (1 << 4)
80#define CSICR1_INV_DATA (1 << 3)
81#define CSICR1_INV_PCLK (1 << 2)
82#define CSICR1_REDGE (1 << 1)
83
84#define SHIFT_STATFF_LEVEL 22
85#define SHIFT_RXFF_LEVEL 19
86#define SHIFT_MCLKDIV 12
87
88/* control reg 3 */
89#define CSICR3_FRMCNT (0xFFFF << 16)
90#define CSICR3_FRMCNT_RST (1 << 15)
91#define CSICR3_DMA_REFLASH_RFF (1 << 14)
92#define CSICR3_DMA_REFLASH_SFF (1 << 13)
93#define CSICR3_DMA_REQ_EN_RFF (1 << 12)
94#define CSICR3_DMA_REQ_EN_SFF (1 << 11)
95#define CSICR3_RXFF_LEVEL(l) (((l) & 7) << 4) /* MX25 */
96#define CSICR3_CSI_SUP (1 << 3)
97#define CSICR3_ZERO_PACK_EN (1 << 2)
98#define CSICR3_ECC_INT_EN (1 << 1)
99#define CSICR3_ECC_AUTO_EN (1 << 0)
100
101#define SHIFT_FRMCNT 16
102
103/* csi status reg */
104#define CSISR_SFF_OR_INT (1 << 25)
105#define CSISR_RFF_OR_INT (1 << 24)
106#define CSISR_STATFF_INT (1 << 21)
107#define CSISR_DMA_TSF_FB2_INT (1 << 20) /* MX25 */
108#define CSISR_DMA_TSF_FB1_INT (1 << 19) /* MX25 */
109#define CSISR_RXFF_INT (1 << 18)
110#define CSISR_EOF_INT (1 << 17)
111#define CSISR_SOF_INT (1 << 16)
112#define CSISR_F2_INT (1 << 15)
113#define CSISR_F1_INT (1 << 14)
114#define CSISR_COF_INT (1 << 13)
115#define CSISR_ECC_INT (1 << 1)
116#define CSISR_DRDY (1 << 0)
117
118#define CSICR1 0x00
119#define CSICR2 0x04
120#define CSISR (cpu_is_mx27() ? 0x08 : 0x18)
121#define CSISTATFIFO 0x0c
122#define CSIRFIFO 0x10
123#define CSIRXCNT 0x14
124#define CSICR3 (cpu_is_mx27() ? 0x1C : 0x08)
125#define CSIDMASA_STATFIFO 0x20
126#define CSIDMATA_STATFIFO 0x24
127#define CSIDMASA_FB1 0x28
128#define CSIDMASA_FB2 0x2c
129#define CSIFBUF_PARA 0x30
130#define CSIIMAG_PARA 0x34
131
132/* EMMA PrP */
133#define PRP_CNTL 0x00
134#define PRP_INTR_CNTL 0x04
135#define PRP_INTRSTATUS 0x08
136#define PRP_SOURCE_Y_PTR 0x0c
137#define PRP_SOURCE_CB_PTR 0x10
138#define PRP_SOURCE_CR_PTR 0x14
139#define PRP_DEST_RGB1_PTR 0x18
140#define PRP_DEST_RGB2_PTR 0x1c
141#define PRP_DEST_Y_PTR 0x20
142#define PRP_DEST_CB_PTR 0x24
143#define PRP_DEST_CR_PTR 0x28
144#define PRP_SRC_FRAME_SIZE 0x2c
145#define PRP_DEST_CH1_LINE_STRIDE 0x30
146#define PRP_SRC_PIXEL_FORMAT_CNTL 0x34
147#define PRP_CH1_PIXEL_FORMAT_CNTL 0x38
148#define PRP_CH1_OUT_IMAGE_SIZE 0x3c
149#define PRP_CH2_OUT_IMAGE_SIZE 0x40
150#define PRP_SRC_LINE_STRIDE 0x44
151#define PRP_CSC_COEF_012 0x48
152#define PRP_CSC_COEF_345 0x4c
153#define PRP_CSC_COEF_678 0x50
154#define PRP_CH1_RZ_HORI_COEF1 0x54
155#define PRP_CH1_RZ_HORI_COEF2 0x58
156#define PRP_CH1_RZ_HORI_VALID 0x5c
157#define PRP_CH1_RZ_VERT_COEF1 0x60
158#define PRP_CH1_RZ_VERT_COEF2 0x64
159#define PRP_CH1_RZ_VERT_VALID 0x68
160#define PRP_CH2_RZ_HORI_COEF1 0x6c
161#define PRP_CH2_RZ_HORI_COEF2 0x70
162#define PRP_CH2_RZ_HORI_VALID 0x74
163#define PRP_CH2_RZ_VERT_COEF1 0x78
164#define PRP_CH2_RZ_VERT_COEF2 0x7c
165#define PRP_CH2_RZ_VERT_VALID 0x80
166
167#define PRP_CNTL_CH1EN (1 << 0)
168#define PRP_CNTL_CH2EN (1 << 1)
169#define PRP_CNTL_CSIEN (1 << 2)
170#define PRP_CNTL_DATA_IN_YUV420 (0 << 3)
171#define PRP_CNTL_DATA_IN_YUV422 (1 << 3)
172#define PRP_CNTL_DATA_IN_RGB16 (2 << 3)
173#define PRP_CNTL_DATA_IN_RGB32 (3 << 3)
174#define PRP_CNTL_CH1_OUT_RGB8 (0 << 5)
175#define PRP_CNTL_CH1_OUT_RGB16 (1 << 5)
176#define PRP_CNTL_CH1_OUT_RGB32 (2 << 5)
177#define PRP_CNTL_CH1_OUT_YUV422 (3 << 5)
178#define PRP_CNTL_CH2_OUT_YUV420 (0 << 7)
179#define PRP_CNTL_CH2_OUT_YUV422 (1 << 7)
180#define PRP_CNTL_CH2_OUT_YUV444 (2 << 7)
181#define PRP_CNTL_CH1_LEN (1 << 9)
182#define PRP_CNTL_CH2_LEN (1 << 10)
183#define PRP_CNTL_SKIP_FRAME (1 << 11)
184#define PRP_CNTL_SWRST (1 << 12)
185#define PRP_CNTL_CLKEN (1 << 13)
186#define PRP_CNTL_WEN (1 << 14)
187#define PRP_CNTL_CH1BYP (1 << 15)
188#define PRP_CNTL_IN_TSKIP(x) ((x) << 16)
189#define PRP_CNTL_CH1_TSKIP(x) ((x) << 19)
190#define PRP_CNTL_CH2_TSKIP(x) ((x) << 22)
191#define PRP_CNTL_INPUT_FIFO_LEVEL(x) ((x) << 25)
192#define PRP_CNTL_RZ_FIFO_LEVEL(x) ((x) << 27)
193#define PRP_CNTL_CH2B1EN (1 << 29)
194#define PRP_CNTL_CH2B2EN (1 << 30)
195#define PRP_CNTL_CH2FEN (1 << 31)
196
197/* IRQ Enable and status register */
198#define PRP_INTR_RDERR (1 << 0)
199#define PRP_INTR_CH1WERR (1 << 1)
200#define PRP_INTR_CH2WERR (1 << 2)
201#define PRP_INTR_CH1FC (1 << 3)
202#define PRP_INTR_CH2FC (1 << 5)
203#define PRP_INTR_LBOVF (1 << 7)
204#define PRP_INTR_CH2OVF (1 << 8)
205
Baruch Siach20669302010-07-04 07:55:10 +0300206#define MAX_VIDEO_MEM 16
207
Javier Martinf4109912011-12-14 13:30:14 -0300208struct mx2_prp_cfg {
209 int channel;
210 u32 in_fmt;
211 u32 out_fmt;
212 u32 src_pixel;
213 u32 ch1_pixel;
214 u32 irq_flags;
215};
216
217/* prp configuration for a client-host fmt pair */
218struct mx2_fmt_cfg {
219 enum v4l2_mbus_pixelcode in_fmt;
220 u32 out_fmt;
221 struct mx2_prp_cfg cfg;
222};
223
Baruch Siach20669302010-07-04 07:55:10 +0300224struct mx2_camera_dev {
225 struct device *dev;
226 struct soc_camera_host soc_host;
227 struct soc_camera_device *icd;
228 struct clk *clk_csi, *clk_emma;
229
230 unsigned int irq_csi, irq_emma;
231 void __iomem *base_csi, *base_emma;
232 unsigned long base_dma;
233
234 struct mx2_camera_platform_data *pdata;
235 struct resource *res_csi, *res_emma;
236 unsigned long platform_flags;
237
238 struct list_head capture;
239 struct list_head active_bufs;
240
241 spinlock_t lock;
242
243 int dma;
244 struct mx2_buffer *active;
245 struct mx2_buffer *fb1_active;
246 struct mx2_buffer *fb2_active;
247
Baruch Siach20669302010-07-04 07:55:10 +0300248 u32 csicr1;
249
Baruch Siach79d3c2c2010-07-27 08:03:30 +0300250 void *discard_buffer;
Baruch Siach20669302010-07-04 07:55:10 +0300251 dma_addr_t discard_buffer_dma;
252 size_t discard_size;
Javier Martinf4109912011-12-14 13:30:14 -0300253 struct mx2_fmt_cfg *emma_prp;
Javier Martinccd1a492011-12-22 12:12:00 -0300254 u32 frame_count;
Baruch Siach20669302010-07-04 07:55:10 +0300255};
256
257/* buffer for one video frame */
258struct mx2_buffer {
259 /* common v4l buffer stuff -- must be first */
260 struct videobuf_buffer vb;
261
262 enum v4l2_mbus_pixelcode code;
263
264 int bufnum;
265};
266
Javier Martinf4109912011-12-14 13:30:14 -0300267static struct mx2_fmt_cfg mx27_emma_prp_table[] = {
268 /*
269 * This is a generic configuration which is valid for most
270 * prp input-output format combinations.
271 * We set the incomming and outgoing pixelformat to a
272 * 16 Bit wide format and adjust the bytesperline
273 * accordingly. With this configuration the inputdata
274 * will not be changed by the emma and could be any type
275 * of 16 Bit Pixelformat.
276 */
277 {
278 .in_fmt = 0,
279 .out_fmt = 0,
280 .cfg = {
281 .channel = 1,
282 .in_fmt = PRP_CNTL_DATA_IN_RGB16,
283 .out_fmt = PRP_CNTL_CH1_OUT_RGB16,
284 .src_pixel = 0x2ca00565, /* RGB565 */
285 .ch1_pixel = 0x2ca00565, /* RGB565 */
286 .irq_flags = PRP_INTR_RDERR | PRP_INTR_CH1WERR |
287 PRP_INTR_CH1FC | PRP_INTR_LBOVF,
288 }
289 },
290 {
291 .in_fmt = V4L2_MBUS_FMT_YUYV8_2X8,
292 .out_fmt = V4L2_PIX_FMT_YUV420,
293 .cfg = {
294 .channel = 2,
295 .in_fmt = PRP_CNTL_DATA_IN_YUV422,
296 .out_fmt = PRP_CNTL_CH2_OUT_YUV420,
297 .src_pixel = 0x22000888, /* YUV422 (YUYV) */
298 .irq_flags = PRP_INTR_RDERR | PRP_INTR_CH2WERR |
299 PRP_INTR_CH2FC | PRP_INTR_LBOVF |
300 PRP_INTR_CH2OVF,
301 }
302 },
303};
304
305static struct mx2_fmt_cfg *mx27_emma_prp_get_format(
306 enum v4l2_mbus_pixelcode in_fmt,
307 u32 out_fmt)
308{
309 int i;
310
311 for (i = 1; i < ARRAY_SIZE(mx27_emma_prp_table); i++)
312 if ((mx27_emma_prp_table[i].in_fmt == in_fmt) &&
313 (mx27_emma_prp_table[i].out_fmt == out_fmt)) {
314 return &mx27_emma_prp_table[i];
315 }
316 /* If no match return the most generic configuration */
317 return &mx27_emma_prp_table[0];
318};
319
Baruch Siach20669302010-07-04 07:55:10 +0300320static void mx2_camera_deactivate(struct mx2_camera_dev *pcdev)
321{
322 unsigned long flags;
323
324 clk_disable(pcdev->clk_csi);
325 writel(0, pcdev->base_csi + CSICR1);
Sascha Hauer2c9ba372012-02-20 07:55:24 -0300326 if (cpu_is_mx27()) {
Baruch Siach20669302010-07-04 07:55:10 +0300327 writel(0, pcdev->base_emma + PRP_CNTL);
328 } else if (cpu_is_mx25()) {
329 spin_lock_irqsave(&pcdev->lock, flags);
330 pcdev->fb1_active = NULL;
331 pcdev->fb2_active = NULL;
332 writel(0, pcdev->base_csi + CSIDMASA_FB1);
333 writel(0, pcdev->base_csi + CSIDMASA_FB2);
334 spin_unlock_irqrestore(&pcdev->lock, flags);
335 }
336}
337
338/*
339 * The following two functions absolutely depend on the fact, that
340 * there can be only one camera on mx2 camera sensor interface
341 */
342static int mx2_camera_add_device(struct soc_camera_device *icd)
343{
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300344 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
Baruch Siach20669302010-07-04 07:55:10 +0300345 struct mx2_camera_dev *pcdev = ici->priv;
346 int ret;
347 u32 csicr1;
348
349 if (pcdev->icd)
350 return -EBUSY;
351
352 ret = clk_enable(pcdev->clk_csi);
353 if (ret < 0)
354 return ret;
355
356 csicr1 = CSICR1_MCLKEN;
357
Sascha Hauer2c9ba372012-02-20 07:55:24 -0300358 if (cpu_is_mx27()) {
Baruch Siach20669302010-07-04 07:55:10 +0300359 csicr1 |= CSICR1_PRP_IF_EN | CSICR1_FCC |
360 CSICR1_RXFF_LEVEL(0);
361 } else if (cpu_is_mx27())
362 csicr1 |= CSICR1_SOF_INTEN | CSICR1_RXFF_LEVEL(2);
363
364 pcdev->csicr1 = csicr1;
365 writel(pcdev->csicr1, pcdev->base_csi + CSICR1);
366
367 pcdev->icd = icd;
Javier Martinccd1a492011-12-22 12:12:00 -0300368 pcdev->frame_count = 0;
Baruch Siach20669302010-07-04 07:55:10 +0300369
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300370 dev_info(icd->parent, "Camera driver attached to camera %d\n",
Baruch Siach20669302010-07-04 07:55:10 +0300371 icd->devnum);
372
373 return 0;
374}
375
376static void mx2_camera_remove_device(struct soc_camera_device *icd)
377{
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300378 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
Baruch Siach20669302010-07-04 07:55:10 +0300379 struct mx2_camera_dev *pcdev = ici->priv;
380
381 BUG_ON(icd != pcdev->icd);
382
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300383 dev_info(icd->parent, "Camera driver detached from camera %d\n",
Baruch Siach20669302010-07-04 07:55:10 +0300384 icd->devnum);
385
386 mx2_camera_deactivate(pcdev);
387
388 if (pcdev->discard_buffer) {
389 dma_free_coherent(ici->v4l2_dev.dev, pcdev->discard_size,
390 pcdev->discard_buffer,
391 pcdev->discard_buffer_dma);
392 pcdev->discard_buffer = NULL;
393 }
394
395 pcdev->icd = NULL;
396}
397
Baruch Siach20669302010-07-04 07:55:10 +0300398static void mx25_camera_frame_done(struct mx2_camera_dev *pcdev, int fb,
399 int state)
400{
401 struct videobuf_buffer *vb;
402 struct mx2_buffer *buf;
403 struct mx2_buffer **fb_active = fb == 1 ? &pcdev->fb1_active :
404 &pcdev->fb2_active;
405 u32 fb_reg = fb == 1 ? CSIDMASA_FB1 : CSIDMASA_FB2;
406 unsigned long flags;
407
408 spin_lock_irqsave(&pcdev->lock, flags);
409
Baruch Siach5384a122010-07-27 09:06:07 -0300410 if (*fb_active == NULL)
411 goto out;
412
Baruch Siach20669302010-07-04 07:55:10 +0300413 vb = &(*fb_active)->vb;
414 dev_dbg(pcdev->dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
415 vb, vb->baddr, vb->bsize);
416
417 vb->state = state;
418 do_gettimeofday(&vb->ts);
419 vb->field_count++;
420
421 wake_up(&vb->done);
422
423 if (list_empty(&pcdev->capture)) {
424 buf = NULL;
425 writel(0, pcdev->base_csi + fb_reg);
426 } else {
427 buf = list_entry(pcdev->capture.next, struct mx2_buffer,
428 vb.queue);
429 vb = &buf->vb;
430 list_del(&vb->queue);
431 vb->state = VIDEOBUF_ACTIVE;
432 writel(videobuf_to_dma_contig(vb), pcdev->base_csi + fb_reg);
433 }
434
435 *fb_active = buf;
436
Baruch Siach5384a122010-07-27 09:06:07 -0300437out:
Baruch Siach20669302010-07-04 07:55:10 +0300438 spin_unlock_irqrestore(&pcdev->lock, flags);
439}
440
441static irqreturn_t mx25_camera_irq(int irq_csi, void *data)
442{
443 struct mx2_camera_dev *pcdev = data;
444 u32 status = readl(pcdev->base_csi + CSISR);
445
446 if (status & CSISR_DMA_TSF_FB1_INT)
447 mx25_camera_frame_done(pcdev, 1, VIDEOBUF_DONE);
448 else if (status & CSISR_DMA_TSF_FB2_INT)
449 mx25_camera_frame_done(pcdev, 2, VIDEOBUF_DONE);
450
451 /* FIXME: handle CSISR_RFF_OR_INT */
452
453 writel(status, pcdev->base_csi + CSISR);
454
455 return IRQ_HANDLED;
456}
457
458/*
459 * Videobuf operations
460 */
461static int mx2_videobuf_setup(struct videobuf_queue *vq, unsigned int *count,
462 unsigned int *size)
463{
464 struct soc_camera_device *icd = vq->priv_data;
465 int bytes_per_line = soc_mbus_bytes_per_line(icd->user_width,
466 icd->current_fmt->host_fmt);
467
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300468 dev_dbg(icd->parent, "count=%d, size=%d\n", *count, *size);
Baruch Siach20669302010-07-04 07:55:10 +0300469
470 if (bytes_per_line < 0)
471 return bytes_per_line;
472
473 *size = bytes_per_line * icd->user_height;
474
475 if (0 == *count)
476 *count = 32;
477 if (*size * *count > MAX_VIDEO_MEM * 1024 * 1024)
478 *count = (MAX_VIDEO_MEM * 1024 * 1024) / *size;
479
480 return 0;
481}
482
483static void free_buffer(struct videobuf_queue *vq, struct mx2_buffer *buf)
484{
485 struct soc_camera_device *icd = vq->priv_data;
486 struct videobuf_buffer *vb = &buf->vb;
487
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300488 dev_dbg(icd->parent, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
Baruch Siach20669302010-07-04 07:55:10 +0300489 vb, vb->baddr, vb->bsize);
490
491 /*
492 * This waits until this buffer is out of danger, i.e., until it is no
Baruch Siach88bfd0b2010-07-27 09:06:09 -0300493 * longer in state VIDEOBUF_QUEUED or VIDEOBUF_ACTIVE
Baruch Siach20669302010-07-04 07:55:10 +0300494 */
Hans Verkuil0e0809a2010-09-26 09:01:26 -0300495 videobuf_waiton(vq, vb, 0, 0);
Baruch Siach20669302010-07-04 07:55:10 +0300496
497 videobuf_dma_contig_free(vq, vb);
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300498 dev_dbg(icd->parent, "%s freed\n", __func__);
Baruch Siach20669302010-07-04 07:55:10 +0300499
500 vb->state = VIDEOBUF_NEEDS_INIT;
501}
502
503static int mx2_videobuf_prepare(struct videobuf_queue *vq,
504 struct videobuf_buffer *vb, enum v4l2_field field)
505{
506 struct soc_camera_device *icd = vq->priv_data;
507 struct mx2_buffer *buf = container_of(vb, struct mx2_buffer, vb);
508 int bytes_per_line = soc_mbus_bytes_per_line(icd->user_width,
509 icd->current_fmt->host_fmt);
510 int ret = 0;
511
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300512 dev_dbg(icd->parent, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
Baruch Siach20669302010-07-04 07:55:10 +0300513 vb, vb->baddr, vb->bsize);
514
515 if (bytes_per_line < 0)
516 return bytes_per_line;
517
518#ifdef DEBUG
519 /*
520 * This can be useful if you want to see if we actually fill
521 * the buffer with something
522 */
523 memset((void *)vb->baddr, 0xaa, vb->bsize);
524#endif
525
526 if (buf->code != icd->current_fmt->code ||
527 vb->width != icd->user_width ||
528 vb->height != icd->user_height ||
529 vb->field != field) {
530 buf->code = icd->current_fmt->code;
531 vb->width = icd->user_width;
532 vb->height = icd->user_height;
533 vb->field = field;
534 vb->state = VIDEOBUF_NEEDS_INIT;
535 }
536
537 vb->size = bytes_per_line * vb->height;
538 if (vb->baddr && vb->bsize < vb->size) {
539 ret = -EINVAL;
540 goto out;
541 }
542
543 if (vb->state == VIDEOBUF_NEEDS_INIT) {
544 ret = videobuf_iolock(vq, vb, NULL);
545 if (ret)
546 goto fail;
547
548 vb->state = VIDEOBUF_PREPARED;
549 }
550
551 return 0;
552
553fail:
554 free_buffer(vq, buf);
555out:
556 return ret;
557}
558
559static void mx2_videobuf_queue(struct videobuf_queue *vq,
560 struct videobuf_buffer *vb)
561{
562 struct soc_camera_device *icd = vq->priv_data;
563 struct soc_camera_host *ici =
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300564 to_soc_camera_host(icd->parent);
Baruch Siach20669302010-07-04 07:55:10 +0300565 struct mx2_camera_dev *pcdev = ici->priv;
566 struct mx2_buffer *buf = container_of(vb, struct mx2_buffer, vb);
567 unsigned long flags;
568
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300569 dev_dbg(icd->parent, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
Baruch Siach20669302010-07-04 07:55:10 +0300570 vb, vb->baddr, vb->bsize);
571
572 spin_lock_irqsave(&pcdev->lock, flags);
573
574 vb->state = VIDEOBUF_QUEUED;
575 list_add_tail(&vb->queue, &pcdev->capture);
576
Sascha Hauer2c9ba372012-02-20 07:55:24 -0300577 if (cpu_is_mx25()) {
Baruch Siach20669302010-07-04 07:55:10 +0300578 u32 csicr3, dma_inten = 0;
579
580 if (pcdev->fb1_active == NULL) {
581 writel(videobuf_to_dma_contig(vb),
582 pcdev->base_csi + CSIDMASA_FB1);
583 pcdev->fb1_active = buf;
584 dma_inten = CSICR1_FB1_DMA_INTEN;
585 } else if (pcdev->fb2_active == NULL) {
586 writel(videobuf_to_dma_contig(vb),
587 pcdev->base_csi + CSIDMASA_FB2);
588 pcdev->fb2_active = buf;
589 dma_inten = CSICR1_FB2_DMA_INTEN;
590 }
591
592 if (dma_inten) {
593 list_del(&vb->queue);
594 vb->state = VIDEOBUF_ACTIVE;
595
596 csicr3 = readl(pcdev->base_csi + CSICR3);
597
598 /* Reflash DMA */
599 writel(csicr3 | CSICR3_DMA_REFLASH_RFF,
600 pcdev->base_csi + CSICR3);
601
602 /* clear & enable interrupts */
603 writel(dma_inten, pcdev->base_csi + CSISR);
604 pcdev->csicr1 |= dma_inten;
605 writel(pcdev->csicr1, pcdev->base_csi + CSICR1);
606
607 /* enable DMA */
608 csicr3 |= CSICR3_DMA_REQ_EN_RFF | CSICR3_RXFF_LEVEL(1);
609 writel(csicr3, pcdev->base_csi + CSICR3);
610 }
611 }
612
Baruch Siach20669302010-07-04 07:55:10 +0300613 spin_unlock_irqrestore(&pcdev->lock, flags);
614}
615
616static void mx2_videobuf_release(struct videobuf_queue *vq,
617 struct videobuf_buffer *vb)
618{
619 struct soc_camera_device *icd = vq->priv_data;
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300620 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
Baruch Siach20669302010-07-04 07:55:10 +0300621 struct mx2_camera_dev *pcdev = ici->priv;
622 struct mx2_buffer *buf = container_of(vb, struct mx2_buffer, vb);
623 unsigned long flags;
624
625#ifdef DEBUG
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300626 dev_dbg(icd->parent, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
Baruch Siach20669302010-07-04 07:55:10 +0300627 vb, vb->baddr, vb->bsize);
628
629 switch (vb->state) {
630 case VIDEOBUF_ACTIVE:
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300631 dev_info(icd->parent, "%s (active)\n", __func__);
Baruch Siach20669302010-07-04 07:55:10 +0300632 break;
633 case VIDEOBUF_QUEUED:
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300634 dev_info(icd->parent, "%s (queued)\n", __func__);
Baruch Siach20669302010-07-04 07:55:10 +0300635 break;
636 case VIDEOBUF_PREPARED:
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300637 dev_info(icd->parent, "%s (prepared)\n", __func__);
Baruch Siach20669302010-07-04 07:55:10 +0300638 break;
639 default:
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300640 dev_info(icd->parent, "%s (unknown) %d\n", __func__,
Baruch Siach20669302010-07-04 07:55:10 +0300641 vb->state);
642 break;
643 }
644#endif
645
646 /*
647 * Terminate only queued but inactive buffers. Active buffers are
648 * released when they become inactive after videobuf_waiton().
649 *
Baruch Siach7c6b7312010-07-27 09:06:10 -0300650 * FIXME: implement forced termination of active buffers for mx27 and
651 * mx27 eMMA, so that the user won't get stuck in an uninterruptible
652 * state. This requires a specific handling for each of the these DMA
653 * types.
Baruch Siach20669302010-07-04 07:55:10 +0300654 */
655 spin_lock_irqsave(&pcdev->lock, flags);
656 if (vb->state == VIDEOBUF_QUEUED) {
657 list_del(&vb->queue);
658 vb->state = VIDEOBUF_ERROR;
Baruch Siach7c6b7312010-07-27 09:06:10 -0300659 } else if (cpu_is_mx25() && vb->state == VIDEOBUF_ACTIVE) {
660 if (pcdev->fb1_active == buf) {
661 pcdev->csicr1 &= ~CSICR1_FB1_DMA_INTEN;
662 writel(0, pcdev->base_csi + CSIDMASA_FB1);
663 pcdev->fb1_active = NULL;
664 } else if (pcdev->fb2_active == buf) {
665 pcdev->csicr1 &= ~CSICR1_FB2_DMA_INTEN;
666 writel(0, pcdev->base_csi + CSIDMASA_FB2);
667 pcdev->fb2_active = NULL;
668 }
669 writel(pcdev->csicr1, pcdev->base_csi + CSICR1);
670 vb->state = VIDEOBUF_ERROR;
Baruch Siach20669302010-07-04 07:55:10 +0300671 }
672 spin_unlock_irqrestore(&pcdev->lock, flags);
673
674 free_buffer(vq, buf);
675}
676
677static struct videobuf_queue_ops mx2_videobuf_ops = {
678 .buf_setup = mx2_videobuf_setup,
679 .buf_prepare = mx2_videobuf_prepare,
680 .buf_queue = mx2_videobuf_queue,
681 .buf_release = mx2_videobuf_release,
682};
683
684static void mx2_camera_init_videobuf(struct videobuf_queue *q,
685 struct soc_camera_device *icd)
686{
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300687 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
Baruch Siach20669302010-07-04 07:55:10 +0300688 struct mx2_camera_dev *pcdev = ici->priv;
689
690 videobuf_queue_dma_contig_init(q, &mx2_videobuf_ops, pcdev->dev,
691 &pcdev->lock, V4L2_BUF_TYPE_VIDEO_CAPTURE,
Guennadi Liakhovetskib6a633c2010-12-25 17:40:26 -0300692 V4L2_FIELD_NONE, sizeof(struct mx2_buffer),
693 icd, &icd->video_lock);
Baruch Siach20669302010-07-04 07:55:10 +0300694}
695
Guennadi Liakhovetskidb592a22011-07-27 12:38:11 -0300696#define MX2_BUS_FLAGS (V4L2_MBUS_MASTER | \
697 V4L2_MBUS_VSYNC_ACTIVE_HIGH | \
698 V4L2_MBUS_VSYNC_ACTIVE_LOW | \
699 V4L2_MBUS_HSYNC_ACTIVE_HIGH | \
700 V4L2_MBUS_HSYNC_ACTIVE_LOW | \
701 V4L2_MBUS_PCLK_SAMPLE_RISING | \
702 V4L2_MBUS_PCLK_SAMPLE_FALLING | \
703 V4L2_MBUS_DATA_ACTIVE_HIGH | \
704 V4L2_MBUS_DATA_ACTIVE_LOW)
Baruch Siach20669302010-07-04 07:55:10 +0300705
706static int mx27_camera_emma_prp_reset(struct mx2_camera_dev *pcdev)
707{
708 u32 cntl;
709 int count = 0;
710
711 cntl = readl(pcdev->base_emma + PRP_CNTL);
712 writel(PRP_CNTL_SWRST, pcdev->base_emma + PRP_CNTL);
713 while (count++ < 100) {
714 if (!(readl(pcdev->base_emma + PRP_CNTL) & PRP_CNTL_SWRST))
715 return 0;
716 barrier();
717 udelay(1);
718 }
719
720 return -ETIMEDOUT;
721}
722
723static void mx27_camera_emma_buf_init(struct soc_camera_device *icd,
724 int bytesperline)
725{
726 struct soc_camera_host *ici =
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300727 to_soc_camera_host(icd->parent);
Baruch Siach20669302010-07-04 07:55:10 +0300728 struct mx2_camera_dev *pcdev = ici->priv;
Javier Martinf4109912011-12-14 13:30:14 -0300729 struct mx2_fmt_cfg *prp = pcdev->emma_prp;
730 u32 imgsize = pcdev->icd->user_height * pcdev->icd->user_width;
Baruch Siach20669302010-07-04 07:55:10 +0300731
Javier Martinf4109912011-12-14 13:30:14 -0300732 if (prp->cfg.channel == 1) {
733 writel(pcdev->discard_buffer_dma,
734 pcdev->base_emma + PRP_DEST_RGB1_PTR);
735 writel(pcdev->discard_buffer_dma,
736 pcdev->base_emma + PRP_DEST_RGB2_PTR);
Baruch Siach20669302010-07-04 07:55:10 +0300737
Javier Martinf4109912011-12-14 13:30:14 -0300738 writel(PRP_CNTL_CH1EN |
739 PRP_CNTL_CSIEN |
740 prp->cfg.in_fmt |
741 prp->cfg.out_fmt |
742 PRP_CNTL_CH1_LEN |
743 PRP_CNTL_CH1BYP |
744 PRP_CNTL_CH1_TSKIP(0) |
745 PRP_CNTL_IN_TSKIP(0),
746 pcdev->base_emma + PRP_CNTL);
747
748 writel((icd->user_width << 16) | icd->user_height,
749 pcdev->base_emma + PRP_SRC_FRAME_SIZE);
750 writel((icd->user_width << 16) | icd->user_height,
751 pcdev->base_emma + PRP_CH1_OUT_IMAGE_SIZE);
752 writel(bytesperline,
753 pcdev->base_emma + PRP_DEST_CH1_LINE_STRIDE);
754 writel(prp->cfg.src_pixel,
755 pcdev->base_emma + PRP_SRC_PIXEL_FORMAT_CNTL);
756 writel(prp->cfg.ch1_pixel,
757 pcdev->base_emma + PRP_CH1_PIXEL_FORMAT_CNTL);
758 } else { /* channel 2 */
759 writel(pcdev->discard_buffer_dma,
760 pcdev->base_emma + PRP_DEST_Y_PTR);
761 writel(pcdev->discard_buffer_dma,
762 pcdev->base_emma + PRP_SOURCE_Y_PTR);
763
764 if (prp->cfg.out_fmt == PRP_CNTL_CH2_OUT_YUV420) {
765 writel(pcdev->discard_buffer_dma + imgsize,
766 pcdev->base_emma + PRP_DEST_CB_PTR);
767 writel(pcdev->discard_buffer_dma + ((5 * imgsize) / 4),
768 pcdev->base_emma + PRP_DEST_CR_PTR);
769 writel(pcdev->discard_buffer_dma + imgsize,
770 pcdev->base_emma + PRP_SOURCE_CB_PTR);
771 writel(pcdev->discard_buffer_dma + ((5 * imgsize) / 4),
772 pcdev->base_emma + PRP_SOURCE_CR_PTR);
773 }
774
775 writel(PRP_CNTL_CH2EN |
Baruch Siach20669302010-07-04 07:55:10 +0300776 PRP_CNTL_CSIEN |
Javier Martinf4109912011-12-14 13:30:14 -0300777 prp->cfg.in_fmt |
778 prp->cfg.out_fmt |
779 PRP_CNTL_CH2_LEN |
780 PRP_CNTL_CH2_TSKIP(0) |
Baruch Siach20669302010-07-04 07:55:10 +0300781 PRP_CNTL_IN_TSKIP(0),
782 pcdev->base_emma + PRP_CNTL);
783
Javier Martinf4109912011-12-14 13:30:14 -0300784 writel((icd->user_width << 16) | icd->user_height,
Baruch Siach20669302010-07-04 07:55:10 +0300785 pcdev->base_emma + PRP_SRC_FRAME_SIZE);
Javier Martinf4109912011-12-14 13:30:14 -0300786
787 writel((icd->user_width << 16) | icd->user_height,
788 pcdev->base_emma + PRP_CH2_OUT_IMAGE_SIZE);
789
790 writel(prp->cfg.src_pixel,
Baruch Siach20669302010-07-04 07:55:10 +0300791 pcdev->base_emma + PRP_SRC_PIXEL_FORMAT_CNTL);
Javier Martinf4109912011-12-14 13:30:14 -0300792
793 }
Baruch Siach20669302010-07-04 07:55:10 +0300794
795 /* Enable interrupts */
Javier Martinf4109912011-12-14 13:30:14 -0300796 writel(prp->cfg.irq_flags, pcdev->base_emma + PRP_INTR_CNTL);
Baruch Siach20669302010-07-04 07:55:10 +0300797}
798
Guennadi Liakhovetski8843d112011-09-21 17:52:51 -0300799static int mx2_camera_set_bus_param(struct soc_camera_device *icd)
Baruch Siach20669302010-07-04 07:55:10 +0300800{
Guennadi Liakhovetskidb592a22011-07-27 12:38:11 -0300801 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
802 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
Baruch Siach20669302010-07-04 07:55:10 +0300803 struct mx2_camera_dev *pcdev = ici->priv;
Guennadi Liakhovetskidb592a22011-07-27 12:38:11 -0300804 struct v4l2_mbus_config cfg = {.type = V4L2_MBUS_PARALLEL,};
805 unsigned long common_flags;
806 int ret;
Baruch Siach20669302010-07-04 07:55:10 +0300807 int bytesperline;
808 u32 csicr1 = pcdev->csicr1;
809
Guennadi Liakhovetskidb592a22011-07-27 12:38:11 -0300810 ret = v4l2_subdev_call(sd, video, g_mbus_config, &cfg);
811 if (!ret) {
812 common_flags = soc_mbus_config_compatible(&cfg, MX2_BUS_FLAGS);
813 if (!common_flags) {
814 dev_warn(icd->parent,
815 "Flags incompatible: camera 0x%x, host 0x%x\n",
816 cfg.flags, MX2_BUS_FLAGS);
817 return -EINVAL;
818 }
819 } else if (ret != -ENOIOCTLCMD) {
Baruch Siach20669302010-07-04 07:55:10 +0300820 return ret;
Guennadi Liakhovetskidb592a22011-07-27 12:38:11 -0300821 } else {
822 common_flags = MX2_BUS_FLAGS;
823 }
Baruch Siach20669302010-07-04 07:55:10 +0300824
Guennadi Liakhovetskidb592a22011-07-27 12:38:11 -0300825 if ((common_flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH) &&
826 (common_flags & V4L2_MBUS_HSYNC_ACTIVE_LOW)) {
827 if (pcdev->platform_flags & MX2_CAMERA_HSYNC_HIGH)
828 common_flags &= ~V4L2_MBUS_HSYNC_ACTIVE_LOW;
829 else
830 common_flags &= ~V4L2_MBUS_HSYNC_ACTIVE_HIGH;
831 }
832
833 if ((common_flags & V4L2_MBUS_PCLK_SAMPLE_RISING) &&
834 (common_flags & V4L2_MBUS_PCLK_SAMPLE_FALLING)) {
835 if (pcdev->platform_flags & MX2_CAMERA_PCLK_SAMPLE_RISING)
836 common_flags &= ~V4L2_MBUS_PCLK_SAMPLE_FALLING;
837 else
838 common_flags &= ~V4L2_MBUS_PCLK_SAMPLE_RISING;
839 }
840
841 cfg.flags = common_flags;
842 ret = v4l2_subdev_call(sd, video, s_mbus_config, &cfg);
843 if (ret < 0 && ret != -ENOIOCTLCMD) {
844 dev_dbg(icd->parent, "camera s_mbus_config(0x%lx) returned %d\n",
845 common_flags, ret);
846 return ret;
847 }
848
849 if (common_flags & V4L2_MBUS_PCLK_SAMPLE_RISING)
Michael Grzeschikd86097e2010-08-03 06:37:55 -0300850 csicr1 |= CSICR1_REDGE;
Guennadi Liakhovetskidb592a22011-07-27 12:38:11 -0300851 if (common_flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH)
Baruch Siach20669302010-07-04 07:55:10 +0300852 csicr1 |= CSICR1_SOF_POL;
Guennadi Liakhovetskidb592a22011-07-27 12:38:11 -0300853 if (common_flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH)
Baruch Siach20669302010-07-04 07:55:10 +0300854 csicr1 |= CSICR1_HSYNC_POL;
855 if (pcdev->platform_flags & MX2_CAMERA_SWAP16)
856 csicr1 |= CSICR1_SWAP16_EN;
857 if (pcdev->platform_flags & MX2_CAMERA_EXT_VSYNC)
858 csicr1 |= CSICR1_EXT_VSYNC;
859 if (pcdev->platform_flags & MX2_CAMERA_CCIR)
860 csicr1 |= CSICR1_CCIR_EN;
861 if (pcdev->platform_flags & MX2_CAMERA_CCIR_INTERLACE)
862 csicr1 |= CSICR1_CCIR_MODE;
863 if (pcdev->platform_flags & MX2_CAMERA_GATED_CLOCK)
864 csicr1 |= CSICR1_GCLK_MODE;
865 if (pcdev->platform_flags & MX2_CAMERA_INV_DATA)
866 csicr1 |= CSICR1_INV_DATA;
867 if (pcdev->platform_flags & MX2_CAMERA_PACK_DIR_MSB)
868 csicr1 |= CSICR1_PACK_DIR;
869
870 pcdev->csicr1 = csicr1;
871
872 bytesperline = soc_mbus_bytes_per_line(icd->user_width,
873 icd->current_fmt->host_fmt);
874 if (bytesperline < 0)
875 return bytesperline;
876
Sascha Hauer2c9ba372012-02-20 07:55:24 -0300877 if (cpu_is_mx27()) {
Baruch Siach20669302010-07-04 07:55:10 +0300878 ret = mx27_camera_emma_prp_reset(pcdev);
879 if (ret)
880 return ret;
881
882 if (pcdev->discard_buffer)
883 dma_free_coherent(ici->v4l2_dev.dev,
884 pcdev->discard_size, pcdev->discard_buffer,
885 pcdev->discard_buffer_dma);
886
887 /*
888 * I didn't manage to properly enable/disable the prp
889 * on a per frame basis during running transfers,
890 * thus we allocate a buffer here and use it to
891 * discard frames when no buffer is available.
892 * Feel free to work on this ;)
893 */
894 pcdev->discard_size = icd->user_height * bytesperline;
895 pcdev->discard_buffer = dma_alloc_coherent(ici->v4l2_dev.dev,
896 pcdev->discard_size, &pcdev->discard_buffer_dma,
897 GFP_KERNEL);
898 if (!pcdev->discard_buffer)
899 return -ENOMEM;
900
901 mx27_camera_emma_buf_init(icd, bytesperline);
902 } else if (cpu_is_mx25()) {
903 writel((bytesperline * icd->user_height) >> 2,
904 pcdev->base_csi + CSIRXCNT);
905 writel((bytesperline << 16) | icd->user_height,
906 pcdev->base_csi + CSIIMAG_PARA);
907 }
908
909 writel(pcdev->csicr1, pcdev->base_csi + CSICR1);
910
911 return 0;
912}
913
914static int mx2_camera_set_crop(struct soc_camera_device *icd,
915 struct v4l2_crop *a)
916{
917 struct v4l2_rect *rect = &a->c;
918 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
919 struct v4l2_mbus_framefmt mf;
920 int ret;
921
922 soc_camera_limit_side(&rect->left, &rect->width, 0, 2, 4096);
923 soc_camera_limit_side(&rect->top, &rect->height, 0, 2, 4096);
924
925 ret = v4l2_subdev_call(sd, video, s_crop, a);
926 if (ret < 0)
927 return ret;
928
929 /* The capture device might have changed its output */
930 ret = v4l2_subdev_call(sd, video, g_mbus_fmt, &mf);
931 if (ret < 0)
932 return ret;
933
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300934 dev_dbg(icd->parent, "Sensor cropped %dx%d\n",
Baruch Siach20669302010-07-04 07:55:10 +0300935 mf.width, mf.height);
936
937 icd->user_width = mf.width;
938 icd->user_height = mf.height;
939
940 return ret;
941}
942
Javier Martinf4109912011-12-14 13:30:14 -0300943static int mx2_camera_get_formats(struct soc_camera_device *icd,
944 unsigned int idx,
945 struct soc_camera_format_xlate *xlate)
946{
947 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
948 const struct soc_mbus_pixelfmt *fmt;
949 struct device *dev = icd->parent;
950 enum v4l2_mbus_pixelcode code;
951 int ret, formats = 0;
952
953 ret = v4l2_subdev_call(sd, video, enum_mbus_fmt, idx, &code);
954 if (ret < 0)
955 /* no more formats */
956 return 0;
957
958 fmt = soc_mbus_get_fmtdesc(code);
959 if (!fmt) {
960 dev_err(dev, "Invalid format code #%u: %d\n", idx, code);
961 return 0;
962 }
963
964 if (code == V4L2_MBUS_FMT_YUYV8_2X8) {
965 formats++;
966 if (xlate) {
967 /*
968 * CH2 can output YUV420 which is a standard format in
969 * soc_mediabus.c
970 */
971 xlate->host_fmt =
972 soc_mbus_get_fmtdesc(V4L2_MBUS_FMT_YUYV8_1_5X8);
973 xlate->code = code;
974 dev_dbg(dev, "Providing host format %s for sensor code %d\n",
975 xlate->host_fmt->name, code);
976 xlate++;
977 }
978 }
979
980 /* Generic pass-trough */
981 formats++;
982 if (xlate) {
983 xlate->host_fmt = fmt;
984 xlate->code = code;
985 xlate++;
986 }
987 return formats;
988}
989
Baruch Siach20669302010-07-04 07:55:10 +0300990static int mx2_camera_set_fmt(struct soc_camera_device *icd,
991 struct v4l2_format *f)
992{
Javier Martinf4109912011-12-14 13:30:14 -0300993 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
994 struct mx2_camera_dev *pcdev = ici->priv;
Baruch Siach20669302010-07-04 07:55:10 +0300995 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
996 const struct soc_camera_format_xlate *xlate;
997 struct v4l2_pix_format *pix = &f->fmt.pix;
998 struct v4l2_mbus_framefmt mf;
999 int ret;
1000
1001 xlate = soc_camera_xlate_by_fourcc(icd, pix->pixelformat);
1002 if (!xlate) {
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -03001003 dev_warn(icd->parent, "Format %x not found\n",
Baruch Siach20669302010-07-04 07:55:10 +03001004 pix->pixelformat);
1005 return -EINVAL;
1006 }
1007
Baruch Siach20669302010-07-04 07:55:10 +03001008 mf.width = pix->width;
1009 mf.height = pix->height;
1010 mf.field = pix->field;
1011 mf.colorspace = pix->colorspace;
1012 mf.code = xlate->code;
1013
1014 ret = v4l2_subdev_call(sd, video, s_mbus_fmt, &mf);
1015 if (ret < 0 && ret != -ENOIOCTLCMD)
1016 return ret;
1017
1018 if (mf.code != xlate->code)
1019 return -EINVAL;
1020
1021 pix->width = mf.width;
1022 pix->height = mf.height;
1023 pix->field = mf.field;
1024 pix->colorspace = mf.colorspace;
1025 icd->current_fmt = xlate;
1026
Sascha Hauer2c9ba372012-02-20 07:55:24 -03001027 if (cpu_is_mx27())
Javier Martinf4109912011-12-14 13:30:14 -03001028 pcdev->emma_prp = mx27_emma_prp_get_format(xlate->code,
1029 xlate->host_fmt->fourcc);
1030
Baruch Siach20669302010-07-04 07:55:10 +03001031 return 0;
1032}
1033
1034static int mx2_camera_try_fmt(struct soc_camera_device *icd,
1035 struct v4l2_format *f)
1036{
Baruch Siach20669302010-07-04 07:55:10 +03001037 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1038 const struct soc_camera_format_xlate *xlate;
1039 struct v4l2_pix_format *pix = &f->fmt.pix;
1040 struct v4l2_mbus_framefmt mf;
1041 __u32 pixfmt = pix->pixelformat;
1042 unsigned int width_limit;
1043 int ret;
1044
1045 xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
1046 if (pixfmt && !xlate) {
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -03001047 dev_warn(icd->parent, "Format %x not found\n", pixfmt);
Baruch Siach20669302010-07-04 07:55:10 +03001048 return -EINVAL;
1049 }
1050
1051 /* FIXME: implement MX27 limits */
1052
Baruch Siach20669302010-07-04 07:55:10 +03001053 /* limit to MX25 hardware capabilities */
1054 if (cpu_is_mx25()) {
1055 if (xlate->host_fmt->bits_per_sample <= 8)
1056 width_limit = 0xffff * 4;
1057 else
1058 width_limit = 0xffff * 2;
1059 /* CSIIMAG_PARA limit */
1060 if (pix->width > width_limit)
1061 pix->width = width_limit;
1062 if (pix->height > 0xffff)
1063 pix->height = 0xffff;
1064
1065 pix->bytesperline = soc_mbus_bytes_per_line(pix->width,
1066 xlate->host_fmt);
1067 if (pix->bytesperline < 0)
1068 return pix->bytesperline;
1069 pix->sizeimage = pix->height * pix->bytesperline;
Guennadi Liakhovetski28281a72011-06-04 15:06:47 -03001070 /* Check against the CSIRXCNT limit */
1071 if (pix->sizeimage > 4 * 0x3ffff) {
1072 /* Adjust geometry, preserve aspect ratio */
1073 unsigned int new_height = int_sqrt(4 * 0x3ffff *
1074 pix->height / pix->bytesperline);
1075 pix->width = new_height * pix->width / pix->height;
1076 pix->height = new_height;
1077 pix->bytesperline = soc_mbus_bytes_per_line(pix->width,
1078 xlate->host_fmt);
1079 BUG_ON(pix->bytesperline < 0);
Baruch Siach20669302010-07-04 07:55:10 +03001080 }
1081 }
1082
1083 /* limit to sensor capabilities */
1084 mf.width = pix->width;
1085 mf.height = pix->height;
1086 mf.field = pix->field;
1087 mf.colorspace = pix->colorspace;
1088 mf.code = xlate->code;
1089
1090 ret = v4l2_subdev_call(sd, video, try_mbus_fmt, &mf);
1091 if (ret < 0)
1092 return ret;
1093
1094 if (mf.field == V4L2_FIELD_ANY)
1095 mf.field = V4L2_FIELD_NONE;
Javier Martinf4109912011-12-14 13:30:14 -03001096 /*
1097 * Driver supports interlaced images provided they have
1098 * both fields so that they can be processed as if they
1099 * were progressive.
1100 */
1101 if (mf.field != V4L2_FIELD_NONE && !V4L2_FIELD_HAS_BOTH(mf.field)) {
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -03001102 dev_err(icd->parent, "Field type %d unsupported.\n",
Baruch Siach20669302010-07-04 07:55:10 +03001103 mf.field);
1104 return -EINVAL;
1105 }
1106
1107 pix->width = mf.width;
1108 pix->height = mf.height;
1109 pix->field = mf.field;
1110 pix->colorspace = mf.colorspace;
1111
1112 return 0;
1113}
1114
1115static int mx2_camera_querycap(struct soc_camera_host *ici,
1116 struct v4l2_capability *cap)
1117{
1118 /* cap->name is set by the friendly caller:-> */
1119 strlcpy(cap->card, MX2_CAM_DRIVER_DESCRIPTION, sizeof(cap->card));
Baruch Siach20669302010-07-04 07:55:10 +03001120 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1121
1122 return 0;
1123}
1124
Sascha Hauer6b101922010-11-08 17:52:45 -03001125static int mx2_camera_reqbufs(struct soc_camera_device *icd,
Baruch Siach20669302010-07-04 07:55:10 +03001126 struct v4l2_requestbuffers *p)
1127{
1128 int i;
1129
1130 for (i = 0; i < p->count; i++) {
Sascha Hauer6b101922010-11-08 17:52:45 -03001131 struct mx2_buffer *buf = container_of(icd->vb_vidq.bufs[i],
Baruch Siach20669302010-07-04 07:55:10 +03001132 struct mx2_buffer, vb);
1133 INIT_LIST_HEAD(&buf->vb.queue);
1134 }
1135
1136 return 0;
1137}
1138
Baruch Siach20669302010-07-04 07:55:10 +03001139static unsigned int mx2_camera_poll(struct file *file, poll_table *pt)
1140{
Sascha Hauer6b101922010-11-08 17:52:45 -03001141 struct soc_camera_device *icd = file->private_data;
Baruch Siach20669302010-07-04 07:55:10 +03001142
Sascha Hauer6b101922010-11-08 17:52:45 -03001143 return videobuf_poll_stream(file, &icd->vb_vidq, pt);
Baruch Siach20669302010-07-04 07:55:10 +03001144}
1145
1146static struct soc_camera_host_ops mx2_soc_camera_host_ops = {
1147 .owner = THIS_MODULE,
1148 .add = mx2_camera_add_device,
1149 .remove = mx2_camera_remove_device,
1150 .set_fmt = mx2_camera_set_fmt,
1151 .set_crop = mx2_camera_set_crop,
Javier Martinf4109912011-12-14 13:30:14 -03001152 .get_formats = mx2_camera_get_formats,
Baruch Siach20669302010-07-04 07:55:10 +03001153 .try_fmt = mx2_camera_try_fmt,
1154 .init_videobuf = mx2_camera_init_videobuf,
1155 .reqbufs = mx2_camera_reqbufs,
1156 .poll = mx2_camera_poll,
1157 .querycap = mx2_camera_querycap,
1158 .set_bus_param = mx2_camera_set_bus_param,
1159};
1160
1161static void mx27_camera_frame_done_emma(struct mx2_camera_dev *pcdev,
1162 int bufnum, int state)
1163{
Javier Martinf4109912011-12-14 13:30:14 -03001164 u32 imgsize = pcdev->icd->user_height * pcdev->icd->user_width;
1165 struct mx2_fmt_cfg *prp = pcdev->emma_prp;
Baruch Siach20669302010-07-04 07:55:10 +03001166 struct mx2_buffer *buf;
1167 struct videobuf_buffer *vb;
1168 unsigned long phys;
1169
1170 if (!list_empty(&pcdev->active_bufs)) {
1171 buf = list_entry(pcdev->active_bufs.next,
1172 struct mx2_buffer, vb.queue);
1173
1174 BUG_ON(buf->bufnum != bufnum);
1175
1176 vb = &buf->vb;
1177#ifdef DEBUG
1178 phys = videobuf_to_dma_contig(vb);
Javier Martinf4109912011-12-14 13:30:14 -03001179 if (prp->cfg.channel == 1) {
1180 if (readl(pcdev->base_emma + PRP_DEST_RGB1_PTR +
1181 4 * bufnum) != phys) {
1182 dev_err(pcdev->dev, "%p != %p\n", phys,
1183 readl(pcdev->base_emma +
1184 PRP_DEST_RGB1_PTR +
1185 4 * bufnum));
1186 }
1187 } else {
1188 if (readl(pcdev->base_emma + PRP_DEST_Y_PTR -
1189 0x14 * bufnum) != phys) {
1190 dev_err(pcdev->dev, "%p != %p\n", phys,
1191 readl(pcdev->base_emma +
1192 PRP_DEST_Y_PTR -
1193 0x14 * bufnum));
1194 }
Baruch Siach20669302010-07-04 07:55:10 +03001195 }
1196#endif
1197 dev_dbg(pcdev->dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__, vb,
1198 vb->baddr, vb->bsize);
1199
1200 list_del(&vb->queue);
1201 vb->state = state;
1202 do_gettimeofday(&vb->ts);
Javier Martinccd1a492011-12-22 12:12:00 -03001203 vb->field_count = pcdev->frame_count * 2;
1204 pcdev->frame_count++;
Baruch Siach20669302010-07-04 07:55:10 +03001205
1206 wake_up(&vb->done);
1207 }
1208
1209 if (list_empty(&pcdev->capture)) {
Javier Martinf4109912011-12-14 13:30:14 -03001210 if (prp->cfg.channel == 1) {
1211 writel(pcdev->discard_buffer_dma, pcdev->base_emma +
1212 PRP_DEST_RGB1_PTR + 4 * bufnum);
1213 } else {
1214 writel(pcdev->discard_buffer_dma, pcdev->base_emma +
1215 PRP_DEST_Y_PTR -
1216 0x14 * bufnum);
1217 if (prp->out_fmt == V4L2_PIX_FMT_YUV420) {
1218 writel(pcdev->discard_buffer_dma + imgsize,
1219 pcdev->base_emma + PRP_DEST_CB_PTR -
1220 0x14 * bufnum);
1221 writel(pcdev->discard_buffer_dma +
1222 ((5 * imgsize) / 4), pcdev->base_emma +
1223 PRP_DEST_CR_PTR - 0x14 * bufnum);
1224 }
1225 }
Baruch Siach20669302010-07-04 07:55:10 +03001226 return;
1227 }
1228
1229 buf = list_entry(pcdev->capture.next,
1230 struct mx2_buffer, vb.queue);
1231
Michael Grzeschikcd9ebdb2010-08-03 06:37:54 -03001232 buf->bufnum = !bufnum;
Baruch Siach20669302010-07-04 07:55:10 +03001233
1234 list_move_tail(pcdev->capture.next, &pcdev->active_bufs);
1235
1236 vb = &buf->vb;
1237 vb->state = VIDEOBUF_ACTIVE;
1238
1239 phys = videobuf_to_dma_contig(vb);
Javier Martinf4109912011-12-14 13:30:14 -03001240 if (prp->cfg.channel == 1) {
1241 writel(phys, pcdev->base_emma + PRP_DEST_RGB1_PTR + 4 * bufnum);
1242 } else {
1243 writel(phys, pcdev->base_emma +
1244 PRP_DEST_Y_PTR - 0x14 * bufnum);
1245 if (prp->cfg.out_fmt == PRP_CNTL_CH2_OUT_YUV420) {
1246 writel(phys + imgsize, pcdev->base_emma +
1247 PRP_DEST_CB_PTR - 0x14 * bufnum);
1248 writel(phys + ((5 * imgsize) / 4), pcdev->base_emma +
1249 PRP_DEST_CR_PTR - 0x14 * bufnum);
1250 }
1251 }
Baruch Siach20669302010-07-04 07:55:10 +03001252}
1253
1254static irqreturn_t mx27_camera_emma_irq(int irq_emma, void *data)
1255{
1256 struct mx2_camera_dev *pcdev = data;
1257 unsigned int status = readl(pcdev->base_emma + PRP_INTRSTATUS);
1258 struct mx2_buffer *buf;
1259
1260 if (status & (1 << 7)) { /* overflow */
1261 u32 cntl;
1262 /*
1263 * We only disable channel 1 here since this is the only
1264 * enabled channel
1265 *
1266 * FIXME: the correct DMA overflow handling should be resetting
1267 * the buffer, returning an error frame, and continuing with
1268 * the next one.
1269 */
1270 cntl = readl(pcdev->base_emma + PRP_CNTL);
Javier Martinf4109912011-12-14 13:30:14 -03001271 writel(cntl & ~(PRP_CNTL_CH1EN | PRP_CNTL_CH2EN),
1272 pcdev->base_emma + PRP_CNTL);
Baruch Siach20669302010-07-04 07:55:10 +03001273 writel(cntl, pcdev->base_emma + PRP_CNTL);
1274 }
Javier Martinf4109912011-12-14 13:30:14 -03001275 if ((((status & (3 << 5)) == (3 << 5)) ||
1276 ((status & (3 << 3)) == (3 << 3)))
Baruch Siach20669302010-07-04 07:55:10 +03001277 && !list_empty(&pcdev->active_bufs)) {
1278 /*
1279 * Both buffers have triggered, process the one we're expecting
1280 * to first
1281 */
1282 buf = list_entry(pcdev->active_bufs.next,
1283 struct mx2_buffer, vb.queue);
1284 mx27_camera_frame_done_emma(pcdev, buf->bufnum, VIDEOBUF_DONE);
1285 status &= ~(1 << (6 - buf->bufnum)); /* mark processed */
1286 }
Javier Martinf4109912011-12-14 13:30:14 -03001287 if ((status & (1 << 6)) || (status & (1 << 4)))
Baruch Siach20669302010-07-04 07:55:10 +03001288 mx27_camera_frame_done_emma(pcdev, 0, VIDEOBUF_DONE);
Javier Martinf4109912011-12-14 13:30:14 -03001289 if ((status & (1 << 5)) || (status & (1 << 3)))
Baruch Siach20669302010-07-04 07:55:10 +03001290 mx27_camera_frame_done_emma(pcdev, 1, VIDEOBUF_DONE);
1291
1292 writel(status, pcdev->base_emma + PRP_INTRSTATUS);
1293
1294 return IRQ_HANDLED;
1295}
1296
1297static int __devinit mx27_camera_emma_init(struct mx2_camera_dev *pcdev)
1298{
1299 struct resource *res_emma = pcdev->res_emma;
1300 int err = 0;
1301
1302 if (!request_mem_region(res_emma->start, resource_size(res_emma),
1303 MX2_CAM_DRV_NAME)) {
1304 err = -EBUSY;
1305 goto out;
1306 }
1307
1308 pcdev->base_emma = ioremap(res_emma->start, resource_size(res_emma));
1309 if (!pcdev->base_emma) {
1310 err = -ENOMEM;
1311 goto exit_release;
1312 }
1313
1314 err = request_irq(pcdev->irq_emma, mx27_camera_emma_irq, 0,
1315 MX2_CAM_DRV_NAME, pcdev);
1316 if (err) {
1317 dev_err(pcdev->dev, "Camera EMMA interrupt register failed \n");
1318 goto exit_iounmap;
1319 }
1320
1321 pcdev->clk_emma = clk_get(NULL, "emma");
1322 if (IS_ERR(pcdev->clk_emma)) {
1323 err = PTR_ERR(pcdev->clk_emma);
1324 goto exit_free_irq;
1325 }
1326
1327 clk_enable(pcdev->clk_emma);
1328
1329 err = mx27_camera_emma_prp_reset(pcdev);
1330 if (err)
1331 goto exit_clk_emma_put;
1332
1333 return err;
1334
1335exit_clk_emma_put:
1336 clk_disable(pcdev->clk_emma);
1337 clk_put(pcdev->clk_emma);
1338exit_free_irq:
1339 free_irq(pcdev->irq_emma, pcdev);
1340exit_iounmap:
1341 iounmap(pcdev->base_emma);
1342exit_release:
1343 release_mem_region(res_emma->start, resource_size(res_emma));
1344out:
1345 return err;
1346}
1347
1348static int __devinit mx2_camera_probe(struct platform_device *pdev)
1349{
1350 struct mx2_camera_dev *pcdev;
1351 struct resource *res_csi, *res_emma;
1352 void __iomem *base_csi;
1353 int irq_csi, irq_emma;
Baruch Siach20669302010-07-04 07:55:10 +03001354 int err = 0;
1355
1356 dev_dbg(&pdev->dev, "initialising\n");
1357
1358 res_csi = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1359 irq_csi = platform_get_irq(pdev, 0);
1360 if (res_csi == NULL || irq_csi < 0) {
1361 dev_err(&pdev->dev, "Missing platform resources data\n");
1362 err = -ENODEV;
1363 goto exit;
1364 }
1365
1366 pcdev = kzalloc(sizeof(*pcdev), GFP_KERNEL);
1367 if (!pcdev) {
1368 dev_err(&pdev->dev, "Could not allocate pcdev\n");
1369 err = -ENOMEM;
1370 goto exit;
1371 }
1372
1373 pcdev->clk_csi = clk_get(&pdev->dev, NULL);
1374 if (IS_ERR(pcdev->clk_csi)) {
1375 err = PTR_ERR(pcdev->clk_csi);
1376 goto exit_kfree;
1377 }
1378
1379 dev_dbg(&pdev->dev, "Camera clock frequency: %ld\n",
1380 clk_get_rate(pcdev->clk_csi));
1381
Baruch Siach20669302010-07-04 07:55:10 +03001382 pcdev->res_csi = res_csi;
1383 pcdev->pdata = pdev->dev.platform_data;
1384 if (pcdev->pdata) {
1385 long rate;
1386
1387 pcdev->platform_flags = pcdev->pdata->flags;
1388
1389 rate = clk_round_rate(pcdev->clk_csi, pcdev->pdata->clk * 2);
1390 if (rate <= 0) {
1391 err = -ENODEV;
1392 goto exit_dma_free;
1393 }
1394 err = clk_set_rate(pcdev->clk_csi, rate);
1395 if (err < 0)
1396 goto exit_dma_free;
1397 }
1398
1399 INIT_LIST_HEAD(&pcdev->capture);
1400 INIT_LIST_HEAD(&pcdev->active_bufs);
1401 spin_lock_init(&pcdev->lock);
1402
1403 /*
1404 * Request the regions.
1405 */
1406 if (!request_mem_region(res_csi->start, resource_size(res_csi),
1407 MX2_CAM_DRV_NAME)) {
1408 err = -EBUSY;
1409 goto exit_dma_free;
1410 }
1411
1412 base_csi = ioremap(res_csi->start, resource_size(res_csi));
1413 if (!base_csi) {
1414 err = -ENOMEM;
1415 goto exit_release;
1416 }
1417 pcdev->irq_csi = irq_csi;
1418 pcdev->base_csi = base_csi;
1419 pcdev->base_dma = res_csi->start;
1420 pcdev->dev = &pdev->dev;
1421
Sascha Hauer2c9ba372012-02-20 07:55:24 -03001422 if (cpu_is_mx25()) {
1423 err = request_irq(pcdev->irq_csi, mx25_camera_irq, 0,
1424 MX2_CAM_DRV_NAME, pcdev);
1425 if (err) {
1426 dev_err(pcdev->dev, "Camera interrupt register failed \n");
1427 goto exit_iounmap;
1428 }
Baruch Siach20669302010-07-04 07:55:10 +03001429 }
1430
1431 if (cpu_is_mx27()) {
1432 /* EMMA support */
1433 res_emma = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1434 irq_emma = platform_get_irq(pdev, 1);
1435
Sascha Hauer2c9ba372012-02-20 07:55:24 -03001436 if (!res_emma || !irq_emma) {
1437 dev_err(&pdev->dev, "no EMMA resources\n");
1438 goto exit_free_irq;
Baruch Siach20669302010-07-04 07:55:10 +03001439 }
Sascha Hauer2c9ba372012-02-20 07:55:24 -03001440
1441 pcdev->res_emma = res_emma;
1442 pcdev->irq_emma = irq_emma;
1443 if (mx27_camera_emma_init(pcdev))
1444 goto exit_free_irq;
Baruch Siach20669302010-07-04 07:55:10 +03001445 }
1446
1447 pcdev->soc_host.drv_name = MX2_CAM_DRV_NAME,
1448 pcdev->soc_host.ops = &mx2_soc_camera_host_ops,
1449 pcdev->soc_host.priv = pcdev;
1450 pcdev->soc_host.v4l2_dev.dev = &pdev->dev;
1451 pcdev->soc_host.nr = pdev->id;
1452 err = soc_camera_host_register(&pcdev->soc_host);
1453 if (err)
1454 goto exit_free_emma;
1455
Michael Grzeschik45f4d4e2010-08-27 09:39:05 -03001456 dev_info(&pdev->dev, "MX2 Camera (CSI) driver probed, clock frequency: %ld\n",
1457 clk_get_rate(pcdev->clk_csi));
1458
Baruch Siach20669302010-07-04 07:55:10 +03001459 return 0;
1460
1461exit_free_emma:
Sascha Hauer2c9ba372012-02-20 07:55:24 -03001462 if (cpu_is_mx27()) {
Baruch Siach20669302010-07-04 07:55:10 +03001463 free_irq(pcdev->irq_emma, pcdev);
1464 clk_disable(pcdev->clk_emma);
1465 clk_put(pcdev->clk_emma);
1466 iounmap(pcdev->base_emma);
Sascha Hauer2c9ba372012-02-20 07:55:24 -03001467 release_mem_region(pcdev->res_emma->start, resource_size(pcdev->res_emma));
Baruch Siach20669302010-07-04 07:55:10 +03001468 }
1469exit_free_irq:
Sascha Hauer2c9ba372012-02-20 07:55:24 -03001470 if (cpu_is_mx25())
1471 free_irq(pcdev->irq_csi, pcdev);
Baruch Siach20669302010-07-04 07:55:10 +03001472exit_iounmap:
1473 iounmap(base_csi);
1474exit_release:
1475 release_mem_region(res_csi->start, resource_size(res_csi));
1476exit_dma_free:
Baruch Siach20669302010-07-04 07:55:10 +03001477 clk_put(pcdev->clk_csi);
Baruch Siach20669302010-07-04 07:55:10 +03001478exit_kfree:
1479 kfree(pcdev);
1480exit:
1481 return err;
1482}
1483
1484static int __devexit mx2_camera_remove(struct platform_device *pdev)
1485{
1486 struct soc_camera_host *soc_host = to_soc_camera_host(&pdev->dev);
1487 struct mx2_camera_dev *pcdev = container_of(soc_host,
1488 struct mx2_camera_dev, soc_host);
1489 struct resource *res;
1490
1491 clk_put(pcdev->clk_csi);
Sascha Hauer2c9ba372012-02-20 07:55:24 -03001492 if (cpu_is_mx25())
1493 free_irq(pcdev->irq_csi, pcdev);
Baruch Siach20669302010-07-04 07:55:10 +03001494 if (cpu_is_mx27())
Baruch Siach20669302010-07-04 07:55:10 +03001495 free_irq(pcdev->irq_emma, pcdev);
1496
1497 soc_camera_host_unregister(&pcdev->soc_host);
1498
1499 iounmap(pcdev->base_csi);
1500
Sascha Hauer2c9ba372012-02-20 07:55:24 -03001501 if (cpu_is_mx27()) {
Baruch Siach20669302010-07-04 07:55:10 +03001502 clk_disable(pcdev->clk_emma);
1503 clk_put(pcdev->clk_emma);
1504 iounmap(pcdev->base_emma);
1505 res = pcdev->res_emma;
1506 release_mem_region(res->start, resource_size(res));
1507 }
1508
1509 res = pcdev->res_csi;
1510 release_mem_region(res->start, resource_size(res));
1511
1512 kfree(pcdev);
1513
1514 dev_info(&pdev->dev, "MX2 Camera driver unloaded\n");
1515
1516 return 0;
1517}
1518
1519static struct platform_driver mx2_camera_driver = {
1520 .driver = {
1521 .name = MX2_CAM_DRV_NAME,
1522 },
1523 .remove = __devexit_p(mx2_camera_remove),
1524};
1525
1526
1527static int __init mx2_camera_init(void)
1528{
1529 return platform_driver_probe(&mx2_camera_driver, &mx2_camera_probe);
1530}
1531
1532static void __exit mx2_camera_exit(void)
1533{
1534 return platform_driver_unregister(&mx2_camera_driver);
1535}
1536
1537module_init(mx2_camera_init);
1538module_exit(mx2_camera_exit);
1539
1540MODULE_DESCRIPTION("i.MX27/i.MX25 SoC Camera Host driver");
1541MODULE_AUTHOR("Sascha Hauer <sha@pengutronix.de>");
1542MODULE_LICENSE("GPL");
Mauro Carvalho Chehab64dc3c12011-06-25 11:28:37 -03001543MODULE_VERSION(MX2_CAM_VERSION);