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