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/v4l2-of.h>
29 #include <media/videobuf2-dma-contig.h>
31 #define MAX_BUFFER_NUM 32
32 #define MAX_SUPPORT_WIDTH 2048
33 #define MAX_SUPPORT_HEIGHT 2048
34 #define VID_LIMIT_BYTES (16 * 1024 * 1024)
35 #define MIN_FRAME_RATE 15
36 #define FRAME_INTERVAL_MILLI_SEC (1000 / MIN_FRAME_RATE)
37 #define ISI_DEFAULT_MCLK_FREQ 25000000
39 /* Frame buffer descriptor */
41 /* Physical address of the frame buffer */
43 /* DMA Control Register(only in HISI2) */
45 /* Physical address of the next fbd */
49 static void set_dma_ctrl(struct fbd
*fb_desc
, u32 ctrl
)
51 fb_desc
->dma_ctrl
= ctrl
;
55 struct list_head list
;
60 /* Frame buffer data */
63 struct isi_dma_desc
*p_dma_desc
;
64 struct list_head list
;
68 /* Protects the access of variables shared with the ISR */
74 struct vb2_alloc_ctx
*alloc_ctx
;
76 /* Allocate descriptors for dma buffer use */
77 struct fbd
*p_fb_descriptors
;
78 dma_addr_t fb_descriptors_phys
;
79 struct list_head dma_desc_head
;
80 struct isi_dma_desc dma_desc
[MAX_BUFFER_NUM
];
82 struct completion complete
;
83 /* ISI peripherial clock */
85 /* ISI_MCK, feed to camera sensor to generate pixel clock */
89 struct isi_platform_data pdata
;
90 u16 width_flags
; /* max 12 bits */
92 struct list_head video_buffer_list
;
93 struct frame_buffer
*active
;
95 struct soc_camera_host soc_host
;
98 static void isi_writel(struct atmel_isi
*isi
, u32 reg
, u32 val
)
100 writel(val
, isi
->regs
+ reg
);
102 static u32
isi_readl(struct atmel_isi
*isi
, u32 reg
)
104 return readl(isi
->regs
+ reg
);
107 static int configure_geometry(struct atmel_isi
*isi
, u32 width
,
108 u32 height
, enum v4l2_mbus_pixelcode code
)
113 /* YUV, including grey */
114 case V4L2_MBUS_FMT_Y8_1X8
:
115 cr
= ISI_CFG2_GRAYSCALE
;
117 case V4L2_MBUS_FMT_VYUY8_2X8
:
118 cr
= ISI_CFG2_YCC_SWAP_MODE_3
;
120 case V4L2_MBUS_FMT_UYVY8_2X8
:
121 cr
= ISI_CFG2_YCC_SWAP_MODE_2
;
123 case V4L2_MBUS_FMT_YVYU8_2X8
:
124 cr
= ISI_CFG2_YCC_SWAP_MODE_1
;
126 case V4L2_MBUS_FMT_YUYV8_2X8
:
127 cr
= ISI_CFG2_YCC_SWAP_DEFAULT
;
134 isi_writel(isi
, ISI_CTRL
, ISI_CTRL_DIS
);
136 cfg2
= isi_readl(isi
, ISI_CFG2
);
137 /* Set YCC swap mode */
138 cfg2
&= ~ISI_CFG2_YCC_SWAP_MODE_MASK
;
141 cfg2
&= ~(ISI_CFG2_IM_HSIZE_MASK
);
142 cfg2
|= ((width
- 1) << ISI_CFG2_IM_HSIZE_OFFSET
) &
143 ISI_CFG2_IM_HSIZE_MASK
;
145 cfg2
&= ~(ISI_CFG2_IM_VSIZE_MASK
);
146 cfg2
|= ((height
- 1) << ISI_CFG2_IM_VSIZE_OFFSET
)
147 & ISI_CFG2_IM_VSIZE_MASK
;
148 isi_writel(isi
, ISI_CFG2
, cfg2
);
153 static irqreturn_t
atmel_isi_handle_streaming(struct atmel_isi
*isi
)
156 struct vb2_buffer
*vb
= &isi
->active
->vb
;
157 struct frame_buffer
*buf
= isi
->active
;
159 list_del_init(&buf
->list
);
160 v4l2_get_timestamp(&vb
->v4l2_buf
.timestamp
);
161 vb
->v4l2_buf
.sequence
= isi
->sequence
++;
162 vb2_buffer_done(vb
, VB2_BUF_STATE_DONE
);
165 if (list_empty(&isi
->video_buffer_list
)) {
168 /* start next dma frame. */
169 isi
->active
= list_entry(isi
->video_buffer_list
.next
,
170 struct frame_buffer
, list
);
171 isi_writel(isi
, ISI_DMA_C_DSCR
,
172 (u32
)isi
->active
->p_dma_desc
->fbd_phys
);
173 isi_writel(isi
, ISI_DMA_C_CTRL
,
174 ISI_DMA_CTRL_FETCH
| ISI_DMA_CTRL_DONE
);
175 isi_writel(isi
, ISI_DMA_CHER
, ISI_DMA_CHSR_C_CH
);
180 /* ISI interrupt service routine */
181 static irqreturn_t
isi_interrupt(int irq
, void *dev_id
)
183 struct atmel_isi
*isi
= dev_id
;
184 u32 status
, mask
, pending
;
185 irqreturn_t ret
= IRQ_NONE
;
187 spin_lock(&isi
->lock
);
189 status
= isi_readl(isi
, ISI_STATUS
);
190 mask
= isi_readl(isi
, ISI_INTMASK
);
191 pending
= status
& mask
;
193 if (pending
& ISI_CTRL_SRST
) {
194 complete(&isi
->complete
);
195 isi_writel(isi
, ISI_INTDIS
, ISI_CTRL_SRST
);
197 } else if (pending
& ISI_CTRL_DIS
) {
198 complete(&isi
->complete
);
199 isi_writel(isi
, ISI_INTDIS
, ISI_CTRL_DIS
);
202 if (likely(pending
& ISI_SR_CXFR_DONE
))
203 ret
= atmel_isi_handle_streaming(isi
);
206 spin_unlock(&isi
->lock
);
210 #define WAIT_ISI_RESET 1
211 #define WAIT_ISI_DISABLE 0
212 static int atmel_isi_wait_status(struct atmel_isi
*isi
, int wait_reset
)
214 unsigned long timeout
;
216 * The reset or disable will only succeed if we have a
217 * pixel clock from the camera.
219 init_completion(&isi
->complete
);
222 isi_writel(isi
, ISI_INTEN
, ISI_CTRL_SRST
);
223 isi_writel(isi
, ISI_CTRL
, ISI_CTRL_SRST
);
225 isi_writel(isi
, ISI_INTEN
, ISI_CTRL_DIS
);
226 isi_writel(isi
, ISI_CTRL
, ISI_CTRL_DIS
);
229 timeout
= wait_for_completion_timeout(&isi
->complete
,
230 msecs_to_jiffies(100));
237 /* ------------------------------------------------------------------
239 ------------------------------------------------------------------*/
240 static int queue_setup(struct vb2_queue
*vq
, const struct v4l2_format
*fmt
,
241 unsigned int *nbuffers
, unsigned int *nplanes
,
242 unsigned int sizes
[], void *alloc_ctxs
[])
244 struct soc_camera_device
*icd
= soc_camera_from_vb2q(vq
);
245 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
246 struct atmel_isi
*isi
= ici
->priv
;
249 size
= icd
->sizeimage
;
251 if (!*nbuffers
|| *nbuffers
> MAX_BUFFER_NUM
)
252 *nbuffers
= MAX_BUFFER_NUM
;
254 if (size
* *nbuffers
> VID_LIMIT_BYTES
)
255 *nbuffers
= VID_LIMIT_BYTES
/ size
;
259 alloc_ctxs
[0] = isi
->alloc_ctx
;
264 dev_dbg(icd
->parent
, "%s, count=%d, size=%ld\n", __func__
,
270 static int buffer_init(struct vb2_buffer
*vb
)
272 struct frame_buffer
*buf
= container_of(vb
, struct frame_buffer
, vb
);
274 buf
->p_dma_desc
= NULL
;
275 INIT_LIST_HEAD(&buf
->list
);
280 static int buffer_prepare(struct vb2_buffer
*vb
)
282 struct soc_camera_device
*icd
= soc_camera_from_vb2q(vb
->vb2_queue
);
283 struct frame_buffer
*buf
= container_of(vb
, struct frame_buffer
, vb
);
284 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
285 struct atmel_isi
*isi
= ici
->priv
;
287 struct isi_dma_desc
*desc
;
289 size
= icd
->sizeimage
;
291 if (vb2_plane_size(vb
, 0) < size
) {
292 dev_err(icd
->parent
, "%s data will not fit into plane (%lu < %lu)\n",
293 __func__
, vb2_plane_size(vb
, 0), size
);
297 vb2_set_plane_payload(&buf
->vb
, 0, size
);
299 if (!buf
->p_dma_desc
) {
300 if (list_empty(&isi
->dma_desc_head
)) {
301 dev_err(icd
->parent
, "Not enough dma descriptors.\n");
304 /* Get an available descriptor */
305 desc
= list_entry(isi
->dma_desc_head
.next
,
306 struct isi_dma_desc
, list
);
307 /* Delete the descriptor since now it is used */
308 list_del_init(&desc
->list
);
310 /* Initialize the dma descriptor */
311 desc
->p_fbd
->fb_address
=
312 vb2_dma_contig_plane_dma_addr(vb
, 0);
313 desc
->p_fbd
->next_fbd_address
= 0;
314 set_dma_ctrl(desc
->p_fbd
, ISI_DMA_CTRL_WB
);
316 buf
->p_dma_desc
= desc
;
322 static void buffer_cleanup(struct vb2_buffer
*vb
)
324 struct soc_camera_device
*icd
= soc_camera_from_vb2q(vb
->vb2_queue
);
325 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
326 struct atmel_isi
*isi
= ici
->priv
;
327 struct frame_buffer
*buf
= container_of(vb
, struct frame_buffer
, vb
);
329 /* This descriptor is available now and we add to head list */
331 list_add(&buf
->p_dma_desc
->list
, &isi
->dma_desc_head
);
334 static void start_dma(struct atmel_isi
*isi
, struct frame_buffer
*buffer
)
338 cfg1
= isi_readl(isi
, ISI_CFG1
);
339 /* Enable irq: cxfr for the codec path, pxfr for the preview path */
340 isi_writel(isi
, ISI_INTEN
,
341 ISI_SR_CXFR_DONE
| ISI_SR_PXFR_DONE
);
343 /* Check if already in a frame */
344 if (isi_readl(isi
, ISI_STATUS
) & ISI_CTRL_CDC
) {
345 dev_err(isi
->soc_host
.icd
->parent
, "Already in frame handling.\n");
349 isi_writel(isi
, ISI_DMA_C_DSCR
, (u32
)buffer
->p_dma_desc
->fbd_phys
);
350 isi_writel(isi
, ISI_DMA_C_CTRL
, ISI_DMA_CTRL_FETCH
| ISI_DMA_CTRL_DONE
);
351 isi_writel(isi
, ISI_DMA_CHER
, ISI_DMA_CHSR_C_CH
);
353 cfg1
&= ~ISI_CFG1_FRATE_DIV_MASK
;
354 /* Enable linked list */
355 cfg1
|= isi
->pdata
.frate
| ISI_CFG1_DISCR
;
357 /* Enable codec path and ISI */
358 ctrl
= ISI_CTRL_CDC
| ISI_CTRL_EN
;
359 isi_writel(isi
, ISI_CTRL
, ctrl
);
360 isi_writel(isi
, ISI_CFG1
, cfg1
);
363 static void buffer_queue(struct vb2_buffer
*vb
)
365 struct soc_camera_device
*icd
= soc_camera_from_vb2q(vb
->vb2_queue
);
366 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
367 struct atmel_isi
*isi
= ici
->priv
;
368 struct frame_buffer
*buf
= container_of(vb
, struct frame_buffer
, vb
);
369 unsigned long flags
= 0;
371 spin_lock_irqsave(&isi
->lock
, flags
);
372 list_add_tail(&buf
->list
, &isi
->video_buffer_list
);
374 if (isi
->active
== NULL
) {
376 if (vb2_is_streaming(vb
->vb2_queue
))
379 spin_unlock_irqrestore(&isi
->lock
, flags
);
382 static int start_streaming(struct vb2_queue
*vq
, unsigned int count
)
384 struct soc_camera_device
*icd
= soc_camera_from_vb2q(vq
);
385 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
386 struct atmel_isi
*isi
= ici
->priv
;
390 ret
= atmel_isi_wait_status(isi
, WAIT_ISI_RESET
);
392 dev_err(icd
->parent
, "Reset ISI timed out\n");
395 /* Disable all interrupts */
396 isi_writel(isi
, ISI_INTDIS
, (u32
)~0UL);
398 spin_lock_irq(&isi
->lock
);
399 /* Clear any pending interrupt */
400 isi_readl(isi
, ISI_STATUS
);
403 start_dma(isi
, isi
->active
);
404 spin_unlock_irq(&isi
->lock
);
409 /* abort streaming and wait for last buffer */
410 static void stop_streaming(struct vb2_queue
*vq
)
412 struct soc_camera_device
*icd
= soc_camera_from_vb2q(vq
);
413 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
414 struct atmel_isi
*isi
= ici
->priv
;
415 struct frame_buffer
*buf
, *node
;
417 unsigned long timeout
;
419 spin_lock_irq(&isi
->lock
);
421 /* Release all active buffers */
422 list_for_each_entry_safe(buf
, node
, &isi
->video_buffer_list
, list
) {
423 list_del_init(&buf
->list
);
424 vb2_buffer_done(&buf
->vb
, VB2_BUF_STATE_ERROR
);
426 spin_unlock_irq(&isi
->lock
);
428 timeout
= jiffies
+ FRAME_INTERVAL_MILLI_SEC
* HZ
;
429 /* Wait until the end of the current frame. */
430 while ((isi_readl(isi
, ISI_STATUS
) & ISI_CTRL_CDC
) &&
431 time_before(jiffies
, timeout
))
434 if (time_after(jiffies
, timeout
)) {
436 "Timeout waiting for finishing codec request\n");
440 /* Disable interrupts */
441 isi_writel(isi
, ISI_INTDIS
,
442 ISI_SR_CXFR_DONE
| ISI_SR_PXFR_DONE
);
444 /* Disable ISI and wait for it is done */
445 ret
= atmel_isi_wait_status(isi
, WAIT_ISI_DISABLE
);
447 dev_err(icd
->parent
, "Disable ISI timed out\n");
450 static struct vb2_ops isi_video_qops
= {
451 .queue_setup
= queue_setup
,
452 .buf_init
= buffer_init
,
453 .buf_prepare
= buffer_prepare
,
454 .buf_cleanup
= buffer_cleanup
,
455 .buf_queue
= buffer_queue
,
456 .start_streaming
= start_streaming
,
457 .stop_streaming
= stop_streaming
,
458 .wait_prepare
= soc_camera_unlock
,
459 .wait_finish
= soc_camera_lock
,
462 /* ------------------------------------------------------------------
463 SOC camera operations for the device
464 ------------------------------------------------------------------*/
465 static int isi_camera_init_videobuf(struct vb2_queue
*q
,
466 struct soc_camera_device
*icd
)
468 q
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
469 q
->io_modes
= VB2_MMAP
;
471 q
->buf_struct_size
= sizeof(struct frame_buffer
);
472 q
->ops
= &isi_video_qops
;
473 q
->mem_ops
= &vb2_dma_contig_memops
;
474 q
->timestamp_flags
= V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC
;
476 return vb2_queue_init(q
);
479 static int isi_camera_set_fmt(struct soc_camera_device
*icd
,
480 struct v4l2_format
*f
)
482 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
483 struct atmel_isi
*isi
= ici
->priv
;
484 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
485 const struct soc_camera_format_xlate
*xlate
;
486 struct v4l2_pix_format
*pix
= &f
->fmt
.pix
;
487 struct v4l2_mbus_framefmt mf
;
490 xlate
= soc_camera_xlate_by_fourcc(icd
, pix
->pixelformat
);
492 dev_warn(icd
->parent
, "Format %x not found\n",
497 dev_dbg(icd
->parent
, "Plan to set format %dx%d\n",
498 pix
->width
, pix
->height
);
500 mf
.width
= pix
->width
;
501 mf
.height
= pix
->height
;
502 mf
.field
= pix
->field
;
503 mf
.colorspace
= pix
->colorspace
;
504 mf
.code
= xlate
->code
;
506 ret
= v4l2_subdev_call(sd
, video
, s_mbus_fmt
, &mf
);
510 if (mf
.code
!= xlate
->code
)
513 ret
= configure_geometry(isi
, pix
->width
, pix
->height
, xlate
->code
);
517 pix
->width
= mf
.width
;
518 pix
->height
= mf
.height
;
519 pix
->field
= mf
.field
;
520 pix
->colorspace
= mf
.colorspace
;
521 icd
->current_fmt
= xlate
;
523 dev_dbg(icd
->parent
, "Finally set format %dx%d\n",
524 pix
->width
, pix
->height
);
529 static int isi_camera_try_fmt(struct soc_camera_device
*icd
,
530 struct v4l2_format
*f
)
532 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
533 const struct soc_camera_format_xlate
*xlate
;
534 struct v4l2_pix_format
*pix
= &f
->fmt
.pix
;
535 struct v4l2_mbus_framefmt mf
;
536 u32 pixfmt
= pix
->pixelformat
;
539 xlate
= soc_camera_xlate_by_fourcc(icd
, pixfmt
);
540 if (pixfmt
&& !xlate
) {
541 dev_warn(icd
->parent
, "Format %x not found\n", pixfmt
);
545 /* limit to Atmel ISI hardware capabilities */
546 if (pix
->height
> MAX_SUPPORT_HEIGHT
)
547 pix
->height
= MAX_SUPPORT_HEIGHT
;
548 if (pix
->width
> MAX_SUPPORT_WIDTH
)
549 pix
->width
= MAX_SUPPORT_WIDTH
;
551 /* limit to sensor capabilities */
552 mf
.width
= pix
->width
;
553 mf
.height
= pix
->height
;
554 mf
.field
= pix
->field
;
555 mf
.colorspace
= pix
->colorspace
;
556 mf
.code
= xlate
->code
;
558 ret
= v4l2_subdev_call(sd
, video
, try_mbus_fmt
, &mf
);
562 pix
->width
= mf
.width
;
563 pix
->height
= mf
.height
;
564 pix
->colorspace
= mf
.colorspace
;
568 pix
->field
= V4L2_FIELD_NONE
;
570 case V4L2_FIELD_NONE
:
573 dev_err(icd
->parent
, "Field type %d unsupported.\n",
581 static const struct soc_mbus_pixelfmt isi_camera_formats
[] = {
583 .fourcc
= V4L2_PIX_FMT_YUYV
,
584 .name
= "Packed YUV422 16 bit",
585 .bits_per_sample
= 8,
586 .packing
= SOC_MBUS_PACKING_2X8_PADHI
,
587 .order
= SOC_MBUS_ORDER_LE
,
588 .layout
= SOC_MBUS_LAYOUT_PACKED
,
592 /* This will be corrected as we get more formats */
593 static bool isi_camera_packing_supported(const struct soc_mbus_pixelfmt
*fmt
)
595 return fmt
->packing
== SOC_MBUS_PACKING_NONE
||
596 (fmt
->bits_per_sample
== 8 &&
597 fmt
->packing
== SOC_MBUS_PACKING_2X8_PADHI
) ||
598 (fmt
->bits_per_sample
> 8 &&
599 fmt
->packing
== SOC_MBUS_PACKING_EXTEND16
);
602 #define ISI_BUS_PARAM (V4L2_MBUS_MASTER | \
603 V4L2_MBUS_HSYNC_ACTIVE_HIGH | \
604 V4L2_MBUS_HSYNC_ACTIVE_LOW | \
605 V4L2_MBUS_VSYNC_ACTIVE_HIGH | \
606 V4L2_MBUS_VSYNC_ACTIVE_LOW | \
607 V4L2_MBUS_PCLK_SAMPLE_RISING | \
608 V4L2_MBUS_PCLK_SAMPLE_FALLING | \
609 V4L2_MBUS_DATA_ACTIVE_HIGH)
611 static int isi_camera_try_bus_param(struct soc_camera_device
*icd
,
612 unsigned char buswidth
)
614 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
615 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
616 struct atmel_isi
*isi
= ici
->priv
;
617 struct v4l2_mbus_config cfg
= {.type
= V4L2_MBUS_PARALLEL
,};
618 unsigned long common_flags
;
621 ret
= v4l2_subdev_call(sd
, video
, g_mbus_config
, &cfg
);
623 common_flags
= soc_mbus_config_compatible(&cfg
,
626 dev_warn(icd
->parent
,
627 "Flags incompatible: camera 0x%x, host 0x%x\n",
628 cfg
.flags
, ISI_BUS_PARAM
);
631 } else if (ret
!= -ENOIOCTLCMD
) {
635 if ((1 << (buswidth
- 1)) & isi
->width_flags
)
641 static int isi_camera_get_formats(struct soc_camera_device
*icd
,
643 struct soc_camera_format_xlate
*xlate
)
645 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
646 int formats
= 0, ret
;
648 enum v4l2_mbus_pixelcode code
;
649 /* soc camera host format */
650 const struct soc_mbus_pixelfmt
*fmt
;
652 ret
= v4l2_subdev_call(sd
, video
, enum_mbus_fmt
, idx
, &code
);
654 /* No more formats */
657 fmt
= soc_mbus_get_fmtdesc(code
);
660 "Invalid format code #%u: %d\n", idx
, code
);
664 /* This also checks support for the requested bits-per-sample */
665 ret
= isi_camera_try_bus_param(icd
, fmt
->bits_per_sample
);
668 "Fail to try the bus parameters.\n");
673 case V4L2_MBUS_FMT_UYVY8_2X8
:
674 case V4L2_MBUS_FMT_VYUY8_2X8
:
675 case V4L2_MBUS_FMT_YUYV8_2X8
:
676 case V4L2_MBUS_FMT_YVYU8_2X8
:
679 xlate
->host_fmt
= &isi_camera_formats
[0];
682 dev_dbg(icd
->parent
, "Providing format %s using code %d\n",
683 isi_camera_formats
[0].name
, code
);
687 if (!isi_camera_packing_supported(fmt
))
691 "Providing format %s in pass-through mode\n",
695 /* Generic pass-through */
698 xlate
->host_fmt
= fmt
;
706 static int isi_camera_add_device(struct soc_camera_device
*icd
)
708 dev_dbg(icd
->parent
, "Atmel ISI Camera driver attached to camera %d\n",
714 static void isi_camera_remove_device(struct soc_camera_device
*icd
)
716 dev_dbg(icd
->parent
, "Atmel ISI Camera driver detached from camera %d\n",
720 /* Called with .host_lock held */
721 static int isi_camera_clock_start(struct soc_camera_host
*ici
)
723 struct atmel_isi
*isi
= ici
->priv
;
726 ret
= clk_prepare_enable(isi
->pclk
);
730 if (!IS_ERR(isi
->mck
)) {
731 ret
= clk_prepare_enable(isi
->mck
);
733 clk_disable_unprepare(isi
->pclk
);
741 /* Called with .host_lock held */
742 static void isi_camera_clock_stop(struct soc_camera_host
*ici
)
744 struct atmel_isi
*isi
= ici
->priv
;
746 if (!IS_ERR(isi
->mck
))
747 clk_disable_unprepare(isi
->mck
);
748 clk_disable_unprepare(isi
->pclk
);
751 static unsigned int isi_camera_poll(struct file
*file
, poll_table
*pt
)
753 struct soc_camera_device
*icd
= file
->private_data
;
755 return vb2_poll(&icd
->vb2_vidq
, file
, pt
);
758 static int isi_camera_querycap(struct soc_camera_host
*ici
,
759 struct v4l2_capability
*cap
)
761 strcpy(cap
->driver
, "atmel-isi");
762 strcpy(cap
->card
, "Atmel Image Sensor Interface");
763 cap
->capabilities
= (V4L2_CAP_VIDEO_CAPTURE
|
768 static int isi_camera_set_bus_param(struct soc_camera_device
*icd
)
770 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
771 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
772 struct atmel_isi
*isi
= ici
->priv
;
773 struct v4l2_mbus_config cfg
= {.type
= V4L2_MBUS_PARALLEL
,};
774 unsigned long common_flags
;
778 ret
= v4l2_subdev_call(sd
, video
, g_mbus_config
, &cfg
);
780 common_flags
= soc_mbus_config_compatible(&cfg
,
783 dev_warn(icd
->parent
,
784 "Flags incompatible: camera 0x%x, host 0x%x\n",
785 cfg
.flags
, ISI_BUS_PARAM
);
788 } else if (ret
!= -ENOIOCTLCMD
) {
791 common_flags
= ISI_BUS_PARAM
;
793 dev_dbg(icd
->parent
, "Flags cam: 0x%x host: 0x%x common: 0x%lx\n",
794 cfg
.flags
, ISI_BUS_PARAM
, common_flags
);
796 /* Make choises, based on platform preferences */
797 if ((common_flags
& V4L2_MBUS_HSYNC_ACTIVE_HIGH
) &&
798 (common_flags
& V4L2_MBUS_HSYNC_ACTIVE_LOW
)) {
799 if (isi
->pdata
.hsync_act_low
)
800 common_flags
&= ~V4L2_MBUS_HSYNC_ACTIVE_HIGH
;
802 common_flags
&= ~V4L2_MBUS_HSYNC_ACTIVE_LOW
;
805 if ((common_flags
& V4L2_MBUS_VSYNC_ACTIVE_HIGH
) &&
806 (common_flags
& V4L2_MBUS_VSYNC_ACTIVE_LOW
)) {
807 if (isi
->pdata
.vsync_act_low
)
808 common_flags
&= ~V4L2_MBUS_VSYNC_ACTIVE_HIGH
;
810 common_flags
&= ~V4L2_MBUS_VSYNC_ACTIVE_LOW
;
813 if ((common_flags
& V4L2_MBUS_PCLK_SAMPLE_RISING
) &&
814 (common_flags
& V4L2_MBUS_PCLK_SAMPLE_FALLING
)) {
815 if (isi
->pdata
.pclk_act_falling
)
816 common_flags
&= ~V4L2_MBUS_PCLK_SAMPLE_RISING
;
818 common_flags
&= ~V4L2_MBUS_PCLK_SAMPLE_FALLING
;
821 cfg
.flags
= common_flags
;
822 ret
= v4l2_subdev_call(sd
, video
, s_mbus_config
, &cfg
);
823 if (ret
< 0 && ret
!= -ENOIOCTLCMD
) {
824 dev_dbg(icd
->parent
, "camera s_mbus_config(0x%lx) returned %d\n",
829 /* set bus param for ISI */
830 if (common_flags
& V4L2_MBUS_HSYNC_ACTIVE_LOW
)
831 cfg1
|= ISI_CFG1_HSYNC_POL_ACTIVE_LOW
;
832 if (common_flags
& V4L2_MBUS_VSYNC_ACTIVE_LOW
)
833 cfg1
|= ISI_CFG1_VSYNC_POL_ACTIVE_LOW
;
834 if (common_flags
& V4L2_MBUS_PCLK_SAMPLE_FALLING
)
835 cfg1
|= ISI_CFG1_PIXCLK_POL_ACTIVE_FALLING
;
837 if (isi
->pdata
.has_emb_sync
)
838 cfg1
|= ISI_CFG1_EMB_SYNC
;
839 if (isi
->pdata
.full_mode
)
840 cfg1
|= ISI_CFG1_FULL_MODE
;
842 isi_writel(isi
, ISI_CTRL
, ISI_CTRL_DIS
);
843 isi_writel(isi
, ISI_CFG1
, cfg1
);
848 static struct soc_camera_host_ops isi_soc_camera_host_ops
= {
849 .owner
= THIS_MODULE
,
850 .add
= isi_camera_add_device
,
851 .remove
= isi_camera_remove_device
,
852 .clock_start
= isi_camera_clock_start
,
853 .clock_stop
= isi_camera_clock_stop
,
854 .set_fmt
= isi_camera_set_fmt
,
855 .try_fmt
= isi_camera_try_fmt
,
856 .get_formats
= isi_camera_get_formats
,
857 .init_videobuf2
= isi_camera_init_videobuf
,
858 .poll
= isi_camera_poll
,
859 .querycap
= isi_camera_querycap
,
860 .set_bus_param
= isi_camera_set_bus_param
,
863 /* -----------------------------------------------------------------------*/
864 static int atmel_isi_remove(struct platform_device
*pdev
)
866 struct soc_camera_host
*soc_host
= to_soc_camera_host(&pdev
->dev
);
867 struct atmel_isi
*isi
= container_of(soc_host
,
868 struct atmel_isi
, soc_host
);
870 soc_camera_host_unregister(soc_host
);
871 vb2_dma_contig_cleanup_ctx(isi
->alloc_ctx
);
872 dma_free_coherent(&pdev
->dev
,
873 sizeof(struct fbd
) * MAX_BUFFER_NUM
,
874 isi
->p_fb_descriptors
,
875 isi
->fb_descriptors_phys
);
880 static int atmel_isi_probe_dt(struct atmel_isi
*isi
,
881 struct platform_device
*pdev
)
883 struct device_node
*np
= pdev
->dev
.of_node
;
884 struct v4l2_of_endpoint ep
;
887 /* Default settings for ISI */
888 isi
->pdata
.full_mode
= 1;
889 isi
->pdata
.mck_hz
= ISI_DEFAULT_MCLK_FREQ
;
890 isi
->pdata
.frate
= ISI_CFG1_FRATE_CAPTURE_ALL
;
892 np
= of_graph_get_next_endpoint(np
, NULL
);
894 dev_err(&pdev
->dev
, "Could not find the endpoint\n");
898 err
= v4l2_of_parse_endpoint(np
, &ep
);
900 dev_err(&pdev
->dev
, "Could not parse the endpoint\n");
904 switch (ep
.bus
.parallel
.bus_width
) {
906 isi
->pdata
.data_width_flags
= ISI_DATAWIDTH_8
;
909 isi
->pdata
.data_width_flags
=
910 ISI_DATAWIDTH_8
| ISI_DATAWIDTH_10
;
913 dev_err(&pdev
->dev
, "Unsupported bus width: %d\n",
914 ep
.bus
.parallel
.bus_width
);
925 static int atmel_isi_probe(struct platform_device
*pdev
)
928 struct atmel_isi
*isi
;
929 struct resource
*regs
;
931 struct device
*dev
= &pdev
->dev
;
932 struct soc_camera_host
*soc_host
;
933 struct isi_platform_data
*pdata
;
935 pdata
= dev
->platform_data
;
936 if ((!pdata
|| !pdata
->data_width_flags
) && !pdev
->dev
.of_node
) {
938 "No config available for Atmel ISI\n");
942 isi
= devm_kzalloc(&pdev
->dev
, sizeof(struct atmel_isi
), GFP_KERNEL
);
944 dev_err(&pdev
->dev
, "Can't allocate interface!\n");
948 isi
->pclk
= devm_clk_get(&pdev
->dev
, "isi_clk");
949 if (IS_ERR(isi
->pclk
))
950 return PTR_ERR(isi
->pclk
);
953 memcpy(&isi
->pdata
, pdata
, sizeof(isi
->pdata
));
955 ret
= atmel_isi_probe_dt(isi
, pdev
);
961 spin_lock_init(&isi
->lock
);
962 INIT_LIST_HEAD(&isi
->video_buffer_list
);
963 INIT_LIST_HEAD(&isi
->dma_desc_head
);
965 /* ISI_MCK is the sensor master clock. It should be handled by the
966 * sensor driver directly, as the ISI has no use for that clock. Make
967 * the clock optional here while platforms transition to the correct
970 isi
->mck
= devm_clk_get(dev
, "isi_mck");
971 if (!IS_ERR(isi
->mck
)) {
972 /* Set ISI_MCK's frequency, it should be faster than pixel
975 ret
= clk_set_rate(isi
->mck
, isi
->pdata
.mck_hz
);
980 isi
->p_fb_descriptors
= dma_alloc_coherent(&pdev
->dev
,
981 sizeof(struct fbd
) * MAX_BUFFER_NUM
,
982 &isi
->fb_descriptors_phys
,
984 if (!isi
->p_fb_descriptors
) {
985 dev_err(&pdev
->dev
, "Can't allocate descriptors!\n");
989 for (i
= 0; i
< MAX_BUFFER_NUM
; i
++) {
990 isi
->dma_desc
[i
].p_fbd
= isi
->p_fb_descriptors
+ i
;
991 isi
->dma_desc
[i
].fbd_phys
= isi
->fb_descriptors_phys
+
992 i
* sizeof(struct fbd
);
993 list_add(&isi
->dma_desc
[i
].list
, &isi
->dma_desc_head
);
996 isi
->alloc_ctx
= vb2_dma_contig_init_ctx(&pdev
->dev
);
997 if (IS_ERR(isi
->alloc_ctx
)) {
998 ret
= PTR_ERR(isi
->alloc_ctx
);
1002 regs
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1003 isi
->regs
= devm_ioremap_resource(&pdev
->dev
, regs
);
1004 if (IS_ERR(isi
->regs
)) {
1005 ret
= PTR_ERR(isi
->regs
);
1009 if (isi
->pdata
.data_width_flags
& ISI_DATAWIDTH_8
)
1010 isi
->width_flags
= 1 << 7;
1011 if (isi
->pdata
.data_width_flags
& ISI_DATAWIDTH_10
)
1012 isi
->width_flags
|= 1 << 9;
1014 isi_writel(isi
, ISI_CTRL
, ISI_CTRL_DIS
);
1016 irq
= platform_get_irq(pdev
, 0);
1017 if (IS_ERR_VALUE(irq
)) {
1022 ret
= devm_request_irq(&pdev
->dev
, irq
, isi_interrupt
, 0, "isi", isi
);
1024 dev_err(&pdev
->dev
, "Unable to request irq %d\n", irq
);
1029 soc_host
= &isi
->soc_host
;
1030 soc_host
->drv_name
= "isi-camera";
1031 soc_host
->ops
= &isi_soc_camera_host_ops
;
1032 soc_host
->priv
= isi
;
1033 soc_host
->v4l2_dev
.dev
= &pdev
->dev
;
1034 soc_host
->nr
= pdev
->id
;
1036 if (isi
->pdata
.asd_sizes
) {
1037 soc_host
->asd
= isi
->pdata
.asd
;
1038 soc_host
->asd_sizes
= isi
->pdata
.asd_sizes
;
1041 ret
= soc_camera_host_register(soc_host
);
1043 dev_err(&pdev
->dev
, "Unable to register soc camera host\n");
1044 goto err_register_soc_camera_host
;
1048 err_register_soc_camera_host
:
1051 vb2_dma_contig_cleanup_ctx(isi
->alloc_ctx
);
1053 dma_free_coherent(&pdev
->dev
,
1054 sizeof(struct fbd
) * MAX_BUFFER_NUM
,
1055 isi
->p_fb_descriptors
,
1056 isi
->fb_descriptors_phys
);
1061 static const struct of_device_id atmel_isi_of_match
[] = {
1062 { .compatible
= "atmel,at91sam9g45-isi" },
1065 MODULE_DEVICE_TABLE(of
, atmel_isi_of_match
);
1067 static struct platform_driver atmel_isi_driver
= {
1068 .remove
= atmel_isi_remove
,
1070 .name
= "atmel_isi",
1071 .owner
= THIS_MODULE
,
1072 .of_match_table
= of_match_ptr(atmel_isi_of_match
),
1076 module_platform_driver_probe(atmel_isi_driver
, atmel_isi_probe
);
1078 MODULE_AUTHOR("Josh Wu <josh.wu@atmel.com>");
1079 MODULE_DESCRIPTION("The V4L2 driver for Atmel Linux");
1080 MODULE_LICENSE("GPL");
1081 MODULE_SUPPORTED_DEVICE("video");