aboutsummaryrefslogtreecommitdiff
path: root/drivers/input/keyboard/u8500-kpd.c
blob: 5adb0f60571f7261fb19f8690050eef088aa3ae4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/*
 * Overview:
 *	Keypad driver for u8500 platform
 *
 * Copyright (C) 2009 STMicroelectronics Pvt. Ltd.
 * Copyright (C) 2009 ST-Ericsson SA
 *
 * 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; either version 2 of the License, or
 * (at your option) any later version.
 */

/* Keypad driver Version */
#define KEYPAD_VER_X		4
#define KEYPAD_VER_Y		0
#define KEYPAD_VER_Z		0

#include <asm/mach/irq.h>
#include <asm/irq.h>
#include <linux/ioport.h>
#include <linux/errno.h>
#include <linux/spinlock.h>
#include <linux/slab.h>
#include <linux/platform_device.h>

#include <linux/input.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/workqueue.h>
#include <linux/wait.h>
#include <linux/completion.h>
#include <linux/bitops.h>
#include <mach/hardware.h>
#include <mach/debug.h>
#include <mach/kpd.h>


#define KEYPAD_NAME		"DRIVER KEYPAD"

extern struct driver_debug_st DBG_ST;
/*function declarations h/w independent*/
irqreturn_t u8500_kp_intrhandler(/*int irq, */void *dev_id);
static void u8500_kp_wq_kscan(struct work_struct *work);

/*
 * Module parameter defination to pass mode of operation
 * 0 = to initialize driver in Interrupt mode (default mode)
 * 1 = to Intialize driver in polling mode of operation
 */
int kpmode;
module_param(kpmode, int, 0);
MODULE_PARM_DESC(kpmode, "Keypad Operating mode (INT/POLL)=(0/1)");

/*static*/ DEFINE_MUTEX(u8500_keymap_mutex);
/*static*/ u8 u8500_keymap = 0; /* Default keymap is keymap 0 */

static ssize_t u8500_keymap_show(struct device *dev,
			      struct device_attribute *attr, char *buf)
{
	return sprintf(buf, "%u\n", u8500_keymap);
}

static ssize_t u8500_keymap_store(struct device *dev,
				  struct device_attribute *attr,
				  const char *buf, size_t count)
{
	int keymap;

	if (sscanf(buf, "%u", &keymap) != 1)
		return -EINVAL;

	if ((keymap != 0) && (keymap != 1))
		return -EINVAL;

	mutex_lock(&u8500_keymap_mutex);
	if (keymap != u8500_keymap)
		u8500_keymap = keymap;
	mutex_unlock(&u8500_keymap_mutex);

	return strnlen(buf, count);
}

static DEVICE_ATTR(keymap, S_IRUGO | S_IWUSR, u8500_keymap_show,
		   u8500_keymap_store);



#if 0
/*
 * u8500_kp_intrhandler - keypad interrupt handler
 *
 * checks for valid interrupt, disables interrupt to avoid any nested interrupt
 * starts work queue for further key processing with debouncing logic
 */
irqreturn_t u8500_kp_intrhandler(/*int irq, */void *dev_id)
{
	struct keypad_t *kp = (struct keypad_t *)dev_id;
	/*if (irq != kp->irq) return IRQ_NONE;*/
	if (!(test_bit(KPINTR_LKBIT, &kp->lockbits))) {
		stm_dbg2(DBG_ST.keypad, "Kp interrupt");
		____atomic_set_bit(KPINTR_LKBIT, &kp->lockbits);

		if (kp->board->irqdis_int)
			kp->board->irqdis_int(kp);
		/* schedule_delayed_work(&kp->kscan_work,
					    kp->board->debounce_period); */
		schedule_work(&kp->kscan_work);
	}
	return IRQ_HANDLED;
}
EXPORT_SYMBOL(u8500_kp_intrhandler);
#endif

/**
 * u8500_kp_wq_kscan - work queue for keypad scanning
 * @work:	pointer to keypad data
 *
 * Executes at each scan tick, execute the key press/release function,
 * Generates key press/release event message for input subsystem for valid key
 * events, enables keypad interrupts (for int mode)
 */

static void u8500_kp_wq_kscan(struct work_struct *work)
{
	int key_cnt = 0;
	struct keypad_t *kp = container_of((struct delayed_work *)work,
					   struct keypad_t, kscan_work);

	if (!kp->mode && kp->board->irqdis)
		kp->board->irqdis(kp);
	if (kp->board->autoscan_results)
		key_cnt = kp->board->autoscan_results(kp);
	else
		printk(KERN_ERR"key scan function not found");

	if (kp->mode) {
		if (0 == key_cnt) {
			/*if no key is pressed and polling mode */
			schedule_delayed_work(&kp->kscan_work,
					      KEYPAD_SCAN_PERIOD);
		} else {
			/*if key is pressed and hold condition */
			printk("Work queue: pressed and hold\n");
			schedule_delayed_work(&kp->kscan_work,
					      KEYPAD_RELEASE_PERIOD);
		}
	} else {
		/**
		 * if interrupt mode then just enable iterrupt and return
		*/
		clear_bit(KPINTR_LKBIT, &kp->lockbits);
		if (kp->board->irqen)
			kp->board->irqen(kp);
	}

}

/**
 * u8500_kp_init_keypad - keypad parameter initialization
 * @kp:		pointer to keypad data
 *
 * Initializes Keybits to enable keyevents
 * Initializes Initial keypress status to default
 * Calls the keypad platform specific init function.
 */
int __init u8500_kp_init_keypad(struct keypad_t *kp)
{
	int row, column, err = 0;
	u8 *p_kcode = kp->board->kcode_tbl;

	if (kp->board->init)
		err = kp->board->init(kp);
	if (err)
		return err;

	for (row = 0; row < MAX_KPROW; row++) {
		for (column = 0; column < MAX_KPCOL; column++) {
			/*set keybits for the keycodes in use */
			set_bit(*p_kcode, kp->inp_dev->keybit);
			/*set key status to default value */
			kp->key_state[row][column] = KEYPAD_STATE_DEFAULT;
			p_kcode++;
		}
	}
	return err;
}

#ifdef CONFIG_PM
/**
 * u8500_kp_suspend - suspend keypad
 * @pdev:       platform data
 * @state:	power down level
 */
int u8500_kp_suspend(struct platform_device *pdev, pm_message_t state)
{
	struct keypad_t *kp = platform_get_drvdata(pdev);
	if (kpmode) {
		printk(KERN_INFO "Enabling interrupt \n");
		kp->board->irqen(kp);
	}
	if (!device_may_wakeup(&pdev->dev)) {
		printk(KERN_INFO "Disabling interrupt\n");
		if (kp->board->irqdis)
			kp->board->irqdis(kp);
		if (kp->board->irqdis_int)
			kp->board->irqdis_int(kp);
	}
	return 0;
}

/**
 * u8500_kp_resume - resumes keypad
 * @pdev:       platform data
 */

int u8500_kp_resume(struct platform_device *pdev)
{	struct keypad_t *kp = platform_get_drvdata(pdev);
	if (kpmode) {
		printk(KERN_INFO "Disabling interrupt\n");
		if (kp->board->irqdis)
			kp->board->irqdis(kp);
		if (kp->board->irqdis_int)
			kp->board->irqdis_int(kp);
	}
	if (!device_may_wakeup(&pdev->dev)) {
		printk(KERN_INFO "Enabling interrupt\n");
		kp->board->irqen(kp);
	}
	return 0;
}

#else
#define u8500_kp_suspend NULL
#define u8500_kp_resume NULL
#endif				/* CONFIG_PM */

/**
 * u8500_kp_probe - keypad module probe function
 * @pdev:	driver platform data
 *
 * Allocates data memory, registers the module with input subsystem,
 * initializes keypad default condition, initializes keypad interrupt handler
 * for interrupt mode operation, initializes keypad work queues functions for
 * polling mode operation
 */
static int __init u8500_kp_probe(struct platform_device *pdev)
{
	struct keypad_t *kp;
	int err = 0;
	struct keypad_device *keypad_board;
	printk(KERN_INFO "\nkeypad probe called");

	if (!pdev)
		return -EINVAL;
	keypad_board = pdev->dev.platform_data;
	kp = kzalloc(sizeof(struct keypad_t), GFP_KERNEL);
	if (!kp) {
		err = -ENOMEM;
		goto err_kzalloc;
	}
	platform_set_drvdata(pdev, kp);
	kp = platform_get_drvdata(pdev);
	kp->irq = platform_get_irq(pdev, 0);
	if (!kp->irq) {
		printk(KERN_ERR "keypad irq not defined");
		err = -1;
		goto err_board;
	}
	printk("\nkp_probe 1");
	if (!keypad_board) {
		printk(KERN_ERR "keypad platform data not defined");
		err = -1;
		goto err_board;
	}
	printk("\nkp_probe 2");
	kp->board = keypad_board;
	kp->mode = kpmode;
	kp->inp_dev = input_allocate_device();
	if (!kp->inp_dev) {
		printk(KERN_ERR "Could not allocate memory for the device");
		err = -1;
		goto err_inp_devalloc;
	}
	printk("\nkp_probe 3");

	kp->inp_dev->evbit[0] = BIT(EV_KEY);
	kp->inp_dev->name = pdev->name;
	kp->inp_dev->phys = "stkpd/input0";

	kp->inp_dev->id.product = KEYPAD_VER_X;
	kp->inp_dev->id.version = KEYPAD_VER_Y * 0x0ff + KEYPAD_VER_Z;
	/* kp->inp_dev->private = kp; */

	clear_bit(KPINTR_LKBIT, &kp->lockbits);

	err = u8500_kp_init_keypad(kp);
	if (err)
		goto err_init_kpd;
	printk("\nkp_probe 4");

	err = device_create_file(&pdev->dev, &dev_attr_keymap);
	if (err < 0) {
		printk(KERN_ERR "\nCould not create sysfs file for keypad");
		goto err_create_sysfs_entry;
	}

	if (input_register_device(kp->inp_dev) < 0) {
		printk(KERN_ERR "Could not register input device");
		err = -1;
		goto err_inp_reg;
	} else {
		printk("Registered keypad module with input subsystem");
	}
	printk("\nkp_probe 5");

	/* Initialize keypad workques  */
	INIT_DELAYED_WORK(&kp->kscan_work, u8500_kp_wq_kscan);

	/* Enable wakeup from keypad */
	device_init_wakeup(&pdev->dev, kp->board->enable_wakeup);
	device_set_wakeup_capable(&pdev->dev, kp->board->enable_wakeup);

	/* Initialize keypad interrupt handler  */
	if (!kp->mode) {	/* true if interrupt mode operation */
#if 0
		err = request_irq(kp->irq, u8500_kp_intrhandler,
				  kp->board->irqtype, kp->inp_dev->name, kp);
		if (err) {
			stm_error("Could not allocate irq %d for keypad",
				   kp->irq);
			printk(KERN_ERR "ERROR = %d\n", err);
			goto err_req_irq;
		}
		kp->board->irqen(kp);
		kp->board->autoscan_en();
#endif
	} else {
		/* Schedule workqueue for polling mode operaion. */
		/* kp->board->autoscan_en(); */
		schedule_delayed_work(&kp->kscan_work, KEYPAD_SCAN_PERIOD);
		printk(KERN_INFO "Keypad polling started");
	}

	printk(KERN_INFO "Module initialized Ver(%d.%d.%d)",
		 KEYPAD_VER_X, KEYPAD_VER_Y, KEYPAD_VER_Z);
	return 0;

#if 0
err_req_irq:
#endif
err_create_sysfs_entry:
	device_remove_file(&pdev->dev, &dev_attr_keymap);
err_inp_reg:
	/* unregistering device */
	input_unregister_device(kp->inp_dev);
err_inp_devalloc:
err_init_kpd:
	input_free_device(kp->inp_dev);
err_board:
	kfree(kp);
err_kzalloc:
	return err;
}

/**
 * u8500_kp_remove - keypad module remove function
 *
 * @pdev:	driver platform data
 *
 * Disables Keypad interrupt if any, frees allocated keypad interrupt if any,
 * cancels keypad work queues if any, deallocate used GPIO pin, unregisters the
 * module, frees used memory
 */
static int u8500_kp_remove(struct platform_device *pdev)
{
	struct keypad_t *kp = platform_get_drvdata(pdev);

	/*Frees allocated keypad interrupt if any */
	if (kp->board->exit)
		kp->board->exit(kp);
#if 0
	if (!kp->mode)
		free_irq(kp->irq, kp);
#endif
	/* cancel and flush keypad work queues if any  */
	cancel_delayed_work(&kp->kscan_work);
	cancel_delayed_work_sync(&kp->kscan_work);
	device_remove_file(&pdev->dev, &dev_attr_keymap);
	/* block until work struct's callback terminates */
	input_unregister_device(kp->inp_dev);
	input_free_device(kp->inp_dev);
	kfree(kp);
	printk(KERN_INFO "Module removed....");
	return 0;
}

struct platform_driver u8500kpd_driver = {
	.probe = u8500_kp_probe,
	.remove = u8500_kp_remove,
	.driver = {
		   .name = "u8500-kp",
		   },
	.suspend = u8500_kp_suspend,
	.resume = u8500_kp_resume,
};

static int __devinit u8500_kp_init(void)
{
	return platform_driver_register(&u8500kpd_driver);
}

static void __exit u8500_kp_exit(void)
{
	platform_driver_unregister(&u8500kpd_driver);
}

module_init(u8500_kp_init);
module_exit(u8500_kp_exit);

MODULE_AUTHOR("ST-Ericsson");
MODULE_DESCRIPTION("u8500 keyboard driver");
MODULE_LICENSE("GPL");