aboutsummaryrefslogtreecommitdiff
path: root/drivers/input/keyboard/neo1973kbd.c
blob: a95dd5806d29b3ab69a4d20d2d1b3c5f8f2e986c (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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
/*
 * Keyboard driver for FIC Neo1973 GSM phone
 *
 * (C) 2006-2007 by Openmoko, Inc.
 * Author: Harald Welte <laforge@openmoko.org>
 * All rights reserved.
 *
 * inspired by corkgbd.c by Richard Purdie
 *
 * 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>
#include <linux/platform_device.h>
#include <linux/init.h>
#include <linux/input.h>
#include <linux/interrupt.h>
#include <linux/jiffies.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/workqueue.h>

#include <mach/gpio.h>
#include <asm/mach-types.h>

#ifdef CONFIG_PM
extern int global_inside_suspend;
#else
#define global_inside_suspend 0
#endif

struct neo1973kbd {
	struct platform_device *pdev;
	struct input_dev *input;
	struct device *cdev;
	struct work_struct work;
	int aux_state;
	int work_in_progress;
	int hp_irq_count_in_work;
	int hp_irq_count;
	int jack_irq;
};

static struct class *neo1973kbd_switch_class;

enum keys {
	NEO1973_KEY_AUX,    /* GTA01 / 02 only */
	NEO1973_KEY_HOLD,
	NEO1973_KEY_JACK,
	NEO1973_KEY_PLUS,   /* GTA03 only */
	NEO1973_KEY_MINUS,  /* GTA03 only */
};

struct neo1973kbd_key {
	const char * name;
	irqreturn_t (*isr)(int irq, void *dev_id);
	int irq;
	int input_key;
};

static irqreturn_t neo1973kbd_aux_irq(int irq, void *dev_id);
static irqreturn_t neo1973kbd_headphone_irq(int irq, void *dev_id);
static irqreturn_t neo1973kbd_default_key_irq(int irq, void *dev_id);


static struct neo1973kbd_key keys[] = {
	[NEO1973_KEY_AUX] = {
		.name = "Neo1973 AUX button",
		.isr = neo1973kbd_aux_irq,
		.input_key = KEY_PHONE,
	},
	[NEO1973_KEY_HOLD] = {
		.name = "Neo1973 HOLD button",
		.isr = neo1973kbd_default_key_irq,
		.input_key = KEY_PAUSE,
	},
	[NEO1973_KEY_JACK] = {
		.name = "Neo1973 Headphone jack",
		.isr = neo1973kbd_headphone_irq,
	},
	[NEO1973_KEY_PLUS] = {
		.name = "GTA03 PLUS button",
		.isr = neo1973kbd_default_key_irq,
		.input_key = KEY_KPPLUS,
	},
	[NEO1973_KEY_MINUS] = {
		.name = "GTA03 MINUS button",
		.isr = neo1973kbd_default_key_irq,
		.input_key = KEY_KPMINUS,
	},
};

/* This timer section filters AUX button IRQ bouncing */

static void aux_key_timer_f(unsigned long data);

static struct timer_list aux_key_timer =
		TIMER_INITIALIZER(aux_key_timer_f, 0, 0);

#define AUX_TIMER_TIMEOUT (HZ >> 7)
#define AUX_TIMER_ALLOWED_NOOP 2
#define AUX_TIMER_CONSECUTIVE_EVENTS 5

struct neo1973kbd *timer_kbd;

static void aux_key_timer_f(unsigned long data)
{
	static int noop_counter;
	static int last_key = -1;
	static int last_count;
	int key_pressed;

	key_pressed =
	    !gpio_get_value(timer_kbd->pdev->resource[NEO1973_KEY_AUX].start);
	if (machine_is_neo1973_gta02())
		key_pressed = !key_pressed;

	if (likely(key_pressed == last_key))
		last_count++;
	else {
		last_count = 1;
		last_key = key_pressed;
	}

	if (unlikely(last_count >= AUX_TIMER_CONSECUTIVE_EVENTS)) {
		if (timer_kbd->aux_state != last_key) {
			input_report_key(timer_kbd->input, KEY_PHONE, last_key);
			input_sync(timer_kbd->input);

			timer_kbd->aux_state = last_key;
			noop_counter = 0;
		}
		last_count = 0;
		if (unlikely(++noop_counter > AUX_TIMER_ALLOWED_NOOP)) {
			noop_counter = 0;
			return;
		}
	}

	mod_timer(&aux_key_timer, jiffies + AUX_TIMER_TIMEOUT);
}

static irqreturn_t neo1973kbd_aux_irq(int irq, void *dev)
{
	mod_timer(&aux_key_timer, jiffies + AUX_TIMER_TIMEOUT);

	return IRQ_HANDLED;
}

static irqreturn_t neo1973kbd_default_key_irq(int irq, void *dev_id)
{
	struct neo1973kbd *kbd = dev_id;
	int n;

	for (n = 0; n < ARRAY_SIZE(keys); n++) {

		if (irq != keys[n].irq)
			continue;

		input_report_key(kbd->input, keys[n].input_key,
				  gpio_get_value(kbd->pdev->resource[n].start));
		input_sync(kbd->input);
	}

	return IRQ_HANDLED;
}


static const char *event_array_jack[2][4] = {
	[0] = {
		"SWITCH_NAME=headset",
		"SWITCH_STATE=0",
		"EVENT=remove",
		NULL
	},
	[1] = {
		"SWITCH_NAME=headset",
		"SWITCH_STATE=1",
		"EVENT=insert",
		NULL
	},
};

static void neo1973kbd_jack_event(struct device *dev, int num)
{
	kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, (char **)event_array_jack[!!num]);
}


static void neo1973kbd_debounce_jack(struct work_struct *work)
{
	struct neo1973kbd *kbd = container_of(work, struct neo1973kbd, work);
	unsigned long flags;
	int loop = 0;
	int level;

	do {
		/*
		 * we wait out any multiple interrupt
		 * stuttering in 100ms lumps
		 */
		do {
			kbd->hp_irq_count_in_work = kbd->hp_irq_count;
			msleep(100);
		} while (kbd->hp_irq_count != kbd->hp_irq_count_in_work);
		/*
		 * no new interrupts on jack for 100ms...
		 * ok we will report it
		 */
		level = gpio_get_value(kbd->pdev->resource[NEO1973_KEY_JACK].start);
		input_report_switch(kbd->input, SW_HEADPHONE_INSERT, level);
		input_sync(kbd->input);
		neo1973kbd_jack_event(kbd->cdev, level);
		/*
		 * we go around the outer loop again if we detect that more
		 * interrupts came while we are servicing here.  But we have
		 * to sequence it carefully with interrupts off
		 */
		local_save_flags(flags);
		/* no interrupts during this work means we can exit the work */
		loop = !!(kbd->hp_irq_count != kbd->hp_irq_count_in_work);
		if (!loop)
			kbd->work_in_progress = 0;
		local_irq_restore(flags);
		/*
		 * interrupt that comes here will either queue a new work action
		 * since work_in_progress is cleared now, or be dealt with
		 * when we loop.
		 */
	} while (loop);
}


static irqreturn_t neo1973kbd_headphone_irq(int irq, void *dev_id)
{
	struct neo1973kbd *neo1973kbd_data = dev_id;

	/*
	 * this interrupt is prone to bouncing and userspace doesn't like
	 * to have to deal with that kind of thing.  So we do not accept
	 * that a jack interrupt is equal to a jack event.  Instead we fire
	 * some work on the first interrupt, and it hangs about in 100ms units
	 * until no more interrupts come.  Then it accepts the state it finds
	 * for jack insert and reports it once
	 */

	neo1973kbd_data->hp_irq_count++;
	/*
	 * the first interrupt we see for a while, we fire the work item
	 * and record the interrupt count when we did that.  If more interrupts
	 * come in the meanwhile, we can tell by the difference in that
	 * stored count and hp_irq_count which increments every interrupt
	 */
	if (!neo1973kbd_data->work_in_progress) {
		neo1973kbd_data->jack_irq = irq;
		neo1973kbd_data->hp_irq_count_in_work =
						neo1973kbd_data->hp_irq_count;
		if (!schedule_work(&neo1973kbd_data->work))
			printk(KERN_ERR
				"Unable to schedule headphone debounce\n");
		else
			neo1973kbd_data->work_in_progress = 1;
	}

	return IRQ_HANDLED;
}

#ifdef CONFIG_PM
static int neo1973kbd_suspend(struct platform_device *dev, pm_message_t state)
{
	if (machine_is_neo1973_gta02()) {
		disable_irq(keys[NEO1973_KEY_AUX].irq);
		del_timer_sync(&aux_key_timer);
	}
	return 0;
}

static int neo1973kbd_resume(struct platform_device *dev)
{
	if (machine_is_neo1973_gta02())
		enable_irq(keys[NEO1973_KEY_AUX].irq);

	return 0;
}
#else
#define neo1973kbd_suspend	NULL
#define neo1973kbd_resume	NULL
#endif

static ssize_t neo1973kbd_switch_name_show(struct device *dev,
			struct device_attribute *attr, char *buf)
{
	return sprintf(buf, "%s\n", "neo1973 Headset Jack");
}

static ssize_t neo1973kbd_switch_state_show(struct device *dev,
			struct device_attribute *attr, char *buf)
{
	struct neo1973kbd *kbd = dev_get_drvdata(dev);
	return sprintf(buf, "%d\n",
		   gpio_get_value(kbd->pdev->resource[NEO1973_KEY_JACK].start));
}

static DEVICE_ATTR(name, S_IRUGO , neo1973kbd_switch_name_show, NULL);
static DEVICE_ATTR(state, S_IRUGO , neo1973kbd_switch_state_show, NULL);

static int neo1973kbd_probe(struct platform_device *pdev)
{
	struct neo1973kbd *neo1973kbd;
	struct input_dev *input_dev;
	int rc;
	int irq;
	int n;

	neo1973kbd = kzalloc(sizeof(struct neo1973kbd), GFP_KERNEL);
	input_dev = input_allocate_device();
	if (!neo1973kbd || !input_dev) {
		kfree(neo1973kbd);
		input_free_device(input_dev);
		return -ENOMEM;
	}

	neo1973kbd->pdev = pdev;
	timer_kbd = neo1973kbd;

	if (pdev->resource[0].flags != 0)
		return -EINVAL;

	platform_set_drvdata(pdev, neo1973kbd);

	neo1973kbd->input = input_dev;

	INIT_WORK(&neo1973kbd->work, neo1973kbd_debounce_jack);

	input_dev->name = "Neo1973 Buttons";
	input_dev->phys = "neo1973kbd/input0";
	input_dev->id.bustype = BUS_HOST;
	input_dev->id.vendor = 0x0001;
	input_dev->id.product = 0x0001;
	input_dev->id.version = 0x0100;
	input_dev->dev.parent = &pdev->dev;

	input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_SW);
	set_bit(SW_HEADPHONE_INSERT, input_dev->swbit);
	set_bit(KEY_PHONE, input_dev->keybit);
	set_bit(KEY_PAUSE, input_dev->keybit);

	rc = input_register_device(neo1973kbd->input);
	if (rc)
		goto out_register;

	neo1973kbd->cdev = device_create(neo1973kbd_switch_class,
					  &pdev->dev, 0, neo1973kbd, "headset");
	if (unlikely(IS_ERR(neo1973kbd->cdev))) {
		rc = PTR_ERR(neo1973kbd->cdev);
		goto out_device_create;
	}

	rc = device_create_file(neo1973kbd->cdev, &dev_attr_name);
	if(rc)
		goto out_device_create_file;

	rc = device_create_file(neo1973kbd->cdev, &dev_attr_state);
	if(rc)
		goto out_device_create_file;

	/* register GPIO IRQs */
	for(n = 0; n < min(pdev->num_resources, ARRAY_SIZE(keys)); n++) {

		if (!pdev->resource[0].start)
			continue;

		irq = gpio_to_irq(pdev->resource[n].start);
		if (irq < 0)
			continue;

		if (request_irq(irq, keys[n].isr, IRQF_DISABLED |
				IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
				keys[n].name, neo1973kbd)) {
			dev_err(&pdev->dev, "Can't get IRQ %u\n", irq);

			/* unwind any irq registrations and fail */

			while (n > 0) {
				n--;
				free_irq(gpio_to_irq(pdev->resource[n].start),
								    neo1973kbd);
			}
			goto out_device_create_file;
		}

		keys[n].irq = irq;
	}

	/*
	 * GTA01 revisions before Bv4 can't be resumed by the PMU, so we use
	 * resume by AUX.
	 */
	if (machine_is_neo1973_gta01())
		enable_irq_wake(keys[NEO1973_KEY_AUX].irq);

	enable_irq_wake(keys[NEO1973_KEY_JACK].irq);

	return 0;

out_device_create_file:
	device_unregister(neo1973kbd->cdev);
out_device_create:
	input_unregister_device(neo1973kbd->input);
out_register:
	input_free_device(neo1973kbd->input);
	platform_set_drvdata(pdev, NULL);
	kfree(neo1973kbd);

	return -ENODEV;
}

static int neo1973kbd_remove(struct platform_device *pdev)
{
	struct neo1973kbd *neo1973kbd = platform_get_drvdata(pdev);

	free_irq(gpio_to_irq(pdev->resource[2].start), neo1973kbd);
	free_irq(gpio_to_irq(pdev->resource[1].start), neo1973kbd);
	free_irq(gpio_to_irq(pdev->resource[0].start), neo1973kbd);

	device_unregister(neo1973kbd->cdev);
	input_unregister_device(neo1973kbd->input);
	input_free_device(neo1973kbd->input);
	platform_set_drvdata(pdev, NULL);
	kfree(neo1973kbd);

	return 0;
}

static struct platform_driver neo1973kbd_driver = {
	.probe		= neo1973kbd_probe,
	.remove		= neo1973kbd_remove,
	.suspend	= neo1973kbd_suspend,
	.resume		= neo1973kbd_resume,
	.driver		= {
		.name	= "neo1973-button",
	},
};

static int __devinit neo1973kbd_init(void)
{
	neo1973kbd_switch_class = class_create(THIS_MODULE, "switch");
	if (IS_ERR(neo1973kbd_switch_class))
		return PTR_ERR(neo1973kbd_switch_class);
	return platform_driver_register(&neo1973kbd_driver);
}

static void __exit neo1973kbd_exit(void)
{
	platform_driver_unregister(&neo1973kbd_driver);
	class_destroy(neo1973kbd_switch_class);
}

module_init(neo1973kbd_init);
module_exit(neo1973kbd_exit);

MODULE_AUTHOR("Harald Welte <laforge@openmoko.org>");
MODULE_DESCRIPTION("FIC Neo1973 buttons input driver");
MODULE_LICENSE("GPL");