bttv-i2c.c 11.1 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7
/*

    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)
8
			   & Marcus Metzler (mocm@thp.uni-koeln.de)
L
Linus Torvalds 已提交
9 10
    (c) 1999-2003 Gerd Knorr <kraxel@bytesex.org>

11 12 13
    (c) 2005 Mauro Carvalho Chehab <mchehab@infradead.org>
	- Multituner support and i2c address binding

L
Linus Torvalds 已提交
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    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/init.h>
#include <linux/delay.h>

#include "bttvp.h"
35
#include <media/v4l2-common.h>
36 37
#include <linux/jiffies.h>
#include <asm/io.h>
L
Linus Torvalds 已提交
38 39 40

static int attach_inform(struct i2c_client *client);

41 42 43
static int i2c_debug;
static int i2c_hw;
static int i2c_scan;
L
Linus Torvalds 已提交
44
module_param(i2c_debug, int, 0644);
45
MODULE_PARM_DESC(i2c_hw,"configure i2c debug level");
L
Linus Torvalds 已提交
46
module_param(i2c_hw,    int, 0444);
47 48
MODULE_PARM_DESC(i2c_hw,"force use of hardware i2c support, "
			"instead of software bitbang");
L
Linus Torvalds 已提交
49 50 51
module_param(i2c_scan,  int, 0444);
MODULE_PARM_DESC(i2c_scan,"scan i2c bus at insmod time");

52 53 54 55 56
static unsigned int i2c_udelay = 5;
module_param(i2c_udelay, int, 0444);
MODULE_PARM_DESC(i2c_udelay,"soft i2c delay at insmod time, in usecs "
		"(should be 5 or higher). Lower value means higher bus speed.");

L
Linus Torvalds 已提交
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
/* ----------------------------------------------------------------------- */
/* I2C functions - bitbanging adapter (software i2c)                       */

static void bttv_bit_setscl(void *data, int state)
{
	struct bttv *btv = (struct bttv*)data;

	if (state)
		btv->i2c_state |= 0x02;
	else
		btv->i2c_state &= ~0x02;
	btwrite(btv->i2c_state, BT848_I2C);
	btread(BT848_I2C);
}

static void bttv_bit_setsda(void *data, int state)
{
	struct bttv *btv = (struct bttv*)data;

	if (state)
		btv->i2c_state |= 0x01;
	else
		btv->i2c_state &= ~0x01;
	btwrite(btv->i2c_state, BT848_I2C);
	btread(BT848_I2C);
}

static int bttv_bit_getscl(void *data)
{
	struct bttv *btv = (struct bttv*)data;
	int state;

	state = btread(BT848_I2C) & 0x02 ? 1 : 0;
	return state;
}

static int bttv_bit_getsda(void *data)
{
	struct bttv *btv = (struct bttv*)data;
	int state;

	state = btread(BT848_I2C) & 0x01;
	return state;
}

102
static struct i2c_algo_bit_data __devinitdata bttv_i2c_algo_bit_template = {
L
Linus Torvalds 已提交
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
	.setsda  = bttv_bit_setsda,
	.setscl  = bttv_bit_setscl,
	.getsda  = bttv_bit_getsda,
	.getscl  = bttv_bit_getscl,
	.udelay  = 16,
	.timeout = 200,
};

/* ----------------------------------------------------------------------- */
/* I2C functions - hardware i2c                                            */

static u32 functionality(struct i2c_adapter *adap)
{
	return I2C_FUNC_SMBUS_EMUL;
}

static int
bttv_i2c_wait_done(struct bttv *btv)
{
	int rc = 0;

124 125 126 127 128
	/* timeout */
	if (wait_event_interruptible_timeout(btv->i2c_queue,
		btv->i2c_done, msecs_to_jiffies(85)) == -ERESTARTSYS)

	rc = -EIO;
L
Linus Torvalds 已提交
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228

	if (btv->i2c_done & BT848_INT_RACK)
		rc = 1;
	btv->i2c_done = 0;
	return rc;
}

#define I2C_HW (BT878_I2C_MODE  | BT848_I2C_SYNC |\
		BT848_I2C_SCL | BT848_I2C_SDA)

static int
bttv_i2c_sendbytes(struct bttv *btv, const struct i2c_msg *msg, int last)
{
	u32 xmit;
	int retval,cnt;

	/* sanity checks */
	if (0 == msg->len)
		return -EINVAL;

	/* start, address + first byte */
	xmit = (msg->addr << 25) | (msg->buf[0] << 16) | I2C_HW;
	if (msg->len > 1 || !last)
		xmit |= BT878_I2C_NOSTOP;
	btwrite(xmit, BT848_I2C);
	retval = bttv_i2c_wait_done(btv);
	if (retval < 0)
		goto err;
	if (retval == 0)
		goto eio;
	if (i2c_debug) {
		printk(" <W %02x %02x", msg->addr << 1, msg->buf[0]);
		if (!(xmit & BT878_I2C_NOSTOP))
			printk(" >\n");
	}

	for (cnt = 1; cnt < msg->len; cnt++ ) {
		/* following bytes */
		xmit = (msg->buf[cnt] << 24) | I2C_HW | BT878_I2C_NOSTART;
		if (cnt < msg->len-1 || !last)
			xmit |= BT878_I2C_NOSTOP;
		btwrite(xmit, BT848_I2C);
		retval = bttv_i2c_wait_done(btv);
		if (retval < 0)
			goto err;
		if (retval == 0)
			goto eio;
		if (i2c_debug) {
			printk(" %02x", msg->buf[cnt]);
			if (!(xmit & BT878_I2C_NOSTOP))
				printk(" >\n");
		}
	}
	return msg->len;

 eio:
	retval = -EIO;
 err:
	if (i2c_debug)
		printk(" ERR: %d\n",retval);
	return retval;
}

static int
bttv_i2c_readbytes(struct bttv *btv, const struct i2c_msg *msg, int last)
{
	u32 xmit;
	u32 cnt;
	int retval;

	for(cnt = 0; cnt < msg->len; cnt++) {
		xmit = (msg->addr << 25) | (1 << 24) | I2C_HW;
		if (cnt < msg->len-1)
			xmit |= BT848_I2C_W3B;
		if (cnt < msg->len-1 || !last)
			xmit |= BT878_I2C_NOSTOP;
		if (cnt)
			xmit |= BT878_I2C_NOSTART;
		btwrite(xmit, BT848_I2C);
		retval = bttv_i2c_wait_done(btv);
		if (retval < 0)
			goto err;
		if (retval == 0)
			goto eio;
		msg->buf[cnt] = ((u32)btread(BT848_I2C) >> 8) & 0xff;
		if (i2c_debug) {
			if (!(xmit & BT878_I2C_NOSTART))
				printk(" <R %02x", (msg->addr << 1) +1);
			printk(" =%02x", msg->buf[cnt]);
			if (!(xmit & BT878_I2C_NOSTOP))
				printk(" >\n");
		}
	}
	return msg->len;

 eio:
	retval = -EIO;
 err:
	if (i2c_debug)
		printk(" ERR: %d\n",retval);
229
	return retval;
L
Linus Torvalds 已提交
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
}

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;
	int i;

	if (i2c_debug)
		printk("bt-i2c:");
	btwrite(BT848_INT_I2CDONE|BT848_INT_RACK, BT848_INT_STAT);
	for (i = 0 ; i < num; i++) {
		if (msgs[i].flags & I2C_M_RD) {
			/* read */
			retval = bttv_i2c_readbytes(btv, &msgs[i], i+1 == num);
			if (retval < 0)
				goto err;
		} else {
			/* write */
			retval = bttv_i2c_sendbytes(btv, &msgs[i], i+1 == num);
			if (retval < 0)
				goto err;
		}
	}
	return num;

 err:
	return retval;
}

260
static const struct i2c_algorithm bttv_algo = {
L
Linus Torvalds 已提交
261 262 263 264 265 266 267 268 269
	.master_xfer   = bttv_i2c_xfer,
	.functionality = functionality,
};

/* ----------------------------------------------------------------------- */
/* I2C functions - common stuff                                            */

static int attach_inform(struct i2c_client *client)
{
270
	struct bttv *btv = i2c_get_adapdata(client->adapter);
271 272 273
	int addr=ADDR_UNSET;


274 275
	if (ADDR_UNSET != bttv_tvcards[btv->c.type].tuner_addr)
		addr = bttv_tvcards[btv->c.type].tuner_addr;
276

L
Linus Torvalds 已提交
277

M
Mauro Carvalho Chehab 已提交
278 279
	if (bttv_debug)
		printk(KERN_DEBUG "bttv%d: %s i2c attach [addr=0x%x,client=%s]\n",
280
			btv->c.nr, client->driver->driver.name, client->addr,
281
			client->name);
M
Mauro Carvalho Chehab 已提交
282 283 284
	if (!client->driver->command)
		return 0;

285 286 287 288
	if (client->driver->id == I2C_DRIVERID_MSP3400)
		btv->i2c_msp34xx_client = client;
	if (client->driver->id == I2C_DRIVERID_TVAUDIO)
		btv->i2c_tvaudio_client = client;
289
	if (btv->tuner_type != TUNER_ABSENT) {
290
		struct tuner_setup tun_setup;
M
Mauro Carvalho Chehab 已提交
291

292
		if (addr == ADDR_UNSET || addr == client->addr) {
293 294
			tun_setup.mode_mask = T_ANALOG_TV | T_DIGITAL_TV | T_RADIO;
			tun_setup.type = btv->tuner_type;
295
			tun_setup.addr = addr;
296
			bttv_call_i2c_clients(btv, TUNER_SET_TYPE_ADDR, &tun_setup);
297
		}
298

M
Mauro Carvalho Chehab 已提交
299 300
	}

301
	return 0;
L
Linus Torvalds 已提交
302 303 304 305 306 307 308 309 310 311 312 313 314
}

void bttv_call_i2c_clients(struct bttv *btv, unsigned int cmd, void *arg)
{
	if (0 != btv->i2c_rc)
		return;
	i2c_clients_command(&btv->c.i2c_adap, cmd, arg);
}


/* read I2C */
int bttv_I2CRead(struct bttv *btv, unsigned char addr, char *probe_for)
{
315
	unsigned char buffer = 0;
L
Linus Torvalds 已提交
316 317 318 319 320 321

	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);
322 323
	btv->i2c_client.addr = addr >> 1;
	if (1 != i2c_master_recv(&btv->i2c_client, &buffer, 1)) {
L
Linus Torvalds 已提交
324 325 326 327 328 329
		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);
330
		return -1;
L
Linus Torvalds 已提交
331 332 333
	}
	if (bttv_verbose && NULL != probe_for)
		printk("found\n");
334
	return buffer;
L
Linus Torvalds 已提交
335 336 337 338
}

/* write I2C */
int bttv_I2CWrite(struct bttv *btv, unsigned char addr, unsigned char b1,
339
		    unsigned char b2, int both)
L
Linus Torvalds 已提交
340
{
341 342
	unsigned char buffer[2];
	int bytes = both ? 2 : 1;
L
Linus Torvalds 已提交
343 344 345

	if (0 != btv->i2c_rc)
		return -1;
346 347 348 349
	btv->i2c_client.addr = addr >> 1;
	buffer[0] = b1;
	buffer[1] = b2;
	if (bytes != i2c_master_send(&btv->i2c_client, buffer, bytes))
L
Linus Torvalds 已提交
350
		return -1;
351
	return 0;
L
Linus Torvalds 已提交
352 353 354 355 356
}

/* read EEPROM content */
void __devinit bttv_readee(struct bttv *btv, unsigned char *eedata, int addr)
{
G
Gerd Knorr 已提交
357 358 359
	memset(eedata, 0, 256);
	if (0 != btv->i2c_rc)
		return;
L
Linus Torvalds 已提交
360 361 362 363 364
	btv->i2c_client.addr = addr >> 1;
	tveeprom_read(&btv->i2c_client, eedata, 256);
}

static char *i2c_devs[128] = {
365
	[ 0x1c >> 1 ] = "lgdt330x",
L
Linus Torvalds 已提交
366 367 368 369 370 371 372 373 374 375 376 377 378
	[ 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;

379
	for (i = 0; i < ARRAY_SIZE(i2c_devs); i++) {
L
Linus Torvalds 已提交
380 381 382 383 384 385 386 387 388 389 390 391
		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] : "???");
	}
}

/* init + register i2c algo-bit adapter */
int __devinit init_bttv_i2c(struct bttv *btv)
{
392
	strlcpy(btv->i2c_client.name, "bttv internal", I2C_NAME_SIZE);
L
Linus Torvalds 已提交
393 394 395 396 397

	if (i2c_hw)
		btv->use_i2c_hw = 1;
	if (btv->use_i2c_hw) {
		/* bt878 */
398 399 400 401
		strlcpy(btv->c.i2c_adap.name, "bt878",
			sizeof(btv->c.i2c_adap.name));
		btv->c.i2c_adap.id = I2C_HW_B_BT848;	/* FIXME */
		btv->c.i2c_adap.algo = &bttv_algo;
L
Linus Torvalds 已提交
402 403
	} else {
		/* bt848 */
404 405 406 407
	/* Prevents usage of invalid delay values */
		if (i2c_udelay<5)
			i2c_udelay=5;

408 409 410
		strlcpy(btv->c.i2c_adap.name, "bttv",
			sizeof(btv->c.i2c_adap.name));
		btv->c.i2c_adap.id = I2C_HW_B_BT848;
L
Linus Torvalds 已提交
411 412
		memcpy(&btv->i2c_algo, &bttv_i2c_algo_bit_template,
		       sizeof(bttv_i2c_algo_bit_template));
413
		btv->i2c_algo.udelay = i2c_udelay;
L
Linus Torvalds 已提交
414 415 416
		btv->i2c_algo.data = btv;
		btv->c.i2c_adap.algo_data = &btv->i2c_algo;
	}
417 418 419
	btv->c.i2c_adap.owner = THIS_MODULE;
	btv->c.i2c_adap.class = I2C_CLASS_TV_ANALOG;
	btv->c.i2c_adap.client_register = attach_inform;
L
Linus Torvalds 已提交
420 421 422 423 424 425

	btv->c.i2c_adap.dev.parent = &btv->c.pci->dev;
	snprintf(btv->c.i2c_adap.name, sizeof(btv->c.i2c_adap.name),
		 "bt%d #%d [%s]", btv->id, btv->c.nr,
		 btv->use_i2c_hw ? "hw" : "sw");

426 427
	i2c_set_adapdata(&btv->c.i2c_adap, btv);
	btv->i2c_client.adapter = &btv->c.i2c_adap;
L
Linus Torvalds 已提交
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450

	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);
	} else {
		bttv_bit_setscl(btv,1);
		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;
}

int __devexit fini_bttv_i2c(struct bttv *btv)
{
	if (0 != btv->i2c_rc)
		return 0;

451
	return i2c_del_adapter(&btv->c.i2c_adap);
L
Linus Torvalds 已提交
452 453 454 455 456 457 458
}

/*
 * Local variables:
 * c-basic-offset: 8
 * End:
 */