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