blob: a34a193249f6ca6486bd832e41fd50eae26a9989 [file] [log] [blame]
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001/*
2 * V4L2 Driver for PXA camera host
3 *
4 * Copyright (C) 2006, Sascha Hauer, Pengutronix
5 * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 */
12
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -030013#include <linux/init.h>
14#include <linux/module.h>
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -030015#include <linux/io.h>
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -030016#include <linux/delay.h>
17#include <linux/dma-mapping.h>
18#include <linux/errno.h>
19#include <linux/fs.h>
20#include <linux/interrupt.h>
21#include <linux/kernel.h>
22#include <linux/mm.h>
23#include <linux/moduleparam.h>
24#include <linux/time.h>
25#include <linux/version.h>
26#include <linux/device.h>
27#include <linux/platform_device.h>
28#include <linux/mutex.h>
29#include <linux/clk.h>
30
31#include <media/v4l2-common.h>
32#include <media/v4l2-dev.h>
33#include <media/soc_camera.h>
34
35#include <linux/videodev2.h>
36
37#include <asm/dma.h>
38#include <asm/arch/pxa-regs.h>
39#include <asm/arch/camera.h>
40
41#define PXA_CAM_VERSION_CODE KERNEL_VERSION(0, 0, 5)
42#define PXA_CAM_DRV_NAME "pxa27x-camera"
43
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -030044#define CICR0_SIM_MP (0 << 24)
45#define CICR0_SIM_SP (1 << 24)
46#define CICR0_SIM_MS (2 << 24)
47#define CICR0_SIM_EP (3 << 24)
48#define CICR0_SIM_ES (4 << 24)
49
50#define CICR1_DW_VAL(x) ((x) & CICR1_DW) /* Data bus width */
51#define CICR1_PPL_VAL(x) (((x) << 15) & CICR1_PPL) /* Pixels per line */
52
53#define CICR2_BLW_VAL(x) (((x) << 24) & CICR2_BLW) /* Beginning-of-line pixel clock wait count */
54#define CICR2_ELW_VAL(x) (((x) << 16) & CICR2_ELW) /* End-of-line pixel clock wait count */
55#define CICR2_HSW_VAL(x) (((x) << 10) & CICR2_HSW) /* Horizontal sync pulse width */
56#define CICR2_BFPW_VAL(x) (((x) << 3) & CICR2_BFPW) /* Beginning-of-frame pixel clock wait count */
57#define CICR2_FSW_VAL(x) (((x) << 0) & CICR2_FSW) /* Frame stabilization wait count */
58
59#define CICR3_BFW_VAL(x) (((x) << 24) & CICR3_BFW) /* Beginning-of-frame line clock wait count */
60#define CICR3_EFW_VAL(x) (((x) << 16) & CICR3_EFW) /* End-of-frame line clock wait count */
61#define CICR3_VSW_VAL(x) (((x) << 11) & CICR3_VSW) /* Vertical sync pulse width */
62#define CICR3_LPF_VAL(x) (((x) << 0) & CICR3_LPF) /* Lines per frame */
63
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -030064#define CICR0_IRQ_MASK (CICR0_TOM | CICR0_RDAVM | CICR0_FEM | CICR0_EOLM | \
65 CICR0_PERRM | CICR0_QDM | CICR0_CDM | CICR0_SOFM | \
66 CICR0_EOFM | CICR0_FOM)
67
68static DEFINE_MUTEX(camera_lock);
69
70/*
71 * Structures
72 */
73
74/* buffer for one video frame */
75struct pxa_buffer {
76 /* common v4l buffer stuff -- must be first */
77 struct videobuf_buffer vb;
78
79 const struct soc_camera_data_format *fmt;
80
81 /* our descriptor list needed for the PXA DMA engine */
82 dma_addr_t sg_dma;
83 struct pxa_dma_desc *sg_cpu;
84 size_t sg_size;
85 int inwork;
86};
87
88struct pxa_framebuffer_queue {
89 dma_addr_t sg_last_dma;
90 struct pxa_dma_desc *sg_last_cpu;
91};
92
93struct pxa_camera_dev {
94 struct device *dev;
95 /* PXA27x is only supposed to handle one camera on its Quick Capture
96 * interface. If anyone ever builds hardware to enable more than
97 * one camera, they will have to modify this driver too */
98 struct soc_camera_device *icd;
99 struct clk *clk;
100
101 unsigned int irq;
102 void __iomem *base;
103 unsigned int dma_chan_y;
104
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300105 struct pxacamera_platform_data *pdata;
106 struct resource *res;
107 unsigned long platform_flags;
108 unsigned long platform_mclk_10khz;
109
110 struct list_head capture;
111
112 spinlock_t lock;
113
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300114 struct pxa_buffer *active;
115};
116
117static const char *pxa_cam_driver_description = "PXA_Camera";
118
119static unsigned int vid_limit = 16; /* Video memory limit, in Mb */
120
121/*
122 * Videobuf operations
123 */
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300124static int pxa_videobuf_setup(struct videobuf_queue *vq, unsigned int *count,
125 unsigned int *size)
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300126{
127 struct soc_camera_device *icd = vq->priv_data;
128
129 dev_dbg(&icd->dev, "count=%d, size=%d\n", *count, *size);
130
131 *size = icd->width * icd->height * ((icd->current_fmt->depth + 7) >> 3);
132
133 if (0 == *count)
134 *count = 32;
135 while (*size * *count > vid_limit * 1024 * 1024)
136 (*count)--;
137
138 return 0;
139}
140
141static void free_buffer(struct videobuf_queue *vq, struct pxa_buffer *buf)
142{
143 struct soc_camera_device *icd = vq->priv_data;
144 struct soc_camera_host *ici =
145 to_soc_camera_host(icd->dev.parent);
146 struct pxa_camera_dev *pcdev = ici->priv;
147 struct videobuf_dmabuf *dma = videobuf_to_dma(&buf->vb);
148
149 BUG_ON(in_interrupt());
150
151 dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %d\n", __FUNCTION__,
152 &buf->vb, buf->vb.baddr, buf->vb.bsize);
153
154 /* This waits until this buffer is out of danger, i.e., until it is no
155 * longer in STATE_QUEUED or STATE_ACTIVE */
156 videobuf_waiton(&buf->vb, 0, 0);
157 videobuf_dma_unmap(vq, dma);
158 videobuf_dma_free(dma);
159
160 if (buf->sg_cpu)
161 dma_free_coherent(pcdev->dev, buf->sg_size, buf->sg_cpu,
162 buf->sg_dma);
163 buf->sg_cpu = NULL;
164
165 buf->vb.state = VIDEOBUF_NEEDS_INIT;
166}
167
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300168static int pxa_videobuf_prepare(struct videobuf_queue *vq,
169 struct videobuf_buffer *vb, enum v4l2_field field)
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300170{
171 struct soc_camera_device *icd = vq->priv_data;
172 struct soc_camera_host *ici =
173 to_soc_camera_host(icd->dev.parent);
174 struct pxa_camera_dev *pcdev = ici->priv;
175 struct pxa_buffer *buf = container_of(vb, struct pxa_buffer, vb);
176 int i, ret;
177
178 dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %d\n", __FUNCTION__,
179 vb, vb->baddr, vb->bsize);
180
181 /* Added list head initialization on alloc */
182 WARN_ON(!list_empty(&vb->queue));
183
184#ifdef DEBUG
185 /* This can be useful if you want to see if we actually fill
186 * the buffer with something */
187 memset((void *)vb->baddr, 0xaa, vb->bsize);
188#endif
189
190 BUG_ON(NULL == icd->current_fmt);
191
192 /* I think, in buf_prepare you only have to protect global data,
193 * the actual buffer is yours */
194 buf->inwork = 1;
195
196 if (buf->fmt != icd->current_fmt ||
197 vb->width != icd->width ||
198 vb->height != icd->height ||
199 vb->field != field) {
200 buf->fmt = icd->current_fmt;
201 vb->width = icd->width;
202 vb->height = icd->height;
203 vb->field = field;
204 vb->state = VIDEOBUF_NEEDS_INIT;
205 }
206
207 vb->size = vb->width * vb->height * ((buf->fmt->depth + 7) >> 3);
208 if (0 != vb->baddr && vb->bsize < vb->size) {
209 ret = -EINVAL;
210 goto out;
211 }
212
213 if (vb->state == VIDEOBUF_NEEDS_INIT) {
214 unsigned int size = vb->size;
215 struct videobuf_dmabuf *dma = videobuf_to_dma(vb);
216
217 ret = videobuf_iolock(vq, vb, NULL);
218 if (ret)
219 goto fail;
220
221 if (buf->sg_cpu)
222 dma_free_coherent(pcdev->dev, buf->sg_size, buf->sg_cpu,
223 buf->sg_dma);
224
225 buf->sg_size = (dma->sglen + 1) * sizeof(struct pxa_dma_desc);
226 buf->sg_cpu = dma_alloc_coherent(pcdev->dev, buf->sg_size,
227 &buf->sg_dma, GFP_KERNEL);
228 if (!buf->sg_cpu) {
229 ret = -ENOMEM;
230 goto fail;
231 }
232
233 dev_dbg(&icd->dev, "nents=%d size: %d sg=0x%p\n",
234 dma->sglen, size, dma->sglist);
235 for (i = 0; i < dma->sglen; i++) {
236 struct scatterlist *sg = dma->sglist;
237 unsigned int dma_len = sg_dma_len(&sg[i]), xfer_len;
238
239 /* CIBR0 */
240 buf->sg_cpu[i].dsadr = pcdev->res->start + 0x28;
241 buf->sg_cpu[i].dtadr = sg_dma_address(&sg[i]);
242 /* PXA270 Developer's Manual 27.4.4.1:
243 * round up to 8 bytes */
244 xfer_len = (min(dma_len, size) + 7) & ~7;
245 if (xfer_len & 7)
246 dev_err(&icd->dev, "Unaligned buffer: "
247 "dma_len %u, size %u\n", dma_len, size);
248 buf->sg_cpu[i].dcmd = DCMD_FLOWSRC | DCMD_BURST8 |
249 DCMD_INCTRGADDR | xfer_len;
250 size -= dma_len;
251 buf->sg_cpu[i].ddadr = buf->sg_dma + (i + 1) *
252 sizeof(struct pxa_dma_desc);
253 }
254 buf->sg_cpu[dma->sglen - 1].ddadr = DDADR_STOP;
255 buf->sg_cpu[dma->sglen - 1].dcmd |= DCMD_ENDIRQEN;
256
257 vb->state = VIDEOBUF_PREPARED;
258 }
259
260 buf->inwork = 0;
261
262 return 0;
263
264fail:
265 free_buffer(vq, buf);
266out:
267 buf->inwork = 0;
268 return ret;
269}
270
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300271static void pxa_videobuf_queue(struct videobuf_queue *vq,
272 struct videobuf_buffer *vb)
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300273{
274 struct soc_camera_device *icd = vq->priv_data;
275 struct soc_camera_host *ici =
276 to_soc_camera_host(icd->dev.parent);
277 struct pxa_camera_dev *pcdev = ici->priv;
278 struct pxa_buffer *buf = container_of(vb, struct pxa_buffer, vb);
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300279 struct pxa_buffer *active;
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300280 struct videobuf_dmabuf *dma = videobuf_to_dma(vb);
281 int nents = dma->sglen;
282 unsigned long flags;
283
284 dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %d\n", __FUNCTION__,
285 vb, vb->baddr, vb->bsize);
286 spin_lock_irqsave(&pcdev->lock, flags);
287
288 list_add_tail(&vb->queue, &pcdev->capture);
289
290 vb->state = VIDEOBUF_ACTIVE;
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300291 active = pcdev->active;
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300292
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300293 if (!active) {
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300294 CIFR |= CIFR_RESET_F;
295 DDADR(pcdev->dma_chan_y) = buf->sg_dma;
296 DCSR(pcdev->dma_chan_y) = DCSR_RUN;
297 pcdev->active = buf;
298 CICR0 |= CICR0_ENB;
299 } else {
300 struct videobuf_dmabuf *active_dma =
301 videobuf_to_dma(&active->vb);
302 /* Stop DMA engine */
303 DCSR(pcdev->dma_chan_y) = 0;
304
305 /* Add the descriptors we just initialized to the currently
306 * running chain
307 */
308 active->sg_cpu[active_dma->sglen - 1].ddadr = buf->sg_dma;
309
310 /* Setup a dummy descriptor with the DMA engines current
311 * state
312 */
313 /* CIBR0 */
314 buf->sg_cpu[nents].dsadr = pcdev->res->start + 0x28;
315 buf->sg_cpu[nents].dtadr = DTADR(pcdev->dma_chan_y);
316 buf->sg_cpu[nents].dcmd = DCMD(pcdev->dma_chan_y);
317
318 if (DDADR(pcdev->dma_chan_y) == DDADR_STOP) {
319 /* The DMA engine is on the last descriptor, set the
320 * next descriptors address to the descriptors
321 * we just initialized
322 */
323 buf->sg_cpu[nents].ddadr = buf->sg_dma;
324 } else {
325 buf->sg_cpu[nents].ddadr = DDADR(pcdev->dma_chan_y);
326 }
327
328 /* The next descriptor is the dummy descriptor */
329 DDADR(pcdev->dma_chan_y) = buf->sg_dma + nents *
330 sizeof(struct pxa_dma_desc);
331
332#ifdef DEBUG
333 if (CISR & CISR_IFO_0) {
334 dev_warn(pcdev->dev, "FIFO overrun\n");
335 DDADR(pcdev->dma_chan_y) = pcdev->active->sg_dma;
336
337 CICR0 &= ~CICR0_ENB;
338 CIFR |= CIFR_RESET_F;
339 DCSR(pcdev->dma_chan_y) = DCSR_RUN;
340 CICR0 |= CICR0_ENB;
341 } else
342#endif
343 DCSR(pcdev->dma_chan_y) = DCSR_RUN;
344 }
345
346 spin_unlock_irqrestore(&pcdev->lock, flags);
347
348}
349
350static void pxa_videobuf_release(struct videobuf_queue *vq,
351 struct videobuf_buffer *vb)
352{
353 struct pxa_buffer *buf = container_of(vb, struct pxa_buffer, vb);
354#ifdef DEBUG
355 struct soc_camera_device *icd = vq->priv_data;
356
357 dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %d\n", __FUNCTION__,
358 vb, vb->baddr, vb->bsize);
359
360 switch (vb->state) {
361 case VIDEOBUF_ACTIVE:
362 dev_dbg(&icd->dev, "%s (active)\n", __FUNCTION__);
363 break;
364 case VIDEOBUF_QUEUED:
365 dev_dbg(&icd->dev, "%s (queued)\n", __FUNCTION__);
366 break;
367 case VIDEOBUF_PREPARED:
368 dev_dbg(&icd->dev, "%s (prepared)\n", __FUNCTION__);
369 break;
370 default:
371 dev_dbg(&icd->dev, "%s (unknown)\n", __FUNCTION__);
372 break;
373 }
374#endif
375
376 free_buffer(vq, buf);
377}
378
379static void pxa_camera_dma_irq_y(int channel, void *data)
380{
381 struct pxa_camera_dev *pcdev = data;
382 struct pxa_buffer *buf;
383 unsigned long flags;
384 unsigned int status;
385 struct videobuf_buffer *vb;
386
387 spin_lock_irqsave(&pcdev->lock, flags);
388
389 status = DCSR(pcdev->dma_chan_y);
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300390 DCSR(pcdev->dma_chan_y) = status;
391
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300392 if (status & DCSR_BUSERR) {
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300393 dev_err(pcdev->dev, "DMA Bus Error IRQ!\n");
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300394 goto out;
395 }
396
397 if (!(status & DCSR_ENDINTR)) {
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300398 dev_err(pcdev->dev, "Unknown DMA IRQ source, "
399 "status: 0x%08x\n", status);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300400 goto out;
401 }
402
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300403 if (!pcdev->active) {
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300404 dev_err(pcdev->dev, "DMA End IRQ with no active buffer!\n");
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300405 goto out;
406 }
407
408 vb = &pcdev->active->vb;
409 buf = container_of(vb, struct pxa_buffer, vb);
410 WARN_ON(buf->inwork || list_empty(&vb->queue));
411 dev_dbg(pcdev->dev, "%s (vb=0x%p) 0x%08lx %d\n", __FUNCTION__,
412 vb, vb->baddr, vb->bsize);
413
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300414 /* _init is used to debug races, see comment in pxa_camera_reqbufs() */
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300415 list_del_init(&vb->queue);
416 vb->state = VIDEOBUF_DONE;
417 do_gettimeofday(&vb->ts);
418 vb->field_count++;
419 wake_up(&vb->done);
420
421 if (list_empty(&pcdev->capture)) {
422 pcdev->active = NULL;
423 DCSR(pcdev->dma_chan_y) = 0;
424 CICR0 &= ~CICR0_ENB;
425 goto out;
426 }
427
428 pcdev->active = list_entry(pcdev->capture.next, struct pxa_buffer,
429 vb.queue);
430
431out:
432 spin_unlock_irqrestore(&pcdev->lock, flags);
433}
434
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300435static struct videobuf_queue_ops pxa_videobuf_ops = {
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300436 .buf_setup = pxa_videobuf_setup,
437 .buf_prepare = pxa_videobuf_prepare,
438 .buf_queue = pxa_videobuf_queue,
439 .buf_release = pxa_videobuf_release,
440};
441
442static int mclk_get_divisor(struct pxa_camera_dev *pcdev)
443{
444 unsigned int mclk_10khz = pcdev->platform_mclk_10khz;
445 unsigned long div;
446 unsigned long lcdclk;
447
448 lcdclk = clk_get_rate(pcdev->clk) / 10000;
449
450 /* We verify platform_mclk_10khz != 0, so if anyone breaks it, here
451 * they get a nice Oops */
452 div = (lcdclk + 2 * mclk_10khz - 1) / (2 * mclk_10khz) - 1;
453
454 dev_dbg(pcdev->dev, "LCD clock %lukHz, target freq %dkHz, "
455 "divisor %lu\n", lcdclk * 10, mclk_10khz * 10, div);
456
457 return div;
458}
459
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300460static void pxa_camera_activate(struct pxa_camera_dev *pcdev)
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300461{
462 struct pxacamera_platform_data *pdata = pcdev->pdata;
463 u32 cicr4 = 0;
464
465 dev_dbg(pcdev->dev, "Registered platform device at %p data %p\n",
466 pcdev, pdata);
467
468 if (pdata && pdata->init) {
469 dev_dbg(pcdev->dev, "%s: Init gpios\n", __FUNCTION__);
470 pdata->init(pcdev->dev);
471 }
472
473 if (pdata && pdata->power) {
474 dev_dbg(pcdev->dev, "%s: Power on camera\n", __FUNCTION__);
475 pdata->power(pcdev->dev, 1);
476 }
477
478 if (pdata && pdata->reset) {
479 dev_dbg(pcdev->dev, "%s: Releasing camera reset\n",
480 __FUNCTION__);
481 pdata->reset(pcdev->dev, 1);
482 }
483
484 CICR0 = 0x3FF; /* disable all interrupts */
485
486 if (pcdev->platform_flags & PXA_CAMERA_PCLK_EN)
487 cicr4 |= CICR4_PCLK_EN;
488 if (pcdev->platform_flags & PXA_CAMERA_MCLK_EN)
489 cicr4 |= CICR4_MCLK_EN;
490 if (pcdev->platform_flags & PXA_CAMERA_PCP)
491 cicr4 |= CICR4_PCP;
492 if (pcdev->platform_flags & PXA_CAMERA_HSP)
493 cicr4 |= CICR4_HSP;
494 if (pcdev->platform_flags & PXA_CAMERA_VSP)
495 cicr4 |= CICR4_VSP;
496
497 CICR4 = mclk_get_divisor(pcdev) | cicr4;
498
499 clk_enable(pcdev->clk);
500}
501
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300502static void pxa_camera_deactivate(struct pxa_camera_dev *pcdev)
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300503{
504 struct pxacamera_platform_data *board = pcdev->pdata;
505
506 clk_disable(pcdev->clk);
507
508 if (board && board->reset) {
509 dev_dbg(pcdev->dev, "%s: Asserting camera reset\n",
510 __FUNCTION__);
511 board->reset(pcdev->dev, 0);
512 }
513
514 if (board && board->power) {
515 dev_dbg(pcdev->dev, "%s: Power off camera\n", __FUNCTION__);
516 board->power(pcdev->dev, 0);
517 }
518}
519
520static irqreturn_t pxa_camera_irq(int irq, void *data)
521{
522 struct pxa_camera_dev *pcdev = data;
523 unsigned int status = CISR;
524
525 dev_dbg(pcdev->dev, "Camera interrupt status 0x%x\n", status);
526
527 CISR = status;
528
529 return IRQ_HANDLED;
530}
531
532/* The following two functions absolutely depend on the fact, that
533 * there can be only one camera on PXA quick capture interface */
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300534static int pxa_camera_add_device(struct soc_camera_device *icd)
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300535{
536 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
537 struct pxa_camera_dev *pcdev = ici->priv;
538 int ret;
539
540 mutex_lock(&camera_lock);
541
542 if (pcdev->icd) {
543 ret = -EBUSY;
544 goto ebusy;
545 }
546
547 dev_info(&icd->dev, "PXA Camera driver attached to camera %d\n",
548 icd->devnum);
549
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300550 pxa_camera_activate(pcdev);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300551 ret = icd->ops->init(icd);
552
553 if (!ret)
554 pcdev->icd = icd;
555
556ebusy:
557 mutex_unlock(&camera_lock);
558
559 return ret;
560}
561
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300562static void pxa_camera_remove_device(struct soc_camera_device *icd)
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300563{
564 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
565 struct pxa_camera_dev *pcdev = ici->priv;
566
567 BUG_ON(icd != pcdev->icd);
568
569 dev_info(&icd->dev, "PXA Camera driver detached from camera %d\n",
570 icd->devnum);
571
572 /* disable capture, disable interrupts */
573 CICR0 = 0x3ff;
574 /* Stop DMA engine */
575 DCSR(pcdev->dma_chan_y) = 0;
576
577 icd->ops->release(icd);
578
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300579 pxa_camera_deactivate(pcdev);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300580
581 pcdev->icd = NULL;
582}
583
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300584static int pxa_camera_set_capture_format(struct soc_camera_device *icd,
585 __u32 pixfmt, struct v4l2_rect *rect)
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300586{
587 struct soc_camera_host *ici =
588 to_soc_camera_host(icd->dev.parent);
589 struct pxa_camera_dev *pcdev = ici->priv;
590 unsigned int datawidth = 0, dw, bpp;
591 u32 cicr0, cicr4 = 0;
592 int ret;
593
594 /* If requested data width is supported by the platform, use it */
595 switch (icd->cached_datawidth) {
596 case 10:
597 if (pcdev->platform_flags & PXA_CAMERA_DATAWIDTH_10)
598 datawidth = IS_DATAWIDTH_10;
599 break;
600 case 9:
601 if (pcdev->platform_flags & PXA_CAMERA_DATAWIDTH_9)
602 datawidth = IS_DATAWIDTH_9;
603 break;
604 case 8:
605 if (pcdev->platform_flags & PXA_CAMERA_DATAWIDTH_8)
606 datawidth = IS_DATAWIDTH_8;
607 }
608 if (!datawidth)
609 return -EINVAL;
610
611 ret = icd->ops->set_capture_format(icd, pixfmt, rect,
612 datawidth |
613 (pcdev->platform_flags & PXA_CAMERA_MASTER ?
614 IS_MASTER : 0) |
615 (pcdev->platform_flags & PXA_CAMERA_HSP ?
616 0 : IS_HSYNC_ACTIVE_HIGH) |
617 (pcdev->platform_flags & PXA_CAMERA_VSP ?
618 0 : IS_VSYNC_ACTIVE_HIGH) |
619 (pcdev->platform_flags & PXA_CAMERA_PCP ?
620 0 : IS_PCLK_SAMPLE_RISING));
621 if (ret < 0)
622 return ret;
623
624 /* Datawidth is now guaranteed to be equal to one of the three values.
625 * We fix bit-per-pixel equal to data-width... */
626 switch (datawidth) {
627 case IS_DATAWIDTH_10:
628 icd->cached_datawidth = 10;
629 dw = 4;
630 bpp = 0x40;
631 break;
632 case IS_DATAWIDTH_9:
633 icd->cached_datawidth = 9;
634 dw = 3;
635 bpp = 0x20;
636 break;
637 default:
638 /* Actually it can only be 8 now,
639 * default is just to silence compiler warnings */
640 case IS_DATAWIDTH_8:
641 icd->cached_datawidth = 8;
642 dw = 2;
643 bpp = 0;
644 }
645
646 if (pcdev->platform_flags & PXA_CAMERA_PCLK_EN)
647 cicr4 |= CICR4_PCLK_EN;
648 if (pcdev->platform_flags & PXA_CAMERA_MCLK_EN)
649 cicr4 |= CICR4_MCLK_EN;
650 if (pcdev->platform_flags & PXA_CAMERA_PCP)
651 cicr4 |= CICR4_PCP;
652 if (pcdev->platform_flags & PXA_CAMERA_HSP)
653 cicr4 |= CICR4_HSP;
654 if (pcdev->platform_flags & PXA_CAMERA_VSP)
655 cicr4 |= CICR4_VSP;
656
657 cicr0 = CICR0;
658 if (cicr0 & CICR0_ENB)
659 CICR0 = cicr0 & ~CICR0_ENB;
660 CICR1 = CICR1_PPL_VAL(rect->width - 1) | bpp | dw;
661 CICR2 = 0;
662 CICR3 = CICR3_LPF_VAL(rect->height - 1) |
663 CICR3_BFW_VAL(min((unsigned short)255, icd->y_skip_top));
664 CICR4 = mclk_get_divisor(pcdev) | cicr4;
665
666 /* CIF interrupts are not used, only DMA */
667 CICR0 = (pcdev->platform_flags & PXA_CAMERA_MASTER ?
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300668 CICR0_SIM_MP : (CICR0_SL_CAP_EN | CICR0_SIM_SP)) |
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300669 CICR0_DMAEN | CICR0_IRQ_MASK | (cicr0 & CICR0_ENB);
670
671 return 0;
672}
673
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300674static int pxa_camera_try_fmt_cap(struct soc_camera_host *ici,
675 struct v4l2_format *f)
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300676{
677 /* limit to pxa hardware capabilities */
678 if (f->fmt.pix.height < 32)
679 f->fmt.pix.height = 32;
680 if (f->fmt.pix.height > 2048)
681 f->fmt.pix.height = 2048;
682 if (f->fmt.pix.width < 48)
683 f->fmt.pix.width = 48;
684 if (f->fmt.pix.width > 2048)
685 f->fmt.pix.width = 2048;
686 f->fmt.pix.width &= ~0x01;
687
688 return 0;
689}
690
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300691static int pxa_camera_reqbufs(struct soc_camera_file *icf,
692 struct v4l2_requestbuffers *p)
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300693{
694 int i;
695
696 /* This is for locking debugging only. I removed spinlocks and now I
697 * check whether .prepare is ever called on a linked buffer, or whether
698 * a dma IRQ can occur for an in-work or unlinked buffer. Until now
699 * it hadn't triggered */
700 for (i = 0; i < p->count; i++) {
701 struct pxa_buffer *buf = container_of(icf->vb_vidq.bufs[i],
702 struct pxa_buffer, vb);
703 buf->inwork = 0;
704 INIT_LIST_HEAD(&buf->vb.queue);
705 }
706
707 return 0;
708}
709
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300710static unsigned int pxa_camera_poll(struct file *file, poll_table *pt)
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300711{
712 struct soc_camera_file *icf = file->private_data;
713 struct pxa_buffer *buf;
714
715 buf = list_entry(icf->vb_vidq.stream.next, struct pxa_buffer,
716 vb.stream);
717
718 poll_wait(file, &buf->vb.done, pt);
719
720 if (buf->vb.state == VIDEOBUF_DONE ||
721 buf->vb.state == VIDEOBUF_ERROR)
722 return POLLIN|POLLRDNORM;
723
724 return 0;
725}
726
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300727static int pxa_camera_querycap(struct soc_camera_host *ici,
728 struct v4l2_capability *cap)
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300729{
730 /* cap->name is set by the firendly caller:-> */
731 strlcpy(cap->card, pxa_cam_driver_description, sizeof(cap->card));
732 cap->version = PXA_CAM_VERSION_CODE;
733 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
734
735 return 0;
736}
737
738/* Should beallocated dynamically too, but we have only one. */
739static struct soc_camera_host pxa_soc_camera_host = {
740 .drv_name = PXA_CAM_DRV_NAME,
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300741 .vbq_ops = &pxa_videobuf_ops,
742 .add = pxa_camera_add_device,
743 .remove = pxa_camera_remove_device,
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300744 .msize = sizeof(struct pxa_buffer),
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300745 .set_capture_format = pxa_camera_set_capture_format,
746 .try_fmt_cap = pxa_camera_try_fmt_cap,
747 .reqbufs = pxa_camera_reqbufs,
748 .poll = pxa_camera_poll,
749 .querycap = pxa_camera_querycap,
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300750};
751
752static int pxa_camera_probe(struct platform_device *pdev)
753{
754 struct pxa_camera_dev *pcdev;
755 struct resource *res;
756 void __iomem *base;
757 unsigned int irq;
758 int err = 0;
759
760 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
761 irq = platform_get_irq(pdev, 0);
762 if (!res || !irq) {
763 err = -ENODEV;
764 goto exit;
765 }
766
767 pcdev = kzalloc(sizeof(*pcdev), GFP_KERNEL);
768 if (!pcdev) {
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300769 dev_err(&pdev->dev, "Could not allocate pcdev\n");
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300770 err = -ENOMEM;
771 goto exit;
772 }
773
774 pcdev->clk = clk_get(&pdev->dev, "CAMCLK");
775 if (IS_ERR(pcdev->clk)) {
776 err = PTR_ERR(pcdev->clk);
777 goto exit_kfree;
778 }
779
780 dev_set_drvdata(&pdev->dev, pcdev);
781 pcdev->res = res;
782
783 pcdev->pdata = pdev->dev.platform_data;
784 pcdev->platform_flags = pcdev->pdata->flags;
785 if (!pcdev->platform_flags & (PXA_CAMERA_DATAWIDTH_8 |
786 PXA_CAMERA_DATAWIDTH_9 | PXA_CAMERA_DATAWIDTH_10)) {
787 /* Platform hasn't set available data widths. This is bad.
788 * Warn and use a default. */
789 dev_warn(&pdev->dev, "WARNING! Platform hasn't set available "
790 "data widths, using default 10 bit\n");
791 pcdev->platform_flags |= PXA_CAMERA_DATAWIDTH_10;
792 }
793 pcdev->platform_mclk_10khz = pcdev->pdata->mclk_10khz;
794 if (!pcdev->platform_mclk_10khz) {
795 dev_warn(&pdev->dev,
796 "mclk_10khz == 0! Please, fix your platform data. "
797 "Using default 20MHz\n");
798 pcdev->platform_mclk_10khz = 2000;
799 }
800
801 INIT_LIST_HEAD(&pcdev->capture);
802 spin_lock_init(&pcdev->lock);
803
804 /*
805 * Request the regions.
806 */
807 if (!request_mem_region(res->start, res->end - res->start + 1,
808 PXA_CAM_DRV_NAME)) {
809 err = -EBUSY;
810 goto exit_clk;
811 }
812
813 base = ioremap(res->start, res->end - res->start + 1);
814 if (!base) {
815 err = -ENOMEM;
816 goto exit_release;
817 }
818 pcdev->irq = irq;
819 pcdev->base = base;
820 pcdev->dev = &pdev->dev;
821
822 /* request dma */
823 pcdev->dma_chan_y = pxa_request_dma("CI_Y", DMA_PRIO_HIGH,
824 pxa_camera_dma_irq_y, pcdev);
825 if (pcdev->dma_chan_y < 0) {
826 dev_err(pcdev->dev, "Can't request DMA for Y\n");
827 err = -ENOMEM;
828 goto exit_iounmap;
829 }
830 dev_dbg(pcdev->dev, "got DMA channel %d\n", pcdev->dma_chan_y);
831
832 DRCMR68 = pcdev->dma_chan_y | DRCMR_MAPVLD;
833
834 /* request irq */
835 err = request_irq(pcdev->irq, pxa_camera_irq, 0, PXA_CAM_DRV_NAME,
836 pcdev);
837 if (err) {
838 dev_err(pcdev->dev, "Camera interrupt register failed \n");
839 goto exit_free_dma;
840 }
841
842 pxa_soc_camera_host.priv = pcdev;
843 pxa_soc_camera_host.dev.parent = &pdev->dev;
844 pxa_soc_camera_host.nr = pdev->id;
845 err = soc_camera_host_register(&pxa_soc_camera_host, THIS_MODULE);
846 if (err)
847 goto exit_free_irq;
848
849 return 0;
850
851exit_free_irq:
852 free_irq(pcdev->irq, pcdev);
853exit_free_dma:
854 pxa_free_dma(pcdev->dma_chan_y);
855exit_iounmap:
856 iounmap(base);
857exit_release:
858 release_mem_region(res->start, res->end - res->start + 1);
859exit_clk:
860 clk_put(pcdev->clk);
861exit_kfree:
862 kfree(pcdev);
863exit:
864 return err;
865}
866
867static int __devexit pxa_camera_remove(struct platform_device *pdev)
868{
869 struct pxa_camera_dev *pcdev = platform_get_drvdata(pdev);
870 struct resource *res;
871
872 clk_put(pcdev->clk);
873
874 pxa_free_dma(pcdev->dma_chan_y);
875 free_irq(pcdev->irq, pcdev);
876
877 soc_camera_host_unregister(&pxa_soc_camera_host);
878
879 iounmap(pcdev->base);
880
881 res = pcdev->res;
882 release_mem_region(res->start, res->end - res->start + 1);
883
884 kfree(pcdev);
885
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300886 dev_info(&pdev->dev, "PXA Camera driver unloaded\n");
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300887
888 return 0;
889}
890
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300891static struct platform_driver pxa_camera_driver = {
892 .driver = {
893 .name = PXA_CAM_DRV_NAME,
894 },
895 .probe = pxa_camera_probe,
896 .remove = __exit_p(pxa_camera_remove),
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300897};
898
899
900static int __devinit pxa_camera_init(void)
901{
902 return platform_driver_register(&pxa_camera_driver);
903}
904
905static void __exit pxa_camera_exit(void)
906{
907 return platform_driver_unregister(&pxa_camera_driver);
908}
909
910module_init(pxa_camera_init);
911module_exit(pxa_camera_exit);
912
913MODULE_DESCRIPTION("PXA27x SoC Camera Host driver");
914MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
915MODULE_LICENSE("GPL");