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