xref: /openbmc/linux/drivers/input/misc/gpio_decoder.c (revision ff68cf0b166efd626e653d9ca55f1a1ec74f3667)
169464161SVignesh R /*
269464161SVignesh R  * Copyright (C) 2016 Texas Instruments Incorporated - http://www.ti.com/
369464161SVignesh R  *
469464161SVignesh R  * This program is free software; you can redistribute it and/or
569464161SVignesh R  * modify it under the terms of the GNU General Public License as
669464161SVignesh R  * published by the Free Software Foundation version 2.
769464161SVignesh R  *
869464161SVignesh R  * This program is distributed "as is" WITHOUT ANY WARRANTY of any
969464161SVignesh R  * kind, whether express or implied; without even the implied warranty
1069464161SVignesh R  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1169464161SVignesh R  * GNU General Public License for more details.
1269464161SVignesh R  *
1369464161SVignesh R  * A generic driver to read multiple gpio lines and translate the
1469464161SVignesh R  * encoded numeric value into an input event.
1569464161SVignesh R  */
1669464161SVignesh R 
1769464161SVignesh R #include <linux/device.h>
1869464161SVignesh R #include <linux/gpio/consumer.h>
1969464161SVignesh R #include <linux/input.h>
2069464161SVignesh R #include <linux/kernel.h>
2169464161SVignesh R #include <linux/module.h>
2269464161SVignesh R #include <linux/of.h>
2369464161SVignesh R #include <linux/platform_device.h>
2469464161SVignesh R 
2569464161SVignesh R struct gpio_decoder {
2669464161SVignesh R 	struct gpio_descs *input_gpios;
2769464161SVignesh R 	struct device *dev;
2869464161SVignesh R 	u32 axis;
2969464161SVignesh R 	u32 last_stable;
3069464161SVignesh R };
3169464161SVignesh R 
3269464161SVignesh R static int gpio_decoder_get_gpios_state(struct gpio_decoder *decoder)
3369464161SVignesh R {
3469464161SVignesh R 	struct gpio_descs *gpios = decoder->input_gpios;
3569464161SVignesh R 	unsigned int ret = 0;
3669464161SVignesh R 	int i, val;
3769464161SVignesh R 
3869464161SVignesh R 	for (i = 0; i < gpios->ndescs; i++) {
3969464161SVignesh R 		val = gpiod_get_value_cansleep(gpios->desc[i]);
4069464161SVignesh R 		if (val < 0) {
4169464161SVignesh R 			dev_err(decoder->dev,
4269464161SVignesh R 				"Error reading gpio %d: %d\n",
4369464161SVignesh R 				desc_to_gpio(gpios->desc[i]), val);
4469464161SVignesh R 			return val;
4569464161SVignesh R 		}
4669464161SVignesh R 
4769464161SVignesh R 		val = !!val;
4869464161SVignesh R 		ret = (ret << 1) | val;
4969464161SVignesh R 	}
5069464161SVignesh R 
5169464161SVignesh R 	return ret;
5269464161SVignesh R }
5369464161SVignesh R 
54*ff68cf0bSDmitry Torokhov static void gpio_decoder_poll_gpios(struct input_dev *input)
5569464161SVignesh R {
56*ff68cf0bSDmitry Torokhov 	struct gpio_decoder *decoder = input_get_drvdata(input);
5769464161SVignesh R 	int state;
5869464161SVignesh R 
5969464161SVignesh R 	state = gpio_decoder_get_gpios_state(decoder);
6069464161SVignesh R 	if (state >= 0 && state != decoder->last_stable) {
61*ff68cf0bSDmitry Torokhov 		input_report_abs(input, decoder->axis, state);
62*ff68cf0bSDmitry Torokhov 		input_sync(input);
6369464161SVignesh R 		decoder->last_stable = state;
6469464161SVignesh R 	}
6569464161SVignesh R }
6669464161SVignesh R 
6769464161SVignesh R static int gpio_decoder_probe(struct platform_device *pdev)
6869464161SVignesh R {
6969464161SVignesh R 	struct device *dev = &pdev->dev;
7069464161SVignesh R 	struct gpio_decoder *decoder;
71*ff68cf0bSDmitry Torokhov 	struct input_dev *input;
7269464161SVignesh R 	u32  max;
7369464161SVignesh R 	int err;
7469464161SVignesh R 
75*ff68cf0bSDmitry Torokhov 	decoder = devm_kzalloc(dev, sizeof(*decoder), GFP_KERNEL);
7669464161SVignesh R 	if (!decoder)
7769464161SVignesh R 		return -ENOMEM;
7869464161SVignesh R 
79*ff68cf0bSDmitry Torokhov 	decoder->dev = dev;
8069464161SVignesh R 	device_property_read_u32(dev, "linux,axis", &decoder->axis);
81*ff68cf0bSDmitry Torokhov 
8269464161SVignesh R 	decoder->input_gpios = devm_gpiod_get_array(dev, NULL, GPIOD_IN);
8369464161SVignesh R 	if (IS_ERR(decoder->input_gpios)) {
8469464161SVignesh R 		dev_err(dev, "unable to acquire input gpios\n");
8569464161SVignesh R 		return PTR_ERR(decoder->input_gpios);
8669464161SVignesh R 	}
87*ff68cf0bSDmitry Torokhov 
8869464161SVignesh R 	if (decoder->input_gpios->ndescs < 2) {
8969464161SVignesh R 		dev_err(dev, "not enough gpios found\n");
9069464161SVignesh R 		return -EINVAL;
9169464161SVignesh R 	}
9269464161SVignesh R 
9369464161SVignesh R 	if (device_property_read_u32(dev, "decoder-max-value", &max))
9469464161SVignesh R 		max = (1U << decoder->input_gpios->ndescs) - 1;
9569464161SVignesh R 
96*ff68cf0bSDmitry Torokhov 	input = devm_input_allocate_device(dev);
97*ff68cf0bSDmitry Torokhov 	if (!input)
9869464161SVignesh R 		return -ENOMEM;
9969464161SVignesh R 
100*ff68cf0bSDmitry Torokhov 	input_set_drvdata(input, decoder);
10169464161SVignesh R 
102*ff68cf0bSDmitry Torokhov 	input->name = pdev->name;
103*ff68cf0bSDmitry Torokhov 	input->id.bustype = BUS_HOST;
104*ff68cf0bSDmitry Torokhov 	input_set_abs_params(input, decoder->axis, 0, max, 0, 0);
10569464161SVignesh R 
106*ff68cf0bSDmitry Torokhov 	err = input_setup_polling(input, gpio_decoder_poll_gpios);
10769464161SVignesh R 	if (err) {
108*ff68cf0bSDmitry Torokhov 		dev_err(dev, "failed to set up polling\n");
109*ff68cf0bSDmitry Torokhov 		return err;
110*ff68cf0bSDmitry Torokhov 	}
111*ff68cf0bSDmitry Torokhov 
112*ff68cf0bSDmitry Torokhov 	err = input_register_device(input);
113*ff68cf0bSDmitry Torokhov 	if (err) {
114*ff68cf0bSDmitry Torokhov 		dev_err(dev, "failed to register input device\n");
11569464161SVignesh R 		return err;
11669464161SVignesh R 	}
11769464161SVignesh R 
11869464161SVignesh R 	return 0;
11969464161SVignesh R }
12069464161SVignesh R 
12169464161SVignesh R #ifdef CONFIG_OF
12269464161SVignesh R static const struct of_device_id gpio_decoder_of_match[] = {
12369464161SVignesh R 	{ .compatible = "gpio-decoder", },
12469464161SVignesh R 	{ },
12569464161SVignesh R };
12669464161SVignesh R MODULE_DEVICE_TABLE(of, gpio_decoder_of_match);
12769464161SVignesh R #endif
12869464161SVignesh R 
12969464161SVignesh R static struct platform_driver gpio_decoder_driver = {
13069464161SVignesh R 	.probe		= gpio_decoder_probe,
13169464161SVignesh R 	.driver		= {
13269464161SVignesh R 		.name	= "gpio-decoder",
13369464161SVignesh R 		.of_match_table = of_match_ptr(gpio_decoder_of_match),
13469464161SVignesh R 	}
13569464161SVignesh R };
13669464161SVignesh R module_platform_driver(gpio_decoder_driver);
13769464161SVignesh R 
13869464161SVignesh R MODULE_DESCRIPTION("GPIO decoder input driver");
13969464161SVignesh R MODULE_AUTHOR("Vignesh R <vigneshr@ti.com>");
14069464161SVignesh R MODULE_LICENSE("GPL v2");
141