Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 1 | /* |
| 2 | * V4L2 Driver for SuperH Mobile CEU interface |
| 3 | * |
| 4 | * Copyright (C) 2008 Magnus Damm |
| 5 | * |
| 6 | * Based on V4L2 Driver for PXA camera host - "pxa_camera.c", |
| 7 | * |
| 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 as published by |
| 13 | * the Free Software Foundation; either version 2 of the License, or |
| 14 | * (at your option) any later version. |
| 15 | */ |
| 16 | |
| 17 | #include <linux/init.h> |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/io.h> |
| 20 | #include <linux/delay.h> |
| 21 | #include <linux/dma-mapping.h> |
| 22 | #include <linux/errno.h> |
| 23 | #include <linux/fs.h> |
| 24 | #include <linux/interrupt.h> |
| 25 | #include <linux/kernel.h> |
| 26 | #include <linux/mm.h> |
| 27 | #include <linux/moduleparam.h> |
| 28 | #include <linux/time.h> |
| 29 | #include <linux/version.h> |
| 30 | #include <linux/device.h> |
| 31 | #include <linux/platform_device.h> |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 32 | #include <linux/videodev2.h> |
Magnus Damm | 6d1386c | 2009-08-14 10:49:17 +0000 | [diff] [blame] | 33 | #include <linux/pm_runtime.h> |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 34 | |
| 35 | #include <media/v4l2-common.h> |
| 36 | #include <media/v4l2-dev.h> |
| 37 | #include <media/soc_camera.h> |
| 38 | #include <media/sh_mobile_ceu.h> |
| 39 | #include <media/videobuf-dma-contig.h> |
| 40 | |
| 41 | /* register offsets for sh7722 / sh7723 */ |
| 42 | |
Magnus Damm | dd54203 | 2008-10-16 19:50:22 -0300 | [diff] [blame] | 43 | #define CAPSR 0x00 /* Capture start register */ |
| 44 | #define CAPCR 0x04 /* Capture control register */ |
| 45 | #define CAMCR 0x08 /* Capture interface control register */ |
| 46 | #define CMCYR 0x0c /* Capture interface cycle register */ |
| 47 | #define CAMOR 0x10 /* Capture interface offset register */ |
| 48 | #define CAPWR 0x14 /* Capture interface width register */ |
| 49 | #define CAIFR 0x18 /* Capture interface input format register */ |
| 50 | #define CSTCR 0x20 /* Camera strobe control register (<= sh7722) */ |
| 51 | #define CSECR 0x24 /* Camera strobe emission count register (<= sh7722) */ |
| 52 | #define CRCNTR 0x28 /* CEU register control register */ |
| 53 | #define CRCMPR 0x2c /* CEU register forcible control register */ |
| 54 | #define CFLCR 0x30 /* Capture filter control register */ |
| 55 | #define CFSZR 0x34 /* Capture filter size clip register */ |
| 56 | #define CDWDR 0x38 /* Capture destination width register */ |
| 57 | #define CDAYR 0x3c /* Capture data address Y register */ |
| 58 | #define CDACR 0x40 /* Capture data address C register */ |
| 59 | #define CDBYR 0x44 /* Capture data bottom-field address Y register */ |
| 60 | #define CDBCR 0x48 /* Capture data bottom-field address C register */ |
| 61 | #define CBDSR 0x4c /* Capture bundle destination size register */ |
| 62 | #define CFWCR 0x5c /* Firewall operation control register */ |
| 63 | #define CLFCR 0x60 /* Capture low-pass filter control register */ |
| 64 | #define CDOCR 0x64 /* Capture data output control register */ |
| 65 | #define CDDCR 0x68 /* Capture data complexity level register */ |
| 66 | #define CDDAR 0x6c /* Capture data complexity level address register */ |
| 67 | #define CEIER 0x70 /* Capture event interrupt enable register */ |
| 68 | #define CETCR 0x74 /* Capture event flag clear register */ |
| 69 | #define CSTSR 0x7c /* Capture status register */ |
| 70 | #define CSRTR 0x80 /* Capture software reset register */ |
| 71 | #define CDSSR 0x84 /* Capture data size register */ |
| 72 | #define CDAYR2 0x90 /* Capture data address Y register 2 */ |
| 73 | #define CDACR2 0x94 /* Capture data address C register 2 */ |
| 74 | #define CDBYR2 0x98 /* Capture data bottom-field address Y register 2 */ |
| 75 | #define CDBCR2 0x9c /* Capture data bottom-field address C register 2 */ |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 76 | |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 77 | /* per video frame buffer */ |
| 78 | struct sh_mobile_ceu_buffer { |
| 79 | struct videobuf_buffer vb; /* v4l buffer must be first */ |
| 80 | const struct soc_camera_data_format *fmt; |
| 81 | }; |
| 82 | |
| 83 | struct sh_mobile_ceu_dev { |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 84 | struct soc_camera_host ici; |
| 85 | struct soc_camera_device *icd; |
| 86 | |
| 87 | unsigned int irq; |
| 88 | void __iomem *base; |
| 89 | unsigned long video_limit; |
| 90 | |
Guennadi Liakhovetski | c60f2b5 | 2008-07-17 17:30:47 -0300 | [diff] [blame] | 91 | /* lock used to protect videobuf */ |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 92 | spinlock_t lock; |
| 93 | struct list_head capture; |
| 94 | struct videobuf_buffer *active; |
Guennadi Liakhovetski | e802967 | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 95 | int is_interlaced; |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 96 | |
| 97 | struct sh_mobile_ceu_info *pdata; |
Magnus Damm | 8be65dc | 2008-12-18 12:38:06 -0300 | [diff] [blame] | 98 | |
| 99 | const struct soc_camera_data_format *camera_fmt; |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 100 | }; |
| 101 | |
Kuninori Morimoto | c354b40 | 2009-02-23 12:12:58 -0300 | [diff] [blame] | 102 | static unsigned long make_bus_param(struct sh_mobile_ceu_dev *pcdev) |
| 103 | { |
| 104 | unsigned long flags; |
| 105 | |
| 106 | flags = SOCAM_MASTER | |
| 107 | SOCAM_PCLK_SAMPLE_RISING | |
| 108 | SOCAM_HSYNC_ACTIVE_HIGH | |
| 109 | SOCAM_HSYNC_ACTIVE_LOW | |
| 110 | SOCAM_VSYNC_ACTIVE_HIGH | |
| 111 | SOCAM_VSYNC_ACTIVE_LOW | |
| 112 | SOCAM_DATA_ACTIVE_HIGH; |
| 113 | |
| 114 | if (pcdev->pdata->flags & SH_CEU_FLAG_USE_8BIT_BUS) |
| 115 | flags |= SOCAM_DATAWIDTH_8; |
| 116 | |
| 117 | if (pcdev->pdata->flags & SH_CEU_FLAG_USE_16BIT_BUS) |
| 118 | flags |= SOCAM_DATAWIDTH_16; |
| 119 | |
| 120 | if (flags & SOCAM_DATAWIDTH_MASK) |
| 121 | return flags; |
| 122 | |
| 123 | return 0; |
| 124 | } |
| 125 | |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 126 | static void ceu_write(struct sh_mobile_ceu_dev *priv, |
Magnus Damm | 7a1a816 | 2008-12-18 12:50:30 -0300 | [diff] [blame] | 127 | unsigned long reg_offs, u32 data) |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 128 | { |
| 129 | iowrite32(data, priv->base + reg_offs); |
| 130 | } |
| 131 | |
Magnus Damm | 7a1a816 | 2008-12-18 12:50:30 -0300 | [diff] [blame] | 132 | static u32 ceu_read(struct sh_mobile_ceu_dev *priv, unsigned long reg_offs) |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 133 | { |
| 134 | return ioread32(priv->base + reg_offs); |
| 135 | } |
| 136 | |
| 137 | /* |
| 138 | * Videobuf operations |
| 139 | */ |
| 140 | static int sh_mobile_ceu_videobuf_setup(struct videobuf_queue *vq, |
| 141 | unsigned int *count, |
| 142 | unsigned int *size) |
| 143 | { |
| 144 | struct soc_camera_device *icd = vq->priv_data; |
| 145 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
| 146 | struct sh_mobile_ceu_dev *pcdev = ici->priv; |
| 147 | int bytes_per_pixel = (icd->current_fmt->depth + 7) >> 3; |
| 148 | |
| 149 | *size = PAGE_ALIGN(icd->width * icd->height * bytes_per_pixel); |
| 150 | |
| 151 | if (0 == *count) |
| 152 | *count = 2; |
| 153 | |
| 154 | if (pcdev->video_limit) { |
| 155 | while (*size * *count > pcdev->video_limit) |
| 156 | (*count)--; |
| 157 | } |
| 158 | |
| 159 | dev_dbg(&icd->dev, "count=%d, size=%d\n", *count, *size); |
| 160 | |
| 161 | return 0; |
| 162 | } |
| 163 | |
| 164 | static void free_buffer(struct videobuf_queue *vq, |
| 165 | struct sh_mobile_ceu_buffer *buf) |
| 166 | { |
| 167 | struct soc_camera_device *icd = vq->priv_data; |
| 168 | |
Hans Verkuil | 86751b0 | 2008-07-18 01:35:14 -0300 | [diff] [blame] | 169 | dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %zd\n", __func__, |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 170 | &buf->vb, buf->vb.baddr, buf->vb.bsize); |
| 171 | |
| 172 | if (in_interrupt()) |
| 173 | BUG(); |
| 174 | |
Magnus Damm | 97215cb | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 175 | videobuf_waiton(&buf->vb, 0, 0); |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 176 | videobuf_dma_contig_free(vq, &buf->vb); |
| 177 | dev_dbg(&icd->dev, "%s freed\n", __func__); |
| 178 | buf->vb.state = VIDEOBUF_NEEDS_INIT; |
| 179 | } |
| 180 | |
Magnus Damm | 7a1a816 | 2008-12-18 12:50:30 -0300 | [diff] [blame] | 181 | #define CEU_CETCR_MAGIC 0x0317f313 /* acknowledge magical interrupt sources */ |
| 182 | #define CEU_CETCR_IGRW (1 << 4) /* prohibited register access interrupt bit */ |
| 183 | #define CEU_CEIER_CPEIE (1 << 0) /* one-frame capture end interrupt */ |
| 184 | #define CEU_CAPCR_CTNCP (1 << 16) /* continuous capture mode (if set) */ |
| 185 | |
| 186 | |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 187 | static void sh_mobile_ceu_capture(struct sh_mobile_ceu_dev *pcdev) |
| 188 | { |
Magnus Damm | 8be65dc | 2008-12-18 12:38:06 -0300 | [diff] [blame] | 189 | struct soc_camera_device *icd = pcdev->icd; |
Kuninori Morimoto | ccab8a2 | 2008-12-18 12:51:21 -0300 | [diff] [blame] | 190 | dma_addr_t phys_addr_top, phys_addr_bottom; |
Magnus Damm | 8be65dc | 2008-12-18 12:38:06 -0300 | [diff] [blame] | 191 | |
Magnus Damm | 7a1a816 | 2008-12-18 12:50:30 -0300 | [diff] [blame] | 192 | /* The hardware is _very_ picky about this sequence. Especially |
| 193 | * the CEU_CETCR_MAGIC value. It seems like we need to acknowledge |
| 194 | * several not-so-well documented interrupt sources in CETCR. |
| 195 | */ |
| 196 | ceu_write(pcdev, CEIER, ceu_read(pcdev, CEIER) & ~CEU_CEIER_CPEIE); |
| 197 | ceu_write(pcdev, CETCR, ~ceu_read(pcdev, CETCR) & CEU_CETCR_MAGIC); |
| 198 | ceu_write(pcdev, CEIER, ceu_read(pcdev, CEIER) | CEU_CEIER_CPEIE); |
| 199 | ceu_write(pcdev, CAPCR, ceu_read(pcdev, CAPCR) & ~CEU_CAPCR_CTNCP); |
| 200 | ceu_write(pcdev, CETCR, CEU_CETCR_MAGIC ^ CEU_CETCR_IGRW); |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 201 | |
Magnus Damm | 8be65dc | 2008-12-18 12:38:06 -0300 | [diff] [blame] | 202 | if (!pcdev->active) |
| 203 | return; |
| 204 | |
Kuninori Morimoto | ccab8a2 | 2008-12-18 12:51:21 -0300 | [diff] [blame] | 205 | phys_addr_top = videobuf_to_dma_contig(pcdev->active); |
| 206 | ceu_write(pcdev, CDAYR, phys_addr_top); |
Guennadi Liakhovetski | e802967 | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 207 | if (pcdev->is_interlaced) { |
Kuninori Morimoto | ccab8a2 | 2008-12-18 12:51:21 -0300 | [diff] [blame] | 208 | phys_addr_bottom = phys_addr_top + icd->width; |
| 209 | ceu_write(pcdev, CDBYR, phys_addr_bottom); |
| 210 | } |
Magnus Damm | 8be65dc | 2008-12-18 12:38:06 -0300 | [diff] [blame] | 211 | |
| 212 | switch (icd->current_fmt->fourcc) { |
| 213 | case V4L2_PIX_FMT_NV12: |
| 214 | case V4L2_PIX_FMT_NV21: |
Magnus Damm | 9e4a56d | 2008-12-18 12:45:33 -0300 | [diff] [blame] | 215 | case V4L2_PIX_FMT_NV16: |
| 216 | case V4L2_PIX_FMT_NV61: |
Kuninori Morimoto | ccab8a2 | 2008-12-18 12:51:21 -0300 | [diff] [blame] | 217 | phys_addr_top += icd->width * icd->height; |
| 218 | ceu_write(pcdev, CDACR, phys_addr_top); |
Guennadi Liakhovetski | e802967 | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 219 | if (pcdev->is_interlaced) { |
Kuninori Morimoto | ccab8a2 | 2008-12-18 12:51:21 -0300 | [diff] [blame] | 220 | phys_addr_bottom = phys_addr_top + icd->width; |
| 221 | ceu_write(pcdev, CDBCR, phys_addr_bottom); |
| 222 | } |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 223 | } |
Magnus Damm | 8be65dc | 2008-12-18 12:38:06 -0300 | [diff] [blame] | 224 | |
| 225 | pcdev->active->state = VIDEOBUF_ACTIVE; |
| 226 | ceu_write(pcdev, CAPSR, 0x1); /* start capture */ |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | static int sh_mobile_ceu_videobuf_prepare(struct videobuf_queue *vq, |
| 230 | struct videobuf_buffer *vb, |
| 231 | enum v4l2_field field) |
| 232 | { |
| 233 | struct soc_camera_device *icd = vq->priv_data; |
| 234 | struct sh_mobile_ceu_buffer *buf; |
| 235 | int ret; |
| 236 | |
| 237 | buf = container_of(vb, struct sh_mobile_ceu_buffer, vb); |
| 238 | |
Hans Verkuil | 86751b0 | 2008-07-18 01:35:14 -0300 | [diff] [blame] | 239 | dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %zd\n", __func__, |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 240 | vb, vb->baddr, vb->bsize); |
| 241 | |
| 242 | /* Added list head initialization on alloc */ |
| 243 | WARN_ON(!list_empty(&vb->queue)); |
| 244 | |
| 245 | #ifdef DEBUG |
| 246 | /* This can be useful if you want to see if we actually fill |
| 247 | * the buffer with something */ |
| 248 | memset((void *)vb->baddr, 0xaa, vb->bsize); |
| 249 | #endif |
| 250 | |
| 251 | BUG_ON(NULL == icd->current_fmt); |
| 252 | |
| 253 | if (buf->fmt != icd->current_fmt || |
| 254 | vb->width != icd->width || |
| 255 | vb->height != icd->height || |
| 256 | vb->field != field) { |
| 257 | buf->fmt = icd->current_fmt; |
| 258 | vb->width = icd->width; |
| 259 | vb->height = icd->height; |
| 260 | vb->field = field; |
| 261 | vb->state = VIDEOBUF_NEEDS_INIT; |
| 262 | } |
| 263 | |
| 264 | vb->size = vb->width * vb->height * ((buf->fmt->depth + 7) >> 3); |
| 265 | if (0 != vb->baddr && vb->bsize < vb->size) { |
| 266 | ret = -EINVAL; |
| 267 | goto out; |
| 268 | } |
| 269 | |
| 270 | if (vb->state == VIDEOBUF_NEEDS_INIT) { |
| 271 | ret = videobuf_iolock(vq, vb, NULL); |
| 272 | if (ret) |
| 273 | goto fail; |
| 274 | vb->state = VIDEOBUF_PREPARED; |
| 275 | } |
| 276 | |
| 277 | return 0; |
| 278 | fail: |
| 279 | free_buffer(vq, buf); |
| 280 | out: |
| 281 | return ret; |
| 282 | } |
| 283 | |
Guennadi Liakhovetski | 2dd54a5 | 2009-08-05 20:06:31 -0300 | [diff] [blame] | 284 | /* Called under spinlock_irqsave(&pcdev->lock, ...) */ |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 285 | static void sh_mobile_ceu_videobuf_queue(struct videobuf_queue *vq, |
| 286 | struct videobuf_buffer *vb) |
| 287 | { |
| 288 | struct soc_camera_device *icd = vq->priv_data; |
| 289 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
| 290 | struct sh_mobile_ceu_dev *pcdev = ici->priv; |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 291 | |
Hans Verkuil | 86751b0 | 2008-07-18 01:35:14 -0300 | [diff] [blame] | 292 | dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %zd\n", __func__, |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 293 | vb, vb->baddr, vb->bsize); |
| 294 | |
Magnus Damm | 51354cc | 2008-10-16 19:51:20 -0300 | [diff] [blame] | 295 | vb->state = VIDEOBUF_QUEUED; |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 296 | list_add_tail(&vb->queue, &pcdev->capture); |
| 297 | |
| 298 | if (!pcdev->active) { |
| 299 | pcdev->active = vb; |
| 300 | sh_mobile_ceu_capture(pcdev); |
| 301 | } |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | static void sh_mobile_ceu_videobuf_release(struct videobuf_queue *vq, |
| 305 | struct videobuf_buffer *vb) |
| 306 | { |
| 307 | free_buffer(vq, container_of(vb, struct sh_mobile_ceu_buffer, vb)); |
| 308 | } |
| 309 | |
| 310 | static struct videobuf_queue_ops sh_mobile_ceu_videobuf_ops = { |
| 311 | .buf_setup = sh_mobile_ceu_videobuf_setup, |
| 312 | .buf_prepare = sh_mobile_ceu_videobuf_prepare, |
| 313 | .buf_queue = sh_mobile_ceu_videobuf_queue, |
| 314 | .buf_release = sh_mobile_ceu_videobuf_release, |
| 315 | }; |
| 316 | |
| 317 | static irqreturn_t sh_mobile_ceu_irq(int irq, void *data) |
| 318 | { |
| 319 | struct sh_mobile_ceu_dev *pcdev = data; |
| 320 | struct videobuf_buffer *vb; |
| 321 | unsigned long flags; |
| 322 | |
| 323 | spin_lock_irqsave(&pcdev->lock, flags); |
| 324 | |
| 325 | vb = pcdev->active; |
| 326 | list_del_init(&vb->queue); |
| 327 | |
| 328 | if (!list_empty(&pcdev->capture)) |
| 329 | pcdev->active = list_entry(pcdev->capture.next, |
| 330 | struct videobuf_buffer, queue); |
| 331 | else |
| 332 | pcdev->active = NULL; |
| 333 | |
| 334 | sh_mobile_ceu_capture(pcdev); |
| 335 | |
| 336 | vb->state = VIDEOBUF_DONE; |
| 337 | do_gettimeofday(&vb->ts); |
| 338 | vb->field_count++; |
| 339 | wake_up(&vb->done); |
| 340 | spin_unlock_irqrestore(&pcdev->lock, flags); |
| 341 | |
| 342 | return IRQ_HANDLED; |
| 343 | } |
| 344 | |
Guennadi Liakhovetski | 1c3bb74 | 2008-12-18 12:28:54 -0300 | [diff] [blame] | 345 | /* Called with .video_lock held */ |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 346 | static int sh_mobile_ceu_add_device(struct soc_camera_device *icd) |
| 347 | { |
| 348 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
| 349 | struct sh_mobile_ceu_dev *pcdev = ici->priv; |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 350 | |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 351 | if (pcdev->icd) |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 352 | return -EBUSY; |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 353 | |
| 354 | dev_info(&icd->dev, |
| 355 | "SuperH Mobile CEU driver attached to camera %d\n", |
| 356 | icd->devnum); |
| 357 | |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 358 | clk_enable(pcdev->clk); |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 359 | |
| 360 | ceu_write(pcdev, CAPSR, 1 << 16); /* reset */ |
| 361 | while (ceu_read(pcdev, CSTSR) & 1) |
| 362 | msleep(1); |
| 363 | |
| 364 | pcdev->icd = icd; |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 365 | |
| 366 | return 0; |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 367 | } |
| 368 | |
Guennadi Liakhovetski | 1c3bb74 | 2008-12-18 12:28:54 -0300 | [diff] [blame] | 369 | /* Called with .video_lock held */ |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 370 | static void sh_mobile_ceu_remove_device(struct soc_camera_device *icd) |
| 371 | { |
| 372 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
| 373 | struct sh_mobile_ceu_dev *pcdev = ici->priv; |
Magnus Damm | 51354cc | 2008-10-16 19:51:20 -0300 | [diff] [blame] | 374 | unsigned long flags; |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 375 | |
| 376 | BUG_ON(icd != pcdev->icd); |
| 377 | |
| 378 | /* disable capture, disable interrupts */ |
| 379 | ceu_write(pcdev, CEIER, 0); |
| 380 | ceu_write(pcdev, CAPSR, 1 << 16); /* reset */ |
Magnus Damm | 51354cc | 2008-10-16 19:51:20 -0300 | [diff] [blame] | 381 | |
| 382 | /* make sure active buffer is canceled */ |
| 383 | spin_lock_irqsave(&pcdev->lock, flags); |
| 384 | if (pcdev->active) { |
| 385 | list_del(&pcdev->active->queue); |
| 386 | pcdev->active->state = VIDEOBUF_ERROR; |
| 387 | wake_up_all(&pcdev->active->done); |
| 388 | pcdev->active = NULL; |
| 389 | } |
| 390 | spin_unlock_irqrestore(&pcdev->lock, flags); |
| 391 | |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 392 | clk_disable(pcdev->clk); |
| 393 | |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 394 | dev_info(&icd->dev, |
| 395 | "SuperH Mobile CEU driver detached from camera %d\n", |
| 396 | icd->devnum); |
| 397 | |
| 398 | pcdev->icd = NULL; |
| 399 | } |
| 400 | |
| 401 | static int sh_mobile_ceu_set_bus_param(struct soc_camera_device *icd, |
| 402 | __u32 pixfmt) |
| 403 | { |
| 404 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
| 405 | struct sh_mobile_ceu_dev *pcdev = ici->priv; |
Kuninori Morimoto | ccab8a2 | 2008-12-18 12:51:21 -0300 | [diff] [blame] | 406 | int ret, buswidth, width, height, cfszr_width, cdwdr_width; |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 407 | unsigned long camera_flags, common_flags, value; |
Magnus Damm | 8be65dc | 2008-12-18 12:38:06 -0300 | [diff] [blame] | 408 | int yuv_mode, yuv_lineskip; |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 409 | |
| 410 | camera_flags = icd->ops->query_bus_param(icd); |
| 411 | common_flags = soc_camera_bus_param_compatible(camera_flags, |
Kuninori Morimoto | c354b40 | 2009-02-23 12:12:58 -0300 | [diff] [blame] | 412 | make_bus_param(pcdev)); |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 413 | if (!common_flags) |
| 414 | return -EINVAL; |
| 415 | |
| 416 | ret = icd->ops->set_bus_param(icd, common_flags); |
| 417 | if (ret < 0) |
| 418 | return ret; |
| 419 | |
| 420 | switch (common_flags & SOCAM_DATAWIDTH_MASK) { |
| 421 | case SOCAM_DATAWIDTH_8: |
| 422 | buswidth = 8; |
| 423 | break; |
| 424 | case SOCAM_DATAWIDTH_16: |
| 425 | buswidth = 16; |
| 426 | break; |
| 427 | default: |
| 428 | return -EINVAL; |
| 429 | } |
| 430 | |
| 431 | ceu_write(pcdev, CRCNTR, 0); |
| 432 | ceu_write(pcdev, CRCMPR, 0); |
| 433 | |
Magnus Damm | 8be65dc | 2008-12-18 12:38:06 -0300 | [diff] [blame] | 434 | value = 0x00000010; /* data fetch by default */ |
| 435 | yuv_mode = yuv_lineskip = 0; |
| 436 | |
| 437 | switch (icd->current_fmt->fourcc) { |
| 438 | case V4L2_PIX_FMT_NV12: |
| 439 | case V4L2_PIX_FMT_NV21: |
| 440 | yuv_lineskip = 1; /* skip for NV12/21, no skip for NV16/61 */ |
Magnus Damm | 9e4a56d | 2008-12-18 12:45:33 -0300 | [diff] [blame] | 441 | /* fall-through */ |
| 442 | case V4L2_PIX_FMT_NV16: |
| 443 | case V4L2_PIX_FMT_NV61: |
Magnus Damm | 8be65dc | 2008-12-18 12:38:06 -0300 | [diff] [blame] | 444 | yuv_mode = 1; |
| 445 | switch (pcdev->camera_fmt->fourcc) { |
| 446 | case V4L2_PIX_FMT_UYVY: |
| 447 | value = 0x00000000; /* Cb0, Y0, Cr0, Y1 */ |
| 448 | break; |
| 449 | case V4L2_PIX_FMT_VYUY: |
| 450 | value = 0x00000100; /* Cr0, Y0, Cb0, Y1 */ |
| 451 | break; |
| 452 | case V4L2_PIX_FMT_YUYV: |
| 453 | value = 0x00000200; /* Y0, Cb0, Y1, Cr0 */ |
| 454 | break; |
| 455 | case V4L2_PIX_FMT_YVYU: |
| 456 | value = 0x00000300; /* Y0, Cr0, Y1, Cb0 */ |
| 457 | break; |
| 458 | default: |
| 459 | BUG(); |
| 460 | } |
| 461 | } |
| 462 | |
Magnus Damm | 7a1a816 | 2008-12-18 12:50:30 -0300 | [diff] [blame] | 463 | if (icd->current_fmt->fourcc == V4L2_PIX_FMT_NV21 || |
| 464 | icd->current_fmt->fourcc == V4L2_PIX_FMT_NV61) |
Magnus Damm | 9e4a56d | 2008-12-18 12:45:33 -0300 | [diff] [blame] | 465 | value ^= 0x00000100; /* swap U, V to change from NV1x->NVx1 */ |
Magnus Damm | 8be65dc | 2008-12-18 12:38:06 -0300 | [diff] [blame] | 466 | |
Magnus Damm | 7a1a816 | 2008-12-18 12:50:30 -0300 | [diff] [blame] | 467 | value |= common_flags & SOCAM_VSYNC_ACTIVE_LOW ? 1 << 1 : 0; |
| 468 | value |= common_flags & SOCAM_HSYNC_ACTIVE_LOW ? 1 << 0 : 0; |
| 469 | value |= buswidth == 16 ? 1 << 12 : 0; |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 470 | ceu_write(pcdev, CAMCR, value); |
| 471 | |
| 472 | ceu_write(pcdev, CAPCR, 0x00300000); |
Guennadi Liakhovetski | e802967 | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 473 | ceu_write(pcdev, CAIFR, pcdev->is_interlaced ? 0x101 : 0); |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 474 | |
| 475 | mdelay(1); |
| 476 | |
Magnus Damm | 8be65dc | 2008-12-18 12:38:06 -0300 | [diff] [blame] | 477 | if (yuv_mode) { |
| 478 | width = icd->width * 2; |
Magnus Damm | 7a1a816 | 2008-12-18 12:50:30 -0300 | [diff] [blame] | 479 | width = buswidth == 16 ? width / 2 : width; |
Magnus Damm | 8be65dc | 2008-12-18 12:38:06 -0300 | [diff] [blame] | 480 | cfszr_width = cdwdr_width = icd->width; |
| 481 | } else { |
| 482 | width = icd->width * ((icd->current_fmt->depth + 7) >> 3); |
Magnus Damm | 7a1a816 | 2008-12-18 12:50:30 -0300 | [diff] [blame] | 483 | width = buswidth == 16 ? width / 2 : width; |
| 484 | cfszr_width = buswidth == 8 ? width / 2 : width; |
| 485 | cdwdr_width = buswidth == 16 ? width * 2 : width; |
Magnus Damm | 8be65dc | 2008-12-18 12:38:06 -0300 | [diff] [blame] | 486 | } |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 487 | |
Kuninori Morimoto | ccab8a2 | 2008-12-18 12:51:21 -0300 | [diff] [blame] | 488 | height = icd->height; |
Guennadi Liakhovetski | e802967 | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 489 | if (pcdev->is_interlaced) { |
Kuninori Morimoto | ccab8a2 | 2008-12-18 12:51:21 -0300 | [diff] [blame] | 490 | height /= 2; |
| 491 | cdwdr_width *= 2; |
| 492 | } |
| 493 | |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 494 | ceu_write(pcdev, CAMOR, 0); |
Kuninori Morimoto | ccab8a2 | 2008-12-18 12:51:21 -0300 | [diff] [blame] | 495 | ceu_write(pcdev, CAPWR, (height << 16) | width); |
Magnus Damm | 8be65dc | 2008-12-18 12:38:06 -0300 | [diff] [blame] | 496 | ceu_write(pcdev, CFLCR, 0); /* no scaling */ |
Kuninori Morimoto | ccab8a2 | 2008-12-18 12:51:21 -0300 | [diff] [blame] | 497 | ceu_write(pcdev, CFSZR, (height << 16) | cfszr_width); |
Magnus Damm | 8be65dc | 2008-12-18 12:38:06 -0300 | [diff] [blame] | 498 | ceu_write(pcdev, CLFCR, 0); /* no lowpass filter */ |
Magnus Damm | dd54203 | 2008-10-16 19:50:22 -0300 | [diff] [blame] | 499 | |
| 500 | /* A few words about byte order (observed in Big Endian mode) |
| 501 | * |
| 502 | * In data fetch mode bytes are received in chunks of 8 bytes. |
| 503 | * D0, D1, D2, D3, D4, D5, D6, D7 (D0 received first) |
| 504 | * |
| 505 | * The data is however by default written to memory in reverse order: |
| 506 | * D7, D6, D5, D4, D3, D2, D1, D0 (D7 written to lowest byte) |
| 507 | * |
| 508 | * The lowest three bits of CDOCR allows us to do swapping, |
Magnus Damm | 2c0a072 | 2008-10-16 19:50:56 -0300 | [diff] [blame] | 509 | * using 7 we swap the data bytes to match the incoming order: |
| 510 | * D0, D1, D2, D3, D4, D5, D6, D7 |
Magnus Damm | dd54203 | 2008-10-16 19:50:22 -0300 | [diff] [blame] | 511 | */ |
Magnus Damm | 8be65dc | 2008-12-18 12:38:06 -0300 | [diff] [blame] | 512 | value = 0x00000017; |
| 513 | if (yuv_lineskip) |
| 514 | value &= ~0x00000010; /* convert 4:2:2 -> 4:2:0 */ |
| 515 | |
| 516 | ceu_write(pcdev, CDOCR, value); |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 517 | |
| 518 | ceu_write(pcdev, CDWDR, cdwdr_width); |
| 519 | ceu_write(pcdev, CFWCR, 0); /* keep "datafetch firewall" disabled */ |
| 520 | |
| 521 | /* not in bundle mode: skip CBDSR, CDAYR2, CDACR2, CDBYR2, CDBCR2 */ |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 522 | return 0; |
| 523 | } |
| 524 | |
Magnus Damm | 9414de3 | 2008-12-18 11:49:06 -0300 | [diff] [blame] | 525 | static int sh_mobile_ceu_try_bus_param(struct soc_camera_device *icd) |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 526 | { |
| 527 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
| 528 | struct sh_mobile_ceu_dev *pcdev = ici->priv; |
| 529 | unsigned long camera_flags, common_flags; |
| 530 | |
| 531 | camera_flags = icd->ops->query_bus_param(icd); |
| 532 | common_flags = soc_camera_bus_param_compatible(camera_flags, |
Kuninori Morimoto | c354b40 | 2009-02-23 12:12:58 -0300 | [diff] [blame] | 533 | make_bus_param(pcdev)); |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 534 | if (!common_flags) |
| 535 | return -EINVAL; |
| 536 | |
| 537 | return 0; |
| 538 | } |
| 539 | |
Magnus Damm | 8be65dc | 2008-12-18 12:38:06 -0300 | [diff] [blame] | 540 | static const struct soc_camera_data_format sh_mobile_ceu_formats[] = { |
| 541 | { |
| 542 | .name = "NV12", |
| 543 | .depth = 12, |
| 544 | .fourcc = V4L2_PIX_FMT_NV12, |
| 545 | .colorspace = V4L2_COLORSPACE_JPEG, |
| 546 | }, |
| 547 | { |
| 548 | .name = "NV21", |
| 549 | .depth = 12, |
| 550 | .fourcc = V4L2_PIX_FMT_NV21, |
| 551 | .colorspace = V4L2_COLORSPACE_JPEG, |
| 552 | }, |
Magnus Damm | 9e4a56d | 2008-12-18 12:45:33 -0300 | [diff] [blame] | 553 | { |
| 554 | .name = "NV16", |
| 555 | .depth = 16, |
| 556 | .fourcc = V4L2_PIX_FMT_NV16, |
| 557 | .colorspace = V4L2_COLORSPACE_JPEG, |
| 558 | }, |
| 559 | { |
| 560 | .name = "NV61", |
| 561 | .depth = 16, |
| 562 | .fourcc = V4L2_PIX_FMT_NV61, |
| 563 | .colorspace = V4L2_COLORSPACE_JPEG, |
| 564 | }, |
Magnus Damm | 8be65dc | 2008-12-18 12:38:06 -0300 | [diff] [blame] | 565 | }; |
| 566 | |
Magnus Damm | 9414de3 | 2008-12-18 11:49:06 -0300 | [diff] [blame] | 567 | static int sh_mobile_ceu_get_formats(struct soc_camera_device *icd, int idx, |
| 568 | struct soc_camera_format_xlate *xlate) |
| 569 | { |
| 570 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
Magnus Damm | 8be65dc | 2008-12-18 12:38:06 -0300 | [diff] [blame] | 571 | int ret, k, n; |
Magnus Damm | 9414de3 | 2008-12-18 11:49:06 -0300 | [diff] [blame] | 572 | int formats = 0; |
| 573 | |
| 574 | ret = sh_mobile_ceu_try_bus_param(icd); |
| 575 | if (ret < 0) |
| 576 | return 0; |
| 577 | |
Guennadi Liakhovetski | c8329ac | 2009-02-23 12:12:58 -0300 | [diff] [blame] | 578 | /* Beginning of a pass */ |
| 579 | if (!idx) |
| 580 | icd->host_priv = NULL; |
| 581 | |
Magnus Damm | 9414de3 | 2008-12-18 11:49:06 -0300 | [diff] [blame] | 582 | switch (icd->formats[idx].fourcc) { |
Magnus Damm | 8be65dc | 2008-12-18 12:38:06 -0300 | [diff] [blame] | 583 | case V4L2_PIX_FMT_UYVY: |
| 584 | case V4L2_PIX_FMT_VYUY: |
| 585 | case V4L2_PIX_FMT_YUYV: |
| 586 | case V4L2_PIX_FMT_YVYU: |
Guennadi Liakhovetski | c8329ac | 2009-02-23 12:12:58 -0300 | [diff] [blame] | 587 | if (icd->host_priv) |
| 588 | goto add_single_format; |
| 589 | |
| 590 | /* |
| 591 | * Our case is simple so far: for any of the above four camera |
| 592 | * formats we add all our four synthesized NV* formats, so, |
| 593 | * just marking the device with a single flag suffices. If |
| 594 | * the format generation rules are more complex, you would have |
| 595 | * to actually hang your already added / counted formats onto |
| 596 | * the host_priv pointer and check whether the format you're |
| 597 | * going to add now is already there. |
| 598 | */ |
| 599 | icd->host_priv = (void *)sh_mobile_ceu_formats; |
| 600 | |
Magnus Damm | 8be65dc | 2008-12-18 12:38:06 -0300 | [diff] [blame] | 601 | n = ARRAY_SIZE(sh_mobile_ceu_formats); |
| 602 | formats += n; |
| 603 | for (k = 0; xlate && k < n; k++) { |
| 604 | xlate->host_fmt = &sh_mobile_ceu_formats[k]; |
| 605 | xlate->cam_fmt = icd->formats + idx; |
| 606 | xlate->buswidth = icd->formats[idx].depth; |
| 607 | xlate++; |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 608 | dev_dbg(ici->v4l2_dev.dev, "Providing format %s using %s\n", |
Magnus Damm | 8be65dc | 2008-12-18 12:38:06 -0300 | [diff] [blame] | 609 | sh_mobile_ceu_formats[k].name, |
| 610 | icd->formats[idx].name); |
| 611 | } |
Magnus Damm | 9414de3 | 2008-12-18 11:49:06 -0300 | [diff] [blame] | 612 | default: |
Guennadi Liakhovetski | c8329ac | 2009-02-23 12:12:58 -0300 | [diff] [blame] | 613 | add_single_format: |
Magnus Damm | 9414de3 | 2008-12-18 11:49:06 -0300 | [diff] [blame] | 614 | /* Generic pass-through */ |
| 615 | formats++; |
| 616 | if (xlate) { |
| 617 | xlate->host_fmt = icd->formats + idx; |
| 618 | xlate->cam_fmt = icd->formats + idx; |
| 619 | xlate->buswidth = icd->formats[idx].depth; |
| 620 | xlate++; |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 621 | dev_dbg(ici->v4l2_dev.dev, |
Magnus Damm | 9414de3 | 2008-12-18 11:49:06 -0300 | [diff] [blame] | 622 | "Providing format %s in pass-through mode\n", |
| 623 | icd->formats[idx].name); |
| 624 | } |
| 625 | } |
| 626 | |
| 627 | return formats; |
| 628 | } |
| 629 | |
Guennadi Liakhovetski | 09e231b | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 630 | static int sh_mobile_ceu_set_crop(struct soc_camera_device *icd, |
| 631 | struct v4l2_rect *rect) |
| 632 | { |
| 633 | return icd->ops->set_crop(icd, rect); |
| 634 | } |
| 635 | |
Guennadi Liakhovetski | d8fac21 | 2008-12-01 09:45:21 -0300 | [diff] [blame] | 636 | static int sh_mobile_ceu_set_fmt(struct soc_camera_device *icd, |
Guennadi Liakhovetski | 09e231b | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 637 | struct v4l2_format *f) |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 638 | { |
Magnus Damm | 9414de3 | 2008-12-18 11:49:06 -0300 | [diff] [blame] | 639 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
Magnus Damm | 8be65dc | 2008-12-18 12:38:06 -0300 | [diff] [blame] | 640 | struct sh_mobile_ceu_dev *pcdev = ici->priv; |
Guennadi Liakhovetski | 09e231b | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 641 | __u32 pixfmt = f->fmt.pix.pixelformat; |
Magnus Damm | 9414de3 | 2008-12-18 11:49:06 -0300 | [diff] [blame] | 642 | const struct soc_camera_format_xlate *xlate; |
Guennadi Liakhovetski | 25c4d74 | 2008-12-01 09:44:59 -0300 | [diff] [blame] | 643 | int ret; |
| 644 | |
Magnus Damm | 9414de3 | 2008-12-18 11:49:06 -0300 | [diff] [blame] | 645 | xlate = soc_camera_xlate_by_fourcc(icd, pixfmt); |
| 646 | if (!xlate) { |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 647 | dev_warn(ici->v4l2_dev.dev, "Format %x not found\n", pixfmt); |
Magnus Damm | 9414de3 | 2008-12-18 11:49:06 -0300 | [diff] [blame] | 648 | return -EINVAL; |
Guennadi Liakhovetski | 25c4d74 | 2008-12-01 09:44:59 -0300 | [diff] [blame] | 649 | } |
| 650 | |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 651 | f->fmt.pix.pixelformat = xlate->cam_fmt->fourcc; |
| 652 | ret = v4l2_device_call_until_err(&ici->v4l2_dev, (__u32)icd, video, s_fmt, f); |
| 653 | f->fmt.pix.pixelformat = pixfmt; |
Guennadi Liakhovetski | 0ad675e | 2009-02-23 12:11:25 -0300 | [diff] [blame] | 654 | if (!ret) { |
Magnus Damm | 9414de3 | 2008-12-18 11:49:06 -0300 | [diff] [blame] | 655 | icd->buswidth = xlate->buswidth; |
| 656 | icd->current_fmt = xlate->host_fmt; |
Magnus Damm | 8be65dc | 2008-12-18 12:38:06 -0300 | [diff] [blame] | 657 | pcdev->camera_fmt = xlate->cam_fmt; |
Magnus Damm | 9414de3 | 2008-12-18 11:49:06 -0300 | [diff] [blame] | 658 | } |
Guennadi Liakhovetski | 25c4d74 | 2008-12-01 09:44:59 -0300 | [diff] [blame] | 659 | |
| 660 | return ret; |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 661 | } |
| 662 | |
Guennadi Liakhovetski | d8fac21 | 2008-12-01 09:45:21 -0300 | [diff] [blame] | 663 | static int sh_mobile_ceu_try_fmt(struct soc_camera_device *icd, |
| 664 | struct v4l2_format *f) |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 665 | { |
Magnus Damm | 9414de3 | 2008-12-18 11:49:06 -0300 | [diff] [blame] | 666 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
Kuninori Morimoto | ccab8a2 | 2008-12-18 12:51:21 -0300 | [diff] [blame] | 667 | struct sh_mobile_ceu_dev *pcdev = ici->priv; |
Magnus Damm | 9414de3 | 2008-12-18 11:49:06 -0300 | [diff] [blame] | 668 | const struct soc_camera_format_xlate *xlate; |
| 669 | __u32 pixfmt = f->fmt.pix.pixelformat; |
Kuninori Morimoto | ccab8a2 | 2008-12-18 12:51:21 -0300 | [diff] [blame] | 670 | int ret; |
Guennadi Liakhovetski | a2c8c68 | 2008-12-01 09:44:53 -0300 | [diff] [blame] | 671 | |
Magnus Damm | 9414de3 | 2008-12-18 11:49:06 -0300 | [diff] [blame] | 672 | xlate = soc_camera_xlate_by_fourcc(icd, pixfmt); |
| 673 | if (!xlate) { |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 674 | dev_warn(ici->v4l2_dev.dev, "Format %x not found\n", pixfmt); |
Guennadi Liakhovetski | 25c4d74 | 2008-12-01 09:44:59 -0300 | [diff] [blame] | 675 | return -EINVAL; |
Magnus Damm | 9414de3 | 2008-12-18 11:49:06 -0300 | [diff] [blame] | 676 | } |
Guennadi Liakhovetski | 25c4d74 | 2008-12-01 09:44:59 -0300 | [diff] [blame] | 677 | |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 678 | /* FIXME: calculate using depth and bus width */ |
| 679 | |
Trent Piepho | bc44fc0 | 2009-05-30 21:45:46 -0300 | [diff] [blame] | 680 | v4l_bound_align_image(&f->fmt.pix.width, 2, 2560, 1, |
| 681 | &f->fmt.pix.height, 4, 1920, 2, 0); |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 682 | |
Guennadi Liakhovetski | 25c4d74 | 2008-12-01 09:44:59 -0300 | [diff] [blame] | 683 | f->fmt.pix.bytesperline = f->fmt.pix.width * |
Magnus Damm | 9414de3 | 2008-12-18 11:49:06 -0300 | [diff] [blame] | 684 | DIV_ROUND_UP(xlate->host_fmt->depth, 8); |
Guennadi Liakhovetski | 25c4d74 | 2008-12-01 09:44:59 -0300 | [diff] [blame] | 685 | f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline; |
| 686 | |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 687 | f->fmt.pix.pixelformat = xlate->cam_fmt->fourcc; |
| 688 | |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 689 | /* limit to sensor capabilities */ |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 690 | ret = v4l2_device_call_until_err(&ici->v4l2_dev, (__u32)icd, video, try_fmt, f); |
| 691 | f->fmt.pix.pixelformat = pixfmt; |
Kuninori Morimoto | ccab8a2 | 2008-12-18 12:51:21 -0300 | [diff] [blame] | 692 | if (ret < 0) |
| 693 | return ret; |
| 694 | |
| 695 | switch (f->fmt.pix.field) { |
| 696 | case V4L2_FIELD_INTERLACED: |
Guennadi Liakhovetski | e802967 | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 697 | pcdev->is_interlaced = 1; |
Kuninori Morimoto | ccab8a2 | 2008-12-18 12:51:21 -0300 | [diff] [blame] | 698 | break; |
| 699 | case V4L2_FIELD_ANY: |
| 700 | f->fmt.pix.field = V4L2_FIELD_NONE; |
| 701 | /* fall-through */ |
| 702 | case V4L2_FIELD_NONE: |
Guennadi Liakhovetski | e802967 | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 703 | pcdev->is_interlaced = 0; |
Kuninori Morimoto | ccab8a2 | 2008-12-18 12:51:21 -0300 | [diff] [blame] | 704 | break; |
| 705 | default: |
| 706 | ret = -EINVAL; |
| 707 | break; |
| 708 | } |
| 709 | |
| 710 | return ret; |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 711 | } |
| 712 | |
| 713 | static int sh_mobile_ceu_reqbufs(struct soc_camera_file *icf, |
| 714 | struct v4l2_requestbuffers *p) |
| 715 | { |
| 716 | int i; |
| 717 | |
| 718 | /* This is for locking debugging only. I removed spinlocks and now I |
| 719 | * check whether .prepare is ever called on a linked buffer, or whether |
| 720 | * a dma IRQ can occur for an in-work or unlinked buffer. Until now |
| 721 | * it hadn't triggered */ |
| 722 | for (i = 0; i < p->count; i++) { |
| 723 | struct sh_mobile_ceu_buffer *buf; |
| 724 | |
| 725 | buf = container_of(icf->vb_vidq.bufs[i], |
| 726 | struct sh_mobile_ceu_buffer, vb); |
| 727 | INIT_LIST_HEAD(&buf->vb.queue); |
| 728 | } |
| 729 | |
| 730 | return 0; |
| 731 | } |
| 732 | |
| 733 | static unsigned int sh_mobile_ceu_poll(struct file *file, poll_table *pt) |
| 734 | { |
| 735 | struct soc_camera_file *icf = file->private_data; |
| 736 | struct sh_mobile_ceu_buffer *buf; |
| 737 | |
| 738 | buf = list_entry(icf->vb_vidq.stream.next, |
| 739 | struct sh_mobile_ceu_buffer, vb.stream); |
| 740 | |
| 741 | poll_wait(file, &buf->vb.done, pt); |
| 742 | |
| 743 | if (buf->vb.state == VIDEOBUF_DONE || |
| 744 | buf->vb.state == VIDEOBUF_ERROR) |
| 745 | return POLLIN|POLLRDNORM; |
| 746 | |
| 747 | return 0; |
| 748 | } |
| 749 | |
| 750 | static int sh_mobile_ceu_querycap(struct soc_camera_host *ici, |
| 751 | struct v4l2_capability *cap) |
| 752 | { |
| 753 | strlcpy(cap->card, "SuperH_Mobile_CEU", sizeof(cap->card)); |
| 754 | cap->version = KERNEL_VERSION(0, 0, 5); |
| 755 | cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING; |
| 756 | return 0; |
| 757 | } |
| 758 | |
| 759 | static void sh_mobile_ceu_init_videobuf(struct videobuf_queue *q, |
| 760 | struct soc_camera_device *icd) |
| 761 | { |
| 762 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
| 763 | struct sh_mobile_ceu_dev *pcdev = ici->priv; |
| 764 | |
| 765 | videobuf_queue_dma_contig_init(q, |
| 766 | &sh_mobile_ceu_videobuf_ops, |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 767 | ici->v4l2_dev.dev, &pcdev->lock, |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 768 | V4L2_BUF_TYPE_VIDEO_CAPTURE, |
Guennadi Liakhovetski | e802967 | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 769 | pcdev->is_interlaced ? |
| 770 | V4L2_FIELD_INTERLACED : V4L2_FIELD_NONE, |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 771 | sizeof(struct sh_mobile_ceu_buffer), |
| 772 | icd); |
| 773 | } |
| 774 | |
| 775 | static struct soc_camera_host_ops sh_mobile_ceu_host_ops = { |
| 776 | .owner = THIS_MODULE, |
| 777 | .add = sh_mobile_ceu_add_device, |
| 778 | .remove = sh_mobile_ceu_remove_device, |
Magnus Damm | 9414de3 | 2008-12-18 11:49:06 -0300 | [diff] [blame] | 779 | .get_formats = sh_mobile_ceu_get_formats, |
Guennadi Liakhovetski | 09e231b | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 780 | .set_crop = sh_mobile_ceu_set_crop, |
Guennadi Liakhovetski | d8fac21 | 2008-12-01 09:45:21 -0300 | [diff] [blame] | 781 | .set_fmt = sh_mobile_ceu_set_fmt, |
| 782 | .try_fmt = sh_mobile_ceu_try_fmt, |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 783 | .reqbufs = sh_mobile_ceu_reqbufs, |
| 784 | .poll = sh_mobile_ceu_poll, |
| 785 | .querycap = sh_mobile_ceu_querycap, |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 786 | .set_bus_param = sh_mobile_ceu_set_bus_param, |
| 787 | .init_videobuf = sh_mobile_ceu_init_videobuf, |
| 788 | }; |
| 789 | |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 790 | static int __devinit sh_mobile_ceu_probe(struct platform_device *pdev) |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 791 | { |
| 792 | struct sh_mobile_ceu_dev *pcdev; |
| 793 | struct resource *res; |
| 794 | void __iomem *base; |
| 795 | unsigned int irq; |
| 796 | int err = 0; |
| 797 | |
| 798 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 799 | irq = platform_get_irq(pdev, 0); |
| 800 | if (!res || !irq) { |
| 801 | dev_err(&pdev->dev, "Not enough CEU platform resources.\n"); |
| 802 | err = -ENODEV; |
| 803 | goto exit; |
| 804 | } |
| 805 | |
| 806 | pcdev = kzalloc(sizeof(*pcdev), GFP_KERNEL); |
| 807 | if (!pcdev) { |
| 808 | dev_err(&pdev->dev, "Could not allocate pcdev\n"); |
| 809 | err = -ENOMEM; |
| 810 | goto exit; |
| 811 | } |
| 812 | |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 813 | INIT_LIST_HEAD(&pcdev->capture); |
| 814 | spin_lock_init(&pcdev->lock); |
| 815 | |
| 816 | pcdev->pdata = pdev->dev.platform_data; |
| 817 | if (!pcdev->pdata) { |
| 818 | err = -EINVAL; |
| 819 | dev_err(&pdev->dev, "CEU platform data not set.\n"); |
| 820 | goto exit_kfree; |
| 821 | } |
| 822 | |
Guennadi Liakhovetski | eb6c855 | 2009-04-24 12:55:18 -0300 | [diff] [blame] | 823 | base = ioremap_nocache(res->start, resource_size(res)); |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 824 | if (!base) { |
| 825 | err = -ENXIO; |
| 826 | dev_err(&pdev->dev, "Unable to ioremap CEU registers.\n"); |
| 827 | goto exit_kfree; |
| 828 | } |
| 829 | |
| 830 | pcdev->irq = irq; |
| 831 | pcdev->base = base; |
| 832 | pcdev->video_limit = 0; /* only enabled if second resource exists */ |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 833 | |
| 834 | res = platform_get_resource(pdev, IORESOURCE_MEM, 1); |
| 835 | if (res) { |
| 836 | err = dma_declare_coherent_memory(&pdev->dev, res->start, |
| 837 | res->start, |
Guennadi Liakhovetski | eb6c855 | 2009-04-24 12:55:18 -0300 | [diff] [blame] | 838 | resource_size(res), |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 839 | DMA_MEMORY_MAP | |
| 840 | DMA_MEMORY_EXCLUSIVE); |
| 841 | if (!err) { |
| 842 | dev_err(&pdev->dev, "Unable to declare CEU memory.\n"); |
| 843 | err = -ENXIO; |
| 844 | goto exit_iounmap; |
| 845 | } |
| 846 | |
Guennadi Liakhovetski | eb6c855 | 2009-04-24 12:55:18 -0300 | [diff] [blame] | 847 | pcdev->video_limit = resource_size(res); |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 848 | } |
| 849 | |
| 850 | /* request irq */ |
| 851 | err = request_irq(pcdev->irq, sh_mobile_ceu_irq, IRQF_DISABLED, |
Kay Sievers | af128a1 | 2008-10-30 00:51:46 -0300 | [diff] [blame] | 852 | dev_name(&pdev->dev), pcdev); |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 853 | if (err) { |
| 854 | dev_err(&pdev->dev, "Unable to register CEU interrupt.\n"); |
| 855 | goto exit_release_mem; |
| 856 | } |
| 857 | |
Magnus Damm | 6d1386c | 2009-08-14 10:49:17 +0000 | [diff] [blame] | 858 | pm_suspend_ignore_children(&pdev->dev, true); |
| 859 | pm_runtime_enable(&pdev->dev); |
| 860 | pm_runtime_resume(&pdev->dev); |
Magnus Damm | a42b6dd | 2008-10-31 20:21:44 +0900 | [diff] [blame] | 861 | |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 862 | pcdev->ici.priv = pcdev; |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 863 | pcdev->ici.v4l2_dev.dev = &pdev->dev; |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 864 | pcdev->ici.nr = pdev->id; |
Kay Sievers | af128a1 | 2008-10-30 00:51:46 -0300 | [diff] [blame] | 865 | pcdev->ici.drv_name = dev_name(&pdev->dev); |
| 866 | pcdev->ici.ops = &sh_mobile_ceu_host_ops; |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 867 | |
| 868 | err = soc_camera_host_register(&pcdev->ici); |
| 869 | if (err) |
Magnus Damm | 6d1386c | 2009-08-14 10:49:17 +0000 | [diff] [blame] | 870 | goto exit_free_irq; |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 871 | |
| 872 | return 0; |
| 873 | |
| 874 | exit_free_irq: |
| 875 | free_irq(pcdev->irq, pcdev); |
| 876 | exit_release_mem: |
| 877 | if (platform_get_resource(pdev, IORESOURCE_MEM, 1)) |
| 878 | dma_release_declared_memory(&pdev->dev); |
| 879 | exit_iounmap: |
| 880 | iounmap(base); |
| 881 | exit_kfree: |
| 882 | kfree(pcdev); |
| 883 | exit: |
| 884 | return err; |
| 885 | } |
| 886 | |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 887 | static int __devexit sh_mobile_ceu_remove(struct platform_device *pdev) |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 888 | { |
Guennadi Liakhovetski | eff505f | 2009-04-24 12:55:48 -0300 | [diff] [blame] | 889 | struct soc_camera_host *soc_host = to_soc_camera_host(&pdev->dev); |
| 890 | struct sh_mobile_ceu_dev *pcdev = container_of(soc_host, |
| 891 | struct sh_mobile_ceu_dev, ici); |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 892 | |
Guennadi Liakhovetski | eff505f | 2009-04-24 12:55:48 -0300 | [diff] [blame] | 893 | soc_camera_host_unregister(soc_host); |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 894 | free_irq(pcdev->irq, pcdev); |
| 895 | if (platform_get_resource(pdev, IORESOURCE_MEM, 1)) |
| 896 | dma_release_declared_memory(&pdev->dev); |
| 897 | iounmap(pcdev->base); |
| 898 | kfree(pcdev); |
| 899 | return 0; |
| 900 | } |
| 901 | |
Magnus Damm | 6d1386c | 2009-08-14 10:49:17 +0000 | [diff] [blame] | 902 | static int sh_mobile_ceu_runtime_nop(struct device *dev) |
| 903 | { |
| 904 | /* Runtime PM callback shared between ->runtime_suspend() |
| 905 | * and ->runtime_resume(). Simply returns success. |
| 906 | * |
| 907 | * This driver re-initializes all registers after |
| 908 | * pm_runtime_get_sync() anyway so there is no need |
| 909 | * to save and restore registers here. |
| 910 | */ |
| 911 | return 0; |
| 912 | } |
| 913 | |
| 914 | static struct dev_pm_ops sh_mobile_ceu_dev_pm_ops = { |
| 915 | .runtime_suspend = sh_mobile_ceu_runtime_nop, |
| 916 | .runtime_resume = sh_mobile_ceu_runtime_nop, |
| 917 | }; |
| 918 | |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 919 | static struct platform_driver sh_mobile_ceu_driver = { |
| 920 | .driver = { |
| 921 | .name = "sh_mobile_ceu", |
Magnus Damm | 6d1386c | 2009-08-14 10:49:17 +0000 | [diff] [blame] | 922 | .pm = &sh_mobile_ceu_dev_pm_ops, |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 923 | }, |
| 924 | .probe = sh_mobile_ceu_probe, |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 925 | .remove = __exit_p(sh_mobile_ceu_remove), |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 926 | }; |
| 927 | |
| 928 | static int __init sh_mobile_ceu_init(void) |
| 929 | { |
| 930 | return platform_driver_register(&sh_mobile_ceu_driver); |
| 931 | } |
| 932 | |
| 933 | static void __exit sh_mobile_ceu_exit(void) |
| 934 | { |
Paul Mundt | 01c1e4c | 2008-08-01 19:48:51 -0300 | [diff] [blame] | 935 | platform_driver_unregister(&sh_mobile_ceu_driver); |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 936 | } |
| 937 | |
| 938 | module_init(sh_mobile_ceu_init); |
| 939 | module_exit(sh_mobile_ceu_exit); |
| 940 | |
| 941 | MODULE_DESCRIPTION("SuperH Mobile CEU driver"); |
| 942 | MODULE_AUTHOR("Magnus Damm"); |
| 943 | MODULE_LICENSE("GPL"); |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 944 | MODULE_ALIAS("platform:sh_mobile_ceu"); |