2019-11-25 20:49:16

by Raul Rangel

[permalink] [raw]
Subject: [PATCH v2 0/2] Convert cros-ec-i2c-tunnel to MFD Cell

Having the cros-ec-i2c-tunnel behave as a MFD Cell fixes a race condition
that could happen if the cros-ec-i2c-tunnel gets probed before the
cros_ec_lpc init has finished.

Changes in v2:
- Extracted platform_device_info into its own variable.
- Moved i2c tunnel into cros_ec_platform_cells
- Requires https://lkml.org/lkml/2019/11/21/208 to correctly enumerate.

Raul E Rangel (2):
platform/chrome: cros_ec: Pass firmware node to MFD device
platform/chrome: i2c: i2c-cros-ec-tunnel: Convert i2c tunnel to MFD
Cell

drivers/i2c/busses/i2c-cros-ec-tunnel.c | 36 +++++++++----------------
drivers/mfd/cros_ec_dev.c | 9 +++++++
drivers/platform/chrome/cros_ec.c | 14 +++++++---
3 files changed, 33 insertions(+), 26 deletions(-)

--
2.24.0.432.g9d3f5f5b63-goog


2019-11-25 20:51:40

by Raul Rangel

[permalink] [raw]
Subject: [PATCH v2 1/2] platform/chrome: cros_ec: Pass firmware node to MFD device

cros_ec_dev needs to have a firmware node associated with it so mfd
cells can be properly bound to the correct ACPI/DT nodes.

Signed-off-by: Raul E Rangel <[email protected]>
---

Before this patch:
$ find /sys/bus/platform/devices/cros-ec-* -iname firmware_node -exec ls -l '{}' \;
<nothing>

After this patch:
$ find /sys/bus/platform/devices/cros-ec-dev.0.auto/ -iname firmware_node -exec ls -l '{}' \;
/sys/bus/platform/devices/cros-ec-dev.0.auto/cros-ec-accel.1.auto/firmware_node -> ../../../../../../LNXSYSTM:00/LNXSYBUS:00/PNP0A08:00/device:1c/PNP0C09:00/GOOG0004:00
/sys/bus/platform/devices/cros-ec-dev.0.auto/cros-ec-chardev.7.auto/firmware_node -> ../../../../../../LNXSYSTM:00/LNXSYBUS:00/PNP0A08:00/device:1c/PNP0C09:00/GOOG0004:00
/sys/bus/platform/devices/cros-ec-dev.0.auto/cros-ec-debugfs.8.auto/firmware_node -> ../../../../../../LNXSYSTM:00/LNXSYBUS:00/PNP0A08:00/device:1c/PNP0C09:00/GOOG0004:00
/sys/bus/platform/devices/cros-ec-dev.0.auto/cros-ec-gyro.2.auto/firmware_node -> ../../../../../../LNXSYSTM:00/LNXSYBUS:00/PNP0A08:00/device:1c/PNP0C09:00/GOOG0004:00
/sys/bus/platform/devices/cros-ec-dev.0.auto/cros-ec-lid-angle.4.auto/firmware_node -> ../../../../../../LNXSYSTM:00/LNXSYBUS:00/PNP0A08:00/device:1c/PNP0C09:00/GOOG0004:00
/sys/bus/platform/devices/cros-ec-dev.0.auto/cros-ec-lightbar.9.auto/firmware_node -> ../../../../../../LNXSYSTM:00/LNXSYBUS:00/PNP0A08:00/device:1c/PNP0C09:00/GOOG0004:00
/sys/bus/platform/devices/cros-ec-dev.0.auto/cros-ec-pd-sysfs.10.auto/firmware_node -> ../../../../../../LNXSYSTM:00/LNXSYBUS:00/PNP0A08:00/device:1c/PNP0C09:00/GOOG0004:00
/sys/bus/platform/devices/cros-ec-dev.0.auto/cros-ec-ring.3.auto/firmware_node -> ../../../../../../LNXSYSTM:00/LNXSYBUS:00/PNP0A08:00/device:1c/PNP0C09:00/GOOG0004:00
/sys/bus/platform/devices/cros-ec-dev.0.auto/cros-ec-sysfs.11.auto/firmware_node -> ../../../../../../LNXSYSTM:00/LNXSYBUS:00/PNP0A08:00/device:1c/PNP0C09:00/GOOG0004:00
/sys/bus/platform/devices/cros-ec-dev.0.auto/cros-usbpd-charger.5.auto/firmware_node -> ../../../../../../LNXSYSTM:00/LNXSYBUS:00/PNP0A08:00/device:1c/PNP0C09:00/GOOG0004:00
/sys/bus/platform/devices/cros-ec-dev.0.auto/cros-usbpd-logger.6.auto/firmware_node -> ../../../../../../LNXSYSTM:00/LNXSYBUS:00/PNP0A08:00/device:1c/PNP0C09:00/GOOG0004:00
/sys/bus/platform/devices/cros-ec-dev.0.auto/firmware_node -> ../../../../../LNXSYSTM:00/LNXSYBUS:00/PNP0A08:00/device:1c/PNP0C09:00/GOOG0004:00

Changes in v2:
- Extracted platform_device_info into its own variable.

drivers/platform/chrome/cros_ec.c | 14 +++++++++++---
1 file changed, 11 insertions(+), 3 deletions(-)

diff --git a/drivers/platform/chrome/cros_ec.c b/drivers/platform/chrome/cros_ec.c
index 51d76037f52a..bb11bc5f73fb 100644
--- a/drivers/platform/chrome/cros_ec.c
+++ b/drivers/platform/chrome/cros_ec.c
@@ -131,6 +131,15 @@ int cros_ec_register(struct cros_ec_device *ec_dev)
{
struct device *dev = ec_dev->dev;
int err = 0;
+ struct platform_device_info ec_platform_info = {
+ .parent = ec_dev->dev,
+ .name = "cros-ec-dev",
+ .id = PLATFORM_DEVID_AUTO,
+ .data = &ec_p,
+ .size_data = sizeof(struct cros_ec_platform),
+ .fwnode = ec_dev->dev->fwnode,
+ .of_node_reused = 1
+ };

BLOCKING_INIT_NOTIFIER_HEAD(&ec_dev->event_notifier);

@@ -167,9 +176,8 @@ int cros_ec_register(struct cros_ec_device *ec_dev)
}

/* Register a platform device for the main EC instance */
- ec_dev->ec = platform_device_register_data(ec_dev->dev, "cros-ec-dev",
- PLATFORM_DEVID_AUTO, &ec_p,
- sizeof(struct cros_ec_platform));
+ ec_dev->ec = platform_device_register_full(&ec_platform_info);
+
if (IS_ERR(ec_dev->ec)) {
dev_err(ec_dev->dev,
"Failed to create CrOS EC platform device\n");
--
2.24.0.432.g9d3f5f5b63-goog

2019-11-25 20:52:02

by Raul Rangel

[permalink] [raw]
Subject: [PATCH v2 2/2] platform/chrome: i2c: i2c-cros-ec-tunnel: Convert i2c tunnel to MFD Cell

If the i2c-cros-ec-tunnel driver is compiled into the kernel, it is
possible that i2c-cros-ec-tunnel could be probed before cros_ec_XXX
has finished initializing and setting the drvdata. This would cause a
NULL pointer panic.

Converting this driver over to an MFD solves the problem and aligns with
where the cros_ec is going.

Signed-off-by: Raul E Rangel <[email protected]>
---
You can now see the device node lives under the mfd device.

$ find /sys/bus/platform/devices/cros-ec-dev.0.auto/cros-ec-i2c-tunnel.12.auto/ -iname firmware_node -exec ls -l '{}' \;
/sys/bus/platform/devices/cros-ec-dev.0.auto/cros-ec-i2c-tunnel.12.auto/firmware_node -> ../../../../../../LNXSYSTM:00/LNXSYBUS:00/PNP0A08:00/device:1c/PNP0C09:00/GOOG0004:00/GOOG0012:00
/sys/bus/platform/devices/cros-ec-dev.0.auto/cros-ec-i2c-tunnel.12.auto/i2c-9/firmware_node -> ../../../../../../../LNXSYSTM:00/LNXSYBUS:00/PNP0A08:00/device:1c/PNP0C09:00/GOOG0004:00/GOOG0012:00
/sys/bus/platform/devices/cros-ec-dev.0.auto/cros-ec-i2c-tunnel.12.auto/i2c-9/i2c-10EC5682:00/firmware_node -> ../../../../../../../../LNXSYSTM:00/LNXSYBUS:00/PNP0A08:00/device:1c/PNP0C09:00/GOOG0004:00/GOOG0012:00/10EC5682:00

Changes in v2:
- Moved i2c tunnel into cros_ec_platform_cells
- Requires https://lkml.org/lkml/2019/11/21/208 to correctly enumerate.

drivers/i2c/busses/i2c-cros-ec-tunnel.c | 36 +++++++++----------------
drivers/mfd/cros_ec_dev.c | 9 +++++++
2 files changed, 22 insertions(+), 23 deletions(-)

diff --git a/drivers/i2c/busses/i2c-cros-ec-tunnel.c b/drivers/i2c/busses/i2c-cros-ec-tunnel.c
index 5d91e33eb600..2e3217678fa3 100644
--- a/drivers/i2c/busses/i2c-cros-ec-tunnel.c
+++ b/drivers/i2c/busses/i2c-cros-ec-tunnel.c
@@ -6,6 +6,7 @@
#include <linux/acpi.h>
#include <linux/module.h>
#include <linux/i2c.h>
+#include <linux/mfd/cros_ec.h>
#include <linux/platform_data/cros_ec_commands.h>
#include <linux/platform_data/cros_ec_proto.h>
#include <linux/platform_device.h>
@@ -27,7 +28,6 @@
struct ec_i2c_device {
struct device *dev;
struct i2c_adapter adap;
- struct cros_ec_device *ec;

u16 remote_bus;

@@ -176,6 +176,7 @@ static int ec_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg i2c_msgs[],
{
struct ec_i2c_device *bus = adap->algo_data;
struct device *dev = bus->dev;
+ struct cros_ec_dev *ec = dev_get_drvdata(dev->parent);
const u16 bus_num = bus->remote_bus;
int request_len;
int response_len;
@@ -183,6 +184,16 @@ static int ec_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg i2c_msgs[],
int result;
struct cros_ec_command *msg;

+ if (!ec) {
+ dev_err(dev, "%s: ec is missing!\n", __func__);
+ return -EINVAL;
+ }
+
+ if (!ec->ec_dev) {
+ dev_err(dev, "%s: ec->ec_dev is missing!\n", __func__);
+ return -EINVAL;
+ }
+
request_len = ec_i2c_count_message(i2c_msgs, num);
if (request_len < 0) {
dev_warn(dev, "Error constructing message %d\n", request_len);
@@ -212,7 +223,7 @@ static int ec_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg i2c_msgs[],
msg->outsize = request_len;
msg->insize = response_len;

- result = cros_ec_cmd_xfer_status(bus->ec, msg);
+ result = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
if (result < 0) {
dev_err(dev, "Error transferring EC i2c message %d\n", result);
goto exit;
@@ -241,17 +252,11 @@ static const struct i2c_algorithm ec_i2c_algorithm = {

static int ec_i2c_probe(struct platform_device *pdev)
{
- struct cros_ec_device *ec = dev_get_drvdata(pdev->dev.parent);
struct device *dev = &pdev->dev;
struct ec_i2c_device *bus = NULL;
u32 remote_bus;
int err;

- if (!ec->cmd_xfer) {
- dev_err(dev, "Missing sendrecv\n");
- return -EINVAL;
- }
-
bus = devm_kzalloc(dev, sizeof(*bus), GFP_KERNEL);
if (bus == NULL)
return -ENOMEM;
@@ -263,7 +268,6 @@ static int ec_i2c_probe(struct platform_device *pdev)
}
bus->remote_bus = remote_bus;

- bus->ec = ec;
bus->dev = dev;

bus->adap.owner = THIS_MODULE;
@@ -292,25 +296,11 @@ static int ec_i2c_remove(struct platform_device *dev)
return 0;
}

-static const struct of_device_id cros_ec_i2c_of_match[] = {
- { .compatible = "google,cros-ec-i2c-tunnel" },
- {},
-};
-MODULE_DEVICE_TABLE(of, cros_ec_i2c_of_match);
-
-static const struct acpi_device_id cros_ec_i2c_tunnel_acpi_id[] = {
- { "GOOG0012", 0 },
- { }
-};
-MODULE_DEVICE_TABLE(acpi, cros_ec_i2c_tunnel_acpi_id);
-
static struct platform_driver ec_i2c_tunnel_driver = {
.probe = ec_i2c_probe,
.remove = ec_i2c_remove,
.driver = {
.name = "cros-ec-i2c-tunnel",
- .acpi_match_table = ACPI_PTR(cros_ec_i2c_tunnel_acpi_id),
- .of_match_table = of_match_ptr(cros_ec_i2c_of_match),
},
};

diff --git a/drivers/mfd/cros_ec_dev.c b/drivers/mfd/cros_ec_dev.c
index 1efdba18f20b..04f8fc5772f0 100644
--- a/drivers/mfd/cros_ec_dev.c
+++ b/drivers/mfd/cros_ec_dev.c
@@ -101,12 +101,21 @@ static const struct cros_feature_to_cells cros_subdevices[] = {
},
};

+static const struct mfd_cell_acpi_match cros_ec_i2c_tunnel_acpi_match = {
+ .pnpid = "GOOG0012"
+};
+
static const struct mfd_cell cros_ec_platform_cells[] = {
{ .name = "cros-ec-chardev", },
{ .name = "cros-ec-debugfs", },
{ .name = "cros-ec-lightbar", },
{ .name = "cros-ec-pd-sysfs" },
{ .name = "cros-ec-sysfs", },
+ {
+ .name = "cros-ec-i2c-tunnel",
+ .acpi_match = &cros_ec_i2c_tunnel_acpi_match,
+ .of_compatible = "google,cros-ec-i2c-tunnel"
+ },
};

static const struct mfd_cell cros_ec_vbc_cells[] = {
--
2.24.0.432.g9d3f5f5b63-goog

2019-11-28 17:04:09

by Enric Balletbo i Serra

[permalink] [raw]
Subject: Re: [PATCH v2 2/2] platform/chrome: i2c: i2c-cros-ec-tunnel: Convert i2c tunnel to MFD Cell

Hi Raul,

On 25/11/19 21:47, Raul E Rangel wrote:
> If the i2c-cros-ec-tunnel driver is compiled into the kernel, it is
> possible that i2c-cros-ec-tunnel could be probed before cros_ec_XXX
> has finished initializing and setting the drvdata. This would cause a
> NULL pointer panic.
>
> Converting this driver over to an MFD solves the problem and aligns with
> where the cros_ec is going.
>
> Signed-off-by: Raul E Rangel <[email protected]>
> ---
> You can now see the device node lives under the mfd device.
>
> $ find /sys/bus/platform/devices/cros-ec-dev.0.auto/cros-ec-i2c-tunnel.12.auto/ -iname firmware_node -exec ls -l '{}' \;
> /sys/bus/platform/devices/cros-ec-dev.0.auto/cros-ec-i2c-tunnel.12.auto/firmware_node -> ../../../../../../LNXSYSTM:00/LNXSYBUS:00/PNP0A08:00/device:1c/PNP0C09:00/GOOG0004:00/GOOG0012:00
> /sys/bus/platform/devices/cros-ec-dev.0.auto/cros-ec-i2c-tunnel.12.auto/i2c-9/firmware_node -> ../../../../../../../LNXSYSTM:00/LNXSYBUS:00/PNP0A08:00/device:1c/PNP0C09:00/GOOG0004:00/GOOG0012:00
> /sys/bus/platform/devices/cros-ec-dev.0.auto/cros-ec-i2c-tunnel.12.auto/i2c-9/i2c-10EC5682:00/firmware_node -> ../../../../../../../../LNXSYSTM:00/LNXSYBUS:00/PNP0A08:00/device:1c/PNP0C09:00/GOOG0004:00/GOOG0012:00/10EC5682:00
>
> Changes in v2:
> - Moved i2c tunnel into cros_ec_platform_cells
> - Requires https://lkml.org/lkml/2019/11/21/208 to correctly enumerate.
>
> drivers/i2c/busses/i2c-cros-ec-tunnel.c | 36 +++++++++----------------
> drivers/mfd/cros_ec_dev.c | 9 +++++++
> 2 files changed, 22 insertions(+), 23 deletions(-)
>
> diff --git a/drivers/i2c/busses/i2c-cros-ec-tunnel.c b/drivers/i2c/busses/i2c-cros-ec-tunnel.c
> index 5d91e33eb600..2e3217678fa3 100644
> --- a/drivers/i2c/busses/i2c-cros-ec-tunnel.c
> +++ b/drivers/i2c/busses/i2c-cros-ec-tunnel.c
> @@ -6,6 +6,7 @@
> #include <linux/acpi.h>
> #include <linux/module.h>
> #include <linux/i2c.h>
> +#include <linux/mfd/cros_ec.h>
> #include <linux/platform_data/cros_ec_commands.h>
> #include <linux/platform_data/cros_ec_proto.h>
> #include <linux/platform_device.h>
> @@ -27,7 +28,6 @@
> struct ec_i2c_device {
> struct device *dev;
> struct i2c_adapter adap;
> - struct cros_ec_device *ec;
>
> u16 remote_bus;
>
> @@ -176,6 +176,7 @@ static int ec_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg i2c_msgs[],
> {
> struct ec_i2c_device *bus = adap->algo_data;
> struct device *dev = bus->dev;
> + struct cros_ec_dev *ec = dev_get_drvdata(dev->parent);
> const u16 bus_num = bus->remote_bus;
> int request_len;
> int response_len;
> @@ -183,6 +184,16 @@ static int ec_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg i2c_msgs[],
> int result;
> struct cros_ec_command *msg;
>
> + if (!ec) {
> + dev_err(dev, "%s: ec is missing!\n", __func__);
> + return -EINVAL;
> + }
> +
> + if (!ec->ec_dev) {
> + dev_err(dev, "%s: ec->ec_dev is missing!\n", __func__);
> + return -EINVAL;
> + }
> +
> request_len = ec_i2c_count_message(i2c_msgs, num);
> if (request_len < 0) {
> dev_warn(dev, "Error constructing message %d\n", request_len);
> @@ -212,7 +223,7 @@ static int ec_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg i2c_msgs[],
> msg->outsize = request_len;
> msg->insize = response_len;
>
> - result = cros_ec_cmd_xfer_status(bus->ec, msg);
> + result = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
> if (result < 0) {
> dev_err(dev, "Error transferring EC i2c message %d\n", result);
> goto exit;
> @@ -241,17 +252,11 @@ static const struct i2c_algorithm ec_i2c_algorithm = {
>
> static int ec_i2c_probe(struct platform_device *pdev)
> {
> - struct cros_ec_device *ec = dev_get_drvdata(pdev->dev.parent);
> struct device *dev = &pdev->dev;
> struct ec_i2c_device *bus = NULL;
> u32 remote_bus;
> int err;
>
> - if (!ec->cmd_xfer) {
> - dev_err(dev, "Missing sendrecv\n");
> - return -EINVAL;
> - }
> -
> bus = devm_kzalloc(dev, sizeof(*bus), GFP_KERNEL);
> if (bus == NULL)
> return -ENOMEM;
> @@ -263,7 +268,6 @@ static int ec_i2c_probe(struct platform_device *pdev)
> }
> bus->remote_bus = remote_bus;
>
> - bus->ec = ec;
> bus->dev = dev;
>
> bus->adap.owner = THIS_MODULE;
> @@ -292,25 +296,11 @@ static int ec_i2c_remove(struct platform_device *dev)
> return 0;
> }
>
> -static const struct of_device_id cros_ec_i2c_of_match[] = {
> - { .compatible = "google,cros-ec-i2c-tunnel" },
> - {},
> -};
> -MODULE_DEVICE_TABLE(of, cros_ec_i2c_of_match);
> -
> -static const struct acpi_device_id cros_ec_i2c_tunnel_acpi_id[] = {
> - { "GOOG0012", 0 },
> - { }
> -};
> -MODULE_DEVICE_TABLE(acpi, cros_ec_i2c_tunnel_acpi_id);
> -
> static struct platform_driver ec_i2c_tunnel_driver = {
> .probe = ec_i2c_probe,
> .remove = ec_i2c_remove,
> .driver = {
> .name = "cros-ec-i2c-tunnel",
> - .acpi_match_table = ACPI_PTR(cros_ec_i2c_tunnel_acpi_id),
> - .of_match_table = of_match_ptr(cros_ec_i2c_of_match),

I still think that the tables should be in the i2c-cros-ec-tunnel.

> },
> };
>
> diff --git a/drivers/mfd/cros_ec_dev.c b/drivers/mfd/cros_ec_dev.c
> index 1efdba18f20b..04f8fc5772f0 100644
> --- a/drivers/mfd/cros_ec_dev.c
> +++ b/drivers/mfd/cros_ec_dev.c
> @@ -101,12 +101,21 @@ static const struct cros_feature_to_cells cros_subdevices[] = {
> },
> };
>
> +static const struct mfd_cell_acpi_match cros_ec_i2c_tunnel_acpi_match = {
> + .pnpid = "GOOG0012"
> +};
> +
> static const struct mfd_cell cros_ec_platform_cells[] = {
> { .name = "cros-ec-chardev", },
> { .name = "cros-ec-debugfs", },
> { .name = "cros-ec-lightbar", },
> { .name = "cros-ec-pd-sysfs" },
> { .name = "cros-ec-sysfs", },
> + {
> + .name = "cros-ec-i2c-tunnel",
> + .acpi_match = &cros_ec_i2c_tunnel_acpi_match,
> + .of_compatible = "google,cros-ec-i2c-tunnel"
> + },

We can't do this. With this change, on OF-based systems, the cros-ec-i2c-tunnel
will be instantiated twice. Once because of the mfd_add_device call for the
above cells and another one because of the call of of_platform_populate.

I think I understand what you are trying to solve, but not convinced about the
solution. I think that I can't test for cros-ec-i2c-tunnel case because are no
devices out, but should be similar for cros-ec-keyb driver. Let me check if I
have an ACPI based device supporting this (Samus is an option, I guess)

Thanks,
Enric

> };
>
> static const struct mfd_cell cros_ec_vbc_cells[] = {
>