blob: 4c1a439373c5b8e0b59bdc6078948b3eb1549153 [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>
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -030032#include <linux/time.h>
33#include <linux/version.h>
34#include <linux/videodev2.h>
35
36#include <media/soc_camera.h>
37#include <media/v4l2-common.h>
38#include <media/v4l2-dev.h>
39#include <media/videobuf-dma-contig.h>
40
41#include <asm/dma.h>
42#include <asm/fiq.h>
43#include <mach/dma-mx1-mx2.h>
44#include <mach/hardware.h>
45#include <mach/mx1_camera.h>
46
47/*
48 * CSI registers
49 */
50#define DMA_CCR(x) (0x8c + ((x) << 6)) /* Control Registers */
51#define DMA_DIMR 0x08 /* Interrupt mask Register */
52#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 */
97 struct videobuf_buffer vb;
98 const struct soc_camera_data_format *fmt;
99 int inwork;
100};
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;
131
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300132 *size = icd->user_width * icd->user_height *
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300133 ((icd->current_fmt->depth + 7) >> 3);
134
135 if (!*count)
136 *count = 32;
137
138 while (*size * *count > MAX_VIDEO_MEM * 1024 * 1024)
139 (*count)--;
140
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300141 dev_dbg(icd->dev.parent, "count=%d, size=%d\n", *count, *size);
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300142
143 return 0;
144}
145
146static void free_buffer(struct videobuf_queue *vq, struct mx1_buffer *buf)
147{
148 struct soc_camera_device *icd = vq->priv_data;
149 struct videobuf_buffer *vb = &buf->vb;
150
151 BUG_ON(in_interrupt());
152
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300153 dev_dbg(icd->dev.parent, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300154 vb, vb->baddr, vb->bsize);
155
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -0300156 /*
157 * This waits until this buffer is out of danger, i.e., until it is no
158 * longer in STATE_QUEUED or STATE_ACTIVE
159 */
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300160 videobuf_waiton(vb, 0, 0);
161 videobuf_dma_contig_free(vq, vb);
162
163 vb->state = VIDEOBUF_NEEDS_INIT;
164}
165
166static int mx1_videobuf_prepare(struct videobuf_queue *vq,
167 struct videobuf_buffer *vb, enum v4l2_field field)
168{
169 struct soc_camera_device *icd = vq->priv_data;
170 struct mx1_buffer *buf = container_of(vb, struct mx1_buffer, vb);
171 int ret;
172
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300173 dev_dbg(icd->dev.parent, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300174 vb, vb->baddr, vb->bsize);
175
176 /* Added list head initialization on alloc */
177 WARN_ON(!list_empty(&vb->queue));
178
179 BUG_ON(NULL == icd->current_fmt);
180
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -0300181 /*
182 * I think, in buf_prepare you only have to protect global data,
183 * the actual buffer is yours
184 */
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300185 buf->inwork = 1;
186
187 if (buf->fmt != icd->current_fmt ||
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300188 vb->width != icd->user_width ||
189 vb->height != icd->user_height ||
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300190 vb->field != field) {
191 buf->fmt = icd->current_fmt;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300192 vb->width = icd->user_width;
193 vb->height = icd->user_height;
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300194 vb->field = field;
195 vb->state = VIDEOBUF_NEEDS_INIT;
196 }
197
198 vb->size = vb->width * vb->height * ((buf->fmt->depth + 7) >> 3);
199 if (0 != vb->baddr && vb->bsize < vb->size) {
200 ret = -EINVAL;
201 goto out;
202 }
203
204 if (vb->state == VIDEOBUF_NEEDS_INIT) {
205 ret = videobuf_iolock(vq, vb, NULL);
206 if (ret)
207 goto fail;
208
209 vb->state = VIDEOBUF_PREPARED;
210 }
211
212 buf->inwork = 0;
213
214 return 0;
215
216fail:
217 free_buffer(vq, buf);
218out:
219 buf->inwork = 0;
220 return ret;
221}
222
223static int mx1_camera_setup_dma(struct mx1_camera_dev *pcdev)
224{
225 struct videobuf_buffer *vbuf = &pcdev->active->vb;
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300226 struct device *dev = pcdev->icd->dev.parent;
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300227 int ret;
228
229 if (unlikely(!pcdev->active)) {
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300230 dev_err(dev, "DMA End IRQ with no active buffer\n");
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300231 return -EFAULT;
232 }
233
234 /* setup sg list for future DMA */
235 ret = imx_dma_setup_single(pcdev->dma_chan,
236 videobuf_to_dma_contig(vbuf),
237 vbuf->size, pcdev->res->start +
238 CSIRXR, DMA_MODE_READ);
239 if (unlikely(ret))
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300240 dev_err(dev, "Failed to setup DMA sg list\n");
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300241
242 return ret;
243}
244
Guennadi Liakhovetski2dd54a52009-08-05 20:06:31 -0300245/* Called under spinlock_irqsave(&pcdev->lock, ...) */
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300246static void mx1_videobuf_queue(struct videobuf_queue *vq,
247 struct videobuf_buffer *vb)
248{
249 struct soc_camera_device *icd = vq->priv_data;
250 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
251 struct mx1_camera_dev *pcdev = ici->priv;
252 struct mx1_buffer *buf = container_of(vb, struct mx1_buffer, vb);
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300253
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300254 dev_dbg(icd->dev.parent, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300255 vb, vb->baddr, vb->bsize);
256
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300257 list_add_tail(&vb->queue, &pcdev->capture);
258
259 vb->state = VIDEOBUF_ACTIVE;
260
261 if (!pcdev->active) {
262 pcdev->active = buf;
263
264 /* setup sg list for future DMA */
265 if (!mx1_camera_setup_dma(pcdev)) {
266 unsigned int temp;
267 /* enable SOF irq */
268 temp = __raw_readl(pcdev->base + CSICR1) |
269 CSICR1_SOF_INTEN;
270 __raw_writel(temp, pcdev->base + CSICR1);
271 }
272 }
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300273}
274
275static void mx1_videobuf_release(struct videobuf_queue *vq,
276 struct videobuf_buffer *vb)
277{
278 struct mx1_buffer *buf = container_of(vb, struct mx1_buffer, vb);
279#ifdef DEBUG
280 struct soc_camera_device *icd = vq->priv_data;
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300281 struct device *dev = icd->dev.parent;
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300282
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300283 dev_dbg(dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300284 vb, vb->baddr, vb->bsize);
285
286 switch (vb->state) {
287 case VIDEOBUF_ACTIVE:
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300288 dev_dbg(dev, "%s (active)\n", __func__);
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300289 break;
290 case VIDEOBUF_QUEUED:
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300291 dev_dbg(dev, "%s (queued)\n", __func__);
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300292 break;
293 case VIDEOBUF_PREPARED:
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300294 dev_dbg(dev, "%s (prepared)\n", __func__);
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300295 break;
296 default:
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300297 dev_dbg(dev, "%s (unknown)\n", __func__);
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300298 break;
299 }
300#endif
301
302 free_buffer(vq, buf);
303}
304
305static void mx1_camera_wakeup(struct mx1_camera_dev *pcdev,
306 struct videobuf_buffer *vb,
307 struct mx1_buffer *buf)
308{
309 /* _init is used to debug races, see comment in mx1_camera_reqbufs() */
310 list_del_init(&vb->queue);
311 vb->state = VIDEOBUF_DONE;
312 do_gettimeofday(&vb->ts);
313 vb->field_count++;
314 wake_up(&vb->done);
315
316 if (list_empty(&pcdev->capture)) {
317 pcdev->active = NULL;
318 return;
319 }
320
321 pcdev->active = list_entry(pcdev->capture.next,
322 struct mx1_buffer, vb.queue);
323
324 /* setup sg list for future DMA */
325 if (likely(!mx1_camera_setup_dma(pcdev))) {
326 unsigned int temp;
327
328 /* enable SOF irq */
329 temp = __raw_readl(pcdev->base + CSICR1) | CSICR1_SOF_INTEN;
330 __raw_writel(temp, pcdev->base + CSICR1);
331 }
332}
333
334static void mx1_camera_dma_irq(int channel, void *data)
335{
336 struct mx1_camera_dev *pcdev = data;
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300337 struct device *dev = pcdev->icd->dev.parent;
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300338 struct mx1_buffer *buf;
339 struct videobuf_buffer *vb;
340 unsigned long flags;
341
342 spin_lock_irqsave(&pcdev->lock, flags);
343
344 imx_dma_disable(channel);
345
346 if (unlikely(!pcdev->active)) {
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300347 dev_err(dev, "DMA End IRQ with no active buffer\n");
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300348 goto out;
349 }
350
351 vb = &pcdev->active->vb;
352 buf = container_of(vb, struct mx1_buffer, vb);
353 WARN_ON(buf->inwork || list_empty(&vb->queue));
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300354 dev_dbg(dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300355 vb, vb->baddr, vb->bsize);
356
357 mx1_camera_wakeup(pcdev, vb, buf);
358out:
359 spin_unlock_irqrestore(&pcdev->lock, flags);
360}
361
362static struct videobuf_queue_ops mx1_videobuf_ops = {
363 .buf_setup = mx1_videobuf_setup,
364 .buf_prepare = mx1_videobuf_prepare,
365 .buf_queue = mx1_videobuf_queue,
366 .buf_release = mx1_videobuf_release,
367};
368
369static void mx1_camera_init_videobuf(struct videobuf_queue *q,
370 struct soc_camera_device *icd)
371{
372 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
373 struct mx1_camera_dev *pcdev = ici->priv;
374
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300375 videobuf_queue_dma_contig_init(q, &mx1_videobuf_ops, icd->dev.parent,
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300376 &pcdev->lock,
377 V4L2_BUF_TYPE_VIDEO_CAPTURE,
378 V4L2_FIELD_NONE,
379 sizeof(struct mx1_buffer), icd);
380}
381
382static int mclk_get_divisor(struct mx1_camera_dev *pcdev)
383{
384 unsigned int mclk = pcdev->mclk;
385 unsigned long div;
386 unsigned long lcdclk;
387
388 lcdclk = clk_get_rate(pcdev->clk);
389
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -0300390 /*
391 * We verify platform_mclk_10khz != 0, so if anyone breaks it, here
392 * they get a nice Oops
393 */
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300394 div = (lcdclk + 2 * mclk - 1) / (2 * mclk) - 1;
395
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300396 dev_dbg(pcdev->icd->dev.parent,
397 "System clock %lukHz, target freq %dkHz, divisor %lu\n",
398 lcdclk / 1000, mclk / 1000, div);
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300399
400 return div;
401}
402
403static void mx1_camera_activate(struct mx1_camera_dev *pcdev)
404{
405 unsigned int csicr1 = CSICR1_EN;
406
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300407 dev_dbg(pcdev->icd->dev.parent, "Activate device\n");
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300408
409 clk_enable(pcdev->clk);
410
411 /* enable CSI before doing anything else */
412 __raw_writel(csicr1, pcdev->base + CSICR1);
413
414 csicr1 |= CSICR1_MCLKEN | CSICR1_FCC | CSICR1_GCLK_MODE;
415 csicr1 |= CSICR1_MCLKDIV(mclk_get_divisor(pcdev));
416 csicr1 |= CSICR1_RXFF_LEVEL(2); /* 16 words */
417
418 __raw_writel(csicr1, pcdev->base + CSICR1);
419}
420
421static void mx1_camera_deactivate(struct mx1_camera_dev *pcdev)
422{
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300423 dev_dbg(pcdev->icd->dev.parent, "Deactivate device\n");
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300424
425 /* Disable all CSI interface */
426 __raw_writel(0x00, pcdev->base + CSICR1);
427
428 clk_disable(pcdev->clk);
429}
430
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -0300431/*
432 * The following two functions absolutely depend on the fact, that
433 * there can be only one camera on i.MX1/i.MXL camera sensor interface
434 */
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300435static int mx1_camera_add_device(struct soc_camera_device *icd)
436{
437 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
438 struct mx1_camera_dev *pcdev = ici->priv;
439 int ret;
440
441 if (pcdev->icd) {
442 ret = -EBUSY;
443 goto ebusy;
444 }
445
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300446 dev_info(icd->dev.parent, "MX1 Camera driver attached to camera %d\n",
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300447 icd->devnum);
448
449 mx1_camera_activate(pcdev);
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300450
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300451 pcdev->icd = icd;
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300452
453ebusy:
454 return ret;
455}
456
457static void mx1_camera_remove_device(struct soc_camera_device *icd)
458{
459 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
460 struct mx1_camera_dev *pcdev = ici->priv;
461 unsigned int csicr1;
462
463 BUG_ON(icd != pcdev->icd);
464
465 /* disable interrupts */
466 csicr1 = __raw_readl(pcdev->base + CSICR1) & ~CSI_IRQ_MASK;
467 __raw_writel(csicr1, pcdev->base + CSICR1);
468
469 /* Stop DMA engine */
470 imx_dma_disable(pcdev->dma_chan);
471
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300472 dev_info(icd->dev.parent, "MX1 Camera driver detached from camera %d\n",
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300473 icd->devnum);
474
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300475 mx1_camera_deactivate(pcdev);
476
477 pcdev->icd = NULL;
478}
479
480static int mx1_camera_set_crop(struct soc_camera_device *icd,
Guennadi Liakhovetski08590b92009-08-25 11:46:54 -0300481 struct v4l2_crop *a)
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300482{
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -0300483 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
Guennadi Liakhovetski08590b92009-08-25 11:46:54 -0300484
485 return v4l2_subdev_call(sd, video, s_crop, a);
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300486}
487
488static int mx1_camera_set_bus_param(struct soc_camera_device *icd, __u32 pixfmt)
489{
490 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
491 struct mx1_camera_dev *pcdev = ici->priv;
492 unsigned long camera_flags, common_flags;
493 unsigned int csicr1;
494 int ret;
495
496 camera_flags = icd->ops->query_bus_param(icd);
497
498 /* MX1 supports only 8bit buswidth */
499 common_flags = soc_camera_bus_param_compatible(camera_flags,
500 CSI_BUS_FLAGS);
501 if (!common_flags)
502 return -EINVAL;
503
504 icd->buswidth = 8;
505
506 /* Make choises, based on platform choice */
507 if ((common_flags & SOCAM_VSYNC_ACTIVE_HIGH) &&
508 (common_flags & SOCAM_VSYNC_ACTIVE_LOW)) {
509 if (!pcdev->pdata ||
510 pcdev->pdata->flags & MX1_CAMERA_VSYNC_HIGH)
511 common_flags &= ~SOCAM_VSYNC_ACTIVE_LOW;
512 else
513 common_flags &= ~SOCAM_VSYNC_ACTIVE_HIGH;
514 }
515
516 if ((common_flags & SOCAM_PCLK_SAMPLE_RISING) &&
517 (common_flags & SOCAM_PCLK_SAMPLE_FALLING)) {
518 if (!pcdev->pdata ||
519 pcdev->pdata->flags & MX1_CAMERA_PCLK_RISING)
520 common_flags &= ~SOCAM_PCLK_SAMPLE_FALLING;
521 else
522 common_flags &= ~SOCAM_PCLK_SAMPLE_RISING;
523 }
524
525 if ((common_flags & SOCAM_DATA_ACTIVE_HIGH) &&
526 (common_flags & SOCAM_DATA_ACTIVE_LOW)) {
527 if (!pcdev->pdata ||
528 pcdev->pdata->flags & MX1_CAMERA_DATA_HIGH)
529 common_flags &= ~SOCAM_DATA_ACTIVE_LOW;
530 else
531 common_flags &= ~SOCAM_DATA_ACTIVE_HIGH;
532 }
533
534 ret = icd->ops->set_bus_param(icd, common_flags);
535 if (ret < 0)
536 return ret;
537
538 csicr1 = __raw_readl(pcdev->base + CSICR1);
539
540 if (common_flags & SOCAM_PCLK_SAMPLE_RISING)
541 csicr1 |= CSICR1_REDGE;
542 if (common_flags & SOCAM_VSYNC_ACTIVE_HIGH)
543 csicr1 |= CSICR1_SOF_POL;
544 if (common_flags & SOCAM_DATA_ACTIVE_LOW)
545 csicr1 |= CSICR1_DATA_POL;
546
547 __raw_writel(csicr1, pcdev->base + CSICR1);
548
549 return 0;
550}
551
552static int mx1_camera_set_fmt(struct soc_camera_device *icd,
553 struct v4l2_format *f)
554{
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -0300555 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300556 const struct soc_camera_format_xlate *xlate;
557 struct v4l2_pix_format *pix = &f->fmt.pix;
558 int ret;
559
560 xlate = soc_camera_xlate_by_fourcc(icd, pix->pixelformat);
561 if (!xlate) {
Guennadi Liakhovetski96c75392009-08-25 11:53:23 -0300562 dev_warn(icd->dev.parent, "Format %x not found\n",
563 pix->pixelformat);
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300564 return -EINVAL;
565 }
566
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -0300567 ret = v4l2_subdev_call(sd, video, s_fmt, f);
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300568 if (!ret) {
569 icd->buswidth = xlate->buswidth;
570 icd->current_fmt = xlate->host_fmt;
571 }
572
573 return ret;
574}
575
576static int mx1_camera_try_fmt(struct soc_camera_device *icd,
577 struct v4l2_format *f)
578{
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -0300579 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300580 /* TODO: limit to mx1 hardware capabilities */
581
582 /* limit to sensor capabilities */
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -0300583 return v4l2_subdev_call(sd, video, try_fmt, f);
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300584}
585
586static int mx1_camera_reqbufs(struct soc_camera_file *icf,
587 struct v4l2_requestbuffers *p)
588{
589 int i;
590
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -0300591 /*
592 * This is for locking debugging only. I removed spinlocks and now I
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300593 * check whether .prepare is ever called on a linked buffer, or whether
594 * a dma IRQ can occur for an in-work or unlinked buffer. Until now
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -0300595 * it hadn't triggered
596 */
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300597 for (i = 0; i < p->count; i++) {
598 struct mx1_buffer *buf = container_of(icf->vb_vidq.bufs[i],
599 struct mx1_buffer, vb);
600 buf->inwork = 0;
601 INIT_LIST_HEAD(&buf->vb.queue);
602 }
603
604 return 0;
605}
606
607static unsigned int mx1_camera_poll(struct file *file, poll_table *pt)
608{
609 struct soc_camera_file *icf = file->private_data;
610 struct mx1_buffer *buf;
611
612 buf = list_entry(icf->vb_vidq.stream.next, struct mx1_buffer,
613 vb.stream);
614
615 poll_wait(file, &buf->vb.done, pt);
616
617 if (buf->vb.state == VIDEOBUF_DONE ||
618 buf->vb.state == VIDEOBUF_ERROR)
619 return POLLIN | POLLRDNORM;
620
621 return 0;
622}
623
624static int mx1_camera_querycap(struct soc_camera_host *ici,
625 struct v4l2_capability *cap)
626{
627 /* cap->name is set by the friendly caller:-> */
628 strlcpy(cap->card, "i.MX1/i.MXL Camera", sizeof(cap->card));
629 cap->version = VERSION_CODE;
630 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
631
632 return 0;
633}
634
635static struct soc_camera_host_ops mx1_soc_camera_host_ops = {
636 .owner = THIS_MODULE,
637 .add = mx1_camera_add_device,
638 .remove = mx1_camera_remove_device,
639 .set_bus_param = mx1_camera_set_bus_param,
640 .set_crop = mx1_camera_set_crop,
641 .set_fmt = mx1_camera_set_fmt,
642 .try_fmt = mx1_camera_try_fmt,
643 .init_videobuf = mx1_camera_init_videobuf,
644 .reqbufs = mx1_camera_reqbufs,
645 .poll = mx1_camera_poll,
646 .querycap = mx1_camera_querycap,
647};
648
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300649static struct fiq_handler fh = {
650 .name = "csi_sof"
651};
652
653static int __init mx1_camera_probe(struct platform_device *pdev)
654{
655 struct mx1_camera_dev *pcdev;
656 struct resource *res;
657 struct pt_regs regs;
658 struct clk *clk;
659 void __iomem *base;
660 unsigned int irq;
661 int err = 0;
662
663 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
664 irq = platform_get_irq(pdev, 0);
665 if (!res || !irq) {
666 err = -ENODEV;
667 goto exit;
668 }
669
670 clk = clk_get(&pdev->dev, "csi_clk");
671 if (IS_ERR(clk)) {
672 err = PTR_ERR(clk);
673 goto exit;
674 }
675
676 pcdev = kzalloc(sizeof(*pcdev), GFP_KERNEL);
677 if (!pcdev) {
678 dev_err(&pdev->dev, "Could not allocate pcdev\n");
679 err = -ENOMEM;
680 goto exit_put_clk;
681 }
682
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300683 pcdev->res = res;
684 pcdev->clk = clk;
685
686 pcdev->pdata = pdev->dev.platform_data;
687
688 if (pcdev->pdata)
689 pcdev->mclk = pcdev->pdata->mclk_10khz * 10000;
690
691 if (!pcdev->mclk) {
692 dev_warn(&pdev->dev,
693 "mclk_10khz == 0! Please, fix your platform data. "
694 "Using default 20MHz\n");
695 pcdev->mclk = 20000000;
696 }
697
698 INIT_LIST_HEAD(&pcdev->capture);
699 spin_lock_init(&pcdev->lock);
700
701 /*
702 * Request the regions.
703 */
704 if (!request_mem_region(res->start, resource_size(res), DRIVER_NAME)) {
705 err = -EBUSY;
706 goto exit_kfree;
707 }
708
709 base = ioremap(res->start, resource_size(res));
710 if (!base) {
711 err = -ENOMEM;
712 goto exit_release;
713 }
714 pcdev->irq = irq;
715 pcdev->base = base;
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300716
717 /* request dma */
718 pcdev->dma_chan = imx_dma_request_by_prio(DRIVER_NAME, DMA_PRIO_HIGH);
719 if (pcdev->dma_chan < 0) {
Guennadi Liakhovetskieff505f2009-04-24 12:55:48 -0300720 dev_err(&pdev->dev, "Can't request DMA for MX1 CSI\n");
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300721 err = -EBUSY;
722 goto exit_iounmap;
723 }
Guennadi Liakhovetskieff505f2009-04-24 12:55:48 -0300724 dev_dbg(&pdev->dev, "got DMA channel %d\n", pcdev->dma_chan);
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300725
726 imx_dma_setup_handlers(pcdev->dma_chan, mx1_camera_dma_irq, NULL,
727 pcdev);
728
729 imx_dma_config_channel(pcdev->dma_chan, IMX_DMA_TYPE_FIFO,
730 IMX_DMA_MEMSIZE_32, DMA_REQ_CSI_R, 0);
731 /* burst length : 16 words = 64 bytes */
732 imx_dma_config_burstlen(pcdev->dma_chan, 0);
733
734 /* request irq */
735 err = claim_fiq(&fh);
736 if (err) {
Guennadi Liakhovetskieff505f2009-04-24 12:55:48 -0300737 dev_err(&pdev->dev, "Camera interrupt register failed \n");
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300738 goto exit_free_dma;
739 }
740
741 set_fiq_handler(&mx1_camera_sof_fiq_start, &mx1_camera_sof_fiq_end -
742 &mx1_camera_sof_fiq_start);
743
744 regs.ARM_r8 = DMA_BASE + DMA_DIMR;
745 regs.ARM_r9 = DMA_BASE + DMA_CCR(pcdev->dma_chan);
746 regs.ARM_r10 = (long)pcdev->base + CSICR1;
747 regs.ARM_fp = (long)pcdev->base + CSISR;
748 regs.ARM_sp = 1 << pcdev->dma_chan;
749 set_fiq_regs(&regs);
750
751 mxc_set_irq_fiq(irq, 1);
752 enable_fiq(irq);
753
Guennadi Liakhovetskieb6c8552009-04-24 12:55:18 -0300754 pcdev->soc_host.drv_name = DRIVER_NAME;
755 pcdev->soc_host.ops = &mx1_soc_camera_host_ops;
756 pcdev->soc_host.priv = pcdev;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300757 pcdev->soc_host.v4l2_dev.dev = &pdev->dev;
Guennadi Liakhovetskieb6c8552009-04-24 12:55:18 -0300758 pcdev->soc_host.nr = pdev->id;
759 err = soc_camera_host_register(&pcdev->soc_host);
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300760 if (err)
761 goto exit_free_irq;
762
763 dev_info(&pdev->dev, "MX1 Camera driver loaded\n");
764
765 return 0;
766
767exit_free_irq:
768 disable_fiq(irq);
769 mxc_set_irq_fiq(irq, 0);
770 release_fiq(&fh);
771exit_free_dma:
772 imx_dma_free(pcdev->dma_chan);
773exit_iounmap:
774 iounmap(base);
775exit_release:
776 release_mem_region(res->start, resource_size(res));
777exit_kfree:
778 kfree(pcdev);
779exit_put_clk:
780 clk_put(clk);
781exit:
782 return err;
783}
784
785static int __exit mx1_camera_remove(struct platform_device *pdev)
786{
Guennadi Liakhovetskieff505f2009-04-24 12:55:48 -0300787 struct soc_camera_host *soc_host = to_soc_camera_host(&pdev->dev);
788 struct mx1_camera_dev *pcdev = container_of(soc_host,
789 struct mx1_camera_dev, soc_host);
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300790 struct resource *res;
791
792 imx_dma_free(pcdev->dma_chan);
793 disable_fiq(pcdev->irq);
794 mxc_set_irq_fiq(pcdev->irq, 0);
795 release_fiq(&fh);
796
797 clk_put(pcdev->clk);
798
Guennadi Liakhovetskieff505f2009-04-24 12:55:48 -0300799 soc_camera_host_unregister(soc_host);
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300800
801 iounmap(pcdev->base);
802
803 res = pcdev->res;
804 release_mem_region(res->start, resource_size(res));
805
806 kfree(pcdev);
807
808 dev_info(&pdev->dev, "MX1 Camera driver unloaded\n");
809
810 return 0;
811}
812
813static struct platform_driver mx1_camera_driver = {
814 .driver = {
815 .name = DRIVER_NAME,
816 },
817 .remove = __exit_p(mx1_camera_remove),
818};
819
820static int __init mx1_camera_init(void)
821{
822 return platform_driver_probe(&mx1_camera_driver, mx1_camera_probe);
823}
824
825static void __exit mx1_camera_exit(void)
826{
827 return platform_driver_unregister(&mx1_camera_driver);
828}
829
830module_init(mx1_camera_init);
831module_exit(mx1_camera_exit);
832
833MODULE_DESCRIPTION("i.MX1/i.MXL SoC Camera Host driver");
834MODULE_AUTHOR("Paulius Zaleckas <paulius.zaleckas@teltonika.lt>");
835MODULE_LICENSE("GPL v2");
836MODULE_ALIAS("platform:" DRIVER_NAME);