MODULE_ALIAS("platform:viafb-camera");
MODULE_AUTHOR("Jonathan Corbet ");
MODULE_DESCRIPTION("VIA framebuffer-based camera controller driver");
MODULE_LICENSE("GPL");
staticbool flip_image;
module_param(flip_image, bool, 0444);
MODULE_PARM_DESC(flip_image, "If set, the sensor will be instructed to flip the image vertically.");
staticbool override_serial;
module_param(override_serial, bool, 0444);
MODULE_PARM_DESC(override_serial, "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.");
struct via_camera { struct v4l2_device v4l2_dev; struct v4l2_ctrl_handler ctrl_handler; struct video_device vdev; struct v4l2_subdev *sensor; struct platform_device *platdev; struct viafb_dev *viadev; struct mutex lock; enum viacam_opstate opstate; unsignedlong flags; struct pm_qos_request qos_request; /* * GPIO info for power/reset management
*/ struct gpio_desc *power_gpio; struct gpio_desc *reset_gpio; /* * I/O memory stuff.
*/ void __iomem *mmio; /* Where the registers live */ void __iomem *fbmem; /* Frame buffer memory */
u32 fb_offset; /* Reserved memory offset (FB) */ /* * Capture buffers and related. The controller supports * up to three, so that's what we have here. These buffers * live in frame buffer memory, so we don't call them "DMA".
*/ unsignedint cb_offsets[3]; /* offsets into fb mem */
u8 __iomem *cb_addrs[3]; /* Kernel-space addresses */ int n_cap_bufs; /* How many are we using? */ struct vb2_queue vq; struct list_head buffer_queue;
u32 sequence; /* * Video format information. sensor_format is kept in a form * that we can use to pass to the sensor. We always run the * sensor in VGA resolution, though, and let the controller * downscale things if need be. So we keep the "real* * dimensions separately.
*/ struct v4l2_pix_format sensor_format; struct v4l2_pix_format user_format;
u32 mbus_code;
};
/* buffer for one video frame */ struct via_buffer { /* common v4l buffer stuff -- must be first */ struct vb2_v4l2_buffer vbuf; struct list_head queue;
};
/* * Yes, this is a hack, but there's only going to be one of these * on any system we know of.
*/ staticstruct via_camera *via_cam_info;
/* * Flag values, manipulated with bitops
*/ #define CF_DMA_ACTIVE 0 /* A frame is incoming */ #define CF_CONFIG_NEEDED 1 /* Must configure hardware */
/* * Format handling. This is ripped almost directly from Hans's changes * to cafe_ccic.c. It's a little unfortunate; until this change, we * didn't need to know anything about the format except its byte depth; * now this information must be managed at this level too.
*/ staticstruct via_format {
__u32 pixelformat; int bpp; /* Bytes per pixel */
u32 mbus_code;
} via_formats[] = {
{
.pixelformat = V4L2_PIX_FMT_YUYV,
.mbus_code = MEDIA_BUS_FMT_YUYV8_2X8,
.bpp = 2,
}, /* RGB444 and Bayer should be doable, but have never been tested with this driver. RGB565 seems to work at the default resolution, but results in color corruption when being scaled by
viacam_set_scaled(), and is disabled as a result. */
}; #define N_VIA_FMTS ARRAY_SIZE(via_formats)
for (i = 0; i < N_VIA_FMTS; i++) if (via_formats[i].pixelformat == pixelformat) return via_formats + i; /* Not found? Then return the first format. */ return via_formats;
}
/*--------------------------------------------------------------------------*/ /* * Sensor power/reset management. This piece is OLPC-specific for * sure; other configurations will have things connected differently.
*/ staticint via_sensor_power_setup(struct via_camera *cam)
{ struct device *dev = &cam->platdev->dev;
cam->power_gpio = devm_gpiod_get(dev, "VGPIO3", GPIOD_OUT_LOW); if (IS_ERR(cam->power_gpio)) return dev_err_probe(dev, PTR_ERR(cam->power_gpio), "failed to get power GPIO");
/* Request the reset line asserted */
cam->reset_gpio = devm_gpiod_get(dev, "VGPIO2", GPIOD_OUT_HIGH); if (IS_ERR(cam->reset_gpio)) return dev_err_probe(dev, PTR_ERR(cam->reset_gpio), "failed to get reset GPIO");
return 0;
}
/* * Power up the sensor and perform the reset dance.
*/ staticvoid via_sensor_power_up(struct via_camera *cam)
{
gpiod_set_value(cam->power_gpio, 1);
gpiod_set_value(cam->reset_gpio, 1);
msleep(20); /* Probably excessive */
gpiod_set_value(cam->reset_gpio, 0);
msleep(20);
}
/* * Configure the sensor. It's up to the caller to ensure * that the camera is in the correct operating state.
*/ staticint viacam_configure_sensor(struct via_camera *cam)
{ struct v4l2_subdev_format format = {
.which = V4L2_SUBDEV_FORMAT_ACTIVE,
}; int ret;
v4l2_fill_mbus_format(&format.format, &cam->sensor_format, cam->mbus_code);
ret = sensor_call(cam, core, init, 0); if (ret == 0)
ret = sensor_call(cam, pad, set_fmt, NULL, &format); /* * OV7670 does weird things if flip is set *before* format...
*/ if (ret == 0)
ret = viacam_set_flip(cam); return ret;
}
/* --------------------------------------------------------------------------*/ /* * Some simple register accessors; they assume that the lock is held. * * Should we want to support the second capture engine, we could * hide the register difference by adding 0x1000 to registers in the * 0x300-350 range.
*/ staticinlinevoid viacam_write_reg(struct via_camera *cam, int reg, int value)
{
iowrite32(value, cam->mmio + reg);
}
/* --------------------------------------------------------------------------*/ /* Interrupt management and handling */
static irqreturn_t viacam_quick_irq(int irq, void *data)
{ struct via_camera *cam = data;
irqreturn_t ret = IRQ_NONE; int icv;
/* * All we do here is to clear the interrupts and tell * the handler thread to wake up.
*/
spin_lock(&cam->viadev->reg_lock);
icv = viacam_read_reg(cam, VCR_INTCTRL); if (icv & VCR_IC_EAV) {
icv |= VCR_IC_EAV|VCR_IC_EVBI|VCR_IC_FFULL;
viacam_write_reg(cam, VCR_INTCTRL, icv);
ret = IRQ_WAKE_THREAD;
}
spin_unlock(&cam->viadev->reg_lock); return ret;
}
/* * Find the next buffer which has somebody waiting on it.
*/ staticstruct via_buffer *viacam_next_buffer(struct via_camera *cam)
{ if (cam->opstate != S_RUNNING) return NULL; if (list_empty(&cam->buffer_queue)) return NULL; return list_entry(cam->buffer_queue.next, struct via_buffer, queue);
}
mutex_lock(&cam->lock); /* * If there is no place to put the data frame, don't bother * with anything else.
*/
vb = viacam_next_buffer(cam); if (vb == NULL) goto done; /* * Figure out which buffer we just completed.
*/
bufn = (viacam_read_reg(cam, VCR_INTCTRL) & VCR_IC_ACTBUF) >> 3;
bufn -= 1; if (bufn < 0)
bufn = cam->n_cap_bufs - 1; /* * Copy over the data and let any waiters know.
*/
sgt = vb2_dma_sg_plane_desc(&vb->vbuf.vb2_buf, 0);
vb->vbuf.vb2_buf.timestamp = ktime_get_ns();
viafb_dma_copy_out_sg(cam->cb_offsets[bufn], sgt->sgl, sgt->nents);
vb->vbuf.sequence = cam->sequence++;
vb->vbuf.field = V4L2_FIELD_NONE;
list_del(&vb->queue);
vb2_buffer_done(&vb->vbuf.vb2_buf, VB2_BUF_STATE_DONE);
done:
mutex_unlock(&cam->lock); return IRQ_HANDLED;
}
/* * These functions must mess around with the general interrupt * control register, which is relevant to much more than just the * camera. Nothing else uses interrupts, though, as of this writing. * Should that situation change, we'll have to improve support at * the via-core level.
*/ staticvoid viacam_int_enable(struct via_camera *cam)
{
viacam_write_reg(cam, VCR_INTCTRL,
VCR_IC_INTEN|VCR_IC_EAV|VCR_IC_EVBI|VCR_IC_FFULL);
viafb_irq_enable(VDE_I_C0AVEN);
}
/* * Set up our capture buffers in framebuffer memory.
*/ staticint viacam_ctlr_cbufs(struct via_camera *cam)
{ int nbuf = cam->viadev->camera_fbmem_size/cam->sensor_format.sizeimage; int i; unsignedint offset;
/* * See how many buffers we can work with.
*/ if (nbuf >= 3) {
cam->n_cap_bufs = 3;
viacam_write_reg_mask(cam, VCR_CAPINTC, VCR_CI_3BUFS,
VCR_CI_3BUFS);
} elseif (nbuf == 2) {
cam->n_cap_bufs = 2;
viacam_write_reg_mask(cam, VCR_CAPINTC, 0, VCR_CI_3BUFS);
} else {
cam_warn(cam, "Insufficient frame buffer memory\n"); return -ENOMEM;
} /* * Set them up.
*/
offset = cam->fb_offset; for (i = 0; i < cam->n_cap_bufs; i++) {
cam->cb_offsets[i] = offset;
cam->cb_addrs[i] = cam->fbmem + offset;
viacam_write_reg(cam, VCR_VBUF1 + i*4, offset & VCR_VBUF_MASK);
offset += cam->sensor_format.sizeimage;
} return 0;
}
/* * Set the scaling register for downscaling the image. * * This register works like this... Vertical scaling is enabled * by bit 26; if that bit is set, downscaling is controlled by the * value in bits 16:25. Those bits are divided by 1024 to get * the scaling factor; setting just bit 25 thus cuts the height * in half. * * Horizontal scaling works about the same, but it's enabled by * bit 11, with bits 0:10 giving the numerator of a fraction * (over 2048) for the scaling value. * * This function is naive in that, if the user departs from * the 3x4 VGA scaling factor, the image will distort. We * could work around that if it really seemed important.
*/ staticvoid viacam_set_scale(struct via_camera *cam)
{ unsignedint avscale; int sf;
if (cam->user_format.width == VGA_WIDTH)
avscale = 0; else {
sf = (cam->user_format.width*2048)/VGA_WIDTH;
avscale = VCR_AVS_HEN | sf;
} if (cam->user_format.height < VGA_HEIGHT) {
sf = (1024*cam->user_format.height)/VGA_HEIGHT;
avscale |= VCR_AVS_VEN | (sf << 16);
}
viacam_write_reg(cam, VCR_AVSCALE, avscale);
}
/* * Configure image-related information into the capture engine.
*/ staticvoid viacam_ctlr_image(struct via_camera *cam)
{ int cicreg;
/* * Disable clock before messing with stuff - from the via * sample driver.
*/
viacam_write_reg(cam, VCR_CAPINTC, ~(VCR_CI_ENABLE|VCR_CI_CLKEN)); /* * Set up the controller for VGA resolution, modulo magic * offsets from the via sample driver.
*/
viacam_write_reg(cam, VCR_HORRANGE, 0x06200120);
viacam_write_reg(cam, VCR_VERTRANGE, 0x01de0000);
viacam_set_scale(cam); /* * Image size info.
*/
viacam_write_reg(cam, VCR_MAXDATA,
(cam->sensor_format.height << 16) |
(cam->sensor_format.bytesperline >> 3));
viacam_write_reg(cam, VCR_MAXVBI, 0);
viacam_write_reg(cam, VCR_VSTRIDE,
cam->user_format.bytesperline & VCR_VS_STRIDE); /* * Set up the capture interface control register, * everything but the "go" bit. * * The FIFO threshold is a bit of a magic number; 8 is what * VIA's sample code uses.
*/
cicreg = VCR_CI_CLKEN |
0x08000000 | /* FIFO threshold */
VCR_CI_FLDINV | /* OLPC-specific? */
VCR_CI_VREFINV | /* OLPC-specific? */
VCR_CI_DIBOTH | /* Capture both fields */
VCR_CI_CCIR601_8; if (cam->n_cap_bufs == 3)
cicreg |= VCR_CI_3BUFS; /* * YUV formats need different byte swapping than RGB.
*/ if (cam->user_format.pixelformat == V4L2_PIX_FMT_YUYV)
cicreg |= VCR_CI_YUYV; else
cicreg |= VCR_CI_UYVY;
viacam_write_reg(cam, VCR_CAPINTC, cicreg);
}
staticint viacam_config_controller(struct via_camera *cam)
{ int ret; unsignedlong flags;
spin_lock_irqsave(&cam->viadev->reg_lock, flags);
ret = viacam_ctlr_cbufs(cam); if (!ret)
viacam_ctlr_image(cam);
spin_unlock_irqrestore(&cam->viadev->reg_lock, flags);
clear_bit(CF_CONFIG_NEEDED, &cam->flags); return ret;
}
/* * Make it start grabbing data.
*/ staticvoid viacam_start_engine(struct via_camera *cam)
{
spin_lock_irq(&cam->viadev->reg_lock);
viacam_write_reg_mask(cam, VCR_CAPINTC, VCR_CI_ENABLE, VCR_CI_ENABLE);
viacam_int_enable(cam);
(void) viacam_read_reg(cam, VCR_CAPINTC); /* Force post */
cam->opstate = S_RUNNING;
spin_unlock_irq(&cam->viadev->reg_lock);
}
if (*num_planes) return sizes[0] < size ? -EINVAL : 0;
*num_planes = 1;
sizes[0] = size; return 0;
}
staticint viacam_vb2_start_streaming(struct vb2_queue *vq, unsignedint count)
{ struct via_camera *cam = vb2_get_drv_priv(vq); struct via_buffer *buf, *tmp; int ret = 0;
if (cam->opstate != S_IDLE) {
ret = -EBUSY; goto out;
} /* * Configure things if need be.
*/ if (test_bit(CF_CONFIG_NEEDED, &cam->flags)) {
ret = viacam_configure_sensor(cam); if (ret) goto out;
ret = viacam_config_controller(cam); if (ret) goto out;
}
cam->sequence = 0; /* * If the CPU goes into C3, the DMA transfer gets corrupted and * users start filing unsightly bug reports. Put in a "latency" * requirement which will keep the CPU out of the deeper sleep * states.
*/
cpu_latency_qos_add_request(&cam->qos_request, 50);
viacam_start_engine(cam); return 0;
out:
list_for_each_entry_safe(buf, tmp, &cam->buffer_queue, queue) {
list_del(&buf->queue);
vb2_buffer_done(&buf->vbuf.vb2_buf, VB2_BUF_STATE_QUEUED);
} return ret;
}
/* * Note the new user. If this is the first one, we'll also * need to power up the sensor.
*/
mutex_lock(&cam->lock);
ret = v4l2_fh_open(filp); if (ret) goto out; if (v4l2_fh_is_singular_file(filp)) {
ret = viafb_request_dma();
mutex_lock(&cam->lock);
last_open = v4l2_fh_is_singular_file(filp);
_vb2_fop_release(filp, NULL); /* * Last one out needs to turn out the lights.
*/ if (last_open) {
via_sensor_power_down(cam);
viafb_release_dma();
}
mutex_unlock(&cam->lock); return 0;
}
/* * Camera must be idle or we can't mess with the * video setup.
*/ if (cam->opstate != S_IDLE) return -EBUSY; /* * Let the sensor code look over and tweak the * requested formatting.
*/
ret = viacam_do_try_fmt(cam, &fmt->fmt.pix, &sfmt.fmt.pix); if (ret) return ret; /* * OK, let's commit to the new format.
*/
cam->user_format = fmt->fmt.pix;
cam->sensor_format = sfmt.fmt.pix;
cam->mbus_code = f->mbus_code;
ret = viacam_configure_sensor(cam); if (!ret)
ret = viacam_config_controller(cam); return ret;
}
if (cam->opstate != S_IDLE) {
viacam_stop_engine(cam);
cam->opstate = state; /* So resume restarts */
}
return 0;
}
staticint viacam_resume(void *priv)
{ struct via_camera *cam = priv; int ret = 0;
/* * Get back to a reasonable operating state.
*/
via_write_reg_mask(VIASR, 0x78, 0, 0x80);
via_write_reg_mask(VIASR, 0x1e, 0xc0, 0xc0);
viacam_int_disable(cam);
set_bit(CF_CONFIG_NEEDED, &cam->flags); /* * Make sure the sensor's power state is correct
*/ if (!list_empty(&cam->vdev.fh_list))
via_sensor_power_up(cam); else
via_sensor_power_down(cam); /* * If it was operating, try to restart it.
*/ if (cam->opstate != S_IDLE) {
mutex_lock(&cam->lock);
ret = viacam_configure_sensor(cam); if (!ret)
ret = viacam_config_controller(cam);
mutex_unlock(&cam->lock); if (!ret)
viacam_start_engine(cam);
}
/* * The OLPC folks put the serial port on the same pin as * the camera. They also get grumpy if we break the * serial port and keep them from using it. So we have * to check the serial enable bit and not step on it.
*/ #define VIACAM_SERIAL_DEVFN 0x88 #define VIACAM_SERIAL_CREG 0x46 #define VIACAM_SERIAL_BIT 0x40
/* * Note that there are actually two capture channels on * the device. We only deal with one for now. That * is encoded here; nothing else assumes it's dealing with * a unique capture device.
*/ struct via_camera *cam;
/* * Ensure that frame buffer memory has been set aside for * this purpose. As an arbitrary limit, refuse to work * with less than two frames of VGA 16-bit data. * * If we ever support the second port, we'll need to set * aside more memory.
*/ if (viadev->camera_fbmem_size < (VGA_HEIGHT*VGA_WIDTH*4)) {
printk(KERN_ERR "viacam: insufficient FB memory reserved\n"); return -ENOMEM;
} if (viadev->engine_mmio == NULL) {
printk(KERN_ERR "viacam: No I/O memory, so no pictures\n"); return -ENOMEM;
}
if (machine_is_olpc() && viacam_serial_is_enabled()) return -EBUSY;
/* * Basic structure initialization.
*/
cam = kzalloc (sizeof(struct via_camera), GFP_KERNEL); if (cam == NULL) return -ENOMEM;
via_cam_info = cam;
cam->platdev = pdev;
cam->viadev = viadev;
cam->opstate = S_IDLE;
cam->user_format = cam->sensor_format = viacam_def_pix_format;
mutex_init(&cam->lock);
INIT_LIST_HEAD(&cam->buffer_queue);
cam->mmio = viadev->engine_mmio;
cam->fbmem = viadev->fbmem;
cam->fb_offset = viadev->camera_fbmem_offset;
cam->flags = 1 << CF_CONFIG_NEEDED;
cam->mbus_code = via_def_mbus_code; /* * Tell V4L that we exist.
*/
ret = v4l2_device_register(&pdev->dev, &cam->v4l2_dev); if (ret) {
dev_err(&pdev->dev, "Unable to register v4l2 device\n"); goto out_free;
}
ret = v4l2_ctrl_handler_init(&cam->ctrl_handler, 10); if (ret) goto out_unregister;
cam->v4l2_dev.ctrl_handler = &cam->ctrl_handler; /* * Convince the system that we can do DMA.
*/
pdev->dev.dma_mask = &viadev->pdev->dma_mask;
ret = dma_set_mask(&pdev->dev, 0xffffffff); if (ret) goto out_ctrl_hdl_free; /* * Fire up the capture port. The write to 0x78 looks purely * OLPCish; any system will need to tweak 0x1e.
*/
via_write_reg_mask(VIASR, 0x78, 0, 0x80);
via_write_reg_mask(VIASR, 0x1e, 0xc0, 0xc0); /* * Get the sensor powered up.
*/
ret = via_sensor_power_setup(cam); if (ret) goto out_ctrl_hdl_free;
via_sensor_power_up(cam);
/* * See if we can't find it on the bus. The VIA_PORT_31 assumption * is OLPC-specific. 0x42 assumption is ov7670-specific.
*/
sensor_adapter = viafb_find_i2c_adapter(VIA_PORT_31);
cam->sensor = v4l2_i2c_new_subdev_board(&cam->v4l2_dev, sensor_adapter,
&ov7670_info, NULL); if (cam->sensor == NULL) {
dev_err(&pdev->dev, "Unable to find the sensor!\n");
ret = -ENODEV; goto out_power_down;
} /* * Get the IRQ.
*/
viacam_int_disable(cam);
ret = request_threaded_irq(viadev->pdev->irq, viacam_quick_irq,
viacam_irq, IRQF_SHARED, "via-camera", cam); if (ret) goto out_power_down;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.