linux 2.6.16.38 w/ vs2.0.3-rc1
[linux-2.6.git] / drivers / media / video / bttv-i2c.c
index c07a0d9..614c120 100644 (file)
@@ -1,10 +1,11 @@
 /*
+
     bttv-i2c.c  --  all the i2c code is here
 
     bttv - Bt848 frame grabber driver
 
     Copyright (C) 1996,97,98 Ralph  Metzler (rjkm@thp.uni-koeln.de)
-                           & Marcus Metzler (mocm@thp.uni-koeln.de)
+                          & Marcus Metzler (mocm@thp.uni-koeln.de)
     (c) 1999-2003 Gerd Knorr <kraxel@bytesex.org>
 
     This program is free software; you can redistribute it and/or modify
     You should have received a copy of the GNU General Public License
     along with this program; if not, write to the Free Software
     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
-    
+
 */
 
 #include <linux/module.h>
+#include <linux/moduleparam.h>
 #include <linux/init.h>
 #include <linux/delay.h>
-#include <asm/io.h>
 
 #include "bttvp.h"
+#include <media/v4l2-common.h>
+#include <linux/jiffies.h>
+#include <asm/io.h>
 
 static struct i2c_algo_bit_data bttv_i2c_algo_bit_template;
 static struct i2c_adapter bttv_i2c_adap_sw_template;
 static struct i2c_adapter bttv_i2c_adap_hw_template;
 static struct i2c_client bttv_i2c_client_template;
 
-#ifndef I2C_PEC
-static void bttv_inc_use(struct i2c_adapter *adap);
-static void bttv_dec_use(struct i2c_adapter *adap);
-#endif
 static int attach_inform(struct i2c_client *client);
 
-static int i2c_debug = 0;
-static int i2c_hw = 0;
-MODULE_PARM(i2c_debug,"i");
-MODULE_PARM(i2c_hw,"i");
+static int i2c_debug;
+static int i2c_hw;
+static int i2c_scan;
+module_param(i2c_debug, int, 0644);
+module_param(i2c_hw,    int, 0444);
+module_param(i2c_scan,  int, 0444);
+MODULE_PARM_DESC(i2c_scan,"scan i2c bus at insmod time");
 
 /* ----------------------------------------------------------------------- */
 /* I2C functions - bitbanging adapter (software i2c)                       */
 
-void bttv_bit_setscl(void *data, int state)
+static void bttv_bit_setscl(void *data, int state)
 {
        struct bttv *btv = (struct bttv*)data;
 
@@ -61,7 +64,7 @@ void bttv_bit_setscl(void *data, int state)
        btread(BT848_I2C);
 }
 
-void bttv_bit_setsda(void *data, int state)
+static void bttv_bit_setsda(void *data, int state)
 {
        struct bttv *btv = (struct bttv*)data;
 
@@ -77,7 +80,7 @@ static int bttv_bit_getscl(void *data)
 {
        struct bttv *btv = (struct bttv*)data;
        int state;
-       
+
        state = btread(BT848_I2C) & 0x02 ? 1 : 0;
        return state;
 }
@@ -102,16 +105,9 @@ static struct i2c_algo_bit_data bttv_i2c_algo_bit_template = {
 };
 
 static struct i2c_adapter bttv_i2c_adap_sw_template = {
-#ifdef I2C_PEC
        .owner             = THIS_MODULE,
-#else
-       .inc_use           = bttv_inc_use,
-       .dec_use           = bttv_dec_use,
-#endif
-#ifdef I2C_CLASS_TV_ANALOG
        .class             = I2C_CLASS_TV_ANALOG,
-#endif
-       I2C_DEVNAME("bt848"),
+       .name              = "bttv",
        .id                = I2C_HW_B_BT848,
        .client_register   = attach_inform,
 };
@@ -119,7 +115,7 @@ static struct i2c_adapter bttv_i2c_adap_sw_template = {
 /* ----------------------------------------------------------------------- */
 /* I2C functions - hardware i2c                                            */
 
-static int algo_control(struct i2c_adapter *adapter, 
+static int algo_control(struct i2c_adapter *adapter,
                        unsigned int cmd, unsigned long arg)
 {
        return 0;
@@ -133,19 +129,14 @@ static u32 functionality(struct i2c_adapter *adap)
 static int
 bttv_i2c_wait_done(struct bttv *btv)
 {
-       DECLARE_WAITQUEUE(wait, current);
        int rc = 0;
-       
-       add_wait_queue(&btv->i2c_queue, &wait);
-       set_current_state(TASK_INTERRUPTIBLE);
-       if (0 == btv->i2c_done)
-               schedule_timeout(HZ/50+1);
-       set_current_state(TASK_RUNNING);
-       remove_wait_queue(&btv->i2c_queue, &wait);
-
-       if (0 == btv->i2c_done)
-               /* timeout */
-               rc = -EIO;
+
+       /* timeout */
+       if (wait_event_interruptible_timeout(btv->i2c_queue,
+               btv->i2c_done, msecs_to_jiffies(85)) == -ERESTARTSYS)
+
+       rc = -EIO;
+
        if (btv->i2c_done & BT848_INT_RACK)
                rc = 1;
        btv->i2c_done = 0;
@@ -245,10 +236,10 @@ bttv_i2c_readbytes(struct bttv *btv, const struct i2c_msg *msg, int last)
  err:
        if (i2c_debug)
                printk(" ERR: %d\n",retval);
-               return retval;
+       return retval;
 }
 
-int bttv_i2c_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg msgs[], int num)
+static int bttv_i2c_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg *msgs, int num)
 {
        struct bttv *btv = i2c_get_adapdata(i2c_adap);
        int retval = 0;
@@ -277,25 +268,16 @@ int bttv_i2c_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg msgs[], int num)
 }
 
 static struct i2c_algorithm bttv_algo = {
-       .name          = "bt878",
-       .id            = I2C_ALGO_BIT | I2C_HW_B_BT848 /* FIXME */,
        .master_xfer   = bttv_i2c_xfer,
        .algo_control  = algo_control,
        .functionality = functionality,
 };
 
 static struct i2c_adapter bttv_i2c_adap_hw_template = {
-#ifdef I2C_PEC
-       .owner         = THIS_MODULE,
-#else
-       .inc_use       = bttv_inc_use,
-       .dec_use       = bttv_dec_use,
-#endif
-#ifdef I2C_CLASS_TV_ANALOG
+       .owner             = THIS_MODULE,
        .class         = I2C_CLASS_TV_ANALOG,
-#endif
-       I2C_DEVNAME("bt878"),
-       .id            = I2C_ALGO_BIT | I2C_HW_B_BT848 /* FIXME */,
+       .name          = "bt878",
+       .id            = I2C_HW_B_BT848 /* FIXME */,
        .algo          = &bttv_algo,
        .client_register = attach_inform,
 };
@@ -303,32 +285,38 @@ static struct i2c_adapter bttv_i2c_adap_hw_template = {
 /* ----------------------------------------------------------------------- */
 /* I2C functions - common stuff                                            */
 
-#ifndef I2C_PEC
-static void bttv_inc_use(struct i2c_adapter *adap)
+static int attach_inform(struct i2c_client *client)
 {
-       MOD_INC_USE_COUNT;
-}
+       struct bttv *btv = i2c_get_adapdata(client->adapter);
+       int addr=ADDR_UNSET;
 
-static void bttv_dec_use(struct i2c_adapter *adap)
-{
-       MOD_DEC_USE_COUNT;
-}
-#endif
 
-static int attach_inform(struct i2c_client *client)
-{
-        struct bttv *btv = i2c_get_adapdata(client->adapter);
-
-       if (btv->tuner_type != UNSET)
-               bttv_call_i2c_clients(btv,TUNER_SET_TYPE,&btv->tuner_type);
-       if (btv->pinnacle_id != UNSET)
-               bttv_call_i2c_clients(btv,AUDC_CONFIG_PINNACLE,
-                                     &btv->pinnacle_id);
-
-        if (bttv_debug)
-               printk("bttv%d: i2c attach [client=%s]\n",
-                      btv->c.nr, i2c_clientname(client));
-        return 0;
+       if (ADDR_UNSET != bttv_tvcards[btv->c.type].tuner_addr)
+               addr = bttv_tvcards[btv->c.type].tuner_addr;
+
+
+       if (bttv_debug)
+               printk(KERN_DEBUG "bttv%d: %s i2c attach [addr=0x%x,client=%s]\n",
+                       btv->c.nr, client->driver->driver.name, client->addr,
+                       client->name);
+       if (!client->driver->command)
+               return 0;
+
+       if (btv->tuner_type != UNSET) {
+               struct tuner_setup tun_setup;
+
+               if ((addr==ADDR_UNSET) ||
+                               (addr==client->addr)) {
+
+                       tun_setup.mode_mask = T_ANALOG_TV | T_DIGITAL_TV | T_RADIO;
+                       tun_setup.type = btv->tuner_type;
+                       tun_setup.addr = addr;
+                       bttv_call_i2c_clients(btv, TUNER_SET_TYPE_ADDR, &tun_setup);
+               }
+
+       }
+
+       return 0;
 }
 
 void bttv_call_i2c_clients(struct bttv *btv, unsigned int cmd, void *arg)
@@ -338,76 +326,85 @@ void bttv_call_i2c_clients(struct bttv *btv, unsigned int cmd, void *arg)
        i2c_clients_command(&btv->c.i2c_adap, cmd, arg);
 }
 
-void bttv_i2c_call(unsigned int card, unsigned int cmd, void *arg)
-{
-       if (card >= bttv_num)
-               return;
-       bttv_call_i2c_clients(&bttvs[card], cmd, arg);
-}
-
 static struct i2c_client bttv_i2c_client_template = {
-       I2C_DEVNAME("bttv internal"),
-        .id       = -1,
+       .name   = "bttv internal",
 };
 
 
 /* read I2C */
-int bttv_I2CRead(struct bttv *btv, unsigned char addr, char *probe_for) 
+int bttv_I2CRead(struct bttv *btv, unsigned char addr, char *probe_for)
 {
-        unsigned char buffer = 0;
+       unsigned char buffer = 0;
 
        if (0 != btv->i2c_rc)
                return -1;
        if (bttv_verbose && NULL != probe_for)
                printk(KERN_INFO "bttv%d: i2c: checking for %s @ 0x%02x... ",
                       btv->c.nr,probe_for,addr);
-        btv->i2c_client.addr = addr >> 1;
-        if (1 != i2c_master_recv(&btv->i2c_client, &buffer, 1)) {
+       btv->i2c_client.addr = addr >> 1;
+       if (1 != i2c_master_recv(&btv->i2c_client, &buffer, 1)) {
                if (NULL != probe_for) {
                        if (bttv_verbose)
                                printk("not found\n");
                } else
                        printk(KERN_WARNING "bttv%d: i2c read 0x%x: error\n",
                               btv->c.nr,addr);
-                return -1;
+               return -1;
        }
        if (bttv_verbose && NULL != probe_for)
                printk("found\n");
-        return buffer;
+       return buffer;
 }
 
 /* write I2C */
 int bttv_I2CWrite(struct bttv *btv, unsigned char addr, unsigned char b1,
-                    unsigned char b2, int both)
+                   unsigned char b2, int both)
 {
-        unsigned char buffer[2];
-        int bytes = both ? 2 : 1;
+       unsigned char buffer[2];
+       int bytes = both ? 2 : 1;
 
        if (0 != btv->i2c_rc)
                return -1;
-        btv->i2c_client.addr = addr >> 1;
-        buffer[0] = b1;
-        buffer[1] = b2;
-        if (bytes != i2c_master_send(&btv->i2c_client, buffer, bytes))
+       btv->i2c_client.addr = addr >> 1;
+       buffer[0] = b1;
+       buffer[1] = b2;
+       if (bytes != i2c_master_send(&btv->i2c_client, buffer, bytes))
                return -1;
-        return 0;
+       return 0;
 }
 
 /* read EEPROM content */
 void __devinit bttv_readee(struct bttv *btv, unsigned char *eedata, int addr)
 {
-       int i;
-        
-       if (bttv_I2CWrite(btv, addr, 0, -1, 0)<0) {
-               printk(KERN_WARNING "bttv: readee error\n");
+       memset(eedata, 0, 256);
+       if (0 != btv->i2c_rc)
                return;
-       }
        btv->i2c_client.addr = addr >> 1;
-       for (i=0; i<256; i+=16) {
-               if (16 != i2c_master_recv(&btv->i2c_client,eedata+i,16)) {
-                       printk(KERN_WARNING "bttv: readee error\n");
-                       break;
-               }
+       tveeprom_read(&btv->i2c_client, eedata, 256);
+}
+
+static char *i2c_devs[128] = {
+       [ 0x1c >> 1 ] = "lgdt330x",
+       [ 0x30 >> 1 ] = "IR (hauppauge)",
+       [ 0x80 >> 1 ] = "msp34xx",
+       [ 0x86 >> 1 ] = "tda9887",
+       [ 0xa0 >> 1 ] = "eeprom",
+       [ 0xc0 >> 1 ] = "tuner (analog)",
+       [ 0xc2 >> 1 ] = "tuner (analog)",
+};
+
+static void do_i2c_scan(char *name, struct i2c_client *c)
+{
+       unsigned char buf;
+       int i,rc;
+
+       for (i = 0; i < 128; i++) {
+               c->addr = i;
+               rc = i2c_master_recv(c,&buf,0);
+               if (rc < 0)
+                       continue;
+               printk("%s: i2c scan: found device @ 0x%x  [%s]\n",
+                      name, i << 1, i2c_devs[i] ? i2c_devs[i] : "???");
        }
 }
 
@@ -438,8 +435,13 @@ int __devinit init_bttv_i2c(struct bttv *btv)
                 "bt%d #%d [%s]", btv->id, btv->c.nr,
                 btv->use_i2c_hw ? "hw" : "sw");
 
-        i2c_set_adapdata(&btv->c.i2c_adap, btv);
-        btv->i2c_client.adapter = &btv->c.i2c_adap;
+       i2c_set_adapdata(&btv->c.i2c_adap, btv);
+       btv->i2c_client.adapter = &btv->c.i2c_adap;
+
+       if (bttv_tvcards[btv->c.type].no_video)
+               btv->c.i2c_adap.class &= ~I2C_CLASS_TV_ANALOG;
+       if (bttv_tvcards[btv->c.type].has_dvb)
+               btv->c.i2c_adap.class |= I2C_CLASS_TV_DIGITAL;
 
        if (btv->use_i2c_hw) {
                btv->i2c_rc = i2c_add_adapter(&btv->c.i2c_adap);
@@ -448,6 +450,8 @@ int __devinit init_bttv_i2c(struct bttv *btv)
                bttv_bit_setsda(btv,1);
                btv->i2c_rc = i2c_bit_add_bus(&btv->c.i2c_adap);
        }
+       if (0 == btv->i2c_rc && i2c_scan)
+               do_i2c_scan(btv->c.name,&btv->i2c_client);
        return btv->i2c_rc;
 }