Merge tag 'mfd-3.8-1' of git://git.kernel.org/pub/scm/linux/kernel/git/sameo/mfd-2.6
[platform/kernel/linux-rpi.git] / drivers / mfd / twl-core.c
index 11b76c0..4f3baad 100644 (file)
@@ -32,6 +32,7 @@
 #include <linux/mutex.h>
 #include <linux/module.h>
 #include <linux/platform_device.h>
+#include <linux/regmap.h>
 #include <linux/clk.h>
 #include <linux/err.h>
 #include <linux/device.h>
@@ -65,9 +66,6 @@
 
 /* Triton Core internal information (BEGIN) */
 
-/* Last - for index max*/
-#define TWL4030_MODULE_LAST            TWL4030_MODULE_SECURED_REG
-
 #define TWL_NUM_SLAVES         4
 
 #define SUB_CHIP_ID0 0
@@ -171,13 +169,7 @@ EXPORT_SYMBOL(twl_rev);
 /* Structure for each TWL4030/TWL6030 Slave */
 struct twl_client {
        struct i2c_client *client;
-       u8 address;
-
-       /* max numb of i2c_msg required is for read =2 */
-       struct i2c_msg xfer_msg[2];
-
-       /* To lock access to xfer_msg */
-       struct mutex xfer_lock;
+       struct regmap *regmap;
 };
 
 static struct twl_client twl_modules[TWL_NUM_SLAVES];
@@ -189,7 +181,7 @@ struct twl_mapping {
 };
 static struct twl_mapping *twl_map;
 
-static struct twl_mapping twl4030_map[TWL4030_MODULE_LAST + 1] = {
+static struct twl_mapping twl4030_map[] = {
        /*
         * NOTE:  don't change this table without updating the
         * <linux/i2c/twl.h> defines for TWL4030_MODULE_*
@@ -197,34 +189,62 @@ static struct twl_mapping twl4030_map[TWL4030_MODULE_LAST + 1] = {
         */
 
        { 0, TWL4030_BASEADD_USB },
-
        { 1, TWL4030_BASEADD_AUDIO_VOICE },
        { 1, TWL4030_BASEADD_GPIO },
        { 1, TWL4030_BASEADD_INTBR },
        { 1, TWL4030_BASEADD_PIH },
-       { 1, TWL4030_BASEADD_TEST },
 
+       { 1, TWL4030_BASEADD_TEST },
        { 2, TWL4030_BASEADD_KEYPAD },
        { 2, TWL4030_BASEADD_MADC },
        { 2, TWL4030_BASEADD_INTERRUPTS },
        { 2, TWL4030_BASEADD_LED },
+
        { 2, TWL4030_BASEADD_MAIN_CHARGE },
        { 2, TWL4030_BASEADD_PRECHARGE },
        { 2, TWL4030_BASEADD_PWM0 },
        { 2, TWL4030_BASEADD_PWM1 },
        { 2, TWL4030_BASEADD_PWMA },
+
        { 2, TWL4030_BASEADD_PWMB },
        { 2, TWL5031_BASEADD_ACCESSORY },
        { 2, TWL5031_BASEADD_INTERRUPTS },
-
        { 3, TWL4030_BASEADD_BACKUP },
        { 3, TWL4030_BASEADD_INT },
+
        { 3, TWL4030_BASEADD_PM_MASTER },
        { 3, TWL4030_BASEADD_PM_RECEIVER },
        { 3, TWL4030_BASEADD_RTC },
        { 3, TWL4030_BASEADD_SECURED_REG },
 };
 
+static struct regmap_config twl4030_regmap_config[4] = {
+       {
+               /* Address 0x48 */
+               .reg_bits = 8,
+               .val_bits = 8,
+               .max_register = 0xff,
+       },
+       {
+               /* Address 0x49 */
+               .reg_bits = 8,
+               .val_bits = 8,
+               .max_register = 0xff,
+       },
+       {
+               /* Address 0x4a */
+               .reg_bits = 8,
+               .val_bits = 8,
+               .max_register = 0xff,
+       },
+       {
+               /* Address 0x4b */
+               .reg_bits = 8,
+               .val_bits = 8,
+               .max_register = 0xff,
+       },
+};
+
 static struct twl_mapping twl6030_map[] = {
        /*
         * NOTE:  don't change this table without updating the
@@ -254,14 +274,35 @@ static struct twl_mapping twl6030_map[] = {
        { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
        { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
        { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
+
        { SUB_CHIP_ID0, TWL6030_BASEADD_PM_MASTER },
        { SUB_CHIP_ID0, TWL6030_BASEADD_PM_SLAVE_MISC },
-
        { SUB_CHIP_ID0, TWL6030_BASEADD_RTC },
        { SUB_CHIP_ID0, TWL6030_BASEADD_MEM },
        { SUB_CHIP_ID1, TWL6025_BASEADD_CHARGER },
 };
 
+static struct regmap_config twl6030_regmap_config[3] = {
+       {
+               /* Address 0x48 */
+               .reg_bits = 8,
+               .val_bits = 8,
+               .max_register = 0xff,
+       },
+       {
+               /* Address 0x49 */
+               .reg_bits = 8,
+               .val_bits = 8,
+               .max_register = 0xff,
+       },
+       {
+               /* Address 0x4a */
+               .reg_bits = 8,
+               .val_bits = 8,
+               .max_register = 0xff,
+       },
+};
+
 /*----------------------------------------------------------------------*/
 
 /* Exported Functions */
@@ -283,9 +324,8 @@ int twl_i2c_write(u8 mod_no, u8 *value, u8 reg, unsigned num_bytes)
        int ret;
        int sid;
        struct twl_client *twl;
-       struct i2c_msg *msg;
 
-       if (unlikely(mod_no > TWL_MODULE_LAST)) {
+       if (unlikely(mod_no >= TWL_MODULE_LAST)) {
                pr_err("%s: invalid module number %d\n", DRIVER_NAME, mod_no);
                return -EPERM;
        }
@@ -301,32 +341,14 @@ int twl_i2c_write(u8 mod_no, u8 *value, u8 reg, unsigned num_bytes)
        }
        twl = &twl_modules[sid];
 
-       mutex_lock(&twl->xfer_lock);
-       /*
-        * [MSG1]: fill the register address data
-        * fill the data Tx buffer
-        */
-       msg = &twl->xfer_msg[0];
-       msg->addr = twl->address;
-       msg->len = num_bytes + 1;
-       msg->flags = 0;
-       msg->buf = value;
-       /* over write the first byte of buffer with the register address */
-       *value = twl_map[mod_no].base + reg;
-       ret = i2c_transfer(twl->client->adapter, twl->xfer_msg, 1);
-       mutex_unlock(&twl->xfer_lock);
-
-       /* i2c_transfer returns number of messages transferred */
-       if (ret != 1) {
-               pr_err("%s: i2c_write failed to transfer all messages\n",
-                       DRIVER_NAME);
-               if (ret < 0)
-                       return ret;
-               else
-                       return -EIO;
-       } else {
-               return 0;
-       }
+       ret = regmap_bulk_write(twl->regmap, twl_map[mod_no].base + reg,
+                               value, num_bytes);
+
+       if (ret)
+               pr_err("%s: Write failed (mod %d, reg 0x%02x count %d)\n",
+                      DRIVER_NAME, mod_no, reg, num_bytes);
+
+       return ret;
 }
 EXPORT_SYMBOL(twl_i2c_write);
 
@@ -342,12 +364,10 @@ EXPORT_SYMBOL(twl_i2c_write);
 int twl_i2c_read(u8 mod_no, u8 *value, u8 reg, unsigned num_bytes)
 {
        int ret;
-       u8 val;
        int sid;
        struct twl_client *twl;
-       struct i2c_msg *msg;
 
-       if (unlikely(mod_no > TWL_MODULE_LAST)) {
+       if (unlikely(mod_no >= TWL_MODULE_LAST)) {
                pr_err("%s: invalid module number %d\n", DRIVER_NAME, mod_no);
                return -EPERM;
        }
@@ -363,34 +383,14 @@ int twl_i2c_read(u8 mod_no, u8 *value, u8 reg, unsigned num_bytes)
        }
        twl = &twl_modules[sid];
 
-       mutex_lock(&twl->xfer_lock);
-       /* [MSG1] fill the register address data */
-       msg = &twl->xfer_msg[0];
-       msg->addr = twl->address;
-       msg->len = 1;
-       msg->flags = 0; /* Read the register value */
-       val = twl_map[mod_no].base + reg;
-       msg->buf = &val;
-       /* [MSG2] fill the data rx buffer */
-       msg = &twl->xfer_msg[1];
-       msg->addr = twl->address;
-       msg->flags = I2C_M_RD;  /* Read the register value */
-       msg->len = num_bytes;   /* only n bytes */
-       msg->buf = value;
-       ret = i2c_transfer(twl->client->adapter, twl->xfer_msg, 2);
-       mutex_unlock(&twl->xfer_lock);
-
-       /* i2c_transfer returns number of messages transferred */
-       if (ret != 2) {
-               pr_err("%s: i2c_read failed to transfer all messages\n",
-                       DRIVER_NAME);
-               if (ret < 0)
-                       return ret;
-               else
-                       return -EIO;
-       } else {
-               return 0;
-       }
+       ret = regmap_bulk_read(twl->regmap, twl_map[mod_no].base + reg,
+                              value, num_bytes);
+
+       if (ret)
+               pr_err("%s: Read failed (mod %d, reg 0x%02x count %d)\n",
+                      DRIVER_NAME, mod_no, reg, num_bytes);
+
+       return ret;
 }
 EXPORT_SYMBOL(twl_i2c_read);
 
@@ -404,12 +404,7 @@ EXPORT_SYMBOL(twl_i2c_read);
  */
 int twl_i2c_write_u8(u8 mod_no, u8 value, u8 reg)
 {
-
-       /* 2 bytes offset 1 contains the data offset 0 is used by i2c_write */
-       u8 temp_buffer[2] = { 0 };
-       /* offset 1 contains the data */
-       temp_buffer[1] = value;
-       return twl_i2c_write(mod_no, temp_buffer, reg, 1);
+       return twl_i2c_write(mod_no, &value, reg, 1);
 }
 EXPORT_SYMBOL(twl_i2c_write_u8);
 
@@ -646,8 +641,9 @@ add_children(struct twl4030_platform_data *pdata, unsigned irq_base,
                        return PTR_ERR(child);
        }
 
-       if (IS_ENABLED(CONFIG_TWL4030_MADC) && pdata->madc) {
-               child = add_child(2, "twl4030_madc",
+       if (IS_ENABLED(CONFIG_TWL4030_MADC) && pdata->madc &&
+           twl_class_is_4030()) {
+               child = add_child(SUB_CHIP_ID2, "twl4030_madc",
                                pdata->madc, sizeof(*pdata->madc),
                                true, irq_base + MADC_INTR_OFFSET, 0);
                if (IS_ERR(child))
@@ -663,15 +659,21 @@ add_children(struct twl4030_platform_data *pdata, unsigned irq_base,
                 * HW security concerns, and "least privilege".
                 */
                sub_chip_id = twl_map[TWL_MODULE_RTC].sid;
-               child = add_child(sub_chip_id, "twl_rtc",
-                               NULL, 0,
+               child = add_child(sub_chip_id, "twl_rtc", NULL, 0,
                                true, irq_base + RTC_INTR_OFFSET, 0);
                if (IS_ERR(child))
                        return PTR_ERR(child);
        }
 
-       if (IS_ENABLED(CONFIG_PWM_TWL6030) && twl_class_is_6030()) {
-               child = add_child(SUB_CHIP_ID1, "twl6030-pwm", NULL, 0,
+       if (IS_ENABLED(CONFIG_PWM_TWL)) {
+               child = add_child(SUB_CHIP_ID1, "twl-pwm", NULL, 0,
+                                 false, 0, 0);
+               if (IS_ERR(child))
+                       return PTR_ERR(child);
+       }
+
+       if (IS_ENABLED(CONFIG_PWM_TWL_LED)) {
+               child = add_child(SUB_CHIP_ID1, "twl-pwmled", NULL, 0,
                                  false, 0, 0);
                if (IS_ERR(child))
                        return PTR_ERR(child);
@@ -723,9 +725,8 @@ add_children(struct twl4030_platform_data *pdata, unsigned irq_base,
 
                }
 
-               child = add_child(0, "twl4030_usb",
-                               pdata->usb, sizeof(*pdata->usb),
-                               true,
+               child = add_child(SUB_CHIP_ID0, "twl4030_usb",
+                               pdata->usb, sizeof(*pdata->usb), true,
                                /* irq0 = USB_PRES, irq1 = USB */
                                irq_base + USB_PRES_INTR_OFFSET,
                                irq_base + USB_INTR_OFFSET);
@@ -773,9 +774,8 @@ add_children(struct twl4030_platform_data *pdata, unsigned irq_base,
 
                pdata->usb->features = features;
 
-               child = add_child(0, "twl6030_usb",
-                       pdata->usb, sizeof(*pdata->usb),
-                       true,
+               child = add_child(SUB_CHIP_ID0, "twl6030_usb",
+                       pdata->usb, sizeof(*pdata->usb), true,
                        /* irq1 = VBUS_PRES, irq0 = USB ID */
                        irq_base + USBOTG_INTR_OFFSET,
                        irq_base + USB_PRES_INTR_OFFSET);
@@ -799,22 +799,22 @@ add_children(struct twl4030_platform_data *pdata, unsigned irq_base,
        }
 
        if (IS_ENABLED(CONFIG_TWL4030_WATCHDOG) && twl_class_is_4030()) {
-               child = add_child(0, "twl4030_wdt", NULL, 0, false, 0, 0);
+               child = add_child(SUB_CHIP_ID3, "twl4030_wdt", NULL, 0,
+                                 false, 0, 0);
                if (IS_ERR(child))
                        return PTR_ERR(child);
        }
 
        if (IS_ENABLED(CONFIG_INPUT_TWL4030_PWRBUTTON) && twl_class_is_4030()) {
-               child = add_child(1, "twl4030_pwrbutton",
-                               NULL, 0, true, irq_base + 8 + 0, 0);
+               child = add_child(SUB_CHIP_ID3, "twl4030_pwrbutton", NULL, 0,
+                                 true, irq_base + 8 + 0, 0);
                if (IS_ERR(child))
                        return PTR_ERR(child);
        }
 
        if (IS_ENABLED(CONFIG_MFD_TWL4030_AUDIO) && pdata->audio &&
            twl_class_is_4030()) {
-               sub_chip_id = twl_map[TWL_MODULE_AUDIO_VOICE].sid;
-               child = add_child(sub_chip_id, "twl4030-audio",
+               child = add_child(SUB_CHIP_ID1, "twl4030-audio",
                                pdata->audio, sizeof(*pdata->audio),
                                false, 0, 0);
                if (IS_ERR(child))
@@ -1054,7 +1054,7 @@ add_children(struct twl4030_platform_data *pdata, unsigned irq_base,
 
        if (IS_ENABLED(CONFIG_CHARGER_TWL4030) && pdata->bci &&
                        !(features & (TPS_SUBSET | TWL5031))) {
-               child = add_child(3, "twl4030_bci",
+               child = add_child(SUB_CHIP_ID3, "twl4030_bci",
                                pdata->bci, sizeof(*pdata->bci), false,
                                /* irq0 = CHG_PRES, irq1 = BCI */
                                irq_base + BCI_PRES_INTR_OFFSET,
@@ -1077,8 +1077,8 @@ static inline int __init protect_pm_master(void)
 {
        int e = 0;
 
-       e = twl_i2c_write_u8(TWL4030_MODULE_PM_MASTER, 0,
-                       TWL4030_PM_MASTER_PROTECT_KEY);
+       e = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, 0,
+                            TWL4030_PM_MASTER_PROTECT_KEY);
        return e;
 }
 
@@ -1086,12 +1086,10 @@ static inline int __init unprotect_pm_master(void)
 {
        int e = 0;
 
-       e |= twl_i2c_write_u8(TWL4030_MODULE_PM_MASTER,
-                       TWL4030_PM_MASTER_KEY_CFG1,
-                       TWL4030_PM_MASTER_PROTECT_KEY);
-       e |= twl_i2c_write_u8(TWL4030_MODULE_PM_MASTER,
-                       TWL4030_PM_MASTER_KEY_CFG2,
-                       TWL4030_PM_MASTER_PROTECT_KEY);
+       e |= twl_i2c_write_u8(TWL_MODULE_PM_MASTER, TWL4030_PM_MASTER_KEY_CFG1,
+                             TWL4030_PM_MASTER_PROTECT_KEY);
+       e |= twl_i2c_write_u8(TWL_MODULE_PM_MASTER, TWL4030_PM_MASTER_KEY_CFG2,
+                             TWL4030_PM_MASTER_PROTECT_KEY);
 
        return e;
 }
@@ -1176,6 +1174,7 @@ twl_probe(struct i2c_client *client, const struct i2c_device_id *id)
        struct twl4030_platform_data    *pdata = client->dev.platform_data;
        struct device_node              *node = client->dev.of_node;
        struct platform_device          *pdev;
+       struct regmap_config            *twl_regmap_config;
        int                             irq_base = 0;
        int                             status;
        unsigned                        i, num_slaves;
@@ -1229,22 +1228,23 @@ twl_probe(struct i2c_client *client, const struct i2c_device_id *id)
        if ((id->driver_data) & TWL6030_CLASS) {
                twl_id = TWL6030_CLASS_ID;
                twl_map = &twl6030_map[0];
+               twl_regmap_config = twl6030_regmap_config;
                num_slaves = TWL_NUM_SLAVES - 1;
        } else {
                twl_id = TWL4030_CLASS_ID;
                twl_map = &twl4030_map[0];
+               twl_regmap_config = twl4030_regmap_config;
                num_slaves = TWL_NUM_SLAVES;
        }
 
        for (i = 0; i < num_slaves; i++) {
                struct twl_client *twl = &twl_modules[i];
 
-               twl->address = client->addr + i;
                if (i == 0) {
                        twl->client = client;
                } else {
                        twl->client = i2c_new_dummy(client->adapter,
-                                       twl->address);
+                                                   client->addr + i);
                        if (!twl->client) {
                                dev_err(&client->dev,
                                        "can't attach client %d\n", i);
@@ -1252,7 +1252,16 @@ twl_probe(struct i2c_client *client, const struct i2c_device_id *id)
                                goto fail;
                        }
                }
-               mutex_init(&twl->xfer_lock);
+
+               twl->regmap = devm_regmap_init_i2c(twl->client,
+                                                  &twl_regmap_config[i]);
+               if (IS_ERR(twl->regmap)) {
+                       status = PTR_ERR(twl->regmap);
+                       dev_err(&client->dev,
+                               "Failed to allocate regmap %d, err: %d\n", i,
+                               status);
+                       goto fail;
+               }
        }
 
        inuse = true;