* [PATCH v2 0/5] platform: arm64: Huawei Matebook E Go embedded controller
@ 2025-01-05 17:41 Pengyu Luo
2025-01-05 17:41 ` [PATCH v2 1/5] dt-bindings: platform: Add Huawei Matebook E Go EC Pengyu Luo
` (5 more replies)
0 siblings, 6 replies; 15+ messages in thread
From: Pengyu Luo @ 2025-01-05 17:41 UTC (permalink / raw)
To: Rob Herring, Krzysztof Kozlowski, Conor Dooley, Bjorn Andersson,
Konrad Dybcio, Hans de Goede, Ilpo Järvinen,
Bryan O'Donoghue, Sebastian Reichel, Heikki Krogerus,
Greg Kroah-Hartman, Jean Delvare, Guenter Roeck
Cc: devicetree, linux-kernel, linux-arm-msm, platform-driver-x86,
linux-pm, linux-usb, linux-hwmon, Dmitry Baryshkov, Pengyu Luo
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 the source file.
Depends: https://lore.kernel.org/linux-arm-msm/20241220160530.444864-1-mitltlatltl@gmail.com
[1] https://lore.kernel.org/all/20240614-yoga-ec-driver-v7-0-9f0b9b40ae76@linaro.org/
[2] https://lore.kernel.org/all/20240315-aspire1-ec-v5-0-f93381deff39@trvn.ru/
Signed-off-by: Pengyu Luo <mitltlatltl@gmail.com>
---
Changes in v2:
- Link to v1: https://lore.kernel.org/linux-arm-msm/20241227171353.404432-1-mitltlatltl@gmail.com
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 (5):
dt-bindings: platform: Add Huawei Matebook E Go EC
platform: arm64: add Huawei Matebook E Go EC driver
usb: typec: ucsi: add Huawei Matebook E Go ucsi driver
power: supply: add Huawei Matebook E Go psy driver
arm64: dts: qcom: gaokun3: Add Embedded Controller node
.../ABI/testing/sysfs-class-power-gaokun | 47 ++
.../bindings/platform/huawei,gaokun-ec.yaml | 127 +++
MAINTAINERS | 9 +
.../boot/dts/qcom/sc8280xp-huawei-gaokun3.dts | 139 ++++
drivers/platform/arm64/Kconfig | 21 +
drivers/platform/arm64/Makefile | 1 +
drivers/platform/arm64/huawei-gaokun-ec.c | 767 ++++++++++++++++++
drivers/power/supply/Kconfig | 10 +
drivers/power/supply/Makefile | 1 +
drivers/power/supply/huawei-gaokun-battery.c | 548 +++++++++++++
drivers/usb/typec/ucsi/Kconfig | 10 +
drivers/usb/typec/ucsi/Makefile | 1 +
drivers/usb/typec/ucsi/ucsi_huawei_gaokun.c | 524 ++++++++++++
.../linux/platform_data/huawei-gaokun-ec.h | 79 ++
14 files changed, 2284 insertions(+)
create mode 100644 Documentation/ABI/testing/sysfs-class-power-gaokun
create mode 100644 Documentation/devicetree/bindings/platform/huawei,gaokun-ec.yaml
create mode 100644 drivers/platform/arm64/huawei-gaokun-ec.c
create mode 100644 drivers/power/supply/huawei-gaokun-battery.c
create mode 100644 drivers/usb/typec/ucsi/ucsi_huawei_gaokun.c
create mode 100644 include/linux/platform_data/huawei-gaokun-ec.h
--
2.47.1
^ permalink raw reply [flat|nested] 15+ messages in thread
* [PATCH v2 1/5] dt-bindings: platform: Add Huawei Matebook E Go EC
2025-01-05 17:41 [PATCH v2 0/5] platform: arm64: Huawei Matebook E Go embedded controller Pengyu Luo
@ 2025-01-05 17:41 ` Pengyu Luo
2025-01-06 7:11 ` Krzysztof Kozlowski
2025-01-05 17:41 ` [PATCH v2 2/5] platform: arm64: add Huawei Matebook E Go EC driver Pengyu Luo
` (4 subsequent siblings)
5 siblings, 1 reply; 15+ messages in thread
From: Pengyu Luo @ 2025-01-05 17:41 UTC (permalink / raw)
To: Rob Herring, Krzysztof Kozlowski, Conor Dooley, Bjorn Andersson,
Konrad Dybcio, Hans de Goede, Ilpo Järvinen,
Bryan O'Donoghue, Sebastian Reichel, Heikki Krogerus,
Greg Kroah-Hartman, Jean Delvare, Guenter Roeck
Cc: devicetree, linux-kernel, linux-arm-msm, platform-driver-x86,
linux-pm, linux-usb, linux-hwmon, Dmitry Baryshkov, Pengyu Luo
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 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
Signed-off-by: Pengyu Luo <mitltlatltl@gmail.com>
---
.../bindings/platform/huawei,gaokun-ec.yaml | 127 ++++++++++++++++++
1 file changed, 127 insertions(+)
create mode 100644 Documentation/devicetree/bindings/platform/huawei,gaokun-ec.yaml
diff --git a/Documentation/devicetree/bindings/platform/huawei,gaokun-ec.yaml b/Documentation/devicetree/bindings/platform/huawei,gaokun-ec.yaml
new file mode 100644
index 000000000..149c0cbe4
--- /dev/null
+++ b/Documentation/devicetree/bindings/platform/huawei,gaokun-ec.yaml
@@ -0,0 +1,127 @@
+# 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 <mitltlatltl@gmail.com>
+
+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:
+ items:
+ - enum:
+ - huawei,gaokun2
+ - huawei,gaokun3
+ - const: huawei,gaokun-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", "huawei,gaokun-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>;
+ };
+ };
+ };
+ };
+ };
+ };
--
2.47.1
^ permalink raw reply related [flat|nested] 15+ messages in thread
* [PATCH v2 2/5] platform: arm64: add Huawei Matebook E Go EC driver
2025-01-05 17:41 [PATCH v2 0/5] platform: arm64: Huawei Matebook E Go embedded controller Pengyu Luo
2025-01-05 17:41 ` [PATCH v2 1/5] dt-bindings: platform: Add Huawei Matebook E Go EC Pengyu Luo
@ 2025-01-05 17:41 ` Pengyu Luo
2025-01-07 12:25 ` Heikki Krogerus
2025-01-05 17:41 ` [PATCH v2 3/5] usb: typec: ucsi: add Huawei Matebook E Go ucsi driver Pengyu Luo
` (3 subsequent siblings)
5 siblings, 1 reply; 15+ messages in thread
From: Pengyu Luo @ 2025-01-05 17:41 UTC (permalink / raw)
To: Rob Herring, Krzysztof Kozlowski, Conor Dooley, Bjorn Andersson,
Konrad Dybcio, Hans de Goede, Ilpo Järvinen,
Bryan O'Donoghue, Sebastian Reichel, Heikki Krogerus,
Greg Kroah-Hartman, Jean Delvare, Guenter Roeck
Cc: devicetree, linux-kernel, linux-arm-msm, platform-driver-x86,
linux-pm, linux-usb, linux-hwmon, Dmitry Baryshkov, 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(sc8280xp@3.0GHz), codename must be gaokun3. (see [1])
Huawei Matebook E Go 2023(sc8280xp@2.69GHz), 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.
[1] https://bugzilla.kernel.org/show_bug.cgi?id=219645
Signed-off-by: Pengyu Luo <mitltlatltl@gmail.com>
---
MAINTAINERS | 9 +
drivers/platform/arm64/Kconfig | 21 +
drivers/platform/arm64/Makefile | 1 +
drivers/platform/arm64/huawei-gaokun-ec.c | 767 ++++++++++++++++++
.../linux/platform_data/huawei-gaokun-ec.h | 79 ++
5 files changed, 877 insertions(+)
create mode 100644 drivers/platform/arm64/huawei-gaokun-ec.c
create mode 100644 include/linux/platform_data/huawei-gaokun-ec.h
diff --git a/MAINTAINERS b/MAINTAINERS
index 85c8a68d1..04263ba32 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -10595,6 +10595,15 @@ 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 <mitltlatltl@gmail.com>
+S: Maintained
+F: Documentation/devicetree/bindings/platform/huawei,gaokun-ec.yaml
+F: drivers/platform/arm64/huawei-gaokun-ec.c
+F: drivers/power/supply/huawei-gaokun-battery.c
+F: drivers/usb/typec/ucsi/ucsi_huawei_gaokun.c
+F: include/linux/platform_data/huawei-gaokun-ec.h
+
HUGETLB SUBSYSTEM
M: Muchun Song <muchun.song@linux.dev>
L: linux-mm@kvack.org
diff --git a/drivers/platform/arm64/Kconfig b/drivers/platform/arm64/Kconfig
index f88395ea3..67dc7aa95 100644
--- a/drivers/platform/arm64/Kconfig
+++ b/drivers/platform/arm64/Kconfig
@@ -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 DRM
+ depends on POWER_SUPPLY
+ depends on INPUT
+ 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
diff --git a/drivers/platform/arm64/Makefile b/drivers/platform/arm64/Makefile
index b2ae9114f..46a99eba3 100644
--- a/drivers/platform/arm64/Makefile
+++ b/drivers/platform/arm64/Makefile
@@ -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
diff --git a/drivers/platform/arm64/huawei-gaokun-ec.c b/drivers/platform/arm64/huawei-gaokun-ec.c
new file mode 100644
index 000000000..743885a3b
--- /dev/null
+++ b/drivers/platform/arm64/huawei-gaokun-ec.c
@@ -0,0 +1,767 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * huawei-gaokun-ec - An EC driver for HUAWEI Matebook E Go
+ *
+ * reference: drivers/platform/arm64/acer-aspire1-ec.c
+ * drivers/platform/arm64/lenovo-yoga-c630.c
+ * drivers/platform/x86/huawei-wmi.c
+ *
+ * Copyright (C) 2024 Pengyu Luo <mitltlatltl@gmail.com>
+ */
+
+#include <linux/auxiliary_bus.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>
+#include <linux/version.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, \
+}
+
+static inline void refill_req(u8 *dest, const u8 *src, size_t size)
+{
+ memcpy(dest + REQ_HDR_SIZE, src, size);
+}
+
+static inline void extr_resp(u8 *dest, const u8 *src, size_t size)
+{
+ memcpy(dest, src + RESP_HDR_SIZE, 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,
+ },
+ };
+
+ mutex_lock(&ec->lock);
+
+ i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
+ usleep_range(2000, 2500); /* have a break, acpi did this */
+
+ mutex_unlock(&ec->lock);
+
+ return *resp;
+}
+
+/* -------------------------------------------------------------------------- */
+/* Common API */
+
+/**
+ * gaokun_ec_read - read from EC
+ * @ec: The gaokun_ec
+ * @req: The sequence to request
+ * @resp_len: The size to read
+ * @resp: Where the data are read to
+ *
+ * Return: 0 on success or negative error code.
+ *
+ * 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.
+ */
+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
+ * @req: The sequence to request
+ *
+ * Return: 0 on success or negative error code.
+ *
+ * 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.
+ */
+int gaokun_ec_write(struct gaokun_ec *ec, u8 *req)
+{
+ u8 resp[] = MKRESP(0);
+
+ return gaokun_ec_request(ec, req, sizeof(resp), resp);
+}
+EXPORT_SYMBOL_GPL(gaokun_ec_write);
+
+int gaokun_ec_read_byte(struct gaokun_ec *ec, u8 *req, u8 *byte)
+{
+ int ret;
+ u8 resp[] = MKRESP(sizeof(*byte));
+
+ ret = gaokun_ec_read(ec, req, sizeof(resp), resp);
+ extr_resp(byte, resp, sizeof(*byte));
+
+ return ret;
+}
+EXPORT_SYMBOL_GPL(gaokun_ec_read_byte);
+
+/**
+ * gaokun_ec_register_notify - Register a notifier callback for EC events.
+ * @ec: The gaokun_ec
+ * @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
+ * @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 */
+
+int gaokun_ec_psy_multi_read(struct gaokun_ec *ec, u8 reg,
+ size_t resp_len, u8 *resp)
+{
+ int i, ret;
+ u8 _resp[] = MKRESP(1);
+ u8 req[] = MKREQ(0x02, EC_READ, 1, 0);
+
+ for (i = 0; i < resp_len; ++i, reg++) {
+ refill_req(req, ®, 1);
+ ret = gaokun_ec_read(ec, req, sizeof(_resp), _resp);
+ if (ret)
+ return ret;
+ extr_resp(&resp[i], _resp, 1);
+ }
+
+ return 0;
+}
+EXPORT_SYMBOL_GPL(gaokun_ec_psy_multi_read);
+
+/* Smart charge */
+#define are_thresholds_valid(start, end) ((end != 0) && (start <= end) && (end <= 100))
+int gaokun_ec_psy_get_smart_charge(struct gaokun_ec *ec,
+ u8 data[GAOKUN_SMART_CHARGE_DATA_SIZE])
+{
+ /* GBCM */
+ u8 req[] = MKREQ(0x02, 0xE4, 0);
+ u8 resp[] = MKRESP(GAOKUN_SMART_CHARGE_DATA_SIZE);
+ int ret;
+
+ ret = gaokun_ec_read(ec, req, sizeof(resp), resp);
+ if (ret)
+ return -EIO;
+
+ extr_resp(data, resp, GAOKUN_SMART_CHARGE_DATA_SIZE);
+
+ return 0;
+}
+EXPORT_SYMBOL_GPL(gaokun_ec_psy_get_smart_charge);
+
+int gaokun_ec_psy_set_smart_charge(struct gaokun_ec *ec,
+ u8 data[GAOKUN_SMART_CHARGE_DATA_SIZE])
+{
+ /* SBCM */
+ u8 req[] = MKREQ(0x02, 0XE3, GAOKUN_SMART_CHARGE_DATA_SIZE);
+
+ if (!are_thresholds_valid(data[2], data[3]))
+ return -EINVAL;
+
+ refill_req(req, data, GAOKUN_SMART_CHARGE_DATA_SIZE);
+
+ return gaokun_ec_write(ec, req);
+}
+EXPORT_SYMBOL_GPL(gaokun_ec_psy_set_smart_charge);
+
+/* Smart charge enable */
+int gaokun_ec_psy_get_smart_charge_enable(struct gaokun_ec *ec, bool *on)
+{
+ /* GBAC */
+ *on = 0; /* clear other 3 Bytes */
+ return gaokun_ec_read_byte(ec, (u8 [])MKREQ(0x02, 0xE6, 0), (u8 *)on);
+}
+EXPORT_SYMBOL_GPL(gaokun_ec_psy_get_smart_charge_enable);
+
+int gaokun_ec_psy_set_smart_charge_enable(struct gaokun_ec *ec, bool on)
+{
+ /* SBAC */
+ return gaokun_ec_write(ec, (u8 [])MKREQ(0x02, 0xE5, 1, on));
+}
+EXPORT_SYMBOL_GPL(gaokun_ec_psy_set_smart_charge_enable);
+
+/* -------------------------------------------------------------------------- */
+/* API For UCSI */
+
+int gaokun_ec_ucsi_read(struct gaokun_ec *ec,
+ u8 resp[GAOKUN_UCSI_READ_SIZE])
+{
+ u8 req[] = MKREQ(0x03, 0xD5, 0);
+ u8 _resp[] = MKRESP(GAOKUN_UCSI_READ_SIZE);
+ int ret;
+
+ ret = gaokun_ec_read(ec, req, sizeof(_resp), _resp);
+ if (ret)
+ return ret;
+
+ extr_resp(resp, _resp, GAOKUN_UCSI_READ_SIZE);
+ return 0;
+}
+EXPORT_SYMBOL_GPL(gaokun_ec_ucsi_read);
+
+int gaokun_ec_ucsi_write(struct gaokun_ec *ec,
+ const u8 req[GAOKUN_UCSI_WRITE_SIZE])
+{
+ u8 _req[] = MKREQ(0x03, 0xD4, GAOKUN_UCSI_WRITE_SIZE);
+
+
+ refill_req(_req, req, GAOKUN_UCSI_WRITE_SIZE);
+
+ return gaokun_ec_write(ec, _req);
+}
+EXPORT_SYMBOL_GPL(gaokun_ec_ucsi_write);
+
+int gaokun_ec_ucsi_get_reg(struct gaokun_ec *ec, u8 *ureg)
+{
+ u8 req[] = MKREQ(0x03, 0xD3, 0);
+ u8 _resp[] = MKRESP(UCSI_REG_SIZE);
+ int ret;
+
+ ret = gaokun_ec_read(ec, req, sizeof(_resp), _resp);
+ if (ret)
+ return ret;
+
+ extr_resp(ureg, _resp, UCSI_REG_SIZE);
+
+ return 0;
+}
+EXPORT_SYMBOL_GPL(gaokun_ec_ucsi_get_reg);
+
+int gaokun_ec_ucsi_pan_ack(struct gaokun_ec *ec, int port_id)
+{
+ u8 req[] = MKREQ(0x03, 0xD2, 1);
+ u8 data = 1 << port_id;
+
+ if (port_id == GAOKUN_UCSI_NO_PORT_UPDATE)
+ data = 0;
+
+ refill_req(req, &data, 1);
+
+ return gaokun_ec_write(ec, req);
+}
+EXPORT_SYMBOL_GPL(gaokun_ec_ucsi_pan_ack);
+
+/* -------------------------------------------------------------------------- */
+/* EC Sysfs */
+
+/* Fn lock */
+static int gaokun_ec_get_fn_lock(struct gaokun_ec *ec, bool *on)
+{
+ /* GFRS */
+ u8 req[] = MKREQ(0x02, 0x6B, 0);
+ int ret;
+ u8 val;
+
+ ret = gaokun_ec_read_byte(ec, req, &val);
+ if (ret)
+ return ret;
+
+ if (val == EC_FN_LOCK_ON)
+ *on = true;
+ else if (val == EC_FN_LOCK_OFF)
+ *on = false;
+ else
+ return -EIO;
+
+ return 0;
+}
+
+static int gaokun_ec_set_fn_lock(struct gaokun_ec *ec, bool on)
+{
+ /* SFRS */
+ u8 req[] = MKREQ(0x02, 0x6C, 1);
+ u8 data;
+
+ if (on)
+ data = EC_FN_LOCK_ON;
+ else
+ data = EC_FN_LOCK_OFF;
+
+ refill_req(req, &data, 1);
+
+ return gaokun_ec_write(ec, req);
+}
+
+static ssize_t fn_lock_show(struct device *dev,
+ struct device_attribute *attr,
+ char *buf)
+{
+ struct gaokun_ec *ec = dev_get_drvdata(dev);
+ bool on;
+ int ret;
+
+ ret = gaokun_ec_get_fn_lock(ec, &on);
+ if (ret)
+ return ret;
+
+ return sysfs_emit(buf, "%d\n", on);
+}
+
+static ssize_t fn_lock_store(struct device *dev,
+ struct device_attribute *attr,
+ const char *buf, size_t size)
+{
+ struct gaokun_ec *ec = dev_get_drvdata(dev);
+ bool on;
+ int ret;
+
+ if (kstrtobool(buf, &on))
+ return -EINVAL;
+
+ ret = gaokun_ec_set_fn_lock(ec, on);
+ if (ret)
+ return ret;
+
+ return size;
+}
+
+static DEVICE_ATTR_RW(fn_lock);
+
+static struct attribute *gaokun_ec_attrs[] = {
+ &dev_attr_fn_lock.attr,
+ NULL,
+};
+ATTRIBUTE_GROUPS(gaokun_ec);
+
+/* Thermal Zone HwMon */
+/* Range from 0 to 0x2C, partial valid */
+static const u8 temp_reg[20] = {0x05, 0x07, 0x08, 0x0E, 0x0F, 0x12, 0x15, 0x1E,
+ 0x1F, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26,
+ 0x27, 0x28, 0x29, 0x2A};
+
+static int gaokun_ec_get_temp(struct gaokun_ec *ec, u8 idx, int *temp)
+{
+ /* GTMP */
+ u8 req[] = MKREQ(0x02, 0x61, 1, temp_reg[idx]);
+ u8 resp[] = MKRESP(sizeof(__le16));
+ __le16 tmp;
+ int ret;
+
+ ret = gaokun_ec_read(ec, req, sizeof(resp), resp);
+ if (ret)
+ return ret;
+
+ extr_resp((u8 *)&tmp, resp, sizeof(tmp));
+ *temp = le16_to_cpu(tmp) * 100; /* convert to HwMon's unit */
+
+ return 0;
+}
+
+static ssize_t get_ec_tz_temp(struct device *dev,
+ struct device_attribute *attr,
+ char *buf)
+{
+ struct gaokun_ec *ec = dev_get_drvdata(dev);
+ int idx, ret, temp;
+
+ idx = (to_sensor_dev_attr(attr))->index - 1;
+ ret = gaokun_ec_get_temp(ec, idx, &temp);
+ if (ret)
+ return ret;
+
+ return sysfs_emit(buf, "%d\n", temp);
+}
+
+static ssize_t ec_tz_temp_label(struct device *dev,
+ struct device_attribute *attr,
+ char *buf)
+{
+ int idx = (to_sensor_dev_attr(attr))->index - 1;
+
+ return sysfs_emit(buf, "EC Thermal Zone %2d Temperature\n", idx);
+}
+
+static SENSOR_DEVICE_ATTR(temp1_input, 0444, get_ec_tz_temp, NULL, 1);
+static SENSOR_DEVICE_ATTR(temp1_label, 0444, ec_tz_temp_label, NULL, 1);
+static SENSOR_DEVICE_ATTR(temp2_input, 0444, get_ec_tz_temp, NULL, 2);
+static SENSOR_DEVICE_ATTR(temp2_label, 0444, ec_tz_temp_label, NULL, 2);
+static SENSOR_DEVICE_ATTR(temp3_input, 0444, get_ec_tz_temp, NULL, 3);
+static SENSOR_DEVICE_ATTR(temp3_label, 0444, ec_tz_temp_label, NULL, 3);
+static SENSOR_DEVICE_ATTR(temp4_input, 0444, get_ec_tz_temp, NULL, 4);
+static SENSOR_DEVICE_ATTR(temp4_label, 0444, ec_tz_temp_label, NULL, 4);
+static SENSOR_DEVICE_ATTR(temp5_input, 0444, get_ec_tz_temp, NULL, 5);
+static SENSOR_DEVICE_ATTR(temp5_label, 0444, ec_tz_temp_label, NULL, 5);
+static SENSOR_DEVICE_ATTR(temp6_input, 0444, get_ec_tz_temp, NULL, 6);
+static SENSOR_DEVICE_ATTR(temp6_label, 0444, ec_tz_temp_label, NULL, 6);
+static SENSOR_DEVICE_ATTR(temp7_input, 0444, get_ec_tz_temp, NULL, 7);
+static SENSOR_DEVICE_ATTR(temp7_label, 0444, ec_tz_temp_label, NULL, 7);
+static SENSOR_DEVICE_ATTR(temp8_input, 0444, get_ec_tz_temp, NULL, 8);
+static SENSOR_DEVICE_ATTR(temp8_label, 0444, ec_tz_temp_label, NULL, 8);
+static SENSOR_DEVICE_ATTR(temp9_input, 0444, get_ec_tz_temp, NULL, 9);
+static SENSOR_DEVICE_ATTR(temp9_label, 0444, ec_tz_temp_label, NULL, 9);
+static SENSOR_DEVICE_ATTR(temp10_input, 0444, get_ec_tz_temp, NULL, 10);
+static SENSOR_DEVICE_ATTR(temp10_label, 0444, ec_tz_temp_label, NULL, 10);
+static SENSOR_DEVICE_ATTR(temp11_input, 0444, get_ec_tz_temp, NULL, 11);
+static SENSOR_DEVICE_ATTR(temp11_label, 0444, ec_tz_temp_label, NULL, 11);
+static SENSOR_DEVICE_ATTR(temp12_input, 0444, get_ec_tz_temp, NULL, 12);
+static SENSOR_DEVICE_ATTR(temp12_label, 0444, ec_tz_temp_label, NULL, 12);
+static SENSOR_DEVICE_ATTR(temp13_input, 0444, get_ec_tz_temp, NULL, 13);
+static SENSOR_DEVICE_ATTR(temp13_label, 0444, ec_tz_temp_label, NULL, 13);
+static SENSOR_DEVICE_ATTR(temp14_input, 0444, get_ec_tz_temp, NULL, 14);
+static SENSOR_DEVICE_ATTR(temp14_label, 0444, ec_tz_temp_label, NULL, 14);
+static SENSOR_DEVICE_ATTR(temp15_input, 0444, get_ec_tz_temp, NULL, 15);
+static SENSOR_DEVICE_ATTR(temp15_label, 0444, ec_tz_temp_label, NULL, 15);
+static SENSOR_DEVICE_ATTR(temp16_input, 0444, get_ec_tz_temp, NULL, 16);
+static SENSOR_DEVICE_ATTR(temp16_label, 0444, ec_tz_temp_label, NULL, 16);
+static SENSOR_DEVICE_ATTR(temp17_input, 0444, get_ec_tz_temp, NULL, 17);
+static SENSOR_DEVICE_ATTR(temp17_label, 0444, ec_tz_temp_label, NULL, 17);
+static SENSOR_DEVICE_ATTR(temp18_input, 0444, get_ec_tz_temp, NULL, 18);
+static SENSOR_DEVICE_ATTR(temp18_label, 0444, ec_tz_temp_label, NULL, 18);
+static SENSOR_DEVICE_ATTR(temp19_input, 0444, get_ec_tz_temp, NULL, 19);
+static SENSOR_DEVICE_ATTR(temp19_label, 0444, ec_tz_temp_label, NULL, 19);
+static SENSOR_DEVICE_ATTR(temp20_input, 0444, get_ec_tz_temp, NULL, 20);
+static SENSOR_DEVICE_ATTR(temp20_label, 0444, ec_tz_temp_label, NULL, 20);
+
+static struct attribute *gaokun_ec_hwmon_attrs[] = {
+ &sensor_dev_attr_temp1_input.dev_attr.attr,
+ &sensor_dev_attr_temp1_label.dev_attr.attr,
+ &sensor_dev_attr_temp2_input.dev_attr.attr,
+ &sensor_dev_attr_temp2_label.dev_attr.attr,
+ &sensor_dev_attr_temp3_input.dev_attr.attr,
+ &sensor_dev_attr_temp3_label.dev_attr.attr,
+ &sensor_dev_attr_temp4_input.dev_attr.attr,
+ &sensor_dev_attr_temp4_label.dev_attr.attr,
+ &sensor_dev_attr_temp5_input.dev_attr.attr,
+ &sensor_dev_attr_temp5_label.dev_attr.attr,
+ &sensor_dev_attr_temp6_input.dev_attr.attr,
+ &sensor_dev_attr_temp6_label.dev_attr.attr,
+ &sensor_dev_attr_temp7_input.dev_attr.attr,
+ &sensor_dev_attr_temp7_label.dev_attr.attr,
+ &sensor_dev_attr_temp8_input.dev_attr.attr,
+ &sensor_dev_attr_temp8_label.dev_attr.attr,
+ &sensor_dev_attr_temp9_input.dev_attr.attr,
+ &sensor_dev_attr_temp9_label.dev_attr.attr,
+ &sensor_dev_attr_temp10_input.dev_attr.attr,
+ &sensor_dev_attr_temp10_label.dev_attr.attr,
+ &sensor_dev_attr_temp11_input.dev_attr.attr,
+ &sensor_dev_attr_temp11_label.dev_attr.attr,
+ &sensor_dev_attr_temp12_input.dev_attr.attr,
+ &sensor_dev_attr_temp12_label.dev_attr.attr,
+ &sensor_dev_attr_temp13_input.dev_attr.attr,
+ &sensor_dev_attr_temp13_label.dev_attr.attr,
+ &sensor_dev_attr_temp14_input.dev_attr.attr,
+ &sensor_dev_attr_temp14_label.dev_attr.attr,
+ &sensor_dev_attr_temp15_input.dev_attr.attr,
+ &sensor_dev_attr_temp15_label.dev_attr.attr,
+ &sensor_dev_attr_temp16_input.dev_attr.attr,
+ &sensor_dev_attr_temp16_label.dev_attr.attr,
+ &sensor_dev_attr_temp17_input.dev_attr.attr,
+ &sensor_dev_attr_temp17_label.dev_attr.attr,
+ &sensor_dev_attr_temp18_input.dev_attr.attr,
+ &sensor_dev_attr_temp18_label.dev_attr.attr,
+ &sensor_dev_attr_temp19_input.dev_attr.attr,
+ &sensor_dev_attr_temp19_label.dev_attr.attr,
+ &sensor_dev_attr_temp20_input.dev_attr.attr,
+ &sensor_dev_attr_temp20_label.dev_attr.attr,
+ NULL
+};
+ATTRIBUTE_GROUPS(gaokun_ec_hwmon);
+
+/* -------------------------------------------------------------------------- */
+/* Modern Standby */
+
+static int gaokun_ec_suspend(struct device *dev)
+{
+ struct gaokun_ec *ec = dev_get_drvdata(dev);
+ u8 req[] = MKREQ(0x02, 0xB2, 1, 0xDB);
+ int ret;
+
+ if (ec->suspended)
+ return 0;
+
+ ret = gaokun_ec_write(ec, req);
+
+ if (ret)
+ return ret;
+
+ ec->suspended = true;
+
+ return 0;
+}
+
+static int gaokun_ec_resume(struct device *dev)
+{
+ struct gaokun_ec *ec = dev_get_drvdata(dev);
+ u8 req[] = MKREQ(0x02, 0xB2, 1, 0xEB);
+ int ret;
+ int i;
+
+ if (!ec->suspended)
+ return 0;
+
+ for (i = 0; i < 3; ++i) {
+ ret = gaokun_ec_write(ec, req);
+ if (ret == 0)
+ break;
+
+ msleep(100); /* EC need time to resume */
+ };
+
+ ec->suspended = false;
+
+ return 0;
+}
+
+static void gaokun_aux_release(struct device *dev)
+{
+ struct auxiliary_device *adev = to_auxiliary_dev(dev);
+
+ kfree(adev);
+}
+
+static void gaokun_aux_remove(void *data)
+{
+ struct auxiliary_device *adev = data;
+
+ auxiliary_device_delete(adev);
+ auxiliary_device_uninit(adev);
+}
+
+static int gaokun_aux_init(struct device *parent, const char *name,
+ struct gaokun_ec *ec)
+{
+ struct auxiliary_device *adev;
+ int ret;
+
+ adev = kzalloc(sizeof(*adev), GFP_KERNEL);
+ if (!adev)
+ return -ENOMEM;
+
+ adev->name = name;
+ adev->id = 0;
+ adev->dev.parent = parent;
+ adev->dev.release = gaokun_aux_release;
+ adev->dev.platform_data = ec;
+ /* Allow aux devices to access parent's DT nodes directly */
+ device_set_of_node_from_dev(&adev->dev, parent);
+
+ ret = auxiliary_device_init(adev);
+ if (ret) {
+ kfree(adev);
+ return ret;
+ }
+
+ ret = auxiliary_device_add(adev);
+ if (ret) {
+ auxiliary_device_uninit(adev);
+ return ret;
+ }
+
+ return devm_add_action_or_reset(parent, gaokun_aux_remove, adev);
+}
+
+/* -------------------------------------------------------------------------- */
+/* EC */
+
+static irqreturn_t gaokun_ec_irq_handler(int irq, void *data)
+{
+ struct gaokun_ec *ec = data;
+ u8 req[] = MKREQ(EC_EVENT, EC_QUERY, 0);
+ u8 status, id;
+ int ret;
+
+ ret = gaokun_ec_read_byte(ec, req, &id);
+ if (ret)
+ return IRQ_HANDLED;
+
+ switch (id) {
+ case 0x0: /* No event */
+ break;
+
+ case EC_EVENT_LID:
+ gaokun_ec_psy_read_byte(ec, EC_LID_STATE, &status);
+ status = EC_LID_OPEN & status;
+ input_report_switch(ec->idev, SW_LID, !status);
+ input_sync(ec->idev);
+ break;
+
+ default:
+ blocking_notifier_call_chain(&ec->notifier_list, id, ec);
+ }
+
+ return IRQ_HANDLED;
+}
+
+static int gaokun_ec_probe(struct i2c_client *client)
+{
+ struct device *dev = &client->dev;
+ struct gaokun_ec *ec;
+ int ret;
+
+ ec = devm_kzalloc(dev, sizeof(*ec), GFP_KERNEL);
+ if (!ec)
+ return -ENOMEM;
+
+ mutex_init(&ec->lock);
+ ec->client = client;
+ i2c_set_clientdata(client, ec);
+ BLOCKING_INIT_NOTIFIER_HEAD(&ec->notifier_list);
+
+ /* Lid switch */
+ ec->idev = devm_input_allocate_device(dev);
+ if (!ec->idev)
+ return -ENOMEM;
+
+ ec->idev->name = "LID";
+ ec->idev->phys = "gaokun-ec/input0";
+ input_set_capability(ec->idev, EV_SW, SW_LID);
+
+ ret = input_register_device(ec->idev);
+ if (ret)
+ return dev_err_probe(dev, ret, "Failed to register input device\n");
+
+ ret = gaokun_aux_init(dev, "psy", ec);
+ if (ret)
+ return ret;
+
+ ret = gaokun_aux_init(dev, "ucsi", ec);
+ if (ret)
+ return ret;
+
+ ret = devm_request_threaded_irq(dev, client->irq, NULL,
+ gaokun_ec_irq_handler, IRQF_ONESHOT,
+ dev_name(dev), ec);
+ if (ret)
+ return dev_err_probe(dev, ret, "Failed to request irq\n");
+
+ ec->hwmon_dev = hwmon_device_register_with_groups(dev, "gaokun_ec_hwmon",
+ ec, gaokun_ec_hwmon_groups);
+ if (IS_ERR(ec->hwmon_dev)) {
+ dev_err(dev, "Failed to register hwmon device\n");
+ return PTR_ERR(ec->hwmon_dev);
+ }
+
+ return 0;
+}
+
+static void gaokun_ec_remove(struct i2c_client *client)
+{
+ struct gaokun_ec *ec = i2c_get_clientdata(client);
+ hwmon_device_unregister(ec->hwmon_dev);
+}
+
+static const struct i2c_device_id gaokun_ec_id[] = {
+ { "gaokun-ec", },
+ { }
+};
+MODULE_DEVICE_TABLE(i2c, gaokun_ec_id);
+
+static const struct of_device_id gaokun_ec_of_match[] = {
+ { .compatible = "huawei,gaokun-ec", },
+ { }
+};
+MODULE_DEVICE_TABLE(of, gaokun_ec_of_match);
+
+static const struct dev_pm_ops gaokun_ec_pm_ops = {
+ NOIRQ_SYSTEM_SLEEP_PM_OPS(gaokun_ec_suspend, gaokun_ec_resume)
+};
+
+static struct i2c_driver gaokun_ec_driver = {
+ .driver = {
+ .name = "gaokun-ec",
+ .of_match_table = gaokun_ec_of_match,
+ .pm = &gaokun_ec_pm_ops,
+ .dev_groups = gaokun_ec_groups,
+ },
+ .probe = gaokun_ec_probe,
+ .remove = gaokun_ec_remove,
+ .id_table = gaokun_ec_id,
+};
+module_i2c_driver(gaokun_ec_driver);
+
+MODULE_DESCRIPTION("HUAWEI Matebook E Go EC driver");
+MODULE_AUTHOR("Pengyu Luo <mitltlatltl@gmail.com>");
+MODULE_LICENSE("GPL");
diff --git a/include/linux/platform_data/huawei-gaokun-ec.h b/include/linux/platform_data/huawei-gaokun-ec.h
new file mode 100644
index 000000000..7a8d17391
--- /dev/null
+++ b/include/linux/platform_data/huawei-gaokun-ec.h
@@ -0,0 +1,79 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Huawei Matebook E Go Embedded Controller
+ *
+ * Copyright (C) 2024 Pengyu Luo <mitltlatltl@gmail.com>
+ */
+
+#ifndef __HUAWEI_GAOKUN_EC_H__
+#define __HUAWEI_GAOKUN_EC_H__
+
+#define GAOKUN_UCSI_CCI_SIZE 4
+#define GAOKUN_UCSI_DATA_SIZE 16
+#define GAOKUN_UCSI_READ_SIZE (GAOKUN_UCSI_CCI_SIZE + GAOKUN_UCSI_DATA_SIZE)
+#define GAOKUN_UCSI_WRITE_SIZE 0x18
+
+#define GAOKUN_UCSI_NO_PORT_UPDATE (-1)
+
+#define GAOKUN_SMART_CHARGE_DATA_SIZE 4 /* mode, delay, start, end */
+
+/* -------------------------------------------------------------------------- */
+
+struct gaokun_ec;
+struct notifier_block;
+
+#define GAOKUN_MOD_NAME "huawei_gaokun_ec"
+#define GAOKUN_DEV_PSY "psy"
+#define GAOKUN_DEV_UCSI "ucsi"
+
+/* -------------------------------------------------------------------------- */
+/* Common API */
+
+int gaokun_ec_register_notify(struct gaokun_ec *ec,
+ struct notifier_block *nb);
+void gaokun_ec_unregister_notify(struct gaokun_ec *ec,
+ struct notifier_block *nb);
+
+int gaokun_ec_read(struct gaokun_ec *ec, const u8 *req,
+ size_t resp_len, u8 *resp);
+int gaokun_ec_write(struct gaokun_ec *ec, u8 *req);
+int gaokun_ec_read_byte(struct gaokun_ec *ec, u8 *req, u8 *byte);
+
+/* -------------------------------------------------------------------------- */
+/* API For PSY */
+
+int gaokun_ec_psy_multi_read(struct gaokun_ec *ec, u8 reg,
+ size_t resp_len, u8 *resp);
+
+static inline int gaokun_ec_psy_read_byte(struct gaokun_ec *ec,
+ u8 reg, u8 *byte)
+{
+ return gaokun_ec_psy_multi_read(ec, reg, sizeof(*byte), byte);
+}
+
+static inline int gaokun_ec_psy_read_word(struct gaokun_ec *ec,
+ u8 reg, u16 *word)
+{
+ return gaokun_ec_psy_multi_read(ec, reg, sizeof(*word), (u8 *)word);
+}
+
+int gaokun_ec_psy_get_smart_charge(struct gaokun_ec *ec,
+ u8 data[GAOKUN_SMART_CHARGE_DATA_SIZE]);
+int gaokun_ec_psy_set_smart_charge(struct gaokun_ec *ec,
+ u8 data[GAOKUN_SMART_CHARGE_DATA_SIZE]);
+
+int gaokun_ec_psy_get_smart_charge_enable(struct gaokun_ec *ec, bool *on);
+int gaokun_ec_psy_set_smart_charge_enable(struct gaokun_ec *ec, bool on);
+
+/* -------------------------------------------------------------------------- */
+/* API For UCSI */
+
+int gaokun_ec_ucsi_read(struct gaokun_ec *ec, u8 resp[GAOKUN_UCSI_READ_SIZE]);
+int gaokun_ec_ucsi_write(struct gaokun_ec *ec,
+ const u8 req[GAOKUN_UCSI_WRITE_SIZE]);
+
+int gaokun_ec_ucsi_get_reg(struct gaokun_ec *ec, u8 *ureg);
+int gaokun_ec_ucsi_pan_ack(struct gaokun_ec *ec, int port_id);
+
+
+#endif /* __HUAWEI_GAOKUN_EC_H__ */
--
2.47.1
^ permalink raw reply related [flat|nested] 15+ messages in thread
* [PATCH v2 3/5] usb: typec: ucsi: add Huawei Matebook E Go ucsi driver
2025-01-05 17:41 [PATCH v2 0/5] platform: arm64: Huawei Matebook E Go embedded controller Pengyu Luo
2025-01-05 17:41 ` [PATCH v2 1/5] dt-bindings: platform: Add Huawei Matebook E Go EC Pengyu Luo
2025-01-05 17:41 ` [PATCH v2 2/5] platform: arm64: add Huawei Matebook E Go EC driver Pengyu Luo
@ 2025-01-05 17:41 ` Pengyu Luo
2025-01-07 12:30 ` Heikki Krogerus
2025-01-05 17:41 ` [PATCH v2 4/5] power: supply: add Huawei Matebook E Go psy driver Pengyu Luo
` (2 subsequent siblings)
5 siblings, 1 reply; 15+ messages in thread
From: Pengyu Luo @ 2025-01-05 17:41 UTC (permalink / raw)
To: Rob Herring, Krzysztof Kozlowski, Conor Dooley, Bjorn Andersson,
Konrad Dybcio, Hans de Goede, Ilpo Järvinen,
Bryan O'Donoghue, Sebastian Reichel, Heikki Krogerus,
Greg Kroah-Hartman, Jean Delvare, Guenter Roeck
Cc: devicetree, linux-kernel, linux-arm-msm, platform-driver-x86,
linux-pm, linux-usb, linux-hwmon, Dmitry Baryshkov, Pengyu Luo
The Huawei Matebook E Go tablet implements the UCSI interface in the
onboard EC. Add the glue driver to interface with the platform's UCSI
implementation.
Signed-off-by: Pengyu Luo <mitltlatltl@gmail.com>
---
drivers/usb/typec/ucsi/Kconfig | 10 +
drivers/usb/typec/ucsi/Makefile | 1 +
drivers/usb/typec/ucsi/ucsi_huawei_gaokun.c | 524 ++++++++++++++++++++
3 files changed, 535 insertions(+)
create mode 100644 drivers/usb/typec/ucsi/ucsi_huawei_gaokun.c
diff --git a/drivers/usb/typec/ucsi/Kconfig b/drivers/usb/typec/ucsi/Kconfig
index 680e1b87b..8a73df84a 100644
--- a/drivers/usb/typec/ucsi/Kconfig
+++ b/drivers/usb/typec/ucsi/Kconfig
@@ -78,4 +78,14 @@ config UCSI_LENOVO_YOGA_C630
To compile the driver as a module, choose M here: the module will be
called ucsi_yoga_c630.
+config UCSI_HUAWEI_GAOKUN
+ tristate "UCSI Interface Driver for Huawei Matebook E Go"
+ depends on EC_HUAWEI_GAOKUN
+ help
+ This driver enables UCSI support on the Huawei Matebook E Go tablet,
+ which is a sc8280xp-based 2-in-1 tablet.
+
+ To compile the driver as a module, choose M here: the module will be
+ called ucsi_huawei_gaokun.
+
endif
diff --git a/drivers/usb/typec/ucsi/Makefile b/drivers/usb/typec/ucsi/Makefile
index aed41d238..0b400122b 100644
--- a/drivers/usb/typec/ucsi/Makefile
+++ b/drivers/usb/typec/ucsi/Makefile
@@ -22,3 +22,4 @@ obj-$(CONFIG_UCSI_CCG) += ucsi_ccg.o
obj-$(CONFIG_UCSI_STM32G0) += ucsi_stm32g0.o
obj-$(CONFIG_UCSI_PMIC_GLINK) += ucsi_glink.o
obj-$(CONFIG_UCSI_LENOVO_YOGA_C630) += ucsi_yoga_c630.o
+obj-$(CONFIG_UCSI_HUAWEI_GAOKUN) += ucsi_huawei_gaokun.o
diff --git a/drivers/usb/typec/ucsi/ucsi_huawei_gaokun.c b/drivers/usb/typec/ucsi/ucsi_huawei_gaokun.c
new file mode 100644
index 000000000..044545b44
--- /dev/null
+++ b/drivers/usb/typec/ucsi/ucsi_huawei_gaokun.c
@@ -0,0 +1,524 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * ucsi-huawei-gaokun - A UCSI driver for HUAWEI Matebook E Go
+ *
+ * reference: drivers/usb/typec/ucsi/ucsi_yoga_c630.c
+ * drivers/usb/typec/ucsi/ucsi_glink.c
+ * drivers/soc/qcom/pmic_glink_altmode.c
+ *
+ * Copyright (C) 2024 Pengyu Luo <mitltlatltl@gmail.com>
+ */
+
+#include <drm/bridge/aux-bridge.h>
+#include <linux/auxiliary_bus.h>
+#include <linux/bitops.h>
+#include <linux/completion.h>
+#include <linux/container_of.h>
+#include <linux/module.h>
+#include <linux/notifier.h>
+#include <linux/of.h>
+#include <linux/platform_data/huawei-gaokun-ec.h>
+#include <linux/string.h>
+#include <linux/usb/pd_vdo.h>
+#include <linux/usb/typec_altmode.h>
+#include <linux/usb/typec_dp.h>
+#include <linux/workqueue_types.h>
+
+#include "ucsi.h"
+
+#define EC_EVENT_UCSI 0x21
+#define EC_EVENT_USB 0x22
+
+#define GAOKUN_CCX_MASK GENMASK(1, 0)
+#define GAOKUN_MUX_MASK GENMASK(3, 2)
+
+#define GAOKUN_DPAM_MASK GENMASK(3, 0)
+#define GAOKUN_HPD_STATE_MASK BIT(4)
+#define GAOKUN_HPD_IRQ_MASK BIT(5)
+
+#define GET_IDX(updt) (ffs(updt) - 1)
+
+#define CCX_TO_ORI(ccx) (++ccx % 3) /* convert ccx to enum typec_orientation */
+
+/* Configuration Channel Extension */
+enum gaokun_ucsi_ccx {
+ USBC_CCX_NORMAL,
+ USBC_CCX_REVERSE,
+ USBC_CCX_NONE,
+};
+
+enum gaokun_ucsi_mux {
+ USBC_MUX_NONE,
+ USBC_MUX_USB_2L,
+ USBC_MUX_DP_4L,
+ USBC_MUX_USB_DP,
+};
+/* based on pmic_glink_altmode_pin_assignment */
+enum gaokun_ucsi_dpam_pan { /* DP Alt Mode Pin Assignments */
+ USBC_DPAM_PAN_NONE,
+ USBC_DPAM_PAN_A, /* Not supported after USB Type-C Standard v1.0b */
+ USBC_DPAM_PAN_B, /* Not supported after USB Type-C Standard v1.0b */
+ USBC_DPAM_PAN_C, /* USBC_DPAM_PAN_C_REVERSE - 6 */
+ USBC_DPAM_PAN_D,
+ USBC_DPAM_PAN_E,
+ USBC_DPAM_PAN_F, /* Not supported after USB Type-C Standard v1.0b */
+ USBC_DPAM_PAN_A_REVERSE,/* Not supported after USB Type-C Standard v1.0b */
+ USBC_DPAM_PAN_B_REVERSE,/* Not supported after USB Type-C Standard v1.0b */
+ USBC_DPAM_PAN_C_REVERSE,
+ USBC_DPAM_PAN_D_REVERSE,
+ USBC_DPAM_PAN_E_REVERSE,
+ USBC_DPAM_PAN_F_REVERSE,/* Not supported after USB Type-C Standard v1.0b */
+};
+
+struct gaokun_ucsi_reg {
+ u8 port_num;
+ u8 port_updt;
+ u8 port_data[4];
+ u8 checksum;
+ u8 reserved;
+} __packed;
+
+struct gaokun_ucsi_port {
+ struct completion usb_ack;
+ spinlock_t lock;
+
+ struct gaokun_ucsi *ucsi;
+ struct auxiliary_device *bridge;
+
+ int idx;
+ enum gaokun_ucsi_ccx ccx;
+ enum gaokun_ucsi_mux mux;
+ u8 mode;
+ u16 svid;
+ u8 hpd_state;
+ u8 hpd_irq;
+};
+
+struct gaokun_ucsi {
+ struct gaokun_ec *ec;
+ struct ucsi *ucsi;
+ struct gaokun_ucsi_port *ports;
+ struct device *dev;
+ struct delayed_work work;
+ struct notifier_block nb;
+ u16 version;
+ u8 port_num;
+};
+
+/* -------------------------------------------------------------------------- */
+/* For UCSI */
+
+static int gaokun_ucsi_read_version(struct ucsi *ucsi, u16 *version)
+{
+ struct gaokun_ucsi *uec = ucsi_get_drvdata(ucsi);
+
+ *version = uec->version;
+
+ return 0;
+}
+
+static int gaokun_ucsi_read_cci(struct ucsi *ucsi, u32 *cci)
+{
+ struct gaokun_ucsi *uec = ucsi_get_drvdata(ucsi);
+ u8 buf[GAOKUN_UCSI_READ_SIZE];
+ int ret;
+
+ ret = gaokun_ec_ucsi_read(uec->ec, buf);
+ if (ret)
+ return ret;
+
+ memcpy(cci, buf, sizeof(*cci));
+
+ return 0;
+}
+
+static int gaokun_ucsi_read_message_in(struct ucsi *ucsi,
+ void *val, size_t val_len)
+{
+ struct gaokun_ucsi *uec = ucsi_get_drvdata(ucsi);
+ u8 buf[GAOKUN_UCSI_READ_SIZE];
+ int ret;
+
+ ret = gaokun_ec_ucsi_read(uec->ec, buf);
+ if (ret)
+ return ret;
+
+ memcpy(val, buf + GAOKUN_UCSI_CCI_SIZE,
+ min(val_len, GAOKUN_UCSI_DATA_SIZE));
+
+ return 0;
+}
+
+static int gaokun_ucsi_async_control(struct ucsi *ucsi, u64 command)
+{
+ struct gaokun_ucsi *uec = ucsi_get_drvdata(ucsi);
+ u8 buf[GAOKUN_UCSI_WRITE_SIZE] = {};
+
+ memcpy(buf, &command, sizeof(command));
+
+ return gaokun_ec_ucsi_write(uec->ec, buf);
+}
+
+static void gaokun_ucsi_update_connector(struct ucsi_connector *con)
+{
+ struct gaokun_ucsi *uec = ucsi_get_drvdata(con->ucsi);
+
+ if (con->num > uec->port_num)
+ return;
+
+ con->typec_cap.orientation_aware = true;
+}
+
+static void gaokun_set_orientation(struct ucsi_connector *con,
+ struct gaokun_ucsi_port *port)
+{
+ enum gaokun_ucsi_ccx ccx;
+ unsigned long flags;
+
+ spin_lock_irqsave(&port->lock, flags);
+ ccx = port->ccx;
+ spin_unlock_irqrestore(&port->lock, flags);
+
+ typec_set_orientation(con->port, CCX_TO_ORI(ccx));
+}
+
+static void gaokun_ucsi_connector_status(struct ucsi_connector *con)
+{
+ struct gaokun_ucsi *uec = ucsi_get_drvdata(con->ucsi);
+ int idx;
+
+ idx = con->num - 1;
+ if (con->num > uec->port_num) {
+ dev_warn(uec->dev, "set orientation out of range: con%d\n", idx);
+ return;
+ }
+
+ gaokun_set_orientation(con, &uec->ports[idx]);
+}
+
+const struct ucsi_operations gaokun_ucsi_ops = {
+ .read_version = gaokun_ucsi_read_version,
+ .read_cci = gaokun_ucsi_read_cci,
+ .read_message_in = gaokun_ucsi_read_message_in,
+ .sync_control = ucsi_sync_control_common,
+ .async_control = gaokun_ucsi_async_control,
+ .update_connector = gaokun_ucsi_update_connector,
+ .connector_status = gaokun_ucsi_connector_status,
+};
+
+/* -------------------------------------------------------------------------- */
+/* For Altmode */
+
+static void gaokun_ucsi_port_update(struct gaokun_ucsi_port *port,
+ const u8 *port_data)
+{
+ struct gaokun_ucsi *uec = port->ucsi;
+ int offset = port->idx * 2; /* every port has 2 Bytes data */
+ unsigned long flags;
+ u8 dcc, ddi;
+
+ dcc = port_data[offset];
+ ddi = port_data[offset + 1];
+
+ spin_lock_irqsave(&port->lock, flags);
+
+ port->ccx = FIELD_GET(GAOKUN_CCX_MASK, dcc);
+ port->mux = FIELD_GET(GAOKUN_MUX_MASK, dcc);
+ port->mode = FIELD_GET(GAOKUN_DPAM_MASK, ddi);
+ port->hpd_state = FIELD_GET(GAOKUN_HPD_STATE_MASK, ddi);
+ port->hpd_irq = FIELD_GET(GAOKUN_HPD_IRQ_MASK, ddi);
+
+ /* Mode and SVID are unused; keeping them to make things clearer */
+ switch (port->mode) {
+ case USBC_DPAM_PAN_C:
+ case USBC_DPAM_PAN_C_REVERSE:
+ port->mode = DP_PIN_ASSIGN_C; /* correct it for usb later */
+ break;
+ case USBC_DPAM_PAN_D:
+ case USBC_DPAM_PAN_D_REVERSE:
+ port->mode = DP_PIN_ASSIGN_D;
+ break;
+ case USBC_DPAM_PAN_E:
+ case USBC_DPAM_PAN_E_REVERSE:
+ port->mode = DP_PIN_ASSIGN_E;
+ break;
+ case USBC_DPAM_PAN_NONE:
+ port->mode = TYPEC_STATE_SAFE;
+ break;
+ default:
+ dev_warn(uec->dev, "unknow mode %d\n", port->mode);
+ break;
+ }
+
+ switch (port->mux) {
+ case USBC_MUX_NONE:
+ port->svid = 0;
+ break;
+ case USBC_MUX_USB_2L:
+ port->svid = USB_SID_PD;
+ port->mode = TYPEC_STATE_USB; /* same as PAN_C, correct it */
+ break;
+ case USBC_MUX_DP_4L:
+ case USBC_MUX_USB_DP:
+ port->svid = USB_SID_DISPLAYPORT;
+ break;
+ default:
+ dev_warn(uec->dev, "unknow mux state %d\n", port->mux);
+ break;
+ }
+
+ spin_unlock_irqrestore(&port->lock, flags);
+}
+
+static int gaokun_ucsi_refresh(struct gaokun_ucsi *uec)
+{
+ struct gaokun_ucsi_reg ureg;
+ int ret, idx;
+
+ ret = gaokun_ec_ucsi_get_reg(uec->ec, (u8 *)&ureg);
+ if (ret)
+ return GAOKUN_UCSI_NO_PORT_UPDATE;
+
+ uec->port_num = ureg.port_num;
+ idx = GET_IDX(ureg.port_updt);
+
+ if (idx < 0 || idx >= ureg.port_num)
+ return GAOKUN_UCSI_NO_PORT_UPDATE;
+
+ gaokun_ucsi_port_update(&uec->ports[idx], ureg.port_data);
+ return idx;
+}
+
+static void gaokun_ucsi_handle_altmode(struct gaokun_ucsi_port *port)
+{
+ struct gaokun_ucsi *uec = port->ucsi;
+ int idx = port->idx;
+
+ if (idx >= uec->ucsi->cap.num_connectors) {
+ dev_warn(uec->dev, "altmode port out of range: %d\n", idx);
+ return;
+ }
+
+ /* UCSI callback .connector_status() have set orientation */
+ if (port->bridge)
+ drm_aux_hpd_bridge_notify(&port->bridge->dev,
+ port->hpd_state ?
+ connector_status_connected :
+ connector_status_disconnected);
+
+ gaokun_ec_ucsi_pan_ack(uec->ec, port->idx);
+}
+
+static void gaokun_ucsi_altmode_notify_ind(struct gaokun_ucsi *uec)
+{
+ int idx;
+
+ if (!uec->ucsi->connector) { /* slow to register */
+ dev_err(uec->dev, "ucsi connector is not initialized yet\n");
+ return;
+ }
+
+ idx = gaokun_ucsi_refresh(uec);
+ if (idx == GAOKUN_UCSI_NO_PORT_UPDATE)
+ gaokun_ec_ucsi_pan_ack(uec->ec, idx); /* ack directly if no update */
+ else
+ gaokun_ucsi_handle_altmode(&uec->ports[idx]);
+}
+
+/*
+ * USB event is necessary for enabling altmode, the event should follow
+ * UCSI event, if not after timeout(this notify may be disabled somehow),
+ * then force to enable altmode.
+ */
+static void gaokun_ucsi_handle_no_usb_event(struct gaokun_ucsi *uec, int idx)
+{
+ struct gaokun_ucsi_port *port;
+
+ port = &uec->ports[idx];
+ if (!wait_for_completion_timeout(&port->usb_ack, 2 * HZ)) {
+ dev_warn(uec->dev, "No USB EVENT, triggered by UCSI EVENT");
+ gaokun_ucsi_altmode_notify_ind(uec);
+ }
+}
+
+static int gaokun_ucsi_notify(struct notifier_block *nb,
+ unsigned long action, void *data)
+{
+ u32 cci;
+ struct gaokun_ucsi *uec = container_of(nb, struct gaokun_ucsi, nb);
+
+ switch (action) {
+ case EC_EVENT_USB:
+ gaokun_ucsi_altmode_notify_ind(uec);
+ return NOTIFY_OK;
+
+ case EC_EVENT_UCSI:
+ uec->ucsi->ops->read_cci(uec->ucsi, &cci);
+ ucsi_notify_common(uec->ucsi, cci);
+ if (UCSI_CCI_CONNECTOR(cci))
+ gaokun_ucsi_handle_no_usb_event(uec, UCSI_CCI_CONNECTOR(cci) - 1);
+
+ return NOTIFY_OK;
+
+ default:
+ return NOTIFY_DONE;
+ }
+}
+
+static inline int gaokun_ucsi_get_port_num(struct gaokun_ucsi *uec)
+{
+ struct gaokun_ucsi_reg ureg;
+ int ret;
+
+ ret = gaokun_ec_ucsi_get_reg(uec->ec, (u8 *)&ureg);
+
+ return ret ? 0 : ureg.port_num;
+}
+
+static int gaokun_ucsi_ports_init(struct gaokun_ucsi *uec)
+{
+ u32 port;
+ int i, ret, port_num;
+ struct device *dev = uec->dev;
+ struct gaokun_ucsi_port *ucsi_port;
+ struct fwnode_handle *fwnode;
+
+ port_num = gaokun_ucsi_get_port_num(uec);
+ uec->port_num = port_num;
+
+ uec->ports = devm_kcalloc(dev, port_num, sizeof(*(uec->ports)),
+ GFP_KERNEL);
+ if (!uec->ports)
+ return -ENOMEM;
+
+ for (i = 0; i < port_num; ++i) {
+ ucsi_port = &uec->ports[i];
+ ucsi_port->ccx = USBC_CCX_NONE;
+ ucsi_port->idx = i;
+ ucsi_port->ucsi = uec;
+ init_completion(&ucsi_port->usb_ack);
+ spin_lock_init(&ucsi_port->lock);
+ }
+
+ device_for_each_child_node(dev, fwnode) {
+ ret = fwnode_property_read_u32(fwnode, "reg", &port);
+ if (ret < 0) {
+ dev_err(dev, "missing reg property of %pOFn\n", fwnode);
+ fwnode_handle_put(fwnode);
+ return ret;
+ }
+
+ if (port >= port_num) {
+ dev_warn(dev, "invalid connector number %d, ignoring\n", port);
+ continue;
+ }
+
+ ucsi_port = &uec->ports[port];
+ ucsi_port->bridge = devm_drm_dp_hpd_bridge_alloc(dev, to_of_node(fwnode));
+ if (IS_ERR(ucsi_port->bridge)) {
+ fwnode_handle_put(fwnode);
+ return PTR_ERR(ucsi_port->bridge);
+ }
+ }
+
+ for (i = 0; i < port_num; i++) {
+ if (!uec->ports[i].bridge)
+ continue;
+
+ ret = devm_drm_dp_hpd_bridge_add(dev, uec->ports[i].bridge);
+ if (ret)
+ return ret;
+ }
+
+ return 0;
+}
+
+static void gaokun_ucsi_register_worker(struct work_struct *work)
+{
+ struct gaokun_ucsi *uec;
+ struct ucsi *ucsi;
+ int ret;
+
+ uec = container_of(work, struct gaokun_ucsi, work.work);
+ ucsi = uec->ucsi;
+ /* This may be a problem specific to sc8280xp-based machines */
+ ucsi->quirks = UCSI_NO_PARTNER_PDOS | UCSI_DELAY_DEVICE_PDOS;
+
+ ret = gaokun_ec_register_notify(uec->ec, &uec->nb);
+ if (ret) {
+ dev_err_probe(ucsi->dev, ret, "notifier register failed\n");
+ return;
+ }
+
+ ret = ucsi_register(ucsi);
+ if (ret)
+ dev_err_probe(ucsi->dev, ret, "ucsi register failed\n");
+}
+
+static inline int gaokun_ucsi_register(struct gaokun_ucsi *uec)
+{
+ /* EC can't handle UCSI properly in the early stage */
+ schedule_delayed_work(&uec->work, 3 * HZ);
+
+ return 0;
+}
+
+static int gaokun_ucsi_probe(struct auxiliary_device *adev,
+ const struct auxiliary_device_id *id)
+{
+ struct gaokun_ec *ec = adev->dev.platform_data;
+ struct device *dev = &adev->dev;
+ struct gaokun_ucsi *uec;
+ int ret;
+
+ uec = devm_kzalloc(dev, sizeof(*uec), GFP_KERNEL);
+ if (!uec)
+ return -ENOMEM;
+
+ uec->ec = ec;
+ uec->dev = dev;
+ uec->version = 0x0100;
+ uec->nb.notifier_call = gaokun_ucsi_notify;
+
+ INIT_DELAYED_WORK(&uec->work, gaokun_ucsi_register_worker);
+
+ ret = gaokun_ucsi_ports_init(uec);
+ if (ret)
+ return ret;
+
+ uec->ucsi = ucsi_create(dev, &gaokun_ucsi_ops);
+ if (IS_ERR(uec->ucsi))
+ return PTR_ERR(uec->ucsi);
+
+ ucsi_set_drvdata(uec->ucsi, uec);
+ auxiliary_set_drvdata(adev, uec);
+
+ return gaokun_ucsi_register(uec);
+}
+
+static void gaokun_ucsi_remove(struct auxiliary_device *adev)
+{
+ struct gaokun_ucsi *uec = auxiliary_get_drvdata(adev);
+
+ gaokun_ec_unregister_notify(uec->ec, &uec->nb);
+ ucsi_unregister(uec->ucsi);
+ ucsi_destroy(uec->ucsi);
+}
+
+static const struct auxiliary_device_id gaokun_ucsi_id_table[] = {
+ { .name = GAOKUN_MOD_NAME "." GAOKUN_DEV_UCSI, },
+ {}
+};
+MODULE_DEVICE_TABLE(auxiliary, gaokun_ucsi_id_table);
+
+static struct auxiliary_driver gaokun_ucsi_driver = {
+ .name = GAOKUN_DEV_UCSI,
+ .id_table = gaokun_ucsi_id_table,
+ .probe = gaokun_ucsi_probe,
+ .remove = gaokun_ucsi_remove,
+};
+
+module_auxiliary_driver(gaokun_ucsi_driver);
+
+MODULE_DESCRIPTION("HUAWEI Matebook E Go UCSI driver");
+MODULE_LICENSE("GPL");
--
2.47.1
^ permalink raw reply related [flat|nested] 15+ messages in thread
* [PATCH v2 4/5] power: supply: add Huawei Matebook E Go psy driver
2025-01-05 17:41 [PATCH v2 0/5] platform: arm64: Huawei Matebook E Go embedded controller Pengyu Luo
` (2 preceding siblings ...)
2025-01-05 17:41 ` [PATCH v2 3/5] usb: typec: ucsi: add Huawei Matebook E Go ucsi driver Pengyu Luo
@ 2025-01-05 17:41 ` Pengyu Luo
2025-01-05 17:41 ` [PATCH v2 5/5] arm64: dts: qcom: gaokun3: Add Embedded Controller node Pengyu Luo
2025-01-06 19:27 ` [PATCH v2 0/5] platform: arm64: Huawei Matebook E Go embedded controller Rob Herring (Arm)
5 siblings, 0 replies; 15+ messages in thread
From: Pengyu Luo @ 2025-01-05 17:41 UTC (permalink / raw)
To: Rob Herring, Krzysztof Kozlowski, Conor Dooley, Bjorn Andersson,
Konrad Dybcio, Hans de Goede, Ilpo Järvinen,
Bryan O'Donoghue, Sebastian Reichel, Heikki Krogerus,
Greg Kroah-Hartman, Jean Delvare, Guenter Roeck
Cc: devicetree, linux-kernel, linux-arm-msm, platform-driver-x86,
linux-pm, linux-usb, linux-hwmon, Dmitry Baryshkov, Pengyu Luo
On the Huawei Matebook E Go tablet the EC provides access to the adapter
and battery status. Add the driver to read power supply status on the
tablet.
Signed-off-by: Pengyu Luo <mitltlatltl@gmail.com>
---
.../ABI/testing/sysfs-class-power-gaokun | 47 ++
drivers/power/supply/Kconfig | 10 +
drivers/power/supply/Makefile | 1 +
drivers/power/supply/huawei-gaokun-battery.c | 548 ++++++++++++++++++
4 files changed, 606 insertions(+)
create mode 100644 Documentation/ABI/testing/sysfs-class-power-gaokun
create mode 100644 drivers/power/supply/huawei-gaokun-battery.c
diff --git a/Documentation/ABI/testing/sysfs-class-power-gaokun b/Documentation/ABI/testing/sysfs-class-power-gaokun
new file mode 100644
index 000000000..b1eb9e8d7
--- /dev/null
+++ b/Documentation/ABI/testing/sysfs-class-power-gaokun
@@ -0,0 +1,47 @@
+What: /sys/class/power_supply/gaokun-ec-battery/smart_charge
+Date: January 2025
+KernelVersion: 6.12
+Contact: Pengyu Luo <mitltlatltl@gmail.com>
+Description:
+ This entry allows configuration of smart charging behavior with
+ four parameters. The format is: <mode> <delay> <start> <stop>.
+
+ - mode: Defines the charging mode (1 or 4). Mode 4 enables delay,
+ while mode 1 does not.
+ - delay: Specifies the delay in hours (non-negative). This is
+ only used when 'mode' is set to 4.
+ - 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'.
+
+ If mode is set to 4, the above charging mode will only occur
+ after the specified delay in hours. If mode is 1, there is
+ no delay.
+
+ Access: Read, Write
+
+ Valid values:
+ - mode: integer value (1 or 4)
+ - delay: integer value, delay in hours (non-negative)
+ - start: integer value, battery percentage (0-100)
+ - stop: integer value, battery percentage (1-100)
+
+What: /sys/class/power_supply/gaokun-ec-battery/smart_charge_enable
+Date: January 2025
+KernelVersion: 6.12
+Contact: Pengyu Luo <mitltlatltl@gmail.com>
+Description:
+ This entry allows enabling smart charging.
+
+ Access: Read, Write
+
+ Valid values: 0 (disabled) or 1 (enabled)
diff --git a/drivers/power/supply/Kconfig b/drivers/power/supply/Kconfig
index 9f2eef678..f00983186 100644
--- a/drivers/power/supply/Kconfig
+++ b/drivers/power/supply/Kconfig
@@ -161,6 +161,16 @@ config BATTERY_DS2782
Say Y here to enable support for the DS2782/DS2786 standalone battery
gas-gauge.
+config BATTERY_HUAWEI_GAOKUN
+ tristate "Huawei Matebook E Go power supply"
+ depends on EC_HUAWEI_GAOKUN
+ help
+ This driver enables battery and adapter support on the Huawei Matebook
+ E Go, which is a sc8280xp-based 2-in-1 tablet.
+
+ To compile the driver as a module, choose M here: the module will be
+ called huawei-gaokun-battery.
+
config BATTERY_LEGO_EV3
tristate "LEGO MINDSTORMS EV3 battery"
depends on OF && IIO && GPIOLIB && (ARCH_DAVINCI_DA850 || COMPILE_TEST)
diff --git a/drivers/power/supply/Makefile b/drivers/power/supply/Makefile
index 59c4a9f40..c0941a832 100644
--- a/drivers/power/supply/Makefile
+++ b/drivers/power/supply/Makefile
@@ -31,6 +31,7 @@ obj-$(CONFIG_BATTERY_DS2781) += ds2781_battery.o
obj-$(CONFIG_BATTERY_DS2782) += ds2782_battery.o
obj-$(CONFIG_BATTERY_GAUGE_LTC2941) += ltc2941-battery-gauge.o
obj-$(CONFIG_BATTERY_GOLDFISH) += goldfish_battery.o
+obj-$(CONFIG_BATTERY_HUAWEI_GAOKUN) += huawei-gaokun-battery.o
obj-$(CONFIG_BATTERY_LEGO_EV3) += lego_ev3_battery.o
obj-$(CONFIG_BATTERY_LENOVO_YOGA_C630) += lenovo_yoga_c630_battery.o
obj-$(CONFIG_BATTERY_PMU) += pmu_battery.o
diff --git a/drivers/power/supply/huawei-gaokun-battery.c b/drivers/power/supply/huawei-gaokun-battery.c
new file mode 100644
index 000000000..65b617aea
--- /dev/null
+++ b/drivers/power/supply/huawei-gaokun-battery.c
@@ -0,0 +1,548 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * huawei-gaokun-battery - A power supply driver for HUAWEI Matebook E Go
+ *
+ * reference: drivers/power/supply/lenovo_yoga_c630_battery.c
+ * drivers/platform/arm64/acer-aspire1-ec.c
+ * drivers/acpi/battery.c
+ * drivers/acpi/ac.c
+ *
+ * Copyright (C) 2024 Pengyu Luo <mitltlatltl@gmail.com>
+ */
+
+#include <linux/auxiliary_bus.h>
+#include <linux/bits.h>
+#include <linux/delay.h>
+#include <linux/jiffies.h>
+#include <linux/module.h>
+#include <linux/notifier.h>
+#include <linux/platform_data/huawei-gaokun-ec.h>
+#include <linux/power_supply.h>
+#include <linux/sprintf.h>
+
+/* -------------------------------------------------------------------------- */
+/* String Data Reg */
+
+#define EC_BAT_VENDOR 0x01 /* from 0x01 to 0x0F, SUNWODA */
+#define EC_BAT_MODEL 0x11 /* from 0x11 to 0x1F, HB30A8P9ECW-22T */
+
+#define EC_ADP_STATUS 0x81
+#define EC_AC_STATUS BIT(0)
+#define EC_BAT_PRESENT BIT(1) /* BATC._STA */
+
+#define EC_BAT_STATUS 0x82 /* _BST */
+#define EC_BAT_DISCHARGING BIT(0)
+#define EC_BAT_CHARGING BIT(1)
+#define EC_BAT_CRITICAL BIT(2) /* Low Battery Level */
+#define EC_BAT_FULL BIT(3)
+
+/* -------------------------------------------------------------------------- */
+/* Word Data Reg */
+
+/* 0x5A: ?
+ * 0x5C: ?
+ * 0x5E: ?
+ * 0X60: ?
+ * 0x84: ?
+ */
+
+#define EC_BAT_STATUS_START 0x90
+#define EC_BAT_PERCENTAGE 0x90
+#define EC_BAT_VOLTAGE 0x92
+#define EC_BAT_CAPACITY 0x94
+#define EC_BAT_FULL_CAPACITY 0x96
+/* 0x98: ? */
+#define EC_BAT_CURRENT 0x9A
+/* 0x9C: ? */
+
+#define EC_BAT_INFO_START 0xA0
+/* 0xA0: POWER_SUPPLY_PROP_INPUT_VOLTAGE_LIMIT? */
+#define EC_BAT_DESIGN_CAPACITY 0xA2
+#define EC_BAT_DESIGN_VOLTAGE 0xA4
+#define EC_BAT_SERIAL_NUMBER 0xA6
+#define EC_BAT_CYCLE_COUNT 0xAA
+
+/* -------------------------------------------------------------------------- */
+/* Battery Event ID */
+
+#define EC_EVENT_BAT_A0 0xA0
+#define EC_EVENT_BAT_A1 0xA1
+#define EC_EVENT_BAT_A2 0xA2
+#define EC_EVENT_BAT_A3 0xA3
+#define EC_EVENT_BAT_B1 0xB1
+/* EVENT B1 A0 A1 repeat about every 1s 2s 3s respectively */
+
+/* ACPI _BIX field, Min sampling time, the duration between two _BST */
+#define CACHE_TIME 3000 /* cache time in milliseconds */
+
+#define MILLI_TO_MICRO 1000
+
+struct gaokun_psy_bat_status {
+ __le16 percentage_now; /* 0x90 */
+ __le16 voltage_now;
+ __le16 capacity_now;
+ __le16 full_capacity;
+ __le16 unknown1;
+ __le16 rate_now;
+ __le16 unknown2; /* 0x9C */
+} __packed;
+
+struct gaokun_psy_bat_info {
+ __le16 unknown3; /* 0xA0 */
+ __le16 design_capacity;
+ __le16 design_voltage;
+ __le16 serial_number;
+ __le16 padding2;
+ __le16 cycle_count; /* 0xAA */
+} __packed;
+
+struct gaokun_psy {
+ struct gaokun_ec *ec;
+ struct device *dev;
+ struct notifier_block nb;
+
+ struct power_supply *bat_psy;
+ struct power_supply *adp_psy;
+
+ unsigned long update_time;
+ struct gaokun_psy_bat_status status;
+ struct gaokun_psy_bat_info info;
+
+ char battery_model[0x10]; /* HB30A8P9ECW-22T, the real one is XXX-22A */
+ char battery_serial[0x10];
+ char battery_vendor[0x10];
+
+ int charge_now;
+ int online;
+ int bat_present;
+};
+
+/* -------------------------------------------------------------------------- */
+/* Adapter */
+
+static int gaokun_psy_get_adp_status(struct gaokun_psy *ecbat)
+{
+ /* _PSR */
+ int ret;
+ u8 online;
+
+ ret = gaokun_ec_psy_read_byte(ecbat->ec, EC_ADP_STATUS, &online);
+ if (ret)
+ return ret;
+
+ ecbat->online = !!(online & EC_AC_STATUS);
+
+ return 0;
+}
+
+static int gaokun_psy_get_adp_property(struct power_supply *psy,
+ enum power_supply_property psp,
+ union power_supply_propval *val)
+{
+ struct gaokun_psy *ecbat = power_supply_get_drvdata(psy);
+ int ret;
+
+ ret = gaokun_psy_get_adp_status(ecbat);
+ if (ret)
+ return ret;
+
+ switch (psp) {
+ case POWER_SUPPLY_PROP_ONLINE:
+ val->intval = ecbat->online;
+ break;
+
+ default:
+ return -EINVAL;
+ }
+
+ return 0;
+}
+
+static enum power_supply_property gaokun_psy_adp_props[] = {
+ POWER_SUPPLY_PROP_ONLINE,
+};
+
+static const struct power_supply_desc gaokun_psy_adp_desc = {
+ .name = "gaokun-ec-adapter",
+ .type = POWER_SUPPLY_TYPE_USB_TYPE_C,
+ .get_property = gaokun_psy_get_adp_property,
+ .properties = gaokun_psy_adp_props,
+ .num_properties = ARRAY_SIZE(gaokun_psy_adp_props),
+};
+
+/* -------------------------------------------------------------------------- */
+/* Battery */
+
+static inline void gaokun_psy_get_bat_present(struct gaokun_psy *ecbat)
+{
+ int ret;
+ u8 present;
+
+ /* Some kind of initialization */
+ gaokun_ec_write(ecbat->ec, (u8 []){0x02, 0xB2, 1, 0x90});
+
+ ret = gaokun_ec_psy_read_byte(ecbat->ec, EC_ADP_STATUS, &present);
+
+ ecbat->bat_present = ret ? false : !!(present & EC_BAT_PRESENT);
+}
+
+static inline int gaokun_psy_bat_present(struct gaokun_psy *ecbat)
+{
+ return ecbat->bat_present;
+}
+
+static int gaokun_psy_get_bat_info(struct gaokun_psy *ecbat)
+{
+ /* _BIX */
+ if (!gaokun_psy_bat_present(ecbat))
+ return 0;
+
+ return gaokun_ec_psy_multi_read(ecbat->ec, EC_BAT_INFO_START,
+ sizeof(ecbat->info), (u8 *)&ecbat->info);
+}
+
+static void gaokun_psy_update_bat_charge(struct gaokun_psy *ecbat)
+{
+ u8 charge;
+
+ gaokun_ec_psy_read_byte(ecbat->ec, EC_BAT_STATUS, &charge);
+
+ switch (charge) {
+ case EC_BAT_CHARGING:
+ ecbat->charge_now = POWER_SUPPLY_STATUS_CHARGING;
+ break;
+ case EC_BAT_DISCHARGING:
+ ecbat->charge_now = POWER_SUPPLY_STATUS_DISCHARGING;
+ break;
+ case EC_BAT_FULL:
+ ecbat->charge_now = POWER_SUPPLY_STATUS_FULL;
+ break;
+ default:
+ dev_warn(ecbat->dev, "unknown charge state %d\n", charge);
+ }
+}
+
+static int gaokun_psy_get_bat_status(struct gaokun_psy *ecbat)
+{
+ /* _BST */
+ int ret;
+
+ if (time_before(jiffies, ecbat->update_time +
+ msecs_to_jiffies(CACHE_TIME)))
+ return 0;
+
+ gaokun_psy_update_bat_charge(ecbat);
+ ret = gaokun_ec_psy_multi_read(ecbat->ec, EC_BAT_STATUS_START,
+ sizeof(ecbat->status), (u8 *)&ecbat->status);
+
+ ecbat->update_time = jiffies;
+
+ return ret;
+}
+
+static void gaokun_psy_init(struct gaokun_psy *ecbat)
+{
+ gaokun_psy_get_bat_present(ecbat);
+ if (!gaokun_psy_bat_present(ecbat))
+ return;
+
+ gaokun_psy_get_bat_info(ecbat);
+
+ snprintf(ecbat->battery_serial, sizeof(ecbat->battery_serial),
+ "%d", le16_to_cpu(ecbat->info.serial_number));
+
+ gaokun_ec_psy_multi_read(ecbat->ec, EC_BAT_VENDOR,
+ sizeof(ecbat->battery_vendor) - 1,
+ ecbat->battery_vendor);
+
+ gaokun_ec_psy_multi_read(ecbat->ec, EC_BAT_MODEL,
+ sizeof(ecbat->battery_model) - 1,
+ ecbat->battery_model);
+
+ ecbat->battery_model[14] = 'A'; /* FIX UP */
+}
+
+static int gaokun_psy_get_bat_property(struct power_supply *psy,
+ enum power_supply_property psp,
+ union power_supply_propval *val)
+{
+ struct gaokun_psy *ecbat = power_supply_get_drvdata(psy);
+
+ if (gaokun_psy_bat_present(ecbat))
+ gaokun_psy_get_bat_status(ecbat);
+ else if (psp != POWER_SUPPLY_PROP_PRESENT)
+ return -ENODEV;
+
+ switch (psp) {
+ case POWER_SUPPLY_PROP_STATUS:
+ val->intval = ecbat->charge_now;
+ break;
+
+ case POWER_SUPPLY_PROP_PRESENT:
+ val->intval = ecbat->bat_present;
+ break;
+
+ case POWER_SUPPLY_PROP_TECHNOLOGY:
+ val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
+ break;
+
+ case POWER_SUPPLY_PROP_CYCLE_COUNT:
+ val->intval = le16_to_cpu(ecbat->info.cycle_count);
+ break;
+
+ case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
+ val->intval = le16_to_cpu(ecbat->info.design_voltage) * MILLI_TO_MICRO;
+ break;
+
+ case POWER_SUPPLY_PROP_VOLTAGE_NOW:
+ val->intval = le16_to_cpu(ecbat->status.voltage_now) * MILLI_TO_MICRO;
+ break;
+
+ case POWER_SUPPLY_PROP_CURRENT_NOW:
+ val->intval = (s16)le16_to_cpu(ecbat->status.rate_now) * MILLI_TO_MICRO;
+ break;
+
+ case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
+ val->intval = le16_to_cpu(ecbat->info.design_capacity) * MILLI_TO_MICRO;
+ break;
+
+ case POWER_SUPPLY_PROP_CHARGE_FULL:
+ val->intval = le16_to_cpu(ecbat->status.full_capacity) * MILLI_TO_MICRO;
+ break;
+
+ case POWER_SUPPLY_PROP_CHARGE_NOW:
+ val->intval = le16_to_cpu(ecbat->status.capacity_now) * MILLI_TO_MICRO;
+ break;
+
+ case POWER_SUPPLY_PROP_CAPACITY:
+ val->intval = le16_to_cpu(ecbat->status.percentage_now);
+ break;
+
+ case POWER_SUPPLY_PROP_MODEL_NAME:
+ val->strval = ecbat->battery_model;
+ break;
+
+ case POWER_SUPPLY_PROP_MANUFACTURER:
+ val->strval = ecbat->battery_vendor;
+ break;
+
+ case POWER_SUPPLY_PROP_SERIAL_NUMBER:
+ val->strval = ecbat->battery_serial;
+ break;
+
+ default:
+ return -EINVAL;
+ }
+ return 0;
+}
+
+static enum power_supply_property gaokun_psy_bat_props[] = {
+ POWER_SUPPLY_PROP_STATUS,
+ POWER_SUPPLY_PROP_PRESENT,
+ POWER_SUPPLY_PROP_TECHNOLOGY,
+ POWER_SUPPLY_PROP_CYCLE_COUNT,
+ POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
+ POWER_SUPPLY_PROP_VOLTAGE_NOW,
+ POWER_SUPPLY_PROP_CURRENT_NOW,
+ POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
+ POWER_SUPPLY_PROP_CHARGE_FULL,
+ POWER_SUPPLY_PROP_CHARGE_NOW,
+ POWER_SUPPLY_PROP_CAPACITY,
+ POWER_SUPPLY_PROP_MODEL_NAME,
+ POWER_SUPPLY_PROP_MANUFACTURER,
+ POWER_SUPPLY_PROP_SERIAL_NUMBER,
+};
+
+static const struct power_supply_desc gaokun_psy_bat_desc = {
+ .name = "gaokun-ec-battery",
+ .type = POWER_SUPPLY_TYPE_BATTERY,
+ .get_property = gaokun_psy_get_bat_property,
+ .properties = gaokun_psy_bat_props,
+ .num_properties = ARRAY_SIZE(gaokun_psy_bat_props),
+};
+
+/* -------------------------------------------------------------------------- */
+/* Sysfs */
+
+/* Smart charge enable */
+static ssize_t smart_charge_enable_show(struct device *dev,
+ struct device_attribute *attr,
+ char *buf)
+{
+ struct power_supply *psy = to_power_supply(dev);
+ struct gaokun_psy *ecbat = power_supply_get_drvdata(psy);
+ int ret;
+ bool on;
+
+ ret = gaokun_ec_psy_get_smart_charge_enable(ecbat->ec, &on);
+ if (ret)
+ return ret;
+
+ return sysfs_emit(buf, "%d\n", on);
+}
+
+static ssize_t smart_charge_enable_store(struct device *dev,
+ struct device_attribute *attr,
+ const char *buf, size_t size)
+{
+ struct power_supply *psy = to_power_supply(dev);
+ struct gaokun_psy *ecbat = power_supply_get_drvdata(psy);
+ int ret;
+ bool on;
+
+ if (kstrtobool(buf, &on))
+ return -EINVAL;
+
+ ret = gaokun_ec_psy_set_smart_charge_enable(ecbat->ec, on);
+ if (ret)
+ return ret;
+
+ return size;
+}
+
+static DEVICE_ATTR_RW(smart_charge_enable);
+
+/* Smart charge */
+static ssize_t smart_charge_show(struct device *dev,
+ struct device_attribute *attr,
+ char *buf)
+{
+ struct power_supply *psy = to_power_supply(dev);
+ struct gaokun_psy *ecbat = power_supply_get_drvdata(psy);
+ u8 bf[GAOKUN_SMART_CHARGE_DATA_SIZE];
+ int ret;
+
+ ret = gaokun_ec_psy_get_smart_charge(ecbat->ec, bf);
+ if (ret)
+ return ret;
+
+ return sysfs_emit(buf, "%d %d %d %d\n",
+ bf[0], bf[1], bf[2], bf[3]);
+}
+
+static ssize_t smart_charge_store(struct device *dev,
+ struct device_attribute *attr,
+ const char *buf, size_t size)
+{
+ struct power_supply *psy = to_power_supply(dev);
+ struct gaokun_psy *ecbat = power_supply_get_drvdata(psy);
+ u8 bf[GAOKUN_SMART_CHARGE_DATA_SIZE];
+ int ret;
+
+ if (sscanf(buf, "%hhd %hhd %hhd %hhd", bf, bf + 1, bf + 2, bf + 3) != 4)
+ return -EINVAL;
+
+ if (bf[0] != 1 && bf[0] != 4)
+ return -EINVAL;
+
+ ret = gaokun_ec_psy_set_smart_charge(ecbat->ec, bf);
+ if (ret)
+ return ret;
+
+ return size;
+}
+
+static DEVICE_ATTR_RW(smart_charge);
+
+static struct attribute *gaokun_psy_features_attrs[] = {
+ &dev_attr_smart_charge_enable.attr,
+ &dev_attr_smart_charge.attr,
+ NULL,
+};
+ATTRIBUTE_GROUPS(gaokun_psy_features);
+
+static int gaokun_psy_notify(struct notifier_block *nb,
+ unsigned long action, void *data)
+{
+ struct gaokun_psy *ecbat = container_of(nb, struct gaokun_psy, nb);
+
+ switch (action) {
+ case EC_EVENT_BAT_A2:
+ case EC_EVENT_BAT_B1:
+ gaokun_psy_get_bat_info(ecbat);
+ return NOTIFY_OK;
+
+ case EC_EVENT_BAT_A0:
+ gaokun_psy_get_adp_status(ecbat);
+ power_supply_changed(ecbat->adp_psy);
+ msleep(10);
+ fallthrough;
+
+ case EC_EVENT_BAT_A1:
+ case EC_EVENT_BAT_A3:
+ if (action == EC_EVENT_BAT_A3) {
+ gaokun_psy_get_bat_info(ecbat);
+ msleep(100);
+ }
+ gaokun_psy_get_bat_status(ecbat);
+ power_supply_changed(ecbat->bat_psy);
+ return NOTIFY_OK;
+
+ default:
+ return NOTIFY_DONE;
+ }
+}
+
+static int gaokun_psy_probe(struct auxiliary_device *adev,
+ const struct auxiliary_device_id *id)
+{
+ struct gaokun_ec *ec = adev->dev.platform_data;
+ struct power_supply_config psy_cfg = {};
+ struct device *dev = &adev->dev;
+ struct gaokun_psy *ecbat;
+
+ ecbat = devm_kzalloc(&adev->dev, sizeof(*ecbat), GFP_KERNEL);
+ if (!ecbat)
+ return -ENOMEM;
+
+ ecbat->ec = ec;
+ ecbat->dev = dev;
+ ecbat->nb.notifier_call = gaokun_psy_notify;
+
+ auxiliary_set_drvdata(adev, ecbat);
+
+ psy_cfg.drv_data = ecbat;
+ ecbat->adp_psy = devm_power_supply_register(dev, &gaokun_psy_adp_desc,
+ &psy_cfg);
+ if (IS_ERR(ecbat->adp_psy))
+ return dev_err_probe(dev, PTR_ERR(ecbat->adp_psy),
+ "Failed to register AC power supply\n");
+
+ psy_cfg.supplied_to = (char **)&gaokun_psy_bat_desc.name;
+ psy_cfg.num_supplicants = 1;
+ psy_cfg.no_wakeup_source = true;
+ psy_cfg.attr_grp = gaokun_psy_features_groups;
+ ecbat->bat_psy = devm_power_supply_register(dev, &gaokun_psy_bat_desc,
+ &psy_cfg);
+ if (IS_ERR(ecbat->bat_psy))
+ return dev_err_probe(dev, PTR_ERR(ecbat->bat_psy),
+ "Failed to register battery power supply\n");
+ gaokun_psy_init(ecbat);
+
+ return gaokun_ec_register_notify(ec, &ecbat->nb);
+}
+
+static void gaokun_psy_remove(struct auxiliary_device *adev)
+{
+ struct gaokun_psy *ecbat = auxiliary_get_drvdata(adev);
+
+ gaokun_ec_unregister_notify(ecbat->ec, &ecbat->nb);
+}
+
+static const struct auxiliary_device_id gaokun_psy_id_table[] = {
+ { .name = GAOKUN_MOD_NAME "." GAOKUN_DEV_PSY, },
+ {}
+};
+MODULE_DEVICE_TABLE(auxiliary, gaokun_psy_id_table);
+
+static struct auxiliary_driver gaokun_psy_driver = {
+ .name = GAOKUN_DEV_PSY,
+ .id_table = gaokun_psy_id_table,
+ .probe = gaokun_psy_probe,
+ .remove = gaokun_psy_remove,
+};
+
+module_auxiliary_driver(gaokun_psy_driver);
+
+MODULE_DESCRIPTION("HUAWEI Matebook E Go psy driver");
+MODULE_LICENSE("GPL");
--
2.47.1
^ permalink raw reply related [flat|nested] 15+ messages in thread
* [PATCH v2 5/5] arm64: dts: qcom: gaokun3: Add Embedded Controller node
2025-01-05 17:41 [PATCH v2 0/5] platform: arm64: Huawei Matebook E Go embedded controller Pengyu Luo
` (3 preceding siblings ...)
2025-01-05 17:41 ` [PATCH v2 4/5] power: supply: add Huawei Matebook E Go psy driver Pengyu Luo
@ 2025-01-05 17:41 ` Pengyu Luo
2025-01-09 14:37 ` Konrad Dybcio
2025-01-06 19:27 ` [PATCH v2 0/5] platform: arm64: Huawei Matebook E Go embedded controller Rob Herring (Arm)
5 siblings, 1 reply; 15+ messages in thread
From: Pengyu Luo @ 2025-01-05 17:41 UTC (permalink / raw)
To: Rob Herring, Krzysztof Kozlowski, Conor Dooley, Bjorn Andersson,
Konrad Dybcio, Hans de Goede, Ilpo Järvinen,
Bryan O'Donoghue, Sebastian Reichel, Heikki Krogerus,
Greg Kroah-Hartman, Jean Delvare, Guenter Roeck
Cc: devicetree, linux-kernel, linux-arm-msm, platform-driver-x86,
linux-pm, linux-usb, linux-hwmon, Dmitry Baryshkov, 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 ports, orientation switches and the QMP combo PHY.
Signed-off-by: Pengyu Luo <mitltlatltl@gmail.com>
---
.../boot/dts/qcom/sc8280xp-huawei-gaokun3.dts | 139 ++++++++++++++++++
1 file changed, 139 insertions(+)
diff --git a/arch/arm64/boot/dts/qcom/sc8280xp-huawei-gaokun3.dts b/arch/arm64/boot/dts/qcom/sc8280xp-huawei-gaokun3.dts
index 09b95f89e..ff5db8f63 100644
--- a/arch/arm64/boot/dts/qcom/sc8280xp-huawei-gaokun3.dts
+++ b/arch/arm64/boot/dts/qcom/sc8280xp-huawei-gaokun3.dts
@@ -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,81 @@ touchscreen@4f {
};
+&i2c15 {
+ clock-frequency = <400000>;
+
+ pinctrl-0 = <&i2c15_default>;
+ pinctrl-names = "default";
+
+ status = "okay";
+
+ embedded-controller@38 {
+ compatible = "huawei,gaokun3", "huawei,gaokun-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>;
+ };
+ };
+ };
+ };
+ };
+};
+
&mdss0 {
status = "okay";
};
@@ -1025,6 +1135,10 @@ &usb_0_qmpphy_dp_in {
remote-endpoint = <&mdss0_dp0_out>;
};
+&usb_0_qmpphy_out {
+ remote-endpoint = <&ucsi0_ss_in>;
+};
+
&usb_1 {
status = "okay";
};
@@ -1054,6 +1168,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 +1295,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 +1426,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";
--
2.47.1
^ permalink raw reply related [flat|nested] 15+ messages in thread
* Re: [PATCH v2 1/5] dt-bindings: platform: Add Huawei Matebook E Go EC
2025-01-05 17:41 ` [PATCH v2 1/5] dt-bindings: platform: Add Huawei Matebook E Go EC Pengyu Luo
@ 2025-01-06 7:11 ` Krzysztof Kozlowski
2025-01-06 8:06 ` Pengyu Luo
0 siblings, 1 reply; 15+ messages in thread
From: Krzysztof Kozlowski @ 2025-01-06 7:11 UTC (permalink / raw)
To: Pengyu Luo
Cc: Rob Herring, Krzysztof Kozlowski, Conor Dooley, Bjorn Andersson,
Konrad Dybcio, Hans de Goede, Ilpo Järvinen,
Bryan O'Donoghue, Sebastian Reichel, Heikki Krogerus,
Greg Kroah-Hartman, Jean Delvare, Guenter Roeck, devicetree,
linux-kernel, linux-arm-msm, platform-driver-x86, linux-pm,
linux-usb, linux-hwmon, Dmitry Baryshkov
On Mon, Jan 06, 2025 at 01:41:55AM +0800, Pengyu Luo wrote:
> +maintainers:
> + - Pengyu Luo <mitltlatltl@gmail.com>
> +
> +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:
> + items:
> + - enum:
> + - huawei,gaokun2
> + - huawei,gaokun3
Missing "-ec", because gaokun2/3 is the name of the board, apparently. You cannot
duplicate compatibles with different meanings and if you tested this you
would see errors.
I think I might mislead you during last talk, where I questioned what is
"gen2" etc.
> + - const: huawei,gaokun-ec
There is no support for gaokun2 here, so I assume you checked and you
know these are compatible. What's more, you claim there is a generic
piece of hardware called gaokun-ec and everything in this family will be
compatible with it. Well, that's my standard disclaimer and disapproval
of using generic compatibles.
So in general what you want here is *only one* compatible called
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:
> + - |+
Drop +
Best regards,
Krzysztof
^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH v2 1/5] dt-bindings: platform: Add Huawei Matebook E Go EC
2025-01-06 7:11 ` Krzysztof Kozlowski
@ 2025-01-06 8:06 ` Pengyu Luo
2025-01-06 12:29 ` Krzysztof Kozlowski
0 siblings, 1 reply; 15+ messages in thread
From: Pengyu Luo @ 2025-01-06 8:06 UTC (permalink / raw)
To: krzk
Cc: andersson, bryan.odonoghue, conor+dt, devicetree,
dmitry.baryshkov, gregkh, hdegoede, heikki.krogerus,
ilpo.jarvinen, jdelvare, konradybcio, krzk+dt, linux-arm-msm,
linux-hwmon, linux-kernel, linux-pm, linux-usb, linux,
mitltlatltl, platform-driver-x86, robh, sre
On Mon, Jan 6, 2025 at 3:11 PM Krzysztof Kozlowski <krzk@kernel.org> wrote:
> On Mon, Jan 06, 2025 at 01:41:55AM +0800, Pengyu Luo wrote:
> > +maintainers:
> > + - Pengyu Luo <mitltlatltl@gmail.com>
> > +
> > +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:
> > + items:
> > + - enum:
> > + - huawei,gaokun2
> > + - huawei,gaokun3
>
> Missing "-ec", because gaokun2/3 is the name of the board, apparently. You cannot
> duplicate compatibles with different meanings and if you tested this you
> would see errors.
>
> I think I might mislead you during last talk, where I questioned what is
> "gen2" etc.
>
Agree
> > + - const: huawei,gaokun-ec
>
> There is no support for gaokun2 here, so I assume you checked and you
> know these are compatible. What's more, you claim there is a generic
> piece of hardware called gaokun-ec and everything in this family will be
> compatible with it. Well, that's my standard disclaimer and disapproval
> of using generic compatibles.
>
> So in general what you want here is *only one* compatible called
> huawei,gaokun3-ec
>
I agree with you. If there is a generic rule to follow, I am not familiar
with this. I have seen some bindings, using like this, so I followed it
recently.
properties:
compatible:
items:
- enum:
- vendor0,device0
- vendor1,device1
- const: generic-device
> > +
> > + 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:
> > + - |+
>
> Drop +
>
Agree
Best Wishes,
Pengyu
^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH v2 1/5] dt-bindings: platform: Add Huawei Matebook E Go EC
2025-01-06 8:06 ` Pengyu Luo
@ 2025-01-06 12:29 ` Krzysztof Kozlowski
0 siblings, 0 replies; 15+ messages in thread
From: Krzysztof Kozlowski @ 2025-01-06 12:29 UTC (permalink / raw)
To: Pengyu Luo
Cc: andersson, bryan.odonoghue, conor+dt, devicetree,
dmitry.baryshkov, gregkh, hdegoede, heikki.krogerus,
ilpo.jarvinen, jdelvare, konradybcio, krzk+dt, linux-arm-msm,
linux-hwmon, linux-kernel, linux-pm, linux-usb, linux,
platform-driver-x86, robh, sre
On 06/01/2025 09:06, Pengyu Luo wrote:
>>> + - const: huawei,gaokun-ec
>>
>> There is no support for gaokun2 here, so I assume you checked and you
>> know these are compatible. What's more, you claim there is a generic
>> piece of hardware called gaokun-ec and everything in this family will be
>> compatible with it. Well, that's my standard disclaimer and disapproval
>> of using generic compatibles.
>>
>> So in general what you want here is *only one* compatible called
>> huawei,gaokun3-ec
>>
>
> I agree with you. If there is a generic rule to follow, I am not familiar
> with this. I have seen some bindings, using like this, so I followed it
> recently.
Generic rule is: wildcards and family names are not allowed. Now what
"generic" means, is different for different devices. If unsure, always
use only device-specific compatibles.
Best regards,
Krzysztof
^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH v2 0/5] platform: arm64: Huawei Matebook E Go embedded controller
2025-01-05 17:41 [PATCH v2 0/5] platform: arm64: Huawei Matebook E Go embedded controller Pengyu Luo
` (4 preceding siblings ...)
2025-01-05 17:41 ` [PATCH v2 5/5] arm64: dts: qcom: gaokun3: Add Embedded Controller node Pengyu Luo
@ 2025-01-06 19:27 ` Rob Herring (Arm)
5 siblings, 0 replies; 15+ messages in thread
From: Rob Herring (Arm) @ 2025-01-06 19:27 UTC (permalink / raw)
To: Pengyu Luo
Cc: Konrad Dybcio, Guenter Roeck, linux-arm-msm, Krzysztof Kozlowski,
Ilpo Järvinen, Dmitry Baryshkov, Sebastian Reichel,
Greg Kroah-Hartman, linux-hwmon, linux-kernel,
platform-driver-x86, Conor Dooley, Heikki Krogerus, devicetree,
Bryan O'Donoghue, Hans de Goede, linux-pm, Jean Delvare,
linux-usb, Bjorn Andersson
On Mon, 06 Jan 2025 01:41:54 +0800, Pengyu Luo wrote:
> 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 the source file.
>
> Depends: https://lore.kernel.org/linux-arm-msm/20241220160530.444864-1-mitltlatltl@gmail.com
>
> [1] https://lore.kernel.org/all/20240614-yoga-ec-driver-v7-0-9f0b9b40ae76@linaro.org/
> [2] https://lore.kernel.org/all/20240315-aspire1-ec-v5-0-f93381deff39@trvn.ru/
>
> Signed-off-by: Pengyu Luo <mitltlatltl@gmail.com>
> ---
> Changes in v2:
> - Link to v1: https://lore.kernel.org/linux-arm-msm/20241227171353.404432-1-mitltlatltl@gmail.com
>
> 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 (5):
> dt-bindings: platform: Add Huawei Matebook E Go EC
> platform: arm64: add Huawei Matebook E Go EC driver
> usb: typec: ucsi: add Huawei Matebook E Go ucsi driver
> power: supply: add Huawei Matebook E Go psy driver
> arm64: dts: qcom: gaokun3: Add Embedded Controller node
>
> .../ABI/testing/sysfs-class-power-gaokun | 47 ++
> .../bindings/platform/huawei,gaokun-ec.yaml | 127 +++
> MAINTAINERS | 9 +
> .../boot/dts/qcom/sc8280xp-huawei-gaokun3.dts | 139 ++++
> drivers/platform/arm64/Kconfig | 21 +
> drivers/platform/arm64/Makefile | 1 +
> drivers/platform/arm64/huawei-gaokun-ec.c | 767 ++++++++++++++++++
> drivers/power/supply/Kconfig | 10 +
> drivers/power/supply/Makefile | 1 +
> drivers/power/supply/huawei-gaokun-battery.c | 548 +++++++++++++
> drivers/usb/typec/ucsi/Kconfig | 10 +
> drivers/usb/typec/ucsi/Makefile | 1 +
> drivers/usb/typec/ucsi/ucsi_huawei_gaokun.c | 524 ++++++++++++
> .../linux/platform_data/huawei-gaokun-ec.h | 79 ++
> 14 files changed, 2284 insertions(+)
> create mode 100644 Documentation/ABI/testing/sysfs-class-power-gaokun
> create mode 100644 Documentation/devicetree/bindings/platform/huawei,gaokun-ec.yaml
> create mode 100644 drivers/platform/arm64/huawei-gaokun-ec.c
> create mode 100644 drivers/power/supply/huawei-gaokun-battery.c
> create mode 100644 drivers/usb/typec/ucsi/ucsi_huawei_gaokun.c
> create mode 100644 include/linux/platform_data/huawei-gaokun-ec.h
>
> --
> 2.47.1
>
>
>
My bot found new DTB warnings on the .dts files added or changed in this
series.
Some warnings may be from an existing SoC .dtsi. Or perhaps the warnings
are fixed by another series. Ultimately, it is up to the platform
maintainer whether these warnings are acceptable or not. No need to reply
unless the platform maintainer has comments.
If you already ran DT checks and didn't see these error(s), then
make sure dt-schema is up to date:
pip3 install dtschema --upgrade
New warnings running 'make CHECK_DTBS=y qcom/sc8280xp-huawei-gaokun3.dtb' for 20250105174159.227831-1-mitltlatltl@gmail.com:
Warning: Duplicate compatible "huawei,gaokun3" found in schemas matching "$id":
http://devicetree.org/schemas/platform/huawei,gaokun-ec.yaml#
http://devicetree.org/schemas/arm/qcom.yaml#
arch/arm64/boot/dts/qcom/sc8280xp-huawei-gaokun3.dtb: embedded-controller@38: $nodename:0: '/' was expected
from schema $id: http://devicetree.org/schemas/arm/qcom.yaml#
arch/arm64/boot/dts/qcom/sc8280xp-huawei-gaokun3.dtb: embedded-controller@38: compatible: 'oneOf' conditional failed, one must be fixed:
['huawei,gaokun3', 'huawei,gaokun-ec'] is too short
'huawei,gaokun3' is not one of ['qcom,apq8016-sbc', 'schneider,apq8016-hmibsc']
'huawei,gaokun3' is not one of ['asus,sparrow', 'huawei,sturgeon', 'lg,lenok', 'samsung,matisse-wifi', 'samsung,milletwifi']
'huawei,gaokun3' is not one of ['asus,nexus7-flo', 'lg,nexus4-mako', 'sony,xperia-yuga', 'qcom,apq8064-cm-qs600', 'qcom,apq8064-ifc6410']
'huawei,gaokun3' is not one of ['qcom,apq8074-dragonboard']
'huawei,gaokun3' is not one of ['qcom,apq8060-dragonboard', 'qcom,msm8660-surf']
'huawei,gaokun3' is not one of ['qcom,apq8084-mtp', 'qcom,apq8084-sbc']
'huawei,gaokun3' is not one of ['microsoft,dempsey', 'microsoft,makepeace', 'microsoft,moneypenny', 'motorola,falcon', 'samsung,ms013g', 'samsung,s3ve3g']
'huawei,gaokun3' is not one of ['htc,memul', 'microsoft,superman-lte', 'microsoft,tesla', 'motorola,peregrine', 'samsung,matisselte']
'huawei,gaokun3' is not one of ['wingtech,wt82918hd']
'huawei,gaokun3' is not one of ['huawei,kiwi', 'longcheer,l9100', 'samsung,a7', 'sony,kanuti-tulip', 'square,apq8039-t2', 'wingtech,wt82918', 'wingtech,wt82918hdhw39']
'huawei,gaokun3' is not one of ['sony,kugo-row', 'sony,suzu-row']
'huawei,gaokun3' is not one of ['qcom,msm8960-cdp', 'samsung,expressatt']
'huawei,gaokun3' is not one of ['lge,hammerhead', 'samsung,hlte', 'sony,xperia-amami', 'sony,xperia-honami']
'huawei,gaokun3' is not one of ['fairphone,fp2', 'htc,m8', 'oneplus,bacon', 'samsung,klte', 'sony,xperia-aries', 'sony,xperia-castor', 'sony,xperia-leo']
'huawei,gaokun3' is not one of ['samsung,kltechn']
'huawei,gaokun3' is not one of ['acer,a1-724', 'alcatel,idol347', 'asus,z00l', 'gplus,fl8005a', 'huawei,g7', 'lg,c50', 'lg,m216', 'longcheer,l8910', 'longcheer,l8150', 'motorola,harpia', 'motorola,osprey', 'motorola,surnia', 'qcom,msm8916-mtp', 'samsung,a3u-eur', 'samsung,a5u-eur', 'samsung,e5', 'samsung,e7', 'samsung,fortuna3g', 'samsung,gprimeltecan', 'samsung,grandmax', 'samsung,grandprimelte', 'samsung,gt510', 'samsung,gt58', 'samsung,j3ltetw', 'samsung,j5', 'samsung,j5x', 'samsung,rossa', 'samsung,serranove', 'thwc,uf896', 'thwc,ufi001c', 'wingtech,wt86518', 'wingtech,wt86528', 'wingtech,wt88047', 'yiming,uz801-v3']
'huawei,gaokun3' is not one of ['xiaomi,riva']
'huawei,gaokun3' is not one of ['motorola,potter', 'xiaomi,daisy', 'xiaomi,mido', 'xiaomi,tissot', 'xiaomi,vince']
'huawei,gaokun3' is not one of ['lg,bullhead', 'lg,h815', 'microsoft,talkman', 'xiaomi,libra']
'huawei,gaokun3' is not one of ['sony,karin_windy']
'huawei,gaokun3' is not one of ['huawei,angler', 'microsoft,cityman', 'sony,ivy-row', 'sony,karin-row', 'sony,satsuki-row', 'sony,sumire-row', 'sony,suzuran-row']
'huawei,gaokun3' is not one of ['arrow,apq8096-db820c', 'inforce,ifc6640']
'huawei,gaokun3' is not one of ['oneplus,oneplus3', 'oneplus,oneplus3t', 'qcom,msm8996-mtp', 'sony,dora-row', 'sony,kagura-row', 'sony,keyaki-row', 'xiaomi,gemini']
'huawei,gaokun3' is not one of ['xiaomi,natrium', 'xiaomi,scorpio']
'huawei,gaokun3' is not one of ['asus,novago-tp370ql', 'fxtec,pro1', 'hp,envy-x2', 'lenovo,miix-630', 'oneplus,cheeseburger', 'oneplus,dumpling', 'qcom,msm8998-mtp', 'sony,xperia-lilac', 'sony,xperia-maple', 'sony,xperia-poplar', 'xiaomi,sagit']
'huawei,gaokun3' is not one of ['8dev,jalapeno', 'alfa-network,ap120c-ac']
'huawei,gaokun3' is not one of ['qcom,ipq4019-ap-dk01.1-c1', 'qcom,ipq4019-ap-dk04.1-c3', 'qcom,ipq4019-ap-dk07.1-c1', 'qcom,ipq4019-ap-dk07.1-c2', 'qcom,ipq4019-dk04.1-c1']
'huawei,gaokun3' is not one of ['qcom,ipq5018-rdp432-c2', 'tplink,archer-ax55-v1']
'huawei,gaokun3' is not one of ['qcom,ipq5332-ap-mi01.2', 'qcom,ipq5332-ap-mi01.3', 'qcom,ipq5332-ap-mi01.6', 'qcom,ipq5332-ap-mi01.9']
'huawei,gaokun3' is not one of ['qcom,ipq5424-rdp466']
'huawei,gaokun3' is not one of ['mikrotik,rb3011', 'qcom,ipq8064-ap148']
'huawei,gaokun3' is not one of ['qcom,ipq8074-hk01', 'qcom,ipq8074-hk10-c1', 'qcom,ipq8074-hk10-c2']
'huawei,gaokun3' is not one of ['qcom,ipq9574-ap-al02-c2', 'qcom,ipq9574-ap-al02-c6', 'qcom,ipq9574-ap-al02-c7', 'qcom,ipq9574-ap-al02-c8', 'qcom,ipq9574-ap-al02-c9']
'swir,mangoh-green-wp8548' was expected
'huawei,gaokun3' is not one of ['qcom,qrb2210-rb1']
'huawei,gaokun3' is not one of ['fairphone,fp5', 'qcom,qcm6490-idp', 'qcom,qcs6490-rb3gen2', 'shift,otter']
'huawei,gaokun3' is not one of ['qcom,qdu1000-idp', 'qcom,qdu1000-x100']
'huawei,gaokun3' is not one of ['qcom,qru1000-idp']
'huawei,gaokun3' is not one of ['qcom,qar2130p']
'huawei,gaokun3' is not one of ['acer,aspire1', 'qcom,sc7180-idp']
'google,coachz-rev1' was expected
'google,coachz' was expected
'google,coachz-rev1-sku0' was expected
'google,coachz-sku0' was expected
'google,homestar-rev2' was expected
'google,homestar-rev3' was expected
'google,homestar' was expected
'google,kingoftown-rev0' was expected
'google,kingoftown' was expected
'google,lazor-rev0' was expected
'google,lazor-rev1' was expected
'google,lazor-rev3' was expected
'google,lazor-rev9' was expected
'google,lazor' was expected
'google,lazor-rev1-sku2' was expected
'google,lazor-rev3-sku2' was expected
'google,lazor-rev9-sku2' was expected
'google,lazor-sku2' was expected
'google,lazor-rev1-sku0' was expected
'google,lazor-rev3-sku0' was expected
'google,lazor-rev9-sku0' was expected
'google,lazor-sku0' was expected
'google,lazor-rev4-sku4' was expected
'google,lazor-rev9-sku4' was expected
'google,lazor-sku4' was expected
'google,lazor-rev4-sku5' was expected
'google,lazor-rev5-sku5' was expected
'google,lazor-rev9-sku6' was expected
'google,lazor-sku6' was expected
'google,mrbland-rev0-sku0' was expected
'google,mrbland-sku1536' was expected
'google,mrbland-rev0-sku16' was expected
'google,mrbland-sku1024' was expected
'google,pazquel-sku5' was expected
'google,pazquel-sku1' was expected
'google,pazquel-sku6' was expected
'google,pazquel-sku0' was expected
'google,pazquel-sku22' was expected
'google,pazquel-sku21' was expected
'google,pompom-rev1' was expected
'google,pompom-rev2' was expected
'google,pompom' was expected
'google,pompom-rev1-sku0' was expected
'google,pompom-rev2-sku0' was expected
'google,pompom-sku0' was expected
'google,quackingstick-sku1537' was expected
'google,quackingstick-sku1536' was expected
'google,trogdor' was expected
'google,trogdor-sku0' was expected
'google,wormdingler-rev0-sku16' was expected
'google,wormdingler-sku1024' was expected
'google,wormdingler-sku1025' was expected
'google,wormdingler-rev0-sku0' was expected
'google,wormdingler-sku0' was expected
'google,wormdingler-sku1' was expected
'qcom,sc7280-crd' was expected
'google,zoglin' was expected
'google,zoglin-sku1536' was expected
'qcom,sc7280-idp' was expected
'qcom,sc7280-idp2' was expected
'google,evoker' was expected
'google,evoker-sku512' was expected
'google,herobrine' was expected
'google,villager-rev0' was expected
'google,villager' was expected
'google,villager-sku512' was expected
'google,zombie' was expected
'google,zombie-sku512' was expected
'google,zombie-sku2' was expected
'google,zombie-sku514' was expected
'huawei,gaokun3' is not one of ['lenovo,flex-5g', 'microsoft,surface-prox', 'qcom,sc8180x-primus']
'huawei,gaokun3' is not one of ['lenovo,tbx605f', 'motorola,ali']
'huawei,gaokun3' is not one of ['sony,discovery-row', 'sony,kirin-row', 'sony,pioneer-row', 'sony,voyager-row']
'huawei,gaokun3' is not one of ['inforce,ifc6560']
'huawei,gaokun3' is not one of ['fairphone,fp3', 'motorola,ocean']
'huawei,gaokun3' is not one of ['sony,mermaid-row']
'huawei,gaokun3' is not one of ['xiaomi,lavender']
'huawei,gaokun3' is not one of ['google,sargo']
'huawei,gaokun3' is not one of ['qcom,sdx55-mtp', 'qcom,sdx55-telit-fn980-tlb', 'qcom,sdx55-t55']
'huawei,gaokun3' is not one of ['qcom,sdx65-mtp']
'huawei,gaokun3' is not one of ['qcom,sdx75-idp']
'huawei,gaokun3' is not one of ['qcom,ipq6018-cp01', 'qcom,ipq6018-cp01-c1']
'huawei,gaokun3' is not one of ['qcom,qcs404-evb-1000', 'qcom,qcs404-evb-4000']
'huawei,gaokun3' is not one of ['qcom,qcs8300-ride']
'huawei,gaokun3' is not one of ['qcom,qcs615-ride']
'huawei,gaokun3' is not one of ['qcom,sa8155p-adp']
'huawei,gaokun3' is not one of ['qcom,sa8295p-adp', 'qcom,sa8540p-ride']
'huawei,gaokun3' is not one of ['qcom,sa8775p-ride', 'qcom,sa8775p-ride-r3']
'huawei,gaokun3' is not one of ['qcom,qcs9100-ride', 'qcom,qcs9100-ride-r3']
'huawei,gaokun3' is not one of ['google,cheza', 'google,cheza-rev1', 'google,cheza-rev2', 'lenovo,yoga-c630', 'lg,judyln', 'lg,judyp', 'oneplus,enchilada', 'oneplus,fajita', 'qcom,sdm845-mtp', 'shift,axolotl', 'samsung,starqltechn', 'samsung,w737', 'sony,akari-row', 'sony,akatsuki-row', 'sony,apollo-row', 'thundercomm,db845c', 'xiaomi,beryllium', 'xiaomi,beryllium-ebbg', 'xiaomi,polaris']
'huawei,gaokun3' is not one of ['oneplus,billie2']
'huawei,gaokun3' is not one of ['qcom,qrb4210-rb2']
'huawei,gaokun3' is not one of ['qcom,sm4450-qrd']
'huawei,gaokun3' is not one of ['fxtec,pro1x']
'huawei,gaokun3' is not one of ['lenovo,j606f']
'huawei,gaokun3' is not one of ['sony,pdx201', 'xiaomi,laurel-sprout']
'huawei,gaokun3' is not one of ['sony,pdx213']
'huawei,gaokun3' is not one of ['sony,pdx225']
'huawei,gaokun3' is not one of ['xiaomi,curtana', 'xiaomi,joyeuse']
'huawei,gaokun3' is not one of ['fairphone,fp4']
'huawei,gaokun3' is not one of ['nothing,spacewar']
'huawei,gaokun3' is not one of ['microsoft,surface-duo', 'qcom,sm8150-hdk', 'qcom,sm8150-mtp', 'sony,bahamut-generic', 'sony,griffin-generic']
'huawei,gaokun3' is not one of ['qcom,qrb5165-rb5', 'qcom,sm8250-hdk', 'qcom,sm8250-mtp', 'sony,pdx203-generic', 'sony,pdx206-generic', 'xiaomi,elish', 'xiaomi,pipa']
'huawei,gaokun3' is not one of ['microsoft,surface-duo2', 'qcom,sm8350-hdk', 'qcom,sm8350-mtp', 'sony,pdx214-generic', 'sony,pdx215-generic']
'huawei,gaokun3' is not one of ['qcom,sm8450-hdk', 'qcom,sm8450-qrd', 'sony,pdx223', 'sony,pdx224']
'huawei,gaokun3' is not one of ['qcom,sm8550-hdk', 'qcom,sm8550-mtp', 'qcom,sm8550-qrd', 'samsung,q5q', 'sony,pdx234']
'huawei,gaokun3' is not one of ['qcom,qcs8550-aim300-aiot']
'huawei,gaokun3' is not one of ['qcom,sm8650-hdk', 'qcom,sm8650-mtp', 'qcom,sm8650-qrd']
'huawei,gaokun3' is not one of ['qcom,x1e001de-devkit']
'huawei,gaokun3' is not one of ['lenovo,thinkpad-t14s']
'huawei,gaokun3' is not one of ['asus,vivobook-s15', 'dell,xps13-9345', 'hp,omnibook-x14', 'lenovo,yoga-slim7x', 'microsoft,romulus13', 'microsoft,romulus15', 'qcom,x1e80100-crd', 'qcom,x1e80100-qcp']
'huawei,gaokun3' is not one of ['qcom,x1p42100-crd']
'qcom,apq8016' was expected
'qcom,apq8026' was expected
'qcom,apq8064' was expected
'qcom,apq8074' was expected
'qcom,msm8660' was expected
'qcom,apq8084' was expected
'qcom,msm8226' was expected
'qcom,msm8926' was expected
'qcom,msm8929' was expected
'qcom,msm8939' was expected
'qcom,msm8956' was expected
'qcom,msm8960' was expected
'qcom,msm8974' was expected
'qcom,msm8974pro' was expected
'samsung,klte' was expected
'qcom,msm8916' was expected
'qcom,msm8917' was expected
'qcom,msm8953' was expected
'qcom,msm8992' was expected
'qcom,apq8094' was expected
'qcom,msm8994' was expected
'qcom,apq8096-sbc' was expected
'qcom,msm8996' was expected
'qcom,msm8996pro' was expected
'qcom,msm8998' was expected
'qcom,ipq4018' was expected
'qcom,ipq4019' was expected
'qcom,ipq5018' was expected
'qcom,ipq5332' was expected
'qcom,ipq5424' was expected
'qcom,ipq8064' was expected
'qcom,ipq8074' was expected
'qcom,ipq9574' was expected
'swir,wp8548' was expected
'qcom,qrb2210' was expected
'qcom,qcm6490' was expected
'qcom,qdu1000' was expected
'qcom,qru1000' was expected
'qcom,sar2130p' was expected
'qcom,sc7180' was expected
'google,coachz-rev2' was expected
'google,coachz-rev2-sku0' was expected
'google,homestar-rev23' was expected
'google,lazor-rev2' was expected
'google,lazor-rev4' was expected
'google,lazor-rev2-sku2' was expected
'google,lazor-rev4-sku2' was expected
'google,lazor-rev2-sku0' was expected
'google,lazor-rev4-sku0' was expected
'google,lazor-rev9-sku10' was expected
'google,lazor-sku10' was expected
'google,lazor-rev5-sku4' was expected
'google,lazor-rev9-sku15' was expected
'google,lazor-sku15' was expected
'google,lazor-rev5-sku6' was expected
'google,lazor-rev9-sku18' was expected
'google,lazor-sku18' was expected
'google,mrbland-sku768' was expected
'google,pazquel-sku4' was expected
'google,pazquel-sku2' was expected
'google,pazquel-sku20' was expected
'google,hoglin-rev3' was expected
'google,hoglin' was expected
'google,hoglin-sku1536' was expected
'google,senor' was expected
'google,piglin' was expected
'qcom,sc7280' was expected
'google,zombie-sku3' was expected
'qcom,sc8180x' was expected
'qcom,sc8280xp' was expected
'qcom,sdm450' was expected
'qcom,sdm630' was expected
'qcom,sda660' was expected
'qcom,sdm632' was expected
'qcom,sdm636' was expected
'qcom,sdm660' was expected
'qcom,sdm670' was expected
'qcom,sdx55' was expected
'qcom,sdx65' was expected
'qcom,sdx75' was expected
'qcom,ipq6018' was expected
'qcom,qcs404-evb' was expected
'qcom,qcs8300' was expected
'qcom,qcs615' was expected
'qcom,sa8155p' was expected
'qcom,sa8540p' was expected
'qcom,sa8775p' was expected
'qcom,qcs9100' was expected
'qcom,sdm845' was expected
'qcom,sm4250' was expected
'qcom,qrb4210' was expected
'qcom,sm4450' was expected
'qcom,sm6115' was expected
'qcom,sm6115p' was expected
'qcom,sm6125' was expected
'qcom,sm6350' was expected
'qcom,sm6375' was expected
'qcom,sm7125' was expected
'qcom,sm7225' was expected
'qcom,sm7325' was expected
'qcom,sm8150' was expected
'qcom,sm8250' was expected
'qcom,sm8350' was expected
'qcom,sm8450' was expected
'qcom,sm8550' was expected
'qcom,qcs8550-aim300' was expected
'qcom,sm8650' was expected
'qcom,x1e001de' was expected
'qcom,x1e78100' was expected
'qcom,x1e80100' was expected
'qcom,x1p42100' was expected
from schema $id: http://devicetree.org/schemas/arm/qcom.yaml#
^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH v2 2/5] platform: arm64: add Huawei Matebook E Go EC driver
2025-01-05 17:41 ` [PATCH v2 2/5] platform: arm64: add Huawei Matebook E Go EC driver Pengyu Luo
@ 2025-01-07 12:25 ` Heikki Krogerus
2025-01-07 16:53 ` Pengyu Luo
0 siblings, 1 reply; 15+ messages in thread
From: Heikki Krogerus @ 2025-01-07 12:25 UTC (permalink / raw)
To: Pengyu Luo
Cc: Rob Herring, Krzysztof Kozlowski, Conor Dooley, Bjorn Andersson,
Konrad Dybcio, Hans de Goede, Ilpo Järvinen,
Bryan O'Donoghue, Sebastian Reichel, Greg Kroah-Hartman,
Jean Delvare, Guenter Roeck, devicetree, linux-kernel,
linux-arm-msm, platform-driver-x86, linux-pm, linux-usb,
linux-hwmon, Dmitry Baryshkov
Hi,
> +/* -------------------------------------------------------------------------- */
> +/* API For UCSI */
> +
> +int gaokun_ec_ucsi_read(struct gaokun_ec *ec,
> + u8 resp[GAOKUN_UCSI_READ_SIZE])
> +{
> + u8 req[] = MKREQ(0x03, 0xD5, 0);
> + u8 _resp[] = MKRESP(GAOKUN_UCSI_READ_SIZE);
> + int ret;
> +
> + ret = gaokun_ec_read(ec, req, sizeof(_resp), _resp);
> + if (ret)
> + return ret;
> +
> + extr_resp(resp, _resp, GAOKUN_UCSI_READ_SIZE);
> + return 0;
> +}
> +EXPORT_SYMBOL_GPL(gaokun_ec_ucsi_read);
> +
> +int gaokun_ec_ucsi_write(struct gaokun_ec *ec,
> + const u8 req[GAOKUN_UCSI_WRITE_SIZE])
> +{
> + u8 _req[] = MKREQ(0x03, 0xD4, GAOKUN_UCSI_WRITE_SIZE);
> +
> +
> + refill_req(_req, req, GAOKUN_UCSI_WRITE_SIZE);
> +
> + return gaokun_ec_write(ec, _req);
> +}
> +EXPORT_SYMBOL_GPL(gaokun_ec_ucsi_write);
> +
> +int gaokun_ec_ucsi_get_reg(struct gaokun_ec *ec, u8 *ureg)
> +{
> + u8 req[] = MKREQ(0x03, 0xD3, 0);
> + u8 _resp[] = MKRESP(UCSI_REG_SIZE);
> + int ret;
> +
> + ret = gaokun_ec_read(ec, req, sizeof(_resp), _resp);
> + if (ret)
> + return ret;
> +
> + extr_resp(ureg, _resp, UCSI_REG_SIZE);
> +
> + return 0;
> +}
> +EXPORT_SYMBOL_GPL(gaokun_ec_ucsi_get_reg);
Why not just take struct gaokun_ucsi_reg as parameter? I did not see
this (or any of these) being used anywhere else except in your UCSI
glue driver. So the prototype would be:
int gaokun_ec_ucsi_get_reg(struct gaokun_ec *ec,
struct gaokun_ucsi_reg *reg);
> +int gaokun_ec_ucsi_pan_ack(struct gaokun_ec *ec, int port_id)
> +{
> + u8 req[] = MKREQ(0x03, 0xD2, 1);
> + u8 data = 1 << port_id;
> +
> + if (port_id == GAOKUN_UCSI_NO_PORT_UPDATE)
> + data = 0;
> +
> + refill_req(req, &data, 1);
> +
> + return gaokun_ec_write(ec, req);
> +}
> +EXPORT_SYMBOL_GPL(gaokun_ec_ucsi_pan_ack);
I think you should add proper kernel doc comments to these exported
functions.
thanks,
--
heikki
^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH v2 3/5] usb: typec: ucsi: add Huawei Matebook E Go ucsi driver
2025-01-05 17:41 ` [PATCH v2 3/5] usb: typec: ucsi: add Huawei Matebook E Go ucsi driver Pengyu Luo
@ 2025-01-07 12:30 ` Heikki Krogerus
2025-01-07 16:54 ` Pengyu Luo
0 siblings, 1 reply; 15+ messages in thread
From: Heikki Krogerus @ 2025-01-07 12:30 UTC (permalink / raw)
To: Pengyu Luo
Cc: Rob Herring, Krzysztof Kozlowski, Conor Dooley, Bjorn Andersson,
Konrad Dybcio, Hans de Goede, Ilpo Järvinen,
Bryan O'Donoghue, Sebastian Reichel, Greg Kroah-Hartman,
Jean Delvare, Guenter Roeck, devicetree, linux-kernel,
linux-arm-msm, platform-driver-x86, linux-pm, linux-usb,
linux-hwmon, Dmitry Baryshkov
Hi,
On Mon, Jan 06, 2025 at 01:41:57AM +0800, Pengyu Luo wrote:
> The Huawei Matebook E Go tablet implements the UCSI interface in the
> onboard EC. Add the glue driver to interface with the platform's UCSI
> implementation.
>
> Signed-off-by: Pengyu Luo <mitltlatltl@gmail.com>
> ---
> drivers/usb/typec/ucsi/Kconfig | 10 +
> drivers/usb/typec/ucsi/Makefile | 1 +
> drivers/usb/typec/ucsi/ucsi_huawei_gaokun.c | 524 ++++++++++++++++++++
> 3 files changed, 535 insertions(+)
> create mode 100644 drivers/usb/typec/ucsi/ucsi_huawei_gaokun.c
This did not apply cleanly anymore, but I guess you'll be rebasing in
any case. A few nitpicks below.
> diff --git a/drivers/usb/typec/ucsi/Kconfig b/drivers/usb/typec/ucsi/Kconfig
> index 680e1b87b..8a73df84a 100644
> --- a/drivers/usb/typec/ucsi/Kconfig
> +++ b/drivers/usb/typec/ucsi/Kconfig
> @@ -78,4 +78,14 @@ config UCSI_LENOVO_YOGA_C630
> To compile the driver as a module, choose M here: the module will be
> called ucsi_yoga_c630.
>
> +config UCSI_HUAWEI_GAOKUN
> + tristate "UCSI Interface Driver for Huawei Matebook E Go"
> + depends on EC_HUAWEI_GAOKUN
> + help
> + This driver enables UCSI support on the Huawei Matebook E Go tablet,
> + which is a sc8280xp-based 2-in-1 tablet.
> +
> + To compile the driver as a module, choose M here: the module will be
> + called ucsi_huawei_gaokun.
> +
> endif
> diff --git a/drivers/usb/typec/ucsi/Makefile b/drivers/usb/typec/ucsi/Makefile
> index aed41d238..0b400122b 100644
> --- a/drivers/usb/typec/ucsi/Makefile
> +++ b/drivers/usb/typec/ucsi/Makefile
> @@ -22,3 +22,4 @@ obj-$(CONFIG_UCSI_CCG) += ucsi_ccg.o
> obj-$(CONFIG_UCSI_STM32G0) += ucsi_stm32g0.o
> obj-$(CONFIG_UCSI_PMIC_GLINK) += ucsi_glink.o
> obj-$(CONFIG_UCSI_LENOVO_YOGA_C630) += ucsi_yoga_c630.o
> +obj-$(CONFIG_UCSI_HUAWEI_GAOKUN) += ucsi_huawei_gaokun.o
> diff --git a/drivers/usb/typec/ucsi/ucsi_huawei_gaokun.c b/drivers/usb/typec/ucsi/ucsi_huawei_gaokun.c
> new file mode 100644
> index 000000000..044545b44
> --- /dev/null
> +++ b/drivers/usb/typec/ucsi/ucsi_huawei_gaokun.c
> @@ -0,0 +1,524 @@
> +// SPDX-License-Identifier: GPL-2.0-only
> +/*
> + * ucsi-huawei-gaokun - A UCSI driver for HUAWEI Matebook E Go
> + *
> + * reference: drivers/usb/typec/ucsi/ucsi_yoga_c630.c
> + * drivers/usb/typec/ucsi/ucsi_glink.c
> + * drivers/soc/qcom/pmic_glink_altmode.c
> + *
> + * Copyright (C) 2024 Pengyu Luo <mitltlatltl@gmail.com>
> + */
> +
> +#include <drm/bridge/aux-bridge.h>
> +#include <linux/auxiliary_bus.h>
> +#include <linux/bitops.h>
> +#include <linux/completion.h>
> +#include <linux/container_of.h>
> +#include <linux/module.h>
> +#include <linux/notifier.h>
> +#include <linux/of.h>
> +#include <linux/platform_data/huawei-gaokun-ec.h>
> +#include <linux/string.h>
> +#include <linux/usb/pd_vdo.h>
> +#include <linux/usb/typec_altmode.h>
> +#include <linux/usb/typec_dp.h>
> +#include <linux/workqueue_types.h>
> +
> +#include "ucsi.h"
> +
> +#define EC_EVENT_UCSI 0x21
> +#define EC_EVENT_USB 0x22
> +
> +#define GAOKUN_CCX_MASK GENMASK(1, 0)
> +#define GAOKUN_MUX_MASK GENMASK(3, 2)
> +
> +#define GAOKUN_DPAM_MASK GENMASK(3, 0)
> +#define GAOKUN_HPD_STATE_MASK BIT(4)
> +#define GAOKUN_HPD_IRQ_MASK BIT(5)
> +
> +#define GET_IDX(updt) (ffs(updt) - 1)
> +
> +#define CCX_TO_ORI(ccx) (++ccx % 3) /* convert ccx to enum typec_orientation */
> +
> +/* Configuration Channel Extension */
> +enum gaokun_ucsi_ccx {
> + USBC_CCX_NORMAL,
> + USBC_CCX_REVERSE,
> + USBC_CCX_NONE,
> +};
> +
> +enum gaokun_ucsi_mux {
> + USBC_MUX_NONE,
> + USBC_MUX_USB_2L,
> + USBC_MUX_DP_4L,
> + USBC_MUX_USB_DP,
> +};
> +/* based on pmic_glink_altmode_pin_assignment */
> +enum gaokun_ucsi_dpam_pan { /* DP Alt Mode Pin Assignments */
> + USBC_DPAM_PAN_NONE,
> + USBC_DPAM_PAN_A, /* Not supported after USB Type-C Standard v1.0b */
> + USBC_DPAM_PAN_B, /* Not supported after USB Type-C Standard v1.0b */
> + USBC_DPAM_PAN_C, /* USBC_DPAM_PAN_C_REVERSE - 6 */
> + USBC_DPAM_PAN_D,
> + USBC_DPAM_PAN_E,
> + USBC_DPAM_PAN_F, /* Not supported after USB Type-C Standard v1.0b */
> + USBC_DPAM_PAN_A_REVERSE,/* Not supported after USB Type-C Standard v1.0b */
> + USBC_DPAM_PAN_B_REVERSE,/* Not supported after USB Type-C Standard v1.0b */
> + USBC_DPAM_PAN_C_REVERSE,
> + USBC_DPAM_PAN_D_REVERSE,
> + USBC_DPAM_PAN_E_REVERSE,
> + USBC_DPAM_PAN_F_REVERSE,/* Not supported after USB Type-C Standard v1.0b */
> +};
> +
> +struct gaokun_ucsi_reg {
> + u8 port_num;
> + u8 port_updt;
> + u8 port_data[4];
> + u8 checksum;
> + u8 reserved;
> +} __packed;
> +
> +struct gaokun_ucsi_port {
> + struct completion usb_ack;
> + spinlock_t lock;
> +
> + struct gaokun_ucsi *ucsi;
> + struct auxiliary_device *bridge;
> +
> + int idx;
> + enum gaokun_ucsi_ccx ccx;
> + enum gaokun_ucsi_mux mux;
> + u8 mode;
> + u16 svid;
> + u8 hpd_state;
> + u8 hpd_irq;
> +};
> +
> +struct gaokun_ucsi {
> + struct gaokun_ec *ec;
> + struct ucsi *ucsi;
> + struct gaokun_ucsi_port *ports;
> + struct device *dev;
> + struct delayed_work work;
> + struct notifier_block nb;
> + u16 version;
> + u8 port_num;
> +};
> +
> +/* -------------------------------------------------------------------------- */
> +/* For UCSI */
> +
> +static int gaokun_ucsi_read_version(struct ucsi *ucsi, u16 *version)
> +{
> + struct gaokun_ucsi *uec = ucsi_get_drvdata(ucsi);
> +
> + *version = uec->version;
> +
> + return 0;
> +}
> +
> +static int gaokun_ucsi_read_cci(struct ucsi *ucsi, u32 *cci)
> +{
> + struct gaokun_ucsi *uec = ucsi_get_drvdata(ucsi);
> + u8 buf[GAOKUN_UCSI_READ_SIZE];
> + int ret;
> +
> + ret = gaokun_ec_ucsi_read(uec->ec, buf);
> + if (ret)
> + return ret;
> +
> + memcpy(cci, buf, sizeof(*cci));
> +
> + return 0;
> +}
> +
> +static int gaokun_ucsi_read_message_in(struct ucsi *ucsi,
> + void *val, size_t val_len)
> +{
> + struct gaokun_ucsi *uec = ucsi_get_drvdata(ucsi);
> + u8 buf[GAOKUN_UCSI_READ_SIZE];
> + int ret;
> +
> + ret = gaokun_ec_ucsi_read(uec->ec, buf);
> + if (ret)
> + return ret;
> +
> + memcpy(val, buf + GAOKUN_UCSI_CCI_SIZE,
> + min(val_len, GAOKUN_UCSI_DATA_SIZE));
> +
> + return 0;
> +}
> +
> +static int gaokun_ucsi_async_control(struct ucsi *ucsi, u64 command)
> +{
> + struct gaokun_ucsi *uec = ucsi_get_drvdata(ucsi);
> + u8 buf[GAOKUN_UCSI_WRITE_SIZE] = {};
> +
> + memcpy(buf, &command, sizeof(command));
> +
> + return gaokun_ec_ucsi_write(uec->ec, buf);
> +}
> +
> +static void gaokun_ucsi_update_connector(struct ucsi_connector *con)
> +{
> + struct gaokun_ucsi *uec = ucsi_get_drvdata(con->ucsi);
> +
> + if (con->num > uec->port_num)
> + return;
> +
> + con->typec_cap.orientation_aware = true;
> +}
> +
> +static void gaokun_set_orientation(struct ucsi_connector *con,
> + struct gaokun_ucsi_port *port)
> +{
> + enum gaokun_ucsi_ccx ccx;
> + unsigned long flags;
> +
> + spin_lock_irqsave(&port->lock, flags);
> + ccx = port->ccx;
> + spin_unlock_irqrestore(&port->lock, flags);
> +
> + typec_set_orientation(con->port, CCX_TO_ORI(ccx));
> +}
> +
> +static void gaokun_ucsi_connector_status(struct ucsi_connector *con)
> +{
> + struct gaokun_ucsi *uec = ucsi_get_drvdata(con->ucsi);
> + int idx;
> +
> + idx = con->num - 1;
> + if (con->num > uec->port_num) {
> + dev_warn(uec->dev, "set orientation out of range: con%d\n", idx);
> + return;
> + }
> +
> + gaokun_set_orientation(con, &uec->ports[idx]);
> +}
> +
> +const struct ucsi_operations gaokun_ucsi_ops = {
> + .read_version = gaokun_ucsi_read_version,
> + .read_cci = gaokun_ucsi_read_cci,
> + .read_message_in = gaokun_ucsi_read_message_in,
> + .sync_control = ucsi_sync_control_common,
> + .async_control = gaokun_ucsi_async_control,
> + .update_connector = gaokun_ucsi_update_connector,
> + .connector_status = gaokun_ucsi_connector_status,
> +};
> +
> +/* -------------------------------------------------------------------------- */
> +/* For Altmode */
> +
> +static void gaokun_ucsi_port_update(struct gaokun_ucsi_port *port,
> + const u8 *port_data)
> +{
> + struct gaokun_ucsi *uec = port->ucsi;
> + int offset = port->idx * 2; /* every port has 2 Bytes data */
> + unsigned long flags;
> + u8 dcc, ddi;
> +
> + dcc = port_data[offset];
> + ddi = port_data[offset + 1];
> +
> + spin_lock_irqsave(&port->lock, flags);
> +
> + port->ccx = FIELD_GET(GAOKUN_CCX_MASK, dcc);
> + port->mux = FIELD_GET(GAOKUN_MUX_MASK, dcc);
> + port->mode = FIELD_GET(GAOKUN_DPAM_MASK, ddi);
> + port->hpd_state = FIELD_GET(GAOKUN_HPD_STATE_MASK, ddi);
> + port->hpd_irq = FIELD_GET(GAOKUN_HPD_IRQ_MASK, ddi);
> +
> + /* Mode and SVID are unused; keeping them to make things clearer */
> + switch (port->mode) {
> + case USBC_DPAM_PAN_C:
> + case USBC_DPAM_PAN_C_REVERSE:
> + port->mode = DP_PIN_ASSIGN_C; /* correct it for usb later */
> + break;
> + case USBC_DPAM_PAN_D:
> + case USBC_DPAM_PAN_D_REVERSE:
> + port->mode = DP_PIN_ASSIGN_D;
> + break;
> + case USBC_DPAM_PAN_E:
> + case USBC_DPAM_PAN_E_REVERSE:
> + port->mode = DP_PIN_ASSIGN_E;
> + break;
> + case USBC_DPAM_PAN_NONE:
> + port->mode = TYPEC_STATE_SAFE;
> + break;
> + default:
> + dev_warn(uec->dev, "unknow mode %d\n", port->mode);
> + break;
> + }
> +
> + switch (port->mux) {
> + case USBC_MUX_NONE:
> + port->svid = 0;
> + break;
> + case USBC_MUX_USB_2L:
> + port->svid = USB_SID_PD;
> + port->mode = TYPEC_STATE_USB; /* same as PAN_C, correct it */
> + break;
> + case USBC_MUX_DP_4L:
> + case USBC_MUX_USB_DP:
> + port->svid = USB_SID_DISPLAYPORT;
> + break;
> + default:
> + dev_warn(uec->dev, "unknow mux state %d\n", port->mux);
> + break;
> + }
> +
> + spin_unlock_irqrestore(&port->lock, flags);
> +}
> +
> +static int gaokun_ucsi_refresh(struct gaokun_ucsi *uec)
> +{
> + struct gaokun_ucsi_reg ureg;
> + int ret, idx;
> +
> + ret = gaokun_ec_ucsi_get_reg(uec->ec, (u8 *)&ureg);
> + if (ret)
> + return GAOKUN_UCSI_NO_PORT_UPDATE;
> +
> + uec->port_num = ureg.port_num;
> + idx = GET_IDX(ureg.port_updt);
> +
> + if (idx < 0 || idx >= ureg.port_num)
> + return GAOKUN_UCSI_NO_PORT_UPDATE;
> +
> + gaokun_ucsi_port_update(&uec->ports[idx], ureg.port_data);
> + return idx;
> +}
> +
> +static void gaokun_ucsi_handle_altmode(struct gaokun_ucsi_port *port)
> +{
> + struct gaokun_ucsi *uec = port->ucsi;
> + int idx = port->idx;
> +
> + if (idx >= uec->ucsi->cap.num_connectors) {
> + dev_warn(uec->dev, "altmode port out of range: %d\n", idx);
> + return;
> + }
> +
> + /* UCSI callback .connector_status() have set orientation */
> + if (port->bridge)
> + drm_aux_hpd_bridge_notify(&port->bridge->dev,
> + port->hpd_state ?
> + connector_status_connected :
> + connector_status_disconnected);
> +
> + gaokun_ec_ucsi_pan_ack(uec->ec, port->idx);
> +}
> +
> +static void gaokun_ucsi_altmode_notify_ind(struct gaokun_ucsi *uec)
> +{
> + int idx;
> +
> + if (!uec->ucsi->connector) { /* slow to register */
> + dev_err(uec->dev, "ucsi connector is not initialized yet\n");
> + return;
> + }
> +
> + idx = gaokun_ucsi_refresh(uec);
> + if (idx == GAOKUN_UCSI_NO_PORT_UPDATE)
> + gaokun_ec_ucsi_pan_ack(uec->ec, idx); /* ack directly if no update */
> + else
> + gaokun_ucsi_handle_altmode(&uec->ports[idx]);
> +}
> +
> +/*
> + * USB event is necessary for enabling altmode, the event should follow
> + * UCSI event, if not after timeout(this notify may be disabled somehow),
> + * then force to enable altmode.
> + */
> +static void gaokun_ucsi_handle_no_usb_event(struct gaokun_ucsi *uec, int idx)
> +{
> + struct gaokun_ucsi_port *port;
> +
> + port = &uec->ports[idx];
> + if (!wait_for_completion_timeout(&port->usb_ack, 2 * HZ)) {
> + dev_warn(uec->dev, "No USB EVENT, triggered by UCSI EVENT");
> + gaokun_ucsi_altmode_notify_ind(uec);
> + }
> +}
> +
> +static int gaokun_ucsi_notify(struct notifier_block *nb,
> + unsigned long action, void *data)
> +{
> + u32 cci;
> + struct gaokun_ucsi *uec = container_of(nb, struct gaokun_ucsi, nb);
> +
> + switch (action) {
> + case EC_EVENT_USB:
> + gaokun_ucsi_altmode_notify_ind(uec);
> + return NOTIFY_OK;
> +
> + case EC_EVENT_UCSI:
> + uec->ucsi->ops->read_cci(uec->ucsi, &cci);
I think it would be more clear here to just call
gaokun_ucsi_read_cci() directly here.
> + ucsi_notify_common(uec->ucsi, cci);
> + if (UCSI_CCI_CONNECTOR(cci))
> + gaokun_ucsi_handle_no_usb_event(uec, UCSI_CCI_CONNECTOR(cci) - 1);
> +
> + return NOTIFY_OK;
> +
> + default:
> + return NOTIFY_DONE;
> + }
> +}
> +
> +static inline int gaokun_ucsi_get_port_num(struct gaokun_ucsi *uec)
> +{
> + struct gaokun_ucsi_reg ureg;
> + int ret;
> +
> + ret = gaokun_ec_ucsi_get_reg(uec->ec, (u8 *)&ureg);
> +
> + return ret ? 0 : ureg.port_num;
> +}
This function does not look like it's necessary to me.
> +static int gaokun_ucsi_ports_init(struct gaokun_ucsi *uec)
> +{
> + u32 port;
> + int i, ret, port_num;
> + struct device *dev = uec->dev;
> + struct gaokun_ucsi_port *ucsi_port;
> + struct fwnode_handle *fwnode;
> +
> + port_num = gaokun_ucsi_get_port_num(uec);
> + uec->port_num = port_num;
struct gaokun_ucsi_reg ureg = { };
gaokun_ec_ucsi_get_reg(uec->ec, &ureg);
port_num = ureg->port_num;
(Btw. port_num here is confusing IMO, because you are not talking
about the number of a port, but instead number of all ports. So
something like num_ports or nports would be better).
> + uec->ports = devm_kcalloc(dev, port_num, sizeof(*(uec->ports)),
> + GFP_KERNEL);
> + if (!uec->ports)
> + return -ENOMEM;
> +
> + for (i = 0; i < port_num; ++i) {
> + ucsi_port = &uec->ports[i];
> + ucsi_port->ccx = USBC_CCX_NONE;
> + ucsi_port->idx = i;
> + ucsi_port->ucsi = uec;
> + init_completion(&ucsi_port->usb_ack);
> + spin_lock_init(&ucsi_port->lock);
> + }
> +
> + device_for_each_child_node(dev, fwnode) {
> + ret = fwnode_property_read_u32(fwnode, "reg", &port);
> + if (ret < 0) {
> + dev_err(dev, "missing reg property of %pOFn\n", fwnode);
> + fwnode_handle_put(fwnode);
> + return ret;
> + }
> +
> + if (port >= port_num) {
> + dev_warn(dev, "invalid connector number %d, ignoring\n", port);
> + continue;
> + }
> +
> + ucsi_port = &uec->ports[port];
> + ucsi_port->bridge = devm_drm_dp_hpd_bridge_alloc(dev, to_of_node(fwnode));
> + if (IS_ERR(ucsi_port->bridge)) {
> + fwnode_handle_put(fwnode);
> + return PTR_ERR(ucsi_port->bridge);
> + }
> + }
> +
> + for (i = 0; i < port_num; i++) {
> + if (!uec->ports[i].bridge)
> + continue;
> +
> + ret = devm_drm_dp_hpd_bridge_add(dev, uec->ports[i].bridge);
> + if (ret)
> + return ret;
> + }
> +
> + return 0;
> +}
> +
> +static void gaokun_ucsi_register_worker(struct work_struct *work)
> +{
> + struct gaokun_ucsi *uec;
> + struct ucsi *ucsi;
> + int ret;
> +
> + uec = container_of(work, struct gaokun_ucsi, work.work);
> + ucsi = uec->ucsi;
> + /* This may be a problem specific to sc8280xp-based machines */
> + ucsi->quirks = UCSI_NO_PARTNER_PDOS | UCSI_DELAY_DEVICE_PDOS;
> +
> + ret = gaokun_ec_register_notify(uec->ec, &uec->nb);
> + if (ret) {
> + dev_err_probe(ucsi->dev, ret, "notifier register failed\n");
> + return;
> + }
> +
> + ret = ucsi_register(ucsi);
> + if (ret)
> + dev_err_probe(ucsi->dev, ret, "ucsi register failed\n");
> +}
> +
> +static inline int gaokun_ucsi_register(struct gaokun_ucsi *uec)
> +{
> + /* EC can't handle UCSI properly in the early stage */
> + schedule_delayed_work(&uec->work, 3 * HZ);
> +
> + return 0;
> +}
This whole function is unnecessary.
> +static int gaokun_ucsi_probe(struct auxiliary_device *adev,
> + const struct auxiliary_device_id *id)
> +{
> + struct gaokun_ec *ec = adev->dev.platform_data;
> + struct device *dev = &adev->dev;
> + struct gaokun_ucsi *uec;
> + int ret;
> +
> + uec = devm_kzalloc(dev, sizeof(*uec), GFP_KERNEL);
> + if (!uec)
> + return -ENOMEM;
> +
> + uec->ec = ec;
> + uec->dev = dev;
> + uec->version = 0x0100;
Please add a definition for UCSI v1.0 to ucsi.h (separate patch).
> + uec->nb.notifier_call = gaokun_ucsi_notify;
> +
> + INIT_DELAYED_WORK(&uec->work, gaokun_ucsi_register_worker);
> +
> + ret = gaokun_ucsi_ports_init(uec);
> + if (ret)
> + return ret;
> +
> + uec->ucsi = ucsi_create(dev, &gaokun_ucsi_ops);
> + if (IS_ERR(uec->ucsi))
> + return PTR_ERR(uec->ucsi);
> +
> + ucsi_set_drvdata(uec->ucsi, uec);
> + auxiliary_set_drvdata(adev, uec);
> +
> + return gaokun_ucsi_register(uec);
> +}
> +
> +static void gaokun_ucsi_remove(struct auxiliary_device *adev)
> +{
> + struct gaokun_ucsi *uec = auxiliary_get_drvdata(adev);
> +
> + gaokun_ec_unregister_notify(uec->ec, &uec->nb);
> + ucsi_unregister(uec->ucsi);
> + ucsi_destroy(uec->ucsi);
> +}
> +
> +static const struct auxiliary_device_id gaokun_ucsi_id_table[] = {
> + { .name = GAOKUN_MOD_NAME "." GAOKUN_DEV_UCSI, },
> + {}
> +};
> +MODULE_DEVICE_TABLE(auxiliary, gaokun_ucsi_id_table);
> +
> +static struct auxiliary_driver gaokun_ucsi_driver = {
> + .name = GAOKUN_DEV_UCSI,
> + .id_table = gaokun_ucsi_id_table,
> + .probe = gaokun_ucsi_probe,
> + .remove = gaokun_ucsi_remove,
> +};
> +
> +module_auxiliary_driver(gaokun_ucsi_driver);
> +
> +MODULE_DESCRIPTION("HUAWEI Matebook E Go UCSI driver");
> +MODULE_LICENSE("GPL");
thanks,
--
heikki
^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH v2 2/5] platform: arm64: add Huawei Matebook E Go EC driver
2025-01-07 12:25 ` Heikki Krogerus
@ 2025-01-07 16:53 ` Pengyu Luo
0 siblings, 0 replies; 15+ messages in thread
From: Pengyu Luo @ 2025-01-07 16:53 UTC (permalink / raw)
To: heikki.krogerus
Cc: andersson, bryan.odonoghue, conor+dt, devicetree,
dmitry.baryshkov, gregkh, hdegoede, ilpo.jarvinen, jdelvare,
konradybcio, krzk+dt, linux-arm-msm, linux-hwmon, linux-kernel,
linux-pm, linux-usb, linux, mitltlatltl, platform-driver-x86,
robh, sre
On Tue, Jan 7, 2025 at 8:25 PM Heikki Krogerus <heikki.krogerus@linux.intel.com> wrote:
>
> Hi,
>
> > +/* -------------------------------------------------------------------------- */
> > +/* API For UCSI */
> > +
> > +int gaokun_ec_ucsi_read(struct gaokun_ec *ec,
> > + u8 resp[GAOKUN_UCSI_READ_SIZE])
> > +{
> > + u8 req[] = MKREQ(0x03, 0xD5, 0);
> > + u8 _resp[] = MKRESP(GAOKUN_UCSI_READ_SIZE);
> > + int ret;
> > +
> > + ret = gaokun_ec_read(ec, req, sizeof(_resp), _resp);
> > + if (ret)
> > + return ret;
> > +
> > + extr_resp(resp, _resp, GAOKUN_UCSI_READ_SIZE);
> > + return 0;
> > +}
> > +EXPORT_SYMBOL_GPL(gaokun_ec_ucsi_read);
> > +
> > +int gaokun_ec_ucsi_write(struct gaokun_ec *ec,
> > + const u8 req[GAOKUN_UCSI_WRITE_SIZE])
> > +{
> > + u8 _req[] = MKREQ(0x03, 0xD4, GAOKUN_UCSI_WRITE_SIZE);
> > +
> > +
> > + refill_req(_req, req, GAOKUN_UCSI_WRITE_SIZE);
> > +
> > + return gaokun_ec_write(ec, _req);
> > +}
> > +EXPORT_SYMBOL_GPL(gaokun_ec_ucsi_write);
> > +
> > +int gaokun_ec_ucsi_get_reg(struct gaokun_ec *ec, u8 *ureg)
> > +{
> > + u8 req[] = MKREQ(0x03, 0xD3, 0);
> > + u8 _resp[] = MKRESP(UCSI_REG_SIZE);
> > + int ret;
> > +
> > + ret = gaokun_ec_read(ec, req, sizeof(_resp), _resp);
> > + if (ret)
> > + return ret;
> > +
> > + extr_resp(ureg, _resp, UCSI_REG_SIZE);
> > +
> > + return 0;
> > +}
> > +EXPORT_SYMBOL_GPL(gaokun_ec_ucsi_get_reg);
>
> Why not just take struct gaokun_ucsi_reg as parameter? I did not see
> this (or any of these) being used anywhere else except in your UCSI
> glue driver. So the prototype would be:
>
> int gaokun_ec_ucsi_get_reg(struct gaokun_ec *ec,
> struct gaokun_ucsi_reg *reg);
>
Understood
> > +int gaokun_ec_ucsi_pan_ack(struct gaokun_ec *ec, int port_id)
> > +{
> > + u8 req[] = MKREQ(0x03, 0xD2, 1);
> > + u8 data = 1 << port_id;
> > +
> > + if (port_id == GAOKUN_UCSI_NO_PORT_UPDATE)
> > + data = 0;
> > +
> > + refill_req(req, &data, 1);
> > +
> > + return gaokun_ec_write(ec, req);
> > +}
> > +EXPORT_SYMBOL_GPL(gaokun_ec_ucsi_pan_ack);
>
> I think you should add proper kernel doc comments to these exported
> functions.
>
Oh, I forgot some of them. gaokun_ec_ucsi_get_reg and
gaokun_ec_ucsi_pan_ack deserve kernel doc comments. Judging from the name,
others are obvious, should I comment all?
> thanks,
>
> --
> heikki
Best wishes,
Pengyu
^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH v2 3/5] usb: typec: ucsi: add Huawei Matebook E Go ucsi driver
2025-01-07 12:30 ` Heikki Krogerus
@ 2025-01-07 16:54 ` Pengyu Luo
0 siblings, 0 replies; 15+ messages in thread
From: Pengyu Luo @ 2025-01-07 16:54 UTC (permalink / raw)
To: heikki.krogerus
Cc: andersson, bryan.odonoghue, conor+dt, devicetree,
dmitry.baryshkov, gregkh, hdegoede, ilpo.jarvinen, jdelvare,
konradybcio, krzk+dt, linux-arm-msm, linux-hwmon, linux-kernel,
linux-pm, linux-usb, linux, mitltlatltl, platform-driver-x86,
robh, sre
On Tue, Jan 7, 2025 at 8:30 PM Heikki Krogerus <heikki.krogerus@linux.intel.com> wrote:
>
> Hi,
>
> On Mon, Jan 06, 2025 at 01:41:57AM +0800, Pengyu Luo wrote:
> > The Huawei Matebook E Go tablet implements the UCSI interface in the
> > onboard EC. Add the glue driver to interface with the platform's UCSI
> > implementation.
> >
> > Signed-off-by: Pengyu Luo <mitltlatltl@gmail.com>
> > ---
> > drivers/usb/typec/ucsi/Kconfig | 10 +
> > drivers/usb/typec/ucsi/Makefile | 1 +
> > drivers/usb/typec/ucsi/ucsi_huawei_gaokun.c | 524 ++++++++++++++++++++
> > 3 files changed, 535 insertions(+)
> > create mode 100644 drivers/usb/typec/ucsi/ucsi_huawei_gaokun.c
>
> This did not apply cleanly anymore, but I guess you'll be rebasing in
> any case. A few nitpicks below.
>
I see. I wrote them based on the mainline, then I formated them. I think
I should rebase to linux-next.
> > diff --git a/drivers/usb/typec/ucsi/Kconfig b/drivers/usb/typec/ucsi/Kconfig
> > index 680e1b87b..8a73df84a 100644
> > --- a/drivers/usb/typec/ucsi/Kconfig
> > +++ b/drivers/usb/typec/ucsi/Kconfig
> > @@ -78,4 +78,14 @@ config UCSI_LENOVO_YOGA_C630
> > To compile the driver as a module, choose M here: the module will be
> > called ucsi_yoga_c630.
> >
> > +config UCSI_HUAWEI_GAOKUN
> > + tristate "UCSI Interface Driver for Huawei Matebook E Go"
> > + depends on EC_HUAWEI_GAOKUN
> > + help
> > + This driver enables UCSI support on the Huawei Matebook E Go tablet,
> > + which is a sc8280xp-based 2-in-1 tablet.
> > +
> > + To compile the driver as a module, choose M here: the module will be
> > + called ucsi_huawei_gaokun.
> > +
> > endif
> > diff --git a/drivers/usb/typec/ucsi/Makefile b/drivers/usb/typec/ucsi/Makefile
> > index aed41d238..0b400122b 100644
> > --- a/drivers/usb/typec/ucsi/Makefile
> > +++ b/drivers/usb/typec/ucsi/Makefile
> > @@ -22,3 +22,4 @@ obj-$(CONFIG_UCSI_CCG) += ucsi_ccg.o
> > obj-$(CONFIG_UCSI_STM32G0) += ucsi_stm32g0.o
> > obj-$(CONFIG_UCSI_PMIC_GLINK) += ucsi_glink.o
> > obj-$(CONFIG_UCSI_LENOVO_YOGA_C630) += ucsi_yoga_c630.o
> > +obj-$(CONFIG_UCSI_HUAWEI_GAOKUN) += ucsi_huawei_gaokun.o
> > diff --git a/drivers/usb/typec/ucsi/ucsi_huawei_gaokun.c b/drivers/usb/typec/ucsi/ucsi_huawei_gaokun.c
> > new file mode 100644
> > index 000000000..044545b44
> > --- /dev/null
> > +++ b/drivers/usb/typec/ucsi/ucsi_huawei_gaokun.c
> > @@ -0,0 +1,524 @@
> > +// SPDX-License-Identifier: GPL-2.0-only
> > +/*
> > + * ucsi-huawei-gaokun - A UCSI driver for HUAWEI Matebook E Go
> > + *
> > + * reference: drivers/usb/typec/ucsi/ucsi_yoga_c630.c
> > + * drivers/usb/typec/ucsi/ucsi_glink.c
> > + * drivers/soc/qcom/pmic_glink_altmode.c
> > + *
> > + * Copyright (C) 2024 Pengyu Luo <mitltlatltl@gmail.com>
> > + */
> > +
> > +#include <drm/bridge/aux-bridge.h>
> > +#include <linux/auxiliary_bus.h>
> > +#include <linux/bitops.h>
> > +#include <linux/completion.h>
> > +#include <linux/container_of.h>
> > +#include <linux/module.h>
> > +#include <linux/notifier.h>
> > +#include <linux/of.h>
> > +#include <linux/platform_data/huawei-gaokun-ec.h>
> > +#include <linux/string.h>
> > +#include <linux/usb/pd_vdo.h>
> > +#include <linux/usb/typec_altmode.h>
> > +#include <linux/usb/typec_dp.h>
> > +#include <linux/workqueue_types.h>
> > +
> > +#include "ucsi.h"
> > +
> > +#define EC_EVENT_UCSI 0x21
> > +#define EC_EVENT_USB 0x22
> > +
> > +#define GAOKUN_CCX_MASK GENMASK(1, 0)
> > +#define GAOKUN_MUX_MASK GENMASK(3, 2)
> > +
> > +#define GAOKUN_DPAM_MASK GENMASK(3, 0)
> > +#define GAOKUN_HPD_STATE_MASK BIT(4)
> > +#define GAOKUN_HPD_IRQ_MASK BIT(5)
> > +
> > +#define GET_IDX(updt) (ffs(updt) - 1)
> > +
> > +#define CCX_TO_ORI(ccx) (++ccx % 3) /* convert ccx to enum typec_orientation */
> > +
> > +/* Configuration Channel Extension */
> > +enum gaokun_ucsi_ccx {
> > + USBC_CCX_NORMAL,
> > + USBC_CCX_REVERSE,
> > + USBC_CCX_NONE,
> > +};
> > +
> > +enum gaokun_ucsi_mux {
> > + USBC_MUX_NONE,
> > + USBC_MUX_USB_2L,
> > + USBC_MUX_DP_4L,
> > + USBC_MUX_USB_DP,
> > +};
> > +/* based on pmic_glink_altmode_pin_assignment */
> > +enum gaokun_ucsi_dpam_pan { /* DP Alt Mode Pin Assignments */
> > + USBC_DPAM_PAN_NONE,
> > + USBC_DPAM_PAN_A, /* Not supported after USB Type-C Standard v1.0b */
> > + USBC_DPAM_PAN_B, /* Not supported after USB Type-C Standard v1.0b */
> > + USBC_DPAM_PAN_C, /* USBC_DPAM_PAN_C_REVERSE - 6 */
> > + USBC_DPAM_PAN_D,
> > + USBC_DPAM_PAN_E,
> > + USBC_DPAM_PAN_F, /* Not supported after USB Type-C Standard v1.0b */
> > + USBC_DPAM_PAN_A_REVERSE,/* Not supported after USB Type-C Standard v1.0b */
> > + USBC_DPAM_PAN_B_REVERSE,/* Not supported after USB Type-C Standard v1.0b */
> > + USBC_DPAM_PAN_C_REVERSE,
> > + USBC_DPAM_PAN_D_REVERSE,
> > + USBC_DPAM_PAN_E_REVERSE,
> > + USBC_DPAM_PAN_F_REVERSE,/* Not supported after USB Type-C Standard v1.0b */
> > +};
> > +
> > +struct gaokun_ucsi_reg {
> > + u8 port_num;
> > + u8 port_updt;
> > + u8 port_data[4];
> > + u8 checksum;
> > + u8 reserved;
> > +} __packed;
> > +
> > +struct gaokun_ucsi_port {
> > + struct completion usb_ack;
> > + spinlock_t lock;
> > +
> > + struct gaokun_ucsi *ucsi;
> > + struct auxiliary_device *bridge;
> > +
> > + int idx;
> > + enum gaokun_ucsi_ccx ccx;
> > + enum gaokun_ucsi_mux mux;
> > + u8 mode;
> > + u16 svid;
> > + u8 hpd_state;
> > + u8 hpd_irq;
> > +};
> > +
> > +struct gaokun_ucsi {
> > + struct gaokun_ec *ec;
> > + struct ucsi *ucsi;
> > + struct gaokun_ucsi_port *ports;
> > + struct device *dev;
> > + struct delayed_work work;
> > + struct notifier_block nb;
> > + u16 version;
> > + u8 port_num;
> > +};
> > +
> > +/* -------------------------------------------------------------------------- */
> > +/* For UCSI */
> > +
> > +static int gaokun_ucsi_read_version(struct ucsi *ucsi, u16 *version)
> > +{
> > + struct gaokun_ucsi *uec = ucsi_get_drvdata(ucsi);
> > +
> > + *version = uec->version;
> > +
> > + return 0;
> > +}
> > +
> > +static int gaokun_ucsi_read_cci(struct ucsi *ucsi, u32 *cci)
> > +{
> > + struct gaokun_ucsi *uec = ucsi_get_drvdata(ucsi);
> > + u8 buf[GAOKUN_UCSI_READ_SIZE];
> > + int ret;
> > +
> > + ret = gaokun_ec_ucsi_read(uec->ec, buf);
> > + if (ret)
> > + return ret;
> > +
> > + memcpy(cci, buf, sizeof(*cci));
> > +
> > + return 0;
> > +}
> > +
> > +static int gaokun_ucsi_read_message_in(struct ucsi *ucsi,
> > + void *val, size_t val_len)
> > +{
> > + struct gaokun_ucsi *uec = ucsi_get_drvdata(ucsi);
> > + u8 buf[GAOKUN_UCSI_READ_SIZE];
> > + int ret;
> > +
> > + ret = gaokun_ec_ucsi_read(uec->ec, buf);
> > + if (ret)
> > + return ret;
> > +
> > + memcpy(val, buf + GAOKUN_UCSI_CCI_SIZE,
> > + min(val_len, GAOKUN_UCSI_DATA_SIZE));
> > +
> > + return 0;
> > +}
> > +
> > +static int gaokun_ucsi_async_control(struct ucsi *ucsi, u64 command)
> > +{
> > + struct gaokun_ucsi *uec = ucsi_get_drvdata(ucsi);
> > + u8 buf[GAOKUN_UCSI_WRITE_SIZE] = {};
> > +
> > + memcpy(buf, &command, sizeof(command));
> > +
> > + return gaokun_ec_ucsi_write(uec->ec, buf);
> > +}
> > +
> > +static void gaokun_ucsi_update_connector(struct ucsi_connector *con)
> > +{
> > + struct gaokun_ucsi *uec = ucsi_get_drvdata(con->ucsi);
> > +
> > + if (con->num > uec->port_num)
> > + return;
> > +
> > + con->typec_cap.orientation_aware = true;
> > +}
> > +
> > +static void gaokun_set_orientation(struct ucsi_connector *con,
> > + struct gaokun_ucsi_port *port)
> > +{
> > + enum gaokun_ucsi_ccx ccx;
> > + unsigned long flags;
> > +
> > + spin_lock_irqsave(&port->lock, flags);
> > + ccx = port->ccx;
> > + spin_unlock_irqrestore(&port->lock, flags);
> > +
> > + typec_set_orientation(con->port, CCX_TO_ORI(ccx));
> > +}
> > +
> > +static void gaokun_ucsi_connector_status(struct ucsi_connector *con)
> > +{
> > + struct gaokun_ucsi *uec = ucsi_get_drvdata(con->ucsi);
> > + int idx;
> > +
> > + idx = con->num - 1;
> > + if (con->num > uec->port_num) {
> > + dev_warn(uec->dev, "set orientation out of range: con%d\n", idx);
> > + return;
> > + }
> > +
> > + gaokun_set_orientation(con, &uec->ports[idx]);
> > +}
> > +
> > +const struct ucsi_operations gaokun_ucsi_ops = {
> > + .read_version = gaokun_ucsi_read_version,
> > + .read_cci = gaokun_ucsi_read_cci,
> > + .read_message_in = gaokun_ucsi_read_message_in,
> > + .sync_control = ucsi_sync_control_common,
> > + .async_control = gaokun_ucsi_async_control,
> > + .update_connector = gaokun_ucsi_update_connector,
> > + .connector_status = gaokun_ucsi_connector_status,
> > +};
> > +
> > +/* -------------------------------------------------------------------------- */
> > +/* For Altmode */
> > +
> > +static void gaokun_ucsi_port_update(struct gaokun_ucsi_port *port,
> > + const u8 *port_data)
> > +{
> > + struct gaokun_ucsi *uec = port->ucsi;
> > + int offset = port->idx * 2; /* every port has 2 Bytes data */
> > + unsigned long flags;
> > + u8 dcc, ddi;
> > +
> > + dcc = port_data[offset];
> > + ddi = port_data[offset + 1];
> > +
> > + spin_lock_irqsave(&port->lock, flags);
> > +
> > + port->ccx = FIELD_GET(GAOKUN_CCX_MASK, dcc);
> > + port->mux = FIELD_GET(GAOKUN_MUX_MASK, dcc);
> > + port->mode = FIELD_GET(GAOKUN_DPAM_MASK, ddi);
> > + port->hpd_state = FIELD_GET(GAOKUN_HPD_STATE_MASK, ddi);
> > + port->hpd_irq = FIELD_GET(GAOKUN_HPD_IRQ_MASK, ddi);
> > +
> > + /* Mode and SVID are unused; keeping them to make things clearer */
> > + switch (port->mode) {
> > + case USBC_DPAM_PAN_C:
> > + case USBC_DPAM_PAN_C_REVERSE:
> > + port->mode = DP_PIN_ASSIGN_C; /* correct it for usb later */
> > + break;
> > + case USBC_DPAM_PAN_D:
> > + case USBC_DPAM_PAN_D_REVERSE:
> > + port->mode = DP_PIN_ASSIGN_D;
> > + break;
> > + case USBC_DPAM_PAN_E:
> > + case USBC_DPAM_PAN_E_REVERSE:
> > + port->mode = DP_PIN_ASSIGN_E;
> > + break;
> > + case USBC_DPAM_PAN_NONE:
> > + port->mode = TYPEC_STATE_SAFE;
> > + break;
> > + default:
> > + dev_warn(uec->dev, "unknow mode %d\n", port->mode);
> > + break;
> > + }
> > +
> > + switch (port->mux) {
> > + case USBC_MUX_NONE:
> > + port->svid = 0;
> > + break;
> > + case USBC_MUX_USB_2L:
> > + port->svid = USB_SID_PD;
> > + port->mode = TYPEC_STATE_USB; /* same as PAN_C, correct it */
> > + break;
> > + case USBC_MUX_DP_4L:
> > + case USBC_MUX_USB_DP:
> > + port->svid = USB_SID_DISPLAYPORT;
> > + break;
> > + default:
> > + dev_warn(uec->dev, "unknow mux state %d\n", port->mux);
> > + break;
> > + }
> > +
> > + spin_unlock_irqrestore(&port->lock, flags);
> > +}
> > +
> > +static int gaokun_ucsi_refresh(struct gaokun_ucsi *uec)
> > +{
> > + struct gaokun_ucsi_reg ureg;
> > + int ret, idx;
> > +
> > + ret = gaokun_ec_ucsi_get_reg(uec->ec, (u8 *)&ureg);
> > + if (ret)
> > + return GAOKUN_UCSI_NO_PORT_UPDATE;
> > +
> > + uec->port_num = ureg.port_num;
> > + idx = GET_IDX(ureg.port_updt);
> > +
> > + if (idx < 0 || idx >= ureg.port_num)
> > + return GAOKUN_UCSI_NO_PORT_UPDATE;
> > +
> > + gaokun_ucsi_port_update(&uec->ports[idx], ureg.port_data);
> > + return idx;
> > +}
> > +
> > +static void gaokun_ucsi_handle_altmode(struct gaokun_ucsi_port *port)
> > +{
> > + struct gaokun_ucsi *uec = port->ucsi;
> > + int idx = port->idx;
> > +
> > + if (idx >= uec->ucsi->cap.num_connectors) {
> > + dev_warn(uec->dev, "altmode port out of range: %d\n", idx);
> > + return;
> > + }
> > +
> > + /* UCSI callback .connector_status() have set orientation */
> > + if (port->bridge)
> > + drm_aux_hpd_bridge_notify(&port->bridge->dev,
> > + port->hpd_state ?
> > + connector_status_connected :
> > + connector_status_disconnected);
> > +
> > + gaokun_ec_ucsi_pan_ack(uec->ec, port->idx);
> > +}
> > +
> > +static void gaokun_ucsi_altmode_notify_ind(struct gaokun_ucsi *uec)
> > +{
> > + int idx;
> > +
> > + if (!uec->ucsi->connector) { /* slow to register */
> > + dev_err(uec->dev, "ucsi connector is not initialized yet\n");
> > + return;
> > + }
> > +
> > + idx = gaokun_ucsi_refresh(uec);
> > + if (idx == GAOKUN_UCSI_NO_PORT_UPDATE)
> > + gaokun_ec_ucsi_pan_ack(uec->ec, idx); /* ack directly if no update */
> > + else
> > + gaokun_ucsi_handle_altmode(&uec->ports[idx]);
> > +}
> > +
> > +/*
> > + * USB event is necessary for enabling altmode, the event should follow
> > + * UCSI event, if not after timeout(this notify may be disabled somehow),
> > + * then force to enable altmode.
> > + */
> > +static void gaokun_ucsi_handle_no_usb_event(struct gaokun_ucsi *uec, int idx)
> > +{
> > + struct gaokun_ucsi_port *port;
> > +
> > + port = &uec->ports[idx];
> > + if (!wait_for_completion_timeout(&port->usb_ack, 2 * HZ)) {
> > + dev_warn(uec->dev, "No USB EVENT, triggered by UCSI EVENT");
> > + gaokun_ucsi_altmode_notify_ind(uec);
> > + }
> > +}
> > +
> > +static int gaokun_ucsi_notify(struct notifier_block *nb,
> > + unsigned long action, void *data)
> > +{
> > + u32 cci;
> > + struct gaokun_ucsi *uec = container_of(nb, struct gaokun_ucsi, nb);
> > +
> > + switch (action) {
> > + case EC_EVENT_USB:
> > + gaokun_ucsi_altmode_notify_ind(uec);
> > + return NOTIFY_OK;
> > +
> > + case EC_EVENT_UCSI:
> > + uec->ucsi->ops->read_cci(uec->ucsi, &cci);
>
> I think it would be more clear here to just call
> gaokun_ucsi_read_cci() directly here.
>
Agree
> > + ucsi_notify_common(uec->ucsi, cci);
> > + if (UCSI_CCI_CONNECTOR(cci))
> > + gaokun_ucsi_handle_no_usb_event(uec, UCSI_CCI_CONNECTOR(cci) - 1);
> > +
> > + return NOTIFY_OK;
> > +
> > + default:
> > + return NOTIFY_DONE;
> > + }
> > +}
> > +
> > +static inline int gaokun_ucsi_get_port_num(struct gaokun_ucsi *uec)
> > +{
> > + struct gaokun_ucsi_reg ureg;
> > + int ret;
> > +
> > + ret = gaokun_ec_ucsi_get_reg(uec->ec, (u8 *)&ureg);
> > +
> > + return ret ? 0 : ureg.port_num;
> > +}
>
> This function does not look like it's necessary to me.
>
Agree
> > +static int gaokun_ucsi_ports_init(struct gaokun_ucsi *uec)
> > +{
> > + u32 port;
> > + int i, ret, port_num;
> > + struct device *dev = uec->dev;
> > + struct gaokun_ucsi_port *ucsi_port;
> > + struct fwnode_handle *fwnode;
> > +
> > + port_num = gaokun_ucsi_get_port_num(uec);
> > + uec->port_num = port_num;
>
> struct gaokun_ucsi_reg ureg = { };
>
> gaokun_ec_ucsi_get_reg(uec->ec, &ureg);
> port_num = ureg->port_num;
>
> (Btw. port_num here is confusing IMO, because you are not talking
> about the number of a port, but instead number of all ports. So
> something like num_ports or nports would be better).
>
Agree
> > + uec->ports = devm_kcalloc(dev, port_num, sizeof(*(uec->ports)),
> > + GFP_KERNEL);
> > + if (!uec->ports)
> > + return -ENOMEM;
> > +
> > + for (i = 0; i < port_num; ++i) {
> > + ucsi_port = &uec->ports[i];
> > + ucsi_port->ccx = USBC_CCX_NONE;
> > + ucsi_port->idx = i;
> > + ucsi_port->ucsi = uec;
> > + init_completion(&ucsi_port->usb_ack);
> > + spin_lock_init(&ucsi_port->lock);
> > + }
> > +
> > + device_for_each_child_node(dev, fwnode) {
> > + ret = fwnode_property_read_u32(fwnode, "reg", &port);
> > + if (ret < 0) {
> > + dev_err(dev, "missing reg property of %pOFn\n", fwnode);
> > + fwnode_handle_put(fwnode);
> > + return ret;
> > + }
> > +
> > + if (port >= port_num) {
> > + dev_warn(dev, "invalid connector number %d, ignoring\n", port);
> > + continue;
> > + }
> > +
> > + ucsi_port = &uec->ports[port];
> > + ucsi_port->bridge = devm_drm_dp_hpd_bridge_alloc(dev, to_of_node(fwnode));
> > + if (IS_ERR(ucsi_port->bridge)) {
> > + fwnode_handle_put(fwnode);
> > + return PTR_ERR(ucsi_port->bridge);
> > + }
> > + }
> > +
> > + for (i = 0; i < port_num; i++) {
> > + if (!uec->ports[i].bridge)
> > + continue;
> > +
> > + ret = devm_drm_dp_hpd_bridge_add(dev, uec->ports[i].bridge);
> > + if (ret)
> > + return ret;
> > + }
> > +
> > + return 0;
> > +}
> > +
> > +static void gaokun_ucsi_register_worker(struct work_struct *work)
> > +{
> > + struct gaokun_ucsi *uec;
> > + struct ucsi *ucsi;
> > + int ret;
> > +
> > + uec = container_of(work, struct gaokun_ucsi, work.work);
> > + ucsi = uec->ucsi;
> > + /* This may be a problem specific to sc8280xp-based machines */
> > + ucsi->quirks = UCSI_NO_PARTNER_PDOS | UCSI_DELAY_DEVICE_PDOS;
> > +
> > + ret = gaokun_ec_register_notify(uec->ec, &uec->nb);
> > + if (ret) {
> > + dev_err_probe(ucsi->dev, ret, "notifier register failed\n");
> > + return;
> > + }
> > +
> > + ret = ucsi_register(ucsi);
> > + if (ret)
> > + dev_err_probe(ucsi->dev, ret, "ucsi register failed\n");
> > +}
> > +
> > +static inline int gaokun_ucsi_register(struct gaokun_ucsi *uec)
> > +{
> > + /* EC can't handle UCSI properly in the early stage */
> > + schedule_delayed_work(&uec->work, 3 * HZ);
> > +
> > + return 0;
> > +}
>
> This whole function is unnecessary.
>
I see, I will move it to probe function
> > +static int gaokun_ucsi_probe(struct auxiliary_device *adev,
> > + const struct auxiliary_device_id *id)
> > +{
> > + struct gaokun_ec *ec = adev->dev.platform_data;
> > + struct device *dev = &adev->dev;
> > + struct gaokun_ucsi *uec;
> > + int ret;
> > +
> > + uec = devm_kzalloc(dev, sizeof(*uec), GFP_KERNEL);
> > + if (!uec)
> > + return -ENOMEM;
> > +
> > + uec->ec = ec;
> > + uec->dev = dev;
> > + uec->version = 0x0100;
>
> Please add a definition for UCSI v1.0 to ucsi.h (separate patch).
>
Understood
> > + uec->nb.notifier_call = gaokun_ucsi_notify;
> > +
> > + INIT_DELAYED_WORK(&uec->work, gaokun_ucsi_register_worker);
> > +
> > + ret = gaokun_ucsi_ports_init(uec);
> > + if (ret)
> > + return ret;
> > +
> > + uec->ucsi = ucsi_create(dev, &gaokun_ucsi_ops);
> > + if (IS_ERR(uec->ucsi))
> > + return PTR_ERR(uec->ucsi);
> > +
> > + ucsi_set_drvdata(uec->ucsi, uec);
> > + auxiliary_set_drvdata(adev, uec);
> > +
> > + return gaokun_ucsi_register(uec);
> > +}
> > +
> > +static void gaokun_ucsi_remove(struct auxiliary_device *adev)
> > +{
> > + struct gaokun_ucsi *uec = auxiliary_get_drvdata(adev);
> > +
> > + gaokun_ec_unregister_notify(uec->ec, &uec->nb);
> > + ucsi_unregister(uec->ucsi);
> > + ucsi_destroy(uec->ucsi);
> > +}
> > +
> > +static const struct auxiliary_device_id gaokun_ucsi_id_table[] = {
> > + { .name = GAOKUN_MOD_NAME "." GAOKUN_DEV_UCSI, },
> > + {}
> > +};
> > +MODULE_DEVICE_TABLE(auxiliary, gaokun_ucsi_id_table);
> > +
> > +static struct auxiliary_driver gaokun_ucsi_driver = {
> > + .name = GAOKUN_DEV_UCSI,
> > + .id_table = gaokun_ucsi_id_table,
> > + .probe = gaokun_ucsi_probe,
> > + .remove = gaokun_ucsi_remove,
> > +};
> > +
> > +module_auxiliary_driver(gaokun_ucsi_driver);
> > +
> > +MODULE_DESCRIPTION("HUAWEI Matebook E Go UCSI driver");
> > +MODULE_LICENSE("GPL");
>
> thanks,
>
> --
> heikki
Best wishes,
Pengyu
^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH v2 5/5] arm64: dts: qcom: gaokun3: Add Embedded Controller node
2025-01-05 17:41 ` [PATCH v2 5/5] arm64: dts: qcom: gaokun3: Add Embedded Controller node Pengyu Luo
@ 2025-01-09 14:37 ` Konrad Dybcio
0 siblings, 0 replies; 15+ messages in thread
From: Konrad Dybcio @ 2025-01-09 14:37 UTC (permalink / raw)
To: Pengyu Luo, Rob Herring, Krzysztof Kozlowski, Conor Dooley,
Bjorn Andersson, Konrad Dybcio, Hans de Goede, Ilpo Järvinen,
Bryan O'Donoghue, Sebastian Reichel, Heikki Krogerus,
Greg Kroah-Hartman, Jean Delvare, Guenter Roeck
Cc: devicetree, linux-kernel, linux-arm-msm, platform-driver-x86,
linux-pm, linux-usb, linux-hwmon, Dmitry Baryshkov
On 5.01.2025 6:41 PM, Pengyu Luo wrote:
> 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 ports, orientation switches and the QMP combo PHY.
>
> Signed-off-by: Pengyu Luo <mitltlatltl@gmail.com>
> ---
> .../boot/dts/qcom/sc8280xp-huawei-gaokun3.dts | 139 ++++++++++++++++++
> 1 file changed, 139 insertions(+)
>
> diff --git a/arch/arm64/boot/dts/qcom/sc8280xp-huawei-gaokun3.dts b/arch/arm64/boot/dts/qcom/sc8280xp-huawei-gaokun3.dts
> index 09b95f89e..ff5db8f63 100644
> --- a/arch/arm64/boot/dts/qcom/sc8280xp-huawei-gaokun3.dts
> +++ b/arch/arm64/boot/dts/qcom/sc8280xp-huawei-gaokun3.dts
> @@ -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";
Sorry, there's still one too many tabs here and under /usb1-sbu-mux
The rest looks okay though!
Konrad
^ permalink raw reply [flat|nested] 15+ messages in thread
end of thread, other threads:[~2025-01-09 14:37 UTC | newest]
Thread overview: 15+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-01-05 17:41 [PATCH v2 0/5] platform: arm64: Huawei Matebook E Go embedded controller Pengyu Luo
2025-01-05 17:41 ` [PATCH v2 1/5] dt-bindings: platform: Add Huawei Matebook E Go EC Pengyu Luo
2025-01-06 7:11 ` Krzysztof Kozlowski
2025-01-06 8:06 ` Pengyu Luo
2025-01-06 12:29 ` Krzysztof Kozlowski
2025-01-05 17:41 ` [PATCH v2 2/5] platform: arm64: add Huawei Matebook E Go EC driver Pengyu Luo
2025-01-07 12:25 ` Heikki Krogerus
2025-01-07 16:53 ` Pengyu Luo
2025-01-05 17:41 ` [PATCH v2 3/5] usb: typec: ucsi: add Huawei Matebook E Go ucsi driver Pengyu Luo
2025-01-07 12:30 ` Heikki Krogerus
2025-01-07 16:54 ` Pengyu Luo
2025-01-05 17:41 ` [PATCH v2 4/5] power: supply: add Huawei Matebook E Go psy driver Pengyu Luo
2025-01-05 17:41 ` [PATCH v2 5/5] arm64: dts: qcom: gaokun3: Add Embedded Controller node Pengyu Luo
2025-01-09 14:37 ` Konrad Dybcio
2025-01-06 19:27 ` [PATCH v2 0/5] platform: arm64: Huawei Matebook E Go embedded controller Rob Herring (Arm)
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).