mfd: twl4030-power: Add support for board specific configuration
[linux-2.6-block.git] / drivers / mfd / twl4030-power.c
index 96162b62f3c0897df3f923ee0d4befcebfcb9048..4846c7b48ebb3d86b41836730390efa1713944a1 100644 (file)
 #include <linux/i2c/twl.h>
 #include <linux/platform_device.h>
 #include <linux/of.h>
+#include <linux/of_device.h>
 
 #include <asm/mach-types.h>
 
 static u8 twl4030_start_script_address = 0x2b;
 
-#define PWR_P1_SW_EVENTS       0x10
-#define PWR_DEVOFF             (1 << 0)
+/* Register bits for P1, P2 and P3_SW_EVENTS */
+#define PWR_STOPON_PRWON       BIT(6)
+#define PWR_STOPON_SYSEN       BIT(5)
+#define PWR_ENABLE_WARMRESET   BIT(4)
+#define PWR_LVL_WAKEUP         BIT(3)
+#define PWR_DEVACT             BIT(2)
+#define PWR_DEVSLP             BIT(1)
+#define PWR_DEVOFF             BIT(0)
+
 #define SEQ_OFFSYNC            (1 << 0)
 
 #define PHY_TO_OFF_PM_MASTER(p)                (p - 0x36)
@@ -52,10 +60,6 @@ static u8 twl4030_start_script_address = 0x2b;
 #define R_CFG_P2_TRANSITION    PHY_TO_OFF_PM_MASTER(0x37)
 #define R_CFG_P3_TRANSITION    PHY_TO_OFF_PM_MASTER(0x38)
 
-#define LVL_WAKEUP     0x08
-
-#define ENABLE_WARMRESET (1<<4)
-
 #define END_OF_SCRIPT          0x3f
 
 #define R_SEQ_ADD_A2S          PHY_TO_OFF_PM_MASTER(0x55)
@@ -125,6 +129,53 @@ static u8 res_config_addrs[] = {
        [RES_MAIN_REF]  = 0x94,
 };
 
+/*
+ * Usable values for .remap_sleep and .remap_off
+ * Based on table "5.3.3 Resource Operating modes"
+ */
+enum {
+       TWL_REMAP_OFF = 0,
+       TWL_REMAP_SLEEP = 8,
+       TWL_REMAP_ACTIVE = 9,
+};
+
+/*
+ * Macros to configure the PM register states for various resources.
+ * Note that we can make MSG_SINGULAR etc private to this driver once
+ * omap3 has been made DT only.
+ */
+#define TWL_DFLT_DELAY         2       /* typically 2 32 KiHz cycles */
+#define TWL_DEV_GRP_P123       (DEV_GRP_P1 | DEV_GRP_P2 | DEV_GRP_P3)
+#define TWL_RESOURCE_SET(res, state)                                   \
+       { MSG_SINGULAR(DEV_GRP_NULL, (res), (state)), TWL_DFLT_DELAY }
+#define TWL_RESOURCE_ON(res)   TWL_RESOURCE_SET(res, RES_STATE_ACTIVE)
+#define TWL_RESOURCE_OFF(res)  TWL_RESOURCE_SET(res, RES_STATE_OFF)
+#define TWL_RESOURCE_RESET(res)        TWL_RESOURCE_SET(res, RES_STATE_WRST)
+/*
+ * It seems that type1 and type2 is just the resource init order
+ * number for the type1 and type2 group.
+ */
+#define TWL_RESOURCE_SET_ACTIVE(res, state)                            \
+       { MSG_SINGULAR(DEV_GRP_NULL, (res), RES_STATE_ACTIVE), (state) }
+#define TWL_RESOURCE_GROUP_RESET(group, type1, type2)                  \
+       { MSG_BROADCAST(DEV_GRP_NULL, (group), (type1), (type2),        \
+               RES_STATE_WRST), TWL_DFLT_DELAY }
+#define TWL_RESOURCE_GROUP_SLEEP(group, type, type2)                   \
+       { MSG_BROADCAST(DEV_GRP_NULL, (group), (type), (type2),         \
+               RES_STATE_SLEEP), TWL_DFLT_DELAY }
+#define TWL_RESOURCE_GROUP_ACTIVE(group, type, type2)                  \
+       { MSG_BROADCAST(DEV_GRP_NULL, (group), (type), (type2),         \
+               RES_STATE_ACTIVE), TWL_DFLT_DELAY }
+#define TWL_REMAP_SLEEP(res, devgrp, typ, typ2)                                \
+       { .resource = (res), .devgroup = (devgrp),                      \
+         .type = (typ), .type2 = (typ2),                               \
+         .remap_off = TWL_REMAP_OFF,                                   \
+         .remap_sleep = TWL_REMAP_SLEEP, }
+#define TWL_REMAP_OFF(res, devgrp, typ, typ2)                          \
+       { .resource = (res), .devgroup = (devgrp),                      \
+         .type = (typ), .type2 = (typ2),                               \
+         .remap_off = TWL_REMAP_OFF, .remap_sleep = TWL_REMAP_OFF, }
+
 static int twl4030_write_script_byte(u8 address, u8 byte)
 {
        int err;
@@ -196,7 +247,7 @@ static int twl4030_config_wakeup3_sequence(u8 address)
        err = twl_i2c_read_u8(TWL_MODULE_PM_MASTER, &data, R_P3_SW_EVENTS);
        if (err)
                goto out;
-       data |= LVL_WAKEUP;
+       data |= PWR_LVL_WAKEUP;
        err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, data, R_P3_SW_EVENTS);
 out:
        if (err)
@@ -219,7 +270,7 @@ static int twl4030_config_wakeup12_sequence(u8 address)
        if (err)
                goto out;
 
-       data |= LVL_WAKEUP;
+       data |= PWR_LVL_WAKEUP;
        err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, data, R_P1_SW_EVENTS);
        if (err)
                goto out;
@@ -228,7 +279,7 @@ static int twl4030_config_wakeup12_sequence(u8 address)
        if (err)
                goto out;
 
-       data |= LVL_WAKEUP;
+       data |= PWR_LVL_WAKEUP;
        err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, data, R_P2_SW_EVENTS);
        if (err)
                goto out;
@@ -281,7 +332,7 @@ static int twl4030_config_warmreset_sequence(u8 address)
        if (err)
                goto out;
 
-       rd_data |= ENABLE_WARMRESET;
+       rd_data |= PWR_ENABLE_WARMRESET;
        err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, rd_data, R_P1_SW_EVENTS);
        if (err)
                goto out;
@@ -290,7 +341,7 @@ static int twl4030_config_warmreset_sequence(u8 address)
        if (err)
                goto out;
 
-       rd_data |= ENABLE_WARMRESET;
+       rd_data |= PWR_ENABLE_WARMRESET;
        err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, rd_data, R_P2_SW_EVENTS);
        if (err)
                goto out;
@@ -299,7 +350,7 @@ static int twl4030_config_warmreset_sequence(u8 address)
        if (err)
                goto out;
 
-       rd_data |= ENABLE_WARMRESET;
+       rd_data |= PWR_ENABLE_WARMRESET;
        err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, rd_data, R_P3_SW_EVENTS);
 out:
        if (err)
@@ -421,6 +472,12 @@ static int load_twl4030_script(struct twl4030_script *tscript,
                        goto out;
        }
        if (tscript->flags & TWL4030_WAKEUP12_SCRIPT) {
+               /* Reset any existing sleep script to avoid hangs on reboot */
+               err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, END_OF_SCRIPT,
+                                      R_SEQ_ADD_A2S);
+               if (err)
+                       goto out;
+
                err = twl4030_config_wakeup12_sequence(address);
                if (err)
                        goto out;
@@ -493,7 +550,8 @@ int twl4030_remove_script(u8 flags)
        return err;
 }
 
-static int twl4030_power_configure_scripts(struct twl4030_power_data *pdata)
+static int
+twl4030_power_configure_scripts(const struct twl4030_power_data *pdata)
 {
        int err;
        int i;
@@ -509,12 +567,34 @@ static int twl4030_power_configure_scripts(struct twl4030_power_data *pdata)
        return 0;
 }
 
-static int twl4030_power_configure_resources(struct twl4030_power_data *pdata)
+static void twl4030_patch_rconfig(struct twl4030_resconfig *common,
+                                 struct twl4030_resconfig *board)
+{
+       while (common->resource) {
+               struct twl4030_resconfig *b = board;
+
+               while (b->resource) {
+                       if (b->resource == common->resource) {
+                               *common = *b;
+                               break;
+                       }
+                       b++;
+               }
+               common++;
+       }
+}
+
+static int
+twl4030_power_configure_resources(const struct twl4030_power_data *pdata)
 {
        struct twl4030_resconfig *resconfig = pdata->resource_config;
+       struct twl4030_resconfig *boardconf = pdata->board_config;
        int err;
 
        if (resconfig) {
+               if (boardconf)
+                       twl4030_patch_rconfig(resconfig, boardconf);
+
                while (resconfig->resource) {
                        err = twl4030_configure_resource(resconfig);
                        if (err)
@@ -541,7 +621,7 @@ void twl4030_power_off(void)
                pr_err("TWL4030 Unable to power off\n");
 }
 
-static bool twl4030_power_use_poweroff(struct twl4030_power_data *pdata,
+static bool twl4030_power_use_poweroff(const struct twl4030_power_data *pdata,
                                        struct device_node *node)
 {
        if (pdata && pdata->use_poweroff)
@@ -553,10 +633,153 @@ static bool twl4030_power_use_poweroff(struct twl4030_power_data *pdata,
        return false;
 }
 
+#ifdef CONFIG_OF
+
+/* Generic warm reset configuration for omap3 */
+
+static struct twl4030_ins omap3_wrst_seq[] = {
+       TWL_RESOURCE_OFF(RES_NRES_PWRON),
+       TWL_RESOURCE_OFF(RES_RESET),
+       TWL_RESOURCE_RESET(RES_MAIN_REF),
+       TWL_RESOURCE_GROUP_RESET(RES_GRP_ALL, RES_TYPE_R0, RES_TYPE2_R2),
+       TWL_RESOURCE_RESET(RES_VUSB_3V1),
+       TWL_RESOURCE_GROUP_RESET(RES_GRP_ALL, RES_TYPE_R0, RES_TYPE2_R1),
+       TWL_RESOURCE_GROUP_RESET(RES_GRP_RC, RES_TYPE_ALL, RES_TYPE2_R0),
+       TWL_RESOURCE_ON(RES_RESET),
+       TWL_RESOURCE_ON(RES_NRES_PWRON),
+};
+
+static struct twl4030_script omap3_wrst_script = {
+       .script = omap3_wrst_seq,
+       .size   = ARRAY_SIZE(omap3_wrst_seq),
+       .flags  = TWL4030_WRST_SCRIPT,
+};
+
+static struct twl4030_script *omap3_reset_scripts[] = {
+       &omap3_wrst_script,
+};
+
+static struct twl4030_resconfig omap3_rconfig[] = {
+       TWL_REMAP_SLEEP(RES_HFCLKOUT, DEV_GRP_P3, -1, -1),
+       TWL_REMAP_SLEEP(RES_VDD1, DEV_GRP_P1, -1, -1),
+       TWL_REMAP_SLEEP(RES_VDD2, DEV_GRP_P1, -1, -1),
+       { 0, 0 },
+};
+
+static struct twl4030_power_data omap3_reset = {
+       .scripts                = omap3_reset_scripts,
+       .num                    = ARRAY_SIZE(omap3_reset_scripts),
+       .resource_config        = omap3_rconfig,
+};
+
+/* Recommended generic default idle configuration for off-idle */
+
+/* Broadcast message to put res to sleep */
+static struct twl4030_ins omap3_idle_sleep_on_seq[] = {
+       TWL_RESOURCE_GROUP_SLEEP(RES_GRP_ALL, RES_TYPE_ALL, 0),
+};
+
+static struct twl4030_script omap3_idle_sleep_on_script = {
+       .script = omap3_idle_sleep_on_seq,
+       .size   = ARRAY_SIZE(omap3_idle_sleep_on_seq),
+       .flags  = TWL4030_SLEEP_SCRIPT,
+};
+
+/* Broadcast message to put res to active */
+static struct twl4030_ins omap3_idle_wakeup_p12_seq[] = {
+       TWL_RESOURCE_GROUP_ACTIVE(RES_GRP_ALL, RES_TYPE_ALL, 0),
+};
+
+static struct twl4030_script omap3_idle_wakeup_p12_script = {
+       .script = omap3_idle_wakeup_p12_seq,
+       .size   = ARRAY_SIZE(omap3_idle_wakeup_p12_seq),
+       .flags  = TWL4030_WAKEUP12_SCRIPT,
+};
+
+/* Broadcast message to put res to active */
+static struct twl4030_ins omap3_idle_wakeup_p3_seq[] = {
+       TWL_RESOURCE_SET_ACTIVE(RES_CLKEN, 0x37),
+       TWL_RESOURCE_GROUP_ACTIVE(RES_GRP_ALL, RES_TYPE_ALL, 0),
+};
+
+static struct twl4030_script omap3_idle_wakeup_p3_script = {
+       .script = omap3_idle_wakeup_p3_seq,
+       .size   = ARRAY_SIZE(omap3_idle_wakeup_p3_seq),
+       .flags  = TWL4030_WAKEUP3_SCRIPT,
+};
+
+static struct twl4030_script *omap3_idle_scripts[] = {
+       &omap3_idle_wakeup_p12_script,
+       &omap3_idle_wakeup_p3_script,
+       &omap3_wrst_script,
+       &omap3_idle_sleep_on_script,
+};
+
+/*
+ * Recommended configuration based on "Recommended Sleep
+ * Sequences for the Zoom Platform":
+ * http://omappedia.com/wiki/File:Recommended_Sleep_Sequences_Zoom.pdf
+ * Note that the type1 and type2 seem to be just the init order number
+ * for type1 and type2 groups as specified in the document mentioned
+ * above.
+ */
+static struct twl4030_resconfig omap3_idle_rconfig[] = {
+       TWL_REMAP_SLEEP(RES_VAUX1, DEV_GRP_NULL, 0, 0),
+       TWL_REMAP_SLEEP(RES_VAUX2, DEV_GRP_NULL, 0, 0),
+       TWL_REMAP_SLEEP(RES_VAUX3, DEV_GRP_NULL, 0, 0),
+       TWL_REMAP_SLEEP(RES_VAUX4, DEV_GRP_NULL, 0, 0),
+       TWL_REMAP_SLEEP(RES_VMMC1, DEV_GRP_NULL, 0, 0),
+       TWL_REMAP_SLEEP(RES_VMMC2, DEV_GRP_NULL, 0, 0),
+       TWL_REMAP_OFF(RES_VPLL1, DEV_GRP_P1, 3, 1),
+       TWL_REMAP_SLEEP(RES_VPLL2, DEV_GRP_P1, 0, 0),
+       TWL_REMAP_SLEEP(RES_VSIM, DEV_GRP_NULL, 0, 0),
+       TWL_REMAP_SLEEP(RES_VDAC, DEV_GRP_NULL, 0, 0),
+       TWL_REMAP_SLEEP(RES_VINTANA1, TWL_DEV_GRP_P123, 1, 2),
+       TWL_REMAP_SLEEP(RES_VINTANA2, TWL_DEV_GRP_P123, 0, 2),
+       TWL_REMAP_SLEEP(RES_VINTDIG, TWL_DEV_GRP_P123, 1, 2),
+       TWL_REMAP_SLEEP(RES_VIO, TWL_DEV_GRP_P123, 2, 2),
+       TWL_REMAP_OFF(RES_VDD1, DEV_GRP_P1, 4, 1),
+       TWL_REMAP_OFF(RES_VDD2, DEV_GRP_P1, 3, 1),
+       TWL_REMAP_SLEEP(RES_VUSB_1V5, DEV_GRP_NULL, 0, 0),
+       TWL_REMAP_SLEEP(RES_VUSB_1V8, DEV_GRP_NULL, 0, 0),
+       TWL_REMAP_SLEEP(RES_VUSB_3V1, TWL_DEV_GRP_P123, 0, 0),
+       /* Resource #20 USB charge pump skipped */
+       TWL_REMAP_SLEEP(RES_REGEN, TWL_DEV_GRP_P123, 2, 1),
+       TWL_REMAP_SLEEP(RES_NRES_PWRON, TWL_DEV_GRP_P123, 0, 1),
+       TWL_REMAP_SLEEP(RES_CLKEN, TWL_DEV_GRP_P123, 3, 2),
+       TWL_REMAP_SLEEP(RES_SYSEN, TWL_DEV_GRP_P123, 6, 1),
+       TWL_REMAP_SLEEP(RES_HFCLKOUT, DEV_GRP_P3, 0, 2),
+       TWL_REMAP_SLEEP(RES_32KCLKOUT, TWL_DEV_GRP_P123, 0, 0),
+       TWL_REMAP_SLEEP(RES_RESET, TWL_DEV_GRP_P123, 6, 0),
+       TWL_REMAP_SLEEP(RES_MAIN_REF, TWL_DEV_GRP_P123, 0, 0),
+       { /* Terminator */ },
+};
+
+static struct twl4030_power_data omap3_idle = {
+       .scripts                = omap3_idle_scripts,
+       .num                    = ARRAY_SIZE(omap3_idle_scripts),
+       .resource_config        = omap3_idle_rconfig,
+};
+
+static struct of_device_id twl4030_power_of_match[] = {
+       {
+               .compatible = "ti,twl4030-power-reset",
+               .data = &omap3_reset,
+       },
+       {
+               .compatible = "ti,twl4030-power-idle",
+               .data = &omap3_idle,
+       },
+       { },
+};
+MODULE_DEVICE_TABLE(of, twl4030_power_of_match);
+#endif /* CONFIG_OF */
+
 static int twl4030_power_probe(struct platform_device *pdev)
 {
-       struct twl4030_power_data *pdata = dev_get_platdata(&pdev->dev);
+       const struct twl4030_power_data *pdata = dev_get_platdata(&pdev->dev);
        struct device_node *node = pdev->dev.of_node;
+       const struct of_device_id *match;
        int err = 0;
        int err2 = 0;
        u8 val;
@@ -577,8 +800,12 @@ static int twl4030_power_probe(struct platform_device *pdev)
                return err;
        }
 
+       match = of_match_device(of_match_ptr(twl4030_power_of_match),
+                               &pdev->dev);
+       if (match && match->data)
+               pdata = match->data;
+
        if (pdata) {
-               /* TODO: convert to device tree */
                err = twl4030_power_configure_scripts(pdata);
                if (err) {
                        pr_err("TWL4030 failed to load scripts\n");
@@ -628,14 +855,6 @@ static int twl4030_power_remove(struct platform_device *pdev)
        return 0;
 }
 
-#ifdef CONFIG_OF
-static const struct of_device_id twl4030_power_of_match[] = {
-       {.compatible = "ti,twl4030-power", },
-       { },
-};
-MODULE_DEVICE_TABLE(of, twl4030_power_of_match);
-#endif
-
 static struct platform_driver twl4030_power_driver = {
        .driver = {
                .name   = "twl4030_power",