colibri_imx6: migrate i2c to using driver model

Migrate I2C to using driver model.

Signed-off-by: Marcel Ziswiler <marcel.ziswiler@toradex.com>
diff --git a/board/toradex/colibri_imx6/pf0100.c b/board/toradex/colibri_imx6/pf0100.c
index fa63865..5e411f1 100644
--- a/board/toradex/colibri_imx6/pf0100.c
+++ b/board/toradex/colibri_imx6/pf0100.c
@@ -1,6 +1,6 @@
 // SPDX-License-Identifier: GPL-2.0+
 /*
- * Copyright (C) 2014-2016, Toradex AG
+ * Copyright (C) 2014-2019, Toradex AG
  */
 
 /*
@@ -29,22 +29,25 @@
 
 unsigned pmic_init(void)
 {
+	int rc;
+	struct udevice *dev = NULL;
 	unsigned programmed = 0;
 	uchar bus = 1;
 	uchar devid, revid, val;
 
 	puts("PMIC: ");
-	if (!((0 == i2c_set_bus_num(bus)) &&
-	      (0 == i2c_probe(PFUZE100_I2C_ADDR)))) {
-		puts("i2c bus failed\n");
+	rc = i2c_get_chip_for_busnum(bus, PFUZE100_I2C_ADDR, 1, &dev);
+	if (rc) {
+		printf("failed to get device for PMIC at address 0x%x\n",
+		       PFUZE100_I2C_ADDR);
 		return 0;
 	}
 	/* get device ident */
-	if (i2c_read(PFUZE100_I2C_ADDR, PFUZE100_DEVICEID, 1, &devid, 1) < 0) {
+	if (dm_i2c_read(dev, PFUZE100_DEVICEID, &devid, 1) < 0) {
 		puts("i2c pmic devid read failed\n");
 		return 0;
 	}
-	if (i2c_read(PFUZE100_I2C_ADDR, PFUZE100_REVID, 1, &revid, 1) < 0) {
+	if (dm_i2c_read(dev, PFUZE100_REVID, &revid, 1) < 0) {
 		puts("i2c pmic revid read failed\n");
 		return 0;
 	}
@@ -52,14 +55,14 @@
 
 #ifdef DEBUG
 	{
-		unsigned i, j;
+		unsigned int i, j;
 
 		for (i = 0; i < 16; i++)
 			printf("\t%x", i);
 		for (j = 0; j < 0x80; ) {
 			printf("\n%2x", j);
 			for (i = 0; i < 16; i++) {
-				i2c_read(PFUZE100_I2C_ADDR, j+i, 1, &val, 1);
+				dm_i2c_read(dev, j + i, &val, 1);
 				printf("\t%2x", val);
 			}
 			j += 0x10;
@@ -67,8 +70,7 @@
 		printf("\nEXT Page 1");
 
 		val = PFUZE100_PAGE_REGISTER_PAGE1;
-		if (i2c_write(PFUZE100_I2C_ADDR, PFUZE100_PAGE_REGISTER, 1,
-			      &val, 1)) {
+		if (dm_i2c_write(dev, PFUZE100_PAGE_REGISTER, &val, 1)) {
 			puts("i2c write failed\n");
 			return 0;
 		}
@@ -76,7 +78,7 @@
 		for (j = 0x80; j < 0x100; ) {
 			printf("\n%2x", j);
 			for (i = 0; i < 16; i++) {
-				i2c_read(PFUZE100_I2C_ADDR, j+i, 1, &val, 1);
+				dm_i2c_read(dev, j + i, &val, 1);
 				printf("\t%2x", val);
 			}
 			j += 0x10;
@@ -84,8 +86,7 @@
 		printf("\nEXT Page 2");
 
 		val = PFUZE100_PAGE_REGISTER_PAGE2;
-		if (i2c_write(PFUZE100_I2C_ADDR, PFUZE100_PAGE_REGISTER, 1,
-			      &val, 1)) {
+		if (dm_i2c_write(dev, PFUZE100_PAGE_REGISTER, &val, 1)) {
 			puts("i2c write failed\n");
 			return 0;
 		}
@@ -93,35 +94,36 @@
 		for (j = 0x80; j < 0x100; ) {
 			printf("\n%2x", j);
 			for (i = 0; i < 16; i++) {
-				i2c_read(PFUZE100_I2C_ADDR, j+i, 1, &val, 1);
+				dm_i2c_read(dev, j + i, &val, 1);
 				printf("\t%2x", val);
 			}
 			j += 0x10;
 		}
 		printf("\n");
 	}
-#endif
+#endif /* DEBUG */
+
 	/* get device programmed state */
 	val = PFUZE100_PAGE_REGISTER_PAGE1;
-	if (i2c_write(PFUZE100_I2C_ADDR, PFUZE100_PAGE_REGISTER, 1, &val, 1)) {
+	if (dm_i2c_write(dev, PFUZE100_PAGE_REGISTER, &val, 1)) {
 		puts("i2c write failed\n");
 		return 0;
 	}
-	if (i2c_read(PFUZE100_I2C_ADDR, PFUZE100_FUSE_POR1, 1, &val, 1) < 0) {
+	if (dm_i2c_read(dev, PFUZE100_FUSE_POR1, &val, 1) < 0) {
 		puts("i2c fuse_por read failed\n");
 		return 0;
 	}
 	if (val & PFUZE100_FUSE_POR_M)
 		programmed++;
 
-	if (i2c_read(PFUZE100_I2C_ADDR, PFUZE100_FUSE_POR2, 1, &val, 1) < 0) {
+	if (dm_i2c_read(dev, PFUZE100_FUSE_POR2, &val, 1) < 0) {
 		puts("i2c fuse_por read failed\n");
 		return programmed;
 	}
 	if (val & PFUZE100_FUSE_POR_M)
 		programmed++;
 
-	if (i2c_read(PFUZE100_I2C_ADDR, PFUZE100_FUSE_POR3, 1, &val, 1) < 0) {
+	if (dm_i2c_read(dev, PFUZE100_FUSE_POR3, &val, 1) < 0) {
 		puts("i2c fuse_por read failed\n");
 		return programmed;
 	}
@@ -146,6 +148,8 @@
 #ifndef CONFIG_SPL_BUILD
 static int pf0100_prog(void)
 {
+	int rc;
+	struct udevice *dev = NULL;
 	unsigned char bus = 1;
 	unsigned char val;
 	unsigned int i;
@@ -159,9 +163,10 @@
 					 ARRAY_SIZE(pmic_prog_pads));
 	gpio_direction_output(PMIC_PROG_VOLTAGE, 0);
 
-	if (!((0 == i2c_set_bus_num(bus)) &&
-	      (0 == i2c_probe(PFUZE100_I2C_ADDR)))) {
-		puts("i2c bus failed\n");
+	rc = i2c_get_chip_for_busnum(bus, PFUZE100_I2C_ADDR, 1, &dev);
+	if (rc) {
+		printf("failed to get device for PMIC at address 0x%x\n",
+		       PFUZE100_I2C_ADDR);
 		return CMD_RET_FAILURE;
 	}
 
@@ -169,8 +174,7 @@
 		switch (pmic_otp_prog[i].cmd) {
 		case pmic_i2c:
 			val = (unsigned char) (pmic_otp_prog[i].value & 0xff);
-			if (i2c_write(PFUZE100_I2C_ADDR, pmic_otp_prog[i].reg,
-				      1, &val, 1)) {
+			if (dm_i2c_write(dev, pmic_otp_prog[i].reg, &val, 1)) {
 				printf("i2c write failed, reg 0x%2x, value 0x%2x\n",
 				       pmic_otp_prog[i].reg, val);
 				return CMD_RET_FAILURE;
@@ -209,4 +213,4 @@
 	"Program the OTP fuses on the PMIC PF0100",
 	""
 );
-#endif
+#endif /* CONFIG_SPL_BUILD */