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; |
| 95 | |
| 96 | struct sh_mobile_ceu_info *pdata; |
Magnus Damm | 8be65dc | 2008-12-18 12:38:06 -0300 | [diff] [blame] | 97 | |
Guennadi Liakhovetski | 961801b | 2009-08-25 11:46:54 -0300 | [diff] [blame^] | 98 | u32 cflcr; |
| 99 | |
Guennadi Liakhovetski | 904078f | 2009-08-25 11:46:52 -0300 | [diff] [blame] | 100 | unsigned int is_interlaced:1; |
| 101 | unsigned int image_mode:1; |
| 102 | unsigned int is_16bit:1; |
| 103 | }; |
| 104 | |
| 105 | struct sh_mobile_ceu_cam { |
| 106 | struct v4l2_rect camera_rect; |
Guennadi Liakhovetski | 961801b | 2009-08-25 11:46:54 -0300 | [diff] [blame^] | 107 | struct v4l2_rect camera_max; |
Guennadi Liakhovetski | 904078f | 2009-08-25 11:46:52 -0300 | [diff] [blame] | 108 | const struct soc_camera_data_format *extra_fmt; |
Magnus Damm | 8be65dc | 2008-12-18 12:38:06 -0300 | [diff] [blame] | 109 | const struct soc_camera_data_format *camera_fmt; |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 110 | }; |
| 111 | |
Kuninori Morimoto | c354b40 | 2009-02-23 12:12:58 -0300 | [diff] [blame] | 112 | static unsigned long make_bus_param(struct sh_mobile_ceu_dev *pcdev) |
| 113 | { |
| 114 | unsigned long flags; |
| 115 | |
| 116 | flags = SOCAM_MASTER | |
| 117 | SOCAM_PCLK_SAMPLE_RISING | |
| 118 | SOCAM_HSYNC_ACTIVE_HIGH | |
| 119 | SOCAM_HSYNC_ACTIVE_LOW | |
| 120 | SOCAM_VSYNC_ACTIVE_HIGH | |
| 121 | SOCAM_VSYNC_ACTIVE_LOW | |
| 122 | SOCAM_DATA_ACTIVE_HIGH; |
| 123 | |
| 124 | if (pcdev->pdata->flags & SH_CEU_FLAG_USE_8BIT_BUS) |
| 125 | flags |= SOCAM_DATAWIDTH_8; |
| 126 | |
| 127 | if (pcdev->pdata->flags & SH_CEU_FLAG_USE_16BIT_BUS) |
| 128 | flags |= SOCAM_DATAWIDTH_16; |
| 129 | |
| 130 | if (flags & SOCAM_DATAWIDTH_MASK) |
| 131 | return flags; |
| 132 | |
| 133 | return 0; |
| 134 | } |
| 135 | |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 136 | static void ceu_write(struct sh_mobile_ceu_dev *priv, |
Magnus Damm | 7a1a816 | 2008-12-18 12:50:30 -0300 | [diff] [blame] | 137 | unsigned long reg_offs, u32 data) |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 138 | { |
| 139 | iowrite32(data, priv->base + reg_offs); |
| 140 | } |
| 141 | |
Magnus Damm | 7a1a816 | 2008-12-18 12:50:30 -0300 | [diff] [blame] | 142 | 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] | 143 | { |
| 144 | return ioread32(priv->base + reg_offs); |
| 145 | } |
| 146 | |
| 147 | /* |
| 148 | * Videobuf operations |
| 149 | */ |
| 150 | static int sh_mobile_ceu_videobuf_setup(struct videobuf_queue *vq, |
| 151 | unsigned int *count, |
| 152 | unsigned int *size) |
| 153 | { |
| 154 | struct soc_camera_device *icd = vq->priv_data; |
| 155 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
| 156 | struct sh_mobile_ceu_dev *pcdev = ici->priv; |
| 157 | int bytes_per_pixel = (icd->current_fmt->depth + 7) >> 3; |
| 158 | |
Guennadi Liakhovetski | a0705b0 | 2009-08-25 11:46:17 -0300 | [diff] [blame] | 159 | *size = PAGE_ALIGN(icd->rect_current.width * icd->rect_current.height * |
| 160 | bytes_per_pixel); |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 161 | |
| 162 | if (0 == *count) |
| 163 | *count = 2; |
| 164 | |
| 165 | if (pcdev->video_limit) { |
| 166 | while (*size * *count > pcdev->video_limit) |
| 167 | (*count)--; |
| 168 | } |
| 169 | |
| 170 | dev_dbg(&icd->dev, "count=%d, size=%d\n", *count, *size); |
| 171 | |
| 172 | return 0; |
| 173 | } |
| 174 | |
| 175 | static void free_buffer(struct videobuf_queue *vq, |
| 176 | struct sh_mobile_ceu_buffer *buf) |
| 177 | { |
| 178 | struct soc_camera_device *icd = vq->priv_data; |
| 179 | |
Hans Verkuil | 86751b0 | 2008-07-18 01:35:14 -0300 | [diff] [blame] | 180 | 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] | 181 | &buf->vb, buf->vb.baddr, buf->vb.bsize); |
| 182 | |
| 183 | if (in_interrupt()) |
| 184 | BUG(); |
| 185 | |
Magnus Damm | 97215cb | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 186 | videobuf_waiton(&buf->vb, 0, 0); |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 187 | videobuf_dma_contig_free(vq, &buf->vb); |
| 188 | dev_dbg(&icd->dev, "%s freed\n", __func__); |
| 189 | buf->vb.state = VIDEOBUF_NEEDS_INIT; |
| 190 | } |
| 191 | |
Magnus Damm | 7a1a816 | 2008-12-18 12:50:30 -0300 | [diff] [blame] | 192 | #define CEU_CETCR_MAGIC 0x0317f313 /* acknowledge magical interrupt sources */ |
| 193 | #define CEU_CETCR_IGRW (1 << 4) /* prohibited register access interrupt bit */ |
| 194 | #define CEU_CEIER_CPEIE (1 << 0) /* one-frame capture end interrupt */ |
| 195 | #define CEU_CAPCR_CTNCP (1 << 16) /* continuous capture mode (if set) */ |
| 196 | |
| 197 | |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 198 | static void sh_mobile_ceu_capture(struct sh_mobile_ceu_dev *pcdev) |
| 199 | { |
Magnus Damm | 8be65dc | 2008-12-18 12:38:06 -0300 | [diff] [blame] | 200 | struct soc_camera_device *icd = pcdev->icd; |
Kuninori Morimoto | ccab8a2 | 2008-12-18 12:51:21 -0300 | [diff] [blame] | 201 | dma_addr_t phys_addr_top, phys_addr_bottom; |
Magnus Damm | 8be65dc | 2008-12-18 12:38:06 -0300 | [diff] [blame] | 202 | |
Magnus Damm | 7a1a816 | 2008-12-18 12:50:30 -0300 | [diff] [blame] | 203 | /* The hardware is _very_ picky about this sequence. Especially |
| 204 | * the CEU_CETCR_MAGIC value. It seems like we need to acknowledge |
| 205 | * several not-so-well documented interrupt sources in CETCR. |
| 206 | */ |
| 207 | ceu_write(pcdev, CEIER, ceu_read(pcdev, CEIER) & ~CEU_CEIER_CPEIE); |
| 208 | ceu_write(pcdev, CETCR, ~ceu_read(pcdev, CETCR) & CEU_CETCR_MAGIC); |
| 209 | ceu_write(pcdev, CEIER, ceu_read(pcdev, CEIER) | CEU_CEIER_CPEIE); |
| 210 | ceu_write(pcdev, CAPCR, ceu_read(pcdev, CAPCR) & ~CEU_CAPCR_CTNCP); |
| 211 | ceu_write(pcdev, CETCR, CEU_CETCR_MAGIC ^ CEU_CETCR_IGRW); |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 212 | |
Magnus Damm | 8be65dc | 2008-12-18 12:38:06 -0300 | [diff] [blame] | 213 | if (!pcdev->active) |
| 214 | return; |
| 215 | |
Kuninori Morimoto | ccab8a2 | 2008-12-18 12:51:21 -0300 | [diff] [blame] | 216 | phys_addr_top = videobuf_to_dma_contig(pcdev->active); |
| 217 | ceu_write(pcdev, CDAYR, phys_addr_top); |
Guennadi Liakhovetski | e802967 | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 218 | if (pcdev->is_interlaced) { |
Guennadi Liakhovetski | a0705b0 | 2009-08-25 11:46:17 -0300 | [diff] [blame] | 219 | phys_addr_bottom = phys_addr_top + icd->rect_current.width; |
Kuninori Morimoto | ccab8a2 | 2008-12-18 12:51:21 -0300 | [diff] [blame] | 220 | ceu_write(pcdev, CDBYR, phys_addr_bottom); |
| 221 | } |
Magnus Damm | 8be65dc | 2008-12-18 12:38:06 -0300 | [diff] [blame] | 222 | |
| 223 | switch (icd->current_fmt->fourcc) { |
| 224 | case V4L2_PIX_FMT_NV12: |
| 225 | case V4L2_PIX_FMT_NV21: |
Magnus Damm | 9e4a56d | 2008-12-18 12:45:33 -0300 | [diff] [blame] | 226 | case V4L2_PIX_FMT_NV16: |
| 227 | case V4L2_PIX_FMT_NV61: |
Guennadi Liakhovetski | a0705b0 | 2009-08-25 11:46:17 -0300 | [diff] [blame] | 228 | phys_addr_top += icd->rect_current.width * |
| 229 | icd->rect_current.height; |
Kuninori Morimoto | ccab8a2 | 2008-12-18 12:51:21 -0300 | [diff] [blame] | 230 | ceu_write(pcdev, CDACR, phys_addr_top); |
Guennadi Liakhovetski | e802967 | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 231 | if (pcdev->is_interlaced) { |
Guennadi Liakhovetski | a0705b0 | 2009-08-25 11:46:17 -0300 | [diff] [blame] | 232 | phys_addr_bottom = phys_addr_top + |
| 233 | icd->rect_current.width; |
Kuninori Morimoto | ccab8a2 | 2008-12-18 12:51:21 -0300 | [diff] [blame] | 234 | ceu_write(pcdev, CDBCR, phys_addr_bottom); |
| 235 | } |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 236 | } |
Magnus Damm | 8be65dc | 2008-12-18 12:38:06 -0300 | [diff] [blame] | 237 | |
| 238 | pcdev->active->state = VIDEOBUF_ACTIVE; |
| 239 | ceu_write(pcdev, CAPSR, 0x1); /* start capture */ |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | static int sh_mobile_ceu_videobuf_prepare(struct videobuf_queue *vq, |
| 243 | struct videobuf_buffer *vb, |
| 244 | enum v4l2_field field) |
| 245 | { |
| 246 | struct soc_camera_device *icd = vq->priv_data; |
| 247 | struct sh_mobile_ceu_buffer *buf; |
| 248 | int ret; |
| 249 | |
| 250 | buf = container_of(vb, struct sh_mobile_ceu_buffer, vb); |
| 251 | |
Hans Verkuil | 86751b0 | 2008-07-18 01:35:14 -0300 | [diff] [blame] | 252 | 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] | 253 | vb, vb->baddr, vb->bsize); |
| 254 | |
| 255 | /* Added list head initialization on alloc */ |
| 256 | WARN_ON(!list_empty(&vb->queue)); |
| 257 | |
| 258 | #ifdef DEBUG |
| 259 | /* This can be useful if you want to see if we actually fill |
| 260 | * the buffer with something */ |
| 261 | memset((void *)vb->baddr, 0xaa, vb->bsize); |
| 262 | #endif |
| 263 | |
| 264 | BUG_ON(NULL == icd->current_fmt); |
| 265 | |
| 266 | if (buf->fmt != icd->current_fmt || |
Guennadi Liakhovetski | a0705b0 | 2009-08-25 11:46:17 -0300 | [diff] [blame] | 267 | vb->width != icd->rect_current.width || |
| 268 | vb->height != icd->rect_current.height || |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 269 | vb->field != field) { |
| 270 | buf->fmt = icd->current_fmt; |
Guennadi Liakhovetski | a0705b0 | 2009-08-25 11:46:17 -0300 | [diff] [blame] | 271 | vb->width = icd->rect_current.width; |
| 272 | vb->height = icd->rect_current.height; |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 273 | vb->field = field; |
| 274 | vb->state = VIDEOBUF_NEEDS_INIT; |
| 275 | } |
| 276 | |
| 277 | vb->size = vb->width * vb->height * ((buf->fmt->depth + 7) >> 3); |
| 278 | if (0 != vb->baddr && vb->bsize < vb->size) { |
| 279 | ret = -EINVAL; |
| 280 | goto out; |
| 281 | } |
| 282 | |
| 283 | if (vb->state == VIDEOBUF_NEEDS_INIT) { |
| 284 | ret = videobuf_iolock(vq, vb, NULL); |
| 285 | if (ret) |
| 286 | goto fail; |
| 287 | vb->state = VIDEOBUF_PREPARED; |
| 288 | } |
| 289 | |
| 290 | return 0; |
| 291 | fail: |
| 292 | free_buffer(vq, buf); |
| 293 | out: |
| 294 | return ret; |
| 295 | } |
| 296 | |
Guennadi Liakhovetski | 2dd54a5 | 2009-08-05 20:06:31 -0300 | [diff] [blame] | 297 | /* Called under spinlock_irqsave(&pcdev->lock, ...) */ |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 298 | static void sh_mobile_ceu_videobuf_queue(struct videobuf_queue *vq, |
| 299 | struct videobuf_buffer *vb) |
| 300 | { |
| 301 | struct soc_camera_device *icd = vq->priv_data; |
| 302 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
| 303 | struct sh_mobile_ceu_dev *pcdev = ici->priv; |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 304 | |
Hans Verkuil | 86751b0 | 2008-07-18 01:35:14 -0300 | [diff] [blame] | 305 | 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] | 306 | vb, vb->baddr, vb->bsize); |
| 307 | |
Magnus Damm | 51354cc | 2008-10-16 19:51:20 -0300 | [diff] [blame] | 308 | vb->state = VIDEOBUF_QUEUED; |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 309 | list_add_tail(&vb->queue, &pcdev->capture); |
| 310 | |
| 311 | if (!pcdev->active) { |
| 312 | pcdev->active = vb; |
| 313 | sh_mobile_ceu_capture(pcdev); |
| 314 | } |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | static void sh_mobile_ceu_videobuf_release(struct videobuf_queue *vq, |
| 318 | struct videobuf_buffer *vb) |
| 319 | { |
Guennadi Liakhovetski | cca0e549 | 2009-08-25 11:46:51 -0300 | [diff] [blame] | 320 | struct soc_camera_device *icd = vq->priv_data; |
| 321 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
| 322 | struct sh_mobile_ceu_dev *pcdev = ici->priv; |
| 323 | unsigned long flags; |
| 324 | |
| 325 | spin_lock_irqsave(&pcdev->lock, flags); |
| 326 | |
| 327 | if (pcdev->active == vb) { |
| 328 | /* disable capture (release DMA buffer), reset */ |
| 329 | ceu_write(pcdev, CAPSR, 1 << 16); |
| 330 | pcdev->active = NULL; |
| 331 | } |
| 332 | |
| 333 | if ((vb->state == VIDEOBUF_ACTIVE || vb->state == VIDEOBUF_QUEUED) && |
| 334 | !list_empty(&vb->queue)) { |
| 335 | vb->state = VIDEOBUF_ERROR; |
| 336 | list_del_init(&vb->queue); |
| 337 | } |
| 338 | |
| 339 | spin_unlock_irqrestore(&pcdev->lock, flags); |
| 340 | |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 341 | free_buffer(vq, container_of(vb, struct sh_mobile_ceu_buffer, vb)); |
| 342 | } |
| 343 | |
| 344 | static struct videobuf_queue_ops sh_mobile_ceu_videobuf_ops = { |
| 345 | .buf_setup = sh_mobile_ceu_videobuf_setup, |
| 346 | .buf_prepare = sh_mobile_ceu_videobuf_prepare, |
| 347 | .buf_queue = sh_mobile_ceu_videobuf_queue, |
| 348 | .buf_release = sh_mobile_ceu_videobuf_release, |
| 349 | }; |
| 350 | |
| 351 | static irqreturn_t sh_mobile_ceu_irq(int irq, void *data) |
| 352 | { |
| 353 | struct sh_mobile_ceu_dev *pcdev = data; |
| 354 | struct videobuf_buffer *vb; |
| 355 | unsigned long flags; |
| 356 | |
| 357 | spin_lock_irqsave(&pcdev->lock, flags); |
| 358 | |
| 359 | vb = pcdev->active; |
Guennadi Liakhovetski | cca0e549 | 2009-08-25 11:46:51 -0300 | [diff] [blame] | 360 | if (!vb) |
| 361 | /* Stale interrupt from a released buffer */ |
| 362 | goto out; |
| 363 | |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 364 | list_del_init(&vb->queue); |
| 365 | |
| 366 | if (!list_empty(&pcdev->capture)) |
| 367 | pcdev->active = list_entry(pcdev->capture.next, |
| 368 | struct videobuf_buffer, queue); |
| 369 | else |
| 370 | pcdev->active = NULL; |
| 371 | |
| 372 | sh_mobile_ceu_capture(pcdev); |
| 373 | |
| 374 | vb->state = VIDEOBUF_DONE; |
| 375 | do_gettimeofday(&vb->ts); |
| 376 | vb->field_count++; |
| 377 | wake_up(&vb->done); |
Guennadi Liakhovetski | cca0e549 | 2009-08-25 11:46:51 -0300 | [diff] [blame] | 378 | |
| 379 | out: |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 380 | spin_unlock_irqrestore(&pcdev->lock, flags); |
| 381 | |
| 382 | return IRQ_HANDLED; |
| 383 | } |
| 384 | |
Guennadi Liakhovetski | 1c3bb74 | 2008-12-18 12:28:54 -0300 | [diff] [blame] | 385 | /* Called with .video_lock held */ |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 386 | static int sh_mobile_ceu_add_device(struct soc_camera_device *icd) |
| 387 | { |
| 388 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
| 389 | struct sh_mobile_ceu_dev *pcdev = ici->priv; |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 390 | |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 391 | if (pcdev->icd) |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 392 | return -EBUSY; |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 393 | |
| 394 | dev_info(&icd->dev, |
| 395 | "SuperH Mobile CEU driver attached to camera %d\n", |
| 396 | icd->devnum); |
| 397 | |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 398 | clk_enable(pcdev->clk); |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 399 | |
| 400 | ceu_write(pcdev, CAPSR, 1 << 16); /* reset */ |
| 401 | while (ceu_read(pcdev, CSTSR) & 1) |
| 402 | msleep(1); |
| 403 | |
| 404 | pcdev->icd = icd; |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 405 | |
| 406 | return 0; |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 407 | } |
| 408 | |
Guennadi Liakhovetski | 1c3bb74 | 2008-12-18 12:28:54 -0300 | [diff] [blame] | 409 | /* Called with .video_lock held */ |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 410 | static void sh_mobile_ceu_remove_device(struct soc_camera_device *icd) |
| 411 | { |
| 412 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
| 413 | struct sh_mobile_ceu_dev *pcdev = ici->priv; |
Magnus Damm | 51354cc | 2008-10-16 19:51:20 -0300 | [diff] [blame] | 414 | unsigned long flags; |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 415 | |
| 416 | BUG_ON(icd != pcdev->icd); |
| 417 | |
| 418 | /* disable capture, disable interrupts */ |
| 419 | ceu_write(pcdev, CEIER, 0); |
| 420 | ceu_write(pcdev, CAPSR, 1 << 16); /* reset */ |
Magnus Damm | 51354cc | 2008-10-16 19:51:20 -0300 | [diff] [blame] | 421 | |
| 422 | /* make sure active buffer is canceled */ |
| 423 | spin_lock_irqsave(&pcdev->lock, flags); |
| 424 | if (pcdev->active) { |
| 425 | list_del(&pcdev->active->queue); |
| 426 | pcdev->active->state = VIDEOBUF_ERROR; |
| 427 | wake_up_all(&pcdev->active->done); |
| 428 | pcdev->active = NULL; |
| 429 | } |
| 430 | spin_unlock_irqrestore(&pcdev->lock, flags); |
| 431 | |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 432 | clk_disable(pcdev->clk); |
| 433 | |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 434 | dev_info(&icd->dev, |
| 435 | "SuperH Mobile CEU driver detached from camera %d\n", |
| 436 | icd->devnum); |
| 437 | |
| 438 | pcdev->icd = NULL; |
| 439 | } |
| 440 | |
Guennadi Liakhovetski | 961801b | 2009-08-25 11:46:54 -0300 | [diff] [blame^] | 441 | /* |
| 442 | * See chapter 29.4.12 "Capture Filter Control Register (CFLCR)" |
| 443 | * in SH7722 Hardware Manual |
| 444 | */ |
| 445 | static unsigned int size_dst(unsigned int src, unsigned int scale) |
| 446 | { |
| 447 | unsigned int mant_pre = scale >> 12; |
| 448 | if (!src || !scale) |
| 449 | return src; |
| 450 | return ((mant_pre + 2 * (src - 1)) / (2 * mant_pre) - 1) * |
| 451 | mant_pre * 4096 / scale + 1; |
| 452 | } |
| 453 | |
| 454 | static unsigned int size_src(unsigned int dst, unsigned int scale) |
| 455 | { |
| 456 | unsigned int mant_pre = scale >> 12, tmp; |
| 457 | if (!dst || !scale) |
| 458 | return dst; |
| 459 | for (tmp = ((dst - 1) * scale + 2048 * mant_pre) / 4096 + 1; |
| 460 | size_dst(tmp, scale) < dst; |
| 461 | tmp++) |
| 462 | ; |
| 463 | return tmp; |
| 464 | } |
| 465 | |
| 466 | static u16 calc_scale(unsigned int src, unsigned int *dst) |
| 467 | { |
| 468 | u16 scale; |
| 469 | |
| 470 | if (src == *dst) |
| 471 | return 0; |
| 472 | |
| 473 | scale = (src * 4096 / *dst) & ~7; |
| 474 | |
| 475 | while (scale > 4096 && size_dst(src, scale) < *dst) |
| 476 | scale -= 8; |
| 477 | |
| 478 | *dst = size_dst(src, scale); |
| 479 | |
| 480 | return scale; |
| 481 | } |
| 482 | |
| 483 | /* rect is guaranteed to not exceed the scaled camera rectangle */ |
Guennadi Liakhovetski | 904078f | 2009-08-25 11:46:52 -0300 | [diff] [blame] | 484 | static void sh_mobile_ceu_set_rect(struct soc_camera_device *icd, |
| 485 | struct v4l2_rect *rect) |
| 486 | { |
| 487 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
| 488 | struct sh_mobile_ceu_cam *cam = icd->host_priv; |
| 489 | struct sh_mobile_ceu_dev *pcdev = ici->priv; |
Guennadi Liakhovetski | 961801b | 2009-08-25 11:46:54 -0300 | [diff] [blame^] | 490 | int width, height, cfszr_width, cdwdr_width, in_width, in_height; |
| 491 | unsigned int left_offset, top_offset, left, top; |
| 492 | unsigned int hscale = pcdev->cflcr & 0xffff; |
| 493 | unsigned int vscale = (pcdev->cflcr >> 16) & 0xffff; |
Guennadi Liakhovetski | 904078f | 2009-08-25 11:46:52 -0300 | [diff] [blame] | 494 | u32 camor; |
| 495 | |
Guennadi Liakhovetski | 961801b | 2009-08-25 11:46:54 -0300 | [diff] [blame^] | 496 | /* Switch to the camera scale */ |
| 497 | left = size_src(rect->left, hscale); |
| 498 | top = size_src(rect->top, vscale); |
| 499 | |
| 500 | dev_dbg(&icd->dev, "Left %u * 0x%x = %u, top %u * 0x%x = %u\n", |
| 501 | rect->left, hscale, left, rect->top, vscale, top); |
| 502 | |
| 503 | if (left > cam->camera_rect.left) { |
| 504 | left_offset = left - cam->camera_rect.left; |
Guennadi Liakhovetski | 904078f | 2009-08-25 11:46:52 -0300 | [diff] [blame] | 505 | } else { |
| 506 | left_offset = 0; |
Guennadi Liakhovetski | 961801b | 2009-08-25 11:46:54 -0300 | [diff] [blame^] | 507 | left = cam->camera_rect.left; |
Guennadi Liakhovetski | 904078f | 2009-08-25 11:46:52 -0300 | [diff] [blame] | 508 | } |
| 509 | |
Guennadi Liakhovetski | 961801b | 2009-08-25 11:46:54 -0300 | [diff] [blame^] | 510 | if (top > cam->camera_rect.top) { |
| 511 | top_offset = top - cam->camera_rect.top; |
Guennadi Liakhovetski | 904078f | 2009-08-25 11:46:52 -0300 | [diff] [blame] | 512 | } else { |
| 513 | top_offset = 0; |
Guennadi Liakhovetski | 961801b | 2009-08-25 11:46:54 -0300 | [diff] [blame^] | 514 | top = cam->camera_rect.top; |
Guennadi Liakhovetski | 904078f | 2009-08-25 11:46:52 -0300 | [diff] [blame] | 515 | } |
| 516 | |
Guennadi Liakhovetski | 961801b | 2009-08-25 11:46:54 -0300 | [diff] [blame^] | 517 | dev_dbg(&icd->dev, "New left %u, top %u, offsets %u:%u\n", |
| 518 | rect->left, rect->top, left_offset, top_offset); |
Guennadi Liakhovetski | 904078f | 2009-08-25 11:46:52 -0300 | [diff] [blame] | 519 | |
| 520 | if (pcdev->image_mode) { |
| 521 | width = rect->width; |
Guennadi Liakhovetski | 961801b | 2009-08-25 11:46:54 -0300 | [diff] [blame^] | 522 | in_width = cam->camera_rect.width; |
| 523 | if (!pcdev->is_16bit) { |
Guennadi Liakhovetski | 904078f | 2009-08-25 11:46:52 -0300 | [diff] [blame] | 524 | width *= 2; |
Guennadi Liakhovetski | 961801b | 2009-08-25 11:46:54 -0300 | [diff] [blame^] | 525 | in_width *= 2; |
| 526 | left_offset *= 2; |
| 527 | } |
Guennadi Liakhovetski | 904078f | 2009-08-25 11:46:52 -0300 | [diff] [blame] | 528 | cfszr_width = cdwdr_width = rect->width; |
| 529 | } else { |
Guennadi Liakhovetski | 961801b | 2009-08-25 11:46:54 -0300 | [diff] [blame^] | 530 | unsigned int w_factor = (icd->current_fmt->depth + 7) >> 3; |
| 531 | if (!pcdev->is_16bit) |
| 532 | w_factor *= 2; |
| 533 | |
| 534 | width = rect->width * w_factor / 2; |
| 535 | in_width = cam->camera_rect.width * w_factor / 2; |
| 536 | left_offset = left_offset * w_factor / 2; |
| 537 | |
Guennadi Liakhovetski | 904078f | 2009-08-25 11:46:52 -0300 | [diff] [blame] | 538 | cfszr_width = pcdev->is_16bit ? width : width / 2; |
| 539 | cdwdr_width = pcdev->is_16bit ? width * 2 : width; |
| 540 | } |
| 541 | |
| 542 | height = rect->height; |
Guennadi Liakhovetski | 961801b | 2009-08-25 11:46:54 -0300 | [diff] [blame^] | 543 | in_height = cam->camera_rect.height; |
Guennadi Liakhovetski | 904078f | 2009-08-25 11:46:52 -0300 | [diff] [blame] | 544 | if (pcdev->is_interlaced) { |
| 545 | height /= 2; |
Guennadi Liakhovetski | 961801b | 2009-08-25 11:46:54 -0300 | [diff] [blame^] | 546 | in_height /= 2; |
| 547 | top_offset /= 2; |
Guennadi Liakhovetski | 904078f | 2009-08-25 11:46:52 -0300 | [diff] [blame] | 548 | cdwdr_width *= 2; |
| 549 | } |
| 550 | |
| 551 | camor = left_offset | (top_offset << 16); |
| 552 | ceu_write(pcdev, CAMOR, camor); |
Guennadi Liakhovetski | 961801b | 2009-08-25 11:46:54 -0300 | [diff] [blame^] | 553 | ceu_write(pcdev, CAPWR, (in_height << 16) | in_width); |
Guennadi Liakhovetski | 904078f | 2009-08-25 11:46:52 -0300 | [diff] [blame] | 554 | ceu_write(pcdev, CFSZR, (height << 16) | cfszr_width); |
| 555 | ceu_write(pcdev, CDWDR, cdwdr_width); |
| 556 | } |
| 557 | |
| 558 | static u32 capture_save_reset(struct sh_mobile_ceu_dev *pcdev) |
| 559 | { |
| 560 | u32 capsr = ceu_read(pcdev, CAPSR); |
| 561 | ceu_write(pcdev, CAPSR, 1 << 16); /* reset, stop capture */ |
| 562 | return capsr; |
| 563 | } |
| 564 | |
| 565 | static void capture_restore(struct sh_mobile_ceu_dev *pcdev, u32 capsr) |
| 566 | { |
| 567 | unsigned long timeout = jiffies + 10 * HZ; |
| 568 | |
| 569 | /* |
| 570 | * Wait until the end of the current frame. It can take a long time, |
| 571 | * but if it has been aborted by a CAPSR reset, it shoule exit sooner. |
| 572 | */ |
| 573 | while ((ceu_read(pcdev, CSTSR) & 1) && time_before(jiffies, timeout)) |
| 574 | msleep(1); |
| 575 | |
| 576 | if (time_after(jiffies, timeout)) { |
| 577 | dev_err(pcdev->ici.v4l2_dev.dev, |
| 578 | "Timeout waiting for frame end! Interface problem?\n"); |
| 579 | return; |
| 580 | } |
| 581 | |
| 582 | /* Wait until reset clears, this shall not hang... */ |
| 583 | while (ceu_read(pcdev, CAPSR) & (1 << 16)) |
| 584 | udelay(10); |
| 585 | |
| 586 | /* Anything to restore? */ |
| 587 | if (capsr & ~(1 << 16)) |
| 588 | ceu_write(pcdev, CAPSR, capsr); |
| 589 | } |
| 590 | |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 591 | static int sh_mobile_ceu_set_bus_param(struct soc_camera_device *icd, |
| 592 | __u32 pixfmt) |
| 593 | { |
| 594 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
| 595 | struct sh_mobile_ceu_dev *pcdev = ici->priv; |
Guennadi Liakhovetski | 904078f | 2009-08-25 11:46:52 -0300 | [diff] [blame] | 596 | int ret; |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 597 | unsigned long camera_flags, common_flags, value; |
Guennadi Liakhovetski | 904078f | 2009-08-25 11:46:52 -0300 | [diff] [blame] | 598 | int yuv_lineskip; |
| 599 | struct sh_mobile_ceu_cam *cam = icd->host_priv; |
| 600 | u32 capsr = capture_save_reset(pcdev); |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 601 | |
| 602 | camera_flags = icd->ops->query_bus_param(icd); |
| 603 | common_flags = soc_camera_bus_param_compatible(camera_flags, |
Kuninori Morimoto | c354b40 | 2009-02-23 12:12:58 -0300 | [diff] [blame] | 604 | make_bus_param(pcdev)); |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 605 | if (!common_flags) |
| 606 | return -EINVAL; |
| 607 | |
| 608 | ret = icd->ops->set_bus_param(icd, common_flags); |
| 609 | if (ret < 0) |
| 610 | return ret; |
| 611 | |
| 612 | switch (common_flags & SOCAM_DATAWIDTH_MASK) { |
| 613 | case SOCAM_DATAWIDTH_8: |
Guennadi Liakhovetski | 904078f | 2009-08-25 11:46:52 -0300 | [diff] [blame] | 614 | pcdev->is_16bit = 0; |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 615 | break; |
| 616 | case SOCAM_DATAWIDTH_16: |
Guennadi Liakhovetski | 904078f | 2009-08-25 11:46:52 -0300 | [diff] [blame] | 617 | pcdev->is_16bit = 1; |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 618 | break; |
| 619 | default: |
| 620 | return -EINVAL; |
| 621 | } |
| 622 | |
| 623 | ceu_write(pcdev, CRCNTR, 0); |
| 624 | ceu_write(pcdev, CRCMPR, 0); |
| 625 | |
Magnus Damm | 8be65dc | 2008-12-18 12:38:06 -0300 | [diff] [blame] | 626 | value = 0x00000010; /* data fetch by default */ |
Guennadi Liakhovetski | 961801b | 2009-08-25 11:46:54 -0300 | [diff] [blame^] | 627 | yuv_lineskip = 0; |
Magnus Damm | 8be65dc | 2008-12-18 12:38:06 -0300 | [diff] [blame] | 628 | |
| 629 | switch (icd->current_fmt->fourcc) { |
| 630 | case V4L2_PIX_FMT_NV12: |
| 631 | case V4L2_PIX_FMT_NV21: |
| 632 | yuv_lineskip = 1; /* skip for NV12/21, no skip for NV16/61 */ |
Magnus Damm | 9e4a56d | 2008-12-18 12:45:33 -0300 | [diff] [blame] | 633 | /* fall-through */ |
| 634 | case V4L2_PIX_FMT_NV16: |
| 635 | case V4L2_PIX_FMT_NV61: |
Guennadi Liakhovetski | 904078f | 2009-08-25 11:46:52 -0300 | [diff] [blame] | 636 | switch (cam->camera_fmt->fourcc) { |
Magnus Damm | 8be65dc | 2008-12-18 12:38:06 -0300 | [diff] [blame] | 637 | case V4L2_PIX_FMT_UYVY: |
| 638 | value = 0x00000000; /* Cb0, Y0, Cr0, Y1 */ |
| 639 | break; |
| 640 | case V4L2_PIX_FMT_VYUY: |
| 641 | value = 0x00000100; /* Cr0, Y0, Cb0, Y1 */ |
| 642 | break; |
| 643 | case V4L2_PIX_FMT_YUYV: |
| 644 | value = 0x00000200; /* Y0, Cb0, Y1, Cr0 */ |
| 645 | break; |
| 646 | case V4L2_PIX_FMT_YVYU: |
| 647 | value = 0x00000300; /* Y0, Cr0, Y1, Cb0 */ |
| 648 | break; |
| 649 | default: |
| 650 | BUG(); |
| 651 | } |
| 652 | } |
| 653 | |
Magnus Damm | 7a1a816 | 2008-12-18 12:50:30 -0300 | [diff] [blame] | 654 | if (icd->current_fmt->fourcc == V4L2_PIX_FMT_NV21 || |
| 655 | icd->current_fmt->fourcc == V4L2_PIX_FMT_NV61) |
Magnus Damm | 9e4a56d | 2008-12-18 12:45:33 -0300 | [diff] [blame] | 656 | value ^= 0x00000100; /* swap U, V to change from NV1x->NVx1 */ |
Magnus Damm | 8be65dc | 2008-12-18 12:38:06 -0300 | [diff] [blame] | 657 | |
Magnus Damm | 7a1a816 | 2008-12-18 12:50:30 -0300 | [diff] [blame] | 658 | value |= common_flags & SOCAM_VSYNC_ACTIVE_LOW ? 1 << 1 : 0; |
| 659 | value |= common_flags & SOCAM_HSYNC_ACTIVE_LOW ? 1 << 0 : 0; |
Guennadi Liakhovetski | 904078f | 2009-08-25 11:46:52 -0300 | [diff] [blame] | 660 | value |= pcdev->is_16bit ? 1 << 12 : 0; |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 661 | ceu_write(pcdev, CAMCR, value); |
| 662 | |
| 663 | ceu_write(pcdev, CAPCR, 0x00300000); |
Guennadi Liakhovetski | e802967 | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 664 | ceu_write(pcdev, CAIFR, pcdev->is_interlaced ? 0x101 : 0); |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 665 | |
| 666 | mdelay(1); |
Guennadi Liakhovetski | 904078f | 2009-08-25 11:46:52 -0300 | [diff] [blame] | 667 | sh_mobile_ceu_set_rect(icd, &icd->rect_current); |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 668 | |
Guennadi Liakhovetski | 961801b | 2009-08-25 11:46:54 -0300 | [diff] [blame^] | 669 | ceu_write(pcdev, CFLCR, pcdev->cflcr); |
Magnus Damm | dd54203 | 2008-10-16 19:50:22 -0300 | [diff] [blame] | 670 | |
| 671 | /* A few words about byte order (observed in Big Endian mode) |
| 672 | * |
| 673 | * In data fetch mode bytes are received in chunks of 8 bytes. |
| 674 | * D0, D1, D2, D3, D4, D5, D6, D7 (D0 received first) |
| 675 | * |
| 676 | * The data is however by default written to memory in reverse order: |
| 677 | * D7, D6, D5, D4, D3, D2, D1, D0 (D7 written to lowest byte) |
| 678 | * |
| 679 | * The lowest three bits of CDOCR allows us to do swapping, |
Magnus Damm | 2c0a072 | 2008-10-16 19:50:56 -0300 | [diff] [blame] | 680 | * using 7 we swap the data bytes to match the incoming order: |
| 681 | * D0, D1, D2, D3, D4, D5, D6, D7 |
Magnus Damm | dd54203 | 2008-10-16 19:50:22 -0300 | [diff] [blame] | 682 | */ |
Magnus Damm | 8be65dc | 2008-12-18 12:38:06 -0300 | [diff] [blame] | 683 | value = 0x00000017; |
| 684 | if (yuv_lineskip) |
| 685 | value &= ~0x00000010; /* convert 4:2:2 -> 4:2:0 */ |
| 686 | |
| 687 | ceu_write(pcdev, CDOCR, value); |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 688 | ceu_write(pcdev, CFWCR, 0); /* keep "datafetch firewall" disabled */ |
| 689 | |
Guennadi Liakhovetski | 961801b | 2009-08-25 11:46:54 -0300 | [diff] [blame^] | 690 | dev_dbg(&icd->dev, "S_FMT successful for %c%c%c%c %ux%u@%u:%u\n", |
Guennadi Liakhovetski | 904078f | 2009-08-25 11:46:52 -0300 | [diff] [blame] | 691 | pixfmt & 0xff, (pixfmt >> 8) & 0xff, |
| 692 | (pixfmt >> 16) & 0xff, (pixfmt >> 24) & 0xff, |
| 693 | icd->rect_current.width, icd->rect_current.height, |
| 694 | icd->rect_current.left, icd->rect_current.top); |
| 695 | |
| 696 | capture_restore(pcdev, capsr); |
| 697 | |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 698 | /* not in bundle mode: skip CBDSR, CDAYR2, CDACR2, CDBYR2, CDBCR2 */ |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 699 | return 0; |
| 700 | } |
| 701 | |
Magnus Damm | 9414de3 | 2008-12-18 11:49:06 -0300 | [diff] [blame] | 702 | 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] | 703 | { |
| 704 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
| 705 | struct sh_mobile_ceu_dev *pcdev = ici->priv; |
| 706 | unsigned long camera_flags, common_flags; |
| 707 | |
| 708 | camera_flags = icd->ops->query_bus_param(icd); |
| 709 | common_flags = soc_camera_bus_param_compatible(camera_flags, |
Kuninori Morimoto | c354b40 | 2009-02-23 12:12:58 -0300 | [diff] [blame] | 710 | make_bus_param(pcdev)); |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 711 | if (!common_flags) |
| 712 | return -EINVAL; |
| 713 | |
| 714 | return 0; |
| 715 | } |
| 716 | |
Magnus Damm | 8be65dc | 2008-12-18 12:38:06 -0300 | [diff] [blame] | 717 | static const struct soc_camera_data_format sh_mobile_ceu_formats[] = { |
| 718 | { |
| 719 | .name = "NV12", |
| 720 | .depth = 12, |
| 721 | .fourcc = V4L2_PIX_FMT_NV12, |
| 722 | .colorspace = V4L2_COLORSPACE_JPEG, |
| 723 | }, |
| 724 | { |
| 725 | .name = "NV21", |
| 726 | .depth = 12, |
| 727 | .fourcc = V4L2_PIX_FMT_NV21, |
| 728 | .colorspace = V4L2_COLORSPACE_JPEG, |
| 729 | }, |
Magnus Damm | 9e4a56d | 2008-12-18 12:45:33 -0300 | [diff] [blame] | 730 | { |
| 731 | .name = "NV16", |
| 732 | .depth = 16, |
| 733 | .fourcc = V4L2_PIX_FMT_NV16, |
| 734 | .colorspace = V4L2_COLORSPACE_JPEG, |
| 735 | }, |
| 736 | { |
| 737 | .name = "NV61", |
| 738 | .depth = 16, |
| 739 | .fourcc = V4L2_PIX_FMT_NV61, |
| 740 | .colorspace = V4L2_COLORSPACE_JPEG, |
| 741 | }, |
Magnus Damm | 8be65dc | 2008-12-18 12:38:06 -0300 | [diff] [blame] | 742 | }; |
| 743 | |
Magnus Damm | 9414de3 | 2008-12-18 11:49:06 -0300 | [diff] [blame] | 744 | static int sh_mobile_ceu_get_formats(struct soc_camera_device *icd, int idx, |
| 745 | struct soc_camera_format_xlate *xlate) |
| 746 | { |
| 747 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
Magnus Damm | 8be65dc | 2008-12-18 12:38:06 -0300 | [diff] [blame] | 748 | int ret, k, n; |
Magnus Damm | 9414de3 | 2008-12-18 11:49:06 -0300 | [diff] [blame] | 749 | int formats = 0; |
Guennadi Liakhovetski | 904078f | 2009-08-25 11:46:52 -0300 | [diff] [blame] | 750 | struct sh_mobile_ceu_cam *cam; |
Magnus Damm | 9414de3 | 2008-12-18 11:49:06 -0300 | [diff] [blame] | 751 | |
| 752 | ret = sh_mobile_ceu_try_bus_param(icd); |
| 753 | if (ret < 0) |
| 754 | return 0; |
| 755 | |
Guennadi Liakhovetski | 904078f | 2009-08-25 11:46:52 -0300 | [diff] [blame] | 756 | if (!icd->host_priv) { |
| 757 | cam = kzalloc(sizeof(*cam), GFP_KERNEL); |
| 758 | if (!cam) |
| 759 | return -ENOMEM; |
| 760 | |
| 761 | icd->host_priv = cam; |
Guennadi Liakhovetski | 961801b | 2009-08-25 11:46:54 -0300 | [diff] [blame^] | 762 | cam->camera_max = icd->rect_max; |
Guennadi Liakhovetski | 904078f | 2009-08-25 11:46:52 -0300 | [diff] [blame] | 763 | } else { |
| 764 | cam = icd->host_priv; |
| 765 | } |
| 766 | |
Guennadi Liakhovetski | c8329ac | 2009-02-23 12:12:58 -0300 | [diff] [blame] | 767 | /* Beginning of a pass */ |
| 768 | if (!idx) |
Guennadi Liakhovetski | 904078f | 2009-08-25 11:46:52 -0300 | [diff] [blame] | 769 | cam->extra_fmt = NULL; |
Guennadi Liakhovetski | c8329ac | 2009-02-23 12:12:58 -0300 | [diff] [blame] | 770 | |
Magnus Damm | 9414de3 | 2008-12-18 11:49:06 -0300 | [diff] [blame] | 771 | switch (icd->formats[idx].fourcc) { |
Magnus Damm | 8be65dc | 2008-12-18 12:38:06 -0300 | [diff] [blame] | 772 | case V4L2_PIX_FMT_UYVY: |
| 773 | case V4L2_PIX_FMT_VYUY: |
| 774 | case V4L2_PIX_FMT_YUYV: |
| 775 | case V4L2_PIX_FMT_YVYU: |
Guennadi Liakhovetski | 904078f | 2009-08-25 11:46:52 -0300 | [diff] [blame] | 776 | if (cam->extra_fmt) |
Guennadi Liakhovetski | c8329ac | 2009-02-23 12:12:58 -0300 | [diff] [blame] | 777 | goto add_single_format; |
| 778 | |
| 779 | /* |
| 780 | * Our case is simple so far: for any of the above four camera |
| 781 | * formats we add all our four synthesized NV* formats, so, |
| 782 | * just marking the device with a single flag suffices. If |
| 783 | * the format generation rules are more complex, you would have |
| 784 | * to actually hang your already added / counted formats onto |
| 785 | * the host_priv pointer and check whether the format you're |
| 786 | * going to add now is already there. |
| 787 | */ |
Guennadi Liakhovetski | 904078f | 2009-08-25 11:46:52 -0300 | [diff] [blame] | 788 | cam->extra_fmt = (void *)sh_mobile_ceu_formats; |
Guennadi Liakhovetski | c8329ac | 2009-02-23 12:12:58 -0300 | [diff] [blame] | 789 | |
Magnus Damm | 8be65dc | 2008-12-18 12:38:06 -0300 | [diff] [blame] | 790 | n = ARRAY_SIZE(sh_mobile_ceu_formats); |
| 791 | formats += n; |
| 792 | for (k = 0; xlate && k < n; k++) { |
| 793 | xlate->host_fmt = &sh_mobile_ceu_formats[k]; |
| 794 | xlate->cam_fmt = icd->formats + idx; |
| 795 | xlate->buswidth = icd->formats[idx].depth; |
| 796 | xlate++; |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 797 | dev_dbg(ici->v4l2_dev.dev, "Providing format %s using %s\n", |
Magnus Damm | 8be65dc | 2008-12-18 12:38:06 -0300 | [diff] [blame] | 798 | sh_mobile_ceu_formats[k].name, |
| 799 | icd->formats[idx].name); |
| 800 | } |
Magnus Damm | 9414de3 | 2008-12-18 11:49:06 -0300 | [diff] [blame] | 801 | default: |
Guennadi Liakhovetski | c8329ac | 2009-02-23 12:12:58 -0300 | [diff] [blame] | 802 | add_single_format: |
Magnus Damm | 9414de3 | 2008-12-18 11:49:06 -0300 | [diff] [blame] | 803 | /* Generic pass-through */ |
| 804 | formats++; |
| 805 | if (xlate) { |
| 806 | xlate->host_fmt = icd->formats + idx; |
| 807 | xlate->cam_fmt = icd->formats + idx; |
| 808 | xlate->buswidth = icd->formats[idx].depth; |
| 809 | xlate++; |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 810 | dev_dbg(ici->v4l2_dev.dev, |
Magnus Damm | 9414de3 | 2008-12-18 11:49:06 -0300 | [diff] [blame] | 811 | "Providing format %s in pass-through mode\n", |
| 812 | icd->formats[idx].name); |
| 813 | } |
| 814 | } |
| 815 | |
| 816 | return formats; |
| 817 | } |
| 818 | |
Guennadi Liakhovetski | 904078f | 2009-08-25 11:46:52 -0300 | [diff] [blame] | 819 | static void sh_mobile_ceu_put_formats(struct soc_camera_device *icd) |
| 820 | { |
| 821 | kfree(icd->host_priv); |
| 822 | icd->host_priv = NULL; |
| 823 | } |
| 824 | |
Guennadi Liakhovetski | 961801b | 2009-08-25 11:46:54 -0300 | [diff] [blame^] | 825 | /* Check if any dimension of r1 is smaller than respective one of r2 */ |
Guennadi Liakhovetski | 904078f | 2009-08-25 11:46:52 -0300 | [diff] [blame] | 826 | static bool is_smaller(struct v4l2_rect *r1, struct v4l2_rect *r2) |
| 827 | { |
| 828 | return r1->width < r2->width || r1->height < r2->height; |
| 829 | } |
| 830 | |
Guennadi Liakhovetski | 961801b | 2009-08-25 11:46:54 -0300 | [diff] [blame^] | 831 | /* Check if r1 fails to cover r2 */ |
| 832 | static bool is_inside(struct v4l2_rect *r1, struct v4l2_rect *r2) |
| 833 | { |
| 834 | return r1->left > r2->left || r1->top > r2->top || |
| 835 | r1->left + r1->width < r2->left + r2->width || |
| 836 | r1->top + r1->height < r2->top + r2->height; |
| 837 | } |
| 838 | |
| 839 | /* |
| 840 | * CEU can scale and crop, but we don't want to waste bandwidth and kill the |
| 841 | * framerate by always requesting the maximum image from the client. For |
| 842 | * cropping we also have to take care of the current scale. The common for both |
| 843 | * scaling and cropping approach is: |
| 844 | * 1. try if the client can produce exactly what requested by the user |
| 845 | * 2. if (1) failed, try to double the client image until we get one big enough |
| 846 | * 3. if (2) failed, try to request the maximum image |
| 847 | */ |
Guennadi Liakhovetski | 09e231b | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 848 | static int sh_mobile_ceu_set_crop(struct soc_camera_device *icd, |
| 849 | struct v4l2_rect *rect) |
| 850 | { |
Guennadi Liakhovetski | 904078f | 2009-08-25 11:46:52 -0300 | [diff] [blame] | 851 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
| 852 | struct sh_mobile_ceu_dev *pcdev = ici->priv; |
Guennadi Liakhovetski | 961801b | 2009-08-25 11:46:54 -0300 | [diff] [blame^] | 853 | struct v4l2_rect cam_rect, target, cam_max; |
Guennadi Liakhovetski | 904078f | 2009-08-25 11:46:52 -0300 | [diff] [blame] | 854 | struct sh_mobile_ceu_cam *cam = icd->host_priv; |
Guennadi Liakhovetski | 961801b | 2009-08-25 11:46:54 -0300 | [diff] [blame^] | 855 | unsigned int hscale = pcdev->cflcr & 0xffff; |
| 856 | unsigned int vscale = (pcdev->cflcr >> 16) & 0xffff; |
Guennadi Liakhovetski | 904078f | 2009-08-25 11:46:52 -0300 | [diff] [blame] | 857 | unsigned short width, height; |
| 858 | u32 capsr; |
| 859 | int ret; |
| 860 | |
Guennadi Liakhovetski | 961801b | 2009-08-25 11:46:54 -0300 | [diff] [blame^] | 861 | /* Scale back up into client units */ |
| 862 | cam_rect.left = size_src(rect->left, hscale); |
| 863 | cam_rect.width = size_src(rect->width, hscale); |
| 864 | cam_rect.top = size_src(rect->top, vscale); |
| 865 | cam_rect.height = size_src(rect->height, vscale); |
Guennadi Liakhovetski | 904078f | 2009-08-25 11:46:52 -0300 | [diff] [blame] | 866 | |
Guennadi Liakhovetski | 961801b | 2009-08-25 11:46:54 -0300 | [diff] [blame^] | 867 | target = cam_rect; |
| 868 | |
| 869 | capsr = capture_save_reset(pcdev); |
| 870 | dev_dbg(&icd->dev, "CAPSR 0x%x, CFLCR 0x%x\n", capsr, pcdev->cflcr); |
| 871 | |
| 872 | /* First attempt - see if the client can deliver a perfect result */ |
Guennadi Liakhovetski | 904078f | 2009-08-25 11:46:52 -0300 | [diff] [blame] | 873 | ret = icd->ops->set_crop(icd, &cam_rect); |
Guennadi Liakhovetski | 961801b | 2009-08-25 11:46:54 -0300 | [diff] [blame^] | 874 | if (!ret && !memcmp(&target, &cam_rect, sizeof(target))) { |
| 875 | dev_dbg(&icd->dev, "Camera S_CROP successful for %ux%u@%u:%u\n", |
Guennadi Liakhovetski | 904078f | 2009-08-25 11:46:52 -0300 | [diff] [blame] | 876 | cam_rect.width, cam_rect.height, |
| 877 | cam_rect.left, cam_rect.top); |
| 878 | goto ceu_set_rect; |
| 879 | } |
| 880 | |
| 881 | /* Try to fix cropping, that camera hasn't managed to do */ |
Guennadi Liakhovetski | 961801b | 2009-08-25 11:46:54 -0300 | [diff] [blame^] | 882 | dev_dbg(&icd->dev, "Fix camera S_CROP %d for %ux%u@%u:%u" |
| 883 | " to %ux%u@%u:%u\n", |
Guennadi Liakhovetski | 904078f | 2009-08-25 11:46:52 -0300 | [diff] [blame] | 884 | ret, cam_rect.width, cam_rect.height, |
Guennadi Liakhovetski | 961801b | 2009-08-25 11:46:54 -0300 | [diff] [blame^] | 885 | cam_rect.left, cam_rect.top, |
| 886 | target.width, target.height, target.left, target.top); |
Guennadi Liakhovetski | 904078f | 2009-08-25 11:46:52 -0300 | [diff] [blame] | 887 | |
| 888 | /* |
| 889 | * Popular special case - some cameras can only handle fixed sizes like |
| 890 | * QVGA, VGA,... Take care to avoid infinite loop. |
| 891 | */ |
Guennadi Liakhovetski | 961801b | 2009-08-25 11:46:54 -0300 | [diff] [blame^] | 892 | width = max(cam_rect.width, 1); |
| 893 | height = max(cam_rect.height, 1); |
| 894 | cam_max.width = size_src(icd->rect_max.width, hscale); |
| 895 | cam_max.left = size_src(icd->rect_max.left, hscale); |
| 896 | cam_max.height = size_src(icd->rect_max.height, vscale); |
| 897 | cam_max.top = size_src(icd->rect_max.top, vscale); |
| 898 | while (!ret && (is_smaller(&cam_rect, &target) || |
| 899 | is_inside(&cam_rect, &target)) && |
| 900 | cam_max.width >= width && cam_max.height >= height) { |
| 901 | |
| 902 | width *= 2; |
| 903 | height *= 2; |
Guennadi Liakhovetski | 904078f | 2009-08-25 11:46:52 -0300 | [diff] [blame] | 904 | cam_rect.width = width; |
| 905 | cam_rect.height = height; |
| 906 | |
Guennadi Liakhovetski | 961801b | 2009-08-25 11:46:54 -0300 | [diff] [blame^] | 907 | /* We do not know what the camera is capable of, play safe */ |
| 908 | if (cam_rect.left > target.left) |
| 909 | cam_rect.left = cam_max.left; |
| 910 | |
| 911 | if (cam_rect.left + cam_rect.width < target.left + target.width) |
| 912 | cam_rect.width = target.left + target.width - |
| 913 | cam_rect.left; |
| 914 | |
| 915 | if (cam_rect.top > target.top) |
| 916 | cam_rect.top = cam_max.top; |
| 917 | |
| 918 | if (cam_rect.top + cam_rect.height < target.top + target.height) |
| 919 | cam_rect.height = target.top + target.height - |
| 920 | cam_rect.top; |
| 921 | |
Guennadi Liakhovetski | 904078f | 2009-08-25 11:46:52 -0300 | [diff] [blame] | 922 | if (cam_rect.width + cam_rect.left > |
Guennadi Liakhovetski | 961801b | 2009-08-25 11:46:54 -0300 | [diff] [blame^] | 923 | cam_max.width + cam_max.left) |
| 924 | cam_rect.left = max(cam_max.width + cam_max.left - |
| 925 | cam_rect.width, cam_max.left); |
Guennadi Liakhovetski | 904078f | 2009-08-25 11:46:52 -0300 | [diff] [blame] | 926 | |
| 927 | if (cam_rect.height + cam_rect.top > |
Guennadi Liakhovetski | 961801b | 2009-08-25 11:46:54 -0300 | [diff] [blame^] | 928 | cam_max.height + cam_max.top) |
| 929 | cam_rect.top = max(cam_max.height + cam_max.top - |
| 930 | cam_rect.height, cam_max.top); |
Guennadi Liakhovetski | 904078f | 2009-08-25 11:46:52 -0300 | [diff] [blame] | 931 | |
| 932 | ret = icd->ops->set_crop(icd, &cam_rect); |
Guennadi Liakhovetski | 961801b | 2009-08-25 11:46:54 -0300 | [diff] [blame^] | 933 | dev_dbg(&icd->dev, "Camera S_CROP %d for %ux%u@%u:%u\n", |
Guennadi Liakhovetski | 904078f | 2009-08-25 11:46:52 -0300 | [diff] [blame] | 934 | ret, cam_rect.width, cam_rect.height, |
| 935 | cam_rect.left, cam_rect.top); |
Guennadi Liakhovetski | 904078f | 2009-08-25 11:46:52 -0300 | [diff] [blame] | 936 | } |
| 937 | |
| 938 | /* |
| 939 | * If the camera failed to configure cropping, it should not modify the |
| 940 | * rectangle |
| 941 | */ |
Guennadi Liakhovetski | 961801b | 2009-08-25 11:46:54 -0300 | [diff] [blame^] | 942 | if ((ret < 0 && (is_smaller(&icd->rect_current, rect) || |
| 943 | is_inside(&icd->rect_current, rect))) || |
| 944 | is_smaller(&cam_rect, &target) || is_inside(&cam_rect, &target)) { |
Guennadi Liakhovetski | 904078f | 2009-08-25 11:46:52 -0300 | [diff] [blame] | 945 | /* |
| 946 | * The camera failed to configure a suitable cropping, |
| 947 | * we cannot use the current rectangle, set to max |
| 948 | */ |
Guennadi Liakhovetski | 961801b | 2009-08-25 11:46:54 -0300 | [diff] [blame^] | 949 | cam_rect = cam_max; |
Guennadi Liakhovetski | 904078f | 2009-08-25 11:46:52 -0300 | [diff] [blame] | 950 | ret = icd->ops->set_crop(icd, &cam_rect); |
Guennadi Liakhovetski | 961801b | 2009-08-25 11:46:54 -0300 | [diff] [blame^] | 951 | dev_dbg(&icd->dev, "Camera S_CROP %d for max %ux%u@%u:%u\n", |
Guennadi Liakhovetski | 904078f | 2009-08-25 11:46:52 -0300 | [diff] [blame] | 952 | ret, cam_rect.width, cam_rect.height, |
| 953 | cam_rect.left, cam_rect.top); |
| 954 | if (ret < 0) |
| 955 | /* All failed, hopefully resume current capture */ |
| 956 | goto resume_capture; |
Guennadi Liakhovetski | 961801b | 2009-08-25 11:46:54 -0300 | [diff] [blame^] | 957 | |
| 958 | /* Finally, adjust the target rectangle */ |
| 959 | if (target.width > cam_rect.width) |
| 960 | target.width = cam_rect.width; |
| 961 | if (target.height > cam_rect.height) |
| 962 | target.height = cam_rect.height; |
| 963 | if (target.left + target.width > cam_rect.left + cam_rect.width) |
| 964 | target.left = cam_rect.left + cam_rect.width - |
| 965 | target.width; |
| 966 | if (target.top + target.height > cam_rect.top + cam_rect.height) |
| 967 | target.top = cam_rect.top + cam_rect.height - |
| 968 | target.height; |
Guennadi Liakhovetski | 904078f | 2009-08-25 11:46:52 -0300 | [diff] [blame] | 969 | } |
| 970 | |
| 971 | /* We now have a rectangle, larger than requested, let's crop */ |
| 972 | |
| 973 | /* |
| 974 | * We have to preserve camera rectangle between close() / open(), |
| 975 | * because soc-camera core calls .set_fmt() on each first open() with |
| 976 | * last before last close() _user_ rectangle, which can be different |
| 977 | * from camera rectangle. |
| 978 | */ |
Guennadi Liakhovetski | 961801b | 2009-08-25 11:46:54 -0300 | [diff] [blame^] | 979 | dev_dbg(&icd->dev, |
| 980 | "SH S_CROP from %ux%u@%u:%u to %ux%u@%u:%u, scale to %ux%u@%u:%u\n", |
Guennadi Liakhovetski | 904078f | 2009-08-25 11:46:52 -0300 | [diff] [blame] | 981 | cam_rect.width, cam_rect.height, cam_rect.left, cam_rect.top, |
Guennadi Liakhovetski | 961801b | 2009-08-25 11:46:54 -0300 | [diff] [blame^] | 982 | target.width, target.height, target.left, target.top, |
Guennadi Liakhovetski | 904078f | 2009-08-25 11:46:52 -0300 | [diff] [blame] | 983 | rect->width, rect->height, rect->left, rect->top); |
| 984 | |
| 985 | ret = 0; |
| 986 | |
| 987 | ceu_set_rect: |
| 988 | cam->camera_rect = cam_rect; |
| 989 | |
Guennadi Liakhovetski | 961801b | 2009-08-25 11:46:54 -0300 | [diff] [blame^] | 990 | rect->width = size_dst(target.width, hscale); |
| 991 | rect->left = size_dst(target.left, hscale); |
| 992 | rect->height = size_dst(target.height, vscale); |
| 993 | rect->top = size_dst(target.top, vscale); |
| 994 | |
| 995 | sh_mobile_ceu_set_rect(icd, rect); |
| 996 | |
| 997 | resume_capture: |
Guennadi Liakhovetski | 904078f | 2009-08-25 11:46:52 -0300 | [diff] [blame] | 998 | /* Set CAMOR, CAPWR, CFSZR, take care of CDWDR */ |
| 999 | if (pcdev->active) |
| 1000 | capsr |= 1; |
Guennadi Liakhovetski | 904078f | 2009-08-25 11:46:52 -0300 | [diff] [blame] | 1001 | capture_restore(pcdev, capsr); |
| 1002 | |
Guennadi Liakhovetski | 904078f | 2009-08-25 11:46:52 -0300 | [diff] [blame] | 1003 | /* Even if only camera cropping succeeded */ |
| 1004 | return ret; |
Guennadi Liakhovetski | 09e231b | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 1005 | } |
| 1006 | |
Guennadi Liakhovetski | 961801b | 2009-08-25 11:46:54 -0300 | [diff] [blame^] | 1007 | /* Similar to set_crop multistage iterative algorithm */ |
Guennadi Liakhovetski | d8fac21 | 2008-12-01 09:45:21 -0300 | [diff] [blame] | 1008 | static int sh_mobile_ceu_set_fmt(struct soc_camera_device *icd, |
Guennadi Liakhovetski | 09e231b | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 1009 | struct v4l2_format *f) |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 1010 | { |
Magnus Damm | 9414de3 | 2008-12-18 11:49:06 -0300 | [diff] [blame] | 1011 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
Guennadi Liakhovetski | 961801b | 2009-08-25 11:46:54 -0300 | [diff] [blame^] | 1012 | struct sh_mobile_ceu_dev *pcdev = ici->priv; |
Guennadi Liakhovetski | 904078f | 2009-08-25 11:46:52 -0300 | [diff] [blame] | 1013 | struct sh_mobile_ceu_cam *cam = icd->host_priv; |
| 1014 | struct v4l2_pix_format *pix = &f->fmt.pix; |
| 1015 | __u32 pixfmt = pix->pixelformat; |
Magnus Damm | 9414de3 | 2008-12-18 11:49:06 -0300 | [diff] [blame] | 1016 | const struct soc_camera_format_xlate *xlate; |
Guennadi Liakhovetski | 961801b | 2009-08-25 11:46:54 -0300 | [diff] [blame^] | 1017 | unsigned int width = pix->width, height = pix->height, tmp_w, tmp_h; |
| 1018 | u16 vscale, hscale; |
| 1019 | int ret, is_interlaced; |
| 1020 | |
| 1021 | switch (pix->field) { |
| 1022 | case V4L2_FIELD_INTERLACED: |
| 1023 | is_interlaced = 1; |
| 1024 | break; |
| 1025 | case V4L2_FIELD_ANY: |
| 1026 | default: |
| 1027 | pix->field = V4L2_FIELD_NONE; |
| 1028 | /* fall-through */ |
| 1029 | case V4L2_FIELD_NONE: |
| 1030 | is_interlaced = 0; |
| 1031 | break; |
| 1032 | } |
Guennadi Liakhovetski | 25c4d74 | 2008-12-01 09:44:59 -0300 | [diff] [blame] | 1033 | |
Magnus Damm | 9414de3 | 2008-12-18 11:49:06 -0300 | [diff] [blame] | 1034 | xlate = soc_camera_xlate_by_fourcc(icd, pixfmt); |
| 1035 | if (!xlate) { |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 1036 | dev_warn(ici->v4l2_dev.dev, "Format %x not found\n", pixfmt); |
Magnus Damm | 9414de3 | 2008-12-18 11:49:06 -0300 | [diff] [blame] | 1037 | return -EINVAL; |
Guennadi Liakhovetski | 25c4d74 | 2008-12-01 09:44:59 -0300 | [diff] [blame] | 1038 | } |
| 1039 | |
Guennadi Liakhovetski | 904078f | 2009-08-25 11:46:52 -0300 | [diff] [blame] | 1040 | pix->pixelformat = xlate->cam_fmt->fourcc; |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 1041 | ret = v4l2_device_call_until_err(&ici->v4l2_dev, (__u32)icd, video, s_fmt, f); |
Guennadi Liakhovetski | 904078f | 2009-08-25 11:46:52 -0300 | [diff] [blame] | 1042 | pix->pixelformat = pixfmt; |
Guennadi Liakhovetski | 961801b | 2009-08-25 11:46:54 -0300 | [diff] [blame^] | 1043 | dev_dbg(&icd->dev, "Camera %d fmt %ux%u, requested %ux%u, max %ux%u\n", |
| 1044 | ret, pix->width, pix->height, width, height, |
| 1045 | icd->rect_max.width, icd->rect_max.height); |
| 1046 | if (ret < 0) |
| 1047 | return ret; |
| 1048 | |
| 1049 | switch (pixfmt) { |
| 1050 | case V4L2_PIX_FMT_NV12: |
| 1051 | case V4L2_PIX_FMT_NV21: |
| 1052 | case V4L2_PIX_FMT_NV16: |
| 1053 | case V4L2_PIX_FMT_NV61: |
| 1054 | pcdev->image_mode = 1; |
| 1055 | break; |
| 1056 | default: |
| 1057 | pcdev->image_mode = 0; |
Magnus Damm | 9414de3 | 2008-12-18 11:49:06 -0300 | [diff] [blame] | 1058 | } |
Guennadi Liakhovetski | 25c4d74 | 2008-12-01 09:44:59 -0300 | [diff] [blame] | 1059 | |
Guennadi Liakhovetski | 961801b | 2009-08-25 11:46:54 -0300 | [diff] [blame^] | 1060 | if ((abs(width - pix->width) < 4 && abs(height - pix->height) < 4) || |
| 1061 | !pcdev->image_mode || is_interlaced) { |
| 1062 | hscale = 0; |
| 1063 | vscale = 0; |
| 1064 | goto out; |
| 1065 | } |
| 1066 | |
| 1067 | /* Camera set a format, but geometry is not precise, try to improve */ |
| 1068 | /* |
| 1069 | * FIXME: when soc-camera is converted to implement traditional S_FMT |
| 1070 | * and S_CROP semantics, replace CEU limits with camera maxima |
| 1071 | */ |
| 1072 | tmp_w = pix->width; |
| 1073 | tmp_h = pix->height; |
| 1074 | while ((width > tmp_w || height > tmp_h) && |
| 1075 | tmp_w < 2560 && tmp_h < 1920) { |
| 1076 | tmp_w = min(2 * tmp_w, (__u32)2560); |
| 1077 | tmp_h = min(2 * tmp_h, (__u32)1920); |
| 1078 | pix->width = tmp_w; |
| 1079 | pix->height = tmp_h; |
| 1080 | pix->pixelformat = xlate->cam_fmt->fourcc; |
| 1081 | ret = v4l2_device_call_until_err(&ici->v4l2_dev, (__u32)icd, |
| 1082 | video, s_fmt, f); |
| 1083 | pix->pixelformat = pixfmt; |
| 1084 | dev_dbg(&icd->dev, "Camera scaled to %ux%u\n", |
| 1085 | pix->width, pix->height); |
| 1086 | if (ret < 0) { |
| 1087 | /* This shouldn't happen */ |
| 1088 | dev_err(&icd->dev, "Client failed to set format: %d\n", |
| 1089 | ret); |
| 1090 | return ret; |
| 1091 | } |
| 1092 | } |
| 1093 | |
| 1094 | /* We cannot scale up */ |
| 1095 | if (width > pix->width) |
| 1096 | width = pix->width; |
| 1097 | |
| 1098 | if (height > pix->height) |
| 1099 | height = pix->height; |
| 1100 | |
| 1101 | /* Let's rock: scale pix->{width x height} down to width x height */ |
| 1102 | hscale = calc_scale(pix->width, &width); |
| 1103 | vscale = calc_scale(pix->height, &height); |
| 1104 | |
| 1105 | dev_dbg(&icd->dev, "W: %u : 0x%x = %u, H: %u : 0x%x = %u\n", |
| 1106 | pix->width, hscale, width, pix->height, vscale, height); |
| 1107 | |
| 1108 | out: |
| 1109 | pcdev->cflcr = hscale | (vscale << 16); |
| 1110 | |
| 1111 | icd->buswidth = xlate->buswidth; |
| 1112 | icd->current_fmt = xlate->host_fmt; |
| 1113 | cam->camera_fmt = xlate->cam_fmt; |
| 1114 | cam->camera_rect.width = pix->width; |
| 1115 | cam->camera_rect.height = pix->height; |
| 1116 | |
| 1117 | icd->rect_max.left = size_dst(cam->camera_max.left, hscale); |
| 1118 | icd->rect_max.width = size_dst(cam->camera_max.width, hscale); |
| 1119 | icd->rect_max.top = size_dst(cam->camera_max.top, vscale); |
| 1120 | icd->rect_max.height = size_dst(cam->camera_max.height, vscale); |
| 1121 | |
| 1122 | icd->rect_current.left = icd->rect_max.left; |
| 1123 | icd->rect_current.top = icd->rect_max.top; |
| 1124 | |
| 1125 | pcdev->is_interlaced = is_interlaced; |
| 1126 | |
| 1127 | pix->width = width; |
| 1128 | pix->height = height; |
| 1129 | |
| 1130 | return 0; |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 1131 | } |
| 1132 | |
Guennadi Liakhovetski | d8fac21 | 2008-12-01 09:45:21 -0300 | [diff] [blame] | 1133 | static int sh_mobile_ceu_try_fmt(struct soc_camera_device *icd, |
| 1134 | struct v4l2_format *f) |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 1135 | { |
Magnus Damm | 9414de3 | 2008-12-18 11:49:06 -0300 | [diff] [blame] | 1136 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
| 1137 | const struct soc_camera_format_xlate *xlate; |
Guennadi Liakhovetski | 904078f | 2009-08-25 11:46:52 -0300 | [diff] [blame] | 1138 | struct v4l2_pix_format *pix = &f->fmt.pix; |
| 1139 | __u32 pixfmt = pix->pixelformat; |
Guennadi Liakhovetski | 961801b | 2009-08-25 11:46:54 -0300 | [diff] [blame^] | 1140 | int width, height; |
Kuninori Morimoto | ccab8a2 | 2008-12-18 12:51:21 -0300 | [diff] [blame] | 1141 | int ret; |
Guennadi Liakhovetski | a2c8c68 | 2008-12-01 09:44:53 -0300 | [diff] [blame] | 1142 | |
Magnus Damm | 9414de3 | 2008-12-18 11:49:06 -0300 | [diff] [blame] | 1143 | xlate = soc_camera_xlate_by_fourcc(icd, pixfmt); |
| 1144 | if (!xlate) { |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 1145 | dev_warn(ici->v4l2_dev.dev, "Format %x not found\n", pixfmt); |
Guennadi Liakhovetski | 25c4d74 | 2008-12-01 09:44:59 -0300 | [diff] [blame] | 1146 | return -EINVAL; |
Magnus Damm | 9414de3 | 2008-12-18 11:49:06 -0300 | [diff] [blame] | 1147 | } |
Guennadi Liakhovetski | 25c4d74 | 2008-12-01 09:44:59 -0300 | [diff] [blame] | 1148 | |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 1149 | /* FIXME: calculate using depth and bus width */ |
| 1150 | |
Guennadi Liakhovetski | 904078f | 2009-08-25 11:46:52 -0300 | [diff] [blame] | 1151 | v4l_bound_align_image(&pix->width, 2, 2560, 1, |
| 1152 | &pix->height, 4, 1920, 2, 0); |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 1153 | |
Guennadi Liakhovetski | 961801b | 2009-08-25 11:46:54 -0300 | [diff] [blame^] | 1154 | width = pix->width; |
| 1155 | height = pix->height; |
| 1156 | |
Guennadi Liakhovetski | 904078f | 2009-08-25 11:46:52 -0300 | [diff] [blame] | 1157 | pix->bytesperline = pix->width * |
Magnus Damm | 9414de3 | 2008-12-18 11:49:06 -0300 | [diff] [blame] | 1158 | DIV_ROUND_UP(xlate->host_fmt->depth, 8); |
Guennadi Liakhovetski | 904078f | 2009-08-25 11:46:52 -0300 | [diff] [blame] | 1159 | pix->sizeimage = pix->height * pix->bytesperline; |
Guennadi Liakhovetski | 25c4d74 | 2008-12-01 09:44:59 -0300 | [diff] [blame] | 1160 | |
Guennadi Liakhovetski | 904078f | 2009-08-25 11:46:52 -0300 | [diff] [blame] | 1161 | pix->pixelformat = xlate->cam_fmt->fourcc; |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 1162 | |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 1163 | /* limit to sensor capabilities */ |
Guennadi Liakhovetski | 961801b | 2009-08-25 11:46:54 -0300 | [diff] [blame^] | 1164 | ret = v4l2_device_call_until_err(&ici->v4l2_dev, (__u32)icd, video, |
| 1165 | try_fmt, f); |
Guennadi Liakhovetski | 904078f | 2009-08-25 11:46:52 -0300 | [diff] [blame] | 1166 | pix->pixelformat = pixfmt; |
Kuninori Morimoto | ccab8a2 | 2008-12-18 12:51:21 -0300 | [diff] [blame] | 1167 | if (ret < 0) |
| 1168 | return ret; |
| 1169 | |
Guennadi Liakhovetski | 961801b | 2009-08-25 11:46:54 -0300 | [diff] [blame^] | 1170 | switch (pixfmt) { |
| 1171 | case V4L2_PIX_FMT_NV12: |
| 1172 | case V4L2_PIX_FMT_NV21: |
| 1173 | case V4L2_PIX_FMT_NV16: |
| 1174 | case V4L2_PIX_FMT_NV61: |
| 1175 | /* FIXME: check against rect_max after converting soc-camera */ |
| 1176 | /* We can scale precisely, need a bigger image from camera */ |
| 1177 | if (pix->width < width || pix->height < height) { |
| 1178 | int tmp_w = pix->width, tmp_h = pix->height; |
| 1179 | pix->width = 2560; |
| 1180 | pix->height = 1920; |
| 1181 | ret = v4l2_device_call_until_err(&ici->v4l2_dev, |
| 1182 | (__u32)icd, video, |
| 1183 | try_fmt, f); |
| 1184 | if (ret < 0) { |
| 1185 | /* Shouldn't actually happen... */ |
| 1186 | dev_err(&icd->dev, |
| 1187 | "FIXME: try_fmt() returned %d\n", ret); |
| 1188 | pix->width = tmp_w; |
| 1189 | pix->height = tmp_h; |
| 1190 | } |
| 1191 | } |
| 1192 | if (pix->width > width) |
| 1193 | pix->width = width; |
| 1194 | if (pix->height > height) |
| 1195 | pix->height = height; |
Kuninori Morimoto | ccab8a2 | 2008-12-18 12:51:21 -0300 | [diff] [blame] | 1196 | } |
| 1197 | |
| 1198 | return ret; |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 1199 | } |
| 1200 | |
| 1201 | static int sh_mobile_ceu_reqbufs(struct soc_camera_file *icf, |
| 1202 | struct v4l2_requestbuffers *p) |
| 1203 | { |
| 1204 | int i; |
| 1205 | |
| 1206 | /* This is for locking debugging only. I removed spinlocks and now I |
| 1207 | * check whether .prepare is ever called on a linked buffer, or whether |
| 1208 | * a dma IRQ can occur for an in-work or unlinked buffer. Until now |
| 1209 | * it hadn't triggered */ |
| 1210 | for (i = 0; i < p->count; i++) { |
| 1211 | struct sh_mobile_ceu_buffer *buf; |
| 1212 | |
| 1213 | buf = container_of(icf->vb_vidq.bufs[i], |
| 1214 | struct sh_mobile_ceu_buffer, vb); |
| 1215 | INIT_LIST_HEAD(&buf->vb.queue); |
| 1216 | } |
| 1217 | |
| 1218 | return 0; |
| 1219 | } |
| 1220 | |
| 1221 | static unsigned int sh_mobile_ceu_poll(struct file *file, poll_table *pt) |
| 1222 | { |
| 1223 | struct soc_camera_file *icf = file->private_data; |
| 1224 | struct sh_mobile_ceu_buffer *buf; |
| 1225 | |
| 1226 | buf = list_entry(icf->vb_vidq.stream.next, |
| 1227 | struct sh_mobile_ceu_buffer, vb.stream); |
| 1228 | |
| 1229 | poll_wait(file, &buf->vb.done, pt); |
| 1230 | |
| 1231 | if (buf->vb.state == VIDEOBUF_DONE || |
| 1232 | buf->vb.state == VIDEOBUF_ERROR) |
| 1233 | return POLLIN|POLLRDNORM; |
| 1234 | |
| 1235 | return 0; |
| 1236 | } |
| 1237 | |
| 1238 | static int sh_mobile_ceu_querycap(struct soc_camera_host *ici, |
| 1239 | struct v4l2_capability *cap) |
| 1240 | { |
| 1241 | strlcpy(cap->card, "SuperH_Mobile_CEU", sizeof(cap->card)); |
| 1242 | cap->version = KERNEL_VERSION(0, 0, 5); |
| 1243 | cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING; |
| 1244 | return 0; |
| 1245 | } |
| 1246 | |
| 1247 | static void sh_mobile_ceu_init_videobuf(struct videobuf_queue *q, |
| 1248 | struct soc_camera_device *icd) |
| 1249 | { |
| 1250 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
| 1251 | struct sh_mobile_ceu_dev *pcdev = ici->priv; |
| 1252 | |
| 1253 | videobuf_queue_dma_contig_init(q, |
| 1254 | &sh_mobile_ceu_videobuf_ops, |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 1255 | ici->v4l2_dev.dev, &pcdev->lock, |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 1256 | V4L2_BUF_TYPE_VIDEO_CAPTURE, |
Guennadi Liakhovetski | e802967 | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 1257 | pcdev->is_interlaced ? |
| 1258 | V4L2_FIELD_INTERLACED : V4L2_FIELD_NONE, |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 1259 | sizeof(struct sh_mobile_ceu_buffer), |
| 1260 | icd); |
| 1261 | } |
| 1262 | |
Guennadi Liakhovetski | 4a6110b | 2009-08-25 11:44:15 -0300 | [diff] [blame] | 1263 | static int sh_mobile_ceu_get_ctrl(struct soc_camera_device *icd, |
| 1264 | struct v4l2_control *ctrl) |
| 1265 | { |
| 1266 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
| 1267 | struct sh_mobile_ceu_dev *pcdev = ici->priv; |
| 1268 | u32 val; |
| 1269 | |
| 1270 | switch (ctrl->id) { |
| 1271 | case V4L2_CID_SHARPNESS: |
| 1272 | val = ceu_read(pcdev, CLFCR); |
| 1273 | ctrl->value = val ^ 1; |
| 1274 | return 0; |
| 1275 | } |
| 1276 | return -ENOIOCTLCMD; |
| 1277 | } |
| 1278 | |
| 1279 | static int sh_mobile_ceu_set_ctrl(struct soc_camera_device *icd, |
| 1280 | struct v4l2_control *ctrl) |
| 1281 | { |
| 1282 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
| 1283 | struct sh_mobile_ceu_dev *pcdev = ici->priv; |
| 1284 | |
| 1285 | switch (ctrl->id) { |
| 1286 | case V4L2_CID_SHARPNESS: |
| 1287 | switch (icd->current_fmt->fourcc) { |
| 1288 | case V4L2_PIX_FMT_NV12: |
| 1289 | case V4L2_PIX_FMT_NV21: |
| 1290 | case V4L2_PIX_FMT_NV16: |
| 1291 | case V4L2_PIX_FMT_NV61: |
| 1292 | ceu_write(pcdev, CLFCR, !ctrl->value); |
| 1293 | return 0; |
| 1294 | } |
| 1295 | return -EINVAL; |
| 1296 | } |
| 1297 | return -ENOIOCTLCMD; |
| 1298 | } |
| 1299 | |
| 1300 | static const struct v4l2_queryctrl sh_mobile_ceu_controls[] = { |
| 1301 | { |
| 1302 | .id = V4L2_CID_SHARPNESS, |
| 1303 | .type = V4L2_CTRL_TYPE_BOOLEAN, |
| 1304 | .name = "Low-pass filter", |
| 1305 | .minimum = 0, |
| 1306 | .maximum = 1, |
| 1307 | .step = 1, |
| 1308 | .default_value = 0, |
| 1309 | }, |
| 1310 | }; |
| 1311 | |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 1312 | static struct soc_camera_host_ops sh_mobile_ceu_host_ops = { |
| 1313 | .owner = THIS_MODULE, |
| 1314 | .add = sh_mobile_ceu_add_device, |
| 1315 | .remove = sh_mobile_ceu_remove_device, |
Magnus Damm | 9414de3 | 2008-12-18 11:49:06 -0300 | [diff] [blame] | 1316 | .get_formats = sh_mobile_ceu_get_formats, |
Guennadi Liakhovetski | 904078f | 2009-08-25 11:46:52 -0300 | [diff] [blame] | 1317 | .put_formats = sh_mobile_ceu_put_formats, |
Guennadi Liakhovetski | 09e231b | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 1318 | .set_crop = sh_mobile_ceu_set_crop, |
Guennadi Liakhovetski | d8fac21 | 2008-12-01 09:45:21 -0300 | [diff] [blame] | 1319 | .set_fmt = sh_mobile_ceu_set_fmt, |
| 1320 | .try_fmt = sh_mobile_ceu_try_fmt, |
Guennadi Liakhovetski | 4a6110b | 2009-08-25 11:44:15 -0300 | [diff] [blame] | 1321 | .set_ctrl = sh_mobile_ceu_set_ctrl, |
| 1322 | .get_ctrl = sh_mobile_ceu_get_ctrl, |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 1323 | .reqbufs = sh_mobile_ceu_reqbufs, |
| 1324 | .poll = sh_mobile_ceu_poll, |
| 1325 | .querycap = sh_mobile_ceu_querycap, |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 1326 | .set_bus_param = sh_mobile_ceu_set_bus_param, |
| 1327 | .init_videobuf = sh_mobile_ceu_init_videobuf, |
Guennadi Liakhovetski | 4a6110b | 2009-08-25 11:44:15 -0300 | [diff] [blame] | 1328 | .controls = sh_mobile_ceu_controls, |
| 1329 | .num_controls = ARRAY_SIZE(sh_mobile_ceu_controls), |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 1330 | }; |
| 1331 | |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 1332 | static int __devinit sh_mobile_ceu_probe(struct platform_device *pdev) |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 1333 | { |
| 1334 | struct sh_mobile_ceu_dev *pcdev; |
| 1335 | struct resource *res; |
| 1336 | void __iomem *base; |
| 1337 | unsigned int irq; |
| 1338 | int err = 0; |
| 1339 | |
| 1340 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 1341 | irq = platform_get_irq(pdev, 0); |
| 1342 | if (!res || !irq) { |
| 1343 | dev_err(&pdev->dev, "Not enough CEU platform resources.\n"); |
| 1344 | err = -ENODEV; |
| 1345 | goto exit; |
| 1346 | } |
| 1347 | |
| 1348 | pcdev = kzalloc(sizeof(*pcdev), GFP_KERNEL); |
| 1349 | if (!pcdev) { |
| 1350 | dev_err(&pdev->dev, "Could not allocate pcdev\n"); |
| 1351 | err = -ENOMEM; |
| 1352 | goto exit; |
| 1353 | } |
| 1354 | |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 1355 | INIT_LIST_HEAD(&pcdev->capture); |
| 1356 | spin_lock_init(&pcdev->lock); |
| 1357 | |
| 1358 | pcdev->pdata = pdev->dev.platform_data; |
| 1359 | if (!pcdev->pdata) { |
| 1360 | err = -EINVAL; |
| 1361 | dev_err(&pdev->dev, "CEU platform data not set.\n"); |
| 1362 | goto exit_kfree; |
| 1363 | } |
| 1364 | |
Guennadi Liakhovetski | eb6c855 | 2009-04-24 12:55:18 -0300 | [diff] [blame] | 1365 | base = ioremap_nocache(res->start, resource_size(res)); |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 1366 | if (!base) { |
| 1367 | err = -ENXIO; |
| 1368 | dev_err(&pdev->dev, "Unable to ioremap CEU registers.\n"); |
| 1369 | goto exit_kfree; |
| 1370 | } |
| 1371 | |
| 1372 | pcdev->irq = irq; |
| 1373 | pcdev->base = base; |
| 1374 | pcdev->video_limit = 0; /* only enabled if second resource exists */ |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 1375 | |
| 1376 | res = platform_get_resource(pdev, IORESOURCE_MEM, 1); |
| 1377 | if (res) { |
| 1378 | err = dma_declare_coherent_memory(&pdev->dev, res->start, |
| 1379 | res->start, |
Guennadi Liakhovetski | eb6c855 | 2009-04-24 12:55:18 -0300 | [diff] [blame] | 1380 | resource_size(res), |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 1381 | DMA_MEMORY_MAP | |
| 1382 | DMA_MEMORY_EXCLUSIVE); |
| 1383 | if (!err) { |
| 1384 | dev_err(&pdev->dev, "Unable to declare CEU memory.\n"); |
| 1385 | err = -ENXIO; |
| 1386 | goto exit_iounmap; |
| 1387 | } |
| 1388 | |
Guennadi Liakhovetski | eb6c855 | 2009-04-24 12:55:18 -0300 | [diff] [blame] | 1389 | pcdev->video_limit = resource_size(res); |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 1390 | } |
| 1391 | |
| 1392 | /* request irq */ |
| 1393 | err = request_irq(pcdev->irq, sh_mobile_ceu_irq, IRQF_DISABLED, |
Kay Sievers | af128a1 | 2008-10-30 00:51:46 -0300 | [diff] [blame] | 1394 | dev_name(&pdev->dev), pcdev); |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 1395 | if (err) { |
| 1396 | dev_err(&pdev->dev, "Unable to register CEU interrupt.\n"); |
| 1397 | goto exit_release_mem; |
| 1398 | } |
| 1399 | |
Magnus Damm | 6d1386c | 2009-08-14 10:49:17 +0000 | [diff] [blame] | 1400 | pm_suspend_ignore_children(&pdev->dev, true); |
| 1401 | pm_runtime_enable(&pdev->dev); |
| 1402 | pm_runtime_resume(&pdev->dev); |
Magnus Damm | a42b6dd | 2008-10-31 20:21:44 +0900 | [diff] [blame] | 1403 | |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 1404 | pcdev->ici.priv = pcdev; |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 1405 | pcdev->ici.v4l2_dev.dev = &pdev->dev; |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 1406 | pcdev->ici.nr = pdev->id; |
Kay Sievers | af128a1 | 2008-10-30 00:51:46 -0300 | [diff] [blame] | 1407 | pcdev->ici.drv_name = dev_name(&pdev->dev); |
| 1408 | pcdev->ici.ops = &sh_mobile_ceu_host_ops; |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 1409 | |
| 1410 | err = soc_camera_host_register(&pcdev->ici); |
| 1411 | if (err) |
Magnus Damm | 6d1386c | 2009-08-14 10:49:17 +0000 | [diff] [blame] | 1412 | goto exit_free_irq; |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 1413 | |
| 1414 | return 0; |
| 1415 | |
| 1416 | exit_free_irq: |
| 1417 | free_irq(pcdev->irq, pcdev); |
| 1418 | exit_release_mem: |
| 1419 | if (platform_get_resource(pdev, IORESOURCE_MEM, 1)) |
| 1420 | dma_release_declared_memory(&pdev->dev); |
| 1421 | exit_iounmap: |
| 1422 | iounmap(base); |
| 1423 | exit_kfree: |
| 1424 | kfree(pcdev); |
| 1425 | exit: |
| 1426 | return err; |
| 1427 | } |
| 1428 | |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 1429 | static int __devexit sh_mobile_ceu_remove(struct platform_device *pdev) |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 1430 | { |
Guennadi Liakhovetski | eff505f | 2009-04-24 12:55:48 -0300 | [diff] [blame] | 1431 | struct soc_camera_host *soc_host = to_soc_camera_host(&pdev->dev); |
| 1432 | struct sh_mobile_ceu_dev *pcdev = container_of(soc_host, |
| 1433 | struct sh_mobile_ceu_dev, ici); |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 1434 | |
Guennadi Liakhovetski | eff505f | 2009-04-24 12:55:48 -0300 | [diff] [blame] | 1435 | soc_camera_host_unregister(soc_host); |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 1436 | free_irq(pcdev->irq, pcdev); |
| 1437 | if (platform_get_resource(pdev, IORESOURCE_MEM, 1)) |
| 1438 | dma_release_declared_memory(&pdev->dev); |
| 1439 | iounmap(pcdev->base); |
| 1440 | kfree(pcdev); |
| 1441 | return 0; |
| 1442 | } |
| 1443 | |
Magnus Damm | 6d1386c | 2009-08-14 10:49:17 +0000 | [diff] [blame] | 1444 | static int sh_mobile_ceu_runtime_nop(struct device *dev) |
| 1445 | { |
| 1446 | /* Runtime PM callback shared between ->runtime_suspend() |
| 1447 | * and ->runtime_resume(). Simply returns success. |
| 1448 | * |
| 1449 | * This driver re-initializes all registers after |
| 1450 | * pm_runtime_get_sync() anyway so there is no need |
| 1451 | * to save and restore registers here. |
| 1452 | */ |
| 1453 | return 0; |
| 1454 | } |
| 1455 | |
| 1456 | static struct dev_pm_ops sh_mobile_ceu_dev_pm_ops = { |
| 1457 | .runtime_suspend = sh_mobile_ceu_runtime_nop, |
| 1458 | .runtime_resume = sh_mobile_ceu_runtime_nop, |
| 1459 | }; |
| 1460 | |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 1461 | static struct platform_driver sh_mobile_ceu_driver = { |
| 1462 | .driver = { |
| 1463 | .name = "sh_mobile_ceu", |
Magnus Damm | 6d1386c | 2009-08-14 10:49:17 +0000 | [diff] [blame] | 1464 | .pm = &sh_mobile_ceu_dev_pm_ops, |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 1465 | }, |
| 1466 | .probe = sh_mobile_ceu_probe, |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 1467 | .remove = __exit_p(sh_mobile_ceu_remove), |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 1468 | }; |
| 1469 | |
| 1470 | static int __init sh_mobile_ceu_init(void) |
| 1471 | { |
| 1472 | return platform_driver_register(&sh_mobile_ceu_driver); |
| 1473 | } |
| 1474 | |
| 1475 | static void __exit sh_mobile_ceu_exit(void) |
| 1476 | { |
Paul Mundt | 01c1e4c | 2008-08-01 19:48:51 -0300 | [diff] [blame] | 1477 | platform_driver_unregister(&sh_mobile_ceu_driver); |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 1478 | } |
| 1479 | |
| 1480 | module_init(sh_mobile_ceu_init); |
| 1481 | module_exit(sh_mobile_ceu_exit); |
| 1482 | |
| 1483 | MODULE_DESCRIPTION("SuperH Mobile CEU driver"); |
| 1484 | MODULE_AUTHOR("Magnus Damm"); |
| 1485 | MODULE_LICENSE("GPL"); |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 1486 | MODULE_ALIAS("platform:sh_mobile_ceu"); |