cx88-i2c.c 5.9 KB
Newer Older
1

L
Linus Torvalds 已提交
2 3 4 5 6
/*

    cx88-i2c.c  --  all the i2c code is here

    Copyright (C) 1996,97,98 Ralph  Metzler (rjkm@thp.uni-koeln.de)
7
			   & Marcus Metzler (mocm@thp.uni-koeln.de)
L
Linus Torvalds 已提交
8 9 10
    (c) 2002 Yurij Sysoev <yurij@naturesoft.net>
    (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 35
    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 <asm/io.h>

#include "cx88.h"
36
#include <media/v4l2-common.h>
L
Linus Torvalds 已提交
37

38
static unsigned int i2c_debug;
L
Linus Torvalds 已提交
39 40 41
module_param(i2c_debug, int, 0644);
MODULE_PARM_DESC(i2c_debug,"enable debug messages [i2c]");

42
static unsigned int i2c_scan;
L
Linus Torvalds 已提交
43 44 45
module_param(i2c_scan, int, 0444);
MODULE_PARM_DESC(i2c_scan,"scan i2c bus at insmod time");

46 47 48 49 50
static unsigned int i2c_udelay = 5;
module_param(i2c_udelay, int, 0644);
MODULE_PARM_DESC(i2c_udelay,"i2c delay at insmod time, in usecs "
		"(should be 5 or higher). Lower value means higher bus speed.");

L
Linus Torvalds 已提交
51 52 53 54 55
#define dprintk(level,fmt, arg...)	if (i2c_debug >= level) \
	printk(KERN_DEBUG "%s: " fmt, core->name , ## arg)

/* ----------------------------------------------------------------------- */

A
Adrian Bunk 已提交
56
static void cx8800_bit_setscl(void *data, int state)
L
Linus Torvalds 已提交
57 58 59 60 61 62 63 64 65 66 67
{
	struct cx88_core *core = data;

	if (state)
		core->i2c_state |= 0x02;
	else
		core->i2c_state &= ~0x02;
	cx_write(MO_I2C, core->i2c_state);
	cx_read(MO_I2C);
}

A
Adrian Bunk 已提交
68
static void cx8800_bit_setsda(void *data, int state)
L
Linus Torvalds 已提交
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
{
	struct cx88_core *core = data;

	if (state)
		core->i2c_state |= 0x01;
	else
		core->i2c_state &= ~0x01;
	cx_write(MO_I2C, core->i2c_state);
	cx_read(MO_I2C);
}

static int cx8800_bit_getscl(void *data)
{
	struct cx88_core *core = data;
	u32 state;

	state = cx_read(MO_I2C);
	return state & 0x02 ? 1 : 0;
}

static int cx8800_bit_getsda(void *data)
{
	struct cx88_core *core = data;
	u32 state;

	state = cx_read(MO_I2C);
	return state & 0x01;
}

/* ----------------------------------------------------------------------- */

100
static const struct i2c_algo_bit_data cx8800_i2c_algo_template = {
L
Linus Torvalds 已提交
101 102 103 104 105 106 107 108 109 110 111
	.setsda  = cx8800_bit_setsda,
	.setscl  = cx8800_bit_setscl,
	.getsda  = cx8800_bit_getsda,
	.getscl  = cx8800_bit_getscl,
	.udelay  = 16,
	.timeout = 200,
};

/* ----------------------------------------------------------------------- */

static char *i2c_devs[128] = {
112
	[ 0x1c >> 1 ] = "lgdt330x",
L
Linus Torvalds 已提交
113 114 115 116
	[ 0x86 >> 1 ] = "tda9887/cx22702",
	[ 0xa0 >> 1 ] = "eeprom",
	[ 0xc0 >> 1 ] = "tuner (analog)",
	[ 0xc2 >> 1 ] = "tuner (analog/dvb)",
117
	[ 0xc8 >> 1 ] = "xc5000",
L
Linus Torvalds 已提交
118 119 120 121 122 123 124
};

static void do_i2c_scan(char *name, struct i2c_client *c)
{
	unsigned char buf;
	int i,rc;

125
	for (i = 0; i < ARRAY_SIZE(i2c_devs); i++) {
L
Linus Torvalds 已提交
126 127 128 129 130 131 132 133 134 135 136 137
		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 cx88_i2c_init(struct cx88_core *core, struct pci_dev *pci)
{
138 139 140 141
	/* Prevents usage of invalid delay values */
	if (i2c_udelay<5)
		i2c_udelay=5;

L
Linus Torvalds 已提交
142 143 144 145 146 147
	memcpy(&core->i2c_algo, &cx8800_i2c_algo_template,
	       sizeof(core->i2c_algo));


	core->i2c_adap.dev.parent = &pci->dev;
	strlcpy(core->i2c_adap.name,core->name,sizeof(core->i2c_adap.name));
148 149 150
	core->i2c_adap.owner = THIS_MODULE;
	core->i2c_adap.id = I2C_HW_B_CX2388x;
	core->i2c_algo.udelay = i2c_udelay;
151
	core->i2c_algo.data = core;
152
	i2c_set_adapdata(&core->i2c_adap, &core->v4l2_dev);
153 154
	core->i2c_adap.algo_data = &core->i2c_algo;
	core->i2c_client.adapter = &core->i2c_adap;
155
	strlcpy(core->i2c_client.name, "cx88xx internal", I2C_NAME_SIZE);
L
Linus Torvalds 已提交
156 157 158 159 160 161

	cx8800_bit_setscl(core,1);
	cx8800_bit_setsda(core,1);

	core->i2c_rc = i2c_bit_add_bus(&core->i2c_adap);
	if (0 == core->i2c_rc) {
162 163 164 165 166
		static u8 tuner_data[] =
			{ 0x0b, 0xdc, 0x86, 0x52 };
		static struct i2c_msg tuner_msg =
			{ .flags = 0, .addr = 0xc2 >> 1, .buf = tuner_data, .len = 4 };

L
Linus Torvalds 已提交
167
		dprintk(1, "i2c register ok\n");
168 169 170 171 172 173 174 175 176 177 178
		switch( core->boardnr ) {
			case CX88_BOARD_HAUPPAUGE_HVR1300:
			case CX88_BOARD_HAUPPAUGE_HVR3000:
			case CX88_BOARD_HAUPPAUGE_HVR4000:
				printk("%s: i2c init: enabling analog demod on HVR1300/3000/4000 tuner\n",
					core->name);
				i2c_transfer(core->i2c_client.adapter, &tuner_msg, 1);
				break;
			default:
				break;
		}
L
Linus Torvalds 已提交
179 180 181 182
		if (i2c_scan)
			do_i2c_scan(core->name,&core->i2c_client);
	} else
		printk("%s: i2c register FAILED\n", core->name);
183

184 185 186 187 188
	return core->i2c_rc;
}

void cx88_i2c_init_ir(struct cx88_core *core)
{
189 190 191 192 193 194 195
	/* Instantiate the IR receiver device, if present */
	if (0 == core->i2c_rc) {
		struct i2c_board_info info;
		const unsigned short addr_list[] = {
			0x18, 0x6b, 0x71,
			I2C_CLIENT_END
		};
196
		const unsigned short *addrp;
197 198 199

		memset(&info, 0, sizeof(struct i2c_board_info));
		strlcpy(info.type, "ir_video", I2C_NAME_SIZE);
200 201 202 203 204 205 206 207 208 209 210 211 212 213
		/*
		 * We can't call i2c_new_probed_device() because it uses
		 * quick writes for probing and at least some R receiver
		 * devices only reply to reads.
		 */
		for (addrp = addr_list; *addrp != I2C_CLIENT_END; addrp++) {
			if (i2c_smbus_xfer(&core->i2c_adap, *addrp, 0,
					   I2C_SMBUS_READ, 0,
					   I2C_SMBUS_QUICK, NULL) >= 0) {
				info.addr = *addrp;
				i2c_new_device(&core->i2c_adap, &info);
				break;
			}
		}
214
	}
L
Linus Torvalds 已提交
215 216 217 218 219 220 221 222 223
}

/* ----------------------------------------------------------------------- */

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