linux/drivers/media/video/mx1_camera.c
<<
>>
Prefs
   1/*
   2 * V4L2 Driver for i.MXL/i.MXL camera (CSI) host
   3 *
   4 * Copyright (C) 2008, Paulius Zaleckas <paulius.zaleckas@teltonika.lt>
   5 * Copyright (C) 2009, Darius Augulis <augulis.darius@gmail.com>
   6 *
   7 * Based on PXA SoC camera driver
   8 * Copyright (C) 2006, Sascha Hauer, Pengutronix
   9 * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
  10 *
  11 * This program is free software; you can redistribute it and/or modify
  12 * it under the terms of the GNU General Public License version 2 as
  13 * published by the Free Software Foundation.
  14 */
  15
  16#include <linux/clk.h>
  17#include <linux/delay.h>
  18#include <linux/device.h>
  19#include <linux/dma-mapping.h>
  20#include <linux/errno.h>
  21#include <linux/fs.h>
  22#include <linux/init.h>
  23#include <linux/interrupt.h>
  24#include <linux/io.h>
  25#include <linux/kernel.h>
  26#include <linux/mm.h>
  27#include <linux/module.h>
  28#include <linux/moduleparam.h>
  29#include <linux/mutex.h>
  30#include <linux/platform_device.h>
  31#include <linux/sched.h>
  32#include <linux/time.h>
  33#include <linux/version.h>
  34#include <linux/videodev2.h>
  35
  36#include <media/soc_camera.h>
  37#include <media/v4l2-common.h>
  38#include <media/v4l2-dev.h>
  39#include <media/videobuf-dma-contig.h>
  40
  41#include <asm/dma.h>
  42#include <asm/fiq.h>
  43#include <mach/dma-mx1-mx2.h>
  44#include <mach/hardware.h>
  45#include <mach/mx1_camera.h>
  46
  47/*
  48 * CSI registers
  49 */
  50#define DMA_CCR(x)      (0x8c + ((x) << 6))     /* Control Registers */
  51#define DMA_DIMR        0x08                    /* Interrupt mask Register */
  52#define CSICR1          0x00                    /* CSI Control Register 1 */
  53#define CSISR           0x08                    /* CSI Status Register */
  54#define CSIRXR          0x10                    /* CSI RxFIFO Register */
  55
  56#define CSICR1_RXFF_LEVEL(x)    (((x) & 0x3) << 19)
  57#define CSICR1_SOF_POL          (1 << 17)
  58#define CSICR1_SOF_INTEN        (1 << 16)
  59#define CSICR1_MCLKDIV(x)       (((x) & 0xf) << 12)
  60#define CSICR1_MCLKEN           (1 << 9)
  61#define CSICR1_FCC              (1 << 8)
  62#define CSICR1_BIG_ENDIAN       (1 << 7)
  63#define CSICR1_CLR_RXFIFO       (1 << 5)
  64#define CSICR1_GCLK_MODE        (1 << 4)
  65#define CSICR1_DATA_POL         (1 << 2)
  66#define CSICR1_REDGE            (1 << 1)
  67#define CSICR1_EN               (1 << 0)
  68
  69#define CSISR_SFF_OR_INT        (1 << 25)
  70#define CSISR_RFF_OR_INT        (1 << 24)
  71#define CSISR_STATFF_INT        (1 << 21)
  72#define CSISR_RXFF_INT          (1 << 18)
  73#define CSISR_SOF_INT           (1 << 16)
  74#define CSISR_DRDY              (1 << 0)
  75
  76#define VERSION_CODE KERNEL_VERSION(0, 0, 1)
  77#define DRIVER_NAME "mx1-camera"
  78
  79#define CSI_IRQ_MASK    (CSISR_SFF_OR_INT | CSISR_RFF_OR_INT | \
  80                        CSISR_STATFF_INT | CSISR_RXFF_INT | CSISR_SOF_INT)
  81
  82#define CSI_BUS_FLAGS   (SOCAM_MASTER | SOCAM_HSYNC_ACTIVE_HIGH | \
  83                        SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW | \
  84                        SOCAM_PCLK_SAMPLE_RISING | SOCAM_PCLK_SAMPLE_FALLING | \
  85                        SOCAM_DATA_ACTIVE_HIGH | SOCAM_DATA_ACTIVE_LOW | \
  86                        SOCAM_DATAWIDTH_8)
  87
  88#define MAX_VIDEO_MEM 16        /* Video memory limit in megabytes */
  89
  90/*
  91 * Structures
  92 */
  93
  94/* buffer for one video frame */
  95struct mx1_buffer {
  96        /* common v4l buffer stuff -- must be first */
  97        struct videobuf_buffer vb;
  98        const struct soc_camera_data_format *fmt;
  99        int inwork;
 100};
 101
 102/* i.MX1/i.MXL is only supposed to handle one camera on its Camera Sensor
 103 * Interface. If anyone ever builds hardware to enable more than
 104 * one camera, they will have to modify this driver too */
 105struct mx1_camera_dev {
 106        struct soc_camera_host          soc_host;
 107        struct soc_camera_device        *icd;
 108        struct mx1_camera_pdata         *pdata;
 109        struct mx1_buffer               *active;
 110        struct resource                 *res;
 111        struct clk                      *clk;
 112        struct list_head                capture;
 113
 114        void __iomem                    *base;
 115        int                             dma_chan;
 116        unsigned int                    irq;
 117        unsigned long                   mclk;
 118
 119        spinlock_t                      lock;
 120};
 121
 122/*
 123 *  Videobuf operations
 124 */
 125static int mx1_videobuf_setup(struct videobuf_queue *vq, unsigned int *count,
 126                              unsigned int *size)
 127{
 128        struct soc_camera_device *icd = vq->priv_data;
 129
 130        *size = icd->user_width * icd->user_height *
 131                ((icd->current_fmt->depth + 7) >> 3);
 132
 133        if (!*count)
 134                *count = 32;
 135
 136        while (*size * *count > MAX_VIDEO_MEM * 1024 * 1024)
 137                (*count)--;
 138
 139        dev_dbg(icd->dev.parent, "count=%d, size=%d\n", *count, *size);
 140
 141        return 0;
 142}
 143
 144static void free_buffer(struct videobuf_queue *vq, struct mx1_buffer *buf)
 145{
 146        struct soc_camera_device *icd = vq->priv_data;
 147        struct videobuf_buffer *vb = &buf->vb;
 148
 149        BUG_ON(in_interrupt());
 150
 151        dev_dbg(icd->dev.parent, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
 152                vb, vb->baddr, vb->bsize);
 153
 154        /* This waits until this buffer is out of danger, i.e., until it is no
 155         * longer in STATE_QUEUED or STATE_ACTIVE */
 156        videobuf_waiton(vb, 0, 0);
 157        videobuf_dma_contig_free(vq, vb);
 158
 159        vb->state = VIDEOBUF_NEEDS_INIT;
 160}
 161
 162static int mx1_videobuf_prepare(struct videobuf_queue *vq,
 163                struct videobuf_buffer *vb, enum v4l2_field field)
 164{
 165        struct soc_camera_device *icd = vq->priv_data;
 166        struct mx1_buffer *buf = container_of(vb, struct mx1_buffer, vb);
 167        int ret;
 168
 169        dev_dbg(icd->dev.parent, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
 170                vb, vb->baddr, vb->bsize);
 171
 172        /* Added list head initialization on alloc */
 173        WARN_ON(!list_empty(&vb->queue));
 174
 175        BUG_ON(NULL == icd->current_fmt);
 176
 177        /* I think, in buf_prepare you only have to protect global data,
 178         * the actual buffer is yours */
 179        buf->inwork = 1;
 180
 181        if (buf->fmt    != icd->current_fmt ||
 182            vb->width   != icd->user_width ||
 183            vb->height  != icd->user_height ||
 184            vb->field   != field) {
 185                buf->fmt        = icd->current_fmt;
 186                vb->width       = icd->user_width;
 187                vb->height      = icd->user_height;
 188                vb->field       = field;
 189                vb->state       = VIDEOBUF_NEEDS_INIT;
 190        }
 191
 192        vb->size = vb->width * vb->height * ((buf->fmt->depth + 7) >> 3);
 193        if (0 != vb->baddr && vb->bsize < vb->size) {
 194                ret = -EINVAL;
 195                goto out;
 196        }
 197
 198        if (vb->state == VIDEOBUF_NEEDS_INIT) {
 199                ret = videobuf_iolock(vq, vb, NULL);
 200                if (ret)
 201                        goto fail;
 202
 203                vb->state = VIDEOBUF_PREPARED;
 204        }
 205
 206        buf->inwork = 0;
 207
 208        return 0;
 209
 210fail:
 211        free_buffer(vq, buf);
 212out:
 213        buf->inwork = 0;
 214        return ret;
 215}
 216
 217static int mx1_camera_setup_dma(struct mx1_camera_dev *pcdev)
 218{
 219        struct videobuf_buffer *vbuf = &pcdev->active->vb;
 220        struct device *dev = pcdev->icd->dev.parent;
 221        int ret;
 222
 223        if (unlikely(!pcdev->active)) {
 224                dev_err(dev, "DMA End IRQ with no active buffer\n");
 225                return -EFAULT;
 226        }
 227
 228        /* setup sg list for future DMA */
 229        ret = imx_dma_setup_single(pcdev->dma_chan,
 230                videobuf_to_dma_contig(vbuf),
 231                vbuf->size, pcdev->res->start +
 232                CSIRXR, DMA_MODE_READ);
 233        if (unlikely(ret))
 234                dev_err(dev, "Failed to setup DMA sg list\n");
 235
 236        return ret;
 237}
 238
 239/* Called under spinlock_irqsave(&pcdev->lock, ...) */
 240static void mx1_videobuf_queue(struct videobuf_queue *vq,
 241                                                struct videobuf_buffer *vb)
 242{
 243        struct soc_camera_device *icd = vq->priv_data;
 244        struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
 245        struct mx1_camera_dev *pcdev = ici->priv;
 246        struct mx1_buffer *buf = container_of(vb, struct mx1_buffer, vb);
 247
 248        dev_dbg(icd->dev.parent, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
 249                vb, vb->baddr, vb->bsize);
 250
 251        list_add_tail(&vb->queue, &pcdev->capture);
 252
 253        vb->state = VIDEOBUF_ACTIVE;
 254
 255        if (!pcdev->active) {
 256                pcdev->active = buf;
 257
 258                /* setup sg list for future DMA */
 259                if (!mx1_camera_setup_dma(pcdev)) {
 260                        unsigned int temp;
 261                        /* enable SOF irq */
 262                        temp = __raw_readl(pcdev->base + CSICR1) |
 263                                                        CSICR1_SOF_INTEN;
 264                        __raw_writel(temp, pcdev->base + CSICR1);
 265                }
 266        }
 267}
 268
 269static void mx1_videobuf_release(struct videobuf_queue *vq,
 270                                 struct videobuf_buffer *vb)
 271{
 272        struct mx1_buffer *buf = container_of(vb, struct mx1_buffer, vb);
 273#ifdef DEBUG
 274        struct soc_camera_device *icd = vq->priv_data;
 275        struct device *dev = icd->dev.parent;
 276
 277        dev_dbg(dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
 278                vb, vb->baddr, vb->bsize);
 279
 280        switch (vb->state) {
 281        case VIDEOBUF_ACTIVE:
 282                dev_dbg(dev, "%s (active)\n", __func__);
 283                break;
 284        case VIDEOBUF_QUEUED:
 285                dev_dbg(dev, "%s (queued)\n", __func__);
 286                break;
 287        case VIDEOBUF_PREPARED:
 288                dev_dbg(dev, "%s (prepared)\n", __func__);
 289                break;
 290        default:
 291                dev_dbg(dev, "%s (unknown)\n", __func__);
 292                break;
 293        }
 294#endif
 295
 296        free_buffer(vq, buf);
 297}
 298
 299static void mx1_camera_wakeup(struct mx1_camera_dev *pcdev,
 300                              struct videobuf_buffer *vb,
 301                              struct mx1_buffer *buf)
 302{
 303        /* _init is used to debug races, see comment in mx1_camera_reqbufs() */
 304        list_del_init(&vb->queue);
 305        vb->state = VIDEOBUF_DONE;
 306        do_gettimeofday(&vb->ts);
 307        vb->field_count++;
 308        wake_up(&vb->done);
 309
 310        if (list_empty(&pcdev->capture)) {
 311                pcdev->active = NULL;
 312                return;
 313        }
 314
 315        pcdev->active = list_entry(pcdev->capture.next,
 316                                   struct mx1_buffer, vb.queue);
 317
 318        /* setup sg list for future DMA */
 319        if (likely(!mx1_camera_setup_dma(pcdev))) {
 320                unsigned int temp;
 321
 322                /* enable SOF irq */
 323                temp = __raw_readl(pcdev->base + CSICR1) | CSICR1_SOF_INTEN;
 324                __raw_writel(temp, pcdev->base + CSICR1);
 325        }
 326}
 327
 328static void mx1_camera_dma_irq(int channel, void *data)
 329{
 330        struct mx1_camera_dev *pcdev = data;
 331        struct device *dev = pcdev->icd->dev.parent;
 332        struct mx1_buffer *buf;
 333        struct videobuf_buffer *vb;
 334        unsigned long flags;
 335
 336        spin_lock_irqsave(&pcdev->lock, flags);
 337
 338        imx_dma_disable(channel);
 339
 340        if (unlikely(!pcdev->active)) {
 341                dev_err(dev, "DMA End IRQ with no active buffer\n");
 342                goto out;
 343        }
 344
 345        vb = &pcdev->active->vb;
 346        buf = container_of(vb, struct mx1_buffer, vb);
 347        WARN_ON(buf->inwork || list_empty(&vb->queue));
 348        dev_dbg(dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
 349                vb, vb->baddr, vb->bsize);
 350
 351        mx1_camera_wakeup(pcdev, vb, buf);
 352out:
 353        spin_unlock_irqrestore(&pcdev->lock, flags);
 354}
 355
 356static struct videobuf_queue_ops mx1_videobuf_ops = {
 357        .buf_setup      = mx1_videobuf_setup,
 358        .buf_prepare    = mx1_videobuf_prepare,
 359        .buf_queue      = mx1_videobuf_queue,
 360        .buf_release    = mx1_videobuf_release,
 361};
 362
 363static void mx1_camera_init_videobuf(struct videobuf_queue *q,
 364                                     struct soc_camera_device *icd)
 365{
 366        struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
 367        struct mx1_camera_dev *pcdev = ici->priv;
 368
 369        videobuf_queue_dma_contig_init(q, &mx1_videobuf_ops, icd->dev.parent,
 370                                        &pcdev->lock,
 371                                        V4L2_BUF_TYPE_VIDEO_CAPTURE,
 372                                        V4L2_FIELD_NONE,
 373                                        sizeof(struct mx1_buffer), icd);
 374}
 375
 376static int mclk_get_divisor(struct mx1_camera_dev *pcdev)
 377{
 378        unsigned int mclk = pcdev->mclk;
 379        unsigned long div;
 380        unsigned long lcdclk;
 381
 382        lcdclk = clk_get_rate(pcdev->clk);
 383
 384        /* We verify platform_mclk_10khz != 0, so if anyone breaks it, here
 385         * they get a nice Oops */
 386        div = (lcdclk + 2 * mclk - 1) / (2 * mclk) - 1;
 387
 388        dev_dbg(pcdev->icd->dev.parent,
 389                "System clock %lukHz, target freq %dkHz, divisor %lu\n",
 390                lcdclk / 1000, mclk / 1000, div);
 391
 392        return div;
 393}
 394
 395static void mx1_camera_activate(struct mx1_camera_dev *pcdev)
 396{
 397        unsigned int csicr1 = CSICR1_EN;
 398
 399        dev_dbg(pcdev->icd->dev.parent, "Activate device\n");
 400
 401        clk_enable(pcdev->clk);
 402
 403        /* enable CSI before doing anything else */
 404        __raw_writel(csicr1, pcdev->base + CSICR1);
 405
 406        csicr1 |= CSICR1_MCLKEN | CSICR1_FCC | CSICR1_GCLK_MODE;
 407        csicr1 |= CSICR1_MCLKDIV(mclk_get_divisor(pcdev));
 408        csicr1 |= CSICR1_RXFF_LEVEL(2); /* 16 words */
 409
 410        __raw_writel(csicr1, pcdev->base + CSICR1);
 411}
 412
 413static void mx1_camera_deactivate(struct mx1_camera_dev *pcdev)
 414{
 415        dev_dbg(pcdev->icd->dev.parent, "Deactivate device\n");
 416
 417        /* Disable all CSI interface */
 418        __raw_writel(0x00, pcdev->base + CSICR1);
 419
 420        clk_disable(pcdev->clk);
 421}
 422
 423/* The following two functions absolutely depend on the fact, that
 424 * there can be only one camera on i.MX1/i.MXL camera sensor interface */
 425static int mx1_camera_add_device(struct soc_camera_device *icd)
 426{
 427        struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
 428        struct mx1_camera_dev *pcdev = ici->priv;
 429        int ret;
 430
 431        if (pcdev->icd) {
 432                ret = -EBUSY;
 433                goto ebusy;
 434        }
 435
 436        dev_info(icd->dev.parent, "MX1 Camera driver attached to camera %d\n",
 437                 icd->devnum);
 438
 439        mx1_camera_activate(pcdev);
 440
 441        pcdev->icd = icd;
 442
 443ebusy:
 444        return ret;
 445}
 446
 447static void mx1_camera_remove_device(struct soc_camera_device *icd)
 448{
 449        struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
 450        struct mx1_camera_dev *pcdev = ici->priv;
 451        unsigned int csicr1;
 452
 453        BUG_ON(icd != pcdev->icd);
 454
 455        /* disable interrupts */
 456        csicr1 = __raw_readl(pcdev->base + CSICR1) & ~CSI_IRQ_MASK;
 457        __raw_writel(csicr1, pcdev->base + CSICR1);
 458
 459        /* Stop DMA engine */
 460        imx_dma_disable(pcdev->dma_chan);
 461
 462        dev_info(icd->dev.parent, "MX1 Camera driver detached from camera %d\n",
 463                 icd->devnum);
 464
 465        mx1_camera_deactivate(pcdev);
 466
 467        pcdev->icd = NULL;
 468}
 469
 470static int mx1_camera_set_crop(struct soc_camera_device *icd,
 471                               struct v4l2_crop *a)
 472{
 473        struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
 474
 475        return v4l2_subdev_call(sd, video, s_crop, a);
 476}
 477
 478static int mx1_camera_set_bus_param(struct soc_camera_device *icd, __u32 pixfmt)
 479{
 480        struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
 481        struct mx1_camera_dev *pcdev = ici->priv;
 482        unsigned long camera_flags, common_flags;
 483        unsigned int csicr1;
 484        int ret;
 485
 486        camera_flags = icd->ops->query_bus_param(icd);
 487
 488        /* MX1 supports only 8bit buswidth */
 489        common_flags = soc_camera_bus_param_compatible(camera_flags,
 490                                                               CSI_BUS_FLAGS);
 491        if (!common_flags)
 492                return -EINVAL;
 493
 494        icd->buswidth = 8;
 495
 496        /* Make choises, based on platform choice */
 497        if ((common_flags & SOCAM_VSYNC_ACTIVE_HIGH) &&
 498                (common_flags & SOCAM_VSYNC_ACTIVE_LOW)) {
 499                        if (!pcdev->pdata ||
 500                             pcdev->pdata->flags & MX1_CAMERA_VSYNC_HIGH)
 501                                common_flags &= ~SOCAM_VSYNC_ACTIVE_LOW;
 502                        else
 503                                common_flags &= ~SOCAM_VSYNC_ACTIVE_HIGH;
 504        }
 505
 506        if ((common_flags & SOCAM_PCLK_SAMPLE_RISING) &&
 507                (common_flags & SOCAM_PCLK_SAMPLE_FALLING)) {
 508                        if (!pcdev->pdata ||
 509                             pcdev->pdata->flags & MX1_CAMERA_PCLK_RISING)
 510                                common_flags &= ~SOCAM_PCLK_SAMPLE_FALLING;
 511                        else
 512                                common_flags &= ~SOCAM_PCLK_SAMPLE_RISING;
 513        }
 514
 515        if ((common_flags & SOCAM_DATA_ACTIVE_HIGH) &&
 516                (common_flags & SOCAM_DATA_ACTIVE_LOW)) {
 517                        if (!pcdev->pdata ||
 518                             pcdev->pdata->flags & MX1_CAMERA_DATA_HIGH)
 519                                common_flags &= ~SOCAM_DATA_ACTIVE_LOW;
 520                        else
 521                                common_flags &= ~SOCAM_DATA_ACTIVE_HIGH;
 522        }
 523
 524        ret = icd->ops->set_bus_param(icd, common_flags);
 525        if (ret < 0)
 526                return ret;
 527
 528        csicr1 = __raw_readl(pcdev->base + CSICR1);
 529
 530        if (common_flags & SOCAM_PCLK_SAMPLE_RISING)
 531                csicr1 |= CSICR1_REDGE;
 532        if (common_flags & SOCAM_VSYNC_ACTIVE_HIGH)
 533                csicr1 |= CSICR1_SOF_POL;
 534        if (common_flags & SOCAM_DATA_ACTIVE_LOW)
 535                csicr1 |= CSICR1_DATA_POL;
 536
 537        __raw_writel(csicr1, pcdev->base + CSICR1);
 538
 539        return 0;
 540}
 541
 542static int mx1_camera_set_fmt(struct soc_camera_device *icd,
 543                              struct v4l2_format *f)
 544{
 545        struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
 546        const struct soc_camera_format_xlate *xlate;
 547        struct v4l2_pix_format *pix = &f->fmt.pix;
 548        int ret;
 549
 550        xlate = soc_camera_xlate_by_fourcc(icd, pix->pixelformat);
 551        if (!xlate) {
 552                dev_warn(icd->dev.parent, "Format %x not found\n",
 553                         pix->pixelformat);
 554                return -EINVAL;
 555        }
 556
 557        ret = v4l2_subdev_call(sd, video, s_fmt, f);
 558        if (!ret) {
 559                icd->buswidth = xlate->buswidth;
 560                icd->current_fmt = xlate->host_fmt;
 561        }
 562
 563        return ret;
 564}
 565
 566static int mx1_camera_try_fmt(struct soc_camera_device *icd,
 567                              struct v4l2_format *f)
 568{
 569        struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
 570        /* TODO: limit to mx1 hardware capabilities */
 571
 572        /* limit to sensor capabilities */
 573        return v4l2_subdev_call(sd, video, try_fmt, f);
 574}
 575
 576static int mx1_camera_reqbufs(struct soc_camera_file *icf,
 577                              struct v4l2_requestbuffers *p)
 578{
 579        int i;
 580
 581        /* This is for locking debugging only. I removed spinlocks and now I
 582         * check whether .prepare is ever called on a linked buffer, or whether
 583         * a dma IRQ can occur for an in-work or unlinked buffer. Until now
 584         * it hadn't triggered */
 585        for (i = 0; i < p->count; i++) {
 586                struct mx1_buffer *buf = container_of(icf->vb_vidq.bufs[i],
 587                                                      struct mx1_buffer, vb);
 588                buf->inwork = 0;
 589                INIT_LIST_HEAD(&buf->vb.queue);
 590        }
 591
 592        return 0;
 593}
 594
 595static unsigned int mx1_camera_poll(struct file *file, poll_table *pt)
 596{
 597        struct soc_camera_file *icf = file->private_data;
 598        struct mx1_buffer *buf;
 599
 600        buf = list_entry(icf->vb_vidq.stream.next, struct mx1_buffer,
 601                         vb.stream);
 602
 603        poll_wait(file, &buf->vb.done, pt);
 604
 605        if (buf->vb.state == VIDEOBUF_DONE ||
 606            buf->vb.state == VIDEOBUF_ERROR)
 607                return POLLIN | POLLRDNORM;
 608
 609        return 0;
 610}
 611
 612static int mx1_camera_querycap(struct soc_camera_host *ici,
 613                               struct v4l2_capability *cap)
 614{
 615        /* cap->name is set by the friendly caller:-> */
 616        strlcpy(cap->card, "i.MX1/i.MXL Camera", sizeof(cap->card));
 617        cap->version = VERSION_CODE;
 618        cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
 619
 620        return 0;
 621}
 622
 623static struct soc_camera_host_ops mx1_soc_camera_host_ops = {
 624        .owner          = THIS_MODULE,
 625        .add            = mx1_camera_add_device,
 626        .remove         = mx1_camera_remove_device,
 627        .set_bus_param  = mx1_camera_set_bus_param,
 628        .set_crop       = mx1_camera_set_crop,
 629        .set_fmt        = mx1_camera_set_fmt,
 630        .try_fmt        = mx1_camera_try_fmt,
 631        .init_videobuf  = mx1_camera_init_videobuf,
 632        .reqbufs        = mx1_camera_reqbufs,
 633        .poll           = mx1_camera_poll,
 634        .querycap       = mx1_camera_querycap,
 635};
 636
 637static struct fiq_handler fh = {
 638        .name           = "csi_sof"
 639};
 640
 641static int __init mx1_camera_probe(struct platform_device *pdev)
 642{
 643        struct mx1_camera_dev *pcdev;
 644        struct resource *res;
 645        struct pt_regs regs;
 646        struct clk *clk;
 647        void __iomem *base;
 648        unsigned int irq;
 649        int err = 0;
 650
 651        res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
 652        irq = platform_get_irq(pdev, 0);
 653        if (!res || !irq) {
 654                err = -ENODEV;
 655                goto exit;
 656        }
 657
 658        clk = clk_get(&pdev->dev, "csi_clk");
 659        if (IS_ERR(clk)) {
 660                err = PTR_ERR(clk);
 661                goto exit;
 662        }
 663
 664        pcdev = kzalloc(sizeof(*pcdev), GFP_KERNEL);
 665        if (!pcdev) {
 666                dev_err(&pdev->dev, "Could not allocate pcdev\n");
 667                err = -ENOMEM;
 668                goto exit_put_clk;
 669        }
 670
 671        pcdev->res = res;
 672        pcdev->clk = clk;
 673
 674        pcdev->pdata = pdev->dev.platform_data;
 675
 676        if (pcdev->pdata)
 677                pcdev->mclk = pcdev->pdata->mclk_10khz * 10000;
 678
 679        if (!pcdev->mclk) {
 680                dev_warn(&pdev->dev,
 681                         "mclk_10khz == 0! Please, fix your platform data. "
 682                         "Using default 20MHz\n");
 683                pcdev->mclk = 20000000;
 684        }
 685
 686        INIT_LIST_HEAD(&pcdev->capture);
 687        spin_lock_init(&pcdev->lock);
 688
 689        /*
 690         * Request the regions.
 691         */
 692        if (!request_mem_region(res->start, resource_size(res), DRIVER_NAME)) {
 693                err = -EBUSY;
 694                goto exit_kfree;
 695        }
 696
 697        base = ioremap(res->start, resource_size(res));
 698        if (!base) {
 699                err = -ENOMEM;
 700                goto exit_release;
 701        }
 702        pcdev->irq = irq;
 703        pcdev->base = base;
 704
 705        /* request dma */
 706        pcdev->dma_chan = imx_dma_request_by_prio(DRIVER_NAME, DMA_PRIO_HIGH);
 707        if (pcdev->dma_chan < 0) {
 708                dev_err(&pdev->dev, "Can't request DMA for MX1 CSI\n");
 709                err = -EBUSY;
 710                goto exit_iounmap;
 711        }
 712        dev_dbg(&pdev->dev, "got DMA channel %d\n", pcdev->dma_chan);
 713
 714        imx_dma_setup_handlers(pcdev->dma_chan, mx1_camera_dma_irq, NULL,
 715                               pcdev);
 716
 717        imx_dma_config_channel(pcdev->dma_chan, IMX_DMA_TYPE_FIFO,
 718                               IMX_DMA_MEMSIZE_32, DMA_REQ_CSI_R, 0);
 719        /* burst length : 16 words = 64 bytes */
 720        imx_dma_config_burstlen(pcdev->dma_chan, 0);
 721
 722        /* request irq */
 723        err = claim_fiq(&fh);
 724        if (err) {
 725                dev_err(&pdev->dev, "Camera interrupt register failed \n");
 726                goto exit_free_dma;
 727        }
 728
 729        set_fiq_handler(&mx1_camera_sof_fiq_start, &mx1_camera_sof_fiq_end -
 730                                                   &mx1_camera_sof_fiq_start);
 731
 732        regs.ARM_r8 = DMA_BASE + DMA_DIMR;
 733        regs.ARM_r9 = DMA_BASE + DMA_CCR(pcdev->dma_chan);
 734        regs.ARM_r10 = (long)pcdev->base + CSICR1;
 735        regs.ARM_fp = (long)pcdev->base + CSISR;
 736        regs.ARM_sp = 1 << pcdev->dma_chan;
 737        set_fiq_regs(&regs);
 738
 739        mxc_set_irq_fiq(irq, 1);
 740        enable_fiq(irq);
 741
 742        pcdev->soc_host.drv_name        = DRIVER_NAME;
 743        pcdev->soc_host.ops             = &mx1_soc_camera_host_ops;
 744        pcdev->soc_host.priv            = pcdev;
 745        pcdev->soc_host.v4l2_dev.dev    = &pdev->dev;
 746        pcdev->soc_host.nr              = pdev->id;
 747        err = soc_camera_host_register(&pcdev->soc_host);
 748        if (err)
 749                goto exit_free_irq;
 750
 751        dev_info(&pdev->dev, "MX1 Camera driver loaded\n");
 752
 753        return 0;
 754
 755exit_free_irq:
 756        disable_fiq(irq);
 757        mxc_set_irq_fiq(irq, 0);
 758        release_fiq(&fh);
 759exit_free_dma:
 760        imx_dma_free(pcdev->dma_chan);
 761exit_iounmap:
 762        iounmap(base);
 763exit_release:
 764        release_mem_region(res->start, resource_size(res));
 765exit_kfree:
 766        kfree(pcdev);
 767exit_put_clk:
 768        clk_put(clk);
 769exit:
 770        return err;
 771}
 772
 773static int __exit mx1_camera_remove(struct platform_device *pdev)
 774{
 775        struct soc_camera_host *soc_host = to_soc_camera_host(&pdev->dev);
 776        struct mx1_camera_dev *pcdev = container_of(soc_host,
 777                                        struct mx1_camera_dev, soc_host);
 778        struct resource *res;
 779
 780        imx_dma_free(pcdev->dma_chan);
 781        disable_fiq(pcdev->irq);
 782        mxc_set_irq_fiq(pcdev->irq, 0);
 783        release_fiq(&fh);
 784
 785        clk_put(pcdev->clk);
 786
 787        soc_camera_host_unregister(soc_host);
 788
 789        iounmap(pcdev->base);
 790
 791        res = pcdev->res;
 792        release_mem_region(res->start, resource_size(res));
 793
 794        kfree(pcdev);
 795
 796        dev_info(&pdev->dev, "MX1 Camera driver unloaded\n");
 797
 798        return 0;
 799}
 800
 801static struct platform_driver mx1_camera_driver = {
 802        .driver         = {
 803                .name   = DRIVER_NAME,
 804        },
 805        .remove         = __exit_p(mx1_camera_remove),
 806};
 807
 808static int __init mx1_camera_init(void)
 809{
 810        return platform_driver_probe(&mx1_camera_driver, mx1_camera_probe);
 811}
 812
 813static void __exit mx1_camera_exit(void)
 814{
 815        return platform_driver_unregister(&mx1_camera_driver);
 816}
 817
 818module_init(mx1_camera_init);
 819module_exit(mx1_camera_exit);
 820
 821MODULE_DESCRIPTION("i.MX1/i.MXL SoC Camera Host driver");
 822MODULE_AUTHOR("Paulius Zaleckas <paulius.zaleckas@teltonika.lt>");
 823MODULE_LICENSE("GPL v2");
 824MODULE_ALIAS("platform:" DRIVER_NAME);
 825
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.