Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 1 | /* |
| 2 | * The Marvell camera core. This device appears in a number of settings, |
| 3 | * so it needs platform-specific support outside of the core. |
| 4 | * |
| 5 | * Copyright 2011 Jonathan Corbet corbet@lwn.net |
| 6 | */ |
| 7 | #include <linux/kernel.h> |
| 8 | #include <linux/module.h> |
| 9 | #include <linux/fs.h> |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 10 | #include <linux/mm.h> |
| 11 | #include <linux/i2c.h> |
| 12 | #include <linux/interrupt.h> |
| 13 | #include <linux/spinlock.h> |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 14 | #include <linux/slab.h> |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 15 | #include <linux/device.h> |
| 16 | #include <linux/wait.h> |
| 17 | #include <linux/list.h> |
| 18 | #include <linux/dma-mapping.h> |
| 19 | #include <linux/delay.h> |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 20 | #include <linux/vmalloc.h> |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 21 | #include <linux/io.h> |
Jonathan Corbet | 362d45b | 2011-06-20 16:14:37 -0300 | [diff] [blame] | 22 | #include <linux/videodev2.h> |
| 23 | #include <media/v4l2-device.h> |
| 24 | #include <media/v4l2-ioctl.h> |
| 25 | #include <media/v4l2-chip-ident.h> |
| 26 | #include <media/ov7670.h> |
| 27 | #include <media/videobuf2-vmalloc.h> |
Jonathan Corbet | a9b36e8 | 2011-06-20 16:14:40 -0300 | [diff] [blame] | 28 | #include <media/videobuf2-dma-contig.h> |
Jonathan Corbet | cbc4f3a | 2011-06-30 17:05:27 -0300 | [diff] [blame^] | 29 | #include <media/videobuf2-dma-sg.h> |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 30 | |
| 31 | #include "mcam-core.h" |
| 32 | |
Jonathan Corbet | a9b36e8 | 2011-06-20 16:14:40 -0300 | [diff] [blame] | 33 | /* |
| 34 | * Basic frame stats - to be deleted shortly |
| 35 | */ |
| 36 | static int frames; |
| 37 | static int singles; |
| 38 | static int delivered; |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 39 | |
| 40 | /* |
| 41 | * Internal DMA buffer management. Since the controller cannot do S/G I/O, |
| 42 | * we must have physically contiguous buffers to bring frames into. |
| 43 | * These parameters control how many buffers we use, whether we |
| 44 | * allocate them at load time (better chance of success, but nails down |
| 45 | * memory) or when somebody tries to use the camera (riskier), and, |
| 46 | * for load-time allocation, how big they should be. |
| 47 | * |
| 48 | * The controller can cycle through three buffers. We could use |
| 49 | * more by flipping pointers around, but it probably makes little |
| 50 | * sense. |
| 51 | */ |
| 52 | |
| 53 | static int alloc_bufs_at_read; |
| 54 | module_param(alloc_bufs_at_read, bool, 0444); |
| 55 | MODULE_PARM_DESC(alloc_bufs_at_read, |
| 56 | "Non-zero value causes DMA buffers to be allocated when the " |
| 57 | "video capture device is read, rather than at module load " |
| 58 | "time. This saves memory, but decreases the chances of " |
Jonathan Corbet | a9b36e8 | 2011-06-20 16:14:40 -0300 | [diff] [blame] | 59 | "successfully getting those buffers. This parameter is " |
| 60 | "only used in the vmalloc buffer mode"); |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 61 | |
| 62 | static int n_dma_bufs = 3; |
| 63 | module_param(n_dma_bufs, uint, 0644); |
| 64 | MODULE_PARM_DESC(n_dma_bufs, |
| 65 | "The number of DMA buffers to allocate. Can be either two " |
| 66 | "(saves memory, makes timing tighter) or three."); |
| 67 | |
| 68 | static int dma_buf_size = VGA_WIDTH * VGA_HEIGHT * 2; /* Worst case */ |
| 69 | module_param(dma_buf_size, uint, 0444); |
| 70 | MODULE_PARM_DESC(dma_buf_size, |
| 71 | "The size of the allocated DMA buffers. If actual operating " |
| 72 | "parameters require larger buffers, an attempt to reallocate " |
| 73 | "will be made."); |
| 74 | |
| 75 | static int min_buffers = 1; |
| 76 | module_param(min_buffers, uint, 0644); |
| 77 | MODULE_PARM_DESC(min_buffers, |
| 78 | "The minimum number of streaming I/O buffers we are willing " |
| 79 | "to work with."); |
| 80 | |
| 81 | static int max_buffers = 10; |
| 82 | module_param(max_buffers, uint, 0644); |
| 83 | MODULE_PARM_DESC(max_buffers, |
| 84 | "The maximum number of streaming I/O buffers an application " |
| 85 | "will be allowed to allocate. These buffers are big and live " |
| 86 | "in vmalloc space."); |
| 87 | |
| 88 | static int flip; |
| 89 | module_param(flip, bool, 0444); |
| 90 | MODULE_PARM_DESC(flip, |
| 91 | "If set, the sensor will be instructed to flip the image " |
| 92 | "vertically."); |
| 93 | |
Jonathan Corbet | a9b36e8 | 2011-06-20 16:14:40 -0300 | [diff] [blame] | 94 | static int buffer_mode = -1; |
| 95 | module_param(buffer_mode, int, 0444); |
| 96 | MODULE_PARM_DESC(buffer_mode, |
| 97 | "Set the buffer mode to be used; default is to go with what " |
| 98 | "the platform driver asks for. Set to 0 for vmalloc, 1 for " |
| 99 | "DMA contiguous."); |
| 100 | |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 101 | /* |
| 102 | * Status flags. Always manipulated with bit operations. |
| 103 | */ |
| 104 | #define CF_BUF0_VALID 0 /* Buffers valid - first three */ |
| 105 | #define CF_BUF1_VALID 1 |
| 106 | #define CF_BUF2_VALID 2 |
| 107 | #define CF_DMA_ACTIVE 3 /* A frame is incoming */ |
| 108 | #define CF_CONFIG_NEEDED 4 /* Must configure hardware */ |
Jonathan Corbet | a9b36e8 | 2011-06-20 16:14:40 -0300 | [diff] [blame] | 109 | #define CF_SINGLE_BUFFER 5 /* Running with a single buffer */ |
Jonathan Corbet | cbc4f3a | 2011-06-30 17:05:27 -0300 | [diff] [blame^] | 110 | #define CF_SG_RESTART 6 /* SG restart needed */ |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 111 | |
| 112 | #define sensor_call(cam, o, f, args...) \ |
| 113 | v4l2_subdev_call(cam->sensor, o, f, ##args) |
| 114 | |
| 115 | static struct mcam_format_struct { |
| 116 | __u8 *desc; |
| 117 | __u32 pixelformat; |
| 118 | int bpp; /* Bytes per pixel */ |
| 119 | enum v4l2_mbus_pixelcode mbus_code; |
| 120 | } mcam_formats[] = { |
| 121 | { |
| 122 | .desc = "YUYV 4:2:2", |
| 123 | .pixelformat = V4L2_PIX_FMT_YUYV, |
| 124 | .mbus_code = V4L2_MBUS_FMT_YUYV8_2X8, |
| 125 | .bpp = 2, |
| 126 | }, |
| 127 | { |
| 128 | .desc = "RGB 444", |
| 129 | .pixelformat = V4L2_PIX_FMT_RGB444, |
| 130 | .mbus_code = V4L2_MBUS_FMT_RGB444_2X8_PADHI_LE, |
| 131 | .bpp = 2, |
| 132 | }, |
| 133 | { |
| 134 | .desc = "RGB 565", |
| 135 | .pixelformat = V4L2_PIX_FMT_RGB565, |
| 136 | .mbus_code = V4L2_MBUS_FMT_RGB565_2X8_LE, |
| 137 | .bpp = 2, |
| 138 | }, |
| 139 | { |
| 140 | .desc = "Raw RGB Bayer", |
| 141 | .pixelformat = V4L2_PIX_FMT_SBGGR8, |
| 142 | .mbus_code = V4L2_MBUS_FMT_SBGGR8_1X8, |
| 143 | .bpp = 1 |
| 144 | }, |
| 145 | }; |
| 146 | #define N_MCAM_FMTS ARRAY_SIZE(mcam_formats) |
| 147 | |
| 148 | static struct mcam_format_struct *mcam_find_format(u32 pixelformat) |
| 149 | { |
| 150 | unsigned i; |
| 151 | |
| 152 | for (i = 0; i < N_MCAM_FMTS; i++) |
| 153 | if (mcam_formats[i].pixelformat == pixelformat) |
| 154 | return mcam_formats + i; |
| 155 | /* Not found? Then return the first format. */ |
| 156 | return mcam_formats; |
| 157 | } |
| 158 | |
| 159 | /* |
| 160 | * Start over with DMA buffers - dev_lock needed. |
| 161 | */ |
| 162 | static void mcam_reset_buffers(struct mcam_camera *cam) |
| 163 | { |
| 164 | int i; |
| 165 | |
| 166 | cam->next_buf = -1; |
| 167 | for (i = 0; i < cam->nbufs; i++) |
| 168 | clear_bit(i, &cam->flags); |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | static inline int mcam_needs_config(struct mcam_camera *cam) |
| 172 | { |
| 173 | return test_bit(CF_CONFIG_NEEDED, &cam->flags); |
| 174 | } |
| 175 | |
| 176 | static void mcam_set_config_needed(struct mcam_camera *cam, int needed) |
| 177 | { |
| 178 | if (needed) |
| 179 | set_bit(CF_CONFIG_NEEDED, &cam->flags); |
| 180 | else |
| 181 | clear_bit(CF_CONFIG_NEEDED, &cam->flags); |
| 182 | } |
| 183 | |
Jonathan Corbet | b5210fd | 2011-06-20 16:14:36 -0300 | [diff] [blame] | 184 | /* |
Jonathan Corbet | cbc4f3a | 2011-06-30 17:05:27 -0300 | [diff] [blame^] | 185 | * The two-word DMA descriptor format used by the Armada 610 and like. There |
| 186 | * Is a three-word format as well (set C1_DESC_3WORD) where the third |
| 187 | * word is a pointer to the next descriptor, but we don't use it. Two-word |
| 188 | * descriptors have to be contiguous in memory. |
| 189 | */ |
| 190 | struct mcam_dma_desc { |
| 191 | u32 dma_addr; |
| 192 | u32 segment_len; |
| 193 | }; |
| 194 | |
| 195 | /* |
Jonathan Corbet | b5210fd | 2011-06-20 16:14:36 -0300 | [diff] [blame] | 196 | * Our buffer type for working with videobuf2. Note that the vb2 |
| 197 | * developers have decreed that struct vb2_buffer must be at the |
| 198 | * beginning of this structure. |
| 199 | */ |
| 200 | struct mcam_vb_buffer { |
| 201 | struct vb2_buffer vb_buf; |
| 202 | struct list_head queue; |
Jonathan Corbet | cbc4f3a | 2011-06-30 17:05:27 -0300 | [diff] [blame^] | 203 | struct mcam_dma_desc *dma_desc; /* Descriptor virtual address */ |
| 204 | dma_addr_t dma_desc_pa; /* Descriptor physical address */ |
| 205 | int dma_desc_nent; /* Number of mapped descriptors */ |
Jonathan Corbet | b5210fd | 2011-06-20 16:14:36 -0300 | [diff] [blame] | 206 | }; |
| 207 | |
| 208 | static inline struct mcam_vb_buffer *vb_to_mvb(struct vb2_buffer *vb) |
| 209 | { |
| 210 | return container_of(vb, struct mcam_vb_buffer, vb_buf); |
| 211 | } |
| 212 | |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 213 | |
| 214 | /* |
Jonathan Corbet | 67a8dbb | 2011-06-11 14:46:49 -0300 | [diff] [blame] | 215 | * Debugging and related. |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 216 | */ |
| 217 | #define cam_err(cam, fmt, arg...) \ |
| 218 | dev_err((cam)->dev, fmt, ##arg); |
| 219 | #define cam_warn(cam, fmt, arg...) \ |
| 220 | dev_warn((cam)->dev, fmt, ##arg); |
| 221 | #define cam_dbg(cam, fmt, arg...) \ |
| 222 | dev_dbg((cam)->dev, fmt, ##arg); |
| 223 | |
| 224 | |
| 225 | |
| 226 | /* ------------------------------------------------------------------- */ |
| 227 | /* |
| 228 | * Deal with the controller. |
| 229 | */ |
| 230 | |
| 231 | /* |
Jonathan Corbet | a9b36e8 | 2011-06-20 16:14:40 -0300 | [diff] [blame] | 232 | * Set up DMA buffers when operating in vmalloc mode |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 233 | */ |
Jonathan Corbet | a9b36e8 | 2011-06-20 16:14:40 -0300 | [diff] [blame] | 234 | static void mcam_ctlr_dma_vmalloc(struct mcam_camera *cam) |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 235 | { |
| 236 | /* |
| 237 | * Store the first two Y buffers (we aren't supporting |
| 238 | * planar formats for now, so no UV bufs). Then either |
| 239 | * set the third if it exists, or tell the controller |
| 240 | * to just use two. |
| 241 | */ |
| 242 | mcam_reg_write(cam, REG_Y0BAR, cam->dma_handles[0]); |
| 243 | mcam_reg_write(cam, REG_Y1BAR, cam->dma_handles[1]); |
| 244 | if (cam->nbufs > 2) { |
| 245 | mcam_reg_write(cam, REG_Y2BAR, cam->dma_handles[2]); |
| 246 | mcam_reg_clear_bit(cam, REG_CTRL1, C1_TWOBUFS); |
| 247 | } else |
| 248 | mcam_reg_set_bit(cam, REG_CTRL1, C1_TWOBUFS); |
Jonathan Corbet | 67a8dbb | 2011-06-11 14:46:49 -0300 | [diff] [blame] | 249 | if (cam->chip_id == V4L2_IDENT_CAFE) |
| 250 | mcam_reg_write(cam, REG_UBAR, 0); /* 32 bits only */ |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 251 | } |
| 252 | |
Jonathan Corbet | a9b36e8 | 2011-06-20 16:14:40 -0300 | [diff] [blame] | 253 | /* |
| 254 | * Set up a contiguous buffer for the given frame. Here also is where |
| 255 | * the underrun strategy is set: if there is no buffer available, reuse |
| 256 | * the buffer from the other BAR and set the CF_SINGLE_BUFFER flag to |
| 257 | * keep the interrupt handler from giving that buffer back to user |
| 258 | * space. In this way, we always have a buffer to DMA to and don't |
| 259 | * have to try to play games stopping and restarting the controller. |
| 260 | */ |
| 261 | static void mcam_set_contig_buffer(struct mcam_camera *cam, int frame) |
| 262 | { |
| 263 | struct mcam_vb_buffer *buf; |
| 264 | /* |
| 265 | * If there are no available buffers, go into single mode |
| 266 | */ |
| 267 | if (list_empty(&cam->buffers)) { |
| 268 | buf = cam->vb_bufs[frame ^ 0x1]; |
| 269 | cam->vb_bufs[frame] = buf; |
| 270 | mcam_reg_write(cam, frame == 0 ? REG_Y0BAR : REG_Y1BAR, |
| 271 | vb2_dma_contig_plane_paddr(&buf->vb_buf, 0)); |
| 272 | set_bit(CF_SINGLE_BUFFER, &cam->flags); |
| 273 | singles++; |
| 274 | return; |
| 275 | } |
| 276 | /* |
| 277 | * OK, we have a buffer we can use. |
| 278 | */ |
| 279 | buf = list_first_entry(&cam->buffers, struct mcam_vb_buffer, queue); |
| 280 | list_del_init(&buf->queue); |
| 281 | mcam_reg_write(cam, frame == 0 ? REG_Y0BAR : REG_Y1BAR, |
| 282 | vb2_dma_contig_plane_paddr(&buf->vb_buf, 0)); |
| 283 | cam->vb_bufs[frame] = buf; |
| 284 | clear_bit(CF_SINGLE_BUFFER, &cam->flags); |
| 285 | } |
| 286 | |
Jonathan Corbet | cbc4f3a | 2011-06-30 17:05:27 -0300 | [diff] [blame^] | 287 | /* |
| 288 | * Initial B_DMA_contig setup. |
| 289 | */ |
Jonathan Corbet | a9b36e8 | 2011-06-20 16:14:40 -0300 | [diff] [blame] | 290 | static void mcam_ctlr_dma_contig(struct mcam_camera *cam) |
| 291 | { |
| 292 | mcam_reg_set_bit(cam, REG_CTRL1, C1_TWOBUFS); |
| 293 | cam->nbufs = 2; |
| 294 | mcam_set_contig_buffer(cam, 0); |
| 295 | mcam_set_contig_buffer(cam, 1); |
| 296 | } |
| 297 | |
| 298 | |
Jonathan Corbet | cbc4f3a | 2011-06-30 17:05:27 -0300 | [diff] [blame^] | 299 | /* |
| 300 | * Set up the next buffer for S/G I/O; caller should be sure that |
| 301 | * the controller is stopped and a buffer is available. |
| 302 | */ |
| 303 | static void mcam_sg_next_buffer(struct mcam_camera *cam) |
Jonathan Corbet | a9b36e8 | 2011-06-20 16:14:40 -0300 | [diff] [blame] | 304 | { |
Jonathan Corbet | cbc4f3a | 2011-06-30 17:05:27 -0300 | [diff] [blame^] | 305 | struct mcam_vb_buffer *buf; |
| 306 | |
| 307 | buf = list_first_entry(&cam->buffers, struct mcam_vb_buffer, queue); |
| 308 | list_del_init(&buf->queue); |
| 309 | mcam_reg_write(cam, REG_DMA_DESC_Y, buf->dma_desc_pa); |
| 310 | mcam_reg_write(cam, REG_DESC_LEN_Y, |
| 311 | buf->dma_desc_nent*sizeof(struct mcam_dma_desc)); |
| 312 | mcam_reg_write(cam, REG_DESC_LEN_U, 0); |
| 313 | mcam_reg_write(cam, REG_DESC_LEN_V, 0); |
| 314 | cam->vb_bufs[0] = buf; |
Jonathan Corbet | a9b36e8 | 2011-06-20 16:14:40 -0300 | [diff] [blame] | 315 | } |
| 316 | |
Jonathan Corbet | cbc4f3a | 2011-06-30 17:05:27 -0300 | [diff] [blame^] | 317 | /* |
| 318 | * Initial B_DMA_sg setup |
| 319 | */ |
| 320 | static void mcam_ctlr_dma_sg(struct mcam_camera *cam) |
| 321 | { |
| 322 | mcam_reg_clear_bit(cam, REG_CTRL1, C1_DESC_3WORD); |
| 323 | mcam_sg_next_buffer(cam); |
| 324 | mcam_reg_set_bit(cam, REG_CTRL1, C1_DESC_ENA); |
| 325 | cam->nbufs = 3; |
| 326 | } |
| 327 | |
| 328 | /* |
| 329 | * Image format setup, independent of DMA scheme. |
| 330 | */ |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 331 | static void mcam_ctlr_image(struct mcam_camera *cam) |
| 332 | { |
| 333 | int imgsz; |
| 334 | struct v4l2_pix_format *fmt = &cam->pix_format; |
| 335 | |
| 336 | imgsz = ((fmt->height << IMGSZ_V_SHIFT) & IMGSZ_V_MASK) | |
| 337 | (fmt->bytesperline & IMGSZ_H_MASK); |
| 338 | mcam_reg_write(cam, REG_IMGSIZE, imgsz); |
| 339 | mcam_reg_write(cam, REG_IMGOFFSET, 0); |
| 340 | /* YPITCH just drops the last two bits */ |
| 341 | mcam_reg_write_mask(cam, REG_IMGPITCH, fmt->bytesperline, |
| 342 | IMGP_YP_MASK); |
| 343 | /* |
| 344 | * Tell the controller about the image format we are using. |
| 345 | */ |
| 346 | switch (cam->pix_format.pixelformat) { |
| 347 | case V4L2_PIX_FMT_YUYV: |
| 348 | mcam_reg_write_mask(cam, REG_CTRL0, |
| 349 | C0_DF_YUV|C0_YUV_PACKED|C0_YUVE_YUYV, |
| 350 | C0_DF_MASK); |
| 351 | break; |
| 352 | |
| 353 | case V4L2_PIX_FMT_RGB444: |
| 354 | mcam_reg_write_mask(cam, REG_CTRL0, |
| 355 | C0_DF_RGB|C0_RGBF_444|C0_RGB4_XRGB, |
| 356 | C0_DF_MASK); |
| 357 | /* Alpha value? */ |
| 358 | break; |
| 359 | |
| 360 | case V4L2_PIX_FMT_RGB565: |
| 361 | mcam_reg_write_mask(cam, REG_CTRL0, |
| 362 | C0_DF_RGB|C0_RGBF_565|C0_RGB5_BGGR, |
| 363 | C0_DF_MASK); |
| 364 | break; |
| 365 | |
| 366 | default: |
| 367 | cam_err(cam, "Unknown format %x\n", cam->pix_format.pixelformat); |
| 368 | break; |
| 369 | } |
| 370 | /* |
| 371 | * Make sure it knows we want to use hsync/vsync. |
| 372 | */ |
| 373 | mcam_reg_write_mask(cam, REG_CTRL0, C0_SIF_HVSYNC, |
| 374 | C0_SIFM_MASK); |
| 375 | } |
| 376 | |
| 377 | |
| 378 | /* |
| 379 | * Configure the controller for operation; caller holds the |
| 380 | * device mutex. |
| 381 | */ |
| 382 | static int mcam_ctlr_configure(struct mcam_camera *cam) |
| 383 | { |
| 384 | unsigned long flags; |
| 385 | |
| 386 | spin_lock_irqsave(&cam->dev_lock, flags); |
Jonathan Corbet | cbc4f3a | 2011-06-30 17:05:27 -0300 | [diff] [blame^] | 387 | switch (cam->buffer_mode) { |
| 388 | case B_vmalloc: |
| 389 | mcam_ctlr_dma_vmalloc(cam); |
| 390 | break; |
| 391 | case B_DMA_contig: |
| 392 | mcam_ctlr_dma_contig(cam); |
| 393 | break; |
| 394 | case B_DMA_sg: |
| 395 | mcam_ctlr_dma_sg(cam); |
| 396 | break; |
| 397 | } |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 398 | mcam_ctlr_image(cam); |
| 399 | mcam_set_config_needed(cam, 0); |
Jonathan Corbet | cbc4f3a | 2011-06-30 17:05:27 -0300 | [diff] [blame^] | 400 | clear_bit(CF_SG_RESTART, &cam->flags); |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 401 | spin_unlock_irqrestore(&cam->dev_lock, flags); |
| 402 | return 0; |
| 403 | } |
| 404 | |
| 405 | static void mcam_ctlr_irq_enable(struct mcam_camera *cam) |
| 406 | { |
| 407 | /* |
| 408 | * Clear any pending interrupts, since we do not |
| 409 | * expect to have I/O active prior to enabling. |
| 410 | */ |
| 411 | mcam_reg_write(cam, REG_IRQSTAT, FRAMEIRQS); |
| 412 | mcam_reg_set_bit(cam, REG_IRQMASK, FRAMEIRQS); |
| 413 | } |
| 414 | |
| 415 | static void mcam_ctlr_irq_disable(struct mcam_camera *cam) |
| 416 | { |
| 417 | mcam_reg_clear_bit(cam, REG_IRQMASK, FRAMEIRQS); |
| 418 | } |
| 419 | |
| 420 | /* |
| 421 | * Make the controller start grabbing images. Everything must |
| 422 | * be set up before doing this. |
| 423 | */ |
| 424 | static void mcam_ctlr_start(struct mcam_camera *cam) |
| 425 | { |
| 426 | /* set_bit performs a read, so no other barrier should be |
| 427 | needed here */ |
| 428 | mcam_reg_set_bit(cam, REG_CTRL0, C0_ENABLE); |
| 429 | } |
| 430 | |
| 431 | static void mcam_ctlr_stop(struct mcam_camera *cam) |
| 432 | { |
| 433 | mcam_reg_clear_bit(cam, REG_CTRL0, C0_ENABLE); |
| 434 | } |
| 435 | |
Jonathan Corbet | cbc4f3a | 2011-06-30 17:05:27 -0300 | [diff] [blame^] | 436 | /* |
| 437 | * Scatter/gather mode requires stopping the controller between |
| 438 | * frames so we can put in a new DMA descriptor array. If no new |
| 439 | * buffer exists at frame completion, the controller is left stopped; |
| 440 | * this function is charged with gettig things going again. |
| 441 | */ |
| 442 | static void mcam_sg_restart(struct mcam_camera *cam) |
| 443 | { |
| 444 | mcam_ctlr_dma_sg(cam); |
| 445 | mcam_ctlr_start(cam); |
| 446 | clear_bit(CF_SG_RESTART, &cam->flags); |
| 447 | } |
| 448 | |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 449 | static void mcam_ctlr_init(struct mcam_camera *cam) |
| 450 | { |
| 451 | unsigned long flags; |
| 452 | |
| 453 | spin_lock_irqsave(&cam->dev_lock, flags); |
| 454 | /* |
| 455 | * Make sure it's not powered down. |
| 456 | */ |
| 457 | mcam_reg_clear_bit(cam, REG_CTRL1, C1_PWRDWN); |
| 458 | /* |
| 459 | * Turn off the enable bit. It sure should be off anyway, |
| 460 | * but it's good to be sure. |
| 461 | */ |
| 462 | mcam_reg_clear_bit(cam, REG_CTRL0, C0_ENABLE); |
| 463 | /* |
| 464 | * Clock the sensor appropriately. Controller clock should |
| 465 | * be 48MHz, sensor "typical" value is half that. |
| 466 | */ |
| 467 | mcam_reg_write_mask(cam, REG_CLKCTRL, 2, CLK_DIV_MASK); |
| 468 | spin_unlock_irqrestore(&cam->dev_lock, flags); |
| 469 | } |
| 470 | |
| 471 | |
| 472 | /* |
| 473 | * Stop the controller, and don't return until we're really sure that no |
| 474 | * further DMA is going on. |
| 475 | */ |
| 476 | static void mcam_ctlr_stop_dma(struct mcam_camera *cam) |
| 477 | { |
| 478 | unsigned long flags; |
| 479 | |
| 480 | /* |
| 481 | * Theory: stop the camera controller (whether it is operating |
| 482 | * or not). Delay briefly just in case we race with the SOF |
| 483 | * interrupt, then wait until no DMA is active. |
| 484 | */ |
| 485 | spin_lock_irqsave(&cam->dev_lock, flags); |
Jonathan Corbet | cbc4f3a | 2011-06-30 17:05:27 -0300 | [diff] [blame^] | 486 | clear_bit(CF_SG_RESTART, &cam->flags); |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 487 | mcam_ctlr_stop(cam); |
Jonathan Corbet | cbc4f3a | 2011-06-30 17:05:27 -0300 | [diff] [blame^] | 488 | cam->state = S_IDLE; |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 489 | spin_unlock_irqrestore(&cam->dev_lock, flags); |
Jonathan Corbet | cbc4f3a | 2011-06-30 17:05:27 -0300 | [diff] [blame^] | 490 | msleep(40); |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 491 | if (test_bit(CF_DMA_ACTIVE, &cam->flags)) |
| 492 | cam_err(cam, "Timeout waiting for DMA to end\n"); |
| 493 | /* This would be bad news - what now? */ |
| 494 | spin_lock_irqsave(&cam->dev_lock, flags); |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 495 | mcam_ctlr_irq_disable(cam); |
| 496 | spin_unlock_irqrestore(&cam->dev_lock, flags); |
| 497 | } |
| 498 | |
| 499 | /* |
| 500 | * Power up and down. |
| 501 | */ |
| 502 | static void mcam_ctlr_power_up(struct mcam_camera *cam) |
| 503 | { |
| 504 | unsigned long flags; |
| 505 | |
| 506 | spin_lock_irqsave(&cam->dev_lock, flags); |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 507 | cam->plat_power_up(cam); |
Jonathan Corbet | 67a8dbb | 2011-06-11 14:46:49 -0300 | [diff] [blame] | 508 | mcam_reg_clear_bit(cam, REG_CTRL1, C1_PWRDWN); |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 509 | spin_unlock_irqrestore(&cam->dev_lock, flags); |
| 510 | msleep(5); /* Just to be sure */ |
| 511 | } |
| 512 | |
| 513 | static void mcam_ctlr_power_down(struct mcam_camera *cam) |
| 514 | { |
| 515 | unsigned long flags; |
| 516 | |
| 517 | spin_lock_irqsave(&cam->dev_lock, flags); |
Jonathan Corbet | 67a8dbb | 2011-06-11 14:46:49 -0300 | [diff] [blame] | 518 | /* |
| 519 | * School of hard knocks department: be sure we do any register |
| 520 | * twiddling on the controller *before* calling the platform |
| 521 | * power down routine. |
| 522 | */ |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 523 | mcam_reg_set_bit(cam, REG_CTRL1, C1_PWRDWN); |
Jonathan Corbet | 67a8dbb | 2011-06-11 14:46:49 -0300 | [diff] [blame] | 524 | cam->plat_power_down(cam); |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 525 | spin_unlock_irqrestore(&cam->dev_lock, flags); |
| 526 | } |
| 527 | |
| 528 | /* -------------------------------------------------------------------- */ |
| 529 | /* |
| 530 | * Communications with the sensor. |
| 531 | */ |
| 532 | |
| 533 | static int __mcam_cam_reset(struct mcam_camera *cam) |
| 534 | { |
| 535 | return sensor_call(cam, core, reset, 0); |
| 536 | } |
| 537 | |
| 538 | /* |
| 539 | * We have found the sensor on the i2c. Let's try to have a |
| 540 | * conversation. |
| 541 | */ |
| 542 | static int mcam_cam_init(struct mcam_camera *cam) |
| 543 | { |
| 544 | struct v4l2_dbg_chip_ident chip; |
| 545 | int ret; |
| 546 | |
| 547 | mutex_lock(&cam->s_mutex); |
| 548 | if (cam->state != S_NOTREADY) |
| 549 | cam_warn(cam, "Cam init with device in funky state %d", |
| 550 | cam->state); |
| 551 | ret = __mcam_cam_reset(cam); |
| 552 | if (ret) |
| 553 | goto out; |
| 554 | chip.ident = V4L2_IDENT_NONE; |
| 555 | chip.match.type = V4L2_CHIP_MATCH_I2C_ADDR; |
| 556 | chip.match.addr = cam->sensor_addr; |
| 557 | ret = sensor_call(cam, core, g_chip_ident, &chip); |
| 558 | if (ret) |
| 559 | goto out; |
| 560 | cam->sensor_type = chip.ident; |
| 561 | if (cam->sensor_type != V4L2_IDENT_OV7670) { |
| 562 | cam_err(cam, "Unsupported sensor type 0x%x", cam->sensor_type); |
| 563 | ret = -EINVAL; |
| 564 | goto out; |
| 565 | } |
| 566 | /* Get/set parameters? */ |
| 567 | ret = 0; |
| 568 | cam->state = S_IDLE; |
| 569 | out: |
| 570 | mcam_ctlr_power_down(cam); |
| 571 | mutex_unlock(&cam->s_mutex); |
| 572 | return ret; |
| 573 | } |
| 574 | |
| 575 | /* |
| 576 | * Configure the sensor to match the parameters we have. Caller should |
| 577 | * hold s_mutex |
| 578 | */ |
| 579 | static int mcam_cam_set_flip(struct mcam_camera *cam) |
| 580 | { |
| 581 | struct v4l2_control ctrl; |
| 582 | |
| 583 | memset(&ctrl, 0, sizeof(ctrl)); |
| 584 | ctrl.id = V4L2_CID_VFLIP; |
| 585 | ctrl.value = flip; |
| 586 | return sensor_call(cam, core, s_ctrl, &ctrl); |
| 587 | } |
| 588 | |
| 589 | |
| 590 | static int mcam_cam_configure(struct mcam_camera *cam) |
| 591 | { |
| 592 | struct v4l2_mbus_framefmt mbus_fmt; |
| 593 | int ret; |
| 594 | |
| 595 | v4l2_fill_mbus_format(&mbus_fmt, &cam->pix_format, cam->mbus_code); |
| 596 | ret = sensor_call(cam, core, init, 0); |
| 597 | if (ret == 0) |
| 598 | ret = sensor_call(cam, video, s_mbus_fmt, &mbus_fmt); |
| 599 | /* |
| 600 | * OV7670 does weird things if flip is set *before* format... |
| 601 | */ |
| 602 | ret += mcam_cam_set_flip(cam); |
| 603 | return ret; |
| 604 | } |
| 605 | |
| 606 | /* -------------------------------------------------------------------- */ |
| 607 | /* |
| 608 | * DMA buffer management. These functions need s_mutex held. |
| 609 | */ |
| 610 | |
Jonathan Corbet | cbc4f3a | 2011-06-30 17:05:27 -0300 | [diff] [blame^] | 611 | /* |
| 612 | * Allocate in-kernel DMA buffers for vmalloc mode. |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 613 | */ |
| 614 | static int mcam_alloc_dma_bufs(struct mcam_camera *cam, int loadtime) |
| 615 | { |
| 616 | int i; |
| 617 | |
| 618 | mcam_set_config_needed(cam, 1); |
| 619 | if (loadtime) |
| 620 | cam->dma_buf_size = dma_buf_size; |
| 621 | else |
| 622 | cam->dma_buf_size = cam->pix_format.sizeimage; |
| 623 | if (n_dma_bufs > 3) |
| 624 | n_dma_bufs = 3; |
| 625 | |
| 626 | cam->nbufs = 0; |
| 627 | for (i = 0; i < n_dma_bufs; i++) { |
| 628 | cam->dma_bufs[i] = dma_alloc_coherent(cam->dev, |
| 629 | cam->dma_buf_size, cam->dma_handles + i, |
| 630 | GFP_KERNEL); |
| 631 | if (cam->dma_bufs[i] == NULL) { |
| 632 | cam_warn(cam, "Failed to allocate DMA buffer\n"); |
| 633 | break; |
| 634 | } |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 635 | (cam->nbufs)++; |
| 636 | } |
| 637 | |
| 638 | switch (cam->nbufs) { |
| 639 | case 1: |
| 640 | dma_free_coherent(cam->dev, cam->dma_buf_size, |
| 641 | cam->dma_bufs[0], cam->dma_handles[0]); |
| 642 | cam->nbufs = 0; |
| 643 | case 0: |
| 644 | cam_err(cam, "Insufficient DMA buffers, cannot operate\n"); |
| 645 | return -ENOMEM; |
| 646 | |
| 647 | case 2: |
| 648 | if (n_dma_bufs > 2) |
| 649 | cam_warn(cam, "Will limp along with only 2 buffers\n"); |
| 650 | break; |
| 651 | } |
| 652 | return 0; |
| 653 | } |
| 654 | |
| 655 | static void mcam_free_dma_bufs(struct mcam_camera *cam) |
| 656 | { |
| 657 | int i; |
| 658 | |
| 659 | for (i = 0; i < cam->nbufs; i++) { |
| 660 | dma_free_coherent(cam->dev, cam->dma_buf_size, |
| 661 | cam->dma_bufs[i], cam->dma_handles[i]); |
| 662 | cam->dma_bufs[i] = NULL; |
| 663 | } |
| 664 | cam->nbufs = 0; |
| 665 | } |
| 666 | |
| 667 | |
| 668 | |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 669 | /* ----------------------------------------------------------------------- */ |
| 670 | /* |
| 671 | * Here starts the V4L2 interface code. |
| 672 | */ |
| 673 | |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 674 | |
| 675 | /* |
| 676 | * Get everything ready, and start grabbing frames. |
| 677 | */ |
Jonathan Corbet | a9b36e8 | 2011-06-20 16:14:40 -0300 | [diff] [blame] | 678 | static int mcam_read_setup(struct mcam_camera *cam) |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 679 | { |
| 680 | int ret; |
| 681 | unsigned long flags; |
| 682 | |
| 683 | /* |
| 684 | * Configuration. If we still don't have DMA buffers, |
| 685 | * make one last, desperate attempt. |
| 686 | */ |
Jonathan Corbet | a9b36e8 | 2011-06-20 16:14:40 -0300 | [diff] [blame] | 687 | if (cam->buffer_mode == B_vmalloc && cam->nbufs == 0 && |
| 688 | mcam_alloc_dma_bufs(cam, 0)) |
| 689 | return -ENOMEM; |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 690 | |
| 691 | if (mcam_needs_config(cam)) { |
| 692 | mcam_cam_configure(cam); |
| 693 | ret = mcam_ctlr_configure(cam); |
| 694 | if (ret) |
| 695 | return ret; |
| 696 | } |
| 697 | |
| 698 | /* |
| 699 | * Turn it loose. |
| 700 | */ |
| 701 | spin_lock_irqsave(&cam->dev_lock, flags); |
| 702 | mcam_reset_buffers(cam); |
| 703 | mcam_ctlr_irq_enable(cam); |
Jonathan Corbet | a9b36e8 | 2011-06-20 16:14:40 -0300 | [diff] [blame] | 704 | cam->state = S_STREAMING; |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 705 | mcam_ctlr_start(cam); |
| 706 | spin_unlock_irqrestore(&cam->dev_lock, flags); |
| 707 | return 0; |
| 708 | } |
| 709 | |
Jonathan Corbet | b5210fd | 2011-06-20 16:14:36 -0300 | [diff] [blame] | 710 | /* ----------------------------------------------------------------------- */ |
| 711 | /* |
| 712 | * Videobuf2 interface code. |
| 713 | */ |
| 714 | |
| 715 | static int mcam_vb_queue_setup(struct vb2_queue *vq, unsigned int *nbufs, |
| 716 | unsigned int *num_planes, unsigned long sizes[], |
| 717 | void *alloc_ctxs[]) |
| 718 | { |
| 719 | struct mcam_camera *cam = vb2_get_drv_priv(vq); |
Jonathan Corbet | cbc4f3a | 2011-06-30 17:05:27 -0300 | [diff] [blame^] | 720 | int minbufs = (cam->buffer_mode == B_DMA_contig) ? 3 : 2; |
Jonathan Corbet | b5210fd | 2011-06-20 16:14:36 -0300 | [diff] [blame] | 721 | |
| 722 | sizes[0] = cam->pix_format.sizeimage; |
| 723 | *num_planes = 1; /* Someday we have to support planar formats... */ |
Jonathan Corbet | cbc4f3a | 2011-06-30 17:05:27 -0300 | [diff] [blame^] | 724 | if (*nbufs < minbufs) |
| 725 | *nbufs = minbufs; |
Jonathan Corbet | a9b36e8 | 2011-06-20 16:14:40 -0300 | [diff] [blame] | 726 | if (cam->buffer_mode == B_DMA_contig) |
| 727 | alloc_ctxs[0] = cam->vb_alloc_ctx; |
Jonathan Corbet | b5210fd | 2011-06-20 16:14:36 -0300 | [diff] [blame] | 728 | return 0; |
| 729 | } |
| 730 | |
Jonathan Corbet | cbc4f3a | 2011-06-30 17:05:27 -0300 | [diff] [blame^] | 731 | /* DMA_sg only */ |
| 732 | static int mcam_vb_sg_buf_init(struct vb2_buffer *vb) |
Jonathan Corbet | b5210fd | 2011-06-20 16:14:36 -0300 | [diff] [blame] | 733 | { |
| 734 | struct mcam_vb_buffer *mvb = vb_to_mvb(vb); |
Jonathan Corbet | cbc4f3a | 2011-06-30 17:05:27 -0300 | [diff] [blame^] | 735 | struct mcam_camera *cam = vb2_get_drv_priv(vb->vb2_queue); |
| 736 | int ndesc = cam->pix_format.sizeimage/PAGE_SIZE + 1; |
Jonathan Corbet | b5210fd | 2011-06-20 16:14:36 -0300 | [diff] [blame] | 737 | |
Jonathan Corbet | cbc4f3a | 2011-06-30 17:05:27 -0300 | [diff] [blame^] | 738 | mvb->dma_desc = dma_alloc_coherent(cam->dev, |
| 739 | ndesc * sizeof(struct mcam_dma_desc), |
| 740 | &mvb->dma_desc_pa, GFP_KERNEL); |
| 741 | if (mvb->dma_desc == NULL) { |
| 742 | cam_err(cam, "Unable to get DMA descriptor array\n"); |
| 743 | return -ENOMEM; |
| 744 | } |
Jonathan Corbet | b5210fd | 2011-06-20 16:14:36 -0300 | [diff] [blame] | 745 | return 0; |
| 746 | } |
| 747 | |
Jonathan Corbet | cbc4f3a | 2011-06-30 17:05:27 -0300 | [diff] [blame^] | 748 | static int mcam_vb_sg_buf_prepare(struct vb2_buffer *vb) |
| 749 | { |
| 750 | struct mcam_vb_buffer *mvb = vb_to_mvb(vb); |
| 751 | struct mcam_camera *cam = vb2_get_drv_priv(vb->vb2_queue); |
| 752 | struct vb2_dma_sg_desc *sgd = vb2_dma_sg_plane_desc(vb, 0); |
| 753 | struct mcam_dma_desc *desc = mvb->dma_desc; |
| 754 | struct scatterlist *sg; |
| 755 | int i; |
| 756 | |
| 757 | mvb->dma_desc_nent = dma_map_sg(cam->dev, sgd->sglist, sgd->num_pages, |
| 758 | DMA_FROM_DEVICE); |
| 759 | if (mvb->dma_desc_nent <= 0) |
| 760 | return -EIO; /* Not sure what's right here */ |
| 761 | for_each_sg(sgd->sglist, sg, mvb->dma_desc_nent, i) { |
| 762 | desc->dma_addr = sg_dma_address(sg); |
| 763 | desc->segment_len = sg_dma_len(sg); |
| 764 | desc++; |
| 765 | } |
| 766 | return 0; |
| 767 | } |
| 768 | |
| 769 | |
Jonathan Corbet | b5210fd | 2011-06-20 16:14:36 -0300 | [diff] [blame] | 770 | static void mcam_vb_buf_queue(struct vb2_buffer *vb) |
| 771 | { |
| 772 | struct mcam_vb_buffer *mvb = vb_to_mvb(vb); |
| 773 | struct mcam_camera *cam = vb2_get_drv_priv(vb->vb2_queue); |
| 774 | unsigned long flags; |
Jonathan Corbet | a9b36e8 | 2011-06-20 16:14:40 -0300 | [diff] [blame] | 775 | int start; |
Jonathan Corbet | b5210fd | 2011-06-20 16:14:36 -0300 | [diff] [blame] | 776 | |
| 777 | spin_lock_irqsave(&cam->dev_lock, flags); |
Jonathan Corbet | a9b36e8 | 2011-06-20 16:14:40 -0300 | [diff] [blame] | 778 | start = (cam->state == S_BUFWAIT) && !list_empty(&cam->buffers); |
| 779 | list_add(&mvb->queue, &cam->buffers); |
Jonathan Corbet | cbc4f3a | 2011-06-30 17:05:27 -0300 | [diff] [blame^] | 780 | if (test_bit(CF_SG_RESTART, &cam->flags)) |
| 781 | mcam_sg_restart(cam); |
Jonathan Corbet | b5210fd | 2011-06-20 16:14:36 -0300 | [diff] [blame] | 782 | spin_unlock_irqrestore(&cam->dev_lock, flags); |
Jonathan Corbet | a9b36e8 | 2011-06-20 16:14:40 -0300 | [diff] [blame] | 783 | if (start) |
| 784 | mcam_read_setup(cam); |
Jonathan Corbet | b5210fd | 2011-06-20 16:14:36 -0300 | [diff] [blame] | 785 | } |
| 786 | |
Jonathan Corbet | cbc4f3a | 2011-06-30 17:05:27 -0300 | [diff] [blame^] | 787 | |
| 788 | static int mcam_vb_sg_buf_finish(struct vb2_buffer *vb) |
| 789 | { |
| 790 | struct mcam_camera *cam = vb2_get_drv_priv(vb->vb2_queue); |
| 791 | struct vb2_dma_sg_desc *sgd = vb2_dma_sg_plane_desc(vb, 0); |
| 792 | |
| 793 | dma_unmap_sg(cam->dev, sgd->sglist, sgd->num_pages, DMA_FROM_DEVICE); |
| 794 | return 0; |
| 795 | } |
| 796 | |
| 797 | static void mcam_vb_sg_buf_cleanup(struct vb2_buffer *vb) |
| 798 | { |
| 799 | struct mcam_camera *cam = vb2_get_drv_priv(vb->vb2_queue); |
| 800 | struct mcam_vb_buffer *mvb = vb_to_mvb(vb); |
| 801 | int ndesc = cam->pix_format.sizeimage/PAGE_SIZE + 1; |
| 802 | |
| 803 | dma_free_coherent(cam->dev, ndesc * sizeof(struct mcam_dma_desc), |
| 804 | mvb->dma_desc, mvb->dma_desc_pa); |
| 805 | } |
| 806 | |
| 807 | |
Jonathan Corbet | b5210fd | 2011-06-20 16:14:36 -0300 | [diff] [blame] | 808 | /* |
| 809 | * vb2 uses these to release the mutex when waiting in dqbuf. I'm |
| 810 | * not actually sure we need to do this (I'm not sure that vb2_dqbuf() needs |
| 811 | * to be called with the mutex held), but better safe than sorry. |
| 812 | */ |
| 813 | static void mcam_vb_wait_prepare(struct vb2_queue *vq) |
| 814 | { |
| 815 | struct mcam_camera *cam = vb2_get_drv_priv(vq); |
| 816 | |
| 817 | mutex_unlock(&cam->s_mutex); |
| 818 | } |
| 819 | |
| 820 | static void mcam_vb_wait_finish(struct vb2_queue *vq) |
| 821 | { |
| 822 | struct mcam_camera *cam = vb2_get_drv_priv(vq); |
| 823 | |
| 824 | mutex_lock(&cam->s_mutex); |
| 825 | } |
| 826 | |
| 827 | /* |
| 828 | * These need to be called with the mutex held from vb2 |
| 829 | */ |
| 830 | static int mcam_vb_start_streaming(struct vb2_queue *vq) |
| 831 | { |
| 832 | struct mcam_camera *cam = vb2_get_drv_priv(vq); |
Jonathan Corbet | b5210fd | 2011-06-20 16:14:36 -0300 | [diff] [blame] | 833 | |
Jonathan Corbet | a9b36e8 | 2011-06-20 16:14:40 -0300 | [diff] [blame] | 834 | if (cam->state != S_IDLE) |
| 835 | return -EINVAL; |
| 836 | cam->sequence = 0; |
| 837 | /* |
| 838 | * Videobuf2 sneakily hoards all the buffers and won't |
| 839 | * give them to us until *after* streaming starts. But |
| 840 | * we can't actually start streaming until we have a |
| 841 | * destination. So go into a wait state and hope they |
| 842 | * give us buffers soon. |
| 843 | */ |
| 844 | if (cam->buffer_mode != B_vmalloc && list_empty(&cam->buffers)) { |
| 845 | cam->state = S_BUFWAIT; |
| 846 | return 0; |
Jonathan Corbet | b5210fd | 2011-06-20 16:14:36 -0300 | [diff] [blame] | 847 | } |
Jonathan Corbet | a9b36e8 | 2011-06-20 16:14:40 -0300 | [diff] [blame] | 848 | return mcam_read_setup(cam); |
Jonathan Corbet | b5210fd | 2011-06-20 16:14:36 -0300 | [diff] [blame] | 849 | } |
| 850 | |
| 851 | static int mcam_vb_stop_streaming(struct vb2_queue *vq) |
| 852 | { |
| 853 | struct mcam_camera *cam = vb2_get_drv_priv(vq); |
| 854 | unsigned long flags; |
| 855 | |
Jonathan Corbet | a9b36e8 | 2011-06-20 16:14:40 -0300 | [diff] [blame] | 856 | if (cam->state == S_BUFWAIT) { |
| 857 | /* They never gave us buffers */ |
| 858 | cam->state = S_IDLE; |
| 859 | return 0; |
| 860 | } |
Jonathan Corbet | b5210fd | 2011-06-20 16:14:36 -0300 | [diff] [blame] | 861 | if (cam->state != S_STREAMING) |
| 862 | return -EINVAL; |
| 863 | mcam_ctlr_stop_dma(cam); |
| 864 | /* |
| 865 | * VB2 reclaims the buffers, so we need to forget |
| 866 | * about them. |
| 867 | */ |
| 868 | spin_lock_irqsave(&cam->dev_lock, flags); |
| 869 | INIT_LIST_HEAD(&cam->buffers); |
| 870 | spin_unlock_irqrestore(&cam->dev_lock, flags); |
| 871 | return 0; |
| 872 | } |
| 873 | |
| 874 | |
| 875 | static const struct vb2_ops mcam_vb2_ops = { |
| 876 | .queue_setup = mcam_vb_queue_setup, |
Jonathan Corbet | b5210fd | 2011-06-20 16:14:36 -0300 | [diff] [blame] | 877 | .buf_queue = mcam_vb_buf_queue, |
| 878 | .start_streaming = mcam_vb_start_streaming, |
| 879 | .stop_streaming = mcam_vb_stop_streaming, |
| 880 | .wait_prepare = mcam_vb_wait_prepare, |
| 881 | .wait_finish = mcam_vb_wait_finish, |
| 882 | }; |
| 883 | |
Jonathan Corbet | cbc4f3a | 2011-06-30 17:05:27 -0300 | [diff] [blame^] | 884 | /* |
| 885 | * Scatter/gather mode complicates things somewhat. |
| 886 | */ |
| 887 | static const struct vb2_ops mcam_vb2_sg_ops = { |
| 888 | .queue_setup = mcam_vb_queue_setup, |
| 889 | .buf_init = mcam_vb_sg_buf_init, |
| 890 | .buf_prepare = mcam_vb_sg_buf_prepare, |
| 891 | .buf_queue = mcam_vb_buf_queue, |
| 892 | .buf_finish = mcam_vb_sg_buf_finish, |
| 893 | .buf_cleanup = mcam_vb_sg_buf_cleanup, |
| 894 | .start_streaming = mcam_vb_start_streaming, |
| 895 | .stop_streaming = mcam_vb_stop_streaming, |
| 896 | .wait_prepare = mcam_vb_wait_prepare, |
| 897 | .wait_finish = mcam_vb_wait_finish, |
| 898 | }; |
| 899 | |
Jonathan Corbet | b5210fd | 2011-06-20 16:14:36 -0300 | [diff] [blame] | 900 | static int mcam_setup_vb2(struct mcam_camera *cam) |
| 901 | { |
| 902 | struct vb2_queue *vq = &cam->vb_queue; |
| 903 | |
| 904 | memset(vq, 0, sizeof(*vq)); |
| 905 | vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
Jonathan Corbet | b5210fd | 2011-06-20 16:14:36 -0300 | [diff] [blame] | 906 | vq->drv_priv = cam; |
Jonathan Corbet | cbc4f3a | 2011-06-30 17:05:27 -0300 | [diff] [blame^] | 907 | INIT_LIST_HEAD(&cam->buffers); |
| 908 | switch (cam->buffer_mode) { |
| 909 | case B_DMA_contig: |
| 910 | vq->ops = &mcam_vb2_ops; |
Jonathan Corbet | a9b36e8 | 2011-06-20 16:14:40 -0300 | [diff] [blame] | 911 | vq->mem_ops = &vb2_dma_contig_memops; |
| 912 | cam->vb_alloc_ctx = vb2_dma_contig_init_ctx(cam->dev); |
Jonathan Corbet | cbc4f3a | 2011-06-30 17:05:27 -0300 | [diff] [blame^] | 913 | vq->io_modes = VB2_MMAP | VB2_USERPTR; |
| 914 | break; |
| 915 | case B_DMA_sg: |
| 916 | vq->ops = &mcam_vb2_sg_ops; |
| 917 | vq->mem_ops = &vb2_dma_sg_memops; |
| 918 | vq->io_modes = VB2_MMAP | VB2_USERPTR; |
| 919 | break; |
| 920 | case B_vmalloc: |
| 921 | vq->ops = &mcam_vb2_ops; |
Jonathan Corbet | a9b36e8 | 2011-06-20 16:14:40 -0300 | [diff] [blame] | 922 | vq->mem_ops = &vb2_vmalloc_memops; |
Jonathan Corbet | cbc4f3a | 2011-06-30 17:05:27 -0300 | [diff] [blame^] | 923 | vq->buf_struct_size = sizeof(struct mcam_vb_buffer); |
| 924 | vq->io_modes = VB2_MMAP; |
| 925 | break; |
| 926 | } |
Jonathan Corbet | b5210fd | 2011-06-20 16:14:36 -0300 | [diff] [blame] | 927 | return vb2_queue_init(vq); |
| 928 | } |
| 929 | |
| 930 | static void mcam_cleanup_vb2(struct mcam_camera *cam) |
| 931 | { |
| 932 | vb2_queue_release(&cam->vb_queue); |
Jonathan Corbet | a9b36e8 | 2011-06-20 16:14:40 -0300 | [diff] [blame] | 933 | if (cam->buffer_mode == B_DMA_contig) |
| 934 | vb2_dma_contig_cleanup_ctx(cam->vb_alloc_ctx); |
Jonathan Corbet | b5210fd | 2011-06-20 16:14:36 -0300 | [diff] [blame] | 935 | } |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 936 | |
| 937 | static ssize_t mcam_v4l_read(struct file *filp, |
| 938 | char __user *buffer, size_t len, loff_t *pos) |
| 939 | { |
| 940 | struct mcam_camera *cam = filp->private_data; |
Jonathan Corbet | b5210fd | 2011-06-20 16:14:36 -0300 | [diff] [blame] | 941 | int ret; |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 942 | |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 943 | mutex_lock(&cam->s_mutex); |
Jonathan Corbet | b5210fd | 2011-06-20 16:14:36 -0300 | [diff] [blame] | 944 | ret = vb2_read(&cam->vb_queue, buffer, len, pos, |
| 945 | filp->f_flags & O_NONBLOCK); |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 946 | mutex_unlock(&cam->s_mutex); |
| 947 | return ret; |
| 948 | } |
| 949 | |
| 950 | |
| 951 | |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 952 | /* |
| 953 | * Streaming I/O support. |
| 954 | */ |
| 955 | |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 956 | static int mcam_vidioc_streamon(struct file *filp, void *priv, |
| 957 | enum v4l2_buf_type type) |
| 958 | { |
| 959 | struct mcam_camera *cam = filp->private_data; |
Jonathan Corbet | b5210fd | 2011-06-20 16:14:36 -0300 | [diff] [blame] | 960 | int ret; |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 961 | |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 962 | mutex_lock(&cam->s_mutex); |
Jonathan Corbet | b5210fd | 2011-06-20 16:14:36 -0300 | [diff] [blame] | 963 | ret = vb2_streamon(&cam->vb_queue, type); |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 964 | mutex_unlock(&cam->s_mutex); |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 965 | return ret; |
| 966 | } |
| 967 | |
| 968 | |
| 969 | static int mcam_vidioc_streamoff(struct file *filp, void *priv, |
| 970 | enum v4l2_buf_type type) |
| 971 | { |
| 972 | struct mcam_camera *cam = filp->private_data; |
Jonathan Corbet | b5210fd | 2011-06-20 16:14:36 -0300 | [diff] [blame] | 973 | int ret; |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 974 | |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 975 | mutex_lock(&cam->s_mutex); |
Jonathan Corbet | b5210fd | 2011-06-20 16:14:36 -0300 | [diff] [blame] | 976 | ret = vb2_streamoff(&cam->vb_queue, type); |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 977 | mutex_unlock(&cam->s_mutex); |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 978 | return ret; |
| 979 | } |
| 980 | |
| 981 | |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 982 | static int mcam_vidioc_reqbufs(struct file *filp, void *priv, |
| 983 | struct v4l2_requestbuffers *req) |
| 984 | { |
| 985 | struct mcam_camera *cam = filp->private_data; |
Jonathan Corbet | b5210fd | 2011-06-20 16:14:36 -0300 | [diff] [blame] | 986 | int ret; |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 987 | |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 988 | mutex_lock(&cam->s_mutex); |
Jonathan Corbet | b5210fd | 2011-06-20 16:14:36 -0300 | [diff] [blame] | 989 | ret = vb2_reqbufs(&cam->vb_queue, req); |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 990 | mutex_unlock(&cam->s_mutex); |
| 991 | return ret; |
| 992 | } |
| 993 | |
| 994 | |
| 995 | static int mcam_vidioc_querybuf(struct file *filp, void *priv, |
| 996 | struct v4l2_buffer *buf) |
| 997 | { |
| 998 | struct mcam_camera *cam = filp->private_data; |
Jonathan Corbet | b5210fd | 2011-06-20 16:14:36 -0300 | [diff] [blame] | 999 | int ret; |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 1000 | |
| 1001 | mutex_lock(&cam->s_mutex); |
Jonathan Corbet | b5210fd | 2011-06-20 16:14:36 -0300 | [diff] [blame] | 1002 | ret = vb2_querybuf(&cam->vb_queue, buf); |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 1003 | mutex_unlock(&cam->s_mutex); |
| 1004 | return ret; |
| 1005 | } |
| 1006 | |
| 1007 | static int mcam_vidioc_qbuf(struct file *filp, void *priv, |
| 1008 | struct v4l2_buffer *buf) |
| 1009 | { |
| 1010 | struct mcam_camera *cam = filp->private_data; |
Jonathan Corbet | b5210fd | 2011-06-20 16:14:36 -0300 | [diff] [blame] | 1011 | int ret; |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 1012 | |
| 1013 | mutex_lock(&cam->s_mutex); |
Jonathan Corbet | b5210fd | 2011-06-20 16:14:36 -0300 | [diff] [blame] | 1014 | ret = vb2_qbuf(&cam->vb_queue, buf); |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 1015 | mutex_unlock(&cam->s_mutex); |
| 1016 | return ret; |
| 1017 | } |
| 1018 | |
| 1019 | static int mcam_vidioc_dqbuf(struct file *filp, void *priv, |
| 1020 | struct v4l2_buffer *buf) |
| 1021 | { |
| 1022 | struct mcam_camera *cam = filp->private_data; |
Jonathan Corbet | b5210fd | 2011-06-20 16:14:36 -0300 | [diff] [blame] | 1023 | int ret; |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 1024 | |
| 1025 | mutex_lock(&cam->s_mutex); |
Jonathan Corbet | b5210fd | 2011-06-20 16:14:36 -0300 | [diff] [blame] | 1026 | ret = vb2_dqbuf(&cam->vb_queue, buf, filp->f_flags & O_NONBLOCK); |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 1027 | mutex_unlock(&cam->s_mutex); |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 1028 | return ret; |
| 1029 | } |
| 1030 | |
| 1031 | |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 1032 | static int mcam_v4l_mmap(struct file *filp, struct vm_area_struct *vma) |
| 1033 | { |
| 1034 | struct mcam_camera *cam = filp->private_data; |
Jonathan Corbet | b5210fd | 2011-06-20 16:14:36 -0300 | [diff] [blame] | 1035 | int ret; |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 1036 | |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 1037 | mutex_lock(&cam->s_mutex); |
Jonathan Corbet | b5210fd | 2011-06-20 16:14:36 -0300 | [diff] [blame] | 1038 | ret = vb2_mmap(&cam->vb_queue, vma); |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 1039 | mutex_unlock(&cam->s_mutex); |
| 1040 | return ret; |
| 1041 | } |
| 1042 | |
| 1043 | |
| 1044 | |
| 1045 | static int mcam_v4l_open(struct file *filp) |
| 1046 | { |
| 1047 | struct mcam_camera *cam = video_drvdata(filp); |
Jonathan Corbet | b5210fd | 2011-06-20 16:14:36 -0300 | [diff] [blame] | 1048 | int ret = 0; |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 1049 | |
| 1050 | filp->private_data = cam; |
| 1051 | |
Jonathan Corbet | a9b36e8 | 2011-06-20 16:14:40 -0300 | [diff] [blame] | 1052 | frames = singles = delivered = 0; |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 1053 | mutex_lock(&cam->s_mutex); |
| 1054 | if (cam->users == 0) { |
Jonathan Corbet | b5210fd | 2011-06-20 16:14:36 -0300 | [diff] [blame] | 1055 | ret = mcam_setup_vb2(cam); |
| 1056 | if (ret) |
| 1057 | goto out; |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 1058 | mcam_ctlr_power_up(cam); |
| 1059 | __mcam_cam_reset(cam); |
| 1060 | mcam_set_config_needed(cam, 1); |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 1061 | } |
| 1062 | (cam->users)++; |
Jonathan Corbet | b5210fd | 2011-06-20 16:14:36 -0300 | [diff] [blame] | 1063 | out: |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 1064 | mutex_unlock(&cam->s_mutex); |
Jonathan Corbet | b5210fd | 2011-06-20 16:14:36 -0300 | [diff] [blame] | 1065 | return ret; |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 1066 | } |
| 1067 | |
| 1068 | |
| 1069 | static int mcam_v4l_release(struct file *filp) |
| 1070 | { |
| 1071 | struct mcam_camera *cam = filp->private_data; |
| 1072 | |
Jonathan Corbet | a9b36e8 | 2011-06-20 16:14:40 -0300 | [diff] [blame] | 1073 | cam_err(cam, "Release, %d frames, %d singles, %d delivered\n", frames, |
| 1074 | singles, delivered); |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 1075 | mutex_lock(&cam->s_mutex); |
| 1076 | (cam->users)--; |
| 1077 | if (filp == cam->owner) { |
| 1078 | mcam_ctlr_stop_dma(cam); |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 1079 | cam->owner = NULL; |
| 1080 | } |
| 1081 | if (cam->users == 0) { |
Jonathan Corbet | b5210fd | 2011-06-20 16:14:36 -0300 | [diff] [blame] | 1082 | mcam_cleanup_vb2(cam); |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 1083 | mcam_ctlr_power_down(cam); |
Jonathan Corbet | a9b36e8 | 2011-06-20 16:14:40 -0300 | [diff] [blame] | 1084 | if (cam->buffer_mode == B_vmalloc && alloc_bufs_at_read) |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 1085 | mcam_free_dma_bufs(cam); |
| 1086 | } |
| 1087 | mutex_unlock(&cam->s_mutex); |
| 1088 | return 0; |
| 1089 | } |
| 1090 | |
| 1091 | |
| 1092 | |
| 1093 | static unsigned int mcam_v4l_poll(struct file *filp, |
| 1094 | struct poll_table_struct *pt) |
| 1095 | { |
| 1096 | struct mcam_camera *cam = filp->private_data; |
Jonathan Corbet | b5210fd | 2011-06-20 16:14:36 -0300 | [diff] [blame] | 1097 | int ret; |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 1098 | |
Jonathan Corbet | b5210fd | 2011-06-20 16:14:36 -0300 | [diff] [blame] | 1099 | mutex_lock(&cam->s_mutex); |
| 1100 | ret = vb2_poll(&cam->vb_queue, filp, pt); |
| 1101 | mutex_unlock(&cam->s_mutex); |
| 1102 | return ret; |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 1103 | } |
| 1104 | |
| 1105 | |
| 1106 | |
| 1107 | static int mcam_vidioc_queryctrl(struct file *filp, void *priv, |
| 1108 | struct v4l2_queryctrl *qc) |
| 1109 | { |
| 1110 | struct mcam_camera *cam = priv; |
| 1111 | int ret; |
| 1112 | |
| 1113 | mutex_lock(&cam->s_mutex); |
| 1114 | ret = sensor_call(cam, core, queryctrl, qc); |
| 1115 | mutex_unlock(&cam->s_mutex); |
| 1116 | return ret; |
| 1117 | } |
| 1118 | |
| 1119 | |
| 1120 | static int mcam_vidioc_g_ctrl(struct file *filp, void *priv, |
| 1121 | struct v4l2_control *ctrl) |
| 1122 | { |
| 1123 | struct mcam_camera *cam = priv; |
| 1124 | int ret; |
| 1125 | |
| 1126 | mutex_lock(&cam->s_mutex); |
| 1127 | ret = sensor_call(cam, core, g_ctrl, ctrl); |
| 1128 | mutex_unlock(&cam->s_mutex); |
| 1129 | return ret; |
| 1130 | } |
| 1131 | |
| 1132 | |
| 1133 | static int mcam_vidioc_s_ctrl(struct file *filp, void *priv, |
| 1134 | struct v4l2_control *ctrl) |
| 1135 | { |
| 1136 | struct mcam_camera *cam = priv; |
| 1137 | int ret; |
| 1138 | |
| 1139 | mutex_lock(&cam->s_mutex); |
| 1140 | ret = sensor_call(cam, core, s_ctrl, ctrl); |
| 1141 | mutex_unlock(&cam->s_mutex); |
| 1142 | return ret; |
| 1143 | } |
| 1144 | |
| 1145 | |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 1146 | static int mcam_vidioc_querycap(struct file *file, void *priv, |
| 1147 | struct v4l2_capability *cap) |
| 1148 | { |
| 1149 | strcpy(cap->driver, "marvell_ccic"); |
| 1150 | strcpy(cap->card, "marvell_ccic"); |
| 1151 | cap->version = 1; |
| 1152 | cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | |
| 1153 | V4L2_CAP_READWRITE | V4L2_CAP_STREAMING; |
| 1154 | return 0; |
| 1155 | } |
| 1156 | |
| 1157 | |
| 1158 | /* |
| 1159 | * The default format we use until somebody says otherwise. |
| 1160 | */ |
| 1161 | static const struct v4l2_pix_format mcam_def_pix_format = { |
| 1162 | .width = VGA_WIDTH, |
| 1163 | .height = VGA_HEIGHT, |
| 1164 | .pixelformat = V4L2_PIX_FMT_YUYV, |
| 1165 | .field = V4L2_FIELD_NONE, |
| 1166 | .bytesperline = VGA_WIDTH*2, |
| 1167 | .sizeimage = VGA_WIDTH*VGA_HEIGHT*2, |
| 1168 | }; |
| 1169 | |
| 1170 | static const enum v4l2_mbus_pixelcode mcam_def_mbus_code = |
| 1171 | V4L2_MBUS_FMT_YUYV8_2X8; |
| 1172 | |
| 1173 | static int mcam_vidioc_enum_fmt_vid_cap(struct file *filp, |
| 1174 | void *priv, struct v4l2_fmtdesc *fmt) |
| 1175 | { |
| 1176 | if (fmt->index >= N_MCAM_FMTS) |
| 1177 | return -EINVAL; |
| 1178 | strlcpy(fmt->description, mcam_formats[fmt->index].desc, |
| 1179 | sizeof(fmt->description)); |
| 1180 | fmt->pixelformat = mcam_formats[fmt->index].pixelformat; |
| 1181 | return 0; |
| 1182 | } |
| 1183 | |
| 1184 | static int mcam_vidioc_try_fmt_vid_cap(struct file *filp, void *priv, |
| 1185 | struct v4l2_format *fmt) |
| 1186 | { |
| 1187 | struct mcam_camera *cam = priv; |
| 1188 | struct mcam_format_struct *f; |
| 1189 | struct v4l2_pix_format *pix = &fmt->fmt.pix; |
| 1190 | struct v4l2_mbus_framefmt mbus_fmt; |
| 1191 | int ret; |
| 1192 | |
| 1193 | f = mcam_find_format(pix->pixelformat); |
| 1194 | pix->pixelformat = f->pixelformat; |
| 1195 | v4l2_fill_mbus_format(&mbus_fmt, pix, f->mbus_code); |
| 1196 | mutex_lock(&cam->s_mutex); |
| 1197 | ret = sensor_call(cam, video, try_mbus_fmt, &mbus_fmt); |
| 1198 | mutex_unlock(&cam->s_mutex); |
| 1199 | v4l2_fill_pix_format(pix, &mbus_fmt); |
| 1200 | pix->bytesperline = pix->width * f->bpp; |
| 1201 | pix->sizeimage = pix->height * pix->bytesperline; |
| 1202 | return ret; |
| 1203 | } |
| 1204 | |
| 1205 | static int mcam_vidioc_s_fmt_vid_cap(struct file *filp, void *priv, |
| 1206 | struct v4l2_format *fmt) |
| 1207 | { |
| 1208 | struct mcam_camera *cam = priv; |
| 1209 | struct mcam_format_struct *f; |
| 1210 | int ret; |
| 1211 | |
| 1212 | /* |
| 1213 | * Can't do anything if the device is not idle |
| 1214 | * Also can't if there are streaming buffers in place. |
| 1215 | */ |
Jonathan Corbet | b5210fd | 2011-06-20 16:14:36 -0300 | [diff] [blame] | 1216 | if (cam->state != S_IDLE || cam->vb_queue.num_buffers > 0) |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 1217 | return -EBUSY; |
| 1218 | |
| 1219 | f = mcam_find_format(fmt->fmt.pix.pixelformat); |
| 1220 | |
| 1221 | /* |
| 1222 | * See if the formatting works in principle. |
| 1223 | */ |
| 1224 | ret = mcam_vidioc_try_fmt_vid_cap(filp, priv, fmt); |
| 1225 | if (ret) |
| 1226 | return ret; |
| 1227 | /* |
| 1228 | * Now we start to change things for real, so let's do it |
| 1229 | * under lock. |
| 1230 | */ |
| 1231 | mutex_lock(&cam->s_mutex); |
| 1232 | cam->pix_format = fmt->fmt.pix; |
| 1233 | cam->mbus_code = f->mbus_code; |
| 1234 | |
| 1235 | /* |
| 1236 | * Make sure we have appropriate DMA buffers. |
| 1237 | */ |
| 1238 | ret = -ENOMEM; |
Jonathan Corbet | a9b36e8 | 2011-06-20 16:14:40 -0300 | [diff] [blame] | 1239 | if (cam->buffer_mode == B_vmalloc) { |
| 1240 | if (cam->nbufs > 0 && |
| 1241 | cam->dma_buf_size < cam->pix_format.sizeimage) |
| 1242 | mcam_free_dma_bufs(cam); |
| 1243 | if (cam->nbufs == 0) { |
| 1244 | if (mcam_alloc_dma_bufs(cam, 0)) |
| 1245 | goto out; |
| 1246 | } |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 1247 | } |
Jonathan Corbet | a9b36e8 | 2011-06-20 16:14:40 -0300 | [diff] [blame] | 1248 | mcam_set_config_needed(cam, 1); |
| 1249 | ret = 0; |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 1250 | out: |
| 1251 | mutex_unlock(&cam->s_mutex); |
| 1252 | return ret; |
| 1253 | } |
| 1254 | |
| 1255 | /* |
| 1256 | * Return our stored notion of how the camera is/should be configured. |
| 1257 | * The V4l2 spec wants us to be smarter, and actually get this from |
| 1258 | * the camera (and not mess with it at open time). Someday. |
| 1259 | */ |
| 1260 | static int mcam_vidioc_g_fmt_vid_cap(struct file *filp, void *priv, |
| 1261 | struct v4l2_format *f) |
| 1262 | { |
| 1263 | struct mcam_camera *cam = priv; |
| 1264 | |
| 1265 | f->fmt.pix = cam->pix_format; |
| 1266 | return 0; |
| 1267 | } |
| 1268 | |
| 1269 | /* |
| 1270 | * We only have one input - the sensor - so minimize the nonsense here. |
| 1271 | */ |
| 1272 | static int mcam_vidioc_enum_input(struct file *filp, void *priv, |
| 1273 | struct v4l2_input *input) |
| 1274 | { |
| 1275 | if (input->index != 0) |
| 1276 | return -EINVAL; |
| 1277 | |
| 1278 | input->type = V4L2_INPUT_TYPE_CAMERA; |
| 1279 | input->std = V4L2_STD_ALL; /* Not sure what should go here */ |
| 1280 | strcpy(input->name, "Camera"); |
| 1281 | return 0; |
| 1282 | } |
| 1283 | |
| 1284 | static int mcam_vidioc_g_input(struct file *filp, void *priv, unsigned int *i) |
| 1285 | { |
| 1286 | *i = 0; |
| 1287 | return 0; |
| 1288 | } |
| 1289 | |
| 1290 | static int mcam_vidioc_s_input(struct file *filp, void *priv, unsigned int i) |
| 1291 | { |
| 1292 | if (i != 0) |
| 1293 | return -EINVAL; |
| 1294 | return 0; |
| 1295 | } |
| 1296 | |
| 1297 | /* from vivi.c */ |
| 1298 | static int mcam_vidioc_s_std(struct file *filp, void *priv, v4l2_std_id *a) |
| 1299 | { |
| 1300 | return 0; |
| 1301 | } |
| 1302 | |
| 1303 | /* |
| 1304 | * G/S_PARM. Most of this is done by the sensor, but we are |
| 1305 | * the level which controls the number of read buffers. |
| 1306 | */ |
| 1307 | static int mcam_vidioc_g_parm(struct file *filp, void *priv, |
| 1308 | struct v4l2_streamparm *parms) |
| 1309 | { |
| 1310 | struct mcam_camera *cam = priv; |
| 1311 | int ret; |
| 1312 | |
| 1313 | mutex_lock(&cam->s_mutex); |
| 1314 | ret = sensor_call(cam, video, g_parm, parms); |
| 1315 | mutex_unlock(&cam->s_mutex); |
| 1316 | parms->parm.capture.readbuffers = n_dma_bufs; |
| 1317 | return ret; |
| 1318 | } |
| 1319 | |
| 1320 | static int mcam_vidioc_s_parm(struct file *filp, void *priv, |
| 1321 | struct v4l2_streamparm *parms) |
| 1322 | { |
| 1323 | struct mcam_camera *cam = priv; |
| 1324 | int ret; |
| 1325 | |
| 1326 | mutex_lock(&cam->s_mutex); |
| 1327 | ret = sensor_call(cam, video, s_parm, parms); |
| 1328 | mutex_unlock(&cam->s_mutex); |
| 1329 | parms->parm.capture.readbuffers = n_dma_bufs; |
| 1330 | return ret; |
| 1331 | } |
| 1332 | |
| 1333 | static int mcam_vidioc_g_chip_ident(struct file *file, void *priv, |
| 1334 | struct v4l2_dbg_chip_ident *chip) |
| 1335 | { |
| 1336 | struct mcam_camera *cam = priv; |
| 1337 | |
| 1338 | chip->ident = V4L2_IDENT_NONE; |
| 1339 | chip->revision = 0; |
| 1340 | if (v4l2_chip_match_host(&chip->match)) { |
| 1341 | chip->ident = cam->chip_id; |
| 1342 | return 0; |
| 1343 | } |
| 1344 | return sensor_call(cam, core, g_chip_ident, chip); |
| 1345 | } |
| 1346 | |
| 1347 | static int mcam_vidioc_enum_framesizes(struct file *filp, void *priv, |
| 1348 | struct v4l2_frmsizeenum *sizes) |
| 1349 | { |
| 1350 | struct mcam_camera *cam = priv; |
| 1351 | int ret; |
| 1352 | |
| 1353 | mutex_lock(&cam->s_mutex); |
| 1354 | ret = sensor_call(cam, video, enum_framesizes, sizes); |
| 1355 | mutex_unlock(&cam->s_mutex); |
| 1356 | return ret; |
| 1357 | } |
| 1358 | |
| 1359 | static int mcam_vidioc_enum_frameintervals(struct file *filp, void *priv, |
| 1360 | struct v4l2_frmivalenum *interval) |
| 1361 | { |
| 1362 | struct mcam_camera *cam = priv; |
| 1363 | int ret; |
| 1364 | |
| 1365 | mutex_lock(&cam->s_mutex); |
| 1366 | ret = sensor_call(cam, video, enum_frameintervals, interval); |
| 1367 | mutex_unlock(&cam->s_mutex); |
| 1368 | return ret; |
| 1369 | } |
| 1370 | |
| 1371 | #ifdef CONFIG_VIDEO_ADV_DEBUG |
| 1372 | static int mcam_vidioc_g_register(struct file *file, void *priv, |
| 1373 | struct v4l2_dbg_register *reg) |
| 1374 | { |
| 1375 | struct mcam_camera *cam = priv; |
| 1376 | |
| 1377 | if (v4l2_chip_match_host(®->match)) { |
| 1378 | reg->val = mcam_reg_read(cam, reg->reg); |
| 1379 | reg->size = 4; |
| 1380 | return 0; |
| 1381 | } |
| 1382 | return sensor_call(cam, core, g_register, reg); |
| 1383 | } |
| 1384 | |
| 1385 | static int mcam_vidioc_s_register(struct file *file, void *priv, |
| 1386 | struct v4l2_dbg_register *reg) |
| 1387 | { |
| 1388 | struct mcam_camera *cam = priv; |
| 1389 | |
| 1390 | if (v4l2_chip_match_host(®->match)) { |
| 1391 | mcam_reg_write(cam, reg->reg, reg->val); |
| 1392 | return 0; |
| 1393 | } |
| 1394 | return sensor_call(cam, core, s_register, reg); |
| 1395 | } |
| 1396 | #endif |
| 1397 | |
| 1398 | /* |
| 1399 | * This template device holds all of those v4l2 methods; we |
| 1400 | * clone it for specific real devices. |
| 1401 | */ |
| 1402 | |
| 1403 | static const struct v4l2_file_operations mcam_v4l_fops = { |
| 1404 | .owner = THIS_MODULE, |
| 1405 | .open = mcam_v4l_open, |
| 1406 | .release = mcam_v4l_release, |
| 1407 | .read = mcam_v4l_read, |
| 1408 | .poll = mcam_v4l_poll, |
| 1409 | .mmap = mcam_v4l_mmap, |
| 1410 | .unlocked_ioctl = video_ioctl2, |
| 1411 | }; |
| 1412 | |
| 1413 | static const struct v4l2_ioctl_ops mcam_v4l_ioctl_ops = { |
| 1414 | .vidioc_querycap = mcam_vidioc_querycap, |
| 1415 | .vidioc_enum_fmt_vid_cap = mcam_vidioc_enum_fmt_vid_cap, |
| 1416 | .vidioc_try_fmt_vid_cap = mcam_vidioc_try_fmt_vid_cap, |
| 1417 | .vidioc_s_fmt_vid_cap = mcam_vidioc_s_fmt_vid_cap, |
| 1418 | .vidioc_g_fmt_vid_cap = mcam_vidioc_g_fmt_vid_cap, |
| 1419 | .vidioc_enum_input = mcam_vidioc_enum_input, |
| 1420 | .vidioc_g_input = mcam_vidioc_g_input, |
| 1421 | .vidioc_s_input = mcam_vidioc_s_input, |
| 1422 | .vidioc_s_std = mcam_vidioc_s_std, |
| 1423 | .vidioc_reqbufs = mcam_vidioc_reqbufs, |
| 1424 | .vidioc_querybuf = mcam_vidioc_querybuf, |
| 1425 | .vidioc_qbuf = mcam_vidioc_qbuf, |
| 1426 | .vidioc_dqbuf = mcam_vidioc_dqbuf, |
| 1427 | .vidioc_streamon = mcam_vidioc_streamon, |
| 1428 | .vidioc_streamoff = mcam_vidioc_streamoff, |
| 1429 | .vidioc_queryctrl = mcam_vidioc_queryctrl, |
| 1430 | .vidioc_g_ctrl = mcam_vidioc_g_ctrl, |
| 1431 | .vidioc_s_ctrl = mcam_vidioc_s_ctrl, |
| 1432 | .vidioc_g_parm = mcam_vidioc_g_parm, |
| 1433 | .vidioc_s_parm = mcam_vidioc_s_parm, |
| 1434 | .vidioc_enum_framesizes = mcam_vidioc_enum_framesizes, |
| 1435 | .vidioc_enum_frameintervals = mcam_vidioc_enum_frameintervals, |
| 1436 | .vidioc_g_chip_ident = mcam_vidioc_g_chip_ident, |
| 1437 | #ifdef CONFIG_VIDEO_ADV_DEBUG |
| 1438 | .vidioc_g_register = mcam_vidioc_g_register, |
| 1439 | .vidioc_s_register = mcam_vidioc_s_register, |
| 1440 | #endif |
| 1441 | }; |
| 1442 | |
| 1443 | static struct video_device mcam_v4l_template = { |
| 1444 | .name = "mcam", |
| 1445 | .tvnorms = V4L2_STD_NTSC_M, |
| 1446 | .current_norm = V4L2_STD_NTSC_M, /* make mplayer happy */ |
| 1447 | |
| 1448 | .fops = &mcam_v4l_fops, |
| 1449 | .ioctl_ops = &mcam_v4l_ioctl_ops, |
| 1450 | .release = video_device_release_empty, |
| 1451 | }; |
| 1452 | |
| 1453 | /* ---------------------------------------------------------------------- */ |
| 1454 | /* |
| 1455 | * Interrupt handler stuff |
| 1456 | */ |
| 1457 | |
| 1458 | |
Jonathan Corbet | a9b36e8 | 2011-06-20 16:14:40 -0300 | [diff] [blame] | 1459 | static void mcam_buffer_done(struct mcam_camera *cam, int frame, |
| 1460 | struct vb2_buffer *vbuf) |
| 1461 | { |
| 1462 | vbuf->v4l2_buf.bytesused = cam->pix_format.sizeimage; |
| 1463 | vbuf->v4l2_buf.sequence = cam->buf_seq[frame]; |
Jonathan Corbet | a9b36e8 | 2011-06-20 16:14:40 -0300 | [diff] [blame] | 1464 | vb2_set_plane_payload(vbuf, 0, cam->pix_format.sizeimage); |
| 1465 | vb2_buffer_done(vbuf, VB2_BUF_STATE_DONE); |
| 1466 | } |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 1467 | |
Jonathan Corbet | a9b36e8 | 2011-06-20 16:14:40 -0300 | [diff] [blame] | 1468 | /* |
| 1469 | * Copy data out to user space in the vmalloc case |
| 1470 | */ |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 1471 | static void mcam_frame_tasklet(unsigned long data) |
| 1472 | { |
| 1473 | struct mcam_camera *cam = (struct mcam_camera *) data; |
| 1474 | int i; |
| 1475 | unsigned long flags; |
Jonathan Corbet | b5210fd | 2011-06-20 16:14:36 -0300 | [diff] [blame] | 1476 | struct mcam_vb_buffer *buf; |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 1477 | |
| 1478 | spin_lock_irqsave(&cam->dev_lock, flags); |
| 1479 | for (i = 0; i < cam->nbufs; i++) { |
| 1480 | int bufno = cam->next_buf; |
Jonathan Corbet | b5210fd | 2011-06-20 16:14:36 -0300 | [diff] [blame] | 1481 | |
| 1482 | if (cam->state != S_STREAMING || bufno < 0) |
| 1483 | break; /* I/O got stopped */ |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 1484 | if (++(cam->next_buf) >= cam->nbufs) |
| 1485 | cam->next_buf = 0; |
| 1486 | if (!test_bit(bufno, &cam->flags)) |
| 1487 | continue; |
Jonathan Corbet | a9b36e8 | 2011-06-20 16:14:40 -0300 | [diff] [blame] | 1488 | if (list_empty(&cam->buffers)) { |
| 1489 | singles++; |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 1490 | break; /* Leave it valid, hope for better later */ |
Jonathan Corbet | a9b36e8 | 2011-06-20 16:14:40 -0300 | [diff] [blame] | 1491 | } |
| 1492 | delivered++; |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 1493 | clear_bit(bufno, &cam->flags); |
Jonathan Corbet | b5210fd | 2011-06-20 16:14:36 -0300 | [diff] [blame] | 1494 | buf = list_first_entry(&cam->buffers, struct mcam_vb_buffer, |
| 1495 | queue); |
| 1496 | list_del_init(&buf->queue); |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 1497 | /* |
| 1498 | * Drop the lock during the big copy. This *should* be safe... |
| 1499 | */ |
| 1500 | spin_unlock_irqrestore(&cam->dev_lock, flags); |
Jonathan Corbet | b5210fd | 2011-06-20 16:14:36 -0300 | [diff] [blame] | 1501 | memcpy(vb2_plane_vaddr(&buf->vb_buf, 0), cam->dma_bufs[bufno], |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 1502 | cam->pix_format.sizeimage); |
Jonathan Corbet | a9b36e8 | 2011-06-20 16:14:40 -0300 | [diff] [blame] | 1503 | mcam_buffer_done(cam, bufno, &buf->vb_buf); |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 1504 | spin_lock_irqsave(&cam->dev_lock, flags); |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 1505 | } |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 1506 | spin_unlock_irqrestore(&cam->dev_lock, flags); |
| 1507 | } |
| 1508 | |
Jonathan Corbet | a9b36e8 | 2011-06-20 16:14:40 -0300 | [diff] [blame] | 1509 | /* |
| 1510 | * For direct DMA, mark the buffer ready and set up another one. |
| 1511 | */ |
Jonathan Corbet | cbc4f3a | 2011-06-30 17:05:27 -0300 | [diff] [blame^] | 1512 | static void mcam_dma_contig_done(struct mcam_camera *cam, int frame) |
Jonathan Corbet | a9b36e8 | 2011-06-20 16:14:40 -0300 | [diff] [blame] | 1513 | { |
| 1514 | struct mcam_vb_buffer *buf = cam->vb_bufs[frame]; |
| 1515 | |
| 1516 | if (!test_bit(CF_SINGLE_BUFFER, &cam->flags)) { |
| 1517 | delivered++; |
| 1518 | mcam_buffer_done(cam, frame, &buf->vb_buf); |
| 1519 | } |
| 1520 | mcam_set_contig_buffer(cam, frame); |
| 1521 | } |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 1522 | |
Jonathan Corbet | cbc4f3a | 2011-06-30 17:05:27 -0300 | [diff] [blame^] | 1523 | /* |
| 1524 | * Frame completion with S/G is trickier. We can't muck with |
| 1525 | * a descriptor chain on the fly, since the controller buffers it |
| 1526 | * internally. So we have to actually stop and restart; Marvell |
| 1527 | * says this is the way to do it. |
| 1528 | * |
| 1529 | * Of course, stopping is easier said than done; experience shows |
| 1530 | * that the controller can start a frame *after* C0_ENABLE has been |
| 1531 | * cleared. So when running in S/G mode, the controller is "stopped" |
| 1532 | * on receipt of the start-of-frame interrupt. That means we can |
| 1533 | * safely change the DMA descriptor array here and restart things |
| 1534 | * (assuming there's another buffer waiting to go). |
| 1535 | */ |
| 1536 | static void mcam_dma_sg_done(struct mcam_camera *cam, int frame) |
| 1537 | { |
| 1538 | struct mcam_vb_buffer *buf = cam->vb_bufs[0]; |
| 1539 | |
| 1540 | /* |
| 1541 | * Very Bad Not Good Things happen if you don't clear |
| 1542 | * C1_DESC_ENA before making any descriptor changes. |
| 1543 | */ |
| 1544 | mcam_reg_clear_bit(cam, REG_CTRL1, C1_DESC_ENA); |
| 1545 | /* |
| 1546 | * If we have another buffer available, put it in and |
| 1547 | * restart the engine. |
| 1548 | */ |
| 1549 | if (!list_empty(&cam->buffers)) { |
| 1550 | mcam_sg_next_buffer(cam); |
| 1551 | mcam_reg_set_bit(cam, REG_CTRL1, C1_DESC_ENA); |
| 1552 | mcam_ctlr_start(cam); |
| 1553 | /* |
| 1554 | * Otherwise set CF_SG_RESTART and the controller will |
| 1555 | * be restarted once another buffer shows up. |
| 1556 | */ |
| 1557 | } else { |
| 1558 | set_bit(CF_SG_RESTART, &cam->flags); |
| 1559 | singles++; |
| 1560 | } |
| 1561 | /* |
| 1562 | * Now we can give the completed frame back to user space. |
| 1563 | */ |
| 1564 | delivered++; |
| 1565 | mcam_buffer_done(cam, frame, &buf->vb_buf); |
| 1566 | } |
| 1567 | |
| 1568 | |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 1569 | |
| 1570 | static void mcam_frame_complete(struct mcam_camera *cam, int frame) |
| 1571 | { |
| 1572 | /* |
| 1573 | * Basic frame housekeeping. |
| 1574 | */ |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 1575 | set_bit(frame, &cam->flags); |
| 1576 | clear_bit(CF_DMA_ACTIVE, &cam->flags); |
Jonathan Corbet | a9b36e8 | 2011-06-20 16:14:40 -0300 | [diff] [blame] | 1577 | cam->next_buf = frame; |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 1578 | cam->buf_seq[frame] = ++(cam->sequence); |
Jonathan Corbet | a9b36e8 | 2011-06-20 16:14:40 -0300 | [diff] [blame] | 1579 | cam->last_delivered = frame; |
Jonathan Corbet | a9b36e8 | 2011-06-20 16:14:40 -0300 | [diff] [blame] | 1580 | frames++; |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 1581 | /* |
Jonathan Corbet | cbc4f3a | 2011-06-30 17:05:27 -0300 | [diff] [blame^] | 1582 | * "This should never happen" |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 1583 | */ |
Jonathan Corbet | cbc4f3a | 2011-06-30 17:05:27 -0300 | [diff] [blame^] | 1584 | if (cam->state != S_STREAMING) |
| 1585 | return; |
| 1586 | /* |
| 1587 | * Process the frame and set up the next one. |
| 1588 | */ |
| 1589 | switch (cam->buffer_mode) { |
| 1590 | case B_vmalloc: |
| 1591 | tasklet_schedule(&cam->s_tasklet); |
| 1592 | break; |
| 1593 | case B_DMA_contig: |
| 1594 | mcam_dma_contig_done(cam, frame); |
| 1595 | break; |
| 1596 | case B_DMA_sg: |
| 1597 | mcam_dma_sg_done(cam, frame); |
| 1598 | break; |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 1599 | } |
| 1600 | } |
| 1601 | |
| 1602 | |
| 1603 | |
| 1604 | |
| 1605 | int mccic_irq(struct mcam_camera *cam, unsigned int irqs) |
| 1606 | { |
| 1607 | unsigned int frame, handled = 0; |
| 1608 | |
| 1609 | mcam_reg_write(cam, REG_IRQSTAT, FRAMEIRQS); /* Clear'em all */ |
| 1610 | /* |
| 1611 | * Handle any frame completions. There really should |
| 1612 | * not be more than one of these, or we have fallen |
| 1613 | * far behind. |
Jonathan Corbet | cbc4f3a | 2011-06-30 17:05:27 -0300 | [diff] [blame^] | 1614 | * |
| 1615 | * When running in S/G mode, the frame number lacks any |
| 1616 | * real meaning - there's only one descriptor array - but |
| 1617 | * the controller still picks a different one to signal |
| 1618 | * each time. |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 1619 | */ |
| 1620 | for (frame = 0; frame < cam->nbufs; frame++) |
| 1621 | if (irqs & (IRQ_EOF0 << frame)) { |
| 1622 | mcam_frame_complete(cam, frame); |
| 1623 | handled = 1; |
| 1624 | } |
| 1625 | /* |
| 1626 | * If a frame starts, note that we have DMA active. This |
| 1627 | * code assumes that we won't get multiple frame interrupts |
| 1628 | * at once; may want to rethink that. |
| 1629 | */ |
| 1630 | if (irqs & (IRQ_SOF0 | IRQ_SOF1 | IRQ_SOF2)) { |
| 1631 | set_bit(CF_DMA_ACTIVE, &cam->flags); |
| 1632 | handled = 1; |
Jonathan Corbet | cbc4f3a | 2011-06-30 17:05:27 -0300 | [diff] [blame^] | 1633 | if (cam->buffer_mode == B_DMA_sg) |
| 1634 | mcam_ctlr_stop(cam); |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 1635 | } |
| 1636 | return handled; |
| 1637 | } |
| 1638 | |
| 1639 | /* |
| 1640 | * Registration and such. |
| 1641 | */ |
| 1642 | |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 1643 | static struct ov7670_config sensor_cfg = { |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 1644 | /* |
| 1645 | * Exclude QCIF mode, because it only captures a tiny portion |
| 1646 | * of the sensor FOV |
| 1647 | */ |
| 1648 | .min_width = 320, |
| 1649 | .min_height = 240, |
| 1650 | }; |
| 1651 | |
| 1652 | |
| 1653 | int mccic_register(struct mcam_camera *cam) |
| 1654 | { |
| 1655 | struct i2c_board_info ov7670_info = { |
| 1656 | .type = "ov7670", |
Jonathan Corbet | 1c68f88 | 2011-06-11 14:46:47 -0300 | [diff] [blame] | 1657 | .addr = 0x42 >> 1, |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 1658 | .platform_data = &sensor_cfg, |
| 1659 | }; |
| 1660 | int ret; |
| 1661 | |
| 1662 | /* |
| 1663 | * Register with V4L |
| 1664 | */ |
| 1665 | ret = v4l2_device_register(cam->dev, &cam->v4l2_dev); |
| 1666 | if (ret) |
| 1667 | return ret; |
| 1668 | |
| 1669 | mutex_init(&cam->s_mutex); |
| 1670 | cam->state = S_NOTREADY; |
| 1671 | mcam_set_config_needed(cam, 1); |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 1672 | cam->pix_format = mcam_def_pix_format; |
| 1673 | cam->mbus_code = mcam_def_mbus_code; |
| 1674 | INIT_LIST_HEAD(&cam->dev_list); |
Jonathan Corbet | b5210fd | 2011-06-20 16:14:36 -0300 | [diff] [blame] | 1675 | INIT_LIST_HEAD(&cam->buffers); |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 1676 | tasklet_init(&cam->s_tasklet, mcam_frame_tasklet, (unsigned long) cam); |
Jonathan Corbet | a9b36e8 | 2011-06-20 16:14:40 -0300 | [diff] [blame] | 1677 | /* |
| 1678 | * User space may want to override the asked-for buffer mode; |
| 1679 | * here's hoping they know what they're doing. |
| 1680 | */ |
| 1681 | if (buffer_mode == 0) |
| 1682 | cam->buffer_mode = B_vmalloc; |
| 1683 | else if (buffer_mode == 1) |
| 1684 | cam->buffer_mode = B_DMA_contig; |
Jonathan Corbet | cbc4f3a | 2011-06-30 17:05:27 -0300 | [diff] [blame^] | 1685 | else if (buffer_mode == 2) { |
| 1686 | if (cam->chip_id == V4L2_IDENT_ARMADA610) |
| 1687 | cam->buffer_mode = B_DMA_sg; |
| 1688 | else { |
| 1689 | printk(KERN_ERR "marvell-cam: Cafe can't do S/G I/O\n"); |
| 1690 | cam->buffer_mode = B_vmalloc; |
| 1691 | } |
| 1692 | } else if (buffer_mode != -1) |
| 1693 | printk(KERN_ERR "marvell-cam: " |
Jonathan Corbet | a9b36e8 | 2011-06-20 16:14:40 -0300 | [diff] [blame] | 1694 | "Strange module buffer mode %d - ignoring\n", |
| 1695 | buffer_mode); |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 1696 | mcam_ctlr_init(cam); |
| 1697 | |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 1698 | /* |
| 1699 | * Try to find the sensor. |
| 1700 | */ |
Jonathan Corbet | 2164b5a | 2011-06-11 14:46:44 -0300 | [diff] [blame] | 1701 | sensor_cfg.clock_speed = cam->clock_speed; |
| 1702 | sensor_cfg.use_smbus = cam->use_smbus; |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 1703 | cam->sensor_addr = ov7670_info.addr; |
| 1704 | cam->sensor = v4l2_i2c_new_subdev_board(&cam->v4l2_dev, |
Jonathan Corbet | 595a93a | 2011-06-11 14:46:48 -0300 | [diff] [blame] | 1705 | cam->i2c_adapter, &ov7670_info, NULL); |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 1706 | if (cam->sensor == NULL) { |
| 1707 | ret = -ENODEV; |
| 1708 | goto out_unregister; |
| 1709 | } |
| 1710 | |
| 1711 | ret = mcam_cam_init(cam); |
| 1712 | if (ret) |
| 1713 | goto out_unregister; |
| 1714 | /* |
| 1715 | * Get the v4l2 setup done. |
| 1716 | */ |
| 1717 | mutex_lock(&cam->s_mutex); |
| 1718 | cam->vdev = mcam_v4l_template; |
| 1719 | cam->vdev.debug = 0; |
| 1720 | cam->vdev.v4l2_dev = &cam->v4l2_dev; |
| 1721 | ret = video_register_device(&cam->vdev, VFL_TYPE_GRABBER, -1); |
| 1722 | if (ret) |
| 1723 | goto out; |
| 1724 | video_set_drvdata(&cam->vdev, cam); |
| 1725 | |
| 1726 | /* |
| 1727 | * If so requested, try to get our DMA buffers now. |
| 1728 | */ |
Jonathan Corbet | a9b36e8 | 2011-06-20 16:14:40 -0300 | [diff] [blame] | 1729 | if (cam->buffer_mode == B_vmalloc && !alloc_bufs_at_read) { |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 1730 | if (mcam_alloc_dma_bufs(cam, 1)) |
| 1731 | cam_warn(cam, "Unable to alloc DMA buffers at load" |
| 1732 | " will try again later."); |
| 1733 | } |
| 1734 | |
| 1735 | out: |
| 1736 | mutex_unlock(&cam->s_mutex); |
| 1737 | return ret; |
| 1738 | out_unregister: |
| 1739 | v4l2_device_unregister(&cam->v4l2_dev); |
| 1740 | return ret; |
| 1741 | } |
| 1742 | |
| 1743 | |
| 1744 | void mccic_shutdown(struct mcam_camera *cam) |
| 1745 | { |
Jonathan Corbet | 67a8dbb | 2011-06-11 14:46:49 -0300 | [diff] [blame] | 1746 | /* |
| 1747 | * If we have no users (and we really, really should have no |
| 1748 | * users) the device will already be powered down. Trying to |
| 1749 | * take it down again will wedge the machine, which is frowned |
| 1750 | * upon. |
| 1751 | */ |
| 1752 | if (cam->users > 0) { |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 1753 | cam_warn(cam, "Removing a device with users!\n"); |
Jonathan Corbet | 67a8dbb | 2011-06-11 14:46:49 -0300 | [diff] [blame] | 1754 | mcam_ctlr_power_down(cam); |
| 1755 | } |
Jonathan Corbet | b5210fd | 2011-06-20 16:14:36 -0300 | [diff] [blame] | 1756 | vb2_queue_release(&cam->vb_queue); |
Jonathan Corbet | a9b36e8 | 2011-06-20 16:14:40 -0300 | [diff] [blame] | 1757 | if (cam->buffer_mode == B_vmalloc) |
| 1758 | mcam_free_dma_bufs(cam); |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 1759 | video_unregister_device(&cam->vdev); |
| 1760 | v4l2_device_unregister(&cam->v4l2_dev); |
| 1761 | } |
| 1762 | |
| 1763 | /* |
| 1764 | * Power management |
| 1765 | */ |
| 1766 | #ifdef CONFIG_PM |
| 1767 | |
| 1768 | void mccic_suspend(struct mcam_camera *cam) |
| 1769 | { |
| 1770 | enum mcam_state cstate = cam->state; |
| 1771 | |
| 1772 | mcam_ctlr_stop_dma(cam); |
| 1773 | mcam_ctlr_power_down(cam); |
| 1774 | cam->state = cstate; |
| 1775 | } |
| 1776 | |
| 1777 | int mccic_resume(struct mcam_camera *cam) |
| 1778 | { |
| 1779 | int ret = 0; |
| 1780 | |
| 1781 | mutex_lock(&cam->s_mutex); |
| 1782 | if (cam->users > 0) { |
| 1783 | mcam_ctlr_power_up(cam); |
| 1784 | __mcam_cam_reset(cam); |
| 1785 | } else { |
| 1786 | mcam_ctlr_power_down(cam); |
| 1787 | } |
| 1788 | mutex_unlock(&cam->s_mutex); |
| 1789 | |
| 1790 | set_bit(CF_CONFIG_NEEDED, &cam->flags); |
Jonathan Corbet | b5210fd | 2011-06-20 16:14:36 -0300 | [diff] [blame] | 1791 | if (cam->state == S_STREAMING) |
Jonathan Corbet | a9b36e8 | 2011-06-20 16:14:40 -0300 | [diff] [blame] | 1792 | ret = mcam_read_setup(cam); |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 1793 | return ret; |
| 1794 | } |
| 1795 | #endif /* CONFIG_PM */ |