dm: i2c: Move error reporting into a common function

Factor out the common code to make it easier to adjust it.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Tom Rini <trini@ti.com>
Acked-by: Heiko Schocher <hs@denx.de>
diff --git a/common/cmd_i2c.c b/common/cmd_i2c.c
index 3a75f94..c266b88 100644
--- a/common/cmd_i2c.c
+++ b/common/cmd_i2c.c
@@ -198,6 +198,19 @@
 	return alen;
 }
 
+enum i2c_err_op {
+	I2C_ERR_READ,
+	I2C_ERR_WRITE,
+};
+
+static int i2c_report_err(int ret, enum i2c_err_op op)
+{
+	printf("Error %s the chip: %d\n",
+	       op == I2C_ERR_READ ? "reading" : "writing", ret);
+
+	return CMD_RET_FAILURE;
+}
+
 /**
  * do_i2c_read() - Handle the "i2c read" command-line command
  * @cmdtp:	Command data struct pointer
@@ -245,7 +258,7 @@
 	memaddr = (u_char *)simple_strtoul(argv[4], NULL, 16);
 
 	if (i2c_read(chip, devaddr, alen, memaddr, length) != 0) {
-		puts ("Error reading the chip.\n");
+		i2c_report_err(-1, I2C_ERR_READ);
 		return 1;
 	}
 	return 0;
@@ -286,8 +299,7 @@
 
 	while (length-- > 0) {
 		if (i2c_write(chip, devaddr++, alen, memaddr++, 1) != 0) {
-			puts("Error writing to the chip.\n");
-			return 1;
+			return i2c_report_err(-1, I2C_ERR_WRITE);
 		}
 /*
  * No write delay with FRAM devices.
@@ -370,7 +382,7 @@
 		linebytes = (nbytes > DISP_LINE_LEN) ? DISP_LINE_LEN : nbytes;
 
 		if (i2c_read(chip, addr, alen, linebuf, linebytes) != 0)
-			puts ("Error reading the chip.\n");
+			i2c_report_err(-1, I2C_ERR_READ);
 		else {
 			printf("%04x:", addr);
 			cp = linebuf;
@@ -452,7 +464,7 @@
 
 	while (count-- > 0) {
 		if (i2c_write(chip, addr++, alen, &byte, 1) != 0)
-			puts ("Error writing the chip.\n");
+			i2c_report_err(-1, I2C_ERR_WRITE);
 		/*
 		 * Wait for the write to complete.  The write can take
 		 * up to 10mSec (we allow a little more time).
@@ -528,7 +540,7 @@
 		addr++;
 	}
 	if (err > 0)
-		puts ("Error reading the chip,\n");
+		i2c_report_err(-1, I2C_ERR_READ);
 	else
 		printf ("%08lx\n", crc);
 
@@ -601,7 +613,7 @@
 	do {
 		printf("%08lx:", addr);
 		if (i2c_read(chip, addr, alen, (uchar *)&data, size) != 0)
-			puts ("\nError reading the chip,\n");
+			i2c_report_err(-1, I2C_ERR_READ);
 		else {
 			data = cpu_to_be32(data);
 			if (size == 1)
@@ -644,7 +656,7 @@
 				 */
 				bootretry_reset_cmd_timeout();
 				if (i2c_write(chip, addr, alen, (uchar *)&data, size) != 0)
-					puts ("Error writing the chip.\n");
+					i2c_report_err(-1, I2C_ERR_WRITE);
 #ifdef CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS
 				udelay(CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS * 1000);
 #endif
@@ -783,7 +795,7 @@
 	 */
 	while (1) {
 		if (i2c_read(chip, addr, alen, bytes, length) != 0)
-			puts ("Error reading the chip.\n");
+			i2c_report_err(-1, I2C_ERR_READ);
 		udelay(delay);
 	}
 
@@ -1341,7 +1353,7 @@
 
 	chip = simple_strtoul(argv[1], NULL, 16);
 	if (i2c_read(chip, 0, 1, (uchar *)&edid, sizeof(edid)) != 0) {
-		puts("Error reading EDID content.\n");
+		i2c_report_err(-1, I2C_ERR_READ);
 		return 1;
 	}