Implement HID over SPI driver power management callbacks.

Signed-off-by: Jingyuan Liang <[email protected]>
---
 drivers/hid/spi-hid/spi-hid-acpi.c |   1 +
 drivers/hid/spi-hid/spi-hid-core.c | 107 +++++++++++++++++++++++++++++++++++++
 drivers/hid/spi-hid/spi-hid-of.c   |   1 +
 drivers/hid/spi-hid/spi-hid.h      |   1 +
 4 files changed, 110 insertions(+)

diff --git a/drivers/hid/spi-hid/spi-hid-acpi.c 
b/drivers/hid/spi-hid/spi-hid-acpi.c
index 612e74fe72f9..2c1e4de99fea 100644
--- a/drivers/hid/spi-hid/spi-hid-acpi.c
+++ b/drivers/hid/spi-hid/spi-hid-acpi.c
@@ -238,6 +238,7 @@ static struct spi_driver spi_hid_acpi_driver = {
        .driver = {
                .name   = "spi_hid_acpi",
                .owner  = THIS_MODULE,
+               .pm     = &spi_hid_core_pm,
                .acpi_match_table = ACPI_PTR(spi_hid_acpi_match),
                .probe_type = PROBE_PREFER_ASYNCHRONOUS,
                .dev_groups = spi_hid_groups,
diff --git a/drivers/hid/spi-hid/spi-hid-core.c 
b/drivers/hid/spi-hid/spi-hid-core.c
index 02beb209a92d..797ba99394f9 100644
--- a/drivers/hid/spi-hid/spi-hid-core.c
+++ b/drivers/hid/spi-hid/spi-hid-core.c
@@ -35,6 +35,8 @@
 #include <linux/list.h>
 #include <linux/module.h>
 #include <linux/mutex.h>
+#include <linux/pm.h>
+#include <linux/pm_wakeirq.h>
 #include <linux/slab.h>
 #include <linux/spi/spi.h>
 #include <linux/string.h>
@@ -236,6 +238,81 @@ static const char *spi_hid_power_mode_string(enum 
hidspi_power_state power_state
        }
 }
 
+static void spi_hid_suspend(struct spi_hid *shid)
+{
+       int error;
+       struct device *dev = &shid->spi->dev;
+
+       guard(mutex)(&shid->power_lock);
+       if (shid->power_state == HIDSPI_OFF)
+               return;
+
+       if (shid->hid) {
+               error = hid_driver_suspend(shid->hid, PMSG_SUSPEND);
+               if (error) {
+                       dev_err(dev, "%s failed to suspend hid driver: %d",
+                               __func__, error);
+                       return;
+               }
+       }
+
+       disable_irq(shid->spi->irq);
+
+       clear_bit(SPI_HID_READY, &shid->flags);
+
+       if (!device_may_wakeup(dev)) {
+               set_bit(SPI_HID_RESET_PENDING, &shid->flags);
+
+               shid->ops->assert_reset(shid->ops);
+
+               error = shid->ops->power_down(shid->ops);
+               if (error) {
+                       dev_err(dev, "%s: could not power down.", __func__);
+                       shid->regulator_error_count++;
+                       shid->regulator_last_error = error;
+                       return;
+               }
+
+               shid->power_state = HIDSPI_OFF;
+       }
+}
+
+static void spi_hid_resume(struct spi_hid *shid)
+{
+       int error;
+       struct device *dev = &shid->spi->dev;
+
+       guard(mutex)(&shid->power_lock);
+       if (shid->power_state == HIDSPI_ON)
+               return;
+
+       enable_irq(shid->spi->irq);
+
+       if (!device_may_wakeup(dev)) {
+               shid->ops->assert_reset(shid->ops);
+
+               shid->ops->sleep_minimal_reset_delay(shid->ops);
+
+               error = shid->ops->power_up(shid->ops);
+               if (error) {
+                       dev_err(dev, "%s: could not power up.", __func__);
+                       shid->regulator_error_count++;
+                       shid->regulator_last_error = error;
+                       return;
+               }
+               shid->power_state = HIDSPI_ON;
+
+               shid->ops->deassert_reset(shid->ops);
+       }
+
+       if (shid->hid) {
+               error = hid_driver_reset_resume(shid->hid);
+               if (error)
+                       dev_err(dev, "%s: failed to reset resume hid driver: 
%d.",
+                               __func__, error);
+       }
+}
+
 static void spi_hid_stop_hid(struct spi_hid *shid)
 {
        struct hid_device *hid = shid->hid;
@@ -1155,6 +1232,13 @@ int spi_hid_core_probe(struct spi_device *spi, struct 
spihid_ops *ops,
                dev_err(dev, "%s: unable to request threaded IRQ.", __func__);
                return error;
        }
+       if (device_may_wakeup(dev)) {
+               error = dev_pm_set_wake_irq(dev, spi->irq);
+               if (error) {
+                       dev_err(dev, "%s: failed to set wake IRQ.", __func__);
+                       return error;
+               }
+       }
 
        error = shid->ops->power_up(shid->ops);
        if (error) {
@@ -1186,6 +1270,29 @@ void spi_hid_core_remove(struct spi_device *spi)
 }
 EXPORT_SYMBOL_GPL(spi_hid_core_remove);
 
+static int spi_hid_core_pm_suspend(struct device *dev)
+{
+       struct spi_hid *shid = dev_get_drvdata(dev);
+
+       spi_hid_suspend(shid);
+
+       return 0;
+}
+
+static int spi_hid_core_pm_resume(struct device *dev)
+{
+       struct spi_hid *shid = dev_get_drvdata(dev);
+
+       spi_hid_resume(shid);
+
+       return 0;
+}
+
+const struct dev_pm_ops spi_hid_core_pm = {
+       SYSTEM_SLEEP_PM_OPS(spi_hid_core_pm_suspend, spi_hid_core_pm_resume)
+};
+EXPORT_SYMBOL_GPL(spi_hid_core_pm);
+
 MODULE_DESCRIPTION("HID over SPI transport driver");
 MODULE_AUTHOR("Dmitry Antipov <[email protected]>");
 MODULE_LICENSE("GPL");
diff --git a/drivers/hid/spi-hid/spi-hid-of.c b/drivers/hid/spi-hid/spi-hid-of.c
index a20c8146230b..bc1d3c5a4dda 100644
--- a/drivers/hid/spi-hid/spi-hid-of.c
+++ b/drivers/hid/spi-hid/spi-hid-of.c
@@ -227,6 +227,7 @@ static struct spi_driver spi_hid_of_driver = {
        .driver = {
                .name   = "spi_hid_of",
                .owner  = THIS_MODULE,
+               .pm     = &spi_hid_core_pm,
                .of_match_table = of_match_ptr(spi_hid_of_match),
                .probe_type = PROBE_PREFER_ASYNCHRONOUS,
                .dev_groups = spi_hid_groups,
diff --git a/drivers/hid/spi-hid/spi-hid.h b/drivers/hid/spi-hid/spi-hid.h
index 1fdd45262647..5651c7fb706a 100644
--- a/drivers/hid/spi-hid/spi-hid.h
+++ b/drivers/hid/spi-hid/spi-hid.h
@@ -40,5 +40,6 @@ int spi_hid_core_probe(struct spi_device *spi, struct 
spihid_ops *ops,
 void spi_hid_core_remove(struct spi_device *spi);
 
 extern const struct attribute_group *spi_hid_groups[];
+extern const struct dev_pm_ops spi_hid_core_pm;
 
 #endif /* SPI_HID_H */

-- 
2.53.0.473.g4a7958ca14-goog


Reply via email to