Path: blob/master/drivers/media/video/gspca/ov519.c
17653 views
/**1* OV519 driver2*3* Copyright (C) 2008-2011 Jean-François Moine <[email protected]>4* Copyright (C) 2009 Hans de Goede <[email protected]>5*6* This module is adapted from the ov51x-jpeg package, which itself7* was adapted from the ov511 driver.8*9* Original copyright for the ov511 driver is:10*11* Copyright (c) 1999-2006 Mark W. McClelland12* Support for OV519, OV8610 Copyright (c) 2003 Joerg Heckenbach13* Many improvements by Bret Wallach <[email protected]>14* Color fixes by by Orion Sky Lawlor <[email protected]> (2/26/2000)15* OV7620 fixes by Charl P. Botha <[email protected]>16* Changes by Claudio Matsuoka <[email protected]>17*18* ov51x-jpeg original copyright is:19*20* Copyright (c) 2004-2007 Romain Beauxis <[email protected]>21* Support for OV7670 sensors was contributed by Sam Skipsey <[email protected]>22*23* This program is free software; you can redistribute it and/or modify24* it under the terms of the GNU General Public License as published by25* the Free Software Foundation; either version 2 of the License, or26* any later version.27*28* This program is distributed in the hope that it will be useful,29* but WITHOUT ANY WARRANTY; without even the implied warranty of30* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the31* GNU General Public License for more details.32*33* You should have received a copy of the GNU General Public License34* along with this program; if not, write to the Free Software35* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA36*37*/38#define MODULE_NAME "ov519"3940#include <linux/input.h>41#include "gspca.h"4243/* The jpeg_hdr is used by w996Xcf only */44/* The CONEX_CAM define for jpeg.h needs renaming, now its used here too */45#define CONEX_CAM46#include "jpeg.h"4748MODULE_AUTHOR("Jean-Francois Moine <http://moinejf.free.fr>");49MODULE_DESCRIPTION("OV519 USB Camera Driver");50MODULE_LICENSE("GPL");5152/* global parameters */53static int frame_rate;5455/* Number of times to retry a failed I2C transaction. Increase this if you56* are getting "Failed to read sensor ID..." */57static int i2c_detect_tries = 10;5859/* controls */60enum e_ctrl {61BRIGHTNESS,62CONTRAST,63EXPOSURE,64COLORS,65HFLIP,66VFLIP,67AUTOBRIGHT,68AUTOGAIN,69FREQ,70NCTRL /* number of controls */71};7273/* ov519 device descriptor */74struct sd {75struct gspca_dev gspca_dev; /* !! must be the first item */7677struct gspca_ctrl ctrls[NCTRL];7879u8 packet_nr;8081char bridge;82#define BRIDGE_OV511 083#define BRIDGE_OV511PLUS 184#define BRIDGE_OV518 285#define BRIDGE_OV518PLUS 386#define BRIDGE_OV519 4 /* = ov530 */87#define BRIDGE_OVFX2 588#define BRIDGE_W9968CF 689#define BRIDGE_MASK 79091char invert_led;92#define BRIDGE_INVERT_LED 89394char snapshot_pressed;95char snapshot_needs_reset;9697/* Determined by sensor type */98u8 sif;99100u8 quality;101#define QUALITY_MIN 50102#define QUALITY_MAX 70103#define QUALITY_DEF 50104105u8 stopped; /* Streaming is temporarily paused */106u8 first_frame;107108u8 frame_rate; /* current Framerate */109u8 clockdiv; /* clockdiv override */110111s8 sensor; /* Type of image sensor chip (SEN_*) */112113u8 sensor_addr;114u16 sensor_width;115u16 sensor_height;116s16 sensor_reg_cache[256];117118u8 jpeg_hdr[JPEG_HDR_SZ];119};120enum sensors {121SEN_OV2610,122SEN_OV2610AE,123SEN_OV3610,124SEN_OV6620,125SEN_OV6630,126SEN_OV66308AF,127SEN_OV7610,128SEN_OV7620,129SEN_OV7620AE,130SEN_OV7640,131SEN_OV7648,132SEN_OV7660,133SEN_OV7670,134SEN_OV76BE,135SEN_OV8610,136};137138/* Note this is a bit of a hack, but the w9968cf driver needs the code for all139the ov sensors which is already present here. When we have the time we140really should move the sensor drivers to v4l2 sub drivers. */141#include "w996Xcf.c"142143/* V4L2 controls supported by the driver */144static void setbrightness(struct gspca_dev *gspca_dev);145static void setcontrast(struct gspca_dev *gspca_dev);146static void setexposure(struct gspca_dev *gspca_dev);147static void setcolors(struct gspca_dev *gspca_dev);148static void sethvflip(struct gspca_dev *gspca_dev);149static void setautobright(struct gspca_dev *gspca_dev);150static int sd_setautogain(struct gspca_dev *gspca_dev, __s32 val);151static void setfreq(struct gspca_dev *gspca_dev);152static void setfreq_i(struct sd *sd);153154static const struct ctrl sd_ctrls[] = {155[BRIGHTNESS] = {156{157.id = V4L2_CID_BRIGHTNESS,158.type = V4L2_CTRL_TYPE_INTEGER,159.name = "Brightness",160.minimum = 0,161.maximum = 255,162.step = 1,163.default_value = 127,164},165.set_control = setbrightness,166},167[CONTRAST] = {168{169.id = V4L2_CID_CONTRAST,170.type = V4L2_CTRL_TYPE_INTEGER,171.name = "Contrast",172.minimum = 0,173.maximum = 255,174.step = 1,175.default_value = 127,176},177.set_control = setcontrast,178},179[EXPOSURE] = {180{181.id = V4L2_CID_EXPOSURE,182.type = V4L2_CTRL_TYPE_INTEGER,183.name = "Exposure",184.minimum = 0,185.maximum = 255,186.step = 1,187.default_value = 127,188},189.set_control = setexposure,190},191[COLORS] = {192{193.id = V4L2_CID_SATURATION,194.type = V4L2_CTRL_TYPE_INTEGER,195.name = "Color",196.minimum = 0,197.maximum = 255,198.step = 1,199.default_value = 127,200},201.set_control = setcolors,202},203/* The flip controls work for sensors ov7660 and ov7670 only */204[HFLIP] = {205{206.id = V4L2_CID_HFLIP,207.type = V4L2_CTRL_TYPE_BOOLEAN,208.name = "Mirror",209.minimum = 0,210.maximum = 1,211.step = 1,212.default_value = 0,213},214.set_control = sethvflip,215},216[VFLIP] = {217{218.id = V4L2_CID_VFLIP,219.type = V4L2_CTRL_TYPE_BOOLEAN,220.name = "Vflip",221.minimum = 0,222.maximum = 1,223.step = 1,224.default_value = 0,225},226.set_control = sethvflip,227},228[AUTOBRIGHT] = {229{230.id = V4L2_CID_AUTOBRIGHTNESS,231.type = V4L2_CTRL_TYPE_BOOLEAN,232.name = "Auto Brightness",233.minimum = 0,234.maximum = 1,235.step = 1,236.default_value = 1,237},238.set_control = setautobright,239},240[AUTOGAIN] = {241{242.id = V4L2_CID_AUTOGAIN,243.type = V4L2_CTRL_TYPE_BOOLEAN,244.name = "Auto Gain",245.minimum = 0,246.maximum = 1,247.step = 1,248.default_value = 1,249.flags = V4L2_CTRL_FLAG_UPDATE250},251.set = sd_setautogain,252},253[FREQ] = {254{255.id = V4L2_CID_POWER_LINE_FREQUENCY,256.type = V4L2_CTRL_TYPE_MENU,257.name = "Light frequency filter",258.minimum = 0,259.maximum = 2, /* 0: no flicker, 1: 50Hz, 2:60Hz, 3: auto */260.step = 1,261.default_value = 0,262},263.set_control = setfreq,264},265};266267/* table of the disabled controls */268static const unsigned ctrl_dis[] = {269[SEN_OV2610] = ((1 << NCTRL) - 1) /* no control */270^ ((1 << EXPOSURE) /* but exposure */271| (1 << AUTOGAIN)), /* and autogain */272273[SEN_OV2610AE] = ((1 << NCTRL) - 1) /* no control */274^ ((1 << EXPOSURE) /* but exposure */275| (1 << AUTOGAIN)), /* and autogain */276277[SEN_OV3610] = (1 << NCTRL) - 1, /* no control */278279[SEN_OV6620] = (1 << HFLIP) |280(1 << VFLIP) |281(1 << EXPOSURE) |282(1 << AUTOGAIN),283284[SEN_OV6630] = (1 << HFLIP) |285(1 << VFLIP) |286(1 << EXPOSURE) |287(1 << AUTOGAIN),288289[SEN_OV66308AF] = (1 << HFLIP) |290(1 << VFLIP) |291(1 << EXPOSURE) |292(1 << AUTOGAIN),293294[SEN_OV7610] = (1 << HFLIP) |295(1 << VFLIP) |296(1 << EXPOSURE) |297(1 << AUTOGAIN),298299[SEN_OV7620] = (1 << HFLIP) |300(1 << VFLIP) |301(1 << EXPOSURE) |302(1 << AUTOGAIN),303304[SEN_OV7620AE] = (1 << HFLIP) |305(1 << VFLIP) |306(1 << EXPOSURE) |307(1 << AUTOGAIN),308309[SEN_OV7640] = (1 << HFLIP) |310(1 << VFLIP) |311(1 << AUTOBRIGHT) |312(1 << CONTRAST) |313(1 << EXPOSURE) |314(1 << AUTOGAIN),315316[SEN_OV7648] = (1 << HFLIP) |317(1 << VFLIP) |318(1 << AUTOBRIGHT) |319(1 << CONTRAST) |320(1 << EXPOSURE) |321(1 << AUTOGAIN),322323[SEN_OV7660] = (1 << AUTOBRIGHT) |324(1 << EXPOSURE) |325(1 << AUTOGAIN),326327[SEN_OV7670] = (1 << COLORS) |328(1 << AUTOBRIGHT) |329(1 << EXPOSURE) |330(1 << AUTOGAIN),331332[SEN_OV76BE] = (1 << HFLIP) |333(1 << VFLIP) |334(1 << EXPOSURE) |335(1 << AUTOGAIN),336337[SEN_OV8610] = (1 << HFLIP) |338(1 << VFLIP) |339(1 << EXPOSURE) |340(1 << AUTOGAIN) |341(1 << FREQ),342};343344static const struct v4l2_pix_format ov519_vga_mode[] = {345{320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,346.bytesperline = 320,347.sizeimage = 320 * 240 * 3 / 8 + 590,348.colorspace = V4L2_COLORSPACE_JPEG,349.priv = 1},350{640, 480, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,351.bytesperline = 640,352.sizeimage = 640 * 480 * 3 / 8 + 590,353.colorspace = V4L2_COLORSPACE_JPEG,354.priv = 0},355};356static const struct v4l2_pix_format ov519_sif_mode[] = {357{160, 120, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,358.bytesperline = 160,359.sizeimage = 160 * 120 * 3 / 8 + 590,360.colorspace = V4L2_COLORSPACE_JPEG,361.priv = 3},362{176, 144, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,363.bytesperline = 176,364.sizeimage = 176 * 144 * 3 / 8 + 590,365.colorspace = V4L2_COLORSPACE_JPEG,366.priv = 1},367{320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,368.bytesperline = 320,369.sizeimage = 320 * 240 * 3 / 8 + 590,370.colorspace = V4L2_COLORSPACE_JPEG,371.priv = 2},372{352, 288, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,373.bytesperline = 352,374.sizeimage = 352 * 288 * 3 / 8 + 590,375.colorspace = V4L2_COLORSPACE_JPEG,376.priv = 0},377};378379/* Note some of the sizeimage values for the ov511 / ov518 may seem380larger then necessary, however they need to be this big as the ov511 /381ov518 always fills the entire isoc frame, using 0 padding bytes when382it doesn't have any data. So with low framerates the amount of data383transferred can become quite large (libv4l will remove all the 0 padding384in userspace). */385static const struct v4l2_pix_format ov518_vga_mode[] = {386{320, 240, V4L2_PIX_FMT_OV518, V4L2_FIELD_NONE,387.bytesperline = 320,388.sizeimage = 320 * 240 * 3,389.colorspace = V4L2_COLORSPACE_JPEG,390.priv = 1},391{640, 480, V4L2_PIX_FMT_OV518, V4L2_FIELD_NONE,392.bytesperline = 640,393.sizeimage = 640 * 480 * 2,394.colorspace = V4L2_COLORSPACE_JPEG,395.priv = 0},396};397static const struct v4l2_pix_format ov518_sif_mode[] = {398{160, 120, V4L2_PIX_FMT_OV518, V4L2_FIELD_NONE,399.bytesperline = 160,400.sizeimage = 70000,401.colorspace = V4L2_COLORSPACE_JPEG,402.priv = 3},403{176, 144, V4L2_PIX_FMT_OV518, V4L2_FIELD_NONE,404.bytesperline = 176,405.sizeimage = 70000,406.colorspace = V4L2_COLORSPACE_JPEG,407.priv = 1},408{320, 240, V4L2_PIX_FMT_OV518, V4L2_FIELD_NONE,409.bytesperline = 320,410.sizeimage = 320 * 240 * 3,411.colorspace = V4L2_COLORSPACE_JPEG,412.priv = 2},413{352, 288, V4L2_PIX_FMT_OV518, V4L2_FIELD_NONE,414.bytesperline = 352,415.sizeimage = 352 * 288 * 3,416.colorspace = V4L2_COLORSPACE_JPEG,417.priv = 0},418};419420static const struct v4l2_pix_format ov511_vga_mode[] = {421{320, 240, V4L2_PIX_FMT_OV511, V4L2_FIELD_NONE,422.bytesperline = 320,423.sizeimage = 320 * 240 * 3,424.colorspace = V4L2_COLORSPACE_JPEG,425.priv = 1},426{640, 480, V4L2_PIX_FMT_OV511, V4L2_FIELD_NONE,427.bytesperline = 640,428.sizeimage = 640 * 480 * 2,429.colorspace = V4L2_COLORSPACE_JPEG,430.priv = 0},431};432static const struct v4l2_pix_format ov511_sif_mode[] = {433{160, 120, V4L2_PIX_FMT_OV511, V4L2_FIELD_NONE,434.bytesperline = 160,435.sizeimage = 70000,436.colorspace = V4L2_COLORSPACE_JPEG,437.priv = 3},438{176, 144, V4L2_PIX_FMT_OV511, V4L2_FIELD_NONE,439.bytesperline = 176,440.sizeimage = 70000,441.colorspace = V4L2_COLORSPACE_JPEG,442.priv = 1},443{320, 240, V4L2_PIX_FMT_OV511, V4L2_FIELD_NONE,444.bytesperline = 320,445.sizeimage = 320 * 240 * 3,446.colorspace = V4L2_COLORSPACE_JPEG,447.priv = 2},448{352, 288, V4L2_PIX_FMT_OV511, V4L2_FIELD_NONE,449.bytesperline = 352,450.sizeimage = 352 * 288 * 3,451.colorspace = V4L2_COLORSPACE_JPEG,452.priv = 0},453};454455static const struct v4l2_pix_format ovfx2_vga_mode[] = {456{320, 240, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,457.bytesperline = 320,458.sizeimage = 320 * 240,459.colorspace = V4L2_COLORSPACE_SRGB,460.priv = 1},461{640, 480, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,462.bytesperline = 640,463.sizeimage = 640 * 480,464.colorspace = V4L2_COLORSPACE_SRGB,465.priv = 0},466};467static const struct v4l2_pix_format ovfx2_cif_mode[] = {468{160, 120, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,469.bytesperline = 160,470.sizeimage = 160 * 120,471.colorspace = V4L2_COLORSPACE_SRGB,472.priv = 3},473{176, 144, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,474.bytesperline = 176,475.sizeimage = 176 * 144,476.colorspace = V4L2_COLORSPACE_SRGB,477.priv = 1},478{320, 240, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,479.bytesperline = 320,480.sizeimage = 320 * 240,481.colorspace = V4L2_COLORSPACE_SRGB,482.priv = 2},483{352, 288, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,484.bytesperline = 352,485.sizeimage = 352 * 288,486.colorspace = V4L2_COLORSPACE_SRGB,487.priv = 0},488};489static const struct v4l2_pix_format ovfx2_ov2610_mode[] = {490{800, 600, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,491.bytesperline = 800,492.sizeimage = 800 * 600,493.colorspace = V4L2_COLORSPACE_SRGB,494.priv = 1},495{1600, 1200, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,496.bytesperline = 1600,497.sizeimage = 1600 * 1200,498.colorspace = V4L2_COLORSPACE_SRGB},499};500static const struct v4l2_pix_format ovfx2_ov3610_mode[] = {501{640, 480, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,502.bytesperline = 640,503.sizeimage = 640 * 480,504.colorspace = V4L2_COLORSPACE_SRGB,505.priv = 1},506{800, 600, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,507.bytesperline = 800,508.sizeimage = 800 * 600,509.colorspace = V4L2_COLORSPACE_SRGB,510.priv = 1},511{1024, 768, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,512.bytesperline = 1024,513.sizeimage = 1024 * 768,514.colorspace = V4L2_COLORSPACE_SRGB,515.priv = 1},516{1600, 1200, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,517.bytesperline = 1600,518.sizeimage = 1600 * 1200,519.colorspace = V4L2_COLORSPACE_SRGB,520.priv = 0},521{2048, 1536, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,522.bytesperline = 2048,523.sizeimage = 2048 * 1536,524.colorspace = V4L2_COLORSPACE_SRGB,525.priv = 0},526};527528/* Registers common to OV511 / OV518 */529#define R51x_FIFO_PSIZE 0x30 /* 2 bytes wide w/ OV518(+) */530#define R51x_SYS_RESET 0x50531/* Reset type flags */532#define OV511_RESET_OMNICE 0x08533#define R51x_SYS_INIT 0x53534#define R51x_SYS_SNAP 0x52535#define R51x_SYS_CUST_ID 0x5f536#define R51x_COMP_LUT_BEGIN 0x80537538/* OV511 Camera interface register numbers */539#define R511_CAM_DELAY 0x10540#define R511_CAM_EDGE 0x11541#define R511_CAM_PXCNT 0x12542#define R511_CAM_LNCNT 0x13543#define R511_CAM_PXDIV 0x14544#define R511_CAM_LNDIV 0x15545#define R511_CAM_UV_EN 0x16546#define R511_CAM_LINE_MODE 0x17547#define R511_CAM_OPTS 0x18548549#define R511_SNAP_FRAME 0x19550#define R511_SNAP_PXCNT 0x1a551#define R511_SNAP_LNCNT 0x1b552#define R511_SNAP_PXDIV 0x1c553#define R511_SNAP_LNDIV 0x1d554#define R511_SNAP_UV_EN 0x1e555#define R511_SNAP_OPTS 0x1f556557#define R511_DRAM_FLOW_CTL 0x20558#define R511_FIFO_OPTS 0x31559#define R511_I2C_CTL 0x40560#define R511_SYS_LED_CTL 0x55 /* OV511+ only */561#define R511_COMP_EN 0x78562#define R511_COMP_LUT_EN 0x79563564/* OV518 Camera interface register numbers */565#define R518_GPIO_OUT 0x56 /* OV518(+) only */566#define R518_GPIO_CTL 0x57 /* OV518(+) only */567568/* OV519 Camera interface register numbers */569#define OV519_R10_H_SIZE 0x10570#define OV519_R11_V_SIZE 0x11571#define OV519_R12_X_OFFSETL 0x12572#define OV519_R13_X_OFFSETH 0x13573#define OV519_R14_Y_OFFSETL 0x14574#define OV519_R15_Y_OFFSETH 0x15575#define OV519_R16_DIVIDER 0x16576#define OV519_R20_DFR 0x20577#define OV519_R25_FORMAT 0x25578579/* OV519 System Controller register numbers */580#define OV519_R51_RESET1 0x51581#define OV519_R54_EN_CLK1 0x54582#define OV519_R57_SNAPSHOT 0x57583584#define OV519_GPIO_DATA_OUT0 0x71585#define OV519_GPIO_IO_CTRL0 0x72586587/*#define OV511_ENDPOINT_ADDRESS 1 * Isoc endpoint number */588589/*590* The FX2 chip does not give us a zero length read at end of frame.591* It does, however, give a short read at the end of a frame, if592* necessary, rather than run two frames together.593*594* By choosing the right bulk transfer size, we are guaranteed to always595* get a short read for the last read of each frame. Frame sizes are596* always a composite number (width * height, or a multiple) so if we597* choose a prime number, we are guaranteed that the last read of a598* frame will be short.599*600* But it isn't that easy: the 2.6 kernel requires a multiple of 4KB,601* otherwise EOVERFLOW "babbling" errors occur. I have not been able602* to figure out why. [PMiller]603*604* The constant (13 * 4096) is the largest "prime enough" number less than 64KB.605*606* It isn't enough to know the number of bytes per frame, in case we607* have data dropouts or buffer overruns (even though the FX2 double608* buffers, there are some pretty strict real time constraints for609* isochronous transfer for larger frame sizes).610*/611/*jfm: this value does not work for 800x600 - see isoc_init */612#define OVFX2_BULK_SIZE (13 * 4096)613614/* I2C registers */615#define R51x_I2C_W_SID 0x41616#define R51x_I2C_SADDR_3 0x42617#define R51x_I2C_SADDR_2 0x43618#define R51x_I2C_R_SID 0x44619#define R51x_I2C_DATA 0x45620#define R518_I2C_CTL 0x47 /* OV518(+) only */621#define OVFX2_I2C_ADDR 0x00622623/* I2C ADDRESSES */624#define OV7xx0_SID 0x42625#define OV_HIRES_SID 0x60 /* OV9xxx / OV2xxx / OV3xxx */626#define OV8xx0_SID 0xa0627#define OV6xx0_SID 0xc0628629/* OV7610 registers */630#define OV7610_REG_GAIN 0x00 /* gain setting (5:0) */631#define OV7610_REG_BLUE 0x01 /* blue channel balance */632#define OV7610_REG_RED 0x02 /* red channel balance */633#define OV7610_REG_SAT 0x03 /* saturation */634#define OV8610_REG_HUE 0x04 /* 04 reserved */635#define OV7610_REG_CNT 0x05 /* Y contrast */636#define OV7610_REG_BRT 0x06 /* Y brightness */637#define OV7610_REG_COM_C 0x14 /* misc common regs */638#define OV7610_REG_ID_HIGH 0x1c /* manufacturer ID MSB */639#define OV7610_REG_ID_LOW 0x1d /* manufacturer ID LSB */640#define OV7610_REG_COM_I 0x29 /* misc settings */641642/* OV7660 and OV7670 registers */643#define OV7670_R00_GAIN 0x00 /* Gain lower 8 bits (rest in vref) */644#define OV7670_R01_BLUE 0x01 /* blue gain */645#define OV7670_R02_RED 0x02 /* red gain */646#define OV7670_R03_VREF 0x03 /* Pieces of GAIN, VSTART, VSTOP */647#define OV7670_R04_COM1 0x04 /* Control 1 */648/*#define OV7670_R07_AECHH 0x07 * AEC MS 5 bits */649#define OV7670_R0C_COM3 0x0c /* Control 3 */650#define OV7670_R0D_COM4 0x0d /* Control 4 */651#define OV7670_R0E_COM5 0x0e /* All "reserved" */652#define OV7670_R0F_COM6 0x0f /* Control 6 */653#define OV7670_R10_AECH 0x10 /* More bits of AEC value */654#define OV7670_R11_CLKRC 0x11 /* Clock control */655#define OV7670_R12_COM7 0x12 /* Control 7 */656#define OV7670_COM7_FMT_VGA 0x00657/*#define OV7670_COM7_YUV 0x00 * YUV */658#define OV7670_COM7_FMT_QVGA 0x10 /* QVGA format */659#define OV7670_COM7_FMT_MASK 0x38660#define OV7670_COM7_RESET 0x80 /* Register reset */661#define OV7670_R13_COM8 0x13 /* Control 8 */662#define OV7670_COM8_AEC 0x01 /* Auto exposure enable */663#define OV7670_COM8_AWB 0x02 /* White balance enable */664#define OV7670_COM8_AGC 0x04 /* Auto gain enable */665#define OV7670_COM8_BFILT 0x20 /* Band filter enable */666#define OV7670_COM8_AECSTEP 0x40 /* Unlimited AEC step size */667#define OV7670_COM8_FASTAEC 0x80 /* Enable fast AGC/AEC */668#define OV7670_R14_COM9 0x14 /* Control 9 - gain ceiling */669#define OV7670_R15_COM10 0x15 /* Control 10 */670#define OV7670_R17_HSTART 0x17 /* Horiz start high bits */671#define OV7670_R18_HSTOP 0x18 /* Horiz stop high bits */672#define OV7670_R19_VSTART 0x19 /* Vert start high bits */673#define OV7670_R1A_VSTOP 0x1a /* Vert stop high bits */674#define OV7670_R1E_MVFP 0x1e /* Mirror / vflip */675#define OV7670_MVFP_VFLIP 0x10 /* vertical flip */676#define OV7670_MVFP_MIRROR 0x20 /* Mirror image */677#define OV7670_R24_AEW 0x24 /* AGC upper limit */678#define OV7670_R25_AEB 0x25 /* AGC lower limit */679#define OV7670_R26_VPT 0x26 /* AGC/AEC fast mode op region */680#define OV7670_R32_HREF 0x32 /* HREF pieces */681#define OV7670_R3A_TSLB 0x3a /* lots of stuff */682#define OV7670_R3B_COM11 0x3b /* Control 11 */683#define OV7670_COM11_EXP 0x02684#define OV7670_COM11_HZAUTO 0x10 /* Auto detect 50/60 Hz */685#define OV7670_R3C_COM12 0x3c /* Control 12 */686#define OV7670_R3D_COM13 0x3d /* Control 13 */687#define OV7670_COM13_GAMMA 0x80 /* Gamma enable */688#define OV7670_COM13_UVSAT 0x40 /* UV saturation auto adjustment */689#define OV7670_R3E_COM14 0x3e /* Control 14 */690#define OV7670_R3F_EDGE 0x3f /* Edge enhancement factor */691#define OV7670_R40_COM15 0x40 /* Control 15 */692/*#define OV7670_COM15_R00FF 0xc0 * 00 to FF */693#define OV7670_R41_COM16 0x41 /* Control 16 */694#define OV7670_COM16_AWBGAIN 0x08 /* AWB gain enable */695/* end of ov7660 common registers */696#define OV7670_R55_BRIGHT 0x55 /* Brightness */697#define OV7670_R56_CONTRAS 0x56 /* Contrast control */698#define OV7670_R69_GFIX 0x69 /* Fix gain control */699/*#define OV7670_R8C_RGB444 0x8c * RGB 444 control */700#define OV7670_R9F_HAECC1 0x9f /* Hist AEC/AGC control 1 */701#define OV7670_RA0_HAECC2 0xa0 /* Hist AEC/AGC control 2 */702#define OV7670_RA5_BD50MAX 0xa5 /* 50hz banding step limit */703#define OV7670_RA6_HAECC3 0xa6 /* Hist AEC/AGC control 3 */704#define OV7670_RA7_HAECC4 0xa7 /* Hist AEC/AGC control 4 */705#define OV7670_RA8_HAECC5 0xa8 /* Hist AEC/AGC control 5 */706#define OV7670_RA9_HAECC6 0xa9 /* Hist AEC/AGC control 6 */707#define OV7670_RAA_HAECC7 0xaa /* Hist AEC/AGC control 7 */708#define OV7670_RAB_BD60MAX 0xab /* 60hz banding step limit */709710struct ov_regvals {711u8 reg;712u8 val;713};714struct ov_i2c_regvals {715u8 reg;716u8 val;717};718719/* Settings for OV2610 camera chip */720static const struct ov_i2c_regvals norm_2610[] = {721{ 0x12, 0x80 }, /* reset */722};723724static const struct ov_i2c_regvals norm_2610ae[] = {725{0x12, 0x80}, /* reset */726{0x13, 0xcd},727{0x09, 0x01},728{0x0d, 0x00},729{0x11, 0x80},730{0x12, 0x20}, /* 1600x1200 */731{0x33, 0x0c},732{0x35, 0x90},733{0x36, 0x37},734/* ms-win traces */735{0x11, 0x83}, /* clock / 3 ? */736{0x2d, 0x00}, /* 60 Hz filter */737{0x24, 0xb0}, /* normal colors */738{0x25, 0x90},739{0x10, 0x43},740};741742static const struct ov_i2c_regvals norm_3620b[] = {743/*744* From the datasheet: "Note that after writing to register COMH745* (0x12) to change the sensor mode, registers related to the746* sensor’s cropping window will be reset back to their default747* values."748*749* "wait 4096 external clock ... to make sure the sensor is750* stable and ready to access registers" i.e. 160us at 24MHz751*/752{ 0x12, 0x80 }, /* COMH reset */753{ 0x12, 0x00 }, /* QXGA, master */754755/*756* 11 CLKRC "Clock Rate Control"757* [7] internal frequency doublers: on758* [6] video port mode: master759* [5:0] clock divider: 1760*/761{ 0x11, 0x80 },762763/*764* 13 COMI "Common Control I"765* = 192 (0xC0) 11000000766* COMI[7] "AEC speed selection"767* = 1 (0x01) 1....... "Faster AEC correction"768* COMI[6] "AEC speed step selection"769* = 1 (0x01) .1...... "Big steps, fast"770* COMI[5] "Banding filter on off"771* = 0 (0x00) ..0..... "Off"772* COMI[4] "Banding filter option"773* = 0 (0x00) ...0.... "Main clock is 48 MHz and774* the PLL is ON"775* COMI[3] "Reserved"776* = 0 (0x00) ....0...777* COMI[2] "AGC auto manual control selection"778* = 0 (0x00) .....0.. "Manual"779* COMI[1] "AWB auto manual control selection"780* = 0 (0x00) ......0. "Manual"781* COMI[0] "Exposure control"782* = 0 (0x00) .......0 "Manual"783*/784{ 0x13, 0xc0 },785786/*787* 09 COMC "Common Control C"788* = 8 (0x08) 00001000789* COMC[7:5] "Reserved"790* = 0 (0x00) 000.....791* COMC[4] "Sleep Mode Enable"792* = 0 (0x00) ...0.... "Normal mode"793* COMC[3:2] "Sensor sampling reset timing selection"794* = 2 (0x02) ....10.. "Longer reset time"795* COMC[1:0] "Output drive current select"796* = 0 (0x00) ......00 "Weakest"797*/798{ 0x09, 0x08 },799800/*801* 0C COMD "Common Control D"802* = 8 (0x08) 00001000803* COMD[7] "Reserved"804* = 0 (0x00) 0.......805* COMD[6] "Swap MSB and LSB at the output port"806* = 0 (0x00) .0...... "False"807* COMD[5:3] "Reserved"808* = 1 (0x01) ..001...809* COMD[2] "Output Average On Off"810* = 0 (0x00) .....0.. "Output Normal"811* COMD[1] "Sensor precharge voltage selection"812* = 0 (0x00) ......0. "Selects internal813* reference precharge814* voltage"815* COMD[0] "Snapshot option"816* = 0 (0x00) .......0 "Enable live video output817* after snapshot sequence"818*/819{ 0x0c, 0x08 },820821/*822* 0D COME "Common Control E"823* = 161 (0xA1) 10100001824* COME[7] "Output average option"825* = 1 (0x01) 1....... "Output average of 4 pixels"826* COME[6] "Anti-blooming control"827* = 0 (0x00) .0...... "Off"828* COME[5:3] "Reserved"829* = 4 (0x04) ..100...830* COME[2] "Clock output power down pin status"831* = 0 (0x00) .....0.. "Tri-state data output pin832* on power down"833* COME[1] "Data output pin status selection at power down"834* = 0 (0x00) ......0. "Tri-state VSYNC, PCLK,835* HREF, and CHSYNC pins on836* power down"837* COME[0] "Auto zero circuit select"838* = 1 (0x01) .......1 "On"839*/840{ 0x0d, 0xa1 },841842/*843* 0E COMF "Common Control F"844* = 112 (0x70) 01110000845* COMF[7] "System clock selection"846* = 0 (0x00) 0....... "Use 24 MHz system clock"847* COMF[6:4] "Reserved"848* = 7 (0x07) .111....849* COMF[3] "Manual auto negative offset canceling selection"850* = 0 (0x00) ....0... "Auto detect negative851* offset and cancel it"852* COMF[2:0] "Reserved"853* = 0 (0x00) .....000854*/855{ 0x0e, 0x70 },856857/*858* 0F COMG "Common Control G"859* = 66 (0x42) 01000010860* COMG[7] "Optical black output selection"861* = 0 (0x00) 0....... "Disable"862* COMG[6] "Black level calibrate selection"863* = 1 (0x01) .1...... "Use optical black pixels864* to calibrate"865* COMG[5:4] "Reserved"866* = 0 (0x00) ..00....867* COMG[3] "Channel offset adjustment"868* = 0 (0x00) ....0... "Disable offset adjustment"869* COMG[2] "ADC black level calibration option"870* = 0 (0x00) .....0.. "Use B/G line and G/R871* line to calibrate each872* channel's black level"873* COMG[1] "Reserved"874* = 1 (0x01) ......1.875* COMG[0] "ADC black level calibration enable"876* = 0 (0x00) .......0 "Disable"877*/878{ 0x0f, 0x42 },879880/*881* 14 COMJ "Common Control J"882* = 198 (0xC6) 11000110883* COMJ[7:6] "AGC gain ceiling"884* = 3 (0x03) 11...... "8x"885* COMJ[5:4] "Reserved"886* = 0 (0x00) ..00....887* COMJ[3] "Auto banding filter"888* = 0 (0x00) ....0... "Banding filter is always889* on off depending on890* COMI[5] setting"891* COMJ[2] "VSYNC drop option"892* = 1 (0x01) .....1.. "SYNC is dropped if frame893* data is dropped"894* COMJ[1] "Frame data drop"895* = 1 (0x01) ......1. "Drop frame data if896* exposure is not within897* tolerance. In AEC mode,898* data is normally dropped899* when data is out of900* range."901* COMJ[0] "Reserved"902* = 0 (0x00) .......0903*/904{ 0x14, 0xc6 },905906/*907* 15 COMK "Common Control K"908* = 2 (0x02) 00000010909* COMK[7] "CHSYNC pin output swap"910* = 0 (0x00) 0....... "CHSYNC"911* COMK[6] "HREF pin output swap"912* = 0 (0x00) .0...... "HREF"913* COMK[5] "PCLK output selection"914* = 0 (0x00) ..0..... "PCLK always output"915* COMK[4] "PCLK edge selection"916* = 0 (0x00) ...0.... "Data valid on falling edge"917* COMK[3] "HREF output polarity"918* = 0 (0x00) ....0... "positive"919* COMK[2] "Reserved"920* = 0 (0x00) .....0..921* COMK[1] "VSYNC polarity"922* = 1 (0x01) ......1. "negative"923* COMK[0] "HSYNC polarity"924* = 0 (0x00) .......0 "positive"925*/926{ 0x15, 0x02 },927928/*929* 33 CHLF "Current Control"930* = 9 (0x09) 00001001931* CHLF[7:6] "Sensor current control"932* = 0 (0x00) 00......933* CHLF[5] "Sensor current range control"934* = 0 (0x00) ..0..... "normal range"935* CHLF[4] "Sensor current"936* = 0 (0x00) ...0.... "normal current"937* CHLF[3] "Sensor buffer current control"938* = 1 (0x01) ....1... "half current"939* CHLF[2] "Column buffer current control"940* = 0 (0x00) .....0.. "normal current"941* CHLF[1] "Analog DSP current control"942* = 0 (0x00) ......0. "normal current"943* CHLF[1] "ADC current control"944* = 0 (0x00) ......0. "normal current"945*/946{ 0x33, 0x09 },947948/*949* 34 VBLM "Blooming Control"950* = 80 (0x50) 01010000951* VBLM[7] "Hard soft reset switch"952* = 0 (0x00) 0....... "Hard reset"953* VBLM[6:4] "Blooming voltage selection"954* = 5 (0x05) .101....955* VBLM[3:0] "Sensor current control"956* = 0 (0x00) ....0000957*/958{ 0x34, 0x50 },959960/*961* 36 VCHG "Sensor Precharge Voltage Control"962* = 0 (0x00) 00000000963* VCHG[7] "Reserved"964* = 0 (0x00) 0.......965* VCHG[6:4] "Sensor precharge voltage control"966* = 0 (0x00) .000....967* VCHG[3:0] "Sensor array common reference"968* = 0 (0x00) ....0000969*/970{ 0x36, 0x00 },971972/*973* 37 ADC "ADC Reference Control"974* = 4 (0x04) 00000100975* ADC[7:4] "Reserved"976* = 0 (0x00) 0000....977* ADC[3] "ADC input signal range"978* = 0 (0x00) ....0... "Input signal 1.0x"979* ADC[2:0] "ADC range control"980* = 4 (0x04) .....100981*/982{ 0x37, 0x04 },983984/*985* 38 ACOM "Analog Common Ground"986* = 82 (0x52) 01010010987* ACOM[7] "Analog gain control"988* = 0 (0x00) 0....... "Gain 1x"989* ACOM[6] "Analog black level calibration"990* = 1 (0x01) .1...... "On"991* ACOM[5:0] "Reserved"992* = 18 (0x12) ..010010993*/994{ 0x38, 0x52 },995996/*997* 3A FREFA "Internal Reference Adjustment"998* = 0 (0x00) 00000000999* FREFA[7:0] "Range"1000* = 0 (0x00) 000000001001*/1002{ 0x3a, 0x00 },10031004/*1005* 3C FVOPT "Internal Reference Adjustment"1006* = 31 (0x1F) 000111111007* FVOPT[7:0] "Range"1008* = 31 (0x1F) 000111111009*/1010{ 0x3c, 0x1f },10111012/*1013* 44 Undocumented = 0 (0x00) 000000001014* 44[7:0] "It's a secret"1015* = 0 (0x00) 000000001016*/1017{ 0x44, 0x00 },10181019/*1020* 40 Undocumented = 0 (0x00) 000000001021* 40[7:0] "It's a secret"1022* = 0 (0x00) 000000001023*/1024{ 0x40, 0x00 },10251026/*1027* 41 Undocumented = 0 (0x00) 000000001028* 41[7:0] "It's a secret"1029* = 0 (0x00) 000000001030*/1031{ 0x41, 0x00 },10321033/*1034* 42 Undocumented = 0 (0x00) 000000001035* 42[7:0] "It's a secret"1036* = 0 (0x00) 000000001037*/1038{ 0x42, 0x00 },10391040/*1041* 43 Undocumented = 0 (0x00) 000000001042* 43[7:0] "It's a secret"1043* = 0 (0x00) 000000001044*/1045{ 0x43, 0x00 },10461047/*1048* 45 Undocumented = 128 (0x80) 100000001049* 45[7:0] "It's a secret"1050* = 128 (0x80) 100000001051*/1052{ 0x45, 0x80 },10531054/*1055* 48 Undocumented = 192 (0xC0) 110000001056* 48[7:0] "It's a secret"1057* = 192 (0xC0) 110000001058*/1059{ 0x48, 0xc0 },10601061/*1062* 49 Undocumented = 25 (0x19) 000110011063* 49[7:0] "It's a secret"1064* = 25 (0x19) 000110011065*/1066{ 0x49, 0x19 },10671068/*1069* 4B Undocumented = 128 (0x80) 100000001070* 4B[7:0] "It's a secret"1071* = 128 (0x80) 100000001072*/1073{ 0x4b, 0x80 },10741075/*1076* 4D Undocumented = 196 (0xC4) 110001001077* 4D[7:0] "It's a secret"1078* = 196 (0xC4) 110001001079*/1080{ 0x4d, 0xc4 },10811082/*1083* 35 VREF "Reference Voltage Control"1084* = 76 (0x4c) 010011001085* VREF[7:5] "Column high reference control"1086* = 2 (0x02) 010..... "higher voltage"1087* VREF[4:2] "Column low reference control"1088* = 3 (0x03) ...011.. "Highest voltage"1089* VREF[1:0] "Reserved"1090* = 0 (0x00) ......001091*/1092{ 0x35, 0x4c },10931094/*1095* 3D Undocumented = 0 (0x00) 000000001096* 3D[7:0] "It's a secret"1097* = 0 (0x00) 000000001098*/1099{ 0x3d, 0x00 },11001101/*1102* 3E Undocumented = 0 (0x00) 000000001103* 3E[7:0] "It's a secret"1104* = 0 (0x00) 000000001105*/1106{ 0x3e, 0x00 },11071108/*1109* 3B FREFB "Internal Reference Adjustment"1110* = 24 (0x18) 000110001111* FREFB[7:0] "Range"1112* = 24 (0x18) 000110001113*/1114{ 0x3b, 0x18 },11151116/*1117* 33 CHLF "Current Control"1118* = 25 (0x19) 000110011119* CHLF[7:6] "Sensor current control"1120* = 0 (0x00) 00......1121* CHLF[5] "Sensor current range control"1122* = 0 (0x00) ..0..... "normal range"1123* CHLF[4] "Sensor current"1124* = 1 (0x01) ...1.... "double current"1125* CHLF[3] "Sensor buffer current control"1126* = 1 (0x01) ....1... "half current"1127* CHLF[2] "Column buffer current control"1128* = 0 (0x00) .....0.. "normal current"1129* CHLF[1] "Analog DSP current control"1130* = 0 (0x00) ......0. "normal current"1131* CHLF[1] "ADC current control"1132* = 0 (0x00) ......0. "normal current"1133*/1134{ 0x33, 0x19 },11351136/*1137* 34 VBLM "Blooming Control"1138* = 90 (0x5A) 010110101139* VBLM[7] "Hard soft reset switch"1140* = 0 (0x00) 0....... "Hard reset"1141* VBLM[6:4] "Blooming voltage selection"1142* = 5 (0x05) .101....1143* VBLM[3:0] "Sensor current control"1144* = 10 (0x0A) ....10101145*/1146{ 0x34, 0x5a },11471148/*1149* 3B FREFB "Internal Reference Adjustment"1150* = 0 (0x00) 000000001151* FREFB[7:0] "Range"1152* = 0 (0x00) 000000001153*/1154{ 0x3b, 0x00 },11551156/*1157* 33 CHLF "Current Control"1158* = 9 (0x09) 000010011159* CHLF[7:6] "Sensor current control"1160* = 0 (0x00) 00......1161* CHLF[5] "Sensor current range control"1162* = 0 (0x00) ..0..... "normal range"1163* CHLF[4] "Sensor current"1164* = 0 (0x00) ...0.... "normal current"1165* CHLF[3] "Sensor buffer current control"1166* = 1 (0x01) ....1... "half current"1167* CHLF[2] "Column buffer current control"1168* = 0 (0x00) .....0.. "normal current"1169* CHLF[1] "Analog DSP current control"1170* = 0 (0x00) ......0. "normal current"1171* CHLF[1] "ADC current control"1172* = 0 (0x00) ......0. "normal current"1173*/1174{ 0x33, 0x09 },11751176/*1177* 34 VBLM "Blooming Control"1178* = 80 (0x50) 010100001179* VBLM[7] "Hard soft reset switch"1180* = 0 (0x00) 0....... "Hard reset"1181* VBLM[6:4] "Blooming voltage selection"1182* = 5 (0x05) .101....1183* VBLM[3:0] "Sensor current control"1184* = 0 (0x00) ....00001185*/1186{ 0x34, 0x50 },11871188/*1189* 12 COMH "Common Control H"1190* = 64 (0x40) 010000001191* COMH[7] "SRST"1192* = 0 (0x00) 0....... "No-op"1193* COMH[6:4] "Resolution selection"1194* = 4 (0x04) .100.... "XGA"1195* COMH[3] "Master slave selection"1196* = 0 (0x00) ....0... "Master mode"1197* COMH[2] "Internal B/R channel option"1198* = 0 (0x00) .....0.. "B/R use same channel"1199* COMH[1] "Color bar test pattern"1200* = 0 (0x00) ......0. "Off"1201* COMH[0] "Reserved"1202* = 0 (0x00) .......01203*/1204{ 0x12, 0x40 },12051206/*1207* 17 HREFST "Horizontal window start"1208* = 31 (0x1F) 000111111209* HREFST[7:0] "Horizontal window start, 8 MSBs"1210* = 31 (0x1F) 000111111211*/1212{ 0x17, 0x1f },12131214/*1215* 18 HREFEND "Horizontal window end"1216* = 95 (0x5F) 010111111217* HREFEND[7:0] "Horizontal Window End, 8 MSBs"1218* = 95 (0x5F) 010111111219*/1220{ 0x18, 0x5f },12211222/*1223* 19 VSTRT "Vertical window start"1224* = 0 (0x00) 000000001225* VSTRT[7:0] "Vertical Window Start, 8 MSBs"1226* = 0 (0x00) 000000001227*/1228{ 0x19, 0x00 },12291230/*1231* 1A VEND "Vertical window end"1232* = 96 (0x60) 011000001233* VEND[7:0] "Vertical Window End, 8 MSBs"1234* = 96 (0x60) 011000001235*/1236{ 0x1a, 0x60 },12371238/*1239* 32 COMM "Common Control M"1240* = 18 (0x12) 000100101241* COMM[7:6] "Pixel clock divide option"1242* = 0 (0x00) 00...... "/1"1243* COMM[5:3] "Horizontal window end position, 3 LSBs"1244* = 2 (0x02) ..010...1245* COMM[2:0] "Horizontal window start position, 3 LSBs"1246* = 2 (0x02) .....0101247*/1248{ 0x32, 0x12 },12491250/*1251* 03 COMA "Common Control A"1252* = 74 (0x4A) 010010101253* COMA[7:4] "AWB Update Threshold"1254* = 4 (0x04) 0100....1255* COMA[3:2] "Vertical window end line control 2 LSBs"1256* = 2 (0x02) ....10..1257* COMA[1:0] "Vertical window start line control 2 LSBs"1258* = 2 (0x02) ......101259*/1260{ 0x03, 0x4a },12611262/*1263* 11 CLKRC "Clock Rate Control"1264* = 128 (0x80) 100000001265* CLKRC[7] "Internal frequency doublers on off seclection"1266* = 1 (0x01) 1....... "On"1267* CLKRC[6] "Digital video master slave selection"1268* = 0 (0x00) .0...... "Master mode, sensor1269* provides PCLK"1270* CLKRC[5:0] "Clock divider { CLK = PCLK/(1+CLKRC[5:0]) }"1271* = 0 (0x00) ..0000001272*/1273{ 0x11, 0x80 },12741275/*1276* 12 COMH "Common Control H"1277* = 0 (0x00) 000000001278* COMH[7] "SRST"1279* = 0 (0x00) 0....... "No-op"1280* COMH[6:4] "Resolution selection"1281* = 0 (0x00) .000.... "QXGA"1282* COMH[3] "Master slave selection"1283* = 0 (0x00) ....0... "Master mode"1284* COMH[2] "Internal B/R channel option"1285* = 0 (0x00) .....0.. "B/R use same channel"1286* COMH[1] "Color bar test pattern"1287* = 0 (0x00) ......0. "Off"1288* COMH[0] "Reserved"1289* = 0 (0x00) .......01290*/1291{ 0x12, 0x00 },12921293/*1294* 12 COMH "Common Control H"1295* = 64 (0x40) 010000001296* COMH[7] "SRST"1297* = 0 (0x00) 0....... "No-op"1298* COMH[6:4] "Resolution selection"1299* = 4 (0x04) .100.... "XGA"1300* COMH[3] "Master slave selection"1301* = 0 (0x00) ....0... "Master mode"1302* COMH[2] "Internal B/R channel option"1303* = 0 (0x00) .....0.. "B/R use same channel"1304* COMH[1] "Color bar test pattern"1305* = 0 (0x00) ......0. "Off"1306* COMH[0] "Reserved"1307* = 0 (0x00) .......01308*/1309{ 0x12, 0x40 },13101311/*1312* 17 HREFST "Horizontal window start"1313* = 31 (0x1F) 000111111314* HREFST[7:0] "Horizontal window start, 8 MSBs"1315* = 31 (0x1F) 000111111316*/1317{ 0x17, 0x1f },13181319/*1320* 18 HREFEND "Horizontal window end"1321* = 95 (0x5F) 010111111322* HREFEND[7:0] "Horizontal Window End, 8 MSBs"1323* = 95 (0x5F) 010111111324*/1325{ 0x18, 0x5f },13261327/*1328* 19 VSTRT "Vertical window start"1329* = 0 (0x00) 000000001330* VSTRT[7:0] "Vertical Window Start, 8 MSBs"1331* = 0 (0x00) 000000001332*/1333{ 0x19, 0x00 },13341335/*1336* 1A VEND "Vertical window end"1337* = 96 (0x60) 011000001338* VEND[7:0] "Vertical Window End, 8 MSBs"1339* = 96 (0x60) 011000001340*/1341{ 0x1a, 0x60 },13421343/*1344* 32 COMM "Common Control M"1345* = 18 (0x12) 000100101346* COMM[7:6] "Pixel clock divide option"1347* = 0 (0x00) 00...... "/1"1348* COMM[5:3] "Horizontal window end position, 3 LSBs"1349* = 2 (0x02) ..010...1350* COMM[2:0] "Horizontal window start position, 3 LSBs"1351* = 2 (0x02) .....0101352*/1353{ 0x32, 0x12 },13541355/*1356* 03 COMA "Common Control A"1357* = 74 (0x4A) 010010101358* COMA[7:4] "AWB Update Threshold"1359* = 4 (0x04) 0100....1360* COMA[3:2] "Vertical window end line control 2 LSBs"1361* = 2 (0x02) ....10..1362* COMA[1:0] "Vertical window start line control 2 LSBs"1363* = 2 (0x02) ......101364*/1365{ 0x03, 0x4a },13661367/*1368* 02 RED "Red Gain Control"1369* = 175 (0xAF) 101011111370* RED[7] "Action"1371* = 1 (0x01) 1....... "gain = 1/(1+bitrev([6:0]))"1372* RED[6:0] "Value"1373* = 47 (0x2F) .01011111374*/1375{ 0x02, 0xaf },13761377/*1378* 2D ADDVSL "VSYNC Pulse Width"1379* = 210 (0xD2) 110100101380* ADDVSL[7:0] "VSYNC pulse width, LSB"1381* = 210 (0xD2) 110100101382*/1383{ 0x2d, 0xd2 },13841385/*1386* 00 GAIN = 24 (0x18) 000110001387* GAIN[7:6] "Reserved"1388* = 0 (0x00) 00......1389* GAIN[5] "Double"1390* = 0 (0x00) ..0..... "False"1391* GAIN[4] "Double"1392* = 1 (0x01) ...1.... "True"1393* GAIN[3:0] "Range"1394* = 8 (0x08) ....10001395*/1396{ 0x00, 0x18 },13971398/*1399* 01 BLUE "Blue Gain Control"1400* = 240 (0xF0) 111100001401* BLUE[7] "Action"1402* = 1 (0x01) 1....... "gain = 1/(1+bitrev([6:0]))"1403* BLUE[6:0] "Value"1404* = 112 (0x70) .11100001405*/1406{ 0x01, 0xf0 },14071408/*1409* 10 AEC "Automatic Exposure Control"1410* = 10 (0x0A) 000010101411* AEC[7:0] "Automatic Exposure Control, 8 MSBs"1412* = 10 (0x0A) 000010101413*/1414{ 0x10, 0x0a },14151416{ 0xe1, 0x67 },1417{ 0xe3, 0x03 },1418{ 0xe4, 0x26 },1419{ 0xe5, 0x3e },1420{ 0xf8, 0x01 },1421{ 0xff, 0x01 },1422};14231424static const struct ov_i2c_regvals norm_6x20[] = {1425{ 0x12, 0x80 }, /* reset */1426{ 0x11, 0x01 },1427{ 0x03, 0x60 },1428{ 0x05, 0x7f }, /* For when autoadjust is off */1429{ 0x07, 0xa8 },1430/* The ratio of 0x0c and 0x0d controls the white point */1431{ 0x0c, 0x24 },1432{ 0x0d, 0x24 },1433{ 0x0f, 0x15 }, /* COMS */1434{ 0x10, 0x75 }, /* AEC Exposure time */1435{ 0x12, 0x24 }, /* Enable AGC */1436{ 0x14, 0x04 },1437/* 0x16: 0x06 helps frame stability with moving objects */1438{ 0x16, 0x06 },1439/* { 0x20, 0x30 }, * Aperture correction enable */1440{ 0x26, 0xb2 }, /* BLC enable */1441/* 0x28: 0x05 Selects RGB format if RGB on */1442{ 0x28, 0x05 },1443{ 0x2a, 0x04 }, /* Disable framerate adjust */1444/* { 0x2b, 0xac }, * Framerate; Set 2a[7] first */1445{ 0x2d, 0x85 },1446{ 0x33, 0xa0 }, /* Color Processing Parameter */1447{ 0x34, 0xd2 }, /* Max A/D range */1448{ 0x38, 0x8b },1449{ 0x39, 0x40 },14501451{ 0x3c, 0x39 }, /* Enable AEC mode changing */1452{ 0x3c, 0x3c }, /* Change AEC mode */1453{ 0x3c, 0x24 }, /* Disable AEC mode changing */14541455{ 0x3d, 0x80 },1456/* These next two registers (0x4a, 0x4b) are undocumented.1457* They control the color balance */1458{ 0x4a, 0x80 },1459{ 0x4b, 0x80 },1460{ 0x4d, 0xd2 }, /* This reduces noise a bit */1461{ 0x4e, 0xc1 },1462{ 0x4f, 0x04 },1463/* Do 50-53 have any effect? */1464/* Toggle 0x12[2] off and on here? */1465};14661467static const struct ov_i2c_regvals norm_6x30[] = {1468{ 0x12, 0x80 }, /* Reset */1469{ 0x00, 0x1f }, /* Gain */1470{ 0x01, 0x99 }, /* Blue gain */1471{ 0x02, 0x7c }, /* Red gain */1472{ 0x03, 0xc0 }, /* Saturation */1473{ 0x05, 0x0a }, /* Contrast */1474{ 0x06, 0x95 }, /* Brightness */1475{ 0x07, 0x2d }, /* Sharpness */1476{ 0x0c, 0x20 },1477{ 0x0d, 0x20 },1478{ 0x0e, 0xa0 }, /* Was 0x20, bit7 enables a 2x gain which we need */1479{ 0x0f, 0x05 },1480{ 0x10, 0x9a },1481{ 0x11, 0x00 }, /* Pixel clock = fastest */1482{ 0x12, 0x24 }, /* Enable AGC and AWB */1483{ 0x13, 0x21 },1484{ 0x14, 0x80 },1485{ 0x15, 0x01 },1486{ 0x16, 0x03 },1487{ 0x17, 0x38 },1488{ 0x18, 0xea },1489{ 0x19, 0x04 },1490{ 0x1a, 0x93 },1491{ 0x1b, 0x00 },1492{ 0x1e, 0xc4 },1493{ 0x1f, 0x04 },1494{ 0x20, 0x20 },1495{ 0x21, 0x10 },1496{ 0x22, 0x88 },1497{ 0x23, 0xc0 }, /* Crystal circuit power level */1498{ 0x25, 0x9a }, /* Increase AEC black ratio */1499{ 0x26, 0xb2 }, /* BLC enable */1500{ 0x27, 0xa2 },1501{ 0x28, 0x00 },1502{ 0x29, 0x00 },1503{ 0x2a, 0x84 }, /* 60 Hz power */1504{ 0x2b, 0xa8 }, /* 60 Hz power */1505{ 0x2c, 0xa0 },1506{ 0x2d, 0x95 }, /* Enable auto-brightness */1507{ 0x2e, 0x88 },1508{ 0x33, 0x26 },1509{ 0x34, 0x03 },1510{ 0x36, 0x8f },1511{ 0x37, 0x80 },1512{ 0x38, 0x83 },1513{ 0x39, 0x80 },1514{ 0x3a, 0x0f },1515{ 0x3b, 0x3c },1516{ 0x3c, 0x1a },1517{ 0x3d, 0x80 },1518{ 0x3e, 0x80 },1519{ 0x3f, 0x0e },1520{ 0x40, 0x00 }, /* White bal */1521{ 0x41, 0x00 }, /* White bal */1522{ 0x42, 0x80 },1523{ 0x43, 0x3f }, /* White bal */1524{ 0x44, 0x80 },1525{ 0x45, 0x20 },1526{ 0x46, 0x20 },1527{ 0x47, 0x80 },1528{ 0x48, 0x7f },1529{ 0x49, 0x00 },1530{ 0x4a, 0x00 },1531{ 0x4b, 0x80 },1532{ 0x4c, 0xd0 },1533{ 0x4d, 0x10 }, /* U = 0.563u, V = 0.714v */1534{ 0x4e, 0x40 },1535{ 0x4f, 0x07 }, /* UV avg., col. killer: max */1536{ 0x50, 0xff },1537{ 0x54, 0x23 }, /* Max AGC gain: 18dB */1538{ 0x55, 0xff },1539{ 0x56, 0x12 },1540{ 0x57, 0x81 },1541{ 0x58, 0x75 },1542{ 0x59, 0x01 }, /* AGC dark current comp.: +1 */1543{ 0x5a, 0x2c },1544{ 0x5b, 0x0f }, /* AWB chrominance levels */1545{ 0x5c, 0x10 },1546{ 0x3d, 0x80 },1547{ 0x27, 0xa6 },1548{ 0x12, 0x20 }, /* Toggle AWB */1549{ 0x12, 0x24 },1550};15511552/* Lawrence Glaister <[email protected]> reports:1553*1554* Register 0x0f in the 7610 has the following effects:1555*1556* 0x85 (AEC method 1): Best overall, good contrast range1557* 0x45 (AEC method 2): Very overexposed1558* 0xa5 (spec sheet default): Ok, but the black level is1559* shifted resulting in loss of contrast1560* 0x05 (old driver setting): very overexposed, too much1561* contrast1562*/1563static const struct ov_i2c_regvals norm_7610[] = {1564{ 0x10, 0xff },1565{ 0x16, 0x06 },1566{ 0x28, 0x24 },1567{ 0x2b, 0xac },1568{ 0x12, 0x00 },1569{ 0x38, 0x81 },1570{ 0x28, 0x24 }, /* 0c */1571{ 0x0f, 0x85 }, /* lg's setting */1572{ 0x15, 0x01 },1573{ 0x20, 0x1c },1574{ 0x23, 0x2a },1575{ 0x24, 0x10 },1576{ 0x25, 0x8a },1577{ 0x26, 0xa2 },1578{ 0x27, 0xc2 },1579{ 0x2a, 0x04 },1580{ 0x2c, 0xfe },1581{ 0x2d, 0x93 },1582{ 0x30, 0x71 },1583{ 0x31, 0x60 },1584{ 0x32, 0x26 },1585{ 0x33, 0x20 },1586{ 0x34, 0x48 },1587{ 0x12, 0x24 },1588{ 0x11, 0x01 },1589{ 0x0c, 0x24 },1590{ 0x0d, 0x24 },1591};15921593static const struct ov_i2c_regvals norm_7620[] = {1594{ 0x12, 0x80 }, /* reset */1595{ 0x00, 0x00 }, /* gain */1596{ 0x01, 0x80 }, /* blue gain */1597{ 0x02, 0x80 }, /* red gain */1598{ 0x03, 0xc0 }, /* OV7670_R03_VREF */1599{ 0x06, 0x60 },1600{ 0x07, 0x00 },1601{ 0x0c, 0x24 },1602{ 0x0c, 0x24 },1603{ 0x0d, 0x24 },1604{ 0x11, 0x01 },1605{ 0x12, 0x24 },1606{ 0x13, 0x01 },1607{ 0x14, 0x84 },1608{ 0x15, 0x01 },1609{ 0x16, 0x03 },1610{ 0x17, 0x2f },1611{ 0x18, 0xcf },1612{ 0x19, 0x06 },1613{ 0x1a, 0xf5 },1614{ 0x1b, 0x00 },1615{ 0x20, 0x18 },1616{ 0x21, 0x80 },1617{ 0x22, 0x80 },1618{ 0x23, 0x00 },1619{ 0x26, 0xa2 },1620{ 0x27, 0xea },1621{ 0x28, 0x22 }, /* Was 0x20, bit1 enables a 2x gain which we need */1622{ 0x29, 0x00 },1623{ 0x2a, 0x10 },1624{ 0x2b, 0x00 },1625{ 0x2c, 0x88 },1626{ 0x2d, 0x91 },1627{ 0x2e, 0x80 },1628{ 0x2f, 0x44 },1629{ 0x60, 0x27 },1630{ 0x61, 0x02 },1631{ 0x62, 0x5f },1632{ 0x63, 0xd5 },1633{ 0x64, 0x57 },1634{ 0x65, 0x83 },1635{ 0x66, 0x55 },1636{ 0x67, 0x92 },1637{ 0x68, 0xcf },1638{ 0x69, 0x76 },1639{ 0x6a, 0x22 },1640{ 0x6b, 0x00 },1641{ 0x6c, 0x02 },1642{ 0x6d, 0x44 },1643{ 0x6e, 0x80 },1644{ 0x6f, 0x1d },1645{ 0x70, 0x8b },1646{ 0x71, 0x00 },1647{ 0x72, 0x14 },1648{ 0x73, 0x54 },1649{ 0x74, 0x00 },1650{ 0x75, 0x8e },1651{ 0x76, 0x00 },1652{ 0x77, 0xff },1653{ 0x78, 0x80 },1654{ 0x79, 0x80 },1655{ 0x7a, 0x80 },1656{ 0x7b, 0xe2 },1657{ 0x7c, 0x00 },1658};16591660/* 7640 and 7648. The defaults should be OK for most registers. */1661static const struct ov_i2c_regvals norm_7640[] = {1662{ 0x12, 0x80 },1663{ 0x12, 0x14 },1664};16651666static const struct ov_regvals init_519_ov7660[] = {1667{ 0x5d, 0x03 }, /* Turn off suspend mode */1668{ 0x53, 0x9b }, /* 0x9f enables the (unused) microcontroller */1669{ 0x54, 0x0f }, /* bit2 (jpeg enable) */1670{ 0xa2, 0x20 }, /* a2-a5 are undocumented */1671{ 0xa3, 0x18 },1672{ 0xa4, 0x04 },1673{ 0xa5, 0x28 },1674{ 0x37, 0x00 }, /* SetUsbInit */1675{ 0x55, 0x02 }, /* 4.096 Mhz audio clock */1676/* Enable both fields, YUV Input, disable defect comp (why?) */1677{ 0x20, 0x0c }, /* 0x0d does U <-> V swap */1678{ 0x21, 0x38 },1679{ 0x22, 0x1d },1680{ 0x17, 0x50 }, /* undocumented */1681{ 0x37, 0x00 }, /* undocumented */1682{ 0x40, 0xff }, /* I2C timeout counter */1683{ 0x46, 0x00 }, /* I2C clock prescaler */1684};1685static const struct ov_i2c_regvals norm_7660[] = {1686{OV7670_R12_COM7, OV7670_COM7_RESET},1687{OV7670_R11_CLKRC, 0x81},1688{0x92, 0x00}, /* DM_LNL */1689{0x93, 0x00}, /* DM_LNH */1690{0x9d, 0x4c}, /* BD50ST */1691{0x9e, 0x3f}, /* BD60ST */1692{OV7670_R3B_COM11, 0x02},1693{OV7670_R13_COM8, 0xf5},1694{OV7670_R10_AECH, 0x00},1695{OV7670_R00_GAIN, 0x00},1696{OV7670_R01_BLUE, 0x7c},1697{OV7670_R02_RED, 0x9d},1698{OV7670_R12_COM7, 0x00},1699{OV7670_R04_COM1, 00},1700{OV7670_R18_HSTOP, 0x01},1701{OV7670_R17_HSTART, 0x13},1702{OV7670_R32_HREF, 0x92},1703{OV7670_R19_VSTART, 0x02},1704{OV7670_R1A_VSTOP, 0x7a},1705{OV7670_R03_VREF, 0x00},1706{OV7670_R0E_COM5, 0x04},1707{OV7670_R0F_COM6, 0x62},1708{OV7670_R15_COM10, 0x00},1709{0x16, 0x02}, /* RSVD */1710{0x1b, 0x00}, /* PSHFT */1711{OV7670_R1E_MVFP, 0x01},1712{0x29, 0x3c}, /* RSVD */1713{0x33, 0x00}, /* CHLF */1714{0x34, 0x07}, /* ARBLM */1715{0x35, 0x84}, /* RSVD */1716{0x36, 0x00}, /* RSVD */1717{0x37, 0x04}, /* ADC */1718{0x39, 0x43}, /* OFON */1719{OV7670_R3A_TSLB, 0x00},1720{OV7670_R3C_COM12, 0x6c},1721{OV7670_R3D_COM13, 0x98},1722{OV7670_R3F_EDGE, 0x23},1723{OV7670_R40_COM15, 0xc1},1724{OV7670_R41_COM16, 0x22},1725{0x6b, 0x0a}, /* DBLV */1726{0xa1, 0x08}, /* RSVD */1727{0x69, 0x80}, /* HV */1728{0x43, 0xf0}, /* RSVD.. */1729{0x44, 0x10},1730{0x45, 0x78},1731{0x46, 0xa8},1732{0x47, 0x60},1733{0x48, 0x80},1734{0x59, 0xba},1735{0x5a, 0x9a},1736{0x5b, 0x22},1737{0x5c, 0xb9},1738{0x5d, 0x9b},1739{0x5e, 0x10},1740{0x5f, 0xe0},1741{0x60, 0x85},1742{0x61, 0x60},1743{0x9f, 0x9d}, /* RSVD */1744{0xa0, 0xa0}, /* DSPC2 */1745{0x4f, 0x60}, /* matrix */1746{0x50, 0x64},1747{0x51, 0x04},1748{0x52, 0x18},1749{0x53, 0x3c},1750{0x54, 0x54},1751{0x55, 0x40},1752{0x56, 0x40},1753{0x57, 0x40},1754{0x58, 0x0d}, /* matrix sign */1755{0x8b, 0xcc}, /* RSVD */1756{0x8c, 0xcc},1757{0x8d, 0xcf},1758{0x6c, 0x40}, /* gamma curve */1759{0x6d, 0xe0},1760{0x6e, 0xa0},1761{0x6f, 0x80},1762{0x70, 0x70},1763{0x71, 0x80},1764{0x72, 0x60},1765{0x73, 0x60},1766{0x74, 0x50},1767{0x75, 0x40},1768{0x76, 0x38},1769{0x77, 0x3c},1770{0x78, 0x32},1771{0x79, 0x1a},1772{0x7a, 0x28},1773{0x7b, 0x24},1774{0x7c, 0x04}, /* gamma curve */1775{0x7d, 0x12},1776{0x7e, 0x26},1777{0x7f, 0x46},1778{0x80, 0x54},1779{0x81, 0x64},1780{0x82, 0x70},1781{0x83, 0x7c},1782{0x84, 0x86},1783{0x85, 0x8e},1784{0x86, 0x9c},1785{0x87, 0xab},1786{0x88, 0xc4},1787{0x89, 0xd1},1788{0x8a, 0xe5},1789{OV7670_R14_COM9, 0x1e},1790{OV7670_R24_AEW, 0x80},1791{OV7670_R25_AEB, 0x72},1792{OV7670_R26_VPT, 0xb3},1793{0x62, 0x80}, /* LCC1 */1794{0x63, 0x80}, /* LCC2 */1795{0x64, 0x06}, /* LCC3 */1796{0x65, 0x00}, /* LCC4 */1797{0x66, 0x01}, /* LCC5 */1798{0x94, 0x0e}, /* RSVD.. */1799{0x95, 0x14},1800{OV7670_R13_COM8, OV7670_COM8_FASTAEC1801| OV7670_COM8_AECSTEP1802| OV7670_COM8_BFILT1803| 0x101804| OV7670_COM8_AGC1805| OV7670_COM8_AWB1806| OV7670_COM8_AEC},1807{0xa1, 0xc8}1808};18091810/* 7670. Defaults taken from OmniVision provided data,1811* as provided by Jonathan Corbet of OLPC */1812static const struct ov_i2c_regvals norm_7670[] = {1813{ OV7670_R12_COM7, OV7670_COM7_RESET },1814{ OV7670_R3A_TSLB, 0x04 }, /* OV */1815{ OV7670_R12_COM7, OV7670_COM7_FMT_VGA }, /* VGA */1816{ OV7670_R11_CLKRC, 0x01 },1817/*1818* Set the hardware window. These values from OV don't entirely1819* make sense - hstop is less than hstart. But they work...1820*/1821{ OV7670_R17_HSTART, 0x13 },1822{ OV7670_R18_HSTOP, 0x01 },1823{ OV7670_R32_HREF, 0xb6 },1824{ OV7670_R19_VSTART, 0x02 },1825{ OV7670_R1A_VSTOP, 0x7a },1826{ OV7670_R03_VREF, 0x0a },18271828{ OV7670_R0C_COM3, 0x00 },1829{ OV7670_R3E_COM14, 0x00 },1830/* Mystery scaling numbers */1831{ 0x70, 0x3a },1832{ 0x71, 0x35 },1833{ 0x72, 0x11 },1834{ 0x73, 0xf0 },1835{ 0xa2, 0x02 },1836/* { OV7670_R15_COM10, 0x0 }, */18371838/* Gamma curve values */1839{ 0x7a, 0x20 },1840{ 0x7b, 0x10 },1841{ 0x7c, 0x1e },1842{ 0x7d, 0x35 },1843{ 0x7e, 0x5a },1844{ 0x7f, 0x69 },1845{ 0x80, 0x76 },1846{ 0x81, 0x80 },1847{ 0x82, 0x88 },1848{ 0x83, 0x8f },1849{ 0x84, 0x96 },1850{ 0x85, 0xa3 },1851{ 0x86, 0xaf },1852{ 0x87, 0xc4 },1853{ 0x88, 0xd7 },1854{ 0x89, 0xe8 },18551856/* AGC and AEC parameters. Note we start by disabling those features,1857then turn them only after tweaking the values. */1858{ OV7670_R13_COM8, OV7670_COM8_FASTAEC1859| OV7670_COM8_AECSTEP1860| OV7670_COM8_BFILT },1861{ OV7670_R00_GAIN, 0x00 },1862{ OV7670_R10_AECH, 0x00 },1863{ OV7670_R0D_COM4, 0x40 }, /* magic reserved bit */1864{ OV7670_R14_COM9, 0x18 }, /* 4x gain + magic rsvd bit */1865{ OV7670_RA5_BD50MAX, 0x05 },1866{ OV7670_RAB_BD60MAX, 0x07 },1867{ OV7670_R24_AEW, 0x95 },1868{ OV7670_R25_AEB, 0x33 },1869{ OV7670_R26_VPT, 0xe3 },1870{ OV7670_R9F_HAECC1, 0x78 },1871{ OV7670_RA0_HAECC2, 0x68 },1872{ 0xa1, 0x03 }, /* magic */1873{ OV7670_RA6_HAECC3, 0xd8 },1874{ OV7670_RA7_HAECC4, 0xd8 },1875{ OV7670_RA8_HAECC5, 0xf0 },1876{ OV7670_RA9_HAECC6, 0x90 },1877{ OV7670_RAA_HAECC7, 0x94 },1878{ OV7670_R13_COM8, OV7670_COM8_FASTAEC1879| OV7670_COM8_AECSTEP1880| OV7670_COM8_BFILT1881| OV7670_COM8_AGC1882| OV7670_COM8_AEC },18831884/* Almost all of these are magic "reserved" values. */1885{ OV7670_R0E_COM5, 0x61 },1886{ OV7670_R0F_COM6, 0x4b },1887{ 0x16, 0x02 },1888{ OV7670_R1E_MVFP, 0x07 },1889{ 0x21, 0x02 },1890{ 0x22, 0x91 },1891{ 0x29, 0x07 },1892{ 0x33, 0x0b },1893{ 0x35, 0x0b },1894{ 0x37, 0x1d },1895{ 0x38, 0x71 },1896{ 0x39, 0x2a },1897{ OV7670_R3C_COM12, 0x78 },1898{ 0x4d, 0x40 },1899{ 0x4e, 0x20 },1900{ OV7670_R69_GFIX, 0x00 },1901{ 0x6b, 0x4a },1902{ 0x74, 0x10 },1903{ 0x8d, 0x4f },1904{ 0x8e, 0x00 },1905{ 0x8f, 0x00 },1906{ 0x90, 0x00 },1907{ 0x91, 0x00 },1908{ 0x96, 0x00 },1909{ 0x9a, 0x00 },1910{ 0xb0, 0x84 },1911{ 0xb1, 0x0c },1912{ 0xb2, 0x0e },1913{ 0xb3, 0x82 },1914{ 0xb8, 0x0a },19151916/* More reserved magic, some of which tweaks white balance */1917{ 0x43, 0x0a },1918{ 0x44, 0xf0 },1919{ 0x45, 0x34 },1920{ 0x46, 0x58 },1921{ 0x47, 0x28 },1922{ 0x48, 0x3a },1923{ 0x59, 0x88 },1924{ 0x5a, 0x88 },1925{ 0x5b, 0x44 },1926{ 0x5c, 0x67 },1927{ 0x5d, 0x49 },1928{ 0x5e, 0x0e },1929{ 0x6c, 0x0a },1930{ 0x6d, 0x55 },1931{ 0x6e, 0x11 },1932{ 0x6f, 0x9f }, /* "9e for advance AWB" */1933{ 0x6a, 0x40 },1934{ OV7670_R01_BLUE, 0x40 },1935{ OV7670_R02_RED, 0x60 },1936{ OV7670_R13_COM8, OV7670_COM8_FASTAEC1937| OV7670_COM8_AECSTEP1938| OV7670_COM8_BFILT1939| OV7670_COM8_AGC1940| OV7670_COM8_AEC1941| OV7670_COM8_AWB },19421943/* Matrix coefficients */1944{ 0x4f, 0x80 },1945{ 0x50, 0x80 },1946{ 0x51, 0x00 },1947{ 0x52, 0x22 },1948{ 0x53, 0x5e },1949{ 0x54, 0x80 },1950{ 0x58, 0x9e },19511952{ OV7670_R41_COM16, OV7670_COM16_AWBGAIN },1953{ OV7670_R3F_EDGE, 0x00 },1954{ 0x75, 0x05 },1955{ 0x76, 0xe1 },1956{ 0x4c, 0x00 },1957{ 0x77, 0x01 },1958{ OV7670_R3D_COM13, OV7670_COM13_GAMMA1959| OV7670_COM13_UVSAT1960| 2}, /* was 3 */1961{ 0x4b, 0x09 },1962{ 0xc9, 0x60 },1963{ OV7670_R41_COM16, 0x38 },1964{ 0x56, 0x40 },19651966{ 0x34, 0x11 },1967{ OV7670_R3B_COM11, OV7670_COM11_EXP|OV7670_COM11_HZAUTO },1968{ 0xa4, 0x88 },1969{ 0x96, 0x00 },1970{ 0x97, 0x30 },1971{ 0x98, 0x20 },1972{ 0x99, 0x30 },1973{ 0x9a, 0x84 },1974{ 0x9b, 0x29 },1975{ 0x9c, 0x03 },1976{ 0x9d, 0x4c },1977{ 0x9e, 0x3f },1978{ 0x78, 0x04 },19791980/* Extra-weird stuff. Some sort of multiplexor register */1981{ 0x79, 0x01 },1982{ 0xc8, 0xf0 },1983{ 0x79, 0x0f },1984{ 0xc8, 0x00 },1985{ 0x79, 0x10 },1986{ 0xc8, 0x7e },1987{ 0x79, 0x0a },1988{ 0xc8, 0x80 },1989{ 0x79, 0x0b },1990{ 0xc8, 0x01 },1991{ 0x79, 0x0c },1992{ 0xc8, 0x0f },1993{ 0x79, 0x0d },1994{ 0xc8, 0x20 },1995{ 0x79, 0x09 },1996{ 0xc8, 0x80 },1997{ 0x79, 0x02 },1998{ 0xc8, 0xc0 },1999{ 0x79, 0x03 },2000{ 0xc8, 0x40 },2001{ 0x79, 0x05 },2002{ 0xc8, 0x30 },2003{ 0x79, 0x26 },2004};20052006static const struct ov_i2c_regvals norm_8610[] = {2007{ 0x12, 0x80 },2008{ 0x00, 0x00 },2009{ 0x01, 0x80 },2010{ 0x02, 0x80 },2011{ 0x03, 0xc0 },2012{ 0x04, 0x30 },2013{ 0x05, 0x30 }, /* was 0x10, new from windrv 090403 */2014{ 0x06, 0x70 }, /* was 0x80, new from windrv 090403 */2015{ 0x0a, 0x86 },2016{ 0x0b, 0xb0 },2017{ 0x0c, 0x20 },2018{ 0x0d, 0x20 },2019{ 0x11, 0x01 },2020{ 0x12, 0x25 },2021{ 0x13, 0x01 },2022{ 0x14, 0x04 },2023{ 0x15, 0x01 }, /* Lin and Win think different about UV order */2024{ 0x16, 0x03 },2025{ 0x17, 0x38 }, /* was 0x2f, new from windrv 090403 */2026{ 0x18, 0xea }, /* was 0xcf, new from windrv 090403 */2027{ 0x19, 0x02 }, /* was 0x06, new from windrv 090403 */2028{ 0x1a, 0xf5 },2029{ 0x1b, 0x00 },2030{ 0x20, 0xd0 }, /* was 0x90, new from windrv 090403 */2031{ 0x23, 0xc0 }, /* was 0x00, new from windrv 090403 */2032{ 0x24, 0x30 }, /* was 0x1d, new from windrv 090403 */2033{ 0x25, 0x50 }, /* was 0x57, new from windrv 090403 */2034{ 0x26, 0xa2 },2035{ 0x27, 0xea },2036{ 0x28, 0x00 },2037{ 0x29, 0x00 },2038{ 0x2a, 0x80 },2039{ 0x2b, 0xc8 }, /* was 0xcc, new from windrv 090403 */2040{ 0x2c, 0xac },2041{ 0x2d, 0x45 }, /* was 0xd5, new from windrv 090403 */2042{ 0x2e, 0x80 },2043{ 0x2f, 0x14 }, /* was 0x01, new from windrv 090403 */2044{ 0x4c, 0x00 },2045{ 0x4d, 0x30 }, /* was 0x10, new from windrv 090403 */2046{ 0x60, 0x02 }, /* was 0x01, new from windrv 090403 */2047{ 0x61, 0x00 }, /* was 0x09, new from windrv 090403 */2048{ 0x62, 0x5f }, /* was 0xd7, new from windrv 090403 */2049{ 0x63, 0xff },2050{ 0x64, 0x53 }, /* new windrv 090403 says 0x57,2051* maybe thats wrong */2052{ 0x65, 0x00 },2053{ 0x66, 0x55 },2054{ 0x67, 0xb0 },2055{ 0x68, 0xc0 }, /* was 0xaf, new from windrv 090403 */2056{ 0x69, 0x02 },2057{ 0x6a, 0x22 },2058{ 0x6b, 0x00 },2059{ 0x6c, 0x99 }, /* was 0x80, old windrv says 0x00, but2060* deleting bit7 colors the first images red */2061{ 0x6d, 0x11 }, /* was 0x00, new from windrv 090403 */2062{ 0x6e, 0x11 }, /* was 0x00, new from windrv 090403 */2063{ 0x6f, 0x01 },2064{ 0x70, 0x8b },2065{ 0x71, 0x00 },2066{ 0x72, 0x14 },2067{ 0x73, 0x54 },2068{ 0x74, 0x00 },/* 0x60? - was 0x00, new from windrv 090403 */2069{ 0x75, 0x0e },2070{ 0x76, 0x02 }, /* was 0x02, new from windrv 090403 */2071{ 0x77, 0xff },2072{ 0x78, 0x80 },2073{ 0x79, 0x80 },2074{ 0x7a, 0x80 },2075{ 0x7b, 0x10 }, /* was 0x13, new from windrv 090403 */2076{ 0x7c, 0x00 },2077{ 0x7d, 0x08 }, /* was 0x09, new from windrv 090403 */2078{ 0x7e, 0x08 }, /* was 0xc0, new from windrv 090403 */2079{ 0x7f, 0xfb },2080{ 0x80, 0x28 },2081{ 0x81, 0x00 },2082{ 0x82, 0x23 },2083{ 0x83, 0x0b },2084{ 0x84, 0x00 },2085{ 0x85, 0x62 }, /* was 0x61, new from windrv 090403 */2086{ 0x86, 0xc9 },2087{ 0x87, 0x00 },2088{ 0x88, 0x00 },2089{ 0x89, 0x01 },2090{ 0x12, 0x20 },2091{ 0x12, 0x25 }, /* was 0x24, new from windrv 090403 */2092};20932094static unsigned char ov7670_abs_to_sm(unsigned char v)2095{2096if (v > 127)2097return v & 0x7f;2098return (128 - v) | 0x80;2099}21002101/* Write a OV519 register */2102static void reg_w(struct sd *sd, u16 index, u16 value)2103{2104int ret, req = 0;21052106if (sd->gspca_dev.usb_err < 0)2107return;21082109switch (sd->bridge) {2110case BRIDGE_OV511:2111case BRIDGE_OV511PLUS:2112req = 2;2113break;2114case BRIDGE_OVFX2:2115req = 0x0a;2116/* fall through */2117case BRIDGE_W9968CF:2118PDEBUG(D_USBO, "SET %02x %04x %04x",2119req, value, index);2120ret = usb_control_msg(sd->gspca_dev.dev,2121usb_sndctrlpipe(sd->gspca_dev.dev, 0),2122req,2123USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,2124value, index, NULL, 0, 500);2125goto leave;2126default:2127req = 1;2128}21292130PDEBUG(D_USBO, "SET %02x 0000 %04x %02x",2131req, index, value);2132sd->gspca_dev.usb_buf[0] = value;2133ret = usb_control_msg(sd->gspca_dev.dev,2134usb_sndctrlpipe(sd->gspca_dev.dev, 0),2135req,2136USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,21370, index,2138sd->gspca_dev.usb_buf, 1, 500);2139leave:2140if (ret < 0) {2141err("reg_w %02x failed %d", index, ret);2142sd->gspca_dev.usb_err = ret;2143return;2144}2145}21462147/* Read from a OV519 register, note not valid for the w9968cf!! */2148/* returns: negative is error, pos or zero is data */2149static int reg_r(struct sd *sd, u16 index)2150{2151int ret;2152int req;21532154if (sd->gspca_dev.usb_err < 0)2155return -1;21562157switch (sd->bridge) {2158case BRIDGE_OV511:2159case BRIDGE_OV511PLUS:2160req = 3;2161break;2162case BRIDGE_OVFX2:2163req = 0x0b;2164break;2165default:2166req = 1;2167}21682169ret = usb_control_msg(sd->gspca_dev.dev,2170usb_rcvctrlpipe(sd->gspca_dev.dev, 0),2171req,2172USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,21730, index, sd->gspca_dev.usb_buf, 1, 500);21742175if (ret >= 0) {2176ret = sd->gspca_dev.usb_buf[0];2177PDEBUG(D_USBI, "GET %02x 0000 %04x %02x",2178req, index, ret);2179} else {2180err("reg_r %02x failed %d", index, ret);2181sd->gspca_dev.usb_err = ret;2182}21832184return ret;2185}21862187/* Read 8 values from a OV519 register */2188static int reg_r8(struct sd *sd,2189u16 index)2190{2191int ret;21922193if (sd->gspca_dev.usb_err < 0)2194return -1;21952196ret = usb_control_msg(sd->gspca_dev.dev,2197usb_rcvctrlpipe(sd->gspca_dev.dev, 0),21981, /* REQ_IO */2199USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,22000, index, sd->gspca_dev.usb_buf, 8, 500);22012202if (ret >= 0) {2203ret = sd->gspca_dev.usb_buf[0];2204} else {2205err("reg_r8 %02x failed %d", index, ret);2206sd->gspca_dev.usb_err = ret;2207}22082209return ret;2210}22112212/*2213* Writes bits at positions specified by mask to an OV51x reg. Bits that are in2214* the same position as 1's in "mask" are cleared and set to "value". Bits2215* that are in the same position as 0's in "mask" are preserved, regardless2216* of their respective state in "value".2217*/2218static void reg_w_mask(struct sd *sd,2219u16 index,2220u8 value,2221u8 mask)2222{2223int ret;2224u8 oldval;22252226if (mask != 0xff) {2227value &= mask; /* Enforce mask on value */2228ret = reg_r(sd, index);2229if (ret < 0)2230return;22312232oldval = ret & ~mask; /* Clear the masked bits */2233value |= oldval; /* Set the desired bits */2234}2235reg_w(sd, index, value);2236}22372238/*2239* Writes multiple (n) byte value to a single register. Only valid with certain2240* registers (0x30 and 0xc4 - 0xce).2241*/2242static void ov518_reg_w32(struct sd *sd, u16 index, u32 value, int n)2243{2244int ret;22452246if (sd->gspca_dev.usb_err < 0)2247return;22482249*((__le32 *) sd->gspca_dev.usb_buf) = __cpu_to_le32(value);22502251ret = usb_control_msg(sd->gspca_dev.dev,2252usb_sndctrlpipe(sd->gspca_dev.dev, 0),22531 /* REG_IO */,2254USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,22550, index,2256sd->gspca_dev.usb_buf, n, 500);2257if (ret < 0) {2258err("reg_w32 %02x failed %d", index, ret);2259sd->gspca_dev.usb_err = ret;2260}2261}22622263static void ov511_i2c_w(struct sd *sd, u8 reg, u8 value)2264{2265int rc, retries;22662267PDEBUG(D_USBO, "ov511_i2c_w %02x %02x", reg, value);22682269/* Three byte write cycle */2270for (retries = 6; ; ) {2271/* Select camera register */2272reg_w(sd, R51x_I2C_SADDR_3, reg);22732274/* Write "value" to I2C data port of OV511 */2275reg_w(sd, R51x_I2C_DATA, value);22762277/* Initiate 3-byte write cycle */2278reg_w(sd, R511_I2C_CTL, 0x01);22792280do {2281rc = reg_r(sd, R511_I2C_CTL);2282} while (rc > 0 && ((rc & 1) == 0)); /* Retry until idle */22832284if (rc < 0)2285return;22862287if ((rc & 2) == 0) /* Ack? */2288break;2289if (--retries < 0) {2290PDEBUG(D_USBO, "i2c write retries exhausted");2291return;2292}2293}2294}22952296static int ov511_i2c_r(struct sd *sd, u8 reg)2297{2298int rc, value, retries;22992300/* Two byte write cycle */2301for (retries = 6; ; ) {2302/* Select camera register */2303reg_w(sd, R51x_I2C_SADDR_2, reg);23042305/* Initiate 2-byte write cycle */2306reg_w(sd, R511_I2C_CTL, 0x03);23072308do {2309rc = reg_r(sd, R511_I2C_CTL);2310} while (rc > 0 && ((rc & 1) == 0)); /* Retry until idle */23112312if (rc < 0)2313return rc;23142315if ((rc & 2) == 0) /* Ack? */2316break;23172318/* I2C abort */2319reg_w(sd, R511_I2C_CTL, 0x10);23202321if (--retries < 0) {2322PDEBUG(D_USBI, "i2c write retries exhausted");2323return -1;2324}2325}23262327/* Two byte read cycle */2328for (retries = 6; ; ) {2329/* Initiate 2-byte read cycle */2330reg_w(sd, R511_I2C_CTL, 0x05);23312332do {2333rc = reg_r(sd, R511_I2C_CTL);2334} while (rc > 0 && ((rc & 1) == 0)); /* Retry until idle */23352336if (rc < 0)2337return rc;23382339if ((rc & 2) == 0) /* Ack? */2340break;23412342/* I2C abort */2343reg_w(sd, R511_I2C_CTL, 0x10);23442345if (--retries < 0) {2346PDEBUG(D_USBI, "i2c read retries exhausted");2347return -1;2348}2349}23502351value = reg_r(sd, R51x_I2C_DATA);23522353PDEBUG(D_USBI, "ov511_i2c_r %02x %02x", reg, value);23542355/* This is needed to make i2c_w() work */2356reg_w(sd, R511_I2C_CTL, 0x05);23572358return value;2359}23602361/*2362* The OV518 I2C I/O procedure is different, hence, this function.2363* This is normally only called from i2c_w(). Note that this function2364* always succeeds regardless of whether the sensor is present and working.2365*/2366static void ov518_i2c_w(struct sd *sd,2367u8 reg,2368u8 value)2369{2370PDEBUG(D_USBO, "ov518_i2c_w %02x %02x", reg, value);23712372/* Select camera register */2373reg_w(sd, R51x_I2C_SADDR_3, reg);23742375/* Write "value" to I2C data port of OV511 */2376reg_w(sd, R51x_I2C_DATA, value);23772378/* Initiate 3-byte write cycle */2379reg_w(sd, R518_I2C_CTL, 0x01);23802381/* wait for write complete */2382msleep(4);2383reg_r8(sd, R518_I2C_CTL);2384}23852386/*2387* returns: negative is error, pos or zero is data2388*2389* The OV518 I2C I/O procedure is different, hence, this function.2390* This is normally only called from i2c_r(). Note that this function2391* always succeeds regardless of whether the sensor is present and working.2392*/2393static int ov518_i2c_r(struct sd *sd, u8 reg)2394{2395int value;23962397/* Select camera register */2398reg_w(sd, R51x_I2C_SADDR_2, reg);23992400/* Initiate 2-byte write cycle */2401reg_w(sd, R518_I2C_CTL, 0x03);24022403/* Initiate 2-byte read cycle */2404reg_w(sd, R518_I2C_CTL, 0x05);2405value = reg_r(sd, R51x_I2C_DATA);2406PDEBUG(D_USBI, "ov518_i2c_r %02x %02x", reg, value);2407return value;2408}24092410static void ovfx2_i2c_w(struct sd *sd, u8 reg, u8 value)2411{2412int ret;24132414if (sd->gspca_dev.usb_err < 0)2415return;24162417ret = usb_control_msg(sd->gspca_dev.dev,2418usb_sndctrlpipe(sd->gspca_dev.dev, 0),24190x02,2420USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,2421(u16) value, (u16) reg, NULL, 0, 500);24222423if (ret < 0) {2424err("ovfx2_i2c_w %02x failed %d", reg, ret);2425sd->gspca_dev.usb_err = ret;2426}24272428PDEBUG(D_USBO, "ovfx2_i2c_w %02x %02x", reg, value);2429}24302431static int ovfx2_i2c_r(struct sd *sd, u8 reg)2432{2433int ret;24342435if (sd->gspca_dev.usb_err < 0)2436return -1;24372438ret = usb_control_msg(sd->gspca_dev.dev,2439usb_rcvctrlpipe(sd->gspca_dev.dev, 0),24400x03,2441USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,24420, (u16) reg, sd->gspca_dev.usb_buf, 1, 500);24432444if (ret >= 0) {2445ret = sd->gspca_dev.usb_buf[0];2446PDEBUG(D_USBI, "ovfx2_i2c_r %02x %02x", reg, ret);2447} else {2448err("ovfx2_i2c_r %02x failed %d", reg, ret);2449sd->gspca_dev.usb_err = ret;2450}24512452return ret;2453}24542455static void i2c_w(struct sd *sd, u8 reg, u8 value)2456{2457if (sd->sensor_reg_cache[reg] == value)2458return;24592460switch (sd->bridge) {2461case BRIDGE_OV511:2462case BRIDGE_OV511PLUS:2463ov511_i2c_w(sd, reg, value);2464break;2465case BRIDGE_OV518:2466case BRIDGE_OV518PLUS:2467case BRIDGE_OV519:2468ov518_i2c_w(sd, reg, value);2469break;2470case BRIDGE_OVFX2:2471ovfx2_i2c_w(sd, reg, value);2472break;2473case BRIDGE_W9968CF:2474w9968cf_i2c_w(sd, reg, value);2475break;2476}24772478if (sd->gspca_dev.usb_err >= 0) {2479/* Up on sensor reset empty the register cache */2480if (reg == 0x12 && (value & 0x80))2481memset(sd->sensor_reg_cache, -1,2482sizeof(sd->sensor_reg_cache));2483else2484sd->sensor_reg_cache[reg] = value;2485}2486}24872488static int i2c_r(struct sd *sd, u8 reg)2489{2490int ret = -1;24912492if (sd->sensor_reg_cache[reg] != -1)2493return sd->sensor_reg_cache[reg];24942495switch (sd->bridge) {2496case BRIDGE_OV511:2497case BRIDGE_OV511PLUS:2498ret = ov511_i2c_r(sd, reg);2499break;2500case BRIDGE_OV518:2501case BRIDGE_OV518PLUS:2502case BRIDGE_OV519:2503ret = ov518_i2c_r(sd, reg);2504break;2505case BRIDGE_OVFX2:2506ret = ovfx2_i2c_r(sd, reg);2507break;2508case BRIDGE_W9968CF:2509ret = w9968cf_i2c_r(sd, reg);2510break;2511}25122513if (ret >= 0)2514sd->sensor_reg_cache[reg] = ret;25152516return ret;2517}25182519/* Writes bits at positions specified by mask to an I2C reg. Bits that are in2520* the same position as 1's in "mask" are cleared and set to "value". Bits2521* that are in the same position as 0's in "mask" are preserved, regardless2522* of their respective state in "value".2523*/2524static void i2c_w_mask(struct sd *sd,2525u8 reg,2526u8 value,2527u8 mask)2528{2529int rc;2530u8 oldval;25312532value &= mask; /* Enforce mask on value */2533rc = i2c_r(sd, reg);2534if (rc < 0)2535return;2536oldval = rc & ~mask; /* Clear the masked bits */2537value |= oldval; /* Set the desired bits */2538i2c_w(sd, reg, value);2539}25402541/* Temporarily stops OV511 from functioning. Must do this before changing2542* registers while the camera is streaming */2543static inline void ov51x_stop(struct sd *sd)2544{2545PDEBUG(D_STREAM, "stopping");2546sd->stopped = 1;2547switch (sd->bridge) {2548case BRIDGE_OV511:2549case BRIDGE_OV511PLUS:2550reg_w(sd, R51x_SYS_RESET, 0x3d);2551break;2552case BRIDGE_OV518:2553case BRIDGE_OV518PLUS:2554reg_w_mask(sd, R51x_SYS_RESET, 0x3a, 0x3a);2555break;2556case BRIDGE_OV519:2557reg_w(sd, OV519_R51_RESET1, 0x0f);2558reg_w(sd, OV519_R51_RESET1, 0x00);2559reg_w(sd, 0x22, 0x00); /* FRAR */2560break;2561case BRIDGE_OVFX2:2562reg_w_mask(sd, 0x0f, 0x00, 0x02);2563break;2564case BRIDGE_W9968CF:2565reg_w(sd, 0x3c, 0x0a05); /* stop USB transfer */2566break;2567}2568}25692570/* Restarts OV511 after ov511_stop() is called. Has no effect if it is not2571* actually stopped (for performance). */2572static inline void ov51x_restart(struct sd *sd)2573{2574PDEBUG(D_STREAM, "restarting");2575if (!sd->stopped)2576return;2577sd->stopped = 0;25782579/* Reinitialize the stream */2580switch (sd->bridge) {2581case BRIDGE_OV511:2582case BRIDGE_OV511PLUS:2583reg_w(sd, R51x_SYS_RESET, 0x00);2584break;2585case BRIDGE_OV518:2586case BRIDGE_OV518PLUS:2587reg_w(sd, 0x2f, 0x80);2588reg_w(sd, R51x_SYS_RESET, 0x00);2589break;2590case BRIDGE_OV519:2591reg_w(sd, OV519_R51_RESET1, 0x0f);2592reg_w(sd, OV519_R51_RESET1, 0x00);2593reg_w(sd, 0x22, 0x1d); /* FRAR */2594break;2595case BRIDGE_OVFX2:2596reg_w_mask(sd, 0x0f, 0x02, 0x02);2597break;2598case BRIDGE_W9968CF:2599reg_w(sd, 0x3c, 0x8a05); /* USB FIFO enable */2600break;2601}2602}26032604static void ov51x_set_slave_ids(struct sd *sd, u8 slave);26052606/* This does an initial reset of an OmniVision sensor and ensures that I2C2607* is synchronized. Returns <0 on failure.2608*/2609static int init_ov_sensor(struct sd *sd, u8 slave)2610{2611int i;26122613ov51x_set_slave_ids(sd, slave);26142615/* Reset the sensor */2616i2c_w(sd, 0x12, 0x80);26172618/* Wait for it to initialize */2619msleep(150);26202621for (i = 0; i < i2c_detect_tries; i++) {2622if (i2c_r(sd, OV7610_REG_ID_HIGH) == 0x7f &&2623i2c_r(sd, OV7610_REG_ID_LOW) == 0xa2) {2624PDEBUG(D_PROBE, "I2C synced in %d attempt(s)", i);2625return 0;2626}26272628/* Reset the sensor */2629i2c_w(sd, 0x12, 0x80);26302631/* Wait for it to initialize */2632msleep(150);26332634/* Dummy read to sync I2C */2635if (i2c_r(sd, 0x00) < 0)2636return -1;2637}2638return -1;2639}26402641/* Set the read and write slave IDs. The "slave" argument is the write slave,2642* and the read slave will be set to (slave + 1).2643* This should not be called from outside the i2c I/O functions.2644* Sets I2C read and write slave IDs. Returns <0 for error2645*/2646static void ov51x_set_slave_ids(struct sd *sd,2647u8 slave)2648{2649switch (sd->bridge) {2650case BRIDGE_OVFX2:2651reg_w(sd, OVFX2_I2C_ADDR, slave);2652return;2653case BRIDGE_W9968CF:2654sd->sensor_addr = slave;2655return;2656}26572658reg_w(sd, R51x_I2C_W_SID, slave);2659reg_w(sd, R51x_I2C_R_SID, slave + 1);2660}26612662static void write_regvals(struct sd *sd,2663const struct ov_regvals *regvals,2664int n)2665{2666while (--n >= 0) {2667reg_w(sd, regvals->reg, regvals->val);2668regvals++;2669}2670}26712672static void write_i2c_regvals(struct sd *sd,2673const struct ov_i2c_regvals *regvals,2674int n)2675{2676while (--n >= 0) {2677i2c_w(sd, regvals->reg, regvals->val);2678regvals++;2679}2680}26812682/****************************************************************************2683*2684* OV511 and sensor configuration2685*2686***************************************************************************/26872688/* This initializes the OV2x10 / OV3610 / OV3620 */2689static void ov_hires_configure(struct sd *sd)2690{2691int high, low;26922693if (sd->bridge != BRIDGE_OVFX2) {2694err("error hires sensors only supported with ovfx2");2695return;2696}26972698PDEBUG(D_PROBE, "starting ov hires configuration");26992700/* Detect sensor (sub)type */2701high = i2c_r(sd, 0x0a);2702low = i2c_r(sd, 0x0b);2703/* info("%x, %x", high, low); */2704if (high == 0x96 && low == 0x40) {2705PDEBUG(D_PROBE, "Sensor is an OV2610");2706sd->sensor = SEN_OV2610;2707} else if (high == 0x96 && low == 0x41) {2708PDEBUG(D_PROBE, "Sensor is an OV2610AE");2709sd->sensor = SEN_OV2610AE;2710} else if (high == 0x36 && (low & 0x0f) == 0x00) {2711PDEBUG(D_PROBE, "Sensor is an OV3610");2712sd->sensor = SEN_OV3610;2713} else {2714err("Error unknown sensor type: %02x%02x",2715high, low);2716}2717}27182719/* This initializes the OV8110, OV8610 sensor. The OV8110 uses2720* the same register settings as the OV8610, since they are very similar.2721*/2722static void ov8xx0_configure(struct sd *sd)2723{2724int rc;27252726PDEBUG(D_PROBE, "starting ov8xx0 configuration");27272728/* Detect sensor (sub)type */2729rc = i2c_r(sd, OV7610_REG_COM_I);2730if (rc < 0) {2731PDEBUG(D_ERR, "Error detecting sensor type");2732return;2733}2734if ((rc & 3) == 1)2735sd->sensor = SEN_OV8610;2736else2737err("Unknown image sensor version: %d", rc & 3);2738}27392740/* This initializes the OV7610, OV7620, or OV76BE sensor. The OV76BE uses2741* the same register settings as the OV7610, since they are very similar.2742*/2743static void ov7xx0_configure(struct sd *sd)2744{2745int rc, high, low;27462747PDEBUG(D_PROBE, "starting OV7xx0 configuration");27482749/* Detect sensor (sub)type */2750rc = i2c_r(sd, OV7610_REG_COM_I);27512752/* add OV7670 here2753* it appears to be wrongly detected as a 7610 by default */2754if (rc < 0) {2755PDEBUG(D_ERR, "Error detecting sensor type");2756return;2757}2758if ((rc & 3) == 3) {2759/* quick hack to make OV7670s work */2760high = i2c_r(sd, 0x0a);2761low = i2c_r(sd, 0x0b);2762/* info("%x, %x", high, low); */2763if (high == 0x76 && (low & 0xf0) == 0x70) {2764PDEBUG(D_PROBE, "Sensor is an OV76%02x", low);2765sd->sensor = SEN_OV7670;2766} else {2767PDEBUG(D_PROBE, "Sensor is an OV7610");2768sd->sensor = SEN_OV7610;2769}2770} else if ((rc & 3) == 1) {2771/* I don't know what's different about the 76BE yet. */2772if (i2c_r(sd, 0x15) & 1) {2773PDEBUG(D_PROBE, "Sensor is an OV7620AE");2774sd->sensor = SEN_OV7620AE;2775} else {2776PDEBUG(D_PROBE, "Sensor is an OV76BE");2777sd->sensor = SEN_OV76BE;2778}2779} else if ((rc & 3) == 0) {2780/* try to read product id registers */2781high = i2c_r(sd, 0x0a);2782if (high < 0) {2783PDEBUG(D_ERR, "Error detecting camera chip PID");2784return;2785}2786low = i2c_r(sd, 0x0b);2787if (low < 0) {2788PDEBUG(D_ERR, "Error detecting camera chip VER");2789return;2790}2791if (high == 0x76) {2792switch (low) {2793case 0x30:2794err("Sensor is an OV7630/OV7635");2795err("7630 is not supported by this driver");2796return;2797case 0x40:2798PDEBUG(D_PROBE, "Sensor is an OV7645");2799sd->sensor = SEN_OV7640; /* FIXME */2800break;2801case 0x45:2802PDEBUG(D_PROBE, "Sensor is an OV7645B");2803sd->sensor = SEN_OV7640; /* FIXME */2804break;2805case 0x48:2806PDEBUG(D_PROBE, "Sensor is an OV7648");2807sd->sensor = SEN_OV7648;2808break;2809case 0x60:2810PDEBUG(D_PROBE, "Sensor is a OV7660");2811sd->sensor = SEN_OV7660;2812sd->invert_led = 0;2813break;2814default:2815PDEBUG(D_PROBE, "Unknown sensor: 0x76%x", low);2816return;2817}2818} else {2819PDEBUG(D_PROBE, "Sensor is an OV7620");2820sd->sensor = SEN_OV7620;2821}2822} else {2823err("Unknown image sensor version: %d", rc & 3);2824}2825}28262827/* This initializes the OV6620, OV6630, OV6630AE, or OV6630AF sensor. */2828static void ov6xx0_configure(struct sd *sd)2829{2830int rc;2831PDEBUG(D_PROBE, "starting OV6xx0 configuration");28322833/* Detect sensor (sub)type */2834rc = i2c_r(sd, OV7610_REG_COM_I);2835if (rc < 0) {2836PDEBUG(D_ERR, "Error detecting sensor type");2837return;2838}28392840/* Ugh. The first two bits are the version bits, but2841* the entire register value must be used. I guess OVT2842* underestimated how many variants they would make. */2843switch (rc) {2844case 0x00:2845sd->sensor = SEN_OV6630;2846warn("WARNING: Sensor is an OV66308. Your camera may have");2847warn("been misdetected in previous driver versions.");2848break;2849case 0x01:2850sd->sensor = SEN_OV6620;2851PDEBUG(D_PROBE, "Sensor is an OV6620");2852break;2853case 0x02:2854sd->sensor = SEN_OV6630;2855PDEBUG(D_PROBE, "Sensor is an OV66308AE");2856break;2857case 0x03:2858sd->sensor = SEN_OV66308AF;2859PDEBUG(D_PROBE, "Sensor is an OV66308AF");2860break;2861case 0x90:2862sd->sensor = SEN_OV6630;2863warn("WARNING: Sensor is an OV66307. Your camera may have");2864warn("been misdetected in previous driver versions.");2865break;2866default:2867err("FATAL: Unknown sensor version: 0x%02x", rc);2868return;2869}28702871/* Set sensor-specific vars */2872sd->sif = 1;2873}28742875/* Turns on or off the LED. Only has an effect with OV511+/OV518(+)/OV519 */2876static void ov51x_led_control(struct sd *sd, int on)2877{2878if (sd->invert_led)2879on = !on;28802881switch (sd->bridge) {2882/* OV511 has no LED control */2883case BRIDGE_OV511PLUS:2884reg_w(sd, R511_SYS_LED_CTL, on);2885break;2886case BRIDGE_OV518:2887case BRIDGE_OV518PLUS:2888reg_w_mask(sd, R518_GPIO_OUT, 0x02 * on, 0x02);2889break;2890case BRIDGE_OV519:2891reg_w_mask(sd, OV519_GPIO_DATA_OUT0, on, 1);2892break;2893}2894}28952896static void sd_reset_snapshot(struct gspca_dev *gspca_dev)2897{2898struct sd *sd = (struct sd *) gspca_dev;28992900if (!sd->snapshot_needs_reset)2901return;29022903/* Note it is important that we clear sd->snapshot_needs_reset,2904before actually clearing the snapshot state in the bridge2905otherwise we might race with the pkt_scan interrupt handler */2906sd->snapshot_needs_reset = 0;29072908switch (sd->bridge) {2909case BRIDGE_OV511:2910case BRIDGE_OV511PLUS:2911reg_w(sd, R51x_SYS_SNAP, 0x02);2912reg_w(sd, R51x_SYS_SNAP, 0x00);2913break;2914case BRIDGE_OV518:2915case BRIDGE_OV518PLUS:2916reg_w(sd, R51x_SYS_SNAP, 0x02); /* Reset */2917reg_w(sd, R51x_SYS_SNAP, 0x01); /* Enable */2918break;2919case BRIDGE_OV519:2920reg_w(sd, R51x_SYS_RESET, 0x40);2921reg_w(sd, R51x_SYS_RESET, 0x00);2922break;2923}2924}29252926static void ov51x_upload_quan_tables(struct sd *sd)2927{2928const unsigned char yQuanTable511[] = {29290, 1, 1, 2, 2, 3, 3, 4,29301, 1, 1, 2, 2, 3, 4, 4,29311, 1, 2, 2, 3, 4, 4, 4,29322, 2, 2, 3, 4, 4, 4, 4,29332, 2, 3, 4, 4, 5, 5, 5,29343, 3, 4, 4, 5, 5, 5, 5,29353, 4, 4, 4, 5, 5, 5, 5,29364, 4, 4, 4, 5, 5, 5, 52937};29382939const unsigned char uvQuanTable511[] = {29400, 2, 2, 3, 4, 4, 4, 4,29412, 2, 2, 4, 4, 4, 4, 4,29422, 2, 3, 4, 4, 4, 4, 4,29433, 4, 4, 4, 4, 4, 4, 4,29444, 4, 4, 4, 4, 4, 4, 4,29454, 4, 4, 4, 4, 4, 4, 4,29464, 4, 4, 4, 4, 4, 4, 4,29474, 4, 4, 4, 4, 4, 4, 42948};29492950/* OV518 quantization tables are 8x4 (instead of 8x8) */2951const unsigned char yQuanTable518[] = {29525, 4, 5, 6, 6, 7, 7, 7,29535, 5, 5, 5, 6, 7, 7, 7,29546, 6, 6, 6, 7, 7, 7, 8,29557, 7, 6, 7, 7, 7, 8, 82956};2957const unsigned char uvQuanTable518[] = {29586, 6, 6, 7, 7, 7, 7, 7,29596, 6, 6, 7, 7, 7, 7, 7,29606, 6, 6, 7, 7, 7, 7, 8,29617, 7, 7, 7, 7, 7, 8, 82962};29632964const unsigned char *pYTable, *pUVTable;2965unsigned char val0, val1;2966int i, size, reg = R51x_COMP_LUT_BEGIN;29672968PDEBUG(D_PROBE, "Uploading quantization tables");29692970if (sd->bridge == BRIDGE_OV511 || sd->bridge == BRIDGE_OV511PLUS) {2971pYTable = yQuanTable511;2972pUVTable = uvQuanTable511;2973size = 32;2974} else {2975pYTable = yQuanTable518;2976pUVTable = uvQuanTable518;2977size = 16;2978}29792980for (i = 0; i < size; i++) {2981val0 = *pYTable++;2982val1 = *pYTable++;2983val0 &= 0x0f;2984val1 &= 0x0f;2985val0 |= val1 << 4;2986reg_w(sd, reg, val0);29872988val0 = *pUVTable++;2989val1 = *pUVTable++;2990val0 &= 0x0f;2991val1 &= 0x0f;2992val0 |= val1 << 4;2993reg_w(sd, reg + size, val0);29942995reg++;2996}2997}29982999/* This initializes the OV511/OV511+ and the sensor */3000static void ov511_configure(struct gspca_dev *gspca_dev)3001{3002struct sd *sd = (struct sd *) gspca_dev;30033004/* For 511 and 511+ */3005const struct ov_regvals init_511[] = {3006{ R51x_SYS_RESET, 0x7f },3007{ R51x_SYS_INIT, 0x01 },3008{ R51x_SYS_RESET, 0x7f },3009{ R51x_SYS_INIT, 0x01 },3010{ R51x_SYS_RESET, 0x3f },3011{ R51x_SYS_INIT, 0x01 },3012{ R51x_SYS_RESET, 0x3d },3013};30143015const struct ov_regvals norm_511[] = {3016{ R511_DRAM_FLOW_CTL, 0x01 },3017{ R51x_SYS_SNAP, 0x00 },3018{ R51x_SYS_SNAP, 0x02 },3019{ R51x_SYS_SNAP, 0x00 },3020{ R511_FIFO_OPTS, 0x1f },3021{ R511_COMP_EN, 0x00 },3022{ R511_COMP_LUT_EN, 0x03 },3023};30243025const struct ov_regvals norm_511_p[] = {3026{ R511_DRAM_FLOW_CTL, 0xff },3027{ R51x_SYS_SNAP, 0x00 },3028{ R51x_SYS_SNAP, 0x02 },3029{ R51x_SYS_SNAP, 0x00 },3030{ R511_FIFO_OPTS, 0xff },3031{ R511_COMP_EN, 0x00 },3032{ R511_COMP_LUT_EN, 0x03 },3033};30343035const struct ov_regvals compress_511[] = {3036{ 0x70, 0x1f },3037{ 0x71, 0x05 },3038{ 0x72, 0x06 },3039{ 0x73, 0x06 },3040{ 0x74, 0x14 },3041{ 0x75, 0x03 },3042{ 0x76, 0x04 },3043{ 0x77, 0x04 },3044};30453046PDEBUG(D_PROBE, "Device custom id %x", reg_r(sd, R51x_SYS_CUST_ID));30473048write_regvals(sd, init_511, ARRAY_SIZE(init_511));30493050switch (sd->bridge) {3051case BRIDGE_OV511:3052write_regvals(sd, norm_511, ARRAY_SIZE(norm_511));3053break;3054case BRIDGE_OV511PLUS:3055write_regvals(sd, norm_511_p, ARRAY_SIZE(norm_511_p));3056break;3057}30583059/* Init compression */3060write_regvals(sd, compress_511, ARRAY_SIZE(compress_511));30613062ov51x_upload_quan_tables(sd);3063}30643065/* This initializes the OV518/OV518+ and the sensor */3066static void ov518_configure(struct gspca_dev *gspca_dev)3067{3068struct sd *sd = (struct sd *) gspca_dev;30693070/* For 518 and 518+ */3071const struct ov_regvals init_518[] = {3072{ R51x_SYS_RESET, 0x40 },3073{ R51x_SYS_INIT, 0xe1 },3074{ R51x_SYS_RESET, 0x3e },3075{ R51x_SYS_INIT, 0xe1 },3076{ R51x_SYS_RESET, 0x00 },3077{ R51x_SYS_INIT, 0xe1 },3078{ 0x46, 0x00 },3079{ 0x5d, 0x03 },3080};30813082const struct ov_regvals norm_518[] = {3083{ R51x_SYS_SNAP, 0x02 }, /* Reset */3084{ R51x_SYS_SNAP, 0x01 }, /* Enable */3085{ 0x31, 0x0f },3086{ 0x5d, 0x03 },3087{ 0x24, 0x9f },3088{ 0x25, 0x90 },3089{ 0x20, 0x00 },3090{ 0x51, 0x04 },3091{ 0x71, 0x19 },3092{ 0x2f, 0x80 },3093};30943095const struct ov_regvals norm_518_p[] = {3096{ R51x_SYS_SNAP, 0x02 }, /* Reset */3097{ R51x_SYS_SNAP, 0x01 }, /* Enable */3098{ 0x31, 0x0f },3099{ 0x5d, 0x03 },3100{ 0x24, 0x9f },3101{ 0x25, 0x90 },3102{ 0x20, 0x60 },3103{ 0x51, 0x02 },3104{ 0x71, 0x19 },3105{ 0x40, 0xff },3106{ 0x41, 0x42 },3107{ 0x46, 0x00 },3108{ 0x33, 0x04 },3109{ 0x21, 0x19 },3110{ 0x3f, 0x10 },3111{ 0x2f, 0x80 },3112};31133114/* First 5 bits of custom ID reg are a revision ID on OV518 */3115PDEBUG(D_PROBE, "Device revision %d",31160x1f & reg_r(sd, R51x_SYS_CUST_ID));31173118write_regvals(sd, init_518, ARRAY_SIZE(init_518));31193120/* Set LED GPIO pin to output mode */3121reg_w_mask(sd, R518_GPIO_CTL, 0x00, 0x02);31223123switch (sd->bridge) {3124case BRIDGE_OV518:3125write_regvals(sd, norm_518, ARRAY_SIZE(norm_518));3126break;3127case BRIDGE_OV518PLUS:3128write_regvals(sd, norm_518_p, ARRAY_SIZE(norm_518_p));3129break;3130}31313132ov51x_upload_quan_tables(sd);31333134reg_w(sd, 0x2f, 0x80);3135}31363137static void ov519_configure(struct sd *sd)3138{3139static const struct ov_regvals init_519[] = {3140{ 0x5a, 0x6d }, /* EnableSystem */3141{ 0x53, 0x9b }, /* don't enable the microcontroller */3142{ OV519_R54_EN_CLK1, 0xff }, /* set bit2 to enable jpeg */3143{ 0x5d, 0x03 },3144{ 0x49, 0x01 },3145{ 0x48, 0x00 },3146/* Set LED pin to output mode. Bit 4 must be cleared or sensor3147* detection will fail. This deserves further investigation. */3148{ OV519_GPIO_IO_CTRL0, 0xee },3149{ OV519_R51_RESET1, 0x0f },3150{ OV519_R51_RESET1, 0x00 },3151{ 0x22, 0x00 },3152/* windows reads 0x55 at this point*/3153};31543155write_regvals(sd, init_519, ARRAY_SIZE(init_519));3156}31573158static void ovfx2_configure(struct sd *sd)3159{3160static const struct ov_regvals init_fx2[] = {3161{ 0x00, 0x60 },3162{ 0x02, 0x01 },3163{ 0x0f, 0x1d },3164{ 0xe9, 0x82 },3165{ 0xea, 0xc7 },3166{ 0xeb, 0x10 },3167{ 0xec, 0xf6 },3168};31693170sd->stopped = 1;31713172write_regvals(sd, init_fx2, ARRAY_SIZE(init_fx2));3173}31743175/* set the mode */3176/* This function works for ov7660 only */3177static void ov519_set_mode(struct sd *sd)3178{3179static const struct ov_regvals bridge_ov7660[2][10] = {3180{{0x10, 0x14}, {0x11, 0x1e}, {0x12, 0x00}, {0x13, 0x00},3181{0x14, 0x00}, {0x15, 0x00}, {0x16, 0x00}, {0x20, 0x0c},3182{0x25, 0x01}, {0x26, 0x00}},3183{{0x10, 0x28}, {0x11, 0x3c}, {0x12, 0x00}, {0x13, 0x00},3184{0x14, 0x00}, {0x15, 0x00}, {0x16, 0x00}, {0x20, 0x0c},3185{0x25, 0x03}, {0x26, 0x00}}3186};3187static const struct ov_i2c_regvals sensor_ov7660[2][3] = {3188{{0x12, 0x00}, {0x24, 0x00}, {0x0c, 0x0c}},3189{{0x12, 0x00}, {0x04, 0x00}, {0x0c, 0x00}}3190};3191static const struct ov_i2c_regvals sensor_ov7660_2[] = {3192{OV7670_R17_HSTART, 0x13},3193{OV7670_R18_HSTOP, 0x01},3194{OV7670_R32_HREF, 0x92},3195{OV7670_R19_VSTART, 0x02},3196{OV7670_R1A_VSTOP, 0x7a},3197{OV7670_R03_VREF, 0x00},3198/* {0x33, 0x00}, */3199/* {0x34, 0x07}, */3200/* {0x36, 0x00}, */3201/* {0x6b, 0x0a}, */3202};32033204write_regvals(sd, bridge_ov7660[sd->gspca_dev.curr_mode],3205ARRAY_SIZE(bridge_ov7660[0]));3206write_i2c_regvals(sd, sensor_ov7660[sd->gspca_dev.curr_mode],3207ARRAY_SIZE(sensor_ov7660[0]));3208write_i2c_regvals(sd, sensor_ov7660_2,3209ARRAY_SIZE(sensor_ov7660_2));3210}32113212/* set the frame rate */3213/* This function works for sensors ov7640, ov7648 ov7660 and ov7670 only */3214static void ov519_set_fr(struct sd *sd)3215{3216int fr;3217u8 clock;3218/* frame rate table with indices:3219* - mode = 0: 320x240, 1: 640x4803220* - fr rate = 0: 30, 1: 25, 2: 20, 3: 15, 4: 10, 5: 53221* - reg = 0: bridge a4, 1: bridge 23, 2: sensor 11 (clock)3222*/3223static const u8 fr_tb[2][6][3] = {3224{{0x04, 0xff, 0x00},3225{0x04, 0x1f, 0x00},3226{0x04, 0x1b, 0x00},3227{0x04, 0x15, 0x00},3228{0x04, 0x09, 0x00},3229{0x04, 0x01, 0x00}},3230{{0x0c, 0xff, 0x00},3231{0x0c, 0x1f, 0x00},3232{0x0c, 0x1b, 0x00},3233{0x04, 0xff, 0x01},3234{0x04, 0x1f, 0x01},3235{0x04, 0x1b, 0x01}},3236};32373238if (frame_rate > 0)3239sd->frame_rate = frame_rate;3240if (sd->frame_rate >= 30)3241fr = 0;3242else if (sd->frame_rate >= 25)3243fr = 1;3244else if (sd->frame_rate >= 20)3245fr = 2;3246else if (sd->frame_rate >= 15)3247fr = 3;3248else if (sd->frame_rate >= 10)3249fr = 4;3250else3251fr = 5;3252reg_w(sd, 0xa4, fr_tb[sd->gspca_dev.curr_mode][fr][0]);3253reg_w(sd, 0x23, fr_tb[sd->gspca_dev.curr_mode][fr][1]);3254clock = fr_tb[sd->gspca_dev.curr_mode][fr][2];3255if (sd->sensor == SEN_OV7660)3256clock |= 0x80; /* enable double clock */3257ov518_i2c_w(sd, OV7670_R11_CLKRC, clock);3258}32593260static void setautogain(struct gspca_dev *gspca_dev)3261{3262struct sd *sd = (struct sd *) gspca_dev;32633264i2c_w_mask(sd, 0x13, sd->ctrls[AUTOGAIN].val ? 0x05 : 0x00, 0x05);3265}32663267/* this function is called at probe time */3268static int sd_config(struct gspca_dev *gspca_dev,3269const struct usb_device_id *id)3270{3271struct sd *sd = (struct sd *) gspca_dev;3272struct cam *cam = &gspca_dev->cam;32733274sd->bridge = id->driver_info & BRIDGE_MASK;3275sd->invert_led = (id->driver_info & BRIDGE_INVERT_LED) != 0;32763277switch (sd->bridge) {3278case BRIDGE_OV511:3279case BRIDGE_OV511PLUS:3280cam->cam_mode = ov511_vga_mode;3281cam->nmodes = ARRAY_SIZE(ov511_vga_mode);3282break;3283case BRIDGE_OV518:3284case BRIDGE_OV518PLUS:3285cam->cam_mode = ov518_vga_mode;3286cam->nmodes = ARRAY_SIZE(ov518_vga_mode);3287break;3288case BRIDGE_OV519:3289cam->cam_mode = ov519_vga_mode;3290cam->nmodes = ARRAY_SIZE(ov519_vga_mode);3291sd->invert_led = !sd->invert_led;3292break;3293case BRIDGE_OVFX2:3294cam->cam_mode = ov519_vga_mode;3295cam->nmodes = ARRAY_SIZE(ov519_vga_mode);3296cam->bulk_size = OVFX2_BULK_SIZE;3297cam->bulk_nurbs = MAX_NURBS;3298cam->bulk = 1;3299break;3300case BRIDGE_W9968CF:3301cam->cam_mode = w9968cf_vga_mode;3302cam->nmodes = ARRAY_SIZE(w9968cf_vga_mode);3303cam->reverse_alts = 1;3304break;3305}33063307gspca_dev->cam.ctrls = sd->ctrls;3308sd->quality = QUALITY_DEF;3309sd->frame_rate = 15;33103311return 0;3312}33133314/* this function is called at probe and resume time */3315static int sd_init(struct gspca_dev *gspca_dev)3316{3317struct sd *sd = (struct sd *) gspca_dev;3318struct cam *cam = &gspca_dev->cam;33193320switch (sd->bridge) {3321case BRIDGE_OV511:3322case BRIDGE_OV511PLUS:3323ov511_configure(gspca_dev);3324break;3325case BRIDGE_OV518:3326case BRIDGE_OV518PLUS:3327ov518_configure(gspca_dev);3328break;3329case BRIDGE_OV519:3330ov519_configure(sd);3331break;3332case BRIDGE_OVFX2:3333ovfx2_configure(sd);3334break;3335case BRIDGE_W9968CF:3336w9968cf_configure(sd);3337break;3338}33393340/* The OV519 must be more aggressive about sensor detection since3341* I2C write will never fail if the sensor is not present. We have3342* to try to initialize the sensor to detect its presence */3343sd->sensor = -1;33443345/* Test for 76xx */3346if (init_ov_sensor(sd, OV7xx0_SID) >= 0) {3347ov7xx0_configure(sd);33483349/* Test for 6xx0 */3350} else if (init_ov_sensor(sd, OV6xx0_SID) >= 0) {3351ov6xx0_configure(sd);33523353/* Test for 8xx0 */3354} else if (init_ov_sensor(sd, OV8xx0_SID) >= 0) {3355ov8xx0_configure(sd);33563357/* Test for 3xxx / 2xxx */3358} else if (init_ov_sensor(sd, OV_HIRES_SID) >= 0) {3359ov_hires_configure(sd);3360} else {3361err("Can't determine sensor slave IDs");3362goto error;3363}33643365if (sd->sensor < 0)3366goto error;33673368ov51x_led_control(sd, 0); /* turn LED off */33693370switch (sd->bridge) {3371case BRIDGE_OV511:3372case BRIDGE_OV511PLUS:3373if (sd->sif) {3374cam->cam_mode = ov511_sif_mode;3375cam->nmodes = ARRAY_SIZE(ov511_sif_mode);3376}3377break;3378case BRIDGE_OV518:3379case BRIDGE_OV518PLUS:3380if (sd->sif) {3381cam->cam_mode = ov518_sif_mode;3382cam->nmodes = ARRAY_SIZE(ov518_sif_mode);3383}3384break;3385case BRIDGE_OV519:3386if (sd->sif) {3387cam->cam_mode = ov519_sif_mode;3388cam->nmodes = ARRAY_SIZE(ov519_sif_mode);3389}3390break;3391case BRIDGE_OVFX2:3392switch (sd->sensor) {3393case SEN_OV2610:3394case SEN_OV2610AE:3395cam->cam_mode = ovfx2_ov2610_mode;3396cam->nmodes = ARRAY_SIZE(ovfx2_ov2610_mode);3397break;3398case SEN_OV3610:3399cam->cam_mode = ovfx2_ov3610_mode;3400cam->nmodes = ARRAY_SIZE(ovfx2_ov3610_mode);3401break;3402default:3403if (sd->sif) {3404cam->cam_mode = ov519_sif_mode;3405cam->nmodes = ARRAY_SIZE(ov519_sif_mode);3406}3407break;3408}3409break;3410case BRIDGE_W9968CF:3411if (sd->sif)3412cam->nmodes = ARRAY_SIZE(w9968cf_vga_mode) - 1;34133414/* w9968cf needs initialisation once the sensor is known */3415w9968cf_init(sd);3416break;3417}34183419gspca_dev->ctrl_dis = ctrl_dis[sd->sensor];34203421/* initialize the sensor */3422switch (sd->sensor) {3423case SEN_OV2610:3424write_i2c_regvals(sd, norm_2610, ARRAY_SIZE(norm_2610));34253426/* Enable autogain, autoexpo, awb, bandfilter */3427i2c_w_mask(sd, 0x13, 0x27, 0x27);3428break;3429case SEN_OV2610AE:3430write_i2c_regvals(sd, norm_2610ae, ARRAY_SIZE(norm_2610ae));34313432/* enable autoexpo */3433i2c_w_mask(sd, 0x13, 0x05, 0x05);3434break;3435case SEN_OV3610:3436write_i2c_regvals(sd, norm_3620b, ARRAY_SIZE(norm_3620b));34373438/* Enable autogain, autoexpo, awb, bandfilter */3439i2c_w_mask(sd, 0x13, 0x27, 0x27);3440break;3441case SEN_OV6620:3442write_i2c_regvals(sd, norm_6x20, ARRAY_SIZE(norm_6x20));3443break;3444case SEN_OV6630:3445case SEN_OV66308AF:3446sd->ctrls[CONTRAST].def = 200;3447/* The default is too low for the ov6630 */3448write_i2c_regvals(sd, norm_6x30, ARRAY_SIZE(norm_6x30));3449break;3450default:3451/* case SEN_OV7610: */3452/* case SEN_OV76BE: */3453write_i2c_regvals(sd, norm_7610, ARRAY_SIZE(norm_7610));3454i2c_w_mask(sd, 0x0e, 0x00, 0x40);3455break;3456case SEN_OV7620:3457case SEN_OV7620AE:3458write_i2c_regvals(sd, norm_7620, ARRAY_SIZE(norm_7620));3459break;3460case SEN_OV7640:3461case SEN_OV7648:3462write_i2c_regvals(sd, norm_7640, ARRAY_SIZE(norm_7640));3463break;3464case SEN_OV7660:3465i2c_w(sd, OV7670_R12_COM7, OV7670_COM7_RESET);3466msleep(14);3467reg_w(sd, OV519_R57_SNAPSHOT, 0x23);3468write_regvals(sd, init_519_ov7660,3469ARRAY_SIZE(init_519_ov7660));3470write_i2c_regvals(sd, norm_7660, ARRAY_SIZE(norm_7660));3471sd->gspca_dev.curr_mode = 1; /* 640x480 */3472ov519_set_mode(sd);3473ov519_set_fr(sd);3474sd->ctrls[COLORS].max = 4; /* 0..4 */3475sd->ctrls[COLORS].val =3476sd->ctrls[COLORS].def = 2;3477setcolors(gspca_dev);3478sd->ctrls[CONTRAST].max = 6; /* 0..6 */3479sd->ctrls[CONTRAST].val =3480sd->ctrls[CONTRAST].def = 3;3481setcontrast(gspca_dev);3482sd->ctrls[BRIGHTNESS].max = 6; /* 0..6 */3483sd->ctrls[BRIGHTNESS].val =3484sd->ctrls[BRIGHTNESS].def = 3;3485setbrightness(gspca_dev);3486sd_reset_snapshot(gspca_dev);3487ov51x_restart(sd);3488ov51x_stop(sd); /* not in win traces */3489ov51x_led_control(sd, 0);3490break;3491case SEN_OV7670:3492sd->ctrls[FREQ].max = 3; /* auto */3493sd->ctrls[FREQ].def = 3;3494write_i2c_regvals(sd, norm_7670, ARRAY_SIZE(norm_7670));3495break;3496case SEN_OV8610:3497write_i2c_regvals(sd, norm_8610, ARRAY_SIZE(norm_8610));3498break;3499}3500return gspca_dev->usb_err;3501error:3502PDEBUG(D_ERR, "OV519 Config failed");3503return -EINVAL;3504}35053506/* function called at start time before URB creation */3507static int sd_isoc_init(struct gspca_dev *gspca_dev)3508{3509struct sd *sd = (struct sd *) gspca_dev;35103511switch (sd->bridge) {3512case BRIDGE_OVFX2:3513if (gspca_dev->width != 800)3514gspca_dev->cam.bulk_size = OVFX2_BULK_SIZE;3515else3516gspca_dev->cam.bulk_size = 7 * 4096;3517break;3518}3519return 0;3520}35213522/* Set up the OV511/OV511+ with the given image parameters.3523*3524* Do not put any sensor-specific code in here (including I2C I/O functions)3525*/3526static void ov511_mode_init_regs(struct sd *sd)3527{3528int hsegs, vsegs, packet_size, fps, needed;3529int interlaced = 0;3530struct usb_host_interface *alt;3531struct usb_interface *intf;35323533intf = usb_ifnum_to_if(sd->gspca_dev.dev, sd->gspca_dev.iface);3534alt = usb_altnum_to_altsetting(intf, sd->gspca_dev.alt);3535if (!alt) {3536err("Couldn't get altsetting");3537sd->gspca_dev.usb_err = -EIO;3538return;3539}35403541packet_size = le16_to_cpu(alt->endpoint[0].desc.wMaxPacketSize);3542reg_w(sd, R51x_FIFO_PSIZE, packet_size >> 5);35433544reg_w(sd, R511_CAM_UV_EN, 0x01);3545reg_w(sd, R511_SNAP_UV_EN, 0x01);3546reg_w(sd, R511_SNAP_OPTS, 0x03);35473548/* Here I'm assuming that snapshot size == image size.3549* I hope that's always true. --claudio3550*/3551hsegs = (sd->gspca_dev.width >> 3) - 1;3552vsegs = (sd->gspca_dev.height >> 3) - 1;35533554reg_w(sd, R511_CAM_PXCNT, hsegs);3555reg_w(sd, R511_CAM_LNCNT, vsegs);3556reg_w(sd, R511_CAM_PXDIV, 0x00);3557reg_w(sd, R511_CAM_LNDIV, 0x00);35583559/* YUV420, low pass filter on */3560reg_w(sd, R511_CAM_OPTS, 0x03);35613562/* Snapshot additions */3563reg_w(sd, R511_SNAP_PXCNT, hsegs);3564reg_w(sd, R511_SNAP_LNCNT, vsegs);3565reg_w(sd, R511_SNAP_PXDIV, 0x00);3566reg_w(sd, R511_SNAP_LNDIV, 0x00);35673568/******** Set the framerate ********/3569if (frame_rate > 0)3570sd->frame_rate = frame_rate;35713572switch (sd->sensor) {3573case SEN_OV6620:3574/* No framerate control, doesn't like higher rates yet */3575sd->clockdiv = 3;3576break;35773578/* Note once the FIXME's in mode_init_ov_sensor_regs() are fixed3579for more sensors we need to do this for them too */3580case SEN_OV7620:3581case SEN_OV7620AE:3582case SEN_OV7640:3583case SEN_OV7648:3584case SEN_OV76BE:3585if (sd->gspca_dev.width == 320)3586interlaced = 1;3587/* Fall through */3588case SEN_OV6630:3589case SEN_OV7610:3590case SEN_OV7670:3591switch (sd->frame_rate) {3592case 30:3593case 25:3594/* Not enough bandwidth to do 640x480 @ 30 fps */3595if (sd->gspca_dev.width != 640) {3596sd->clockdiv = 0;3597break;3598}3599/* Fall through for 640x480 case */3600default:3601/* case 20: */3602/* case 15: */3603sd->clockdiv = 1;3604break;3605case 10:3606sd->clockdiv = 2;3607break;3608case 5:3609sd->clockdiv = 5;3610break;3611}3612if (interlaced) {3613sd->clockdiv = (sd->clockdiv + 1) * 2 - 1;3614/* Higher then 10 does not work */3615if (sd->clockdiv > 10)3616sd->clockdiv = 10;3617}3618break;36193620case SEN_OV8610:3621/* No framerate control ?? */3622sd->clockdiv = 0;3623break;3624}36253626/* Check if we have enough bandwidth to disable compression */3627fps = (interlaced ? 60 : 30) / (sd->clockdiv + 1) + 1;3628needed = fps * sd->gspca_dev.width * sd->gspca_dev.height * 3 / 2;3629/* 1400 is a conservative estimate of the max nr of isoc packets/sec */3630if (needed > 1400 * packet_size) {3631/* Enable Y and UV quantization and compression */3632reg_w(sd, R511_COMP_EN, 0x07);3633reg_w(sd, R511_COMP_LUT_EN, 0x03);3634} else {3635reg_w(sd, R511_COMP_EN, 0x06);3636reg_w(sd, R511_COMP_LUT_EN, 0x00);3637}36383639reg_w(sd, R51x_SYS_RESET, OV511_RESET_OMNICE);3640reg_w(sd, R51x_SYS_RESET, 0);3641}36423643/* Sets up the OV518/OV518+ with the given image parameters3644*3645* OV518 needs a completely different approach, until we can figure out what3646* the individual registers do. Also, only 15 FPS is supported now.3647*3648* Do not put any sensor-specific code in here (including I2C I/O functions)3649*/3650static void ov518_mode_init_regs(struct sd *sd)3651{3652int hsegs, vsegs, packet_size;3653struct usb_host_interface *alt;3654struct usb_interface *intf;36553656intf = usb_ifnum_to_if(sd->gspca_dev.dev, sd->gspca_dev.iface);3657alt = usb_altnum_to_altsetting(intf, sd->gspca_dev.alt);3658if (!alt) {3659err("Couldn't get altsetting");3660sd->gspca_dev.usb_err = -EIO;3661return;3662}36633664packet_size = le16_to_cpu(alt->endpoint[0].desc.wMaxPacketSize);3665ov518_reg_w32(sd, R51x_FIFO_PSIZE, packet_size & ~7, 2);36663667/******** Set the mode ********/3668reg_w(sd, 0x2b, 0);3669reg_w(sd, 0x2c, 0);3670reg_w(sd, 0x2d, 0);3671reg_w(sd, 0x2e, 0);3672reg_w(sd, 0x3b, 0);3673reg_w(sd, 0x3c, 0);3674reg_w(sd, 0x3d, 0);3675reg_w(sd, 0x3e, 0);36763677if (sd->bridge == BRIDGE_OV518) {3678/* Set 8-bit (YVYU) input format */3679reg_w_mask(sd, 0x20, 0x08, 0x08);36803681/* Set 12-bit (4:2:0) output format */3682reg_w_mask(sd, 0x28, 0x80, 0xf0);3683reg_w_mask(sd, 0x38, 0x80, 0xf0);3684} else {3685reg_w(sd, 0x28, 0x80);3686reg_w(sd, 0x38, 0x80);3687}36883689hsegs = sd->gspca_dev.width / 16;3690vsegs = sd->gspca_dev.height / 4;36913692reg_w(sd, 0x29, hsegs);3693reg_w(sd, 0x2a, vsegs);36943695reg_w(sd, 0x39, hsegs);3696reg_w(sd, 0x3a, vsegs);36973698/* Windows driver does this here; who knows why */3699reg_w(sd, 0x2f, 0x80);37003701/******** Set the framerate ********/3702sd->clockdiv = 1;37033704/* Mode independent, but framerate dependent, regs */3705/* 0x51: Clock divider; Only works on some cams which use 2 crystals */3706reg_w(sd, 0x51, 0x04);3707reg_w(sd, 0x22, 0x18);3708reg_w(sd, 0x23, 0xff);37093710if (sd->bridge == BRIDGE_OV518PLUS) {3711switch (sd->sensor) {3712case SEN_OV7620AE:3713if (sd->gspca_dev.width == 320) {3714reg_w(sd, 0x20, 0x00);3715reg_w(sd, 0x21, 0x19);3716} else {3717reg_w(sd, 0x20, 0x60);3718reg_w(sd, 0x21, 0x1f);3719}3720break;3721case SEN_OV7620:3722reg_w(sd, 0x20, 0x00);3723reg_w(sd, 0x21, 0x19);3724break;3725default:3726reg_w(sd, 0x21, 0x19);3727}3728} else3729reg_w(sd, 0x71, 0x17); /* Compression-related? */37303731/* FIXME: Sensor-specific */3732/* Bit 5 is what matters here. Of course, it is "reserved" */3733i2c_w(sd, 0x54, 0x23);37343735reg_w(sd, 0x2f, 0x80);37363737if (sd->bridge == BRIDGE_OV518PLUS) {3738reg_w(sd, 0x24, 0x94);3739reg_w(sd, 0x25, 0x90);3740ov518_reg_w32(sd, 0xc4, 400, 2); /* 190h */3741ov518_reg_w32(sd, 0xc6, 540, 2); /* 21ch */3742ov518_reg_w32(sd, 0xc7, 540, 2); /* 21ch */3743ov518_reg_w32(sd, 0xc8, 108, 2); /* 6ch */3744ov518_reg_w32(sd, 0xca, 131098, 3); /* 2001ah */3745ov518_reg_w32(sd, 0xcb, 532, 2); /* 214h */3746ov518_reg_w32(sd, 0xcc, 2400, 2); /* 960h */3747ov518_reg_w32(sd, 0xcd, 32, 2); /* 20h */3748ov518_reg_w32(sd, 0xce, 608, 2); /* 260h */3749} else {3750reg_w(sd, 0x24, 0x9f);3751reg_w(sd, 0x25, 0x90);3752ov518_reg_w32(sd, 0xc4, 400, 2); /* 190h */3753ov518_reg_w32(sd, 0xc6, 381, 2); /* 17dh */3754ov518_reg_w32(sd, 0xc7, 381, 2); /* 17dh */3755ov518_reg_w32(sd, 0xc8, 128, 2); /* 80h */3756ov518_reg_w32(sd, 0xca, 183331, 3); /* 2cc23h */3757ov518_reg_w32(sd, 0xcb, 746, 2); /* 2eah */3758ov518_reg_w32(sd, 0xcc, 1750, 2); /* 6d6h */3759ov518_reg_w32(sd, 0xcd, 45, 2); /* 2dh */3760ov518_reg_w32(sd, 0xce, 851, 2); /* 353h */3761}37623763reg_w(sd, 0x2f, 0x80);3764}37653766/* Sets up the OV519 with the given image parameters3767*3768* OV519 needs a completely different approach, until we can figure out what3769* the individual registers do.3770*3771* Do not put any sensor-specific code in here (including I2C I/O functions)3772*/3773static void ov519_mode_init_regs(struct sd *sd)3774{3775static const struct ov_regvals mode_init_519_ov7670[] = {3776{ 0x5d, 0x03 }, /* Turn off suspend mode */3777{ 0x53, 0x9f }, /* was 9b in 1.65-1.08 */3778{ OV519_R54_EN_CLK1, 0x0f }, /* bit2 (jpeg enable) */3779{ 0xa2, 0x20 }, /* a2-a5 are undocumented */3780{ 0xa3, 0x18 },3781{ 0xa4, 0x04 },3782{ 0xa5, 0x28 },3783{ 0x37, 0x00 }, /* SetUsbInit */3784{ 0x55, 0x02 }, /* 4.096 Mhz audio clock */3785/* Enable both fields, YUV Input, disable defect comp (why?) */3786{ 0x20, 0x0c },3787{ 0x21, 0x38 },3788{ 0x22, 0x1d },3789{ 0x17, 0x50 }, /* undocumented */3790{ 0x37, 0x00 }, /* undocumented */3791{ 0x40, 0xff }, /* I2C timeout counter */3792{ 0x46, 0x00 }, /* I2C clock prescaler */3793{ 0x59, 0x04 }, /* new from windrv 090403 */3794{ 0xff, 0x00 }, /* undocumented */3795/* windows reads 0x55 at this point, why? */3796};37973798static const struct ov_regvals mode_init_519[] = {3799{ 0x5d, 0x03 }, /* Turn off suspend mode */3800{ 0x53, 0x9f }, /* was 9b in 1.65-1.08 */3801{ OV519_R54_EN_CLK1, 0x0f }, /* bit2 (jpeg enable) */3802{ 0xa2, 0x20 }, /* a2-a5 are undocumented */3803{ 0xa3, 0x18 },3804{ 0xa4, 0x04 },3805{ 0xa5, 0x28 },3806{ 0x37, 0x00 }, /* SetUsbInit */3807{ 0x55, 0x02 }, /* 4.096 Mhz audio clock */3808/* Enable both fields, YUV Input, disable defect comp (why?) */3809{ 0x22, 0x1d },3810{ 0x17, 0x50 }, /* undocumented */3811{ 0x37, 0x00 }, /* undocumented */3812{ 0x40, 0xff }, /* I2C timeout counter */3813{ 0x46, 0x00 }, /* I2C clock prescaler */3814{ 0x59, 0x04 }, /* new from windrv 090403 */3815{ 0xff, 0x00 }, /* undocumented */3816/* windows reads 0x55 at this point, why? */3817};38183819/******** Set the mode ********/3820switch (sd->sensor) {3821default:3822write_regvals(sd, mode_init_519, ARRAY_SIZE(mode_init_519));3823if (sd->sensor == SEN_OV7640 ||3824sd->sensor == SEN_OV7648) {3825/* Select 8-bit input mode */3826reg_w_mask(sd, OV519_R20_DFR, 0x10, 0x10);3827}3828break;3829case SEN_OV7660:3830return; /* done by ov519_set_mode/fr() */3831case SEN_OV7670:3832write_regvals(sd, mode_init_519_ov7670,3833ARRAY_SIZE(mode_init_519_ov7670));3834break;3835}38363837reg_w(sd, OV519_R10_H_SIZE, sd->gspca_dev.width >> 4);3838reg_w(sd, OV519_R11_V_SIZE, sd->gspca_dev.height >> 3);3839if (sd->sensor == SEN_OV7670 &&3840sd->gspca_dev.cam.cam_mode[sd->gspca_dev.curr_mode].priv)3841reg_w(sd, OV519_R12_X_OFFSETL, 0x04);3842else if (sd->sensor == SEN_OV7648 &&3843sd->gspca_dev.cam.cam_mode[sd->gspca_dev.curr_mode].priv)3844reg_w(sd, OV519_R12_X_OFFSETL, 0x01);3845else3846reg_w(sd, OV519_R12_X_OFFSETL, 0x00);3847reg_w(sd, OV519_R13_X_OFFSETH, 0x00);3848reg_w(sd, OV519_R14_Y_OFFSETL, 0x00);3849reg_w(sd, OV519_R15_Y_OFFSETH, 0x00);3850reg_w(sd, OV519_R16_DIVIDER, 0x00);3851reg_w(sd, OV519_R25_FORMAT, 0x03); /* YUV422 */3852reg_w(sd, 0x26, 0x00); /* Undocumented */38533854/******** Set the framerate ********/3855if (frame_rate > 0)3856sd->frame_rate = frame_rate;38573858/* FIXME: These are only valid at the max resolution. */3859sd->clockdiv = 0;3860switch (sd->sensor) {3861case SEN_OV7640:3862case SEN_OV7648:3863switch (sd->frame_rate) {3864default:3865/* case 30: */3866reg_w(sd, 0xa4, 0x0c);3867reg_w(sd, 0x23, 0xff);3868break;3869case 25:3870reg_w(sd, 0xa4, 0x0c);3871reg_w(sd, 0x23, 0x1f);3872break;3873case 20:3874reg_w(sd, 0xa4, 0x0c);3875reg_w(sd, 0x23, 0x1b);3876break;3877case 15:3878reg_w(sd, 0xa4, 0x04);3879reg_w(sd, 0x23, 0xff);3880sd->clockdiv = 1;3881break;3882case 10:3883reg_w(sd, 0xa4, 0x04);3884reg_w(sd, 0x23, 0x1f);3885sd->clockdiv = 1;3886break;3887case 5:3888reg_w(sd, 0xa4, 0x04);3889reg_w(sd, 0x23, 0x1b);3890sd->clockdiv = 1;3891break;3892}3893break;3894case SEN_OV8610:3895switch (sd->frame_rate) {3896default: /* 15 fps */3897/* case 15: */3898reg_w(sd, 0xa4, 0x06);3899reg_w(sd, 0x23, 0xff);3900break;3901case 10:3902reg_w(sd, 0xa4, 0x06);3903reg_w(sd, 0x23, 0x1f);3904break;3905case 5:3906reg_w(sd, 0xa4, 0x06);3907reg_w(sd, 0x23, 0x1b);3908break;3909}3910break;3911case SEN_OV7670: /* guesses, based on 7640 */3912PDEBUG(D_STREAM, "Setting framerate to %d fps",3913(sd->frame_rate == 0) ? 15 : sd->frame_rate);3914reg_w(sd, 0xa4, 0x10);3915switch (sd->frame_rate) {3916case 30:3917reg_w(sd, 0x23, 0xff);3918break;3919case 20:3920reg_w(sd, 0x23, 0x1b);3921break;3922default:3923/* case 15: */3924reg_w(sd, 0x23, 0xff);3925sd->clockdiv = 1;3926break;3927}3928break;3929}3930}39313932static void mode_init_ov_sensor_regs(struct sd *sd)3933{3934struct gspca_dev *gspca_dev;3935int qvga, xstart, xend, ystart, yend;3936u8 v;39373938gspca_dev = &sd->gspca_dev;3939qvga = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv & 1;39403941/******** Mode (VGA/QVGA) and sensor specific regs ********/3942switch (sd->sensor) {3943case SEN_OV2610:3944i2c_w_mask(sd, 0x14, qvga ? 0x20 : 0x00, 0x20);3945i2c_w_mask(sd, 0x28, qvga ? 0x00 : 0x20, 0x20);3946i2c_w(sd, 0x24, qvga ? 0x20 : 0x3a);3947i2c_w(sd, 0x25, qvga ? 0x30 : 0x60);3948i2c_w_mask(sd, 0x2d, qvga ? 0x40 : 0x00, 0x40);3949i2c_w_mask(sd, 0x67, qvga ? 0xf0 : 0x90, 0xf0);3950i2c_w_mask(sd, 0x74, qvga ? 0x20 : 0x00, 0x20);3951return;3952case SEN_OV2610AE: {3953u8 v;39543955/* frame rates:3956* 10fps / 5 fps for 1600x12003957* 40fps / 20fps for 800x6003958*/3959v = 80;3960if (qvga) {3961if (sd->frame_rate < 25)3962v = 0x81;3963} else {3964if (sd->frame_rate < 10)3965v = 0x81;3966}3967i2c_w(sd, 0x11, v);3968i2c_w(sd, 0x12, qvga ? 0x60 : 0x20);3969return;3970}3971case SEN_OV3610:3972if (qvga) {3973xstart = (1040 - gspca_dev->width) / 2 + (0x1f << 4);3974ystart = (776 - gspca_dev->height) / 2;3975} else {3976xstart = (2076 - gspca_dev->width) / 2 + (0x10 << 4);3977ystart = (1544 - gspca_dev->height) / 2;3978}3979xend = xstart + gspca_dev->width;3980yend = ystart + gspca_dev->height;3981/* Writing to the COMH register resets the other windowing regs3982to their default values, so we must do this first. */3983i2c_w_mask(sd, 0x12, qvga ? 0x40 : 0x00, 0xf0);3984i2c_w_mask(sd, 0x32,3985(((xend >> 1) & 7) << 3) | ((xstart >> 1) & 7),39860x3f);3987i2c_w_mask(sd, 0x03,3988(((yend >> 1) & 3) << 2) | ((ystart >> 1) & 3),39890x0f);3990i2c_w(sd, 0x17, xstart >> 4);3991i2c_w(sd, 0x18, xend >> 4);3992i2c_w(sd, 0x19, ystart >> 3);3993i2c_w(sd, 0x1a, yend >> 3);3994return;3995case SEN_OV8610:3996/* For OV8610 qvga means qsvga */3997i2c_w_mask(sd, OV7610_REG_COM_C, qvga ? (1 << 5) : 0, 1 << 5);3998i2c_w_mask(sd, 0x13, 0x00, 0x20); /* Select 16 bit data bus */3999i2c_w_mask(sd, 0x12, 0x04, 0x06); /* AWB: 1 Test pattern: 0 */4000i2c_w_mask(sd, 0x2d, 0x00, 0x40); /* from windrv 090403 */4001i2c_w_mask(sd, 0x28, 0x20, 0x20); /* progressive mode on */4002break;4003case SEN_OV7610:4004i2c_w_mask(sd, 0x14, qvga ? 0x20 : 0x00, 0x20);4005i2c_w(sd, 0x35, qvga ? 0x1e : 0x9e);4006i2c_w_mask(sd, 0x13, 0x00, 0x20); /* Select 16 bit data bus */4007i2c_w_mask(sd, 0x12, 0x04, 0x06); /* AWB: 1 Test pattern: 0 */4008break;4009case SEN_OV7620:4010case SEN_OV7620AE:4011case SEN_OV76BE:4012i2c_w_mask(sd, 0x14, qvga ? 0x20 : 0x00, 0x20);4013i2c_w_mask(sd, 0x28, qvga ? 0x00 : 0x20, 0x20);4014i2c_w(sd, 0x24, qvga ? 0x20 : 0x3a);4015i2c_w(sd, 0x25, qvga ? 0x30 : 0x60);4016i2c_w_mask(sd, 0x2d, qvga ? 0x40 : 0x00, 0x40);4017i2c_w_mask(sd, 0x67, qvga ? 0xb0 : 0x90, 0xf0);4018i2c_w_mask(sd, 0x74, qvga ? 0x20 : 0x00, 0x20);4019i2c_w_mask(sd, 0x13, 0x00, 0x20); /* Select 16 bit data bus */4020i2c_w_mask(sd, 0x12, 0x04, 0x06); /* AWB: 1 Test pattern: 0 */4021if (sd->sensor == SEN_OV76BE)4022i2c_w(sd, 0x35, qvga ? 0x1e : 0x9e);4023break;4024case SEN_OV7640:4025case SEN_OV7648:4026i2c_w_mask(sd, 0x14, qvga ? 0x20 : 0x00, 0x20);4027i2c_w_mask(sd, 0x28, qvga ? 0x00 : 0x20, 0x20);4028/* Setting this undocumented bit in qvga mode removes a very4029annoying vertical shaking of the image */4030i2c_w_mask(sd, 0x2d, qvga ? 0x40 : 0x00, 0x40);4031/* Unknown */4032i2c_w_mask(sd, 0x67, qvga ? 0xf0 : 0x90, 0xf0);4033/* Allow higher automatic gain (to allow higher framerates) */4034i2c_w_mask(sd, 0x74, qvga ? 0x20 : 0x00, 0x20);4035i2c_w_mask(sd, 0x12, 0x04, 0x04); /* AWB: 1 */4036break;4037case SEN_OV7670:4038/* set COM7_FMT_VGA or COM7_FMT_QVGA4039* do we need to set anything else?4040* HSTART etc are set in set_ov_sensor_window itself */4041i2c_w_mask(sd, OV7670_R12_COM7,4042qvga ? OV7670_COM7_FMT_QVGA : OV7670_COM7_FMT_VGA,4043OV7670_COM7_FMT_MASK);4044i2c_w_mask(sd, 0x13, 0x00, 0x20); /* Select 16 bit data bus */4045i2c_w_mask(sd, OV7670_R13_COM8, OV7670_COM8_AWB,4046OV7670_COM8_AWB);4047if (qvga) { /* QVGA from ov7670.c by4048* Jonathan Corbet */4049xstart = 164;4050xend = 28;4051ystart = 14;4052yend = 494;4053} else { /* VGA */4054xstart = 158;4055xend = 14;4056ystart = 10;4057yend = 490;4058}4059/* OV7670 hardware window registers are split across4060* multiple locations */4061i2c_w(sd, OV7670_R17_HSTART, xstart >> 3);4062i2c_w(sd, OV7670_R18_HSTOP, xend >> 3);4063v = i2c_r(sd, OV7670_R32_HREF);4064v = (v & 0xc0) | ((xend & 0x7) << 3) | (xstart & 0x07);4065msleep(10); /* need to sleep between read and write to4066* same reg! */4067i2c_w(sd, OV7670_R32_HREF, v);40684069i2c_w(sd, OV7670_R19_VSTART, ystart >> 2);4070i2c_w(sd, OV7670_R1A_VSTOP, yend >> 2);4071v = i2c_r(sd, OV7670_R03_VREF);4072v = (v & 0xc0) | ((yend & 0x3) << 2) | (ystart & 0x03);4073msleep(10); /* need to sleep between read and write to4074* same reg! */4075i2c_w(sd, OV7670_R03_VREF, v);4076break;4077case SEN_OV6620:4078i2c_w_mask(sd, 0x14, qvga ? 0x20 : 0x00, 0x20);4079i2c_w_mask(sd, 0x13, 0x00, 0x20); /* Select 16 bit data bus */4080i2c_w_mask(sd, 0x12, 0x04, 0x06); /* AWB: 1 Test pattern: 0 */4081break;4082case SEN_OV6630:4083case SEN_OV66308AF:4084i2c_w_mask(sd, 0x14, qvga ? 0x20 : 0x00, 0x20);4085i2c_w_mask(sd, 0x12, 0x04, 0x06); /* AWB: 1 Test pattern: 0 */4086break;4087default:4088return;4089}40904091/******** Clock programming ********/4092i2c_w(sd, 0x11, sd->clockdiv);4093}40944095/* this function works for bridge ov519 and sensors ov7660 and ov7670 only */4096static void sethvflip(struct gspca_dev *gspca_dev)4097{4098struct sd *sd = (struct sd *) gspca_dev;40994100if (sd->gspca_dev.streaming)4101reg_w(sd, OV519_R51_RESET1, 0x0f); /* block stream */4102i2c_w_mask(sd, OV7670_R1E_MVFP,4103OV7670_MVFP_MIRROR * sd->ctrls[HFLIP].val4104| OV7670_MVFP_VFLIP * sd->ctrls[VFLIP].val,4105OV7670_MVFP_MIRROR | OV7670_MVFP_VFLIP);4106if (sd->gspca_dev.streaming)4107reg_w(sd, OV519_R51_RESET1, 0x00); /* restart stream */4108}41094110static void set_ov_sensor_window(struct sd *sd)4111{4112struct gspca_dev *gspca_dev;4113int qvga, crop;4114int hwsbase, hwebase, vwsbase, vwebase, hwscale, vwscale;41154116/* mode setup is fully handled in mode_init_ov_sensor_regs for these */4117switch (sd->sensor) {4118case SEN_OV2610:4119case SEN_OV2610AE:4120case SEN_OV3610:4121case SEN_OV7670:4122mode_init_ov_sensor_regs(sd);4123return;4124case SEN_OV7660:4125ov519_set_mode(sd);4126ov519_set_fr(sd);4127return;4128}41294130gspca_dev = &sd->gspca_dev;4131qvga = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv & 1;4132crop = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv & 2;41334134/* The different sensor ICs handle setting up of window differently.4135* IF YOU SET IT WRONG, YOU WILL GET ALL ZERO ISOC DATA FROM OV51x!! */4136switch (sd->sensor) {4137case SEN_OV8610:4138hwsbase = 0x1e;4139hwebase = 0x1e;4140vwsbase = 0x02;4141vwebase = 0x02;4142break;4143case SEN_OV7610:4144case SEN_OV76BE:4145hwsbase = 0x38;4146hwebase = 0x3a;4147vwsbase = vwebase = 0x05;4148break;4149case SEN_OV6620:4150case SEN_OV6630:4151case SEN_OV66308AF:4152hwsbase = 0x38;4153hwebase = 0x3a;4154vwsbase = 0x05;4155vwebase = 0x06;4156if (sd->sensor == SEN_OV66308AF && qvga)4157/* HDG: this fixes U and V getting swapped */4158hwsbase++;4159if (crop) {4160hwsbase += 8;4161hwebase += 8;4162vwsbase += 11;4163vwebase += 11;4164}4165break;4166case SEN_OV7620:4167case SEN_OV7620AE:4168hwsbase = 0x2f; /* From 7620.SET (spec is wrong) */4169hwebase = 0x2f;4170vwsbase = vwebase = 0x05;4171break;4172case SEN_OV7640:4173case SEN_OV7648:4174hwsbase = 0x1a;4175hwebase = 0x1a;4176vwsbase = vwebase = 0x03;4177break;4178default:4179return;4180}41814182switch (sd->sensor) {4183case SEN_OV6620:4184case SEN_OV6630:4185case SEN_OV66308AF:4186if (qvga) { /* QCIF */4187hwscale = 0;4188vwscale = 0;4189} else { /* CIF */4190hwscale = 1;4191vwscale = 1; /* The datasheet says 0;4192* it's wrong */4193}4194break;4195case SEN_OV8610:4196if (qvga) { /* QSVGA */4197hwscale = 1;4198vwscale = 1;4199} else { /* SVGA */4200hwscale = 2;4201vwscale = 2;4202}4203break;4204default: /* SEN_OV7xx0 */4205if (qvga) { /* QVGA */4206hwscale = 1;4207vwscale = 0;4208} else { /* VGA */4209hwscale = 2;4210vwscale = 1;4211}4212}42134214mode_init_ov_sensor_regs(sd);42154216i2c_w(sd, 0x17, hwsbase);4217i2c_w(sd, 0x18, hwebase + (sd->sensor_width >> hwscale));4218i2c_w(sd, 0x19, vwsbase);4219i2c_w(sd, 0x1a, vwebase + (sd->sensor_height >> vwscale));4220}42214222/* -- start the camera -- */4223static int sd_start(struct gspca_dev *gspca_dev)4224{4225struct sd *sd = (struct sd *) gspca_dev;42264227/* Default for most bridges, allow bridge_mode_init_regs to override */4228sd->sensor_width = sd->gspca_dev.width;4229sd->sensor_height = sd->gspca_dev.height;42304231switch (sd->bridge) {4232case BRIDGE_OV511:4233case BRIDGE_OV511PLUS:4234ov511_mode_init_regs(sd);4235break;4236case BRIDGE_OV518:4237case BRIDGE_OV518PLUS:4238ov518_mode_init_regs(sd);4239break;4240case BRIDGE_OV519:4241ov519_mode_init_regs(sd);4242break;4243/* case BRIDGE_OVFX2: nothing to do */4244case BRIDGE_W9968CF:4245w9968cf_mode_init_regs(sd);4246break;4247}42484249set_ov_sensor_window(sd);42504251if (!(sd->gspca_dev.ctrl_dis & (1 << CONTRAST)))4252setcontrast(gspca_dev);4253if (!(sd->gspca_dev.ctrl_dis & (1 << BRIGHTNESS)))4254setbrightness(gspca_dev);4255if (!(sd->gspca_dev.ctrl_dis & (1 << EXPOSURE)))4256setexposure(gspca_dev);4257if (!(sd->gspca_dev.ctrl_dis & (1 << COLORS)))4258setcolors(gspca_dev);4259if (!(sd->gspca_dev.ctrl_dis & ((1 << HFLIP) | (1 << VFLIP))))4260sethvflip(gspca_dev);4261if (!(sd->gspca_dev.ctrl_dis & (1 << AUTOBRIGHT)))4262setautobright(gspca_dev);4263if (!(sd->gspca_dev.ctrl_dis & (1 << AUTOGAIN)))4264setautogain(gspca_dev);4265if (!(sd->gspca_dev.ctrl_dis & (1 << FREQ)))4266setfreq_i(sd);42674268/* Force clear snapshot state in case the snapshot button was4269pressed while we weren't streaming */4270sd->snapshot_needs_reset = 1;4271sd_reset_snapshot(gspca_dev);42724273sd->first_frame = 3;42744275ov51x_restart(sd);4276ov51x_led_control(sd, 1);4277return gspca_dev->usb_err;4278}42794280static void sd_stopN(struct gspca_dev *gspca_dev)4281{4282struct sd *sd = (struct sd *) gspca_dev;42834284ov51x_stop(sd);4285ov51x_led_control(sd, 0);4286}42874288static void sd_stop0(struct gspca_dev *gspca_dev)4289{4290struct sd *sd = (struct sd *) gspca_dev;42914292if (!sd->gspca_dev.present)4293return;4294if (sd->bridge == BRIDGE_W9968CF)4295w9968cf_stop0(sd);42964297#if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)4298/* If the last button state is pressed, release it now! */4299if (sd->snapshot_pressed) {4300input_report_key(gspca_dev->input_dev, KEY_CAMERA, 0);4301input_sync(gspca_dev->input_dev);4302sd->snapshot_pressed = 0;4303}4304#endif4305if (sd->bridge == BRIDGE_OV519)4306reg_w(sd, OV519_R57_SNAPSHOT, 0x23);4307}43084309static void ov51x_handle_button(struct gspca_dev *gspca_dev, u8 state)4310{4311struct sd *sd = (struct sd *) gspca_dev;43124313if (sd->snapshot_pressed != state) {4314#if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)4315input_report_key(gspca_dev->input_dev, KEY_CAMERA, state);4316input_sync(gspca_dev->input_dev);4317#endif4318if (state)4319sd->snapshot_needs_reset = 1;43204321sd->snapshot_pressed = state;4322} else {4323/* On the ov511 / ov519 we need to reset the button state4324multiple times, as resetting does not work as long as the4325button stays pressed */4326switch (sd->bridge) {4327case BRIDGE_OV511:4328case BRIDGE_OV511PLUS:4329case BRIDGE_OV519:4330if (state)4331sd->snapshot_needs_reset = 1;4332break;4333}4334}4335}43364337static void ov511_pkt_scan(struct gspca_dev *gspca_dev,4338u8 *in, /* isoc packet */4339int len) /* iso packet length */4340{4341struct sd *sd = (struct sd *) gspca_dev;43424343/* SOF/EOF packets have 1st to 8th bytes zeroed and the 9th4344* byte non-zero. The EOF packet has image width/height in the4345* 10th and 11th bytes. The 9th byte is given as follows:4346*4347* bit 7: EOF4348* 6: compression enabled4349* 5: 422/420/400 modes4350* 4: 422/420/400 modes4351* 3: 14352* 2: snapshot button on4353* 1: snapshot frame4354* 0: even/odd field4355*/4356if (!(in[0] | in[1] | in[2] | in[3] | in[4] | in[5] | in[6] | in[7]) &&4357(in[8] & 0x08)) {4358ov51x_handle_button(gspca_dev, (in[8] >> 2) & 1);4359if (in[8] & 0x80) {4360/* Frame end */4361if ((in[9] + 1) * 8 != gspca_dev->width ||4362(in[10] + 1) * 8 != gspca_dev->height) {4363PDEBUG(D_ERR, "Invalid frame size, got: %dx%d,"4364" requested: %dx%d\n",4365(in[9] + 1) * 8, (in[10] + 1) * 8,4366gspca_dev->width, gspca_dev->height);4367gspca_dev->last_packet_type = DISCARD_PACKET;4368return;4369}4370/* Add 11 byte footer to frame, might be useful */4371gspca_frame_add(gspca_dev, LAST_PACKET, in, 11);4372return;4373} else {4374/* Frame start */4375gspca_frame_add(gspca_dev, FIRST_PACKET, in, 0);4376sd->packet_nr = 0;4377}4378}43794380/* Ignore the packet number */4381len--;43824383/* intermediate packet */4384gspca_frame_add(gspca_dev, INTER_PACKET, in, len);4385}43864387static void ov518_pkt_scan(struct gspca_dev *gspca_dev,4388u8 *data, /* isoc packet */4389int len) /* iso packet length */4390{4391struct sd *sd = (struct sd *) gspca_dev;43924393/* A false positive here is likely, until OVT gives me4394* the definitive SOF/EOF format */4395if ((!(data[0] | data[1] | data[2] | data[3] | data[5])) && data[6]) {4396ov51x_handle_button(gspca_dev, (data[6] >> 1) & 1);4397gspca_frame_add(gspca_dev, LAST_PACKET, NULL, 0);4398gspca_frame_add(gspca_dev, FIRST_PACKET, NULL, 0);4399sd->packet_nr = 0;4400}44014402if (gspca_dev->last_packet_type == DISCARD_PACKET)4403return;44044405/* Does this device use packet numbers ? */4406if (len & 7) {4407len--;4408if (sd->packet_nr == data[len])4409sd->packet_nr++;4410/* The last few packets of the frame (which are all 0's4411except that they may contain part of the footer), are4412numbered 0 */4413else if (sd->packet_nr == 0 || data[len]) {4414PDEBUG(D_ERR, "Invalid packet nr: %d (expect: %d)",4415(int)data[len], (int)sd->packet_nr);4416gspca_dev->last_packet_type = DISCARD_PACKET;4417return;4418}4419}44204421/* intermediate packet */4422gspca_frame_add(gspca_dev, INTER_PACKET, data, len);4423}44244425static void ov519_pkt_scan(struct gspca_dev *gspca_dev,4426u8 *data, /* isoc packet */4427int len) /* iso packet length */4428{4429/* Header of ov519 is 16 bytes:4430* Byte Value Description4431* 0 0xff magic4432* 1 0xff magic4433* 2 0xff magic4434* 3 0xXX 0x50 = SOF, 0x51 = EOF4435* 9 0xXX 0x01 initial frame without data,4436* 0x00 standard frame with image4437* 14 Lo in EOF: length of image data / 84438* 15 Hi4439*/44404441if (data[0] == 0xff && data[1] == 0xff && data[2] == 0xff) {4442switch (data[3]) {4443case 0x50: /* start of frame */4444/* Don't check the button state here, as the state4445usually (always ?) changes at EOF and checking it4446here leads to unnecessary snapshot state resets. */4447#define HDRSZ 164448data += HDRSZ;4449len -= HDRSZ;4450#undef HDRSZ4451if (data[0] == 0xff || data[1] == 0xd8)4452gspca_frame_add(gspca_dev, FIRST_PACKET,4453data, len);4454else4455gspca_dev->last_packet_type = DISCARD_PACKET;4456return;4457case 0x51: /* end of frame */4458ov51x_handle_button(gspca_dev, data[11] & 1);4459if (data[9] != 0)4460gspca_dev->last_packet_type = DISCARD_PACKET;4461gspca_frame_add(gspca_dev, LAST_PACKET,4462NULL, 0);4463return;4464}4465}44664467/* intermediate packet */4468gspca_frame_add(gspca_dev, INTER_PACKET, data, len);4469}44704471static void ovfx2_pkt_scan(struct gspca_dev *gspca_dev,4472u8 *data, /* isoc packet */4473int len) /* iso packet length */4474{4475struct sd *sd = (struct sd *) gspca_dev;44764477gspca_frame_add(gspca_dev, INTER_PACKET, data, len);44784479/* A short read signals EOF */4480if (len < gspca_dev->cam.bulk_size) {4481/* If the frame is short, and it is one of the first ones4482the sensor and bridge are still syncing, so drop it. */4483if (sd->first_frame) {4484sd->first_frame--;4485if (gspca_dev->image_len <4486sd->gspca_dev.width * sd->gspca_dev.height)4487gspca_dev->last_packet_type = DISCARD_PACKET;4488}4489gspca_frame_add(gspca_dev, LAST_PACKET, NULL, 0);4490gspca_frame_add(gspca_dev, FIRST_PACKET, NULL, 0);4491}4492}44934494static void sd_pkt_scan(struct gspca_dev *gspca_dev,4495u8 *data, /* isoc packet */4496int len) /* iso packet length */4497{4498struct sd *sd = (struct sd *) gspca_dev;44994500switch (sd->bridge) {4501case BRIDGE_OV511:4502case BRIDGE_OV511PLUS:4503ov511_pkt_scan(gspca_dev, data, len);4504break;4505case BRIDGE_OV518:4506case BRIDGE_OV518PLUS:4507ov518_pkt_scan(gspca_dev, data, len);4508break;4509case BRIDGE_OV519:4510ov519_pkt_scan(gspca_dev, data, len);4511break;4512case BRIDGE_OVFX2:4513ovfx2_pkt_scan(gspca_dev, data, len);4514break;4515case BRIDGE_W9968CF:4516w9968cf_pkt_scan(gspca_dev, data, len);4517break;4518}4519}45204521/* -- management routines -- */45224523static void setbrightness(struct gspca_dev *gspca_dev)4524{4525struct sd *sd = (struct sd *) gspca_dev;4526int val;4527static const struct ov_i2c_regvals brit_7660[][7] = {4528{{0x0f, 0x6a}, {0x24, 0x40}, {0x25, 0x2b}, {0x26, 0x90},4529{0x27, 0xe0}, {0x28, 0xe0}, {0x2c, 0xe0}},4530{{0x0f, 0x6a}, {0x24, 0x50}, {0x25, 0x40}, {0x26, 0xa1},4531{0x27, 0xc0}, {0x28, 0xc0}, {0x2c, 0xc0}},4532{{0x0f, 0x6a}, {0x24, 0x68}, {0x25, 0x58}, {0x26, 0xc2},4533{0x27, 0xa0}, {0x28, 0xa0}, {0x2c, 0xa0}},4534{{0x0f, 0x6a}, {0x24, 0x70}, {0x25, 0x68}, {0x26, 0xd3},4535{0x27, 0x80}, {0x28, 0x80}, {0x2c, 0x80}},4536{{0x0f, 0x6a}, {0x24, 0x80}, {0x25, 0x70}, {0x26, 0xd3},4537{0x27, 0x20}, {0x28, 0x20}, {0x2c, 0x20}},4538{{0x0f, 0x6a}, {0x24, 0x88}, {0x25, 0x78}, {0x26, 0xd3},4539{0x27, 0x40}, {0x28, 0x40}, {0x2c, 0x40}},4540{{0x0f, 0x6a}, {0x24, 0x90}, {0x25, 0x80}, {0x26, 0xd4},4541{0x27, 0x60}, {0x28, 0x60}, {0x2c, 0x60}}4542};45434544val = sd->ctrls[BRIGHTNESS].val;4545switch (sd->sensor) {4546case SEN_OV8610:4547case SEN_OV7610:4548case SEN_OV76BE:4549case SEN_OV6620:4550case SEN_OV6630:4551case SEN_OV66308AF:4552case SEN_OV7640:4553case SEN_OV7648:4554i2c_w(sd, OV7610_REG_BRT, val);4555break;4556case SEN_OV7620:4557case SEN_OV7620AE:4558/* 7620 doesn't like manual changes when in auto mode */4559if (!sd->ctrls[AUTOBRIGHT].val)4560i2c_w(sd, OV7610_REG_BRT, val);4561break;4562case SEN_OV7660:4563write_i2c_regvals(sd, brit_7660[val],4564ARRAY_SIZE(brit_7660[0]));4565break;4566case SEN_OV7670:4567/*win trace4568* i2c_w_mask(sd, OV7670_R13_COM8, 0, OV7670_COM8_AEC); */4569i2c_w(sd, OV7670_R55_BRIGHT, ov7670_abs_to_sm(val));4570break;4571}4572}45734574static void setcontrast(struct gspca_dev *gspca_dev)4575{4576struct sd *sd = (struct sd *) gspca_dev;4577int val;4578static const struct ov_i2c_regvals contrast_7660[][31] = {4579{{0x6c, 0xf0}, {0x6d, 0xf0}, {0x6e, 0xf8}, {0x6f, 0xa0},4580{0x70, 0x58}, {0x71, 0x38}, {0x72, 0x30}, {0x73, 0x30},4581{0x74, 0x28}, {0x75, 0x28}, {0x76, 0x24}, {0x77, 0x24},4582{0x78, 0x22}, {0x79, 0x28}, {0x7a, 0x2a}, {0x7b, 0x34},4583{0x7c, 0x0f}, {0x7d, 0x1e}, {0x7e, 0x3d}, {0x7f, 0x65},4584{0x80, 0x70}, {0x81, 0x77}, {0x82, 0x7d}, {0x83, 0x83},4585{0x84, 0x88}, {0x85, 0x8d}, {0x86, 0x96}, {0x87, 0x9f},4586{0x88, 0xb0}, {0x89, 0xc4}, {0x8a, 0xd9}},4587{{0x6c, 0xf0}, {0x6d, 0xf0}, {0x6e, 0xf8}, {0x6f, 0x94},4588{0x70, 0x58}, {0x71, 0x40}, {0x72, 0x30}, {0x73, 0x30},4589{0x74, 0x30}, {0x75, 0x30}, {0x76, 0x2c}, {0x77, 0x24},4590{0x78, 0x22}, {0x79, 0x28}, {0x7a, 0x2a}, {0x7b, 0x31},4591{0x7c, 0x0f}, {0x7d, 0x1e}, {0x7e, 0x3d}, {0x7f, 0x62},4592{0x80, 0x6d}, {0x81, 0x75}, {0x82, 0x7b}, {0x83, 0x81},4593{0x84, 0x87}, {0x85, 0x8d}, {0x86, 0x98}, {0x87, 0xa1},4594{0x88, 0xb2}, {0x89, 0xc6}, {0x8a, 0xdb}},4595{{0x6c, 0xf0}, {0x6d, 0xf0}, {0x6e, 0xf0}, {0x6f, 0x84},4596{0x70, 0x58}, {0x71, 0x48}, {0x72, 0x40}, {0x73, 0x40},4597{0x74, 0x28}, {0x75, 0x28}, {0x76, 0x28}, {0x77, 0x24},4598{0x78, 0x26}, {0x79, 0x28}, {0x7a, 0x28}, {0x7b, 0x34},4599{0x7c, 0x0f}, {0x7d, 0x1e}, {0x7e, 0x3c}, {0x7f, 0x5d},4600{0x80, 0x68}, {0x81, 0x71}, {0x82, 0x79}, {0x83, 0x81},4601{0x84, 0x86}, {0x85, 0x8b}, {0x86, 0x95}, {0x87, 0x9e},4602{0x88, 0xb1}, {0x89, 0xc5}, {0x8a, 0xd9}},4603{{0x6c, 0xf0}, {0x6d, 0xf0}, {0x6e, 0xf0}, {0x6f, 0x70},4604{0x70, 0x58}, {0x71, 0x58}, {0x72, 0x48}, {0x73, 0x48},4605{0x74, 0x38}, {0x75, 0x40}, {0x76, 0x34}, {0x77, 0x34},4606{0x78, 0x2e}, {0x79, 0x28}, {0x7a, 0x24}, {0x7b, 0x22},4607{0x7c, 0x0f}, {0x7d, 0x1e}, {0x7e, 0x3c}, {0x7f, 0x58},4608{0x80, 0x63}, {0x81, 0x6e}, {0x82, 0x77}, {0x83, 0x80},4609{0x84, 0x87}, {0x85, 0x8f}, {0x86, 0x9c}, {0x87, 0xa9},4610{0x88, 0xc0}, {0x89, 0xd4}, {0x8a, 0xe6}},4611{{0x6c, 0xa0}, {0x6d, 0xf0}, {0x6e, 0x90}, {0x6f, 0x80},4612{0x70, 0x70}, {0x71, 0x80}, {0x72, 0x60}, {0x73, 0x60},4613{0x74, 0x58}, {0x75, 0x60}, {0x76, 0x4c}, {0x77, 0x38},4614{0x78, 0x38}, {0x79, 0x2a}, {0x7a, 0x20}, {0x7b, 0x0e},4615{0x7c, 0x0a}, {0x7d, 0x14}, {0x7e, 0x26}, {0x7f, 0x46},4616{0x80, 0x54}, {0x81, 0x64}, {0x82, 0x70}, {0x83, 0x7c},4617{0x84, 0x87}, {0x85, 0x93}, {0x86, 0xa6}, {0x87, 0xb4},4618{0x88, 0xd0}, {0x89, 0xe5}, {0x8a, 0xf5}},4619{{0x6c, 0x60}, {0x6d, 0x80}, {0x6e, 0x60}, {0x6f, 0x80},4620{0x70, 0x80}, {0x71, 0x80}, {0x72, 0x88}, {0x73, 0x30},4621{0x74, 0x70}, {0x75, 0x68}, {0x76, 0x64}, {0x77, 0x50},4622{0x78, 0x3c}, {0x79, 0x22}, {0x7a, 0x10}, {0x7b, 0x08},4623{0x7c, 0x06}, {0x7d, 0x0e}, {0x7e, 0x1a}, {0x7f, 0x3a},4624{0x80, 0x4a}, {0x81, 0x5a}, {0x82, 0x6b}, {0x83, 0x7b},4625{0x84, 0x89}, {0x85, 0x96}, {0x86, 0xaf}, {0x87, 0xc3},4626{0x88, 0xe1}, {0x89, 0xf2}, {0x8a, 0xfa}},4627{{0x6c, 0x20}, {0x6d, 0x40}, {0x6e, 0x20}, {0x6f, 0x60},4628{0x70, 0x88}, {0x71, 0xc8}, {0x72, 0xc0}, {0x73, 0xb8},4629{0x74, 0xa8}, {0x75, 0xb8}, {0x76, 0x80}, {0x77, 0x5c},4630{0x78, 0x26}, {0x79, 0x10}, {0x7a, 0x08}, {0x7b, 0x04},4631{0x7c, 0x02}, {0x7d, 0x06}, {0x7e, 0x0a}, {0x7f, 0x22},4632{0x80, 0x33}, {0x81, 0x4c}, {0x82, 0x64}, {0x83, 0x7b},4633{0x84, 0x90}, {0x85, 0xa7}, {0x86, 0xc7}, {0x87, 0xde},4634{0x88, 0xf1}, {0x89, 0xf9}, {0x8a, 0xfd}},4635};46364637val = sd->ctrls[CONTRAST].val;4638switch (sd->sensor) {4639case SEN_OV7610:4640case SEN_OV6620:4641i2c_w(sd, OV7610_REG_CNT, val);4642break;4643case SEN_OV6630:4644case SEN_OV66308AF:4645i2c_w_mask(sd, OV7610_REG_CNT, val >> 4, 0x0f);4646break;4647case SEN_OV8610: {4648static const u8 ctab[] = {46490x03, 0x09, 0x0b, 0x0f, 0x53, 0x6f, 0x35, 0x7f4650};46514652/* Use Y gamma control instead. Bit 0 enables it. */4653i2c_w(sd, 0x64, ctab[val >> 5]);4654break;4655}4656case SEN_OV7620:4657case SEN_OV7620AE: {4658static const u8 ctab[] = {46590x01, 0x05, 0x09, 0x11, 0x15, 0x35, 0x37, 0x57,46600x5b, 0xa5, 0xa7, 0xc7, 0xc9, 0xcf, 0xef, 0xff4661};46624663/* Use Y gamma control instead. Bit 0 enables it. */4664i2c_w(sd, 0x64, ctab[val >> 4]);4665break;4666}4667case SEN_OV7660:4668write_i2c_regvals(sd, contrast_7660[val],4669ARRAY_SIZE(contrast_7660[0]));4670break;4671case SEN_OV7670:4672/* check that this isn't just the same as ov7610 */4673i2c_w(sd, OV7670_R56_CONTRAS, val >> 1);4674break;4675}4676}46774678static void setexposure(struct gspca_dev *gspca_dev)4679{4680struct sd *sd = (struct sd *) gspca_dev;46814682if (!sd->ctrls[AUTOGAIN].val)4683i2c_w(sd, 0x10, sd->ctrls[EXPOSURE].val);4684}46854686static void setcolors(struct gspca_dev *gspca_dev)4687{4688struct sd *sd = (struct sd *) gspca_dev;4689int val;4690static const struct ov_i2c_regvals colors_7660[][6] = {4691{{0x4f, 0x28}, {0x50, 0x2a}, {0x51, 0x02}, {0x52, 0x0a},4692{0x53, 0x19}, {0x54, 0x23}},4693{{0x4f, 0x47}, {0x50, 0x4a}, {0x51, 0x03}, {0x52, 0x11},4694{0x53, 0x2c}, {0x54, 0x3e}},4695{{0x4f, 0x66}, {0x50, 0x6b}, {0x51, 0x05}, {0x52, 0x19},4696{0x53, 0x40}, {0x54, 0x59}},4697{{0x4f, 0x84}, {0x50, 0x8b}, {0x51, 0x06}, {0x52, 0x20},4698{0x53, 0x53}, {0x54, 0x73}},4699{{0x4f, 0xa3}, {0x50, 0xab}, {0x51, 0x08}, {0x52, 0x28},4700{0x53, 0x66}, {0x54, 0x8e}},4701};47024703val = sd->ctrls[COLORS].val;4704switch (sd->sensor) {4705case SEN_OV8610:4706case SEN_OV7610:4707case SEN_OV76BE:4708case SEN_OV6620:4709case SEN_OV6630:4710case SEN_OV66308AF:4711i2c_w(sd, OV7610_REG_SAT, val);4712break;4713case SEN_OV7620:4714case SEN_OV7620AE:4715/* Use UV gamma control instead. Bits 0 & 7 are reserved. */4716/* rc = ov_i2c_write(sd->dev, 0x62, (val >> 9) & 0x7e);4717if (rc < 0)4718goto out; */4719i2c_w(sd, OV7610_REG_SAT, val);4720break;4721case SEN_OV7640:4722case SEN_OV7648:4723i2c_w(sd, OV7610_REG_SAT, val & 0xf0);4724break;4725case SEN_OV7660:4726write_i2c_regvals(sd, colors_7660[val],4727ARRAY_SIZE(colors_7660[0]));4728break;4729case SEN_OV7670:4730/* supported later once I work out how to do it4731* transparently fail now! */4732/* set REG_COM13 values for UV sat auto mode */4733break;4734}4735}47364737static void setautobright(struct gspca_dev *gspca_dev)4738{4739struct sd *sd = (struct sd *) gspca_dev;47404741i2c_w_mask(sd, 0x2d, sd->ctrls[AUTOBRIGHT].val ? 0x10 : 0x00, 0x10);4742}47434744static int sd_setautogain(struct gspca_dev *gspca_dev, __s32 val)4745{4746struct sd *sd = (struct sd *) gspca_dev;47474748sd->ctrls[AUTOGAIN].val = val;4749if (val) {4750gspca_dev->ctrl_inac |= (1 << EXPOSURE);4751} else {4752gspca_dev->ctrl_inac &= ~(1 << EXPOSURE);4753sd->ctrls[EXPOSURE].val = i2c_r(sd, 0x10);4754}4755if (gspca_dev->streaming)4756setautogain(gspca_dev);4757return gspca_dev->usb_err;4758}47594760static void setfreq_i(struct sd *sd)4761{4762if (sd->sensor == SEN_OV76604763|| sd->sensor == SEN_OV7670) {4764switch (sd->ctrls[FREQ].val) {4765case 0: /* Banding filter disabled */4766i2c_w_mask(sd, OV7670_R13_COM8, 0, OV7670_COM8_BFILT);4767break;4768case 1: /* 50 hz */4769i2c_w_mask(sd, OV7670_R13_COM8, OV7670_COM8_BFILT,4770OV7670_COM8_BFILT);4771i2c_w_mask(sd, OV7670_R3B_COM11, 0x08, 0x18);4772break;4773case 2: /* 60 hz */4774i2c_w_mask(sd, OV7670_R13_COM8, OV7670_COM8_BFILT,4775OV7670_COM8_BFILT);4776i2c_w_mask(sd, OV7670_R3B_COM11, 0x00, 0x18);4777break;4778case 3: /* Auto hz - ov7670 only */4779i2c_w_mask(sd, OV7670_R13_COM8, OV7670_COM8_BFILT,4780OV7670_COM8_BFILT);4781i2c_w_mask(sd, OV7670_R3B_COM11, OV7670_COM11_HZAUTO,47820x18);4783break;4784}4785} else {4786switch (sd->ctrls[FREQ].val) {4787case 0: /* Banding filter disabled */4788i2c_w_mask(sd, 0x2d, 0x00, 0x04);4789i2c_w_mask(sd, 0x2a, 0x00, 0x80);4790break;4791case 1: /* 50 hz (filter on and framerate adj) */4792i2c_w_mask(sd, 0x2d, 0x04, 0x04);4793i2c_w_mask(sd, 0x2a, 0x80, 0x80);4794/* 20 fps -> 16.667 fps */4795if (sd->sensor == SEN_OV6620 ||4796sd->sensor == SEN_OV6630 ||4797sd->sensor == SEN_OV66308AF)4798i2c_w(sd, 0x2b, 0x5e);4799else4800i2c_w(sd, 0x2b, 0xac);4801break;4802case 2: /* 60 hz (filter on, ...) */4803i2c_w_mask(sd, 0x2d, 0x04, 0x04);4804if (sd->sensor == SEN_OV6620 ||4805sd->sensor == SEN_OV6630 ||4806sd->sensor == SEN_OV66308AF) {4807/* 20 fps -> 15 fps */4808i2c_w_mask(sd, 0x2a, 0x80, 0x80);4809i2c_w(sd, 0x2b, 0xa8);4810} else {4811/* no framerate adj. */4812i2c_w_mask(sd, 0x2a, 0x00, 0x80);4813}4814break;4815}4816}4817}4818static void setfreq(struct gspca_dev *gspca_dev)4819{4820struct sd *sd = (struct sd *) gspca_dev;48214822setfreq_i(sd);48234824/* Ugly but necessary */4825if (sd->bridge == BRIDGE_W9968CF)4826w9968cf_set_crop_window(sd);4827}48284829static int sd_querymenu(struct gspca_dev *gspca_dev,4830struct v4l2_querymenu *menu)4831{4832struct sd *sd = (struct sd *) gspca_dev;48334834switch (menu->id) {4835case V4L2_CID_POWER_LINE_FREQUENCY:4836switch (menu->index) {4837case 0: /* V4L2_CID_POWER_LINE_FREQUENCY_DISABLED */4838strcpy((char *) menu->name, "NoFliker");4839return 0;4840case 1: /* V4L2_CID_POWER_LINE_FREQUENCY_50HZ */4841strcpy((char *) menu->name, "50 Hz");4842return 0;4843case 2: /* V4L2_CID_POWER_LINE_FREQUENCY_60HZ */4844strcpy((char *) menu->name, "60 Hz");4845return 0;4846case 3:4847if (sd->sensor != SEN_OV7670)4848return -EINVAL;48494850strcpy((char *) menu->name, "Automatic");4851return 0;4852}4853break;4854}4855return -EINVAL;4856}48574858static int sd_get_jcomp(struct gspca_dev *gspca_dev,4859struct v4l2_jpegcompression *jcomp)4860{4861struct sd *sd = (struct sd *) gspca_dev;48624863if (sd->bridge != BRIDGE_W9968CF)4864return -EINVAL;48654866memset(jcomp, 0, sizeof *jcomp);4867jcomp->quality = sd->quality;4868jcomp->jpeg_markers = V4L2_JPEG_MARKER_DHT | V4L2_JPEG_MARKER_DQT |4869V4L2_JPEG_MARKER_DRI;4870return 0;4871}48724873static int sd_set_jcomp(struct gspca_dev *gspca_dev,4874struct v4l2_jpegcompression *jcomp)4875{4876struct sd *sd = (struct sd *) gspca_dev;48774878if (sd->bridge != BRIDGE_W9968CF)4879return -EINVAL;48804881if (gspca_dev->streaming)4882return -EBUSY;48834884if (jcomp->quality < QUALITY_MIN)4885sd->quality = QUALITY_MIN;4886else if (jcomp->quality > QUALITY_MAX)4887sd->quality = QUALITY_MAX;4888else4889sd->quality = jcomp->quality;48904891/* Return resulting jcomp params to app */4892sd_get_jcomp(gspca_dev, jcomp);48934894return 0;4895}48964897/* sub-driver description */4898static const struct sd_desc sd_desc = {4899.name = MODULE_NAME,4900.ctrls = sd_ctrls,4901.nctrls = ARRAY_SIZE(sd_ctrls),4902.config = sd_config,4903.init = sd_init,4904.isoc_init = sd_isoc_init,4905.start = sd_start,4906.stopN = sd_stopN,4907.stop0 = sd_stop0,4908.pkt_scan = sd_pkt_scan,4909.dq_callback = sd_reset_snapshot,4910.querymenu = sd_querymenu,4911.get_jcomp = sd_get_jcomp,4912.set_jcomp = sd_set_jcomp,4913#if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)4914.other_input = 1,4915#endif4916};49174918/* -- module initialisation -- */4919static const struct usb_device_id device_table[] = {4920{USB_DEVICE(0x041e, 0x4003), .driver_info = BRIDGE_W9968CF },4921{USB_DEVICE(0x041e, 0x4052), .driver_info = BRIDGE_OV519 },4922{USB_DEVICE(0x041e, 0x405f), .driver_info = BRIDGE_OV519 },4923{USB_DEVICE(0x041e, 0x4060), .driver_info = BRIDGE_OV519 },4924{USB_DEVICE(0x041e, 0x4061), .driver_info = BRIDGE_OV519 },4925{USB_DEVICE(0x041e, 0x4064),4926.driver_info = BRIDGE_OV519 | BRIDGE_INVERT_LED },4927{USB_DEVICE(0x041e, 0x4067), .driver_info = BRIDGE_OV519 },4928{USB_DEVICE(0x041e, 0x4068),4929.driver_info = BRIDGE_OV519 | BRIDGE_INVERT_LED },4930{USB_DEVICE(0x045e, 0x028c), .driver_info = BRIDGE_OV519 },4931{USB_DEVICE(0x054c, 0x0154), .driver_info = BRIDGE_OV519 },4932{USB_DEVICE(0x054c, 0x0155),4933.driver_info = BRIDGE_OV519 | BRIDGE_INVERT_LED },4934{USB_DEVICE(0x05a9, 0x0511), .driver_info = BRIDGE_OV511 },4935{USB_DEVICE(0x05a9, 0x0518), .driver_info = BRIDGE_OV518 },4936{USB_DEVICE(0x05a9, 0x0519), .driver_info = BRIDGE_OV519 },4937{USB_DEVICE(0x05a9, 0x0530), .driver_info = BRIDGE_OV519 },4938{USB_DEVICE(0x05a9, 0x2800), .driver_info = BRIDGE_OVFX2 },4939{USB_DEVICE(0x05a9, 0x4519), .driver_info = BRIDGE_OV519 },4940{USB_DEVICE(0x05a9, 0x8519), .driver_info = BRIDGE_OV519 },4941{USB_DEVICE(0x05a9, 0xa511), .driver_info = BRIDGE_OV511PLUS },4942{USB_DEVICE(0x05a9, 0xa518), .driver_info = BRIDGE_OV518PLUS },4943{USB_DEVICE(0x0813, 0x0002), .driver_info = BRIDGE_OV511PLUS },4944{USB_DEVICE(0x0b62, 0x0059), .driver_info = BRIDGE_OVFX2 },4945{USB_DEVICE(0x0e96, 0xc001), .driver_info = BRIDGE_OVFX2 },4946{USB_DEVICE(0x1046, 0x9967), .driver_info = BRIDGE_W9968CF },4947{USB_DEVICE(0x8020, 0xef04), .driver_info = BRIDGE_OVFX2 },4948{}4949};49504951MODULE_DEVICE_TABLE(usb, device_table);49524953/* -- device connect -- */4954static int sd_probe(struct usb_interface *intf,4955const struct usb_device_id *id)4956{4957return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),4958THIS_MODULE);4959}49604961static struct usb_driver sd_driver = {4962.name = MODULE_NAME,4963.id_table = device_table,4964.probe = sd_probe,4965.disconnect = gspca_disconnect,4966#ifdef CONFIG_PM4967.suspend = gspca_suspend,4968.resume = gspca_resume,4969#endif4970};49714972/* -- module insert / remove -- */4973static int __init sd_mod_init(void)4974{4975return usb_register(&sd_driver);4976}4977static void __exit sd_mod_exit(void)4978{4979usb_deregister(&sd_driver);4980}49814982module_init(sd_mod_init);4983module_exit(sd_mod_exit);49844985module_param(frame_rate, int, 0644);4986MODULE_PARM_DESC(frame_rate, "Frame rate (5, 10, 15, 20 or 30 fps)");498749884989