bridge: fix a possible use after free
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / media / video / atmel-isi.c
blob7b89f00501b827efc52aee371eede35ef5d37b31
1 /*
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>
6 * and Sedji Gaouaou
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>
17 #include <linux/fs.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 /* ISI states */
38 enum {
39 ISI_STATE_IDLE = 0,
40 ISI_STATE_READY,
41 ISI_STATE_WAIT_SOF,
44 /* Frame buffer descriptor */
45 struct fbd {
46 /* Physical address of the frame buffer */
47 u32 fb_address;
48 /* DMA Control Register(only in HISI2) */
49 u32 dma_ctrl;
50 /* Physical address of the next fbd */
51 u32 next_fbd_address;
54 static void set_dma_ctrl(struct fbd *fb_desc, u32 ctrl)
56 fb_desc->dma_ctrl = ctrl;
59 struct isi_dma_desc {
60 struct list_head list;
61 struct fbd *p_fbd;
62 u32 fbd_phys;
65 /* Frame buffer data */
66 struct frame_buffer {
67 struct vb2_buffer vb;
68 struct isi_dma_desc *p_dma_desc;
69 struct list_head list;
72 struct atmel_isi {
73 /* Protects the access of variables shared with the ISR */
74 spinlock_t lock;
75 void __iomem *regs;
77 int sequence;
78 /* State of the ISI module in capturing mode */
79 int state;
81 /* Wait queue for waiting for SOF */
82 wait_queue_head_t vsync_wq;
84 struct vb2_alloc_ctx *alloc_ctx;
86 /* Allocate descriptors for dma buffer use */
87 struct fbd *p_fb_descriptors;
88 u32 fb_descriptors_phys;
89 struct list_head dma_desc_head;
90 struct isi_dma_desc dma_desc[MAX_BUFFER_NUM];
92 struct completion complete;
93 struct clk *pclk;
94 unsigned int irq;
96 struct isi_platform_data *pdata;
98 struct list_head video_buffer_list;
99 struct frame_buffer *active;
101 struct soc_camera_device *icd;
102 struct soc_camera_host soc_host;
105 static void isi_writel(struct atmel_isi *isi, u32 reg, u32 val)
107 writel(val, isi->regs + reg);
109 static u32 isi_readl(struct atmel_isi *isi, u32 reg)
111 return readl(isi->regs + reg);
114 static int configure_geometry(struct atmel_isi *isi, u32 width,
115 u32 height, enum v4l2_mbus_pixelcode code)
117 u32 cfg2, cr;
119 switch (code) {
120 /* YUV, including grey */
121 case V4L2_MBUS_FMT_Y8_1X8:
122 cr = ISI_CFG2_GRAYSCALE;
123 break;
124 case V4L2_MBUS_FMT_UYVY8_2X8:
125 cr = ISI_CFG2_YCC_SWAP_MODE_3;
126 break;
127 case V4L2_MBUS_FMT_VYUY8_2X8:
128 cr = ISI_CFG2_YCC_SWAP_MODE_2;
129 break;
130 case V4L2_MBUS_FMT_YUYV8_2X8:
131 cr = ISI_CFG2_YCC_SWAP_MODE_1;
132 break;
133 case V4L2_MBUS_FMT_YVYU8_2X8:
134 cr = ISI_CFG2_YCC_SWAP_DEFAULT;
135 break;
136 /* RGB, TODO */
137 default:
138 return -EINVAL;
141 isi_writel(isi, ISI_CTRL, ISI_CTRL_DIS);
143 cfg2 = isi_readl(isi, ISI_CFG2);
144 cfg2 |= cr;
145 /* Set width */
146 cfg2 &= ~(ISI_CFG2_IM_HSIZE_MASK);
147 cfg2 |= ((width - 1) << ISI_CFG2_IM_HSIZE_OFFSET) &
148 ISI_CFG2_IM_HSIZE_MASK;
149 /* Set height */
150 cfg2 &= ~(ISI_CFG2_IM_VSIZE_MASK);
151 cfg2 |= ((height - 1) << ISI_CFG2_IM_VSIZE_OFFSET)
152 & ISI_CFG2_IM_VSIZE_MASK;
153 isi_writel(isi, ISI_CFG2, cfg2);
155 return 0;
158 static irqreturn_t atmel_isi_handle_streaming(struct atmel_isi *isi)
160 if (isi->active) {
161 struct vb2_buffer *vb = &isi->active->vb;
162 struct frame_buffer *buf = isi->active;
164 list_del_init(&buf->list);
165 do_gettimeofday(&vb->v4l2_buf.timestamp);
166 vb->v4l2_buf.sequence = isi->sequence++;
167 vb2_buffer_done(vb, VB2_BUF_STATE_DONE);
170 if (list_empty(&isi->video_buffer_list)) {
171 isi->active = NULL;
172 } else {
173 /* start next dma frame. */
174 isi->active = list_entry(isi->video_buffer_list.next,
175 struct frame_buffer, list);
176 isi_writel(isi, ISI_DMA_C_DSCR,
177 isi->active->p_dma_desc->fbd_phys);
178 isi_writel(isi, ISI_DMA_C_CTRL,
179 ISI_DMA_CTRL_FETCH | ISI_DMA_CTRL_DONE);
180 isi_writel(isi, ISI_DMA_CHER, ISI_DMA_CHSR_C_CH);
182 return IRQ_HANDLED;
185 /* ISI interrupt service routine */
186 static irqreturn_t isi_interrupt(int irq, void *dev_id)
188 struct atmel_isi *isi = dev_id;
189 u32 status, mask, pending;
190 irqreturn_t ret = IRQ_NONE;
192 spin_lock(&isi->lock);
194 status = isi_readl(isi, ISI_STATUS);
195 mask = isi_readl(isi, ISI_INTMASK);
196 pending = status & mask;
198 if (pending & ISI_CTRL_SRST) {
199 complete(&isi->complete);
200 isi_writel(isi, ISI_INTDIS, ISI_CTRL_SRST);
201 ret = IRQ_HANDLED;
202 } else if (pending & ISI_CTRL_DIS) {
203 complete(&isi->complete);
204 isi_writel(isi, ISI_INTDIS, ISI_CTRL_DIS);
205 ret = IRQ_HANDLED;
206 } else {
207 if ((pending & ISI_SR_VSYNC) &&
208 (isi->state == ISI_STATE_IDLE)) {
209 isi->state = ISI_STATE_READY;
210 wake_up_interruptible(&isi->vsync_wq);
211 ret = IRQ_HANDLED;
213 if (likely(pending & ISI_SR_CXFR_DONE))
214 ret = atmel_isi_handle_streaming(isi);
217 spin_unlock(&isi->lock);
218 return ret;
221 #define WAIT_ISI_RESET 1
222 #define WAIT_ISI_DISABLE 0
223 static int atmel_isi_wait_status(struct atmel_isi *isi, int wait_reset)
225 unsigned long timeout;
227 * The reset or disable will only succeed if we have a
228 * pixel clock from the camera.
230 init_completion(&isi->complete);
232 if (wait_reset) {
233 isi_writel(isi, ISI_INTEN, ISI_CTRL_SRST);
234 isi_writel(isi, ISI_CTRL, ISI_CTRL_SRST);
235 } else {
236 isi_writel(isi, ISI_INTEN, ISI_CTRL_DIS);
237 isi_writel(isi, ISI_CTRL, ISI_CTRL_DIS);
240 timeout = wait_for_completion_timeout(&isi->complete,
241 msecs_to_jiffies(100));
242 if (timeout == 0)
243 return -ETIMEDOUT;
245 return 0;
248 /* ------------------------------------------------------------------
249 Videobuf operations
250 ------------------------------------------------------------------*/
251 static int queue_setup(struct vb2_queue *vq, unsigned int *nbuffers,
252 unsigned int *nplanes, unsigned long sizes[],
253 void *alloc_ctxs[])
255 struct soc_camera_device *icd = soc_camera_from_vb2q(vq);
256 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
257 struct atmel_isi *isi = ici->priv;
258 unsigned long size;
259 int ret, bytes_per_line;
261 /* Reset ISI */
262 ret = atmel_isi_wait_status(isi, WAIT_ISI_RESET);
263 if (ret < 0) {
264 dev_err(icd->parent, "Reset ISI timed out\n");
265 return ret;
267 /* Disable all interrupts */
268 isi_writel(isi, ISI_INTDIS, ~0UL);
270 bytes_per_line = soc_mbus_bytes_per_line(icd->user_width,
271 icd->current_fmt->host_fmt);
273 if (bytes_per_line < 0)
274 return bytes_per_line;
276 size = bytes_per_line * icd->user_height;
278 if (!*nbuffers || *nbuffers > MAX_BUFFER_NUM)
279 *nbuffers = MAX_BUFFER_NUM;
281 if (size * *nbuffers > VID_LIMIT_BYTES)
282 *nbuffers = VID_LIMIT_BYTES / size;
284 *nplanes = 1;
285 sizes[0] = size;
286 alloc_ctxs[0] = isi->alloc_ctx;
288 isi->sequence = 0;
289 isi->active = NULL;
291 dev_dbg(icd->parent, "%s, count=%d, size=%ld\n", __func__,
292 *nbuffers, size);
294 return 0;
297 static int buffer_init(struct vb2_buffer *vb)
299 struct frame_buffer *buf = container_of(vb, struct frame_buffer, vb);
301 buf->p_dma_desc = NULL;
302 INIT_LIST_HEAD(&buf->list);
304 return 0;
307 static int buffer_prepare(struct vb2_buffer *vb)
309 struct soc_camera_device *icd = soc_camera_from_vb2q(vb->vb2_queue);
310 struct frame_buffer *buf = container_of(vb, struct frame_buffer, vb);
311 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
312 struct atmel_isi *isi = ici->priv;
313 unsigned long size;
314 struct isi_dma_desc *desc;
315 int bytes_per_line = soc_mbus_bytes_per_line(icd->user_width,
316 icd->current_fmt->host_fmt);
318 if (bytes_per_line < 0)
319 return bytes_per_line;
321 size = bytes_per_line * icd->user_height;
323 if (vb2_plane_size(vb, 0) < size) {
324 dev_err(icd->parent, "%s data will not fit into plane (%lu < %lu)\n",
325 __func__, vb2_plane_size(vb, 0), size);
326 return -EINVAL;
329 vb2_set_plane_payload(&buf->vb, 0, size);
331 if (!buf->p_dma_desc) {
332 if (list_empty(&isi->dma_desc_head)) {
333 dev_err(icd->parent, "Not enough dma descriptors.\n");
334 return -EINVAL;
335 } else {
336 /* Get an available descriptor */
337 desc = list_entry(isi->dma_desc_head.next,
338 struct isi_dma_desc, list);
339 /* Delete the descriptor since now it is used */
340 list_del_init(&desc->list);
342 /* Initialize the dma descriptor */
343 desc->p_fbd->fb_address =
344 vb2_dma_contig_plane_paddr(vb, 0);
345 desc->p_fbd->next_fbd_address = 0;
346 set_dma_ctrl(desc->p_fbd, ISI_DMA_CTRL_WB);
348 buf->p_dma_desc = desc;
351 return 0;
354 static void buffer_cleanup(struct vb2_buffer *vb)
356 struct soc_camera_device *icd = soc_camera_from_vb2q(vb->vb2_queue);
357 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
358 struct atmel_isi *isi = ici->priv;
359 struct frame_buffer *buf = container_of(vb, struct frame_buffer, vb);
361 /* This descriptor is available now and we add to head list */
362 if (buf->p_dma_desc)
363 list_add(&buf->p_dma_desc->list, &isi->dma_desc_head);
366 static void start_dma(struct atmel_isi *isi, struct frame_buffer *buffer)
368 u32 ctrl, cfg1;
370 cfg1 = isi_readl(isi, ISI_CFG1);
371 /* Enable irq: cxfr for the codec path, pxfr for the preview path */
372 isi_writel(isi, ISI_INTEN,
373 ISI_SR_CXFR_DONE | ISI_SR_PXFR_DONE);
375 /* Check if already in a frame */
376 if (isi_readl(isi, ISI_STATUS) & ISI_CTRL_CDC) {
377 dev_err(isi->icd->parent, "Already in frame handling.\n");
378 return;
381 isi_writel(isi, ISI_DMA_C_DSCR, buffer->p_dma_desc->fbd_phys);
382 isi_writel(isi, ISI_DMA_C_CTRL, ISI_DMA_CTRL_FETCH | ISI_DMA_CTRL_DONE);
383 isi_writel(isi, ISI_DMA_CHER, ISI_DMA_CHSR_C_CH);
385 /* Enable linked list */
386 cfg1 |= isi->pdata->frate | ISI_CFG1_DISCR;
388 /* Enable codec path and ISI */
389 ctrl = ISI_CTRL_CDC | ISI_CTRL_EN;
390 isi_writel(isi, ISI_CTRL, ctrl);
391 isi_writel(isi, ISI_CFG1, cfg1);
394 static void buffer_queue(struct vb2_buffer *vb)
396 struct soc_camera_device *icd = soc_camera_from_vb2q(vb->vb2_queue);
397 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
398 struct atmel_isi *isi = ici->priv;
399 struct frame_buffer *buf = container_of(vb, struct frame_buffer, vb);
400 unsigned long flags = 0;
402 spin_lock_irqsave(&isi->lock, flags);
403 list_add_tail(&buf->list, &isi->video_buffer_list);
405 if (isi->active == NULL) {
406 isi->active = buf;
407 start_dma(isi, buf);
409 spin_unlock_irqrestore(&isi->lock, flags);
412 static int start_streaming(struct vb2_queue *vq)
414 struct soc_camera_device *icd = soc_camera_from_vb2q(vq);
415 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
416 struct atmel_isi *isi = ici->priv;
418 u32 sr = 0;
419 int ret;
421 spin_lock_irq(&isi->lock);
422 isi->state = ISI_STATE_IDLE;
423 /* Clear any pending SOF interrupt */
424 sr = isi_readl(isi, ISI_STATUS);
425 /* Enable VSYNC interrupt for SOF */
426 isi_writel(isi, ISI_INTEN, ISI_SR_VSYNC);
427 isi_writel(isi, ISI_CTRL, ISI_CTRL_EN);
428 spin_unlock_irq(&isi->lock);
430 dev_dbg(icd->parent, "Waiting for SOF\n");
431 ret = wait_event_interruptible(isi->vsync_wq,
432 isi->state != ISI_STATE_IDLE);
433 if (ret)
434 return ret;
436 if (isi->state != ISI_STATE_READY)
437 return -EIO;
439 spin_lock_irq(&isi->lock);
440 isi->state = ISI_STATE_WAIT_SOF;
441 isi_writel(isi, ISI_INTDIS, ISI_SR_VSYNC);
442 spin_unlock_irq(&isi->lock);
444 return 0;
447 /* abort streaming and wait for last buffer */
448 static int stop_streaming(struct vb2_queue *vq)
450 struct soc_camera_device *icd = soc_camera_from_vb2q(vq);
451 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
452 struct atmel_isi *isi = ici->priv;
453 struct frame_buffer *buf, *node;
454 int ret = 0;
455 unsigned long timeout;
457 spin_lock_irq(&isi->lock);
458 isi->active = NULL;
459 /* Release all active buffers */
460 list_for_each_entry_safe(buf, node, &isi->video_buffer_list, list) {
461 list_del_init(&buf->list);
462 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
464 spin_unlock_irq(&isi->lock);
466 timeout = jiffies + FRAME_INTERVAL_MILLI_SEC * HZ;
467 /* Wait until the end of the current frame. */
468 while ((isi_readl(isi, ISI_STATUS) & ISI_CTRL_CDC) &&
469 time_before(jiffies, timeout))
470 msleep(1);
472 if (time_after(jiffies, timeout)) {
473 dev_err(icd->parent,
474 "Timeout waiting for finishing codec request\n");
475 return -ETIMEDOUT;
478 /* Disable interrupts */
479 isi_writel(isi, ISI_INTDIS,
480 ISI_SR_CXFR_DONE | ISI_SR_PXFR_DONE);
482 /* Disable ISI and wait for it is done */
483 ret = atmel_isi_wait_status(isi, WAIT_ISI_DISABLE);
484 if (ret < 0)
485 dev_err(icd->parent, "Disable ISI timed out\n");
487 return ret;
490 static struct vb2_ops isi_video_qops = {
491 .queue_setup = queue_setup,
492 .buf_init = buffer_init,
493 .buf_prepare = buffer_prepare,
494 .buf_cleanup = buffer_cleanup,
495 .buf_queue = buffer_queue,
496 .start_streaming = start_streaming,
497 .stop_streaming = stop_streaming,
498 .wait_prepare = soc_camera_unlock,
499 .wait_finish = soc_camera_lock,
502 /* ------------------------------------------------------------------
503 SOC camera operations for the device
504 ------------------------------------------------------------------*/
505 static int isi_camera_init_videobuf(struct vb2_queue *q,
506 struct soc_camera_device *icd)
508 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
509 q->io_modes = VB2_MMAP;
510 q->drv_priv = icd;
511 q->buf_struct_size = sizeof(struct frame_buffer);
512 q->ops = &isi_video_qops;
513 q->mem_ops = &vb2_dma_contig_memops;
515 return vb2_queue_init(q);
518 static int isi_camera_set_fmt(struct soc_camera_device *icd,
519 struct v4l2_format *f)
521 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
522 struct atmel_isi *isi = ici->priv;
523 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
524 const struct soc_camera_format_xlate *xlate;
525 struct v4l2_pix_format *pix = &f->fmt.pix;
526 struct v4l2_mbus_framefmt mf;
527 int ret;
529 xlate = soc_camera_xlate_by_fourcc(icd, pix->pixelformat);
530 if (!xlate) {
531 dev_warn(icd->parent, "Format %x not found\n",
532 pix->pixelformat);
533 return -EINVAL;
536 dev_dbg(icd->parent, "Plan to set format %dx%d\n",
537 pix->width, pix->height);
539 mf.width = pix->width;
540 mf.height = pix->height;
541 mf.field = pix->field;
542 mf.colorspace = pix->colorspace;
543 mf.code = xlate->code;
545 ret = v4l2_subdev_call(sd, video, s_mbus_fmt, &mf);
546 if (ret < 0)
547 return ret;
549 if (mf.code != xlate->code)
550 return -EINVAL;
552 ret = configure_geometry(isi, pix->width, pix->height, xlate->code);
553 if (ret < 0)
554 return ret;
556 pix->width = mf.width;
557 pix->height = mf.height;
558 pix->field = mf.field;
559 pix->colorspace = mf.colorspace;
560 icd->current_fmt = xlate;
562 dev_dbg(icd->parent, "Finally set format %dx%d\n",
563 pix->width, pix->height);
565 return ret;
568 static int isi_camera_try_fmt(struct soc_camera_device *icd,
569 struct v4l2_format *f)
571 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
572 const struct soc_camera_format_xlate *xlate;
573 struct v4l2_pix_format *pix = &f->fmt.pix;
574 struct v4l2_mbus_framefmt mf;
575 u32 pixfmt = pix->pixelformat;
576 int ret;
578 xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
579 if (pixfmt && !xlate) {
580 dev_warn(icd->parent, "Format %x not found\n", pixfmt);
581 return -EINVAL;
584 /* limit to Atmel ISI hardware capabilities */
585 if (pix->height > MAX_SUPPORT_HEIGHT)
586 pix->height = MAX_SUPPORT_HEIGHT;
587 if (pix->width > MAX_SUPPORT_WIDTH)
588 pix->width = MAX_SUPPORT_WIDTH;
590 /* limit to sensor capabilities */
591 mf.width = pix->width;
592 mf.height = pix->height;
593 mf.field = pix->field;
594 mf.colorspace = pix->colorspace;
595 mf.code = xlate->code;
597 ret = v4l2_subdev_call(sd, video, try_mbus_fmt, &mf);
598 if (ret < 0)
599 return ret;
601 pix->width = mf.width;
602 pix->height = mf.height;
603 pix->colorspace = mf.colorspace;
605 switch (mf.field) {
606 case V4L2_FIELD_ANY:
607 pix->field = V4L2_FIELD_NONE;
608 break;
609 case V4L2_FIELD_NONE:
610 break;
611 default:
612 dev_err(icd->parent, "Field type %d unsupported.\n",
613 mf.field);
614 ret = -EINVAL;
617 return ret;
620 static const struct soc_mbus_pixelfmt isi_camera_formats[] = {
622 .fourcc = V4L2_PIX_FMT_YUYV,
623 .name = "Packed YUV422 16 bit",
624 .bits_per_sample = 8,
625 .packing = SOC_MBUS_PACKING_2X8_PADHI,
626 .order = SOC_MBUS_ORDER_LE,
630 /* This will be corrected as we get more formats */
631 static bool isi_camera_packing_supported(const struct soc_mbus_pixelfmt *fmt)
633 return fmt->packing == SOC_MBUS_PACKING_NONE ||
634 (fmt->bits_per_sample == 8 &&
635 fmt->packing == SOC_MBUS_PACKING_2X8_PADHI) ||
636 (fmt->bits_per_sample > 8 &&
637 fmt->packing == SOC_MBUS_PACKING_EXTEND16);
640 static unsigned long make_bus_param(struct atmel_isi *isi)
642 unsigned long flags;
644 * Platform specified synchronization and pixel clock polarities are
645 * only a recommendation and are only used during probing. Atmel ISI
646 * camera interface only works in master mode, i.e., uses HSYNC and
647 * VSYNC signals from the sensor
649 flags = SOCAM_MASTER |
650 SOCAM_HSYNC_ACTIVE_HIGH |
651 SOCAM_HSYNC_ACTIVE_LOW |
652 SOCAM_VSYNC_ACTIVE_HIGH |
653 SOCAM_VSYNC_ACTIVE_LOW |
654 SOCAM_PCLK_SAMPLE_RISING |
655 SOCAM_PCLK_SAMPLE_FALLING |
656 SOCAM_DATA_ACTIVE_HIGH;
658 if (isi->pdata->data_width_flags & ISI_DATAWIDTH_10)
659 flags |= SOCAM_DATAWIDTH_10;
661 if (isi->pdata->data_width_flags & ISI_DATAWIDTH_8)
662 flags |= SOCAM_DATAWIDTH_8;
664 if (flags & SOCAM_DATAWIDTH_MASK)
665 return flags;
667 return 0;
670 static int isi_camera_try_bus_param(struct soc_camera_device *icd,
671 unsigned char buswidth)
673 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
674 struct atmel_isi *isi = ici->priv;
675 unsigned long camera_flags;
676 int ret;
678 camera_flags = icd->ops->query_bus_param(icd);
679 ret = soc_camera_bus_param_compatible(camera_flags,
680 make_bus_param(isi));
681 if (!ret)
682 return -EINVAL;
683 return 0;
687 static int isi_camera_get_formats(struct soc_camera_device *icd,
688 unsigned int idx,
689 struct soc_camera_format_xlate *xlate)
691 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
692 int formats = 0, ret;
693 /* sensor format */
694 enum v4l2_mbus_pixelcode code;
695 /* soc camera host format */
696 const struct soc_mbus_pixelfmt *fmt;
698 ret = v4l2_subdev_call(sd, video, enum_mbus_fmt, idx, &code);
699 if (ret < 0)
700 /* No more formats */
701 return 0;
703 fmt = soc_mbus_get_fmtdesc(code);
704 if (!fmt) {
705 dev_err(icd->parent,
706 "Invalid format code #%u: %d\n", idx, code);
707 return 0;
710 /* This also checks support for the requested bits-per-sample */
711 ret = isi_camera_try_bus_param(icd, fmt->bits_per_sample);
712 if (ret < 0) {
713 dev_err(icd->parent,
714 "Fail to try the bus parameters.\n");
715 return 0;
718 switch (code) {
719 case V4L2_MBUS_FMT_UYVY8_2X8:
720 case V4L2_MBUS_FMT_VYUY8_2X8:
721 case V4L2_MBUS_FMT_YUYV8_2X8:
722 case V4L2_MBUS_FMT_YVYU8_2X8:
723 formats++;
724 if (xlate) {
725 xlate->host_fmt = &isi_camera_formats[0];
726 xlate->code = code;
727 xlate++;
728 dev_dbg(icd->parent, "Providing format %s using code %d\n",
729 isi_camera_formats[0].name, code);
731 break;
732 default:
733 if (!isi_camera_packing_supported(fmt))
734 return 0;
735 if (xlate)
736 dev_dbg(icd->parent,
737 "Providing format %s in pass-through mode\n",
738 fmt->name);
741 /* Generic pass-through */
742 formats++;
743 if (xlate) {
744 xlate->host_fmt = fmt;
745 xlate->code = code;
746 xlate++;
749 return formats;
752 /* Called with .video_lock held */
753 static int isi_camera_add_device(struct soc_camera_device *icd)
755 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
756 struct atmel_isi *isi = ici->priv;
757 int ret;
759 if (isi->icd)
760 return -EBUSY;
762 ret = clk_enable(isi->pclk);
763 if (ret)
764 return ret;
766 isi->icd = icd;
767 dev_dbg(icd->parent, "Atmel ISI Camera driver attached to camera %d\n",
768 icd->devnum);
769 return 0;
771 /* Called with .video_lock held */
772 static void isi_camera_remove_device(struct soc_camera_device *icd)
774 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
775 struct atmel_isi *isi = ici->priv;
777 BUG_ON(icd != isi->icd);
779 clk_disable(isi->pclk);
780 isi->icd = NULL;
782 dev_dbg(icd->parent, "Atmel ISI Camera driver detached from camera %d\n",
783 icd->devnum);
786 static unsigned int isi_camera_poll(struct file *file, poll_table *pt)
788 struct soc_camera_device *icd = file->private_data;
790 return vb2_poll(&icd->vb2_vidq, file, pt);
793 static int isi_camera_querycap(struct soc_camera_host *ici,
794 struct v4l2_capability *cap)
796 strcpy(cap->driver, "atmel-isi");
797 strcpy(cap->card, "Atmel Image Sensor Interface");
798 cap->capabilities = (V4L2_CAP_VIDEO_CAPTURE |
799 V4L2_CAP_STREAMING);
800 return 0;
803 static int isi_camera_set_bus_param(struct soc_camera_device *icd, u32 pixfmt)
805 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
806 struct atmel_isi *isi = ici->priv;
807 unsigned long bus_flags, camera_flags, common_flags;
808 int ret;
809 u32 cfg1 = 0;
811 camera_flags = icd->ops->query_bus_param(icd);
813 bus_flags = make_bus_param(isi);
814 common_flags = soc_camera_bus_param_compatible(camera_flags, bus_flags);
815 dev_dbg(icd->parent, "Flags cam: 0x%lx host: 0x%lx common: 0x%lx\n",
816 camera_flags, bus_flags, common_flags);
817 if (!common_flags)
818 return -EINVAL;
820 /* Make choises, based on platform preferences */
821 if ((common_flags & SOCAM_HSYNC_ACTIVE_HIGH) &&
822 (common_flags & SOCAM_HSYNC_ACTIVE_LOW)) {
823 if (isi->pdata->hsync_act_low)
824 common_flags &= ~SOCAM_HSYNC_ACTIVE_HIGH;
825 else
826 common_flags &= ~SOCAM_HSYNC_ACTIVE_LOW;
829 if ((common_flags & SOCAM_VSYNC_ACTIVE_HIGH) &&
830 (common_flags & SOCAM_VSYNC_ACTIVE_LOW)) {
831 if (isi->pdata->vsync_act_low)
832 common_flags &= ~SOCAM_VSYNC_ACTIVE_HIGH;
833 else
834 common_flags &= ~SOCAM_VSYNC_ACTIVE_LOW;
837 if ((common_flags & SOCAM_PCLK_SAMPLE_RISING) &&
838 (common_flags & SOCAM_PCLK_SAMPLE_FALLING)) {
839 if (isi->pdata->pclk_act_falling)
840 common_flags &= ~SOCAM_PCLK_SAMPLE_RISING;
841 else
842 common_flags &= ~SOCAM_PCLK_SAMPLE_FALLING;
845 ret = icd->ops->set_bus_param(icd, common_flags);
846 if (ret < 0) {
847 dev_dbg(icd->parent, "Camera set_bus_param(%lx) returned %d\n",
848 common_flags, ret);
849 return ret;
852 /* set bus param for ISI */
853 if (common_flags & SOCAM_HSYNC_ACTIVE_LOW)
854 cfg1 |= ISI_CFG1_HSYNC_POL_ACTIVE_LOW;
855 if (common_flags & SOCAM_VSYNC_ACTIVE_LOW)
856 cfg1 |= ISI_CFG1_VSYNC_POL_ACTIVE_LOW;
857 if (common_flags & SOCAM_PCLK_SAMPLE_FALLING)
858 cfg1 |= ISI_CFG1_PIXCLK_POL_ACTIVE_FALLING;
860 if (isi->pdata->has_emb_sync)
861 cfg1 |= ISI_CFG1_EMB_SYNC;
862 if (isi->pdata->isi_full_mode)
863 cfg1 |= ISI_CFG1_FULL_MODE;
865 isi_writel(isi, ISI_CTRL, ISI_CTRL_DIS);
866 isi_writel(isi, ISI_CFG1, cfg1);
868 return 0;
871 static struct soc_camera_host_ops isi_soc_camera_host_ops = {
872 .owner = THIS_MODULE,
873 .add = isi_camera_add_device,
874 .remove = isi_camera_remove_device,
875 .set_fmt = isi_camera_set_fmt,
876 .try_fmt = isi_camera_try_fmt,
877 .get_formats = isi_camera_get_formats,
878 .init_videobuf2 = isi_camera_init_videobuf,
879 .poll = isi_camera_poll,
880 .querycap = isi_camera_querycap,
881 .set_bus_param = isi_camera_set_bus_param,
884 /* -----------------------------------------------------------------------*/
885 static int __devexit atmel_isi_remove(struct platform_device *pdev)
887 struct soc_camera_host *soc_host = to_soc_camera_host(&pdev->dev);
888 struct atmel_isi *isi = container_of(soc_host,
889 struct atmel_isi, soc_host);
891 free_irq(isi->irq, isi);
892 soc_camera_host_unregister(soc_host);
893 vb2_dma_contig_cleanup_ctx(isi->alloc_ctx);
894 dma_free_coherent(&pdev->dev,
895 sizeof(struct fbd) * MAX_BUFFER_NUM,
896 isi->p_fb_descriptors,
897 isi->fb_descriptors_phys);
899 iounmap(isi->regs);
900 clk_put(isi->pclk);
901 kfree(isi);
903 return 0;
906 static int __devinit atmel_isi_probe(struct platform_device *pdev)
908 unsigned int irq;
909 struct atmel_isi *isi;
910 struct clk *pclk;
911 struct resource *regs;
912 int ret, i;
913 struct device *dev = &pdev->dev;
914 struct soc_camera_host *soc_host;
915 struct isi_platform_data *pdata;
917 pdata = dev->platform_data;
918 if (!pdata || !pdata->data_width_flags) {
919 dev_err(&pdev->dev,
920 "No config available for Atmel ISI\n");
921 return -EINVAL;
924 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
925 if (!regs)
926 return -ENXIO;
928 pclk = clk_get(&pdev->dev, "isi_clk");
929 if (IS_ERR(pclk))
930 return PTR_ERR(pclk);
932 isi = kzalloc(sizeof(struct atmel_isi), GFP_KERNEL);
933 if (!isi) {
934 ret = -ENOMEM;
935 dev_err(&pdev->dev, "Can't allocate interface!\n");
936 goto err_alloc_isi;
939 isi->pclk = pclk;
940 isi->pdata = pdata;
941 isi->active = NULL;
942 spin_lock_init(&isi->lock);
943 init_waitqueue_head(&isi->vsync_wq);
944 INIT_LIST_HEAD(&isi->video_buffer_list);
945 INIT_LIST_HEAD(&isi->dma_desc_head);
947 isi->p_fb_descriptors = dma_alloc_coherent(&pdev->dev,
948 sizeof(struct fbd) * MAX_BUFFER_NUM,
949 &isi->fb_descriptors_phys,
950 GFP_KERNEL);
951 if (!isi->p_fb_descriptors) {
952 ret = -ENOMEM;
953 dev_err(&pdev->dev, "Can't allocate descriptors!\n");
954 goto err_alloc_descriptors;
957 for (i = 0; i < MAX_BUFFER_NUM; i++) {
958 isi->dma_desc[i].p_fbd = isi->p_fb_descriptors + i;
959 isi->dma_desc[i].fbd_phys = isi->fb_descriptors_phys +
960 i * sizeof(struct fbd);
961 list_add(&isi->dma_desc[i].list, &isi->dma_desc_head);
964 isi->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
965 if (IS_ERR(isi->alloc_ctx)) {
966 ret = PTR_ERR(isi->alloc_ctx);
967 goto err_alloc_ctx;
970 isi->regs = ioremap(regs->start, resource_size(regs));
971 if (!isi->regs) {
972 ret = -ENOMEM;
973 goto err_ioremap;
976 isi_writel(isi, ISI_CTRL, ISI_CTRL_DIS);
978 irq = platform_get_irq(pdev, 0);
979 if (irq < 0) {
980 ret = irq;
981 goto err_req_irq;
984 ret = request_irq(irq, isi_interrupt, 0, "isi", isi);
985 if (ret) {
986 dev_err(&pdev->dev, "Unable to request irq %d\n", irq);
987 goto err_req_irq;
989 isi->irq = irq;
991 soc_host = &isi->soc_host;
992 soc_host->drv_name = "isi-camera";
993 soc_host->ops = &isi_soc_camera_host_ops;
994 soc_host->priv = isi;
995 soc_host->v4l2_dev.dev = &pdev->dev;
996 soc_host->nr = pdev->id;
998 ret = soc_camera_host_register(soc_host);
999 if (ret) {
1000 dev_err(&pdev->dev, "Unable to register soc camera host\n");
1001 goto err_register_soc_camera_host;
1003 return 0;
1005 err_register_soc_camera_host:
1006 free_irq(isi->irq, isi);
1007 err_req_irq:
1008 iounmap(isi->regs);
1009 err_ioremap:
1010 vb2_dma_contig_cleanup_ctx(isi->alloc_ctx);
1011 err_alloc_ctx:
1012 dma_free_coherent(&pdev->dev,
1013 sizeof(struct fbd) * MAX_BUFFER_NUM,
1014 isi->p_fb_descriptors,
1015 isi->fb_descriptors_phys);
1016 err_alloc_descriptors:
1017 kfree(isi);
1018 err_alloc_isi:
1019 clk_put(isi->pclk);
1021 return ret;
1024 static struct platform_driver atmel_isi_driver = {
1025 .probe = atmel_isi_probe,
1026 .remove = __devexit_p(atmel_isi_remove),
1027 .driver = {
1028 .name = "atmel_isi",
1029 .owner = THIS_MODULE,
1033 static int __init atmel_isi_init_module(void)
1035 return platform_driver_probe(&atmel_isi_driver, &atmel_isi_probe);
1038 static void __exit atmel_isi_exit(void)
1040 platform_driver_unregister(&atmel_isi_driver);
1042 module_init(atmel_isi_init_module);
1043 module_exit(atmel_isi_exit);
1045 MODULE_AUTHOR("Josh Wu <josh.wu@atmel.com>");
1046 MODULE_DESCRIPTION("The V4L2 driver for Atmel Linux");
1047 MODULE_LICENSE("GPL");
1048 MODULE_SUPPORTED_DEVICE("video");