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 |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License version 2 as |
| 8 | * published by the Free Software Foundation. |
| 9 | */ |
| 10 | |
Hans-Christian Egtvedt | 5f56550 | 2007-06-14 23:32:35 -0400 | [diff] [blame] | 11 | #include <linux/module.h> |
| 12 | #include <linux/platform_device.h> |
| 13 | #include <linux/input-polldev.h> |
Mark Brown | 55158c8 | 2012-04-10 09:03:03 -0700 | [diff] [blame] | 14 | #include <linux/gpio.h> |
Hans-Christian Egtvedt | 5f56550 | 2007-06-14 23:32:35 -0400 | [diff] [blame] | 15 | #include <linux/gpio_mouse.h> |
| 16 | |
Hans-Christian Egtvedt | 5f56550 | 2007-06-14 23:32:35 -0400 | [diff] [blame] | 17 | |
| 18 | /* |
| 19 | * 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] | 20 | * 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] | 21 | */ |
| 22 | static void gpio_mouse_scan(struct input_polled_dev *dev) |
| 23 | { |
| 24 | struct gpio_mouse_platform_data *gpio = dev->private; |
| 25 | struct input_dev *input = dev->input; |
| 26 | int x, y; |
| 27 | |
| 28 | if (gpio->bleft >= 0) |
| 29 | input_report_key(input, BTN_LEFT, |
| 30 | gpio_get_value(gpio->bleft) ^ gpio->polarity); |
| 31 | if (gpio->bmiddle >= 0) |
| 32 | input_report_key(input, BTN_MIDDLE, |
| 33 | gpio_get_value(gpio->bmiddle) ^ gpio->polarity); |
| 34 | if (gpio->bright >= 0) |
| 35 | input_report_key(input, BTN_RIGHT, |
| 36 | gpio_get_value(gpio->bright) ^ gpio->polarity); |
| 37 | |
| 38 | x = (gpio_get_value(gpio->right) ^ gpio->polarity) |
| 39 | - (gpio_get_value(gpio->left) ^ gpio->polarity); |
| 40 | y = (gpio_get_value(gpio->down) ^ gpio->polarity) |
| 41 | - (gpio_get_value(gpio->up) ^ gpio->polarity); |
| 42 | |
| 43 | input_report_rel(input, REL_X, x); |
| 44 | input_report_rel(input, REL_Y, y); |
| 45 | input_sync(input); |
| 46 | } |
| 47 | |
Bill Pemberton | 5298cc4 | 2012-11-23 21:38:25 -0800 | [diff] [blame] | 48 | static int gpio_mouse_probe(struct platform_device *pdev) |
Hans-Christian Egtvedt | 5f56550 | 2007-06-14 23:32:35 -0400 | [diff] [blame] | 49 | { |
Jingoo Han | c838cb3 | 2013-12-05 19:21:10 -0800 | [diff] [blame] | 50 | struct gpio_mouse_platform_data *pdata = dev_get_platdata(&pdev->dev); |
Hans-Christian Egtvedt | 5f56550 | 2007-06-14 23:32:35 -0400 | [diff] [blame] | 51 | struct input_polled_dev *input_poll; |
| 52 | struct input_dev *input; |
| 53 | int pin, i; |
| 54 | int error; |
| 55 | |
| 56 | if (!pdata) { |
| 57 | dev_err(&pdev->dev, "no platform data\n"); |
| 58 | error = -ENXIO; |
| 59 | goto out; |
| 60 | } |
| 61 | |
| 62 | if (pdata->scan_ms < 0) { |
| 63 | dev_err(&pdev->dev, "invalid scan time\n"); |
| 64 | error = -EINVAL; |
| 65 | goto out; |
| 66 | } |
| 67 | |
| 68 | for (i = 0; i < GPIO_MOUSE_PIN_MAX; i++) { |
| 69 | pin = pdata->pins[i]; |
| 70 | |
| 71 | if (pin < 0) { |
| 72 | |
| 73 | if (i <= GPIO_MOUSE_PIN_RIGHT) { |
| 74 | /* Mouse direction is required. */ |
| 75 | dev_err(&pdev->dev, |
| 76 | "missing GPIO for directions\n"); |
| 77 | error = -EINVAL; |
| 78 | goto out_free_gpios; |
| 79 | } |
| 80 | |
| 81 | if (i == GPIO_MOUSE_PIN_BLEFT) |
| 82 | dev_dbg(&pdev->dev, "no left button defined\n"); |
| 83 | |
| 84 | } else { |
| 85 | error = gpio_request(pin, "gpio_mouse"); |
| 86 | if (error) { |
| 87 | dev_err(&pdev->dev, "fail %d pin (%d idx)\n", |
| 88 | pin, i); |
| 89 | goto out_free_gpios; |
| 90 | } |
| 91 | |
| 92 | gpio_direction_input(pin); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | input_poll = input_allocate_polled_device(); |
| 97 | if (!input_poll) { |
| 98 | dev_err(&pdev->dev, "not enough memory for input device\n"); |
| 99 | error = -ENOMEM; |
| 100 | goto out_free_gpios; |
| 101 | } |
| 102 | |
| 103 | platform_set_drvdata(pdev, input_poll); |
| 104 | |
| 105 | /* set input-polldev handlers */ |
| 106 | input_poll->private = pdata; |
| 107 | input_poll->poll = gpio_mouse_scan; |
| 108 | input_poll->poll_interval = pdata->scan_ms; |
| 109 | |
| 110 | input = input_poll->input; |
| 111 | input->name = pdev->name; |
| 112 | input->id.bustype = BUS_HOST; |
| 113 | input->dev.parent = &pdev->dev; |
| 114 | |
| 115 | input_set_capability(input, EV_REL, REL_X); |
| 116 | input_set_capability(input, EV_REL, REL_Y); |
| 117 | if (pdata->bleft >= 0) |
| 118 | input_set_capability(input, EV_KEY, BTN_LEFT); |
| 119 | if (pdata->bmiddle >= 0) |
| 120 | input_set_capability(input, EV_KEY, BTN_MIDDLE); |
| 121 | if (pdata->bright >= 0) |
| 122 | input_set_capability(input, EV_KEY, BTN_RIGHT); |
| 123 | |
| 124 | error = input_register_polled_device(input_poll); |
| 125 | if (error) { |
| 126 | dev_err(&pdev->dev, "could not register input device\n"); |
| 127 | goto out_free_polldev; |
| 128 | } |
| 129 | |
| 130 | dev_dbg(&pdev->dev, "%d ms scan time, buttons: %s%s%s\n", |
| 131 | pdata->scan_ms, |
| 132 | pdata->bleft < 0 ? "" : "left ", |
| 133 | pdata->bmiddle < 0 ? "" : "middle ", |
| 134 | pdata->bright < 0 ? "" : "right"); |
| 135 | |
| 136 | return 0; |
| 137 | |
| 138 | out_free_polldev: |
| 139 | input_free_polled_device(input_poll); |
Hans-Christian Egtvedt | 5f56550 | 2007-06-14 23:32:35 -0400 | [diff] [blame] | 140 | |
| 141 | out_free_gpios: |
| 142 | while (--i >= 0) { |
| 143 | pin = pdata->pins[i]; |
| 144 | if (pin) |
| 145 | gpio_free(pin); |
| 146 | } |
| 147 | out: |
| 148 | return error; |
| 149 | } |
| 150 | |
Bill Pemberton | e2619cf | 2012-11-23 21:50:47 -0800 | [diff] [blame] | 151 | static int gpio_mouse_remove(struct platform_device *pdev) |
Hans-Christian Egtvedt | 5f56550 | 2007-06-14 23:32:35 -0400 | [diff] [blame] | 152 | { |
| 153 | struct input_polled_dev *input = platform_get_drvdata(pdev); |
| 154 | struct gpio_mouse_platform_data *pdata = input->private; |
| 155 | int pin, i; |
| 156 | |
| 157 | input_unregister_polled_device(input); |
| 158 | input_free_polled_device(input); |
| 159 | |
| 160 | for (i = 0; i < GPIO_MOUSE_PIN_MAX; i++) { |
| 161 | pin = pdata->pins[i]; |
| 162 | if (pin >= 0) |
| 163 | gpio_free(pin); |
| 164 | } |
| 165 | |
Hans-Christian Egtvedt | 5f56550 | 2007-06-14 23:32:35 -0400 | [diff] [blame] | 166 | return 0; |
| 167 | } |
| 168 | |
Roel Kluin | 0de048a | 2008-12-20 05:19:43 -0500 | [diff] [blame] | 169 | static struct platform_driver gpio_mouse_device_driver = { |
Saeed Bishara | eeafa5e | 2009-07-07 22:11:52 -0700 | [diff] [blame] | 170 | .probe = gpio_mouse_probe, |
Bill Pemberton | 1cb0aa8 | 2012-11-23 21:27:39 -0800 | [diff] [blame] | 171 | .remove = gpio_mouse_remove, |
Hans-Christian Egtvedt | 5f56550 | 2007-06-14 23:32:35 -0400 | [diff] [blame] | 172 | .driver = { |
| 173 | .name = "gpio_mouse", |
Hans-Christian Egtvedt | 5f56550 | 2007-06-14 23:32:35 -0400 | [diff] [blame] | 174 | } |
| 175 | }; |
JJ Ding | 4fcdeac | 2011-11-29 11:08:41 -0800 | [diff] [blame] | 176 | module_platform_driver(gpio_mouse_device_driver); |
Hans-Christian Egtvedt | 5f56550 | 2007-06-14 23:32:35 -0400 | [diff] [blame] | 177 | |
Hans-Christian Egtvedt | 7c40952 | 2011-06-29 00:13:26 -0700 | [diff] [blame] | 178 | MODULE_AUTHOR("Hans-Christian Egtvedt <egtvedt@samfundet.no>"); |
Hans-Christian Egtvedt | 5f56550 | 2007-06-14 23:32:35 -0400 | [diff] [blame] | 179 | MODULE_DESCRIPTION("GPIO mouse driver"); |
| 180 | MODULE_LICENSE("GPL"); |
Saeed Bishara | eeafa5e | 2009-07-07 22:11:52 -0700 | [diff] [blame] | 181 | MODULE_ALIAS("platform:gpio_mouse"); /* work with hotplug and coldplug */ |
| 182 | |