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