Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright (C) 2016 Texas Instruments Incorporated - http://www.ti.com/
0004  *
0005  * A generic driver to read multiple gpio lines and translate the
0006  * encoded numeric value into an input event.
0007  */
0008 
0009 #include <linux/device.h>
0010 #include <linux/gpio/consumer.h>
0011 #include <linux/input.h>
0012 #include <linux/kernel.h>
0013 #include <linux/module.h>
0014 #include <linux/of.h>
0015 #include <linux/platform_device.h>
0016 
0017 struct gpio_decoder {
0018     struct gpio_descs *input_gpios;
0019     struct device *dev;
0020     u32 axis;
0021     u32 last_stable;
0022 };
0023 
0024 static int gpio_decoder_get_gpios_state(struct gpio_decoder *decoder)
0025 {
0026     struct gpio_descs *gpios = decoder->input_gpios;
0027     unsigned int ret = 0;
0028     int i, val;
0029 
0030     for (i = 0; i < gpios->ndescs; i++) {
0031         val = gpiod_get_value_cansleep(gpios->desc[i]);
0032         if (val < 0) {
0033             dev_err(decoder->dev,
0034                 "Error reading gpio %d: %d\n",
0035                 desc_to_gpio(gpios->desc[i]), val);
0036             return val;
0037         }
0038 
0039         val = !!val;
0040         ret = (ret << 1) | val;
0041     }
0042 
0043     return ret;
0044 }
0045 
0046 static void gpio_decoder_poll_gpios(struct input_dev *input)
0047 {
0048     struct gpio_decoder *decoder = input_get_drvdata(input);
0049     int state;
0050 
0051     state = gpio_decoder_get_gpios_state(decoder);
0052     if (state >= 0 && state != decoder->last_stable) {
0053         input_report_abs(input, decoder->axis, state);
0054         input_sync(input);
0055         decoder->last_stable = state;
0056     }
0057 }
0058 
0059 static int gpio_decoder_probe(struct platform_device *pdev)
0060 {
0061     struct device *dev = &pdev->dev;
0062     struct gpio_decoder *decoder;
0063     struct input_dev *input;
0064     u32  max;
0065     int err;
0066 
0067     decoder = devm_kzalloc(dev, sizeof(*decoder), GFP_KERNEL);
0068     if (!decoder)
0069         return -ENOMEM;
0070 
0071     decoder->dev = dev;
0072     device_property_read_u32(dev, "linux,axis", &decoder->axis);
0073 
0074     decoder->input_gpios = devm_gpiod_get_array(dev, NULL, GPIOD_IN);
0075     if (IS_ERR(decoder->input_gpios)) {
0076         dev_err(dev, "unable to acquire input gpios\n");
0077         return PTR_ERR(decoder->input_gpios);
0078     }
0079 
0080     if (decoder->input_gpios->ndescs < 2) {
0081         dev_err(dev, "not enough gpios found\n");
0082         return -EINVAL;
0083     }
0084 
0085     if (device_property_read_u32(dev, "decoder-max-value", &max))
0086         max = (1U << decoder->input_gpios->ndescs) - 1;
0087 
0088     input = devm_input_allocate_device(dev);
0089     if (!input)
0090         return -ENOMEM;
0091 
0092     input_set_drvdata(input, decoder);
0093 
0094     input->name = pdev->name;
0095     input->id.bustype = BUS_HOST;
0096     input_set_abs_params(input, decoder->axis, 0, max, 0, 0);
0097 
0098     err = input_setup_polling(input, gpio_decoder_poll_gpios);
0099     if (err) {
0100         dev_err(dev, "failed to set up polling\n");
0101         return err;
0102     }
0103 
0104     err = input_register_device(input);
0105     if (err) {
0106         dev_err(dev, "failed to register input device\n");
0107         return err;
0108     }
0109 
0110     return 0;
0111 }
0112 
0113 #ifdef CONFIG_OF
0114 static const struct of_device_id gpio_decoder_of_match[] = {
0115     { .compatible = "gpio-decoder", },
0116     { },
0117 };
0118 MODULE_DEVICE_TABLE(of, gpio_decoder_of_match);
0119 #endif
0120 
0121 static struct platform_driver gpio_decoder_driver = {
0122     .probe      = gpio_decoder_probe,
0123     .driver     = {
0124         .name   = "gpio-decoder",
0125         .of_match_table = of_match_ptr(gpio_decoder_of_match),
0126     }
0127 };
0128 module_platform_driver(gpio_decoder_driver);
0129 
0130 MODULE_DESCRIPTION("GPIO decoder input driver");
0131 MODULE_AUTHOR("Vignesh R <vigneshr@ti.com>");
0132 MODULE_LICENSE("GPL v2");