Hans-Christian Egtvedt | 5f56550 | 2007-06-14 23:32:35 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Driver for simulating a mouse on GPIO lines. |
| 3 | * |
| 4 | * Copyright (C) 2007 Atmel Corporation |
Linus Walleij | 836bd41 | 2017-10-19 16:24:24 -0700 | [diff] [blame^] | 5 | * Copyright (C) 2017 Linus Walleij <linus.walleij@linaro.org> |
Hans-Christian Egtvedt | 5f56550 | 2007-06-14 23:32:35 -0400 | [diff] [blame] | 6 | * |
| 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 Egtvedt | 5f56550 | 2007-06-14 23:32:35 -0400 | [diff] [blame] | 12 | #include <linux/module.h> |
| 13 | #include <linux/platform_device.h> |
| 14 | #include <linux/input-polldev.h> |
Linus Walleij | 836bd41 | 2017-10-19 16:24:24 -0700 | [diff] [blame^] | 15 | #include <linux/gpio/consumer.h> |
| 16 | #include <linux/property.h> |
Linus Walleij | c5053e6 | 2017-10-19 16:19:03 -0700 | [diff] [blame] | 17 | |
| 18 | /** |
Linus Walleij | 34cf5a1 | 2017-10-19 16:19:41 -0700 | [diff] [blame] | 19 | * struct gpio_mouse |
Linus Walleij | 836bd41 | 2017-10-19 16:24:24 -0700 | [diff] [blame^] | 20 | * @scan_ms: the scan interval in milliseconds. |
Linus Walleij | c5053e6 | 2017-10-19 16:19:03 -0700 | [diff] [blame] | 21 | * @up: GPIO line for up value. |
| 22 | * @down: GPIO line for down value. |
| 23 | * @left: GPIO line for left value. |
| 24 | * @right: GPIO line for right value. |
| 25 | * @bleft: GPIO line for left button. |
| 26 | * @bmiddle: GPIO line for middle button. |
| 27 | * @bright: GPIO line for right button. |
Linus Walleij | c5053e6 | 2017-10-19 16:19:03 -0700 | [diff] [blame] | 28 | * |
| 29 | * This struct must be added to the platform_device in the board code. |
| 30 | * It is used by the gpio_mouse driver to setup GPIO lines and to |
| 31 | * calculate mouse movement. |
| 32 | */ |
Linus Walleij | 34cf5a1 | 2017-10-19 16:19:41 -0700 | [diff] [blame] | 33 | struct gpio_mouse { |
Linus Walleij | 836bd41 | 2017-10-19 16:24:24 -0700 | [diff] [blame^] | 34 | u32 scan_ms; |
| 35 | struct gpio_desc *up; |
| 36 | struct gpio_desc *down; |
| 37 | struct gpio_desc *left; |
| 38 | struct gpio_desc *right; |
| 39 | struct gpio_desc *bleft; |
| 40 | struct gpio_desc *bmiddle; |
| 41 | struct gpio_desc *bright; |
Linus Walleij | c5053e6 | 2017-10-19 16:19:03 -0700 | [diff] [blame] | 42 | }; |
Hans-Christian Egtvedt | 5f56550 | 2007-06-14 23:32:35 -0400 | [diff] [blame] | 43 | |
| 44 | /* |
| 45 | * Timer function which is run every scan_ms ms when the device is opened. |
Uwe Kleine-Koenig | 3a070ad | 2009-01-12 23:35:48 +0100 | [diff] [blame] | 46 | * The dev input variable is set to the the input_dev pointer. |
Hans-Christian Egtvedt | 5f56550 | 2007-06-14 23:32:35 -0400 | [diff] [blame] | 47 | */ |
| 48 | static void gpio_mouse_scan(struct input_polled_dev *dev) |
| 49 | { |
Linus Walleij | 34cf5a1 | 2017-10-19 16:19:41 -0700 | [diff] [blame] | 50 | struct gpio_mouse *gpio = dev->private; |
Hans-Christian Egtvedt | 5f56550 | 2007-06-14 23:32:35 -0400 | [diff] [blame] | 51 | struct input_dev *input = dev->input; |
| 52 | int x, y; |
| 53 | |
Linus Walleij | 836bd41 | 2017-10-19 16:24:24 -0700 | [diff] [blame^] | 54 | if (gpio->bleft) |
Hans-Christian Egtvedt | 5f56550 | 2007-06-14 23:32:35 -0400 | [diff] [blame] | 55 | input_report_key(input, BTN_LEFT, |
Linus Walleij | 836bd41 | 2017-10-19 16:24:24 -0700 | [diff] [blame^] | 56 | gpiod_get_value(gpio->bleft)); |
| 57 | if (gpio->bmiddle) |
Hans-Christian Egtvedt | 5f56550 | 2007-06-14 23:32:35 -0400 | [diff] [blame] | 58 | input_report_key(input, BTN_MIDDLE, |
Linus Walleij | 836bd41 | 2017-10-19 16:24:24 -0700 | [diff] [blame^] | 59 | gpiod_get_value(gpio->bmiddle)); |
| 60 | if (gpio->bright) |
Hans-Christian Egtvedt | 5f56550 | 2007-06-14 23:32:35 -0400 | [diff] [blame] | 61 | input_report_key(input, BTN_RIGHT, |
Linus Walleij | 836bd41 | 2017-10-19 16:24:24 -0700 | [diff] [blame^] | 62 | gpiod_get_value(gpio->bright)); |
Hans-Christian Egtvedt | 5f56550 | 2007-06-14 23:32:35 -0400 | [diff] [blame] | 63 | |
Linus Walleij | 836bd41 | 2017-10-19 16:24:24 -0700 | [diff] [blame^] | 64 | x = gpiod_get_value(gpio->right) - gpiod_get_value(gpio->left); |
| 65 | y = gpiod_get_value(gpio->down) - gpiod_get_value(gpio->up); |
Hans-Christian Egtvedt | 5f56550 | 2007-06-14 23:32:35 -0400 | [diff] [blame] | 66 | |
| 67 | input_report_rel(input, REL_X, x); |
| 68 | input_report_rel(input, REL_Y, y); |
| 69 | input_sync(input); |
| 70 | } |
| 71 | |
Bill Pemberton | 5298cc4 | 2012-11-23 21:38:25 -0800 | [diff] [blame] | 72 | static int gpio_mouse_probe(struct platform_device *pdev) |
Hans-Christian Egtvedt | 5f56550 | 2007-06-14 23:32:35 -0400 | [diff] [blame] | 73 | { |
Linus Walleij | c5053e6 | 2017-10-19 16:19:03 -0700 | [diff] [blame] | 74 | struct device *dev = &pdev->dev; |
Linus Walleij | 34cf5a1 | 2017-10-19 16:19:41 -0700 | [diff] [blame] | 75 | struct gpio_mouse *gmouse; |
Hans-Christian Egtvedt | 5f56550 | 2007-06-14 23:32:35 -0400 | [diff] [blame] | 76 | struct input_polled_dev *input_poll; |
| 77 | struct input_dev *input; |
Linus Walleij | 836bd41 | 2017-10-19 16:24:24 -0700 | [diff] [blame^] | 78 | int ret; |
Hans-Christian Egtvedt | 5f56550 | 2007-06-14 23:32:35 -0400 | [diff] [blame] | 79 | |
Linus Walleij | 34cf5a1 | 2017-10-19 16:19:41 -0700 | [diff] [blame] | 80 | gmouse = devm_kzalloc(dev, sizeof(*gmouse), GFP_KERNEL); |
| 81 | if (!gmouse) |
Linus Walleij | c5053e6 | 2017-10-19 16:19:03 -0700 | [diff] [blame] | 82 | return -ENOMEM; |
Hans-Christian Egtvedt | 5f56550 | 2007-06-14 23:32:35 -0400 | [diff] [blame] | 83 | |
Linus Walleij | 836bd41 | 2017-10-19 16:24:24 -0700 | [diff] [blame^] | 84 | /* Assign some default scanning time */ |
| 85 | ret = device_property_read_u32(dev, "scan-interval-ms", |
| 86 | &gmouse->scan_ms); |
| 87 | if (ret || gmouse->scan_ms == 0) { |
| 88 | dev_warn(dev, "invalid scan time, set to 50 ms\n"); |
| 89 | gmouse->scan_ms = 50; |
Hans-Christian Egtvedt | 5f56550 | 2007-06-14 23:32:35 -0400 | [diff] [blame] | 90 | } |
| 91 | |
Linus Walleij | 836bd41 | 2017-10-19 16:24:24 -0700 | [diff] [blame^] | 92 | gmouse->up = devm_gpiod_get(dev, "up", GPIOD_IN); |
| 93 | if (IS_ERR(gmouse->up)) |
| 94 | return PTR_ERR(gmouse->up); |
| 95 | gmouse->down = devm_gpiod_get(dev, "down", GPIOD_IN); |
| 96 | if (IS_ERR(gmouse->down)) |
| 97 | return PTR_ERR(gmouse->down); |
| 98 | gmouse->left = devm_gpiod_get(dev, "left", GPIOD_IN); |
| 99 | if (IS_ERR(gmouse->left)) |
| 100 | return PTR_ERR(gmouse->left); |
| 101 | gmouse->right = devm_gpiod_get(dev, "right", GPIOD_IN); |
| 102 | if (IS_ERR(gmouse->right)) |
| 103 | return PTR_ERR(gmouse->right); |
Hans-Christian Egtvedt | 5f56550 | 2007-06-14 23:32:35 -0400 | [diff] [blame] | 104 | |
Linus Walleij | 836bd41 | 2017-10-19 16:24:24 -0700 | [diff] [blame^] | 105 | gmouse->bleft = devm_gpiod_get_optional(dev, "button-left", GPIOD_IN); |
| 106 | if (IS_ERR(gmouse->bleft)) |
| 107 | return PTR_ERR(gmouse->bleft); |
| 108 | gmouse->bmiddle = devm_gpiod_get_optional(dev, "button-middle", |
| 109 | GPIOD_IN); |
| 110 | if (IS_ERR(gmouse->bmiddle)) |
| 111 | return PTR_ERR(gmouse->bmiddle); |
| 112 | gmouse->bright = devm_gpiod_get_optional(dev, "button-right", |
| 113 | GPIOD_IN); |
| 114 | if (IS_ERR(gmouse->bright)) |
| 115 | return PTR_ERR(gmouse->bright); |
Hans-Christian Egtvedt | 5f56550 | 2007-06-14 23:32:35 -0400 | [diff] [blame] | 116 | |
Linus Walleij | 836bd41 | 2017-10-19 16:24:24 -0700 | [diff] [blame^] | 117 | input_poll = devm_input_allocate_polled_device(dev); |
Hans-Christian Egtvedt | 5f56550 | 2007-06-14 23:32:35 -0400 | [diff] [blame] | 118 | if (!input_poll) { |
Linus Walleij | 836bd41 | 2017-10-19 16:24:24 -0700 | [diff] [blame^] | 119 | dev_err(dev, "not enough memory for input device\n"); |
| 120 | return -ENOMEM; |
Hans-Christian Egtvedt | 5f56550 | 2007-06-14 23:32:35 -0400 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | platform_set_drvdata(pdev, input_poll); |
| 124 | |
| 125 | /* set input-polldev handlers */ |
Linus Walleij | 34cf5a1 | 2017-10-19 16:19:41 -0700 | [diff] [blame] | 126 | input_poll->private = gmouse; |
Hans-Christian Egtvedt | 5f56550 | 2007-06-14 23:32:35 -0400 | [diff] [blame] | 127 | input_poll->poll = gpio_mouse_scan; |
Linus Walleij | 34cf5a1 | 2017-10-19 16:19:41 -0700 | [diff] [blame] | 128 | input_poll->poll_interval = gmouse->scan_ms; |
Hans-Christian Egtvedt | 5f56550 | 2007-06-14 23:32:35 -0400 | [diff] [blame] | 129 | |
| 130 | input = input_poll->input; |
| 131 | input->name = pdev->name; |
| 132 | input->id.bustype = BUS_HOST; |
| 133 | input->dev.parent = &pdev->dev; |
| 134 | |
| 135 | input_set_capability(input, EV_REL, REL_X); |
| 136 | input_set_capability(input, EV_REL, REL_Y); |
Linus Walleij | 836bd41 | 2017-10-19 16:24:24 -0700 | [diff] [blame^] | 137 | if (gmouse->bleft) |
Hans-Christian Egtvedt | 5f56550 | 2007-06-14 23:32:35 -0400 | [diff] [blame] | 138 | input_set_capability(input, EV_KEY, BTN_LEFT); |
Linus Walleij | 836bd41 | 2017-10-19 16:24:24 -0700 | [diff] [blame^] | 139 | if (gmouse->bmiddle) |
Hans-Christian Egtvedt | 5f56550 | 2007-06-14 23:32:35 -0400 | [diff] [blame] | 140 | input_set_capability(input, EV_KEY, BTN_MIDDLE); |
Linus Walleij | 836bd41 | 2017-10-19 16:24:24 -0700 | [diff] [blame^] | 141 | if (gmouse->bright) |
Hans-Christian Egtvedt | 5f56550 | 2007-06-14 23:32:35 -0400 | [diff] [blame] | 142 | input_set_capability(input, EV_KEY, BTN_RIGHT); |
| 143 | |
Linus Walleij | 836bd41 | 2017-10-19 16:24:24 -0700 | [diff] [blame^] | 144 | ret = input_register_polled_device(input_poll); |
| 145 | if (ret) { |
| 146 | dev_err(dev, "could not register input device\n"); |
| 147 | return ret; |
Hans-Christian Egtvedt | 5f56550 | 2007-06-14 23:32:35 -0400 | [diff] [blame] | 148 | } |
| 149 | |
Linus Walleij | 836bd41 | 2017-10-19 16:24:24 -0700 | [diff] [blame^] | 150 | dev_dbg(dev, "%d ms scan time, buttons: %s%s%s\n", |
| 151 | gmouse->scan_ms, |
| 152 | gmouse->bleft ? "" : "left ", |
| 153 | gmouse->bmiddle ? "" : "middle ", |
| 154 | gmouse->bright ? "" : "right"); |
Hans-Christian Egtvedt | 5f56550 | 2007-06-14 23:32:35 -0400 | [diff] [blame] | 155 | |
Hans-Christian Egtvedt | 5f56550 | 2007-06-14 23:32:35 -0400 | [diff] [blame] | 156 | return 0; |
| 157 | } |
| 158 | |
Roel Kluin | 0de048a | 2008-12-20 05:19:43 -0500 | [diff] [blame] | 159 | static struct platform_driver gpio_mouse_device_driver = { |
Saeed Bishara | eeafa5e | 2009-07-07 22:11:52 -0700 | [diff] [blame] | 160 | .probe = gpio_mouse_probe, |
Hans-Christian Egtvedt | 5f56550 | 2007-06-14 23:32:35 -0400 | [diff] [blame] | 161 | .driver = { |
| 162 | .name = "gpio_mouse", |
Hans-Christian Egtvedt | 5f56550 | 2007-06-14 23:32:35 -0400 | [diff] [blame] | 163 | } |
| 164 | }; |
JJ Ding | 4fcdeac | 2011-11-29 11:08:41 -0800 | [diff] [blame] | 165 | module_platform_driver(gpio_mouse_device_driver); |
Hans-Christian Egtvedt | 5f56550 | 2007-06-14 23:32:35 -0400 | [diff] [blame] | 166 | |
Hans-Christian Egtvedt | 7c40952 | 2011-06-29 00:13:26 -0700 | [diff] [blame] | 167 | MODULE_AUTHOR("Hans-Christian Egtvedt <egtvedt@samfundet.no>"); |
Hans-Christian Egtvedt | 5f56550 | 2007-06-14 23:32:35 -0400 | [diff] [blame] | 168 | MODULE_DESCRIPTION("GPIO mouse driver"); |
| 169 | MODULE_LICENSE("GPL"); |
Saeed Bishara | eeafa5e | 2009-07-07 22:11:52 -0700 | [diff] [blame] | 170 | MODULE_ALIAS("platform:gpio_mouse"); /* work with hotplug and coldplug */ |