flexcop-i2c.c 7.86 KB
Newer Older
1
/*
2
 * Linux driver for digital TV devices equipped with B2C2 FlexcopII(b)/III
3
 * flexcop-i2c.c - flexcop internal 2Wire bus (I2C) and dvb i2c initialization
4
 * see flexcop.c for copyright information
5 6 7 8 9
 */
#include "flexcop.h"

#define FC_MAX_I2C_RETRIES 100000

10 11
static int flexcop_i2c_operation(struct flexcop_device *fc,
		flexcop_ibi_value *r100)
12
{
13
	int i;
14 15 16 17 18 19 20 21 22 23 24 25
	flexcop_ibi_value r;

	r100->tw_sm_c_100.working_start = 1;
	deb_i2c("r100 before: %08x\n",r100->raw);

	fc->write_ibi_reg(fc, tw_sm_c_100, ibi_zero);
	fc->write_ibi_reg(fc, tw_sm_c_100, *r100); /* initiating i2c operation */

	for (i = 0; i < FC_MAX_I2C_RETRIES; i++) {
		r = fc->read_ibi_reg(fc, tw_sm_c_100);

		if (!r.tw_sm_c_100.no_base_addr_ack_error) {
26
			if (r.tw_sm_c_100.st_done) {
27 28 29 30 31 32
				*r100 = r;
				deb_i2c("i2c success\n");
				return 0;
			}
		} else {
			deb_i2c("suffering from an i2c ack_error\n");
33
			return -EREMOTEIO;
34 35
		}
	}
36 37
	deb_i2c("tried %d times i2c operation, "
			"never finished or too many ack errors.\n", i);
38 39 40
	return -EREMOTEIO;
}

41
static int flexcop_i2c_read4(struct flexcop_i2c_adapter *i2c,
42
		flexcop_ibi_value r100, u8 *buf)
43 44
{
	flexcop_ibi_value r104;
45 46
	int len = r100.tw_sm_c_100.total_bytes,
		/* remember total_bytes is buflen-1 */
47 48
		ret;

49 50 51 52 53 54 55 56 57 58 59 60
	/* work-around to have CableStar2 and SkyStar2 rev 2.7 work
	 * correctly:
	 *
	 * the ITD1000 is behind an i2c-gate which closes automatically
	 * after an i2c-transaction the STV0297 needs 2 consecutive reads
	 * one with no_base_addr = 0 and one with 1
	 *
	 * those two work-arounds are conflictin: we check for the card
	 * type, it is set when probing the ITD1000 */
	if (i2c->fc->dev_type == FC_SKY_REV27)
		r100.tw_sm_c_100.no_base_addr_ack_error = i2c->no_base_addr;

61
	ret = flexcop_i2c_operation(i2c->fc, &r100);
62 63 64 65 66
	if (ret != 0) {
		deb_i2c("Retrying operation\n");
		r100.tw_sm_c_100.no_base_addr_ack_error = i2c->no_base_addr;
		ret = flexcop_i2c_operation(i2c->fc, &r100);
	}
67 68 69
	if (ret != 0) {
		deb_i2c("read failed. %d\n", ret);
		return ret;
70
	}
71 72 73

	buf[0] = r100.tw_sm_c_100.data1_reg;

74
	if (len > 0) {
75 76
		r104 = i2c->fc->read_ibi_reg(i2c->fc, tw_sm_c_104);
		deb_i2c("read: r100: %08x, r104: %08x\n", r100.raw, r104.raw);
77 78 79 80 81 82

		/* there is at least one more byte, otherwise we wouldn't be here */
		buf[1] = r104.tw_sm_c_104.data2_reg;
		if (len > 1) buf[2] = r104.tw_sm_c_104.data3_reg;
		if (len > 2) buf[3] = r104.tw_sm_c_104.data4_reg;
	}
83 84 85
	return 0;
}

86 87
static int flexcop_i2c_write4(struct flexcop_device *fc,
		flexcop_ibi_value r100, u8 *buf)
88 89 90 91 92 93 94 95 96 97 98
{
	flexcop_ibi_value r104;
	int len = r100.tw_sm_c_100.total_bytes; /* remember total_bytes is buflen-1 */
	r104.raw = 0;

	/* there is at least one byte, otherwise we wouldn't be here */
	r100.tw_sm_c_100.data1_reg = buf[0];
	r104.tw_sm_c_104.data2_reg = len > 0 ? buf[1] : 0;
	r104.tw_sm_c_104.data3_reg = len > 1 ? buf[2] : 0;
	r104.tw_sm_c_104.data4_reg = len > 2 ? buf[3] : 0;

99
	deb_i2c("write: r100: %08x, r104: %08x\n", r100.raw, r104.raw);
100 101

	/* write the additional i2c data before doing the actual i2c operation */
102 103
	fc->write_ibi_reg(fc, tw_sm_c_104, r104);
	return flexcop_i2c_operation(fc, &r100);
104 105
}

106
int flexcop_i2c_request(struct flexcop_i2c_adapter *i2c,
107
		flexcop_access_op_t op, u8 chipaddr, u8 addr, u8 *buf, u16 len)
108 109
{
	int ret;
110 111 112 113 114

#ifdef DUMP_I2C_MESSAGES
	int i;
#endif

115 116 117 118 119 120 121
	u16 bytes_to_transfer;
	flexcop_ibi_value r100;

	deb_i2c("op = %d\n",op);
	r100.raw = 0;
	r100.tw_sm_c_100.chipaddr = chipaddr;
	r100.tw_sm_c_100.twoWS_rw = op;
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
	r100.tw_sm_c_100.twoWS_port_reg = i2c->port;

#ifdef DUMP_I2C_MESSAGES
	printk(KERN_DEBUG "%d ", i2c->port);
	if (op == FC_READ)
		printk("rd(");
	else
		printk("wr(");
	printk("%02x): %02x ", chipaddr, addr);
#endif

	/* in that case addr is the only value ->
	 * we write it twice as baseaddr and val0
	 * BBTI is doing it like that for ISL6421 at least */
	if (i2c->no_base_addr && len == 0 && op == FC_WRITE) {
		buf = &addr;
		len = 1;
	}
140 141 142

	while (len != 0) {
		bytes_to_transfer = len > 4 ? 4 : len;
143

144 145 146 147
		r100.tw_sm_c_100.total_bytes = bytes_to_transfer - 1;
		r100.tw_sm_c_100.baseaddr = addr;

		if (op == FC_READ)
148
			ret = flexcop_i2c_read4(i2c, r100, buf);
149
		else
150 151 152 153 154 155
			ret = flexcop_i2c_write4(i2c->fc, r100, buf);

#ifdef DUMP_I2C_MESSAGES
		for (i = 0; i < bytes_to_transfer; i++)
			printk("%02x ", buf[i]);
#endif
156 157 158 159 160 161 162

		if (ret < 0)
			return ret;

		buf  += bytes_to_transfer;
		addr += bytes_to_transfer;
		len  -= bytes_to_transfer;
163 164 165 166 167
	}

#ifdef DUMP_I2C_MESSAGES
	printk("\n");
#endif
168 169

	return 0;
170
}
171 172
/* exported for PCI i2c */
EXPORT_SYMBOL(flexcop_i2c_request);
173 174

/* master xfer callback for demodulator */
175 176
static int flexcop_master_xfer(struct i2c_adapter *i2c_adap,
		struct i2c_msg msgs[], int num)
177
{
178
	struct flexcop_i2c_adapter *i2c = i2c_get_adapdata(i2c_adap);
179 180
	int i, ret = 0;

181 182 183 184 185 186 187
	/* Some drivers use 1 byte or 0 byte reads as probes, which this
	 * driver doesn't support.  These probes will always fail, so this
	 * hack makes them always succeed.  If one knew how, it would of
	 * course be better to actually do the read.  */
	if (num == 1 && msgs[0].flags == I2C_M_RD && msgs[0].len <= 1)
		return 1;

188
	if (mutex_lock_interruptible(&i2c->fc->i2c_mutex))
189 190
		return -ERESTARTSYS;

191 192 193 194
	for (i = 0; i < num; i++) {
		/* reading */
		if (i+1 < num && (msgs[i+1].flags == I2C_M_RD)) {
			ret = i2c->fc->i2c_request(i2c, FC_READ, msgs[i].addr,
195 196
					msgs[i].buf[0], msgs[i+1].buf,
					msgs[i+1].len);
197 198 199
			i++; /* skip the following message */
		} else /* writing */
			ret = i2c->fc->i2c_request(i2c, FC_WRITE, msgs[i].addr,
200 201
					msgs[i].buf[0], &msgs[i].buf[1],
					msgs[i].len - 1);
202
		if (ret < 0) {
203
			deb_i2c("i2c master_xfer failed");
204 205 206 207
			break;
		}
	}

208
	mutex_unlock(&i2c->fc->i2c_mutex);
209

210 211
	if (ret == 0)
		ret = num;
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
	return ret;
}

static u32 flexcop_i2c_func(struct i2c_adapter *adapter)
{
	return I2C_FUNC_I2C;
}

static struct i2c_algorithm flexcop_algo = {
	.master_xfer	= flexcop_master_xfer,
	.functionality	= flexcop_i2c_func,
};

int flexcop_i2c_init(struct flexcop_device *fc)
{
	int ret;
228
	mutex_init(&fc->i2c_mutex);
229

230 231 232 233 234 235 236
	fc->fc_i2c_adap[0].fc = fc;
	fc->fc_i2c_adap[1].fc = fc;
	fc->fc_i2c_adap[2].fc = fc;
	fc->fc_i2c_adap[0].port = FC_I2C_PORT_DEMOD;
	fc->fc_i2c_adap[1].port = FC_I2C_PORT_EEPROM;
	fc->fc_i2c_adap[2].port = FC_I2C_PORT_TUNER;

237
	strlcpy(fc->fc_i2c_adap[0].i2c_adap.name, "B2C2 FlexCop I2C to demod",
238
			sizeof(fc->fc_i2c_adap[0].i2c_adap.name));
239
	strlcpy(fc->fc_i2c_adap[1].i2c_adap.name, "B2C2 FlexCop I2C to eeprom",
240
			sizeof(fc->fc_i2c_adap[1].i2c_adap.name));
241
	strlcpy(fc->fc_i2c_adap[2].i2c_adap.name, "B2C2 FlexCop I2C to tuner",
242
			sizeof(fc->fc_i2c_adap[2].i2c_adap.name));
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263

	i2c_set_adapdata(&fc->fc_i2c_adap[0].i2c_adap, &fc->fc_i2c_adap[0]);
	i2c_set_adapdata(&fc->fc_i2c_adap[1].i2c_adap, &fc->fc_i2c_adap[1]);
	i2c_set_adapdata(&fc->fc_i2c_adap[2].i2c_adap, &fc->fc_i2c_adap[2]);

	fc->fc_i2c_adap[0].i2c_adap.class =
		fc->fc_i2c_adap[1].i2c_adap.class =
		fc->fc_i2c_adap[2].i2c_adap.class = I2C_CLASS_TV_DIGITAL;
	fc->fc_i2c_adap[0].i2c_adap.algo =
		fc->fc_i2c_adap[1].i2c_adap.algo =
		fc->fc_i2c_adap[2].i2c_adap.algo = &flexcop_algo;
	fc->fc_i2c_adap[0].i2c_adap.algo_data =
		fc->fc_i2c_adap[1].i2c_adap.algo_data =
		fc->fc_i2c_adap[2].i2c_adap.algo_data = NULL;
	fc->fc_i2c_adap[0].i2c_adap.dev.parent =
		fc->fc_i2c_adap[1].i2c_adap.dev.parent =
		fc->fc_i2c_adap[2].i2c_adap.dev.parent = fc->dev;

	ret = i2c_add_adapter(&fc->fc_i2c_adap[0].i2c_adap);
	if (ret < 0)
		return ret;
264

265 266 267
	ret = i2c_add_adapter(&fc->fc_i2c_adap[1].i2c_adap);
	if (ret < 0)
		goto adap_1_failed;
268

269 270 271
	ret = i2c_add_adapter(&fc->fc_i2c_adap[2].i2c_adap);
	if (ret < 0)
		goto adap_2_failed;
272 273 274

	fc->init_state |= FC_STATE_I2C_INIT;
	return 0;
275 276 277 278 279 280

adap_2_failed:
	i2c_del_adapter(&fc->fc_i2c_adap[1].i2c_adap);
adap_1_failed:
	i2c_del_adapter(&fc->fc_i2c_adap[0].i2c_adap);
	return ret;
281 282 283 284
}

void flexcop_i2c_exit(struct flexcop_device *fc)
{
285 286 287 288 289
	if (fc->init_state & FC_STATE_I2C_INIT) {
		i2c_del_adapter(&fc->fc_i2c_adap[2].i2c_adap);
		i2c_del_adapter(&fc->fc_i2c_adap[1].i2c_adap);
		i2c_del_adapter(&fc->fc_i2c_adap[0].i2c_adap);
	}
290 291
	fc->init_state &= ~FC_STATE_I2C_INIT;
}