linux/drivers/media/video/cafe_ccic.c
<<
>>
Prefs
   1/*
   2 * A driver for the CMOS camera controller in the Marvell 88ALP01 "cafe"
   3 * multifunction chip.  Currently works with the Omnivision OV7670
   4 * sensor.
   5 *
   6 * The data sheet for this device can be found at:
   7 *    http://www.marvell.com/products/pc_connectivity/88alp01/ 
   8 *
   9 * Copyright 2006 One Laptop Per Child Association, Inc.
  10 * Copyright 2006-7 Jonathan Corbet <corbet@lwn.net>
  11 *
  12 * Written by Jonathan Corbet, corbet@lwn.net.
  13 *
  14 * v4l2_device/v4l2_subdev conversion by:
  15 * Copyright (C) 2009 Hans Verkuil <hverkuil@xs4all.nl>
  16 *
  17 * Note: this conversion is untested! Please contact the linux-media
  18 * mailinglist if you can test this, together with the test results.
  19 *
  20 * This file may be distributed under the terms of the GNU General
  21 * Public License, version 2.
  22 */
  23
  24#include <linux/kernel.h>
  25#include <linux/module.h>
  26#include <linux/init.h>
  27#include <linux/fs.h>
  28#include <linux/dmi.h>
  29#include <linux/mm.h>
  30#include <linux/pci.h>
  31#include <linux/i2c.h>
  32#include <linux/interrupt.h>
  33#include <linux/spinlock.h>
  34#include <linux/videodev2.h>
  35#include <linux/slab.h>
  36#include <media/v4l2-device.h>
  37#include <media/v4l2-ioctl.h>
  38#include <media/v4l2-chip-ident.h>
  39#include <linux/device.h>
  40#include <linux/wait.h>
  41#include <linux/list.h>
  42#include <linux/dma-mapping.h>
  43#include <linux/delay.h>
  44#include <linux/jiffies.h>
  45#include <linux/vmalloc.h>
  46
  47#include <asm/uaccess.h>
  48#include <asm/io.h>
  49
  50#include "ov7670.h"
  51#include "cafe_ccic-regs.h"
  52
  53#define CAFE_VERSION 0x000002
  54
  55
  56/*
  57 * Parameters.
  58 */
  59MODULE_AUTHOR("Jonathan Corbet <corbet@lwn.net>");
  60MODULE_DESCRIPTION("Marvell 88ALP01 CMOS Camera Controller driver");
  61MODULE_LICENSE("GPL");
  62MODULE_SUPPORTED_DEVICE("Video");
  63
  64/*
  65 * Internal DMA buffer management.  Since the controller cannot do S/G I/O,
  66 * we must have physically contiguous buffers to bring frames into.
  67 * These parameters control how many buffers we use, whether we
  68 * allocate them at load time (better chance of success, but nails down
  69 * memory) or when somebody tries to use the camera (riskier), and,
  70 * for load-time allocation, how big they should be.
  71 *
  72 * The controller can cycle through three buffers.  We could use
  73 * more by flipping pointers around, but it probably makes little
  74 * sense.
  75 */
  76
  77#define MAX_DMA_BUFS 3
  78static int alloc_bufs_at_read;
  79module_param(alloc_bufs_at_read, bool, 0444);
  80MODULE_PARM_DESC(alloc_bufs_at_read,
  81                "Non-zero value causes DMA buffers to be allocated when the "
  82                "video capture device is read, rather than at module load "
  83                "time.  This saves memory, but decreases the chances of "
  84                "successfully getting those buffers.");
  85
  86static int n_dma_bufs = 3;
  87module_param(n_dma_bufs, uint, 0644);
  88MODULE_PARM_DESC(n_dma_bufs,
  89                "The number of DMA buffers to allocate.  Can be either two "
  90                "(saves memory, makes timing tighter) or three.");
  91
  92static int dma_buf_size = VGA_WIDTH * VGA_HEIGHT * 2;  /* Worst case */
  93module_param(dma_buf_size, uint, 0444);
  94MODULE_PARM_DESC(dma_buf_size,
  95                "The size of the allocated DMA buffers.  If actual operating "
  96                "parameters require larger buffers, an attempt to reallocate "
  97                "will be made.");
  98
  99static int min_buffers = 1;
 100module_param(min_buffers, uint, 0644);
 101MODULE_PARM_DESC(min_buffers,
 102                "The minimum number of streaming I/O buffers we are willing "
 103                "to work with.");
 104
 105static int max_buffers = 10;
 106module_param(max_buffers, uint, 0644);
 107MODULE_PARM_DESC(max_buffers,
 108                "The maximum number of streaming I/O buffers an application "
 109                "will be allowed to allocate.  These buffers are big and live "
 110                "in vmalloc space.");
 111
 112static int flip;
 113module_param(flip, bool, 0444);
 114MODULE_PARM_DESC(flip,
 115                "If set, the sensor will be instructed to flip the image "
 116                "vertically.");
 117
 118
 119enum cafe_state {
 120        S_NOTREADY,     /* Not yet initialized */
 121        S_IDLE,         /* Just hanging around */
 122        S_FLAKED,       /* Some sort of problem */
 123        S_SINGLEREAD,   /* In read() */
 124        S_SPECREAD,     /* Speculative read (for future read()) */
 125        S_STREAMING     /* Streaming data */
 126};
 127
 128/*
 129 * Tracking of streaming I/O buffers.
 130 */
 131struct cafe_sio_buffer {
 132        struct list_head list;
 133        struct v4l2_buffer v4lbuf;
 134        char *buffer;   /* Where it lives in kernel space */
 135        int mapcount;
 136        struct cafe_camera *cam;
 137};
 138
 139/*
 140 * A description of one of our devices.
 141 * Locking: controlled by s_mutex.  Certain fields, however, require
 142 *          the dev_lock spinlock; they are marked as such by comments.
 143 *          dev_lock is also required for access to device registers.
 144 */
 145struct cafe_camera
 146{
 147        struct v4l2_device v4l2_dev;
 148        enum cafe_state state;
 149        unsigned long flags;            /* Buffer status, mainly (dev_lock) */
 150        int users;                      /* How many open FDs */
 151        struct file *owner;             /* Who has data access (v4l2) */
 152
 153        /*
 154         * Subsystem structures.
 155         */
 156        struct pci_dev *pdev;
 157        struct video_device vdev;
 158        struct i2c_adapter i2c_adapter;
 159        struct v4l2_subdev *sensor;
 160        unsigned short sensor_addr;
 161
 162        unsigned char __iomem *regs;
 163        struct list_head dev_list;      /* link to other devices */
 164
 165        /* DMA buffers */
 166        unsigned int nbufs;             /* How many are alloc'd */
 167        int next_buf;                   /* Next to consume (dev_lock) */
 168        unsigned int dma_buf_size;      /* allocated size */
 169        void *dma_bufs[MAX_DMA_BUFS];   /* Internal buffer addresses */
 170        dma_addr_t dma_handles[MAX_DMA_BUFS]; /* Buffer bus addresses */
 171        unsigned int specframes;        /* Unconsumed spec frames (dev_lock) */
 172        unsigned int sequence;          /* Frame sequence number */
 173        unsigned int buf_seq[MAX_DMA_BUFS]; /* Sequence for individual buffers */
 174
 175        /* Streaming buffers */
 176        unsigned int n_sbufs;           /* How many we have */
 177        struct cafe_sio_buffer *sb_bufs; /* The array of housekeeping structs */
 178        struct list_head sb_avail;      /* Available for data (we own) (dev_lock) */
 179        struct list_head sb_full;       /* With data (user space owns) (dev_lock) */
 180        struct tasklet_struct s_tasklet;
 181
 182        /* Current operating parameters */
 183        u32 sensor_type;                /* Currently ov7670 only */
 184        struct v4l2_pix_format pix_format;
 185        enum v4l2_mbus_pixelcode mbus_code;
 186
 187        /* Locks */
 188        struct mutex s_mutex; /* Access to this structure */
 189        spinlock_t dev_lock;  /* Access to device */
 190
 191        /* Misc */
 192        wait_queue_head_t smbus_wait;   /* Waiting on i2c events */
 193        wait_queue_head_t iowait;       /* Waiting on frame data */
 194};
 195
 196/*
 197 * Status flags.  Always manipulated with bit operations.
 198 */
 199#define CF_BUF0_VALID    0      /* Buffers valid - first three */
 200#define CF_BUF1_VALID    1
 201#define CF_BUF2_VALID    2
 202#define CF_DMA_ACTIVE    3      /* A frame is incoming */
 203#define CF_CONFIG_NEEDED 4      /* Must configure hardware */
 204
 205#define sensor_call(cam, o, f, args...) \
 206        v4l2_subdev_call(cam->sensor, o, f, ##args)
 207
 208static inline struct cafe_camera *to_cam(struct v4l2_device *dev)
 209{
 210        return container_of(dev, struct cafe_camera, v4l2_dev);
 211}
 212
 213static struct cafe_format_struct {
 214        __u8 *desc;
 215        __u32 pixelformat;
 216        int bpp;   /* Bytes per pixel */
 217        enum v4l2_mbus_pixelcode mbus_code;
 218} cafe_formats[] = {
 219        {
 220                .desc           = "YUYV 4:2:2",
 221                .pixelformat    = V4L2_PIX_FMT_YUYV,
 222                .mbus_code      = V4L2_MBUS_FMT_YUYV8_2X8,
 223                .bpp            = 2,
 224        },
 225        {
 226                .desc           = "RGB 444",
 227                .pixelformat    = V4L2_PIX_FMT_RGB444,
 228                .mbus_code      = V4L2_MBUS_FMT_RGB444_2X8_PADHI_LE,
 229                .bpp            = 2,
 230        },
 231        {
 232                .desc           = "RGB 565",
 233                .pixelformat    = V4L2_PIX_FMT_RGB565,
 234                .mbus_code      = V4L2_MBUS_FMT_RGB565_2X8_LE,
 235                .bpp            = 2,
 236        },
 237        {
 238                .desc           = "Raw RGB Bayer",
 239                .pixelformat    = V4L2_PIX_FMT_SBGGR8,
 240                .mbus_code      = V4L2_MBUS_FMT_SBGGR8_1X8,
 241                .bpp            = 1
 242        },
 243};
 244#define N_CAFE_FMTS ARRAY_SIZE(cafe_formats)
 245
 246static struct cafe_format_struct *cafe_find_format(u32 pixelformat)
 247{
 248        unsigned i;
 249
 250        for (i = 0; i < N_CAFE_FMTS; i++)
 251                if (cafe_formats[i].pixelformat == pixelformat)
 252                        return cafe_formats + i;
 253        /* Not found? Then return the first format. */
 254        return cafe_formats;
 255}
 256
 257/*
 258 * Start over with DMA buffers - dev_lock needed.
 259 */
 260static void cafe_reset_buffers(struct cafe_camera *cam)
 261{
 262        int i;
 263
 264        cam->next_buf = -1;
 265        for (i = 0; i < cam->nbufs; i++)
 266                clear_bit(i, &cam->flags);
 267        cam->specframes = 0;
 268}
 269
 270static inline int cafe_needs_config(struct cafe_camera *cam)
 271{
 272        return test_bit(CF_CONFIG_NEEDED, &cam->flags);
 273}
 274
 275static void cafe_set_config_needed(struct cafe_camera *cam, int needed)
 276{
 277        if (needed)
 278                set_bit(CF_CONFIG_NEEDED, &cam->flags);
 279        else
 280                clear_bit(CF_CONFIG_NEEDED, &cam->flags);
 281}
 282
 283
 284
 285
 286/*
 287 * Debugging and related.
 288 */
 289#define cam_err(cam, fmt, arg...) \
 290        dev_err(&(cam)->pdev->dev, fmt, ##arg);
 291#define cam_warn(cam, fmt, arg...) \
 292        dev_warn(&(cam)->pdev->dev, fmt, ##arg);
 293#define cam_dbg(cam, fmt, arg...) \
 294        dev_dbg(&(cam)->pdev->dev, fmt, ##arg);
 295
 296
 297/* ---------------------------------------------------------------------*/
 298
 299/*
 300 * Device register I/O
 301 */
 302static inline void cafe_reg_write(struct cafe_camera *cam, unsigned int reg,
 303                unsigned int val)
 304{
 305        iowrite32(val, cam->regs + reg);
 306}
 307
 308static inline unsigned int cafe_reg_read(struct cafe_camera *cam,
 309                unsigned int reg)
 310{
 311        return ioread32(cam->regs + reg);
 312}
 313
 314
 315static inline void cafe_reg_write_mask(struct cafe_camera *cam, unsigned int reg,
 316                unsigned int val, unsigned int mask)
 317{
 318        unsigned int v = cafe_reg_read(cam, reg);
 319
 320        v = (v & ~mask) | (val & mask);
 321        cafe_reg_write(cam, reg, v);
 322}
 323
 324static inline void cafe_reg_clear_bit(struct cafe_camera *cam,
 325                unsigned int reg, unsigned int val)
 326{
 327        cafe_reg_write_mask(cam, reg, 0, val);
 328}
 329
 330static inline void cafe_reg_set_bit(struct cafe_camera *cam,
 331                unsigned int reg, unsigned int val)
 332{
 333        cafe_reg_write_mask(cam, reg, val, val);
 334}
 335
 336
 337
 338/* -------------------------------------------------------------------- */
 339/*
 340 * The I2C/SMBUS interface to the camera itself starts here.  The
 341 * controller handles SMBUS itself, presenting a relatively simple register
 342 * interface; all we have to do is to tell it where to route the data.
 343 */
 344#define CAFE_SMBUS_TIMEOUT (HZ)  /* generous */
 345
 346static int cafe_smbus_write_done(struct cafe_camera *cam)
 347{
 348        unsigned long flags;
 349        int c1;
 350
 351        /*
 352         * We must delay after the interrupt, or the controller gets confused
 353         * and never does give us good status.  Fortunately, we don't do this
 354         * often.
 355         */
 356        udelay(20);
 357        spin_lock_irqsave(&cam->dev_lock, flags);
 358        c1 = cafe_reg_read(cam, REG_TWSIC1);
 359        spin_unlock_irqrestore(&cam->dev_lock, flags);
 360        return (c1 & (TWSIC1_WSTAT|TWSIC1_ERROR)) != TWSIC1_WSTAT;
 361}
 362
 363static int cafe_smbus_write_data(struct cafe_camera *cam,
 364                u16 addr, u8 command, u8 value)
 365{
 366        unsigned int rval;
 367        unsigned long flags;
 368
 369        spin_lock_irqsave(&cam->dev_lock, flags);
 370        rval = TWSIC0_EN | ((addr << TWSIC0_SID_SHIFT) & TWSIC0_SID);
 371        rval |= TWSIC0_OVMAGIC;  /* Make OV sensors work */
 372        /*
 373         * Marvell sez set clkdiv to all 1's for now.
 374         */
 375        rval |= TWSIC0_CLKDIV;
 376        cafe_reg_write(cam, REG_TWSIC0, rval);
 377        (void) cafe_reg_read(cam, REG_TWSIC1); /* force write */
 378        rval = value | ((command << TWSIC1_ADDR_SHIFT) & TWSIC1_ADDR);
 379        cafe_reg_write(cam, REG_TWSIC1, rval);
 380        spin_unlock_irqrestore(&cam->dev_lock, flags);
 381
 382        /* Unfortunately, reading TWSIC1 too soon after sending a command
 383         * causes the device to die.
 384         * Use a busy-wait because we often send a large quantity of small
 385         * commands at-once; using msleep() would cause a lot of context
 386         * switches which take longer than 2ms, resulting in a noticable
 387         * boot-time and capture-start delays.
 388         */
 389        mdelay(2);
 390
 391        /*
 392         * Another sad fact is that sometimes, commands silently complete but
 393         * cafe_smbus_write_done() never becomes aware of this.
 394         * This happens at random and appears to possible occur with any
 395         * command.
 396         * We don't understand why this is. We work around this issue
 397         * with the timeout in the wait below, assuming that all commands
 398         * complete within the timeout.
 399         */
 400        wait_event_timeout(cam->smbus_wait, cafe_smbus_write_done(cam),
 401                        CAFE_SMBUS_TIMEOUT);
 402
 403        spin_lock_irqsave(&cam->dev_lock, flags);
 404        rval = cafe_reg_read(cam, REG_TWSIC1);
 405        spin_unlock_irqrestore(&cam->dev_lock, flags);
 406
 407        if (rval & TWSIC1_WSTAT) {
 408                cam_err(cam, "SMBUS write (%02x/%02x/%02x) timed out\n", addr,
 409                                command, value);
 410                return -EIO;
 411        }
 412        if (rval & TWSIC1_ERROR) {
 413                cam_err(cam, "SMBUS write (%02x/%02x/%02x) error\n", addr,
 414                                command, value);
 415                return -EIO;
 416        }
 417        return 0;
 418}
 419
 420
 421
 422static int cafe_smbus_read_done(struct cafe_camera *cam)
 423{
 424        unsigned long flags;
 425        int c1;
 426
 427        /*
 428         * We must delay after the interrupt, or the controller gets confused
 429         * and never does give us good status.  Fortunately, we don't do this
 430         * often.
 431         */
 432        udelay(20);
 433        spin_lock_irqsave(&cam->dev_lock, flags);
 434        c1 = cafe_reg_read(cam, REG_TWSIC1);
 435        spin_unlock_irqrestore(&cam->dev_lock, flags);
 436        return c1 & (TWSIC1_RVALID|TWSIC1_ERROR);
 437}
 438
 439
 440
 441static int cafe_smbus_read_data(struct cafe_camera *cam,
 442                u16 addr, u8 command, u8 *value)
 443{
 444        unsigned int rval;
 445        unsigned long flags;
 446
 447        spin_lock_irqsave(&cam->dev_lock, flags);
 448        rval = TWSIC0_EN | ((addr << TWSIC0_SID_SHIFT) & TWSIC0_SID);
 449        rval |= TWSIC0_OVMAGIC; /* Make OV sensors work */
 450        /*
 451         * Marvel sez set clkdiv to all 1's for now.
 452         */
 453        rval |= TWSIC0_CLKDIV;
 454        cafe_reg_write(cam, REG_TWSIC0, rval);
 455        (void) cafe_reg_read(cam, REG_TWSIC1); /* force write */
 456        rval = TWSIC1_READ | ((command << TWSIC1_ADDR_SHIFT) & TWSIC1_ADDR);
 457        cafe_reg_write(cam, REG_TWSIC1, rval);
 458        spin_unlock_irqrestore(&cam->dev_lock, flags);
 459
 460        wait_event_timeout(cam->smbus_wait,
 461                        cafe_smbus_read_done(cam), CAFE_SMBUS_TIMEOUT);
 462        spin_lock_irqsave(&cam->dev_lock, flags);
 463        rval = cafe_reg_read(cam, REG_TWSIC1);
 464        spin_unlock_irqrestore(&cam->dev_lock, flags);
 465
 466        if (rval & TWSIC1_ERROR) {
 467                cam_err(cam, "SMBUS read (%02x/%02x) error\n", addr, command);
 468                return -EIO;
 469        }
 470        if (! (rval & TWSIC1_RVALID)) {
 471                cam_err(cam, "SMBUS read (%02x/%02x) timed out\n", addr,
 472                                command);
 473                return -EIO;
 474        }
 475        *value = rval & 0xff;
 476        return 0;
 477}
 478
 479/*
 480 * Perform a transfer over SMBUS.  This thing is called under
 481 * the i2c bus lock, so we shouldn't race with ourselves...
 482 */
 483static int cafe_smbus_xfer(struct i2c_adapter *adapter, u16 addr,
 484                unsigned short flags, char rw, u8 command,
 485                int size, union i2c_smbus_data *data)
 486{
 487        struct v4l2_device *v4l2_dev = i2c_get_adapdata(adapter);
 488        struct cafe_camera *cam = to_cam(v4l2_dev);
 489        int ret = -EINVAL;
 490
 491        /*
 492         * This interface would appear to only do byte data ops.  OK
 493         * it can do word too, but the cam chip has no use for that.
 494         */
 495        if (size != I2C_SMBUS_BYTE_DATA) {
 496                cam_err(cam, "funky xfer size %d\n", size);
 497                return -EINVAL;
 498        }
 499
 500        if (rw == I2C_SMBUS_WRITE)
 501                ret = cafe_smbus_write_data(cam, addr, command, data->byte);
 502        else if (rw == I2C_SMBUS_READ)
 503                ret = cafe_smbus_read_data(cam, addr, command, &data->byte);
 504        return ret;
 505}
 506
 507
 508static void cafe_smbus_enable_irq(struct cafe_camera *cam)
 509{
 510        unsigned long flags;
 511
 512        spin_lock_irqsave(&cam->dev_lock, flags);
 513        cafe_reg_set_bit(cam, REG_IRQMASK, TWSIIRQS);
 514        spin_unlock_irqrestore(&cam->dev_lock, flags);
 515}
 516
 517static u32 cafe_smbus_func(struct i2c_adapter *adapter)
 518{
 519        return I2C_FUNC_SMBUS_READ_BYTE_DATA  |
 520               I2C_FUNC_SMBUS_WRITE_BYTE_DATA;
 521}
 522
 523static struct i2c_algorithm cafe_smbus_algo = {
 524        .smbus_xfer = cafe_smbus_xfer,
 525        .functionality = cafe_smbus_func
 526};
 527
 528/* Somebody is on the bus */
 529static void cafe_ctlr_stop_dma(struct cafe_camera *cam);
 530static void cafe_ctlr_power_down(struct cafe_camera *cam);
 531
 532static int cafe_smbus_setup(struct cafe_camera *cam)
 533{
 534        struct i2c_adapter *adap = &cam->i2c_adapter;
 535        int ret;
 536
 537        cafe_smbus_enable_irq(cam);
 538        adap->owner = THIS_MODULE;
 539        adap->algo = &cafe_smbus_algo;
 540        strcpy(adap->name, "cafe_ccic");
 541        adap->dev.parent = &cam->pdev->dev;
 542        i2c_set_adapdata(adap, &cam->v4l2_dev);
 543        ret = i2c_add_adapter(adap);
 544        if (ret)
 545                printk(KERN_ERR "Unable to register cafe i2c adapter\n");
 546        return ret;
 547}
 548
 549static void cafe_smbus_shutdown(struct cafe_camera *cam)
 550{
 551        i2c_del_adapter(&cam->i2c_adapter);
 552}
 553
 554
 555/* ------------------------------------------------------------------- */
 556/*
 557 * Deal with the controller.
 558 */
 559
 560/*
 561 * Do everything we think we need to have the interface operating
 562 * according to the desired format.
 563 */
 564static void cafe_ctlr_dma(struct cafe_camera *cam)
 565{
 566        /*
 567         * Store the first two Y buffers (we aren't supporting
 568         * planar formats for now, so no UV bufs).  Then either
 569         * set the third if it exists, or tell the controller
 570         * to just use two.
 571         */
 572        cafe_reg_write(cam, REG_Y0BAR, cam->dma_handles[0]);
 573        cafe_reg_write(cam, REG_Y1BAR, cam->dma_handles[1]);
 574        if (cam->nbufs > 2) {
 575                cafe_reg_write(cam, REG_Y2BAR, cam->dma_handles[2]);
 576                cafe_reg_clear_bit(cam, REG_CTRL1, C1_TWOBUFS);
 577        }
 578        else
 579                cafe_reg_set_bit(cam, REG_CTRL1, C1_TWOBUFS);
 580        cafe_reg_write(cam, REG_UBAR, 0); /* 32 bits only for now */
 581}
 582
 583static void cafe_ctlr_image(struct cafe_camera *cam)
 584{
 585        int imgsz;
 586        struct v4l2_pix_format *fmt = &cam->pix_format;
 587
 588        imgsz = ((fmt->height << IMGSZ_V_SHIFT) & IMGSZ_V_MASK) |
 589                (fmt->bytesperline & IMGSZ_H_MASK);
 590        cafe_reg_write(cam, REG_IMGSIZE, imgsz);
 591        cafe_reg_write(cam, REG_IMGOFFSET, 0);
 592        /* YPITCH just drops the last two bits */
 593        cafe_reg_write_mask(cam, REG_IMGPITCH, fmt->bytesperline,
 594                        IMGP_YP_MASK);
 595        /*
 596         * Tell the controller about the image format we are using.
 597         */
 598        switch (cam->pix_format.pixelformat) {
 599        case V4L2_PIX_FMT_YUYV:
 600            cafe_reg_write_mask(cam, REG_CTRL0,
 601                            C0_DF_YUV|C0_YUV_PACKED|C0_YUVE_YUYV,
 602                            C0_DF_MASK);
 603            break;
 604
 605        case V4L2_PIX_FMT_RGB444:
 606            cafe_reg_write_mask(cam, REG_CTRL0,
 607                            C0_DF_RGB|C0_RGBF_444|C0_RGB4_XRGB,
 608                            C0_DF_MASK);
 609                /* Alpha value? */
 610            break;
 611
 612        case V4L2_PIX_FMT_RGB565:
 613            cafe_reg_write_mask(cam, REG_CTRL0,
 614                            C0_DF_RGB|C0_RGBF_565|C0_RGB5_BGGR,
 615                            C0_DF_MASK);
 616            break;
 617
 618        default:
 619            cam_err(cam, "Unknown format %x\n", cam->pix_format.pixelformat);
 620            break;
 621        }
 622        /*
 623         * Make sure it knows we want to use hsync/vsync.
 624         */
 625        cafe_reg_write_mask(cam, REG_CTRL0, C0_SIF_HVSYNC,
 626                        C0_SIFM_MASK);
 627}
 628
 629
 630/*
 631 * Configure the controller for operation; caller holds the
 632 * device mutex.
 633 */
 634static int cafe_ctlr_configure(struct cafe_camera *cam)
 635{
 636        unsigned long flags;
 637
 638        spin_lock_irqsave(&cam->dev_lock, flags);
 639        cafe_ctlr_dma(cam);
 640        cafe_ctlr_image(cam);
 641        cafe_set_config_needed(cam, 0);
 642        spin_unlock_irqrestore(&cam->dev_lock, flags);
 643        return 0;
 644}
 645
 646static void cafe_ctlr_irq_enable(struct cafe_camera *cam)
 647{
 648        /*
 649         * Clear any pending interrupts, since we do not
 650         * expect to have I/O active prior to enabling.
 651         */
 652        cafe_reg_write(cam, REG_IRQSTAT, FRAMEIRQS);
 653        cafe_reg_set_bit(cam, REG_IRQMASK, FRAMEIRQS);
 654}
 655
 656static void cafe_ctlr_irq_disable(struct cafe_camera *cam)
 657{
 658        cafe_reg_clear_bit(cam, REG_IRQMASK, FRAMEIRQS);
 659}
 660
 661/*
 662 * Make the controller start grabbing images.  Everything must
 663 * be set up before doing this.
 664 */
 665static void cafe_ctlr_start(struct cafe_camera *cam)
 666{
 667        /* set_bit performs a read, so no other barrier should be
 668           needed here */
 669        cafe_reg_set_bit(cam, REG_CTRL0, C0_ENABLE);
 670}
 671
 672static void cafe_ctlr_stop(struct cafe_camera *cam)
 673{
 674        cafe_reg_clear_bit(cam, REG_CTRL0, C0_ENABLE);
 675}
 676
 677static void cafe_ctlr_init(struct cafe_camera *cam)
 678{
 679        unsigned long flags;
 680
 681        spin_lock_irqsave(&cam->dev_lock, flags);
 682        /*
 683         * Added magic to bring up the hardware on the B-Test board
 684         */
 685        cafe_reg_write(cam, 0x3038, 0x8);
 686        cafe_reg_write(cam, 0x315c, 0x80008);
 687        /*
 688         * Go through the dance needed to wake the device up.
 689         * Note that these registers are global and shared
 690         * with the NAND and SD devices.  Interaction between the
 691         * three still needs to be examined.
 692         */
 693        cafe_reg_write(cam, REG_GL_CSR, GCSR_SRS|GCSR_MRS); /* Needed? */
 694        cafe_reg_write(cam, REG_GL_CSR, GCSR_SRC|GCSR_MRC);
 695        cafe_reg_write(cam, REG_GL_CSR, GCSR_SRC|GCSR_MRS);
 696        /*
 697         * Here we must wait a bit for the controller to come around.
 698         */
 699        spin_unlock_irqrestore(&cam->dev_lock, flags);
 700        msleep(5);
 701        spin_lock_irqsave(&cam->dev_lock, flags);
 702
 703        cafe_reg_write(cam, REG_GL_CSR, GCSR_CCIC_EN|GCSR_SRC|GCSR_MRC);
 704        cafe_reg_set_bit(cam, REG_GL_IMASK, GIMSK_CCIC_EN);
 705        /*
 706         * Make sure it's not powered down.
 707         */
 708        cafe_reg_clear_bit(cam, REG_CTRL1, C1_PWRDWN);
 709        /*
 710         * Turn off the enable bit.  It sure should be off anyway,
 711         * but it's good to be sure.
 712         */
 713        cafe_reg_clear_bit(cam, REG_CTRL0, C0_ENABLE);
 714        /*
 715         * Mask all interrupts.
 716         */
 717        cafe_reg_write(cam, REG_IRQMASK, 0);
 718        /*
 719         * Clock the sensor appropriately.  Controller clock should
 720         * be 48MHz, sensor "typical" value is half that.
 721         */
 722        cafe_reg_write_mask(cam, REG_CLKCTRL, 2, CLK_DIV_MASK);
 723        spin_unlock_irqrestore(&cam->dev_lock, flags);
 724}
 725
 726
 727/*
 728 * Stop the controller, and don't return until we're really sure that no
 729 * further DMA is going on.
 730 */
 731static void cafe_ctlr_stop_dma(struct cafe_camera *cam)
 732{
 733        unsigned long flags;
 734
 735        /*
 736         * Theory: stop the camera controller (whether it is operating
 737         * or not).  Delay briefly just in case we race with the SOF
 738         * interrupt, then wait until no DMA is active.
 739         */
 740        spin_lock_irqsave(&cam->dev_lock, flags);
 741        cafe_ctlr_stop(cam);
 742        spin_unlock_irqrestore(&cam->dev_lock, flags);
 743        mdelay(1);
 744        wait_event_timeout(cam->iowait,
 745                        !test_bit(CF_DMA_ACTIVE, &cam->flags), HZ);
 746        if (test_bit(CF_DMA_ACTIVE, &cam->flags))
 747                cam_err(cam, "Timeout waiting for DMA to end\n");
 748                /* This would be bad news - what now? */
 749        spin_lock_irqsave(&cam->dev_lock, flags);
 750        cam->state = S_IDLE;
 751        cafe_ctlr_irq_disable(cam);
 752        spin_unlock_irqrestore(&cam->dev_lock, flags);
 753}
 754
 755/*
 756 * Power up and down.
 757 */
 758static void cafe_ctlr_power_up(struct cafe_camera *cam)
 759{
 760        unsigned long flags;
 761
 762        spin_lock_irqsave(&cam->dev_lock, flags);
 763        cafe_reg_clear_bit(cam, REG_CTRL1, C1_PWRDWN);
 764        /*
 765         * Part one of the sensor dance: turn the global
 766         * GPIO signal on.
 767         */
 768        cafe_reg_write(cam, REG_GL_FCR, GFCR_GPIO_ON);
 769        cafe_reg_write(cam, REG_GL_GPIOR, GGPIO_OUT|GGPIO_VAL);
 770        /*
 771         * Put the sensor into operational mode (assumes OLPC-style
 772         * wiring).  Control 0 is reset - set to 1 to operate.
 773         * Control 1 is power down, set to 0 to operate.
 774         */
 775        cafe_reg_write(cam, REG_GPR, GPR_C1EN|GPR_C0EN); /* pwr up, reset */
 776/*      mdelay(1); */ /* Marvell says 1ms will do it */
 777        cafe_reg_write(cam, REG_GPR, GPR_C1EN|GPR_C0EN|GPR_C0);
 778/*      mdelay(1); */ /* Enough? */
 779        spin_unlock_irqrestore(&cam->dev_lock, flags);
 780        msleep(5); /* Just to be sure */
 781}
 782
 783static void cafe_ctlr_power_down(struct cafe_camera *cam)
 784{
 785        unsigned long flags;
 786
 787        spin_lock_irqsave(&cam->dev_lock, flags);
 788        cafe_reg_write(cam, REG_GPR, GPR_C1EN|GPR_C0EN|GPR_C1);
 789        cafe_reg_write(cam, REG_GL_FCR, GFCR_GPIO_ON);
 790        cafe_reg_write(cam, REG_GL_GPIOR, GGPIO_OUT);
 791        cafe_reg_set_bit(cam, REG_CTRL1, C1_PWRDWN);
 792        spin_unlock_irqrestore(&cam->dev_lock, flags);
 793}
 794
 795/* -------------------------------------------------------------------- */
 796/*
 797 * Communications with the sensor.
 798 */
 799
 800static int __cafe_cam_reset(struct cafe_camera *cam)
 801{
 802        return sensor_call(cam, core, reset, 0);
 803}
 804
 805/*
 806 * We have found the sensor on the i2c.  Let's try to have a
 807 * conversation.
 808 */
 809static int cafe_cam_init(struct cafe_camera *cam)
 810{
 811        struct v4l2_dbg_chip_ident chip;
 812        int ret;
 813
 814        mutex_lock(&cam->s_mutex);
 815        if (cam->state != S_NOTREADY)
 816                cam_warn(cam, "Cam init with device in funky state %d",
 817                                cam->state);
 818        ret = __cafe_cam_reset(cam);
 819        if (ret)
 820                goto out;
 821        chip.ident = V4L2_IDENT_NONE;
 822        chip.match.type = V4L2_CHIP_MATCH_I2C_ADDR;
 823        chip.match.addr = cam->sensor_addr;
 824        ret = sensor_call(cam, core, g_chip_ident, &chip);
 825        if (ret)
 826                goto out;
 827        cam->sensor_type = chip.ident;
 828        if (cam->sensor_type != V4L2_IDENT_OV7670) {
 829                cam_err(cam, "Unsupported sensor type 0x%x", cam->sensor_type);
 830                ret = -EINVAL;
 831                goto out;
 832        }
 833/* Get/set parameters? */
 834        ret = 0;
 835        cam->state = S_IDLE;
 836  out:
 837        cafe_ctlr_power_down(cam);
 838        mutex_unlock(&cam->s_mutex);
 839        return ret;
 840}
 841
 842/*
 843 * Configure the sensor to match the parameters we have.  Caller should
 844 * hold s_mutex
 845 */
 846static int cafe_cam_set_flip(struct cafe_camera *cam)
 847{
 848        struct v4l2_control ctrl;
 849
 850        memset(&ctrl, 0, sizeof(ctrl));
 851        ctrl.id = V4L2_CID_VFLIP;
 852        ctrl.value = flip;
 853        return sensor_call(cam, core, s_ctrl, &ctrl);
 854}
 855
 856
 857static int cafe_cam_configure(struct cafe_camera *cam)
 858{
 859        struct v4l2_mbus_framefmt mbus_fmt;
 860        int ret;
 861
 862        if (cam->state != S_IDLE)
 863                return -EINVAL;
 864        v4l2_fill_mbus_format(&mbus_fmt, &cam->pix_format, cam->mbus_code);
 865        ret = sensor_call(cam, core, init, 0);
 866        if (ret == 0)
 867                ret = sensor_call(cam, video, s_mbus_fmt, &mbus_fmt);
 868        /*
 869         * OV7670 does weird things if flip is set *before* format...
 870         */
 871        ret += cafe_cam_set_flip(cam);
 872        return ret;
 873}
 874
 875/* -------------------------------------------------------------------- */
 876/*
 877 * DMA buffer management.  These functions need s_mutex held.
 878 */
 879
 880/* FIXME: this is inefficient as hell, since dma_alloc_coherent just
 881 * does a get_free_pages() call, and we waste a good chunk of an orderN
 882 * allocation.  Should try to allocate the whole set in one chunk.
 883 */
 884static int cafe_alloc_dma_bufs(struct cafe_camera *cam, int loadtime)
 885{
 886        int i;
 887
 888        cafe_set_config_needed(cam, 1);
 889        if (loadtime)
 890                cam->dma_buf_size = dma_buf_size;
 891        else
 892                cam->dma_buf_size = cam->pix_format.sizeimage;
 893        if (n_dma_bufs > 3)
 894                n_dma_bufs = 3;
 895
 896        cam->nbufs = 0;
 897        for (i = 0; i < n_dma_bufs; i++) {
 898                cam->dma_bufs[i] = dma_alloc_coherent(&cam->pdev->dev,
 899                                cam->dma_buf_size, cam->dma_handles + i,
 900                                GFP_KERNEL);
 901                if (cam->dma_bufs[i] == NULL) {
 902                        cam_warn(cam, "Failed to allocate DMA buffer\n");
 903                        break;
 904                }
 905                /* For debug, remove eventually */
 906                memset(cam->dma_bufs[i], 0xcc, cam->dma_buf_size);
 907                (cam->nbufs)++;
 908        }
 909
 910        switch (cam->nbufs) {
 911        case 1:
 912            dma_free_coherent(&cam->pdev->dev, cam->dma_buf_size,
 913                            cam->dma_bufs[0], cam->dma_handles[0]);
 914            cam->nbufs = 0;
 915        case 0:
 916            cam_err(cam, "Insufficient DMA buffers, cannot operate\n");
 917            return -ENOMEM;
 918
 919        case 2:
 920            if (n_dma_bufs > 2)
 921                    cam_warn(cam, "Will limp along with only 2 buffers\n");
 922            break;
 923        }
 924        return 0;
 925}
 926
 927static void cafe_free_dma_bufs(struct cafe_camera *cam)
 928{
 929        int i;
 930
 931        for (i = 0; i < cam->nbufs; i++) {
 932                dma_free_coherent(&cam->pdev->dev, cam->dma_buf_size,
 933                                cam->dma_bufs[i], cam->dma_handles[i]);
 934                cam->dma_bufs[i] = NULL;
 935        }
 936        cam->nbufs = 0;
 937}
 938
 939
 940
 941
 942
 943/* ----------------------------------------------------------------------- */
 944/*
 945 * Here starts the V4L2 interface code.
 946 */
 947
 948/*
 949 * Read an image from the device.
 950 */
 951static ssize_t cafe_deliver_buffer(struct cafe_camera *cam,
 952                char __user *buffer, size_t len, loff_t *pos)
 953{
 954        int bufno;
 955        unsigned long flags;
 956
 957        spin_lock_irqsave(&cam->dev_lock, flags);
 958        if (cam->next_buf < 0) {
 959                cam_err(cam, "deliver_buffer: No next buffer\n");
 960                spin_unlock_irqrestore(&cam->dev_lock, flags);
 961                return -EIO;
 962        }
 963        bufno = cam->next_buf;
 964        clear_bit(bufno, &cam->flags);
 965        if (++(cam->next_buf) >= cam->nbufs)
 966                cam->next_buf = 0;
 967        if (! test_bit(cam->next_buf, &cam->flags))
 968                cam->next_buf = -1;
 969        cam->specframes = 0;
 970        spin_unlock_irqrestore(&cam->dev_lock, flags);
 971
 972        if (len > cam->pix_format.sizeimage)
 973                len = cam->pix_format.sizeimage;
 974        if (copy_to_user(buffer, cam->dma_bufs[bufno], len))
 975                return -EFAULT;
 976        (*pos) += len;
 977        return len;
 978}
 979
 980/*
 981 * Get everything ready, and start grabbing frames.
 982 */
 983static int cafe_read_setup(struct cafe_camera *cam, enum cafe_state state)
 984{
 985        int ret;
 986        unsigned long flags;
 987
 988        /*
 989         * Configuration.  If we still don't have DMA buffers,
 990         * make one last, desperate attempt.
 991         */
 992        if (cam->nbufs == 0)
 993                if (cafe_alloc_dma_bufs(cam, 0))
 994                        return -ENOMEM;
 995
 996        if (cafe_needs_config(cam)) {
 997                cafe_cam_configure(cam);
 998                ret = cafe_ctlr_configure(cam);
 999                if (ret)
1000                        return ret;
1001        }
1002
1003        /*
1004         * Turn it loose.
1005         */
1006        spin_lock_irqsave(&cam->dev_lock, flags);
1007        cafe_reset_buffers(cam);
1008        cafe_ctlr_irq_enable(cam);
1009        cam->state = state;
1010        cafe_ctlr_start(cam);
1011        spin_unlock_irqrestore(&cam->dev_lock, flags);
1012        return 0;
1013}
1014
1015
1016static ssize_t cafe_v4l_read(struct file *filp,
1017                char __user *buffer, size_t len, loff_t *pos)
1018{
1019        struct cafe_camera *cam = filp->private_data;
1020        int ret = 0;
1021
1022        /*
1023         * Perhaps we're in speculative read mode and already
1024         * have data?
1025         */
1026        mutex_lock(&cam->s_mutex);
1027        if (cam->state == S_SPECREAD) {
1028                if (cam->next_buf >= 0) {
1029                        ret = cafe_deliver_buffer(cam, buffer, len, pos);
1030                        if (ret != 0)
1031                                goto out_unlock;
1032                }
1033        } else if (cam->state == S_FLAKED || cam->state == S_NOTREADY) {
1034                ret = -EIO;
1035                goto out_unlock;
1036        } else if (cam->state != S_IDLE) {
1037                ret = -EBUSY;
1038                goto out_unlock;
1039        }
1040
1041        /*
1042         * v4l2: multiple processes can open the device, but only
1043         * one gets to grab data from it.
1044         */
1045        if (cam->owner && cam->owner != filp) {
1046                ret = -EBUSY;
1047                goto out_unlock;
1048        }
1049        cam->owner = filp;
1050
1051        /*
1052         * Do setup if need be.
1053         */
1054        if (cam->state != S_SPECREAD) {
1055                ret = cafe_read_setup(cam, S_SINGLEREAD);
1056                if (ret)
1057                        goto out_unlock;
1058        }
1059        /*
1060         * Wait for something to happen.  This should probably
1061         * be interruptible (FIXME).
1062         */
1063        wait_event_timeout(cam->iowait, cam->next_buf >= 0, HZ);
1064        if (cam->next_buf < 0) {
1065                cam_err(cam, "read() operation timed out\n");
1066                cafe_ctlr_stop_dma(cam);
1067                ret = -EIO;
1068                goto out_unlock;
1069        }
1070        /*
1071         * Give them their data and we should be done.
1072         */
1073        ret = cafe_deliver_buffer(cam, buffer, len, pos);
1074
1075  out_unlock:
1076        mutex_unlock(&cam->s_mutex);
1077        return ret;
1078}
1079
1080
1081
1082
1083
1084
1085
1086
1087/*
1088 * Streaming I/O support.
1089 */
1090
1091
1092
1093static int cafe_vidioc_streamon(struct file *filp, void *priv,
1094                enum v4l2_buf_type type)
1095{
1096        struct cafe_camera *cam = filp->private_data;
1097        int ret = -EINVAL;
1098
1099        if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1100                goto out;
1101        mutex_lock(&cam->s_mutex);
1102        if (cam->state != S_IDLE || cam->n_sbufs == 0)
1103                goto out_unlock;
1104
1105        cam->sequence = 0;
1106        ret = cafe_read_setup(cam, S_STREAMING);
1107
1108  out_unlock:
1109        mutex_unlock(&cam->s_mutex);
1110  out:
1111        return ret;
1112}
1113
1114
1115static int cafe_vidioc_streamoff(struct file *filp, void *priv,
1116                enum v4l2_buf_type type)
1117{
1118        struct cafe_camera *cam = filp->private_data;
1119        int ret = -EINVAL;
1120
1121        if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1122                goto out;
1123        mutex_lock(&cam->s_mutex);
1124        if (cam->state != S_STREAMING)
1125                goto out_unlock;
1126
1127        cafe_ctlr_stop_dma(cam);
1128        ret = 0;
1129
1130  out_unlock:
1131        mutex_unlock(&cam->s_mutex);
1132  out:
1133        return ret;
1134}
1135
1136
1137
1138static int cafe_setup_siobuf(struct cafe_camera *cam, int index)
1139{
1140        struct cafe_sio_buffer *buf = cam->sb_bufs + index;
1141
1142        INIT_LIST_HEAD(&buf->list);
1143        buf->v4lbuf.length = PAGE_ALIGN(cam->pix_format.sizeimage);
1144        buf->buffer = vmalloc_user(buf->v4lbuf.length);
1145        if (buf->buffer == NULL)
1146                return -ENOMEM;
1147        buf->mapcount = 0;
1148        buf->cam = cam;
1149
1150        buf->v4lbuf.index = index;
1151        buf->v4lbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1152        buf->v4lbuf.field = V4L2_FIELD_NONE;
1153        buf->v4lbuf.memory = V4L2_MEMORY_MMAP;
1154        /*
1155         * Offset: must be 32-bit even on a 64-bit system.  videobuf-dma-sg
1156         * just uses the length times the index, but the spec warns
1157         * against doing just that - vma merging problems.  So we
1158         * leave a gap between each pair of buffers.
1159         */
1160        buf->v4lbuf.m.offset = 2*index*buf->v4lbuf.length;
1161        return 0;
1162}
1163
1164static int cafe_free_sio_buffers(struct cafe_camera *cam)
1165{
1166        int i;
1167
1168        /*
1169         * If any buffers are mapped, we cannot free them at all.
1170         */
1171        for (i = 0; i < cam->n_sbufs; i++)
1172                if (cam->sb_bufs[i].mapcount > 0)
1173                        return -EBUSY;
1174        /*
1175         * OK, let's do it.
1176         */
1177        for (i = 0; i < cam->n_sbufs; i++)
1178                vfree(cam->sb_bufs[i].buffer);
1179        cam->n_sbufs = 0;
1180        kfree(cam->sb_bufs);
1181        cam->sb_bufs = NULL;
1182        INIT_LIST_HEAD(&cam->sb_avail);
1183        INIT_LIST_HEAD(&cam->sb_full);
1184        return 0;
1185}
1186
1187
1188
1189static int cafe_vidioc_reqbufs(struct file *filp, void *priv,
1190                struct v4l2_requestbuffers *req)
1191{
1192        struct cafe_camera *cam = filp->private_data;
1193        int ret = 0;  /* Silence warning */
1194
1195        /*
1196         * Make sure it's something we can do.  User pointers could be
1197         * implemented without great pain, but that's not been done yet.
1198         */
1199        if (req->memory != V4L2_MEMORY_MMAP)
1200                return -EINVAL;
1201        /*
1202         * If they ask for zero buffers, they really want us to stop streaming
1203         * (if it's happening) and free everything.  Should we check owner?
1204         */
1205        mutex_lock(&cam->s_mutex);
1206        if (req->count == 0) {
1207                if (cam->state == S_STREAMING)
1208                        cafe_ctlr_stop_dma(cam);
1209                ret = cafe_free_sio_buffers (cam);
1210                goto out;
1211        }
1212        /*
1213         * Device needs to be idle and working.  We *could* try to do the
1214         * right thing in S_SPECREAD by shutting things down, but it
1215         * probably doesn't matter.
1216         */
1217        if (cam->state != S_IDLE || (cam->owner && cam->owner != filp)) {
1218                ret = -EBUSY;
1219                goto out;
1220        }
1221        cam->owner = filp;
1222
1223        if (req->count < min_buffers)
1224                req->count = min_buffers;
1225        else if (req->count > max_buffers)
1226                req->count = max_buffers;
1227        if (cam->n_sbufs > 0) {
1228                ret = cafe_free_sio_buffers(cam);
1229                if (ret)
1230                        goto out;
1231        }
1232
1233        cam->sb_bufs = kzalloc(req->count*sizeof(struct cafe_sio_buffer),
1234                        GFP_KERNEL);
1235        if (cam->sb_bufs == NULL) {
1236                ret = -ENOMEM;
1237                goto out;
1238        }
1239        for (cam->n_sbufs = 0; cam->n_sbufs < req->count; (cam->n_sbufs++)) {
1240                ret = cafe_setup_siobuf(cam, cam->n_sbufs);
1241                if (ret)
1242                        break;
1243        }
1244
1245        if (cam->n_sbufs == 0)  /* no luck at all - ret already set */
1246                kfree(cam->sb_bufs);
1247        req->count = cam->n_sbufs;  /* In case of partial success */
1248
1249  out:
1250        mutex_unlock(&cam->s_mutex);
1251        return ret;
1252}
1253
1254
1255static int cafe_vidioc_querybuf(struct file *filp, void *priv,
1256                struct v4l2_buffer *buf)
1257{
1258        struct cafe_camera *cam = filp->private_data;
1259        int ret = -EINVAL;
1260
1261        mutex_lock(&cam->s_mutex);
1262        if (buf->index >= cam->n_sbufs)
1263                goto out;
1264        *buf = cam->sb_bufs[buf->index].v4lbuf;
1265        ret = 0;
1266  out:
1267        mutex_unlock(&cam->s_mutex);
1268        return ret;
1269}
1270
1271static int cafe_vidioc_qbuf(struct file *filp, void *priv,
1272                struct v4l2_buffer *buf)
1273{
1274        struct cafe_camera *cam = filp->private_data;
1275        struct cafe_sio_buffer *sbuf;
1276        int ret = -EINVAL;
1277        unsigned long flags;
1278
1279        mutex_lock(&cam->s_mutex);
1280        if (buf->index >= cam->n_sbufs)
1281                goto out;
1282        sbuf = cam->sb_bufs + buf->index;
1283        if (sbuf->v4lbuf.flags & V4L2_BUF_FLAG_QUEUED) {
1284                ret = 0; /* Already queued?? */
1285                goto out;
1286        }
1287        if (sbuf->v4lbuf.flags & V4L2_BUF_FLAG_DONE) {
1288                /* Spec doesn't say anything, seems appropriate tho */
1289                ret = -EBUSY;
1290                goto out;
1291        }
1292        sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_QUEUED;
1293        spin_lock_irqsave(&cam->dev_lock, flags);
1294        list_add(&sbuf->list, &cam->sb_avail);
1295        spin_unlock_irqrestore(&cam->dev_lock, flags);
1296        ret = 0;
1297  out:
1298        mutex_unlock(&cam->s_mutex);
1299        return ret;
1300}
1301
1302static int cafe_vidioc_dqbuf(struct file *filp, void *priv,
1303                struct v4l2_buffer *buf)
1304{
1305        struct cafe_camera *cam = filp->private_data;
1306        struct cafe_sio_buffer *sbuf;
1307        int ret = -EINVAL;
1308        unsigned long flags;
1309
1310        mutex_lock(&cam->s_mutex);
1311        if (cam->state != S_STREAMING)
1312                goto out_unlock;
1313        if (list_empty(&cam->sb_full) && filp->f_flags & O_NONBLOCK) {
1314                ret = -EAGAIN;
1315                goto out_unlock;
1316        }
1317
1318        while (list_empty(&cam->sb_full) && cam->state == S_STREAMING) {
1319                mutex_unlock(&cam->s_mutex);
1320                if (wait_event_interruptible(cam->iowait,
1321                                                !list_empty(&cam->sb_full))) {
1322                        ret = -ERESTARTSYS;
1323                        goto out;
1324                }
1325                mutex_lock(&cam->s_mutex);
1326        }
1327
1328        if (cam->state != S_STREAMING)
1329                ret = -EINTR;
1330        else {
1331                spin_lock_irqsave(&cam->dev_lock, flags);
1332                /* Should probably recheck !list_empty() here */
1333                sbuf = list_entry(cam->sb_full.next,
1334                                struct cafe_sio_buffer, list);
1335                list_del_init(&sbuf->list);
1336                spin_unlock_irqrestore(&cam->dev_lock, flags);
1337                sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_DONE;
1338                *buf = sbuf->v4lbuf;
1339                ret = 0;
1340        }
1341
1342  out_unlock:
1343        mutex_unlock(&cam->s_mutex);
1344  out:
1345        return ret;
1346}
1347
1348
1349
1350static void cafe_v4l_vm_open(struct vm_area_struct *vma)
1351{
1352        struct cafe_sio_buffer *sbuf = vma->vm_private_data;
1353        /*
1354         * Locking: done under mmap_sem, so we don't need to
1355         * go back to the camera lock here.
1356         */
1357        sbuf->mapcount++;
1358}
1359
1360
1361static void cafe_v4l_vm_close(struct vm_area_struct *vma)
1362{
1363        struct cafe_sio_buffer *sbuf = vma->vm_private_data;
1364
1365        mutex_lock(&sbuf->cam->s_mutex);
1366        sbuf->mapcount--;
1367        /* Docs say we should stop I/O too... */
1368        if (sbuf->mapcount == 0)
1369                sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_MAPPED;
1370        mutex_unlock(&sbuf->cam->s_mutex);
1371}
1372
1373static const struct vm_operations_struct cafe_v4l_vm_ops = {
1374        .open = cafe_v4l_vm_open,
1375        .close = cafe_v4l_vm_close
1376};
1377
1378
1379static int cafe_v4l_mmap(struct file *filp, struct vm_area_struct *vma)
1380{
1381        struct cafe_camera *cam = filp->private_data;
1382        unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
1383        int ret = -EINVAL;
1384        int i;
1385        struct cafe_sio_buffer *sbuf = NULL;
1386
1387        if (! (vma->vm_flags & VM_WRITE) || ! (vma->vm_flags & VM_SHARED))
1388                return -EINVAL;
1389        /*
1390         * Find the buffer they are looking for.
1391         */
1392        mutex_lock(&cam->s_mutex);
1393        for (i = 0; i < cam->n_sbufs; i++)
1394                if (cam->sb_bufs[i].v4lbuf.m.offset == offset) {
1395                        sbuf = cam->sb_bufs + i;
1396                        break;
1397                }
1398        if (sbuf == NULL)
1399                goto out;
1400
1401        ret = remap_vmalloc_range(vma, sbuf->buffer, 0);
1402        if (ret)
1403                goto out;
1404        vma->vm_flags |= VM_DONTEXPAND;
1405        vma->vm_private_data = sbuf;
1406        vma->vm_ops = &cafe_v4l_vm_ops;
1407        sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_MAPPED;
1408        cafe_v4l_vm_open(vma);
1409        ret = 0;
1410  out:
1411        mutex_unlock(&cam->s_mutex);
1412        return ret;
1413}
1414
1415
1416
1417static int cafe_v4l_open(struct file *filp)
1418{
1419        struct cafe_camera *cam = video_drvdata(filp);
1420
1421        filp->private_data = cam;
1422
1423        mutex_lock(&cam->s_mutex);
1424        if (cam->users == 0) {
1425                cafe_ctlr_power_up(cam);
1426                __cafe_cam_reset(cam);
1427                cafe_set_config_needed(cam, 1);
1428        /* FIXME make sure this is complete */
1429        }
1430        (cam->users)++;
1431        mutex_unlock(&cam->s_mutex);
1432        return 0;
1433}
1434
1435
1436static int cafe_v4l_release(struct file *filp)
1437{
1438        struct cafe_camera *cam = filp->private_data;
1439
1440        mutex_lock(&cam->s_mutex);
1441        (cam->users)--;
1442        if (filp == cam->owner) {
1443                cafe_ctlr_stop_dma(cam);
1444                cafe_free_sio_buffers(cam);
1445                cam->owner = NULL;
1446        }
1447        if (cam->users == 0) {
1448                cafe_ctlr_power_down(cam);
1449                if (alloc_bufs_at_read)
1450                        cafe_free_dma_bufs(cam);
1451        }
1452        mutex_unlock(&cam->s_mutex);
1453        return 0;
1454}
1455
1456
1457
1458static unsigned int cafe_v4l_poll(struct file *filp,
1459                struct poll_table_struct *pt)
1460{
1461        struct cafe_camera *cam = filp->private_data;
1462
1463        poll_wait(filp, &cam->iowait, pt);
1464        if (cam->next_buf >= 0)
1465                return POLLIN | POLLRDNORM;
1466        return 0;
1467}
1468
1469
1470
1471static int cafe_vidioc_queryctrl(struct file *filp, void *priv,
1472                struct v4l2_queryctrl *qc)
1473{
1474        struct cafe_camera *cam = priv;
1475        int ret;
1476
1477        mutex_lock(&cam->s_mutex);
1478        ret = sensor_call(cam, core, queryctrl, qc);
1479        mutex_unlock(&cam->s_mutex);
1480        return ret;
1481}
1482
1483
1484static int cafe_vidioc_g_ctrl(struct file *filp, void *priv,
1485                struct v4l2_control *ctrl)
1486{
1487        struct cafe_camera *cam = priv;
1488        int ret;
1489
1490        mutex_lock(&cam->s_mutex);
1491        ret = sensor_call(cam, core, g_ctrl, ctrl);
1492        mutex_unlock(&cam->s_mutex);
1493        return ret;
1494}
1495
1496
1497static int cafe_vidioc_s_ctrl(struct file *filp, void *priv,
1498                struct v4l2_control *ctrl)
1499{
1500        struct cafe_camera *cam = priv;
1501        int ret;
1502
1503        mutex_lock(&cam->s_mutex);
1504        ret = sensor_call(cam, core, s_ctrl, ctrl);
1505        mutex_unlock(&cam->s_mutex);
1506        return ret;
1507}
1508
1509
1510
1511
1512
1513static int cafe_vidioc_querycap(struct file *file, void *priv,
1514                struct v4l2_capability *cap)
1515{
1516        strcpy(cap->driver, "cafe_ccic");
1517        strcpy(cap->card, "cafe_ccic");
1518        cap->version = CAFE_VERSION;
1519        cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
1520                V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1521        return 0;
1522}
1523
1524
1525/*
1526 * The default format we use until somebody says otherwise.
1527 */
1528static const struct v4l2_pix_format cafe_def_pix_format = {
1529        .width          = VGA_WIDTH,
1530        .height         = VGA_HEIGHT,
1531        .pixelformat    = V4L2_PIX_FMT_YUYV,
1532        .field          = V4L2_FIELD_NONE,
1533        .bytesperline   = VGA_WIDTH*2,
1534        .sizeimage      = VGA_WIDTH*VGA_HEIGHT*2,
1535};
1536
1537static const enum v4l2_mbus_pixelcode cafe_def_mbus_code =
1538                                        V4L2_MBUS_FMT_YUYV8_2X8;
1539
1540static int cafe_vidioc_enum_fmt_vid_cap(struct file *filp,
1541                void *priv, struct v4l2_fmtdesc *fmt)
1542{
1543        if (fmt->index >= N_CAFE_FMTS)
1544                return -EINVAL;
1545        strlcpy(fmt->description, cafe_formats[fmt->index].desc,
1546                        sizeof(fmt->description));
1547        fmt->pixelformat = cafe_formats[fmt->index].pixelformat;
1548        return 0;
1549}
1550
1551static int cafe_vidioc_try_fmt_vid_cap(struct file *filp, void *priv,
1552                struct v4l2_format *fmt)
1553{
1554        struct cafe_camera *cam = priv;
1555        struct cafe_format_struct *f;
1556        struct v4l2_pix_format *pix = &fmt->fmt.pix;
1557        struct v4l2_mbus_framefmt mbus_fmt;
1558        int ret;
1559
1560        f = cafe_find_format(pix->pixelformat);
1561        pix->pixelformat = f->pixelformat;
1562        v4l2_fill_mbus_format(&mbus_fmt, pix, f->mbus_code);
1563        mutex_lock(&cam->s_mutex);
1564        ret = sensor_call(cam, video, try_mbus_fmt, &mbus_fmt);
1565        mutex_unlock(&cam->s_mutex);
1566        v4l2_fill_pix_format(pix, &mbus_fmt);
1567        pix->bytesperline = pix->width * f->bpp;
1568        pix->sizeimage = pix->height * pix->bytesperline;
1569        return ret;
1570}
1571
1572static int cafe_vidioc_s_fmt_vid_cap(struct file *filp, void *priv,
1573                struct v4l2_format *fmt)
1574{
1575        struct cafe_camera *cam = priv;
1576        struct cafe_format_struct *f;
1577        int ret;
1578
1579        /*
1580         * Can't do anything if the device is not idle
1581         * Also can't if there are streaming buffers in place.
1582         */
1583        if (cam->state != S_IDLE || cam->n_sbufs > 0)
1584                return -EBUSY;
1585
1586        f = cafe_find_format(fmt->fmt.pix.pixelformat);
1587
1588        /*
1589         * See if the formatting works in principle.
1590         */
1591        ret = cafe_vidioc_try_fmt_vid_cap(filp, priv, fmt);
1592        if (ret)
1593                return ret;
1594        /*
1595         * Now we start to change things for real, so let's do it
1596         * under lock.
1597         */
1598        mutex_lock(&cam->s_mutex);
1599        cam->pix_format = fmt->fmt.pix;
1600        cam->mbus_code = f->mbus_code;
1601
1602        /*
1603         * Make sure we have appropriate DMA buffers.
1604         */
1605        ret = -ENOMEM;
1606        if (cam->nbufs > 0 && cam->dma_buf_size < cam->pix_format.sizeimage)
1607                cafe_free_dma_bufs(cam);
1608        if (cam->nbufs == 0) {
1609                if (cafe_alloc_dma_bufs(cam, 0))
1610                        goto out;
1611        }
1612        /*
1613         * It looks like this might work, so let's program the sensor.
1614         */
1615        ret = cafe_cam_configure(cam);
1616        if (! ret)
1617                ret = cafe_ctlr_configure(cam);
1618  out:
1619        mutex_unlock(&cam->s_mutex);
1620        return ret;
1621}
1622
1623/*
1624 * Return our stored notion of how the camera is/should be configured.
1625 * The V4l2 spec wants us to be smarter, and actually get this from
1626 * the camera (and not mess with it at open time).  Someday.
1627 */
1628static int cafe_vidioc_g_fmt_vid_cap(struct file *filp, void *priv,
1629                struct v4l2_format *f)
1630{
1631        struct cafe_camera *cam = priv;
1632
1633        f->fmt.pix = cam->pix_format;
1634        return 0;
1635}
1636
1637/*
1638 * We only have one input - the sensor - so minimize the nonsense here.
1639 */
1640static int cafe_vidioc_enum_input(struct file *filp, void *priv,
1641                struct v4l2_input *input)
1642{
1643        if (input->index != 0)
1644                return -EINVAL;
1645
1646        input->type = V4L2_INPUT_TYPE_CAMERA;
1647        input->std = V4L2_STD_ALL; /* Not sure what should go here */
1648        strcpy(input->name, "Camera");
1649        return 0;
1650}
1651
1652static int cafe_vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
1653{
1654        *i = 0;
1655        return 0;
1656}
1657
1658static int cafe_vidioc_s_input(struct file *filp, void *priv, unsigned int i)
1659{
1660        if (i != 0)
1661                return -EINVAL;
1662        return 0;
1663}
1664
1665/* from vivi.c */
1666static int cafe_vidioc_s_std(struct file *filp, void *priv, v4l2_std_id *a)
1667{
1668        return 0;
1669}
1670
1671/*
1672 * G/S_PARM.  Most of this is done by the sensor, but we are
1673 * the level which controls the number of read buffers.
1674 */
1675static int cafe_vidioc_g_parm(struct file *filp, void *priv,
1676                struct v4l2_streamparm *parms)
1677{
1678        struct cafe_camera *cam = priv;
1679        int ret;
1680
1681        mutex_lock(&cam->s_mutex);
1682        ret = sensor_call(cam, video, g_parm, parms);
1683        mutex_unlock(&cam->s_mutex);
1684        parms->parm.capture.readbuffers = n_dma_bufs;
1685        return ret;
1686}
1687
1688static int cafe_vidioc_s_parm(struct file *filp, void *priv,
1689                struct v4l2_streamparm *parms)
1690{
1691        struct cafe_camera *cam = priv;
1692        int ret;
1693
1694        mutex_lock(&cam->s_mutex);
1695        ret = sensor_call(cam, video, s_parm, parms);
1696        mutex_unlock(&cam->s_mutex);
1697        parms->parm.capture.readbuffers = n_dma_bufs;
1698        return ret;
1699}
1700
1701static int cafe_vidioc_g_chip_ident(struct file *file, void *priv,
1702                struct v4l2_dbg_chip_ident *chip)
1703{
1704        struct cafe_camera *cam = priv;
1705
1706        chip->ident = V4L2_IDENT_NONE;
1707        chip->revision = 0;
1708        if (v4l2_chip_match_host(&chip->match)) {
1709                chip->ident = V4L2_IDENT_CAFE;
1710                return 0;
1711        }
1712        return sensor_call(cam, core, g_chip_ident, chip);
1713}
1714
1715static int cafe_vidioc_enum_framesizes(struct file *filp, void *priv,
1716                struct v4l2_frmsizeenum *sizes)
1717{
1718        struct cafe_camera *cam = priv;
1719        int ret;
1720
1721        mutex_lock(&cam->s_mutex);
1722        ret = sensor_call(cam, video, enum_framesizes, sizes);
1723        mutex_unlock(&cam->s_mutex);
1724        return ret;
1725}
1726
1727static int cafe_vidioc_enum_frameintervals(struct file *filp, void *priv,
1728                struct v4l2_frmivalenum *interval)
1729{
1730        struct cafe_camera *cam = priv;
1731        int ret;
1732
1733        mutex_lock(&cam->s_mutex);
1734        ret = sensor_call(cam, video, enum_frameintervals, interval);
1735        mutex_unlock(&cam->s_mutex);
1736        return ret;
1737}
1738
1739#ifdef CONFIG_VIDEO_ADV_DEBUG
1740static int cafe_vidioc_g_register(struct file *file, void *priv,
1741                struct v4l2_dbg_register *reg)
1742{
1743        struct cafe_camera *cam = priv;
1744
1745        if (v4l2_chip_match_host(&reg->match)) {
1746                reg->val = cafe_reg_read(cam, reg->reg);
1747                reg->size = 4;
1748                return 0;
1749        }
1750        return sensor_call(cam, core, g_register, reg);
1751}
1752
1753static int cafe_vidioc_s_register(struct file *file, void *priv,
1754                struct v4l2_dbg_register *reg)
1755{
1756        struct cafe_camera *cam = priv;
1757
1758        if (v4l2_chip_match_host(&reg->match)) {
1759                cafe_reg_write(cam, reg->reg, reg->val);
1760                return 0;
1761        }
1762        return sensor_call(cam, core, s_register, reg);
1763}
1764#endif
1765
1766/*
1767 * This template device holds all of those v4l2 methods; we
1768 * clone it for specific real devices.
1769 */
1770
1771static const struct v4l2_file_operations cafe_v4l_fops = {
1772        .owner = THIS_MODULE,
1773        .open = cafe_v4l_open,
1774        .release = cafe_v4l_release,
1775        .read = cafe_v4l_read,
1776        .poll = cafe_v4l_poll,
1777        .mmap = cafe_v4l_mmap,
1778        .unlocked_ioctl = video_ioctl2,
1779};
1780
1781static const struct v4l2_ioctl_ops cafe_v4l_ioctl_ops = {
1782        .vidioc_querycap        = cafe_vidioc_querycap,
1783        .vidioc_enum_fmt_vid_cap = cafe_vidioc_enum_fmt_vid_cap,
1784        .vidioc_try_fmt_vid_cap = cafe_vidioc_try_fmt_vid_cap,
1785        .vidioc_s_fmt_vid_cap   = cafe_vidioc_s_fmt_vid_cap,
1786        .vidioc_g_fmt_vid_cap   = cafe_vidioc_g_fmt_vid_cap,
1787        .vidioc_enum_input      = cafe_vidioc_enum_input,
1788        .vidioc_g_input         = cafe_vidioc_g_input,
1789        .vidioc_s_input         = cafe_vidioc_s_input,
1790        .vidioc_s_std           = cafe_vidioc_s_std,
1791        .vidioc_reqbufs         = cafe_vidioc_reqbufs,
1792        .vidioc_querybuf        = cafe_vidioc_querybuf,
1793        .vidioc_qbuf            = cafe_vidioc_qbuf,
1794        .vidioc_dqbuf           = cafe_vidioc_dqbuf,
1795        .vidioc_streamon        = cafe_vidioc_streamon,
1796        .vidioc_streamoff       = cafe_vidioc_streamoff,
1797        .vidioc_queryctrl       = cafe_vidioc_queryctrl,
1798        .vidioc_g_ctrl          = cafe_vidioc_g_ctrl,
1799        .vidioc_s_ctrl          = cafe_vidioc_s_ctrl,
1800        .vidioc_g_parm          = cafe_vidioc_g_parm,
1801        .vidioc_s_parm          = cafe_vidioc_s_parm,
1802        .vidioc_enum_framesizes = cafe_vidioc_enum_framesizes,
1803        .vidioc_enum_frameintervals = cafe_vidioc_enum_frameintervals,
1804        .vidioc_g_chip_ident    = cafe_vidioc_g_chip_ident,
1805#ifdef CONFIG_VIDEO_ADV_DEBUG
1806        .vidioc_g_register      = cafe_vidioc_g_register,
1807        .vidioc_s_register      = cafe_vidioc_s_register,
1808#endif
1809};
1810
1811static struct video_device cafe_v4l_template = {
1812        .name = "cafe",
1813        .tvnorms = V4L2_STD_NTSC_M,
1814        .current_norm = V4L2_STD_NTSC_M,  /* make mplayer happy */
1815
1816        .fops = &cafe_v4l_fops,
1817        .ioctl_ops = &cafe_v4l_ioctl_ops,
1818        .release = video_device_release_empty,
1819};
1820
1821
1822/* ---------------------------------------------------------------------- */
1823/*
1824 * Interrupt handler stuff
1825 */
1826
1827
1828
1829static void cafe_frame_tasklet(unsigned long data)
1830{
1831        struct cafe_camera *cam = (struct cafe_camera *) data;
1832        int i;
1833        unsigned long flags;
1834        struct cafe_sio_buffer *sbuf;
1835
1836        spin_lock_irqsave(&cam->dev_lock, flags);
1837        for (i = 0; i < cam->nbufs; i++) {
1838                int bufno = cam->next_buf;
1839                if (bufno < 0) {  /* "will never happen" */
1840                        cam_err(cam, "No valid bufs in tasklet!\n");
1841                        break;
1842                }
1843                if (++(cam->next_buf) >= cam->nbufs)
1844                        cam->next_buf = 0;
1845                if (! test_bit(bufno, &cam->flags))
1846                        continue;
1847                if (list_empty(&cam->sb_avail))
1848                        break;  /* Leave it valid, hope for better later */
1849                clear_bit(bufno, &cam->flags);
1850                sbuf = list_entry(cam->sb_avail.next,
1851                                struct cafe_sio_buffer, list);
1852                /*
1853                 * Drop the lock during the big copy.  This *should* be safe...
1854                 */
1855                spin_unlock_irqrestore(&cam->dev_lock, flags);
1856                memcpy(sbuf->buffer, cam->dma_bufs[bufno],
1857                                cam->pix_format.sizeimage);
1858                sbuf->v4lbuf.bytesused = cam->pix_format.sizeimage;
1859                sbuf->v4lbuf.sequence = cam->buf_seq[bufno];
1860                sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_QUEUED;
1861                sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_DONE;
1862                spin_lock_irqsave(&cam->dev_lock, flags);
1863                list_move_tail(&sbuf->list, &cam->sb_full);
1864        }
1865        if (! list_empty(&cam->sb_full))
1866                wake_up(&cam->iowait);
1867        spin_unlock_irqrestore(&cam->dev_lock, flags);
1868}
1869
1870
1871
1872static void cafe_frame_complete(struct cafe_camera *cam, int frame)
1873{
1874        /*
1875         * Basic frame housekeeping.
1876         */
1877        if (test_bit(frame, &cam->flags) && printk_ratelimit())
1878                cam_err(cam, "Frame overrun on %d, frames lost\n", frame);
1879        set_bit(frame, &cam->flags);
1880        clear_bit(CF_DMA_ACTIVE, &cam->flags);
1881        if (cam->next_buf < 0)
1882                cam->next_buf = frame;
1883        cam->buf_seq[frame] = ++(cam->sequence);
1884
1885        switch (cam->state) {
1886        /*
1887         * If in single read mode, try going speculative.
1888         */
1889            case S_SINGLEREAD:
1890                cam->state = S_SPECREAD;
1891                cam->specframes = 0;
1892                wake_up(&cam->iowait);
1893                break;
1894
1895        /*
1896         * If we are already doing speculative reads, and nobody is
1897         * reading them, just stop.
1898         */
1899            case S_SPECREAD:
1900                if (++(cam->specframes) >= cam->nbufs) {
1901                        cafe_ctlr_stop(cam);
1902                        cafe_ctlr_irq_disable(cam);
1903                        cam->state = S_IDLE;
1904                }
1905                wake_up(&cam->iowait);
1906                break;
1907        /*
1908         * For the streaming case, we defer the real work to the
1909         * camera tasklet.
1910         *
1911         * FIXME: if the application is not consuming the buffers,
1912         * we should eventually put things on hold and restart in
1913         * vidioc_dqbuf().
1914         */
1915            case S_STREAMING:
1916                tasklet_schedule(&cam->s_tasklet);
1917                break;
1918
1919            default:
1920                cam_err(cam, "Frame interrupt in non-operational state\n");
1921                break;
1922        }
1923}
1924
1925
1926
1927
1928static void cafe_frame_irq(struct cafe_camera *cam, unsigned int irqs)
1929{
1930        unsigned int frame;
1931
1932        cafe_reg_write(cam, REG_IRQSTAT, FRAMEIRQS); /* Clear'em all */
1933        /*
1934         * Handle any frame completions.  There really should
1935         * not be more than one of these, or we have fallen
1936         * far behind.
1937         */
1938        for (frame = 0; frame < cam->nbufs; frame++)
1939                if (irqs & (IRQ_EOF0 << frame))
1940                        cafe_frame_complete(cam, frame);
1941        /*
1942         * If a frame starts, note that we have DMA active.  This
1943         * code assumes that we won't get multiple frame interrupts
1944         * at once; may want to rethink that.
1945         */
1946        if (irqs & (IRQ_SOF0 | IRQ_SOF1 | IRQ_SOF2))
1947                set_bit(CF_DMA_ACTIVE, &cam->flags);
1948}
1949
1950
1951
1952static irqreturn_t cafe_irq(int irq, void *data)
1953{
1954        struct cafe_camera *cam = data;
1955        unsigned int irqs;
1956
1957        spin_lock(&cam->dev_lock);
1958        irqs = cafe_reg_read(cam, REG_IRQSTAT);
1959        if ((irqs & ALLIRQS) == 0) {
1960                spin_unlock(&cam->dev_lock);
1961                return IRQ_NONE;
1962        }
1963        if (irqs & FRAMEIRQS)
1964                cafe_frame_irq(cam, irqs);
1965        if (irqs & TWSIIRQS) {
1966                cafe_reg_write(cam, REG_IRQSTAT, TWSIIRQS);
1967                wake_up(&cam->smbus_wait);
1968        }
1969        spin_unlock(&cam->dev_lock);
1970        return IRQ_HANDLED;
1971}
1972
1973
1974/* -------------------------------------------------------------------------- */
1975/*
1976 * PCI interface stuff.
1977 */
1978
1979static const struct dmi_system_id olpc_xo1_dmi[] = {
1980        {
1981                .matches = {
1982                        DMI_MATCH(DMI_SYS_VENDOR, "OLPC"),
1983                        DMI_MATCH(DMI_PRODUCT_NAME, "XO"),
1984                        DMI_MATCH(DMI_PRODUCT_VERSION, "1"),
1985                },
1986        },
1987        { }
1988};
1989
1990static int cafe_pci_probe(struct pci_dev *pdev,
1991                const struct pci_device_id *id)
1992{
1993        int ret;
1994        struct cafe_camera *cam;
1995        struct ov7670_config sensor_cfg = {
1996                /* This controller only does SMBUS */
1997                .use_smbus = true,
1998
1999                /*
2000                 * Exclude QCIF mode, because it only captures a tiny portion
2001                 * of the sensor FOV
2002                 */
2003                .min_width = 320,
2004                .min_height = 240,
2005        };
2006
2007        /*
2008         * Start putting together one of our big camera structures.
2009         */
2010        ret = -ENOMEM;
2011        cam = kzalloc(sizeof(struct cafe_camera), GFP_KERNEL);
2012        if (cam == NULL)
2013                goto out;
2014        ret = v4l2_device_register(&pdev->dev, &cam->v4l2_dev);
2015        if (ret)
2016                goto out_free;
2017
2018        mutex_init(&cam->s_mutex);
2019        spin_lock_init(&cam->dev_lock);
2020        cam->state = S_NOTREADY;
2021        cafe_set_config_needed(cam, 1);
2022        init_waitqueue_head(&cam->smbus_wait);
2023        init_waitqueue_head(&cam->iowait);
2024        cam->pdev = pdev;
2025        cam->pix_format = cafe_def_pix_format;
2026        cam->mbus_code = cafe_def_mbus_code;
2027        INIT_LIST_HEAD(&cam->dev_list);
2028        INIT_LIST_HEAD(&cam->sb_avail);
2029        INIT_LIST_HEAD(&cam->sb_full);
2030        tasklet_init(&cam->s_tasklet, cafe_frame_tasklet, (unsigned long) cam);
2031        /*
2032         * Get set up on the PCI bus.
2033         */
2034        ret = pci_enable_device(pdev);
2035        if (ret)
2036                goto out_unreg;
2037        pci_set_master(pdev);
2038
2039        ret = -EIO;
2040        cam->regs = pci_iomap(pdev, 0, 0);
2041        if (! cam->regs) {
2042                printk(KERN_ERR "Unable to ioremap cafe-ccic regs\n");
2043                goto out_unreg;
2044        }
2045        ret = request_irq(pdev->irq, cafe_irq, IRQF_SHARED, "cafe-ccic", cam);
2046        if (ret)
2047                goto out_iounmap;
2048        /*
2049         * Initialize the controller and leave it powered up.  It will
2050         * stay that way until the sensor driver shows up.
2051         */
2052        cafe_ctlr_init(cam);
2053        cafe_ctlr_power_up(cam);
2054        /*
2055         * Set up I2C/SMBUS communications.  We have to drop the mutex here
2056         * because the sensor could attach in this call chain, leading to
2057         * unsightly deadlocks.
2058         */
2059        ret = cafe_smbus_setup(cam);
2060        if (ret)
2061                goto out_freeirq;
2062
2063        /* Apply XO-1 clock speed */
2064        if (dmi_check_system(olpc_xo1_dmi))
2065                sensor_cfg.clock_speed = 45;
2066
2067        cam->sensor_addr = 0x42;
2068        cam->sensor = v4l2_i2c_new_subdev_cfg(&cam->v4l2_dev, &cam->i2c_adapter,
2069                        "ov7670", 0, &sensor_cfg, cam->sensor_addr, NULL);
2070        if (cam->sensor == NULL) {
2071                ret = -ENODEV;
2072                goto out_smbus;
2073        }
2074
2075        ret = cafe_cam_init(cam);
2076        if (ret)
2077                goto out_smbus;
2078
2079        /*
2080         * Get the v4l2 setup done.
2081         */
2082        mutex_lock(&cam->s_mutex);
2083        cam->vdev = cafe_v4l_template;
2084        cam->vdev.debug = 0;
2085/*      cam->vdev.debug = V4L2_DEBUG_IOCTL_ARG;*/
2086        cam->vdev.v4l2_dev = &cam->v4l2_dev;
2087        ret = video_register_device(&cam->vdev, VFL_TYPE_GRABBER, -1);
2088        if (ret)
2089                goto out_unlock;
2090        video_set_drvdata(&cam->vdev, cam);
2091
2092        /*
2093         * If so requested, try to get our DMA buffers now.
2094         */
2095        if (!alloc_bufs_at_read) {
2096                if (cafe_alloc_dma_bufs(cam, 1))
2097                        cam_warn(cam, "Unable to alloc DMA buffers at load"
2098                                        " will try again later.");
2099        }
2100
2101        mutex_unlock(&cam->s_mutex);
2102        return 0;
2103
2104out_unlock:
2105        mutex_unlock(&cam->s_mutex);
2106out_smbus:
2107        cafe_smbus_shutdown(cam);
2108out_freeirq:
2109        cafe_ctlr_power_down(cam);
2110        free_irq(pdev->irq, cam);
2111out_iounmap:
2112        pci_iounmap(pdev, cam->regs);
2113out_free:
2114        v4l2_device_unregister(&cam->v4l2_dev);
2115out_unreg:
2116        kfree(cam);
2117out:
2118        return ret;
2119}
2120
2121
2122/*
2123 * Shut down an initialized device
2124 */
2125static void cafe_shutdown(struct cafe_camera *cam)
2126{
2127/* FIXME: Make sure we take care of everything here */
2128        if (cam->n_sbufs > 0)
2129                /* What if they are still mapped?  Shouldn't be, but... */
2130                cafe_free_sio_buffers(cam);
2131        cafe_ctlr_stop_dma(cam);
2132        cafe_ctlr_power_down(cam);
2133        cafe_smbus_shutdown(cam);
2134        cafe_free_dma_bufs(cam);
2135        free_irq(cam->pdev->irq, cam);
2136        pci_iounmap(cam->pdev, cam->regs);
2137        video_unregister_device(&cam->vdev);
2138}
2139
2140
2141static void cafe_pci_remove(struct pci_dev *pdev)
2142{
2143        struct v4l2_device *v4l2_dev = dev_get_drvdata(&pdev->dev);
2144        struct cafe_camera *cam = to_cam(v4l2_dev);
2145
2146        if (cam == NULL) {
2147                printk(KERN_WARNING "pci_remove on unknown pdev %p\n", pdev);
2148                return;
2149        }
2150        mutex_lock(&cam->s_mutex);
2151        if (cam->users > 0)
2152                cam_warn(cam, "Removing a device with users!\n");
2153        cafe_shutdown(cam);
2154        v4l2_device_unregister(&cam->v4l2_dev);
2155        kfree(cam);
2156/* No unlock - it no longer exists */
2157}
2158
2159
2160#ifdef CONFIG_PM
2161/*
2162 * Basic power management.
2163 */
2164static int cafe_pci_suspend(struct pci_dev *pdev, pm_message_t state)
2165{
2166        struct v4l2_device *v4l2_dev = dev_get_drvdata(&pdev->dev);
2167        struct cafe_camera *cam = to_cam(v4l2_dev);
2168        int ret;
2169        enum cafe_state cstate;
2170
2171        ret = pci_save_state(pdev);
2172        if (ret)
2173                return ret;
2174        cstate = cam->state; /* HACK - stop_dma sets to idle */
2175        cafe_ctlr_stop_dma(cam);
2176        cafe_ctlr_power_down(cam);
2177        pci_disable_device(pdev);
2178        cam->state = cstate;
2179        return 0;
2180}
2181
2182
2183static int cafe_pci_resume(struct pci_dev *pdev)
2184{
2185        struct v4l2_device *v4l2_dev = dev_get_drvdata(&pdev->dev);
2186        struct cafe_camera *cam = to_cam(v4l2_dev);
2187        int ret = 0;
2188
2189        ret = pci_restore_state(pdev);
2190        if (ret)
2191                return ret;
2192        ret = pci_enable_device(pdev);
2193
2194        if (ret) {
2195                cam_warn(cam, "Unable to re-enable device on resume!\n");
2196                return ret;
2197        }
2198        cafe_ctlr_init(cam);
2199        cafe_ctlr_power_down(cam);
2200
2201        mutex_lock(&cam->s_mutex);
2202        if (cam->users > 0) {
2203                cafe_ctlr_power_up(cam);
2204                __cafe_cam_reset(cam);
2205        }
2206        mutex_unlock(&cam->s_mutex);
2207
2208        set_bit(CF_CONFIG_NEEDED, &cam->flags);
2209        if (cam->state == S_SPECREAD)
2210                cam->state = S_IDLE;  /* Don't bother restarting */
2211        else if (cam->state == S_SINGLEREAD || cam->state == S_STREAMING)
2212                ret = cafe_read_setup(cam, cam->state);
2213        return ret;
2214}
2215
2216#endif  /* CONFIG_PM */
2217
2218
2219static struct pci_device_id cafe_ids[] = {
2220        { PCI_DEVICE(PCI_VENDOR_ID_MARVELL,
2221                     PCI_DEVICE_ID_MARVELL_88ALP01_CCIC) },
2222        { 0, }
2223};
2224
2225MODULE_DEVICE_TABLE(pci, cafe_ids);
2226
2227static struct pci_driver cafe_pci_driver = {
2228        .name = "cafe1000-ccic",
2229        .id_table = cafe_ids,
2230        .probe = cafe_pci_probe,
2231        .remove = cafe_pci_remove,
2232#ifdef CONFIG_PM
2233        .suspend = cafe_pci_suspend,
2234        .resume = cafe_pci_resume,
2235#endif
2236};
2237
2238
2239
2240
2241static int __init cafe_init(void)
2242{
2243        int ret;
2244
2245        printk(KERN_NOTICE "Marvell M88ALP01 'CAFE' Camera Controller version %d\n",
2246                        CAFE_VERSION);
2247        ret = pci_register_driver(&cafe_pci_driver);
2248        if (ret) {
2249                printk(KERN_ERR "Unable to register cafe_ccic driver\n");
2250                goto out;
2251        }
2252        ret = 0;
2253
2254  out:
2255        return ret;
2256}
2257
2258
2259static void __exit cafe_exit(void)
2260{
2261        pci_unregister_driver(&cafe_pci_driver);
2262}
2263
2264module_init(cafe_init);
2265module_exit(cafe_exit);
2266
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.