root/drivers/media/platform/via-camera.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. via_find_format
  2. via_sensor_power_setup
  3. via_sensor_power_up
  4. via_sensor_power_down
  5. via_sensor_power_release
  6. viacam_set_flip
  7. viacam_configure_sensor
  8. viacam_write_reg
  9. viacam_read_reg
  10. viacam_write_reg_mask
  11. viacam_quick_irq
  12. viacam_next_buffer
  13. viacam_irq
  14. viacam_int_enable
  15. viacam_int_disable
  16. viacam_ctlr_cbufs
  17. viacam_set_scale
  18. viacam_ctlr_image
  19. viacam_config_controller
  20. viacam_start_engine
  21. viacam_stop_engine
  22. vb2_to_via_buffer
  23. viacam_vb2_queue
  24. viacam_vb2_prepare
  25. viacam_vb2_queue_setup
  26. viacam_vb2_start_streaming
  27. viacam_vb2_stop_streaming
  28. viacam_open
  29. viacam_release
  30. viacam_enum_input
  31. viacam_g_input
  32. viacam_s_input
  33. viacam_enum_fmt_vid_cap
  34. viacam_fmt_pre
  35. viacam_fmt_post
  36. viacam_do_try_fmt
  37. viacam_try_fmt_vid_cap
  38. viacam_g_fmt_vid_cap
  39. viacam_s_fmt_vid_cap
  40. viacam_querycap
  41. viacam_g_parm
  42. viacam_s_parm
  43. viacam_enum_framesizes
  44. viacam_enum_frameintervals
  45. viacam_suspend
  46. viacam_resume
  47. viacam_serial_is_enabled
  48. viacam_probe
  49. viacam_remove

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

/* [<][>][^][v][top][bottom][index][help] */