1*2aec85b2SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
269464161SVignesh R /*
369464161SVignesh R  * Copyright (C) 2016 Texas Instruments Incorporated - http://www.ti.com/
469464161SVignesh R  *
569464161SVignesh R  * A generic driver to read multiple gpio lines and translate the
669464161SVignesh R  * encoded numeric value into an input event.
769464161SVignesh R  */
869464161SVignesh R 
969464161SVignesh R #include <linux/device.h>
1069464161SVignesh R #include <linux/gpio/consumer.h>
1169464161SVignesh R #include <linux/input.h>
1269464161SVignesh R #include <linux/kernel.h>
1369464161SVignesh R #include <linux/module.h>
1469464161SVignesh R #include <linux/of.h>
1569464161SVignesh R #include <linux/platform_device.h>
1669464161SVignesh R 
1769464161SVignesh R struct gpio_decoder {
1869464161SVignesh R 	struct gpio_descs *input_gpios;
1969464161SVignesh R 	struct device *dev;
2069464161SVignesh R 	u32 axis;
2169464161SVignesh R 	u32 last_stable;
2269464161SVignesh R };
2369464161SVignesh R 
gpio_decoder_get_gpios_state(struct gpio_decoder * decoder)2469464161SVignesh R static int gpio_decoder_get_gpios_state(struct gpio_decoder *decoder)
2569464161SVignesh R {
2669464161SVignesh R 	struct gpio_descs *gpios = decoder->input_gpios;
2769464161SVignesh R 	unsigned int ret = 0;
2869464161SVignesh R 	int i, val;
2969464161SVignesh R 
3069464161SVignesh R 	for (i = 0; i < gpios->ndescs; i++) {
3169464161SVignesh R 		val = gpiod_get_value_cansleep(gpios->desc[i]);
3269464161SVignesh R 		if (val < 0) {
3369464161SVignesh R 			dev_err(decoder->dev,
3469464161SVignesh R 				"Error reading gpio %d: %d\n",
3569464161SVignesh R 				desc_to_gpio(gpios->desc[i]), val);
3669464161SVignesh R 			return val;
3769464161SVignesh R 		}
3869464161SVignesh R 
3969464161SVignesh R 		val = !!val;
4069464161SVignesh R 		ret = (ret << 1) | val;
4169464161SVignesh R 	}
4269464161SVignesh R 
4369464161SVignesh R 	return ret;
4469464161SVignesh R }
4569464161SVignesh R 
gpio_decoder_poll_gpios(struct input_dev * input)46ff68cf0bSDmitry Torokhov static void gpio_decoder_poll_gpios(struct input_dev *input)
4769464161SVignesh R {
48ff68cf0bSDmitry Torokhov 	struct gpio_decoder *decoder = input_get_drvdata(input);
4969464161SVignesh R 	int state;
5069464161SVignesh R 
5169464161SVignesh R 	state = gpio_decoder_get_gpios_state(decoder);
5269464161SVignesh R 	if (state >= 0 && state != decoder->last_stable) {
53ff68cf0bSDmitry Torokhov 		input_report_abs(input, decoder->axis, state);
54ff68cf0bSDmitry Torokhov 		input_sync(input);
5569464161SVignesh R 		decoder->last_stable = state;
5669464161SVignesh R 	}
5769464161SVignesh R }
5869464161SVignesh R 
gpio_decoder_probe(struct platform_device * pdev)5969464161SVignesh R static int gpio_decoder_probe(struct platform_device *pdev)
6069464161SVignesh R {
6169464161SVignesh R 	struct device *dev = &pdev->dev;
6269464161SVignesh R 	struct gpio_decoder *decoder;
63ff68cf0bSDmitry Torokhov 	struct input_dev *input;
6469464161SVignesh R 	u32  max;
6569464161SVignesh R 	int err;
6669464161SVignesh R 
67ff68cf0bSDmitry Torokhov 	decoder = devm_kzalloc(dev, sizeof(*decoder), GFP_KERNEL);
6869464161SVignesh R 	if (!decoder)
6969464161SVignesh R 		return -ENOMEM;
7069464161SVignesh R 
71ff68cf0bSDmitry Torokhov 	decoder->dev = dev;
7269464161SVignesh R 	device_property_read_u32(dev, "linux,axis", &decoder->axis);
73ff68cf0bSDmitry Torokhov 
7469464161SVignesh R 	decoder->input_gpios = devm_gpiod_get_array(dev, NULL, GPIOD_IN);
7569464161SVignesh R 	if (IS_ERR(decoder->input_gpios)) {
7669464161SVignesh R 		dev_err(dev, "unable to acquire input gpios\n");
7769464161SVignesh R 		return PTR_ERR(decoder->input_gpios);
7869464161SVignesh R 	}
79ff68cf0bSDmitry Torokhov 
8069464161SVignesh R 	if (decoder->input_gpios->ndescs < 2) {
8169464161SVignesh R 		dev_err(dev, "not enough gpios found\n");
8269464161SVignesh R 		return -EINVAL;
8369464161SVignesh R 	}
8469464161SVignesh R 
8569464161SVignesh R 	if (device_property_read_u32(dev, "decoder-max-value", &max))
8669464161SVignesh R 		max = (1U << decoder->input_gpios->ndescs) - 1;
8769464161SVignesh R 
88ff68cf0bSDmitry Torokhov 	input = devm_input_allocate_device(dev);
89ff68cf0bSDmitry Torokhov 	if (!input)
9069464161SVignesh R 		return -ENOMEM;
9169464161SVignesh R 
92ff68cf0bSDmitry Torokhov 	input_set_drvdata(input, decoder);
9369464161SVignesh R 
94ff68cf0bSDmitry Torokhov 	input->name = pdev->name;
95ff68cf0bSDmitry Torokhov 	input->id.bustype = BUS_HOST;
96ff68cf0bSDmitry Torokhov 	input_set_abs_params(input, decoder->axis, 0, max, 0, 0);
9769464161SVignesh R 
98ff68cf0bSDmitry Torokhov 	err = input_setup_polling(input, gpio_decoder_poll_gpios);
9969464161SVignesh R 	if (err) {
100ff68cf0bSDmitry Torokhov 		dev_err(dev, "failed to set up polling\n");
101ff68cf0bSDmitry Torokhov 		return err;
102ff68cf0bSDmitry Torokhov 	}
103ff68cf0bSDmitry Torokhov 
104ff68cf0bSDmitry Torokhov 	err = input_register_device(input);
105ff68cf0bSDmitry Torokhov 	if (err) {
106ff68cf0bSDmitry Torokhov 		dev_err(dev, "failed to register input device\n");
10769464161SVignesh R 		return err;
10869464161SVignesh R 	}
10969464161SVignesh R 
11069464161SVignesh R 	return 0;
11169464161SVignesh R }
11269464161SVignesh R 
11369464161SVignesh R #ifdef CONFIG_OF
11469464161SVignesh R static const struct of_device_id gpio_decoder_of_match[] = {
11569464161SVignesh R 	{ .compatible = "gpio-decoder", },
11669464161SVignesh R 	{ },
11769464161SVignesh R };
11869464161SVignesh R MODULE_DEVICE_TABLE(of, gpio_decoder_of_match);
11969464161SVignesh R #endif
12069464161SVignesh R 
12169464161SVignesh R static struct platform_driver gpio_decoder_driver = {
12269464161SVignesh R 	.probe		= gpio_decoder_probe,
12369464161SVignesh R 	.driver		= {
12469464161SVignesh R 		.name	= "gpio-decoder",
12569464161SVignesh R 		.of_match_table = of_match_ptr(gpio_decoder_of_match),
12669464161SVignesh R 	}
12769464161SVignesh R };
12869464161SVignesh R module_platform_driver(gpio_decoder_driver);
12969464161SVignesh R 
13069464161SVignesh R MODULE_DESCRIPTION("GPIO decoder input driver");
13169464161SVignesh R MODULE_AUTHOR("Vignesh R <vigneshr@ti.com>");
13269464161SVignesh R MODULE_LICENSE("GPL v2");
133