Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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