2007-06-15 11:32:35 +08:00
|
|
|
/*
|
|
|
|
* Driver for simulating a mouse on GPIO lines.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2007 Atmel Corporation
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License version 2 as
|
|
|
|
* published by the Free Software Foundation.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/platform_device.h>
|
|
|
|
#include <linux/input-polldev.h>
|
2012-04-11 00:03:03 +08:00
|
|
|
#include <linux/gpio.h>
|
2007-06-15 11:32:35 +08:00
|
|
|
|
2017-10-20 07:19:03 +08:00
|
|
|
#define GPIO_MOUSE_POLARITY_ACT_HIGH 0x00
|
|
|
|
#define GPIO_MOUSE_POLARITY_ACT_LOW 0x01
|
|
|
|
|
|
|
|
#define GPIO_MOUSE_PIN_UP 0
|
|
|
|
#define GPIO_MOUSE_PIN_DOWN 1
|
|
|
|
#define GPIO_MOUSE_PIN_LEFT 2
|
|
|
|
#define GPIO_MOUSE_PIN_RIGHT 3
|
|
|
|
#define GPIO_MOUSE_PIN_BLEFT 4
|
|
|
|
#define GPIO_MOUSE_PIN_BMIDDLE 5
|
|
|
|
#define GPIO_MOUSE_PIN_BRIGHT 6
|
|
|
|
#define GPIO_MOUSE_PIN_MAX 7
|
|
|
|
|
|
|
|
/**
|
2017-10-20 07:19:41 +08:00
|
|
|
* struct gpio_mouse
|
2017-10-20 07:19:03 +08:00
|
|
|
* @scan_ms: integer in ms specifying the scan periode.
|
|
|
|
* @polarity: Pin polarity, active high or low.
|
|
|
|
* @up: GPIO line for up value.
|
|
|
|
* @down: GPIO line for down value.
|
|
|
|
* @left: GPIO line for left value.
|
|
|
|
* @right: GPIO line for right value.
|
|
|
|
* @bleft: GPIO line for left button.
|
|
|
|
* @bmiddle: GPIO line for middle button.
|
|
|
|
* @bright: GPIO line for right button.
|
|
|
|
* @pins: GPIO line numbers used for the mouse.
|
|
|
|
*
|
|
|
|
* This struct must be added to the platform_device in the board code.
|
|
|
|
* It is used by the gpio_mouse driver to setup GPIO lines and to
|
|
|
|
* calculate mouse movement.
|
|
|
|
*/
|
2017-10-20 07:19:41 +08:00
|
|
|
struct gpio_mouse {
|
2017-10-20 07:19:03 +08:00
|
|
|
int scan_ms;
|
|
|
|
int polarity;
|
|
|
|
|
|
|
|
union {
|
|
|
|
struct {
|
|
|
|
int up;
|
|
|
|
int down;
|
|
|
|
int left;
|
|
|
|
int right;
|
|
|
|
|
|
|
|
int bleft;
|
|
|
|
int bmiddle;
|
|
|
|
int bright;
|
|
|
|
};
|
|
|
|
int pins[GPIO_MOUSE_PIN_MAX];
|
|
|
|
};
|
|
|
|
};
|
2007-06-15 11:32:35 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Timer function which is run every scan_ms ms when the device is opened.
|
2009-01-13 06:35:48 +08:00
|
|
|
* The dev input variable is set to the the input_dev pointer.
|
2007-06-15 11:32:35 +08:00
|
|
|
*/
|
|
|
|
static void gpio_mouse_scan(struct input_polled_dev *dev)
|
|
|
|
{
|
2017-10-20 07:19:41 +08:00
|
|
|
struct gpio_mouse *gpio = dev->private;
|
2007-06-15 11:32:35 +08:00
|
|
|
struct input_dev *input = dev->input;
|
|
|
|
int x, y;
|
|
|
|
|
|
|
|
if (gpio->bleft >= 0)
|
|
|
|
input_report_key(input, BTN_LEFT,
|
|
|
|
gpio_get_value(gpio->bleft) ^ gpio->polarity);
|
|
|
|
if (gpio->bmiddle >= 0)
|
|
|
|
input_report_key(input, BTN_MIDDLE,
|
|
|
|
gpio_get_value(gpio->bmiddle) ^ gpio->polarity);
|
|
|
|
if (gpio->bright >= 0)
|
|
|
|
input_report_key(input, BTN_RIGHT,
|
|
|
|
gpio_get_value(gpio->bright) ^ gpio->polarity);
|
|
|
|
|
|
|
|
x = (gpio_get_value(gpio->right) ^ gpio->polarity)
|
|
|
|
- (gpio_get_value(gpio->left) ^ gpio->polarity);
|
|
|
|
y = (gpio_get_value(gpio->down) ^ gpio->polarity)
|
|
|
|
- (gpio_get_value(gpio->up) ^ gpio->polarity);
|
|
|
|
|
|
|
|
input_report_rel(input, REL_X, x);
|
|
|
|
input_report_rel(input, REL_Y, y);
|
|
|
|
input_sync(input);
|
|
|
|
}
|
|
|
|
|
2012-11-24 13:38:25 +08:00
|
|
|
static int gpio_mouse_probe(struct platform_device *pdev)
|
2007-06-15 11:32:35 +08:00
|
|
|
{
|
2017-10-20 07:19:03 +08:00
|
|
|
struct device *dev = &pdev->dev;
|
2017-10-20 07:19:41 +08:00
|
|
|
struct gpio_mouse *gmouse;
|
2007-06-15 11:32:35 +08:00
|
|
|
struct input_polled_dev *input_poll;
|
|
|
|
struct input_dev *input;
|
|
|
|
int pin, i;
|
|
|
|
int error;
|
|
|
|
|
2017-10-20 07:19:41 +08:00
|
|
|
gmouse = devm_kzalloc(dev, sizeof(*gmouse), GFP_KERNEL);
|
|
|
|
if (!gmouse)
|
2017-10-20 07:19:03 +08:00
|
|
|
return -ENOMEM;
|
2007-06-15 11:32:35 +08:00
|
|
|
|
2017-10-20 07:19:41 +08:00
|
|
|
if (gmouse->scan_ms < 0) {
|
2007-06-15 11:32:35 +08:00
|
|
|
dev_err(&pdev->dev, "invalid scan time\n");
|
|
|
|
error = -EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < GPIO_MOUSE_PIN_MAX; i++) {
|
2017-10-20 07:19:41 +08:00
|
|
|
pin = gmouse->pins[i];
|
2007-06-15 11:32:35 +08:00
|
|
|
|
|
|
|
if (pin < 0) {
|
|
|
|
|
|
|
|
if (i <= GPIO_MOUSE_PIN_RIGHT) {
|
|
|
|
/* Mouse direction is required. */
|
|
|
|
dev_err(&pdev->dev,
|
|
|
|
"missing GPIO for directions\n");
|
|
|
|
error = -EINVAL;
|
|
|
|
goto out_free_gpios;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i == GPIO_MOUSE_PIN_BLEFT)
|
|
|
|
dev_dbg(&pdev->dev, "no left button defined\n");
|
|
|
|
|
|
|
|
} else {
|
|
|
|
error = gpio_request(pin, "gpio_mouse");
|
|
|
|
if (error) {
|
|
|
|
dev_err(&pdev->dev, "fail %d pin (%d idx)\n",
|
|
|
|
pin, i);
|
|
|
|
goto out_free_gpios;
|
|
|
|
}
|
|
|
|
|
|
|
|
gpio_direction_input(pin);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
input_poll = input_allocate_polled_device();
|
|
|
|
if (!input_poll) {
|
|
|
|
dev_err(&pdev->dev, "not enough memory for input device\n");
|
|
|
|
error = -ENOMEM;
|
|
|
|
goto out_free_gpios;
|
|
|
|
}
|
|
|
|
|
|
|
|
platform_set_drvdata(pdev, input_poll);
|
|
|
|
|
|
|
|
/* set input-polldev handlers */
|
2017-10-20 07:19:41 +08:00
|
|
|
input_poll->private = gmouse;
|
2007-06-15 11:32:35 +08:00
|
|
|
input_poll->poll = gpio_mouse_scan;
|
2017-10-20 07:19:41 +08:00
|
|
|
input_poll->poll_interval = gmouse->scan_ms;
|
2007-06-15 11:32:35 +08:00
|
|
|
|
|
|
|
input = input_poll->input;
|
|
|
|
input->name = pdev->name;
|
|
|
|
input->id.bustype = BUS_HOST;
|
|
|
|
input->dev.parent = &pdev->dev;
|
|
|
|
|
|
|
|
input_set_capability(input, EV_REL, REL_X);
|
|
|
|
input_set_capability(input, EV_REL, REL_Y);
|
2017-10-20 07:19:41 +08:00
|
|
|
if (gmouse->bleft >= 0)
|
2007-06-15 11:32:35 +08:00
|
|
|
input_set_capability(input, EV_KEY, BTN_LEFT);
|
2017-10-20 07:19:41 +08:00
|
|
|
if (gmouse->bmiddle >= 0)
|
2007-06-15 11:32:35 +08:00
|
|
|
input_set_capability(input, EV_KEY, BTN_MIDDLE);
|
2017-10-20 07:19:41 +08:00
|
|
|
if (gmouse->bright >= 0)
|
2007-06-15 11:32:35 +08:00
|
|
|
input_set_capability(input, EV_KEY, BTN_RIGHT);
|
|
|
|
|
|
|
|
error = input_register_polled_device(input_poll);
|
|
|
|
if (error) {
|
|
|
|
dev_err(&pdev->dev, "could not register input device\n");
|
|
|
|
goto out_free_polldev;
|
|
|
|
}
|
|
|
|
|
|
|
|
dev_dbg(&pdev->dev, "%d ms scan time, buttons: %s%s%s\n",
|
2017-10-20 07:19:41 +08:00
|
|
|
gmouse->scan_ms,
|
|
|
|
gmouse->bleft < 0 ? "" : "left ",
|
|
|
|
gmouse->bmiddle < 0 ? "" : "middle ",
|
|
|
|
gmouse->bright < 0 ? "" : "right");
|
2007-06-15 11:32:35 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
out_free_polldev:
|
|
|
|
input_free_polled_device(input_poll);
|
|
|
|
|
|
|
|
out_free_gpios:
|
|
|
|
while (--i >= 0) {
|
2017-10-20 07:19:41 +08:00
|
|
|
pin = gmouse->pins[i];
|
2007-06-15 11:32:35 +08:00
|
|
|
if (pin)
|
|
|
|
gpio_free(pin);
|
|
|
|
}
|
|
|
|
out:
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2012-11-24 13:50:47 +08:00
|
|
|
static int gpio_mouse_remove(struct platform_device *pdev)
|
2007-06-15 11:32:35 +08:00
|
|
|
{
|
|
|
|
struct input_polled_dev *input = platform_get_drvdata(pdev);
|
2017-10-20 07:19:41 +08:00
|
|
|
struct gpio_mouse *gmouse = input->private;
|
2007-06-15 11:32:35 +08:00
|
|
|
int pin, i;
|
|
|
|
|
|
|
|
input_unregister_polled_device(input);
|
|
|
|
input_free_polled_device(input);
|
|
|
|
|
|
|
|
for (i = 0; i < GPIO_MOUSE_PIN_MAX; i++) {
|
2017-10-20 07:19:41 +08:00
|
|
|
pin = gmouse->pins[i];
|
2007-06-15 11:32:35 +08:00
|
|
|
if (pin >= 0)
|
|
|
|
gpio_free(pin);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-12-20 18:19:43 +08:00
|
|
|
static struct platform_driver gpio_mouse_device_driver = {
|
2009-07-08 13:11:52 +08:00
|
|
|
.probe = gpio_mouse_probe,
|
2012-11-24 13:27:39 +08:00
|
|
|
.remove = gpio_mouse_remove,
|
2007-06-15 11:32:35 +08:00
|
|
|
.driver = {
|
|
|
|
.name = "gpio_mouse",
|
|
|
|
}
|
|
|
|
};
|
2011-11-30 03:08:41 +08:00
|
|
|
module_platform_driver(gpio_mouse_device_driver);
|
2007-06-15 11:32:35 +08:00
|
|
|
|
2011-06-29 15:13:26 +08:00
|
|
|
MODULE_AUTHOR("Hans-Christian Egtvedt <egtvedt@samfundet.no>");
|
2007-06-15 11:32:35 +08:00
|
|
|
MODULE_DESCRIPTION("GPIO mouse driver");
|
|
|
|
MODULE_LICENSE("GPL");
|
2009-07-08 13:11:52 +08:00
|
|
|
MODULE_ALIAS("platform:gpio_mouse"); /* work with hotplug and coldplug */
|
|
|
|
|