Path: blob/master/drivers/media/video/gspca/stv06xx/stv06xx_hdcs.c
17984 views
/*1* Copyright (c) 2001 Jean-Fredric Clere, Nikolas Zimmermann, Georg Acher2* Mark Cave-Ayland, Carlo E Prelz, Dick Streefland3* Copyright (c) 2002, 2003 Tuukka Toivonen4* Copyright (c) 2008 Erik Andrén5* Copyright (c) 2008 Chia-I Wu6*7* This program is free software; you can redistribute it and/or modify8* it under the terms of the GNU General Public License as published by9* the Free Software Foundation; either version 2 of the License, or10* (at your option) any later version.11*12* This program is distributed in the hope that it will be useful,13* but WITHOUT ANY WARRANTY; without even the implied warranty of14* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the15* GNU General Public License for more details.16*17* You should have received a copy of the GNU General Public License18* along with this program; if not, write to the Free Software19* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA20*21* P/N 861037: Sensor HDCS1000 ASIC STV060022* P/N 861050-0010: Sensor HDCS1000 ASIC STV060023* P/N 861050-0020: Sensor Photobit PB100 ASIC STV0600-1 - QuickCam Express24* P/N 861055: Sensor ST VV6410 ASIC STV0610 - LEGO cam25* P/N 861075-0040: Sensor HDCS1000 ASIC26* P/N 961179-0700: Sensor ST VV6410 ASIC STV0602 - Dexxa WebCam USB27* P/N 861040-0000: Sensor ST VV6410 ASIC STV0610 - QuickCam Web28*/2930#include "stv06xx_hdcs.h"3132static const struct ctrl hdcs1x00_ctrl[] = {33{34{35.id = V4L2_CID_EXPOSURE,36.type = V4L2_CTRL_TYPE_INTEGER,37.name = "exposure",38.minimum = 0x00,39.maximum = 0xff,40.step = 0x1,41.default_value = HDCS_DEFAULT_EXPOSURE,42.flags = V4L2_CTRL_FLAG_SLIDER43},44.set = hdcs_set_exposure,45.get = hdcs_get_exposure46}, {47{48.id = V4L2_CID_GAIN,49.type = V4L2_CTRL_TYPE_INTEGER,50.name = "gain",51.minimum = 0x00,52.maximum = 0xff,53.step = 0x1,54.default_value = HDCS_DEFAULT_GAIN,55.flags = V4L2_CTRL_FLAG_SLIDER56},57.set = hdcs_set_gain,58.get = hdcs_get_gain59}60};6162static struct v4l2_pix_format hdcs1x00_mode[] = {63{64HDCS_1X00_DEF_WIDTH,65HDCS_1X00_DEF_HEIGHT,66V4L2_PIX_FMT_SGRBG8,67V4L2_FIELD_NONE,68.sizeimage =69HDCS_1X00_DEF_WIDTH * HDCS_1X00_DEF_HEIGHT,70.bytesperline = HDCS_1X00_DEF_WIDTH,71.colorspace = V4L2_COLORSPACE_SRGB,72.priv = 173}74};7576static const struct ctrl hdcs1020_ctrl[] = {77{78{79.id = V4L2_CID_EXPOSURE,80.type = V4L2_CTRL_TYPE_INTEGER,81.name = "exposure",82.minimum = 0x00,83.maximum = 0xffff,84.step = 0x1,85.default_value = HDCS_DEFAULT_EXPOSURE,86.flags = V4L2_CTRL_FLAG_SLIDER87},88.set = hdcs_set_exposure,89.get = hdcs_get_exposure90}, {91{92.id = V4L2_CID_GAIN,93.type = V4L2_CTRL_TYPE_INTEGER,94.name = "gain",95.minimum = 0x00,96.maximum = 0xff,97.step = 0x1,98.default_value = HDCS_DEFAULT_GAIN,99.flags = V4L2_CTRL_FLAG_SLIDER100},101.set = hdcs_set_gain,102.get = hdcs_get_gain103}104};105106static struct v4l2_pix_format hdcs1020_mode[] = {107{108HDCS_1020_DEF_WIDTH,109HDCS_1020_DEF_HEIGHT,110V4L2_PIX_FMT_SGRBG8,111V4L2_FIELD_NONE,112.sizeimage =113HDCS_1020_DEF_WIDTH * HDCS_1020_DEF_HEIGHT,114.bytesperline = HDCS_1020_DEF_WIDTH,115.colorspace = V4L2_COLORSPACE_SRGB,116.priv = 1117}118};119120enum hdcs_power_state {121HDCS_STATE_SLEEP,122HDCS_STATE_IDLE,123HDCS_STATE_RUN124};125126/* no lock? */127struct hdcs {128enum hdcs_power_state state;129int w, h;130131/* visible area of the sensor array */132struct {133int left, top;134int width, height;135int border;136} array;137138struct {139/* Column timing overhead */140u8 cto;141/* Column processing overhead */142u8 cpo;143/* Row sample period constant */144u16 rs;145/* Exposure reset duration */146u16 er;147} exp;148149int psmp;150u8 exp_cache, gain_cache;151};152153static int hdcs_reg_write_seq(struct sd *sd, u8 reg, u8 *vals, u8 len)154{155u8 regs[I2C_MAX_BYTES * 2];156int i;157158if (unlikely((len <= 0) || (len >= I2C_MAX_BYTES) ||159(reg + len > 0xff)))160return -EINVAL;161162for (i = 0; i < len; i++) {163regs[2 * i] = reg;164regs[2 * i + 1] = vals[i];165/* All addresses are shifted left one bit166* as bit 0 toggles r/w */167reg += 2;168}169170return stv06xx_write_sensor_bytes(sd, regs, len);171}172173static int hdcs_set_state(struct sd *sd, enum hdcs_power_state state)174{175struct hdcs *hdcs = sd->sensor_priv;176u8 val;177int ret;178179if (hdcs->state == state)180return 0;181182/* we need to go idle before running or sleeping */183if (hdcs->state != HDCS_STATE_IDLE) {184ret = stv06xx_write_sensor(sd, HDCS_REG_CONTROL(sd), 0);185if (ret)186return ret;187}188189hdcs->state = HDCS_STATE_IDLE;190191if (state == HDCS_STATE_IDLE)192return 0;193194switch (state) {195case HDCS_STATE_SLEEP:196val = HDCS_SLEEP_MODE;197break;198199case HDCS_STATE_RUN:200val = HDCS_RUN_ENABLE;201break;202203default:204return -EINVAL;205}206207ret = stv06xx_write_sensor(sd, HDCS_REG_CONTROL(sd), val);208209/* Update the state if the write succeeded */210if (!ret)211hdcs->state = state;212213return ret;214}215216static int hdcs_reset(struct sd *sd)217{218struct hdcs *hdcs = sd->sensor_priv;219int err;220221err = stv06xx_write_sensor(sd, HDCS_REG_CONTROL(sd), 1);222if (err < 0)223return err;224225err = stv06xx_write_sensor(sd, HDCS_REG_CONTROL(sd), 0);226if (err < 0)227hdcs->state = HDCS_STATE_IDLE;228229return err;230}231232static int hdcs_get_exposure(struct gspca_dev *gspca_dev, __s32 *val)233{234struct sd *sd = (struct sd *) gspca_dev;235struct hdcs *hdcs = sd->sensor_priv;236237*val = hdcs->exp_cache;238239return 0;240}241242static int hdcs_set_exposure(struct gspca_dev *gspca_dev, __s32 val)243{244struct sd *sd = (struct sd *) gspca_dev;245struct hdcs *hdcs = sd->sensor_priv;246int rowexp, srowexp;247int max_srowexp;248/* Column time period */249int ct;250/* Column processing period */251int cp;252/* Row processing period */253int rp;254/* Minimum number of column timing periods255within the column processing period */256int mnct;257int cycles, err;258u8 exp[14];259260val &= 0xff;261hdcs->exp_cache = val;262263cycles = val * HDCS_CLK_FREQ_MHZ * 257;264265ct = hdcs->exp.cto + hdcs->psmp + (HDCS_ADC_START_SIG_DUR + 2);266cp = hdcs->exp.cto + (hdcs->w * ct / 2);267268/* the cycles one row takes */269rp = hdcs->exp.rs + cp;270271rowexp = cycles / rp;272273/* the remaining cycles */274cycles -= rowexp * rp;275276/* calculate sub-row exposure */277if (IS_1020(sd)) {278/* see HDCS-1020 datasheet 3.5.6.4, p. 63 */279srowexp = hdcs->w - (cycles + hdcs->exp.er + 13) / ct;280281mnct = (hdcs->exp.er + 12 + ct - 1) / ct;282max_srowexp = hdcs->w - mnct;283} else {284/* see HDCS-1000 datasheet 3.4.5.5, p. 61 */285srowexp = cp - hdcs->exp.er - 6 - cycles;286287mnct = (hdcs->exp.er + 5 + ct - 1) / ct;288max_srowexp = cp - mnct * ct - 1;289}290291if (srowexp < 0)292srowexp = 0;293else if (srowexp > max_srowexp)294srowexp = max_srowexp;295296if (IS_1020(sd)) {297exp[0] = HDCS20_CONTROL;298exp[1] = 0x00; /* Stop streaming */299exp[2] = HDCS_ROWEXPL;300exp[3] = rowexp & 0xff;301exp[4] = HDCS_ROWEXPH;302exp[5] = rowexp >> 8;303exp[6] = HDCS20_SROWEXP;304exp[7] = (srowexp >> 2) & 0xff;305exp[8] = HDCS20_ERROR;306exp[9] = 0x10; /* Clear exposure error flag*/307exp[10] = HDCS20_CONTROL;308exp[11] = 0x04; /* Restart streaming */309err = stv06xx_write_sensor_bytes(sd, exp, 6);310} else {311exp[0] = HDCS00_CONTROL;312exp[1] = 0x00; /* Stop streaming */313exp[2] = HDCS_ROWEXPL;314exp[3] = rowexp & 0xff;315exp[4] = HDCS_ROWEXPH;316exp[5] = rowexp >> 8;317exp[6] = HDCS00_SROWEXPL;318exp[7] = srowexp & 0xff;319exp[8] = HDCS00_SROWEXPH;320exp[9] = srowexp >> 8;321exp[10] = HDCS_STATUS;322exp[11] = 0x10; /* Clear exposure error flag*/323exp[12] = HDCS00_CONTROL;324exp[13] = 0x04; /* Restart streaming */325err = stv06xx_write_sensor_bytes(sd, exp, 7);326if (err < 0)327return err;328}329PDEBUG(D_V4L2, "Writing exposure %d, rowexp %d, srowexp %d",330val, rowexp, srowexp);331return err;332}333334static int hdcs_set_gains(struct sd *sd, u8 g)335{336struct hdcs *hdcs = sd->sensor_priv;337int err;338u8 gains[4];339340hdcs->gain_cache = g;341342/* the voltage gain Av = (1 + 19 * val / 127) * (1 + bit7) */343if (g > 127)344g = 0x80 | (g / 2);345346gains[0] = g;347gains[1] = g;348gains[2] = g;349gains[3] = g;350351err = hdcs_reg_write_seq(sd, HDCS_ERECPGA, gains, 4);352return err;353}354355static int hdcs_get_gain(struct gspca_dev *gspca_dev, __s32 *val)356{357struct sd *sd = (struct sd *) gspca_dev;358struct hdcs *hdcs = sd->sensor_priv;359360*val = hdcs->gain_cache;361362return 0;363}364365static int hdcs_set_gain(struct gspca_dev *gspca_dev, __s32 val)366{367PDEBUG(D_V4L2, "Writing gain %d", val);368return hdcs_set_gains((struct sd *) gspca_dev,369val & 0xff);370}371372static int hdcs_set_size(struct sd *sd,373unsigned int width, unsigned int height)374{375struct hdcs *hdcs = sd->sensor_priv;376u8 win[4];377unsigned int x, y;378int err;379380/* must be multiple of 4 */381width = (width + 3) & ~0x3;382height = (height + 3) & ~0x3;383384if (width > hdcs->array.width)385width = hdcs->array.width;386387if (IS_1020(sd)) {388/* the borders are also invalid */389if (height + 2 * hdcs->array.border + HDCS_1020_BOTTOM_Y_SKIP390> hdcs->array.height)391height = hdcs->array.height - 2 * hdcs->array.border -392HDCS_1020_BOTTOM_Y_SKIP;393394y = (hdcs->array.height - HDCS_1020_BOTTOM_Y_SKIP - height) / 2395+ hdcs->array.top;396} else {397if (height > hdcs->array.height)398height = hdcs->array.height;399400y = hdcs->array.top + (hdcs->array.height - height) / 2;401}402403x = hdcs->array.left + (hdcs->array.width - width) / 2;404405win[0] = y / 4;406win[1] = x / 4;407win[2] = (y + height) / 4 - 1;408win[3] = (x + width) / 4 - 1;409410err = hdcs_reg_write_seq(sd, HDCS_FWROW, win, 4);411if (err < 0)412return err;413414/* Update the current width and height */415hdcs->w = width;416hdcs->h = height;417return err;418}419420static int hdcs_probe_1x00(struct sd *sd)421{422struct hdcs *hdcs;423u16 sensor;424int ret;425426ret = stv06xx_read_sensor(sd, HDCS_IDENT, &sensor);427if (ret < 0 || sensor != 0x08)428return -ENODEV;429430info("HDCS-1000/1100 sensor detected");431432sd->gspca_dev.cam.cam_mode = hdcs1x00_mode;433sd->gspca_dev.cam.nmodes = ARRAY_SIZE(hdcs1x00_mode);434sd->desc.ctrls = hdcs1x00_ctrl;435sd->desc.nctrls = ARRAY_SIZE(hdcs1x00_ctrl);436437hdcs = kmalloc(sizeof(struct hdcs), GFP_KERNEL);438if (!hdcs)439return -ENOMEM;440441hdcs->array.left = 8;442hdcs->array.top = 8;443hdcs->array.width = HDCS_1X00_DEF_WIDTH;444hdcs->array.height = HDCS_1X00_DEF_HEIGHT;445hdcs->array.border = 4;446447hdcs->exp.cto = 4;448hdcs->exp.cpo = 2;449hdcs->exp.rs = 186;450hdcs->exp.er = 100;451452/*453* Frame rate on HDCS-1000 with STV600 depends on PSMP:454* 4 = doesn't work at all455* 5 = 7.8 fps,456* 6 = 6.9 fps,457* 8 = 6.3 fps,458* 10 = 5.5 fps,459* 15 = 4.4 fps,460* 31 = 2.8 fps461*462* Frame rate on HDCS-1000 with STV602 depends on PSMP:463* 15 = doesn't work at all464* 18 = doesn't work at all465* 19 = 7.3 fps466* 20 = 7.4 fps467* 21 = 7.4 fps468* 22 = 7.4 fps469* 24 = 6.3 fps470* 30 = 5.4 fps471*/472hdcs->psmp = (sd->bridge == BRIDGE_STV602) ? 20 : 5;473474sd->sensor_priv = hdcs;475476return 0;477}478479static int hdcs_probe_1020(struct sd *sd)480{481struct hdcs *hdcs;482u16 sensor;483int ret;484485ret = stv06xx_read_sensor(sd, HDCS_IDENT, &sensor);486if (ret < 0 || sensor != 0x10)487return -ENODEV;488489info("HDCS-1020 sensor detected");490491sd->gspca_dev.cam.cam_mode = hdcs1020_mode;492sd->gspca_dev.cam.nmodes = ARRAY_SIZE(hdcs1020_mode);493sd->desc.ctrls = hdcs1020_ctrl;494sd->desc.nctrls = ARRAY_SIZE(hdcs1020_ctrl);495496hdcs = kmalloc(sizeof(struct hdcs), GFP_KERNEL);497if (!hdcs)498return -ENOMEM;499500/*501* From Andrey's test image: looks like HDCS-1020 upper-left502* visible pixel is at 24,8 (y maybe even smaller?) and lower-right503* visible pixel at 375,299 (x maybe even larger?)504*/505hdcs->array.left = 24;506hdcs->array.top = 4;507hdcs->array.width = HDCS_1020_DEF_WIDTH;508hdcs->array.height = 304;509hdcs->array.border = 4;510511hdcs->psmp = 6;512513hdcs->exp.cto = 3;514hdcs->exp.cpo = 3;515hdcs->exp.rs = 155;516hdcs->exp.er = 96;517518sd->sensor_priv = hdcs;519520return 0;521}522523static int hdcs_start(struct sd *sd)524{525PDEBUG(D_STREAM, "Starting stream");526527return hdcs_set_state(sd, HDCS_STATE_RUN);528}529530static int hdcs_stop(struct sd *sd)531{532PDEBUG(D_STREAM, "Halting stream");533534return hdcs_set_state(sd, HDCS_STATE_SLEEP);535}536537static void hdcs_disconnect(struct sd *sd)538{539PDEBUG(D_PROBE, "Disconnecting the sensor");540kfree(sd->sensor_priv);541}542543static int hdcs_init(struct sd *sd)544{545struct hdcs *hdcs = sd->sensor_priv;546int i, err = 0;547548/* Set the STV0602AA in STV0600 emulation mode */549if (sd->bridge == BRIDGE_STV602)550stv06xx_write_bridge(sd, STV_STV0600_EMULATION, 1);551552/* Execute the bridge init */553for (i = 0; i < ARRAY_SIZE(stv_bridge_init) && !err; i++) {554err = stv06xx_write_bridge(sd, stv_bridge_init[i][0],555stv_bridge_init[i][1]);556}557if (err < 0)558return err;559560/* sensor soft reset */561hdcs_reset(sd);562563/* Execute the sensor init */564for (i = 0; i < ARRAY_SIZE(stv_sensor_init) && !err; i++) {565err = stv06xx_write_sensor(sd, stv_sensor_init[i][0],566stv_sensor_init[i][1]);567}568if (err < 0)569return err;570571/* Enable continuous frame capture, bit 2: stop when frame complete */572err = stv06xx_write_sensor(sd, HDCS_REG_CONFIG(sd), BIT(3));573if (err < 0)574return err;575576/* Set PGA sample duration577(was 0x7E for the STV602, but caused slow framerate with HDCS-1020) */578if (IS_1020(sd))579err = stv06xx_write_sensor(sd, HDCS_TCTRL,580(HDCS_ADC_START_SIG_DUR << 6) | hdcs->psmp);581else582err = stv06xx_write_sensor(sd, HDCS_TCTRL,583(HDCS_ADC_START_SIG_DUR << 5) | hdcs->psmp);584if (err < 0)585return err;586587err = hdcs_set_gains(sd, HDCS_DEFAULT_GAIN);588if (err < 0)589return err;590591err = hdcs_set_size(sd, hdcs->array.width, hdcs->array.height);592if (err < 0)593return err;594595err = hdcs_set_exposure(&sd->gspca_dev, HDCS_DEFAULT_EXPOSURE);596return err;597}598599static int hdcs_dump(struct sd *sd)600{601u16 reg, val;602603info("Dumping sensor registers:");604605for (reg = HDCS_IDENT; reg <= HDCS_ROWEXPH; reg++) {606stv06xx_read_sensor(sd, reg, &val);607info("reg 0x%02x = 0x%02x", reg, val);608}609return 0;610}611612613