Path: blob/master/drivers/media/video/bt8xx/bttv-i2c.c
10785 views
/*12bttv-i2c.c -- all the i2c code is here34bttv - Bt848 frame grabber driver56Copyright (C) 1996,97,98 Ralph Metzler ([email protected])7& Marcus Metzler ([email protected])8(c) 1999-2003 Gerd Knorr <[email protected]>910(c) 2005 Mauro Carvalho Chehab <[email protected]>11- Multituner support and i2c address binding1213This program is free software; you can redistribute it and/or modify14it under the terms of the GNU General Public License as published by15the Free Software Foundation; either version 2 of the License, or16(at your option) any later version.1718This program is distributed in the hope that it will be useful,19but WITHOUT ANY WARRANTY; without even the implied warranty of20MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the21GNU General Public License for more details.2223You should have received a copy of the GNU General Public License24along with this program; if not, write to the Free Software25Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.2627*/2829#include <linux/module.h>30#include <linux/init.h>31#include <linux/delay.h>3233#include "bttvp.h"34#include <media/v4l2-common.h>35#include <linux/jiffies.h>36#include <asm/io.h>3738static int i2c_debug;39static int i2c_hw;40static int i2c_scan;41module_param(i2c_debug, int, 0644);42MODULE_PARM_DESC(i2c_debug, "configure i2c debug level");43module_param(i2c_hw, int, 0444);44MODULE_PARM_DESC(i2c_hw,"force use of hardware i2c support, "45"instead of software bitbang");46module_param(i2c_scan, int, 0444);47MODULE_PARM_DESC(i2c_scan,"scan i2c bus at insmod time");4849static unsigned int i2c_udelay = 5;50module_param(i2c_udelay, int, 0444);51MODULE_PARM_DESC(i2c_udelay,"soft i2c delay at insmod time, in usecs "52"(should be 5 or higher). Lower value means higher bus speed.");5354/* ----------------------------------------------------------------------- */55/* I2C functions - bitbanging adapter (software i2c) */5657static void bttv_bit_setscl(void *data, int state)58{59struct bttv *btv = (struct bttv*)data;6061if (state)62btv->i2c_state |= 0x02;63else64btv->i2c_state &= ~0x02;65btwrite(btv->i2c_state, BT848_I2C);66btread(BT848_I2C);67}6869static void bttv_bit_setsda(void *data, int state)70{71struct bttv *btv = (struct bttv*)data;7273if (state)74btv->i2c_state |= 0x01;75else76btv->i2c_state &= ~0x01;77btwrite(btv->i2c_state, BT848_I2C);78btread(BT848_I2C);79}8081static int bttv_bit_getscl(void *data)82{83struct bttv *btv = (struct bttv*)data;84int state;8586state = btread(BT848_I2C) & 0x02 ? 1 : 0;87return state;88}8990static int bttv_bit_getsda(void *data)91{92struct bttv *btv = (struct bttv*)data;93int state;9495state = btread(BT848_I2C) & 0x01;96return state;97}9899static struct i2c_algo_bit_data __devinitdata bttv_i2c_algo_bit_template = {100.setsda = bttv_bit_setsda,101.setscl = bttv_bit_setscl,102.getsda = bttv_bit_getsda,103.getscl = bttv_bit_getscl,104.udelay = 16,105.timeout = 200,106};107108/* ----------------------------------------------------------------------- */109/* I2C functions - hardware i2c */110111static u32 functionality(struct i2c_adapter *adap)112{113return I2C_FUNC_SMBUS_EMUL;114}115116static int117bttv_i2c_wait_done(struct bttv *btv)118{119int rc = 0;120121/* timeout */122if (wait_event_interruptible_timeout(btv->i2c_queue,123btv->i2c_done, msecs_to_jiffies(85)) == -ERESTARTSYS)124rc = -EIO;125126if (btv->i2c_done & BT848_INT_RACK)127rc = 1;128btv->i2c_done = 0;129return rc;130}131132#define I2C_HW (BT878_I2C_MODE | BT848_I2C_SYNC |\133BT848_I2C_SCL | BT848_I2C_SDA)134135static int136bttv_i2c_sendbytes(struct bttv *btv, const struct i2c_msg *msg, int last)137{138u32 xmit;139int retval,cnt;140141/* sanity checks */142if (0 == msg->len)143return -EINVAL;144145/* start, address + first byte */146xmit = (msg->addr << 25) | (msg->buf[0] << 16) | I2C_HW;147if (msg->len > 1 || !last)148xmit |= BT878_I2C_NOSTOP;149btwrite(xmit, BT848_I2C);150retval = bttv_i2c_wait_done(btv);151if (retval < 0)152goto err;153if (retval == 0)154goto eio;155if (i2c_debug) {156printk(" <W %02x %02x", msg->addr << 1, msg->buf[0]);157if (!(xmit & BT878_I2C_NOSTOP))158printk(" >\n");159}160161for (cnt = 1; cnt < msg->len; cnt++ ) {162/* following bytes */163xmit = (msg->buf[cnt] << 24) | I2C_HW | BT878_I2C_NOSTART;164if (cnt < msg->len-1 || !last)165xmit |= BT878_I2C_NOSTOP;166btwrite(xmit, BT848_I2C);167retval = bttv_i2c_wait_done(btv);168if (retval < 0)169goto err;170if (retval == 0)171goto eio;172if (i2c_debug) {173printk(" %02x", msg->buf[cnt]);174if (!(xmit & BT878_I2C_NOSTOP))175printk(" >\n");176}177}178return msg->len;179180eio:181retval = -EIO;182err:183if (i2c_debug)184printk(" ERR: %d\n",retval);185return retval;186}187188static int189bttv_i2c_readbytes(struct bttv *btv, const struct i2c_msg *msg, int last)190{191u32 xmit;192u32 cnt;193int retval;194195for(cnt = 0; cnt < msg->len; cnt++) {196xmit = (msg->addr << 25) | (1 << 24) | I2C_HW;197if (cnt < msg->len-1)198xmit |= BT848_I2C_W3B;199if (cnt < msg->len-1 || !last)200xmit |= BT878_I2C_NOSTOP;201if (cnt)202xmit |= BT878_I2C_NOSTART;203btwrite(xmit, BT848_I2C);204retval = bttv_i2c_wait_done(btv);205if (retval < 0)206goto err;207if (retval == 0)208goto eio;209msg->buf[cnt] = ((u32)btread(BT848_I2C) >> 8) & 0xff;210if (i2c_debug) {211if (!(xmit & BT878_I2C_NOSTART))212printk(" <R %02x", (msg->addr << 1) +1);213printk(" =%02x", msg->buf[cnt]);214if (!(xmit & BT878_I2C_NOSTOP))215printk(" >\n");216}217}218return msg->len;219220eio:221retval = -EIO;222err:223if (i2c_debug)224printk(" ERR: %d\n",retval);225return retval;226}227228static int bttv_i2c_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg *msgs, int num)229{230struct v4l2_device *v4l2_dev = i2c_get_adapdata(i2c_adap);231struct bttv *btv = to_bttv(v4l2_dev);232int retval = 0;233int i;234235if (i2c_debug)236printk("bt-i2c:");237btwrite(BT848_INT_I2CDONE|BT848_INT_RACK, BT848_INT_STAT);238for (i = 0 ; i < num; i++) {239if (msgs[i].flags & I2C_M_RD) {240/* read */241retval = bttv_i2c_readbytes(btv, &msgs[i], i+1 == num);242if (retval < 0)243goto err;244} else {245/* write */246retval = bttv_i2c_sendbytes(btv, &msgs[i], i+1 == num);247if (retval < 0)248goto err;249}250}251return num;252253err:254return retval;255}256257static const struct i2c_algorithm bttv_algo = {258.master_xfer = bttv_i2c_xfer,259.functionality = functionality,260};261262/* ----------------------------------------------------------------------- */263/* I2C functions - common stuff */264265/* read I2C */266int bttv_I2CRead(struct bttv *btv, unsigned char addr, char *probe_for)267{268unsigned char buffer = 0;269270if (0 != btv->i2c_rc)271return -1;272if (bttv_verbose && NULL != probe_for)273printk(KERN_INFO "bttv%d: i2c: checking for %s @ 0x%02x... ",274btv->c.nr,probe_for,addr);275btv->i2c_client.addr = addr >> 1;276if (1 != i2c_master_recv(&btv->i2c_client, &buffer, 1)) {277if (NULL != probe_for) {278if (bttv_verbose)279printk("not found\n");280} else281printk(KERN_WARNING "bttv%d: i2c read 0x%x: error\n",282btv->c.nr,addr);283return -1;284}285if (bttv_verbose && NULL != probe_for)286printk("found\n");287return buffer;288}289290/* write I2C */291int bttv_I2CWrite(struct bttv *btv, unsigned char addr, unsigned char b1,292unsigned char b2, int both)293{294unsigned char buffer[2];295int bytes = both ? 2 : 1;296297if (0 != btv->i2c_rc)298return -1;299btv->i2c_client.addr = addr >> 1;300buffer[0] = b1;301buffer[1] = b2;302if (bytes != i2c_master_send(&btv->i2c_client, buffer, bytes))303return -1;304return 0;305}306307/* read EEPROM content */308void __devinit bttv_readee(struct bttv *btv, unsigned char *eedata, int addr)309{310memset(eedata, 0, 256);311if (0 != btv->i2c_rc)312return;313btv->i2c_client.addr = addr >> 1;314tveeprom_read(&btv->i2c_client, eedata, 256);315}316317static char *i2c_devs[128] = {318[ 0x1c >> 1 ] = "lgdt330x",319[ 0x30 >> 1 ] = "IR (hauppauge)",320[ 0x80 >> 1 ] = "msp34xx",321[ 0x86 >> 1 ] = "tda9887",322[ 0xa0 >> 1 ] = "eeprom",323[ 0xc0 >> 1 ] = "tuner (analog)",324[ 0xc2 >> 1 ] = "tuner (analog)",325};326327static void do_i2c_scan(char *name, struct i2c_client *c)328{329unsigned char buf;330int i,rc;331332for (i = 0; i < ARRAY_SIZE(i2c_devs); i++) {333c->addr = i;334rc = i2c_master_recv(c,&buf,0);335if (rc < 0)336continue;337printk("%s: i2c scan: found device @ 0x%x [%s]\n",338name, i << 1, i2c_devs[i] ? i2c_devs[i] : "???");339}340}341342/* init + register i2c algo-bit adapter */343int __devinit init_bttv_i2c(struct bttv *btv)344{345strlcpy(btv->i2c_client.name, "bttv internal", I2C_NAME_SIZE);346347if (i2c_hw)348btv->use_i2c_hw = 1;349if (btv->use_i2c_hw) {350/* bt878 */351strlcpy(btv->c.i2c_adap.name, "bt878",352sizeof(btv->c.i2c_adap.name));353btv->c.i2c_adap.algo = &bttv_algo;354} else {355/* bt848 */356/* Prevents usage of invalid delay values */357if (i2c_udelay<5)358i2c_udelay=5;359360strlcpy(btv->c.i2c_adap.name, "bttv",361sizeof(btv->c.i2c_adap.name));362memcpy(&btv->i2c_algo, &bttv_i2c_algo_bit_template,363sizeof(bttv_i2c_algo_bit_template));364btv->i2c_algo.udelay = i2c_udelay;365btv->i2c_algo.data = btv;366btv->c.i2c_adap.algo_data = &btv->i2c_algo;367}368btv->c.i2c_adap.owner = THIS_MODULE;369370btv->c.i2c_adap.dev.parent = &btv->c.pci->dev;371snprintf(btv->c.i2c_adap.name, sizeof(btv->c.i2c_adap.name),372"bt%d #%d [%s]", btv->id, btv->c.nr,373btv->use_i2c_hw ? "hw" : "sw");374375i2c_set_adapdata(&btv->c.i2c_adap, &btv->c.v4l2_dev);376btv->i2c_client.adapter = &btv->c.i2c_adap;377378379if (btv->use_i2c_hw) {380btv->i2c_rc = i2c_add_adapter(&btv->c.i2c_adap);381} else {382bttv_bit_setscl(btv,1);383bttv_bit_setsda(btv,1);384btv->i2c_rc = i2c_bit_add_bus(&btv->c.i2c_adap);385}386if (0 == btv->i2c_rc && i2c_scan)387do_i2c_scan(btv->c.v4l2_dev.name, &btv->i2c_client);388389return btv->i2c_rc;390}391392393