2 * Copyright (c) 2011 Atmel Corporation
3 * Josh Wu, <josh.wu@atmel.com>
5 * Based on previous work by Lars Haring, <lars.haring@atmel.com>
7 * Based on the bttv driver for Bt848 with respective copyright holders
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
14 #include <linux/clk.h>
15 #include <linux/completion.h>
16 #include <linux/delay.h>
18 #include <linux/init.h>
19 #include <linux/interrupt.h>
20 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include <linux/platform_device.h>
23 #include <linux/slab.h>
25 #include <media/atmel-isi.h>
26 #include <media/soc_camera.h>
27 #include <media/soc_mediabus.h>
28 #include <media/videobuf2-dma-contig.h>
30 #define MAX_BUFFER_NUM 32
31 #define MAX_SUPPORT_WIDTH 2048
32 #define MAX_SUPPORT_HEIGHT 2048
33 #define VID_LIMIT_BYTES (16 * 1024 * 1024)
34 #define MIN_FRAME_RATE 15
35 #define FRAME_INTERVAL_MILLI_SEC (1000 / MIN_FRAME_RATE)
37 /* Frame buffer descriptor */
39 /* Physical address of the frame buffer */
41 /* DMA Control Register(only in HISI2) */
43 /* Physical address of the next fbd */
47 static void set_dma_ctrl(struct fbd
*fb_desc
, u32 ctrl
)
49 fb_desc
->dma_ctrl
= ctrl
;
53 struct list_head list
;
58 /* Frame buffer data */
61 struct isi_dma_desc
*p_dma_desc
;
62 struct list_head list
;
66 /* Protects the access of variables shared with the ISR */
72 struct vb2_alloc_ctx
*alloc_ctx
;
74 /* Allocate descriptors for dma buffer use */
75 struct fbd
*p_fb_descriptors
;
76 u32 fb_descriptors_phys
;
77 struct list_head dma_desc_head
;
78 struct isi_dma_desc dma_desc
[MAX_BUFFER_NUM
];
80 struct completion complete
;
81 /* ISI peripherial clock */
83 /* ISI_MCK, feed to camera sensor to generate pixel clock */
87 struct isi_platform_data
*pdata
;
88 u16 width_flags
; /* max 12 bits */
90 struct list_head video_buffer_list
;
91 struct frame_buffer
*active
;
93 struct soc_camera_host soc_host
;
96 static void isi_writel(struct atmel_isi
*isi
, u32 reg
, u32 val
)
98 writel(val
, isi
->regs
+ reg
);
100 static u32
isi_readl(struct atmel_isi
*isi
, u32 reg
)
102 return readl(isi
->regs
+ reg
);
105 static int configure_geometry(struct atmel_isi
*isi
, u32 width
,
106 u32 height
, enum v4l2_mbus_pixelcode code
)
111 /* YUV, including grey */
112 case V4L2_MBUS_FMT_Y8_1X8
:
113 cr
= ISI_CFG2_GRAYSCALE
;
115 case V4L2_MBUS_FMT_VYUY8_2X8
:
116 cr
= ISI_CFG2_YCC_SWAP_MODE_3
;
118 case V4L2_MBUS_FMT_UYVY8_2X8
:
119 cr
= ISI_CFG2_YCC_SWAP_MODE_2
;
121 case V4L2_MBUS_FMT_YVYU8_2X8
:
122 cr
= ISI_CFG2_YCC_SWAP_MODE_1
;
124 case V4L2_MBUS_FMT_YUYV8_2X8
:
125 cr
= ISI_CFG2_YCC_SWAP_DEFAULT
;
132 isi_writel(isi
, ISI_CTRL
, ISI_CTRL_DIS
);
134 cfg2
= isi_readl(isi
, ISI_CFG2
);
135 /* Set YCC swap mode */
136 cfg2
&= ~ISI_CFG2_YCC_SWAP_MODE_MASK
;
139 cfg2
&= ~(ISI_CFG2_IM_HSIZE_MASK
);
140 cfg2
|= ((width
- 1) << ISI_CFG2_IM_HSIZE_OFFSET
) &
141 ISI_CFG2_IM_HSIZE_MASK
;
143 cfg2
&= ~(ISI_CFG2_IM_VSIZE_MASK
);
144 cfg2
|= ((height
- 1) << ISI_CFG2_IM_VSIZE_OFFSET
)
145 & ISI_CFG2_IM_VSIZE_MASK
;
146 isi_writel(isi
, ISI_CFG2
, cfg2
);
151 static irqreturn_t
atmel_isi_handle_streaming(struct atmel_isi
*isi
)
154 struct vb2_buffer
*vb
= &isi
->active
->vb
;
155 struct frame_buffer
*buf
= isi
->active
;
157 list_del_init(&buf
->list
);
158 v4l2_get_timestamp(&vb
->v4l2_buf
.timestamp
);
159 vb
->v4l2_buf
.sequence
= isi
->sequence
++;
160 vb2_buffer_done(vb
, VB2_BUF_STATE_DONE
);
163 if (list_empty(&isi
->video_buffer_list
)) {
166 /* start next dma frame. */
167 isi
->active
= list_entry(isi
->video_buffer_list
.next
,
168 struct frame_buffer
, list
);
169 isi_writel(isi
, ISI_DMA_C_DSCR
,
170 isi
->active
->p_dma_desc
->fbd_phys
);
171 isi_writel(isi
, ISI_DMA_C_CTRL
,
172 ISI_DMA_CTRL_FETCH
| ISI_DMA_CTRL_DONE
);
173 isi_writel(isi
, ISI_DMA_CHER
, ISI_DMA_CHSR_C_CH
);
178 /* ISI interrupt service routine */
179 static irqreturn_t
isi_interrupt(int irq
, void *dev_id
)
181 struct atmel_isi
*isi
= dev_id
;
182 u32 status
, mask
, pending
;
183 irqreturn_t ret
= IRQ_NONE
;
185 spin_lock(&isi
->lock
);
187 status
= isi_readl(isi
, ISI_STATUS
);
188 mask
= isi_readl(isi
, ISI_INTMASK
);
189 pending
= status
& mask
;
191 if (pending
& ISI_CTRL_SRST
) {
192 complete(&isi
->complete
);
193 isi_writel(isi
, ISI_INTDIS
, ISI_CTRL_SRST
);
195 } else if (pending
& ISI_CTRL_DIS
) {
196 complete(&isi
->complete
);
197 isi_writel(isi
, ISI_INTDIS
, ISI_CTRL_DIS
);
200 if (likely(pending
& ISI_SR_CXFR_DONE
))
201 ret
= atmel_isi_handle_streaming(isi
);
204 spin_unlock(&isi
->lock
);
208 #define WAIT_ISI_RESET 1
209 #define WAIT_ISI_DISABLE 0
210 static int atmel_isi_wait_status(struct atmel_isi
*isi
, int wait_reset
)
212 unsigned long timeout
;
214 * The reset or disable will only succeed if we have a
215 * pixel clock from the camera.
217 init_completion(&isi
->complete
);
220 isi_writel(isi
, ISI_INTEN
, ISI_CTRL_SRST
);
221 isi_writel(isi
, ISI_CTRL
, ISI_CTRL_SRST
);
223 isi_writel(isi
, ISI_INTEN
, ISI_CTRL_DIS
);
224 isi_writel(isi
, ISI_CTRL
, ISI_CTRL_DIS
);
227 timeout
= wait_for_completion_timeout(&isi
->complete
,
228 msecs_to_jiffies(100));
235 /* ------------------------------------------------------------------
237 ------------------------------------------------------------------*/
238 static int queue_setup(struct vb2_queue
*vq
, const struct v4l2_format
*fmt
,
239 unsigned int *nbuffers
, unsigned int *nplanes
,
240 unsigned int sizes
[], void *alloc_ctxs
[])
242 struct soc_camera_device
*icd
= soc_camera_from_vb2q(vq
);
243 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
244 struct atmel_isi
*isi
= ici
->priv
;
247 size
= icd
->sizeimage
;
249 if (!*nbuffers
|| *nbuffers
> MAX_BUFFER_NUM
)
250 *nbuffers
= MAX_BUFFER_NUM
;
252 if (size
* *nbuffers
> VID_LIMIT_BYTES
)
253 *nbuffers
= VID_LIMIT_BYTES
/ size
;
257 alloc_ctxs
[0] = isi
->alloc_ctx
;
262 dev_dbg(icd
->parent
, "%s, count=%d, size=%ld\n", __func__
,
268 static int buffer_init(struct vb2_buffer
*vb
)
270 struct frame_buffer
*buf
= container_of(vb
, struct frame_buffer
, vb
);
272 buf
->p_dma_desc
= NULL
;
273 INIT_LIST_HEAD(&buf
->list
);
278 static int buffer_prepare(struct vb2_buffer
*vb
)
280 struct soc_camera_device
*icd
= soc_camera_from_vb2q(vb
->vb2_queue
);
281 struct frame_buffer
*buf
= container_of(vb
, struct frame_buffer
, vb
);
282 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
283 struct atmel_isi
*isi
= ici
->priv
;
285 struct isi_dma_desc
*desc
;
287 size
= icd
->sizeimage
;
289 if (vb2_plane_size(vb
, 0) < size
) {
290 dev_err(icd
->parent
, "%s data will not fit into plane (%lu < %lu)\n",
291 __func__
, vb2_plane_size(vb
, 0), size
);
295 vb2_set_plane_payload(&buf
->vb
, 0, size
);
297 if (!buf
->p_dma_desc
) {
298 if (list_empty(&isi
->dma_desc_head
)) {
299 dev_err(icd
->parent
, "Not enough dma descriptors.\n");
302 /* Get an available descriptor */
303 desc
= list_entry(isi
->dma_desc_head
.next
,
304 struct isi_dma_desc
, list
);
305 /* Delete the descriptor since now it is used */
306 list_del_init(&desc
->list
);
308 /* Initialize the dma descriptor */
309 desc
->p_fbd
->fb_address
=
310 vb2_dma_contig_plane_dma_addr(vb
, 0);
311 desc
->p_fbd
->next_fbd_address
= 0;
312 set_dma_ctrl(desc
->p_fbd
, ISI_DMA_CTRL_WB
);
314 buf
->p_dma_desc
= desc
;
320 static void buffer_cleanup(struct vb2_buffer
*vb
)
322 struct soc_camera_device
*icd
= soc_camera_from_vb2q(vb
->vb2_queue
);
323 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
324 struct atmel_isi
*isi
= ici
->priv
;
325 struct frame_buffer
*buf
= container_of(vb
, struct frame_buffer
, vb
);
327 /* This descriptor is available now and we add to head list */
329 list_add(&buf
->p_dma_desc
->list
, &isi
->dma_desc_head
);
332 static void start_dma(struct atmel_isi
*isi
, struct frame_buffer
*buffer
)
336 cfg1
= isi_readl(isi
, ISI_CFG1
);
337 /* Enable irq: cxfr for the codec path, pxfr for the preview path */
338 isi_writel(isi
, ISI_INTEN
,
339 ISI_SR_CXFR_DONE
| ISI_SR_PXFR_DONE
);
341 /* Check if already in a frame */
342 if (isi_readl(isi
, ISI_STATUS
) & ISI_CTRL_CDC
) {
343 dev_err(isi
->soc_host
.icd
->parent
, "Already in frame handling.\n");
347 isi_writel(isi
, ISI_DMA_C_DSCR
, buffer
->p_dma_desc
->fbd_phys
);
348 isi_writel(isi
, ISI_DMA_C_CTRL
, ISI_DMA_CTRL_FETCH
| ISI_DMA_CTRL_DONE
);
349 isi_writel(isi
, ISI_DMA_CHER
, ISI_DMA_CHSR_C_CH
);
351 cfg1
&= ~ISI_CFG1_FRATE_DIV_MASK
;
352 /* Enable linked list */
353 cfg1
|= isi
->pdata
->frate
| ISI_CFG1_DISCR
;
355 /* Enable codec path and ISI */
356 ctrl
= ISI_CTRL_CDC
| ISI_CTRL_EN
;
357 isi_writel(isi
, ISI_CTRL
, ctrl
);
358 isi_writel(isi
, ISI_CFG1
, cfg1
);
361 static void buffer_queue(struct vb2_buffer
*vb
)
363 struct soc_camera_device
*icd
= soc_camera_from_vb2q(vb
->vb2_queue
);
364 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
365 struct atmel_isi
*isi
= ici
->priv
;
366 struct frame_buffer
*buf
= container_of(vb
, struct frame_buffer
, vb
);
367 unsigned long flags
= 0;
369 spin_lock_irqsave(&isi
->lock
, flags
);
370 list_add_tail(&buf
->list
, &isi
->video_buffer_list
);
372 if (isi
->active
== NULL
) {
374 if (vb2_is_streaming(vb
->vb2_queue
))
377 spin_unlock_irqrestore(&isi
->lock
, flags
);
380 static int start_streaming(struct vb2_queue
*vq
, unsigned int count
)
382 struct soc_camera_device
*icd
= soc_camera_from_vb2q(vq
);
383 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
384 struct atmel_isi
*isi
= ici
->priv
;
389 ret
= atmel_isi_wait_status(isi
, WAIT_ISI_RESET
);
391 dev_err(icd
->parent
, "Reset ISI timed out\n");
394 /* Disable all interrupts */
395 isi_writel(isi
, ISI_INTDIS
, ~0UL);
397 spin_lock_irq(&isi
->lock
);
398 /* Clear any pending interrupt */
399 sr
= isi_readl(isi
, ISI_STATUS
);
402 start_dma(isi
, isi
->active
);
403 spin_unlock_irq(&isi
->lock
);
408 /* abort streaming and wait for last buffer */
409 static int stop_streaming(struct vb2_queue
*vq
)
411 struct soc_camera_device
*icd
= soc_camera_from_vb2q(vq
);
412 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
413 struct atmel_isi
*isi
= ici
->priv
;
414 struct frame_buffer
*buf
, *node
;
416 unsigned long timeout
;
418 spin_lock_irq(&isi
->lock
);
420 /* Release all active buffers */
421 list_for_each_entry_safe(buf
, node
, &isi
->video_buffer_list
, list
) {
422 list_del_init(&buf
->list
);
423 vb2_buffer_done(&buf
->vb
, VB2_BUF_STATE_ERROR
);
425 spin_unlock_irq(&isi
->lock
);
427 timeout
= jiffies
+ FRAME_INTERVAL_MILLI_SEC
* HZ
;
428 /* Wait until the end of the current frame. */
429 while ((isi_readl(isi
, ISI_STATUS
) & ISI_CTRL_CDC
) &&
430 time_before(jiffies
, timeout
))
433 if (time_after(jiffies
, timeout
)) {
435 "Timeout waiting for finishing codec request\n");
439 /* Disable interrupts */
440 isi_writel(isi
, ISI_INTDIS
,
441 ISI_SR_CXFR_DONE
| ISI_SR_PXFR_DONE
);
443 /* Disable ISI and wait for it is done */
444 ret
= atmel_isi_wait_status(isi
, WAIT_ISI_DISABLE
);
446 dev_err(icd
->parent
, "Disable ISI timed out\n");
451 static struct vb2_ops isi_video_qops
= {
452 .queue_setup
= queue_setup
,
453 .buf_init
= buffer_init
,
454 .buf_prepare
= buffer_prepare
,
455 .buf_cleanup
= buffer_cleanup
,
456 .buf_queue
= buffer_queue
,
457 .start_streaming
= start_streaming
,
458 .stop_streaming
= stop_streaming
,
459 .wait_prepare
= soc_camera_unlock
,
460 .wait_finish
= soc_camera_lock
,
463 /* ------------------------------------------------------------------
464 SOC camera operations for the device
465 ------------------------------------------------------------------*/
466 static int isi_camera_init_videobuf(struct vb2_queue
*q
,
467 struct soc_camera_device
*icd
)
469 q
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
470 q
->io_modes
= VB2_MMAP
;
472 q
->buf_struct_size
= sizeof(struct frame_buffer
);
473 q
->ops
= &isi_video_qops
;
474 q
->mem_ops
= &vb2_dma_contig_memops
;
475 q
->timestamp_type
= V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC
;
477 return vb2_queue_init(q
);
480 static int isi_camera_set_fmt(struct soc_camera_device
*icd
,
481 struct v4l2_format
*f
)
483 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
484 struct atmel_isi
*isi
= ici
->priv
;
485 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
486 const struct soc_camera_format_xlate
*xlate
;
487 struct v4l2_pix_format
*pix
= &f
->fmt
.pix
;
488 struct v4l2_mbus_framefmt mf
;
491 xlate
= soc_camera_xlate_by_fourcc(icd
, pix
->pixelformat
);
493 dev_warn(icd
->parent
, "Format %x not found\n",
498 dev_dbg(icd
->parent
, "Plan to set format %dx%d\n",
499 pix
->width
, pix
->height
);
501 mf
.width
= pix
->width
;
502 mf
.height
= pix
->height
;
503 mf
.field
= pix
->field
;
504 mf
.colorspace
= pix
->colorspace
;
505 mf
.code
= xlate
->code
;
507 ret
= v4l2_subdev_call(sd
, video
, s_mbus_fmt
, &mf
);
511 if (mf
.code
!= xlate
->code
)
514 ret
= configure_geometry(isi
, pix
->width
, pix
->height
, xlate
->code
);
518 pix
->width
= mf
.width
;
519 pix
->height
= mf
.height
;
520 pix
->field
= mf
.field
;
521 pix
->colorspace
= mf
.colorspace
;
522 icd
->current_fmt
= xlate
;
524 dev_dbg(icd
->parent
, "Finally set format %dx%d\n",
525 pix
->width
, pix
->height
);
530 static int isi_camera_try_fmt(struct soc_camera_device
*icd
,
531 struct v4l2_format
*f
)
533 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
534 const struct soc_camera_format_xlate
*xlate
;
535 struct v4l2_pix_format
*pix
= &f
->fmt
.pix
;
536 struct v4l2_mbus_framefmt mf
;
537 u32 pixfmt
= pix
->pixelformat
;
540 xlate
= soc_camera_xlate_by_fourcc(icd
, pixfmt
);
541 if (pixfmt
&& !xlate
) {
542 dev_warn(icd
->parent
, "Format %x not found\n", pixfmt
);
546 /* limit to Atmel ISI hardware capabilities */
547 if (pix
->height
> MAX_SUPPORT_HEIGHT
)
548 pix
->height
= MAX_SUPPORT_HEIGHT
;
549 if (pix
->width
> MAX_SUPPORT_WIDTH
)
550 pix
->width
= MAX_SUPPORT_WIDTH
;
552 /* limit to sensor capabilities */
553 mf
.width
= pix
->width
;
554 mf
.height
= pix
->height
;
555 mf
.field
= pix
->field
;
556 mf
.colorspace
= pix
->colorspace
;
557 mf
.code
= xlate
->code
;
559 ret
= v4l2_subdev_call(sd
, video
, try_mbus_fmt
, &mf
);
563 pix
->width
= mf
.width
;
564 pix
->height
= mf
.height
;
565 pix
->colorspace
= mf
.colorspace
;
569 pix
->field
= V4L2_FIELD_NONE
;
571 case V4L2_FIELD_NONE
:
574 dev_err(icd
->parent
, "Field type %d unsupported.\n",
582 static const struct soc_mbus_pixelfmt isi_camera_formats
[] = {
584 .fourcc
= V4L2_PIX_FMT_YUYV
,
585 .name
= "Packed YUV422 16 bit",
586 .bits_per_sample
= 8,
587 .packing
= SOC_MBUS_PACKING_2X8_PADHI
,
588 .order
= SOC_MBUS_ORDER_LE
,
589 .layout
= SOC_MBUS_LAYOUT_PACKED
,
593 /* This will be corrected as we get more formats */
594 static bool isi_camera_packing_supported(const struct soc_mbus_pixelfmt
*fmt
)
596 return fmt
->packing
== SOC_MBUS_PACKING_NONE
||
597 (fmt
->bits_per_sample
== 8 &&
598 fmt
->packing
== SOC_MBUS_PACKING_2X8_PADHI
) ||
599 (fmt
->bits_per_sample
> 8 &&
600 fmt
->packing
== SOC_MBUS_PACKING_EXTEND16
);
603 #define ISI_BUS_PARAM (V4L2_MBUS_MASTER | \
604 V4L2_MBUS_HSYNC_ACTIVE_HIGH | \
605 V4L2_MBUS_HSYNC_ACTIVE_LOW | \
606 V4L2_MBUS_VSYNC_ACTIVE_HIGH | \
607 V4L2_MBUS_VSYNC_ACTIVE_LOW | \
608 V4L2_MBUS_PCLK_SAMPLE_RISING | \
609 V4L2_MBUS_PCLK_SAMPLE_FALLING | \
610 V4L2_MBUS_DATA_ACTIVE_HIGH)
612 static int isi_camera_try_bus_param(struct soc_camera_device
*icd
,
613 unsigned char buswidth
)
615 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
616 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
617 struct atmel_isi
*isi
= ici
->priv
;
618 struct v4l2_mbus_config cfg
= {.type
= V4L2_MBUS_PARALLEL
,};
619 unsigned long common_flags
;
622 ret
= v4l2_subdev_call(sd
, video
, g_mbus_config
, &cfg
);
624 common_flags
= soc_mbus_config_compatible(&cfg
,
627 dev_warn(icd
->parent
,
628 "Flags incompatible: camera 0x%x, host 0x%x\n",
629 cfg
.flags
, ISI_BUS_PARAM
);
632 } else if (ret
!= -ENOIOCTLCMD
) {
636 if ((1 << (buswidth
- 1)) & isi
->width_flags
)
642 static int isi_camera_get_formats(struct soc_camera_device
*icd
,
644 struct soc_camera_format_xlate
*xlate
)
646 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
647 int formats
= 0, ret
;
649 enum v4l2_mbus_pixelcode code
;
650 /* soc camera host format */
651 const struct soc_mbus_pixelfmt
*fmt
;
653 ret
= v4l2_subdev_call(sd
, video
, enum_mbus_fmt
, idx
, &code
);
655 /* No more formats */
658 fmt
= soc_mbus_get_fmtdesc(code
);
661 "Invalid format code #%u: %d\n", idx
, code
);
665 /* This also checks support for the requested bits-per-sample */
666 ret
= isi_camera_try_bus_param(icd
, fmt
->bits_per_sample
);
669 "Fail to try the bus parameters.\n");
674 case V4L2_MBUS_FMT_UYVY8_2X8
:
675 case V4L2_MBUS_FMT_VYUY8_2X8
:
676 case V4L2_MBUS_FMT_YUYV8_2X8
:
677 case V4L2_MBUS_FMT_YVYU8_2X8
:
680 xlate
->host_fmt
= &isi_camera_formats
[0];
683 dev_dbg(icd
->parent
, "Providing format %s using code %d\n",
684 isi_camera_formats
[0].name
, code
);
688 if (!isi_camera_packing_supported(fmt
))
692 "Providing format %s in pass-through mode\n",
696 /* Generic pass-through */
699 xlate
->host_fmt
= fmt
;
707 static int isi_camera_add_device(struct soc_camera_device
*icd
)
709 dev_dbg(icd
->parent
, "Atmel ISI Camera driver attached to camera %d\n",
715 static void isi_camera_remove_device(struct soc_camera_device
*icd
)
717 dev_dbg(icd
->parent
, "Atmel ISI Camera driver detached from camera %d\n",
721 /* Called with .host_lock held */
722 static int isi_camera_clock_start(struct soc_camera_host
*ici
)
724 struct atmel_isi
*isi
= ici
->priv
;
727 ret
= clk_prepare_enable(isi
->pclk
);
731 if (!IS_ERR(isi
->mck
)) {
732 ret
= clk_prepare_enable(isi
->mck
);
734 clk_disable_unprepare(isi
->pclk
);
742 /* Called with .host_lock held */
743 static void isi_camera_clock_stop(struct soc_camera_host
*ici
)
745 struct atmel_isi
*isi
= ici
->priv
;
747 if (!IS_ERR(isi
->mck
))
748 clk_disable_unprepare(isi
->mck
);
749 clk_disable_unprepare(isi
->pclk
);
752 static unsigned int isi_camera_poll(struct file
*file
, poll_table
*pt
)
754 struct soc_camera_device
*icd
= file
->private_data
;
756 return vb2_poll(&icd
->vb2_vidq
, file
, pt
);
759 static int isi_camera_querycap(struct soc_camera_host
*ici
,
760 struct v4l2_capability
*cap
)
762 strcpy(cap
->driver
, "atmel-isi");
763 strcpy(cap
->card
, "Atmel Image Sensor Interface");
764 cap
->capabilities
= (V4L2_CAP_VIDEO_CAPTURE
|
769 static int isi_camera_set_bus_param(struct soc_camera_device
*icd
)
771 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
772 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
773 struct atmel_isi
*isi
= ici
->priv
;
774 struct v4l2_mbus_config cfg
= {.type
= V4L2_MBUS_PARALLEL
,};
775 unsigned long common_flags
;
779 ret
= v4l2_subdev_call(sd
, video
, g_mbus_config
, &cfg
);
781 common_flags
= soc_mbus_config_compatible(&cfg
,
784 dev_warn(icd
->parent
,
785 "Flags incompatible: camera 0x%x, host 0x%x\n",
786 cfg
.flags
, ISI_BUS_PARAM
);
789 } else if (ret
!= -ENOIOCTLCMD
) {
792 common_flags
= ISI_BUS_PARAM
;
794 dev_dbg(icd
->parent
, "Flags cam: 0x%x host: 0x%x common: 0x%lx\n",
795 cfg
.flags
, ISI_BUS_PARAM
, common_flags
);
797 /* Make choises, based on platform preferences */
798 if ((common_flags
& V4L2_MBUS_HSYNC_ACTIVE_HIGH
) &&
799 (common_flags
& V4L2_MBUS_HSYNC_ACTIVE_LOW
)) {
800 if (isi
->pdata
->hsync_act_low
)
801 common_flags
&= ~V4L2_MBUS_HSYNC_ACTIVE_HIGH
;
803 common_flags
&= ~V4L2_MBUS_HSYNC_ACTIVE_LOW
;
806 if ((common_flags
& V4L2_MBUS_VSYNC_ACTIVE_HIGH
) &&
807 (common_flags
& V4L2_MBUS_VSYNC_ACTIVE_LOW
)) {
808 if (isi
->pdata
->vsync_act_low
)
809 common_flags
&= ~V4L2_MBUS_VSYNC_ACTIVE_HIGH
;
811 common_flags
&= ~V4L2_MBUS_VSYNC_ACTIVE_LOW
;
814 if ((common_flags
& V4L2_MBUS_PCLK_SAMPLE_RISING
) &&
815 (common_flags
& V4L2_MBUS_PCLK_SAMPLE_FALLING
)) {
816 if (isi
->pdata
->pclk_act_falling
)
817 common_flags
&= ~V4L2_MBUS_PCLK_SAMPLE_RISING
;
819 common_flags
&= ~V4L2_MBUS_PCLK_SAMPLE_FALLING
;
822 cfg
.flags
= common_flags
;
823 ret
= v4l2_subdev_call(sd
, video
, s_mbus_config
, &cfg
);
824 if (ret
< 0 && ret
!= -ENOIOCTLCMD
) {
825 dev_dbg(icd
->parent
, "camera s_mbus_config(0x%lx) returned %d\n",
830 /* set bus param for ISI */
831 if (common_flags
& V4L2_MBUS_HSYNC_ACTIVE_LOW
)
832 cfg1
|= ISI_CFG1_HSYNC_POL_ACTIVE_LOW
;
833 if (common_flags
& V4L2_MBUS_VSYNC_ACTIVE_LOW
)
834 cfg1
|= ISI_CFG1_VSYNC_POL_ACTIVE_LOW
;
835 if (common_flags
& V4L2_MBUS_PCLK_SAMPLE_FALLING
)
836 cfg1
|= ISI_CFG1_PIXCLK_POL_ACTIVE_FALLING
;
838 if (isi
->pdata
->has_emb_sync
)
839 cfg1
|= ISI_CFG1_EMB_SYNC
;
840 if (isi
->pdata
->full_mode
)
841 cfg1
|= ISI_CFG1_FULL_MODE
;
843 isi_writel(isi
, ISI_CTRL
, ISI_CTRL_DIS
);
844 isi_writel(isi
, ISI_CFG1
, cfg1
);
849 static struct soc_camera_host_ops isi_soc_camera_host_ops
= {
850 .owner
= THIS_MODULE
,
851 .add
= isi_camera_add_device
,
852 .remove
= isi_camera_remove_device
,
853 .clock_start
= isi_camera_clock_start
,
854 .clock_stop
= isi_camera_clock_stop
,
855 .set_fmt
= isi_camera_set_fmt
,
856 .try_fmt
= isi_camera_try_fmt
,
857 .get_formats
= isi_camera_get_formats
,
858 .init_videobuf2
= isi_camera_init_videobuf
,
859 .poll
= isi_camera_poll
,
860 .querycap
= isi_camera_querycap
,
861 .set_bus_param
= isi_camera_set_bus_param
,
864 /* -----------------------------------------------------------------------*/
865 static int atmel_isi_remove(struct platform_device
*pdev
)
867 struct soc_camera_host
*soc_host
= to_soc_camera_host(&pdev
->dev
);
868 struct atmel_isi
*isi
= container_of(soc_host
,
869 struct atmel_isi
, soc_host
);
871 soc_camera_host_unregister(soc_host
);
872 vb2_dma_contig_cleanup_ctx(isi
->alloc_ctx
);
873 dma_free_coherent(&pdev
->dev
,
874 sizeof(struct fbd
) * MAX_BUFFER_NUM
,
875 isi
->p_fb_descriptors
,
876 isi
->fb_descriptors_phys
);
881 static int atmel_isi_probe(struct platform_device
*pdev
)
884 struct atmel_isi
*isi
;
885 struct resource
*regs
;
887 struct device
*dev
= &pdev
->dev
;
888 struct soc_camera_host
*soc_host
;
889 struct isi_platform_data
*pdata
;
891 pdata
= dev
->platform_data
;
892 if (!pdata
|| !pdata
->data_width_flags
) {
894 "No config available for Atmel ISI\n");
898 isi
= devm_kzalloc(&pdev
->dev
, sizeof(struct atmel_isi
), GFP_KERNEL
);
900 dev_err(&pdev
->dev
, "Can't allocate interface!\n");
904 isi
->pclk
= devm_clk_get(&pdev
->dev
, "isi_clk");
905 if (IS_ERR(isi
->pclk
))
906 return PTR_ERR(isi
->pclk
);
910 spin_lock_init(&isi
->lock
);
911 INIT_LIST_HEAD(&isi
->video_buffer_list
);
912 INIT_LIST_HEAD(&isi
->dma_desc_head
);
914 /* ISI_MCK is the sensor master clock. It should be handled by the
915 * sensor driver directly, as the ISI has no use for that clock. Make
916 * the clock optional here while platforms transition to the correct
919 isi
->mck
= devm_clk_get(dev
, "isi_mck");
920 if (!IS_ERR(isi
->mck
)) {
921 /* Set ISI_MCK's frequency, it should be faster than pixel
924 ret
= clk_set_rate(isi
->mck
, pdata
->mck_hz
);
929 isi
->p_fb_descriptors
= dma_alloc_coherent(&pdev
->dev
,
930 sizeof(struct fbd
) * MAX_BUFFER_NUM
,
931 &isi
->fb_descriptors_phys
,
933 if (!isi
->p_fb_descriptors
) {
934 dev_err(&pdev
->dev
, "Can't allocate descriptors!\n");
938 for (i
= 0; i
< MAX_BUFFER_NUM
; i
++) {
939 isi
->dma_desc
[i
].p_fbd
= isi
->p_fb_descriptors
+ i
;
940 isi
->dma_desc
[i
].fbd_phys
= isi
->fb_descriptors_phys
+
941 i
* sizeof(struct fbd
);
942 list_add(&isi
->dma_desc
[i
].list
, &isi
->dma_desc_head
);
945 isi
->alloc_ctx
= vb2_dma_contig_init_ctx(&pdev
->dev
);
946 if (IS_ERR(isi
->alloc_ctx
)) {
947 ret
= PTR_ERR(isi
->alloc_ctx
);
951 regs
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
952 isi
->regs
= devm_ioremap_resource(&pdev
->dev
, regs
);
953 if (IS_ERR(isi
->regs
)) {
954 ret
= PTR_ERR(isi
->regs
);
958 if (pdata
->data_width_flags
& ISI_DATAWIDTH_8
)
959 isi
->width_flags
= 1 << 7;
960 if (pdata
->data_width_flags
& ISI_DATAWIDTH_10
)
961 isi
->width_flags
|= 1 << 9;
963 isi_writel(isi
, ISI_CTRL
, ISI_CTRL_DIS
);
965 irq
= platform_get_irq(pdev
, 0);
966 if (IS_ERR_VALUE(irq
)) {
971 ret
= devm_request_irq(&pdev
->dev
, irq
, isi_interrupt
, 0, "isi", isi
);
973 dev_err(&pdev
->dev
, "Unable to request irq %d\n", irq
);
978 soc_host
= &isi
->soc_host
;
979 soc_host
->drv_name
= "isi-camera";
980 soc_host
->ops
= &isi_soc_camera_host_ops
;
981 soc_host
->priv
= isi
;
982 soc_host
->v4l2_dev
.dev
= &pdev
->dev
;
983 soc_host
->nr
= pdev
->id
;
985 ret
= soc_camera_host_register(soc_host
);
987 dev_err(&pdev
->dev
, "Unable to register soc camera host\n");
988 goto err_register_soc_camera_host
;
992 err_register_soc_camera_host
:
995 vb2_dma_contig_cleanup_ctx(isi
->alloc_ctx
);
997 dma_free_coherent(&pdev
->dev
,
998 sizeof(struct fbd
) * MAX_BUFFER_NUM
,
999 isi
->p_fb_descriptors
,
1000 isi
->fb_descriptors_phys
);
1005 static struct platform_driver atmel_isi_driver
= {
1006 .remove
= atmel_isi_remove
,
1008 .name
= "atmel_isi",
1009 .owner
= THIS_MODULE
,
1013 module_platform_driver_probe(atmel_isi_driver
, atmel_isi_probe
);
1015 MODULE_AUTHOR("Josh Wu <josh.wu@atmel.com>");
1016 MODULE_DESCRIPTION("The V4L2 driver for Atmel Linux");
1017 MODULE_LICENSE("GPL");
1018 MODULE_SUPPORTED_DEVICE("video");