ti: remove usage of DM_I2C_COMPAT and don't disable DM_I2C in SPL

DM_I2C_COMPAT is a compatibility layer that allows using the non-DM I2C
API when DM_I2C is used. The goal is to eventually remove DM_I2C_COMPAT
when all I2C "clients" have been migrated to use the DM API.
This a step in that direction for the TI based platforms.
Build tested with buildman:
buildman -dle am33xx ti omap3 omap4 omap5 davinci keystone

boot tested with:
am335x_evm, am335x_boneblack, am335x_boneblack_vboot (DM version),
am57xx_evm, dra7xx_evm, k2g_evm, am437x_evm

Signed-off-by: Jean-Jacques Hiblot <jjhiblot@ti.com>
Reviewed-by: Tom Rini <trini@konsulko.com>
Reviewed-by: Heiko Schocher <hs@denx.de>
diff --git a/board/ti/am335x/board.c b/board/ti/am335x/board.c
index 1384525..d67f94a 100644
--- a/board/ti/am335x/board.c
+++ b/board/ti/am335x/board.c
@@ -70,8 +70,9 @@
 void do_board_detect(void)
 {
 	enable_i2c0_pin_mux();
+#ifndef CONFIG_DM_I2C
 	i2c_init(CONFIG_SYS_OMAP24_I2C_SPEED, CONFIG_SYS_OMAP24_I2C_SLAVE);
-
+#endif
 	if (ti_i2c_eeprom_am_get(CONFIG_EEPROM_BUS_ADDRESS,
 				 CONFIG_EEPROM_CHIP_ADDRESS))
 		printf("ti_i2c_eeprom_init failed\n");
@@ -328,8 +329,14 @@
 	if (board_is_bone() && !strncmp(board_ti_get_rev(), "00A1", 4))
 		return;
 
+#ifndef CONFIG_DM_I2C
 	if (i2c_probe(TPS65217_CHIP_PM))
 		return;
+#else
+	if (power_tps65217_init(0))
+		return;
+#endif
+
 
 	/*
 	 * On Beaglebone White we need to ensure we have AC power
@@ -421,9 +428,13 @@
 	 * 1.10V.  For MPU voltage we need to switch based on
 	 * the frequency we are running at.
 	 */
+#ifndef CONFIG_DM_I2C
 	if (i2c_probe(TPS65910_CTRL_I2C_ADDR))
 		return;
-
+#else
+	if (power_tps65910_init(0))
+		return;
+#endif
 	/*
 	 * Depending on MPU clock and PG we will need a different
 	 * VDD to drive at that speed.
@@ -451,8 +462,10 @@
 
 	if (first_time) {
 		enable_i2c0_pin_mux();
+#ifndef CONFIG_DM_I2C
 		i2c_init(CONFIG_SYS_OMAP24_I2C_SPEED,
 			 CONFIG_SYS_OMAP24_I2C_SLAVE);
+#endif
 		first_time = false;
 	}
 }
diff --git a/board/ti/am335x/mux.c b/board/ti/am335x/mux.c
index 41333f9..04f4b8e 100644
--- a/board/ti/am335x/mux.c
+++ b/board/ti/am335x/mux.c
@@ -329,12 +329,23 @@
 {
 	unsigned short val;
 
+#ifndef CONFIG_DM_I2C
 	if (i2c_probe(I2C_CPLD_ADDR))
 		return PROFILE_NONE;
 
 	if (i2c_read(I2C_CPLD_ADDR, CFG_REG, 1, (unsigned char *)(&val), 2))
 		return PROFILE_NONE;
+#else
+	struct udevice *dev = NULL;
+	int rc;
 
+	rc = i2c_get_chip_for_busnum(0, I2C_CPLD_ADDR, 1, &dev);
+	if (rc)
+		return PROFILE_NONE;
+	rc = dm_i2c_read(dev, CFG_REG, (unsigned char *)(&val), 2);
+	if (rc)
+		return PROFILE_NONE;
+#endif
 	return (1 << (val & PROFILE_MASK));
 }
 
diff --git a/board/ti/am43xx/board.c b/board/ti/am43xx/board.c
index 2a59b06..31bc0f4 100644
--- a/board/ti/am43xx/board.c
+++ b/board/ti/am43xx/board.c
@@ -43,6 +43,8 @@
 #ifdef CONFIG_TI_I2C_BOARD_DETECT
 void do_board_detect(void)
 {
+	/* Ensure I2C is initialized for EEPROM access*/
+	gpi2c_init();
 	if (ti_i2c_eeprom_am_get(CONFIG_EEPROM_BUS_ADDRESS,
 				 CONFIG_EEPROM_CHIP_ADDRESS))
 		printf("ti_i2c_eeprom_init failed\n");
@@ -386,8 +388,13 @@
 {
 	int mpu_vdd, ddr_volt;
 
+#ifndef CONFIG_DM_I2C
 	if (i2c_probe(TPS65218_CHIP_PM))
 		return;
+#else
+	if (power_tps65218_init(0))
+		return;
+#endif
 
 	switch (m) {
 	case 1000:
@@ -439,8 +446,13 @@
 {
 	int mpu_vdd;
 
+#ifndef CONFIG_DM_I2C
 	if (i2c_probe(TPS62362_I2C_ADDR))
 		return;
+#else
+	if (power_tps62362_init(0))
+		return;
+#endif
 
 	switch (m) {
 	case 1000:
@@ -462,14 +474,12 @@
 		puts("Unknown MPU clock, not scaling\n");
 		return;
 	}
-
 	/* Set VDD_MPU voltage */
 	if (tps62362_voltage_update(TPS62362_SET3, mpu_vdd)) {
 		printf("%s failure\n", __func__);
 		return;
 	}
 }
-
 void gpi2c_init(void)
 {
 	/* When needed to be invoked prior to BSS initialization */
@@ -477,8 +487,10 @@
 
 	if (first_time) {
 		enable_i2c0_pin_mux();
+#ifndef CONFIG_DM_I2C
 		i2c_init(CONFIG_SYS_OMAP24_I2C_SPEED,
 			 CONFIG_SYS_OMAP24_I2C_SLAVE);
+#endif
 		first_time = false;
 	}
 }
@@ -614,20 +626,32 @@
 /* setup board specific PMIC */
 int power_init_board(void)
 {
-	struct pmic *p;
-
+	int rc;
+#ifndef CONFIG_DM_I2C
+	struct pmic *p = NULL;
+#endif
 	if (board_is_idk()) {
-		power_tps62362_init(I2C_PMIC);
+		rc = power_tps62362_init(0);
+		if (rc)
+			goto done;
+#ifndef CONFIG_DM_I2C
 		p = pmic_get("TPS62362");
-		if (p && !pmic_probe(p))
-			puts("PMIC:  TPS62362\n");
+		if (!p || pmic_probe(p))
+			goto done;
+#endif
+		puts("PMIC:  TPS62362\n");
 	} else {
-		power_tps65218_init(I2C_PMIC);
+		rc = power_tps65218_init(0);
+		if (rc)
+			goto done;
+#ifndef CONFIG_DM_I2C
 		p = pmic_get("TPS65218_PMIC");
-		if (p && !pmic_probe(p))
-			puts("PMIC:  TPS65218\n");
+		if (!p || pmic_probe(p))
+			goto done;
+#endif
+		puts("PMIC:  TPS65218\n");
 	}
-
+done:
 	return 0;
 }
 
diff --git a/board/ti/am57xx/board.c b/board/ti/am57xx/board.c
index 177a324..355ea55 100644
--- a/board/ti/am57xx/board.c
+++ b/board/ti/am57xx/board.c
@@ -623,7 +623,7 @@
 {
 	int r = -ENODEV;
 	char *idk_lcd = "no";
-	uint8_t buf = 0;
+	u8 buf = 0;
 
 	/* Only valid for IDKs */
 	if (board_is_x15() || board_is_am572x_evm())
@@ -633,6 +633,7 @@
 	if (board_is_am571x_idk() && !am571x_idk_needs_lcd())
 		goto out;
 
+#ifndef CONFIG_DM_I2C
 	r = i2c_set_bus_num(OSD_TS_FT_BUS_ADDRESS);
 	if (r) {
 		printf("%s: Failed to set bus address to %d: %d\n",
@@ -657,6 +658,32 @@
 		       OSD_TS_FT_REG_ID, r);
 		goto out;
 	}
+#else
+	struct udevice *dev;
+
+	r = i2c_get_chip_for_busnum(OSD_TS_FT_BUS_ADDRESS,
+				    OSD_TS_FT_CHIP_ADDRESS, 1, &dev);
+	if (r) {
+		printf("%s: Failed to get I2C device %d/%d (ret %d)\n",
+		       __func__, OSD_TS_FT_BUS_ADDRESS, OSD_TS_FT_CHIP_ADDRESS,
+		       r);
+		/* AM572x IDK has no explicit settings for optional LCD kit */
+		if (board_is_am571x_idk())
+			printf("%s: Touch screen detect failed: %d!\n",
+			       __func__, r);
+		goto out;
+	}
+
+	/* Read FT ID */
+	r = dm_i2c_reg_read(dev, OSD_TS_FT_REG_ID);
+	if (r < 0) {
+		printf("%s: Touch screen ID read %d:0x%02x[0x%02x] failed:%d\n",
+		       __func__, OSD_TS_FT_BUS_ADDRESS, OSD_TS_FT_CHIP_ADDRESS,
+		       OSD_TS_FT_REG_ID, r);
+		goto out;
+	}
+	buf = (u8)r;
+#endif
 
 	switch (buf) {
 	case OSD_TS_FT_ID_5606:
diff --git a/board/ti/common/board_detect.c b/board/ti/common/board_detect.c
index 085e732..e258e22 100644
--- a/board/ti/common/board_detect.c
+++ b/board/ti/common/board_detect.c
@@ -14,43 +14,7 @@
 
 #include "board_detect.h"
 
-#if defined(CONFIG_DM_I2C_COMPAT)
-/**
- * ti_i2c_set_alen - Set chip's i2c address length
- * @bus_addr - I2C bus number
- * @dev_addr - I2C eeprom id
- * @alen     - I2C address length in bytes
- *
- * DM_I2C by default sets the address length to be used to 1. This
- * function allows this address length to be changed to match the
- * eeprom used for board detection.
- */
-int __maybe_unused ti_i2c_set_alen(int bus_addr, int dev_addr, int alen)
-{
-	struct udevice *dev;
-	struct udevice *bus;
-	int rc;
-
-	rc = uclass_get_device_by_seq(UCLASS_I2C, bus_addr, &bus);
-	if (rc)
-		return rc;
-	rc = i2c_get_chip(bus, dev_addr, 1, &dev);
-	if (rc)
-		return rc;
-	rc = i2c_set_chip_offset_len(dev, alen);
-	if (rc)
-		return rc;
-
-	return 0;
-}
-#else
-int __maybe_unused ti_i2c_set_alen(int bus_addr, int dev_addr, int alen)
-{
-	return 0;
-}
-#endif
-
-#if !defined(CONFIG_DM_I2C) || defined(CONFIG_DM_I2C_COMPAT)
+#if !defined(CONFIG_DM_I2C)
 /**
  * ti_i2c_eeprom_init - Initialize an i2c bus and probe for a device
  * @i2c_bus: i2c bus number to initialize
@@ -83,17 +47,7 @@
 static int __maybe_unused ti_i2c_eeprom_read(int dev_addr, int offset,
 					     uchar *ep, int epsize)
 {
-	int bus_num, rc, alen;
-
-	bus_num = i2c_get_bus_num();
-
-	alen = 2;
-
-	rc = ti_i2c_set_alen(bus_num, dev_addr, alen);
-	if (rc)
-		return rc;
-
-	return i2c_read(dev_addr, offset, alen, ep, epsize);
+	return i2c_read(dev_addr, offset, 2, ep, epsize);
 }
 #endif
 
@@ -127,7 +81,7 @@
 	u32 hdr_read;
 	int rc;
 
-#if defined(CONFIG_DM_I2C) && !defined(CONFIG_DM_I2C_COMPAT)
+#if defined(CONFIG_DM_I2C)
 	struct udevice *dev;
 	struct udevice *bus;
 
@@ -185,10 +139,6 @@
 	 */
 	byte = 2;
 
-	rc = ti_i2c_set_alen(bus_addr, dev_addr, byte);
-	if (rc)
-		return rc;
-
 	rc = i2c_read(dev_addr, 0x0, byte, (uint8_t *)&hdr_read, 4);
 	if (rc)
 		return rc;
@@ -202,10 +152,6 @@
 		 */
 		byte = 1;
 		if (rc) {
-			rc = ti_i2c_set_alen(bus_addr, dev_addr, byte);
-			if (rc)
-				return rc;
-
 			rc = i2c_read(dev_addr, 0x0, byte, (uint8_t *)&hdr_read,
 				      4);
 		}
diff --git a/board/ti/ks2_evm/board_k2g.c b/board/ti/ks2_evm/board_k2g.c
index 87dc4d0..39a782e 100644
--- a/board/ti/ks2_evm/board_k2g.c
+++ b/board/ti/ks2_evm/board_k2g.c
@@ -251,6 +251,7 @@
 #if defined(CONFIG_DTB_RESELECT)
 static int k2g_alt_board_detect(void)
 {
+#ifndef CONFIG_DM_I2C
 	int rc;
 
 	rc = i2c_set_bus_num(1);
@@ -260,7 +261,17 @@
 	rc = i2c_probe(K2G_GP_AUDIO_CODEC_ADDRESS);
 	if (rc)
 		return rc;
+#else
+	struct udevice *bus, *dev;
+	int rc;
 
+	rc = uclass_get_device_by_seq(UCLASS_I2C, 1, &bus);
+	if (rc)
+		return rc;
+	rc = dm_i2c_probe(bus, K2G_GP_AUDIO_CODEC_ADDRESS, 0, &dev);
+	if (rc)
+		return rc;
+#endif
 	ti_i2c_eeprom_am_set("66AK2GGP", "1.0X");
 
 	return 0;