All of lore.kernel.org
 help / color / mirror / Atom feed
From: Chunfeng Yun <chunfeng.yun@mediatek.com>
To: Rob Herring <robh+dt@kernel.org>,
	Felipe Balbi <felipe.balbi@linux.intel.com>,
	Matthias Brugger <matthias.bgg@gmail.com>,
	Mathias Nyman <mathias.nyman@intel.com>
Cc: Mark Rutland <mark.rutland@arm.com>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	Catalin Marinas <catalin.marinas@arm.com>,
	Will Deacon <will.deacon@arm.com>,
	Chunfeng Yun <chunfeng.yun@mediatek.com>,
	Jean Delvare <jdelvare@suse.de>,
	Sean Wang <sean.wang@mediatek.com>, <devicetree@vger.kernel.org>,
	<linux-kernel@vger.kernel.org>, <linux-usb@vger.kernel.org>,
	<linux-arm-kernel@lists.infradead.org>,
	<linux-mediatek@lists.infradead.org>
Subject: [PATCH 1/7] soc: mediatek: Add USB wakeup driver
Date: Sat, 9 Dec 2017 16:45:30 +0800	[thread overview]
Message-ID: <1512809136-2779-2-git-send-email-chunfeng.yun@mediatek.com> (raw)
In-Reply-To: <1512809136-2779-1-git-send-email-chunfeng.yun@mediatek.com>

This driver is used to support usb wakeup which is controlled by
the glue layer between SSUSB and SPM. Usually the glue layer is put
into a system controller, such as pericfg module, which is
represented by a syscon node in DTS.
Due to the glue layer may vary on different SoCs, it's useful to
extract a separated driver to simplify usb controller drivers.

Signed-off-by: Chunfeng Yun <chunfeng.yun@mediatek.com>
---
 drivers/soc/mediatek/Kconfig                  |   8 +
 drivers/soc/mediatek/Makefile                 |   1 +
 drivers/soc/mediatek/mtk-usb-wakeup.c         | 519 ++++++++++++++++++++++++++
 include/dt-bindings/soc/mediatek,usb-wakeup.h |  15 +
 include/linux/soc/mediatek/usb-wakeup.h       |  88 +++++
 5 files changed, 631 insertions(+)
 create mode 100644 drivers/soc/mediatek/mtk-usb-wakeup.c
 create mode 100644 include/dt-bindings/soc/mediatek,usb-wakeup.h
 create mode 100644 include/linux/soc/mediatek/usb-wakeup.h

diff --git a/drivers/soc/mediatek/Kconfig b/drivers/soc/mediatek/Kconfig
index a7d0667..30cd226 100644
--- a/drivers/soc/mediatek/Kconfig
+++ b/drivers/soc/mediatek/Kconfig
@@ -31,4 +31,12 @@ config MTK_SCPSYS
 	  Say yes here to add support for the MediaTek SCPSYS power domain
 	  driver.
 
+config MTK_UWK
+	bool "MediaTek USB Wakeup Support"
+	select REGMAP
+	help
+	  Say yes here to add support for the MediaTek SSUSB-SPM glue layer
+	  which supports some different type of USB wakeup, such as IP-SLEEP,
+	  LINESTATE, IDDIG etc, and it can support multi SSUSB controllers.
+
 endmenu
diff --git a/drivers/soc/mediatek/Makefile b/drivers/soc/mediatek/Makefile
index 12998b0..66fbb54f 100644
--- a/drivers/soc/mediatek/Makefile
+++ b/drivers/soc/mediatek/Makefile
@@ -1,3 +1,4 @@
 obj-$(CONFIG_MTK_INFRACFG) += mtk-infracfg.o
 obj-$(CONFIG_MTK_PMIC_WRAP) += mtk-pmic-wrap.o
 obj-$(CONFIG_MTK_SCPSYS) += mtk-scpsys.o
+obj-$(CONFIG_MTK_UWK) += mtk-usb-wakeup.o
diff --git a/drivers/soc/mediatek/mtk-usb-wakeup.c b/drivers/soc/mediatek/mtk-usb-wakeup.c
new file mode 100644
index 0000000..16539a6
--- /dev/null
+++ b/drivers/soc/mediatek/mtk-usb-wakeup.c
@@ -0,0 +1,519 @@
+/*
+ * Copyright (c) 2017 MediaTek Inc.
+ * Author: Chunfeng Yun <chunfeng.yun@mediatek.com>
+ *
+ * SPDX-License-Identifier: GPL-2.0
+ *
+ */
+
+#include <dt-bindings/soc/mediatek,usb-wakeup.h>
+#include <linux/kernel.h>
+#include <linux/mfd/syscon.h>
+#include <linux/module.h>
+#include <linux/of.h>
+#include <linux/of_platform.h>
+#include <linux/regmap.h>
+#include <linux/slab.h>
+#include <linux/soc/mediatek/usb-wakeup.h>
+
+/* mt8173, mt8176 etc */
+#define PERI_WK_CTRL1	0x4
+#define WC1_IS_C(x)	(((x) & 0xf) << 26) /* cycle debounce */
+#define WC1_IS_EN	BIT(25)
+#define WC1_IS_P	BIT(6)  /* polarity for ip sleep */
+
+/* mt2712 etc */
+#define PERI_SSUSB_SPM_CTRL	0x0
+#define SSC_LINE_STATE_CHG	GENMASK(11, 8)
+#define SSC_LINE_STATE_EN	GENMASK(6, 5)
+#define SSC_IP_SLEEP_EN	BIT(4)
+#define SSC_SPM_INT_EN		BIT(1)
+
+enum mtk_uwk_vers {
+	MTK_UWK_V1 = 1,
+	MTK_UWK_V2,
+};
+
+struct mtk_uwk_pdata {
+	enum mtk_uwk_vers vers;
+};
+
+/**
+ * @reg_base: register offset within a syscon @wkc (e.g. pericfg module)
+ * @type: the types of wakeup, such as IP-SLEEP, LINE-STATE etc
+ */
+struct mtk_uwk_instance {
+	struct mtu_wakeup uwk;
+	u32 reg_base;
+	u32 reg_len;
+	u32 type;
+};
+
+struct mtk_uwk {
+	struct device *dev;
+	struct regmap *wkc;
+	const struct mtk_uwk_pdata *data;
+	struct mtk_uwk_instance **inst;
+	int num_inst;
+};
+
+static LIST_HEAD(of_uwk_providers);
+static DEFINE_MUTEX(of_uwk_mutex);
+
+static struct mtu_wakeup_provider *of_uwk_provider_add(struct device *dev,
+		struct mtu_wakeup *(*of_xlate)(struct device *dev,
+		struct of_phandle_args *args))
+{
+	struct mtu_wakeup_provider *provider;
+
+	provider = kzalloc(sizeof(*provider), GFP_KERNEL);
+	if (!provider)
+		return ERR_PTR(-ENOMEM);
+
+	provider->dev = dev;
+	provider->of_node = of_node_get(dev->of_node);
+	provider->of_xlate = of_xlate;
+
+	mutex_lock(&of_uwk_mutex);
+	list_add_tail(&provider->list, &of_uwk_providers);
+	mutex_unlock(&of_uwk_mutex);
+
+	return provider;
+}
+
+static void of_uwk_provider_del(struct device_node *np)
+{
+	struct mtu_wakeup_provider *provider;
+
+	mutex_lock(&of_uwk_mutex);
+	list_for_each_entry(provider, &of_uwk_providers, list) {
+		if (provider->of_node == np) {
+			list_del(&provider->list);
+			of_node_put(provider->of_node);
+			kfree(provider);
+			break;
+		}
+	}
+	mutex_unlock(&of_uwk_mutex);
+}
+
+static struct mtu_wakeup *of_uwk_get_from_provider(
+		struct of_phandle_args *args)
+{
+	struct mtu_wakeup_provider *provider;
+	struct device_node *child_np;
+	struct mtu_wakeup *uwk;
+
+	mutex_lock(&of_uwk_mutex);
+	list_for_each_entry(provider, &of_uwk_providers, list) {
+		for_each_child_of_node(provider->of_node, child_np) {
+			if (child_np == args->np) {
+				uwk = provider->of_xlate(provider->dev, args);
+				mutex_unlock(&of_uwk_mutex);
+				return uwk;
+			}
+		}
+	}
+	mutex_unlock(&of_uwk_mutex);
+
+	return ERR_PTR(-EPROBE_DEFER);
+}
+
+static struct mtu_wakeup *of_uwk_get(struct device_node *np, int index)
+{
+	struct mtu_wakeup *uwk = NULL;
+	struct of_phandle_args args;
+	int ret;
+
+	ret = of_parse_phandle_with_args(np, "mediatek,uwks",
+				"#mediatek,uwk-cells", index, &args);
+	if (ret)
+		return ERR_PTR(-ENODEV);
+
+	if (!of_device_is_available(args.np)) {
+		dev_warn(uwk->parent, "Requested uwk is disabled\n");
+		uwk = ERR_PTR(-ENODEV);
+		goto put_node;
+	}
+
+	uwk = of_uwk_get_from_provider(&args);
+
+put_node:
+	of_node_put(args.np);
+	return uwk;
+}
+
+static void devm_uwk_release(struct device *dev, void *res)
+{
+	struct mtu_wakeup *uwk = *(struct mtu_wakeup **)res;
+
+	if (IS_ERR_OR_NULL(uwk))
+		return;
+
+	module_put(uwk->ops->owner);
+	put_device(uwk->parent);
+}
+
+struct mtu_wakeup *devm_of_uwk_get_by_index(
+		struct device *dev, struct device_node *np, int index)
+{
+	struct mtu_wakeup **ptr, *uwk;
+
+	ptr = devres_alloc(devm_uwk_release, sizeof(*ptr), GFP_KERNEL);
+	if (!ptr)
+		return ERR_PTR(-ENOMEM);
+
+	uwk = of_uwk_get(np, index);
+	if (IS_ERR(uwk)) {
+		devres_free(ptr);
+		return uwk;
+	}
+
+	if (!try_module_get(uwk->ops->owner)) {
+		devres_free(ptr);
+		return ERR_PTR(-EPROBE_DEFER);
+	}
+
+	get_device(uwk->parent);
+
+	*ptr = uwk;
+	devres_add(dev, ptr);
+
+	return uwk;
+}
+EXPORT_SYMBOL_GPL(devm_of_uwk_get_by_index);
+
+int mtu_wakeup_enable(struct mtu_wakeup *uwk)
+{
+	int ret = 0;
+
+	if (!uwk)
+		return 0;
+
+	mutex_lock(&uwk->mutex);
+	if (uwk->count == 0 && uwk->ops->enable) {
+		ret = uwk->ops->enable(uwk);
+		if (ret) {
+			dev_err(uwk->parent, "uwk enable failed(%d)\n", ret);
+			goto out;
+		}
+	}
+	++uwk->count;
+
+out:
+	mutex_unlock(&uwk->mutex);
+	return ret;
+}
+EXPORT_SYMBOL_GPL(mtu_wakeup_enable);
+
+int mtu_wakeup_disable(struct mtu_wakeup *uwk)
+{
+	int ret = 0;
+
+	if (!uwk)
+		return 0;
+
+	mutex_lock(&uwk->mutex);
+	if (uwk->count == 1 && uwk->ops->disable) {
+		ret =  uwk->ops->disable(uwk);
+		if (ret) {
+			dev_err(uwk->parent, "uwk disable failed(%d)\n", ret);
+			goto out;
+		}
+	}
+	--uwk->count;
+
+out:
+	mutex_unlock(&uwk->mutex);
+	return ret;
+}
+EXPORT_SYMBOL_GPL(mtu_wakeup_disable);
+
+static struct mtk_uwk_instance *to_mwk_inst(struct mtu_wakeup *uwk)
+{
+	return uwk ? container_of(uwk, struct mtk_uwk_instance, uwk) : NULL;
+}
+
+static int mwk_v1_enable(struct mtk_uwk *mwk, struct mtk_uwk_instance *inst)
+{
+	struct regmap *wkc = mwk->wkc;
+	u32 val;
+
+	/* Only IP-SLEEP is supported */
+	if (inst->type != MTU_WK_IP_SLEEP)
+		return 0;
+
+	regmap_read(wkc, PERI_WK_CTRL1, &val);
+	val &= ~(WC1_IS_P | WC1_IS_C(0xf));
+	val |= WC1_IS_EN | WC1_IS_C(0x8);
+	regmap_write(wkc, PERI_WK_CTRL1, val);
+	regmap_read(wkc, PERI_WK_CTRL1, &val);
+	dev_dbg(mwk->dev, "%s: WK_CTRL1=%#x, type=%d\n",
+		__func__, val, inst->type);
+
+	return 0;
+}
+
+static int mwk_v1_disable(struct mtk_uwk *mwk, struct mtk_uwk_instance *inst)
+{
+	if (inst->type == MTU_WK_IP_SLEEP)
+		regmap_update_bits(mwk->wkc, PERI_WK_CTRL1, WC1_IS_EN, 0);
+
+	return 0;
+}
+
+static int mwk_v2_enable(struct mtk_uwk *mwk, struct mtk_uwk_instance *inst)
+{
+	struct regmap *wkc = mwk->wkc;
+	u32 rbase = inst->reg_base;
+	u32 val;
+
+	regmap_read(wkc, rbase + PERI_SSUSB_SPM_CTRL, &val);
+	switch (inst->type) {
+	case MTU_WK_IP_SLEEP:
+		val |= SSC_IP_SLEEP_EN;
+		break;
+	case MTU_WK_LINE_STATE:
+		val |= SSC_LINE_STATE_EN | SSC_LINE_STATE_CHG;
+		break;
+	default:
+		/* checked by xlate, ignore the error */
+		break;
+	}
+	val |= SSC_SPM_INT_EN;
+	regmap_write(wkc, rbase + PERI_SSUSB_SPM_CTRL, val);
+	regmap_read(wkc, rbase + PERI_SSUSB_SPM_CTRL, &val);
+	dev_dbg(mwk->dev, "%s: CTRL=%#x, type=%d\n",
+		__func__, val, inst->type);
+
+	return 0;
+}
+
+static int mwk_v2_disable(struct mtk_uwk *mwk, struct mtk_uwk_instance *inst)
+{
+	struct regmap *wkc = mwk->wkc;
+	u32 rbase = inst->reg_base;
+	u32 val;
+
+	regmap_read(wkc, rbase + PERI_SSUSB_SPM_CTRL, &val);
+	switch (inst->type) {
+	case MTU_WK_IP_SLEEP:
+		val &= ~SSC_IP_SLEEP_EN;
+		break;
+	case MTU_WK_LINE_STATE:
+		val &= ~(SSC_LINE_STATE_EN | SSC_LINE_STATE_CHG);
+		break;
+	default:
+		break;
+	}
+	val &= ~SSC_SPM_INT_EN;
+	regmap_write(wkc, rbase + PERI_SSUSB_SPM_CTRL, val);
+	dev_dbg(mwk->dev, "%s: type=%d\n", __func__, inst->type);
+
+	return 0;
+}
+
+static int mwk_enable(struct mtu_wakeup *uwk)
+{
+	struct mtk_uwk_instance *inst = to_mwk_inst(uwk);
+	struct mtk_uwk *mwk = dev_get_drvdata(uwk->parent);
+	int ret = 0;
+
+	switch (mwk->data->vers) {
+	case MTK_UWK_V1:
+		ret = mwk_v1_enable(mwk, inst);
+		break;
+	case MTK_UWK_V2:
+		ret = mwk_v2_enable(mwk, inst);
+		break;
+	default:
+		break;
+	}
+	return ret;
+}
+
+static int mwk_disable(struct mtu_wakeup *uwk)
+{
+	struct mtk_uwk_instance *inst = to_mwk_inst(uwk);
+	struct mtk_uwk *mwk = dev_get_drvdata(uwk->parent);
+	int ret = 0;
+
+	switch (mwk->data->vers) {
+	case MTK_UWK_V1:
+		ret = mwk_v1_disable(mwk, inst);
+		break;
+	case MTK_UWK_V2:
+		ret = mwk_v2_disable(mwk, inst);
+		break;
+	default:
+		break;
+	}
+	return ret;
+}
+
+static struct mtk_uwk_instance *mwk_inst_create(struct device *dev,
+		struct device_node *np,
+		const struct mtu_wakeup_ops *ops)
+{
+	struct mtk_uwk_instance *inst;
+	struct mtu_wakeup *uwk;
+	u32 buf[2];
+	int ret;
+
+	inst = devm_kzalloc(dev, sizeof(*inst), GFP_KERNEL);
+	if (!inst)
+		return ERR_PTR(-ENOMEM);
+
+	ret = of_property_read_u32_array(np, "reg", buf, ARRAY_SIZE(buf));
+	if (ret) {
+		dev_err(dev, "fail to read reg\n");
+		return ERR_PTR(ret);
+	}
+
+	inst->reg_base = buf[0];
+	inst->reg_len = buf[1];
+	uwk = &inst->uwk;
+	uwk->node = np;
+	uwk->ops = ops;
+	uwk->parent = dev;
+	mutex_init(&uwk->mutex);
+	dev_dbg(dev, "reg: %#x/%#x\n", inst->reg_base, inst->reg_len);
+
+	return inst;
+}
+
+static struct mtu_wakeup *mwk_xlate(struct device *dev,
+		struct of_phandle_args *args)
+{
+	struct mtk_uwk *mwk = dev_get_drvdata(dev);
+	struct mtk_uwk_instance *inst = NULL;
+	struct device_node *uwk_np = args->np;
+	int index;
+
+	if (args->args_count != 1) {
+		dev_err(dev, "invalid number of cells in uwk property\n");
+		return ERR_PTR(-EINVAL);
+	}
+
+	for (index = 0; index < mwk->num_inst; index++)
+		if (uwk_np == mwk->inst[index]->uwk.node) {
+			inst = mwk->inst[index];
+			break;
+		}
+
+	if (!inst) {
+		dev_err(dev, "failed to find appropriate uwk\n");
+		return ERR_PTR(-EINVAL);
+	}
+
+	inst->type = args->args[0];
+	if (!(inst->type == MTU_WK_IP_SLEEP ||
+	      inst->type == MTU_WK_LINE_STATE)) {
+		dev_err(dev, "unsupported uwk type=%d\n", inst->type);
+		return ERR_PTR(-EINVAL);
+	}
+
+	return &inst->uwk;
+}
+
+static const struct mtu_wakeup_ops mwk_ops = {
+	.enable = mwk_enable,
+	.disable = mwk_disable,
+	.owner = THIS_MODULE,
+};
+
+static const struct mtk_uwk_pdata mwk_v1_pdata = {
+	.vers = MTK_UWK_V1,
+};
+
+static const struct mtk_uwk_pdata mwk_v2_pdata = {
+	.vers = MTK_UWK_V2,
+};
+
+static const struct of_device_id mwk_id_table[] = {
+	{ .compatible = "mediatek,usb-wk-v1", .data = &mwk_v1_pdata },
+	{ .compatible = "mediatek,usb-wk-v2", .data = &mwk_v2_pdata },
+	{ },
+};
+MODULE_DEVICE_TABLE(of, mwk_id_table);
+
+static int mtk_uwk_probe(struct platform_device *pdev)
+{
+	struct device *dev = &pdev->dev;
+	struct device_node *np = dev->of_node;
+	struct device_node *child_np;
+	struct mtu_wakeup_provider *provider;
+	struct mtk_uwk *mwk;
+	int index;
+	int ret;
+
+	mwk = devm_kzalloc(dev, sizeof(*mwk), GFP_KERNEL);
+	if (!mwk)
+		return -ENOMEM;
+
+	mwk->data = of_device_get_match_data(dev);
+	if (!mwk->data)
+		return -EINVAL;
+
+	mwk->num_inst = of_get_child_count(np);
+	mwk->inst = devm_kcalloc(dev, mwk->num_inst,
+				  sizeof(*mwk->inst), GFP_KERNEL);
+	if (!mwk->inst)
+		return -ENOMEM;
+
+	mwk->dev = dev;
+	platform_set_drvdata(pdev, mwk);
+
+	mwk->wkc = syscon_regmap_lookup_by_phandle(np, "mediatek,wkc");
+	if (IS_ERR(mwk->wkc)) {
+		dev_err(dev, "fail to get mediatek,wkc syscon\n");
+		return PTR_ERR(mwk->wkc);
+	}
+
+	index = 0;
+	for_each_child_of_node(np, child_np) {
+		struct mtk_uwk_instance *inst;
+
+		inst = mwk_inst_create(dev, child_np, &mwk_ops);
+		if (IS_ERR(inst)) {
+			dev_err(dev, "failed to create mwk instance\n");
+			ret = PTR_ERR(inst);
+			goto put_child;
+		}
+
+		mwk->inst[index] = inst;
+		index++;
+	}
+
+	provider = of_uwk_provider_add(dev, mwk_xlate);
+
+	return PTR_ERR_OR_ZERO(provider);
+
+put_child:
+	of_node_put(child_np);
+	return ret;
+}
+
+static int mtk_uwk_remove(struct platform_device *pdev)
+{
+	struct device *dev = &pdev->dev;
+	struct device_node *np = dev->of_node;
+
+	of_uwk_provider_del(np);
+	return 0;
+}
+
+static struct platform_driver mtk_uwk_drv = {
+	.probe = mtk_uwk_probe,
+	.remove = mtk_uwk_remove,
+	.driver = {
+		.name = "mtk_uwk",
+		.owner = THIS_MODULE,
+		.of_match_table = mwk_id_table,
+	},
+};
+
+module_platform_driver(mtk_uwk_drv);
+MODULE_AUTHOR("Chunfeng Yun <chunfeng.yun@mediatek.com>");
+MODULE_DESCRIPTION("MediaTek USB Wakeup driver");
+MODULE_LICENSE("GPL v2");
diff --git a/include/dt-bindings/soc/mediatek,usb-wakeup.h b/include/dt-bindings/soc/mediatek,usb-wakeup.h
new file mode 100644
index 0000000..2461795
--- /dev/null
+++ b/include/dt-bindings/soc/mediatek,usb-wakeup.h
@@ -0,0 +1,15 @@
+/*
+ * Copyright (c) 2017 MediaTek Inc.
+ * Author: Chunfeng Yun <chunfeng.yun@mediatek.com>
+ *
+ * SPDX-License-Identifier: GPL-2.0
+ *
+ */
+
+#ifndef __DT_BINDINGS_MTK_USB_WK_H__
+#define __DT_BINDINGS_MTK_USB_WK_H__
+
+#define MTU_WK_IP_SLEEP	1
+#define MTU_WK_LINE_STATE	2
+
+#endif
diff --git a/include/linux/soc/mediatek/usb-wakeup.h b/include/linux/soc/mediatek/usb-wakeup.h
new file mode 100644
index 0000000..5697367
--- /dev/null
+++ b/include/linux/soc/mediatek/usb-wakeup.h
@@ -0,0 +1,88 @@
+/*
+ * Copyright (c) 2017 MediaTek Inc.
+ * Author: Chunfeng Yun <chunfeng.yun@mediatek.com>
+ *
+ * SPDX-License-Identifier: GPL-2.0
+ *
+ */
+
+#ifndef __MTK_USB_WAKEUP_H__
+#define __MTK_USB_WAKEUP_H__
+
+#include <linux/device.h>
+#include <linux/err.h>
+#include <linux/mutex.h>
+#include <linux/of.h>
+
+struct mtu_wakeup;
+
+/**
+ * struct mtu_wakeup_ops - set of function pointers for performing
+ *    mtu_wakeup operations
+ * @enable: enable a type of usb wakeup when system suspend
+ * @disable: disable a type of usb wakeup when system resume
+ * @owner: the module owner using the ops
+ */
+struct mtu_wakeup_ops {
+	int	(*enable)(struct mtu_wakeup *uwk);
+	int	(*disable)(struct mtu_wakeup *uwk);
+	struct module *owner;
+};
+
+/**
+ * struct mtu_wakeup - represents the MediaTek USB wakeup device
+ * @parent: the parent device of the mtu_wakeup
+ * @node: associated device tree node
+ * @ops: function pointers for performing mtu_wakeup operations
+ * @mutex: mutex to protect @ops
+ * @count: used to protect when the mtu_wakeup is used by multiple consumers
+ */
+struct mtu_wakeup {
+	struct device *parent;
+	struct device_node *node;
+	const struct mtu_wakeup_ops *ops;
+	struct mutex mutex;
+	int count;
+};
+
+/**
+ * struct mtu_wakeup_provider - represents the mtu_wakeup provider
+ * @dev: the parent device of the mtu_wakeup
+ * @list: to maintain a linked list of mtu_wakeup providers
+ * @of_node: associated device tree node
+ * @of_xlate: function pointer to obtain mtu_wakeup instance from
+ *	its tree node
+ */
+struct mtu_wakeup_provider {
+	struct device *dev;
+	struct list_head list;
+	struct device_node *of_node;
+	struct mtu_wakeup *(*of_xlate)(struct device *dev,
+		struct of_phandle_args *args);
+};
+
+#if IS_ENABLED(CONFIG_MTK_UWK)
+struct mtu_wakeup *devm_of_uwk_get_by_index(
+	struct device *dev, struct device_node *np, int index);
+int mtu_wakeup_enable(struct mtu_wakeup *uwk);
+int mtu_wakeup_disable(struct mtu_wakeup *uwk);
+
+#else
+struct mtu_wakeup *devm_of_uwk_get_by_index(
+	struct device *dev, struct device_node *np, int index)
+{
+	return ERR_PTR(-ENODEV);
+}
+
+int mtu_wakeup_enable(struct mtu_wakeup *uwk)
+{
+	return uwk ? -ENODEV : 0;
+}
+
+int mtu_wakeup_disable(struct mtu_wakeup *uwk)
+{
+	return uwk ? -ENODEV : 0;
+}
+#endif
+
+#endif	/* __MTK_USB_WAKEUP_H__ */
-- 
1.9.1

WARNING: multiple messages have this Message-ID (diff)
From: Chunfeng Yun <chunfeng.yun-NuS5LvNUpcJWk0Htik3J/w@public.gmane.org>
To: Rob Herring <robh+dt-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org>,
	Felipe Balbi
	<felipe.balbi-VuQAYsv1563Yd54FQh9/CA@public.gmane.org>,
	Matthias Brugger
	<matthias.bgg-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>,
	Mathias Nyman
	<mathias.nyman-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org>
Cc: Mark Rutland <mark.rutland-5wv7dgnIgG8@public.gmane.org>,
	Greg Kroah-Hartman
	<gregkh-hQyY1W1yCW8ekmWlsbkhG0B+6BGkLq7r@public.gmane.org>,
	Catalin Marinas <catalin.marinas-5wv7dgnIgG8@public.gmane.org>,
	Will Deacon <will.deacon-5wv7dgnIgG8@public.gmane.org>,
	Chunfeng Yun
	<chunfeng.yun-NuS5LvNUpcJWk0Htik3J/w@public.gmane.org>,
	Jean Delvare <jdelvare-l3A5Bk7waGM@public.gmane.org>,
	Sean Wang <sean.wang-NuS5LvNUpcJWk0Htik3J/w@public.gmane.org>,
	devicetree-u79uwXL29TY76Z2rM5mHXA@public.gmane.org,
	linux-kernel-u79uwXL29TY76Z2rM5mHXA@public.gmane.org,
	linux-usb-u79uwXL29TY76Z2rM5mHXA@public.gmane.org,
	linux-arm-kernel-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r@public.gmane.org,
	linux-mediatek-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r@public.gmane.org
Subject: [PATCH 1/7] soc: mediatek: Add USB wakeup driver
Date: Sat, 9 Dec 2017 16:45:30 +0800	[thread overview]
Message-ID: <1512809136-2779-2-git-send-email-chunfeng.yun@mediatek.com> (raw)
In-Reply-To: <1512809136-2779-1-git-send-email-chunfeng.yun-NuS5LvNUpcJWk0Htik3J/w@public.gmane.org>

This driver is used to support usb wakeup which is controlled by
the glue layer between SSUSB and SPM. Usually the glue layer is put
into a system controller, such as pericfg module, which is
represented by a syscon node in DTS.
Due to the glue layer may vary on different SoCs, it's useful to
extract a separated driver to simplify usb controller drivers.

Signed-off-by: Chunfeng Yun <chunfeng.yun-NuS5LvNUpcJWk0Htik3J/w@public.gmane.org>
---
 drivers/soc/mediatek/Kconfig                  |   8 +
 drivers/soc/mediatek/Makefile                 |   1 +
 drivers/soc/mediatek/mtk-usb-wakeup.c         | 519 ++++++++++++++++++++++++++
 include/dt-bindings/soc/mediatek,usb-wakeup.h |  15 +
 include/linux/soc/mediatek/usb-wakeup.h       |  88 +++++
 5 files changed, 631 insertions(+)
 create mode 100644 drivers/soc/mediatek/mtk-usb-wakeup.c
 create mode 100644 include/dt-bindings/soc/mediatek,usb-wakeup.h
 create mode 100644 include/linux/soc/mediatek/usb-wakeup.h

diff --git a/drivers/soc/mediatek/Kconfig b/drivers/soc/mediatek/Kconfig
index a7d0667..30cd226 100644
--- a/drivers/soc/mediatek/Kconfig
+++ b/drivers/soc/mediatek/Kconfig
@@ -31,4 +31,12 @@ config MTK_SCPSYS
 	  Say yes here to add support for the MediaTek SCPSYS power domain
 	  driver.
 
+config MTK_UWK
+	bool "MediaTek USB Wakeup Support"
+	select REGMAP
+	help
+	  Say yes here to add support for the MediaTek SSUSB-SPM glue layer
+	  which supports some different type of USB wakeup, such as IP-SLEEP,
+	  LINESTATE, IDDIG etc, and it can support multi SSUSB controllers.
+
 endmenu
diff --git a/drivers/soc/mediatek/Makefile b/drivers/soc/mediatek/Makefile
index 12998b0..66fbb54f 100644
--- a/drivers/soc/mediatek/Makefile
+++ b/drivers/soc/mediatek/Makefile
@@ -1,3 +1,4 @@
 obj-$(CONFIG_MTK_INFRACFG) += mtk-infracfg.o
 obj-$(CONFIG_MTK_PMIC_WRAP) += mtk-pmic-wrap.o
 obj-$(CONFIG_MTK_SCPSYS) += mtk-scpsys.o
+obj-$(CONFIG_MTK_UWK) += mtk-usb-wakeup.o
diff --git a/drivers/soc/mediatek/mtk-usb-wakeup.c b/drivers/soc/mediatek/mtk-usb-wakeup.c
new file mode 100644
index 0000000..16539a6
--- /dev/null
+++ b/drivers/soc/mediatek/mtk-usb-wakeup.c
@@ -0,0 +1,519 @@
+/*
+ * Copyright (c) 2017 MediaTek Inc.
+ * Author: Chunfeng Yun <chunfeng.yun-NuS5LvNUpcJWk0Htik3J/w@public.gmane.org>
+ *
+ * SPDX-License-Identifier: GPL-2.0
+ *
+ */
+
+#include <dt-bindings/soc/mediatek,usb-wakeup.h>
+#include <linux/kernel.h>
+#include <linux/mfd/syscon.h>
+#include <linux/module.h>
+#include <linux/of.h>
+#include <linux/of_platform.h>
+#include <linux/regmap.h>
+#include <linux/slab.h>
+#include <linux/soc/mediatek/usb-wakeup.h>
+
+/* mt8173, mt8176 etc */
+#define PERI_WK_CTRL1	0x4
+#define WC1_IS_C(x)	(((x) & 0xf) << 26) /* cycle debounce */
+#define WC1_IS_EN	BIT(25)
+#define WC1_IS_P	BIT(6)  /* polarity for ip sleep */
+
+/* mt2712 etc */
+#define PERI_SSUSB_SPM_CTRL	0x0
+#define SSC_LINE_STATE_CHG	GENMASK(11, 8)
+#define SSC_LINE_STATE_EN	GENMASK(6, 5)
+#define SSC_IP_SLEEP_EN	BIT(4)
+#define SSC_SPM_INT_EN		BIT(1)
+
+enum mtk_uwk_vers {
+	MTK_UWK_V1 = 1,
+	MTK_UWK_V2,
+};
+
+struct mtk_uwk_pdata {
+	enum mtk_uwk_vers vers;
+};
+
+/**
+ * @reg_base: register offset within a syscon @wkc (e.g. pericfg module)
+ * @type: the types of wakeup, such as IP-SLEEP, LINE-STATE etc
+ */
+struct mtk_uwk_instance {
+	struct mtu_wakeup uwk;
+	u32 reg_base;
+	u32 reg_len;
+	u32 type;
+};
+
+struct mtk_uwk {
+	struct device *dev;
+	struct regmap *wkc;
+	const struct mtk_uwk_pdata *data;
+	struct mtk_uwk_instance **inst;
+	int num_inst;
+};
+
+static LIST_HEAD(of_uwk_providers);
+static DEFINE_MUTEX(of_uwk_mutex);
+
+static struct mtu_wakeup_provider *of_uwk_provider_add(struct device *dev,
+		struct mtu_wakeup *(*of_xlate)(struct device *dev,
+		struct of_phandle_args *args))
+{
+	struct mtu_wakeup_provider *provider;
+
+	provider = kzalloc(sizeof(*provider), GFP_KERNEL);
+	if (!provider)
+		return ERR_PTR(-ENOMEM);
+
+	provider->dev = dev;
+	provider->of_node = of_node_get(dev->of_node);
+	provider->of_xlate = of_xlate;
+
+	mutex_lock(&of_uwk_mutex);
+	list_add_tail(&provider->list, &of_uwk_providers);
+	mutex_unlock(&of_uwk_mutex);
+
+	return provider;
+}
+
+static void of_uwk_provider_del(struct device_node *np)
+{
+	struct mtu_wakeup_provider *provider;
+
+	mutex_lock(&of_uwk_mutex);
+	list_for_each_entry(provider, &of_uwk_providers, list) {
+		if (provider->of_node == np) {
+			list_del(&provider->list);
+			of_node_put(provider->of_node);
+			kfree(provider);
+			break;
+		}
+	}
+	mutex_unlock(&of_uwk_mutex);
+}
+
+static struct mtu_wakeup *of_uwk_get_from_provider(
+		struct of_phandle_args *args)
+{
+	struct mtu_wakeup_provider *provider;
+	struct device_node *child_np;
+	struct mtu_wakeup *uwk;
+
+	mutex_lock(&of_uwk_mutex);
+	list_for_each_entry(provider, &of_uwk_providers, list) {
+		for_each_child_of_node(provider->of_node, child_np) {
+			if (child_np == args->np) {
+				uwk = provider->of_xlate(provider->dev, args);
+				mutex_unlock(&of_uwk_mutex);
+				return uwk;
+			}
+		}
+	}
+	mutex_unlock(&of_uwk_mutex);
+
+	return ERR_PTR(-EPROBE_DEFER);
+}
+
+static struct mtu_wakeup *of_uwk_get(struct device_node *np, int index)
+{
+	struct mtu_wakeup *uwk = NULL;
+	struct of_phandle_args args;
+	int ret;
+
+	ret = of_parse_phandle_with_args(np, "mediatek,uwks",
+				"#mediatek,uwk-cells", index, &args);
+	if (ret)
+		return ERR_PTR(-ENODEV);
+
+	if (!of_device_is_available(args.np)) {
+		dev_warn(uwk->parent, "Requested uwk is disabled\n");
+		uwk = ERR_PTR(-ENODEV);
+		goto put_node;
+	}
+
+	uwk = of_uwk_get_from_provider(&args);
+
+put_node:
+	of_node_put(args.np);
+	return uwk;
+}
+
+static void devm_uwk_release(struct device *dev, void *res)
+{
+	struct mtu_wakeup *uwk = *(struct mtu_wakeup **)res;
+
+	if (IS_ERR_OR_NULL(uwk))
+		return;
+
+	module_put(uwk->ops->owner);
+	put_device(uwk->parent);
+}
+
+struct mtu_wakeup *devm_of_uwk_get_by_index(
+		struct device *dev, struct device_node *np, int index)
+{
+	struct mtu_wakeup **ptr, *uwk;
+
+	ptr = devres_alloc(devm_uwk_release, sizeof(*ptr), GFP_KERNEL);
+	if (!ptr)
+		return ERR_PTR(-ENOMEM);
+
+	uwk = of_uwk_get(np, index);
+	if (IS_ERR(uwk)) {
+		devres_free(ptr);
+		return uwk;
+	}
+
+	if (!try_module_get(uwk->ops->owner)) {
+		devres_free(ptr);
+		return ERR_PTR(-EPROBE_DEFER);
+	}
+
+	get_device(uwk->parent);
+
+	*ptr = uwk;
+	devres_add(dev, ptr);
+
+	return uwk;
+}
+EXPORT_SYMBOL_GPL(devm_of_uwk_get_by_index);
+
+int mtu_wakeup_enable(struct mtu_wakeup *uwk)
+{
+	int ret = 0;
+
+	if (!uwk)
+		return 0;
+
+	mutex_lock(&uwk->mutex);
+	if (uwk->count == 0 && uwk->ops->enable) {
+		ret = uwk->ops->enable(uwk);
+		if (ret) {
+			dev_err(uwk->parent, "uwk enable failed(%d)\n", ret);
+			goto out;
+		}
+	}
+	++uwk->count;
+
+out:
+	mutex_unlock(&uwk->mutex);
+	return ret;
+}
+EXPORT_SYMBOL_GPL(mtu_wakeup_enable);
+
+int mtu_wakeup_disable(struct mtu_wakeup *uwk)
+{
+	int ret = 0;
+
+	if (!uwk)
+		return 0;
+
+	mutex_lock(&uwk->mutex);
+	if (uwk->count == 1 && uwk->ops->disable) {
+		ret =  uwk->ops->disable(uwk);
+		if (ret) {
+			dev_err(uwk->parent, "uwk disable failed(%d)\n", ret);
+			goto out;
+		}
+	}
+	--uwk->count;
+
+out:
+	mutex_unlock(&uwk->mutex);
+	return ret;
+}
+EXPORT_SYMBOL_GPL(mtu_wakeup_disable);
+
+static struct mtk_uwk_instance *to_mwk_inst(struct mtu_wakeup *uwk)
+{
+	return uwk ? container_of(uwk, struct mtk_uwk_instance, uwk) : NULL;
+}
+
+static int mwk_v1_enable(struct mtk_uwk *mwk, struct mtk_uwk_instance *inst)
+{
+	struct regmap *wkc = mwk->wkc;
+	u32 val;
+
+	/* Only IP-SLEEP is supported */
+	if (inst->type != MTU_WK_IP_SLEEP)
+		return 0;
+
+	regmap_read(wkc, PERI_WK_CTRL1, &val);
+	val &= ~(WC1_IS_P | WC1_IS_C(0xf));
+	val |= WC1_IS_EN | WC1_IS_C(0x8);
+	regmap_write(wkc, PERI_WK_CTRL1, val);
+	regmap_read(wkc, PERI_WK_CTRL1, &val);
+	dev_dbg(mwk->dev, "%s: WK_CTRL1=%#x, type=%d\n",
+		__func__, val, inst->type);
+
+	return 0;
+}
+
+static int mwk_v1_disable(struct mtk_uwk *mwk, struct mtk_uwk_instance *inst)
+{
+	if (inst->type == MTU_WK_IP_SLEEP)
+		regmap_update_bits(mwk->wkc, PERI_WK_CTRL1, WC1_IS_EN, 0);
+
+	return 0;
+}
+
+static int mwk_v2_enable(struct mtk_uwk *mwk, struct mtk_uwk_instance *inst)
+{
+	struct regmap *wkc = mwk->wkc;
+	u32 rbase = inst->reg_base;
+	u32 val;
+
+	regmap_read(wkc, rbase + PERI_SSUSB_SPM_CTRL, &val);
+	switch (inst->type) {
+	case MTU_WK_IP_SLEEP:
+		val |= SSC_IP_SLEEP_EN;
+		break;
+	case MTU_WK_LINE_STATE:
+		val |= SSC_LINE_STATE_EN | SSC_LINE_STATE_CHG;
+		break;
+	default:
+		/* checked by xlate, ignore the error */
+		break;
+	}
+	val |= SSC_SPM_INT_EN;
+	regmap_write(wkc, rbase + PERI_SSUSB_SPM_CTRL, val);
+	regmap_read(wkc, rbase + PERI_SSUSB_SPM_CTRL, &val);
+	dev_dbg(mwk->dev, "%s: CTRL=%#x, type=%d\n",
+		__func__, val, inst->type);
+
+	return 0;
+}
+
+static int mwk_v2_disable(struct mtk_uwk *mwk, struct mtk_uwk_instance *inst)
+{
+	struct regmap *wkc = mwk->wkc;
+	u32 rbase = inst->reg_base;
+	u32 val;
+
+	regmap_read(wkc, rbase + PERI_SSUSB_SPM_CTRL, &val);
+	switch (inst->type) {
+	case MTU_WK_IP_SLEEP:
+		val &= ~SSC_IP_SLEEP_EN;
+		break;
+	case MTU_WK_LINE_STATE:
+		val &= ~(SSC_LINE_STATE_EN | SSC_LINE_STATE_CHG);
+		break;
+	default:
+		break;
+	}
+	val &= ~SSC_SPM_INT_EN;
+	regmap_write(wkc, rbase + PERI_SSUSB_SPM_CTRL, val);
+	dev_dbg(mwk->dev, "%s: type=%d\n", __func__, inst->type);
+
+	return 0;
+}
+
+static int mwk_enable(struct mtu_wakeup *uwk)
+{
+	struct mtk_uwk_instance *inst = to_mwk_inst(uwk);
+	struct mtk_uwk *mwk = dev_get_drvdata(uwk->parent);
+	int ret = 0;
+
+	switch (mwk->data->vers) {
+	case MTK_UWK_V1:
+		ret = mwk_v1_enable(mwk, inst);
+		break;
+	case MTK_UWK_V2:
+		ret = mwk_v2_enable(mwk, inst);
+		break;
+	default:
+		break;
+	}
+	return ret;
+}
+
+static int mwk_disable(struct mtu_wakeup *uwk)
+{
+	struct mtk_uwk_instance *inst = to_mwk_inst(uwk);
+	struct mtk_uwk *mwk = dev_get_drvdata(uwk->parent);
+	int ret = 0;
+
+	switch (mwk->data->vers) {
+	case MTK_UWK_V1:
+		ret = mwk_v1_disable(mwk, inst);
+		break;
+	case MTK_UWK_V2:
+		ret = mwk_v2_disable(mwk, inst);
+		break;
+	default:
+		break;
+	}
+	return ret;
+}
+
+static struct mtk_uwk_instance *mwk_inst_create(struct device *dev,
+		struct device_node *np,
+		const struct mtu_wakeup_ops *ops)
+{
+	struct mtk_uwk_instance *inst;
+	struct mtu_wakeup *uwk;
+	u32 buf[2];
+	int ret;
+
+	inst = devm_kzalloc(dev, sizeof(*inst), GFP_KERNEL);
+	if (!inst)
+		return ERR_PTR(-ENOMEM);
+
+	ret = of_property_read_u32_array(np, "reg", buf, ARRAY_SIZE(buf));
+	if (ret) {
+		dev_err(dev, "fail to read reg\n");
+		return ERR_PTR(ret);
+	}
+
+	inst->reg_base = buf[0];
+	inst->reg_len = buf[1];
+	uwk = &inst->uwk;
+	uwk->node = np;
+	uwk->ops = ops;
+	uwk->parent = dev;
+	mutex_init(&uwk->mutex);
+	dev_dbg(dev, "reg: %#x/%#x\n", inst->reg_base, inst->reg_len);
+
+	return inst;
+}
+
+static struct mtu_wakeup *mwk_xlate(struct device *dev,
+		struct of_phandle_args *args)
+{
+	struct mtk_uwk *mwk = dev_get_drvdata(dev);
+	struct mtk_uwk_instance *inst = NULL;
+	struct device_node *uwk_np = args->np;
+	int index;
+
+	if (args->args_count != 1) {
+		dev_err(dev, "invalid number of cells in uwk property\n");
+		return ERR_PTR(-EINVAL);
+	}
+
+	for (index = 0; index < mwk->num_inst; index++)
+		if (uwk_np == mwk->inst[index]->uwk.node) {
+			inst = mwk->inst[index];
+			break;
+		}
+
+	if (!inst) {
+		dev_err(dev, "failed to find appropriate uwk\n");
+		return ERR_PTR(-EINVAL);
+	}
+
+	inst->type = args->args[0];
+	if (!(inst->type == MTU_WK_IP_SLEEP ||
+	      inst->type == MTU_WK_LINE_STATE)) {
+		dev_err(dev, "unsupported uwk type=%d\n", inst->type);
+		return ERR_PTR(-EINVAL);
+	}
+
+	return &inst->uwk;
+}
+
+static const struct mtu_wakeup_ops mwk_ops = {
+	.enable = mwk_enable,
+	.disable = mwk_disable,
+	.owner = THIS_MODULE,
+};
+
+static const struct mtk_uwk_pdata mwk_v1_pdata = {
+	.vers = MTK_UWK_V1,
+};
+
+static const struct mtk_uwk_pdata mwk_v2_pdata = {
+	.vers = MTK_UWK_V2,
+};
+
+static const struct of_device_id mwk_id_table[] = {
+	{ .compatible = "mediatek,usb-wk-v1", .data = &mwk_v1_pdata },
+	{ .compatible = "mediatek,usb-wk-v2", .data = &mwk_v2_pdata },
+	{ },
+};
+MODULE_DEVICE_TABLE(of, mwk_id_table);
+
+static int mtk_uwk_probe(struct platform_device *pdev)
+{
+	struct device *dev = &pdev->dev;
+	struct device_node *np = dev->of_node;
+	struct device_node *child_np;
+	struct mtu_wakeup_provider *provider;
+	struct mtk_uwk *mwk;
+	int index;
+	int ret;
+
+	mwk = devm_kzalloc(dev, sizeof(*mwk), GFP_KERNEL);
+	if (!mwk)
+		return -ENOMEM;
+
+	mwk->data = of_device_get_match_data(dev);
+	if (!mwk->data)
+		return -EINVAL;
+
+	mwk->num_inst = of_get_child_count(np);
+	mwk->inst = devm_kcalloc(dev, mwk->num_inst,
+				  sizeof(*mwk->inst), GFP_KERNEL);
+	if (!mwk->inst)
+		return -ENOMEM;
+
+	mwk->dev = dev;
+	platform_set_drvdata(pdev, mwk);
+
+	mwk->wkc = syscon_regmap_lookup_by_phandle(np, "mediatek,wkc");
+	if (IS_ERR(mwk->wkc)) {
+		dev_err(dev, "fail to get mediatek,wkc syscon\n");
+		return PTR_ERR(mwk->wkc);
+	}
+
+	index = 0;
+	for_each_child_of_node(np, child_np) {
+		struct mtk_uwk_instance *inst;
+
+		inst = mwk_inst_create(dev, child_np, &mwk_ops);
+		if (IS_ERR(inst)) {
+			dev_err(dev, "failed to create mwk instance\n");
+			ret = PTR_ERR(inst);
+			goto put_child;
+		}
+
+		mwk->inst[index] = inst;
+		index++;
+	}
+
+	provider = of_uwk_provider_add(dev, mwk_xlate);
+
+	return PTR_ERR_OR_ZERO(provider);
+
+put_child:
+	of_node_put(child_np);
+	return ret;
+}
+
+static int mtk_uwk_remove(struct platform_device *pdev)
+{
+	struct device *dev = &pdev->dev;
+	struct device_node *np = dev->of_node;
+
+	of_uwk_provider_del(np);
+	return 0;
+}
+
+static struct platform_driver mtk_uwk_drv = {
+	.probe = mtk_uwk_probe,
+	.remove = mtk_uwk_remove,
+	.driver = {
+		.name = "mtk_uwk",
+		.owner = THIS_MODULE,
+		.of_match_table = mwk_id_table,
+	},
+};
+
+module_platform_driver(mtk_uwk_drv);
+MODULE_AUTHOR("Chunfeng Yun <chunfeng.yun-NuS5LvNUpcJWk0Htik3J/w@public.gmane.org>");
+MODULE_DESCRIPTION("MediaTek USB Wakeup driver");
+MODULE_LICENSE("GPL v2");
diff --git a/include/dt-bindings/soc/mediatek,usb-wakeup.h b/include/dt-bindings/soc/mediatek,usb-wakeup.h
new file mode 100644
index 0000000..2461795
--- /dev/null
+++ b/include/dt-bindings/soc/mediatek,usb-wakeup.h
@@ -0,0 +1,15 @@
+/*
+ * Copyright (c) 2017 MediaTek Inc.
+ * Author: Chunfeng Yun <chunfeng.yun-NuS5LvNUpcJWk0Htik3J/w@public.gmane.org>
+ *
+ * SPDX-License-Identifier: GPL-2.0
+ *
+ */
+
+#ifndef __DT_BINDINGS_MTK_USB_WK_H__
+#define __DT_BINDINGS_MTK_USB_WK_H__
+
+#define MTU_WK_IP_SLEEP	1
+#define MTU_WK_LINE_STATE	2
+
+#endif
diff --git a/include/linux/soc/mediatek/usb-wakeup.h b/include/linux/soc/mediatek/usb-wakeup.h
new file mode 100644
index 0000000..5697367
--- /dev/null
+++ b/include/linux/soc/mediatek/usb-wakeup.h
@@ -0,0 +1,88 @@
+/*
+ * Copyright (c) 2017 MediaTek Inc.
+ * Author: Chunfeng Yun <chunfeng.yun-NuS5LvNUpcJWk0Htik3J/w@public.gmane.org>
+ *
+ * SPDX-License-Identifier: GPL-2.0
+ *
+ */
+
+#ifndef __MTK_USB_WAKEUP_H__
+#define __MTK_USB_WAKEUP_H__
+
+#include <linux/device.h>
+#include <linux/err.h>
+#include <linux/mutex.h>
+#include <linux/of.h>
+
+struct mtu_wakeup;
+
+/**
+ * struct mtu_wakeup_ops - set of function pointers for performing
+ *    mtu_wakeup operations
+ * @enable: enable a type of usb wakeup when system suspend
+ * @disable: disable a type of usb wakeup when system resume
+ * @owner: the module owner using the ops
+ */
+struct mtu_wakeup_ops {
+	int	(*enable)(struct mtu_wakeup *uwk);
+	int	(*disable)(struct mtu_wakeup *uwk);
+	struct module *owner;
+};
+
+/**
+ * struct mtu_wakeup - represents the MediaTek USB wakeup device
+ * @parent: the parent device of the mtu_wakeup
+ * @node: associated device tree node
+ * @ops: function pointers for performing mtu_wakeup operations
+ * @mutex: mutex to protect @ops
+ * @count: used to protect when the mtu_wakeup is used by multiple consumers
+ */
+struct mtu_wakeup {
+	struct device *parent;
+	struct device_node *node;
+	const struct mtu_wakeup_ops *ops;
+	struct mutex mutex;
+	int count;
+};
+
+/**
+ * struct mtu_wakeup_provider - represents the mtu_wakeup provider
+ * @dev: the parent device of the mtu_wakeup
+ * @list: to maintain a linked list of mtu_wakeup providers
+ * @of_node: associated device tree node
+ * @of_xlate: function pointer to obtain mtu_wakeup instance from
+ *	its tree node
+ */
+struct mtu_wakeup_provider {
+	struct device *dev;
+	struct list_head list;
+	struct device_node *of_node;
+	struct mtu_wakeup *(*of_xlate)(struct device *dev,
+		struct of_phandle_args *args);
+};
+
+#if IS_ENABLED(CONFIG_MTK_UWK)
+struct mtu_wakeup *devm_of_uwk_get_by_index(
+	struct device *dev, struct device_node *np, int index);
+int mtu_wakeup_enable(struct mtu_wakeup *uwk);
+int mtu_wakeup_disable(struct mtu_wakeup *uwk);
+
+#else
+struct mtu_wakeup *devm_of_uwk_get_by_index(
+	struct device *dev, struct device_node *np, int index)
+{
+	return ERR_PTR(-ENODEV);
+}
+
+int mtu_wakeup_enable(struct mtu_wakeup *uwk)
+{
+	return uwk ? -ENODEV : 0;
+}
+
+int mtu_wakeup_disable(struct mtu_wakeup *uwk)
+{
+	return uwk ? -ENODEV : 0;
+}
+#endif
+
+#endif	/* __MTK_USB_WAKEUP_H__ */
-- 
1.9.1

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

WARNING: multiple messages have this Message-ID (diff)
From: Chunfeng Yun <chunfeng.yun@mediatek.com>
To: Rob Herring <robh+dt@kernel.org>,
	Felipe Balbi <felipe.balbi@linux.intel.com>,
	Matthias Brugger <matthias.bgg@gmail.com>,
	Mathias Nyman <mathias.nyman@intel.com>
Cc: Mark Rutland <mark.rutland@arm.com>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	Catalin Marinas <catalin.marinas@arm.com>,
	Will Deacon <will.deacon@arm.com>,
	Chunfeng Yun <chunfeng.yun@mediatek.com>,
	Jean Delvare <jdelvare@suse.de>,
	Sean Wang <sean.wang@mediatek.com>,
	devicetree@vger.kernel.org, linux-kernel@vger.kernel.org,
	linux-usb@vger.kernel.org, linux-arm-kernel@lists.infradead.org,
	linux-mediatek@lists.infradead.org
Subject: [1/7] soc: mediatek: Add USB wakeup driver
Date: Sat, 9 Dec 2017 16:45:30 +0800	[thread overview]
Message-ID: <1512809136-2779-2-git-send-email-chunfeng.yun@mediatek.com> (raw)

This driver is used to support usb wakeup which is controlled by
the glue layer between SSUSB and SPM. Usually the glue layer is put
into a system controller, such as pericfg module, which is
represented by a syscon node in DTS.
Due to the glue layer may vary on different SoCs, it's useful to
extract a separated driver to simplify usb controller drivers.

Signed-off-by: Chunfeng Yun <chunfeng.yun@mediatek.com>
---
 drivers/soc/mediatek/Kconfig                  |   8 +
 drivers/soc/mediatek/Makefile                 |   1 +
 drivers/soc/mediatek/mtk-usb-wakeup.c         | 519 ++++++++++++++++++++++++++
 include/dt-bindings/soc/mediatek,usb-wakeup.h |  15 +
 include/linux/soc/mediatek/usb-wakeup.h       |  88 +++++
 5 files changed, 631 insertions(+)
 create mode 100644 drivers/soc/mediatek/mtk-usb-wakeup.c
 create mode 100644 include/dt-bindings/soc/mediatek,usb-wakeup.h
 create mode 100644 include/linux/soc/mediatek/usb-wakeup.h

diff --git a/drivers/soc/mediatek/Kconfig b/drivers/soc/mediatek/Kconfig
index a7d0667..30cd226 100644
--- a/drivers/soc/mediatek/Kconfig
+++ b/drivers/soc/mediatek/Kconfig
@@ -31,4 +31,12 @@ config MTK_SCPSYS
 	  Say yes here to add support for the MediaTek SCPSYS power domain
 	  driver.
 
+config MTK_UWK
+	bool "MediaTek USB Wakeup Support"
+	select REGMAP
+	help
+	  Say yes here to add support for the MediaTek SSUSB-SPM glue layer
+	  which supports some different type of USB wakeup, such as IP-SLEEP,
+	  LINESTATE, IDDIG etc, and it can support multi SSUSB controllers.
+
 endmenu
diff --git a/drivers/soc/mediatek/Makefile b/drivers/soc/mediatek/Makefile
index 12998b0..66fbb54f 100644
--- a/drivers/soc/mediatek/Makefile
+++ b/drivers/soc/mediatek/Makefile
@@ -1,3 +1,4 @@
 obj-$(CONFIG_MTK_INFRACFG) += mtk-infracfg.o
 obj-$(CONFIG_MTK_PMIC_WRAP) += mtk-pmic-wrap.o
 obj-$(CONFIG_MTK_SCPSYS) += mtk-scpsys.o
+obj-$(CONFIG_MTK_UWK) += mtk-usb-wakeup.o
diff --git a/drivers/soc/mediatek/mtk-usb-wakeup.c b/drivers/soc/mediatek/mtk-usb-wakeup.c
new file mode 100644
index 0000000..16539a6
--- /dev/null
+++ b/drivers/soc/mediatek/mtk-usb-wakeup.c
@@ -0,0 +1,519 @@
+/*
+ * Copyright (c) 2017 MediaTek Inc.
+ * Author: Chunfeng Yun <chunfeng.yun@mediatek.com>
+ *
+ * SPDX-License-Identifier: GPL-2.0
+ *
+ */
+
+#include <dt-bindings/soc/mediatek,usb-wakeup.h>
+#include <linux/kernel.h>
+#include <linux/mfd/syscon.h>
+#include <linux/module.h>
+#include <linux/of.h>
+#include <linux/of_platform.h>
+#include <linux/regmap.h>
+#include <linux/slab.h>
+#include <linux/soc/mediatek/usb-wakeup.h>
+
+/* mt8173, mt8176 etc */
+#define PERI_WK_CTRL1	0x4
+#define WC1_IS_C(x)	(((x) & 0xf) << 26) /* cycle debounce */
+#define WC1_IS_EN	BIT(25)
+#define WC1_IS_P	BIT(6)  /* polarity for ip sleep */
+
+/* mt2712 etc */
+#define PERI_SSUSB_SPM_CTRL	0x0
+#define SSC_LINE_STATE_CHG	GENMASK(11, 8)
+#define SSC_LINE_STATE_EN	GENMASK(6, 5)
+#define SSC_IP_SLEEP_EN	BIT(4)
+#define SSC_SPM_INT_EN		BIT(1)
+
+enum mtk_uwk_vers {
+	MTK_UWK_V1 = 1,
+	MTK_UWK_V2,
+};
+
+struct mtk_uwk_pdata {
+	enum mtk_uwk_vers vers;
+};
+
+/**
+ * @reg_base: register offset within a syscon @wkc (e.g. pericfg module)
+ * @type: the types of wakeup, such as IP-SLEEP, LINE-STATE etc
+ */
+struct mtk_uwk_instance {
+	struct mtu_wakeup uwk;
+	u32 reg_base;
+	u32 reg_len;
+	u32 type;
+};
+
+struct mtk_uwk {
+	struct device *dev;
+	struct regmap *wkc;
+	const struct mtk_uwk_pdata *data;
+	struct mtk_uwk_instance **inst;
+	int num_inst;
+};
+
+static LIST_HEAD(of_uwk_providers);
+static DEFINE_MUTEX(of_uwk_mutex);
+
+static struct mtu_wakeup_provider *of_uwk_provider_add(struct device *dev,
+		struct mtu_wakeup *(*of_xlate)(struct device *dev,
+		struct of_phandle_args *args))
+{
+	struct mtu_wakeup_provider *provider;
+
+	provider = kzalloc(sizeof(*provider), GFP_KERNEL);
+	if (!provider)
+		return ERR_PTR(-ENOMEM);
+
+	provider->dev = dev;
+	provider->of_node = of_node_get(dev->of_node);
+	provider->of_xlate = of_xlate;
+
+	mutex_lock(&of_uwk_mutex);
+	list_add_tail(&provider->list, &of_uwk_providers);
+	mutex_unlock(&of_uwk_mutex);
+
+	return provider;
+}
+
+static void of_uwk_provider_del(struct device_node *np)
+{
+	struct mtu_wakeup_provider *provider;
+
+	mutex_lock(&of_uwk_mutex);
+	list_for_each_entry(provider, &of_uwk_providers, list) {
+		if (provider->of_node == np) {
+			list_del(&provider->list);
+			of_node_put(provider->of_node);
+			kfree(provider);
+			break;
+		}
+	}
+	mutex_unlock(&of_uwk_mutex);
+}
+
+static struct mtu_wakeup *of_uwk_get_from_provider(
+		struct of_phandle_args *args)
+{
+	struct mtu_wakeup_provider *provider;
+	struct device_node *child_np;
+	struct mtu_wakeup *uwk;
+
+	mutex_lock(&of_uwk_mutex);
+	list_for_each_entry(provider, &of_uwk_providers, list) {
+		for_each_child_of_node(provider->of_node, child_np) {
+			if (child_np == args->np) {
+				uwk = provider->of_xlate(provider->dev, args);
+				mutex_unlock(&of_uwk_mutex);
+				return uwk;
+			}
+		}
+	}
+	mutex_unlock(&of_uwk_mutex);
+
+	return ERR_PTR(-EPROBE_DEFER);
+}
+
+static struct mtu_wakeup *of_uwk_get(struct device_node *np, int index)
+{
+	struct mtu_wakeup *uwk = NULL;
+	struct of_phandle_args args;
+	int ret;
+
+	ret = of_parse_phandle_with_args(np, "mediatek,uwks",
+				"#mediatek,uwk-cells", index, &args);
+	if (ret)
+		return ERR_PTR(-ENODEV);
+
+	if (!of_device_is_available(args.np)) {
+		dev_warn(uwk->parent, "Requested uwk is disabled\n");
+		uwk = ERR_PTR(-ENODEV);
+		goto put_node;
+	}
+
+	uwk = of_uwk_get_from_provider(&args);
+
+put_node:
+	of_node_put(args.np);
+	return uwk;
+}
+
+static void devm_uwk_release(struct device *dev, void *res)
+{
+	struct mtu_wakeup *uwk = *(struct mtu_wakeup **)res;
+
+	if (IS_ERR_OR_NULL(uwk))
+		return;
+
+	module_put(uwk->ops->owner);
+	put_device(uwk->parent);
+}
+
+struct mtu_wakeup *devm_of_uwk_get_by_index(
+		struct device *dev, struct device_node *np, int index)
+{
+	struct mtu_wakeup **ptr, *uwk;
+
+	ptr = devres_alloc(devm_uwk_release, sizeof(*ptr), GFP_KERNEL);
+	if (!ptr)
+		return ERR_PTR(-ENOMEM);
+
+	uwk = of_uwk_get(np, index);
+	if (IS_ERR(uwk)) {
+		devres_free(ptr);
+		return uwk;
+	}
+
+	if (!try_module_get(uwk->ops->owner)) {
+		devres_free(ptr);
+		return ERR_PTR(-EPROBE_DEFER);
+	}
+
+	get_device(uwk->parent);
+
+	*ptr = uwk;
+	devres_add(dev, ptr);
+
+	return uwk;
+}
+EXPORT_SYMBOL_GPL(devm_of_uwk_get_by_index);
+
+int mtu_wakeup_enable(struct mtu_wakeup *uwk)
+{
+	int ret = 0;
+
+	if (!uwk)
+		return 0;
+
+	mutex_lock(&uwk->mutex);
+	if (uwk->count == 0 && uwk->ops->enable) {
+		ret = uwk->ops->enable(uwk);
+		if (ret) {
+			dev_err(uwk->parent, "uwk enable failed(%d)\n", ret);
+			goto out;
+		}
+	}
+	++uwk->count;
+
+out:
+	mutex_unlock(&uwk->mutex);
+	return ret;
+}
+EXPORT_SYMBOL_GPL(mtu_wakeup_enable);
+
+int mtu_wakeup_disable(struct mtu_wakeup *uwk)
+{
+	int ret = 0;
+
+	if (!uwk)
+		return 0;
+
+	mutex_lock(&uwk->mutex);
+	if (uwk->count == 1 && uwk->ops->disable) {
+		ret =  uwk->ops->disable(uwk);
+		if (ret) {
+			dev_err(uwk->parent, "uwk disable failed(%d)\n", ret);
+			goto out;
+		}
+	}
+	--uwk->count;
+
+out:
+	mutex_unlock(&uwk->mutex);
+	return ret;
+}
+EXPORT_SYMBOL_GPL(mtu_wakeup_disable);
+
+static struct mtk_uwk_instance *to_mwk_inst(struct mtu_wakeup *uwk)
+{
+	return uwk ? container_of(uwk, struct mtk_uwk_instance, uwk) : NULL;
+}
+
+static int mwk_v1_enable(struct mtk_uwk *mwk, struct mtk_uwk_instance *inst)
+{
+	struct regmap *wkc = mwk->wkc;
+	u32 val;
+
+	/* Only IP-SLEEP is supported */
+	if (inst->type != MTU_WK_IP_SLEEP)
+		return 0;
+
+	regmap_read(wkc, PERI_WK_CTRL1, &val);
+	val &= ~(WC1_IS_P | WC1_IS_C(0xf));
+	val |= WC1_IS_EN | WC1_IS_C(0x8);
+	regmap_write(wkc, PERI_WK_CTRL1, val);
+	regmap_read(wkc, PERI_WK_CTRL1, &val);
+	dev_dbg(mwk->dev, "%s: WK_CTRL1=%#x, type=%d\n",
+		__func__, val, inst->type);
+
+	return 0;
+}
+
+static int mwk_v1_disable(struct mtk_uwk *mwk, struct mtk_uwk_instance *inst)
+{
+	if (inst->type == MTU_WK_IP_SLEEP)
+		regmap_update_bits(mwk->wkc, PERI_WK_CTRL1, WC1_IS_EN, 0);
+
+	return 0;
+}
+
+static int mwk_v2_enable(struct mtk_uwk *mwk, struct mtk_uwk_instance *inst)
+{
+	struct regmap *wkc = mwk->wkc;
+	u32 rbase = inst->reg_base;
+	u32 val;
+
+	regmap_read(wkc, rbase + PERI_SSUSB_SPM_CTRL, &val);
+	switch (inst->type) {
+	case MTU_WK_IP_SLEEP:
+		val |= SSC_IP_SLEEP_EN;
+		break;
+	case MTU_WK_LINE_STATE:
+		val |= SSC_LINE_STATE_EN | SSC_LINE_STATE_CHG;
+		break;
+	default:
+		/* checked by xlate, ignore the error */
+		break;
+	}
+	val |= SSC_SPM_INT_EN;
+	regmap_write(wkc, rbase + PERI_SSUSB_SPM_CTRL, val);
+	regmap_read(wkc, rbase + PERI_SSUSB_SPM_CTRL, &val);
+	dev_dbg(mwk->dev, "%s: CTRL=%#x, type=%d\n",
+		__func__, val, inst->type);
+
+	return 0;
+}
+
+static int mwk_v2_disable(struct mtk_uwk *mwk, struct mtk_uwk_instance *inst)
+{
+	struct regmap *wkc = mwk->wkc;
+	u32 rbase = inst->reg_base;
+	u32 val;
+
+	regmap_read(wkc, rbase + PERI_SSUSB_SPM_CTRL, &val);
+	switch (inst->type) {
+	case MTU_WK_IP_SLEEP:
+		val &= ~SSC_IP_SLEEP_EN;
+		break;
+	case MTU_WK_LINE_STATE:
+		val &= ~(SSC_LINE_STATE_EN | SSC_LINE_STATE_CHG);
+		break;
+	default:
+		break;
+	}
+	val &= ~SSC_SPM_INT_EN;
+	regmap_write(wkc, rbase + PERI_SSUSB_SPM_CTRL, val);
+	dev_dbg(mwk->dev, "%s: type=%d\n", __func__, inst->type);
+
+	return 0;
+}
+
+static int mwk_enable(struct mtu_wakeup *uwk)
+{
+	struct mtk_uwk_instance *inst = to_mwk_inst(uwk);
+	struct mtk_uwk *mwk = dev_get_drvdata(uwk->parent);
+	int ret = 0;
+
+	switch (mwk->data->vers) {
+	case MTK_UWK_V1:
+		ret = mwk_v1_enable(mwk, inst);
+		break;
+	case MTK_UWK_V2:
+		ret = mwk_v2_enable(mwk, inst);
+		break;
+	default:
+		break;
+	}
+	return ret;
+}
+
+static int mwk_disable(struct mtu_wakeup *uwk)
+{
+	struct mtk_uwk_instance *inst = to_mwk_inst(uwk);
+	struct mtk_uwk *mwk = dev_get_drvdata(uwk->parent);
+	int ret = 0;
+
+	switch (mwk->data->vers) {
+	case MTK_UWK_V1:
+		ret = mwk_v1_disable(mwk, inst);
+		break;
+	case MTK_UWK_V2:
+		ret = mwk_v2_disable(mwk, inst);
+		break;
+	default:
+		break;
+	}
+	return ret;
+}
+
+static struct mtk_uwk_instance *mwk_inst_create(struct device *dev,
+		struct device_node *np,
+		const struct mtu_wakeup_ops *ops)
+{
+	struct mtk_uwk_instance *inst;
+	struct mtu_wakeup *uwk;
+	u32 buf[2];
+	int ret;
+
+	inst = devm_kzalloc(dev, sizeof(*inst), GFP_KERNEL);
+	if (!inst)
+		return ERR_PTR(-ENOMEM);
+
+	ret = of_property_read_u32_array(np, "reg", buf, ARRAY_SIZE(buf));
+	if (ret) {
+		dev_err(dev, "fail to read reg\n");
+		return ERR_PTR(ret);
+	}
+
+	inst->reg_base = buf[0];
+	inst->reg_len = buf[1];
+	uwk = &inst->uwk;
+	uwk->node = np;
+	uwk->ops = ops;
+	uwk->parent = dev;
+	mutex_init(&uwk->mutex);
+	dev_dbg(dev, "reg: %#x/%#x\n", inst->reg_base, inst->reg_len);
+
+	return inst;
+}
+
+static struct mtu_wakeup *mwk_xlate(struct device *dev,
+		struct of_phandle_args *args)
+{
+	struct mtk_uwk *mwk = dev_get_drvdata(dev);
+	struct mtk_uwk_instance *inst = NULL;
+	struct device_node *uwk_np = args->np;
+	int index;
+
+	if (args->args_count != 1) {
+		dev_err(dev, "invalid number of cells in uwk property\n");
+		return ERR_PTR(-EINVAL);
+	}
+
+	for (index = 0; index < mwk->num_inst; index++)
+		if (uwk_np == mwk->inst[index]->uwk.node) {
+			inst = mwk->inst[index];
+			break;
+		}
+
+	if (!inst) {
+		dev_err(dev, "failed to find appropriate uwk\n");
+		return ERR_PTR(-EINVAL);
+	}
+
+	inst->type = args->args[0];
+	if (!(inst->type == MTU_WK_IP_SLEEP ||
+	      inst->type == MTU_WK_LINE_STATE)) {
+		dev_err(dev, "unsupported uwk type=%d\n", inst->type);
+		return ERR_PTR(-EINVAL);
+	}
+
+	return &inst->uwk;
+}
+
+static const struct mtu_wakeup_ops mwk_ops = {
+	.enable = mwk_enable,
+	.disable = mwk_disable,
+	.owner = THIS_MODULE,
+};
+
+static const struct mtk_uwk_pdata mwk_v1_pdata = {
+	.vers = MTK_UWK_V1,
+};
+
+static const struct mtk_uwk_pdata mwk_v2_pdata = {
+	.vers = MTK_UWK_V2,
+};
+
+static const struct of_device_id mwk_id_table[] = {
+	{ .compatible = "mediatek,usb-wk-v1", .data = &mwk_v1_pdata },
+	{ .compatible = "mediatek,usb-wk-v2", .data = &mwk_v2_pdata },
+	{ },
+};
+MODULE_DEVICE_TABLE(of, mwk_id_table);
+
+static int mtk_uwk_probe(struct platform_device *pdev)
+{
+	struct device *dev = &pdev->dev;
+	struct device_node *np = dev->of_node;
+	struct device_node *child_np;
+	struct mtu_wakeup_provider *provider;
+	struct mtk_uwk *mwk;
+	int index;
+	int ret;
+
+	mwk = devm_kzalloc(dev, sizeof(*mwk), GFP_KERNEL);
+	if (!mwk)
+		return -ENOMEM;
+
+	mwk->data = of_device_get_match_data(dev);
+	if (!mwk->data)
+		return -EINVAL;
+
+	mwk->num_inst = of_get_child_count(np);
+	mwk->inst = devm_kcalloc(dev, mwk->num_inst,
+				  sizeof(*mwk->inst), GFP_KERNEL);
+	if (!mwk->inst)
+		return -ENOMEM;
+
+	mwk->dev = dev;
+	platform_set_drvdata(pdev, mwk);
+
+	mwk->wkc = syscon_regmap_lookup_by_phandle(np, "mediatek,wkc");
+	if (IS_ERR(mwk->wkc)) {
+		dev_err(dev, "fail to get mediatek,wkc syscon\n");
+		return PTR_ERR(mwk->wkc);
+	}
+
+	index = 0;
+	for_each_child_of_node(np, child_np) {
+		struct mtk_uwk_instance *inst;
+
+		inst = mwk_inst_create(dev, child_np, &mwk_ops);
+		if (IS_ERR(inst)) {
+			dev_err(dev, "failed to create mwk instance\n");
+			ret = PTR_ERR(inst);
+			goto put_child;
+		}
+
+		mwk->inst[index] = inst;
+		index++;
+	}
+
+	provider = of_uwk_provider_add(dev, mwk_xlate);
+
+	return PTR_ERR_OR_ZERO(provider);
+
+put_child:
+	of_node_put(child_np);
+	return ret;
+}
+
+static int mtk_uwk_remove(struct platform_device *pdev)
+{
+	struct device *dev = &pdev->dev;
+	struct device_node *np = dev->of_node;
+
+	of_uwk_provider_del(np);
+	return 0;
+}
+
+static struct platform_driver mtk_uwk_drv = {
+	.probe = mtk_uwk_probe,
+	.remove = mtk_uwk_remove,
+	.driver = {
+		.name = "mtk_uwk",
+		.owner = THIS_MODULE,
+		.of_match_table = mwk_id_table,
+	},
+};
+
+module_platform_driver(mtk_uwk_drv);
+MODULE_AUTHOR("Chunfeng Yun <chunfeng.yun@mediatek.com>");
+MODULE_DESCRIPTION("MediaTek USB Wakeup driver");
+MODULE_LICENSE("GPL v2");
diff --git a/include/dt-bindings/soc/mediatek,usb-wakeup.h b/include/dt-bindings/soc/mediatek,usb-wakeup.h
new file mode 100644
index 0000000..2461795
--- /dev/null
+++ b/include/dt-bindings/soc/mediatek,usb-wakeup.h
@@ -0,0 +1,15 @@
+/*
+ * Copyright (c) 2017 MediaTek Inc.
+ * Author: Chunfeng Yun <chunfeng.yun@mediatek.com>
+ *
+ * SPDX-License-Identifier: GPL-2.0
+ *
+ */
+
+#ifndef __DT_BINDINGS_MTK_USB_WK_H__
+#define __DT_BINDINGS_MTK_USB_WK_H__
+
+#define MTU_WK_IP_SLEEP	1
+#define MTU_WK_LINE_STATE	2
+
+#endif
diff --git a/include/linux/soc/mediatek/usb-wakeup.h b/include/linux/soc/mediatek/usb-wakeup.h
new file mode 100644
index 0000000..5697367
--- /dev/null
+++ b/include/linux/soc/mediatek/usb-wakeup.h
@@ -0,0 +1,88 @@
+/*
+ * Copyright (c) 2017 MediaTek Inc.
+ * Author: Chunfeng Yun <chunfeng.yun@mediatek.com>
+ *
+ * SPDX-License-Identifier: GPL-2.0
+ *
+ */
+
+#ifndef __MTK_USB_WAKEUP_H__
+#define __MTK_USB_WAKEUP_H__
+
+#include <linux/device.h>
+#include <linux/err.h>
+#include <linux/mutex.h>
+#include <linux/of.h>
+
+struct mtu_wakeup;
+
+/**
+ * struct mtu_wakeup_ops - set of function pointers for performing
+ *    mtu_wakeup operations
+ * @enable: enable a type of usb wakeup when system suspend
+ * @disable: disable a type of usb wakeup when system resume
+ * @owner: the module owner using the ops
+ */
+struct mtu_wakeup_ops {
+	int	(*enable)(struct mtu_wakeup *uwk);
+	int	(*disable)(struct mtu_wakeup *uwk);
+	struct module *owner;
+};
+
+/**
+ * struct mtu_wakeup - represents the MediaTek USB wakeup device
+ * @parent: the parent device of the mtu_wakeup
+ * @node: associated device tree node
+ * @ops: function pointers for performing mtu_wakeup operations
+ * @mutex: mutex to protect @ops
+ * @count: used to protect when the mtu_wakeup is used by multiple consumers
+ */
+struct mtu_wakeup {
+	struct device *parent;
+	struct device_node *node;
+	const struct mtu_wakeup_ops *ops;
+	struct mutex mutex;
+	int count;
+};
+
+/**
+ * struct mtu_wakeup_provider - represents the mtu_wakeup provider
+ * @dev: the parent device of the mtu_wakeup
+ * @list: to maintain a linked list of mtu_wakeup providers
+ * @of_node: associated device tree node
+ * @of_xlate: function pointer to obtain mtu_wakeup instance from
+ *	its tree node
+ */
+struct mtu_wakeup_provider {
+	struct device *dev;
+	struct list_head list;
+	struct device_node *of_node;
+	struct mtu_wakeup *(*of_xlate)(struct device *dev,
+		struct of_phandle_args *args);
+};
+
+#if IS_ENABLED(CONFIG_MTK_UWK)
+struct mtu_wakeup *devm_of_uwk_get_by_index(
+	struct device *dev, struct device_node *np, int index);
+int mtu_wakeup_enable(struct mtu_wakeup *uwk);
+int mtu_wakeup_disable(struct mtu_wakeup *uwk);
+
+#else
+struct mtu_wakeup *devm_of_uwk_get_by_index(
+	struct device *dev, struct device_node *np, int index)
+{
+	return ERR_PTR(-ENODEV);
+}
+
+int mtu_wakeup_enable(struct mtu_wakeup *uwk)
+{
+	return uwk ? -ENODEV : 0;
+}
+
+int mtu_wakeup_disable(struct mtu_wakeup *uwk)
+{
+	return uwk ? -ENODEV : 0;
+}
+#endif
+
+#endif	/* __MTK_USB_WAKEUP_H__ */

WARNING: multiple messages have this Message-ID (diff)
From: chunfeng.yun@mediatek.com (Chunfeng Yun)
To: linux-arm-kernel@lists.infradead.org
Subject: [PATCH 1/7] soc: mediatek: Add USB wakeup driver
Date: Sat, 9 Dec 2017 16:45:30 +0800	[thread overview]
Message-ID: <1512809136-2779-2-git-send-email-chunfeng.yun@mediatek.com> (raw)
In-Reply-To: <1512809136-2779-1-git-send-email-chunfeng.yun@mediatek.com>

This driver is used to support usb wakeup which is controlled by
the glue layer between SSUSB and SPM. Usually the glue layer is put
into a system controller, such as pericfg module, which is
represented by a syscon node in DTS.
Due to the glue layer may vary on different SoCs, it's useful to
extract a separated driver to simplify usb controller drivers.

Signed-off-by: Chunfeng Yun <chunfeng.yun@mediatek.com>
---
 drivers/soc/mediatek/Kconfig                  |   8 +
 drivers/soc/mediatek/Makefile                 |   1 +
 drivers/soc/mediatek/mtk-usb-wakeup.c         | 519 ++++++++++++++++++++++++++
 include/dt-bindings/soc/mediatek,usb-wakeup.h |  15 +
 include/linux/soc/mediatek/usb-wakeup.h       |  88 +++++
 5 files changed, 631 insertions(+)
 create mode 100644 drivers/soc/mediatek/mtk-usb-wakeup.c
 create mode 100644 include/dt-bindings/soc/mediatek,usb-wakeup.h
 create mode 100644 include/linux/soc/mediatek/usb-wakeup.h

diff --git a/drivers/soc/mediatek/Kconfig b/drivers/soc/mediatek/Kconfig
index a7d0667..30cd226 100644
--- a/drivers/soc/mediatek/Kconfig
+++ b/drivers/soc/mediatek/Kconfig
@@ -31,4 +31,12 @@ config MTK_SCPSYS
 	  Say yes here to add support for the MediaTek SCPSYS power domain
 	  driver.
 
+config MTK_UWK
+	bool "MediaTek USB Wakeup Support"
+	select REGMAP
+	help
+	  Say yes here to add support for the MediaTek SSUSB-SPM glue layer
+	  which supports some different type of USB wakeup, such as IP-SLEEP,
+	  LINESTATE, IDDIG etc, and it can support multi SSUSB controllers.
+
 endmenu
diff --git a/drivers/soc/mediatek/Makefile b/drivers/soc/mediatek/Makefile
index 12998b0..66fbb54f 100644
--- a/drivers/soc/mediatek/Makefile
+++ b/drivers/soc/mediatek/Makefile
@@ -1,3 +1,4 @@
 obj-$(CONFIG_MTK_INFRACFG) += mtk-infracfg.o
 obj-$(CONFIG_MTK_PMIC_WRAP) += mtk-pmic-wrap.o
 obj-$(CONFIG_MTK_SCPSYS) += mtk-scpsys.o
+obj-$(CONFIG_MTK_UWK) += mtk-usb-wakeup.o
diff --git a/drivers/soc/mediatek/mtk-usb-wakeup.c b/drivers/soc/mediatek/mtk-usb-wakeup.c
new file mode 100644
index 0000000..16539a6
--- /dev/null
+++ b/drivers/soc/mediatek/mtk-usb-wakeup.c
@@ -0,0 +1,519 @@
+/*
+ * Copyright (c) 2017 MediaTek Inc.
+ * Author: Chunfeng Yun <chunfeng.yun@mediatek.com>
+ *
+ * SPDX-License-Identifier: GPL-2.0
+ *
+ */
+
+#include <dt-bindings/soc/mediatek,usb-wakeup.h>
+#include <linux/kernel.h>
+#include <linux/mfd/syscon.h>
+#include <linux/module.h>
+#include <linux/of.h>
+#include <linux/of_platform.h>
+#include <linux/regmap.h>
+#include <linux/slab.h>
+#include <linux/soc/mediatek/usb-wakeup.h>
+
+/* mt8173, mt8176 etc */
+#define PERI_WK_CTRL1	0x4
+#define WC1_IS_C(x)	(((x) & 0xf) << 26) /* cycle debounce */
+#define WC1_IS_EN	BIT(25)
+#define WC1_IS_P	BIT(6)  /* polarity for ip sleep */
+
+/* mt2712 etc */
+#define PERI_SSUSB_SPM_CTRL	0x0
+#define SSC_LINE_STATE_CHG	GENMASK(11, 8)
+#define SSC_LINE_STATE_EN	GENMASK(6, 5)
+#define SSC_IP_SLEEP_EN	BIT(4)
+#define SSC_SPM_INT_EN		BIT(1)
+
+enum mtk_uwk_vers {
+	MTK_UWK_V1 = 1,
+	MTK_UWK_V2,
+};
+
+struct mtk_uwk_pdata {
+	enum mtk_uwk_vers vers;
+};
+
+/**
+ * @reg_base: register offset within a syscon @wkc (e.g. pericfg module)
+ * @type: the types of wakeup, such as IP-SLEEP, LINE-STATE etc
+ */
+struct mtk_uwk_instance {
+	struct mtu_wakeup uwk;
+	u32 reg_base;
+	u32 reg_len;
+	u32 type;
+};
+
+struct mtk_uwk {
+	struct device *dev;
+	struct regmap *wkc;
+	const struct mtk_uwk_pdata *data;
+	struct mtk_uwk_instance **inst;
+	int num_inst;
+};
+
+static LIST_HEAD(of_uwk_providers);
+static DEFINE_MUTEX(of_uwk_mutex);
+
+static struct mtu_wakeup_provider *of_uwk_provider_add(struct device *dev,
+		struct mtu_wakeup *(*of_xlate)(struct device *dev,
+		struct of_phandle_args *args))
+{
+	struct mtu_wakeup_provider *provider;
+
+	provider = kzalloc(sizeof(*provider), GFP_KERNEL);
+	if (!provider)
+		return ERR_PTR(-ENOMEM);
+
+	provider->dev = dev;
+	provider->of_node = of_node_get(dev->of_node);
+	provider->of_xlate = of_xlate;
+
+	mutex_lock(&of_uwk_mutex);
+	list_add_tail(&provider->list, &of_uwk_providers);
+	mutex_unlock(&of_uwk_mutex);
+
+	return provider;
+}
+
+static void of_uwk_provider_del(struct device_node *np)
+{
+	struct mtu_wakeup_provider *provider;
+
+	mutex_lock(&of_uwk_mutex);
+	list_for_each_entry(provider, &of_uwk_providers, list) {
+		if (provider->of_node == np) {
+			list_del(&provider->list);
+			of_node_put(provider->of_node);
+			kfree(provider);
+			break;
+		}
+	}
+	mutex_unlock(&of_uwk_mutex);
+}
+
+static struct mtu_wakeup *of_uwk_get_from_provider(
+		struct of_phandle_args *args)
+{
+	struct mtu_wakeup_provider *provider;
+	struct device_node *child_np;
+	struct mtu_wakeup *uwk;
+
+	mutex_lock(&of_uwk_mutex);
+	list_for_each_entry(provider, &of_uwk_providers, list) {
+		for_each_child_of_node(provider->of_node, child_np) {
+			if (child_np == args->np) {
+				uwk = provider->of_xlate(provider->dev, args);
+				mutex_unlock(&of_uwk_mutex);
+				return uwk;
+			}
+		}
+	}
+	mutex_unlock(&of_uwk_mutex);
+
+	return ERR_PTR(-EPROBE_DEFER);
+}
+
+static struct mtu_wakeup *of_uwk_get(struct device_node *np, int index)
+{
+	struct mtu_wakeup *uwk = NULL;
+	struct of_phandle_args args;
+	int ret;
+
+	ret = of_parse_phandle_with_args(np, "mediatek,uwks",
+				"#mediatek,uwk-cells", index, &args);
+	if (ret)
+		return ERR_PTR(-ENODEV);
+
+	if (!of_device_is_available(args.np)) {
+		dev_warn(uwk->parent, "Requested uwk is disabled\n");
+		uwk = ERR_PTR(-ENODEV);
+		goto put_node;
+	}
+
+	uwk = of_uwk_get_from_provider(&args);
+
+put_node:
+	of_node_put(args.np);
+	return uwk;
+}
+
+static void devm_uwk_release(struct device *dev, void *res)
+{
+	struct mtu_wakeup *uwk = *(struct mtu_wakeup **)res;
+
+	if (IS_ERR_OR_NULL(uwk))
+		return;
+
+	module_put(uwk->ops->owner);
+	put_device(uwk->parent);
+}
+
+struct mtu_wakeup *devm_of_uwk_get_by_index(
+		struct device *dev, struct device_node *np, int index)
+{
+	struct mtu_wakeup **ptr, *uwk;
+
+	ptr = devres_alloc(devm_uwk_release, sizeof(*ptr), GFP_KERNEL);
+	if (!ptr)
+		return ERR_PTR(-ENOMEM);
+
+	uwk = of_uwk_get(np, index);
+	if (IS_ERR(uwk)) {
+		devres_free(ptr);
+		return uwk;
+	}
+
+	if (!try_module_get(uwk->ops->owner)) {
+		devres_free(ptr);
+		return ERR_PTR(-EPROBE_DEFER);
+	}
+
+	get_device(uwk->parent);
+
+	*ptr = uwk;
+	devres_add(dev, ptr);
+
+	return uwk;
+}
+EXPORT_SYMBOL_GPL(devm_of_uwk_get_by_index);
+
+int mtu_wakeup_enable(struct mtu_wakeup *uwk)
+{
+	int ret = 0;
+
+	if (!uwk)
+		return 0;
+
+	mutex_lock(&uwk->mutex);
+	if (uwk->count == 0 && uwk->ops->enable) {
+		ret = uwk->ops->enable(uwk);
+		if (ret) {
+			dev_err(uwk->parent, "uwk enable failed(%d)\n", ret);
+			goto out;
+		}
+	}
+	++uwk->count;
+
+out:
+	mutex_unlock(&uwk->mutex);
+	return ret;
+}
+EXPORT_SYMBOL_GPL(mtu_wakeup_enable);
+
+int mtu_wakeup_disable(struct mtu_wakeup *uwk)
+{
+	int ret = 0;
+
+	if (!uwk)
+		return 0;
+
+	mutex_lock(&uwk->mutex);
+	if (uwk->count == 1 && uwk->ops->disable) {
+		ret =  uwk->ops->disable(uwk);
+		if (ret) {
+			dev_err(uwk->parent, "uwk disable failed(%d)\n", ret);
+			goto out;
+		}
+	}
+	--uwk->count;
+
+out:
+	mutex_unlock(&uwk->mutex);
+	return ret;
+}
+EXPORT_SYMBOL_GPL(mtu_wakeup_disable);
+
+static struct mtk_uwk_instance *to_mwk_inst(struct mtu_wakeup *uwk)
+{
+	return uwk ? container_of(uwk, struct mtk_uwk_instance, uwk) : NULL;
+}
+
+static int mwk_v1_enable(struct mtk_uwk *mwk, struct mtk_uwk_instance *inst)
+{
+	struct regmap *wkc = mwk->wkc;
+	u32 val;
+
+	/* Only IP-SLEEP is supported */
+	if (inst->type != MTU_WK_IP_SLEEP)
+		return 0;
+
+	regmap_read(wkc, PERI_WK_CTRL1, &val);
+	val &= ~(WC1_IS_P | WC1_IS_C(0xf));
+	val |= WC1_IS_EN | WC1_IS_C(0x8);
+	regmap_write(wkc, PERI_WK_CTRL1, val);
+	regmap_read(wkc, PERI_WK_CTRL1, &val);
+	dev_dbg(mwk->dev, "%s: WK_CTRL1=%#x, type=%d\n",
+		__func__, val, inst->type);
+
+	return 0;
+}
+
+static int mwk_v1_disable(struct mtk_uwk *mwk, struct mtk_uwk_instance *inst)
+{
+	if (inst->type == MTU_WK_IP_SLEEP)
+		regmap_update_bits(mwk->wkc, PERI_WK_CTRL1, WC1_IS_EN, 0);
+
+	return 0;
+}
+
+static int mwk_v2_enable(struct mtk_uwk *mwk, struct mtk_uwk_instance *inst)
+{
+	struct regmap *wkc = mwk->wkc;
+	u32 rbase = inst->reg_base;
+	u32 val;
+
+	regmap_read(wkc, rbase + PERI_SSUSB_SPM_CTRL, &val);
+	switch (inst->type) {
+	case MTU_WK_IP_SLEEP:
+		val |= SSC_IP_SLEEP_EN;
+		break;
+	case MTU_WK_LINE_STATE:
+		val |= SSC_LINE_STATE_EN | SSC_LINE_STATE_CHG;
+		break;
+	default:
+		/* checked by xlate, ignore the error */
+		break;
+	}
+	val |= SSC_SPM_INT_EN;
+	regmap_write(wkc, rbase + PERI_SSUSB_SPM_CTRL, val);
+	regmap_read(wkc, rbase + PERI_SSUSB_SPM_CTRL, &val);
+	dev_dbg(mwk->dev, "%s: CTRL=%#x, type=%d\n",
+		__func__, val, inst->type);
+
+	return 0;
+}
+
+static int mwk_v2_disable(struct mtk_uwk *mwk, struct mtk_uwk_instance *inst)
+{
+	struct regmap *wkc = mwk->wkc;
+	u32 rbase = inst->reg_base;
+	u32 val;
+
+	regmap_read(wkc, rbase + PERI_SSUSB_SPM_CTRL, &val);
+	switch (inst->type) {
+	case MTU_WK_IP_SLEEP:
+		val &= ~SSC_IP_SLEEP_EN;
+		break;
+	case MTU_WK_LINE_STATE:
+		val &= ~(SSC_LINE_STATE_EN | SSC_LINE_STATE_CHG);
+		break;
+	default:
+		break;
+	}
+	val &= ~SSC_SPM_INT_EN;
+	regmap_write(wkc, rbase + PERI_SSUSB_SPM_CTRL, val);
+	dev_dbg(mwk->dev, "%s: type=%d\n", __func__, inst->type);
+
+	return 0;
+}
+
+static int mwk_enable(struct mtu_wakeup *uwk)
+{
+	struct mtk_uwk_instance *inst = to_mwk_inst(uwk);
+	struct mtk_uwk *mwk = dev_get_drvdata(uwk->parent);
+	int ret = 0;
+
+	switch (mwk->data->vers) {
+	case MTK_UWK_V1:
+		ret = mwk_v1_enable(mwk, inst);
+		break;
+	case MTK_UWK_V2:
+		ret = mwk_v2_enable(mwk, inst);
+		break;
+	default:
+		break;
+	}
+	return ret;
+}
+
+static int mwk_disable(struct mtu_wakeup *uwk)
+{
+	struct mtk_uwk_instance *inst = to_mwk_inst(uwk);
+	struct mtk_uwk *mwk = dev_get_drvdata(uwk->parent);
+	int ret = 0;
+
+	switch (mwk->data->vers) {
+	case MTK_UWK_V1:
+		ret = mwk_v1_disable(mwk, inst);
+		break;
+	case MTK_UWK_V2:
+		ret = mwk_v2_disable(mwk, inst);
+		break;
+	default:
+		break;
+	}
+	return ret;
+}
+
+static struct mtk_uwk_instance *mwk_inst_create(struct device *dev,
+		struct device_node *np,
+		const struct mtu_wakeup_ops *ops)
+{
+	struct mtk_uwk_instance *inst;
+	struct mtu_wakeup *uwk;
+	u32 buf[2];
+	int ret;
+
+	inst = devm_kzalloc(dev, sizeof(*inst), GFP_KERNEL);
+	if (!inst)
+		return ERR_PTR(-ENOMEM);
+
+	ret = of_property_read_u32_array(np, "reg", buf, ARRAY_SIZE(buf));
+	if (ret) {
+		dev_err(dev, "fail to read reg\n");
+		return ERR_PTR(ret);
+	}
+
+	inst->reg_base = buf[0];
+	inst->reg_len = buf[1];
+	uwk = &inst->uwk;
+	uwk->node = np;
+	uwk->ops = ops;
+	uwk->parent = dev;
+	mutex_init(&uwk->mutex);
+	dev_dbg(dev, "reg: %#x/%#x\n", inst->reg_base, inst->reg_len);
+
+	return inst;
+}
+
+static struct mtu_wakeup *mwk_xlate(struct device *dev,
+		struct of_phandle_args *args)
+{
+	struct mtk_uwk *mwk = dev_get_drvdata(dev);
+	struct mtk_uwk_instance *inst = NULL;
+	struct device_node *uwk_np = args->np;
+	int index;
+
+	if (args->args_count != 1) {
+		dev_err(dev, "invalid number of cells in uwk property\n");
+		return ERR_PTR(-EINVAL);
+	}
+
+	for (index = 0; index < mwk->num_inst; index++)
+		if (uwk_np == mwk->inst[index]->uwk.node) {
+			inst = mwk->inst[index];
+			break;
+		}
+
+	if (!inst) {
+		dev_err(dev, "failed to find appropriate uwk\n");
+		return ERR_PTR(-EINVAL);
+	}
+
+	inst->type = args->args[0];
+	if (!(inst->type == MTU_WK_IP_SLEEP ||
+	      inst->type == MTU_WK_LINE_STATE)) {
+		dev_err(dev, "unsupported uwk type=%d\n", inst->type);
+		return ERR_PTR(-EINVAL);
+	}
+
+	return &inst->uwk;
+}
+
+static const struct mtu_wakeup_ops mwk_ops = {
+	.enable = mwk_enable,
+	.disable = mwk_disable,
+	.owner = THIS_MODULE,
+};
+
+static const struct mtk_uwk_pdata mwk_v1_pdata = {
+	.vers = MTK_UWK_V1,
+};
+
+static const struct mtk_uwk_pdata mwk_v2_pdata = {
+	.vers = MTK_UWK_V2,
+};
+
+static const struct of_device_id mwk_id_table[] = {
+	{ .compatible = "mediatek,usb-wk-v1", .data = &mwk_v1_pdata },
+	{ .compatible = "mediatek,usb-wk-v2", .data = &mwk_v2_pdata },
+	{ },
+};
+MODULE_DEVICE_TABLE(of, mwk_id_table);
+
+static int mtk_uwk_probe(struct platform_device *pdev)
+{
+	struct device *dev = &pdev->dev;
+	struct device_node *np = dev->of_node;
+	struct device_node *child_np;
+	struct mtu_wakeup_provider *provider;
+	struct mtk_uwk *mwk;
+	int index;
+	int ret;
+
+	mwk = devm_kzalloc(dev, sizeof(*mwk), GFP_KERNEL);
+	if (!mwk)
+		return -ENOMEM;
+
+	mwk->data = of_device_get_match_data(dev);
+	if (!mwk->data)
+		return -EINVAL;
+
+	mwk->num_inst = of_get_child_count(np);
+	mwk->inst = devm_kcalloc(dev, mwk->num_inst,
+				  sizeof(*mwk->inst), GFP_KERNEL);
+	if (!mwk->inst)
+		return -ENOMEM;
+
+	mwk->dev = dev;
+	platform_set_drvdata(pdev, mwk);
+
+	mwk->wkc = syscon_regmap_lookup_by_phandle(np, "mediatek,wkc");
+	if (IS_ERR(mwk->wkc)) {
+		dev_err(dev, "fail to get mediatek,wkc syscon\n");
+		return PTR_ERR(mwk->wkc);
+	}
+
+	index = 0;
+	for_each_child_of_node(np, child_np) {
+		struct mtk_uwk_instance *inst;
+
+		inst = mwk_inst_create(dev, child_np, &mwk_ops);
+		if (IS_ERR(inst)) {
+			dev_err(dev, "failed to create mwk instance\n");
+			ret = PTR_ERR(inst);
+			goto put_child;
+		}
+
+		mwk->inst[index] = inst;
+		index++;
+	}
+
+	provider = of_uwk_provider_add(dev, mwk_xlate);
+
+	return PTR_ERR_OR_ZERO(provider);
+
+put_child:
+	of_node_put(child_np);
+	return ret;
+}
+
+static int mtk_uwk_remove(struct platform_device *pdev)
+{
+	struct device *dev = &pdev->dev;
+	struct device_node *np = dev->of_node;
+
+	of_uwk_provider_del(np);
+	return 0;
+}
+
+static struct platform_driver mtk_uwk_drv = {
+	.probe = mtk_uwk_probe,
+	.remove = mtk_uwk_remove,
+	.driver = {
+		.name = "mtk_uwk",
+		.owner = THIS_MODULE,
+		.of_match_table = mwk_id_table,
+	},
+};
+
+module_platform_driver(mtk_uwk_drv);
+MODULE_AUTHOR("Chunfeng Yun <chunfeng.yun@mediatek.com>");
+MODULE_DESCRIPTION("MediaTek USB Wakeup driver");
+MODULE_LICENSE("GPL v2");
diff --git a/include/dt-bindings/soc/mediatek,usb-wakeup.h b/include/dt-bindings/soc/mediatek,usb-wakeup.h
new file mode 100644
index 0000000..2461795
--- /dev/null
+++ b/include/dt-bindings/soc/mediatek,usb-wakeup.h
@@ -0,0 +1,15 @@
+/*
+ * Copyright (c) 2017 MediaTek Inc.
+ * Author: Chunfeng Yun <chunfeng.yun@mediatek.com>
+ *
+ * SPDX-License-Identifier: GPL-2.0
+ *
+ */
+
+#ifndef __DT_BINDINGS_MTK_USB_WK_H__
+#define __DT_BINDINGS_MTK_USB_WK_H__
+
+#define MTU_WK_IP_SLEEP	1
+#define MTU_WK_LINE_STATE	2
+
+#endif
diff --git a/include/linux/soc/mediatek/usb-wakeup.h b/include/linux/soc/mediatek/usb-wakeup.h
new file mode 100644
index 0000000..5697367
--- /dev/null
+++ b/include/linux/soc/mediatek/usb-wakeup.h
@@ -0,0 +1,88 @@
+/*
+ * Copyright (c) 2017 MediaTek Inc.
+ * Author: Chunfeng Yun <chunfeng.yun@mediatek.com>
+ *
+ * SPDX-License-Identifier: GPL-2.0
+ *
+ */
+
+#ifndef __MTK_USB_WAKEUP_H__
+#define __MTK_USB_WAKEUP_H__
+
+#include <linux/device.h>
+#include <linux/err.h>
+#include <linux/mutex.h>
+#include <linux/of.h>
+
+struct mtu_wakeup;
+
+/**
+ * struct mtu_wakeup_ops - set of function pointers for performing
+ *    mtu_wakeup operations
+ * @enable: enable a type of usb wakeup when system suspend
+ * @disable: disable a type of usb wakeup when system resume
+ * @owner: the module owner using the ops
+ */
+struct mtu_wakeup_ops {
+	int	(*enable)(struct mtu_wakeup *uwk);
+	int	(*disable)(struct mtu_wakeup *uwk);
+	struct module *owner;
+};
+
+/**
+ * struct mtu_wakeup - represents the MediaTek USB wakeup device
+ * @parent: the parent device of the mtu_wakeup
+ * @node: associated device tree node
+ * @ops: function pointers for performing mtu_wakeup operations
+ * @mutex: mutex to protect @ops
+ * @count: used to protect when the mtu_wakeup is used by multiple consumers
+ */
+struct mtu_wakeup {
+	struct device *parent;
+	struct device_node *node;
+	const struct mtu_wakeup_ops *ops;
+	struct mutex mutex;
+	int count;
+};
+
+/**
+ * struct mtu_wakeup_provider - represents the mtu_wakeup provider
+ * @dev: the parent device of the mtu_wakeup
+ * @list: to maintain a linked list of mtu_wakeup providers
+ * @of_node: associated device tree node
+ * @of_xlate: function pointer to obtain mtu_wakeup instance from
+ *	its tree node
+ */
+struct mtu_wakeup_provider {
+	struct device *dev;
+	struct list_head list;
+	struct device_node *of_node;
+	struct mtu_wakeup *(*of_xlate)(struct device *dev,
+		struct of_phandle_args *args);
+};
+
+#if IS_ENABLED(CONFIG_MTK_UWK)
+struct mtu_wakeup *devm_of_uwk_get_by_index(
+	struct device *dev, struct device_node *np, int index);
+int mtu_wakeup_enable(struct mtu_wakeup *uwk);
+int mtu_wakeup_disable(struct mtu_wakeup *uwk);
+
+#else
+struct mtu_wakeup *devm_of_uwk_get_by_index(
+	struct device *dev, struct device_node *np, int index)
+{
+	return ERR_PTR(-ENODEV);
+}
+
+int mtu_wakeup_enable(struct mtu_wakeup *uwk)
+{
+	return uwk ? -ENODEV : 0;
+}
+
+int mtu_wakeup_disable(struct mtu_wakeup *uwk)
+{
+	return uwk ? -ENODEV : 0;
+}
+#endif
+
+#endif	/* __MTK_USB_WAKEUP_H__ */
-- 
1.9.1

  reply	other threads:[~2017-12-09  8:47 UTC|newest]

Thread overview: 45+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-12-09  8:45 [PATCH 0/7] Add USB remote wakeup driver Chunfeng Yun
2017-12-09  8:45 ` Chunfeng Yun
2017-12-09  8:45 ` Chunfeng Yun
2017-12-09  8:45 ` Chunfeng Yun [this message]
2017-12-09  8:45   ` [PATCH 1/7] soc: mediatek: Add USB " Chunfeng Yun
2017-12-09  8:45   ` [1/7] " Chunfeng Yun
2017-12-09  8:45   ` [PATCH 1/7] " Chunfeng Yun
2017-12-15 20:55   ` Rob Herring
2017-12-15 20:55     ` Rob Herring
2017-12-15 20:55     ` [1/7] " Rob Herring
2017-12-15 20:55     ` [PATCH 1/7] " Rob Herring
2017-12-21  6:50     ` Chunfeng Yun
2017-12-21  6:50       ` Chunfeng Yun
2017-12-21  6:50       ` [1/7] " Chunfeng Yun
2017-12-21  6:50       ` [PATCH 1/7] " Chunfeng Yun
2017-12-09  8:45 ` [PATCH 2/7] dt-bindings: soc: mediatek: add bindings document for USB wakeup Chunfeng Yun
2017-12-09  8:45   ` Chunfeng Yun
2017-12-09  8:45   ` [2/7] " Chunfeng Yun
2017-12-09  8:45   ` [PATCH 2/7] " Chunfeng Yun
2017-12-09  8:45 ` [PATCH 3/7] usb: xhci-mtk: use APIs of mtu_wakeup to support remote wakeup Chunfeng Yun
2017-12-09  8:45   ` Chunfeng Yun
2017-12-09  8:45   ` [3/7] " Chunfeng Yun
2017-12-09  8:45   ` [PATCH 3/7] " Chunfeng Yun
2017-12-09  8:45 ` [PATCH 4/7] usb: mtu3: " Chunfeng Yun
2017-12-09  8:45   ` Chunfeng Yun
2017-12-09  8:45   ` [4/7] " Chunfeng Yun
2017-12-09  8:45   ` [PATCH 4/7] " Chunfeng Yun
2017-12-09  8:45 ` [PATCH 5/7] dt-bindings: usb: mtk-xhci: add USB wakeup properties Chunfeng Yun
2017-12-09  8:45   ` Chunfeng Yun
2017-12-09  8:45   ` [5/7] " Chunfeng Yun
2017-12-09  8:45   ` [PATCH 5/7] " Chunfeng Yun
2017-12-09  8:45 ` [PATCH 6/7] dt-bindings: usb: mtu3: " Chunfeng Yun
2017-12-09  8:45   ` Chunfeng Yun
2017-12-09  8:45   ` [6/7] " Chunfeng Yun
2017-12-09  8:45   ` [PATCH 6/7] " Chunfeng Yun
2017-12-09  8:45 ` [PATCH 7/7] arm64: dts: mt8173: add uwk node and remove unused usb property Chunfeng Yun
2017-12-09  8:45   ` Chunfeng Yun
2017-12-09  8:45   ` [7/7] " Chunfeng Yun
2017-12-09  8:45   ` [PATCH 7/7] " Chunfeng Yun
2017-12-15 20:55 ` [PATCH 0/7] Add USB remote wakeup driver Rob Herring
2017-12-15 20:55   ` Rob Herring
2017-12-15 20:55   ` Rob Herring
2017-12-21  6:48   ` Chunfeng Yun
2017-12-21  6:48     ` Chunfeng Yun
2017-12-21  6:48     ` Chunfeng Yun

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1512809136-2779-2-git-send-email-chunfeng.yun@mediatek.com \
    --to=chunfeng.yun@mediatek.com \
    --cc=catalin.marinas@arm.com \
    --cc=devicetree@vger.kernel.org \
    --cc=felipe.balbi@linux.intel.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=jdelvare@suse.de \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mediatek@lists.infradead.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=mathias.nyman@intel.com \
    --cc=matthias.bgg@gmail.com \
    --cc=robh+dt@kernel.org \
    --cc=sean.wang@mediatek.com \
    --cc=will.deacon@arm.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.