blob: 3875483ab9d55573a7cfcb71d2e4c12bdcd48306 [file] [log] [blame]
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -03001/*
2 * V4L2 Driver for i.MXL/i.MXL camera (CSI) host
3 *
4 * Copyright (C) 2008, Paulius Zaleckas <paulius.zaleckas@teltonika.lt>
5 * Copyright (C) 2009, Darius Augulis <augulis.darius@gmail.com>
6 *
7 * Based on PXA SoC camera driver
8 * Copyright (C) 2006, Sascha Hauer, Pengutronix
9 * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
14 */
15
16#include <linux/clk.h>
17#include <linux/delay.h>
18#include <linux/device.h>
19#include <linux/dma-mapping.h>
20#include <linux/errno.h>
21#include <linux/fs.h>
22#include <linux/init.h>
23#include <linux/interrupt.h>
24#include <linux/io.h>
25#include <linux/kernel.h>
26#include <linux/mm.h>
27#include <linux/module.h>
28#include <linux/moduleparam.h>
29#include <linux/mutex.h>
30#include <linux/platform_device.h>
31#include <linux/time.h>
32#include <linux/version.h>
33#include <linux/videodev2.h>
34
35#include <media/soc_camera.h>
36#include <media/v4l2-common.h>
37#include <media/v4l2-dev.h>
38#include <media/videobuf-dma-contig.h>
39
40#include <asm/dma.h>
41#include <asm/fiq.h>
42#include <mach/dma-mx1-mx2.h>
43#include <mach/hardware.h>
44#include <mach/mx1_camera.h>
45
46/*
47 * CSI registers
48 */
49#define DMA_CCR(x) (0x8c + ((x) << 6)) /* Control Registers */
50#define DMA_DIMR 0x08 /* Interrupt mask Register */
51#define CSICR1 0x00 /* CSI Control Register 1 */
52#define CSISR 0x08 /* CSI Status Register */
53#define CSIRXR 0x10 /* CSI RxFIFO Register */
54
55#define CSICR1_RXFF_LEVEL(x) (((x) & 0x3) << 19)
56#define CSICR1_SOF_POL (1 << 17)
57#define CSICR1_SOF_INTEN (1 << 16)
58#define CSICR1_MCLKDIV(x) (((x) & 0xf) << 12)
59#define CSICR1_MCLKEN (1 << 9)
60#define CSICR1_FCC (1 << 8)
61#define CSICR1_BIG_ENDIAN (1 << 7)
62#define CSICR1_CLR_RXFIFO (1 << 5)
63#define CSICR1_GCLK_MODE (1 << 4)
64#define CSICR1_DATA_POL (1 << 2)
65#define CSICR1_REDGE (1 << 1)
66#define CSICR1_EN (1 << 0)
67
68#define CSISR_SFF_OR_INT (1 << 25)
69#define CSISR_RFF_OR_INT (1 << 24)
70#define CSISR_STATFF_INT (1 << 21)
71#define CSISR_RXFF_INT (1 << 18)
72#define CSISR_SOF_INT (1 << 16)
73#define CSISR_DRDY (1 << 0)
74
75#define VERSION_CODE KERNEL_VERSION(0, 0, 1)
76#define DRIVER_NAME "mx1-camera"
77
78#define CSI_IRQ_MASK (CSISR_SFF_OR_INT | CSISR_RFF_OR_INT | \
79 CSISR_STATFF_INT | CSISR_RXFF_INT | CSISR_SOF_INT)
80
81#define CSI_BUS_FLAGS (SOCAM_MASTER | SOCAM_HSYNC_ACTIVE_HIGH | \
82 SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW | \
83 SOCAM_PCLK_SAMPLE_RISING | SOCAM_PCLK_SAMPLE_FALLING | \
84 SOCAM_DATA_ACTIVE_HIGH | SOCAM_DATA_ACTIVE_LOW | \
85 SOCAM_DATAWIDTH_8)
86
87#define MAX_VIDEO_MEM 16 /* Video memory limit in megabytes */
88
89/*
90 * Structures
91 */
92
93/* buffer for one video frame */
94struct mx1_buffer {
95 /* common v4l buffer stuff -- must be first */
96 struct videobuf_buffer vb;
97 const struct soc_camera_data_format *fmt;
98 int inwork;
99};
100
101/* i.MX1/i.MXL is only supposed to handle one camera on its Camera Sensor
102 * Interface. If anyone ever builds hardware to enable more than
103 * one camera, they will have to modify this driver too */
104struct mx1_camera_dev {
Guennadi Liakhovetskieb6c8552009-04-24 12:55:18 -0300105 struct soc_camera_host soc_host;
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300106 struct soc_camera_device *icd;
107 struct mx1_camera_pdata *pdata;
108 struct mx1_buffer *active;
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300109 struct resource *res;
110 struct clk *clk;
111 struct list_head capture;
112
113 void __iomem *base;
114 int dma_chan;
115 unsigned int irq;
116 unsigned long mclk;
117
118 spinlock_t lock;
119};
120
121/*
122 * Videobuf operations
123 */
124static int mx1_videobuf_setup(struct videobuf_queue *vq, unsigned int *count,
125 unsigned int *size)
126{
127 struct soc_camera_device *icd = vq->priv_data;
128
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300129 *size = icd->user_width * icd->user_height *
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300130 ((icd->current_fmt->depth + 7) >> 3);
131
132 if (!*count)
133 *count = 32;
134
135 while (*size * *count > MAX_VIDEO_MEM * 1024 * 1024)
136 (*count)--;
137
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300138 dev_dbg(icd->dev.parent, "count=%d, size=%d\n", *count, *size);
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300139
140 return 0;
141}
142
143static void free_buffer(struct videobuf_queue *vq, struct mx1_buffer *buf)
144{
145 struct soc_camera_device *icd = vq->priv_data;
146 struct videobuf_buffer *vb = &buf->vb;
147
148 BUG_ON(in_interrupt());
149
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300150 dev_dbg(icd->dev.parent, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300151 vb, vb->baddr, vb->bsize);
152
153 /* This waits until this buffer is out of danger, i.e., until it is no
154 * longer in STATE_QUEUED or STATE_ACTIVE */
155 videobuf_waiton(vb, 0, 0);
156 videobuf_dma_contig_free(vq, vb);
157
158 vb->state = VIDEOBUF_NEEDS_INIT;
159}
160
161static int mx1_videobuf_prepare(struct videobuf_queue *vq,
162 struct videobuf_buffer *vb, enum v4l2_field field)
163{
164 struct soc_camera_device *icd = vq->priv_data;
165 struct mx1_buffer *buf = container_of(vb, struct mx1_buffer, vb);
166 int ret;
167
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300168 dev_dbg(icd->dev.parent, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300169 vb, vb->baddr, vb->bsize);
170
171 /* Added list head initialization on alloc */
172 WARN_ON(!list_empty(&vb->queue));
173
174 BUG_ON(NULL == icd->current_fmt);
175
176 /* I think, in buf_prepare you only have to protect global data,
177 * the actual buffer is yours */
178 buf->inwork = 1;
179
180 if (buf->fmt != icd->current_fmt ||
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300181 vb->width != icd->user_width ||
182 vb->height != icd->user_height ||
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300183 vb->field != field) {
184 buf->fmt = icd->current_fmt;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300185 vb->width = icd->user_width;
186 vb->height = icd->user_height;
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300187 vb->field = field;
188 vb->state = VIDEOBUF_NEEDS_INIT;
189 }
190
191 vb->size = vb->width * vb->height * ((buf->fmt->depth + 7) >> 3);
192 if (0 != vb->baddr && vb->bsize < vb->size) {
193 ret = -EINVAL;
194 goto out;
195 }
196
197 if (vb->state == VIDEOBUF_NEEDS_INIT) {
198 ret = videobuf_iolock(vq, vb, NULL);
199 if (ret)
200 goto fail;
201
202 vb->state = VIDEOBUF_PREPARED;
203 }
204
205 buf->inwork = 0;
206
207 return 0;
208
209fail:
210 free_buffer(vq, buf);
211out:
212 buf->inwork = 0;
213 return ret;
214}
215
216static int mx1_camera_setup_dma(struct mx1_camera_dev *pcdev)
217{
218 struct videobuf_buffer *vbuf = &pcdev->active->vb;
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300219 struct device *dev = pcdev->icd->dev.parent;
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300220 int ret;
221
222 if (unlikely(!pcdev->active)) {
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300223 dev_err(dev, "DMA End IRQ with no active buffer\n");
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300224 return -EFAULT;
225 }
226
227 /* setup sg list for future DMA */
228 ret = imx_dma_setup_single(pcdev->dma_chan,
229 videobuf_to_dma_contig(vbuf),
230 vbuf->size, pcdev->res->start +
231 CSIRXR, DMA_MODE_READ);
232 if (unlikely(ret))
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300233 dev_err(dev, "Failed to setup DMA sg list\n");
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300234
235 return ret;
236}
237
Guennadi Liakhovetski2dd54a52009-08-05 20:06:31 -0300238/* Called under spinlock_irqsave(&pcdev->lock, ...) */
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300239static void mx1_videobuf_queue(struct videobuf_queue *vq,
240 struct videobuf_buffer *vb)
241{
242 struct soc_camera_device *icd = vq->priv_data;
243 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
244 struct mx1_camera_dev *pcdev = ici->priv;
245 struct mx1_buffer *buf = container_of(vb, struct mx1_buffer, vb);
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300246
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300247 dev_dbg(icd->dev.parent, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300248 vb, vb->baddr, vb->bsize);
249
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300250 list_add_tail(&vb->queue, &pcdev->capture);
251
252 vb->state = VIDEOBUF_ACTIVE;
253
254 if (!pcdev->active) {
255 pcdev->active = buf;
256
257 /* setup sg list for future DMA */
258 if (!mx1_camera_setup_dma(pcdev)) {
259 unsigned int temp;
260 /* enable SOF irq */
261 temp = __raw_readl(pcdev->base + CSICR1) |
262 CSICR1_SOF_INTEN;
263 __raw_writel(temp, pcdev->base + CSICR1);
264 }
265 }
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300266}
267
268static void mx1_videobuf_release(struct videobuf_queue *vq,
269 struct videobuf_buffer *vb)
270{
271 struct mx1_buffer *buf = container_of(vb, struct mx1_buffer, vb);
272#ifdef DEBUG
273 struct soc_camera_device *icd = vq->priv_data;
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300274 struct device *dev = icd->dev.parent;
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300275
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300276 dev_dbg(dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300277 vb, vb->baddr, vb->bsize);
278
279 switch (vb->state) {
280 case VIDEOBUF_ACTIVE:
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300281 dev_dbg(dev, "%s (active)\n", __func__);
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300282 break;
283 case VIDEOBUF_QUEUED:
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300284 dev_dbg(dev, "%s (queued)\n", __func__);
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300285 break;
286 case VIDEOBUF_PREPARED:
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300287 dev_dbg(dev, "%s (prepared)\n", __func__);
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300288 break;
289 default:
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300290 dev_dbg(dev, "%s (unknown)\n", __func__);
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300291 break;
292 }
293#endif
294
295 free_buffer(vq, buf);
296}
297
298static void mx1_camera_wakeup(struct mx1_camera_dev *pcdev,
299 struct videobuf_buffer *vb,
300 struct mx1_buffer *buf)
301{
302 /* _init is used to debug races, see comment in mx1_camera_reqbufs() */
303 list_del_init(&vb->queue);
304 vb->state = VIDEOBUF_DONE;
305 do_gettimeofday(&vb->ts);
306 vb->field_count++;
307 wake_up(&vb->done);
308
309 if (list_empty(&pcdev->capture)) {
310 pcdev->active = NULL;
311 return;
312 }
313
314 pcdev->active = list_entry(pcdev->capture.next,
315 struct mx1_buffer, vb.queue);
316
317 /* setup sg list for future DMA */
318 if (likely(!mx1_camera_setup_dma(pcdev))) {
319 unsigned int temp;
320
321 /* enable SOF irq */
322 temp = __raw_readl(pcdev->base + CSICR1) | CSICR1_SOF_INTEN;
323 __raw_writel(temp, pcdev->base + CSICR1);
324 }
325}
326
327static void mx1_camera_dma_irq(int channel, void *data)
328{
329 struct mx1_camera_dev *pcdev = data;
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300330 struct device *dev = pcdev->icd->dev.parent;
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300331 struct mx1_buffer *buf;
332 struct videobuf_buffer *vb;
333 unsigned long flags;
334
335 spin_lock_irqsave(&pcdev->lock, flags);
336
337 imx_dma_disable(channel);
338
339 if (unlikely(!pcdev->active)) {
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300340 dev_err(dev, "DMA End IRQ with no active buffer\n");
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300341 goto out;
342 }
343
344 vb = &pcdev->active->vb;
345 buf = container_of(vb, struct mx1_buffer, vb);
346 WARN_ON(buf->inwork || list_empty(&vb->queue));
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300347 dev_dbg(dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300348 vb, vb->baddr, vb->bsize);
349
350 mx1_camera_wakeup(pcdev, vb, buf);
351out:
352 spin_unlock_irqrestore(&pcdev->lock, flags);
353}
354
355static struct videobuf_queue_ops mx1_videobuf_ops = {
356 .buf_setup = mx1_videobuf_setup,
357 .buf_prepare = mx1_videobuf_prepare,
358 .buf_queue = mx1_videobuf_queue,
359 .buf_release = mx1_videobuf_release,
360};
361
362static void mx1_camera_init_videobuf(struct videobuf_queue *q,
363 struct soc_camera_device *icd)
364{
365 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
366 struct mx1_camera_dev *pcdev = ici->priv;
367
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300368 videobuf_queue_dma_contig_init(q, &mx1_videobuf_ops, icd->dev.parent,
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300369 &pcdev->lock,
370 V4L2_BUF_TYPE_VIDEO_CAPTURE,
371 V4L2_FIELD_NONE,
372 sizeof(struct mx1_buffer), icd);
373}
374
375static int mclk_get_divisor(struct mx1_camera_dev *pcdev)
376{
377 unsigned int mclk = pcdev->mclk;
378 unsigned long div;
379 unsigned long lcdclk;
380
381 lcdclk = clk_get_rate(pcdev->clk);
382
383 /* We verify platform_mclk_10khz != 0, so if anyone breaks it, here
384 * they get a nice Oops */
385 div = (lcdclk + 2 * mclk - 1) / (2 * mclk) - 1;
386
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300387 dev_dbg(pcdev->icd->dev.parent,
388 "System clock %lukHz, target freq %dkHz, divisor %lu\n",
389 lcdclk / 1000, mclk / 1000, div);
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300390
391 return div;
392}
393
394static void mx1_camera_activate(struct mx1_camera_dev *pcdev)
395{
396 unsigned int csicr1 = CSICR1_EN;
397
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300398 dev_dbg(pcdev->icd->dev.parent, "Activate device\n");
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300399
400 clk_enable(pcdev->clk);
401
402 /* enable CSI before doing anything else */
403 __raw_writel(csicr1, pcdev->base + CSICR1);
404
405 csicr1 |= CSICR1_MCLKEN | CSICR1_FCC | CSICR1_GCLK_MODE;
406 csicr1 |= CSICR1_MCLKDIV(mclk_get_divisor(pcdev));
407 csicr1 |= CSICR1_RXFF_LEVEL(2); /* 16 words */
408
409 __raw_writel(csicr1, pcdev->base + CSICR1);
410}
411
412static void mx1_camera_deactivate(struct mx1_camera_dev *pcdev)
413{
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300414 dev_dbg(pcdev->icd->dev.parent, "Deactivate device\n");
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300415
416 /* Disable all CSI interface */
417 __raw_writel(0x00, pcdev->base + CSICR1);
418
419 clk_disable(pcdev->clk);
420}
421
422/* The following two functions absolutely depend on the fact, that
423 * there can be only one camera on i.MX1/i.MXL camera sensor interface */
424static int mx1_camera_add_device(struct soc_camera_device *icd)
425{
426 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
427 struct mx1_camera_dev *pcdev = ici->priv;
428 int ret;
429
430 if (pcdev->icd) {
431 ret = -EBUSY;
432 goto ebusy;
433 }
434
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300435 dev_info(icd->dev.parent, "MX1 Camera driver attached to camera %d\n",
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300436 icd->devnum);
437
438 mx1_camera_activate(pcdev);
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300439
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300440 pcdev->icd = icd;
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300441
442ebusy:
443 return ret;
444}
445
446static void mx1_camera_remove_device(struct soc_camera_device *icd)
447{
448 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
449 struct mx1_camera_dev *pcdev = ici->priv;
450 unsigned int csicr1;
451
452 BUG_ON(icd != pcdev->icd);
453
454 /* disable interrupts */
455 csicr1 = __raw_readl(pcdev->base + CSICR1) & ~CSI_IRQ_MASK;
456 __raw_writel(csicr1, pcdev->base + CSICR1);
457
458 /* Stop DMA engine */
459 imx_dma_disable(pcdev->dma_chan);
460
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300461 dev_info(icd->dev.parent, "MX1 Camera driver detached from camera %d\n",
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300462 icd->devnum);
463
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300464 mx1_camera_deactivate(pcdev);
465
466 pcdev->icd = NULL;
467}
468
469static int mx1_camera_set_crop(struct soc_camera_device *icd,
Guennadi Liakhovetski08590b92009-08-25 11:46:54 -0300470 struct v4l2_crop *a)
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300471{
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -0300472 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
Guennadi Liakhovetski08590b92009-08-25 11:46:54 -0300473
474 return v4l2_subdev_call(sd, video, s_crop, a);
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300475}
476
477static int mx1_camera_set_bus_param(struct soc_camera_device *icd, __u32 pixfmt)
478{
479 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
480 struct mx1_camera_dev *pcdev = ici->priv;
481 unsigned long camera_flags, common_flags;
482 unsigned int csicr1;
483 int ret;
484
485 camera_flags = icd->ops->query_bus_param(icd);
486
487 /* MX1 supports only 8bit buswidth */
488 common_flags = soc_camera_bus_param_compatible(camera_flags,
489 CSI_BUS_FLAGS);
490 if (!common_flags)
491 return -EINVAL;
492
493 icd->buswidth = 8;
494
495 /* Make choises, based on platform choice */
496 if ((common_flags & SOCAM_VSYNC_ACTIVE_HIGH) &&
497 (common_flags & SOCAM_VSYNC_ACTIVE_LOW)) {
498 if (!pcdev->pdata ||
499 pcdev->pdata->flags & MX1_CAMERA_VSYNC_HIGH)
500 common_flags &= ~SOCAM_VSYNC_ACTIVE_LOW;
501 else
502 common_flags &= ~SOCAM_VSYNC_ACTIVE_HIGH;
503 }
504
505 if ((common_flags & SOCAM_PCLK_SAMPLE_RISING) &&
506 (common_flags & SOCAM_PCLK_SAMPLE_FALLING)) {
507 if (!pcdev->pdata ||
508 pcdev->pdata->flags & MX1_CAMERA_PCLK_RISING)
509 common_flags &= ~SOCAM_PCLK_SAMPLE_FALLING;
510 else
511 common_flags &= ~SOCAM_PCLK_SAMPLE_RISING;
512 }
513
514 if ((common_flags & SOCAM_DATA_ACTIVE_HIGH) &&
515 (common_flags & SOCAM_DATA_ACTIVE_LOW)) {
516 if (!pcdev->pdata ||
517 pcdev->pdata->flags & MX1_CAMERA_DATA_HIGH)
518 common_flags &= ~SOCAM_DATA_ACTIVE_LOW;
519 else
520 common_flags &= ~SOCAM_DATA_ACTIVE_HIGH;
521 }
522
523 ret = icd->ops->set_bus_param(icd, common_flags);
524 if (ret < 0)
525 return ret;
526
527 csicr1 = __raw_readl(pcdev->base + CSICR1);
528
529 if (common_flags & SOCAM_PCLK_SAMPLE_RISING)
530 csicr1 |= CSICR1_REDGE;
531 if (common_flags & SOCAM_VSYNC_ACTIVE_HIGH)
532 csicr1 |= CSICR1_SOF_POL;
533 if (common_flags & SOCAM_DATA_ACTIVE_LOW)
534 csicr1 |= CSICR1_DATA_POL;
535
536 __raw_writel(csicr1, pcdev->base + CSICR1);
537
538 return 0;
539}
540
541static int mx1_camera_set_fmt(struct soc_camera_device *icd,
542 struct v4l2_format *f)
543{
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -0300544 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300545 const struct soc_camera_format_xlate *xlate;
546 struct v4l2_pix_format *pix = &f->fmt.pix;
547 int ret;
548
549 xlate = soc_camera_xlate_by_fourcc(icd, pix->pixelformat);
550 if (!xlate) {
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300551 dev_warn(icd->dev.parent, "Format %x not found\n", pix->pixelformat);
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300552 return -EINVAL;
553 }
554
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -0300555 ret = v4l2_subdev_call(sd, video, s_fmt, f);
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300556 if (!ret) {
557 icd->buswidth = xlate->buswidth;
558 icd->current_fmt = xlate->host_fmt;
559 }
560
561 return ret;
562}
563
564static int mx1_camera_try_fmt(struct soc_camera_device *icd,
565 struct v4l2_format *f)
566{
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -0300567 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300568 /* TODO: limit to mx1 hardware capabilities */
569
570 /* limit to sensor capabilities */
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -0300571 return v4l2_subdev_call(sd, video, try_fmt, f);
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300572}
573
574static int mx1_camera_reqbufs(struct soc_camera_file *icf,
575 struct v4l2_requestbuffers *p)
576{
577 int i;
578
579 /* This is for locking debugging only. I removed spinlocks and now I
580 * check whether .prepare is ever called on a linked buffer, or whether
581 * a dma IRQ can occur for an in-work or unlinked buffer. Until now
582 * it hadn't triggered */
583 for (i = 0; i < p->count; i++) {
584 struct mx1_buffer *buf = container_of(icf->vb_vidq.bufs[i],
585 struct mx1_buffer, vb);
586 buf->inwork = 0;
587 INIT_LIST_HEAD(&buf->vb.queue);
588 }
589
590 return 0;
591}
592
593static unsigned int mx1_camera_poll(struct file *file, poll_table *pt)
594{
595 struct soc_camera_file *icf = file->private_data;
596 struct mx1_buffer *buf;
597
598 buf = list_entry(icf->vb_vidq.stream.next, struct mx1_buffer,
599 vb.stream);
600
601 poll_wait(file, &buf->vb.done, pt);
602
603 if (buf->vb.state == VIDEOBUF_DONE ||
604 buf->vb.state == VIDEOBUF_ERROR)
605 return POLLIN | POLLRDNORM;
606
607 return 0;
608}
609
610static int mx1_camera_querycap(struct soc_camera_host *ici,
611 struct v4l2_capability *cap)
612{
613 /* cap->name is set by the friendly caller:-> */
614 strlcpy(cap->card, "i.MX1/i.MXL Camera", sizeof(cap->card));
615 cap->version = VERSION_CODE;
616 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
617
618 return 0;
619}
620
621static struct soc_camera_host_ops mx1_soc_camera_host_ops = {
622 .owner = THIS_MODULE,
623 .add = mx1_camera_add_device,
624 .remove = mx1_camera_remove_device,
625 .set_bus_param = mx1_camera_set_bus_param,
626 .set_crop = mx1_camera_set_crop,
627 .set_fmt = mx1_camera_set_fmt,
628 .try_fmt = mx1_camera_try_fmt,
629 .init_videobuf = mx1_camera_init_videobuf,
630 .reqbufs = mx1_camera_reqbufs,
631 .poll = mx1_camera_poll,
632 .querycap = mx1_camera_querycap,
633};
634
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300635static struct fiq_handler fh = {
636 .name = "csi_sof"
637};
638
639static int __init mx1_camera_probe(struct platform_device *pdev)
640{
641 struct mx1_camera_dev *pcdev;
642 struct resource *res;
643 struct pt_regs regs;
644 struct clk *clk;
645 void __iomem *base;
646 unsigned int irq;
647 int err = 0;
648
649 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
650 irq = platform_get_irq(pdev, 0);
651 if (!res || !irq) {
652 err = -ENODEV;
653 goto exit;
654 }
655
656 clk = clk_get(&pdev->dev, "csi_clk");
657 if (IS_ERR(clk)) {
658 err = PTR_ERR(clk);
659 goto exit;
660 }
661
662 pcdev = kzalloc(sizeof(*pcdev), GFP_KERNEL);
663 if (!pcdev) {
664 dev_err(&pdev->dev, "Could not allocate pcdev\n");
665 err = -ENOMEM;
666 goto exit_put_clk;
667 }
668
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300669 pcdev->res = res;
670 pcdev->clk = clk;
671
672 pcdev->pdata = pdev->dev.platform_data;
673
674 if (pcdev->pdata)
675 pcdev->mclk = pcdev->pdata->mclk_10khz * 10000;
676
677 if (!pcdev->mclk) {
678 dev_warn(&pdev->dev,
679 "mclk_10khz == 0! Please, fix your platform data. "
680 "Using default 20MHz\n");
681 pcdev->mclk = 20000000;
682 }
683
684 INIT_LIST_HEAD(&pcdev->capture);
685 spin_lock_init(&pcdev->lock);
686
687 /*
688 * Request the regions.
689 */
690 if (!request_mem_region(res->start, resource_size(res), DRIVER_NAME)) {
691 err = -EBUSY;
692 goto exit_kfree;
693 }
694
695 base = ioremap(res->start, resource_size(res));
696 if (!base) {
697 err = -ENOMEM;
698 goto exit_release;
699 }
700 pcdev->irq = irq;
701 pcdev->base = base;
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300702
703 /* request dma */
704 pcdev->dma_chan = imx_dma_request_by_prio(DRIVER_NAME, DMA_PRIO_HIGH);
705 if (pcdev->dma_chan < 0) {
Guennadi Liakhovetskieff505f2009-04-24 12:55:48 -0300706 dev_err(&pdev->dev, "Can't request DMA for MX1 CSI\n");
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300707 err = -EBUSY;
708 goto exit_iounmap;
709 }
Guennadi Liakhovetskieff505f2009-04-24 12:55:48 -0300710 dev_dbg(&pdev->dev, "got DMA channel %d\n", pcdev->dma_chan);
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300711
712 imx_dma_setup_handlers(pcdev->dma_chan, mx1_camera_dma_irq, NULL,
713 pcdev);
714
715 imx_dma_config_channel(pcdev->dma_chan, IMX_DMA_TYPE_FIFO,
716 IMX_DMA_MEMSIZE_32, DMA_REQ_CSI_R, 0);
717 /* burst length : 16 words = 64 bytes */
718 imx_dma_config_burstlen(pcdev->dma_chan, 0);
719
720 /* request irq */
721 err = claim_fiq(&fh);
722 if (err) {
Guennadi Liakhovetskieff505f2009-04-24 12:55:48 -0300723 dev_err(&pdev->dev, "Camera interrupt register failed \n");
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300724 goto exit_free_dma;
725 }
726
727 set_fiq_handler(&mx1_camera_sof_fiq_start, &mx1_camera_sof_fiq_end -
728 &mx1_camera_sof_fiq_start);
729
730 regs.ARM_r8 = DMA_BASE + DMA_DIMR;
731 regs.ARM_r9 = DMA_BASE + DMA_CCR(pcdev->dma_chan);
732 regs.ARM_r10 = (long)pcdev->base + CSICR1;
733 regs.ARM_fp = (long)pcdev->base + CSISR;
734 regs.ARM_sp = 1 << pcdev->dma_chan;
735 set_fiq_regs(&regs);
736
737 mxc_set_irq_fiq(irq, 1);
738 enable_fiq(irq);
739
Guennadi Liakhovetskieb6c8552009-04-24 12:55:18 -0300740 pcdev->soc_host.drv_name = DRIVER_NAME;
741 pcdev->soc_host.ops = &mx1_soc_camera_host_ops;
742 pcdev->soc_host.priv = pcdev;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300743 pcdev->soc_host.v4l2_dev.dev = &pdev->dev;
Guennadi Liakhovetskieb6c8552009-04-24 12:55:18 -0300744 pcdev->soc_host.nr = pdev->id;
745 err = soc_camera_host_register(&pcdev->soc_host);
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300746 if (err)
747 goto exit_free_irq;
748
749 dev_info(&pdev->dev, "MX1 Camera driver loaded\n");
750
751 return 0;
752
753exit_free_irq:
754 disable_fiq(irq);
755 mxc_set_irq_fiq(irq, 0);
756 release_fiq(&fh);
757exit_free_dma:
758 imx_dma_free(pcdev->dma_chan);
759exit_iounmap:
760 iounmap(base);
761exit_release:
762 release_mem_region(res->start, resource_size(res));
763exit_kfree:
764 kfree(pcdev);
765exit_put_clk:
766 clk_put(clk);
767exit:
768 return err;
769}
770
771static int __exit mx1_camera_remove(struct platform_device *pdev)
772{
Guennadi Liakhovetskieff505f2009-04-24 12:55:48 -0300773 struct soc_camera_host *soc_host = to_soc_camera_host(&pdev->dev);
774 struct mx1_camera_dev *pcdev = container_of(soc_host,
775 struct mx1_camera_dev, soc_host);
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300776 struct resource *res;
777
778 imx_dma_free(pcdev->dma_chan);
779 disable_fiq(pcdev->irq);
780 mxc_set_irq_fiq(pcdev->irq, 0);
781 release_fiq(&fh);
782
783 clk_put(pcdev->clk);
784
Guennadi Liakhovetskieff505f2009-04-24 12:55:48 -0300785 soc_camera_host_unregister(soc_host);
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300786
787 iounmap(pcdev->base);
788
789 res = pcdev->res;
790 release_mem_region(res->start, resource_size(res));
791
792 kfree(pcdev);
793
794 dev_info(&pdev->dev, "MX1 Camera driver unloaded\n");
795
796 return 0;
797}
798
799static struct platform_driver mx1_camera_driver = {
800 .driver = {
801 .name = DRIVER_NAME,
802 },
803 .remove = __exit_p(mx1_camera_remove),
804};
805
806static int __init mx1_camera_init(void)
807{
808 return platform_driver_probe(&mx1_camera_driver, mx1_camera_probe);
809}
810
811static void __exit mx1_camera_exit(void)
812{
813 return platform_driver_unregister(&mx1_camera_driver);
814}
815
816module_init(mx1_camera_init);
817module_exit(mx1_camera_exit);
818
819MODULE_DESCRIPTION("i.MX1/i.MXL SoC Camera Host driver");
820MODULE_AUTHOR("Paulius Zaleckas <paulius.zaleckas@teltonika.lt>");
821MODULE_LICENSE("GPL v2");
822MODULE_ALIAS("platform:" DRIVER_NAME);