blob: a26d8be6f7959b5f67ac20aac069c00202a31272 [file] [log] [blame]
Hans-Christian Egtvedt5f565502007-06-14 23:32:35 -04001/*
2 * Driver for simulating a mouse on GPIO lines.
3 *
4 * Copyright (C) 2007 Atmel Corporation
Linus Walleij836bd412017-10-19 16:24:24 -07005 * Copyright (C) 2017 Linus Walleij <linus.walleij@linaro.org>
Hans-Christian Egtvedt5f565502007-06-14 23:32:35 -04006 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11
Hans-Christian Egtvedt5f565502007-06-14 23:32:35 -040012#include <linux/module.h>
13#include <linux/platform_device.h>
14#include <linux/input-polldev.h>
Linus Walleij836bd412017-10-19 16:24:24 -070015#include <linux/gpio/consumer.h>
16#include <linux/property.h>
Linus Walleijadb77b32017-10-19 16:28:46 -070017#include <linux/of.h>
Linus Walleijc5053e62017-10-19 16:19:03 -070018
19/**
Linus Walleij34cf5a12017-10-19 16:19:41 -070020 * struct gpio_mouse
Linus Walleij836bd412017-10-19 16:24:24 -070021 * @scan_ms: the scan interval in milliseconds.
Linus Walleijc5053e62017-10-19 16:19:03 -070022 * @up: GPIO line for up value.
23 * @down: GPIO line for down value.
24 * @left: GPIO line for left value.
25 * @right: GPIO line for right value.
26 * @bleft: GPIO line for left button.
27 * @bmiddle: GPIO line for middle button.
28 * @bright: GPIO line for right button.
Linus Walleijc5053e62017-10-19 16:19:03 -070029 *
30 * This struct must be added to the platform_device in the board code.
31 * It is used by the gpio_mouse driver to setup GPIO lines and to
32 * calculate mouse movement.
33 */
Linus Walleij34cf5a12017-10-19 16:19:41 -070034struct gpio_mouse {
Linus Walleij836bd412017-10-19 16:24:24 -070035 u32 scan_ms;
36 struct gpio_desc *up;
37 struct gpio_desc *down;
38 struct gpio_desc *left;
39 struct gpio_desc *right;
40 struct gpio_desc *bleft;
41 struct gpio_desc *bmiddle;
42 struct gpio_desc *bright;
Linus Walleijc5053e62017-10-19 16:19:03 -070043};
Hans-Christian Egtvedt5f565502007-06-14 23:32:35 -040044
45/*
46 * Timer function which is run every scan_ms ms when the device is opened.
Uwe Kleine-Koenig3a070ad2009-01-12 23:35:48 +010047 * The dev input variable is set to the the input_dev pointer.
Hans-Christian Egtvedt5f565502007-06-14 23:32:35 -040048 */
49static void gpio_mouse_scan(struct input_polled_dev *dev)
50{
Linus Walleij34cf5a12017-10-19 16:19:41 -070051 struct gpio_mouse *gpio = dev->private;
Hans-Christian Egtvedt5f565502007-06-14 23:32:35 -040052 struct input_dev *input = dev->input;
53 int x, y;
54
Linus Walleij836bd412017-10-19 16:24:24 -070055 if (gpio->bleft)
Hans-Christian Egtvedt5f565502007-06-14 23:32:35 -040056 input_report_key(input, BTN_LEFT,
Linus Walleij836bd412017-10-19 16:24:24 -070057 gpiod_get_value(gpio->bleft));
58 if (gpio->bmiddle)
Hans-Christian Egtvedt5f565502007-06-14 23:32:35 -040059 input_report_key(input, BTN_MIDDLE,
Linus Walleij836bd412017-10-19 16:24:24 -070060 gpiod_get_value(gpio->bmiddle));
61 if (gpio->bright)
Hans-Christian Egtvedt5f565502007-06-14 23:32:35 -040062 input_report_key(input, BTN_RIGHT,
Linus Walleij836bd412017-10-19 16:24:24 -070063 gpiod_get_value(gpio->bright));
Hans-Christian Egtvedt5f565502007-06-14 23:32:35 -040064
Linus Walleij836bd412017-10-19 16:24:24 -070065 x = gpiod_get_value(gpio->right) - gpiod_get_value(gpio->left);
66 y = gpiod_get_value(gpio->down) - gpiod_get_value(gpio->up);
Hans-Christian Egtvedt5f565502007-06-14 23:32:35 -040067
68 input_report_rel(input, REL_X, x);
69 input_report_rel(input, REL_Y, y);
70 input_sync(input);
71}
72
Bill Pemberton5298cc42012-11-23 21:38:25 -080073static int gpio_mouse_probe(struct platform_device *pdev)
Hans-Christian Egtvedt5f565502007-06-14 23:32:35 -040074{
Linus Walleijc5053e62017-10-19 16:19:03 -070075 struct device *dev = &pdev->dev;
Linus Walleij34cf5a12017-10-19 16:19:41 -070076 struct gpio_mouse *gmouse;
Hans-Christian Egtvedt5f565502007-06-14 23:32:35 -040077 struct input_polled_dev *input_poll;
78 struct input_dev *input;
Linus Walleij836bd412017-10-19 16:24:24 -070079 int ret;
Hans-Christian Egtvedt5f565502007-06-14 23:32:35 -040080
Linus Walleij34cf5a12017-10-19 16:19:41 -070081 gmouse = devm_kzalloc(dev, sizeof(*gmouse), GFP_KERNEL);
82 if (!gmouse)
Linus Walleijc5053e62017-10-19 16:19:03 -070083 return -ENOMEM;
Hans-Christian Egtvedt5f565502007-06-14 23:32:35 -040084
Linus Walleij836bd412017-10-19 16:24:24 -070085 /* Assign some default scanning time */
86 ret = device_property_read_u32(dev, "scan-interval-ms",
87 &gmouse->scan_ms);
88 if (ret || gmouse->scan_ms == 0) {
89 dev_warn(dev, "invalid scan time, set to 50 ms\n");
90 gmouse->scan_ms = 50;
Hans-Christian Egtvedt5f565502007-06-14 23:32:35 -040091 }
92
Linus Walleij836bd412017-10-19 16:24:24 -070093 gmouse->up = devm_gpiod_get(dev, "up", GPIOD_IN);
94 if (IS_ERR(gmouse->up))
95 return PTR_ERR(gmouse->up);
96 gmouse->down = devm_gpiod_get(dev, "down", GPIOD_IN);
97 if (IS_ERR(gmouse->down))
98 return PTR_ERR(gmouse->down);
99 gmouse->left = devm_gpiod_get(dev, "left", GPIOD_IN);
100 if (IS_ERR(gmouse->left))
101 return PTR_ERR(gmouse->left);
102 gmouse->right = devm_gpiod_get(dev, "right", GPIOD_IN);
103 if (IS_ERR(gmouse->right))
104 return PTR_ERR(gmouse->right);
Hans-Christian Egtvedt5f565502007-06-14 23:32:35 -0400105
Linus Walleij836bd412017-10-19 16:24:24 -0700106 gmouse->bleft = devm_gpiod_get_optional(dev, "button-left", GPIOD_IN);
107 if (IS_ERR(gmouse->bleft))
108 return PTR_ERR(gmouse->bleft);
109 gmouse->bmiddle = devm_gpiod_get_optional(dev, "button-middle",
110 GPIOD_IN);
111 if (IS_ERR(gmouse->bmiddle))
112 return PTR_ERR(gmouse->bmiddle);
113 gmouse->bright = devm_gpiod_get_optional(dev, "button-right",
114 GPIOD_IN);
115 if (IS_ERR(gmouse->bright))
116 return PTR_ERR(gmouse->bright);
Hans-Christian Egtvedt5f565502007-06-14 23:32:35 -0400117
Linus Walleij836bd412017-10-19 16:24:24 -0700118 input_poll = devm_input_allocate_polled_device(dev);
Hans-Christian Egtvedt5f565502007-06-14 23:32:35 -0400119 if (!input_poll) {
Linus Walleij836bd412017-10-19 16:24:24 -0700120 dev_err(dev, "not enough memory for input device\n");
121 return -ENOMEM;
Hans-Christian Egtvedt5f565502007-06-14 23:32:35 -0400122 }
123
124 platform_set_drvdata(pdev, input_poll);
125
126 /* set input-polldev handlers */
Linus Walleij34cf5a12017-10-19 16:19:41 -0700127 input_poll->private = gmouse;
Hans-Christian Egtvedt5f565502007-06-14 23:32:35 -0400128 input_poll->poll = gpio_mouse_scan;
Linus Walleij34cf5a12017-10-19 16:19:41 -0700129 input_poll->poll_interval = gmouse->scan_ms;
Hans-Christian Egtvedt5f565502007-06-14 23:32:35 -0400130
131 input = input_poll->input;
132 input->name = pdev->name;
133 input->id.bustype = BUS_HOST;
134 input->dev.parent = &pdev->dev;
135
136 input_set_capability(input, EV_REL, REL_X);
137 input_set_capability(input, EV_REL, REL_Y);
Linus Walleij836bd412017-10-19 16:24:24 -0700138 if (gmouse->bleft)
Hans-Christian Egtvedt5f565502007-06-14 23:32:35 -0400139 input_set_capability(input, EV_KEY, BTN_LEFT);
Linus Walleij836bd412017-10-19 16:24:24 -0700140 if (gmouse->bmiddle)
Hans-Christian Egtvedt5f565502007-06-14 23:32:35 -0400141 input_set_capability(input, EV_KEY, BTN_MIDDLE);
Linus Walleij836bd412017-10-19 16:24:24 -0700142 if (gmouse->bright)
Hans-Christian Egtvedt5f565502007-06-14 23:32:35 -0400143 input_set_capability(input, EV_KEY, BTN_RIGHT);
144
Linus Walleij836bd412017-10-19 16:24:24 -0700145 ret = input_register_polled_device(input_poll);
146 if (ret) {
147 dev_err(dev, "could not register input device\n");
148 return ret;
Hans-Christian Egtvedt5f565502007-06-14 23:32:35 -0400149 }
150
Linus Walleij836bd412017-10-19 16:24:24 -0700151 dev_dbg(dev, "%d ms scan time, buttons: %s%s%s\n",
152 gmouse->scan_ms,
153 gmouse->bleft ? "" : "left ",
154 gmouse->bmiddle ? "" : "middle ",
155 gmouse->bright ? "" : "right");
Hans-Christian Egtvedt5f565502007-06-14 23:32:35 -0400156
Hans-Christian Egtvedt5f565502007-06-14 23:32:35 -0400157 return 0;
158}
159
Linus Walleijadb77b32017-10-19 16:28:46 -0700160static const struct of_device_id gpio_mouse_of_match[] = {
161 { .compatible = "gpio-mouse", },
162 { },
163};
164MODULE_DEVICE_TABLE(of, gpio_mouse_of_match);
165
Roel Kluin0de048a2008-12-20 05:19:43 -0500166static struct platform_driver gpio_mouse_device_driver = {
Saeed Bisharaeeafa5e2009-07-07 22:11:52 -0700167 .probe = gpio_mouse_probe,
Hans-Christian Egtvedt5f565502007-06-14 23:32:35 -0400168 .driver = {
169 .name = "gpio_mouse",
Linus Walleijadb77b32017-10-19 16:28:46 -0700170 .of_match_table = gpio_mouse_of_match,
Hans-Christian Egtvedt5f565502007-06-14 23:32:35 -0400171 }
172};
JJ Ding4fcdeac2011-11-29 11:08:41 -0800173module_platform_driver(gpio_mouse_device_driver);
Hans-Christian Egtvedt5f565502007-06-14 23:32:35 -0400174
Hans-Christian Egtvedt7c409522011-06-29 00:13:26 -0700175MODULE_AUTHOR("Hans-Christian Egtvedt <egtvedt@samfundet.no>");
Hans-Christian Egtvedt5f565502007-06-14 23:32:35 -0400176MODULE_DESCRIPTION("GPIO mouse driver");
177MODULE_LICENSE("GPL");
Saeed Bisharaeeafa5e2009-07-07 22:11:52 -0700178MODULE_ALIAS("platform:gpio_mouse"); /* work with hotplug and coldplug */