Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

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