linux/drivers/media/platform/via-camera.c
<<
>>
Prefs
   1/*
   2 * Driver for the VIA Chrome integrated camera controller.
   3 *
   4 * Copyright 2009,2010 Jonathan Corbet <corbet@lwn.net>
   5 * Distributable under the terms of the GNU General Public License, version 2
   6 *
   7 * This work was supported by the One Laptop Per Child project
   8 */
   9#include <linux/kernel.h>
  10#include <linux/module.h>
  11#include <linux/device.h>
  12#include <linux/list.h>
  13#include <linux/pci.h>
  14#include <linux/gpio.h>
  15#include <linux/interrupt.h>
  16#include <linux/platform_device.h>
  17#include <linux/videodev2.h>
  18#include <media/v4l2-device.h>
  19#include <media/v4l2-ioctl.h>
  20#include <media/v4l2-ctrls.h>
  21#include <media/v4l2-image-sizes.h>
  22#include <media/i2c/ov7670.h>
  23#include <media/videobuf-dma-sg.h>
  24#include <linux/delay.h>
  25#include <linux/dma-mapping.h>
  26#include <linux/pm_qos.h>
  27#include <linux/via-core.h>
  28#include <linux/via-gpio.h>
  29#include <linux/via_i2c.h>
  30#include <asm/olpc.h>
  31
  32#include "via-camera.h"
  33
  34MODULE_ALIAS("platform:viafb-camera");
  35MODULE_AUTHOR("Jonathan Corbet <corbet@lwn.net>");
  36MODULE_DESCRIPTION("VIA framebuffer-based camera controller driver");
  37MODULE_LICENSE("GPL");
  38
  39static bool flip_image;
  40module_param(flip_image, bool, 0444);
  41MODULE_PARM_DESC(flip_image,
  42                "If set, the sensor will be instructed to flip the image vertically.");
  43
  44static bool override_serial;
  45module_param(override_serial, bool, 0444);
  46MODULE_PARM_DESC(override_serial,
  47                "The camera driver will normally refuse to load if the XO 1.5 serial port is enabled.  Set this option to force-enable the camera.");
  48
  49/*
  50 * The structure describing our camera.
  51 */
  52enum viacam_opstate { S_IDLE = 0, S_RUNNING = 1 };
  53
  54struct via_camera {
  55        struct v4l2_device v4l2_dev;
  56        struct v4l2_ctrl_handler ctrl_handler;
  57        struct video_device vdev;
  58        struct v4l2_subdev *sensor;
  59        struct platform_device *platdev;
  60        struct viafb_dev *viadev;
  61        struct mutex lock;
  62        enum viacam_opstate opstate;
  63        unsigned long flags;
  64        struct pm_qos_request qos_request;
  65        /*
  66         * GPIO info for power/reset management
  67         */
  68        int power_gpio;
  69        int reset_gpio;
  70        /*
  71         * I/O memory stuff.
  72         */
  73        void __iomem *mmio;     /* Where the registers live */
  74        void __iomem *fbmem;    /* Frame buffer memory */
  75        u32 fb_offset;          /* Reserved memory offset (FB) */
  76        /*
  77         * Capture buffers and related.  The controller supports
  78         * up to three, so that's what we have here.  These buffers
  79         * live in frame buffer memory, so we don't call them "DMA".
  80         */
  81        unsigned int cb_offsets[3];     /* offsets into fb mem */
  82        u8 __iomem *cb_addrs[3];                /* Kernel-space addresses */
  83        int n_cap_bufs;                 /* How many are we using? */
  84        int next_buf;
  85        struct videobuf_queue vb_queue;
  86        struct list_head buffer_queue;  /* prot. by reg_lock */
  87        /*
  88         * User tracking.
  89         */
  90        int users;
  91        struct file *owner;
  92        /*
  93         * Video format information.  sensor_format is kept in a form
  94         * that we can use to pass to the sensor.  We always run the
  95         * sensor in VGA resolution, though, and let the controller
  96         * downscale things if need be.  So we keep the "real*
  97         * dimensions separately.
  98         */
  99        struct v4l2_pix_format sensor_format;
 100        struct v4l2_pix_format user_format;
 101        u32 mbus_code;
 102};
 103
 104/*
 105 * Yes, this is a hack, but there's only going to be one of these
 106 * on any system we know of.
 107 */
 108static struct via_camera *via_cam_info;
 109
 110/*
 111 * Flag values, manipulated with bitops
 112 */
 113#define CF_DMA_ACTIVE    0      /* A frame is incoming */
 114#define CF_CONFIG_NEEDED 1      /* Must configure hardware */
 115
 116
 117/*
 118 * Nasty ugly v4l2 boilerplate.
 119 */
 120#define sensor_call(cam, optype, func, args...) \
 121        v4l2_subdev_call(cam->sensor, optype, func, ##args)
 122
 123/*
 124 * Debugging and related.
 125 */
 126#define cam_err(cam, fmt, arg...) \
 127        dev_err(&(cam)->platdev->dev, fmt, ##arg);
 128#define cam_warn(cam, fmt, arg...) \
 129        dev_warn(&(cam)->platdev->dev, fmt, ##arg);
 130#define cam_dbg(cam, fmt, arg...) \
 131        dev_dbg(&(cam)->platdev->dev, fmt, ##arg);
 132
 133/*
 134 * Format handling.  This is ripped almost directly from Hans's changes
 135 * to cafe_ccic.c.  It's a little unfortunate; until this change, we
 136 * didn't need to know anything about the format except its byte depth;
 137 * now this information must be managed at this level too.
 138 */
 139static struct via_format {
 140        __u8 *desc;
 141        __u32 pixelformat;
 142        int bpp;   /* Bytes per pixel */
 143        u32 mbus_code;
 144} via_formats[] = {
 145        {
 146                .desc           = "YUYV 4:2:2",
 147                .pixelformat    = V4L2_PIX_FMT_YUYV,
 148                .mbus_code      = MEDIA_BUS_FMT_YUYV8_2X8,
 149                .bpp            = 2,
 150        },
 151        /* RGB444 and Bayer should be doable, but have never been
 152           tested with this driver. RGB565 seems to work at the default
 153           resolution, but results in color corruption when being scaled by
 154           viacam_set_scaled(), and is disabled as a result. */
 155};
 156#define N_VIA_FMTS ARRAY_SIZE(via_formats)
 157
 158static struct via_format *via_find_format(u32 pixelformat)
 159{
 160        unsigned i;
 161
 162        for (i = 0; i < N_VIA_FMTS; i++)
 163                if (via_formats[i].pixelformat == pixelformat)
 164                        return via_formats + i;
 165        /* Not found? Then return the first format. */
 166        return via_formats;
 167}
 168
 169
 170/*--------------------------------------------------------------------------*/
 171/*
 172 * Sensor power/reset management.  This piece is OLPC-specific for
 173 * sure; other configurations will have things connected differently.
 174 */
 175static int via_sensor_power_setup(struct via_camera *cam)
 176{
 177        int ret;
 178
 179        cam->power_gpio = viafb_gpio_lookup("VGPIO3");
 180        cam->reset_gpio = viafb_gpio_lookup("VGPIO2");
 181        if (cam->power_gpio < 0 || cam->reset_gpio < 0) {
 182                dev_err(&cam->platdev->dev, "Unable to find GPIO lines\n");
 183                return -EINVAL;
 184        }
 185        ret = gpio_request(cam->power_gpio, "viafb-camera");
 186        if (ret) {
 187                dev_err(&cam->platdev->dev, "Unable to request power GPIO\n");
 188                return ret;
 189        }
 190        ret = gpio_request(cam->reset_gpio, "viafb-camera");
 191        if (ret) {
 192                dev_err(&cam->platdev->dev, "Unable to request reset GPIO\n");
 193                gpio_free(cam->power_gpio);
 194                return ret;
 195        }
 196        gpio_direction_output(cam->power_gpio, 0);
 197        gpio_direction_output(cam->reset_gpio, 0);
 198        return 0;
 199}
 200
 201/*
 202 * Power up the sensor and perform the reset dance.
 203 */
 204static void via_sensor_power_up(struct via_camera *cam)
 205{
 206        gpio_set_value(cam->power_gpio, 1);
 207        gpio_set_value(cam->reset_gpio, 0);
 208        msleep(20);  /* Probably excessive */
 209        gpio_set_value(cam->reset_gpio, 1);
 210        msleep(20);
 211}
 212
 213static void via_sensor_power_down(struct via_camera *cam)
 214{
 215        gpio_set_value(cam->power_gpio, 0);
 216        gpio_set_value(cam->reset_gpio, 0);
 217}
 218
 219
 220static void via_sensor_power_release(struct via_camera *cam)
 221{
 222        via_sensor_power_down(cam);
 223        gpio_free(cam->power_gpio);
 224        gpio_free(cam->reset_gpio);
 225}
 226
 227/* --------------------------------------------------------------------------*/
 228/* Sensor ops */
 229
 230/*
 231 * Manage the ov7670 "flip" bit, which needs special help.
 232 */
 233static int viacam_set_flip(struct via_camera *cam)
 234{
 235        struct v4l2_control ctrl;
 236
 237        memset(&ctrl, 0, sizeof(ctrl));
 238        ctrl.id = V4L2_CID_VFLIP;
 239        ctrl.value = flip_image;
 240        return v4l2_s_ctrl(NULL, cam->sensor->ctrl_handler, &ctrl);
 241}
 242
 243/*
 244 * Configure the sensor.  It's up to the caller to ensure
 245 * that the camera is in the correct operating state.
 246 */
 247static int viacam_configure_sensor(struct via_camera *cam)
 248{
 249        struct v4l2_subdev_format format = {
 250                .which = V4L2_SUBDEV_FORMAT_ACTIVE,
 251        };
 252        int ret;
 253
 254        v4l2_fill_mbus_format(&format.format, &cam->sensor_format, cam->mbus_code);
 255        ret = sensor_call(cam, core, init, 0);
 256        if (ret == 0)
 257                ret = sensor_call(cam, pad, set_fmt, NULL, &format);
 258        /*
 259         * OV7670 does weird things if flip is set *before* format...
 260         */
 261        if (ret == 0)
 262                ret = viacam_set_flip(cam);
 263        return ret;
 264}
 265
 266
 267
 268/* --------------------------------------------------------------------------*/
 269/*
 270 * Some simple register accessors; they assume that the lock is held.
 271 *
 272 * Should we want to support the second capture engine, we could
 273 * hide the register difference by adding 0x1000 to registers in the
 274 * 0x300-350 range.
 275 */
 276static inline void viacam_write_reg(struct via_camera *cam,
 277                int reg, int value)
 278{
 279        iowrite32(value, cam->mmio + reg);
 280}
 281
 282static inline int viacam_read_reg(struct via_camera *cam, int reg)
 283{
 284        return ioread32(cam->mmio + reg);
 285}
 286
 287static inline void viacam_write_reg_mask(struct via_camera *cam,
 288                int reg, int value, int mask)
 289{
 290        int tmp = viacam_read_reg(cam, reg);
 291
 292        tmp = (tmp & ~mask) | (value & mask);
 293        viacam_write_reg(cam, reg, tmp);
 294}
 295
 296
 297/* --------------------------------------------------------------------------*/
 298/* Interrupt management and handling */
 299
 300static irqreturn_t viacam_quick_irq(int irq, void *data)
 301{
 302        struct via_camera *cam = data;
 303        irqreturn_t ret = IRQ_NONE;
 304        int icv;
 305
 306        /*
 307         * All we do here is to clear the interrupts and tell
 308         * the handler thread to wake up.
 309         */
 310        spin_lock(&cam->viadev->reg_lock);
 311        icv = viacam_read_reg(cam, VCR_INTCTRL);
 312        if (icv & VCR_IC_EAV) {
 313                icv |= VCR_IC_EAV|VCR_IC_EVBI|VCR_IC_FFULL;
 314                viacam_write_reg(cam, VCR_INTCTRL, icv);
 315                ret = IRQ_WAKE_THREAD;
 316        }
 317        spin_unlock(&cam->viadev->reg_lock);
 318        return ret;
 319}
 320
 321/*
 322 * Find the next videobuf buffer which has somebody waiting on it.
 323 */
 324static struct videobuf_buffer *viacam_next_buffer(struct via_camera *cam)
 325{
 326        unsigned long flags;
 327        struct videobuf_buffer *buf = NULL;
 328
 329        spin_lock_irqsave(&cam->viadev->reg_lock, flags);
 330        if (cam->opstate != S_RUNNING)
 331                goto out;
 332        if (list_empty(&cam->buffer_queue))
 333                goto out;
 334        buf = list_entry(cam->buffer_queue.next, struct videobuf_buffer, queue);
 335        if (!waitqueue_active(&buf->done)) {/* Nobody waiting */
 336                buf = NULL;
 337                goto out;
 338        }
 339        list_del(&buf->queue);
 340        buf->state = VIDEOBUF_ACTIVE;
 341out:
 342        spin_unlock_irqrestore(&cam->viadev->reg_lock, flags);
 343        return buf;
 344}
 345
 346/*
 347 * The threaded IRQ handler.
 348 */
 349static irqreturn_t viacam_irq(int irq, void *data)
 350{
 351        int bufn;
 352        struct videobuf_buffer *vb;
 353        struct via_camera *cam = data;
 354        struct videobuf_dmabuf *vdma;
 355
 356        /*
 357         * If there is no place to put the data frame, don't bother
 358         * with anything else.
 359         */
 360        vb = viacam_next_buffer(cam);
 361        if (vb == NULL)
 362                goto done;
 363        /*
 364         * Figure out which buffer we just completed.
 365         */
 366        bufn = (viacam_read_reg(cam, VCR_INTCTRL) & VCR_IC_ACTBUF) >> 3;
 367        bufn -= 1;
 368        if (bufn < 0)
 369                bufn = cam->n_cap_bufs - 1;
 370        /*
 371         * Copy over the data and let any waiters know.
 372         */
 373        vdma = videobuf_to_dma(vb);
 374        viafb_dma_copy_out_sg(cam->cb_offsets[bufn], vdma->sglist, vdma->sglen);
 375        vb->state = VIDEOBUF_DONE;
 376        vb->size = cam->user_format.sizeimage;
 377        wake_up(&vb->done);
 378done:
 379        return IRQ_HANDLED;
 380}
 381
 382
 383/*
 384 * These functions must mess around with the general interrupt
 385 * control register, which is relevant to much more than just the
 386 * camera.  Nothing else uses interrupts, though, as of this writing.
 387 * Should that situation change, we'll have to improve support at
 388 * the via-core level.
 389 */
 390static void viacam_int_enable(struct via_camera *cam)
 391{
 392        viacam_write_reg(cam, VCR_INTCTRL,
 393                        VCR_IC_INTEN|VCR_IC_EAV|VCR_IC_EVBI|VCR_IC_FFULL);
 394        viafb_irq_enable(VDE_I_C0AVEN);
 395}
 396
 397static void viacam_int_disable(struct via_camera *cam)
 398{
 399        viafb_irq_disable(VDE_I_C0AVEN);
 400        viacam_write_reg(cam, VCR_INTCTRL, 0);
 401}
 402
 403
 404
 405/* --------------------------------------------------------------------------*/
 406/* Controller operations */
 407
 408/*
 409 * Set up our capture buffers in framebuffer memory.
 410 */
 411static int viacam_ctlr_cbufs(struct via_camera *cam)
 412{
 413        int nbuf = cam->viadev->camera_fbmem_size/cam->sensor_format.sizeimage;
 414        int i;
 415        unsigned int offset;
 416
 417        /*
 418         * See how many buffers we can work with.
 419         */
 420        if (nbuf >= 3) {
 421                cam->n_cap_bufs = 3;
 422                viacam_write_reg_mask(cam, VCR_CAPINTC, VCR_CI_3BUFS,
 423                                VCR_CI_3BUFS);
 424        } else if (nbuf == 2) {
 425                cam->n_cap_bufs = 2;
 426                viacam_write_reg_mask(cam, VCR_CAPINTC, 0, VCR_CI_3BUFS);
 427        } else {
 428                cam_warn(cam, "Insufficient frame buffer memory\n");
 429                return -ENOMEM;
 430        }
 431        /*
 432         * Set them up.
 433         */
 434        offset = cam->fb_offset;
 435        for (i = 0; i < cam->n_cap_bufs; i++) {
 436                cam->cb_offsets[i] = offset;
 437                cam->cb_addrs[i] = cam->fbmem + offset;
 438                viacam_write_reg(cam, VCR_VBUF1 + i*4, offset & VCR_VBUF_MASK);
 439                offset += cam->sensor_format.sizeimage;
 440        }
 441        return 0;
 442}
 443
 444/*
 445 * Set the scaling register for downscaling the image.
 446 *
 447 * This register works like this...  Vertical scaling is enabled
 448 * by bit 26; if that bit is set, downscaling is controlled by the
 449 * value in bits 16:25.  Those bits are divided by 1024 to get
 450 * the scaling factor; setting just bit 25 thus cuts the height
 451 * in half.
 452 *
 453 * Horizontal scaling works about the same, but it's enabled by
 454 * bit 11, with bits 0:10 giving the numerator of a fraction
 455 * (over 2048) for the scaling value.
 456 *
 457 * This function is naive in that, if the user departs from
 458 * the 3x4 VGA scaling factor, the image will distort.  We
 459 * could work around that if it really seemed important.
 460 */
 461static void viacam_set_scale(struct via_camera *cam)
 462{
 463        unsigned int avscale;
 464        int sf;
 465
 466        if (cam->user_format.width == VGA_WIDTH)
 467                avscale = 0;
 468        else {
 469                sf = (cam->user_format.width*2048)/VGA_WIDTH;
 470                avscale = VCR_AVS_HEN | sf;
 471        }
 472        if (cam->user_format.height < VGA_HEIGHT) {
 473                sf = (1024*cam->user_format.height)/VGA_HEIGHT;
 474                avscale |= VCR_AVS_VEN | (sf << 16);
 475        }
 476        viacam_write_reg(cam, VCR_AVSCALE, avscale);
 477}
 478
 479
 480/*
 481 * Configure image-related information into the capture engine.
 482 */
 483static void viacam_ctlr_image(struct via_camera *cam)
 484{
 485        int cicreg;
 486
 487        /*
 488         * Disable clock before messing with stuff - from the via
 489         * sample driver.
 490         */
 491        viacam_write_reg(cam, VCR_CAPINTC, ~(VCR_CI_ENABLE|VCR_CI_CLKEN));
 492        /*
 493         * Set up the controller for VGA resolution, modulo magic
 494         * offsets from the via sample driver.
 495         */
 496        viacam_write_reg(cam, VCR_HORRANGE, 0x06200120);
 497        viacam_write_reg(cam, VCR_VERTRANGE, 0x01de0000);
 498        viacam_set_scale(cam);
 499        /*
 500         * Image size info.
 501         */
 502        viacam_write_reg(cam, VCR_MAXDATA,
 503                        (cam->sensor_format.height << 16) |
 504                        (cam->sensor_format.bytesperline >> 3));
 505        viacam_write_reg(cam, VCR_MAXVBI, 0);
 506        viacam_write_reg(cam, VCR_VSTRIDE,
 507                        cam->user_format.bytesperline & VCR_VS_STRIDE);
 508        /*
 509         * Set up the capture interface control register,
 510         * everything but the "go" bit.
 511         *
 512         * The FIFO threshold is a bit of a magic number; 8 is what
 513         * VIA's sample code uses.
 514         */
 515        cicreg = VCR_CI_CLKEN |
 516                0x08000000 |            /* FIFO threshold */
 517                VCR_CI_FLDINV |         /* OLPC-specific? */
 518                VCR_CI_VREFINV |        /* OLPC-specific? */
 519                VCR_CI_DIBOTH |         /* Capture both fields */
 520                VCR_CI_CCIR601_8;
 521        if (cam->n_cap_bufs == 3)
 522                cicreg |= VCR_CI_3BUFS;
 523        /*
 524         * YUV formats need different byte swapping than RGB.
 525         */
 526        if (cam->user_format.pixelformat == V4L2_PIX_FMT_YUYV)
 527                cicreg |= VCR_CI_YUYV;
 528        else
 529                cicreg |= VCR_CI_UYVY;
 530        viacam_write_reg(cam, VCR_CAPINTC, cicreg);
 531}
 532
 533
 534static int viacam_config_controller(struct via_camera *cam)
 535{
 536        int ret;
 537        unsigned long flags;
 538
 539        spin_lock_irqsave(&cam->viadev->reg_lock, flags);
 540        ret = viacam_ctlr_cbufs(cam);
 541        if (!ret)
 542                viacam_ctlr_image(cam);
 543        spin_unlock_irqrestore(&cam->viadev->reg_lock, flags);
 544        clear_bit(CF_CONFIG_NEEDED, &cam->flags);
 545        return ret;
 546}
 547
 548/*
 549 * Make it start grabbing data.
 550 */
 551static void viacam_start_engine(struct via_camera *cam)
 552{
 553        spin_lock_irq(&cam->viadev->reg_lock);
 554        cam->next_buf = 0;
 555        viacam_write_reg_mask(cam, VCR_CAPINTC, VCR_CI_ENABLE, VCR_CI_ENABLE);
 556        viacam_int_enable(cam);
 557        (void) viacam_read_reg(cam, VCR_CAPINTC); /* Force post */
 558        cam->opstate = S_RUNNING;
 559        spin_unlock_irq(&cam->viadev->reg_lock);
 560}
 561
 562
 563static void viacam_stop_engine(struct via_camera *cam)
 564{
 565        spin_lock_irq(&cam->viadev->reg_lock);
 566        viacam_int_disable(cam);
 567        viacam_write_reg_mask(cam, VCR_CAPINTC, 0, VCR_CI_ENABLE);
 568        (void) viacam_read_reg(cam, VCR_CAPINTC); /* Force post */
 569        cam->opstate = S_IDLE;
 570        spin_unlock_irq(&cam->viadev->reg_lock);
 571}
 572
 573
 574/* --------------------------------------------------------------------------*/
 575/* Videobuf callback ops */
 576
 577/*
 578 * buffer_setup.  The purpose of this one would appear to be to tell
 579 * videobuf how big a single image is.  It's also evidently up to us
 580 * to put some sort of limit on the maximum number of buffers allowed.
 581 */
 582static int viacam_vb_buf_setup(struct videobuf_queue *q,
 583                unsigned int *count, unsigned int *size)
 584{
 585        struct via_camera *cam = q->priv_data;
 586
 587        *size = cam->user_format.sizeimage;
 588        if (*count == 0 || *count > 6)  /* Arbitrary number */
 589                *count = 6;
 590        return 0;
 591}
 592
 593/*
 594 * Prepare a buffer.
 595 */
 596static int viacam_vb_buf_prepare(struct videobuf_queue *q,
 597                struct videobuf_buffer *vb, enum v4l2_field field)
 598{
 599        struct via_camera *cam = q->priv_data;
 600
 601        vb->size = cam->user_format.sizeimage;
 602        vb->width = cam->user_format.width; /* bytesperline???? */
 603        vb->height = cam->user_format.height;
 604        vb->field = field;
 605        if (vb->state == VIDEOBUF_NEEDS_INIT) {
 606                int ret = videobuf_iolock(q, vb, NULL);
 607                if (ret)
 608                        return ret;
 609        }
 610        vb->state = VIDEOBUF_PREPARED;
 611        return 0;
 612}
 613
 614/*
 615 * We've got a buffer to put data into.
 616 *
 617 * FIXME: check for a running engine and valid buffers?
 618 */
 619static void viacam_vb_buf_queue(struct videobuf_queue *q,
 620                struct videobuf_buffer *vb)
 621{
 622        struct via_camera *cam = q->priv_data;
 623
 624        /*
 625         * Note that videobuf holds the lock when it calls
 626         * us, so we need not (indeed, cannot) take it here.
 627         */
 628        vb->state = VIDEOBUF_QUEUED;
 629        list_add_tail(&vb->queue, &cam->buffer_queue);
 630}
 631
 632/*
 633 * Free a buffer.
 634 */
 635static void viacam_vb_buf_release(struct videobuf_queue *q,
 636                struct videobuf_buffer *vb)
 637{
 638        struct via_camera *cam = q->priv_data;
 639
 640        videobuf_dma_unmap(&cam->platdev->dev, videobuf_to_dma(vb));
 641        videobuf_dma_free(videobuf_to_dma(vb));
 642        vb->state = VIDEOBUF_NEEDS_INIT;
 643}
 644
 645static const struct videobuf_queue_ops viacam_vb_ops = {
 646        .buf_setup      = viacam_vb_buf_setup,
 647        .buf_prepare    = viacam_vb_buf_prepare,
 648        .buf_queue      = viacam_vb_buf_queue,
 649        .buf_release    = viacam_vb_buf_release,
 650};
 651
 652/* --------------------------------------------------------------------------*/
 653/* File operations */
 654
 655static int viacam_open(struct file *filp)
 656{
 657        struct via_camera *cam = video_drvdata(filp);
 658
 659        filp->private_data = cam;
 660        /*
 661         * Note the new user.  If this is the first one, we'll also
 662         * need to power up the sensor.
 663         */
 664        mutex_lock(&cam->lock);
 665        if (cam->users == 0) {
 666                int ret = viafb_request_dma();
 667
 668                if (ret) {
 669                        mutex_unlock(&cam->lock);
 670                        return ret;
 671                }
 672                via_sensor_power_up(cam);
 673                set_bit(CF_CONFIG_NEEDED, &cam->flags);
 674                /*
 675                 * Hook into videobuf.  Evidently this cannot fail.
 676                 */
 677                videobuf_queue_sg_init(&cam->vb_queue, &viacam_vb_ops,
 678                                &cam->platdev->dev, &cam->viadev->reg_lock,
 679                                V4L2_BUF_TYPE_VIDEO_CAPTURE, V4L2_FIELD_NONE,
 680                                sizeof(struct videobuf_buffer), cam, NULL);
 681        }
 682        (cam->users)++;
 683        mutex_unlock(&cam->lock);
 684        return 0;
 685}
 686
 687static int viacam_release(struct file *filp)
 688{
 689        struct via_camera *cam = video_drvdata(filp);
 690
 691        mutex_lock(&cam->lock);
 692        (cam->users)--;
 693        /*
 694         * If the "owner" is closing, shut down any ongoing
 695         * operations.
 696         */
 697        if (filp == cam->owner) {
 698                videobuf_stop(&cam->vb_queue);
 699                /*
 700                 * We don't hold the spinlock here, but, if release()
 701                 * is being called by the owner, nobody else will
 702                 * be changing the state.  And an extra stop would
 703                 * not hurt anyway.
 704                 */
 705                if (cam->opstate != S_IDLE)
 706                        viacam_stop_engine(cam);
 707                cam->owner = NULL;
 708        }
 709        /*
 710         * Last one out needs to turn out the lights.
 711         */
 712        if (cam->users == 0) {
 713                videobuf_mmap_free(&cam->vb_queue);
 714                via_sensor_power_down(cam);
 715                viafb_release_dma();
 716        }
 717        mutex_unlock(&cam->lock);
 718        return 0;
 719}
 720
 721/*
 722 * Read a frame from the device.
 723 */
 724static ssize_t viacam_read(struct file *filp, char __user *buffer,
 725                size_t len, loff_t *pos)
 726{
 727        struct via_camera *cam = video_drvdata(filp);
 728        int ret;
 729
 730        mutex_lock(&cam->lock);
 731        /*
 732         * Enforce the V4l2 "only one owner gets to read data" rule.
 733         */
 734        if (cam->owner && cam->owner != filp) {
 735                ret = -EBUSY;
 736                goto out_unlock;
 737        }
 738        cam->owner = filp;
 739        /*
 740         * Do we need to configure the hardware?
 741         */
 742        if (test_bit(CF_CONFIG_NEEDED, &cam->flags)) {
 743                ret = viacam_configure_sensor(cam);
 744                if (!ret)
 745                        ret = viacam_config_controller(cam);
 746                if (ret)
 747                        goto out_unlock;
 748        }
 749        /*
 750         * Fire up the capture engine, then have videobuf do
 751         * the heavy lifting.  Someday it would be good to avoid
 752         * stopping and restarting the engine each time.
 753         */
 754        INIT_LIST_HEAD(&cam->buffer_queue);
 755        viacam_start_engine(cam);
 756        ret = videobuf_read_stream(&cam->vb_queue, buffer, len, pos, 0,
 757                        filp->f_flags & O_NONBLOCK);
 758        viacam_stop_engine(cam);
 759        /* videobuf_stop() ?? */
 760
 761out_unlock:
 762        mutex_unlock(&cam->lock);
 763        return ret;
 764}
 765
 766
 767static unsigned int viacam_poll(struct file *filp, struct poll_table_struct *pt)
 768{
 769        struct via_camera *cam = video_drvdata(filp);
 770
 771        return videobuf_poll_stream(filp, &cam->vb_queue, pt);
 772}
 773
 774
 775static int viacam_mmap(struct file *filp, struct vm_area_struct *vma)
 776{
 777        struct via_camera *cam = video_drvdata(filp);
 778
 779        return videobuf_mmap_mapper(&cam->vb_queue, vma);
 780}
 781
 782
 783
 784static const struct v4l2_file_operations viacam_fops = {
 785        .owner          = THIS_MODULE,
 786        .open           = viacam_open,
 787        .release        = viacam_release,
 788        .read           = viacam_read,
 789        .poll           = viacam_poll,
 790        .mmap           = viacam_mmap,
 791        .unlocked_ioctl = video_ioctl2,
 792};
 793
 794/*----------------------------------------------------------------------------*/
 795/*
 796 * The long list of v4l2 ioctl ops
 797 */
 798
 799/*
 800 * Only one input.
 801 */
 802static int viacam_enum_input(struct file *filp, void *priv,
 803                struct v4l2_input *input)
 804{
 805        if (input->index != 0)
 806                return -EINVAL;
 807
 808        input->type = V4L2_INPUT_TYPE_CAMERA;
 809        input->std = V4L2_STD_ALL; /* Not sure what should go here */
 810        strcpy(input->name, "Camera");
 811        return 0;
 812}
 813
 814static int viacam_g_input(struct file *filp, void *priv, unsigned int *i)
 815{
 816        *i = 0;
 817        return 0;
 818}
 819
 820static int viacam_s_input(struct file *filp, void *priv, unsigned int i)
 821{
 822        if (i != 0)
 823                return -EINVAL;
 824        return 0;
 825}
 826
 827static int viacam_s_std(struct file *filp, void *priv, v4l2_std_id std)
 828{
 829        return 0;
 830}
 831
 832static int viacam_g_std(struct file *filp, void *priv, v4l2_std_id *std)
 833{
 834        *std = V4L2_STD_NTSC_M;
 835        return 0;
 836}
 837
 838/*
 839 * Video format stuff.  Here is our default format until
 840 * user space messes with things.
 841 */
 842static const struct v4l2_pix_format viacam_def_pix_format = {
 843        .width          = VGA_WIDTH,
 844        .height         = VGA_HEIGHT,
 845        .pixelformat    = V4L2_PIX_FMT_YUYV,
 846        .field          = V4L2_FIELD_NONE,
 847        .bytesperline   = VGA_WIDTH * 2,
 848        .sizeimage      = VGA_WIDTH * VGA_HEIGHT * 2,
 849};
 850
 851static const u32 via_def_mbus_code = MEDIA_BUS_FMT_YUYV8_2X8;
 852
 853static int viacam_enum_fmt_vid_cap(struct file *filp, void *priv,
 854                struct v4l2_fmtdesc *fmt)
 855{
 856        if (fmt->index >= N_VIA_FMTS)
 857                return -EINVAL;
 858        strlcpy(fmt->description, via_formats[fmt->index].desc,
 859                        sizeof(fmt->description));
 860        fmt->pixelformat = via_formats[fmt->index].pixelformat;
 861        return 0;
 862}
 863
 864/*
 865 * Figure out proper image dimensions, but always force the
 866 * sensor to VGA.
 867 */
 868static void viacam_fmt_pre(struct v4l2_pix_format *userfmt,
 869                struct v4l2_pix_format *sensorfmt)
 870{
 871        *sensorfmt = *userfmt;
 872        if (userfmt->width < QCIF_WIDTH || userfmt->height < QCIF_HEIGHT) {
 873                userfmt->width = QCIF_WIDTH;
 874                userfmt->height = QCIF_HEIGHT;
 875        }
 876        if (userfmt->width > VGA_WIDTH || userfmt->height > VGA_HEIGHT) {
 877                userfmt->width = VGA_WIDTH;
 878                userfmt->height = VGA_HEIGHT;
 879        }
 880        sensorfmt->width = VGA_WIDTH;
 881        sensorfmt->height = VGA_HEIGHT;
 882}
 883
 884static void viacam_fmt_post(struct v4l2_pix_format *userfmt,
 885                struct v4l2_pix_format *sensorfmt)
 886{
 887        struct via_format *f = via_find_format(userfmt->pixelformat);
 888
 889        sensorfmt->bytesperline = sensorfmt->width * f->bpp;
 890        sensorfmt->sizeimage = sensorfmt->height * sensorfmt->bytesperline;
 891        userfmt->pixelformat = sensorfmt->pixelformat;
 892        userfmt->field = sensorfmt->field;
 893        userfmt->bytesperline = 2 * userfmt->width;
 894        userfmt->sizeimage = userfmt->bytesperline * userfmt->height;
 895}
 896
 897
 898/*
 899 * The real work of figuring out a workable format.
 900 */
 901static int viacam_do_try_fmt(struct via_camera *cam,
 902                struct v4l2_pix_format *upix, struct v4l2_pix_format *spix)
 903{
 904        int ret;
 905        struct v4l2_subdev_pad_config pad_cfg;
 906        struct v4l2_subdev_format format = {
 907                .which = V4L2_SUBDEV_FORMAT_TRY,
 908        };
 909        struct via_format *f = via_find_format(upix->pixelformat);
 910
 911        upix->pixelformat = f->pixelformat;
 912        viacam_fmt_pre(upix, spix);
 913        v4l2_fill_mbus_format(&format.format, spix, f->mbus_code);
 914        ret = sensor_call(cam, pad, set_fmt, &pad_cfg, &format);
 915        v4l2_fill_pix_format(spix, &format.format);
 916        viacam_fmt_post(upix, spix);
 917        return ret;
 918}
 919
 920
 921
 922static int viacam_try_fmt_vid_cap(struct file *filp, void *priv,
 923                struct v4l2_format *fmt)
 924{
 925        struct via_camera *cam = priv;
 926        struct v4l2_format sfmt;
 927        int ret;
 928
 929        mutex_lock(&cam->lock);
 930        ret = viacam_do_try_fmt(cam, &fmt->fmt.pix, &sfmt.fmt.pix);
 931        mutex_unlock(&cam->lock);
 932        return ret;
 933}
 934
 935
 936static int viacam_g_fmt_vid_cap(struct file *filp, void *priv,
 937                struct v4l2_format *fmt)
 938{
 939        struct via_camera *cam = priv;
 940
 941        mutex_lock(&cam->lock);
 942        fmt->fmt.pix = cam->user_format;
 943        mutex_unlock(&cam->lock);
 944        return 0;
 945}
 946
 947static int viacam_s_fmt_vid_cap(struct file *filp, void *priv,
 948                struct v4l2_format *fmt)
 949{
 950        struct via_camera *cam = priv;
 951        int ret;
 952        struct v4l2_format sfmt;
 953        struct via_format *f = via_find_format(fmt->fmt.pix.pixelformat);
 954
 955        /*
 956         * Camera must be idle or we can't mess with the
 957         * video setup.
 958         */
 959        mutex_lock(&cam->lock);
 960        if (cam->opstate != S_IDLE) {
 961                ret = -EBUSY;
 962                goto out;
 963        }
 964        /*
 965         * Let the sensor code look over and tweak the
 966         * requested formatting.
 967         */
 968        ret = viacam_do_try_fmt(cam, &fmt->fmt.pix, &sfmt.fmt.pix);
 969        if (ret)
 970                goto out;
 971        /*
 972         * OK, let's commit to the new format.
 973         */
 974        cam->user_format = fmt->fmt.pix;
 975        cam->sensor_format = sfmt.fmt.pix;
 976        cam->mbus_code = f->mbus_code;
 977        ret = viacam_configure_sensor(cam);
 978        if (!ret)
 979                ret = viacam_config_controller(cam);
 980out:
 981        mutex_unlock(&cam->lock);
 982        return ret;
 983}
 984
 985static int viacam_querycap(struct file *filp, void *priv,
 986                struct v4l2_capability *cap)
 987{
 988        strcpy(cap->driver, "via-camera");
 989        strcpy(cap->card, "via-camera");
 990        cap->device_caps = V4L2_CAP_VIDEO_CAPTURE |
 991                V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
 992        cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
 993        return 0;
 994}
 995
 996/*
 997 * Streaming operations - pure videobuf stuff.
 998 */
 999static int viacam_reqbufs(struct file *filp, void *priv,
1000                struct v4l2_requestbuffers *rb)
1001{
1002        struct via_camera *cam = priv;
1003
1004        return videobuf_reqbufs(&cam->vb_queue, rb);
1005}
1006
1007static int viacam_querybuf(struct file *filp, void *priv,
1008                struct v4l2_buffer *buf)
1009{
1010        struct via_camera *cam = priv;
1011
1012        return videobuf_querybuf(&cam->vb_queue, buf);
1013}
1014
1015static int viacam_qbuf(struct file *filp, void *priv, struct v4l2_buffer *buf)
1016{
1017        struct via_camera *cam = priv;
1018
1019        return videobuf_qbuf(&cam->vb_queue, buf);
1020}
1021
1022static int viacam_dqbuf(struct file *filp, void *priv, struct v4l2_buffer *buf)
1023{
1024        struct via_camera *cam = priv;
1025
1026        return videobuf_dqbuf(&cam->vb_queue, buf, filp->f_flags & O_NONBLOCK);
1027}
1028
1029static int viacam_streamon(struct file *filp, void *priv, enum v4l2_buf_type t)
1030{
1031        struct via_camera *cam = priv;
1032        int ret = 0;
1033
1034        if (t != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1035                return -EINVAL;
1036
1037        mutex_lock(&cam->lock);
1038        if (cam->opstate != S_IDLE) {
1039                ret = -EBUSY;
1040                goto out;
1041        }
1042        /*
1043         * Enforce the V4l2 "only one owner gets to read data" rule.
1044         */
1045        if (cam->owner && cam->owner != filp) {
1046                ret = -EBUSY;
1047                goto out;
1048        }
1049        cam->owner = filp;
1050        /*
1051         * Configure things if need be.
1052         */
1053        if (test_bit(CF_CONFIG_NEEDED, &cam->flags)) {
1054                ret = viacam_configure_sensor(cam);
1055                if (ret)
1056                        goto out;
1057                ret = viacam_config_controller(cam);
1058                if (ret)
1059                        goto out;
1060        }
1061        /*
1062         * If the CPU goes into C3, the DMA transfer gets corrupted and
1063         * users start filing unsightly bug reports.  Put in a "latency"
1064         * requirement which will keep the CPU out of the deeper sleep
1065         * states.
1066         */
1067        pm_qos_add_request(&cam->qos_request, PM_QOS_CPU_DMA_LATENCY, 50);
1068        /*
1069         * Fire things up.
1070         */
1071        INIT_LIST_HEAD(&cam->buffer_queue);
1072        ret = videobuf_streamon(&cam->vb_queue);
1073        if (!ret)
1074                viacam_start_engine(cam);
1075out:
1076        mutex_unlock(&cam->lock);
1077        return ret;
1078}
1079
1080static int viacam_streamoff(struct file *filp, void *priv, enum v4l2_buf_type t)
1081{
1082        struct via_camera *cam = priv;
1083        int ret;
1084
1085        if (t != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1086                return -EINVAL;
1087        mutex_lock(&cam->lock);
1088        if (cam->opstate != S_RUNNING) {
1089                ret = -EINVAL;
1090                goto out;
1091        }
1092        pm_qos_remove_request(&cam->qos_request);
1093        viacam_stop_engine(cam);
1094        /*
1095         * Videobuf will recycle all of the outstanding buffers, but
1096         * we should be sure we don't retain any references to
1097         * any of them.
1098         */
1099        ret = videobuf_streamoff(&cam->vb_queue);
1100        INIT_LIST_HEAD(&cam->buffer_queue);
1101out:
1102        mutex_unlock(&cam->lock);
1103        return ret;
1104}
1105
1106/* G/S_PARM */
1107
1108static int viacam_g_parm(struct file *filp, void *priv,
1109                struct v4l2_streamparm *parm)
1110{
1111        struct via_camera *cam = priv;
1112        int ret;
1113
1114        mutex_lock(&cam->lock);
1115        ret = sensor_call(cam, video, g_parm, parm);
1116        mutex_unlock(&cam->lock);
1117        parm->parm.capture.readbuffers = cam->n_cap_bufs;
1118        return ret;
1119}
1120
1121static int viacam_s_parm(struct file *filp, void *priv,
1122                struct v4l2_streamparm *parm)
1123{
1124        struct via_camera *cam = priv;
1125        int ret;
1126
1127        mutex_lock(&cam->lock);
1128        ret = sensor_call(cam, video, s_parm, parm);
1129        mutex_unlock(&cam->lock);
1130        parm->parm.capture.readbuffers = cam->n_cap_bufs;
1131        return ret;
1132}
1133
1134static int viacam_enum_framesizes(struct file *filp, void *priv,
1135                struct v4l2_frmsizeenum *sizes)
1136{
1137        if (sizes->index != 0)
1138                return -EINVAL;
1139        sizes->type = V4L2_FRMSIZE_TYPE_CONTINUOUS;
1140        sizes->stepwise.min_width = QCIF_WIDTH;
1141        sizes->stepwise.min_height = QCIF_HEIGHT;
1142        sizes->stepwise.max_width = VGA_WIDTH;
1143        sizes->stepwise.max_height = VGA_HEIGHT;
1144        sizes->stepwise.step_width = sizes->stepwise.step_height = 1;
1145        return 0;
1146}
1147
1148static int viacam_enum_frameintervals(struct file *filp, void *priv,
1149                struct v4l2_frmivalenum *interval)
1150{
1151        struct via_camera *cam = priv;
1152        struct v4l2_subdev_frame_interval_enum fie = {
1153                .index = interval->index,
1154                .code = cam->mbus_code,
1155                .width = cam->sensor_format.width,
1156                .height = cam->sensor_format.height,
1157                .which = V4L2_SUBDEV_FORMAT_ACTIVE,
1158        };
1159        int ret;
1160
1161        mutex_lock(&cam->lock);
1162        ret = sensor_call(cam, pad, enum_frame_interval, NULL, &fie);
1163        mutex_unlock(&cam->lock);
1164        if (ret)
1165                return ret;
1166        interval->type = V4L2_FRMIVAL_TYPE_DISCRETE;
1167        interval->discrete = fie.interval;
1168        return 0;
1169}
1170
1171
1172
1173static const struct v4l2_ioctl_ops viacam_ioctl_ops = {
1174        .vidioc_enum_input      = viacam_enum_input,
1175        .vidioc_g_input         = viacam_g_input,
1176        .vidioc_s_input         = viacam_s_input,
1177        .vidioc_s_std           = viacam_s_std,
1178        .vidioc_g_std           = viacam_g_std,
1179        .vidioc_enum_fmt_vid_cap = viacam_enum_fmt_vid_cap,
1180        .vidioc_try_fmt_vid_cap = viacam_try_fmt_vid_cap,
1181        .vidioc_g_fmt_vid_cap   = viacam_g_fmt_vid_cap,
1182        .vidioc_s_fmt_vid_cap   = viacam_s_fmt_vid_cap,
1183        .vidioc_querycap        = viacam_querycap,
1184        .vidioc_reqbufs         = viacam_reqbufs,
1185        .vidioc_querybuf        = viacam_querybuf,
1186        .vidioc_qbuf            = viacam_qbuf,
1187        .vidioc_dqbuf           = viacam_dqbuf,
1188        .vidioc_streamon        = viacam_streamon,
1189        .vidioc_streamoff       = viacam_streamoff,
1190        .vidioc_g_parm          = viacam_g_parm,
1191        .vidioc_s_parm          = viacam_s_parm,
1192        .vidioc_enum_framesizes = viacam_enum_framesizes,
1193        .vidioc_enum_frameintervals = viacam_enum_frameintervals,
1194};
1195
1196/*----------------------------------------------------------------------------*/
1197
1198/*
1199 * Power management.
1200 */
1201#ifdef CONFIG_PM
1202
1203static int viacam_suspend(void *priv)
1204{
1205        struct via_camera *cam = priv;
1206        enum viacam_opstate state = cam->opstate;
1207
1208        if (cam->opstate != S_IDLE) {
1209                viacam_stop_engine(cam);
1210                cam->opstate = state; /* So resume restarts */
1211        }
1212
1213        return 0;
1214}
1215
1216static int viacam_resume(void *priv)
1217{
1218        struct via_camera *cam = priv;
1219        int ret = 0;
1220
1221        /*
1222         * Get back to a reasonable operating state.
1223         */
1224        via_write_reg_mask(VIASR, 0x78, 0, 0x80);
1225        via_write_reg_mask(VIASR, 0x1e, 0xc0, 0xc0);
1226        viacam_int_disable(cam);
1227        set_bit(CF_CONFIG_NEEDED, &cam->flags);
1228        /*
1229         * Make sure the sensor's power state is correct
1230         */
1231        if (cam->users > 0)
1232                via_sensor_power_up(cam);
1233        else
1234                via_sensor_power_down(cam);
1235        /*
1236         * If it was operating, try to restart it.
1237         */
1238        if (cam->opstate != S_IDLE) {
1239                mutex_lock(&cam->lock);
1240                ret = viacam_configure_sensor(cam);
1241                if (!ret)
1242                        ret = viacam_config_controller(cam);
1243                mutex_unlock(&cam->lock);
1244                if (!ret)
1245                        viacam_start_engine(cam);
1246        }
1247
1248        return ret;
1249}
1250
1251static struct viafb_pm_hooks viacam_pm_hooks = {
1252        .suspend = viacam_suspend,
1253        .resume = viacam_resume
1254};
1255
1256#endif /* CONFIG_PM */
1257
1258/*
1259 * Setup stuff.
1260 */
1261
1262static struct video_device viacam_v4l_template = {
1263        .name           = "via-camera",
1264        .minor          = -1,
1265        .tvnorms        = V4L2_STD_NTSC_M,
1266        .fops           = &viacam_fops,
1267        .ioctl_ops      = &viacam_ioctl_ops,
1268        .release        = video_device_release_empty, /* Check this */
1269};
1270
1271/*
1272 * The OLPC folks put the serial port on the same pin as
1273 * the camera.  They also get grumpy if we break the
1274 * serial port and keep them from using it.  So we have
1275 * to check the serial enable bit and not step on it.
1276 */
1277#define VIACAM_SERIAL_DEVFN 0x88
1278#define VIACAM_SERIAL_CREG 0x46
1279#define VIACAM_SERIAL_BIT 0x40
1280
1281static bool viacam_serial_is_enabled(void)
1282{
1283        struct pci_bus *pbus = pci_find_bus(0, 0);
1284        u8 cbyte;
1285
1286        if (!pbus)
1287                return false;
1288        pci_bus_read_config_byte(pbus, VIACAM_SERIAL_DEVFN,
1289                        VIACAM_SERIAL_CREG, &cbyte);
1290        if ((cbyte & VIACAM_SERIAL_BIT) == 0)
1291                return false; /* Not enabled */
1292        if (!override_serial) {
1293                printk(KERN_NOTICE "Via camera: serial port is enabled, " \
1294                                "refusing to load.\n");
1295                printk(KERN_NOTICE "Specify override_serial=1 to force " \
1296                                "module loading.\n");
1297                return true;
1298        }
1299        printk(KERN_NOTICE "Via camera: overriding serial port\n");
1300        pci_bus_write_config_byte(pbus, VIACAM_SERIAL_DEVFN,
1301                        VIACAM_SERIAL_CREG, cbyte & ~VIACAM_SERIAL_BIT);
1302        return false;
1303}
1304
1305static struct ov7670_config sensor_cfg = {
1306        /* The XO-1.5 (only known user) clocks the camera at 90MHz. */
1307        .clock_speed = 90,
1308};
1309
1310static int viacam_probe(struct platform_device *pdev)
1311{
1312        int ret;
1313        struct i2c_adapter *sensor_adapter;
1314        struct viafb_dev *viadev = pdev->dev.platform_data;
1315        struct i2c_board_info ov7670_info = {
1316                .type = "ov7670",
1317                .addr = 0x42 >> 1,
1318                .platform_data = &sensor_cfg,
1319        };
1320
1321        /*
1322         * Note that there are actually two capture channels on
1323         * the device.  We only deal with one for now.  That
1324         * is encoded here; nothing else assumes it's dealing with
1325         * a unique capture device.
1326         */
1327        struct via_camera *cam;
1328
1329        /*
1330         * Ensure that frame buffer memory has been set aside for
1331         * this purpose.  As an arbitrary limit, refuse to work
1332         * with less than two frames of VGA 16-bit data.
1333         *
1334         * If we ever support the second port, we'll need to set
1335         * aside more memory.
1336         */
1337        if (viadev->camera_fbmem_size < (VGA_HEIGHT*VGA_WIDTH*4)) {
1338                printk(KERN_ERR "viacam: insufficient FB memory reserved\n");
1339                return -ENOMEM;
1340        }
1341        if (viadev->engine_mmio == NULL) {
1342                printk(KERN_ERR "viacam: No I/O memory, so no pictures\n");
1343                return -ENOMEM;
1344        }
1345
1346        if (machine_is_olpc() && viacam_serial_is_enabled())
1347                return -EBUSY;
1348
1349        /*
1350         * Basic structure initialization.
1351         */
1352        cam = kzalloc (sizeof(struct via_camera), GFP_KERNEL);
1353        if (cam == NULL)
1354                return -ENOMEM;
1355        via_cam_info = cam;
1356        cam->platdev = pdev;
1357        cam->viadev = viadev;
1358        cam->users = 0;
1359        cam->owner = NULL;
1360        cam->opstate = S_IDLE;
1361        cam->user_format = cam->sensor_format = viacam_def_pix_format;
1362        mutex_init(&cam->lock);
1363        INIT_LIST_HEAD(&cam->buffer_queue);
1364        cam->mmio = viadev->engine_mmio;
1365        cam->fbmem = viadev->fbmem;
1366        cam->fb_offset = viadev->camera_fbmem_offset;
1367        cam->flags = 1 << CF_CONFIG_NEEDED;
1368        cam->mbus_code = via_def_mbus_code;
1369        /*
1370         * Tell V4L that we exist.
1371         */
1372        ret = v4l2_device_register(&pdev->dev, &cam->v4l2_dev);
1373        if (ret) {
1374                dev_err(&pdev->dev, "Unable to register v4l2 device\n");
1375                goto out_free;
1376        }
1377        ret = v4l2_ctrl_handler_init(&cam->ctrl_handler, 10);
1378        if (ret)
1379                goto out_unregister;
1380        cam->v4l2_dev.ctrl_handler = &cam->ctrl_handler;
1381        /*
1382         * Convince the system that we can do DMA.
1383         */
1384        pdev->dev.dma_mask = &viadev->pdev->dma_mask;
1385        dma_set_mask(&pdev->dev, 0xffffffff);
1386        /*
1387         * Fire up the capture port.  The write to 0x78 looks purely
1388         * OLPCish; any system will need to tweak 0x1e.
1389         */
1390        via_write_reg_mask(VIASR, 0x78, 0, 0x80);
1391        via_write_reg_mask(VIASR, 0x1e, 0xc0, 0xc0);
1392        /*
1393         * Get the sensor powered up.
1394         */
1395        ret = via_sensor_power_setup(cam);
1396        if (ret)
1397                goto out_ctrl_hdl_free;
1398        via_sensor_power_up(cam);
1399
1400        /*
1401         * See if we can't find it on the bus.  The VIA_PORT_31 assumption
1402         * is OLPC-specific.  0x42 assumption is ov7670-specific.
1403         */
1404        sensor_adapter = viafb_find_i2c_adapter(VIA_PORT_31);
1405        cam->sensor = v4l2_i2c_new_subdev_board(&cam->v4l2_dev, sensor_adapter,
1406                        &ov7670_info, NULL);
1407        if (cam->sensor == NULL) {
1408                dev_err(&pdev->dev, "Unable to find the sensor!\n");
1409                ret = -ENODEV;
1410                goto out_power_down;
1411        }
1412        /*
1413         * Get the IRQ.
1414         */
1415        viacam_int_disable(cam);
1416        ret = request_threaded_irq(viadev->pdev->irq, viacam_quick_irq,
1417                        viacam_irq, IRQF_SHARED, "via-camera", cam);
1418        if (ret)
1419                goto out_power_down;
1420        /*
1421         * Tell V4l2 that we exist.
1422         */
1423        cam->vdev = viacam_v4l_template;
1424        cam->vdev.v4l2_dev = &cam->v4l2_dev;
1425        ret = video_register_device(&cam->vdev, VFL_TYPE_GRABBER, -1);
1426        if (ret)
1427                goto out_irq;
1428        video_set_drvdata(&cam->vdev, cam);
1429
1430#ifdef CONFIG_PM
1431        /*
1432         * Hook into PM events
1433         */
1434        viacam_pm_hooks.private = cam;
1435        viafb_pm_register(&viacam_pm_hooks);
1436#endif
1437
1438        /* Power the sensor down until somebody opens the device */
1439        via_sensor_power_down(cam);
1440        return 0;
1441
1442out_irq:
1443        free_irq(viadev->pdev->irq, cam);
1444out_power_down:
1445        via_sensor_power_release(cam);
1446out_ctrl_hdl_free:
1447        v4l2_ctrl_handler_free(&cam->ctrl_handler);
1448out_unregister:
1449        v4l2_device_unregister(&cam->v4l2_dev);
1450out_free:
1451        kfree(cam);
1452        return ret;
1453}
1454
1455static int viacam_remove(struct platform_device *pdev)
1456{
1457        struct via_camera *cam = via_cam_info;
1458        struct viafb_dev *viadev = pdev->dev.platform_data;
1459
1460        video_unregister_device(&cam->vdev);
1461        v4l2_device_unregister(&cam->v4l2_dev);
1462        free_irq(viadev->pdev->irq, cam);
1463        via_sensor_power_release(cam);
1464        v4l2_ctrl_handler_free(&cam->ctrl_handler);
1465        kfree(cam);
1466        via_cam_info = NULL;
1467        return 0;
1468}
1469
1470static struct platform_driver viacam_driver = {
1471        .driver = {
1472                .name = "viafb-camera",
1473        },
1474        .probe = viacam_probe,
1475        .remove = viacam_remove,
1476};
1477
1478module_platform_driver(viacam_driver);
1479