From 4507ce6f21a09a0a46d1967b14eddb25f727e020 Mon Sep 17 00:00:00 2001
From: Zorro Liu <lyx@rock-chips.com>
Date: Wed, 28 Apr 2021 20:55:11 +0800
Subject: [PATCH] drivers: input: sensor: hall sensor mh248: support ebook
 ultra sleep

Signed-off-by: Zorro Liu <lyx@rock-chips.com>
Change-Id: I59aca72daffeaa0ef184269d60a8bc8c1440b2d5
---
 drivers/input/sensors/hall/mh248.c | 145 ++++++++++++++++++++++++++++-
 1 file changed, 142 insertions(+), 3 deletions(-)
 mode change 100644 => 100755 drivers/input/sensors/hall/mh248.c

diff --git a/drivers/input/sensors/hall/mh248.c b/drivers/input/sensors/hall/mh248.c
old mode 100644
new mode 100755
index 63aabd6644c6..bb390570dd69
--- a/drivers/input/sensors/hall/mh248.c
+++ b/drivers/input/sensors/hall/mh248.c
@@ -33,17 +33,62 @@
 #include <linux/fb.h>
 #include <linux/notifier.h>
 #include <linux/rk_keys.h>
+#include <linux/suspend.h>
+#include <linux/syscore_ops.h>
 
+#ifdef CONFIG_ROCKCHIP_EBC_DEV
+#include "../../../gpu/drm/rockchip/ebc-dev/ebc_dev.h"
+#endif
+
+#define SUPPORT_ULTRA_SLEEP
 struct mh248_para {
 	struct device *dev;
 	struct notifier_block fb_notif;
+	struct notifier_block ebc_notif;
 	struct mutex ops_lock;
+	struct input_dev *hall_input;
 	int is_suspend;
 	int gpio_pin;
 	int irq;
 	int active_value;
+#ifdef SUPPORT_ULTRA_SLEEP
+	void __iomem	*pmu_gpio_regs;
+	int is_pmu_wakeup;
+#endif
 };
 
+#ifdef SUPPORT_ULTRA_SLEEP
+#define PMU_GPIO_REG_BASE 0xFDD60000 //GPIO0_A3, PMIC INT PIN
+#define PMU_GPIO_REG_SIZE 0x100
+#define PMU_GPIO_INT_STATUS_REG 0x50
+#define PMU_GPIO_INT_MASK 0x08
+#endif
+
+struct mh248_para *g_mh248 = NULL;
+
+static int hall_ebc_notifier_callback(struct notifier_block *self,
+				     unsigned long action, void *data)
+{
+	struct mh248_para *mh248;
+
+	mh248 = container_of(self, struct mh248_para, ebc_notif);
+
+	mutex_lock(&mh248->ops_lock);
+
+	if (action == EBC_FB_BLANK) {
+		printk("%s: ebc fb blank\n", __func__);
+		mh248->is_suspend = 1;
+	}
+	else if (action == EBC_FB_UNBLANK) {
+		printk("%s: ebc fb unblank\n", __func__);
+		mh248->is_suspend = 0;
+	}
+
+	mutex_unlock(&mh248->ops_lock);
+
+	return NOTIFY_OK;
+}
+
 static int hall_fb_notifier_callback(struct notifier_block *self,
 				     unsigned long action, void *data)
 {
@@ -84,16 +129,45 @@ static irqreturn_t hall_mh248_interrupt(int irq, void *dev_id)
 
 	if ((gpio_value != mh248->active_value) &&
 	    (mh248->is_suspend == 0)) {
-		rk_send_power_key(1);
-		rk_send_power_key(0);
+		input_report_key(g_mh248->hall_input, KEY_POWER, 1);
+		input_sync(g_mh248->hall_input);
+		input_report_key(g_mh248->hall_input, KEY_POWER, 0);
+		input_sync(g_mh248->hall_input);
 	} else if ((gpio_value == mh248->active_value) &&
 		   (mh248->is_suspend == 1)) {
-		rk_send_wakeup_key();
+		input_report_key(g_mh248->hall_input, KEY_WAKEUP, 1);
+		input_sync(g_mh248->hall_input);
+		input_report_key(g_mh248->hall_input, KEY_WAKEUP, 0);
+		input_sync(g_mh248->hall_input);
 	}
 
 	return IRQ_HANDLED;
 }
 
+#ifdef SUPPORT_ULTRA_SLEEP
+static int mh248_syscore_suspend(void)
+{
+	return 0;
+}
+
+static void mh248_syscore_resume(void)
+{
+	u32 intr_status;
+
+	intr_status = readl_relaxed(g_mh248->pmu_gpio_regs + PMU_GPIO_INT_STATUS_REG);
+	if (intr_status & PMU_GPIO_INT_MASK)
+		g_mh248->is_pmu_wakeup = 1;
+	else
+		g_mh248->is_pmu_wakeup = 0;
+	printk("%s: GPIO0 INT status =  %x\n", __func__, intr_status);
+}
+
+static struct syscore_ops mh248_syscore_ops = {
+	.suspend = mh248_syscore_suspend,
+	.resume = mh248_syscore_resume,
+};
+#endif
+
 static int hall_mh248_probe(struct platform_device *pdev)
 {
 	struct device_node *np = pdev->dev.of_node;
@@ -141,14 +215,76 @@ static int hall_mh248_probe(struct platform_device *pdev)
 		return ret;
 	}
 
+	mh248->hall_input = devm_input_allocate_device(&pdev->dev);
+	if (!mh248->hall_input) {
+		dev_err(&pdev->dev, "Can't allocate hall input dev\n");
+		return -ENOMEM;
+	}
+	mh248->hall_input->name = "hall wake key";
+	input_set_capability(mh248->hall_input, EV_KEY, KEY_POWER);
+	input_set_capability(mh248->hall_input, EV_KEY, KEY_WAKEUP);
+
+	ret = input_register_device(mh248->hall_input);
+	if (ret) {
+		dev_err(&pdev->dev, "Unable to register input device, error: %d\n", ret);
+		return ret;
+	}
+
 	enable_irq_wake(mh248->irq);
 	mh248->fb_notif.notifier_call = hall_fb_notifier_callback;
 	fb_register_client(&mh248->fb_notif);
+
+	mh248->ebc_notif.notifier_call = hall_ebc_notifier_callback;
+	ebc_register_notifier(&mh248->ebc_notif);
+
+#ifdef SUPPORT_ULTRA_SLEEP
+	mh248->pmu_gpio_regs = ioremap(PMU_GPIO_REG_BASE, PMU_GPIO_REG_SIZE);
+	register_syscore_ops(&mh248_syscore_ops);
+#endif
+
+	g_mh248 = mh248;
+
 	dev_info(mh248->dev, "hall_mh248_probe success.\n");
 
 	return 0;
 }
 
+#ifdef SUPPORT_ULTRA_SLEEP
+static int mh248_suspend(struct device *dev)
+{
+	if (mem_sleep_current == PM_SUSPEND_MEM_ULTRA)
+		disable_irq(g_mh248->irq);
+
+	return 0;
+}
+
+static int mh248_resume(struct device *dev)
+{
+	int gpio_value = 0;
+
+	if (mem_sleep_current == PM_SUSPEND_MEM_ULTRA) {
+		if (!g_mh248->is_pmu_wakeup) {
+			gpio_value = gpio_get_value(g_mh248->gpio_pin);
+			if ((gpio_value == g_mh248->active_value) &&
+				   (g_mh248->is_suspend == 1)) {
+				input_report_key(g_mh248->hall_input, KEY_WAKEUP, 1);
+				input_sync(g_mh248->hall_input);
+				input_report_key(g_mh248->hall_input, KEY_WAKEUP, 0);
+				input_sync(g_mh248->hall_input);
+				printk("%s: send wakeup key\n", __func__);
+			}
+		}
+		enable_irq(g_mh248->irq);
+	}
+	return 0;
+}
+
+static const struct dev_pm_ops mh248_pm = {
+	.resume = mh248_resume,
+	.suspend = mh248_suspend,
+};
+#endif
+
 static const struct of_device_id hall_mh248_match[] = {
 	{ .compatible = "hall-mh248" },
 	{ /* Sentinel */ }
@@ -160,6 +296,9 @@ static struct platform_driver hall_mh248_driver = {
 		.name = "mh248",
 		.owner = THIS_MODULE,
 		.of_match_table	= hall_mh248_match,
+#ifdef SUPPORT_ULTRA_SLEEP
+		.pm = &mh248_pm,
+#endif
 	},
 };
 
-- 
2.17.1

