0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0019
0020 #include <linux/module.h>
0021 #include <linux/init.h>
0022 #include <linux/delay.h>
0023
0024 #include "bttvp.h"
0025 #include <media/v4l2-common.h>
0026 #include <linux/jiffies.h>
0027 #include <asm/io.h>
0028
0029 static int i2c_debug;
0030 static int i2c_hw;
0031 static int i2c_scan;
0032 module_param(i2c_debug, int, 0644);
0033 MODULE_PARM_DESC(i2c_debug, "configure i2c debug level");
0034 module_param(i2c_hw, int, 0444);
0035 MODULE_PARM_DESC(i2c_hw, "force use of hardware i2c support, instead of software bitbang");
0036 module_param(i2c_scan, int, 0444);
0037 MODULE_PARM_DESC(i2c_scan,"scan i2c bus at insmod time");
0038
0039 static unsigned int i2c_udelay = 5;
0040 module_param(i2c_udelay, int, 0444);
0041 MODULE_PARM_DESC(i2c_udelay, "soft i2c delay at insmod time, in usecs (should be 5 or higher). Lower value means higher bus speed.");
0042
0043
0044
0045
0046 static void bttv_bit_setscl(void *data, int state)
0047 {
0048 struct bttv *btv = (struct bttv*)data;
0049
0050 if (state)
0051 btv->i2c_state |= 0x02;
0052 else
0053 btv->i2c_state &= ~0x02;
0054 btwrite(btv->i2c_state, BT848_I2C);
0055 btread(BT848_I2C);
0056 }
0057
0058 static void bttv_bit_setsda(void *data, int state)
0059 {
0060 struct bttv *btv = (struct bttv*)data;
0061
0062 if (state)
0063 btv->i2c_state |= 0x01;
0064 else
0065 btv->i2c_state &= ~0x01;
0066 btwrite(btv->i2c_state, BT848_I2C);
0067 btread(BT848_I2C);
0068 }
0069
0070 static int bttv_bit_getscl(void *data)
0071 {
0072 struct bttv *btv = (struct bttv*)data;
0073 int state;
0074
0075 state = btread(BT848_I2C) & 0x02 ? 1 : 0;
0076 return state;
0077 }
0078
0079 static int bttv_bit_getsda(void *data)
0080 {
0081 struct bttv *btv = (struct bttv*)data;
0082 int state;
0083
0084 state = btread(BT848_I2C) & 0x01;
0085 return state;
0086 }
0087
0088 static const struct i2c_algo_bit_data bttv_i2c_algo_bit_template = {
0089 .setsda = bttv_bit_setsda,
0090 .setscl = bttv_bit_setscl,
0091 .getsda = bttv_bit_getsda,
0092 .getscl = bttv_bit_getscl,
0093 .udelay = 16,
0094 .timeout = 200,
0095 };
0096
0097
0098
0099
0100 static u32 functionality(struct i2c_adapter *adap)
0101 {
0102 return I2C_FUNC_SMBUS_EMUL;
0103 }
0104
0105 static int
0106 bttv_i2c_wait_done(struct bttv *btv)
0107 {
0108 int rc = 0;
0109
0110
0111 if (wait_event_interruptible_timeout(btv->i2c_queue,
0112 btv->i2c_done, msecs_to_jiffies(85)) == -ERESTARTSYS)
0113 rc = -EIO;
0114
0115 if (btv->i2c_done & BT848_INT_RACK)
0116 rc = 1;
0117 btv->i2c_done = 0;
0118 return rc;
0119 }
0120
0121 #define I2C_HW (BT878_I2C_MODE | BT848_I2C_SYNC |\
0122 BT848_I2C_SCL | BT848_I2C_SDA)
0123
0124 static int
0125 bttv_i2c_sendbytes(struct bttv *btv, const struct i2c_msg *msg, int last)
0126 {
0127 u32 xmit;
0128 int retval,cnt;
0129
0130
0131 if (0 == msg->len)
0132 return -EINVAL;
0133
0134
0135 xmit = (msg->addr << 25) | (msg->buf[0] << 16) | I2C_HW;
0136 if (msg->len > 1 || !last)
0137 xmit |= BT878_I2C_NOSTOP;
0138 btwrite(xmit, BT848_I2C);
0139 retval = bttv_i2c_wait_done(btv);
0140 if (retval < 0)
0141 goto err;
0142 if (retval == 0)
0143 goto eio;
0144 if (i2c_debug) {
0145 pr_cont(" <W %02x %02x", msg->addr << 1, msg->buf[0]);
0146 }
0147
0148 for (cnt = 1; cnt < msg->len; cnt++ ) {
0149
0150 xmit = (msg->buf[cnt] << 24) | I2C_HW | BT878_I2C_NOSTART;
0151 if (cnt < msg->len-1 || !last)
0152 xmit |= BT878_I2C_NOSTOP;
0153 btwrite(xmit, BT848_I2C);
0154 retval = bttv_i2c_wait_done(btv);
0155 if (retval < 0)
0156 goto err;
0157 if (retval == 0)
0158 goto eio;
0159 if (i2c_debug)
0160 pr_cont(" %02x", msg->buf[cnt]);
0161 }
0162 if (i2c_debug && !(xmit & BT878_I2C_NOSTOP))
0163 pr_cont(">\n");
0164 return msg->len;
0165
0166 eio:
0167 retval = -EIO;
0168 err:
0169 if (i2c_debug)
0170 pr_cont(" ERR: %d\n",retval);
0171 return retval;
0172 }
0173
0174 static int
0175 bttv_i2c_readbytes(struct bttv *btv, const struct i2c_msg *msg, int last)
0176 {
0177 u32 xmit;
0178 u32 cnt;
0179 int retval;
0180
0181 for (cnt = 0; cnt < msg->len; cnt++) {
0182 xmit = (msg->addr << 25) | (1 << 24) | I2C_HW;
0183 if (cnt < msg->len-1)
0184 xmit |= BT848_I2C_W3B;
0185 if (cnt < msg->len-1 || !last)
0186 xmit |= BT878_I2C_NOSTOP;
0187 if (cnt)
0188 xmit |= BT878_I2C_NOSTART;
0189
0190 if (i2c_debug) {
0191 if (!(xmit & BT878_I2C_NOSTART))
0192 pr_cont(" <R %02x", (msg->addr << 1) +1);
0193 }
0194
0195 btwrite(xmit, BT848_I2C);
0196 retval = bttv_i2c_wait_done(btv);
0197 if (retval < 0)
0198 goto err;
0199 if (retval == 0)
0200 goto eio;
0201 msg->buf[cnt] = ((u32)btread(BT848_I2C) >> 8) & 0xff;
0202 if (i2c_debug) {
0203 pr_cont(" =%02x", msg->buf[cnt]);
0204 }
0205 if (i2c_debug && !(xmit & BT878_I2C_NOSTOP))
0206 pr_cont(" >\n");
0207 }
0208
0209
0210 return msg->len;
0211
0212 eio:
0213 retval = -EIO;
0214 err:
0215 if (i2c_debug)
0216 pr_cont(" ERR: %d\n",retval);
0217 return retval;
0218 }
0219
0220 static int bttv_i2c_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg *msgs, int num)
0221 {
0222 struct v4l2_device *v4l2_dev = i2c_get_adapdata(i2c_adap);
0223 struct bttv *btv = to_bttv(v4l2_dev);
0224 int retval = 0;
0225 int i;
0226
0227 if (i2c_debug)
0228 pr_debug("bt-i2c:");
0229
0230 btwrite(BT848_INT_I2CDONE|BT848_INT_RACK, BT848_INT_STAT);
0231 for (i = 0 ; i < num; i++) {
0232 if (msgs[i].flags & I2C_M_RD) {
0233
0234 retval = bttv_i2c_readbytes(btv, &msgs[i], i+1 == num);
0235 if (retval < 0)
0236 goto err;
0237 } else {
0238
0239 retval = bttv_i2c_sendbytes(btv, &msgs[i], i+1 == num);
0240 if (retval < 0)
0241 goto err;
0242 }
0243 }
0244 return num;
0245
0246 err:
0247 return retval;
0248 }
0249
0250 static const struct i2c_algorithm bttv_algo = {
0251 .master_xfer = bttv_i2c_xfer,
0252 .functionality = functionality,
0253 };
0254
0255
0256
0257
0258
0259 int bttv_I2CRead(struct bttv *btv, unsigned char addr, char *probe_for)
0260 {
0261 unsigned char buffer = 0;
0262
0263 if (0 != btv->i2c_rc)
0264 return -1;
0265 if (bttv_verbose && NULL != probe_for)
0266 pr_info("%d: i2c: checking for %s @ 0x%02x... ",
0267 btv->c.nr, probe_for, addr);
0268 btv->i2c_client.addr = addr >> 1;
0269 if (1 != i2c_master_recv(&btv->i2c_client, &buffer, 1)) {
0270 if (NULL != probe_for) {
0271 if (bttv_verbose)
0272 pr_cont("not found\n");
0273 } else
0274 pr_warn("%d: i2c read 0x%x: error\n",
0275 btv->c.nr, addr);
0276 return -1;
0277 }
0278 if (bttv_verbose && NULL != probe_for)
0279 pr_cont("found\n");
0280 return buffer;
0281 }
0282
0283
0284 int bttv_I2CWrite(struct bttv *btv, unsigned char addr, unsigned char b1,
0285 unsigned char b2, int both)
0286 {
0287 unsigned char buffer[2];
0288 int bytes = both ? 2 : 1;
0289
0290 if (0 != btv->i2c_rc)
0291 return -1;
0292 btv->i2c_client.addr = addr >> 1;
0293 buffer[0] = b1;
0294 buffer[1] = b2;
0295 if (bytes != i2c_master_send(&btv->i2c_client, buffer, bytes))
0296 return -1;
0297 return 0;
0298 }
0299
0300
0301 void bttv_readee(struct bttv *btv, unsigned char *eedata, int addr)
0302 {
0303 memset(eedata, 0, 256);
0304 if (0 != btv->i2c_rc)
0305 return;
0306 btv->i2c_client.addr = addr >> 1;
0307 tveeprom_read(&btv->i2c_client, eedata, 256);
0308 }
0309
0310 static char *i2c_devs[128] = {
0311 [ 0x1c >> 1 ] = "lgdt330x",
0312 [ 0x30 >> 1 ] = "IR (hauppauge)",
0313 [ 0x80 >> 1 ] = "msp34xx",
0314 [ 0x86 >> 1 ] = "tda9887",
0315 [ 0xa0 >> 1 ] = "eeprom",
0316 [ 0xc0 >> 1 ] = "tuner (analog)",
0317 [ 0xc2 >> 1 ] = "tuner (analog)",
0318 };
0319
0320 static void do_i2c_scan(char *name, struct i2c_client *c)
0321 {
0322 unsigned char buf;
0323 int i,rc;
0324
0325 for (i = 0; i < ARRAY_SIZE(i2c_devs); i++) {
0326 c->addr = i;
0327 rc = i2c_master_recv(c,&buf,0);
0328 if (rc < 0)
0329 continue;
0330 pr_info("%s: i2c scan: found device @ 0x%x [%s]\n",
0331 name, i << 1, i2c_devs[i] ? i2c_devs[i] : "???");
0332 }
0333 }
0334
0335
0336 int init_bttv_i2c(struct bttv *btv)
0337 {
0338 strscpy(btv->i2c_client.name, "bttv internal", I2C_NAME_SIZE);
0339
0340 if (i2c_hw)
0341 btv->use_i2c_hw = 1;
0342 if (btv->use_i2c_hw) {
0343
0344 strscpy(btv->c.i2c_adap.name, "bt878",
0345 sizeof(btv->c.i2c_adap.name));
0346 btv->c.i2c_adap.algo = &bttv_algo;
0347 } else {
0348
0349
0350 if (i2c_udelay<5)
0351 i2c_udelay=5;
0352
0353 strscpy(btv->c.i2c_adap.name, "bttv",
0354 sizeof(btv->c.i2c_adap.name));
0355 btv->i2c_algo = bttv_i2c_algo_bit_template;
0356 btv->i2c_algo.udelay = i2c_udelay;
0357 btv->i2c_algo.data = btv;
0358 btv->c.i2c_adap.algo_data = &btv->i2c_algo;
0359 }
0360 btv->c.i2c_adap.owner = THIS_MODULE;
0361
0362 btv->c.i2c_adap.dev.parent = &btv->c.pci->dev;
0363 snprintf(btv->c.i2c_adap.name, sizeof(btv->c.i2c_adap.name),
0364 "bt%d #%d [%s]", btv->id, btv->c.nr,
0365 btv->use_i2c_hw ? "hw" : "sw");
0366
0367 i2c_set_adapdata(&btv->c.i2c_adap, &btv->c.v4l2_dev);
0368 btv->i2c_client.adapter = &btv->c.i2c_adap;
0369
0370
0371 if (btv->use_i2c_hw) {
0372 btv->i2c_rc = i2c_add_adapter(&btv->c.i2c_adap);
0373 } else {
0374 bttv_bit_setscl(btv,1);
0375 bttv_bit_setsda(btv,1);
0376 btv->i2c_rc = i2c_bit_add_bus(&btv->c.i2c_adap);
0377 }
0378 if (0 == btv->i2c_rc && i2c_scan)
0379 do_i2c_scan(btv->c.v4l2_dev.name, &btv->i2c_client);
0380
0381 return btv->i2c_rc;
0382 }
0383
0384 int fini_bttv_i2c(struct bttv *btv)
0385 {
0386 if (btv->i2c_rc == 0)
0387 i2c_del_adapter(&btv->c.i2c_adap);
0388
0389 return 0;
0390 }