Add support to spi-hid to be a panel follower.

Signed-off-by: Jingyuan Liang <[email protected]>
---
 drivers/hid/spi-hid/spi-hid-core.c | 164 ++++++++++++++++++++++++++++++-------
 drivers/hid/spi-hid/spi-hid-core.h |   7 ++
 2 files changed, 142 insertions(+), 29 deletions(-)

diff --git a/drivers/hid/spi-hid/spi-hid-core.c 
b/drivers/hid/spi-hid/spi-hid-core.c
index 517f06913477..27f25d95ed28 100644
--- a/drivers/hid/spi-hid/spi-hid-core.c
+++ b/drivers/hid/spi-hid/spi-hid-core.c
@@ -1281,6 +1281,106 @@ const struct attribute_group *spi_hid_groups[] = {
 };
 EXPORT_SYMBOL_GPL(spi_hid_groups);
 
+/*
+ * At the end of probe we initialize the device:
+ *   0) assert reset, bias the interrupt line
+ *   1) sleep minimal reset delay
+ *   2) power up the device
+ *   3) deassert reset (high)
+ * After this we expect an IRQ with a reset response.
+ */
+static int spi_hid_dev_init(struct spi_hid *shid)
+{
+       struct spi_device *spi = shid->spi;
+       struct device *dev = &spi->dev;
+       int error;
+
+       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\n", __func__);
+               shid->regulator_error_count++;
+               shid->regulator_last_error = error;
+               return error;
+       }
+
+       shid->ops->deassert_reset(shid->ops);
+
+       enable_irq(spi->irq);
+
+       return 0;
+}
+
+static void spi_hid_panel_follower_work(struct work_struct *work)
+{
+       struct spi_hid *shid = container_of(work, struct spi_hid,
+                                           panel_follower_work);
+       int error;
+
+       if (!shid->desc.hid_version)
+               error = spi_hid_dev_init(shid);
+       else
+               error = spi_hid_resume(shid);
+       if (error)
+               dev_warn(&shid->spi->dev, "Power on failed: %d\n", error);
+       else
+               WRITE_ONCE(shid->panel_follower_work_finished, true);
+}
+
+static int spi_hid_panel_follower_resume(struct drm_panel_follower *follower)
+{
+       struct spi_hid *shid = container_of(follower, struct spi_hid, 
panel_follower);
+
+       /*
+        * Powering on a touchscreen can be a slow process. Queue the work to
+        * the system workqueue so we don't block the panel's power up.
+        */
+       WRITE_ONCE(shid->panel_follower_work_finished, false);
+       schedule_work(&shid->panel_follower_work);
+
+       return 0;
+}
+
+static int spi_hid_panel_follower_suspend(struct drm_panel_follower *follower)
+{
+       struct spi_hid *shid = container_of(follower, struct spi_hid, 
panel_follower);
+
+       cancel_work_sync(&shid->panel_follower_work);
+
+       if (!READ_ONCE(shid->panel_follower_work_finished))
+               return 0;
+
+       return spi_hid_suspend(shid);
+}
+
+static const struct drm_panel_follower_funcs
+                               spi_hid_panel_follower_prepare_funcs = {
+       .panel_prepared = spi_hid_panel_follower_resume,
+       .panel_unpreparing = spi_hid_panel_follower_suspend,
+};
+
+static int spi_hid_register_panel_follower(struct spi_hid *shid)
+{
+       struct device *dev = &shid->spi->dev;
+
+       shid->panel_follower.funcs = &spi_hid_panel_follower_prepare_funcs;
+
+       /*
+        * If we're not in control of our own power up/power down then we can't
+        * do the logic to manage wakeups. Give a warning if a user thought
+        * that was possible then force the capability off.
+        */
+       if (device_can_wakeup(dev)) {
+               dev_warn(dev, "Can't wakeup if following panel\n");
+               device_set_wakeup_capable(dev, false);
+       }
+
+       return drm_panel_add_follower(dev, &shid->panel_follower);
+}
+
 int spi_hid_core_probe(struct spi_device *spi, struct spihid_ops *ops,
                       struct spi_hid_conf *conf)
 {
@@ -1300,6 +1400,7 @@ int spi_hid_core_probe(struct spi_device *spi, struct 
spihid_ops *ops,
        shid->ops = ops;
        shid->conf = conf;
        set_bit(SPI_HID_RESET_PENDING, &shid->flags);
+       shid->is_panel_follower = drm_is_panel_follower(&spi->dev);
 
        spi_set_drvdata(spi, shid);
 
@@ -1313,6 +1414,7 @@ int spi_hid_core_probe(struct spi_device *spi, struct 
spihid_ops *ops,
        init_completion(&shid->output_done);
 
        INIT_WORK(&shid->reset_work, spi_hid_reset_work);
+       INIT_WORK(&shid->panel_follower_work, spi_hid_panel_follower_work);
 
        /*
         * We need to allocate the buffer without knowing the maximum
@@ -1323,20 +1425,6 @@ int spi_hid_core_probe(struct spi_device *spi, struct 
spihid_ops *ops,
        if (error)
                return error;
 
-       /*
-        * At the end of probe we initialize the device:
-        *   0) assert reset, bias the interrupt line
-        *   1) sleep minimal reset delay
-        *   2) request IRQ
-        *   3) power up the device
-        *   4) deassert reset (high)
-        * After this we expect an IRQ with a reset response.
-        */
-
-       shid->ops->assert_reset(shid->ops);
-
-       shid->ops->sleep_minimal_reset_delay(shid->ops);
-
        error = devm_request_threaded_irq(dev, spi->irq, NULL, spi_hid_dev_irq,
                                          IRQF_ONESHOT | IRQF_NO_AUTOEN, 
dev_name(&spi->dev), shid);
        if (error) {
@@ -1351,22 +1439,28 @@ int spi_hid_core_probe(struct spi_device *spi, struct 
spihid_ops *ops,
                }
        }
 
-       error = shid->ops->power_up(shid->ops);
-       if (error) {
-               dev_err(dev, "%s: could not power up\n", __func__);
-               if (device_may_wakeup(dev))
-                       dev_pm_clear_wake_irq(dev);
-               return error;
+       if (shid->is_panel_follower) {
+               error = spi_hid_register_panel_follower(shid);
+               if (error) {
+                       dev_err_probe(dev, error,
+                                     "Failed to register panel follower");
+                       goto err_wake_irq;
+               }
+       } else {
+               error = spi_hid_dev_init(shid);
+               if (error)
+                       goto err_wake_irq;
        }
 
-       shid->ops->deassert_reset(shid->ops);
-
-       enable_irq(spi->irq);
-
        dev_dbg(dev, "%s: d3 -> %s\n", __func__,
                spi_hid_power_mode_string(shid->power_state));
 
        return 0;
+
+err_wake_irq:
+       if (device_may_wakeup(dev))
+               dev_pm_clear_wake_irq(dev);
+       return error;
 }
 EXPORT_SYMBOL_GPL(spi_hid_core_probe);
 
@@ -1376,15 +1470,21 @@ void spi_hid_core_remove(struct spi_device *spi)
        struct device *dev = &spi->dev;
        int error;
 
-       disable_irq(spi->irq);
+       if (shid->is_panel_follower)
+               drm_panel_remove_follower(&shid->panel_follower);
+       else
+               disable_irq(spi->irq);
+
        cancel_work_sync(&shid->reset_work);
 
        spi_hid_stop_hid(shid);
 
-       shid->ops->assert_reset(shid->ops);
-       error = shid->ops->power_down(shid->ops);
-       if (error)
-               dev_err(dev, "failed to disable regulator\n");
+       if (shid->power_state != HIDSPI_OFF) {
+               shid->ops->assert_reset(shid->ops);
+               error = shid->ops->power_down(shid->ops);
+               if (error)
+                       dev_err(dev, "failed to disable regulator\n");
+       }
 
        if (device_may_wakeup(dev))
                dev_pm_clear_wake_irq(dev);
@@ -1395,6 +1495,9 @@ static int spi_hid_core_pm_suspend(struct device *dev)
 {
        struct spi_hid *shid = dev_get_drvdata(dev);
 
+       if (shid->is_panel_follower)
+               return 0;
+
        return spi_hid_suspend(shid);
 }
 
@@ -1402,6 +1505,9 @@ static int spi_hid_core_pm_resume(struct device *dev)
 {
        struct spi_hid *shid = dev_get_drvdata(dev);
 
+       if (shid->is_panel_follower)
+               return 0;
+
        return spi_hid_resume(shid);
 }
 
diff --git a/drivers/hid/spi-hid/spi-hid-core.h 
b/drivers/hid/spi-hid/spi-hid-core.h
index 293e2cfcfbf7..261b2fd7f332 100644
--- a/drivers/hid/spi-hid/spi-hid-core.h
+++ b/drivers/hid/spi-hid/spi-hid-core.h
@@ -10,6 +10,8 @@
 #include <linux/hid-over-spi.h>
 #include <linux/spi/spi.h>
 
+#include <drm/drm_panel.h>
+
 /* Protocol message size constants */
 #define SPI_HID_READ_APPROVAL_LEN              5
 #define SPI_HID_OUTPUT_HEADER_LEN              8
@@ -56,6 +58,10 @@ struct spi_hid {
        struct spi_hid_input_buf *input;        /* Input buffer. */
        struct spi_hid_input_buf *response;     /* Response buffer. */
 
+       struct drm_panel_follower panel_follower;
+       bool    is_panel_follower;
+       bool    panel_follower_work_finished;
+
        u16 response_length;
        u16 bufsize;
 
@@ -66,6 +72,7 @@ struct spi_hid {
        unsigned long flags;    /* device flags. */
 
        struct work_struct reset_work;
+       struct work_struct panel_follower_work;
 
        /* Control lock to ensure complete output transaction. */
        struct mutex output_lock;

-- 
2.54.0.1064.gd145956f57-goog


Reply via email to