cachepc-linux

Fork of AMDESE/linux with modifications for CachePC side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-linux
Log | Files | Refs | README | LICENSE | sfeed.txt

gpio_decoder.c (3541B)


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