Merge "platform : msm_shared : Modified mmc_page_size() function"
diff --git a/app/aboot/aboot.c b/app/aboot/aboot.c
index 5d522f8..690e606 100644
--- a/app/aboot/aboot.c
+++ b/app/aboot/aboot.c
@@ -4683,6 +4683,13 @@
 		return -1;
 	if (header->width == 0 || header->height == 0)
 		return -1;
+	if ((UINT_MAX/512 >= header->blocks) && (header->blocks != 0)){
+		if (header->blocks*512 < header->width * header->height)
+			return -1;
+	}
+	else {
+		return -1;
+	}
 	return 0;
 }
 
@@ -4717,10 +4724,9 @@
 
 	fb_display = fbcon_display();
 	if (fb_display) {
-		if (header->type && (header->blocks != 0) &&
-				(UINT_MAX >= header->blocks * 512) &&
-				((header->blocks * 512) <=  (fb_display->width *
-				fb_display->height * (fb_display->bpp / 8)))) {
+		if (header->type &&
+		     ((header->blocks * 512) <=  (fb_display->width *
+			fb_display->height * (fb_display->bpp / 8)))) {
 					/* RLE24 compressed data */
 			uint8_t *base = (uint8_t *) fb_display->base + LOGO_IMG_OFFSET;
 
@@ -4818,8 +4824,8 @@
 	}
 
 	if (fb_display) {
-		if (header->type && (header->blocks != 0) &&
-			(UINT_MAX >= header->blocks * 512 + LOGO_IMG_HEADER_SIZE) &&
+		if (header->type &&
+			(((UINT_MAX - LOGO_IMG_HEADER_SIZE) / 512) >= header->blocks) &&
 			((header->blocks * 512) <=  (fb_display->width *
 			fb_display->height * (fb_display->bpp / 8)))) {
 			/* 1 RLE24 compressed data */
diff --git a/dev/gcdb/display/include/panel_gc9305_qvga_spi_cmd.h b/dev/gcdb/display/include/panel_gc9305_qvga_spi_cmd.h
index 01f5839..0498afc 100644
--- a/dev/gcdb/display/include/panel_gc9305_qvga_spi_cmd.h
+++ b/dev/gcdb/display/include/panel_gc9305_qvga_spi_cmd.h
@@ -267,6 +267,11 @@
 	1, 1, 4095, 100, 1, "PMIC_8941"
 };
 
-#define GC9305_QVGA_CMD_SIGNATURE 0xFFFF
+static uint8_t gc9305_signature_addr = 0x04;
 
+static uint8_t gc9305_signature_len = 3;
+
+static uint8_t gc9305_signature[] = {
+	0x00, 0x93, 0x05
+};
 #endif /* PANEL_GC9305_QVGA_SPI_CMD_H */
diff --git a/platform/msm_shared/display.c b/platform/msm_shared/display.c
index 780ecfc..2217d99 100644
--- a/platform/msm_shared/display.c
+++ b/platform/msm_shared/display.c
@@ -36,6 +36,7 @@
 #include <malloc.h>
 #include <qpic.h>
 #include <target.h>
+#include "mdss_spi.h"
 #ifdef DISPLAY_TYPE_MDSS
 #include <target/display.h>
 #endif
@@ -130,6 +131,11 @@
 		ret = mdss_spi_init();
 		if (ret)
 			goto msm_display_config_out;
+		if (target_panel_auto_detect_enabled()) {
+			ret = spi_check_panel_id(pinfo);
+			if (ret)
+				goto msm_display_config_out;
+		}
 		ret = mdss_spi_panel_init(pinfo);
 		if (ret)
 			goto msm_display_config_out;
diff --git a/platform/msm_shared/include/mdss_spi.h b/platform/msm_shared/include/mdss_spi.h
new file mode 100644
index 0000000..87f062b
--- /dev/null
+++ b/platform/msm_shared/include/mdss_spi.h
@@ -0,0 +1,39 @@
+/* Copyright (c) 2020, 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
+ * met:
+ *     * Redistributions of source code must retain the above copyright
+ *       notice, this list of conditions and the following disclaimer.
+ *     * Redistributions in binary form must reproduce the above
+ *       copyright notice, this list of conditions and the following
+ *       disclaimer in the documentation and/or other materials provided
+ *       with the distribution.
+ *     * Neither the name of The Linux Foundation nor the names of its
+ *       contributors may be used to endorse or promote products derived
+ *       from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+ * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+ * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
+ * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#include <msm_panel.h>
+
+#ifdef DISPLAY_TYPE_SPI_SUPPORT
+int spi_check_panel_id(struct msm_panel_info *pinfo);
+#else
+static inline int spi_check_panel_id(struct msm_panel_info *pinfo)
+{
+	return 0;
+}
+#endif
diff --git a/platform/msm_shared/include/msm_panel.h b/platform/msm_shared/include/msm_panel.h
index 3b1d3df..134c6a3 100644
--- a/platform/msm_shared/include/msm_panel.h
+++ b/platform/msm_shared/include/msm_panel.h
@@ -105,7 +105,7 @@
 	struct mdss_spi_cmd *panel_cmds;
 	uint8_t *signature_addr;
 	uint8_t *signature;
-	uint8_t *signature_len;
+	uint8_t signature_len;
 };
 
 struct hdmi_panel_info {
diff --git a/platform/msm_shared/mdss_spi.c b/platform/msm_shared/mdss_spi.c
index 386a56b..4c5fd98 100644
--- a/platform/msm_shared/mdss_spi.c
+++ b/platform/msm_shared/mdss_spi.c
@@ -33,9 +33,10 @@
 #include <platform/gpio.h>
 #include <dev/gpio.h>
 #include <platform/timer.h>
+#include "mdss_spi.h"
 
-#define SUCCESS		0
-#define FAIL		1
+#define SUCCESS           0
+#define FAIL              1
 
 static struct qup_spi_dev *dev = NULL;
 
@@ -107,7 +108,7 @@
 	return ret;
 }
 
-static void spi_read_panel_data(unsigned char *buf,  int len)
+void spi_read_panel_data(unsigned char *buf,  int len)
 {
 	int ret = 0;
 
@@ -128,6 +129,35 @@
 	return;
 }
 
+int spi_check_panel_id(struct msm_panel_info *pinfo)
+{
+	int i = 0;
+	int len;
+	int ret = SUCCESS;
+	unsigned char *buf;
+
+	if (!pinfo->spi.signature || !pinfo->spi.signature_len)
+		return ret;
+
+	len = pinfo->spi.signature_len;
+	buf = (unsigned char*) malloc(len + 1);
+
+	mdss_spi_write_cmd(pinfo->spi.signature_addr);
+	spi_read_panel_data(buf, len + 1);
+
+	for (i = 0; i < len; i++) {
+		/* left shift a bit to match SPI panel timming */
+		if(pinfo->spi.signature[i] !=
+			 (((buf[i] << 1) | (buf[i + 1] >> 7)) & 0xFF)) {
+			ret = FAIL;
+			break;
+		}
+	}
+
+	free(buf);
+	return ret;
+}
+
 int mdss_spi_init(void)
 {
 	if (!dev) {
@@ -139,9 +169,10 @@
 	}
 
 	gpio_tlmm_config(spi_dc_gpio.pin_id, 0, spi_dc_gpio.pin_direction,
-			spi_dc_gpio.pin_pull, spi_dc_gpio.pin_strength,
-			spi_dc_gpio.pin_state);
+				spi_dc_gpio.pin_pull, spi_dc_gpio.pin_strength,
+				spi_dc_gpio.pin_state);
 	return SUCCESS;
+
 }
 
 int mdss_spi_panel_init(struct msm_panel_info *pinfo)
@@ -149,6 +180,7 @@
 	int cmd_count = 0;
 
 	while (cmd_count < pinfo->spi.num_of_panel_cmds) {
+
 		if (pinfo->spi.panel_cmds[cmd_count].cmds_post_tg){
 			cmd_count ++;
 			continue;
@@ -165,6 +197,7 @@
 
 		cmd_count ++;
 	}
+
 	return SUCCESS;
 }
 
@@ -192,7 +225,7 @@
 	}
 
 	while (cmd_count < pinfo->spi.num_of_panel_cmds) {
-		if (pinfo->spi.panel_cmds[cmd_count].cmds_post_tg){
+		if (pinfo->spi.panel_cmds[cmd_count].cmds_post_tg) {
 			payload = pinfo->spi.panel_cmds[cmd_count].payload;
 			mdss_spi_write_cmd((unsigned char *) payload);
 			if (pinfo->spi.panel_cmds[cmd_count].size > 1)
diff --git a/target/msm8909/include/target/display.h b/target/msm8909/include/target/display.h
index b778cf2..892e060 100644
--- a/target/msm8909/include/target/display.h
+++ b/target/msm8909/include/target/display.h
@@ -66,7 +66,7 @@
 };
 
 static struct gpio_pin spi_dc_gpio = {
-        "msmgpio", 59, 3, 1, 0, 1
+        "msmgpio", 110, 3, 1, 0, 1
 };
 
 static struct gpio_pin te_gpio = {
@@ -121,6 +121,6 @@
 #define MIPI_VSYNC_FRONT_PORCH_LINES 9
 
 #define SPI_BLSP_ID                  1
-#define SPI_QUP_ID                   3
+#define SPI_QUP_ID                   4
 
 #endif
diff --git a/target/msm8909/init.c b/target/msm8909/init.c
index 72c6cc4..c3ed780 100644
--- a/target/msm8909/init.c
+++ b/target/msm8909/init.c
@@ -500,8 +500,21 @@
 uint8_t target_panel_auto_detect_enabled()
 {
 	uint8_t ret = 0;
+	uint32_t platform = board_platform_id();
+	uint32_t hw_id = board_hardware_id();
+	uint32_t target_id = board_target_id();
+	uint32_t plat_hw_ver_major = ((target_id >> 16) & 0xFF);
 
-	switch(board_hardware_id()) {
+	switch(platform) {
+	case MSM8905:
+		switch(hw_id) {
+		case HW_PLATFORM_QRD:
+			if(plat_hw_ver_major > 0x10 && plat_hw_ver_major < 0x13)
+				ret = 1;
+			break;
+		default:
+			break;
+		}
 	default:
 		ret = 0;
 		break;
diff --git a/target/msm8909/oem_panel.c b/target/msm8909/oem_panel.c
index 83fd4fd..b624c80 100644
--- a/target/msm8909/oem_panel.c
+++ b/target/msm8909/oem_panel.c
@@ -52,7 +52,7 @@
 #include "include/panel_st7789v2_qvga_spi_cmd.h"
 #include "include/panel_gc9305_qvga_spi_cmd.h"
 
-#define DISPLAY_MAX_PANEL_DETECTION 0
+#define DISPLAY_MAX_PANEL_DETECTION 2
 #define ILI9806E_FWVGA_VIDEO_PANEL_POST_INIT_DELAY 68
 
 enum {
@@ -443,7 +443,7 @@
 		panelstruct->panelresetseq	= &st7789v2_qvga_cmd_reset_seq;
 		panelstruct->backlightinfo	= &st7789v2_qvga_cmd_backlight;
 		pinfo->spi.panel_cmds		= st7789v2_qvga_cmd_on_command;
-		pinfo->spi.num_of_panel_cmds= ST7789v2_QVGA_CMD_ON_COMMAND;
+		pinfo->spi.num_of_panel_cmds    = ST7789v2_QVGA_CMD_ON_COMMAND;
 		pinfo->spi.signature_addr	= &st7789v2_signature_addr;
 		pinfo->spi.signature		= st7789v2_signature;
 		pinfo->spi.signature_len	= st7789v2_signature_len;
@@ -460,6 +460,12 @@
 					= gc9305_qvga_cmd_on_command;
 		pinfo->spi.num_of_panel_cmds
 					= GC9305_QVGA_CMD_ON_COMMAND;
+		pinfo->spi.signature_addr
+					= &gc9305_signature_addr;
+		pinfo->spi.signature
+					= gc9305_signature;
+		pinfo->spi.signature_len
+					= gc9305_signature_len;
 		pan_type = PANEL_TYPE_SPI;
 		break;
 	case UNKNOWN_PANEL:
@@ -492,6 +498,8 @@
 	uint32_t platform_type = board_platform_id();
 	uint32_t platform_subtype = board_hardware_subtype();
 	int32_t panel_override_id;
+	uint32_t target_id = board_target_id();
+	uint32_t plat_hw_ver_major = ((target_id >> 16) & 0xFF);;
 
 	if (panel_name) {
 		panel_override_id = panel_name_to_id(supp_panels,
@@ -541,8 +549,24 @@
 	case HW_PLATFORM_QRD:
 		switch (platform_subtype) {
 			case QRD_SKUA:
-				if (MSM8905 == board_platform_id())
-					panel_id = GC9305_QVGA_SPI_CMD_PANEL;
+				if (MSM8905 == board_platform_id()) {
+					if (target_panel_auto_detect_enabled()) {
+						/* QRD8905 Nand SKU */
+						switch (auto_pan_loop) {
+							case 0:
+								panel_id = ST7789v2_QVGA_SPI_CMD_PANEL;
+								break;
+							case 1:
+								panel_id = GC9305_QVGA_SPI_CMD_PANEL;
+								break;
+							default:
+								panel_id = ST7789v2_QVGA_SPI_CMD_PANEL;
+								break;
+						}
+					} else
+						panel_id = GC9305_QVGA_SPI_CMD_PANEL;
+					auto_pan_loop++;
+				}
 				else
 					panel_id = HX8379A_FWVGA_SKUA_VIDEO_PANEL;
 				break;
diff --git a/target/msm8952/rules.mk b/target/msm8952/rules.mk
index ccc7e92..97408f9 100644
--- a/target/msm8952/rules.mk
+++ b/target/msm8952/rules.mk
@@ -20,6 +20,7 @@
 
 DEFINES += DISPLAY_TYPE_MIPI=1
 DEFINES += DISPLAY_TYPE_DSI6G=1
+DEFINES += DISPLAY_TYPE_SPI_SUPPORT=1
 
 DEFINES += PMI_CONFIGURED=1