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