fsl_pmic: add I2C interface support
authorLiu Hui-R64343 <r64343@freescale.com>
Mon, 3 Jan 2011 22:27:40 +0000 (22:27 +0000)
committerAlbert Aribaud <albert.aribaud@free.fr>
Tue, 1 Feb 2011 23:54:42 +0000 (00:54 +0100)
This patch add I2C interface for fsl_pmic driver support

Signed-off-by: Jason Liu <r64343@freescale.com>
drivers/misc/fsl_pmic.c

index 5ee1de176ea3c418491dfd9a909f89ed079de056..ef80ad949aed59f7578f082b5473671903c6fd70 100644 (file)
 
 #include <config.h>
 #include <common.h>
-#include <spi.h>
 #include <asm/errno.h>
 #include <linux/types.h>
 #include <fsl_pmic.h>
 
+static int check_param(u32 reg, u32 write)
+{
+       if (reg > 63 || write > 1) {
+               printf("<reg num> = %d is invalid. Should be less then 63\n",
+                       reg);
+               return -1;
+       }
+
+       return 0;
+}
+
+#ifdef CONFIG_FSL_PMIC_I2C
+#include <i2c.h>
+
+u32 pmic_reg(u32 reg, u32 val, u32 write)
+{
+       unsigned char buf[4] = { 0 };
+       u32 ret_val = 0;
+
+       if (check_param(reg, write))
+               return -1;
+
+       if (write) {
+               buf[0] = (val >> 16) & 0xff;
+               buf[1] = (val >> 8) & 0xff;
+               buf[2] = (val) & 0xff;
+               if (i2c_write(CONFIG_SYS_FSL_PMIC_I2C_ADDR, reg, 1, buf, 3))
+                       return -1;
+       } else {
+               if (i2c_read(CONFIG_SYS_FSL_PMIC_I2C_ADDR, reg, 1, buf, 3))
+                       return -1;
+               ret_val = buf[0] << 16 | buf[1] << 8 | buf[2];
+       }
+
+       return ret_val;
+}
+#else /* SPI interface */
+#include <spi.h>
 static struct spi_slave *slave;
 
 struct spi_slave *pmic_spi_probe(void)
@@ -55,11 +92,8 @@ u32 pmic_reg(u32 reg, u32 val, u32 write)
                        return -1;
        }
 
-       if (reg > 63 || write > 1) {
-               printf("<reg num> = %d is invalid. Should be less then 63\n",
-                       reg);
+       if (check_param(reg, write))
                return -1;
-       }
 
        if (spi_claim_bus(slave))
                return -1;
@@ -87,6 +121,7 @@ u32 pmic_reg(u32 reg, u32 val, u32 write)
        spi_release_bus(slave);
        return cpu_to_be32(pmic_rx);
 }
+#endif
 
 void pmic_reg_write(u32 reg, u32 value)
 {