Merge "platform: msm8994: Call scm init during plat early init"
diff --git a/AndroidBoot.mk b/AndroidBoot.mk
index 0cb6128..089c4d8 100644
--- a/AndroidBoot.mk
+++ b/AndroidBoot.mk
@@ -3,7 +3,7 @@
 ifndef 2ND_TARGET_GCC_VERSION
 CROSS_COMPILE := ../../../prebuilts/gcc/linux-x86/arm/arm-eabi-$(TARGET_GCC_VERSION)/bin/arm-eabi-
 else
-CROSS_COMPILE := ../../../prebuilts/gcc/linux-x86/arm/arm-eabi-4.7/bin/arm-eabi-
+CROSS_COMPILE := ../../../prebuilts/gcc/linux-x86/arm/arm-eabi-$(2ND_TARGET_GCC_VERSION)/bin/arm-eabi-
 endif
 
 # Set flags if we need to include security libs
diff --git a/app/aboot/rules.mk b/app/aboot/rules.mk
index a26937b..5342d7a 100644
--- a/app/aboot/rules.mk
+++ b/app/aboot/rules.mk
@@ -2,6 +2,8 @@
 
 INCLUDES += -I$(LK_TOP_DIR)/platform/msm_shared/include
 
+DEFINES += ASSERT_ON_TAMPER=1
+
 OBJS += \
 	$(LOCAL_DIR)/aboot.o \
 	$(LOCAL_DIR)/fastboot.o \
diff --git a/dev/pmic/pm8x41/include/pm8x41.h b/dev/pmic/pm8x41/include/pm8x41.h
index 472e69f..8681feb 100644
--- a/dev/pmic/pm8x41/include/pm8x41.h
+++ b/dev/pmic/pm8x41/include/pm8x41.h
@@ -171,6 +171,12 @@
 	MPP_ENABLE,
 };
 
+enum mvs_en_ctl
+{
+	MVS_DISABLE,
+	MVS_ENABLE,
+};
+
 enum mpp_mode
 {
 	MPP_DIGITAL_INPUT,
@@ -190,9 +196,15 @@
 	enum mpp_mode_en_source_select mode;
 };
 
+struct pm8x41_mvs
+{
+	uint32_t                       base;
+};
+
 #define PM8x41_MMP2_BASE                      0xA100
 #define PM8x41_MMP3_BASE                      0xA200
 #define PM8x41_MMP4_BASE                      0xA300
+#define PM8x41_MVS1_BASE                      0x18400
 
 void pm8x41_lpg_write(uint8_t chan, uint8_t off, uint8_t val);
 int pm8x41_gpio_get(uint8_t gpio, uint8_t *status);
@@ -212,6 +224,7 @@
 uint32_t pm8x41_get_pwrkey_is_pressed();
 void pm8x41_config_output_mpp(struct pm8x41_mpp *mpp);
 void pm8x41_enable_mpp(struct pm8x41_mpp *mpp, enum mpp_en_ctl enable);
+void pm8x41_enable_mvs(struct pm8x41_mvs *mvs, enum mvs_en_ctl enable);
 uint8_t pm8x41_get_is_cold_boot();
 void pm8x41_diff_clock_ctrl(uint8_t enable);
 void pm8x41_clear_pmic_watchdog(void);
diff --git a/dev/pmic/pm8x41/include/pm8x41_hw.h b/dev/pmic/pm8x41/include/pm8x41_hw.h
index 9d05219..db4473c 100644
--- a/dev/pmic/pm8x41/include/pm8x41_hw.h
+++ b/dev/pmic/pm8x41/include/pm8x41_hw.h
@@ -99,6 +99,10 @@
 #define MPP_MODE_CTL_MODE_SHIFT               4
 #define MPP_EN_CTL_ENABLE_SHIFT               7
 
+/* MVS registers */
+#define MVS_EN_CTL                            0x46
+#define MVS_EN_CTL_ENABLE_SHIFT               7
+
 void pm8x41_reg_write(uint32_t addr, uint8_t val);
 uint8_t pm8x41_reg_read(uint32_t addr);
 
diff --git a/dev/pmic/pm8x41/pm8x41.c b/dev/pmic/pm8x41/pm8x41.c
index 3b9783d..2471d0a 100644
--- a/dev/pmic/pm8x41/pm8x41.c
+++ b/dev/pmic/pm8x41/pm8x41.c
@@ -397,6 +397,13 @@
 	return REG_READ(PON_POFF_REASON2);
 }
 
+void pm8x41_enable_mvs(struct pm8x41_mvs *mvs, enum mvs_en_ctl enable)
+{
+	ASSERT(mvs);
+
+	REG_WRITE(mvs->base + MVS_EN_CTL, enable << MVS_EN_CTL_ENABLE_SHIFT);
+}
+
 void pm8x41_enable_mpp(struct pm8x41_mpp *mpp, enum mpp_en_ctl enable)
 {
 	ASSERT(mpp);
diff --git a/lib/heap/heap.c b/lib/heap/heap.c
index a3a35fb..a4835f9 100644
--- a/lib/heap/heap.c
+++ b/lib/heap/heap.c
@@ -237,6 +237,11 @@
 	if (alignment & (alignment - 1))
 		return NULL;
 
+	if(size > (size + sizeof(struct alloc_struct_begin)))
+	{
+		dprintf(CRITICAL, "invalid input size\n");
+		return NULL;
+	}
 	// we always put a size field + base pointer + magic in front of the allocation
 	size += sizeof(struct alloc_struct_begin);
 #if DEBUG_HEAP
@@ -258,6 +263,11 @@
 			alignment = 16;
 
 		// add alignment for worst case fit
+		if(size > (size + alignment))
+		{
+			dprintf(CRITICAL, "invalid input alignment\n");
+			return NULL;
+		}
 		size += alignment;
 	}
 
diff --git a/makefile b/makefile
index 40cb9f0..0cf7e7c 100644
--- a/makefile
+++ b/makefile
@@ -63,6 +63,7 @@
 # setup toolchain prefix
 TOOLCHAIN_PREFIX ?= arm-eabi-
 CFLAGS += -fstack-protector-all
+CFLAGS += -fno-strict-overflow
 CPPFLAGS := -fno-exceptions -fno-rtti -fno-threadsafe-statics
 #CPPFLAGS += -Weffc++
 ASMFLAGS := -DASSEMBLY
diff --git a/platform/apq8084/acpuclock.c b/platform/apq8084/acpuclock.c
index 7f903fb..78c7b64 100644
--- a/platform/apq8084/acpuclock.c
+++ b/platform/apq8084/acpuclock.c
@@ -556,6 +556,39 @@
 	}
 }
 
+void hdmi_clk_enable(void)
+{
+	int ret;
+
+	/* Configure hdmi ahb clock */
+	ret = clk_get_set_enable("hdmi_ahb_clk", 0, 1);
+	if(ret) {
+		dprintf(CRITICAL, "failed to set hdmi_ahb_clk ret = %d\n", ret);
+		ASSERT(0);
+	}
+
+	/* Configure hdmi core clock */
+	ret = clk_get_set_enable("hdmi_core_clk", 19200000, 1);
+	if(ret) {
+		dprintf(CRITICAL, "failed to set hdmi_core_clk ret = %d\n", ret);
+		ASSERT(0);
+	}
+
+	/* Configure hdmi pixel clock */
+	ret = clk_get_set_enable("hdmi_extp_clk", 148500000, 1);
+	if(ret) {
+		dprintf(CRITICAL, "failed to set hdmi_extp_clk ret = %d\n", ret);
+		ASSERT(0);
+	}
+}
+
+void hdmi_clk_disable(void)
+{
+	clk_disable(clk_get("hdmi_extp_clk"));
+	clk_disable(clk_get("hdmi_core_clk"));
+	clk_disable(clk_get("hdmi_ahb_clk"));
+}
+
 void edp_clk_enable(void)
 {
 	int ret;
diff --git a/platform/apq8084/apq8084-clock.c b/platform/apq8084/apq8084-clock.c
index 08d6d2a..f01aa7e 100644
--- a/platform/apq8084/apq8084-clock.c
+++ b/platform/apq8084/apq8084-clock.c
@@ -1,4 +1,4 @@
-/* Copyright (c) 2013, The Linux Foundation. All rights reserved.
+/* Copyright (c) 2013-2014, The Linux Foundation. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions are
@@ -47,6 +47,7 @@
 #define gpll0_mm_source_val 5
 #define edppll_270_mm_source_val 4
 #define edppll_350_mm_source_val 4
+#define hdmipll_mm_source_val 3
 
 struct clk_freq_tbl rcg_dummy_freq = F_END;
 
@@ -690,6 +691,69 @@
 	},
 };
 
+static struct branch_clk mdss_hdmi_ahb_clk = {
+	.cbcr_reg = MDSS_HDMI_AHB_CBCR,
+	.has_sibling = 1,
+	.c = {
+		.dbg_name = "mdss_hdmi_ahb_clk",
+		.ops = &clk_ops_branch,
+	},
+};
+
+static struct clk_freq_tbl ftbl_mdss_hdmi_clk[] = {
+        F_MM( 19200000,  cxo,    1, 0, 0),
+        F_END
+};
+
+static struct rcg_clk hdmi_clk_src = {
+        .cmd_reg = HDMI_CMD_RCGR,
+	.cfg_reg = HDMI_CFG_RCGR,
+        .set_rate = clock_lib2_rcg_set_rate_hid,
+        .freq_tbl = ftbl_mdss_hdmi_clk,
+        .current_freq = &rcg_dummy_freq,
+        .c = {
+                .dbg_name = "hdmi_clk_src",
+                .ops = &clk_ops_rcg,
+        },
+};
+
+static struct branch_clk mdss_hdmi_clk = {
+	.cbcr_reg = MDSS_HDMI_CBCR,
+	.has_sibling = 0,
+	.parent = &hdmi_clk_src.c,
+	.c = {
+		.dbg_name = "mdss_hdmi_clk",
+		.ops = &clk_ops_branch,
+	},
+};
+
+static struct clk_freq_tbl ftbl_mdss_extpclk_clk[] = {
+	F_MDSS(148500000, hdmipll, 1, 0, 0),
+	F_END
+};
+
+static struct rcg_clk extpclk_clk_src = {
+	.cmd_reg = EXTPCLK_CMD_RCGR,
+	.cfg_reg = EXTPCLK_CFG_RCGR,
+	.set_rate = clock_lib2_rcg_set_rate_hid,
+	.freq_tbl = ftbl_mdss_extpclk_clk,
+	.current_freq = &rcg_dummy_freq,
+	.c = {
+		.dbg_name = "extpclk_clk_src",
+		.ops = &clk_ops_rcg,
+	},
+};
+
+static struct branch_clk mdss_extpclk_clk = {
+	.cbcr_reg = MDSS_EXTPCLK_CBCR,
+	.has_sibling = 0,
+	.parent = &extpclk_clk_src.c,
+	.c = {
+		.dbg_name = "mdss_extpclk_clk",
+		.ops = &clk_ops_branch,
+	},
+};
+
 static struct clk_freq_tbl ftbl_mdss_edpaux_clk[] = {
 	F_MM(19200000,    cxo,   1,   0,   0),
 	F_END
@@ -838,6 +902,10 @@
 	CLK_LOOKUP("edp_pixel_clk",        mdss_edppixel_clk.c),
 	CLK_LOOKUP("edp_link_clk",         mdss_edplink_clk.c),
 	CLK_LOOKUP("edp_aux_clk",          mdss_edpaux_clk.c),
+
+        CLK_LOOKUP("hdmi_ahb_clk",         mdss_hdmi_ahb_clk.c),
+        CLK_LOOKUP("hdmi_core_clk",        mdss_hdmi_clk.c),
+        CLK_LOOKUP("hdmi_extp_clk",        mdss_extpclk_clk.c),
 };
 
 void platform_clock_init(void)
diff --git a/platform/apq8084/include/platform/clock.h b/platform/apq8084/include/platform/clock.h
index 7f4ebfd..a3bb284 100644
--- a/platform/apq8084/include/platform/clock.h
+++ b/platform/apq8084/include/platform/clock.h
@@ -1,4 +1,4 @@
-/* Copyright (c) 2013, The Linux Foundation. All rights reserved.
+/* Copyright (c) 2013-2014, The Linux Foundation. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions are
@@ -50,6 +50,13 @@
 #define MDP_CBCR                        REG_MM(0x231C)
 #define MDP_LUT_CBCR                    REG_MM(0x2320)
 #define MDP_AHB_CBCR                    REG_MM(0x2308)
+#define MDSS_HDMI_AHB_CBCR              REG_MM(0x230C)
+#define MDSS_HDMI_CBCR                  REG_MM(0x2338)
+#define MDSS_EXTPCLK_CBCR               REG_MM(0x2324)
+#define EXTPCLK_CMD_RCGR                REG_MM(0x2060)
+#define EXTPCLK_CFG_RCGR                REG_MM(0x2064)
+#define HDMI_CMD_RCGR                   REG_MM(0x2100)
+#define HDMI_CFG_RCGR                   REG_MM(0x2104)
 
 #define MDP_AXI_CMD_RCGR                REG_MM(0x5040)
 #define MDP_AXI_CFG_RCGR                REG_MM(0x5044)
diff --git a/platform/msm8916/platform.c b/platform/msm8916/platform.c
index cd428e8..4deaa1b 100644
--- a/platform/msm8916/platform.c
+++ b/platform/msm8916/platform.c
@@ -190,6 +190,7 @@
 		case MSM8236:
 		case MSM8636:
 		case MSM8936:
+		case MSM8239:
 			ret = 1;
 			break;
 		default:
diff --git a/platform/msm8994/rules.mk b/platform/msm8994/rules.mk
index a9fd66f..641e3cd 100644
--- a/platform/msm8994/rules.mk
+++ b/platform/msm8994/rules.mk
@@ -13,6 +13,8 @@
 DEFINES += WITH_CPU_EARLY_INIT=0 WITH_CPU_WARM_BOOT=0 \
 	   MMC_SLOT=$(MMC_SLOT)
 
+DEFINES += TZ_SAVE_KERNEL_HASH
+
 INCLUDES += -I$(LOCAL_DIR)/include -I$(LK_TOP_DIR)/platform/msm_shared/include
 
 DEVS += fbcon
diff --git a/platform/msm_shared/board.c b/platform/msm_shared/board.c
index 5c7dee8..fdc8553 100644
--- a/platform/msm_shared/board.c
+++ b/platform/msm_shared/board.c
@@ -50,6 +50,7 @@
 	unsigned int board_info_len = 0;
 	unsigned ret = 0;
 	unsigned format = 0;
+	unsigned pmic_type = 0;
 	uint8_t i;
 	uint16_t format_major = 0;
 	uint16_t format_minor = 0;
@@ -131,6 +132,17 @@
 			for (i = 0; i < SMEM_V8_SMEM_MAX_PMIC_DEVICES; i++) {
 				board.pmic_info[i].pmic_type = board_info_v8.pmic_info[i].pmic_type;
 				board.pmic_info[i].pmic_version = board_info_v8.pmic_info[i].pmic_version;
+
+				/*
+				 * fill in pimc_board_info with pmic type and pmic version information
+				 * bit no  		  	    |31  24   | 23  16 	    | 15   8 	     |7		  0|
+				 * pimc_board_info = |Unused | Major version | Minor version|PMIC_MODEL|
+				 *
+				 */
+				pmic_type = board_info_v8.pmic_info[i].pmic_type == PMIC_IS_INVALID? 0 : board_info_v8.pmic_info[i].pmic_type;
+
+				board.pmic_info[i].pmic_target = (((board_info_v8.pmic_info[i].pmic_version >> 16) & 0xff) << 16) |
+					   ((board_info_v8.pmic_info[i].pmic_version & 0xff) << 8) | (pmic_type & 0xff);
 			}
 
 			if (format_minor == 0x9)
@@ -195,12 +207,21 @@
 	for (i = 0; i < num_ent && i < SMEM_MAX_PMIC_DEVICES; i++) {
 		info->pmic_type = board.pmic_info[i].pmic_type;
 		info->pmic_version = board.pmic_info[i].pmic_version;
+		info->pmic_target = board.pmic_info[i].pmic_target;
 		info++;
 	}
 
 	return (i--);
 }
 
+uint32_t board_pmic_target(uint8_t num_ent)
+{
+	if (num_ent < SMEM_MAX_PMIC_DEVICES) {
+		return board.pmic_info[num_ent].pmic_target;
+	}
+	return 0;
+}
+
 uint32_t board_soc_version()
 {
 	return board.platform_version;
diff --git a/platform/msm_shared/dev_tree.c b/platform/msm_shared/dev_tree.c
index 774c8b6..bd24628 100644
--- a/platform/msm_shared/dev_tree.c
+++ b/platform/msm_shared/dev_tree.c
@@ -35,6 +35,8 @@
 #include <string.h>
 #include <platform.h>
 #include <board.h>
+#include <list.h>
+#include <kernel/thread.h>
 
 struct dt_entry_v1
 {
@@ -46,8 +48,9 @@
 };
 
 static struct dt_mem_node_info mem_node;
-
-static int platform_dt_match(struct dt_entry *cur_dt_entry, struct board_dt_entry *board_dt_data, uint32_t subtype_mask);
+static int platform_dt_absolute_match(struct dt_entry *cur_dt_entry, struct dt_entry_node *dt_list);
+static struct dt_entry *platform_dt_match_best(struct dt_entry_node *dt_list);
+static int update_dtb_entry_node(struct dt_entry_node *dt_list, uint32_t dtb_info);
 extern int target_is_emmc_boot(void);
 extern uint32_t target_dev_tree_mem(void *fdt, uint32_t memory_node_offset);
 /* TODO: This function needs to be moved to target layer to check violations
@@ -58,30 +61,66 @@
 /* Returns soc version if platform id and hardware id matches
    otherwise return 0xFFFFFFFF */
 #define INVALID_SOC_REV_ID 0XFFFFFFFF
-static uint32_t dev_tree_compatible(void *dtb)
+
+/* Add function to allocate dt entry list, used for recording
+*  the entry which conform to platform_dt_absolute_match()
+*/
+static struct dt_entry_node *dt_entry_list_init(void)
+{
+	struct dt_entry_node *dt_node_member = NULL;
+
+	dt_node_member = (struct dt_entry_node *)
+		malloc(sizeof(struct dt_entry_node));
+
+	ASSERT(dt_node_member);
+
+	list_clear_node(&dt_node_member->node);
+	dt_node_member->dt_entry_m = (struct dt_entry *)
+			malloc(sizeof(struct dt_entry));
+	ASSERT(dt_node_member->dt_entry_m);
+
+	memset(dt_node_member->dt_entry_m ,0 ,sizeof(struct dt_entry));
+	return dt_node_member;
+}
+
+static void insert_dt_entry_in_queue(struct dt_entry_node *dt_list, struct dt_entry_node *dt_node_member)
+{
+	list_add_tail(&dt_list->node, &dt_node_member->node);
+}
+
+static void dt_entry_list_delete(struct dt_entry_node *dt_node_member)
+{
+	if (list_in_list(&dt_node_member->node)) {
+			list_delete(&dt_node_member->node);
+			free(dt_node_member->dt_entry_m);
+			free(dt_node_member);
+	}
+}
+
+static int dev_tree_compatible(void *dtb, uint32_t dtb_size, struct dt_entry_node *dtb_list)
 {
 	int root_offset;
 	const void *prop = NULL;
 	const char *plat_prop = NULL;
 	const char *board_prop = NULL;
+	const char *pmic_prop = NULL;
 	char *model = NULL;
-	struct dt_entry cur_dt_entry;
-	struct dt_entry *dt_entry_v2 = NULL;
+	struct dt_entry *cur_dt_entry;
+	struct dt_entry *dt_entry_array = NULL;
 	struct board_id *board_data = NULL;
 	struct plat_id *platform_data = NULL;
+	struct pmic_id *pmic_data = NULL;
 	int len;
 	int len_board_id;
 	int len_plat_id;
 	int min_plat_id_len = 0;
-	uint32_t target_variant_id;
+	int len_pmic_id;
 	uint32_t dtb_ver;
 	uint32_t num_entries = 0;
-	uint32_t i, j, k;
-	uint32_t found = 0;
+	uint32_t i, j, k, n;
 	uint32_t msm_data_count;
 	uint32_t board_data_count;
-	uint32_t soc_rev;
-	struct board_dt_entry board_dt_data;
+	uint32_t pmic_data_count;
 
 	root_offset = fdt_path_offset(dtb, "/");
 	if (root_offset < 0)
@@ -95,12 +134,21 @@
 	} else {
 		dprintf(INFO, "model does not exist in device tree\n");
 	}
-
-	/* Find the board-id prop from DTB , if board-id is present then
-	 * the DTB is version 2 */
+	/* Find the pmic-id prop from DTB , if pmic-id is present then
+	* the DTB is version 3, otherwise find the board-id prop from DTB ,
+	* if board-id is present then the DTB is version 2 */
+	pmic_prop = (const char *)fdt_getprop(dtb, root_offset, "qcom,pmic-id", &len_pmic_id);
 	board_prop = (const char *)fdt_getprop(dtb, root_offset, "qcom,board-id", &len_board_id);
-	if (board_prop && len_board_id > 0)
-	{
+	if (pmic_prop && (len_pmic_id > 0) && board_prop && (len_board_id > 0)) {
+		if ((len_pmic_id % PMIC_ID_SIZE) || (len_board_id % BOARD_ID_SIZE))
+		{
+			dprintf(CRITICAL, "qcom,pmic-id(%d) or qcom,board-id(%d) in device tree is not a multiple of (%d %d)\n",
+				len_pmic_id, len_board_id, PMIC_ID_SIZE, BOARD_ID_SIZE);
+			return false;
+		}
+		dtb_ver = DEV_TREE_VERSION_V3;
+		min_plat_id_len = PLAT_ID_SIZE;
+	} else if (board_prop && len_board_id > 0) {
 		if (len_board_id % BOARD_ID_SIZE)
 		{
 			dprintf(CRITICAL, "qcom,board-id in device tree is (%d) not a multiple of (%d)\n",
@@ -109,9 +157,7 @@
 		}
 		dtb_ver = DEV_TREE_VERSION_V2;
 		min_plat_id_len = PLAT_ID_SIZE;
-	}
-	else
-	{
+	} else {
 		dtb_ver = DEV_TREE_VERSION_V1;
 		min_plat_id_len = DT_ENTRY_V1_SIZE;
 	}
@@ -133,197 +179,226 @@
 	 * y: variant_id
 	 * z: SOC rev
 	 */
-	if (dtb_ver == DEV_TREE_VERSION_V1)
-	{
-		while (len_plat_id)
-		{
-			cur_dt_entry.platform_id = fdt32_to_cpu(((const struct dt_entry_v1 *)plat_prop)->platform_id);
-			cur_dt_entry.variant_id = fdt32_to_cpu(((const struct dt_entry_v1 *)plat_prop)->variant_id);
-			cur_dt_entry.soc_rev = fdt32_to_cpu(((const struct dt_entry_v1 *)plat_prop)->soc_rev);
-			cur_dt_entry.board_hw_subtype = board_hardware_subtype();
+	if (dtb_ver == DEV_TREE_VERSION_V1) {
+		cur_dt_entry = (struct dt_entry *)
+				malloc(sizeof(struct dt_entry));
 
-			board_dt_data.target_variant_id = board_hardware_id();
-			board_dt_data.platform_variant_id = board_platform_id();
+		if (!cur_dt_entry) {
+			dprintf(CRITICAL, "Out of memory\n");
+			return false;
+		}
+		memset(cur_dt_entry, 0, sizeof(struct dt_entry));
+
+		while (len_plat_id) {
+			cur_dt_entry->platform_id = fdt32_to_cpu(((const struct dt_entry_v1 *)plat_prop)->platform_id);
+			cur_dt_entry->variant_id = fdt32_to_cpu(((const struct dt_entry_v1 *)plat_prop)->variant_id);
+			cur_dt_entry->soc_rev = fdt32_to_cpu(((const struct dt_entry_v1 *)plat_prop)->soc_rev);
+			cur_dt_entry->board_hw_subtype =
+				fdt32_to_cpu(((const struct dt_entry_v1 *)plat_prop)->variant_id) >> 0x18;
+			cur_dt_entry->pmic_rev[0] = board_pmic_target(0);
+			cur_dt_entry->pmic_rev[1] = board_pmic_target(1);
+			cur_dt_entry->pmic_rev[2] = board_pmic_target(2);
+			cur_dt_entry->pmic_rev[3] = board_pmic_target(3);
+			cur_dt_entry->offset = (uint32_t)dtb;
+			cur_dt_entry->size = dtb_size;
 
 			dprintf(SPEW, "Found an appended flattened device tree (%s - %u %u 0x%x)\n",
-				model ? model : "unknown",
-				cur_dt_entry.platform_id, cur_dt_entry.variant_id, cur_dt_entry.soc_rev);
+				*model ? model : "unknown",
+				cur_dt_entry->platform_id, cur_dt_entry->variant_id, cur_dt_entry->soc_rev);
 
-			if (platform_dt_match(&cur_dt_entry, &board_dt_data, 0) == 1)
-			{
+			if (platform_dt_absolute_match(cur_dt_entry, dtb_list)) {
+				dprintf(SPEW, "Device tree exact match the board: <%u %u 0x%x> != <%u %u 0x%x>\n",
+					cur_dt_entry->platform_id,
+					cur_dt_entry->variant_id,
+					cur_dt_entry->soc_rev,
+					board_platform_id(),
+					board_hardware_id(),
+					board_soc_version());
+
+			} else {
 				dprintf(SPEW, "Device tree's msm_id doesn't match the board: <%u %u 0x%x> != <%u %u 0x%x>\n",
-							  cur_dt_entry.platform_id,
-							  cur_dt_entry.variant_id,
-							  cur_dt_entry.soc_rev,
-							  board_platform_id(),
-							  board_hardware_id(),
-							  board_soc_version());
+					cur_dt_entry->platform_id,
+					cur_dt_entry->variant_id,
+					cur_dt_entry->soc_rev,
+					board_platform_id(),
+					board_hardware_id(),
+					board_soc_version());
 				plat_prop += DT_ENTRY_V1_SIZE;
 				len_plat_id -= DT_ENTRY_V1_SIZE;
 				continue;
 			}
-			else
-			{
-				found = 1;
-				break;
-			}
 		}
+		free(cur_dt_entry);
+
 	}
 	/*
-	 * If DTB Version is '2' then we have split DTB with board & msm data
-	 * populated saperately in board-id & msm-id prop respectively.
+	 * If DTB Version is '3' then we have split DTB with board & msm data & pmic
+	 * populated saperately in board-id & msm-id & pmic-id prop respectively.
 	 * Extract the data & prepare a look up table
 	 */
-	else if (dtb_ver == DEV_TREE_VERSION_V2)
-	{
+	else if (dtb_ver == DEV_TREE_VERSION_V2 || dtb_ver == DEV_TREE_VERSION_V3) {
 		board_data_count = (len_board_id / BOARD_ID_SIZE);
 		msm_data_count = (len_plat_id / PLAT_ID_SIZE);
+		/* If dtb version is v2.0, the pmic_data_count will be <= 0 */
+		pmic_data_count = (len_pmic_id / PMIC_ID_SIZE);
 
-		/* If we are using dtb v2.0, then we have split board & msm data in the DTB */
+		/* If we are using dtb v3.0, then we have split board, msm & pmic data in the DTB
+		*  If we are using dtb v2.0, then we have split board & msmdata in the DTB
+		*/
 		board_data = (struct board_id *) malloc(sizeof(struct board_id) * (len_board_id / BOARD_ID_SIZE));
 		ASSERT(board_data);
 		platform_data = (struct plat_id *) malloc(sizeof(struct plat_id) * (len_plat_id / PLAT_ID_SIZE));
 		ASSERT(platform_data);
+		if (dtb_ver == DEV_TREE_VERSION_V3) {
+			pmic_data = (struct pmic_id *) malloc(sizeof(struct pmic_id) * (len_pmic_id / PMIC_ID_SIZE));
+			ASSERT(pmic_data);
+		}
 		i = 0;
 
 		/* Extract board data from DTB */
-		for(i = 0 ; i < board_data_count; i++)
-		{
+		for(i = 0 ; i < board_data_count; i++) {
 			board_data[i].variant_id = fdt32_to_cpu(((struct board_id *)board_prop)->variant_id);
 			board_data[i].platform_subtype = fdt32_to_cpu(((struct board_id *)board_prop)->platform_subtype);
+			/* For V2/V3 version of DTBs we have platform version field as part
+			 * of variant ID, in such case the subtype will be mentioned as 0x0
+			 * As the qcom, board-id = <0xSSPMPmPH, 0x0>
+			 * SS -- Subtype
+			 * PM -- Platform major version
+			 * Pm -- Platform minor version
+			 * PH -- Platform hardware CDP/MTP
+			 * In such case to make it compatible with LK algorithm move the subtype
+			 * from variant_id to subtype field
+			 */
+			if (board_data[i].platform_subtype == 0)
+				board_data[i].platform_subtype =
+					fdt32_to_cpu(((struct board_id *)board_prop)->variant_id) >> 0x18;
+
 			len_board_id -= sizeof(struct board_id);
 			board_prop += sizeof(struct board_id);
 		}
 
 		/* Extract platform data from DTB */
-		for(i = 0 ; i < msm_data_count; i++)
-		{
+		for(i = 0 ; i < msm_data_count; i++) {
 			platform_data[i].platform_id = fdt32_to_cpu(((struct plat_id *)plat_prop)->platform_id);
 			platform_data[i].soc_rev = fdt32_to_cpu(((struct plat_id *)plat_prop)->soc_rev);
 			len_plat_id -= sizeof(struct plat_id);
 			plat_prop += sizeof(struct plat_id);
 		}
 
-		/* We need to merge board & platform data into dt entry structure */
-		num_entries = msm_data_count * board_data_count;
+		if (dtb_ver == DEV_TREE_VERSION_V3 && pmic_prop) {
+			/* Extract pmic data from DTB */
+			for(i = 0 ; i < pmic_data_count; i++) {
+				pmic_data[i].pmic_version[0]= fdt32_to_cpu(((struct pmic_id *)pmic_prop)->pmic_version[0]);
+				pmic_data[i].pmic_version[1]= fdt32_to_cpu(((struct pmic_id *)pmic_prop)->pmic_version[1]);
+				pmic_data[i].pmic_version[2]= fdt32_to_cpu(((struct pmic_id *)pmic_prop)->pmic_version[2]);
+				pmic_data[i].pmic_version[3]= fdt32_to_cpu(((struct pmic_id *)pmic_prop)->pmic_version[3]);
+				len_pmic_id -= sizeof(struct pmic_id);
+				pmic_prop += sizeof(struct pmic_id);
+			}
 
-		if (((uint64_t)msm_data_count * (uint64_t)board_data_count) == msm_data_count * board_data_count) {
-			dt_entry_v2 = (struct dt_entry*) malloc(sizeof(struct dt_entry) * num_entries);
-			ASSERT(dt_entry_v2);
+			/* We need to merge board & platform data into dt entry structure */
+			num_entries = msm_data_count * board_data_count * pmic_data_count;
+		} else {
+			/* We need to merge board & platform data into dt entry structure */
+			num_entries = msm_data_count * board_data_count;
+		}
 
-			/* If we have '<X>; <Y>; <Z>' as platform data & '<A>; <B>; <C>' as board data.
-			 * Then dt entry should look like
-			 * <X ,A >;<X, B>;<X, C>;
-			 * <Y ,A >;<Y, B>;<Y, C>;
-			 * <Z ,A >;<Z, B>;<Z, C>;
-			 */
-			i = 0;
-			k = 0;
-			for (i = 0; i < msm_data_count; i++)
-			{
-				for (j = 0; j < board_data_count; j++)
-				{
-					dt_entry_v2[k].platform_id = platform_data[i].platform_id;
-					dt_entry_v2[k].soc_rev = platform_data[i].soc_rev;
-					dt_entry_v2[k].variant_id = board_data[j].variant_id;
-					dt_entry_v2[k].board_hw_subtype = board_data[j].platform_subtype;
+		if ((((uint64_t)msm_data_count * (uint64_t)board_data_count * (uint64_t)pmic_data_count) !=
+			msm_data_count * board_data_count * pmic_data_count) ||
+			(((uint64_t)msm_data_count * (uint64_t)board_data_count) != msm_data_count * board_data_count)) {
+
+			free(board_data);
+			free(platform_data);
+			if (pmic_data)
+				free(pmic_data);
+			if (model)
+				free(model);
+			return false;
+		}
+
+		dt_entry_array = (struct dt_entry*) malloc(sizeof(struct dt_entry) * num_entries);
+		ASSERT(dt_entry_array);
+
+		/* If we have '<X>; <Y>; <Z>' as platform data & '<A>; <B>; <C>' as board data.
+		 * Then dt entry should look like
+		 * <X ,A >;<X, B>;<X, C>;
+		 * <Y ,A >;<Y, B>;<Y, C>;
+		 * <Z ,A >;<Z, B>;<Z, C>;
+		 */
+		i = 0;
+		k = 0;
+		n = 0;
+		for (i = 0; i < msm_data_count; i++) {
+			for (j = 0; j < board_data_count; j++) {
+				if (dtb_ver == DEV_TREE_VERSION_V3 && pmic_prop) {
+					for (n = 0; n < pmic_data_count; n++) {
+						dt_entry_array[k].platform_id = platform_data[i].platform_id;
+						dt_entry_array[k].soc_rev = platform_data[i].soc_rev;
+						dt_entry_array[k].variant_id = board_data[j].variant_id;
+						dt_entry_array[k].board_hw_subtype = board_data[j].platform_subtype;
+						dt_entry_array[k].pmic_rev[0]= pmic_data[n].pmic_version[0];
+						dt_entry_array[k].pmic_rev[1]= pmic_data[n].pmic_version[1];
+						dt_entry_array[k].pmic_rev[2]= pmic_data[n].pmic_version[2];
+						dt_entry_array[k].pmic_rev[3]= pmic_data[n].pmic_version[3];
+						dt_entry_array[k].offset = (uint32_t)dtb;
+						dt_entry_array[k].size = dtb_size;
+						k++;
+					}
+
+				} else {
+					dt_entry_array[k].platform_id = platform_data[i].platform_id;
+					dt_entry_array[k].soc_rev = platform_data[i].soc_rev;
+					dt_entry_array[k].variant_id = board_data[j].variant_id;
+					dt_entry_array[k].board_hw_subtype = board_data[j].platform_subtype;
+					dt_entry_array[k].pmic_rev[0]= board_pmic_target(0);
+					dt_entry_array[k].pmic_rev[1]= board_pmic_target(1);
+					dt_entry_array[k].pmic_rev[2]= board_pmic_target(2);
+					dt_entry_array[k].pmic_rev[3]= board_pmic_target(3);
+					dt_entry_array[k].offset = (uint32_t)dtb;
+					dt_entry_array[k].size = dtb_size;
 					k++;
 				}
 			}
+		}
 
-			/* Now find the matching entry in the merged list */
-			if (board_hardware_id() == HW_PLATFORM_QRD)
-			{
-				board_dt_data.target_variant_id = board_target_id();
-				board_dt_data.platform_variant_id = board_platform_id();
-			}
-			else
-			{
-				board_dt_data.target_variant_id = board_hardware_id() | ((board_hardware_subtype() & 0xff) << 24);
-				board_dt_data.platform_variant_id = board_platform_id();
-			}
+		for (i=0 ;i < num_entries; i++) {
+			dprintf(SPEW, "Found an appended flattened device tree (%s - %u %u %u 0x%x)\n",
+				*model ? model : "unknown",
+				dt_entry_array[i].platform_id, dt_entry_array[i].variant_id, dt_entry_array[i].board_hw_subtype, dt_entry_array[i].soc_rev);
 
-			for (i=0 ;i < num_entries; i++)
-			{
-				dprintf(SPEW, "Found an appended flattened device tree (%s - %u %u %u 0x%x)\n",
-					*model ? model : "unknown",
-					dt_entry_v2[i].platform_id, dt_entry_v2[i].variant_id, dt_entry_v2[i].board_hw_subtype, dt_entry_v2[i].soc_rev);
+			if (platform_dt_absolute_match(&(dt_entry_array[i]), dtb_list)) {
+				dprintf(SPEW, "Device tree exact match the board: <%u %u %u 0x%x> == <%u %u %u 0x%x>\n",
+					dt_entry_array[i].platform_id,
+					dt_entry_array[i].variant_id,
+					dt_entry_array[i].soc_rev,
+					dt_entry_array[i].board_hw_subtype,
+					board_platform_id(),
+					board_hardware_id(),
+					board_hardware_subtype(),
+					board_soc_version());
 
-				if (platform_dt_match(&dt_entry_v2[i], &board_dt_data, 0xff) == 1)
-				{
-					dprintf(SPEW, "Device tree's msm_id doesn't match the board: <%u %u %u 0x%x> != <%u %u %u 0x%x>\n",
-									  dt_entry_v2[i].platform_id,
-									  dt_entry_v2[i].variant_id,
-									  dt_entry_v2[i].soc_rev,
-									  dt_entry_v2[i].board_hw_subtype,
-									board_platform_id(),
-									board_hardware_id(),
-									board_hardware_subtype(),
-									board_soc_version());
-						continue;
-				}
-				else
-				{
-					/* If found a match, return the cur_dt_entry */
-					found = 1;
-					cur_dt_entry = dt_entry_v2[i];
-					break;
-				}
+			} else {
+				dprintf(SPEW, "Device tree's msm_id doesn't match the board: <%u %u %u 0x%x> != <%u %u %u 0x%x>\n",
+					dt_entry_array[i].platform_id,
+					dt_entry_array[i].variant_id,
+					dt_entry_array[i].soc_rev,
+					dt_entry_array[i].board_hw_subtype,
+					board_platform_id(),
+					board_hardware_id(),
+					board_hardware_subtype(),
+					board_soc_version());
 			}
 		}
-		else
-			dprintf(CRITICAL, "Device tree got corrupted\n");
+
+		free(board_data);
+		free(platform_data);
+		if (pmic_data)
+			free(pmic_data);
+		free(dt_entry_array);
 	}
-
-	if (dtb_ver == DEV_TREE_VERSION_V1)
-	{
-		if (!found)
-			soc_rev =  INVALID_SOC_REV_ID;
-		else
-		{
-			soc_rev = cur_dt_entry.soc_rev;
-
-		dprintf(INFO, "Device tree's msm_id matches the board: <%u %u %u 0x%x> == <%u %u %u 0x%x>\n",
-			cur_dt_entry.platform_id,
-			cur_dt_entry.variant_id,
-			cur_dt_entry.board_hw_subtype,
-			cur_dt_entry.soc_rev,
-			board_platform_id(),
-			board_hardware_id(),
-			board_hardware_subtype(),
-			board_soc_version());
-		}
-		return soc_rev;
-	}
-	else if (dtb_ver == DEV_TREE_VERSION_V2)
-	{
-		if (!found)
-		{
-			soc_rev =  INVALID_SOC_REV_ID;
-			goto end;
-		}
-		else
-			soc_rev = cur_dt_entry.soc_rev;
-
-		dprintf(INFO, "Device tree's msm_id matches the board: <%u %u %u 0x%x> == <%u %u %u 0x%x>\n",
-			cur_dt_entry.platform_id,
-			cur_dt_entry.variant_id,
-			cur_dt_entry.board_hw_subtype,
-			cur_dt_entry.soc_rev,
-			board_platform_id(),
-			board_hardware_id(),
-			board_hardware_subtype(),
-			board_soc_version());
-	}
-
-end:
-	free(board_data);
-	free(platform_data);
-	free(dt_entry_v2);
-	if(model) {
+	if (model)
 		free(model);
-	}
-	return soc_rev;
+	return true;
 }
 
 /*
@@ -341,10 +416,25 @@
 {
 	void *kernel_end = kernel + kernel_size;
 	uint32_t app_dtb_offset = 0;
-	void *dtb;
+	void *dtb = NULL;
 	void *bestmatch_tag = NULL;
+	struct dt_entry *best_match_dt_entry = NULL;
 	uint32_t bestmatch_tag_size;
-	uint32_t bestmatch_soc_rev_id = INVALID_SOC_REV_ID;
+	struct dt_entry_node *dt_entry_queue = NULL;
+	struct dt_entry_node *dt_node_tmp1 = NULL;
+	struct dt_entry_node *dt_node_tmp2 = NULL;
+
+
+	/* Initialize the dtb entry node*/
+	dt_entry_queue = (struct dt_entry_node *)
+				malloc(sizeof(struct dt_entry_node));
+
+	if (!dt_entry_queue) {
+		dprintf(CRITICAL, "Out of memory\n");
+		return NULL;
+	}
+	list_initialize(&dt_entry_queue->node);
+
 
 	memcpy((void*) &app_dtb_offset, (void*) (kernel + DTB_OFFSET), sizeof(uint32_t));
 
@@ -353,7 +443,6 @@
 	}
 	dtb = kernel + app_dtb_offset;
 	while (((uintptr_t)dtb + sizeof(struct fdt_header)) < (uintptr_t)kernel_end) {
-		uint32_t dtb_soc_rev_id;
 		struct fdt_header dtb_hdr;
 		uint32_t dtb_size;
 
@@ -371,34 +460,36 @@
 			return NULL;
 		}
 
-		/* now that we know we have a valid DTB, we need to copy
-		 * it somewhere aligned, like tags */
-		memcpy(tags, dtb, dtb_size);
-
-		dtb_soc_rev_id = dev_tree_compatible(tags);
-		if (dtb_soc_rev_id == board_soc_version()) {
-			/* clear out the old DTB magic so kernel doesn't find it */
-			*((uint32_t *)(kernel + app_dtb_offset)) = 0;
-			return tags;
-		} else if ((dtb_soc_rev_id != INVALID_SOC_REV_ID) &&
-				   (dtb_soc_rev_id < board_soc_version())) {
-			/* if current bestmatch is less than new dtb_soc_rev_id then update
-			   bestmatch_tag */
-			if((bestmatch_soc_rev_id == INVALID_SOC_REV_ID) ||
-			   (bestmatch_soc_rev_id < dtb_soc_rev_id)) {
-				bestmatch_tag = dtb;
-				bestmatch_tag_size = dtb_size;
-				bestmatch_soc_rev_id = dtb_soc_rev_id;
-			}
-		}
+		dev_tree_compatible(dtb, dtb_size, dt_entry_queue);
 
 		/* goto the next device tree if any */
 		dtb += dtb_size;
 	}
 
+	best_match_dt_entry = platform_dt_match_best(dt_entry_queue);
+	if (best_match_dt_entry){
+		bestmatch_tag = (void *)best_match_dt_entry->offset;
+		bestmatch_tag_size = best_match_dt_entry->size;
+		dprintf(INFO, "Best match DTB tags %u/%08x/0x%08x/%x/%x/%x/%x/%x/%x/%x\n",
+			best_match_dt_entry->platform_id, best_match_dt_entry->variant_id,
+			best_match_dt_entry->board_hw_subtype, best_match_dt_entry->soc_rev,
+			best_match_dt_entry->pmic_rev[0], best_match_dt_entry->pmic_rev[1],
+			best_match_dt_entry->pmic_rev[2], best_match_dt_entry->pmic_rev[3],
+			best_match_dt_entry->offset, best_match_dt_entry->size);
+		dprintf(INFO, "Using pmic info 0x%0x/0x%x/0x%x/0x%0x for device 0x%0x/0x%x/0x%x/0x%0x\n",
+			best_match_dt_entry->pmic_rev[0], best_match_dt_entry->pmic_rev[1],
+			best_match_dt_entry->pmic_rev[2], best_match_dt_entry->pmic_rev[3],
+			board_pmic_target(0), board_pmic_target(1),
+			board_pmic_target(2), board_pmic_target(3));
+	}
+	/* free queue's memory */
+	list_for_every_entry(&dt_entry_queue->node, dt_node_tmp1, dt_node, node) {
+		dt_node_tmp2 = dt_node_tmp1->node.prev;
+		dt_entry_list_delete(dt_node_tmp1);
+		dt_node_tmp1 = dt_node_tmp2;
+	}
+
 	if(bestmatch_tag) {
-		dprintf(INFO,"DTB found with bestmatch soc rev id 0x%x.Board soc rev id 0x%x\n",
-				bestmatch_soc_rev_id, board_soc_version());
 		memcpy(tags, bestmatch_tag, bestmatch_tag_size);
 		/* clear out the old DTB magic so kernel doesn't find it */
 		*((uint32_t *)(kernel + app_dtb_offset)) = 0;
@@ -425,6 +516,8 @@
 	if (table->version == DEV_TREE_VERSION_V1) {
 		dt_entry_size = sizeof(struct dt_entry_v1);
 	} else if (table->version == DEV_TREE_VERSION_V2) {
+		dt_entry_size = sizeof(struct dt_entry_v2);
+	} else if (table->version == DEV_TREE_VERSION_V3) {
 		dt_entry_size = sizeof(struct dt_entry);
 	} else {
 		dprintf(CRITICAL, "ERROR: Unsupported version (%d) in DT table \n",
@@ -445,64 +538,350 @@
 	return 0;
 }
 
-static int platform_dt_match(struct dt_entry *cur_dt_entry, struct board_dt_entry *board_dt_data, uint32_t subtype_mask)
+static int platform_dt_absolute_match(struct dt_entry *cur_dt_entry, struct dt_entry_node *dt_list)
 {
-	/*
-	 * 1. Check if cur_dt_entry has platform_hw_version major & minor present?
-	 * 2. If present, calculate cur_dt_target_id for the current platform as:
-	 * 3. bit no |31  24 | 23   16| 15   8 |7         0|
-	 * 4.        |subtype| major  | minor  |hw_platform|
-	 */
-	uint32_t cur_dt_target_id ;
 	uint32_t cur_dt_hlos_subtype;
+	uint32_t cur_dt_hw_platform;
+	uint32_t cur_dt_hw_subtype;
+	uint32_t cur_dt_msm_id;
+	dt_node *dt_node_tmp = NULL;
 
-	/*
-	 * if variant_id has platform_hw_ver has major = 0xff and minor = 0xff,
-	 * ignore the major & minor versions from the DTB entry
-	 */
-	if ((cur_dt_entry->variant_id & 0xffff00) == 0xffff00)
-		cur_dt_target_id  = (cur_dt_entry->variant_id & 0xff0000ff) | (board_dt_data->target_variant_id & 0xffff00);
-	/*
-	 * We have a valid platform_hw_version major & minor numbers in the board-id, so
-	 * use the board-id from the DTB.
-	 * Note: For some QRD platforms the format used is qcom, board-id = <0xMVmVPT 0xPS>
-	 * where: MV: platform major ver, mV: platform minor ver, PT: platform type
-	 * PS: platform subtype, so we need to put PS @ bit 24-31 to be backward compatible.
-	 */
-	else
-		cur_dt_target_id = cur_dt_entry->variant_id | ((cur_dt_entry->board_hw_subtype & subtype_mask & 0xff) << 24);
+	/* Platform-id
+	* bit no |31	 24|23	16|15	0|
+	*        |reserved|foundry-id|msm-id|
+	*/
+	cur_dt_msm_id = (cur_dt_entry->platform_id & 0x0000ffff);
+	cur_dt_hw_platform = (cur_dt_entry->variant_id & 0x000000ff);
+	cur_dt_hw_subtype = (cur_dt_entry->board_hw_subtype);
+
+
 	/* Determine the bits 23:8 to check the DT with the DDR Size */
 	cur_dt_hlos_subtype = (cur_dt_entry->board_hw_subtype & 0xffff00);
 
-	/* 1. must match the platform_id, platform_hw_id, platform_version
-	*  2. soc rev number equal then return 0
-	*  3. dt soc rev number less than cdt return -1
-	*  4. otherwise return 1
+	/* 1. must match the msm_id, platform_hw_id, platform_subtype and DDR size
+	*  soc, board major/minor, pmic major/minor must less than board info
+	*  2. find the matched DTB then return 1
+	*  3. otherwise return 0
 	*/
+	if((cur_dt_msm_id == (board_platform_id() & 0x0000ffff)) &&
+		(cur_dt_hw_platform == board_hardware_id()) &&
+		(cur_dt_hw_subtype == board_hardware_subtype()) &&
+		(cur_dt_hlos_subtype == target_get_hlos_subtype()) &&
+		(cur_dt_entry->soc_rev <= board_soc_version()) &&
+		((cur_dt_entry->variant_id & 0x00ffff00) <= (board_target_id() & 0x00ffff00)) &&
+		((cur_dt_entry->pmic_rev[0] & 0x00ffff00) <= (board_pmic_target(0) & 0x00ffff00)) &&
+		((cur_dt_entry->pmic_rev[1] & 0x00ffff00) <= (board_pmic_target(1) & 0x00ffff00)) &&
+		((cur_dt_entry->pmic_rev[2] & 0x00ffff00) <= (board_pmic_target(2) & 0x00ffff00)) &&
+		((cur_dt_entry->pmic_rev[3] & 0x00ffff00) <= (board_pmic_target(3) & 0x00ffff00))) {
 
-	if((cur_dt_entry->platform_id == board_dt_data->platform_variant_id) &&
-		(cur_dt_target_id == board_dt_data->target_variant_id) &&
-		(cur_dt_hlos_subtype == target_get_hlos_subtype())) {
-		if(cur_dt_entry->soc_rev == board_soc_version()) {
+		dt_node_tmp = dt_entry_list_init();
+		memcpy((char*)dt_node_tmp->dt_entry_m,(char*)cur_dt_entry, sizeof(struct dt_entry));
+
+		dprintf(SPEW, "Add DTB entry %u/%08x/0x%08x/%x/%x/%x/%x/%x/%x/%x\n",
+			dt_node_tmp->dt_entry_m->platform_id, dt_node_tmp->dt_entry_m->variant_id,
+			dt_node_tmp->dt_entry_m->board_hw_subtype, dt_node_tmp->dt_entry_m->soc_rev,
+			dt_node_tmp->dt_entry_m->pmic_rev[0], dt_node_tmp->dt_entry_m->pmic_rev[1],
+			dt_node_tmp->dt_entry_m->pmic_rev[2], dt_node_tmp->dt_entry_m->pmic_rev[3],
+			dt_node_tmp->dt_entry_m->offset, dt_node_tmp->dt_entry_m->size);
+
+		insert_dt_entry_in_queue(dt_list, dt_node_tmp);
+		return 1;
+	}
+	return 0;
+}
+
+static int platform_dt_absolute_compat_match(struct dt_entry_node *dt_list, uint32_t dtb_info) {
+	struct dt_entry_node *dt_node_tmp1 = NULL;
+	struct dt_entry_node *dt_node_tmp2 = NULL;
+	uint32_t current_info = 0;
+	uint32_t board_info = 0;
+	uint32_t best_info = 0;
+	uint32_t current_pmic_model[4] = {0, 0, 0, 0};
+	uint32_t board_pmic_model[4] = {0, 0, 0, 0};
+	uint32_t best_pmic_model[4] = {0, 0, 0, 0};
+	uint32_t delete_current_dt = 0;
+	uint32_t i;
+
+	/* start to select the exact entry
+	* default to exact match 0, if find current DTB entry info is the same as board info,
+	* then exact match board info.
+	*/
+	list_for_every_entry(&dt_list->node, dt_node_tmp1, dt_node, node) {
+		if (!dt_node_tmp1){
+			dprintf(SPEW, "Current node is the end\n");
+			break;
+		}
+		switch(dtb_info) {
+		case DTB_FOUNDRY:
+			current_info = ((dt_node_tmp1->dt_entry_m->platform_id) & 0x00ff0000);
+			board_info = board_foundry_id();
+			break;
+		case DTB_PMIC_MODEL:
+			for (i = 0; i < 4; i++) {
+				current_pmic_model[i] = (dt_node_tmp1->dt_entry_m->pmic_rev[i] & 0xff);
+				board_pmic_model[i] = (board_pmic_target(i) & 0xff);
+			}
+			break;
+		default:
+			dprintf(CRITICAL, "ERROR: Unsupported version (%d) in dt node check \n",
+					dtb_info);
 			return 0;
-		} else if(cur_dt_entry->soc_rev < board_soc_version()) {
-			return -1;
+		}
+
+		if (dtb_info == DTB_PMIC_MODEL) {
+			if ((current_pmic_model[0] == board_pmic_model[0]) &&
+				(current_pmic_model[1] == board_pmic_model[1]) &&
+				(current_pmic_model[2] == board_pmic_model[2]) &&
+				(current_pmic_model[3] == board_pmic_model[3])) {
+
+				for (i = 0; i < 4; i++) {
+					best_pmic_model[i] = current_pmic_model[i];
+				}
+				break;
+			}
+		} else {
+			if (current_info == board_info) {
+				best_info = current_info;
+				break;
+			}
+		}
+	}
+
+	list_for_every_entry(&dt_list->node, dt_node_tmp1, dt_node, node) {
+		if (!dt_node_tmp1){
+			dprintf(SPEW, "Current node is the end\n");
+			break;
+		}
+		switch(dtb_info) {
+		case DTB_FOUNDRY:
+			current_info = ((dt_node_tmp1->dt_entry_m->platform_id) & 0x00ff0000);
+			break;
+		case DTB_PMIC_MODEL:
+			for (i = 0; i < 4; i++) {
+				current_pmic_model[i] = (dt_node_tmp1->dt_entry_m->pmic_rev[i] & 0xff);
+			}
+			break;
+		default:
+			dprintf(CRITICAL, "ERROR: Unsupported version (%d) in dt node check \n",
+					dtb_info);
+			return 0;
+		}
+
+		if (dtb_info == DTB_PMIC_MODEL) {
+			if ((current_pmic_model[0] != best_pmic_model[0]) ||
+				(current_pmic_model[1] != best_pmic_model[1]) ||
+				(current_pmic_model[2] != best_pmic_model[2]) ||
+				(current_pmic_model[3] != best_pmic_model[3])) {
+
+				delete_current_dt = 1;
+			}
+		} else {
+			if (current_info != best_info) {
+				delete_current_dt = 1;
+			}
+		}
+
+		if (delete_current_dt) {
+			dprintf(SPEW, "Delete don't fit DTB entry %u/%08x/0x%08x/%x/%x/%x/%x/%x/%x/%x\n",
+				dt_node_tmp1->dt_entry_m->platform_id, dt_node_tmp1->dt_entry_m->variant_id,
+				dt_node_tmp1->dt_entry_m->board_hw_subtype, dt_node_tmp1->dt_entry_m->soc_rev,
+				dt_node_tmp1->dt_entry_m->pmic_rev[0], dt_node_tmp1->dt_entry_m->pmic_rev[1],
+				dt_node_tmp1->dt_entry_m->pmic_rev[2], dt_node_tmp1->dt_entry_m->pmic_rev[3],
+				dt_node_tmp1->dt_entry_m->offset, dt_node_tmp1->dt_entry_m->size);
+
+			dt_node_tmp2 = dt_node_tmp1->node.prev;
+			dt_entry_list_delete(dt_node_tmp1);
+			dt_node_tmp1 = dt_node_tmp2;
+			delete_current_dt = 0;
 		}
 	}
 
 	return 1;
 }
 
-static int __dev_tree_get_entry_info(struct dt_table *table, struct dt_entry *dt_entry_info,
-										struct board_dt_entry *board_dt_data, uint32_t subtype_mask)
+static int update_dtb_entry_node(struct dt_entry_node *dt_list, uint32_t dtb_info) {
+	struct dt_entry_node *dt_node_tmp1 = NULL;
+	struct dt_entry_node *dt_node_tmp2 = NULL;
+	uint32_t current_info = 0;
+	uint32_t board_info = 0;
+	uint32_t best_info = 0;
+
+	/* start to select the best entry*/
+	list_for_every_entry(&dt_list->node, dt_node_tmp1, dt_node, node) {
+		if (!dt_node_tmp1){
+			dprintf(SPEW, "Current node is the end\n");
+			break;
+		}
+		switch(dtb_info) {
+		case DTB_SOC:
+			current_info = dt_node_tmp1->dt_entry_m->soc_rev;
+			board_info = board_soc_version();
+			break;
+		case DTB_MAJOR_MINOR:
+			current_info = ((dt_node_tmp1->dt_entry_m->variant_id) & 0x00ffff00);
+			board_info = (board_target_id() & 0x00ffff00);
+			break;
+		case DTB_PMIC0:
+			current_info = ((dt_node_tmp1->dt_entry_m->pmic_rev[0]) & 0x00ffff00);
+			board_info = (board_pmic_target(0) & 0x00ffff00);
+			break;
+		case DTB_PMIC1:
+			current_info = ((dt_node_tmp1->dt_entry_m->pmic_rev[1]) & 0x00ffff00);
+			board_info = (board_pmic_target(1) & 0x00ffff00);
+			break;
+		case DTB_PMIC2:
+			current_info = ((dt_node_tmp1->dt_entry_m->pmic_rev[2]) & 0x00ffff00);
+			board_info = (board_pmic_target(2) & 0x00ffff00);
+			break;
+		case DTB_PMIC3:
+			current_info = ((dt_node_tmp1->dt_entry_m->pmic_rev[3]) & 0x00ffff00);
+			board_info = (board_pmic_target(3) & 0x00ffff00);
+			break;
+		default:
+			dprintf(CRITICAL, "ERROR: Unsupported version (%d) in dt node check \n",
+					dtb_info);
+			return 0;
+		}
+
+		if (current_info == board_info) {
+			best_info = current_info;
+			break;
+		}
+		if ((current_info < board_info) && (current_info > best_info)) {
+			best_info = current_info;
+		}
+		if (current_info < best_info) {
+			dprintf(SPEW, "Delete don't fit DTB entry %u/%08x/0x%08x/%x/%x/%x/%x/%x/%x/%x\n",
+				dt_node_tmp1->dt_entry_m->platform_id, dt_node_tmp1->dt_entry_m->variant_id,
+				dt_node_tmp1->dt_entry_m->board_hw_subtype, dt_node_tmp1->dt_entry_m->soc_rev,
+				dt_node_tmp1->dt_entry_m->pmic_rev[0], dt_node_tmp1->dt_entry_m->pmic_rev[1],
+				dt_node_tmp1->dt_entry_m->pmic_rev[2], dt_node_tmp1->dt_entry_m->pmic_rev[3],
+				dt_node_tmp1->dt_entry_m->offset, dt_node_tmp1->dt_entry_m->size);
+
+			dt_node_tmp2 = dt_node_tmp1->node.prev;
+			dt_entry_list_delete(dt_node_tmp1);
+			dt_node_tmp1 = dt_node_tmp2;
+		}
+	}
+
+	list_for_every_entry(&dt_list->node, dt_node_tmp1, dt_node, node) {
+		if (!dt_node_tmp1){
+			dprintf(SPEW, "Current node is the end\n");
+			break;
+		}
+		switch(dtb_info) {
+		case DTB_SOC:
+			current_info = dt_node_tmp1->dt_entry_m->soc_rev;
+			break;
+		case DTB_MAJOR_MINOR:
+			current_info = ((dt_node_tmp1->dt_entry_m->variant_id) & 0x00ffff00);
+			break;
+		case DTB_PMIC0:
+			current_info = ((dt_node_tmp1->dt_entry_m->pmic_rev[0]) & 0x00ffff00);
+			break;
+		case DTB_PMIC1:
+			current_info = ((dt_node_tmp1->dt_entry_m->pmic_rev[1]) & 0x00ffff00);
+			break;
+		case DTB_PMIC2:
+			current_info = ((dt_node_tmp1->dt_entry_m->pmic_rev[2]) & 0x00ffff00);
+			break;
+		case DTB_PMIC3:
+			current_info = ((dt_node_tmp1->dt_entry_m->pmic_rev[3]) & 0x00ffff00);
+			break;
+		default:
+			dprintf(CRITICAL, "ERROR: Unsupported version (%d) in dt node check \n",
+					dtb_info);
+			return 0;
+		}
+
+		if (current_info != best_info) {
+			dprintf(SPEW, "Delete don't fit DTB entry %u/%08x/0x%08x/%x/%x/%x/%x/%x/%x/%x\n",
+				dt_node_tmp1->dt_entry_m->platform_id, dt_node_tmp1->dt_entry_m->variant_id,
+				dt_node_tmp1->dt_entry_m->board_hw_subtype, dt_node_tmp1->dt_entry_m->soc_rev,
+				dt_node_tmp1->dt_entry_m->pmic_rev[0], dt_node_tmp1->dt_entry_m->pmic_rev[1],
+				dt_node_tmp1->dt_entry_m->pmic_rev[2], dt_node_tmp1->dt_entry_m->pmic_rev[3],
+				dt_node_tmp1->dt_entry_m->offset, dt_node_tmp1->dt_entry_m->size);
+
+			dt_node_tmp2 = dt_node_tmp1->node.prev;
+			dt_entry_list_delete(dt_node_tmp1);
+			dt_node_tmp1 = dt_node_tmp2;
+		}
+	}
+	return 1;
+}
+
+static struct dt_entry *platform_dt_match_best(struct dt_entry_node *dt_list)
+{
+	struct dt_entry_node *dt_node_tmp1 = NULL;
+
+	/* check Foundry id
+	* the foundry id must exact match board founddry id, this is compatibility check,
+	* if couldn't find the exact match from DTB, will exact match 0x0.
+	*/
+	if (!platform_dt_absolute_compat_match(dt_list, DTB_FOUNDRY))
+		return NULL;
+
+	/* check PMIC model
+	* the PMIC model must exact match board PMIC model, this is compatibility check,
+	* if couldn't find the exact match from DTB, will exact match 0x0.
+	*/
+	if (!platform_dt_absolute_compat_match(dt_list, DTB_PMIC_MODEL))
+		return NULL;
+
+	/* check soc version
+	* the suitable soc version must less than or equal to board soc version
+	*/
+	if (!update_dtb_entry_node(dt_list, DTB_SOC))
+		return NULL;
+
+	/*check major and minor version
+	* the suitable major&minor version must less than or equal to board major&minor version
+	*/
+	if (!update_dtb_entry_node(dt_list, DTB_MAJOR_MINOR))
+		return NULL;
+
+	/*check pmic info
+	* the suitable pmic major&minor info must less than or equal to board pmic major&minor version
+	*/
+	if (!update_dtb_entry_node(dt_list, DTB_PMIC0))
+		return NULL;
+	if (!update_dtb_entry_node(dt_list, DTB_PMIC1))
+		return NULL;
+	if (!update_dtb_entry_node(dt_list, DTB_PMIC2))
+		return NULL;
+	if (!update_dtb_entry_node(dt_list, DTB_PMIC3))
+		return NULL;
+
+	list_for_every_entry(&dt_list->node, dt_node_tmp1, dt_node, node) {
+		if (!dt_node_tmp1) {
+			dprintf(CRITICAL, "ERROR: Couldn't find the suitable DTB!\n");
+			return NULL;
+		}
+		if (dt_node_tmp1->dt_entry_m)
+			return dt_node_tmp1->dt_entry_m;
+	}
+
+	return NULL;
+}
+
+/* Function to obtain the index information for the correct device tree
+ *  based on the platform data.
+ *  If a matching device tree is found, the information is returned in the
+ *  "dt_entry_info" out parameter and a function value of 0 is returned, otherwise
+ *  a non-zero function value is returned.
+ */
+int dev_tree_get_entry_info(struct dt_table *table, struct dt_entry *dt_entry_info)
 {
 	uint32_t i;
-	unsigned char *table_ptr;
+	unsigned char *table_ptr = NULL;
 	struct dt_entry dt_entry_buf_1;
-	struct dt_entry dt_entry_buf_2;
-	struct dt_entry *cur_dt_entry;
-	struct dt_entry *best_match_dt_entry;
-	struct dt_entry_v1 *dt_entry_v1;
+	struct dt_entry *cur_dt_entry = NULL;
+	struct dt_entry *best_match_dt_entry = NULL;
+	struct dt_entry_v1 *dt_entry_v1 = NULL;
+	struct dt_entry_v2 *dt_entry_v2 = NULL;
+	struct dt_entry_node *dt_entry_queue = NULL;
+	struct dt_entry_node *dt_node_tmp1 = NULL;
+	struct dt_entry_node *dt_node_tmp2 = NULL;
 	uint32_t found = 0;
 
 	if (!dt_entry_info) {
@@ -514,7 +893,16 @@
 	table_ptr = (unsigned char *)table + DEV_TREE_HEADER_SIZE;
 	cur_dt_entry = &dt_entry_buf_1;
 	best_match_dt_entry = NULL;
+	dt_entry_queue = (struct dt_entry_node *)
+				malloc(sizeof(struct dt_entry_node));
 
+	if (!dt_entry_queue) {
+		dprintf(CRITICAL, "Out of memory\n");
+		return -1;
+	}
+
+	list_initialize(&dt_entry_queue->node);
+	dprintf(INFO, "DTB Total entry: %d, DTB version: %d\n", table->num_entries, table->version);
 	for(i = 0; found == 0 && i < table->num_entries; i++)
 	{
 		memset(cur_dt_entry, 0, sizeof(struct dt_entry));
@@ -524,48 +912,74 @@
 			cur_dt_entry->platform_id = dt_entry_v1->platform_id;
 			cur_dt_entry->variant_id = dt_entry_v1->variant_id;
 			cur_dt_entry->soc_rev = dt_entry_v1->soc_rev;
-			cur_dt_entry->board_hw_subtype = board_hardware_subtype();
+			cur_dt_entry->board_hw_subtype = (dt_entry_v1->variant_id >> 0x18);
+			cur_dt_entry->pmic_rev[0] = board_pmic_target(0);
+			cur_dt_entry->pmic_rev[1] = board_pmic_target(1);
+			cur_dt_entry->pmic_rev[2] = board_pmic_target(2);
+			cur_dt_entry->pmic_rev[3] = board_pmic_target(3);
 			cur_dt_entry->offset = dt_entry_v1->offset;
 			cur_dt_entry->size = dt_entry_v1->size;
 			table_ptr += sizeof(struct dt_entry_v1);
 			break;
 		case DEV_TREE_VERSION_V2:
+			dt_entry_v2 = (struct dt_entry_v2*)table_ptr;
+			cur_dt_entry->platform_id = dt_entry_v2->platform_id;
+			cur_dt_entry->variant_id = dt_entry_v2->variant_id;
+			cur_dt_entry->soc_rev = dt_entry_v2->soc_rev;
+			/* For V2 version of DTBs we have platform version field as part
+			 * of variant ID, in such case the subtype will be mentioned as 0x0
+			 * As the qcom, board-id = <0xSSPMPmPH, 0x0>
+			 * SS -- Subtype
+			 * PM -- Platform major version
+			 * Pm -- Platform minor version
+			 * PH -- Platform hardware CDP/MTP
+			 * In such case to make it compatible with LK algorithm move the subtype
+			 * from variant_id to subtype field
+			 */
+			if (dt_entry_v2->board_hw_subtype == 0)
+				cur_dt_entry->board_hw_subtype = (cur_dt_entry->variant_id >> 0x18);
+			else
+				cur_dt_entry->board_hw_subtype = dt_entry_v2->board_hw_subtype;
+			cur_dt_entry->pmic_rev[0] = board_pmic_target(0);
+			cur_dt_entry->pmic_rev[1] = board_pmic_target(1);
+			cur_dt_entry->pmic_rev[2] = board_pmic_target(2);
+			cur_dt_entry->pmic_rev[3] = board_pmic_target(3);
+			cur_dt_entry->offset = dt_entry_v2->offset;
+			cur_dt_entry->size = dt_entry_v2->size;
+			table_ptr += sizeof(struct dt_entry_v2);
+			break;
+		case DEV_TREE_VERSION_V3:
 			memcpy(cur_dt_entry, (struct dt_entry *)table_ptr,
 				   sizeof(struct dt_entry));
+			/* For V3 version of DTBs we have platform version field as part
+			 * of variant ID, in such case the subtype will be mentioned as 0x0
+			 * As the qcom, board-id = <0xSSPMPmPH, 0x0>
+			 * SS -- Subtype
+			 * PM -- Platform major version
+			 * Pm -- Platform minor version
+			 * PH -- Platform hardware CDP/MTP
+			 * In such case to make it compatible with LK algorithm move the subtype
+			 * from variant_id to subtype field
+			 */
+			if (cur_dt_entry->board_hw_subtype == 0)
+				cur_dt_entry->board_hw_subtype = (cur_dt_entry->variant_id >> 0x18);
+
 			table_ptr += sizeof(struct dt_entry);
 			break;
 		default:
 			dprintf(CRITICAL, "ERROR: Unsupported version (%d) in DT table \n",
 					table->version);
+			free(dt_entry_queue);
 			return -1;
 		}
 
-		/* DTBs are stored in the ascending order of soc revision.
-		 * For eg: Rev0..Rev1..Rev2 & so on.
-		 * we pickup the DTB with highest soc rev number which is less
-		 * than or equal to actual hardware
-		 */
-		switch(platform_dt_match(cur_dt_entry, board_dt_data, subtype_mask)) {
-		case 0:
-			best_match_dt_entry = cur_dt_entry;
-			found = 1;
-			break;
-		case -1:
-			if (!best_match_dt_entry) {
-				/* copy structure */
-				best_match_dt_entry = cur_dt_entry;
-				cur_dt_entry = &dt_entry_buf_2;
-			} else {
-				/* Swap dt_entry buffers */
-				struct dt_entry *temp = cur_dt_entry;
-				cur_dt_entry = best_match_dt_entry;
-				best_match_dt_entry = temp;
-			}
-		default:
-			break;
-		}
-	}
+		/* DTBs must match the platform_id, platform_hw_id, platform_subtype and DDR size.
+		* The satisfactory DTBs are stored in dt_entry_queue
+		*/
+		platform_dt_absolute_match(cur_dt_entry, dt_entry_queue);
 
+	}
+	best_match_dt_entry = platform_dt_match_best(dt_entry_queue);
 	if (best_match_dt_entry) {
 		*dt_entry_info = *best_match_dt_entry;
 		found = 1;
@@ -575,71 +989,37 @@
 		dprintf(INFO, "Using DTB entry 0x%08x/%08x/0x%08x/%u for device 0x%08x/%08x/0x%08x/%u\n",
 				dt_entry_info->platform_id, dt_entry_info->soc_rev,
 				dt_entry_info->variant_id, dt_entry_info->board_hw_subtype,
-				board_dt_data->platform_variant_id, board_soc_version(),
-				board_dt_data->target_variant_id, board_hardware_subtype());
+				board_platform_id(), board_soc_version(),
+				board_target_id(), board_hardware_subtype());
+		if (dt_entry_info->pmic_rev[0] == 0 && dt_entry_info->pmic_rev[0] == 0 &&
+			dt_entry_info->pmic_rev[0] == 0 && dt_entry_info->pmic_rev[0] == 0) {
+			dprintf(SPEW, "No maintain pmic info in DTB, device pmic info is 0x%0x/0x%x/0x%x/0x%0x\n",
+					board_pmic_target(0), board_pmic_target(1),
+					board_pmic_target(2), board_pmic_target(3));
+		} else {
+			dprintf(INFO, "Using pmic info 0x%0x/0x%x/0x%x/0x%0x for device 0x%0x/0x%x/0x%x/0x%0x\n",
+					dt_entry_info->pmic_rev[0], dt_entry_info->pmic_rev[1],
+					dt_entry_info->pmic_rev[2], dt_entry_info->pmic_rev[3],
+					board_pmic_target(0), board_pmic_target(1),
+					board_pmic_target(2), board_pmic_target(3));
+		}
 		return 0;
 	}
 
-	dprintf(CRITICAL, "INFO: Unable to find suitable device tree for device (0x%08x/0x%08x/0x%08x/%u)\n",
-			board_dt_data->platform_variant_id, board_soc_version(),
-			board_dt_data->target_variant_id, board_hardware_subtype());
+	dprintf(CRITICAL, "ERROR: Unable to find suitable device tree for device (%u/0x%08x/0x%08x/%u)\n",
+			board_platform_id(), board_soc_version(),
+			board_target_id(), board_hardware_subtype());
+
+	list_for_every_entry(&dt_entry_queue->node, dt_node_tmp1, dt_node, node) {
+		/* free node memory */
+		dt_node_tmp2 = dt_node_tmp1->node.prev;
+		dt_entry_list_delete(dt_node_tmp1);
+		dt_node_tmp1 = dt_node_tmp2;
+	}
+	free(dt_entry_queue);
 	return -1;
 }
 
-/* Function to obtain the index information for the correct device tree
- *  based on the platform data.
- *  If a matching device tree is found, the information is returned in the
- *  "dt_entry_info" out parameter and a function value of 0 is returned, otherwise
- *  a non-zero function value is returned.
- */
-int dev_tree_get_entry_info(struct dt_table *table, struct dt_entry *dt_entry_info)
-{
-	struct board_dt_entry board_dt_data;
-
-	/* 1. Look for new board-id (platform version + hw + subtype) & new msm-id (soc ver + soc id + foundry-id) */
-	board_dt_data.target_variant_id = board_target_id();
-	/* Platform-id
-	 * bit no |31    24|23      16|15   0|
-	 *        |reserved|foundry-id|msm-id|
-	 */
-	board_dt_data.platform_variant_id = board_platform_id() | (board_foundry_id() << 16);
-	if (__dev_tree_get_entry_info(table, dt_entry_info, &board_dt_data, 0xff) == 0) {
-		return 0;
-	}
-
-	/* 2. Look for new board-id & old msm-id (no foundry-id) */
-	board_dt_data.target_variant_id = board_target_id();
-	board_dt_data.platform_variant_id = board_platform_id();
-	if (__dev_tree_get_entry_info(table, dt_entry_info, &board_dt_data, 0xff) == 0) {
-		return 0;
-	}
-
-	/*
-	 * for compatible with version 1 and version 2 dtbtool
-	 * will compare the subtype inside the variant id
-	 */
-
-	/* 3. Look for old board-id (no platform version) & new msm-id (with foundry-id) */
-	board_dt_data.target_variant_id = board_hardware_id() | ((board_hardware_subtype() & 0xff) << 24);
-	board_dt_data.platform_variant_id = board_platform_id() | (board_foundry_id() << 16);
-	if (__dev_tree_get_entry_info(table, dt_entry_info, &board_dt_data, 0xff) == 0) {
-		return 0;
-	}
-	/* 4. Look for old board-id (no platform versions) & old msm-id(no foundry-id) */
-	board_dt_data.target_variant_id = board_hardware_id() | ((board_hardware_subtype() & 0xff) << 24);
-	board_dt_data.platform_variant_id = board_platform_id();
-	if (__dev_tree_get_entry_info(table, dt_entry_info, &board_dt_data, 0xff) == 0) {
-		return 0;
-	}
-
-	/*
-	* add compatible with old device selection method which don't compare subtype
-	*/
-	board_dt_data.target_variant_id = board_hardware_id();
-	board_dt_data.platform_variant_id = board_platform_id();
-	return __dev_tree_get_entry_info(table, dt_entry_info, &board_dt_data, 0);
-}
-
 /* Function to add the first RAM partition info to the device tree.
  * Note: The function replaces the reg property in the "/memory" node
  * with the addr and size provided.
diff --git a/platform/msm_shared/include/board.h b/platform/msm_shared/include/board.h
index 9efde4d..d7e82d5 100644
--- a/platform/msm_shared/include/board.h
+++ b/platform/msm_shared/include/board.h
@@ -1,4 +1,4 @@
-/* Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
+/* Copyright (c) 2012-2014, The Linux Foundation. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions are
@@ -39,6 +39,7 @@
 struct board_pmic_data {
 	uint32_t pmic_type;
 	uint32_t pmic_version;
+	uint32_t pmic_target;
 };
 
 struct board_data {
@@ -65,6 +66,7 @@
 uint32_t board_hardware_subtype(void);
 uint32_t board_get_ddr_subtype(void);
 uint32_t board_hlos_subtype(void);
+uint32_t board_pmic_target(uint8_t num_ent);
 
 /* DDR Subtype Macros
  * Determine the DDR Size on the device and define
diff --git a/platform/msm_shared/include/dev_tree.h b/platform/msm_shared/include/dev_tree.h
index ef41046..a6534c0 100644
--- a/platform/msm_shared/include/dev_tree.h
+++ b/platform/msm_shared/include/dev_tree.h
@@ -1,4 +1,4 @@
-/* Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
+/* Copyright (c) 2012-2014, The Linux Foundation. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions are
@@ -27,6 +27,7 @@
 */
 
 #include <debug.h>
+#include <list.h>
 
 #ifndef __DEVICE_TREE__
 #define __DEVICE_TREE__
@@ -36,6 +37,8 @@
 #define DEV_TREE_MAGIC_LEN      4
 #define DEV_TREE_VERSION_V1     1
 #define DEV_TREE_VERSION_V2     2
+#define DEV_TREE_VERSION_V3     3
+
 #define DEV_TREE_HEADER_SIZE    12
 
 #define DTB_MAGIC               0xedfe0dd0
@@ -54,6 +57,18 @@
 #define DT_ENTRY_V1_SIZE        0xC
 #define PLAT_ID_SIZE            0x8
 #define BOARD_ID_SIZE           0x8
+#define PMIC_ID_SIZE           0x8
+
+
+struct dt_entry_v2
+{
+	uint32_t platform_id;
+	uint32_t variant_id;
+	uint32_t board_hw_subtype;
+	uint32_t soc_rev;
+	uint32_t offset;
+	uint32_t size;
+};
 
 struct dt_entry
 {
@@ -61,6 +76,7 @@
 	uint32_t variant_id;
 	uint32_t board_hw_subtype;
 	uint32_t soc_rev;
+	uint32_t pmic_rev[4];
 	uint32_t offset;
 	uint32_t size;
 };
@@ -84,6 +100,11 @@
 	uint32_t platform_subtype;
 };
 
+struct pmic_id
+{
+	uint32_t pmic_version[4];
+};
+
 struct dt_mem_node_info
 {
 	uint32_t offset;
@@ -92,17 +113,28 @@
 	uint32_t size_cell_size;
 };
 
+enum dt_entry_info
+{
+	DTB_FOUNDRY = 0,
+	DTB_SOC,
+	DTB_MAJOR_MINOR,
+	DTB_PMIC0,
+	DTB_PMIC1,
+	DTB_PMIC2,
+	DTB_PMIC3,
+	DTB_PMIC_MODEL,
+};
+
 enum dt_err_codes
 {
 	DT_OP_SUCCESS,
 	DT_OP_FAILURE = -1,
 };
 
-struct board_dt_entry
-{
-	uint32_t target_variant_id;
-	uint32_t platform_variant_id;
-};
+typedef struct dt_entry_node {
+	struct list_node node;
+	struct dt_entry * dt_entry_m;
+}dt_node;
 
 int dev_tree_validate(struct dt_table *table, unsigned int page_size, uint32_t *dt_hdr_size);
 int dev_tree_get_entry_info(struct dt_table *table, struct dt_entry *dt_entry_info);
diff --git a/platform/msm_shared/mipi_dsi_autopll_20nm.c b/platform/msm_shared/mipi_dsi_autopll_20nm.c
index 754eb18..0f7052a 100644
--- a/platform/msm_shared/mipi_dsi_autopll_20nm.c
+++ b/platform/msm_shared/mipi_dsi_autopll_20nm.c
@@ -227,6 +227,15 @@
 	pll_20nm_phy_loop_bw_config(pll_base);
 }
 
+static void mdss_dsi_pll_20nm_disable(uint32_t pll_base)
+{
+	dprintf(SPEW, "Disabling DSI PHY PLL \n");
+	writel(0x042, pll_base + MMSS_DSI_PHY_PLL_PLL_VCOTAIL_EN);
+	writel(0x002, pll_base + MMSS_DSI_PHY_PLL_BIAS_EN_CLKBUFLR_EN);
+	writel(0x002, pll_base + MMSS_DSI_PHY_PLL_RESETSM_CNTRL3);
+	dmb();
+}
+
 int32_t mdss_dsi_auto_pll_20nm_config(uint32_t pll_base, uint32_t ctl_base,
 				struct mdss_dsi_pll_config *pd)
 {
@@ -235,6 +244,15 @@
 	mdss_dsi_phy_sw_reset(ctl_base);
 	pll_20nm_phy_config(pll_base);
 
+	/*
+	 * For 20nm PHY, DSI PLL 1 drains some current in its reset state.
+	 * Need to turn off the DSI1 PLL explicitly.
+	 */
+	if (ctl_base == MIPI_DSI0_BASE) {
+		dprintf(SPEW, "Calling disable function for PHY PLL 1 \n");
+		mdss_dsi_pll_20nm_disable(DSI1_PLL_BASE);
+	}
+
 	/* set up divider */
 	data = readl(pll_base + MMSS_DSI_PHY_PLL_POST_DIVIDER_CONTROL);
 	data |= 0x080; /* bit 7 */
diff --git a/platform/msm_shared/rpm-smd.c b/platform/msm_shared/rpm-smd.c
index 17b7ff5..6144fba 100644
--- a/platform/msm_shared/rpm-smd.c
+++ b/platform/msm_shared/rpm-smd.c
@@ -103,9 +103,6 @@
 
 			ret = smd_write(&ch, smd_data, len_to_smd, SMD_APPS_RPM);
 
-			/* TODO: Use events */
-			mdelay(1);
-
 			/* Read the response */
 			ack_msg_len = rpm_recv_data(&rlen);
 
diff --git a/platform/msm_shared/smd.c b/platform/msm_shared/smd.c
index 6377e7e..dfb5b62 100644
--- a/platform/msm_shared/smd.c
+++ b/platform/msm_shared/smd.c
@@ -167,8 +167,20 @@
 		return -1;
 	}
 
-	/* Clear the data_written flag */
-	ch->port_info->ch1.data_written = 0;
+	/* Wait until the data updated in the smd buffer is equal to smd packet header*/
+	while ((ch->port_info->ch1.write_index - ch->port_info->ch1.read_index) < sizeof(smd_pkt_hdr))
+	{
+		/* Get the update info from memory */
+		arch_invalidate_cache_range((addr_t) ch->port_info, size);
+
+		if ((ch->port_info->ch1.read_index + sizeof(smd_pkt_hdr)) >= ch->fifo_size)
+		{
+			dprintf(CRITICAL, "At %d:%s:RX channel read index [%u] is greater than RX fifo size[%u]\n",
+							   __LINE__,__func__, ch->port_info->ch1.read_index, ch->fifo_size);
+			return -1;
+		}
+	}
+
 
 	arch_invalidate_cache_range((addr_t)(ch->recv_buf + ch->port_info->ch1.read_index), sizeof(smd_hdr));
 
@@ -177,6 +189,21 @@
 
 	*len = smd_hdr.pkt_size;
 
+	/* Wait on the data being updated in SMEM before returing the response */
+	while ((ch->port_info->ch1.write_index - ch->port_info->ch1.read_index) < smd_hdr.pkt_size)
+	{
+		/* Get the update info from memory */
+		arch_invalidate_cache_range((addr_t) ch->port_info, size);
+
+		if ((ch->port_info->ch1.read_index + sizeof(smd_hdr) + smd_hdr.pkt_size) >= ch->fifo_size)
+		{
+			dprintf(CRITICAL, "At %d:%s:RX channel read index [%u] is greater than RX fifo size[%u]\n",
+							   __LINE__,__func__, ch->port_info->ch1.read_index, ch->fifo_size);
+			return -1;
+		}
+	}
+
+	/* We are good to return the response now */
 	return (uint8_t*)(ch->recv_buf + ch->port_info->ch1.read_index + sizeof(smd_hdr));
 }
 
@@ -184,10 +211,12 @@
 {
 	ch->port_info->ch1.read_index += sizeof(smd_pkt_hdr) + len;
 
+	/* Clear the data_written flag */
+	ch->port_info->ch1.data_written = 0;
+
 	/* Set the data_read flag */
-	ch->port_info->ch1.data_read = 1;
+	ch->port_info->ch0.data_read = 1;
 	ch->port_info->ch0.mask_recv_intr = 1;
-	ch->port_info->ch0.state_updated = 1;
 
 	dsb();
 
@@ -244,7 +273,6 @@
 
 	ch->port_info->ch0.data_written = 1;
 	ch->port_info->ch0.mask_recv_intr = 0;
-	ch->port_info->ch0.state_updated = 1;
 
 	dsb();
 
diff --git a/platform/msm_shared/smem.h b/platform/msm_shared/smem.h
index 14925f5..c5789a8 100644
--- a/platform/msm_shared/smem.h
+++ b/platform/msm_shared/smem.h
@@ -375,6 +375,7 @@
 	FSM9010  = 255,
 	FSM9016  = 256,
 	FSM9055  = 257,
+	MSM8239  = 263,
 };
 
 enum platform {
diff --git a/project/msm8916.mk b/project/msm8916.mk
index 22793b2..201f173 100644
--- a/project/msm8916.mk
+++ b/project/msm8916.mk
@@ -26,7 +26,7 @@
 DEFINES += ABOOT_FORCE_TAGS_ADDR=0x81E00000
 
 #Enable the feature of long press power on
-DEFINES += LONG_PRESS_POWER_ON=0
+DEFINES += LONG_PRESS_POWER_ON=1
 
 #Disable thumb mode
 ENABLE_THUMB := false
diff --git a/target/apq8084/target_display.c b/target/apq8084/target_display.c
index 1dd3a66..88b0a9a 100755
--- a/target/apq8084/target_display.c
+++ b/target/apq8084/target_display.c
@@ -371,7 +371,7 @@
 
 	if (!strcmp(panel_name, HDMI_PANEL_NAME)) {
 		if (buf_size < (prefix_string_len + LK_OVERRIDE_PANEL_LEN +
-				HDMI_CONTROLLER_STRING)) {
+				strlen(HDMI_CONTROLLER_STRING))) {
 			dprintf(CRITICAL, "command line argument is greater than buffer size\n");
 			return false;
 		}
diff --git a/target/msm8916/init.c b/target/msm8916/init.c
index 2385d24..bcac813 100644
--- a/target/msm8916/init.c
+++ b/target/msm8916/init.c
@@ -341,6 +341,7 @@
 	case MSM8236:
 	case MSM8636:
 	case MSM8936:
+	case MSM8239:
 		board->baseband = BASEBAND_MSM;
 	break;
 	case APQ8016: