Merge "target: msm8953: enable HX8399C HD+ panel for SDM632 CDP target"
diff --git a/dev/gcdb/display/include/panel_auo_400p_cmd.h b/dev/gcdb/display/include/panel_auo_400p_cmd.h
index f772f2d..400d156 100644
--- a/dev/gcdb/display/include/panel_auo_400p_cmd.h
+++ b/dev/gcdb/display/include/panel_auo_400p_cmd.h
@@ -1,4 +1,4 @@
-/* Copyright (c) 2016-2017, The Linux Foundation. All rights reserved.
+/* Copyright (c) 2016-2018, 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
@@ -38,7 +38,7 @@
 /*---------------------------------------------------------------------------*/
 static struct panel_config auo_400p_cmd_panel_data = {
 	"qcom,mdss_dsi_auo_400p_cmd", "dsi:0:", "qcom,mdss-dsi-panel",
-	10, 1, "DISPLAY_1", 0, 0, 60, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ""
+	10, 1, "DISPLAY_1", 0, 0, 60, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL
 };
 
 /*---------------------------------------------------------------------------*/
diff --git a/dev/gcdb/display/include/panel_auo_cx_qvga_cmd.h b/dev/gcdb/display/include/panel_auo_cx_qvga_cmd.h
index b0ac9c3..caaf084 100644
--- a/dev/gcdb/display/include/panel_auo_cx_qvga_cmd.h
+++ b/dev/gcdb/display/include/panel_auo_cx_qvga_cmd.h
@@ -1,4 +1,4 @@
-/* Copyright (c) 2015, 2017, The Linux Foundation. All rights reserved.
+/* Copyright (c) 2015, 2018, 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
@@ -39,7 +39,7 @@
 /*---------------------------------------------------------------------------*/
 static struct panel_config auo_cx_qvga_cmd_panel_data = {
 	"qcom,mdss_dsi_auo_cx_qvga_cmd", "dsi:0:", "qcom,mdss-dsi-panel",
-	10, 1, "DISPLAY_1", 0, 0, 60, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ""
+	10, 1, "DISPLAY_1", 0, 0, 60, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL
 };
 
 /*---------------------------------------------------------------------------*/
diff --git a/dev/gcdb/display/include/panel_hx8399c_hd_plus_video.h b/dev/gcdb/display/include/panel_hx8399c_hd_plus_video.h
index 679c7d5..4a32b0f 100644
--- a/dev/gcdb/display/include/panel_hx8399c_hd_plus_video.h
+++ b/dev/gcdb/display/include/panel_hx8399c_hd_plus_video.h
@@ -47,7 +47,7 @@
 /* Panel resolution                                                          */
 /*---------------------------------------------------------------------------*/
 static struct panel_resolution hx8399c_hd_plus_video_panel_res = {
-	720, 1440, 48, 48, 16, 0, 60, 40, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0
+	720, 1440, 48, 48, 16, 0, 36, 40, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0
 };
 
 /*---------------------------------------------------------------------------*/
diff --git a/dev/gcdb/display/include/panel_truly_1080p_cmd.h b/dev/gcdb/display/include/panel_truly_1080p_cmd.h
index 16b8e3a..428417f 100644
--- a/dev/gcdb/display/include/panel_truly_1080p_cmd.h
+++ b/dev/gcdb/display/include/panel_truly_1080p_cmd.h
@@ -1,4 +1,4 @@
-/* Copyright (c) 2015-2016, The Linux Foundation. All rights reserved.
+/* Copyright (c) 2015-2016, 2018 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
@@ -323,6 +323,14 @@
 	0x23, 0x18, 0x7, 0x8, 0x5, 0x3, 0x4, 0xa0,
 };
 
+static const uint32_t truly_1080p_cmd_12nm_timings[] = {
+  0x17, 0x0A, 0x0F, 0x06, 0x02, 0x08, 0x06, 0x0E
+};
+
+struct labibb_desc truly_1080p_cmd_labibb = {
+  0, 0, 5700000, 5700000, 5700000, 5700000, 3, 3, 1, 0
+};
+
 static struct panel_reset_sequence truly_1080p_cmd_panel_reset_seq = {
 	{ 1, 0, 1, }, { 200, 200, 200, }, 2
 };
diff --git a/dev/gcdb/display/include/panel_truly_1080p_video.h b/dev/gcdb/display/include/panel_truly_1080p_video.h
index d535f68..f92d655 100755
--- a/dev/gcdb/display/include/panel_truly_1080p_video.h
+++ b/dev/gcdb/display/include/panel_truly_1080p_video.h
@@ -1,4 +1,4 @@
-/* Copyright (c) 2015-2016, The Linux Foundation. All rights reserved.
+/* Copyright (c) 2015-2016, 2018 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
@@ -282,6 +282,10 @@
   0xE6, 0x38, 0x26, 0x00, 0x68, 0x6e, 0x2A, 0x3c, 0x44, 0x03, 0x04, 0x00
 };
 
+static const uint32_t truly_1080p_video_12nm_timings[] = {
+  0x17, 0x0A, 0x0F, 0x06, 0x02, 0x08, 0x06, 0x0E
+};
+
 static const uint32_t truly_1080p_14nm_video_timings[] = {
 	0x23, 0x1e, 0x8, 0x9, 0x5, 0x3, 0x4, 0xa0,
 	0x23, 0x1e, 0x8, 0x9, 0x5, 0x3, 0x4, 0xa0,
@@ -294,6 +298,10 @@
   0, 4, 0x02, 0x2d
 };
 
+struct labibb_desc truly_1080p_video_labibb = {
+  0, 0, 5700000, 5700000, 5700000, 5700000, 3, 3, 1, 0
+};
+
 static struct panel_reset_sequence truly_1080p_video_panel_reset_seq = {
 { 1, 0, 1, }, { 200, 200, 200, }, 2
 };
diff --git a/platform/msm_shared/partition_parser.c b/platform/msm_shared/partition_parser.c
old mode 100644
new mode 100755
index 3752ba8..7874adb
--- a/platform/msm_shared/partition_parser.c
+++ b/platform/msm_shared/partition_parser.c
@@ -1,4 +1,4 @@
-/* Copyright (c) 2011-2017, The Linux Foundation. All rights reserved.
+/* Copyright (c) 2011-2018, 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
@@ -83,6 +83,8 @@
 struct partition_entry *partition_entries;
 static unsigned gpt_partitions_exist = 0;
 static unsigned partition_count;
+/* this is a pointer to ptn_entries_buffer */
+static unsigned char *new_buffer = NULL;
 
 unsigned partition_get_partition_count()
 {
@@ -273,10 +275,12 @@
 	unsigned int n = 0;	/* Counter for UTF-16 -> 8 conversion */
 	unsigned char UTF16_name[MAX_GPT_NAME_SIZE];
 	/* LBA of first partition -- 1 Block after Protected MBR + 1 for PT */
-	unsigned long long partition_0;
 	uint64_t device_density;
 	uint8_t *data = NULL;
+	uint8_t *data_org_ptr = NULL;
 	uint32_t part_entry_cnt = block_size / ENTRY_SIZE;
+	uint32_t blocks_for_entries =
+			(NUM_PARTITIONS * PARTITION_ENTRY_SIZE)/block_size;
 
 	/* Get the density of the mmc device */
 
@@ -289,6 +293,7 @@
 		ret = -1;
 		goto end;
 	}
+	data_org_ptr = data;
 
 	/* Print out the GPT first */
 	ret = mmc_read(block_size, (unsigned int *)data, block_size);
@@ -330,19 +335,11 @@
 		}
 		parse_secondary_gpt = 0;
 	}
-	partition_0 = GET_LLWORD_FROM_BYTE(&data[PARTITION_ENTRIES_OFFSET]);
 	/* Read GPT Entries */
 	for (i = 0; i < (ROUNDUP(max_partition_count, part_entry_cnt)) / part_entry_cnt; i++) {
 		ASSERT(partition_count < NUM_PARTITIONS);
-		ret = mmc_read((partition_0 * block_size) + (i * block_size),
-						(uint32_t *) data, block_size);
-
-		if (ret) {
-			dprintf(CRITICAL,
-				"GPT: mmc read card failed reading partition entries.\n");
-			goto end;
-		}
-
+		
+		data = (new_buffer + (i * block_size));
 		for (j = 0; j < part_entry_cnt; j++) {
 			memcpy(&(partition_entries[partition_count].type_guid),
 			       &data[(j * partition_entry_size)],
@@ -368,13 +365,28 @@
 			    GET_LLWORD_FROM_BYTE(&data
 						 [(j * partition_entry_size) +
 						  LAST_LBA_OFFSET]);
+
+			/* If partition entry LBA is not valid, skip this entry
+				and parse next entry */
+			if (partition_entries[partition_count].first_lba < first_usable_lba
+				|| partition_entries[partition_count].last_lba >
+						(device_density/block_size -
+						(blocks_for_entries + GPT_HEADER_BLOCKS + 1))
+				|| partition_entries[partition_count].first_lba >
+					partition_entries[partition_count].last_lba)
+			{
+				dprintf(CRITICAL, "Partition entry(%d), lba not valid\n", j);
+				partition_count++;
+				continue;
+			}
+
 			partition_entries[partition_count].size =
 			    partition_entries[partition_count].last_lba -
 			    partition_entries[partition_count].first_lba + 1;
 			partition_entries[partition_count].attribute_flag =
 			    GET_LLWORD_FROM_BYTE(&data
 						 [(j * partition_entry_size) +
-						  ATTRIBUTE_FLAG_OFFSET]);
+				 		  ATTRIBUTE_FLAG_OFFSET]);
 
 			memset(&UTF16_name, 0x00, MAX_GPT_NAME_SIZE);
 			memcpy(UTF16_name, &data[(j * partition_entry_size) +
@@ -394,8 +406,10 @@
 		}
 	}
 end:
-	if (data)
-		free(data);
+	if (data_org_ptr)
+		free(data_org_ptr);
+	if (new_buffer)
+		free(new_buffer);
 
 	return ret;
 }
@@ -589,6 +603,8 @@
 	unsigned int partition_entry_array_start;
 	unsigned char *primary_gpt_header;
 	unsigned char *secondary_gpt_header;
+	//define as 64 bit unsigned int
+	unsigned long long *last_partition_entry;
 	unsigned int offset;
 	unsigned long long card_size_sec;
 	int total_part = 0;
@@ -624,9 +640,14 @@
 					(ptn_entries_blocks + GPT_HEADER_BLOCKS))));
 
 	/* Find last partition */
-	while (*(primary_gpt_header + block_size + total_part * ENTRY_SIZE) !=
-	       0) {
+	last_partition_entry = (unsigned long long *)
+		(primary_gpt_header + block_size + total_part * ENTRY_SIZE);
+	//need check 128 bit for GUID
+	while (*last_partition_entry != 0 ||
+		*(last_partition_entry + 1) != 0 ) {
 		total_part++;
+		last_partition_entry = (unsigned long long *)
+			(primary_gpt_header + block_size + total_part * ENTRY_SIZE);
 	}
 
 	/* Patching last partition */
@@ -1163,11 +1184,12 @@
 	uint32_t ret = 0;
 	uint32_t partitions_for_block = 0;
 	uint32_t blocks_to_read = 0;
-	unsigned char *new_buffer = NULL;
 	unsigned long long last_usable_lba = 0;
 	unsigned long long partition_0 = 0;
 	unsigned long long current_lba = 0;
 	uint32_t block_size = mmc_get_device_blocksize();
+	uint32_t blocks_for_entries =
+			(NUM_PARTITIONS * PARTITION_ENTRY_SIZE)/ block_size;
 	/* Get the density of the mmc device */
 	uint64_t device_density = mmc_get_device_capacity();
 
@@ -1215,10 +1237,19 @@
 	/*current lba and GPT lba should be same*/
 	if (!parse_secondary_gpt) {
 		if (current_lba != GPT_LBA) {
-			dprintf(CRITICAL,"GPT first usable LBA mismatch\n");
+			dprintf(CRITICAL,"Primary GPT first usable LBA mismatch\n");
 			return 1;
 		}
 	}
+	else
+	{
+		if (current_lba != ((device_density/block_size) - 1))
+		{
+			dprintf(CRITICAL,"Secondary GPT first usable LBA mismatch\n");
+			return 1;
+		}
+	}
+
 	/*check for first lba should be with in the valid range*/
 	if (*first_usable_lba > (device_density/block_size)) {
 		dprintf(CRITICAL,"Invalid first_usable_lba\n");
@@ -1258,11 +1289,24 @@
 	if (!flashing_gpt) {
 		partition_0 = GET_LLWORD_FROM_BYTE(&buffer[PARTITION_ENTRIES_OFFSET]);
 		/*start LBA should always be 2 in primary GPT*/
-		if(partition_0 != 0x2 && !parse_secondary_gpt) {
-			dprintf(CRITICAL, "Starting LBA mismatch\n");
-			ret = 1;
-			goto fail;
-
+		if (!parse_secondary_gpt)
+		{
+			if (partition_0 != 0x2)
+			{
+				dprintf(CRITICAL, "PrimaryGPT starting LBA mismatch\n");
+				ret = 1;
+				goto fail;
+			}
+		}
+		else
+		{
+			if (partition_0 != ((device_density/block_size) -
+						(blocks_for_entries + GPT_HEADER_BLOCKS)))
+			{
+				dprintf(CRITICAL, "BackupGPT starting LBA mismatch\n");
+				ret = 1;
+				goto fail;
+			}
 		}
 		/*read the partition entries to new_buffer*/
 		ret = mmc_read((partition_0) * (block_size), (unsigned int *)new_buffer, (blocks_to_read * block_size));
@@ -1278,6 +1322,7 @@
 			dprintf(CRITICAL,"Partition entires crc mismatch crc_val= %u with crc_val_org= %u\n",crc_val,crc_val_org);
 			ret = 1;
 		}
+		return ret;
 	}
 fail:
 	free(new_buffer);
diff --git a/platform/msm_shared/qpic_nand.c b/platform/msm_shared/qpic_nand.c
index 146f809..7fe25f9 100644
--- a/platform/msm_shared/qpic_nand.c
+++ b/platform/msm_shared/qpic_nand.c
@@ -885,6 +885,10 @@
 		 * Bad block value is stored in the first page of the block.
 		 */
 		/* Read the first page in the block. */
+		/* Ensure we always read first page of block */
+		if (page & flash.num_pages_per_blk_mask)
+			page = page - (page & flash.num_pages_per_blk_mask);
+
 		cwperpage = flash.cws_per_page;
 
 		/* Read page cmd */
diff --git a/target/init.c b/target/init.c
index 72ae157..2358438 100644
--- a/target/init.c
+++ b/target/init.c
@@ -347,6 +347,7 @@
 	switch(platform)
 	{
 		case APQ8053:
+		case APQ8009:
 			return verified_boot_le;
 		default:
 			break;
diff --git a/target/msm8909/init.c b/target/msm8909/init.c
index 7ebf174..2558067 100644
--- a/target/msm8909/init.c
+++ b/target/msm8909/init.c
@@ -870,7 +870,8 @@
 uint32_t target_get_pmic()
 {
 	if ((board_hardware_subtype() == HW_PLATFORM_SUBTYPE_8909_PM660) ||
-		(board_hardware_subtype() == HW_PLATFORM_SUBTYPE_8909_PM660_V1))
+		(board_hardware_subtype() == HW_PLATFORM_SUBTYPE_8909_PM660_V1) ||
+		(board_hardware_subtype() == HW_PLATFORM_SUBTYPE_8909_COMPAL_ALPHA))
 		return PMIC_IS_PM660;
 	else
 		return PMIC_IS_PM8909;
diff --git a/target/msm8952/oem_panel.c b/target/msm8952/oem_panel.c
index 7c869e1..b0a1536 100644
--- a/target/msm8952/oem_panel.c
+++ b/target/msm8952/oem_panel.c
@@ -176,7 +176,13 @@
 	switch (panel_id) {
 	case TRULY_1080P_VIDEO_PANEL:
 		panelstruct->paneldata    = &truly_1080p_video_panel_data;
-		panelstruct->paneldata->panel_with_enable_gpio = 1;
+		panelstruct->backlightinfo = &truly_1080p_video_backlight;
+		if (platform_is_sdm439() || platform_is_sdm429()) {
+			panelstruct->paneldata->panel_with_enable_gpio = 0;
+			panelstruct->backlightinfo->bl_interface_type = 0;
+		} else {
+			panelstruct->paneldata->panel_with_enable_gpio = 1;
+		}
 		panelstruct->panelres     = &truly_1080p_video_panel_res;
 		panelstruct->color        = &truly_1080p_video_color;
 		panelstruct->videopanel   = &truly_1080p_video_video_panel;
@@ -187,7 +193,7 @@
 			= &truly_1080p_video_timing_info;
 		panelstruct->panelresetseq
 					 = &truly_1080p_video_panel_reset_seq;
-		panelstruct->backlightinfo = &truly_1080p_video_backlight;
+		pinfo->labibb = &truly_1080p_video_labibb;
 		pinfo->mipi.panel_on_cmds
 			= truly_1080p_video_on_command;
 		pinfo->mipi.num_of_panel_on_cmds
@@ -196,13 +202,24 @@
 			= truly_1080p_video_off_command;
 		pinfo->mipi.num_of_panel_off_cmds
 			= TRULY_1080P_VIDEO_OFF_COMMAND;
-		memcpy(phy_db->timing,
-			truly_1080p_video_timings, TIMING_SIZE);
+		if (phy_db->pll_type == DSI_PLL_TYPE_12NM)
+			memcpy(phy_db->timing,
+				truly_1080p_video_12nm_timings,
+				TIMING_SIZE_12NM);
+		else
+			memcpy(phy_db->timing,
+				truly_1080p_video_timings, TIMING_SIZE);
 		pinfo->mipi.signature 	= TRULY_1080P_VIDEO_SIGNATURE;
 		break;
 	case TRULY_1080P_CMD_PANEL:
 		panelstruct->paneldata    = &truly_1080p_cmd_panel_data;
-		panelstruct->paneldata->panel_with_enable_gpio = 1;
+		panelstruct->backlightinfo = &truly_1080p_cmd_backlight;
+		if (platform_is_sdm439() || platform_is_sdm429()) {
+			panelstruct->paneldata->panel_with_enable_gpio = 0;
+			panelstruct->backlightinfo->bl_interface_type = 0;
+		} else {
+			panelstruct->paneldata->panel_with_enable_gpio = 1;
+		}
 		panelstruct->panelres     = &truly_1080p_cmd_panel_res;
 		panelstruct->color        = &truly_1080p_cmd_color;
 		panelstruct->videopanel   = &truly_1080p_cmd_video_panel;
@@ -213,7 +230,7 @@
 			= &truly_1080p_cmd_timing_info;
 		panelstruct->panelresetseq
 					 = &truly_1080p_cmd_panel_reset_seq;
-		panelstruct->backlightinfo = &truly_1080p_cmd_backlight;
+		pinfo->labibb = &truly_1080p_cmd_labibb;
 		pinfo->mipi.panel_on_cmds
 			= truly_1080p_cmd_on_command;
 		pinfo->mipi.num_of_panel_on_cmds
@@ -222,8 +239,13 @@
 			= truly_1080p_cmd_off_command;
 		pinfo->mipi.num_of_panel_off_cmds
 			= TRULY_1080P_CMD_OFF_COMMAND;
-		memcpy(phy_db->timing,
-			truly_1080p_cmd_timings, TIMING_SIZE);
+		if (phy_db->pll_type == DSI_PLL_TYPE_12NM)
+			memcpy(phy_db->timing,
+				truly_1080p_cmd_12nm_timings,
+				TIMING_SIZE_12NM);
+		else
+			memcpy(phy_db->timing,
+				truly_1080p_cmd_timings, TIMING_SIZE);
 		pinfo->mipi.signature 	= TRULY_1080P_CMD_SIGNATURE;
 		break;
 	case OTM1906C_1080P_CMD_PANEL:
@@ -585,7 +607,9 @@
 		memcpy(phy_db->timing,
 						hx8394f_720p_video_timings, TIMING_SIZE);
 		pinfo->mipi.signature = HX8394F_720P_VIDEO_SIGNATURE;
-		pinfo->disable_wled_labibb = true;
+		if ((board_platform_id() == SDM429) || (board_platform_id() == SDM439) ||
+				(board_platform_id() == SDA429) || (board_platform_id() == SDA439))
+			pinfo->disable_wled_labibb = true;
 		break;
 	case BYD_1200P_VIDEO_PANEL:
 		panelstruct->paneldata    = &byd_1200p_video_panel_data;
diff --git a/target/msm8953/oem_panel.c b/target/msm8953/oem_panel.c
index b1eac2c..4de8664 100644
--- a/target/msm8953/oem_panel.c
+++ b/target/msm8953/oem_panel.c
@@ -257,6 +257,12 @@
 	case HX8399C_FHD_PLUSE_VIDEO_PANEL:
 		panelstruct->paneldata    = &hx8399c_fhd_pluse_video_panel_data;
 		panelstruct->panelres     = &hx8399c_fhd_pluse_video_panel_res;
+		panelstruct->panelres->hfront_porch = 52;
+		panelstruct->panelres->hback_porch = 48;
+		panelstruct->panelres->hpulse_width = 8;
+		panelstruct->panelres->vfront_porch = 8;
+		panelstruct->panelres->vback_porch = 8;
+		panelstruct->panelres->vpulse_width = 4;
 		panelstruct->color        = &hx8399c_fhd_pluse_video_color;
 		panelstruct->videopanel   =
 				&hx8399c_fhd_pluse_video_video_panel;