Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2011 Atmel Corporation |
| 3 | * Josh Wu, <josh.wu@atmel.com> |
| 4 | * |
| 5 | * Based on previous work by Lars Haring, <lars.haring@atmel.com> |
| 6 | * and Sedji Gaouaou |
| 7 | * Based on the bttv driver for Bt848 with respective copyright holders |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License version 2 as |
| 11 | * published by the Free Software Foundation. |
| 12 | */ |
| 13 | |
| 14 | #include <linux/clk.h> |
| 15 | #include <linux/completion.h> |
| 16 | #include <linux/delay.h> |
| 17 | #include <linux/fs.h> |
| 18 | #include <linux/init.h> |
| 19 | #include <linux/interrupt.h> |
| 20 | #include <linux/kernel.h> |
| 21 | #include <linux/module.h> |
| 22 | #include <linux/platform_device.h> |
| 23 | #include <linux/slab.h> |
| 24 | |
| 25 | #include <media/atmel-isi.h> |
| 26 | #include <media/soc_camera.h> |
| 27 | #include <media/soc_mediabus.h> |
| 28 | #include <media/videobuf2-dma-contig.h> |
| 29 | |
| 30 | #define MAX_BUFFER_NUM 32 |
| 31 | #define MAX_SUPPORT_WIDTH 2048 |
| 32 | #define MAX_SUPPORT_HEIGHT 2048 |
| 33 | #define VID_LIMIT_BYTES (16 * 1024 * 1024) |
| 34 | #define MIN_FRAME_RATE 15 |
| 35 | #define FRAME_INTERVAL_MILLI_SEC (1000 / MIN_FRAME_RATE) |
| 36 | |
| 37 | /* ISI states */ |
| 38 | enum { |
| 39 | ISI_STATE_IDLE = 0, |
| 40 | ISI_STATE_READY, |
| 41 | ISI_STATE_WAIT_SOF, |
| 42 | }; |
| 43 | |
| 44 | /* Frame buffer descriptor */ |
| 45 | struct fbd { |
| 46 | /* Physical address of the frame buffer */ |
| 47 | u32 fb_address; |
| 48 | /* DMA Control Register(only in HISI2) */ |
| 49 | u32 dma_ctrl; |
| 50 | /* Physical address of the next fbd */ |
| 51 | u32 next_fbd_address; |
| 52 | }; |
| 53 | |
| 54 | static void set_dma_ctrl(struct fbd *fb_desc, u32 ctrl) |
| 55 | { |
| 56 | fb_desc->dma_ctrl = ctrl; |
| 57 | } |
| 58 | |
| 59 | struct isi_dma_desc { |
| 60 | struct list_head list; |
| 61 | struct fbd *p_fbd; |
| 62 | u32 fbd_phys; |
| 63 | }; |
| 64 | |
| 65 | /* Frame buffer data */ |
| 66 | struct frame_buffer { |
| 67 | struct vb2_buffer vb; |
| 68 | struct isi_dma_desc *p_dma_desc; |
| 69 | struct list_head list; |
| 70 | }; |
| 71 | |
| 72 | struct atmel_isi { |
| 73 | /* Protects the access of variables shared with the ISR */ |
| 74 | spinlock_t lock; |
| 75 | void __iomem *regs; |
| 76 | |
| 77 | int sequence; |
| 78 | /* State of the ISI module in capturing mode */ |
| 79 | int state; |
| 80 | |
| 81 | /* Wait queue for waiting for SOF */ |
| 82 | wait_queue_head_t vsync_wq; |
| 83 | |
| 84 | struct vb2_alloc_ctx *alloc_ctx; |
| 85 | |
| 86 | /* Allocate descriptors for dma buffer use */ |
| 87 | struct fbd *p_fb_descriptors; |
| 88 | u32 fb_descriptors_phys; |
| 89 | struct list_head dma_desc_head; |
| 90 | struct isi_dma_desc dma_desc[MAX_BUFFER_NUM]; |
| 91 | |
| 92 | struct completion complete; |
| 93 | struct clk *pclk; |
| 94 | unsigned int irq; |
| 95 | |
| 96 | struct isi_platform_data *pdata; |
Guennadi Liakhovetski | a4e9f10 | 2011-07-27 12:18:37 -0300 | [diff] [blame] | 97 | u16 width_flags; /* max 12 bits */ |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 98 | |
| 99 | struct list_head video_buffer_list; |
| 100 | struct frame_buffer *active; |
| 101 | |
| 102 | struct soc_camera_device *icd; |
| 103 | struct soc_camera_host soc_host; |
| 104 | }; |
| 105 | |
| 106 | static void isi_writel(struct atmel_isi *isi, u32 reg, u32 val) |
| 107 | { |
| 108 | writel(val, isi->regs + reg); |
| 109 | } |
| 110 | static u32 isi_readl(struct atmel_isi *isi, u32 reg) |
| 111 | { |
| 112 | return readl(isi->regs + reg); |
| 113 | } |
| 114 | |
| 115 | static int configure_geometry(struct atmel_isi *isi, u32 width, |
| 116 | u32 height, enum v4l2_mbus_pixelcode code) |
| 117 | { |
| 118 | u32 cfg2, cr; |
| 119 | |
| 120 | switch (code) { |
| 121 | /* YUV, including grey */ |
| 122 | case V4L2_MBUS_FMT_Y8_1X8: |
| 123 | cr = ISI_CFG2_GRAYSCALE; |
| 124 | break; |
| 125 | case V4L2_MBUS_FMT_UYVY8_2X8: |
| 126 | cr = ISI_CFG2_YCC_SWAP_MODE_3; |
| 127 | break; |
| 128 | case V4L2_MBUS_FMT_VYUY8_2X8: |
| 129 | cr = ISI_CFG2_YCC_SWAP_MODE_2; |
| 130 | break; |
| 131 | case V4L2_MBUS_FMT_YUYV8_2X8: |
| 132 | cr = ISI_CFG2_YCC_SWAP_MODE_1; |
| 133 | break; |
| 134 | case V4L2_MBUS_FMT_YVYU8_2X8: |
| 135 | cr = ISI_CFG2_YCC_SWAP_DEFAULT; |
| 136 | break; |
| 137 | /* RGB, TODO */ |
| 138 | default: |
| 139 | return -EINVAL; |
| 140 | } |
| 141 | |
| 142 | isi_writel(isi, ISI_CTRL, ISI_CTRL_DIS); |
| 143 | |
| 144 | cfg2 = isi_readl(isi, ISI_CFG2); |
| 145 | cfg2 |= cr; |
| 146 | /* Set width */ |
| 147 | cfg2 &= ~(ISI_CFG2_IM_HSIZE_MASK); |
| 148 | cfg2 |= ((width - 1) << ISI_CFG2_IM_HSIZE_OFFSET) & |
| 149 | ISI_CFG2_IM_HSIZE_MASK; |
| 150 | /* Set height */ |
| 151 | cfg2 &= ~(ISI_CFG2_IM_VSIZE_MASK); |
| 152 | cfg2 |= ((height - 1) << ISI_CFG2_IM_VSIZE_OFFSET) |
| 153 | & ISI_CFG2_IM_VSIZE_MASK; |
| 154 | isi_writel(isi, ISI_CFG2, cfg2); |
| 155 | |
| 156 | return 0; |
| 157 | } |
| 158 | |
| 159 | static irqreturn_t atmel_isi_handle_streaming(struct atmel_isi *isi) |
| 160 | { |
| 161 | if (isi->active) { |
| 162 | struct vb2_buffer *vb = &isi->active->vb; |
| 163 | struct frame_buffer *buf = isi->active; |
| 164 | |
| 165 | list_del_init(&buf->list); |
| 166 | do_gettimeofday(&vb->v4l2_buf.timestamp); |
| 167 | vb->v4l2_buf.sequence = isi->sequence++; |
| 168 | vb2_buffer_done(vb, VB2_BUF_STATE_DONE); |
| 169 | } |
| 170 | |
| 171 | if (list_empty(&isi->video_buffer_list)) { |
| 172 | isi->active = NULL; |
| 173 | } else { |
| 174 | /* start next dma frame. */ |
| 175 | isi->active = list_entry(isi->video_buffer_list.next, |
| 176 | struct frame_buffer, list); |
| 177 | isi_writel(isi, ISI_DMA_C_DSCR, |
| 178 | isi->active->p_dma_desc->fbd_phys); |
| 179 | isi_writel(isi, ISI_DMA_C_CTRL, |
| 180 | ISI_DMA_CTRL_FETCH | ISI_DMA_CTRL_DONE); |
| 181 | isi_writel(isi, ISI_DMA_CHER, ISI_DMA_CHSR_C_CH); |
| 182 | } |
| 183 | return IRQ_HANDLED; |
| 184 | } |
| 185 | |
| 186 | /* ISI interrupt service routine */ |
| 187 | static irqreturn_t isi_interrupt(int irq, void *dev_id) |
| 188 | { |
| 189 | struct atmel_isi *isi = dev_id; |
| 190 | u32 status, mask, pending; |
| 191 | irqreturn_t ret = IRQ_NONE; |
| 192 | |
| 193 | spin_lock(&isi->lock); |
| 194 | |
| 195 | status = isi_readl(isi, ISI_STATUS); |
| 196 | mask = isi_readl(isi, ISI_INTMASK); |
| 197 | pending = status & mask; |
| 198 | |
| 199 | if (pending & ISI_CTRL_SRST) { |
| 200 | complete(&isi->complete); |
| 201 | isi_writel(isi, ISI_INTDIS, ISI_CTRL_SRST); |
| 202 | ret = IRQ_HANDLED; |
| 203 | } else if (pending & ISI_CTRL_DIS) { |
| 204 | complete(&isi->complete); |
| 205 | isi_writel(isi, ISI_INTDIS, ISI_CTRL_DIS); |
| 206 | ret = IRQ_HANDLED; |
| 207 | } else { |
| 208 | if ((pending & ISI_SR_VSYNC) && |
| 209 | (isi->state == ISI_STATE_IDLE)) { |
| 210 | isi->state = ISI_STATE_READY; |
| 211 | wake_up_interruptible(&isi->vsync_wq); |
| 212 | ret = IRQ_HANDLED; |
| 213 | } |
| 214 | if (likely(pending & ISI_SR_CXFR_DONE)) |
| 215 | ret = atmel_isi_handle_streaming(isi); |
| 216 | } |
| 217 | |
| 218 | spin_unlock(&isi->lock); |
| 219 | return ret; |
| 220 | } |
| 221 | |
| 222 | #define WAIT_ISI_RESET 1 |
| 223 | #define WAIT_ISI_DISABLE 0 |
| 224 | static int atmel_isi_wait_status(struct atmel_isi *isi, int wait_reset) |
| 225 | { |
| 226 | unsigned long timeout; |
| 227 | /* |
| 228 | * The reset or disable will only succeed if we have a |
| 229 | * pixel clock from the camera. |
| 230 | */ |
| 231 | init_completion(&isi->complete); |
| 232 | |
| 233 | if (wait_reset) { |
| 234 | isi_writel(isi, ISI_INTEN, ISI_CTRL_SRST); |
| 235 | isi_writel(isi, ISI_CTRL, ISI_CTRL_SRST); |
| 236 | } else { |
| 237 | isi_writel(isi, ISI_INTEN, ISI_CTRL_DIS); |
| 238 | isi_writel(isi, ISI_CTRL, ISI_CTRL_DIS); |
| 239 | } |
| 240 | |
| 241 | timeout = wait_for_completion_timeout(&isi->complete, |
| 242 | msecs_to_jiffies(100)); |
| 243 | if (timeout == 0) |
| 244 | return -ETIMEDOUT; |
| 245 | |
| 246 | return 0; |
| 247 | } |
| 248 | |
| 249 | /* ------------------------------------------------------------------ |
| 250 | Videobuf operations |
| 251 | ------------------------------------------------------------------*/ |
Guennadi Liakhovetski | fc714e7 | 2011-08-24 10:30:21 -0300 | [diff] [blame] | 252 | static int queue_setup(struct vb2_queue *vq, const struct v4l2_format *fmt, |
| 253 | unsigned int *nbuffers, unsigned int *nplanes, |
| 254 | unsigned int sizes[], void *alloc_ctxs[]) |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 255 | { |
| 256 | struct soc_camera_device *icd = soc_camera_from_vb2q(vq); |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 257 | struct soc_camera_host *ici = to_soc_camera_host(icd->parent); |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 258 | struct atmel_isi *isi = ici->priv; |
| 259 | unsigned long size; |
| 260 | int ret, bytes_per_line; |
| 261 | |
| 262 | /* Reset ISI */ |
| 263 | ret = atmel_isi_wait_status(isi, WAIT_ISI_RESET); |
| 264 | if (ret < 0) { |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 265 | dev_err(icd->parent, "Reset ISI timed out\n"); |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 266 | return ret; |
| 267 | } |
| 268 | /* Disable all interrupts */ |
| 269 | isi_writel(isi, ISI_INTDIS, ~0UL); |
| 270 | |
| 271 | bytes_per_line = soc_mbus_bytes_per_line(icd->user_width, |
| 272 | icd->current_fmt->host_fmt); |
| 273 | |
| 274 | if (bytes_per_line < 0) |
| 275 | return bytes_per_line; |
| 276 | |
| 277 | size = bytes_per_line * icd->user_height; |
| 278 | |
| 279 | if (!*nbuffers || *nbuffers > MAX_BUFFER_NUM) |
| 280 | *nbuffers = MAX_BUFFER_NUM; |
| 281 | |
| 282 | if (size * *nbuffers > VID_LIMIT_BYTES) |
| 283 | *nbuffers = VID_LIMIT_BYTES / size; |
| 284 | |
| 285 | *nplanes = 1; |
| 286 | sizes[0] = size; |
| 287 | alloc_ctxs[0] = isi->alloc_ctx; |
| 288 | |
| 289 | isi->sequence = 0; |
| 290 | isi->active = NULL; |
| 291 | |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 292 | dev_dbg(icd->parent, "%s, count=%d, size=%ld\n", __func__, |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 293 | *nbuffers, size); |
| 294 | |
| 295 | return 0; |
| 296 | } |
| 297 | |
| 298 | static int buffer_init(struct vb2_buffer *vb) |
| 299 | { |
| 300 | struct frame_buffer *buf = container_of(vb, struct frame_buffer, vb); |
| 301 | |
| 302 | buf->p_dma_desc = NULL; |
| 303 | INIT_LIST_HEAD(&buf->list); |
| 304 | |
| 305 | return 0; |
| 306 | } |
| 307 | |
| 308 | static int buffer_prepare(struct vb2_buffer *vb) |
| 309 | { |
| 310 | struct soc_camera_device *icd = soc_camera_from_vb2q(vb->vb2_queue); |
| 311 | struct frame_buffer *buf = container_of(vb, struct frame_buffer, vb); |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 312 | struct soc_camera_host *ici = to_soc_camera_host(icd->parent); |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 313 | struct atmel_isi *isi = ici->priv; |
| 314 | unsigned long size; |
| 315 | struct isi_dma_desc *desc; |
| 316 | int bytes_per_line = soc_mbus_bytes_per_line(icd->user_width, |
| 317 | icd->current_fmt->host_fmt); |
| 318 | |
| 319 | if (bytes_per_line < 0) |
| 320 | return bytes_per_line; |
| 321 | |
| 322 | size = bytes_per_line * icd->user_height; |
| 323 | |
| 324 | if (vb2_plane_size(vb, 0) < size) { |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 325 | dev_err(icd->parent, "%s data will not fit into plane (%lu < %lu)\n", |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 326 | __func__, vb2_plane_size(vb, 0), size); |
| 327 | return -EINVAL; |
| 328 | } |
| 329 | |
| 330 | vb2_set_plane_payload(&buf->vb, 0, size); |
| 331 | |
| 332 | if (!buf->p_dma_desc) { |
| 333 | if (list_empty(&isi->dma_desc_head)) { |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 334 | dev_err(icd->parent, "Not enough dma descriptors.\n"); |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 335 | return -EINVAL; |
| 336 | } else { |
| 337 | /* Get an available descriptor */ |
| 338 | desc = list_entry(isi->dma_desc_head.next, |
| 339 | struct isi_dma_desc, list); |
| 340 | /* Delete the descriptor since now it is used */ |
| 341 | list_del_init(&desc->list); |
| 342 | |
| 343 | /* Initialize the dma descriptor */ |
| 344 | desc->p_fbd->fb_address = |
Marek Szyprowski | ba7fcb0 | 2011-08-29 03:20:56 -0300 | [diff] [blame] | 345 | vb2_dma_contig_plane_dma_addr(vb, 0); |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 346 | desc->p_fbd->next_fbd_address = 0; |
| 347 | set_dma_ctrl(desc->p_fbd, ISI_DMA_CTRL_WB); |
| 348 | |
| 349 | buf->p_dma_desc = desc; |
| 350 | } |
| 351 | } |
| 352 | return 0; |
| 353 | } |
| 354 | |
| 355 | static void buffer_cleanup(struct vb2_buffer *vb) |
| 356 | { |
| 357 | struct soc_camera_device *icd = soc_camera_from_vb2q(vb->vb2_queue); |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 358 | struct soc_camera_host *ici = to_soc_camera_host(icd->parent); |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 359 | struct atmel_isi *isi = ici->priv; |
| 360 | struct frame_buffer *buf = container_of(vb, struct frame_buffer, vb); |
| 361 | |
| 362 | /* This descriptor is available now and we add to head list */ |
| 363 | if (buf->p_dma_desc) |
| 364 | list_add(&buf->p_dma_desc->list, &isi->dma_desc_head); |
| 365 | } |
| 366 | |
| 367 | static void start_dma(struct atmel_isi *isi, struct frame_buffer *buffer) |
| 368 | { |
| 369 | u32 ctrl, cfg1; |
| 370 | |
| 371 | cfg1 = isi_readl(isi, ISI_CFG1); |
| 372 | /* Enable irq: cxfr for the codec path, pxfr for the preview path */ |
| 373 | isi_writel(isi, ISI_INTEN, |
| 374 | ISI_SR_CXFR_DONE | ISI_SR_PXFR_DONE); |
| 375 | |
| 376 | /* Check if already in a frame */ |
| 377 | if (isi_readl(isi, ISI_STATUS) & ISI_CTRL_CDC) { |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 378 | dev_err(isi->icd->parent, "Already in frame handling.\n"); |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 379 | return; |
| 380 | } |
| 381 | |
| 382 | isi_writel(isi, ISI_DMA_C_DSCR, buffer->p_dma_desc->fbd_phys); |
| 383 | isi_writel(isi, ISI_DMA_C_CTRL, ISI_DMA_CTRL_FETCH | ISI_DMA_CTRL_DONE); |
| 384 | isi_writel(isi, ISI_DMA_CHER, ISI_DMA_CHSR_C_CH); |
| 385 | |
| 386 | /* Enable linked list */ |
| 387 | cfg1 |= isi->pdata->frate | ISI_CFG1_DISCR; |
| 388 | |
| 389 | /* Enable codec path and ISI */ |
| 390 | ctrl = ISI_CTRL_CDC | ISI_CTRL_EN; |
| 391 | isi_writel(isi, ISI_CTRL, ctrl); |
| 392 | isi_writel(isi, ISI_CFG1, cfg1); |
| 393 | } |
| 394 | |
| 395 | static void buffer_queue(struct vb2_buffer *vb) |
| 396 | { |
| 397 | struct soc_camera_device *icd = soc_camera_from_vb2q(vb->vb2_queue); |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 398 | struct soc_camera_host *ici = to_soc_camera_host(icd->parent); |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 399 | struct atmel_isi *isi = ici->priv; |
| 400 | struct frame_buffer *buf = container_of(vb, struct frame_buffer, vb); |
| 401 | unsigned long flags = 0; |
| 402 | |
| 403 | spin_lock_irqsave(&isi->lock, flags); |
| 404 | list_add_tail(&buf->list, &isi->video_buffer_list); |
| 405 | |
| 406 | if (isi->active == NULL) { |
| 407 | isi->active = buf; |
Marek Szyprowski | bd323e2 | 2011-08-29 08:51:49 -0300 | [diff] [blame] | 408 | if (vb2_is_streaming(vb->vb2_queue)) |
| 409 | start_dma(isi, buf); |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 410 | } |
| 411 | spin_unlock_irqrestore(&isi->lock, flags); |
| 412 | } |
| 413 | |
Marek Szyprowski | bd323e2 | 2011-08-29 08:51:49 -0300 | [diff] [blame] | 414 | static int start_streaming(struct vb2_queue *vq, unsigned int count) |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 415 | { |
| 416 | struct soc_camera_device *icd = soc_camera_from_vb2q(vq); |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 417 | struct soc_camera_host *ici = to_soc_camera_host(icd->parent); |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 418 | struct atmel_isi *isi = ici->priv; |
| 419 | |
| 420 | u32 sr = 0; |
| 421 | int ret; |
| 422 | |
| 423 | spin_lock_irq(&isi->lock); |
| 424 | isi->state = ISI_STATE_IDLE; |
| 425 | /* Clear any pending SOF interrupt */ |
| 426 | sr = isi_readl(isi, ISI_STATUS); |
| 427 | /* Enable VSYNC interrupt for SOF */ |
| 428 | isi_writel(isi, ISI_INTEN, ISI_SR_VSYNC); |
| 429 | isi_writel(isi, ISI_CTRL, ISI_CTRL_EN); |
| 430 | spin_unlock_irq(&isi->lock); |
| 431 | |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 432 | dev_dbg(icd->parent, "Waiting for SOF\n"); |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 433 | ret = wait_event_interruptible(isi->vsync_wq, |
| 434 | isi->state != ISI_STATE_IDLE); |
| 435 | if (ret) |
Marek Szyprowski | bd323e2 | 2011-08-29 08:51:49 -0300 | [diff] [blame] | 436 | goto err; |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 437 | |
Marek Szyprowski | bd323e2 | 2011-08-29 08:51:49 -0300 | [diff] [blame] | 438 | if (isi->state != ISI_STATE_READY) { |
| 439 | ret = -EIO; |
| 440 | goto err; |
| 441 | } |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 442 | |
| 443 | spin_lock_irq(&isi->lock); |
| 444 | isi->state = ISI_STATE_WAIT_SOF; |
| 445 | isi_writel(isi, ISI_INTDIS, ISI_SR_VSYNC); |
Marek Szyprowski | bd323e2 | 2011-08-29 08:51:49 -0300 | [diff] [blame] | 446 | if (count) |
| 447 | start_dma(isi, isi->active); |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 448 | spin_unlock_irq(&isi->lock); |
| 449 | |
| 450 | return 0; |
Marek Szyprowski | bd323e2 | 2011-08-29 08:51:49 -0300 | [diff] [blame] | 451 | err: |
| 452 | isi->active = NULL; |
| 453 | isi->sequence = 0; |
| 454 | INIT_LIST_HEAD(&isi->video_buffer_list); |
| 455 | return ret; |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | /* abort streaming and wait for last buffer */ |
| 459 | static int stop_streaming(struct vb2_queue *vq) |
| 460 | { |
| 461 | struct soc_camera_device *icd = soc_camera_from_vb2q(vq); |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 462 | struct soc_camera_host *ici = to_soc_camera_host(icd->parent); |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 463 | struct atmel_isi *isi = ici->priv; |
| 464 | struct frame_buffer *buf, *node; |
| 465 | int ret = 0; |
| 466 | unsigned long timeout; |
| 467 | |
| 468 | spin_lock_irq(&isi->lock); |
| 469 | isi->active = NULL; |
| 470 | /* Release all active buffers */ |
| 471 | list_for_each_entry_safe(buf, node, &isi->video_buffer_list, list) { |
| 472 | list_del_init(&buf->list); |
| 473 | vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR); |
| 474 | } |
| 475 | spin_unlock_irq(&isi->lock); |
| 476 | |
| 477 | timeout = jiffies + FRAME_INTERVAL_MILLI_SEC * HZ; |
| 478 | /* Wait until the end of the current frame. */ |
| 479 | while ((isi_readl(isi, ISI_STATUS) & ISI_CTRL_CDC) && |
| 480 | time_before(jiffies, timeout)) |
| 481 | msleep(1); |
| 482 | |
| 483 | if (time_after(jiffies, timeout)) { |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 484 | dev_err(icd->parent, |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 485 | "Timeout waiting for finishing codec request\n"); |
| 486 | return -ETIMEDOUT; |
| 487 | } |
| 488 | |
| 489 | /* Disable interrupts */ |
| 490 | isi_writel(isi, ISI_INTDIS, |
| 491 | ISI_SR_CXFR_DONE | ISI_SR_PXFR_DONE); |
| 492 | |
| 493 | /* Disable ISI and wait for it is done */ |
| 494 | ret = atmel_isi_wait_status(isi, WAIT_ISI_DISABLE); |
| 495 | if (ret < 0) |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 496 | dev_err(icd->parent, "Disable ISI timed out\n"); |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 497 | |
| 498 | return ret; |
| 499 | } |
| 500 | |
| 501 | static struct vb2_ops isi_video_qops = { |
| 502 | .queue_setup = queue_setup, |
| 503 | .buf_init = buffer_init, |
| 504 | .buf_prepare = buffer_prepare, |
| 505 | .buf_cleanup = buffer_cleanup, |
| 506 | .buf_queue = buffer_queue, |
| 507 | .start_streaming = start_streaming, |
| 508 | .stop_streaming = stop_streaming, |
| 509 | .wait_prepare = soc_camera_unlock, |
| 510 | .wait_finish = soc_camera_lock, |
| 511 | }; |
| 512 | |
| 513 | /* ------------------------------------------------------------------ |
| 514 | SOC camera operations for the device |
| 515 | ------------------------------------------------------------------*/ |
| 516 | static int isi_camera_init_videobuf(struct vb2_queue *q, |
| 517 | struct soc_camera_device *icd) |
| 518 | { |
| 519 | q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 520 | q->io_modes = VB2_MMAP; |
| 521 | q->drv_priv = icd; |
| 522 | q->buf_struct_size = sizeof(struct frame_buffer); |
| 523 | q->ops = &isi_video_qops; |
| 524 | q->mem_ops = &vb2_dma_contig_memops; |
| 525 | |
| 526 | return vb2_queue_init(q); |
| 527 | } |
| 528 | |
| 529 | static int isi_camera_set_fmt(struct soc_camera_device *icd, |
| 530 | struct v4l2_format *f) |
| 531 | { |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 532 | struct soc_camera_host *ici = to_soc_camera_host(icd->parent); |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 533 | struct atmel_isi *isi = ici->priv; |
| 534 | struct v4l2_subdev *sd = soc_camera_to_subdev(icd); |
| 535 | const struct soc_camera_format_xlate *xlate; |
| 536 | struct v4l2_pix_format *pix = &f->fmt.pix; |
| 537 | struct v4l2_mbus_framefmt mf; |
| 538 | int ret; |
| 539 | |
| 540 | xlate = soc_camera_xlate_by_fourcc(icd, pix->pixelformat); |
| 541 | if (!xlate) { |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 542 | dev_warn(icd->parent, "Format %x not found\n", |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 543 | pix->pixelformat); |
| 544 | return -EINVAL; |
| 545 | } |
| 546 | |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 547 | dev_dbg(icd->parent, "Plan to set format %dx%d\n", |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 548 | pix->width, pix->height); |
| 549 | |
| 550 | mf.width = pix->width; |
| 551 | mf.height = pix->height; |
| 552 | mf.field = pix->field; |
| 553 | mf.colorspace = pix->colorspace; |
| 554 | mf.code = xlate->code; |
| 555 | |
| 556 | ret = v4l2_subdev_call(sd, video, s_mbus_fmt, &mf); |
| 557 | if (ret < 0) |
| 558 | return ret; |
| 559 | |
| 560 | if (mf.code != xlate->code) |
| 561 | return -EINVAL; |
| 562 | |
| 563 | ret = configure_geometry(isi, pix->width, pix->height, xlate->code); |
| 564 | if (ret < 0) |
| 565 | return ret; |
| 566 | |
| 567 | pix->width = mf.width; |
| 568 | pix->height = mf.height; |
| 569 | pix->field = mf.field; |
| 570 | pix->colorspace = mf.colorspace; |
| 571 | icd->current_fmt = xlate; |
| 572 | |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 573 | dev_dbg(icd->parent, "Finally set format %dx%d\n", |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 574 | pix->width, pix->height); |
| 575 | |
| 576 | return ret; |
| 577 | } |
| 578 | |
| 579 | static int isi_camera_try_fmt(struct soc_camera_device *icd, |
| 580 | struct v4l2_format *f) |
| 581 | { |
| 582 | struct v4l2_subdev *sd = soc_camera_to_subdev(icd); |
| 583 | const struct soc_camera_format_xlate *xlate; |
| 584 | struct v4l2_pix_format *pix = &f->fmt.pix; |
| 585 | struct v4l2_mbus_framefmt mf; |
| 586 | u32 pixfmt = pix->pixelformat; |
| 587 | int ret; |
| 588 | |
| 589 | xlate = soc_camera_xlate_by_fourcc(icd, pixfmt); |
| 590 | if (pixfmt && !xlate) { |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 591 | dev_warn(icd->parent, "Format %x not found\n", pixfmt); |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 592 | return -EINVAL; |
| 593 | } |
| 594 | |
| 595 | /* limit to Atmel ISI hardware capabilities */ |
| 596 | if (pix->height > MAX_SUPPORT_HEIGHT) |
| 597 | pix->height = MAX_SUPPORT_HEIGHT; |
| 598 | if (pix->width > MAX_SUPPORT_WIDTH) |
| 599 | pix->width = MAX_SUPPORT_WIDTH; |
| 600 | |
| 601 | /* limit to sensor capabilities */ |
| 602 | mf.width = pix->width; |
| 603 | mf.height = pix->height; |
| 604 | mf.field = pix->field; |
| 605 | mf.colorspace = pix->colorspace; |
| 606 | mf.code = xlate->code; |
| 607 | |
| 608 | ret = v4l2_subdev_call(sd, video, try_mbus_fmt, &mf); |
| 609 | if (ret < 0) |
| 610 | return ret; |
| 611 | |
| 612 | pix->width = mf.width; |
| 613 | pix->height = mf.height; |
| 614 | pix->colorspace = mf.colorspace; |
| 615 | |
| 616 | switch (mf.field) { |
| 617 | case V4L2_FIELD_ANY: |
| 618 | pix->field = V4L2_FIELD_NONE; |
| 619 | break; |
| 620 | case V4L2_FIELD_NONE: |
| 621 | break; |
| 622 | default: |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 623 | dev_err(icd->parent, "Field type %d unsupported.\n", |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 624 | mf.field); |
| 625 | ret = -EINVAL; |
| 626 | } |
| 627 | |
| 628 | return ret; |
| 629 | } |
| 630 | |
| 631 | static const struct soc_mbus_pixelfmt isi_camera_formats[] = { |
| 632 | { |
| 633 | .fourcc = V4L2_PIX_FMT_YUYV, |
| 634 | .name = "Packed YUV422 16 bit", |
| 635 | .bits_per_sample = 8, |
| 636 | .packing = SOC_MBUS_PACKING_2X8_PADHI, |
| 637 | .order = SOC_MBUS_ORDER_LE, |
| 638 | }, |
| 639 | }; |
| 640 | |
| 641 | /* This will be corrected as we get more formats */ |
| 642 | static bool isi_camera_packing_supported(const struct soc_mbus_pixelfmt *fmt) |
| 643 | { |
| 644 | return fmt->packing == SOC_MBUS_PACKING_NONE || |
| 645 | (fmt->bits_per_sample == 8 && |
| 646 | fmt->packing == SOC_MBUS_PACKING_2X8_PADHI) || |
| 647 | (fmt->bits_per_sample > 8 && |
| 648 | fmt->packing == SOC_MBUS_PACKING_EXTEND16); |
| 649 | } |
| 650 | |
Guennadi Liakhovetski | a4e9f10 | 2011-07-27 12:18:37 -0300 | [diff] [blame] | 651 | #define ISI_BUS_PARAM (V4L2_MBUS_MASTER | \ |
| 652 | V4L2_MBUS_HSYNC_ACTIVE_HIGH | \ |
| 653 | V4L2_MBUS_HSYNC_ACTIVE_LOW | \ |
| 654 | V4L2_MBUS_VSYNC_ACTIVE_HIGH | \ |
| 655 | V4L2_MBUS_VSYNC_ACTIVE_LOW | \ |
| 656 | V4L2_MBUS_PCLK_SAMPLE_RISING | \ |
| 657 | V4L2_MBUS_PCLK_SAMPLE_FALLING | \ |
| 658 | V4L2_MBUS_DATA_ACTIVE_HIGH) |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 659 | |
| 660 | static int isi_camera_try_bus_param(struct soc_camera_device *icd, |
| 661 | unsigned char buswidth) |
| 662 | { |
Guennadi Liakhovetski | a4e9f10 | 2011-07-27 12:18:37 -0300 | [diff] [blame] | 663 | struct v4l2_subdev *sd = soc_camera_to_subdev(icd); |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 664 | struct soc_camera_host *ici = to_soc_camera_host(icd->parent); |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 665 | struct atmel_isi *isi = ici->priv; |
Guennadi Liakhovetski | a4e9f10 | 2011-07-27 12:18:37 -0300 | [diff] [blame] | 666 | struct v4l2_mbus_config cfg = {.type = V4L2_MBUS_PARALLEL,}; |
| 667 | unsigned long common_flags; |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 668 | int ret; |
| 669 | |
Guennadi Liakhovetski | a4e9f10 | 2011-07-27 12:18:37 -0300 | [diff] [blame] | 670 | ret = v4l2_subdev_call(sd, video, g_mbus_config, &cfg); |
| 671 | if (!ret) { |
| 672 | common_flags = soc_mbus_config_compatible(&cfg, |
| 673 | ISI_BUS_PARAM); |
| 674 | if (!common_flags) { |
| 675 | dev_warn(icd->parent, |
| 676 | "Flags incompatible: camera 0x%x, host 0x%x\n", |
| 677 | cfg.flags, ISI_BUS_PARAM); |
| 678 | return -EINVAL; |
| 679 | } |
| 680 | } else if (ret != -ENOIOCTLCMD) { |
| 681 | return ret; |
| 682 | } |
| 683 | |
| 684 | if ((1 << (buswidth - 1)) & isi->width_flags) |
| 685 | return 0; |
| 686 | return -EINVAL; |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | |
| 690 | static int isi_camera_get_formats(struct soc_camera_device *icd, |
| 691 | unsigned int idx, |
| 692 | struct soc_camera_format_xlate *xlate) |
| 693 | { |
| 694 | struct v4l2_subdev *sd = soc_camera_to_subdev(icd); |
| 695 | int formats = 0, ret; |
| 696 | /* sensor format */ |
| 697 | enum v4l2_mbus_pixelcode code; |
| 698 | /* soc camera host format */ |
| 699 | const struct soc_mbus_pixelfmt *fmt; |
| 700 | |
| 701 | ret = v4l2_subdev_call(sd, video, enum_mbus_fmt, idx, &code); |
| 702 | if (ret < 0) |
| 703 | /* No more formats */ |
| 704 | return 0; |
| 705 | |
| 706 | fmt = soc_mbus_get_fmtdesc(code); |
| 707 | if (!fmt) { |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 708 | dev_err(icd->parent, |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 709 | "Invalid format code #%u: %d\n", idx, code); |
| 710 | return 0; |
| 711 | } |
| 712 | |
| 713 | /* This also checks support for the requested bits-per-sample */ |
| 714 | ret = isi_camera_try_bus_param(icd, fmt->bits_per_sample); |
| 715 | if (ret < 0) { |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 716 | dev_err(icd->parent, |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 717 | "Fail to try the bus parameters.\n"); |
| 718 | return 0; |
| 719 | } |
| 720 | |
| 721 | switch (code) { |
| 722 | case V4L2_MBUS_FMT_UYVY8_2X8: |
| 723 | case V4L2_MBUS_FMT_VYUY8_2X8: |
| 724 | case V4L2_MBUS_FMT_YUYV8_2X8: |
| 725 | case V4L2_MBUS_FMT_YVYU8_2X8: |
| 726 | formats++; |
| 727 | if (xlate) { |
| 728 | xlate->host_fmt = &isi_camera_formats[0]; |
| 729 | xlate->code = code; |
| 730 | xlate++; |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 731 | dev_dbg(icd->parent, "Providing format %s using code %d\n", |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 732 | isi_camera_formats[0].name, code); |
| 733 | } |
| 734 | break; |
| 735 | default: |
| 736 | if (!isi_camera_packing_supported(fmt)) |
| 737 | return 0; |
| 738 | if (xlate) |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 739 | dev_dbg(icd->parent, |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 740 | "Providing format %s in pass-through mode\n", |
| 741 | fmt->name); |
| 742 | } |
| 743 | |
| 744 | /* Generic pass-through */ |
| 745 | formats++; |
| 746 | if (xlate) { |
| 747 | xlate->host_fmt = fmt; |
| 748 | xlate->code = code; |
| 749 | xlate++; |
| 750 | } |
| 751 | |
| 752 | return formats; |
| 753 | } |
| 754 | |
| 755 | /* Called with .video_lock held */ |
| 756 | static int isi_camera_add_device(struct soc_camera_device *icd) |
| 757 | { |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 758 | struct soc_camera_host *ici = to_soc_camera_host(icd->parent); |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 759 | struct atmel_isi *isi = ici->priv; |
| 760 | int ret; |
| 761 | |
| 762 | if (isi->icd) |
| 763 | return -EBUSY; |
| 764 | |
| 765 | ret = clk_enable(isi->pclk); |
| 766 | if (ret) |
| 767 | return ret; |
| 768 | |
| 769 | isi->icd = icd; |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 770 | dev_dbg(icd->parent, "Atmel ISI Camera driver attached to camera %d\n", |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 771 | icd->devnum); |
| 772 | return 0; |
| 773 | } |
| 774 | /* Called with .video_lock held */ |
| 775 | static void isi_camera_remove_device(struct soc_camera_device *icd) |
| 776 | { |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 777 | struct soc_camera_host *ici = to_soc_camera_host(icd->parent); |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 778 | struct atmel_isi *isi = ici->priv; |
| 779 | |
| 780 | BUG_ON(icd != isi->icd); |
| 781 | |
| 782 | clk_disable(isi->pclk); |
| 783 | isi->icd = NULL; |
| 784 | |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 785 | dev_dbg(icd->parent, "Atmel ISI Camera driver detached from camera %d\n", |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 786 | icd->devnum); |
| 787 | } |
| 788 | |
| 789 | static unsigned int isi_camera_poll(struct file *file, poll_table *pt) |
| 790 | { |
| 791 | struct soc_camera_device *icd = file->private_data; |
| 792 | |
| 793 | return vb2_poll(&icd->vb2_vidq, file, pt); |
| 794 | } |
| 795 | |
| 796 | static int isi_camera_querycap(struct soc_camera_host *ici, |
| 797 | struct v4l2_capability *cap) |
| 798 | { |
| 799 | strcpy(cap->driver, "atmel-isi"); |
| 800 | strcpy(cap->card, "Atmel Image Sensor Interface"); |
| 801 | cap->capabilities = (V4L2_CAP_VIDEO_CAPTURE | |
| 802 | V4L2_CAP_STREAMING); |
| 803 | return 0; |
| 804 | } |
| 805 | |
| 806 | static int isi_camera_set_bus_param(struct soc_camera_device *icd, u32 pixfmt) |
| 807 | { |
Guennadi Liakhovetski | a4e9f10 | 2011-07-27 12:18:37 -0300 | [diff] [blame] | 808 | struct v4l2_subdev *sd = soc_camera_to_subdev(icd); |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 809 | struct soc_camera_host *ici = to_soc_camera_host(icd->parent); |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 810 | struct atmel_isi *isi = ici->priv; |
Guennadi Liakhovetski | a4e9f10 | 2011-07-27 12:18:37 -0300 | [diff] [blame] | 811 | struct v4l2_mbus_config cfg = {.type = V4L2_MBUS_PARALLEL,}; |
| 812 | unsigned long common_flags; |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 813 | int ret; |
| 814 | u32 cfg1 = 0; |
| 815 | |
Guennadi Liakhovetski | a4e9f10 | 2011-07-27 12:18:37 -0300 | [diff] [blame] | 816 | ret = v4l2_subdev_call(sd, video, g_mbus_config, &cfg); |
| 817 | if (!ret) { |
| 818 | common_flags = soc_mbus_config_compatible(&cfg, |
| 819 | ISI_BUS_PARAM); |
| 820 | if (!common_flags) { |
| 821 | dev_warn(icd->parent, |
| 822 | "Flags incompatible: camera 0x%x, host 0x%x\n", |
| 823 | cfg.flags, ISI_BUS_PARAM); |
| 824 | return -EINVAL; |
| 825 | } |
| 826 | } else if (ret != -ENOIOCTLCMD) { |
| 827 | return ret; |
| 828 | } else { |
| 829 | common_flags = ISI_BUS_PARAM; |
| 830 | } |
| 831 | dev_dbg(icd->parent, "Flags cam: 0x%x host: 0x%x common: 0x%lx\n", |
| 832 | cfg.flags, ISI_BUS_PARAM, common_flags); |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 833 | |
| 834 | /* Make choises, based on platform preferences */ |
Guennadi Liakhovetski | a4e9f10 | 2011-07-27 12:18:37 -0300 | [diff] [blame] | 835 | if ((common_flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH) && |
| 836 | (common_flags & V4L2_MBUS_HSYNC_ACTIVE_LOW)) { |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 837 | if (isi->pdata->hsync_act_low) |
Guennadi Liakhovetski | a4e9f10 | 2011-07-27 12:18:37 -0300 | [diff] [blame] | 838 | common_flags &= ~V4L2_MBUS_HSYNC_ACTIVE_HIGH; |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 839 | else |
Guennadi Liakhovetski | a4e9f10 | 2011-07-27 12:18:37 -0300 | [diff] [blame] | 840 | common_flags &= ~V4L2_MBUS_HSYNC_ACTIVE_LOW; |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 841 | } |
| 842 | |
Guennadi Liakhovetski | a4e9f10 | 2011-07-27 12:18:37 -0300 | [diff] [blame] | 843 | if ((common_flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH) && |
| 844 | (common_flags & V4L2_MBUS_VSYNC_ACTIVE_LOW)) { |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 845 | if (isi->pdata->vsync_act_low) |
Guennadi Liakhovetski | a4e9f10 | 2011-07-27 12:18:37 -0300 | [diff] [blame] | 846 | common_flags &= ~V4L2_MBUS_VSYNC_ACTIVE_HIGH; |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 847 | else |
Guennadi Liakhovetski | a4e9f10 | 2011-07-27 12:18:37 -0300 | [diff] [blame] | 848 | common_flags &= ~V4L2_MBUS_VSYNC_ACTIVE_LOW; |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 849 | } |
| 850 | |
Guennadi Liakhovetski | a4e9f10 | 2011-07-27 12:18:37 -0300 | [diff] [blame] | 851 | if ((common_flags & V4L2_MBUS_PCLK_SAMPLE_RISING) && |
| 852 | (common_flags & V4L2_MBUS_PCLK_SAMPLE_FALLING)) { |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 853 | if (isi->pdata->pclk_act_falling) |
Guennadi Liakhovetski | a4e9f10 | 2011-07-27 12:18:37 -0300 | [diff] [blame] | 854 | common_flags &= ~V4L2_MBUS_PCLK_SAMPLE_RISING; |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 855 | else |
Guennadi Liakhovetski | a4e9f10 | 2011-07-27 12:18:37 -0300 | [diff] [blame] | 856 | common_flags &= ~V4L2_MBUS_PCLK_SAMPLE_FALLING; |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 857 | } |
| 858 | |
Guennadi Liakhovetski | a4e9f10 | 2011-07-27 12:18:37 -0300 | [diff] [blame] | 859 | cfg.flags = common_flags; |
| 860 | ret = v4l2_subdev_call(sd, video, s_mbus_config, &cfg); |
| 861 | if (ret < 0 && ret != -ENOIOCTLCMD) { |
| 862 | dev_dbg(icd->parent, "camera s_mbus_config(0x%lx) returned %d\n", |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 863 | common_flags, ret); |
| 864 | return ret; |
| 865 | } |
| 866 | |
| 867 | /* set bus param for ISI */ |
Guennadi Liakhovetski | a4e9f10 | 2011-07-27 12:18:37 -0300 | [diff] [blame] | 868 | if (common_flags & V4L2_MBUS_HSYNC_ACTIVE_LOW) |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 869 | cfg1 |= ISI_CFG1_HSYNC_POL_ACTIVE_LOW; |
Guennadi Liakhovetski | a4e9f10 | 2011-07-27 12:18:37 -0300 | [diff] [blame] | 870 | if (common_flags & V4L2_MBUS_VSYNC_ACTIVE_LOW) |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 871 | cfg1 |= ISI_CFG1_VSYNC_POL_ACTIVE_LOW; |
Guennadi Liakhovetski | a4e9f10 | 2011-07-27 12:18:37 -0300 | [diff] [blame] | 872 | if (common_flags & V4L2_MBUS_PCLK_SAMPLE_FALLING) |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 873 | cfg1 |= ISI_CFG1_PIXCLK_POL_ACTIVE_FALLING; |
| 874 | |
| 875 | if (isi->pdata->has_emb_sync) |
| 876 | cfg1 |= ISI_CFG1_EMB_SYNC; |
| 877 | if (isi->pdata->isi_full_mode) |
| 878 | cfg1 |= ISI_CFG1_FULL_MODE; |
| 879 | |
| 880 | isi_writel(isi, ISI_CTRL, ISI_CTRL_DIS); |
| 881 | isi_writel(isi, ISI_CFG1, cfg1); |
| 882 | |
| 883 | return 0; |
| 884 | } |
| 885 | |
| 886 | static struct soc_camera_host_ops isi_soc_camera_host_ops = { |
| 887 | .owner = THIS_MODULE, |
| 888 | .add = isi_camera_add_device, |
| 889 | .remove = isi_camera_remove_device, |
| 890 | .set_fmt = isi_camera_set_fmt, |
| 891 | .try_fmt = isi_camera_try_fmt, |
| 892 | .get_formats = isi_camera_get_formats, |
| 893 | .init_videobuf2 = isi_camera_init_videobuf, |
| 894 | .poll = isi_camera_poll, |
| 895 | .querycap = isi_camera_querycap, |
| 896 | .set_bus_param = isi_camera_set_bus_param, |
| 897 | }; |
| 898 | |
| 899 | /* -----------------------------------------------------------------------*/ |
| 900 | static int __devexit atmel_isi_remove(struct platform_device *pdev) |
| 901 | { |
| 902 | struct soc_camera_host *soc_host = to_soc_camera_host(&pdev->dev); |
| 903 | struct atmel_isi *isi = container_of(soc_host, |
| 904 | struct atmel_isi, soc_host); |
| 905 | |
| 906 | free_irq(isi->irq, isi); |
| 907 | soc_camera_host_unregister(soc_host); |
| 908 | vb2_dma_contig_cleanup_ctx(isi->alloc_ctx); |
| 909 | dma_free_coherent(&pdev->dev, |
| 910 | sizeof(struct fbd) * MAX_BUFFER_NUM, |
| 911 | isi->p_fb_descriptors, |
| 912 | isi->fb_descriptors_phys); |
| 913 | |
| 914 | iounmap(isi->regs); |
| 915 | clk_put(isi->pclk); |
| 916 | kfree(isi); |
| 917 | |
| 918 | return 0; |
| 919 | } |
| 920 | |
| 921 | static int __devinit atmel_isi_probe(struct platform_device *pdev) |
| 922 | { |
| 923 | unsigned int irq; |
| 924 | struct atmel_isi *isi; |
| 925 | struct clk *pclk; |
| 926 | struct resource *regs; |
| 927 | int ret, i; |
| 928 | struct device *dev = &pdev->dev; |
| 929 | struct soc_camera_host *soc_host; |
| 930 | struct isi_platform_data *pdata; |
| 931 | |
| 932 | pdata = dev->platform_data; |
| 933 | if (!pdata || !pdata->data_width_flags) { |
| 934 | dev_err(&pdev->dev, |
| 935 | "No config available for Atmel ISI\n"); |
| 936 | return -EINVAL; |
| 937 | } |
| 938 | |
| 939 | regs = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 940 | if (!regs) |
| 941 | return -ENXIO; |
| 942 | |
| 943 | pclk = clk_get(&pdev->dev, "isi_clk"); |
| 944 | if (IS_ERR(pclk)) |
| 945 | return PTR_ERR(pclk); |
| 946 | |
| 947 | isi = kzalloc(sizeof(struct atmel_isi), GFP_KERNEL); |
| 948 | if (!isi) { |
| 949 | ret = -ENOMEM; |
| 950 | dev_err(&pdev->dev, "Can't allocate interface!\n"); |
| 951 | goto err_alloc_isi; |
| 952 | } |
| 953 | |
| 954 | isi->pclk = pclk; |
| 955 | isi->pdata = pdata; |
| 956 | isi->active = NULL; |
| 957 | spin_lock_init(&isi->lock); |
| 958 | init_waitqueue_head(&isi->vsync_wq); |
| 959 | INIT_LIST_HEAD(&isi->video_buffer_list); |
| 960 | INIT_LIST_HEAD(&isi->dma_desc_head); |
| 961 | |
| 962 | isi->p_fb_descriptors = dma_alloc_coherent(&pdev->dev, |
| 963 | sizeof(struct fbd) * MAX_BUFFER_NUM, |
| 964 | &isi->fb_descriptors_phys, |
| 965 | GFP_KERNEL); |
| 966 | if (!isi->p_fb_descriptors) { |
| 967 | ret = -ENOMEM; |
| 968 | dev_err(&pdev->dev, "Can't allocate descriptors!\n"); |
| 969 | goto err_alloc_descriptors; |
| 970 | } |
| 971 | |
| 972 | for (i = 0; i < MAX_BUFFER_NUM; i++) { |
| 973 | isi->dma_desc[i].p_fbd = isi->p_fb_descriptors + i; |
| 974 | isi->dma_desc[i].fbd_phys = isi->fb_descriptors_phys + |
| 975 | i * sizeof(struct fbd); |
| 976 | list_add(&isi->dma_desc[i].list, &isi->dma_desc_head); |
| 977 | } |
| 978 | |
| 979 | isi->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev); |
| 980 | if (IS_ERR(isi->alloc_ctx)) { |
| 981 | ret = PTR_ERR(isi->alloc_ctx); |
| 982 | goto err_alloc_ctx; |
| 983 | } |
| 984 | |
| 985 | isi->regs = ioremap(regs->start, resource_size(regs)); |
| 986 | if (!isi->regs) { |
| 987 | ret = -ENOMEM; |
| 988 | goto err_ioremap; |
| 989 | } |
| 990 | |
Guennadi Liakhovetski | a4e9f10 | 2011-07-27 12:18:37 -0300 | [diff] [blame] | 991 | if (pdata->data_width_flags & ISI_DATAWIDTH_8) |
| 992 | isi->width_flags = 1 << 7; |
| 993 | if (pdata->data_width_flags & ISI_DATAWIDTH_10) |
| 994 | isi->width_flags |= 1 << 9; |
| 995 | |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 996 | isi_writel(isi, ISI_CTRL, ISI_CTRL_DIS); |
| 997 | |
| 998 | irq = platform_get_irq(pdev, 0); |
| 999 | if (irq < 0) { |
| 1000 | ret = irq; |
| 1001 | goto err_req_irq; |
| 1002 | } |
| 1003 | |
| 1004 | ret = request_irq(irq, isi_interrupt, 0, "isi", isi); |
| 1005 | if (ret) { |
| 1006 | dev_err(&pdev->dev, "Unable to request irq %d\n", irq); |
| 1007 | goto err_req_irq; |
| 1008 | } |
| 1009 | isi->irq = irq; |
| 1010 | |
| 1011 | soc_host = &isi->soc_host; |
| 1012 | soc_host->drv_name = "isi-camera"; |
| 1013 | soc_host->ops = &isi_soc_camera_host_ops; |
| 1014 | soc_host->priv = isi; |
| 1015 | soc_host->v4l2_dev.dev = &pdev->dev; |
| 1016 | soc_host->nr = pdev->id; |
| 1017 | |
| 1018 | ret = soc_camera_host_register(soc_host); |
| 1019 | if (ret) { |
| 1020 | dev_err(&pdev->dev, "Unable to register soc camera host\n"); |
| 1021 | goto err_register_soc_camera_host; |
| 1022 | } |
| 1023 | return 0; |
| 1024 | |
| 1025 | err_register_soc_camera_host: |
| 1026 | free_irq(isi->irq, isi); |
| 1027 | err_req_irq: |
| 1028 | iounmap(isi->regs); |
| 1029 | err_ioremap: |
| 1030 | vb2_dma_contig_cleanup_ctx(isi->alloc_ctx); |
| 1031 | err_alloc_ctx: |
| 1032 | dma_free_coherent(&pdev->dev, |
| 1033 | sizeof(struct fbd) * MAX_BUFFER_NUM, |
| 1034 | isi->p_fb_descriptors, |
| 1035 | isi->fb_descriptors_phys); |
| 1036 | err_alloc_descriptors: |
| 1037 | kfree(isi); |
| 1038 | err_alloc_isi: |
| 1039 | clk_put(isi->pclk); |
| 1040 | |
| 1041 | return ret; |
| 1042 | } |
| 1043 | |
| 1044 | static struct platform_driver atmel_isi_driver = { |
| 1045 | .probe = atmel_isi_probe, |
| 1046 | .remove = __devexit_p(atmel_isi_remove), |
| 1047 | .driver = { |
| 1048 | .name = "atmel_isi", |
| 1049 | .owner = THIS_MODULE, |
| 1050 | }, |
| 1051 | }; |
| 1052 | |
| 1053 | static int __init atmel_isi_init_module(void) |
| 1054 | { |
| 1055 | return platform_driver_probe(&atmel_isi_driver, &atmel_isi_probe); |
| 1056 | } |
| 1057 | |
| 1058 | static void __exit atmel_isi_exit(void) |
| 1059 | { |
| 1060 | platform_driver_unregister(&atmel_isi_driver); |
| 1061 | } |
| 1062 | module_init(atmel_isi_init_module); |
| 1063 | module_exit(atmel_isi_exit); |
| 1064 | |
| 1065 | MODULE_AUTHOR("Josh Wu <josh.wu@atmel.com>"); |
| 1066 | MODULE_DESCRIPTION("The V4L2 driver for Atmel Linux"); |
| 1067 | MODULE_LICENSE("GPL"); |
| 1068 | MODULE_SUPPORTED_DEVICE("video"); |