From f7ba5de3af0c7d3abd7624676e07752e5d8b7ebd Mon Sep 17 00:00:00 2001
From: Jacob Pan <jacob.jun.pan@intel.com>
Date: Fri, 4 Dec 2009 10:57:07 -0800
Subject: [PATCH 040/104] MRST: touch screen driver

---
 drivers/input/touchscreen/Kconfig    |    9 +
 drivers/input/touchscreen/Makefile   |    2 +
 drivers/input/touchscreen/mrstouch.c |  947 ++++++++++++++++++++++++++++++++++
 3 files changed, 958 insertions(+), 0 deletions(-)
 create mode 100644 drivers/input/touchscreen/mrstouch.c

diff --git a/drivers/input/touchscreen/Kconfig b/drivers/input/touchscreen/Kconfig
index dfafc76..6dd2674 100644
--- a/drivers/input/touchscreen/Kconfig
+++ b/drivers/input/touchscreen/Kconfig
@@ -577,4 +577,13 @@ config TOUCHSCREEN_PCAP
 
 	  To compile this driver as a module, choose M here: the
 	  module will be called pcap_ts.
+
+config TOUCHSCREEN_MRSTOUCH
+	tristate "Intel Moorstown Resistive touchscreen"
+	depends on LNW_IPC
+	default y
+	help
+	  Say Y here if you have a Intel Moorstown based Touchscreen
+	  If unsure, say N.
+
 endif
diff --git a/drivers/input/touchscreen/Makefile b/drivers/input/touchscreen/Makefile
index d61a3b4..15ad257 100644
--- a/drivers/input/touchscreen/Makefile
+++ b/drivers/input/touchscreen/Makefile
@@ -45,3 +45,5 @@ obj-$(CONFIG_TOUCHSCREEN_WM97XX_ATMEL)	+= atmel-wm97xx.o
 obj-$(CONFIG_TOUCHSCREEN_WM97XX_MAINSTONE)	+= mainstone-wm97xx.o
 obj-$(CONFIG_TOUCHSCREEN_WM97XX_ZYLONITE)	+= zylonite-wm97xx.o
 obj-$(CONFIG_TOUCHSCREEN_W90X900)	+= w90p910_ts.o
+obj-$(CONFIG_TOUCHSCREEN_MRSTOUCH)    += mrstouch.o
+
diff --git a/drivers/input/touchscreen/mrstouch.c b/drivers/input/touchscreen/mrstouch.c
new file mode 100644
index 0000000..f6aba7d
--- /dev/null
+++ b/drivers/input/touchscreen/mrstouch.c
@@ -0,0 +1,947 @@
+/*
+ * mrstouch.c - Intel Moorestown Resistive Touch Screen Driver
+ *
+ * Copyright (C) 2008 Intel Corp
+ *
+ * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; ifnot, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ *
+ * Questions/Comments/Bug fixes to Sreedhara (sreedhara.ds@intel.com)
+ * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ */
+#include <linux/module.h>
+#include <linux/init.h>
+#include <linux/input.h>
+#include <linux/interrupt.h>
+#include <linux/err.h>
+#include <linux/param.h>
+#include <linux/spi/spi.h>
+#include <linux/irq.h>
+#include <linux/delay.h>
+#include <linux/kthread.h>
+#include <asm/ipc_defs.h>
+
+MODULE_AUTHOR("Sreedhara Murthy. D.S, sreedhara.ds@intel.com");
+MODULE_DESCRIPTION("Intel Moorestown Resistive Touch Screen Driver");
+MODULE_LICENSE("GPL");
+
+#if defined(MRSTOUCH_DEBUG)
+#define mrstouch_debug(fmt, args...)\
+	do { \
+		printk(KERN_DEBUG "\n[MRSTOUCH(%d)] - ", __LINE__); \
+		printk(KERN_DEBUG fmt, ##args); \
+	} while (0);
+#else
+#define mrstouch_debug(fmt, args...)
+#endif
+
+#define mrstouch_error(fmt, args...)\
+	do { \
+		printk(KERN_ERR "\n[MRSTOUCH(%d)] - ", __LINE__); \
+		printk(KERN_ERR fmt, ##args); \
+	} while (0);
+
+/* PMIC Interrupt registers */
+#define PMIC_REG_ID1   0x00 /*PMIC ID1 register */
+
+/* PMIC Interrupt registers */
+#define PMIC_REG_INT   0x04 /*PMIC interrupt register */
+#define PMIC_REG_MINT  0x05 /*PMIC interrupt mask register */
+
+/* ADC Interrupt registers */
+#define PMIC_REG_ADCINT   0x5F /*ADC interrupt register */
+#define PMIC_REG_MADCINT  0x60 /*ADC interrupt mask register */
+
+/* ADC Control registers */
+#define PMIC_REG_ADCCNTL1    0x61 /*ADC control register */
+
+/* ADC Channel Selection registers */
+#define PMICADDR0     0xA4
+#define END_OF_CHANNEL 0x1F
+
+/* ADC Result register */
+#define PMIC_REG_ADCSNS0H   0x64
+
+/* ADC channels for touch screen */
+#define MRST_TS_CHAN10   0xA /* Touch screen X+ connection */
+#define MRST_TS_CHAN11   0xB /* Touch screen X- connection */
+#define MRST_TS_CHAN12   0xC /* Touch screen Y+ connection */
+#define MRST_TS_CHAN13   0xD /* Touch screen Y- connection */
+
+/* Touch screen coordinate constants */
+#define TOUCH_PRESSURE   	5
+#define TOUCH_PRESSURE_FS	100
+
+#define XMOVE_LIMIT	5
+#define YMOVE_LIMIT	5
+#define XYMOVE_CNT	3
+
+#define MAX_10BIT	((1<<10)-1)
+
+/* Touch screen channel BIAS constants */
+#define XBIAS		0x20
+#define YBIAS		0x40
+#define ZBIAS		0x80
+
+/* Touch screen coordinates */
+#define MIN_X		10
+#define MAX_X		1024
+#define MIN_Y		10
+#define MAX_Y		1024
+#define WAIT_ADC_COMPLETION 10
+
+/* PMIC ADC round robin delays */
+#define ADC_LOOP_DELAY0 0x0 /* Continuous loop */
+#define ADC_LOOP_DELAY1 0x1 /* 4.5  ms approximate */
+
+/* PMIC Vendor Identifiers */
+#define PMIC_VENDOR_FS  0 /* PMIC vendor FreeScale */
+#define PMIC_VENDOR_MAXIM 1 /* PMIC vendor MAXIM */
+#define PMIC_VENDOR_NEC 2 /* PMIC vendor NEC */
+#define MRSTOUCH_MAX_CHANNELS 32 /* Maximum ADC channels */
+
+/* Touch screen device structure */
+struct mrstouch_dev {
+	struct spi_device *spi; /* SPI device associated with touch screen */
+	struct input_dev *input; /* input device for touchscreen*/
+	char 		phys[32]; /* Device name */
+	struct task_struct *pendet_thrd; /* PENDET interrupt handler */
+	struct semaphore lock; /* Sync between interrupt and PENDET handler */
+	bool            busy; /* Busy flag */
+	u16             asr; /* Address selection register */
+	int             irq;    /* Touch screen IRQ # */
+	uint		vendor;  /* PMIC vendor */
+	uint		rev;  /* PMIC revision */
+	bool		suspended; /* Device suspended status */
+	bool		disabled;  /* Device disabled status */
+	u16		x;  /* X coordinate */
+	u16		y;  /* Y coordinate */
+	bool		pendown;  /* PEN position */
+} ;
+
+
+/* Global Pointer to Touch screen device */
+static struct mrstouch_dev *mrstouchdevp;
+
+/* Utility to read PMIC ID */
+static int mrstouch_pmic_id(uint *vendor, uint *rev)
+{
+	int err;
+	struct ipc_pmic_reg_data ipcbuf;
+
+	ipcbuf.ioc = 1;
+	ipcbuf.num_entries = 1;
+	ipcbuf.pmic_reg_data[0].register_address = PMIC_REG_ID1;
+
+	err = ipc_pmic_register_read(&ipcbuf);
+	if (err)
+		return -1;
+
+	*vendor = (ipcbuf.pmic_reg_data[0].value) & 0x7;
+	*rev = (ipcbuf.pmic_reg_data[0].value >> 3) & 0x7;
+
+	return 0;
+}
+
+/*
+ * Parse ADC channels to find end of the channel configured by other ADC user
+ * NEC and MAXIM requires 4 channels and FreeScale needs 18 channels
+ */
+static int mrstouch_chan_parse(void)
+{
+	int err, i, j, chan, found;
+	struct ipc_pmic_reg_data ipcbuf;
+
+	ipcbuf.ioc = 1;
+
+	found = -1;
+	ipcbuf.num_entries = 4;
+
+	for (i = 0; i < MRSTOUCH_MAX_CHANNELS; i++) {
+		if (found >= 0)
+			break;
+
+		for (j = 0; j <= 3; j++)
+			ipcbuf.pmic_reg_data[j].register_address = PMICADDR0+i;
+
+		err = ipc_pmic_register_read(&ipcbuf);
+		if (err)
+			return -1;
+
+		for (j = 0; j < ipcbuf.num_entries; j++) {
+			chan = ipcbuf.pmic_reg_data[j].value;
+			if (chan == END_OF_CHANNEL) {
+				found = i;
+				break;
+			}
+		}
+	}
+
+	if (found < 0)
+		return 0;
+
+	if (mrstouchdevp->vendor == PMIC_VENDOR_FS) {
+		if (found && found > (MRSTOUCH_MAX_CHANNELS - 18))
+			return -1;
+	} else {
+		if (found && found > (MRSTOUCH_MAX_CHANNELS - 4))
+			return -1;
+	}
+	return found;
+}
+
+/* Utility to enable/disable pendet.
+ * pendet set to true enables PENDET interrupt
+ * pendet set to false disables PENDET interrupt
+ * Also clears RND mask bit
+*/
+static void pendet_enable(bool pendet)
+{
+	u8 adccntrl1 = 0;
+	u8 pendet_enabled = 0;
+	int retry = 0;
+	struct ipc_pmic_reg_data ipcbuf;
+
+	ipcbuf.ioc = 1;
+
+	ipcbuf.num_entries = 1;
+	ipcbuf.pmic_reg_data[0].register_address = PMIC_REG_ADCCNTL1;
+	ipc_pmic_register_read(&ipcbuf);
+	adccntrl1 = ipcbuf.pmic_reg_data[0].value;
+
+	if (pendet)
+		adccntrl1 |= 0x20; /* Enable pendet */
+	else
+		adccntrl1 &= 0xDF; /* Disable pendet */
+
+	ipcbuf.num_entries = 2;
+	ipcbuf.pmic_reg_data[0].register_address = PMIC_REG_MADCINT;
+	ipcbuf.pmic_reg_data[0].value = 0x0;
+	ipcbuf.pmic_reg_data[1].register_address = PMIC_REG_ADCCNTL1;
+	ipcbuf.pmic_reg_data[1].value = adccntrl1;
+	ipc_pmic_register_write(&ipcbuf, 1);
+
+	if (!pendet)
+		return;
+
+
+	/*
+	 * Sometimes even after ipc_pmic_register_write success
+	 * the PMIC register value is not updated. Retry few iterations
+	 * to enable pendet.
+	*/
+	ipcbuf.num_entries = 1;
+	ipcbuf.pmic_reg_data[0].register_address = PMIC_REG_ADCCNTL1;
+	ipc_pmic_register_read(&ipcbuf);
+	pendet_enabled = (ipcbuf.pmic_reg_data[0].value >> 5) & 0x01;
+
+	retry = 0;
+	while (!pendet_enabled) {
+		retry++;
+		msleep(10);
+		ipcbuf.pmic_reg_data[0].register_address = PMIC_REG_ADCCNTL1;
+		ipcbuf.pmic_reg_data[0].value = adccntrl1;
+		ipc_pmic_register_write(&ipcbuf, 1);
+
+		ipcbuf.pmic_reg_data[0].register_address = PMIC_REG_ADCCNTL1;
+		ipc_pmic_register_read(&ipcbuf);
+		pendet_enabled = (ipcbuf.pmic_reg_data[0].value >> 5) & 0x01;
+		if (retry >= 10) {
+			printk(KERN_ERR "Touch screen disabled\n");
+			break;
+		}
+	}
+}
+
+
+/* To read PMIC ADC touch screen result
+ * Reads ADC storage registers for higher 7 and lower 3 bits
+ * converts the two readings to single value and turns off gain bit
+ */
+static int mrstouch_ts_chan_read(u16 offset, u16 chan, u16 *vp, u16 *vm)
+{
+	int err, count;
+	u16 result;
+	struct ipc_pmic_reg_data	ipcbuf;
+
+	ipcbuf.ioc = 1;
+	ipcbuf.num_entries = 4;
+
+	result = PMIC_REG_ADCSNS0H + offset;
+
+	if (chan == MRST_TS_CHAN12)
+		result += 4;
+
+	for (count = 0; count <= 3; count++)
+		ipcbuf.pmic_reg_data[count].register_address = result++;
+
+	err = ipc_pmic_register_read(&ipcbuf);
+	if (err)
+		return -1;
+
+	*vp = ipcbuf.pmic_reg_data[0].value << 3; /* Higher 7 bits */
+	*vp |= ipcbuf.pmic_reg_data[1].value & 0x7; /* Lower 3 bits */
+	*vp &= 0x3FF;
+
+	*vm = ipcbuf.pmic_reg_data[2].value << 3; /* Higher 7 bits */
+	*vm |= ipcbuf.pmic_reg_data[3].value & 0x7; /* Lower 3 bits */
+	*vm &= 0x3FF;
+
+	return 0;
+}
+
+/* To configure touch screen channels
+ * Writes touch screen channels to ADC address selection registers
+ */
+static int mrstouch_ts_chan_set(uint offset)
+{
+	int err, count;
+	u16 chan;
+	struct ipc_pmic_reg_data	ipcbuf;
+
+	ipcbuf.ioc = 1;
+	ipcbuf.num_entries = 5;
+
+	chan = PMICADDR0 + offset;
+	for (count = 0; count <= 3; count++) {
+		ipcbuf.pmic_reg_data[count].register_address = chan++;
+		ipcbuf.pmic_reg_data[count].value = MRST_TS_CHAN10 + count;
+	}
+	ipcbuf.pmic_reg_data[count].register_address = chan;
+	ipcbuf.pmic_reg_data[count].value = END_OF_CHANNEL;
+
+	err = ipc_pmic_register_write(&ipcbuf, 1);
+	if (err)
+		return -1;
+
+	return 0;
+}
+
+/* Initialize ADC */
+static int mrstouch_adc_init(struct mrstouch_dev *tsdev)
+{
+	int err, start;
+	struct ipc_pmic_mod_reg_data ipcbuf;
+
+	err = mrstouch_pmic_id(&tsdev->vendor, &tsdev->rev);
+	if (err) {
+		printk(KERN_ERR "Error in reading PMIC Id");
+		return err;
+	}
+
+	start = mrstouch_chan_parse();
+	if (start == -1) {
+		printk(KERN_ERR "Error in parse channels");
+		return start;
+	}
+
+	tsdev->asr = start;
+
+	mrstouch_debug("Channel offset(%d): 0x%X\n", tsdev->asr, tsdev->vendor);
+
+	/* ADC power on, start, enable PENDET and set loop delay
+	 * ADC loop delay is set to 4.5 ms approximately
+	 * Loop delay more than this results in jitter in adc readings
+	 * Setting loop delay to 0 (continous loop) in MAXIM stops PENDET
+	 * interrupt generation sometimes.
+	 */
+	ipcbuf.ioc = 1;
+	ipcbuf.num_entries = 2;
+	ipcbuf.pmic_mod_reg_data[0].register_address = PMIC_REG_ADCCNTL1;
+	ipcbuf.pmic_mod_reg_data[0].bit_map = 0xE7;
+
+	ipcbuf.pmic_mod_reg_data[1].register_address = PMIC_REG_MADCINT;
+	ipcbuf.pmic_mod_reg_data[1].bit_map = 0x03;
+
+	if (tsdev->vendor == PMIC_VENDOR_FS) {
+		ipcbuf.pmic_mod_reg_data[0].value = 0xE0 | ADC_LOOP_DELAY0;
+		ipcbuf.pmic_mod_reg_data[1].value = 0x5;
+	} else {
+		/* NEC and MAXIm not consistent with loop delay 0 */
+		ipcbuf.pmic_mod_reg_data[0].value = 0xE0 | ADC_LOOP_DELAY1;
+		ipcbuf.pmic_mod_reg_data[1].value = 0x0;
+
+		/* configure touch screen channels */
+		err = mrstouch_ts_chan_set(tsdev->asr);
+		if (err)
+			return err;
+	}
+
+	err = ipc_pmic_register_read_modify(&ipcbuf);
+
+	return err;
+}
+
+/* Reports x,y coordinates to event subsystem */
+static void mrstouch_report_xy(u16 x, u16 y, u16 z)
+{
+	int xdiff, ydiff;
+
+	if (mrstouchdevp->pendown && z <= TOUCH_PRESSURE) {
+		/* Pen removed, report button release */
+		mrstouch_debug("BTN REL(%d)", z);
+		input_report_key(mrstouchdevp->input, BTN_TOUCH, 0);
+		mrstouchdevp->pendown = false;
+	}
+
+	xdiff = abs(x - mrstouchdevp->x);
+	ydiff = abs(y - mrstouchdevp->y);
+
+	/*
+	if x and y values changes for XYMOVE_CNT readings it is considered
+	as stylus is moving. This is required to differentiate between stylus
+	movement and jitter
+	*/
+	if (x < MIN_X || x > MAX_X || y < MIN_Y || y > MAX_Y) {
+		/* Spurious values, release button if touched and return */
+		if (mrstouchdevp->pendown) {
+			mrstouch_debug("BTN REL(%d)", z);
+			input_report_key(mrstouchdevp->input, BTN_TOUCH, 0);
+			mrstouchdevp->pendown = false;
+		}
+		return;
+	} else if (xdiff >= XMOVE_LIMIT || ydiff >= YMOVE_LIMIT) {
+		mrstouchdevp->x = x;
+		mrstouchdevp->y = y;
+
+		input_report_abs(mrstouchdevp->input, ABS_X, x);
+		input_report_abs(mrstouchdevp->input, ABS_Y, y);
+		input_sync(mrstouchdevp->input);
+	}
+
+
+	if (!mrstouchdevp->pendown && z > TOUCH_PRESSURE) {
+		/* Pen touched, report button touch */
+		mrstouch_debug("BTN TCH(%d, %d, %d)", x, y, z);
+		input_report_key(mrstouchdevp->input, BTN_TOUCH, 1);
+		mrstouchdevp->pendown = true;
+	}
+}
+
+
+/* Utility to start ADC, used by freescale handler */
+static int pendet_mask(void)
+{
+	int err = 0;
+	struct ipc_pmic_mod_reg_data ipcbuf;
+
+	ipcbuf.ioc = 1;
+	ipcbuf.num_entries = 1;
+	ipcbuf.pmic_mod_reg_data[1].register_address = PMIC_REG_MADCINT;
+	ipcbuf.pmic_mod_reg_data[1].bit_map = 0x02;
+	ipcbuf.pmic_mod_reg_data[1].value = 0x01;
+
+	err = ipc_pmic_register_read_modify(&ipcbuf);
+
+	return err;
+}
+
+/* Utility to stop ADC, used by freescale handler */
+static int pendet_umask(void)
+{
+	int err = 0;
+	struct ipc_pmic_mod_reg_data ipcbuf;
+
+	ipcbuf.ioc = 1;
+	ipcbuf.num_entries = 1;
+	ipcbuf.pmic_mod_reg_data[1].register_address = PMIC_REG_MADCINT;
+	ipcbuf.pmic_mod_reg_data[1].bit_map = 0x02;
+	ipcbuf.pmic_mod_reg_data[1].value = 0x0;
+
+	err = ipc_pmic_register_read_modify(&ipcbuf);
+
+	return err;
+}
+
+/* Utility to read ADC, used by freescale handler */
+static int mrstouch_pmic_fs_adc_read(struct mrstouch_dev *tsdev)
+{
+	int err;
+	u16 x, y, z, result;
+	struct ipc_pmic_reg_data ipcbuf;
+
+	result = PMIC_REG_ADCSNS0H + tsdev->asr;
+
+	ipcbuf.ioc = 1;
+	ipcbuf.num_entries = 4;
+	ipcbuf.pmic_reg_data[0].register_address = result + 4;
+	ipcbuf.pmic_reg_data[1].register_address = result + 5;
+	ipcbuf.pmic_reg_data[2].register_address = result + 16;
+	ipcbuf.pmic_reg_data[3].register_address = result + 17;
+
+	err = ipc_pmic_register_read(&ipcbuf);
+	if (err)
+		goto ipc_error;
+
+	x = ipcbuf.pmic_reg_data[0].value << 3; /* Higher 7 bits */
+	x |= ipcbuf.pmic_reg_data[1].value & 0x7; /* Lower 3 bits */
+	x &= 0x3FF;
+
+	y = ipcbuf.pmic_reg_data[2].value << 3; /* Higher 7 bits */
+	y |= ipcbuf.pmic_reg_data[3].value & 0x7; /* Lower 3 bits */
+	y &= 0x3FF;
+
+	/* Read Z value */
+	ipcbuf.num_entries = 2;
+	ipcbuf.pmic_reg_data[0].register_address = result + 28;
+	ipcbuf.pmic_reg_data[1].register_address = result + 29;
+
+	err = ipc_pmic_register_read(&ipcbuf);
+	if (err)
+		goto ipc_error;
+
+	z = ipcbuf.pmic_reg_data[0].value << 3; /* Higher 7 bits */
+	z |= ipcbuf.pmic_reg_data[1].value & 0x7; /* Lower 3 bits */
+	z &= 0x3FF;
+
+#if defined(MRSTOUCH_PRINT_XYZP)
+	mrstouch_debug("X: %d, Y: %d, Z: %d", x, y, z);
+#endif
+
+	if (z >= TOUCH_PRESSURE_FS) {
+		mrstouch_report_xy(x, y, TOUCH_PRESSURE - 1); /* Pen Removed */
+		return TOUCH_PRESSURE - 1;
+	} else {
+		mrstouch_report_xy(x, y, TOUCH_PRESSURE + 1); /* Pen Touched */
+		return TOUCH_PRESSURE + 1;
+	}
+
+	return 0;
+
+ipc_error:
+	printk(KERN_ERR "IPC Error: %s", __func__);
+	return -1;
+}
+
+/* To handle free scale pmic pendet interrupt */
+static int pmic0_pendet(void *data)
+{
+	int err, count;
+	u16 chan;
+	unsigned int touched;
+	struct ipc_pmic_reg_data ipcbuf;
+	struct mrstouch_dev *tsdev = (struct mrstouch_dev *)data;
+
+	chan = PMICADDR0 + tsdev->asr;
+
+	ipcbuf.ioc = 1;
+	/* Set X BIAS */
+	ipcbuf.num_entries = 5;
+	for (count = 0; count <= 3; count++) {
+		ipcbuf.pmic_reg_data[count].register_address = chan++;
+		ipcbuf.pmic_reg_data[count].value = 0x2A;
+	}
+	ipcbuf.pmic_reg_data[count].register_address = chan++; /* Dummy */
+	ipcbuf.pmic_reg_data[count].value = 0;
+
+	err = ipc_pmic_register_write(&ipcbuf, 1);
+	if (err)
+		goto ipc_error;
+
+	msleep(WAIT_ADC_COMPLETION);
+
+	/* Set Y BIAS */
+	ipcbuf.num_entries = 5;
+	for (count = 0; count <= 3; count++) {
+		ipcbuf.pmic_reg_data[count].register_address = chan++;
+		ipcbuf.pmic_reg_data[count].value = 0x4A;
+	}
+	ipcbuf.pmic_reg_data[count].register_address = chan++; /* Dummy */
+	ipcbuf.pmic_reg_data[count].value = 0;
+
+	err = ipc_pmic_register_write(&ipcbuf, 1);
+	if (err)
+		goto ipc_error;
+
+	msleep(WAIT_ADC_COMPLETION);
+
+	/* Set Z BIAS */
+	chan += 2;
+	ipcbuf.num_entries = 4;
+	for (count = 0; count <= 3; count++) {
+		ipcbuf.pmic_reg_data[count].register_address = chan++;
+		ipcbuf.pmic_reg_data[count].value = 0x8A;
+	}
+
+	err = ipc_pmic_register_write(&ipcbuf, 1);
+	if (err)
+		goto ipc_error;
+
+	msleep(WAIT_ADC_COMPLETION);
+
+	/*Read touch screen channels till pen removed
+	* Freescale reports constant value of z for all points
+	* z is high when screen is not touched and low when touched
+	* Map high z value to not touched and low z value to pen touched
+	*/
+	touched = mrstouch_pmic_fs_adc_read(tsdev);
+	while (touched > TOUCH_PRESSURE) {
+		touched = mrstouch_pmic_fs_adc_read(tsdev);
+		msleep(WAIT_ADC_COMPLETION);
+	}
+
+	/* Clear all TS channels */
+	chan = PMICADDR0 + tsdev->asr;
+	ipcbuf.ioc = 1;
+	ipcbuf.num_entries = 5;
+	for (count = 0; count <= 4; count++) {
+		ipcbuf.pmic_reg_data[count].register_address = chan++;
+		ipcbuf.pmic_reg_data[count].value = 0x0;
+	}
+	err = ipc_pmic_register_write(&ipcbuf, 1);
+	if (err)
+		goto ipc_error;
+
+	for (count = 0; count <= 4; count++) {
+		ipcbuf.pmic_reg_data[count].register_address = chan++;
+		ipcbuf.pmic_reg_data[count].value = 0x0;
+	}
+	err = ipc_pmic_register_write(&ipcbuf, 1);
+	if (err)
+		goto ipc_error;
+
+	chan += 2;
+	for (count = 0; count <= 4; count++) {
+		ipcbuf.pmic_reg_data[count].register_address = chan++;
+		ipcbuf.pmic_reg_data[count].value = 0x0;
+	}
+	err = ipc_pmic_register_write(&ipcbuf, 1);
+	if (err)
+		goto ipc_error;
+
+	return 0;
+
+ipc_error:
+	printk(KERN_ERR "IPC Error: %s", __func__);
+	return -1;
+}
+
+
+/* To enable X, Y and Z bias values
+ * Enables YPYM for X channels and XPXM for Y channels
+ */
+static int mrstouch_ts_bias_set(uint offset, uint bias)
+{
+	int err, count;
+	u16 chan, start;
+	struct ipc_pmic_reg_data ipcbuf;
+
+	chan = PMICADDR0 + offset;
+	start = MRST_TS_CHAN10;
+
+	ipcbuf.ioc = 1;
+	ipcbuf.num_entries = 4;
+
+	for (count = 0; count <= 3; count++) {
+		ipcbuf.pmic_reg_data[count].register_address = chan++;
+		ipcbuf.pmic_reg_data[count].value = bias | (start + count);
+	}
+
+	err = ipc_pmic_register_write(&ipcbuf, 1);
+	if (err)
+		return -1;
+
+	return 0;
+}
+
+/* To read touch screen channel values */
+static int mrstouch_adc_read(struct mrstouch_dev *tsdev)
+{
+	int err;
+	u16 xp, xm, yp, ym, zp, zm;
+
+	/* configure Y bias for X channels */
+	err = mrstouch_ts_bias_set(tsdev->asr, YBIAS);
+	if (err)
+		goto ipc_error;
+
+	msleep(WAIT_ADC_COMPLETION);
+
+	/* read x+ and x- channels */
+	err = mrstouch_ts_chan_read(tsdev->asr, MRST_TS_CHAN10, &xp, &xm);
+	if (err)
+		goto ipc_error;
+
+	/* configure x bias for y channels */
+	err = mrstouch_ts_bias_set(tsdev->asr, XBIAS);
+	if (err)
+		goto ipc_error;
+
+	msleep(WAIT_ADC_COMPLETION);
+
+	/* read y+ and y- channels */
+	err = mrstouch_ts_chan_read(tsdev->asr, MRST_TS_CHAN12, &yp, &ym);
+	if (err)
+		goto ipc_error;
+
+	/* configure z bias for x and y channels */
+	err = mrstouch_ts_bias_set(tsdev->asr, ZBIAS);
+	if (err)
+		goto ipc_error;
+
+	msleep(WAIT_ADC_COMPLETION);
+
+	/* read z+ and z- channels */
+	err = mrstouch_ts_chan_read(tsdev->asr, MRST_TS_CHAN10, &zp, &zm);
+	if (err)
+		goto ipc_error;
+
+#if defined(MRSTOUCH_PRINT_XYZP)
+	printk(KERN_INFO "X+: %d, Y+: %d, Z+: %d\n", xp, yp, zp);
+#endif
+
+#if defined(MRSTOUCH_PRINT_XYZM)
+	printk(KERN_INFO "X-: %d, Y-: %d, Z-: %d\n", xm, ym, zm);
+#endif
+
+	mrstouch_report_xy(xp, yp, zp); /* report x and y to eventX */
+
+	return zp;
+
+ipc_error:
+	printk(KERN_ERR "IPC Error: %s", __func__);
+	return -1;
+}
+
+/* PENDET interrupt handler function for NEC and MAXIM */
+static void pmic12_pendet(void *data)
+{
+	unsigned int touched;
+	struct mrstouch_dev *tsdev = (struct mrstouch_dev *)data;
+
+	/* read touch screen channels till pen removed */
+	touched = mrstouch_adc_read(tsdev);
+	while (touched > TOUCH_PRESSURE) {
+		msleep(WAIT_ADC_COMPLETION);
+		touched = mrstouch_adc_read(tsdev);
+	}
+}
+
+/* Handler to process PENDET interrupt */
+int mrstouch_pendet(void *data)
+{
+	struct mrstouch_dev *tsdev = (struct mrstouch_dev *)data;
+	while (1) {
+		/* Wait for PENDET interrupt */
+		if (down_interruptible(&tsdev->lock)) {
+			msleep(WAIT_ADC_COMPLETION);
+			continue;
+		}
+
+		if (tsdev->busy)
+			return 0;
+
+		tsdev->busy = true;
+
+		if (mrstouchdevp->vendor == PMIC_VENDOR_NEC ||
+			mrstouchdevp->vendor == PMIC_VENDOR_MAXIM) {
+			/* PENDET must be disabled in NEC before reading ADC */
+			pendet_enable(false); /* Disbale PENDET */
+			pmic12_pendet(mrstouchdevp);
+			pendet_enable(true); /*Enable PENDET */
+		} else if (mrstouchdevp->vendor == PMIC_VENDOR_FS) {
+			pendet_umask(); /* Stop ADC */
+			pmic0_pendet(mrstouchdevp);
+			pendet_mask(); /* Stop ADC */
+		} else
+			printk(KERN_ERR "Unknown PMIC, Not supported\n");
+
+		tsdev->busy = false;
+
+	}
+	return 0;
+}
+
+/* PENDET interrupt handler */
+static irqreturn_t pendet_intr_handler(int irq, void *handle)
+{
+	struct mrstouch_dev *tsdev = (struct mrstouch_dev *)handle;
+
+	up(&tsdev->lock);
+	return IRQ_HANDLED;
+}
+
+/* Intializes input device and registers with input subsystem */
+static int ts_input_dev_init(struct mrstouch_dev *tsdev, struct spi_device *spi)
+{
+	int err = 0;
+
+	mrstouch_debug("%s", __func__);
+
+	tsdev->input = input_allocate_device();
+	if (!tsdev->input) {
+		mrstouch_error("%s", "Input dev allocation failed");
+		return -1;
+	}
+
+	tsdev->input->name = "mrst_touchscreen";
+	snprintf(tsdev->phys, sizeof(tsdev->phys),
+			"%s/input0", dev_name(&spi->dev));
+	tsdev->input->phys = tsdev->phys;
+	tsdev->input->dev.parent = &spi->dev;
+
+	tsdev->input->id.vendor = tsdev->vendor;
+	tsdev->input->id.version = tsdev->rev;
+
+	tsdev->input->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
+	tsdev->input->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
+
+	input_set_abs_params(tsdev->input, ABS_X, MIN_X, MIN_Y, 0, 0);
+	input_set_abs_params(tsdev->input, ABS_Y, MIN_X, MIN_Y, 0, 0);
+
+	err = input_register_device(tsdev->input);
+	if (err) {
+		mrstouch_error("%s", "Input dev registration failed");
+		input_free_device(tsdev->input);
+		return -1;
+	}
+
+	mrstouch_debug("%s", "mrstouch initialized");
+
+	return 0;
+
+}
+
+/* Probe function for touch screen driver */
+static int __devinit mrstouch_probe(struct spi_device *mrstouch_spi)
+{
+	int err;
+	unsigned int myirq;
+	struct mrstouch_dev *tsdev;
+
+	mrstouch_debug("%s(%p)", __func__, mrstouch_spi);
+
+	mrstouchdevp = NULL;
+	myirq = mrstouch_spi->irq;
+
+	if (!mrstouch_spi->irq) {
+		mrstouch_error("%s(%d)", "No IRQ", myirq);
+		return -1;
+	}
+
+	tsdev = kzalloc(sizeof(struct mrstouch_dev), GFP_KERNEL);
+	if (!tsdev) {
+		mrstouch_error("%s", "ERROR: Memory failure");
+		return -ENOMEM;
+	}
+
+	tsdev->irq = myirq;
+	mrstouchdevp = tsdev;
+
+	if (mrstouch_adc_init(tsdev) != 0) {
+		mrstouch_error("%s", "ADC init failed");
+		goto mrstouch_err_free_mem;
+	}
+
+	dev_set_drvdata(&mrstouch_spi->dev, tsdev);
+	tsdev->spi = mrstouch_spi;
+
+	err = ts_input_dev_init(tsdev, mrstouch_spi);
+	if (err != 0) {
+		mrstouch_error("%s", "ts_input_dev_init failed");
+		goto mrstouch_err_free_mem;
+	}
+
+	sema_init(&tsdev->lock, 1);
+	if (down_interruptible(&tsdev->lock)) {
+		mrstouch_error("%s", "tsdev->lock Error");
+		goto mrstouch_err_free_mem;
+	}
+
+	mrstouch_debug("Requesting IRQ-%d", myirq);
+	err = request_irq(myirq, pendet_intr_handler,
+				0, "mrstouch", tsdev);
+	if (err) {
+		mrstouch_error("IRQ Request Failed - %d", err);
+		goto mrstouch_err_free_mem;
+	}
+
+	tsdev->pendet_thrd = kthread_run(mrstouch_pendet,
+				(void *)tsdev, "pendet handler");
+	if (IS_ERR(tsdev->pendet_thrd)) {
+		dev_err(&tsdev->spi->dev, "kthread_run failed \n");
+		goto mrstouch_err_free_mem;
+	}
+
+	mrstouch_debug("%s", "Driver initialized");
+
+	return 0;
+
+ mrstouch_err_free_mem:
+	kfree(tsdev);
+	return -1;
+}
+
+static int mrstouch_suspend(struct spi_device *spi, pm_message_t msg)
+{
+	mrstouch_debug("%s", __func__);
+	mrstouchdevp->suspended = 1;
+	return 0;
+}
+
+static int mrstouch_resume(struct spi_device *spi)
+{
+	mrstouch_debug("%s", __func__);
+	mrstouchdevp->suspended = 0;
+	return 0;
+}
+
+static int mrstouch_remove(struct spi_device *spi)
+{
+	mrstouch_debug("%s", __func__);
+	free_irq(mrstouchdevp->irq, mrstouchdevp);
+	input_unregister_device(mrstouchdevp->input);
+	input_free_device(mrstouchdevp->input);
+	kfree(mrstouchdevp);
+	if (mrstouchdevp->pendet_thrd)
+		kthread_stop(mrstouchdevp->pendet_thrd);
+	return 0;
+}
+
+static struct spi_driver mrstouch_driver = {
+	.driver = {
+		.name   = "pmic_touch",
+		.bus    = &spi_bus_type,
+		.owner  = THIS_MODULE,
+	},
+	.probe          = mrstouch_probe,
+	.suspend        = mrstouch_suspend,
+	.resume         = mrstouch_resume,
+	.remove         = mrstouch_remove,
+};
+
+static int __init mrstouch_module_init(void)
+{
+	int err;
+
+	mrstouch_debug("%s", __func__);
+	err = spi_register_driver(&mrstouch_driver);
+	if (err) {
+		mrstouch_debug("%s(%d)", "SPI PENDET failed", err);
+		return -1;
+	}
+
+	return 0;
+}
+
+static void __exit mrstouch_module_exit(void)
+{
+	mrstouch_debug("%s", __func__);
+	spi_unregister_driver(&mrstouch_driver);
+	return;
+}
+
+module_init(mrstouch_module_init);
+module_exit(mrstouch_module_exit);
-- 
1.6.2.5

