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