Path: blob/master/drivers/media/video/gspca/w996Xcf.c
17653 views
/**1*2* GSPCA sub driver for W996[78]CF JPEG USB Dual Mode Camera Chip.3*4* Copyright (C) 2009 Hans de Goede <[email protected]>5*6* This module is adapted from the in kernel v4l1 w9968cf driver:7*8* Copyright (C) 2002-2004 by Luca Risolia <[email protected]>9*10* This program is free software; you can redistribute it and/or modify11* it under the terms of the GNU General Public License as published by12* the Free Software Foundation; either version 2 of the License, or13* any later version.14*15* This program is distributed in the hope that it will be useful,16* but WITHOUT ANY WARRANTY; without even the implied warranty of17* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the18* GNU General Public License for more details.19*20* You should have received a copy of the GNU General Public License21* along with this program; if not, write to the Free Software22* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA23*24*/2526/* Note this is not a stand alone driver, it gets included in ov519.c, this27is a bit of a hack, but it needs the driver code for a lot of different28ov sensors which is already present in ov519.c (the old v4l1 driver used29the ovchipcam framework). When we have the time we really should move30the sensor drivers to v4l2 sub drivers, and properly split of this31driver from ov519.c */3233#define W9968CF_I2C_BUS_DELAY 4 /* delay in us for I2C bit r/w operations */3435#define Y_QUANTABLE (&sd->jpeg_hdr[JPEG_QT0_OFFSET])36#define UV_QUANTABLE (&sd->jpeg_hdr[JPEG_QT1_OFFSET])3738static const struct v4l2_pix_format w9968cf_vga_mode[] = {39{160, 120, V4L2_PIX_FMT_UYVY, V4L2_FIELD_NONE,40.bytesperline = 160 * 2,41.sizeimage = 160 * 120 * 2,42.colorspace = V4L2_COLORSPACE_JPEG},43{176, 144, V4L2_PIX_FMT_UYVY, V4L2_FIELD_NONE,44.bytesperline = 176 * 2,45.sizeimage = 176 * 144 * 2,46.colorspace = V4L2_COLORSPACE_JPEG},47{320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,48.bytesperline = 320 * 2,49.sizeimage = 320 * 240 * 2,50.colorspace = V4L2_COLORSPACE_JPEG},51{352, 288, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,52.bytesperline = 352 * 2,53.sizeimage = 352 * 288 * 2,54.colorspace = V4L2_COLORSPACE_JPEG},55{640, 480, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,56.bytesperline = 640 * 2,57.sizeimage = 640 * 480 * 2,58.colorspace = V4L2_COLORSPACE_JPEG},59};6061static void reg_w(struct sd *sd, u16 index, u16 value);6263/*--------------------------------------------------------------------------64Write 64-bit data to the fast serial bus registers.65Return 0 on success, -1 otherwise.66--------------------------------------------------------------------------*/67static void w9968cf_write_fsb(struct sd *sd, u16* data)68{69struct usb_device *udev = sd->gspca_dev.dev;70u16 value;71int ret;7273if (sd->gspca_dev.usb_err < 0)74return;7576value = *data++;77memcpy(sd->gspca_dev.usb_buf, data, 6);7879ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0,80USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,81value, 0x06, sd->gspca_dev.usb_buf, 6, 500);82if (ret < 0) {83err("Write FSB registers failed (%d)", ret);84sd->gspca_dev.usb_err = ret;85}86}8788/*--------------------------------------------------------------------------89Write data to the serial bus control register.90Return 0 on success, a negative number otherwise.91--------------------------------------------------------------------------*/92static void w9968cf_write_sb(struct sd *sd, u16 value)93{94int ret;9596if (sd->gspca_dev.usb_err < 0)97return;9899/* We don't use reg_w here, as that would cause all writes when100bitbanging i2c to be logged, making the logs impossible to read */101ret = usb_control_msg(sd->gspca_dev.dev,102usb_sndctrlpipe(sd->gspca_dev.dev, 0),1030,104USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,105value, 0x01, NULL, 0, 500);106107udelay(W9968CF_I2C_BUS_DELAY);108109if (ret < 0) {110err("Write SB reg [01] %04x failed", value);111sd->gspca_dev.usb_err = ret;112}113}114115/*--------------------------------------------------------------------------116Read data from the serial bus control register.117Return 0 on success, a negative number otherwise.118--------------------------------------------------------------------------*/119static int w9968cf_read_sb(struct sd *sd)120{121int ret;122123if (sd->gspca_dev.usb_err < 0)124return -1;125126/* We don't use reg_r here, as the w9968cf is special and has 16127bit registers instead of 8 bit */128ret = usb_control_msg(sd->gspca_dev.dev,129usb_rcvctrlpipe(sd->gspca_dev.dev, 0),1301,131USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,1320, 0x01, sd->gspca_dev.usb_buf, 2, 500);133if (ret >= 0) {134ret = sd->gspca_dev.usb_buf[0] |135(sd->gspca_dev.usb_buf[1] << 8);136} else {137err("Read SB reg [01] failed");138sd->gspca_dev.usb_err = ret;139}140141udelay(W9968CF_I2C_BUS_DELAY);142143return ret;144}145146/*--------------------------------------------------------------------------147Upload quantization tables for the JPEG compression.148This function is called by w9968cf_start_transfer().149Return 0 on success, a negative number otherwise.150--------------------------------------------------------------------------*/151static void w9968cf_upload_quantizationtables(struct sd *sd)152{153u16 a, b;154int i, j;155156reg_w(sd, 0x39, 0x0010); /* JPEG clock enable */157158for (i = 0, j = 0; i < 32; i++, j += 2) {159a = Y_QUANTABLE[j] | ((unsigned)(Y_QUANTABLE[j + 1]) << 8);160b = UV_QUANTABLE[j] | ((unsigned)(UV_QUANTABLE[j + 1]) << 8);161reg_w(sd, 0x40 + i, a);162reg_w(sd, 0x60 + i, b);163}164reg_w(sd, 0x39, 0x0012); /* JPEG encoder enable */165}166167/****************************************************************************168* Low-level I2C I/O functions. *169* The adapter supports the following I2C transfer functions: *170* i2c_adap_fastwrite_byte_data() (at 400 kHz bit frequency only) *171* i2c_adap_read_byte_data() *172* i2c_adap_read_byte() *173****************************************************************************/174175static void w9968cf_smbus_start(struct sd *sd)176{177w9968cf_write_sb(sd, 0x0011); /* SDE=1, SDA=0, SCL=1 */178w9968cf_write_sb(sd, 0x0010); /* SDE=1, SDA=0, SCL=0 */179}180181static void w9968cf_smbus_stop(struct sd *sd)182{183w9968cf_write_sb(sd, 0x0010); /* SDE=1, SDA=0, SCL=0 */184w9968cf_write_sb(sd, 0x0011); /* SDE=1, SDA=0, SCL=1 */185w9968cf_write_sb(sd, 0x0013); /* SDE=1, SDA=1, SCL=1 */186}187188static void w9968cf_smbus_write_byte(struct sd *sd, u8 v)189{190u8 bit;191int sda;192193for (bit = 0 ; bit < 8 ; bit++) {194sda = (v & 0x80) ? 2 : 0;195v <<= 1;196/* SDE=1, SDA=sda, SCL=0 */197w9968cf_write_sb(sd, 0x10 | sda);198/* SDE=1, SDA=sda, SCL=1 */199w9968cf_write_sb(sd, 0x11 | sda);200/* SDE=1, SDA=sda, SCL=0 */201w9968cf_write_sb(sd, 0x10 | sda);202}203}204205static void w9968cf_smbus_read_byte(struct sd *sd, u8 *v)206{207u8 bit;208209/* No need to ensure SDA is high as we are always called after210read_ack which ends with SDA high */211*v = 0;212for (bit = 0 ; bit < 8 ; bit++) {213*v <<= 1;214/* SDE=1, SDA=1, SCL=1 */215w9968cf_write_sb(sd, 0x0013);216*v |= (w9968cf_read_sb(sd) & 0x0008) ? 1 : 0;217/* SDE=1, SDA=1, SCL=0 */218w9968cf_write_sb(sd, 0x0012);219}220}221222static void w9968cf_smbus_write_nack(struct sd *sd)223{224/* No need to ensure SDA is high as we are always called after225read_byte which ends with SDA high */226w9968cf_write_sb(sd, 0x0013); /* SDE=1, SDA=1, SCL=1 */227w9968cf_write_sb(sd, 0x0012); /* SDE=1, SDA=1, SCL=0 */228}229230static void w9968cf_smbus_read_ack(struct sd *sd)231{232int sda;233234/* Ensure SDA is high before raising clock to avoid a spurious stop */235w9968cf_write_sb(sd, 0x0012); /* SDE=1, SDA=1, SCL=0 */236w9968cf_write_sb(sd, 0x0013); /* SDE=1, SDA=1, SCL=1 */237sda = w9968cf_read_sb(sd);238w9968cf_write_sb(sd, 0x0012); /* SDE=1, SDA=1, SCL=0 */239if (sda >= 0 && (sda & 0x08)) {240PDEBUG(D_USBI, "Did not receive i2c ACK");241sd->gspca_dev.usb_err = -EIO;242}243}244245/* SMBus protocol: S Addr Wr [A] Subaddr [A] Value [A] P */246static void w9968cf_i2c_w(struct sd *sd, u8 reg, u8 value)247{248u16* data = (u16 *)sd->gspca_dev.usb_buf;249250data[0] = 0x082f | ((sd->sensor_addr & 0x80) ? 0x1500 : 0x0);251data[0] |= (sd->sensor_addr & 0x40) ? 0x4000 : 0x0;252data[1] = 0x2082 | ((sd->sensor_addr & 0x40) ? 0x0005 : 0x0);253data[1] |= (sd->sensor_addr & 0x20) ? 0x0150 : 0x0;254data[1] |= (sd->sensor_addr & 0x10) ? 0x5400 : 0x0;255data[2] = 0x8208 | ((sd->sensor_addr & 0x08) ? 0x0015 : 0x0);256data[2] |= (sd->sensor_addr & 0x04) ? 0x0540 : 0x0;257data[2] |= (sd->sensor_addr & 0x02) ? 0x5000 : 0x0;258data[3] = 0x1d20 | ((sd->sensor_addr & 0x02) ? 0x0001 : 0x0);259data[3] |= (sd->sensor_addr & 0x01) ? 0x0054 : 0x0;260261w9968cf_write_fsb(sd, data);262263data[0] = 0x8208 | ((reg & 0x80) ? 0x0015 : 0x0);264data[0] |= (reg & 0x40) ? 0x0540 : 0x0;265data[0] |= (reg & 0x20) ? 0x5000 : 0x0;266data[1] = 0x0820 | ((reg & 0x20) ? 0x0001 : 0x0);267data[1] |= (reg & 0x10) ? 0x0054 : 0x0;268data[1] |= (reg & 0x08) ? 0x1500 : 0x0;269data[1] |= (reg & 0x04) ? 0x4000 : 0x0;270data[2] = 0x2082 | ((reg & 0x04) ? 0x0005 : 0x0);271data[2] |= (reg & 0x02) ? 0x0150 : 0x0;272data[2] |= (reg & 0x01) ? 0x5400 : 0x0;273data[3] = 0x001d;274275w9968cf_write_fsb(sd, data);276277data[0] = 0x8208 | ((value & 0x80) ? 0x0015 : 0x0);278data[0] |= (value & 0x40) ? 0x0540 : 0x0;279data[0] |= (value & 0x20) ? 0x5000 : 0x0;280data[1] = 0x0820 | ((value & 0x20) ? 0x0001 : 0x0);281data[1] |= (value & 0x10) ? 0x0054 : 0x0;282data[1] |= (value & 0x08) ? 0x1500 : 0x0;283data[1] |= (value & 0x04) ? 0x4000 : 0x0;284data[2] = 0x2082 | ((value & 0x04) ? 0x0005 : 0x0);285data[2] |= (value & 0x02) ? 0x0150 : 0x0;286data[2] |= (value & 0x01) ? 0x5400 : 0x0;287data[3] = 0xfe1d;288289w9968cf_write_fsb(sd, data);290291PDEBUG(D_USBO, "i2c 0x%02x -> [0x%02x]", value, reg);292}293294/* SMBus protocol: S Addr Wr [A] Subaddr [A] P S Addr+1 Rd [A] [Value] NA P */295static int w9968cf_i2c_r(struct sd *sd, u8 reg)296{297int ret = 0;298u8 value;299300/* Fast serial bus data control disable */301w9968cf_write_sb(sd, 0x0013); /* don't change ! */302303w9968cf_smbus_start(sd);304w9968cf_smbus_write_byte(sd, sd->sensor_addr);305w9968cf_smbus_read_ack(sd);306w9968cf_smbus_write_byte(sd, reg);307w9968cf_smbus_read_ack(sd);308w9968cf_smbus_stop(sd);309w9968cf_smbus_start(sd);310w9968cf_smbus_write_byte(sd, sd->sensor_addr + 1);311w9968cf_smbus_read_ack(sd);312w9968cf_smbus_read_byte(sd, &value);313/* signal we don't want to read anymore, the v4l1 driver used to314send an ack here which is very wrong! (and then fixed315the issues this gave by retrying reads) */316w9968cf_smbus_write_nack(sd);317w9968cf_smbus_stop(sd);318319/* Fast serial bus data control re-enable */320w9968cf_write_sb(sd, 0x0030);321322if (sd->gspca_dev.usb_err >= 0) {323ret = value;324PDEBUG(D_USBI, "i2c [0x%02X] -> 0x%02X", reg, value);325} else326PDEBUG(D_ERR, "i2c read [0x%02x] failed", reg);327328return ret;329}330331/*--------------------------------------------------------------------------332Turn on the LED on some webcams. A beep should be heard too.333Return 0 on success, a negative number otherwise.334--------------------------------------------------------------------------*/335static void w9968cf_configure(struct sd *sd)336{337reg_w(sd, 0x00, 0xff00); /* power-down */338reg_w(sd, 0x00, 0xbf17); /* reset everything */339reg_w(sd, 0x00, 0xbf10); /* normal operation */340reg_w(sd, 0x01, 0x0010); /* serial bus, SDS high */341reg_w(sd, 0x01, 0x0000); /* serial bus, SDS low */342reg_w(sd, 0x01, 0x0010); /* ..high 'beep-beep' */343reg_w(sd, 0x01, 0x0030); /* Set sda scl to FSB mode */344345sd->stopped = 1;346}347348static void w9968cf_init(struct sd *sd)349{350unsigned long hw_bufsize = sd->sif ? (352 * 288 * 2) : (640 * 480 * 2),351y0 = 0x0000,352u0 = y0 + hw_bufsize / 2,353v0 = u0 + hw_bufsize / 4,354y1 = v0 + hw_bufsize / 4,355u1 = y1 + hw_bufsize / 2,356v1 = u1 + hw_bufsize / 4;357358reg_w(sd, 0x00, 0xff00); /* power off */359reg_w(sd, 0x00, 0xbf10); /* power on */360361reg_w(sd, 0x03, 0x405d); /* DRAM timings */362reg_w(sd, 0x04, 0x0030); /* SDRAM timings */363364reg_w(sd, 0x20, y0 & 0xffff); /* Y buf.0, low */365reg_w(sd, 0x21, y0 >> 16); /* Y buf.0, high */366reg_w(sd, 0x24, u0 & 0xffff); /* U buf.0, low */367reg_w(sd, 0x25, u0 >> 16); /* U buf.0, high */368reg_w(sd, 0x28, v0 & 0xffff); /* V buf.0, low */369reg_w(sd, 0x29, v0 >> 16); /* V buf.0, high */370371reg_w(sd, 0x22, y1 & 0xffff); /* Y buf.1, low */372reg_w(sd, 0x23, y1 >> 16); /* Y buf.1, high */373reg_w(sd, 0x26, u1 & 0xffff); /* U buf.1, low */374reg_w(sd, 0x27, u1 >> 16); /* U buf.1, high */375reg_w(sd, 0x2a, v1 & 0xffff); /* V buf.1, low */376reg_w(sd, 0x2b, v1 >> 16); /* V buf.1, high */377378reg_w(sd, 0x32, y1 & 0xffff); /* JPEG buf 0 low */379reg_w(sd, 0x33, y1 >> 16); /* JPEG buf 0 high */380381reg_w(sd, 0x34, y1 & 0xffff); /* JPEG buf 1 low */382reg_w(sd, 0x35, y1 >> 16); /* JPEG bug 1 high */383384reg_w(sd, 0x36, 0x0000);/* JPEG restart interval */385reg_w(sd, 0x37, 0x0804);/*JPEG VLE FIFO threshold*/386reg_w(sd, 0x38, 0x0000);/* disable hw up-scaling */387reg_w(sd, 0x3f, 0x0000); /* JPEG/MCTL test data */388}389390static void w9968cf_set_crop_window(struct sd *sd)391{392int start_cropx, start_cropy, x, y, fw, fh, cw, ch,393max_width, max_height;394395if (sd->sif) {396max_width = 352;397max_height = 288;398} else {399max_width = 640;400max_height = 480;401}402403if (sd->sensor == SEN_OV7620) {404/* Sigh, this is dependend on the clock / framerate changes405made by the frequency control, sick. */406if (sd->ctrls[FREQ].val == 1) {407start_cropx = 277;408start_cropy = 37;409} else {410start_cropx = 105;411start_cropy = 37;412}413} else {414start_cropx = 320;415start_cropy = 35;416}417418/* Work around to avoid FP arithmetics */419#define SC(x) ((x) << 10)420421/* Scaling factors */422fw = SC(sd->gspca_dev.width) / max_width;423fh = SC(sd->gspca_dev.height) / max_height;424425cw = (fw >= fh) ? max_width : SC(sd->gspca_dev.width) / fh;426ch = (fw >= fh) ? SC(sd->gspca_dev.height) / fw : max_height;427428sd->sensor_width = max_width;429sd->sensor_height = max_height;430431x = (max_width - cw) / 2;432y = (max_height - ch) / 2;433434reg_w(sd, 0x10, start_cropx + x);435reg_w(sd, 0x11, start_cropy + y);436reg_w(sd, 0x12, start_cropx + x + cw);437reg_w(sd, 0x13, start_cropy + y + ch);438}439440static void w9968cf_mode_init_regs(struct sd *sd)441{442int val, vs_polarity, hs_polarity;443444w9968cf_set_crop_window(sd);445446reg_w(sd, 0x14, sd->gspca_dev.width);447reg_w(sd, 0x15, sd->gspca_dev.height);448449/* JPEG width & height */450reg_w(sd, 0x30, sd->gspca_dev.width);451reg_w(sd, 0x31, sd->gspca_dev.height);452453/* Y & UV frame buffer strides (in WORD) */454if (w9968cf_vga_mode[sd->gspca_dev.curr_mode].pixelformat ==455V4L2_PIX_FMT_JPEG) {456reg_w(sd, 0x2c, sd->gspca_dev.width / 2);457reg_w(sd, 0x2d, sd->gspca_dev.width / 4);458} else459reg_w(sd, 0x2c, sd->gspca_dev.width);460461reg_w(sd, 0x00, 0xbf17); /* reset everything */462reg_w(sd, 0x00, 0xbf10); /* normal operation */463464/* Transfer size in WORDS (for UYVY format only) */465val = sd->gspca_dev.width * sd->gspca_dev.height;466reg_w(sd, 0x3d, val & 0xffff); /* low bits */467reg_w(sd, 0x3e, val >> 16); /* high bits */468469if (w9968cf_vga_mode[sd->gspca_dev.curr_mode].pixelformat ==470V4L2_PIX_FMT_JPEG) {471/* We may get called multiple times (usb isoc bw negotiat.) */472jpeg_define(sd->jpeg_hdr, sd->gspca_dev.height,473sd->gspca_dev.width, 0x22); /* JPEG 420 */474jpeg_set_qual(sd->jpeg_hdr, sd->quality);475w9968cf_upload_quantizationtables(sd);476}477478/* Video Capture Control Register */479if (sd->sensor == SEN_OV7620) {480/* Seems to work around a bug in the image sensor */481vs_polarity = 1;482hs_polarity = 1;483} else {484vs_polarity = 1;485hs_polarity = 0;486}487488val = (vs_polarity << 12) | (hs_polarity << 11);489490/* NOTE: We may not have enough memory to do double buffering while491doing compression (amount of memory differs per model cam).492So we use the second image buffer also as jpeg stream buffer493(see w9968cf_init), and disable double buffering. */494if (w9968cf_vga_mode[sd->gspca_dev.curr_mode].pixelformat ==495V4L2_PIX_FMT_JPEG) {496/* val |= 0x0002; YUV422P */497val |= 0x0003; /* YUV420P */498} else499val |= 0x0080; /* Enable HW double buffering */500501/* val |= 0x0020; enable clamping */502/* val |= 0x0008; enable (1-2-1) filter */503/* val |= 0x000c; enable (2-3-6-3-2) filter */504505val |= 0x8000; /* capt. enable */506507reg_w(sd, 0x16, val);508509sd->gspca_dev.empty_packet = 0;510}511512static void w9968cf_stop0(struct sd *sd)513{514reg_w(sd, 0x39, 0x0000); /* disable JPEG encoder */515reg_w(sd, 0x16, 0x0000); /* stop video capture */516}517518/* The w9968cf docs say that a 0 sized packet means EOF (and also SOF519for the next frame). This seems to simply not be true when operating520in JPEG mode, in this case there may be empty packets within the521frame. So in JPEG mode use the JPEG SOI marker to detect SOF.522523Note to make things even more interesting the w9968cf sends *PLANAR* jpeg,524to be precise it sends: SOI, SOF, DRI, SOS, Y-data, SOS, U-data, SOS,525V-data, EOI. */526static void w9968cf_pkt_scan(struct gspca_dev *gspca_dev,527u8 *data, /* isoc packet */528int len) /* iso packet length */529{530struct sd *sd = (struct sd *) gspca_dev;531532if (w9968cf_vga_mode[gspca_dev->curr_mode].pixelformat ==533V4L2_PIX_FMT_JPEG) {534if (len >= 2 &&535data[0] == 0xff &&536data[1] == 0xd8) {537gspca_frame_add(gspca_dev, LAST_PACKET,538NULL, 0);539gspca_frame_add(gspca_dev, FIRST_PACKET,540sd->jpeg_hdr, JPEG_HDR_SZ);541/* Strip the ff d8, our own header (which adds542huffman and quantization tables) already has this */543len -= 2;544data += 2;545}546} else {547/* In UYVY mode an empty packet signals EOF */548if (gspca_dev->empty_packet) {549gspca_frame_add(gspca_dev, LAST_PACKET,550NULL, 0);551gspca_frame_add(gspca_dev, FIRST_PACKET,552NULL, 0);553gspca_dev->empty_packet = 0;554}555}556gspca_frame_add(gspca_dev, INTER_PACKET, data, len);557}558559560