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