aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/input/misc/alps_gpio_scrollwheel.c
blob: 4a789267c47573d999b8c1ab2c7e817cd02877e6 (plain) (blame)
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
/*
 * kernel/drivers/input/misc/alps_gpio_scrollwheel.c
 *
 * Copyright (c) 2010, NVIDIA Corporation.
 *
 * Driver for ScrollWheel on GPIO lines capable of generating interrupts.
 *
 * 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.
 *
 * 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.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */


#include <linux/module.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/interrupt.h>
#include <linux/irq.h>
#include <linux/sched.h>
#include <linux/pm.h>
#include <linux/slab.h>
#include <linux/sysctl.h>
#include <linux/proc_fs.h>
#include <linux/delay.h>
#include <linux/platform_device.h>
#include <linux/input.h>
#include <linux/gpio_scrollwheel.h>
#include <linux/workqueue.h>
#include <linux/gpio.h>

struct scrollwheel_button_data {
	struct gpio_scrollwheel_button *button;
	struct input_dev *input;
	struct timer_list timer;
	struct work_struct work;
	int timer_debounce;	/* in msecs */
	int rotgpio;
	bool disabled;
};

struct gpio_scrollwheel_drvdata {
	struct input_dev *input;
	struct mutex disable_lock;
	unsigned int n_buttons;
	int (*enable)(struct device *dev);
	void (*disable)(struct device *dev);
	struct scrollwheel_button_data data[0];
};

static void scrollwheel_report_key(struct scrollwheel_button_data *bdata)
{
	struct gpio_scrollwheel_button *button = bdata->button;
	struct input_dev *input = bdata->input;
	int state = (gpio_get_value(button->gpio) ? 1 : 0) ^ \
				button->active_low;
	int state2 = 0;

	switch (button->pinaction) {
	case GPIO_SCROLLWHEEL_PIN_PRESS:
		input_report_key(input, KEY_ENTER, 1);
		input_report_key(input, KEY_ENTER, 0);
		input_sync(input);
		break;

	case GPIO_SCROLLWHEEL_PIN_ROT1:
	case GPIO_SCROLLWHEEL_PIN_ROT2:
		state2 = (gpio_get_value(bdata->rotgpio) ? 1 : 0) \
					^ button->active_low;
		if (state != state2) {
			input_report_key(input, KEY_DOWN, 1);
			input_report_key(input, KEY_DOWN, 0);
		} else {
			input_report_key(input, KEY_UP, 1);
			input_report_key(input, KEY_UP, 0);
		}
		input_sync(input);
		break;

	default:
		pr_err("%s:Line=%d, Invalid Pinaction\n", __func__, __LINE__);
	}
}

static void scrollwheel_work_func(struct work_struct *work)
{
	struct scrollwheel_button_data *bdata =
		container_of(work, struct scrollwheel_button_data, work);

	scrollwheel_report_key(bdata);
}

static void scrollwheel_timer(unsigned long _data)
{
	struct scrollwheel_button_data *data = \
		(struct scrollwheel_button_data *)_data;

	schedule_work(&data->work);
}

static irqreturn_t scrollwheel_isr(int irq, void *dev_id)
{
	struct scrollwheel_button_data *bdata = dev_id;
	struct gpio_scrollwheel_button *button = bdata->button;

	BUG_ON(irq != gpio_to_irq(button->gpio));

	if (bdata->timer_debounce)
		mod_timer(&bdata->timer,
			jiffies + msecs_to_jiffies(bdata->timer_debounce));
	else
		schedule_work(&bdata->work);

	return IRQ_HANDLED;
}

static int __devinit gpio_scrollwheel_setup_key(struct platform_device *pdev,
			struct scrollwheel_button_data *bdata,
			struct gpio_scrollwheel_button *button)
{
	char *desc = button->desc ? button->desc : "gpio_scrollwheel";
	struct device *dev = &pdev->dev;
	unsigned long irqflags;
	int irq, error;

	setup_timer(&bdata->timer, scrollwheel_timer, (unsigned long)bdata);
	INIT_WORK(&bdata->work, scrollwheel_work_func);

	error = gpio_request(button->gpio, desc);
	if (error < 0) {
		dev_err(dev, "failed to request GPIO %d, error %d\n",
			button->gpio, error);
		return error;
	}

	error = gpio_direction_input(button->gpio);
	if (error < 0) {
		dev_err(dev, "failed to configure"
			" direction for GPIO %d, error %d\n",
			button->gpio, error);
		goto fail;
	}

	if (button->debounce_interval) {
		error = gpio_set_debounce(button->gpio,
					  button->debounce_interval * 1000);
		/* use timer if gpiolib doesn't provide debounce */
		if (error < 0)
			bdata->timer_debounce = button->debounce_interval;
	}

	irq = gpio_to_irq(button->gpio);
	if (irq < 0) {
		error = irq;
		dev_err(dev, "Unable to get irq no for GPIO %d, error %d\n",
			button->gpio, error);
		goto fail;
	}

	irqflags = IRQF_TRIGGER_FALLING;

	error = request_irq(irq, scrollwheel_isr, irqflags, desc, bdata);
	if (error) {
		dev_err(dev, "Unable to claim irq %d; error %d\n",
			irq, error);
		goto fail;
	}

	return 0;

fail:
	return error;
}

static int gpio_scrollwheel_open(struct input_dev *input)
{
	struct gpio_scrollwheel_drvdata *ddata = input_get_drvdata(input);

	return ddata->enable ? ddata->enable(input->dev.parent) : 0;
}

static void gpio_scrollwheel_close(struct input_dev *input)
{
	struct gpio_scrollwheel_drvdata *ddata = input_get_drvdata(input);

	if (ddata->disable)
		ddata->disable(input->dev.parent);
}

static int __devinit gpio_scrollwheel_probe(struct platform_device *pdev)
{
	struct gpio_scrollwheel_platform_data *pdata = pdev->dev.platform_data;
	struct gpio_scrollwheel_drvdata *ddata;
	struct device *dev = &pdev->dev;
	struct input_dev *input;
	int i, error;

	ddata = kzalloc(sizeof(struct gpio_scrollwheel_drvdata) +
		pdata->nbuttons * sizeof(struct scrollwheel_button_data),
		GFP_KERNEL);
	if (ddata == NULL) {
		dev_err(dev, "failed to allocate memory\n");
		error = -ENOMEM;
		return error;
	}

	input = input_allocate_device();
	if (input == NULL) {
		dev_err(dev, "failed to allocate input device\n");
		error = -ENOMEM;
		kfree(ddata);
		return error;
	}

	ddata->input = input;
	ddata->n_buttons = pdata->nbuttons;
	ddata->enable = pdata->enable;
	ddata->disable = pdata->disable;
	mutex_init(&ddata->disable_lock);

	platform_set_drvdata(pdev, ddata);
	input_set_drvdata(input, ddata);

	input->name = pdev->name;
	input->phys = "gpio-scrollwheel/input0";
	input->dev.parent = &pdev->dev;
	input->open = gpio_scrollwheel_open;
	input->close = gpio_scrollwheel_close;

	input->id.bustype = BUS_HOST;
	input->id.vendor = 0x0001;
	input->id.product = 0x0001;
	input->id.version = 0x0100;

	/* Enable auto repeat feature of Linux input subsystem */
	if (pdata->rep)
		__set_bit(EV_REP, input->evbit);

	for (i = 0; i < pdata->nbuttons; i++) {
		struct gpio_scrollwheel_button *button = &pdata->buttons[i];
		struct scrollwheel_button_data *bdata = &ddata->data[i];

		bdata->input = input;
		bdata->button = button;

		if (button->pinaction == GPIO_SCROLLWHEEL_PIN_PRESS ||
			button->pinaction == GPIO_SCROLLWHEEL_PIN_ROT1) {
			error = gpio_scrollwheel_setup_key(pdev, bdata, button);
			if (error)
				goto fail;
		} else {
			if (button->pinaction == GPIO_SCROLLWHEEL_PIN_ONOFF) {
				gpio_request(button->gpio, button->desc);
				gpio_direction_output(button->gpio, 0);
			}

			if (button->pinaction == GPIO_SCROLLWHEEL_PIN_ROT2) {
				gpio_request(button->gpio, button->desc);
				gpio_direction_input(button->gpio);
				/* Save rot2 gpio number in rot1 context */
				ddata->data[2].rotgpio = button->gpio;
			}
		}
	}

	/* set input capability */
	__set_bit(EV_KEY, input->evbit);
	__set_bit(KEY_ENTER, input->keybit);
	__set_bit(KEY_UP, input->keybit);
	__set_bit(KEY_DOWN, input->keybit);

	error = input_register_device(input);
	if (error) {
		dev_err(dev, "Unable to register input device, error: %d\n",
			error);
		goto fail;
	}

	input_sync(input);

	return 0;

fail:
	while (--i >= 0) {
		if (pdata->buttons[i].pinaction == GPIO_SCROLLWHEEL_PIN_PRESS ||
			pdata->buttons[i].pinaction == GPIO_SCROLLWHEEL_PIN_ROT1) {
			free_irq(gpio_to_irq(pdata->buttons[i].gpio), &ddata->data[i]);
			if (ddata->data[i].timer_debounce)
				del_timer_sync(&ddata->data[i].timer);
			cancel_work_sync(&ddata->data[i].work);
		}
		gpio_free(pdata->buttons[i].gpio);
	}

	platform_set_drvdata(pdev, NULL);
	input_free_device(input);
	kfree(ddata);
	return error;
}

static int __devexit gpio_scrollwheel_remove(struct platform_device *pdev)
{
	struct gpio_scrollwheel_platform_data *pdata = pdev->dev.platform_data;
	struct gpio_scrollwheel_drvdata *ddata = platform_get_drvdata(pdev);
	struct input_dev *input = ddata->input;
	int i;

	for (i = 0; i < pdata->nbuttons; i++) {
		if (pdata->buttons[i].pinaction == GPIO_SCROLLWHEEL_PIN_PRESS ||
			pdata->buttons[i].pinaction == GPIO_SCROLLWHEEL_PIN_ROT1) {
			int irq = gpio_to_irq(pdata->buttons[i].gpio);
			free_irq(irq, &ddata->data[i]);
			if (ddata->data[i].timer_debounce)
				del_timer_sync(&ddata->data[i].timer);
			cancel_work_sync(&ddata->data[i].work);
		}
		gpio_free(pdata->buttons[i].gpio);
	}

	input_unregister_device(input);

	return 0;
}


#ifdef CONFIG_PM
static int gpio_scrollwheel_suspend(struct device *dev)
{
	struct platform_device *pdev = to_platform_device(dev);
	struct gpio_scrollwheel_platform_data *pdata = pdev->dev.platform_data;
	struct gpio_scrollwheel_drvdata *ddata = platform_get_drvdata(pdev);
	int i, irq;

	for (i = 0; i < pdata->nbuttons; i++) {
		if (pdata->buttons[i].pinaction == GPIO_SCROLLWHEEL_PIN_PRESS ||
			pdata->buttons[i].pinaction == GPIO_SCROLLWHEEL_PIN_ROT1) {
			irq = gpio_to_irq(pdata->buttons[i].gpio);
			disable_irq(irq);
			if (ddata->data[i].timer_debounce)
				del_timer_sync(&ddata->data[i].timer);
			cancel_work_sync(&ddata->data[i].work);
		} else {
			if (pdata->buttons[i].pinaction == GPIO_SCROLLWHEEL_PIN_ONOFF)
				gpio_direction_output(pdata->buttons[i].gpio, 1);
			else {
				irq = gpio_to_irq(pdata->buttons[i].gpio);
				disable_irq(irq);
			}
		}
	}
	return 0;
}

static int gpio_scrollwheel_resume(struct device *dev)
{
	struct platform_device *pdev = to_platform_device(dev);
	struct gpio_scrollwheel_platform_data *pdata = pdev->dev.platform_data;
	struct gpio_scrollwheel_drvdata *ddata = platform_get_drvdata(pdev);
	int i, irq;

	for (i = 0; i < pdata->nbuttons; i++) {
		if (pdata->buttons[i].pinaction == GPIO_SCROLLWHEEL_PIN_PRESS ||
			pdata->buttons[i].pinaction == GPIO_SCROLLWHEEL_PIN_ROT1) {
			irq = gpio_to_irq(pdata->buttons[i].gpio);
			enable_irq(irq);
			if (ddata->data[i].timer_debounce)
				setup_timer(&ddata->data[i].timer,\
				scrollwheel_timer, (unsigned long)&ddata->data[i]);

			INIT_WORK(&ddata->data[i].work, scrollwheel_work_func);
		} else {
			if (pdata->buttons[i].pinaction == GPIO_SCROLLWHEEL_PIN_ONOFF)
				gpio_direction_output(pdata->buttons[i].gpio, 0);
			else {
				irq = gpio_to_irq(pdata->buttons[i].gpio);
				enable_irq(irq);
			}
		}
	}

	return 0;
}

static const struct dev_pm_ops gpio_scrollwheel_pm_ops = {
	.suspend	= gpio_scrollwheel_suspend,
	.resume		= gpio_scrollwheel_resume,
};
#endif

static struct platform_driver gpio_scrollwheel_device_driver = {
	.probe		= gpio_scrollwheel_probe,
	.remove		= __devexit_p(gpio_scrollwheel_remove),
	.driver		= {
		.name	= "alps-gpio-scrollwheel",
		.owner	= THIS_MODULE,
#ifdef CONFIG_PM
		.pm	= &gpio_scrollwheel_pm_ops,
#endif
	}
};

static int __init gpio_scrollwheel_init(void)
{
	return platform_driver_register(&gpio_scrollwheel_device_driver);
}

static void __exit gpio_scrollwheel_exit(void)
{
	platform_driver_unregister(&gpio_scrollwheel_device_driver);
}

module_init(gpio_scrollwheel_init);
module_exit(gpio_scrollwheel_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("NVIDIA Corporation");
MODULE_DESCRIPTION("Alps SRBE ScrollWheel driver");

MODULE_ALIAS("platform:alps-gpio-scrollwheel");