blob: 63f8a0cc33d89ec167701c1bee1a0614eebed89f [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>
Guennadi Liakhovetskif39c1ab2009-11-09 16:11:34 -030031#include <linux/sched.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/slab.h>
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -030033#include <linux/time.h>
34#include <linux/version.h>
35#include <linux/videodev2.h>
36
37#include <media/soc_camera.h>
38#include <media/v4l2-common.h>
39#include <media/v4l2-dev.h>
40#include <media/videobuf-dma-contig.h>
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -030041#include <media/soc_mediabus.h>
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -030042
43#include <asm/dma.h>
44#include <asm/fiq.h>
45#include <mach/dma-mx1-mx2.h>
46#include <mach/hardware.h>
47#include <mach/mx1_camera.h>
48
49/*
50 * CSI registers
51 */
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -030052#define CSICR1 0x00 /* CSI Control Register 1 */
53#define CSISR 0x08 /* CSI Status Register */
54#define CSIRXR 0x10 /* CSI RxFIFO Register */
55
56#define CSICR1_RXFF_LEVEL(x) (((x) & 0x3) << 19)
57#define CSICR1_SOF_POL (1 << 17)
58#define CSICR1_SOF_INTEN (1 << 16)
59#define CSICR1_MCLKDIV(x) (((x) & 0xf) << 12)
60#define CSICR1_MCLKEN (1 << 9)
61#define CSICR1_FCC (1 << 8)
62#define CSICR1_BIG_ENDIAN (1 << 7)
63#define CSICR1_CLR_RXFIFO (1 << 5)
64#define CSICR1_GCLK_MODE (1 << 4)
65#define CSICR1_DATA_POL (1 << 2)
66#define CSICR1_REDGE (1 << 1)
67#define CSICR1_EN (1 << 0)
68
69#define CSISR_SFF_OR_INT (1 << 25)
70#define CSISR_RFF_OR_INT (1 << 24)
71#define CSISR_STATFF_INT (1 << 21)
72#define CSISR_RXFF_INT (1 << 18)
73#define CSISR_SOF_INT (1 << 16)
74#define CSISR_DRDY (1 << 0)
75
76#define VERSION_CODE KERNEL_VERSION(0, 0, 1)
77#define DRIVER_NAME "mx1-camera"
78
79#define CSI_IRQ_MASK (CSISR_SFF_OR_INT | CSISR_RFF_OR_INT | \
80 CSISR_STATFF_INT | CSISR_RXFF_INT | CSISR_SOF_INT)
81
82#define CSI_BUS_FLAGS (SOCAM_MASTER | SOCAM_HSYNC_ACTIVE_HIGH | \
83 SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW | \
84 SOCAM_PCLK_SAMPLE_RISING | SOCAM_PCLK_SAMPLE_FALLING | \
85 SOCAM_DATA_ACTIVE_HIGH | SOCAM_DATA_ACTIVE_LOW | \
86 SOCAM_DATAWIDTH_8)
87
88#define MAX_VIDEO_MEM 16 /* Video memory limit in megabytes */
89
90/*
91 * Structures
92 */
93
94/* buffer for one video frame */
95struct mx1_buffer {
96 /* common v4l buffer stuff -- must be first */
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -030097 struct videobuf_buffer vb;
98 enum v4l2_mbus_pixelcode code;
99 int inwork;
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300100};
101
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -0300102/*
103 * i.MX1/i.MXL is only supposed to handle one camera on its Camera Sensor
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300104 * Interface. If anyone ever builds hardware to enable more than
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -0300105 * one camera, they will have to modify this driver too
106 */
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300107struct mx1_camera_dev {
Guennadi Liakhovetskieb6c8552009-04-24 12:55:18 -0300108 struct soc_camera_host soc_host;
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300109 struct soc_camera_device *icd;
110 struct mx1_camera_pdata *pdata;
111 struct mx1_buffer *active;
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300112 struct resource *res;
113 struct clk *clk;
114 struct list_head capture;
115
116 void __iomem *base;
117 int dma_chan;
118 unsigned int irq;
119 unsigned long mclk;
120
121 spinlock_t lock;
122};
123
124/*
125 * Videobuf operations
126 */
127static int mx1_videobuf_setup(struct videobuf_queue *vq, unsigned int *count,
128 unsigned int *size)
129{
130 struct soc_camera_device *icd = vq->priv_data;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300131 int bytes_per_line = soc_mbus_bytes_per_line(icd->user_width,
132 icd->current_fmt->host_fmt);
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300133
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300134 if (bytes_per_line < 0)
135 return bytes_per_line;
136
137 *size = bytes_per_line * icd->user_height;
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300138
139 if (!*count)
140 *count = 32;
141
Andreas Bombedab7e312010-03-21 16:02:45 -0300142 if (*size * *count > MAX_VIDEO_MEM * 1024 * 1024)
143 *count = (MAX_VIDEO_MEM * 1024 * 1024) / *size;
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300144
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300145 dev_dbg(icd->dev.parent, "count=%d, size=%d\n", *count, *size);
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300146
147 return 0;
148}
149
150static void free_buffer(struct videobuf_queue *vq, struct mx1_buffer *buf)
151{
152 struct soc_camera_device *icd = vq->priv_data;
153 struct videobuf_buffer *vb = &buf->vb;
154
155 BUG_ON(in_interrupt());
156
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300157 dev_dbg(icd->dev.parent, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300158 vb, vb->baddr, vb->bsize);
159
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -0300160 /*
161 * This waits until this buffer is out of danger, i.e., until it is no
162 * longer in STATE_QUEUED or STATE_ACTIVE
163 */
Hans Verkuil0e0809a2010-09-26 09:01:26 -0300164 videobuf_waiton(vq, vb, 0, 0);
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300165 videobuf_dma_contig_free(vq, vb);
166
167 vb->state = VIDEOBUF_NEEDS_INIT;
168}
169
170static int mx1_videobuf_prepare(struct videobuf_queue *vq,
171 struct videobuf_buffer *vb, enum v4l2_field field)
172{
173 struct soc_camera_device *icd = vq->priv_data;
174 struct mx1_buffer *buf = container_of(vb, struct mx1_buffer, vb);
175 int ret;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300176 int bytes_per_line = soc_mbus_bytes_per_line(icd->user_width,
177 icd->current_fmt->host_fmt);
178
179 if (bytes_per_line < 0)
180 return bytes_per_line;
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300181
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300182 dev_dbg(icd->dev.parent, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300183 vb, vb->baddr, vb->bsize);
184
185 /* Added list head initialization on alloc */
186 WARN_ON(!list_empty(&vb->queue));
187
188 BUG_ON(NULL == icd->current_fmt);
189
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -0300190 /*
191 * I think, in buf_prepare you only have to protect global data,
192 * the actual buffer is yours
193 */
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300194 buf->inwork = 1;
195
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300196 if (buf->code != icd->current_fmt->code ||
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300197 vb->width != icd->user_width ||
198 vb->height != icd->user_height ||
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300199 vb->field != field) {
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300200 buf->code = icd->current_fmt->code;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300201 vb->width = icd->user_width;
202 vb->height = icd->user_height;
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300203 vb->field = field;
204 vb->state = VIDEOBUF_NEEDS_INIT;
205 }
206
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300207 vb->size = bytes_per_line * vb->height;
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300208 if (0 != vb->baddr && vb->bsize < vb->size) {
209 ret = -EINVAL;
210 goto out;
211 }
212
213 if (vb->state == VIDEOBUF_NEEDS_INIT) {
214 ret = videobuf_iolock(vq, vb, NULL);
215 if (ret)
216 goto fail;
217
218 vb->state = VIDEOBUF_PREPARED;
219 }
220
221 buf->inwork = 0;
222
223 return 0;
224
225fail:
226 free_buffer(vq, buf);
227out:
228 buf->inwork = 0;
229 return ret;
230}
231
232static int mx1_camera_setup_dma(struct mx1_camera_dev *pcdev)
233{
234 struct videobuf_buffer *vbuf = &pcdev->active->vb;
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300235 struct device *dev = pcdev->icd->dev.parent;
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300236 int ret;
237
238 if (unlikely(!pcdev->active)) {
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300239 dev_err(dev, "DMA End IRQ with no active buffer\n");
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300240 return -EFAULT;
241 }
242
243 /* setup sg list for future DMA */
244 ret = imx_dma_setup_single(pcdev->dma_chan,
245 videobuf_to_dma_contig(vbuf),
246 vbuf->size, pcdev->res->start +
247 CSIRXR, DMA_MODE_READ);
248 if (unlikely(ret))
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300249 dev_err(dev, "Failed to setup DMA sg list\n");
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300250
251 return ret;
252}
253
Guennadi Liakhovetski2dd54a52009-08-05 20:06:31 -0300254/* Called under spinlock_irqsave(&pcdev->lock, ...) */
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300255static void mx1_videobuf_queue(struct videobuf_queue *vq,
256 struct videobuf_buffer *vb)
257{
258 struct soc_camera_device *icd = vq->priv_data;
259 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
260 struct mx1_camera_dev *pcdev = ici->priv;
261 struct mx1_buffer *buf = container_of(vb, struct mx1_buffer, vb);
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300262
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300263 dev_dbg(icd->dev.parent, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300264 vb, vb->baddr, vb->bsize);
265
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300266 list_add_tail(&vb->queue, &pcdev->capture);
267
268 vb->state = VIDEOBUF_ACTIVE;
269
270 if (!pcdev->active) {
271 pcdev->active = buf;
272
273 /* setup sg list for future DMA */
274 if (!mx1_camera_setup_dma(pcdev)) {
275 unsigned int temp;
276 /* enable SOF irq */
277 temp = __raw_readl(pcdev->base + CSICR1) |
278 CSICR1_SOF_INTEN;
279 __raw_writel(temp, pcdev->base + CSICR1);
280 }
281 }
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300282}
283
284static void mx1_videobuf_release(struct videobuf_queue *vq,
285 struct videobuf_buffer *vb)
286{
287 struct mx1_buffer *buf = container_of(vb, struct mx1_buffer, vb);
288#ifdef DEBUG
289 struct soc_camera_device *icd = vq->priv_data;
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300290 struct device *dev = icd->dev.parent;
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300291
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300292 dev_dbg(dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300293 vb, vb->baddr, vb->bsize);
294
295 switch (vb->state) {
296 case VIDEOBUF_ACTIVE:
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300297 dev_dbg(dev, "%s (active)\n", __func__);
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300298 break;
299 case VIDEOBUF_QUEUED:
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300300 dev_dbg(dev, "%s (queued)\n", __func__);
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300301 break;
302 case VIDEOBUF_PREPARED:
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300303 dev_dbg(dev, "%s (prepared)\n", __func__);
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300304 break;
305 default:
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300306 dev_dbg(dev, "%s (unknown)\n", __func__);
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300307 break;
308 }
309#endif
310
311 free_buffer(vq, buf);
312}
313
314static void mx1_camera_wakeup(struct mx1_camera_dev *pcdev,
315 struct videobuf_buffer *vb,
316 struct mx1_buffer *buf)
317{
318 /* _init is used to debug races, see comment in mx1_camera_reqbufs() */
319 list_del_init(&vb->queue);
320 vb->state = VIDEOBUF_DONE;
321 do_gettimeofday(&vb->ts);
322 vb->field_count++;
323 wake_up(&vb->done);
324
325 if (list_empty(&pcdev->capture)) {
326 pcdev->active = NULL;
327 return;
328 }
329
330 pcdev->active = list_entry(pcdev->capture.next,
331 struct mx1_buffer, vb.queue);
332
333 /* setup sg list for future DMA */
334 if (likely(!mx1_camera_setup_dma(pcdev))) {
335 unsigned int temp;
336
337 /* enable SOF irq */
338 temp = __raw_readl(pcdev->base + CSICR1) | CSICR1_SOF_INTEN;
339 __raw_writel(temp, pcdev->base + CSICR1);
340 }
341}
342
343static void mx1_camera_dma_irq(int channel, void *data)
344{
345 struct mx1_camera_dev *pcdev = data;
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300346 struct device *dev = pcdev->icd->dev.parent;
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300347 struct mx1_buffer *buf;
348 struct videobuf_buffer *vb;
349 unsigned long flags;
350
351 spin_lock_irqsave(&pcdev->lock, flags);
352
353 imx_dma_disable(channel);
354
355 if (unlikely(!pcdev->active)) {
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300356 dev_err(dev, "DMA End IRQ with no active buffer\n");
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300357 goto out;
358 }
359
360 vb = &pcdev->active->vb;
361 buf = container_of(vb, struct mx1_buffer, vb);
362 WARN_ON(buf->inwork || list_empty(&vb->queue));
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300363 dev_dbg(dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300364 vb, vb->baddr, vb->bsize);
365
366 mx1_camera_wakeup(pcdev, vb, buf);
367out:
368 spin_unlock_irqrestore(&pcdev->lock, flags);
369}
370
371static struct videobuf_queue_ops mx1_videobuf_ops = {
372 .buf_setup = mx1_videobuf_setup,
373 .buf_prepare = mx1_videobuf_prepare,
374 .buf_queue = mx1_videobuf_queue,
375 .buf_release = mx1_videobuf_release,
376};
377
378static void mx1_camera_init_videobuf(struct videobuf_queue *q,
379 struct soc_camera_device *icd)
380{
381 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
382 struct mx1_camera_dev *pcdev = ici->priv;
383
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300384 videobuf_queue_dma_contig_init(q, &mx1_videobuf_ops, icd->dev.parent,
Guennadi Liakhovetskib6a633c2010-12-25 17:40:26 -0300385 &pcdev->lock, V4L2_BUF_TYPE_VIDEO_CAPTURE,
386 V4L2_FIELD_NONE,
387 sizeof(struct mx1_buffer), icd, &icd->video_lock);
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300388}
389
390static int mclk_get_divisor(struct mx1_camera_dev *pcdev)
391{
392 unsigned int mclk = pcdev->mclk;
393 unsigned long div;
394 unsigned long lcdclk;
395
396 lcdclk = clk_get_rate(pcdev->clk);
397
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -0300398 /*
399 * We verify platform_mclk_10khz != 0, so if anyone breaks it, here
400 * they get a nice Oops
401 */
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300402 div = (lcdclk + 2 * mclk - 1) / (2 * mclk) - 1;
403
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300404 dev_dbg(pcdev->icd->dev.parent,
405 "System clock %lukHz, target freq %dkHz, divisor %lu\n",
406 lcdclk / 1000, mclk / 1000, div);
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300407
408 return div;
409}
410
411static void mx1_camera_activate(struct mx1_camera_dev *pcdev)
412{
413 unsigned int csicr1 = CSICR1_EN;
414
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300415 dev_dbg(pcdev->icd->dev.parent, "Activate device\n");
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300416
417 clk_enable(pcdev->clk);
418
419 /* enable CSI before doing anything else */
420 __raw_writel(csicr1, pcdev->base + CSICR1);
421
422 csicr1 |= CSICR1_MCLKEN | CSICR1_FCC | CSICR1_GCLK_MODE;
423 csicr1 |= CSICR1_MCLKDIV(mclk_get_divisor(pcdev));
424 csicr1 |= CSICR1_RXFF_LEVEL(2); /* 16 words */
425
426 __raw_writel(csicr1, pcdev->base + CSICR1);
427}
428
429static void mx1_camera_deactivate(struct mx1_camera_dev *pcdev)
430{
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300431 dev_dbg(pcdev->icd->dev.parent, "Deactivate device\n");
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300432
433 /* Disable all CSI interface */
434 __raw_writel(0x00, pcdev->base + CSICR1);
435
436 clk_disable(pcdev->clk);
437}
438
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -0300439/*
440 * The following two functions absolutely depend on the fact, that
441 * there can be only one camera on i.MX1/i.MXL camera sensor interface
442 */
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300443static int mx1_camera_add_device(struct soc_camera_device *icd)
444{
445 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
446 struct mx1_camera_dev *pcdev = ici->priv;
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300447
Andre Bartke258c0562011-06-10 07:57:54 -0300448 if (pcdev->icd)
449 return -EBUSY;
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300450
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300451 dev_info(icd->dev.parent, "MX1 Camera driver attached to camera %d\n",
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300452 icd->devnum);
453
454 mx1_camera_activate(pcdev);
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300455
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300456 pcdev->icd = icd;
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300457
Andre Bartke258c0562011-06-10 07:57:54 -0300458 return 0;
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300459}
460
461static void mx1_camera_remove_device(struct soc_camera_device *icd)
462{
463 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
464 struct mx1_camera_dev *pcdev = ici->priv;
465 unsigned int csicr1;
466
467 BUG_ON(icd != pcdev->icd);
468
469 /* disable interrupts */
470 csicr1 = __raw_readl(pcdev->base + CSICR1) & ~CSI_IRQ_MASK;
471 __raw_writel(csicr1, pcdev->base + CSICR1);
472
473 /* Stop DMA engine */
474 imx_dma_disable(pcdev->dma_chan);
475
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300476 dev_info(icd->dev.parent, "MX1 Camera driver detached from camera %d\n",
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300477 icd->devnum);
478
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300479 mx1_camera_deactivate(pcdev);
480
481 pcdev->icd = NULL;
482}
483
484static int mx1_camera_set_crop(struct soc_camera_device *icd,
Guennadi Liakhovetski08590b92009-08-25 11:46:54 -0300485 struct v4l2_crop *a)
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300486{
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -0300487 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
Guennadi Liakhovetski08590b92009-08-25 11:46:54 -0300488
489 return v4l2_subdev_call(sd, video, s_crop, a);
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300490}
491
492static int mx1_camera_set_bus_param(struct soc_camera_device *icd, __u32 pixfmt)
493{
494 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
495 struct mx1_camera_dev *pcdev = ici->priv;
496 unsigned long camera_flags, common_flags;
497 unsigned int csicr1;
498 int ret;
499
500 camera_flags = icd->ops->query_bus_param(icd);
501
502 /* MX1 supports only 8bit buswidth */
503 common_flags = soc_camera_bus_param_compatible(camera_flags,
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300504 CSI_BUS_FLAGS);
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300505 if (!common_flags)
506 return -EINVAL;
507
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300508 /* Make choises, based on platform choice */
509 if ((common_flags & SOCAM_VSYNC_ACTIVE_HIGH) &&
510 (common_flags & SOCAM_VSYNC_ACTIVE_LOW)) {
511 if (!pcdev->pdata ||
512 pcdev->pdata->flags & MX1_CAMERA_VSYNC_HIGH)
513 common_flags &= ~SOCAM_VSYNC_ACTIVE_LOW;
514 else
515 common_flags &= ~SOCAM_VSYNC_ACTIVE_HIGH;
516 }
517
518 if ((common_flags & SOCAM_PCLK_SAMPLE_RISING) &&
519 (common_flags & SOCAM_PCLK_SAMPLE_FALLING)) {
520 if (!pcdev->pdata ||
521 pcdev->pdata->flags & MX1_CAMERA_PCLK_RISING)
522 common_flags &= ~SOCAM_PCLK_SAMPLE_FALLING;
523 else
524 common_flags &= ~SOCAM_PCLK_SAMPLE_RISING;
525 }
526
527 if ((common_flags & SOCAM_DATA_ACTIVE_HIGH) &&
528 (common_flags & SOCAM_DATA_ACTIVE_LOW)) {
529 if (!pcdev->pdata ||
530 pcdev->pdata->flags & MX1_CAMERA_DATA_HIGH)
531 common_flags &= ~SOCAM_DATA_ACTIVE_LOW;
532 else
533 common_flags &= ~SOCAM_DATA_ACTIVE_HIGH;
534 }
535
536 ret = icd->ops->set_bus_param(icd, common_flags);
537 if (ret < 0)
538 return ret;
539
540 csicr1 = __raw_readl(pcdev->base + CSICR1);
541
542 if (common_flags & SOCAM_PCLK_SAMPLE_RISING)
543 csicr1 |= CSICR1_REDGE;
544 if (common_flags & SOCAM_VSYNC_ACTIVE_HIGH)
545 csicr1 |= CSICR1_SOF_POL;
546 if (common_flags & SOCAM_DATA_ACTIVE_LOW)
547 csicr1 |= CSICR1_DATA_POL;
548
549 __raw_writel(csicr1, pcdev->base + CSICR1);
550
551 return 0;
552}
553
554static int mx1_camera_set_fmt(struct soc_camera_device *icd,
555 struct v4l2_format *f)
556{
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -0300557 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300558 const struct soc_camera_format_xlate *xlate;
559 struct v4l2_pix_format *pix = &f->fmt.pix;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300560 struct v4l2_mbus_framefmt mf;
561 int ret, buswidth;
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300562
563 xlate = soc_camera_xlate_by_fourcc(icd, pix->pixelformat);
564 if (!xlate) {
Guennadi Liakhovetski96c75392009-08-25 11:53:23 -0300565 dev_warn(icd->dev.parent, "Format %x not found\n",
566 pix->pixelformat);
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300567 return -EINVAL;
568 }
569
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300570 buswidth = xlate->host_fmt->bits_per_sample;
571 if (buswidth > 8) {
572 dev_warn(icd->dev.parent,
573 "bits-per-sample %d for format %x unsupported\n",
574 buswidth, pix->pixelformat);
575 return -EINVAL;
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300576 }
577
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300578 mf.width = pix->width;
579 mf.height = pix->height;
580 mf.field = pix->field;
581 mf.colorspace = pix->colorspace;
582 mf.code = xlate->code;
583
584 ret = v4l2_subdev_call(sd, video, s_mbus_fmt, &mf);
585 if (ret < 0)
586 return ret;
587
588 if (mf.code != xlate->code)
589 return -EINVAL;
590
591 pix->width = mf.width;
592 pix->height = mf.height;
593 pix->field = mf.field;
594 pix->colorspace = mf.colorspace;
595 icd->current_fmt = xlate;
596
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300597 return ret;
598}
599
600static int mx1_camera_try_fmt(struct soc_camera_device *icd,
601 struct v4l2_format *f)
602{
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -0300603 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300604 const struct soc_camera_format_xlate *xlate;
605 struct v4l2_pix_format *pix = &f->fmt.pix;
606 struct v4l2_mbus_framefmt mf;
607 int ret;
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300608 /* TODO: limit to mx1 hardware capabilities */
609
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300610 xlate = soc_camera_xlate_by_fourcc(icd, pix->pixelformat);
611 if (!xlate) {
612 dev_warn(icd->dev.parent, "Format %x not found\n",
613 pix->pixelformat);
614 return -EINVAL;
615 }
616
617 mf.width = pix->width;
618 mf.height = pix->height;
619 mf.field = pix->field;
620 mf.colorspace = pix->colorspace;
621 mf.code = xlate->code;
622
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300623 /* limit to sensor capabilities */
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300624 ret = v4l2_subdev_call(sd, video, try_mbus_fmt, &mf);
625 if (ret < 0)
626 return ret;
627
628 pix->width = mf.width;
629 pix->height = mf.height;
630 pix->field = mf.field;
631 pix->colorspace = mf.colorspace;
632
633 return 0;
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300634}
635
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300636static int mx1_camera_reqbufs(struct soc_camera_device *icd,
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300637 struct v4l2_requestbuffers *p)
638{
639 int i;
640
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -0300641 /*
642 * This is for locking debugging only. I removed spinlocks and now I
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300643 * check whether .prepare is ever called on a linked buffer, or whether
644 * a dma IRQ can occur for an in-work or unlinked buffer. Until now
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -0300645 * it hadn't triggered
646 */
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300647 for (i = 0; i < p->count; i++) {
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300648 struct mx1_buffer *buf = container_of(icd->vb_vidq.bufs[i],
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300649 struct mx1_buffer, vb);
650 buf->inwork = 0;
651 INIT_LIST_HEAD(&buf->vb.queue);
652 }
653
654 return 0;
655}
656
657static unsigned int mx1_camera_poll(struct file *file, poll_table *pt)
658{
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300659 struct soc_camera_device *icd = file->private_data;
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300660 struct mx1_buffer *buf;
661
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300662 buf = list_entry(icd->vb_vidq.stream.next, struct mx1_buffer,
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300663 vb.stream);
664
665 poll_wait(file, &buf->vb.done, pt);
666
667 if (buf->vb.state == VIDEOBUF_DONE ||
668 buf->vb.state == VIDEOBUF_ERROR)
669 return POLLIN | POLLRDNORM;
670
671 return 0;
672}
673
674static int mx1_camera_querycap(struct soc_camera_host *ici,
675 struct v4l2_capability *cap)
676{
677 /* cap->name is set by the friendly caller:-> */
678 strlcpy(cap->card, "i.MX1/i.MXL Camera", sizeof(cap->card));
679 cap->version = VERSION_CODE;
680 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
681
682 return 0;
683}
684
685static struct soc_camera_host_ops mx1_soc_camera_host_ops = {
686 .owner = THIS_MODULE,
687 .add = mx1_camera_add_device,
688 .remove = mx1_camera_remove_device,
689 .set_bus_param = mx1_camera_set_bus_param,
690 .set_crop = mx1_camera_set_crop,
691 .set_fmt = mx1_camera_set_fmt,
692 .try_fmt = mx1_camera_try_fmt,
693 .init_videobuf = mx1_camera_init_videobuf,
694 .reqbufs = mx1_camera_reqbufs,
695 .poll = mx1_camera_poll,
696 .querycap = mx1_camera_querycap,
697};
698
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300699static struct fiq_handler fh = {
700 .name = "csi_sof"
701};
702
703static int __init mx1_camera_probe(struct platform_device *pdev)
704{
705 struct mx1_camera_dev *pcdev;
706 struct resource *res;
707 struct pt_regs regs;
708 struct clk *clk;
709 void __iomem *base;
710 unsigned int irq;
711 int err = 0;
712
713 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
714 irq = platform_get_irq(pdev, 0);
Uwe Kleine-König30883ea2010-01-09 20:44:06 -0300715 if (!res || (int)irq <= 0) {
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300716 err = -ENODEV;
717 goto exit;
718 }
719
720 clk = clk_get(&pdev->dev, "csi_clk");
721 if (IS_ERR(clk)) {
722 err = PTR_ERR(clk);
723 goto exit;
724 }
725
726 pcdev = kzalloc(sizeof(*pcdev), GFP_KERNEL);
727 if (!pcdev) {
728 dev_err(&pdev->dev, "Could not allocate pcdev\n");
729 err = -ENOMEM;
730 goto exit_put_clk;
731 }
732
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300733 pcdev->res = res;
734 pcdev->clk = clk;
735
736 pcdev->pdata = pdev->dev.platform_data;
737
738 if (pcdev->pdata)
739 pcdev->mclk = pcdev->pdata->mclk_10khz * 10000;
740
741 if (!pcdev->mclk) {
742 dev_warn(&pdev->dev,
743 "mclk_10khz == 0! Please, fix your platform data. "
744 "Using default 20MHz\n");
745 pcdev->mclk = 20000000;
746 }
747
748 INIT_LIST_HEAD(&pcdev->capture);
749 spin_lock_init(&pcdev->lock);
750
751 /*
752 * Request the regions.
753 */
754 if (!request_mem_region(res->start, resource_size(res), DRIVER_NAME)) {
755 err = -EBUSY;
756 goto exit_kfree;
757 }
758
759 base = ioremap(res->start, resource_size(res));
760 if (!base) {
761 err = -ENOMEM;
762 goto exit_release;
763 }
764 pcdev->irq = irq;
765 pcdev->base = base;
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300766
767 /* request dma */
768 pcdev->dma_chan = imx_dma_request_by_prio(DRIVER_NAME, DMA_PRIO_HIGH);
769 if (pcdev->dma_chan < 0) {
Guennadi Liakhovetskieff505f2009-04-24 12:55:48 -0300770 dev_err(&pdev->dev, "Can't request DMA for MX1 CSI\n");
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300771 err = -EBUSY;
772 goto exit_iounmap;
773 }
Guennadi Liakhovetskieff505f2009-04-24 12:55:48 -0300774 dev_dbg(&pdev->dev, "got DMA channel %d\n", pcdev->dma_chan);
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300775
776 imx_dma_setup_handlers(pcdev->dma_chan, mx1_camera_dma_irq, NULL,
777 pcdev);
778
779 imx_dma_config_channel(pcdev->dma_chan, IMX_DMA_TYPE_FIFO,
Uwe Kleine-Königb7d41d62010-03-27 18:42:13 -0300780 IMX_DMA_MEMSIZE_32, MX1_DMA_REQ_CSI_R, 0);
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300781 /* burst length : 16 words = 64 bytes */
782 imx_dma_config_burstlen(pcdev->dma_chan, 0);
783
784 /* request irq */
785 err = claim_fiq(&fh);
786 if (err) {
Guennadi Liakhovetskieff505f2009-04-24 12:55:48 -0300787 dev_err(&pdev->dev, "Camera interrupt register failed \n");
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300788 goto exit_free_dma;
789 }
790
791 set_fiq_handler(&mx1_camera_sof_fiq_start, &mx1_camera_sof_fiq_end -
792 &mx1_camera_sof_fiq_start);
793
Uwe Kleine-Königb7d41d62010-03-27 18:42:13 -0300794 regs.ARM_r8 = (long)MX1_DMA_DIMR;
795 regs.ARM_r9 = (long)MX1_DMA_CCR(pcdev->dma_chan);
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300796 regs.ARM_r10 = (long)pcdev->base + CSICR1;
797 regs.ARM_fp = (long)pcdev->base + CSISR;
798 regs.ARM_sp = 1 << pcdev->dma_chan;
799 set_fiq_regs(&regs);
800
801 mxc_set_irq_fiq(irq, 1);
802 enable_fiq(irq);
803
Guennadi Liakhovetskieb6c8552009-04-24 12:55:18 -0300804 pcdev->soc_host.drv_name = DRIVER_NAME;
805 pcdev->soc_host.ops = &mx1_soc_camera_host_ops;
806 pcdev->soc_host.priv = pcdev;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300807 pcdev->soc_host.v4l2_dev.dev = &pdev->dev;
Guennadi Liakhovetskieb6c8552009-04-24 12:55:18 -0300808 pcdev->soc_host.nr = pdev->id;
809 err = soc_camera_host_register(&pcdev->soc_host);
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300810 if (err)
811 goto exit_free_irq;
812
813 dev_info(&pdev->dev, "MX1 Camera driver loaded\n");
814
815 return 0;
816
817exit_free_irq:
818 disable_fiq(irq);
819 mxc_set_irq_fiq(irq, 0);
820 release_fiq(&fh);
821exit_free_dma:
822 imx_dma_free(pcdev->dma_chan);
823exit_iounmap:
824 iounmap(base);
825exit_release:
826 release_mem_region(res->start, resource_size(res));
827exit_kfree:
828 kfree(pcdev);
829exit_put_clk:
830 clk_put(clk);
831exit:
832 return err;
833}
834
835static int __exit mx1_camera_remove(struct platform_device *pdev)
836{
Guennadi Liakhovetskieff505f2009-04-24 12:55:48 -0300837 struct soc_camera_host *soc_host = to_soc_camera_host(&pdev->dev);
838 struct mx1_camera_dev *pcdev = container_of(soc_host,
839 struct mx1_camera_dev, soc_host);
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300840 struct resource *res;
841
842 imx_dma_free(pcdev->dma_chan);
843 disable_fiq(pcdev->irq);
844 mxc_set_irq_fiq(pcdev->irq, 0);
845 release_fiq(&fh);
846
847 clk_put(pcdev->clk);
848
Guennadi Liakhovetskieff505f2009-04-24 12:55:48 -0300849 soc_camera_host_unregister(soc_host);
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300850
851 iounmap(pcdev->base);
852
853 res = pcdev->res;
854 release_mem_region(res->start, resource_size(res));
855
856 kfree(pcdev);
857
858 dev_info(&pdev->dev, "MX1 Camera driver unloaded\n");
859
860 return 0;
861}
862
863static struct platform_driver mx1_camera_driver = {
864 .driver = {
865 .name = DRIVER_NAME,
866 },
867 .remove = __exit_p(mx1_camera_remove),
868};
869
870static int __init mx1_camera_init(void)
871{
872 return platform_driver_probe(&mx1_camera_driver, mx1_camera_probe);
873}
874
875static void __exit mx1_camera_exit(void)
876{
877 return platform_driver_unregister(&mx1_camera_driver);
878}
879
880module_init(mx1_camera_init);
881module_exit(mx1_camera_exit);
882
883MODULE_DESCRIPTION("i.MX1/i.MXL SoC Camera Host driver");
884MODULE_AUTHOR("Paulius Zaleckas <paulius.zaleckas@teltonika.lt>");
885MODULE_LICENSE("GPL v2");
886MODULE_ALIAS("platform:" DRIVER_NAME);