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> |
Guennadi Liakhovetski | 3dac322 | 2011-03-28 13:39:16 -0300 | [diff] [blame] | 20 | #include <linux/completion.h> |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 21 | #include <linux/delay.h> |
| 22 | #include <linux/dma-mapping.h> |
| 23 | #include <linux/errno.h> |
| 24 | #include <linux/fs.h> |
| 25 | #include <linux/interrupt.h> |
| 26 | #include <linux/kernel.h> |
| 27 | #include <linux/mm.h> |
| 28 | #include <linux/moduleparam.h> |
| 29 | #include <linux/time.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> |
Guennadi Liakhovetski | 6b526fe | 2011-07-01 11:19:58 -0300 | [diff] [blame] | 41 | #include <media/sh_mobile_csi2.h> |
Guennadi Liakhovetski | a616898 | 2011-02-18 05:30:15 -0300 | [diff] [blame] | 42 | #include <media/videobuf2-dma-contig.h> |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 43 | #include <media/v4l2-mediabus.h> |
| 44 | #include <media/soc_mediabus.h> |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 45 | |
| 46 | /* register offsets for sh7722 / sh7723 */ |
| 47 | |
Magnus Damm | dd54203 | 2008-10-16 19:50:22 -0300 | [diff] [blame] | 48 | #define CAPSR 0x00 /* Capture start register */ |
| 49 | #define CAPCR 0x04 /* Capture control register */ |
| 50 | #define CAMCR 0x08 /* Capture interface control register */ |
| 51 | #define CMCYR 0x0c /* Capture interface cycle register */ |
| 52 | #define CAMOR 0x10 /* Capture interface offset register */ |
| 53 | #define CAPWR 0x14 /* Capture interface width register */ |
| 54 | #define CAIFR 0x18 /* Capture interface input format register */ |
| 55 | #define CSTCR 0x20 /* Camera strobe control register (<= sh7722) */ |
| 56 | #define CSECR 0x24 /* Camera strobe emission count register (<= sh7722) */ |
| 57 | #define CRCNTR 0x28 /* CEU register control register */ |
| 58 | #define CRCMPR 0x2c /* CEU register forcible control register */ |
| 59 | #define CFLCR 0x30 /* Capture filter control register */ |
| 60 | #define CFSZR 0x34 /* Capture filter size clip register */ |
| 61 | #define CDWDR 0x38 /* Capture destination width register */ |
| 62 | #define CDAYR 0x3c /* Capture data address Y register */ |
| 63 | #define CDACR 0x40 /* Capture data address C register */ |
| 64 | #define CDBYR 0x44 /* Capture data bottom-field address Y register */ |
| 65 | #define CDBCR 0x48 /* Capture data bottom-field address C register */ |
| 66 | #define CBDSR 0x4c /* Capture bundle destination size register */ |
| 67 | #define CFWCR 0x5c /* Firewall operation control register */ |
| 68 | #define CLFCR 0x60 /* Capture low-pass filter control register */ |
| 69 | #define CDOCR 0x64 /* Capture data output control register */ |
| 70 | #define CDDCR 0x68 /* Capture data complexity level register */ |
| 71 | #define CDDAR 0x6c /* Capture data complexity level address register */ |
| 72 | #define CEIER 0x70 /* Capture event interrupt enable register */ |
| 73 | #define CETCR 0x74 /* Capture event flag clear register */ |
| 74 | #define CSTSR 0x7c /* Capture status register */ |
| 75 | #define CSRTR 0x80 /* Capture software reset register */ |
| 76 | #define CDSSR 0x84 /* Capture data size register */ |
| 77 | #define CDAYR2 0x90 /* Capture data address Y register 2 */ |
| 78 | #define CDACR2 0x94 /* Capture data address C register 2 */ |
| 79 | #define CDBYR2 0x98 /* Capture data bottom-field address Y register 2 */ |
| 80 | #define CDBCR2 0x9c /* Capture data bottom-field address C register 2 */ |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 81 | |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 82 | #undef DEBUG_GEOMETRY |
| 83 | #ifdef DEBUG_GEOMETRY |
| 84 | #define dev_geo dev_info |
| 85 | #else |
| 86 | #define dev_geo dev_dbg |
| 87 | #endif |
| 88 | |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 89 | /* per video frame buffer */ |
| 90 | struct sh_mobile_ceu_buffer { |
Guennadi Liakhovetski | a616898 | 2011-02-18 05:30:15 -0300 | [diff] [blame] | 91 | struct vb2_buffer vb; /* v4l buffer must be first */ |
| 92 | struct list_head queue; |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 93 | }; |
| 94 | |
| 95 | struct sh_mobile_ceu_dev { |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 96 | struct soc_camera_host ici; |
| 97 | struct soc_camera_device *icd; |
Guennadi Liakhovetski | 6b526fe | 2011-07-01 11:19:58 -0300 | [diff] [blame] | 98 | struct platform_device *csi2_pdev; |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 99 | |
| 100 | unsigned int irq; |
| 101 | void __iomem *base; |
Guennadi Liakhovetski | b5518a4 | 2011-11-03 10:11:11 -0300 | [diff] [blame] | 102 | size_t video_limit; |
| 103 | size_t buf_total; |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 104 | |
Guennadi Liakhovetski | a616898 | 2011-02-18 05:30:15 -0300 | [diff] [blame] | 105 | spinlock_t lock; /* Protects video buffer lists */ |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 106 | struct list_head capture; |
Guennadi Liakhovetski | a616898 | 2011-02-18 05:30:15 -0300 | [diff] [blame] | 107 | struct vb2_buffer *active; |
| 108 | struct vb2_alloc_ctx *alloc_ctx; |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 109 | |
| 110 | struct sh_mobile_ceu_info *pdata; |
Guennadi Liakhovetski | 3dac322 | 2011-03-28 13:39:16 -0300 | [diff] [blame] | 111 | struct completion complete; |
Magnus Damm | 8be65dc | 2008-12-18 12:38:06 -0300 | [diff] [blame] | 112 | |
Guennadi Liakhovetski | 961801b | 2009-08-25 11:46:54 -0300 | [diff] [blame] | 113 | u32 cflcr; |
| 114 | |
Guennadi Liakhovetski | 48e971c | 2012-03-14 08:37:03 -0300 | [diff] [blame] | 115 | /* static max sizes either from platform data or default */ |
| 116 | int max_width; |
| 117 | int max_height; |
| 118 | |
Kuninori Morimoto | 1edcc10 | 2009-12-11 11:53:53 -0300 | [diff] [blame] | 119 | enum v4l2_field field; |
Guennadi Liakhovetski | a616898 | 2011-02-18 05:30:15 -0300 | [diff] [blame] | 120 | int sequence; |
Kuninori Morimoto | 1edcc10 | 2009-12-11 11:53:53 -0300 | [diff] [blame] | 121 | |
Guennadi Liakhovetski | 904078f | 2009-08-25 11:46:52 -0300 | [diff] [blame] | 122 | unsigned int image_mode:1; |
| 123 | unsigned int is_16bit:1; |
Guennadi Liakhovetski | 3dac322 | 2011-03-28 13:39:16 -0300 | [diff] [blame] | 124 | unsigned int frozen:1; |
Guennadi Liakhovetski | 904078f | 2009-08-25 11:46:52 -0300 | [diff] [blame] | 125 | }; |
| 126 | |
| 127 | struct sh_mobile_ceu_cam { |
Guennadi Liakhovetski | eec5ce0 | 2011-07-22 07:43:15 -0300 | [diff] [blame] | 128 | /* CEU offsets within the camera output, before the CEU scaler */ |
Guennadi Liakhovetski | e622681 | 2010-03-23 11:23:31 -0300 | [diff] [blame] | 129 | unsigned int ceu_left; |
| 130 | unsigned int ceu_top; |
| 131 | /* Client output, as seen by the CEU */ |
| 132 | unsigned int width; |
| 133 | unsigned int height; |
| 134 | /* |
| 135 | * User window from S_CROP / G_CROP, produced by client cropping and |
| 136 | * scaling, CEU scaling and CEU cropping, mapped back onto the client |
| 137 | * input window |
| 138 | */ |
| 139 | struct v4l2_rect subrect; |
| 140 | /* Camera cropping rectangle */ |
| 141 | struct v4l2_rect rect; |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 142 | const struct soc_mbus_pixelfmt *extra_fmt; |
| 143 | enum v4l2_mbus_pixelcode code; |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 144 | }; |
| 145 | |
Guennadi Liakhovetski | a616898 | 2011-02-18 05:30:15 -0300 | [diff] [blame] | 146 | static struct sh_mobile_ceu_buffer *to_ceu_vb(struct vb2_buffer *vb) |
| 147 | { |
| 148 | return container_of(vb, struct sh_mobile_ceu_buffer, vb); |
| 149 | } |
| 150 | |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 151 | static void ceu_write(struct sh_mobile_ceu_dev *priv, |
Magnus Damm | 7a1a816 | 2008-12-18 12:50:30 -0300 | [diff] [blame] | 152 | unsigned long reg_offs, u32 data) |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 153 | { |
| 154 | iowrite32(data, priv->base + reg_offs); |
| 155 | } |
| 156 | |
Magnus Damm | 7a1a816 | 2008-12-18 12:50:30 -0300 | [diff] [blame] | 157 | 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] | 158 | { |
| 159 | return ioread32(priv->base + reg_offs); |
| 160 | } |
| 161 | |
Kuninori Morimoto | b1de7ae | 2009-10-05 12:48:20 -0300 | [diff] [blame] | 162 | static int sh_mobile_ceu_soft_reset(struct sh_mobile_ceu_dev *pcdev) |
| 163 | { |
| 164 | int i, success = 0; |
| 165 | struct soc_camera_device *icd = pcdev->icd; |
| 166 | |
| 167 | ceu_write(pcdev, CAPSR, 1 << 16); /* reset */ |
| 168 | |
| 169 | /* wait CSTSR.CPTON bit */ |
| 170 | for (i = 0; i < 1000; i++) { |
| 171 | if (!(ceu_read(pcdev, CSTSR) & 1)) { |
| 172 | success++; |
| 173 | break; |
| 174 | } |
| 175 | udelay(1); |
| 176 | } |
| 177 | |
| 178 | /* wait CAPSR.CPKIL bit */ |
| 179 | for (i = 0; i < 1000; i++) { |
| 180 | if (!(ceu_read(pcdev, CAPSR) & (1 << 16))) { |
| 181 | success++; |
| 182 | break; |
| 183 | } |
| 184 | udelay(1); |
| 185 | } |
| 186 | |
| 187 | |
| 188 | if (2 != success) { |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 189 | dev_warn(icd->pdev, "soft reset time out\n"); |
Kuninori Morimoto | b1de7ae | 2009-10-05 12:48:20 -0300 | [diff] [blame] | 190 | return -EIO; |
| 191 | } |
| 192 | |
| 193 | return 0; |
| 194 | } |
| 195 | |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 196 | /* |
| 197 | * Videobuf operations |
| 198 | */ |
Guennadi Liakhovetski | b5518a4 | 2011-11-03 10:11:11 -0300 | [diff] [blame] | 199 | |
| 200 | /* |
| 201 | * .queue_setup() is called to check, whether the driver can accept the |
| 202 | * requested number of buffers and to fill in plane sizes |
| 203 | * for the current frame format if required |
| 204 | */ |
Guennadi Liakhovetski | a616898 | 2011-02-18 05:30:15 -0300 | [diff] [blame] | 205 | static int sh_mobile_ceu_videobuf_setup(struct vb2_queue *vq, |
Guennadi Liakhovetski | fc714e7 | 2011-08-24 10:30:21 -0300 | [diff] [blame] | 206 | const struct v4l2_format *fmt, |
Guennadi Liakhovetski | a616898 | 2011-02-18 05:30:15 -0300 | [diff] [blame] | 207 | unsigned int *count, unsigned int *num_planes, |
Marek Szyprowski | 035aa14 | 2011-08-24 06:43:36 -0300 | [diff] [blame] | 208 | unsigned int sizes[], void *alloc_ctxs[]) |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 209 | { |
Guennadi Liakhovetski | a616898 | 2011-02-18 05:30:15 -0300 | [diff] [blame] | 210 | struct soc_camera_device *icd = container_of(vq, struct soc_camera_device, vb2_vidq); |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 211 | struct soc_camera_host *ici = to_soc_camera_host(icd->parent); |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 212 | struct sh_mobile_ceu_dev *pcdev = ici->priv; |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 213 | |
Guennadi Liakhovetski | b5518a4 | 2011-11-03 10:11:11 -0300 | [diff] [blame] | 214 | if (fmt) { |
| 215 | const struct soc_camera_format_xlate *xlate = soc_camera_xlate_by_fourcc(icd, |
| 216 | fmt->fmt.pix.pixelformat); |
Laurent Pinchart | bed8d80 | 2012-03-21 08:03:26 -0300 | [diff] [blame^] | 217 | unsigned int bytes_per_line; |
| 218 | int ret; |
Laurent Pinchart | 2b61d46e | 2012-03-21 08:03:21 -0300 | [diff] [blame] | 219 | |
Guennadi Liakhovetski | b5518a4 | 2011-11-03 10:11:11 -0300 | [diff] [blame] | 220 | if (!xlate) |
| 221 | return -EINVAL; |
Laurent Pinchart | 2b61d46e | 2012-03-21 08:03:21 -0300 | [diff] [blame] | 222 | |
Laurent Pinchart | bed8d80 | 2012-03-21 08:03:26 -0300 | [diff] [blame^] | 223 | ret = soc_mbus_bytes_per_line(fmt->fmt.pix.width, |
| 224 | xlate->host_fmt); |
| 225 | if (ret < 0) |
| 226 | return ret; |
Laurent Pinchart | 2b61d46e | 2012-03-21 08:03:21 -0300 | [diff] [blame] | 227 | |
Laurent Pinchart | bed8d80 | 2012-03-21 08:03:26 -0300 | [diff] [blame^] | 228 | bytes_per_line = max_t(u32, fmt->fmt.pix.bytesperline, ret); |
| 229 | |
| 230 | ret = soc_mbus_image_size(xlate->host_fmt, bytes_per_line, |
| 231 | fmt->fmt.pix.height); |
| 232 | if (ret < 0) |
| 233 | return ret; |
| 234 | |
| 235 | sizes[0] = max_t(u32, fmt->fmt.pix.sizeimage, ret); |
Guennadi Liakhovetski | b5518a4 | 2011-11-03 10:11:11 -0300 | [diff] [blame] | 236 | } else { |
| 237 | /* Called from VIDIOC_REQBUFS or in compatibility mode */ |
Laurent Pinchart | 2b61d46e | 2012-03-21 08:03:21 -0300 | [diff] [blame] | 238 | sizes[0] = icd->sizeimage; |
Guennadi Liakhovetski | b5518a4 | 2011-11-03 10:11:11 -0300 | [diff] [blame] | 239 | } |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 240 | |
Guennadi Liakhovetski | a616898 | 2011-02-18 05:30:15 -0300 | [diff] [blame] | 241 | alloc_ctxs[0] = pcdev->alloc_ctx; |
| 242 | |
Guennadi Liakhovetski | b5518a4 | 2011-11-03 10:11:11 -0300 | [diff] [blame] | 243 | if (!vq->num_buffers) |
| 244 | pcdev->sequence = 0; |
| 245 | |
Guennadi Liakhovetski | a616898 | 2011-02-18 05:30:15 -0300 | [diff] [blame] | 246 | if (!*count) |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 247 | *count = 2; |
| 248 | |
Guennadi Liakhovetski | b5518a4 | 2011-11-03 10:11:11 -0300 | [diff] [blame] | 249 | /* If *num_planes != 0, we have already verified *count. */ |
| 250 | if (pcdev->video_limit && !*num_planes) { |
| 251 | size_t size = PAGE_ALIGN(sizes[0]) * *count; |
| 252 | |
| 253 | if (size + pcdev->buf_total > pcdev->video_limit) |
| 254 | *count = (pcdev->video_limit - pcdev->buf_total) / |
| 255 | PAGE_ALIGN(sizes[0]); |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 256 | } |
| 257 | |
Guennadi Liakhovetski | b5518a4 | 2011-11-03 10:11:11 -0300 | [diff] [blame] | 258 | *num_planes = 1; |
| 259 | |
Marek Szyprowski | 035aa14 | 2011-08-24 06:43:36 -0300 | [diff] [blame] | 260 | dev_dbg(icd->parent, "count=%d, size=%u\n", *count, sizes[0]); |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 261 | |
| 262 | return 0; |
| 263 | } |
| 264 | |
Magnus Damm | 7a1a816 | 2008-12-18 12:50:30 -0300 | [diff] [blame] | 265 | #define CEU_CETCR_MAGIC 0x0317f313 /* acknowledge magical interrupt sources */ |
| 266 | #define CEU_CETCR_IGRW (1 << 4) /* prohibited register access interrupt bit */ |
| 267 | #define CEU_CEIER_CPEIE (1 << 0) /* one-frame capture end interrupt */ |
Kuninori Morimoto | 5cf93f1 | 2009-10-05 12:48:59 -0300 | [diff] [blame] | 268 | #define CEU_CEIER_VBP (1 << 20) /* vbp error */ |
Magnus Damm | 7a1a816 | 2008-12-18 12:50:30 -0300 | [diff] [blame] | 269 | #define CEU_CAPCR_CTNCP (1 << 16) /* continuous capture mode (if set) */ |
Kuninori Morimoto | 5cf93f1 | 2009-10-05 12:48:59 -0300 | [diff] [blame] | 270 | #define CEU_CEIER_MASK (CEU_CEIER_CPEIE | CEU_CEIER_VBP) |
Magnus Damm | 7a1a816 | 2008-12-18 12:50:30 -0300 | [diff] [blame] | 271 | |
| 272 | |
Kuninori Morimoto | 5cf93f1 | 2009-10-05 12:48:59 -0300 | [diff] [blame] | 273 | /* |
| 274 | * return value doesn't reflex the success/failure to queue the new buffer, |
| 275 | * but rather the status of the previous buffer. |
| 276 | */ |
| 277 | static int sh_mobile_ceu_capture(struct sh_mobile_ceu_dev *pcdev) |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 278 | { |
Magnus Damm | 8be65dc | 2008-12-18 12:38:06 -0300 | [diff] [blame] | 279 | struct soc_camera_device *icd = pcdev->icd; |
Kuninori Morimoto | ccab8a2 | 2008-12-18 12:51:21 -0300 | [diff] [blame] | 280 | dma_addr_t phys_addr_top, phys_addr_bottom; |
Kuninori Morimoto | 1edcc10 | 2009-12-11 11:53:53 -0300 | [diff] [blame] | 281 | unsigned long top1, top2; |
| 282 | unsigned long bottom1, bottom2; |
Kuninori Morimoto | 5cf93f1 | 2009-10-05 12:48:59 -0300 | [diff] [blame] | 283 | u32 status; |
Guennadi Liakhovetski | 15f517e | 2011-07-26 11:03:37 -0300 | [diff] [blame] | 284 | bool planar; |
Kuninori Morimoto | 5cf93f1 | 2009-10-05 12:48:59 -0300 | [diff] [blame] | 285 | int ret = 0; |
Magnus Damm | 8be65dc | 2008-12-18 12:38:06 -0300 | [diff] [blame] | 286 | |
Guennadi Liakhovetski | 5d28d52 | 2009-12-11 11:15:05 -0300 | [diff] [blame] | 287 | /* |
| 288 | * The hardware is _very_ picky about this sequence. Especially |
Magnus Damm | 7a1a816 | 2008-12-18 12:50:30 -0300 | [diff] [blame] | 289 | * the CEU_CETCR_MAGIC value. It seems like we need to acknowledge |
| 290 | * several not-so-well documented interrupt sources in CETCR. |
| 291 | */ |
Kuninori Morimoto | 5cf93f1 | 2009-10-05 12:48:59 -0300 | [diff] [blame] | 292 | ceu_write(pcdev, CEIER, ceu_read(pcdev, CEIER) & ~CEU_CEIER_MASK); |
| 293 | status = ceu_read(pcdev, CETCR); |
| 294 | ceu_write(pcdev, CETCR, ~status & CEU_CETCR_MAGIC); |
Guennadi Liakhovetski | 3dac322 | 2011-03-28 13:39:16 -0300 | [diff] [blame] | 295 | if (!pcdev->frozen) |
| 296 | ceu_write(pcdev, CEIER, ceu_read(pcdev, CEIER) | CEU_CEIER_MASK); |
Magnus Damm | 7a1a816 | 2008-12-18 12:50:30 -0300 | [diff] [blame] | 297 | ceu_write(pcdev, CAPCR, ceu_read(pcdev, CAPCR) & ~CEU_CAPCR_CTNCP); |
| 298 | ceu_write(pcdev, CETCR, CEU_CETCR_MAGIC ^ CEU_CETCR_IGRW); |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 299 | |
Kuninori Morimoto | 5cf93f1 | 2009-10-05 12:48:59 -0300 | [diff] [blame] | 300 | /* |
| 301 | * When a VBP interrupt occurs, a capture end interrupt does not occur |
| 302 | * and the image of that frame is not captured correctly. So, soft reset |
| 303 | * is needed here. |
| 304 | */ |
| 305 | if (status & CEU_CEIER_VBP) { |
| 306 | sh_mobile_ceu_soft_reset(pcdev); |
| 307 | ret = -EIO; |
| 308 | } |
| 309 | |
Guennadi Liakhovetski | 3dac322 | 2011-03-28 13:39:16 -0300 | [diff] [blame] | 310 | if (pcdev->frozen) { |
| 311 | complete(&pcdev->complete); |
| 312 | return ret; |
| 313 | } |
| 314 | |
Magnus Damm | 8be65dc | 2008-12-18 12:38:06 -0300 | [diff] [blame] | 315 | if (!pcdev->active) |
Kuninori Morimoto | 5cf93f1 | 2009-10-05 12:48:59 -0300 | [diff] [blame] | 316 | return ret; |
Magnus Damm | 8be65dc | 2008-12-18 12:38:06 -0300 | [diff] [blame] | 317 | |
Kuninori Morimoto | 1edcc10 | 2009-12-11 11:53:53 -0300 | [diff] [blame] | 318 | if (V4L2_FIELD_INTERLACED_BT == pcdev->field) { |
| 319 | top1 = CDBYR; |
| 320 | top2 = CDBCR; |
| 321 | bottom1 = CDAYR; |
| 322 | bottom2 = CDACR; |
| 323 | } else { |
| 324 | top1 = CDAYR; |
| 325 | top2 = CDACR; |
| 326 | bottom1 = CDBYR; |
| 327 | bottom2 = CDBCR; |
| 328 | } |
| 329 | |
Marek Szyprowski | ba7fcb0 | 2011-08-29 03:20:56 -0300 | [diff] [blame] | 330 | phys_addr_top = vb2_dma_contig_plane_dma_addr(pcdev->active, 0); |
Guennadi Liakhovetski | a616898 | 2011-02-18 05:30:15 -0300 | [diff] [blame] | 331 | |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 332 | switch (icd->current_fmt->host_fmt->fourcc) { |
Magnus Damm | 8be65dc | 2008-12-18 12:38:06 -0300 | [diff] [blame] | 333 | case V4L2_PIX_FMT_NV12: |
| 334 | case V4L2_PIX_FMT_NV21: |
Magnus Damm | 9e4a56d | 2008-12-18 12:45:33 -0300 | [diff] [blame] | 335 | case V4L2_PIX_FMT_NV16: |
| 336 | case V4L2_PIX_FMT_NV61: |
Guennadi Liakhovetski | 15f517e | 2011-07-26 11:03:37 -0300 | [diff] [blame] | 337 | planar = true; |
| 338 | break; |
| 339 | default: |
| 340 | planar = false; |
| 341 | } |
| 342 | |
| 343 | ceu_write(pcdev, top1, phys_addr_top); |
| 344 | if (V4L2_FIELD_NONE != pcdev->field) { |
| 345 | if (planar) |
| 346 | phys_addr_bottom = phys_addr_top + icd->user_width; |
| 347 | else |
Laurent Pinchart | 1c0f95e | 2012-03-21 08:03:22 -0300 | [diff] [blame] | 348 | phys_addr_bottom = phys_addr_top + icd->bytesperline; |
Guennadi Liakhovetski | 15f517e | 2011-07-26 11:03:37 -0300 | [diff] [blame] | 349 | ceu_write(pcdev, bottom1, phys_addr_bottom); |
| 350 | } |
| 351 | |
| 352 | if (planar) { |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 353 | phys_addr_top += icd->user_width * |
| 354 | icd->user_height; |
Kuninori Morimoto | 1edcc10 | 2009-12-11 11:53:53 -0300 | [diff] [blame] | 355 | ceu_write(pcdev, top2, phys_addr_top); |
| 356 | if (V4L2_FIELD_NONE != pcdev->field) { |
| 357 | phys_addr_bottom = phys_addr_top + icd->user_width; |
| 358 | ceu_write(pcdev, bottom2, phys_addr_bottom); |
Kuninori Morimoto | ccab8a2 | 2008-12-18 12:51:21 -0300 | [diff] [blame] | 359 | } |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 360 | } |
Magnus Damm | 8be65dc | 2008-12-18 12:38:06 -0300 | [diff] [blame] | 361 | |
Magnus Damm | 8be65dc | 2008-12-18 12:38:06 -0300 | [diff] [blame] | 362 | ceu_write(pcdev, CAPSR, 0x1); /* start capture */ |
Kuninori Morimoto | 5cf93f1 | 2009-10-05 12:48:59 -0300 | [diff] [blame] | 363 | |
| 364 | return ret; |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 365 | } |
| 366 | |
Guennadi Liakhovetski | a616898 | 2011-02-18 05:30:15 -0300 | [diff] [blame] | 367 | static int sh_mobile_ceu_videobuf_prepare(struct vb2_buffer *vb) |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 368 | { |
Guennadi Liakhovetski | b5518a4 | 2011-11-03 10:11:11 -0300 | [diff] [blame] | 369 | struct sh_mobile_ceu_buffer *buf = to_ceu_vb(vb); |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 370 | |
| 371 | /* Added list head initialization on alloc */ |
Guennadi Liakhovetski | a616898 | 2011-02-18 05:30:15 -0300 | [diff] [blame] | 372 | WARN(!list_empty(&buf->queue), "Buffer %p on queue!\n", vb); |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 373 | |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 374 | return 0; |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 375 | } |
| 376 | |
Guennadi Liakhovetski | a616898 | 2011-02-18 05:30:15 -0300 | [diff] [blame] | 377 | static void sh_mobile_ceu_videobuf_queue(struct vb2_buffer *vb) |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 378 | { |
Guennadi Liakhovetski | a616898 | 2011-02-18 05:30:15 -0300 | [diff] [blame] | 379 | struct soc_camera_device *icd = container_of(vb->vb2_queue, struct soc_camera_device, vb2_vidq); |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 380 | struct soc_camera_host *ici = to_soc_camera_host(icd->parent); |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 381 | struct sh_mobile_ceu_dev *pcdev = ici->priv; |
Guennadi Liakhovetski | a616898 | 2011-02-18 05:30:15 -0300 | [diff] [blame] | 382 | struct sh_mobile_ceu_buffer *buf = to_ceu_vb(vb); |
Guennadi Liakhovetski | b5518a4 | 2011-11-03 10:11:11 -0300 | [diff] [blame] | 383 | unsigned long size; |
Guennadi Liakhovetski | b5518a4 | 2011-11-03 10:11:11 -0300 | [diff] [blame] | 384 | |
Laurent Pinchart | 2b61d46e | 2012-03-21 08:03:21 -0300 | [diff] [blame] | 385 | size = icd->sizeimage; |
Guennadi Liakhovetski | b5518a4 | 2011-11-03 10:11:11 -0300 | [diff] [blame] | 386 | |
| 387 | if (vb2_plane_size(vb, 0) < size) { |
| 388 | dev_err(icd->parent, "Buffer #%d too small (%lu < %lu)\n", |
| 389 | vb->v4l2_buf.index, vb2_plane_size(vb, 0), size); |
| 390 | goto error; |
| 391 | } |
| 392 | |
| 393 | vb2_set_plane_payload(vb, 0, size); |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 394 | |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 395 | dev_dbg(icd->parent, "%s (vb=0x%p) 0x%p %lu\n", __func__, |
Guennadi Liakhovetski | a616898 | 2011-02-18 05:30:15 -0300 | [diff] [blame] | 396 | vb, vb2_plane_vaddr(vb, 0), vb2_get_plane_payload(vb, 0)); |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 397 | |
Guennadi Liakhovetski | b5518a4 | 2011-11-03 10:11:11 -0300 | [diff] [blame] | 398 | #ifdef DEBUG |
| 399 | /* |
| 400 | * This can be useful if you want to see if we actually fill |
| 401 | * the buffer with something |
| 402 | */ |
| 403 | if (vb2_plane_vaddr(vb, 0)) |
| 404 | memset(vb2_plane_vaddr(vb, 0), 0xaa, vb2_get_plane_payload(vb, 0)); |
| 405 | #endif |
| 406 | |
Guennadi Liakhovetski | 3dac322 | 2011-03-28 13:39:16 -0300 | [diff] [blame] | 407 | spin_lock_irq(&pcdev->lock); |
Guennadi Liakhovetski | a616898 | 2011-02-18 05:30:15 -0300 | [diff] [blame] | 408 | list_add_tail(&buf->queue, &pcdev->capture); |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 409 | |
| 410 | if (!pcdev->active) { |
Kuninori Morimoto | 5cf93f1 | 2009-10-05 12:48:59 -0300 | [diff] [blame] | 411 | /* |
| 412 | * Because there were no active buffer at this moment, |
| 413 | * we are not interested in the return value of |
| 414 | * sh_mobile_ceu_capture here. |
| 415 | */ |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 416 | pcdev->active = vb; |
| 417 | sh_mobile_ceu_capture(pcdev); |
| 418 | } |
Guennadi Liakhovetski | 3dac322 | 2011-03-28 13:39:16 -0300 | [diff] [blame] | 419 | spin_unlock_irq(&pcdev->lock); |
Guennadi Liakhovetski | b5518a4 | 2011-11-03 10:11:11 -0300 | [diff] [blame] | 420 | |
| 421 | return; |
| 422 | |
| 423 | error: |
| 424 | vb2_buffer_done(vb, VB2_BUF_STATE_ERROR); |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 425 | } |
| 426 | |
Guennadi Liakhovetski | a616898 | 2011-02-18 05:30:15 -0300 | [diff] [blame] | 427 | static void sh_mobile_ceu_videobuf_release(struct vb2_buffer *vb) |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 428 | { |
Guennadi Liakhovetski | a616898 | 2011-02-18 05:30:15 -0300 | [diff] [blame] | 429 | struct soc_camera_device *icd = container_of(vb->vb2_queue, struct soc_camera_device, vb2_vidq); |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 430 | struct soc_camera_host *ici = to_soc_camera_host(icd->parent); |
Guennadi Liakhovetski | a616898 | 2011-02-18 05:30:15 -0300 | [diff] [blame] | 431 | struct sh_mobile_ceu_buffer *buf = to_ceu_vb(vb); |
Guennadi Liakhovetski | cca0e549 | 2009-08-25 11:46:51 -0300 | [diff] [blame] | 432 | struct sh_mobile_ceu_dev *pcdev = ici->priv; |
Guennadi Liakhovetski | cca0e549 | 2009-08-25 11:46:51 -0300 | [diff] [blame] | 433 | |
Guennadi Liakhovetski | 3dac322 | 2011-03-28 13:39:16 -0300 | [diff] [blame] | 434 | spin_lock_irq(&pcdev->lock); |
Guennadi Liakhovetski | cca0e549 | 2009-08-25 11:46:51 -0300 | [diff] [blame] | 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 | |
Guennadi Liakhovetski | cb74cf5 | 2011-07-04 12:15:14 -0300 | [diff] [blame] | 442 | /* |
| 443 | * Doesn't hurt also if the list is empty, but it hurts, if queuing the |
| 444 | * buffer failed, and .buf_init() hasn't been called |
| 445 | */ |
| 446 | if (buf->queue.next) |
| 447 | list_del_init(&buf->queue); |
Guennadi Liakhovetski | cca0e549 | 2009-08-25 11:46:51 -0300 | [diff] [blame] | 448 | |
Guennadi Liakhovetski | b5518a4 | 2011-11-03 10:11:11 -0300 | [diff] [blame] | 449 | pcdev->buf_total -= PAGE_ALIGN(vb2_plane_size(vb, 0)); |
| 450 | dev_dbg(icd->parent, "%s() %zu bytes buffers\n", __func__, |
| 451 | pcdev->buf_total); |
| 452 | |
Guennadi Liakhovetski | 3dac322 | 2011-03-28 13:39:16 -0300 | [diff] [blame] | 453 | spin_unlock_irq(&pcdev->lock); |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 454 | } |
| 455 | |
Guennadi Liakhovetski | a616898 | 2011-02-18 05:30:15 -0300 | [diff] [blame] | 456 | static int sh_mobile_ceu_videobuf_init(struct vb2_buffer *vb) |
| 457 | { |
Guennadi Liakhovetski | b5518a4 | 2011-11-03 10:11:11 -0300 | [diff] [blame] | 458 | struct soc_camera_device *icd = container_of(vb->vb2_queue, struct soc_camera_device, vb2_vidq); |
| 459 | struct soc_camera_host *ici = to_soc_camera_host(icd->parent); |
| 460 | struct sh_mobile_ceu_dev *pcdev = ici->priv; |
| 461 | |
| 462 | pcdev->buf_total += PAGE_ALIGN(vb2_plane_size(vb, 0)); |
| 463 | dev_dbg(icd->parent, "%s() %zu bytes buffers\n", __func__, |
| 464 | pcdev->buf_total); |
| 465 | |
Guennadi Liakhovetski | a616898 | 2011-02-18 05:30:15 -0300 | [diff] [blame] | 466 | /* This is for locking debugging only */ |
| 467 | INIT_LIST_HEAD(&to_ceu_vb(vb)->queue); |
| 468 | return 0; |
| 469 | } |
| 470 | |
Guennadi Liakhovetski | 066f786 | 2011-03-24 12:51:07 -0300 | [diff] [blame] | 471 | static int sh_mobile_ceu_stop_streaming(struct vb2_queue *q) |
| 472 | { |
| 473 | struct soc_camera_device *icd = container_of(q, struct soc_camera_device, vb2_vidq); |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 474 | struct soc_camera_host *ici = to_soc_camera_host(icd->parent); |
Guennadi Liakhovetski | 066f786 | 2011-03-24 12:51:07 -0300 | [diff] [blame] | 475 | struct sh_mobile_ceu_dev *pcdev = ici->priv; |
| 476 | struct list_head *buf_head, *tmp; |
Guennadi Liakhovetski | 066f786 | 2011-03-24 12:51:07 -0300 | [diff] [blame] | 477 | |
Guennadi Liakhovetski | 3dac322 | 2011-03-28 13:39:16 -0300 | [diff] [blame] | 478 | spin_lock_irq(&pcdev->lock); |
Guennadi Liakhovetski | 066f786 | 2011-03-24 12:51:07 -0300 | [diff] [blame] | 479 | |
| 480 | pcdev->active = NULL; |
| 481 | |
| 482 | list_for_each_safe(buf_head, tmp, &pcdev->capture) |
| 483 | list_del_init(buf_head); |
| 484 | |
Guennadi Liakhovetski | 3dac322 | 2011-03-28 13:39:16 -0300 | [diff] [blame] | 485 | spin_unlock_irq(&pcdev->lock); |
Guennadi Liakhovetski | 066f786 | 2011-03-24 12:51:07 -0300 | [diff] [blame] | 486 | |
| 487 | return sh_mobile_ceu_soft_reset(pcdev); |
| 488 | } |
| 489 | |
Guennadi Liakhovetski | a616898 | 2011-02-18 05:30:15 -0300 | [diff] [blame] | 490 | static struct vb2_ops sh_mobile_ceu_videobuf_ops = { |
| 491 | .queue_setup = sh_mobile_ceu_videobuf_setup, |
| 492 | .buf_prepare = sh_mobile_ceu_videobuf_prepare, |
| 493 | .buf_queue = sh_mobile_ceu_videobuf_queue, |
| 494 | .buf_cleanup = sh_mobile_ceu_videobuf_release, |
| 495 | .buf_init = sh_mobile_ceu_videobuf_init, |
| 496 | .wait_prepare = soc_camera_unlock, |
| 497 | .wait_finish = soc_camera_lock, |
Guennadi Liakhovetski | 066f786 | 2011-03-24 12:51:07 -0300 | [diff] [blame] | 498 | .stop_streaming = sh_mobile_ceu_stop_streaming, |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 499 | }; |
| 500 | |
| 501 | static irqreturn_t sh_mobile_ceu_irq(int irq, void *data) |
| 502 | { |
| 503 | struct sh_mobile_ceu_dev *pcdev = data; |
Guennadi Liakhovetski | a616898 | 2011-02-18 05:30:15 -0300 | [diff] [blame] | 504 | struct vb2_buffer *vb; |
| 505 | int ret; |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 506 | |
Guennadi Liakhovetski | a616898 | 2011-02-18 05:30:15 -0300 | [diff] [blame] | 507 | spin_lock(&pcdev->lock); |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 508 | |
| 509 | vb = pcdev->active; |
Guennadi Liakhovetski | cca0e549 | 2009-08-25 11:46:51 -0300 | [diff] [blame] | 510 | if (!vb) |
| 511 | /* Stale interrupt from a released buffer */ |
| 512 | goto out; |
| 513 | |
Guennadi Liakhovetski | a616898 | 2011-02-18 05:30:15 -0300 | [diff] [blame] | 514 | list_del_init(&to_ceu_vb(vb)->queue); |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 515 | |
| 516 | if (!list_empty(&pcdev->capture)) |
Guennadi Liakhovetski | a616898 | 2011-02-18 05:30:15 -0300 | [diff] [blame] | 517 | pcdev->active = &list_entry(pcdev->capture.next, |
| 518 | struct sh_mobile_ceu_buffer, queue)->vb; |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 519 | else |
| 520 | pcdev->active = NULL; |
| 521 | |
Guennadi Liakhovetski | a616898 | 2011-02-18 05:30:15 -0300 | [diff] [blame] | 522 | ret = sh_mobile_ceu_capture(pcdev); |
| 523 | do_gettimeofday(&vb->v4l2_buf.timestamp); |
| 524 | if (!ret) { |
| 525 | vb->v4l2_buf.field = pcdev->field; |
| 526 | vb->v4l2_buf.sequence = pcdev->sequence++; |
| 527 | } |
| 528 | vb2_buffer_done(vb, ret < 0 ? VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE); |
Guennadi Liakhovetski | cca0e549 | 2009-08-25 11:46:51 -0300 | [diff] [blame] | 529 | |
| 530 | out: |
Guennadi Liakhovetski | a616898 | 2011-02-18 05:30:15 -0300 | [diff] [blame] | 531 | spin_unlock(&pcdev->lock); |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 532 | |
| 533 | return IRQ_HANDLED; |
| 534 | } |
| 535 | |
Guennadi Liakhovetski | 6b526fe | 2011-07-01 11:19:58 -0300 | [diff] [blame] | 536 | static struct v4l2_subdev *find_csi2(struct sh_mobile_ceu_dev *pcdev) |
| 537 | { |
| 538 | struct v4l2_subdev *sd; |
| 539 | |
| 540 | if (!pcdev->csi2_pdev) |
| 541 | return NULL; |
| 542 | |
| 543 | v4l2_device_for_each_subdev(sd, &pcdev->ici.v4l2_dev) |
| 544 | if (&pcdev->csi2_pdev->dev == v4l2_get_subdevdata(sd)) |
| 545 | return sd; |
| 546 | |
| 547 | return NULL; |
| 548 | } |
| 549 | |
Guennadi Liakhovetski | 1c3bb74 | 2008-12-18 12:28:54 -0300 | [diff] [blame] | 550 | /* Called with .video_lock held */ |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 551 | static int sh_mobile_ceu_add_device(struct soc_camera_device *icd) |
| 552 | { |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 553 | struct soc_camera_host *ici = to_soc_camera_host(icd->parent); |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 554 | struct sh_mobile_ceu_dev *pcdev = ici->priv; |
Guennadi Liakhovetski | 6b526fe | 2011-07-01 11:19:58 -0300 | [diff] [blame] | 555 | struct v4l2_subdev *csi2_sd; |
Guennadi Liakhovetski | 6ed7c03 | 2009-12-11 11:15:06 -0300 | [diff] [blame] | 556 | int ret; |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 557 | |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 558 | if (pcdev->icd) |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 559 | return -EBUSY; |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 560 | |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 561 | dev_info(icd->parent, |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 562 | "SuperH Mobile CEU driver attached to camera %d\n", |
| 563 | icd->devnum); |
| 564 | |
Guennadi Liakhovetski | a79aebf | 2009-09-25 13:36:51 +0900 | [diff] [blame] | 565 | pm_runtime_get_sync(ici->v4l2_dev.dev); |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 566 | |
Guennadi Liakhovetski | b5518a4 | 2011-11-03 10:11:11 -0300 | [diff] [blame] | 567 | pcdev->buf_total = 0; |
| 568 | |
Guennadi Liakhovetski | 6ed7c03 | 2009-12-11 11:15:06 -0300 | [diff] [blame] | 569 | ret = sh_mobile_ceu_soft_reset(pcdev); |
Guennadi Liakhovetski | 6b526fe | 2011-07-01 11:19:58 -0300 | [diff] [blame] | 570 | |
| 571 | csi2_sd = find_csi2(pcdev); |
Guennadi Liakhovetski | 4c0b036 | 2011-12-05 16:01:13 -0300 | [diff] [blame] | 572 | if (csi2_sd) { |
| 573 | csi2_sd->grp_id = soc_camera_grp_id(icd); |
| 574 | v4l2_set_subdev_hostdata(csi2_sd, icd); |
| 575 | } |
Guennadi Liakhovetski | 6b526fe | 2011-07-01 11:19:58 -0300 | [diff] [blame] | 576 | |
| 577 | ret = v4l2_subdev_call(csi2_sd, core, s_power, 1); |
Guennadi Liakhovetski | 489759c | 2011-09-07 11:59:47 -0300 | [diff] [blame] | 578 | if (ret < 0 && ret != -ENOIOCTLCMD && ret != -ENODEV) { |
Guennadi Liakhovetski | 6b526fe | 2011-07-01 11:19:58 -0300 | [diff] [blame] | 579 | pm_runtime_put_sync(ici->v4l2_dev.dev); |
Guennadi Liakhovetski | 489759c | 2011-09-07 11:59:47 -0300 | [diff] [blame] | 580 | return ret; |
Guennadi Liakhovetski | 6b526fe | 2011-07-01 11:19:58 -0300 | [diff] [blame] | 581 | } |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 582 | |
Guennadi Liakhovetski | 489759c | 2011-09-07 11:59:47 -0300 | [diff] [blame] | 583 | /* |
| 584 | * -ENODEV is special: either csi2_sd == NULL or the CSI-2 driver |
| 585 | * has not found this soc-camera device among its clients |
| 586 | */ |
| 587 | if (ret == -ENODEV && csi2_sd) |
| 588 | csi2_sd->grp_id = 0; |
| 589 | pcdev->icd = icd; |
| 590 | |
| 591 | return 0; |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 592 | } |
| 593 | |
Guennadi Liakhovetski | 1c3bb74 | 2008-12-18 12:28:54 -0300 | [diff] [blame] | 594 | /* Called with .video_lock held */ |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 595 | static void sh_mobile_ceu_remove_device(struct soc_camera_device *icd) |
| 596 | { |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 597 | struct soc_camera_host *ici = to_soc_camera_host(icd->parent); |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 598 | struct sh_mobile_ceu_dev *pcdev = ici->priv; |
Guennadi Liakhovetski | 6b526fe | 2011-07-01 11:19:58 -0300 | [diff] [blame] | 599 | struct v4l2_subdev *csi2_sd = find_csi2(pcdev); |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 600 | |
| 601 | BUG_ON(icd != pcdev->icd); |
| 602 | |
Guennadi Liakhovetski | 6b526fe | 2011-07-01 11:19:58 -0300 | [diff] [blame] | 603 | v4l2_subdev_call(csi2_sd, core, s_power, 0); |
Guennadi Liakhovetski | 489759c | 2011-09-07 11:59:47 -0300 | [diff] [blame] | 604 | if (csi2_sd) |
| 605 | csi2_sd->grp_id = 0; |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 606 | /* disable capture, disable interrupts */ |
| 607 | ceu_write(pcdev, CEIER, 0); |
Kuninori Morimoto | b1de7ae | 2009-10-05 12:48:20 -0300 | [diff] [blame] | 608 | sh_mobile_ceu_soft_reset(pcdev); |
Magnus Damm | 51354cc | 2008-10-16 19:51:20 -0300 | [diff] [blame] | 609 | |
| 610 | /* make sure active buffer is canceled */ |
Guennadi Liakhovetski | 3dac322 | 2011-03-28 13:39:16 -0300 | [diff] [blame] | 611 | spin_lock_irq(&pcdev->lock); |
Magnus Damm | 51354cc | 2008-10-16 19:51:20 -0300 | [diff] [blame] | 612 | if (pcdev->active) { |
Guennadi Liakhovetski | a616898 | 2011-02-18 05:30:15 -0300 | [diff] [blame] | 613 | list_del_init(&to_ceu_vb(pcdev->active)->queue); |
| 614 | vb2_buffer_done(pcdev->active, VB2_BUF_STATE_ERROR); |
Magnus Damm | 51354cc | 2008-10-16 19:51:20 -0300 | [diff] [blame] | 615 | pcdev->active = NULL; |
| 616 | } |
Guennadi Liakhovetski | 3dac322 | 2011-03-28 13:39:16 -0300 | [diff] [blame] | 617 | spin_unlock_irq(&pcdev->lock); |
Magnus Damm | 51354cc | 2008-10-16 19:51:20 -0300 | [diff] [blame] | 618 | |
Guennadi Liakhovetski | a79aebf | 2009-09-25 13:36:51 +0900 | [diff] [blame] | 619 | pm_runtime_put_sync(ici->v4l2_dev.dev); |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 620 | |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 621 | dev_info(icd->parent, |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 622 | "SuperH Mobile CEU driver detached from camera %d\n", |
| 623 | icd->devnum); |
| 624 | |
| 625 | pcdev->icd = NULL; |
| 626 | } |
| 627 | |
Guennadi Liakhovetski | 961801b | 2009-08-25 11:46:54 -0300 | [diff] [blame] | 628 | /* |
| 629 | * See chapter 29.4.12 "Capture Filter Control Register (CFLCR)" |
| 630 | * in SH7722 Hardware Manual |
| 631 | */ |
| 632 | static unsigned int size_dst(unsigned int src, unsigned int scale) |
| 633 | { |
| 634 | unsigned int mant_pre = scale >> 12; |
| 635 | if (!src || !scale) |
| 636 | return src; |
| 637 | return ((mant_pre + 2 * (src - 1)) / (2 * mant_pre) - 1) * |
| 638 | mant_pre * 4096 / scale + 1; |
| 639 | } |
| 640 | |
Guennadi Liakhovetski | 961801b | 2009-08-25 11:46:54 -0300 | [diff] [blame] | 641 | static u16 calc_scale(unsigned int src, unsigned int *dst) |
| 642 | { |
| 643 | u16 scale; |
| 644 | |
| 645 | if (src == *dst) |
| 646 | return 0; |
| 647 | |
| 648 | scale = (src * 4096 / *dst) & ~7; |
| 649 | |
| 650 | while (scale > 4096 && size_dst(src, scale) < *dst) |
| 651 | scale -= 8; |
| 652 | |
| 653 | *dst = size_dst(src, scale); |
| 654 | |
| 655 | return scale; |
| 656 | } |
| 657 | |
| 658 | /* rect is guaranteed to not exceed the scaled camera rectangle */ |
Guennadi Liakhovetski | e622681 | 2010-03-23 11:23:31 -0300 | [diff] [blame] | 659 | static void sh_mobile_ceu_set_rect(struct soc_camera_device *icd) |
Guennadi Liakhovetski | 904078f | 2009-08-25 11:46:52 -0300 | [diff] [blame] | 660 | { |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 661 | struct soc_camera_host *ici = to_soc_camera_host(icd->parent); |
Guennadi Liakhovetski | 904078f | 2009-08-25 11:46:52 -0300 | [diff] [blame] | 662 | struct sh_mobile_ceu_cam *cam = icd->host_priv; |
| 663 | struct sh_mobile_ceu_dev *pcdev = ici->priv; |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 664 | unsigned int height, width, cdwdr_width, in_width, in_height; |
| 665 | unsigned int left_offset, top_offset; |
Guennadi Liakhovetski | 904078f | 2009-08-25 11:46:52 -0300 | [diff] [blame] | 666 | u32 camor; |
| 667 | |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 668 | dev_geo(icd->parent, "Crop %ux%u@%u:%u\n", |
Guennadi Liakhovetski | e622681 | 2010-03-23 11:23:31 -0300 | [diff] [blame] | 669 | icd->user_width, icd->user_height, cam->ceu_left, cam->ceu_top); |
Guennadi Liakhovetski | 961801b | 2009-08-25 11:46:54 -0300 | [diff] [blame] | 670 | |
Guennadi Liakhovetski | e622681 | 2010-03-23 11:23:31 -0300 | [diff] [blame] | 671 | left_offset = cam->ceu_left; |
| 672 | top_offset = cam->ceu_top; |
Guennadi Liakhovetski | 904078f | 2009-08-25 11:46:52 -0300 | [diff] [blame] | 673 | |
Guennadi Liakhovetski | eec5ce0 | 2011-07-22 07:43:15 -0300 | [diff] [blame] | 674 | WARN_ON(icd->user_width & 3 || icd->user_height & 3); |
| 675 | |
| 676 | width = icd->user_width; |
| 677 | |
Guennadi Liakhovetski | 904078f | 2009-08-25 11:46:52 -0300 | [diff] [blame] | 678 | if (pcdev->image_mode) { |
Guennadi Liakhovetski | e622681 | 2010-03-23 11:23:31 -0300 | [diff] [blame] | 679 | in_width = cam->width; |
Guennadi Liakhovetski | 961801b | 2009-08-25 11:46:54 -0300 | [diff] [blame] | 680 | if (!pcdev->is_16bit) { |
Guennadi Liakhovetski | 961801b | 2009-08-25 11:46:54 -0300 | [diff] [blame] | 681 | in_width *= 2; |
| 682 | left_offset *= 2; |
| 683 | } |
Guennadi Liakhovetski | eec5ce0 | 2011-07-22 07:43:15 -0300 | [diff] [blame] | 684 | cdwdr_width = width; |
Guennadi Liakhovetski | 904078f | 2009-08-25 11:46:52 -0300 | [diff] [blame] | 685 | } else { |
Guennadi Liakhovetski | eec5ce0 | 2011-07-22 07:43:15 -0300 | [diff] [blame] | 686 | int bytes_per_line = soc_mbus_bytes_per_line(width, |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 687 | icd->current_fmt->host_fmt); |
| 688 | unsigned int w_factor; |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 689 | |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 690 | switch (icd->current_fmt->host_fmt->packing) { |
| 691 | case SOC_MBUS_PACKING_2X8_PADHI: |
| 692 | w_factor = 2; |
| 693 | break; |
| 694 | default: |
| 695 | w_factor = 1; |
| 696 | } |
Guennadi Liakhovetski | 961801b | 2009-08-25 11:46:54 -0300 | [diff] [blame] | 697 | |
Guennadi Liakhovetski | e622681 | 2010-03-23 11:23:31 -0300 | [diff] [blame] | 698 | in_width = cam->width * w_factor; |
Guennadi Liakhovetski | eec5ce0 | 2011-07-22 07:43:15 -0300 | [diff] [blame] | 699 | left_offset *= w_factor; |
Guennadi Liakhovetski | 961801b | 2009-08-25 11:46:54 -0300 | [diff] [blame] | 700 | |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 701 | if (bytes_per_line < 0) |
Guennadi Liakhovetski | eec5ce0 | 2011-07-22 07:43:15 -0300 | [diff] [blame] | 702 | cdwdr_width = width; |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 703 | else |
| 704 | cdwdr_width = bytes_per_line; |
Guennadi Liakhovetski | 904078f | 2009-08-25 11:46:52 -0300 | [diff] [blame] | 705 | } |
| 706 | |
Guennadi Liakhovetski | e622681 | 2010-03-23 11:23:31 -0300 | [diff] [blame] | 707 | height = icd->user_height; |
| 708 | in_height = cam->height; |
Kuninori Morimoto | 1edcc10 | 2009-12-11 11:53:53 -0300 | [diff] [blame] | 709 | if (V4L2_FIELD_NONE != pcdev->field) { |
Guennadi Liakhovetski | eec5ce0 | 2011-07-22 07:43:15 -0300 | [diff] [blame] | 710 | height = (height / 2) & ~3; |
Guennadi Liakhovetski | 961801b | 2009-08-25 11:46:54 -0300 | [diff] [blame] | 711 | in_height /= 2; |
| 712 | top_offset /= 2; |
Guennadi Liakhovetski | 904078f | 2009-08-25 11:46:52 -0300 | [diff] [blame] | 713 | cdwdr_width *= 2; |
| 714 | } |
| 715 | |
Guennadi Liakhovetski | b3b5020 | 2010-07-26 12:13:34 -0300 | [diff] [blame] | 716 | /* CSI2 special configuration */ |
Guennadi Liakhovetski | 6b526fe | 2011-07-01 11:19:58 -0300 | [diff] [blame] | 717 | if (pcdev->pdata->csi2) { |
Guennadi Liakhovetski | b3b5020 | 2010-07-26 12:13:34 -0300 | [diff] [blame] | 718 | in_width = ((in_width - 2) * 2); |
| 719 | left_offset *= 2; |
| 720 | } |
| 721 | |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 722 | /* Set CAMOR, CAPWR, CFSZR, take care of CDWDR */ |
Guennadi Liakhovetski | 904078f | 2009-08-25 11:46:52 -0300 | [diff] [blame] | 723 | camor = left_offset | (top_offset << 16); |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 724 | |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 725 | dev_geo(icd->parent, |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 726 | "CAMOR 0x%x, CAPWR 0x%x, CFSZR 0x%x, CDWDR 0x%x\n", camor, |
| 727 | (in_height << 16) | in_width, (height << 16) | width, |
| 728 | cdwdr_width); |
| 729 | |
Guennadi Liakhovetski | 904078f | 2009-08-25 11:46:52 -0300 | [diff] [blame] | 730 | ceu_write(pcdev, CAMOR, camor); |
Guennadi Liakhovetski | 961801b | 2009-08-25 11:46:54 -0300 | [diff] [blame] | 731 | ceu_write(pcdev, CAPWR, (in_height << 16) | in_width); |
Guennadi Liakhovetski | eec5ce0 | 2011-07-22 07:43:15 -0300 | [diff] [blame] | 732 | /* CFSZR clipping is applied _after_ the scaling filter (CFLCR) */ |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 733 | ceu_write(pcdev, CFSZR, (height << 16) | width); |
Guennadi Liakhovetski | 904078f | 2009-08-25 11:46:52 -0300 | [diff] [blame] | 734 | ceu_write(pcdev, CDWDR, cdwdr_width); |
| 735 | } |
| 736 | |
| 737 | static u32 capture_save_reset(struct sh_mobile_ceu_dev *pcdev) |
| 738 | { |
| 739 | u32 capsr = ceu_read(pcdev, CAPSR); |
| 740 | ceu_write(pcdev, CAPSR, 1 << 16); /* reset, stop capture */ |
| 741 | return capsr; |
| 742 | } |
| 743 | |
| 744 | static void capture_restore(struct sh_mobile_ceu_dev *pcdev, u32 capsr) |
| 745 | { |
| 746 | unsigned long timeout = jiffies + 10 * HZ; |
| 747 | |
| 748 | /* |
| 749 | * Wait until the end of the current frame. It can take a long time, |
| 750 | * but if it has been aborted by a CAPSR reset, it shoule exit sooner. |
| 751 | */ |
| 752 | while ((ceu_read(pcdev, CSTSR) & 1) && time_before(jiffies, timeout)) |
| 753 | msleep(1); |
| 754 | |
| 755 | if (time_after(jiffies, timeout)) { |
| 756 | dev_err(pcdev->ici.v4l2_dev.dev, |
| 757 | "Timeout waiting for frame end! Interface problem?\n"); |
| 758 | return; |
| 759 | } |
| 760 | |
| 761 | /* Wait until reset clears, this shall not hang... */ |
| 762 | while (ceu_read(pcdev, CAPSR) & (1 << 16)) |
| 763 | udelay(10); |
| 764 | |
| 765 | /* Anything to restore? */ |
| 766 | if (capsr & ~(1 << 16)) |
| 767 | ceu_write(pcdev, CAPSR, capsr); |
| 768 | } |
| 769 | |
Guennadi Liakhovetski | e1db704 | 2011-07-27 18:17:56 -0300 | [diff] [blame] | 770 | /* Find the bus subdevice driver, e.g., CSI2 */ |
| 771 | static struct v4l2_subdev *find_bus_subdev(struct sh_mobile_ceu_dev *pcdev, |
| 772 | struct soc_camera_device *icd) |
| 773 | { |
Guennadi Liakhovetski | 6ac939c | 2011-08-22 12:35:09 -0300 | [diff] [blame] | 774 | if (pcdev->csi2_pdev) { |
| 775 | struct v4l2_subdev *csi2_sd = find_csi2(pcdev); |
Guennadi Liakhovetski | 4c0b036 | 2011-12-05 16:01:13 -0300 | [diff] [blame] | 776 | if (csi2_sd && csi2_sd->grp_id == soc_camera_grp_id(icd)) |
Guennadi Liakhovetski | 6ac939c | 2011-08-22 12:35:09 -0300 | [diff] [blame] | 777 | return csi2_sd; |
| 778 | } |
Guennadi Liakhovetski | e1db704 | 2011-07-27 18:17:56 -0300 | [diff] [blame] | 779 | |
Guennadi Liakhovetski | 6ac939c | 2011-08-22 12:35:09 -0300 | [diff] [blame] | 780 | return soc_camera_to_subdev(icd); |
Guennadi Liakhovetski | e1db704 | 2011-07-27 18:17:56 -0300 | [diff] [blame] | 781 | } |
| 782 | |
| 783 | #define CEU_BUS_FLAGS (V4L2_MBUS_MASTER | \ |
| 784 | V4L2_MBUS_PCLK_SAMPLE_RISING | \ |
| 785 | V4L2_MBUS_HSYNC_ACTIVE_HIGH | \ |
| 786 | V4L2_MBUS_HSYNC_ACTIVE_LOW | \ |
| 787 | V4L2_MBUS_VSYNC_ACTIVE_HIGH | \ |
| 788 | V4L2_MBUS_VSYNC_ACTIVE_LOW | \ |
| 789 | V4L2_MBUS_DATA_ACTIVE_HIGH) |
| 790 | |
Guennadi Liakhovetski | a616898 | 2011-02-18 05:30:15 -0300 | [diff] [blame] | 791 | /* Capture is not running, no interrupts, no locking needed */ |
Guennadi Liakhovetski | 8843d11 | 2011-09-21 17:52:51 -0300 | [diff] [blame] | 792 | static int sh_mobile_ceu_set_bus_param(struct soc_camera_device *icd) |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 793 | { |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 794 | struct soc_camera_host *ici = to_soc_camera_host(icd->parent); |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 795 | struct sh_mobile_ceu_dev *pcdev = ici->priv; |
Guennadi Liakhovetski | e1db704 | 2011-07-27 18:17:56 -0300 | [diff] [blame] | 796 | struct v4l2_subdev *sd = find_bus_subdev(pcdev, icd); |
Guennadi Liakhovetski | 904078f | 2009-08-25 11:46:52 -0300 | [diff] [blame] | 797 | struct sh_mobile_ceu_cam *cam = icd->host_priv; |
Guennadi Liakhovetski | e1db704 | 2011-07-27 18:17:56 -0300 | [diff] [blame] | 798 | struct v4l2_mbus_config cfg = {.type = V4L2_MBUS_PARALLEL,}; |
| 799 | unsigned long value, common_flags = CEU_BUS_FLAGS; |
Guennadi Liakhovetski | 904078f | 2009-08-25 11:46:52 -0300 | [diff] [blame] | 800 | u32 capsr = capture_save_reset(pcdev); |
Guennadi Liakhovetski | e1db704 | 2011-07-27 18:17:56 -0300 | [diff] [blame] | 801 | unsigned int yuv_lineskip; |
| 802 | int ret; |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 803 | |
Guennadi Liakhovetski | e1db704 | 2011-07-27 18:17:56 -0300 | [diff] [blame] | 804 | /* |
| 805 | * If the client doesn't implement g_mbus_config, we just use our |
| 806 | * platform data |
| 807 | */ |
| 808 | ret = v4l2_subdev_call(sd, video, g_mbus_config, &cfg); |
| 809 | if (!ret) { |
| 810 | common_flags = soc_mbus_config_compatible(&cfg, |
| 811 | common_flags); |
| 812 | if (!common_flags) |
| 813 | return -EINVAL; |
| 814 | } else if (ret != -ENOIOCTLCMD) { |
| 815 | return ret; |
| 816 | } |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 817 | |
Kuninori Morimoto | 85dc1cf | 2009-12-11 11:53:54 -0300 | [diff] [blame] | 818 | /* Make choises, based on platform preferences */ |
Guennadi Liakhovetski | e1db704 | 2011-07-27 18:17:56 -0300 | [diff] [blame] | 819 | if ((common_flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH) && |
| 820 | (common_flags & V4L2_MBUS_HSYNC_ACTIVE_LOW)) { |
Kuninori Morimoto | 85dc1cf | 2009-12-11 11:53:54 -0300 | [diff] [blame] | 821 | if (pcdev->pdata->flags & SH_CEU_FLAG_HSYNC_LOW) |
Guennadi Liakhovetski | e1db704 | 2011-07-27 18:17:56 -0300 | [diff] [blame] | 822 | common_flags &= ~V4L2_MBUS_HSYNC_ACTIVE_HIGH; |
Kuninori Morimoto | 85dc1cf | 2009-12-11 11:53:54 -0300 | [diff] [blame] | 823 | else |
Guennadi Liakhovetski | e1db704 | 2011-07-27 18:17:56 -0300 | [diff] [blame] | 824 | common_flags &= ~V4L2_MBUS_HSYNC_ACTIVE_LOW; |
Kuninori Morimoto | 85dc1cf | 2009-12-11 11:53:54 -0300 | [diff] [blame] | 825 | } |
| 826 | |
Guennadi Liakhovetski | e1db704 | 2011-07-27 18:17:56 -0300 | [diff] [blame] | 827 | if ((common_flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH) && |
| 828 | (common_flags & V4L2_MBUS_VSYNC_ACTIVE_LOW)) { |
Kuninori Morimoto | 85dc1cf | 2009-12-11 11:53:54 -0300 | [diff] [blame] | 829 | if (pcdev->pdata->flags & SH_CEU_FLAG_VSYNC_LOW) |
Guennadi Liakhovetski | e1db704 | 2011-07-27 18:17:56 -0300 | [diff] [blame] | 830 | common_flags &= ~V4L2_MBUS_VSYNC_ACTIVE_HIGH; |
Kuninori Morimoto | 85dc1cf | 2009-12-11 11:53:54 -0300 | [diff] [blame] | 831 | else |
Guennadi Liakhovetski | e1db704 | 2011-07-27 18:17:56 -0300 | [diff] [blame] | 832 | common_flags &= ~V4L2_MBUS_VSYNC_ACTIVE_LOW; |
Kuninori Morimoto | 85dc1cf | 2009-12-11 11:53:54 -0300 | [diff] [blame] | 833 | } |
| 834 | |
Guennadi Liakhovetski | e1db704 | 2011-07-27 18:17:56 -0300 | [diff] [blame] | 835 | cfg.flags = common_flags; |
| 836 | ret = v4l2_subdev_call(sd, video, s_mbus_config, &cfg); |
| 837 | if (ret < 0 && ret != -ENOIOCTLCMD) |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 838 | return ret; |
| 839 | |
Guennadi Liakhovetski | e1db704 | 2011-07-27 18:17:56 -0300 | [diff] [blame] | 840 | if (icd->current_fmt->host_fmt->bits_per_sample > 8) |
Guennadi Liakhovetski | 904078f | 2009-08-25 11:46:52 -0300 | [diff] [blame] | 841 | pcdev->is_16bit = 1; |
Guennadi Liakhovetski | e1db704 | 2011-07-27 18:17:56 -0300 | [diff] [blame] | 842 | else |
| 843 | pcdev->is_16bit = 0; |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 844 | |
| 845 | ceu_write(pcdev, CRCNTR, 0); |
| 846 | ceu_write(pcdev, CRCMPR, 0); |
| 847 | |
Magnus Damm | 8be65dc | 2008-12-18 12:38:06 -0300 | [diff] [blame] | 848 | value = 0x00000010; /* data fetch by default */ |
Guennadi Liakhovetski | e1db704 | 2011-07-27 18:17:56 -0300 | [diff] [blame] | 849 | yuv_lineskip = 0x10; |
Magnus Damm | 8be65dc | 2008-12-18 12:38:06 -0300 | [diff] [blame] | 850 | |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 851 | switch (icd->current_fmt->host_fmt->fourcc) { |
Magnus Damm | 8be65dc | 2008-12-18 12:38:06 -0300 | [diff] [blame] | 852 | case V4L2_PIX_FMT_NV12: |
| 853 | case V4L2_PIX_FMT_NV21: |
Guennadi Liakhovetski | e1db704 | 2011-07-27 18:17:56 -0300 | [diff] [blame] | 854 | /* convert 4:2:2 -> 4:2:0 */ |
| 855 | yuv_lineskip = 0; /* skip for NV12/21, no skip for NV16/61 */ |
Magnus Damm | 9e4a56d | 2008-12-18 12:45:33 -0300 | [diff] [blame] | 856 | /* fall-through */ |
| 857 | case V4L2_PIX_FMT_NV16: |
| 858 | case V4L2_PIX_FMT_NV61: |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 859 | switch (cam->code) { |
Guennadi Liakhovetski | ace6e97 | 2010-07-22 16:52:51 -0300 | [diff] [blame] | 860 | case V4L2_MBUS_FMT_UYVY8_2X8: |
Magnus Damm | 8be65dc | 2008-12-18 12:38:06 -0300 | [diff] [blame] | 861 | value = 0x00000000; /* Cb0, Y0, Cr0, Y1 */ |
| 862 | break; |
Guennadi Liakhovetski | ace6e97 | 2010-07-22 16:52:51 -0300 | [diff] [blame] | 863 | case V4L2_MBUS_FMT_VYUY8_2X8: |
Magnus Damm | 8be65dc | 2008-12-18 12:38:06 -0300 | [diff] [blame] | 864 | value = 0x00000100; /* Cr0, Y0, Cb0, Y1 */ |
| 865 | break; |
Guennadi Liakhovetski | ace6e97 | 2010-07-22 16:52:51 -0300 | [diff] [blame] | 866 | case V4L2_MBUS_FMT_YUYV8_2X8: |
Magnus Damm | 8be65dc | 2008-12-18 12:38:06 -0300 | [diff] [blame] | 867 | value = 0x00000200; /* Y0, Cb0, Y1, Cr0 */ |
| 868 | break; |
Guennadi Liakhovetski | ace6e97 | 2010-07-22 16:52:51 -0300 | [diff] [blame] | 869 | case V4L2_MBUS_FMT_YVYU8_2X8: |
Magnus Damm | 8be65dc | 2008-12-18 12:38:06 -0300 | [diff] [blame] | 870 | value = 0x00000300; /* Y0, Cr0, Y1, Cb0 */ |
| 871 | break; |
| 872 | default: |
| 873 | BUG(); |
| 874 | } |
| 875 | } |
| 876 | |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 877 | if (icd->current_fmt->host_fmt->fourcc == V4L2_PIX_FMT_NV21 || |
| 878 | icd->current_fmt->host_fmt->fourcc == V4L2_PIX_FMT_NV61) |
Magnus Damm | 9e4a56d | 2008-12-18 12:45:33 -0300 | [diff] [blame] | 879 | value ^= 0x00000100; /* swap U, V to change from NV1x->NVx1 */ |
Magnus Damm | 8be65dc | 2008-12-18 12:38:06 -0300 | [diff] [blame] | 880 | |
Guennadi Liakhovetski | e1db704 | 2011-07-27 18:17:56 -0300 | [diff] [blame] | 881 | value |= common_flags & V4L2_MBUS_VSYNC_ACTIVE_LOW ? 1 << 1 : 0; |
| 882 | value |= common_flags & V4L2_MBUS_HSYNC_ACTIVE_LOW ? 1 << 0 : 0; |
Guennadi Liakhovetski | 904078f | 2009-08-25 11:46:52 -0300 | [diff] [blame] | 883 | value |= pcdev->is_16bit ? 1 << 12 : 0; |
Guennadi Liakhovetski | b3b5020 | 2010-07-26 12:13:34 -0300 | [diff] [blame] | 884 | |
| 885 | /* CSI2 mode */ |
Guennadi Liakhovetski | 6b526fe | 2011-07-01 11:19:58 -0300 | [diff] [blame] | 886 | if (pcdev->pdata->csi2) |
Guennadi Liakhovetski | b3b5020 | 2010-07-26 12:13:34 -0300 | [diff] [blame] | 887 | value |= 3 << 12; |
| 888 | |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 889 | ceu_write(pcdev, CAMCR, value); |
| 890 | |
| 891 | ceu_write(pcdev, CAPCR, 0x00300000); |
Kuninori Morimoto | 1edcc10 | 2009-12-11 11:53:53 -0300 | [diff] [blame] | 892 | |
| 893 | switch (pcdev->field) { |
| 894 | case V4L2_FIELD_INTERLACED_TB: |
| 895 | value = 0x101; |
| 896 | break; |
| 897 | case V4L2_FIELD_INTERLACED_BT: |
| 898 | value = 0x102; |
| 899 | break; |
| 900 | default: |
| 901 | value = 0; |
| 902 | break; |
| 903 | } |
| 904 | ceu_write(pcdev, CAIFR, value); |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 905 | |
Guennadi Liakhovetski | e622681 | 2010-03-23 11:23:31 -0300 | [diff] [blame] | 906 | sh_mobile_ceu_set_rect(icd); |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 907 | mdelay(1); |
| 908 | |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 909 | dev_geo(icd->parent, "CFLCR 0x%x\n", pcdev->cflcr); |
Guennadi Liakhovetski | 961801b | 2009-08-25 11:46:54 -0300 | [diff] [blame] | 910 | ceu_write(pcdev, CFLCR, pcdev->cflcr); |
Magnus Damm | dd54203 | 2008-10-16 19:50:22 -0300 | [diff] [blame] | 911 | |
Guennadi Liakhovetski | 5d28d52 | 2009-12-11 11:15:05 -0300 | [diff] [blame] | 912 | /* |
| 913 | * A few words about byte order (observed in Big Endian mode) |
Magnus Damm | dd54203 | 2008-10-16 19:50:22 -0300 | [diff] [blame] | 914 | * |
| 915 | * In data fetch mode bytes are received in chunks of 8 bytes. |
| 916 | * D0, D1, D2, D3, D4, D5, D6, D7 (D0 received first) |
| 917 | * |
| 918 | * The data is however by default written to memory in reverse order: |
| 919 | * D7, D6, D5, D4, D3, D2, D1, D0 (D7 written to lowest byte) |
| 920 | * |
| 921 | * The lowest three bits of CDOCR allows us to do swapping, |
Magnus Damm | 2c0a072 | 2008-10-16 19:50:56 -0300 | [diff] [blame] | 922 | * using 7 we swap the data bytes to match the incoming order: |
| 923 | * D0, D1, D2, D3, D4, D5, D6, D7 |
Magnus Damm | dd54203 | 2008-10-16 19:50:22 -0300 | [diff] [blame] | 924 | */ |
Guennadi Liakhovetski | e1db704 | 2011-07-27 18:17:56 -0300 | [diff] [blame] | 925 | value = 0x00000007 | yuv_lineskip; |
Magnus Damm | 8be65dc | 2008-12-18 12:38:06 -0300 | [diff] [blame] | 926 | |
| 927 | ceu_write(pcdev, CDOCR, value); |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 928 | ceu_write(pcdev, CFWCR, 0); /* keep "datafetch firewall" disabled */ |
| 929 | |
Guennadi Liakhovetski | 904078f | 2009-08-25 11:46:52 -0300 | [diff] [blame] | 930 | capture_restore(pcdev, capsr); |
| 931 | |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 932 | /* not in bundle mode: skip CBDSR, CDAYR2, CDACR2, CDBYR2, CDBCR2 */ |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 933 | return 0; |
| 934 | } |
| 935 | |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 936 | static int sh_mobile_ceu_try_bus_param(struct soc_camera_device *icd, |
| 937 | unsigned char buswidth) |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 938 | { |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 939 | struct soc_camera_host *ici = to_soc_camera_host(icd->parent); |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 940 | struct sh_mobile_ceu_dev *pcdev = ici->priv; |
Guennadi Liakhovetski | e1db704 | 2011-07-27 18:17:56 -0300 | [diff] [blame] | 941 | struct v4l2_subdev *sd = find_bus_subdev(pcdev, icd); |
| 942 | unsigned long common_flags = CEU_BUS_FLAGS; |
| 943 | struct v4l2_mbus_config cfg = {.type = V4L2_MBUS_PARALLEL,}; |
| 944 | int ret; |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 945 | |
Guennadi Liakhovetski | e1db704 | 2011-07-27 18:17:56 -0300 | [diff] [blame] | 946 | ret = v4l2_subdev_call(sd, video, g_mbus_config, &cfg); |
| 947 | if (!ret) |
| 948 | common_flags = soc_mbus_config_compatible(&cfg, |
| 949 | common_flags); |
| 950 | else if (ret != -ENOIOCTLCMD) |
| 951 | return ret; |
| 952 | |
| 953 | if (!common_flags || buswidth > 16) |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 954 | return -EINVAL; |
| 955 | |
| 956 | return 0; |
| 957 | } |
| 958 | |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 959 | static const struct soc_mbus_pixelfmt sh_mobile_ceu_formats[] = { |
Magnus Damm | 8be65dc | 2008-12-18 12:38:06 -0300 | [diff] [blame] | 960 | { |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 961 | .fourcc = V4L2_PIX_FMT_NV12, |
| 962 | .name = "NV12", |
Guennadi Liakhovetski | e1db704 | 2011-07-27 18:17:56 -0300 | [diff] [blame] | 963 | .bits_per_sample = 8, |
| 964 | .packing = SOC_MBUS_PACKING_1_5X8, |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 965 | .order = SOC_MBUS_ORDER_LE, |
Laurent Pinchart | ad3b81f | 2012-03-21 08:03:23 -0300 | [diff] [blame] | 966 | .layout = SOC_MBUS_LAYOUT_PLANAR_2Y_C, |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 967 | }, { |
| 968 | .fourcc = V4L2_PIX_FMT_NV21, |
| 969 | .name = "NV21", |
Guennadi Liakhovetski | e1db704 | 2011-07-27 18:17:56 -0300 | [diff] [blame] | 970 | .bits_per_sample = 8, |
| 971 | .packing = SOC_MBUS_PACKING_1_5X8, |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 972 | .order = SOC_MBUS_ORDER_LE, |
Laurent Pinchart | ad3b81f | 2012-03-21 08:03:23 -0300 | [diff] [blame] | 973 | .layout = SOC_MBUS_LAYOUT_PLANAR_2Y_C, |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 974 | }, { |
| 975 | .fourcc = V4L2_PIX_FMT_NV16, |
| 976 | .name = "NV16", |
Guennadi Liakhovetski | e1db704 | 2011-07-27 18:17:56 -0300 | [diff] [blame] | 977 | .bits_per_sample = 8, |
| 978 | .packing = SOC_MBUS_PACKING_2X8_PADHI, |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 979 | .order = SOC_MBUS_ORDER_LE, |
Laurent Pinchart | ad3b81f | 2012-03-21 08:03:23 -0300 | [diff] [blame] | 980 | .layout = SOC_MBUS_LAYOUT_PLANAR_Y_C, |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 981 | }, { |
| 982 | .fourcc = V4L2_PIX_FMT_NV61, |
| 983 | .name = "NV61", |
Guennadi Liakhovetski | e1db704 | 2011-07-27 18:17:56 -0300 | [diff] [blame] | 984 | .bits_per_sample = 8, |
| 985 | .packing = SOC_MBUS_PACKING_2X8_PADHI, |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 986 | .order = SOC_MBUS_ORDER_LE, |
Laurent Pinchart | ad3b81f | 2012-03-21 08:03:23 -0300 | [diff] [blame] | 987 | .layout = SOC_MBUS_LAYOUT_PLANAR_Y_C, |
Magnus Damm | 9e4a56d | 2008-12-18 12:45:33 -0300 | [diff] [blame] | 988 | }, |
Magnus Damm | 8be65dc | 2008-12-18 12:38:06 -0300 | [diff] [blame] | 989 | }; |
| 990 | |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 991 | /* This will be corrected as we get more formats */ |
| 992 | static bool sh_mobile_ceu_packing_supported(const struct soc_mbus_pixelfmt *fmt) |
| 993 | { |
| 994 | return fmt->packing == SOC_MBUS_PACKING_NONE || |
| 995 | (fmt->bits_per_sample == 8 && |
Guennadi Liakhovetski | e1db704 | 2011-07-27 18:17:56 -0300 | [diff] [blame] | 996 | fmt->packing == SOC_MBUS_PACKING_1_5X8) || |
| 997 | (fmt->bits_per_sample == 8 && |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 998 | fmt->packing == SOC_MBUS_PACKING_2X8_PADHI) || |
| 999 | (fmt->bits_per_sample > 8 && |
| 1000 | fmt->packing == SOC_MBUS_PACKING_EXTEND16); |
| 1001 | } |
| 1002 | |
Guennadi Liakhovetski | e622681 | 2010-03-23 11:23:31 -0300 | [diff] [blame] | 1003 | static int client_g_rect(struct v4l2_subdev *sd, struct v4l2_rect *rect); |
| 1004 | |
Hans Verkuil | d34bfcd2 | 2011-09-05 17:07:47 -0300 | [diff] [blame] | 1005 | static struct soc_camera_device *ctrl_to_icd(struct v4l2_ctrl *ctrl) |
| 1006 | { |
| 1007 | return container_of(ctrl->handler, struct soc_camera_device, |
| 1008 | ctrl_handler); |
| 1009 | } |
| 1010 | |
| 1011 | static int sh_mobile_ceu_s_ctrl(struct v4l2_ctrl *ctrl) |
| 1012 | { |
| 1013 | struct soc_camera_device *icd = ctrl_to_icd(ctrl); |
| 1014 | struct soc_camera_host *ici = to_soc_camera_host(icd->parent); |
| 1015 | struct sh_mobile_ceu_dev *pcdev = ici->priv; |
| 1016 | |
| 1017 | switch (ctrl->id) { |
| 1018 | case V4L2_CID_SHARPNESS: |
| 1019 | switch (icd->current_fmt->host_fmt->fourcc) { |
| 1020 | case V4L2_PIX_FMT_NV12: |
| 1021 | case V4L2_PIX_FMT_NV21: |
| 1022 | case V4L2_PIX_FMT_NV16: |
| 1023 | case V4L2_PIX_FMT_NV61: |
| 1024 | ceu_write(pcdev, CLFCR, !ctrl->val); |
| 1025 | return 0; |
| 1026 | } |
| 1027 | break; |
| 1028 | } |
| 1029 | |
| 1030 | return -EINVAL; |
| 1031 | } |
| 1032 | |
| 1033 | static const struct v4l2_ctrl_ops sh_mobile_ceu_ctrl_ops = { |
| 1034 | .s_ctrl = sh_mobile_ceu_s_ctrl, |
| 1035 | }; |
| 1036 | |
Hans Verkuil | 3805f20 | 2010-05-08 17:55:00 -0300 | [diff] [blame] | 1037 | 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] | 1038 | struct soc_camera_format_xlate *xlate) |
| 1039 | { |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 1040 | struct v4l2_subdev *sd = soc_camera_to_subdev(icd); |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 1041 | struct device *dev = icd->parent; |
Guennadi Liakhovetski | b3b5020 | 2010-07-26 12:13:34 -0300 | [diff] [blame] | 1042 | struct soc_camera_host *ici = to_soc_camera_host(dev); |
| 1043 | struct sh_mobile_ceu_dev *pcdev = ici->priv; |
Magnus Damm | 8be65dc | 2008-12-18 12:38:06 -0300 | [diff] [blame] | 1044 | int ret, k, n; |
Magnus Damm | 9414de3 | 2008-12-18 11:49:06 -0300 | [diff] [blame] | 1045 | int formats = 0; |
Guennadi Liakhovetski | 904078f | 2009-08-25 11:46:52 -0300 | [diff] [blame] | 1046 | struct sh_mobile_ceu_cam *cam; |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 1047 | enum v4l2_mbus_pixelcode code; |
| 1048 | const struct soc_mbus_pixelfmt *fmt; |
Magnus Damm | 9414de3 | 2008-12-18 11:49:06 -0300 | [diff] [blame] | 1049 | |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 1050 | ret = v4l2_subdev_call(sd, video, enum_mbus_fmt, idx, &code); |
| 1051 | if (ret < 0) |
| 1052 | /* No more formats */ |
| 1053 | return 0; |
| 1054 | |
| 1055 | fmt = soc_mbus_get_fmtdesc(code); |
| 1056 | if (!fmt) { |
Guennadi Liakhovetski | d2dcad4 | 2011-05-18 06:49:54 -0300 | [diff] [blame] | 1057 | dev_warn(dev, "unsupported format code #%u: %d\n", idx, code); |
| 1058 | return 0; |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 1059 | } |
| 1060 | |
Guennadi Liakhovetski | 6b526fe | 2011-07-01 11:19:58 -0300 | [diff] [blame] | 1061 | if (!pcdev->pdata->csi2) { |
Guennadi Liakhovetski | e1db704 | 2011-07-27 18:17:56 -0300 | [diff] [blame] | 1062 | /* Are there any restrictions in the CSI-2 case? */ |
Guennadi Liakhovetski | b3b5020 | 2010-07-26 12:13:34 -0300 | [diff] [blame] | 1063 | ret = sh_mobile_ceu_try_bus_param(icd, fmt->bits_per_sample); |
| 1064 | if (ret < 0) |
| 1065 | return 0; |
| 1066 | } |
Magnus Damm | 9414de3 | 2008-12-18 11:49:06 -0300 | [diff] [blame] | 1067 | |
Guennadi Liakhovetski | 904078f | 2009-08-25 11:46:52 -0300 | [diff] [blame] | 1068 | if (!icd->host_priv) { |
Guennadi Liakhovetski | e622681 | 2010-03-23 11:23:31 -0300 | [diff] [blame] | 1069 | struct v4l2_mbus_framefmt mf; |
| 1070 | struct v4l2_rect rect; |
Guennadi Liakhovetski | e622681 | 2010-03-23 11:23:31 -0300 | [diff] [blame] | 1071 | int shift = 0; |
| 1072 | |
Hans Verkuil | d34bfcd2 | 2011-09-05 17:07:47 -0300 | [diff] [blame] | 1073 | /* Add our control */ |
| 1074 | v4l2_ctrl_new_std(&icd->ctrl_handler, &sh_mobile_ceu_ctrl_ops, |
| 1075 | V4L2_CID_SHARPNESS, 0, 1, 1, 0); |
| 1076 | if (icd->ctrl_handler.error) |
| 1077 | return icd->ctrl_handler.error; |
| 1078 | |
Guennadi Liakhovetski | e622681 | 2010-03-23 11:23:31 -0300 | [diff] [blame] | 1079 | /* FIXME: subwindow is lost between close / open */ |
| 1080 | |
| 1081 | /* Cache current client geometry */ |
| 1082 | ret = client_g_rect(sd, &rect); |
| 1083 | if (ret < 0) |
| 1084 | return ret; |
| 1085 | |
| 1086 | /* First time */ |
| 1087 | ret = v4l2_subdev_call(sd, video, g_mbus_fmt, &mf); |
| 1088 | if (ret < 0) |
| 1089 | return ret; |
| 1090 | |
Guennadi Liakhovetski | 48e971c | 2012-03-14 08:37:03 -0300 | [diff] [blame] | 1091 | /* |
| 1092 | * All currently existing CEU implementations support 2560x1920 |
| 1093 | * or larger frames. If the sensor is proposing too big a frame, |
| 1094 | * don't bother with possibly supportred by the CEU larger |
| 1095 | * sizes, just try VGA multiples. If needed, this can be |
| 1096 | * adjusted in the future. |
| 1097 | */ |
| 1098 | while ((mf.width > pcdev->max_width || |
| 1099 | mf.height > pcdev->max_height) && shift < 4) { |
Guennadi Liakhovetski | e622681 | 2010-03-23 11:23:31 -0300 | [diff] [blame] | 1100 | /* Try 2560x1920, 1280x960, 640x480, 320x240 */ |
| 1101 | mf.width = 2560 >> shift; |
| 1102 | mf.height = 1920 >> shift; |
Guennadi Liakhovetski | 4c0b036 | 2011-12-05 16:01:13 -0300 | [diff] [blame] | 1103 | ret = v4l2_device_call_until_err(sd->v4l2_dev, |
| 1104 | soc_camera_grp_id(icd), video, |
| 1105 | s_mbus_fmt, &mf); |
Guennadi Liakhovetski | e622681 | 2010-03-23 11:23:31 -0300 | [diff] [blame] | 1106 | if (ret < 0) |
| 1107 | return ret; |
| 1108 | shift++; |
| 1109 | } |
| 1110 | |
| 1111 | if (shift == 4) { |
| 1112 | dev_err(dev, "Failed to configure the client below %ux%x\n", |
| 1113 | mf.width, mf.height); |
| 1114 | return -EIO; |
| 1115 | } |
| 1116 | |
| 1117 | dev_geo(dev, "camera fmt %ux%u\n", mf.width, mf.height); |
| 1118 | |
Guennadi Liakhovetski | 904078f | 2009-08-25 11:46:52 -0300 | [diff] [blame] | 1119 | cam = kzalloc(sizeof(*cam), GFP_KERNEL); |
| 1120 | if (!cam) |
| 1121 | return -ENOMEM; |
| 1122 | |
Guennadi Liakhovetski | e622681 | 2010-03-23 11:23:31 -0300 | [diff] [blame] | 1123 | /* We are called with current camera crop, initialise subrect with it */ |
| 1124 | cam->rect = rect; |
| 1125 | cam->subrect = rect; |
| 1126 | |
| 1127 | cam->width = mf.width; |
| 1128 | cam->height = mf.height; |
| 1129 | |
Guennadi Liakhovetski | 904078f | 2009-08-25 11:46:52 -0300 | [diff] [blame] | 1130 | icd->host_priv = cam; |
| 1131 | } else { |
| 1132 | cam = icd->host_priv; |
| 1133 | } |
| 1134 | |
Guennadi Liakhovetski | c8329ac | 2009-02-23 12:12:58 -0300 | [diff] [blame] | 1135 | /* Beginning of a pass */ |
| 1136 | if (!idx) |
Guennadi Liakhovetski | 904078f | 2009-08-25 11:46:52 -0300 | [diff] [blame] | 1137 | cam->extra_fmt = NULL; |
Guennadi Liakhovetski | c8329ac | 2009-02-23 12:12:58 -0300 | [diff] [blame] | 1138 | |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 1139 | switch (code) { |
Guennadi Liakhovetski | ace6e97 | 2010-07-22 16:52:51 -0300 | [diff] [blame] | 1140 | case V4L2_MBUS_FMT_UYVY8_2X8: |
| 1141 | case V4L2_MBUS_FMT_VYUY8_2X8: |
| 1142 | case V4L2_MBUS_FMT_YUYV8_2X8: |
| 1143 | case V4L2_MBUS_FMT_YVYU8_2X8: |
Guennadi Liakhovetski | 904078f | 2009-08-25 11:46:52 -0300 | [diff] [blame] | 1144 | if (cam->extra_fmt) |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 1145 | break; |
Guennadi Liakhovetski | c8329ac | 2009-02-23 12:12:58 -0300 | [diff] [blame] | 1146 | |
| 1147 | /* |
| 1148 | * Our case is simple so far: for any of the above four camera |
| 1149 | * formats we add all our four synthesized NV* formats, so, |
| 1150 | * just marking the device with a single flag suffices. If |
| 1151 | * the format generation rules are more complex, you would have |
| 1152 | * to actually hang your already added / counted formats onto |
| 1153 | * the host_priv pointer and check whether the format you're |
| 1154 | * going to add now is already there. |
| 1155 | */ |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 1156 | cam->extra_fmt = sh_mobile_ceu_formats; |
Guennadi Liakhovetski | c8329ac | 2009-02-23 12:12:58 -0300 | [diff] [blame] | 1157 | |
Magnus Damm | 8be65dc | 2008-12-18 12:38:06 -0300 | [diff] [blame] | 1158 | n = ARRAY_SIZE(sh_mobile_ceu_formats); |
| 1159 | formats += n; |
| 1160 | for (k = 0; xlate && k < n; k++) { |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 1161 | xlate->host_fmt = &sh_mobile_ceu_formats[k]; |
| 1162 | xlate->code = code; |
Magnus Damm | 8be65dc | 2008-12-18 12:38:06 -0300 | [diff] [blame] | 1163 | xlate++; |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 1164 | dev_dbg(dev, "Providing format %s using code %d\n", |
| 1165 | sh_mobile_ceu_formats[k].name, code); |
Magnus Damm | 8be65dc | 2008-12-18 12:38:06 -0300 | [diff] [blame] | 1166 | } |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 1167 | break; |
Magnus Damm | 9414de3 | 2008-12-18 11:49:06 -0300 | [diff] [blame] | 1168 | default: |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 1169 | if (!sh_mobile_ceu_packing_supported(fmt)) |
| 1170 | return 0; |
| 1171 | } |
| 1172 | |
| 1173 | /* Generic pass-through */ |
| 1174 | formats++; |
| 1175 | if (xlate) { |
| 1176 | xlate->host_fmt = fmt; |
| 1177 | xlate->code = code; |
| 1178 | xlate++; |
| 1179 | dev_dbg(dev, "Providing format %s in pass-through mode\n", |
Guennadi Liakhovetski | e53a480 | 2010-06-18 04:20:56 -0300 | [diff] [blame] | 1180 | fmt->name); |
Magnus Damm | 9414de3 | 2008-12-18 11:49:06 -0300 | [diff] [blame] | 1181 | } |
| 1182 | |
| 1183 | return formats; |
| 1184 | } |
| 1185 | |
Guennadi Liakhovetski | 904078f | 2009-08-25 11:46:52 -0300 | [diff] [blame] | 1186 | static void sh_mobile_ceu_put_formats(struct soc_camera_device *icd) |
| 1187 | { |
| 1188 | kfree(icd->host_priv); |
| 1189 | icd->host_priv = NULL; |
| 1190 | } |
| 1191 | |
Guennadi Liakhovetski | 961801b | 2009-08-25 11:46:54 -0300 | [diff] [blame] | 1192 | /* 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] | 1193 | static bool is_smaller(struct v4l2_rect *r1, struct v4l2_rect *r2) |
| 1194 | { |
| 1195 | return r1->width < r2->width || r1->height < r2->height; |
| 1196 | } |
| 1197 | |
Guennadi Liakhovetski | 961801b | 2009-08-25 11:46:54 -0300 | [diff] [blame] | 1198 | /* Check if r1 fails to cover r2 */ |
| 1199 | static bool is_inside(struct v4l2_rect *r1, struct v4l2_rect *r2) |
| 1200 | { |
| 1201 | return r1->left > r2->left || r1->top > r2->top || |
| 1202 | r1->left + r1->width < r2->left + r2->width || |
| 1203 | r1->top + r1->height < r2->top + r2->height; |
| 1204 | } |
| 1205 | |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 1206 | static unsigned int scale_down(unsigned int size, unsigned int scale) |
| 1207 | { |
| 1208 | return (size * 4096 + scale / 2) / scale; |
| 1209 | } |
| 1210 | |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 1211 | static unsigned int calc_generic_scale(unsigned int input, unsigned int output) |
| 1212 | { |
| 1213 | return (input * 4096 + output / 2) / output; |
| 1214 | } |
| 1215 | |
Guennadi Liakhovetski | e622681 | 2010-03-23 11:23:31 -0300 | [diff] [blame] | 1216 | /* Get and store current client crop */ |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 1217 | static int client_g_rect(struct v4l2_subdev *sd, struct v4l2_rect *rect) |
| 1218 | { |
| 1219 | struct v4l2_crop crop; |
| 1220 | struct v4l2_cropcap cap; |
| 1221 | int ret; |
| 1222 | |
| 1223 | crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 1224 | |
| 1225 | ret = v4l2_subdev_call(sd, video, g_crop, &crop); |
| 1226 | if (!ret) { |
| 1227 | *rect = crop.c; |
| 1228 | return ret; |
| 1229 | } |
| 1230 | |
| 1231 | /* Camera driver doesn't support .g_crop(), assume default rectangle */ |
| 1232 | cap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 1233 | |
| 1234 | ret = v4l2_subdev_call(sd, video, cropcap, &cap); |
Guennadi Liakhovetski | e622681 | 2010-03-23 11:23:31 -0300 | [diff] [blame] | 1235 | if (!ret) |
| 1236 | *rect = cap.defrect; |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 1237 | |
| 1238 | return ret; |
| 1239 | } |
| 1240 | |
Guennadi Liakhovetski | e622681 | 2010-03-23 11:23:31 -0300 | [diff] [blame] | 1241 | /* Client crop has changed, update our sub-rectangle to remain within the area */ |
| 1242 | static void update_subrect(struct sh_mobile_ceu_cam *cam) |
| 1243 | { |
| 1244 | struct v4l2_rect *rect = &cam->rect, *subrect = &cam->subrect; |
| 1245 | |
| 1246 | if (rect->width < subrect->width) |
| 1247 | subrect->width = rect->width; |
| 1248 | |
| 1249 | if (rect->height < subrect->height) |
| 1250 | subrect->height = rect->height; |
| 1251 | |
| 1252 | if (rect->left > subrect->left) |
| 1253 | subrect->left = rect->left; |
| 1254 | else if (rect->left + rect->width > |
| 1255 | subrect->left + subrect->width) |
| 1256 | subrect->left = rect->left + rect->width - |
| 1257 | subrect->width; |
| 1258 | |
| 1259 | if (rect->top > subrect->top) |
| 1260 | subrect->top = rect->top; |
| 1261 | else if (rect->top + rect->height > |
| 1262 | subrect->top + subrect->height) |
| 1263 | subrect->top = rect->top + rect->height - |
| 1264 | subrect->height; |
| 1265 | } |
| 1266 | |
Guennadi Liakhovetski | 961801b | 2009-08-25 11:46:54 -0300 | [diff] [blame] | 1267 | /* |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 1268 | * The common for both scaling and cropping iterative approach is: |
Guennadi Liakhovetski | 961801b | 2009-08-25 11:46:54 -0300 | [diff] [blame] | 1269 | * 1. try if the client can produce exactly what requested by the user |
| 1270 | * 2. if (1) failed, try to double the client image until we get one big enough |
| 1271 | * 3. if (2) failed, try to request the maximum image |
| 1272 | */ |
Guennadi Liakhovetski | e622681 | 2010-03-23 11:23:31 -0300 | [diff] [blame] | 1273 | 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] | 1274 | struct v4l2_crop *cam_crop) |
| 1275 | { |
Guennadi Liakhovetski | e622681 | 2010-03-23 11:23:31 -0300 | [diff] [blame] | 1276 | struct v4l2_subdev *sd = soc_camera_to_subdev(icd); |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 1277 | struct v4l2_rect *rect = &crop->c, *cam_rect = &cam_crop->c; |
| 1278 | struct device *dev = sd->v4l2_dev->dev; |
Guennadi Liakhovetski | e622681 | 2010-03-23 11:23:31 -0300 | [diff] [blame] | 1279 | struct sh_mobile_ceu_cam *cam = icd->host_priv; |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 1280 | struct v4l2_cropcap cap; |
| 1281 | int ret; |
| 1282 | unsigned int width, height; |
| 1283 | |
| 1284 | v4l2_subdev_call(sd, video, s_crop, crop); |
| 1285 | ret = client_g_rect(sd, cam_rect); |
| 1286 | if (ret < 0) |
| 1287 | return ret; |
| 1288 | |
| 1289 | /* |
| 1290 | * Now cam_crop contains the current camera input rectangle, and it must |
| 1291 | * be within camera cropcap bounds |
| 1292 | */ |
| 1293 | if (!memcmp(rect, cam_rect, sizeof(*rect))) { |
| 1294 | /* Even if camera S_CROP failed, but camera rectangle matches */ |
Márton Németh | e26b314 | 2010-02-24 17:13:29 -0300 | [diff] [blame] | 1295 | 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] | 1296 | rect->width, rect->height, rect->left, rect->top); |
Guennadi Liakhovetski | e622681 | 2010-03-23 11:23:31 -0300 | [diff] [blame] | 1297 | cam->rect = *cam_rect; |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 1298 | return 0; |
| 1299 | } |
| 1300 | |
| 1301 | /* 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] | 1302 | 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] | 1303 | cam_rect->width, cam_rect->height, |
| 1304 | cam_rect->left, cam_rect->top, |
| 1305 | rect->width, rect->height, rect->left, rect->top); |
| 1306 | |
| 1307 | /* We need sensor maximum rectangle */ |
| 1308 | ret = v4l2_subdev_call(sd, video, cropcap, &cap); |
| 1309 | if (ret < 0) |
| 1310 | return ret; |
| 1311 | |
Guennadi Liakhovetski | e622681 | 2010-03-23 11:23:31 -0300 | [diff] [blame] | 1312 | /* Put user requested rectangle within sensor bounds */ |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 1313 | soc_camera_limit_side(&rect->left, &rect->width, cap.bounds.left, 2, |
| 1314 | cap.bounds.width); |
| 1315 | soc_camera_limit_side(&rect->top, &rect->height, cap.bounds.top, 4, |
| 1316 | cap.bounds.height); |
| 1317 | |
| 1318 | /* |
| 1319 | * Popular special case - some cameras can only handle fixed sizes like |
| 1320 | * QVGA, VGA,... Take care to avoid infinite loop. |
| 1321 | */ |
| 1322 | width = max(cam_rect->width, 2); |
| 1323 | height = max(cam_rect->height, 2); |
| 1324 | |
Guennadi Liakhovetski | e622681 | 2010-03-23 11:23:31 -0300 | [diff] [blame] | 1325 | /* |
| 1326 | * Loop as long as sensor is not covering the requested rectangle and |
| 1327 | * is still within its bounds |
| 1328 | */ |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 1329 | while (!ret && (is_smaller(cam_rect, rect) || |
| 1330 | is_inside(cam_rect, rect)) && |
| 1331 | (cap.bounds.width > width || cap.bounds.height > height)) { |
| 1332 | |
| 1333 | width *= 2; |
| 1334 | height *= 2; |
| 1335 | |
| 1336 | cam_rect->width = width; |
| 1337 | cam_rect->height = height; |
| 1338 | |
| 1339 | /* |
| 1340 | * We do not know what capabilities the camera has to set up |
| 1341 | * left and top borders. We could try to be smarter in iterating |
| 1342 | * them, e.g., if camera current left is to the right of the |
| 1343 | * target left, set it to the middle point between the current |
| 1344 | * left and minimum left. But that would add too much |
| 1345 | * complexity: we would have to iterate each border separately. |
Guennadi Liakhovetski | e622681 | 2010-03-23 11:23:31 -0300 | [diff] [blame] | 1346 | * Instead we just drop to the left and top bounds. |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 1347 | */ |
| 1348 | if (cam_rect->left > rect->left) |
| 1349 | cam_rect->left = cap.bounds.left; |
| 1350 | |
| 1351 | if (cam_rect->left + cam_rect->width < rect->left + rect->width) |
| 1352 | cam_rect->width = rect->left + rect->width - |
| 1353 | cam_rect->left; |
| 1354 | |
| 1355 | if (cam_rect->top > rect->top) |
| 1356 | cam_rect->top = cap.bounds.top; |
| 1357 | |
| 1358 | if (cam_rect->top + cam_rect->height < rect->top + rect->height) |
| 1359 | cam_rect->height = rect->top + rect->height - |
| 1360 | cam_rect->top; |
| 1361 | |
| 1362 | v4l2_subdev_call(sd, video, s_crop, cam_crop); |
| 1363 | ret = client_g_rect(sd, cam_rect); |
Márton Németh | e26b314 | 2010-02-24 17:13:29 -0300 | [diff] [blame] | 1364 | 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] | 1365 | cam_rect->width, cam_rect->height, |
| 1366 | cam_rect->left, cam_rect->top); |
| 1367 | } |
| 1368 | |
| 1369 | /* S_CROP must not modify the rectangle */ |
| 1370 | if (is_smaller(cam_rect, rect) || is_inside(cam_rect, rect)) { |
| 1371 | /* |
| 1372 | * The camera failed to configure a suitable cropping, |
| 1373 | * we cannot use the current rectangle, set to max |
| 1374 | */ |
| 1375 | *cam_rect = cap.bounds; |
| 1376 | v4l2_subdev_call(sd, video, s_crop, cam_crop); |
| 1377 | ret = client_g_rect(sd, cam_rect); |
Márton Németh | e26b314 | 2010-02-24 17:13:29 -0300 | [diff] [blame] | 1378 | 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] | 1379 | cam_rect->width, cam_rect->height, |
| 1380 | cam_rect->left, cam_rect->top); |
| 1381 | } |
| 1382 | |
Guennadi Liakhovetski | e622681 | 2010-03-23 11:23:31 -0300 | [diff] [blame] | 1383 | if (!ret) { |
| 1384 | cam->rect = *cam_rect; |
| 1385 | update_subrect(cam); |
| 1386 | } |
| 1387 | |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 1388 | return ret; |
| 1389 | } |
| 1390 | |
Guennadi Liakhovetski | e622681 | 2010-03-23 11:23:31 -0300 | [diff] [blame] | 1391 | /* Iterative s_mbus_fmt, also updates cached client crop on success */ |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 1392 | static int client_s_fmt(struct soc_camera_device *icd, |
| 1393 | struct v4l2_mbus_framefmt *mf, bool ceu_can_scale) |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 1394 | { |
Guennadi Liakhovetski | 48e971c | 2012-03-14 08:37:03 -0300 | [diff] [blame] | 1395 | struct soc_camera_host *ici = to_soc_camera_host(icd->parent); |
| 1396 | struct sh_mobile_ceu_dev *pcdev = ici->priv; |
Guennadi Liakhovetski | e622681 | 2010-03-23 11:23:31 -0300 | [diff] [blame] | 1397 | struct sh_mobile_ceu_cam *cam = icd->host_priv; |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 1398 | struct v4l2_subdev *sd = soc_camera_to_subdev(icd); |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 1399 | struct device *dev = icd->parent; |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 1400 | unsigned int width = mf->width, height = mf->height, tmp_w, tmp_h; |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 1401 | unsigned int max_width, max_height; |
| 1402 | struct v4l2_cropcap cap; |
Guennadi Liakhovetski | 3698874 | 2011-07-22 09:44:40 -0300 | [diff] [blame] | 1403 | bool ceu_1to1; |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 1404 | int ret; |
| 1405 | |
Guennadi Liakhovetski | 4c0b036 | 2011-12-05 16:01:13 -0300 | [diff] [blame] | 1406 | ret = v4l2_device_call_until_err(sd->v4l2_dev, |
| 1407 | soc_camera_grp_id(icd), video, |
Guennadi Liakhovetski | b3b5020 | 2010-07-26 12:13:34 -0300 | [diff] [blame] | 1408 | s_mbus_fmt, mf); |
Guennadi Liakhovetski | e622681 | 2010-03-23 11:23:31 -0300 | [diff] [blame] | 1409 | if (ret < 0) |
| 1410 | return ret; |
| 1411 | |
| 1412 | dev_geo(dev, "camera scaled to %ux%u\n", mf->width, mf->height); |
| 1413 | |
Guennadi Liakhovetski | 3698874 | 2011-07-22 09:44:40 -0300 | [diff] [blame] | 1414 | if (width == mf->width && height == mf->height) { |
| 1415 | /* Perfect! The client has done it all. */ |
| 1416 | ceu_1to1 = true; |
| 1417 | goto update_cache; |
| 1418 | } |
| 1419 | |
| 1420 | ceu_1to1 = false; |
| 1421 | if (!ceu_can_scale) |
Guennadi Liakhovetski | e622681 | 2010-03-23 11:23:31 -0300 | [diff] [blame] | 1422 | goto update_cache; |
| 1423 | |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 1424 | cap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 1425 | |
| 1426 | ret = v4l2_subdev_call(sd, video, cropcap, &cap); |
| 1427 | if (ret < 0) |
| 1428 | return ret; |
| 1429 | |
Guennadi Liakhovetski | 48e971c | 2012-03-14 08:37:03 -0300 | [diff] [blame] | 1430 | max_width = min(cap.bounds.width, pcdev->max_width); |
| 1431 | max_height = min(cap.bounds.height, pcdev->max_height); |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 1432 | |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 1433 | /* Camera set a format, but geometry is not precise, try to improve */ |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 1434 | tmp_w = mf->width; |
| 1435 | tmp_h = mf->height; |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 1436 | |
| 1437 | /* width <= max_width && height <= max_height - guaranteed by try_fmt */ |
| 1438 | while ((width > tmp_w || height > tmp_h) && |
| 1439 | tmp_w < max_width && tmp_h < max_height) { |
| 1440 | tmp_w = min(2 * tmp_w, max_width); |
| 1441 | tmp_h = min(2 * tmp_h, max_height); |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 1442 | mf->width = tmp_w; |
| 1443 | mf->height = tmp_h; |
Guennadi Liakhovetski | 4c0b036 | 2011-12-05 16:01:13 -0300 | [diff] [blame] | 1444 | ret = v4l2_device_call_until_err(sd->v4l2_dev, |
| 1445 | soc_camera_grp_id(icd), video, |
| 1446 | s_mbus_fmt, mf); |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 1447 | dev_geo(dev, "Camera scaled to %ux%u\n", |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 1448 | mf->width, mf->height); |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 1449 | if (ret < 0) { |
| 1450 | /* This shouldn't happen */ |
| 1451 | dev_err(dev, "Client failed to set format: %d\n", ret); |
| 1452 | return ret; |
| 1453 | } |
| 1454 | } |
| 1455 | |
Guennadi Liakhovetski | e622681 | 2010-03-23 11:23:31 -0300 | [diff] [blame] | 1456 | update_cache: |
| 1457 | /* Update cache */ |
| 1458 | ret = client_g_rect(sd, &cam->rect); |
| 1459 | if (ret < 0) |
| 1460 | return ret; |
| 1461 | |
Guennadi Liakhovetski | 3698874 | 2011-07-22 09:44:40 -0300 | [diff] [blame] | 1462 | if (ceu_1to1) |
| 1463 | cam->subrect = cam->rect; |
| 1464 | else |
| 1465 | update_subrect(cam); |
Guennadi Liakhovetski | e622681 | 2010-03-23 11:23:31 -0300 | [diff] [blame] | 1466 | |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 1467 | return 0; |
| 1468 | } |
| 1469 | |
| 1470 | /** |
Guennadi Liakhovetski | e622681 | 2010-03-23 11:23:31 -0300 | [diff] [blame] | 1471 | * @width - on output: user width, mapped back to input |
| 1472 | * @height - on output: user height, mapped back to input |
| 1473 | * @mf - in- / output camera output window |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 1474 | */ |
Guennadi Liakhovetski | e622681 | 2010-03-23 11:23:31 -0300 | [diff] [blame] | 1475 | static int client_scale(struct soc_camera_device *icd, |
| 1476 | struct v4l2_mbus_framefmt *mf, |
| 1477 | unsigned int *width, unsigned int *height, |
| 1478 | bool ceu_can_scale) |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 1479 | { |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 1480 | struct sh_mobile_ceu_cam *cam = icd->host_priv; |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 1481 | struct device *dev = icd->parent; |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 1482 | struct v4l2_mbus_framefmt mf_tmp = *mf; |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 1483 | unsigned int scale_h, scale_v; |
| 1484 | int ret; |
| 1485 | |
Guennadi Liakhovetski | e622681 | 2010-03-23 11:23:31 -0300 | [diff] [blame] | 1486 | /* |
| 1487 | * 5. Apply iterative camera S_FMT for camera user window (also updates |
| 1488 | * client crop cache and the imaginary sub-rectangle). |
| 1489 | */ |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 1490 | ret = client_s_fmt(icd, &mf_tmp, ceu_can_scale); |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 1491 | if (ret < 0) |
| 1492 | return ret; |
| 1493 | |
| 1494 | dev_geo(dev, "5: camera scaled to %ux%u\n", |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 1495 | mf_tmp.width, mf_tmp.height); |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 1496 | |
| 1497 | /* 6. Retrieve camera output window (g_fmt) */ |
| 1498 | |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 1499 | /* unneeded - it is already in "mf_tmp" */ |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 1500 | |
Guennadi Liakhovetski | e622681 | 2010-03-23 11:23:31 -0300 | [diff] [blame] | 1501 | /* 7. Calculate new client scales. */ |
| 1502 | scale_h = calc_generic_scale(cam->rect.width, mf_tmp.width); |
| 1503 | scale_v = calc_generic_scale(cam->rect.height, mf_tmp.height); |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 1504 | |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 1505 | mf->width = mf_tmp.width; |
| 1506 | mf->height = mf_tmp.height; |
| 1507 | mf->colorspace = mf_tmp.colorspace; |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 1508 | |
| 1509 | /* |
| 1510 | * 8. Calculate new CEU crop - apply camera scales to previously |
Guennadi Liakhovetski | e622681 | 2010-03-23 11:23:31 -0300 | [diff] [blame] | 1511 | * updated "effective" crop. |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 1512 | */ |
Guennadi Liakhovetski | e622681 | 2010-03-23 11:23:31 -0300 | [diff] [blame] | 1513 | *width = scale_down(cam->subrect.width, scale_h); |
| 1514 | *height = scale_down(cam->subrect.height, scale_v); |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 1515 | |
Guennadi Liakhovetski | e622681 | 2010-03-23 11:23:31 -0300 | [diff] [blame] | 1516 | 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] | 1517 | |
| 1518 | return 0; |
| 1519 | } |
| 1520 | |
| 1521 | /* |
| 1522 | * CEU can scale and crop, but we don't want to waste bandwidth and kill the |
| 1523 | * framerate by always requesting the maximum image from the client. See |
Guennadi Liakhovetski | 3dac322 | 2011-03-28 13:39:16 -0300 | [diff] [blame] | 1524 | * Documentation/video4linux/sh_mobile_ceu_camera.txt for a description of |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 1525 | * scaling and cropping algorithms and for the meaning of referenced here steps. |
| 1526 | */ |
Guennadi Liakhovetski | 09e231b | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 1527 | static int sh_mobile_ceu_set_crop(struct soc_camera_device *icd, |
Guennadi Liakhovetski | 08590b9 | 2009-08-25 11:46:54 -0300 | [diff] [blame] | 1528 | struct v4l2_crop *a) |
Guennadi Liakhovetski | 09e231b | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 1529 | { |
Guennadi Liakhovetski | 08590b9 | 2009-08-25 11:46:54 -0300 | [diff] [blame] | 1530 | struct v4l2_rect *rect = &a->c; |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 1531 | struct device *dev = icd->parent; |
| 1532 | struct soc_camera_host *ici = to_soc_camera_host(dev); |
Guennadi Liakhovetski | 904078f | 2009-08-25 11:46:52 -0300 | [diff] [blame] | 1533 | struct sh_mobile_ceu_dev *pcdev = ici->priv; |
Guennadi Liakhovetski | 08590b9 | 2009-08-25 11:46:54 -0300 | [diff] [blame] | 1534 | struct v4l2_crop cam_crop; |
Guennadi Liakhovetski | 904078f | 2009-08-25 11:46:52 -0300 | [diff] [blame] | 1535 | struct sh_mobile_ceu_cam *cam = icd->host_priv; |
Guennadi Liakhovetski | e622681 | 2010-03-23 11:23:31 -0300 | [diff] [blame] | 1536 | struct v4l2_rect *cam_rect = &cam_crop.c; |
Guennadi Liakhovetski | c9c1f1c | 2009-08-25 11:46:59 -0300 | [diff] [blame] | 1537 | struct v4l2_subdev *sd = soc_camera_to_subdev(icd); |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 1538 | struct v4l2_mbus_framefmt mf; |
Guennadi Liakhovetski | e622681 | 2010-03-23 11:23:31 -0300 | [diff] [blame] | 1539 | unsigned int scale_cam_h, scale_cam_v, scale_ceu_h, scale_ceu_v, |
Guennadi Liakhovetski | 787d0f9 | 2011-02-16 18:42:24 -0300 | [diff] [blame] | 1540 | out_width, out_height; |
Guennadi Liakhovetski | e622681 | 2010-03-23 11:23:31 -0300 | [diff] [blame] | 1541 | int interm_width, interm_height; |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 1542 | u32 capsr, cflcr; |
Guennadi Liakhovetski | 904078f | 2009-08-25 11:46:52 -0300 | [diff] [blame] | 1543 | int ret; |
| 1544 | |
Guennadi Liakhovetski | e622681 | 2010-03-23 11:23:31 -0300 | [diff] [blame] | 1545 | dev_geo(dev, "S_CROP(%ux%u@%u:%u)\n", rect->width, rect->height, |
| 1546 | rect->left, rect->top); |
| 1547 | |
| 1548 | /* During camera cropping its output window can change too, stop CEU */ |
| 1549 | capsr = capture_save_reset(pcdev); |
| 1550 | dev_dbg(dev, "CAPSR 0x%x, CFLCR 0x%x\n", capsr, pcdev->cflcr); |
| 1551 | |
Guennadi Liakhovetski | eec5ce0 | 2011-07-22 07:43:15 -0300 | [diff] [blame] | 1552 | /* |
| 1553 | * 1. - 2. Apply iterative camera S_CROP for new input window, read back |
| 1554 | * actual camera rectangle. |
| 1555 | */ |
Guennadi Liakhovetski | e622681 | 2010-03-23 11:23:31 -0300 | [diff] [blame] | 1556 | ret = client_s_crop(icd, a, &cam_crop); |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 1557 | if (ret < 0) |
| 1558 | return ret; |
Guennadi Liakhovetski | 904078f | 2009-08-25 11:46:52 -0300 | [diff] [blame] | 1559 | |
Guennadi Liakhovetski | e622681 | 2010-03-23 11:23:31 -0300 | [diff] [blame] | 1560 | 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] | 1561 | cam_rect->width, cam_rect->height, |
| 1562 | cam_rect->left, cam_rect->top); |
| 1563 | |
| 1564 | /* On success cam_crop contains current camera crop */ |
| 1565 | |
Guennadi Liakhovetski | e622681 | 2010-03-23 11:23:31 -0300 | [diff] [blame] | 1566 | /* 3. Retrieve camera output window */ |
| 1567 | ret = v4l2_subdev_call(sd, video, g_mbus_fmt, &mf); |
| 1568 | if (ret < 0) |
| 1569 | return ret; |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 1570 | |
Guennadi Liakhovetski | 48e971c | 2012-03-14 08:37:03 -0300 | [diff] [blame] | 1571 | if (mf.width > pcdev->max_width || mf.height > pcdev->max_height) |
Guennadi Liakhovetski | e622681 | 2010-03-23 11:23:31 -0300 | [diff] [blame] | 1572 | return -EINVAL; |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 1573 | |
Guennadi Liakhovetski | e622681 | 2010-03-23 11:23:31 -0300 | [diff] [blame] | 1574 | /* 4. Calculate camera scales */ |
| 1575 | scale_cam_h = calc_generic_scale(cam_rect->width, mf.width); |
| 1576 | scale_cam_v = calc_generic_scale(cam_rect->height, mf.height); |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 1577 | |
Guennadi Liakhovetski | e622681 | 2010-03-23 11:23:31 -0300 | [diff] [blame] | 1578 | /* Calculate intermediate window */ |
| 1579 | interm_width = scale_down(rect->width, scale_cam_h); |
| 1580 | interm_height = scale_down(rect->height, scale_cam_v); |
| 1581 | |
Guennadi Liakhovetski | 3dac322 | 2011-03-28 13:39:16 -0300 | [diff] [blame] | 1582 | if (interm_width < icd->user_width) { |
| 1583 | u32 new_scale_h; |
| 1584 | |
| 1585 | new_scale_h = calc_generic_scale(rect->width, icd->user_width); |
| 1586 | |
| 1587 | mf.width = scale_down(cam_rect->width, new_scale_h); |
| 1588 | } |
| 1589 | |
| 1590 | if (interm_height < icd->user_height) { |
| 1591 | u32 new_scale_v; |
| 1592 | |
| 1593 | new_scale_v = calc_generic_scale(rect->height, icd->user_height); |
| 1594 | |
| 1595 | mf.height = scale_down(cam_rect->height, new_scale_v); |
| 1596 | } |
| 1597 | |
| 1598 | if (interm_width < icd->user_width || interm_height < icd->user_height) { |
Guennadi Liakhovetski | 4c0b036 | 2011-12-05 16:01:13 -0300 | [diff] [blame] | 1599 | ret = v4l2_device_call_until_err(sd->v4l2_dev, |
| 1600 | soc_camera_grp_id(icd), video, |
| 1601 | s_mbus_fmt, &mf); |
Guennadi Liakhovetski | 3dac322 | 2011-03-28 13:39:16 -0300 | [diff] [blame] | 1602 | if (ret < 0) |
| 1603 | return ret; |
| 1604 | |
| 1605 | dev_geo(dev, "New camera output %ux%u\n", mf.width, mf.height); |
| 1606 | scale_cam_h = calc_generic_scale(cam_rect->width, mf.width); |
| 1607 | scale_cam_v = calc_generic_scale(cam_rect->height, mf.height); |
| 1608 | interm_width = scale_down(rect->width, scale_cam_h); |
| 1609 | interm_height = scale_down(rect->height, scale_cam_v); |
| 1610 | } |
| 1611 | |
| 1612 | /* Cache camera output window */ |
| 1613 | cam->width = mf.width; |
| 1614 | cam->height = mf.height; |
| 1615 | |
Guennadi Liakhovetski | e622681 | 2010-03-23 11:23:31 -0300 | [diff] [blame] | 1616 | if (pcdev->image_mode) { |
| 1617 | out_width = min(interm_width, icd->user_width); |
| 1618 | out_height = min(interm_height, icd->user_height); |
| 1619 | } else { |
| 1620 | out_width = interm_width; |
| 1621 | out_height = interm_height; |
| 1622 | } |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 1623 | |
| 1624 | /* |
Guennadi Liakhovetski | e622681 | 2010-03-23 11:23:31 -0300 | [diff] [blame] | 1625 | * 5. Calculate CEU scales from camera scales from results of (5) and |
| 1626 | * the user window |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 1627 | */ |
Guennadi Liakhovetski | e622681 | 2010-03-23 11:23:31 -0300 | [diff] [blame] | 1628 | scale_ceu_h = calc_scale(interm_width, &out_width); |
| 1629 | scale_ceu_v = calc_scale(interm_height, &out_height); |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 1630 | |
Guennadi Liakhovetski | e622681 | 2010-03-23 11:23:31 -0300 | [diff] [blame] | 1631 | 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] | 1632 | |
Guennadi Liakhovetski | e622681 | 2010-03-23 11:23:31 -0300 | [diff] [blame] | 1633 | /* Apply CEU scales. */ |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 1634 | cflcr = scale_ceu_h | (scale_ceu_v << 16); |
| 1635 | if (cflcr != pcdev->cflcr) { |
| 1636 | pcdev->cflcr = cflcr; |
| 1637 | ceu_write(pcdev, CFLCR, cflcr); |
Guennadi Liakhovetski | 904078f | 2009-08-25 11:46:52 -0300 | [diff] [blame] | 1638 | } |
| 1639 | |
Guennadi Liakhovetski | eec5ce0 | 2011-07-22 07:43:15 -0300 | [diff] [blame] | 1640 | icd->user_width = out_width & ~3; |
| 1641 | icd->user_height = out_height & ~3; |
| 1642 | /* Offsets are applied at the CEU scaling filter input */ |
Guennadi Liakhovetski | 787d0f9 | 2011-02-16 18:42:24 -0300 | [diff] [blame] | 1643 | cam->ceu_left = scale_down(rect->left - cam_rect->left, scale_cam_h) & ~1; |
| 1644 | cam->ceu_top = scale_down(rect->top - cam_rect->top, scale_cam_v) & ~1; |
Guennadi Liakhovetski | e622681 | 2010-03-23 11:23:31 -0300 | [diff] [blame] | 1645 | |
| 1646 | /* 6. Use CEU cropping to crop to the new window. */ |
| 1647 | sh_mobile_ceu_set_rect(icd); |
| 1648 | |
| 1649 | cam->subrect = *rect; |
| 1650 | |
| 1651 | dev_geo(dev, "6: CEU cropped to %ux%u@%u:%u\n", |
| 1652 | icd->user_width, icd->user_height, |
| 1653 | cam->ceu_left, cam->ceu_top); |
| 1654 | |
Guennadi Liakhovetski | 787d0f9 | 2011-02-16 18:42:24 -0300 | [diff] [blame] | 1655 | /* Restore capture. The CE bit can be cleared by the hardware */ |
Guennadi Liakhovetski | 904078f | 2009-08-25 11:46:52 -0300 | [diff] [blame] | 1656 | if (pcdev->active) |
| 1657 | capsr |= 1; |
Guennadi Liakhovetski | 904078f | 2009-08-25 11:46:52 -0300 | [diff] [blame] | 1658 | capture_restore(pcdev, capsr); |
| 1659 | |
Guennadi Liakhovetski | 904078f | 2009-08-25 11:46:52 -0300 | [diff] [blame] | 1660 | /* Even if only camera cropping succeeded */ |
| 1661 | return ret; |
Guennadi Liakhovetski | 09e231b | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 1662 | } |
| 1663 | |
Guennadi Liakhovetski | e622681 | 2010-03-23 11:23:31 -0300 | [diff] [blame] | 1664 | static int sh_mobile_ceu_get_crop(struct soc_camera_device *icd, |
| 1665 | struct v4l2_crop *a) |
| 1666 | { |
| 1667 | struct sh_mobile_ceu_cam *cam = icd->host_priv; |
| 1668 | |
| 1669 | a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 1670 | a->c = cam->subrect; |
| 1671 | |
| 1672 | return 0; |
| 1673 | } |
| 1674 | |
| 1675 | /* |
| 1676 | * Calculate real client output window by applying new scales to the current |
| 1677 | * client crop. New scales are calculated from the requested output format and |
| 1678 | * CEU crop, mapped backed onto the client input (subrect). |
| 1679 | */ |
| 1680 | static void calculate_client_output(struct soc_camera_device *icd, |
Guennadi Liakhovetski | eec5ce0 | 2011-07-22 07:43:15 -0300 | [diff] [blame] | 1681 | const struct v4l2_pix_format *pix, struct v4l2_mbus_framefmt *mf) |
Guennadi Liakhovetski | e622681 | 2010-03-23 11:23:31 -0300 | [diff] [blame] | 1682 | { |
| 1683 | struct sh_mobile_ceu_cam *cam = icd->host_priv; |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 1684 | struct device *dev = icd->parent; |
Guennadi Liakhovetski | e622681 | 2010-03-23 11:23:31 -0300 | [diff] [blame] | 1685 | struct v4l2_rect *cam_subrect = &cam->subrect; |
| 1686 | unsigned int scale_v, scale_h; |
| 1687 | |
| 1688 | if (cam_subrect->width == cam->rect.width && |
| 1689 | cam_subrect->height == cam->rect.height) { |
| 1690 | /* No sub-cropping */ |
| 1691 | mf->width = pix->width; |
| 1692 | mf->height = pix->height; |
| 1693 | return; |
| 1694 | } |
| 1695 | |
| 1696 | /* 1.-2. Current camera scales and subwin - cached. */ |
| 1697 | |
| 1698 | dev_geo(dev, "2: subwin %ux%u@%u:%u\n", |
| 1699 | cam_subrect->width, cam_subrect->height, |
| 1700 | cam_subrect->left, cam_subrect->top); |
| 1701 | |
| 1702 | /* |
| 1703 | * 3. Calculate new combined scales from input sub-window to requested |
| 1704 | * user window. |
| 1705 | */ |
| 1706 | |
| 1707 | /* |
| 1708 | * TODO: CEU cannot scale images larger than VGA to smaller than SubQCIF |
| 1709 | * (128x96) or larger than VGA |
| 1710 | */ |
| 1711 | scale_h = calc_generic_scale(cam_subrect->width, pix->width); |
| 1712 | scale_v = calc_generic_scale(cam_subrect->height, pix->height); |
| 1713 | |
| 1714 | dev_geo(dev, "3: scales %u:%u\n", scale_h, scale_v); |
| 1715 | |
| 1716 | /* |
Guennadi Liakhovetski | 3698874 | 2011-07-22 09:44:40 -0300 | [diff] [blame] | 1717 | * 4. Calculate desired client output window by applying combined scales |
| 1718 | * to client (real) input window. |
Guennadi Liakhovetski | e622681 | 2010-03-23 11:23:31 -0300 | [diff] [blame] | 1719 | */ |
| 1720 | mf->width = scale_down(cam->rect.width, scale_h); |
| 1721 | mf->height = scale_down(cam->rect.height, scale_v); |
| 1722 | } |
| 1723 | |
Guennadi Liakhovetski | 961801b | 2009-08-25 11:46:54 -0300 | [diff] [blame] | 1724 | /* Similar to set_crop multistage iterative algorithm */ |
Guennadi Liakhovetski | d8fac21 | 2008-12-01 09:45:21 -0300 | [diff] [blame] | 1725 | static int sh_mobile_ceu_set_fmt(struct soc_camera_device *icd, |
Guennadi Liakhovetski | 09e231b | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 1726 | struct v4l2_format *f) |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 1727 | { |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 1728 | struct device *dev = icd->parent; |
| 1729 | struct soc_camera_host *ici = to_soc_camera_host(dev); |
Guennadi Liakhovetski | 961801b | 2009-08-25 11:46:54 -0300 | [diff] [blame] | 1730 | struct sh_mobile_ceu_dev *pcdev = ici->priv; |
Guennadi Liakhovetski | 904078f | 2009-08-25 11:46:52 -0300 | [diff] [blame] | 1731 | struct sh_mobile_ceu_cam *cam = icd->host_priv; |
| 1732 | struct v4l2_pix_format *pix = &f->fmt.pix; |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 1733 | struct v4l2_mbus_framefmt mf; |
Guennadi Liakhovetski | 904078f | 2009-08-25 11:46:52 -0300 | [diff] [blame] | 1734 | __u32 pixfmt = pix->pixelformat; |
Magnus Damm | 9414de3 | 2008-12-18 11:49:06 -0300 | [diff] [blame] | 1735 | const struct soc_camera_format_xlate *xlate; |
Guennadi Liakhovetski | b3b5020 | 2010-07-26 12:13:34 -0300 | [diff] [blame] | 1736 | /* Keep Compiler Happy */ |
| 1737 | unsigned int ceu_sub_width = 0, ceu_sub_height = 0; |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 1738 | u16 scale_v, scale_h; |
| 1739 | int ret; |
Kuninori Morimoto | 1edcc10 | 2009-12-11 11:53:53 -0300 | [diff] [blame] | 1740 | bool image_mode; |
| 1741 | enum v4l2_field field; |
Guennadi Liakhovetski | 961801b | 2009-08-25 11:46:54 -0300 | [diff] [blame] | 1742 | |
| 1743 | switch (pix->field) { |
Guennadi Liakhovetski | 961801b | 2009-08-25 11:46:54 -0300 | [diff] [blame] | 1744 | default: |
| 1745 | pix->field = V4L2_FIELD_NONE; |
| 1746 | /* fall-through */ |
Kuninori Morimoto | 1edcc10 | 2009-12-11 11:53:53 -0300 | [diff] [blame] | 1747 | case V4L2_FIELD_INTERLACED_TB: |
| 1748 | case V4L2_FIELD_INTERLACED_BT: |
Guennadi Liakhovetski | 961801b | 2009-08-25 11:46:54 -0300 | [diff] [blame] | 1749 | case V4L2_FIELD_NONE: |
Kuninori Morimoto | 1edcc10 | 2009-12-11 11:53:53 -0300 | [diff] [blame] | 1750 | field = pix->field; |
| 1751 | break; |
| 1752 | case V4L2_FIELD_INTERLACED: |
| 1753 | field = V4L2_FIELD_INTERLACED_TB; |
Guennadi Liakhovetski | 961801b | 2009-08-25 11:46:54 -0300 | [diff] [blame] | 1754 | break; |
| 1755 | } |
Guennadi Liakhovetski | 25c4d74 | 2008-12-01 09:44:59 -0300 | [diff] [blame] | 1756 | |
Magnus Damm | 9414de3 | 2008-12-18 11:49:06 -0300 | [diff] [blame] | 1757 | xlate = soc_camera_xlate_by_fourcc(icd, pixfmt); |
| 1758 | if (!xlate) { |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 1759 | dev_warn(dev, "Format %x not found\n", pixfmt); |
Magnus Damm | 9414de3 | 2008-12-18 11:49:06 -0300 | [diff] [blame] | 1760 | return -EINVAL; |
Guennadi Liakhovetski | 25c4d74 | 2008-12-01 09:44:59 -0300 | [diff] [blame] | 1761 | } |
| 1762 | |
Guennadi Liakhovetski | 3698874 | 2011-07-22 09:44:40 -0300 | [diff] [blame] | 1763 | /* 1.-4. Calculate desired client output geometry */ |
Guennadi Liakhovetski | eec5ce0 | 2011-07-22 07:43:15 -0300 | [diff] [blame] | 1764 | calculate_client_output(icd, pix, &mf); |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 1765 | mf.field = pix->field; |
| 1766 | mf.colorspace = pix->colorspace; |
| 1767 | mf.code = xlate->code; |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 1768 | |
Guennadi Liakhovetski | 961801b | 2009-08-25 11:46:54 -0300 | [diff] [blame] | 1769 | switch (pixfmt) { |
| 1770 | case V4L2_PIX_FMT_NV12: |
| 1771 | case V4L2_PIX_FMT_NV21: |
| 1772 | case V4L2_PIX_FMT_NV16: |
| 1773 | case V4L2_PIX_FMT_NV61: |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 1774 | image_mode = true; |
Guennadi Liakhovetski | 961801b | 2009-08-25 11:46:54 -0300 | [diff] [blame] | 1775 | break; |
| 1776 | default: |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 1777 | image_mode = false; |
Magnus Damm | 9414de3 | 2008-12-18 11:49:06 -0300 | [diff] [blame] | 1778 | } |
Guennadi Liakhovetski | 25c4d74 | 2008-12-01 09:44:59 -0300 | [diff] [blame] | 1779 | |
Guennadi Liakhovetski | b5518a4 | 2011-11-03 10:11:11 -0300 | [diff] [blame] | 1780 | dev_geo(dev, "S_FMT(pix=0x%x, fld 0x%x, code 0x%x, %ux%u)\n", pixfmt, mf.field, mf.code, |
Guennadi Liakhovetski | e1db704 | 2011-07-27 18:17:56 -0300 | [diff] [blame] | 1781 | pix->width, pix->height); |
| 1782 | |
Guennadi Liakhovetski | e622681 | 2010-03-23 11:23:31 -0300 | [diff] [blame] | 1783 | 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] | 1784 | |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 1785 | /* 5. - 9. */ |
Guennadi Liakhovetski | e622681 | 2010-03-23 11:23:31 -0300 | [diff] [blame] | 1786 | ret = client_scale(icd, &mf, &ceu_sub_width, &ceu_sub_height, |
Kuninori Morimoto | 1edcc10 | 2009-12-11 11:53:53 -0300 | [diff] [blame] | 1787 | image_mode && V4L2_FIELD_NONE == field); |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 1788 | |
Guennadi Liakhovetski | e622681 | 2010-03-23 11:23:31 -0300 | [diff] [blame] | 1789 | dev_geo(dev, "5-9: client scale return %d\n", ret); |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 1790 | |
| 1791 | /* Done with the camera. Now see if we can improve the result */ |
| 1792 | |
Guennadi Liakhovetski | b3b5020 | 2010-07-26 12:13:34 -0300 | [diff] [blame] | 1793 | dev_geo(dev, "fmt %ux%u, requested %ux%u\n", |
| 1794 | mf.width, mf.height, pix->width, pix->height); |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 1795 | if (ret < 0) |
| 1796 | return ret; |
| 1797 | |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 1798 | if (mf.code != xlate->code) |
| 1799 | return -EINVAL; |
| 1800 | |
Guennadi Liakhovetski | e622681 | 2010-03-23 11:23:31 -0300 | [diff] [blame] | 1801 | /* 9. Prepare CEU crop */ |
| 1802 | cam->width = mf.width; |
| 1803 | cam->height = mf.height; |
| 1804 | |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 1805 | /* 10. Use CEU scaling to scale to the requested user window. */ |
Guennadi Liakhovetski | 961801b | 2009-08-25 11:46:54 -0300 | [diff] [blame] | 1806 | |
| 1807 | /* We cannot scale up */ |
Guennadi Liakhovetski | e622681 | 2010-03-23 11:23:31 -0300 | [diff] [blame] | 1808 | if (pix->width > ceu_sub_width) |
| 1809 | ceu_sub_width = pix->width; |
Guennadi Liakhovetski | 961801b | 2009-08-25 11:46:54 -0300 | [diff] [blame] | 1810 | |
Guennadi Liakhovetski | e622681 | 2010-03-23 11:23:31 -0300 | [diff] [blame] | 1811 | if (pix->height > ceu_sub_height) |
| 1812 | ceu_sub_height = pix->height; |
Guennadi Liakhovetski | 961801b | 2009-08-25 11:46:54 -0300 | [diff] [blame] | 1813 | |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 1814 | pix->colorspace = mf.colorspace; |
| 1815 | |
| 1816 | if (image_mode) { |
| 1817 | /* Scale pix->{width x height} down to width x height */ |
Guennadi Liakhovetski | e622681 | 2010-03-23 11:23:31 -0300 | [diff] [blame] | 1818 | scale_h = calc_scale(ceu_sub_width, &pix->width); |
| 1819 | scale_v = calc_scale(ceu_sub_height, &pix->height); |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 1820 | } else { |
Guennadi Liakhovetski | e622681 | 2010-03-23 11:23:31 -0300 | [diff] [blame] | 1821 | pix->width = ceu_sub_width; |
| 1822 | pix->height = ceu_sub_height; |
| 1823 | scale_h = 0; |
| 1824 | scale_v = 0; |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 1825 | } |
Guennadi Liakhovetski | 961801b | 2009-08-25 11:46:54 -0300 | [diff] [blame] | 1826 | |
Guennadi Liakhovetski | e622681 | 2010-03-23 11:23:31 -0300 | [diff] [blame] | 1827 | pcdev->cflcr = scale_h | (scale_v << 16); |
| 1828 | |
| 1829 | /* |
| 1830 | * We have calculated CFLCR, the actual configuration will be performed |
| 1831 | * in sh_mobile_ceu_set_bus_param() |
| 1832 | */ |
| 1833 | |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 1834 | 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] | 1835 | ceu_sub_width, scale_h, pix->width, |
| 1836 | ceu_sub_height, scale_v, pix->height); |
Guennadi Liakhovetski | 961801b | 2009-08-25 11:46:54 -0300 | [diff] [blame] | 1837 | |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 1838 | cam->code = xlate->code; |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 1839 | icd->current_fmt = xlate; |
Guennadi Liakhovetski | 961801b | 2009-08-25 11:46:54 -0300 | [diff] [blame] | 1840 | |
Kuninori Morimoto | 1edcc10 | 2009-12-11 11:53:53 -0300 | [diff] [blame] | 1841 | pcdev->field = field; |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 1842 | pcdev->image_mode = image_mode; |
Guennadi Liakhovetski | 961801b | 2009-08-25 11:46:54 -0300 | [diff] [blame] | 1843 | |
Guennadi Liakhovetski | eec5ce0 | 2011-07-22 07:43:15 -0300 | [diff] [blame] | 1844 | /* CFSZR requirement */ |
| 1845 | pix->width &= ~3; |
| 1846 | pix->height &= ~3; |
| 1847 | |
Guennadi Liakhovetski | 961801b | 2009-08-25 11:46:54 -0300 | [diff] [blame] | 1848 | return 0; |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 1849 | } |
| 1850 | |
Guennadi Liakhovetski | d8fac21 | 2008-12-01 09:45:21 -0300 | [diff] [blame] | 1851 | static int sh_mobile_ceu_try_fmt(struct soc_camera_device *icd, |
| 1852 | struct v4l2_format *f) |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 1853 | { |
Guennadi Liakhovetski | 48e971c | 2012-03-14 08:37:03 -0300 | [diff] [blame] | 1854 | struct soc_camera_host *ici = to_soc_camera_host(icd->parent); |
| 1855 | struct sh_mobile_ceu_dev *pcdev = ici->priv; |
Magnus Damm | 9414de3 | 2008-12-18 11:49:06 -0300 | [diff] [blame] | 1856 | const struct soc_camera_format_xlate *xlate; |
Guennadi Liakhovetski | 904078f | 2009-08-25 11:46:52 -0300 | [diff] [blame] | 1857 | struct v4l2_pix_format *pix = &f->fmt.pix; |
Guennadi Liakhovetski | c9c1f1c | 2009-08-25 11:46:59 -0300 | [diff] [blame] | 1858 | struct v4l2_subdev *sd = soc_camera_to_subdev(icd); |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 1859 | struct v4l2_mbus_framefmt mf; |
Guennadi Liakhovetski | 904078f | 2009-08-25 11:46:52 -0300 | [diff] [blame] | 1860 | __u32 pixfmt = pix->pixelformat; |
Guennadi Liakhovetski | 961801b | 2009-08-25 11:46:54 -0300 | [diff] [blame] | 1861 | int width, height; |
Kuninori Morimoto | ccab8a2 | 2008-12-18 12:51:21 -0300 | [diff] [blame] | 1862 | int ret; |
Guennadi Liakhovetski | a2c8c68 | 2008-12-01 09:44:53 -0300 | [diff] [blame] | 1863 | |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 1864 | dev_geo(icd->parent, "TRY_FMT(pix=0x%x, %ux%u)\n", |
Guennadi Liakhovetski | b3b5020 | 2010-07-26 12:13:34 -0300 | [diff] [blame] | 1865 | pixfmt, pix->width, pix->height); |
| 1866 | |
Magnus Damm | 9414de3 | 2008-12-18 11:49:06 -0300 | [diff] [blame] | 1867 | xlate = soc_camera_xlate_by_fourcc(icd, pixfmt); |
| 1868 | if (!xlate) { |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 1869 | dev_warn(icd->parent, "Format %x not found\n", pixfmt); |
Guennadi Liakhovetski | 25c4d74 | 2008-12-01 09:44:59 -0300 | [diff] [blame] | 1870 | return -EINVAL; |
Magnus Damm | 9414de3 | 2008-12-18 11:49:06 -0300 | [diff] [blame] | 1871 | } |
Guennadi Liakhovetski | 25c4d74 | 2008-12-01 09:44:59 -0300 | [diff] [blame] | 1872 | |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 1873 | /* FIXME: calculate using depth and bus width */ |
| 1874 | |
Guennadi Liakhovetski | eec5ce0 | 2011-07-22 07:43:15 -0300 | [diff] [blame] | 1875 | /* CFSZR requires height and width to be 4-pixel aligned */ |
Guennadi Liakhovetski | 48e971c | 2012-03-14 08:37:03 -0300 | [diff] [blame] | 1876 | v4l_bound_align_image(&pix->width, 2, pcdev->max_width, 2, |
| 1877 | &pix->height, 4, pcdev->max_height, 2, 0); |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 1878 | |
Guennadi Liakhovetski | 961801b | 2009-08-25 11:46:54 -0300 | [diff] [blame] | 1879 | width = pix->width; |
| 1880 | height = pix->height; |
| 1881 | |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 1882 | /* limit to sensor capabilities */ |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 1883 | mf.width = pix->width; |
| 1884 | mf.height = pix->height; |
| 1885 | mf.field = pix->field; |
| 1886 | mf.code = xlate->code; |
| 1887 | mf.colorspace = pix->colorspace; |
| 1888 | |
Guennadi Liakhovetski | 4c0b036 | 2011-12-05 16:01:13 -0300 | [diff] [blame] | 1889 | ret = v4l2_device_call_until_err(sd->v4l2_dev, soc_camera_grp_id(icd), |
| 1890 | video, try_mbus_fmt, &mf); |
Kuninori Morimoto | ccab8a2 | 2008-12-18 12:51:21 -0300 | [diff] [blame] | 1891 | if (ret < 0) |
| 1892 | return ret; |
| 1893 | |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 1894 | pix->width = mf.width; |
| 1895 | pix->height = mf.height; |
| 1896 | pix->field = mf.field; |
| 1897 | pix->colorspace = mf.colorspace; |
| 1898 | |
Guennadi Liakhovetski | 961801b | 2009-08-25 11:46:54 -0300 | [diff] [blame] | 1899 | switch (pixfmt) { |
| 1900 | case V4L2_PIX_FMT_NV12: |
| 1901 | case V4L2_PIX_FMT_NV21: |
| 1902 | case V4L2_PIX_FMT_NV16: |
| 1903 | case V4L2_PIX_FMT_NV61: |
| 1904 | /* FIXME: check against rect_max after converting soc-camera */ |
| 1905 | /* We can scale precisely, need a bigger image from camera */ |
| 1906 | if (pix->width < width || pix->height < height) { |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 1907 | /* |
| 1908 | * We presume, the sensor behaves sanely, i.e., if |
| 1909 | * requested a bigger rectangle, it will not return a |
| 1910 | * smaller one. |
| 1911 | */ |
Guennadi Liakhovetski | 48e971c | 2012-03-14 08:37:03 -0300 | [diff] [blame] | 1912 | mf.width = pcdev->max_width; |
| 1913 | mf.height = pcdev->max_height; |
Guennadi Liakhovetski | 4c0b036 | 2011-12-05 16:01:13 -0300 | [diff] [blame] | 1914 | ret = v4l2_device_call_until_err(sd->v4l2_dev, |
| 1915 | soc_camera_grp_id(icd), video, |
| 1916 | try_mbus_fmt, &mf); |
Guennadi Liakhovetski | 961801b | 2009-08-25 11:46:54 -0300 | [diff] [blame] | 1917 | if (ret < 0) { |
| 1918 | /* Shouldn't actually happen... */ |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 1919 | dev_err(icd->parent, |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 1920 | "FIXME: client try_fmt() = %d\n", ret); |
| 1921 | return ret; |
Guennadi Liakhovetski | 961801b | 2009-08-25 11:46:54 -0300 | [diff] [blame] | 1922 | } |
| 1923 | } |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 1924 | /* We will scale exactly */ |
| 1925 | if (mf.width > width) |
Guennadi Liakhovetski | 961801b | 2009-08-25 11:46:54 -0300 | [diff] [blame] | 1926 | pix->width = width; |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 1927 | if (mf.height > height) |
Guennadi Liakhovetski | 961801b | 2009-08-25 11:46:54 -0300 | [diff] [blame] | 1928 | pix->height = height; |
Kuninori Morimoto | ccab8a2 | 2008-12-18 12:51:21 -0300 | [diff] [blame] | 1929 | } |
| 1930 | |
Guennadi Liakhovetski | eec5ce0 | 2011-07-22 07:43:15 -0300 | [diff] [blame] | 1931 | pix->width &= ~3; |
| 1932 | pix->height &= ~3; |
| 1933 | |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 1934 | dev_geo(icd->parent, "%s(): return %d, fmt 0x%x, %ux%u\n", |
Guennadi Liakhovetski | b3b5020 | 2010-07-26 12:13:34 -0300 | [diff] [blame] | 1935 | __func__, ret, pix->pixelformat, pix->width, pix->height); |
| 1936 | |
Kuninori Morimoto | ccab8a2 | 2008-12-18 12:51:21 -0300 | [diff] [blame] | 1937 | return ret; |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 1938 | } |
| 1939 | |
Guennadi Liakhovetski | 3dac322 | 2011-03-28 13:39:16 -0300 | [diff] [blame] | 1940 | static int sh_mobile_ceu_set_livecrop(struct soc_camera_device *icd, |
| 1941 | struct v4l2_crop *a) |
| 1942 | { |
| 1943 | struct v4l2_subdev *sd = soc_camera_to_subdev(icd); |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 1944 | struct soc_camera_host *ici = to_soc_camera_host(icd->parent); |
Guennadi Liakhovetski | 3dac322 | 2011-03-28 13:39:16 -0300 | [diff] [blame] | 1945 | struct sh_mobile_ceu_dev *pcdev = ici->priv; |
| 1946 | u32 out_width = icd->user_width, out_height = icd->user_height; |
| 1947 | int ret; |
| 1948 | |
| 1949 | /* Freeze queue */ |
| 1950 | pcdev->frozen = 1; |
| 1951 | /* Wait for frame */ |
| 1952 | ret = wait_for_completion_interruptible(&pcdev->complete); |
| 1953 | /* Stop the client */ |
| 1954 | ret = v4l2_subdev_call(sd, video, s_stream, 0); |
| 1955 | if (ret < 0) |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 1956 | dev_warn(icd->parent, |
Guennadi Liakhovetski | 3dac322 | 2011-03-28 13:39:16 -0300 | [diff] [blame] | 1957 | "Client failed to stop the stream: %d\n", ret); |
| 1958 | else |
| 1959 | /* Do the crop, if it fails, there's nothing more we can do */ |
| 1960 | sh_mobile_ceu_set_crop(icd, a); |
| 1961 | |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 1962 | dev_geo(icd->parent, "Output after crop: %ux%u\n", icd->user_width, icd->user_height); |
Guennadi Liakhovetski | 3dac322 | 2011-03-28 13:39:16 -0300 | [diff] [blame] | 1963 | |
| 1964 | if (icd->user_width != out_width || icd->user_height != out_height) { |
| 1965 | struct v4l2_format f = { |
| 1966 | .type = V4L2_BUF_TYPE_VIDEO_CAPTURE, |
| 1967 | .fmt.pix = { |
| 1968 | .width = out_width, |
| 1969 | .height = out_height, |
| 1970 | .pixelformat = icd->current_fmt->host_fmt->fourcc, |
| 1971 | .field = pcdev->field, |
| 1972 | .colorspace = icd->colorspace, |
| 1973 | }, |
| 1974 | }; |
| 1975 | ret = sh_mobile_ceu_set_fmt(icd, &f); |
| 1976 | if (!ret && (out_width != f.fmt.pix.width || |
| 1977 | out_height != f.fmt.pix.height)) |
| 1978 | ret = -EINVAL; |
| 1979 | if (!ret) { |
Guennadi Liakhovetski | eec5ce0 | 2011-07-22 07:43:15 -0300 | [diff] [blame] | 1980 | icd->user_width = out_width & ~3; |
| 1981 | icd->user_height = out_height & ~3; |
Guennadi Liakhovetski | 8843d11 | 2011-09-21 17:52:51 -0300 | [diff] [blame] | 1982 | ret = sh_mobile_ceu_set_bus_param(icd); |
Guennadi Liakhovetski | 3dac322 | 2011-03-28 13:39:16 -0300 | [diff] [blame] | 1983 | } |
| 1984 | } |
| 1985 | |
| 1986 | /* Thaw the queue */ |
| 1987 | pcdev->frozen = 0; |
| 1988 | spin_lock_irq(&pcdev->lock); |
| 1989 | sh_mobile_ceu_capture(pcdev); |
| 1990 | spin_unlock_irq(&pcdev->lock); |
| 1991 | /* Start the client */ |
| 1992 | ret = v4l2_subdev_call(sd, video, s_stream, 1); |
| 1993 | return ret; |
| 1994 | } |
| 1995 | |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 1996 | static unsigned int sh_mobile_ceu_poll(struct file *file, poll_table *pt) |
| 1997 | { |
Guennadi Liakhovetski | 57bee29 | 2010-08-17 14:29:51 -0300 | [diff] [blame] | 1998 | struct soc_camera_device *icd = file->private_data; |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 1999 | |
Guennadi Liakhovetski | a616898 | 2011-02-18 05:30:15 -0300 | [diff] [blame] | 2000 | return vb2_poll(&icd->vb2_vidq, file, pt); |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 2001 | } |
| 2002 | |
| 2003 | static int sh_mobile_ceu_querycap(struct soc_camera_host *ici, |
| 2004 | struct v4l2_capability *cap) |
| 2005 | { |
| 2006 | strlcpy(cap->card, "SuperH_Mobile_CEU", sizeof(cap->card)); |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 2007 | cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING; |
| 2008 | return 0; |
| 2009 | } |
| 2010 | |
Guennadi Liakhovetski | a616898 | 2011-02-18 05:30:15 -0300 | [diff] [blame] | 2011 | static int sh_mobile_ceu_init_videobuf(struct vb2_queue *q, |
| 2012 | struct soc_camera_device *icd) |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 2013 | { |
Guennadi Liakhovetski | a616898 | 2011-02-18 05:30:15 -0300 | [diff] [blame] | 2014 | q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 2015 | q->io_modes = VB2_MMAP | VB2_USERPTR; |
| 2016 | q->drv_priv = icd; |
| 2017 | q->ops = &sh_mobile_ceu_videobuf_ops; |
| 2018 | q->mem_ops = &vb2_dma_contig_memops; |
| 2019 | q->buf_struct_size = sizeof(struct sh_mobile_ceu_buffer); |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 2020 | |
Guennadi Liakhovetski | a616898 | 2011-02-18 05:30:15 -0300 | [diff] [blame] | 2021 | return vb2_queue_init(q); |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 2022 | } |
| 2023 | |
| 2024 | static struct soc_camera_host_ops sh_mobile_ceu_host_ops = { |
| 2025 | .owner = THIS_MODULE, |
| 2026 | .add = sh_mobile_ceu_add_device, |
| 2027 | .remove = sh_mobile_ceu_remove_device, |
Magnus Damm | 9414de3 | 2008-12-18 11:49:06 -0300 | [diff] [blame] | 2028 | .get_formats = sh_mobile_ceu_get_formats, |
Guennadi Liakhovetski | 904078f | 2009-08-25 11:46:52 -0300 | [diff] [blame] | 2029 | .put_formats = sh_mobile_ceu_put_formats, |
Guennadi Liakhovetski | e622681 | 2010-03-23 11:23:31 -0300 | [diff] [blame] | 2030 | .get_crop = sh_mobile_ceu_get_crop, |
Guennadi Liakhovetski | 09e231b | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 2031 | .set_crop = sh_mobile_ceu_set_crop, |
Guennadi Liakhovetski | 3dac322 | 2011-03-28 13:39:16 -0300 | [diff] [blame] | 2032 | .set_livecrop = sh_mobile_ceu_set_livecrop, |
Guennadi Liakhovetski | d8fac21 | 2008-12-01 09:45:21 -0300 | [diff] [blame] | 2033 | .set_fmt = sh_mobile_ceu_set_fmt, |
| 2034 | .try_fmt = sh_mobile_ceu_try_fmt, |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 2035 | .poll = sh_mobile_ceu_poll, |
| 2036 | .querycap = sh_mobile_ceu_querycap, |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 2037 | .set_bus_param = sh_mobile_ceu_set_bus_param, |
Guennadi Liakhovetski | a616898 | 2011-02-18 05:30:15 -0300 | [diff] [blame] | 2038 | .init_videobuf2 = sh_mobile_ceu_init_videobuf, |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 2039 | }; |
| 2040 | |
Guennadi Liakhovetski | b3b5020 | 2010-07-26 12:13:34 -0300 | [diff] [blame] | 2041 | struct bus_wait { |
| 2042 | struct notifier_block notifier; |
| 2043 | struct completion completion; |
| 2044 | struct device *dev; |
| 2045 | }; |
| 2046 | |
| 2047 | static int bus_notify(struct notifier_block *nb, |
| 2048 | unsigned long action, void *data) |
| 2049 | { |
| 2050 | struct device *dev = data; |
| 2051 | struct bus_wait *wait = container_of(nb, struct bus_wait, notifier); |
| 2052 | |
| 2053 | if (wait->dev != dev) |
| 2054 | return NOTIFY_DONE; |
| 2055 | |
| 2056 | switch (action) { |
| 2057 | case BUS_NOTIFY_UNBOUND_DRIVER: |
| 2058 | /* Protect from module unloading */ |
| 2059 | wait_for_completion(&wait->completion); |
| 2060 | return NOTIFY_OK; |
| 2061 | } |
| 2062 | return NOTIFY_DONE; |
| 2063 | } |
| 2064 | |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 2065 | static int __devinit sh_mobile_ceu_probe(struct platform_device *pdev) |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 2066 | { |
| 2067 | struct sh_mobile_ceu_dev *pcdev; |
| 2068 | struct resource *res; |
| 2069 | void __iomem *base; |
| 2070 | unsigned int irq; |
| 2071 | int err = 0; |
Guennadi Liakhovetski | b3b5020 | 2010-07-26 12:13:34 -0300 | [diff] [blame] | 2072 | struct bus_wait wait = { |
| 2073 | .completion = COMPLETION_INITIALIZER_ONSTACK(wait.completion), |
| 2074 | .notifier.notifier_call = bus_notify, |
| 2075 | }; |
Guennadi Liakhovetski | 6b526fe | 2011-07-01 11:19:58 -0300 | [diff] [blame] | 2076 | struct sh_mobile_ceu_companion *csi2; |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 2077 | |
| 2078 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 2079 | irq = platform_get_irq(pdev, 0); |
Uwe Kleine-König | b89fc2e | 2010-01-09 20:45:13 -0300 | [diff] [blame] | 2080 | if (!res || (int)irq <= 0) { |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 2081 | dev_err(&pdev->dev, "Not enough CEU platform resources.\n"); |
| 2082 | err = -ENODEV; |
| 2083 | goto exit; |
| 2084 | } |
| 2085 | |
| 2086 | pcdev = kzalloc(sizeof(*pcdev), GFP_KERNEL); |
| 2087 | if (!pcdev) { |
| 2088 | dev_err(&pdev->dev, "Could not allocate pcdev\n"); |
| 2089 | err = -ENOMEM; |
| 2090 | goto exit; |
| 2091 | } |
| 2092 | |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 2093 | INIT_LIST_HEAD(&pcdev->capture); |
| 2094 | spin_lock_init(&pcdev->lock); |
Guennadi Liakhovetski | 3dac322 | 2011-03-28 13:39:16 -0300 | [diff] [blame] | 2095 | init_completion(&pcdev->complete); |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 2096 | |
| 2097 | pcdev->pdata = pdev->dev.platform_data; |
| 2098 | if (!pcdev->pdata) { |
| 2099 | err = -EINVAL; |
| 2100 | dev_err(&pdev->dev, "CEU platform data not set.\n"); |
| 2101 | goto exit_kfree; |
| 2102 | } |
| 2103 | |
Guennadi Liakhovetski | 48e971c | 2012-03-14 08:37:03 -0300 | [diff] [blame] | 2104 | pcdev->max_width = pcdev->pdata->max_width ? : 2560; |
| 2105 | pcdev->max_height = pcdev->pdata->max_height ? : 1920; |
| 2106 | |
Guennadi Liakhovetski | eb6c855 | 2009-04-24 12:55:18 -0300 | [diff] [blame] | 2107 | base = ioremap_nocache(res->start, resource_size(res)); |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 2108 | if (!base) { |
| 2109 | err = -ENXIO; |
| 2110 | dev_err(&pdev->dev, "Unable to ioremap CEU registers.\n"); |
| 2111 | goto exit_kfree; |
| 2112 | } |
| 2113 | |
| 2114 | pcdev->irq = irq; |
| 2115 | pcdev->base = base; |
| 2116 | pcdev->video_limit = 0; /* only enabled if second resource exists */ |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 2117 | |
| 2118 | res = platform_get_resource(pdev, IORESOURCE_MEM, 1); |
| 2119 | if (res) { |
| 2120 | err = dma_declare_coherent_memory(&pdev->dev, res->start, |
| 2121 | res->start, |
Guennadi Liakhovetski | eb6c855 | 2009-04-24 12:55:18 -0300 | [diff] [blame] | 2122 | resource_size(res), |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 2123 | DMA_MEMORY_MAP | |
| 2124 | DMA_MEMORY_EXCLUSIVE); |
| 2125 | if (!err) { |
| 2126 | dev_err(&pdev->dev, "Unable to declare CEU memory.\n"); |
| 2127 | err = -ENXIO; |
| 2128 | goto exit_iounmap; |
| 2129 | } |
| 2130 | |
Guennadi Liakhovetski | eb6c855 | 2009-04-24 12:55:18 -0300 | [diff] [blame] | 2131 | pcdev->video_limit = resource_size(res); |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 2132 | } |
| 2133 | |
| 2134 | /* request irq */ |
| 2135 | err = request_irq(pcdev->irq, sh_mobile_ceu_irq, IRQF_DISABLED, |
Kay Sievers | af128a1 | 2008-10-30 00:51:46 -0300 | [diff] [blame] | 2136 | dev_name(&pdev->dev), pcdev); |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 2137 | if (err) { |
| 2138 | dev_err(&pdev->dev, "Unable to register CEU interrupt.\n"); |
| 2139 | goto exit_release_mem; |
| 2140 | } |
| 2141 | |
Magnus Damm | 6d1386c | 2009-08-14 10:49:17 +0000 | [diff] [blame] | 2142 | pm_suspend_ignore_children(&pdev->dev, true); |
| 2143 | pm_runtime_enable(&pdev->dev); |
| 2144 | pm_runtime_resume(&pdev->dev); |
Magnus Damm | a42b6dd | 2008-10-31 20:21:44 +0900 | [diff] [blame] | 2145 | |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 2146 | pcdev->ici.priv = pcdev; |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 2147 | pcdev->ici.v4l2_dev.dev = &pdev->dev; |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 2148 | pcdev->ici.nr = pdev->id; |
Kay Sievers | af128a1 | 2008-10-30 00:51:46 -0300 | [diff] [blame] | 2149 | pcdev->ici.drv_name = dev_name(&pdev->dev); |
| 2150 | pcdev->ici.ops = &sh_mobile_ceu_host_ops; |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 2151 | |
Guennadi Liakhovetski | 6b526fe | 2011-07-01 11:19:58 -0300 | [diff] [blame] | 2152 | pcdev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev); |
| 2153 | if (IS_ERR(pcdev->alloc_ctx)) { |
| 2154 | err = PTR_ERR(pcdev->alloc_ctx); |
| 2155 | goto exit_free_clk; |
| 2156 | } |
| 2157 | |
| 2158 | err = soc_camera_host_register(&pcdev->ici); |
| 2159 | if (err) |
| 2160 | goto exit_free_ctx; |
| 2161 | |
Guennadi Liakhovetski | b3b5020 | 2010-07-26 12:13:34 -0300 | [diff] [blame] | 2162 | /* CSI2 interfacing */ |
Guennadi Liakhovetski | 6b526fe | 2011-07-01 11:19:58 -0300 | [diff] [blame] | 2163 | csi2 = pcdev->pdata->csi2; |
Guennadi Liakhovetski | b3b5020 | 2010-07-26 12:13:34 -0300 | [diff] [blame] | 2164 | if (csi2) { |
Guennadi Liakhovetski | 6b526fe | 2011-07-01 11:19:58 -0300 | [diff] [blame] | 2165 | struct platform_device *csi2_pdev = |
| 2166 | platform_device_alloc("sh-mobile-csi2", csi2->id); |
| 2167 | struct sh_csi2_pdata *csi2_pdata = csi2->platform_data; |
| 2168 | |
| 2169 | if (!csi2_pdev) { |
| 2170 | err = -ENOMEM; |
| 2171 | goto exit_host_unregister; |
| 2172 | } |
| 2173 | |
| 2174 | pcdev->csi2_pdev = csi2_pdev; |
| 2175 | |
| 2176 | err = platform_device_add_data(csi2_pdev, csi2_pdata, sizeof(*csi2_pdata)); |
| 2177 | if (err < 0) |
| 2178 | goto exit_pdev_put; |
| 2179 | |
| 2180 | csi2_pdata = csi2_pdev->dev.platform_data; |
| 2181 | csi2_pdata->v4l2_dev = &pcdev->ici.v4l2_dev; |
| 2182 | |
| 2183 | csi2_pdev->resource = csi2->resource; |
| 2184 | csi2_pdev->num_resources = csi2->num_resources; |
| 2185 | |
| 2186 | err = platform_device_add(csi2_pdev); |
| 2187 | if (err < 0) |
| 2188 | goto exit_pdev_put; |
| 2189 | |
| 2190 | wait.dev = &csi2_pdev->dev; |
Guennadi Liakhovetski | b3b5020 | 2010-07-26 12:13:34 -0300 | [diff] [blame] | 2191 | |
| 2192 | err = bus_register_notifier(&platform_bus_type, &wait.notifier); |
| 2193 | if (err < 0) |
Guennadi Liakhovetski | 6b526fe | 2011-07-01 11:19:58 -0300 | [diff] [blame] | 2194 | goto exit_pdev_unregister; |
Guennadi Liakhovetski | b3b5020 | 2010-07-26 12:13:34 -0300 | [diff] [blame] | 2195 | |
| 2196 | /* |
| 2197 | * From this point the driver module will not unload, until |
| 2198 | * we complete the completion. |
| 2199 | */ |
| 2200 | |
Guennadi Liakhovetski | 6b526fe | 2011-07-01 11:19:58 -0300 | [diff] [blame] | 2201 | if (!csi2_pdev->dev.driver) { |
Guennadi Liakhovetski | b3b5020 | 2010-07-26 12:13:34 -0300 | [diff] [blame] | 2202 | complete(&wait.completion); |
| 2203 | /* Either too late, or probing failed */ |
| 2204 | bus_unregister_notifier(&platform_bus_type, &wait.notifier); |
| 2205 | err = -ENXIO; |
Guennadi Liakhovetski | 6b526fe | 2011-07-01 11:19:58 -0300 | [diff] [blame] | 2206 | goto exit_pdev_unregister; |
Guennadi Liakhovetski | b3b5020 | 2010-07-26 12:13:34 -0300 | [diff] [blame] | 2207 | } |
| 2208 | |
| 2209 | /* |
| 2210 | * The module is still loaded, in the worst case it is hanging |
| 2211 | * in device release on our completion. So, _now_ dereferencing |
| 2212 | * the "owner" is safe! |
| 2213 | */ |
| 2214 | |
Guennadi Liakhovetski | 6b526fe | 2011-07-01 11:19:58 -0300 | [diff] [blame] | 2215 | err = try_module_get(csi2_pdev->dev.driver->owner); |
Guennadi Liakhovetski | b3b5020 | 2010-07-26 12:13:34 -0300 | [diff] [blame] | 2216 | |
| 2217 | /* Let notifier complete, if it has been locked */ |
| 2218 | complete(&wait.completion); |
| 2219 | bus_unregister_notifier(&platform_bus_type, &wait.notifier); |
| 2220 | if (!err) { |
| 2221 | err = -ENODEV; |
Guennadi Liakhovetski | 6b526fe | 2011-07-01 11:19:58 -0300 | [diff] [blame] | 2222 | goto exit_pdev_unregister; |
Guennadi Liakhovetski | b3b5020 | 2010-07-26 12:13:34 -0300 | [diff] [blame] | 2223 | } |
| 2224 | } |
| 2225 | |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 2226 | return 0; |
| 2227 | |
Guennadi Liakhovetski | 6b526fe | 2011-07-01 11:19:58 -0300 | [diff] [blame] | 2228 | exit_pdev_unregister: |
| 2229 | platform_device_del(pcdev->csi2_pdev); |
| 2230 | exit_pdev_put: |
| 2231 | pcdev->csi2_pdev->resource = NULL; |
| 2232 | platform_device_put(pcdev->csi2_pdev); |
| 2233 | exit_host_unregister: |
| 2234 | soc_camera_host_unregister(&pcdev->ici); |
Guennadi Liakhovetski | a616898 | 2011-02-18 05:30:15 -0300 | [diff] [blame] | 2235 | exit_free_ctx: |
| 2236 | vb2_dma_contig_cleanup_ctx(pcdev->alloc_ctx); |
Kuninori Morimoto | 055e05a | 2009-11-09 16:13:24 -0300 | [diff] [blame] | 2237 | exit_free_clk: |
| 2238 | pm_runtime_disable(&pdev->dev); |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 2239 | free_irq(pcdev->irq, pcdev); |
| 2240 | exit_release_mem: |
| 2241 | if (platform_get_resource(pdev, IORESOURCE_MEM, 1)) |
| 2242 | dma_release_declared_memory(&pdev->dev); |
| 2243 | exit_iounmap: |
| 2244 | iounmap(base); |
| 2245 | exit_kfree: |
| 2246 | kfree(pcdev); |
| 2247 | exit: |
| 2248 | return err; |
| 2249 | } |
| 2250 | |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 2251 | static int __devexit sh_mobile_ceu_remove(struct platform_device *pdev) |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 2252 | { |
Guennadi Liakhovetski | eff505f | 2009-04-24 12:55:48 -0300 | [diff] [blame] | 2253 | struct soc_camera_host *soc_host = to_soc_camera_host(&pdev->dev); |
| 2254 | struct sh_mobile_ceu_dev *pcdev = container_of(soc_host, |
| 2255 | struct sh_mobile_ceu_dev, ici); |
Guennadi Liakhovetski | 6b526fe | 2011-07-01 11:19:58 -0300 | [diff] [blame] | 2256 | struct platform_device *csi2_pdev = pcdev->csi2_pdev; |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 2257 | |
Guennadi Liakhovetski | eff505f | 2009-04-24 12:55:48 -0300 | [diff] [blame] | 2258 | soc_camera_host_unregister(soc_host); |
Kuninori Morimoto | 055e05a | 2009-11-09 16:13:24 -0300 | [diff] [blame] | 2259 | pm_runtime_disable(&pdev->dev); |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 2260 | free_irq(pcdev->irq, pcdev); |
| 2261 | if (platform_get_resource(pdev, IORESOURCE_MEM, 1)) |
| 2262 | dma_release_declared_memory(&pdev->dev); |
| 2263 | iounmap(pcdev->base); |
Guennadi Liakhovetski | a616898 | 2011-02-18 05:30:15 -0300 | [diff] [blame] | 2264 | vb2_dma_contig_cleanup_ctx(pcdev->alloc_ctx); |
Guennadi Liakhovetski | 6b526fe | 2011-07-01 11:19:58 -0300 | [diff] [blame] | 2265 | if (csi2_pdev && csi2_pdev->dev.driver) { |
| 2266 | struct module *csi2_drv = csi2_pdev->dev.driver->owner; |
| 2267 | platform_device_del(csi2_pdev); |
| 2268 | csi2_pdev->resource = NULL; |
| 2269 | platform_device_put(csi2_pdev); |
| 2270 | module_put(csi2_drv); |
| 2271 | } |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 2272 | kfree(pcdev); |
Guennadi Liakhovetski | b3b5020 | 2010-07-26 12:13:34 -0300 | [diff] [blame] | 2273 | |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 2274 | return 0; |
| 2275 | } |
| 2276 | |
Magnus Damm | 6d1386c | 2009-08-14 10:49:17 +0000 | [diff] [blame] | 2277 | static int sh_mobile_ceu_runtime_nop(struct device *dev) |
| 2278 | { |
| 2279 | /* Runtime PM callback shared between ->runtime_suspend() |
| 2280 | * and ->runtime_resume(). Simply returns success. |
| 2281 | * |
| 2282 | * This driver re-initializes all registers after |
| 2283 | * pm_runtime_get_sync() anyway so there is no need |
| 2284 | * to save and restore registers here. |
| 2285 | */ |
| 2286 | return 0; |
| 2287 | } |
| 2288 | |
Alexey Dobriyan | 4714521 | 2009-12-14 18:00:08 -0800 | [diff] [blame] | 2289 | static const struct dev_pm_ops sh_mobile_ceu_dev_pm_ops = { |
Magnus Damm | 6d1386c | 2009-08-14 10:49:17 +0000 | [diff] [blame] | 2290 | .runtime_suspend = sh_mobile_ceu_runtime_nop, |
| 2291 | .runtime_resume = sh_mobile_ceu_runtime_nop, |
| 2292 | }; |
| 2293 | |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 2294 | static struct platform_driver sh_mobile_ceu_driver = { |
| 2295 | .driver = { |
| 2296 | .name = "sh_mobile_ceu", |
Magnus Damm | 6d1386c | 2009-08-14 10:49:17 +0000 | [diff] [blame] | 2297 | .pm = &sh_mobile_ceu_dev_pm_ops, |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 2298 | }, |
| 2299 | .probe = sh_mobile_ceu_probe, |
Uwe Kleine-König | 50e55fd | 2009-12-10 17:00:13 -0300 | [diff] [blame] | 2300 | .remove = __devexit_p(sh_mobile_ceu_remove), |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 2301 | }; |
| 2302 | |
| 2303 | static int __init sh_mobile_ceu_init(void) |
| 2304 | { |
Guennadi Liakhovetski | b3b5020 | 2010-07-26 12:13:34 -0300 | [diff] [blame] | 2305 | /* Whatever return code */ |
| 2306 | request_module("sh_mobile_csi2"); |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 2307 | return platform_driver_register(&sh_mobile_ceu_driver); |
| 2308 | } |
| 2309 | |
| 2310 | static void __exit sh_mobile_ceu_exit(void) |
| 2311 | { |
Paul Mundt | 01c1e4c | 2008-08-01 19:48:51 -0300 | [diff] [blame] | 2312 | platform_driver_unregister(&sh_mobile_ceu_driver); |
Magnus Damm | 0d3244d | 2008-07-16 22:59:28 -0300 | [diff] [blame] | 2313 | } |
| 2314 | |
| 2315 | module_init(sh_mobile_ceu_init); |
| 2316 | module_exit(sh_mobile_ceu_exit); |
| 2317 | |
| 2318 | MODULE_DESCRIPTION("SuperH Mobile CEU driver"); |
| 2319 | MODULE_AUTHOR("Magnus Damm"); |
| 2320 | MODULE_LICENSE("GPL"); |
Mauro Carvalho Chehab | 64dc3c1 | 2011-06-25 11:28:37 -0300 | [diff] [blame] | 2321 | MODULE_VERSION("0.0.6"); |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 2322 | MODULE_ALIAS("platform:sh_mobile_ceu"); |