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