2024-03-12 08:52:59

by Nikita Travkin

[permalink] [raw]
Subject: [PATCH v4 0/4] platform: arm64: Acer Aspire 1 embedded controller

The laptop contains an embedded controller that provides a set of
features:

- Battery and charger monitoring
- USB Type-C DP alt mode HPD monitoring
- Lid status detection
- Small amount of keyboard configuration*

[*] The keyboard is handled by the same EC but it has a dedicated i2c
bus and is already enabled. This port only provides fn key behavior
configuration.

Unfortunately, while all this functionality is implemented in ACPI, it's
currently not possible to use ACPI to boot Linux on such Qualcomm
devices. Thus this series implements and enables a new driver that
provides support for the EC features.

The EC would be one of the last pieces to get almost full support for the
Acer Aspire 1 laptop in the upstream Linux kernel.

This series is similar to the EC driver for Lenovo Yoga C630, proposed
in [1] but seemingly never followed up...

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

Signed-off-by: Nikita Travkin <[email protected]>
---
Changes in v4:
- Move to platform/arm64 (Sebastian, Hans)
- Drop fn mode dt property (Rob)
- Add fn_lock attribute in sysfs (Rob)
- Report psy present correctly (Sebastian)
- Link to v3: https://lore.kernel.org/r/[email protected]

Changes in v3:
- Supress warning on few no-op events.
- Invert the fn key behavior (Rob, Conor)
- Link to v2: https://lore.kernel.org/r/[email protected]

Changes in v2:
- Drop incorrectly allowed reg in the ec connector binding (Krzysztof)
- Minor style changes (Konrad)
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Nikita Travkin (4):
dt-bindings: platform: Add Acer Aspire 1 EC
platform: Add ARM64 platform directory
platform: arm64: Add Acer Aspire 1 embedded controller driver
arm64: dts: qcom: acer-aspire1: Add embedded controller

.../bindings/platform/acer,aspire1-ec.yaml | 60 +++
MAINTAINERS | 9 +
arch/arm64/boot/dts/qcom/sc7180-acer-aspire1.dts | 40 +-
drivers/platform/Kconfig | 2 +
drivers/platform/Makefile | 1 +
drivers/platform/arm64/Kconfig | 35 ++
drivers/platform/arm64/Makefile | 8 +
drivers/platform/arm64/acer-aspire1-ec.c | 555 +++++++++++++++++++++
8 files changed, 709 insertions(+), 1 deletion(-)
---
base-commit: a1184cae56bcb96b86df3ee0377cec507a3f56e0
change-id: 20231206-aspire1-ec-6b3d2cac1a72

Best regards,
--
Nikita Travkin <[email protected]>



2024-03-12 08:53:00

by Nikita Travkin

[permalink] [raw]
Subject: [PATCH v4 1/4] dt-bindings: platform: Add Acer Aspire 1 EC

Add binding for the EC found in the Acer Aspire 1 laptop.

Signed-off-by: Nikita Travkin <[email protected]>
---
.../bindings/platform/acer,aspire1-ec.yaml | 60 ++++++++++++++++++++++
1 file changed, 60 insertions(+)

diff --git a/Documentation/devicetree/bindings/platform/acer,aspire1-ec.yaml b/Documentation/devicetree/bindings/platform/acer,aspire1-ec.yaml
new file mode 100644
index 000000000000..7cb0134134ff
--- /dev/null
+++ b/Documentation/devicetree/bindings/platform/acer,aspire1-ec.yaml
@@ -0,0 +1,60 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/platform/acer,aspire1-ec.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Acer Aspire 1 Embedded Controller
+
+maintainers:
+ - Nikita Travkin <[email protected]>
+
+description:
+ The Acer Aspire 1 laptop uses an embedded controller to control battery
+ and charging as well as to provide a set of misc features such as the
+ laptop lid status and HPD events for the USB Type-C DP alt mode.
+
+properties:
+ compatible:
+ const: acer,aspire1-ec
+
+ reg:
+ const: 0x76
+
+ interrupts:
+ maxItems: 1
+
+ connector:
+ $ref: /schemas/connector/usb-connector.yaml#
+
+required:
+ - compatible
+ - reg
+ - interrupts
+
+additionalProperties: false
+
+examples:
+ - |
+ #include <dt-bindings/interrupt-controller/irq.h>
+ i2c {
+ #address-cells = <1>;
+ #size-cells = <0>;
+
+ embedded-controller@76 {
+ compatible = "acer,aspire1-ec";
+ reg = <0x76>;
+
+ interrupts-extended = <&tlmm 30 IRQ_TYPE_LEVEL_LOW>;
+
+ connector {
+ compatible = "usb-c-connector";
+
+ port {
+ ec_dp_in: endpoint {
+ remote-endpoint = <&mdss_dp_out>;
+ };
+ };
+ };
+ };
+ };

--
2.43.2


2024-03-12 08:53:14

by Nikita Travkin

[permalink] [raw]
Subject: [PATCH v4 2/4] platform: Add ARM64 platform directory

Some ARM64 based laptops and computers require vendor/board specific
drivers for their embedded controllers. Even though usually the most
important functionality of those devices is implemented inside ACPI,
unfortunately Linux doesn't currently have great support for ACPI on
platforms like Qualcomm Snapdragon that are used in most ARM64 laptops
today. Instead Linux relies on Device Tree for Qualcomm based devices
and it's significantly easier to reimplement the EC functionality in
a dedicated driver than to make use of ACPI code.

This commit introduces a new platform/arm64 subdirectory to give a
place to such drivers for EC-like devices.

A new MAINTAINERS entry is added for this directory. Patches to files in
this directory will be taken up by the platform-drivers-x86 team (i.e.
Hans de Goede and Mark Gross).

Signed-off-by: Nikita Travkin <[email protected]>
---
MAINTAINERS | 9 +++++++++
drivers/platform/Kconfig | 2 ++
drivers/platform/Makefile | 1 +
drivers/platform/arm64/Kconfig | 19 +++++++++++++++++++
drivers/platform/arm64/Makefile | 6 ++++++
5 files changed, 37 insertions(+)

diff --git a/MAINTAINERS b/MAINTAINERS
index b43102ca365d..ec8d706a99aa 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -3050,6 +3050,15 @@ F: drivers/mmc/host/sdhci-of-arasan.c
N: zynq
N: xilinx

+ARM64 PLATFORM DRIVERS
+M: Hans de Goede <[email protected]>
+M: Ilpo Järvinen <[email protected]>
+L: [email protected]
+S: Maintained
+Q: https://patchwork.kernel.org/project/platform-driver-x86/list/
+T: git git://git.kernel.org/pub/scm/linux/kernel/git/pdx86/platform-drivers-x86.git
+F: drivers/platform/arm64/
+
ARM64 PORT (AARCH64 ARCHITECTURE)
M: Catalin Marinas <[email protected]>
M: Will Deacon <[email protected]>
diff --git a/drivers/platform/Kconfig b/drivers/platform/Kconfig
index 868b20361769..81a298517df2 100644
--- a/drivers/platform/Kconfig
+++ b/drivers/platform/Kconfig
@@ -14,3 +14,5 @@ source "drivers/platform/olpc/Kconfig"
source "drivers/platform/surface/Kconfig"

source "drivers/platform/x86/Kconfig"
+
+source "drivers/platform/arm64/Kconfig"
diff --git a/drivers/platform/Makefile b/drivers/platform/Makefile
index 41640172975a..fbbe4f77aa5d 100644
--- a/drivers/platform/Makefile
+++ b/drivers/platform/Makefile
@@ -11,3 +11,4 @@ obj-$(CONFIG_OLPC_EC) += olpc/
obj-$(CONFIG_GOLDFISH) += goldfish/
obj-$(CONFIG_CHROME_PLATFORMS) += chrome/
obj-$(CONFIG_SURFACE_PLATFORMS) += surface/
+obj-$(CONFIG_ARM64) += arm64/
diff --git a/drivers/platform/arm64/Kconfig b/drivers/platform/arm64/Kconfig
new file mode 100644
index 000000000000..644b83ede093
--- /dev/null
+++ b/drivers/platform/arm64/Kconfig
@@ -0,0 +1,19 @@
+# SPDX-License-Identifier: GPL-2.0-only
+#
+# EC-like Drivers for aarch64 based devices.
+#
+
+menuconfig ARM64_PLATFORM_DEVICES
+ bool "ARM64 Platform-Specific Device Drivers"
+ depends on ARM64 || COMPILE_TEST
+ default y
+ help
+ Say Y here to get to see options for platform-specific device drivers
+ for arm64 based devices, primarily EC-like device drivers.
+ This option alone does not add any kernel code.
+
+ If you say N, all options in this submenu will be skipped and disabled.
+
+if ARM64_PLATFORM_DEVICES
+
+endif # ARM64_PLATFORM_DEVICES
diff --git a/drivers/platform/arm64/Makefile b/drivers/platform/arm64/Makefile
new file mode 100644
index 000000000000..f91cdc7155e2
--- /dev/null
+++ b/drivers/platform/arm64/Makefile
@@ -0,0 +1,6 @@
+# SPDX-License-Identifier: GPL-2.0-only
+#
+# Makefile for linux/drivers/platform/arm64
+#
+# This dir should only include drivers for EC-like devices.
+#

--
2.43.2


2024-03-12 09:03:10

by Nikita Travkin

[permalink] [raw]
Subject: [PATCH v4 3/4] platform: arm64: Add Acer Aspire 1 embedded controller driver

Acer Aspire 1 is a Snapdragon 7c based laptop. It uses an embedded
controller to perform a set of various functions, such as:

- Battery and charger monitoring;
- Keyboard layout control (i.e. fn_lock settings);
- USB Type-C DP alt mode HPD notifications;
- Laptop lid status.

Unfortunately, while all this functionality is implemented in ACPI, it's
currently not possible to use ACPI to boot Linux on such Qualcomm
devices. To allow Linux to still support the features provided by EC,
this driver reimplments the relevant ACPI parts. This allows us to boot
the laptop with Device Tree and retain all the features.

Signed-off-by: Nikita Travkin <[email protected]>
---
drivers/platform/arm64/Kconfig | 16 +
drivers/platform/arm64/Makefile | 2 +
drivers/platform/arm64/acer-aspire1-ec.c | 555 +++++++++++++++++++++++++++++++
3 files changed, 573 insertions(+)

diff --git a/drivers/platform/arm64/Kconfig b/drivers/platform/arm64/Kconfig
index 644b83ede093..07d47879a9e3 100644
--- a/drivers/platform/arm64/Kconfig
+++ b/drivers/platform/arm64/Kconfig
@@ -16,4 +16,20 @@ menuconfig ARM64_PLATFORM_DEVICES

if ARM64_PLATFORM_DEVICES

+config EC_ACER_ASPIRE1
+ tristate "Acer Aspire 1 Emedded Controller driver"
+ depends on I2C
+ depends on DRM
+ depends on POWER_SUPPLY
+ depends on INPUT
+ help
+ Say Y here to enable the EC driver for the (Snapdragon-based)
+ Acer Aspire 1 laptop. The EC handles battery and charging
+ monitoring as well as some misc functions like the lid sensor
+ and USB Type-C DP HPD events.
+
+ This driver provides battery and AC status support for the mentioned
+ laptop where this information is not properly exposed via the
+ standard ACPI devices.
+
endif # ARM64_PLATFORM_DEVICES
diff --git a/drivers/platform/arm64/Makefile b/drivers/platform/arm64/Makefile
index f91cdc7155e2..4fcc9855579b 100644
--- a/drivers/platform/arm64/Makefile
+++ b/drivers/platform/arm64/Makefile
@@ -4,3 +4,5 @@
#
# This dir should only include drivers for EC-like devices.
#
+
+obj-$(CONFIG_EC_ACER_ASPIRE1) += acer-aspire1-ec.o
diff --git a/drivers/platform/arm64/acer-aspire1-ec.c b/drivers/platform/arm64/acer-aspire1-ec.c
new file mode 100644
index 000000000000..3941e24c5c7c
--- /dev/null
+++ b/drivers/platform/arm64/acer-aspire1-ec.c
@@ -0,0 +1,555 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/* Copyright (c) 2023, Nikita Travkin <[email protected]> */
+
+#include <linux/module.h>
+#include <linux/platform_device.h>
+#include <linux/power_supply.h>
+#include <linux/i2c.h>
+#include <linux/input.h>
+#include <linux/delay.h>
+
+#include <linux/usb/typec_mux.h>
+#include <drm/drm_bridge.h>
+
+#define ASPIRE_EC_EVENT 0x05
+
+#define ASPIRE_EC_EVENT_WATCHDOG 0x20
+#define ASPIRE_EC_EVENT_KBD_BKL_ON 0x57
+#define ASPIRE_EC_EVENT_KBD_BKL_OFF 0x58
+#define ASPIRE_EC_EVENT_LID_CLOSE 0x9b
+#define ASPIRE_EC_EVENT_LID_OPEN 0x9c
+#define ASPIRE_EC_EVENT_BKL_UNBLANKED 0x9d
+#define ASPIRE_EC_EVENT_BKL_BLANKED 0x9e
+#define ASPIRE_EC_EVENT_FG_INF_CHG 0x85
+#define ASPIRE_EC_EVENT_FG_STA_CHG 0xc6
+#define ASPIRE_EC_EVENT_HPD_DIS 0xa3
+#define ASPIRE_EC_EVENT_HPD_CON 0xa4
+
+#define ASPIRE_EC_FG_DYNAMIC 0x07
+#define ASPIRE_EC_FG_STATIC 0x08
+
+#define ASPIRE_EC_FG_FLAG_PRESENT BIT(0)
+#define ASPIRE_EC_FG_FLAG_FULL BIT(1)
+#define ASPIRE_EC_FG_FLAG_DISCHARGING BIT(2)
+#define ASPIRE_EC_FG_FLAG_CHARGING BIT(3)
+
+#define ASPIRE_EC_RAM_READ 0x20
+#define ASPIRE_EC_RAM_WRITE 0x21
+
+#define ASPIRE_EC_RAM_WATCHDOG 0x19
+#define ASPIRE_EC_RAM_KBD_MODE 0x43
+
+#define ASPIRE_EC_RAM_KBD_FN_EN BIT(0)
+#define ASPIRE_EC_RAM_KBD_MEDIA_ON_TOP BIT(5)
+#define ASPIRE_EC_RAM_KBD_ALWAYS_SET BIT(6)
+#define ASPIRE_EC_RAM_KBD_NUM_LAYER_EN BIT(7)
+
+#define ASPIRE_EC_RAM_KBD_MODE_2 0x60
+
+#define ASPIRE_EC_RAM_KBD_MEDIA_NOTIFY BIT(3)
+
+#define ASPIRE_EC_RAM_HPD_STATUS 0xf4
+#define ASPIRE_EC_HPD_CONNECTED 0x03
+
+#define ASPIRE_EC_RAM_LID_STATUS 0x4c
+#define ASPIRE_EC_LID_OPEN BIT(6)
+
+#define ASPIRE_EC_RAM_ADP 0x40
+#define ASPIRE_EC_AC_STATUS BIT(0)
+
+struct aspire_ec {
+ struct i2c_client *client;
+ struct power_supply *bat_psy;
+ struct power_supply *adp_psy;
+ struct input_dev *idev;
+
+ bool bridge_configured;
+ struct drm_bridge bridge;
+ struct work_struct work;
+};
+
+static int aspire_ec_ram_read(struct i2c_client *client, u8 off, u8 *data, u8 data_len)
+{
+ i2c_smbus_write_byte_data(client, ASPIRE_EC_RAM_READ, off);
+ i2c_smbus_read_i2c_block_data(client, ASPIRE_EC_RAM_READ, data_len, data);
+ return 0;
+}
+
+static int aspire_ec_ram_write(struct i2c_client *client, u8 off, u8 data)
+{
+ u8 tmp[2] = {off, data};
+
+ i2c_smbus_write_i2c_block_data(client, ASPIRE_EC_RAM_WRITE, sizeof(tmp), tmp);
+ return 0;
+}
+
+static irqreturn_t aspire_ec_irq_handler(int irq, void *data)
+{
+ struct aspire_ec *ec = data;
+ int id;
+ u8 tmp;
+
+ /*
+ * The original ACPI firmware actually has a small sleep in the handler.
+ *
+ * It seems like in most cases it's not needed but when the device
+ * just exits suspend, our i2c driver has a brief time where data
+ * transfer is not possible yet. So this delay allows us to suppress
+ * quite a bunch of spurious error messages in dmesg. Thus it's kept.
+ */
+ usleep_range(15000, 30000);
+
+ id = i2c_smbus_read_byte_data(ec->client, ASPIRE_EC_EVENT);
+ if (id < 0) {
+ dev_err(&ec->client->dev, "Failed to read event id: %pe\n", ERR_PTR(id));
+ return IRQ_HANDLED;
+ }
+
+ switch (id) {
+ case 0x0: /* No event */
+ break;
+
+ case ASPIRE_EC_EVENT_WATCHDOG:
+ /*
+ * Here acpi responds to the event and clears some bit.
+ * Notify (\_SB.I2C3.BAT1, 0x81) // Information Change
+ * Notify (\_SB.I2C3.ADP1, 0x80) // Status Change
+ */
+ aspire_ec_ram_read(ec->client, ASPIRE_EC_RAM_WATCHDOG, &tmp, sizeof(tmp));
+ aspire_ec_ram_write(ec->client, ASPIRE_EC_RAM_WATCHDOG, tmp & 0xbf);
+ break;
+
+ case ASPIRE_EC_EVENT_LID_CLOSE:
+ /* Notify (\_SB.LID0, 0x80) // Status Change */
+ input_report_switch(ec->idev, SW_LID, 1);
+ input_sync(ec->idev);
+ break;
+
+ case ASPIRE_EC_EVENT_LID_OPEN:
+ /* Notify (\_SB.LID0, 0x80) // Status Change */
+ input_report_switch(ec->idev, SW_LID, 0);
+ input_sync(ec->idev);
+ break;
+
+ case ASPIRE_EC_EVENT_FG_INF_CHG:
+ /* Notify (\_SB.I2C3.BAT1, 0x81) // Information Change */
+ case ASPIRE_EC_EVENT_FG_STA_CHG:
+ /* Notify (\_SB.I2C3.BAT1, 0x80) // Status Change */
+ power_supply_changed(ec->bat_psy);
+ power_supply_changed(ec->adp_psy);
+ break;
+
+ case ASPIRE_EC_EVENT_HPD_DIS:
+ if (ec->bridge_configured)
+ drm_bridge_hpd_notify(&ec->bridge, connector_status_disconnected);
+ break;
+
+ case ASPIRE_EC_EVENT_HPD_CON:
+ if (ec->bridge_configured)
+ drm_bridge_hpd_notify(&ec->bridge, connector_status_connected);
+ break;
+
+ case ASPIRE_EC_EVENT_BKL_BLANKED:
+ case ASPIRE_EC_EVENT_BKL_UNBLANKED:
+ /* Display backlight blanked on FN+F6. No action needed. */
+ break;
+
+ case ASPIRE_EC_EVENT_KBD_BKL_ON:
+ case ASPIRE_EC_EVENT_KBD_BKL_OFF:
+ /*
+ * There is a keyboard backlight connector on Aspire 1 that is
+ * controlled by FN+F8. There is no kb backlight on the device though.
+ * Seems like this is used on other devices like Acer Spin 7.
+ * No action needed.
+ */
+ break;
+
+ default:
+ dev_warn(&ec->client->dev, "Unknown event id=0x%x\n", id);
+ }
+
+ return IRQ_HANDLED;
+}
+
+/*
+ * Power supply.
+ */
+
+struct aspire_ec_bat_psy_static_data {
+ u8 unk1;
+ u8 flags;
+ __le16 unk2;
+ __le16 voltage_design;
+ __le16 capacity_full;
+ __le16 unk3;
+ __le16 serial;
+ u8 model_id;
+ u8 vendor_id;
+} __packed;
+
+static const char * const aspire_ec_bat_psy_battery_model[] = {
+ "AP18C4K",
+ "AP18C8K",
+ "AP19B8K",
+ "AP16M4J",
+ "AP16M5J",
+};
+
+static const char * const aspire_ec_bat_psy_battery_vendor[] = {
+ "SANYO",
+ "SONY",
+ "PANASONIC",
+ "SAMSUNG",
+ "SIMPLO",
+ "MOTOROLA",
+ "CELXPERT",
+ "LGC",
+ "GETAC",
+ "MURATA",
+};
+
+struct aspire_ec_bat_psy_dynamic_data {
+ u8 unk1;
+ u8 flags;
+ u8 unk2;
+ __le16 capacity_now;
+ __le16 voltage_now;
+ __le16 current_now;
+ __le16 unk3;
+ __le16 unk4;
+} __packed;
+
+static int aspire_ec_bat_psy_get_property(struct power_supply *psy,
+ enum power_supply_property psp,
+ union power_supply_propval *val)
+{
+ struct aspire_ec *ec = power_supply_get_drvdata(psy);
+ struct aspire_ec_bat_psy_static_data sdat;
+ struct aspire_ec_bat_psy_dynamic_data ddat;
+
+ i2c_smbus_read_i2c_block_data(ec->client, ASPIRE_EC_FG_STATIC, sizeof(sdat), (u8 *)&sdat);
+ i2c_smbus_read_i2c_block_data(ec->client, ASPIRE_EC_FG_DYNAMIC, sizeof(ddat), (u8 *)&ddat);
+
+ switch (psp) {
+ case POWER_SUPPLY_PROP_STATUS:
+ val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
+ if (ddat.flags & ASPIRE_EC_FG_FLAG_CHARGING)
+ val->intval = POWER_SUPPLY_STATUS_CHARGING;
+ else if (ddat.flags & ASPIRE_EC_FG_FLAG_DISCHARGING)
+ val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
+ else if (ddat.flags & ASPIRE_EC_FG_FLAG_FULL)
+ val->intval = POWER_SUPPLY_STATUS_FULL;
+ break;
+
+ case POWER_SUPPLY_PROP_VOLTAGE_NOW:
+ val->intval = le16_to_cpu(ddat.voltage_now) * 1000;
+ break;
+
+ case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
+ val->intval = le16_to_cpu(sdat.voltage_design) * 1000;
+ break;
+
+ case POWER_SUPPLY_PROP_CHARGE_NOW:
+ val->intval = le16_to_cpu(ddat.capacity_now) * 1000;
+ break;
+
+ case POWER_SUPPLY_PROP_CHARGE_FULL:
+ val->intval = le16_to_cpu(sdat.capacity_full) * 1000;
+ break;
+
+ case POWER_SUPPLY_PROP_CAPACITY:
+ val->intval = le16_to_cpu(ddat.capacity_now) * 100;
+ val->intval /= le16_to_cpu(sdat.capacity_full);
+ break;
+
+ case POWER_SUPPLY_PROP_CURRENT_NOW:
+ val->intval = (s16)le16_to_cpu(ddat.current_now) * 1000;
+ break;
+
+ case POWER_SUPPLY_PROP_PRESENT:
+ val->intval = !!(ddat.flags & ASPIRE_EC_FG_FLAG_PRESENT);
+ break;
+
+ case POWER_SUPPLY_PROP_SCOPE:
+ val->intval = POWER_SUPPLY_SCOPE_SYSTEM;
+ break;
+
+ case POWER_SUPPLY_PROP_MODEL_NAME:
+ if (sdat.model_id - 1 < ARRAY_SIZE(aspire_ec_bat_psy_battery_model))
+ val->strval = aspire_ec_bat_psy_battery_model[sdat.model_id - 1];
+ else
+ val->strval = "Unknown";
+ break;
+
+ case POWER_SUPPLY_PROP_MANUFACTURER:
+ if (sdat.vendor_id - 3 < ARRAY_SIZE(aspire_ec_bat_psy_battery_vendor))
+ val->strval = aspire_ec_bat_psy_battery_vendor[sdat.vendor_id - 3];
+ else
+ val->strval = "Unknown";
+ break;
+
+ default:
+ return -EINVAL;
+ }
+
+ return 0;
+}
+
+static enum power_supply_property aspire_ec_bat_psy_props[] = {
+ POWER_SUPPLY_PROP_STATUS,
+ POWER_SUPPLY_PROP_VOLTAGE_NOW,
+ POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
+ POWER_SUPPLY_PROP_CHARGE_NOW,
+ POWER_SUPPLY_PROP_CHARGE_FULL,
+ POWER_SUPPLY_PROP_CAPACITY,
+ POWER_SUPPLY_PROP_CURRENT_NOW,
+ POWER_SUPPLY_PROP_PRESENT,
+ POWER_SUPPLY_PROP_SCOPE,
+ POWER_SUPPLY_PROP_MODEL_NAME,
+ POWER_SUPPLY_PROP_MANUFACTURER,
+};
+
+static const struct power_supply_desc aspire_ec_bat_psy_desc = {
+ .name = "aspire-ec-bat",
+ .type = POWER_SUPPLY_TYPE_BATTERY,
+ .get_property = aspire_ec_bat_psy_get_property,
+ .properties = aspire_ec_bat_psy_props,
+ .num_properties = ARRAY_SIZE(aspire_ec_bat_psy_props),
+};
+
+static int aspire_ec_adp_psy_get_property(struct power_supply *psy,
+ enum power_supply_property psp,
+ union power_supply_propval *val)
+{
+ struct aspire_ec *ec = power_supply_get_drvdata(psy);
+ u8 tmp;
+
+ switch (psp) {
+ case POWER_SUPPLY_PROP_ONLINE:
+ aspire_ec_ram_read(ec->client, ASPIRE_EC_RAM_ADP, &tmp, sizeof(tmp));
+ val->intval = !!(tmp & ASPIRE_EC_AC_STATUS);
+ break;
+
+ default:
+ return -EINVAL;
+ }
+
+ return 0;
+}
+
+static enum power_supply_property aspire_ec_adp_psy_props[] = {
+ POWER_SUPPLY_PROP_ONLINE,
+};
+
+static const struct power_supply_desc aspire_ec_adp_psy_desc = {
+ .name = "aspire-ec-adp",
+ .type = POWER_SUPPLY_TYPE_MAINS,
+ .get_property = aspire_ec_adp_psy_get_property,
+ .properties = aspire_ec_adp_psy_props,
+ .num_properties = ARRAY_SIZE(aspire_ec_adp_psy_props),
+};
+
+/*
+ * USB-C DP Alt mode HPD.
+ */
+
+static int aspire_ec_bridge_attach(struct drm_bridge *bridge, enum drm_bridge_attach_flags flags)
+{
+ return flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR ? 0 : -EINVAL;
+}
+
+static void aspire_ec_bridge_update_hpd_work(struct work_struct *work)
+{
+ struct aspire_ec *ec = container_of(work, struct aspire_ec, work);
+ u8 tmp;
+
+ aspire_ec_ram_read(ec->client, ASPIRE_EC_RAM_HPD_STATUS, &tmp, sizeof(tmp));
+ if (tmp == ASPIRE_EC_HPD_CONNECTED)
+ drm_bridge_hpd_notify(&ec->bridge, connector_status_connected);
+ else
+ drm_bridge_hpd_notify(&ec->bridge, connector_status_disconnected);
+}
+
+static void aspire_ec_bridge_hpd_enable(struct drm_bridge *bridge)
+{
+ struct aspire_ec *ec = container_of(bridge, struct aspire_ec, bridge);
+
+ schedule_work(&ec->work);
+}
+
+static const struct drm_bridge_funcs aspire_ec_bridge_funcs = {
+ .hpd_enable = aspire_ec_bridge_hpd_enable,
+ .attach = aspire_ec_bridge_attach,
+};
+
+/*
+ * Sysfs attributes.
+ */
+
+static ssize_t fn_lock_show(struct device *dev, struct device_attribute *attr, char *buf)
+{
+ struct aspire_ec *ec = i2c_get_clientdata(to_i2c_client(dev));
+ u8 tmp;
+
+ aspire_ec_ram_read(ec->client, ASPIRE_EC_RAM_KBD_MODE, &tmp, sizeof(tmp));
+
+ return sysfs_emit(buf, "%d\n", !(tmp & ASPIRE_EC_RAM_KBD_MEDIA_ON_TOP));
+}
+
+static ssize_t fn_lock_store(struct device *dev, struct device_attribute *attr,
+ const char *buf, size_t count)
+{
+ struct aspire_ec *ec = i2c_get_clientdata(to_i2c_client(dev));
+ u8 tmp;
+
+ bool state;
+ int ret;
+
+ ret = kstrtobool(buf, &state);
+ if (ret)
+ return ret;
+
+ aspire_ec_ram_read(ec->client, ASPIRE_EC_RAM_KBD_MODE, &tmp, sizeof(tmp));
+
+ if (state)
+ tmp &= ~ASPIRE_EC_RAM_KBD_MEDIA_ON_TOP;
+ else
+ tmp |= ASPIRE_EC_RAM_KBD_MEDIA_ON_TOP;
+
+ aspire_ec_ram_write(ec->client, ASPIRE_EC_RAM_KBD_MODE, tmp);
+
+ return count;
+}
+
+static DEVICE_ATTR_RW(fn_lock);
+
+static struct attribute *aspire_ec_attributes[] = {
+ &dev_attr_fn_lock.attr,
+ NULL
+};
+
+static const struct attribute_group aspire_ec_attribute_group = {
+ .attrs = aspire_ec_attributes
+};
+
+static int aspire_ec_probe(struct i2c_client *client)
+{
+ struct power_supply_config psy_cfg = {0};
+ struct device *dev = &client->dev;
+ struct fwnode_handle *fwnode;
+ struct aspire_ec *ec;
+ int ret;
+ u8 tmp;
+
+ ec = devm_kzalloc(dev, sizeof(*ec), GFP_KERNEL);
+ if (!ec)
+ return -ENOMEM;
+
+ ec->client = client;
+ i2c_set_clientdata(client, ec);
+
+ /* Battery status reports */
+ psy_cfg.drv_data = ec;
+ ec->bat_psy = devm_power_supply_register(dev, &aspire_ec_bat_psy_desc, &psy_cfg);
+ if (IS_ERR(ec->bat_psy))
+ return dev_err_probe(dev, PTR_ERR(ec->bat_psy),
+ "Failed to register battery power supply\n");
+
+ ec->adp_psy = devm_power_supply_register(dev, &aspire_ec_adp_psy_desc, &psy_cfg);
+ if (IS_ERR(ec->adp_psy))
+ return dev_err_probe(dev, PTR_ERR(ec->adp_psy),
+ "Failed to register ac power supply\n");
+
+ /* Lid switch */
+ ec->idev = devm_input_allocate_device(dev);
+ if (!ec->idev)
+ return -ENOMEM;
+
+ ec->idev->name = "aspire-ec";
+ ec->idev->phys = "aspire-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, "Input device register failed\n");
+
+ /* Enable the keyboard fn keys */
+ tmp = ASPIRE_EC_RAM_KBD_FN_EN | ASPIRE_EC_RAM_KBD_ALWAYS_SET;
+ tmp |= ASPIRE_EC_RAM_KBD_MEDIA_ON_TOP;
+ aspire_ec_ram_write(client, ASPIRE_EC_RAM_KBD_MODE, tmp);
+
+ aspire_ec_ram_read(client, ASPIRE_EC_RAM_KBD_MODE_2, &tmp, sizeof(tmp));
+ tmp |= ASPIRE_EC_RAM_KBD_MEDIA_NOTIFY;
+ aspire_ec_ram_write(client, ASPIRE_EC_RAM_KBD_MODE_2, tmp);
+
+ ret = devm_device_add_group(dev, &aspire_ec_attribute_group);
+ if (ret)
+ return dev_err_probe(dev, ret, "Failed to create attribute group\n");
+
+ /* External Type-C display attach reports */
+ fwnode = device_get_named_child_node(dev, "connector");
+ if (fwnode) {
+ INIT_WORK(&ec->work, aspire_ec_bridge_update_hpd_work);
+ ec->bridge.funcs = &aspire_ec_bridge_funcs;
+ ec->bridge.of_node = to_of_node(fwnode);
+ ec->bridge.ops = DRM_BRIDGE_OP_HPD;
+ ec->bridge.type = DRM_MODE_CONNECTOR_USB;
+
+ ret = devm_drm_bridge_add(dev, &ec->bridge);
+ if (ret) {
+ fwnode_handle_put(fwnode);
+ return dev_err_probe(dev, ret, "Failed to register drm bridge\n");
+ }
+
+ ec->bridge_configured = true;
+ }
+
+ ret = devm_request_threaded_irq(dev, client->irq, NULL,
+ aspire_ec_irq_handler, IRQF_ONESHOT,
+ dev_name(dev), ec);
+ if (ret)
+ return dev_err_probe(dev, ret, "Failed to request irq\n");
+
+ return 0;
+}
+
+static int aspire_ec_resume(struct device *dev)
+{
+ struct aspire_ec *ec = i2c_get_clientdata(to_i2c_client(dev));
+ u8 tmp;
+
+ aspire_ec_ram_read(ec->client, ASPIRE_EC_RAM_LID_STATUS, &tmp, sizeof(tmp));
+ input_report_switch(ec->idev, SW_LID, !!(tmp & ASPIRE_EC_LID_OPEN));
+ input_sync(ec->idev);
+
+ return 0;
+}
+
+static const struct i2c_device_id aspire_ec_id[] = {
+ { "aspire1-ec", },
+ { }
+};
+MODULE_DEVICE_TABLE(i2c, aspire_ec_id);
+
+static const struct of_device_id aspire_ec_of_match[] = {
+ { .compatible = "acer,aspire1-ec", },
+ { }
+};
+MODULE_DEVICE_TABLE(of, aspire_ec_of_match);
+
+static DEFINE_SIMPLE_DEV_PM_OPS(aspire_ec_pm_ops, NULL, aspire_ec_resume);
+
+static struct i2c_driver aspire_ec_driver = {
+ .driver = {
+ .name = "aspire-ec",
+ .of_match_table = aspire_ec_of_match,
+ .pm = pm_sleep_ptr(&aspire_ec_pm_ops),
+ },
+ .probe = aspire_ec_probe,
+ .id_table = aspire_ec_id,
+};
+module_i2c_driver(aspire_ec_driver);
+
+MODULE_DESCRIPTION("Acer Aspire 1 embedded controller");
+MODULE_AUTHOR("Nikita Travkin <[email protected]>");
+MODULE_LICENSE("GPL");

--
2.43.2


2024-03-12 09:03:37

by Nikita Travkin

[permalink] [raw]
Subject: [PATCH v4 4/4] arm64: dts: qcom: acer-aspire1: Add embedded controller

The laptop contains an embedded controller that provides a set of
features:

- Battery and charger monitoring
- USB Type-C DP alt mode HPD monitoring
- Lid status detection
- Small amount of keyboard configuration*

[*] The keyboard is handled by the same EC but it has a dedicated i2c
bus and is already enabled. This port only provides fn key behavior
configuration.

Add the EC to the device tree and describe the relationship between the
EC-managed type-c port and the SoC DisplayPort.

Reviewed-by: Konrad Dybcio <[email protected]>
Signed-off-by: Nikita Travkin <[email protected]>
---
arch/arm64/boot/dts/qcom/sc7180-acer-aspire1.dts | 40 +++++++++++++++++++++++-
1 file changed, 39 insertions(+), 1 deletion(-)

diff --git a/arch/arm64/boot/dts/qcom/sc7180-acer-aspire1.dts b/arch/arm64/boot/dts/qcom/sc7180-acer-aspire1.dts
index 5afcb8212f49..3f0d3e33894a 100644
--- a/arch/arm64/boot/dts/qcom/sc7180-acer-aspire1.dts
+++ b/arch/arm64/boot/dts/qcom/sc7180-acer-aspire1.dts
@@ -255,7 +255,25 @@ &i2c2 {
clock-frequency = <400000>;
status = "okay";

- /* embedded-controller@76 */
+ embedded-controller@76 {
+ compatible = "acer,aspire1-ec";
+ reg = <0x76>;
+
+ interrupts-extended = <&tlmm 30 IRQ_TYPE_LEVEL_LOW>;
+
+ pinctrl-0 = <&ec_int_default>;
+ pinctrl-names = "default";
+
+ connector {
+ compatible = "usb-c-connector";
+
+ port {
+ ec_dp_in: endpoint {
+ remote-endpoint = <&mdss_dp_out>;
+ };
+ };
+ };
+ };
};

&i2c4 {
@@ -419,6 +437,19 @@ &mdss {
status = "okay";
};

+&mdss_dp {
+ data-lanes = <0 1>;
+
+ vdda-1p2-supply = <&vreg_l3c_1p2>;
+ vdda-0p9-supply = <&vreg_l4a_0p8>;
+
+ status = "okay";
+};
+
+&mdss_dp_out {
+ remote-endpoint = <&ec_dp_in>;
+};
+
&mdss_dsi0 {
vdda-supply = <&vreg_l3c_1p2>;
status = "okay";
@@ -857,6 +888,13 @@ codec_irq_default: codec-irq-deault-state {
bias-disable;
};

+ ec_int_default: ec-int-default-state {
+ pins = "gpio30";
+ function = "gpio";
+ drive-strength = <2>;
+ bias-disable;
+ };
+
edp_bridge_irq_default: edp-bridge-irq-default-state {
pins = "gpio11";
function = "gpio";

--
2.43.2


2024-03-12 11:37:06

by Bryan O'Donoghue

[permalink] [raw]
Subject: Re: [PATCH v4 2/4] platform: Add ARM64 platform directory

On 12/03/2024 08:42, Nikita Travkin wrote:
> Some ARM64 based laptops and computers require vendor/board specific
> drivers for their embedded controllers. Even though usually the most
> important functionality of those devices is implemented inside ACPI,
> unfortunately Linux doesn't currently have great support for ACPI on
> platforms like Qualcomm Snapdragon that are used in most ARM64 laptops
> today. Instead Linux relies on Device Tree for Qualcomm based devices
> and it's significantly easier to reimplement the EC functionality in
> a dedicated driver than to make use of ACPI code.
>
> This commit introduces a new platform/arm64 subdirectory to give a
> place to such drivers for EC-like devices.
>
> A new MAINTAINERS entry is added for this directory. Patches to files in
> this directory will be taken up by the platform-drivers-x86 team (i.e.
> Hans de Goede and Mark Gross).
>
> Signed-off-by: Nikita Travkin <[email protected]>
> ---
> MAINTAINERS | 9 +++++++++
> drivers/platform/Kconfig | 2 ++
> drivers/platform/Makefile | 1 +
> drivers/platform/arm64/Kconfig | 19 +++++++++++++++++++
> drivers/platform/arm64/Makefile | 6 ++++++
> 5 files changed, 37 insertions(+)
>
> diff --git a/MAINTAINERS b/MAINTAINERS
> index b43102ca365d..ec8d706a99aa 100644
> --- a/MAINTAINERS
> +++ b/MAINTAINERS
> @@ -3050,6 +3050,15 @@ F: drivers/mmc/host/sdhci-of-arasan.c
> N: zynq
> N: xilinx
>
> +ARM64 PLATFORM DRIVERS
> +M: Hans de Goede <[email protected]>
> +M: Ilpo Järvinen <[email protected]>
> +L: [email protected]
> +S: Maintained
> +Q: https://patchwork.kernel.org/project/platform-driver-x86/list/
> +T: git git://git.kernel.org/pub/scm/linux/kernel/git/pdx86/platform-drivers-x86.git

Surely some sort of Arm specific mailing list should be added here ?
platform-drivers-x86 for arm64 platform drivers standalone, makes little
sense.

Perhaps for each new SoC class added - you could add the appropriate
mailing list linux-arm-msm is suspiciously missing from the list even
though the only driver that will live in this directory after this
series is a qcom based device.

And if tomorrow someone added a Rockchip based EC controller then you'd
assume the rockchip mailing list should get a ping.

---
bod

2024-03-12 11:56:17

by Nikita Travkin

[permalink] [raw]
Subject: Re: [PATCH v4 2/4] platform: Add ARM64 platform directory

Bryan O'Donoghue писал(а) 12.03.2024 16:36:
> On 12/03/2024 08:42, Nikita Travkin wrote:
>> Some ARM64 based laptops and computers require vendor/board specific
>> drivers for their embedded controllers. Even though usually the most
>> important functionality of those devices is implemented inside ACPI,
>> unfortunately Linux doesn't currently have great support for ACPI on
>> platforms like Qualcomm Snapdragon that are used in most ARM64 laptops
>> today. Instead Linux relies on Device Tree for Qualcomm based devices
>> and it's significantly easier to reimplement the EC functionality in
>> a dedicated driver than to make use of ACPI code.
>>
>> This commit introduces a new platform/arm64 subdirectory to give a
>> place to such drivers for EC-like devices.
>>
>> A new MAINTAINERS entry is added for this directory. Patches to files in
>> this directory will be taken up by the platform-drivers-x86 team (i.e.
>> Hans de Goede and Mark Gross).
>>
>> Signed-off-by: Nikita Travkin <[email protected]>
>> ---
>> MAINTAINERS | 9 +++++++++
>> drivers/platform/Kconfig | 2 ++
>> drivers/platform/Makefile | 1 +
>> drivers/platform/arm64/Kconfig | 19 +++++++++++++++++++
>> drivers/platform/arm64/Makefile | 6 ++++++
>> 5 files changed, 37 insertions(+)
>>
>> diff --git a/MAINTAINERS b/MAINTAINERS
>> index b43102ca365d..ec8d706a99aa 100644
>> --- a/MAINTAINERS
>> +++ b/MAINTAINERS
>> @@ -3050,6 +3050,15 @@ F: drivers/mmc/host/sdhci-of-arasan.c
>> N: zynq
>> N: xilinx
>> +ARM64 PLATFORM DRIVERS
>> +M: Hans de Goede <[email protected]>
>> +M: Ilpo Järvinen <[email protected]>
>> +L: [email protected]
>> +S: Maintained
>> +Q: https://patchwork.kernel.org/project/platform-driver-x86/list/
>> +T: git git://git.kernel.org/pub/scm/linux/kernel/git/pdx86/platform-drivers-x86.git
>
> Surely some sort of Arm specific mailing list should be added here ? platform-drivers-x86 for arm64 platform drivers standalone, makes little sense.
>

We agreed with Hans that pdx86 list/tree can work for EC drivers for
other platforms because many maintainers familiar with ECs through x86
are already there.

> Perhaps for each new SoC class added - you could add the appropriate mailing list linux-arm-msm is suspiciously missing from the list even though the only driver that will live in this directory after this series is a qcom based device.
>
> And if tomorrow someone added a Rockchip based EC controller then you'd assume the rockchip mailing list should get a ping.

I believe that even though those drivers are "board specific" (Hans
asked to only include EC drivers here, and we have soc/ for other things
anyway) they are not at all "platform"/"soc" specific, so I'm not sure
adding arm lists here is a great idea:

I don't think these drivers would be too specific to the SoC given it's
just an i2c peripheral most of the time, and considering that it seems
there soon will be many WoA devices with socs from many vendors, we would
just have a collection of all the arm platform lists here...

So even if for now, while all existing WoA devices use Snapdragon chips,
we could get away with adding linux-arm-msm, it may end up just spamming
all the platform lists for no reason when the list grows...

I think it's better for the contributors to CC the relevant list for
their board themselves, which is easily done implicitly by adding dts
changes along the way, like in this series.

Of course if you and other people on linux-arm-msm are fine with that
possibility, we could add an extra list there and see if it gets out of
hand.

Nikita

>
> ---
> bod

2024-03-12 11:58:36

by Bryan O'Donoghue

[permalink] [raw]
Subject: Re: [PATCH v4 3/4] platform: arm64: Add Acer Aspire 1 embedded controller driver

On 12/03/2024 08:42, Nikita Travkin wrote:
> Acer Aspire 1 is a Snapdragon 7c based laptop. It uses an embedded
> controller to perform a set of various functions, such as:
>
> - Battery and charger monitoring;
> - Keyboard layout control (i.e. fn_lock settings);
> - USB Type-C DP alt mode HPD notifications;
> - Laptop lid status.
>
> Unfortunately, while all this functionality is implemented in ACPI, it's
> currently not possible to use ACPI to boot Linux on such Qualcomm
> devices. To allow Linux to still support the features provided by EC,
> this driver reimplments the relevant ACPI parts. This allows us to boot
> the laptop with Device Tree and retain all the features.
>
> Signed-off-by: Nikita Travkin <[email protected]>
> ---
> drivers/platform/arm64/Kconfig | 16 +
> drivers/platform/arm64/Makefile | 2 +
> drivers/platform/arm64/acer-aspire1-ec.c | 555 +++++++++++++++++++++++++++++++

You should be listing yourself as a maintainer for a driver you contribute.

Good work BTW.

> 3 files changed, 573 insertions(+)
>
> diff --git a/drivers/platform/arm64/Kconfig b/drivers/platform/arm64/Kconfig
> index 644b83ede093..07d47879a9e3 100644
> --- a/drivers/platform/arm64/Kconfig
> +++ b/drivers/platform/arm64/Kconfig
> @@ -16,4 +16,20 @@ menuconfig ARM64_PLATFORM_DEVICES
>
> if ARM64_PLATFORM_DEVICES
>
> +config EC_ACER_ASPIRE1
> + tristate "Acer Aspire 1 Emedded Controller driver"
> + depends on I2C
> + depends on DRM
> + depends on POWER_SUPPLY
> + depends on INPUT
> + help
> + Say Y here to enable the EC driver for the (Snapdragon-based)
> + Acer Aspire 1 laptop. The EC handles battery and charging
> + monitoring as well as some misc functions like the lid sensor
> + and USB Type-C DP HPD events.
> +
> + This driver provides battery and AC status support for the mentioned
> + laptop where this information is not properly exposed via the
> + standard ACPI devices.
> +
> endif # ARM64_PLATFORM_DEVICES
> diff --git a/drivers/platform/arm64/Makefile b/drivers/platform/arm64/Makefile
> index f91cdc7155e2..4fcc9855579b 100644
> --- a/drivers/platform/arm64/Makefile
> +++ b/drivers/platform/arm64/Makefile
> @@ -4,3 +4,5 @@
> #
> # This dir should only include drivers for EC-like devices.
> #
> +
> +obj-$(CONFIG_EC_ACER_ASPIRE1) += acer-aspire1-ec.o
> diff --git a/drivers/platform/arm64/acer-aspire1-ec.c b/drivers/platform/arm64/acer-aspire1-ec.c
> new file mode 100644
> index 000000000000..3941e24c5c7c
> --- /dev/null
> +++ b/drivers/platform/arm64/acer-aspire1-ec.c
> @@ -0,0 +1,555 @@
> +// SPDX-License-Identifier: GPL-2.0-only
> +/* Copyright (c) 2023, Nikita Travkin <[email protected]> */

2024

> +
> +#include <linux/module.h>
> +#include <linux/platform_device.h>
> +#include <linux/power_supply.h>
> +#include <linux/i2c.h>
> +#include <linux/input.h>
> +#include <linux/delay.h>

This list should be alphabetised.

> +
> +#include <linux/usb/typec_mux.h>
> +#include <drm/drm_bridge.h>

Zap the newline and sort all of these headers alphabetically please.

> +
> +#define ASPIRE_EC_EVENT 0x05
> +
> +#define ASPIRE_EC_EVENT_WATCHDOG 0x20
> +#define ASPIRE_EC_EVENT_KBD_BKL_ON 0x57
> +#define ASPIRE_EC_EVENT_KBD_BKL_OFF 0x58
> +#define ASPIRE_EC_EVENT_LID_CLOSE 0x9b
> +#define ASPIRE_EC_EVENT_LID_OPEN 0x9c
> +#define ASPIRE_EC_EVENT_BKL_UNBLANKED 0x9d
> +#define ASPIRE_EC_EVENT_BKL_BLANKED 0x9e
> +#define ASPIRE_EC_EVENT_FG_INF_CHG 0x85
> +#define ASPIRE_EC_EVENT_FG_STA_CHG 0xc6
> +#define ASPIRE_EC_EVENT_HPD_DIS 0xa3
> +#define ASPIRE_EC_EVENT_HPD_CON 0xa4
> +
> +#define ASPIRE_EC_FG_DYNAMIC 0x07
> +#define ASPIRE_EC_FG_STATIC 0x08
> +
> +#define ASPIRE_EC_FG_FLAG_PRESENT BIT(0)
> +#define ASPIRE_EC_FG_FLAG_FULL BIT(1)
> +#define ASPIRE_EC_FG_FLAG_DISCHARGING BIT(2)
> +#define ASPIRE_EC_FG_FLAG_CHARGING BIT(3)
> +
> +#define ASPIRE_EC_RAM_READ 0x20
> +#define ASPIRE_EC_RAM_WRITE 0x21
> +
> +#define ASPIRE_EC_RAM_WATCHDOG 0x19
> +#define ASPIRE_EC_RAM_KBD_MODE 0x43
> +
> +#define ASPIRE_EC_RAM_KBD_FN_EN BIT(0)
> +#define ASPIRE_EC_RAM_KBD_MEDIA_ON_TOP BIT(5)
> +#define ASPIRE_EC_RAM_KBD_ALWAYS_SET BIT(6)
> +#define ASPIRE_EC_RAM_KBD_NUM_LAYER_EN BIT(7)
> +
> +#define ASPIRE_EC_RAM_KBD_MODE_2 0x60
> +
> +#define ASPIRE_EC_RAM_KBD_MEDIA_NOTIFY BIT(3)
> +
> +#define ASPIRE_EC_RAM_HPD_STATUS 0xf4
> +#define ASPIRE_EC_HPD_CONNECTED 0x03
> +
> +#define ASPIRE_EC_RAM_LID_STATUS 0x4c
> +#define ASPIRE_EC_LID_OPEN BIT(6)
> +
> +#define ASPIRE_EC_RAM_ADP 0x40
> +#define ASPIRE_EC_AC_STATUS BIT(0)
> +
> +struct aspire_ec {
> + struct i2c_client *client;
> + struct power_supply *bat_psy;
> + struct power_supply *adp_psy;
> + struct input_dev *idev;
> +
> + bool bridge_configured;
> + struct drm_bridge bridge;
> + struct work_struct work;
> +};
> +
> +static int aspire_ec_ram_read(struct i2c_client *client, u8 off, u8 *data, u8 data_len)
> +{
> + i2c_smbus_write_byte_data(client, ASPIRE_EC_RAM_READ, off);
> + i2c_smbus_read_i2c_block_data(client, ASPIRE_EC_RAM_READ, data_len, data);
> + return 0;
> +}
> +
> +static int aspire_ec_ram_write(struct i2c_client *client, u8 off, u8 data)
> +{
> + u8 tmp[2] = {off, data};
> +
> + i2c_smbus_write_i2c_block_data(client, ASPIRE_EC_RAM_WRITE, sizeof(tmp), tmp);
> + return 0;
> +}
> +
> +static irqreturn_t aspire_ec_irq_handler(int irq, void *data)
> +{
> + struct aspire_ec *ec = data;
> + int id;
> + u8 tmp;
> +
> + /*
> + * The original ACPI firmware actually has a small sleep in the handler.
> + *
> + * It seems like in most cases it's not needed but when the device
> + * just exits suspend, our i2c driver has a brief time where data
> + * transfer is not possible yet. So this delay allows us to suppress
> + * quite a bunch of spurious error messages in dmesg. Thus it's kept.
> + */
> + usleep_range(15000, 30000);
> +
> + id = i2c_smbus_read_byte_data(ec->client, ASPIRE_EC_EVENT);
> + if (id < 0) {
> + dev_err(&ec->client->dev, "Failed to read event id: %pe\n", ERR_PTR(id));
> + return IRQ_HANDLED;
> + }
> +
> + switch (id) {
> + case 0x0: /* No event */
> + break;
> +
> + case ASPIRE_EC_EVENT_WATCHDOG:
> + /*
> + * Here acpi responds to the event and clears some bit.
> + * Notify (\_SB.I2C3.BAT1, 0x81) // Information Change
> + * Notify (\_SB.I2C3.ADP1, 0x80) // Status Change
> + */
> + aspire_ec_ram_read(ec->client, ASPIRE_EC_RAM_WATCHDOG, &tmp, sizeof(tmp));
> + aspire_ec_ram_write(ec->client, ASPIRE_EC_RAM_WATCHDOG, tmp & 0xbf);
> + break;
> +
> + case ASPIRE_EC_EVENT_LID_CLOSE:
> + /* Notify (\_SB.LID0, 0x80) // Status Change */
> + input_report_switch(ec->idev, SW_LID, 1);
> + input_sync(ec->idev);
> + break;
> +
> + case ASPIRE_EC_EVENT_LID_OPEN:
> + /* Notify (\_SB.LID0, 0x80) // Status Change */
> + input_report_switch(ec->idev, SW_LID, 0);
> + input_sync(ec->idev);
> + break;
> +
> + case ASPIRE_EC_EVENT_FG_INF_CHG:
> + /* Notify (\_SB.I2C3.BAT1, 0x81) // Information Change */

fallthrough;

> + case ASPIRE_EC_EVENT_FG_STA_CHG:
> + /* Notify (\_SB.I2C3.BAT1, 0x80) // Status Change */
> + power_supply_changed(ec->bat_psy);
> + power_supply_changed(ec->adp_psy);
> + break;
> +
> + case ASPIRE_EC_EVENT_HPD_DIS:
> + if (ec->bridge_configured)
> + drm_bridge_hpd_notify(&ec->bridge, connector_status_disconnected);
> + break;
> +
> + case ASPIRE_EC_EVENT_HPD_CON:
> + if (ec->bridge_configured)
> + drm_bridge_hpd_notify(&ec->bridge, connector_status_connected);
> + break;
> +
> + case ASPIRE_EC_EVENT_BKL_BLANKED:
> + case ASPIRE_EC_EVENT_BKL_UNBLANKED:
> + /* Display backlight blanked on FN+F6. No action needed. */
> + break;
> +
> + case ASPIRE_EC_EVENT_KBD_BKL_ON:
> + case ASPIRE_EC_EVENT_KBD_BKL_OFF:
> + /*
> + * There is a keyboard backlight connector on Aspire 1 that is
> + * controlled by FN+F8. There is no kb backlight on the device though.
> + * Seems like this is used on other devices like Acer Spin 7.
> + * No action needed.
> + */
> + break;
> +
> + default:
> + dev_warn(&ec->client->dev, "Unknown event id=0x%x\n", id);
> + }
> +
> + return IRQ_HANDLED;
> +}
> +
> +/*
> + * Power supply.
> + */
> +
> +struct aspire_ec_bat_psy_static_data {
> + u8 unk1;
> + u8 flags;
> + __le16 unk2;
> + __le16 voltage_design;
> + __le16 capacity_full;
> + __le16 unk3;
> + __le16 serial;
> + u8 model_id;
> + u8 vendor_id;
> +} __packed;
> +
> +static const char * const aspire_ec_bat_psy_battery_model[] = {
> + "AP18C4K",
> + "AP18C8K",
> + "AP19B8K",
> + "AP16M4J",
> + "AP16M5J",
> +};
> +
> +static const char * const aspire_ec_bat_psy_battery_vendor[] = {
> + "SANYO",
> + "SONY",
> + "PANASONIC",
> + "SAMSUNG",
> + "SIMPLO",
> + "MOTOROLA",
> + "CELXPERT",
> + "LGC",
> + "GETAC",
> + "MURATA",
> +};
> +
> +struct aspire_ec_bat_psy_dynamic_data {
> + u8 unk1;
> + u8 flags;
> + u8 unk2;
> + __le16 capacity_now;
> + __le16 voltage_now;
> + __le16 current_now;
> + __le16 unk3;
> + __le16 unk4;
> +} __packed;
> +
> +static int aspire_ec_bat_psy_get_property(struct power_supply *psy,
> + enum power_supply_property psp,
> + union power_supply_propval *val)
> +{
> + struct aspire_ec *ec = power_supply_get_drvdata(psy);
> + struct aspire_ec_bat_psy_static_data sdat;
> + struct aspire_ec_bat_psy_dynamic_data ddat;
> +
> + i2c_smbus_read_i2c_block_data(ec->client, ASPIRE_EC_FG_STATIC, sizeof(sdat), (u8 *)&sdat);
> + i2c_smbus_read_i2c_block_data(ec->client, ASPIRE_EC_FG_DYNAMIC, sizeof(ddat), (u8 *)&ddat);
> +
> + switch (psp) {
> + case POWER_SUPPLY_PROP_STATUS:
> + val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
> + if (ddat.flags & ASPIRE_EC_FG_FLAG_CHARGING)
> + val->intval = POWER_SUPPLY_STATUS_CHARGING;
> + else if (ddat.flags & ASPIRE_EC_FG_FLAG_DISCHARGING)
> + val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
> + else if (ddat.flags & ASPIRE_EC_FG_FLAG_FULL)
> + val->intval = POWER_SUPPLY_STATUS_FULL;
> + break;
> +
> + case POWER_SUPPLY_PROP_VOLTAGE_NOW:
> + val->intval = le16_to_cpu(ddat.voltage_now) * 1000;
> + break;
> +
> + case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
> + val->intval = le16_to_cpu(sdat.voltage_design) * 1000;
> + break;
> +
> + case POWER_SUPPLY_PROP_CHARGE_NOW:
> + val->intval = le16_to_cpu(ddat.capacity_now) * 1000;
> + break;
> +
> + case POWER_SUPPLY_PROP_CHARGE_FULL:
> + val->intval = le16_to_cpu(sdat.capacity_full) * 1000;
> + break;

You could stick this "* 1000" stuff in a macro

> +
> + case POWER_SUPPLY_PROP_CAPACITY:
> + val->intval = le16_to_cpu(ddat.capacity_now) * 100;
> + val->intval /= le16_to_cpu(sdat.capacity_full);
> + break;
> +
> + case POWER_SUPPLY_PROP_CURRENT_NOW:
> + val->intval = (s16)le16_to_cpu(ddat.current_now) * 1000;
> + break;
> +
> + case POWER_SUPPLY_PROP_PRESENT:
> + val->intval = !!(ddat.flags & ASPIRE_EC_FG_FLAG_PRESENT);
> + break;
> +
> + case POWER_SUPPLY_PROP_SCOPE:
> + val->intval = POWER_SUPPLY_SCOPE_SYSTEM;
> + break;
> +
> + case POWER_SUPPLY_PROP_MODEL_NAME:
> + if (sdat.model_id - 1 < ARRAY_SIZE(aspire_ec_bat_psy_battery_model))
> + val->strval = aspire_ec_bat_psy_battery_model[sdat.model_id - 1];
> + else
> + val->strval = "Unknown";
> + break;
> +
> + case POWER_SUPPLY_PROP_MANUFACTURER:
> + if (sdat.vendor_id - 3 < ARRAY_SIZE(aspire_ec_bat_psy_battery_vendor))
> + val->strval = aspire_ec_bat_psy_battery_vendor[sdat.vendor_id - 3];

How does this -3 offset not underflow ?

Seems a bit dodgy to me - can you add a comment to the code to explain ?
Its not immediately obvious the -3 is OK.

Also could you take an index instead of replicating the -value stepdown
each time ?

int myindex = sdat.model_id - 1;

if (myindex < someconstraint)
strval = somearry[myindex];

> + else
> + val->strval = "Unknown";
> + break;
> +
> + default:
> + return -EINVAL;
> + }
> +
> + return 0;
> +}
> +
> +static enum power_supply_property aspire_ec_bat_psy_props[] = {
> + POWER_SUPPLY_PROP_STATUS,
> + POWER_SUPPLY_PROP_VOLTAGE_NOW,
> + POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
> + POWER_SUPPLY_PROP_CHARGE_NOW,
> + POWER_SUPPLY_PROP_CHARGE_FULL,
> + POWER_SUPPLY_PROP_CAPACITY,
> + POWER_SUPPLY_PROP_CURRENT_NOW,
> + POWER_SUPPLY_PROP_PRESENT,
> + POWER_SUPPLY_PROP_SCOPE,
> + POWER_SUPPLY_PROP_MODEL_NAME,
> + POWER_SUPPLY_PROP_MANUFACTURER,
> +};
> +
> +static const struct power_supply_desc aspire_ec_bat_psy_desc = {
> + .name = "aspire-ec-bat",
> + .type = POWER_SUPPLY_TYPE_BATTERY,
> + .get_property = aspire_ec_bat_psy_get_property,
> + .properties = aspire_ec_bat_psy_props,
> + .num_properties = ARRAY_SIZE(aspire_ec_bat_psy_props),
> +};
> +
> +static int aspire_ec_adp_psy_get_property(struct power_supply *psy,
> + enum power_supply_property psp,
> + union power_supply_propval *val)
> +{
> + struct aspire_ec *ec = power_supply_get_drvdata(psy);
> + u8 tmp;
> +
> + switch (psp) {
> + case POWER_SUPPLY_PROP_ONLINE:
> + aspire_ec_ram_read(ec->client, ASPIRE_EC_RAM_ADP, &tmp, sizeof(tmp));
> + val->intval = !!(tmp & ASPIRE_EC_AC_STATUS);
> + break;
> +
> + default:
> + return -EINVAL;
> + }
> +
> + return 0;
> +}
> +
> +static enum power_supply_property aspire_ec_adp_psy_props[] = {
> + POWER_SUPPLY_PROP_ONLINE,
> +};
> +
> +static const struct power_supply_desc aspire_ec_adp_psy_desc = {
> + .name = "aspire-ec-adp",
> + .type = POWER_SUPPLY_TYPE_MAINS,
> + .get_property = aspire_ec_adp_psy_get_property,
> + .properties = aspire_ec_adp_psy_props,
> + .num_properties = ARRAY_SIZE(aspire_ec_adp_psy_props),
> +};
> +
> +/*
> + * USB-C DP Alt mode HPD.
> + */
> +
> +static int aspire_ec_bridge_attach(struct drm_bridge *bridge, enum drm_bridge_attach_flags flags)
> +{
> + return flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR ? 0 : -EINVAL;
> +}
> +
> +static void aspire_ec_bridge_update_hpd_work(struct work_struct *work)
> +{
> + struct aspire_ec *ec = container_of(work, struct aspire_ec, work);
> + u8 tmp;
> +
> + aspire_ec_ram_read(ec->client, ASPIRE_EC_RAM_HPD_STATUS, &tmp, sizeof(tmp));
> + if (tmp == ASPIRE_EC_HPD_CONNECTED)
> + drm_bridge_hpd_notify(&ec->bridge, connector_status_connected);
> + else
> + drm_bridge_hpd_notify(&ec->bridge, connector_status_disconnected);
> +}
> +
> +static void aspire_ec_bridge_hpd_enable(struct drm_bridge *bridge)
> +{
> + struct aspire_ec *ec = container_of(bridge, struct aspire_ec, bridge);
> +
> + schedule_work(&ec->work);
> +}
> +
> +static const struct drm_bridge_funcs aspire_ec_bridge_funcs = {
> + .hpd_enable = aspire_ec_bridge_hpd_enable,
> + .attach = aspire_ec_bridge_attach,
> +};
> +
> +/*
> + * Sysfs attributes.
> + */
> +
> +static ssize_t fn_lock_show(struct device *dev, struct device_attribute *attr, char *buf)
> +{
> + struct aspire_ec *ec = i2c_get_clientdata(to_i2c_client(dev));
> + u8 tmp;
> +
> + aspire_ec_ram_read(ec->client, ASPIRE_EC_RAM_KBD_MODE, &tmp, sizeof(tmp));
> +
> + return sysfs_emit(buf, "%d\n", !(tmp & ASPIRE_EC_RAM_KBD_MEDIA_ON_TOP));
> +}
> +
> +static ssize_t fn_lock_store(struct device *dev, struct device_attribute *attr,
> + const char *buf, size_t count)
> +{
> + struct aspire_ec *ec = i2c_get_clientdata(to_i2c_client(dev));
> + u8 tmp;
> +
> + bool state;
> + int ret;
> +
> + ret = kstrtobool(buf, &state);
> + if (ret)
> + return ret;
> +
> + aspire_ec_ram_read(ec->client, ASPIRE_EC_RAM_KBD_MODE, &tmp, sizeof(tmp));
> +
> + if (state)
> + tmp &= ~ASPIRE_EC_RAM_KBD_MEDIA_ON_TOP;
> + else
> + tmp |= ASPIRE_EC_RAM_KBD_MEDIA_ON_TOP;
> +
> + aspire_ec_ram_write(ec->client, ASPIRE_EC_RAM_KBD_MODE, tmp);
> +
> + return count;
> +}
> +
> +static DEVICE_ATTR_RW(fn_lock);
> +
> +static struct attribute *aspire_ec_attributes[] = {
> + &dev_attr_fn_lock.attr,
> + NULL
> +};
> +
> +static const struct attribute_group aspire_ec_attribute_group = {
> + .attrs = aspire_ec_attributes
> +};
> +
> +static int aspire_ec_probe(struct i2c_client *client)
> +{
> + struct power_supply_config psy_cfg = {0};
> + struct device *dev = &client->dev;
> + struct fwnode_handle *fwnode;
> + struct aspire_ec *ec;
> + int ret;
> + u8 tmp;
> +
> + ec = devm_kzalloc(dev, sizeof(*ec), GFP_KERNEL);
> + if (!ec)
> + return -ENOMEM;
> +
> + ec->client = client;
> + i2c_set_clientdata(client, ec);
> +
> + /* Battery status reports */
> + psy_cfg.drv_data = ec;
> + ec->bat_psy = devm_power_supply_register(dev, &aspire_ec_bat_psy_desc, &psy_cfg);
> + if (IS_ERR(ec->bat_psy))
> + return dev_err_probe(dev, PTR_ERR(ec->bat_psy),
> + "Failed to register battery power supply\n");
> +
> + ec->adp_psy = devm_power_supply_register(dev, &aspire_ec_adp_psy_desc, &psy_cfg);
> + if (IS_ERR(ec->adp_psy))
> + return dev_err_probe(dev, PTR_ERR(ec->adp_psy),
> + "Failed to register ac power supply\n");
> +
> + /* Lid switch */
> + ec->idev = devm_input_allocate_device(dev);
> + if (!ec->idev)
> + return -ENOMEM;
> +
> + ec->idev->name = "aspire-ec";
> + ec->idev->phys = "aspire-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, "Input device register failed\n");
> +
> + /* Enable the keyboard fn keys */
> + tmp = ASPIRE_EC_RAM_KBD_FN_EN | ASPIRE_EC_RAM_KBD_ALWAYS_SET;
> + tmp |= ASPIRE_EC_RAM_KBD_MEDIA_ON_TOP;
> + aspire_ec_ram_write(client, ASPIRE_EC_RAM_KBD_MODE, tmp);
> +
> + aspire_ec_ram_read(client, ASPIRE_EC_RAM_KBD_MODE_2, &tmp, sizeof(tmp));
> + tmp |= ASPIRE_EC_RAM_KBD_MEDIA_NOTIFY;
> + aspire_ec_ram_write(client, ASPIRE_EC_RAM_KBD_MODE_2, tmp);
> +
> + ret = devm_device_add_group(dev, &aspire_ec_attribute_group);
> + if (ret)

Don't you need input_unregister_device() in this case ? suggest a goto
jump label to clean up down the end of your probe() function.

> + return dev_err_probe(dev, ret, "Failed to create attribute group\n");
> +
> + /* External Type-C display attach reports */
> + fwnode = device_get_named_child_node(dev, "connector");
> + if (fwnode) {
> + INIT_WORK(&ec->work, aspire_ec_bridge_update_hpd_work);
> + ec->bridge.funcs = &aspire_ec_bridge_funcs;
> + ec->bridge.of_node = to_of_node(fwnode);
> + ec->bridge.ops = DRM_BRIDGE_OP_HPD;
> + ec->bridge.type = DRM_MODE_CONNECTOR_USB;
> +
> + ret = devm_drm_bridge_add(dev, &ec->bridge);
> + if (ret) {
> + fwnode_handle_put(fwnode);
> + return dev_err_probe(dev, ret, "Failed to register drm bridge\n");
> + }
> +
> + ec->bridge_configured = true;
> + }
> +
> + ret = devm_request_threaded_irq(dev, client->irq, NULL,
> + aspire_ec_irq_handler, IRQF_ONESHOT,
> + dev_name(dev), ec);
> + if (ret)
> + return dev_err_probe(dev, ret, "Failed to request irq\n");
> +
> + return 0;

jump label here for stuff that doens't just clean up

> +}
> +
> +static int aspire_ec_resume(struct device *dev)
> +{
> + struct aspire_ec *ec = i2c_get_clientdata(to_i2c_client(dev));
> + u8 tmp;
> +
> + aspire_ec_ram_read(ec->client, ASPIRE_EC_RAM_LID_STATUS, &tmp, sizeof(tmp));
> + input_report_switch(ec->idev, SW_LID, !!(tmp & ASPIRE_EC_LID_OPEN));
> + input_sync(ec->idev);
> +
> + return 0;
> +}
> +
> +static const struct i2c_device_id aspire_ec_id[] = {
> + { "aspire1-ec", },
> + { }
> +};
> +MODULE_DEVICE_TABLE(i2c, aspire_ec_id);
> +
> +static const struct of_device_id aspire_ec_of_match[] = {
> + { .compatible = "acer,aspire1-ec", },
> + { }
> +};
> +MODULE_DEVICE_TABLE(of, aspire_ec_of_match);
> +
> +static DEFINE_SIMPLE_DEV_PM_OPS(aspire_ec_pm_ops, NULL, aspire_ec_resume);
> +
> +static struct i2c_driver aspire_ec_driver = {
> + .driver = {
> + .name = "aspire-ec",
> + .of_match_table = aspire_ec_of_match,
> + .pm = pm_sleep_ptr(&aspire_ec_pm_ops),
> + },
> + .probe = aspire_ec_probe,
> + .id_table = aspire_ec_id,
> +};
> +module_i2c_driver(aspire_ec_driver);

Don't you need an exit function to clean up stuff that's not devm_ derived ?

input_unregister_device() ?

> +
> +MODULE_DESCRIPTION("Acer Aspire 1 embedded controller");
> +MODULE_AUTHOR("Nikita Travkin <[email protected]>");
> +MODULE_LICENSE("GPL");
>


2024-03-12 12:08:52

by Bryan O'Donoghue

[permalink] [raw]
Subject: Re: [PATCH v4 2/4] platform: Add ARM64 platform directory

On 12/03/2024 11:40, Ilpo Järvinen wrote:
> On Tue, 12 Mar 2024, Nikita Travkin wrote:
>
>> Some ARM64 based laptops and computers require vendor/board specific
>> drivers for their embedded controllers. Even though usually the most
>> important functionality of those devices is implemented inside ACPI,
>> unfortunately Linux doesn't currently have great support for ACPI on
>> platforms like Qualcomm Snapdragon that are used in most ARM64 laptops
>> today. Instead Linux relies on Device Tree for Qualcomm based devices
>> and it's significantly easier to reimplement the EC functionality in
>> a dedicated driver than to make use of ACPI code.
>>
>> This commit introduces a new platform/arm64 subdirectory to give a
>> place to such drivers for EC-like devices.
>>
>> A new MAINTAINERS entry is added for this directory. Patches to files in
>> this directory will be taken up by the platform-drivers-x86 team (i.e.
>> Hans de Goede and Mark Gross).
>
> Mark -> me.
>
>> +ARM64 PLATFORM DRIVERS
>> +M: Hans de Goede <[email protected]>
>> +M: Ilpo Järvinen <[email protected]>
>> +L: [email protected]
>> +S: Maintained
>> +Q: https://patchwork.kernel.org/project/platform-driver-x86/list/
>> +T: git git://git.kernel.org/pub/scm/linux/kernel/git/pdx86/platform-drivers-x86.git
>> +F: drivers/platform/arm64/
>
> Is some ARM64 person going to pay attention to these patches (you or
> perhaps somebody else)?
>
> It's perfectly fine to have some ARM64 person(s) listed as an additional
> maintainer there even if the patches themselves are routed through Hans
> and me (and pdx86 tree). With Mellanox and Surface platform drivers which
> are also routed through pdx86 tree, we have Hans + me + 3rd person listed
> as maintainers.

You can add me as a +R.

Perhaps Dmitry and Konrad would want to be on the list too.

Actually since Dmitry has already done some work on this, I think he
should be on the review list for this series.

Adding..

>
> (This is not to force anything on anyone but it could be beneficial if
> somebody more familiar with ARM64 is in the loop.)

---
bod

2024-03-12 12:13:02

by Nikita Travkin

[permalink] [raw]
Subject: Re: [PATCH v4 2/4] platform: Add ARM64 platform directory

Ilpo Järvinen писал(а) 12.03.2024 16:40:
> On Tue, 12 Mar 2024, Nikita Travkin wrote:
>
>> Some ARM64 based laptops and computers require vendor/board specific
>> drivers for their embedded controllers. Even though usually the most
>> important functionality of those devices is implemented inside ACPI,
>> unfortunately Linux doesn't currently have great support for ACPI on
>> platforms like Qualcomm Snapdragon that are used in most ARM64 laptops
>> today. Instead Linux relies on Device Tree for Qualcomm based devices
>> and it's significantly easier to reimplement the EC functionality in
>> a dedicated driver than to make use of ACPI code.
>>
>> This commit introduces a new platform/arm64 subdirectory to give a
>> place to such drivers for EC-like devices.
>>
>> A new MAINTAINERS entry is added for this directory. Patches to files in
>> this directory will be taken up by the platform-drivers-x86 team (i.e.
>> Hans de Goede and Mark Gross).
>
> Mark -> me.

Oops, copied without noticing that, sorry, will reword.

>
>> +ARM64 PLATFORM DRIVERS
>> +M: Hans de Goede <[email protected]>
>> +M: Ilpo Järvinen <[email protected]>
>> +L: [email protected]
>> +S: Maintained
>> +Q: https://patchwork.kernel.org/project/platform-driver-x86/list/
>> +T: git git://git.kernel.org/pub/scm/linux/kernel/git/pdx86/platform-drivers-x86.git
>> +F: drivers/platform/arm64/
>
> Is some ARM64 person going to pay attention to these patches (you or
> perhaps somebody else)?
>
> It's perfectly fine to have some ARM64 person(s) listed as an additional
> maintainer there even if the patches themselves are routed through Hans
> and me (and pdx86 tree). With Mellanox and Surface platform drivers which
> are also routed through pdx86 tree, we have Hans + me + 3rd person listed
> as maintainers.
>
> (This is not to force anything on anyone but it could be beneficial if
> somebody more familiar with ARM64 is in the loop.)

I've just replied to Bryan with my thoughts on this, but if you think
explicit arm list would be good here, we could indeed add linux-arm-msm
for now as Bryan suggested.

Nikita

2024-03-12 12:23:53

by Nikita Travkin

[permalink] [raw]
Subject: Re: [PATCH v4 3/4] platform: arm64: Add Acer Aspire 1 embedded controller driver

Bryan O'Donoghue писал(а) 12.03.2024 16:58:
> On 12/03/2024 08:42, Nikita Travkin wrote:
>> Acer Aspire 1 is a Snapdragon 7c based laptop. It uses an embedded
>> controller to perform a set of various functions, such as:
>>
>> - Battery and charger monitoring;
>> - Keyboard layout control (i.e. fn_lock settings);
>> - USB Type-C DP alt mode HPD notifications;
>> - Laptop lid status.
>>
>> Unfortunately, while all this functionality is implemented in ACPI, it's
>> currently not possible to use ACPI to boot Linux on such Qualcomm
>> devices. To allow Linux to still support the features provided by EC,
>> this driver reimplments the relevant ACPI parts. This allows us to boot
>> the laptop with Device Tree and retain all the features.
>>
>> Signed-off-by: Nikita Travkin <[email protected]>
>> ---
>> drivers/platform/arm64/Kconfig | 16 +
>> drivers/platform/arm64/Makefile | 2 +
>> drivers/platform/arm64/acer-aspire1-ec.c | 555 +++++++++++++++++++++++++++++++
>
> You should be listing yourself as a maintainer for a driver you contribute.

I always believed that being in the AUTHOR() at the bottom of the driver
would guarantee me being in CC for patches, which so far worked great,
thus I was always hesitent adding extra entries in MAINTAINERS.

>
> Good work BTW.
>
>> 3 files changed, 573 insertions(+)
>>
>> diff --git a/drivers/platform/arm64/Kconfig b/drivers/platform/arm64/Kconfig
>> index 644b83ede093..07d47879a9e3 100644
>> --- a/drivers/platform/arm64/Kconfig
>> +++ b/drivers/platform/arm64/Kconfig
>> @@ -16,4 +16,20 @@ menuconfig ARM64_PLATFORM_DEVICES
>> if ARM64_PLATFORM_DEVICES
>> +config EC_ACER_ASPIRE1
>> + tristate "Acer Aspire 1 Emedded Controller driver"
>> + depends on I2C
>> + depends on DRM
>> + depends on POWER_SUPPLY
>> + depends on INPUT
>> + help
>> + Say Y here to enable the EC driver for the (Snapdragon-based)
>> + Acer Aspire 1 laptop. The EC handles battery and charging
>> + monitoring as well as some misc functions like the lid sensor
>> + and USB Type-C DP HPD events.
>> +
>> + This driver provides battery and AC status support for the mentioned
>> + laptop where this information is not properly exposed via the
>> + standard ACPI devices.
>> +
>> endif # ARM64_PLATFORM_DEVICES
>> diff --git a/drivers/platform/arm64/Makefile b/drivers/platform/arm64/Makefile
>> index f91cdc7155e2..4fcc9855579b 100644
>> --- a/drivers/platform/arm64/Makefile
>> +++ b/drivers/platform/arm64/Makefile
>> @@ -4,3 +4,5 @@
>> #
>> # This dir should only include drivers for EC-like devices.
>> #
>> +
>> +obj-$(CONFIG_EC_ACER_ASPIRE1) += acer-aspire1-ec.o
>> diff --git a/drivers/platform/arm64/acer-aspire1-ec.c b/drivers/platform/arm64/acer-aspire1-ec.c
>> new file mode 100644
>> index 000000000000..3941e24c5c7c
>> --- /dev/null
>> +++ b/drivers/platform/arm64/acer-aspire1-ec.c
>> @@ -0,0 +1,555 @@
>> +// SPDX-License-Identifier: GPL-2.0-only
>> +/* Copyright (c) 2023, Nikita Travkin <[email protected]> */
>
> 2024
>

That driver was looong in the oven :D

>> +
>> +#include <linux/module.h>
>> +#include <linux/platform_device.h>
>> +#include <linux/power_supply.h>
>> +#include <linux/i2c.h>
>> +#include <linux/input.h>
>> +#include <linux/delay.h>
>
> This list should be alphabetised.
>
>> +
>> +#include <linux/usb/typec_mux.h>
>> +#include <drm/drm_bridge.h>
>
> Zap the newline and sort all of these headers alphabetically please.
>

Ack, will cleanup.

>> +
>> +#define ASPIRE_EC_EVENT 0x05
>> +
>> +#define ASPIRE_EC_EVENT_WATCHDOG 0x20
>> +#define ASPIRE_EC_EVENT_KBD_BKL_ON 0x57
>> +#define ASPIRE_EC_EVENT_KBD_BKL_OFF 0x58
>> +#define ASPIRE_EC_EVENT_LID_CLOSE 0x9b
>> +#define ASPIRE_EC_EVENT_LID_OPEN 0x9c
>> +#define ASPIRE_EC_EVENT_BKL_UNBLANKED 0x9d
>> +#define ASPIRE_EC_EVENT_BKL_BLANKED 0x9e
>> +#define ASPIRE_EC_EVENT_FG_INF_CHG 0x85
>> +#define ASPIRE_EC_EVENT_FG_STA_CHG 0xc6
>> +#define ASPIRE_EC_EVENT_HPD_DIS 0xa3
>> +#define ASPIRE_EC_EVENT_HPD_CON 0xa4
>> +
>> +#define ASPIRE_EC_FG_DYNAMIC 0x07
>> +#define ASPIRE_EC_FG_STATIC 0x08
>> +
>> +#define ASPIRE_EC_FG_FLAG_PRESENT BIT(0)
>> +#define ASPIRE_EC_FG_FLAG_FULL BIT(1)
>> +#define ASPIRE_EC_FG_FLAG_DISCHARGING BIT(2)
>> +#define ASPIRE_EC_FG_FLAG_CHARGING BIT(3)
>> +
>> +#define ASPIRE_EC_RAM_READ 0x20
>> +#define ASPIRE_EC_RAM_WRITE 0x21
>> +
>> +#define ASPIRE_EC_RAM_WATCHDOG 0x19
>> +#define ASPIRE_EC_RAM_KBD_MODE 0x43
>> +
>> +#define ASPIRE_EC_RAM_KBD_FN_EN BIT(0)
>> +#define ASPIRE_EC_RAM_KBD_MEDIA_ON_TOP BIT(5)
>> +#define ASPIRE_EC_RAM_KBD_ALWAYS_SET BIT(6)
>> +#define ASPIRE_EC_RAM_KBD_NUM_LAYER_EN BIT(7)
>> +
>> +#define ASPIRE_EC_RAM_KBD_MODE_2 0x60
>> +
>> +#define ASPIRE_EC_RAM_KBD_MEDIA_NOTIFY BIT(3)
>> +
>> +#define ASPIRE_EC_RAM_HPD_STATUS 0xf4
>> +#define ASPIRE_EC_HPD_CONNECTED 0x03
>> +
>> +#define ASPIRE_EC_RAM_LID_STATUS 0x4c
>> +#define ASPIRE_EC_LID_OPEN BIT(6)
>> +
>> +#define ASPIRE_EC_RAM_ADP 0x40
>> +#define ASPIRE_EC_AC_STATUS BIT(0)
>> +
>> +struct aspire_ec {
>> + struct i2c_client *client;
>> + struct power_supply *bat_psy;
>> + struct power_supply *adp_psy;
>> + struct input_dev *idev;
>> +
>> + bool bridge_configured;
>> + struct drm_bridge bridge;
>> + struct work_struct work;
>> +};
>> +
>> +static int aspire_ec_ram_read(struct i2c_client *client, u8 off, u8 *data, u8 data_len)
>> +{
>> + i2c_smbus_write_byte_data(client, ASPIRE_EC_RAM_READ, off);
>> + i2c_smbus_read_i2c_block_data(client, ASPIRE_EC_RAM_READ, data_len, data);
>> + return 0;
>> +}
>> +
>> +static int aspire_ec_ram_write(struct i2c_client *client, u8 off, u8 data)
>> +{
>> + u8 tmp[2] = {off, data};
>> +
>> + i2c_smbus_write_i2c_block_data(client, ASPIRE_EC_RAM_WRITE, sizeof(tmp), tmp);
>> + return 0;
>> +}
>> +
>> +static irqreturn_t aspire_ec_irq_handler(int irq, void *data)
>> +{
>> + struct aspire_ec *ec = data;
>> + int id;
>> + u8 tmp;
>> +
>> + /*
>> + * The original ACPI firmware actually has a small sleep in the handler.
>> + *
>> + * It seems like in most cases it's not needed but when the device
>> + * just exits suspend, our i2c driver has a brief time where data
>> + * transfer is not possible yet. So this delay allows us to suppress
>> + * quite a bunch of spurious error messages in dmesg. Thus it's kept.
>> + */
>> + usleep_range(15000, 30000);
>> +
>> + id = i2c_smbus_read_byte_data(ec->client, ASPIRE_EC_EVENT);
>> + if (id < 0) {
>> + dev_err(&ec->client->dev, "Failed to read event id: %pe\n", ERR_PTR(id));
>> + return IRQ_HANDLED;
>> + }
>> +
>> + switch (id) {
>> + case 0x0: /* No event */
>> + break;
>> +
>> + case ASPIRE_EC_EVENT_WATCHDOG:
>> + /*
>> + * Here acpi responds to the event and clears some bit.
>> + * Notify (\_SB.I2C3.BAT1, 0x81) // Information Change
>> + * Notify (\_SB.I2C3.ADP1, 0x80) // Status Change
>> + */
>> + aspire_ec_ram_read(ec->client, ASPIRE_EC_RAM_WATCHDOG, &tmp, sizeof(tmp));
>> + aspire_ec_ram_write(ec->client, ASPIRE_EC_RAM_WATCHDOG, tmp & 0xbf);
>> + break;
>> +
>> + case ASPIRE_EC_EVENT_LID_CLOSE:
>> + /* Notify (\_SB.LID0, 0x80) // Status Change */
>> + input_report_switch(ec->idev, SW_LID, 1);
>> + input_sync(ec->idev);
>> + break;
>> +
>> + case ASPIRE_EC_EVENT_LID_OPEN:
>> + /* Notify (\_SB.LID0, 0x80) // Status Change */
>> + input_report_switch(ec->idev, SW_LID, 0);
>> + input_sync(ec->idev);
>> + break;
>> +
>> + case ASPIRE_EC_EVENT_FG_INF_CHG:
>> + /* Notify (\_SB.I2C3.BAT1, 0x81) // Information Change */
>
> fallthrough;
>

Hm I believe this would not warn since it's just two values for the same
code, just with an extra comment inbetween?

>> + case ASPIRE_EC_EVENT_FG_STA_CHG:
>> + /* Notify (\_SB.I2C3.BAT1, 0x80) // Status Change */
>> + power_supply_changed(ec->bat_psy);
>> + power_supply_changed(ec->adp_psy);
>> + break;
>> +
>> + case ASPIRE_EC_EVENT_HPD_DIS:
>> + if (ec->bridge_configured)
>> + drm_bridge_hpd_notify(&ec->bridge, connector_status_disconnected);
>> + break;
>> +
>> + case ASPIRE_EC_EVENT_HPD_CON:
>> + if (ec->bridge_configured)
>> + drm_bridge_hpd_notify(&ec->bridge, connector_status_connected);
>> + break;
>> +
>> + case ASPIRE_EC_EVENT_BKL_BLANKED:
>> + case ASPIRE_EC_EVENT_BKL_UNBLANKED:
>> + /* Display backlight blanked on FN+F6. No action needed. */
>> + break;
>> +
>> + case ASPIRE_EC_EVENT_KBD_BKL_ON:
>> + case ASPIRE_EC_EVENT_KBD_BKL_OFF:
>> + /*
>> + * There is a keyboard backlight connector on Aspire 1 that is
>> + * controlled by FN+F8. There is no kb backlight on the device though.
>> + * Seems like this is used on other devices like Acer Spin 7.
>> + * No action needed.
>> + */
>> + break;
>> +
>> + default:
>> + dev_warn(&ec->client->dev, "Unknown event id=0x%x\n", id);
>> + }
>> +
>> + return IRQ_HANDLED;
>> +}
>> +
>> +/*
>> + * Power supply.
>> + */
>> +
>> +struct aspire_ec_bat_psy_static_data {
>> + u8 unk1;
>> + u8 flags;
>> + __le16 unk2;
>> + __le16 voltage_design;
>> + __le16 capacity_full;
>> + __le16 unk3;
>> + __le16 serial;
>> + u8 model_id;
>> + u8 vendor_id;
>> +} __packed;
>> +
>> +static const char * const aspire_ec_bat_psy_battery_model[] = {
>> + "AP18C4K",
>> + "AP18C8K",
>> + "AP19B8K",
>> + "AP16M4J",
>> + "AP16M5J",
>> +};
>> +
>> +static const char * const aspire_ec_bat_psy_battery_vendor[] = {
>> + "SANYO",
>> + "SONY",
>> + "PANASONIC",
>> + "SAMSUNG",
>> + "SIMPLO",
>> + "MOTOROLA",
>> + "CELXPERT",
>> + "LGC",
>> + "GETAC",
>> + "MURATA",
>> +};
>> +
>> +struct aspire_ec_bat_psy_dynamic_data {
>> + u8 unk1;
>> + u8 flags;
>> + u8 unk2;
>> + __le16 capacity_now;
>> + __le16 voltage_now;
>> + __le16 current_now;
>> + __le16 unk3;
>> + __le16 unk4;
>> +} __packed;
>> +
>> +static int aspire_ec_bat_psy_get_property(struct power_supply *psy,
>> + enum power_supply_property psp,
>> + union power_supply_propval *val)
>> +{
>> + struct aspire_ec *ec = power_supply_get_drvdata(psy);
>> + struct aspire_ec_bat_psy_static_data sdat;
>> + struct aspire_ec_bat_psy_dynamic_data ddat;
>> +
>> + i2c_smbus_read_i2c_block_data(ec->client, ASPIRE_EC_FG_STATIC, sizeof(sdat), (u8 *)&sdat);
>> + i2c_smbus_read_i2c_block_data(ec->client, ASPIRE_EC_FG_DYNAMIC, sizeof(ddat), (u8 *)&ddat);
>> +
>> + switch (psp) {
>> + case POWER_SUPPLY_PROP_STATUS:
>> + val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
>> + if (ddat.flags & ASPIRE_EC_FG_FLAG_CHARGING)
>> + val->intval = POWER_SUPPLY_STATUS_CHARGING;
>> + else if (ddat.flags & ASPIRE_EC_FG_FLAG_DISCHARGING)
>> + val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
>> + else if (ddat.flags & ASPIRE_EC_FG_FLAG_FULL)
>> + val->intval = POWER_SUPPLY_STATUS_FULL;
>> + break;
>> +
>> + case POWER_SUPPLY_PROP_VOLTAGE_NOW:
>> + val->intval = le16_to_cpu(ddat.voltage_now) * 1000;
>> + break;
>> +
>> + case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
>> + val->intval = le16_to_cpu(sdat.voltage_design) * 1000;
>> + break;
>> +
>> + case POWER_SUPPLY_PROP_CHARGE_NOW:
>> + val->intval = le16_to_cpu(ddat.capacity_now) * 1000;
>> + break;
>> +
>> + case POWER_SUPPLY_PROP_CHARGE_FULL:
>> + val->intval = le16_to_cpu(sdat.capacity_full) * 1000;
>> + break;
>
> You could stick this "* 1000" stuff in a macro
>

acpi/battery.c also explicitly sets the multiplier so I think it's the
"common" way to do this.

>> +
>> + case POWER_SUPPLY_PROP_CAPACITY:
>> + val->intval = le16_to_cpu(ddat.capacity_now) * 100;
>> + val->intval /= le16_to_cpu(sdat.capacity_full);
>> + break;
>> +
>> + case POWER_SUPPLY_PROP_CURRENT_NOW:
>> + val->intval = (s16)le16_to_cpu(ddat.current_now) * 1000;
>> + break;
>> +
>> + case POWER_SUPPLY_PROP_PRESENT:
>> + val->intval = !!(ddat.flags & ASPIRE_EC_FG_FLAG_PRESENT);
>> + break;
>> +
>> + case POWER_SUPPLY_PROP_SCOPE:
>> + val->intval = POWER_SUPPLY_SCOPE_SYSTEM;
>> + break;
>> +
>> + case POWER_SUPPLY_PROP_MODEL_NAME:
>> + if (sdat.model_id - 1 < ARRAY_SIZE(aspire_ec_bat_psy_battery_model))
>> + val->strval = aspire_ec_bat_psy_battery_model[sdat.model_id - 1];
>> + else
>> + val->strval = "Unknown";
>> + break;
>> +
>> + case POWER_SUPPLY_PROP_MANUFACTURER:
>> + if (sdat.vendor_id - 3 < ARRAY_SIZE(aspire_ec_bat_psy_battery_vendor))
>> + val->strval = aspire_ec_bat_psy_battery_vendor[sdat.vendor_id - 3];
>
> How does this -3 offset not underflow ?
>

vendor_id here is unsigned so the if check would actually overflow,
though explaining that I guess it's better to be explicit there and let
the compiler optimize that check away anyway... I will update the if
condition with an extra (id >= 3).

> Seems a bit dodgy to me - can you add a comment to the code to explain ? Its not immediately obvious the -3 is OK.
>
> Also could you take an index instead of replicating the -value stepdown each time ?
>
> int myindex = sdat.model_id - 1;
>
> if (myindex < someconstraint)
> strval = somearry[myindex];
>

I decided against adding a dedicated index variable since there is only
one actual use for each, so it's easy to see where it goes.

>> + else
>> + val->strval = "Unknown";
>> + break;
>> +
>> + default:
>> + return -EINVAL;
>> + }
>> +
>> + return 0;
>> +}
>> +
>> +static enum power_supply_property aspire_ec_bat_psy_props[] = {
>> + POWER_SUPPLY_PROP_STATUS,
>> + POWER_SUPPLY_PROP_VOLTAGE_NOW,
>> + POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
>> + POWER_SUPPLY_PROP_CHARGE_NOW,
>> + POWER_SUPPLY_PROP_CHARGE_FULL,
>> + POWER_SUPPLY_PROP_CAPACITY,
>> + POWER_SUPPLY_PROP_CURRENT_NOW,
>> + POWER_SUPPLY_PROP_PRESENT,
>> + POWER_SUPPLY_PROP_SCOPE,
>> + POWER_SUPPLY_PROP_MODEL_NAME,
>> + POWER_SUPPLY_PROP_MANUFACTURER,
>> +};
>> +
>> +static const struct power_supply_desc aspire_ec_bat_psy_desc = {
>> + .name = "aspire-ec-bat",
>> + .type = POWER_SUPPLY_TYPE_BATTERY,
>> + .get_property = aspire_ec_bat_psy_get_property,
>> + .properties = aspire_ec_bat_psy_props,
>> + .num_properties = ARRAY_SIZE(aspire_ec_bat_psy_props),
>> +};
>> +
>> +static int aspire_ec_adp_psy_get_property(struct power_supply *psy,
>> + enum power_supply_property psp,
>> + union power_supply_propval *val)
>> +{
>> + struct aspire_ec *ec = power_supply_get_drvdata(psy);
>> + u8 tmp;
>> +
>> + switch (psp) {
>> + case POWER_SUPPLY_PROP_ONLINE:
>> + aspire_ec_ram_read(ec->client, ASPIRE_EC_RAM_ADP, &tmp, sizeof(tmp));
>> + val->intval = !!(tmp & ASPIRE_EC_AC_STATUS);
>> + break;
>> +
>> + default:
>> + return -EINVAL;
>> + }
>> +
>> + return 0;
>> +}
>> +
>> +static enum power_supply_property aspire_ec_adp_psy_props[] = {
>> + POWER_SUPPLY_PROP_ONLINE,
>> +};
>> +
>> +static const struct power_supply_desc aspire_ec_adp_psy_desc = {
>> + .name = "aspire-ec-adp",
>> + .type = POWER_SUPPLY_TYPE_MAINS,
>> + .get_property = aspire_ec_adp_psy_get_property,
>> + .properties = aspire_ec_adp_psy_props,
>> + .num_properties = ARRAY_SIZE(aspire_ec_adp_psy_props),
>> +};
>> +
>> +/*
>> + * USB-C DP Alt mode HPD.
>> + */
>> +
>> +static int aspire_ec_bridge_attach(struct drm_bridge *bridge, enum drm_bridge_attach_flags flags)
>> +{
>> + return flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR ? 0 : -EINVAL;
>> +}
>> +
>> +static void aspire_ec_bridge_update_hpd_work(struct work_struct *work)
>> +{
>> + struct aspire_ec *ec = container_of(work, struct aspire_ec, work);
>> + u8 tmp;
>> +
>> + aspire_ec_ram_read(ec->client, ASPIRE_EC_RAM_HPD_STATUS, &tmp, sizeof(tmp));
>> + if (tmp == ASPIRE_EC_HPD_CONNECTED)
>> + drm_bridge_hpd_notify(&ec->bridge, connector_status_connected);
>> + else
>> + drm_bridge_hpd_notify(&ec->bridge, connector_status_disconnected);
>> +}
>> +
>> +static void aspire_ec_bridge_hpd_enable(struct drm_bridge *bridge)
>> +{
>> + struct aspire_ec *ec = container_of(bridge, struct aspire_ec, bridge);
>> +
>> + schedule_work(&ec->work);
>> +}
>> +
>> +static const struct drm_bridge_funcs aspire_ec_bridge_funcs = {
>> + .hpd_enable = aspire_ec_bridge_hpd_enable,
>> + .attach = aspire_ec_bridge_attach,
>> +};
>> +
>> +/*
>> + * Sysfs attributes.
>> + */
>> +
>> +static ssize_t fn_lock_show(struct device *dev, struct device_attribute *attr, char *buf)
>> +{
>> + struct aspire_ec *ec = i2c_get_clientdata(to_i2c_client(dev));
>> + u8 tmp;
>> +
>> + aspire_ec_ram_read(ec->client, ASPIRE_EC_RAM_KBD_MODE, &tmp, sizeof(tmp));
>> +
>> + return sysfs_emit(buf, "%d\n", !(tmp & ASPIRE_EC_RAM_KBD_MEDIA_ON_TOP));
>> +}
>> +
>> +static ssize_t fn_lock_store(struct device *dev, struct device_attribute *attr,
>> + const char *buf, size_t count)
>> +{
>> + struct aspire_ec *ec = i2c_get_clientdata(to_i2c_client(dev));
>> + u8 tmp;
>> +
>> + bool state;
>> + int ret;
>> +
>> + ret = kstrtobool(buf, &state);
>> + if (ret)
>> + return ret;
>> +
>> + aspire_ec_ram_read(ec->client, ASPIRE_EC_RAM_KBD_MODE, &tmp, sizeof(tmp));
>> +
>> + if (state)
>> + tmp &= ~ASPIRE_EC_RAM_KBD_MEDIA_ON_TOP;
>> + else
>> + tmp |= ASPIRE_EC_RAM_KBD_MEDIA_ON_TOP;
>> +
>> + aspire_ec_ram_write(ec->client, ASPIRE_EC_RAM_KBD_MODE, tmp);
>> +
>> + return count;
>> +}
>> +
>> +static DEVICE_ATTR_RW(fn_lock);
>> +
>> +static struct attribute *aspire_ec_attributes[] = {
>> + &dev_attr_fn_lock.attr,
>> + NULL
>> +};
>> +
>> +static const struct attribute_group aspire_ec_attribute_group = {
>> + .attrs = aspire_ec_attributes
>> +};
>> +
>> +static int aspire_ec_probe(struct i2c_client *client)
>> +{
>> + struct power_supply_config psy_cfg = {0};
>> + struct device *dev = &client->dev;
>> + struct fwnode_handle *fwnode;
>> + struct aspire_ec *ec;
>> + int ret;
>> + u8 tmp;
>> +
>> + ec = devm_kzalloc(dev, sizeof(*ec), GFP_KERNEL);
>> + if (!ec)
>> + return -ENOMEM;
>> +
>> + ec->client = client;
>> + i2c_set_clientdata(client, ec);
>> +
>> + /* Battery status reports */
>> + psy_cfg.drv_data = ec;
>> + ec->bat_psy = devm_power_supply_register(dev, &aspire_ec_bat_psy_desc, &psy_cfg);
>> + if (IS_ERR(ec->bat_psy))
>> + return dev_err_probe(dev, PTR_ERR(ec->bat_psy),
>> + "Failed to register battery power supply\n");
>> +
>> + ec->adp_psy = devm_power_supply_register(dev, &aspire_ec_adp_psy_desc, &psy_cfg);
>> + if (IS_ERR(ec->adp_psy))
>> + return dev_err_probe(dev, PTR_ERR(ec->adp_psy),
>> + "Failed to register ac power supply\n");
>> +
>> + /* Lid switch */
>> + ec->idev = devm_input_allocate_device(dev);
>> + if (!ec->idev)
>> + return -ENOMEM;
>> +
>> + ec->idev->name = "aspire-ec";
>> + ec->idev->phys = "aspire-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, "Input device register failed\n");
>> +
>> + /* Enable the keyboard fn keys */
>> + tmp = ASPIRE_EC_RAM_KBD_FN_EN | ASPIRE_EC_RAM_KBD_ALWAYS_SET;
>> + tmp |= ASPIRE_EC_RAM_KBD_MEDIA_ON_TOP;
>> + aspire_ec_ram_write(client, ASPIRE_EC_RAM_KBD_MODE, tmp);
>> +
>> + aspire_ec_ram_read(client, ASPIRE_EC_RAM_KBD_MODE_2, &tmp, sizeof(tmp));
>> + tmp |= ASPIRE_EC_RAM_KBD_MEDIA_NOTIFY;
>> + aspire_ec_ram_write(client, ASPIRE_EC_RAM_KBD_MODE_2, tmp);
>> +
>> + ret = devm_device_add_group(dev, &aspire_ec_attribute_group);
>> + if (ret)
>
> Don't you need input_unregister_device() in this case ? suggest a goto jump label to clean up down the end of your probe() function.
>

devm_input_allocate_device guarantees that devm will un-register it,
which I myself also forget every time and have to re-check :D

* Managed input devices do not need to be explicitly unregistered or
* freed as it will be done automatically when owner device unbinds from
* its driver (or binding fails).

So this driver only uses devm resources and thus doesn't need to
clean after itself.

Thanks for your review!
Nikita

>> + return dev_err_probe(dev, ret, "Failed to create attribute group\n");
>> +
>> + /* External Type-C display attach reports */
>> + fwnode = device_get_named_child_node(dev, "connector");
>> + if (fwnode) {
>> + INIT_WORK(&ec->work, aspire_ec_bridge_update_hpd_work);
>> + ec->bridge.funcs = &aspire_ec_bridge_funcs;
>> + ec->bridge.of_node = to_of_node(fwnode);
>> + ec->bridge.ops = DRM_BRIDGE_OP_HPD;
>> + ec->bridge.type = DRM_MODE_CONNECTOR_USB;
>> +
>> + ret = devm_drm_bridge_add(dev, &ec->bridge);
>> + if (ret) {
>> + fwnode_handle_put(fwnode);
>> + return dev_err_probe(dev, ret, "Failed to register drm bridge\n");
>> + }
>> +
>> + ec->bridge_configured = true;
>> + }
>> +
>> + ret = devm_request_threaded_irq(dev, client->irq, NULL,
>> + aspire_ec_irq_handler, IRQF_ONESHOT,
>> + dev_name(dev), ec);
>> + if (ret)
>> + return dev_err_probe(dev, ret, "Failed to request irq\n");
>> +
>> + return 0;
>
> jump label here for stuff that doens't just clean up
>
>> +}
>> +
>> +static int aspire_ec_resume(struct device *dev)
>> +{
>> + struct aspire_ec *ec = i2c_get_clientdata(to_i2c_client(dev));
>> + u8 tmp;
>> +
>> + aspire_ec_ram_read(ec->client, ASPIRE_EC_RAM_LID_STATUS, &tmp, sizeof(tmp));
>> + input_report_switch(ec->idev, SW_LID, !!(tmp & ASPIRE_EC_LID_OPEN));
>> + input_sync(ec->idev);
>> +
>> + return 0;
>> +}
>> +
>> +static const struct i2c_device_id aspire_ec_id[] = {
>> + { "aspire1-ec", },
>> + { }
>> +};
>> +MODULE_DEVICE_TABLE(i2c, aspire_ec_id);
>> +
>> +static const struct of_device_id aspire_ec_of_match[] = {
>> + { .compatible = "acer,aspire1-ec", },
>> + { }
>> +};
>> +MODULE_DEVICE_TABLE(of, aspire_ec_of_match);
>> +
>> +static DEFINE_SIMPLE_DEV_PM_OPS(aspire_ec_pm_ops, NULL, aspire_ec_resume);
>> +
>> +static struct i2c_driver aspire_ec_driver = {
>> + .driver = {
>> + .name = "aspire-ec",
>> + .of_match_table = aspire_ec_of_match,
>> + .pm = pm_sleep_ptr(&aspire_ec_pm_ops),
>> + },
>> + .probe = aspire_ec_probe,
>> + .id_table = aspire_ec_id,
>> +};
>> +module_i2c_driver(aspire_ec_driver);
>
> Don't you need an exit function to clean up stuff that's not devm_ derived ?
>
> input_unregister_device() ?
>
>> +
>> +MODULE_DESCRIPTION("Acer Aspire 1 embedded controller");
>> +MODULE_AUTHOR("Nikita Travkin <[email protected]>");
>> +MODULE_LICENSE("GPL");
>>

2024-03-12 12:44:20

by Bryan O'Donoghue

[permalink] [raw]
Subject: Re: [PATCH v4 3/4] platform: arm64: Add Acer Aspire 1 embedded controller driver

On 12/03/2024 12:23, Nikita Travkin wrote:
> Bryan O'Donoghue писал(а) 12.03.2024 16:58:
>> On 12/03/2024 08:42, Nikita Travkin wrote:
>>> Acer Aspire 1 is a Snapdragon 7c based laptop. It uses an embedded
>>> controller to perform a set of various functions, such as:
>>>
>>> - Battery and charger monitoring;
>>> - Keyboard layout control (i.e. fn_lock settings);
>>> - USB Type-C DP alt mode HPD notifications;
>>> - Laptop lid status.
>>>
>>> Unfortunately, while all this functionality is implemented in ACPI, it's
>>> currently not possible to use ACPI to boot Linux on such Qualcomm
>>> devices. To allow Linux to still support the features provided by EC,
>>> this driver reimplments the relevant ACPI parts. This allows us to boot
>>> the laptop with Device Tree and retain all the features.
>>>
>>> Signed-off-by: Nikita Travkin <[email protected]>
>>> ---
>>> drivers/platform/arm64/Kconfig | 16 +
>>> drivers/platform/arm64/Makefile | 2 +
>>> drivers/platform/arm64/acer-aspire1-ec.c | 555 +++++++++++++++++++++++++++++++
>>
>> You should be listing yourself as a maintainer for a driver you contribute.
>
> I always believed that being in the AUTHOR() at the bottom of the driver
> would guarantee me being in CC for patches, which so far worked great,
> thus I was always hesitent adding extra entries in MAINTAINERS.

There's no such rule that I'm aware of there.

scripts/get_maintainer.pl won't list a driver author for the CC list

This is a substantial body of code, you should own it upstream.

>>> + case ASPIRE_EC_EVENT_FG_INF_CHG:
>>> + /* Notify (\_SB.I2C3.BAT1, 0x81) // Information Change */
>>
>> fallthrough;
>>
>
> Hm I believe this would not warn since it's just two values for the same
> code, just with an extra comment inbetween?

True

>>> + case POWER_SUPPLY_PROP_VOLTAGE_NOW:
>>> + val->intval = le16_to_cpu(ddat.voltage_now) * 1000;
>>> + break;
>>> +
>>> + case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
>>> + val->intval = le16_to_cpu(sdat.voltage_design) * 1000;
>>> + break;
>>> +
>>> + case POWER_SUPPLY_PROP_CHARGE_NOW:
>>> + val->intval = le16_to_cpu(ddat.capacity_now) * 1000;
>>> + break;
>>> +
>>> + case POWER_SUPPLY_PROP_CHARGE_FULL:
>>> + val->intval = le16_to_cpu(sdat.capacity_full) * 1000;
>>> + break;
>>
>> You could stick this "* 1000" stuff in a macro
>>
>
> acpi/battery.c also explicitly sets the multiplier so I think it's the
> "common" way to do this.

common != nice

Purely aesthetics but anyway consider decomposing the replication down.

>>> +
>>> + case POWER_SUPPLY_PROP_CAPACITY:
>>> + val->intval = le16_to_cpu(ddat.capacity_now) * 100;
>>> + val->intval /= le16_to_cpu(sdat.capacity_full);
>>> + break;
>>> +
>>> + case POWER_SUPPLY_PROP_CURRENT_NOW:
>>> + val->intval = (s16)le16_to_cpu(ddat.current_now) * 1000;
>>> + break;
>>> +
>>> + case POWER_SUPPLY_PROP_PRESENT:
>>> + val->intval = !!(ddat.flags & ASPIRE_EC_FG_FLAG_PRESENT);
>>> + break;
>>> +
>>> + case POWER_SUPPLY_PROP_SCOPE:
>>> + val->intval = POWER_SUPPLY_SCOPE_SYSTEM;
>>> + break;
>>> +
>>> + case POWER_SUPPLY_PROP_MODEL_NAME:
>>> + if (sdat.model_id - 1 < ARRAY_SIZE(aspire_ec_bat_psy_battery_model))
>>> + val->strval = aspire_ec_bat_psy_battery_model[sdat.model_id - 1];
>>> + else
>>> + val->strval = "Unknown";
>>> + break;
>>> +
>>> + case POWER_SUPPLY_PROP_MANUFACTURER:
>>> + if (sdat.vendor_id - 3 < ARRAY_SIZE(aspire_ec_bat_psy_battery_vendor))
>>> + val->strval = aspire_ec_bat_psy_battery_vendor[sdat.vendor_id - 3];
>>
>> How does this -3 offset not underflow ?
>>
>
> vendor_id here is unsigned so the if check would actually overflow,
> though explaining that I guess it's better to be explicit there and let
> the compiler optimize that check away anyway... I will update the if
> condition with an extra (id >= 3).

What's the "3" about though, that's what's not jumping out at me here.

>
>> Seems a bit dodgy to me - can you add a comment to the code to explain ? Its not immediately obvious the -3 is OK.
>>
>> Also could you take an index instead of replicating the -value stepdown each time ?
>>
>> int myindex = sdat.model_id - 1;
>>
>> if (myindex < someconstraint)
>> strval = somearry[myindex];
>>
>
> I decided against adding a dedicated index variable since there is only
> one actual use for each, so it's easy to see where it goes.

But you do it twice which is why I'm suggesting take an index and do it
once.

Then add

Reviewed-by: Bryan O'Donoghue <[email protected]>

2024-03-12 14:04:57

by Nikita Travkin

[permalink] [raw]
Subject: Re: [PATCH v4 3/4] platform: arm64: Add Acer Aspire 1 embedded controller driver

Ilpo Järvinen писал(а) 12.03.2024 17:31:
> On Tue, 12 Mar 2024, Nikita Travkin wrote:
>
>> Acer Aspire 1 is a Snapdragon 7c based laptop. It uses an embedded
>> controller to perform a set of various functions, such as:
>>
>> - Battery and charger monitoring;
>> - Keyboard layout control (i.e. fn_lock settings);
>> - USB Type-C DP alt mode HPD notifications;
>> - Laptop lid status.
>>
>> Unfortunately, while all this functionality is implemented in ACPI, it's
>> currently not possible to use ACPI to boot Linux on such Qualcomm
>> devices. To allow Linux to still support the features provided by EC,
>> this driver reimplments the relevant ACPI parts. This allows us to boot
>> the laptop with Device Tree and retain all the features.
>>
>> Signed-off-by: Nikita Travkin <[email protected]>
>> ---
>> drivers/platform/arm64/Kconfig | 16 +
>> drivers/platform/arm64/Makefile | 2 +
>> drivers/platform/arm64/acer-aspire1-ec.c | 555 +++++++++++++++++++++++++++++++
>> 3 files changed, 573 insertions(+)
>>
>> diff --git a/drivers/platform/arm64/Kconfig b/drivers/platform/arm64/Kconfig
>> index 644b83ede093..07d47879a9e3 100644
>> --- a/drivers/platform/arm64/Kconfig
>> +++ b/drivers/platform/arm64/Kconfig
>> @@ -16,4 +16,20 @@ menuconfig ARM64_PLATFORM_DEVICES
>>
>> if ARM64_PLATFORM_DEVICES
>>
>> +config EC_ACER_ASPIRE1
>> + tristate "Acer Aspire 1 Emedded Controller driver"
>> + depends on I2C
>> + depends on DRM
>> + depends on POWER_SUPPLY
>> + depends on INPUT
>> + help
>> + Say Y here to enable the EC driver for the (Snapdragon-based)
>> + Acer Aspire 1 laptop. The EC handles battery and charging
>> + monitoring as well as some misc functions like the lid sensor
>> + and USB Type-C DP HPD events.
>> +
>> + This driver provides battery and AC status support for the mentioned
>> + laptop where this information is not properly exposed via the
>> + standard ACPI devices.
>> +
>> endif # ARM64_PLATFORM_DEVICES
>> diff --git a/drivers/platform/arm64/Makefile b/drivers/platform/arm64/Makefile
>> index f91cdc7155e2..4fcc9855579b 100644
>> --- a/drivers/platform/arm64/Makefile
>> +++ b/drivers/platform/arm64/Makefile
>> @@ -4,3 +4,5 @@
>> #
>> # This dir should only include drivers for EC-like devices.
>> #
>> +
>> +obj-$(CONFIG_EC_ACER_ASPIRE1) += acer-aspire1-ec.o
>> diff --git a/drivers/platform/arm64/acer-aspire1-ec.c b/drivers/platform/arm64/acer-aspire1-ec.c
>> new file mode 100644
>> index 000000000000..3941e24c5c7c
>> --- /dev/null
>> +++ b/drivers/platform/arm64/acer-aspire1-ec.c
>> @@ -0,0 +1,555 @@
>> +// SPDX-License-Identifier: GPL-2.0-only
>> +/* Copyright (c) 2023, Nikita Travkin <[email protected]> */
>> +
>> +#include <linux/module.h>
>> +#include <linux/platform_device.h>
>> +#include <linux/power_supply.h>
>> +#include <linux/i2c.h>
>> +#include <linux/input.h>
>> +#include <linux/delay.h>
>> +
>> +#include <linux/usb/typec_mux.h>
>> +#include <drm/drm_bridge.h>
>> +
>> +#define ASPIRE_EC_EVENT 0x05
>> +
>> +#define ASPIRE_EC_EVENT_WATCHDOG 0x20
>> +#define ASPIRE_EC_EVENT_KBD_BKL_ON 0x57
>> +#define ASPIRE_EC_EVENT_KBD_BKL_OFF 0x58
>> +#define ASPIRE_EC_EVENT_LID_CLOSE 0x9b
>> +#define ASPIRE_EC_EVENT_LID_OPEN 0x9c
>> +#define ASPIRE_EC_EVENT_BKL_UNBLANKED 0x9d
>> +#define ASPIRE_EC_EVENT_BKL_BLANKED 0x9e
>> +#define ASPIRE_EC_EVENT_FG_INF_CHG 0x85
>> +#define ASPIRE_EC_EVENT_FG_STA_CHG 0xc6
>> +#define ASPIRE_EC_EVENT_HPD_DIS 0xa3
>> +#define ASPIRE_EC_EVENT_HPD_CON 0xa4
>> +
>> +#define ASPIRE_EC_FG_DYNAMIC 0x07
>> +#define ASPIRE_EC_FG_STATIC 0x08
>> +
>> +#define ASPIRE_EC_FG_FLAG_PRESENT BIT(0)
>
> Add #include <linux/bits.h>
>

Ack

>> +#define ASPIRE_EC_FG_FLAG_FULL BIT(1)
>> +#define ASPIRE_EC_FG_FLAG_DISCHARGING BIT(2)
>> +#define ASPIRE_EC_FG_FLAG_CHARGING BIT(3)
>> +
>> +#define ASPIRE_EC_RAM_READ 0x20
>> +#define ASPIRE_EC_RAM_WRITE 0x21
>> +
>> +#define ASPIRE_EC_RAM_WATCHDOG 0x19
>> +#define ASPIRE_EC_RAM_KBD_MODE 0x43
>> +
>> +#define ASPIRE_EC_RAM_KBD_FN_EN BIT(0)
>> +#define ASPIRE_EC_RAM_KBD_MEDIA_ON_TOP BIT(5)
>> +#define ASPIRE_EC_RAM_KBD_ALWAYS_SET BIT(6)
>> +#define ASPIRE_EC_RAM_KBD_NUM_LAYER_EN BIT(7)
>> +
>> +#define ASPIRE_EC_RAM_KBD_MODE_2 0x60
>> +
>> +#define ASPIRE_EC_RAM_KBD_MEDIA_NOTIFY BIT(3)
>> +
>> +#define ASPIRE_EC_RAM_HPD_STATUS 0xf4
>> +#define ASPIRE_EC_HPD_CONNECTED 0x03
>> +
>> +#define ASPIRE_EC_RAM_LID_STATUS 0x4c
>> +#define ASPIRE_EC_LID_OPEN BIT(6)
>> +
>> +#define ASPIRE_EC_RAM_ADP 0x40
>> +#define ASPIRE_EC_AC_STATUS BIT(0)
>> +
>> +struct aspire_ec {
>> + struct i2c_client *client;
>> + struct power_supply *bat_psy;
>> + struct power_supply *adp_psy;
>> + struct input_dev *idev;
>> +
>> + bool bridge_configured;
>> + struct drm_bridge bridge;
>> + struct work_struct work;
>
> Include missing.
>

Will add too.

>> +static int aspire_ec_ram_read(struct i2c_client *client, u8 off, u8 *data, u8 data_len)
>> +{
>> + i2c_smbus_write_byte_data(client, ASPIRE_EC_RAM_READ, off);
>> + i2c_smbus_read_i2c_block_data(client, ASPIRE_EC_RAM_READ, data_len, data);
>> + return 0;
>> +}
>> +
>> +static int aspire_ec_ram_write(struct i2c_client *client, u8 off, u8 data)
>> +{
>> + u8 tmp[2] = {off, data};
>> +
>> + i2c_smbus_write_i2c_block_data(client, ASPIRE_EC_RAM_WRITE, sizeof(tmp), tmp);
>> + return 0;
>> +}
>> +
>> +static irqreturn_t aspire_ec_irq_handler(int irq, void *data)
>> +{
>> + struct aspire_ec *ec = data;
>> + int id;
>> + u8 tmp;
>> +
>> + /*
>> + * The original ACPI firmware actually has a small sleep in the handler.
>> + *
>> + * It seems like in most cases it's not needed but when the device
>> + * just exits suspend, our i2c driver has a brief time where data
>> + * transfer is not possible yet. So this delay allows us to suppress
>> + * quite a bunch of spurious error messages in dmesg. Thus it's kept.
>> + */
>> + usleep_range(15000, 30000);
>> +
>> + id = i2c_smbus_read_byte_data(ec->client, ASPIRE_EC_EVENT);
>> + if (id < 0) {
>> + dev_err(&ec->client->dev, "Failed to read event id: %pe\n", ERR_PTR(id));
>> + return IRQ_HANDLED;
>> + }
>> +
>> + switch (id) {
>> + case 0x0: /* No event */
>> + break;
>> +
>> + case ASPIRE_EC_EVENT_WATCHDOG:
>> + /*
>> + * Here acpi responds to the event and clears some bit.
>> + * Notify (\_SB.I2C3.BAT1, 0x81) // Information Change
>> + * Notify (\_SB.I2C3.ADP1, 0x80) // Status Change
>> + */
>> + aspire_ec_ram_read(ec->client, ASPIRE_EC_RAM_WATCHDOG, &tmp, sizeof(tmp));
>> + aspire_ec_ram_write(ec->client, ASPIRE_EC_RAM_WATCHDOG, tmp & 0xbf);
>
> Magic 0xbf? Is this a clear bit operation which should use
> tmp & ~DEFINEDNAME
> ?
>

Right, unfortunately I couldn't get much more from the decompiled dsdt.
But given I've already called this event "watchdog" could name the bit
the same too. Will change.

>> + break;
>> +
>> + case ASPIRE_EC_EVENT_LID_CLOSE:
>> + /* Notify (\_SB.LID0, 0x80) // Status Change */
>> + input_report_switch(ec->idev, SW_LID, 1);
>> + input_sync(ec->idev);
>> + break;
>> +
>> + case ASPIRE_EC_EVENT_LID_OPEN:
>> + /* Notify (\_SB.LID0, 0x80) // Status Change */
>> + input_report_switch(ec->idev, SW_LID, 0);
>> + input_sync(ec->idev);
>> + break;
>> +
>> + case ASPIRE_EC_EVENT_FG_INF_CHG:
>> + /* Notify (\_SB.I2C3.BAT1, 0x81) // Information Change */
>
> fallthrough;
>
> I'm not 100% sure if it's needed because you only have a comment there but
> it surely doesn't hurt to have it.
>

Ack, given both you and Bryan think it's better to have it here, will add.

>> + case ASPIRE_EC_EVENT_FG_STA_CHG:
>> + /* Notify (\_SB.I2C3.BAT1, 0x80) // Status Change */
>> + power_supply_changed(ec->bat_psy);
>> + power_supply_changed(ec->adp_psy);
>> + break;
>> +
>> + case ASPIRE_EC_EVENT_HPD_DIS:
>> + if (ec->bridge_configured)
>> + drm_bridge_hpd_notify(&ec->bridge, connector_status_disconnected);
>> + break;
>> +
>> + case ASPIRE_EC_EVENT_HPD_CON:
>> + if (ec->bridge_configured)
>> + drm_bridge_hpd_notify(&ec->bridge, connector_status_connected);
>> + break;
>> +
>> + case ASPIRE_EC_EVENT_BKL_BLANKED:
>> + case ASPIRE_EC_EVENT_BKL_UNBLANKED:
>> + /* Display backlight blanked on FN+F6. No action needed. */
>> + break;
>> +
>> + case ASPIRE_EC_EVENT_KBD_BKL_ON:
>> + case ASPIRE_EC_EVENT_KBD_BKL_OFF:
>> + /*
>> + * There is a keyboard backlight connector on Aspire 1 that is
>> + * controlled by FN+F8. There is no kb backlight on the device though.
>> + * Seems like this is used on other devices like Acer Spin 7.
>> + * No action needed.
>> + */
>> + break;
>> +
>> + default:
>> + dev_warn(&ec->client->dev, "Unknown event id=0x%x\n", id);
>> + }
>> +
>> + return IRQ_HANDLED;
>> +}
>> +
>> +/*
>> + * Power supply.
>> + */
>> +
>> +struct aspire_ec_bat_psy_static_data {
>> + u8 unk1;
>> + u8 flags;
>> + __le16 unk2;
>> + __le16 voltage_design;
>> + __le16 capacity_full;
>> + __le16 unk3;
>> + __le16 serial;
>> + u8 model_id;
>> + u8 vendor_id;
>> +} __packed;
>> +
>> +static const char * const aspire_ec_bat_psy_battery_model[] = {
>> + "AP18C4K",
>> + "AP18C8K",
>> + "AP19B8K",
>> + "AP16M4J",
>> + "AP16M5J",
>> +};
>> +
>> +static const char * const aspire_ec_bat_psy_battery_vendor[] = {
>> + "SANYO",
>> + "SONY",
>> + "PANASONIC",
>> + "SAMSUNG",
>> + "SIMPLO",
>> + "MOTOROLA",
>> + "CELXPERT",
>> + "LGC",
>> + "GETAC",
>> + "MURATA",
>> +};
>> +
>> +struct aspire_ec_bat_psy_dynamic_data {
>> + u8 unk1;
>> + u8 flags;
>> + u8 unk2;
>> + __le16 capacity_now;
>> + __le16 voltage_now;
>> + __le16 current_now;
>> + __le16 unk3;
>> + __le16 unk4;
>> +} __packed;
>
> What are the ARM64 rules when it comes to unaligned access? If it's not
> always supported, you need to use get_unaligned_le16() for accessing those
> __le16 fields.
>

Hm, you're right, arm64 spec definitely has something about alignment
checks so it's better to use that even though I'm not exactly sure about
exact rules. (though I guess in times like that I'd think "why can't
compiler take care of this" haha) Will use it for ddat fields.

>> +static int aspire_ec_bat_psy_get_property(struct power_supply *psy,
>> + enum power_supply_property psp,
>> + union power_supply_propval *val)
>> +{
>> + struct aspire_ec *ec = power_supply_get_drvdata(psy);
>> + struct aspire_ec_bat_psy_static_data sdat;
>> + struct aspire_ec_bat_psy_dynamic_data ddat;
>> +
>> + i2c_smbus_read_i2c_block_data(ec->client, ASPIRE_EC_FG_STATIC, sizeof(sdat), (u8 *)&sdat);
>> + i2c_smbus_read_i2c_block_data(ec->client, ASPIRE_EC_FG_DYNAMIC, sizeof(ddat), (u8 *)&ddat);
>> +
>> + switch (psp) {
>> + case POWER_SUPPLY_PROP_STATUS:
>> + val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
>> + if (ddat.flags & ASPIRE_EC_FG_FLAG_CHARGING)
>> + val->intval = POWER_SUPPLY_STATUS_CHARGING;
>> + else if (ddat.flags & ASPIRE_EC_FG_FLAG_DISCHARGING)
>> + val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
>> + else if (ddat.flags & ASPIRE_EC_FG_FLAG_FULL)
>> + val->intval = POWER_SUPPLY_STATUS_FULL;
>> + break;
>> +
>> + case POWER_SUPPLY_PROP_VOLTAGE_NOW:
>> + val->intval = le16_to_cpu(ddat.voltage_now) * 1000;
>
> linux/units.h provides something that can often be used instead of 10^n
> literals.
>

This is a conversion from Milli-Units to Micro-Units so I guess the best
way to define that using existing consts would be (MICRO/MILLI)

I will add '#define MILLI_TO_MICRO 1000' and use that here.

>> + break;
>> +
>> + case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
>> + val->intval = le16_to_cpu(sdat.voltage_design) * 1000;
>> + break;
>> +
>> + case POWER_SUPPLY_PROP_CHARGE_NOW:
>> + val->intval = le16_to_cpu(ddat.capacity_now) * 1000;
>> + break;
>> +
>> + case POWER_SUPPLY_PROP_CHARGE_FULL:
>> + val->intval = le16_to_cpu(sdat.capacity_full) * 1000;
>> + break;
>> +
>> + case POWER_SUPPLY_PROP_CAPACITY:
>> + val->intval = le16_to_cpu(ddat.capacity_now) * 100;
>> + val->intval /= le16_to_cpu(sdat.capacity_full);
>> + break;
>> +
>> + case POWER_SUPPLY_PROP_CURRENT_NOW:
>> + val->intval = (s16)le16_to_cpu(ddat.current_now) * 1000;
>> + break;
>> +
>> + case POWER_SUPPLY_PROP_PRESENT:
>> + val->intval = !!(ddat.flags & ASPIRE_EC_FG_FLAG_PRESENT);
>> + break;
>> +
>> + case POWER_SUPPLY_PROP_SCOPE:
>> + val->intval = POWER_SUPPLY_SCOPE_SYSTEM;
>> + break;
>> +
>> + case POWER_SUPPLY_PROP_MODEL_NAME:
>> + if (sdat.model_id - 1 < ARRAY_SIZE(aspire_ec_bat_psy_battery_model))
>> + val->strval = aspire_ec_bat_psy_battery_model[sdat.model_id - 1];
>
> Underflow handling (also here, the other place was mentioned already)?

Ack, I'm going to update if for both.

>
>> + else
>> + val->strval = "Unknown";
>> + break;
>> +
>> + case POWER_SUPPLY_PROP_MANUFACTURER:
>> + if (sdat.vendor_id - 3 < ARRAY_SIZE(aspire_ec_bat_psy_battery_vendor))
>> + val->strval = aspire_ec_bat_psy_battery_vendor[sdat.vendor_id - 3];
>> + else
>> + val->strval = "Unknown";
>> + break;
>> +
>> + default:
>> + return -EINVAL;
>> + }
>> +
>> + return 0;
>> +}
>> +
>> +static enum power_supply_property aspire_ec_bat_psy_props[] = {
>> + POWER_SUPPLY_PROP_STATUS,
>> + POWER_SUPPLY_PROP_VOLTAGE_NOW,
>> + POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
>> + POWER_SUPPLY_PROP_CHARGE_NOW,
>> + POWER_SUPPLY_PROP_CHARGE_FULL,
>> + POWER_SUPPLY_PROP_CAPACITY,
>> + POWER_SUPPLY_PROP_CURRENT_NOW,
>> + POWER_SUPPLY_PROP_PRESENT,
>> + POWER_SUPPLY_PROP_SCOPE,
>> + POWER_SUPPLY_PROP_MODEL_NAME,
>> + POWER_SUPPLY_PROP_MANUFACTURER,
>> +};
>> +
>> +static const struct power_supply_desc aspire_ec_bat_psy_desc = {
>> + .name = "aspire-ec-bat",
>> + .type = POWER_SUPPLY_TYPE_BATTERY,
>> + .get_property = aspire_ec_bat_psy_get_property,
>> + .properties = aspire_ec_bat_psy_props,
>> + .num_properties = ARRAY_SIZE(aspire_ec_bat_psy_props),
>> +};
>> +
>> +static int aspire_ec_adp_psy_get_property(struct power_supply *psy,
>> + enum power_supply_property psp,
>> + union power_supply_propval *val)
>> +{
>> + struct aspire_ec *ec = power_supply_get_drvdata(psy);
>> + u8 tmp;
>> +
>> + switch (psp) {
>> + case POWER_SUPPLY_PROP_ONLINE:
>> + aspire_ec_ram_read(ec->client, ASPIRE_EC_RAM_ADP, &tmp, sizeof(tmp));
>> + val->intval = !!(tmp & ASPIRE_EC_AC_STATUS);
>> + break;
>> +
>> + default:
>> + return -EINVAL;
>> + }
>> +
>> + return 0;
>> +}
>> +
>> +static enum power_supply_property aspire_ec_adp_psy_props[] = {
>> + POWER_SUPPLY_PROP_ONLINE,
>> +};
>> +
>> +static const struct power_supply_desc aspire_ec_adp_psy_desc = {
>> + .name = "aspire-ec-adp",
>> + .type = POWER_SUPPLY_TYPE_MAINS,
>> + .get_property = aspire_ec_adp_psy_get_property,
>> + .properties = aspire_ec_adp_psy_props,
>> + .num_properties = ARRAY_SIZE(aspire_ec_adp_psy_props),
>> +};
>> +
>> +/*
>> + * USB-C DP Alt mode HPD.
>> + */
>> +
>> +static int aspire_ec_bridge_attach(struct drm_bridge *bridge, enum drm_bridge_attach_flags flags)
>> +{
>> + return flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR ? 0 : -EINVAL;
>> +}
>> +
>> +static void aspire_ec_bridge_update_hpd_work(struct work_struct *work)
>> +{
>> + struct aspire_ec *ec = container_of(work, struct aspire_ec, work);
>> + u8 tmp;
>> +
>> + aspire_ec_ram_read(ec->client, ASPIRE_EC_RAM_HPD_STATUS, &tmp, sizeof(tmp));
>> + if (tmp == ASPIRE_EC_HPD_CONNECTED)
>> + drm_bridge_hpd_notify(&ec->bridge, connector_status_connected);
>> + else
>> + drm_bridge_hpd_notify(&ec->bridge, connector_status_disconnected);
>> +}
>> +
>> +static void aspire_ec_bridge_hpd_enable(struct drm_bridge *bridge)
>> +{
>> + struct aspire_ec *ec = container_of(bridge, struct aspire_ec, bridge);
>> +
>> + schedule_work(&ec->work);
>> +}
>> +
>> +static const struct drm_bridge_funcs aspire_ec_bridge_funcs = {
>> + .hpd_enable = aspire_ec_bridge_hpd_enable,
>> + .attach = aspire_ec_bridge_attach,
>> +};
>> +
>> +/*
>> + * Sysfs attributes.
>> + */
>> +
>> +static ssize_t fn_lock_show(struct device *dev, struct device_attribute *attr, char *buf)
>> +{
>> + struct aspire_ec *ec = i2c_get_clientdata(to_i2c_client(dev));
>> + u8 tmp;
>> +
>> + aspire_ec_ram_read(ec->client, ASPIRE_EC_RAM_KBD_MODE, &tmp, sizeof(tmp));
>> +
>> + return sysfs_emit(buf, "%d\n", !(tmp & ASPIRE_EC_RAM_KBD_MEDIA_ON_TOP));
>
> %u

Ack.

>
>> +}
>> +
>> +static ssize_t fn_lock_store(struct device *dev, struct device_attribute *attr,
>> + const char *buf, size_t count)
>> +{
>> + struct aspire_ec *ec = i2c_get_clientdata(to_i2c_client(dev));
>> + u8 tmp;
>> +
>> + bool state;
>> + int ret;
>> +
>> + ret = kstrtobool(buf, &state);
>> + if (ret)
>> + return ret;
>> +
>> + aspire_ec_ram_read(ec->client, ASPIRE_EC_RAM_KBD_MODE, &tmp, sizeof(tmp));
>> +
>> + if (state)
>> + tmp &= ~ASPIRE_EC_RAM_KBD_MEDIA_ON_TOP;
>> + else
>> + tmp |= ASPIRE_EC_RAM_KBD_MEDIA_ON_TOP;
>> +
>> + aspire_ec_ram_write(ec->client, ASPIRE_EC_RAM_KBD_MODE, tmp);
>> +
>> + return count;
>> +}
>> +
>> +static DEVICE_ATTR_RW(fn_lock);
>> +
>> +static struct attribute *aspire_ec_attributes[] = {
>> + &dev_attr_fn_lock.attr,
>> + NULL
>> +};
>> +
>> +static const struct attribute_group aspire_ec_attribute_group = {
>> + .attrs = aspire_ec_attributes
>> +};
>> +
>> +static int aspire_ec_probe(struct i2c_client *client)
>> +{
>> + struct power_supply_config psy_cfg = {0};
>> + struct device *dev = &client->dev;
>> + struct fwnode_handle *fwnode;
>> + struct aspire_ec *ec;
>> + int ret;
>> + u8 tmp;
>> +
>> + ec = devm_kzalloc(dev, sizeof(*ec), GFP_KERNEL);
>> + if (!ec)
>> + return -ENOMEM;
>> +
>> + ec->client = client;
>> + i2c_set_clientdata(client, ec);
>> +
>> + /* Battery status reports */
>> + psy_cfg.drv_data = ec;
>> + ec->bat_psy = devm_power_supply_register(dev, &aspire_ec_bat_psy_desc, &psy_cfg);
>> + if (IS_ERR(ec->bat_psy))
>> + return dev_err_probe(dev, PTR_ERR(ec->bat_psy),
>> + "Failed to register battery power supply\n");
>> +
>> + ec->adp_psy = devm_power_supply_register(dev, &aspire_ec_adp_psy_desc, &psy_cfg);
>> + if (IS_ERR(ec->adp_psy))
>> + return dev_err_probe(dev, PTR_ERR(ec->adp_psy),
>> + "Failed to register ac power supply\n");
>
> AC

Ack.

>
>> + /* Lid switch */
>> + ec->idev = devm_input_allocate_device(dev);
>> + if (!ec->idev)
>> + return -ENOMEM;
>> +
>> + ec->idev->name = "aspire-ec";
>> + ec->idev->phys = "aspire-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, "Input device register failed\n");
>> +
>> + /* Enable the keyboard fn keys */
>> + tmp = ASPIRE_EC_RAM_KBD_FN_EN | ASPIRE_EC_RAM_KBD_ALWAYS_SET;
>> + tmp |= ASPIRE_EC_RAM_KBD_MEDIA_ON_TOP;
>> + aspire_ec_ram_write(client, ASPIRE_EC_RAM_KBD_MODE, tmp);
>> +
>> + aspire_ec_ram_read(client, ASPIRE_EC_RAM_KBD_MODE_2, &tmp, sizeof(tmp));
>> + tmp |= ASPIRE_EC_RAM_KBD_MEDIA_NOTIFY;
>> + aspire_ec_ram_write(client, ASPIRE_EC_RAM_KBD_MODE_2, tmp);
>> +
>> + ret = devm_device_add_group(dev, &aspire_ec_attribute_group);
>> + if (ret)
>> + return dev_err_probe(dev, ret, "Failed to create attribute group\n");
>
> Can't you just .dev_groups ?

Oh, I've missed that this exists, will use it instead.

>
>
> In general, this felt to be in a quite good shape already, good work!

Thanks for your review!
Nikita

2024-03-12 14:10:10

by Nikita Travkin

[permalink] [raw]
Subject: Re: [PATCH v4 3/4] platform: arm64: Add Acer Aspire 1 embedded controller driver

Bryan O'Donoghue писал(а) 12.03.2024 17:44:
> On 12/03/2024 12:23, Nikita Travkin wrote:
>> Bryan O'Donoghue писал(а) 12.03.2024 16:58:
>>> On 12/03/2024 08:42, Nikita Travkin wrote:
>>>> Acer Aspire 1 is a Snapdragon 7c based laptop. It uses an embedded
>>>> controller to perform a set of various functions, such as:
>>>>
>>>> - Battery and charger monitoring;
>>>> - Keyboard layout control (i.e. fn_lock settings);
>>>> - USB Type-C DP alt mode HPD notifications;
>>>> - Laptop lid status.
>>>>
>>>> Unfortunately, while all this functionality is implemented in ACPI, it's
>>>> currently not possible to use ACPI to boot Linux on such Qualcomm
>>>> devices. To allow Linux to still support the features provided by EC,
>>>> this driver reimplments the relevant ACPI parts. This allows us to boot
>>>> the laptop with Device Tree and retain all the features.
>>>>
>>>> Signed-off-by: Nikita Travkin <[email protected]>
>>>> ---
>>>> drivers/platform/arm64/Kconfig | 16 +
>>>> drivers/platform/arm64/Makefile | 2 +
>>>> drivers/platform/arm64/acer-aspire1-ec.c | 555 +++++++++++++++++++++++++++++++
>>>
>>> You should be listing yourself as a maintainer for a driver you contribute.
>>
>> I always believed that being in the AUTHOR() at the bottom of the driver
>> would guarantee me being in CC for patches, which so far worked great,
>> thus I was always hesitent adding extra entries in MAINTAINERS.
>
> There's no such rule that I'm aware of there.
>
> scripts/get_maintainer.pl won't list a driver author for the CC list
>
> This is a substantial body of code, you should own it upstream.
>

Hm, ack, will add an entry in MAINTAINERS for this.

>>>> + case ASPIRE_EC_EVENT_FG_INF_CHG:
>>>> + /* Notify (\_SB.I2C3.BAT1, 0x81) // Information Change */
>>>
>>> fallthrough;
>>>
>>
>> Hm I believe this would not warn since it's just two values for the same
>> code, just with an extra comment inbetween?
>
> True
>

(Adding anyway given Ilpo also thinks it's better than not)

>>>> + case POWER_SUPPLY_PROP_VOLTAGE_NOW:
>>>> + val->intval = le16_to_cpu(ddat.voltage_now) * 1000;
>>>> + break;
>>>> +
>>>> + case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
>>>> + val->intval = le16_to_cpu(sdat.voltage_design) * 1000;
>>>> + break;
>>>> +
>>>> + case POWER_SUPPLY_PROP_CHARGE_NOW:
>>>> + val->intval = le16_to_cpu(ddat.capacity_now) * 1000;
>>>> + break;
>>>> +
>>>> + case POWER_SUPPLY_PROP_CHARGE_FULL:
>>>> + val->intval = le16_to_cpu(sdat.capacity_full) * 1000;
>>>> + break;
>>>
>>> You could stick this "* 1000" stuff in a macro
>>>
>>
>> acpi/battery.c also explicitly sets the multiplier so I think it's the
>> "common" way to do this.
>
> common != nice
>
> Purely aesthetics but anyway consider decomposing the replication down.
>

(Adding a macro for this)

>>>> +
>>>> + case POWER_SUPPLY_PROP_CAPACITY:
>>>> + val->intval = le16_to_cpu(ddat.capacity_now) * 100;
>>>> + val->intval /= le16_to_cpu(sdat.capacity_full);
>>>> + break;
>>>> +
>>>> + case POWER_SUPPLY_PROP_CURRENT_NOW:
>>>> + val->intval = (s16)le16_to_cpu(ddat.current_now) * 1000;
>>>> + break;
>>>> +
>>>> + case POWER_SUPPLY_PROP_PRESENT:
>>>> + val->intval = !!(ddat.flags & ASPIRE_EC_FG_FLAG_PRESENT);
>>>> + break;
>>>> +
>>>> + case POWER_SUPPLY_PROP_SCOPE:
>>>> + val->intval = POWER_SUPPLY_SCOPE_SYSTEM;
>>>> + break;
>>>> +
>>>> + case POWER_SUPPLY_PROP_MODEL_NAME:
>>>> + if (sdat.model_id - 1 < ARRAY_SIZE(aspire_ec_bat_psy_battery_model))
>>>> + val->strval = aspire_ec_bat_psy_battery_model[sdat.model_id - 1];
>>>> + else
>>>> + val->strval = "Unknown";
>>>> + break;
>>>> +
>>>> + case POWER_SUPPLY_PROP_MANUFACTURER:
>>>> + if (sdat.vendor_id - 3 < ARRAY_SIZE(aspire_ec_bat_psy_battery_vendor))
>>>> + val->strval = aspire_ec_bat_psy_battery_vendor[sdat.vendor_id - 3];
>>>
>>> How does this -3 offset not underflow ?
>>>
>>
>> vendor_id here is unsigned so the if check would actually overflow,
>> though explaining that I guess it's better to be explicit there and let
>> the compiler optimize that check away anyway... I will update the if
>> condition with an extra (id >= 3).
>
> What's the "3" about though, that's what's not jumping out at me here.
>

Ah, well... the 3 comes from a big if/elseif table in the decompiled dsdt
which starts at 3... I will add a small comment near it.

>>
>>> Seems a bit dodgy to me - can you add a comment to the code to explain ? Its not immediately obvious the -3 is OK.
>>>
>>> Also could you take an index instead of replicating the -value stepdown each time ?
>>>
>>> int myindex = sdat.model_id - 1;
>>>
>>> if (myindex < someconstraint)
>>> strval = somearry[myindex];
>>>
>>
>> I decided against adding a dedicated index variable since there is only
>> one actual use for each, so it's easy to see where it goes.
>
> But you do it twice which is why I'm suggesting take an index and do it once.
>
> Then add
>
> Reviewed-by: Bryan O'Donoghue <[email protected]>

Ack, given there is also >0, will add str_index variable for the offset
index.

Thanks!
Nikita

2024-03-12 14:39:15

by Rob Herring (Arm)

[permalink] [raw]
Subject: Re: [PATCH v4 1/4] dt-bindings: platform: Add Acer Aspire 1 EC


On Tue, 12 Mar 2024 13:42:07 +0500, Nikita Travkin wrote:
> Add binding for the EC found in the Acer Aspire 1 laptop.
>
> Signed-off-by: Nikita Travkin <[email protected]>
> ---
> .../bindings/platform/acer,aspire1-ec.yaml | 60 ++++++++++++++++++++++
> 1 file changed, 60 insertions(+)
>

Reviewed-by: Rob Herring <[email protected]>


2024-03-12 15:17:22

by Ilpo Järvinen

[permalink] [raw]
Subject: Re: [PATCH v4 2/4] platform: Add ARM64 platform directory

On Tue, 12 Mar 2024, Nikita Travkin wrote:

> Some ARM64 based laptops and computers require vendor/board specific
> drivers for their embedded controllers. Even though usually the most
> important functionality of those devices is implemented inside ACPI,
> unfortunately Linux doesn't currently have great support for ACPI on
> platforms like Qualcomm Snapdragon that are used in most ARM64 laptops
> today. Instead Linux relies on Device Tree for Qualcomm based devices
> and it's significantly easier to reimplement the EC functionality in
> a dedicated driver than to make use of ACPI code.
>
> This commit introduces a new platform/arm64 subdirectory to give a
> place to such drivers for EC-like devices.
>
> A new MAINTAINERS entry is added for this directory. Patches to files in
> this directory will be taken up by the platform-drivers-x86 team (i.e.
> Hans de Goede and Mark Gross).

Mark -> me.

> +ARM64 PLATFORM DRIVERS
> +M: Hans de Goede <[email protected]>
> +M: Ilpo J?rvinen <[email protected]>
> +L: [email protected]
> +S: Maintained
> +Q: https://patchwork.kernel.org/project/platform-driver-x86/list/
> +T: git git://git.kernel.org/pub/scm/linux/kernel/git/pdx86/platform-drivers-x86.git
> +F: drivers/platform/arm64/

Is some ARM64 person going to pay attention to these patches (you or
perhaps somebody else)?

It's perfectly fine to have some ARM64 person(s) listed as an additional
maintainer there even if the patches themselves are routed through Hans
and me (and pdx86 tree). With Mellanox and Surface platform drivers which
are also routed through pdx86 tree, we have Hans + me + 3rd person listed
as maintainers.

(This is not to force anything on anyone but it could be beneficial if
somebody more familiar with ARM64 is in the loop.)

--
i.

2024-03-12 15:22:03

by Ilpo Järvinen

[permalink] [raw]
Subject: Re: [PATCH v4 2/4] platform: Add ARM64 platform directory

On Tue, 12 Mar 2024, Bryan O'Donoghue wrote:

> On 12/03/2024 08:42, Nikita Travkin wrote:
> > Some ARM64 based laptops and computers require vendor/board specific
> > drivers for their embedded controllers. Even though usually the most
> > important functionality of those devices is implemented inside ACPI,
> > unfortunately Linux doesn't currently have great support for ACPI on
> > platforms like Qualcomm Snapdragon that are used in most ARM64 laptops
> > today. Instead Linux relies on Device Tree for Qualcomm based devices
> > and it's significantly easier to reimplement the EC functionality in
> > a dedicated driver than to make use of ACPI code.
> >
> > This commit introduces a new platform/arm64 subdirectory to give a
> > place to such drivers for EC-like devices.
> >
> > A new MAINTAINERS entry is added for this directory. Patches to files in
> > this directory will be taken up by the platform-drivers-x86 team (i.e.
> > Hans de Goede and Mark Gross).
> >
> > Signed-off-by: Nikita Travkin <[email protected]>
> > ---
> > MAINTAINERS | 9 +++++++++
> > drivers/platform/Kconfig | 2 ++
> > drivers/platform/Makefile | 1 +
> > drivers/platform/arm64/Kconfig | 19 +++++++++++++++++++
> > drivers/platform/arm64/Makefile | 6 ++++++
> > 5 files changed, 37 insertions(+)
> >
> > diff --git a/MAINTAINERS b/MAINTAINERS
> > index b43102ca365d..ec8d706a99aa 100644
> > --- a/MAINTAINERS
> > +++ b/MAINTAINERS
> > @@ -3050,6 +3050,15 @@ F: drivers/mmc/host/sdhci-of-arasan.c
> > N: zynq
> > N: xilinx
> > +ARM64 PLATFORM DRIVERS
> > +M: Hans de Goede <[email protected]>
> > +M: Ilpo Järvinen <[email protected]>
> > +L: [email protected]
> > +S: Maintained
> > +Q: https://patchwork.kernel.org/project/platform-driver-x86/list/
> > +T: git
> > git://git.kernel.org/pub/scm/linux/kernel/git/pdx86/platform-drivers-x86.git
>
> Surely some sort of Arm specific mailing list should be added here ?
> platform-drivers-x86 for arm64 platform drivers standalone, makes little
> sense.
>
> Perhaps for each new SoC class added - you could add the appropriate mailing
> list linux-arm-msm is suspiciously missing from the list even though the only
> driver that will live in this directory after this series is a qcom based
> device.
>
> And if tomorrow someone added a Rockchip based EC controller then you'd assume
> the rockchip mailing list should get a ping.

While generic arm mailing list is perhaps lacking too, for specific
driver related lists it is better to add separate MAINTAINERS entries for
the particular driver and put L: there.

--
i.

2024-03-12 17:56:55

by Ilpo Järvinen

[permalink] [raw]
Subject: Re: [PATCH v4 3/4] platform: arm64: Add Acer Aspire 1 embedded controller driver

On Tue, 12 Mar 2024, Nikita Travkin wrote:

> Acer Aspire 1 is a Snapdragon 7c based laptop. It uses an embedded
> controller to perform a set of various functions, such as:
>
> - Battery and charger monitoring;
> - Keyboard layout control (i.e. fn_lock settings);
> - USB Type-C DP alt mode HPD notifications;
> - Laptop lid status.
>
> Unfortunately, while all this functionality is implemented in ACPI, it's
> currently not possible to use ACPI to boot Linux on such Qualcomm
> devices. To allow Linux to still support the features provided by EC,
> this driver reimplments the relevant ACPI parts. This allows us to boot
> the laptop with Device Tree and retain all the features.
>
> Signed-off-by: Nikita Travkin <[email protected]>
> ---
> drivers/platform/arm64/Kconfig | 16 +
> drivers/platform/arm64/Makefile | 2 +
> drivers/platform/arm64/acer-aspire1-ec.c | 555 +++++++++++++++++++++++++++++++
> 3 files changed, 573 insertions(+)
>
> diff --git a/drivers/platform/arm64/Kconfig b/drivers/platform/arm64/Kconfig
> index 644b83ede093..07d47879a9e3 100644
> --- a/drivers/platform/arm64/Kconfig
> +++ b/drivers/platform/arm64/Kconfig
> @@ -16,4 +16,20 @@ menuconfig ARM64_PLATFORM_DEVICES
>
> if ARM64_PLATFORM_DEVICES
>
> +config EC_ACER_ASPIRE1
> + tristate "Acer Aspire 1 Emedded Controller driver"
> + depends on I2C
> + depends on DRM
> + depends on POWER_SUPPLY
> + depends on INPUT
> + help
> + Say Y here to enable the EC driver for the (Snapdragon-based)
> + Acer Aspire 1 laptop. The EC handles battery and charging
> + monitoring as well as some misc functions like the lid sensor
> + and USB Type-C DP HPD events.
> +
> + This driver provides battery and AC status support for the mentioned
> + laptop where this information is not properly exposed via the
> + standard ACPI devices.
> +
> endif # ARM64_PLATFORM_DEVICES
> diff --git a/drivers/platform/arm64/Makefile b/drivers/platform/arm64/Makefile
> index f91cdc7155e2..4fcc9855579b 100644
> --- a/drivers/platform/arm64/Makefile
> +++ b/drivers/platform/arm64/Makefile
> @@ -4,3 +4,5 @@
> #
> # This dir should only include drivers for EC-like devices.
> #
> +
> +obj-$(CONFIG_EC_ACER_ASPIRE1) += acer-aspire1-ec.o
> diff --git a/drivers/platform/arm64/acer-aspire1-ec.c b/drivers/platform/arm64/acer-aspire1-ec.c
> new file mode 100644
> index 000000000000..3941e24c5c7c
> --- /dev/null
> +++ b/drivers/platform/arm64/acer-aspire1-ec.c
> @@ -0,0 +1,555 @@
> +// SPDX-License-Identifier: GPL-2.0-only
> +/* Copyright (c) 2023, Nikita Travkin <[email protected]> */
> +
> +#include <linux/module.h>
> +#include <linux/platform_device.h>
> +#include <linux/power_supply.h>
> +#include <linux/i2c.h>
> +#include <linux/input.h>
> +#include <linux/delay.h>
> +
> +#include <linux/usb/typec_mux.h>
> +#include <drm/drm_bridge.h>
> +
> +#define ASPIRE_EC_EVENT 0x05
> +
> +#define ASPIRE_EC_EVENT_WATCHDOG 0x20
> +#define ASPIRE_EC_EVENT_KBD_BKL_ON 0x57
> +#define ASPIRE_EC_EVENT_KBD_BKL_OFF 0x58
> +#define ASPIRE_EC_EVENT_LID_CLOSE 0x9b
> +#define ASPIRE_EC_EVENT_LID_OPEN 0x9c
> +#define ASPIRE_EC_EVENT_BKL_UNBLANKED 0x9d
> +#define ASPIRE_EC_EVENT_BKL_BLANKED 0x9e
> +#define ASPIRE_EC_EVENT_FG_INF_CHG 0x85
> +#define ASPIRE_EC_EVENT_FG_STA_CHG 0xc6
> +#define ASPIRE_EC_EVENT_HPD_DIS 0xa3
> +#define ASPIRE_EC_EVENT_HPD_CON 0xa4
> +
> +#define ASPIRE_EC_FG_DYNAMIC 0x07
> +#define ASPIRE_EC_FG_STATIC 0x08
> +
> +#define ASPIRE_EC_FG_FLAG_PRESENT BIT(0)

Add #include <linux/bits.h>

> +#define ASPIRE_EC_FG_FLAG_FULL BIT(1)
> +#define ASPIRE_EC_FG_FLAG_DISCHARGING BIT(2)
> +#define ASPIRE_EC_FG_FLAG_CHARGING BIT(3)
> +
> +#define ASPIRE_EC_RAM_READ 0x20
> +#define ASPIRE_EC_RAM_WRITE 0x21
> +
> +#define ASPIRE_EC_RAM_WATCHDOG 0x19
> +#define ASPIRE_EC_RAM_KBD_MODE 0x43
> +
> +#define ASPIRE_EC_RAM_KBD_FN_EN BIT(0)
> +#define ASPIRE_EC_RAM_KBD_MEDIA_ON_TOP BIT(5)
> +#define ASPIRE_EC_RAM_KBD_ALWAYS_SET BIT(6)
> +#define ASPIRE_EC_RAM_KBD_NUM_LAYER_EN BIT(7)
> +
> +#define ASPIRE_EC_RAM_KBD_MODE_2 0x60
> +
> +#define ASPIRE_EC_RAM_KBD_MEDIA_NOTIFY BIT(3)
> +
> +#define ASPIRE_EC_RAM_HPD_STATUS 0xf4
> +#define ASPIRE_EC_HPD_CONNECTED 0x03
> +
> +#define ASPIRE_EC_RAM_LID_STATUS 0x4c
> +#define ASPIRE_EC_LID_OPEN BIT(6)
> +
> +#define ASPIRE_EC_RAM_ADP 0x40
> +#define ASPIRE_EC_AC_STATUS BIT(0)
> +
> +struct aspire_ec {
> + struct i2c_client *client;
> + struct power_supply *bat_psy;
> + struct power_supply *adp_psy;
> + struct input_dev *idev;
> +
> + bool bridge_configured;
> + struct drm_bridge bridge;
> + struct work_struct work;

Include missing.

> +static int aspire_ec_ram_read(struct i2c_client *client, u8 off, u8 *data, u8 data_len)
> +{
> + i2c_smbus_write_byte_data(client, ASPIRE_EC_RAM_READ, off);
> + i2c_smbus_read_i2c_block_data(client, ASPIRE_EC_RAM_READ, data_len, data);
> + return 0;
> +}
> +
> +static int aspire_ec_ram_write(struct i2c_client *client, u8 off, u8 data)
> +{
> + u8 tmp[2] = {off, data};
> +
> + i2c_smbus_write_i2c_block_data(client, ASPIRE_EC_RAM_WRITE, sizeof(tmp), tmp);
> + return 0;
> +}
> +
> +static irqreturn_t aspire_ec_irq_handler(int irq, void *data)
> +{
> + struct aspire_ec *ec = data;
> + int id;
> + u8 tmp;
> +
> + /*
> + * The original ACPI firmware actually has a small sleep in the handler.
> + *
> + * It seems like in most cases it's not needed but when the device
> + * just exits suspend, our i2c driver has a brief time where data
> + * transfer is not possible yet. So this delay allows us to suppress
> + * quite a bunch of spurious error messages in dmesg. Thus it's kept.
> + */
> + usleep_range(15000, 30000);
> +
> + id = i2c_smbus_read_byte_data(ec->client, ASPIRE_EC_EVENT);
> + if (id < 0) {
> + dev_err(&ec->client->dev, "Failed to read event id: %pe\n", ERR_PTR(id));
> + return IRQ_HANDLED;
> + }
> +
> + switch (id) {
> + case 0x0: /* No event */
> + break;
> +
> + case ASPIRE_EC_EVENT_WATCHDOG:
> + /*
> + * Here acpi responds to the event and clears some bit.
> + * Notify (\_SB.I2C3.BAT1, 0x81) // Information Change
> + * Notify (\_SB.I2C3.ADP1, 0x80) // Status Change
> + */
> + aspire_ec_ram_read(ec->client, ASPIRE_EC_RAM_WATCHDOG, &tmp, sizeof(tmp));
> + aspire_ec_ram_write(ec->client, ASPIRE_EC_RAM_WATCHDOG, tmp & 0xbf);

Magic 0xbf? Is this a clear bit operation which should use
tmp & ~DEFINEDNAME
?

> + break;
> +
> + case ASPIRE_EC_EVENT_LID_CLOSE:
> + /* Notify (\_SB.LID0, 0x80) // Status Change */
> + input_report_switch(ec->idev, SW_LID, 1);
> + input_sync(ec->idev);
> + break;
> +
> + case ASPIRE_EC_EVENT_LID_OPEN:
> + /* Notify (\_SB.LID0, 0x80) // Status Change */
> + input_report_switch(ec->idev, SW_LID, 0);
> + input_sync(ec->idev);
> + break;
> +
> + case ASPIRE_EC_EVENT_FG_INF_CHG:
> + /* Notify (\_SB.I2C3.BAT1, 0x81) // Information Change */

fallthrough;

I'm not 100% sure if it's needed because you only have a comment there but
it surely doesn't hurt to have it.

> + case ASPIRE_EC_EVENT_FG_STA_CHG:
> + /* Notify (\_SB.I2C3.BAT1, 0x80) // Status Change */
> + power_supply_changed(ec->bat_psy);
> + power_supply_changed(ec->adp_psy);
> + break;
> +
> + case ASPIRE_EC_EVENT_HPD_DIS:
> + if (ec->bridge_configured)
> + drm_bridge_hpd_notify(&ec->bridge, connector_status_disconnected);
> + break;
> +
> + case ASPIRE_EC_EVENT_HPD_CON:
> + if (ec->bridge_configured)
> + drm_bridge_hpd_notify(&ec->bridge, connector_status_connected);
> + break;
> +
> + case ASPIRE_EC_EVENT_BKL_BLANKED:
> + case ASPIRE_EC_EVENT_BKL_UNBLANKED:
> + /* Display backlight blanked on FN+F6. No action needed. */
> + break;
> +
> + case ASPIRE_EC_EVENT_KBD_BKL_ON:
> + case ASPIRE_EC_EVENT_KBD_BKL_OFF:
> + /*
> + * There is a keyboard backlight connector on Aspire 1 that is
> + * controlled by FN+F8. There is no kb backlight on the device though.
> + * Seems like this is used on other devices like Acer Spin 7.
> + * No action needed.
> + */
> + break;
> +
> + default:
> + dev_warn(&ec->client->dev, "Unknown event id=0x%x\n", id);
> + }
> +
> + return IRQ_HANDLED;
> +}
> +
> +/*
> + * Power supply.
> + */
> +
> +struct aspire_ec_bat_psy_static_data {
> + u8 unk1;
> + u8 flags;
> + __le16 unk2;
> + __le16 voltage_design;
> + __le16 capacity_full;
> + __le16 unk3;
> + __le16 serial;
> + u8 model_id;
> + u8 vendor_id;
> +} __packed;
> +
> +static const char * const aspire_ec_bat_psy_battery_model[] = {
> + "AP18C4K",
> + "AP18C8K",
> + "AP19B8K",
> + "AP16M4J",
> + "AP16M5J",
> +};
> +
> +static const char * const aspire_ec_bat_psy_battery_vendor[] = {
> + "SANYO",
> + "SONY",
> + "PANASONIC",
> + "SAMSUNG",
> + "SIMPLO",
> + "MOTOROLA",
> + "CELXPERT",
> + "LGC",
> + "GETAC",
> + "MURATA",
> +};
> +
> +struct aspire_ec_bat_psy_dynamic_data {
> + u8 unk1;
> + u8 flags;
> + u8 unk2;
> + __le16 capacity_now;
> + __le16 voltage_now;
> + __le16 current_now;
> + __le16 unk3;
> + __le16 unk4;
> +} __packed;

What are the ARM64 rules when it comes to unaligned access? If it's not
always supported, you need to use get_unaligned_le16() for accessing those
__le16 fields.

> +static int aspire_ec_bat_psy_get_property(struct power_supply *psy,
> + enum power_supply_property psp,
> + union power_supply_propval *val)
> +{
> + struct aspire_ec *ec = power_supply_get_drvdata(psy);
> + struct aspire_ec_bat_psy_static_data sdat;
> + struct aspire_ec_bat_psy_dynamic_data ddat;
> +
> + i2c_smbus_read_i2c_block_data(ec->client, ASPIRE_EC_FG_STATIC, sizeof(sdat), (u8 *)&sdat);
> + i2c_smbus_read_i2c_block_data(ec->client, ASPIRE_EC_FG_DYNAMIC, sizeof(ddat), (u8 *)&ddat);
> +
> + switch (psp) {
> + case POWER_SUPPLY_PROP_STATUS:
> + val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
> + if (ddat.flags & ASPIRE_EC_FG_FLAG_CHARGING)
> + val->intval = POWER_SUPPLY_STATUS_CHARGING;
> + else if (ddat.flags & ASPIRE_EC_FG_FLAG_DISCHARGING)
> + val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
> + else if (ddat.flags & ASPIRE_EC_FG_FLAG_FULL)
> + val->intval = POWER_SUPPLY_STATUS_FULL;
> + break;
> +
> + case POWER_SUPPLY_PROP_VOLTAGE_NOW:
> + val->intval = le16_to_cpu(ddat.voltage_now) * 1000;

linux/units.h provides something that can often be used instead of 10^n
literals.

> + break;
> +
> + case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
> + val->intval = le16_to_cpu(sdat.voltage_design) * 1000;
> + break;
> +
> + case POWER_SUPPLY_PROP_CHARGE_NOW:
> + val->intval = le16_to_cpu(ddat.capacity_now) * 1000;
> + break;
> +
> + case POWER_SUPPLY_PROP_CHARGE_FULL:
> + val->intval = le16_to_cpu(sdat.capacity_full) * 1000;
> + break;
> +
> + case POWER_SUPPLY_PROP_CAPACITY:
> + val->intval = le16_to_cpu(ddat.capacity_now) * 100;
> + val->intval /= le16_to_cpu(sdat.capacity_full);
> + break;
> +
> + case POWER_SUPPLY_PROP_CURRENT_NOW:
> + val->intval = (s16)le16_to_cpu(ddat.current_now) * 1000;
> + break;
> +
> + case POWER_SUPPLY_PROP_PRESENT:
> + val->intval = !!(ddat.flags & ASPIRE_EC_FG_FLAG_PRESENT);
> + break;
> +
> + case POWER_SUPPLY_PROP_SCOPE:
> + val->intval = POWER_SUPPLY_SCOPE_SYSTEM;
> + break;
> +
> + case POWER_SUPPLY_PROP_MODEL_NAME:
> + if (sdat.model_id - 1 < ARRAY_SIZE(aspire_ec_bat_psy_battery_model))
> + val->strval = aspire_ec_bat_psy_battery_model[sdat.model_id - 1];

Underflow handling (also here, the other place was mentioned already)?

> + else
> + val->strval = "Unknown";
> + break;
> +
> + case POWER_SUPPLY_PROP_MANUFACTURER:
> + if (sdat.vendor_id - 3 < ARRAY_SIZE(aspire_ec_bat_psy_battery_vendor))
> + val->strval = aspire_ec_bat_psy_battery_vendor[sdat.vendor_id - 3];
> + else
> + val->strval = "Unknown";
> + break;
> +
> + default:
> + return -EINVAL;
> + }
> +
> + return 0;
> +}
> +
> +static enum power_supply_property aspire_ec_bat_psy_props[] = {
> + POWER_SUPPLY_PROP_STATUS,
> + POWER_SUPPLY_PROP_VOLTAGE_NOW,
> + POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
> + POWER_SUPPLY_PROP_CHARGE_NOW,
> + POWER_SUPPLY_PROP_CHARGE_FULL,
> + POWER_SUPPLY_PROP_CAPACITY,
> + POWER_SUPPLY_PROP_CURRENT_NOW,
> + POWER_SUPPLY_PROP_PRESENT,
> + POWER_SUPPLY_PROP_SCOPE,
> + POWER_SUPPLY_PROP_MODEL_NAME,
> + POWER_SUPPLY_PROP_MANUFACTURER,
> +};
> +
> +static const struct power_supply_desc aspire_ec_bat_psy_desc = {
> + .name = "aspire-ec-bat",
> + .type = POWER_SUPPLY_TYPE_BATTERY,
> + .get_property = aspire_ec_bat_psy_get_property,
> + .properties = aspire_ec_bat_psy_props,
> + .num_properties = ARRAY_SIZE(aspire_ec_bat_psy_props),
> +};
> +
> +static int aspire_ec_adp_psy_get_property(struct power_supply *psy,
> + enum power_supply_property psp,
> + union power_supply_propval *val)
> +{
> + struct aspire_ec *ec = power_supply_get_drvdata(psy);
> + u8 tmp;
> +
> + switch (psp) {
> + case POWER_SUPPLY_PROP_ONLINE:
> + aspire_ec_ram_read(ec->client, ASPIRE_EC_RAM_ADP, &tmp, sizeof(tmp));
> + val->intval = !!(tmp & ASPIRE_EC_AC_STATUS);
> + break;
> +
> + default:
> + return -EINVAL;
> + }
> +
> + return 0;
> +}
> +
> +static enum power_supply_property aspire_ec_adp_psy_props[] = {
> + POWER_SUPPLY_PROP_ONLINE,
> +};
> +
> +static const struct power_supply_desc aspire_ec_adp_psy_desc = {
> + .name = "aspire-ec-adp",
> + .type = POWER_SUPPLY_TYPE_MAINS,
> + .get_property = aspire_ec_adp_psy_get_property,
> + .properties = aspire_ec_adp_psy_props,
> + .num_properties = ARRAY_SIZE(aspire_ec_adp_psy_props),
> +};
> +
> +/*
> + * USB-C DP Alt mode HPD.
> + */
> +
> +static int aspire_ec_bridge_attach(struct drm_bridge *bridge, enum drm_bridge_attach_flags flags)
> +{
> + return flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR ? 0 : -EINVAL;
> +}
> +
> +static void aspire_ec_bridge_update_hpd_work(struct work_struct *work)
> +{
> + struct aspire_ec *ec = container_of(work, struct aspire_ec, work);
> + u8 tmp;
> +
> + aspire_ec_ram_read(ec->client, ASPIRE_EC_RAM_HPD_STATUS, &tmp, sizeof(tmp));
> + if (tmp == ASPIRE_EC_HPD_CONNECTED)
> + drm_bridge_hpd_notify(&ec->bridge, connector_status_connected);
> + else
> + drm_bridge_hpd_notify(&ec->bridge, connector_status_disconnected);
> +}
> +
> +static void aspire_ec_bridge_hpd_enable(struct drm_bridge *bridge)
> +{
> + struct aspire_ec *ec = container_of(bridge, struct aspire_ec, bridge);
> +
> + schedule_work(&ec->work);
> +}
> +
> +static const struct drm_bridge_funcs aspire_ec_bridge_funcs = {
> + .hpd_enable = aspire_ec_bridge_hpd_enable,
> + .attach = aspire_ec_bridge_attach,
> +};
> +
> +/*
> + * Sysfs attributes.
> + */
> +
> +static ssize_t fn_lock_show(struct device *dev, struct device_attribute *attr, char *buf)
> +{
> + struct aspire_ec *ec = i2c_get_clientdata(to_i2c_client(dev));
> + u8 tmp;
> +
> + aspire_ec_ram_read(ec->client, ASPIRE_EC_RAM_KBD_MODE, &tmp, sizeof(tmp));
> +
> + return sysfs_emit(buf, "%d\n", !(tmp & ASPIRE_EC_RAM_KBD_MEDIA_ON_TOP));

%u

> +}
> +
> +static ssize_t fn_lock_store(struct device *dev, struct device_attribute *attr,
> + const char *buf, size_t count)
> +{
> + struct aspire_ec *ec = i2c_get_clientdata(to_i2c_client(dev));
> + u8 tmp;
> +
> + bool state;
> + int ret;
> +
> + ret = kstrtobool(buf, &state);
> + if (ret)
> + return ret;
> +
> + aspire_ec_ram_read(ec->client, ASPIRE_EC_RAM_KBD_MODE, &tmp, sizeof(tmp));
> +
> + if (state)
> + tmp &= ~ASPIRE_EC_RAM_KBD_MEDIA_ON_TOP;
> + else
> + tmp |= ASPIRE_EC_RAM_KBD_MEDIA_ON_TOP;
> +
> + aspire_ec_ram_write(ec->client, ASPIRE_EC_RAM_KBD_MODE, tmp);
> +
> + return count;
> +}
> +
> +static DEVICE_ATTR_RW(fn_lock);
> +
> +static struct attribute *aspire_ec_attributes[] = {
> + &dev_attr_fn_lock.attr,
> + NULL
> +};
> +
> +static const struct attribute_group aspire_ec_attribute_group = {
> + .attrs = aspire_ec_attributes
> +};
> +
> +static int aspire_ec_probe(struct i2c_client *client)
> +{
> + struct power_supply_config psy_cfg = {0};
> + struct device *dev = &client->dev;
> + struct fwnode_handle *fwnode;
> + struct aspire_ec *ec;
> + int ret;
> + u8 tmp;
> +
> + ec = devm_kzalloc(dev, sizeof(*ec), GFP_KERNEL);
> + if (!ec)
> + return -ENOMEM;
> +
> + ec->client = client;
> + i2c_set_clientdata(client, ec);
> +
> + /* Battery status reports */
> + psy_cfg.drv_data = ec;
> + ec->bat_psy = devm_power_supply_register(dev, &aspire_ec_bat_psy_desc, &psy_cfg);
> + if (IS_ERR(ec->bat_psy))
> + return dev_err_probe(dev, PTR_ERR(ec->bat_psy),
> + "Failed to register battery power supply\n");
> +
> + ec->adp_psy = devm_power_supply_register(dev, &aspire_ec_adp_psy_desc, &psy_cfg);
> + if (IS_ERR(ec->adp_psy))
> + return dev_err_probe(dev, PTR_ERR(ec->adp_psy),
> + "Failed to register ac power supply\n");

AC

> + /* Lid switch */
> + ec->idev = devm_input_allocate_device(dev);
> + if (!ec->idev)
> + return -ENOMEM;
> +
> + ec->idev->name = "aspire-ec";
> + ec->idev->phys = "aspire-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, "Input device register failed\n");
> +
> + /* Enable the keyboard fn keys */
> + tmp = ASPIRE_EC_RAM_KBD_FN_EN | ASPIRE_EC_RAM_KBD_ALWAYS_SET;
> + tmp |= ASPIRE_EC_RAM_KBD_MEDIA_ON_TOP;
> + aspire_ec_ram_write(client, ASPIRE_EC_RAM_KBD_MODE, tmp);
> +
> + aspire_ec_ram_read(client, ASPIRE_EC_RAM_KBD_MODE_2, &tmp, sizeof(tmp));
> + tmp |= ASPIRE_EC_RAM_KBD_MEDIA_NOTIFY;
> + aspire_ec_ram_write(client, ASPIRE_EC_RAM_KBD_MODE_2, tmp);
> +
> + ret = devm_device_add_group(dev, &aspire_ec_attribute_group);
> + if (ret)
> + return dev_err_probe(dev, ret, "Failed to create attribute group\n");

Can't you just .dev_groups ?


In general, this felt to be in a quite good shape already, good work!

--
i.