Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 1 | /* |
| 2 | * V4L2 Driver for i.MXL/i.MXL camera (CSI) host |
| 3 | * |
| 4 | * Copyright (C) 2008, Paulius Zaleckas <paulius.zaleckas@teltonika.lt> |
| 5 | * Copyright (C) 2009, Darius Augulis <augulis.darius@gmail.com> |
| 6 | * |
| 7 | * Based on PXA SoC camera driver |
| 8 | * Copyright (C) 2006, Sascha Hauer, Pengutronix |
| 9 | * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de> |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License version 2 as |
| 13 | * published by the Free Software Foundation. |
| 14 | */ |
| 15 | |
| 16 | #include <linux/clk.h> |
| 17 | #include <linux/delay.h> |
| 18 | #include <linux/device.h> |
| 19 | #include <linux/dma-mapping.h> |
| 20 | #include <linux/errno.h> |
| 21 | #include <linux/fs.h> |
| 22 | #include <linux/init.h> |
| 23 | #include <linux/interrupt.h> |
| 24 | #include <linux/io.h> |
| 25 | #include <linux/kernel.h> |
| 26 | #include <linux/mm.h> |
| 27 | #include <linux/module.h> |
| 28 | #include <linux/moduleparam.h> |
| 29 | #include <linux/mutex.h> |
| 30 | #include <linux/platform_device.h> |
Guennadi Liakhovetski | f39c1ab | 2009-11-09 16:11:34 -0300 | [diff] [blame] | 31 | #include <linux/sched.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 32 | #include <linux/slab.h> |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 33 | #include <linux/time.h> |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 34 | #include <linux/videodev2.h> |
| 35 | |
| 36 | #include <media/soc_camera.h> |
| 37 | #include <media/v4l2-common.h> |
| 38 | #include <media/v4l2-dev.h> |
| 39 | #include <media/videobuf-dma-contig.h> |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 40 | #include <media/soc_mediabus.h> |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 41 | |
| 42 | #include <asm/dma.h> |
| 43 | #include <asm/fiq.h> |
| 44 | #include <mach/dma-mx1-mx2.h> |
| 45 | #include <mach/hardware.h> |
| 46 | #include <mach/mx1_camera.h> |
| 47 | |
| 48 | /* |
| 49 | * CSI registers |
| 50 | */ |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 51 | #define CSICR1 0x00 /* CSI Control Register 1 */ |
| 52 | #define CSISR 0x08 /* CSI Status Register */ |
| 53 | #define CSIRXR 0x10 /* CSI RxFIFO Register */ |
| 54 | |
| 55 | #define CSICR1_RXFF_LEVEL(x) (((x) & 0x3) << 19) |
| 56 | #define CSICR1_SOF_POL (1 << 17) |
| 57 | #define CSICR1_SOF_INTEN (1 << 16) |
| 58 | #define CSICR1_MCLKDIV(x) (((x) & 0xf) << 12) |
| 59 | #define CSICR1_MCLKEN (1 << 9) |
| 60 | #define CSICR1_FCC (1 << 8) |
| 61 | #define CSICR1_BIG_ENDIAN (1 << 7) |
| 62 | #define CSICR1_CLR_RXFIFO (1 << 5) |
| 63 | #define CSICR1_GCLK_MODE (1 << 4) |
| 64 | #define CSICR1_DATA_POL (1 << 2) |
| 65 | #define CSICR1_REDGE (1 << 1) |
| 66 | #define CSICR1_EN (1 << 0) |
| 67 | |
| 68 | #define CSISR_SFF_OR_INT (1 << 25) |
| 69 | #define CSISR_RFF_OR_INT (1 << 24) |
| 70 | #define CSISR_STATFF_INT (1 << 21) |
| 71 | #define CSISR_RXFF_INT (1 << 18) |
| 72 | #define CSISR_SOF_INT (1 << 16) |
| 73 | #define CSISR_DRDY (1 << 0) |
| 74 | |
Mauro Carvalho Chehab | 64dc3c1 | 2011-06-25 11:28:37 -0300 | [diff] [blame] | 75 | #define DRIVER_VERSION "0.0.2" |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 76 | #define DRIVER_NAME "mx1-camera" |
| 77 | |
| 78 | #define CSI_IRQ_MASK (CSISR_SFF_OR_INT | CSISR_RFF_OR_INT | \ |
| 79 | CSISR_STATFF_INT | CSISR_RXFF_INT | CSISR_SOF_INT) |
| 80 | |
Guennadi Liakhovetski | 8acbfd3 | 2011-07-27 12:30:21 -0300 | [diff] [blame] | 81 | #define CSI_BUS_FLAGS (V4L2_MBUS_MASTER | V4L2_MBUS_HSYNC_ACTIVE_HIGH | \ |
| 82 | V4L2_MBUS_VSYNC_ACTIVE_HIGH | V4L2_MBUS_VSYNC_ACTIVE_LOW | \ |
| 83 | V4L2_MBUS_PCLK_SAMPLE_RISING | V4L2_MBUS_PCLK_SAMPLE_FALLING | \ |
| 84 | V4L2_MBUS_DATA_ACTIVE_HIGH | V4L2_MBUS_DATA_ACTIVE_LOW) |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 85 | |
| 86 | #define MAX_VIDEO_MEM 16 /* Video memory limit in megabytes */ |
| 87 | |
| 88 | /* |
| 89 | * Structures |
| 90 | */ |
| 91 | |
| 92 | /* buffer for one video frame */ |
| 93 | struct mx1_buffer { |
| 94 | /* common v4l buffer stuff -- must be first */ |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 95 | struct videobuf_buffer vb; |
| 96 | enum v4l2_mbus_pixelcode code; |
| 97 | int inwork; |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 98 | }; |
| 99 | |
Guennadi Liakhovetski | 5d28d52 | 2009-12-11 11:15:05 -0300 | [diff] [blame] | 100 | /* |
| 101 | * i.MX1/i.MXL is only supposed to handle one camera on its Camera Sensor |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 102 | * Interface. If anyone ever builds hardware to enable more than |
Guennadi Liakhovetski | 5d28d52 | 2009-12-11 11:15:05 -0300 | [diff] [blame] | 103 | * one camera, they will have to modify this driver too |
| 104 | */ |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 105 | struct mx1_camera_dev { |
Guennadi Liakhovetski | eb6c855 | 2009-04-24 12:55:18 -0300 | [diff] [blame] | 106 | struct soc_camera_host soc_host; |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 107 | struct soc_camera_device *icd; |
| 108 | struct mx1_camera_pdata *pdata; |
| 109 | struct mx1_buffer *active; |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 110 | struct resource *res; |
| 111 | struct clk *clk; |
| 112 | struct list_head capture; |
| 113 | |
| 114 | void __iomem *base; |
| 115 | int dma_chan; |
| 116 | unsigned int irq; |
| 117 | unsigned long mclk; |
| 118 | |
| 119 | spinlock_t lock; |
| 120 | }; |
| 121 | |
| 122 | /* |
| 123 | * Videobuf operations |
| 124 | */ |
| 125 | static int mx1_videobuf_setup(struct videobuf_queue *vq, unsigned int *count, |
| 126 | unsigned int *size) |
| 127 | { |
| 128 | struct soc_camera_device *icd = vq->priv_data; |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 129 | int bytes_per_line = soc_mbus_bytes_per_line(icd->user_width, |
| 130 | icd->current_fmt->host_fmt); |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 131 | |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 132 | if (bytes_per_line < 0) |
| 133 | return bytes_per_line; |
| 134 | |
| 135 | *size = bytes_per_line * icd->user_height; |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 136 | |
| 137 | if (!*count) |
| 138 | *count = 32; |
| 139 | |
Andreas Bombe | dab7e31 | 2010-03-21 16:02:45 -0300 | [diff] [blame] | 140 | if (*size * *count > MAX_VIDEO_MEM * 1024 * 1024) |
| 141 | *count = (MAX_VIDEO_MEM * 1024 * 1024) / *size; |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 142 | |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 143 | dev_dbg(icd->parent, "count=%d, size=%d\n", *count, *size); |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 144 | |
| 145 | return 0; |
| 146 | } |
| 147 | |
| 148 | static void free_buffer(struct videobuf_queue *vq, struct mx1_buffer *buf) |
| 149 | { |
| 150 | struct soc_camera_device *icd = vq->priv_data; |
| 151 | struct videobuf_buffer *vb = &buf->vb; |
| 152 | |
| 153 | BUG_ON(in_interrupt()); |
| 154 | |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 155 | dev_dbg(icd->parent, "%s (vb=0x%p) 0x%08lx %d\n", __func__, |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 156 | vb, vb->baddr, vb->bsize); |
| 157 | |
Guennadi Liakhovetski | 5d28d52 | 2009-12-11 11:15:05 -0300 | [diff] [blame] | 158 | /* |
| 159 | * This waits until this buffer is out of danger, i.e., until it is no |
| 160 | * longer in STATE_QUEUED or STATE_ACTIVE |
| 161 | */ |
Hans Verkuil | 0e0809a | 2010-09-26 09:01:26 -0300 | [diff] [blame] | 162 | videobuf_waiton(vq, vb, 0, 0); |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 163 | videobuf_dma_contig_free(vq, vb); |
| 164 | |
| 165 | vb->state = VIDEOBUF_NEEDS_INIT; |
| 166 | } |
| 167 | |
| 168 | static int mx1_videobuf_prepare(struct videobuf_queue *vq, |
| 169 | struct videobuf_buffer *vb, enum v4l2_field field) |
| 170 | { |
| 171 | struct soc_camera_device *icd = vq->priv_data; |
| 172 | struct mx1_buffer *buf = container_of(vb, struct mx1_buffer, vb); |
| 173 | int ret; |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 174 | int bytes_per_line = soc_mbus_bytes_per_line(icd->user_width, |
| 175 | icd->current_fmt->host_fmt); |
| 176 | |
| 177 | if (bytes_per_line < 0) |
| 178 | return bytes_per_line; |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 179 | |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 180 | dev_dbg(icd->parent, "%s (vb=0x%p) 0x%08lx %d\n", __func__, |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 181 | vb, vb->baddr, vb->bsize); |
| 182 | |
| 183 | /* Added list head initialization on alloc */ |
| 184 | WARN_ON(!list_empty(&vb->queue)); |
| 185 | |
| 186 | BUG_ON(NULL == icd->current_fmt); |
| 187 | |
Guennadi Liakhovetski | 5d28d52 | 2009-12-11 11:15:05 -0300 | [diff] [blame] | 188 | /* |
| 189 | * I think, in buf_prepare you only have to protect global data, |
| 190 | * the actual buffer is yours |
| 191 | */ |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 192 | buf->inwork = 1; |
| 193 | |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 194 | if (buf->code != icd->current_fmt->code || |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 195 | vb->width != icd->user_width || |
| 196 | vb->height != icd->user_height || |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 197 | vb->field != field) { |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 198 | buf->code = icd->current_fmt->code; |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 199 | vb->width = icd->user_width; |
| 200 | vb->height = icd->user_height; |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 201 | vb->field = field; |
| 202 | vb->state = VIDEOBUF_NEEDS_INIT; |
| 203 | } |
| 204 | |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 205 | vb->size = bytes_per_line * vb->height; |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 206 | if (0 != vb->baddr && vb->bsize < vb->size) { |
| 207 | ret = -EINVAL; |
| 208 | goto out; |
| 209 | } |
| 210 | |
| 211 | if (vb->state == VIDEOBUF_NEEDS_INIT) { |
| 212 | ret = videobuf_iolock(vq, vb, NULL); |
| 213 | if (ret) |
| 214 | goto fail; |
| 215 | |
| 216 | vb->state = VIDEOBUF_PREPARED; |
| 217 | } |
| 218 | |
| 219 | buf->inwork = 0; |
| 220 | |
| 221 | return 0; |
| 222 | |
| 223 | fail: |
| 224 | free_buffer(vq, buf); |
| 225 | out: |
| 226 | buf->inwork = 0; |
| 227 | return ret; |
| 228 | } |
| 229 | |
| 230 | static int mx1_camera_setup_dma(struct mx1_camera_dev *pcdev) |
| 231 | { |
| 232 | struct videobuf_buffer *vbuf = &pcdev->active->vb; |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 233 | struct device *dev = pcdev->icd->parent; |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 234 | int ret; |
| 235 | |
| 236 | if (unlikely(!pcdev->active)) { |
Guennadi Liakhovetski | 0166b74 | 2009-08-25 11:47:00 -0300 | [diff] [blame] | 237 | dev_err(dev, "DMA End IRQ with no active buffer\n"); |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 238 | return -EFAULT; |
| 239 | } |
| 240 | |
| 241 | /* setup sg list for future DMA */ |
| 242 | ret = imx_dma_setup_single(pcdev->dma_chan, |
| 243 | videobuf_to_dma_contig(vbuf), |
| 244 | vbuf->size, pcdev->res->start + |
| 245 | CSIRXR, DMA_MODE_READ); |
| 246 | if (unlikely(ret)) |
Guennadi Liakhovetski | 0166b74 | 2009-08-25 11:47:00 -0300 | [diff] [blame] | 247 | dev_err(dev, "Failed to setup DMA sg list\n"); |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 248 | |
| 249 | return ret; |
| 250 | } |
| 251 | |
Guennadi Liakhovetski | 2dd54a5 | 2009-08-05 20:06:31 -0300 | [diff] [blame] | 252 | /* Called under spinlock_irqsave(&pcdev->lock, ...) */ |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 253 | static void mx1_videobuf_queue(struct videobuf_queue *vq, |
| 254 | struct videobuf_buffer *vb) |
| 255 | { |
| 256 | struct soc_camera_device *icd = vq->priv_data; |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 257 | struct soc_camera_host *ici = to_soc_camera_host(icd->parent); |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 258 | struct mx1_camera_dev *pcdev = ici->priv; |
| 259 | struct mx1_buffer *buf = container_of(vb, struct mx1_buffer, vb); |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 260 | |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 261 | dev_dbg(icd->parent, "%s (vb=0x%p) 0x%08lx %d\n", __func__, |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 262 | vb, vb->baddr, vb->bsize); |
| 263 | |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 264 | list_add_tail(&vb->queue, &pcdev->capture); |
| 265 | |
| 266 | vb->state = VIDEOBUF_ACTIVE; |
| 267 | |
| 268 | if (!pcdev->active) { |
| 269 | pcdev->active = buf; |
| 270 | |
| 271 | /* setup sg list for future DMA */ |
| 272 | if (!mx1_camera_setup_dma(pcdev)) { |
| 273 | unsigned int temp; |
| 274 | /* enable SOF irq */ |
| 275 | temp = __raw_readl(pcdev->base + CSICR1) | |
| 276 | CSICR1_SOF_INTEN; |
| 277 | __raw_writel(temp, pcdev->base + CSICR1); |
| 278 | } |
| 279 | } |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | static void mx1_videobuf_release(struct videobuf_queue *vq, |
| 283 | struct videobuf_buffer *vb) |
| 284 | { |
| 285 | struct mx1_buffer *buf = container_of(vb, struct mx1_buffer, vb); |
| 286 | #ifdef DEBUG |
| 287 | struct soc_camera_device *icd = vq->priv_data; |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 288 | struct device *dev = icd->parent; |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 289 | |
Guennadi Liakhovetski | 0166b74 | 2009-08-25 11:47:00 -0300 | [diff] [blame] | 290 | dev_dbg(dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__, |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 291 | vb, vb->baddr, vb->bsize); |
| 292 | |
| 293 | switch (vb->state) { |
| 294 | case VIDEOBUF_ACTIVE: |
Guennadi Liakhovetski | 0166b74 | 2009-08-25 11:47:00 -0300 | [diff] [blame] | 295 | dev_dbg(dev, "%s (active)\n", __func__); |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 296 | break; |
| 297 | case VIDEOBUF_QUEUED: |
Guennadi Liakhovetski | 0166b74 | 2009-08-25 11:47:00 -0300 | [diff] [blame] | 298 | dev_dbg(dev, "%s (queued)\n", __func__); |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 299 | break; |
| 300 | case VIDEOBUF_PREPARED: |
Guennadi Liakhovetski | 0166b74 | 2009-08-25 11:47:00 -0300 | [diff] [blame] | 301 | dev_dbg(dev, "%s (prepared)\n", __func__); |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 302 | break; |
| 303 | default: |
Guennadi Liakhovetski | 0166b74 | 2009-08-25 11:47:00 -0300 | [diff] [blame] | 304 | dev_dbg(dev, "%s (unknown)\n", __func__); |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 305 | break; |
| 306 | } |
| 307 | #endif |
| 308 | |
| 309 | free_buffer(vq, buf); |
| 310 | } |
| 311 | |
| 312 | static void mx1_camera_wakeup(struct mx1_camera_dev *pcdev, |
| 313 | struct videobuf_buffer *vb, |
| 314 | struct mx1_buffer *buf) |
| 315 | { |
| 316 | /* _init is used to debug races, see comment in mx1_camera_reqbufs() */ |
| 317 | list_del_init(&vb->queue); |
| 318 | vb->state = VIDEOBUF_DONE; |
| 319 | do_gettimeofday(&vb->ts); |
| 320 | vb->field_count++; |
| 321 | wake_up(&vb->done); |
| 322 | |
| 323 | if (list_empty(&pcdev->capture)) { |
| 324 | pcdev->active = NULL; |
| 325 | return; |
| 326 | } |
| 327 | |
| 328 | pcdev->active = list_entry(pcdev->capture.next, |
| 329 | struct mx1_buffer, vb.queue); |
| 330 | |
| 331 | /* setup sg list for future DMA */ |
| 332 | if (likely(!mx1_camera_setup_dma(pcdev))) { |
| 333 | unsigned int temp; |
| 334 | |
| 335 | /* enable SOF irq */ |
| 336 | temp = __raw_readl(pcdev->base + CSICR1) | CSICR1_SOF_INTEN; |
| 337 | __raw_writel(temp, pcdev->base + CSICR1); |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | static void mx1_camera_dma_irq(int channel, void *data) |
| 342 | { |
| 343 | struct mx1_camera_dev *pcdev = data; |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 344 | struct device *dev = pcdev->icd->parent; |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 345 | struct mx1_buffer *buf; |
| 346 | struct videobuf_buffer *vb; |
| 347 | unsigned long flags; |
| 348 | |
| 349 | spin_lock_irqsave(&pcdev->lock, flags); |
| 350 | |
| 351 | imx_dma_disable(channel); |
| 352 | |
| 353 | if (unlikely(!pcdev->active)) { |
Guennadi Liakhovetski | 0166b74 | 2009-08-25 11:47:00 -0300 | [diff] [blame] | 354 | dev_err(dev, "DMA End IRQ with no active buffer\n"); |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 355 | goto out; |
| 356 | } |
| 357 | |
| 358 | vb = &pcdev->active->vb; |
| 359 | buf = container_of(vb, struct mx1_buffer, vb); |
| 360 | WARN_ON(buf->inwork || list_empty(&vb->queue)); |
Guennadi Liakhovetski | 0166b74 | 2009-08-25 11:47:00 -0300 | [diff] [blame] | 361 | dev_dbg(dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__, |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 362 | vb, vb->baddr, vb->bsize); |
| 363 | |
| 364 | mx1_camera_wakeup(pcdev, vb, buf); |
| 365 | out: |
| 366 | spin_unlock_irqrestore(&pcdev->lock, flags); |
| 367 | } |
| 368 | |
| 369 | static struct videobuf_queue_ops mx1_videobuf_ops = { |
| 370 | .buf_setup = mx1_videobuf_setup, |
| 371 | .buf_prepare = mx1_videobuf_prepare, |
| 372 | .buf_queue = mx1_videobuf_queue, |
| 373 | .buf_release = mx1_videobuf_release, |
| 374 | }; |
| 375 | |
| 376 | static void mx1_camera_init_videobuf(struct videobuf_queue *q, |
| 377 | struct soc_camera_device *icd) |
| 378 | { |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 379 | struct soc_camera_host *ici = to_soc_camera_host(icd->parent); |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 380 | struct mx1_camera_dev *pcdev = ici->priv; |
| 381 | |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 382 | videobuf_queue_dma_contig_init(q, &mx1_videobuf_ops, icd->parent, |
Guennadi Liakhovetski | b6a633c | 2010-12-25 17:40:26 -0300 | [diff] [blame] | 383 | &pcdev->lock, V4L2_BUF_TYPE_VIDEO_CAPTURE, |
| 384 | V4L2_FIELD_NONE, |
| 385 | sizeof(struct mx1_buffer), icd, &icd->video_lock); |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | static int mclk_get_divisor(struct mx1_camera_dev *pcdev) |
| 389 | { |
| 390 | unsigned int mclk = pcdev->mclk; |
| 391 | unsigned long div; |
| 392 | unsigned long lcdclk; |
| 393 | |
| 394 | lcdclk = clk_get_rate(pcdev->clk); |
| 395 | |
Guennadi Liakhovetski | 5d28d52 | 2009-12-11 11:15:05 -0300 | [diff] [blame] | 396 | /* |
| 397 | * We verify platform_mclk_10khz != 0, so if anyone breaks it, here |
| 398 | * they get a nice Oops |
| 399 | */ |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 400 | div = (lcdclk + 2 * mclk - 1) / (2 * mclk) - 1; |
| 401 | |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 402 | dev_dbg(pcdev->icd->parent, |
Guennadi Liakhovetski | 0166b74 | 2009-08-25 11:47:00 -0300 | [diff] [blame] | 403 | "System clock %lukHz, target freq %dkHz, divisor %lu\n", |
| 404 | lcdclk / 1000, mclk / 1000, div); |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 405 | |
| 406 | return div; |
| 407 | } |
| 408 | |
| 409 | static void mx1_camera_activate(struct mx1_camera_dev *pcdev) |
| 410 | { |
| 411 | unsigned int csicr1 = CSICR1_EN; |
| 412 | |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 413 | dev_dbg(pcdev->icd->parent, "Activate device\n"); |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 414 | |
| 415 | clk_enable(pcdev->clk); |
| 416 | |
| 417 | /* enable CSI before doing anything else */ |
| 418 | __raw_writel(csicr1, pcdev->base + CSICR1); |
| 419 | |
| 420 | csicr1 |= CSICR1_MCLKEN | CSICR1_FCC | CSICR1_GCLK_MODE; |
| 421 | csicr1 |= CSICR1_MCLKDIV(mclk_get_divisor(pcdev)); |
| 422 | csicr1 |= CSICR1_RXFF_LEVEL(2); /* 16 words */ |
| 423 | |
| 424 | __raw_writel(csicr1, pcdev->base + CSICR1); |
| 425 | } |
| 426 | |
| 427 | static void mx1_camera_deactivate(struct mx1_camera_dev *pcdev) |
| 428 | { |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 429 | dev_dbg(pcdev->icd->parent, "Deactivate device\n"); |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 430 | |
| 431 | /* Disable all CSI interface */ |
| 432 | __raw_writel(0x00, pcdev->base + CSICR1); |
| 433 | |
| 434 | clk_disable(pcdev->clk); |
| 435 | } |
| 436 | |
Guennadi Liakhovetski | 5d28d52 | 2009-12-11 11:15:05 -0300 | [diff] [blame] | 437 | /* |
| 438 | * The following two functions absolutely depend on the fact, that |
| 439 | * there can be only one camera on i.MX1/i.MXL camera sensor interface |
| 440 | */ |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 441 | static int mx1_camera_add_device(struct soc_camera_device *icd) |
| 442 | { |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 443 | struct soc_camera_host *ici = to_soc_camera_host(icd->parent); |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 444 | struct mx1_camera_dev *pcdev = ici->priv; |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 445 | |
Andre Bartke | 258c056 | 2011-06-10 07:57:54 -0300 | [diff] [blame] | 446 | if (pcdev->icd) |
| 447 | return -EBUSY; |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 448 | |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 449 | dev_info(icd->parent, "MX1 Camera driver attached to camera %d\n", |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 450 | icd->devnum); |
| 451 | |
| 452 | mx1_camera_activate(pcdev); |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 453 | |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 454 | pcdev->icd = icd; |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 455 | |
Andre Bartke | 258c056 | 2011-06-10 07:57:54 -0300 | [diff] [blame] | 456 | return 0; |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | static void mx1_camera_remove_device(struct soc_camera_device *icd) |
| 460 | { |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 461 | struct soc_camera_host *ici = to_soc_camera_host(icd->parent); |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 462 | struct mx1_camera_dev *pcdev = ici->priv; |
| 463 | unsigned int csicr1; |
| 464 | |
| 465 | BUG_ON(icd != pcdev->icd); |
| 466 | |
| 467 | /* disable interrupts */ |
| 468 | csicr1 = __raw_readl(pcdev->base + CSICR1) & ~CSI_IRQ_MASK; |
| 469 | __raw_writel(csicr1, pcdev->base + CSICR1); |
| 470 | |
| 471 | /* Stop DMA engine */ |
| 472 | imx_dma_disable(pcdev->dma_chan); |
| 473 | |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 474 | dev_info(icd->parent, "MX1 Camera driver detached from camera %d\n", |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 475 | icd->devnum); |
| 476 | |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 477 | mx1_camera_deactivate(pcdev); |
| 478 | |
| 479 | pcdev->icd = NULL; |
| 480 | } |
| 481 | |
| 482 | static int mx1_camera_set_crop(struct soc_camera_device *icd, |
Guennadi Liakhovetski | 08590b9 | 2009-08-25 11:46:54 -0300 | [diff] [blame] | 483 | struct v4l2_crop *a) |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 484 | { |
Guennadi Liakhovetski | c9c1f1c | 2009-08-25 11:46:59 -0300 | [diff] [blame] | 485 | struct v4l2_subdev *sd = soc_camera_to_subdev(icd); |
Guennadi Liakhovetski | 08590b9 | 2009-08-25 11:46:54 -0300 | [diff] [blame] | 486 | |
| 487 | return v4l2_subdev_call(sd, video, s_crop, a); |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 488 | } |
| 489 | |
Guennadi Liakhovetski | 8843d11 | 2011-09-21 17:52:51 -0300 | [diff] [blame] | 490 | static int mx1_camera_set_bus_param(struct soc_camera_device *icd) |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 491 | { |
Guennadi Liakhovetski | 8acbfd3 | 2011-07-27 12:30:21 -0300 | [diff] [blame] | 492 | struct v4l2_subdev *sd = soc_camera_to_subdev(icd); |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 493 | struct soc_camera_host *ici = to_soc_camera_host(icd->parent); |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 494 | struct mx1_camera_dev *pcdev = ici->priv; |
Guennadi Liakhovetski | 8acbfd3 | 2011-07-27 12:30:21 -0300 | [diff] [blame] | 495 | struct v4l2_mbus_config cfg = {.type = V4L2_MBUS_PARALLEL,}; |
| 496 | unsigned long common_flags; |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 497 | unsigned int csicr1; |
| 498 | int ret; |
| 499 | |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 500 | /* MX1 supports only 8bit buswidth */ |
Guennadi Liakhovetski | 8acbfd3 | 2011-07-27 12:30:21 -0300 | [diff] [blame] | 501 | ret = v4l2_subdev_call(sd, video, g_mbus_config, &cfg); |
| 502 | if (!ret) { |
| 503 | common_flags = soc_mbus_config_compatible(&cfg, CSI_BUS_FLAGS); |
| 504 | if (!common_flags) { |
| 505 | dev_warn(icd->parent, |
| 506 | "Flags incompatible: camera 0x%x, host 0x%x\n", |
| 507 | cfg.flags, CSI_BUS_FLAGS); |
| 508 | return -EINVAL; |
| 509 | } |
| 510 | } else if (ret != -ENOIOCTLCMD) { |
| 511 | return ret; |
| 512 | } else { |
| 513 | common_flags = CSI_BUS_FLAGS; |
| 514 | } |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 515 | |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 516 | /* Make choises, based on platform choice */ |
Guennadi Liakhovetski | 8acbfd3 | 2011-07-27 12:30:21 -0300 | [diff] [blame] | 517 | if ((common_flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH) && |
| 518 | (common_flags & V4L2_MBUS_VSYNC_ACTIVE_LOW)) { |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 519 | if (!pcdev->pdata || |
| 520 | pcdev->pdata->flags & MX1_CAMERA_VSYNC_HIGH) |
Guennadi Liakhovetski | 8acbfd3 | 2011-07-27 12:30:21 -0300 | [diff] [blame] | 521 | common_flags &= ~V4L2_MBUS_VSYNC_ACTIVE_LOW; |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 522 | else |
Guennadi Liakhovetski | 8acbfd3 | 2011-07-27 12:30:21 -0300 | [diff] [blame] | 523 | common_flags &= ~V4L2_MBUS_VSYNC_ACTIVE_HIGH; |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 524 | } |
| 525 | |
Guennadi Liakhovetski | 8acbfd3 | 2011-07-27 12:30:21 -0300 | [diff] [blame] | 526 | if ((common_flags & V4L2_MBUS_PCLK_SAMPLE_RISING) && |
| 527 | (common_flags & V4L2_MBUS_PCLK_SAMPLE_FALLING)) { |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 528 | if (!pcdev->pdata || |
| 529 | pcdev->pdata->flags & MX1_CAMERA_PCLK_RISING) |
Guennadi Liakhovetski | 8acbfd3 | 2011-07-27 12:30:21 -0300 | [diff] [blame] | 530 | common_flags &= ~V4L2_MBUS_PCLK_SAMPLE_FALLING; |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 531 | else |
Guennadi Liakhovetski | 8acbfd3 | 2011-07-27 12:30:21 -0300 | [diff] [blame] | 532 | common_flags &= ~V4L2_MBUS_PCLK_SAMPLE_RISING; |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 533 | } |
| 534 | |
Guennadi Liakhovetski | 8acbfd3 | 2011-07-27 12:30:21 -0300 | [diff] [blame] | 535 | if ((common_flags & V4L2_MBUS_DATA_ACTIVE_HIGH) && |
| 536 | (common_flags & V4L2_MBUS_DATA_ACTIVE_LOW)) { |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 537 | if (!pcdev->pdata || |
| 538 | pcdev->pdata->flags & MX1_CAMERA_DATA_HIGH) |
Guennadi Liakhovetski | 8acbfd3 | 2011-07-27 12:30:21 -0300 | [diff] [blame] | 539 | common_flags &= ~V4L2_MBUS_DATA_ACTIVE_LOW; |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 540 | else |
Guennadi Liakhovetski | 8acbfd3 | 2011-07-27 12:30:21 -0300 | [diff] [blame] | 541 | common_flags &= ~V4L2_MBUS_DATA_ACTIVE_HIGH; |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 542 | } |
| 543 | |
Guennadi Liakhovetski | 8acbfd3 | 2011-07-27 12:30:21 -0300 | [diff] [blame] | 544 | cfg.flags = common_flags; |
| 545 | ret = v4l2_subdev_call(sd, video, s_mbus_config, &cfg); |
| 546 | if (ret < 0 && ret != -ENOIOCTLCMD) { |
| 547 | dev_dbg(icd->parent, "camera s_mbus_config(0x%lx) returned %d\n", |
| 548 | common_flags, ret); |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 549 | return ret; |
Guennadi Liakhovetski | 8acbfd3 | 2011-07-27 12:30:21 -0300 | [diff] [blame] | 550 | } |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 551 | |
| 552 | csicr1 = __raw_readl(pcdev->base + CSICR1); |
| 553 | |
Guennadi Liakhovetski | 8acbfd3 | 2011-07-27 12:30:21 -0300 | [diff] [blame] | 554 | if (common_flags & V4L2_MBUS_PCLK_SAMPLE_RISING) |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 555 | csicr1 |= CSICR1_REDGE; |
Guennadi Liakhovetski | 8acbfd3 | 2011-07-27 12:30:21 -0300 | [diff] [blame] | 556 | if (common_flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH) |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 557 | csicr1 |= CSICR1_SOF_POL; |
Guennadi Liakhovetski | 8acbfd3 | 2011-07-27 12:30:21 -0300 | [diff] [blame] | 558 | if (common_flags & V4L2_MBUS_DATA_ACTIVE_LOW) |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 559 | csicr1 |= CSICR1_DATA_POL; |
| 560 | |
| 561 | __raw_writel(csicr1, pcdev->base + CSICR1); |
| 562 | |
| 563 | return 0; |
| 564 | } |
| 565 | |
| 566 | static int mx1_camera_set_fmt(struct soc_camera_device *icd, |
| 567 | struct v4l2_format *f) |
| 568 | { |
Guennadi Liakhovetski | c9c1f1c | 2009-08-25 11:46:59 -0300 | [diff] [blame] | 569 | struct v4l2_subdev *sd = soc_camera_to_subdev(icd); |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 570 | const struct soc_camera_format_xlate *xlate; |
| 571 | struct v4l2_pix_format *pix = &f->fmt.pix; |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 572 | struct v4l2_mbus_framefmt mf; |
| 573 | int ret, buswidth; |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 574 | |
| 575 | xlate = soc_camera_xlate_by_fourcc(icd, pix->pixelformat); |
| 576 | if (!xlate) { |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 577 | dev_warn(icd->parent, "Format %x not found\n", |
Guennadi Liakhovetski | 96c7539 | 2009-08-25 11:53:23 -0300 | [diff] [blame] | 578 | pix->pixelformat); |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 579 | return -EINVAL; |
| 580 | } |
| 581 | |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 582 | buswidth = xlate->host_fmt->bits_per_sample; |
| 583 | if (buswidth > 8) { |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 584 | dev_warn(icd->parent, |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 585 | "bits-per-sample %d for format %x unsupported\n", |
| 586 | buswidth, pix->pixelformat); |
| 587 | return -EINVAL; |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 588 | } |
| 589 | |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 590 | mf.width = pix->width; |
| 591 | mf.height = pix->height; |
| 592 | mf.field = pix->field; |
| 593 | mf.colorspace = pix->colorspace; |
| 594 | mf.code = xlate->code; |
| 595 | |
| 596 | ret = v4l2_subdev_call(sd, video, s_mbus_fmt, &mf); |
| 597 | if (ret < 0) |
| 598 | return ret; |
| 599 | |
| 600 | if (mf.code != xlate->code) |
| 601 | return -EINVAL; |
| 602 | |
| 603 | pix->width = mf.width; |
| 604 | pix->height = mf.height; |
| 605 | pix->field = mf.field; |
| 606 | pix->colorspace = mf.colorspace; |
| 607 | icd->current_fmt = xlate; |
| 608 | |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 609 | return ret; |
| 610 | } |
| 611 | |
| 612 | static int mx1_camera_try_fmt(struct soc_camera_device *icd, |
| 613 | struct v4l2_format *f) |
| 614 | { |
Guennadi Liakhovetski | c9c1f1c | 2009-08-25 11:46:59 -0300 | [diff] [blame] | 615 | struct v4l2_subdev *sd = soc_camera_to_subdev(icd); |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 616 | const struct soc_camera_format_xlate *xlate; |
| 617 | struct v4l2_pix_format *pix = &f->fmt.pix; |
| 618 | struct v4l2_mbus_framefmt mf; |
| 619 | int ret; |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 620 | /* TODO: limit to mx1 hardware capabilities */ |
| 621 | |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 622 | xlate = soc_camera_xlate_by_fourcc(icd, pix->pixelformat); |
| 623 | if (!xlate) { |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 624 | dev_warn(icd->parent, "Format %x not found\n", |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 625 | pix->pixelformat); |
| 626 | return -EINVAL; |
| 627 | } |
| 628 | |
| 629 | mf.width = pix->width; |
| 630 | mf.height = pix->height; |
| 631 | mf.field = pix->field; |
| 632 | mf.colorspace = pix->colorspace; |
| 633 | mf.code = xlate->code; |
| 634 | |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 635 | /* limit to sensor capabilities */ |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 636 | ret = v4l2_subdev_call(sd, video, try_mbus_fmt, &mf); |
| 637 | if (ret < 0) |
| 638 | return ret; |
| 639 | |
| 640 | pix->width = mf.width; |
| 641 | pix->height = mf.height; |
| 642 | pix->field = mf.field; |
| 643 | pix->colorspace = mf.colorspace; |
| 644 | |
| 645 | return 0; |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 646 | } |
| 647 | |
Guennadi Liakhovetski | 57bee29 | 2010-08-17 14:29:51 -0300 | [diff] [blame] | 648 | static int mx1_camera_reqbufs(struct soc_camera_device *icd, |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 649 | struct v4l2_requestbuffers *p) |
| 650 | { |
| 651 | int i; |
| 652 | |
Guennadi Liakhovetski | 5d28d52 | 2009-12-11 11:15:05 -0300 | [diff] [blame] | 653 | /* |
| 654 | * This is for locking debugging only. I removed spinlocks and now I |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 655 | * check whether .prepare is ever called on a linked buffer, or whether |
| 656 | * a dma IRQ can occur for an in-work or unlinked buffer. Until now |
Guennadi Liakhovetski | 5d28d52 | 2009-12-11 11:15:05 -0300 | [diff] [blame] | 657 | * it hadn't triggered |
| 658 | */ |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 659 | for (i = 0; i < p->count; i++) { |
Guennadi Liakhovetski | 57bee29 | 2010-08-17 14:29:51 -0300 | [diff] [blame] | 660 | struct mx1_buffer *buf = container_of(icd->vb_vidq.bufs[i], |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 661 | struct mx1_buffer, vb); |
| 662 | buf->inwork = 0; |
| 663 | INIT_LIST_HEAD(&buf->vb.queue); |
| 664 | } |
| 665 | |
| 666 | return 0; |
| 667 | } |
| 668 | |
| 669 | static unsigned int mx1_camera_poll(struct file *file, poll_table *pt) |
| 670 | { |
Guennadi Liakhovetski | 57bee29 | 2010-08-17 14:29:51 -0300 | [diff] [blame] | 671 | struct soc_camera_device *icd = file->private_data; |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 672 | struct mx1_buffer *buf; |
| 673 | |
Guennadi Liakhovetski | 57bee29 | 2010-08-17 14:29:51 -0300 | [diff] [blame] | 674 | buf = list_entry(icd->vb_vidq.stream.next, struct mx1_buffer, |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 675 | vb.stream); |
| 676 | |
| 677 | poll_wait(file, &buf->vb.done, pt); |
| 678 | |
| 679 | if (buf->vb.state == VIDEOBUF_DONE || |
| 680 | buf->vb.state == VIDEOBUF_ERROR) |
| 681 | return POLLIN | POLLRDNORM; |
| 682 | |
| 683 | return 0; |
| 684 | } |
| 685 | |
| 686 | static int mx1_camera_querycap(struct soc_camera_host *ici, |
| 687 | struct v4l2_capability *cap) |
| 688 | { |
| 689 | /* cap->name is set by the friendly caller:-> */ |
| 690 | strlcpy(cap->card, "i.MX1/i.MXL Camera", sizeof(cap->card)); |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 691 | cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING; |
| 692 | |
| 693 | return 0; |
| 694 | } |
| 695 | |
| 696 | static struct soc_camera_host_ops mx1_soc_camera_host_ops = { |
| 697 | .owner = THIS_MODULE, |
| 698 | .add = mx1_camera_add_device, |
| 699 | .remove = mx1_camera_remove_device, |
| 700 | .set_bus_param = mx1_camera_set_bus_param, |
| 701 | .set_crop = mx1_camera_set_crop, |
| 702 | .set_fmt = mx1_camera_set_fmt, |
| 703 | .try_fmt = mx1_camera_try_fmt, |
| 704 | .init_videobuf = mx1_camera_init_videobuf, |
| 705 | .reqbufs = mx1_camera_reqbufs, |
| 706 | .poll = mx1_camera_poll, |
| 707 | .querycap = mx1_camera_querycap, |
| 708 | }; |
| 709 | |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 710 | static struct fiq_handler fh = { |
| 711 | .name = "csi_sof" |
| 712 | }; |
| 713 | |
| 714 | static int __init mx1_camera_probe(struct platform_device *pdev) |
| 715 | { |
| 716 | struct mx1_camera_dev *pcdev; |
| 717 | struct resource *res; |
| 718 | struct pt_regs regs; |
| 719 | struct clk *clk; |
| 720 | void __iomem *base; |
| 721 | unsigned int irq; |
| 722 | int err = 0; |
| 723 | |
| 724 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 725 | irq = platform_get_irq(pdev, 0); |
Uwe Kleine-König | 30883ea | 2010-01-09 20:44:06 -0300 | [diff] [blame] | 726 | if (!res || (int)irq <= 0) { |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 727 | err = -ENODEV; |
| 728 | goto exit; |
| 729 | } |
| 730 | |
| 731 | clk = clk_get(&pdev->dev, "csi_clk"); |
| 732 | if (IS_ERR(clk)) { |
| 733 | err = PTR_ERR(clk); |
| 734 | goto exit; |
| 735 | } |
| 736 | |
| 737 | pcdev = kzalloc(sizeof(*pcdev), GFP_KERNEL); |
| 738 | if (!pcdev) { |
| 739 | dev_err(&pdev->dev, "Could not allocate pcdev\n"); |
| 740 | err = -ENOMEM; |
| 741 | goto exit_put_clk; |
| 742 | } |
| 743 | |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 744 | pcdev->res = res; |
| 745 | pcdev->clk = clk; |
| 746 | |
| 747 | pcdev->pdata = pdev->dev.platform_data; |
| 748 | |
| 749 | if (pcdev->pdata) |
| 750 | pcdev->mclk = pcdev->pdata->mclk_10khz * 10000; |
| 751 | |
| 752 | if (!pcdev->mclk) { |
| 753 | dev_warn(&pdev->dev, |
| 754 | "mclk_10khz == 0! Please, fix your platform data. " |
| 755 | "Using default 20MHz\n"); |
| 756 | pcdev->mclk = 20000000; |
| 757 | } |
| 758 | |
| 759 | INIT_LIST_HEAD(&pcdev->capture); |
| 760 | spin_lock_init(&pcdev->lock); |
| 761 | |
| 762 | /* |
| 763 | * Request the regions. |
| 764 | */ |
| 765 | if (!request_mem_region(res->start, resource_size(res), DRIVER_NAME)) { |
| 766 | err = -EBUSY; |
| 767 | goto exit_kfree; |
| 768 | } |
| 769 | |
| 770 | base = ioremap(res->start, resource_size(res)); |
| 771 | if (!base) { |
| 772 | err = -ENOMEM; |
| 773 | goto exit_release; |
| 774 | } |
| 775 | pcdev->irq = irq; |
| 776 | pcdev->base = base; |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 777 | |
| 778 | /* request dma */ |
| 779 | pcdev->dma_chan = imx_dma_request_by_prio(DRIVER_NAME, DMA_PRIO_HIGH); |
| 780 | if (pcdev->dma_chan < 0) { |
Guennadi Liakhovetski | eff505f | 2009-04-24 12:55:48 -0300 | [diff] [blame] | 781 | dev_err(&pdev->dev, "Can't request DMA for MX1 CSI\n"); |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 782 | err = -EBUSY; |
| 783 | goto exit_iounmap; |
| 784 | } |
Guennadi Liakhovetski | eff505f | 2009-04-24 12:55:48 -0300 | [diff] [blame] | 785 | dev_dbg(&pdev->dev, "got DMA channel %d\n", pcdev->dma_chan); |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 786 | |
| 787 | imx_dma_setup_handlers(pcdev->dma_chan, mx1_camera_dma_irq, NULL, |
| 788 | pcdev); |
| 789 | |
| 790 | imx_dma_config_channel(pcdev->dma_chan, IMX_DMA_TYPE_FIFO, |
Uwe Kleine-König | b7d41d6 | 2010-03-27 18:42:13 -0300 | [diff] [blame] | 791 | IMX_DMA_MEMSIZE_32, MX1_DMA_REQ_CSI_R, 0); |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 792 | /* burst length : 16 words = 64 bytes */ |
| 793 | imx_dma_config_burstlen(pcdev->dma_chan, 0); |
| 794 | |
| 795 | /* request irq */ |
| 796 | err = claim_fiq(&fh); |
| 797 | if (err) { |
Guennadi Liakhovetski | eff505f | 2009-04-24 12:55:48 -0300 | [diff] [blame] | 798 | dev_err(&pdev->dev, "Camera interrupt register failed \n"); |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 799 | goto exit_free_dma; |
| 800 | } |
| 801 | |
| 802 | set_fiq_handler(&mx1_camera_sof_fiq_start, &mx1_camera_sof_fiq_end - |
| 803 | &mx1_camera_sof_fiq_start); |
| 804 | |
Uwe Kleine-König | b7d41d6 | 2010-03-27 18:42:13 -0300 | [diff] [blame] | 805 | regs.ARM_r8 = (long)MX1_DMA_DIMR; |
| 806 | regs.ARM_r9 = (long)MX1_DMA_CCR(pcdev->dma_chan); |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 807 | regs.ARM_r10 = (long)pcdev->base + CSICR1; |
| 808 | regs.ARM_fp = (long)pcdev->base + CSISR; |
| 809 | regs.ARM_sp = 1 << pcdev->dma_chan; |
| 810 | set_fiq_regs(®s); |
| 811 | |
| 812 | mxc_set_irq_fiq(irq, 1); |
| 813 | enable_fiq(irq); |
| 814 | |
Guennadi Liakhovetski | eb6c855 | 2009-04-24 12:55:18 -0300 | [diff] [blame] | 815 | pcdev->soc_host.drv_name = DRIVER_NAME; |
| 816 | pcdev->soc_host.ops = &mx1_soc_camera_host_ops; |
| 817 | pcdev->soc_host.priv = pcdev; |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 818 | pcdev->soc_host.v4l2_dev.dev = &pdev->dev; |
Guennadi Liakhovetski | eb6c855 | 2009-04-24 12:55:18 -0300 | [diff] [blame] | 819 | pcdev->soc_host.nr = pdev->id; |
| 820 | err = soc_camera_host_register(&pcdev->soc_host); |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 821 | if (err) |
| 822 | goto exit_free_irq; |
| 823 | |
| 824 | dev_info(&pdev->dev, "MX1 Camera driver loaded\n"); |
| 825 | |
| 826 | return 0; |
| 827 | |
| 828 | exit_free_irq: |
| 829 | disable_fiq(irq); |
| 830 | mxc_set_irq_fiq(irq, 0); |
| 831 | release_fiq(&fh); |
| 832 | exit_free_dma: |
| 833 | imx_dma_free(pcdev->dma_chan); |
| 834 | exit_iounmap: |
| 835 | iounmap(base); |
| 836 | exit_release: |
| 837 | release_mem_region(res->start, resource_size(res)); |
| 838 | exit_kfree: |
| 839 | kfree(pcdev); |
| 840 | exit_put_clk: |
| 841 | clk_put(clk); |
| 842 | exit: |
| 843 | return err; |
| 844 | } |
| 845 | |
| 846 | static int __exit mx1_camera_remove(struct platform_device *pdev) |
| 847 | { |
Guennadi Liakhovetski | eff505f | 2009-04-24 12:55:48 -0300 | [diff] [blame] | 848 | struct soc_camera_host *soc_host = to_soc_camera_host(&pdev->dev); |
| 849 | struct mx1_camera_dev *pcdev = container_of(soc_host, |
| 850 | struct mx1_camera_dev, soc_host); |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 851 | struct resource *res; |
| 852 | |
| 853 | imx_dma_free(pcdev->dma_chan); |
| 854 | disable_fiq(pcdev->irq); |
| 855 | mxc_set_irq_fiq(pcdev->irq, 0); |
| 856 | release_fiq(&fh); |
| 857 | |
| 858 | clk_put(pcdev->clk); |
| 859 | |
Guennadi Liakhovetski | eff505f | 2009-04-24 12:55:48 -0300 | [diff] [blame] | 860 | soc_camera_host_unregister(soc_host); |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 861 | |
| 862 | iounmap(pcdev->base); |
| 863 | |
| 864 | res = pcdev->res; |
| 865 | release_mem_region(res->start, resource_size(res)); |
| 866 | |
| 867 | kfree(pcdev); |
| 868 | |
| 869 | dev_info(&pdev->dev, "MX1 Camera driver unloaded\n"); |
| 870 | |
| 871 | return 0; |
| 872 | } |
| 873 | |
| 874 | static struct platform_driver mx1_camera_driver = { |
| 875 | .driver = { |
| 876 | .name = DRIVER_NAME, |
| 877 | }, |
| 878 | .remove = __exit_p(mx1_camera_remove), |
| 879 | }; |
| 880 | |
| 881 | static int __init mx1_camera_init(void) |
| 882 | { |
| 883 | return platform_driver_probe(&mx1_camera_driver, mx1_camera_probe); |
| 884 | } |
| 885 | |
| 886 | static void __exit mx1_camera_exit(void) |
| 887 | { |
| 888 | return platform_driver_unregister(&mx1_camera_driver); |
| 889 | } |
| 890 | |
| 891 | module_init(mx1_camera_init); |
| 892 | module_exit(mx1_camera_exit); |
| 893 | |
| 894 | MODULE_DESCRIPTION("i.MX1/i.MXL SoC Camera Host driver"); |
| 895 | MODULE_AUTHOR("Paulius Zaleckas <paulius.zaleckas@teltonika.lt>"); |
| 896 | MODULE_LICENSE("GPL v2"); |
Mauro Carvalho Chehab | 64dc3c1 | 2011-06-25 11:28:37 -0300 | [diff] [blame] | 897 | MODULE_VERSION(DRIVER_VERSION); |
Paulius Zaleckas | 6acc81c | 2009-04-03 10:34:05 -0300 | [diff] [blame] | 898 | MODULE_ALIAS("platform:" DRIVER_NAME); |