mfd: Add support for Asus Transformer embedded controller
Subsystems (6)
v6 cover mfd: Add support for Asus Transformer embedded controller
Add support for embedded controller used in Asus Transformers for managing power and input functions.
---
Changes in v2:
- converted sysfs debug exports into debugfs
- added kernel-doc comments for exposed functions
- fixed minor typos and inconsistencies
Changes in v3:
- dropped DockRAM commits (both schema and driver)
- integrated DockRAM functionality directly into the controller driver
- EC schema moved to embedded controllers folder
- removed all cell descriptions from the schema
- removed all compatibles from the cell drivers
- adjusted naming conventions to better align with the ASUS Transformers
- defined EC variant sets to provide coverage for all known devices
Changes in v4:
- grouped known programming models of EC chronologically (both schema and driver)
- call debugfs init only if CONFIG_DEBUG_FS is enabled
Changes in v5:
- added asus,tf600t-ec-dock compatible to schema
- rebased on top of v7.0
- kzalloc > kzalloc_obj in keys and kbc
Changes in v6:
- removed asus_ec_handle_smi casting
- asus_dockram_access_ctl > asus_ec_get_ctl in control_reg_*
- fixed battery Kconfig description
---
Michał Mirosław (6):
mfd: Add driver for ASUS Transformer embedded controller
input: serio: Add driver for ASUS Transformer dock keyboard and
touchpad
input: keyboard: Add driver for ASUS Transformer dock multimedia keys
leds: Add driver for ASUS Transformer LEDs
power: supply: Add driver for ASUS Transformer battery
power: supply: Add charger driver for Asus Transformers
Svyatoslav Ryhel (1):
dt-bindings: embedded-controller: document ASUS Transformer EC
.../asus,tf201-ec-pad.yaml | 119 +++ drivers/input/keyboard/Kconfig | 10 + drivers/input/keyboard/Makefile | 1 + .../input/keyboard/asus-transformer-ec-keys.c | 272 +++++++ drivers/input/serio/Kconfig | 15 + drivers/input/serio/Makefile | 1 + drivers/input/serio/asus-transformer-ec-kbc.c | 147 ++++ drivers/leds/Kconfig | 11 + drivers/leds/Makefile | 1 + drivers/leds/leds-asus-transformer-ec.c | 79 ++ drivers/mfd/Kconfig | 14 + drivers/mfd/Makefile | 1 + drivers/mfd/asus-transformer-ec.c | 762 ++++++++++++++++++ drivers/power/supply/Kconfig | 22 + drivers/power/supply/Makefile | 2 + .../supply/asus-transformer-ec-battery.c | 272 +++++++ .../supply/asus-transformer-ec-charger.c | 193 +++++ include/linux/mfd/asus-transformer-ec.h | 162 ++++ 18 files changed, 2084 insertions(+) create mode 100644 Documentation/devicetree/bindings/embedded-controller/asus,tf201-ec-pad.yaml create mode 100644 drivers/input/keyboard/asus-transformer-ec-keys.c create mode 100644 drivers/input/serio/asus-transformer-ec-kbc.c create mode 100644 drivers/leds/leds-asus-transformer-ec.c create mode 100644 drivers/mfd/asus-transformer-ec.c create mode 100644 drivers/power/supply/asus-transformer-ec-battery.c create mode 100644 drivers/power/supply/asus-transformer-ec-charger.c create mode 100644 include/linux/mfd/asus-transformer-ec.h
Signature · 1 line
2.51.0
PATCH v6 1/7 dt-bindings: embedded-controller: document ASUS Transformer EC
Document embedded controller used in ASUS Transformer device series.
--- .../asus,tf201-ec-pad.yaml | 119 ++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 Documentation/devicetree/bindings/embedded-controller/asus,tf201-ec-pad.yaml
Documentation/devicetree/bindings/embedded-controller/asus,tf201-ec-pad.yaml new file +119 -0
@@ -0,0 +1,119 @@ +# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/embedded-controller/asus,tf201-ec-pad.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: ASUS Transformer's Embedded Controller + +description: + Several Nuvoton based Embedded Controllers attached to an I2C bus, + running a custom ASUS firmware, specific to the ASUS Transformer + device series. + +maintainers: + - Svyatoslav Ryhel <[email protected]> + +properties: + compatible: + description: + The 'pad' suffix is used for the controller within the tablet, while + the 'dock' suffix refers to the controller in the mobile dock keyboard. + oneOf: + - enum: + - asus,sl101-ec-dock + - asus,tf101-ec-dock + - asus,tf201-ec-pad + - asus,tf600t-ec-dock + - asus,tf600t-ec-pad + + - items: + - enum: + - asus,tf101g-ec-dock + - asus,tf201-ec-dock + - asus,tf300t-ec-dock + - asus,tf300tg-ec-dock + - asus,tf300tl-ec-dock + - asus,tf700t-ec-dock + - const: asus,tf101-ec-dock + + - items: + - enum: + - asus,tf300t-ec-pad + - asus,tf300tg-ec-pad + - asus,tf300tl-ec-pad + - asus,tf700t-ec-pad + - const: asus,tf201-ec-pad + + - items: + - enum: + - asus,tf701t-ec-dock + - const: asus,tf600t-ec-dock + + - items: + - enum: + - asus,p1801-t-ec-pad + - asus,tf701t-ec-pad + - const: asus,tf600t-ec-pad + + reg: + description: + The ASUS Transformer EC has a main I2C address and an associated + DockRAM device, which provides power-related functions for the + embedded controller. Both addresses are required for operation. + minItems: 2 + + reg-names: + items: + - const: ec + - const: dockram + + interrupts: + maxItems: 1 + + request-gpios: + maxItems: 1 + +required: + - compatible + - reg + - reg-names + +allOf: + - $ref: /schemas/power/supply/power-supply.yaml + - if: + properties: + compatible: + not: + contains: + const: asus,tf600t-ec-dock + then: + required: + - interrupts + - request-gpios + +unevaluatedProperties: false + +examples: + - | + #include <dt-bindings/gpio/gpio.h> + #include <dt-bindings/interrupt-controller/irq.h> + + i2c { + #address-cells = <1>; + #size-cells = <0>; + + embedded-controller@19 { + compatible = "asus,tf201-ec-dock", "asus,tf101-ec-dock"; + reg = <0x19>, <0x1b>; + reg-names = "ec", "dockram"; + + interrupt-parent = <&gpio>; + interrupts = <151 IRQ_TYPE_LEVEL_LOW>; + + request-gpios = <&gpio 134 GPIO_ACTIVE_LOW>; + + monitored-battery = <&dock_battery>; + }; + }; +...
Signature · 1 line
2.51.0
PATCH v6 2/7 mfd: Add driver for ASUS Transformer embedded controller
In reply to Svyatoslav RyhelFrom: Michał Mirosław <[email protected]>
Support Nuvoton NPCE795-based ECs as used in Asus Transformer TF201, TF300T, TF300TG, TF300TL and TF700T pad and dock, as well as TF101 dock and TF600T, P1801-T and TF701T pad. This is a glue driver handling detection and common operations for EC's functions.
--- drivers/mfd/Kconfig | 14 + drivers/mfd/Makefile | 1 + drivers/mfd/asus-transformer-ec.c | 762 ++++++++++++++++++++++++ include/linux/mfd/asus-transformer-ec.h | 162 +++++ 4 files changed, 939 insertions(+) create mode 100644 drivers/mfd/asus-transformer-ec.c create mode 100644 include/linux/mfd/asus-transformer-ec.h
drivers/mfd/Kconfig +14 -0
@@ -137,6 +137,20 @@ config MFD_AAT2870_CORE additional drivers must be enabled in order to use the functionality of the device. +config MFD_ASUS_TRANSFORMER_EC + tristate "ASUS Transformer's embedded controller" + depends on I2C && OF + help + Support ECs found in ASUS Transformer's Pad and Mobile Dock. + + This provides shared glue for functional part drivers: + asus-transformer-ec-kbc, asus-transformer-ec-keys, + leds-asus-transformer-ec, asus-transformer-ec-battery + and asus-transformer-ec-charger. + + This driver can also be built as a module. If so, the module + will be called asus-transformer-ec. + config MFD_AT91_USART tristate "AT91 USART Driver" select MFD_CORE
drivers/mfd/Makefile +1 -0
@@ -10,6 +10,7 @@ obj-$(CONFIG_MFD_88PM805) += 88pm805.o 88pm80x.o obj-$(CONFIG_MFD_88PM886_PMIC) += 88pm886.o obj-$(CONFIG_MFD_ACT8945A) += act8945a.o obj-$(CONFIG_MFD_SM501) += sm501.o +obj-$(CONFIG_MFD_ASUS_TRANSFORMER_EC) += asus-transformer-ec.o obj-$(CONFIG_ARCH_BCM2835) += bcm2835-pm.o obj-$(CONFIG_MFD_BCM590XX) += bcm590xx.o obj-$(CONFIG_MFD_BD9571MWV) += bd9571mwv.o
drivers/mfd/asus-transformer-ec.c new file +762 -0
762 lines not on this page · raw message
include/linux/mfd/asus-transformer-ec.h new file +162 -0
162 lines not on this page · raw message
Signature · 1 line
2.51.0
Lee Jones
maintainerRe: [PATCH v6 2/7] mfd: Add driver for ASUS Transformer embedded controller
Quoted 27 lines · Svyatoslav Ryhel
From: Michał Mirosław <[email protected]>
Support Nuvoton NPCE795-based ECs as used in Asus Transformer TF201, TF300T, TF300TG, TF300TL and TF700T pad and dock, as well as TF101 dock and TF600T, P1801-T and TF701T pad. This is a glue driver handling detection and common operations for EC's functions.
Co-developed-by Svyatoslav Ryhel <[email protected]>Signed-off-by Svyatoslav Ryhel <[email protected]>Signed-off-by Michał Mirosław <[email protected]>--- drivers/mfd/Kconfig | 14 + drivers/mfd/Makefile | 1 + drivers/mfd/asus-transformer-ec.c | 762 ++++++++++++++++++++++++ include/linux/mfd/asus-transformer-ec.h | 162 +++++ 4 files changed, 939 insertions(+) create mode 100644 drivers/mfd/asus-transformer-ec.c create mode 100644 include/linux/mfd/asus-transformer-ec.hdrivers/mfd/Kconfig +0
-0@@ -137,6 +137,20 @@ config MFD_AAT2870_CORE additional drivers must be enabled in order to use the functionality of the device.
drivers/mfd/Kconfig config MFD_AAT2870_CORE · line 149
Quoted 10 lines
drivers/mfd/Kconfig +10
-0+config MFD_ASUS_TRANSFORMER_EC + tristate "ASUS Transformer's embedded controller" + depends on I2C && OF + help + Support ECs found in ASUS Transformer's Pad and Mobile Dock. + + This provides shared glue for functional part drivers: + asus-transformer-ec-kbc, asus-transformer-ec-keys, + leds-asus-transformer-ec, asus-transformer-ec-battery + and asus-transformer-ec-charger.
A 760 line driver deserves more than just a list of leaf drivers.
Quoted 24 lines
drivers/mfd/Kconfig +3
-0+ This driver can also be built as a module. If so, the module + will be called asus-transformer-ec. + config MFD_AT91_USART tristate "AT91 USART Driver" select MFD_COREdrivers/mfd/Makefile +1
-0@@ -10,6 +10,7 @@ obj-$(CONFIG_MFD_88PM805) += 88pm805.o 88pm80x.o obj-$(CONFIG_MFD_88PM886_PMIC) += 88pm886.o obj-$(CONFIG_MFD_ACT8945A) += act8945a.o obj-$(CONFIG_MFD_SM501) += sm501.o +obj-$(CONFIG_MFD_ASUS_TRANSFORMER_EC) += asus-transformer-ec.o obj-$(CONFIG_ARCH_BCM2835) += bcm2835-pm.o obj-$(CONFIG_MFD_BCM590XX) += bcm590xx.o obj-$(CONFIG_MFD_BD9571MWV) += bd9571mwv.odrivers/mfd/asus-transformer-ec.c new file +0
-0
drivers/mfd/asus-transformer-ec.c line 7
Quoted 7 lines
drivers/mfd/asus-transformer-ec.c new file +7
-0@@ -0,0 +1,762 @@ +// SPDX-License-Identifier: GPL-2.0-or-later + +#include <linux/array_size.h> +#include <linux/debugfs.h> +#include <linux/delay.h> +#include <linux/gpio/consumer.h> +#include <linux/device.h>
Should we sort the '#include' directives alphabetically? For instance, 'device.h' should typically appear before 'gpio/consumer.h'.
Quoted 9 lines
drivers/mfd/asus-transformer-ec.c new file +9
-0+#include <linux/err.h> +#include <linux/i2c.h> +#include <linux/interrupt.h> +#include <linux/mfd/asus-transformer-ec.h> +#include <linux/mfd/core.h> +#include <linux/mod_devicetable.h> +#include <linux/module.h> +#include <linux/mutex.h> +#include <linux/property.h>
drivers/mfd/asus-transformer-ec.c line 26
Quoted 10 lines
drivers/mfd/asus-transformer-ec.c new file +10
-0+#include <linux/slab.h> +#include <linux/string.h> +#include <linux/types.h> +#include <linux/unaligned.h> + +#define ASUSEC_RSP_BUFFER_SIZE 8 + +struct asus_ec_chip_data { +const char *name; +const struct mfd_cell *mfd_devices;
Use global static const structs instead.
Also, please do not pass MFD registration data through another registration API like DT. It opens the gates to too much hackery. I'm not saying that _this_ driver would do such a thing, but it's easier just to keep the blanket rule in place.
drivers/mfd/asus-transformer-ec.c line 32
Quoted 6 lines
drivers/mfd/asus-transformer-ec.c new file +6
-0+unsigned int num_devices; +bool clr_fmode;/* clear Factory Mode bit in EC control register */ +}; + +struct asus_ec_data { +struct asusec_info info;
You have 'data' and 'info' which a) using non-forthcoming nomenclature and doesn't tell me anything and then you b) put 'info' in the device's driver_data attribute which is very confusing. driver_data should be for what we call ddata which I assume is expressed as 'data' here.
drivers/mfd/asus-transformer-ec.c line 34
Quoted 2 lines
drivers/mfd/asus-transformer-ec.c new file +2
-0+struct mutex ecreq_lock;/* prevent simultaneous access */ +struct gpio_desc *ecreq;
If I hadn't seen the declaration, I'd have no idea this was a GPIO descriptor. Please improve the nomenclature throughout.
drivers/mfd/asus-transformer-ec.c line 35
Quoted 1 line
drivers/mfd/asus-transformer-ec.c new file +1
-0+struct i2c_client *self;
Again, please use standard naming conventions:
% git grep "struct i2c_client" | grep "\*self" | wc -l 0
% git grep "struct i2c_client" | grep "\*client" | wc -l 6304
% git grep "struct i2c_client" | grep "\*i2c" | wc -l 903
drivers/mfd/asus-transformer-ec.c line 36
Quoted 1 line
drivers/mfd/asus-transformer-ec.c new file +1
-0+const struct asus_ec_chip_data *data;
'data', 'priv' and 'info' should be improved.
drivers/mfd/asus-transformer-ec.c line 37
Quoted 1 line
drivers/mfd/asus-transformer-ec.c new file +1
-0+char ec_data[DOCKRAM_ENTRY_BUFSIZE ];
An array of chars called 'data'. This could be anything.
drivers/mfd/asus-transformer-ec.c line 38
Quoted 1 line
drivers/mfd/asus-transformer-ec.c new file +1
-0+bool logging_disabled;
This debugging tool is probably never going to be used again.
Keep it local.
Quoted 15 lines
drivers/mfd/asus-transformer-ec.c new file +15
-0+}; + +struct dockram_ec_data { +struct mutex ctl_lock;/* prevent simultaneous access */ +char ctl_data[DOCKRAM_ENTRY_BUFSIZE ]; +}; + +#define to_ec_data (ec) \ + container_of(ec, struct asus_ec_data, info) + +/** +* asus_dockram_read - Read a register from the DockRAM device. +* @client: Handle to the DockRAM device. +* @reg: Register to read. +* @buf: Byte array into which data will be read; must be large enough to
drivers/mfd/asus-transformer-ec.c line 63
Quoted 10 lines
drivers/mfd/asus-transformer-ec.c new file +10
-0+* hold the data returned by the DockRAM. +* +* This executes the DockRAM read based on the SMBus "block read" protocol +* or its emulation. It extracts DOCKRAM_ENTRY_SIZE bytes from the set +* register address. +* +* Returns a negative errno code else zero on success. +*/ +int asus_dockram_read (struct i2c_client *client,int reg,char *buf) +{
Have you considered using Regmap for register access instead of implementing custom functions? Remaps already deals with caching and locking mechanisms that you'd get for free.
This looks like it would be replaced with devm_regmap_init_i2c().
Quoted 7 lines
drivers/mfd/asus-transformer-ec.c new file +7
-0+struct device *dev = &client->dev; +int ret; + +memset (buf,0 ,DOCKRAM_ENTRY_BUFSIZE ); + ret =i2c_smbus_read_i2c_block_data (client, reg, +DOCKRAM_ENTRY_BUFSIZE , buf); +if (ret <0 )
drivers/mfd/asus-transformer-ec.c line 80
Quoted 10 lines
drivers/mfd/asus-transformer-ec.c new file +10
-0+return ret; + +if (buf[0 ] >DOCKRAM_ENTRY_SIZE ) { +dev_err (dev,"bad data len; buffer: %*ph; ret: %d\n" , +DOCKRAM_ENTRY_BUFSIZE , buf, ret); +return -EPROTO ; + } + +dev_dbg (dev,"got data; buffer: %*ph; ret: %d\n" , +DOCKRAM_ENTRY_BUFSIZE , buf, ret);
Please remove all of these debug messages.
Quoted 43 lines
drivers/mfd/asus-transformer-ec.c new file +43
-0+ +return 0 ; +} +EXPORT_SYMBOL_GPL (asus_dockram_read); + +/** +* asus_dockram_write - Write a byte array to a register of the DockRAM device. +* @client: Handle to the DockRAM device. +* @reg: Register to write to. +* @buf: Byte array to be written (up to DOCKRAM_ENTRY_SIZE bytes). +* +* This executes the DockRAM write based on the SMBus "block write" +* protocol or its emulation. It writes DOCKRAM_ENTRY_SIZE bytes to the +* specified register address. +* +* Returns a negative errno code else zero on success. +*/ +int asus_dockram_write (struct i2c_client *client,int reg,const char *buf) +{ +if (buf[0 ] >DOCKRAM_ENTRY_SIZE ) +return -EINVAL ; + +dev_dbg (&client->dev,"sending data; buffer: %*ph\n" , buf[0 ] +1 , buf); + +return i2c_smbus_write_i2c_block_data (client, reg, buf[0 ] +1 , buf); +} +EXPORT_SYMBOL_GPL (asus_dockram_write); + +/** +* asus_dockram_access_ctl - Read from or write to the DockRAM control register. +* @client: Handle to the DockRAM device. +* @out: Pointer to a variable where the register value will be stored. +* @mask: Bitmask of bits to be cleared. +* @xor: Bitmask of bits to be set (via XOR). +* +* This performs a control register read if @out is provided and both @mask +* and @xor are zero. Otherwise, it performs a control register update if +* @mask and @xor are provided. +* +* Returns a negative errno code else zero on success. +*/ +int asus_dockram_access_ctl (struct i2c_client *client,u64 *out,u64 mask, +u64 xor)
drivers/mfd/asus-transformer-ec.c line 133
Quoted 10 lines
drivers/mfd/asus-transformer-ec.c new file +10
-0+{ +struct dockram_ec_data *priv =i2c_get_clientdata (client); +char *buf = priv->ctl_data; +u64 val; +int ret =0 ; + +guard (mutex)(&priv->ctl_lock); + + ret =asus_dockram_read (client,ASUSEC_DOCKRAM_CONTROL , buf); +if (ret <0 )
Could we check for errors using 'if (ret)' instead of 'if (ret < 0)' here, unless a positive return value has a specific meaning we need to handle?
Quoted 38 lines
drivers/mfd/asus-transformer-ec.c new file +38
-0+ goto exit; + +if (buf[0 ] !=ASUSEC_CTL_SIZE ) { + ret = -EPROTO ; + goto exit; + } + + val =get_unaligned_le64 (buf +1 ); + +if (out) + *out = val; + +if (mask || xor) { +put_unaligned_le64 ((val & ~mask) ^ xor, buf +1 ); + ret =asus_dockram_write (client,ASUSEC_DOCKRAM_CONTROL , buf); + } + +exit: +if (ret <0 ) +dev_err (&client->dev,"Failed to access control flags: %d\n" , + ret); + +return ret; +} +EXPORT_SYMBOL_GPL (asus_dockram_access_ctl); + +static void asus_ec_remove_notifier (struct device *dev,void *res) +{ +struct asusec_info *ec =dev_get_drvdata (dev->parent); +struct notifier_block **nb = res; + +blocking_notifier_chain_unregister (&ec->notify_list, *nb); +} + +/** +* devm_asus_ec_register_notifier - Managed registration of notifier to an +* ASUS EC blocking notifier chain. +* @pdev: Device requesting the notifier (used for resource management).
drivers/mfd/asus-transformer-ec.c line 181
Quoted 10 lines
drivers/mfd/asus-transformer-ec.c new file +10
-0+* @nb: Notifier block to be registered. +* +* Register a notifier to the ASUS EC blocking notifier chain. The notifier +* will be automatically unregistered when the requesting device is detached. +* +* Return: 0 on success or a negative error code on failure. +*/ +int devm_asus_ec_register_notifier (struct platform_device *pdev, +struct notifier_block *nb) +{
Hand-rolling devres managed resources is usually reserved for subsystem level API calls. Why do the usual device driver .remove() handling work for you?
Quoted 49 lines
drivers/mfd/asus-transformer-ec.c new file +49
-0+struct asusec_info *ec =dev_get_drvdata (pdev->dev.parent); +struct notifier_block **res; +int ret; + + res =devres_alloc (asus_ec_remove_notifier,sizeof (*res),GFP_KERNEL ); +if (!res) +return -ENOMEM ; + + *res = nb; + ret =blocking_notifier_chain_register (&ec->notify_list, nb); +if (ret) { +devres_free (res); +return ret; + } + +devres_add (&pdev->dev, res); + +return 0 ; +} +EXPORT_SYMBOL_GPL (devm_asus_ec_register_notifier); + +static int asus_ec_signal_request (const struct asusec_info *ec) +{ +struct asus_ec_data *priv =to_ec_data (ec); + +guard (mutex)(&priv->ecreq_lock); + +dev_dbg (&priv->self->dev,"EC request\n" ); + +gpiod_set_value_cansleep (priv->ecreq,1 ); +msleep (50 ); + +gpiod_set_value_cansleep (priv->ecreq,0 ); +msleep (200 ); + +return 0 ; +} + +static int asus_ec_write (struct asus_ec_data *priv,u16 data) +{ +int ret =i2c_smbus_write_word_data (priv->self,ASUSEC_WRITE_BUF , data); + +dev_dbg (&priv->self->dev,"EC write: %04x, ret = %d\n" , data, ret); + +return ret; +} + +static int asus_ec_read (struct asus_ec_data *priv,bool in_irq) +{
drivers/mfd/asus-transformer-ec.c line 240
Quoted 10 lines
drivers/mfd/asus-transformer-ec.c new file +10
-0+int ret =i2c_smbus_read_i2c_block_data (priv->self,ASUSEC_READ_BUF , +sizeof (priv->ec_data), + priv->ec_data); + +dev_dbg (&priv->self->dev,"EC read: %*ph, ret = %d%s\n" , +sizeof (priv->ec_data), priv->ec_data, + ret, in_irq ?"; in irq" :"" ); + +return ret; +}
No abstractions for the sake of it. All this goes away with Regmap anyway.
drivers/mfd/asus-transformer-ec.c line 252
Quoted 12 lines
drivers/mfd/asus-transformer-ec.c new file +12
-0+ +/** +* asus_ec_i2c_command - Send a 16-bit command to the ASUS EC. +* @ec: Pointer to the shared ASUS EC structure. +* @data: The 16-bit command (word) to be sent. +* +* Return: 0 on success or a negative error code on failure. +*/ +int asus_ec_i2c_command (const struct asusec_info *ec,u16 data) +{ +return asus_ec_write (to_ec_data (ec), data); +}
Is this wrapper function strictly necessary? We should try to avoid superfluous abstractions that do nothing but call another function.
Quoted 250 lines
drivers/mfd/asus-transformer-ec.c new file +250
-0+EXPORT_SYMBOL_GPL (asus_ec_i2c_command); + +static void asus_ec_clear_buffer (struct asus_ec_data *priv) +{ +int retry =ASUSEC_RSP_BUFFER_SIZE ; + +while (retry--) { +if (asus_ec_read (priv, false) <0 ) +continue ; + +if (priv->ec_data[1 ] &ASUSEC_OBF_MASK ) +continue ; + +break ; + } +} + +static int asus_ec_log_info (struct asus_ec_data *priv,unsigned int reg, +const char *name,char **out) +{ +char buf[DOCKRAM_ENTRY_BUFSIZE ]; +int ret; + + ret =asus_dockram_read (priv->info.dockram, reg, buf); +if (ret <0 ) +return ret; + +if (!priv->logging_disabled) +dev_info (&priv->self->dev,"%-14s: %.*s\n" , name, + buf[0 ], buf +1 ); + +if (out) + *out =kstrndup (buf +1 , buf[0 ],GFP_KERNEL ); + +return 0 ; +} + +static int asus_ec_reset (struct asus_ec_data *priv) +{ +int retry, ret; + +for (retry =0 ; retry <3 ; retry++) { + ret =asus_ec_write (priv,0 ); +if (!ret) +return 0 ; + +msleep (300 ); + } + +return ret; +} + +static int asus_ec_magic_debug (struct asus_ec_data *priv) +{ +u64 flag; +int ret; + + ret =asus_ec_get_ctl (&priv->info, &flag); +if (ret <0 ) +return ret; + + flag &=ASUSEC_CTL_SUSB_MODE ; +dev_info (&priv->self->dev,"EC FW behaviour: %s\n" , + flag ?"susb on when receive ec_req" : +"susb on when system wakeup" ); + +return 0 ; +} + +static int asus_ec_set_factory_mode (struct asus_ec_data *priv,bool on) +{ +dev_info (&priv->self->dev,"Entering %s mode.\n" , on ?"factory" : +"normal" ); + +return asus_ec_update_ctl (&priv->info,ASUSEC_CTL_FACTORY_MODE , + on ?ASUSEC_CTL_FACTORY_MODE :0 ); +} + +static int asus_ec_detect (struct asus_ec_data *priv) +{ +char *model =NULL ; +int ret; + + ret =asus_ec_reset (priv); +if (ret) + goto err_exit; + +asus_ec_clear_buffer (priv); + + ret =asus_ec_log_info (priv,ASUSEC_DOCKRAM_INFO_MODEL ,"model" , &model); +if (ret) + goto err_exit; + + ret =asus_ec_log_info (priv,ASUSEC_DOCKRAM_INFO_FW ,"FW version" ,NULL ); +if (ret) + goto err_exit; + + ret =asus_ec_log_info (priv,ASUSEC_DOCKRAM_INFO_CFGFMT ,"Config format" ,NULL ); +if (ret) + goto err_exit; + + ret =asus_ec_log_info (priv,ASUSEC_DOCKRAM_INFO_HW ,"HW version" ,NULL ); +if (ret) + goto err_exit; + + priv->logging_disabled = true; + + ret =asus_ec_magic_debug (priv); +if (ret) + goto err_exit; + + priv->info.model = model; + priv->info.name = priv->data->name; + +if (priv->data->clr_fmode) +asus_ec_set_factory_mode (priv, false); + +err_exit: +if (ret) +dev_err (&priv->self->dev,"failed to access EC: %d\n" , ret); + +return ret; +} + +static void asus_ec_handle_smi (struct asus_ec_data *priv,unsigned int code) +{ +dev_dbg (&priv->self->dev,"SMI interrupt: 0x%02x\n" , code); + +switch (code) { +case ASUSEC_SMI_HANDSHAKE : +case ASUSEC_SMI_RESET : +asus_ec_detect (priv); +break ; + } +} + +static irqreturn_t asus_ec_interrupt (int irq,void *dev_id) +{ +struct asus_ec_data *priv = dev_id; +unsigned long notify_action; +int ret; + + ret =asus_ec_read (priv, true); +if (ret <=0 || !(priv->ec_data[1 ] &ASUSEC_OBF_MASK )) +return IRQ_NONE ; + + notify_action = priv->ec_data[1 ]; +if (notify_action &ASUSEC_SMI_MASK ) { +unsigned int code = priv->ec_data[2 ]; + +asus_ec_handle_smi (priv, code); + + notify_action |= code <<8 ; +dev_dbg (&priv->self->dev,"SMI code: 0x%02x\n" , code); + } + +blocking_notifier_call_chain (&priv->info.notify_list, + notify_action, priv->ec_data); + +return IRQ_HANDLED ; +} + +static ssize_t dockram_read (struct file *filp,char __user *buf, +size_t count,loff_t *ppos) +{ +struct i2c_client *client = filp->private_data; +unsigned int reg, rsize; +ssize_t n_read =0 , val; +loff_t off = *ppos; +char *data; +int ret; + + reg = off /DOCKRAM_ENTRY_SIZE ; + off %=DOCKRAM_ENTRY_SIZE ; + rsize =DOCKRAM_ENTRIES *DOCKRAM_ENTRY_SIZE ; + +if (!count) +return 0 ; + + data =kmalloc (DOCKRAM_ENTRY_BUFSIZE ,GFP_KERNEL ); + +while (reg <DOCKRAM_ENTRIES ) { +unsigned int len =DOCKRAM_ENTRY_SIZE - off; + +if (len > rsize) + len = rsize; + + ret =asus_dockram_read (client, reg, data); +if (ret <0 ) { +if (!n_read) + n_read = ret; +break ; + } + + val =copy_to_user (buf, data +1 + off, len); +if (val == len) +return -EFAULT ; + + *ppos += len; + n_read += len; + +if (len == rsize) +break ; + + rsize -= len; + buf += len; + off =0 ; + ++reg; + } + +kfree (data); + +return n_read; +} + +static int dockram_write_one (struct i2c_client *client,int reg, +const char __user *buf,size_t count) +{ +struct dockram_ec_data *priv =i2c_get_clientdata (client); +int ret; + +if (!count || count >DOCKRAM_ENTRY_SIZE ) +return -EINVAL ; +if (buf[0 ] != count -1 ) +return -EINVAL ; + +guard (mutex)(&priv->ctl_lock); + + priv->ctl_data[0 ] = (u8 )count; +memcpy (priv->ctl_data +1 , buf, count); + ret =asus_dockram_write (client, reg, priv->ctl_data); + +return ret; +} + +static ssize_t dockram_write (struct file *filp,const char __user *buf, +size_t count,loff_t *ppos) +{ +struct i2c_client *client = filp->private_data; +unsigned int reg; +loff_t off = *ppos; +int ret; + +if (off %DOCKRAM_ENTRY_SIZE !=0 ) +return -EINVAL ; + + reg = off /DOCKRAM_ENTRY_SIZE ; +if (reg >=DOCKRAM_ENTRIES ) +return -EINVAL ; +
drivers/mfd/asus-transformer-ec.c line 512
Quoted 10 lines
drivers/mfd/asus-transformer-ec.c new file +10
-0+ ret =dockram_write_one (client, reg, buf, count); + +return ret <0 ? ret : count; +} + +static const struct debugfs_short_fops dockram_fops = { + .read = dockram_read, + .write = dockram_write, + .llseek = default_llseek, +};
You should not be giving userspace free reign over device memory.
If you switch to Regmap, you can use regmap-debugfs.c for this.
Quoted 52 lines
drivers/mfd/asus-transformer-ec.c new file +52
-0+static int control_reg_get (void *ec,u64 *val) +{ +return asus_ec_get_ctl (ec, val); +} + +static int control_reg_set (void *ec,u64 val) +{ +return asus_ec_update_ctl (ec, ~0ull , val); +} + +DEFINE_DEBUGFS_ATTRIBUTE (control_reg_fops, control_reg_get, + control_reg_set,"%016llx\n" ); + +static int ec_request_set (void *ec,u64 val) +{ +if (val) +asus_ec_signal_request (ec); + +return 0 ; +} + +DEFINE_DEBUGFS_ATTRIBUTE (ec_request_fops,NULL , ec_request_set,"%llu\n" ); + +static int ec_irq_set (void *ec,u64 val) +{ +struct asus_ec_data *priv =to_ec_data (ec); + +if (val) +irq_wake_thread (priv->self->irq, priv); + +return 0 ; +} + +DEFINE_DEBUGFS_ATTRIBUTE (ec_irq_fops,NULL , ec_irq_set,"%llu\n" ); + +static void asus_ec_debugfs_remove (void *debugfs_root) +{ +debugfs_remove_recursive (debugfs_root); +} + +static void devm_asus_ec_debugfs_init (struct device *dev) +{ +struct asusec_info *ec =dev_get_drvdata (dev); +struct asus_ec_data *priv =to_ec_data (ec); +struct dentry *debugfs_root, *dockram_dir; +char *name =devm_kasprintf (dev,GFP_KERNEL ,"asus-ec-%s" , + priv->data->name); + + debugfs_root =debugfs_create_dir (name,NULL ); + dockram_dir =debugfs_create_dir ("dockram" , debugfs_root); + + debugfs_create_file("ec_irq", 0200, debugfs_root, ec,
drivers/mfd/asus-transformer-ec.c line 575
Quoted 10 lines
drivers/mfd/asus-transformer-ec.c new file +10
-0+ &ec_irq_fops); +debugfs_create_file ("ec_request" ,0200 , debugfs_root, ec, + &ec_request_fops); +debugfs_create_file ("control_reg" ,0644 , dockram_dir, ec, + &control_reg_fops); +debugfs_create_file ("dockram" ,0644 , dockram_dir, + priv->info.dockram, &dockram_fops); + +devm_add_action_or_reset (dev, asus_ec_debugfs_remove, debugfs_root); +}
Why is this being controlled via debugfs?
Use the correct kernel APIs instead.
If this is just for development, keep it and all of the extra verbose printing in the BSP tree. It does not belong in the upstream kernel.
Quoted 26 lines
drivers/mfd/asus-transformer-ec.c new file +26
-0+static void asus_ec_release_dockram_dev (void *client) +{ +i2c_unregister_device (client); +} + +static struct i2c_client *devm_asus_dockram_get (struct device *dev) +{ +struct i2c_client *parent =to_i2c_client (dev); +struct i2c_client *dockram; +struct dockram_ec_data *priv; +int ret; + + dockram =i2c_new_ancillary_device (parent,"dockram" , + parent->addr +2 ); +if (IS_ERR (dockram)) +return dockram; + + ret =devm_add_action_or_reset (dev, asus_ec_release_dockram_dev, + dockram); +if (ret) +return ERR_PTR (ret); + + priv =devm_kzalloc (&dockram->dev,sizeof (*priv),GFP_KERNEL ); +if (!priv) +return ERR_PTR (-ENOMEM ); +
drivers/mfd/asus-transformer-ec.c line 612
Quoted 10 lines
drivers/mfd/asus-transformer-ec.c new file +10
-0+i2c_set_clientdata (dockram, priv); +mutex_init (&priv->ctl_lock); + +return dockram; +} + +static int asus_ec_probe (struct i2c_client *client) +{ +struct device *dev = &client->dev; +struct asus_ec_data *priv;
Could we use a clearer, more specific name for the private data variable instead of the generic term 'priv'? Using something like 'ddata' is usually preferred.
Quoted 16 lines
drivers/mfd/asus-transformer-ec.c new file +16
-0+int ret; + +if (!i2c_check_functionality (client->adapter,I2C_FUNC_SMBUS_I2C_BLOCK )) +return dev_err_probe (dev, -ENXIO , +"I2C bus is missing required SMBus block mode support\n" ); + + priv =devm_kzalloc (dev,sizeof (*priv),GFP_KERNEL ); +if (!priv) +return -ENOMEM ; + + priv->data =device_get_match_data (dev); +if (!priv->data) +return -ENODEV ; + +i2c_set_clientdata (client, priv); + priv->self = client;
drivers/mfd/asus-transformer-ec.c line 638
Quoted 10 lines
drivers/mfd/asus-transformer-ec.c new file +10
-0+ + priv->info.dockram =devm_asus_dockram_get (dev); +if (IS_ERR (priv->info.dockram)) +return dev_err_probe (dev,PTR_ERR (priv->info.dockram), +"failed to get dockram\n" ); + + priv->ecreq =devm_gpiod_get (dev,"request" ,GPIOD_OUT_LOW ); +if (IS_ERR (priv->ecreq)) +return dev_err_probe (dev,PTR_ERR (priv->ecreq), +"failed to get request GPIO\n" );
"get" or "request"
Quoted 34 lines
drivers/mfd/asus-transformer-ec.c new file +34
-0+BLOCKING_INIT_NOTIFIER_HEAD (&priv->info.notify_list); +mutex_init (&priv->ecreq_lock); + +asus_ec_signal_request (&priv->info); + + ret =asus_ec_detect (priv); +if (ret) +return dev_err_probe (dev, ret,"failed to detect EC version\n" ); + + ret =devm_request_threaded_irq (dev, client->irq,NULL , + &asus_ec_interrupt, +IRQF_ONESHOT |IRQF_SHARED , + client->name, priv); +if (ret) +return dev_err_probe (dev, ret,"failed to register IRQ\n" ); + +/* Parent I2C controller uses DMA, ASUS EC and child devices do not */ + client->dev.coherent_dma_mask =0 ; + client->dev.dma_mask = &client->dev.coherent_dma_mask; + +if (IS_ENABLED (CONFIG_DEBUG_FS )) +devm_asus_ec_debugfs_init (dev); + +return devm_mfd_add_devices (dev,0 , priv->data->mfd_devices, + priv->data->num_devices,NULL ,0 ,NULL ); +} + +static const struct mfd_cell asus_ec_sl101_dock_mfd_devices[] = { + { + .name ="asus-transformer-ec-kbc" , + }, +}; + +static const struct asus_ec_chip_data asus_ec_sl101_dock_data = {
drivers/mfd/asus-transformer-ec.c line 683
Quoted 10 lines
drivers/mfd/asus-transformer-ec.c new file +10
-0+ .name = "dock", + .mfd_devices = asus_ec_sl101_dock_mfd_devices, + .num_devices = ARRAY_SIZE(asus_ec_sl101_dock_mfd_devices), + .clr_fmode = false, +}; + +static const struct mfd_cell asus_ec_tf101_dock_mfd_devices[] = { + { + .name ="asus-transformer-ec-battery" , + .id =1 ,
Why doesn't PLATFORM_DEVID_AUTO already do the right thing?
Quoted 50 lines
drivers/mfd/asus-transformer-ec.c new file +50
-0+ }, { + .name = "asus-transformer-ec-charger", + .id = 1, + }, { + .name = "asus-transformer-ec-led", + .id = 1, + }, { + .name = "asus-transformer-ec-keys", + }, { + .name = "asus-transformer-ec-kbc", + }, +}; + +static const struct asus_ec_chip_data asus_ec_tf101_dock_data = { + .name = "dock", + .mfd_devices = asus_ec_tf101_dock_mfd_devices, + .num_devices = ARRAY_SIZE(asus_ec_tf101_dock_mfd_devices), + .clr_fmode = false, +}; + +static const struct mfd_cell asus_ec_tf201_pad_mfd_devices[] = { + { + .name = "asus-transformer-ec-battery", + .id = 0, + }, { + .name = "asus-transformer-ec-led", + .id = 0, + }, +}; + +static const struct asus_ec_chip_data asus_ec_tf201_pad_data = { + .name = "pad", + .mfd_devices = asus_ec_tf201_pad_mfd_devices, + .num_devices = ARRAY_SIZE(asus_ec_tf201_pad_mfd_devices), + .clr_fmode = true, +}; + +static const struct mfd_cell asus_ec_tf600t_pad_mfd_devices[] = { + { + .name = "asus-transformer-ec-battery", + .id = 0, + }, { + .name = "asus-transformer-ec-charger", + .id = 0, + }, { + .name = "asus-transformer-ec-led", + .id = 0, + }, +}; +
drivers/mfd/asus-transformer-ec.c line 743
Quoted 10 lines
drivers/mfd/asus-transformer-ec.c new file +10
-0+static const struct asus_ec_chip_data asus_ec_tf600t_pad_data = { + .name ="pad" , + .mfd_devices = asus_ec_tf600t_pad_mfd_devices, + .num_devices =ARRAY_SIZE (asus_ec_tf600t_pad_mfd_devices), + .clr_fmode = true, +}; + +static const struct of_device_id asus_ec_match[] = { + { .compatible ="asus,sl101-ec-dock" , .data = &asus_ec_sl101_dock_data }, + { .compatible ="asus,tf101-ec-dock" , .data = &asus_ec_tf101_dock_data },
Could we use the match table's data field to store an 'enum' or integer ID instead of passing platform data via the '.data' field? We could then use a 'switch' statement in the C code to select the correct 'mfd_cell' array.
Quoted 113 lines
drivers/mfd/asus-transformer-ec.c new file +19
-0+ { .compatible = "asus,tf201-ec-pad", .data = &asus_ec_tf201_pad_data }, + { .compatible = "asus,tf600t-ec-pad", .data = &asus_ec_tf600t_pad_data }, + { } +}; +MODULE_DEVICE_TABLE (of, asus_ec_match); + +static struct i2c_driver asus_ec_driver = { + .driver = { + .name ="asus-transformer-ec" , + .of_match_table = asus_ec_match, + }, + .probe = asus_ec_probe, +}; +module_i2c_driver (asus_ec_driver); + +MODULE_AUTHOR ("Michał Mirosław <[email protected]>" ); +MODULE_AUTHOR ("Svyatoslav Ryhel <[email protected]>" ); +MODULE_DESCRIPTION ("ASUS Transformer's EC driver" ); +MODULE_LICENSE ("GPL" );include/linux/mfd/asus-transformer-ec.h new file +88
-0@@ -0,0 +1,162 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef __MFD_ASUS_TRANSFORMER_EC_H +#define __MFD_ASUS_TRANSFORMER_EC_H + +#include <linux/notifier.h> +#include <linux/platform_device.h> +#include <linux/workqueue.h> + +struct i2c_client ; + +struct asusec_info { +const char *model; +const char *name; +struct i2c_client *dockram; +struct workqueue_struct *wq; +struct blocking_notifier_head notify_list; +}; + +#define DOCKRAM_ENTRIES 0x100 +#define DOCKRAM_ENTRY_SIZE 32 +#define DOCKRAM_ENTRY_BUFSIZE (DOCKRAM_ENTRY_SIZE + 1) + +/* interrupt sources */ +#define ASUSEC_OBF_MASK BIT(0) +#define ASUSEC_KEY_MASK BIT(2) +#define ASUSEC_KBC_MASK BIT(3) +#define ASUSEC_AUX_MASK BIT(5) +#define ASUSEC_SCI_MASK BIT(6) +#define ASUSEC_SMI_MASK BIT(7) + +/* SMI notification codes */ +#define ASUSEC_SMI_POWER_NOTIFY 0x31/* [un]plugging USB cable */ +#define ASUSEC_SMI_HANDSHAKE 0x50/* response to ec_req edge */ +#define ASUSEC_SMI_WAKE 0x53 +#define ASUSEC_SMI_RESET 0x5f +#define ASUSEC_SMI_ADAPTER_EVENT 0x60/* [un]plugging charger to dock */ +#define ASUSEC_SMI_BACKLIGHT_ON 0x63 +#define ASUSEC_SMI_AUDIO_DOCK_IN 0x70 + +#define ASUSEC_SMI_ACTION (code) (ASUSEC_SMI_MASK | ASUSEC_OBF_MASK | \ + (ASUSEC_SMI_##code << 8)) + +/* control register [0x0a] layout */ +#define ASUSEC_CTL_SIZE 8 + +/* +* EC reports power from 40-pin connector in the LSB of the control +* register. The following values have been observed (xor 0x02): +* +* PAD-ec no-plug 0x40 / PAD-ec DOCK 0x20 / DOCK-ec no-plug 0x40 +* PAD-ec AC 0x25 / PAD-ec DOCK+AC 0x24 / DOCK-ec AC 0x25 +* PAD-ec USB 0x45 / PAD-ec DOCK+USB 0x24 / DOCK-ec USB 0x41 +*/ + +#define ASUSEC_CTL_DIRECT_POWER_SOURCE BIT_ULL(0) +#define ASUSEC_STAT_CHARGING BIT_ULL(2) +#define ASUSEC_CTL_FULL_POWER_SOURCE BIT_ULL(5) +#define ASUSEC_CTL_SUSB_MODE BIT_ULL(9) +#define ASUSEC_CMD_SUSPEND_S3 BIT_ULL(33) +#define ASUSEC_CTL_TEST_DISCHARGE BIT_ULL(35) +#define ASUSEC_CMD_SUSPEND_INHIBIT BIT_ULL(37) +#define ASUSEC_CTL_FACTORY_MODE BIT_ULL(38) +#define ASUSEC_CTL_KEEP_AWAKE BIT_ULL(39) +#define ASUSEC_CTL_USB_CHARGE BIT_ULL(40) +#define ASUSEC_CTL_LED_BLINK BIT_ULL(40) +#define ASUSEC_CTL_LED_AMBER BIT_ULL(41) +#define ASUSEC_CTL_LED_GREEN BIT_ULL(42) +#define ASUSEC_CMD_SWITCH_HDMI BIT_ULL(56) +#define ASUSEC_CMD_WIN_SHUTDOWN BIT_ULL(62) + +#define ASUSEC_DOCKRAM_INFO_MODEL 0x01 +#define ASUSEC_DOCKRAM_INFO_FW 0x02 +#define ASUSEC_DOCKRAM_INFO_CFGFMT 0x03 +#define ASUSEC_DOCKRAM_INFO_HW 0x04 +#define ASUSEC_DOCKRAM_CONTROL 0x0a +#define ASUSEC_DOCKRAM_BATT_CTL 0x14 + +#define ASUSEC_WRITE_BUF 0x64 +#define ASUSEC_READ_BUF 0x6a + +/* dockram comm */ +int asus_dockram_read (struct i2c_client *client,int reg,char *buf); +int asus_dockram_write (struct i2c_client *client,int reg,const char *buf); +int asus_dockram_access_ctl (struct i2c_client *client, +u64 *out,u64 mask,u64 xor); + +/* EC public API */ +
include/linux/mfd/asus-transformer-ec.h line 98
Quoted 10 lines
include/linux/mfd/asus-transformer-ec.h new file +10
-0+/** +* cell_to_ec - Request the shared ASUS EC structure via a subdevice's pdev. +* @pdev: EC subdevice pdev requesting access to the shared ASUS EC structure. +* +* Returns a pointer to the asusec_info structure. +*/ +static inline struct asusec_info *cell_to_ec (struct platform_device *pdev) +{ +return dev_get_drvdata (pdev->dev.parent); +}
This is both misleading and already exists.
Quoted 49 lines
include/linux/mfd/asus-transformer-ec.h new file +49
-0+ +/** +* asus_ec_get_ctl - Read from the DockRAM control register. +* @ec: Pointer to the shared ASUS EC structure. +* @out: Pointer to the variable where the register value will be stored. +* +* Performs a control register read and stores the value in @out. +* +* Return: 0 on success, or a negative errno code on failure. +*/ +static inline int asus_ec_get_ctl (const struct asusec_info *ec,u64 *out) +{ +return asus_dockram_access_ctl (ec->dockram, out,0 ,0 ); +} + +/** +* asus_ec_update_ctl - Update the DockRAM control register. +* @ec: Pointer to the shared ASUS EC structure. +* @mask: Bitmask of bits to be cleared. +* @xor: Bitmask of bits to be toggled or set (via XOR). +* +* Performs a read-modify-write update on the control register using +* the provided @mask and @xor values. +* +* Return: 0 on success, or a negative errno code on failure. +*/ +static inline int asus_ec_update_ctl (const struct asusec_info *ec, +u64 mask,u64 xor) +{ +return asus_dockram_access_ctl (ec->dockram,NULL , mask, xor); +} + +/** +* asus_ec_set_ctl_bits - Sets bits of the DockRAM control register. +* @ec: Pointer to the shared ASUS EC structure. +* @mask: Bitmask of bits to be set. +* +* Sets bits of the control register using the provided @mask value. +* +* Return: 0 on success, or a negative errno code on failure. +*/ +static inline int asus_ec_set_ctl_bits (const struct asusec_info *ec,u64 mask) +{ +return asus_dockram_access_ctl (ec->dockram,NULL , mask, mask); +} + +/** +* asus_ec_clear_ctl_bits - Clears bits of the DockRAM control register. +* @ec: Pointer to the shared ASUS EC structure.
include/linux/mfd/asus-transformer-ec.h line 157
Quoted 10 lines
include/linux/mfd/asus-transformer-ec.h new file +10
-0+* @mask: Bitmask of bits to be cleared. +* +* Clears bits of the control register using the provided @mask value. +* +* Return: 0 on success, or a negative errno code on failure. +*/ +static inline int asus_ec_clear_ctl_bits (const struct asusec_info *ec,u64 mask) +{ +return asus_dockram_access_ctl (ec->dockram,NULL , mask,0 ); +}
We don't typically allow abstraction for the sake of abstraction.
Quoted 8 lines
include/linux/mfd/asus-transformer-ec.h new file +4
-0+int asus_ec_i2c_command (const struct asusec_info *ec,u16 data); +int devm_asus_ec_register_notifier (struct platform_device *dev, +struct notifier_block *nb); +#endif /* __MFD_ASUS_TRANSFORMER_EC_H */ Signature · 1 line
2.51.0
Signature · 1 line
Lee Jones
Re: [PATCH v6 2/7] mfd: Add driver for ASUS Transformer embedded controller
чт, 14 трав. 2026 р. о 13:02 Lee Jones <[email protected]> пише:
Quoted 30 lines · Svyatoslav Ryhel
Quoted 28 lines
From: Michał Mirosław <[email protected]>
Support Nuvoton NPCE795-based ECs as used in Asus Transformer TF201, TF300T, TF300TG, TF300TL and TF700T pad and dock, as well as TF101 dock and TF600T, P1801-T and TF701T pad. This is a glue driver handling detection and common operations for EC's functions.
Co-developed-by Svyatoslav Ryhel <[email protected]>Signed-off-by Svyatoslav Ryhel <[email protected]>Signed-off-by Michał Mirosław <[email protected]>--- drivers/mfd/Kconfig | 14 + drivers/mfd/Makefile | 1 + drivers/mfd/asus-transformer-ec.c | 762 ++++++++++++++++++++++++ include/linux/mfd/asus-transformer-ec.h | 162 +++++ 4 files changed, 939 insertions(+) create mode 100644 drivers/mfd/asus-transformer-ec.c create mode 100644 include/linux/mfd/asus-transformer-ec.hdrivers/mfd/Kconfig +0
-0@@ -137,6 +137,20 @@ config MFD_AAT2870_CORE additional drivers must be enabled in order to use the functionality of the device.
Quoted 13 lines
drivers/mfd/Kconfig config MFD_AAT2870_CORE · line 149
Quoted 10 lines
drivers/mfd/Kconfig +10
-0+config MFD_ASUS_TRANSFORMER_EC + tristate "ASUS Transformer's embedded controller" + depends on I2C && OF + help + Support ECs found in ASUS Transformer's Pad and Mobile Dock. + + This provides shared glue for functional part drivers: + asus-transformer-ec-kbc, asus-transformer-ec-keys, + leds-asus-transformer-ec, asus-transformer-ec-battery + and asus-transformer-ec-charger.A 760 line driver deserves more than just a list of leaf drivers.
Ok, I will make some meaningful description.
Quoted 26 lines
Quoted 26 lines
drivers/mfd/Kconfig +3
-0+ This driver can also be built as a module. If so, the module + will be called asus-transformer-ec. + config MFD_AT91_USART tristate "AT91 USART Driver" select MFD_COREdrivers/mfd/Makefile +1
-0@@ -10,6 +10,7 @@ obj-$(CONFIG_MFD_88PM805) += 88pm805.o 88pm80x.o obj-$(CONFIG_MFD_88PM886_PMIC) += 88pm886.o obj-$(CONFIG_MFD_ACT8945A) += act8945a.o obj-$(CONFIG_MFD_SM501) += sm501.o +obj-$(CONFIG_MFD_ASUS_TRANSFORMER_EC) += asus-transformer-ec.o obj-$(CONFIG_ARCH_BCM2835) += bcm2835-pm.o obj-$(CONFIG_MFD_BCM590XX) += bcm590xx.o obj-$(CONFIG_MFD_BD9571MWV) += bd9571mwv.odrivers/mfd/asus-transformer-ec.c new file +2
-0@@ -0,0 +1,762 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +
Quoted 9 lines
drivers/mfd/asus-transformer-ec.c line 7
Quoted 5 lines
drivers/mfd/asus-transformer-ec.c new file +5
-0+#include <linux/array_size.h> +#include <linux/debugfs.h> +#include <linux/delay.h> +#include <linux/gpio/consumer.h> +#include <linux/device.h> Should we sort the '#include' directives alphabetically? For instance, '
device.h' should typically appear before 'gpio/consumer.h'.
Yes, it is my bad, I will fix it.
Quoted 22 lines
Quoted 9 lines
drivers/mfd/asus-transformer-ec.c new file +9
-0+#include <linux/err.h> +#include <linux/i2c.h> +#include <linux/interrupt.h> +#include <linux/mfd/asus-transformer-ec.h> +#include <linux/mfd/core.h> +#include <linux/mod_devicetable.h> +#include <linux/module.h> +#include <linux/mutex.h> +#include <linux/property.h> drivers/mfd/asus-transformer-ec.c line 26
Quoted 10 lines
drivers/mfd/asus-transformer-ec.c new file +10
-0+#include <linux/slab.h> +#include <linux/string.h> +#include <linux/types.h> +#include <linux/unaligned.h> + +#define ASUSEC_RSP_BUFFER_SIZE 8 + +struct asus_ec_chip_data { +const char *name; +const struct mfd_cell *mfd_devices;Use global
static const structsinstead.
Quoted 5 lines
Also, please do not pass MFD registration data through another registration API like DT. It opens the gates to too much hackery. I'm not saying that _this_ driver would do such a thing, but it's easier just to keep the blanket rule in place.
Yes, you have already made it clear in my CPCAP patchset. I have it applied for the v7 here.
Quoted 12 lines
drivers/mfd/asus-transformer-ec.c line 32
Quoted 6 lines
drivers/mfd/asus-transformer-ec.c new file +6
-0+unsigned int num_devices; +bool clr_fmode;/* clear Factory Mode bit in EC control register */ +}; + +struct asus_ec_data { +struct asusec_info info;You have 'data' and 'info' which a) using non-forthcoming nomenclature and doesn't tell me anything and then you b) put 'info' in the device's
driver_dataattribute which is very confusing.driver_datashould be for what we call ddata which I assume is expressed as 'data' here.
asusec_info is shared among all child devices and is exposed while remaining elements of this struct are for internal use only.
Quoted 10 lines
drivers/mfd/asus-transformer-ec.c line 34
Quoted 2 lines
drivers/mfd/asus-transformer-ec.c new file +2
-0+struct mutex ecreq_lock;/* prevent simultaneous access */ +struct gpio_desc *ecreq;If I hadn't seen the declaration, I'd have no idea this was a GPIO descriptor. Please improve the nomenclature throughout.
drivers/mfd/asus-transformer-ec.c line 35
Quoted 1 line
drivers/mfd/asus-transformer-ec.c new file +1
-0+struct i2c_client *self;Again, please use standard naming conventions:
Quoted 9 lines
% git grep "struct
i2c_client" | grep "\*self" | wc -l 0% git grep "struct
i2c_client" | grep "\*client" | wc -l 6304% git grep "struct
i2c_client" | grep "\*i2c" | wc -l 903
ok, noted.
Quoted 8 lines
drivers/mfd/asus-transformer-ec.c line 36
Quoted 1 line
drivers/mfd/asus-transformer-ec.c new file +1
-0+const struct asus_ec_chip_data *data;'data', 'priv' and 'info' should be improved.
drivers/mfd/asus-transformer-ec.c line 37
Quoted 1 line
drivers/mfd/asus-transformer-ec.c new file +1
-0+char ec_data[DOCKRAM_ENTRY_BUFSIZE ];An array of chars called 'data'. This could be anything.
Do you have a comprehensive list of name conventions you find suitable?
Quoted 36 lines
drivers/mfd/asus-transformer-ec.c line 38
Quoted 1 line
drivers/mfd/asus-transformer-ec.c new file +1
-0+bool logging_disabled;This debugging tool is probably never going to be used again.
Keep it local.
Quoted 15 lines
drivers/mfd/asus-transformer-ec.c new file +15
-0+}; + +struct dockram_ec_data { +struct mutex ctl_lock;/* prevent simultaneous access */ +char ctl_data[DOCKRAM_ENTRY_BUFSIZE ]; +}; + +#define to_ec_data (ec) \ + container_of(ec, struct asus_ec_data, info) + +/** +* asus_dockram_read - Read a register from the DockRAM device. +* @client: Handle to the DockRAM device. +* @reg: Register to read. +* @buf: Byte array into which data will be read; must be large enough to drivers/mfd/asus-transformer-ec.c line 63
Quoted 10 lines
drivers/mfd/asus-transformer-ec.c new file +10
-0+* hold the data returned by the DockRAM. +* +* This executes the DockRAM read based on the SMBus "block read" protocol +* or its emulation. It extracts DOCKRAM_ENTRY_SIZE bytes from the set +* register address. +* +* Returns a negative errno code else zero on success. +*/ +int asus_dockram_read (struct i2c_client *client,int reg,char *buf) +{Have you considered using Regmap for register access instead of implementing custom functions? Remaps already deals with caching and locking mechanisms that you'd get for free.
Quoted 2 lines
This looks like it would be replaced with
devm_regmap_init_i2c().
I will consider this, thank you.
Quoted 11 lines
Quoted 11 lines
drivers/mfd/asus-transformer-ec.c new file +11
-0+struct device *dev = &client->dev; +int ret; + +memset (buf,0 ,DOCKRAM_ENTRY_BUFSIZE ); + ret =i2c_smbus_read_i2c_block_data (client, reg, +DOCKRAM_ENTRY_BUFSIZE , buf); +if (ret <0 ) +return ret; + +if (buf[0 ] >DOCKRAM_ENTRY_SIZE ) { + dev_err(dev, "bad data len; buffer: %*ph; ret: %d\n",
Quoted 9 lines
drivers/mfd/asus-transformer-ec.c line 80
Quoted 6 lines
drivers/mfd/asus-transformer-ec.c new file +6
-0+ DOCKRAM_ENTRY_BUFSIZE, buf, ret); +return -EPROTO ; + } + +dev_dbg (dev,"got data; buffer: %*ph; ret: %d\n" , +DOCKRAM_ENTRY_BUFSIZE , buf, ret);Please remove all of these debug messages.
Why debug messages cannot be preserved? They are specifically marked as dev_dbg
Quoted 48 lines
Quoted 48 lines
drivers/mfd/asus-transformer-ec.c new file +48
-0+ +return 0 ; +} +EXPORT_SYMBOL_GPL (asus_dockram_read); + +/** +* asus_dockram_write - Write a byte array to a register of the DockRAM device. +* @client: Handle to the DockRAM device. +* @reg: Register to write to. +* @buf: Byte array to be written (up to DOCKRAM_ENTRY_SIZE bytes). +* +* This executes the DockRAM write based on the SMBus "block write" +* protocol or its emulation. It writes DOCKRAM_ENTRY_SIZE bytes to the +* specified register address. +* +* Returns a negative errno code else zero on success. +*/ +int asus_dockram_write (struct i2c_client *client,int reg,const char *buf) +{ +if (buf[0 ] >DOCKRAM_ENTRY_SIZE ) +return -EINVAL ; + +dev_dbg (&client->dev,"sending data; buffer: %*ph\n" , buf[0 ] +1 , buf); + +return i2c_smbus_write_i2c_block_data (client, reg, buf[0 ] +1 , buf); +} +EXPORT_SYMBOL_GPL (asus_dockram_write); + +/** +* asus_dockram_access_ctl - Read from or write to the DockRAM control register. +* @client: Handle to the DockRAM device. +* @out: Pointer to a variable where the register value will be stored. +* @mask: Bitmask of bits to be cleared. +* @xor: Bitmask of bits to be set (via XOR). +* +* This performs a control register read if @out is provided and both @mask +* and @xor are zero. Otherwise, it performs a control register update if +* @mask and @xor are provided. +* +* Returns a negative errno code else zero on success. +*/ +int asus_dockram_access_ctl (struct i2c_client *client,u64 *out,u64 mask, +u64 xor) +{ +struct dockram_ec_data *priv =i2c_get_clientdata (client); +char *buf = priv->ctl_data; +u64 val; +int ret =0 ;
Quoted 9 lines
drivers/mfd/asus-transformer-ec.c line 133
Quoted 5 lines
drivers/mfd/asus-transformer-ec.c new file +5
-0+ +guard (mutex)(&priv->ctl_lock); + + ret =asus_dockram_read (client,ASUSEC_DOCKRAM_CONTROL , buf); +if (ret <0 )Could we check for errors using 'if (ret)' instead of 'if (ret < 0)' here, unless a positive return value has a specific meaning we need to handle?
Sure, I will switch to 'if (ret)'
Quoted 44 lines
Quoted 44 lines
drivers/mfd/asus-transformer-ec.c new file +44
-0+ goto exit; + +if (buf[0 ] !=ASUSEC_CTL_SIZE ) { + ret = -EPROTO ; + goto exit; + } + + val =get_unaligned_le64 (buf +1 ); + +if (out) + *out = val; + +if (mask || xor) { +put_unaligned_le64 ((val & ~mask) ^ xor, buf +1 ); + ret =asus_dockram_write (client,ASUSEC_DOCKRAM_CONTROL , buf); + } + +exit: +if (ret <0 ) +dev_err (&client->dev,"Failed to access control flags: %d\n" , + ret); + +return ret; +} +EXPORT_SYMBOL_GPL (asus_dockram_access_ctl); + +static void asus_ec_remove_notifier (struct device *dev,void *res) +{ +struct asusec_info *ec =dev_get_drvdata (dev->parent); +struct notifier_block **nb = res; + +blocking_notifier_chain_unregister (&ec->notify_list, *nb); +} + +/** +* devm_asus_ec_register_notifier - Managed registration of notifier to an +* ASUS EC blocking notifier chain. +* @pdev: Device requesting the notifier (used for resource management). +* @nb: Notifier block to be registered. +* +* Register a notifier to the ASUS EC blocking notifier chain. The notifier +* will be automatically unregistered when the requesting device is detached. +* +* Return: 0 on success or a negative error code on failure.
Quoted 9 lines
drivers/mfd/asus-transformer-ec.c line 181
Quoted 4 lines
drivers/mfd/asus-transformer-ec.c new file +4
-0+*/ +int devm_asus_ec_register_notifier (struct platform_device *pdev, +struct notifier_block *nb) +{Hand-rolling devres managed resources is usually reserved for subsystem level API calls. Why do the usual device driver .remove() handling work for you?
This is used by 3 subdevices: serio, keys and charger, so this just seems cleaner way to register and deregister notifier.
Quoted 61 lines
Quoted 49 lines
drivers/mfd/asus-transformer-ec.c new file +49
-0+struct asusec_info *ec =dev_get_drvdata (pdev->dev.parent); +struct notifier_block **res; +int ret; + + res =devres_alloc (asus_ec_remove_notifier,sizeof (*res),GFP_KERNEL ); +if (!res) +return -ENOMEM ; + + *res = nb; + ret =blocking_notifier_chain_register (&ec->notify_list, nb); +if (ret) { +devres_free (res); +return ret; + } + +devres_add (&pdev->dev, res); + +return 0 ; +} +EXPORT_SYMBOL_GPL (devm_asus_ec_register_notifier); + +static int asus_ec_signal_request (const struct asusec_info *ec) +{ +struct asus_ec_data *priv =to_ec_data (ec); + +guard (mutex)(&priv->ecreq_lock); + +dev_dbg (&priv->self->dev,"EC request\n" ); + +gpiod_set_value_cansleep (priv->ecreq,1 ); +msleep (50 ); + +gpiod_set_value_cansleep (priv->ecreq,0 ); +msleep (200 ); + +return 0 ; +} + +static int asus_ec_write (struct asus_ec_data *priv,u16 data) +{ +int ret =i2c_smbus_write_word_data (priv->self,ASUSEC_WRITE_BUF , data); + +dev_dbg (&priv->self->dev,"EC write: %04x, ret = %d\n" , data, ret); + +return ret; +} + +static int asus_ec_read (struct asus_ec_data *priv,bool in_irq) +{drivers/mfd/asus-transformer-ec.c line 240
Quoted 10 lines
drivers/mfd/asus-transformer-ec.c new file +10
-0+int ret =i2c_smbus_read_i2c_block_data (priv->self,ASUSEC_READ_BUF , +sizeof (priv->ec_data), + priv->ec_data); + +dev_dbg (&priv->self->dev,"EC read: %*ph, ret = %d%s\n" , +sizeof (priv->ec_data), priv->ec_data, + ret, in_irq ?"; in irq" :"" ); + +return ret; +}No abstractions for the sake of it. All this goes away with Regmap anyway.
Quoted 16 lines
drivers/mfd/asus-transformer-ec.c line 252
Quoted 12 lines
drivers/mfd/asus-transformer-ec.c new file +12
-0+ +/** +* asus_ec_i2c_command - Send a 16-bit command to the ASUS EC. +* @ec: Pointer to the shared ASUS EC structure. +* @data: The 16-bit command (word) to be sent. +* +* Return: 0 on success or a negative error code on failure. +*/ +int asus_ec_i2c_command (const struct asusec_info *ec,u16 data) +{ +return asus_ec_write (to_ec_data (ec), data); +}Is this wrapper function strictly necessary? We should try to avoid superfluous abstractions that do nothing but call another function.
This one is used by serio and keys too. I will see what I can do with regmaps.
Quoted 263 lines
Quoted 250 lines
drivers/mfd/asus-transformer-ec.c new file +250
-0+EXPORT_SYMBOL_GPL (asus_ec_i2c_command); + +static void asus_ec_clear_buffer (struct asus_ec_data *priv) +{ +int retry =ASUSEC_RSP_BUFFER_SIZE ; + +while (retry--) { +if (asus_ec_read (priv, false) <0 ) +continue ; + +if (priv->ec_data[1 ] &ASUSEC_OBF_MASK ) +continue ; + +break ; + } +} + +static int asus_ec_log_info (struct asus_ec_data *priv,unsigned int reg, +const char *name,char **out) +{ +char buf[DOCKRAM_ENTRY_BUFSIZE ]; +int ret; + + ret =asus_dockram_read (priv->info.dockram, reg, buf); +if (ret <0 ) +return ret; + +if (!priv->logging_disabled) +dev_info (&priv->self->dev,"%-14s: %.*s\n" , name, + buf[0 ], buf +1 ); + +if (out) + *out =kstrndup (buf +1 , buf[0 ],GFP_KERNEL ); + +return 0 ; +} + +static int asus_ec_reset (struct asus_ec_data *priv) +{ +int retry, ret; + +for (retry =0 ; retry <3 ; retry++) { + ret =asus_ec_write (priv,0 ); +if (!ret) +return 0 ; + +msleep (300 ); + } + +return ret; +} + +static int asus_ec_magic_debug (struct asus_ec_data *priv) +{ +u64 flag; +int ret; + + ret =asus_ec_get_ctl (&priv->info, &flag); +if (ret <0 ) +return ret; + + flag &=ASUSEC_CTL_SUSB_MODE ; +dev_info (&priv->self->dev,"EC FW behaviour: %s\n" , + flag ?"susb on when receive ec_req" : +"susb on when system wakeup" ); + +return 0 ; +} + +static int asus_ec_set_factory_mode (struct asus_ec_data *priv,bool on) +{ +dev_info (&priv->self->dev,"Entering %s mode.\n" , on ?"factory" : +"normal" ); + +return asus_ec_update_ctl (&priv->info,ASUSEC_CTL_FACTORY_MODE , + on ?ASUSEC_CTL_FACTORY_MODE :0 ); +} + +static int asus_ec_detect (struct asus_ec_data *priv) +{ +char *model =NULL ; +int ret; + + ret =asus_ec_reset (priv); +if (ret) + goto err_exit; + +asus_ec_clear_buffer (priv); + + ret =asus_ec_log_info (priv,ASUSEC_DOCKRAM_INFO_MODEL ,"model" , &model); +if (ret) + goto err_exit; + + ret =asus_ec_log_info (priv,ASUSEC_DOCKRAM_INFO_FW ,"FW version" ,NULL ); +if (ret) + goto err_exit; + + ret =asus_ec_log_info (priv,ASUSEC_DOCKRAM_INFO_CFGFMT ,"Config format" ,NULL ); +if (ret) + goto err_exit; + + ret =asus_ec_log_info (priv,ASUSEC_DOCKRAM_INFO_HW ,"HW version" ,NULL ); +if (ret) + goto err_exit; + + priv->logging_disabled = true; + + ret =asus_ec_magic_debug (priv); +if (ret) + goto err_exit; + + priv->info.model = model; + priv->info.name = priv->data->name; + +if (priv->data->clr_fmode) +asus_ec_set_factory_mode (priv, false); + +err_exit: +if (ret) +dev_err (&priv->self->dev,"failed to access EC: %d\n" , ret); + +return ret; +} + +static void asus_ec_handle_smi (struct asus_ec_data *priv,unsigned int code) +{ +dev_dbg (&priv->self->dev,"SMI interrupt: 0x%02x\n" , code); + +switch (code) { +case ASUSEC_SMI_HANDSHAKE : +case ASUSEC_SMI_RESET : +asus_ec_detect (priv); +break ; + } +} + +static irqreturn_t asus_ec_interrupt (int irq,void *dev_id) +{ +struct asus_ec_data *priv = dev_id; +unsigned long notify_action; +int ret; + + ret =asus_ec_read (priv, true); +if (ret <=0 || !(priv->ec_data[1 ] &ASUSEC_OBF_MASK )) +return IRQ_NONE ; + + notify_action = priv->ec_data[1 ]; +if (notify_action &ASUSEC_SMI_MASK ) { +unsigned int code = priv->ec_data[2 ]; + +asus_ec_handle_smi (priv, code); + + notify_action |= code <<8 ; +dev_dbg (&priv->self->dev,"SMI code: 0x%02x\n" , code); + } + +blocking_notifier_call_chain (&priv->info.notify_list, + notify_action, priv->ec_data); + +return IRQ_HANDLED ; +} + +static ssize_t dockram_read (struct file *filp,char __user *buf, +size_t count,loff_t *ppos) +{ +struct i2c_client *client = filp->private_data; +unsigned int reg, rsize; +ssize_t n_read =0 , val; +loff_t off = *ppos; +char *data; +int ret; + + reg = off /DOCKRAM_ENTRY_SIZE ; + off %=DOCKRAM_ENTRY_SIZE ; + rsize =DOCKRAM_ENTRIES *DOCKRAM_ENTRY_SIZE ; + +if (!count) +return 0 ; + + data =kmalloc (DOCKRAM_ENTRY_BUFSIZE ,GFP_KERNEL ); + +while (reg <DOCKRAM_ENTRIES ) { +unsigned int len =DOCKRAM_ENTRY_SIZE - off; + +if (len > rsize) + len = rsize; + + ret =asus_dockram_read (client, reg, data); +if (ret <0 ) { +if (!n_read) + n_read = ret; +break ; + } + + val =copy_to_user (buf, data +1 + off, len); +if (val == len) +return -EFAULT ; + + *ppos += len; + n_read += len; + +if (len == rsize) +break ; + + rsize -= len; + buf += len; + off =0 ; + ++reg; + } + +kfree (data); + +return n_read; +} + +static int dockram_write_one (struct i2c_client *client,int reg, +const char __user *buf,size_t count) +{ +struct dockram_ec_data *priv =i2c_get_clientdata (client); +int ret; + +if (!count || count >DOCKRAM_ENTRY_SIZE ) +return -EINVAL ; +if (buf[0 ] != count -1 ) +return -EINVAL ; + +guard (mutex)(&priv->ctl_lock); + + priv->ctl_data[0 ] = (u8 )count; +memcpy (priv->ctl_data +1 , buf, count); + ret =asus_dockram_write (client, reg, priv->ctl_data); + +return ret; +} + +static ssize_t dockram_write (struct file *filp,const char __user *buf, +size_t count,loff_t *ppos) +{ +struct i2c_client *client = filp->private_data; +unsigned int reg; +loff_t off = *ppos; +int ret; + +if (off %DOCKRAM_ENTRY_SIZE !=0 ) +return -EINVAL ; + + reg = off /DOCKRAM_ENTRY_SIZE ; +if (reg >=DOCKRAM_ENTRIES ) +return -EINVAL ; +drivers/mfd/asus-transformer-ec.c line 512
Quoted 10 lines
drivers/mfd/asus-transformer-ec.c new file +10
-0+ ret =dockram_write_one (client, reg, buf, count); + +return ret <0 ? ret : count; +} + +static const struct debugfs_short_fops dockram_fops = { + .read = dockram_read, + .write = dockram_write, + .llseek = default_llseek, +};You should not be giving userspace free reign over device memory.
Quoted 2 lines
If you switch to Regmap, you can use
regmap-debugfs.cfor this.
Noted
Quoted 65 lines
Quoted 52 lines
drivers/mfd/asus-transformer-ec.c new file +52
-0+static int control_reg_get (void *ec,u64 *val) +{ +return asus_ec_get_ctl (ec, val); +} + +static int control_reg_set (void *ec,u64 val) +{ +return asus_ec_update_ctl (ec, ~0ull , val); +} + +DEFINE_DEBUGFS_ATTRIBUTE (control_reg_fops, control_reg_get, + control_reg_set,"%016llx\n" ); + +static int ec_request_set (void *ec,u64 val) +{ +if (val) +asus_ec_signal_request (ec); + +return 0 ; +} + +DEFINE_DEBUGFS_ATTRIBUTE (ec_request_fops,NULL , ec_request_set,"%llu\n" ); + +static int ec_irq_set (void *ec,u64 val) +{ +struct asus_ec_data *priv =to_ec_data (ec); + +if (val) +irq_wake_thread (priv->self->irq, priv); + +return 0 ; +} + +DEFINE_DEBUGFS_ATTRIBUTE (ec_irq_fops,NULL , ec_irq_set,"%llu\n" ); + +static void asus_ec_debugfs_remove (void *debugfs_root) +{ +debugfs_remove_recursive (debugfs_root); +} + +static void devm_asus_ec_debugfs_init (struct device *dev) +{ +struct asusec_info *ec =dev_get_drvdata (dev); +struct asus_ec_data *priv =to_ec_data (ec); +struct dentry *debugfs_root, *dockram_dir; +char *name =devm_kasprintf (dev,GFP_KERNEL ,"asus-ec-%s" , + priv->data->name); + + debugfs_root =debugfs_create_dir (name,NULL ); + dockram_dir =debugfs_create_dir ("dockram" , debugfs_root); + + debugfs_create_file("ec_irq", 0200, debugfs_root, ec,drivers/mfd/asus-transformer-ec.c line 575
Quoted 10 lines
drivers/mfd/asus-transformer-ec.c new file +10
-0+ &ec_irq_fops); +debugfs_create_file ("ec_request" ,0200 , debugfs_root, ec, + &ec_request_fops); +debugfs_create_file ("control_reg" ,0644 , dockram_dir, ec, + &control_reg_fops); +debugfs_create_file ("dockram" ,0644 , dockram_dir, + priv->info.dockram, &dockram_fops); + +devm_add_action_or_reset (dev, asus_ec_debugfs_remove, debugfs_root); +}Why is this being controlled via debugfs?
Quoted 5 lines
Use the correct kernel APIs instead.
If this is just for development, keep it and all of the extra verbose printing in the BSP tree. It does not belong in the upstream kernel.
Prints are required to show Firmware type, version and behavior.
Quoted 32 lines
Quoted 32 lines
drivers/mfd/asus-transformer-ec.c new file +32
-0+static void asus_ec_release_dockram_dev (void *client) +{ +i2c_unregister_device (client); +} + +static struct i2c_client *devm_asus_dockram_get (struct device *dev) +{ +struct i2c_client *parent =to_i2c_client (dev); +struct i2c_client *dockram; +struct dockram_ec_data *priv; +int ret; + + dockram =i2c_new_ancillary_device (parent,"dockram" , + parent->addr +2 ); +if (IS_ERR (dockram)) +return dockram; + + ret =devm_add_action_or_reset (dev, asus_ec_release_dockram_dev, + dockram); +if (ret) +return ERR_PTR (ret); + + priv =devm_kzalloc (&dockram->dev,sizeof (*priv),GFP_KERNEL ); +if (!priv) +return ERR_PTR (-ENOMEM ); + +i2c_set_clientdata (dockram, priv); +mutex_init (&priv->ctl_lock); + +return dockram; +} +
Quoted 9 lines
drivers/mfd/asus-transformer-ec.c line 612
Quoted 4 lines
drivers/mfd/asus-transformer-ec.c new file +4
-0+static int asus_ec_probe (struct i2c_client *client) +{ +struct device *dev = &client->dev; +struct asus_ec_data *priv;Could we use a clearer, more specific name for the private data variable instead of the generic term 'priv'? Using something like 'ddata' is usually preferred.
fine
Quoted 20 lines
Quoted 20 lines
drivers/mfd/asus-transformer-ec.c new file +20
-0+int ret; + +if (!i2c_check_functionality (client->adapter,I2C_FUNC_SMBUS_I2C_BLOCK )) +return dev_err_probe (dev, -ENXIO , +"I2C bus is missing required SMBus block mode support\n" ); + + priv =devm_kzalloc (dev,sizeof (*priv),GFP_KERNEL ); +if (!priv) +return -ENOMEM ; + + priv->data =device_get_match_data (dev); +if (!priv->data) +return -ENODEV ; + +i2c_set_clientdata (client, priv); + priv->self = client; + + priv->info.dockram =devm_asus_dockram_get (dev); +if (IS_ERR (priv->info.dockram)) + return dev_err_probe(dev, PTR_ERR(priv->info.dockram),
Quoted 9 lines
drivers/mfd/asus-transformer-ec.c line 638
Quoted 6 lines
drivers/mfd/asus-transformer-ec.c new file +6
-0+ "failed to get dockram\n"); + + priv->ecreq =devm_gpiod_get (dev,"request" ,GPIOD_OUT_LOW ); +if (IS_ERR (priv->ecreq)) +return dev_err_probe (dev,PTR_ERR (priv->ecreq), +"failed to get request GPIO\n" );"get" or "request"
request is gpio's name, request gpio
Quoted 38 lines
Quoted 38 lines
drivers/mfd/asus-transformer-ec.c new file +38
-0+BLOCKING_INIT_NOTIFIER_HEAD (&priv->info.notify_list); +mutex_init (&priv->ecreq_lock); + +asus_ec_signal_request (&priv->info); + + ret =asus_ec_detect (priv); +if (ret) +return dev_err_probe (dev, ret,"failed to detect EC version\n" ); + + ret =devm_request_threaded_irq (dev, client->irq,NULL , + &asus_ec_interrupt, +IRQF_ONESHOT |IRQF_SHARED , + client->name, priv); +if (ret) +return dev_err_probe (dev, ret,"failed to register IRQ\n" ); + +/* Parent I2C controller uses DMA, ASUS EC and child devices do not */ + client->dev.coherent_dma_mask =0 ; + client->dev.dma_mask = &client->dev.coherent_dma_mask; + +if (IS_ENABLED (CONFIG_DEBUG_FS )) +devm_asus_ec_debugfs_init (dev); + +return devm_mfd_add_devices (dev,0 , priv->data->mfd_devices, + priv->data->num_devices,NULL ,0 ,NULL ); +} + +static const struct mfd_cell asus_ec_sl101_dock_mfd_devices[] = { + { + .name ="asus-transformer-ec-kbc" , + }, +}; + +static const struct asus_ec_chip_data asus_ec_sl101_dock_data = { + .name ="dock" , + .mfd_devices = asus_ec_sl101_dock_mfd_devices, + .num_devices =ARRAY_SIZE (asus_ec_sl101_dock_mfd_devices), + .clr_fmode = false,
Quoted 9 lines
drivers/mfd/asus-transformer-ec.c line 683
Quoted 6 lines
drivers/mfd/asus-transformer-ec.c new file +6
-0+}; + +static const struct mfd_cell asus_ec_tf101_dock_mfd_devices[] = { + { + .name ="asus-transformer-ec-battery" , + .id =1 ,Why doesn't
PLATFORM_DEVID_AUTOalready do the right thing?
There may be 2 of this EC in the device with similar set of cells, last time I have tested EC that was registered second caused clash with first EC cause of these cells. I will check if this behavior changed now.
Quoted 56 lines
Quoted 56 lines
drivers/mfd/asus-transformer-ec.c new file +56
-0+ }, { + .name = "asus-transformer-ec-charger", + .id = 1, + }, { + .name = "asus-transformer-ec-led", + .id = 1, + }, { + .name = "asus-transformer-ec-keys", + }, { + .name = "asus-transformer-ec-kbc", + }, +}; + +static const struct asus_ec_chip_data asus_ec_tf101_dock_data = { + .name = "dock", + .mfd_devices = asus_ec_tf101_dock_mfd_devices, + .num_devices = ARRAY_SIZE(asus_ec_tf101_dock_mfd_devices), + .clr_fmode = false, +}; + +static const struct mfd_cell asus_ec_tf201_pad_mfd_devices[] = { + { + .name = "asus-transformer-ec-battery", + .id = 0, + }, { + .name = "asus-transformer-ec-led", + .id = 0, + }, +}; + +static const struct asus_ec_chip_data asus_ec_tf201_pad_data = { + .name = "pad", + .mfd_devices = asus_ec_tf201_pad_mfd_devices, + .num_devices = ARRAY_SIZE(asus_ec_tf201_pad_mfd_devices), + .clr_fmode = true, +}; + +static const struct mfd_cell asus_ec_tf600t_pad_mfd_devices[] = { + { + .name = "asus-transformer-ec-battery", + .id = 0, + }, { + .name = "asus-transformer-ec-charger", + .id = 0, + }, { + .name = "asus-transformer-ec-led", + .id = 0, + }, +}; + +static const struct asus_ec_chip_data asus_ec_tf600t_pad_data = { + .name = "pad", + .mfd_devices = asus_ec_tf600t_pad_mfd_devices, + .num_devices = ARRAY_SIZE(asus_ec_tf600t_pad_mfd_devices), + .clr_fmode = true, +};
Quoted 8 lines
drivers/mfd/asus-transformer-ec.c line 743
Quoted 4 lines
drivers/mfd/asus-transformer-ec.c new file +4
-0+ +static const struct of_device_id asus_ec_match[] = { + { .compatible ="asus,sl101-ec-dock" , .data = &asus_ec_sl101_dock_data }, + { .compatible ="asus,tf101-ec-dock" , .data = &asus_ec_tf101_dock_data },Could we use the match table's data field to store an 'enum' or integer ID instead of passing platform data via the '.data' field? We could then use a 'switch' statement in the C code to select the correct '
mfd_cell' array.
Yes
Quoted 199 lines
Quoted 113 lines
drivers/mfd/asus-transformer-ec.c new file +19
-0+ { .compatible = "asus,tf201-ec-pad", .data = &asus_ec_tf201_pad_data }, + { .compatible = "asus,tf600t-ec-pad", .data = &asus_ec_tf600t_pad_data }, + { } +}; +MODULE_DEVICE_TABLE (of, asus_ec_match); + +static struct i2c_driver asus_ec_driver = { + .driver = { + .name ="asus-transformer-ec" , + .of_match_table = asus_ec_match, + }, + .probe = asus_ec_probe, +}; +module_i2c_driver (asus_ec_driver); + +MODULE_AUTHOR ("Michał Mirosław <[email protected]>" ); +MODULE_AUTHOR ("Svyatoslav Ryhel <[email protected]>" ); +MODULE_DESCRIPTION ("ASUS Transformer's EC driver" ); +MODULE_LICENSE ("GPL" );include/linux/mfd/asus-transformer-ec.h new file +88
-0@@ -0,0 +1,162 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef __MFD_ASUS_TRANSFORMER_EC_H +#define __MFD_ASUS_TRANSFORMER_EC_H + +#include <linux/notifier.h> +#include <linux/platform_device.h> +#include <linux/workqueue.h> + +struct i2c_client ; + +struct asusec_info { +const char *model; +const char *name; +struct i2c_client *dockram; +struct workqueue_struct *wq; +struct blocking_notifier_head notify_list; +}; + +#define DOCKRAM_ENTRIES 0x100 +#define DOCKRAM_ENTRY_SIZE 32 +#define DOCKRAM_ENTRY_BUFSIZE (DOCKRAM_ENTRY_SIZE + 1) + +/* interrupt sources */ +#define ASUSEC_OBF_MASK BIT(0) +#define ASUSEC_KEY_MASK BIT(2) +#define ASUSEC_KBC_MASK BIT(3) +#define ASUSEC_AUX_MASK BIT(5) +#define ASUSEC_SCI_MASK BIT(6) +#define ASUSEC_SMI_MASK BIT(7) + +/* SMI notification codes */ +#define ASUSEC_SMI_POWER_NOTIFY 0x31/* [un]plugging USB cable */ +#define ASUSEC_SMI_HANDSHAKE 0x50/* response to ec_req edge */ +#define ASUSEC_SMI_WAKE 0x53 +#define ASUSEC_SMI_RESET 0x5f +#define ASUSEC_SMI_ADAPTER_EVENT 0x60/* [un]plugging charger to dock */ +#define ASUSEC_SMI_BACKLIGHT_ON 0x63 +#define ASUSEC_SMI_AUDIO_DOCK_IN 0x70 + +#define ASUSEC_SMI_ACTION (code) (ASUSEC_SMI_MASK | ASUSEC_OBF_MASK | \ + (ASUSEC_SMI_##code << 8)) + +/* control register [0x0a] layout */ +#define ASUSEC_CTL_SIZE 8 + +/* +* EC reports power from 40-pin connector in the LSB of the control +* register. The following values have been observed (xor 0x02): +* +* PAD-ec no-plug 0x40 / PAD-ec DOCK 0x20 / DOCK-ec no-plug 0x40 +* PAD-ec AC 0x25 / PAD-ec DOCK+AC 0x24 / DOCK-ec AC 0x25 +* PAD-ec USB 0x45 / PAD-ec DOCK+USB 0x24 / DOCK-ec USB 0x41 +*/ + +#define ASUSEC_CTL_DIRECT_POWER_SOURCE BIT_ULL(0) +#define ASUSEC_STAT_CHARGING BIT_ULL(2) +#define ASUSEC_CTL_FULL_POWER_SOURCE BIT_ULL(5) +#define ASUSEC_CTL_SUSB_MODE BIT_ULL(9) +#define ASUSEC_CMD_SUSPEND_S3 BIT_ULL(33) +#define ASUSEC_CTL_TEST_DISCHARGE BIT_ULL(35) +#define ASUSEC_CMD_SUSPEND_INHIBIT BIT_ULL(37) +#define ASUSEC_CTL_FACTORY_MODE BIT_ULL(38) +#define ASUSEC_CTL_KEEP_AWAKE BIT_ULL(39) +#define ASUSEC_CTL_USB_CHARGE BIT_ULL(40) +#define ASUSEC_CTL_LED_BLINK BIT_ULL(40) +#define ASUSEC_CTL_LED_AMBER BIT_ULL(41) +#define ASUSEC_CTL_LED_GREEN BIT_ULL(42) +#define ASUSEC_CMD_SWITCH_HDMI BIT_ULL(56) +#define ASUSEC_CMD_WIN_SHUTDOWN BIT_ULL(62) + +#define ASUSEC_DOCKRAM_INFO_MODEL 0x01 +#define ASUSEC_DOCKRAM_INFO_FW 0x02 +#define ASUSEC_DOCKRAM_INFO_CFGFMT 0x03 +#define ASUSEC_DOCKRAM_INFO_HW 0x04 +#define ASUSEC_DOCKRAM_CONTROL 0x0a +#define ASUSEC_DOCKRAM_BATT_CTL 0x14 + +#define ASUSEC_WRITE_BUF 0x64 +#define ASUSEC_READ_BUF 0x6a + +/* dockram comm */ +int asus_dockram_read (struct i2c_client *client,int reg,char *buf); +int asus_dockram_write (struct i2c_client *client,int reg,const char *buf); +int asus_dockram_access_ctl (struct i2c_client *client, +u64 *out,u64 mask,u64 xor); + +/* EC public API */ +include/linux/mfd/asus-transformer-ec.h line 98
Quoted 10 lines
include/linux/mfd/asus-transformer-ec.h new file +10
-0+/** +* cell_to_ec - Request the shared ASUS EC structure via a subdevice's pdev. +* @pdev: EC subdevice pdev requesting access to the shared ASUS EC structure. +* +* Returns a pointer to the asusec_info structure. +*/ +static inline struct asusec_info *cell_to_ec (struct platform_device *pdev) +{ +return dev_get_drvdata (pdev->dev.parent); +}This is both misleading and already exists.
Quoted 49 lines
include/linux/mfd/asus-transformer-ec.h new file +49
-0+ +/** +* asus_ec_get_ctl - Read from the DockRAM control register. +* @ec: Pointer to the shared ASUS EC structure. +* @out: Pointer to the variable where the register value will be stored. +* +* Performs a control register read and stores the value in @out. +* +* Return: 0 on success, or a negative errno code on failure. +*/ +static inline int asus_ec_get_ctl (const struct asusec_info *ec,u64 *out) +{ +return asus_dockram_access_ctl (ec->dockram, out,0 ,0 ); +} + +/** +* asus_ec_update_ctl - Update the DockRAM control register. +* @ec: Pointer to the shared ASUS EC structure. +* @mask: Bitmask of bits to be cleared. +* @xor: Bitmask of bits to be toggled or set (via XOR). +* +* Performs a read-modify-write update on the control register using +* the provided @mask and @xor values. +* +* Return: 0 on success, or a negative errno code on failure. +*/ +static inline int asus_ec_update_ctl (const struct asusec_info *ec, +u64 mask,u64 xor) +{ +return asus_dockram_access_ctl (ec->dockram,NULL , mask, xor); +} + +/** +* asus_ec_set_ctl_bits - Sets bits of the DockRAM control register. +* @ec: Pointer to the shared ASUS EC structure. +* @mask: Bitmask of bits to be set. +* +* Sets bits of the control register using the provided @mask value. +* +* Return: 0 on success, or a negative errno code on failure. +*/ +static inline int asus_ec_set_ctl_bits (const struct asusec_info *ec,u64 mask) +{ +return asus_dockram_access_ctl (ec->dockram,NULL , mask, mask); +} + +/** +* asus_ec_clear_ctl_bits - Clears bits of the DockRAM control register. +* @ec: Pointer to the shared ASUS EC structure. include/linux/mfd/asus-transformer-ec.h line 157
Quoted 10 lines
include/linux/mfd/asus-transformer-ec.h new file +10
-0+* @mask: Bitmask of bits to be cleared. +* +* Clears bits of the control register using the provided @mask value. +* +* Return: 0 on success, or a negative errno code on failure. +*/ +static inline int asus_ec_clear_ctl_bits (const struct asusec_info *ec,u64 mask) +{ +return asus_dockram_access_ctl (ec->dockram,NULL , mask,0 ); +}We don't typically allow abstraction for the sake of abstraction.
Quoted 8 lines
include/linux/mfd/asus-transformer-ec.h new file +4
-1+int asus_ec_i2c_command (const struct asusec_info *ec,u16 data); +int devm_asus_ec_register_notifier (struct platform_device *dev, +struct notifier_block *nb); +#endif /* __MFD_ASUS_TRANSFORMER_EC_H */ --2.51.0
--
Lee Jones
Lee Jones
maintainerRe: [PATCH v6 2/7] mfd: Add driver for ASUS Transformer embedded controller
Quoted 22 lines · Svyatoslav Ryhel
чт, 14 трав. 2026 р. о 13:02 Lee Jones <[email protected]> пише:
Quoted 21 lines · Svyatoslav Ryhel
Quoted 19 lines
From: Michał Mirosław <[email protected]>
Support Nuvoton NPCE795-based ECs as used in Asus Transformer TF201, TF300T, TF300TG, TF300TL and TF700T pad and dock, as well as TF101 dock and TF600T, P1801-T and TF701T pad. This is a glue driver handling detection and common operations for EC's functions.
Co-developed-by Svyatoslav Ryhel <[email protected]>Signed-off-by Svyatoslav Ryhel <[email protected]>Signed-off-by Michał Mirosław <[email protected]>--- drivers/mfd/Kconfig | 14 + drivers/mfd/Makefile | 1 + drivers/mfd/asus-transformer-ec.c | 762 ++++++++++++++++++++++++ include/linux/mfd/asus-transformer-ec.h | 162 +++++ 4 files changed, 939 insertions(+) create mode 100644 drivers/mfd/asus-transformer-ec.c create mode 100644 include/linux/mfd/asus-transformer-ec.h
[...]
Quoted 13 lines
Quoted 12 lines
drivers/mfd/asus-transformer-ec.c line 32
Quoted 6 lines
drivers/mfd/asus-transformer-ec.c new file +6
-0+unsigned int num_devices; +bool clr_fmode;/* clear Factory Mode bit in EC control register */ +}; + +struct asus_ec_data { +struct asusec_info info;You have 'data' and 'info' which a) using non-forthcoming nomenclature and doesn't tell me anything and then you b) put 'info' in the device's
driver_dataattribute which is very confusing.driver_datashould be for what we call ddata which I assume is expressed as 'data' here.
Quoted 2 lines
asusec_infois shared among all child devices and is exposed while remaining elements of this struct are for internal use only.
Our terminology for that is usually ddata, that gets stored in 'struct devices' device_data attribute.
Quoted 24 lines
Quoted 10 lines
drivers/mfd/asus-transformer-ec.c line 34
Quoted 2 lines
drivers/mfd/asus-transformer-ec.c new file +2
-0+struct mutex ecreq_lock;/* prevent simultaneous access */ +struct gpio_desc *ecreq;If I hadn't seen the declaration, I'd have no idea this was a GPIO descriptor. Please improve the nomenclature throughout.
Quoted 1 line
+ struct
i2c_client*self;Again, please use standard naming conventions:
Quoted 9 lines
% git grep "struct
i2c_client" | grep "\*self" | wc -l 0% git grep "struct
i2c_client" | grep "\*client" | wc -l 6304% git grep "struct
i2c_client" | grep "\*i2c" | wc -l 903ok, noted.
Quoted 2 lines
Quoted 1 line
+ const struct
asus_ec_chip_data*data;
Quoted 8 lines
Quoted 6 lines
'data', 'priv' and 'info' should be improved.
Quoted 1 line
+ char ec_data[DOCKRAM_ENTRY_BUFSIZE];
An array of chars called 'data'. This could be anything.
Do you have a comprehensive list of name conventions you find suitable?
Anything descriptive that alludes to the type of data being held there.
There are 100's of good examples, but a handful of generic / bad ones.
Quoted 62 lines
Quoted 36 lines
Quoted 1 line
+ bool
logging_disabled;This debugging tool is probably never going to be used again.
Keep it local.
Quoted 15 lines
drivers/mfd/asus-transformer-ec.c new file +15
-0+}; + +struct dockram_ec_data { +struct mutex ctl_lock;/* prevent simultaneous access */ +char ctl_data[DOCKRAM_ENTRY_BUFSIZE ]; +}; + +#define to_ec_data (ec) \ + container_of(ec, struct asus_ec_data, info) + +/** +* asus_dockram_read - Read a register from the DockRAM device. +* @client: Handle to the DockRAM device. +* @reg: Register to read. +* @buf: Byte array into which data will be read; must be large enough to drivers/mfd/asus-transformer-ec.c line 63
Quoted 10 lines
drivers/mfd/asus-transformer-ec.c new file +10
-0+* hold the data returned by the DockRAM. +* +* This executes the DockRAM read based on the SMBus "block read" protocol +* or its emulation. It extracts DOCKRAM_ENTRY_SIZE bytes from the set +* register address. +* +* Returns a negative errno code else zero on success. +*/ +int asus_dockram_read (struct i2c_client *client,int reg,char *buf) +{Have you considered using Regmap for register access instead of implementing custom functions? Remaps already deals with caching and locking mechanisms that you'd get for free.
Quoted 2 lines
This looks like it would be replaced with
devm_regmap_init_i2c().I will consider this, thank you.
Quoted 11 lines
Quoted 11 lines
drivers/mfd/asus-transformer-ec.c new file +11
-0+struct device *dev = &client->dev; +int ret; + +memset (buf,0 ,DOCKRAM_ENTRY_BUFSIZE ); + ret =i2c_smbus_read_i2c_block_data (client, reg, +DOCKRAM_ENTRY_BUFSIZE , buf); +if (ret <0 ) +return ret; + +if (buf[0 ] >DOCKRAM_ENTRY_SIZE ) { + dev_err(dev, "bad data len; buffer: %*ph; ret: %d\n",Quoted 9 lines
drivers/mfd/asus-transformer-ec.c line 80
Quoted 6 lines
drivers/mfd/asus-transformer-ec.c new file +6
-0+ DOCKRAM_ENTRY_BUFSIZE, buf, ret); +return -EPROTO ; + } + +dev_dbg (dev,"got data; buffer: %*ph; ret: %d\n" , +DOCKRAM_ENTRY_BUFSIZE , buf, ret);Please remove all of these debug messages.
Quoted 1 line
Why debug messages cannot be preserved? They are specifically marked as
dev_dbg
It's a general convention.
After initial development, they tend to just litter the code-base.
Debug prints can be useful higher up the stack though.
[...]
Quoted 20 lines
Quoted 10 lines
Quoted 10 lines
+/**
+ *devm_asus_ec_register_notifier- Managed registration of notifier to an
+ * ASUS EC blocking notifier chain.
+ * @pdev: Device requesting the notifier (used for resource management).
+ * @nb: Notifier block to be registered.
+ *
+ * Register a notifier to the ASUS EC blocking notifier chain. The notifier
+ * will be automatically unregistered when the requesting device is detached.
+ *
+ * Return: 0 on success or a negative error code on failure.Quoted 9 lines
drivers/mfd/asus-transformer-ec.c line 181
Quoted 4 lines
+ */
drivers/mfd/asus-transformer-ec.c new file +3
-0+int devm_asus_ec_register_notifier (struct platform_device *pdev, +struct notifier_block *nb) +{Hand-rolling devres managed resources is usually reserved for subsystem level API calls. Why do the usual device driver .remove() handling work for you?
Quoted 2 lines
This is used by 3 subdevices: serio, keys and charger, so this just seems cleaner way to register and deregister notifier.
Clean to me would be to use the infrastructure that's put in place already. Unless I am missing the point of all of this.
[...]
Quoted 30 lines
Quoted 20 lines
Quoted 20 lines
drivers/mfd/asus-transformer-ec.c new file +20
-0+int ret; + +if (!i2c_check_functionality (client->adapter,I2C_FUNC_SMBUS_I2C_BLOCK )) +return dev_err_probe (dev, -ENXIO , +"I2C bus is missing required SMBus block mode support\n" ); + + priv =devm_kzalloc (dev,sizeof (*priv),GFP_KERNEL ); +if (!priv) +return -ENOMEM ; + + priv->data =device_get_match_data (dev); +if (!priv->data) +return -ENODEV ; + +i2c_set_clientdata (client, priv); + priv->self = client; + + priv->info.dockram =devm_asus_dockram_get (dev); +if (IS_ERR (priv->info.dockram)) + return dev_err_probe(dev, PTR_ERR(priv->info.dockram),Quoted 9 lines
drivers/mfd/asus-transformer-ec.c line 638
Quoted 6 lines
drivers/mfd/asus-transformer-ec.c new file +6
-0+ "failed to get dockram\n"); + + priv->ecreq =devm_gpiod_get (dev,"request" ,GPIOD_OUT_LOW ); +if (IS_ERR (priv->ecreq)) +return dev_err_probe (dev,PTR_ERR (priv->ecreq), +"failed to get request GPIO\n" );"get" or "request"
Quoted 1 line
request is gpio's name, request gpio
Ah yes. Maybe use 's to help with that. Right now is just reads strangely.
[...]
Signature · 1 line
Lee Jones
Re: [PATCH v6 2/7] mfd: Add driver for ASUS Transformer embedded controller
чт, 14 трав. 2026 р. о 18:50 Lee Jones <[email protected]> пише:
Quoted 75 lines · Svyatoslav Ryhel
Quoted 23 lines
чт, 14 трав. 2026 р. о 13:02 Lee Jones <[email protected]> пише:
Quoted 21 lines · Svyatoslav Ryhel
Quoted 19 lines
From: Michał Mirosław <[email protected]>
Support Nuvoton NPCE795-based ECs as used in Asus Transformer TF201, TF300T, TF300TG, TF300TL and TF700T pad and dock, as well as TF101 dock and TF600T, P1801-T and TF701T pad. This is a glue driver handling detection and common operations for EC's functions.
Co-developed-by Svyatoslav Ryhel <[email protected]>Signed-off-by Svyatoslav Ryhel <[email protected]>Signed-off-by Michał Mirosław <[email protected]>--- drivers/mfd/Kconfig | 14 + drivers/mfd/Makefile | 1 + drivers/mfd/asus-transformer-ec.c | 762 ++++++++++++++++++++++++ include/linux/mfd/asus-transformer-ec.h | 162 +++++ 4 files changed, 939 insertions(+) create mode 100644 drivers/mfd/asus-transformer-ec.c create mode 100644 include/linux/mfd/asus-transformer-ec.h[...]
Quoted 13 lines
Quoted 12 lines
drivers/mfd/asus-transformer-ec.c line 32
Quoted 6 lines
drivers/mfd/asus-transformer-ec.c new file +6
-0+unsigned int num_devices; +bool clr_fmode;/* clear Factory Mode bit in EC control register */ +}; + +struct asus_ec_data { +struct asusec_info info;You have 'data' and 'info' which a) using non-forthcoming nomenclature and doesn't tell me anything and then you b) put 'info' in the device's
driver_dataattribute which is very confusing.driver_datashould be for what we call ddata which I assume is expressed as 'data' here.Quoted 2 lines
asusec_infois shared among all child devices and is exposed while remaining elements of this struct are for internal use only.Our terminology for that is usually ddata, that gets stored in 'struct devices'
device_dataattribute.Quoted 28 lines
Quoted 10 lines
drivers/mfd/asus-transformer-ec.c line 34
Quoted 2 lines
drivers/mfd/asus-transformer-ec.c new file +2
-0+struct mutex ecreq_lock;/* prevent simultaneous access */ +struct gpio_desc *ecreq;If I hadn't seen the declaration, I'd have no idea this was a GPIO descriptor. Please improve the nomenclature throughout.
Quoted 1 line
+ struct
i2c_client*self;Again, please use standard naming conventions:
Quoted 9 lines
% git grep "struct
i2c_client" | grep "\*self" | wc -l 0% git grep "struct
i2c_client" | grep "\*client" | wc -l 6304% git grep "struct
i2c_client" | grep "\*i2c" | wc -l 903ok, noted.
Quoted 6 lines
Quoted 1 line
+ const struct
asus_ec_chip_data*data;'data', 'priv' and 'info' should be improved.
Quoted 1 line
+ char ec_data[DOCKRAM_ENTRY_BUFSIZE];
Quoted 50 lines
Quoted 4 lines
Quoted 2 lines
An array of chars called 'data'. This could be anything.
Do you have a comprehensive list of name conventions you find suitable?
Anything descriptive that alludes to the type of data being held there.
There are 100's of good examples, but a handful of generic / bad ones.
Quoted 41 lines
Quoted 36 lines
Quoted 1 line
+ bool
logging_disabled;This debugging tool is probably never going to be used again.
Keep it local.
Quoted 15 lines
drivers/mfd/asus-transformer-ec.c new file +15
-0+}; + +struct dockram_ec_data { +struct mutex ctl_lock;/* prevent simultaneous access */ +char ctl_data[DOCKRAM_ENTRY_BUFSIZE ]; +}; + +#define to_ec_data (ec) \ + container_of(ec, struct asus_ec_data, info) + +/** +* asus_dockram_read - Read a register from the DockRAM device. +* @client: Handle to the DockRAM device. +* @reg: Register to read. +* @buf: Byte array into which data will be read; must be large enough to drivers/mfd/asus-transformer-ec.c line 63
Quoted 10 lines
drivers/mfd/asus-transformer-ec.c new file +10
-0+* hold the data returned by the DockRAM. +* +* This executes the DockRAM read based on the SMBus "block read" protocol +* or its emulation. It extracts DOCKRAM_ENTRY_SIZE bytes from the set +* register address. +* +* Returns a negative errno code else zero on success. +*/ +int asus_dockram_read (struct i2c_client *client,int reg,char *buf) +{Have you considered using Regmap for register access instead of implementing custom functions? Remaps already deals with caching and locking mechanisms that you'd get for free.
Quoted 2 lines
This looks like it would be replaced with
devm_regmap_init_i2c().I will consider this, thank you.
It seems that regmap does not fit for this purpose, but I might switch to plain i2c_smbus_read_i2c_block_data
Quoted 21 lines
Quoted 21 lines
Quoted 20 lines
Quoted 7 lines
drivers/mfd/asus-transformer-ec.c new file +7
-0+struct device *dev = &client->dev; +int ret; + +memset (buf,0 ,DOCKRAM_ENTRY_BUFSIZE ); + ret =i2c_smbus_read_i2c_block_data (client, reg, +DOCKRAM_ENTRY_BUFSIZE , buf); +if (ret <0 )drivers/mfd/asus-transformer-ec.c line 80
Quoted 10 lines
drivers/mfd/asus-transformer-ec.c new file +10
-0+return ret; + +if (buf[0 ] >DOCKRAM_ENTRY_SIZE ) { +dev_err (dev,"bad data len; buffer: %*ph; ret: %d\n" , +DOCKRAM_ENTRY_BUFSIZE , buf, ret); +return -EPROTO ; + } + +dev_dbg (dev,"got data; buffer: %*ph; ret: %d\n" , +DOCKRAM_ENTRY_BUFSIZE , buf, ret);Please remove all of these debug messages.
Quoted 8 lines
Quoted 1 line
Why debug messages cannot be preserved? They are specifically marked as
dev_dbgIt's a general convention.
After initial development, they tend to just litter the code-base.
Debug prints can be useful higher up the stack though.
I am fine with removing all debugs and logging but I strongly would like to keep EC model and firmware version along with susb and factory status. That may be quite useful in identifying EC used and its behavior without need in rebuilding the kernel and digging huge piles of downstream code in order to find how to dump these values.
Quoted 68 lines
[...]
Quoted 20 lines
Quoted 10 lines
Quoted 10 lines
+/**
+ *devm_asus_ec_register_notifier- Managed registration of notifier to an
+ * ASUS EC blocking notifier chain.
+ * @pdev: Device requesting the notifier (used for resource management).
+ * @nb: Notifier block to be registered.
+ *
+ * Register a notifier to the ASUS EC blocking notifier chain. The notifier
+ * will be automatically unregistered when the requesting device is detached.
+ *
+ * Return: 0 on success or a negative error code on failure.Quoted 9 lines
drivers/mfd/asus-transformer-ec.c line 181
Quoted 4 lines
+ */
drivers/mfd/asus-transformer-ec.c new file +3
-0+int devm_asus_ec_register_notifier (struct platform_device *pdev, +struct notifier_block *nb) +{Hand-rolling devres managed resources is usually reserved for subsystem level API calls. Why do the usual device driver .remove() handling work for you?
Quoted 2 lines
This is used by 3 subdevices: serio, keys and charger, so this just seems cleaner way to register and deregister notifier.
Clean to me would be to use the infrastructure that's put in place already. Unless I am missing the point of all of this.
[...]
Quoted 30 lines
Quoted 20 lines
Quoted 20 lines
drivers/mfd/asus-transformer-ec.c new file +20
-0+int ret; + +if (!i2c_check_functionality (client->adapter,I2C_FUNC_SMBUS_I2C_BLOCK )) +return dev_err_probe (dev, -ENXIO , +"I2C bus is missing required SMBus block mode support\n" ); + + priv =devm_kzalloc (dev,sizeof (*priv),GFP_KERNEL ); +if (!priv) +return -ENOMEM ; + + priv->data =device_get_match_data (dev); +if (!priv->data) +return -ENODEV ; + +i2c_set_clientdata (client, priv); + priv->self = client; + + priv->info.dockram =devm_asus_dockram_get (dev); +if (IS_ERR (priv->info.dockram)) + return dev_err_probe(dev, PTR_ERR(priv->info.dockram),Quoted 9 lines
drivers/mfd/asus-transformer-ec.c line 638
Quoted 6 lines
drivers/mfd/asus-transformer-ec.c new file +6
-0+ "failed to get dockram\n"); + + priv->ecreq =devm_gpiod_get (dev,"request" ,GPIOD_OUT_LOW ); +if (IS_ERR (priv->ecreq)) +return dev_err_probe (dev,PTR_ERR (priv->ecreq), +"failed to get request GPIO\n" );"get" or "request"
Quoted 1 line
request is gpio's name, request gpio
Ah yes. Maybe use 's to help with that. Right now is just reads strangely.
[...]
--
Lee Jones
Lee Jones
maintainerRe: [PATCH v6 2/7] mfd: Add driver for ASUS Transformer embedded controller
Quoted 121 lines · Svyatoslav Ryhel
чт, 14 трав. 2026 р. о 18:50 Lee Jones <[email protected]> пише:
Quoted 120 lines · Svyatoslav Ryhel
Quoted 23 lines
чт, 14 трав. 2026 р. о 13:02 Lee Jones <[email protected]> пише:
Quoted 21 lines · Svyatoslav Ryhel
Quoted 19 lines
From: Michał Mirosław <[email protected]>
Support Nuvoton NPCE795-based ECs as used in Asus Transformer TF201, TF300T, TF300TG, TF300TL and TF700T pad and dock, as well as TF101 dock and TF600T, P1801-T and TF701T pad. This is a glue driver handling detection and common operations for EC's functions.
Co-developed-by Svyatoslav Ryhel <[email protected]>Signed-off-by Svyatoslav Ryhel <[email protected]>Signed-off-by Michał Mirosław <[email protected]>--- drivers/mfd/Kconfig | 14 + drivers/mfd/Makefile | 1 + drivers/mfd/asus-transformer-ec.c | 762 ++++++++++++++++++++++++ include/linux/mfd/asus-transformer-ec.h | 162 +++++ 4 files changed, 939 insertions(+) create mode 100644 drivers/mfd/asus-transformer-ec.c create mode 100644 include/linux/mfd/asus-transformer-ec.h[...]
Quoted 13 lines
Quoted 12 lines
drivers/mfd/asus-transformer-ec.c line 32
Quoted 6 lines
drivers/mfd/asus-transformer-ec.c new file +6
-0+unsigned int num_devices; +bool clr_fmode;/* clear Factory Mode bit in EC control register */ +}; + +struct asus_ec_data { +struct asusec_info info;You have 'data' and 'info' which a) using non-forthcoming nomenclature and doesn't tell me anything and then you b) put 'info' in the device's
driver_dataattribute which is very confusing.driver_datashould be for what we call ddata which I assume is expressed as 'data' here.Quoted 2 lines
asusec_infois shared among all child devices and is exposed while remaining elements of this struct are for internal use only.Our terminology for that is usually ddata, that gets stored in 'struct devices'
device_dataattribute.Quoted 24 lines
Quoted 10 lines
drivers/mfd/asus-transformer-ec.c line 34
Quoted 2 lines
drivers/mfd/asus-transformer-ec.c new file +2
-0+struct mutex ecreq_lock;/* prevent simultaneous access */ +struct gpio_desc *ecreq;If I hadn't seen the declaration, I'd have no idea this was a GPIO descriptor. Please improve the nomenclature throughout.
Quoted 1 line
+ struct
i2c_client*self;Again, please use standard naming conventions:
Quoted 9 lines
% git grep "struct
i2c_client" | grep "\*self" | wc -l 0% git grep "struct
i2c_client" | grep "\*client" | wc -l 6304% git grep "struct
i2c_client" | grep "\*i2c" | wc -l 903ok, noted.
Quoted 2 lines
Quoted 1 line
+ const struct
asus_ec_chip_data*data;Quoted 8 lines
Quoted 6 lines
'data', 'priv' and 'info' should be improved.
Quoted 1 line
+ char ec_data[DOCKRAM_ENTRY_BUFSIZE];
An array of chars called 'data'. This could be anything.
Do you have a comprehensive list of name conventions you find suitable?
Anything descriptive that alludes to the type of data being held there.
There are 100's of good examples, but a handful of generic / bad ones.
Quoted 36 lines
Quoted 36 lines
Quoted 1 line
+ bool
logging_disabled;This debugging tool is probably never going to be used again.
Keep it local.
Quoted 15 lines
drivers/mfd/asus-transformer-ec.c new file +15
-0+}; + +struct dockram_ec_data { +struct mutex ctl_lock;/* prevent simultaneous access */ +char ctl_data[DOCKRAM_ENTRY_BUFSIZE ]; +}; + +#define to_ec_data (ec) \ + container_of(ec, struct asus_ec_data, info) + +/** +* asus_dockram_read - Read a register from the DockRAM device. +* @client: Handle to the DockRAM device. +* @reg: Register to read. +* @buf: Byte array into which data will be read; must be large enough to drivers/mfd/asus-transformer-ec.c line 63
Quoted 10 lines
drivers/mfd/asus-transformer-ec.c new file +10
-0+* hold the data returned by the DockRAM. +* +* This executes the DockRAM read based on the SMBus "block read" protocol +* or its emulation. It extracts DOCKRAM_ENTRY_SIZE bytes from the set +* register address. +* +* Returns a negative errno code else zero on success. +*/ +int asus_dockram_read (struct i2c_client *client,int reg,char *buf) +{Have you considered using Regmap for register access instead of implementing custom functions? Remaps already deals with caching and locking mechanisms that you'd get for free.
Quoted 8 lines
Quoted 5 lines
Quoted 5 lines
Quoted 2 lines
This looks like it would be replaced with
devm_regmap_init_i2c().I will consider this, thank you.
It seems that regmap does not fit for this purpose, but I might switch to plain
i2c_smbus_read_i2c_block_data
Can you explain why Regmap doesn't work for you?
Regmap is just a wrapper about i2c_smbus_read_i2c_block_data() and friends.
Quoted 27 lines
Quoted 27 lines
Quoted 21 lines
Quoted 11 lines
Quoted 11 lines
drivers/mfd/asus-transformer-ec.c new file +11
-0+struct device *dev = &client->dev; +int ret; + +memset (buf,0 ,DOCKRAM_ENTRY_BUFSIZE ); + ret =i2c_smbus_read_i2c_block_data (client, reg, +DOCKRAM_ENTRY_BUFSIZE , buf); +if (ret <0 ) +return ret; + +if (buf[0 ] >DOCKRAM_ENTRY_SIZE ) { + dev_err(dev, "bad data len; buffer: %*ph; ret: %d\n",Quoted 9 lines
drivers/mfd/asus-transformer-ec.c line 80
Quoted 6 lines
drivers/mfd/asus-transformer-ec.c new file +6
-0+ DOCKRAM_ENTRY_BUFSIZE, buf, ret); +return -EPROTO ; + } + +dev_dbg (dev,"got data; buffer: %*ph; ret: %d\n" , +DOCKRAM_ENTRY_BUFSIZE , buf, ret);Please remove all of these debug messages.
Quoted 1 line
Why debug messages cannot be preserved? They are specifically marked as
dev_dbgIt's a general convention.
After initial development, they tend to just litter the code-base.
Quoted 8 lines
Quoted 2 lines
Debug prints can be useful higher up the stack though.
I am fine with removing all debugs and logging but I strongly would like to keep EC model and firmware version along with susb and factory status. That may be quite useful in identifying EC used and its behavior without need in rebuilding the kernel and digging huge piles of downstream code in order to find how to dump these values.
Yes, you can keep this sort of thing as INFO.
Signature · 1 line
Lee Jones
Re: [PATCH v6 2/7] mfd: Add driver for ASUS Transformer embedded controller
пн, 18 трав. 2026 р. о 13:13 Lee Jones <[email protected]> пише:
Quoted 130 lines · Svyatoslav Ryhel
Quoted 128 lines
чт, 14 трав. 2026 р. о 18:50 Lee Jones <[email protected]> пише:
Quoted 125 lines · Svyatoslav Ryhel
Quoted 23 lines
чт, 14 трав. 2026 р. о 13:02 Lee Jones <[email protected]> пише:
Quoted 21 lines · Svyatoslav Ryhel
Quoted 19 lines
From: Michał Mirosław <[email protected]>
Support Nuvoton NPCE795-based ECs as used in Asus Transformer TF201, TF300T, TF300TG, TF300TL and TF700T pad and dock, as well as TF101 dock and TF600T, P1801-T and TF701T pad. This is a glue driver handling detection and common operations for EC's functions.
Co-developed-by Svyatoslav Ryhel <[email protected]>Signed-off-by Svyatoslav Ryhel <[email protected]>Signed-off-by Michał Mirosław <[email protected]>--- drivers/mfd/Kconfig | 14 + drivers/mfd/Makefile | 1 + drivers/mfd/asus-transformer-ec.c | 762 ++++++++++++++++++++++++ include/linux/mfd/asus-transformer-ec.h | 162 +++++ 4 files changed, 939 insertions(+) create mode 100644 drivers/mfd/asus-transformer-ec.c create mode 100644 include/linux/mfd/asus-transformer-ec.h[...]
Quoted 13 lines
Quoted 12 lines
drivers/mfd/asus-transformer-ec.c line 32
Quoted 6 lines
drivers/mfd/asus-transformer-ec.c new file +6
-0+unsigned int num_devices; +bool clr_fmode;/* clear Factory Mode bit in EC control register */ +}; + +struct asus_ec_data { +struct asusec_info info;You have 'data' and 'info' which a) using non-forthcoming nomenclature and doesn't tell me anything and then you b) put 'info' in the device's
driver_dataattribute which is very confusing.driver_datashould be for what we call ddata which I assume is expressed as 'data' here.Quoted 2 lines
asusec_infois shared among all child devices and is exposed while remaining elements of this struct are for internal use only.Our terminology for that is usually ddata, that gets stored in 'struct devices'
device_dataattribute.Quoted 24 lines
Quoted 10 lines
drivers/mfd/asus-transformer-ec.c line 34
Quoted 2 lines
drivers/mfd/asus-transformer-ec.c new file +2
-0+struct mutex ecreq_lock;/* prevent simultaneous access */ +struct gpio_desc *ecreq;If I hadn't seen the declaration, I'd have no idea this was a GPIO descriptor. Please improve the nomenclature throughout.
Quoted 1 line
+ struct
i2c_client*self;Again, please use standard naming conventions:
Quoted 9 lines
% git grep "struct
i2c_client" | grep "\*self" | wc -l 0% git grep "struct
i2c_client" | grep "\*client" | wc -l 6304% git grep "struct
i2c_client" | grep "\*i2c" | wc -l 903ok, noted.
Quoted 2 lines
Quoted 1 line
+ const struct
asus_ec_chip_data*data;Quoted 8 lines
Quoted 6 lines
'data', 'priv' and 'info' should be improved.
Quoted 1 line
+ char ec_data[DOCKRAM_ENTRY_BUFSIZE];
An array of chars called 'data'. This could be anything.
Do you have a comprehensive list of name conventions you find suitable?
Anything descriptive that alludes to the type of data being held there.
There are 100's of good examples, but a handful of generic / bad ones.
Quoted 41 lines
Quoted 36 lines
Quoted 1 line
+ bool
logging_disabled;This debugging tool is probably never going to be used again.
Keep it local.
Quoted 15 lines
drivers/mfd/asus-transformer-ec.c new file +15
-0+}; + +struct dockram_ec_data { +struct mutex ctl_lock;/* prevent simultaneous access */ +char ctl_data[DOCKRAM_ENTRY_BUFSIZE ]; +}; + +#define to_ec_data (ec) \ + container_of(ec, struct asus_ec_data, info) + +/** +* asus_dockram_read - Read a register from the DockRAM device. +* @client: Handle to the DockRAM device. +* @reg: Register to read. +* @buf: Byte array into which data will be read; must be large enough to drivers/mfd/asus-transformer-ec.c line 63
Quoted 10 lines
drivers/mfd/asus-transformer-ec.c new file +10
-0+* hold the data returned by the DockRAM. +* +* This executes the DockRAM read based on the SMBus "block read" protocol +* or its emulation. It extracts DOCKRAM_ENTRY_SIZE bytes from the set +* register address. +* +* Returns a negative errno code else zero on success. +*/ +int asus_dockram_read (struct i2c_client *client,int reg,char *buf) +{Have you considered using Regmap for register access instead of implementing custom functions? Remaps already deals with caching and locking mechanisms that you'd get for free.
Quoted 2 lines
This looks like it would be replaced with
devm_regmap_init_i2c().I will consider this, thank you.
Quoted 7 lines
Quoted 2 lines
It seems that regmap does not fit for this purpose, but I might switch to plain
i2c_smbus_read_i2c_block_dataCan you explain why Regmap doesn't work for you?
Regmap is just a wrapper about
i2c_smbus_read_i2c_block_data()and friends.
Honestly, it seems that this will cause more hassle. Regmap has max bit size of reg 64 which will require use of regmap_read/write_raw and obviously no regmap bit update operations will be possible. If this chip was a plain 8 bit register i2c device, obviously I would use regmap.
I have removed most of custom functions in favor for i2c smbus block helpers and left only asus_dockram_access_ctl since it is used in many places.
Quoted 30 lines
Quoted 30 lines
Quoted 29 lines
Quoted 21 lines
Quoted 11 lines
Quoted 11 lines
drivers/mfd/asus-transformer-ec.c new file +11
-0+struct device *dev = &client->dev; +int ret; + +memset (buf,0 ,DOCKRAM_ENTRY_BUFSIZE ); + ret =i2c_smbus_read_i2c_block_data (client, reg, +DOCKRAM_ENTRY_BUFSIZE , buf); +if (ret <0 ) +return ret; + +if (buf[0 ] >DOCKRAM_ENTRY_SIZE ) { + dev_err(dev, "bad data len; buffer: %*ph; ret: %d\n",Quoted 9 lines
drivers/mfd/asus-transformer-ec.c line 80
Quoted 6 lines
drivers/mfd/asus-transformer-ec.c new file +6
-0+ DOCKRAM_ENTRY_BUFSIZE, buf, ret); +return -EPROTO ; + } + +dev_dbg (dev,"got data; buffer: %*ph; ret: %d\n" , +DOCKRAM_ENTRY_BUFSIZE , buf, ret);Please remove all of these debug messages.
Quoted 1 line
Why debug messages cannot be preserved? They are specifically marked as
dev_dbgIt's a general convention.
After initial development, they tend to just litter the code-base.
Debug prints can be useful higher up the stack though.
Quoted 8 lines
Quoted 5 lines
I am fine with removing all debugs and logging but I strongly would like to keep EC model and firmware version along with susb and factory status. That may be quite useful in identifying EC used and its behavior without need in rebuilding the kernel and digging huge piles of downstream code in order to find how to dump these values.
Yes, you can keep this sort of thing as INFO.
Noted. I have send v7 with your suggestions applied. Change log is in the cover commit.
Quoted 2 lines
--
Lee Jones
Re: [PATCH v6 2/7] mfd: Add driver for ASUS Transformer embedded controller
In reply to Lee Jonesчт, 14 трав. 2026 р. о 13:02 Lee Jones <[email protected]> пише:
Quoted 180 lines · Svyatoslav Ryhel
Quoted 28 lines
From: Michał Mirosław <[email protected]>
Support Nuvoton NPCE795-based ECs as used in Asus Transformer TF201, TF300T, TF300TG, TF300TL and TF700T pad and dock, as well as TF101 dock and TF600T, P1801-T and TF701T pad. This is a glue driver handling detection and common operations for EC's functions.
Co-developed-by Svyatoslav Ryhel <[email protected]>Signed-off-by Svyatoslav Ryhel <[email protected]>Signed-off-by Michał Mirosław <[email protected]>--- drivers/mfd/Kconfig | 14 + drivers/mfd/Makefile | 1 + drivers/mfd/asus-transformer-ec.c | 762 ++++++++++++++++++++++++ include/linux/mfd/asus-transformer-ec.h | 162 +++++ 4 files changed, 939 insertions(+) create mode 100644 drivers/mfd/asus-transformer-ec.c create mode 100644 include/linux/mfd/asus-transformer-ec.hdrivers/mfd/Kconfig +0
-0@@ -137,6 +137,20 @@ config MFD_AAT2870_CORE additional drivers must be enabled in order to use the functionality of the device.drivers/mfd/Kconfig config MFD_AAT2870_CORE · line 149
Quoted 10 lines
drivers/mfd/Kconfig +10
-0+config MFD_ASUS_TRANSFORMER_EC + tristate "ASUS Transformer's embedded controller" + depends on I2C && OF + help + Support ECs found in ASUS Transformer's Pad and Mobile Dock. + + This provides shared glue for functional part drivers: + asus-transformer-ec-kbc, asus-transformer-ec-keys, + leds-asus-transformer-ec, asus-transformer-ec-battery + and asus-transformer-ec-charger.A 760 line driver deserves more than just a list of leaf drivers.
Quoted 24 lines
drivers/mfd/Kconfig +3
-0+ This driver can also be built as a module. If so, the module + will be called asus-transformer-ec. + config MFD_AT91_USART tristate "AT91 USART Driver" select MFD_COREdrivers/mfd/Makefile +1
-0@@ -10,6 +10,7 @@ obj-$(CONFIG_MFD_88PM805) += 88pm805.o 88pm80x.o obj-$(CONFIG_MFD_88PM886_PMIC) += 88pm886.o obj-$(CONFIG_MFD_ACT8945A) += act8945a.o obj-$(CONFIG_MFD_SM501) += sm501.o +obj-$(CONFIG_MFD_ASUS_TRANSFORMER_EC) += asus-transformer-ec.o obj-$(CONFIG_ARCH_BCM2835) += bcm2835-pm.o obj-$(CONFIG_MFD_BCM590XX) += bcm590xx.o obj-$(CONFIG_MFD_BD9571MWV) += bd9571mwv.odrivers/mfd/asus-transformer-ec.c new file +0
-0drivers/mfd/asus-transformer-ec.c line 7
Quoted 7 lines
drivers/mfd/asus-transformer-ec.c new file +7
-0@@ -0,0 +1,762 @@ +// SPDX-License-Identifier: GPL-2.0-or-later + +#include <linux/array_size.h> +#include <linux/debugfs.h> +#include <linux/delay.h> +#include <linux/gpio/consumer.h> +#include <linux/device.h> Should we sort the '#include' directives alphabetically? For instance, '
device.h' should typically appear before 'gpio/consumer.h'.Quoted 9 lines
drivers/mfd/asus-transformer-ec.c new file +9
-0+#include <linux/err.h> +#include <linux/i2c.h> +#include <linux/interrupt.h> +#include <linux/mfd/asus-transformer-ec.h> +#include <linux/mfd/core.h> +#include <linux/mod_devicetable.h> +#include <linux/module.h> +#include <linux/mutex.h> +#include <linux/property.h> drivers/mfd/asus-transformer-ec.c line 26
Quoted 10 lines
drivers/mfd/asus-transformer-ec.c new file +10
-0+#include <linux/slab.h> +#include <linux/string.h> +#include <linux/types.h> +#include <linux/unaligned.h> + +#define ASUSEC_RSP_BUFFER_SIZE 8 + +struct asus_ec_chip_data { +const char *name; +const struct mfd_cell *mfd_devices;Use global
static const structsinstead.Also, please do not pass MFD registration data through another registration API like DT. It opens the gates to too much hackery. I'm not saying that _this_ driver would do such a thing, but it's easier just to keep the blanket rule in place.
drivers/mfd/asus-transformer-ec.c line 32
Quoted 6 lines
drivers/mfd/asus-transformer-ec.c new file +6
-0+unsigned int num_devices; +bool clr_fmode;/* clear Factory Mode bit in EC control register */ +}; + +struct asus_ec_data { +struct asusec_info info;You have 'data' and 'info' which a) using non-forthcoming nomenclature and doesn't tell me anything and then you b) put 'info' in the device's
driver_dataattribute which is very confusing.driver_datashould be for what we call ddata which I assume is expressed as 'data' here.drivers/mfd/asus-transformer-ec.c line 34
Quoted 2 lines
drivers/mfd/asus-transformer-ec.c new file +2
-0+struct mutex ecreq_lock;/* prevent simultaneous access */ +struct gpio_desc *ecreq;If I hadn't seen the declaration, I'd have no idea this was a GPIO descriptor. Please improve the nomenclature throughout.
drivers/mfd/asus-transformer-ec.c line 35
Quoted 1 line
drivers/mfd/asus-transformer-ec.c new file +1
-0+struct i2c_client *self;Again, please use standard naming conventions:
% git grep "struct
i2c_client" | grep "\*self" | wc -l 0% git grep "struct
i2c_client" | grep "\*client" | wc -l 6304% git grep "struct
i2c_client" | grep "\*i2c" | wc -l 903drivers/mfd/asus-transformer-ec.c line 36
Quoted 1 line
drivers/mfd/asus-transformer-ec.c new file +1
-0+const struct asus_ec_chip_data *data;'data', 'priv' and 'info' should be improved.
drivers/mfd/asus-transformer-ec.c line 37
Quoted 1 line
drivers/mfd/asus-transformer-ec.c new file +1
-0+char ec_data[DOCKRAM_ENTRY_BUFSIZE ];An array of chars called 'data'. This could be anything.
drivers/mfd/asus-transformer-ec.c line 38
Quoted 1 line
drivers/mfd/asus-transformer-ec.c new file +1
-0+bool logging_disabled;This debugging tool is probably never going to be used again.
Keep it local.
Quoted 15 lines
drivers/mfd/asus-transformer-ec.c new file +15
-0+}; + +struct dockram_ec_data { +struct mutex ctl_lock;/* prevent simultaneous access */ +char ctl_data[DOCKRAM_ENTRY_BUFSIZE ]; +}; + +#define to_ec_data (ec) \ + container_of(ec, struct asus_ec_data, info) + +/** +* asus_dockram_read - Read a register from the DockRAM device. +* @client: Handle to the DockRAM device. +* @reg: Register to read. +* @buf: Byte array into which data will be read; must be large enough to drivers/mfd/asus-transformer-ec.c line 63
Quoted 10 lines
drivers/mfd/asus-transformer-ec.c new file +10
-0+* hold the data returned by the DockRAM. +* +* This executes the DockRAM read based on the SMBus "block read" protocol +* or its emulation. It extracts DOCKRAM_ENTRY_SIZE bytes from the set +* register address. +* +* Returns a negative errno code else zero on success. +*/ +int asus_dockram_read (struct i2c_client *client,int reg,char *buf) +{Have you considered using Regmap for register access instead of implementing custom functions? Remaps already deals with caching and locking mechanisms that you'd get for free.
Quoted 2 lines
This looks like it would be replaced with
devm_regmap_init_i2c().
I don't see any instances regmap handling 256 bit registers.
Quoted 923 lines
Quoted 7 lines
drivers/mfd/asus-transformer-ec.c new file +7
-0+struct device *dev = &client->dev; +int ret; + +memset (buf,0 ,DOCKRAM_ENTRY_BUFSIZE ); + ret =i2c_smbus_read_i2c_block_data (client, reg, +DOCKRAM_ENTRY_BUFSIZE , buf); +if (ret <0 )drivers/mfd/asus-transformer-ec.c line 80
Quoted 10 lines
drivers/mfd/asus-transformer-ec.c new file +10
-0+return ret; + +if (buf[0 ] >DOCKRAM_ENTRY_SIZE ) { +dev_err (dev,"bad data len; buffer: %*ph; ret: %d\n" , +DOCKRAM_ENTRY_BUFSIZE , buf, ret); +return -EPROTO ; + } + +dev_dbg (dev,"got data; buffer: %*ph; ret: %d\n" , +DOCKRAM_ENTRY_BUFSIZE , buf, ret);Please remove all of these debug messages.
Quoted 43 lines
drivers/mfd/asus-transformer-ec.c new file +43
-0+ +return 0 ; +} +EXPORT_SYMBOL_GPL (asus_dockram_read); + +/** +* asus_dockram_write - Write a byte array to a register of the DockRAM device. +* @client: Handle to the DockRAM device. +* @reg: Register to write to. +* @buf: Byte array to be written (up to DOCKRAM_ENTRY_SIZE bytes). +* +* This executes the DockRAM write based on the SMBus "block write" +* protocol or its emulation. It writes DOCKRAM_ENTRY_SIZE bytes to the +* specified register address. +* +* Returns a negative errno code else zero on success. +*/ +int asus_dockram_write (struct i2c_client *client,int reg,const char *buf) +{ +if (buf[0 ] >DOCKRAM_ENTRY_SIZE ) +return -EINVAL ; + +dev_dbg (&client->dev,"sending data; buffer: %*ph\n" , buf[0 ] +1 , buf); + +return i2c_smbus_write_i2c_block_data (client, reg, buf[0 ] +1 , buf); +} +EXPORT_SYMBOL_GPL (asus_dockram_write); + +/** +* asus_dockram_access_ctl - Read from or write to the DockRAM control register. +* @client: Handle to the DockRAM device. +* @out: Pointer to a variable where the register value will be stored. +* @mask: Bitmask of bits to be cleared. +* @xor: Bitmask of bits to be set (via XOR). +* +* This performs a control register read if @out is provided and both @mask +* and @xor are zero. Otherwise, it performs a control register update if +* @mask and @xor are provided. +* +* Returns a negative errno code else zero on success. +*/ +int asus_dockram_access_ctl (struct i2c_client *client,u64 *out,u64 mask, +u64 xor)drivers/mfd/asus-transformer-ec.c line 133
Quoted 10 lines
drivers/mfd/asus-transformer-ec.c new file +10
-0+{ +struct dockram_ec_data *priv =i2c_get_clientdata (client); +char *buf = priv->ctl_data; +u64 val; +int ret =0 ; + +guard (mutex)(&priv->ctl_lock); + + ret =asus_dockram_read (client,ASUSEC_DOCKRAM_CONTROL , buf); +if (ret <0 )Could we check for errors using 'if (ret)' instead of 'if (ret < 0)' here, unless a positive return value has a specific meaning we need to handle?
Quoted 38 lines
drivers/mfd/asus-transformer-ec.c new file +38
-0+ goto exit; + +if (buf[0 ] !=ASUSEC_CTL_SIZE ) { + ret = -EPROTO ; + goto exit; + } + + val =get_unaligned_le64 (buf +1 ); + +if (out) + *out = val; + +if (mask || xor) { +put_unaligned_le64 ((val & ~mask) ^ xor, buf +1 ); + ret =asus_dockram_write (client,ASUSEC_DOCKRAM_CONTROL , buf); + } + +exit: +if (ret <0 ) +dev_err (&client->dev,"Failed to access control flags: %d\n" , + ret); + +return ret; +} +EXPORT_SYMBOL_GPL (asus_dockram_access_ctl); + +static void asus_ec_remove_notifier (struct device *dev,void *res) +{ +struct asusec_info *ec =dev_get_drvdata (dev->parent); +struct notifier_block **nb = res; + +blocking_notifier_chain_unregister (&ec->notify_list, *nb); +} + +/** +* devm_asus_ec_register_notifier - Managed registration of notifier to an +* ASUS EC blocking notifier chain. +* @pdev: Device requesting the notifier (used for resource management). drivers/mfd/asus-transformer-ec.c line 181
Quoted 10 lines
drivers/mfd/asus-transformer-ec.c new file +10
-0+* @nb: Notifier block to be registered. +* +* Register a notifier to the ASUS EC blocking notifier chain. The notifier +* will be automatically unregistered when the requesting device is detached. +* +* Return: 0 on success or a negative error code on failure. +*/ +int devm_asus_ec_register_notifier (struct platform_device *pdev, +struct notifier_block *nb) +{Hand-rolling devres managed resources is usually reserved for subsystem level API calls. Why do the usual device driver .remove() handling work for you?
Quoted 49 lines
drivers/mfd/asus-transformer-ec.c new file +49
-0+struct asusec_info *ec =dev_get_drvdata (pdev->dev.parent); +struct notifier_block **res; +int ret; + + res =devres_alloc (asus_ec_remove_notifier,sizeof (*res),GFP_KERNEL ); +if (!res) +return -ENOMEM ; + + *res = nb; + ret =blocking_notifier_chain_register (&ec->notify_list, nb); +if (ret) { +devres_free (res); +return ret; + } + +devres_add (&pdev->dev, res); + +return 0 ; +} +EXPORT_SYMBOL_GPL (devm_asus_ec_register_notifier); + +static int asus_ec_signal_request (const struct asusec_info *ec) +{ +struct asus_ec_data *priv =to_ec_data (ec); + +guard (mutex)(&priv->ecreq_lock); + +dev_dbg (&priv->self->dev,"EC request\n" ); + +gpiod_set_value_cansleep (priv->ecreq,1 ); +msleep (50 ); + +gpiod_set_value_cansleep (priv->ecreq,0 ); +msleep (200 ); + +return 0 ; +} + +static int asus_ec_write (struct asus_ec_data *priv,u16 data) +{ +int ret =i2c_smbus_write_word_data (priv->self,ASUSEC_WRITE_BUF , data); + +dev_dbg (&priv->self->dev,"EC write: %04x, ret = %d\n" , data, ret); + +return ret; +} + +static int asus_ec_read (struct asus_ec_data *priv,bool in_irq) +{drivers/mfd/asus-transformer-ec.c line 240
Quoted 10 lines
drivers/mfd/asus-transformer-ec.c new file +10
-0+int ret =i2c_smbus_read_i2c_block_data (priv->self,ASUSEC_READ_BUF , +sizeof (priv->ec_data), + priv->ec_data); + +dev_dbg (&priv->self->dev,"EC read: %*ph, ret = %d%s\n" , +sizeof (priv->ec_data), priv->ec_data, + ret, in_irq ?"; in irq" :"" ); + +return ret; +}No abstractions for the sake of it. All this goes away with Regmap anyway.
drivers/mfd/asus-transformer-ec.c line 252
Quoted 12 lines
drivers/mfd/asus-transformer-ec.c new file +12
-0+ +/** +* asus_ec_i2c_command - Send a 16-bit command to the ASUS EC. +* @ec: Pointer to the shared ASUS EC structure. +* @data: The 16-bit command (word) to be sent. +* +* Return: 0 on success or a negative error code on failure. +*/ +int asus_ec_i2c_command (const struct asusec_info *ec,u16 data) +{ +return asus_ec_write (to_ec_data (ec), data); +}Is this wrapper function strictly necessary? We should try to avoid superfluous abstractions that do nothing but call another function.
Quoted 250 lines
drivers/mfd/asus-transformer-ec.c new file +250
-0+EXPORT_SYMBOL_GPL (asus_ec_i2c_command); + +static void asus_ec_clear_buffer (struct asus_ec_data *priv) +{ +int retry =ASUSEC_RSP_BUFFER_SIZE ; + +while (retry--) { +if (asus_ec_read (priv, false) <0 ) +continue ; + +if (priv->ec_data[1 ] &ASUSEC_OBF_MASK ) +continue ; + +break ; + } +} + +static int asus_ec_log_info (struct asus_ec_data *priv,unsigned int reg, +const char *name,char **out) +{ +char buf[DOCKRAM_ENTRY_BUFSIZE ]; +int ret; + + ret =asus_dockram_read (priv->info.dockram, reg, buf); +if (ret <0 ) +return ret; + +if (!priv->logging_disabled) +dev_info (&priv->self->dev,"%-14s: %.*s\n" , name, + buf[0 ], buf +1 ); + +if (out) + *out =kstrndup (buf +1 , buf[0 ],GFP_KERNEL ); + +return 0 ; +} + +static int asus_ec_reset (struct asus_ec_data *priv) +{ +int retry, ret; + +for (retry =0 ; retry <3 ; retry++) { + ret =asus_ec_write (priv,0 ); +if (!ret) +return 0 ; + +msleep (300 ); + } + +return ret; +} + +static int asus_ec_magic_debug (struct asus_ec_data *priv) +{ +u64 flag; +int ret; + + ret =asus_ec_get_ctl (&priv->info, &flag); +if (ret <0 ) +return ret; + + flag &=ASUSEC_CTL_SUSB_MODE ; +dev_info (&priv->self->dev,"EC FW behaviour: %s\n" , + flag ?"susb on when receive ec_req" : +"susb on when system wakeup" ); + +return 0 ; +} + +static int asus_ec_set_factory_mode (struct asus_ec_data *priv,bool on) +{ +dev_info (&priv->self->dev,"Entering %s mode.\n" , on ?"factory" : +"normal" ); + +return asus_ec_update_ctl (&priv->info,ASUSEC_CTL_FACTORY_MODE , + on ?ASUSEC_CTL_FACTORY_MODE :0 ); +} + +static int asus_ec_detect (struct asus_ec_data *priv) +{ +char *model =NULL ; +int ret; + + ret =asus_ec_reset (priv); +if (ret) + goto err_exit; + +asus_ec_clear_buffer (priv); + + ret =asus_ec_log_info (priv,ASUSEC_DOCKRAM_INFO_MODEL ,"model" , &model); +if (ret) + goto err_exit; + + ret =asus_ec_log_info (priv,ASUSEC_DOCKRAM_INFO_FW ,"FW version" ,NULL ); +if (ret) + goto err_exit; + + ret =asus_ec_log_info (priv,ASUSEC_DOCKRAM_INFO_CFGFMT ,"Config format" ,NULL ); +if (ret) + goto err_exit; + + ret =asus_ec_log_info (priv,ASUSEC_DOCKRAM_INFO_HW ,"HW version" ,NULL ); +if (ret) + goto err_exit; + + priv->logging_disabled = true; + + ret =asus_ec_magic_debug (priv); +if (ret) + goto err_exit; + + priv->info.model = model; + priv->info.name = priv->data->name; + +if (priv->data->clr_fmode) +asus_ec_set_factory_mode (priv, false); + +err_exit: +if (ret) +dev_err (&priv->self->dev,"failed to access EC: %d\n" , ret); + +return ret; +} + +static void asus_ec_handle_smi (struct asus_ec_data *priv,unsigned int code) +{ +dev_dbg (&priv->self->dev,"SMI interrupt: 0x%02x\n" , code); + +switch (code) { +case ASUSEC_SMI_HANDSHAKE : +case ASUSEC_SMI_RESET : +asus_ec_detect (priv); +break ; + } +} + +static irqreturn_t asus_ec_interrupt (int irq,void *dev_id) +{ +struct asus_ec_data *priv = dev_id; +unsigned long notify_action; +int ret; + + ret =asus_ec_read (priv, true); +if (ret <=0 || !(priv->ec_data[1 ] &ASUSEC_OBF_MASK )) +return IRQ_NONE ; + + notify_action = priv->ec_data[1 ]; +if (notify_action &ASUSEC_SMI_MASK ) { +unsigned int code = priv->ec_data[2 ]; + +asus_ec_handle_smi (priv, code); + + notify_action |= code <<8 ; +dev_dbg (&priv->self->dev,"SMI code: 0x%02x\n" , code); + } + +blocking_notifier_call_chain (&priv->info.notify_list, + notify_action, priv->ec_data); + +return IRQ_HANDLED ; +} + +static ssize_t dockram_read (struct file *filp,char __user *buf, +size_t count,loff_t *ppos) +{ +struct i2c_client *client = filp->private_data; +unsigned int reg, rsize; +ssize_t n_read =0 , val; +loff_t off = *ppos; +char *data; +int ret; + + reg = off /DOCKRAM_ENTRY_SIZE ; + off %=DOCKRAM_ENTRY_SIZE ; + rsize =DOCKRAM_ENTRIES *DOCKRAM_ENTRY_SIZE ; + +if (!count) +return 0 ; + + data =kmalloc (DOCKRAM_ENTRY_BUFSIZE ,GFP_KERNEL ); + +while (reg <DOCKRAM_ENTRIES ) { +unsigned int len =DOCKRAM_ENTRY_SIZE - off; + +if (len > rsize) + len = rsize; + + ret =asus_dockram_read (client, reg, data); +if (ret <0 ) { +if (!n_read) + n_read = ret; +break ; + } + + val =copy_to_user (buf, data +1 + off, len); +if (val == len) +return -EFAULT ; + + *ppos += len; + n_read += len; + +if (len == rsize) +break ; + + rsize -= len; + buf += len; + off =0 ; + ++reg; + } + +kfree (data); + +return n_read; +} + +static int dockram_write_one (struct i2c_client *client,int reg, +const char __user *buf,size_t count) +{ +struct dockram_ec_data *priv =i2c_get_clientdata (client); +int ret; + +if (!count || count >DOCKRAM_ENTRY_SIZE ) +return -EINVAL ; +if (buf[0 ] != count -1 ) +return -EINVAL ; + +guard (mutex)(&priv->ctl_lock); + + priv->ctl_data[0 ] = (u8 )count; +memcpy (priv->ctl_data +1 , buf, count); + ret =asus_dockram_write (client, reg, priv->ctl_data); + +return ret; +} + +static ssize_t dockram_write (struct file *filp,const char __user *buf, +size_t count,loff_t *ppos) +{ +struct i2c_client *client = filp->private_data; +unsigned int reg; +loff_t off = *ppos; +int ret; + +if (off %DOCKRAM_ENTRY_SIZE !=0 ) +return -EINVAL ; + + reg = off /DOCKRAM_ENTRY_SIZE ; +if (reg >=DOCKRAM_ENTRIES ) +return -EINVAL ; +drivers/mfd/asus-transformer-ec.c line 512
Quoted 10 lines
drivers/mfd/asus-transformer-ec.c new file +10
-0+ ret =dockram_write_one (client, reg, buf, count); + +return ret <0 ? ret : count; +} + +static const struct debugfs_short_fops dockram_fops = { + .read = dockram_read, + .write = dockram_write, + .llseek = default_llseek, +};You should not be giving userspace free reign over device memory.
If you switch to Regmap, you can use
regmap-debugfs.cfor this.Quoted 52 lines
drivers/mfd/asus-transformer-ec.c new file +52
-0+static int control_reg_get (void *ec,u64 *val) +{ +return asus_ec_get_ctl (ec, val); +} + +static int control_reg_set (void *ec,u64 val) +{ +return asus_ec_update_ctl (ec, ~0ull , val); +} + +DEFINE_DEBUGFS_ATTRIBUTE (control_reg_fops, control_reg_get, + control_reg_set,"%016llx\n" ); + +static int ec_request_set (void *ec,u64 val) +{ +if (val) +asus_ec_signal_request (ec); + +return 0 ; +} + +DEFINE_DEBUGFS_ATTRIBUTE (ec_request_fops,NULL , ec_request_set,"%llu\n" ); + +static int ec_irq_set (void *ec,u64 val) +{ +struct asus_ec_data *priv =to_ec_data (ec); + +if (val) +irq_wake_thread (priv->self->irq, priv); + +return 0 ; +} + +DEFINE_DEBUGFS_ATTRIBUTE (ec_irq_fops,NULL , ec_irq_set,"%llu\n" ); + +static void asus_ec_debugfs_remove (void *debugfs_root) +{ +debugfs_remove_recursive (debugfs_root); +} + +static void devm_asus_ec_debugfs_init (struct device *dev) +{ +struct asusec_info *ec =dev_get_drvdata (dev); +struct asus_ec_data *priv =to_ec_data (ec); +struct dentry *debugfs_root, *dockram_dir; +char *name =devm_kasprintf (dev,GFP_KERNEL ,"asus-ec-%s" , + priv->data->name); + + debugfs_root =debugfs_create_dir (name,NULL ); + dockram_dir =debugfs_create_dir ("dockram" , debugfs_root); + + debugfs_create_file("ec_irq", 0200, debugfs_root, ec,drivers/mfd/asus-transformer-ec.c line 575
Quoted 10 lines
drivers/mfd/asus-transformer-ec.c new file +10
-0+ &ec_irq_fops); +debugfs_create_file ("ec_request" ,0200 , debugfs_root, ec, + &ec_request_fops); +debugfs_create_file ("control_reg" ,0644 , dockram_dir, ec, + &control_reg_fops); +debugfs_create_file ("dockram" ,0644 , dockram_dir, + priv->info.dockram, &dockram_fops); + +devm_add_action_or_reset (dev, asus_ec_debugfs_remove, debugfs_root); +}Why is this being controlled via debugfs?
Use the correct kernel APIs instead.
If this is just for development, keep it and all of the extra verbose printing in the BSP tree. It does not belong in the upstream kernel.
Quoted 26 lines
drivers/mfd/asus-transformer-ec.c new file +26
-0+static void asus_ec_release_dockram_dev (void *client) +{ +i2c_unregister_device (client); +} + +static struct i2c_client *devm_asus_dockram_get (struct device *dev) +{ +struct i2c_client *parent =to_i2c_client (dev); +struct i2c_client *dockram; +struct dockram_ec_data *priv; +int ret; + + dockram =i2c_new_ancillary_device (parent,"dockram" , + parent->addr +2 ); +if (IS_ERR (dockram)) +return dockram; + + ret =devm_add_action_or_reset (dev, asus_ec_release_dockram_dev, + dockram); +if (ret) +return ERR_PTR (ret); + + priv =devm_kzalloc (&dockram->dev,sizeof (*priv),GFP_KERNEL ); +if (!priv) +return ERR_PTR (-ENOMEM ); +drivers/mfd/asus-transformer-ec.c line 612
Quoted 10 lines
drivers/mfd/asus-transformer-ec.c new file +10
-0+i2c_set_clientdata (dockram, priv); +mutex_init (&priv->ctl_lock); + +return dockram; +} + +static int asus_ec_probe (struct i2c_client *client) +{ +struct device *dev = &client->dev; +struct asus_ec_data *priv;Could we use a clearer, more specific name for the private data variable instead of the generic term 'priv'? Using something like 'ddata' is usually preferred.
Quoted 16 lines
drivers/mfd/asus-transformer-ec.c new file +16
-0+int ret; + +if (!i2c_check_functionality (client->adapter,I2C_FUNC_SMBUS_I2C_BLOCK )) +return dev_err_probe (dev, -ENXIO , +"I2C bus is missing required SMBus block mode support\n" ); + + priv =devm_kzalloc (dev,sizeof (*priv),GFP_KERNEL ); +if (!priv) +return -ENOMEM ; + + priv->data =device_get_match_data (dev); +if (!priv->data) +return -ENODEV ; + +i2c_set_clientdata (client, priv); + priv->self = client;drivers/mfd/asus-transformer-ec.c line 638
Quoted 10 lines
drivers/mfd/asus-transformer-ec.c new file +10
-0+ + priv->info.dockram =devm_asus_dockram_get (dev); +if (IS_ERR (priv->info.dockram)) +return dev_err_probe (dev,PTR_ERR (priv->info.dockram), +"failed to get dockram\n" ); + + priv->ecreq =devm_gpiod_get (dev,"request" ,GPIOD_OUT_LOW ); +if (IS_ERR (priv->ecreq)) +return dev_err_probe (dev,PTR_ERR (priv->ecreq), +"failed to get request GPIO\n" );"get" or "request"
Quoted 34 lines
drivers/mfd/asus-transformer-ec.c new file +34
-0+BLOCKING_INIT_NOTIFIER_HEAD (&priv->info.notify_list); +mutex_init (&priv->ecreq_lock); + +asus_ec_signal_request (&priv->info); + + ret =asus_ec_detect (priv); +if (ret) +return dev_err_probe (dev, ret,"failed to detect EC version\n" ); + + ret =devm_request_threaded_irq (dev, client->irq,NULL , + &asus_ec_interrupt, +IRQF_ONESHOT |IRQF_SHARED , + client->name, priv); +if (ret) +return dev_err_probe (dev, ret,"failed to register IRQ\n" ); + +/* Parent I2C controller uses DMA, ASUS EC and child devices do not */ + client->dev.coherent_dma_mask =0 ; + client->dev.dma_mask = &client->dev.coherent_dma_mask; + +if (IS_ENABLED (CONFIG_DEBUG_FS )) +devm_asus_ec_debugfs_init (dev); + +return devm_mfd_add_devices (dev,0 , priv->data->mfd_devices, + priv->data->num_devices,NULL ,0 ,NULL ); +} + +static const struct mfd_cell asus_ec_sl101_dock_mfd_devices[] = { + { + .name ="asus-transformer-ec-kbc" , + }, +}; + +static const struct asus_ec_chip_data asus_ec_sl101_dock_data = {drivers/mfd/asus-transformer-ec.c line 683
Quoted 10 lines
drivers/mfd/asus-transformer-ec.c new file +10
-0+ .name = "dock", + .mfd_devices = asus_ec_sl101_dock_mfd_devices, + .num_devices = ARRAY_SIZE(asus_ec_sl101_dock_mfd_devices), + .clr_fmode = false, +}; + +static const struct mfd_cell asus_ec_tf101_dock_mfd_devices[] = { + { + .name ="asus-transformer-ec-battery" , + .id =1 ,Why doesn't
PLATFORM_DEVID_AUTOalready do the right thing?Quoted 50 lines
drivers/mfd/asus-transformer-ec.c new file +50
-0+ }, { + .name = "asus-transformer-ec-charger", + .id = 1, + }, { + .name = "asus-transformer-ec-led", + .id = 1, + }, { + .name = "asus-transformer-ec-keys", + }, { + .name = "asus-transformer-ec-kbc", + }, +}; + +static const struct asus_ec_chip_data asus_ec_tf101_dock_data = { + .name = "dock", + .mfd_devices = asus_ec_tf101_dock_mfd_devices, + .num_devices = ARRAY_SIZE(asus_ec_tf101_dock_mfd_devices), + .clr_fmode = false, +}; + +static const struct mfd_cell asus_ec_tf201_pad_mfd_devices[] = { + { + .name = "asus-transformer-ec-battery", + .id = 0, + }, { + .name = "asus-transformer-ec-led", + .id = 0, + }, +}; + +static const struct asus_ec_chip_data asus_ec_tf201_pad_data = { + .name = "pad", + .mfd_devices = asus_ec_tf201_pad_mfd_devices, + .num_devices = ARRAY_SIZE(asus_ec_tf201_pad_mfd_devices), + .clr_fmode = true, +}; + +static const struct mfd_cell asus_ec_tf600t_pad_mfd_devices[] = { + { + .name = "asus-transformer-ec-battery", + .id = 0, + }, { + .name = "asus-transformer-ec-charger", + .id = 0, + }, { + .name = "asus-transformer-ec-led", + .id = 0, + }, +}; +drivers/mfd/asus-transformer-ec.c line 743
Quoted 10 lines
drivers/mfd/asus-transformer-ec.c new file +10
-0+static const struct asus_ec_chip_data asus_ec_tf600t_pad_data = { + .name ="pad" , + .mfd_devices = asus_ec_tf600t_pad_mfd_devices, + .num_devices =ARRAY_SIZE (asus_ec_tf600t_pad_mfd_devices), + .clr_fmode = true, +}; + +static const struct of_device_id asus_ec_match[] = { + { .compatible ="asus,sl101-ec-dock" , .data = &asus_ec_sl101_dock_data }, + { .compatible ="asus,tf101-ec-dock" , .data = &asus_ec_tf101_dock_data },Could we use the match table's data field to store an 'enum' or integer ID instead of passing platform data via the '.data' field? We could then use a 'switch' statement in the C code to select the correct '
mfd_cell' array.Quoted 113 lines
drivers/mfd/asus-transformer-ec.c new file +19
-0+ { .compatible = "asus,tf201-ec-pad", .data = &asus_ec_tf201_pad_data }, + { .compatible = "asus,tf600t-ec-pad", .data = &asus_ec_tf600t_pad_data }, + { } +}; +MODULE_DEVICE_TABLE (of, asus_ec_match); + +static struct i2c_driver asus_ec_driver = { + .driver = { + .name ="asus-transformer-ec" , + .of_match_table = asus_ec_match, + }, + .probe = asus_ec_probe, +}; +module_i2c_driver (asus_ec_driver); + +MODULE_AUTHOR ("Michał Mirosław <[email protected]>" ); +MODULE_AUTHOR ("Svyatoslav Ryhel <[email protected]>" ); +MODULE_DESCRIPTION ("ASUS Transformer's EC driver" ); +MODULE_LICENSE ("GPL" );include/linux/mfd/asus-transformer-ec.h new file +88
-0@@ -0,0 +1,162 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef __MFD_ASUS_TRANSFORMER_EC_H +#define __MFD_ASUS_TRANSFORMER_EC_H + +#include <linux/notifier.h> +#include <linux/platform_device.h> +#include <linux/workqueue.h> + +struct i2c_client ; + +struct asusec_info { +const char *model; +const char *name; +struct i2c_client *dockram; +struct workqueue_struct *wq; +struct blocking_notifier_head notify_list; +}; + +#define DOCKRAM_ENTRIES 0x100 +#define DOCKRAM_ENTRY_SIZE 32 +#define DOCKRAM_ENTRY_BUFSIZE (DOCKRAM_ENTRY_SIZE + 1) + +/* interrupt sources */ +#define ASUSEC_OBF_MASK BIT(0) +#define ASUSEC_KEY_MASK BIT(2) +#define ASUSEC_KBC_MASK BIT(3) +#define ASUSEC_AUX_MASK BIT(5) +#define ASUSEC_SCI_MASK BIT(6) +#define ASUSEC_SMI_MASK BIT(7) + +/* SMI notification codes */ +#define ASUSEC_SMI_POWER_NOTIFY 0x31/* [un]plugging USB cable */ +#define ASUSEC_SMI_HANDSHAKE 0x50/* response to ec_req edge */ +#define ASUSEC_SMI_WAKE 0x53 +#define ASUSEC_SMI_RESET 0x5f +#define ASUSEC_SMI_ADAPTER_EVENT 0x60/* [un]plugging charger to dock */ +#define ASUSEC_SMI_BACKLIGHT_ON 0x63 +#define ASUSEC_SMI_AUDIO_DOCK_IN 0x70 + +#define ASUSEC_SMI_ACTION (code) (ASUSEC_SMI_MASK | ASUSEC_OBF_MASK | \ + (ASUSEC_SMI_##code << 8)) + +/* control register [0x0a] layout */ +#define ASUSEC_CTL_SIZE 8 + +/* +* EC reports power from 40-pin connector in the LSB of the control +* register. The following values have been observed (xor 0x02): +* +* PAD-ec no-plug 0x40 / PAD-ec DOCK 0x20 / DOCK-ec no-plug 0x40 +* PAD-ec AC 0x25 / PAD-ec DOCK+AC 0x24 / DOCK-ec AC 0x25 +* PAD-ec USB 0x45 / PAD-ec DOCK+USB 0x24 / DOCK-ec USB 0x41 +*/ + +#define ASUSEC_CTL_DIRECT_POWER_SOURCE BIT_ULL(0) +#define ASUSEC_STAT_CHARGING BIT_ULL(2) +#define ASUSEC_CTL_FULL_POWER_SOURCE BIT_ULL(5) +#define ASUSEC_CTL_SUSB_MODE BIT_ULL(9) +#define ASUSEC_CMD_SUSPEND_S3 BIT_ULL(33) +#define ASUSEC_CTL_TEST_DISCHARGE BIT_ULL(35) +#define ASUSEC_CMD_SUSPEND_INHIBIT BIT_ULL(37) +#define ASUSEC_CTL_FACTORY_MODE BIT_ULL(38) +#define ASUSEC_CTL_KEEP_AWAKE BIT_ULL(39) +#define ASUSEC_CTL_USB_CHARGE BIT_ULL(40) +#define ASUSEC_CTL_LED_BLINK BIT_ULL(40) +#define ASUSEC_CTL_LED_AMBER BIT_ULL(41) +#define ASUSEC_CTL_LED_GREEN BIT_ULL(42) +#define ASUSEC_CMD_SWITCH_HDMI BIT_ULL(56) +#define ASUSEC_CMD_WIN_SHUTDOWN BIT_ULL(62) + +#define ASUSEC_DOCKRAM_INFO_MODEL 0x01 +#define ASUSEC_DOCKRAM_INFO_FW 0x02 +#define ASUSEC_DOCKRAM_INFO_CFGFMT 0x03 +#define ASUSEC_DOCKRAM_INFO_HW 0x04 +#define ASUSEC_DOCKRAM_CONTROL 0x0a +#define ASUSEC_DOCKRAM_BATT_CTL 0x14 + +#define ASUSEC_WRITE_BUF 0x64 +#define ASUSEC_READ_BUF 0x6a + +/* dockram comm */ +int asus_dockram_read (struct i2c_client *client,int reg,char *buf); +int asus_dockram_write (struct i2c_client *client,int reg,const char *buf); +int asus_dockram_access_ctl (struct i2c_client *client, +u64 *out,u64 mask,u64 xor); + +/* EC public API */ +include/linux/mfd/asus-transformer-ec.h line 98
Quoted 10 lines
include/linux/mfd/asus-transformer-ec.h new file +10
-0+/** +* cell_to_ec - Request the shared ASUS EC structure via a subdevice's pdev. +* @pdev: EC subdevice pdev requesting access to the shared ASUS EC structure. +* +* Returns a pointer to the asusec_info structure. +*/ +static inline struct asusec_info *cell_to_ec (struct platform_device *pdev) +{ +return dev_get_drvdata (pdev->dev.parent); +}This is both misleading and already exists.
Quoted 49 lines
include/linux/mfd/asus-transformer-ec.h new file +49
-0+ +/** +* asus_ec_get_ctl - Read from the DockRAM control register. +* @ec: Pointer to the shared ASUS EC structure. +* @out: Pointer to the variable where the register value will be stored. +* +* Performs a control register read and stores the value in @out. +* +* Return: 0 on success, or a negative errno code on failure. +*/ +static inline int asus_ec_get_ctl (const struct asusec_info *ec,u64 *out) +{ +return asus_dockram_access_ctl (ec->dockram, out,0 ,0 ); +} + +/** +* asus_ec_update_ctl - Update the DockRAM control register. +* @ec: Pointer to the shared ASUS EC structure. +* @mask: Bitmask of bits to be cleared. +* @xor: Bitmask of bits to be toggled or set (via XOR). +* +* Performs a read-modify-write update on the control register using +* the provided @mask and @xor values. +* +* Return: 0 on success, or a negative errno code on failure. +*/ +static inline int asus_ec_update_ctl (const struct asusec_info *ec, +u64 mask,u64 xor) +{ +return asus_dockram_access_ctl (ec->dockram,NULL , mask, xor); +} + +/** +* asus_ec_set_ctl_bits - Sets bits of the DockRAM control register. +* @ec: Pointer to the shared ASUS EC structure. +* @mask: Bitmask of bits to be set. +* +* Sets bits of the control register using the provided @mask value. +* +* Return: 0 on success, or a negative errno code on failure. +*/ +static inline int asus_ec_set_ctl_bits (const struct asusec_info *ec,u64 mask) +{ +return asus_dockram_access_ctl (ec->dockram,NULL , mask, mask); +} + +/** +* asus_ec_clear_ctl_bits - Clears bits of the DockRAM control register. +* @ec: Pointer to the shared ASUS EC structure. include/linux/mfd/asus-transformer-ec.h line 157
Quoted 10 lines
include/linux/mfd/asus-transformer-ec.h new file +10
-0+* @mask: Bitmask of bits to be cleared. +* +* Clears bits of the control register using the provided @mask value. +* +* Return: 0 on success, or a negative errno code on failure. +*/ +static inline int asus_ec_clear_ctl_bits (const struct asusec_info *ec,u64 mask) +{ +return asus_dockram_access_ctl (ec->dockram,NULL , mask,0 ); +}We don't typically allow abstraction for the sake of abstraction.
Quoted 8 lines
include/linux/mfd/asus-transformer-ec.h new file +4
-1+int asus_ec_i2c_command (const struct asusec_info *ec,u16 data); +int devm_asus_ec_register_notifier (struct platform_device *dev, +struct notifier_block *nb); +#endif /* __MFD_ASUS_TRANSFORMER_EC_H */ --2.51.0
--
Lee Jones
PATCH v6 3/7 input: serio: Add driver for ASUS Transformer dock keyboard and touchpad
In reply to Svyatoslav RyhelFrom: Michał Mirosław <[email protected]>
Add input driver for ASUS Transformer dock keyboard and touchpad.
Some keys in ASUS Dock report keycodes that don't make sense according to their position, this patch modifies the incoming data that is sent to serio to send proper scancodes.
--- drivers/input/serio/Kconfig | 15 ++ drivers/input/serio/Makefile | 1 + drivers/input/serio/asus-transformer-ec-kbc.c | 147 ++++++++++++++++++ 3 files changed, 163 insertions(+) create mode 100644 drivers/input/serio/asus-transformer-ec-kbc.c
drivers/input/serio/Kconfig +15 -0
@@ -84,6 +84,21 @@ config SERIO_RPCKBD To compile this driver as a module, choose M here: the module will be called rpckbd. +config SERIO_ASUS_TRANSFORMER_EC + tristate "Asus Transformer's Dock keyboard and touchpad controller" + depends on MFD_ASUS_TRANSFORMER_EC + help + Say Y here if you want to use the keyboard and/or touchpad on + Asus Transformed's Mobile Dock. + + For keyboard support you also need atkbd driver. + + For touchpad support you also need psmouse driver with Elantech + touchpad option enabled. + + To compile this driver as a module, choose M here: the module will + be called asus-transformer-ec-kbc. + config SERIO_AMBAKMI tristate "AMBA KMI keyboard controller" depends on ARM_AMBA
drivers/input/serio/Makefile +1 -0
@@ -12,6 +12,7 @@ obj-$(CONFIG_SERIO_SERPORT) += serport.o obj-$(CONFIG_SERIO_RPCKBD) += rpckbd.o obj-$(CONFIG_SERIO_SA1111) += sa1111ps2.o obj-$(CONFIG_SERIO_AMBAKMI) += ambakmi.o +obj-$(CONFIG_SERIO_ASUS_TRANSFORMER_EC) += asus-transformer-ec-kbc.o obj-$(CONFIG_SERIO_Q40KBD) += q40kbd.o obj-$(CONFIG_SERIO_GSCPS2) += gscps2.o obj-$(CONFIG_HP_SDC) += hp_sdc.o
drivers/input/serio/asus-transformer-ec-kbc.c new file +147 -0
147 lines not on this page · raw message
Signature · 1 line
2.51.0
PATCH v6 4/7 input: keyboard: Add driver for ASUS Transformer dock multimedia keys
In reply to Svyatoslav RyhelFrom: Michał Mirosław <[email protected]>
Add support for multimedia top button row of ASUS Transformer's Mobile Dock keyboard. Driver is made that function keys (F1-F12) are used by default which suits average Linux use better and with pressing ScreenLock + AltGr function keys layout is switched to multimedia keys. Since this only modifies codes sent by asus-ec-keys it doesn't affect normal keyboards at all.
--- drivers/input/keyboard/Kconfig | 10 + drivers/input/keyboard/Makefile | 1 + .../input/keyboard/asus-transformer-ec-keys.c | 272 ++++++++++++++++++ 3 files changed, 283 insertions(+) create mode 100644 drivers/input/keyboard/asus-transformer-ec-keys.c
drivers/input/keyboard/Kconfig +10 -0
@@ -89,6 +89,16 @@ config KEYBOARD_APPLESPI To compile this driver as a module, choose M here: the module will be called applespi. +config KEYBOARD_ASUS_TRANSFORMER_EC + tristate "Asus Transformer's Mobile Dock multimedia keys" + depends on MFD_ASUS_TRANSFORMER_EC + help + Say Y here if you want to use multimedia keys present on Asus + Transformer's Mobile Dock. + + To compile this driver as a module, choose M here: the + module will be called asus-transformer-ec-keys. + config KEYBOARD_ATARI tristate "Atari keyboard" depends on ATARI
drivers/input/keyboard/Makefile +1 -0
@@ -11,6 +11,7 @@ obj-$(CONFIG_KEYBOARD_ADP5585) += adp5585-keys.o obj-$(CONFIG_KEYBOARD_ADP5588) += adp5588-keys.o obj-$(CONFIG_KEYBOARD_AMIGA) += amikbd.o obj-$(CONFIG_KEYBOARD_APPLESPI) += applespi.o +obj-$(CONFIG_KEYBOARD_ASUS_TRANSFORMER_EC) += asus-transformer-ec-keys.o obj-$(CONFIG_KEYBOARD_ATARI) += atakbd.o obj-$(CONFIG_KEYBOARD_ATKBD) += atkbd.o obj-$(CONFIG_KEYBOARD_BCM) += bcm-keypad.o
drivers/input/keyboard/asus-transformer-ec-keys.c new file +272 -0
272 lines not on this page · raw message
Signature · 1 line
2.51.0
PATCH v6 5/7 leds: Add driver for ASUS Transformer LEDs
In reply to Svyatoslav RyhelFrom: Michał Mirosław <[email protected]>
ASUS Transformer tablets have a green and an amber LED on both the Pad and the Dock. If both LEDs are enabled simultaneously, the emitted light will be yellow.
--- drivers/leds/Kconfig | 11 ++++ drivers/leds/Makefile | 1 + drivers/leds/leds-asus-transformer-ec.c | 79 +++++++++++++++++++++++++ 3 files changed, 91 insertions(+) create mode 100644 drivers/leds/leds-asus-transformer-ec.c
drivers/leds/Kconfig +11 -0
@@ -120,6 +120,17 @@ config LEDS_OSRAM_AMS_AS3668 To compile this driver as a module, choose M here: the module will be called leds-as3668. +config LEDS_ASUS_TRANSFORMER_EC + tristate "LED Support for Asus Transformer charging LED" + depends on LEDS_CLASS + depends on MFD_ASUS_TRANSFORMER_EC + help + This option enables support for charging indicator on + Asus Transformer's Pad and it's Dock. + + To compile this driver as a module, choose M here: the module + will be called leds-asus-transformer-ec. + config LEDS_AW200XX tristate "LED support for Awinic AW20036/AW20054/AW20072/AW20108" depends on LEDS_CLASS
drivers/leds/Makefile +1 -0
@@ -16,6 +16,7 @@ obj-$(CONFIG_LEDS_AN30259A) += leds-an30259a.o obj-$(CONFIG_LEDS_APU) += leds-apu.o obj-$(CONFIG_LEDS_ARIEL) += leds-ariel.o obj-$(CONFIG_LEDS_AS3668) += leds-as3668.o +obj-$(CONFIG_LEDS_ASUS_TRANSFORMER_EC) += leds-asus-transformer-ec.o obj-$(CONFIG_LEDS_AW200XX) += leds-aw200xx.o obj-$(CONFIG_LEDS_AW2013) += leds-aw2013.o obj-$(CONFIG_LEDS_BCM6328) += leds-bcm6328.o
drivers/leds/leds-asus-transformer-ec.c new file +79 -0
79 lines not on this page · raw message
Signature · 1 line
2.51.0
Lee Jones
maintainerRe: [PATCH v6 5/7] leds: Add driver for ASUS Transformer LEDs
Quoted 62 lines · Svyatoslav Ryhel
From: Michał Mirosław <[email protected]>
ASUS Transformer tablets have a green and an amber LED on both the Pad and the Dock. If both LEDs are enabled simultaneously, the emitted light will be yellow.
Co-developed-by Svyatoslav Ryhel <[email protected]>Signed-off-by Svyatoslav Ryhel <[email protected]>Signed-off-by Michał Mirosław <[email protected]>--- drivers/leds/Kconfig | 11 ++++ drivers/leds/Makefile | 1 + drivers/leds/leds-asus-transformer-ec.c | 79 +++++++++++++++++++++++++ 3 files changed, 91 insertions(+) create mode 100644 drivers/leds/leds-asus-transformer-ec.cdrivers/leds/Kconfig +11
-0@@ -120,6 +120,17 @@ config LEDS_OSRAM_AMS_AS3668 To compile this driver as a module, choose M here: the module will be called leds-as3668. +config LEDS_ASUS_TRANSFORMER_EC + tristate "LED Support for Asus Transformer charging LED" + depends on LEDS_CLASS + depends on MFD_ASUS_TRANSFORMER_EC + help + This option enables support for charging indicator on + Asus Transformer's Pad and it's Dock. + + To compile this driver as a module, choose M here: the module + will be called leds-asus-transformer-ec. + config LEDS_AW200XX tristate "LED support for Awinic AW20036/AW20054/AW20072/AW20108" depends on LEDS_CLASSdrivers/leds/Makefile +1
-0@@ -16,6 +16,7 @@ obj-$(CONFIG_LEDS_AN30259A) += leds-an30259a.o obj-$(CONFIG_LEDS_APU) += leds-apu.o obj-$(CONFIG_LEDS_ARIEL) += leds-ariel.o obj-$(CONFIG_LEDS_AS3668) += leds-as3668.o +obj-$(CONFIG_LEDS_ASUS_TRANSFORMER_EC) += leds-asus-transformer-ec.o obj-$(CONFIG_LEDS_AW200XX) += leds-aw200xx.o obj-$(CONFIG_LEDS_AW2013) += leds-aw2013.o obj-$(CONFIG_LEDS_BCM6328) += leds-bcm6328.odrivers/leds/leds-asus-transformer-ec.c new file +6
-0@@ -0,0 +1,79 @@ +// SPDX-License-Identifier: GPL-2.0-only + +#include <linux/err.h> +#include <linux/leds.h> +#include <linux/mfd/asus-transformer-ec.h> +#include <linux/module.h>
drivers/leds/leds-asus-transformer-ec.c line 16
Quoted 10 lines
drivers/leds/leds-asus-transformer-ec.c new file +10
-0+#include <linux/platform_device.h> +#include <linux/slab.h> + +static void asus_ec_led_set_brightness_amber (struct led_classdev *led, +enum led_brightness brightness) +{ +const struct asusec_info *ec =dev_get_drvdata (led->dev->parent); + +if (brightness) +asus_ec_set_ctl_bits (ec,ASUSEC_CTL_LED_AMBER );
Why not Regmap?
Quoted 4 lines
drivers/leds/leds-asus-transformer-ec.c new file +4
-0+else +asus_ec_clear_ctl_bits (ec,ASUSEC_CTL_LED_AMBER ); +} +
drivers/leds/leds-asus-transformer-ec.c line 30
Quoted 10 lines
drivers/leds/leds-asus-transformer-ec.c new file +10
-0+static void asus_ec_led_set_brightness_green (struct led_classdev *led, +enum led_brightness brightness) +{ +const struct asusec_info *ec =dev_get_drvdata (led->dev->parent); + +if (brightness) +asus_ec_set_ctl_bits (ec,ASUSEC_CTL_LED_GREEN ); +else +asus_ec_clear_ctl_bits (ec,ASUSEC_CTL_LED_GREEN ); +}
These are identical bar one variable.
drivers/leds/leds-asus-transformer-ec.c line 39
Quoted 8 lines
drivers/leds/leds-asus-transformer-ec.c new file +8
-0+static int asus_ec_led_probe (struct platform_device *pdev) +{ +struct asusec_info *ec =cell_to_ec (pdev); +struct device *dev = &pdev->dev; +struct led_classdev *amber_led, *green_led; +int ret; + +platform_set_drvdata (pdev, ec);
This appears to be unused.
Quoted 15 lines
drivers/leds/leds-asus-transformer-ec.c new file +15
-0+ amber_led =devm_kzalloc (dev,sizeof (*amber_led),GFP_KERNEL ); +if (!amber_led) +return -ENOMEM ; + + amber_led->name =devm_kasprintf (dev,GFP_KERNEL ,"%s::amber" , ec->name); + amber_led->max_brightness =1 ; + amber_led->flags =LED_CORE_SUSPENDRESUME |LED_RETAIN_AT_SHUTDOWN ; + amber_led->brightness_set = asus_ec_led_set_brightness_amber; + + ret =devm_led_classdev_register (dev, amber_led); +if (ret) +return dev_err_probe (dev, ret,"failed to register amber LED\n" ); + + green_led =devm_kzalloc (dev,sizeof (*green_led),GFP_KERNEL ); +if (!green_led)
drivers/leds/leds-asus-transformer-ec.c line 65
Quoted 10 lines
drivers/leds/leds-asus-transformer-ec.c new file +10
-0+return -ENOMEM ; + + green_led->name =devm_kasprintf (dev,GFP_KERNEL ,"%s::green" , ec->name); + green_led->max_brightness =1 ; + green_led->flags =LED_CORE_SUSPENDRESUME |LED_RETAIN_AT_SHUTDOWN ; + green_led->brightness_set = asus_ec_led_set_brightness_green; + + ret =devm_led_classdev_register (dev, green_led); +if (ret) +return dev_err_probe (dev, ret,"failed to register green LED\n" );
Imagine instead of 2 LEDs, you had 20. Would you copy and paste this 20 times? Please re-author this as though there were many more so it remains efficient and scaleable.
Quoted 17 lines
drivers/leds/leds-asus-transformer-ec.c new file +13
-0+return 0 ; +} + +static struct platform_driver asus_ec_led_driver = { + .driver.name ="asus-transformer-ec-led" , + .probe = asus_ec_led_probe, +}; +module_platform_driver (asus_ec_led_driver); + +MODULE_AUTHOR ("Michał Mirosław <[email protected]>" ); +MODULE_AUTHOR ("Svyatoslav Ryhel <[email protected]>" ); +MODULE_DESCRIPTION ("ASUS Transformer's charging LED driver" ); +MODULE_LICENSE ("GPL" );Signature · 1 line
2.51.0
Signature · 1 line
Lee Jones
PATCH v6 6/7 power: supply: Add driver for ASUS Transformer battery
In reply to Svyatoslav RyhelFrom: Michał Mirosław <[email protected]>
Driver implements one battery cell per EC controller and supports reading of battery status for ASUS Transformer's pad and mobile dock.
--- drivers/power/supply/Kconfig | 11 + drivers/power/supply/Makefile | 1 + .../supply/asus-transformer-ec-battery.c | 272 ++++++++++++++++++ 3 files changed, 284 insertions(+) create mode 100644 drivers/power/supply/asus-transformer-ec-battery.c
drivers/power/supply/Kconfig +11 -0
@@ -122,6 +122,17 @@ config BATTERY_CHAGALL This driver can also be built as a module. If so, the module will be called chagall-battery. +config BATTERY_ASUS_TRANSFORMER_EC + tristate "Asus Transformer's battery driver" + depends on MFD_ASUS_TRANSFORMER_EC + help + Say Y to enable support for battery status access on Tegra based + ASUS Transformer devices. + + This sub-driver supports battery cells found in Asus Transformer + tablets and mobile docks and controlled by a special embedded + controller. + config BATTERY_CPCAP tristate "Motorola CPCAP PMIC battery driver" depends on MFD_CPCAP && IIO
drivers/power/supply/Makefile +1 -0
@@ -22,6 +22,7 @@ obj-$(CONFIG_TEST_POWER) += test_power.o obj-$(CONFIG_BATTERY_88PM860X) += 88pm860x_battery.o obj-$(CONFIG_CHARGER_ADP5061) += adp5061.o obj-$(CONFIG_BATTERY_ACT8945A) += act8945a_charger.o +obj-$(CONFIG_BATTERY_ASUS_TRANSFORMER_EC) += asus-transformer-ec-battery.o obj-$(CONFIG_BATTERY_AXP20X) += axp20x_battery.o obj-$(CONFIG_CHARGER_AXP20X) += axp20x_ac_power.o obj-$(CONFIG_BATTERY_CHAGALL) += chagall-battery.o
drivers/power/supply/asus-transformer-ec-battery.c new file +272 -0
272 lines not on this page · raw message
Signature · 1 line
2.51.0
PATCH v6 7/7 power: supply: Add charger driver for Asus Transformers
In reply to Svyatoslav RyhelFrom: Michał Mirosław <[email protected]>
Add support for charger detection capabilities found in the embedded controller of ASUS Transformer devices.
--- drivers/power/supply/Kconfig | 11 + drivers/power/supply/Makefile | 1 + .../supply/asus-transformer-ec-charger.c | 193 ++++++++++++++++++ 3 files changed, 205 insertions(+) create mode 100644 drivers/power/supply/asus-transformer-ec-charger.c
drivers/power/supply/Kconfig +11 -0
@@ -508,6 +508,17 @@ config CHARGER_88PM860X help Say Y here to enable charger for Marvell 88PM860x chip. +config CHARGER_ASUS_TRANSFORMER_EC + tristate "Asus Transformer's charger driver" + depends on MFD_ASUS_TRANSFORMER_EC + help + Say Y here to enable support AC plug detection on Asus Transformer + Dock. + + This sub-driver supports charger detection mechanism found in Asus + Transformer tablets and mobile docks and controlled by special + embedded controller. + config CHARGER_PF1550 tristate "NXP PF1550 battery charger driver" depends on MFD_PF1550
drivers/power/supply/Makefile +1 -0
@@ -69,6 +69,7 @@ obj-$(CONFIG_CHARGER_RT9471) += rt9471.o obj-$(CONFIG_CHARGER_RT9756) += rt9756.o obj-$(CONFIG_BATTERY_TWL4030_MADC) += twl4030_madc_battery.o obj-$(CONFIG_CHARGER_88PM860X) += 88pm860x_charger.o +obj-$(CONFIG_CHARGER_ASUS_TRANSFORMER_EC) += asus-transformer-ec-charger.o obj-$(CONFIG_CHARGER_PF1550) += pf1550-charger.o obj-$(CONFIG_BATTERY_RX51) += rx51_battery.o obj-$(CONFIG_AB8500_BM) += ab8500_bmdata.o ab8500_charger.o ab8500_fg.o ab8500_btemp.o ab8500_chargalg.o
drivers/power/supply/asus-transformer-ec-charger.c new file +193 -0
193 lines not on this page · raw message
Signature · 1 line
2.51.0