
#
# Patch managed by http://www.holgerschurig.de/patcher.html
#

--- /dev/null
+++ linux2/drivers/input/keyboard/corgikbd.c
@@ -0,0 +1,295 @@
+/*
+ *  Corgi keyboard driver for Linux/ARM
+ *
+ *  Copyright (c) 2004 Richard Purdie
+ *
+ *  Based on xtkbd.c/locomkbd.c
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License version 2 as
+ *  published by the Free Software Foundation.
+ *
+ * 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.
+ *
+ */
+
+#include <linux/config.h>
+#include <linux/slab.h>
+#include <linux/module.h>
+#include <linux/init.h>
+#include <linux/input.h>
+#include <linux/delay.h>
+#include <linux/device.h>
+#include <linux/interrupt.h>
+#include <linux/ioport.h>
+
+#include <asm/irq.h>
+
+//#include <asm/arch/irqs.h>
+#include <asm/arch/corgi.h>
+#include <asm/arch/pxa-regs.h>
+#include <asm/arch/hardware.h>
+
+#define KB_ROWS			8
+#define KB_COLS			12
+#define KB_ROWMASK(r)		(1 << (r))
+#define SCANCODE(r,c)		( ((r)<<4) + (c) + 1 )
+//#define	NR_SCANCODES	(SCANCODE(KB_ROWS-1,KB_COLS-1)+1)
+#define	NR_SCANCODES	128
+#define SCAN_INTERVAL		(HZ/10)
+#define CORGIKBD_PRESSED	1
+
+#define KEY_CALENDER	KEY_F1
+#define KEY_ADDRESS   	KEY_F2
+#define KEY_FN		   	KEY_F3 
+#define KEY_OFF			KEY_F4
+#define KEY_EXOK		KEY_F5 
+#define KEY_EXCANCEL	KEY_F6
+#define KEY_EXJOGDOWN	KEY_F7 
+#define KEY_EXJOGUP		KEY_F8 
+#define KEY_JAP1		KEY_F9 
+#define KEY_JAP2		KEY_F10
+#define KEY_OK1			KEY_F11
+#define KEY_MENU1		KEY_F12
+
+static unsigned char corgikbd_keycode[NR_SCANCODES] = {
+	0,                                                                                                                /* 0 */
+	0, KEY_1, KEY_3, KEY_5, KEY_6, KEY_7, KEY_9, KEY_0, KEY_BACKSPACE, 0, 0, 0, 0, 0, 0, 0, 	                      /* 1-16 */
+	0, KEY_2, KEY_4, KEY_R, KEY_Y, KEY_8, KEY_I, KEY_O, KEY_P, 0, 0, 0, 0, 0, 0, 0,                                   /* 17-32 */
+	KEY_TAB, KEY_Q, KEY_E, KEY_T, KEY_G, KEY_U, KEY_J, KEY_K, 0, 0, 0, 0, 0, 0, 0, 0,                                 /* 33-48 */
+	KEY_CALENDER, KEY_W, KEY_S, KEY_F, KEY_V, KEY_H, KEY_M, KEY_L, 0, KEY_RIGHTSHIFT, 0, 0, 0, 0, 0, 0,               /* 49-64 */
+	KEY_ADDRESS, KEY_A, KEY_D, KEY_C, KEY_B, KEY_N, KEY_DOT, 0, KEY_ENTER, 0, KEY_LEFTSHIFT, 0, 0, 0, 0, 0, 	  /* 65-80 */
+	KEY_MAIL, KEY_Z, KEY_X, KEY_MINUS, KEY_SPACE, KEY_COMMA, 0, KEY_UP, 0, 0, 0, KEY_FN, 0, 0, 0, 0,                 /* 81-96 */
+	KEY_HOME, KEY_JAP1, KEY_JAP2, KEY_CANCEL, KEY_OK1, KEY_MENU1, KEY_LEFT, KEY_DOWN, KEY_RIGHT, 0, 0, 0, 0, 0, 0, 0,  /* 97-112 */
+	KEY_OFF, KEY_EXOK, KEY_EXCANCEL, KEY_EXJOGDOWN, KEY_EXJOGUP, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  	                  /* 113-127 */
+};
+
+
+struct corgikbd {
+	unsigned char keycode[NR_SCANCODES];
+	struct input_dev input;
+	char phys[32];
+
+	unsigned char state[NR_SCANCODES];
+	spinlock_t lock;
+	
+	struct timer_list timer;
+};
+
+static struct corgikbd corgikbd;
+
+#define KB_DISCHARGE_DELAY	10
+#define KB_ACTIVATE_DELAY	10
+
+#define GET_ROWS_STATUS(c)       (((GPLR1 & CORGI_GPIO_HIGH_SENSE_BIT) >> CORGI_GPIO_HIGH_SENSE_RSHIFT) | ((GPLR2 & CORGI_GPIO_LOW_SENSE_BIT) << CORGI_GPIO_LOW_SENSE_LSHIFT))
+
+static inline void corgikbd_discharge_all(void)
+{
+	// STROBE All HiZ
+	GPCR2  = CORGI_GPIO_ALL_STROBE_BIT;
+	GPDR2 &= ~CORGI_GPIO_ALL_STROBE_BIT;
+}
+
+/* helper functions for reading the keyboard matrix */
+static inline void corgikbd_charge_all(void)
+{
+
+}
+
+static inline void corgikbd_activate_all(void)
+{
+	// STROBE ALL -> High
+	GPSR2  = CORGI_GPIO_ALL_STROBE_BIT;
+	GPDR2 |= CORGI_GPIO_ALL_STROBE_BIT;
+
+	udelay(KB_DISCHARGE_DELAY);
+
+	// STATE CLEAR
+	GEDR1 |= CORGI_GPIO_HIGH_SENSE_BIT;
+	GEDR2 |= CORGI_GPIO_LOW_SENSE_BIT;
+}
+
+static inline void corgikbd_activate_col(int col)
+{
+	// STROBE col -> High, not col -> HiZ
+	GPSR2 = CORGI_GPIO_STROBE_BIT(col);
+	GPDR2 = (GPDR2 & ~CORGI_GPIO_ALL_STROBE_BIT) | CORGI_GPIO_STROBE_BIT(col);
+}
+
+static inline void corgikbd_reset_col(int col)
+{
+	// STROBE col -> Low
+	GPCR2 = CORGI_GPIO_STROBE_BIT(col);
+	// STROBE col -> out, not col -> HiZ
+	GPDR2 = (GPDR2 & ~CORGI_GPIO_ALL_STROBE_BIT) | CORGI_GPIO_STROBE_BIT(col);
+}
+
+/*
+ * The corgi keyboard only generates interrupts when a key is pressed.
+ * So when a key is pressed, we enable a timer.  This timer scans the
+ * keyboard, and this is how we detect when the key is released.
+ */
+
+/* Scan the hardware keyboard and push any changes up through the input layer */
+static void corgikbd_scankeyboard(struct corgikbd *corgikbd_data, struct pt_regs *regs) 
+{
+	unsigned int row, col, rowd, scancode;
+	unsigned long flags;
+	unsigned int num_pressed;
+
+	udelay(100); //WAIT_CHATTERING_DELAY;
+
+	spin_lock_irqsave(&corgikbd_data->lock, flags);
+
+	if (regs)
+		input_regs(&corgikbd_data->input, regs);
+	
+	corgikbd_charge_all();
+
+	num_pressed = 0;
+	for (col = 0; col < KB_COLS; col++) {
+		/*
+		 * Discharge the output driver capacitatance
+		 * in the keyboard matrix. (Yes it is significant..)
+		 */
+
+		corgikbd_discharge_all();
+		udelay(KB_DISCHARGE_DELAY);
+		corgikbd_activate_col( col);
+
+		udelay(KB_ACTIVATE_DELAY);
+		
+		rowd = GET_ROWS_STATUS(col);
+		for (row = 0; row < KB_ROWS; row++ ) {
+			scancode = SCANCODE(row, col);
+			if (rowd & KB_ROWMASK(row)) {
+				num_pressed += 1;
+				if (!(corgikbd_data->state[scancode] & CORGIKBD_PRESSED)) {
+					corgikbd_data->state[scancode] |= CORGIKBD_PRESSED;
+					printk("kbd_press=%d\n",scancode);
+					input_report_key(&corgikbd_data->input, corgikbd_data->keycode[scancode], 1);
+				}
+			} else {
+				if (corgikbd_data->state[scancode] & CORGIKBD_PRESSED) {
+					corgikbd_data->state[scancode] &= ~CORGIKBD_PRESSED;
+					printk("kbd_release=%d\n",scancode);
+					input_report_key(&corgikbd_data->input, corgikbd_data->keycode[scancode], 0);
+				}
+			}
+		}
+		corgikbd_reset_col(col);
+	}
+	
+	corgikbd_activate_all();
+
+	input_sync(&corgikbd_data->input);
+
+	/* if any keys are pressed, enable the timer */
+	if (num_pressed)
+		mod_timer(&corgikbd_data->timer, jiffies + SCAN_INTERVAL);
+
+	spin_unlock_irqrestore(&corgikbd_data->lock, flags);
+}
+
+/* 
+ * corgi keyboard interrupt handler.
+ */
+static irqreturn_t corgikbd_interrupt(int irq, void *dev_id, struct pt_regs *regs)
+{
+	struct corgikbd *corgikbd_data = dev_id;
+	/** wait chattering delay **/
+	udelay(100);
+
+	corgikbd_scankeyboard(corgikbd_data, regs);
+
+	return IRQ_HANDLED;
+}
+
+/*
+ * corgi timer checking for released keys
+ */
+static void corgikbd_timer_callback(unsigned long data)
+{
+	struct corgikbd *corgikbd_data = (struct corgikbd *) data;
+	corgikbd_scankeyboard(corgikbd_data, NULL);
+}
+
+
+static int __init corgikbd_init(void) {
+
+	int i;
+
+	spin_lock_init(corgikbd.lock);
+
+	init_timer(&corgikbd.timer);
+	corgikbd.timer.function = corgikbd_timer_callback;
+	corgikbd.timer.data = (unsigned long) &corgikbd;
+
+	corgikbd.input.evbit[0] = BIT(EV_KEY) | BIT(EV_REP);
+	
+	init_input_dev(&corgikbd.input);
+	corgikbd.input.keycode = corgikbd.keycode;
+	corgikbd.input.keycodesize = sizeof(unsigned char);
+	corgikbd.input.keycodemax = ARRAY_SIZE(corgikbd_keycode);
+	corgikbd.input.private = &corgikbd;
+
+	memcpy(corgikbd.keycode, corgikbd_keycode, sizeof(corgikbd.keycode));
+	for (i = 0; i < NR_SCANCODES; i++)
+		set_bit(corgikbd.keycode[i], corgikbd.input.keybit);
+	clear_bit(0, corgikbd.input.keybit);
+
+	strcpy(corgikbd.phys, "corgikbd/input0");
+
+	corgikbd.input.name = "corgi keyboard";
+	corgikbd.input.phys = corgikbd.phys;
+	corgikbd.input.id.bustype = BUS_XTKBD;
+	corgikbd.input.id.vendor = 0x0001;
+	corgikbd.input.id.product = 0x0001;
+	corgikbd.input.id.version = 0x0100;
+
+	// STROBE ALL -> High
+	// SENSE ALL -> In
+	GPSR2    = CORGI_GPIO_ALL_STROBE_BIT;
+	GPDR1   &= ~CORGI_GPIO_HIGH_SENSE_BIT;
+	GPDR2   &= ~CORGI_GPIO_LOW_SENSE_BIT;
+	GPDR2   |= CORGI_GPIO_ALL_STROBE_BIT;
+	GEDR1   |= CORGI_GPIO_HIGH_SENSE_BIT;
+	GEDR2   |= CORGI_GPIO_LOW_SENSE_BIT;
+	GAFR1_U &= ~CORGI_GAFR_HIGH_SENSE_BIT;
+	GAFR2_L &= ~(CORGI_GAFR_LOW_SENSE_BIT | CORGI_GAFR_ALL_STROBE_BIT);
+
+	/* SENSE:RisingEdge */
+	for (i = 0; i < CORGI_KEY_SENSE_NUM; i++) {
+		set_irq_type(CORGI_IRQ_GPIO_KEY_SENSE(i),IRQT_RISING);						
+		if (request_irq(CORGI_IRQ_GPIO_KEY_SENSE(i), corgikbd_interrupt,
+						SA_INTERRUPT, "corgikbd", &corgikbd)) {
+			printk("corgikbd: Can't get IRQ: %d for keyboard!\n", i);
+		}
+		//else printk("corgikbd: Got IRQ: %d for keyboard!\n", i);
+	}
+
+	input_register_device(&corgikbd.input);
+
+	printk(KERN_INFO "input: corgi keyboard on corgikbd\n");
+
+	return 0;
+}
+
+static void __exit corgikbd_exit(void) {
+	
+	int i;
+	for (i = 0; i < CORGI_KEY_SENSE_NUM; i++) {
+		free_irq(CORGI_IRQ_GPIO_KEY_SENSE(i),&corgikbd); 
+	}
+	input_unregister_device(&corgikbd.input);
+}
+
+module_init(corgikbd_init);
+module_exit(corgikbd_exit);
+
+MODULE_AUTHOR("Richard Purdie <rpurdie@rpsys.net>");
+MODULE_DESCRIPTION("Corgi keyboard driver");
+MODULE_LICENSE("GPL");
--- linux2/drivers/input/keyboard/Makefile~rp-corgikbd-r0.patch
+++ linux2/drivers/input/keyboard/Makefile
@@ -13,3 +13,4 @@
 obj-$(CONFIG_KEYBOARD_LOCOMO)		+= locomokbd.o
 obj-$(CONFIG_KEYBOARD_NEWTON)		+= newtonkbd.o
 obj-$(CONFIG_KEYBOARD_98KBD)		+= 98kbd.o
+obj-$(CONFIG_KEYBOARD_CORGI)		+= corgikbd.o
--- linux2/drivers/input/keyboard/Kconfig~rp-corgikbd-r0.patch
+++ linux2/drivers/input/keyboard/Kconfig
@@ -63,6 +63,15 @@
 	  To compile this driver as a module, choose M here: the
 	  module will be called locomokbd.
 
+config KEYBOARD_CORGI
+	tristate "Corgi Keyboard Support"
+	depends on INPUT && INPUT_KEYBOARD
+	help
+	  Say Y here if you are running Linux on a Sharp Zaurus SL-C7xx (Corgi,Shepherd,Husky or Boxer)
+
+	  To compile this driver as a module, choose M here: the 
+	  module will be called corgikbd.
+
 config KEYBOARD_XTKBD
 	tristate "XT Keyboard support"
 	depends on INPUT && INPUT_KEYBOARD

