Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 1 | /* |
| 2 | * V4L2 Driver for i.MX27/i.MX25 camera host |
| 3 | * |
| 4 | * Copyright (C) 2008, Sascha Hauer, Pengutronix |
| 5 | * Copyright (C) 2010, Baruch Siach, Orex Computed Radiography |
Javier Martin | c6a41e3 | 2012-01-30 09:14:10 -0300 | [diff] [blame^] | 6 | * Copyright (C) 2012, Javier Martin, Vista Silicon S.L. |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | */ |
| 13 | |
| 14 | #include <linux/init.h> |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/io.h> |
| 17 | #include <linux/delay.h> |
| 18 | #include <linux/slab.h> |
| 19 | #include <linux/dma-mapping.h> |
| 20 | #include <linux/errno.h> |
| 21 | #include <linux/fs.h> |
| 22 | #include <linux/interrupt.h> |
| 23 | #include <linux/kernel.h> |
| 24 | #include <linux/mm.h> |
| 25 | #include <linux/moduleparam.h> |
| 26 | #include <linux/time.h> |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 27 | #include <linux/device.h> |
| 28 | #include <linux/platform_device.h> |
| 29 | #include <linux/mutex.h> |
| 30 | #include <linux/clk.h> |
| 31 | |
| 32 | #include <media/v4l2-common.h> |
| 33 | #include <media/v4l2-dev.h> |
Javier Martin | c6a41e3 | 2012-01-30 09:14:10 -0300 | [diff] [blame^] | 34 | #include <media/videobuf2-core.h> |
| 35 | #include <media/videobuf2-dma-contig.h> |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 36 | #include <media/soc_camera.h> |
| 37 | #include <media/soc_mediabus.h> |
| 38 | |
| 39 | #include <linux/videodev2.h> |
| 40 | |
| 41 | #include <mach/mx2_cam.h> |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 42 | #include <mach/hardware.h> |
| 43 | |
| 44 | #include <asm/dma.h> |
| 45 | |
| 46 | #define MX2_CAM_DRV_NAME "mx2-camera" |
Mauro Carvalho Chehab | 64dc3c1 | 2011-06-25 11:28:37 -0300 | [diff] [blame] | 47 | #define MX2_CAM_VERSION "0.0.6" |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 48 | #define MX2_CAM_DRIVER_DESCRIPTION "i.MX2x_Camera" |
| 49 | |
| 50 | /* reset values */ |
| 51 | #define CSICR1_RESET_VAL 0x40000800 |
| 52 | #define CSICR2_RESET_VAL 0x0 |
| 53 | #define CSICR3_RESET_VAL 0x0 |
| 54 | |
| 55 | /* csi control reg 1 */ |
| 56 | #define CSICR1_SWAP16_EN (1 << 31) |
| 57 | #define CSICR1_EXT_VSYNC (1 << 30) |
| 58 | #define CSICR1_EOF_INTEN (1 << 29) |
| 59 | #define CSICR1_PRP_IF_EN (1 << 28) |
| 60 | #define CSICR1_CCIR_MODE (1 << 27) |
| 61 | #define CSICR1_COF_INTEN (1 << 26) |
| 62 | #define CSICR1_SF_OR_INTEN (1 << 25) |
| 63 | #define CSICR1_RF_OR_INTEN (1 << 24) |
| 64 | #define CSICR1_STATFF_LEVEL (3 << 22) |
| 65 | #define CSICR1_STATFF_INTEN (1 << 21) |
| 66 | #define CSICR1_RXFF_LEVEL(l) (((l) & 3) << 19) /* MX27 */ |
| 67 | #define CSICR1_FB2_DMA_INTEN (1 << 20) /* MX25 */ |
| 68 | #define CSICR1_FB1_DMA_INTEN (1 << 19) /* MX25 */ |
| 69 | #define CSICR1_RXFF_INTEN (1 << 18) |
| 70 | #define CSICR1_SOF_POL (1 << 17) |
| 71 | #define CSICR1_SOF_INTEN (1 << 16) |
| 72 | #define CSICR1_MCLKDIV(d) (((d) & 0xF) << 12) |
| 73 | #define CSICR1_HSYNC_POL (1 << 11) |
| 74 | #define CSICR1_CCIR_EN (1 << 10) |
| 75 | #define CSICR1_MCLKEN (1 << 9) |
| 76 | #define CSICR1_FCC (1 << 8) |
| 77 | #define CSICR1_PACK_DIR (1 << 7) |
| 78 | #define CSICR1_CLR_STATFIFO (1 << 6) |
| 79 | #define CSICR1_CLR_RXFIFO (1 << 5) |
| 80 | #define CSICR1_GCLK_MODE (1 << 4) |
| 81 | #define CSICR1_INV_DATA (1 << 3) |
| 82 | #define CSICR1_INV_PCLK (1 << 2) |
| 83 | #define CSICR1_REDGE (1 << 1) |
| 84 | |
| 85 | #define SHIFT_STATFF_LEVEL 22 |
| 86 | #define SHIFT_RXFF_LEVEL 19 |
| 87 | #define SHIFT_MCLKDIV 12 |
| 88 | |
| 89 | /* control reg 3 */ |
| 90 | #define CSICR3_FRMCNT (0xFFFF << 16) |
| 91 | #define CSICR3_FRMCNT_RST (1 << 15) |
| 92 | #define CSICR3_DMA_REFLASH_RFF (1 << 14) |
| 93 | #define CSICR3_DMA_REFLASH_SFF (1 << 13) |
| 94 | #define CSICR3_DMA_REQ_EN_RFF (1 << 12) |
| 95 | #define CSICR3_DMA_REQ_EN_SFF (1 << 11) |
| 96 | #define CSICR3_RXFF_LEVEL(l) (((l) & 7) << 4) /* MX25 */ |
| 97 | #define CSICR3_CSI_SUP (1 << 3) |
| 98 | #define CSICR3_ZERO_PACK_EN (1 << 2) |
| 99 | #define CSICR3_ECC_INT_EN (1 << 1) |
| 100 | #define CSICR3_ECC_AUTO_EN (1 << 0) |
| 101 | |
| 102 | #define SHIFT_FRMCNT 16 |
| 103 | |
| 104 | /* csi status reg */ |
| 105 | #define CSISR_SFF_OR_INT (1 << 25) |
| 106 | #define CSISR_RFF_OR_INT (1 << 24) |
| 107 | #define CSISR_STATFF_INT (1 << 21) |
| 108 | #define CSISR_DMA_TSF_FB2_INT (1 << 20) /* MX25 */ |
| 109 | #define CSISR_DMA_TSF_FB1_INT (1 << 19) /* MX25 */ |
| 110 | #define CSISR_RXFF_INT (1 << 18) |
| 111 | #define CSISR_EOF_INT (1 << 17) |
| 112 | #define CSISR_SOF_INT (1 << 16) |
| 113 | #define CSISR_F2_INT (1 << 15) |
| 114 | #define CSISR_F1_INT (1 << 14) |
| 115 | #define CSISR_COF_INT (1 << 13) |
| 116 | #define CSISR_ECC_INT (1 << 1) |
| 117 | #define CSISR_DRDY (1 << 0) |
| 118 | |
| 119 | #define CSICR1 0x00 |
| 120 | #define CSICR2 0x04 |
| 121 | #define CSISR (cpu_is_mx27() ? 0x08 : 0x18) |
| 122 | #define CSISTATFIFO 0x0c |
| 123 | #define CSIRFIFO 0x10 |
| 124 | #define CSIRXCNT 0x14 |
| 125 | #define CSICR3 (cpu_is_mx27() ? 0x1C : 0x08) |
| 126 | #define CSIDMASA_STATFIFO 0x20 |
| 127 | #define CSIDMATA_STATFIFO 0x24 |
| 128 | #define CSIDMASA_FB1 0x28 |
| 129 | #define CSIDMASA_FB2 0x2c |
| 130 | #define CSIFBUF_PARA 0x30 |
| 131 | #define CSIIMAG_PARA 0x34 |
| 132 | |
| 133 | /* EMMA PrP */ |
| 134 | #define PRP_CNTL 0x00 |
| 135 | #define PRP_INTR_CNTL 0x04 |
| 136 | #define PRP_INTRSTATUS 0x08 |
| 137 | #define PRP_SOURCE_Y_PTR 0x0c |
| 138 | #define PRP_SOURCE_CB_PTR 0x10 |
| 139 | #define PRP_SOURCE_CR_PTR 0x14 |
| 140 | #define PRP_DEST_RGB1_PTR 0x18 |
| 141 | #define PRP_DEST_RGB2_PTR 0x1c |
| 142 | #define PRP_DEST_Y_PTR 0x20 |
| 143 | #define PRP_DEST_CB_PTR 0x24 |
| 144 | #define PRP_DEST_CR_PTR 0x28 |
| 145 | #define PRP_SRC_FRAME_SIZE 0x2c |
| 146 | #define PRP_DEST_CH1_LINE_STRIDE 0x30 |
| 147 | #define PRP_SRC_PIXEL_FORMAT_CNTL 0x34 |
| 148 | #define PRP_CH1_PIXEL_FORMAT_CNTL 0x38 |
| 149 | #define PRP_CH1_OUT_IMAGE_SIZE 0x3c |
| 150 | #define PRP_CH2_OUT_IMAGE_SIZE 0x40 |
| 151 | #define PRP_SRC_LINE_STRIDE 0x44 |
| 152 | #define PRP_CSC_COEF_012 0x48 |
| 153 | #define PRP_CSC_COEF_345 0x4c |
| 154 | #define PRP_CSC_COEF_678 0x50 |
| 155 | #define PRP_CH1_RZ_HORI_COEF1 0x54 |
| 156 | #define PRP_CH1_RZ_HORI_COEF2 0x58 |
| 157 | #define PRP_CH1_RZ_HORI_VALID 0x5c |
| 158 | #define PRP_CH1_RZ_VERT_COEF1 0x60 |
| 159 | #define PRP_CH1_RZ_VERT_COEF2 0x64 |
| 160 | #define PRP_CH1_RZ_VERT_VALID 0x68 |
| 161 | #define PRP_CH2_RZ_HORI_COEF1 0x6c |
| 162 | #define PRP_CH2_RZ_HORI_COEF2 0x70 |
| 163 | #define PRP_CH2_RZ_HORI_VALID 0x74 |
| 164 | #define PRP_CH2_RZ_VERT_COEF1 0x78 |
| 165 | #define PRP_CH2_RZ_VERT_COEF2 0x7c |
| 166 | #define PRP_CH2_RZ_VERT_VALID 0x80 |
| 167 | |
| 168 | #define PRP_CNTL_CH1EN (1 << 0) |
| 169 | #define PRP_CNTL_CH2EN (1 << 1) |
| 170 | #define PRP_CNTL_CSIEN (1 << 2) |
| 171 | #define PRP_CNTL_DATA_IN_YUV420 (0 << 3) |
| 172 | #define PRP_CNTL_DATA_IN_YUV422 (1 << 3) |
| 173 | #define PRP_CNTL_DATA_IN_RGB16 (2 << 3) |
| 174 | #define PRP_CNTL_DATA_IN_RGB32 (3 << 3) |
| 175 | #define PRP_CNTL_CH1_OUT_RGB8 (0 << 5) |
| 176 | #define PRP_CNTL_CH1_OUT_RGB16 (1 << 5) |
| 177 | #define PRP_CNTL_CH1_OUT_RGB32 (2 << 5) |
| 178 | #define PRP_CNTL_CH1_OUT_YUV422 (3 << 5) |
| 179 | #define PRP_CNTL_CH2_OUT_YUV420 (0 << 7) |
| 180 | #define PRP_CNTL_CH2_OUT_YUV422 (1 << 7) |
| 181 | #define PRP_CNTL_CH2_OUT_YUV444 (2 << 7) |
| 182 | #define PRP_CNTL_CH1_LEN (1 << 9) |
| 183 | #define PRP_CNTL_CH2_LEN (1 << 10) |
| 184 | #define PRP_CNTL_SKIP_FRAME (1 << 11) |
| 185 | #define PRP_CNTL_SWRST (1 << 12) |
| 186 | #define PRP_CNTL_CLKEN (1 << 13) |
| 187 | #define PRP_CNTL_WEN (1 << 14) |
| 188 | #define PRP_CNTL_CH1BYP (1 << 15) |
| 189 | #define PRP_CNTL_IN_TSKIP(x) ((x) << 16) |
| 190 | #define PRP_CNTL_CH1_TSKIP(x) ((x) << 19) |
| 191 | #define PRP_CNTL_CH2_TSKIP(x) ((x) << 22) |
| 192 | #define PRP_CNTL_INPUT_FIFO_LEVEL(x) ((x) << 25) |
| 193 | #define PRP_CNTL_RZ_FIFO_LEVEL(x) ((x) << 27) |
| 194 | #define PRP_CNTL_CH2B1EN (1 << 29) |
| 195 | #define PRP_CNTL_CH2B2EN (1 << 30) |
| 196 | #define PRP_CNTL_CH2FEN (1 << 31) |
| 197 | |
| 198 | /* IRQ Enable and status register */ |
| 199 | #define PRP_INTR_RDERR (1 << 0) |
| 200 | #define PRP_INTR_CH1WERR (1 << 1) |
| 201 | #define PRP_INTR_CH2WERR (1 << 2) |
| 202 | #define PRP_INTR_CH1FC (1 << 3) |
| 203 | #define PRP_INTR_CH2FC (1 << 5) |
| 204 | #define PRP_INTR_LBOVF (1 << 7) |
| 205 | #define PRP_INTR_CH2OVF (1 << 8) |
| 206 | |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 207 | #define MAX_VIDEO_MEM 16 |
| 208 | |
Javier Martin | f410991 | 2011-12-14 13:30:14 -0300 | [diff] [blame] | 209 | struct mx2_prp_cfg { |
| 210 | int channel; |
| 211 | u32 in_fmt; |
| 212 | u32 out_fmt; |
| 213 | u32 src_pixel; |
| 214 | u32 ch1_pixel; |
| 215 | u32 irq_flags; |
| 216 | }; |
| 217 | |
| 218 | /* prp configuration for a client-host fmt pair */ |
| 219 | struct mx2_fmt_cfg { |
| 220 | enum v4l2_mbus_pixelcode in_fmt; |
| 221 | u32 out_fmt; |
| 222 | struct mx2_prp_cfg cfg; |
| 223 | }; |
| 224 | |
Javier Martin | c6a41e3 | 2012-01-30 09:14:10 -0300 | [diff] [blame^] | 225 | enum mx2_buffer_state { |
| 226 | MX2_STATE_QUEUED, |
| 227 | MX2_STATE_ACTIVE, |
| 228 | MX2_STATE_DONE, |
| 229 | }; |
| 230 | |
| 231 | /* buffer for one video frame */ |
| 232 | struct mx2_buffer { |
| 233 | /* common v4l buffer stuff -- must be first */ |
| 234 | struct vb2_buffer vb; |
| 235 | struct list_head queue; |
| 236 | enum mx2_buffer_state state; |
| 237 | |
| 238 | int bufnum; |
| 239 | }; |
| 240 | |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 241 | struct mx2_camera_dev { |
| 242 | struct device *dev; |
| 243 | struct soc_camera_host soc_host; |
| 244 | struct soc_camera_device *icd; |
| 245 | struct clk *clk_csi, *clk_emma; |
| 246 | |
| 247 | unsigned int irq_csi, irq_emma; |
| 248 | void __iomem *base_csi, *base_emma; |
| 249 | unsigned long base_dma; |
| 250 | |
| 251 | struct mx2_camera_platform_data *pdata; |
| 252 | struct resource *res_csi, *res_emma; |
| 253 | unsigned long platform_flags; |
| 254 | |
| 255 | struct list_head capture; |
| 256 | struct list_head active_bufs; |
| 257 | |
| 258 | spinlock_t lock; |
| 259 | |
| 260 | int dma; |
| 261 | struct mx2_buffer *active; |
| 262 | struct mx2_buffer *fb1_active; |
| 263 | struct mx2_buffer *fb2_active; |
| 264 | |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 265 | u32 csicr1; |
| 266 | |
Baruch Siach | 79d3c2c | 2010-07-27 08:03:30 +0300 | [diff] [blame] | 267 | void *discard_buffer; |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 268 | dma_addr_t discard_buffer_dma; |
| 269 | size_t discard_size; |
Javier Martin | f410991 | 2011-12-14 13:30:14 -0300 | [diff] [blame] | 270 | struct mx2_fmt_cfg *emma_prp; |
Javier Martin | ccd1a49 | 2011-12-22 12:12:00 -0300 | [diff] [blame] | 271 | u32 frame_count; |
Javier Martin | c6a41e3 | 2012-01-30 09:14:10 -0300 | [diff] [blame^] | 272 | struct vb2_alloc_ctx *alloc_ctx; |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 273 | }; |
| 274 | |
Javier Martin | f410991 | 2011-12-14 13:30:14 -0300 | [diff] [blame] | 275 | static struct mx2_fmt_cfg mx27_emma_prp_table[] = { |
| 276 | /* |
| 277 | * This is a generic configuration which is valid for most |
| 278 | * prp input-output format combinations. |
| 279 | * We set the incomming and outgoing pixelformat to a |
| 280 | * 16 Bit wide format and adjust the bytesperline |
| 281 | * accordingly. With this configuration the inputdata |
| 282 | * will not be changed by the emma and could be any type |
| 283 | * of 16 Bit Pixelformat. |
| 284 | */ |
| 285 | { |
| 286 | .in_fmt = 0, |
| 287 | .out_fmt = 0, |
| 288 | .cfg = { |
| 289 | .channel = 1, |
| 290 | .in_fmt = PRP_CNTL_DATA_IN_RGB16, |
| 291 | .out_fmt = PRP_CNTL_CH1_OUT_RGB16, |
| 292 | .src_pixel = 0x2ca00565, /* RGB565 */ |
| 293 | .ch1_pixel = 0x2ca00565, /* RGB565 */ |
| 294 | .irq_flags = PRP_INTR_RDERR | PRP_INTR_CH1WERR | |
| 295 | PRP_INTR_CH1FC | PRP_INTR_LBOVF, |
| 296 | } |
| 297 | }, |
| 298 | { |
| 299 | .in_fmt = V4L2_MBUS_FMT_YUYV8_2X8, |
| 300 | .out_fmt = V4L2_PIX_FMT_YUV420, |
| 301 | .cfg = { |
| 302 | .channel = 2, |
| 303 | .in_fmt = PRP_CNTL_DATA_IN_YUV422, |
| 304 | .out_fmt = PRP_CNTL_CH2_OUT_YUV420, |
| 305 | .src_pixel = 0x22000888, /* YUV422 (YUYV) */ |
| 306 | .irq_flags = PRP_INTR_RDERR | PRP_INTR_CH2WERR | |
| 307 | PRP_INTR_CH2FC | PRP_INTR_LBOVF | |
| 308 | PRP_INTR_CH2OVF, |
| 309 | } |
| 310 | }, |
| 311 | }; |
| 312 | |
| 313 | static struct mx2_fmt_cfg *mx27_emma_prp_get_format( |
| 314 | enum v4l2_mbus_pixelcode in_fmt, |
| 315 | u32 out_fmt) |
| 316 | { |
| 317 | int i; |
| 318 | |
| 319 | for (i = 1; i < ARRAY_SIZE(mx27_emma_prp_table); i++) |
| 320 | if ((mx27_emma_prp_table[i].in_fmt == in_fmt) && |
| 321 | (mx27_emma_prp_table[i].out_fmt == out_fmt)) { |
| 322 | return &mx27_emma_prp_table[i]; |
| 323 | } |
| 324 | /* If no match return the most generic configuration */ |
| 325 | return &mx27_emma_prp_table[0]; |
| 326 | }; |
| 327 | |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 328 | static void mx2_camera_deactivate(struct mx2_camera_dev *pcdev) |
| 329 | { |
| 330 | unsigned long flags; |
| 331 | |
| 332 | clk_disable(pcdev->clk_csi); |
| 333 | writel(0, pcdev->base_csi + CSICR1); |
Sascha Hauer | 2c9ba37 | 2012-02-20 07:55:24 -0300 | [diff] [blame] | 334 | if (cpu_is_mx27()) { |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 335 | writel(0, pcdev->base_emma + PRP_CNTL); |
| 336 | } else if (cpu_is_mx25()) { |
| 337 | spin_lock_irqsave(&pcdev->lock, flags); |
| 338 | pcdev->fb1_active = NULL; |
| 339 | pcdev->fb2_active = NULL; |
| 340 | writel(0, pcdev->base_csi + CSIDMASA_FB1); |
| 341 | writel(0, pcdev->base_csi + CSIDMASA_FB2); |
| 342 | spin_unlock_irqrestore(&pcdev->lock, flags); |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | /* |
| 347 | * The following two functions absolutely depend on the fact, that |
| 348 | * there can be only one camera on mx2 camera sensor interface |
| 349 | */ |
| 350 | static int mx2_camera_add_device(struct soc_camera_device *icd) |
| 351 | { |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 352 | struct soc_camera_host *ici = to_soc_camera_host(icd->parent); |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 353 | struct mx2_camera_dev *pcdev = ici->priv; |
| 354 | int ret; |
| 355 | u32 csicr1; |
| 356 | |
| 357 | if (pcdev->icd) |
| 358 | return -EBUSY; |
| 359 | |
| 360 | ret = clk_enable(pcdev->clk_csi); |
| 361 | if (ret < 0) |
| 362 | return ret; |
| 363 | |
| 364 | csicr1 = CSICR1_MCLKEN; |
| 365 | |
Sascha Hauer | 2c9ba37 | 2012-02-20 07:55:24 -0300 | [diff] [blame] | 366 | if (cpu_is_mx27()) { |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 367 | csicr1 |= CSICR1_PRP_IF_EN | CSICR1_FCC | |
| 368 | CSICR1_RXFF_LEVEL(0); |
| 369 | } else if (cpu_is_mx27()) |
| 370 | csicr1 |= CSICR1_SOF_INTEN | CSICR1_RXFF_LEVEL(2); |
| 371 | |
| 372 | pcdev->csicr1 = csicr1; |
| 373 | writel(pcdev->csicr1, pcdev->base_csi + CSICR1); |
| 374 | |
| 375 | pcdev->icd = icd; |
Javier Martin | ccd1a49 | 2011-12-22 12:12:00 -0300 | [diff] [blame] | 376 | pcdev->frame_count = 0; |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 377 | |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 378 | dev_info(icd->parent, "Camera driver attached to camera %d\n", |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 379 | icd->devnum); |
| 380 | |
| 381 | return 0; |
| 382 | } |
| 383 | |
| 384 | static void mx2_camera_remove_device(struct soc_camera_device *icd) |
| 385 | { |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 386 | struct soc_camera_host *ici = to_soc_camera_host(icd->parent); |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 387 | struct mx2_camera_dev *pcdev = ici->priv; |
| 388 | |
| 389 | BUG_ON(icd != pcdev->icd); |
| 390 | |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 391 | dev_info(icd->parent, "Camera driver detached from camera %d\n", |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 392 | icd->devnum); |
| 393 | |
| 394 | mx2_camera_deactivate(pcdev); |
| 395 | |
| 396 | if (pcdev->discard_buffer) { |
| 397 | dma_free_coherent(ici->v4l2_dev.dev, pcdev->discard_size, |
| 398 | pcdev->discard_buffer, |
| 399 | pcdev->discard_buffer_dma); |
| 400 | pcdev->discard_buffer = NULL; |
| 401 | } |
| 402 | |
| 403 | pcdev->icd = NULL; |
| 404 | } |
| 405 | |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 406 | static void mx25_camera_frame_done(struct mx2_camera_dev *pcdev, int fb, |
| 407 | int state) |
| 408 | { |
Javier Martin | c6a41e3 | 2012-01-30 09:14:10 -0300 | [diff] [blame^] | 409 | struct vb2_buffer *vb; |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 410 | struct mx2_buffer *buf; |
| 411 | struct mx2_buffer **fb_active = fb == 1 ? &pcdev->fb1_active : |
| 412 | &pcdev->fb2_active; |
| 413 | u32 fb_reg = fb == 1 ? CSIDMASA_FB1 : CSIDMASA_FB2; |
| 414 | unsigned long flags; |
| 415 | |
| 416 | spin_lock_irqsave(&pcdev->lock, flags); |
| 417 | |
Baruch Siach | 5384a12 | 2010-07-27 09:06:07 -0300 | [diff] [blame] | 418 | if (*fb_active == NULL) |
| 419 | goto out; |
| 420 | |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 421 | vb = &(*fb_active)->vb; |
Javier Martin | c6a41e3 | 2012-01-30 09:14:10 -0300 | [diff] [blame^] | 422 | dev_dbg(pcdev->dev, "%s (vb=0x%p) 0x%p %lu\n", __func__, |
| 423 | vb, vb2_plane_vaddr(vb, 0), vb2_get_plane_payload(vb, 0)); |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 424 | |
Javier Martin | c6a41e3 | 2012-01-30 09:14:10 -0300 | [diff] [blame^] | 425 | do_gettimeofday(&vb->v4l2_buf.timestamp); |
| 426 | vb->v4l2_buf.sequence++; |
| 427 | vb2_buffer_done(vb, VB2_BUF_STATE_DONE); |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 428 | |
| 429 | if (list_empty(&pcdev->capture)) { |
| 430 | buf = NULL; |
| 431 | writel(0, pcdev->base_csi + fb_reg); |
| 432 | } else { |
| 433 | buf = list_entry(pcdev->capture.next, struct mx2_buffer, |
Javier Martin | c6a41e3 | 2012-01-30 09:14:10 -0300 | [diff] [blame^] | 434 | queue); |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 435 | vb = &buf->vb; |
Javier Martin | c6a41e3 | 2012-01-30 09:14:10 -0300 | [diff] [blame^] | 436 | list_del(&buf->queue); |
| 437 | buf->state = MX2_STATE_ACTIVE; |
| 438 | writel(vb2_dma_contig_plane_dma_addr(vb, 0), |
| 439 | pcdev->base_csi + fb_reg); |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | *fb_active = buf; |
| 443 | |
Baruch Siach | 5384a12 | 2010-07-27 09:06:07 -0300 | [diff] [blame] | 444 | out: |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 445 | spin_unlock_irqrestore(&pcdev->lock, flags); |
| 446 | } |
| 447 | |
| 448 | static irqreturn_t mx25_camera_irq(int irq_csi, void *data) |
| 449 | { |
| 450 | struct mx2_camera_dev *pcdev = data; |
| 451 | u32 status = readl(pcdev->base_csi + CSISR); |
| 452 | |
| 453 | if (status & CSISR_DMA_TSF_FB1_INT) |
Javier Martin | c6a41e3 | 2012-01-30 09:14:10 -0300 | [diff] [blame^] | 454 | mx25_camera_frame_done(pcdev, 1, MX2_STATE_DONE); |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 455 | else if (status & CSISR_DMA_TSF_FB2_INT) |
Javier Martin | c6a41e3 | 2012-01-30 09:14:10 -0300 | [diff] [blame^] | 456 | mx25_camera_frame_done(pcdev, 2, MX2_STATE_DONE); |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 457 | |
| 458 | /* FIXME: handle CSISR_RFF_OR_INT */ |
| 459 | |
| 460 | writel(status, pcdev->base_csi + CSISR); |
| 461 | |
| 462 | return IRQ_HANDLED; |
| 463 | } |
| 464 | |
| 465 | /* |
| 466 | * Videobuf operations |
| 467 | */ |
Javier Martin | c6a41e3 | 2012-01-30 09:14:10 -0300 | [diff] [blame^] | 468 | static int mx2_videobuf_setup(struct vb2_queue *vq, |
| 469 | const struct v4l2_format *fmt, |
| 470 | unsigned int *count, unsigned int *num_planes, |
| 471 | unsigned int sizes[], void *alloc_ctxs[]) |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 472 | { |
Javier Martin | c6a41e3 | 2012-01-30 09:14:10 -0300 | [diff] [blame^] | 473 | struct soc_camera_device *icd = soc_camera_from_vb2q(vq); |
| 474 | struct soc_camera_host *ici = to_soc_camera_host(icd->parent); |
| 475 | struct mx2_camera_dev *pcdev = ici->priv; |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 476 | int bytes_per_line = soc_mbus_bytes_per_line(icd->user_width, |
| 477 | icd->current_fmt->host_fmt); |
| 478 | |
Javier Martin | c6a41e3 | 2012-01-30 09:14:10 -0300 | [diff] [blame^] | 479 | dev_dbg(icd->parent, "count=%d, size=%d\n", *count, sizes[0]); |
| 480 | |
| 481 | /* TODO: support for VIDIOC_CREATE_BUFS not ready */ |
| 482 | if (fmt != NULL) |
| 483 | return -ENOTTY; |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 484 | |
| 485 | if (bytes_per_line < 0) |
| 486 | return bytes_per_line; |
| 487 | |
Javier Martin | c6a41e3 | 2012-01-30 09:14:10 -0300 | [diff] [blame^] | 488 | alloc_ctxs[0] = pcdev->alloc_ctx; |
| 489 | |
| 490 | sizes[0] = bytes_per_line * icd->user_height; |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 491 | |
| 492 | if (0 == *count) |
| 493 | *count = 32; |
Javier Martin | c6a41e3 | 2012-01-30 09:14:10 -0300 | [diff] [blame^] | 494 | if (!*num_planes && |
| 495 | sizes[0] * *count > MAX_VIDEO_MEM * 1024 * 1024) |
| 496 | *count = (MAX_VIDEO_MEM * 1024 * 1024) / sizes[0]; |
| 497 | |
| 498 | *num_planes = 1; |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 499 | |
| 500 | return 0; |
| 501 | } |
| 502 | |
Javier Martin | c6a41e3 | 2012-01-30 09:14:10 -0300 | [diff] [blame^] | 503 | static int mx2_videobuf_prepare(struct vb2_buffer *vb) |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 504 | { |
Javier Martin | c6a41e3 | 2012-01-30 09:14:10 -0300 | [diff] [blame^] | 505 | struct soc_camera_device *icd = soc_camera_from_vb2q(vb->vb2_queue); |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 506 | int bytes_per_line = soc_mbus_bytes_per_line(icd->user_width, |
| 507 | icd->current_fmt->host_fmt); |
| 508 | int ret = 0; |
| 509 | |
Javier Martin | c6a41e3 | 2012-01-30 09:14:10 -0300 | [diff] [blame^] | 510 | dev_dbg(icd->parent, "%s (vb=0x%p) 0x%p %lu\n", __func__, |
| 511 | vb, vb2_plane_vaddr(vb, 0), vb2_get_plane_payload(vb, 0)); |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 512 | |
| 513 | if (bytes_per_line < 0) |
| 514 | return bytes_per_line; |
| 515 | |
| 516 | #ifdef DEBUG |
| 517 | /* |
| 518 | * This can be useful if you want to see if we actually fill |
| 519 | * the buffer with something |
| 520 | */ |
Javier Martin | c6a41e3 | 2012-01-30 09:14:10 -0300 | [diff] [blame^] | 521 | memset((void *)vb2_plane_vaddr(vb, 0), |
| 522 | 0xaa, vb2_get_plane_payload(vb, 0)); |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 523 | #endif |
| 524 | |
Javier Martin | c6a41e3 | 2012-01-30 09:14:10 -0300 | [diff] [blame^] | 525 | vb2_set_plane_payload(vb, 0, bytes_per_line * icd->user_height); |
| 526 | if (vb2_plane_vaddr(vb, 0) && |
| 527 | vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0)) { |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 528 | ret = -EINVAL; |
| 529 | goto out; |
| 530 | } |
| 531 | |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 532 | return 0; |
| 533 | |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 534 | out: |
| 535 | return ret; |
| 536 | } |
| 537 | |
Javier Martin | c6a41e3 | 2012-01-30 09:14:10 -0300 | [diff] [blame^] | 538 | static void mx2_videobuf_queue(struct vb2_buffer *vb) |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 539 | { |
Javier Martin | c6a41e3 | 2012-01-30 09:14:10 -0300 | [diff] [blame^] | 540 | struct soc_camera_device *icd = soc_camera_from_vb2q(vb->vb2_queue); |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 541 | struct soc_camera_host *ici = |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 542 | to_soc_camera_host(icd->parent); |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 543 | struct mx2_camera_dev *pcdev = ici->priv; |
| 544 | struct mx2_buffer *buf = container_of(vb, struct mx2_buffer, vb); |
| 545 | unsigned long flags; |
| 546 | |
Javier Martin | c6a41e3 | 2012-01-30 09:14:10 -0300 | [diff] [blame^] | 547 | dev_dbg(icd->parent, "%s (vb=0x%p) 0x%p %lu\n", __func__, |
| 548 | vb, vb2_plane_vaddr(vb, 0), vb2_get_plane_payload(vb, 0)); |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 549 | |
| 550 | spin_lock_irqsave(&pcdev->lock, flags); |
| 551 | |
Javier Martin | c6a41e3 | 2012-01-30 09:14:10 -0300 | [diff] [blame^] | 552 | buf->state = MX2_STATE_QUEUED; |
| 553 | list_add_tail(&buf->queue, &pcdev->capture); |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 554 | |
Sascha Hauer | 2c9ba37 | 2012-02-20 07:55:24 -0300 | [diff] [blame] | 555 | if (cpu_is_mx25()) { |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 556 | u32 csicr3, dma_inten = 0; |
| 557 | |
| 558 | if (pcdev->fb1_active == NULL) { |
Javier Martin | c6a41e3 | 2012-01-30 09:14:10 -0300 | [diff] [blame^] | 559 | writel(vb2_dma_contig_plane_dma_addr(vb, 0), |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 560 | pcdev->base_csi + CSIDMASA_FB1); |
| 561 | pcdev->fb1_active = buf; |
| 562 | dma_inten = CSICR1_FB1_DMA_INTEN; |
| 563 | } else if (pcdev->fb2_active == NULL) { |
Javier Martin | c6a41e3 | 2012-01-30 09:14:10 -0300 | [diff] [blame^] | 564 | writel(vb2_dma_contig_plane_dma_addr(vb, 0), |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 565 | pcdev->base_csi + CSIDMASA_FB2); |
| 566 | pcdev->fb2_active = buf; |
| 567 | dma_inten = CSICR1_FB2_DMA_INTEN; |
| 568 | } |
| 569 | |
| 570 | if (dma_inten) { |
Javier Martin | c6a41e3 | 2012-01-30 09:14:10 -0300 | [diff] [blame^] | 571 | list_del(&buf->queue); |
| 572 | buf->state = MX2_STATE_ACTIVE; |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 573 | |
| 574 | csicr3 = readl(pcdev->base_csi + CSICR3); |
| 575 | |
| 576 | /* Reflash DMA */ |
| 577 | writel(csicr3 | CSICR3_DMA_REFLASH_RFF, |
| 578 | pcdev->base_csi + CSICR3); |
| 579 | |
| 580 | /* clear & enable interrupts */ |
| 581 | writel(dma_inten, pcdev->base_csi + CSISR); |
| 582 | pcdev->csicr1 |= dma_inten; |
| 583 | writel(pcdev->csicr1, pcdev->base_csi + CSICR1); |
| 584 | |
| 585 | /* enable DMA */ |
| 586 | csicr3 |= CSICR3_DMA_REQ_EN_RFF | CSICR3_RXFF_LEVEL(1); |
| 587 | writel(csicr3, pcdev->base_csi + CSICR3); |
| 588 | } |
| 589 | } |
| 590 | |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 591 | spin_unlock_irqrestore(&pcdev->lock, flags); |
| 592 | } |
| 593 | |
Javier Martin | c6a41e3 | 2012-01-30 09:14:10 -0300 | [diff] [blame^] | 594 | static void mx2_videobuf_release(struct vb2_buffer *vb) |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 595 | { |
Javier Martin | c6a41e3 | 2012-01-30 09:14:10 -0300 | [diff] [blame^] | 596 | struct soc_camera_device *icd = soc_camera_from_vb2q(vb->vb2_queue); |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 597 | struct soc_camera_host *ici = to_soc_camera_host(icd->parent); |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 598 | struct mx2_camera_dev *pcdev = ici->priv; |
| 599 | struct mx2_buffer *buf = container_of(vb, struct mx2_buffer, vb); |
| 600 | unsigned long flags; |
| 601 | |
| 602 | #ifdef DEBUG |
Javier Martin | c6a41e3 | 2012-01-30 09:14:10 -0300 | [diff] [blame^] | 603 | dev_dbg(icd->parent, "%s (vb=0x%p) 0x%p %lu\n", __func__, |
| 604 | vb, vb2_plane_vaddr(vb, 0), vb2_get_plane_payload(vb, 0)); |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 605 | |
Javier Martin | c6a41e3 | 2012-01-30 09:14:10 -0300 | [diff] [blame^] | 606 | switch (buf->state) { |
| 607 | case MX2_STATE_ACTIVE: |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 608 | dev_info(icd->parent, "%s (active)\n", __func__); |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 609 | break; |
Javier Martin | c6a41e3 | 2012-01-30 09:14:10 -0300 | [diff] [blame^] | 610 | case MX2_STATE_QUEUED: |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 611 | dev_info(icd->parent, "%s (queued)\n", __func__); |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 612 | break; |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 613 | default: |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 614 | dev_info(icd->parent, "%s (unknown) %d\n", __func__, |
Javier Martin | c6a41e3 | 2012-01-30 09:14:10 -0300 | [diff] [blame^] | 615 | buf->state); |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 616 | break; |
| 617 | } |
| 618 | #endif |
| 619 | |
| 620 | /* |
| 621 | * Terminate only queued but inactive buffers. Active buffers are |
| 622 | * released when they become inactive after videobuf_waiton(). |
| 623 | * |
Baruch Siach | 7c6b731 | 2010-07-27 09:06:10 -0300 | [diff] [blame] | 624 | * FIXME: implement forced termination of active buffers for mx27 and |
| 625 | * mx27 eMMA, so that the user won't get stuck in an uninterruptible |
| 626 | * state. This requires a specific handling for each of the these DMA |
| 627 | * types. |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 628 | */ |
Javier Martin | c6a41e3 | 2012-01-30 09:14:10 -0300 | [diff] [blame^] | 629 | |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 630 | spin_lock_irqsave(&pcdev->lock, flags); |
Javier Martin | c6a41e3 | 2012-01-30 09:14:10 -0300 | [diff] [blame^] | 631 | list_del_init(&buf->queue); |
| 632 | if (cpu_is_mx25() && buf->state == MX2_STATE_ACTIVE) { |
Baruch Siach | 7c6b731 | 2010-07-27 09:06:10 -0300 | [diff] [blame] | 633 | if (pcdev->fb1_active == buf) { |
| 634 | pcdev->csicr1 &= ~CSICR1_FB1_DMA_INTEN; |
| 635 | writel(0, pcdev->base_csi + CSIDMASA_FB1); |
| 636 | pcdev->fb1_active = NULL; |
| 637 | } else if (pcdev->fb2_active == buf) { |
| 638 | pcdev->csicr1 &= ~CSICR1_FB2_DMA_INTEN; |
| 639 | writel(0, pcdev->base_csi + CSIDMASA_FB2); |
| 640 | pcdev->fb2_active = NULL; |
| 641 | } |
| 642 | writel(pcdev->csicr1, pcdev->base_csi + CSICR1); |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 643 | } |
| 644 | spin_unlock_irqrestore(&pcdev->lock, flags); |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 645 | } |
| 646 | |
Javier Martin | c6a41e3 | 2012-01-30 09:14:10 -0300 | [diff] [blame^] | 647 | static struct vb2_ops mx2_videobuf_ops = { |
| 648 | .queue_setup = mx2_videobuf_setup, |
| 649 | .buf_prepare = mx2_videobuf_prepare, |
| 650 | .buf_queue = mx2_videobuf_queue, |
| 651 | .buf_cleanup = mx2_videobuf_release, |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 652 | }; |
| 653 | |
Javier Martin | c6a41e3 | 2012-01-30 09:14:10 -0300 | [diff] [blame^] | 654 | static int mx2_camera_init_videobuf(struct vb2_queue *q, |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 655 | struct soc_camera_device *icd) |
| 656 | { |
Javier Martin | c6a41e3 | 2012-01-30 09:14:10 -0300 | [diff] [blame^] | 657 | q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 658 | q->io_modes = VB2_MMAP | VB2_USERPTR; |
| 659 | q->drv_priv = icd; |
| 660 | q->ops = &mx2_videobuf_ops; |
| 661 | q->mem_ops = &vb2_dma_contig_memops; |
| 662 | q->buf_struct_size = sizeof(struct mx2_buffer); |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 663 | |
Javier Martin | c6a41e3 | 2012-01-30 09:14:10 -0300 | [diff] [blame^] | 664 | return vb2_queue_init(q); |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 665 | } |
| 666 | |
Guennadi Liakhovetski | db592a2 | 2011-07-27 12:38:11 -0300 | [diff] [blame] | 667 | #define MX2_BUS_FLAGS (V4L2_MBUS_MASTER | \ |
| 668 | V4L2_MBUS_VSYNC_ACTIVE_HIGH | \ |
| 669 | V4L2_MBUS_VSYNC_ACTIVE_LOW | \ |
| 670 | V4L2_MBUS_HSYNC_ACTIVE_HIGH | \ |
| 671 | V4L2_MBUS_HSYNC_ACTIVE_LOW | \ |
| 672 | V4L2_MBUS_PCLK_SAMPLE_RISING | \ |
| 673 | V4L2_MBUS_PCLK_SAMPLE_FALLING | \ |
| 674 | V4L2_MBUS_DATA_ACTIVE_HIGH | \ |
| 675 | V4L2_MBUS_DATA_ACTIVE_LOW) |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 676 | |
| 677 | static int mx27_camera_emma_prp_reset(struct mx2_camera_dev *pcdev) |
| 678 | { |
| 679 | u32 cntl; |
| 680 | int count = 0; |
| 681 | |
| 682 | cntl = readl(pcdev->base_emma + PRP_CNTL); |
| 683 | writel(PRP_CNTL_SWRST, pcdev->base_emma + PRP_CNTL); |
| 684 | while (count++ < 100) { |
| 685 | if (!(readl(pcdev->base_emma + PRP_CNTL) & PRP_CNTL_SWRST)) |
| 686 | return 0; |
| 687 | barrier(); |
| 688 | udelay(1); |
| 689 | } |
| 690 | |
| 691 | return -ETIMEDOUT; |
| 692 | } |
| 693 | |
| 694 | static void mx27_camera_emma_buf_init(struct soc_camera_device *icd, |
| 695 | int bytesperline) |
| 696 | { |
| 697 | struct soc_camera_host *ici = |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 698 | to_soc_camera_host(icd->parent); |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 699 | struct mx2_camera_dev *pcdev = ici->priv; |
Javier Martin | f410991 | 2011-12-14 13:30:14 -0300 | [diff] [blame] | 700 | struct mx2_fmt_cfg *prp = pcdev->emma_prp; |
| 701 | u32 imgsize = pcdev->icd->user_height * pcdev->icd->user_width; |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 702 | |
Javier Martin | f410991 | 2011-12-14 13:30:14 -0300 | [diff] [blame] | 703 | if (prp->cfg.channel == 1) { |
| 704 | writel(pcdev->discard_buffer_dma, |
| 705 | pcdev->base_emma + PRP_DEST_RGB1_PTR); |
| 706 | writel(pcdev->discard_buffer_dma, |
| 707 | pcdev->base_emma + PRP_DEST_RGB2_PTR); |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 708 | |
Javier Martin | f410991 | 2011-12-14 13:30:14 -0300 | [diff] [blame] | 709 | writel(PRP_CNTL_CH1EN | |
| 710 | PRP_CNTL_CSIEN | |
| 711 | prp->cfg.in_fmt | |
| 712 | prp->cfg.out_fmt | |
| 713 | PRP_CNTL_CH1_LEN | |
| 714 | PRP_CNTL_CH1BYP | |
| 715 | PRP_CNTL_CH1_TSKIP(0) | |
| 716 | PRP_CNTL_IN_TSKIP(0), |
| 717 | pcdev->base_emma + PRP_CNTL); |
| 718 | |
| 719 | writel((icd->user_width << 16) | icd->user_height, |
| 720 | pcdev->base_emma + PRP_SRC_FRAME_SIZE); |
| 721 | writel((icd->user_width << 16) | icd->user_height, |
| 722 | pcdev->base_emma + PRP_CH1_OUT_IMAGE_SIZE); |
| 723 | writel(bytesperline, |
| 724 | pcdev->base_emma + PRP_DEST_CH1_LINE_STRIDE); |
| 725 | writel(prp->cfg.src_pixel, |
| 726 | pcdev->base_emma + PRP_SRC_PIXEL_FORMAT_CNTL); |
| 727 | writel(prp->cfg.ch1_pixel, |
| 728 | pcdev->base_emma + PRP_CH1_PIXEL_FORMAT_CNTL); |
| 729 | } else { /* channel 2 */ |
| 730 | writel(pcdev->discard_buffer_dma, |
| 731 | pcdev->base_emma + PRP_DEST_Y_PTR); |
| 732 | writel(pcdev->discard_buffer_dma, |
| 733 | pcdev->base_emma + PRP_SOURCE_Y_PTR); |
| 734 | |
| 735 | if (prp->cfg.out_fmt == PRP_CNTL_CH2_OUT_YUV420) { |
| 736 | writel(pcdev->discard_buffer_dma + imgsize, |
| 737 | pcdev->base_emma + PRP_DEST_CB_PTR); |
| 738 | writel(pcdev->discard_buffer_dma + ((5 * imgsize) / 4), |
| 739 | pcdev->base_emma + PRP_DEST_CR_PTR); |
| 740 | writel(pcdev->discard_buffer_dma + imgsize, |
| 741 | pcdev->base_emma + PRP_SOURCE_CB_PTR); |
| 742 | writel(pcdev->discard_buffer_dma + ((5 * imgsize) / 4), |
| 743 | pcdev->base_emma + PRP_SOURCE_CR_PTR); |
| 744 | } |
| 745 | |
| 746 | writel(PRP_CNTL_CH2EN | |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 747 | PRP_CNTL_CSIEN | |
Javier Martin | f410991 | 2011-12-14 13:30:14 -0300 | [diff] [blame] | 748 | prp->cfg.in_fmt | |
| 749 | prp->cfg.out_fmt | |
| 750 | PRP_CNTL_CH2_LEN | |
| 751 | PRP_CNTL_CH2_TSKIP(0) | |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 752 | PRP_CNTL_IN_TSKIP(0), |
| 753 | pcdev->base_emma + PRP_CNTL); |
| 754 | |
Javier Martin | f410991 | 2011-12-14 13:30:14 -0300 | [diff] [blame] | 755 | writel((icd->user_width << 16) | icd->user_height, |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 756 | pcdev->base_emma + PRP_SRC_FRAME_SIZE); |
Javier Martin | f410991 | 2011-12-14 13:30:14 -0300 | [diff] [blame] | 757 | |
| 758 | writel((icd->user_width << 16) | icd->user_height, |
| 759 | pcdev->base_emma + PRP_CH2_OUT_IMAGE_SIZE); |
| 760 | |
| 761 | writel(prp->cfg.src_pixel, |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 762 | pcdev->base_emma + PRP_SRC_PIXEL_FORMAT_CNTL); |
Javier Martin | f410991 | 2011-12-14 13:30:14 -0300 | [diff] [blame] | 763 | |
| 764 | } |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 765 | |
| 766 | /* Enable interrupts */ |
Javier Martin | f410991 | 2011-12-14 13:30:14 -0300 | [diff] [blame] | 767 | writel(prp->cfg.irq_flags, pcdev->base_emma + PRP_INTR_CNTL); |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 768 | } |
| 769 | |
Guennadi Liakhovetski | 8843d11 | 2011-09-21 17:52:51 -0300 | [diff] [blame] | 770 | static int mx2_camera_set_bus_param(struct soc_camera_device *icd) |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 771 | { |
Guennadi Liakhovetski | db592a2 | 2011-07-27 12:38:11 -0300 | [diff] [blame] | 772 | struct v4l2_subdev *sd = soc_camera_to_subdev(icd); |
| 773 | struct soc_camera_host *ici = to_soc_camera_host(icd->parent); |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 774 | struct mx2_camera_dev *pcdev = ici->priv; |
Guennadi Liakhovetski | db592a2 | 2011-07-27 12:38:11 -0300 | [diff] [blame] | 775 | struct v4l2_mbus_config cfg = {.type = V4L2_MBUS_PARALLEL,}; |
| 776 | unsigned long common_flags; |
| 777 | int ret; |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 778 | int bytesperline; |
| 779 | u32 csicr1 = pcdev->csicr1; |
| 780 | |
Guennadi Liakhovetski | db592a2 | 2011-07-27 12:38:11 -0300 | [diff] [blame] | 781 | ret = v4l2_subdev_call(sd, video, g_mbus_config, &cfg); |
| 782 | if (!ret) { |
| 783 | common_flags = soc_mbus_config_compatible(&cfg, MX2_BUS_FLAGS); |
| 784 | if (!common_flags) { |
| 785 | dev_warn(icd->parent, |
| 786 | "Flags incompatible: camera 0x%x, host 0x%x\n", |
| 787 | cfg.flags, MX2_BUS_FLAGS); |
| 788 | return -EINVAL; |
| 789 | } |
| 790 | } else if (ret != -ENOIOCTLCMD) { |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 791 | return ret; |
Guennadi Liakhovetski | db592a2 | 2011-07-27 12:38:11 -0300 | [diff] [blame] | 792 | } else { |
| 793 | common_flags = MX2_BUS_FLAGS; |
| 794 | } |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 795 | |
Guennadi Liakhovetski | db592a2 | 2011-07-27 12:38:11 -0300 | [diff] [blame] | 796 | if ((common_flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH) && |
| 797 | (common_flags & V4L2_MBUS_HSYNC_ACTIVE_LOW)) { |
| 798 | if (pcdev->platform_flags & MX2_CAMERA_HSYNC_HIGH) |
| 799 | common_flags &= ~V4L2_MBUS_HSYNC_ACTIVE_LOW; |
| 800 | else |
| 801 | common_flags &= ~V4L2_MBUS_HSYNC_ACTIVE_HIGH; |
| 802 | } |
| 803 | |
| 804 | if ((common_flags & V4L2_MBUS_PCLK_SAMPLE_RISING) && |
| 805 | (common_flags & V4L2_MBUS_PCLK_SAMPLE_FALLING)) { |
| 806 | if (pcdev->platform_flags & MX2_CAMERA_PCLK_SAMPLE_RISING) |
| 807 | common_flags &= ~V4L2_MBUS_PCLK_SAMPLE_FALLING; |
| 808 | else |
| 809 | common_flags &= ~V4L2_MBUS_PCLK_SAMPLE_RISING; |
| 810 | } |
| 811 | |
| 812 | cfg.flags = common_flags; |
| 813 | ret = v4l2_subdev_call(sd, video, s_mbus_config, &cfg); |
| 814 | if (ret < 0 && ret != -ENOIOCTLCMD) { |
| 815 | dev_dbg(icd->parent, "camera s_mbus_config(0x%lx) returned %d\n", |
| 816 | common_flags, ret); |
| 817 | return ret; |
| 818 | } |
| 819 | |
| 820 | if (common_flags & V4L2_MBUS_PCLK_SAMPLE_RISING) |
Michael Grzeschik | d86097e | 2010-08-03 06:37:55 -0300 | [diff] [blame] | 821 | csicr1 |= CSICR1_REDGE; |
Guennadi Liakhovetski | db592a2 | 2011-07-27 12:38:11 -0300 | [diff] [blame] | 822 | if (common_flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH) |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 823 | csicr1 |= CSICR1_SOF_POL; |
Guennadi Liakhovetski | db592a2 | 2011-07-27 12:38:11 -0300 | [diff] [blame] | 824 | if (common_flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH) |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 825 | csicr1 |= CSICR1_HSYNC_POL; |
| 826 | if (pcdev->platform_flags & MX2_CAMERA_SWAP16) |
| 827 | csicr1 |= CSICR1_SWAP16_EN; |
| 828 | if (pcdev->platform_flags & MX2_CAMERA_EXT_VSYNC) |
| 829 | csicr1 |= CSICR1_EXT_VSYNC; |
| 830 | if (pcdev->platform_flags & MX2_CAMERA_CCIR) |
| 831 | csicr1 |= CSICR1_CCIR_EN; |
| 832 | if (pcdev->platform_flags & MX2_CAMERA_CCIR_INTERLACE) |
| 833 | csicr1 |= CSICR1_CCIR_MODE; |
| 834 | if (pcdev->platform_flags & MX2_CAMERA_GATED_CLOCK) |
| 835 | csicr1 |= CSICR1_GCLK_MODE; |
| 836 | if (pcdev->platform_flags & MX2_CAMERA_INV_DATA) |
| 837 | csicr1 |= CSICR1_INV_DATA; |
| 838 | if (pcdev->platform_flags & MX2_CAMERA_PACK_DIR_MSB) |
| 839 | csicr1 |= CSICR1_PACK_DIR; |
| 840 | |
| 841 | pcdev->csicr1 = csicr1; |
| 842 | |
| 843 | bytesperline = soc_mbus_bytes_per_line(icd->user_width, |
| 844 | icd->current_fmt->host_fmt); |
| 845 | if (bytesperline < 0) |
| 846 | return bytesperline; |
| 847 | |
Sascha Hauer | 2c9ba37 | 2012-02-20 07:55:24 -0300 | [diff] [blame] | 848 | if (cpu_is_mx27()) { |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 849 | ret = mx27_camera_emma_prp_reset(pcdev); |
| 850 | if (ret) |
| 851 | return ret; |
| 852 | |
| 853 | if (pcdev->discard_buffer) |
| 854 | dma_free_coherent(ici->v4l2_dev.dev, |
| 855 | pcdev->discard_size, pcdev->discard_buffer, |
| 856 | pcdev->discard_buffer_dma); |
| 857 | |
| 858 | /* |
| 859 | * I didn't manage to properly enable/disable the prp |
| 860 | * on a per frame basis during running transfers, |
| 861 | * thus we allocate a buffer here and use it to |
| 862 | * discard frames when no buffer is available. |
| 863 | * Feel free to work on this ;) |
| 864 | */ |
| 865 | pcdev->discard_size = icd->user_height * bytesperline; |
| 866 | pcdev->discard_buffer = dma_alloc_coherent(ici->v4l2_dev.dev, |
| 867 | pcdev->discard_size, &pcdev->discard_buffer_dma, |
| 868 | GFP_KERNEL); |
| 869 | if (!pcdev->discard_buffer) |
| 870 | return -ENOMEM; |
| 871 | |
| 872 | mx27_camera_emma_buf_init(icd, bytesperline); |
| 873 | } else if (cpu_is_mx25()) { |
| 874 | writel((bytesperline * icd->user_height) >> 2, |
| 875 | pcdev->base_csi + CSIRXCNT); |
| 876 | writel((bytesperline << 16) | icd->user_height, |
| 877 | pcdev->base_csi + CSIIMAG_PARA); |
| 878 | } |
| 879 | |
| 880 | writel(pcdev->csicr1, pcdev->base_csi + CSICR1); |
| 881 | |
| 882 | return 0; |
| 883 | } |
| 884 | |
| 885 | static int mx2_camera_set_crop(struct soc_camera_device *icd, |
| 886 | struct v4l2_crop *a) |
| 887 | { |
| 888 | struct v4l2_rect *rect = &a->c; |
| 889 | struct v4l2_subdev *sd = soc_camera_to_subdev(icd); |
| 890 | struct v4l2_mbus_framefmt mf; |
| 891 | int ret; |
| 892 | |
| 893 | soc_camera_limit_side(&rect->left, &rect->width, 0, 2, 4096); |
| 894 | soc_camera_limit_side(&rect->top, &rect->height, 0, 2, 4096); |
| 895 | |
| 896 | ret = v4l2_subdev_call(sd, video, s_crop, a); |
| 897 | if (ret < 0) |
| 898 | return ret; |
| 899 | |
| 900 | /* The capture device might have changed its output */ |
| 901 | ret = v4l2_subdev_call(sd, video, g_mbus_fmt, &mf); |
| 902 | if (ret < 0) |
| 903 | return ret; |
| 904 | |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 905 | dev_dbg(icd->parent, "Sensor cropped %dx%d\n", |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 906 | mf.width, mf.height); |
| 907 | |
| 908 | icd->user_width = mf.width; |
| 909 | icd->user_height = mf.height; |
| 910 | |
| 911 | return ret; |
| 912 | } |
| 913 | |
Javier Martin | f410991 | 2011-12-14 13:30:14 -0300 | [diff] [blame] | 914 | static int mx2_camera_get_formats(struct soc_camera_device *icd, |
| 915 | unsigned int idx, |
| 916 | struct soc_camera_format_xlate *xlate) |
| 917 | { |
| 918 | struct v4l2_subdev *sd = soc_camera_to_subdev(icd); |
| 919 | const struct soc_mbus_pixelfmt *fmt; |
| 920 | struct device *dev = icd->parent; |
| 921 | enum v4l2_mbus_pixelcode code; |
| 922 | int ret, formats = 0; |
| 923 | |
| 924 | ret = v4l2_subdev_call(sd, video, enum_mbus_fmt, idx, &code); |
| 925 | if (ret < 0) |
| 926 | /* no more formats */ |
| 927 | return 0; |
| 928 | |
| 929 | fmt = soc_mbus_get_fmtdesc(code); |
| 930 | if (!fmt) { |
| 931 | dev_err(dev, "Invalid format code #%u: %d\n", idx, code); |
| 932 | return 0; |
| 933 | } |
| 934 | |
| 935 | if (code == V4L2_MBUS_FMT_YUYV8_2X8) { |
| 936 | formats++; |
| 937 | if (xlate) { |
| 938 | /* |
| 939 | * CH2 can output YUV420 which is a standard format in |
| 940 | * soc_mediabus.c |
| 941 | */ |
| 942 | xlate->host_fmt = |
| 943 | soc_mbus_get_fmtdesc(V4L2_MBUS_FMT_YUYV8_1_5X8); |
| 944 | xlate->code = code; |
| 945 | dev_dbg(dev, "Providing host format %s for sensor code %d\n", |
| 946 | xlate->host_fmt->name, code); |
| 947 | xlate++; |
| 948 | } |
| 949 | } |
| 950 | |
| 951 | /* Generic pass-trough */ |
| 952 | formats++; |
| 953 | if (xlate) { |
| 954 | xlate->host_fmt = fmt; |
| 955 | xlate->code = code; |
| 956 | xlate++; |
| 957 | } |
| 958 | return formats; |
| 959 | } |
| 960 | |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 961 | static int mx2_camera_set_fmt(struct soc_camera_device *icd, |
| 962 | struct v4l2_format *f) |
| 963 | { |
Javier Martin | f410991 | 2011-12-14 13:30:14 -0300 | [diff] [blame] | 964 | struct soc_camera_host *ici = to_soc_camera_host(icd->parent); |
| 965 | struct mx2_camera_dev *pcdev = ici->priv; |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 966 | struct v4l2_subdev *sd = soc_camera_to_subdev(icd); |
| 967 | const struct soc_camera_format_xlate *xlate; |
| 968 | struct v4l2_pix_format *pix = &f->fmt.pix; |
| 969 | struct v4l2_mbus_framefmt mf; |
| 970 | int ret; |
| 971 | |
| 972 | xlate = soc_camera_xlate_by_fourcc(icd, pix->pixelformat); |
| 973 | if (!xlate) { |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 974 | dev_warn(icd->parent, "Format %x not found\n", |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 975 | pix->pixelformat); |
| 976 | return -EINVAL; |
| 977 | } |
| 978 | |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 979 | mf.width = pix->width; |
| 980 | mf.height = pix->height; |
| 981 | mf.field = pix->field; |
| 982 | mf.colorspace = pix->colorspace; |
| 983 | mf.code = xlate->code; |
| 984 | |
| 985 | ret = v4l2_subdev_call(sd, video, s_mbus_fmt, &mf); |
| 986 | if (ret < 0 && ret != -ENOIOCTLCMD) |
| 987 | return ret; |
| 988 | |
| 989 | if (mf.code != xlate->code) |
| 990 | return -EINVAL; |
| 991 | |
| 992 | pix->width = mf.width; |
| 993 | pix->height = mf.height; |
| 994 | pix->field = mf.field; |
| 995 | pix->colorspace = mf.colorspace; |
| 996 | icd->current_fmt = xlate; |
| 997 | |
Sascha Hauer | 2c9ba37 | 2012-02-20 07:55:24 -0300 | [diff] [blame] | 998 | if (cpu_is_mx27()) |
Javier Martin | f410991 | 2011-12-14 13:30:14 -0300 | [diff] [blame] | 999 | pcdev->emma_prp = mx27_emma_prp_get_format(xlate->code, |
| 1000 | xlate->host_fmt->fourcc); |
| 1001 | |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 1002 | return 0; |
| 1003 | } |
| 1004 | |
| 1005 | static int mx2_camera_try_fmt(struct soc_camera_device *icd, |
| 1006 | struct v4l2_format *f) |
| 1007 | { |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 1008 | struct v4l2_subdev *sd = soc_camera_to_subdev(icd); |
| 1009 | const struct soc_camera_format_xlate *xlate; |
| 1010 | struct v4l2_pix_format *pix = &f->fmt.pix; |
| 1011 | struct v4l2_mbus_framefmt mf; |
| 1012 | __u32 pixfmt = pix->pixelformat; |
| 1013 | unsigned int width_limit; |
| 1014 | int ret; |
| 1015 | |
| 1016 | xlate = soc_camera_xlate_by_fourcc(icd, pixfmt); |
| 1017 | if (pixfmt && !xlate) { |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 1018 | dev_warn(icd->parent, "Format %x not found\n", pixfmt); |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 1019 | return -EINVAL; |
| 1020 | } |
| 1021 | |
| 1022 | /* FIXME: implement MX27 limits */ |
| 1023 | |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 1024 | /* limit to MX25 hardware capabilities */ |
| 1025 | if (cpu_is_mx25()) { |
| 1026 | if (xlate->host_fmt->bits_per_sample <= 8) |
| 1027 | width_limit = 0xffff * 4; |
| 1028 | else |
| 1029 | width_limit = 0xffff * 2; |
| 1030 | /* CSIIMAG_PARA limit */ |
| 1031 | if (pix->width > width_limit) |
| 1032 | pix->width = width_limit; |
| 1033 | if (pix->height > 0xffff) |
| 1034 | pix->height = 0xffff; |
| 1035 | |
| 1036 | pix->bytesperline = soc_mbus_bytes_per_line(pix->width, |
| 1037 | xlate->host_fmt); |
| 1038 | if (pix->bytesperline < 0) |
| 1039 | return pix->bytesperline; |
| 1040 | pix->sizeimage = pix->height * pix->bytesperline; |
Guennadi Liakhovetski | 28281a7 | 2011-06-04 15:06:47 -0300 | [diff] [blame] | 1041 | /* Check against the CSIRXCNT limit */ |
| 1042 | if (pix->sizeimage > 4 * 0x3ffff) { |
| 1043 | /* Adjust geometry, preserve aspect ratio */ |
| 1044 | unsigned int new_height = int_sqrt(4 * 0x3ffff * |
| 1045 | pix->height / pix->bytesperline); |
| 1046 | pix->width = new_height * pix->width / pix->height; |
| 1047 | pix->height = new_height; |
| 1048 | pix->bytesperline = soc_mbus_bytes_per_line(pix->width, |
| 1049 | xlate->host_fmt); |
| 1050 | BUG_ON(pix->bytesperline < 0); |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 1051 | } |
| 1052 | } |
| 1053 | |
| 1054 | /* limit to sensor capabilities */ |
| 1055 | mf.width = pix->width; |
| 1056 | mf.height = pix->height; |
| 1057 | mf.field = pix->field; |
| 1058 | mf.colorspace = pix->colorspace; |
| 1059 | mf.code = xlate->code; |
| 1060 | |
| 1061 | ret = v4l2_subdev_call(sd, video, try_mbus_fmt, &mf); |
| 1062 | if (ret < 0) |
| 1063 | return ret; |
| 1064 | |
| 1065 | if (mf.field == V4L2_FIELD_ANY) |
| 1066 | mf.field = V4L2_FIELD_NONE; |
Javier Martin | f410991 | 2011-12-14 13:30:14 -0300 | [diff] [blame] | 1067 | /* |
| 1068 | * Driver supports interlaced images provided they have |
| 1069 | * both fields so that they can be processed as if they |
| 1070 | * were progressive. |
| 1071 | */ |
| 1072 | if (mf.field != V4L2_FIELD_NONE && !V4L2_FIELD_HAS_BOTH(mf.field)) { |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 1073 | dev_err(icd->parent, "Field type %d unsupported.\n", |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 1074 | mf.field); |
| 1075 | return -EINVAL; |
| 1076 | } |
| 1077 | |
| 1078 | pix->width = mf.width; |
| 1079 | pix->height = mf.height; |
| 1080 | pix->field = mf.field; |
| 1081 | pix->colorspace = mf.colorspace; |
| 1082 | |
| 1083 | return 0; |
| 1084 | } |
| 1085 | |
| 1086 | static int mx2_camera_querycap(struct soc_camera_host *ici, |
| 1087 | struct v4l2_capability *cap) |
| 1088 | { |
| 1089 | /* cap->name is set by the friendly caller:-> */ |
| 1090 | strlcpy(cap->card, MX2_CAM_DRIVER_DESCRIPTION, sizeof(cap->card)); |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 1091 | cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING; |
| 1092 | |
| 1093 | return 0; |
| 1094 | } |
| 1095 | |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 1096 | static unsigned int mx2_camera_poll(struct file *file, poll_table *pt) |
| 1097 | { |
Sascha Hauer | 6b10192 | 2010-11-08 17:52:45 -0300 | [diff] [blame] | 1098 | struct soc_camera_device *icd = file->private_data; |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 1099 | |
Javier Martin | c6a41e3 | 2012-01-30 09:14:10 -0300 | [diff] [blame^] | 1100 | return vb2_poll(&icd->vb2_vidq, file, pt); |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 1101 | } |
| 1102 | |
| 1103 | static struct soc_camera_host_ops mx2_soc_camera_host_ops = { |
| 1104 | .owner = THIS_MODULE, |
| 1105 | .add = mx2_camera_add_device, |
| 1106 | .remove = mx2_camera_remove_device, |
| 1107 | .set_fmt = mx2_camera_set_fmt, |
| 1108 | .set_crop = mx2_camera_set_crop, |
Javier Martin | f410991 | 2011-12-14 13:30:14 -0300 | [diff] [blame] | 1109 | .get_formats = mx2_camera_get_formats, |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 1110 | .try_fmt = mx2_camera_try_fmt, |
Javier Martin | c6a41e3 | 2012-01-30 09:14:10 -0300 | [diff] [blame^] | 1111 | .init_videobuf2 = mx2_camera_init_videobuf, |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 1112 | .poll = mx2_camera_poll, |
| 1113 | .querycap = mx2_camera_querycap, |
| 1114 | .set_bus_param = mx2_camera_set_bus_param, |
| 1115 | }; |
| 1116 | |
| 1117 | static void mx27_camera_frame_done_emma(struct mx2_camera_dev *pcdev, |
Javier Martin | c6a41e3 | 2012-01-30 09:14:10 -0300 | [diff] [blame^] | 1118 | int bufnum) |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 1119 | { |
Javier Martin | f410991 | 2011-12-14 13:30:14 -0300 | [diff] [blame] | 1120 | u32 imgsize = pcdev->icd->user_height * pcdev->icd->user_width; |
| 1121 | struct mx2_fmt_cfg *prp = pcdev->emma_prp; |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 1122 | struct mx2_buffer *buf; |
Javier Martin | c6a41e3 | 2012-01-30 09:14:10 -0300 | [diff] [blame^] | 1123 | struct vb2_buffer *vb; |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 1124 | unsigned long phys; |
| 1125 | |
| 1126 | if (!list_empty(&pcdev->active_bufs)) { |
| 1127 | buf = list_entry(pcdev->active_bufs.next, |
Javier Martin | c6a41e3 | 2012-01-30 09:14:10 -0300 | [diff] [blame^] | 1128 | struct mx2_buffer, queue); |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 1129 | |
| 1130 | BUG_ON(buf->bufnum != bufnum); |
| 1131 | |
| 1132 | vb = &buf->vb; |
| 1133 | #ifdef DEBUG |
Javier Martin | c6a41e3 | 2012-01-30 09:14:10 -0300 | [diff] [blame^] | 1134 | phys = vb2_dma_contig_plane_dma_addr(vb, 0); |
Javier Martin | f410991 | 2011-12-14 13:30:14 -0300 | [diff] [blame] | 1135 | if (prp->cfg.channel == 1) { |
| 1136 | if (readl(pcdev->base_emma + PRP_DEST_RGB1_PTR + |
| 1137 | 4 * bufnum) != phys) { |
| 1138 | dev_err(pcdev->dev, "%p != %p\n", phys, |
| 1139 | readl(pcdev->base_emma + |
| 1140 | PRP_DEST_RGB1_PTR + |
| 1141 | 4 * bufnum)); |
| 1142 | } |
| 1143 | } else { |
| 1144 | if (readl(pcdev->base_emma + PRP_DEST_Y_PTR - |
| 1145 | 0x14 * bufnum) != phys) { |
| 1146 | dev_err(pcdev->dev, "%p != %p\n", phys, |
| 1147 | readl(pcdev->base_emma + |
| 1148 | PRP_DEST_Y_PTR - |
| 1149 | 0x14 * bufnum)); |
| 1150 | } |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 1151 | } |
| 1152 | #endif |
Javier Martin | c6a41e3 | 2012-01-30 09:14:10 -0300 | [diff] [blame^] | 1153 | dev_dbg(pcdev->dev, "%s (vb=0x%p) 0x%p %lu\n", __func__, vb, |
| 1154 | vb2_plane_vaddr(vb, 0), |
| 1155 | vb2_get_plane_payload(vb, 0)); |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 1156 | |
Javier Martin | c6a41e3 | 2012-01-30 09:14:10 -0300 | [diff] [blame^] | 1157 | list_del_init(&buf->queue); |
| 1158 | do_gettimeofday(&vb->v4l2_buf.timestamp); |
Javier Martin | ccd1a49 | 2011-12-22 12:12:00 -0300 | [diff] [blame] | 1159 | pcdev->frame_count++; |
Javier Martin | c6a41e3 | 2012-01-30 09:14:10 -0300 | [diff] [blame^] | 1160 | vb->v4l2_buf.sequence = pcdev->frame_count; |
| 1161 | vb2_buffer_done(vb, VB2_BUF_STATE_DONE); |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 1162 | } |
| 1163 | |
| 1164 | if (list_empty(&pcdev->capture)) { |
Javier Martin | f410991 | 2011-12-14 13:30:14 -0300 | [diff] [blame] | 1165 | if (prp->cfg.channel == 1) { |
| 1166 | writel(pcdev->discard_buffer_dma, pcdev->base_emma + |
| 1167 | PRP_DEST_RGB1_PTR + 4 * bufnum); |
| 1168 | } else { |
| 1169 | writel(pcdev->discard_buffer_dma, pcdev->base_emma + |
| 1170 | PRP_DEST_Y_PTR - |
| 1171 | 0x14 * bufnum); |
| 1172 | if (prp->out_fmt == V4L2_PIX_FMT_YUV420) { |
| 1173 | writel(pcdev->discard_buffer_dma + imgsize, |
| 1174 | pcdev->base_emma + PRP_DEST_CB_PTR - |
| 1175 | 0x14 * bufnum); |
| 1176 | writel(pcdev->discard_buffer_dma + |
| 1177 | ((5 * imgsize) / 4), pcdev->base_emma + |
| 1178 | PRP_DEST_CR_PTR - 0x14 * bufnum); |
| 1179 | } |
| 1180 | } |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 1181 | return; |
| 1182 | } |
| 1183 | |
| 1184 | buf = list_entry(pcdev->capture.next, |
Javier Martin | c6a41e3 | 2012-01-30 09:14:10 -0300 | [diff] [blame^] | 1185 | struct mx2_buffer, queue); |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 1186 | |
Michael Grzeschik | cd9ebdb | 2010-08-03 06:37:54 -0300 | [diff] [blame] | 1187 | buf->bufnum = !bufnum; |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 1188 | |
| 1189 | list_move_tail(pcdev->capture.next, &pcdev->active_bufs); |
| 1190 | |
| 1191 | vb = &buf->vb; |
Javier Martin | c6a41e3 | 2012-01-30 09:14:10 -0300 | [diff] [blame^] | 1192 | buf->state = MX2_STATE_ACTIVE; |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 1193 | |
Javier Martin | c6a41e3 | 2012-01-30 09:14:10 -0300 | [diff] [blame^] | 1194 | phys = vb2_dma_contig_plane_dma_addr(vb, 0); |
Javier Martin | f410991 | 2011-12-14 13:30:14 -0300 | [diff] [blame] | 1195 | if (prp->cfg.channel == 1) { |
| 1196 | writel(phys, pcdev->base_emma + PRP_DEST_RGB1_PTR + 4 * bufnum); |
| 1197 | } else { |
| 1198 | writel(phys, pcdev->base_emma + |
| 1199 | PRP_DEST_Y_PTR - 0x14 * bufnum); |
| 1200 | if (prp->cfg.out_fmt == PRP_CNTL_CH2_OUT_YUV420) { |
| 1201 | writel(phys + imgsize, pcdev->base_emma + |
| 1202 | PRP_DEST_CB_PTR - 0x14 * bufnum); |
| 1203 | writel(phys + ((5 * imgsize) / 4), pcdev->base_emma + |
| 1204 | PRP_DEST_CR_PTR - 0x14 * bufnum); |
| 1205 | } |
| 1206 | } |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 1207 | } |
| 1208 | |
| 1209 | static irqreturn_t mx27_camera_emma_irq(int irq_emma, void *data) |
| 1210 | { |
| 1211 | struct mx2_camera_dev *pcdev = data; |
| 1212 | unsigned int status = readl(pcdev->base_emma + PRP_INTRSTATUS); |
| 1213 | struct mx2_buffer *buf; |
| 1214 | |
| 1215 | if (status & (1 << 7)) { /* overflow */ |
| 1216 | u32 cntl; |
| 1217 | /* |
| 1218 | * We only disable channel 1 here since this is the only |
| 1219 | * enabled channel |
| 1220 | * |
| 1221 | * FIXME: the correct DMA overflow handling should be resetting |
| 1222 | * the buffer, returning an error frame, and continuing with |
| 1223 | * the next one. |
| 1224 | */ |
| 1225 | cntl = readl(pcdev->base_emma + PRP_CNTL); |
Javier Martin | f410991 | 2011-12-14 13:30:14 -0300 | [diff] [blame] | 1226 | writel(cntl & ~(PRP_CNTL_CH1EN | PRP_CNTL_CH2EN), |
| 1227 | pcdev->base_emma + PRP_CNTL); |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 1228 | writel(cntl, pcdev->base_emma + PRP_CNTL); |
| 1229 | } |
Javier Martin | f410991 | 2011-12-14 13:30:14 -0300 | [diff] [blame] | 1230 | if ((((status & (3 << 5)) == (3 << 5)) || |
| 1231 | ((status & (3 << 3)) == (3 << 3))) |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 1232 | && !list_empty(&pcdev->active_bufs)) { |
| 1233 | /* |
| 1234 | * Both buffers have triggered, process the one we're expecting |
| 1235 | * to first |
| 1236 | */ |
| 1237 | buf = list_entry(pcdev->active_bufs.next, |
Javier Martin | c6a41e3 | 2012-01-30 09:14:10 -0300 | [diff] [blame^] | 1238 | struct mx2_buffer, queue); |
| 1239 | mx27_camera_frame_done_emma(pcdev, buf->bufnum); |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 1240 | status &= ~(1 << (6 - buf->bufnum)); /* mark processed */ |
| 1241 | } |
Javier Martin | f410991 | 2011-12-14 13:30:14 -0300 | [diff] [blame] | 1242 | if ((status & (1 << 6)) || (status & (1 << 4))) |
Javier Martin | c6a41e3 | 2012-01-30 09:14:10 -0300 | [diff] [blame^] | 1243 | mx27_camera_frame_done_emma(pcdev, 0); |
Javier Martin | f410991 | 2011-12-14 13:30:14 -0300 | [diff] [blame] | 1244 | if ((status & (1 << 5)) || (status & (1 << 3))) |
Javier Martin | c6a41e3 | 2012-01-30 09:14:10 -0300 | [diff] [blame^] | 1245 | mx27_camera_frame_done_emma(pcdev, 1); |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 1246 | |
| 1247 | writel(status, pcdev->base_emma + PRP_INTRSTATUS); |
| 1248 | |
| 1249 | return IRQ_HANDLED; |
| 1250 | } |
| 1251 | |
| 1252 | static int __devinit mx27_camera_emma_init(struct mx2_camera_dev *pcdev) |
| 1253 | { |
| 1254 | struct resource *res_emma = pcdev->res_emma; |
| 1255 | int err = 0; |
| 1256 | |
| 1257 | if (!request_mem_region(res_emma->start, resource_size(res_emma), |
| 1258 | MX2_CAM_DRV_NAME)) { |
| 1259 | err = -EBUSY; |
| 1260 | goto out; |
| 1261 | } |
| 1262 | |
| 1263 | pcdev->base_emma = ioremap(res_emma->start, resource_size(res_emma)); |
| 1264 | if (!pcdev->base_emma) { |
| 1265 | err = -ENOMEM; |
| 1266 | goto exit_release; |
| 1267 | } |
| 1268 | |
| 1269 | err = request_irq(pcdev->irq_emma, mx27_camera_emma_irq, 0, |
| 1270 | MX2_CAM_DRV_NAME, pcdev); |
| 1271 | if (err) { |
| 1272 | dev_err(pcdev->dev, "Camera EMMA interrupt register failed \n"); |
| 1273 | goto exit_iounmap; |
| 1274 | } |
| 1275 | |
| 1276 | pcdev->clk_emma = clk_get(NULL, "emma"); |
| 1277 | if (IS_ERR(pcdev->clk_emma)) { |
| 1278 | err = PTR_ERR(pcdev->clk_emma); |
| 1279 | goto exit_free_irq; |
| 1280 | } |
| 1281 | |
| 1282 | clk_enable(pcdev->clk_emma); |
| 1283 | |
| 1284 | err = mx27_camera_emma_prp_reset(pcdev); |
| 1285 | if (err) |
| 1286 | goto exit_clk_emma_put; |
| 1287 | |
| 1288 | return err; |
| 1289 | |
| 1290 | exit_clk_emma_put: |
| 1291 | clk_disable(pcdev->clk_emma); |
| 1292 | clk_put(pcdev->clk_emma); |
| 1293 | exit_free_irq: |
| 1294 | free_irq(pcdev->irq_emma, pcdev); |
| 1295 | exit_iounmap: |
| 1296 | iounmap(pcdev->base_emma); |
| 1297 | exit_release: |
| 1298 | release_mem_region(res_emma->start, resource_size(res_emma)); |
| 1299 | out: |
| 1300 | return err; |
| 1301 | } |
| 1302 | |
| 1303 | static int __devinit mx2_camera_probe(struct platform_device *pdev) |
| 1304 | { |
| 1305 | struct mx2_camera_dev *pcdev; |
| 1306 | struct resource *res_csi, *res_emma; |
| 1307 | void __iomem *base_csi; |
| 1308 | int irq_csi, irq_emma; |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 1309 | int err = 0; |
| 1310 | |
| 1311 | dev_dbg(&pdev->dev, "initialising\n"); |
| 1312 | |
| 1313 | res_csi = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 1314 | irq_csi = platform_get_irq(pdev, 0); |
| 1315 | if (res_csi == NULL || irq_csi < 0) { |
| 1316 | dev_err(&pdev->dev, "Missing platform resources data\n"); |
| 1317 | err = -ENODEV; |
| 1318 | goto exit; |
| 1319 | } |
| 1320 | |
| 1321 | pcdev = kzalloc(sizeof(*pcdev), GFP_KERNEL); |
| 1322 | if (!pcdev) { |
| 1323 | dev_err(&pdev->dev, "Could not allocate pcdev\n"); |
| 1324 | err = -ENOMEM; |
| 1325 | goto exit; |
| 1326 | } |
| 1327 | |
| 1328 | pcdev->clk_csi = clk_get(&pdev->dev, NULL); |
| 1329 | if (IS_ERR(pcdev->clk_csi)) { |
| 1330 | err = PTR_ERR(pcdev->clk_csi); |
| 1331 | goto exit_kfree; |
| 1332 | } |
| 1333 | |
| 1334 | dev_dbg(&pdev->dev, "Camera clock frequency: %ld\n", |
| 1335 | clk_get_rate(pcdev->clk_csi)); |
| 1336 | |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 1337 | pcdev->res_csi = res_csi; |
| 1338 | pcdev->pdata = pdev->dev.platform_data; |
| 1339 | if (pcdev->pdata) { |
| 1340 | long rate; |
| 1341 | |
| 1342 | pcdev->platform_flags = pcdev->pdata->flags; |
| 1343 | |
| 1344 | rate = clk_round_rate(pcdev->clk_csi, pcdev->pdata->clk * 2); |
| 1345 | if (rate <= 0) { |
| 1346 | err = -ENODEV; |
| 1347 | goto exit_dma_free; |
| 1348 | } |
| 1349 | err = clk_set_rate(pcdev->clk_csi, rate); |
| 1350 | if (err < 0) |
| 1351 | goto exit_dma_free; |
| 1352 | } |
| 1353 | |
| 1354 | INIT_LIST_HEAD(&pcdev->capture); |
| 1355 | INIT_LIST_HEAD(&pcdev->active_bufs); |
| 1356 | spin_lock_init(&pcdev->lock); |
| 1357 | |
| 1358 | /* |
| 1359 | * Request the regions. |
| 1360 | */ |
| 1361 | if (!request_mem_region(res_csi->start, resource_size(res_csi), |
| 1362 | MX2_CAM_DRV_NAME)) { |
| 1363 | err = -EBUSY; |
| 1364 | goto exit_dma_free; |
| 1365 | } |
| 1366 | |
| 1367 | base_csi = ioremap(res_csi->start, resource_size(res_csi)); |
| 1368 | if (!base_csi) { |
| 1369 | err = -ENOMEM; |
| 1370 | goto exit_release; |
| 1371 | } |
| 1372 | pcdev->irq_csi = irq_csi; |
| 1373 | pcdev->base_csi = base_csi; |
| 1374 | pcdev->base_dma = res_csi->start; |
| 1375 | pcdev->dev = &pdev->dev; |
| 1376 | |
Sascha Hauer | 2c9ba37 | 2012-02-20 07:55:24 -0300 | [diff] [blame] | 1377 | if (cpu_is_mx25()) { |
| 1378 | err = request_irq(pcdev->irq_csi, mx25_camera_irq, 0, |
| 1379 | MX2_CAM_DRV_NAME, pcdev); |
| 1380 | if (err) { |
| 1381 | dev_err(pcdev->dev, "Camera interrupt register failed \n"); |
| 1382 | goto exit_iounmap; |
| 1383 | } |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 1384 | } |
| 1385 | |
| 1386 | if (cpu_is_mx27()) { |
| 1387 | /* EMMA support */ |
| 1388 | res_emma = platform_get_resource(pdev, IORESOURCE_MEM, 1); |
| 1389 | irq_emma = platform_get_irq(pdev, 1); |
| 1390 | |
Sascha Hauer | 2c9ba37 | 2012-02-20 07:55:24 -0300 | [diff] [blame] | 1391 | if (!res_emma || !irq_emma) { |
| 1392 | dev_err(&pdev->dev, "no EMMA resources\n"); |
| 1393 | goto exit_free_irq; |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 1394 | } |
Sascha Hauer | 2c9ba37 | 2012-02-20 07:55:24 -0300 | [diff] [blame] | 1395 | |
| 1396 | pcdev->res_emma = res_emma; |
| 1397 | pcdev->irq_emma = irq_emma; |
| 1398 | if (mx27_camera_emma_init(pcdev)) |
| 1399 | goto exit_free_irq; |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 1400 | } |
| 1401 | |
| 1402 | pcdev->soc_host.drv_name = MX2_CAM_DRV_NAME, |
| 1403 | pcdev->soc_host.ops = &mx2_soc_camera_host_ops, |
| 1404 | pcdev->soc_host.priv = pcdev; |
| 1405 | pcdev->soc_host.v4l2_dev.dev = &pdev->dev; |
| 1406 | pcdev->soc_host.nr = pdev->id; |
Javier Martin | c6a41e3 | 2012-01-30 09:14:10 -0300 | [diff] [blame^] | 1407 | |
| 1408 | pcdev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev); |
| 1409 | if (IS_ERR(pcdev->alloc_ctx)) { |
| 1410 | err = PTR_ERR(pcdev->alloc_ctx); |
| 1411 | goto eallocctx; |
| 1412 | } |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 1413 | err = soc_camera_host_register(&pcdev->soc_host); |
| 1414 | if (err) |
| 1415 | goto exit_free_emma; |
| 1416 | |
Michael Grzeschik | 45f4d4e | 2010-08-27 09:39:05 -0300 | [diff] [blame] | 1417 | dev_info(&pdev->dev, "MX2 Camera (CSI) driver probed, clock frequency: %ld\n", |
| 1418 | clk_get_rate(pcdev->clk_csi)); |
| 1419 | |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 1420 | return 0; |
| 1421 | |
| 1422 | exit_free_emma: |
Javier Martin | c6a41e3 | 2012-01-30 09:14:10 -0300 | [diff] [blame^] | 1423 | vb2_dma_contig_cleanup_ctx(pcdev->alloc_ctx); |
| 1424 | eallocctx: |
Sascha Hauer | 2c9ba37 | 2012-02-20 07:55:24 -0300 | [diff] [blame] | 1425 | if (cpu_is_mx27()) { |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 1426 | free_irq(pcdev->irq_emma, pcdev); |
| 1427 | clk_disable(pcdev->clk_emma); |
| 1428 | clk_put(pcdev->clk_emma); |
| 1429 | iounmap(pcdev->base_emma); |
Sascha Hauer | 2c9ba37 | 2012-02-20 07:55:24 -0300 | [diff] [blame] | 1430 | release_mem_region(pcdev->res_emma->start, resource_size(pcdev->res_emma)); |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 1431 | } |
| 1432 | exit_free_irq: |
Sascha Hauer | 2c9ba37 | 2012-02-20 07:55:24 -0300 | [diff] [blame] | 1433 | if (cpu_is_mx25()) |
| 1434 | free_irq(pcdev->irq_csi, pcdev); |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 1435 | exit_iounmap: |
| 1436 | iounmap(base_csi); |
| 1437 | exit_release: |
| 1438 | release_mem_region(res_csi->start, resource_size(res_csi)); |
| 1439 | exit_dma_free: |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 1440 | clk_put(pcdev->clk_csi); |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 1441 | exit_kfree: |
| 1442 | kfree(pcdev); |
| 1443 | exit: |
| 1444 | return err; |
| 1445 | } |
| 1446 | |
| 1447 | static int __devexit mx2_camera_remove(struct platform_device *pdev) |
| 1448 | { |
| 1449 | struct soc_camera_host *soc_host = to_soc_camera_host(&pdev->dev); |
| 1450 | struct mx2_camera_dev *pcdev = container_of(soc_host, |
| 1451 | struct mx2_camera_dev, soc_host); |
| 1452 | struct resource *res; |
| 1453 | |
| 1454 | clk_put(pcdev->clk_csi); |
Sascha Hauer | 2c9ba37 | 2012-02-20 07:55:24 -0300 | [diff] [blame] | 1455 | if (cpu_is_mx25()) |
| 1456 | free_irq(pcdev->irq_csi, pcdev); |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 1457 | if (cpu_is_mx27()) |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 1458 | free_irq(pcdev->irq_emma, pcdev); |
| 1459 | |
| 1460 | soc_camera_host_unregister(&pcdev->soc_host); |
| 1461 | |
Javier Martin | c6a41e3 | 2012-01-30 09:14:10 -0300 | [diff] [blame^] | 1462 | vb2_dma_contig_cleanup_ctx(pcdev->alloc_ctx); |
| 1463 | |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 1464 | iounmap(pcdev->base_csi); |
| 1465 | |
Sascha Hauer | 2c9ba37 | 2012-02-20 07:55:24 -0300 | [diff] [blame] | 1466 | if (cpu_is_mx27()) { |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 1467 | clk_disable(pcdev->clk_emma); |
| 1468 | clk_put(pcdev->clk_emma); |
| 1469 | iounmap(pcdev->base_emma); |
| 1470 | res = pcdev->res_emma; |
| 1471 | release_mem_region(res->start, resource_size(res)); |
| 1472 | } |
| 1473 | |
| 1474 | res = pcdev->res_csi; |
| 1475 | release_mem_region(res->start, resource_size(res)); |
| 1476 | |
| 1477 | kfree(pcdev); |
| 1478 | |
| 1479 | dev_info(&pdev->dev, "MX2 Camera driver unloaded\n"); |
| 1480 | |
| 1481 | return 0; |
| 1482 | } |
| 1483 | |
| 1484 | static struct platform_driver mx2_camera_driver = { |
| 1485 | .driver = { |
| 1486 | .name = MX2_CAM_DRV_NAME, |
| 1487 | }, |
| 1488 | .remove = __devexit_p(mx2_camera_remove), |
| 1489 | }; |
| 1490 | |
| 1491 | |
| 1492 | static int __init mx2_camera_init(void) |
| 1493 | { |
| 1494 | return platform_driver_probe(&mx2_camera_driver, &mx2_camera_probe); |
| 1495 | } |
| 1496 | |
| 1497 | static void __exit mx2_camera_exit(void) |
| 1498 | { |
| 1499 | return platform_driver_unregister(&mx2_camera_driver); |
| 1500 | } |
| 1501 | |
| 1502 | module_init(mx2_camera_init); |
| 1503 | module_exit(mx2_camera_exit); |
| 1504 | |
| 1505 | MODULE_DESCRIPTION("i.MX27/i.MX25 SoC Camera Host driver"); |
| 1506 | MODULE_AUTHOR("Sascha Hauer <sha@pengutronix.de>"); |
| 1507 | MODULE_LICENSE("GPL"); |
Mauro Carvalho Chehab | 64dc3c1 | 2011-06-25 11:28:37 -0300 | [diff] [blame] | 1508 | MODULE_VERSION(MX2_CAM_VERSION); |