2 * V4L2 SoC Camera driver for OMAP1 Camera Interface
4 * Copyright (C) 2010, Janusz Krzysztofik <jkrzyszt@tis.icnet.pl>
6 * Based on V4L2 Driver for i.MXL/i.MXL camera (CSI) host
7 * Copyright (C) 2008, Paulius Zaleckas <paulius.zaleckas@teltonika.lt>
8 * Copyright (C) 2009, Darius Augulis <augulis.darius@gmail.com>
10 * Based on PXA SoC camera driver
11 * Copyright (C) 2006, Sascha Hauer, Pengutronix
12 * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
14 * Hardware specific bits initialy based on former work by Matt Callow
15 * drivers/media/video/omap/omap1510cam.c
16 * Copyright (C) 2006 Matt Callow
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License version 2 as
20 * published by the Free Software Foundation.
24 #include <linux/clk.h>
25 #include <linux/dma-mapping.h>
26 #include <linux/interrupt.h>
27 #include <linux/module.h>
28 #include <linux/platform_device.h>
29 #include <linux/slab.h>
31 #include <media/omap1_camera.h>
32 #include <media/soc_camera.h>
33 #include <media/soc_mediabus.h>
34 #include <media/videobuf-dma-contig.h>
35 #include <media/videobuf-dma-sg.h>
40 #define DRIVER_NAME "omap1-camera"
41 #define DRIVER_VERSION "0.0.2"
45 * ---------------------------------------------------------------------------
46 * OMAP1 Camera Interface registers
47 * ---------------------------------------------------------------------------
50 #define REG_CTRLCLOCK 0x00
51 #define REG_IT_STATUS 0x04
53 #define REG_STATUS 0x0C
54 #define REG_CAMDATA 0x10
56 #define REG_PEAK_COUNTER 0x18
58 /* CTRLCLOCK bit shifts */
59 #define LCLK_EN BIT(7)
60 #define DPLL_EN BIT(6)
61 #define MCLK_EN BIT(5)
62 #define CAMEXCLK_EN BIT(4)
64 #define FOSCMOD_SHIFT 0
65 #define FOSCMOD_MASK (0x7 << FOSCMOD_SHIFT)
66 #define FOSCMOD_12MHz 0x0
67 #define FOSCMOD_6MHz 0x2
68 #define FOSCMOD_9_6MHz 0x4
69 #define FOSCMOD_24MHz 0x5
70 #define FOSCMOD_8MHz 0x6
72 /* IT_STATUS bit shifts */
73 #define DATA_TRANSFER BIT(5)
74 #define FIFO_FULL BIT(4)
81 #define RAZ_FIFO BIT(18)
82 #define EN_FIFO_FULL BIT(17)
83 #define EN_NIRQ BIT(16)
84 #define THRESHOLD_SHIFT 9
85 #define THRESHOLD_MASK (0x7f << THRESHOLD_SHIFT)
87 #define EN_H_DOWN BIT(7)
88 #define EN_H_UP BIT(6)
89 #define EN_V_DOWN BIT(5)
90 #define EN_V_UP BIT(4)
91 #define ORDERCAMD BIT(3)
93 #define IRQ_MASK (EN_V_UP | EN_V_DOWN | EN_H_UP | EN_H_DOWN | \
94 EN_NIRQ | EN_FIFO_FULL)
96 /* STATUS bit shifts */
97 #define HSTATUS BIT(1)
98 #define VSTATUS BIT(0)
100 /* GPIO bit shifts */
101 #define CAM_RST BIT(0)
103 /* end of OMAP1 Camera Interface registers */
106 #define SOCAM_BUS_FLAGS (V4L2_MBUS_MASTER | \
107 V4L2_MBUS_HSYNC_ACTIVE_HIGH | V4L2_MBUS_VSYNC_ACTIVE_HIGH | \
108 V4L2_MBUS_PCLK_SAMPLE_RISING | V4L2_MBUS_PCLK_SAMPLE_FALLING | \
109 V4L2_MBUS_DATA_ACTIVE_HIGH)
112 #define FIFO_SIZE ((THRESHOLD_MASK >> THRESHOLD_SHIFT) + 1)
113 #define FIFO_SHIFT __fls(FIFO_SIZE)
115 #define DMA_BURST_SHIFT (1 + OMAP_DMA_DATA_BURST_4)
116 #define DMA_BURST_SIZE (1 << DMA_BURST_SHIFT)
118 #define DMA_ELEMENT_SHIFT OMAP_DMA_DATA_TYPE_S32
119 #define DMA_ELEMENT_SIZE (1 << DMA_ELEMENT_SHIFT)
121 #define DMA_FRAME_SHIFT_CONTIG (FIFO_SHIFT - 1)
122 #define DMA_FRAME_SHIFT_SG DMA_BURST_SHIFT
124 #define DMA_FRAME_SHIFT(x) ((x) == OMAP1_CAM_DMA_CONTIG ? \
125 DMA_FRAME_SHIFT_CONTIG : \
127 #define DMA_FRAME_SIZE(x) (1 << DMA_FRAME_SHIFT(x))
128 #define DMA_SYNC OMAP_DMA_SYNC_FRAME
129 #define THRESHOLD_LEVEL DMA_FRAME_SIZE
132 #define MAX_VIDEO_MEM 4 /* arbitrary video memory limit in MB */
139 /* buffer for one video frame */
140 struct omap1_cam_buf
{
141 struct videobuf_buffer vb
;
142 enum v4l2_mbus_pixelcode code
;
144 struct scatterlist
*sgbuf
;
147 enum videobuf_state result
;
150 struct omap1_cam_dev
{
151 struct soc_camera_host soc_host
;
152 struct soc_camera_device
*icd
;
160 struct omap1_cam_platform_data
*pdata
;
161 struct resource
*res
;
162 unsigned long pflags
;
163 unsigned long camexclk
;
165 struct list_head capture
;
167 /* lock used to protect videobuf */
170 /* Pointers to DMA buffers */
171 struct omap1_cam_buf
*active
;
172 struct omap1_cam_buf
*ready
;
174 enum omap1_cam_vb_mode vb_mode
;
175 int (*mmap_mapper
)(struct videobuf_queue
*q
,
176 struct videobuf_buffer
*buf
,
177 struct vm_area_struct
*vma
);
183 static void cam_write(struct omap1_cam_dev
*pcdev
, u16 reg
, u32 val
)
185 pcdev
->reg_cache
[reg
/ sizeof(u32
)] = val
;
186 __raw_writel(val
, pcdev
->base
+ reg
);
189 static u32
cam_read(struct omap1_cam_dev
*pcdev
, u16 reg
, bool from_cache
)
191 return !from_cache
? __raw_readl(pcdev
->base
+ reg
) :
192 pcdev
->reg_cache
[reg
/ sizeof(u32
)];
195 #define CAM_READ(pcdev, reg) \
196 cam_read(pcdev, REG_##reg, false)
197 #define CAM_WRITE(pcdev, reg, val) \
198 cam_write(pcdev, REG_##reg, val)
199 #define CAM_READ_CACHE(pcdev, reg) \
200 cam_read(pcdev, REG_##reg, true)
203 * Videobuf operations
205 static int omap1_videobuf_setup(struct videobuf_queue
*vq
, unsigned int *count
,
208 struct soc_camera_device
*icd
= vq
->priv_data
;
209 int bytes_per_line
= soc_mbus_bytes_per_line(icd
->user_width
,
210 icd
->current_fmt
->host_fmt
);
211 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
212 struct omap1_cam_dev
*pcdev
= ici
->priv
;
214 if (bytes_per_line
< 0)
215 return bytes_per_line
;
217 *size
= bytes_per_line
* icd
->user_height
;
219 if (!*count
|| *count
< OMAP1_CAMERA_MIN_BUF_COUNT(pcdev
->vb_mode
))
220 *count
= OMAP1_CAMERA_MIN_BUF_COUNT(pcdev
->vb_mode
);
222 if (*size
* *count
> MAX_VIDEO_MEM
* 1024 * 1024)
223 *count
= (MAX_VIDEO_MEM
* 1024 * 1024) / *size
;
226 "%s: count=%d, size=%d\n", __func__
, *count
, *size
);
231 static void free_buffer(struct videobuf_queue
*vq
, struct omap1_cam_buf
*buf
,
232 enum omap1_cam_vb_mode vb_mode
)
234 struct videobuf_buffer
*vb
= &buf
->vb
;
236 BUG_ON(in_interrupt());
238 videobuf_waiton(vq
, vb
, 0, 0);
240 if (vb_mode
== OMAP1_CAM_DMA_CONTIG
) {
241 videobuf_dma_contig_free(vq
, vb
);
243 struct soc_camera_device
*icd
= vq
->priv_data
;
244 struct device
*dev
= icd
->parent
;
245 struct videobuf_dmabuf
*dma
= videobuf_to_dma(vb
);
247 videobuf_dma_unmap(dev
, dma
);
248 videobuf_dma_free(dma
);
251 vb
->state
= VIDEOBUF_NEEDS_INIT
;
254 static int omap1_videobuf_prepare(struct videobuf_queue
*vq
,
255 struct videobuf_buffer
*vb
, enum v4l2_field field
)
257 struct soc_camera_device
*icd
= vq
->priv_data
;
258 struct omap1_cam_buf
*buf
= container_of(vb
, struct omap1_cam_buf
, vb
);
259 int bytes_per_line
= soc_mbus_bytes_per_line(icd
->user_width
,
260 icd
->current_fmt
->host_fmt
);
261 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
262 struct omap1_cam_dev
*pcdev
= ici
->priv
;
265 if (bytes_per_line
< 0)
266 return bytes_per_line
;
268 WARN_ON(!list_empty(&vb
->queue
));
270 BUG_ON(NULL
== icd
->current_fmt
);
274 if (buf
->code
!= icd
->current_fmt
->code
|| vb
->field
!= field
||
275 vb
->width
!= icd
->user_width
||
276 vb
->height
!= icd
->user_height
) {
277 buf
->code
= icd
->current_fmt
->code
;
278 vb
->width
= icd
->user_width
;
279 vb
->height
= icd
->user_height
;
281 vb
->state
= VIDEOBUF_NEEDS_INIT
;
284 vb
->size
= bytes_per_line
* vb
->height
;
286 if (vb
->baddr
&& vb
->bsize
< vb
->size
) {
291 if (vb
->state
== VIDEOBUF_NEEDS_INIT
) {
292 ret
= videobuf_iolock(vq
, vb
, NULL
);
296 vb
->state
= VIDEOBUF_PREPARED
;
302 free_buffer(vq
, buf
, pcdev
->vb_mode
);
308 static void set_dma_dest_params(int dma_ch
, struct omap1_cam_buf
*buf
,
309 enum omap1_cam_vb_mode vb_mode
)
312 unsigned int block_size
;
314 if (vb_mode
== OMAP1_CAM_DMA_CONTIG
) {
315 dma_addr
= videobuf_to_dma_contig(&buf
->vb
);
316 block_size
= buf
->vb
.size
;
318 if (WARN_ON(!buf
->sgbuf
)) {
319 buf
->result
= VIDEOBUF_ERROR
;
322 dma_addr
= sg_dma_address(buf
->sgbuf
);
323 if (WARN_ON(!dma_addr
)) {
325 buf
->result
= VIDEOBUF_ERROR
;
328 block_size
= sg_dma_len(buf
->sgbuf
);
329 if (WARN_ON(!block_size
)) {
331 buf
->result
= VIDEOBUF_ERROR
;
334 if (unlikely(buf
->bytes_left
< block_size
))
335 block_size
= buf
->bytes_left
;
336 if (WARN_ON(dma_addr
& (DMA_FRAME_SIZE(vb_mode
) *
337 DMA_ELEMENT_SIZE
- 1))) {
338 dma_addr
= ALIGN(dma_addr
, DMA_FRAME_SIZE(vb_mode
) *
340 block_size
&= ~(DMA_FRAME_SIZE(vb_mode
) *
341 DMA_ELEMENT_SIZE
- 1);
343 buf
->bytes_left
-= block_size
;
347 omap_set_dma_dest_params(dma_ch
,
348 OMAP_DMA_PORT_EMIFF
, OMAP_DMA_AMODE_POST_INC
, dma_addr
, 0, 0);
349 omap_set_dma_transfer_params(dma_ch
,
350 OMAP_DMA_DATA_TYPE_S32
, DMA_FRAME_SIZE(vb_mode
),
351 block_size
>> (DMA_FRAME_SHIFT(vb_mode
) + DMA_ELEMENT_SHIFT
),
355 static struct omap1_cam_buf
*prepare_next_vb(struct omap1_cam_dev
*pcdev
)
357 struct omap1_cam_buf
*buf
;
360 * If there is already a buffer pointed out by the pcdev->ready,
361 * (re)use it, otherwise try to fetch and configure a new one.
365 if (list_empty(&pcdev
->capture
))
367 buf
= list_entry(pcdev
->capture
.next
,
368 struct omap1_cam_buf
, vb
.queue
);
369 buf
->vb
.state
= VIDEOBUF_ACTIVE
;
371 list_del_init(&buf
->vb
.queue
);
374 if (pcdev
->vb_mode
== OMAP1_CAM_DMA_CONTIG
) {
376 * In CONTIG mode, we can safely enter next buffer parameters
377 * into the DMA programming register set after the DMA
378 * has already been activated on the previous buffer
380 set_dma_dest_params(pcdev
->dma_ch
, buf
, pcdev
->vb_mode
);
383 * In SG mode, the above is not safe since there are probably
384 * a bunch of sgbufs from previous sglist still pending.
385 * Instead, mark the sglist fresh for the upcoming
394 static struct scatterlist
*try_next_sgbuf(int dma_ch
, struct omap1_cam_buf
*buf
)
396 struct scatterlist
*sgbuf
;
398 if (likely(buf
->sgbuf
)) {
399 /* current sglist is active */
400 if (unlikely(!buf
->bytes_left
)) {
401 /* indicate sglist complete */
404 /* process next sgbuf */
405 sgbuf
= sg_next(buf
->sgbuf
);
406 if (WARN_ON(!sgbuf
)) {
407 buf
->result
= VIDEOBUF_ERROR
;
408 } else if (WARN_ON(!sg_dma_len(sgbuf
))) {
410 buf
->result
= VIDEOBUF_ERROR
;
415 /* sglist is fresh, initialize it before using */
416 struct videobuf_dmabuf
*dma
= videobuf_to_dma(&buf
->vb
);
419 if (!(WARN_ON(!sgbuf
))) {
422 buf
->bytes_left
= buf
->vb
.size
;
423 buf
->result
= VIDEOBUF_DONE
;
428 * Put our next sgbuf parameters (address, size)
429 * into the DMA programming register set.
431 set_dma_dest_params(dma_ch
, buf
, OMAP1_CAM_DMA_SG
);
436 static void start_capture(struct omap1_cam_dev
*pcdev
)
438 struct omap1_cam_buf
*buf
= pcdev
->active
;
439 u32 ctrlclock
= CAM_READ_CACHE(pcdev
, CTRLCLOCK
);
440 u32 mode
= CAM_READ_CACHE(pcdev
, MODE
) & ~EN_V_DOWN
;
446 * Enable start of frame interrupt, which we will use for activating
447 * our end of frame watchdog when capture actually starts.
451 if (unlikely(ctrlclock
& LCLK_EN
))
452 /* stop pixel clock before FIFO reset */
453 CAM_WRITE(pcdev
, CTRLCLOCK
, ctrlclock
& ~LCLK_EN
);
455 CAM_WRITE(pcdev
, MODE
, mode
| RAZ_FIFO
);
457 omap_start_dma(pcdev
->dma_ch
);
459 if (pcdev
->vb_mode
== OMAP1_CAM_DMA_SG
) {
461 * In SG mode, it's a good moment for fetching next sgbuf
462 * from the current sglist and, if available, already putting
463 * its parameters into the DMA programming register set.
465 try_next_sgbuf(pcdev
->dma_ch
, buf
);
468 /* (re)enable pixel clock */
469 CAM_WRITE(pcdev
, CTRLCLOCK
, ctrlclock
| LCLK_EN
);
470 /* release FIFO reset */
471 CAM_WRITE(pcdev
, MODE
, mode
);
474 static void suspend_capture(struct omap1_cam_dev
*pcdev
)
476 u32 ctrlclock
= CAM_READ_CACHE(pcdev
, CTRLCLOCK
);
478 CAM_WRITE(pcdev
, CTRLCLOCK
, ctrlclock
& ~LCLK_EN
);
479 omap_stop_dma(pcdev
->dma_ch
);
482 static void disable_capture(struct omap1_cam_dev
*pcdev
)
484 u32 mode
= CAM_READ_CACHE(pcdev
, MODE
);
486 CAM_WRITE(pcdev
, MODE
, mode
& ~(IRQ_MASK
| DMA
));
489 static void omap1_videobuf_queue(struct videobuf_queue
*vq
,
490 struct videobuf_buffer
*vb
)
492 struct soc_camera_device
*icd
= vq
->priv_data
;
493 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
494 struct omap1_cam_dev
*pcdev
= ici
->priv
;
495 struct omap1_cam_buf
*buf
;
498 list_add_tail(&vb
->queue
, &pcdev
->capture
);
499 vb
->state
= VIDEOBUF_QUEUED
;
503 * Capture in progress, so don't touch pcdev->ready even if
504 * empty. Since the transfer of the DMA programming register set
505 * content to the DMA working register set is done automatically
506 * by the DMA hardware, this can pretty well happen while we
507 * are keeping the lock here. Leave fetching it from the queue
508 * to be done when a next DMA interrupt occures instead.
513 WARN_ON(pcdev
->ready
);
515 buf
= prepare_next_vb(pcdev
);
523 "%s: capture not active, setup FIFO, start DMA\n", __func__
);
524 mode
= CAM_READ_CACHE(pcdev
, MODE
) & ~THRESHOLD_MASK
;
525 mode
|= THRESHOLD_LEVEL(pcdev
->vb_mode
) << THRESHOLD_SHIFT
;
526 CAM_WRITE(pcdev
, MODE
, mode
| EN_FIFO_FULL
| DMA
);
528 if (pcdev
->vb_mode
== OMAP1_CAM_DMA_SG
) {
530 * In SG mode, the above prepare_next_vb() didn't actually
531 * put anything into the DMA programming register set,
532 * so we have to do it now, before activating DMA.
534 try_next_sgbuf(pcdev
->dma_ch
, buf
);
537 start_capture(pcdev
);
540 static void omap1_videobuf_release(struct videobuf_queue
*vq
,
541 struct videobuf_buffer
*vb
)
543 struct omap1_cam_buf
*buf
=
544 container_of(vb
, struct omap1_cam_buf
, vb
);
545 struct soc_camera_device
*icd
= vq
->priv_data
;
546 struct device
*dev
= icd
->parent
;
547 struct soc_camera_host
*ici
= to_soc_camera_host(dev
);
548 struct omap1_cam_dev
*pcdev
= ici
->priv
;
552 dev_dbg(dev
, "%s (done)\n", __func__
);
554 case VIDEOBUF_ACTIVE
:
555 dev_dbg(dev
, "%s (active)\n", __func__
);
557 case VIDEOBUF_QUEUED
:
558 dev_dbg(dev
, "%s (queued)\n", __func__
);
560 case VIDEOBUF_PREPARED
:
561 dev_dbg(dev
, "%s (prepared)\n", __func__
);
564 dev_dbg(dev
, "%s (unknown %d)\n", __func__
, vb
->state
);
568 free_buffer(vq
, buf
, pcdev
->vb_mode
);
571 static void videobuf_done(struct omap1_cam_dev
*pcdev
,
572 enum videobuf_state result
)
574 struct omap1_cam_buf
*buf
= pcdev
->active
;
575 struct videobuf_buffer
*vb
;
576 struct device
*dev
= pcdev
->icd
->parent
;
579 suspend_capture(pcdev
);
580 disable_capture(pcdev
);
584 if (result
== VIDEOBUF_ERROR
)
585 suspend_capture(pcdev
);
588 if (waitqueue_active(&vb
->done
)) {
589 if (!pcdev
->ready
&& result
!= VIDEOBUF_ERROR
) {
591 * No next buffer has been entered into the DMA
592 * programming register set on time (could be done only
593 * while the previous DMA interurpt was processed, not
594 * later), so the last DMA block, be it a whole buffer
595 * if in CONTIG or its last sgbuf if in SG mode, is
596 * about to be reused by the just autoreinitialized DMA
597 * engine, and overwritten with next frame data. Best we
598 * can do is stopping the capture as soon as possible,
599 * hopefully before the next frame start.
601 suspend_capture(pcdev
);
604 do_gettimeofday(&vb
->ts
);
605 if (result
!= VIDEOBUF_ERROR
)
609 /* shift in next buffer */
616 * No next buffer was ready on time (see above), so
617 * indicate error condition to force capture restart or
618 * stop, depending on next buffer already queued or not.
620 result
= VIDEOBUF_ERROR
;
621 prepare_next_vb(pcdev
);
627 } else if (pcdev
->ready
) {
629 * In both CONTIG and SG mode, the DMA engine has possibly
630 * been already autoreinitialized with the preprogrammed
631 * pcdev->ready buffer. We can either accept this fact
632 * and just swap the buffers, or provoke an error condition
633 * and restart capture. The former seems less intrusive.
635 dev_dbg(dev
, "%s: nobody waiting on videobuf, swap with next\n",
637 pcdev
->active
= pcdev
->ready
;
639 if (pcdev
->vb_mode
== OMAP1_CAM_DMA_SG
) {
641 * In SG mode, we have to make sure that the buffer we
642 * are putting back into the pcdev->ready is marked
652 * No next buffer has been entered into
653 * the DMA programming register set on time.
655 if (pcdev
->vb_mode
== OMAP1_CAM_DMA_CONTIG
) {
657 * In CONTIG mode, the DMA engine has already been
658 * reinitialized with the current buffer. Best we can do
659 * is not touching it.
662 "%s: nobody waiting on videobuf, reuse it\n",
666 * In SG mode, the DMA engine has just been
667 * autoreinitialized with the last sgbuf from the
668 * current list. Restart capture in order to transfer
669 * next frame start into the first sgbuf, not the last
672 if (result
!= VIDEOBUF_ERROR
) {
673 suspend_capture(pcdev
);
674 result
= VIDEOBUF_ERROR
;
680 dev_dbg(dev
, "%s: no more videobufs, stop capture\n", __func__
);
681 disable_capture(pcdev
);
685 if (pcdev
->vb_mode
== OMAP1_CAM_DMA_CONTIG
) {
687 * In CONTIG mode, the current buffer parameters had already
688 * been entered into the DMA programming register set while the
689 * buffer was fetched with prepare_next_vb(), they may have also
690 * been transferred into the runtime set and already active if
691 * the DMA still running.
694 /* In SG mode, extra steps are required */
695 if (result
== VIDEOBUF_ERROR
)
696 /* make sure we (re)use sglist from start on error */
700 * In any case, enter the next sgbuf parameters into the DMA
701 * programming register set. They will be used either during
702 * nearest DMA autoreinitialization or, in case of an error,
703 * on DMA startup below.
705 try_next_sgbuf(pcdev
->dma_ch
, buf
);
708 if (result
== VIDEOBUF_ERROR
) {
709 dev_dbg(dev
, "%s: videobuf error; reset FIFO, restart DMA\n",
711 start_capture(pcdev
);
713 * In SG mode, the above also resulted in the next sgbuf
714 * parameters being entered into the DMA programming register
715 * set, making them ready for next DMA autoreinitialization.
720 * Finally, try fetching next buffer.
721 * In CONTIG mode, it will also enter it into the DMA programming
722 * register set, making it ready for next DMA autoreinitialization.
724 prepare_next_vb(pcdev
);
727 static void dma_isr(int channel
, unsigned short status
, void *data
)
729 struct omap1_cam_dev
*pcdev
= data
;
730 struct omap1_cam_buf
*buf
= pcdev
->active
;
733 spin_lock_irqsave(&pcdev
->lock
, flags
);
736 suspend_capture(pcdev
);
737 disable_capture(pcdev
);
741 if (pcdev
->vb_mode
== OMAP1_CAM_DMA_CONTIG
) {
743 * In CONTIG mode, assume we have just managed to collect the
744 * whole frame, hopefully before our end of frame watchdog is
745 * triggered. Then, all we have to do is disabling the watchdog
746 * for this frame, and calling videobuf_done() with success
749 CAM_WRITE(pcdev
, MODE
,
750 CAM_READ_CACHE(pcdev
, MODE
) & ~EN_V_DOWN
);
751 videobuf_done(pcdev
, VIDEOBUF_DONE
);
754 * In SG mode, we have to process every sgbuf from the current
755 * sglist, one after another.
759 * Current sglist not completed yet, try fetching next
760 * sgbuf, hopefully putting it into the DMA programming
761 * register set, making it ready for next DMA
762 * autoreinitialization.
764 try_next_sgbuf(pcdev
->dma_ch
, buf
);
769 * No more sgbufs left in the current sglist. This
770 * doesn't mean that the whole videobuffer is already
771 * complete, but only that the last sgbuf from the
772 * current sglist is about to be filled. It will be
773 * ready on next DMA interrupt, signalled with the
774 * buf->sgbuf set back to NULL.
776 if (buf
->result
!= VIDEOBUF_ERROR
) {
778 * Video frame collected without errors so far,
779 * we can prepare for collecting a next one
780 * as soon as DMA gets autoreinitialized
781 * after the current (last) sgbuf is completed.
783 buf
= prepare_next_vb(pcdev
);
787 try_next_sgbuf(pcdev
->dma_ch
, buf
);
791 /* end of videobuf */
792 videobuf_done(pcdev
, buf
->result
);
796 spin_unlock_irqrestore(&pcdev
->lock
, flags
);
799 static irqreturn_t
cam_isr(int irq
, void *data
)
801 struct omap1_cam_dev
*pcdev
= data
;
802 struct device
*dev
= pcdev
->icd
->parent
;
803 struct omap1_cam_buf
*buf
= pcdev
->active
;
807 it_status
= CAM_READ(pcdev
, IT_STATUS
);
811 spin_lock_irqsave(&pcdev
->lock
, flags
);
814 dev_warn(dev
, "%s: unhandled camera interrupt, status == %#x\n",
815 __func__
, it_status
);
816 suspend_capture(pcdev
);
817 disable_capture(pcdev
);
821 if (unlikely(it_status
& FIFO_FULL
)) {
822 dev_warn(dev
, "%s: FIFO overflow\n", __func__
);
824 } else if (it_status
& V_DOWN
) {
825 /* end of video frame watchdog */
826 if (pcdev
->vb_mode
== OMAP1_CAM_DMA_CONTIG
) {
828 * In CONTIG mode, the watchdog is disabled with
829 * successful DMA end of block interrupt, and reenabled
830 * on next frame start. If we get here, there is nothing
831 * to check, we must be out of sync.
834 if (buf
->sgcount
== 2) {
836 * If exactly 2 sgbufs from the next sglist have
837 * been programmed into the DMA engine (the
838 * first one already transferred into the DMA
839 * runtime register set, the second one still
840 * in the programming set), then we are in sync.
845 dev_notice(dev
, "%s: unexpected end of video frame\n",
848 } else if (it_status
& V_UP
) {
851 if (pcdev
->vb_mode
== OMAP1_CAM_DMA_CONTIG
) {
853 * In CONTIG mode, we need this interrupt every frame
854 * in oredr to reenable our end of frame watchdog.
856 mode
= CAM_READ_CACHE(pcdev
, MODE
);
859 * In SG mode, the below enabled end of frame watchdog
860 * is kept on permanently, so we can turn this one shot
863 mode
= CAM_READ_CACHE(pcdev
, MODE
) & ~EN_V_UP
;
866 if (!(mode
& EN_V_DOWN
)) {
867 /* (re)enable end of frame watchdog interrupt */
870 CAM_WRITE(pcdev
, MODE
, mode
);
874 dev_warn(dev
, "%s: unhandled camera interrupt, status == %#x\n",
875 __func__
, it_status
);
879 videobuf_done(pcdev
, VIDEOBUF_ERROR
);
881 spin_unlock_irqrestore(&pcdev
->lock
, flags
);
885 static struct videobuf_queue_ops omap1_videobuf_ops
= {
886 .buf_setup
= omap1_videobuf_setup
,
887 .buf_prepare
= omap1_videobuf_prepare
,
888 .buf_queue
= omap1_videobuf_queue
,
889 .buf_release
= omap1_videobuf_release
,
894 * SOC Camera host operations
897 static void sensor_reset(struct omap1_cam_dev
*pcdev
, bool reset
)
899 /* apply/release camera sensor reset if requested by platform data */
900 if (pcdev
->pflags
& OMAP1_CAMERA_RST_HIGH
)
901 CAM_WRITE(pcdev
, GPIO
, reset
);
902 else if (pcdev
->pflags
& OMAP1_CAMERA_RST_LOW
)
903 CAM_WRITE(pcdev
, GPIO
, !reset
);
907 * The following two functions absolutely depend on the fact, that
908 * there can be only one camera on OMAP1 camera sensor interface
910 static int omap1_cam_add_device(struct soc_camera_device
*icd
)
912 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
913 struct omap1_cam_dev
*pcdev
= ici
->priv
;
919 clk_enable(pcdev
->clk
);
921 /* setup sensor clock */
922 ctrlclock
= CAM_READ(pcdev
, CTRLCLOCK
);
923 ctrlclock
&= ~(CAMEXCLK_EN
| MCLK_EN
| DPLL_EN
);
924 CAM_WRITE(pcdev
, CTRLCLOCK
, ctrlclock
);
926 ctrlclock
&= ~FOSCMOD_MASK
;
927 switch (pcdev
->camexclk
) {
929 ctrlclock
|= CAMEXCLK_EN
| FOSCMOD_6MHz
;
932 ctrlclock
|= CAMEXCLK_EN
| FOSCMOD_8MHz
| DPLL_EN
;
935 ctrlclock
|= CAMEXCLK_EN
| FOSCMOD_9_6MHz
| DPLL_EN
;
938 ctrlclock
|= CAMEXCLK_EN
| FOSCMOD_12MHz
;
941 ctrlclock
|= CAMEXCLK_EN
| FOSCMOD_24MHz
| DPLL_EN
;
945 CAM_WRITE(pcdev
, CTRLCLOCK
, ctrlclock
& ~DPLL_EN
);
947 /* enable internal clock */
948 ctrlclock
|= MCLK_EN
;
949 CAM_WRITE(pcdev
, CTRLCLOCK
, ctrlclock
);
951 sensor_reset(pcdev
, false);
955 dev_dbg(icd
->parent
, "OMAP1 Camera driver attached to camera %d\n",
960 static void omap1_cam_remove_device(struct soc_camera_device
*icd
)
962 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
963 struct omap1_cam_dev
*pcdev
= ici
->priv
;
966 BUG_ON(icd
!= pcdev
->icd
);
968 suspend_capture(pcdev
);
969 disable_capture(pcdev
);
971 sensor_reset(pcdev
, true);
973 /* disable and release system clocks */
974 ctrlclock
= CAM_READ_CACHE(pcdev
, CTRLCLOCK
);
975 ctrlclock
&= ~(MCLK_EN
| DPLL_EN
| CAMEXCLK_EN
);
976 CAM_WRITE(pcdev
, CTRLCLOCK
, ctrlclock
);
978 ctrlclock
= (ctrlclock
& ~FOSCMOD_MASK
) | FOSCMOD_12MHz
;
979 CAM_WRITE(pcdev
, CTRLCLOCK
, ctrlclock
);
980 CAM_WRITE(pcdev
, CTRLCLOCK
, ctrlclock
| MCLK_EN
);
982 CAM_WRITE(pcdev
, CTRLCLOCK
, ctrlclock
& ~MCLK_EN
);
984 clk_disable(pcdev
->clk
);
989 "OMAP1 Camera driver detached from camera %d\n", icd
->devnum
);
992 /* Duplicate standard formats based on host capability of byte swapping */
993 static const struct soc_mbus_lookup omap1_cam_formats
[] = {
995 .code
= V4L2_MBUS_FMT_UYVY8_2X8
,
997 .fourcc
= V4L2_PIX_FMT_YUYV
,
999 .bits_per_sample
= 8,
1000 .packing
= SOC_MBUS_PACKING_2X8_PADHI
,
1001 .order
= SOC_MBUS_ORDER_BE
,
1004 .code
= V4L2_MBUS_FMT_VYUY8_2X8
,
1006 .fourcc
= V4L2_PIX_FMT_YVYU
,
1008 .bits_per_sample
= 8,
1009 .packing
= SOC_MBUS_PACKING_2X8_PADHI
,
1010 .order
= SOC_MBUS_ORDER_BE
,
1013 .code
= V4L2_MBUS_FMT_YUYV8_2X8
,
1015 .fourcc
= V4L2_PIX_FMT_UYVY
,
1017 .bits_per_sample
= 8,
1018 .packing
= SOC_MBUS_PACKING_2X8_PADHI
,
1019 .order
= SOC_MBUS_ORDER_BE
,
1022 .code
= V4L2_MBUS_FMT_YVYU8_2X8
,
1024 .fourcc
= V4L2_PIX_FMT_VYUY
,
1026 .bits_per_sample
= 8,
1027 .packing
= SOC_MBUS_PACKING_2X8_PADHI
,
1028 .order
= SOC_MBUS_ORDER_BE
,
1031 .code
= V4L2_MBUS_FMT_RGB555_2X8_PADHI_BE
,
1033 .fourcc
= V4L2_PIX_FMT_RGB555
,
1035 .bits_per_sample
= 8,
1036 .packing
= SOC_MBUS_PACKING_2X8_PADHI
,
1037 .order
= SOC_MBUS_ORDER_BE
,
1040 .code
= V4L2_MBUS_FMT_RGB555_2X8_PADHI_LE
,
1042 .fourcc
= V4L2_PIX_FMT_RGB555X
,
1044 .bits_per_sample
= 8,
1045 .packing
= SOC_MBUS_PACKING_2X8_PADHI
,
1046 .order
= SOC_MBUS_ORDER_BE
,
1049 .code
= V4L2_MBUS_FMT_RGB565_2X8_BE
,
1051 .fourcc
= V4L2_PIX_FMT_RGB565
,
1053 .bits_per_sample
= 8,
1054 .packing
= SOC_MBUS_PACKING_2X8_PADHI
,
1055 .order
= SOC_MBUS_ORDER_BE
,
1058 .code
= V4L2_MBUS_FMT_RGB565_2X8_LE
,
1060 .fourcc
= V4L2_PIX_FMT_RGB565X
,
1062 .bits_per_sample
= 8,
1063 .packing
= SOC_MBUS_PACKING_2X8_PADHI
,
1064 .order
= SOC_MBUS_ORDER_BE
,
1069 static int omap1_cam_get_formats(struct soc_camera_device
*icd
,
1070 unsigned int idx
, struct soc_camera_format_xlate
*xlate
)
1072 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
1073 struct device
*dev
= icd
->parent
;
1074 int formats
= 0, ret
;
1075 enum v4l2_mbus_pixelcode code
;
1076 const struct soc_mbus_pixelfmt
*fmt
;
1078 ret
= v4l2_subdev_call(sd
, video
, enum_mbus_fmt
, idx
, &code
);
1080 /* No more formats */
1083 fmt
= soc_mbus_get_fmtdesc(code
);
1085 dev_warn(dev
, "%s: unsupported format code #%d: %d\n", __func__
,
1090 /* Check support for the requested bits-per-sample */
1091 if (fmt
->bits_per_sample
!= 8)
1095 case V4L2_MBUS_FMT_YUYV8_2X8
:
1096 case V4L2_MBUS_FMT_YVYU8_2X8
:
1097 case V4L2_MBUS_FMT_UYVY8_2X8
:
1098 case V4L2_MBUS_FMT_VYUY8_2X8
:
1099 case V4L2_MBUS_FMT_RGB555_2X8_PADHI_BE
:
1100 case V4L2_MBUS_FMT_RGB555_2X8_PADHI_LE
:
1101 case V4L2_MBUS_FMT_RGB565_2X8_BE
:
1102 case V4L2_MBUS_FMT_RGB565_2X8_LE
:
1105 xlate
->host_fmt
= soc_mbus_find_fmtdesc(code
,
1107 ARRAY_SIZE(omap1_cam_formats
));
1111 "%s: providing format %s as byte swapped code #%d\n",
1112 __func__
, xlate
->host_fmt
->name
, code
);
1117 "%s: providing format %s in pass-through mode\n",
1118 __func__
, fmt
->name
);
1122 xlate
->host_fmt
= fmt
;
1130 static bool is_dma_aligned(s32 bytes_per_line
, unsigned int height
,
1131 enum omap1_cam_vb_mode vb_mode
)
1133 int size
= bytes_per_line
* height
;
1135 return IS_ALIGNED(bytes_per_line
, DMA_ELEMENT_SIZE
) &&
1136 IS_ALIGNED(size
, DMA_FRAME_SIZE(vb_mode
) * DMA_ELEMENT_SIZE
);
1139 static int dma_align(int *width
, int *height
,
1140 const struct soc_mbus_pixelfmt
*fmt
,
1141 enum omap1_cam_vb_mode vb_mode
, bool enlarge
)
1143 s32 bytes_per_line
= soc_mbus_bytes_per_line(*width
, fmt
);
1145 if (bytes_per_line
< 0)
1146 return bytes_per_line
;
1148 if (!is_dma_aligned(bytes_per_line
, *height
, vb_mode
)) {
1149 unsigned int pxalign
= __fls(bytes_per_line
/ *width
);
1150 unsigned int salign
= DMA_FRAME_SHIFT(vb_mode
) +
1151 DMA_ELEMENT_SHIFT
- pxalign
;
1152 unsigned int incr
= enlarge
<< salign
;
1154 v4l_bound_align_image(width
, 1, *width
+ incr
, 0,
1155 height
, 1, *height
+ incr
, 0, salign
);
1161 #define subdev_call_with_sense(pcdev, dev, icd, sd, function, args...) \
1163 struct soc_camera_sense sense = { \
1164 .master_clock = pcdev->camexclk, \
1165 .pixel_clock_max = 0, \
1170 sense.pixel_clock_max = pcdev->pdata->lclk_khz_max * 1000; \
1171 icd->sense = &sense; \
1172 __ret = v4l2_subdev_call(sd, video, function, ##args); \
1173 icd->sense = NULL; \
1175 if (sense.flags & SOCAM_SENSE_PCLK_CHANGED) { \
1176 if (sense.pixel_clock > sense.pixel_clock_max) { \
1178 "%s: pixel clock %lu set by the camera too high!\n", \
1179 __func__, sense.pixel_clock); \
1186 static int set_mbus_format(struct omap1_cam_dev
*pcdev
, struct device
*dev
,
1187 struct soc_camera_device
*icd
, struct v4l2_subdev
*sd
,
1188 struct v4l2_mbus_framefmt
*mf
,
1189 const struct soc_camera_format_xlate
*xlate
)
1192 int ret
= subdev_call_with_sense(pcdev
, dev
, icd
, sd
, s_mbus_fmt
, mf
);
1195 dev_err(dev
, "%s: s_mbus_fmt failed\n", __func__
);
1199 if (mf
->code
!= xlate
->code
) {
1200 dev_err(dev
, "%s: unexpected pixel code change\n", __func__
);
1204 bytes_per_line
= soc_mbus_bytes_per_line(mf
->width
, xlate
->host_fmt
);
1205 if (bytes_per_line
< 0) {
1206 dev_err(dev
, "%s: soc_mbus_bytes_per_line() failed\n",
1208 return bytes_per_line
;
1211 if (!is_dma_aligned(bytes_per_line
, mf
->height
, pcdev
->vb_mode
)) {
1212 dev_err(dev
, "%s: resulting geometry %ux%u not DMA aligned\n",
1213 __func__
, mf
->width
, mf
->height
);
1219 static int omap1_cam_set_crop(struct soc_camera_device
*icd
,
1220 struct v4l2_crop
*crop
)
1222 struct v4l2_rect
*rect
= &crop
->c
;
1223 const struct soc_camera_format_xlate
*xlate
= icd
->current_fmt
;
1224 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
1225 struct device
*dev
= icd
->parent
;
1226 struct soc_camera_host
*ici
= to_soc_camera_host(dev
);
1227 struct omap1_cam_dev
*pcdev
= ici
->priv
;
1228 struct v4l2_mbus_framefmt mf
;
1231 ret
= subdev_call_with_sense(pcdev
, dev
, icd
, sd
, s_crop
, crop
);
1233 dev_warn(dev
, "%s: failed to crop to %ux%u@%u:%u\n", __func__
,
1234 rect
->width
, rect
->height
, rect
->left
, rect
->top
);
1238 ret
= v4l2_subdev_call(sd
, video
, g_mbus_fmt
, &mf
);
1240 dev_warn(dev
, "%s: failed to fetch current format\n", __func__
);
1244 ret
= dma_align(&mf
.width
, &mf
.height
, xlate
->host_fmt
, pcdev
->vb_mode
,
1247 dev_err(dev
, "%s: failed to align %ux%u %s with DMA\n",
1248 __func__
, mf
.width
, mf
.height
,
1249 xlate
->host_fmt
->name
);
1254 /* sensor returned geometry not DMA aligned, trying to fix */
1255 ret
= set_mbus_format(pcdev
, dev
, icd
, sd
, &mf
, xlate
);
1257 dev_err(dev
, "%s: failed to set format\n", __func__
);
1262 icd
->user_width
= mf
.width
;
1263 icd
->user_height
= mf
.height
;
1268 static int omap1_cam_set_fmt(struct soc_camera_device
*icd
,
1269 struct v4l2_format
*f
)
1271 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
1272 const struct soc_camera_format_xlate
*xlate
;
1273 struct device
*dev
= icd
->parent
;
1274 struct soc_camera_host
*ici
= to_soc_camera_host(dev
);
1275 struct omap1_cam_dev
*pcdev
= ici
->priv
;
1276 struct v4l2_pix_format
*pix
= &f
->fmt
.pix
;
1277 struct v4l2_mbus_framefmt mf
;
1280 xlate
= soc_camera_xlate_by_fourcc(icd
, pix
->pixelformat
);
1282 dev_warn(dev
, "%s: format %#x not found\n", __func__
,
1287 mf
.width
= pix
->width
;
1288 mf
.height
= pix
->height
;
1289 mf
.field
= pix
->field
;
1290 mf
.colorspace
= pix
->colorspace
;
1291 mf
.code
= xlate
->code
;
1293 ret
= dma_align(&mf
.width
, &mf
.height
, xlate
->host_fmt
, pcdev
->vb_mode
,
1296 dev_err(dev
, "%s: failed to align %ux%u %s with DMA\n",
1297 __func__
, pix
->width
, pix
->height
,
1298 xlate
->host_fmt
->name
);
1302 ret
= set_mbus_format(pcdev
, dev
, icd
, sd
, &mf
, xlate
);
1304 dev_err(dev
, "%s: failed to set format\n", __func__
);
1308 pix
->width
= mf
.width
;
1309 pix
->height
= mf
.height
;
1310 pix
->field
= mf
.field
;
1311 pix
->colorspace
= mf
.colorspace
;
1312 icd
->current_fmt
= xlate
;
1317 static int omap1_cam_try_fmt(struct soc_camera_device
*icd
,
1318 struct v4l2_format
*f
)
1320 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
1321 const struct soc_camera_format_xlate
*xlate
;
1322 struct v4l2_pix_format
*pix
= &f
->fmt
.pix
;
1323 struct v4l2_mbus_framefmt mf
;
1325 /* TODO: limit to mx1 hardware capabilities */
1327 xlate
= soc_camera_xlate_by_fourcc(icd
, pix
->pixelformat
);
1329 dev_warn(icd
->parent
, "Format %#x not found\n",
1334 mf
.width
= pix
->width
;
1335 mf
.height
= pix
->height
;
1336 mf
.field
= pix
->field
;
1337 mf
.colorspace
= pix
->colorspace
;
1338 mf
.code
= xlate
->code
;
1340 /* limit to sensor capabilities */
1341 ret
= v4l2_subdev_call(sd
, video
, try_mbus_fmt
, &mf
);
1345 pix
->width
= mf
.width
;
1346 pix
->height
= mf
.height
;
1347 pix
->field
= mf
.field
;
1348 pix
->colorspace
= mf
.colorspace
;
1353 static bool sg_mode
;
1356 * Local mmap_mapper wrapper,
1357 * used for detecting videobuf-dma-contig buffer allocation failures
1358 * and switching to videobuf-dma-sg automatically for future attempts.
1360 static int omap1_cam_mmap_mapper(struct videobuf_queue
*q
,
1361 struct videobuf_buffer
*buf
,
1362 struct vm_area_struct
*vma
)
1364 struct soc_camera_device
*icd
= q
->priv_data
;
1365 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
1366 struct omap1_cam_dev
*pcdev
= ici
->priv
;
1369 ret
= pcdev
->mmap_mapper(q
, buf
, vma
);
1377 static void omap1_cam_init_videobuf(struct videobuf_queue
*q
,
1378 struct soc_camera_device
*icd
)
1380 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
1381 struct omap1_cam_dev
*pcdev
= ici
->priv
;
1384 videobuf_queue_dma_contig_init(q
, &omap1_videobuf_ops
,
1385 icd
->parent
, &pcdev
->lock
,
1386 V4L2_BUF_TYPE_VIDEO_CAPTURE
, V4L2_FIELD_NONE
,
1387 sizeof(struct omap1_cam_buf
), icd
, &icd
->video_lock
);
1389 videobuf_queue_sg_init(q
, &omap1_videobuf_ops
,
1390 icd
->parent
, &pcdev
->lock
,
1391 V4L2_BUF_TYPE_VIDEO_CAPTURE
, V4L2_FIELD_NONE
,
1392 sizeof(struct omap1_cam_buf
), icd
, &icd
->video_lock
);
1394 /* use videobuf mode (auto)selected with the module parameter */
1395 pcdev
->vb_mode
= sg_mode
? OMAP1_CAM_DMA_SG
: OMAP1_CAM_DMA_CONTIG
;
1398 * Ensure we substitute the videobuf-dma-contig version of the
1399 * mmap_mapper() callback with our own wrapper, used for switching
1400 * automatically to videobuf-dma-sg on buffer allocation failure.
1402 if (!sg_mode
&& q
->int_ops
->mmap_mapper
!= omap1_cam_mmap_mapper
) {
1403 pcdev
->mmap_mapper
= q
->int_ops
->mmap_mapper
;
1404 q
->int_ops
->mmap_mapper
= omap1_cam_mmap_mapper
;
1408 static int omap1_cam_reqbufs(struct soc_camera_device
*icd
,
1409 struct v4l2_requestbuffers
*p
)
1414 * This is for locking debugging only. I removed spinlocks and now I
1415 * check whether .prepare is ever called on a linked buffer, or whether
1416 * a dma IRQ can occur for an in-work or unlinked buffer. Until now
1417 * it hadn't triggered
1419 for (i
= 0; i
< p
->count
; i
++) {
1420 struct omap1_cam_buf
*buf
= container_of(icd
->vb_vidq
.bufs
[i
],
1421 struct omap1_cam_buf
, vb
);
1423 INIT_LIST_HEAD(&buf
->vb
.queue
);
1429 static int omap1_cam_querycap(struct soc_camera_host
*ici
,
1430 struct v4l2_capability
*cap
)
1432 /* cap->name is set by the friendly caller:-> */
1433 strlcpy(cap
->card
, "OMAP1 Camera", sizeof(cap
->card
));
1434 cap
->capabilities
= V4L2_CAP_VIDEO_CAPTURE
| V4L2_CAP_STREAMING
;
1439 static int omap1_cam_set_bus_param(struct soc_camera_device
*icd
)
1441 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
1442 struct device
*dev
= icd
->parent
;
1443 struct soc_camera_host
*ici
= to_soc_camera_host(dev
);
1444 struct omap1_cam_dev
*pcdev
= ici
->priv
;
1445 u32 pixfmt
= icd
->current_fmt
->host_fmt
->fourcc
;
1446 const struct soc_camera_format_xlate
*xlate
;
1447 const struct soc_mbus_pixelfmt
*fmt
;
1448 struct v4l2_mbus_config cfg
= {.type
= V4L2_MBUS_PARALLEL
,};
1449 unsigned long common_flags
;
1450 u32 ctrlclock
, mode
;
1453 ret
= v4l2_subdev_call(sd
, video
, g_mbus_config
, &cfg
);
1455 common_flags
= soc_mbus_config_compatible(&cfg
, SOCAM_BUS_FLAGS
);
1456 if (!common_flags
) {
1458 "Flags incompatible: camera 0x%x, host 0x%x\n",
1459 cfg
.flags
, SOCAM_BUS_FLAGS
);
1462 } else if (ret
!= -ENOIOCTLCMD
) {
1465 common_flags
= SOCAM_BUS_FLAGS
;
1468 /* Make choices, possibly based on platform configuration */
1469 if ((common_flags
& V4L2_MBUS_PCLK_SAMPLE_RISING
) &&
1470 (common_flags
& V4L2_MBUS_PCLK_SAMPLE_FALLING
)) {
1471 if (!pcdev
->pdata
||
1472 pcdev
->pdata
->flags
& OMAP1_CAMERA_LCLK_RISING
)
1473 common_flags
&= ~V4L2_MBUS_PCLK_SAMPLE_FALLING
;
1475 common_flags
&= ~V4L2_MBUS_PCLK_SAMPLE_RISING
;
1478 cfg
.flags
= common_flags
;
1479 ret
= v4l2_subdev_call(sd
, video
, s_mbus_config
, &cfg
);
1480 if (ret
< 0 && ret
!= -ENOIOCTLCMD
) {
1481 dev_dbg(dev
, "camera s_mbus_config(0x%lx) returned %d\n",
1486 ctrlclock
= CAM_READ_CACHE(pcdev
, CTRLCLOCK
);
1487 if (ctrlclock
& LCLK_EN
)
1488 CAM_WRITE(pcdev
, CTRLCLOCK
, ctrlclock
& ~LCLK_EN
);
1490 if (common_flags
& V4L2_MBUS_PCLK_SAMPLE_RISING
) {
1491 dev_dbg(dev
, "CTRLCLOCK_REG |= POLCLK\n");
1492 ctrlclock
|= POLCLK
;
1494 dev_dbg(dev
, "CTRLCLOCK_REG &= ~POLCLK\n");
1495 ctrlclock
&= ~POLCLK
;
1497 CAM_WRITE(pcdev
, CTRLCLOCK
, ctrlclock
& ~LCLK_EN
);
1499 if (ctrlclock
& LCLK_EN
)
1500 CAM_WRITE(pcdev
, CTRLCLOCK
, ctrlclock
);
1502 /* select bus endianess */
1503 xlate
= soc_camera_xlate_by_fourcc(icd
, pixfmt
);
1504 fmt
= xlate
->host_fmt
;
1506 mode
= CAM_READ(pcdev
, MODE
) & ~(RAZ_FIFO
| IRQ_MASK
| DMA
);
1507 if (fmt
->order
== SOC_MBUS_ORDER_LE
) {
1508 dev_dbg(dev
, "MODE_REG &= ~ORDERCAMD\n");
1509 CAM_WRITE(pcdev
, MODE
, mode
& ~ORDERCAMD
);
1511 dev_dbg(dev
, "MODE_REG |= ORDERCAMD\n");
1512 CAM_WRITE(pcdev
, MODE
, mode
| ORDERCAMD
);
1518 static unsigned int omap1_cam_poll(struct file
*file
, poll_table
*pt
)
1520 struct soc_camera_device
*icd
= file
->private_data
;
1521 struct omap1_cam_buf
*buf
;
1523 buf
= list_entry(icd
->vb_vidq
.stream
.next
, struct omap1_cam_buf
,
1526 poll_wait(file
, &buf
->vb
.done
, pt
);
1528 if (buf
->vb
.state
== VIDEOBUF_DONE
||
1529 buf
->vb
.state
== VIDEOBUF_ERROR
)
1530 return POLLIN
| POLLRDNORM
;
1535 static struct soc_camera_host_ops omap1_host_ops
= {
1536 .owner
= THIS_MODULE
,
1537 .add
= omap1_cam_add_device
,
1538 .remove
= omap1_cam_remove_device
,
1539 .get_formats
= omap1_cam_get_formats
,
1540 .set_crop
= omap1_cam_set_crop
,
1541 .set_fmt
= omap1_cam_set_fmt
,
1542 .try_fmt
= omap1_cam_try_fmt
,
1543 .init_videobuf
= omap1_cam_init_videobuf
,
1544 .reqbufs
= omap1_cam_reqbufs
,
1545 .querycap
= omap1_cam_querycap
,
1546 .set_bus_param
= omap1_cam_set_bus_param
,
1547 .poll
= omap1_cam_poll
,
1550 static int __init
omap1_cam_probe(struct platform_device
*pdev
)
1552 struct omap1_cam_dev
*pcdev
;
1553 struct resource
*res
;
1559 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1560 irq
= platform_get_irq(pdev
, 0);
1561 if (!res
|| (int)irq
<= 0) {
1566 clk
= clk_get(&pdev
->dev
, "armper_ck");
1572 pcdev
= kzalloc(sizeof(*pcdev
) + resource_size(res
), GFP_KERNEL
);
1574 dev_err(&pdev
->dev
, "Could not allocate pcdev\n");
1582 pcdev
->pdata
= pdev
->dev
.platform_data
;
1584 pcdev
->pflags
= pcdev
->pdata
->flags
;
1585 pcdev
->camexclk
= pcdev
->pdata
->camexclk_khz
* 1000;
1588 switch (pcdev
->camexclk
) {
1596 /* pcdev->camexclk != 0 => pcdev->pdata != NULL */
1597 dev_warn(&pdev
->dev
,
1598 "Incorrect sensor clock frequency %ld kHz, "
1599 "should be one of 0, 6, 8, 9.6, 12 or 24 MHz, "
1600 "please correct your platform data\n",
1601 pcdev
->pdata
->camexclk_khz
);
1602 pcdev
->camexclk
= 0;
1604 dev_info(&pdev
->dev
, "Not providing sensor clock\n");
1607 INIT_LIST_HEAD(&pcdev
->capture
);
1608 spin_lock_init(&pcdev
->lock
);
1611 * Request the region.
1613 if (!request_mem_region(res
->start
, resource_size(res
), DRIVER_NAME
)) {
1618 base
= ioremap(res
->start
, resource_size(res
));
1626 sensor_reset(pcdev
, true);
1628 err
= omap_request_dma(OMAP_DMA_CAMERA_IF_RX
, DRIVER_NAME
,
1629 dma_isr
, (void *)pcdev
, &pcdev
->dma_ch
);
1631 dev_err(&pdev
->dev
, "Can't request DMA for OMAP1 Camera\n");
1635 dev_dbg(&pdev
->dev
, "got DMA channel %d\n", pcdev
->dma_ch
);
1637 /* preconfigure DMA */
1638 omap_set_dma_src_params(pcdev
->dma_ch
, OMAP_DMA_PORT_TIPB
,
1639 OMAP_DMA_AMODE_CONSTANT
, res
->start
+ REG_CAMDATA
,
1641 omap_set_dma_dest_burst_mode(pcdev
->dma_ch
, OMAP_DMA_DATA_BURST_4
);
1642 /* setup DMA autoinitialization */
1643 omap_dma_link_lch(pcdev
->dma_ch
, pcdev
->dma_ch
);
1645 err
= request_irq(pcdev
->irq
, cam_isr
, 0, DRIVER_NAME
, pcdev
);
1647 dev_err(&pdev
->dev
, "Camera interrupt register failed\n");
1651 pcdev
->soc_host
.drv_name
= DRIVER_NAME
;
1652 pcdev
->soc_host
.ops
= &omap1_host_ops
;
1653 pcdev
->soc_host
.priv
= pcdev
;
1654 pcdev
->soc_host
.v4l2_dev
.dev
= &pdev
->dev
;
1655 pcdev
->soc_host
.nr
= pdev
->id
;
1657 err
= soc_camera_host_register(&pcdev
->soc_host
);
1661 dev_info(&pdev
->dev
, "OMAP1 Camera Interface driver loaded\n");
1666 free_irq(pcdev
->irq
, pcdev
);
1668 omap_free_dma(pcdev
->dma_ch
);
1672 release_mem_region(res
->start
, resource_size(res
));
1681 static int __exit
omap1_cam_remove(struct platform_device
*pdev
)
1683 struct soc_camera_host
*soc_host
= to_soc_camera_host(&pdev
->dev
);
1684 struct omap1_cam_dev
*pcdev
= container_of(soc_host
,
1685 struct omap1_cam_dev
, soc_host
);
1686 struct resource
*res
;
1688 free_irq(pcdev
->irq
, pcdev
);
1690 omap_free_dma(pcdev
->dma_ch
);
1692 soc_camera_host_unregister(soc_host
);
1694 iounmap(pcdev
->base
);
1697 release_mem_region(res
->start
, resource_size(res
));
1699 clk_put(pcdev
->clk
);
1703 dev_info(&pdev
->dev
, "OMAP1 Camera Interface driver unloaded\n");
1708 static struct platform_driver omap1_cam_driver
= {
1710 .name
= DRIVER_NAME
,
1712 .probe
= omap1_cam_probe
,
1713 .remove
= __exit_p(omap1_cam_remove
),
1716 module_platform_driver(omap1_cam_driver
);
1718 module_param(sg_mode
, bool, 0644);
1719 MODULE_PARM_DESC(sg_mode
, "videobuf mode, 0: dma-contig (default), 1: dma-sg");
1721 MODULE_DESCRIPTION("OMAP1 Camera Interface driver");
1722 MODULE_AUTHOR("Janusz Krzysztofik <jkrzyszt@tis.icnet.pl>");
1723 MODULE_LICENSE("GPL v2");
1724 MODULE_VERSION(DRIVER_VERSION
);
1725 MODULE_ALIAS("platform:" DRIVER_NAME
);