[target/msm8660_surf]: Add keypad driver support.

Add code to interface with PMIC SSBI arbiter registers to scan keypad for
msm8660 target. Add two additional functions to read, write and configure
the PMIC gpios used for keypad. Add code to use different read/write functions
for PMIC SSBI2 interface for msm7630 and PMIC arbiter wrapper interface for
msm8660 target.

Change-Id: Idde16232785dfc06728dea29c05b99251e45ba9f
diff --git a/app/aboot/aboot.c b/app/aboot/aboot.c
index 3b220d0..b09c9cd 100755
--- a/app/aboot/aboot.c
+++ b/app/aboot/aboot.c
@@ -544,6 +544,14 @@
 	dprintf(INFO, "Diplay initialized\n");
 	disp_init = 1;
 	#endif
+
+        if (keys_get_state(KEY_HOME) != 0)
+            boot_into_recovery = 1;
+        if (keys_get_state(KEY_BACK) != 0)
+            goto fastboot;
+        if (keys_get_state(KEY_CLEAR) != 0)
+            goto fastboot;
+
 	if (target_is_emmc_boot())
         {
             page_size = 2048;
@@ -554,13 +562,6 @@
         {
             page_size = flash_page_size();
             page_mask = page_size - 1;
-            if (keys_get_state(KEY_HOME) != 0)
-                    boot_into_recovery = 1;
-            if (keys_get_state(KEY_BACK) != 0)
-                    goto fastboot;
-            if (keys_get_state(KEY_CLEAR) != 0)
-                    goto fastboot;
-
             reboot_mode = check_reboot_mode();
             if (reboot_mode == RECOVERY_MODE){
                     boot_into_recovery = 1;
diff --git a/dev/keys/gpio_keypad.c b/dev/keys/gpio_keypad.c
index b3e21c1..abd894a 100644
--- a/dev/keys/gpio_keypad.c
+++ b/dev/keys/gpio_keypad.c
@@ -2,7 +2,7 @@
  * Copyright (c) 2009, Google Inc.
  * All rights reserved.
  *
- * Copyright (c) 2009, Code Aurora Forum. All rights reserved.
+ * Copyright (c) 2009-2010, Code Aurora Forum. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -289,9 +289,69 @@
 	return 0;
 }
 
+int pa1_ssbi2_read_bytes(unsigned char  *buffer, unsigned short length,
+                                                unsigned short slave_addr)
+{
+    unsigned val = 0x0;
+    unsigned temp = 0x0000;
+    unsigned char *buf = buffer;
+    unsigned short len = length;
+    unsigned short addr = slave_addr;
+    unsigned long timeout = SSBI_TIMEOUT_US;
+
+    while(len)
+    {
+        val |= ((addr << PA1_SSBI2_REG_ADDR_SHIFT) |
+		(PA1_SSBI2_CMD_READ << PA1_SSBI2_CMD_RDWRN_SHIFT));
+        writel(val, PA1_SSBI2_CMD);
+        while(!((temp = readl(PA1_SSBI2_RD_STATUS)) & (1 << PA1_SSBI2_TRANS_DONE_SHIFT))) {
+            if (--timeout == 0) {
+	        dprintf(INFO, "In Device ready function:Timeout\n");
+	        return 1;
+	    }
+	}
+        len--;
+        *buf++ = (temp & (PA1_SSBI2_REG_DATA_MASK << PA1_SSBI2_REG_DATA_SHIFT));
+    }
+    return 0;
+}
+
+int pa1_ssbi2_write_bytes(unsigned char  *buffer, unsigned short length,
+                                                unsigned short slave_addr)
+{
+    unsigned val;
+    unsigned char *buf = buffer;
+    unsigned short len = length;
+    unsigned short addr = slave_addr;
+    unsigned temp = 0x00;
+    unsigned char written_data1 = 0x00;
+    unsigned long timeout = SSBI_TIMEOUT_US;
+    //unsigned char written_data2 = 0x00;
+
+    while(len)
+    {
+        temp = 0x00;
+        written_data1 = 0x00;
+        val = (addr << PA1_SSBI2_REG_ADDR_SHIFT) |
+	  (PA1_SSBI2_CMD_WRITE << PA1_SSBI2_CMD_RDWRN_SHIFT) |
+ (*buf & 0xFF);
+        writel(val, PA1_SSBI2_CMD);
+        while(!((temp = readl(PA1_SSBI2_RD_STATUS)) & (1 << PA1_SSBI2_TRANS_DONE_SHIFT))) {
+            if (--timeout == 0) {
+	        dprintf(INFO, "In Device write function:Timeout\n");
+	        return 1;
+	    }
+	}
+        len--;
+        buf++;
+    }
+    return 0;
+}
+
 int pm8058_gpio_config(int gpio, struct pm8058_gpio *param)
 {
 	int	rc;
+        write_func wr_function = (qwerty_keypad->keypad_info)->wr_func;
 	unsigned char bank[8];
 	static int dir_map[] = {
 		PM8058_GPIO_MODE_OFF,
@@ -329,7 +389,7 @@
 		((param->function << PM8058_GPIO_FUNC_SHIFT) &
 			PM8058_GPIO_FUNC_MASK);
 
-	rc = i2c_ssbi_write_bytes(bank, 5, SSBI_REG_ADDR_GPIO(gpio));
+	rc = (*wr_function)(bank, 5, SSBI_REG_ADDR_GPIO(gpio));
 	if (rc) {
 	        dprintf(INFO, "Failed on 1st ssbi_write(): rc=%d.\n", rc);
 		return 1;
@@ -387,32 +447,37 @@
 {
     unsigned char kypd_cntl_init = 0x84;
     unsigned char kypd_scan_init = 0x20;
+    int rows = (qwerty_keypad->keypad_info)->rows;
+    int columns = (qwerty_keypad->keypad_info)->columns;
+    write_func wr_function = (qwerty_keypad->keypad_info)->wr_func;
 
-    if (i2c_ssbi_write_bytes(&kypd_cntl_init, 1, SSBI_REG_KYPD_CNTL_ADDR))
+    if ((*wr_function)(&kypd_cntl_init, 1, SSBI_REG_KYPD_CNTL_ADDR))
       dprintf (CRITICAL, "Error in initializing SSBI_REG_KYPD_CNTL register\n");
 
-    if (i2c_ssbi_write_bytes(&kypd_scan_init, 1, SSBI_REG_KYPD_SCAN_ADDR))
+    if ((*wr_function)(&kypd_scan_init, 1, SSBI_REG_KYPD_SCAN_ADDR))
       dprintf (CRITICAL, "Error in initializing SSBI_REG_KYPD_SCAN register\n");
 
-    pm8058_gpio_config_kypd_sns(SSBI_OFFSET_ADDR_GPIO_KYPD_SNS, NUM_OF_KYPD_SNS_GPIOS);
-    pm8058_gpio_config_kypd_drv(SSBI_OFFSET_ADDR_GPIO_KYPD_DRV,  NUM_OF_KYPD_DRV_GPIOS);
+    pm8058_gpio_config_kypd_sns(SSBI_OFFSET_ADDR_GPIO_KYPD_SNS, columns);
+    pm8058_gpio_config_kypd_drv(SSBI_OFFSET_ADDR_GPIO_KYPD_DRV,  rows);
 }
 
 static enum handler_return
 scan_qwerty_keypad(struct timer *timer, time_t now, void *arg)
 {
-    int rows = (qwerty_keypad->keypad_info)->rows;
-    int columns = NUM_OF_KYPD_SNS_GPIOS;
+    unsigned int rows = (qwerty_keypad->keypad_info)->rows;
+    unsigned int columns = (qwerty_keypad->keypad_info)->columns;
+    unsigned int num_of_ssbi_reads = (qwerty_keypad->keypad_info)->num_of_reads;
+    read_func rd_function = (qwerty_keypad->keypad_info)->rd_func;
     unsigned char column_new_keys = 0x00;
     unsigned char column_old_keys = 0x00;
     int shift = 0;
     static int key_detected = 0;
 
-    if (i2c_ssbi_read_bytes((qwerty_keypad->keypad_info)->rec_keys, NUM_OF_SSBI_READS,
+    if ((*rd_function)((qwerty_keypad->keypad_info)->rec_keys, num_of_ssbi_reads,
                                                  SSBI_REG_KYPD_REC_DATA_ADDR))
       dprintf (CRITICAL, "Error in initializing SSBI_REG_KYPD_CNTL register\n");
 
-    if (i2c_ssbi_read_bytes((qwerty_keypad->keypad_info)->old_keys, NUM_OF_SSBI_READS,
+    if ((*rd_function)((qwerty_keypad->keypad_info)->old_keys, num_of_ssbi_reads,
                                                  SSBI_REG_KYPD_OLD_DATA_ADDR))
       dprintf (CRITICAL, "Error in initializing SSBI_REG_KYPD_CNTL register\n");
 
@@ -426,7 +491,7 @@
 	        column_old_keys = ((qwerty_keypad->keypad_info)->old_keys[rows]);
 	        if (((0x01 << columns) & (~column_new_keys))
 		    && !((0x01 << columns) & (~column_old_keys))) {
-	            shift = (rows * (qwerty_keypad->keypad_info)->columns) + columns;
+	            shift = (rows * 8) + columns;
 	            if ((qwerty_keypad->keypad_info)->keymap[shift]) {
 		      if (shift != key_detected) {
 			    key_detected = shift;
@@ -459,14 +524,14 @@
 {
     int len;
 
-    ssbi_gpio_init();
-
     len = sizeof(struct gpio_qwerty_kp);
     qwerty_keypad = malloc(len);
     ASSERT(qwerty_keypad);
 
     memset(qwerty_keypad, 0, len);
     qwerty_keypad->keypad_info = qwerty_kp;
+    ssbi_gpio_init();
+
     qwerty_keypad->num_of_scans = 0;
 
     event_init(&qwerty_keypad->full_scan, false, EVENT_FLAG_AUTOUNSIGNAL);
diff --git a/include/dev/gpio_keypad.h b/include/dev/gpio_keypad.h
index 7a87714..feada32 100644
--- a/include/dev/gpio_keypad.h
+++ b/include/dev/gpio_keypad.h
@@ -2,7 +2,7 @@
  * Copyright (c) 2008, Google Inc.
  * All rights reserved.
  *
- * Copyright (c) 2009, Code Aurora Forum. All rights reserved.
+ * Copyright (c) 2009-2010, Code Aurora Forum. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -167,9 +167,21 @@
 #define	PM8058_GPIO_FUNC_MASK		0x0E
 #define	PM8058_GPIO_FUNC_SHIFT		1
 
-#define NUM_OF_SSBI_READS               6
-#define NUM_OF_KYPD_DRV_GPIOS           5
-#define NUM_OF_KYPD_SNS_GPIOS           5
+
+/* PMIC Arbiter 1: SSBI2 Configuration Micro ARM registers */
+#define PA1_SSBI2_CMD                   0x00500000
+#define PA1_SSBI2_RD_STATUS             0x00500004
+
+#define PA1_SSBI2_REG_ADDR_SHIFT        8
+#define PA1_SSBI2_CMD_RDWRN_SHIFT       24
+#define PA1_SSBI2_TRANS_DONE_SHIFT      27
+
+#define PA1_SSBI2_REG_DATA_MASK         0xFF
+#define PA1_SSBI2_REG_DATA_SHIFT        0
+
+#define PA1_SSBI2_CMD_READ              1
+#define PA1_SSBI2_CMD_WRITE             0
+
 
 struct pm8058_gpio {
 	int		direction;
@@ -180,6 +192,9 @@
 	int		inv_int_pol;	/* invert interrupt polarity */
 };
 
+typedef int (*read_func)(unsigned char *, unsigned short, unsigned short);
+typedef int (*write_func)(unsigned char *, unsigned short, unsigned short);
+
 struct qwerty_keypad_info {
 	/* size must be ninputs * noutputs */
 	unsigned int   *keymap;
@@ -187,6 +202,9 @@
         unsigned char  *rec_keys;
         unsigned int   rows;
         unsigned int   columns;
+        unsigned int   num_of_reads;
+        read_func      rd_func;
+        write_func     wr_func;
 	/* time to wait before reading inputs after driving each output */
 	time_t         settle_time;
 	time_t         poll_time;
@@ -205,5 +223,13 @@
 	SSBI_MODE2_REG_ADDR_15_8_MASK))
 
 void ssbi_keypad_init (struct qwerty_keypad_info *);
+int i2c_ssbi_read_bytes(unsigned char  *buffer, unsigned short length,
+			unsigned short slave_addr);
+int i2c_ssbi_write_bytes(unsigned char  *buffer, unsigned short length,
+			 unsigned short slave_addr);
+int pa1_ssbi2_read_bytes(unsigned char  *buffer, unsigned short length,
+			 unsigned short slave_addr);
+int pa1_ssbi2_write_bytes(unsigned char  *buffer, unsigned short length,
+			  unsigned short slave_addr);
 
 #endif /* __DEV_GPIO_KEYPAD_H */
diff --git a/target/msm7630_surf/keypad.c b/target/msm7630_surf/keypad.c
index c7181dc..7261126 100644
--- a/target/msm7630_surf/keypad.c
+++ b/target/msm7630_surf/keypad.c
@@ -1,7 +1,7 @@
 /*
  * Copyright (c) 2009, Google Inc.
  * All rights reserved.
- * Copyright (c) 2009, Code Aurora Forum. All rights reserved.
+ * Copyright (c) 2009-2010, Code Aurora Forum. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -51,8 +51,11 @@
     .keymap         = qwerty_keymap,
     .old_keys       = qwerty_keys_old,
     .rec_keys       = qwerty_keys_new,
-    .rows           = ARRAY_SIZE(qwerty_keys_new),
-    .columns        = sizeof(unsigned char) * 8,
+    .rows           = 5,
+    .columns        = 5,
+    .num_of_reads   = 6,
+    .rd_func        = &i2c_ssbi_read_bytes,
+    .wr_func        = &i2c_ssbi_write_bytes,
     .settle_time    = 5 /* msec */,
     .poll_time	    = 20 /* msec */,
 };
diff --git a/target/msm8660_surf/init.c b/target/msm8660_surf/init.c
index 5543f3d..3362b60 100755
--- a/target/msm8660_surf/init.c
+++ b/target/msm8660_surf/init.c
@@ -50,6 +50,10 @@
 
     dprintf(INFO, "target_init()\n");
 
+#if (!ENABLE_NANDWRITE)
+	keys_init();
+	keypad_init();
+#endif
     if(mmc_boot_main())
     {
         dprintf(CRITICAL, "mmc init failed!");
diff --git a/target/msm8660_surf/keypad.c b/target/msm8660_surf/keypad.c
new file mode 100644
index 0000000..2ae3ece
--- /dev/null
+++ b/target/msm8660_surf/keypad.c
@@ -0,0 +1,60 @@
+/*
+ * Copyright (c) 2010, Code Aurora Forum. 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 Code Aurora 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 BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "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 <dev/keys.h>
+#include <dev/gpio_keypad.h>
+
+#define BITS_IN_ELEMENT(x) (sizeof(x)[0] * 8)
+
+static unsigned char qwerty_keys_old[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
+static unsigned char qwerty_keys_new[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
+
+#define KEYMAP_INDEX(row, col) (row)* BITS_IN_ELEMENT(qwerty_keys_new) + (col)
+
+static unsigned int qwerty_keymap[] = {
+    [KEYMAP_INDEX(1, 3)] = KEY_BACK,          /* Volume down key */
+};
+
+static struct qwerty_keypad_info qwerty_keypad = {
+    .keymap         = qwerty_keymap,
+    .old_keys       = qwerty_keys_old,
+    .rec_keys       = qwerty_keys_new,
+    .rows           = 6,
+    .columns        = 5,
+    .num_of_reads   = 6,
+    .rd_func        = &pa1_ssbi2_read_bytes,
+    .wr_func        = &pa1_ssbi2_write_bytes,
+    .settle_time    = 5 /* msec */,
+    .poll_time	    = 20 /* msec */,
+};
+
+void keypad_init(void)
+{
+    ssbi_keypad_init(&qwerty_keypad);
+}
diff --git a/target/msm8660_surf/rules.mk b/target/msm8660_surf/rules.mk
index 6fe811c..a871d8a 100755
--- a/target/msm8660_surf/rules.mk
+++ b/target/msm8660_surf/rules.mk
@@ -33,4 +33,5 @@
 
 OBJS += \
 	$(LOCAL_DIR)/init.o \
-	$(LOCAL_DIR)/atags.o
+	$(LOCAL_DIR)/atags.o \
+	$(LOCAL_DIR)/keypad.o