From e3e12f52b214121840b44c91de5b3e5af5d3eb84 Mon Sep 17 00:00:00 2001
From: hc <hc@nodka.com>
Date: Mon, 06 Nov 2023 03:04:41 +0000
Subject: [PATCH] rk3568 rt init

---
 kernel/drivers/gpu/drm/bridge/synopsys/dw-hdmi-cec.c |  239 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 230 insertions(+), 9 deletions(-)

diff --git a/kernel/drivers/gpu/drm/bridge/synopsys/dw-hdmi-cec.c b/kernel/drivers/gpu/drm/bridge/synopsys/dw-hdmi-cec.c
index 6c32351..993f525 100644
--- a/kernel/drivers/gpu/drm/bridge/synopsys/dw-hdmi-cec.c
+++ b/kernel/drivers/gpu/drm/bridge/synopsys/dw-hdmi-cec.c
@@ -7,8 +7,10 @@
  * it under the terms of the GNU General Public License version 2 as
  * published by the Free Software Foundation.
  */
+#include <linux/input.h>
 #include <linux/interrupt.h>
 #include <linux/io.h>
+#include <linux/miscdevice.h>
 #include <linux/module.h>
 #include <linux/platform_device.h>
 #include <linux/sched.h>
@@ -24,8 +26,11 @@
 enum {
 	HDMI_IH_CEC_STAT0	= 0x0106,
 	HDMI_IH_MUTE_CEC_STAT0	= 0x0186,
+	HDMI_IH_MUTE		= 0x01ff,
 
 	HDMI_CEC_CTRL		= 0x7d00,
+	CEC_TRANS_MASK		= 0x7,
+	CEC_CTRL_STANDBY	= BIT(4),
 	CEC_CTRL_START		= BIT(0),
 	CEC_CTRL_FRAME_TYP	= 3 << 1,
 	CEC_CTRL_RETRY		= 0 << 1,
@@ -50,12 +55,15 @@
 	HDMI_CEC_RX_CNT		= 0x7d08,
 	HDMI_CEC_TX_DATA0	= 0x7d10,
 	HDMI_CEC_RX_DATA0	= 0x7d20,
+	HDMI_CEC_RX_DATA1	= 0x7d21,
 	HDMI_CEC_LOCK		= 0x7d30,
 	HDMI_CEC_WKUPCTRL	= 0x7d31,
 };
 
 struct dw_hdmi_cec {
+	struct device *dev;
 	struct dw_hdmi *hdmi;
+	struct miscdevice misc_dev;
 	const struct dw_hdmi_cec_ops *ops;
 	u32 addresses;
 	struct cec_adapter *adap;
@@ -64,7 +72,12 @@
 	bool tx_done;
 	bool rx_done;
 	struct cec_notifier *notify;
+	struct input_dev *devinput;
 	int irq;
+	int wake_irq;
+	bool wake_en;
+	bool standby_en;
+	struct mutex wake_lock;
 };
 
 static void dw_hdmi_write(struct dw_hdmi_cec *cec, u8 val, int offset)
@@ -75,6 +88,11 @@
 static u8 dw_hdmi_read(struct dw_hdmi_cec *cec, int offset)
 {
 	return cec->ops->read(cec->hdmi, offset);
+}
+
+static void dw_hdmi_mod(struct dw_hdmi_cec *cec, unsigned int offset, u8 mask, u8 val)
+{
+	cec->ops->mod(cec->hdmi, val, mask, offset);
 }
 
 static int dw_hdmi_cec_log_addr(struct cec_adapter *adap, u8 logical_addr)
@@ -115,7 +133,7 @@
 		dw_hdmi_write(cec, msg->msg[i], HDMI_CEC_TX_DATA0 + i);
 
 	dw_hdmi_write(cec, msg->len, HDMI_CEC_TX_CNT);
-	dw_hdmi_write(cec, ctrl | CEC_CTRL_START, HDMI_CEC_CTRL);
+	dw_hdmi_mod(cec, HDMI_CEC_CTRL, CEC_TRANS_MASK, ctrl | CEC_CTRL_START);
 
 	return 0;
 }
@@ -191,19 +209,27 @@
 	struct dw_hdmi_cec *cec = cec_get_drvdata(adap);
 
 	if (!enable) {
-		dw_hdmi_write(cec, ~0, HDMI_CEC_MASK);
-		dw_hdmi_write(cec, ~0, HDMI_IH_MUTE_CEC_STAT0);
 		dw_hdmi_write(cec, 0, HDMI_CEC_POLARITY);
 
-		cec->ops->disable(cec->hdmi);
+		if (cec->wake_en && cec->standby_en) {
+			dw_hdmi_write(cec, 0xff, HDMI_IH_CEC_STAT0);
+			dw_hdmi_mod(cec, HDMI_CEC_CTRL, CEC_CTRL_STANDBY, CEC_CTRL_STANDBY);
+			dw_hdmi_write(cec, 0, HDMI_CEC_LOCK);
+			dw_hdmi_write(cec, 0xff, HDMI_CEC_WKUPCTRL);
+			dw_hdmi_write(cec, ~(1 << 6), HDMI_CEC_MASK);
+			dw_hdmi_write(cec, ~(1 << 6), HDMI_IH_MUTE_CEC_STAT0);
+			dw_hdmi_write(cec, 0x01, HDMI_IH_MUTE);
+		} else {
+			cec->ops->disable(cec->hdmi);
+		}
 	} else {
 		unsigned int irqs;
 
-		dw_hdmi_write(cec, 0, HDMI_CEC_CTRL);
+		dw_hdmi_cec_log_addr(cec->adap, CEC_LOG_ADDR_INVALID);
+		dw_hdmi_mod(cec, HDMI_CEC_CTRL, CEC_CTRL_STANDBY, 0);
+		dw_hdmi_write(cec, 0x02, HDMI_IH_MUTE);
 		dw_hdmi_write(cec, ~0, HDMI_IH_CEC_STAT0);
 		dw_hdmi_write(cec, 0, HDMI_CEC_LOCK);
-
-		dw_hdmi_cec_log_addr(cec->adap, CEC_LOG_ADDR_INVALID);
 
 		cec->ops->enable(cec->hdmi);
 
@@ -229,6 +255,170 @@
 	cec_delete_adapter(cec->adap);
 }
 
+static irqreturn_t dw_hdmi_cec_wake_irq(int irq, void *data)
+{
+	struct cec_adapter *adap = data;
+	struct dw_hdmi_cec *cec = cec_get_drvdata(adap);
+	u8 cec_int;
+
+	cec_int = dw_hdmi_read(cec, HDMI_IH_CEC_STAT0);
+	if (!cec_int)
+		return IRQ_NONE;
+
+	dw_hdmi_write(cec, 0x02, HDMI_IH_MUTE);
+	dw_hdmi_write(cec, cec_int, HDMI_IH_CEC_STAT0);
+	dw_hdmi_write(cec, 0x00, HDMI_CEC_WKUPCTRL);
+
+	if (!cec->wake_en)
+		return IRQ_HANDLED;
+
+	return IRQ_WAKE_THREAD;
+}
+
+static irqreturn_t dw_hdmi_cec_wake_thread(int irq, void *data)
+{
+	struct cec_adapter *adap = data;
+	struct dw_hdmi_cec *cec = cec_get_drvdata(adap);
+
+	mutex_lock(&cec->wake_lock);
+
+	if (!cec->standby_en) {
+		mutex_unlock(&cec->wake_lock);
+		return IRQ_HANDLED;
+	}
+	cec->standby_en = false;
+
+	dev_dbg(cec->dev, "wakeup opcode:0x%x\n", dw_hdmi_read(cec, HDMI_CEC_RX_DATA1));
+	input_event(cec->devinput, EV_KEY, KEY_POWER, 1);
+	input_sync(cec->devinput);
+	input_event(cec->devinput, EV_KEY, KEY_POWER, 0);
+	input_sync(cec->devinput);
+	mutex_unlock(&cec->wake_lock);
+
+	return IRQ_HANDLED;
+}
+
+static int rockchip_hdmi_cec_input_init(struct dw_hdmi_cec *cec)
+{
+	int err;
+
+	cec->devinput = devm_input_allocate_device(cec->dev);
+	if (!cec->devinput)
+		return -EPERM;
+
+	cec->devinput->name = "hdmi_cec_key";
+	cec->devinput->phys = "hdmi_cec_key/input0";
+	cec->devinput->id.bustype = BUS_HOST;
+	cec->devinput->id.vendor = 0x0001;
+	cec->devinput->id.product = 0x0001;
+	cec->devinput->id.version = 0x0100;
+
+	err = input_register_device(cec->devinput);
+	if (err < 0) {
+		input_free_device(cec->devinput);
+		return err;
+	}
+	input_set_capability(cec->devinput, EV_KEY, KEY_POWER);
+
+	return 0;
+}
+
+static long cec_standby(struct cec_adapter *adap, __u8 __user *parg)
+{
+	u8 en;
+	int ret;
+	struct dw_hdmi_cec *cec = cec_get_drvdata(adap);
+
+	mutex_lock(&cec->wake_lock);
+	if (copy_from_user(&en, parg, sizeof(en))) {
+		mutex_unlock(&cec->wake_lock);
+		return -EFAULT;
+	}
+
+	cec->standby_en = !en;
+	ret = adap->ops->adap_enable(adap, en);
+	mutex_unlock(&cec->wake_lock);
+
+	return ret;
+}
+
+static long cec_func_en(struct dw_hdmi_cec *cec, int __user *parg)
+{
+	int en_mask;
+
+	if (copy_from_user(&en_mask, parg, sizeof(en_mask)))
+		return -EFAULT;
+
+	cec->wake_en = (en_mask & CEC_EN) && (en_mask & CEC_WAKE);
+
+	return 0;
+}
+
+static long dw_hdmi_cec_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
+{
+	struct dw_hdmi_cec *cec;
+	struct miscdevice *misc_dev;
+	void __user *data;
+
+	if (!f)
+		return -EFAULT;
+
+	misc_dev = f->private_data;
+	cec = container_of(misc_dev, struct dw_hdmi_cec, misc_dev);
+	data = (void __user *)arg;
+
+	switch (cmd) {
+	case CEC_STANDBY:
+		return cec_standby(cec->adap, data);
+	case CEC_FUNC_EN:
+		return cec_func_en(cec, data);
+	default:
+		return -EINVAL;
+	}
+
+	return -ENOTTY;
+}
+
+static int dw_hdmi_cec_open(struct inode *inode, struct file *f)
+{
+	return 0;
+}
+
+static int dw_hdmi_cec_release(struct inode *inode, struct file *f)
+{
+	return 0;
+}
+
+static const struct file_operations dw_hdmi_cec_file_operations = {
+	.compat_ioctl = dw_hdmi_cec_ioctl,
+	.unlocked_ioctl = dw_hdmi_cec_ioctl,
+	.open = dw_hdmi_cec_open,
+	.release = dw_hdmi_cec_release,
+	.owner = THIS_MODULE,
+};
+
+void dw_hdmi_hpd_wake_up(struct platform_device *pdev)
+{
+	struct dw_hdmi_cec *cec = platform_get_drvdata(pdev);
+
+	mutex_lock(&cec->wake_lock);
+
+	if (!cec->standby_en) {
+		mutex_unlock(&cec->wake_lock);
+		return;
+	}
+	cec->standby_en = false;
+
+	dw_hdmi_write(cec, 0x02, HDMI_IH_MUTE);
+
+	input_event(cec->devinput, EV_KEY, KEY_POWER, 1);
+	input_sync(cec->devinput);
+	input_event(cec->devinput, EV_KEY, KEY_POWER, 0);
+	input_sync(cec->devinput);
+	mutex_unlock(&cec->wake_lock);
+}
+EXPORT_SYMBOL_GPL(dw_hdmi_hpd_wake_up);
+
 static int dw_hdmi_cec_probe(struct platform_device *pdev)
 {
 	struct dw_hdmi_cec_data *data = dev_get_platdata(&pdev->dev);
@@ -247,9 +437,13 @@
 	if (!cec)
 		return -ENOMEM;
 
+	cec->dev = &pdev->dev;
 	cec->irq = data->irq;
+	cec->wake_irq = data->wake_irq;
 	cec->ops = data->ops;
 	cec->hdmi = data->hdmi;
+
+	mutex_init(&cec->wake_lock);
 
 	platform_set_drvdata(pdev, cec);
 
@@ -276,10 +470,26 @@
 
 	ret = devm_request_threaded_irq(&pdev->dev, cec->irq,
 					dw_hdmi_cec_hardirq,
-					dw_hdmi_cec_thread, IRQF_SHARED,
+					dw_hdmi_cec_thread, IRQF_SHARED | IRQF_ONESHOT,
 					"dw-hdmi-cec", cec->adap);
 	if (ret < 0)
 		return ret;
+
+	if (cec->wake_irq > 0) {
+		ret = devm_request_threaded_irq(&pdev->dev, cec->wake_irq,
+						dw_hdmi_cec_wake_irq,
+						dw_hdmi_cec_wake_thread,
+						IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
+						"cec-wakeup", cec->adap);
+		if (ret) {
+			dev_err(&pdev->dev,
+				"hdmi_cec request_irq failed (%d).\n",
+				ret);
+			return ret;
+		}
+		device_init_wakeup(&pdev->dev, 1);
+		enable_irq_wake(cec->wake_irq);
+	}
 
 	cec->notify = cec_notifier_get(pdev->dev.parent);
 	if (!cec->notify)
@@ -298,8 +508,18 @@
 	devm_remove_action(&pdev->dev, dw_hdmi_cec_del, cec);
 
 	cec_register_cec_notifier(cec->adap, cec->notify);
+	rockchip_hdmi_cec_input_init(cec);
 
-	return 0;
+	cec->misc_dev.name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "rk_cec");
+	if (!cec->misc_dev.name)
+		return -ENOMEM;
+	cec->misc_dev.minor = MISC_DYNAMIC_MINOR;
+	cec->misc_dev.fops = &dw_hdmi_cec_file_operations;
+	cec->misc_dev.mode = 0666;
+
+	ret = misc_register(&cec->misc_dev);
+
+	return ret;
 }
 
 static int dw_hdmi_cec_remove(struct platform_device *pdev)
@@ -308,6 +528,7 @@
 
 	cec_unregister_adapter(cec->adap);
 	cec_notifier_put(cec->notify);
+	misc_deregister(&cec->misc_dev);
 
 	return 0;
 }

--
Gitblit v1.6.2