Skip to contentSkip to conversation

Add protocol v3 support

PATCH Superseded Fabien Lahoudere 3 participants 12 messages Jun 27, 2019 – Jun 28, 2019

Subsystems (2)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

cover Add protocol v3 support

This series is a split of the following patch:
https://lkml.org/lkml/2019/6/18/268
To fix Enric comments from https://lkml.org/lkml/2019/6/25/949
I extract it from the other serie to speed up acceptance because other patches need it to be upstreamed.

Fabien Lahoudere (2):
iio: common: cros_ec_sensors: determine protocol version
iio: common: cros_ec_sensors: set default frequencies

 .../cros_ec_sensors/cros_ec_sensors_core.c    | 80 ++++++++++++++++++-
 .../linux/iio/common/cros_ec_sensors_core.h   |  3 +
 2 files changed, 82 insertions(+), 1 deletion(-)
Signature · 1 line
2.20.1
#1 lore raw

PATCH 1/2 iio: common: cros_ec_sensors: determine protocol version

This patch adds a function to determine which version of the protocol is used to communicate with EC.

---
 .../cros_ec_sensors/cros_ec_sensors_core.c    | 36 ++++++++++++++++++-
 1 file changed, 35 insertions(+), 1 deletion(-)
drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c +35 -1
index 130362ca421b..2e0f97448e64 100644
@@ -25,6 +25,31 @@ static char *cros_ec_loc[] = {
 	[MOTIONSENSE_LOC_MAX] = "unknown",
 };
 
+static int cros_ec_get_host_cmd_version_mask(struct cros_ec_device *ec_dev,
+					     u16 cmd_offset, u16 cmd, u32 *mask)
+{
+	int ret;
+	struct {
+		struct cros_ec_command msg;
+		union {
+			struct ec_params_get_cmd_versions params;
+			struct ec_response_get_cmd_versions resp;
+		};
+	} __packed buf = {
+		.msg = {
+			.command = EC_CMD_GET_CMD_VERSIONS + cmd_offset,
+			.insize = sizeof(struct ec_response_get_cmd_versions),
+			.outsize = sizeof(struct ec_params_get_cmd_versions)
+			},
+		.params = {.cmd = cmd}
+	};
+
+	ret = cros_ec_cmd_xfer_status(ec_dev, &buf.msg);
+	if (ret >= 0)
+		*mask = buf.resp.version_mask;
+	return ret;
+}
+
 int cros_ec_sensors_core_init(struct platform_device *pdev,
 			      struct iio_dev *indio_dev,
 			      bool physical_device)
@@ -33,6 +58,8 @@ int cros_ec_sensors_core_init(struct platform_device *pdev,
 	struct cros_ec_sensors_core_state *state = iio_priv(indio_dev);
 	struct cros_ec_dev *ec = dev_get_drvdata(pdev->dev.parent);
 	struct cros_ec_sensor_platform *sensor_platform = dev_get_platdata(dev);
+	u32 ver_mask;
+	int ret;
 
 	platform_set_drvdata(pdev, indio_dev);
 
@@ -47,8 +74,15 @@ int cros_ec_sensors_core_init(struct platform_device *pdev,
 
 	mutex_init(&state->cmd_lock);
 
+	ret = cros_ec_get_host_cmd_version_mask(state->ec,
+						ec->cmd_offset,
+						EC_CMD_MOTION_SENSE_CMD,
+						&ver_mask);
+	if (ret < 0)
+		return ret;
+
 	/* Set up the host command structure. */
-	state->msg->version = 2;
+	state->msg->version = fls(ver_mask) - 1;;
 	state->msg->command = EC_CMD_MOTION_SENSE_CMD + ec->cmd_offset;
 	state->msg->outsize = sizeof(struct ec_params_motion_sense);
 
Signature · 1 line
2.20.1
Tested-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] iio: common: cros_ec_sensors: determine protocol version

Hi,

cc'ing Doug, Gwendal and Enrico that might be interested to give a review.

This patch can be picked alone without 2/2, an is needed to have cros-ec-sensors legacy support on ARM (see [1] and [2])

Jonathan, as [1] and [2] will go through the chrome-platform tree if you don't mind I'd also like to carry with this patch once you're fine with it.

Thanks,
~ Enric

[1] https://patchwork.kernel.org/patch/11014329/
[2] https://patchwork.kernel.org/patch/11014327/

Quoted 5 lines · Fabien Lahoudere

This patch adds a function to determine which version of the protocol is used to communicate with EC.

Quoted 67 lines
---
 .../cros_ec_sensors/cros_ec_sensors_core.c    | 36 ++++++++++++++++++-
 1 file changed, 35 insertions(+), 1 deletion(-)
drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c +35 -1
index 130362ca421b..2e0f97448e64 100644
@@ -25,6 +25,31 @@ static char *cros_ec_loc[] = {
 	[MOTIONSENSE_LOC_MAX] = "unknown",
 };
 
+static int cros_ec_get_host_cmd_version_mask(struct cros_ec_device *ec_dev,
+					     u16 cmd_offset, u16 cmd, u32 *mask)
+{
+	int ret;
+	struct {
+		struct cros_ec_command msg;
+		union {
+			struct ec_params_get_cmd_versions params;
+			struct ec_response_get_cmd_versions resp;
+		};
+	} __packed buf = {
+		.msg = {
+			.command = EC_CMD_GET_CMD_VERSIONS + cmd_offset,
+			.insize = sizeof(struct ec_response_get_cmd_versions),
+			.outsize = sizeof(struct ec_params_get_cmd_versions)
+			},
+		.params = {.cmd = cmd}
+	};
+
+	ret = cros_ec_cmd_xfer_status(ec_dev, &buf.msg);
+	if (ret >= 0)
+		*mask = buf.resp.version_mask;
+	return ret;
+}
+
 int cros_ec_sensors_core_init(struct platform_device *pdev,
 			      struct iio_dev *indio_dev,
 			      bool physical_device)
@@ -33,6 +58,8 @@ int cros_ec_sensors_core_init(struct platform_device *pdev,
 	struct cros_ec_sensors_core_state *state = iio_priv(indio_dev);
 	struct cros_ec_dev *ec = dev_get_drvdata(pdev->dev.parent);
 	struct cros_ec_sensor_platform *sensor_platform = dev_get_platdata(dev);
+	u32 ver_mask;
+	int ret;
 
 	platform_set_drvdata(pdev, indio_dev);
 
@@ -47,8 +74,15 @@ int cros_ec_sensors_core_init(struct platform_device *pdev,
 
 	mutex_init(&state->cmd_lock);
 
+	ret = cros_ec_get_host_cmd_version_mask(state->ec,
+						ec->cmd_offset,
+						EC_CMD_MOTION_SENSE_CMD,
+						&ver_mask);
+	if (ret < 0)
+		return ret;
+
 	/* Set up the host command structure. */
-	state->msg->version = 2;
+	state->msg->version = fls(ver_mask) - 1;;
 	state->msg->command = EC_CMD_MOTION_SENSE_CMD + ec->cmd_offset;
 	state->msg->outsize = sizeof(struct ec_params_motion_sense);
 
#3 lore raw

Re: [PATCH 1/2] iio: common: cros_ec_sensors: determine protocol version

Quoted 38 lines · Enric Balletbo i Serra

Hi,

cc'ing Doug, Gwendal and Enrico that might be interested to give a review.

This patch can be picked alone without 2/2, an is needed to have cros-ec-sensors legacy support on ARM (see [1] and [2])

Jonathan, as [1] and [2] will go through the chrome-platform tree if you don't mind I'd also like to carry with this patch once you're fine with it.

Thanks,
~ Enric

[1] https://patchwork.kernel.org/patch/11014329/
[2] https://patchwork.kernel.org/patch/11014327/

Quoted 5 lines · Fabien Lahoudere

This patch adds a function to determine which version of the protocol is used to communicate with EC.

Quoted 12 lines
---
 .../cros_ec_sensors/cros_ec_sensors_core.c    | 36 ++++++++++++++++++-
 1 file changed, 35 insertions(+), 1 deletion(-)
drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c +0 -0
index 130362ca421b..2e0f97448e64 100644
@@ -25,6 +25,31 @@ static char *cros_ec_loc[] = {
      [MOTIONSENSE_LOC_MAX] = "unknown",
 };
 
Quoted 12 lines

drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c static char *cros_ec_loc[] = { · line 39

Quoted 12 lines
drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c +12 -0
+static int cros_ec_get_host_cmd_version_mask(struct cros_ec_device *ec_dev,
+                                          u16 cmd_offset, u16 cmd, u32 *mask)
+{
+     int ret;
+     struct {
+             struct cros_ec_command msg;
+             union {
+                     struct ec_params_get_cmd_versions params;
+                     struct ec_response_get_cmd_versions resp;
+             };
+     } __packed buf = {
+             .msg = {

add
.version = 0,
As the variable is coming from the stack, the version should be set.

Quoted 9 lines

drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c static char *cros_ec_loc[] = { · line 48

Quoted 9 lines
drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c +9 -0
+                     .command = EC_CMD_GET_CMD_VERSIONS + cmd_offset,
+                     .insize = sizeof(struct ec_response_get_cmd_versions),
+                     .outsize = sizeof(struct ec_params_get_cmd_versions)
+                     },
+             .params = {.cmd = cmd}
+     };
+
+     ret = cros_ec_cmd_xfer_status(ec_dev, &buf.msg);
+     if (ret >= 0)

It should be > 0: when the command is a success, it returns the number of byte in the response buffer. When don't expect == 0 here, because when successful, EC_CMD_GET_CMD_VERSIONS will return a mask.

Quoted 18 lines
Quoted 18 lines
drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c +6 -0
+             *mask = buf.resp.version_mask;
+     return ret;
+}
+
 int cros_ec_sensors_core_init(struct platform_device *pdev,
                            struct iio_dev *indio_dev,
                            bool physical_device)
@@ -33,6 +58,8 @@ int cros_ec_sensors_core_init(struct platform_device *pdev,
      struct cros_ec_sensors_core_state *state = iio_priv(indio_dev);
      struct cros_ec_dev *ec = dev_get_drvdata(pdev->dev.parent);
      struct cros_ec_sensor_platform *sensor_platform = dev_get_platdata(dev);
+     u32 ver_mask;
+     int ret;
 
      platform_set_drvdata(pdev, indio_dev);
 
@@ -47,8 +74,15 @@ int cros_ec_sensors_core_init(struct platform_device *pdev,
 
Quoted 7 lines

drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c int cros_ec_sensors_core_init(struct platform_device *pdev, · line 81

Quoted 7 lines
drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c +5 -0
      mutex_init(&state->cmd_lock);
 
+     ret = cros_ec_get_host_cmd_version_mask(state->ec,
+                                             ec->cmd_offset,
+                                             EC_CMD_MOTION_SENSE_CMD,
+                                             &ver_mask);
+     if (ret < 0)

Use:
if (ret <= 0 || ver_mask == 0) {
In case the EC is really old or misbehaving, we don't want to set an invalid version later.

Quoted 9 lines
Quoted 9 lines
drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c +3 -1
+             return ret;
+
      /* Set up the host command structure. */
-     state->msg->version = 2;
+     state->msg->version = fls(ver_mask) - 1;;
      state->msg->command = EC_CMD_MOTION_SENSE_CMD + ec->cmd_offset;
      state->msg->outsize = sizeof(struct ec_params_motion_sense);
 
#4 lore raw

Re: [PATCH 1/2] iio: common: cros_ec_sensors: determine protocol version

Thanks Gwendal for reviewing.

Quoted 55 lines · Gwendal Grignou
Quoted 53 lines · Enric Balletbo i Serra

Hi,

cc'ing Doug, Gwendal and Enrico that might be interested to give a review.

This patch can be picked alone without 2/2, an is needed to have cros-ec-sensors
legacy support on ARM (see [1] and [2])

Jonathan, as [1] and [2] will go through the chrome-platform tree if you don't
mind I'd also like to carry with this patch once you're fine with it.

Thanks,
~ Enric

[1] https://patchwork.kernel.org/patch/11014329/
[2] https://patchwork.kernel.org/patch/11014327/

Quoted 5 lines · Fabien Lahoudere

This patch adds a function to determine which version of the protocol is used to communicate with EC.

Quoted 24 lines
---
 .../cros_ec_sensors/cros_ec_sensors_core.c    | 36
++++++++++++++++++-
 1 file changed, 35 insertions(+), 1 deletion(-)

diff --git a/drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c b/drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c index 130362ca421b..2e0f97448e64 100644

drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c +7 -0
@@ -25,6 +25,31 @@ static char *cros_ec_loc[] = {
      [MOTIONSENSE_LOC_MAX] = "unknown",
 };
 
+static int cros_ec_get_host_cmd_version_mask(struct
 cros_ec_device *ec_dev,
+                                          u16 cmd_offset, u16
 cmd, u32 *mask)
+{
+     int ret;
+     struct {
+             struct cros_ec_command msg;
+             union {
Quoted 8 lines
Quoted 5 lines

drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c static char *cros_ec_loc[] = { · line 39

Quoted 5 lines
drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c +5 -0
+                     struct ec_params_get_cmd_versions params;
+                     struct ec_response_get_cmd_versions resp;
+             };
+     } __packed buf = {
+             .msg = {

add
.version = 0,
As the variable is coming from the stack, the version should be set.

Ack

Quoted 12 lines
Quoted 4 lines
Quoted 4 lines
drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c +2 -0
+                     .command = EC_CMD_GET_CMD_VERSIONS +
 cmd_offset,
+                     .insize = sizeof(struct

ec_response_get_cmd_versions),

Quoted 8 lines

drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c static char *cros_ec_loc[] = { · line 48

Quoted 8 lines

+ .outsize = sizeof(struct
ec_params_get_cmd_versions)

drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c +6 -0
+                     },
+             .params = {.cmd = cmd}
+     };
+
+     ret = cros_ec_cmd_xfer_status(ec_dev, &buf.msg);
+     if (ret >= 0)
Quoted 4 lines

It should be > 0: when the command is a success, it returns the number
of byte in the response buffer. When don't expect == 0 here, because when successful, EC_CMD_GET_CMD_VERSIONS will return a mask.

Ack, so we assume that on success, 0 is not possible.

Quoted 24 lines
Quoted 24 lines
Quoted 24 lines
drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c +6 -0
+             *mask = buf.resp.version_mask;
+     return ret;
+}
+
 int cros_ec_sensors_core_init(struct platform_device *pdev,
                            struct iio_dev *indio_dev,
                            bool physical_device)
@@ -33,6 +58,8 @@ int cros_ec_sensors_core_init(struct
 platform_device *pdev,
      struct cros_ec_sensors_core_state *state =
 iio_priv(indio_dev);
      struct cros_ec_dev *ec = dev_get_drvdata(pdev->dev.parent);
      struct cros_ec_sensor_platform *sensor_platform =
 dev_get_platdata(dev);
+     u32 ver_mask;
+     int ret;
      platform_set_drvdata(pdev, indio_dev);
drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c +0 -0
@@ -47,8 +74,15 @@ int cros_ec_sensors_core_init(struct
 platform_device *pdev,
 
      mutex_init(&state->cmd_lock);
 
Quoted 10 lines
Quoted 6 lines

drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c int cros_ec_sensors_core_init(struct platform_device *pdev, · line 81

Quoted 6 lines
drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c +5 -0
+     ret = cros_ec_get_host_cmd_version_mask(state->ec,
+                                             ec->cmd_offset,
+                                             EC_CMD_MOTION_SENSE
 _CMD,
+                                             &ver_mask);
+     if (ret < 0)

Use:
if (ret <= 0 || ver_mask == 0) {
In case the EC is really old or misbehaving, we don't want to set an invalid version later.

Ack, indeed the communication can work but with invalid data.

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c +3 -1
+             return ret;
+
      /* Set up the host command structure. */
-     state->msg->version = 2;
+     state->msg->version = fls(ver_mask) - 1;;
      state->msg->command = EC_CMD_MOTION_SENSE_CMD + ec-
Quoted 1 line

cmd_offset;

      state->msg->outsize = sizeof(struct

ec_params_motion_sense);

#5 lore raw

Re: [PATCH 1/2] iio: common: cros_ec_sensors: determine protocol version

Hi Fabien, Gwendal

Quoted 67 lines · Fabien Lahoudere

Thanks Gwendal for reviewing.

Quoted 63 lines · Gwendal Grignou
Quoted 44 lines · Enric Balletbo i Serra

Hi,

cc'ing Doug, Gwendal and Enrico that might be interested to give a review.

This patch can be picked alone without 2/2, an is needed to have cros-ec-sensors
legacy support on ARM (see [1] and [2])

Jonathan, as [1] and [2] will go through the chrome-platform tree if you don't
mind I'd also like to carry with this patch once you're fine with it.

Thanks,
~ Enric

[1] https://patchwork.kernel.org/patch/11014329/
[2] https://patchwork.kernel.org/patch/11014327/

Quoted 5 lines · Fabien Lahoudere

This patch adds a function to determine which version of the protocol is used to communicate with EC.

Quoted 15 lines
---
 .../cros_ec_sensors/cros_ec_sensors_core.c    | 36
++++++++++++++++++-
 1 file changed, 35 insertions(+), 1 deletion(-)

diff --git a/drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c b/drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c index 130362ca421b..2e0f97448e64 100644

drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c +0 -0
@@ -25,6 +25,31 @@ static char *cros_ec_loc[] = {
      [MOTIONSENSE_LOC_MAX] = "unknown",
 };
 
Quoted 14 lines
Quoted 4 lines
drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c +2 -0
+static int cros_ec_get_host_cmd_version_mask(struct
 cros_ec_device *ec_dev,
+                                          u16 cmd_offset, u16
 cmd, u32 *mask)

drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c static char *cros_ec_loc[] = { · line 39

Quoted 10 lines
drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c +10 -0
+{
+     int ret;
+     struct {
+             struct cros_ec_command msg;
+             union {
+                     struct ec_params_get_cmd_versions params;
+                     struct ec_response_get_cmd_versions resp;
+             };
+     } __packed buf = {
+             .msg = {

add
.version = 0,
As the variable is coming from the stack, the version should be set.

I think that from the C standard when using struct partial initialization in c it follows the same rules as static so shouldn't be really needed. Anyway this is always confusing me is for that I tend to use buf = { }; or memset the struct and then assign the required values so it's clear that all the memebers of the struct are initialized.

Quoted 19 lines

Ack

Quoted 16 lines
Quoted 4 lines
Quoted 4 lines
drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c +2 -0
+                     .command = EC_CMD_GET_CMD_VERSIONS +
 cmd_offset,
+                     .insize = sizeof(struct

ec_response_get_cmd_versions),

Quoted 8 lines

drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c static char *cros_ec_loc[] = { · line 48

Quoted 8 lines

+ .outsize = sizeof(struct
ec_params_get_cmd_versions)

drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c +6 -0
+                     },
+             .params = {.cmd = cmd}
+     };
+
+     ret = cros_ec_cmd_xfer_status(ec_dev, &buf.msg);
+     if (ret >= 0)

It should be > 0: when the command is a success, it returns the number
of byte in the response buffer. When don't expect == 0 here, because when successful, EC_CMD_GET_CMD_VERSIONS will return a mask.

Gwendal, from the downstream commit I see:

        ret = cros_ec_cmd_xfer_status(ec_dev, msg);
        if (ret >= 0) {
                if (msg->result == EC_RES_SUCCESS)
                        *mask = resp->version_mask;
                else
                        *mask = 0;
        }
        return ret;

I think we're confusing cros_ec_cmd_xfer_status() vs cros_ec_cmd_xfer()?

cros_ec_cmd_xfer_status() will return _only_ a value >= 0 value _if result is EC_RES_SUCCESS_ otherwise will return a negative value (see cros_ec_cmd_xfer_status() implementation). So the second check is not really needed and the same can be implemented as:

        ret = cros_ec_cmd_xfer_status(ec_dev, msg);
        if (ret < 0)
                *mask = 0;
        else
                *mask = resp->version_mask;

        return ret;

But then I don't see the point to set the mask to 0 and even can be simplified as:

        ret = cros_ec_cmd_xfer_status(ec_dev, msg);
        if (ret < 0)
                return ret;

        *mask = msg.resp.version_mask;

        return 0;

So

cros_ec_get_host_cmd_version_mask() returns 0 on success or negative value on error (protocol error || result != EC_RES_SUCCESS)

Quoted 36 lines

Ack, so we assume that on success, 0 is not possible.

Quoted 24 lines
Quoted 24 lines
Quoted 24 lines
drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c +6 -0
+             *mask = buf.resp.version_mask;
+     return ret;
+}
+
 int cros_ec_sensors_core_init(struct platform_device *pdev,
                            struct iio_dev *indio_dev,
                            bool physical_device)
@@ -33,6 +58,8 @@ int cros_ec_sensors_core_init(struct
 platform_device *pdev,
      struct cros_ec_sensors_core_state *state =
 iio_priv(indio_dev);
      struct cros_ec_dev *ec = dev_get_drvdata(pdev->dev.parent);
      struct cros_ec_sensor_platform *sensor_platform =
 dev_get_platdata(dev);
+     u32 ver_mask;
+     int ret;
      platform_set_drvdata(pdev, indio_dev);
drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c +0 -0
@@ -47,8 +74,15 @@ int cros_ec_sensors_core_init(struct
 platform_device *pdev,
 
      mutex_init(&state->cmd_lock);
 
Quoted 10 lines
Quoted 6 lines

drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c int cros_ec_sensors_core_init(struct platform_device *pdev, · line 81

Quoted 6 lines
drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c +5 -0
+     ret = cros_ec_get_host_cmd_version_mask(state->ec,
+                                             ec->cmd_offset,
+                                             EC_CMD_MOTION_SENSE
 _CMD,
+                                             &ver_mask);
+     if (ret < 0)

Use:
if (ret <= 0 || ver_mask == 0) {
In case the EC is really old or misbehaving, we don't want to set an invalid version later.

Quoted 1 line

Ack, indeed the communication can work but with invalid data.

From the downstream commit:

        if (ret < 0 || ver_mask == 0) {
                dev_warn(dev, "Motionsense cmd version too old, aborting...\n");
                return -ENODEV;
        }

But with the above implementation is the same as

        if (ret < 0) {
                dev_warn(dev, "Motionsense cmd version too old, aborting...\n");
                return -ENODEV;
        }

Or I'm missing something and what we really want is to cover a corner case? I such case I think we should use cros_ec_cmd_xfer() instead of cros_ec_cmd_xfer_status()

Thanks,
~ Enric

Quoted 12 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c +3 -1
+             return ret;
+
      /* Set up the host command structure. */
-     state->msg->version = 2;
+     state->msg->version = fls(ver_mask) - 1;;
      state->msg->command = EC_CMD_MOTION_SENSE_CMD + ec-
Quoted 1 line

cmd_offset;

      state->msg->outsize = sizeof(struct

ec_params_motion_sense);

#6 lore raw

Re: [PATCH 1/2] iio: common: cros_ec_sensors: determine protocol version

In reply to Gwendal Grignou
Quoted 103 lines · Gwendal Grignou
Quoted 44 lines · Enric Balletbo i Serra

Hi,

cc'ing Doug, Gwendal and Enrico that might be interested to give a review.

This patch can be picked alone without 2/2, an is needed to have cros-ec-sensors
legacy support on ARM (see [1] and [2])

Jonathan, as [1] and [2] will go through the chrome-platform tree if you don't
mind I'd also like to carry with this patch once you're fine with it.

Thanks,
~ Enric

[1] https://patchwork.kernel.org/patch/11014329/
[2] https://patchwork.kernel.org/patch/11014327/

Quoted 5 lines · Fabien Lahoudere

This patch adds a function to determine which version of the protocol is used to communicate with EC.

Quoted 15 lines
---
 .../cros_ec_sensors/cros_ec_sensors_core.c    | 36
++++++++++++++++++-
 1 file changed, 35 insertions(+), 1 deletion(-)

diff --git a/drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c b/drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c index 130362ca421b..2e0f97448e64 100644

drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c +0 -0
@@ -25,6 +25,31 @@ static char *cros_ec_loc[] = {
      [MOTIONSENSE_LOC_MAX] = "unknown",
 };
 
Quoted 14 lines
Quoted 4 lines
drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c +2 -0
+static int cros_ec_get_host_cmd_version_mask(struct
 cros_ec_device *ec_dev,
+                                          u16 cmd_offset, u16
 cmd, u32 *mask)

drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c static char *cros_ec_loc[] = { · line 39

Quoted 10 lines
drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c +10 -0
+{
+     int ret;
+     struct {
+             struct cros_ec_command msg;
+             union {
+                     struct ec_params_get_cmd_versions params;
+                     struct ec_response_get_cmd_versions resp;
+             };
+     } __packed buf = {
+             .msg = {

add
.version = 0,
As the variable is coming from the stack, the version should be set.

Quoted 4 lines
Quoted 4 lines
drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c +2 -0
+                     .command = EC_CMD_GET_CMD_VERSIONS +
 cmd_offset,
+                     .insize = sizeof(struct

ec_response_get_cmd_versions),

Quoted 8 lines

drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c static char *cros_ec_loc[] = { · line 48

Quoted 8 lines

+ .outsize = sizeof(struct
ec_params_get_cmd_versions)

drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c +6 -0
+                     },
+             .params = {.cmd = cmd}
+     };
+
+     ret = cros_ec_cmd_xfer_status(ec_dev, &buf.msg);
+     if (ret >= 0)

It should be > 0: when the command is a success, it returns the number
of byte in the response buffer. When don't expect == 0 here, because when successful, EC_CMD_GET_CMD_VERSIONS will return a mask.

Quoted 24 lines
Quoted 24 lines
drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c +6 -0
+             *mask = buf.resp.version_mask;
+     return ret;
+}
+
 int cros_ec_sensors_core_init(struct platform_device *pdev,
                            struct iio_dev *indio_dev,
                            bool physical_device)
@@ -33,6 +58,8 @@ int cros_ec_sensors_core_init(struct
 platform_device *pdev,
      struct cros_ec_sensors_core_state *state =
 iio_priv(indio_dev);
      struct cros_ec_dev *ec = dev_get_drvdata(pdev->dev.parent);
      struct cros_ec_sensor_platform *sensor_platform =
 dev_get_platdata(dev);
+     u32 ver_mask;
+     int ret;
      platform_set_drvdata(pdev, indio_dev);
drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c +0 -0
@@ -47,8 +74,15 @@ int cros_ec_sensors_core_init(struct
 platform_device *pdev,
 
      mutex_init(&state->cmd_lock);
 
Quoted 10 lines
Quoted 6 lines

drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c int cros_ec_sensors_core_init(struct platform_device *pdev, · line 81

Quoted 6 lines
drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c +5 -0
+     ret = cros_ec_get_host_cmd_version_mask(state->ec,
+                                             ec->cmd_offset,
+                                             EC_CMD_MOTION_SENSE
 _CMD,
+                                             &ver_mask);
+     if (ret < 0)

Use:
if (ret <= 0 || ver_mask == 0) {
In case the EC is really old or misbehaving, we don't want to set an invalid version later.

To not return a positive value on error if ret >= 0 and ver_mask = 0 I would prefer this:

	if (ret <= 0)
		return ret;

	if (ver_mask == 0)
		return -EIO;

Let me know if I am wrong

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c +3 -1
+             return ret;
+
      /* Set up the host command structure. */
-     state->msg->version = 2;
+     state->msg->version = fls(ver_mask) - 1;;
      state->msg->command = EC_CMD_MOTION_SENSE_CMD + ec-
Quoted 1 line

cmd_offset;

      state->msg->outsize = sizeof(struct

ec_params_motion_sense);

#7 lore raw

Re: [PATCH 1/2] iio: common: cros_ec_sensors: determine protocol version

Hi Fabien, Gwendal,

Quoted 118 lines · Fabien Lahoudere
Quoted 103 lines · Gwendal Grignou
Quoted 44 lines · Enric Balletbo i Serra

Hi,

cc'ing Doug, Gwendal and Enrico that might be interested to give a review.

This patch can be picked alone without 2/2, an is needed to have cros-ec-sensors
legacy support on ARM (see [1] and [2])

Jonathan, as [1] and [2] will go through the chrome-platform tree if you don't
mind I'd also like to carry with this patch once you're fine with it.

Thanks,
~ Enric

[1] https://patchwork.kernel.org/patch/11014329/
[2] https://patchwork.kernel.org/patch/11014327/

Quoted 5 lines · Fabien Lahoudere

This patch adds a function to determine which version of the protocol is used to communicate with EC.

Quoted 15 lines
---
 .../cros_ec_sensors/cros_ec_sensors_core.c    | 36
++++++++++++++++++-
 1 file changed, 35 insertions(+), 1 deletion(-)

diff --git a/drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c b/drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c index 130362ca421b..2e0f97448e64 100644

drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c +0 -0
@@ -25,6 +25,31 @@ static char *cros_ec_loc[] = {
      [MOTIONSENSE_LOC_MAX] = "unknown",
 };
 
Quoted 14 lines
Quoted 4 lines
drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c +2 -0
+static int cros_ec_get_host_cmd_version_mask(struct
 cros_ec_device *ec_dev,
+                                          u16 cmd_offset, u16
 cmd, u32 *mask)

drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c static char *cros_ec_loc[] = { · line 39

Quoted 10 lines
drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c +10 -0
+{
+     int ret;
+     struct {
+             struct cros_ec_command msg;
+             union {
+                     struct ec_params_get_cmd_versions params;
+                     struct ec_response_get_cmd_versions resp;
+             };
+     } __packed buf = {
+             .msg = {

add
.version = 0,
As the variable is coming from the stack, the version should be set.

Quoted 4 lines
Quoted 4 lines
drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c +2 -0
+                     .command = EC_CMD_GET_CMD_VERSIONS +
 cmd_offset,
+                     .insize = sizeof(struct

ec_response_get_cmd_versions),

Quoted 8 lines

drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c static char *cros_ec_loc[] = { · line 48

Quoted 8 lines

+ .outsize = sizeof(struct
ec_params_get_cmd_versions)

drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c +6 -0
+                     },
+             .params = {.cmd = cmd}
+     };
+
+     ret = cros_ec_cmd_xfer_status(ec_dev, &buf.msg);
+     if (ret >= 0)

It should be > 0: when the command is a success, it returns the number
of byte in the response buffer. When don't expect == 0 here, because when successful, EC_CMD_GET_CMD_VERSIONS will return a mask.

Quoted 24 lines
Quoted 24 lines
drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c +6 -0
+             *mask = buf.resp.version_mask;
+     return ret;
+}
+
 int cros_ec_sensors_core_init(struct platform_device *pdev,
                            struct iio_dev *indio_dev,
                            bool physical_device)
@@ -33,6 +58,8 @@ int cros_ec_sensors_core_init(struct
 platform_device *pdev,
      struct cros_ec_sensors_core_state *state =
 iio_priv(indio_dev);
      struct cros_ec_dev *ec = dev_get_drvdata(pdev->dev.parent);
      struct cros_ec_sensor_platform *sensor_platform =
 dev_get_platdata(dev);
+     u32 ver_mask;
+     int ret;
      platform_set_drvdata(pdev, indio_dev);
drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c +0 -0
@@ -47,8 +74,15 @@ int cros_ec_sensors_core_init(struct
 platform_device *pdev,
 
      mutex_init(&state->cmd_lock);
 
Quoted 10 lines
Quoted 6 lines

drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c int cros_ec_sensors_core_init(struct platform_device *pdev, · line 81

Quoted 6 lines
drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c +5 -0
+     ret = cros_ec_get_host_cmd_version_mask(state->ec,
+                                             ec->cmd_offset,
+                                             EC_CMD_MOTION_SENSE
 _CMD,
+                                             &ver_mask);
+     if (ret < 0)

Use:
if (ret <= 0 || ver_mask == 0) {
In case the EC is really old or misbehaving, we don't want to set an invalid version later.

To not return a positive value on error if ret >= 0 and ver_mask = 0 I would prefer this:

Quoted 8 lines
	if (ret <= 0)
		return ret;

	if (ver_mask == 0)
		return -EIO;

Let me know if I am wrong

Ok, after discussing with Fabien I think I understood all this and I was confused. So the thing is that some very old EC sets the version_mask to 0 and the communication succeeds. I think all this deserves a comment in the code for dummies like me :-)

Thanks,
~ Enric

Quoted 1 line
Quoted 1 line
Quoted 1 line

drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c int cros_ec_sensors_core_init(struct · line 84

Quoted 1 line
drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c +1 -0
+           

return ret;

Quoted 11 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c +2 -1
+
      /* Set up the host command structure. */
-     state->msg->version = 2;
+     state->msg->version = fls(ver_mask) - 1;;
      state->msg->command = EC_CMD_MOTION_SENSE_CMD + ec-
Quoted 1 line

cmd_offset;

      state->msg->outsize = sizeof(struct

ec_params_motion_sense);

Reviewed-by Tested-by
#8 lore raw

Re: [PATCH 1/2] iio: common: cros_ec_sensors: determine protocol version

Quoted 128 lines · Enric Balletbo i Serra

Hi Fabien, Gwendal,

Quoted 124 lines · Fabien Lahoudere
Quoted 103 lines · Gwendal Grignou
Quoted 44 lines · Enric Balletbo i Serra

Hi,

cc'ing Doug, Gwendal and Enrico that might be interested to give a review.

This patch can be picked alone without 2/2, an is needed to have cros-ec-sensors
legacy support on ARM (see [1] and [2])

Jonathan, as [1] and [2] will go through the chrome-platform tree if you don't
mind I'd also like to carry with this patch once you're fine with it.

Thanks,
~ Enric

[1] https://patchwork.kernel.org/patch/11014329/
[2] https://patchwork.kernel.org/patch/11014327/

Quoted 5 lines · Fabien Lahoudere

This patch adds a function to determine which version of the protocol is used to communicate with EC.

Quoted 15 lines
---
 .../cros_ec_sensors/cros_ec_sensors_core.c    | 36
++++++++++++++++++-
 1 file changed, 35 insertions(+), 1 deletion(-)

diff --git a/drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c b/drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c index 130362ca421b..2e0f97448e64 100644

drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c +0 -0
@@ -25,6 +25,31 @@ static char *cros_ec_loc[] = {
      [MOTIONSENSE_LOC_MAX] = "unknown",
 };
 
Quoted 14 lines
Quoted 4 lines
drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c +2 -0
+static int cros_ec_get_host_cmd_version_mask(struct
 cros_ec_device *ec_dev,
+                                          u16 cmd_offset, u16
 cmd, u32 *mask)

drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c static char *cros_ec_loc[] = { · line 39

Quoted 10 lines
drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c +10 -0
+{
+     int ret;
+     struct {
+             struct cros_ec_command msg;
+             union {
+                     struct ec_params_get_cmd_versions params;
+                     struct ec_response_get_cmd_versions resp;
+             };
+     } __packed buf = {
+             .msg = {

add
.version = 0,
As the variable is coming from the stack, the version should be set.

Quoted 4 lines
Quoted 4 lines
drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c +2 -0
+                     .command = EC_CMD_GET_CMD_VERSIONS +
 cmd_offset,
+                     .insize = sizeof(struct

ec_response_get_cmd_versions),

Quoted 8 lines

drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c static char *cros_ec_loc[] = { · line 48

Quoted 8 lines

+ .outsize = sizeof(struct
ec_params_get_cmd_versions)

drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c +6 -0
+                     },
+             .params = {.cmd = cmd}
+     };
+
+     ret = cros_ec_cmd_xfer_status(ec_dev, &buf.msg);
+     if (ret >= 0)

It should be > 0: when the command is a success, it returns the number
of byte in the response buffer. When don't expect == 0 here, because when successful, EC_CMD_GET_CMD_VERSIONS will return a mask.

Quoted 24 lines
Quoted 24 lines
drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c +6 -0
+             *mask = buf.resp.version_mask;
+     return ret;
+}
+
 int cros_ec_sensors_core_init(struct platform_device *pdev,
                            struct iio_dev *indio_dev,
                            bool physical_device)
@@ -33,6 +58,8 @@ int cros_ec_sensors_core_init(struct
 platform_device *pdev,
      struct cros_ec_sensors_core_state *state =
 iio_priv(indio_dev);
      struct cros_ec_dev *ec = dev_get_drvdata(pdev->dev.parent);
      struct cros_ec_sensor_platform *sensor_platform =
 dev_get_platdata(dev);
+     u32 ver_mask;
+     int ret;
      platform_set_drvdata(pdev, indio_dev);
drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c +0 -0
@@ -47,8 +74,15 @@ int cros_ec_sensors_core_init(struct
 platform_device *pdev,
 
      mutex_init(&state->cmd_lock);
 
Quoted 10 lines
Quoted 6 lines

drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c int cros_ec_sensors_core_init(struct platform_device *pdev, · line 81

Quoted 6 lines
drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c +5 -0
+     ret = cros_ec_get_host_cmd_version_mask(state->ec,
+                                             ec->cmd_offset,
+                                             EC_CMD_MOTION_SENSE
 _CMD,
+                                             &ver_mask);
+     if (ret < 0)

Use:
if (ret <= 0 || ver_mask == 0) {
In case the EC is really old or misbehaving, we don't want to set an invalid version later.

To not return a positive value on error if ret >= 0 and ver_mask = 0 I would prefer this:

      if (ret <= 0)
              return ret;

      if (ver_mask == 0)
              return -EIO;
Quoted 7 lines
Quoted 2 lines

Let me know if I am wrong

Ok, after discussing with Fabien I think I understood all this and I was confused. So the thing is that some very old EC sets the version_mask to 0 and the communication succeeds. I think all this deserves a comment in the code for dummies like me :-)

Looking into the code, when the command is successful, the EC would return 4 and a valid ver_mask, otherwise ret would -EPROTO (EC answers INVALID_PARAM).

So the original code will work as is. Thanks for your help about C [partial] Initialization.

Quoted 17 lines

Thanks,
~ Enric

Quoted 1 line
Quoted 1 line
Quoted 1 line

drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c int cros_ec_sensors_core_init(struct · line 84

Quoted 1 line
drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c +1 -0
+

return ret;

Quoted 11 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c +2 -1
+
      /* Set up the host command structure. */
-     state->msg->version = 2;
+     state->msg->version = fls(ver_mask) - 1;;
      state->msg->command = EC_CMD_MOTION_SENSE_CMD + ec-
Quoted 1 line

cmd_offset;

      state->msg->outsize = sizeof(struct

ec_params_motion_sense);

#9 lore raw

PATCH 2/2 iio: common: cros_ec_sensors: set default frequencies

In reply to Fabien Lahoudere

Version 3 of the EC protocol provides min and max frequencies for EC sensors. Default frequencies are provided for earlier protocol.

---
 .../cros_ec_sensors/cros_ec_sensors_core.c    | 44 +++++++++++++++++++
 .../linux/iio/common/cros_ec_sensors_core.h   |  3 ++
 2 files changed, 47 insertions(+)
drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c +44 -0
index 2e0f97448e64..72f56d54cccd 100644
@@ -50,6 +50,37 @@ static int cros_ec_get_host_cmd_version_mask(struct cros_ec_device *ec_dev,
 	return ret;
 }
 
+static void get_default_min_max_freq(enum motionsensor_type type,
+				     u32 *min_freq,
+				     u32 *max_freq)
+{
+	switch (type) {
+	case MOTIONSENSE_TYPE_ACCEL:
+	case MOTIONSENSE_TYPE_GYRO:
+		*min_freq = 12500;
+		*max_freq = 100000;
+		break;
+	case MOTIONSENSE_TYPE_MAG:
+		*min_freq = 5000;
+		*max_freq = 25000;
+		break;
+	case MOTIONSENSE_TYPE_PROX:
+	case MOTIONSENSE_TYPE_LIGHT:
+		*min_freq = 100;
+		*max_freq = 50000;
+		break;
+	case MOTIONSENSE_TYPE_BARO:
+		*min_freq = 250;
+		*max_freq = 20000;
+		break;
+	case MOTIONSENSE_TYPE_ACTIVITY:
+	default:
+		*min_freq = 0;
+		*max_freq = 0;
+		break;
+	}
+}
+
 int cros_ec_sensors_core_init(struct platform_device *pdev,
 			      struct iio_dev *indio_dev,
 			      bool physical_device)
@@ -100,6 +131,19 @@ int cros_ec_sensors_core_init(struct platform_device *pdev,
 		}
 		state->type = state->resp->info.type;
 		state->loc = state->resp->info.location;
+
+		/* Value to stop the device */
+		state->frequencies[0] = 0;
+		if (state->msg->version < 3) {
+			get_default_min_max_freq(state->resp->info.type,
+						 &state->frequencies[1],
+						 &state->frequencies[2]);
+		} else {
+			state->frequencies[1] =
+			    state->resp->info_3.min_frequency;
+			state->frequencies[2] =
+			    state->resp->info_3.max_frequency;
+		}
 	}
 
 	return 0;
include/linux/iio/common/cros_ec_sensors_core.h +3 -0
index 0c636b9fe8d7..94c87da22c04 100644
@@ -70,6 +70,9 @@ struct cros_ec_sensors_core_state {
 				    unsigned long scan_mask, s16 *data);
 
 	int curr_sampl_freq;
+
+	/* Disable, Min and Max Sampling Frequency in mHz */
+	int frequencies[3];
 };
 
 /**
Signature · 1 line
2.20.1
#10 lore raw

Re: [PATCH 2/2] iio: common: cros_ec_sensors: set default frequencies

Hi Fabien,

Quoted 3 lines · Fabien Lahoudere

Version 3 of the EC protocol provides min and max frequencies for EC sensors. Default frequencies are provided for earlier protocol.

This patch should really go together with a respin of your previous patchset to 'Expose cros_ec_sensors frequency range via iio sysfs' [1]

[1] https://www.spinics.net/lists/linux-iio/msg44963.html

Quoted 84 lines
---
 .../cros_ec_sensors/cros_ec_sensors_core.c    | 44 +++++++++++++++++++
 .../linux/iio/common/cros_ec_sensors_core.h   |  3 ++
 2 files changed, 47 insertions(+)
drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c +44 -0
index 2e0f97448e64..72f56d54cccd 100644
@@ -50,6 +50,37 @@ static int cros_ec_get_host_cmd_version_mask(struct cros_ec_device *ec_dev,
 	return ret;
 }
 
+static void get_default_min_max_freq(enum motionsensor_type type,
+				     u32 *min_freq,
+				     u32 *max_freq)
+{
+	switch (type) {
+	case MOTIONSENSE_TYPE_ACCEL:
+	case MOTIONSENSE_TYPE_GYRO:
+		*min_freq = 12500;
+		*max_freq = 100000;
+		break;
+	case MOTIONSENSE_TYPE_MAG:
+		*min_freq = 5000;
+		*max_freq = 25000;
+		break;
+	case MOTIONSENSE_TYPE_PROX:
+	case MOTIONSENSE_TYPE_LIGHT:
+		*min_freq = 100;
+		*max_freq = 50000;
+		break;
+	case MOTIONSENSE_TYPE_BARO:
+		*min_freq = 250;
+		*max_freq = 20000;
+		break;
+	case MOTIONSENSE_TYPE_ACTIVITY:
+	default:
+		*min_freq = 0;
+		*max_freq = 0;
+		break;
+	}
+}
+
 int cros_ec_sensors_core_init(struct platform_device *pdev,
 			      struct iio_dev *indio_dev,
 			      bool physical_device)
@@ -100,6 +131,19 @@ int cros_ec_sensors_core_init(struct platform_device *pdev,
 		}
 		state->type = state->resp->info.type;
 		state->loc = state->resp->info.location;
+
+		/* Value to stop the device */
+		state->frequencies[0] = 0;
+		if (state->msg->version < 3) {
+			get_default_min_max_freq(state->resp->info.type,
+						 &state->frequencies[1],
+						 &state->frequencies[2]);
+		} else {
+			state->frequencies[1] =
+			    state->resp->info_3.min_frequency;
+			state->frequencies[2] =
+			    state->resp->info_3.max_frequency;
+		}
 	}
 
 	return 0;
include/linux/iio/common/cros_ec_sensors_core.h +3 -0
index 0c636b9fe8d7..94c87da22c04 100644
@@ -70,6 +70,9 @@ struct cros_ec_sensors_core_state {
 				    unsigned long scan_mask, s16 *data);
 
 	int curr_sampl_freq;
+
+	/* Disable, Min and Max Sampling Frequency in mHz */
+	int frequencies[3];
 };
 
 /**
#11 lore raw

Re: [PATCH 2/2] iio: common: cros_ec_sensors: set default frequencies

Hi Enric

Quoted 8 lines · Enric Balletbo i Serra

Hi Fabien,

Quoted 4 lines · Fabien Lahoudere

Version 3 of the EC protocol provides min and max frequencies for EC sensors.
Default frequencies are provided for earlier protocol.

Quoted 6 lines

This patch should really go together with a respin of your previous patchset to
'Expose cros_ec_sensors frequency range via iio sysfs' [1]

[1] https://www.spinics.net/lists/linux-iio/msg44963.html

I agree, you're right. I will send only the first patch with Gwendal comments for v2.

Thanks

Quoted 94 lines
Quoted 94 lines
---
 .../cros_ec_sensors/cros_ec_sensors_core.c    | 44
+++++++++++++++++++
 .../linux/iio/common/cros_ec_sensors_core.h   |  3 ++
 2 files changed, 47 insertions(+)

diff --git a/drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c b/drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c index 2e0f97448e64..72f56d54cccd 100644

drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c +31 -0
@@ -50,6 +50,37 @@ static int
 cros_ec_get_host_cmd_version_mask(struct cros_ec_device *ec_dev,
 	return ret;
 }
 
+static void get_default_min_max_freq(enum motionsensor_type type,
+				     u32 *min_freq,
+				     u32 *max_freq)
+{
+	switch (type) {
+	case MOTIONSENSE_TYPE_ACCEL:
+	case MOTIONSENSE_TYPE_GYRO:
+		*min_freq = 12500;
+		*max_freq = 100000;
+		break;
+	case MOTIONSENSE_TYPE_MAG:
+		*min_freq = 5000;
+		*max_freq = 25000;
+		break;
+	case MOTIONSENSE_TYPE_PROX:
+	case MOTIONSENSE_TYPE_LIGHT:
+		*min_freq = 100;
+		*max_freq = 50000;
+		break;
+	case MOTIONSENSE_TYPE_BARO:
+		*min_freq = 250;
+		*max_freq = 20000;
+		break;
+	case MOTIONSENSE_TYPE_ACTIVITY:
+	default:
+		*min_freq = 0;
+		*max_freq = 0;
+		break;
+	}
+}
+
 int cros_ec_sensors_core_init(struct platform_device *pdev,
 			      struct iio_dev *indio_dev,
drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c +7 -0
 			      bool physical_device)
@@ -100,6 +131,19 @@ int cros_ec_sensors_core_init(struct
 platform_device *pdev,
 		}
 		state->type = state->resp->info.type;
 		state->loc = state->resp->info.location;
+
+		/* Value to stop the device */
+		state->frequencies[0] = 0;
+		if (state->msg->version < 3) {
+			get_default_min_max_freq(state->resp-
 >info.type,
+						 &state-
 >frequencies[1],
+						 &state-
Quoted 1 line

frequencies[2]);

drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c +6 -0
+		} else {
+			state->frequencies[1] =
+			    state->resp->info_3.min_frequency;
+			state->frequencies[2] =
+			    state->resp->info_3.max_frequency;
+		}
 	}
 
 	return 0;
include/linux/iio/common/cros_ec_sensors_core.h +3 -0
index 0c636b9fe8d7..94c87da22c04 100644
@@ -70,6 +70,9 @@ struct cros_ec_sensors_core_state {
 				    unsigned long scan_mask, s16
 *data);
 
 	int curr_sampl_freq;
+
+	/* Disable, Min and Max Sampling Frequency in mHz */
+	int frequencies[3];
 };
 

/**