Path: blob/master/drivers/media/video/ivtv/ivtv-i2c.c
17751 views
/*1I2C functions2Copyright (C) 2003-2004 Kevin Thayer <nufan_wfk at yahoo.com>3Copyright (C) 2005-2007 Hans Verkuil <[email protected]>45This program is free software; you can redistribute it and/or modify6it under the terms of the GNU General Public License as published by7the Free Software Foundation; either version 2 of the License, or8(at your option) any later version.910This program is distributed in the hope that it will be useful,11but WITHOUT ANY WARRANTY; without even the implied warranty of12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the13GNU General Public License for more details.1415You should have received a copy of the GNU General Public License16along with this program; if not, write to the Free Software17Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA18*/1920/*21This file includes an i2c implementation that was reverse engineered22from the Hauppauge windows driver. Older ivtv versions used i2c-algo-bit,23which whilst fine under most circumstances, had trouble with the Zilog24CPU on the PVR-150 which handles IR functions (occasional inability to25communicate with the chip until it was reset) and also with the i2c26bus being completely unreachable when multiple PVR cards were present.2728The implementation is very similar to i2c-algo-bit, but there are enough29subtle differences that the two are hard to merge. The general strategy30employed by i2c-algo-bit is to use udelay() to implement the timing31when putting out bits on the scl/sda lines. The general strategy taken32here is to poll the lines for state changes (see ivtv_waitscl and33ivtv_waitsda). In addition there are small delays at various locations34which poll the SCL line 5 times (ivtv_scldelay). I would guess that35since this is memory mapped I/O that the length of those delays is tied36to the PCI bus clock. There is some extra code to do with recovery37and retries. Since it is not known what causes the actual i2c problems38in the first place, the only goal if one was to attempt to use39i2c-algo-bit would be to try to make it follow the same code path.40This would be a lot of work, and I'm also not convinced that it would41provide a generic benefit to i2c-algo-bit. Therefore consider this42an engineering solution -- not pretty, but it works.4344Some more general comments about what we are doing:4546The i2c bus is a 2 wire serial bus, with clock (SCL) and data (SDA)47lines. To communicate on the bus (as a master, we don't act as a slave),48we first initiate a start condition (ivtv_start). We then write the49address of the device that we want to communicate with, along with a flag50that indicates whether this is a read or a write. The slave then issues51an ACK signal (ivtv_ack), which tells us that it is ready for reading /52writing. We then proceed with reading or writing (ivtv_read/ivtv_write),53and finally issue a stop condition (ivtv_stop) to make the bus available54to other masters.5556There is an additional form of transaction where a write may be57immediately followed by a read. In this case, there is no intervening58stop condition. (Only the msp3400 chip uses this method of data transfer).59*/6061#include "ivtv-driver.h"62#include "ivtv-cards.h"63#include "ivtv-gpio.h"64#include "ivtv-i2c.h"65#include <media/cx25840.h>6667/* i2c implementation for cx23415/6 chip, ivtv project.68* Author: Kevin Thayer (nufan_wfk at yahoo.com)69*/70/* i2c stuff */71#define IVTV_REG_I2C_SETSCL_OFFSET 0x700072#define IVTV_REG_I2C_SETSDA_OFFSET 0x700473#define IVTV_REG_I2C_GETSCL_OFFSET 0x700874#define IVTV_REG_I2C_GETSDA_OFFSET 0x700c7576#define IVTV_CS53L32A_I2C_ADDR 0x1177#define IVTV_M52790_I2C_ADDR 0x4878#define IVTV_CX25840_I2C_ADDR 0x4479#define IVTV_SAA7115_I2C_ADDR 0x2180#define IVTV_SAA7127_I2C_ADDR 0x4481#define IVTV_SAA717x_I2C_ADDR 0x2182#define IVTV_MSP3400_I2C_ADDR 0x4083#define IVTV_HAUPPAUGE_I2C_ADDR 0x5084#define IVTV_WM8739_I2C_ADDR 0x1a85#define IVTV_WM8775_I2C_ADDR 0x1b86#define IVTV_TEA5767_I2C_ADDR 0x6087#define IVTV_UPD64031A_I2C_ADDR 0x1288#define IVTV_UPD64083_I2C_ADDR 0x5c89#define IVTV_VP27SMPX_I2C_ADDR 0x5b90#define IVTV_M52790_I2C_ADDR 0x4891#define IVTV_AVERMEDIA_IR_RX_I2C_ADDR 0x4092#define IVTV_HAUP_EXT_IR_RX_I2C_ADDR 0x1a93#define IVTV_HAUP_INT_IR_RX_I2C_ADDR 0x1894#define IVTV_Z8F0811_IR_TX_I2C_ADDR 0x7095#define IVTV_Z8F0811_IR_RX_I2C_ADDR 0x7196#define IVTV_ADAPTEC_IR_ADDR 0x6b9798/* This array should match the IVTV_HW_ defines */99static const u8 hw_addrs[] = {100IVTV_CX25840_I2C_ADDR,101IVTV_SAA7115_I2C_ADDR,102IVTV_SAA7127_I2C_ADDR,103IVTV_MSP3400_I2C_ADDR,1040,105IVTV_WM8775_I2C_ADDR,106IVTV_CS53L32A_I2C_ADDR,1070,108IVTV_SAA7115_I2C_ADDR,109IVTV_UPD64031A_I2C_ADDR,110IVTV_UPD64083_I2C_ADDR,111IVTV_SAA717x_I2C_ADDR,112IVTV_WM8739_I2C_ADDR,113IVTV_VP27SMPX_I2C_ADDR,114IVTV_M52790_I2C_ADDR,1150, /* IVTV_HW_GPIO dummy driver ID */116IVTV_AVERMEDIA_IR_RX_I2C_ADDR, /* IVTV_HW_I2C_IR_RX_AVER */117IVTV_HAUP_EXT_IR_RX_I2C_ADDR, /* IVTV_HW_I2C_IR_RX_HAUP_EXT */118IVTV_HAUP_INT_IR_RX_I2C_ADDR, /* IVTV_HW_I2C_IR_RX_HAUP_INT */119IVTV_Z8F0811_IR_TX_I2C_ADDR, /* IVTV_HW_Z8F0811_IR_TX_HAUP */120IVTV_Z8F0811_IR_RX_I2C_ADDR, /* IVTV_HW_Z8F0811_IR_RX_HAUP */121IVTV_ADAPTEC_IR_ADDR, /* IVTV_HW_I2C_IR_RX_ADAPTEC */122};123124/* This array should match the IVTV_HW_ defines */125static const char * const hw_devicenames[] = {126"cx25840",127"saa7115",128"saa7127_auto", /* saa7127 or saa7129 */129"msp3400",130"tuner",131"wm8775",132"cs53l32a",133"tveeprom",134"saa7114",135"upd64031a",136"upd64083",137"saa717x",138"wm8739",139"vp27smpx",140"m52790",141"gpio",142"ir_video", /* IVTV_HW_I2C_IR_RX_AVER */143"ir_video", /* IVTV_HW_I2C_IR_RX_HAUP_EXT */144"ir_video", /* IVTV_HW_I2C_IR_RX_HAUP_INT */145"ir_tx_z8f0811_haup", /* IVTV_HW_Z8F0811_IR_TX_HAUP */146"ir_rx_z8f0811_haup", /* IVTV_HW_Z8F0811_IR_RX_HAUP */147"ir_video", /* IVTV_HW_I2C_IR_RX_ADAPTEC */148};149150static int get_key_adaptec(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)151{152unsigned char keybuf[4];153154keybuf[0] = 0x00;155i2c_master_send(ir->c, keybuf, 1);156/* poll IR chip */157if (i2c_master_recv(ir->c, keybuf, sizeof(keybuf)) != sizeof(keybuf)) {158return 0;159}160161/* key pressed ? */162if (keybuf[2] == 0xff)163return 0;164165/* remove repeat bit */166keybuf[2] &= 0x7f;167keybuf[3] |= 0x80;168169*ir_key = keybuf[3] | keybuf[2] << 8 | keybuf[1] << 16 |keybuf[0] << 24;170*ir_raw = *ir_key;171172return 1;173}174175static int ivtv_i2c_new_ir(struct ivtv *itv, u32 hw, const char *type, u8 addr)176{177struct i2c_board_info info;178struct i2c_adapter *adap = &itv->i2c_adap;179struct IR_i2c_init_data *init_data = &itv->ir_i2c_init_data;180unsigned short addr_list[2] = { addr, I2C_CLIENT_END };181182/* Only allow one IR transmitter to be registered per board */183if (hw & IVTV_HW_IR_TX_ANY) {184if (itv->hw_flags & IVTV_HW_IR_TX_ANY)185return -1;186memset(&info, 0, sizeof(struct i2c_board_info));187strlcpy(info.type, type, I2C_NAME_SIZE);188return i2c_new_probed_device(adap, &info, addr_list, NULL)189== NULL ? -1 : 0;190}191192/* Only allow one IR receiver to be registered per board */193if (itv->hw_flags & IVTV_HW_IR_RX_ANY)194return -1;195196/* Our default information for ir-kbd-i2c.c to use */197switch (hw) {198case IVTV_HW_I2C_IR_RX_AVER:199init_data->ir_codes = RC_MAP_AVERMEDIA_CARDBUS;200init_data->internal_get_key_func =201IR_KBD_GET_KEY_AVERMEDIA_CARDBUS;202init_data->type = RC_TYPE_OTHER;203init_data->name = "AVerMedia AVerTV card";204break;205case IVTV_HW_I2C_IR_RX_HAUP_EXT:206case IVTV_HW_I2C_IR_RX_HAUP_INT:207init_data->ir_codes = RC_MAP_HAUPPAUGE;208init_data->internal_get_key_func = IR_KBD_GET_KEY_HAUP;209init_data->type = RC_TYPE_RC5;210init_data->name = itv->card_name;211break;212case IVTV_HW_Z8F0811_IR_RX_HAUP:213/* Default to grey remote */214init_data->ir_codes = RC_MAP_HAUPPAUGE;215init_data->internal_get_key_func = IR_KBD_GET_KEY_HAUP_XVR;216init_data->type = RC_TYPE_RC5;217init_data->name = itv->card_name;218break;219case IVTV_HW_I2C_IR_RX_ADAPTEC:220init_data->get_key = get_key_adaptec;221init_data->name = itv->card_name;222/* FIXME: The protocol and RC_MAP needs to be corrected */223init_data->ir_codes = RC_MAP_EMPTY;224init_data->type = RC_TYPE_UNKNOWN;225break;226}227228memset(&info, 0, sizeof(struct i2c_board_info));229info.platform_data = init_data;230strlcpy(info.type, type, I2C_NAME_SIZE);231232return i2c_new_probed_device(adap, &info, addr_list, NULL) == NULL ?233-1 : 0;234}235236/* Instantiate the IR receiver device using probing -- undesirable */237struct i2c_client *ivtv_i2c_new_ir_legacy(struct ivtv *itv)238{239struct i2c_board_info info;240/*241* The external IR receiver is at i2c address 0x34.242* The internal IR receiver is at i2c address 0x30.243*244* In theory, both can be fitted, and Hauppauge suggests an external245* overrides an internal. That's why we probe 0x1a (~0x34) first. CB246*247* Some of these addresses we probe may collide with other i2c address248* allocations, so this function must be called after all other i2c249* devices we care about are registered.250*/251const unsigned short addr_list[] = {2520x1a, /* Hauppauge IR external - collides with WM8739 */2530x18, /* Hauppauge IR internal */254I2C_CLIENT_END255};256257memset(&info, 0, sizeof(struct i2c_board_info));258strlcpy(info.type, "ir_video", I2C_NAME_SIZE);259return i2c_new_probed_device(&itv->i2c_adap, &info, addr_list, NULL);260}261262int ivtv_i2c_register(struct ivtv *itv, unsigned idx)263{264struct v4l2_subdev *sd;265struct i2c_adapter *adap = &itv->i2c_adap;266const char *type = hw_devicenames[idx];267u32 hw = 1 << idx;268269if (idx >= ARRAY_SIZE(hw_addrs))270return -1;271if (hw == IVTV_HW_TUNER) {272/* special tuner handling */273sd = v4l2_i2c_new_subdev(&itv->v4l2_dev, adap, type, 0,274itv->card_i2c->radio);275if (sd)276sd->grp_id = 1 << idx;277sd = v4l2_i2c_new_subdev(&itv->v4l2_dev, adap, type, 0,278itv->card_i2c->demod);279if (sd)280sd->grp_id = 1 << idx;281sd = v4l2_i2c_new_subdev(&itv->v4l2_dev, adap, type, 0,282itv->card_i2c->tv);283if (sd)284sd->grp_id = 1 << idx;285return sd ? 0 : -1;286}287288if (hw & IVTV_HW_IR_ANY)289return ivtv_i2c_new_ir(itv, hw, type, hw_addrs[idx]);290291/* Is it not an I2C device or one we do not wish to register? */292if (!hw_addrs[idx])293return -1;294295/* It's an I2C device other than an analog tuner or IR chip */296if (hw == IVTV_HW_UPD64031A || hw == IVTV_HW_UPD6408X) {297sd = v4l2_i2c_new_subdev(&itv->v4l2_dev,298adap, type, 0, I2C_ADDRS(hw_addrs[idx]));299} else if (hw == IVTV_HW_CX25840) {300struct cx25840_platform_data pdata;301struct i2c_board_info cx25840_info = {302.type = "cx25840",303.addr = hw_addrs[idx],304.platform_data = &pdata,305};306307pdata.pvr150_workaround = itv->pvr150_workaround;308sd = v4l2_i2c_new_subdev_board(&itv->v4l2_dev, adap,309&cx25840_info, NULL);310} else {311sd = v4l2_i2c_new_subdev(&itv->v4l2_dev,312adap, type, hw_addrs[idx], NULL);313}314if (sd)315sd->grp_id = 1 << idx;316return sd ? 0 : -1;317}318319struct v4l2_subdev *ivtv_find_hw(struct ivtv *itv, u32 hw)320{321struct v4l2_subdev *result = NULL;322struct v4l2_subdev *sd;323324spin_lock(&itv->v4l2_dev.lock);325v4l2_device_for_each_subdev(sd, &itv->v4l2_dev) {326if (sd->grp_id == hw) {327result = sd;328break;329}330}331spin_unlock(&itv->v4l2_dev.lock);332return result;333}334335/* Set the serial clock line to the desired state */336static void ivtv_setscl(struct ivtv *itv, int state)337{338/* write them out */339/* write bits are inverted */340write_reg(~state, IVTV_REG_I2C_SETSCL_OFFSET);341}342343/* Set the serial data line to the desired state */344static void ivtv_setsda(struct ivtv *itv, int state)345{346/* write them out */347/* write bits are inverted */348write_reg(~state & 1, IVTV_REG_I2C_SETSDA_OFFSET);349}350351/* Read the serial clock line */352static int ivtv_getscl(struct ivtv *itv)353{354return read_reg(IVTV_REG_I2C_GETSCL_OFFSET) & 1;355}356357/* Read the serial data line */358static int ivtv_getsda(struct ivtv *itv)359{360return read_reg(IVTV_REG_I2C_GETSDA_OFFSET) & 1;361}362363/* Implement a short delay by polling the serial clock line */364static void ivtv_scldelay(struct ivtv *itv)365{366int i;367368for (i = 0; i < 5; ++i)369ivtv_getscl(itv);370}371372/* Wait for the serial clock line to become set to a specific value */373static int ivtv_waitscl(struct ivtv *itv, int val)374{375int i;376377ivtv_scldelay(itv);378for (i = 0; i < 1000; ++i) {379if (ivtv_getscl(itv) == val)380return 1;381}382return 0;383}384385/* Wait for the serial data line to become set to a specific value */386static int ivtv_waitsda(struct ivtv *itv, int val)387{388int i;389390ivtv_scldelay(itv);391for (i = 0; i < 1000; ++i) {392if (ivtv_getsda(itv) == val)393return 1;394}395return 0;396}397398/* Wait for the slave to issue an ACK */399static int ivtv_ack(struct ivtv *itv)400{401int ret = 0;402403if (ivtv_getscl(itv) == 1) {404IVTV_DEBUG_HI_I2C("SCL was high starting an ack\n");405ivtv_setscl(itv, 0);406if (!ivtv_waitscl(itv, 0)) {407IVTV_DEBUG_I2C("Could not set SCL low starting an ack\n");408return -EREMOTEIO;409}410}411ivtv_setsda(itv, 1);412ivtv_scldelay(itv);413ivtv_setscl(itv, 1);414if (!ivtv_waitsda(itv, 0)) {415IVTV_DEBUG_I2C("Slave did not ack\n");416ret = -EREMOTEIO;417}418ivtv_setscl(itv, 0);419if (!ivtv_waitscl(itv, 0)) {420IVTV_DEBUG_I2C("Failed to set SCL low after ACK\n");421ret = -EREMOTEIO;422}423return ret;424}425426/* Write a single byte to the i2c bus and wait for the slave to ACK */427static int ivtv_sendbyte(struct ivtv *itv, unsigned char byte)428{429int i, bit;430431IVTV_DEBUG_HI_I2C("write %x\n",byte);432for (i = 0; i < 8; ++i, byte<<=1) {433ivtv_setscl(itv, 0);434if (!ivtv_waitscl(itv, 0)) {435IVTV_DEBUG_I2C("Error setting SCL low\n");436return -EREMOTEIO;437}438bit = (byte>>7)&1;439ivtv_setsda(itv, bit);440if (!ivtv_waitsda(itv, bit)) {441IVTV_DEBUG_I2C("Error setting SDA\n");442return -EREMOTEIO;443}444ivtv_setscl(itv, 1);445if (!ivtv_waitscl(itv, 1)) {446IVTV_DEBUG_I2C("Slave not ready for bit\n");447return -EREMOTEIO;448}449}450ivtv_setscl(itv, 0);451if (!ivtv_waitscl(itv, 0)) {452IVTV_DEBUG_I2C("Error setting SCL low\n");453return -EREMOTEIO;454}455return ivtv_ack(itv);456}457458/* Read a byte from the i2c bus and send a NACK if applicable (i.e. for the459final byte) */460static int ivtv_readbyte(struct ivtv *itv, unsigned char *byte, int nack)461{462int i;463464*byte = 0;465466ivtv_setsda(itv, 1);467ivtv_scldelay(itv);468for (i = 0; i < 8; ++i) {469ivtv_setscl(itv, 0);470ivtv_scldelay(itv);471ivtv_setscl(itv, 1);472if (!ivtv_waitscl(itv, 1)) {473IVTV_DEBUG_I2C("Error setting SCL high\n");474return -EREMOTEIO;475}476*byte = ((*byte)<<1)|ivtv_getsda(itv);477}478ivtv_setscl(itv, 0);479ivtv_scldelay(itv);480ivtv_setsda(itv, nack);481ivtv_scldelay(itv);482ivtv_setscl(itv, 1);483ivtv_scldelay(itv);484ivtv_setscl(itv, 0);485ivtv_scldelay(itv);486IVTV_DEBUG_HI_I2C("read %x\n",*byte);487return 0;488}489490/* Issue a start condition on the i2c bus to alert slaves to prepare for491an address write */492static int ivtv_start(struct ivtv *itv)493{494int sda;495496sda = ivtv_getsda(itv);497if (sda != 1) {498IVTV_DEBUG_HI_I2C("SDA was low at start\n");499ivtv_setsda(itv, 1);500if (!ivtv_waitsda(itv, 1)) {501IVTV_DEBUG_I2C("SDA stuck low\n");502return -EREMOTEIO;503}504}505if (ivtv_getscl(itv) != 1) {506ivtv_setscl(itv, 1);507if (!ivtv_waitscl(itv, 1)) {508IVTV_DEBUG_I2C("SCL stuck low at start\n");509return -EREMOTEIO;510}511}512ivtv_setsda(itv, 0);513ivtv_scldelay(itv);514return 0;515}516517/* Issue a stop condition on the i2c bus to release it */518static int ivtv_stop(struct ivtv *itv)519{520int i;521522if (ivtv_getscl(itv) != 0) {523IVTV_DEBUG_HI_I2C("SCL not low when stopping\n");524ivtv_setscl(itv, 0);525if (!ivtv_waitscl(itv, 0)) {526IVTV_DEBUG_I2C("SCL could not be set low\n");527}528}529ivtv_setsda(itv, 0);530ivtv_scldelay(itv);531ivtv_setscl(itv, 1);532if (!ivtv_waitscl(itv, 1)) {533IVTV_DEBUG_I2C("SCL could not be set high\n");534return -EREMOTEIO;535}536ivtv_scldelay(itv);537ivtv_setsda(itv, 1);538if (!ivtv_waitsda(itv, 1)) {539IVTV_DEBUG_I2C("resetting I2C\n");540for (i = 0; i < 16; ++i) {541ivtv_setscl(itv, 0);542ivtv_scldelay(itv);543ivtv_setscl(itv, 1);544ivtv_scldelay(itv);545ivtv_setsda(itv, 1);546}547ivtv_waitsda(itv, 1);548return -EREMOTEIO;549}550return 0;551}552553/* Write a message to the given i2c slave. do_stop may be 0 to prevent554issuing the i2c stop condition (when following with a read) */555static int ivtv_write(struct ivtv *itv, unsigned char addr, unsigned char *data, u32 len, int do_stop)556{557int retry, ret = -EREMOTEIO;558u32 i;559560for (retry = 0; ret != 0 && retry < 8; ++retry) {561ret = ivtv_start(itv);562563if (ret == 0) {564ret = ivtv_sendbyte(itv, addr<<1);565for (i = 0; ret == 0 && i < len; ++i)566ret = ivtv_sendbyte(itv, data[i]);567}568if (ret != 0 || do_stop) {569ivtv_stop(itv);570}571}572if (ret)573IVTV_DEBUG_I2C("i2c write to %x failed\n", addr);574return ret;575}576577/* Read data from the given i2c slave. A stop condition is always issued. */578static int ivtv_read(struct ivtv *itv, unsigned char addr, unsigned char *data, u32 len)579{580int retry, ret = -EREMOTEIO;581u32 i;582583for (retry = 0; ret != 0 && retry < 8; ++retry) {584ret = ivtv_start(itv);585if (ret == 0)586ret = ivtv_sendbyte(itv, (addr << 1) | 1);587for (i = 0; ret == 0 && i < len; ++i) {588ret = ivtv_readbyte(itv, &data[i], i == len - 1);589}590ivtv_stop(itv);591}592if (ret)593IVTV_DEBUG_I2C("i2c read from %x failed\n", addr);594return ret;595}596597/* Kernel i2c transfer implementation. Takes a number of messages to be read598or written. If a read follows a write, this will occur without an599intervening stop condition */600static int ivtv_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg *msgs, int num)601{602struct v4l2_device *v4l2_dev = i2c_get_adapdata(i2c_adap);603struct ivtv *itv = to_ivtv(v4l2_dev);604int retval;605int i;606607mutex_lock(&itv->i2c_bus_lock);608for (i = retval = 0; retval == 0 && i < num; i++) {609if (msgs[i].flags & I2C_M_RD)610retval = ivtv_read(itv, msgs[i].addr, msgs[i].buf, msgs[i].len);611else {612/* if followed by a read, don't stop */613int stop = !(i + 1 < num && msgs[i + 1].flags == I2C_M_RD);614615retval = ivtv_write(itv, msgs[i].addr, msgs[i].buf, msgs[i].len, stop);616}617}618mutex_unlock(&itv->i2c_bus_lock);619return retval ? retval : num;620}621622/* Kernel i2c capabilities */623static u32 ivtv_functionality(struct i2c_adapter *adap)624{625return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;626}627628static struct i2c_algorithm ivtv_algo = {629.master_xfer = ivtv_xfer,630.functionality = ivtv_functionality,631};632633/* template for our-bit banger */634static struct i2c_adapter ivtv_i2c_adap_hw_template = {635.name = "ivtv i2c driver",636.algo = &ivtv_algo,637.algo_data = NULL, /* filled from template */638.owner = THIS_MODULE,639};640641static void ivtv_setscl_old(void *data, int state)642{643struct ivtv *itv = (struct ivtv *)data;644645if (state)646itv->i2c_state |= 0x01;647else648itv->i2c_state &= ~0x01;649650/* write them out */651/* write bits are inverted */652write_reg(~itv->i2c_state, IVTV_REG_I2C_SETSCL_OFFSET);653}654655static void ivtv_setsda_old(void *data, int state)656{657struct ivtv *itv = (struct ivtv *)data;658659if (state)660itv->i2c_state |= 0x01;661else662itv->i2c_state &= ~0x01;663664/* write them out */665/* write bits are inverted */666write_reg(~itv->i2c_state, IVTV_REG_I2C_SETSDA_OFFSET);667}668669static int ivtv_getscl_old(void *data)670{671struct ivtv *itv = (struct ivtv *)data;672673return read_reg(IVTV_REG_I2C_GETSCL_OFFSET) & 1;674}675676static int ivtv_getsda_old(void *data)677{678struct ivtv *itv = (struct ivtv *)data;679680return read_reg(IVTV_REG_I2C_GETSDA_OFFSET) & 1;681}682683/* template for i2c-bit-algo */684static struct i2c_adapter ivtv_i2c_adap_template = {685.name = "ivtv i2c driver",686.algo = NULL, /* set by i2c-algo-bit */687.algo_data = NULL, /* filled from template */688.owner = THIS_MODULE,689};690691#define IVTV_ALGO_BIT_TIMEOUT (2) /* seconds */692693static const struct i2c_algo_bit_data ivtv_i2c_algo_template = {694.setsda = ivtv_setsda_old,695.setscl = ivtv_setscl_old,696.getsda = ivtv_getsda_old,697.getscl = ivtv_getscl_old,698.udelay = IVTV_DEFAULT_I2C_CLOCK_PERIOD / 2, /* microseconds */699.timeout = IVTV_ALGO_BIT_TIMEOUT * HZ, /* jiffies */700};701702static struct i2c_client ivtv_i2c_client_template = {703.name = "ivtv internal",704};705706/* init + register i2c adapter */707int init_ivtv_i2c(struct ivtv *itv)708{709int retval;710711IVTV_DEBUG_I2C("i2c init\n");712713/* Sanity checks for the I2C hardware arrays. They must be the714* same size.715*/716if (ARRAY_SIZE(hw_devicenames) != ARRAY_SIZE(hw_addrs)) {717IVTV_ERR("Mismatched I2C hardware arrays\n");718return -ENODEV;719}720if (itv->options.newi2c > 0) {721memcpy(&itv->i2c_adap, &ivtv_i2c_adap_hw_template,722sizeof(struct i2c_adapter));723} else {724memcpy(&itv->i2c_adap, &ivtv_i2c_adap_template,725sizeof(struct i2c_adapter));726memcpy(&itv->i2c_algo, &ivtv_i2c_algo_template,727sizeof(struct i2c_algo_bit_data));728}729itv->i2c_algo.udelay = itv->options.i2c_clock_period / 2;730itv->i2c_algo.data = itv;731itv->i2c_adap.algo_data = &itv->i2c_algo;732733sprintf(itv->i2c_adap.name + strlen(itv->i2c_adap.name), " #%d",734itv->instance);735i2c_set_adapdata(&itv->i2c_adap, &itv->v4l2_dev);736737memcpy(&itv->i2c_client, &ivtv_i2c_client_template,738sizeof(struct i2c_client));739itv->i2c_client.adapter = &itv->i2c_adap;740itv->i2c_adap.dev.parent = &itv->pdev->dev;741742IVTV_DEBUG_I2C("setting scl and sda to 1\n");743ivtv_setscl(itv, 1);744ivtv_setsda(itv, 1);745746if (itv->options.newi2c > 0)747retval = i2c_add_adapter(&itv->i2c_adap);748else749retval = i2c_bit_add_bus(&itv->i2c_adap);750751return retval;752}753754void exit_ivtv_i2c(struct ivtv *itv)755{756IVTV_DEBUG_I2C("i2c exit\n");757758i2c_del_adapter(&itv->i2c_adap);759}760761762