| 1 | |
| 2 | |
| 3 | * Copyright (C) 2016 Texas Instruments Incorporated - http://www.ti.com/
|
| 4 | *
|
| 5 | * A generic driver to read multiple gpio lines and translate the
|
| 6 | * encoded numeric value into an input event.
|
| 7 |
|
| 8 |
|
| 9 | #include <linux/bitmap.h>
|
| 10 | #include <linux/dev_printk.h>
|
| 11 | #include <linux/device/devres.h>
|
| 12 | #include <linux/err.h>
|
| 13 | #include <linux/gpio/consumer.h>
|
| 14 | #include <linux/input.h>
|
| 15 | #include <linux/minmax.h>
|
| 16 | #include <linux/module.h>
|
| 17 | #include <linux/platform_device.h>
|
| 18 | #include <linux/property.h>
|
| 19 | #include <linux/types.h>
|
| 20 |
|
| 21 | struct gpio_decoder {
|
| 22 | struct gpio_descs *input_gpios;
|
| 23 | struct device *dev;
|
| 24 | u32 axis;
|
| 25 | u32 last_stable;
|
| 26 | };
|
| 27 |
|
| 28 | static int gpio_decoder_get_gpios_state(struct gpio_decoder *decoder)
|
| 29 | {
|
| 30 | struct gpio_descs *gpios = decoder->input_gpios;
|
| 31 | DECLARE_BITMAP(values, 32);
|
| 32 | unsigned int size;
|
| 33 | int err;
|
| 34 |
|
| 35 | size = min(gpios->ndescs, 32U);
|
| 36 | err = gpiod_get_array_value_cansleep(size, gpios->desc, gpios->info, values);
|
| 37 | if (err) {
|
| 38 | dev_err(decoder->dev, "Error reading GPIO: %d\n", err);
|
| 39 | return err;
|
| 40 | }
|
| 41 |
|
| 42 | return bitmap_read(values, 0, size);
|
| 43 | }
|
| 44 |
|
| 45 | static void gpio_decoder_poll_gpios(struct input_dev *input)
|
| 46 | {
|
| 47 | struct gpio_decoder *decoder = input_get_drvdata(input);
|
| 48 | int state;
|
| 49 |
|
| 50 | state = gpio_decoder_get_gpios_state(decoder);
|
| 51 | if (state >= 0 && state != decoder->last_stable) {
|
| 52 | input_report_abs(input, decoder->axis, state);
|
| 53 | input_sync(input);
|
| 54 | decoder->last_stable = state;
|
| 55 | }
|
| 56 | }
|
| 57 |
|
| 58 | static int gpio_decoder_probe(struct platform_device *pdev)
|
| 59 | {
|
| 60 | struct device *dev = &pdev->dev;
|
| 61 | struct gpio_decoder *decoder;
|
| 62 | struct input_dev *input;
|
| 63 | u32 max;
|
| 64 | int err;
|
| 65 |
|
| 66 | decoder = devm_kzalloc(dev, sizeof(*decoder), GFP_KERNEL);
|
| 67 | if (!decoder)
|
| 68 | return -ENOMEM;
|
| 69 |
|
| 70 | decoder->dev = dev;
|
| 71 | device_property_read_u32(dev, "linux,axis", &decoder->axis);
|
| 72 |
|
| 73 | decoder->input_gpios = devm_gpiod_get_array(dev, NULL, GPIOD_IN);
|
| 74 | if (IS_ERR(decoder->input_gpios))
|
| 75 | return dev_err_probe(dev, PTR_ERR(decoder->input_gpios),
|
| 76 | "unable to acquire input gpios\n");
|
| 77 |
|
| 78 | if (decoder->input_gpios->ndescs < 2)
|
| 79 | return dev_err_probe(dev, -EINVAL, "not enough gpios found\n");
|
| 80 |
|
| 81 | if (decoder->input_gpios->ndescs > 31)
|
| 82 | return dev_err_probe(dev, -EINVAL, "too many gpios found\n");
|
| 83 |
|
| 84 | if (device_property_read_u32(dev, "decoder-max-value", &max))
|
| 85 | max = BIT(decoder->input_gpios->ndescs) - 1;
|
| 86 |
|
| 87 | input = devm_input_allocate_device(dev);
|
| 88 | if (!input)
|
| 89 | return -ENOMEM;
|
| 90 |
|
| 91 | input_set_drvdata(input, decoder);
|
| 92 |
|
| 93 | input->name = pdev->name;
|
| 94 | input->id.bustype = BUS_HOST;
|
| 95 | input_set_abs_params(input, decoder->axis, 0, max, 0, 0);
|
| 96 |
|
| 97 | err = input_setup_polling(input, gpio_decoder_poll_gpios);
|
| 98 | if (err)
|
| 99 | return dev_err_probe(dev, err, "failed to set up polling\n");
|
| 100 |
|
| 101 | err = input_register_device(input);
|
| 102 | if (err)
|
| 103 | return dev_err_probe(dev, err, "failed to register input device\n");
|
| 104 |
|
| 105 | return 0;
|
| 106 | }
|
| 107 |
|
| 108 | static const struct of_device_id gpio_decoder_of_match[] = {
|
| 109 | { .compatible = "gpio-decoder", },
|
| 110 | { }
|
| 111 | };
|
| 112 | MODULE_DEVICE_TABLE(of, gpio_decoder_of_match);
|
| 113 |
|
| 114 | static struct platform_driver gpio_decoder_driver = {
|
| 115 | .probe = gpio_decoder_probe,
|
| 116 | .driver = {
|
| 117 | .name = "gpio-decoder",
|
| 118 | .of_match_table = gpio_decoder_of_match,
|
| 119 | }
|
| 120 | };
|
| 121 | module_platform_driver(gpio_decoder_driver);
|
| 122 |
|
| 123 | MODULE_DESCRIPTION("GPIO decoder input driver");
|
| 124 | MODULE_AUTHOR("Vignesh R <[email protected]>");
|
| 125 | MODULE_LICENSE("GPL v2");
|