msm: lpm: Remove enums in the device tree

Add pxo, l2 and sleep modes enum to string mapping. Add functional
interface to get enum values all resource types, sleep modes.

Change-Id: I8200b17863fe20bc327f42c28549d505616ab6a9
Signed-off-by: Archana Sathyakumar <asathyak@codeaurora.org>
diff --git a/arch/arm/mach-msm/lpm_levels.c b/arch/arm/mach-msm/lpm_levels.c
index 8194721..aa33f2c 100644
--- a/arch/arm/mach-msm/lpm_levels.c
+++ b/arch/arm/mach-msm/lpm_levels.c
@@ -28,6 +28,8 @@
 	MSM_LPM_LVL_DBG_IDLE_LIMITS = BIT(1),
 };
 
+#define MAX_STR_LEN 30
+
 static int msm_lpm_lvl_dbg_msk;
 
 module_param_named(
@@ -41,6 +43,48 @@
 static DEFINE_PER_CPU(int , lpm_permitted_level);
 static DEFINE_PER_CPU(struct atomic_notifier_head, lpm_notify_head);
 
+static int msm_pm_get_sleep_mode_value(struct device_node *node,
+			const char *key, uint32_t *sleep_mode_val)
+{
+	int i;
+	struct lpm_lookup_table {
+		uint32_t modes;
+		const char *mode_name;
+	};
+	struct lpm_lookup_table pm_sm_lookup[] = {
+		{MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT,
+			"wfi"},
+		{MSM_PM_SLEEP_MODE_RAMP_DOWN_AND_WAIT_FOR_INTERRUPT,
+			"ramp_down_and_wfi"},
+		{MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE,
+			"standalone_pc"},
+		{MSM_PM_SLEEP_MODE_POWER_COLLAPSE,
+			"pc"},
+		{MSM_PM_SLEEP_MODE_RETENTION,
+			"retention"},
+		{MSM_PM_SLEEP_MODE_POWER_COLLAPSE_SUSPEND,
+			"pc_suspend"},
+		{MSM_PM_SLEEP_MODE_POWER_COLLAPSE_NO_XO_SHUTDOWN,
+			"pc_no_xo_shutdown"}
+	};
+	int ret;
+	const char *mode_name;
+
+	ret = of_property_read_string(node, key, &mode_name);
+	if (!ret) {
+		ret = -EINVAL;
+		for (i = 0; i < ARRAY_SIZE(pm_sm_lookup); i++) {
+			if (!strncmp(mode_name, pm_sm_lookup[i].mode_name,
+				MAX_STR_LEN)) {
+				*sleep_mode_val = pm_sm_lookup[i].modes;
+				ret = 0;
+				break;
+			}
+		}
+	}
+	return ret;
+}
+
 static void msm_lpm_level_update(void)
 {
 	unsigned int lpm_level;
@@ -343,19 +387,19 @@
 		level->available = false;
 
 		key = "qcom,mode";
-		ret = of_property_read_u32(node, key, &val);
+		ret = msm_pm_get_sleep_mode_value(node, key, &val);
 		if (ret)
 			goto fail;
 		level->sleep_mode = val;
 
 		key = "qcom,xo";
-		ret = of_property_read_u32(node, key, &val);
+		ret = msm_lpm_get_xo_value(node, key, &val);
 		if (ret)
 			goto fail;
 		level->rs_limits.pxo = val;
 
 		key = "qcom,l2";
-		ret = of_property_read_u32(node, key, &val);
+		ret = msm_lpm_get_l2_cache_value(node, key, &val);
 		if (ret)
 			goto fail;
 		level->rs_limits.l2_cache = val;
diff --git a/arch/arm/mach-msm/lpm_resources.c b/arch/arm/mach-msm/lpm_resources.c
index 60184b4..f0e5ebd 100644
--- a/arch/arm/mach-msm/lpm_resources.c
+++ b/arch/arm/mach-msm/lpm_resources.c
@@ -51,29 +51,39 @@
 #define MAX_RS_SIZE		(4)
 #define IS_RPM_CTL(rs) \
 	(!strncmp(rs->name, "rpm_ctl", MAX_RS_NAME))
+#define MAX_STR_LEN		30
 
 static bool msm_lpm_beyond_limits_vdd_dig(struct msm_rpmrs_limits *limits);
 static void msm_lpm_aggregate_vdd_dig(struct msm_rpmrs_limits *limits);
 static void msm_lpm_flush_vdd_dig(int notify_rpm);
 static void msm_lpm_notify_vdd_dig(struct msm_rpm_notifier_data
 					*rpm_notifier_cb);
+static int msm_lpm_init_value_vdd_dig(struct device_node *node,
+					char *key, uint32_t *default_value);
 
 static bool msm_lpm_beyond_limits_vdd_mem(struct msm_rpmrs_limits *limits);
 static void msm_lpm_aggregate_vdd_mem(struct msm_rpmrs_limits *limits);
 static void msm_lpm_flush_vdd_mem(int notify_rpm);
 static void msm_lpm_notify_vdd_mem(struct msm_rpm_notifier_data
 					*rpm_notifier_cb);
+static int msm_lpm_init_value_vdd_mem(struct device_node *node,
+					char *key, uint32_t *default_value);
+
 
 static bool msm_lpm_beyond_limits_pxo(struct msm_rpmrs_limits *limits);
 static void msm_lpm_aggregate_pxo(struct msm_rpmrs_limits *limits);
 static void msm_lpm_flush_pxo(int notify_rpm);
 static void msm_lpm_notify_pxo(struct msm_rpm_notifier_data
 					*rpm_notifier_cb);
+static int msm_lpm_init_value_pxo(struct device_node *node,
+				char *key, uint32_t *default_value);
 
 
 static bool msm_lpm_beyond_limits_l2(struct msm_rpmrs_limits *limits);
 static void msm_lpm_flush_l2(int notify_rpm);
 static void msm_lpm_aggregate_l2(struct msm_rpmrs_limits *limits);
+static int msm_lpm_init_value_l2(struct device_node *node,
+				char *key, uint32_t *default_value);
 
 static void msm_lpm_flush_rpm_ctl(int notify_rpm);
 
@@ -127,8 +137,14 @@
 	void (*flush)(int notify_rpm);
 	void (*notify)(struct msm_rpm_notifier_data *rpm_notifier_cb);
 	struct kobj_attribute ko_attr;
+	int (*init_value)(struct device_node *node,
+			char *key, uint32_t *default_value);
 };
 
+struct lpm_lookup_table {
+	uint32_t modes;
+	const char *mode_name;
+};
 
 static struct msm_lpm_resource msm_lpm_l2 = {
 	.name = "l2",
@@ -138,6 +154,7 @@
 	.notify = NULL,
 	.valid = false,
 	.ko_attr = RPMRS_ATTR(l2),
+	.init_value = msm_lpm_init_value_l2,
 };
 
 static struct msm_lpm_resource msm_lpm_vdd_dig = {
@@ -148,6 +165,7 @@
 	.notify = msm_lpm_notify_vdd_dig,
 	.valid = false,
 	.ko_attr = RPMRS_ATTR(vdd_dig),
+	.init_value = msm_lpm_init_value_vdd_dig,
 };
 
 static struct msm_lpm_resource msm_lpm_vdd_mem = {
@@ -158,6 +176,7 @@
 	.notify = msm_lpm_notify_vdd_mem,
 	.valid = false,
 	.ko_attr = RPMRS_ATTR(vdd_mem),
+	.init_value = msm_lpm_init_value_vdd_mem,
 };
 
 static struct msm_lpm_resource msm_lpm_pxo = {
@@ -168,6 +187,7 @@
 	.notify = msm_lpm_notify_pxo,
 	.valid = false,
 	.ko_attr = RPMRS_ATTR(pxo),
+	.init_value = msm_lpm_init_value_pxo,
 };
 
 static struct msm_lpm_resource *msm_lpm_resources[] = {
@@ -444,6 +464,12 @@
 				__func__, lpm);
 }
 
+static int msm_lpm_init_value_l2(struct device_node *node,
+					char *key, uint32_t *default_value)
+{
+	return msm_lpm_get_l2_cache_value(node, key, default_value);
+}
+
 static void msm_lpm_flush_l2(int notify_rpm)
 {
 	struct msm_lpm_resource *rs = &msm_lpm_l2;
@@ -451,6 +477,34 @@
 	msm_lpm_set_l2_mode(rs->sleep_value, notify_rpm);
 }
 
+int msm_lpm_get_l2_cache_value(struct device_node *node,
+			char *key, uint32_t *l2_val)
+{
+	int i;
+	struct lpm_lookup_table l2_mode_lookup[] = {
+		{MSM_LPM_L2_CACHE_HSFS_OPEN, "l2_cache_pc"},
+		{MSM_LPM_L2_CACHE_GDHS, "l2_cache_gdhs"},
+		{MSM_LPM_L2_CACHE_RETENTION, "l2_cache_retention"},
+		{MSM_LPM_L2_CACHE_ACTIVE, "l2_cache_active"}
+	};
+	const char *l2_str;
+	int ret;
+
+	ret = of_property_read_string(node, key, &l2_str);
+	if (!ret) {
+		ret = -EINVAL;
+		for (i = 0; i < ARRAY_SIZE(l2_mode_lookup); i++) {
+			if (!strncmp(l2_str, l2_mode_lookup[i].mode_name,
+				MAX_STR_LEN)) {
+				*l2_val = l2_mode_lookup[i].modes;
+				ret = 0;
+				break;
+			}
+		}
+	}
+	return ret;
+}
+
 /* RPM CTL */
 static void msm_lpm_flush_rpm_ctl(int notify_rpm)
 {
@@ -484,6 +538,12 @@
 	return ret;
 }
 
+static int msm_lpm_init_value_vdd_dig(struct device_node *node,
+					char *key, uint32_t *default_value)
+{
+	return of_property_read_u32(node, key, default_value);
+}
+
 static void msm_lpm_aggregate_vdd_dig(struct msm_rpmrs_limits *limits)
 {
 	struct msm_lpm_resource *rs = &msm_lpm_vdd_dig;
@@ -570,6 +630,12 @@
 	msm_lpm_notify_common(rpm_notifier_cb, rs);
 }
 
+static int msm_lpm_init_value_vdd_mem(struct device_node *node,
+					char *key, uint32_t *default_value)
+{
+	return of_property_read_u32(node, key, default_value);
+}
+
 /*PXO*/
 static bool msm_lpm_beyond_limits_pxo(struct msm_rpmrs_limits *limits)
 {
@@ -628,11 +694,43 @@
 	msm_lpm_notify_common(rpm_notifier_cb, rs);
 }
 
+static int msm_lpm_init_value_pxo(struct device_node *node,
+				char *key, uint32_t *default_value)
+{
+	return msm_lpm_get_xo_value(node, key, default_value);
+}
+
 static inline bool msm_lpm_use_mpm(struct msm_rpmrs_limits *limits)
 {
 	return (limits->pxo == MSM_LPM_PXO_OFF);
 }
 
+int msm_lpm_get_xo_value(struct device_node *node,
+			char *key, uint32_t *xo_val)
+{
+	int  i;
+	struct lpm_lookup_table pxo_mode_lookup[] = {
+		{MSM_LPM_PXO_OFF, "xo_off"},
+		{MSM_LPM_PXO_ON, "xo_on"}
+	};
+	const char *xo_str;
+	int ret;
+
+	ret = of_property_read_string(node, key, &xo_str);
+	if (!ret) {
+		ret = -EINVAL;
+		for (i = 0; i < ARRAY_SIZE(pxo_mode_lookup); i++) {
+			if (!strncmp(xo_str, pxo_mode_lookup[i].mode_name,
+				MAX_STR_LEN)) {
+				*xo_val = pxo_mode_lookup[i].modes;
+				ret = 0;
+				break;
+			}
+		}
+	}
+	return ret;
+}
+
 /* LPM levels interface */
 bool msm_lpm_level_beyond_limit(struct msm_rpmrs_limits *limits)
 {
@@ -797,7 +895,7 @@
 		struct msm_lpm_resource *rs = NULL;
 		const char *val;
 		int i;
-		uint32_t resource_type;
+		bool local_resource;
 
 		key = "qcom,name";
 		ret = of_property_read_string(node, key, &val);
@@ -822,8 +920,7 @@
 		}
 
 		key = "qcom,init-value";
-		ret = of_property_read_u32(node, key,
-				&rs->rs_data.default_value);
+		ret = rs->init_value(node, key, &rs->rs_data.default_value);
 		if (ret) {
 			pr_err("%s():Failed to read %s\n", __func__, key);
 			goto fail;
@@ -831,18 +928,13 @@
 
 		rs->rs_data.value = rs->rs_data.default_value;
 
-		key = "qcom,resource-type";
-		ret = of_property_read_u32(node, key, &resource_type);
-		if (ret) {
-			pr_err("Failed to read resource-type\n");
-			goto fail;
-		}
+		key = "qcom,local-resource-type";
+		local_resource = of_property_read_bool(node, key);
 
-		switch (resource_type) {
-		case MSM_LPM_RPM_RS_TYPE:
+		if (!local_resource) {
 			key = "qcom,type";
 			ret = of_property_read_u32(node, key,
-						&rs->rs_data.type);
+					&rs->rs_data.type);
 			if (ret) {
 				pr_err("Failed to read type\n");
 				goto fail;
@@ -873,15 +965,9 @@
 				goto fail;
 			}
 			/* fall through */
-
-		case MSM_LPM_LOCAL_RS_TYPE:
-			rs->valid = true;
-			break;
-		default:
-			pr_err("%s: Invalid resource type %d", __func__,
-					resource_type);
-			goto fail;
 		}
+
+		rs->valid = true;
 	}
 	msm_rpm_register_notifier(&msm_lpm_rpm_nblk);
 	msm_lpm_init_rpm_ctl();
diff --git a/arch/arm/mach-msm/lpm_resources.h b/arch/arm/mach-msm/lpm_resources.h
index 1a2d72d..105cfe6 100644
--- a/arch/arm/mach-msm/lpm_resources.h
+++ b/arch/arm/mach-msm/lpm_resources.h
@@ -17,15 +17,15 @@
 #include "test-lpm.h"
 
 enum {
-	MSM_LPM_PXO_OFF = 0,
-	MSM_LPM_PXO_ON = 1,
+	MSM_LPM_PXO_OFF,
+	MSM_LPM_PXO_ON
 };
 
 enum {
-	MSM_LPM_L2_CACHE_HSFS_OPEN = 0,
-	MSM_LPM_L2_CACHE_GDHS = 1,
-	MSM_LPM_L2_CACHE_RETENTION = 2,
-	MSM_LPM_L2_CACHE_ACTIVE = 3,
+	MSM_LPM_L2_CACHE_HSFS_OPEN,
+	MSM_LPM_L2_CACHE_GDHS,
+	MSM_LPM_L2_CACHE_RETENTION,
+	MSM_LPM_L2_CACHE_ACTIVE,
 };
 
 struct msm_rpmrs_limits {
@@ -99,6 +99,24 @@
 uint32_t msm_pm_get_vdd_dig(struct msm_rpmrs_limits *limits);
 
 /**
+ * msm_lpm_get_xo_value() - get the enum value for xo
+ * @node		pointer to the device node
+ * @key			pxo property key
+ * @xo_val		xo enum value
+ */
+int msm_lpm_get_xo_value(struct device_node *node,
+			char *key, uint32_t *xo_val);
+
+/**
+ * msm_lpm_get_l2_cache_value() - get the enum value for l2 cache
+ * @node                pointer to the device node
+ * @key                 l2 cache property key
+ * @l2_val              l2 mode enum value
+ */
+int msm_lpm_get_l2_cache_value(struct device_node *node,
+				char *key, uint32_t *l2_val);
+
+/**
  * struct msm_lpm_sleep_data - abstraction to get sleep data
  * @limits:	pointer to the msm_rpmrs_limits structure
  * @kernel_sleep:	kernel sleep time as decided by the power calculation