cx88-i2c.c 6.7 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 36
    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/moduleparam.h>
#include <linux/init.h>

#include <asm/io.h>

#include "cx88.h"
37
#include <media/v4l2-common.h>
L
Linus Torvalds 已提交
38 39 40 41 42 43 44 45 46

static unsigned int i2c_debug = 0;
module_param(i2c_debug, int, 0644);
MODULE_PARM_DESC(i2c_debug,"enable debug messages [i2c]");

static unsigned int i2c_scan = 0;
module_param(i2c_scan, int, 0444);
MODULE_PARM_DESC(i2c_scan,"scan i2c bus at insmod time");

47 48 49 50 51
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 已提交
52 53 54 55 56
#define dprintk(level,fmt, arg...)	if (i2c_debug >= level) \
	printk(KERN_DEBUG "%s: " fmt, core->name , ## arg)

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

A
Adrian Bunk 已提交
57
static void cx8800_bit_setscl(void *data, int state)
L
Linus Torvalds 已提交
58 59 60 61 62 63 64 65 66 67 68
{
	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 已提交
69
static void cx8800_bit_setsda(void *data, int state)
L
Linus Torvalds 已提交
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 102
{
	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;
}

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

static int attach_inform(struct i2c_client *client)
{
103
	struct tuner_setup tun_setup;
L
Linus Torvalds 已提交
104 105
	struct cx88_core *core = i2c_get_adapdata(client->adapter);

M
Mauro Carvalho Chehab 已提交
106
	dprintk(1, "%s i2c attach [addr=0x%x,client=%s]\n",
107
		client->driver->driver.name, client->addr, client->name);
L
Linus Torvalds 已提交
108 109 110
	if (!client->driver->command)
		return 0;

111
	if (core->radio_type != UNSET) {
M
Mauro Carvalho Chehab 已提交
112 113 114 115 116 117 118
		if ((core->radio_addr==ADDR_UNSET)||(core->radio_addr==client->addr)) {
			tun_setup.mode_mask = T_RADIO;
			tun_setup.type = core->radio_type;
			tun_setup.addr = core->radio_addr;

			client->driver->command (client, TUNER_SET_TYPE_ADDR, &tun_setup);
		}
119 120
	}
	if (core->tuner_type != UNSET) {
M
Mauro Carvalho Chehab 已提交
121 122 123 124 125 126 127 128
		if ((core->tuner_addr==ADDR_UNSET)||(core->tuner_addr==client->addr)) {

			tun_setup.mode_mask = T_ANALOG_TV;
			tun_setup.type = core->tuner_type;
			tun_setup.addr = core->tuner_addr;

			client->driver->command (client,TUNER_SET_TYPE_ADDR, &tun_setup);
		}
129
	}
130

L
Linus Torvalds 已提交
131 132 133 134 135 136 137 138 139
	if (core->tda9887_conf)
		client->driver->command(client, TDA9887_SET_CONFIG, &core->tda9887_conf);
	return 0;
}

static int detach_inform(struct i2c_client *client)
{
	struct cx88_core *core = i2c_get_adapdata(client->adapter);

140
	dprintk(1, "i2c detach [client=%s]\n", client->name);
L
Linus Torvalds 已提交
141 142 143 144 145 146 147
	return 0;
}

void cx88_call_i2c_clients(struct cx88_core *core, unsigned int cmd, void *arg)
{
	if (0 != core->i2c_rc)
		return;
148

149
#if defined(CONFIG_VIDEO_BUF_DVB) || defined(CONFIG_VIDEO_BUF_DVB_MODULE)
150
	if ( (core->dvbdev) && (core->dvbdev->dvb.frontend) ) {
151 152
		if (core->dvbdev->dvb.frontend->ops.i2c_gate_ctrl)
			core->dvbdev->dvb.frontend->ops.i2c_gate_ctrl(core->dvbdev->dvb.frontend, 1);
153 154 155

		i2c_clients_command(&core->i2c_adap, cmd, arg);

156 157
		if (core->dvbdev->dvb.frontend->ops.i2c_gate_ctrl)
			core->dvbdev->dvb.frontend->ops.i2c_gate_ctrl(core->dvbdev->dvb.frontend, 0);
158
	} else
159
#endif
160
		i2c_clients_command(&core->i2c_adap, cmd, arg);
L
Linus Torvalds 已提交
161 162
}

163
static const struct i2c_algo_bit_data cx8800_i2c_algo_template = {
L
Linus Torvalds 已提交
164 165 166 167 168 169 170 171 172 173 174
	.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] = {
175
	[ 0x1c >> 1 ] = "lgdt330x",
L
Linus Torvalds 已提交
176 177 178 179 180 181 182 183 184 185 186
	[ 0x86 >> 1 ] = "tda9887/cx22702",
	[ 0xa0 >> 1 ] = "eeprom",
	[ 0xc0 >> 1 ] = "tuner (analog)",
	[ 0xc2 >> 1 ] = "tuner (analog/dvb)",
};

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

187
	for (i = 0; i < ARRAY_SIZE(i2c_devs); i++) {
L
Linus Torvalds 已提交
188 189 190 191 192 193 194 195 196 197 198 199
		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)
{
200 201 202 203
	/* Prevents usage of invalid delay values */
	if (i2c_udelay<5)
		i2c_udelay=5;

L
Linus Torvalds 已提交
204 205 206 207 208
	memcpy(&core->i2c_algo, &cx8800_i2c_algo_template,
	       sizeof(core->i2c_algo));

	if (core->tuner_type != TUNER_ABSENT)
		core->i2c_adap.class |= I2C_CLASS_TV_ANALOG;
209
	if (cx88_boards[core->board].mpeg & CX88_MPEG_DVB)
L
Linus Torvalds 已提交
210 211 212 213
		core->i2c_adap.class |= I2C_CLASS_TV_DIGITAL;

	core->i2c_adap.dev.parent = &pci->dev;
	strlcpy(core->i2c_adap.name,core->name,sizeof(core->i2c_adap.name));
214 215 216 217 218
	core->i2c_adap.owner = THIS_MODULE;
	core->i2c_adap.id = I2C_HW_B_CX2388x;
	core->i2c_adap.client_register = attach_inform;
	core->i2c_adap.client_unregister = detach_inform;
	core->i2c_algo.udelay = i2c_udelay;
219 220 221 222
	core->i2c_algo.data = core;
	i2c_set_adapdata(&core->i2c_adap,core);
	core->i2c_adap.algo_data = &core->i2c_algo;
	core->i2c_client.adapter = &core->i2c_adap;
223
	strlcpy(core->i2c_client.name, "cx88xx internal", I2C_NAME_SIZE);
L
Linus Torvalds 已提交
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246

	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) {
		dprintk(1, "i2c register ok\n");
		if (i2c_scan)
			do_i2c_scan(core->name,&core->i2c_client);
	} else
		printk("%s: i2c register FAILED\n", core->name);
	return core->i2c_rc;
}

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

EXPORT_SYMBOL(cx88_call_i2c_clients);

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