From de1ae4c44093c7f62af58caf3bdba83bb9531295 Mon Sep 17 00:00:00 2001
From: Patil, Rachna <rachna@ti.com>
Date: Fri, 11 Nov 2011 18:03:27 +0530
Subject: [PATCH 1/9] AM335x: Add support for TSC on Beta GP EVM.

AN1 and AN2(analog inputs to analog front end) were swapped
on alpha EVM's. This change is IP dependent, hence changes are
made in the driver to support the beta EVM.

Signed-off-by: Patil, Rachna <rachna@ti.com>
---
 arch/arm/mach-omap2/board-am335xevm.c |    8 +++
 drivers/input/touchscreen/ti_tscadc.c |   83 ++++++++++++++++++++++-----------
 include/linux/input/ti_tscadc.h       |   10 ++++
 3 files changed, 73 insertions(+), 28 deletions(-)

diff --git a/arch/arm/mach-omap2/board-am335xevm.c b/arch/arm/mach-omap2/board-am335xevm.c
index b27fb97..7f52de1 100644
--- a/arch/arm/mach-omap2/board-am335xevm.c
+++ b/arch/arm/mach-omap2/board-am335xevm.c
@@ -920,6 +920,14 @@ static void dvi_init(int evm_id, int profile)
 static void tsc_init(int evm_id, int profile)
 {
 	int err;
+
+	if (gp_evm_revision == GP_EVM_REV_IS_1_1A) {
+		am335x_touchscreen_data.analog_input = 1;
+		pr_info("TSC connected to beta GP EVM\n");
+	} else {
+		am335x_touchscreen_data.analog_input = 0;
+		pr_info("TSC connected to alpha GP EVM\n");
+	}
 	setup_pin_mux(tsc_pin_mux);
 	err = platform_device_register(&tsc_device);
 	if (err)
diff --git a/drivers/input/touchscreen/ti_tscadc.c b/drivers/input/touchscreen/ti_tscadc.c
index 1f4db8d..021db7f 100644
--- a/drivers/input/touchscreen/ti_tscadc.c
+++ b/drivers/input/touchscreen/ti_tscadc.c
@@ -65,13 +65,16 @@
 #define TSCADC_STEPCONFIG_RFP		(1 << 12)
 #define TSCADC_STEPCONFIG_INM		(1 << 18)
 #define TSCADC_STEPCONFIG_INP_4		(1 << 19)
+#define TSCADC_STEPCONFIG_INP		(1 << 20)
 #define TSCADC_STEPCONFIG_INP_5		(1 << 21)
 #define TSCADC_STEPCONFIG_FIFO1		(1 << 26)
 #define TSCADC_STEPCONFIG_IDLE_INP	(1 << 22)
 #define TSCADC_STEPCONFIG_OPENDLY	0x018
 #define TSCADC_STEPCONFIG_SAMPLEDLY	0x88
-#define TSCADC_STEPCHARGE_INM		BIT(16)
-#define TSCADC_STEPCHARGE_INP		BIT(20)
+#define TSCADC_STEPCHARGE_INM_SWAP     BIT(16)
+#define TSCADC_STEPCHARGE_INM          BIT(15)
+#define TSCADC_STEPCHARGE_INP_SWAP     BIT(20)
+#define TSCADC_STEPCHARGE_INP          BIT(19)
 #define TSCADC_STEPCHARGE_RFM		(1 << 23)
 #define TSCADC_STEPCHARGE_DELAY		0x1
 #define TSCADC_CNTRLREG_TSCSSENB	BIT(0)
@@ -94,6 +97,7 @@ unsigned int bckup_x = 0, bckup_y = 0;
 struct tscadc {
 	struct input_dev	*input;
 	int			wires;
+	int			analog_input;
 	struct clk		*clk;
 	int			irq;
 	void __iomem		*tsc_base;
@@ -121,21 +125,37 @@ static void tsc_step_config(struct tscadc *ts_dev)
 	delay = TSCADC_STEPCONFIG_SAMPLEDLY | TSCADC_STEPCONFIG_OPENDLY;
 
 	stepconfigx = TSCADC_STEPCONFIG_MODE_HWSYNC |
-			TSCADC_STEPCONFIG_2SAMPLES_AVG | TSCADC_STEPCONFIG_XPP |
-			TSCADC_STEPCONFIG_YPN;
+			TSCADC_STEPCONFIG_2SAMPLES_AVG | TSCADC_STEPCONFIG_XPP;
+
 	switch (ts_dev->wires) {
 	case 4:
-		stepconfigx |= TSCADC_STEPCONFIG_INP_4;
+		if (ts_dev->analog_input == 0)
+			stepconfigx |= TSCADC_STEPCONFIG_INP_4 |
+				TSCADC_STEPCONFIG_YPN;
+		else
+			stepconfigx |= TSCADC_STEPCONFIG_INP |
+				TSCADC_STEPCONFIG_XNN;
 		break;
 	case 5:
-		stepconfigx |= TSCADC_STEPCONFIG_YPP |
-				TSCADC_STEPCONFIG_YNN |
+		stepconfigx |= TSCADC_STEPCONFIG_YNN |
 				TSCADC_STEPCONFIG_INP_5;
+		if (ts_dev->analog_input == 0)
+			stepconfigx |= TSCADC_STEPCONFIG_XNP |
+				TSCADC_STEPCONFIG_YPN;
+		else
+			stepconfigx |= TSCADC_STEPCONFIG_XNN |
+				TSCADC_STEPCONFIG_YPP;
 		break;
 	case 8:
-		stepconfigx |= TSCADC_STEPCONFIG_INP_4;
+		if (ts_dev->analog_input == 0)
+			stepconfigx |= TSCADC_STEPCONFIG_INP_4 |
+				TSCADC_STEPCONFIG_YPN;
+		else
+			stepconfigx |= TSCADC_STEPCONFIG_INP |
+				TSCADC_STEPCONFIG_XNN;
 		break;
 	}
+
 	for (i = 1; i < 7; i++) {
 		tscadc_writel(ts_dev, TSCADC_REG_STEPCONFIG(i), stepconfigx);
 		tscadc_writel(ts_dev, TSCADC_REG_STEPDELAY(i), delay);
@@ -146,18 +166,28 @@ static void tsc_step_config(struct tscadc *ts_dev)
 			TSCADC_STEPCONFIG_INM | TSCADC_STEPCONFIG_FIFO1;
 	switch (ts_dev->wires) {
 	case 4:
-		stepconfigy |= TSCADC_STEPCONFIG_XNP;
+		if (ts_dev->analog_input == 0)
+			stepconfigy |= TSCADC_STEPCONFIG_XNP;
+		else
+			stepconfigy |= TSCADC_STEPCONFIG_YPP;
 		break;
 	case 5:
-		stepconfigy |= TSCADC_STEPCONFIG_XPP |
-				TSCADC_STEPCONFIG_XNP |
-				TSCADC_STEPCONFIG_YPN |
-				TSCADC_STEPCONFIG_INP_5;
+		stepconfigy |= TSCADC_STEPCONFIG_XPP | TSCADC_STEPCONFIG_INP_5;
+		if (ts_dev->analog_input == 0)
+			stepconfigy |= TSCADC_STEPCONFIG_XNN |
+				TSCADC_STEPCONFIG_YPP;
+		else
+			stepconfigy |= TSCADC_STEPCONFIG_XNP |
+				TSCADC_STEPCONFIG_YPN;
 		break;
 	case 8:
-		stepconfigy |= TSCADC_STEPCONFIG_XNP;
+		if (ts_dev->analog_input == 0)
+			stepconfigy |= TSCADC_STEPCONFIG_XNP;
+		else
+			stepconfigy |= TSCADC_STEPCONFIG_YPP;
 		break;
 	}
+
 	for (i = 7; i < 13; i++) {
 		tscadc_writel(ts_dev, TSCADC_REG_STEPCONFIG(i), stepconfigy);
 		tscadc_writel(ts_dev, TSCADC_REG_STEPDELAY(i), delay);
@@ -166,8 +196,12 @@ static void tsc_step_config(struct tscadc *ts_dev)
 	chargeconfig = TSCADC_STEPCONFIG_XPP |
 			TSCADC_STEPCONFIG_YNN |
 			TSCADC_STEPCONFIG_RFP |
-			TSCADC_STEPCHARGE_INM | TSCADC_STEPCHARGE_INP |
 			TSCADC_STEPCHARGE_RFM;
+	if (ts_dev->analog_input == 0)
+		chargeconfig |= TSCADC_STEPCHARGE_INM_SWAP |
+			TSCADC_STEPCHARGE_INP_SWAP;
+	else
+		chargeconfig |= TSCADC_STEPCHARGE_INM | TSCADC_STEPCHARGE_INP;
 	tscadc_writel(ts_dev, TSCADC_REG_CHARGECONFIG, chargeconfig);
 	tscadc_writel(ts_dev, TSCADC_REG_CHARGEDELAY, TSCADC_STEPCHARGE_DELAY);
 
@@ -180,20 +214,12 @@ static void tsc_idle_config(struct tscadc *ts_config)
 	unsigned int	 idleconfig;
 
 	idleconfig = TSCADC_STEPCONFIG_YNN |
-				TSCADC_STEPCONFIG_XNN |
-				TSCADC_STEPCONFIG_INM;
+			TSCADC_STEPCONFIG_INM | TSCADC_STEPCONFIG_IDLE_INP;
+	if (ts_config->analog_input == 0)
+		idleconfig |= TSCADC_STEPCONFIG_XNN;
+	else
+		idleconfig |= TSCADC_STEPCONFIG_YPN;
 
-	switch (ts_config->wires) {
-	case 4:
-		idleconfig |= TSCADC_STEPCONFIG_IDLE_INP;
-		break;
-	case 5:
-		idleconfig |= TSCADC_STEPCONFIG_INP_5;
-		break;
-	case 8:
-		idleconfig |= TSCADC_STEPCONFIG_INP_4;
-		break;
-	}
 	tscadc_writel(ts_config, TSCADC_REG_IDLECONFIG, idleconfig);
 }
 
@@ -376,6 +402,7 @@ static	int __devinit tscadc_probe(struct platform_device *pdev)
 	tscadc_writel(ts_dev, TSCADC_REG_IRQWAKEUP, TSCADC_IRQWKUP_ENB);
 
 	ts_dev->wires = pdata->wires;
+	ts_dev->analog_input = pdata->analog_input;
 
 	/* Set the control register bits */
 	ctrl = TSCADC_CNTRLREG_STEPCONFIGWRT |
diff --git a/include/linux/input/ti_tscadc.h b/include/linux/input/ti_tscadc.h
index 29f87db..b9d9a46 100644
--- a/include/linux/input/ti_tscadc.h
+++ b/include/linux/input/ti_tscadc.h
@@ -3,8 +3,18 @@
  * @wires:		Wires refer to application modes
  *			i.e. 4/5/8 wire touchscreen support
  *			on the platform
+ * @analog_input:	Analog inputs refer to the order in which the
+ *			connections are made to the AFE. If the connections
+ *			are as : XPUL = AN0,XNUR = AN1,YPLL = AN2,
+ *			YNLR = AN3, then this variable is set to 1.
+ *			Else if the connections are as :
+ *			XPUL = AN0,YPLL = AN1,XNUR = AN2,
+ *			YNLR = AN3, then set this variable to
+ *			0.
  */
 
 struct tsc_data {
 	int wires;
+	int analog_input;
+
 };
-- 
1.7.2.5

