V4L/DVB (7469): Preparation for supporting new devices, cleanup and saneness

To prepare the support for new device to the flexcop-family some preparation and cleanups was done + some saneness:

- created an i2c-adapter for each i2c-port available. Easier usage for devices with several device on different i2c-busses
- initialize i2c before doing the eeprom read
- changed the way to attach the different frontends, easier to read now
- enabled support for i2c-devices having no register address (1-byte access)

Signed-off-by: Patrick Boettcher <pb@linuxtv.org>
Signed-off-by: Mauro Carvalho Chehab <mchehab@infradead.org>
diff --git a/drivers/media/dvb/b2c2/flexcop-i2c.c b/drivers/media/dvb/b2c2/flexcop-i2c.c
index 6bf858a..55973ea 100644
--- a/drivers/media/dvb/b2c2/flexcop-i2c.c
+++ b/drivers/media/dvb/b2c2/flexcop-i2c.c
@@ -9,6 +9,8 @@
 
 #define FC_MAX_I2C_RETRIES 100000
 
+/* #define DUMP_I2C_MESSAGES */
+
 static int flexcop_i2c_operation(struct flexcop_device *fc, flexcop_ibi_value *r100)
 {
 	int i;
@@ -38,30 +40,25 @@
 	return -EREMOTEIO;
 }
 
-static int flexcop_i2c_read4(struct flexcop_device *fc, flexcop_ibi_value r100, u8 *buf)
+static int flexcop_i2c_read4(struct flexcop_i2c_adapter *i2c,
+	flexcop_ibi_value r100, u8 *buf)
 {
 	flexcop_ibi_value r104;
 	int len = r100.tw_sm_c_100.total_bytes, /* remember total_bytes is buflen-1 */
 		ret;
 
-	if ((ret = flexcop_i2c_operation(fc,&r100)) != 0) {
-		/* The Cablestar needs a different kind of i2c-transfer (does not
-		 * support "Repeat Start"):
-		 * wait for the ACK failure,
-		 * and do a subsequent read with the Bit 30 enabled
-		 */
-		r100.tw_sm_c_100.no_base_addr_ack_error = 1;
-		if ((ret = flexcop_i2c_operation(fc,&r100)) != 0) {
-			deb_i2c("no_base_addr read failed. %d\n",ret);
-			return ret;
-		}
+	r100.tw_sm_c_100.no_base_addr_ack_error = i2c->no_base_addr;
+	ret = flexcop_i2c_operation(i2c->fc, &r100);
+	if (ret != 0) {
+		deb_i2c("read failed. %d\n", ret);
+		return ret;
 	}
 
 	buf[0] = r100.tw_sm_c_100.data1_reg;
 
 	if (len > 0) {
-		r104 = fc->read_ibi_reg(fc,tw_sm_c_104);
-		deb_i2c("read: r100: %08x, r104: %08x\n",r100.raw,r104.raw);
+		r104 = i2c->fc->read_ibi_reg(i2c->fc, tw_sm_c_104);
+		deb_i2c("read: r100: %08x, r104: %08x\n", r100.raw, r104.raw);
 
 		/* there is at least one more byte, otherwise we wouldn't be here */
 		buf[1] = r104.tw_sm_c_104.data2_reg;
@@ -85,17 +82,22 @@
 	r104.tw_sm_c_104.data3_reg = len > 1 ? buf[2] : 0;
 	r104.tw_sm_c_104.data4_reg = len > 2 ? buf[3] : 0;
 
-	deb_i2c("write: r100: %08x, r104: %08x\n",r100.raw,r104.raw);
+	deb_i2c("write: r100: %08x, r104: %08x\n", r100.raw, r104.raw);
 
 	/* write the additional i2c data before doing the actual i2c operation */
-	fc->write_ibi_reg(fc,tw_sm_c_104,r104);
-	return flexcop_i2c_operation(fc,&r100);
+	fc->write_ibi_reg(fc, tw_sm_c_104, r104);
+	return flexcop_i2c_operation(fc, &r100);
 }
 
-int flexcop_i2c_request(struct flexcop_device *fc, flexcop_access_op_t op,
-		flexcop_i2c_port_t port, u8 chipaddr, u8 addr, u8 *buf, u16 len)
+int flexcop_i2c_request(struct flexcop_i2c_adapter *i2c,
+	flexcop_access_op_t op, u8 chipaddr, u8 addr, u8 *buf, u16 len)
 {
 	int ret;
+
+#ifdef DUMP_I2C_MESSAGES
+	int i;
+#endif
+
 	u16 bytes_to_transfer;
 	flexcop_ibi_value r100;
 
@@ -103,7 +105,25 @@
 	r100.raw = 0;
 	r100.tw_sm_c_100.chipaddr = chipaddr;
 	r100.tw_sm_c_100.twoWS_rw = op;
-	r100.tw_sm_c_100.twoWS_port_reg = port;
+	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;
+	}
 
 	while (len != 0) {
 		bytes_to_transfer = len > 4 ? 4 : len;
@@ -112,9 +132,14 @@
 		r100.tw_sm_c_100.baseaddr = addr;
 
 		if (op == FC_READ)
-			ret = flexcop_i2c_read4(fc, r100, buf);
+			ret = flexcop_i2c_read4(i2c, r100, buf);
 		else
-			ret = flexcop_i2c_write4(fc,r100, buf);
+			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
 
 		if (ret < 0)
 			return ret;
@@ -122,7 +147,11 @@
 		buf  += bytes_to_transfer;
 		addr += bytes_to_transfer;
 		len  -= bytes_to_transfer;
-	};
+	}
+
+#ifdef DUMP_I2C_MESSAGES
+	printk("\n");
+#endif
 
 	return 0;
 }
@@ -132,7 +161,7 @@
 /* master xfer callback for demodulator */
 static int flexcop_master_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg msgs[], int num)
 {
-	struct flexcop_device *fc = i2c_get_adapdata(i2c_adap);
+	struct flexcop_i2c_adapter *i2c = i2c_get_adapdata(i2c_adap);
 	int i, ret = 0;
 
 	/* Some drivers use 1 byte or 0 byte reads as probes, which this
@@ -142,34 +171,29 @@
 	if (num == 1 && msgs[0].flags == I2C_M_RD && msgs[0].len <= 1)
 		return 1;
 
-	if (mutex_lock_interruptible(&fc->i2c_mutex))
+	if (mutex_lock_interruptible(&i2c->fc->i2c_mutex))
 		return -ERESTARTSYS;
 
-	/* reading */
-	if (num == 2 &&
-		msgs[0].flags == 0 &&
-		msgs[1].flags == I2C_M_RD &&
-		msgs[0].buf != NULL &&
-		msgs[1].buf != NULL) {
-
-		ret = fc->i2c_request(fc, FC_READ, FC_I2C_PORT_DEMOD, msgs[0].addr, msgs[0].buf[0], msgs[1].buf, msgs[1].len);
-
-	} else for (i = 0; i < num; i++) { /* writing command */
-		if (msgs[i].flags != 0 || msgs[i].buf == NULL || msgs[i].len < 2) {
-			ret = -EINVAL;
+	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,
+				msgs[i].buf[0], msgs[i+1].buf, msgs[i+1].len);
+			i++; /* skip the following message */
+		} else /* writing */
+			ret = i2c->fc->i2c_request(i2c, FC_WRITE, msgs[i].addr,
+				msgs[i].buf[0], &msgs[i].buf[1],
+				msgs[i].len - 1);
+		if (ret < 0) {
+			err("i2c master_xfer failed");
 			break;
 		}
-
-		ret = fc->i2c_request(fc, FC_WRITE, FC_I2C_PORT_DEMOD, msgs[i].addr, msgs[i].buf[0], &msgs[i].buf[1], msgs[i].len - 1);
 	}
 
-	if (ret < 0)
-		err("i2c master_xfer failed");
-	else
+	mutex_unlock(&i2c->fc->i2c_mutex);
+
+	if (ret == 0)
 		ret = num;
-
-	mutex_unlock(&fc->i2c_mutex);
-
 	return ret;
 }
 
@@ -189,28 +213,68 @@
 
 	mutex_init(&fc->i2c_mutex);
 
-	memset(&fc->i2c_adap, 0, sizeof(struct i2c_adapter));
-	strncpy(fc->i2c_adap.name, "B2C2 FlexCop device",
-		sizeof(fc->i2c_adap.name));
+	fc->fc_i2c_adap[0].fc = fc;
+	fc->fc_i2c_adap[1].fc = fc;
+	fc->fc_i2c_adap[2].fc = fc;
 
-	i2c_set_adapdata(&fc->i2c_adap,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;
 
-	fc->i2c_adap.class	    = I2C_CLASS_TV_DIGITAL;
-	fc->i2c_adap.algo       = &flexcop_algo;
-	fc->i2c_adap.algo_data  = NULL;
-	fc->i2c_adap.dev.parent	= fc->dev;
+	strncpy(fc->fc_i2c_adap[0].i2c_adap.name,
+		"B2C2 FlexCop I2C to demod", I2C_NAME_SIZE);
+	strncpy(fc->fc_i2c_adap[1].i2c_adap.name,
+		"B2C2 FlexCop I2C to eeprom", I2C_NAME_SIZE);
+	strncpy(fc->fc_i2c_adap[2].i2c_adap.name,
+		"B2C2 FlexCop I2C to tuner", I2C_NAME_SIZE);
 
-	if ((ret = i2c_add_adapter(&fc->i2c_adap)) < 0)
+	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;
 
+	ret = i2c_add_adapter(&fc->fc_i2c_adap[1].i2c_adap);
+	if (ret < 0)
+		goto adap_1_failed;
+
+	ret = i2c_add_adapter(&fc->fc_i2c_adap[2].i2c_adap);
+	if (ret < 0)
+		goto adap_2_failed;
+
 	fc->init_state |= FC_STATE_I2C_INIT;
 	return 0;
+
+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;
 }
 
 void flexcop_i2c_exit(struct flexcop_device *fc)
 {
-	if (fc->init_state & FC_STATE_I2C_INIT)
-		i2c_del_adapter(&fc->i2c_adap);
+	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);
+	}
 
 	fc->init_state &= ~FC_STATE_I2C_INIT;
 }