summaryrefslogtreecommitdiffstats
path: root/drivers/input/gpio_keys.c
blob: 543ad1a47058db9b6cfe7355a1ce18aae810b31c (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
/*
 * Copyright (C) 2011 Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
 *
 * Under GPLv2
 */

#include <common.h>
#include <errno.h>
#include <init.h>
#include <clock.h>
#include <gpio_keys.h>
#include <poller.h>
#include <gpio.h>

static inline struct gpio_keys_platform_data *
poller_to_gk_pdata(struct poller_struct *poller)
{
	return container_of(poller, struct gpio_keys_platform_data, poller);
}

static inline struct gpio_keys_platform_data *
cdev_to_gk_pdata(struct console_device *cdev)
{
	return container_of(cdev, struct gpio_keys_platform_data, cdev);
}

static void gpio_key_poller(struct poller_struct *poller)
{
	struct gpio_keys_platform_data *pdata = poller_to_gk_pdata(poller);
	struct gpio_keys_button *gb;
	int i, val;

	for (i = 0; i < pdata->nbuttons; i++) {

		gb = &pdata->buttons[i];
		val = gpio_get_value(gb->gpio);

		if (val != gb->previous_state && val != gb->active_low) {
			kfifo_put(pdata->recv_fifo, (u_char*)&gb->code, sizeof(int));
			debug("pressed gpio(%d) as %d\n", gb->gpio, gb->code);
		}
		gb->previous_state = val;
	}
}

static int gpio_keys_tstc(struct console_device *cdev)
{
	struct gpio_keys_platform_data *pdata = cdev_to_gk_pdata(cdev);

	return (kfifo_len(pdata->recv_fifo) == 0) ? 0 : 1;
}

static int gpio_keys_getc(struct console_device *cdev)
{
	int code = 0;
	struct gpio_keys_platform_data *pdata = cdev_to_gk_pdata(cdev);

	kfifo_get(pdata->recv_fifo, (u_char*)&code, sizeof(int));
	return code;
}

static int __init gpio_keys_probe(struct device_d *dev)
{
	int ret, i, gpio;
	struct gpio_keys_platform_data *pdata;
	struct console_device *cdev;

	pdata = dev->platform_data;

	if (!pdata) {
		/* small (so we copy it) but critical! */
		pr_err("missing platform_data\n");
		return -ENODEV;
	}

	if (!pdata->fifo_size)
		pdata->fifo_size = 50;

	pdata->recv_fifo = kfifo_alloc(pdata->fifo_size);

	for (i = 0; i < pdata->nbuttons; i++) {
		gpio = pdata->buttons[i].gpio;
		ret = gpio_request(gpio, "gpio_keys");
		if (ret) {
			pr_err("gpio_keys: (%d) can not be requested\n", gpio);
			return ret;
		}
		gpio_direction_input(gpio);
	}

	pdata->poller.func = gpio_key_poller;

	cdev = &pdata->cdev;
	dev->type_data = cdev;
	cdev->dev = dev;
	cdev->f_caps = CONSOLE_STDIN;
	cdev->tstc = gpio_keys_tstc;
	cdev->getc = gpio_keys_getc;

	console_register(&pdata->cdev);

	return poller_register(&pdata->poller);
}

static struct driver_d gpio_keys_driver = {
	.name	= "gpio_keys",
	.probe	= gpio_keys_probe,
};

static int gpio_keys_init(void)
{
	platform_driver_register(&gpio_keys_driver);
	return 0;
}
device_initcall(gpio_keys_init);