// SPDX-License-Identifier: GPL-2.0-or-later /* * GSPCA sub driver for W996[78]CF JPEG USB Dual Mode Camera Chip. * * Copyright (C) 2009 Hans de Goede <hdegoede@redhat.com> * * This module is adapted from the in kernel v4l1 w9968cf driver: * * Copyright (C) 2002-2004 by Luca Risolia <luca.risolia@studio.unibo.it>
*/
/* Note this is not a stand alone driver, it gets included in ov519.c, this is a bit of a hack, but it needs the driver code for a lot of different ov sensors which is already present in ov519.c (the old v4l1 driver used the ovchipcam framework). When we have the time we really should move the sensor drivers to v4l2 sub drivers, and properly split of this
driver from ov519.c */
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#define W9968CF_I2C_BUS_DELAY 4 /* delay in us for I2C bit r/w operations */
/*-------------------------------------------------------------------------- Write 64-bit data to the fast serial bus registers. Return 0 on success, -1 otherwise.
--------------------------------------------------------------------------*/ staticvoid w9968cf_write_fsb(struct sd *sd, u16* data)
{ struct usb_device *udev = sd->gspca_dev.dev;
u16 value; int ret;
if (sd->gspca_dev.usb_err < 0) return;
value = *data++;
memcpy(sd->gspca_dev.usb_buf, data, 6);
/* Avoid things going to fast for the bridge with a xhci host */
udelay(150);
ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0,
USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
value, 0x06, sd->gspca_dev.usb_buf, 6, 500); if (ret < 0) {
pr_err("Write FSB registers failed (%d)\n", ret);
sd->gspca_dev.usb_err = ret;
}
}
/*-------------------------------------------------------------------------- Write data to the serial bus control register. Return 0 on success, a negative number otherwise.
--------------------------------------------------------------------------*/ staticvoid w9968cf_write_sb(struct sd *sd, u16 value)
{ int ret;
if (sd->gspca_dev.usb_err < 0) return;
/* Avoid things going to fast for the bridge with a xhci host */
udelay(150);
/* We don't use reg_w here, as that would cause all writes when
bitbanging i2c to be logged, making the logs impossible to read */
ret = usb_control_msg(sd->gspca_dev.dev,
usb_sndctrlpipe(sd->gspca_dev.dev, 0),
0,
USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
value, 0x01, NULL, 0, 500);
/*-------------------------------------------------------------------------- Read data from the serial bus control register. Return 0 on success, a negative number otherwise.
--------------------------------------------------------------------------*/ staticint w9968cf_read_sb(struct sd *sd)
{ int ret;
if (sd->gspca_dev.usb_err < 0) return -1;
/* Avoid things going to fast for the bridge with a xhci host */
udelay(150);
/* We don't use reg_r here, as the w9968cf is special and has 16
bit registers instead of 8 bit */
ret = usb_control_msg(sd->gspca_dev.dev,
usb_rcvctrlpipe(sd->gspca_dev.dev, 0),
1,
USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
0, 0x01, sd->gspca_dev.usb_buf, 2, 500); if (ret >= 0) {
ret = sd->gspca_dev.usb_buf[0] |
(sd->gspca_dev.usb_buf[1] << 8);
} else {
pr_err("Read SB reg [01] failed\n");
sd->gspca_dev.usb_err = ret; /* * Make sure the buffer is zeroed to avoid uninitialized * values.
*/
memset(sd->gspca_dev.usb_buf, 0, 2);
}
udelay(W9968CF_I2C_BUS_DELAY);
return ret;
}
/*-------------------------------------------------------------------------- Upload quantization tables for the JPEG compression. This function is called by w9968cf_start_transfer(). Return 0 on success, a negative number otherwise.
--------------------------------------------------------------------------*/ staticvoid w9968cf_upload_quantizationtables(struct sd *sd)
{
u16 a, b; int i, j;
reg_w(sd, 0x39, 0x0010); /* JPEG clock enable */
for (i = 0, j = 0; i < 32; i++, j += 2) {
a = Y_QUANTABLE[j] | ((unsigned)(Y_QUANTABLE[j + 1]) << 8);
b = UV_QUANTABLE[j] | ((unsigned)(UV_QUANTABLE[j + 1]) << 8);
reg_w(sd, 0x40 + i, a);
reg_w(sd, 0x60 + i, b);
}
reg_w(sd, 0x39, 0x0012); /* JPEG encoder enable */
}
/**************************************************************************** * Low-level I2C I/O functions. * * The adapter supports the following I2C transfer functions: * * i2c_adap_fastwrite_byte_data() (at 400 kHz bit frequency only) * * i2c_adap_read_byte_data() * * i2c_adap_read_byte() *
****************************************************************************/
/* No need to ensure SDA is high as we are always called after
read_ack which ends with SDA high */
*v = 0; for (bit = 0 ; bit < 8 ; bit++) {
*v <<= 1; /* SDE=1, SDA=1, SCL=1 */
w9968cf_write_sb(sd, 0x0013);
*v |= (w9968cf_read_sb(sd) & 0x0008) ? 1 : 0; /* SDE=1, SDA=1, SCL=0 */
w9968cf_write_sb(sd, 0x0012);
}
}
staticvoid w9968cf_smbus_write_nack(struct sd *sd)
{ /* No need to ensure SDA is high as we are always called after
read_byte which ends with SDA high */
w9968cf_write_sb(sd, 0x0013); /* SDE=1, SDA=1, SCL=1 */
w9968cf_write_sb(sd, 0x0012); /* SDE=1, SDA=1, SCL=0 */
}
/* SMBus protocol: S Addr Wr [A] Subaddr [A] P S Addr+1 Rd [A] [Value] NA P */ staticint w9968cf_i2c_r(struct sd *sd, u8 reg)
{ struct gspca_dev *gspca_dev = (struct gspca_dev *)sd; int ret = 0;
u8 value;
/* Fast serial bus data control disable */
w9968cf_write_sb(sd, 0x0013); /* don't change ! */
w9968cf_smbus_start(sd);
w9968cf_smbus_write_byte(sd, sd->sensor_addr);
w9968cf_smbus_read_ack(sd);
w9968cf_smbus_write_byte(sd, reg);
w9968cf_smbus_read_ack(sd);
w9968cf_smbus_stop(sd);
w9968cf_smbus_start(sd);
w9968cf_smbus_write_byte(sd, sd->sensor_addr + 1);
w9968cf_smbus_read_ack(sd);
w9968cf_smbus_read_byte(sd, &value); /* signal we don't want to read anymore, the v4l1 driver used to send an ack here which is very wrong! (and then fixed
the issues this gave by retrying reads) */
w9968cf_smbus_write_nack(sd);
w9968cf_smbus_stop(sd);
/* Fast serial bus data control re-enable */
w9968cf_write_sb(sd, 0x0030);
/*-------------------------------------------------------------------------- Turn on the LED on some webcams. A beep should be heard too. Return 0 on success, a negative number otherwise.
--------------------------------------------------------------------------*/ staticvoid w9968cf_configure(struct sd *sd)
{
reg_w(sd, 0x00, 0xff00); /* power-down */
reg_w(sd, 0x00, 0xbf17); /* reset everything */
reg_w(sd, 0x00, 0xbf10); /* normal operation */
reg_w(sd, 0x01, 0x0010); /* serial bus, SDS high */
reg_w(sd, 0x01, 0x0000); /* serial bus, SDS low */
reg_w(sd, 0x01, 0x0010); /* ..high 'beep-beep' */
reg_w(sd, 0x01, 0x0030); /* Set sda scl to FSB mode */
if (sd->sensor == SEN_OV7620) { /* * Sigh, this is dependend on the clock / framerate changes * made by the frequency control, sick. * * Note we cannot use v4l2_ctrl_g_ctrl here, as we get called * from ov519.c:setfreq() with the ctrl lock held!
*/ if (sd->freq->val == 1) {
start_cropx = 277;
start_cropy = 37;
} else {
start_cropx = 105;
start_cropy = 37;
}
} else {
start_cropx = 320;
start_cropy = 35;
}
/* Work around to avoid FP arithmetic */ #define SC(x) ((x) << 10)
/* Transfer size in WORDS (for UYVY format only) */
val = sd->gspca_dev.pixfmt.width * sd->gspca_dev.pixfmt.height;
reg_w(sd, 0x3d, val & 0xffff); /* low bits */
reg_w(sd, 0x3e, val >> 16); /* high bits */
if (w9968cf_vga_mode[sd->gspca_dev.curr_mode].pixelformat ==
V4L2_PIX_FMT_JPEG) { /* We may get called multiple times (usb isoc bw negotiat.) */
jpeg_define(sd->jpeg_hdr, sd->gspca_dev.pixfmt.height,
sd->gspca_dev.pixfmt.width, 0x22); /* JPEG 420 */
jpeg_set_qual(sd->jpeg_hdr, v4l2_ctrl_g_ctrl(sd->jpegqual));
w9968cf_upload_quantizationtables(sd);
v4l2_ctrl_grab(sd->jpegqual, true);
}
/* Video Capture Control Register */ if (sd->sensor == SEN_OV7620) { /* Seems to work around a bug in the image sensor */
vs_polarity = 1;
hs_polarity = 1;
} else {
vs_polarity = 1;
hs_polarity = 0;
}
val = (vs_polarity << 12) | (hs_polarity << 11);
/* NOTE: We may not have enough memory to do double buffering while doing compression (amount of memory differs per model cam). So we use the second image buffer also as jpeg stream buffer
(see w9968cf_init), and disable double buffering. */ if (w9968cf_vga_mode[sd->gspca_dev.curr_mode].pixelformat ==
V4L2_PIX_FMT_JPEG) { /* val |= 0x0002; YUV422P */
val |= 0x0003; /* YUV420P */
} else
val |= 0x0080; /* Enable HW double buffering */
/* val |= 0x0020; enable clamping */ /* val |= 0x0008; enable (1-2-1) filter */ /* val |= 0x000c; enable (2-3-6-3-2) filter */
/* The w9968cf docs say that a 0 sized packet means EOF (and also SOF for the next frame). This seems to simply not be true when operating in JPEG mode, in this case there may be empty packets within the frame. So in JPEG mode use the JPEG SOI marker to detect SOF.
Note to make things even more interesting the w9968cf sends *PLANAR* jpeg, to be precise it sends: SOI, SOF, DRI, SOS, Y-data, SOS, U-data, SOS,
V-data, EOI. */ staticvoid w9968cf_pkt_scan(struct gspca_dev *gspca_dev,
u8 *data, /* isoc packet */ int len) /* iso packet length */
{ struct sd *sd = (struct sd *) gspca_dev;
if (w9968cf_vga_mode[gspca_dev->curr_mode].pixelformat ==
V4L2_PIX_FMT_JPEG) { if (len >= 2 &&
data[0] == 0xff &&
data[1] == 0xd8) {
gspca_frame_add(gspca_dev, LAST_PACKET,
NULL, 0);
gspca_frame_add(gspca_dev, FIRST_PACKET,
sd->jpeg_hdr, JPEG_HDR_SZ); /* Strip the ff d8, our own header (which adds
huffman and quantization tables) already has this */
len -= 2;
data += 2;
}
} else { /* In UYVY mode an empty packet signals EOF */ if (gspca_dev->empty_packet) {
gspca_frame_add(gspca_dev, LAST_PACKET,
NULL, 0);
gspca_frame_add(gspca_dev, FIRST_PACKET,
NULL, 0);
gspca_dev->empty_packet = 0;
}
}
gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
}