corgikbd.c 12.1 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 *  Keyboard driver for Sharp Corgi models (SL-C7xx)
 *
 *  Copyright (c) 2004-2005 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.
 *
 */

#include <linux/delay.h>
15
#include <linux/platform_device.h>
L
Linus Torvalds 已提交
16 17 18
#include <linux/init.h>
#include <linux/input.h>
#include <linux/interrupt.h>
19
#include <linux/jiffies.h>
L
Linus Torvalds 已提交
20 21 22
#include <linux/module.h>
#include <linux/slab.h>

23 24 25 26
#include <mach/corgi.h>
#include <mach/hardware.h>
#include <mach/pxa-regs.h>
#include <mach/pxa2xx-gpio.h>
L
Linus Torvalds 已提交
27 28 29 30 31 32
#include <asm/hardware/scoop.h>

#define KB_ROWS				8
#define KB_COLS				12
#define KB_ROWMASK(r)		(1 << (r))
#define SCANCODE(r,c)		( ((r)<<4) + (c) + 1 )
33 34
/* zero code, 124 scancodes */
#define	NR_SCANCODES		( SCANCODE(KB_ROWS-1,KB_COLS-1) +1 +1 )
L
Linus Torvalds 已提交
35

36 37
#define SCAN_INTERVAL		(50) /* ms */
#define HINGE_SCAN_INTERVAL	(250) /* ms */
L
Linus Torvalds 已提交
38 39 40 41

#define CORGI_KEY_CALENDER	KEY_F1
#define CORGI_KEY_ADDRESS	KEY_F2
#define CORGI_KEY_FN		KEY_F3
42
#define CORGI_KEY_CANCEL	KEY_F4
L
Linus Torvalds 已提交
43 44 45 46 47 48 49
#define CORGI_KEY_OFF		KEY_SUSPEND
#define CORGI_KEY_EXOK		KEY_F5
#define CORGI_KEY_EXCANCEL	KEY_F6
#define CORGI_KEY_EXJOGDOWN	KEY_F7
#define CORGI_KEY_EXJOGUP	KEY_F8
#define CORGI_KEY_JAP1		KEY_LEFTCTRL
#define CORGI_KEY_JAP2		KEY_LEFTALT
50
#define CORGI_KEY_MAIL		KEY_F10
L
Linus Torvalds 已提交
51 52 53 54 55 56 57 58 59 60
#define CORGI_KEY_OK		KEY_F11
#define CORGI_KEY_MENU		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 */
	CORGI_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 */
	CORGI_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 */
61 62
	CORGI_KEY_MAIL, KEY_Z, KEY_X, KEY_MINUS, KEY_SPACE, KEY_COMMA, 0, KEY_UP, 0, 0, 0, CORGI_KEY_FN, 0, 0, 0, 0,            /* 81-96 */
	KEY_SYSRQ, CORGI_KEY_JAP1, CORGI_KEY_JAP2, CORGI_KEY_CANCEL, CORGI_KEY_OK, CORGI_KEY_MENU, KEY_LEFT, KEY_DOWN, KEY_RIGHT, 0, 0, 0, 0, 0, 0, 0,  /* 97-112 */
L
Linus Torvalds 已提交
63 64 65 66 67 68
	CORGI_KEY_OFF, CORGI_KEY_EXOK, CORGI_KEY_EXCANCEL, CORGI_KEY_EXJOGDOWN, CORGI_KEY_EXJOGUP, 0, 0, 0, 0, 0, 0, 0,   /* 113-124 */
};


struct corgikbd {
	unsigned char keycode[ARRAY_SIZE(corgikbd_keycode)];
69
	struct input_dev *input;
L
Linus Torvalds 已提交
70 71 72 73

	spinlock_t lock;
	struct timer_list timer;
	struct timer_list htimer;
74 75 76

	unsigned int suspended;
	unsigned long suspend_jiffies;
L
Linus Torvalds 已提交
77 78 79 80 81 82 83 84 85 86 87 88 89 90
};

#define KB_DISCHARGE_DELAY	10
#define KB_ACTIVATE_DELAY	10

/* Helper functions for reading the keyboard matrix
 * Note: We should really be using pxa_gpio_mode to alter GPDR but it
 *       requires a function call per GPIO bit which is excessive
 *       when we need to access 12 bits at once multiple times.
 * These functions must be called within local_irq_save()/local_irq_restore()
 * or similar.
 */
static inline void corgikbd_discharge_all(void)
{
91
	/* STROBE All HiZ */
L
Linus Torvalds 已提交
92 93 94 95 96 97
	GPCR2  = CORGI_GPIO_ALL_STROBE_BIT;
	GPDR2 &= ~CORGI_GPIO_ALL_STROBE_BIT;
}

static inline void corgikbd_activate_all(void)
{
98
	/* STROBE ALL -> High */
L
Linus Torvalds 已提交
99 100 101 102 103
	GPSR2  = CORGI_GPIO_ALL_STROBE_BIT;
	GPDR2 |= CORGI_GPIO_ALL_STROBE_BIT;

	udelay(KB_DISCHARGE_DELAY);

104
	/* Clear any interrupts we may have triggered when altering the GPIO lines */
L
Linus Torvalds 已提交
105 106 107 108 109 110
	GEDR1 = CORGI_GPIO_HIGH_SENSE_BIT;
	GEDR2 = CORGI_GPIO_LOW_SENSE_BIT;
}

static inline void corgikbd_activate_col(int col)
{
111
	/* STROBE col -> High, not col -> HiZ */
L
Linus Torvalds 已提交
112 113 114 115 116 117
	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)
{
118
	/* STROBE col -> Low */
L
Linus Torvalds 已提交
119
	GPCR2 = CORGI_GPIO_STROBE_BIT(col);
120
	/* STROBE col -> out, not col -> HiZ */
L
Linus Torvalds 已提交
121 122 123 124 125 126 127 128 129 130 131 132
	GPDR2 = (GPDR2 & ~CORGI_GPIO_ALL_STROBE_BIT) | CORGI_GPIO_STROBE_BIT(col);
}

#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))

/*
 * The corgi keyboard only generates interrupts when a key is pressed.
 * When a key is pressed, we enable a timer which then scans the
 * keyboard to detect when the key is released.
 */

/* Scan the hardware keyboard and push any changes up through the input layer */
133
static void corgikbd_scankeyboard(struct corgikbd *corgikbd_data)
L
Linus Torvalds 已提交
134
{
135
	unsigned int row, col, rowd;
L
Linus Torvalds 已提交
136 137 138
	unsigned long flags;
	unsigned int num_pressed;

139 140 141
	if (corgikbd_data->suspended)
		return;

L
Linus Torvalds 已提交
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
	spin_lock_irqsave(&corgikbd_data->lock, flags);

	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++) {
159 160
			unsigned int scancode, pressed;

L
Linus Torvalds 已提交
161
			scancode = SCANCODE(row, col);
162 163
			pressed = rowd & KB_ROWMASK(row);

164
			input_report_key(corgikbd_data->input, corgikbd_data->keycode[scancode], pressed);
165 166

			if (pressed)
L
Linus Torvalds 已提交
167
				num_pressed++;
168 169 170

			if (pressed && (corgikbd_data->keycode[scancode] == CORGI_KEY_OFF)
					&& time_after(jiffies, corgikbd_data->suspend_jiffies + HZ)) {
171
				input_event(corgikbd_data->input, EV_PWR, CORGI_KEY_OFF, 1);
172 173
				corgikbd_data->suspend_jiffies=jiffies;
			}
L
Linus Torvalds 已提交
174 175 176 177 178 179
		}
		corgikbd_reset_col(col);
	}

	corgikbd_activate_all();

180
	input_sync(corgikbd_data->input);
L
Linus Torvalds 已提交
181 182 183

	/* if any keys are pressed, enable the timer */
	if (num_pressed)
184
		mod_timer(&corgikbd_data->timer, jiffies + msecs_to_jiffies(SCAN_INTERVAL));
L
Linus Torvalds 已提交
185 186 187 188 189 190 191

	spin_unlock_irqrestore(&corgikbd_data->lock, flags);
}

/*
 * corgi keyboard interrupt handler.
 */
192
static irqreturn_t corgikbd_interrupt(int irq, void *dev_id)
L
Linus Torvalds 已提交
193 194 195 196 197 198
{
	struct corgikbd *corgikbd_data = dev_id;

	if (!timer_pending(&corgikbd_data->timer)) {
		/** wait chattering delay **/
		udelay(20);
199
		corgikbd_scankeyboard(corgikbd_data);
L
Linus Torvalds 已提交
200 201 202 203 204 205 206 207 208 209 210
	}

	return IRQ_HANDLED;
}

/*
 * corgi timer checking for released keys
 */
static void corgikbd_timer_callback(unsigned long data)
{
	struct corgikbd *corgikbd_data = (struct corgikbd *) data;
211
	corgikbd_scankeyboard(corgikbd_data);
L
Linus Torvalds 已提交
212 213 214 215 216 217
}

/*
 * The hinge switches generate no interrupt so they need to be
 * monitored by a timer.
 *
218 219 220 221 222
 * We debounce the switches and pass them to the input system.
 *
 *  gprr == 0x00 - Keyboard with Landscape Screen
 *          0x08 - No Keyboard with Portrait Screen
 *          0x0c - Keyboard and Screen Closed
L
Linus Torvalds 已提交
223 224
 */

225
#define READ_GPIO_BIT(x)    (GPLR(x) & GPIO_bit(x))
L
Linus Torvalds 已提交
226 227 228 229 230 231 232 233 234 235
#define HINGE_STABLE_COUNT 2
static int sharpsl_hinge_state;
static int hinge_count;

static void corgikbd_hinge_timer(unsigned long data)
{
	struct corgikbd *corgikbd_data = (struct corgikbd *) data;
	unsigned long gprr;
	unsigned long flags;

236
	gprr = read_scoop_reg(&corgiscoop_device.dev, SCOOP_GPRR) & (CORGI_SCP_SWA | CORGI_SCP_SWB);
237
	gprr |= (READ_GPIO_BIT(CORGI_GPIO_AK_INT) != 0);
L
Linus Torvalds 已提交
238 239 240 241 242 243 244 245
	if (gprr != sharpsl_hinge_state) {
		hinge_count = 0;
		sharpsl_hinge_state = gprr;
	} else if (hinge_count < HINGE_STABLE_COUNT) {
		hinge_count++;
		if (hinge_count >= HINGE_STABLE_COUNT) {
			spin_lock_irqsave(&corgikbd_data->lock, flags);

246 247 248
			input_report_switch(corgikbd_data->input, SW_LID, ((sharpsl_hinge_state & CORGI_SCP_SWA) != 0));
			input_report_switch(corgikbd_data->input, SW_TABLET_MODE, ((sharpsl_hinge_state & CORGI_SCP_SWB) != 0));
			input_report_switch(corgikbd_data->input, SW_HEADPHONE_INSERT, (READ_GPIO_BIT(CORGI_GPIO_AK_INT) != 0));
249
			input_sync(corgikbd_data->input);
L
Linus Torvalds 已提交
250 251 252 253

			spin_unlock_irqrestore(&corgikbd_data->lock, flags);
		}
	}
254
	mod_timer(&corgikbd_data->htimer, jiffies + msecs_to_jiffies(HINGE_SCAN_INTERVAL));
L
Linus Torvalds 已提交
255 256
}

257
#ifdef CONFIG_PM
258
static int corgikbd_suspend(struct platform_device *dev, pm_message_t state)
259
{
260
	int i;
261
	struct corgikbd *corgikbd = platform_get_drvdata(dev);
262

263
	corgikbd->suspended = 1;
264 265 266 267
	/* strobe 0 is the power key so this can't be made an input for
	   powersaving therefore i = 1 */
	for (i = 1; i < CORGI_KEY_STROBE_NUM; i++)
		pxa_gpio_mode(CORGI_GPIO_KEY_STROBE(i) | GPIO_IN);
268

269 270 271
	return 0;
}

272
static int corgikbd_resume(struct platform_device *dev)
273
{
274
	int i;
275
	struct corgikbd *corgikbd = platform_get_drvdata(dev);
276

277 278 279
	for (i = 1; i < CORGI_KEY_STROBE_NUM; i++)
		pxa_gpio_mode(CORGI_GPIO_KEY_STROBE(i) | GPIO_OUT | GPIO_DFLT_HIGH);

280 281 282
	/* Upon resume, ignore the suspend key for a short while */
	corgikbd->suspend_jiffies=jiffies;
	corgikbd->suspended = 0;
283 284 285 286 287 288 289 290

	return 0;
}
#else
#define corgikbd_suspend	NULL
#define corgikbd_resume		NULL
#endif

291
static int __init corgikbd_probe(struct platform_device *pdev)
L
Linus Torvalds 已提交
292 293
{
	struct corgikbd *corgikbd;
294
	struct input_dev *input_dev;
295
	int i, err = -ENOMEM;
L
Linus Torvalds 已提交
296

297
	corgikbd = kzalloc(sizeof(struct corgikbd), GFP_KERNEL);
298
	input_dev = input_allocate_device();
299 300
	if (!corgikbd || !input_dev)
		goto fail;
L
Linus Torvalds 已提交
301

302
	platform_set_drvdata(pdev, corgikbd);
L
Linus Torvalds 已提交
303

304
	corgikbd->input = input_dev;
305
	spin_lock_init(&corgikbd->lock);
L
Linus Torvalds 已提交
306 307 308 309 310 311 312 313 314 315 316

	/* Init Keyboard rescan timer */
	init_timer(&corgikbd->timer);
	corgikbd->timer.function = corgikbd_timer_callback;
	corgikbd->timer.data = (unsigned long) corgikbd;

	/* Init Hinge Timer */
	init_timer(&corgikbd->htimer);
	corgikbd->htimer.function = corgikbd_hinge_timer;
	corgikbd->htimer.data = (unsigned long) corgikbd;

317 318
	corgikbd->suspend_jiffies=jiffies;

L
Linus Torvalds 已提交
319
	memcpy(corgikbd->keycode, corgikbd_keycode, sizeof(corgikbd->keycode));
320 321 322 323 324 325 326

	input_dev->name = "Corgi Keyboard";
	input_dev->phys = "corgikbd/input0";
	input_dev->id.bustype = BUS_HOST;
	input_dev->id.vendor = 0x0001;
	input_dev->id.product = 0x0001;
	input_dev->id.version = 0x0100;
327
	input_dev->dev.parent = &pdev->dev;
328

329 330
	input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP) |
		BIT_MASK(EV_PWR) | BIT_MASK(EV_SW);
331 332 333 334
	input_dev->keycode = corgikbd->keycode;
	input_dev->keycodesize = sizeof(unsigned char);
	input_dev->keycodemax = ARRAY_SIZE(corgikbd_keycode);

L
Linus Torvalds 已提交
335
	for (i = 0; i < ARRAY_SIZE(corgikbd_keycode); i++)
336 337
		set_bit(corgikbd->keycode[i], input_dev->keybit);
	clear_bit(0, input_dev->keybit);
338 339 340
	set_bit(SW_LID, input_dev->swbit);
	set_bit(SW_TABLET_MODE, input_dev->swbit);
	set_bit(SW_HEADPHONE_INSERT, input_dev->swbit);
341

342 343 344
	err = input_register_device(corgikbd->input);
	if (err)
		goto fail;
L
Linus Torvalds 已提交
345

346
	mod_timer(&corgikbd->htimer, jiffies + msecs_to_jiffies(HINGE_SCAN_INTERVAL));
L
Linus Torvalds 已提交
347 348 349 350 351

	/* Setup sense interrupts - RisingEdge Detect, sense lines as inputs */
	for (i = 0; i < CORGI_KEY_SENSE_NUM; i++) {
		pxa_gpio_mode(CORGI_GPIO_KEY_SENSE(i) | GPIO_IN);
		if (request_irq(CORGI_IRQ_GPIO_KEY_SENSE(i), corgikbd_interrupt,
352
				IRQF_DISABLED | IRQF_TRIGGER_RISING,
R
Russell King 已提交
353
				"corgikbd", corgikbd))
L
Linus Torvalds 已提交
354 355 356 357 358 359 360
			printk(KERN_WARNING "corgikbd: Can't get IRQ: %d!\n", i);
	}

	/* Set Strobe lines as outputs - set high */
	for (i = 0; i < CORGI_KEY_STROBE_NUM; i++)
		pxa_gpio_mode(CORGI_GPIO_KEY_STROBE(i) | GPIO_OUT | GPIO_DFLT_HIGH);

361 362 363
	/* Setup the headphone jack as an input */
	pxa_gpio_mode(CORGI_GPIO_AK_INT | GPIO_IN);

L
Linus Torvalds 已提交
364
	return 0;
365 366 367 368

 fail:	input_free_device(input_dev);
	kfree(corgikbd);
	return err;
L
Linus Torvalds 已提交
369 370
}

371
static int corgikbd_remove(struct platform_device *pdev)
L
Linus Torvalds 已提交
372 373
{
	int i;
374
	struct corgikbd *corgikbd = platform_get_drvdata(pdev);
L
Linus Torvalds 已提交
375 376 377 378 379 380 381

	for (i = 0; i < CORGI_KEY_SENSE_NUM; i++)
		free_irq(CORGI_IRQ_GPIO_KEY_SENSE(i), corgikbd);

	del_timer_sync(&corgikbd->htimer);
	del_timer_sync(&corgikbd->timer);

382
	input_unregister_device(corgikbd->input);
L
Linus Torvalds 已提交
383 384 385 386 387 388

	kfree(corgikbd);

	return 0;
}

389
static struct platform_driver corgikbd_driver = {
L
Linus Torvalds 已提交
390 391
	.probe		= corgikbd_probe,
	.remove		= corgikbd_remove,
392 393
	.suspend	= corgikbd_suspend,
	.resume		= corgikbd_resume,
394 395
	.driver		= {
		.name	= "corgi-keyboard",
396
		.owner	= THIS_MODULE,
397
	},
L
Linus Torvalds 已提交
398 399 400 401
};

static int __devinit corgikbd_init(void)
{
402
	return platform_driver_register(&corgikbd_driver);
L
Linus Torvalds 已提交
403 404 405 406
}

static void __exit corgikbd_exit(void)
{
407
	platform_driver_unregister(&corgikbd_driver);
L
Linus Torvalds 已提交
408 409 410 411 412 413 414
}

module_init(corgikbd_init);
module_exit(corgikbd_exit);

MODULE_AUTHOR("Richard Purdie <rpurdie@rpsys.net>");
MODULE_DESCRIPTION("Corgi Keyboard Driver");
415
MODULE_LICENSE("GPL v2");
416
MODULE_ALIAS("platform:corgi-keyboard");