From: Jarrett Schultz <[email protected]>

Detect SPI HID devices described in Device Tree.

Signed-off-by: Dmitry Antipov <[email protected]>
Signed-off-by: Jingyuan Liang <[email protected]>
---
 drivers/hid/spi-hid/Kconfig      |  15 +++
 drivers/hid/spi-hid/Makefile     |   1 +
 drivers/hid/spi-hid/spi-hid-of.c | 210 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 226 insertions(+)

diff --git a/drivers/hid/spi-hid/Kconfig b/drivers/hid/spi-hid/Kconfig
index 303ada03a1bf..63f202975ac3 100644
--- a/drivers/hid/spi-hid/Kconfig
+++ b/drivers/hid/spi-hid/Kconfig
@@ -26,6 +26,21 @@ config SPI_HID_ACPI
          will be called spi-hid-acpi. It will also build/depend on the
          module spi-hid.
 
+config SPI_HID_OF
+       tristate "HID over SPI transport layer Open Firmware driver"
+       depends on OF
+       select SPI_HID_CORE
+       help
+         Say Y here if you use a keyboard, a touchpad, a touchscreen, or any
+         other HID based devices which are connected to your computer via SPI.
+         This driver supports Open Firmware (Device Tree)-based systems.
+
+         If unsure, say N.
+
+         This support is also available as a module.  If so, the module
+         will be called spi-hid-of. It will also build/depend on the
+         module spi-hid.
+
 config SPI_HID_CORE
        tristate
 endif
diff --git a/drivers/hid/spi-hid/Makefile b/drivers/hid/spi-hid/Makefile
index 3ca326602643..31192e71edae 100644
--- a/drivers/hid/spi-hid/Makefile
+++ b/drivers/hid/spi-hid/Makefile
@@ -9,3 +9,4 @@ obj-$(CONFIG_SPI_HID_CORE)      += spi-hid.o
 spi-hid-objs                   = spi-hid-core.o
 CFLAGS_spi-hid-core.o          := -I$(src)
 obj-$(CONFIG_SPI_HID_ACPI)     += spi-hid-acpi.o
+obj-$(CONFIG_SPI_HID_OF)       += spi-hid-of.o
diff --git a/drivers/hid/spi-hid/spi-hid-of.c b/drivers/hid/spi-hid/spi-hid-of.c
new file mode 100644
index 000000000000..d1bbdc17102e
--- /dev/null
+++ b/drivers/hid/spi-hid/spi-hid-of.c
@@ -0,0 +1,210 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * HID over SPI protocol, Open Firmware related code
+ *
+ * Copyright (c) 2021 Microsoft Corporation
+ *
+ * This code was forked out of the HID over SPI core code, which is partially
+ * based on "HID over I2C protocol implementation:
+ *
+ * Copyright (c) 2012 Benjamin Tissoires <[email protected]>
+ * Copyright (c) 2012 Ecole Nationale de l'Aviation Civile, France
+ * Copyright (c) 2012 Red Hat, Inc
+ *
+ * which in turn is partially based on "USB HID support for Linux":
+ *
+ * Copyright (c) 1999 Andreas Gal
+ * Copyright (c) 2000-2005 Vojtech Pavlik <[email protected]>
+ * Copyright (c) 2005 Michael Haboustak <[email protected]> for Concept2, Inc
+ * Copyright (c) 2007-2008 Oliver Neukum
+ * Copyright (c) 2006-2010 Jiri Kosina
+ */
+
+#include <linux/container_of.h>
+#include <linux/delay.h>
+#include <linux/device.h>
+#include <linux/err.h>
+#include <linux/gpio/consumer.h>
+#include <linux/module.h>
+#include <linux/regulator/consumer.h>
+#include <linux/spi/spi.h>
+#include <linux/types.h>
+
+#include "spi-hid.h"
+
+/* Per-compatible data: protocol parameters and timings */
+struct spi_hid_of_data {
+       struct spi_hid_conf conf;
+       u32 post_power_on_delay_ms;
+       u32 minimal_reset_delay_ms;
+};
+
+/* Config structure is filled with match data and Device Tree resources */
+struct spi_hid_of_config {
+       struct spihid_ops ops;
+
+       struct spi_hid_of_data data;
+
+       struct gpio_desc *reset_gpio;
+       struct regulator *supply;
+       bool supply_enabled;
+};
+
+static const struct spi_hid_of_data microsoft_g6_data = {
+       .conf = {
+               .input_report_header_address = 0x1000,
+               .input_report_body_address = 0x1004,
+               .output_report_address = 0x2000,
+               .read_opcode = 0x0b,
+               .write_opcode = 0x02,
+       },
+       .post_power_on_delay_ms = 10,
+       .minimal_reset_delay_ms = 100,
+};
+
+static int spi_hid_of_populate_config(struct spi_hid_of_config *conf,
+                                     struct device *dev)
+{
+       conf->supply = devm_regulator_get(dev, "vdd");
+       if (IS_ERR(conf->supply))
+               return dev_err_probe(dev, PTR_ERR(conf->supply),
+                                    "Failed to get regulator\n");
+       conf->supply_enabled = false;
+
+       conf->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
+       if (IS_ERR(conf->reset_gpio))
+               return dev_err_probe(dev, PTR_ERR(conf->reset_gpio),
+                                    "Failed to get reset GPIO\n");
+
+       return 0;
+}
+
+static int spi_hid_of_power_down(struct spihid_ops *ops)
+{
+       struct spi_hid_of_config *conf = container_of(ops,
+                                                     struct spi_hid_of_config,
+                                                     ops);
+       int error;
+
+       if (!conf->supply_enabled)
+               return 0;
+
+       error = regulator_disable(conf->supply);
+       if (error == 0)
+               conf->supply_enabled = false;
+
+       return error;
+}
+
+static int spi_hid_of_power_up(struct spihid_ops *ops)
+{
+       struct spi_hid_of_config *conf = container_of(ops,
+                                                     struct spi_hid_of_config,
+                                                     ops);
+       int error;
+
+       if (conf->supply_enabled)
+               return 0;
+
+       error = regulator_enable(conf->supply);
+
+       if (error == 0) {
+               conf->supply_enabled = true;
+               fsleep(1000 * conf->data.post_power_on_delay_ms);
+       }
+
+       return error;
+}
+
+static int spi_hid_of_assert_reset(struct spihid_ops *ops)
+{
+       struct spi_hid_of_config *conf = container_of(ops,
+                                                     struct spi_hid_of_config,
+                                                     ops);
+
+       return gpiod_set_value_cansleep(conf->reset_gpio, 1);
+}
+
+static int spi_hid_of_deassert_reset(struct spihid_ops *ops)
+{
+       struct spi_hid_of_config *conf = container_of(ops,
+                                                     struct spi_hid_of_config,
+                                                     ops);
+
+       return gpiod_set_value_cansleep(conf->reset_gpio, 0);
+}
+
+static void spi_hid_of_sleep_minimal_reset_delay(struct spihid_ops *ops)
+{
+       struct spi_hid_of_config *conf = container_of(ops,
+                                                     struct spi_hid_of_config,
+                                                     ops);
+       fsleep(1000 * conf->data.minimal_reset_delay_ms);
+}
+
+static int spi_hid_of_probe(struct spi_device *spi)
+{
+       struct device *dev = &spi->dev;
+       const struct spi_hid_of_data *data;
+       struct spi_hid_of_config *config;
+       int error;
+
+       config = devm_kzalloc(dev, sizeof(struct spi_hid_of_config),
+                             GFP_KERNEL);
+       if (!config)
+               return -ENOMEM;
+
+       config->ops.power_up = spi_hid_of_power_up;
+       config->ops.power_down = spi_hid_of_power_down;
+       config->ops.assert_reset = spi_hid_of_assert_reset;
+       config->ops.deassert_reset = spi_hid_of_deassert_reset;
+       config->ops.sleep_minimal_reset_delay =
+               spi_hid_of_sleep_minimal_reset_delay;
+
+       data = spi_get_device_match_data(spi);
+       if (!data)
+               return dev_err_probe(dev, -ENODEV, "No match data\n");
+
+       config->data = *data;
+
+       /*
+        * FIXME: multi-SPI not supported. Once it is, derive the
+        * HID-over-SPI flags from spi->mode.
+        */
+
+       error = spi_hid_of_populate_config(config, dev);
+       if (error)
+               return dev_err_probe(dev, error, "Unable to populate config 
data\n");
+
+       return spi_hid_core_probe(spi, &config->ops, &config->data.conf);
+}
+
+static const struct of_device_id spi_hid_of_match[] = {
+       { .compatible = "microsoft,g6-touch-digitizer", .data = 
&microsoft_g6_data },
+       { }
+};
+MODULE_DEVICE_TABLE(of, spi_hid_of_match);
+
+static const struct spi_device_id spi_hid_of_id_table[] = {
+       { .name = "g6-touch-digitizer", .driver_data = 
(kernel_ulong_t)&microsoft_g6_data },
+       { }
+};
+MODULE_DEVICE_TABLE(spi, spi_hid_of_id_table);
+
+static struct spi_driver spi_hid_of_driver = {
+       .driver = {
+               .name = "spi_hid_of",
+               .owner = THIS_MODULE,
+               .of_match_table = spi_hid_of_match,
+               .probe_type = PROBE_PREFER_ASYNCHRONOUS,
+       },
+       .probe = spi_hid_of_probe,
+       .remove = spi_hid_core_remove,
+       .id_table = spi_hid_of_id_table,
+};
+
+module_spi_driver(spi_hid_of_driver);
+
+MODULE_DESCRIPTION("HID over SPI OF transport driver");
+MODULE_AUTHOR("Dmitry Antipov <[email protected]>");
+MODULE_LICENSE("GPL");

-- 
2.56.0.257.gf7497c7d27-goog


Reply via email to