blob: c167cc3de4928cee1f0a28b1dbade61320d1bcf7 [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>
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -030040#include <media/soc_mediabus.h>
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -030041
42#include <asm/dma.h>
43#include <asm/fiq.h>
44#include <mach/dma-mx1-mx2.h>
45#include <mach/hardware.h>
46#include <mach/mx1_camera.h>
47
48/*
49 * CSI registers
50 */
51#define DMA_CCR(x) (0x8c + ((x) << 6)) /* Control Registers */
52#define DMA_DIMR 0x08 /* Interrupt mask Register */
53#define CSICR1 0x00 /* CSI Control Register 1 */
54#define CSISR 0x08 /* CSI Status Register */
55#define CSIRXR 0x10 /* CSI RxFIFO Register */
56
57#define CSICR1_RXFF_LEVEL(x) (((x) & 0x3) << 19)
58#define CSICR1_SOF_POL (1 << 17)
59#define CSICR1_SOF_INTEN (1 << 16)
60#define CSICR1_MCLKDIV(x) (((x) & 0xf) << 12)
61#define CSICR1_MCLKEN (1 << 9)
62#define CSICR1_FCC (1 << 8)
63#define CSICR1_BIG_ENDIAN (1 << 7)
64#define CSICR1_CLR_RXFIFO (1 << 5)
65#define CSICR1_GCLK_MODE (1 << 4)
66#define CSICR1_DATA_POL (1 << 2)
67#define CSICR1_REDGE (1 << 1)
68#define CSICR1_EN (1 << 0)
69
70#define CSISR_SFF_OR_INT (1 << 25)
71#define CSISR_RFF_OR_INT (1 << 24)
72#define CSISR_STATFF_INT (1 << 21)
73#define CSISR_RXFF_INT (1 << 18)
74#define CSISR_SOF_INT (1 << 16)
75#define CSISR_DRDY (1 << 0)
76
77#define VERSION_CODE KERNEL_VERSION(0, 0, 1)
78#define DRIVER_NAME "mx1-camera"
79
80#define CSI_IRQ_MASK (CSISR_SFF_OR_INT | CSISR_RFF_OR_INT | \
81 CSISR_STATFF_INT | CSISR_RXFF_INT | CSISR_SOF_INT)
82
83#define CSI_BUS_FLAGS (SOCAM_MASTER | SOCAM_HSYNC_ACTIVE_HIGH | \
84 SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW | \
85 SOCAM_PCLK_SAMPLE_RISING | SOCAM_PCLK_SAMPLE_FALLING | \
86 SOCAM_DATA_ACTIVE_HIGH | SOCAM_DATA_ACTIVE_LOW | \
87 SOCAM_DATAWIDTH_8)
88
89#define MAX_VIDEO_MEM 16 /* Video memory limit in megabytes */
90
91/*
92 * Structures
93 */
94
95/* buffer for one video frame */
96struct mx1_buffer {
97 /* common v4l buffer stuff -- must be first */
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -030098 struct videobuf_buffer vb;
99 enum v4l2_mbus_pixelcode code;
100 int inwork;
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300101};
102
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -0300103/*
104 * i.MX1/i.MXL is only supposed to handle one camera on its Camera Sensor
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300105 * Interface. If anyone ever builds hardware to enable more than
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -0300106 * one camera, they will have to modify this driver too
107 */
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300108struct mx1_camera_dev {
Guennadi Liakhovetskieb6c8552009-04-24 12:55:18 -0300109 struct soc_camera_host soc_host;
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300110 struct soc_camera_device *icd;
111 struct mx1_camera_pdata *pdata;
112 struct mx1_buffer *active;
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300113 struct resource *res;
114 struct clk *clk;
115 struct list_head capture;
116
117 void __iomem *base;
118 int dma_chan;
119 unsigned int irq;
120 unsigned long mclk;
121
122 spinlock_t lock;
123};
124
125/*
126 * Videobuf operations
127 */
128static int mx1_videobuf_setup(struct videobuf_queue *vq, unsigned int *count,
129 unsigned int *size)
130{
131 struct soc_camera_device *icd = vq->priv_data;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300132 int bytes_per_line = soc_mbus_bytes_per_line(icd->user_width,
133 icd->current_fmt->host_fmt);
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300134
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300135 if (bytes_per_line < 0)
136 return bytes_per_line;
137
138 *size = bytes_per_line * icd->user_height;
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300139
140 if (!*count)
141 *count = 32;
142
143 while (*size * *count > MAX_VIDEO_MEM * 1024 * 1024)
144 (*count)--;
145
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300146 dev_dbg(icd->dev.parent, "count=%d, size=%d\n", *count, *size);
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300147
148 return 0;
149}
150
151static void free_buffer(struct videobuf_queue *vq, struct mx1_buffer *buf)
152{
153 struct soc_camera_device *icd = vq->priv_data;
154 struct videobuf_buffer *vb = &buf->vb;
155
156 BUG_ON(in_interrupt());
157
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300158 dev_dbg(icd->dev.parent, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300159 vb, vb->baddr, vb->bsize);
160
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -0300161 /*
162 * This waits until this buffer is out of danger, i.e., until it is no
163 * longer in STATE_QUEUED or STATE_ACTIVE
164 */
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300165 videobuf_waiton(vb, 0, 0);
166 videobuf_dma_contig_free(vq, vb);
167
168 vb->state = VIDEOBUF_NEEDS_INIT;
169}
170
171static int mx1_videobuf_prepare(struct videobuf_queue *vq,
172 struct videobuf_buffer *vb, enum v4l2_field field)
173{
174 struct soc_camera_device *icd = vq->priv_data;
175 struct mx1_buffer *buf = container_of(vb, struct mx1_buffer, vb);
176 int ret;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300177 int bytes_per_line = soc_mbus_bytes_per_line(icd->user_width,
178 icd->current_fmt->host_fmt);
179
180 if (bytes_per_line < 0)
181 return bytes_per_line;
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300182
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300183 dev_dbg(icd->dev.parent, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300184 vb, vb->baddr, vb->bsize);
185
186 /* Added list head initialization on alloc */
187 WARN_ON(!list_empty(&vb->queue));
188
189 BUG_ON(NULL == icd->current_fmt);
190
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -0300191 /*
192 * I think, in buf_prepare you only have to protect global data,
193 * the actual buffer is yours
194 */
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300195 buf->inwork = 1;
196
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300197 if (buf->code != icd->current_fmt->code ||
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300198 vb->width != icd->user_width ||
199 vb->height != icd->user_height ||
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300200 vb->field != field) {
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300201 buf->code = icd->current_fmt->code;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300202 vb->width = icd->user_width;
203 vb->height = icd->user_height;
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300204 vb->field = field;
205 vb->state = VIDEOBUF_NEEDS_INIT;
206 }
207
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300208 vb->size = bytes_per_line * vb->height;
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300209 if (0 != vb->baddr && vb->bsize < vb->size) {
210 ret = -EINVAL;
211 goto out;
212 }
213
214 if (vb->state == VIDEOBUF_NEEDS_INIT) {
215 ret = videobuf_iolock(vq, vb, NULL);
216 if (ret)
217 goto fail;
218
219 vb->state = VIDEOBUF_PREPARED;
220 }
221
222 buf->inwork = 0;
223
224 return 0;
225
226fail:
227 free_buffer(vq, buf);
228out:
229 buf->inwork = 0;
230 return ret;
231}
232
233static int mx1_camera_setup_dma(struct mx1_camera_dev *pcdev)
234{
235 struct videobuf_buffer *vbuf = &pcdev->active->vb;
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300236 struct device *dev = pcdev->icd->dev.parent;
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300237 int ret;
238
239 if (unlikely(!pcdev->active)) {
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300240 dev_err(dev, "DMA End IRQ with no active buffer\n");
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300241 return -EFAULT;
242 }
243
244 /* setup sg list for future DMA */
245 ret = imx_dma_setup_single(pcdev->dma_chan,
246 videobuf_to_dma_contig(vbuf),
247 vbuf->size, pcdev->res->start +
248 CSIRXR, DMA_MODE_READ);
249 if (unlikely(ret))
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300250 dev_err(dev, "Failed to setup DMA sg list\n");
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300251
252 return ret;
253}
254
Guennadi Liakhovetski2dd54a52009-08-05 20:06:31 -0300255/* Called under spinlock_irqsave(&pcdev->lock, ...) */
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300256static void mx1_videobuf_queue(struct videobuf_queue *vq,
257 struct videobuf_buffer *vb)
258{
259 struct soc_camera_device *icd = vq->priv_data;
260 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
261 struct mx1_camera_dev *pcdev = ici->priv;
262 struct mx1_buffer *buf = container_of(vb, struct mx1_buffer, vb);
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300263
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300264 dev_dbg(icd->dev.parent, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300265 vb, vb->baddr, vb->bsize);
266
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300267 list_add_tail(&vb->queue, &pcdev->capture);
268
269 vb->state = VIDEOBUF_ACTIVE;
270
271 if (!pcdev->active) {
272 pcdev->active = buf;
273
274 /* setup sg list for future DMA */
275 if (!mx1_camera_setup_dma(pcdev)) {
276 unsigned int temp;
277 /* enable SOF irq */
278 temp = __raw_readl(pcdev->base + CSICR1) |
279 CSICR1_SOF_INTEN;
280 __raw_writel(temp, pcdev->base + CSICR1);
281 }
282 }
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300283}
284
285static void mx1_videobuf_release(struct videobuf_queue *vq,
286 struct videobuf_buffer *vb)
287{
288 struct mx1_buffer *buf = container_of(vb, struct mx1_buffer, vb);
289#ifdef DEBUG
290 struct soc_camera_device *icd = vq->priv_data;
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300291 struct device *dev = icd->dev.parent;
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300292
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300293 dev_dbg(dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300294 vb, vb->baddr, vb->bsize);
295
296 switch (vb->state) {
297 case VIDEOBUF_ACTIVE:
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300298 dev_dbg(dev, "%s (active)\n", __func__);
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300299 break;
300 case VIDEOBUF_QUEUED:
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300301 dev_dbg(dev, "%s (queued)\n", __func__);
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300302 break;
303 case VIDEOBUF_PREPARED:
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300304 dev_dbg(dev, "%s (prepared)\n", __func__);
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300305 break;
306 default:
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300307 dev_dbg(dev, "%s (unknown)\n", __func__);
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300308 break;
309 }
310#endif
311
312 free_buffer(vq, buf);
313}
314
315static void mx1_camera_wakeup(struct mx1_camera_dev *pcdev,
316 struct videobuf_buffer *vb,
317 struct mx1_buffer *buf)
318{
319 /* _init is used to debug races, see comment in mx1_camera_reqbufs() */
320 list_del_init(&vb->queue);
321 vb->state = VIDEOBUF_DONE;
322 do_gettimeofday(&vb->ts);
323 vb->field_count++;
324 wake_up(&vb->done);
325
326 if (list_empty(&pcdev->capture)) {
327 pcdev->active = NULL;
328 return;
329 }
330
331 pcdev->active = list_entry(pcdev->capture.next,
332 struct mx1_buffer, vb.queue);
333
334 /* setup sg list for future DMA */
335 if (likely(!mx1_camera_setup_dma(pcdev))) {
336 unsigned int temp;
337
338 /* enable SOF irq */
339 temp = __raw_readl(pcdev->base + CSICR1) | CSICR1_SOF_INTEN;
340 __raw_writel(temp, pcdev->base + CSICR1);
341 }
342}
343
344static void mx1_camera_dma_irq(int channel, void *data)
345{
346 struct mx1_camera_dev *pcdev = data;
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300347 struct device *dev = pcdev->icd->dev.parent;
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300348 struct mx1_buffer *buf;
349 struct videobuf_buffer *vb;
350 unsigned long flags;
351
352 spin_lock_irqsave(&pcdev->lock, flags);
353
354 imx_dma_disable(channel);
355
356 if (unlikely(!pcdev->active)) {
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300357 dev_err(dev, "DMA End IRQ with no active buffer\n");
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300358 goto out;
359 }
360
361 vb = &pcdev->active->vb;
362 buf = container_of(vb, struct mx1_buffer, vb);
363 WARN_ON(buf->inwork || list_empty(&vb->queue));
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300364 dev_dbg(dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300365 vb, vb->baddr, vb->bsize);
366
367 mx1_camera_wakeup(pcdev, vb, buf);
368out:
369 spin_unlock_irqrestore(&pcdev->lock, flags);
370}
371
372static struct videobuf_queue_ops mx1_videobuf_ops = {
373 .buf_setup = mx1_videobuf_setup,
374 .buf_prepare = mx1_videobuf_prepare,
375 .buf_queue = mx1_videobuf_queue,
376 .buf_release = mx1_videobuf_release,
377};
378
379static void mx1_camera_init_videobuf(struct videobuf_queue *q,
380 struct soc_camera_device *icd)
381{
382 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
383 struct mx1_camera_dev *pcdev = ici->priv;
384
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300385 videobuf_queue_dma_contig_init(q, &mx1_videobuf_ops, icd->dev.parent,
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300386 &pcdev->lock,
387 V4L2_BUF_TYPE_VIDEO_CAPTURE,
388 V4L2_FIELD_NONE,
389 sizeof(struct mx1_buffer), icd);
390}
391
392static int mclk_get_divisor(struct mx1_camera_dev *pcdev)
393{
394 unsigned int mclk = pcdev->mclk;
395 unsigned long div;
396 unsigned long lcdclk;
397
398 lcdclk = clk_get_rate(pcdev->clk);
399
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -0300400 /*
401 * We verify platform_mclk_10khz != 0, so if anyone breaks it, here
402 * they get a nice Oops
403 */
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300404 div = (lcdclk + 2 * mclk - 1) / (2 * mclk) - 1;
405
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300406 dev_dbg(pcdev->icd->dev.parent,
407 "System clock %lukHz, target freq %dkHz, divisor %lu\n",
408 lcdclk / 1000, mclk / 1000, div);
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300409
410 return div;
411}
412
413static void mx1_camera_activate(struct mx1_camera_dev *pcdev)
414{
415 unsigned int csicr1 = CSICR1_EN;
416
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300417 dev_dbg(pcdev->icd->dev.parent, "Activate device\n");
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300418
419 clk_enable(pcdev->clk);
420
421 /* enable CSI before doing anything else */
422 __raw_writel(csicr1, pcdev->base + CSICR1);
423
424 csicr1 |= CSICR1_MCLKEN | CSICR1_FCC | CSICR1_GCLK_MODE;
425 csicr1 |= CSICR1_MCLKDIV(mclk_get_divisor(pcdev));
426 csicr1 |= CSICR1_RXFF_LEVEL(2); /* 16 words */
427
428 __raw_writel(csicr1, pcdev->base + CSICR1);
429}
430
431static void mx1_camera_deactivate(struct mx1_camera_dev *pcdev)
432{
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300433 dev_dbg(pcdev->icd->dev.parent, "Deactivate device\n");
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300434
435 /* Disable all CSI interface */
436 __raw_writel(0x00, pcdev->base + CSICR1);
437
438 clk_disable(pcdev->clk);
439}
440
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -0300441/*
442 * The following two functions absolutely depend on the fact, that
443 * there can be only one camera on i.MX1/i.MXL camera sensor interface
444 */
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300445static int mx1_camera_add_device(struct soc_camera_device *icd)
446{
447 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
448 struct mx1_camera_dev *pcdev = ici->priv;
449 int ret;
450
451 if (pcdev->icd) {
452 ret = -EBUSY;
453 goto ebusy;
454 }
455
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300456 dev_info(icd->dev.parent, "MX1 Camera driver attached to camera %d\n",
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300457 icd->devnum);
458
459 mx1_camera_activate(pcdev);
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300460
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300461 pcdev->icd = icd;
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300462
463ebusy:
464 return ret;
465}
466
467static void mx1_camera_remove_device(struct soc_camera_device *icd)
468{
469 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
470 struct mx1_camera_dev *pcdev = ici->priv;
471 unsigned int csicr1;
472
473 BUG_ON(icd != pcdev->icd);
474
475 /* disable interrupts */
476 csicr1 = __raw_readl(pcdev->base + CSICR1) & ~CSI_IRQ_MASK;
477 __raw_writel(csicr1, pcdev->base + CSICR1);
478
479 /* Stop DMA engine */
480 imx_dma_disable(pcdev->dma_chan);
481
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300482 dev_info(icd->dev.parent, "MX1 Camera driver detached from camera %d\n",
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300483 icd->devnum);
484
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300485 mx1_camera_deactivate(pcdev);
486
487 pcdev->icd = NULL;
488}
489
490static int mx1_camera_set_crop(struct soc_camera_device *icd,
Guennadi Liakhovetski08590b92009-08-25 11:46:54 -0300491 struct v4l2_crop *a)
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300492{
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -0300493 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
Guennadi Liakhovetski08590b92009-08-25 11:46:54 -0300494
495 return v4l2_subdev_call(sd, video, s_crop, a);
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300496}
497
498static int mx1_camera_set_bus_param(struct soc_camera_device *icd, __u32 pixfmt)
499{
500 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
501 struct mx1_camera_dev *pcdev = ici->priv;
502 unsigned long camera_flags, common_flags;
503 unsigned int csicr1;
504 int ret;
505
506 camera_flags = icd->ops->query_bus_param(icd);
507
508 /* MX1 supports only 8bit buswidth */
509 common_flags = soc_camera_bus_param_compatible(camera_flags,
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300510 CSI_BUS_FLAGS);
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300511 if (!common_flags)
512 return -EINVAL;
513
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300514 /* Make choises, based on platform choice */
515 if ((common_flags & SOCAM_VSYNC_ACTIVE_HIGH) &&
516 (common_flags & SOCAM_VSYNC_ACTIVE_LOW)) {
517 if (!pcdev->pdata ||
518 pcdev->pdata->flags & MX1_CAMERA_VSYNC_HIGH)
519 common_flags &= ~SOCAM_VSYNC_ACTIVE_LOW;
520 else
521 common_flags &= ~SOCAM_VSYNC_ACTIVE_HIGH;
522 }
523
524 if ((common_flags & SOCAM_PCLK_SAMPLE_RISING) &&
525 (common_flags & SOCAM_PCLK_SAMPLE_FALLING)) {
526 if (!pcdev->pdata ||
527 pcdev->pdata->flags & MX1_CAMERA_PCLK_RISING)
528 common_flags &= ~SOCAM_PCLK_SAMPLE_FALLING;
529 else
530 common_flags &= ~SOCAM_PCLK_SAMPLE_RISING;
531 }
532
533 if ((common_flags & SOCAM_DATA_ACTIVE_HIGH) &&
534 (common_flags & SOCAM_DATA_ACTIVE_LOW)) {
535 if (!pcdev->pdata ||
536 pcdev->pdata->flags & MX1_CAMERA_DATA_HIGH)
537 common_flags &= ~SOCAM_DATA_ACTIVE_LOW;
538 else
539 common_flags &= ~SOCAM_DATA_ACTIVE_HIGH;
540 }
541
542 ret = icd->ops->set_bus_param(icd, common_flags);
543 if (ret < 0)
544 return ret;
545
546 csicr1 = __raw_readl(pcdev->base + CSICR1);
547
548 if (common_flags & SOCAM_PCLK_SAMPLE_RISING)
549 csicr1 |= CSICR1_REDGE;
550 if (common_flags & SOCAM_VSYNC_ACTIVE_HIGH)
551 csicr1 |= CSICR1_SOF_POL;
552 if (common_flags & SOCAM_DATA_ACTIVE_LOW)
553 csicr1 |= CSICR1_DATA_POL;
554
555 __raw_writel(csicr1, pcdev->base + CSICR1);
556
557 return 0;
558}
559
560static int mx1_camera_set_fmt(struct soc_camera_device *icd,
561 struct v4l2_format *f)
562{
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -0300563 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300564 const struct soc_camera_format_xlate *xlate;
565 struct v4l2_pix_format *pix = &f->fmt.pix;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300566 struct v4l2_mbus_framefmt mf;
567 int ret, buswidth;
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300568
569 xlate = soc_camera_xlate_by_fourcc(icd, pix->pixelformat);
570 if (!xlate) {
Guennadi Liakhovetski96c75392009-08-25 11:53:23 -0300571 dev_warn(icd->dev.parent, "Format %x not found\n",
572 pix->pixelformat);
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300573 return -EINVAL;
574 }
575
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300576 buswidth = xlate->host_fmt->bits_per_sample;
577 if (buswidth > 8) {
578 dev_warn(icd->dev.parent,
579 "bits-per-sample %d for format %x unsupported\n",
580 buswidth, pix->pixelformat);
581 return -EINVAL;
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300582 }
583
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300584 mf.width = pix->width;
585 mf.height = pix->height;
586 mf.field = pix->field;
587 mf.colorspace = pix->colorspace;
588 mf.code = xlate->code;
589
590 ret = v4l2_subdev_call(sd, video, s_mbus_fmt, &mf);
591 if (ret < 0)
592 return ret;
593
594 if (mf.code != xlate->code)
595 return -EINVAL;
596
597 pix->width = mf.width;
598 pix->height = mf.height;
599 pix->field = mf.field;
600 pix->colorspace = mf.colorspace;
601 icd->current_fmt = xlate;
602
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300603 return ret;
604}
605
606static int mx1_camera_try_fmt(struct soc_camera_device *icd,
607 struct v4l2_format *f)
608{
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -0300609 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300610 const struct soc_camera_format_xlate *xlate;
611 struct v4l2_pix_format *pix = &f->fmt.pix;
612 struct v4l2_mbus_framefmt mf;
613 int ret;
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300614 /* TODO: limit to mx1 hardware capabilities */
615
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300616 xlate = soc_camera_xlate_by_fourcc(icd, pix->pixelformat);
617 if (!xlate) {
618 dev_warn(icd->dev.parent, "Format %x not found\n",
619 pix->pixelformat);
620 return -EINVAL;
621 }
622
623 mf.width = pix->width;
624 mf.height = pix->height;
625 mf.field = pix->field;
626 mf.colorspace = pix->colorspace;
627 mf.code = xlate->code;
628
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300629 /* limit to sensor capabilities */
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300630 ret = v4l2_subdev_call(sd, video, try_mbus_fmt, &mf);
631 if (ret < 0)
632 return ret;
633
634 pix->width = mf.width;
635 pix->height = mf.height;
636 pix->field = mf.field;
637 pix->colorspace = mf.colorspace;
638
639 return 0;
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300640}
641
642static int mx1_camera_reqbufs(struct soc_camera_file *icf,
643 struct v4l2_requestbuffers *p)
644{
645 int i;
646
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -0300647 /*
648 * This is for locking debugging only. I removed spinlocks and now I
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300649 * check whether .prepare is ever called on a linked buffer, or whether
650 * a dma IRQ can occur for an in-work or unlinked buffer. Until now
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -0300651 * it hadn't triggered
652 */
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300653 for (i = 0; i < p->count; i++) {
654 struct mx1_buffer *buf = container_of(icf->vb_vidq.bufs[i],
655 struct mx1_buffer, vb);
656 buf->inwork = 0;
657 INIT_LIST_HEAD(&buf->vb.queue);
658 }
659
660 return 0;
661}
662
663static unsigned int mx1_camera_poll(struct file *file, poll_table *pt)
664{
665 struct soc_camera_file *icf = file->private_data;
666 struct mx1_buffer *buf;
667
668 buf = list_entry(icf->vb_vidq.stream.next, struct mx1_buffer,
669 vb.stream);
670
671 poll_wait(file, &buf->vb.done, pt);
672
673 if (buf->vb.state == VIDEOBUF_DONE ||
674 buf->vb.state == VIDEOBUF_ERROR)
675 return POLLIN | POLLRDNORM;
676
677 return 0;
678}
679
680static int mx1_camera_querycap(struct soc_camera_host *ici,
681 struct v4l2_capability *cap)
682{
683 /* cap->name is set by the friendly caller:-> */
684 strlcpy(cap->card, "i.MX1/i.MXL Camera", sizeof(cap->card));
685 cap->version = VERSION_CODE;
686 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
687
688 return 0;
689}
690
691static struct soc_camera_host_ops mx1_soc_camera_host_ops = {
692 .owner = THIS_MODULE,
693 .add = mx1_camera_add_device,
694 .remove = mx1_camera_remove_device,
695 .set_bus_param = mx1_camera_set_bus_param,
696 .set_crop = mx1_camera_set_crop,
697 .set_fmt = mx1_camera_set_fmt,
698 .try_fmt = mx1_camera_try_fmt,
699 .init_videobuf = mx1_camera_init_videobuf,
700 .reqbufs = mx1_camera_reqbufs,
701 .poll = mx1_camera_poll,
702 .querycap = mx1_camera_querycap,
703};
704
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300705static struct fiq_handler fh = {
706 .name = "csi_sof"
707};
708
709static int __init mx1_camera_probe(struct platform_device *pdev)
710{
711 struct mx1_camera_dev *pcdev;
712 struct resource *res;
713 struct pt_regs regs;
714 struct clk *clk;
715 void __iomem *base;
716 unsigned int irq;
717 int err = 0;
718
719 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
720 irq = platform_get_irq(pdev, 0);
Uwe Kleine-König30883ea2010-01-09 20:44:06 -0300721 if (!res || (int)irq <= 0) {
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300722 err = -ENODEV;
723 goto exit;
724 }
725
726 clk = clk_get(&pdev->dev, "csi_clk");
727 if (IS_ERR(clk)) {
728 err = PTR_ERR(clk);
729 goto exit;
730 }
731
732 pcdev = kzalloc(sizeof(*pcdev), GFP_KERNEL);
733 if (!pcdev) {
734 dev_err(&pdev->dev, "Could not allocate pcdev\n");
735 err = -ENOMEM;
736 goto exit_put_clk;
737 }
738
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300739 pcdev->res = res;
740 pcdev->clk = clk;
741
742 pcdev->pdata = pdev->dev.platform_data;
743
744 if (pcdev->pdata)
745 pcdev->mclk = pcdev->pdata->mclk_10khz * 10000;
746
747 if (!pcdev->mclk) {
748 dev_warn(&pdev->dev,
749 "mclk_10khz == 0! Please, fix your platform data. "
750 "Using default 20MHz\n");
751 pcdev->mclk = 20000000;
752 }
753
754 INIT_LIST_HEAD(&pcdev->capture);
755 spin_lock_init(&pcdev->lock);
756
757 /*
758 * Request the regions.
759 */
760 if (!request_mem_region(res->start, resource_size(res), DRIVER_NAME)) {
761 err = -EBUSY;
762 goto exit_kfree;
763 }
764
765 base = ioremap(res->start, resource_size(res));
766 if (!base) {
767 err = -ENOMEM;
768 goto exit_release;
769 }
770 pcdev->irq = irq;
771 pcdev->base = base;
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300772
773 /* request dma */
774 pcdev->dma_chan = imx_dma_request_by_prio(DRIVER_NAME, DMA_PRIO_HIGH);
775 if (pcdev->dma_chan < 0) {
Guennadi Liakhovetskieff505f2009-04-24 12:55:48 -0300776 dev_err(&pdev->dev, "Can't request DMA for MX1 CSI\n");
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300777 err = -EBUSY;
778 goto exit_iounmap;
779 }
Guennadi Liakhovetskieff505f2009-04-24 12:55:48 -0300780 dev_dbg(&pdev->dev, "got DMA channel %d\n", pcdev->dma_chan);
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300781
782 imx_dma_setup_handlers(pcdev->dma_chan, mx1_camera_dma_irq, NULL,
783 pcdev);
784
785 imx_dma_config_channel(pcdev->dma_chan, IMX_DMA_TYPE_FIFO,
786 IMX_DMA_MEMSIZE_32, DMA_REQ_CSI_R, 0);
787 /* burst length : 16 words = 64 bytes */
788 imx_dma_config_burstlen(pcdev->dma_chan, 0);
789
790 /* request irq */
791 err = claim_fiq(&fh);
792 if (err) {
Guennadi Liakhovetskieff505f2009-04-24 12:55:48 -0300793 dev_err(&pdev->dev, "Camera interrupt register failed \n");
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300794 goto exit_free_dma;
795 }
796
797 set_fiq_handler(&mx1_camera_sof_fiq_start, &mx1_camera_sof_fiq_end -
798 &mx1_camera_sof_fiq_start);
799
800 regs.ARM_r8 = DMA_BASE + DMA_DIMR;
801 regs.ARM_r9 = DMA_BASE + DMA_CCR(pcdev->dma_chan);
802 regs.ARM_r10 = (long)pcdev->base + CSICR1;
803 regs.ARM_fp = (long)pcdev->base + CSISR;
804 regs.ARM_sp = 1 << pcdev->dma_chan;
805 set_fiq_regs(&regs);
806
807 mxc_set_irq_fiq(irq, 1);
808 enable_fiq(irq);
809
Guennadi Liakhovetskieb6c8552009-04-24 12:55:18 -0300810 pcdev->soc_host.drv_name = DRIVER_NAME;
811 pcdev->soc_host.ops = &mx1_soc_camera_host_ops;
812 pcdev->soc_host.priv = pcdev;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300813 pcdev->soc_host.v4l2_dev.dev = &pdev->dev;
Guennadi Liakhovetskieb6c8552009-04-24 12:55:18 -0300814 pcdev->soc_host.nr = pdev->id;
815 err = soc_camera_host_register(&pcdev->soc_host);
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300816 if (err)
817 goto exit_free_irq;
818
819 dev_info(&pdev->dev, "MX1 Camera driver loaded\n");
820
821 return 0;
822
823exit_free_irq:
824 disable_fiq(irq);
825 mxc_set_irq_fiq(irq, 0);
826 release_fiq(&fh);
827exit_free_dma:
828 imx_dma_free(pcdev->dma_chan);
829exit_iounmap:
830 iounmap(base);
831exit_release:
832 release_mem_region(res->start, resource_size(res));
833exit_kfree:
834 kfree(pcdev);
835exit_put_clk:
836 clk_put(clk);
837exit:
838 return err;
839}
840
841static int __exit mx1_camera_remove(struct platform_device *pdev)
842{
Guennadi Liakhovetskieff505f2009-04-24 12:55:48 -0300843 struct soc_camera_host *soc_host = to_soc_camera_host(&pdev->dev);
844 struct mx1_camera_dev *pcdev = container_of(soc_host,
845 struct mx1_camera_dev, soc_host);
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300846 struct resource *res;
847
848 imx_dma_free(pcdev->dma_chan);
849 disable_fiq(pcdev->irq);
850 mxc_set_irq_fiq(pcdev->irq, 0);
851 release_fiq(&fh);
852
853 clk_put(pcdev->clk);
854
Guennadi Liakhovetskieff505f2009-04-24 12:55:48 -0300855 soc_camera_host_unregister(soc_host);
Paulius Zaleckas6acc81c2009-04-03 10:34:05 -0300856
857 iounmap(pcdev->base);
858
859 res = pcdev->res;
860 release_mem_region(res->start, resource_size(res));
861
862 kfree(pcdev);
863
864 dev_info(&pdev->dev, "MX1 Camera driver unloaded\n");
865
866 return 0;
867}
868
869static struct platform_driver mx1_camera_driver = {
870 .driver = {
871 .name = DRIVER_NAME,
872 },
873 .remove = __exit_p(mx1_camera_remove),
874};
875
876static int __init mx1_camera_init(void)
877{
878 return platform_driver_probe(&mx1_camera_driver, mx1_camera_probe);
879}
880
881static void __exit mx1_camera_exit(void)
882{
883 return platform_driver_unregister(&mx1_camera_driver);
884}
885
886module_init(mx1_camera_init);
887module_exit(mx1_camera_exit);
888
889MODULE_DESCRIPTION("i.MX1/i.MXL SoC Camera Host driver");
890MODULE_AUTHOR("Paulius Zaleckas <paulius.zaleckas@teltonika.lt>");
891MODULE_LICENSE("GPL v2");
892MODULE_ALIAS("platform:" DRIVER_NAME);