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 | fb51cbd | 2012-01-30 09:14:11 -0300 | [diff] [blame^] | 376 | pcdev->frame_count = -1; |
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 | fb51cbd | 2012-01-30 09:14:11 -0300 | [diff] [blame^] | 647 | static int mx2_start_streaming(struct vb2_queue *q, unsigned int count) |
| 648 | { |
| 649 | struct soc_camera_device *icd = soc_camera_from_vb2q(q); |
| 650 | struct soc_camera_host *ici = |
| 651 | to_soc_camera_host(icd->parent); |
| 652 | struct mx2_camera_dev *pcdev = ici->priv; |
| 653 | struct mx2_fmt_cfg *prp = pcdev->emma_prp; |
| 654 | |
| 655 | if (cpu_is_mx27()) { |
| 656 | unsigned long flags; |
| 657 | if (count < 2) |
| 658 | return -EINVAL; |
| 659 | |
| 660 | spin_lock_irqsave(&pcdev->lock, flags); |
| 661 | if (prp->cfg.channel == 1) { |
| 662 | writel(PRP_CNTL_CH1EN | |
| 663 | PRP_CNTL_CSIEN | |
| 664 | prp->cfg.in_fmt | |
| 665 | prp->cfg.out_fmt | |
| 666 | PRP_CNTL_CH1_LEN | |
| 667 | PRP_CNTL_CH1BYP | |
| 668 | PRP_CNTL_CH1_TSKIP(0) | |
| 669 | PRP_CNTL_IN_TSKIP(0), |
| 670 | pcdev->base_emma + PRP_CNTL); |
| 671 | } else { |
| 672 | writel(PRP_CNTL_CH2EN | |
| 673 | PRP_CNTL_CSIEN | |
| 674 | prp->cfg.in_fmt | |
| 675 | prp->cfg.out_fmt | |
| 676 | PRP_CNTL_CH2_LEN | |
| 677 | PRP_CNTL_CH2_TSKIP(0) | |
| 678 | PRP_CNTL_IN_TSKIP(0), |
| 679 | pcdev->base_emma + PRP_CNTL); |
| 680 | } |
| 681 | spin_unlock_irqrestore(&pcdev->lock, flags); |
| 682 | } |
| 683 | |
| 684 | return 0; |
| 685 | } |
| 686 | |
| 687 | static int mx2_stop_streaming(struct vb2_queue *q) |
| 688 | { |
| 689 | struct soc_camera_device *icd = soc_camera_from_vb2q(q); |
| 690 | struct soc_camera_host *ici = |
| 691 | to_soc_camera_host(icd->parent); |
| 692 | struct mx2_camera_dev *pcdev = ici->priv; |
| 693 | struct mx2_fmt_cfg *prp = pcdev->emma_prp; |
| 694 | unsigned long flags; |
| 695 | u32 cntl; |
| 696 | |
| 697 | spin_lock_irqsave(&pcdev->lock, flags); |
| 698 | if (cpu_is_mx27()) { |
| 699 | cntl = readl(pcdev->base_emma + PRP_CNTL); |
| 700 | if (prp->cfg.channel == 1) { |
| 701 | writel(cntl & ~PRP_CNTL_CH1EN, |
| 702 | pcdev->base_emma + PRP_CNTL); |
| 703 | } else { |
| 704 | writel(cntl & ~PRP_CNTL_CH2EN, |
| 705 | pcdev->base_emma + PRP_CNTL); |
| 706 | } |
| 707 | } |
| 708 | spin_unlock_irqrestore(&pcdev->lock, flags); |
| 709 | |
| 710 | return 0; |
| 711 | } |
| 712 | |
Javier Martin | c6a41e3 | 2012-01-30 09:14:10 -0300 | [diff] [blame] | 713 | static struct vb2_ops mx2_videobuf_ops = { |
Javier Martin | fb51cbd | 2012-01-30 09:14:11 -0300 | [diff] [blame^] | 714 | .queue_setup = mx2_videobuf_setup, |
| 715 | .buf_prepare = mx2_videobuf_prepare, |
| 716 | .buf_queue = mx2_videobuf_queue, |
| 717 | .buf_cleanup = mx2_videobuf_release, |
| 718 | .start_streaming = mx2_start_streaming, |
| 719 | .stop_streaming = mx2_stop_streaming, |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 720 | }; |
| 721 | |
Javier Martin | c6a41e3 | 2012-01-30 09:14:10 -0300 | [diff] [blame] | 722 | static int mx2_camera_init_videobuf(struct vb2_queue *q, |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 723 | struct soc_camera_device *icd) |
| 724 | { |
Javier Martin | c6a41e3 | 2012-01-30 09:14:10 -0300 | [diff] [blame] | 725 | q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 726 | q->io_modes = VB2_MMAP | VB2_USERPTR; |
| 727 | q->drv_priv = icd; |
| 728 | q->ops = &mx2_videobuf_ops; |
| 729 | q->mem_ops = &vb2_dma_contig_memops; |
| 730 | q->buf_struct_size = sizeof(struct mx2_buffer); |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 731 | |
Javier Martin | c6a41e3 | 2012-01-30 09:14:10 -0300 | [diff] [blame] | 732 | return vb2_queue_init(q); |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 733 | } |
| 734 | |
Guennadi Liakhovetski | db592a2 | 2011-07-27 12:38:11 -0300 | [diff] [blame] | 735 | #define MX2_BUS_FLAGS (V4L2_MBUS_MASTER | \ |
| 736 | V4L2_MBUS_VSYNC_ACTIVE_HIGH | \ |
| 737 | V4L2_MBUS_VSYNC_ACTIVE_LOW | \ |
| 738 | V4L2_MBUS_HSYNC_ACTIVE_HIGH | \ |
| 739 | V4L2_MBUS_HSYNC_ACTIVE_LOW | \ |
| 740 | V4L2_MBUS_PCLK_SAMPLE_RISING | \ |
| 741 | V4L2_MBUS_PCLK_SAMPLE_FALLING | \ |
| 742 | V4L2_MBUS_DATA_ACTIVE_HIGH | \ |
| 743 | V4L2_MBUS_DATA_ACTIVE_LOW) |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 744 | |
| 745 | static int mx27_camera_emma_prp_reset(struct mx2_camera_dev *pcdev) |
| 746 | { |
| 747 | u32 cntl; |
| 748 | int count = 0; |
| 749 | |
| 750 | cntl = readl(pcdev->base_emma + PRP_CNTL); |
| 751 | writel(PRP_CNTL_SWRST, pcdev->base_emma + PRP_CNTL); |
| 752 | while (count++ < 100) { |
| 753 | if (!(readl(pcdev->base_emma + PRP_CNTL) & PRP_CNTL_SWRST)) |
| 754 | return 0; |
| 755 | barrier(); |
| 756 | udelay(1); |
| 757 | } |
| 758 | |
| 759 | return -ETIMEDOUT; |
| 760 | } |
| 761 | |
| 762 | static void mx27_camera_emma_buf_init(struct soc_camera_device *icd, |
| 763 | int bytesperline) |
| 764 | { |
| 765 | struct soc_camera_host *ici = |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 766 | to_soc_camera_host(icd->parent); |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 767 | struct mx2_camera_dev *pcdev = ici->priv; |
Javier Martin | f410991 | 2011-12-14 13:30:14 -0300 | [diff] [blame] | 768 | struct mx2_fmt_cfg *prp = pcdev->emma_prp; |
| 769 | u32 imgsize = pcdev->icd->user_height * pcdev->icd->user_width; |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 770 | |
Javier Martin | f410991 | 2011-12-14 13:30:14 -0300 | [diff] [blame] | 771 | if (prp->cfg.channel == 1) { |
| 772 | writel(pcdev->discard_buffer_dma, |
| 773 | pcdev->base_emma + PRP_DEST_RGB1_PTR); |
| 774 | writel(pcdev->discard_buffer_dma, |
| 775 | pcdev->base_emma + PRP_DEST_RGB2_PTR); |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 776 | |
Javier Martin | f410991 | 2011-12-14 13:30:14 -0300 | [diff] [blame] | 777 | writel((icd->user_width << 16) | icd->user_height, |
| 778 | pcdev->base_emma + PRP_SRC_FRAME_SIZE); |
| 779 | writel((icd->user_width << 16) | icd->user_height, |
| 780 | pcdev->base_emma + PRP_CH1_OUT_IMAGE_SIZE); |
| 781 | writel(bytesperline, |
| 782 | pcdev->base_emma + PRP_DEST_CH1_LINE_STRIDE); |
| 783 | writel(prp->cfg.src_pixel, |
| 784 | pcdev->base_emma + PRP_SRC_PIXEL_FORMAT_CNTL); |
| 785 | writel(prp->cfg.ch1_pixel, |
| 786 | pcdev->base_emma + PRP_CH1_PIXEL_FORMAT_CNTL); |
| 787 | } else { /* channel 2 */ |
| 788 | writel(pcdev->discard_buffer_dma, |
| 789 | pcdev->base_emma + PRP_DEST_Y_PTR); |
| 790 | writel(pcdev->discard_buffer_dma, |
| 791 | pcdev->base_emma + PRP_SOURCE_Y_PTR); |
| 792 | |
| 793 | if (prp->cfg.out_fmt == PRP_CNTL_CH2_OUT_YUV420) { |
| 794 | writel(pcdev->discard_buffer_dma + imgsize, |
| 795 | pcdev->base_emma + PRP_DEST_CB_PTR); |
| 796 | writel(pcdev->discard_buffer_dma + ((5 * imgsize) / 4), |
| 797 | pcdev->base_emma + PRP_DEST_CR_PTR); |
| 798 | writel(pcdev->discard_buffer_dma + imgsize, |
| 799 | pcdev->base_emma + PRP_SOURCE_CB_PTR); |
| 800 | writel(pcdev->discard_buffer_dma + ((5 * imgsize) / 4), |
| 801 | pcdev->base_emma + PRP_SOURCE_CR_PTR); |
| 802 | } |
| 803 | |
Javier Martin | f410991 | 2011-12-14 13:30:14 -0300 | [diff] [blame] | 804 | writel((icd->user_width << 16) | icd->user_height, |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 805 | pcdev->base_emma + PRP_SRC_FRAME_SIZE); |
Javier Martin | f410991 | 2011-12-14 13:30:14 -0300 | [diff] [blame] | 806 | |
| 807 | writel((icd->user_width << 16) | icd->user_height, |
| 808 | pcdev->base_emma + PRP_CH2_OUT_IMAGE_SIZE); |
| 809 | |
| 810 | writel(prp->cfg.src_pixel, |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 811 | pcdev->base_emma + PRP_SRC_PIXEL_FORMAT_CNTL); |
Javier Martin | f410991 | 2011-12-14 13:30:14 -0300 | [diff] [blame] | 812 | |
| 813 | } |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 814 | |
| 815 | /* Enable interrupts */ |
Javier Martin | f410991 | 2011-12-14 13:30:14 -0300 | [diff] [blame] | 816 | writel(prp->cfg.irq_flags, pcdev->base_emma + PRP_INTR_CNTL); |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 817 | } |
| 818 | |
Guennadi Liakhovetski | 8843d11 | 2011-09-21 17:52:51 -0300 | [diff] [blame] | 819 | static int mx2_camera_set_bus_param(struct soc_camera_device *icd) |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 820 | { |
Guennadi Liakhovetski | db592a2 | 2011-07-27 12:38:11 -0300 | [diff] [blame] | 821 | struct v4l2_subdev *sd = soc_camera_to_subdev(icd); |
| 822 | struct soc_camera_host *ici = to_soc_camera_host(icd->parent); |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 823 | struct mx2_camera_dev *pcdev = ici->priv; |
Guennadi Liakhovetski | db592a2 | 2011-07-27 12:38:11 -0300 | [diff] [blame] | 824 | struct v4l2_mbus_config cfg = {.type = V4L2_MBUS_PARALLEL,}; |
| 825 | unsigned long common_flags; |
| 826 | int ret; |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 827 | int bytesperline; |
| 828 | u32 csicr1 = pcdev->csicr1; |
| 829 | |
Guennadi Liakhovetski | db592a2 | 2011-07-27 12:38:11 -0300 | [diff] [blame] | 830 | ret = v4l2_subdev_call(sd, video, g_mbus_config, &cfg); |
| 831 | if (!ret) { |
| 832 | common_flags = soc_mbus_config_compatible(&cfg, MX2_BUS_FLAGS); |
| 833 | if (!common_flags) { |
| 834 | dev_warn(icd->parent, |
| 835 | "Flags incompatible: camera 0x%x, host 0x%x\n", |
| 836 | cfg.flags, MX2_BUS_FLAGS); |
| 837 | return -EINVAL; |
| 838 | } |
| 839 | } else if (ret != -ENOIOCTLCMD) { |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 840 | return ret; |
Guennadi Liakhovetski | db592a2 | 2011-07-27 12:38:11 -0300 | [diff] [blame] | 841 | } else { |
| 842 | common_flags = MX2_BUS_FLAGS; |
| 843 | } |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 844 | |
Guennadi Liakhovetski | db592a2 | 2011-07-27 12:38:11 -0300 | [diff] [blame] | 845 | if ((common_flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH) && |
| 846 | (common_flags & V4L2_MBUS_HSYNC_ACTIVE_LOW)) { |
| 847 | if (pcdev->platform_flags & MX2_CAMERA_HSYNC_HIGH) |
| 848 | common_flags &= ~V4L2_MBUS_HSYNC_ACTIVE_LOW; |
| 849 | else |
| 850 | common_flags &= ~V4L2_MBUS_HSYNC_ACTIVE_HIGH; |
| 851 | } |
| 852 | |
| 853 | if ((common_flags & V4L2_MBUS_PCLK_SAMPLE_RISING) && |
| 854 | (common_flags & V4L2_MBUS_PCLK_SAMPLE_FALLING)) { |
| 855 | if (pcdev->platform_flags & MX2_CAMERA_PCLK_SAMPLE_RISING) |
| 856 | common_flags &= ~V4L2_MBUS_PCLK_SAMPLE_FALLING; |
| 857 | else |
| 858 | common_flags &= ~V4L2_MBUS_PCLK_SAMPLE_RISING; |
| 859 | } |
| 860 | |
| 861 | cfg.flags = common_flags; |
| 862 | ret = v4l2_subdev_call(sd, video, s_mbus_config, &cfg); |
| 863 | if (ret < 0 && ret != -ENOIOCTLCMD) { |
| 864 | dev_dbg(icd->parent, "camera s_mbus_config(0x%lx) returned %d\n", |
| 865 | common_flags, ret); |
| 866 | return ret; |
| 867 | } |
| 868 | |
| 869 | if (common_flags & V4L2_MBUS_PCLK_SAMPLE_RISING) |
Michael Grzeschik | d86097e | 2010-08-03 06:37:55 -0300 | [diff] [blame] | 870 | csicr1 |= CSICR1_REDGE; |
Guennadi Liakhovetski | db592a2 | 2011-07-27 12:38:11 -0300 | [diff] [blame] | 871 | if (common_flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH) |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 872 | csicr1 |= CSICR1_SOF_POL; |
Guennadi Liakhovetski | db592a2 | 2011-07-27 12:38:11 -0300 | [diff] [blame] | 873 | if (common_flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH) |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 874 | csicr1 |= CSICR1_HSYNC_POL; |
| 875 | if (pcdev->platform_flags & MX2_CAMERA_SWAP16) |
| 876 | csicr1 |= CSICR1_SWAP16_EN; |
| 877 | if (pcdev->platform_flags & MX2_CAMERA_EXT_VSYNC) |
| 878 | csicr1 |= CSICR1_EXT_VSYNC; |
| 879 | if (pcdev->platform_flags & MX2_CAMERA_CCIR) |
| 880 | csicr1 |= CSICR1_CCIR_EN; |
| 881 | if (pcdev->platform_flags & MX2_CAMERA_CCIR_INTERLACE) |
| 882 | csicr1 |= CSICR1_CCIR_MODE; |
| 883 | if (pcdev->platform_flags & MX2_CAMERA_GATED_CLOCK) |
| 884 | csicr1 |= CSICR1_GCLK_MODE; |
| 885 | if (pcdev->platform_flags & MX2_CAMERA_INV_DATA) |
| 886 | csicr1 |= CSICR1_INV_DATA; |
| 887 | if (pcdev->platform_flags & MX2_CAMERA_PACK_DIR_MSB) |
| 888 | csicr1 |= CSICR1_PACK_DIR; |
| 889 | |
| 890 | pcdev->csicr1 = csicr1; |
| 891 | |
| 892 | bytesperline = soc_mbus_bytes_per_line(icd->user_width, |
| 893 | icd->current_fmt->host_fmt); |
| 894 | if (bytesperline < 0) |
| 895 | return bytesperline; |
| 896 | |
Sascha Hauer | 2c9ba37 | 2012-02-20 07:55:24 -0300 | [diff] [blame] | 897 | if (cpu_is_mx27()) { |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 898 | ret = mx27_camera_emma_prp_reset(pcdev); |
| 899 | if (ret) |
| 900 | return ret; |
| 901 | |
| 902 | if (pcdev->discard_buffer) |
| 903 | dma_free_coherent(ici->v4l2_dev.dev, |
| 904 | pcdev->discard_size, pcdev->discard_buffer, |
| 905 | pcdev->discard_buffer_dma); |
| 906 | |
| 907 | /* |
| 908 | * I didn't manage to properly enable/disable the prp |
| 909 | * on a per frame basis during running transfers, |
| 910 | * thus we allocate a buffer here and use it to |
| 911 | * discard frames when no buffer is available. |
| 912 | * Feel free to work on this ;) |
| 913 | */ |
| 914 | pcdev->discard_size = icd->user_height * bytesperline; |
| 915 | pcdev->discard_buffer = dma_alloc_coherent(ici->v4l2_dev.dev, |
| 916 | pcdev->discard_size, &pcdev->discard_buffer_dma, |
| 917 | GFP_KERNEL); |
| 918 | if (!pcdev->discard_buffer) |
| 919 | return -ENOMEM; |
| 920 | |
| 921 | mx27_camera_emma_buf_init(icd, bytesperline); |
| 922 | } else if (cpu_is_mx25()) { |
| 923 | writel((bytesperline * icd->user_height) >> 2, |
| 924 | pcdev->base_csi + CSIRXCNT); |
| 925 | writel((bytesperline << 16) | icd->user_height, |
| 926 | pcdev->base_csi + CSIIMAG_PARA); |
| 927 | } |
| 928 | |
| 929 | writel(pcdev->csicr1, pcdev->base_csi + CSICR1); |
| 930 | |
| 931 | return 0; |
| 932 | } |
| 933 | |
| 934 | static int mx2_camera_set_crop(struct soc_camera_device *icd, |
| 935 | struct v4l2_crop *a) |
| 936 | { |
| 937 | struct v4l2_rect *rect = &a->c; |
| 938 | struct v4l2_subdev *sd = soc_camera_to_subdev(icd); |
| 939 | struct v4l2_mbus_framefmt mf; |
| 940 | int ret; |
| 941 | |
| 942 | soc_camera_limit_side(&rect->left, &rect->width, 0, 2, 4096); |
| 943 | soc_camera_limit_side(&rect->top, &rect->height, 0, 2, 4096); |
| 944 | |
| 945 | ret = v4l2_subdev_call(sd, video, s_crop, a); |
| 946 | if (ret < 0) |
| 947 | return ret; |
| 948 | |
| 949 | /* The capture device might have changed its output */ |
| 950 | ret = v4l2_subdev_call(sd, video, g_mbus_fmt, &mf); |
| 951 | if (ret < 0) |
| 952 | return ret; |
| 953 | |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 954 | dev_dbg(icd->parent, "Sensor cropped %dx%d\n", |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 955 | mf.width, mf.height); |
| 956 | |
| 957 | icd->user_width = mf.width; |
| 958 | icd->user_height = mf.height; |
| 959 | |
| 960 | return ret; |
| 961 | } |
| 962 | |
Javier Martin | f410991 | 2011-12-14 13:30:14 -0300 | [diff] [blame] | 963 | static int mx2_camera_get_formats(struct soc_camera_device *icd, |
| 964 | unsigned int idx, |
| 965 | struct soc_camera_format_xlate *xlate) |
| 966 | { |
| 967 | struct v4l2_subdev *sd = soc_camera_to_subdev(icd); |
| 968 | const struct soc_mbus_pixelfmt *fmt; |
| 969 | struct device *dev = icd->parent; |
| 970 | enum v4l2_mbus_pixelcode code; |
| 971 | int ret, formats = 0; |
| 972 | |
| 973 | ret = v4l2_subdev_call(sd, video, enum_mbus_fmt, idx, &code); |
| 974 | if (ret < 0) |
| 975 | /* no more formats */ |
| 976 | return 0; |
| 977 | |
| 978 | fmt = soc_mbus_get_fmtdesc(code); |
| 979 | if (!fmt) { |
| 980 | dev_err(dev, "Invalid format code #%u: %d\n", idx, code); |
| 981 | return 0; |
| 982 | } |
| 983 | |
| 984 | if (code == V4L2_MBUS_FMT_YUYV8_2X8) { |
| 985 | formats++; |
| 986 | if (xlate) { |
| 987 | /* |
| 988 | * CH2 can output YUV420 which is a standard format in |
| 989 | * soc_mediabus.c |
| 990 | */ |
| 991 | xlate->host_fmt = |
| 992 | soc_mbus_get_fmtdesc(V4L2_MBUS_FMT_YUYV8_1_5X8); |
| 993 | xlate->code = code; |
| 994 | dev_dbg(dev, "Providing host format %s for sensor code %d\n", |
| 995 | xlate->host_fmt->name, code); |
| 996 | xlate++; |
| 997 | } |
| 998 | } |
| 999 | |
| 1000 | /* Generic pass-trough */ |
| 1001 | formats++; |
| 1002 | if (xlate) { |
| 1003 | xlate->host_fmt = fmt; |
| 1004 | xlate->code = code; |
| 1005 | xlate++; |
| 1006 | } |
| 1007 | return formats; |
| 1008 | } |
| 1009 | |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 1010 | static int mx2_camera_set_fmt(struct soc_camera_device *icd, |
| 1011 | struct v4l2_format *f) |
| 1012 | { |
Javier Martin | f410991 | 2011-12-14 13:30:14 -0300 | [diff] [blame] | 1013 | struct soc_camera_host *ici = to_soc_camera_host(icd->parent); |
| 1014 | struct mx2_camera_dev *pcdev = ici->priv; |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 1015 | struct v4l2_subdev *sd = soc_camera_to_subdev(icd); |
| 1016 | const struct soc_camera_format_xlate *xlate; |
| 1017 | struct v4l2_pix_format *pix = &f->fmt.pix; |
| 1018 | struct v4l2_mbus_framefmt mf; |
| 1019 | int ret; |
| 1020 | |
| 1021 | xlate = soc_camera_xlate_by_fourcc(icd, pix->pixelformat); |
| 1022 | if (!xlate) { |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 1023 | dev_warn(icd->parent, "Format %x not found\n", |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 1024 | pix->pixelformat); |
| 1025 | return -EINVAL; |
| 1026 | } |
| 1027 | |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 1028 | mf.width = pix->width; |
| 1029 | mf.height = pix->height; |
| 1030 | mf.field = pix->field; |
| 1031 | mf.colorspace = pix->colorspace; |
| 1032 | mf.code = xlate->code; |
| 1033 | |
| 1034 | ret = v4l2_subdev_call(sd, video, s_mbus_fmt, &mf); |
| 1035 | if (ret < 0 && ret != -ENOIOCTLCMD) |
| 1036 | return ret; |
| 1037 | |
| 1038 | if (mf.code != xlate->code) |
| 1039 | return -EINVAL; |
| 1040 | |
| 1041 | pix->width = mf.width; |
| 1042 | pix->height = mf.height; |
| 1043 | pix->field = mf.field; |
| 1044 | pix->colorspace = mf.colorspace; |
| 1045 | icd->current_fmt = xlate; |
| 1046 | |
Sascha Hauer | 2c9ba37 | 2012-02-20 07:55:24 -0300 | [diff] [blame] | 1047 | if (cpu_is_mx27()) |
Javier Martin | f410991 | 2011-12-14 13:30:14 -0300 | [diff] [blame] | 1048 | pcdev->emma_prp = mx27_emma_prp_get_format(xlate->code, |
| 1049 | xlate->host_fmt->fourcc); |
| 1050 | |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 1051 | return 0; |
| 1052 | } |
| 1053 | |
| 1054 | static int mx2_camera_try_fmt(struct soc_camera_device *icd, |
| 1055 | struct v4l2_format *f) |
| 1056 | { |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 1057 | struct v4l2_subdev *sd = soc_camera_to_subdev(icd); |
| 1058 | const struct soc_camera_format_xlate *xlate; |
| 1059 | struct v4l2_pix_format *pix = &f->fmt.pix; |
| 1060 | struct v4l2_mbus_framefmt mf; |
| 1061 | __u32 pixfmt = pix->pixelformat; |
| 1062 | unsigned int width_limit; |
| 1063 | int ret; |
| 1064 | |
| 1065 | xlate = soc_camera_xlate_by_fourcc(icd, pixfmt); |
| 1066 | if (pixfmt && !xlate) { |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 1067 | dev_warn(icd->parent, "Format %x not found\n", pixfmt); |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 1068 | return -EINVAL; |
| 1069 | } |
| 1070 | |
| 1071 | /* FIXME: implement MX27 limits */ |
| 1072 | |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 1073 | /* limit to MX25 hardware capabilities */ |
| 1074 | if (cpu_is_mx25()) { |
| 1075 | if (xlate->host_fmt->bits_per_sample <= 8) |
| 1076 | width_limit = 0xffff * 4; |
| 1077 | else |
| 1078 | width_limit = 0xffff * 2; |
| 1079 | /* CSIIMAG_PARA limit */ |
| 1080 | if (pix->width > width_limit) |
| 1081 | pix->width = width_limit; |
| 1082 | if (pix->height > 0xffff) |
| 1083 | pix->height = 0xffff; |
| 1084 | |
| 1085 | pix->bytesperline = soc_mbus_bytes_per_line(pix->width, |
| 1086 | xlate->host_fmt); |
| 1087 | if (pix->bytesperline < 0) |
| 1088 | return pix->bytesperline; |
| 1089 | pix->sizeimage = pix->height * pix->bytesperline; |
Guennadi Liakhovetski | 28281a7 | 2011-06-04 15:06:47 -0300 | [diff] [blame] | 1090 | /* Check against the CSIRXCNT limit */ |
| 1091 | if (pix->sizeimage > 4 * 0x3ffff) { |
| 1092 | /* Adjust geometry, preserve aspect ratio */ |
| 1093 | unsigned int new_height = int_sqrt(4 * 0x3ffff * |
| 1094 | pix->height / pix->bytesperline); |
| 1095 | pix->width = new_height * pix->width / pix->height; |
| 1096 | pix->height = new_height; |
| 1097 | pix->bytesperline = soc_mbus_bytes_per_line(pix->width, |
| 1098 | xlate->host_fmt); |
| 1099 | BUG_ON(pix->bytesperline < 0); |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 1100 | } |
| 1101 | } |
| 1102 | |
| 1103 | /* limit to sensor capabilities */ |
| 1104 | mf.width = pix->width; |
| 1105 | mf.height = pix->height; |
| 1106 | mf.field = pix->field; |
| 1107 | mf.colorspace = pix->colorspace; |
| 1108 | mf.code = xlate->code; |
| 1109 | |
| 1110 | ret = v4l2_subdev_call(sd, video, try_mbus_fmt, &mf); |
| 1111 | if (ret < 0) |
| 1112 | return ret; |
| 1113 | |
| 1114 | if (mf.field == V4L2_FIELD_ANY) |
| 1115 | mf.field = V4L2_FIELD_NONE; |
Javier Martin | f410991 | 2011-12-14 13:30:14 -0300 | [diff] [blame] | 1116 | /* |
| 1117 | * Driver supports interlaced images provided they have |
| 1118 | * both fields so that they can be processed as if they |
| 1119 | * were progressive. |
| 1120 | */ |
| 1121 | if (mf.field != V4L2_FIELD_NONE && !V4L2_FIELD_HAS_BOTH(mf.field)) { |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 1122 | dev_err(icd->parent, "Field type %d unsupported.\n", |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 1123 | mf.field); |
| 1124 | return -EINVAL; |
| 1125 | } |
| 1126 | |
| 1127 | pix->width = mf.width; |
| 1128 | pix->height = mf.height; |
| 1129 | pix->field = mf.field; |
| 1130 | pix->colorspace = mf.colorspace; |
| 1131 | |
| 1132 | return 0; |
| 1133 | } |
| 1134 | |
| 1135 | static int mx2_camera_querycap(struct soc_camera_host *ici, |
| 1136 | struct v4l2_capability *cap) |
| 1137 | { |
| 1138 | /* cap->name is set by the friendly caller:-> */ |
| 1139 | strlcpy(cap->card, MX2_CAM_DRIVER_DESCRIPTION, sizeof(cap->card)); |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 1140 | cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING; |
| 1141 | |
| 1142 | return 0; |
| 1143 | } |
| 1144 | |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 1145 | static unsigned int mx2_camera_poll(struct file *file, poll_table *pt) |
| 1146 | { |
Sascha Hauer | 6b10192 | 2010-11-08 17:52:45 -0300 | [diff] [blame] | 1147 | struct soc_camera_device *icd = file->private_data; |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 1148 | |
Javier Martin | c6a41e3 | 2012-01-30 09:14:10 -0300 | [diff] [blame] | 1149 | return vb2_poll(&icd->vb2_vidq, file, pt); |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 1150 | } |
| 1151 | |
| 1152 | static struct soc_camera_host_ops mx2_soc_camera_host_ops = { |
| 1153 | .owner = THIS_MODULE, |
| 1154 | .add = mx2_camera_add_device, |
| 1155 | .remove = mx2_camera_remove_device, |
| 1156 | .set_fmt = mx2_camera_set_fmt, |
| 1157 | .set_crop = mx2_camera_set_crop, |
Javier Martin | f410991 | 2011-12-14 13:30:14 -0300 | [diff] [blame] | 1158 | .get_formats = mx2_camera_get_formats, |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 1159 | .try_fmt = mx2_camera_try_fmt, |
Javier Martin | c6a41e3 | 2012-01-30 09:14:10 -0300 | [diff] [blame] | 1160 | .init_videobuf2 = mx2_camera_init_videobuf, |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 1161 | .poll = mx2_camera_poll, |
| 1162 | .querycap = mx2_camera_querycap, |
| 1163 | .set_bus_param = mx2_camera_set_bus_param, |
| 1164 | }; |
| 1165 | |
| 1166 | static void mx27_camera_frame_done_emma(struct mx2_camera_dev *pcdev, |
Javier Martin | c6a41e3 | 2012-01-30 09:14:10 -0300 | [diff] [blame] | 1167 | int bufnum) |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 1168 | { |
Javier Martin | f410991 | 2011-12-14 13:30:14 -0300 | [diff] [blame] | 1169 | u32 imgsize = pcdev->icd->user_height * pcdev->icd->user_width; |
| 1170 | struct mx2_fmt_cfg *prp = pcdev->emma_prp; |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 1171 | struct mx2_buffer *buf; |
Javier Martin | c6a41e3 | 2012-01-30 09:14:10 -0300 | [diff] [blame] | 1172 | struct vb2_buffer *vb; |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 1173 | unsigned long phys; |
| 1174 | |
| 1175 | if (!list_empty(&pcdev->active_bufs)) { |
| 1176 | buf = list_entry(pcdev->active_bufs.next, |
Javier Martin | c6a41e3 | 2012-01-30 09:14:10 -0300 | [diff] [blame] | 1177 | struct mx2_buffer, queue); |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 1178 | |
| 1179 | BUG_ON(buf->bufnum != bufnum); |
| 1180 | |
| 1181 | vb = &buf->vb; |
| 1182 | #ifdef DEBUG |
Javier Martin | c6a41e3 | 2012-01-30 09:14:10 -0300 | [diff] [blame] | 1183 | phys = vb2_dma_contig_plane_dma_addr(vb, 0); |
Javier Martin | f410991 | 2011-12-14 13:30:14 -0300 | [diff] [blame] | 1184 | if (prp->cfg.channel == 1) { |
| 1185 | if (readl(pcdev->base_emma + PRP_DEST_RGB1_PTR + |
| 1186 | 4 * bufnum) != phys) { |
| 1187 | dev_err(pcdev->dev, "%p != %p\n", phys, |
| 1188 | readl(pcdev->base_emma + |
| 1189 | PRP_DEST_RGB1_PTR + |
| 1190 | 4 * bufnum)); |
| 1191 | } |
| 1192 | } else { |
| 1193 | if (readl(pcdev->base_emma + PRP_DEST_Y_PTR - |
| 1194 | 0x14 * bufnum) != phys) { |
| 1195 | dev_err(pcdev->dev, "%p != %p\n", phys, |
| 1196 | readl(pcdev->base_emma + |
| 1197 | PRP_DEST_Y_PTR - |
| 1198 | 0x14 * bufnum)); |
| 1199 | } |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 1200 | } |
| 1201 | #endif |
Javier Martin | c6a41e3 | 2012-01-30 09:14:10 -0300 | [diff] [blame] | 1202 | dev_dbg(pcdev->dev, "%s (vb=0x%p) 0x%p %lu\n", __func__, vb, |
| 1203 | vb2_plane_vaddr(vb, 0), |
| 1204 | vb2_get_plane_payload(vb, 0)); |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 1205 | |
Javier Martin | c6a41e3 | 2012-01-30 09:14:10 -0300 | [diff] [blame] | 1206 | list_del_init(&buf->queue); |
| 1207 | do_gettimeofday(&vb->v4l2_buf.timestamp); |
Javier Martin | c6a41e3 | 2012-01-30 09:14:10 -0300 | [diff] [blame] | 1208 | vb->v4l2_buf.sequence = pcdev->frame_count; |
| 1209 | vb2_buffer_done(vb, VB2_BUF_STATE_DONE); |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 1210 | } |
| 1211 | |
Javier Martin | fb51cbd | 2012-01-30 09:14:11 -0300 | [diff] [blame^] | 1212 | pcdev->frame_count++; |
| 1213 | |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 1214 | if (list_empty(&pcdev->capture)) { |
Javier Martin | f410991 | 2011-12-14 13:30:14 -0300 | [diff] [blame] | 1215 | if (prp->cfg.channel == 1) { |
| 1216 | writel(pcdev->discard_buffer_dma, pcdev->base_emma + |
| 1217 | PRP_DEST_RGB1_PTR + 4 * bufnum); |
| 1218 | } else { |
| 1219 | writel(pcdev->discard_buffer_dma, pcdev->base_emma + |
| 1220 | PRP_DEST_Y_PTR - |
| 1221 | 0x14 * bufnum); |
| 1222 | if (prp->out_fmt == V4L2_PIX_FMT_YUV420) { |
| 1223 | writel(pcdev->discard_buffer_dma + imgsize, |
| 1224 | pcdev->base_emma + PRP_DEST_CB_PTR - |
| 1225 | 0x14 * bufnum); |
| 1226 | writel(pcdev->discard_buffer_dma + |
| 1227 | ((5 * imgsize) / 4), pcdev->base_emma + |
| 1228 | PRP_DEST_CR_PTR - 0x14 * bufnum); |
| 1229 | } |
| 1230 | } |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 1231 | return; |
| 1232 | } |
| 1233 | |
| 1234 | buf = list_entry(pcdev->capture.next, |
Javier Martin | c6a41e3 | 2012-01-30 09:14:10 -0300 | [diff] [blame] | 1235 | struct mx2_buffer, queue); |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 1236 | |
Michael Grzeschik | cd9ebdb | 2010-08-03 06:37:54 -0300 | [diff] [blame] | 1237 | buf->bufnum = !bufnum; |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 1238 | |
| 1239 | list_move_tail(pcdev->capture.next, &pcdev->active_bufs); |
| 1240 | |
| 1241 | vb = &buf->vb; |
Javier Martin | c6a41e3 | 2012-01-30 09:14:10 -0300 | [diff] [blame] | 1242 | buf->state = MX2_STATE_ACTIVE; |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 1243 | |
Javier Martin | c6a41e3 | 2012-01-30 09:14:10 -0300 | [diff] [blame] | 1244 | phys = vb2_dma_contig_plane_dma_addr(vb, 0); |
Javier Martin | f410991 | 2011-12-14 13:30:14 -0300 | [diff] [blame] | 1245 | if (prp->cfg.channel == 1) { |
| 1246 | writel(phys, pcdev->base_emma + PRP_DEST_RGB1_PTR + 4 * bufnum); |
| 1247 | } else { |
| 1248 | writel(phys, pcdev->base_emma + |
| 1249 | PRP_DEST_Y_PTR - 0x14 * bufnum); |
| 1250 | if (prp->cfg.out_fmt == PRP_CNTL_CH2_OUT_YUV420) { |
| 1251 | writel(phys + imgsize, pcdev->base_emma + |
| 1252 | PRP_DEST_CB_PTR - 0x14 * bufnum); |
| 1253 | writel(phys + ((5 * imgsize) / 4), pcdev->base_emma + |
| 1254 | PRP_DEST_CR_PTR - 0x14 * bufnum); |
| 1255 | } |
| 1256 | } |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 1257 | } |
| 1258 | |
| 1259 | static irqreturn_t mx27_camera_emma_irq(int irq_emma, void *data) |
| 1260 | { |
| 1261 | struct mx2_camera_dev *pcdev = data; |
| 1262 | unsigned int status = readl(pcdev->base_emma + PRP_INTRSTATUS); |
| 1263 | struct mx2_buffer *buf; |
| 1264 | |
| 1265 | if (status & (1 << 7)) { /* overflow */ |
| 1266 | u32 cntl; |
| 1267 | /* |
| 1268 | * We only disable channel 1 here since this is the only |
| 1269 | * enabled channel |
| 1270 | * |
| 1271 | * FIXME: the correct DMA overflow handling should be resetting |
| 1272 | * the buffer, returning an error frame, and continuing with |
| 1273 | * the next one. |
| 1274 | */ |
| 1275 | cntl = readl(pcdev->base_emma + PRP_CNTL); |
Javier Martin | f410991 | 2011-12-14 13:30:14 -0300 | [diff] [blame] | 1276 | writel(cntl & ~(PRP_CNTL_CH1EN | PRP_CNTL_CH2EN), |
| 1277 | pcdev->base_emma + PRP_CNTL); |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 1278 | writel(cntl, pcdev->base_emma + PRP_CNTL); |
| 1279 | } |
Javier Martin | f410991 | 2011-12-14 13:30:14 -0300 | [diff] [blame] | 1280 | if ((((status & (3 << 5)) == (3 << 5)) || |
| 1281 | ((status & (3 << 3)) == (3 << 3))) |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 1282 | && !list_empty(&pcdev->active_bufs)) { |
| 1283 | /* |
| 1284 | * Both buffers have triggered, process the one we're expecting |
| 1285 | * to first |
| 1286 | */ |
| 1287 | buf = list_entry(pcdev->active_bufs.next, |
Javier Martin | c6a41e3 | 2012-01-30 09:14:10 -0300 | [diff] [blame] | 1288 | struct mx2_buffer, queue); |
| 1289 | mx27_camera_frame_done_emma(pcdev, buf->bufnum); |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 1290 | status &= ~(1 << (6 - buf->bufnum)); /* mark processed */ |
| 1291 | } |
Javier Martin | f410991 | 2011-12-14 13:30:14 -0300 | [diff] [blame] | 1292 | if ((status & (1 << 6)) || (status & (1 << 4))) |
Javier Martin | c6a41e3 | 2012-01-30 09:14:10 -0300 | [diff] [blame] | 1293 | mx27_camera_frame_done_emma(pcdev, 0); |
Javier Martin | f410991 | 2011-12-14 13:30:14 -0300 | [diff] [blame] | 1294 | if ((status & (1 << 5)) || (status & (1 << 3))) |
Javier Martin | c6a41e3 | 2012-01-30 09:14:10 -0300 | [diff] [blame] | 1295 | mx27_camera_frame_done_emma(pcdev, 1); |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 1296 | |
| 1297 | writel(status, pcdev->base_emma + PRP_INTRSTATUS); |
| 1298 | |
| 1299 | return IRQ_HANDLED; |
| 1300 | } |
| 1301 | |
| 1302 | static int __devinit mx27_camera_emma_init(struct mx2_camera_dev *pcdev) |
| 1303 | { |
| 1304 | struct resource *res_emma = pcdev->res_emma; |
| 1305 | int err = 0; |
| 1306 | |
| 1307 | if (!request_mem_region(res_emma->start, resource_size(res_emma), |
| 1308 | MX2_CAM_DRV_NAME)) { |
| 1309 | err = -EBUSY; |
| 1310 | goto out; |
| 1311 | } |
| 1312 | |
| 1313 | pcdev->base_emma = ioremap(res_emma->start, resource_size(res_emma)); |
| 1314 | if (!pcdev->base_emma) { |
| 1315 | err = -ENOMEM; |
| 1316 | goto exit_release; |
| 1317 | } |
| 1318 | |
| 1319 | err = request_irq(pcdev->irq_emma, mx27_camera_emma_irq, 0, |
| 1320 | MX2_CAM_DRV_NAME, pcdev); |
| 1321 | if (err) { |
| 1322 | dev_err(pcdev->dev, "Camera EMMA interrupt register failed \n"); |
| 1323 | goto exit_iounmap; |
| 1324 | } |
| 1325 | |
| 1326 | pcdev->clk_emma = clk_get(NULL, "emma"); |
| 1327 | if (IS_ERR(pcdev->clk_emma)) { |
| 1328 | err = PTR_ERR(pcdev->clk_emma); |
| 1329 | goto exit_free_irq; |
| 1330 | } |
| 1331 | |
| 1332 | clk_enable(pcdev->clk_emma); |
| 1333 | |
| 1334 | err = mx27_camera_emma_prp_reset(pcdev); |
| 1335 | if (err) |
| 1336 | goto exit_clk_emma_put; |
| 1337 | |
| 1338 | return err; |
| 1339 | |
| 1340 | exit_clk_emma_put: |
| 1341 | clk_disable(pcdev->clk_emma); |
| 1342 | clk_put(pcdev->clk_emma); |
| 1343 | exit_free_irq: |
| 1344 | free_irq(pcdev->irq_emma, pcdev); |
| 1345 | exit_iounmap: |
| 1346 | iounmap(pcdev->base_emma); |
| 1347 | exit_release: |
| 1348 | release_mem_region(res_emma->start, resource_size(res_emma)); |
| 1349 | out: |
| 1350 | return err; |
| 1351 | } |
| 1352 | |
| 1353 | static int __devinit mx2_camera_probe(struct platform_device *pdev) |
| 1354 | { |
| 1355 | struct mx2_camera_dev *pcdev; |
| 1356 | struct resource *res_csi, *res_emma; |
| 1357 | void __iomem *base_csi; |
| 1358 | int irq_csi, irq_emma; |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 1359 | int err = 0; |
| 1360 | |
| 1361 | dev_dbg(&pdev->dev, "initialising\n"); |
| 1362 | |
| 1363 | res_csi = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 1364 | irq_csi = platform_get_irq(pdev, 0); |
| 1365 | if (res_csi == NULL || irq_csi < 0) { |
| 1366 | dev_err(&pdev->dev, "Missing platform resources data\n"); |
| 1367 | err = -ENODEV; |
| 1368 | goto exit; |
| 1369 | } |
| 1370 | |
| 1371 | pcdev = kzalloc(sizeof(*pcdev), GFP_KERNEL); |
| 1372 | if (!pcdev) { |
| 1373 | dev_err(&pdev->dev, "Could not allocate pcdev\n"); |
| 1374 | err = -ENOMEM; |
| 1375 | goto exit; |
| 1376 | } |
| 1377 | |
| 1378 | pcdev->clk_csi = clk_get(&pdev->dev, NULL); |
| 1379 | if (IS_ERR(pcdev->clk_csi)) { |
| 1380 | err = PTR_ERR(pcdev->clk_csi); |
| 1381 | goto exit_kfree; |
| 1382 | } |
| 1383 | |
| 1384 | dev_dbg(&pdev->dev, "Camera clock frequency: %ld\n", |
| 1385 | clk_get_rate(pcdev->clk_csi)); |
| 1386 | |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 1387 | pcdev->res_csi = res_csi; |
| 1388 | pcdev->pdata = pdev->dev.platform_data; |
| 1389 | if (pcdev->pdata) { |
| 1390 | long rate; |
| 1391 | |
| 1392 | pcdev->platform_flags = pcdev->pdata->flags; |
| 1393 | |
| 1394 | rate = clk_round_rate(pcdev->clk_csi, pcdev->pdata->clk * 2); |
| 1395 | if (rate <= 0) { |
| 1396 | err = -ENODEV; |
| 1397 | goto exit_dma_free; |
| 1398 | } |
| 1399 | err = clk_set_rate(pcdev->clk_csi, rate); |
| 1400 | if (err < 0) |
| 1401 | goto exit_dma_free; |
| 1402 | } |
| 1403 | |
| 1404 | INIT_LIST_HEAD(&pcdev->capture); |
| 1405 | INIT_LIST_HEAD(&pcdev->active_bufs); |
| 1406 | spin_lock_init(&pcdev->lock); |
| 1407 | |
| 1408 | /* |
| 1409 | * Request the regions. |
| 1410 | */ |
| 1411 | if (!request_mem_region(res_csi->start, resource_size(res_csi), |
| 1412 | MX2_CAM_DRV_NAME)) { |
| 1413 | err = -EBUSY; |
| 1414 | goto exit_dma_free; |
| 1415 | } |
| 1416 | |
| 1417 | base_csi = ioremap(res_csi->start, resource_size(res_csi)); |
| 1418 | if (!base_csi) { |
| 1419 | err = -ENOMEM; |
| 1420 | goto exit_release; |
| 1421 | } |
| 1422 | pcdev->irq_csi = irq_csi; |
| 1423 | pcdev->base_csi = base_csi; |
| 1424 | pcdev->base_dma = res_csi->start; |
| 1425 | pcdev->dev = &pdev->dev; |
| 1426 | |
Sascha Hauer | 2c9ba37 | 2012-02-20 07:55:24 -0300 | [diff] [blame] | 1427 | if (cpu_is_mx25()) { |
| 1428 | err = request_irq(pcdev->irq_csi, mx25_camera_irq, 0, |
| 1429 | MX2_CAM_DRV_NAME, pcdev); |
| 1430 | if (err) { |
| 1431 | dev_err(pcdev->dev, "Camera interrupt register failed \n"); |
| 1432 | goto exit_iounmap; |
| 1433 | } |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 1434 | } |
| 1435 | |
| 1436 | if (cpu_is_mx27()) { |
| 1437 | /* EMMA support */ |
| 1438 | res_emma = platform_get_resource(pdev, IORESOURCE_MEM, 1); |
| 1439 | irq_emma = platform_get_irq(pdev, 1); |
| 1440 | |
Sascha Hauer | 2c9ba37 | 2012-02-20 07:55:24 -0300 | [diff] [blame] | 1441 | if (!res_emma || !irq_emma) { |
| 1442 | dev_err(&pdev->dev, "no EMMA resources\n"); |
| 1443 | goto exit_free_irq; |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 1444 | } |
Sascha Hauer | 2c9ba37 | 2012-02-20 07:55:24 -0300 | [diff] [blame] | 1445 | |
| 1446 | pcdev->res_emma = res_emma; |
| 1447 | pcdev->irq_emma = irq_emma; |
| 1448 | if (mx27_camera_emma_init(pcdev)) |
| 1449 | goto exit_free_irq; |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 1450 | } |
| 1451 | |
| 1452 | pcdev->soc_host.drv_name = MX2_CAM_DRV_NAME, |
| 1453 | pcdev->soc_host.ops = &mx2_soc_camera_host_ops, |
| 1454 | pcdev->soc_host.priv = pcdev; |
| 1455 | pcdev->soc_host.v4l2_dev.dev = &pdev->dev; |
| 1456 | pcdev->soc_host.nr = pdev->id; |
Javier Martin | c6a41e3 | 2012-01-30 09:14:10 -0300 | [diff] [blame] | 1457 | |
| 1458 | pcdev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev); |
| 1459 | if (IS_ERR(pcdev->alloc_ctx)) { |
| 1460 | err = PTR_ERR(pcdev->alloc_ctx); |
| 1461 | goto eallocctx; |
| 1462 | } |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 1463 | err = soc_camera_host_register(&pcdev->soc_host); |
| 1464 | if (err) |
| 1465 | goto exit_free_emma; |
| 1466 | |
Michael Grzeschik | 45f4d4e | 2010-08-27 09:39:05 -0300 | [diff] [blame] | 1467 | dev_info(&pdev->dev, "MX2 Camera (CSI) driver probed, clock frequency: %ld\n", |
| 1468 | clk_get_rate(pcdev->clk_csi)); |
| 1469 | |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 1470 | return 0; |
| 1471 | |
| 1472 | exit_free_emma: |
Javier Martin | c6a41e3 | 2012-01-30 09:14:10 -0300 | [diff] [blame] | 1473 | vb2_dma_contig_cleanup_ctx(pcdev->alloc_ctx); |
| 1474 | eallocctx: |
Sascha Hauer | 2c9ba37 | 2012-02-20 07:55:24 -0300 | [diff] [blame] | 1475 | if (cpu_is_mx27()) { |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 1476 | free_irq(pcdev->irq_emma, pcdev); |
| 1477 | clk_disable(pcdev->clk_emma); |
| 1478 | clk_put(pcdev->clk_emma); |
| 1479 | iounmap(pcdev->base_emma); |
Sascha Hauer | 2c9ba37 | 2012-02-20 07:55:24 -0300 | [diff] [blame] | 1480 | release_mem_region(pcdev->res_emma->start, resource_size(pcdev->res_emma)); |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 1481 | } |
| 1482 | exit_free_irq: |
Sascha Hauer | 2c9ba37 | 2012-02-20 07:55:24 -0300 | [diff] [blame] | 1483 | if (cpu_is_mx25()) |
| 1484 | free_irq(pcdev->irq_csi, pcdev); |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 1485 | exit_iounmap: |
| 1486 | iounmap(base_csi); |
| 1487 | exit_release: |
| 1488 | release_mem_region(res_csi->start, resource_size(res_csi)); |
| 1489 | exit_dma_free: |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 1490 | clk_put(pcdev->clk_csi); |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 1491 | exit_kfree: |
| 1492 | kfree(pcdev); |
| 1493 | exit: |
| 1494 | return err; |
| 1495 | } |
| 1496 | |
| 1497 | static int __devexit mx2_camera_remove(struct platform_device *pdev) |
| 1498 | { |
| 1499 | struct soc_camera_host *soc_host = to_soc_camera_host(&pdev->dev); |
| 1500 | struct mx2_camera_dev *pcdev = container_of(soc_host, |
| 1501 | struct mx2_camera_dev, soc_host); |
| 1502 | struct resource *res; |
| 1503 | |
| 1504 | clk_put(pcdev->clk_csi); |
Sascha Hauer | 2c9ba37 | 2012-02-20 07:55:24 -0300 | [diff] [blame] | 1505 | if (cpu_is_mx25()) |
| 1506 | free_irq(pcdev->irq_csi, pcdev); |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 1507 | if (cpu_is_mx27()) |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 1508 | free_irq(pcdev->irq_emma, pcdev); |
| 1509 | |
| 1510 | soc_camera_host_unregister(&pcdev->soc_host); |
| 1511 | |
Javier Martin | c6a41e3 | 2012-01-30 09:14:10 -0300 | [diff] [blame] | 1512 | vb2_dma_contig_cleanup_ctx(pcdev->alloc_ctx); |
| 1513 | |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 1514 | iounmap(pcdev->base_csi); |
| 1515 | |
Sascha Hauer | 2c9ba37 | 2012-02-20 07:55:24 -0300 | [diff] [blame] | 1516 | if (cpu_is_mx27()) { |
Baruch Siach | 2066930 | 2010-07-04 07:55:10 +0300 | [diff] [blame] | 1517 | clk_disable(pcdev->clk_emma); |
| 1518 | clk_put(pcdev->clk_emma); |
| 1519 | iounmap(pcdev->base_emma); |
| 1520 | res = pcdev->res_emma; |
| 1521 | release_mem_region(res->start, resource_size(res)); |
| 1522 | } |
| 1523 | |
| 1524 | res = pcdev->res_csi; |
| 1525 | release_mem_region(res->start, resource_size(res)); |
| 1526 | |
| 1527 | kfree(pcdev); |
| 1528 | |
| 1529 | dev_info(&pdev->dev, "MX2 Camera driver unloaded\n"); |
| 1530 | |
| 1531 | return 0; |
| 1532 | } |
| 1533 | |
| 1534 | static struct platform_driver mx2_camera_driver = { |
| 1535 | .driver = { |
| 1536 | .name = MX2_CAM_DRV_NAME, |
| 1537 | }, |
| 1538 | .remove = __devexit_p(mx2_camera_remove), |
| 1539 | }; |
| 1540 | |
| 1541 | |
| 1542 | static int __init mx2_camera_init(void) |
| 1543 | { |
| 1544 | return platform_driver_probe(&mx2_camera_driver, &mx2_camera_probe); |
| 1545 | } |
| 1546 | |
| 1547 | static void __exit mx2_camera_exit(void) |
| 1548 | { |
| 1549 | return platform_driver_unregister(&mx2_camera_driver); |
| 1550 | } |
| 1551 | |
| 1552 | module_init(mx2_camera_init); |
| 1553 | module_exit(mx2_camera_exit); |
| 1554 | |
| 1555 | MODULE_DESCRIPTION("i.MX27/i.MX25 SoC Camera Host driver"); |
| 1556 | MODULE_AUTHOR("Sascha Hauer <sha@pengutronix.de>"); |
| 1557 | MODULE_LICENSE("GPL"); |
Mauro Carvalho Chehab | 64dc3c1 | 2011-06-25 11:28:37 -0300 | [diff] [blame] | 1558 | MODULE_VERSION(MX2_CAM_VERSION); |