Skip to contentSkip to conversation

platform/chrome: cros_ec_typec: Use workqueue for port update

PATCH Superseded Prashant Malani 2 participants 6 messages Jun 29, 2020

Subsystems (3)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/2 platform/chrome: cros_ec_typec: Use workqueue for port update

Use a work queue to call the port update routines, instead of doing it directly in the PD notifier callback. This will prevent other drivers with PD notifier callbacks from being blocked on the port update routine completing.

---

Changes in v2:
- No changes.

 drivers/platform/chrome/cros_ec_typec.c | 28 ++++++++++++++++++++-----
 1 file changed, 23 insertions(+), 5 deletions(-)
drivers/platform/chrome/cros_ec_typec.c +23 -5
index 0c041b79cbba..630170fb2cbe 100644
@@ -58,6 +58,7 @@ struct cros_typec_data {
 	/* Array of ports, indexed by port number. */
 	struct cros_typec_port *ports[EC_USB_PD_MAX_PORTS];
 	struct notifier_block nb;
+	struct work_struct port_work;
 };
 
 static int cros_typec_parse_port_props(struct typec_capability *cap,
@@ -619,18 +620,29 @@ static int cros_typec_get_cmd_version(struct cros_typec_data *typec)
 	return 0;
 }
 
-static int cros_ec_typec_event(struct notifier_block *nb,
-			       unsigned long host_event, void *_notify)
+static void cros_typec_port_work(struct work_struct *work)
 {
-	struct cros_typec_data *typec = container_of(nb, struct cros_typec_data,
-						     nb);
-	int ret, i;
+	struct cros_typec_data *typec = container_of(work,
+						     struct cros_typec_data,
+						     port_work);
+	int ret;
+	int i;
 
 	for (i = 0; i < typec->num_ports; i++) {
 		ret = cros_typec_port_update(typec, i);
 		if (ret < 0)
 			dev_warn(typec->dev, "Update failed for port: %d\n", i);
 	}
+}
+
+
+static int cros_ec_typec_event(struct notifier_block *nb,
+			       unsigned long host_event, void *_notify)
+{
+	struct cros_typec_data *typec = container_of(nb, struct cros_typec_data,
+						     nb);
+
+	schedule_work(&typec->port_work);
 
 	return NOTIFY_OK;
 }
@@ -689,6 +701,12 @@ static int cros_typec_probe(struct platform_device *pdev)
 	if (ret < 0)
 		return ret;
 
+	INIT_WORK(&typec->port_work, cros_typec_port_work);
+
+	/*
+	 * Safe to call port update here, since we haven't registered the
+	 * PD notifier yet.
+	 */
 	for (i = 0; i < typec->num_ports; i++) {
 		ret = cros_typec_port_update(typec, i);
 		if (ret < 0)
Signature · 1 line
2.27.0.212.ge8ba1cc988-goog
#1 lore raw
Quoted 31 lines · Prashant Malani

Use a work queue to call the port update routines, instead of doing it directly in the PD notifier callback. This will prevent other drivers with PD notifier callbacks from being blocked on the port update routine completing.

---

Changes in v2:
- No changes.

 drivers/platform/chrome/cros_ec_typec.c | 28 ++++++++++++++++++++-----
 1 file changed, 23 insertions(+), 5 deletions(-)
drivers/platform/chrome/cros_ec_typec.c +1 -0
index 0c041b79cbba..630170fb2cbe 100644
@@ -58,6 +58,7 @@ struct cros_typec_data {
        /* Array of ports, indexed by port number. */
        struct cros_typec_port *ports[EC_USB_PD_MAX_PORTS];
        struct notifier_block nb;
+       struct work_struct port_work;
 };
 
 static int cros_typec_parse_port_props(struct typec_capability *cap,
@@ -619,18 +620,29 @@ static int cros_typec_get_cmd_version(struct cros_typec_data *typec)
        return 0;
 }
 

drivers/platform/chrome/cros_ec_typec.c static int cros_typec_get_cmd_version(struct cros_typec_data *typec) · line 627

Quoted 10 lines
drivers/platform/chrome/cros_ec_typec.c +4 -5
-static int cros_ec_typec_event(struct notifier_block *nb,
-                              unsigned long host_event, void *_notify)
+static void cros_typec_port_work(struct work_struct *work)
 {
-       struct cros_typec_data *typec = container_of(nb, struct cros_typec_data,
-                                                    nb);
-       int ret, i;
+       struct cros_typec_data *typec = container_of(work,
+                                                    struct cros_typec_data,
+                                                    port_work);

Nit, but the upstream line length limit is now 100 characters, and this is a perfect example where this would come handy.

Quoted 7 lines
drivers/platform/chrome/cros_ec_typec.c +2 -0
+       int ret;
+       int i;
 
        for (i = 0; i < typec->num_ports; i++) {
                ret = cros_typec_port_update(typec, i);
                if (ret < 0)
                        dev_warn(typec->dev, "Update failed for port: %d\n", i);

drivers/platform/chrome/cros_ec_typec.c static int cros_typec_get_cmd_version(struct cros_typec_data *typec) · line 644

Quoted 10 lines
drivers/platform/chrome/cros_ec_typec.c +9 -0
        }
+}
+
+
+static int cros_ec_typec_event(struct notifier_block *nb,
+                              unsigned long host_event, void *_notify)
+{
+       struct cros_typec_data *typec = container_of(nb, struct cros_typec_data,
+                                                    nb);
+

... and even more so here.

Quoted 20 lines
drivers/platform/chrome/cros_ec_typec.c +7 -1
+       schedule_work(&typec->port_work);
 
        return NOTIFY_OK;
 }
@@ -689,6 +701,12 @@ static int cros_typec_probe(struct platform_device *pdev)
        if (ret < 0)
                return ret;
 
+       INIT_WORK(&typec->port_work, cros_typec_port_work);
+
+       /*
+        * Safe to call port update here, since we haven't registered the
+        * PD notifier yet.
+        */
        for (i = 0; i < typec->num_ports; i++) {
                ret = cros_typec_port_update(typec, i);
                if (ret < 0)
--

2.27.0.212.ge8ba1cc988-goog

#2 lore raw

Hi Guenter,

Thanks for the review!

Quoted 33 lines · Guenter Roeck
Quoted 31 lines · Prashant Malani

Use a work queue to call the port update routines, instead of doing it directly in the PD notifier callback. This will prevent other drivers with PD notifier callbacks from being blocked on the port update routine completing.

---

Changes in v2:
- No changes.

 drivers/platform/chrome/cros_ec_typec.c | 28 ++++++++++++++++++++-----
 1 file changed, 23 insertions(+), 5 deletions(-)
drivers/platform/chrome/cros_ec_typec.c +1 -0
index 0c041b79cbba..630170fb2cbe 100644
@@ -58,6 +58,7 @@ struct cros_typec_data {
        /* Array of ports, indexed by port number. */
        struct cros_typec_port *ports[EC_USB_PD_MAX_PORTS];
        struct notifier_block nb;
+       struct work_struct port_work;
 };
 
 static int cros_typec_parse_port_props(struct typec_capability *cap,
@@ -619,18 +620,29 @@ static int cros_typec_get_cmd_version(struct cros_typec_data *typec)
        return 0;
 }
 
Quoted 13 lines

drivers/platform/chrome/cros_ec_typec.c static int cros_typec_get_cmd_version(struct cros_typec_data *typec) · line 627

Quoted 10 lines
drivers/platform/chrome/cros_ec_typec.c +4 -5
-static int cros_ec_typec_event(struct notifier_block *nb,
-                              unsigned long host_event, void *_notify)
+static void cros_typec_port_work(struct work_struct *work)
 {
-       struct cros_typec_data *typec = container_of(nb, struct cros_typec_data,
-                                                    nb);
-       int ret, i;
+       struct cros_typec_data *typec = container_of(work,
+                                                    struct cros_typec_data,
+                                                    port_work);

Nit, but the upstream line length limit is now 100 characters, and this is a perfect example where this would come handy.

Done.

Quoted 4 lines
Quoted 4 lines
drivers/platform/chrome/cros_ec_typec.c +2 -0
+       int ret;
+       int i;
 
Quoted 15 lines
Quoted 4 lines
drivers/platform/chrome/cros_ec_typec.c +0 -0
        for (i = 0; i < typec->num_ports; i++) {
                ret = cros_typec_port_update(typec, i);
                if (ret < 0)
                        dev_warn(typec->dev, "Update failed for port: %d\n", i);

drivers/platform/chrome/cros_ec_typec.c static int cros_typec_get_cmd_version(struct cros_typec_data *typec) · line 644

Quoted 10 lines
drivers/platform/chrome/cros_ec_typec.c +9 -0
        }
+}
+
+
+static int cros_ec_typec_event(struct notifier_block *nb,
+                              unsigned long host_event, void *_notify)
+{
+       struct cros_typec_data *typec = container_of(nb, struct cros_typec_data,
+                                                    nb);
+

... and even more so here.

Done.

Quoted 21 lines
Quoted 21 lines
drivers/platform/chrome/cros_ec_typec.c +7 -1
+       schedule_work(&typec->port_work);
 
        return NOTIFY_OK;
 }
@@ -689,6 +701,12 @@ static int cros_typec_probe(struct platform_device *pdev)
        if (ret < 0)
                return ret;
 
+       INIT_WORK(&typec->port_work, cros_typec_port_work);
+
+       /*
+        * Safe to call port update here, since we haven't registered the
+        * PD notifier yet.
+        */
        for (i = 0; i < typec->num_ports; i++) {
                ret = cros_typec_port_update(typec, i);
                if (ret < 0)
--

2.27.0.212.ge8ba1cc988-goog

#3 lore raw

PATCH v2 2/2 platform/chrome: cros_ec_typec: Add PM support

In reply to Prashant Malani

Define basic suspend resume functions for cros-ec-typec. On suspend, we simply ensure that any pending port update work is completed, and on resume, we re-poll the port state to account for any changes/disconnections that might have occurred during suspend.

---

Changes in v2:
- Remove #ifdef-ery, add __maybe_unused tag to functions.

 drivers/platform/chrome/cros_ec_typec.c | 26 +++++++++++++++++++++++++
 1 file changed, 26 insertions(+)
drivers/platform/chrome/cros_ec_typec.c +26 -0
index 630170fb2cbe..b2e7e928e788 100644
@@ -725,11 +725,37 @@ static int cros_typec_probe(struct platform_device *pdev)
 	return ret;
 }
 
+static int __maybe_unused cros_typec_suspend(struct device *dev)
+{
+	struct cros_typec_data *typec = dev_get_drvdata(dev);
+
+	cancel_work_sync(&typec->port_work);
+
+	return 0;
+}
+
+static int __maybe_unused cros_typec_resume(struct device *dev)
+{
+	struct cros_typec_data *typec = dev_get_drvdata(dev);
+
+	/* Refresh port state. */
+	schedule_work(&typec->port_work);
+
+	return 0;
+}
+
+static const struct dev_pm_ops cros_typec_pm_ops = {
+	SET_SYSTEM_SLEEP_PM_OPS(cros_typec_suspend, cros_typec_resume)
+};
+
+#define DEV_PM_OPS	(&cros_typec_pm_ops)
+
 static struct platform_driver cros_typec_driver = {
 	.driver	= {
 		.name = DRV_NAME,
 		.acpi_match_table = ACPI_PTR(cros_typec_acpi_id),
 		.of_match_table = of_match_ptr(cros_typec_of_match),
+		.pm = DEV_PM_OPS,
 	},
 	.probe = cros_typec_probe,
 };
Signature · 1 line
2.27.0.212.ge8ba1cc988-goog
#4 lore raw

Re: [PATCH v2 2/2] platform/chrome: cros_ec_typec: Add PM support

Quoted 37 lines · Prashant Malani

Define basic suspend resume functions for cros-ec-typec. On suspend, we simply ensure that any pending port update work is completed, and on resume, we re-poll the port state to account for any changes/disconnections that might have occurred during suspend.

---

Changes in v2:
- Remove #ifdef-ery, add __maybe_unused tag to functions.

 drivers/platform/chrome/cros_ec_typec.c | 26 +++++++++++++++++++++++++
 1 file changed, 26 insertions(+)
drivers/platform/chrome/cros_ec_typec.c +14 -0
index 630170fb2cbe..b2e7e928e788 100644
@@ -725,11 +725,37 @@ static int cros_typec_probe(struct platform_device *pdev)
        return ret;
 }
 
+static int __maybe_unused cros_typec_suspend(struct device *dev)
+{
+       struct cros_typec_data *typec = dev_get_drvdata(dev);
+
+       cancel_work_sync(&typec->port_work);
+
+       return 0;
+}
+
+static int __maybe_unused cros_typec_resume(struct device *dev)
+{
+       struct cros_typec_data *typec = dev_get_drvdata(dev);
+
+       /* Refresh port state. */

drivers/platform/chrome/cros_ec_typec.c static int cros_typec_probe(struct platform_device *pdev) · line 751

Quoted 10 lines
drivers/platform/chrome/cros_ec_typec.c +10 -0
+       schedule_work(&typec->port_work);
+
+       return 0;
+}
+
+static const struct dev_pm_ops cros_typec_pm_ops = {
+       SET_SYSTEM_SLEEP_PM_OPS(cros_typec_suspend, cros_typec_resume)
+};
+
+#define DEV_PM_OPS     (&cros_typec_pm_ops)

I don't think this define adds any value.

Quoted 13 lines
drivers/platform/chrome/cros_ec_typec.c +2 -1
+
 static struct platform_driver cros_typec_driver = {
        .driver = {
                .name = DRV_NAME,
                .acpi_match_table = ACPI_PTR(cros_typec_acpi_id),
                .of_match_table = of_match_ptr(cros_typec_of_match),
+               .pm = DEV_PM_OPS,
        },
        .probe = cros_typec_probe,
 };
--

2.27.0.212.ge8ba1cc988-goog

#5 lore raw

Re: [PATCH v2 2/2] platform/chrome: cros_ec_typec: Add PM support

Hi Guenter,

Thanks for reviewing the patch.

Quoted 43 lines · Guenter Roeck
Quoted 41 lines · Prashant Malani

Define basic suspend resume functions for cros-ec-typec. On suspend, we simply ensure that any pending port update work is completed, and on resume, we re-poll the port state to account for any changes/disconnections that might have occurred during suspend.

---

Changes in v2:
- Remove #ifdef-ery, add __maybe_unused tag to functions.

 drivers/platform/chrome/cros_ec_typec.c | 26 +++++++++++++++++++++++++
 1 file changed, 26 insertions(+)
drivers/platform/chrome/cros_ec_typec.c +18 -0
index 630170fb2cbe..b2e7e928e788 100644
@@ -725,11 +725,37 @@ static int cros_typec_probe(struct platform_device *pdev)
        return ret;
 }
 
+static int __maybe_unused cros_typec_suspend(struct device *dev)
+{
+       struct cros_typec_data *typec = dev_get_drvdata(dev);
+
+       cancel_work_sync(&typec->port_work);
+
+       return 0;
+}
+
+static int __maybe_unused cros_typec_resume(struct device *dev)
+{
+       struct cros_typec_data *typec = dev_get_drvdata(dev);
+
+       /* Refresh port state. */
+       schedule_work(&typec->port_work);
+
+       return 0;
+}
Quoted 8 lines

drivers/platform/chrome/cros_ec_typec.c static int cros_typec_probe(struct platform_device *pdev) · line 751

Quoted 6 lines
drivers/platform/chrome/cros_ec_typec.c +6 -0
+
+static const struct dev_pm_ops cros_typec_pm_ops = {
+       SET_SYSTEM_SLEEP_PM_OPS(cros_typec_suspend, cros_typec_resume)
+};
+
+#define DEV_PM_OPS     (&cros_typec_pm_ops)

I don't think this define adds any value.

Right. Sorry, got sloppy here. Will push another patchset. Thanks for catching this.

Best regards,

Quoted 14 lines
Quoted 14 lines
drivers/platform/chrome/cros_ec_typec.c +2 -1
+
 static struct platform_driver cros_typec_driver = {
        .driver = {
                .name = DRV_NAME,
                .acpi_match_table = ACPI_PTR(cros_typec_acpi_id),
                .of_match_table = of_match_ptr(cros_typec_of_match),
+               .pm = DEV_PM_OPS,
        },
        .probe = cros_typec_probe,
 };
--

2.27.0.212.ge8ba1cc988-goog