Skip to contentSkip to conversation

platform: arm64: Huawei Matebook E Go embedded controller

PATCH Superseded Pengyu Luo 6 participants 13 messages Jan 16, 2025 – Jan 17, 2025

Subsystems (7)

v4 of 7 Superseded Latest is v7 →

lore
Share & tools
lore

Pengyu Luo

author maintainer
#0 lore raw

v4 cover platform: arm64: Huawei Matebook E Go embedded controller

This adds binding, drivers and the DT support for the Huawei Matebook E Go (sc8280xp-based) Embedded Controller which is also found in Huawei Matebook E Go LTE (sc8180x-based), but I don't have the sc8180x one to perform tests, so this series enable support for sc8280xp variant only, this series provides the following features:

- battery and charger information report
- charging thresholds control
- FN lock (An alternative method)
- LID switch detection
- Temperature sensors
- USB Type-C altmode
- USB Type-C PD(high power)

Thanks to the work of Bjorn and Dmitry([1]), the work of Nikita([2]), writing a EC driver won't be suffering. This work refers a lot to their work, also, many other works. I mentioned them in commit messages.

Depends: https://lore.kernel.org/linux-arm-msm/[email protected]

[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/

base-commit: b323d8e7bc03d27dec646bfdccb7d1a92411f189

---
Changes in v4:
- use new API to register hwmon device instead of the deprecated one. (Guenter)
- add Reviewed-by tag for dt-binding (Krzysztof)
- drop unnecessary header (Ilpo)
- use guard mutex (Ilpo)
- improve comments and naming (Ilpo)
- add a shallow copy version of extr_resp() (Ilpo)
- add functions to handle resp and req whose size is 1
- drop PSY and UCSI subdrivers, commit them once the base driver is upstreamed
- Link to v3: https://lore.kernel.org/linux-arm-msm/[email protected]

Changes in v3:
- Link to v2: https://lore.kernel.org/linux-arm-msm/[email protected]

dt-binding:
- drop generic compatibles. (Krzysztof)
- remove '+' to use literal block style. (Krzysztof)

ec:
- take struct gaokun_ucsi_reg as parameter (Heikki)
- add almost all kernel doc comments (Krzysztof, Heikki)

ucsi:
- drop unnecessary ucsi quirks (Dmitry)
- add UCSI v1.0 to ucsi.h (Heikki)
- use gaokun_ucsi_read_cci() to read cci directly (Heikki)
- drop unnecessary gaokun_ucsi_get_port_num (Heikki)
- rename member port_num => num_ports (Heikki)
- fix completion, forgot to signal threads in previous version

dt:
- fix indentation (Konrad)
- add a link between role switch and connector

Changes in v2:
- Link to v1: https://lore.kernel.org/linux-arm-msm/[email protected]

global:
- drop qcom's products(i.e. sc8180x, sx8280xp) everywhere, use 'product'-based instead(Krzysztof, Bryan)
- drop Cc Nikita Travkin, we had discussed the device in PM.
- add myself to MAINTAINERS

dt-binding:
- fix building (Rob Herring (Arm))
- remove unnecessary code (Krzysztof)
- add bugzilla documentation, insights of gaokun(see [1] or patch[1/5]) (Krzysztof, Aiqun(Maria))
- explain the difference between PMIC GLink and gaokun EC (Aiqun(Maria))

ec:
- use Linux style comments (Krzysztof)
- add a comment for mutex lock (Krzysztof)
- add more kerneldoc for exported functions (Krzysztof)
- eliminate unnecessary conditions (Bryan)
- add a macro for check thresholds (Bryan)
- improve English (Bryan)
- use existing sysfs interface(hwmon, psy) whenever possible (Krzysztof)
- use __le16 and related endianess conversion function for temp data (Ilpo)
- drop alias for packet headers (Ilpo)
- avoid hardcoding i2c msgs size (Aiqun(Maria))
- add a comment for the sleep in critial region (Bryan, Aiqun(Maria))
- use macro to construct packet (Bryan, Aiqun(Maria))

wmi:
- dropped

ucsi:
- reorder headers (Bryan)
- a comment for the orientation map macro (Bryan)
- make mux mode map more explicit(minus six is very clear now) (Bryan, Dmitry)
- handle port update exceptions return (Bryan)
- a comment for the UCSI quirks (Dmitry)
- use the inline hint for the short register function (Dmitry)
- use the API with delay to handle register instead of a direct sleep (Bryan)
- handle unfinished initialization early

psy:
- add charging related sysfs to here (Krzysztof, Dmitry)
- document ABI for power_supply sysfs (Krzysztof)
- drop charging threshold, use smart charging instead

dts:
- correct indentation, properties' order. (Konrad)

Pengyu Luo (3):
dt-bindings: platform: Add Huawei Matebook E Go EC
platform: arm64: add Huawei Matebook E Go EC driver
arm64: dts: qcom: gaokun3: Add Embedded Controller node

 .../bindings/platform/huawei,gaokun-ec.yaml   | 124 +++
 MAINTAINERS                                   |   7 +
 .../boot/dts/qcom/sc8280xp-huawei-gaokun3.dts | 163 ++++
 drivers/platform/arm64/Kconfig                |  21 +
 drivers/platform/arm64/Makefile               |   1 +
 drivers/platform/arm64/huawei-gaokun-ec.c     | 787 ++++++++++++++++++
 .../linux/platform_data/huawei-gaokun-ec.h    |  80 ++
 7 files changed, 1183 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/platform/huawei,gaokun-ec.yaml
 create mode 100644 drivers/platform/arm64/huawei-gaokun-ec.c
 create mode 100644 include/linux/platform_data/huawei-gaokun-ec.h
Signature · 1 line
2.48.1
author maintainer
#1 lore raw

PATCH v4 1/3 dt-bindings: platform: Add Huawei Matebook E Go EC

Add binding for the EC found in the Huawei Matebook E Go and Huawei Matebook E Go LTE 2-in-1 tablets, the former one is a QS sc8280xp based tablet, the latter one is a QS sc8180x based tablet.

This series has a codename, gaokun. More information about gaokun, please check https://bugzilla.kernel.org/show_bug.cgi?id=219645

---
 .../bindings/platform/huawei,gaokun-ec.yaml   | 124 ++++++++++++++++++
 1 file changed, 124 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/platform/huawei,gaokun-ec.yaml
Documentation/devicetree/bindings/platform/huawei,gaokun-ec.yaml new file +124 -0
new file mode 100644
index 000000000..4a03b0ee3
@@ -0,0 +1,124 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/platform/huawei,gaokun-ec.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Huawei Matebook E Go Embedded Controller
+
+maintainers:
+  - Pengyu Luo <[email protected]>
+
+description:
+  Different from other Qualcomm Snapdragon sc8180x and sc8280xp-based
+  machines, the Huawei Matebook E Go tablets use embedded controllers
+  while others use a system called PMIC GLink which handles battery,
+  UCSI, USB Type-C DP Alt Mode. In addition, Huawei's implementation
+  also handles additional features, such as charging thresholds, FN
+  lock, smart charging, tablet lid status, thermal sensors, and more.
+
+properties:
+  compatible:
+    enum:
+      - huawei,gaokun3-ec
+
+  reg:
+    const: 0x38
+
+  '#address-cells':
+    const: 1
+
+  '#size-cells':
+    const: 0
+
+  interrupts:
+    maxItems: 1
+
+patternProperties:
+  '^connector@[01]$':
+    $ref: /schemas/connector/usb-connector.yaml#
+
+    properties:
+      reg:
+        maxItems: 1
+
+required:
+  - compatible
+  - reg
+  - interrupts
+
+additionalProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/interrupt-controller/irq.h>
+    i2c {
+        #address-cells = <1>;
+        #size-cells = <0>;
+
+        embedded-controller@38 {
+            compatible = "huawei,gaokun3-ec";
+            reg = <0x38>;
+
+            interrupts-extended = <&tlmm 107 IRQ_TYPE_LEVEL_LOW>;
+
+            #address-cells = <1>;
+            #size-cells = <0>;
+
+            connector@0 {
+                compatible = "usb-c-connector";
+                reg = <0>;
+                power-role = "dual";
+                data-role = "dual";
+
+                ports {
+                    #address-cells = <1>;
+                    #size-cells = <0>;
+
+                    port@0 {
+                        reg = <0>;
+
+                        ucsi0_ss_in: endpoint {
+                            remote-endpoint = <&usb_0_qmpphy_out>;
+                        };
+                    };
+
+                    port@1 {
+                        reg = <1>;
+
+                        ucsi0_sbu: endpoint {
+                            remote-endpoint = <&usb0_sbu_mux>;
+                        };
+                    };
+                };
+            };
+
+            connector@1 {
+                compatible = "usb-c-connector";
+                reg = <1>;
+                power-role = "dual";
+                data-role = "dual";
+
+                ports {
+                    #address-cells = <1>;
+                    #size-cells = <0>;
+
+                    port@0 {
+                        reg = <0>;
+
+                        ucsi1_ss_in: endpoint {
+                            remote-endpoint = <&usb_1_qmpphy_out>;
+                        };
+                    };
+
+                    port@1 {
+                        reg = <1>;
+
+                        ucsi1_sbu: endpoint {
+                            remote-endpoint = <&usb1_sbu_mux>;
+                        };
+                    };
+                };
+            };
+        };
+    };
Signature · 1 line
2.48.1
author maintainer
#2 lore raw

PATCH v4 2/3 platform: arm64: add Huawei Matebook E Go EC driver

In reply to Pengyu Luo

There are three variants of which Huawei released the first two simultaneously.

Huawei Matebook E Go LTE(sc8180x), codename seems to be gaokun2. Huawei Matebook E Go([email protected]), codename must be gaokun3. (see [1]) Huawei Matebook E Go 2023([email protected]), codename should be also gaokun3.

Adding support for the latter two variants for now, this driver should also work for the sc8180x variant according to acpi table files, but I don't have the device to test yet.

Different from other Qualcomm Snapdragon sc8280xp based machines, the Huawei Matebook E Go uses an embedded controller while others use a system called PMIC GLink. This embedded controller can be used to perform a set of various functions, including, but not limited to:

- Battery and charger monitoring;
- Charge control and smart charge;
- Fn_lock settings;
- Tablet lid status;
- Temperature sensors;
- USB Type-C notifications (ports orientation, DP alt mode HPD);
- USB Type-C PD (according to observation, up to 48w).

Add a driver for the EC which creates devices for UCSI and power supply devices.

This driver is inspired by the following drivers:

        drivers/platform/arm64/acer-aspire1-ec.c
        drivers/platform/arm64/lenovo-yoga-c630.c
        drivers/platform/x86/huawei-wmi.c

[1] https://bugzilla.kernel.org/show_bug.cgi?id=219645

---
 MAINTAINERS                                   |   7 +
 drivers/platform/arm64/Kconfig                |  21 +
 drivers/platform/arm64/Makefile               |   1 +
 drivers/platform/arm64/huawei-gaokun-ec.c     | 787 ++++++++++++++++++
 .../linux/platform_data/huawei-gaokun-ec.h    |  80 ++
 5 files changed, 896 insertions(+)
 create mode 100644 drivers/platform/arm64/huawei-gaokun-ec.c
 create mode 100644 include/linux/platform_data/huawei-gaokun-ec.h
MAINTAINERS +7 -0
index 33b9cd11a..27ff24e7d 100644
@@ -10692,6 +10692,13 @@ S:	Maintained
 F:	Documentation/networking/device_drivers/ethernet/huawei/hinic.rst
 F:	drivers/net/ethernet/huawei/hinic/
 
+HUAWEI MATEBOOK E GO EMBEDDED CONTROLLER DRIVER
+M:	Pengyu Luo <[email protected]>
+S:	Maintained
+F:	Documentation/devicetree/bindings/platform/huawei,gaokun-ec.yaml
+F:	drivers/platform/arm64/huawei-gaokun-ec.c
+F:	include/linux/platform_data/huawei-gaokun-ec.h
+
 HUGETLB SUBSYSTEM
 M:	Muchun Song <[email protected]>
 L:	[email protected]
drivers/platform/arm64/Kconfig +21 -0
index f88395ea3..6ceee3c16 100644
@@ -33,6 +33,27 @@ config EC_ACER_ASPIRE1
 	  laptop where this information is not properly exposed via the
 	  standard ACPI devices.
 
+config EC_HUAWEI_GAOKUN
+	tristate "Huawei Matebook E Go Embedded Controller driver"
+	depends on ARCH_QCOM || COMPILE_TEST
+	depends on I2C
+	depends on INPUT
+	select AUXILIARY_BUS
+
+	help
+	  Say Y here to enable the EC driver for the Huawei Matebook E Go
+	  which is a sc8280xp-based 2-in-1 tablet. The driver handles battery
+	  (information, charge control) and USB Type-C DP HPD events as well
+	  as some misc functions like the lid sensor and temperature sensors,
+	  etc.
+
+	  This driver provides battery and AC status support for the mentioned
+	  laptop where this information is not properly exposed via the
+	  standard ACPI devices.
+
+	  Say M or Y here to include this support.
+
+
 config EC_LENOVO_YOGA_C630
 	tristate "Lenovo Yoga C630 Embedded Controller driver"
 	depends on ARCH_QCOM || COMPILE_TEST
drivers/platform/arm64/Makefile +1 -0
index b2ae9114f..46a99eba3 100644
@@ -6,4 +6,5 @@
 #
 
 obj-$(CONFIG_EC_ACER_ASPIRE1)	+= acer-aspire1-ec.o
+obj-$(CONFIG_EC_HUAWEI_GAOKUN)	+= huawei-gaokun-ec.o
 obj-$(CONFIG_EC_LENOVO_YOGA_C630) += lenovo-yoga-c630.o
drivers/platform/arm64/huawei-gaokun-ec.c new file +787 -0

787 lines not on this page · raw message

include/linux/platform_data/huawei-gaokun-ec.h new file +80 -0

80 lines not on this page · raw message

Signature · 1 line
2.48.1
author maintainer
#11 lore raw

PATCH v4 3/3 arm64: dts: qcom: gaokun3: Add Embedded Controller node

In reply to Pengyu Luo

The Embedded Controller in the Huawei Matebook E Go is accessible on &i2c15 and provides battery and adapter status, port orientation status, as well as HPD event notifications for two USB Type-C port, etc.

Add the EC to the device tree and describe the relationship among the type-c connectors, role switches, orientation switches and the QMP combo PHY.

---
 .../boot/dts/qcom/sc8280xp-huawei-gaokun3.dts | 163 ++++++++++++++++++
 1 file changed, 163 insertions(+)
arch/arm64/boot/dts/qcom/sc8280xp-huawei-gaokun3.dts +163 -0
index 09b95f89e..1667c7157 100644
@@ -28,6 +28,7 @@ / {
 
 	aliases {
 		i2c4 = &i2c4;
+		i2c15 = &i2c15;
 		serial1 = &uart2;
 	};
 
@@ -216,6 +217,40 @@ map1 {
 		};
 	};
 
+	usb0-sbu-mux {
+		compatible = "pericom,pi3usb102", "gpio-sbu-mux";
+
+		select-gpios = <&tlmm 164 GPIO_ACTIVE_HIGH>;
+
+		pinctrl-0 = <&usb0_sbu_default>;
+		pinctrl-names = "default";
+
+		orientation-switch;
+
+		port {
+			usb0_sbu_mux: endpoint {
+				remote-endpoint = <&ucsi0_sbu>;
+			};
+		};
+	};
+
+	usb1-sbu-mux {
+		compatible = "pericom,pi3usb102", "gpio-sbu-mux";
+
+		select-gpios = <&tlmm 47 GPIO_ACTIVE_HIGH>;
+
+		pinctrl-0 = <&usb1_sbu_default>;
+		pinctrl-names = "default";
+
+		orientation-switch;
+
+		port {
+			usb1_sbu_mux: endpoint {
+				remote-endpoint = <&ucsi1_sbu>;
+			};
+		};
+	};
+
 	wcn6855-pmu {
 		compatible = "qcom,wcn6855-pmu";
 
@@ -584,6 +619,97 @@ touchscreen@4f {
 
 };
 
+&i2c15 {
+	clock-frequency = <400000>;
+
+	pinctrl-0 = <&i2c15_default>;
+	pinctrl-names = "default";
+
+	status = "okay";
+
+	embedded-controller@38 {
+		compatible = "huawei,gaokun3-ec";
+		reg = <0x38>;
+
+		interrupts-extended = <&tlmm 107 IRQ_TYPE_LEVEL_LOW>;
+
+		#address-cells = <1>;
+		#size-cells = <0>;
+
+		connector@0 {
+			compatible = "usb-c-connector";
+			reg = <0>;
+			power-role = "dual";
+			data-role = "dual";
+
+			ports {
+				#address-cells = <1>;
+				#size-cells = <0>;
+
+				port@0 {
+					reg = <0>;
+
+					ucsi0_hs_in: endpoint {
+						remote-endpoint = <&usb_0_dwc3_hs>;
+					};
+				};
+
+				port@1 {
+					reg = <1>;
+
+					ucsi0_ss_in: endpoint {
+						remote-endpoint = <&usb_0_qmpphy_out>;
+					};
+				};
+
+				port@2 {
+					reg = <2>;
+
+					ucsi0_sbu: endpoint {
+						remote-endpoint = <&usb0_sbu_mux>;
+					};
+				};
+			};
+		};
+
+		connector@1 {
+			compatible = "usb-c-connector";
+			reg = <1>;
+			power-role = "dual";
+			data-role = "dual";
+
+			ports {
+				#address-cells = <1>;
+				#size-cells = <0>;
+
+				port@0 {
+					reg = <0>;
+
+					ucsi1_hs_in: endpoint {
+						remote-endpoint = <&usb_1_dwc3_hs>;
+					};
+				};
+
+				port@1 {
+					reg = <1>;
+
+					ucsi1_ss_in: endpoint {
+						remote-endpoint = <&usb_1_qmpphy_out>;
+					};
+				};
+
+				port@2 {
+					reg = <2>;
+
+					ucsi1_sbu: endpoint {
+						remote-endpoint = <&usb1_sbu_mux>;
+					};
+				};
+			};
+		};
+	};
+};
+
 &mdss0 {
 	status = "okay";
 };
@@ -1004,6 +1130,10 @@ &usb_0_dwc3 {
 	dr_mode = "host";
 };
 
+&usb_0_dwc3_hs {
+	remote-endpoint = <&ucsi0_hs_in>;
+};
+
 &usb_0_hsphy {
 	vdda-pll-supply = <&vreg_l9d>;
 	vdda18-supply = <&vreg_l1c>;
@@ -1025,6 +1155,10 @@ &usb_0_qmpphy_dp_in {
 	remote-endpoint = <&mdss0_dp0_out>;
 };
 
+&usb_0_qmpphy_out {
+	remote-endpoint = <&ucsi0_ss_in>;
+};
+
 &usb_1 {
 	status = "okay";
 };
@@ -1033,6 +1167,10 @@ &usb_1_dwc3 {
 	dr_mode = "host";
 };
 
+&usb_1_dwc3_hs {
+	remote-endpoint = <&ucsi1_hs_in>;
+};
+
 &usb_1_hsphy {
 	vdda-pll-supply = <&vreg_l4b>;
 	vdda18-supply = <&vreg_l1c>;
@@ -1054,6 +1192,10 @@ &usb_1_qmpphy_dp_in {
 	remote-endpoint = <&mdss0_dp1_out>;
 };
 
+&usb_1_qmpphy_out {
+	remote-endpoint = <&ucsi1_ss_in>;
+};
+
 &usb_2 {
 	status = "okay";
 };
@@ -1177,6 +1319,13 @@ i2c4_default: i2c4-default-state {
 		bias-disable;
 	};
 
+	i2c15_default: i2c15-default-state {
+		pins = "gpio36", "gpio37";
+		function = "qup15";
+		drive-strength = <2>;
+		bias-pull-up;
+	};
+
 	mode_pin_active: mode-pin-state {
 		pins = "gpio26";
 		function = "gpio";
@@ -1301,6 +1450,20 @@ tx-pins {
 		};
 	};
 
+	usb0_sbu_default: usb0-sbu-state {
+		pins = "gpio164";
+		function = "gpio";
+		drive-strength = <16>;
+		bias-disable;
+	};
+
+	usb1_sbu_default: usb1-sbu-state {
+		pins = "gpio47";
+		function = "gpio";
+		drive-strength = <16>;
+		bias-disable;
+	};
+
 	wcd_default: wcd-default-state {
 		reset-pins {
 			pins = "gpio106";
Signature · 1 line
2.48.1
Reviewed-by on v4 3/3
#12 lore raw

Re: [PATCH v4 3/3] arm64: dts: qcom: gaokun3: Add Embedded Controller node

Quoted 11 lines · Pengyu Luo

The Embedded Controller in the Huawei Matebook E Go is accessible on &i2c15 and provides battery and adapter status, port orientation status, as well as HPD event notifications for two USB Type-C port, etc.

Add the EC to the device tree and describe the relationship among the type-c connectors, role switches, orientation switches and the QMP combo PHY.

---
 .../boot/dts/qcom/sc8280xp-huawei-gaokun3.dts | 163 ++++++++++++++++++

Best regards,
Krzysztof

#3 lore raw

Re: [PATCH v4 2/3] platform: arm64: add Huawei Matebook E Go EC driver

In reply to Pengyu Luo
Quoted 223 lines · Pengyu Luo

There are three variants of which Huawei released the first two simultaneously.

Huawei Matebook E Go LTE(sc8180x), codename seems to be gaokun2. Huawei Matebook E Go([email protected]), codename must be gaokun3. (see [1]) Huawei Matebook E Go 2023([email protected]), codename should be also gaokun3.

Adding support for the latter two variants for now, this driver should also work for the sc8180x variant according to acpi table files, but I don't have the device to test yet.

Different from other Qualcomm Snapdragon sc8280xp based machines, the Huawei Matebook E Go uses an embedded controller while others use a system called PMIC GLink. This embedded controller can be used to perform a set of various functions, including, but not limited to:

- Battery and charger monitoring;
- Charge control and smart charge;
- Fn_lock settings;
- Tablet lid status;
- Temperature sensors;
- USB Type-C notifications (ports orientation, DP alt mode HPD);
- USB Type-C PD (according to observation, up to 48w).

Add a driver for the EC which creates devices for UCSI and power supply devices.

This driver is inspired by the following drivers:

         drivers/platform/arm64/acer-aspire1-ec.c
         drivers/platform/arm64/lenovo-yoga-c630.c
         drivers/platform/x86/huawei-wmi.c

[1] https://bugzilla.kernel.org/show_bug.cgi?id=219645

---
  MAINTAINERS                                   |   7 +
  drivers/platform/arm64/Kconfig                |  21 +
  drivers/platform/arm64/Makefile               |   1 +
  drivers/platform/arm64/huawei-gaokun-ec.c     | 787 ++++++++++++++++++
  .../linux/platform_data/huawei-gaokun-ec.h    |  80 ++
  5 files changed, 896 insertions(+)

create mode 100644 drivers/platform/arm64/huawei-gaokun-ec.c create mode 100644 include/linux/platform_data/huawei-gaokun-ec.h

MAINTAINERS +7 -0
index 33b9cd11a..27ff24e7d 100644
@@ -10692,6 +10692,13 @@ S:	Maintained
  F:	Documentation/networking/device_drivers/ethernet/huawei/hinic.rst
  F:	drivers/net/ethernet/huawei/hinic/
 
+HUAWEI MATEBOOK E GO EMBEDDED CONTROLLER DRIVER
+M:	Pengyu Luo <[email protected]>
+S:	Maintained
+F:	Documentation/devicetree/bindings/platform/huawei,gaokun-ec.yaml
+F:	drivers/platform/arm64/huawei-gaokun-ec.c
+F:	include/linux/platform_data/huawei-gaokun-ec.h
+
  HUGETLB SUBSYSTEM
  M:	Muchun Song <[email protected]>
  L:	[email protected]
drivers/platform/arm64/Kconfig +21 -0
index f88395ea3..6ceee3c16 100644
@@ -33,6 +33,27 @@ config EC_ACER_ASPIRE1
  	  laptop where this information is not properly exposed via the
  	  standard ACPI devices.
 
+config EC_HUAWEI_GAOKUN
+	tristate "Huawei Matebook E Go Embedded Controller driver"
+	depends on ARCH_QCOM || COMPILE_TEST
+	depends on I2C
+	depends on INPUT
+	select AUXILIARY_BUS
+
+	help
+	  Say Y here to enable the EC driver for the Huawei Matebook E Go
+	  which is a sc8280xp-based 2-in-1 tablet. The driver handles battery
+	  (information, charge control) and USB Type-C DP HPD events as well
+	  as some misc functions like the lid sensor and temperature sensors,
+	  etc.
+
+	  This driver provides battery and AC status support for the mentioned
+	  laptop where this information is not properly exposed via the
+	  standard ACPI devices.
+
+	  Say M or Y here to include this support.
+
+
  config EC_LENOVO_YOGA_C630
  	tristate "Lenovo Yoga C630 Embedded Controller driver"
  	depends on ARCH_QCOM || COMPILE_TEST
drivers/platform/arm64/Makefile +1 -0
index b2ae9114f..46a99eba3 100644
@@ -6,4 +6,5 @@
  #
 
  obj-$(CONFIG_EC_ACER_ASPIRE1)	+= acer-aspire1-ec.o
+obj-$(CONFIG_EC_HUAWEI_GAOKUN)	+= huawei-gaokun-ec.o
  obj-$(CONFIG_EC_LENOVO_YOGA_C630) += lenovo-yoga-c630.o
drivers/platform/arm64/huawei-gaokun-ec.c new file +112 -0
new file mode 100644
index 000000000..5b09b7d7c
@@ -0,0 +1,787 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * huawei-gaokun-ec - An EC driver for HUAWEI Matebook E Go
+ *
+ * Copyright (C) 2024-2025 Pengyu Luo <[email protected]>
+ */
+
+#include <linux/auxiliary_bus.h>
+#include <linux/cleanup.h>
+#include <linux/delay.h>
+#include <linux/device.h>
+#include <linux/hwmon.h>
+#include <linux/hwmon-sysfs.h>
+#include <linux/i2c.h>
+#include <linux/input.h>
+#include <linux/notifier.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/platform_data/huawei-gaokun-ec.h>
+
+#define EC_EVENT		0x06
+
+/* Also can be found in ACPI specification 12.3 */
+#define EC_READ			0x80
+#define EC_WRITE		0x81
+#define EC_BURST		0x82
+#define EC_QUERY		0x84
+
+#define EC_FN_LOCK_ON		0x5A
+#define EC_FN_LOCK_OFF		0x55
+
+#define EC_EVENT_LID		0x81
+
+#define EC_LID_STATE		0x80
+#define EC_LID_OPEN		BIT(1)
+
+#define UCSI_REG_SIZE		7
+
+/*
+ * For tx, command sequences are arranged as
+ * {master_cmd, slave_cmd, data_len, data_seq}
+ */
+#define REQ_HDR_SIZE		3
+#define INPUT_SIZE_OFFSET	2
+#define REQ_LEN(req) (REQ_HDR_SIZE + req[INPUT_SIZE_OFFSET])
+
+/*
+ * For rx, data sequences are arranged as
+ * {status, data_len(unreliable), data_seq}
+ */
+#define RESP_HDR_SIZE		2
+
+#define MKREQ(REG0, REG1, SIZE, ...)			\
+{							\
+	REG0, REG1, SIZE,				\
+	/* ## will remove comma when SIZE is 0 */	\
+	## __VA_ARGS__,					\
+	/* make sure len(pkt[3:]) >= SIZE */		\
+	[3 + SIZE] = 0,					\
+}
+
+#define MKRESP(SIZE)				\
+{						\
+	[RESP_HDR_SIZE + SIZE - 1] = 0,		\
+}
+
+/* Possible size 1, 4, 20, 24. Most of the time, the size is 1. */
+static inline void refill_req(u8 *dest, const u8 *src, size_t size)
+{
+	memcpy(dest + REQ_HDR_SIZE, src, size);
+}
+
+static inline void refill_req_byte(u8 *dest, const u8 *src)
+{
+	dest[REQ_HDR_SIZE] = *src;
+}
+
+/* Possible size 1, 2, 4, 7, 20. Most of the time, the size is 1. */
+static inline void extr_resp(u8 *dest, const u8 *src, size_t size)
+{
+	memcpy(dest, src + RESP_HDR_SIZE, size);
+}
+
+static inline void extr_resp_byte(u8 *dest, const u8 *src)
+{
+	*dest = src[RESP_HDR_SIZE];
+}
+
+static inline void *extr_resp_shallow(const u8 *src)
+{
+	return src + RESP_HDR_SIZE;
+}
+
+struct gaokun_ec {
+	struct i2c_client *client;
+	struct mutex lock; /* EC transaction lock */
+	struct blocking_notifier_head notifier_list;
+	struct device *hwmon_dev;
+	struct input_dev *idev;
+	bool suspended;
+};
+
+static int gaokun_ec_request(struct gaokun_ec *ec, const u8 *req,
+			     size_t resp_len, u8 *resp)
+{
+	struct i2c_client *client = ec->client;
+	struct i2c_msg msgs[2] = {
+		{
+			.addr = client->addr,
+			.flags = client->flags,
+			.len = REQ_LEN(req),
+			.buf = req,

drivers/platform/arm64/huawei-gaokun-ec.c line 122

Quoted 10 lines
drivers/platform/arm64/huawei-gaokun-ec.c new file +10 -0
+		}, {
+			.addr = client->addr,
+			.flags = client->flags | I2C_M_RD,
+			.len = resp_len,
+			.buf = resp,
+		},
+	};
+
+	guard(mutex)(&ec->lock);
+	i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));

You should trap the result code of i2c_transfer() and push it up the call stack.

drivers/platform/arm64/huawei-gaokun-ec.c line 125

Quoted 3 lines
drivers/platform/arm64/huawei-gaokun-ec.c new file +3 -0
+	usleep_range(2000, 2500); /* have a break, ACPI did this */
+
+	return *resp ? -EIO : 0;

If the value @ *resp is non-zero return -EIO ?

Why ?

Quoted 138 lines
drivers/platform/arm64/huawei-gaokun-ec.c new file +138 -0
+}
+
+/* -------------------------------------------------------------------------- */
+/* Common API */
+
+/**
+ * gaokun_ec_read - Read from EC
+ * @ec: The gaokun_ec structure
+ * @req: The sequence to request
+ * @resp_len: The size to read
+ * @resp: The buffer to store response sequence
+ *
+ * This function is used to read data after writing a magic sequence to EC.
+ * All EC operations depend on this function.
+ *
+ * Huawei uses magic sequences everywhere to complete various functions, all
+ * these sequences are passed to ECCD(a ACPI method which is quiet similar
+ * to gaokun_ec_request), there is no good abstraction to generalize these
+ * sequences, so just wrap it for now. Almost all magic sequences are kept
+ * in this file.
+ *
+ * Return: 0 on success or negative error code.
+ */
+int gaokun_ec_read(struct gaokun_ec *ec, const u8 *req,
+		   size_t resp_len, u8 *resp)
+{
+	return gaokun_ec_request(ec, req, resp_len, resp);
+}
+EXPORT_SYMBOL_GPL(gaokun_ec_read);
+
+/**
+ * gaokun_ec_write - Write to EC
+ * @ec: The gaokun_ec structure
+ * @req: The sequence to request
+ *
+ * This function has no big difference from gaokun_ec_read. When caller care
+ * only write status and no actual data are returned, then use it.
+ *
+ * Return: 0 on success or negative error code.
+ */
+int gaokun_ec_write(struct gaokun_ec *ec, const u8 *req)
+{
+	u8 ec_resp[] = MKRESP(0);
+
+	return gaokun_ec_request(ec, req, sizeof(ec_resp), ec_resp);
+}
+EXPORT_SYMBOL_GPL(gaokun_ec_write);
+
+int gaokun_ec_read_byte(struct gaokun_ec *ec, const u8 *req, u8 *byte)
+{
+	int ret;
+	u8 ec_resp[] = MKRESP(sizeof(*byte));
+
+	ret = gaokun_ec_read(ec, req, sizeof(ec_resp), ec_resp);
+	extr_resp_byte(byte, ec_resp);
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(gaokun_ec_read_byte);
+
+/**
+ * gaokun_ec_register_notify - Register a notifier callback for EC events.
+ * @ec: The gaokun_ec structure
+ * @nb: Notifier block pointer to register
+ *
+ * Return: 0 on success or negative error code.
+ */
+int gaokun_ec_register_notify(struct gaokun_ec *ec, struct notifier_block *nb)
+{
+	return blocking_notifier_chain_register(&ec->notifier_list, nb);
+}
+EXPORT_SYMBOL_GPL(gaokun_ec_register_notify);
+
+/**
+ * gaokun_ec_unregister_notify - Unregister notifier callback for EC events.
+ * @ec: The gaokun_ec structure
+ * @nb: Notifier block pointer to unregister
+ *
+ * Unregister a notifier callback that was previously registered with
+ * gaokun_ec_register_notify().
+ */
+void gaokun_ec_unregister_notify(struct gaokun_ec *ec, struct notifier_block *nb)
+{
+	blocking_notifier_chain_unregister(&ec->notifier_list, nb);
+}
+EXPORT_SYMBOL_GPL(gaokun_ec_unregister_notify);
+
+/* -------------------------------------------------------------------------- */
+/* API for PSY */
+
+/**
+ * gaokun_ec_psy_multi_read - Read contiguous registers
+ * @ec: The gaokun_ec structure
+ * @reg: The start register
+ * @resp_len: The number of registers to be read
+ * @resp: The buffer to store response sequence
+ *
+ * Return: 0 on success or negative error code.
+ */
+int gaokun_ec_psy_multi_read(struct gaokun_ec *ec, u8 reg,
+			     size_t resp_len, u8 *resp)
+{
+	u8 ec_req[] = MKREQ(0x02, EC_READ, 1, 0);
+	u8 ec_resp[] = MKRESP(1);
+	int i, ret;
+
+	for (i = 0; i < resp_len; ++i, reg++) {
+		refill_req_byte(ec_req, &reg);
+		ret = gaokun_ec_read(ec, ec_req, sizeof(ec_resp), ec_resp);
+		if (ret)
+			return ret;
+		extr_resp_byte(&resp[i], ec_resp);
+	}
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(gaokun_ec_psy_multi_read);
+
+/* Smart charge */
+
+/**
+ * gaokun_ec_psy_get_smart_charge - Get smart charge data from EC
+ * @ec: The gaokun_ec structure
+ * @resp: The buffer to store response sequence (mode, delay, start, end)
+ *
+ * Return: 0 on success or negative error code.
+ */
+int gaokun_ec_psy_get_smart_charge(struct gaokun_ec *ec,
+				   u8 resp[GAOKUN_SMART_CHARGE_DATA_SIZE])
+{
+	/* GBCM */
+	u8 ec_req[] = MKREQ(0x02, 0xE4, 0);
+	u8 ec_resp[] = MKRESP(GAOKUN_SMART_CHARGE_DATA_SIZE);
+	int ret;
+
+	ret = gaokun_ec_read(ec, ec_req, sizeof(ec_resp), ec_resp);
+	if (ret)
+		return ret;

drivers/platform/arm64/huawei-gaokun-ec.c line 273

Quoted 10 lines
drivers/platform/arm64/huawei-gaokun-ec.c new file +10 -0
+
+	extr_resp(resp, ec_resp, GAOKUN_SMART_CHARGE_DATA_SIZE);
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(gaokun_ec_psy_get_smart_charge);
+
+static inline bool are_thresholds_valid(u8 start, u8 end)
+{
+	return end != 0 && start <= end && end <= 100;

Why 100 ? Still feels like an arbitrary number.

Could you add a comment to explain where 100 comes from ?

Quoted 27 lines
drivers/platform/arm64/huawei-gaokun-ec.c new file +27 -0
+}
+
+/**
+ * gaokun_ec_psy_set_smart_charge - Set smart charge data
+ * @ec: The gaokun_ec structure
+ * @req: The sequence to request (mode, delay, start, end)
+ *
+ * Return: 0 on success or negative error code.
+ */
+int gaokun_ec_psy_set_smart_charge(struct gaokun_ec *ec,
+				   const u8 req[GAOKUN_SMART_CHARGE_DATA_SIZE])
+{
+	/* SBCM */
+	u8 ec_req[] = MKREQ(0x02, 0xE3, GAOKUN_SMART_CHARGE_DATA_SIZE);
+
+	if (!are_thresholds_valid(req[2], req[3]))
+		return -EINVAL;
+
+	refill_req(ec_req, req, GAOKUN_SMART_CHARGE_DATA_SIZE);
+
+	return gaokun_ec_write(ec, ec_req);
+}
+EXPORT_SYMBOL_GPL(gaokun_ec_psy_set_smart_charge);
+
+/* Smart charge enable */
+
+/**

drivers/platform/arm64/huawei-gaokun-ec.c line 310

Quoted 10 lines
drivers/platform/arm64/huawei-gaokun-ec.c new file +10 -0
+ * gaokun_ec_psy_get_smart_charge_enable - Get smart charge state
+ * @ec: The gaokun_ec structure
+ * @on: The state
+ *
+ * Return: 0 on success or negative error code.
+ */
+int gaokun_ec_psy_get_smart_charge_enable(struct gaokun_ec *ec, bool *on)
+{
+	/* GBAC */
+	u8 ec_req[] = MKREQ(0x02, 0xE6, 0);

0xE6 == SMART_CHARGE_ENABLE right ?

In which case instead of passing magic numbers inline, it would be nice to declare an enum or set of defines that enumerates the command set and then pass those values to MKREQ instead of the hex.

Does the first parameter indicate write ? 0x02. 0x03 seems to indicate read too ?

If so again please name the hex values as defines/enums and then pass the descriptive names.

Looking much more readable now though.

---
bod

author maintainer
#4 lore raw

Re: [PATCH v4 2/3] platform: arm64: add Huawei Matebook E Go EC driver

Quoted 229 lines · Bryan O'Donoghue
Quoted 228 lines · Pengyu Luo

There are three variants of which Huawei released the first two simultaneously.

Huawei Matebook E Go LTE(sc8180x), codename seems to be gaokun2. Huawei Matebook E Go([email protected]), codename must be gaokun3. (see [1]) Huawei Matebook E Go 2023([email protected]), codename should be also gaokun3.

Adding support for the latter two variants for now, this driver should also work for the sc8180x variant according to acpi table files, but I don't have the device to test yet.

Different from other Qualcomm Snapdragon sc8280xp based machines, the Huawei Matebook E Go uses an embedded controller while others use a system called PMIC GLink. This embedded controller can be used to perform a set of various functions, including, but not limited to:

- Battery and charger monitoring;
- Charge control and smart charge;
- Fn_lock settings;
- Tablet lid status;
- Temperature sensors;
- USB Type-C notifications (ports orientation, DP alt mode HPD);
- USB Type-C PD (according to observation, up to 48w).

Add a driver for the EC which creates devices for UCSI and power supply devices.

This driver is inspired by the following drivers:

         drivers/platform/arm64/acer-aspire1-ec.c
         drivers/platform/arm64/lenovo-yoga-c630.c
         drivers/platform/x86/huawei-wmi.c

[1] https://bugzilla.kernel.org/show_bug.cgi?id=219645

---
  MAINTAINERS                                   |   7 +
  drivers/platform/arm64/Kconfig                |  21 +
  drivers/platform/arm64/Makefile               |   1 +
  drivers/platform/arm64/huawei-gaokun-ec.c     | 787 ++++++++++++++++++
  .../linux/platform_data/huawei-gaokun-ec.h    |  80 ++
  5 files changed, 896 insertions(+)

create mode 100644 drivers/platform/arm64/huawei-gaokun-ec.c create mode 100644 include/linux/platform_data/huawei-gaokun-ec.h

MAINTAINERS +7 -0
index 33b9cd11a..27ff24e7d 100644
@@ -10692,6 +10692,13 @@ S:   Maintained
  F:  Documentation/networking/device_drivers/ethernet/huawei/hinic.rst
  F:  drivers/net/ethernet/huawei/hinic/
 
+HUAWEI MATEBOOK E GO EMBEDDED CONTROLLER DRIVER
+M:   Pengyu Luo <[email protected]>
+S:   Maintained
+F:   Documentation/devicetree/bindings/platform/huawei,gaokun-ec.yaml
+F:   drivers/platform/arm64/huawei-gaokun-ec.c
+F:   include/linux/platform_data/huawei-gaokun-ec.h
+
  HUGETLB SUBSYSTEM
  M:  Muchun Song <[email protected]>
  L:  [email protected]
drivers/platform/arm64/Kconfig +21 -0
index f88395ea3..6ceee3c16 100644
@@ -33,6 +33,27 @@ config EC_ACER_ASPIRE1
        laptop where this information is not properly exposed via the
        standard ACPI devices.
 
+config EC_HUAWEI_GAOKUN
+     tristate "Huawei Matebook E Go Embedded Controller driver"
+     depends on ARCH_QCOM || COMPILE_TEST
+     depends on I2C
+     depends on INPUT
+     select AUXILIARY_BUS
+
+     help
+       Say Y here to enable the EC driver for the Huawei Matebook E Go
+       which is a sc8280xp-based 2-in-1 tablet. The driver handles battery
+       (information, charge control) and USB Type-C DP HPD events as well
+       as some misc functions like the lid sensor and temperature sensors,
+       etc.
+
+       This driver provides battery and AC status support for the mentioned
+       laptop where this information is not properly exposed via the
+       standard ACPI devices.
+
+       Say M or Y here to include this support.
+
+
  config EC_LENOVO_YOGA_C630
      tristate "Lenovo Yoga C630 Embedded Controller driver"
      depends on ARCH_QCOM || COMPILE_TEST
drivers/platform/arm64/Makefile +1 -0
index b2ae9114f..46a99eba3 100644
@@ -6,4 +6,5 @@
  #
 
  obj-$(CONFIG_EC_ACER_ASPIRE1)       += acer-aspire1-ec.o
+obj-$(CONFIG_EC_HUAWEI_GAOKUN)       += huawei-gaokun-ec.o
  obj-$(CONFIG_EC_LENOVO_YOGA_C630) += lenovo-yoga-c630.o
drivers/platform/arm64/huawei-gaokun-ec.c new file +117 -0
new file mode 100644
index 000000000..5b09b7d7c
@@ -0,0 +1,787 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * huawei-gaokun-ec - An EC driver for HUAWEI Matebook E Go
+ *
+ * Copyright (C) 2024-2025 Pengyu Luo <[email protected]>
+ */
+
+#include <linux/auxiliary_bus.h>
+#include <linux/cleanup.h>
+#include <linux/delay.h>
+#include <linux/device.h>
+#include <linux/hwmon.h>
+#include <linux/hwmon-sysfs.h>
+#include <linux/i2c.h>
+#include <linux/input.h>
+#include <linux/notifier.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/platform_data/huawei-gaokun-ec.h>
+
+#define EC_EVENT             0x06
+
+/* Also can be found in ACPI specification 12.3 */
+#define EC_READ                      0x80
+#define EC_WRITE             0x81
+#define EC_BURST             0x82
+#define EC_QUERY             0x84
+
+#define EC_FN_LOCK_ON                0x5A
+#define EC_FN_LOCK_OFF               0x55
+
+#define EC_EVENT_LID         0x81
+
+#define EC_LID_STATE         0x80
+#define EC_LID_OPEN          BIT(1)
+
+#define UCSI_REG_SIZE                7
+
+/*
+ * For tx, command sequences are arranged as
+ * {master_cmd, slave_cmd, data_len, data_seq}
+ */
+#define REQ_HDR_SIZE         3
+#define INPUT_SIZE_OFFSET    2
+#define REQ_LEN(req) (REQ_HDR_SIZE + req[INPUT_SIZE_OFFSET])
+
+/*
+ * For rx, data sequences are arranged as
+ * {status, data_len(unreliable), data_seq}
+ */
+#define RESP_HDR_SIZE                2
+
+#define MKREQ(REG0, REG1, SIZE, ...)                 \
+{                                                    \
+     REG0, REG1, SIZE,                               \
+     /* ## will remove comma when SIZE is 0 */       \
+     ## __VA_ARGS__,                                 \
+     /* make sure len(pkt[3:]) >= SIZE */            \
+     [3 + SIZE] = 0,                                 \
+}
+
+#define MKRESP(SIZE)                         \
+{                                            \
+     [RESP_HDR_SIZE + SIZE - 1] = 0,         \
+}
+
+/* Possible size 1, 4, 20, 24. Most of the time, the size is 1. */
+static inline void refill_req(u8 *dest, const u8 *src, size_t size)
+{
+     memcpy(dest + REQ_HDR_SIZE, src, size);
+}
+
+static inline void refill_req_byte(u8 *dest, const u8 *src)
+{
+     dest[REQ_HDR_SIZE] = *src;
+}
+
+/* Possible size 1, 2, 4, 7, 20. Most of the time, the size is 1. */
+static inline void extr_resp(u8 *dest, const u8 *src, size_t size)
+{
+     memcpy(dest, src + RESP_HDR_SIZE, size);
+}
+
+static inline void extr_resp_byte(u8 *dest, const u8 *src)
+{
+     *dest = src[RESP_HDR_SIZE];
+}
+
+static inline void *extr_resp_shallow(const u8 *src)
+{
+     return src + RESP_HDR_SIZE;
+}
+
+struct gaokun_ec {
+     struct i2c_client *client;
+     struct mutex lock; /* EC transaction lock */
+     struct blocking_notifier_head notifier_list;
+     struct device *hwmon_dev;
+     struct input_dev *idev;
+     bool suspended;
+};
+
+static int gaokun_ec_request(struct gaokun_ec *ec, const u8 *req,
+                          size_t resp_len, u8 *resp)
+{
+     struct i2c_client *client = ec->client;
+     struct i2c_msg msgs[2] = {
+             {
+                     .addr = client->addr,
+                     .flags = client->flags,
+                     .len = REQ_LEN(req),
+                     .buf = req,
+             }, {
+                     .addr = client->addr,
+                     .flags = client->flags | I2C_M_RD,
+                     .len = resp_len,
+                     .buf = resp,
Quoted 9 lines

drivers/platform/arm64/huawei-gaokun-ec.c line 122

Quoted 5 lines
drivers/platform/arm64/huawei-gaokun-ec.c new file +5 -0
+             },
+     };
+
+     guard(mutex)(&ec->lock);
+     i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));

You should trap the result code of i2c_transfer() and push it up the call stack.

This EC uses SMBus Protocol, I guess. Qualcomm I2C driver doesn't support this though. The response structure define by SMBus I mentioned them above (Please also check ACPI specification 13.2.5)

+/*
+ * For rx, data sequences are arranged as
+ * {status, data_len(unreliable), data_seq}
+ */

So the first byte is status code.

Quoted 8 lines

drivers/platform/arm64/huawei-gaokun-ec.c line 125

Quoted 3 lines
drivers/platform/arm64/huawei-gaokun-ec.c new file +3 -0
+     usleep_range(2000, 2500); /* have a break, ACPI did this */
+
+     return *resp ? -EIO : 0;

If the value @ *resp is non-zero return -EIO ?

Why ?

Mentioned above.

Quoted 151 lines
Quoted 138 lines
drivers/platform/arm64/huawei-gaokun-ec.c new file +138 -0
+}
+
+/* -------------------------------------------------------------------------- */
+/* Common API */
+
+/**
+ * gaokun_ec_read - Read from EC
+ * @ec: The gaokun_ec structure
+ * @req: The sequence to request
+ * @resp_len: The size to read
+ * @resp: The buffer to store response sequence
+ *
+ * This function is used to read data after writing a magic sequence to EC.
+ * All EC operations depend on this function.
+ *
+ * Huawei uses magic sequences everywhere to complete various functions, all
+ * these sequences are passed to ECCD(a ACPI method which is quiet similar
+ * to gaokun_ec_request), there is no good abstraction to generalize these
+ * sequences, so just wrap it for now. Almost all magic sequences are kept
+ * in this file.
+ *
+ * Return: 0 on success or negative error code.
+ */
+int gaokun_ec_read(struct gaokun_ec *ec, const u8 *req,
+                size_t resp_len, u8 *resp)
+{
+     return gaokun_ec_request(ec, req, resp_len, resp);
+}
+EXPORT_SYMBOL_GPL(gaokun_ec_read);
+
+/**
+ * gaokun_ec_write - Write to EC
+ * @ec: The gaokun_ec structure
+ * @req: The sequence to request
+ *
+ * This function has no big difference from gaokun_ec_read. When caller care
+ * only write status and no actual data are returned, then use it.
+ *
+ * Return: 0 on success or negative error code.
+ */
+int gaokun_ec_write(struct gaokun_ec *ec, const u8 *req)
+{
+     u8 ec_resp[] = MKRESP(0);
+
+     return gaokun_ec_request(ec, req, sizeof(ec_resp), ec_resp);
+}
+EXPORT_SYMBOL_GPL(gaokun_ec_write);
+
+int gaokun_ec_read_byte(struct gaokun_ec *ec, const u8 *req, u8 *byte)
+{
+     int ret;
+     u8 ec_resp[] = MKRESP(sizeof(*byte));
+
+     ret = gaokun_ec_read(ec, req, sizeof(ec_resp), ec_resp);
+     extr_resp_byte(byte, ec_resp);
+
+     return ret;
+}
+EXPORT_SYMBOL_GPL(gaokun_ec_read_byte);
+
+/**
+ * gaokun_ec_register_notify - Register a notifier callback for EC events.
+ * @ec: The gaokun_ec structure
+ * @nb: Notifier block pointer to register
+ *
+ * Return: 0 on success or negative error code.
+ */
+int gaokun_ec_register_notify(struct gaokun_ec *ec, struct notifier_block *nb)
+{
+     return blocking_notifier_chain_register(&ec->notifier_list, nb);
+}
+EXPORT_SYMBOL_GPL(gaokun_ec_register_notify);
+
+/**
+ * gaokun_ec_unregister_notify - Unregister notifier callback for EC events.
+ * @ec: The gaokun_ec structure
+ * @nb: Notifier block pointer to unregister
+ *
+ * Unregister a notifier callback that was previously registered with
+ * gaokun_ec_register_notify().
+ */
+void gaokun_ec_unregister_notify(struct gaokun_ec *ec, struct notifier_block *nb)
+{
+     blocking_notifier_chain_unregister(&ec->notifier_list, nb);
+}
+EXPORT_SYMBOL_GPL(gaokun_ec_unregister_notify);
+
+/* -------------------------------------------------------------------------- */
+/* API for PSY */
+
+/**
+ * gaokun_ec_psy_multi_read - Read contiguous registers
+ * @ec: The gaokun_ec structure
+ * @reg: The start register
+ * @resp_len: The number of registers to be read
+ * @resp: The buffer to store response sequence
+ *
+ * Return: 0 on success or negative error code.
+ */
+int gaokun_ec_psy_multi_read(struct gaokun_ec *ec, u8 reg,
+                          size_t resp_len, u8 *resp)
+{
+     u8 ec_req[] = MKREQ(0x02, EC_READ, 1, 0);
+     u8 ec_resp[] = MKRESP(1);
+     int i, ret;
+
+     for (i = 0; i < resp_len; ++i, reg++) {
+             refill_req_byte(ec_req, &reg);
+             ret = gaokun_ec_read(ec, ec_req, sizeof(ec_resp), ec_resp);
+             if (ret)
+                     return ret;
+             extr_resp_byte(&resp[i], ec_resp);
+     }
+
+     return 0;
+}
+EXPORT_SYMBOL_GPL(gaokun_ec_psy_multi_read);
+
+/* Smart charge */
+
+/**
+ * gaokun_ec_psy_get_smart_charge - Get smart charge data from EC
+ * @ec: The gaokun_ec structure
+ * @resp: The buffer to store response sequence (mode, delay, start, end)
+ *
+ * Return: 0 on success or negative error code.
+ */
+int gaokun_ec_psy_get_smart_charge(struct gaokun_ec *ec,
+                                u8 resp[GAOKUN_SMART_CHARGE_DATA_SIZE])
+{
+     /* GBCM */
+     u8 ec_req[] = MKREQ(0x02, 0xE4, 0);
+     u8 ec_resp[] = MKRESP(GAOKUN_SMART_CHARGE_DATA_SIZE);
+     int ret;
+
+     ret = gaokun_ec_read(ec, ec_req, sizeof(ec_resp), ec_resp);
+     if (ret)
+             return ret;

drivers/platform/arm64/huawei-gaokun-ec.c line 273

Quoted 10 lines
drivers/platform/arm64/huawei-gaokun-ec.c new file +10 -0
+
+     extr_resp(resp, ec_resp, GAOKUN_SMART_CHARGE_DATA_SIZE);
+
+     return 0;
+}
+EXPORT_SYMBOL_GPL(gaokun_ec_psy_get_smart_charge);
+
+static inline bool are_thresholds_valid(u8 start, u8 end)
+{
+     return end != 0 && start <= end && end <= 100;

Why 100 ? Still feels like an arbitrary number.

Quoted 2 lines

Could you add a comment to explain where 100 comes from ?

You may don't get it. It is just a battery percentage, greater than 100 is invalid.

start: The battery percentage at which charging starts (0-100).
stop: The battery percentage at which charging stops (1-100).

When the laptop is connected to a power adapter, it starts charging if the battery level is below the 'start' value. It continues charging until the battery reaches the 'stop' level. If the battery is already above the 'stop' level, charging is paused.

When the power adapter is always connected, charging will begin if the battery level falls below 'start', and charging will stop once the battery reaches 'stop'.

Quoted 40 lines
Quoted 27 lines
drivers/platform/arm64/huawei-gaokun-ec.c new file +27 -0
+}
+
+/**
+ * gaokun_ec_psy_set_smart_charge - Set smart charge data
+ * @ec: The gaokun_ec structure
+ * @req: The sequence to request (mode, delay, start, end)
+ *
+ * Return: 0 on success or negative error code.
+ */
+int gaokun_ec_psy_set_smart_charge(struct gaokun_ec *ec,
+                                const u8 req[GAOKUN_SMART_CHARGE_DATA_SIZE])
+{
+     /* SBCM */
+     u8 ec_req[] = MKREQ(0x02, 0xE3, GAOKUN_SMART_CHARGE_DATA_SIZE);
+
+     if (!are_thresholds_valid(req[2], req[3]))
+             return -EINVAL;
+
+     refill_req(ec_req, req, GAOKUN_SMART_CHARGE_DATA_SIZE);
+
+     return gaokun_ec_write(ec, ec_req);
+}
+EXPORT_SYMBOL_GPL(gaokun_ec_psy_set_smart_charge);
+
+/* Smart charge enable */
+
+/**

drivers/platform/arm64/huawei-gaokun-ec.c line 310

Quoted 10 lines
drivers/platform/arm64/huawei-gaokun-ec.c new file +10 -0
+ * gaokun_ec_psy_get_smart_charge_enable - Get smart charge state
+ * @ec: The gaokun_ec structure
+ * @on: The state
+ *
+ * Return: 0 on success or negative error code.
+ */
+int gaokun_ec_psy_get_smart_charge_enable(struct gaokun_ec *ec, bool *on)
+{
+     /* GBAC */
+     u8 ec_req[] = MKREQ(0x02, 0xE6, 0);

0xE6 == SMART_CHARGE_ENABLE right ?

Quoted 7 lines

In which case instead of passing magic numbers inline, it would be nice to declare an enum or set of defines that enumerates the command set and then pass those values to MKREQ instead of the hex.

Does the first parameter indicate write ? 0x02. 0x03 seems to indicate read too ?

Sadly, I am not sure. only 0x06 is used when getting event id, so I named it. Read or write use both 0x02, 0x03.

Quoted 3 lines

If so again please name the hex values as defines/enums and then pass the descriptive names.

Function names indicate the usage of registers, these registers are used once only, never reused. If you insist, I will add defines for them.

Quoted 4 lines

Looking much more readable now though.

---
bod

Best wishes,
Pengyu

#5 lore raw

Re: [PATCH v4 2/3] platform: arm64: add Huawei Matebook E Go EC driver

czw., 16 sty 2025 o 19:17 Pengyu Luo <[email protected]> napisał(a):

Quoted 241 lines · Bryan O'Donoghue
Quoted 229 lines · Pengyu Luo
Quoted 228 lines

There are three variants of which Huawei released the first two simultaneously.

Huawei Matebook E Go LTE(sc8180x), codename seems to be gaokun2. Huawei Matebook E Go([email protected]), codename must be gaokun3. (see [1]) Huawei Matebook E Go 2023([email protected]), codename should be also gaokun3.

Adding support for the latter two variants for now, this driver should also work for the sc8180x variant according to acpi table files, but I don't have the device to test yet.

Different from other Qualcomm Snapdragon sc8280xp based machines, the Huawei Matebook E Go uses an embedded controller while others use a system called PMIC GLink. This embedded controller can be used to perform a set of various functions, including, but not limited to:

- Battery and charger monitoring;
- Charge control and smart charge;
- Fn_lock settings;
- Tablet lid status;
- Temperature sensors;
- USB Type-C notifications (ports orientation, DP alt mode HPD);
- USB Type-C PD (according to observation, up to 48w).

Add a driver for the EC which creates devices for UCSI and power supply devices.

This driver is inspired by the following drivers:

         drivers/platform/arm64/acer-aspire1-ec.c
         drivers/platform/arm64/lenovo-yoga-c630.c
         drivers/platform/x86/huawei-wmi.c

[1] https://bugzilla.kernel.org/show_bug.cgi?id=219645

---
  MAINTAINERS                                   |   7 +
  drivers/platform/arm64/Kconfig                |  21 +
  drivers/platform/arm64/Makefile               |   1 +
  drivers/platform/arm64/huawei-gaokun-ec.c     | 787 ++++++++++++++++++
  .../linux/platform_data/huawei-gaokun-ec.h    |  80 ++
  5 files changed, 896 insertions(+)

create mode 100644 drivers/platform/arm64/huawei-gaokun-ec.c create mode 100644 include/linux/platform_data/huawei-gaokun-ec.h

MAINTAINERS +7 -0
index 33b9cd11a..27ff24e7d 100644
@@ -10692,6 +10692,13 @@ S:   Maintained
  F:  Documentation/networking/device_drivers/ethernet/huawei/hinic.rst
  F:  drivers/net/ethernet/huawei/hinic/
 
+HUAWEI MATEBOOK E GO EMBEDDED CONTROLLER DRIVER
+M:   Pengyu Luo <[email protected]>
+S:   Maintained
+F:   Documentation/devicetree/bindings/platform/huawei,gaokun-ec.yaml
+F:   drivers/platform/arm64/huawei-gaokun-ec.c
+F:   include/linux/platform_data/huawei-gaokun-ec.h
+
  HUGETLB SUBSYSTEM
  M:  Muchun Song <[email protected]>
  L:  [email protected]
drivers/platform/arm64/Kconfig +21 -0
index f88395ea3..6ceee3c16 100644
@@ -33,6 +33,27 @@ config EC_ACER_ASPIRE1
        laptop where this information is not properly exposed via the
        standard ACPI devices.
 
+config EC_HUAWEI_GAOKUN
+     tristate "Huawei Matebook E Go Embedded Controller driver"
+     depends on ARCH_QCOM || COMPILE_TEST
+     depends on I2C
+     depends on INPUT
+     select AUXILIARY_BUS
+
+     help
+       Say Y here to enable the EC driver for the Huawei Matebook E Go
+       which is a sc8280xp-based 2-in-1 tablet. The driver handles battery
+       (information, charge control) and USB Type-C DP HPD events as well
+       as some misc functions like the lid sensor and temperature sensors,
+       etc.
+
+       This driver provides battery and AC status support for the mentioned
+       laptop where this information is not properly exposed via the
+       standard ACPI devices.
+
+       Say M or Y here to include this support.
+
+
  config EC_LENOVO_YOGA_C630
      tristate "Lenovo Yoga C630 Embedded Controller driver"
      depends on ARCH_QCOM || COMPILE_TEST
drivers/platform/arm64/Makefile +1 -0
index b2ae9114f..46a99eba3 100644
@@ -6,4 +6,5 @@
  #
 
  obj-$(CONFIG_EC_ACER_ASPIRE1)       += acer-aspire1-ec.o
+obj-$(CONFIG_EC_HUAWEI_GAOKUN)       += huawei-gaokun-ec.o
  obj-$(CONFIG_EC_LENOVO_YOGA_C630) += lenovo-yoga-c630.o
drivers/platform/arm64/huawei-gaokun-ec.c new file +117 -0
new file mode 100644
index 000000000..5b09b7d7c
@@ -0,0 +1,787 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * huawei-gaokun-ec - An EC driver for HUAWEI Matebook E Go
+ *
+ * Copyright (C) 2024-2025 Pengyu Luo <[email protected]>
+ */
+
+#include <linux/auxiliary_bus.h>
+#include <linux/cleanup.h>
+#include <linux/delay.h>
+#include <linux/device.h>
+#include <linux/hwmon.h>
+#include <linux/hwmon-sysfs.h>
+#include <linux/i2c.h>
+#include <linux/input.h>
+#include <linux/notifier.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/platform_data/huawei-gaokun-ec.h>
+
+#define EC_EVENT             0x06
+
+/* Also can be found in ACPI specification 12.3 */
+#define EC_READ                      0x80
+#define EC_WRITE             0x81
+#define EC_BURST             0x82
+#define EC_QUERY             0x84
+
+#define EC_FN_LOCK_ON                0x5A
+#define EC_FN_LOCK_OFF               0x55
+
+#define EC_EVENT_LID         0x81
+
+#define EC_LID_STATE         0x80
+#define EC_LID_OPEN          BIT(1)
+
+#define UCSI_REG_SIZE                7
+
+/*
+ * For tx, command sequences are arranged as
+ * {master_cmd, slave_cmd, data_len, data_seq}
+ */
+#define REQ_HDR_SIZE         3
+#define INPUT_SIZE_OFFSET    2
+#define REQ_LEN(req) (REQ_HDR_SIZE + req[INPUT_SIZE_OFFSET])
+
+/*
+ * For rx, data sequences are arranged as
+ * {status, data_len(unreliable), data_seq}
+ */
+#define RESP_HDR_SIZE                2
+
+#define MKREQ(REG0, REG1, SIZE, ...)                 \
+{                                                    \
+     REG0, REG1, SIZE,                               \
+     /* ## will remove comma when SIZE is 0 */       \
+     ## __VA_ARGS__,                                 \
+     /* make sure len(pkt[3:]) >= SIZE */            \
+     [3 + SIZE] = 0,                                 \
+}
+
+#define MKRESP(SIZE)                         \
+{                                            \
+     [RESP_HDR_SIZE + SIZE - 1] = 0,         \
+}
+
+/* Possible size 1, 4, 20, 24. Most of the time, the size is 1. */
+static inline void refill_req(u8 *dest, const u8 *src, size_t size)
+{
+     memcpy(dest + REQ_HDR_SIZE, src, size);
+}
+
+static inline void refill_req_byte(u8 *dest, const u8 *src)
+{
+     dest[REQ_HDR_SIZE] = *src;
+}
+
+/* Possible size 1, 2, 4, 7, 20. Most of the time, the size is 1. */
+static inline void extr_resp(u8 *dest, const u8 *src, size_t size)
+{
+     memcpy(dest, src + RESP_HDR_SIZE, size);
+}
+
+static inline void extr_resp_byte(u8 *dest, const u8 *src)
+{
+     *dest = src[RESP_HDR_SIZE];
+}
+
+static inline void *extr_resp_shallow(const u8 *src)
+{
+     return src + RESP_HDR_SIZE;
+}
+
+struct gaokun_ec {
+     struct i2c_client *client;
+     struct mutex lock; /* EC transaction lock */
+     struct blocking_notifier_head notifier_list;
+     struct device *hwmon_dev;
+     struct input_dev *idev;
+     bool suspended;
+};
+
+static int gaokun_ec_request(struct gaokun_ec *ec, const u8 *req,
+                          size_t resp_len, u8 *resp)
+{
+     struct i2c_client *client = ec->client;
+     struct i2c_msg msgs[2] = {
+             {
+                     .addr = client->addr,
+                     .flags = client->flags,
+                     .len = REQ_LEN(req),
+                     .buf = req,
+             }, {
+                     .addr = client->addr,
+                     .flags = client->flags | I2C_M_RD,
+                     .len = resp_len,
+                     .buf = resp,
Quoted 9 lines

drivers/platform/arm64/huawei-gaokun-ec.c line 122

Quoted 5 lines
drivers/platform/arm64/huawei-gaokun-ec.c new file +5 -0
+             },
+     };
+
+     guard(mutex)(&ec->lock);
+     i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));

You should trap the result code of i2c_transfer() and push it up the call stack.

Quoted 3 lines

This EC uses SMBus Protocol, I guess. Qualcomm I2C driver doesn't support this though. The response structure define by SMBus I mentioned them above (Please also check ACPI specification 13.2.5)

You can use i2c_smbus_write_byte_data and others with plain i2c controllers, They all are mentioned and described in Documentation/driver-api/i2c.rst I've used those APIs(with i2c controller in x1e80100) in my EC driver, and they worked fine

Quoted 257 lines

+/*
+ * For rx, data sequences are arranged as
+ * {status, data_len(unreliable), data_seq}
+ */

So the first byte is status code.

Quoted 8 lines

drivers/platform/arm64/huawei-gaokun-ec.c line 125

Quoted 3 lines
drivers/platform/arm64/huawei-gaokun-ec.c new file +3 -0
+     usleep_range(2000, 2500); /* have a break, ACPI did this */
+
+     return *resp ? -EIO : 0;

If the value @ *resp is non-zero return -EIO ?

Why ?

Mentioned above.

Quoted 151 lines
Quoted 138 lines
drivers/platform/arm64/huawei-gaokun-ec.c new file +138 -0
+}
+
+/* -------------------------------------------------------------------------- */
+/* Common API */
+
+/**
+ * gaokun_ec_read - Read from EC
+ * @ec: The gaokun_ec structure
+ * @req: The sequence to request
+ * @resp_len: The size to read
+ * @resp: The buffer to store response sequence
+ *
+ * This function is used to read data after writing a magic sequence to EC.
+ * All EC operations depend on this function.
+ *
+ * Huawei uses magic sequences everywhere to complete various functions, all
+ * these sequences are passed to ECCD(a ACPI method which is quiet similar
+ * to gaokun_ec_request), there is no good abstraction to generalize these
+ * sequences, so just wrap it for now. Almost all magic sequences are kept
+ * in this file.
+ *
+ * Return: 0 on success or negative error code.
+ */
+int gaokun_ec_read(struct gaokun_ec *ec, const u8 *req,
+                size_t resp_len, u8 *resp)
+{
+     return gaokun_ec_request(ec, req, resp_len, resp);
+}
+EXPORT_SYMBOL_GPL(gaokun_ec_read);
+
+/**
+ * gaokun_ec_write - Write to EC
+ * @ec: The gaokun_ec structure
+ * @req: The sequence to request
+ *
+ * This function has no big difference from gaokun_ec_read. When caller care
+ * only write status and no actual data are returned, then use it.
+ *
+ * Return: 0 on success or negative error code.
+ */
+int gaokun_ec_write(struct gaokun_ec *ec, const u8 *req)
+{
+     u8 ec_resp[] = MKRESP(0);
+
+     return gaokun_ec_request(ec, req, sizeof(ec_resp), ec_resp);
+}
+EXPORT_SYMBOL_GPL(gaokun_ec_write);
+
+int gaokun_ec_read_byte(struct gaokun_ec *ec, const u8 *req, u8 *byte)
+{
+     int ret;
+     u8 ec_resp[] = MKRESP(sizeof(*byte));
+
+     ret = gaokun_ec_read(ec, req, sizeof(ec_resp), ec_resp);
+     extr_resp_byte(byte, ec_resp);
+
+     return ret;
+}
+EXPORT_SYMBOL_GPL(gaokun_ec_read_byte);
+
+/**
+ * gaokun_ec_register_notify - Register a notifier callback for EC events.
+ * @ec: The gaokun_ec structure
+ * @nb: Notifier block pointer to register
+ *
+ * Return: 0 on success or negative error code.
+ */
+int gaokun_ec_register_notify(struct gaokun_ec *ec, struct notifier_block *nb)
+{
+     return blocking_notifier_chain_register(&ec->notifier_list, nb);
+}
+EXPORT_SYMBOL_GPL(gaokun_ec_register_notify);
+
+/**
+ * gaokun_ec_unregister_notify - Unregister notifier callback for EC events.
+ * @ec: The gaokun_ec structure
+ * @nb: Notifier block pointer to unregister
+ *
+ * Unregister a notifier callback that was previously registered with
+ * gaokun_ec_register_notify().
+ */
+void gaokun_ec_unregister_notify(struct gaokun_ec *ec, struct notifier_block *nb)
+{
+     blocking_notifier_chain_unregister(&ec->notifier_list, nb);
+}
+EXPORT_SYMBOL_GPL(gaokun_ec_unregister_notify);
+
+/* -------------------------------------------------------------------------- */
+/* API for PSY */
+
+/**
+ * gaokun_ec_psy_multi_read - Read contiguous registers
+ * @ec: The gaokun_ec structure
+ * @reg: The start register
+ * @resp_len: The number of registers to be read
+ * @resp: The buffer to store response sequence
+ *
+ * Return: 0 on success or negative error code.
+ */
+int gaokun_ec_psy_multi_read(struct gaokun_ec *ec, u8 reg,
+                          size_t resp_len, u8 *resp)
+{
+     u8 ec_req[] = MKREQ(0x02, EC_READ, 1, 0);
+     u8 ec_resp[] = MKRESP(1);
+     int i, ret;
+
+     for (i = 0; i < resp_len; ++i, reg++) {
+             refill_req_byte(ec_req, &reg);
+             ret = gaokun_ec_read(ec, ec_req, sizeof(ec_resp), ec_resp);
+             if (ret)
+                     return ret;
+             extr_resp_byte(&resp[i], ec_resp);
+     }
+
+     return 0;
+}
+EXPORT_SYMBOL_GPL(gaokun_ec_psy_multi_read);
+
+/* Smart charge */
+
+/**
+ * gaokun_ec_psy_get_smart_charge - Get smart charge data from EC
+ * @ec: The gaokun_ec structure
+ * @resp: The buffer to store response sequence (mode, delay, start, end)
+ *
+ * Return: 0 on success or negative error code.
+ */
+int gaokun_ec_psy_get_smart_charge(struct gaokun_ec *ec,
+                                u8 resp[GAOKUN_SMART_CHARGE_DATA_SIZE])
+{
+     /* GBCM */
+     u8 ec_req[] = MKREQ(0x02, 0xE4, 0);
+     u8 ec_resp[] = MKRESP(GAOKUN_SMART_CHARGE_DATA_SIZE);
+     int ret;
+
+     ret = gaokun_ec_read(ec, ec_req, sizeof(ec_resp), ec_resp);
+     if (ret)
+             return ret;

drivers/platform/arm64/huawei-gaokun-ec.c line 273

Quoted 10 lines
drivers/platform/arm64/huawei-gaokun-ec.c new file +10 -0
+
+     extr_resp(resp, ec_resp, GAOKUN_SMART_CHARGE_DATA_SIZE);
+
+     return 0;
+}
+EXPORT_SYMBOL_GPL(gaokun_ec_psy_get_smart_charge);
+
+static inline bool are_thresholds_valid(u8 start, u8 end)
+{
+     return end != 0 && start <= end && end <= 100;

Why 100 ? Still feels like an arbitrary number.

Quoted 2 lines

Could you add a comment to explain where 100 comes from ?

You may don't get it. It is just a battery percentage, greater than 100 is invalid.

start: The battery percentage at which charging starts (0-100).
stop: The battery percentage at which charging stops (1-100).

When the laptop is connected to a power adapter, it starts charging if the battery level is below the 'start' value. It continues charging until the battery reaches the 'stop' level. If the battery is already above the 'stop' level, charging is paused.

When the power adapter is always connected, charging will begin if the battery level falls below 'start', and charging will stop once the battery reaches 'stop'.

Quoted 40 lines
Quoted 27 lines
drivers/platform/arm64/huawei-gaokun-ec.c new file +27 -0
+}
+
+/**
+ * gaokun_ec_psy_set_smart_charge - Set smart charge data
+ * @ec: The gaokun_ec structure
+ * @req: The sequence to request (mode, delay, start, end)
+ *
+ * Return: 0 on success or negative error code.
+ */
+int gaokun_ec_psy_set_smart_charge(struct gaokun_ec *ec,
+                                const u8 req[GAOKUN_SMART_CHARGE_DATA_SIZE])
+{
+     /* SBCM */
+     u8 ec_req[] = MKREQ(0x02, 0xE3, GAOKUN_SMART_CHARGE_DATA_SIZE);
+
+     if (!are_thresholds_valid(req[2], req[3]))
+             return -EINVAL;
+
+     refill_req(ec_req, req, GAOKUN_SMART_CHARGE_DATA_SIZE);
+
+     return gaokun_ec_write(ec, ec_req);
+}
+EXPORT_SYMBOL_GPL(gaokun_ec_psy_set_smart_charge);
+
+/* Smart charge enable */
+
+/**

drivers/platform/arm64/huawei-gaokun-ec.c line 310

Quoted 10 lines
drivers/platform/arm64/huawei-gaokun-ec.c new file +10 -0
+ * gaokun_ec_psy_get_smart_charge_enable - Get smart charge state
+ * @ec: The gaokun_ec structure
+ * @on: The state
+ *
+ * Return: 0 on success or negative error code.
+ */
+int gaokun_ec_psy_get_smart_charge_enable(struct gaokun_ec *ec, bool *on)
+{
+     /* GBAC */
+     u8 ec_req[] = MKREQ(0x02, 0xE6, 0);

0xE6 == SMART_CHARGE_ENABLE right ?

Quoted 7 lines

In which case instead of passing magic numbers inline, it would be nice to declare an enum or set of defines that enumerates the command set and then pass those values to MKREQ instead of the hex.

Does the first parameter indicate write ? 0x02. 0x03 seems to indicate read too ?

Sadly, I am not sure. only 0x06 is used when getting event id, so I named it. Read or write use both 0x02, 0x03.

Quoted 3 lines

If so again please name the hex values as defines/enums and then pass the descriptive names.

Function names indicate the usage of registers, these registers are used once only, never reused. If you insist, I will add defines for them.

Quoted 4 lines

Looking much more readable now though.

---
bod

Best wishes,
Pengyu

author maintainer
#6 lore raw

Re: [PATCH v4 2/3] platform: arm64: add Huawei Matebook E Go EC driver

Quoted 242 lines · Maya Matuszczyk

czw., 16 sty 2025 o 19:17 Pengyu Luo <[email protected]> napisał(a):

Quoted 241 lines · Bryan O'Donoghue
Quoted 238 lines · Pengyu Luo
Quoted 223 lines

There are three variants of which Huawei released the first two simultaneously.

Huawei Matebook E Go LTE(sc8180x), codename seems to be gaokun2. Huawei Matebook E Go([email protected]), codename must be gaokun3. (see [1]) Huawei Matebook E Go 2023([email protected]), codename should be also gaokun3.

Adding support for the latter two variants for now, this driver should also work for the sc8180x variant according to acpi table files, but I don't have the device to test yet.

Different from other Qualcomm Snapdragon sc8280xp based machines, the Huawei Matebook E Go uses an embedded controller while others use a system called PMIC GLink. This embedded controller can be used to perform a set of various functions, including, but not limited to:

- Battery and charger monitoring;
- Charge control and smart charge;
- Fn_lock settings;
- Tablet lid status;
- Temperature sensors;
- USB Type-C notifications (ports orientation, DP alt mode HPD);
- USB Type-C PD (according to observation, up to 48w).

Add a driver for the EC which creates devices for UCSI and power supply devices.

This driver is inspired by the following drivers:

         drivers/platform/arm64/acer-aspire1-ec.c
         drivers/platform/arm64/lenovo-yoga-c630.c
         drivers/platform/x86/huawei-wmi.c

[1] https://bugzilla.kernel.org/show_bug.cgi?id=219645

---
  MAINTAINERS                                   |   7 +
  drivers/platform/arm64/Kconfig                |  21 +
  drivers/platform/arm64/Makefile               |   1 +
  drivers/platform/arm64/huawei-gaokun-ec.c     | 787 ++++++++++++++++++
  .../linux/platform_data/huawei-gaokun-ec.h    |  80 ++
  5 files changed, 896 insertions(+)

create mode 100644 drivers/platform/arm64/huawei-gaokun-ec.c create mode 100644 include/linux/platform_data/huawei-gaokun-ec.h

MAINTAINERS +7 -0
index 33b9cd11a..27ff24e7d 100644
@@ -10692,6 +10692,13 @@ S:   Maintained
  F:  Documentation/networking/device_drivers/ethernet/huawei/hinic.rst
  F:  drivers/net/ethernet/huawei/hinic/
 
+HUAWEI MATEBOOK E GO EMBEDDED CONTROLLER DRIVER
+M:   Pengyu Luo <[email protected]>
+S:   Maintained
+F:   Documentation/devicetree/bindings/platform/huawei,gaokun-ec.yaml
+F:   drivers/platform/arm64/huawei-gaokun-ec.c
+F:   include/linux/platform_data/huawei-gaokun-ec.h
+
  HUGETLB SUBSYSTEM
  M:  Muchun Song <[email protected]>
  L:  [email protected]
drivers/platform/arm64/Kconfig +21 -0
index f88395ea3..6ceee3c16 100644
@@ -33,6 +33,27 @@ config EC_ACER_ASPIRE1
        laptop where this information is not properly exposed via the
        standard ACPI devices.
 
+config EC_HUAWEI_GAOKUN
+     tristate "Huawei Matebook E Go Embedded Controller driver"
+     depends on ARCH_QCOM || COMPILE_TEST
+     depends on I2C
+     depends on INPUT
+     select AUXILIARY_BUS
+
+     help
+       Say Y here to enable the EC driver for the Huawei Matebook E Go
+       which is a sc8280xp-based 2-in-1 tablet. The driver handles battery
+       (information, charge control) and USB Type-C DP HPD events as well
+       as some misc functions like the lid sensor and temperature sensors,
+       etc.
+
+       This driver provides battery and AC status support for the mentioned
+       laptop where this information is not properly exposed via the
+       standard ACPI devices.
+
+       Say M or Y here to include this support.
+
+
  config EC_LENOVO_YOGA_C630
      tristate "Lenovo Yoga C630 Embedded Controller driver"
      depends on ARCH_QCOM || COMPILE_TEST
drivers/platform/arm64/Makefile +1 -0
index b2ae9114f..46a99eba3 100644
@@ -6,4 +6,5 @@
  #
 
  obj-$(CONFIG_EC_ACER_ASPIRE1)       += acer-aspire1-ec.o
+obj-$(CONFIG_EC_HUAWEI_GAOKUN)       += huawei-gaokun-ec.o
  obj-$(CONFIG_EC_LENOVO_YOGA_C630) += lenovo-yoga-c630.o
drivers/platform/arm64/huawei-gaokun-ec.c new file +112 -0
new file mode 100644
index 000000000..5b09b7d7c
@@ -0,0 +1,787 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * huawei-gaokun-ec - An EC driver for HUAWEI Matebook E Go
+ *
+ * Copyright (C) 2024-2025 Pengyu Luo <[email protected]>
+ */
+
+#include <linux/auxiliary_bus.h>
+#include <linux/cleanup.h>
+#include <linux/delay.h>
+#include <linux/device.h>
+#include <linux/hwmon.h>
+#include <linux/hwmon-sysfs.h>
+#include <linux/i2c.h>
+#include <linux/input.h>
+#include <linux/notifier.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/platform_data/huawei-gaokun-ec.h>
+
+#define EC_EVENT             0x06
+
+/* Also can be found in ACPI specification 12.3 */
+#define EC_READ                      0x80
+#define EC_WRITE             0x81
+#define EC_BURST             0x82
+#define EC_QUERY             0x84
+
+#define EC_FN_LOCK_ON                0x5A
+#define EC_FN_LOCK_OFF               0x55
+
+#define EC_EVENT_LID         0x81
+
+#define EC_LID_STATE         0x80
+#define EC_LID_OPEN          BIT(1)
+
+#define UCSI_REG_SIZE                7
+
+/*
+ * For tx, command sequences are arranged as
+ * {master_cmd, slave_cmd, data_len, data_seq}
+ */
+#define REQ_HDR_SIZE         3
+#define INPUT_SIZE_OFFSET    2
+#define REQ_LEN(req) (REQ_HDR_SIZE + req[INPUT_SIZE_OFFSET])
+
+/*
+ * For rx, data sequences are arranged as
+ * {status, data_len(unreliable), data_seq}
+ */
+#define RESP_HDR_SIZE                2
+
+#define MKREQ(REG0, REG1, SIZE, ...)                 \
+{                                                    \
+     REG0, REG1, SIZE,                               \
+     /* ## will remove comma when SIZE is 0 */       \
+     ## __VA_ARGS__,                                 \
+     /* make sure len(pkt[3:]) >= SIZE */            \
+     [3 + SIZE] = 0,                                 \
+}
+
+#define MKRESP(SIZE)                         \
+{                                            \
+     [RESP_HDR_SIZE + SIZE - 1] = 0,         \
+}
+
+/* Possible size 1, 4, 20, 24. Most of the time, the size is 1. */
+static inline void refill_req(u8 *dest, const u8 *src, size_t size)
+{
+     memcpy(dest + REQ_HDR_SIZE, src, size);
+}
+
+static inline void refill_req_byte(u8 *dest, const u8 *src)
+{
+     dest[REQ_HDR_SIZE] = *src;
+}
+
+/* Possible size 1, 2, 4, 7, 20. Most of the time, the size is 1. */
+static inline void extr_resp(u8 *dest, const u8 *src, size_t size)
+{
+     memcpy(dest, src + RESP_HDR_SIZE, size);
+}
+
+static inline void extr_resp_byte(u8 *dest, const u8 *src)
+{
+     *dest = src[RESP_HDR_SIZE];
+}
+
+static inline void *extr_resp_shallow(const u8 *src)
+{
+     return src + RESP_HDR_SIZE;
+}
+
+struct gaokun_ec {
+     struct i2c_client *client;
+     struct mutex lock; /* EC transaction lock */
+     struct blocking_notifier_head notifier_list;
+     struct device *hwmon_dev;
+     struct input_dev *idev;
+     bool suspended;
+};
+
+static int gaokun_ec_request(struct gaokun_ec *ec, const u8 *req,
+                          size_t resp_len, u8 *resp)
+{
+     struct i2c_client *client = ec->client;
+     struct i2c_msg msgs[2] = {
+             {
+                     .addr = client->addr,
+                     .flags = client->flags,
+                     .len = REQ_LEN(req),
+                     .buf = req,

drivers/platform/arm64/huawei-gaokun-ec.c line 122

Quoted 10 lines
drivers/platform/arm64/huawei-gaokun-ec.c new file +10 -0
+             }, {
+                     .addr = client->addr,
+                     .flags = client->flags | I2C_M_RD,
+                     .len = resp_len,
+                     .buf = resp,
+             },
+     };
+
+     guard(mutex)(&ec->lock);
+     i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));

You should trap the result code of i2c_transfer() and push it up the call stack.

Quoted 9 lines
Quoted 3 lines

This EC uses SMBus Protocol, I guess. Qualcomm I2C driver doesn't support this though. The response structure define by SMBus I mentioned them above (Please also check ACPI specification 13.2.5)

You can use i2c_smbus_write_byte_data and others with plain i2c controllers, They all are mentioned and described in Documentation/driver-api/i2c.rst I've used those APIs(with i2c controller in x1e80100) in my EC driver, and they worked fine

I talked about this in detail. See

Quoted 250 lines
Quoted 250 lines

+/*
+ * For rx, data sequences are arranged as
+ * {status, data_len(unreliable), data_seq}
+ */

So the first byte is status code.

Quoted 8 lines

drivers/platform/arm64/huawei-gaokun-ec.c line 125

Quoted 3 lines
drivers/platform/arm64/huawei-gaokun-ec.c new file +3 -0
+     usleep_range(2000, 2500); /* have a break, ACPI did this */
+
+     return *resp ? -EIO : 0;

If the value @ *resp is non-zero return -EIO ?

Why ?

Mentioned above.

Quoted 151 lines
Quoted 138 lines
drivers/platform/arm64/huawei-gaokun-ec.c new file +138 -0
+}
+
+/* -------------------------------------------------------------------------- */
+/* Common API */
+
+/**
+ * gaokun_ec_read - Read from EC
+ * @ec: The gaokun_ec structure
+ * @req: The sequence to request
+ * @resp_len: The size to read
+ * @resp: The buffer to store response sequence
+ *
+ * This function is used to read data after writing a magic sequence to EC.
+ * All EC operations depend on this function.
+ *
+ * Huawei uses magic sequences everywhere to complete various functions, all
+ * these sequences are passed to ECCD(a ACPI method which is quiet similar
+ * to gaokun_ec_request), there is no good abstraction to generalize these
+ * sequences, so just wrap it for now. Almost all magic sequences are kept
+ * in this file.
+ *
+ * Return: 0 on success or negative error code.
+ */
+int gaokun_ec_read(struct gaokun_ec *ec, const u8 *req,
+                size_t resp_len, u8 *resp)
+{
+     return gaokun_ec_request(ec, req, resp_len, resp);
+}
+EXPORT_SYMBOL_GPL(gaokun_ec_read);
+
+/**
+ * gaokun_ec_write - Write to EC
+ * @ec: The gaokun_ec structure
+ * @req: The sequence to request
+ *
+ * This function has no big difference from gaokun_ec_read. When caller care
+ * only write status and no actual data are returned, then use it.
+ *
+ * Return: 0 on success or negative error code.
+ */
+int gaokun_ec_write(struct gaokun_ec *ec, const u8 *req)
+{
+     u8 ec_resp[] = MKRESP(0);
+
+     return gaokun_ec_request(ec, req, sizeof(ec_resp), ec_resp);
+}
+EXPORT_SYMBOL_GPL(gaokun_ec_write);
+
+int gaokun_ec_read_byte(struct gaokun_ec *ec, const u8 *req, u8 *byte)
+{
+     int ret;
+     u8 ec_resp[] = MKRESP(sizeof(*byte));
+
+     ret = gaokun_ec_read(ec, req, sizeof(ec_resp), ec_resp);
+     extr_resp_byte(byte, ec_resp);
+
+     return ret;
+}
+EXPORT_SYMBOL_GPL(gaokun_ec_read_byte);
+
+/**
+ * gaokun_ec_register_notify - Register a notifier callback for EC events.
+ * @ec: The gaokun_ec structure
+ * @nb: Notifier block pointer to register
+ *
+ * Return: 0 on success or negative error code.
+ */
+int gaokun_ec_register_notify(struct gaokun_ec *ec, struct notifier_block *nb)
+{
+     return blocking_notifier_chain_register(&ec->notifier_list, nb);
+}
+EXPORT_SYMBOL_GPL(gaokun_ec_register_notify);
+
+/**
+ * gaokun_ec_unregister_notify - Unregister notifier callback for EC events.
+ * @ec: The gaokun_ec structure
+ * @nb: Notifier block pointer to unregister
+ *
+ * Unregister a notifier callback that was previously registered with
+ * gaokun_ec_register_notify().
+ */
+void gaokun_ec_unregister_notify(struct gaokun_ec *ec, struct notifier_block *nb)
+{
+     blocking_notifier_chain_unregister(&ec->notifier_list, nb);
+}
+EXPORT_SYMBOL_GPL(gaokun_ec_unregister_notify);
+
+/* -------------------------------------------------------------------------- */
+/* API for PSY */
+
+/**
+ * gaokun_ec_psy_multi_read - Read contiguous registers
+ * @ec: The gaokun_ec structure
+ * @reg: The start register
+ * @resp_len: The number of registers to be read
+ * @resp: The buffer to store response sequence
+ *
+ * Return: 0 on success or negative error code.
+ */
+int gaokun_ec_psy_multi_read(struct gaokun_ec *ec, u8 reg,
+                          size_t resp_len, u8 *resp)
+{
+     u8 ec_req[] = MKREQ(0x02, EC_READ, 1, 0);
+     u8 ec_resp[] = MKRESP(1);
+     int i, ret;
+
+     for (i = 0; i < resp_len; ++i, reg++) {
+             refill_req_byte(ec_req, &reg);
+             ret = gaokun_ec_read(ec, ec_req, sizeof(ec_resp), ec_resp);
+             if (ret)
+                     return ret;
+             extr_resp_byte(&resp[i], ec_resp);
+     }
+
+     return 0;
+}
+EXPORT_SYMBOL_GPL(gaokun_ec_psy_multi_read);
+
+/* Smart charge */
+
+/**
+ * gaokun_ec_psy_get_smart_charge - Get smart charge data from EC
+ * @ec: The gaokun_ec structure
+ * @resp: The buffer to store response sequence (mode, delay, start, end)
+ *
+ * Return: 0 on success or negative error code.
+ */
+int gaokun_ec_psy_get_smart_charge(struct gaokun_ec *ec,
+                                u8 resp[GAOKUN_SMART_CHARGE_DATA_SIZE])
+{
+     /* GBCM */
+     u8 ec_req[] = MKREQ(0x02, 0xE4, 0);
+     u8 ec_resp[] = MKRESP(GAOKUN_SMART_CHARGE_DATA_SIZE);
+     int ret;
+
+     ret = gaokun_ec_read(ec, ec_req, sizeof(ec_resp), ec_resp);
+     if (ret)
+             return ret;

drivers/platform/arm64/huawei-gaokun-ec.c line 273

Quoted 10 lines
drivers/platform/arm64/huawei-gaokun-ec.c new file +10 -0
+
+     extr_resp(resp, ec_resp, GAOKUN_SMART_CHARGE_DATA_SIZE);
+
+     return 0;
+}
+EXPORT_SYMBOL_GPL(gaokun_ec_psy_get_smart_charge);
+
+static inline bool are_thresholds_valid(u8 start, u8 end)
+{
+     return end != 0 && start <= end && end <= 100;

Why 100 ? Still feels like an arbitrary number.

Quoted 2 lines

Could you add a comment to explain where 100 comes from ?

You may don't get it. It is just a battery percentage, greater than 100 is invalid.

start: The battery percentage at which charging starts (0-100).
stop: The battery percentage at which charging stops (1-100).

When the laptop is connected to a power adapter, it starts charging if the battery level is below the 'start' value. It continues charging until the battery reaches the 'stop' level. If the battery is already above the 'stop' level, charging is paused.

When the power adapter is always connected, charging will begin if the battery level falls below 'start', and charging will stop once the battery reaches 'stop'.

Quoted 40 lines
Quoted 27 lines
drivers/platform/arm64/huawei-gaokun-ec.c new file +27 -0
+}
+
+/**
+ * gaokun_ec_psy_set_smart_charge - Set smart charge data
+ * @ec: The gaokun_ec structure
+ * @req: The sequence to request (mode, delay, start, end)
+ *
+ * Return: 0 on success or negative error code.
+ */
+int gaokun_ec_psy_set_smart_charge(struct gaokun_ec *ec,
+                                const u8 req[GAOKUN_SMART_CHARGE_DATA_SIZE])
+{
+     /* SBCM */
+     u8 ec_req[] = MKREQ(0x02, 0xE3, GAOKUN_SMART_CHARGE_DATA_SIZE);
+
+     if (!are_thresholds_valid(req[2], req[3]))
+             return -EINVAL;
+
+     refill_req(ec_req, req, GAOKUN_SMART_CHARGE_DATA_SIZE);
+
+     return gaokun_ec_write(ec, ec_req);
+}
+EXPORT_SYMBOL_GPL(gaokun_ec_psy_set_smart_charge);
+
+/* Smart charge enable */
+
+/**

drivers/platform/arm64/huawei-gaokun-ec.c line 310

Quoted 10 lines
drivers/platform/arm64/huawei-gaokun-ec.c new file +10 -0
+ * gaokun_ec_psy_get_smart_charge_enable - Get smart charge state
+ * @ec: The gaokun_ec structure
+ * @on: The state
+ *
+ * Return: 0 on success or negative error code.
+ */
+int gaokun_ec_psy_get_smart_charge_enable(struct gaokun_ec *ec, bool *on)
+{
+     /* GBAC */
+     u8 ec_req[] = MKREQ(0x02, 0xE6, 0);

0xE6 == SMART_CHARGE_ENABLE right ?

Quoted 7 lines

In which case instead of passing magic numbers inline, it would be nice to declare an enum or set of defines that enumerates the command set and then pass those values to MKREQ instead of the hex.

Does the first parameter indicate write ? 0x02. 0x03 seems to indicate read too ?

Sadly, I am not sure. only 0x06 is used when getting event id, so I named it. Read or write use both 0x02, 0x03.

Quoted 3 lines

If so again please name the hex values as defines/enums and then pass the descriptive names.

Function names indicate the usage of registers, these registers are used once only, never reused. If you insist, I will add defines for them.

Quoted 2 lines

Looking much more readable now though.

Quoted 7 lines
Quoted 7 lines
Quoted 2 lines

---
bod

Best wishes,
Pengyu

Best wishes,
Pengyu

#7 lore raw

Re: [PATCH v4 2/3] platform: arm64: add Huawei Matebook E Go EC driver

In reply to Pengyu Luo
Quoted 6 lines · Pengyu Luo
Quoted 5 lines · Bryan O'Donoghue
Quoted 3 lines · Pengyu Luo
drivers/platform/arm64/huawei-gaokun-ec.c new file +3 -0
+
+     guard(mutex)(&ec->lock);
+     i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
Quoted 7 lines
Quoted 3 lines

You should trap the result code of i2c_transfer() and push it up the call stack.

This EC uses SMBus Protocol, I guess. Qualcomm I2C driver doesn't support this though. The response structure define by SMBus I mentioned them above (Please also check ACPI specification 13.2.5)

What difference does that make ? The i2c controller itself can return error codes via i2c_transfer().

You should trap those error codes and take action if they happen.

Quoted 12 lines

+/*
+ * For rx, data sequences are arranged as
+ * {status, data_len(unreliable), data_seq}
+ */

So the first byte is status code.

Quoted 4 lines
Quoted 3 lines
drivers/platform/arm64/huawei-gaokun-ec.c new file +3 -0
+     usleep_range(2000, 2500); /* have a break, ACPI did this */
+
+     return *resp ? -EIO : 0;
Quoted 6 lines
Quoted 4 lines

If the value @ *resp is non-zero return -EIO ?

Why ?

Mentioned above.

Right, please try to take the result code of i2c_transfer() and if it indicates error, transmit that error up the call stack.

Quoted 152 lines
Quoted 152 lines
Quoted 139 lines
drivers/platform/arm64/huawei-gaokun-ec.c new file +138 -0
+}
+
+/* -------------------------------------------------------------------------- */
+/* Common API */
+
+/**
+ * gaokun_ec_read - Read from EC
+ * @ec: The gaokun_ec structure
+ * @req: The sequence to request
+ * @resp_len: The size to read
+ * @resp: The buffer to store response sequence
+ *
+ * This function is used to read data after writing a magic sequence to EC.
+ * All EC operations depend on this function.
+ *
+ * Huawei uses magic sequences everywhere to complete various functions, all
+ * these sequences are passed to ECCD(a ACPI method which is quiet similar
+ * to gaokun_ec_request), there is no good abstraction to generalize these
+ * sequences, so just wrap it for now. Almost all magic sequences are kept
+ * in this file.
+ *
+ * Return: 0 on success or negative error code.
+ */
+int gaokun_ec_read(struct gaokun_ec *ec, const u8 *req,
+                size_t resp_len, u8 *resp)
+{
+     return gaokun_ec_request(ec, req, resp_len, resp);
+}
+EXPORT_SYMBOL_GPL(gaokun_ec_read);
+
+/**
+ * gaokun_ec_write - Write to EC
+ * @ec: The gaokun_ec structure
+ * @req: The sequence to request
+ *
+ * This function has no big difference from gaokun_ec_read. When caller care
+ * only write status and no actual data are returned, then use it.
+ *
+ * Return: 0 on success or negative error code.
+ */
+int gaokun_ec_write(struct gaokun_ec *ec, const u8 *req)
+{
+     u8 ec_resp[] = MKRESP(0);
+
+     return gaokun_ec_request(ec, req, sizeof(ec_resp), ec_resp);
+}
+EXPORT_SYMBOL_GPL(gaokun_ec_write);
+
+int gaokun_ec_read_byte(struct gaokun_ec *ec, const u8 *req, u8 *byte)
+{
+     int ret;
+     u8 ec_resp[] = MKRESP(sizeof(*byte));
+
+     ret = gaokun_ec_read(ec, req, sizeof(ec_resp), ec_resp);
+     extr_resp_byte(byte, ec_resp);
+
+     return ret;
+}
+EXPORT_SYMBOL_GPL(gaokun_ec_read_byte);
+
+/**
+ * gaokun_ec_register_notify - Register a notifier callback for EC events.
+ * @ec: The gaokun_ec structure
+ * @nb: Notifier block pointer to register
+ *
+ * Return: 0 on success or negative error code.
+ */
+int gaokun_ec_register_notify(struct gaokun_ec *ec, struct notifier_block *nb)
+{
+     return blocking_notifier_chain_register(&ec->notifier_list, nb);
+}
+EXPORT_SYMBOL_GPL(gaokun_ec_register_notify);
+
+/**
+ * gaokun_ec_unregister_notify - Unregister notifier callback for EC events.
+ * @ec: The gaokun_ec structure
+ * @nb: Notifier block pointer to unregister
+ *
+ * Unregister a notifier callback that was previously registered with
+ * gaokun_ec_register_notify().
+ */
+void gaokun_ec_unregister_notify(struct gaokun_ec *ec, struct notifier_block *nb)
+{
+     blocking_notifier_chain_unregister(&ec->notifier_list, nb);
+}
+EXPORT_SYMBOL_GPL(gaokun_ec_unregister_notify);
+
+/* -------------------------------------------------------------------------- */
+/* API for PSY */
+
+/**
+ * gaokun_ec_psy_multi_read - Read contiguous registers
+ * @ec: The gaokun_ec structure
+ * @reg: The start register
+ * @resp_len: The number of registers to be read
+ * @resp: The buffer to store response sequence
+ *
+ * Return: 0 on success or negative error code.
+ */
+int gaokun_ec_psy_multi_read(struct gaokun_ec *ec, u8 reg,
+                          size_t resp_len, u8 *resp)
+{
+     u8 ec_req[] = MKREQ(0x02, EC_READ, 1, 0);
+     u8 ec_resp[] = MKRESP(1);
+     int i, ret;
+
+     for (i = 0; i < resp_len; ++i, reg++) {
+             refill_req_byte(ec_req, &reg);
+             ret = gaokun_ec_read(ec, ec_req, sizeof(ec_resp), ec_resp);
+             if (ret)
+                     return ret;
+             extr_resp_byte(&resp[i], ec_resp);
+     }
+
+     return 0;
+}
+EXPORT_SYMBOL_GPL(gaokun_ec_psy_multi_read);
+
+/* Smart charge */
+
+/**
+ * gaokun_ec_psy_get_smart_charge - Get smart charge data from EC
+ * @ec: The gaokun_ec structure
+ * @resp: The buffer to store response sequence (mode, delay, start, end)
+ *
+ * Return: 0 on success or negative error code.
+ */
+int gaokun_ec_psy_get_smart_charge(struct gaokun_ec *ec,
+                                u8 resp[GAOKUN_SMART_CHARGE_DATA_SIZE])
+{
+     /* GBCM */
+     u8 ec_req[] = MKREQ(0x02, 0xE4, 0);
+     u8 ec_resp[] = MKRESP(GAOKUN_SMART_CHARGE_DATA_SIZE);
+     int ret;
+
+     ret = gaokun_ec_read(ec, ec_req, sizeof(ec_resp), ec_resp);
+     if (ret)
+             return ret;

drivers/platform/arm64/huawei-gaokun-ec.c line 273

Quoted 10 lines
drivers/platform/arm64/huawei-gaokun-ec.c new file +10 -0
+
+     extr_resp(resp, ec_resp, GAOKUN_SMART_CHARGE_DATA_SIZE);
+
+     return 0;
+}
+EXPORT_SYMBOL_GPL(gaokun_ec_psy_get_smart_charge);
+
+static inline bool are_thresholds_valid(u8 start, u8 end)
+{
+     return end != 0 && start <= end && end <= 100;

Why 100 ? Still feels like an arbitrary number.

Quoted 5 lines
Quoted 2 lines

Could you add a comment to explain where 100 comes from ?

You may don't get it. It is just a battery percentage, greater than 100 is invalid.

100 meaning maximum capacity, good stuff.

Please use a define with a descriptive name. That way the meaning is obvious.

In fact if the name of the function related to battery capacity then the meaning of the numbers would be more obvious.

static inline bool validate_battery_threshold_range(u8 start, u8 end) {

	return end != 0 && start <= end && end <= 100;
}
Quoted 3 lines

start: The battery percentage at which charging starts (0-100).
stop: The battery percentage at which charging stops (1-100).

Or just add this comment directly above the function.

---
bod

#9 lore raw

Re: [PATCH v4 2/3] platform: arm64: add Huawei Matebook E Go EC driver

In reply to Pengyu Luo
kernel test robot noticed the following build warnings:

Hi Pengyu,

kernel test robot noticed the following build warnings:

[auto build test WARNING on b323d8e7bc03d27dec646bfdccb7d1a92411f189]

url:    https://github.com/intel-lab-lkp/linux/commits/Pengyu-Luo/dt-bindings-platform-Add-Huawei-Matebook-E-Go-EC/20250116-192206
base:   b323d8e7bc03d27dec646bfdccb7d1a92411f189
patch link:    https://lore.kernel.org/r/20250116111559.83641-3-mitltlatltl%40gmail.com
patch subject: [PATCH v4 2/3] platform: arm64: add Huawei Matebook E Go EC driver
config: sh-allmodconfig (https://download.01.org/0day-ci/archive/20250117/[email protected]/config)
compiler: sh4-linux-gcc (GCC) 14.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250117/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

drivers/platform/arm64/huawei-gaokun-ec.c: In function 'extr_resp_shallow':

>> drivers/platform/arm64/huawei-gaokun-ec.c:91:20: warning: return discards 'const' qualifier from pointer target type [-Wdiscarded-qualifiers]
      91 |         return src + RESP_HDR_SIZE;
         |                    ^
   drivers/platform/arm64/huawei-gaokun-ec.c: In function 'gaokun_ec_request':
>> drivers/platform/arm64/huawei-gaokun-ec.c:112:32: warning: initialization discards 'const' qualifier from pointer target type [-Wdiscarded-qualifiers]

112 | .buf = req,
| ^~~

vim +/const +91 drivers/platform/arm64/huawei-gaokun-ec.c

    88	
    89	static inline void *extr_resp_shallow(const u8 *src)
    90	{
  > 91		return src + RESP_HDR_SIZE;
    92	}
    93	
    94	struct gaokun_ec {
    95		struct i2c_client *client;
    96		struct mutex lock; /* EC transaction lock */
    97		struct blocking_notifier_head notifier_list;
    98		struct device *hwmon_dev;
    99		struct input_dev *idev;
   100		bool suspended;
   101	};
   102	
   103	static int gaokun_ec_request(struct gaokun_ec *ec, const u8 *req,
   104				     size_t resp_len, u8 *resp)
   105	{
   106		struct i2c_client *client = ec->client;
   107		struct i2c_msg msgs[2] = {
   108			{
   109				.addr = client->addr,
   110				.flags = client->flags,
   111				.len = REQ_LEN(req),
 > 112				.buf = req,

113 }, {
114 .addr = client->addr,
115 .flags = client->flags | I2C_M_RD,
116 .len = resp_len,
117 .buf = resp,
118 },
119 };
120
121 guard(mutex)(&ec->lock);
122 i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
123 usleep_range(2000, 2500); /* have a break, ACPI did this */
124
125 return *resp ? -EIO : 0;
126 }
127

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#10 lore raw

Re: [PATCH v4 2/3] platform: arm64: add Huawei Matebook E Go EC driver

In reply to Pengyu Luo
kernel test robot noticed the following build errors:

Hi Pengyu,

kernel test robot noticed the following build errors:

[auto build test ERROR on b323d8e7bc03d27dec646bfdccb7d1a92411f189]

url:    https://github.com/intel-lab-lkp/linux/commits/Pengyu-Luo/dt-bindings-platform-Add-Huawei-Matebook-E-Go-EC/20250116-192206
base:   b323d8e7bc03d27dec646bfdccb7d1a92411f189
patch link:    https://lore.kernel.org/r/20250116111559.83641-3-mitltlatltl%40gmail.com
patch subject: [PATCH v4 2/3] platform: arm64: add Huawei Matebook E Go EC driver
config: s390-allmodconfig (https://download.01.org/0day-ci/archive/20250117/[email protected]/config)
compiler: clang version 19.1.3 (https://github.com/llvm/llvm-project ab51eccf88f5321e7c60591c5546b254b6afab99)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250117/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

In file included from drivers/platform/arm64/huawei-gaokun-ec.c:8:
In file included from include/linux/auxiliary_bus.h:11:
In file included from include/linux/device.h:32:
In file included from include/linux/device/driver.h:21:
In file included from include/linux/module.h:19:
In file included from include/linux/elf.h:6:
In file included from arch/s390/include/asm/elf.h:181:
In file included from arch/s390/include/asm/mmu_context.h:11:
In file included from arch/s390/include/asm/pgalloc.h:18:
In file included from include/linux/mm.h:2224:
include/linux/vmstat.h:504:43: warning: arithmetic between different enumeration types ('enum zone_stat_item' and 'enum numa_stat_item') [-Wenum-enum-conversion]

     504 |         return vmstat_text[NR_VM_ZONE_STAT_ITEMS +
         |                            ~~~~~~~~~~~~~~~~~~~~~ ^
     505 |                            item];
         |                            ~~~~
   include/linux/vmstat.h:511:43: warning: arithmetic between different enumeration types ('enum zone_stat_item' and 'enum numa_stat_item') [-Wenum-enum-conversion]
     511 |         return vmstat_text[NR_VM_ZONE_STAT_ITEMS +
         |                            ~~~~~~~~~~~~~~~~~~~~~ ^
     512 |                            NR_VM_NUMA_EVENT_ITEMS +
         |                            ~~~~~~~~~~~~~~~~~~~~~~
   include/linux/vmstat.h:524:43: warning: arithmetic between different enumeration types ('enum zone_stat_item' and 'enum numa_stat_item') [-Wenum-enum-conversion]
     524 |         return vmstat_text[NR_VM_ZONE_STAT_ITEMS +
         |                            ~~~~~~~~~~~~~~~~~~~~~ ^
     525 |                            NR_VM_NUMA_EVENT_ITEMS +
         |                            ~~~~~~~~~~~~~~~~~~~~~~
>> drivers/platform/arm64/huawei-gaokun-ec.c:91:9: error: returning 'const u8 *' (aka 'const unsigned char *') from a function with result type 'void *' discards qualifiers [-Werror,-Wincompatible-pointer-types-discards-qualifiers]
      91 |         return src + RESP_HDR_SIZE;
         |                ^~~~~~~~~~~~~~~~~~~
>> drivers/platform/arm64/huawei-gaokun-ec.c:112:11: error: initializing '__u8 *' (aka 'unsigned char *') with an expression of type 'const u8 *' (aka 'const unsigned char *') discards qualifiers [-Werror,-Wincompatible-pointer-types-discards-qualifiers]

112 | .buf = req,
| ^~~
3 warnings and 2 errors generated.

vim +91 drivers/platform/arm64/huawei-gaokun-ec.c

    88	
    89	static inline void *extr_resp_shallow(const u8 *src)
    90	{
  > 91		return src + RESP_HDR_SIZE;
    92	}
    93	
    94	struct gaokun_ec {
    95		struct i2c_client *client;
    96		struct mutex lock; /* EC transaction lock */
    97		struct blocking_notifier_head notifier_list;
    98		struct device *hwmon_dev;
    99		struct input_dev *idev;
   100		bool suspended;
   101	};
   102	
   103	static int gaokun_ec_request(struct gaokun_ec *ec, const u8 *req,
   104				     size_t resp_len, u8 *resp)
   105	{
   106		struct i2c_client *client = ec->client;
   107		struct i2c_msg msgs[2] = {
   108			{
   109				.addr = client->addr,
   110				.flags = client->flags,
   111				.len = REQ_LEN(req),
 > 112				.buf = req,

113 }, {
114 .addr = client->addr,
115 .flags = client->flags | I2C_M_RD,
116 .len = resp_len,
117 .buf = resp,
118 },
119 };
120
121 guard(mutex)(&ec->lock);
122 i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
123 usleep_range(2000, 2500); /* have a break, ACPI did this */
124
125 return *resp ? -EIO : 0;
126 }
127

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
maintainer
#8 lore raw

Re: [PATCH v4 2/3] platform: arm64: add Huawei Matebook E Go EC driver

In reply to Bryan O'Donoghue
Quoted 220 lines · Bryan O'Donoghue
Quoted 7 lines · Pengyu Luo
Quoted 5 lines · Bryan O'Donoghue
Quoted 3 lines · Pengyu Luo
drivers/platform/arm64/huawei-gaokun-ec.c new file +3 -0
+
+     guard(mutex)(&ec->lock);
+     i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
Quoted 7 lines
Quoted 3 lines

You should trap the result code of i2c_transfer() and push it up the call stack.

This EC uses SMBus Protocol, I guess. Qualcomm I2C driver doesn't support this though. The response structure define by SMBus I mentioned them above (Please also check ACPI specification 13.2.5)

What difference does that make ? The i2c controller itself can return error codes via i2c_transfer().

You should trap those error codes and take action if they happen.

Quoted 12 lines

+/*
+ * For rx, data sequences are arranged as
+ * {status, data_len(unreliable), data_seq}
+ */

So the first byte is status code.

Quoted 4 lines
Quoted 3 lines
drivers/platform/arm64/huawei-gaokun-ec.c new file +3 -0
+     usleep_range(2000, 2500); /* have a break, ACPI did this */
+
+     return *resp ? -EIO : 0;
Quoted 6 lines
Quoted 4 lines

If the value @ *resp is non-zero return -EIO ?

Why ?

Mentioned above.

Right, please try to take the result code of i2c_transfer() and if it indicates error, transmit that error up the call stack.

Quoted 168 lines
Quoted 168 lines
Quoted 155 lines
drivers/platform/arm64/huawei-gaokun-ec.c new file +3 -1
+}
+
+/*
--------------------------------------------------------------------------

*/

drivers/platform/arm64/huawei-gaokun-ec.c new file +10 -0
+/* Common API */
+
+/**
+ * gaokun_ec_read - Read from EC
+ * @ec: The gaokun_ec structure
+ * @req: The sequence to request
+ * @resp_len: The size to read
+ * @resp: The buffer to store response sequence
+ *
+ * This function is used to read data after writing a magic sequence to

EC.
+ * All EC operations depend on this function.
+ *
+ * Huawei uses magic sequences everywhere to complete various functions, all
+ * these sequences are passed to ECCD(a ACPI method which is quiet similar
+ * to gaokun_ec_request), there is no good abstraction to generalize these
+ * sequences, so just wrap it for now. Almost all magic sequences are kept
+ * in this file.
+ *
+ * Return: 0 on success or negative error code.
+ */

drivers/platform/arm64/huawei-gaokun-ec.c new file +13 -0
+int gaokun_ec_read(struct gaokun_ec *ec, const u8 *req,
+                size_t resp_len, u8 *resp)
+{
+     return gaokun_ec_request(ec, req, resp_len, resp);
+}
+EXPORT_SYMBOL_GPL(gaokun_ec_read);
+
+/**
+ * gaokun_ec_write - Write to EC
+ * @ec: The gaokun_ec structure
+ * @req: The sequence to request
+ *
+ * This function has no big difference from gaokun_ec_read. When caller

care
+ * only write status and no actual data are returned, then use it.
+ *
+ * Return: 0 on success or negative error code.
+ */

drivers/platform/arm64/huawei-gaokun-ec.c new file +22 -0
+int gaokun_ec_write(struct gaokun_ec *ec, const u8 *req)
+{
+     u8 ec_resp[] = MKRESP(0);
+
+     return gaokun_ec_request(ec, req, sizeof(ec_resp), ec_resp);
+}
+EXPORT_SYMBOL_GPL(gaokun_ec_write);
+
+int gaokun_ec_read_byte(struct gaokun_ec *ec, const u8 *req, u8 *byte)
+{
+     int ret;
+     u8 ec_resp[] = MKRESP(sizeof(*byte));
+
+     ret = gaokun_ec_read(ec, req, sizeof(ec_resp), ec_resp);
+     extr_resp_byte(byte, ec_resp);
+
+     return ret;
+}
+EXPORT_SYMBOL_GPL(gaokun_ec_read_byte);
+
+/**
+ * gaokun_ec_register_notify - Register a notifier callback for EC

events.
+ * @ec: The gaokun_ec structure
+ * @nb: Notifier block pointer to register
+ *
+ * Return: 0 on success or negative error code.
+ */
+int gaokun_ec_register_notify(struct gaokun_ec *ec, struct notifier_block *nb)

drivers/platform/arm64/huawei-gaokun-ec.c new file +7 -0
+{
+     return blocking_notifier_chain_register(&ec->notifier_list, nb);
+}
+EXPORT_SYMBOL_GPL(gaokun_ec_register_notify);
+
+/**
+ * gaokun_ec_unregister_notify - Unregister notifier callback for EC

events.
+ * @ec: The gaokun_ec structure
+ * @nb: Notifier block pointer to unregister
+ *
+ * Unregister a notifier callback that was previously registered with
+ * gaokun_ec_register_notify().
+ */
+void gaokun_ec_unregister_notify(struct gaokun_ec *ec, struct notifier_block *nb)

drivers/platform/arm64/huawei-gaokun-ec.c new file +6 -1
+{
+     blocking_notifier_chain_unregister(&ec->notifier_list, nb);
+}
+EXPORT_SYMBOL_GPL(gaokun_ec_unregister_notify);
+
+/*
--------------------------------------------------------------------------

*/

drivers/platform/arm64/huawei-gaokun-ec.c new file +21 -0
+/* API for PSY */
+
+/**
+ * gaokun_ec_psy_multi_read - Read contiguous registers
+ * @ec: The gaokun_ec structure
+ * @reg: The start register
+ * @resp_len: The number of registers to be read
+ * @resp: The buffer to store response sequence
+ *
+ * Return: 0 on success or negative error code.
+ */
+int gaokun_ec_psy_multi_read(struct gaokun_ec *ec, u8 reg,
+                          size_t resp_len, u8 *resp)
+{
+     u8 ec_req[] = MKREQ(0x02, EC_READ, 1, 0);
+     u8 ec_resp[] = MKRESP(1);
+     int i, ret;
+
+     for (i = 0; i < resp_len; ++i, reg++) {
+             refill_req_byte(ec_req, &reg);
+             ret = gaokun_ec_read(ec, ec_req, sizeof(ec_resp),

ec_resp);

drivers/platform/arm64/huawei-gaokun-ec.c new file +15 -0
+             if (ret)
+                     return ret;
+             extr_resp_byte(&resp[i], ec_resp);
+     }
+
+     return 0;
+}
+EXPORT_SYMBOL_GPL(gaokun_ec_psy_multi_read);
+
+/* Smart charge */
+
+/**
+ * gaokun_ec_psy_get_smart_charge - Get smart charge data from EC
+ * @ec: The gaokun_ec structure
+ * @resp: The buffer to store response sequence (mode, delay, start,

end)
+ *
+ * Return: 0 on success or negative error code.
+ */

drivers/platform/arm64/huawei-gaokun-ec.c new file +11 -0
+int gaokun_ec_psy_get_smart_charge(struct gaokun_ec *ec,
+                                u8 resp[GAOKUN_SMART_CHARGE_DATA_SIZE])
+{
+     /* GBCM */
+     u8 ec_req[] = MKREQ(0x02, 0xE4, 0);
+     u8 ec_resp[] = MKRESP(GAOKUN_SMART_CHARGE_DATA_SIZE);
+     int ret;
+
+     ret = gaokun_ec_read(ec, ec_req, sizeof(ec_resp), ec_resp);
+     if (ret)
+             return ret;

drivers/platform/arm64/huawei-gaokun-ec.c line 273

Quoted 10 lines
drivers/platform/arm64/huawei-gaokun-ec.c new file +10 -0
+
+     extr_resp(resp, ec_resp, GAOKUN_SMART_CHARGE_DATA_SIZE);
+
+     return 0;
+}
+EXPORT_SYMBOL_GPL(gaokun_ec_psy_get_smart_charge);
+
+static inline bool are_thresholds_valid(u8 start, u8 end)
+{
+     return end != 0 && start <= end && end <= 100;

Why 100 ? Still feels like an arbitrary number.

Quoted 5 lines
Quoted 2 lines

Could you add a comment to explain where 100 comes from ?

You may don't get it. It is just a battery percentage, greater than 100 is invalid.

100 meaning maximum capacity, good stuff.

Quoted 8 lines

Please use a define with a descriptive name. That way the meaning is obvious.

In fact if the name of the function related to battery capacity then the meaning of the numbers would be more obvious.

static inline bool validate_battery_threshold_range(u8 start, u8 end) {

	return end != 0 && start <= end && end <= 100;
}

I suggest going with this latter option. 100% tends to be in its literal form elsewhere too. I suppose we don't even have a generic define for "100%", at least I don't recall coming across one nor found any with a quick git grep underneath include/.

But I agree the naming of this function could be improved like you suggest.

Signature · 1 line
 i.
Quoted 9 lines
Quoted 3 lines

start: The battery percentage at which charging starts (0-100).
stop: The battery percentage at which charging stops (1-100).

Or just add this comment directly above the function.

---
bod