Skip to contentSkip to conversation

chrome: lightbar: Report number of segments

PATCH Superseded Gwendal Grignou 3 participants 3 messages Jan 24 – Jan 26

Subsystems (2)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 chrome: lightbar: Report number of segments

Add attribue num_segments to return the number of exposed LED segments in the lightbar. It can be smaller than the number of physical leds in the lightbar.

Test: Check the attribute is present and returns a value when read.

---
 drivers/platform/chrome/cros_ec_lightbar.c    | 46 +++++++++++++++++++
 .../linux/platform_data/cros_ec_commands.h    | 11 +++++
 2 files changed, 57 insertions(+)
drivers/platform/chrome/cros_ec_lightbar.c +46 -0
index 87634f6921b7..85f30c8dfba3 100644
@@ -173,6 +173,50 @@ static ssize_t version_show(struct device *dev,
 	return sysfs_emit(buf, "%d %d\n", version, flags);
 }
 
+static ssize_t num_segments_show(struct device *dev,
+				 struct device_attribute *attr, char *buf)
+{
+	uint32_t num = 0;
+	struct cros_ec_dev *ec = to_cros_ec_dev(dev);
+	int ret;
+
+	ret = lb_throttle();
+	if (ret)
+		return ret;
+
+	struct ec_params_lightbar *param;
+	struct ec_response_lightbar *resp;
+	struct cros_ec_command *msg;
+
+	msg = alloc_lightbar_cmd_msg(ec);
+	if (!msg)
+		return -ENOMEM;
+
+	param = (struct ec_params_lightbar *)msg->data;
+	param->cmd = LIGHTBAR_CMD_GET_PARAMS_V3;
+	msg->outsize = sizeof(param->cmd);
+	msg->result = sizeof(resp->get_params_v3);
+	ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
+	if (ret < 0 && ret != -EINVAL)
+		goto exit;
+
+	if (msg->result == EC_RES_SUCCESS) {
+		resp = (struct ec_response_lightbar *)msg->data;
+		num = resp->get_params_v3.reported_led_num;
+	}
+
+	/*
+	 * Anything else (ie, EC_RES_INVALID_COMMAND) - no direct control over
+	 * LEDs, return that no leds are supported.
+	 */
+	ret = 0;
+exit:
+	kfree(msg);
+	if (ret)
+		return ret;
+	return sysfs_emit(buf, "%d\n", num);
+}
+
 static ssize_t brightness_store(struct device *dev,
 				struct device_attribute *attr,
 				const char *buf, size_t count)
@@ -505,6 +549,7 @@ static ssize_t userspace_control_store(struct device *dev,
 /* Module initialization */
 
 static DEVICE_ATTR_RW(interval_msec);
+static DEVICE_ATTR_RO(num_segments);
 static DEVICE_ATTR_RO(version);
 static DEVICE_ATTR_WO(brightness);
 static DEVICE_ATTR_WO(led_rgb);
@@ -514,6 +559,7 @@ static DEVICE_ATTR_RW(userspace_control);
 
 static struct attribute *__lb_cmds_attrs[] = {
 	&dev_attr_interval_msec.attr,
+	&dev_attr_num_segments.attr,
 	&dev_attr_version.attr,
 	&dev_attr_brightness.attr,
 	&dev_attr_led_rgb.attr,
include/linux/platform_data/cros_ec_commands.h +11 -0
index 69294f79cc88..9cbf024f56c3 100644
@@ -2005,6 +2005,14 @@ struct lightbar_params_v2_colors {
 	struct rgb_s color[8];			/* 0-3 are Google colors */
 } __ec_todo_packed;
 
+struct lightbar_params_v3 {
+	/*
+	 *  Number of LEDs reported by the EC.
+	 *  May be less than the actual number of LEDs in the lightbar.
+	 */
+	uint8_t reported_led_num;
+} __ec_todo_packed;
+
 /* Lightbar program. */
 #define EC_LB_PROG_LEN 192
 struct lightbar_program {
@@ -2086,6 +2094,8 @@ struct ec_response_lightbar {
 		struct lightbar_params_v2_thresholds get_params_v2_thlds;
 		struct lightbar_params_v2_colors get_params_v2_colors;
 
+		struct lightbar_params_v3 get_params_v3;
+
 		struct __ec_todo_unpacked {
 			uint32_t num;
 			uint32_t flags;
@@ -2143,6 +2153,7 @@ enum lightbar_command {
 	LIGHTBAR_CMD_SET_PARAMS_V2_THRESHOLDS = 31,
 	LIGHTBAR_CMD_GET_PARAMS_V2_COLORS = 32,
 	LIGHTBAR_CMD_SET_PARAMS_V2_COLORS = 33,
+	LIGHTBAR_CMD_GET_PARAMS_V3 = 34,
 	LIGHTBAR_NUM_CMDS
 };
 
Signature · 1 line
2.52.0.457.g6b5491de43-goog
maintainer
#1 lore raw
Quoted 5 lines · Gwendal Grignou

Add attribue num_segments to return the number of exposed LED segments in the lightbar. It can be smaller than the number of physical leds in the lightbar.

Test: Check the attribute is present and returns a value when read.

Please use "platform/chrome" prefix for the commit title.

drivers/platform/chrome/cros_ec_lightbar.c static ssize_t version_show(struct device *dev, · line 179

Quoted 4 lines
drivers/platform/chrome/cros_ec_lightbar.c +4 -0
+static ssize_t num_segments_show(struct device *dev,
+				 struct device_attribute *attr, char *buf)
+{
+	uint32_t num = 0;

./scripts/checkpatch.pl --strict:
CHECK: Prefer kernel type 'u32' over 'uint32_t'

drivers/platform/chrome/cros_ec_lightbar.c static ssize_t version_show(struct device *dev, · line 189

Quoted 10 lines
drivers/platform/chrome/cros_ec_lightbar.c +10 -0
+	struct cros_ec_dev *ec = to_cros_ec_dev(dev);
+	int ret;
+
+	ret = lb_throttle();
+	if (ret)
+		return ret;
+
+	struct ec_params_lightbar *param;
+	struct ec_response_lightbar *resp;
+	struct cros_ec_command *msg;

For clarity, I prefer to move the 3 declarations before calling lb_throttle() in the case.

drivers/platform/chrome/cros_ec_lightbar.c static ssize_t version_show(struct device *dev, · line 198

Quoted 9 lines
drivers/platform/chrome/cros_ec_lightbar.c +9 -0
+
+	msg = alloc_lightbar_cmd_msg(ec);
+	if (!msg)
+		return -ENOMEM;
+
+	param = (struct ec_params_lightbar *)msg->data;
+	param->cmd = LIGHTBAR_CMD_GET_PARAMS_V3;
+	msg->outsize = sizeof(param->cmd);
+	msg->result = sizeof(resp->get_params_v3);
             ^^^^^^ insize
Quoted 9 lines
drivers/platform/chrome/cros_ec_lightbar.c +9 -0
+	ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
+	if (ret < 0 && ret != -EINVAL)
+		goto exit;
+
+	if (msg->result == EC_RES_SUCCESS) {
+		resp = (struct ec_response_lightbar *)msg->data;
+		num = resp->get_params_v3.reported_led_num;
+	}
+

drivers/platform/chrome/cros_ec_lightbar.c static ssize_t version_show(struct device *dev, · line 217

Quoted 10 lines
drivers/platform/chrome/cros_ec_lightbar.c +10 -0
+	/*
+	 * Anything else (ie, EC_RES_INVALID_COMMAND) - no direct control over
+	 * LEDs, return that no leds are supported.
+	 */
+	ret = 0;
+exit:
+	kfree(msg);
+	if (ret)
+		return ret;
+	return sysfs_emit(buf, "%d\n", num);
                                 ^ `num` is an u32.
Quoted 1 line

+}

How about:

      ret = sysfs_emit(buf, "%u\n", num);
  exit:
      kfree(msg);
      return ret;
Quoted 29 lines
include/linux/platform_data/cros_ec_commands.h +10 -0
index 69294f79cc88..9cbf024f56c3 100644
@@ -2005,6 +2005,14 @@ struct lightbar_params_v2_colors {
 	struct rgb_s color[8];			/* 0-3 are Google colors */
 } __ec_todo_packed;
 
+struct lightbar_params_v3 {
+	/*
+	 *  Number of LEDs reported by the EC.
+	 *  May be less than the actual number of LEDs in the lightbar.
+	 */
+	uint8_t reported_led_num;
+} __ec_todo_packed;
+
 /* Lightbar program. */
 #define EC_LB_PROG_LEN 192
 struct lightbar_program {
@@ -2086,6 +2094,8 @@ struct ec_response_lightbar {
 		struct lightbar_params_v2_thresholds get_params_v2_thlds;
 		struct lightbar_params_v2_colors get_params_v2_colors;
 
+		struct lightbar_params_v3 get_params_v3;
+
 		struct __ec_todo_unpacked {
 			uint32_t num;
 			uint32_t flags;

include/linux/platform_data/cros_ec_commands.h enum lightbar_command { · line 2158

Quoted 6 lines
include/linux/platform_data/cros_ec_commands.h +1 -0
@@ -2143,6 +2153,7 @@ enum lightbar_command {
 	LIGHTBAR_CMD_SET_PARAMS_V2_THRESHOLDS = 31,
 	LIGHTBAR_CMD_GET_PARAMS_V2_COLORS = 32,
 	LIGHTBAR_CMD_SET_PARAMS_V2_COLORS = 33,
+	LIGHTBAR_CMD_GET_PARAMS_V3 = 34,
 	LIGHTBAR_NUM_CMDS
 };

Haven't seen the change in
https://chromium.googlesource.com/chromiumos/platform/ec/+/refs/heads/main/include/ec_commands.h,
do we need to wait until the change landed in EC firmware?

#2 lore raw
Quoted 9 lines · Tzung-Bi Shih
Quoted 5 lines · Gwendal Grignou

Add attribue num_segments to return the number of exposed LED segments in the lightbar. It can be smaller than the number of physical leds in the lightbar.

Test: Check the attribute is present and returns a value when read.

Please use "platform/chrome" prefix for the commit title.

Done (in v2).

Quoted 8 lines

drivers/platform/chrome/cros_ec_lightbar.c static ssize_t version_show(struct device *dev, · line 179

Quoted 5 lines
drivers/platform/chrome/cros_ec_lightbar.c +4 -0
+static ssize_t num_segments_show(struct device *dev,
+                              struct device_attribute *attr, char *buf)
+{
+     uint32_t num = 0;

./scripts/checkpatch.pl --strict:
CHECK: Prefer kernel type 'u32' over 'uint32_t'

I kept uint... to be consistent with the rest of the code.

Quoted 14 lines

drivers/platform/chrome/cros_ec_lightbar.c static ssize_t version_show(struct device *dev, · line 189

Quoted 11 lines
drivers/platform/chrome/cros_ec_lightbar.c +10 -0
+     struct cros_ec_dev *ec = to_cros_ec_dev(dev);
+     int ret;
+
+     ret = lb_throttle();
+     if (ret)
+             return ret;
+
+     struct ec_params_lightbar *param;
+     struct ec_response_lightbar *resp;
+     struct cros_ec_command *msg;

For clarity, I prefer to move the 3 declarations before calling lb_throttle() in the case.

Done

Quoted 32 lines

drivers/platform/chrome/cros_ec_lightbar.c static ssize_t version_show(struct device *dev, · line 198

Quoted 10 lines
drivers/platform/chrome/cros_ec_lightbar.c +9 -0
+
+     msg = alloc_lightbar_cmd_msg(ec);
+     if (!msg)
+             return -ENOMEM;
+
+     param = (struct ec_params_lightbar *)msg->data;
+     param->cmd = LIGHTBAR_CMD_GET_PARAMS_V3;
+     msg->outsize = sizeof(param->cmd);
+     msg->result = sizeof(resp->get_params_v3);
             ^^^^^^ insize
Quoted 9 lines
drivers/platform/chrome/cros_ec_lightbar.c +9 -0
+     ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
+     if (ret < 0 && ret != -EINVAL)
+             goto exit;
+
+     if (msg->result == EC_RES_SUCCESS) {
+             resp = (struct ec_response_lightbar *)msg->data;
+             num = resp->get_params_v3.reported_led_num;
+     }
+

drivers/platform/chrome/cros_ec_lightbar.c static ssize_t version_show(struct device *dev, · line 217

Quoted 10 lines
drivers/platform/chrome/cros_ec_lightbar.c +10 -0
+     /*
+      * Anything else (ie, EC_RES_INVALID_COMMAND) - no direct control over
+      * LEDs, return that no leds are supported.
+      */
+     ret = 0;
+exit:
+     kfree(msg);
+     if (ret)
+             return ret;
+     return sysfs_emit(buf, "%d\n", num);
                                 ^ `num` is an u32.
Quoted 8 lines
Quoted 1 line

+}

How about:

      ret = sysfs_emit(buf, "%u\n", num);
  exit:
      kfree(msg);
      return ret;

Done.

Quoted 25 lines
Quoted 25 lines
include/linux/platform_data/cros_ec_commands.h +8 -0
index 69294f79cc88..9cbf024f56c3 100644
@@ -2005,6 +2005,14 @@ struct lightbar_params_v2_colors {
      struct rgb_s color[8];                  /* 0-3 are Google colors */
 } __ec_todo_packed;
 
+struct lightbar_params_v3 {
+     /*
+      *  Number of LEDs reported by the EC.
+      *  May be less than the actual number of LEDs in the lightbar.
+      */
+     uint8_t reported_led_num;
+} __ec_todo_packed;
+
 /* Lightbar program. */
 #define EC_LB_PROG_LEN 192
 struct lightbar_program {
@@ -2086,6 +2094,8 @@ struct ec_response_lightbar {
              struct lightbar_params_v2_thresholds get_params_v2_thlds;
              struct lightbar_params_v2_colors get_params_v2_colors;
 
Quoted 16 lines

include/linux/platform_data/cros_ec_commands.h enum lightbar_command { · line 2158

Quoted 12 lines
include/linux/platform_data/cros_ec_commands.h +3 -0
+             struct lightbar_params_v3 get_params_v3;
+
              struct __ec_todo_unpacked {
                      uint32_t num;
                      uint32_t flags;
@@ -2143,6 +2153,7 @@ enum lightbar_command {
      LIGHTBAR_CMD_SET_PARAMS_V2_THRESHOLDS = 31,
      LIGHTBAR_CMD_GET_PARAMS_V2_COLORS = 32,
      LIGHTBAR_CMD_SET_PARAMS_V2_COLORS = 33,
+     LIGHTBAR_CMD_GET_PARAMS_V3 = 34,
      LIGHTBAR_NUM_CMDS
 };

Haven't seen the change in
https://chromium.googlesource.com/chromiumos/platform/ec/+/refs/heads/main/include/ec_commands.h,
do we need to wait until the change landed in EC firmware?

For reference, crrev.com/c/7515877. Will post v2 once the change lands.