blob: 461436f6f087cb9ddde7e6634b0f971366c0aa83 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Hans-Christian Egtvedt5f565502007-06-14 23:32:35 -04002/*
3 * Driver for simulating a mouse on GPIO lines.
4 *
5 * Copyright (C) 2007 Atmel Corporation
Linus Walleij836bd412017-10-19 16:24:24 -07006 * Copyright (C) 2017 Linus Walleij <linus.walleij@linaro.org>
Hans-Christian Egtvedt5f565502007-06-14 23:32:35 -04007 */
8
Hans-Christian Egtvedt5f565502007-06-14 23:32:35 -04009#include <linux/module.h>
10#include <linux/platform_device.h>
11#include <linux/input-polldev.h>
Linus Walleij836bd412017-10-19 16:24:24 -070012#include <linux/gpio/consumer.h>
13#include <linux/property.h>
Linus Walleijadb77b32017-10-19 16:28:46 -070014#include <linux/of.h>
Linus Walleijc5053e62017-10-19 16:19:03 -070015
16/**
Linus Walleij34cf5a12017-10-19 16:19:41 -070017 * struct gpio_mouse
Linus Walleij836bd412017-10-19 16:24:24 -070018 * @scan_ms: the scan interval in milliseconds.
Linus Walleijc5053e62017-10-19 16:19:03 -070019 * @up: GPIO line for up value.
20 * @down: GPIO line for down value.
21 * @left: GPIO line for left value.
22 * @right: GPIO line for right value.
23 * @bleft: GPIO line for left button.
24 * @bmiddle: GPIO line for middle button.
25 * @bright: GPIO line for right button.
Linus Walleijc5053e62017-10-19 16:19:03 -070026 *
27 * This struct must be added to the platform_device in the board code.
28 * It is used by the gpio_mouse driver to setup GPIO lines and to
29 * calculate mouse movement.
30 */
Linus Walleij34cf5a12017-10-19 16:19:41 -070031struct gpio_mouse {
Linus Walleij836bd412017-10-19 16:24:24 -070032 u32 scan_ms;
33 struct gpio_desc *up;
34 struct gpio_desc *down;
35 struct gpio_desc *left;
36 struct gpio_desc *right;
37 struct gpio_desc *bleft;
38 struct gpio_desc *bmiddle;
39 struct gpio_desc *bright;
Linus Walleijc5053e62017-10-19 16:19:03 -070040};
Hans-Christian Egtvedt5f565502007-06-14 23:32:35 -040041
42/*
43 * Timer function which is run every scan_ms ms when the device is opened.
Uwe Kleine-Koenig3a070ad2009-01-12 23:35:48 +010044 * The dev input variable is set to the the input_dev pointer.
Hans-Christian Egtvedt5f565502007-06-14 23:32:35 -040045 */
46static void gpio_mouse_scan(struct input_polled_dev *dev)
47{
Linus Walleij34cf5a12017-10-19 16:19:41 -070048 struct gpio_mouse *gpio = dev->private;
Hans-Christian Egtvedt5f565502007-06-14 23:32:35 -040049 struct input_dev *input = dev->input;
50 int x, y;
51
Linus Walleij836bd412017-10-19 16:24:24 -070052 if (gpio->bleft)
Hans-Christian Egtvedt5f565502007-06-14 23:32:35 -040053 input_report_key(input, BTN_LEFT,
Linus Walleij836bd412017-10-19 16:24:24 -070054 gpiod_get_value(gpio->bleft));
55 if (gpio->bmiddle)
Hans-Christian Egtvedt5f565502007-06-14 23:32:35 -040056 input_report_key(input, BTN_MIDDLE,
Linus Walleij836bd412017-10-19 16:24:24 -070057 gpiod_get_value(gpio->bmiddle));
58 if (gpio->bright)
Hans-Christian Egtvedt5f565502007-06-14 23:32:35 -040059 input_report_key(input, BTN_RIGHT,
Linus Walleij836bd412017-10-19 16:24:24 -070060 gpiod_get_value(gpio->bright));
Hans-Christian Egtvedt5f565502007-06-14 23:32:35 -040061
Linus Walleij836bd412017-10-19 16:24:24 -070062 x = gpiod_get_value(gpio->right) - gpiod_get_value(gpio->left);
63 y = gpiod_get_value(gpio->down) - gpiod_get_value(gpio->up);
Hans-Christian Egtvedt5f565502007-06-14 23:32:35 -040064
65 input_report_rel(input, REL_X, x);
66 input_report_rel(input, REL_Y, y);
67 input_sync(input);
68}
69
Bill Pemberton5298cc42012-11-23 21:38:25 -080070static int gpio_mouse_probe(struct platform_device *pdev)
Hans-Christian Egtvedt5f565502007-06-14 23:32:35 -040071{
Linus Walleijc5053e62017-10-19 16:19:03 -070072 struct device *dev = &pdev->dev;
Linus Walleij34cf5a12017-10-19 16:19:41 -070073 struct gpio_mouse *gmouse;
Hans-Christian Egtvedt5f565502007-06-14 23:32:35 -040074 struct input_polled_dev *input_poll;
75 struct input_dev *input;
Linus Walleij836bd412017-10-19 16:24:24 -070076 int ret;
Hans-Christian Egtvedt5f565502007-06-14 23:32:35 -040077
Linus Walleij34cf5a12017-10-19 16:19:41 -070078 gmouse = devm_kzalloc(dev, sizeof(*gmouse), GFP_KERNEL);
79 if (!gmouse)
Linus Walleijc5053e62017-10-19 16:19:03 -070080 return -ENOMEM;
Hans-Christian Egtvedt5f565502007-06-14 23:32:35 -040081
Linus Walleij836bd412017-10-19 16:24:24 -070082 /* Assign some default scanning time */
83 ret = device_property_read_u32(dev, "scan-interval-ms",
84 &gmouse->scan_ms);
85 if (ret || gmouse->scan_ms == 0) {
86 dev_warn(dev, "invalid scan time, set to 50 ms\n");
87 gmouse->scan_ms = 50;
Hans-Christian Egtvedt5f565502007-06-14 23:32:35 -040088 }
89
Linus Walleij836bd412017-10-19 16:24:24 -070090 gmouse->up = devm_gpiod_get(dev, "up", GPIOD_IN);
91 if (IS_ERR(gmouse->up))
92 return PTR_ERR(gmouse->up);
93 gmouse->down = devm_gpiod_get(dev, "down", GPIOD_IN);
94 if (IS_ERR(gmouse->down))
95 return PTR_ERR(gmouse->down);
96 gmouse->left = devm_gpiod_get(dev, "left", GPIOD_IN);
97 if (IS_ERR(gmouse->left))
98 return PTR_ERR(gmouse->left);
99 gmouse->right = devm_gpiod_get(dev, "right", GPIOD_IN);
100 if (IS_ERR(gmouse->right))
101 return PTR_ERR(gmouse->right);
Hans-Christian Egtvedt5f565502007-06-14 23:32:35 -0400102
Linus Walleij836bd412017-10-19 16:24:24 -0700103 gmouse->bleft = devm_gpiod_get_optional(dev, "button-left", GPIOD_IN);
104 if (IS_ERR(gmouse->bleft))
105 return PTR_ERR(gmouse->bleft);
106 gmouse->bmiddle = devm_gpiod_get_optional(dev, "button-middle",
107 GPIOD_IN);
108 if (IS_ERR(gmouse->bmiddle))
109 return PTR_ERR(gmouse->bmiddle);
110 gmouse->bright = devm_gpiod_get_optional(dev, "button-right",
111 GPIOD_IN);
112 if (IS_ERR(gmouse->bright))
113 return PTR_ERR(gmouse->bright);
Hans-Christian Egtvedt5f565502007-06-14 23:32:35 -0400114
Linus Walleij836bd412017-10-19 16:24:24 -0700115 input_poll = devm_input_allocate_polled_device(dev);
Hans-Christian Egtvedt5f565502007-06-14 23:32:35 -0400116 if (!input_poll) {
Linus Walleij836bd412017-10-19 16:24:24 -0700117 dev_err(dev, "not enough memory for input device\n");
118 return -ENOMEM;
Hans-Christian Egtvedt5f565502007-06-14 23:32:35 -0400119 }
120
121 platform_set_drvdata(pdev, input_poll);
122
123 /* set input-polldev handlers */
Linus Walleij34cf5a12017-10-19 16:19:41 -0700124 input_poll->private = gmouse;
Hans-Christian Egtvedt5f565502007-06-14 23:32:35 -0400125 input_poll->poll = gpio_mouse_scan;
Linus Walleij34cf5a12017-10-19 16:19:41 -0700126 input_poll->poll_interval = gmouse->scan_ms;
Hans-Christian Egtvedt5f565502007-06-14 23:32:35 -0400127
128 input = input_poll->input;
129 input->name = pdev->name;
130 input->id.bustype = BUS_HOST;
131 input->dev.parent = &pdev->dev;
132
133 input_set_capability(input, EV_REL, REL_X);
134 input_set_capability(input, EV_REL, REL_Y);
Linus Walleij836bd412017-10-19 16:24:24 -0700135 if (gmouse->bleft)
Hans-Christian Egtvedt5f565502007-06-14 23:32:35 -0400136 input_set_capability(input, EV_KEY, BTN_LEFT);
Linus Walleij836bd412017-10-19 16:24:24 -0700137 if (gmouse->bmiddle)
Hans-Christian Egtvedt5f565502007-06-14 23:32:35 -0400138 input_set_capability(input, EV_KEY, BTN_MIDDLE);
Linus Walleij836bd412017-10-19 16:24:24 -0700139 if (gmouse->bright)
Hans-Christian Egtvedt5f565502007-06-14 23:32:35 -0400140 input_set_capability(input, EV_KEY, BTN_RIGHT);
141
Linus Walleij836bd412017-10-19 16:24:24 -0700142 ret = input_register_polled_device(input_poll);
143 if (ret) {
144 dev_err(dev, "could not register input device\n");
145 return ret;
Hans-Christian Egtvedt5f565502007-06-14 23:32:35 -0400146 }
147
Linus Walleij836bd412017-10-19 16:24:24 -0700148 dev_dbg(dev, "%d ms scan time, buttons: %s%s%s\n",
149 gmouse->scan_ms,
150 gmouse->bleft ? "" : "left ",
151 gmouse->bmiddle ? "" : "middle ",
152 gmouse->bright ? "" : "right");
Hans-Christian Egtvedt5f565502007-06-14 23:32:35 -0400153
Hans-Christian Egtvedt5f565502007-06-14 23:32:35 -0400154 return 0;
155}
156
Linus Walleijadb77b32017-10-19 16:28:46 -0700157static const struct of_device_id gpio_mouse_of_match[] = {
158 { .compatible = "gpio-mouse", },
159 { },
160};
161MODULE_DEVICE_TABLE(of, gpio_mouse_of_match);
162
Roel Kluin0de048a2008-12-20 05:19:43 -0500163static struct platform_driver gpio_mouse_device_driver = {
Saeed Bisharaeeafa5e2009-07-07 22:11:52 -0700164 .probe = gpio_mouse_probe,
Hans-Christian Egtvedt5f565502007-06-14 23:32:35 -0400165 .driver = {
166 .name = "gpio_mouse",
Linus Walleijadb77b32017-10-19 16:28:46 -0700167 .of_match_table = gpio_mouse_of_match,
Hans-Christian Egtvedt5f565502007-06-14 23:32:35 -0400168 }
169};
JJ Ding4fcdeac2011-11-29 11:08:41 -0800170module_platform_driver(gpio_mouse_device_driver);
Hans-Christian Egtvedt5f565502007-06-14 23:32:35 -0400171
Hans-Christian Egtvedt7c409522011-06-29 00:13:26 -0700172MODULE_AUTHOR("Hans-Christian Egtvedt <egtvedt@samfundet.no>");
Hans-Christian Egtvedt5f565502007-06-14 23:32:35 -0400173MODULE_DESCRIPTION("GPIO mouse driver");
174MODULE_LICENSE("GPL");
Saeed Bisharaeeafa5e2009-07-07 22:11:52 -0700175MODULE_ALIAS("platform:gpio_mouse"); /* work with hotplug and coldplug */