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) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2)  * Copyright (C) 2016 Texas Instruments Incorporated - http://www.ti.com/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * This program is free software; you can redistribute it and/or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * modify it under the terms of the GNU General Public License as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * published by the Free Software Foundation version 2.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  * This program is distributed "as is" WITHOUT ANY WARRANTY of any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  * kind, whether express or implied; without even the implied warranty
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  * GNU General Public License for more details.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13)  * A generic driver to read multiple gpio lines and translate the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14)  * encoded numeric value into an input event.
^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) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/gpio/consumer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/input.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include <linux/of.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) struct gpio_decoder {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	struct gpio_descs *input_gpios;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	u32 axis;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	u32 last_stable;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) static int gpio_decoder_get_gpios_state(struct gpio_decoder *decoder)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	struct gpio_descs *gpios = decoder->input_gpios;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	unsigned int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	int i, val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	for (i = 0; i < gpios->ndescs; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 		val = gpiod_get_value_cansleep(gpios->desc[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 		if (val < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 			dev_err(decoder->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 				"Error reading gpio %d: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 				desc_to_gpio(gpios->desc[i]), val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 			return val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 		val = !!val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 		ret = (ret << 1) | val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) static void gpio_decoder_poll_gpios(struct input_dev *input)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	struct gpio_decoder *decoder = input_get_drvdata(input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	int state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	state = gpio_decoder_get_gpios_state(decoder);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	if (state >= 0 && state != decoder->last_stable) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 		input_report_abs(input, decoder->axis, state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 		input_sync(input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 		decoder->last_stable = state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) static int gpio_decoder_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	struct device *dev = &pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	struct gpio_decoder *decoder;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	struct input_dev *input;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	u32  max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	decoder = devm_kzalloc(dev, sizeof(*decoder), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	if (!decoder)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	decoder->dev = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	device_property_read_u32(dev, "linux,axis", &decoder->axis);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	decoder->input_gpios = devm_gpiod_get_array(dev, NULL, GPIOD_IN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	if (IS_ERR(decoder->input_gpios)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 		dev_err(dev, "unable to acquire input gpios\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 		return PTR_ERR(decoder->input_gpios);
^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) 	if (decoder->input_gpios->ndescs < 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 		dev_err(dev, "not enough gpios found\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	if (device_property_read_u32(dev, "decoder-max-value", &max))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 		max = (1U << decoder->input_gpios->ndescs) - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	input = devm_input_allocate_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	if (!input)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	input_set_drvdata(input, decoder);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	input->name = pdev->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	input->id.bustype = BUS_HOST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	input_set_abs_params(input, decoder->axis, 0, max, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	err = input_setup_polling(input, gpio_decoder_poll_gpios);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 		dev_err(dev, "failed to set up polling\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	err = input_register_device(input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 		dev_err(dev, "failed to register input device\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) #ifdef CONFIG_OF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) static const struct of_device_id gpio_decoder_of_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	{ .compatible = "gpio-decoder", },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	{ },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) MODULE_DEVICE_TABLE(of, gpio_decoder_of_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) static struct platform_driver gpio_decoder_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	.probe		= gpio_decoder_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	.driver		= {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 		.name	= "gpio-decoder",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 		.of_match_table = of_match_ptr(gpio_decoder_of_match),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) module_platform_driver(gpio_decoder_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) MODULE_DESCRIPTION("GPIO decoder input driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) MODULE_AUTHOR("Vignesh R <vigneshr@ti.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) MODULE_LICENSE("GPL v2");