All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2 0/5] usb: typec: ucsi: ccg: add runtime pm support
@ 2019-05-20 18:37 Ajay Gupta
  2019-05-20 18:37 ` [PATCH v2 1/5] i2c: nvidia-gpu: refactor master_xfer Ajay Gupta
                   ` (4 more replies)
  0 siblings, 5 replies; 10+ messages in thread
From: Ajay Gupta @ 2019-05-20 18:37 UTC (permalink / raw)
  To: heikki.krogerus, wsa; +Cc: linux-usb, linux-i2c, Ajay Gupta

Hi Heikki and Wolfram

These patches add support for runtime power management for UCSI CCGx driver.
I have tested them with NVIDIA GPU card which has i2c interface to talk to
CCG controller. I have added runtime pm support for the i2c bus driver as well.

First patch refactors master_xfer() of i2c driver fixing comment from Wolfram.
Second and third patch add support for runtime pm in i2c bus driver and UCSI CCGx
driver.

Last two patches add workaround for an old version of ccg firmware
which has known issue of missing interrupt when a device is connected
to runtime resume the ccg controller. The workaround is needed because
if a GPU card doesn't get new firmware but gets new kernel then also it
should continue to work. The workaround is to request runtime resume of
i2c client which is UCSI Cypress CCGx driver. CCG driver will call the ISR
for any connector change event only if NVIDIA GPU has old CCG firmware with
the known issue.

All the five patches should go together since ucsi_ccg driver is dependent
on i2c driver for runtime pm wokaround. I would prefer it to go through
usb-tree since usb ucsi_ccg driver is the main driver getting runtime pm
functionality with the series.

Thanks
Ajay


Ajay Gupta (5):
  i2c: nvidia-gpu: refactor master_xfer
  i2c: nvidia-gpu: add runtime pm support
  usb: typec: ucsi: ccg: enable runtime pm support
  i2c: nvidia-gpu: resume ccgx i2c client
  usb: typec: ucsi: ccg: add runtime pm workaround

 drivers/i2c/busses/i2c-nvidia-gpu.c |  60 ++++++++----
 drivers/usb/typec/ucsi/ucsi.c       |   1 +
 drivers/usb/typec/ucsi/ucsi_ccg.c   | 140 +++++++++++++++++++++++++++-
 3 files changed, 181 insertions(+), 20 deletions(-)

-- 
2.17.1


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [PATCH v2 1/5] i2c: nvidia-gpu: refactor master_xfer
  2019-05-20 18:37 [PATCH v2 0/5] usb: typec: ucsi: ccg: add runtime pm support Ajay Gupta
@ 2019-05-20 18:37 ` Ajay Gupta
  2019-05-20 18:37 ` [PATCH v2 2/5] i2c: nvidia-gpu: add runtime pm support Ajay Gupta
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 10+ messages in thread
From: Ajay Gupta @ 2019-05-20 18:37 UTC (permalink / raw)
  To: heikki.krogerus, wsa; +Cc: linux-usb, linux-i2c, Ajay Gupta

From: Ajay Gupta <ajayg@nvidia.com>

Added a local variable "send_stop" to simplify "goto" statements.

The "send_stop" handles below two case
1) When first i2c start fails and so i2c stop is not sent before
exiting

2) When i2c stop failed after all transfers and we do not need to
send another stop before exiting.

Signed-off-by: Ajay Gupta <ajayg@nvidia.com>
---
 drivers/i2c/busses/i2c-nvidia-gpu.c | 27 ++++++++++++++++-----------
 1 file changed, 16 insertions(+), 11 deletions(-)

diff --git a/drivers/i2c/busses/i2c-nvidia-gpu.c b/drivers/i2c/busses/i2c-nvidia-gpu.c
index 1c8f708f212b..2d9561ec2320 100644
--- a/drivers/i2c/busses/i2c-nvidia-gpu.c
+++ b/drivers/i2c/busses/i2c-nvidia-gpu.c
@@ -169,6 +169,7 @@ static int gpu_i2c_master_xfer(struct i2c_adapter *adap,
 {
 	struct gpu_i2c_dev *i2cd = i2c_get_adapdata(adap);
 	int status, status2;
+	bool send_stop = true;
 	int i, j;
 
 	/*
@@ -182,37 +183,41 @@ static int gpu_i2c_master_xfer(struct i2c_adapter *adap,
 			/* gpu_i2c_read has implicit start */
 			status = gpu_i2c_read(i2cd, msgs[i].buf, msgs[i].len);
 			if (status < 0)
-				goto stop;
+				goto exit;
 		} else {
 			u8 addr = i2c_8bit_addr_from_msg(msgs + i);
 
 			status = gpu_i2c_start(i2cd);
 			if (status < 0) {
 				if (i == 0)
-					return status;
-				goto stop;
+					send_stop = false;
+				goto exit;
 			}
 
 			status = gpu_i2c_write(i2cd, addr);
 			if (status < 0)
-				goto stop;
+				goto exit;
 
 			for (j = 0; j < msgs[i].len; j++) {
 				status = gpu_i2c_write(i2cd, msgs[i].buf[j]);
 				if (status < 0)
-					goto stop;
+					goto exit;
 			}
 		}
 	}
 	status = gpu_i2c_stop(i2cd);
-	if (status < 0)
-		return status;
+	if (status < 0) {
+		send_stop = false;
+		goto exit;
+	}
 
 	return i;
-stop:
-	status2 = gpu_i2c_stop(i2cd);
-	if (status2 < 0)
-		dev_err(i2cd->dev, "i2c stop failed %d\n", status2);
+exit:
+	if (send_stop) {
+		status2 = gpu_i2c_stop(i2cd);
+		if (status2 < 0)
+			dev_err(i2cd->dev, "i2c stop failed %d\n", status2);
+	}
 	return status;
 }
 
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH v2 2/5] i2c: nvidia-gpu: add runtime pm support
  2019-05-20 18:37 [PATCH v2 0/5] usb: typec: ucsi: ccg: add runtime pm support Ajay Gupta
  2019-05-20 18:37 ` [PATCH v2 1/5] i2c: nvidia-gpu: refactor master_xfer Ajay Gupta
@ 2019-05-20 18:37 ` Ajay Gupta
  2019-05-20 18:37 ` [PATCH v2 3/5] usb: typec: ucsi: ccg: enable " Ajay Gupta
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 10+ messages in thread
From: Ajay Gupta @ 2019-05-20 18:37 UTC (permalink / raw)
  To: heikki.krogerus, wsa; +Cc: linux-usb, linux-i2c, Ajay Gupta

From: Ajay Gupta <ajayg@nvidia.com>

Enable runtime pm support with autosuspend delay of three second.
This is to make sure I2C client device Cypress CCGx has completed
all transaction.

Signed-off-by: Ajay Gupta <ajayg@nvidia.com>
---
Changes from v1->v2: None

 drivers/i2c/busses/i2c-nvidia-gpu.c | 19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)

diff --git a/drivers/i2c/busses/i2c-nvidia-gpu.c b/drivers/i2c/busses/i2c-nvidia-gpu.c
index 2d9561ec2320..af445590041b 100644
--- a/drivers/i2c/busses/i2c-nvidia-gpu.c
+++ b/drivers/i2c/busses/i2c-nvidia-gpu.c
@@ -176,6 +176,7 @@ static int gpu_i2c_master_xfer(struct i2c_adapter *adap,
 	 * The controller supports maximum 4 byte read due to known
 	 * limitation of sending STOP after every read.
 	 */
+	pm_runtime_get_sync(i2cd->dev);
 	for (i = 0; i < num; i++) {
 		if (msgs[i].flags & I2C_M_RD) {
 			/* program client address before starting read */
@@ -211,6 +212,8 @@ static int gpu_i2c_master_xfer(struct i2c_adapter *adap,
 		goto exit;
 	}
 
+	pm_runtime_mark_last_busy(i2cd->dev);
+	pm_runtime_put_autosuspend(i2cd->dev);
 	return i;
 exit:
 	if (send_stop) {
@@ -218,6 +221,8 @@ static int gpu_i2c_master_xfer(struct i2c_adapter *adap,
 		if (status2 < 0)
 			dev_err(i2cd->dev, "i2c stop failed %d\n", status2);
 	}
+	pm_runtime_mark_last_busy(i2cd->dev);
+	pm_runtime_put_autosuspend(i2cd->dev);
 	return status;
 }
 
@@ -337,6 +342,11 @@ static int gpu_i2c_probe(struct pci_dev *pdev, const struct pci_device_id *id)
 		goto del_adapter;
 	}
 
+	pm_runtime_set_autosuspend_delay(&pdev->dev, 3000);
+	pm_runtime_use_autosuspend(&pdev->dev);
+	pm_runtime_put_autosuspend(&pdev->dev);
+	pm_runtime_allow(&pdev->dev);
+
 	return 0;
 
 del_adapter:
@@ -350,10 +360,16 @@ static void gpu_i2c_remove(struct pci_dev *pdev)
 {
 	struct gpu_i2c_dev *i2cd = dev_get_drvdata(&pdev->dev);
 
+	pm_runtime_get_noresume(i2cd->dev);
 	i2c_del_adapter(&i2cd->adapter);
 	pci_free_irq_vectors(pdev);
 }
 
+static int gpu_i2c_suspend(struct device *dev)
+{
+	return 0;
+}
+
 static __maybe_unused int gpu_i2c_resume(struct device *dev)
 {
 	struct gpu_i2c_dev *i2cd = dev_get_drvdata(dev);
@@ -362,7 +378,8 @@ static __maybe_unused int gpu_i2c_resume(struct device *dev)
 	return 0;
 }
 
-static UNIVERSAL_DEV_PM_OPS(gpu_i2c_driver_pm, NULL, gpu_i2c_resume, NULL);
+static UNIVERSAL_DEV_PM_OPS(gpu_i2c_driver_pm, gpu_i2c_suspend, gpu_i2c_resume,
+			    NULL);
 
 static struct pci_driver gpu_i2c_driver = {
 	.name		= "nvidia-gpu",
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH v2 3/5] usb: typec: ucsi: ccg: enable runtime pm support
  2019-05-20 18:37 [PATCH v2 0/5] usb: typec: ucsi: ccg: add runtime pm support Ajay Gupta
  2019-05-20 18:37 ` [PATCH v2 1/5] i2c: nvidia-gpu: refactor master_xfer Ajay Gupta
  2019-05-20 18:37 ` [PATCH v2 2/5] i2c: nvidia-gpu: add runtime pm support Ajay Gupta
@ 2019-05-20 18:37 ` Ajay Gupta
  2019-05-21 13:37   ` Heikki Krogerus
  2019-05-20 18:37 ` [PATCH v2 4/5] i2c: nvidia-gpu: resume ccgx i2c client Ajay Gupta
  2019-05-20 18:37 ` [PATCH v2 5/5] usb: typec: ucsi: ccg: add runtime pm workaround Ajay Gupta
  4 siblings, 1 reply; 10+ messages in thread
From: Ajay Gupta @ 2019-05-20 18:37 UTC (permalink / raw)
  To: heikki.krogerus, wsa; +Cc: linux-usb, linux-i2c, Ajay Gupta

From: Ajay Gupta <ajayg@nvidia.com>

The change enables runtime pm support to UCSI CCG driver.
ucsi_send_command() is used in resume path and so exported
ucsi_send_command() symbol in ucsi.c for modular build.

Signed-off-by: Ajay Gupta <ajayg@nvidia.com>
---
Changes from v1->v2 : None

 drivers/usb/typec/ucsi/ucsi.c     |  1 +
 drivers/usb/typec/ucsi/ucsi_ccg.c | 60 +++++++++++++++++++++++++++++++
 2 files changed, 61 insertions(+)

diff --git a/drivers/usb/typec/ucsi/ucsi.c b/drivers/usb/typec/ucsi/ucsi.c
index 7850b851cecd..e9454134d399 100644
--- a/drivers/usb/typec/ucsi/ucsi.c
+++ b/drivers/usb/typec/ucsi/ucsi.c
@@ -206,6 +206,7 @@ int ucsi_send_command(struct ucsi *ucsi, struct ucsi_control *ctrl,
 
 	return ret;
 }
+EXPORT_SYMBOL_GPL(ucsi_send_command);
 
 /* -------------------------------------------------------------------------- */
 
diff --git a/drivers/usb/typec/ucsi/ucsi_ccg.c b/drivers/usb/typec/ucsi/ucsi_ccg.c
index 9d46aa9e4e35..cc7094ecda2d 100644
--- a/drivers/usb/typec/ucsi/ucsi_ccg.c
+++ b/drivers/usb/typec/ucsi/ucsi_ccg.c
@@ -14,6 +14,8 @@
 #include <linux/module.h>
 #include <linux/pci.h>
 #include <linux/platform_device.h>
+#include <linux/pm.h>
+#include <linux/pm_runtime.h>
 
 #include <asm/unaligned.h>
 #include "ucsi.h"
@@ -210,6 +212,7 @@ static int ccg_read(struct ucsi_ccg *uc, u16 rab, u8 *data, u32 len)
 	if (quirks && quirks->max_read_len)
 		max_read_len = quirks->max_read_len;
 
+	pm_runtime_get_sync(uc->dev);
 	while (rem_len > 0) {
 		msgs[1].buf = &data[len - rem_len];
 		rlen = min_t(u16, rem_len, max_read_len);
@@ -218,12 +221,14 @@ static int ccg_read(struct ucsi_ccg *uc, u16 rab, u8 *data, u32 len)
 		status = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
 		if (status < 0) {
 			dev_err(uc->dev, "i2c_transfer failed %d\n", status);
+			pm_runtime_put_sync(uc->dev);
 			return status;
 		}
 		rab += rlen;
 		rem_len -= rlen;
 	}
 
+	pm_runtime_put_sync(uc->dev);
 	return 0;
 }
 
@@ -249,13 +254,16 @@ static int ccg_write(struct ucsi_ccg *uc, u16 rab, u8 *data, u32 len)
 	msgs[0].len = len + sizeof(rab);
 	msgs[0].buf = buf;
 
+	pm_runtime_get_sync(uc->dev);
 	status = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
 	if (status < 0) {
 		dev_err(uc->dev, "i2c_transfer failed %d\n", status);
+		pm_runtime_put_sync(uc->dev);
 		kfree(buf);
 		return status;
 	}
 
+	pm_runtime_put_sync(uc->dev);
 	kfree(buf);
 	return 0;
 }
@@ -1134,6 +1142,10 @@ static int ucsi_ccg_probe(struct i2c_client *client,
 	if (status)
 		dev_err(uc->dev, "cannot create sysfs group: %d\n", status);
 
+	pm_runtime_set_active(uc->dev);
+	pm_runtime_enable(uc->dev);
+	pm_runtime_idle(uc->dev);
+
 	return 0;
 }
 
@@ -1143,6 +1155,7 @@ static int ucsi_ccg_remove(struct i2c_client *client)
 
 	cancel_work_sync(&uc->work);
 	ucsi_unregister_ppm(uc->ucsi);
+	pm_runtime_disable(uc->dev);
 	free_irq(uc->irq, uc);
 	sysfs_remove_group(&uc->dev->kobj, &ucsi_ccg_attr_group);
 
@@ -1155,9 +1168,56 @@ static const struct i2c_device_id ucsi_ccg_device_id[] = {
 };
 MODULE_DEVICE_TABLE(i2c, ucsi_ccg_device_id);
 
+static int ucsi_ccg_suspend(struct device *dev)
+{
+	return 0;
+}
+
+static int ucsi_ccg_resume(struct device *dev)
+{
+	struct i2c_client *client = to_i2c_client(dev);
+	struct ucsi_ccg *uc = i2c_get_clientdata(client);
+	struct ucsi *ucsi = uc->ucsi;
+	struct ucsi_control c;
+	int ret;
+
+	/* restore UCSI notification enable mask */
+	UCSI_CMD_SET_NTFY_ENABLE(c, UCSI_ENABLE_NTFY_ALL);
+	ret = ucsi_send_command(ucsi, &c, NULL, 0);
+	if (ret < 0) {
+		dev_err(uc->dev, "%s: failed to set notification enable - %d\n",
+			__func__, ret);
+	}
+	return 0;
+}
+
+static int ucsi_ccg_runtime_suspend(struct device *dev)
+{
+	return 0;
+}
+
+static int ucsi_ccg_runtime_resume(struct device *dev)
+{
+	return 0;
+}
+
+static int ucsi_ccg_runtime_idle(struct device *dev)
+{
+	return 0;
+}
+
+static const struct dev_pm_ops ucsi_ccg_pm = {
+	.suspend = ucsi_ccg_suspend,
+	.resume = ucsi_ccg_resume,
+	.runtime_suspend = ucsi_ccg_runtime_suspend,
+	.runtime_resume = ucsi_ccg_runtime_resume,
+	.runtime_idle = ucsi_ccg_runtime_idle,
+};
+
 static struct i2c_driver ucsi_ccg_driver = {
 	.driver = {
 		.name = "ucsi_ccg",
+		.pm = &ucsi_ccg_pm,
 	},
 	.probe = ucsi_ccg_probe,
 	.remove = ucsi_ccg_remove,
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH v2 4/5] i2c: nvidia-gpu: resume ccgx i2c client
  2019-05-20 18:37 [PATCH v2 0/5] usb: typec: ucsi: ccg: add runtime pm support Ajay Gupta
                   ` (2 preceding siblings ...)
  2019-05-20 18:37 ` [PATCH v2 3/5] usb: typec: ucsi: ccg: enable " Ajay Gupta
@ 2019-05-20 18:37 ` Ajay Gupta
  2019-05-20 18:37 ` [PATCH v2 5/5] usb: typec: ucsi: ccg: add runtime pm workaround Ajay Gupta
  4 siblings, 0 replies; 10+ messages in thread
From: Ajay Gupta @ 2019-05-20 18:37 UTC (permalink / raw)
  To: heikki.krogerus, wsa; +Cc: linux-usb, linux-i2c, Ajay Gupta

From: Ajay Gupta <ajayg@nvidia.com>

Cypress USB Type-C CCGx controller firmware version 3.1.10
(which is being used in many NVIDIA GPU cards) has known issue of
not triggering interrupt when a USB device is hot plugged to runtime
resume the controller. If any GPU card gets latest kernel with runtime
pm support but does not get latest fixed firmware then also it should
continue to work and therefore a workaround is required to check for
any connector change event

The workaround is to request runtime resume of i2c client
which is UCSI Cypress CCGx driver. CCG driver will call the ISR
for any connector change event only if NVIDIA GPU has old
CCG firmware with the known issue.

Signed-off-by: Ajay Gupta <ajayg@nvidia.com>
---
Changes from v1->v2:
	Fixed comment from Wolfram by fixing comment style.

 drivers/i2c/busses/i2c-nvidia-gpu.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)

diff --git a/drivers/i2c/busses/i2c-nvidia-gpu.c b/drivers/i2c/busses/i2c-nvidia-gpu.c
index af445590041b..6826740fd0f5 100644
--- a/drivers/i2c/busses/i2c-nvidia-gpu.c
+++ b/drivers/i2c/busses/i2c-nvidia-gpu.c
@@ -51,6 +51,7 @@ struct gpu_i2c_dev {
 	void __iomem *regs;
 	struct i2c_adapter adapter;
 	struct i2c_board_info *gpu_ccgx_ucsi;
+	struct i2c_client *ccgx_client;
 };
 
 static void gpu_enable_i2c_bus(struct gpu_i2c_dev *i2cd)
@@ -271,8 +272,6 @@ static const struct property_entry ccgx_props[] = {
 
 static int gpu_populate_client(struct gpu_i2c_dev *i2cd, int irq)
 {
-	struct i2c_client *ccgx_client;
-
 	i2cd->gpu_ccgx_ucsi = devm_kzalloc(i2cd->dev,
 					   sizeof(*i2cd->gpu_ccgx_ucsi),
 					   GFP_KERNEL);
@@ -284,8 +283,8 @@ static int gpu_populate_client(struct gpu_i2c_dev *i2cd, int irq)
 	i2cd->gpu_ccgx_ucsi->addr = 0x8;
 	i2cd->gpu_ccgx_ucsi->irq = irq;
 	i2cd->gpu_ccgx_ucsi->properties = ccgx_props;
-	ccgx_client = i2c_new_device(&i2cd->adapter, i2cd->gpu_ccgx_ucsi);
-	if (!ccgx_client)
+	i2cd->ccgx_client = i2c_new_device(&i2cd->adapter, i2cd->gpu_ccgx_ucsi);
+	if (!i2cd->ccgx_client)
 		return -ENODEV;
 
 	return 0;
@@ -375,6 +374,13 @@ static __maybe_unused int gpu_i2c_resume(struct device *dev)
 	struct gpu_i2c_dev *i2cd = dev_get_drvdata(dev);
 
 	gpu_enable_i2c_bus(i2cd);
+	/*
+	 * Runtime resume ccgx client so that it can see for any
+	 * connector change event. Old ccg firmware has known
+	 * issue of not triggering interrupt when a device is
+	 * connected to runtime resume the controller.
+	 */
+	pm_request_resume(&i2cd->ccgx_client->dev);
 	return 0;
 }
 
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH v2 5/5] usb: typec: ucsi: ccg: add runtime pm workaround
  2019-05-20 18:37 [PATCH v2 0/5] usb: typec: ucsi: ccg: add runtime pm support Ajay Gupta
                   ` (3 preceding siblings ...)
  2019-05-20 18:37 ` [PATCH v2 4/5] i2c: nvidia-gpu: resume ccgx i2c client Ajay Gupta
@ 2019-05-20 18:37 ` Ajay Gupta
  4 siblings, 0 replies; 10+ messages in thread
From: Ajay Gupta @ 2019-05-20 18:37 UTC (permalink / raw)
  To: heikki.krogerus, wsa; +Cc: linux-usb, linux-i2c, Ajay Gupta

From: Ajay Gupta <ajayg@nvidia.com>

Cypress USB Type-C CCGx controller firmware version 3.1.10
(which is being used in many NVIDIA GPU cards) has known issue of
not triggering interrupt when a USB device is hot plugged to runtime
resume the controller. If any GPU card gets latest kernel with runtime
pm support but does not get latest fixed firmware then also it should
continue to work and therefore a workaround is required to check for
any connector change event.

The workaround is that i2c bus driver will call pm_request_resume()
to runtime resume ucsi_ccg driver. CCG driver will call the ISR
for any connector change event for NVIDIA GPU card and only if it has
old CCG firmware with the known issue.

Signed-off-by: Ajay Gupta <ajayg@nvidia.com>
---
Changes from v1->v2:
	- Fixed comment style
	- Added new macros for NVIDIA old FW builds instead of structure
	  variable for them
 drivers/usb/typec/ucsi/ucsi_ccg.c | 80 +++++++++++++++++++++++++++++--
 1 file changed, 76 insertions(+), 4 deletions(-)

diff --git a/drivers/usb/typec/ucsi/ucsi_ccg.c b/drivers/usb/typec/ucsi/ucsi_ccg.c
index cc7094ecda2d..0b9f60861497 100644
--- a/drivers/usb/typec/ucsi/ucsi_ccg.c
+++ b/drivers/usb/typec/ucsi/ucsi_ccg.c
@@ -109,12 +109,21 @@ struct version_format {
 	__le16 build;
 	u8 patch;
 	u8 ver;
+#define CCG_VERSION_PATCH(x) ((x) << 16)
+#define CCG_VERSION(x)	((x) << 24)
 #define CCG_VERSION_MIN_SHIFT (0)
 #define CCG_VERSION_MIN_MASK (0xf << CCG_VERSION_MIN_SHIFT)
 #define CCG_VERSION_MAJ_SHIFT (4)
 #define CCG_VERSION_MAJ_MASK (0xf << CCG_VERSION_MAJ_SHIFT)
 } __packed;
 
+/*
+ * Firmware version 3.1.10 or earlier, built for NVIDIA has known issue
+ * of missing interrupt when a device is connected for runtime resume
+ */
+#define CCG_FW_BUILD_NVIDIA	(('n' << 8) | 'v')
+#define CCG_OLD_FW_VERSION	(CCG_VERSION(0x31) | CCG_VERSION_PATCH(10))
+
 struct version_info {
 	struct version_format base;
 	struct version_format app;
@@ -172,6 +181,7 @@ struct ucsi_ccg {
 	struct ccg_dev_info info;
 	/* version info for boot, primary and secondary */
 	struct version_info version[FW2 + 1];
+	u32 fw_version;
 	/* CCG HPI communication flags */
 	unsigned long flags;
 #define RESET_PENDING	0
@@ -185,6 +195,8 @@ struct ucsi_ccg {
 
 	/* fw build with vendor information */
 	u16 fw_build;
+	bool run_isr; /* flag to call ISR routine during resume */
+	struct work_struct pm_work;
 };
 
 static int ccg_read(struct ucsi_ccg *uc, u16 rab, u8 *data, u32 len)
@@ -212,6 +224,18 @@ static int ccg_read(struct ucsi_ccg *uc, u16 rab, u8 *data, u32 len)
 	if (quirks && quirks->max_read_len)
 		max_read_len = quirks->max_read_len;
 
+	if (uc->fw_build == CCG_FW_BUILD_NVIDIA &&
+	    uc->fw_version <= CCG_OLD_FW_VERSION) {
+		mutex_lock(&uc->lock);
+		/*
+		 * Do not schedule pm_work to run ISR in
+		 * ucsi_ccg_runtime_resume() after pm_runtime_get_sync()
+		 * since we are already in ISR path.
+		 */
+		uc->run_isr = false;
+		mutex_unlock(&uc->lock);
+	}
+
 	pm_runtime_get_sync(uc->dev);
 	while (rem_len > 0) {
 		msgs[1].buf = &data[len - rem_len];
@@ -254,6 +278,18 @@ static int ccg_write(struct ucsi_ccg *uc, u16 rab, u8 *data, u32 len)
 	msgs[0].len = len + sizeof(rab);
 	msgs[0].buf = buf;
 
+	if (uc->fw_build == CCG_FW_BUILD_NVIDIA &&
+	    uc->fw_version <= CCG_OLD_FW_VERSION) {
+		mutex_lock(&uc->lock);
+		/*
+		 * Do not schedule pm_work to run ISR in
+		 * ucsi_ccg_runtime_resume() after pm_runtime_get_sync()
+		 * since we are already in ISR path.
+		 */
+		uc->run_isr = false;
+		mutex_unlock(&uc->lock);
+	}
+
 	pm_runtime_get_sync(uc->dev);
 	status = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
 	if (status < 0) {
@@ -383,6 +419,13 @@ static irqreturn_t ccg_irq_handler(int irq, void *data)
 	return IRQ_HANDLED;
 }
 
+static void ccg_pm_workaround_work(struct work_struct *pm_work)
+{
+	struct ucsi_ccg *uc = container_of(pm_work, struct ucsi_ccg, pm_work);
+
+	ucsi_notify(uc->ucsi);
+}
+
 static int get_fw_info(struct ucsi_ccg *uc)
 {
 	int err;
@@ -392,6 +435,9 @@ static int get_fw_info(struct ucsi_ccg *uc)
 	if (err < 0)
 		return err;
 
+	uc->fw_version = CCG_VERSION(uc->version[FW2].app.ver) |
+			CCG_VERSION_PATCH(uc->version[FW2].app.patch);
+
 	err = ccg_read(uc, CCGX_RAB_DEVICE_MODE, (u8 *)(&uc->info),
 		       sizeof(uc->info));
 	if (err < 0)
@@ -740,11 +786,12 @@ static bool ccg_check_fw_version(struct ucsi_ccg *uc, const char *fw_name,
 	}
 
 	/* compare input version with FWCT version */
-	cur_version = le16_to_cpu(app->build) | app->patch << 16 |
-			app->ver << 24;
+	cur_version = le16_to_cpu(app->build) | CCG_VERSION_PATCH(app->patch) |
+			CCG_VERSION(app->ver);
 
-	new_version = le16_to_cpu(fw_cfg.app.build) | fw_cfg.app.patch << 16 |
-			fw_cfg.app.ver << 24;
+	new_version = le16_to_cpu(fw_cfg.app.build) |
+			CCG_VERSION_PATCH(fw_cfg.app.patch) |
+			CCG_VERSION(fw_cfg.app.ver);
 
 	if (!ccg_check_vendor_version(uc, app, &fw_cfg))
 		goto out_release_firmware;
@@ -1084,8 +1131,10 @@ static int ucsi_ccg_probe(struct i2c_client *client,
 	uc->ppm.sync = ucsi_ccg_sync;
 	uc->dev = dev;
 	uc->client = client;
+	uc->run_isr = true;
 	mutex_init(&uc->lock);
 	INIT_WORK(&uc->work, ccg_update_firmware);
+	INIT_WORK(&uc->pm_work, ccg_pm_workaround_work);
 
 	/* Only fail FW flashing when FW build information is not provided */
 	status = device_property_read_u16(dev, "ccgx,firmware-build",
@@ -1153,6 +1202,7 @@ static int ucsi_ccg_remove(struct i2c_client *client)
 {
 	struct ucsi_ccg *uc = i2c_get_clientdata(client);
 
+	cancel_work_sync(&uc->pm_work);
 	cancel_work_sync(&uc->work);
 	ucsi_unregister_ppm(uc->ucsi);
 	pm_runtime_disable(uc->dev);
@@ -1198,6 +1248,28 @@ static int ucsi_ccg_runtime_suspend(struct device *dev)
 
 static int ucsi_ccg_runtime_resume(struct device *dev)
 {
+	struct i2c_client *client = to_i2c_client(dev);
+	struct ucsi_ccg *uc = i2c_get_clientdata(client);
+	bool schedule = true;
+
+	/*
+	 * Firmware version 3.1.10 or earlier, built for NVIDIA has known issue
+	 * of missing interrupt when a device is connected for runtime resume.
+	 * Schedule a work to call ISR as a workaround.
+	 */
+	if (uc->fw_build == CCG_FW_BUILD_NVIDIA &&
+	    uc->fw_version <= CCG_OLD_FW_VERSION) {
+		mutex_lock(&uc->lock);
+		if (!uc->run_isr) {
+			uc->run_isr = true;
+			schedule = false;
+		}
+		mutex_unlock(&uc->lock);
+
+		if (schedule)
+			schedule_work(&uc->pm_work);
+	}
+
 	return 0;
 }
 
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* Re: [PATCH v2 3/5] usb: typec: ucsi: ccg: enable runtime pm support
  2019-05-20 18:37 ` [PATCH v2 3/5] usb: typec: ucsi: ccg: enable " Ajay Gupta
@ 2019-05-21 13:37   ` Heikki Krogerus
  2019-05-21 13:49     ` Heikki Krogerus
  2019-05-21 17:44     ` Ajay Gupta
  0 siblings, 2 replies; 10+ messages in thread
From: Heikki Krogerus @ 2019-05-21 13:37 UTC (permalink / raw)
  To: Ajay Gupta; +Cc: wsa, linux-usb, linux-i2c, Ajay Gupta

Hi,

On Mon, May 20, 2019 at 11:37:48AM -0700, Ajay Gupta wrote:
> +static int ucsi_ccg_resume(struct device *dev)
> +{
> +	struct i2c_client *client = to_i2c_client(dev);
> +	struct ucsi_ccg *uc = i2c_get_clientdata(client);
> +	struct ucsi *ucsi = uc->ucsi;
> +	struct ucsi_control c;
> +	int ret;
> +
> +	/* restore UCSI notification enable mask */
> +	UCSI_CMD_SET_NTFY_ENABLE(c, UCSI_ENABLE_NTFY_ALL);
> +	ret = ucsi_send_command(ucsi, &c, NULL, 0);
> +	if (ret < 0) {
> +		dev_err(uc->dev, "%s: failed to set notification enable - %d\n",
> +			__func__, ret);
> +	}
> +	return 0;
> +}

I would prefer that we did this for all methods in ucsi.c, not just
ccgx. Could you add resume callback to struct ucsi_ppm, and then call
it here.

> +static int ucsi_ccg_runtime_suspend(struct device *dev)
> +{
> +	return 0;
> +}
> +
> +static int ucsi_ccg_runtime_resume(struct device *dev)
> +{
> +	return 0;
> +}
> +
> +static int ucsi_ccg_runtime_idle(struct device *dev)
> +{
> +	return 0;
> +}
> +
> +static const struct dev_pm_ops ucsi_ccg_pm = {
> +	.suspend = ucsi_ccg_suspend,
> +	.resume = ucsi_ccg_resume,
> +	.runtime_suspend = ucsi_ccg_runtime_suspend,
> +	.runtime_resume = ucsi_ccg_runtime_resume,
> +	.runtime_idle = ucsi_ccg_runtime_idle,
> +};
> +
>  static struct i2c_driver ucsi_ccg_driver = {
>  	.driver = {
>  		.name = "ucsi_ccg",
> +		.pm = &ucsi_ccg_pm,
>  	},
>  	.probe = ucsi_ccg_probe,
>  	.remove = ucsi_ccg_remove,

thanks,

-- 
heikki

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH v2 3/5] usb: typec: ucsi: ccg: enable runtime pm support
  2019-05-21 13:37   ` Heikki Krogerus
@ 2019-05-21 13:49     ` Heikki Krogerus
  2019-05-21 17:44     ` Ajay Gupta
  1 sibling, 0 replies; 10+ messages in thread
From: Heikki Krogerus @ 2019-05-21 13:49 UTC (permalink / raw)
  To: Ajay Gupta; +Cc: wsa, linux-usb, linux-i2c, Ajay Gupta

On Tue, May 21, 2019 at 04:37:27PM +0300, Heikki Krogerus wrote:
> Hi,
> 
> On Mon, May 20, 2019 at 11:37:48AM -0700, Ajay Gupta wrote:
> > +static int ucsi_ccg_resume(struct device *dev)
> > +{
> > +	struct i2c_client *client = to_i2c_client(dev);
> > +	struct ucsi_ccg *uc = i2c_get_clientdata(client);
> > +	struct ucsi *ucsi = uc->ucsi;
> > +	struct ucsi_control c;
> > +	int ret;
> > +
> > +	/* restore UCSI notification enable mask */
> > +	UCSI_CMD_SET_NTFY_ENABLE(c, UCSI_ENABLE_NTFY_ALL);
> > +	ret = ucsi_send_command(ucsi, &c, NULL, 0);
> > +	if (ret < 0) {
> > +		dev_err(uc->dev, "%s: failed to set notification enable - %d\n",
> > +			__func__, ret);
> > +	}
> > +	return 0;
> > +}
> 
> I would prefer that we did this for all methods in ucsi.c, not just
> ccgx. Could you add resume callback to struct ucsi_ppm, and then call
> it here.
> 
> > +static int ucsi_ccg_runtime_suspend(struct device *dev)
> > +{
> > +	return 0;
> > +}
> > +
> > +static int ucsi_ccg_runtime_resume(struct device *dev)
> > +{
> > +	return 0;
> > +}
> > +
> > +static int ucsi_ccg_runtime_idle(struct device *dev)
> > +{
> > +	return 0;
> > +}

Oh yes, and do you really need to supply all of those stubs?

thanks,

-- 
heikki

^ permalink raw reply	[flat|nested] 10+ messages in thread

* RE: [PATCH v2 3/5] usb: typec: ucsi: ccg: enable runtime pm support
  2019-05-21 13:37   ` Heikki Krogerus
  2019-05-21 13:49     ` Heikki Krogerus
@ 2019-05-21 17:44     ` Ajay Gupta
  2019-05-22 11:12       ` Heikki Krogerus
  1 sibling, 1 reply; 10+ messages in thread
From: Ajay Gupta @ 2019-05-21 17:44 UTC (permalink / raw)
  To: Heikki Krogerus, Ajay Gupta; +Cc: wsa, linux-usb, linux-i2c

Hi Heikki

> > +static int ucsi_ccg_resume(struct device *dev) {
> > +	struct i2c_client *client = to_i2c_client(dev);
> > +	struct ucsi_ccg *uc = i2c_get_clientdata(client);
> > +	struct ucsi *ucsi = uc->ucsi;
> > +	struct ucsi_control c;
> > +	int ret;
> > +
> > +	/* restore UCSI notification enable mask */
> > +	UCSI_CMD_SET_NTFY_ENABLE(c, UCSI_ENABLE_NTFY_ALL);
> > +	ret = ucsi_send_command(ucsi, &c, NULL, 0);
> > +	if (ret < 0) {
> > +		dev_err(uc->dev, "%s: failed to set notification enable - %d\n",
> > +			__func__, ret);
> > +	}
> > +	return 0;
> > +}
> 
> I would prefer that we did this for all methods in ucsi.c, not just ccgx. Could you
> add resume callback to struct ucsi_ppm, and then call it here.
struct ucsi_ppm currently have .sync() and .cmd() callback which is implemented by
ucsi_ccg and ucsi_acpi and invoked by usci.c. 

Is it okay to add a callback in this structure and implement inside ucsi.c and invoke
from ucsi_ccg and ucsi_acpi? OR we can just add a function in ucsi.c and export it
and use it from ucsi_ccg and ucsi_acpi?

> 
> > +static int ucsi_ccg_runtime_suspend(struct device *dev) {
> > +	return 0;
> > +}
> > +
> > +static int ucsi_ccg_runtime_resume(struct device *dev) {
> > +	return 0;
> > +}
> > +
> > +static int ucsi_ccg_runtime_idle(struct device *dev) {
> > +	return 0;
> > +}

> > Oh yes, and do you really need to supply all of those stubs?
We can drop ucsi_ccg_runtime_idle() but we do need
ucsi_ccg_runtime_suspend() and ucsi_ccg_runtime_resume() for
runtime pm functionality.

Thanks
Ajay
> nvpublic
> > +
> > +static const struct dev_pm_ops ucsi_ccg_pm = {
> > +	.suspend = ucsi_ccg_suspend,
> > +	.resume = ucsi_ccg_resume,
> > +	.runtime_suspend = ucsi_ccg_runtime_suspend,
> > +	.runtime_resume = ucsi_ccg_runtime_resume,
> > +	.runtime_idle = ucsi_ccg_runtime_idle, };
> > +
> >  static struct i2c_driver ucsi_ccg_driver = {
> >  	.driver = {
> >  		.name = "ucsi_ccg",
> > +		.pm = &ucsi_ccg_pm,
> >  	},
> >  	.probe = ucsi_ccg_probe,
> >  	.remove = ucsi_ccg_remove,
> 
> thanks,
> 
> --
> heikki

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH v2 3/5] usb: typec: ucsi: ccg: enable runtime pm support
  2019-05-21 17:44     ` Ajay Gupta
@ 2019-05-22 11:12       ` Heikki Krogerus
  0 siblings, 0 replies; 10+ messages in thread
From: Heikki Krogerus @ 2019-05-22 11:12 UTC (permalink / raw)
  To: Ajay Gupta; +Cc: Ajay Gupta, wsa, linux-usb, linux-i2c

On Tue, May 21, 2019 at 05:44:50PM +0000, Ajay Gupta wrote:
> Hi Heikki
> 
> > > +static int ucsi_ccg_resume(struct device *dev) {
> > > +	struct i2c_client *client = to_i2c_client(dev);
> > > +	struct ucsi_ccg *uc = i2c_get_clientdata(client);
> > > +	struct ucsi *ucsi = uc->ucsi;
> > > +	struct ucsi_control c;
> > > +	int ret;
> > > +
> > > +	/* restore UCSI notification enable mask */
> > > +	UCSI_CMD_SET_NTFY_ENABLE(c, UCSI_ENABLE_NTFY_ALL);
> > > +	ret = ucsi_send_command(ucsi, &c, NULL, 0);
> > > +	if (ret < 0) {
> > > +		dev_err(uc->dev, "%s: failed to set notification enable - %d\n",
> > > +			__func__, ret);
> > > +	}
> > > +	return 0;
> > > +}
> > 
> > I would prefer that we did this for all methods in ucsi.c, not just ccgx. Could you
> > add resume callback to struct ucsi_ppm, and then call it here.
> struct ucsi_ppm currently have .sync() and .cmd() callback which is implemented by
> ucsi_ccg and ucsi_acpi and invoked by usci.c. 
> 
> Is it okay to add a callback in this structure and implement inside ucsi.c and invoke
> from ucsi_ccg and ucsi_acpi? OR we can just add a function in ucsi.c and export it
> and use it from ucsi_ccg and ucsi_acpi?

Right! Export the function. Sorry.

thanks,

-- 
heikki

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2019-05-22 11:12 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-05-20 18:37 [PATCH v2 0/5] usb: typec: ucsi: ccg: add runtime pm support Ajay Gupta
2019-05-20 18:37 ` [PATCH v2 1/5] i2c: nvidia-gpu: refactor master_xfer Ajay Gupta
2019-05-20 18:37 ` [PATCH v2 2/5] i2c: nvidia-gpu: add runtime pm support Ajay Gupta
2019-05-20 18:37 ` [PATCH v2 3/5] usb: typec: ucsi: ccg: enable " Ajay Gupta
2019-05-21 13:37   ` Heikki Krogerus
2019-05-21 13:49     ` Heikki Krogerus
2019-05-21 17:44     ` Ajay Gupta
2019-05-22 11:12       ` Heikki Krogerus
2019-05-20 18:37 ` [PATCH v2 4/5] i2c: nvidia-gpu: resume ccgx i2c client Ajay Gupta
2019-05-20 18:37 ` [PATCH v2 5/5] usb: typec: ucsi: ccg: add runtime pm workaround Ajay Gupta

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.