Skip to contentSkip to conversation

MAINTAINERS: Add Advantech AHC1EC0 embedded controller entry

PATCH Superseded Campion Kang 6 participants 20 messages Jan 18, 2021 – Mar 24, 2021 3 messages not in the archive

Subsystems (4)

v6 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

PATCH v6 1/6 MAINTAINERS: Add Advantech AHC1EC0 embedded controller entry

Add Advantech AHC1EC0 embedded controller entry

Changed since V5:
- add include/linux/mfd/ahc1ec0.h

---
 MAINTAINERS | 11 +++++++++++
 1 file changed, 11 insertions(+)
MAINTAINERS +11 -0
index 00836f6452f0..e1ccdaadb5ee 100644
@@ -562,6 +562,17 @@ S:	Maintained
 F:	Documentation/scsi/advansys.rst
 F:	drivers/scsi/advansys.c
 
+ADVANTECH AHC1EC0 EMBEDDED CONTROLLER DRIVER
+M:	Campion Kang <[email protected]>
+L:	[email protected]
+S:	Maintained
+F:	Documentation/devicetree/bindings/mfd/ahc1ec0.yaml
+F:	drivers/hwmon/ahc1ec0-hwmon.c
+F:	drivers/mfd/ahc1ec0.c
+F:	drivers/watchdog/ahc1ec0-wdt.c
+F:	include/dt-bindings/mfd/ahc1ec0-dt.h
+F:	include/linux/mfd/ahc1ec0.h
+
 ADXL34X THREE-AXIS DIGITAL ACCELEROMETER DRIVER (ADXL345/ADXL346)
 M:	Michael Hennerich <[email protected]>
 S:	Supported
Signature · 1 line
2.17.1
#1 lore raw

PATCH v6 2/6 mfd: ahc1ec0: Add Advantech EC include file used by dt-bindings

This files defines the sud-device types and hwmon profiles support by Advantech embedded controller.

---
 include/dt-bindings/mfd/ahc1ec0-dt.h | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)
 create mode 100644 include/dt-bindings/mfd/ahc1ec0-dt.h
include/dt-bindings/mfd/ahc1ec0-dt.h new file +25 -0
new file mode 100644
index 000000000000..389a7a7f8f02
@@ -0,0 +1,25 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Device Tree defines for Advantech Embedded Controller (AHC1EC0)
+ */
+
+#ifndef _DT_BINDINGS_MFD_AHC1EC0_H
+#define _DT_BINDINGS_MFD_AHC1EC0_H
+
+/* Sub-device Definitions */
+#define AHC1EC0_SUBDEV_BRIGHTNESS 0x0
+#define AHC1EC0_SUBDEV_EEPROM     0x1
+#define AHC1EC0_SUBDEV_GPIO       0x2
+#define AHC1EC0_SUBDEV_HWMON      0x3
+#define AHC1EC0_SUBDEV_LED        0x4
+#define AHC1EC0_SUBDEV_WDT        0x5
+
+/* HWMON Profile Definitions */
+#define AHC1EC0_HWMON_PRO_TEMPLATE 0x0
+#define AHC1EC0_HWMON_PRO_TPC5XXX  0x1
+#define AHC1EC0_HWMON_PRO_PRVR4    0x2
+#define AHC1EC0_HWMON_PRO_UNO2271G 0x3
+#define AHC1EC0_HWMON_PRO_UNO1172A 0x4
+#define AHC1EC0_HWMON_PRO_UNO1372G 0x5
+
+#endif /* _DT_BINDINGS_MFD_AHC1EC0_H */
Signature · 1 line
2.17.1
#2 lore raw

Re: [PATCH v6 2/6] mfd: ahc1ec0: Add Advantech EC include file used by dt-bindings

Quoted 8 lines · Campion Kang

This files defines the sud-device types and hwmon profiles support by Advantech embedded controller.

---
 include/dt-bindings/mfd/ahc1ec0-dt.h | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)
 create mode 100644 include/dt-bindings/mfd/ahc1ec0-dt.h

For my own reference (apply this as-is to your sign-off block):

Signature · 4 lines
Lee Jones [李琼斯]
Senior Technical Lead - Developer Services
Linaro.org │ Open source software for Arm SoCs
Follow Linaro: Facebook | Twitter | Blog
#3 lore raw

PATCH v6 3/6 dt-bindings: mfd: ahc1ec0.yaml: Add Advantech embedded controller - AHC1EC0

In reply to Campion Kang

Add DT binding schema for Advantech embedded controller AHC1EC0.

Changed since V5:
- rename dt-bindings/mfd/ahc1ec0.h to dt-bindings/mfd/ahc1ec0-dt.h that found errors by bot 'make dt_binding_check'

---
 .../devicetree/bindings/mfd/ahc1ec0.yaml      | 69 +++++++++++++++++++
 1 file changed, 69 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/mfd/ahc1ec0.yaml
Documentation/devicetree/bindings/mfd/ahc1ec0.yaml new file +69 -0
new file mode 100644
index 000000000000..40af14bb9c0a
@@ -0,0 +1,69 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/mfd/ahc1ec0.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Advantech Embedded Controller (AHC1EC0)
+
+maintainers:
+  - Campion Kang <[email protected]>
+
+description: |
+  AHC1EC0 is one of the embedded controllers used by Advantech to provide several
+  functions such as watchdog, hwmon, brightness, etc. Advantech related applications
+  can control the whole system via these functions.
+
+properties:
+  compatible:
+    const: advantech,ahc1ec0
+
+  advantech,sub-dev-nb:
+    description:
+      The number of sub-devices specified in the platform.
+    $ref: /schemas/types.yaml#/definitions/uint32
+    maxItems: 1
+
+  advantech,sub-dev:
+    description:
+      A list of the sub-devices supported in the platform. Defines for the
+      appropriate values can found in dt-bindings/mfd/ahc1ec0-dt.h.
+    $ref: "/schemas/types.yaml#/definitions/uint32-array"
+    minItems: 1
+    maxItems: 6
+
+  advantech,hwmon-profile:
+    description:
+      The number of sub-devices specified in the platform. Defines for the
+      hwmon profiles can found in dt-bindings/mfd/ahc1ec0-dt.
+    $ref: /schemas/types.yaml#/definitions/uint32
+    maxItems: 1
+
+required:
+  - compatible
+  - advantech,sub-dev-nb
+  - advantech,sub-dev
+
+if:
+  properties:
+    advantech,sub-dev:
+      contains:
+        const: 0x3
+then:
+  required:
+    - advantech,hwmon-profile
+
+additionalProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/mfd/ahc1ec0-dt.h>
+    ahc1ec0 {
+        compatible = "advantech,ahc1ec0";
+
+        advantech,sub-dev-nb = <2>;
+        advantech,sub-dev = <AHC1EC0_SUBDEV_HWMON
+                             AHC1EC0_SUBDEV_WDT>;
+
+        advantech,hwmon-profile = <AHC1EC0_HWMON_PRO_UNO2271G>;
+    };
Signature · 1 line
2.17.1
maintainer
#4 lore raw

Re: [PATCH v6 3/6] dt-bindings: mfd: ahc1ec0.yaml: Add Advantech embedded controller - AHC1EC0

Quoted 33 lines · Campion Kang

Add DT binding schema for Advantech embedded controller AHC1EC0.

Changed since V5:
- rename dt-bindings/mfd/ahc1ec0.h to dt-bindings/mfd/ahc1ec0-dt.h that found errors by bot 'make dt_binding_check'

---
 .../devicetree/bindings/mfd/ahc1ec0.yaml      | 69 +++++++++++++++++++
 1 file changed, 69 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/mfd/ahc1ec0.yaml
Documentation/devicetree/bindings/mfd/ahc1ec0.yaml new file +15 -0
new file mode 100644
index 000000000000..40af14bb9c0a
@@ -0,0 +1,69 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/mfd/ahc1ec0.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Advantech Embedded Controller (AHC1EC0)
+
+maintainers:
+  - Campion Kang <[email protected]>
+
+description: |
+  AHC1EC0 is one of the embedded controllers used by Advantech to provide several
+  functions such as watchdog, hwmon, brightness, etc. Advantech related applications
+  can control the whole system via these functions.

Documentation/devicetree/bindings/mfd/ahc1ec0.yaml line 25

Quoted 10 lines
Documentation/devicetree/bindings/mfd/ahc1ec0.yaml new file +10 -0
+
+properties:
+  compatible:
+    const: advantech,ahc1ec0
+
+  advantech,sub-dev-nb:
+    description:
+      The number of sub-devices specified in the platform.
+    $ref: /schemas/types.yaml#/definitions/uint32
+    maxItems: 1

You don't need this, just count the length of the next property:

Documentation/devicetree/bindings/mfd/ahc1ec0.yaml line 33

Quoted 8 lines
Documentation/devicetree/bindings/mfd/ahc1ec0.yaml new file +8 -0
+
+  advantech,sub-dev:
+    description:
+      A list of the sub-devices supported in the platform. Defines for the
+      appropriate values can found in dt-bindings/mfd/ahc1ec0-dt.h.
+    $ref: "/schemas/types.yaml#/definitions/uint32-array"
+    minItems: 1
+    maxItems: 6

But as I said before, this binding is odd. It doesn't look like how we do any other MFD. Either we have child nodes or they are implicit.

Quoted 39 lines
Documentation/devicetree/bindings/mfd/ahc1ec0.yaml new file +36 -0
+
+  advantech,hwmon-profile:
+    description:
+      The number of sub-devices specified in the platform. Defines for the
+      hwmon profiles can found in dt-bindings/mfd/ahc1ec0-dt.
+    $ref: /schemas/types.yaml#/definitions/uint32
+    maxItems: 1
+
+required:
+  - compatible
+  - advantech,sub-dev-nb
+  - advantech,sub-dev
+
+if:
+  properties:
+    advantech,sub-dev:
+      contains:
+        const: 0x3
+then:
+  required:
+    - advantech,hwmon-profile
+
+additionalProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/mfd/ahc1ec0-dt.h>
+    ahc1ec0 {
+        compatible = "advantech,ahc1ec0";
+
+        advantech,sub-dev-nb = <2>;
+        advantech,sub-dev = <AHC1EC0_SUBDEV_HWMON
+                             AHC1EC0_SUBDEV_WDT>;
+
+        advantech,hwmon-profile = <AHC1EC0_HWMON_PRO_UNO2271G>;
+    };
Signature · 1 line
2.17.1
#5 lore raw

PATCH v6 4/6 mfd: ahc1ec0: Add support for Advantech embedded controller

In reply to Campion Kang

AHC1EC0 is the embedded controller driver for Advantech industrial products. This provides sub-devices such as hwmon and watchdog, and also expose functions for sub-devices to read/write the value to embedded controller.

Changed since V5:
- Kconfig: add "AHC1EC0" string to clearly define the EC name
- fix the code according to reviewer's suggestion
- remove unnecessary header files
- change the structure name to lower case, align with others
naming

---
 drivers/mfd/Kconfig         |  10 +
 drivers/mfd/Makefile        |   2 +
 drivers/mfd/ahc1ec0.c       | 808 ++++++++++++++++++++++++++++++++++++
 include/linux/mfd/ahc1ec0.h | 276 ++++++++++++
 4 files changed, 1096 insertions(+)
 create mode 100644 drivers/mfd/ahc1ec0.c
 create mode 100644 include/linux/mfd/ahc1ec0.h
drivers/mfd/Kconfig +10 -0
index bdfce7b15621..7d5fb5c17d9a 100644
@@ -2154,5 +2154,15 @@ config MFD_INTEL_M10_BMC
 	  additional drivers must be enabled in order to use the functionality
 	  of the device.
 
+config MFD_AHC1EC0
+	tristate "Advantech AHC1EC0 Embedded Controller Module"
+	depends on X86
+	select MFD_CORE
+	help
+	  This is the core function that for Advantech EC drivers. It
+	  includes the sub-devices such as hwmon, watchdog, etc. And also
+	  provides expose functions for sub-devices to read/write the value
+	  to embedded controller.
+
 endmenu
 endif
drivers/mfd/Makefile +2 -0
index 14fdb188af02..a6af9d8825f4 100644
@@ -268,3 +268,5 @@ obj-$(CONFIG_MFD_KHADAS_MCU) 	+= khadas-mcu.o
 obj-$(CONFIG_SGI_MFD_IOC3)	+= ioc3.o
 obj-$(CONFIG_MFD_SIMPLE_MFD_I2C)	+= simple-mfd-i2c.o
 obj-$(CONFIG_MFD_INTEL_M10_BMC)   += intel-m10-bmc.o
+
+obj-$(CONFIG_MFD_AHC1EC0)	+= ahc1ec0.o
drivers/mfd/ahc1ec0.c new file +808 -0

808 lines not on this page · raw message

include/linux/mfd/ahc1ec0.h new file +276 -0

276 lines not on this page · raw message

Signature · 1 line
2.17.1
Tested-by Chia-Lin Kao (AceLan) on v6 4/6
#6 lore raw

Re: [PATCH v6 4/6] mfd: ahc1ec0: Add support for Advantech embedded controller

Campion Kang <[email protected]> 於 2021年1月18日 週一 下午8:37寫道:

Quoted 1,150 lines

AHC1EC0 is the embedded controller driver for Advantech industrial products. This provides sub-devices such as hwmon and watchdog, and also expose functions for sub-devices to read/write the value to embedded controller.

Changed since V5:
- Kconfig: add "AHC1EC0" string to clearly define the EC name
- fix the code according to reviewer's suggestion
- remove unnecessary header files
- change the structure name to lower case, align with others
naming

---
 drivers/mfd/Kconfig         |  10 +
 drivers/mfd/Makefile        |   2 +
 drivers/mfd/ahc1ec0.c       | 808 ++++++++++++++++++++++++++++++++++++
 include/linux/mfd/ahc1ec0.h | 276 ++++++++++++
 4 files changed, 1096 insertions(+)
 create mode 100644 drivers/mfd/ahc1ec0.c
 create mode 100644 include/linux/mfd/ahc1ec0.h
drivers/mfd/Kconfig +10 -0
index bdfce7b15621..7d5fb5c17d9a 100644
@@ -2154,5 +2154,15 @@ config MFD_INTEL_M10_BMC
          additional drivers must be enabled in order to use the functionality
          of the device.
 
+config MFD_AHC1EC0
+       tristate "Advantech AHC1EC0 Embedded Controller Module"
+       depends on X86
+       select MFD_CORE
+       help
+         This is the core function that for Advantech EC drivers. It
+         includes the sub-devices such as hwmon, watchdog, etc. And also
+         provides expose functions for sub-devices to read/write the value
+         to embedded controller.
+
 endmenu
 endif
drivers/mfd/Makefile +2 -0
index 14fdb188af02..a6af9d8825f4 100644
@@ -268,3 +268,5 @@ obj-$(CONFIG_MFD_KHADAS_MCU)        += khadas-mcu.o
 obj-$(CONFIG_SGI_MFD_IOC3)     += ioc3.o
 obj-$(CONFIG_MFD_SIMPLE_MFD_I2C)       += simple-mfd-i2c.o
 obj-$(CONFIG_MFD_INTEL_M10_BMC)   += intel-m10-bmc.o
+
+obj-$(CONFIG_MFD_AHC1EC0)      += ahc1ec0.o
drivers/mfd/ahc1ec0.c new file +808 -0
new file mode 100644
index 000000000000..015f4307a54e
@@ -0,0 +1,808 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Advantech embedded controller core driver AHC1EC0
+ *
+ * Copyright 2020 Advantech IIoT Group
+ *
+ */
+
+#include <linux/acpi.h>
+#include <linux/delay.h>
+#include <linux/dmi.h>
+#include <linux/errno.h>
+#include <linux/mfd/ahc1ec0.h>
+#include <linux/mfd/core.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/of_platform.h>
+#include <linux/platform_device.h>
+
+#define DRV_NAME      "ahc1ec0"
+
+enum {
+       ADVEC_SUBDEV_BRIGHTNESS = 0,
+       ADVEC_SUBDEV_EEPROM,
+       ADVEC_SUBDEV_GPIO,
+       ADVEC_SUBDEV_HWMON,
+       ADVEC_SUBDEV_LED,
+       ADVEC_SUBDEV_WDT,
+       ADVEC_SUBDEV_MAX,
+};
+
+/* Wait IBF (Input Buffer Full) clear */
+static int ec_wait_write(void)
+{
+       int i;
+
+       for (i = 0; i < EC_MAX_TIMEOUT_COUNT; i++) {
+               if ((inb(EC_COMMAND_PORT) & EC_COMMAND_BIT_IBF) == 0)
+                       return 0;
+
+               udelay(EC_RETRY_UDELAY);
+       }
+
+       return -ETIMEDOUT;
+}
+
+/* Wait OBF (Output Buffer Full) data ready */
+static int ec_wait_read(void)
+{
+       int i;
+
+       for (i = 0; i < EC_MAX_TIMEOUT_COUNT; i++) {
+               if ((inb(EC_COMMAND_PORT) & EC_COMMAND_BIT_OBF) != 0)
+                       return 0;
+
+               udelay(EC_RETRY_UDELAY);
+       }
+
+       return -ETIMEDOUT;
+}
+
+/* Read data from EC HW RAM, the process is the following:
+ * Step 0. Wait IBF clear to send command
+ * Step 1. Send read command to EC command port
+ * Step 2. Wait IBF clear that means command is got by EC
+ * Step 3. Send read address to EC data port
+ * Step 4. Wait OBF data ready
+ * Step 5. Get data from EC data port
+ */
+int read_hw_ram(struct adv_ec_platform_data *adv_ec_data, unsigned char addr, unsigned char *data)
+{
+       int ret;
+
+       mutex_lock(&adv_ec_data->lock);
+
+       ret = ec_wait_write();
+       if (ret)
+               goto error;
+       outb(EC_HW_RAM_READ, EC_COMMAND_PORT);
+
+       ret = ec_wait_write();
+       if (ret)
+               goto error;
+       outb(addr, EC_STATUS_PORT);
+
+       ret = ec_wait_read();
+       if (ret)
+               goto error;
+       *data = inb(EC_STATUS_PORT);
+
+       mutex_unlock(&adv_ec_data->lock);
+
+       return ret;
+
+error:
+       mutex_unlock(&adv_ec_data->lock);
+       dev_err(adv_ec_data->dev, "%s: Wait for IBF or OBF too long. line: %d\n", __func__,
+              __LINE__);
+
+       return ret;
+}
+
+/* Write data to EC HW RAM
+ * Step 0. Wait IBF clear to send command
+ * Step 1. Send write command to EC command port
+ * Step 2. Wait IBF clear that means command is got by EC
+ * Step 3. Send write address to EC data port
+ * Step 4. Wait IBF clear that means command is got by EC
+ * Step 5. Send data to EC data port
+ */
+int write_hw_ram(struct adv_ec_platform_data *adv_ec_data, unsigned char addr, unsigned char data)
+{
+       int ret;
+
+       mutex_lock(&adv_ec_data->lock);
+
+       ret = ec_wait_write();
+       if (ret)
+               goto error;
+       outb(EC_HW_RAM_WRITE, EC_COMMAND_PORT);
+
+       ret = ec_wait_write();
+       if (ret)
+               goto error;
+       outb(addr, EC_STATUS_PORT);
+
+       ret = ec_wait_write();
+       if (ret)
+               goto error;
+       outb(data, EC_STATUS_PORT);
+
+       mutex_unlock(&adv_ec_data->lock);
+
+       return 0;
+
+error:
+       mutex_unlock(&adv_ec_data->lock);
+
+       dev_err(adv_ec_data->dev, "%s: Wait for IBF or OBF too long. line: %d\n", __func__,
+              __LINE__);
+
+       return ret;
+}
+EXPORT_SYMBOL_GPL(write_hw_ram);
+
+/* Get dynamic control table */
+static int adv_get_dynamic_tab(struct adv_ec_platform_data *adv_ec_data)
+{
+       int i, ret;
+       unsigned char pin_tmp, device_id;
+
+       mutex_lock(&adv_ec_data->lock);
+
+       for (i = 0; i < EC_MAX_TBL_NUM; i++) {
+               adv_ec_data->dym_tbl[i].device_id = 0xff;
+               adv_ec_data->dym_tbl[i].hw_pin_num = 0xff;
+       }
+
+       for (i = 0; i < EC_MAX_TBL_NUM; i++) {
+               ret = ec_wait_write();
+               if (ret) {
+                       dev_dbg(adv_ec_data->dev, "%s: ec_wait_write. line: %d\n", __func__,
+                               __LINE__);
+                       goto error;
+               }
+               outb(EC_TBL_WRITE_ITEM, EC_COMMAND_PORT);
+
+               ret = ec_wait_write();
+               if (ret) {
+                       dev_dbg(adv_ec_data->dev, "%s: ec_wait_write. line: %d\n", __func__,
+                               __LINE__);
+                       goto error;
+               }
+               outb(i, EC_STATUS_PORT);
+
+               ret = ec_wait_read();
+               if (ret) {
+                       dev_dbg(adv_ec_data->dev, "%s: ec_wait_read. line: %d\n", __func__,
+                               __LINE__);
+                       goto error;
+               }
+
+               /*
+                *  If item is defined, EC will return item number.
+                *  If table item is not defined, EC will return 0xFF.
+                */
+               pin_tmp = inb(EC_STATUS_PORT);
+               if (pin_tmp == 0xff) {
+                       dev_dbg(adv_ec_data->dev, "%s: inb(EC_STATUS_PORT)=0x%02x != 0xff.\n",
+                               __func__, pin_tmp);
+                       goto pass;
+               }
+
+               ret = ec_wait_write();
+               if (ret) {
+                       dev_dbg(adv_ec_data->dev, "%s: ec_wait_write. line: %d\n", __func__,
+                               __LINE__);
+                       goto error;
+               }
+               outb(EC_TBL_GET_PIN, EC_COMMAND_PORT);
+
+               ret = ec_wait_read();
+               if (ret) {
+                       dev_dbg(adv_ec_data->dev, "%s: ec_wait_read. line: %d\n", __func__,
+                               __LINE__);
+                       goto error;
+               }
+               pin_tmp = inb(EC_STATUS_PORT) & 0xff;
+               if (pin_tmp == 0xff) {
+                       dev_dbg(adv_ec_data->dev, "%s: pin_tmp(0x%02X). line: %d\n", __func__,
+                               pin_tmp, __LINE__);
+                       goto pass;
+               }
+
+               ret = ec_wait_write();
+               if (ret)
+                       goto error;
+               outb(EC_TBL_GET_DEVID, EC_COMMAND_PORT);
+
+               ret = ec_wait_read();
+               if (ret) {
+                       dev_dbg(adv_ec_data->dev, "%s: ec_wait_read. line: %d\n", __func__,
+                               __LINE__);
+                       goto error;
+               }
+               device_id = inb(EC_STATUS_PORT) & 0xff;
+
+               dev_dbg(adv_ec_data->dev, "%s: device_id=0x%02X. line: %d\n", __func__,
+                       device_id, __LINE__);
+
+               adv_ec_data->dym_tbl[i].device_id = device_id;
+               adv_ec_data->dym_tbl[i].hw_pin_num = pin_tmp;
+       }
+
+pass:
+       mutex_unlock(&adv_ec_data->lock);
+       return 0;
+
+error:
+       mutex_unlock(&adv_ec_data->lock);
+       dev_err(adv_ec_data->dev, "%s: Wait for IBF or OBF too long. line: %d\n",
+               __func__, __LINE__);
+       return ret;
+}
+
+int read_ad_value(struct adv_ec_platform_data *adv_ec_data, unsigned char hwpin,
+               unsigned char multi)
+{
+       int ret;
+       u32 ret_val;
+       unsigned int LSB, MSB;
+
+       mutex_lock(&adv_ec_data->lock);
+       ret = ec_wait_write();
+       if (ret)
+               goto error;
+       outb(EC_AD_INDEX_WRITE, EC_COMMAND_PORT);
+
+       ret = ec_wait_write();
+       if (ret)
+               goto error;
+       outb(hwpin, EC_STATUS_PORT);
+
+       ret = ec_wait_read();
+       if (ret)
+               goto error;
+
+       if (inb(EC_STATUS_PORT) == 0xff) {
+               mutex_unlock(&adv_ec_data->lock);
+               return -1;
+       }
+
+       ret = ec_wait_write();
+       if (ret)
+               goto error;
+       outb(EC_AD_LSB_READ, EC_COMMAND_PORT);
+
+       ret = ec_wait_read();
+       if (ret)
+               goto error;
+       LSB = inb(EC_STATUS_PORT);
+
+       ret = ec_wait_write();
+       if (ret)
+               goto error;
+       outb(EC_AD_MSB_READ, EC_COMMAND_PORT);
+
+       ret = ec_wait_read();
+       if (ret)
+               goto error;
+       MSB = inb(EC_STATUS_PORT);
+       ret_val = ((MSB << 8) | LSB) & 0x03FF;
+       ret_val = ret_val * multi * 100;
+
+       mutex_unlock(&adv_ec_data->lock);
+       return ret_val;
+
+error:
+       mutex_unlock(&adv_ec_data->lock);
+
+       dev_warn(adv_ec_data->dev, "%s: Wait for IBF or OBF too long. line: %d\n", __func__,
+               __LINE__);
+
+       return ret;
+}
+EXPORT_SYMBOL_GPL(read_ad_value);
+
+int read_acpi_value(struct adv_ec_platform_data *adv_ec_data, unsigned char addr,
+               unsigned char *pvalue)
+{
+       int ret;
+       unsigned char value;
+
+       mutex_lock(&adv_ec_data->lock);
+
+       ret = ec_wait_write();
+       if (ret)
+               goto error;
+       outb(EC_ACPI_RAM_READ, EC_COMMAND_PORT);
+
+       ret = ec_wait_write();
+       if (ret)
+               goto error;
+       outb(addr, EC_STATUS_PORT);
+
+       ret = ec_wait_read();
+       if (ret)
+               goto error;
+       value = inb(EC_STATUS_PORT);
+       *pvalue = value;
+
+       mutex_unlock(&adv_ec_data->lock);
+
+       return 0;
+
+error:
+       mutex_unlock(&adv_ec_data->lock);
+
+       dev_warn(adv_ec_data->dev, "%s: Wait for IBF or OBF too long. line: %d\n", __func__,
+               __LINE__);
+
+       return ret;
+}
+EXPORT_SYMBOL_GPL(read_acpi_value);
+
+int write_acpi_value(struct adv_ec_platform_data *adv_ec_data, unsigned char addr,
+               unsigned char value)
+{
+       int ret;
+
+       mutex_lock(&adv_ec_data->lock);
+
+       ret = ec_wait_write();
+       if (ret)
+               goto error;
+       outb(EC_ACPI_DATA_WRITE, EC_COMMAND_PORT);
+
+       ret = ec_wait_write();
+       if (ret)
+               goto error;
+       outb(addr, EC_STATUS_PORT);
+
+       ret = ec_wait_write();
+       if (ret)
+               goto error;
+       outb(value, EC_STATUS_PORT);
+
+       mutex_unlock(&adv_ec_data->lock);
+       return 0;
+
+error:
+       mutex_unlock(&adv_ec_data->lock);
+
+       dev_warn(adv_ec_data->dev, "%s: Wait for IBF or OBF too long. line: %d\n", __func__,
+               __LINE__);
+
+       return ret;
+}
+
+int read_gpio_status(struct adv_ec_platform_data *adv_ec_data, unsigned char PinNumber,
+               unsigned char *pvalue)
+{
+       int ret;
+
+       unsigned char gpio_status_value;
+
+       mutex_lock(&adv_ec_data->lock);
+
+       ret = ec_wait_write();
+       if (ret)
+               goto error;
+       outb(EC_GPIO_INDEX_WRITE, EC_COMMAND_PORT);
+
+       ret = ec_wait_write();
+       if (ret)
+               goto error;
+       outb(PinNumber, EC_STATUS_PORT);
+
+       ret = ec_wait_read();
+       if (ret)
+               goto error;
+
+       if (inb(EC_STATUS_PORT) == 0xff) {
+               dev_err(adv_ec_data->dev, "%s: Read Pin Number error!!\n", __func__);
+               mutex_unlock(&adv_ec_data->lock);
+               return -1;
+       }
+
+       ret = ec_wait_write();
+       if (ret)
+               goto error;
+       outb(EC_GPIO_STATUS_READ, EC_COMMAND_PORT);
+
+       ret = ec_wait_read();
+       if (ret)
+               goto error;
+       gpio_status_value = inb(EC_STATUS_PORT);
+
+       *pvalue = gpio_status_value;
+       mutex_unlock(&adv_ec_data->lock);
+       return 0;
+
+error:
+       mutex_unlock(&adv_ec_data->lock);
+
+       dev_warn(adv_ec_data->dev, "%s: Wait for IBF or OBF too long. line: %d\n", __func__,
+               __LINE__);
+       return ret;
+}
+
+int write_gpio_status(struct adv_ec_platform_data *adv_ec_data, unsigned char PinNumber,
+               unsigned char value)
+{
+       int ret;
+
+       mutex_lock(&adv_ec_data->lock);
+
+       ret = ec_wait_write();
+       if (ret)
+               goto error;
+       outb(EC_GPIO_INDEX_WRITE, EC_COMMAND_PORT);
+
+       ret = ec_wait_write();
+       if (ret)
+               goto error;
+       outb(PinNumber, EC_STATUS_PORT);
+
+       ret = ec_wait_read();
+       if (ret)
+               goto error;
+
+       if (inb(EC_STATUS_PORT) == 0xff) {
+               mutex_unlock(&adv_ec_data->lock);
+               dev_err(adv_ec_data->dev, "%s: Read Pin Number error!!\n", __func__);
+               return -1;
+       }
+
+       ret = ec_wait_write();
+       if (ret)
+               goto error;
+       outb(EC_GPIO_STATUS_WRITE, EC_COMMAND_PORT);
+
+       ret = ec_wait_write();
+       if (ret)
+               goto error;
+       outb(value, EC_STATUS_PORT);
+
+       mutex_unlock(&adv_ec_data->lock);
+       return 0;
+
+error:
+       mutex_unlock(&adv_ec_data->lock);
+       dev_err(adv_ec_data->dev, "%s: Wait for IBF or OBF too long. line: %d", __func__,
+               __LINE__);
+
+       return ret;
+}
+
+int read_gpio_dir(struct adv_ec_platform_data *adv_ec_data, unsigned char PinNumber,
+               unsigned char *pvalue)
+{
+       int ret;
+       unsigned char gpio_dir_value;
+
+       mutex_lock(&adv_ec_data->lock);
+
+       ret = ec_wait_write();
+       if (ret)
+               goto error;
+       outb(EC_GPIO_INDEX_WRITE, EC_COMMAND_PORT);
+
+       ret = ec_wait_write();
+       if (ret)
+               goto error;
+       outb(PinNumber, EC_STATUS_PORT);
+
+       ret = ec_wait_read();
+       if (ret)
+               goto error;
+
+       if (inb(EC_STATUS_PORT) == 0xff) {
+               mutex_unlock(&adv_ec_data->lock);
+               dev_err(adv_ec_data->dev, "%s: Read Pin Number error!! line: %d\n", __func__,
+                       __LINE__);
+               return -1;
+       }
+
+       ret = ec_wait_write();
+       if (ret)
+               goto error;
+       outb(EC_GPIO_DIR_READ, EC_COMMAND_PORT);
+
+       ret = ec_wait_read();
+       if (ret)
+               goto error;
+       gpio_dir_value = inb(EC_STATUS_PORT);
+       *pvalue = gpio_dir_value;
+
+       mutex_unlock(&adv_ec_data->lock);
+       return 0;
+
+error:
+       mutex_unlock(&adv_ec_data->lock);
+
+       dev_warn(adv_ec_data->dev, "%s: Wait for IBF or OBF too long. line: %d\n", __func__,
+                       __LINE__);
+
+       return ret;
+}
+
+int write_gpio_dir(struct adv_ec_platform_data *adv_ec_data, unsigned char PinNumber,
+               unsigned char value)
+{
+       int ret;
+
+       mutex_lock(&adv_ec_data->lock);
+
+       ret = ec_wait_write();
+       if (ret)
+               goto error;
+       outb(EC_GPIO_INDEX_WRITE, EC_COMMAND_PORT);
+
+       ret = ec_wait_write();
+       if (ret)
+               goto error;
+       outb(PinNumber, EC_STATUS_PORT);
+
+       ret = ec_wait_read();
+       if (ret)
+               goto error;
+
+       if (inb(EC_STATUS_PORT) == 0xff) {
+               mutex_unlock(&adv_ec_data->lock);
+               dev_warn(adv_ec_data->dev, "%s: Read Pin Number error!! line: %d\n", __func__,
+                       __LINE__);
+
+               return -1;
+       }
+
+       ret = ec_wait_write();
+       if (ret)
+               goto error;
+       outb(EC_GPIO_DIR_WRITE, EC_COMMAND_PORT);
+
+       ret = ec_wait_write();
+       if (ret)
+               goto error;
+       outb(value, EC_STATUS_PORT);
+
+       mutex_unlock(&adv_ec_data->lock);
+       return 0;
+
+error:
+       mutex_unlock(&adv_ec_data->lock);
+
+       dev_warn(adv_ec_data->dev, "%s: Wait for IBF or OBF too long. line: %d\n", __func__,
+                       __LINE__);
+
+       return ret;
+}
+
+int write_hwram_command(struct adv_ec_platform_data *adv_ec_data, unsigned char data)
+{
+       int ret;
+
+       mutex_lock(&adv_ec_data->lock);
+
+       ret = ec_wait_write();
+       if (ret)
+               goto error;
+       outb(data, EC_COMMAND_PORT);
+
+       mutex_unlock(&adv_ec_data->lock);
+       return 0;
+
+error:
+       mutex_unlock(&adv_ec_data->lock);
+
+       dev_warn(adv_ec_data->dev, "%s: Wait for IBF or OBF too long. line: %d\n", __func__,
+                       __LINE__);
+
+       return ret;
+}
+EXPORT_SYMBOL_GPL(write_hwram_command);
+
+static int adv_ec_get_productname(struct adv_ec_platform_data *adv_ec_data, char *product)
+{
+       const char *vendor, *device;
+       int length = 0;
+
+       /* Check it is Advantech board */
+       vendor = dmi_get_system_info(DMI_SYS_VENDOR);
+       if (memcmp(vendor, "Advantech", sizeof("Advantech")) != 0)
+               return -ENODEV;
+
+       /* Get product model name */
+       device = dmi_get_system_info(DMI_PRODUCT_NAME);
+       if (device) {
+               while ((device[length] != ' ')
+                       && (length < AMI_ADVANTECH_BOARD_ID_LENGTH))
+                       length++;
+               memset(product, 0, AMI_ADVANTECH_BOARD_ID_LENGTH);
+               memmove(product, device, length);
+
+               dev_info(adv_ec_data->dev, "BIOS Product Name = %s\n", product);
+
+               return 0;
+       }
+
+       dev_warn(adv_ec_data->dev, "This device is not Advantech Board (%s)!\n", product);
+
+       return -ENODEV;
+}
+
+static const struct mfd_cell adv_ec_sub_cells[] = {
+       { .name = "adv-ec-brightness", },
+       { .name = "adv-ec-eeprom", },
+       { .name = "adv-ec-gpio", },
+       { .name = "ahc1ec0-hwmon", },
+       { .name = "adv-ec-led", },
+       { .name = "ahc1ec0-wdt", },
+};
+
+static int adv_ec_init_ec_data(struct adv_ec_platform_data *adv_ec_data)
+{
+       int ret;
+
+       adv_ec_data->sub_dev_mask = 0;
+       adv_ec_data->sub_dev_nb = 0;
+       adv_ec_data->dym_tbl = NULL;
+       adv_ec_data->bios_product_name = NULL;
+
+       mutex_init(&adv_ec_data->lock);
+
+       /* Get product name */
+       adv_ec_data->bios_product_name =
+               devm_kzalloc(adv_ec_data->dev, AMI_ADVANTECH_BOARD_ID_LENGTH, GFP_KERNEL);
+       if (!adv_ec_data->bios_product_name)
+               return -ENOMEM;
+
+       memset(adv_ec_data->bios_product_name, 0, AMI_ADVANTECH_BOARD_ID_LENGTH);
+       ret = adv_ec_get_productname(adv_ec_data, adv_ec_data->bios_product_name);
+       if (ret)
+               return ret;
+
+       /* Get pin table */
+       adv_ec_data->dym_tbl = devm_kzalloc(adv_ec_data->dev,
+                                       EC_MAX_TBL_NUM * sizeof(struct ec_dynamic_table),
+                                       GFP_KERNEL);
+       if (!adv_ec_data->dym_tbl)
+               return -ENOMEM;
+
+       ret = adv_get_dynamic_tab(adv_ec_data);
+
+       return ret;
+}
+
+static int adv_ec_parse_prop(struct adv_ec_platform_data *adv_ec_data)
+{
+       int i, ret;
+       u32 nb, sub_dev[ADVEC_SUBDEV_MAX];
+
+       ret = device_property_read_u32(adv_ec_data->dev, "advantech,sub-dev-nb", &nb);
+       if (ret < 0) {
+               dev_err(adv_ec_data->dev, "get sub-dev-nb failed! (%d)\n", ret);
+               return ret;
+       }
+       adv_ec_data->sub_dev_nb = nb;
+
+       ret = device_property_read_u32_array(adv_ec_data->dev, "advantech,sub-dev",
+                                            sub_dev, nb);
+       if (ret < 0) {
+               dev_err(adv_ec_data->dev, "get sub-dev failed! (%d)\n", ret);
+               return ret;
+       }
+
+       for (i = 0; i < nb; i++) {
+               switch (sub_dev[i]) {
+               case ADVEC_SUBDEV_BRIGHTNESS:
+               case ADVEC_SUBDEV_EEPROM:
+               case ADVEC_SUBDEV_GPIO:
+               case ADVEC_SUBDEV_HWMON:
+               case ADVEC_SUBDEV_LED:
+               case ADVEC_SUBDEV_WDT:
+                       adv_ec_data->sub_dev_mask |= BIT(sub_dev[i]);
+                       break;
+               default:
+                       dev_err(adv_ec_data->dev, "invalid prop value(%d)!\n",
+                               sub_dev[i]);
+               }
+       }
+       dev_info(adv_ec_data->dev, "sub-dev mask = 0x%x\n", adv_ec_data->sub_dev_mask);
+
+       return 0;
+}
+
+static int adv_ec_probe(struct platform_device *pdev)
+{
+       int ret, i;
+       struct device *dev = &pdev->dev;
+       struct adv_ec_platform_data *adv_ec_data;
+
+       adv_ec_data = devm_kzalloc(dev, sizeof(struct adv_ec_platform_data), GFP_KERNEL);
+       if (!adv_ec_data)
+               return -ENOMEM;
+
+       dev_set_drvdata(dev, adv_ec_data);
+       adv_ec_data->dev = dev;
+
+       ret = adv_ec_init_ec_data(adv_ec_data);
+       if (ret)
+               goto err_init_data;
+
+       ret = adv_ec_parse_prop(adv_ec_data);
+       if (ret)
+               goto err_prop;
+
+       /* check whether this EC has the following subdevices. */
+       for (i = 0; i < ARRAY_SIZE(adv_ec_sub_cells); i++) {
+               if (adv_ec_data->sub_dev_mask & BIT(i)) {
+                       ret = mfd_add_hotplug_devices(dev, &adv_ec_sub_cells[i], 1);
+                       dev_info(adv_ec_data->dev, "mfd_add_hotplug_devices[%d] %s\n", i,
+                               adv_ec_sub_cells[i].name);
+                       if (ret)
+                               dev_err(dev, "failed to add %s subdevice: %d\n",
+                                       adv_ec_sub_cells[i].name, ret);
+               }
+       }
+
+       dev_info(adv_ec_data->dev, "Advantech EC probe done");
+
+       return 0;
+
+err_prop:
+       dev_err(dev, "failed to probe\n");
+
+err_init_data:
+       mutex_destroy(&adv_ec_data->lock);
+
+       dev_err(dev, "failed to init data\n");
+
+       return ret;
+}
+
+static int adv_ec_remove(struct platform_device *pdev)
+{
+       struct adv_ec_platform_data *adv_ec_data;
+
+       adv_ec_data = dev_get_drvdata(&pdev->dev);
+
+       mutex_destroy(&adv_ec_data->lock);
+
+       mfd_remove_devices(&pdev->dev);
+
+       return 0;
+}
+
+static const struct of_device_id adv_ec_of_match[] __maybe_unused = {
+       {
+               .compatible = "advantech,ahc1ec0",
+       },
+       {}
+};
+MODULE_DEVICE_TABLE(of, adv_ec_of_match);
+
+static const struct acpi_device_id adv_ec_acpi_match[] __maybe_unused = {
+       {"AHC1EC0", 0},
+       {},
+};
+MODULE_DEVICE_TABLE(acpi, adv_ec_acpi_match);
+
+static struct platform_driver adv_ec_driver = {
+       .driver = {
+               .name = DRV_NAME,
+               .of_match_table = of_match_ptr(adv_ec_of_match),
+               .acpi_match_table = ACPI_PTR(adv_ec_acpi_match),
+       },
+       .probe = adv_ec_probe,
+       .remove = adv_ec_remove,
+};
+module_platform_driver(adv_ec_driver);
+
+MODULE_LICENSE("GPL");
+MODULE_ALIAS("platform:" DRV_NAME);
+MODULE_DESCRIPTION("Advantech Embedded Controller core driver.");
+MODULE_AUTHOR("Campion Kang <[email protected]>");
+MODULE_AUTHOR("Jianfeng Dai <[email protected]>");
+MODULE_VERSION("1.0");
include/linux/mfd/ahc1ec0.h new file +276 -1
new file mode 100644
index 000000000000..1b01e10c1fef
@@ -0,0 +1,276 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+#ifndef __LINUX_MFD_AHC1EC0_H
+#define __LINUX_MFD_AHC1EC0_H
+
+#include <linux/device.h>
+
+#define EC_COMMAND_PORT             0x29A /* EC I/O command port */
+#define EC_STATUS_PORT              0x299 /* EC I/O data port */
+
+#define EC_RETRY_UDELAY              200 /* EC command retry delay in microseconds */
+#define EC_MAX_TIMEOUT_COUNT        5000 /* EC command max retry count */
+#define EC_COMMAND_BIT_OBF          0x01 /* Bit 0 is for OBF ready (Output buffer full) */
+#define EC_COMMAND_BIT_IBF          0x02 /* Bit 1 is for IBF ready (Input buffer full) */
+
+/* Analog to digital converter command */
+#define EC_AD_INDEX_WRITE   0x15 /* Write ADC port number into index */
+#define EC_AD_LSB_READ      0x16 /* Read ADC LSB value from ADC port */
+#define EC_AD_MSB_READ      0x1F /* Read ADC MSB value from ADC port */
+
+/* Voltage device ID */
+#define EC_DID_SMBOEM0      0x28 /* SMBUS/I2C. Smbus channel 0 */
+#define EC_DID_CMOSBAT      0x50 /* CMOS coin battery voltage */
+#define EC_DID_CMOSBAT_X2   0x51 /* CMOS coin battery voltage*2 */
+#define EC_DID_CMOSBAT_X10  0x52 /* CMOS coin battery voltage*10 */
+#define EC_DID_5VS0         0x56 /* 5VS0 voltage */
+#define EC_DID_5VS0_X2      0x57 /* 5VS0 voltage*2 */
+#define EC_DID_5VS0_X10     0x58 /* 5VS0 voltage*10 */
+#define EC_DID_5VS5         0x59 /* 5VS5 voltage */
+#define EC_DID_5VS5_X2      0x5A /* 5VS5 voltage*2 */
+#define EC_DID_5VS5_X10     0x5B /* 5VS5 voltage*10 */
+#define EC_DID_12VS0        0x62 /* 12VS0 voltage */
+#define EC_DID_12VS0_X2     0x63 /* 12VS0 voltage*2 */
+#define EC_DID_12VS0_X10    0x64 /* 12VS0 voltage*10 */
+#define EC_DID_VCOREA       0x65 /* CPU A core voltage */
+#define EC_DID_VCOREA_X2    0x66 /* CPU A core voltage*2 */
+#define EC_DID_VCOREA_X10   0x67 /* CPU A core voltage*10 */
+#define EC_DID_VCOREB       0x68 /* CPU B core voltage */
+#define EC_DID_VCOREB_X2    0x69 /* CPU B core voltage*2 */
+#define EC_DID_VCOREB_X10   0x6A /* CPU B core voltage*10 */
+#define EC_DID_DC           0x6B /* ADC. onboard voltage */
+#define EC_DID_DC_X2        0x6C /* ADC. onboard voltage*2 */
+#define EC_DID_DC_X10       0x6D /* ADC. onboard voltage*10 */
+
+/* Current device ID */
+#define EC_DID_CURRENT              0x74
+
+/* ACPI commands */
+#define EC_ACPI_RAM_READ            0x80
+#define EC_ACPI_RAM_WRITE           0x81
+
+/*
+ *  Dynamic control table commands
+ *  The table includes HW pin number, Device ID, and Pin polarity
+ */
+#define EC_TBL_WRITE_ITEM           0x20
+#define EC_TBL_GET_PIN              0x21
+#define EC_TBL_GET_DEVID            0x22
+#define EC_MAX_TBL_NUM              32
+
+/* LED Device ID table */
+#define EC_DID_LED_RUN              0xE1
+#define EC_DID_LED_ERR              0xE2
+#define EC_DID_LED_SYS_RECOVERY     0xE3
+#define EC_DID_LED_D105_G           0xE4
+#define EC_DID_LED_D106_G           0xE5
+#define EC_DID_LED_D107_G           0xE6
+
+/* LED control HW RAM address 0xA0-0xAF */
+#define EC_HWRAM_LED_BASE_ADDR      0xA0
+#define EC_HWRAM_LED_PIN(N)         (EC_HWRAM_LED_BASE_ADDR + (4 * (N))) // N:0-3
+#define EC_HWRAM_LED_CTRL_HIBYTE(N) (EC_HWRAM_LED_BASE_ADDR + (4 * (N)) + 1)
+#define EC_HWRAM_LED_CTRL_LOBYTE(N) (EC_HWRAM_LED_BASE_ADDR + (4 * (N)) + 2)
+#define EC_HWRAM_LED_DEVICE_ID(N)   (EC_HWRAM_LED_BASE_ADDR + (4 * (N)) + 3)
+
+/* LED control bit */
+#define LED_CTRL_ENABLE_BIT()           BIT(4)
+#define LED_CTRL_INTCTL_BIT()           BIT(5)
+#define LED_CTRL_LEDBIT_MASK            (0x03FF << 6)
+#define LED_CTRL_POLARITY_MASK          (0x000F << 0)
+#define LED_CTRL_INTCTL_EXTERNAL        0
+#define LED_CTRL_INTCTL_INTERNAL        1
+
+#define LED_DISABLE  0x0
+#define LED_ON       0x1
+#define LED_FAST     0x3
+#define LED_NORMAL   0x5
+#define LED_SLOW     0x7
+#define LED_MANUAL   0xF
+
+#define LED_CTRL_LEDBIT_DISABLE        0x0000
+#define LED_CTRL_LEDBIT_ON             0x03FF
+#define LED_CTRL_LEDBIT_FAST   0x02AA
+#define LED_CTRL_LEDBIT_NORMAL 0x0333
+#define LED_CTRL_LEDBIT_SLOW   0x03E0
+
+/* Get the device name */
+#define AMI_ADVANTECH_BOARD_ID_LENGTH  32
+
+/*
+ * Advantech Embedded Controller watchdog commands
+ * EC can send multi-stage watchdog event. System can setup watchdog event
+ * independently to make up event sequence.
+ */
+#define EC_COMMANS_PORT_IBF_MASK       0x02
+#define EC_RESET_EVENT                         0x04
+#define        EC_WDT_START                            0x28
+#define        EC_WDT_STOP                                     0x29
+#define        EC_WDT_RESET                            0x2A
+#define        EC_WDT_BOOTTMEWDT_STOP          0x2B
+
+#define EC_HW_RAM                                      0x89
+
+#define EC_EVENT_FLAG                          0x57
+#define EC_ENABLE_DELAY_H                      0x58
+#define EC_ENABLE_DELAY_L                      0x59
+#define EC_POWER_BTN_TIME_H                    0x5A
+#define EC_POWER_BTN_TIME_L                    0x5B
+#define EC_RESET_DELAY_TIME_H          0x5E
+#define EC_RESET_DELAY_TIME_L          0x5F
+#define EC_PIN_DELAY_TIME_H                    0x60
+#define EC_PIN_DELAY_TIME_L                    0x61
+#define EC_SCI_DELAY_TIME_H                    0x62
+#define EC_SCI_DELAY_TIME_L                    0x63
+
+/* EC ACPI commands */
+#define EC_ACPI_DATA_READ                      0x80
+#define EC_ACPI_DATA_WRITE                     0x81
+
+/* Brightness ACPI Addr */
+#define BRIGHTNESS_ACPI_ADDR           0x50
+
+/* EC HW RAM commands */
+#define EC_HW_EXTEND_RAM_READ          0x86
+#define EC_HW_EXTEND_RAM_WRITE         0x87
+#define        EC_HW_RAM_READ                          0x88
+#define EC_HW_RAM_WRITE                                0x89
+
+/* EC Smbus commands */
+#define EC_SMBUS_CHANNEL_SET           0x8A     /* Set selector number (SMBUS channel) */
+#define EC_SMBUS_ENABLE_I2C                    0x8C     /* Enable channel I2C */
+#define EC_SMBUS_DISABLE_I2C           0x8D     /* Disable channel I2C */
+
+/* Smbus transmit protocol */
+#define EC_SMBUS_PROTOCOL                      0x00
+
+/* SMBUS status */
+#define EC_SMBUS_STATUS                                0x01
+
+/* SMBUS device slave address (bit0 must be 0) */
+#define EC_SMBUS_SLV_ADDR                      0x02
+
+/* SMBUS device command */
+#define EC_SMBUS_CMD                           0x03
+
+/* 0x04-0x24 Data In read process, return data are stored in this address */
+#define EC_SMBUS_DATA                          0x04
+
+#define EC_SMBUS_DAT_OFFSET(n) (EC_SMBUS_DATA + (n))
+
+/* SMBUS channel selector (0-4) */
+#define EC_SMBUS_CHANNEL                       0x2B
+
+/* EC SMBUS transmit Protocol code */
+#define SMBUS_QUICK_WRITE                      0x02 /* Write Quick Command */
+#define SMBUS_QUICK_READ                       0x03 /* Read Quick Command */
+#define SMBUS_BYTE_SEND                                0x04 /* Send Byte */
+#define SMBUS_BYTE_RECEIVE                     0x05 /* Receive Byte */
+#define SMBUS_BYTE_WRITE                       0x06 /* Write Byte */
+#define SMBUS_BYTE_READ                                0x07 /* Read Byte */
+#define SMBUS_WORD_WRITE                       0x08 /* Write Word */
+#define SMBUS_WORD_READ                                0x09 /* Read Word */
+#define SMBUS_BLOCK_WRITE                      0x0A /* Write Block */
+#define SMBUS_BLOCK_READ                       0x0B /* Read Block */
+#define SMBUS_PROC_CALL                                0x0C /* Process Call */
+#define SMBUS_BLOCK_PROC_CALL          0x0D /* Write Block-Read Block Process Call */
+#define SMBUS_I2C_READ_WRITE           0x0E /* I2C block Read-Write */
+#define SMBUS_I2C_WRITE_READ           0x0F /* I2C block Write-Read */
+
+/* GPIO control commands */
+#define EC_GPIO_INDEX_WRITE                    0x10
+#define EC_GPIO_STATUS_READ                    0x11
+#define EC_GPIO_STATUS_WRITE           0x12
+#define EC_GPIO_DIR_READ                       0x1D
+#define EC_GPIO_DIR_WRITE                      0x1E
+
+/* One Key Recovery commands */
+#define EC_ONE_KEY_FLAG                                0x9C
+
+/* ASG OEM commands */
+#define EC_ASG_OEM                                     0xEA
+#define EC_ASG_OEM_READ                                0x00
+#define EC_ASG_OEM_WRITE                       0x01
+#define EC_OEM_POWER_STATUS_VIN1       0X10
+#define EC_OEM_POWER_STATUS_VIN2       0X11
+#define EC_OEM_POWER_STATUS_BAT1       0X12
+#define EC_OEM_POWER_STATUS_BAT2       0X13
+
+/* GPIO DEVICE ID */
+#define EC_DID_ALTGPIO_0                       0x10    /* 0x10 AltGpio0 User define gpio */
+#define EC_DID_ALTGPIO_1                       0x11    /* 0x11 AltGpio1 User define gpio */
+#define EC_DID_ALTGPIO_2                       0x12    /* 0x12 AltGpio2 User define gpio */
+#define EC_DID_ALTGPIO_3                       0x13    /* 0x13 AltGpio3 User define gpio */
+#define EC_DID_ALTGPIO_4                       0x14    /* 0x14 AltGpio4 User define gpio */
+#define EC_DID_ALTGPIO_5                       0x15    /* 0x15 AltGpio5 User define gpio */
+#define EC_DID_ALTGPIO_6                       0x16    /* 0x16 AltGpio6 User define gpio */
+#define EC_DID_ALTGPIO_7                       0x17    /* 0x17 AltGpio7 User define gpio */
+
+/* Lmsensor Chip Register */
+#define NSLM96163_CHANNEL                      0x02
+
+/* NS_LM96163 address 0x98 */
+#define NSLM96163_ADDR                         0x98
+
+/* LM96163 index(0x00) Local Temperature (Signed MSB) */
+#define NSLM96163_LOC_TEMP                     0x00
+
+/* HWMON registers */
+#define INA266_REG_VOLTAGE          0x02    /* 1.25mV */
+#define INA266_REG_POWER            0x03    /* 25mW */
+#define INA266_REG_CURRENT          0x04    /* 1mA */
+
+struct ec_hw_pin_table {
+       unsigned int vbat[2];
+       unsigned int v5[2];
+       unsigned int v12[2];
+       unsigned int vcore[2];
+       unsigned int vdc[2];
+       unsigned int ec_current[2];
+       unsigned int power[2];
+};
+
+struct ec_dynamic_table {
+       unsigned char device_id;
+       unsigned char hw_pin_num;
+};
+
+struct ec_smbuso_em0 {
+       unsigned char hw_pin_num;
+};
+
+struct pled_hw_pin_tbl {
+       unsigned int pled[6];
+};
+
+struct adv_ec_platform_data {
+       char *bios_product_name;
+       int sub_dev_nb;
+       u32 sub_dev_mask;
+       struct mutex lock;
+       struct device *dev;
+       struct class *adv_ec_class;
+
+       struct ec_dynamic_table *dym_tbl;
+};
+
+int read_ad_value(struct adv_ec_platform_data *adv_ec_data, unsigned char hwpin,
+                       unsigned char multi);
+int read_acpi_value(struct adv_ec_platform_data *adv_ec_data, unsigned char addr,
+                       unsigned char *pvalue);
+int write_acpi_value(struct adv_ec_platform_data *adv_ec_data, unsigned char addr,
+                       unsigned char value);
+int read_hw_ram(struct adv_ec_platform_data *adv_ec_data, unsigned char addr,
+                       unsigned char *data);
+int write_hw_ram(struct adv_ec_platform_data *adv_ec_data, unsigned char addr,
+                       unsigned char data);
+int write_hwram_command(struct adv_ec_platform_data *adv_ec_data, unsigned char data);
+int read_gpio_status(struct adv_ec_platform_data *adv_ec_data, unsigned char PinNumber,
+                       unsigned char *pvalue);
+int write_gpio_status(struct adv_ec_platform_data *adv_ec_data, unsigned char PinNumber,
+                       unsigned char value);
+int read_gpio_dir(struct adv_ec_platform_data *adv_ec_data, unsigned char PinNumber,
+                       unsigned char *pvalue);
+int write_gpio_dir(struct adv_ec_platform_data *adv_ec_data, unsigned char PinNumber,
+                       unsigned char value);
+
+#endif /* __LINUX_MFD_AHC1EC0_H */
--
Quoted 2 lines

2.17.1

#7 lore raw

Re: [PATCH v6 4/6] mfd: ahc1ec0: Add support for Advantech embedded controller

Quoted 23 lines · AceLan Kao

Campion Kang <[email protected]> 於 2021年1月18日 週一 下午8:37寫道:

Quoted 22 lines

AHC1EC0 is the embedded controller driver for Advantech industrial products. This provides sub-devices such as hwmon and watchdog, and also expose functions for sub-devices to read/write the value to embedded controller.

Changed since V5:
- Kconfig: add "AHC1EC0" string to clearly define the EC name
- fix the code according to reviewer's suggestion
- remove unnecessary header files
- change the structure name to lower case, align with others
naming

---
 drivers/mfd/Kconfig         |  10 +
 drivers/mfd/Makefile        |   2 +
 drivers/mfd/ahc1ec0.c       | 808 ++++++++++++++++++++++++++++++++++++
 include/linux/mfd/ahc1ec0.h | 276 ++++++++++++
 4 files changed, 1096 insertions(+)
 create mode 100644 drivers/mfd/ahc1ec0.c
 create mode 100644 include/linux/mfd/ahc1ec0.h

[...]

NB: Snipped 1000 lines.

Quoted 1 line

Would you be kind enough to snip your replies please AceLan?

It would have the benefit of saving a lot of people a little time.

TIA.

Signature · 4 lines
Lee Jones [李琼斯]
Senior Technical Lead - Developer Services
Linaro.org │ Open source software for Arm SoCs
Follow Linaro: Facebook | Twitter | Blog
#8 lore raw

Re: [PATCH v6 4/6] mfd: ahc1ec0: Add support for Advantech embedded controller

In reply to Campion Kang
Quoted 1 line · Campion Kang

AHC1EC0 is the embedded controller driver for Advantech industrial

It would be nice to have the model number in the subject line.

Drop "driver".

Quoted 2 lines

products. This provides sub-devices such as hwmon and watchdog, and also

"HWMON and Watchdog"

Quoted 1 line

expose functions for sub-devices to read/write the value to embedded

"exposes"

Quoted 24 lines

controller.

Changed since V5:
- Kconfig: add "AHC1EC0" string to clearly define the EC name
- fix the code according to reviewer's suggestion
- remove unnecessary header files
- change the structure name to lower case, align with others
naming

---
 drivers/mfd/Kconfig         |  10 +
 drivers/mfd/Makefile        |   2 +
 drivers/mfd/ahc1ec0.c       | 808 ++++++++++++++++++++++++++++++++++++
 include/linux/mfd/ahc1ec0.h | 276 ++++++++++++
 4 files changed, 1096 insertions(+)
 create mode 100644 drivers/mfd/ahc1ec0.c
 create mode 100644 include/linux/mfd/ahc1ec0.h
drivers/mfd/Kconfig +0 -0
index bdfce7b15621..7d5fb5c17d9a 100644

drivers/mfd/Kconfig config MFD_INTEL_M10_BMC · line 2158

Quoted 5 lines
drivers/mfd/Kconfig +2 -0
@@ -2154,5 +2154,15 @@ config MFD_INTEL_M10_BMC
 	  additional drivers must be enabled in order to use the functionality
 	  of the device.
 
+config MFD_AHC1EC0
+	tristate "Advantech AHC1EC0 Embedded Controller Module"

Please remove "Module"

drivers/mfd/Kconfig config MFD_INTEL_M10_BMC · line 2165

Quoted 7 lines
drivers/mfd/Kconfig +7 -0
+	depends on X86
+	select MFD_CORE
+	help
+	  This is the core function that for Advantech EC drivers. It
+	  includes the sub-devices such as hwmon, watchdog, etc. And also
+	  provides expose functions for sub-devices to read/write the value
+	  to embedded controller.

"This provides core functionality for the Advantech AHC1EC0 Embedded Controller (EC) along with registration for HWMON and Watchdog sub-devices. It also provides read and write APIs for communication with the EC."

drivers/mfd/Makefile obj-$(CONFIG_MFD_KHADAS_MCU) += khadas-mcu.o · line 271

Quoted 11 lines
drivers/mfd/Kconfig +0 -0
 endmenu
 endif
drivers/mfd/Makefile +1 -0
index 14fdb188af02..a6af9d8825f4 100644
@@ -268,3 +268,5 @@ obj-$(CONFIG_MFD_KHADAS_MCU) 	+= khadas-mcu.o
 obj-$(CONFIG_SGI_MFD_IOC3)	+= ioc3.o
 obj-$(CONFIG_MFD_SIMPLE_MFD_I2C)	+= simple-mfd-i2c.o
 obj-$(CONFIG_MFD_INTEL_M10_BMC)   += intel-m10-bmc.o
+

You don't need to space this out.

drivers/mfd/ahc1ec0.c line 3

Quoted 10 lines
drivers/mfd/Makefile +1 -0
+obj-$(CONFIG_MFD_AHC1EC0)	+= ahc1ec0.o
drivers/mfd/ahc1ec0.c new file +3 -0
new file mode 100644
index 000000000000..015f4307a54e
@@ -0,0 +1,808 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Advantech embedded controller core driver AHC1EC0

Advantech AHC1EC0 Embedded Controller

drivers/mfd/ahc1ec0.c line 5

Quoted 1 line
drivers/mfd/ahc1ec0.c new file +1 -0
+ * Copyright 2020 Advantech IIoT Group

This is out of date.

drivers/mfd/ahc1ec0.c line 5

Quoted 1 line
drivers/mfd/ahc1ec0.c new file +1 -0
+ *

Drop this '\n' please.

Quoted 4 lines
drivers/mfd/ahc1ec0.c new file +4 -0
+ */
+
+#include <linux/acpi.h>
+#include <linux/delay.h>

drivers/mfd/ahc1ec0.c line 20

Quoted 10 lines
drivers/mfd/ahc1ec0.c new file +10 -0
+#include <linux/dmi.h>
+#include <linux/errno.h>
+#include <linux/mfd/ahc1ec0.h>
+#include <linux/mfd/core.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/of_platform.h>
+#include <linux/platform_device.h>
+
+#define DRV_NAME      "ahc1ec0"

Please don't do this. Just use the string in-place.

drivers/mfd/ahc1ec0.c line 30

Quoted 9 lines
drivers/mfd/ahc1ec0.c new file +9 -0
+enum {
+	ADVEC_SUBDEV_BRIGHTNESS = 0,
+	ADVEC_SUBDEV_EEPROM,
+	ADVEC_SUBDEV_GPIO,
+	ADVEC_SUBDEV_HWMON,
+	ADVEC_SUBDEV_LED,
+	ADVEC_SUBDEV_WDT,
+	ADVEC_SUBDEV_MAX,
+};

Are these arbitrary?

Quoted 60 lines
drivers/mfd/ahc1ec0.c new file +59 -0
+/* Wait IBF (Input Buffer Full) clear */
+static int ec_wait_write(void)
+{
+	int i;
+
+	for (i = 0; i < EC_MAX_TIMEOUT_COUNT; i++) {
+		if ((inb(EC_COMMAND_PORT) & EC_COMMAND_BIT_IBF) == 0)
+			return 0;
+
+		udelay(EC_RETRY_UDELAY);
+	}
+
+	return -ETIMEDOUT;
+}
 
+/* Wait OBF (Output Buffer Full) data ready */
+static int ec_wait_read(void)
+{
+	int i;
+
+	for (i = 0; i < EC_MAX_TIMEOUT_COUNT; i++) {
+		if ((inb(EC_COMMAND_PORT) & EC_COMMAND_BIT_OBF) != 0)
+			return 0;
+
+		udelay(EC_RETRY_UDELAY);
+	}
+
+	return -ETIMEDOUT;
+}
+
+/* Read data from EC HW RAM, the process is the following:
+ * Step 0. Wait IBF clear to send command
+ * Step 1. Send read command to EC command port
+ * Step 2. Wait IBF clear that means command is got by EC
+ * Step 3. Send read address to EC data port
+ * Step 4. Wait OBF data ready
+ * Step 5. Get data from EC data port
+ */
+int read_hw_ram(struct adv_ec_platform_data *adv_ec_data, unsigned char addr, unsigned char *data)
+{
+	int ret;
+
+	mutex_lock(&adv_ec_data->lock);
+
+	ret = ec_wait_write();
+	if (ret)
+		goto error;
+	outb(EC_HW_RAM_READ, EC_COMMAND_PORT);
+
+	ret = ec_wait_write();
+	if (ret)
+		goto error;
+	outb(addr, EC_STATUS_PORT);
+
+	ret = ec_wait_read();
+	if (ret)
+		goto error;
+	*data = inb(EC_STATUS_PORT);
+
+	mutex_unlock(&adv_ec_data->lock);

drivers/mfd/ahc1ec0.c line 101

Quoted 10 lines
drivers/mfd/ahc1ec0.c new file +10 -0
+
+	return ret;
+
+error:
+	mutex_unlock(&adv_ec_data->lock);
+	dev_err(adv_ec_data->dev, "%s: Wait for IBF or OBF too long. line: %d\n", __func__,
+	       __LINE__);
+
+	return ret;
+}

EXPORT?

Quoted 32 lines
drivers/mfd/ahc1ec0.c new file +32 -0
+/* Write data to EC HW RAM
+ * Step 0. Wait IBF clear to send command
+ * Step 1. Send write command to EC command port
+ * Step 2. Wait IBF clear that means command is got by EC
+ * Step 3. Send write address to EC data port
+ * Step 4. Wait IBF clear that means command is got by EC
+ * Step 5. Send data to EC data port
+ */
+int write_hw_ram(struct adv_ec_platform_data *adv_ec_data, unsigned char addr, unsigned char data)
+{
+	int ret;
+
+	mutex_lock(&adv_ec_data->lock);
+
+	ret = ec_wait_write();
+	if (ret)
+		goto error;
+	outb(EC_HW_RAM_WRITE, EC_COMMAND_PORT);
+
+	ret = ec_wait_write();
+	if (ret)
+		goto error;
+	outb(addr, EC_STATUS_PORT);
+
+	ret = ec_wait_write();
+	if (ret)
+		goto error;
+	outb(data, EC_STATUS_PORT);
+
+	mutex_unlock(&adv_ec_data->lock);
+
+	return 0;

drivers/mfd/ahc1ec0.c line 144

Quoted 10 lines
drivers/mfd/ahc1ec0.c new file +10 -0
+
+error:
+	mutex_unlock(&adv_ec_data->lock);
+
+	dev_err(adv_ec_data->dev, "%s: Wait for IBF or OBF too long. line: %d\n", __func__,
+	       __LINE__);
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(write_hw_ram);

If you're going to export these, they should be in their own namespace.

Quoted 8 lines
drivers/mfd/ahc1ec0.c new file +8 -0
+/* Get dynamic control table */
+static int adv_get_dynamic_tab(struct adv_ec_platform_data *adv_ec_data)
+{
+	int i, ret;
+	unsigned char pin_tmp, device_id;
+
+	mutex_lock(&adv_ec_data->lock);
+

drivers/mfd/ahc1ec0.c line 163

Quoted 10 lines
drivers/mfd/ahc1ec0.c new file +10 -0
+	for (i = 0; i < EC_MAX_TBL_NUM; i++) {
+		adv_ec_data->dym_tbl[i].device_id = 0xff;
+		adv_ec_data->dym_tbl[i].hw_pin_num = 0xff;
+	}
+
+	for (i = 0; i < EC_MAX_TBL_NUM; i++) {
+		ret = ec_wait_write();
+		if (ret) {
+			dev_dbg(adv_ec_data->dev, "%s: ec_wait_write. line: %d\n", __func__,
+				__LINE__);

Please remove all of this debug code.

drivers/mfd/ahc1ec0.c line 166

Quoted 3 lines
drivers/mfd/ahc1ec0.c new file +3 -0
+			goto error;
+		}
+		outb(EC_TBL_WRITE_ITEM, EC_COMMAND_PORT);

More basic commentary is required throughout I think.

Quoted 11 lines
drivers/mfd/ahc1ec0.c new file +11 -0
+		ret = ec_wait_write();
+		if (ret) {
+			dev_dbg(adv_ec_data->dev, "%s: ec_wait_write. line: %d\n", __func__,
+				__LINE__);
+			goto error;
+		}
+		outb(i, EC_STATUS_PORT);
+
+		ret = ec_wait_read();
+		if (ret) {
+			dev_dbg(adv_ec_data->dev, "%s: ec_wait_read. line: %d\n", __func__,

drivers/mfd/ahc1ec0.c line 188

Quoted 10 lines
drivers/mfd/ahc1ec0.c new file +10 -0
+				__LINE__);
+			goto error;
+		}
+
+		/*
+		 *  If item is defined, EC will return item number.
+		 *  If table item is not defined, EC will return 0xFF.
+		 */
+		pin_tmp = inb(EC_STATUS_PORT);
+		if (pin_tmp == 0xff) {

Please define all magic numbers.

Quoted 44 lines
drivers/mfd/ahc1ec0.c new file +44 -0
+			dev_dbg(adv_ec_data->dev, "%s: inb(EC_STATUS_PORT)=0x%02x != 0xff.\n",
+				__func__, pin_tmp);
+			goto pass;
+		}
+
+		ret = ec_wait_write();
+		if (ret) {
+			dev_dbg(adv_ec_data->dev, "%s: ec_wait_write. line: %d\n", __func__,
+				__LINE__);
+			goto error;
+		}
+		outb(EC_TBL_GET_PIN, EC_COMMAND_PORT);
+
+		ret = ec_wait_read();
+		if (ret) {
+			dev_dbg(adv_ec_data->dev, "%s: ec_wait_read. line: %d\n", __func__,
+				__LINE__);
+			goto error;
+		}
+		pin_tmp = inb(EC_STATUS_PORT) & 0xff;
+		if (pin_tmp == 0xff) {
+			dev_dbg(adv_ec_data->dev, "%s: pin_tmp(0x%02X). line: %d\n", __func__,
+				pin_tmp, __LINE__);
+			goto pass;
+		}
+
+		ret = ec_wait_write();
+		if (ret)
+			goto error;
+		outb(EC_TBL_GET_DEVID, EC_COMMAND_PORT);
+
+		ret = ec_wait_read();
+		if (ret) {
+			dev_dbg(adv_ec_data->dev, "%s: ec_wait_read. line: %d\n", __func__,
+				__LINE__);
+			goto error;
+		}
+		device_id = inb(EC_STATUS_PORT) & 0xff;
+
+		dev_dbg(adv_ec_data->dev, "%s: device_id=0x%02X. line: %d\n", __func__,
+			device_id, __LINE__);
+
+		adv_ec_data->dym_tbl[i].device_id = device_id;
+		adv_ec_data->dym_tbl[i].hw_pin_num = pin_tmp;

drivers/mfd/ahc1ec0.c line 242

Quoted 10 lines
drivers/mfd/ahc1ec0.c new file +10 -0
+	}
+
+pass:
+	mutex_unlock(&adv_ec_data->lock);
+	return 0;
+
+error:
+	mutex_unlock(&adv_ec_data->lock);
+	dev_err(adv_ec_data->dev, "%s: Wait for IBF or OBF too long. line: %d\n",
+		__func__, __LINE__);

Remove all _func_ and _LINE_ info. It's seldom helpful.

drivers/mfd/ahc1ec0.c line 246

Quoted 4 lines
drivers/mfd/ahc1ec0.c new file +4 -0
+	return ret;
+}
+
+int read_ad_value(struct adv_ec_platform_data *adv_ec_data, unsigned char hwpin,

What is 'ad'?

Quoted 46 lines
drivers/mfd/ahc1ec0.c new file +46 -0
+		unsigned char multi)
+{
+	int ret;
+	u32 ret_val;
+	unsigned int LSB, MSB;
+
+	mutex_lock(&adv_ec_data->lock);
+	ret = ec_wait_write();
+	if (ret)
+		goto error;
+	outb(EC_AD_INDEX_WRITE, EC_COMMAND_PORT);
+
+	ret = ec_wait_write();
+	if (ret)
+		goto error;
+	outb(hwpin, EC_STATUS_PORT);
+
+	ret = ec_wait_read();
+	if (ret)
+		goto error;
+
+	if (inb(EC_STATUS_PORT) == 0xff) {
+		mutex_unlock(&adv_ec_data->lock);
+		return -1;
+	}
+
+	ret = ec_wait_write();
+	if (ret)
+		goto error;
+	outb(EC_AD_LSB_READ, EC_COMMAND_PORT);
+
+	ret = ec_wait_read();
+	if (ret)
+		goto error;
+	LSB = inb(EC_STATUS_PORT);
+
+	ret = ec_wait_write();
+	if (ret)
+		goto error;
+	outb(EC_AD_MSB_READ, EC_COMMAND_PORT);
+
+	ret = ec_wait_read();
+	if (ret)
+		goto error;
+	MSB = inb(EC_STATUS_PORT);
+	ret_val = ((MSB << 8) | LSB) & 0x03FF;

drivers/mfd/ahc1ec0.c line 302

Quoted 10 lines
drivers/mfd/ahc1ec0.c new file +10 -0
+	ret_val = ret_val * multi * 100;
+
+	mutex_unlock(&adv_ec_data->lock);
+	return ret_val;
+
+error:
+	mutex_unlock(&adv_ec_data->lock);
+
+	dev_warn(adv_ec_data->dev, "%s: Wait for IBF or OBF too long. line: %d\n", __func__,
+		__LINE__);

dev_err()

Quoted 31 lines
drivers/mfd/ahc1ec0.c new file +31 -0
+	return ret;
+}
+EXPORT_SYMBOL_GPL(read_ad_value);
+
+int read_acpi_value(struct adv_ec_platform_data *adv_ec_data, unsigned char addr,
+		unsigned char *pvalue)
+{
+	int ret;
+	unsigned char value;
+
+	mutex_lock(&adv_ec_data->lock);
+
+	ret = ec_wait_write();
+	if (ret)
+		goto error;
+	outb(EC_ACPI_RAM_READ, EC_COMMAND_PORT);
+
+	ret = ec_wait_write();
+	if (ret)
+		goto error;
+	outb(addr, EC_STATUS_PORT);
+
+	ret = ec_wait_read();
+	if (ret)
+		goto error;
+	value = inb(EC_STATUS_PORT);
+	*pvalue = value;
+
+	mutex_unlock(&adv_ec_data->lock);
+
+	return 0;

drivers/mfd/ahc1ec0.c line 344

Quoted 10 lines
drivers/mfd/ahc1ec0.c new file +10 -0
+
+error:
+	mutex_unlock(&adv_ec_data->lock);
+
+	dev_warn(adv_ec_data->dev, "%s: Wait for IBF or OBF too long. line: %d\n", __func__,
+		__LINE__);
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(read_acpi_value);

Namespace.

Quoted 23 lines
drivers/mfd/ahc1ec0.c new file +23 -0
+int write_acpi_value(struct adv_ec_platform_data *adv_ec_data, unsigned char addr,
+		unsigned char value)
+{
+	int ret;
+
+	mutex_lock(&adv_ec_data->lock);
+
+	ret = ec_wait_write();
+	if (ret)
+		goto error;
+	outb(EC_ACPI_DATA_WRITE, EC_COMMAND_PORT);
+
+	ret = ec_wait_write();
+	if (ret)
+		goto error;
+	outb(addr, EC_STATUS_PORT);
+
+	ret = ec_wait_write();
+	if (ret)
+		goto error;
+	outb(value, EC_STATUS_PORT);
+
+	mutex_unlock(&adv_ec_data->lock);

drivers/mfd/ahc1ec0.c line 378

Quoted 10 lines
drivers/mfd/ahc1ec0.c new file +10 -0
+	return 0;
+
+error:
+	mutex_unlock(&adv_ec_data->lock);
+
+	dev_warn(adv_ec_data->dev, "%s: Wait for IBF or OBF too long. line: %d\n", __func__,
+		__LINE__);
+
+	return ret;
+}

EXPORT?

I think this API (i.e. all of the functions above) should be moved into drivers/platform. They really don't have a place in MFD.

Quoted 191 lines
drivers/mfd/ahc1ec0.c new file +191 -0
+int read_gpio_status(struct adv_ec_platform_data *adv_ec_data, unsigned char PinNumber,
+		unsigned char *pvalue)
+{
+	int ret;
+
+	unsigned char gpio_status_value;
+
+	mutex_lock(&adv_ec_data->lock);
+
+	ret = ec_wait_write();
+	if (ret)
+		goto error;
+	outb(EC_GPIO_INDEX_WRITE, EC_COMMAND_PORT);
+
+	ret = ec_wait_write();
+	if (ret)
+		goto error;
+	outb(PinNumber, EC_STATUS_PORT);
+
+	ret = ec_wait_read();
+	if (ret)
+		goto error;
+
+	if (inb(EC_STATUS_PORT) == 0xff) {
+		dev_err(adv_ec_data->dev, "%s: Read Pin Number error!!\n", __func__);
+		mutex_unlock(&adv_ec_data->lock);
+		return -1;
+	}
+
+	ret = ec_wait_write();
+	if (ret)
+		goto error;
+	outb(EC_GPIO_STATUS_READ, EC_COMMAND_PORT);
+
+	ret = ec_wait_read();
+	if (ret)
+		goto error;
+	gpio_status_value = inb(EC_STATUS_PORT);
+
+	*pvalue = gpio_status_value;
+	mutex_unlock(&adv_ec_data->lock);
+	return 0;
+
+error:
+	mutex_unlock(&adv_ec_data->lock);
+
+	dev_warn(adv_ec_data->dev, "%s: Wait for IBF or OBF too long. line: %d\n", __func__,
+		__LINE__);
+	return ret;
+}
+
+int write_gpio_status(struct adv_ec_platform_data *adv_ec_data, unsigned char PinNumber,
+		unsigned char value)
+{
+	int ret;
+
+	mutex_lock(&adv_ec_data->lock);
+
+	ret = ec_wait_write();
+	if (ret)
+		goto error;
+	outb(EC_GPIO_INDEX_WRITE, EC_COMMAND_PORT);
+
+	ret = ec_wait_write();
+	if (ret)
+		goto error;
+	outb(PinNumber, EC_STATUS_PORT);
+
+	ret = ec_wait_read();
+	if (ret)
+		goto error;
+
+	if (inb(EC_STATUS_PORT) == 0xff) {
+		mutex_unlock(&adv_ec_data->lock);
+		dev_err(adv_ec_data->dev, "%s: Read Pin Number error!!\n", __func__);
+		return -1;
+	}
+
+	ret = ec_wait_write();
+	if (ret)
+		goto error;
+	outb(EC_GPIO_STATUS_WRITE, EC_COMMAND_PORT);
+
+	ret = ec_wait_write();
+	if (ret)
+		goto error;
+	outb(value, EC_STATUS_PORT);
+
+	mutex_unlock(&adv_ec_data->lock);
+	return 0;
+
+error:
+	mutex_unlock(&adv_ec_data->lock);
+	dev_err(adv_ec_data->dev, "%s: Wait for IBF or OBF too long. line: %d", __func__,
+		__LINE__);
+
+	return ret;
+}
+
+int read_gpio_dir(struct adv_ec_platform_data *adv_ec_data, unsigned char PinNumber,
+		unsigned char *pvalue)
+{
+	int ret;
+	unsigned char gpio_dir_value;
+
+	mutex_lock(&adv_ec_data->lock);
+
+	ret = ec_wait_write();
+	if (ret)
+		goto error;
+	outb(EC_GPIO_INDEX_WRITE, EC_COMMAND_PORT);
+
+	ret = ec_wait_write();
+	if (ret)
+		goto error;
+	outb(PinNumber, EC_STATUS_PORT);
+
+	ret = ec_wait_read();
+	if (ret)
+		goto error;
+
+	if (inb(EC_STATUS_PORT) == 0xff) {
+		mutex_unlock(&adv_ec_data->lock);
+		dev_err(adv_ec_data->dev, "%s: Read Pin Number error!! line: %d\n", __func__,
+			__LINE__);
+		return -1;
+	}
+
+	ret = ec_wait_write();
+	if (ret)
+		goto error;
+	outb(EC_GPIO_DIR_READ, EC_COMMAND_PORT);
+
+	ret = ec_wait_read();
+	if (ret)
+		goto error;
+	gpio_dir_value = inb(EC_STATUS_PORT);
+	*pvalue = gpio_dir_value;
+
+	mutex_unlock(&adv_ec_data->lock);
+	return 0;
+
+error:
+	mutex_unlock(&adv_ec_data->lock);
+
+	dev_warn(adv_ec_data->dev, "%s: Wait for IBF or OBF too long. line: %d\n", __func__,
+			__LINE__);
+
+	return ret;
+}
+
+int write_gpio_dir(struct adv_ec_platform_data *adv_ec_data, unsigned char PinNumber,
+		unsigned char value)
+{
+	int ret;
+
+	mutex_lock(&adv_ec_data->lock);
+
+	ret = ec_wait_write();
+	if (ret)
+		goto error;
+	outb(EC_GPIO_INDEX_WRITE, EC_COMMAND_PORT);
+
+	ret = ec_wait_write();
+	if (ret)
+		goto error;
+	outb(PinNumber, EC_STATUS_PORT);
+
+	ret = ec_wait_read();
+	if (ret)
+		goto error;
+
+	if (inb(EC_STATUS_PORT) == 0xff) {
+		mutex_unlock(&adv_ec_data->lock);
+		dev_warn(adv_ec_data->dev, "%s: Read Pin Number error!! line: %d\n", __func__,
+			__LINE__);
+
+		return -1;
+	}
+
+	ret = ec_wait_write();
+	if (ret)
+		goto error;
+	outb(EC_GPIO_DIR_WRITE, EC_COMMAND_PORT);
+
+	ret = ec_wait_write();
+	if (ret)
+		goto error;
+	outb(value, EC_STATUS_PORT);
+
+	mutex_unlock(&adv_ec_data->lock);

drivers/mfd/ahc1ec0.c line 580

Quoted 10 lines
drivers/mfd/ahc1ec0.c new file +10 -0
+	return 0;
+
+error:
+	mutex_unlock(&adv_ec_data->lock);
+
+	dev_warn(adv_ec_data->dev, "%s: Wait for IBF or OBF too long. line: %d\n", __func__,
+			__LINE__);
+
+	return ret;
+}

All of the GPIO functions above should move into drivers/gpio.

Quoted 42 lines
drivers/mfd/ahc1ec0.c new file +42 -0
+int write_hwram_command(struct adv_ec_platform_data *adv_ec_data, unsigned char data)
+{
+	int ret;
+
+	mutex_lock(&adv_ec_data->lock);
+
+	ret = ec_wait_write();
+	if (ret)
+		goto error;
+	outb(data, EC_COMMAND_PORT);
+
+	mutex_unlock(&adv_ec_data->lock);
+	return 0;
+
+error:
+	mutex_unlock(&adv_ec_data->lock);
+
+	dev_warn(adv_ec_data->dev, "%s: Wait for IBF or OBF too long. line: %d\n", __func__,
+			__LINE__);
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(write_hwram_command);
+
+static int adv_ec_get_productname(struct adv_ec_platform_data *adv_ec_data, char *product)
+{
+	const char *vendor, *device;
+	int length = 0;
+
+	/* Check it is Advantech board */
+	vendor = dmi_get_system_info(DMI_SYS_VENDOR);
+	if (memcmp(vendor, "Advantech", sizeof("Advantech")) != 0)
+		return -ENODEV;
+
+	/* Get product model name */
+	device = dmi_get_system_info(DMI_PRODUCT_NAME);
+	if (device) {
+		while ((device[length] != ' ')
+			&& (length < AMI_ADVANTECH_BOARD_ID_LENGTH))
+			length++;
+		memset(product, 0, AMI_ADVANTECH_BOARD_ID_LENGTH);
+		memmove(product, device, length);

drivers/mfd/ahc1ec0.c line 633

Quoted 10 lines
drivers/mfd/ahc1ec0.c new file +10 -0
+
+		dev_info(adv_ec_data->dev, "BIOS Product Name = %s\n", product);
+
+		return 0;
+	}
+
+	dev_warn(adv_ec_data->dev, "This device is not Advantech Board (%s)!\n", product);
+
+	return -ENODEV;
+}

These should go into drivers/platform.

Quoted 7 lines
drivers/mfd/ahc1ec0.c new file +7 -0
+static const struct mfd_cell adv_ec_sub_cells[] = {
+	{ .name = "adv-ec-brightness", },
+	{ .name = "adv-ec-eeprom", },
+	{ .name = "adv-ec-gpio", },
+	{ .name = "ahc1ec0-hwmon", },
+	{ .name = "adv-ec-led", },
+	{ .name = "ahc1ec0-wdt", },

drivers/mfd/ahc1ec0.c line 651

Quoted 10 lines
drivers/mfd/ahc1ec0.c new file +10 -0
+};
+
+static int adv_ec_init_ec_data(struct adv_ec_platform_data *adv_ec_data)
+{
+	int ret;
+
+	adv_ec_data->sub_dev_mask = 0;
+	adv_ec_data->sub_dev_nb = 0;
+	adv_ec_data->dym_tbl = NULL;
+	adv_ec_data->bios_product_name = NULL;

Why are pre-initialising these?

drivers/mfd/ahc1ec0.c line 661

Quoted 9 lines
drivers/mfd/ahc1ec0.c new file +9 -0
+	mutex_init(&adv_ec_data->lock);
+
+	/* Get product name */
+	adv_ec_data->bios_product_name =
+		devm_kzalloc(adv_ec_data->dev, AMI_ADVANTECH_BOARD_ID_LENGTH, GFP_KERNEL);
+	if (!adv_ec_data->bios_product_name)
+		return -ENOMEM;
+
+	memset(adv_ec_data->bios_product_name, 0, AMI_ADVANTECH_BOARD_ID_LENGTH);

Why are you doing this?

drivers/mfd/ahc1ec0.c line 671

Quoted 10 lines
drivers/mfd/ahc1ec0.c new file +10 -0
+	ret = adv_ec_get_productname(adv_ec_data, adv_ec_data->bios_product_name);
+	if (ret)
+		return ret;
+
+	/* Get pin table */
+	adv_ec_data->dym_tbl = devm_kzalloc(adv_ec_data->dev,
+					EC_MAX_TBL_NUM * sizeof(struct ec_dynamic_table),
+					GFP_KERNEL);
+	if (!adv_ec_data->dym_tbl)
+		return -ENOMEM;

What does a dynamic table do?

drivers/mfd/ahc1ec0.c line 673

Quoted 1 line
drivers/mfd/ahc1ec0.c new file +1 -0
+	ret = adv_get_dynamic_tab(adv_ec_data);

return adv_get_dynamic_tab();

drivers/mfd/ahc1ec0.c line 683

Quoted 9 lines
drivers/mfd/ahc1ec0.c new file +9 -0
+	return ret;
+}
+
+static int adv_ec_parse_prop(struct adv_ec_platform_data *adv_ec_data)
+{
+	int i, ret;
+	u32 nb, sub_dev[ADVEC_SUBDEV_MAX];
+
+	ret = device_property_read_u32(adv_ec_data->dev, "advantech,sub-dev-nb", &nb);

Indexing devices is generally not a good strategy.

drivers/mfd/ahc1ec0.c line 688

Quoted 5 lines
drivers/mfd/ahc1ec0.c new file +5 -0
+	if (ret < 0) {
+		dev_err(adv_ec_data->dev, "get sub-dev-nb failed! (%d)\n", ret);
+		return ret;
+	}
+	adv_ec_data->sub_dev_nb = nb;

'nb' is not a good choice for a variable name.

drivers/mfd/ahc1ec0.c line 692

Quoted 3 lines
drivers/mfd/ahc1ec0.c new file +3 -0
+	ret = device_property_read_u32_array(adv_ec_data->dev, "advantech,sub-dev",
+					     sub_dev, nb);
+	if (ret < 0) {

Is '> 0' valid?

drivers/mfd/ahc1ec0.c line 693

Quoted 1 line
drivers/mfd/ahc1ec0.c new file +1 -0
+		dev_err(adv_ec_data->dev, "get sub-dev failed! (%d)\n", ret);

Please use proper error messages.

"Failed to read 'advantech,sub-dev' property"

Quoted 18 lines
drivers/mfd/ahc1ec0.c new file +18 -0
+		return ret;
+	}
+
+	for (i = 0; i < nb; i++) {
+		switch (sub_dev[i]) {
+		case ADVEC_SUBDEV_BRIGHTNESS:
+		case ADVEC_SUBDEV_EEPROM:
+		case ADVEC_SUBDEV_GPIO:
+		case ADVEC_SUBDEV_HWMON:
+		case ADVEC_SUBDEV_LED:
+		case ADVEC_SUBDEV_WDT:
+			adv_ec_data->sub_dev_mask |= BIT(sub_dev[i]);
+			break;
+		default:
+			dev_err(adv_ec_data->dev, "invalid prop value(%d)!\n",
+				sub_dev[i]);
+		}
+	}

drivers/mfd/ahc1ec0.c line 721

Quoted 10 lines
drivers/mfd/ahc1ec0.c new file +10 -0
+	dev_info(adv_ec_data->dev, "sub-dev mask = 0x%x\n", adv_ec_data->sub_dev_mask);
+
+	return 0;
+}
+
+static int adv_ec_probe(struct platform_device *pdev)
+{
+	int ret, i;
+	struct device *dev = &pdev->dev;
+	struct adv_ec_platform_data *adv_ec_data;

This is not platform data. This is driver data.

struct adv_ec_ddata *ddata;

drivers/mfd/ahc1ec0.c line 723

Quoted 1 line
drivers/mfd/ahc1ec0.c new file +1 -0
+	adv_ec_data = devm_kzalloc(dev, sizeof(struct adv_ec_platform_data), GFP_KERNEL);

sizeof(*adv_ec_data)

Quoted 8 lines
drivers/mfd/ahc1ec0.c new file +8 -0
+	if (!adv_ec_data)
+		return -ENOMEM;
+
+	dev_set_drvdata(dev, adv_ec_data);
+	adv_ec_data->dev = dev;
+
+	ret = adv_ec_init_ec_data(adv_ec_data);
+	if (ret)

drivers/mfd/ahc1ec0.c line 741

Quoted 10 lines
drivers/mfd/ahc1ec0.c new file +10 -0
+		goto err_init_data;
+
+	ret = adv_ec_parse_prop(adv_ec_data);
+	if (ret)
+		goto err_prop;
+
+	/* check whether this EC has the following subdevices. */
+	for (i = 0; i < ARRAY_SIZE(adv_ec_sub_cells); i++) {
+		if (adv_ec_data->sub_dev_mask & BIT(i)) {
+			ret = mfd_add_hotplug_devices(dev, &adv_ec_sub_cells[i], 1);

Why have you chosen to use hotplug here?

drivers/mfd/ahc1ec0.c line 748

Quoted 7 lines
drivers/mfd/ahc1ec0.c new file +7 -0
+			dev_info(adv_ec_data->dev, "mfd_add_hotplug_devices[%d] %s\n", i,
+				adv_ec_sub_cells[i].name);
+			if (ret)
+				dev_err(dev, "failed to add %s subdevice: %d\n",
+					adv_ec_sub_cells[i].name, ret);
+		}
+	}

This is a mess!

Where are you pulling these devices from?

drivers/mfd/ahc1ec0.c line 760

Quoted 11 lines
drivers/mfd/ahc1ec0.c new file +11 -0
+	dev_info(adv_ec_data->dev, "Advantech EC probe done");
+
+	return 0;
+
+err_prop:
+	dev_err(dev, "failed to probe\n");
+
+err_init_data:
+	mutex_destroy(&adv_ec_data->lock);
+
+	dev_err(dev, "failed to init data\n");

You don't need 2 error messages in the error path.

drivers/mfd/ahc1ec0.c line 773

Quoted 12 lines
drivers/mfd/ahc1ec0.c new file +12 -0
+	return ret;
+}
+
+static int adv_ec_remove(struct platform_device *pdev)
+{
+	struct adv_ec_platform_data *adv_ec_data;
+
+	adv_ec_data = dev_get_drvdata(&pdev->dev);
+
+	mutex_destroy(&adv_ec_data->lock);
+
+	mfd_remove_devices(&pdev->dev);

If you don't use the hotplug variant, you can use devm_* and omit this.

drivers/mfd/ahc1ec0.c line 781

Quoted 7 lines
drivers/mfd/ahc1ec0.c new file +7 -0
+	return 0;
+}
+
+static const struct of_device_id adv_ec_of_match[] __maybe_unused = {
+	{
+		.compatible = "advantech,ahc1ec0",
+	},

Put this on one line please.

drivers/mfd/ahc1ec0.c line 787

Quoted 6 lines
drivers/mfd/ahc1ec0.c new file +6 -0
+	{}
+};
+MODULE_DEVICE_TABLE(of, adv_ec_of_match);
+
+static const struct acpi_device_id adv_ec_acpi_match[] __maybe_unused = {
+	{"AHC1EC0", 0},

Spaces inside the '{' and '}' please.

drivers/mfd/ahc1ec0.c line 788

Quoted 1 line
drivers/mfd/ahc1ec0.c new file +1 -0
+	{},

',' here, but not on the one above. Please be consistent.

Quoted 7 lines
drivers/mfd/ahc1ec0.c new file +7 -0
+};
+MODULE_DEVICE_TABLE(acpi, adv_ec_acpi_match);
+
+static struct platform_driver adv_ec_driver = {
+	.driver = {
+		.name = DRV_NAME,
+		.of_match_table = of_match_ptr(adv_ec_of_match),

drivers/mfd/ahc1ec0.c line 805

Quoted 10 lines
drivers/mfd/ahc1ec0.c new file +10 -0
+		.acpi_match_table = ACPI_PTR(adv_ec_acpi_match),
+	},
+	.probe = adv_ec_probe,
+	.remove = adv_ec_remove,
+};
+module_platform_driver(adv_ec_driver);
+
+MODULE_LICENSE("GPL");
+MODULE_ALIAS("platform:" DRV_NAME);
+MODULE_DESCRIPTION("Advantech Embedded Controller core driver.");

Name as in the header.

drivers/mfd/ahc1ec0.c line 808

Quoted 3 lines
drivers/mfd/ahc1ec0.c new file +3 -0
+MODULE_AUTHOR("Campion Kang <[email protected]>");
+MODULE_AUTHOR("Jianfeng Dai <[email protected]>");
+MODULE_VERSION("1.0");

Is this used?

Quoted 242 lines
include/linux/mfd/ahc1ec0.h new file +236 -0
new file mode 100644
index 000000000000..1b01e10c1fef
@@ -0,0 +1,276 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+#ifndef __LINUX_MFD_AHC1EC0_H
+#define __LINUX_MFD_AHC1EC0_H
+
+#include <linux/device.h>
+
+#define EC_COMMAND_PORT             0x29A /* EC I/O command port */
+#define EC_STATUS_PORT              0x299 /* EC I/O data port */
+
+#define EC_RETRY_UDELAY              200 /* EC command retry delay in microseconds */
+#define EC_MAX_TIMEOUT_COUNT        5000 /* EC command max retry count */
+#define EC_COMMAND_BIT_OBF          0x01 /* Bit 0 is for OBF ready (Output buffer full) */
+#define EC_COMMAND_BIT_IBF          0x02 /* Bit 1 is for IBF ready (Input buffer full) */
+
+/* Analog to digital converter command */
+#define EC_AD_INDEX_WRITE   0x15 /* Write ADC port number into index */
+#define EC_AD_LSB_READ      0x16 /* Read ADC LSB value from ADC port */
+#define EC_AD_MSB_READ      0x1F /* Read ADC MSB value from ADC port */
+
+/* Voltage device ID */
+#define EC_DID_SMBOEM0      0x28 /* SMBUS/I2C. Smbus channel 0 */
+#define EC_DID_CMOSBAT      0x50 /* CMOS coin battery voltage */
+#define EC_DID_CMOSBAT_X2   0x51 /* CMOS coin battery voltage*2 */
+#define EC_DID_CMOSBAT_X10  0x52 /* CMOS coin battery voltage*10 */
+#define EC_DID_5VS0         0x56 /* 5VS0 voltage */
+#define EC_DID_5VS0_X2      0x57 /* 5VS0 voltage*2 */
+#define EC_DID_5VS0_X10     0x58 /* 5VS0 voltage*10 */
+#define EC_DID_5VS5         0x59 /* 5VS5 voltage */
+#define EC_DID_5VS5_X2      0x5A /* 5VS5 voltage*2 */
+#define EC_DID_5VS5_X10     0x5B /* 5VS5 voltage*10 */
+#define EC_DID_12VS0        0x62 /* 12VS0 voltage */
+#define EC_DID_12VS0_X2     0x63 /* 12VS0 voltage*2 */
+#define EC_DID_12VS0_X10    0x64 /* 12VS0 voltage*10 */
+#define EC_DID_VCOREA       0x65 /* CPU A core voltage */
+#define EC_DID_VCOREA_X2    0x66 /* CPU A core voltage*2 */
+#define EC_DID_VCOREA_X10   0x67 /* CPU A core voltage*10 */
+#define EC_DID_VCOREB       0x68 /* CPU B core voltage */
+#define EC_DID_VCOREB_X2    0x69 /* CPU B core voltage*2 */
+#define EC_DID_VCOREB_X10   0x6A /* CPU B core voltage*10 */
+#define EC_DID_DC           0x6B /* ADC. onboard voltage */
+#define EC_DID_DC_X2        0x6C /* ADC. onboard voltage*2 */
+#define EC_DID_DC_X10       0x6D /* ADC. onboard voltage*10 */
+
+/* Current device ID */
+#define EC_DID_CURRENT              0x74
+
+/* ACPI commands */
+#define EC_ACPI_RAM_READ            0x80
+#define EC_ACPI_RAM_WRITE           0x81
+
+/*
+ *  Dynamic control table commands
+ *  The table includes HW pin number, Device ID, and Pin polarity
+ */
+#define EC_TBL_WRITE_ITEM           0x20
+#define EC_TBL_GET_PIN              0x21
+#define EC_TBL_GET_DEVID            0x22
+#define EC_MAX_TBL_NUM              32
+
+/* LED Device ID table */
+#define EC_DID_LED_RUN              0xE1
+#define EC_DID_LED_ERR              0xE2
+#define EC_DID_LED_SYS_RECOVERY     0xE3
+#define EC_DID_LED_D105_G           0xE4
+#define EC_DID_LED_D106_G           0xE5
+#define EC_DID_LED_D107_G           0xE6
+
+/* LED control HW RAM address 0xA0-0xAF */
+#define EC_HWRAM_LED_BASE_ADDR      0xA0
+#define EC_HWRAM_LED_PIN(N)         (EC_HWRAM_LED_BASE_ADDR + (4 * (N))) // N:0-3
+#define EC_HWRAM_LED_CTRL_HIBYTE(N) (EC_HWRAM_LED_BASE_ADDR + (4 * (N)) + 1)
+#define EC_HWRAM_LED_CTRL_LOBYTE(N) (EC_HWRAM_LED_BASE_ADDR + (4 * (N)) + 2)
+#define EC_HWRAM_LED_DEVICE_ID(N)   (EC_HWRAM_LED_BASE_ADDR + (4 * (N)) + 3)
+
+/* LED control bit */
+#define LED_CTRL_ENABLE_BIT()           BIT(4)
+#define LED_CTRL_INTCTL_BIT()           BIT(5)
+#define LED_CTRL_LEDBIT_MASK            (0x03FF << 6)
+#define LED_CTRL_POLARITY_MASK          (0x000F << 0)
+#define LED_CTRL_INTCTL_EXTERNAL        0
+#define LED_CTRL_INTCTL_INTERNAL        1
+
+#define LED_DISABLE  0x0
+#define LED_ON       0x1
+#define LED_FAST     0x3
+#define LED_NORMAL   0x5
+#define LED_SLOW     0x7
+#define LED_MANUAL   0xF
+
+#define LED_CTRL_LEDBIT_DISABLE	0x0000
+#define LED_CTRL_LEDBIT_ON		0x03FF
+#define LED_CTRL_LEDBIT_FAST	0x02AA
+#define LED_CTRL_LEDBIT_NORMAL	0x0333
+#define LED_CTRL_LEDBIT_SLOW	0x03E0
+
+/* Get the device name */
+#define AMI_ADVANTECH_BOARD_ID_LENGTH	32
+
+/*
+ * Advantech Embedded Controller watchdog commands
+ * EC can send multi-stage watchdog event. System can setup watchdog event
+ * independently to make up event sequence.
+ */
+#define EC_COMMANS_PORT_IBF_MASK	0x02
+#define EC_RESET_EVENT				0x04
+#define	EC_WDT_START				0x28
+#define	EC_WDT_STOP					0x29
+#define	EC_WDT_RESET				0x2A
+#define	EC_WDT_BOOTTMEWDT_STOP		0x2B
+
+#define EC_HW_RAM					0x89
+
+#define EC_EVENT_FLAG				0x57
+#define EC_ENABLE_DELAY_H			0x58
+#define EC_ENABLE_DELAY_L			0x59
+#define EC_POWER_BTN_TIME_H			0x5A
+#define EC_POWER_BTN_TIME_L			0x5B
+#define EC_RESET_DELAY_TIME_H		0x5E
+#define EC_RESET_DELAY_TIME_L		0x5F
+#define EC_PIN_DELAY_TIME_H			0x60
+#define EC_PIN_DELAY_TIME_L			0x61
+#define EC_SCI_DELAY_TIME_H			0x62
+#define EC_SCI_DELAY_TIME_L			0x63
+
+/* EC ACPI commands */
+#define EC_ACPI_DATA_READ			0x80
+#define EC_ACPI_DATA_WRITE			0x81
+
+/* Brightness ACPI Addr */
+#define BRIGHTNESS_ACPI_ADDR		0x50
+
+/* EC HW RAM commands */
+#define EC_HW_EXTEND_RAM_READ		0x86
+#define EC_HW_EXTEND_RAM_WRITE		0x87
+#define	EC_HW_RAM_READ				0x88
+#define EC_HW_RAM_WRITE				0x89
+
+/* EC Smbus commands */
+#define EC_SMBUS_CHANNEL_SET		0x8A	 /* Set selector number (SMBUS channel) */
+#define EC_SMBUS_ENABLE_I2C			0x8C	 /* Enable channel I2C */
+#define EC_SMBUS_DISABLE_I2C		0x8D	 /* Disable channel I2C */
+
+/* Smbus transmit protocol */
+#define EC_SMBUS_PROTOCOL			0x00
+
+/* SMBUS status */
+#define EC_SMBUS_STATUS				0x01
+
+/* SMBUS device slave address (bit0 must be 0) */
+#define EC_SMBUS_SLV_ADDR			0x02
+
+/* SMBUS device command */
+#define EC_SMBUS_CMD				0x03
+
+/* 0x04-0x24 Data In read process, return data are stored in this address */
+#define EC_SMBUS_DATA				0x04
+
+#define EC_SMBUS_DAT_OFFSET(n)	(EC_SMBUS_DATA + (n))
+
+/* SMBUS channel selector (0-4) */
+#define EC_SMBUS_CHANNEL			0x2B
+
+/* EC SMBUS transmit Protocol code */
+#define SMBUS_QUICK_WRITE			0x02 /* Write Quick Command */
+#define SMBUS_QUICK_READ			0x03 /* Read Quick Command */
+#define SMBUS_BYTE_SEND				0x04 /* Send Byte */
+#define SMBUS_BYTE_RECEIVE			0x05 /* Receive Byte */
+#define SMBUS_BYTE_WRITE			0x06 /* Write Byte */
+#define SMBUS_BYTE_READ				0x07 /* Read Byte */
+#define SMBUS_WORD_WRITE			0x08 /* Write Word */
+#define SMBUS_WORD_READ				0x09 /* Read Word */
+#define SMBUS_BLOCK_WRITE			0x0A /* Write Block */
+#define SMBUS_BLOCK_READ			0x0B /* Read Block */
+#define SMBUS_PROC_CALL				0x0C /* Process Call */
+#define SMBUS_BLOCK_PROC_CALL		0x0D /* Write Block-Read Block Process Call */
+#define SMBUS_I2C_READ_WRITE		0x0E /* I2C block Read-Write */
+#define SMBUS_I2C_WRITE_READ		0x0F /* I2C block Write-Read */
+
+/* GPIO control commands */
+#define EC_GPIO_INDEX_WRITE			0x10
+#define EC_GPIO_STATUS_READ			0x11
+#define EC_GPIO_STATUS_WRITE		0x12
+#define EC_GPIO_DIR_READ			0x1D
+#define EC_GPIO_DIR_WRITE			0x1E
+
+/* One Key Recovery commands */
+#define EC_ONE_KEY_FLAG				0x9C
+
+/* ASG OEM commands */
+#define EC_ASG_OEM					0xEA
+#define EC_ASG_OEM_READ				0x00
+#define EC_ASG_OEM_WRITE			0x01
+#define EC_OEM_POWER_STATUS_VIN1	0X10
+#define EC_OEM_POWER_STATUS_VIN2	0X11
+#define EC_OEM_POWER_STATUS_BAT1	0X12
+#define EC_OEM_POWER_STATUS_BAT2	0X13
+
+/* GPIO DEVICE ID */
+#define EC_DID_ALTGPIO_0			0x10    /* 0x10 AltGpio0 User define gpio */
+#define EC_DID_ALTGPIO_1			0x11    /* 0x11 AltGpio1 User define gpio */
+#define EC_DID_ALTGPIO_2			0x12    /* 0x12 AltGpio2 User define gpio */
+#define EC_DID_ALTGPIO_3			0x13    /* 0x13 AltGpio3 User define gpio */
+#define EC_DID_ALTGPIO_4			0x14    /* 0x14 AltGpio4 User define gpio */
+#define EC_DID_ALTGPIO_5			0x15    /* 0x15 AltGpio5 User define gpio */
+#define EC_DID_ALTGPIO_6			0x16    /* 0x16 AltGpio6 User define gpio */
+#define EC_DID_ALTGPIO_7			0x17    /* 0x17 AltGpio7 User define gpio */
+
+/* Lmsensor Chip Register */
+#define NSLM96163_CHANNEL			0x02
+
+/* NS_LM96163 address 0x98 */
+#define NSLM96163_ADDR				0x98
+
+/* LM96163 index(0x00) Local Temperature (Signed MSB) */
+#define NSLM96163_LOC_TEMP			0x00
+
+/* HWMON registers */
+#define INA266_REG_VOLTAGE          0x02    /* 1.25mV */
+#define INA266_REG_POWER            0x03    /* 25mW */
+#define INA266_REG_CURRENT          0x04    /* 1mA */
+
+struct ec_hw_pin_table {
+	unsigned int vbat[2];
+	unsigned int v5[2];
+	unsigned int v12[2];
+	unsigned int vcore[2];
+	unsigned int vdc[2];
+	unsigned int ec_current[2];
+	unsigned int power[2];
+};
+
+struct ec_dynamic_table {
+	unsigned char device_id;
+	unsigned char hw_pin_num;
+};
+

include/linux/mfd/ahc1ec0.h line 246

Quoted 10 lines
include/linux/mfd/ahc1ec0.h new file +10 -0
+struct ec_smbuso_em0 {
+	unsigned char hw_pin_num;
+};
+
+struct pled_hw_pin_tbl {
+	unsigned int pled[6];
+};
+
+struct adv_ec_platform_data {
+	char *bios_product_name;

Where is this used?

include/linux/mfd/ahc1ec0.h line 254

Quoted 8 lines
include/linux/mfd/ahc1ec0.h new file +8 -0
+	int sub_dev_nb;
+	u32 sub_dev_mask;
+	struct mutex lock;
+	struct device *dev;
+	struct class *adv_ec_class;
+
+	struct ec_dynamic_table *dym_tbl;
+};

Check whether all of these really need to be in device data i.e. check that they are all used in sub-devices.

Quoted 21 lines
include/linux/mfd/ahc1ec0.h new file +21 -0
+int read_ad_value(struct adv_ec_platform_data *adv_ec_data, unsigned char hwpin,
+			unsigned char multi);
+int read_acpi_value(struct adv_ec_platform_data *adv_ec_data, unsigned char addr,
+			unsigned char *pvalue);
+int write_acpi_value(struct adv_ec_platform_data *adv_ec_data, unsigned char addr,
+			unsigned char value);
+int read_hw_ram(struct adv_ec_platform_data *adv_ec_data, unsigned char addr,
+			unsigned char *data);
+int write_hw_ram(struct adv_ec_platform_data *adv_ec_data, unsigned char addr,
+			unsigned char data);
+int write_hwram_command(struct adv_ec_platform_data *adv_ec_data, unsigned char data);
+int read_gpio_status(struct adv_ec_platform_data *adv_ec_data, unsigned char PinNumber,
+			unsigned char *pvalue);
+int write_gpio_status(struct adv_ec_platform_data *adv_ec_data, unsigned char PinNumber,
+			unsigned char value);
+int read_gpio_dir(struct adv_ec_platform_data *adv_ec_data, unsigned char PinNumber,
+			unsigned char *pvalue);
+int write_gpio_dir(struct adv_ec_platform_data *adv_ec_data, unsigned char PinNumber,
+			unsigned char value);
+
+#endif /* __LINUX_MFD_AHC1EC0_H */
Signature · 4 lines
Lee Jones [李琼斯]
Senior Technical Lead - Developer Services
Linaro.org │ Open source software for Arm SoCs
Follow Linaro: Facebook | Twitter | Blog

A message that is not in the archive (sent off-list or not yet received): [email protected]

#10 lore raw

Re: FW: [PATCH v6 4/6] mfd: ahc1ec0: Add support for Advantech embedded controller

sorry ,
Test whether this letter will be returned as a spam

Campion.Kang <[email protected]> 於 2021年3月12日 週五 下午2:57寫道:

Quoted 1,380 lines

test

Quoted 1,377 lines

-----Original Message-----
From: Lee Jones <[email protected]>
Sent: Wednesday, March 10, 2021 12:08 AM
To: Campion.Kang <[email protected]>
Cc: Rob Herring <[email protected]>; [email protected]; [email protected]; Jean Delvare <[email protected]>; Guenter Roeck <[email protected]>; Wim Van Sebroeck <[email protected]>; [email protected]; [email protected]; AceLan Kao <[email protected]>
Subject: Re: [PATCH v6 4/6] mfd: ahc1ec0: Add support for Advantech embedded controller

Quoted 1 line · Campion Kang

AHC1EC0 is the embedded controller driver for Advantech industrial

It would be nice to have the model number in the subject line.

Drop "driver".

Quoted 2 lines

products. This provides sub-devices such as hwmon and watchdog, and also

"HWMON and Watchdog"

Quoted 1 line

expose functions for sub-devices to read/write the value to embedded

"exposes"

Quoted 9 lines

controller.

Changed since V5:
- Kconfig: add "AHC1EC0" string to clearly define the EC name
- fix the code according to reviewer's suggestion
- remove unnecessary header files
- change the structure name to lower case, align with others
naming

Quoted 5 lines
---
 drivers/mfd/Kconfig         |  10 +
 drivers/mfd/Makefile        |   2 +
 drivers/mfd/ahc1ec0.c       | 808

++++++++++++++++++++++++++++++++++++

Quoted 10 lines
 include/linux/mfd/ahc1ec0.h | 276 ++++++++++++
 4 files changed, 1096 insertions(+)
 create mode 100644 drivers/mfd/ahc1ec0.c
 create mode 100644 include/linux/mfd/ahc1ec0.h
drivers/mfd/Kconfig +0 -0
index bdfce7b15621..7d5fb5c17d9a 100644

drivers/mfd/Kconfig config MFD_INTEL_M10_BMC · line 2158

Quoted 5 lines
drivers/mfd/Kconfig +2 -0
@@ -2154,5 +2154,15 @@ config MFD_INTEL_M10_BMC
       additional drivers must be enabled in order to use the functionality
       of the device.
 
+config MFD_AHC1EC0
+    tristate "Advantech AHC1EC0 Embedded Controller Module"

Please remove "Module"

drivers/mfd/Kconfig config MFD_INTEL_M10_BMC · line 2165

Quoted 7 lines
drivers/mfd/Kconfig +7 -0
+    depends on X86
+    select MFD_CORE
+    help
+      This is the core function that for Advantech EC drivers. It
+      includes the sub-devices such as hwmon, watchdog, etc. And also
+      provides expose functions for sub-devices to read/write the value
+      to embedded controller.

"This provides core functionality for the Advantech AHC1EC0 Embedded Controller (EC) along with registration for HWMON and Watchdog sub-devices. It also provides read and write APIs for communication with the EC."

drivers/mfd/Makefile obj-$(CONFIG_MFD_KHADAS_MCU) +=

Quoted 7 lines
drivers/mfd/Kconfig +0 -0
 endmenu
 endif
drivers/mfd/Makefile +0 -0
index 14fdb188af02..a6af9d8825f4 100644
@@ -268,3 +268,5 @@ obj-$(CONFIG_MFD_KHADAS_MCU)     +=

khadas-mcu.o

drivers/mfd/Makefile obj-$(CONFIG_MFD_KHADAS_MCU) += khadas-mcu.o · line 271

Quoted 4 lines
drivers/mfd/Makefile +1 -0
 obj-$(CONFIG_SGI_MFD_IOC3)  += ioc3.o
 obj-$(CONFIG_MFD_SIMPLE_MFD_I2C)    += simple-mfd-i2c.o
 obj-$(CONFIG_MFD_INTEL_M10_BMC)   += intel-m10-bmc.o
+

You don't need to space this out.

drivers/mfd/ahc1ec0.c line 3

Quoted 10 lines
drivers/mfd/Makefile +1 -0
+obj-$(CONFIG_MFD_AHC1EC0)   += ahc1ec0.o
drivers/mfd/ahc1ec0.c new file +3 -0
new file mode 100644
index 000000000000..015f4307a54e
@@ -0,0 +1,808 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Advantech embedded controller core driver AHC1EC0

Advantech AHC1EC0 Embedded Controller

drivers/mfd/ahc1ec0.c line 5

Quoted 1 line
drivers/mfd/ahc1ec0.c new file +1 -0
+ * Copyright 2020 Advantech IIoT Group

This is out of date.

drivers/mfd/ahc1ec0.c line 5

Quoted 1 line
drivers/mfd/ahc1ec0.c new file +1 -0
+ *

Drop this '\n' please.

Quoted 4 lines
drivers/mfd/ahc1ec0.c new file +4 -0
+ */
+
+#include <linux/acpi.h>
+#include <linux/delay.h>

drivers/mfd/ahc1ec0.c line 20

Quoted 10 lines
drivers/mfd/ahc1ec0.c new file +10 -0
+#include <linux/dmi.h>
+#include <linux/errno.h>
+#include <linux/mfd/ahc1ec0.h>
+#include <linux/mfd/core.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/of_platform.h>
+#include <linux/platform_device.h>
+
+#define DRV_NAME      "ahc1ec0"

Please don't do this. Just use the string in-place.

drivers/mfd/ahc1ec0.c line 30

Quoted 9 lines
drivers/mfd/ahc1ec0.c new file +9 -0
+enum {
+    ADVEC_SUBDEV_BRIGHTNESS = 0,
+    ADVEC_SUBDEV_EEPROM,
+    ADVEC_SUBDEV_GPIO,
+    ADVEC_SUBDEV_HWMON,
+    ADVEC_SUBDEV_LED,
+    ADVEC_SUBDEV_WDT,
+    ADVEC_SUBDEV_MAX,
+};

Are these arbitrary?

Quoted 29 lines
drivers/mfd/ahc1ec0.c new file +28 -0
+/* Wait IBF (Input Buffer Full) clear */
+static int ec_wait_write(void)
+{
+    int i;
+
+    for (i = 0; i < EC_MAX_TIMEOUT_COUNT; i++) {
+            if ((inb(EC_COMMAND_PORT) & EC_COMMAND_BIT_IBF) == 0)
+                    return 0;
+
+            udelay(EC_RETRY_UDELAY);
+    }
+
+    return -ETIMEDOUT;
+}
 
+/* Wait OBF (Output Buffer Full) data ready */
+static int ec_wait_read(void)
+{
+    int i;
+
+    for (i = 0; i < EC_MAX_TIMEOUT_COUNT; i++) {
+            if ((inb(EC_COMMAND_PORT) & EC_COMMAND_BIT_OBF) != 0)
+                    return 0;
+
+            udelay(EC_RETRY_UDELAY);
+    }
+
+    return -ETIMEDOUT;
+}

drivers/mfd/ahc1ec0.c

Quoted 10 lines
drivers/mfd/ahc1ec0.c new file +10 -0
+
+/* Read data from EC HW RAM, the process is the following:
+ * Step 0. Wait IBF clear to send command
+ * Step 1. Send read command to EC command port
+ * Step 2. Wait IBF clear that means command is got by EC
+ * Step 3. Send read address to EC data port
+ * Step 4. Wait OBF data ready
+ * Step 5. Get data from EC data port
+ */
+int read_hw_ram(struct adv_ec_platform_data *adv_ec_data, unsigned char

addr, unsigned char *data)

Quoted 17 lines
drivers/mfd/ahc1ec0.c new file +17 -0
+{
+    int ret;
+
+    mutex_lock(&adv_ec_data->lock);
+
+    ret = ec_wait_write();
+    if (ret)
+            goto error;
+    outb(EC_HW_RAM_READ, EC_COMMAND_PORT);
+
+    ret = ec_wait_write();
+    if (ret)
+            goto error;
+    outb(addr, EC_STATUS_PORT);
+
+    ret = ec_wait_read();
+    if (ret)

drivers/mfd/ahc1ec0.c

Quoted 10 lines
drivers/mfd/ahc1ec0.c new file +10 -0
+            goto error;
+    *data = inb(EC_STATUS_PORT);
+
+    mutex_unlock(&adv_ec_data->lock);
+
+    return ret;
+
+error:
+    mutex_unlock(&adv_ec_data->lock);
+    dev_err(adv_ec_data->dev, "%s: Wait for IBF or OBF too long. line: %d\n",

__func__,

drivers/mfd/ahc1ec0.c line 101

Quoted 4 lines
drivers/mfd/ahc1ec0.c new file +4 -0
+           __LINE__);
+
+    return ret;
+}

EXPORT?

drivers/mfd/ahc1ec0.c

Quoted 9 lines
drivers/mfd/ahc1ec0.c new file +9 -0
+/* Write data to EC HW RAM
+ * Step 0. Wait IBF clear to send command
+ * Step 1. Send write command to EC command port
+ * Step 2. Wait IBF clear that means command is got by EC
+ * Step 3. Send write address to EC data port
+ * Step 4. Wait IBF clear that means command is got by EC
+ * Step 5. Send data to EC data port
+ */
+int write_hw_ram(struct adv_ec_platform_data *adv_ec_data, unsigned char

addr, unsigned char data)

Quoted 18 lines
drivers/mfd/ahc1ec0.c new file +18 -0
+{
+    int ret;
+
+    mutex_lock(&adv_ec_data->lock);
+
+    ret = ec_wait_write();
+    if (ret)
+            goto error;
+    outb(EC_HW_RAM_WRITE, EC_COMMAND_PORT);
+
+    ret = ec_wait_write();
+    if (ret)
+            goto error;
+    outb(addr, EC_STATUS_PORT);
+
+    ret = ec_wait_write();
+    if (ret)
+            goto error;

drivers/mfd/ahc1ec0.c

Quoted 10 lines
drivers/mfd/ahc1ec0.c new file +10 -0
+    outb(data, EC_STATUS_PORT);
+
+    mutex_unlock(&adv_ec_data->lock);
+
+    return 0;
+
+error:
+    mutex_unlock(&adv_ec_data->lock);
+
+    dev_err(adv_ec_data->dev, "%s: Wait for IBF or OBF too long. line: %d\n",

__func__,

drivers/mfd/ahc1ec0.c line 144

Quoted 5 lines
drivers/mfd/ahc1ec0.c new file +5 -0
+           __LINE__);
+
+    return ret;
+}
+EXPORT_SYMBOL_GPL(write_hw_ram);

If you're going to export these, they should be in their own namespace.

Quoted 7 lines
drivers/mfd/ahc1ec0.c new file +7 -0
+/* Get dynamic control table */
+static int adv_get_dynamic_tab(struct adv_ec_platform_data *adv_ec_data)
+{
+    int i, ret;
+    unsigned char pin_tmp, device_id;
+
+    mutex_lock(&adv_ec_data->lock);

drivers/mfd/ahc1ec0.c

Quoted 10 lines
drivers/mfd/ahc1ec0.c new file +10 -0
+
+    for (i = 0; i < EC_MAX_TBL_NUM; i++) {
+            adv_ec_data->dym_tbl[i].device_id = 0xff;
+            adv_ec_data->dym_tbl[i].hw_pin_num = 0xff;
+    }
+
+    for (i = 0; i < EC_MAX_TBL_NUM; i++) {
+            ret = ec_wait_write();
+            if (ret) {
+                    dev_dbg(adv_ec_data->dev, "%s: ec_wait_write. line: %d\n",

__func__,

Quoted 1 line

+ __LINE__);

Please remove all of this debug code.

drivers/mfd/ahc1ec0.c line 166

Quoted 3 lines
drivers/mfd/ahc1ec0.c new file +3 -0
+                    goto error;
+            }
+            outb(EC_TBL_WRITE_ITEM, EC_COMMAND_PORT);

More basic commentary is required throughout I think.

drivers/mfd/ahc1ec0.c

Quoted 3 lines
drivers/mfd/ahc1ec0.c new file +3 -0
+            ret = ec_wait_write();
+            if (ret) {
+                    dev_dbg(adv_ec_data->dev, "%s: ec_wait_write. line: %d\n",

__func__,

drivers/mfd/ahc1ec0.c

Quoted 8 lines
drivers/mfd/ahc1ec0.c new file +8 -0
+                            __LINE__);
+                    goto error;
+            }
+            outb(i, EC_STATUS_PORT);
+
+            ret = ec_wait_read();
+            if (ret) {
+                    dev_dbg(adv_ec_data->dev, "%s: ec_wait_read. line: %d\n",

__func__,

drivers/mfd/ahc1ec0.c line 188

Quoted 10 lines
drivers/mfd/ahc1ec0.c new file +10 -0
+                            __LINE__);
+                    goto error;
+            }
+
+            /*
+             *  If item is defined, EC will return item number.
+             *  If table item is not defined, EC will return 0xFF.
+             */
+            pin_tmp = inb(EC_STATUS_PORT);
+            if (pin_tmp == 0xff) {

Please define all magic numbers.

Quoted 1 line

+ dev_dbg(adv_ec_data->dev, "%s:

inb(EC_STATUS_PORT)=0x%02x != 0xff.\n",

drivers/mfd/ahc1ec0.c

Quoted 7 lines
drivers/mfd/ahc1ec0.c new file +7 -0
+                            __func__, pin_tmp);
+                    goto pass;
+            }
+
+            ret = ec_wait_write();
+            if (ret) {
+                    dev_dbg(adv_ec_data->dev, "%s: ec_wait_write. line: %d\n",

__func__,

drivers/mfd/ahc1ec0.c

Quoted 8 lines
drivers/mfd/ahc1ec0.c new file +8 -0
+                            __LINE__);
+                    goto error;
+            }
+            outb(EC_TBL_GET_PIN, EC_COMMAND_PORT);
+
+            ret = ec_wait_read();
+            if (ret) {
+                    dev_dbg(adv_ec_data->dev, "%s: ec_wait_read. line: %d\n",

__func__,

drivers/mfd/ahc1ec0.c

Quoted 6 lines
drivers/mfd/ahc1ec0.c new file +6 -0
+                            __LINE__);
+                    goto error;
+            }
+            pin_tmp = inb(EC_STATUS_PORT) & 0xff;
+            if (pin_tmp == 0xff) {
+                    dev_dbg(adv_ec_data->dev, "%s: pin_tmp(0x%02X). line: %d\n",

__func__,

drivers/mfd/ahc1ec0.c

Quoted 12 lines
drivers/mfd/ahc1ec0.c new file +12 -0
+                            pin_tmp, __LINE__);
+                    goto pass;
+            }
+
+            ret = ec_wait_write();
+            if (ret)
+                    goto error;
+            outb(EC_TBL_GET_DEVID, EC_COMMAND_PORT);
+
+            ret = ec_wait_read();
+            if (ret) {
+                    dev_dbg(adv_ec_data->dev, "%s: ec_wait_read. line: %d\n",

__func__,

drivers/mfd/ahc1ec0.c

Quoted 6 lines
drivers/mfd/ahc1ec0.c new file +6 -0
+                            __LINE__);
+                    goto error;
+            }
+            device_id = inb(EC_STATUS_PORT) & 0xff;
+
+            dev_dbg(adv_ec_data->dev, "%s: device_id=0x%02X. line: %d\n",

__func__,

Quoted 4 lines
drivers/mfd/ahc1ec0.c new file +4 -0
+                    device_id, __LINE__);
+
+            adv_ec_data->dym_tbl[i].device_id = device_id;
+            adv_ec_data->dym_tbl[i].hw_pin_num = pin_tmp;

drivers/mfd/ahc1ec0.c line 242

Quoted 10 lines
drivers/mfd/ahc1ec0.c new file +10 -0
+    }
+
+pass:
+    mutex_unlock(&adv_ec_data->lock);
+    return 0;
+
+error:
+    mutex_unlock(&adv_ec_data->lock);
+    dev_err(adv_ec_data->dev, "%s: Wait for IBF or OBF too long. line: %d\n",
+            __func__, __LINE__);

Remove all _func_ and _LINE_ info. It's seldom helpful.

drivers/mfd/ahc1ec0.c

Quoted 4 lines
drivers/mfd/ahc1ec0.c new file +4 -0
+    return ret;
+}
+
+int read_ad_value(struct adv_ec_platform_data *adv_ec_data, unsigned

char hwpin,

What is 'ad'?

Quoted 45 lines
drivers/mfd/ahc1ec0.c new file +45 -0
+            unsigned char multi)
+{
+    int ret;
+    u32 ret_val;
+    unsigned int LSB, MSB;
+
+    mutex_lock(&adv_ec_data->lock);
+    ret = ec_wait_write();
+    if (ret)
+            goto error;
+    outb(EC_AD_INDEX_WRITE, EC_COMMAND_PORT);
+
+    ret = ec_wait_write();
+    if (ret)
+            goto error;
+    outb(hwpin, EC_STATUS_PORT);
+
+    ret = ec_wait_read();
+    if (ret)
+            goto error;
+
+    if (inb(EC_STATUS_PORT) == 0xff) {
+            mutex_unlock(&adv_ec_data->lock);
+            return -1;
+    }
+
+    ret = ec_wait_write();
+    if (ret)
+            goto error;
+    outb(EC_AD_LSB_READ, EC_COMMAND_PORT);
+
+    ret = ec_wait_read();
+    if (ret)
+            goto error;
+    LSB = inb(EC_STATUS_PORT);
+
+    ret = ec_wait_write();
+    if (ret)
+            goto error;
+    outb(EC_AD_MSB_READ, EC_COMMAND_PORT);
+
+    ret = ec_wait_read();
+    if (ret)
+            goto error;
+    MSB = inb(EC_STATUS_PORT);

drivers/mfd/ahc1ec0.c

Quoted 10 lines
drivers/mfd/ahc1ec0.c new file +10 -0
+    ret_val = ((MSB << 8) | LSB) & 0x03FF;
+    ret_val = ret_val * multi * 100;
+
+    mutex_unlock(&adv_ec_data->lock);
+    return ret_val;
+
+error:
+    mutex_unlock(&adv_ec_data->lock);
+
+    dev_warn(adv_ec_data->dev, "%s: Wait for IBF or OBF too long. line: %d\n",

__func__,

Quoted 1 line

+ __LINE__);

dev_err()

drivers/mfd/ahc1ec0.c

Quoted 5 lines
drivers/mfd/ahc1ec0.c new file +5 -0
+    return ret;
+}
+EXPORT_SYMBOL_GPL(read_ad_value);
+
+int read_acpi_value(struct adv_ec_platform_data *adv_ec_data, unsigned

char addr,

Quoted 21 lines
drivers/mfd/ahc1ec0.c new file +21 -0
+            unsigned char *pvalue)
+{
+    int ret;
+    unsigned char value;
+
+    mutex_lock(&adv_ec_data->lock);
+
+    ret = ec_wait_write();
+    if (ret)
+            goto error;
+    outb(EC_ACPI_RAM_READ, EC_COMMAND_PORT);
+
+    ret = ec_wait_write();
+    if (ret)
+            goto error;
+    outb(addr, EC_STATUS_PORT);
+
+    ret = ec_wait_read();
+    if (ret)
+            goto error;
+    value = inb(EC_STATUS_PORT);

drivers/mfd/ahc1ec0.c

Quoted 10 lines
drivers/mfd/ahc1ec0.c new file +10 -0
+    *pvalue = value;
+
+    mutex_unlock(&adv_ec_data->lock);
+
+    return 0;
+
+error:
+    mutex_unlock(&adv_ec_data->lock);
+
+    dev_warn(adv_ec_data->dev, "%s: Wait for IBF or OBF too long. line: %d\n",

__func__,

drivers/mfd/ahc1ec0.c line 344

Quoted 5 lines
drivers/mfd/ahc1ec0.c new file +5 -0
+            __LINE__);
+
+    return ret;
+}
+EXPORT_SYMBOL_GPL(read_acpi_value);

Namespace.

Quoted 1 line

+int write_acpi_value(struct adv_ec_platform_data *adv_ec_data, unsigned

char addr,

Quoted 18 lines
drivers/mfd/ahc1ec0.c new file +18 -0
+            unsigned char value)
+{
+    int ret;
+
+    mutex_lock(&adv_ec_data->lock);
+
+    ret = ec_wait_write();
+    if (ret)
+            goto error;
+    outb(EC_ACPI_DATA_WRITE, EC_COMMAND_PORT);
+
+    ret = ec_wait_write();
+    if (ret)
+            goto error;
+    outb(addr, EC_STATUS_PORT);
+
+    ret = ec_wait_write();
+    if (ret)

drivers/mfd/ahc1ec0.c

Quoted 10 lines
drivers/mfd/ahc1ec0.c new file +10 -0
+            goto error;
+    outb(value, EC_STATUS_PORT);
+
+    mutex_unlock(&adv_ec_data->lock);
+    return 0;
+
+error:
+    mutex_unlock(&adv_ec_data->lock);
+
+    dev_warn(adv_ec_data->dev, "%s: Wait for IBF or OBF too long. line: %d\n",

__func__,

drivers/mfd/ahc1ec0.c line 101

Quoted 4 lines
drivers/mfd/ahc1ec0.c new file +4 -0
+            __LINE__);
+
+    return ret;
+}

EXPORT?

I think this API (i.e. all of the functions above) should be moved into drivers/platform. They really don't have a place in MFD.

Quoted 1 line

+int read_gpio_status(struct adv_ec_platform_data *adv_ec_data, unsigned

char PinNumber,

Quoted 14 lines
drivers/mfd/ahc1ec0.c new file +14 -0
+            unsigned char *pvalue)
+{
+    int ret;
+
+    unsigned char gpio_status_value;
+
+    mutex_lock(&adv_ec_data->lock);
+
+    ret = ec_wait_write();
+    if (ret)
+            goto error;
+    outb(EC_GPIO_INDEX_WRITE, EC_COMMAND_PORT);
+
+    ret = ec_wait_write();

drivers/mfd/ahc1ec0.c

Quoted 10 lines
drivers/mfd/ahc1ec0.c new file +10 -0
+    if (ret)
+            goto error;
+    outb(PinNumber, EC_STATUS_PORT);
+
+    ret = ec_wait_read();
+    if (ret)
+            goto error;
+
+    if (inb(EC_STATUS_PORT) == 0xff) {
+            dev_err(adv_ec_data->dev, "%s: Read Pin Number error!!\n",

__func__);

Quoted 12 lines
drivers/mfd/ahc1ec0.c new file +12 -0
+            mutex_unlock(&adv_ec_data->lock);
+            return -1;
+    }
+
+    ret = ec_wait_write();
+    if (ret)
+            goto error;
+    outb(EC_GPIO_STATUS_READ, EC_COMMAND_PORT);
+
+    ret = ec_wait_read();
+    if (ret)
+            goto error;

drivers/mfd/ahc1ec0.c

Quoted 10 lines
drivers/mfd/ahc1ec0.c new file +10 -0
+    gpio_status_value = inb(EC_STATUS_PORT);
+
+    *pvalue = gpio_status_value;
+    mutex_unlock(&adv_ec_data->lock);
+    return 0;
+
+error:
+    mutex_unlock(&adv_ec_data->lock);
+
+    dev_warn(adv_ec_data->dev, "%s: Wait for IBF or OBF too long. line: %d\n",

__func__,

drivers/mfd/ahc1ec0.c

Quoted 5 lines
drivers/mfd/ahc1ec0.c new file +5 -0
+            __LINE__);
+    return ret;
+}
+
+int write_gpio_status(struct adv_ec_platform_data *adv_ec_data, unsigned

char PinNumber,

Quoted 13 lines
drivers/mfd/ahc1ec0.c new file +13 -0
+            unsigned char value)
+{
+    int ret;
+
+    mutex_lock(&adv_ec_data->lock);
+
+    ret = ec_wait_write();
+    if (ret)
+            goto error;
+    outb(EC_GPIO_INDEX_WRITE, EC_COMMAND_PORT);
+
+    ret = ec_wait_write();
+    if (ret)

drivers/mfd/ahc1ec0.c

Quoted 10 lines
drivers/mfd/ahc1ec0.c new file +10 -0
+            goto error;
+    outb(PinNumber, EC_STATUS_PORT);
+
+    ret = ec_wait_read();
+    if (ret)
+            goto error;
+
+    if (inb(EC_STATUS_PORT) == 0xff) {
+            mutex_unlock(&adv_ec_data->lock);
+            dev_err(adv_ec_data->dev, "%s: Read Pin Number error!!\n",

__func__);

Quoted 9 lines
drivers/mfd/ahc1ec0.c new file +9 -0
+            return -1;
+    }
+
+    ret = ec_wait_write();
+    if (ret)
+            goto error;
+    outb(EC_GPIO_STATUS_WRITE, EC_COMMAND_PORT);
+
+    ret = ec_wait_write();

drivers/mfd/ahc1ec0.c

Quoted 10 lines
drivers/mfd/ahc1ec0.c new file +10 -0
+    if (ret)
+            goto error;
+    outb(value, EC_STATUS_PORT);
+
+    mutex_unlock(&adv_ec_data->lock);
+    return 0;
+
+error:
+    mutex_unlock(&adv_ec_data->lock);
+    dev_err(adv_ec_data->dev, "%s: Wait for IBF or OBF too long. line: %d",

__func__,

drivers/mfd/ahc1ec0.c

Quoted 6 lines
drivers/mfd/ahc1ec0.c new file +6 -0
+            __LINE__);
+
+    return ret;
+}
+
+int read_gpio_dir(struct adv_ec_platform_data *adv_ec_data, unsigned char

PinNumber,

Quoted 14 lines
drivers/mfd/ahc1ec0.c new file +14 -0
+            unsigned char *pvalue)
+{
+    int ret;
+    unsigned char gpio_dir_value;
+
+    mutex_lock(&adv_ec_data->lock);
+
+    ret = ec_wait_write();
+    if (ret)
+            goto error;
+    outb(EC_GPIO_INDEX_WRITE, EC_COMMAND_PORT);
+
+    ret = ec_wait_write();
+    if (ret)

drivers/mfd/ahc1ec0.c

Quoted 10 lines
drivers/mfd/ahc1ec0.c new file +10 -0
+            goto error;
+    outb(PinNumber, EC_STATUS_PORT);
+
+    ret = ec_wait_read();
+    if (ret)
+            goto error;
+
+    if (inb(EC_STATUS_PORT) == 0xff) {
+            mutex_unlock(&adv_ec_data->lock);
+            dev_err(adv_ec_data->dev, "%s: Read Pin Number error!! line: %d\n",

__func__,

Quoted 12 lines
drivers/mfd/ahc1ec0.c new file +12 -0
+                    __LINE__);
+            return -1;
+    }
+
+    ret = ec_wait_write();
+    if (ret)
+            goto error;
+    outb(EC_GPIO_DIR_READ, EC_COMMAND_PORT);
+
+    ret = ec_wait_read();
+    if (ret)
+            goto error;

drivers/mfd/ahc1ec0.c

Quoted 10 lines
drivers/mfd/ahc1ec0.c new file +10 -0
+    gpio_dir_value = inb(EC_STATUS_PORT);
+    *pvalue = gpio_dir_value;
+
+    mutex_unlock(&adv_ec_data->lock);
+    return 0;
+
+error:
+    mutex_unlock(&adv_ec_data->lock);
+
+    dev_warn(adv_ec_data->dev, "%s: Wait for IBF or OBF too long. line: %d\n",

__func__,

drivers/mfd/ahc1ec0.c

Quoted 6 lines
drivers/mfd/ahc1ec0.c new file +6 -0
+                    __LINE__);
+
+    return ret;
+}
+
+int write_gpio_dir(struct adv_ec_platform_data *adv_ec_data, unsigned

char PinNumber,

Quoted 13 lines
drivers/mfd/ahc1ec0.c new file +13 -0
+            unsigned char value)
+{
+    int ret;
+
+    mutex_lock(&adv_ec_data->lock);
+
+    ret = ec_wait_write();
+    if (ret)
+            goto error;
+    outb(EC_GPIO_INDEX_WRITE, EC_COMMAND_PORT);
+
+    ret = ec_wait_write();
+    if (ret)

drivers/mfd/ahc1ec0.c

Quoted 10 lines
drivers/mfd/ahc1ec0.c new file +10 -0
+            goto error;
+    outb(PinNumber, EC_STATUS_PORT);
+
+    ret = ec_wait_read();
+    if (ret)
+            goto error;
+
+    if (inb(EC_STATUS_PORT) == 0xff) {
+            mutex_unlock(&adv_ec_data->lock);
+            dev_warn(adv_ec_data->dev, "%s: Read Pin Number error!! line:

%d\n", __func__,

Quoted 12 lines
drivers/mfd/ahc1ec0.c new file +12 -0
+                    __LINE__);
+
+            return -1;
+    }
+
+    ret = ec_wait_write();
+    if (ret)
+            goto error;
+    outb(EC_GPIO_DIR_WRITE, EC_COMMAND_PORT);
+
+    ret = ec_wait_write();
+    if (ret)

drivers/mfd/ahc1ec0.c

Quoted 10 lines
drivers/mfd/ahc1ec0.c new file +10 -0
+            goto error;
+    outb(value, EC_STATUS_PORT);
+
+    mutex_unlock(&adv_ec_data->lock);
+    return 0;
+
+error:
+    mutex_unlock(&adv_ec_data->lock);
+
+    dev_warn(adv_ec_data->dev, "%s: Wait for IBF or OBF too long. line: %d\n",

__func__,

drivers/mfd/ahc1ec0.c line 101

Quoted 4 lines
drivers/mfd/ahc1ec0.c new file +4 -0
+                    __LINE__);
+
+    return ret;
+}

All of the GPIO functions above should move into drivers/gpio.

Quoted 1 line

+int write_hwram_command(struct adv_ec_platform_data *adv_ec_data,

unsigned char data)

Quoted 7 lines
drivers/mfd/ahc1ec0.c new file +7 -0
+{
+    int ret;
+
+    mutex_lock(&adv_ec_data->lock);
+
+    ret = ec_wait_write();
+    if (ret)

drivers/mfd/ahc1ec0.c

Quoted 10 lines
drivers/mfd/ahc1ec0.c new file +10 -0
+            goto error;
+    outb(data, EC_COMMAND_PORT);
+
+    mutex_unlock(&adv_ec_data->lock);
+    return 0;
+
+error:
+    mutex_unlock(&adv_ec_data->lock);
+
+    dev_warn(adv_ec_data->dev, "%s: Wait for IBF or OBF too long. line: %d\n",

__func__,

drivers/mfd/ahc1ec0.c

Quoted 7 lines
drivers/mfd/ahc1ec0.c new file +7 -0
+                    __LINE__);
+
+    return ret;
+}
+EXPORT_SYMBOL_GPL(write_hwram_command);
+
+static int adv_ec_get_productname(struct adv_ec_platform_data

*adv_ec_data, char *product)

Quoted 14 lines
drivers/mfd/ahc1ec0.c new file +14 -0
+{
+    const char *vendor, *device;
+    int length = 0;
+
+    /* Check it is Advantech board */
+    vendor = dmi_get_system_info(DMI_SYS_VENDOR);
+    if (memcmp(vendor, "Advantech", sizeof("Advantech")) != 0)
+            return -ENODEV;
+
+    /* Get product model name */
+    device = dmi_get_system_info(DMI_PRODUCT_NAME);
+    if (device) {
+            while ((device[length] != ' ')
+                    && (length < AMI_ADVANTECH_BOARD_ID_LENGTH))

drivers/mfd/ahc1ec0.c

Quoted 10 lines
drivers/mfd/ahc1ec0.c new file +10 -0
+                    length++;
+            memset(product, 0, AMI_ADVANTECH_BOARD_ID_LENGTH);
+            memmove(product, device, length);
+
+            dev_info(adv_ec_data->dev, "BIOS Product Name = %s\n", product);
+
+            return 0;
+    }
+
+    dev_warn(adv_ec_data->dev, "This device is not Advantech Board (%s)!\n",

product);

drivers/mfd/ahc1ec0.c line 628

Quoted 3 lines
drivers/mfd/ahc1ec0.c new file +3 -0
+
+    return -ENODEV;
+}

These should go into drivers/platform.

Quoted 7 lines
drivers/mfd/ahc1ec0.c new file +7 -0
+static const struct mfd_cell adv_ec_sub_cells[] = {
+    { .name = "adv-ec-brightness", },
+    { .name = "adv-ec-eeprom", },
+    { .name = "adv-ec-gpio", },
+    { .name = "ahc1ec0-hwmon", },
+    { .name = "adv-ec-led", },
+    { .name = "ahc1ec0-wdt", },

drivers/mfd/ahc1ec0.c line 651

Quoted 10 lines
drivers/mfd/ahc1ec0.c new file +10 -0
+};
+
+static int adv_ec_init_ec_data(struct adv_ec_platform_data *adv_ec_data)
+{
+    int ret;
+
+    adv_ec_data->sub_dev_mask = 0;
+    adv_ec_data->sub_dev_nb = 0;
+    adv_ec_data->dym_tbl = NULL;
+    adv_ec_data->bios_product_name = NULL;

Why are pre-initialising these?

drivers/mfd/ahc1ec0.c

Quoted 5 lines
drivers/mfd/ahc1ec0.c new file +5 -0
+    mutex_init(&adv_ec_data->lock);
+
+    /* Get product name */
+    adv_ec_data->bios_product_name =
+            devm_kzalloc(adv_ec_data->dev,

AMI_ADVANTECH_BOARD_ID_LENGTH, GFP_KERNEL);

drivers/mfd/ahc1ec0.c

Quoted 4 lines
drivers/mfd/ahc1ec0.c new file +4 -0
+    if (!adv_ec_data->bios_product_name)
+            return -ENOMEM;
+
+    memset(adv_ec_data->bios_product_name, 0,

AMI_ADVANTECH_BOARD_ID_LENGTH);

Why are you doing this?

Quoted 1 line

+ ret = adv_ec_get_productname(adv_ec_data,

adv_ec_data->bios_product_name);

drivers/mfd/ahc1ec0.c line 671

Quoted 9 lines
drivers/mfd/ahc1ec0.c new file +9 -0
+    if (ret)
+            return ret;
+
+    /* Get pin table */
+    adv_ec_data->dym_tbl = devm_kzalloc(adv_ec_data->dev,
+                                    EC_MAX_TBL_NUM * sizeof(struct ec_dynamic_table),
+                                    GFP_KERNEL);
+    if (!adv_ec_data->dym_tbl)
+            return -ENOMEM;

What does a dynamic table do?

Quoted 1 line

+ ret = adv_get_dynamic_tab(adv_ec_data);

return adv_get_dynamic_tab();

drivers/mfd/ahc1ec0.c

Quoted 9 lines
drivers/mfd/ahc1ec0.c new file +9 -0
+    return ret;
+}
+
+static int adv_ec_parse_prop(struct adv_ec_platform_data *adv_ec_data)
+{
+    int i, ret;
+    u32 nb, sub_dev[ADVEC_SUBDEV_MAX];
+
+    ret = device_property_read_u32(adv_ec_data->dev,

"advantech,sub-dev-nb", &nb);

Indexing devices is generally not a good strategy.

drivers/mfd/ahc1ec0.c line 688

Quoted 5 lines
drivers/mfd/ahc1ec0.c new file +5 -0
+    if (ret < 0) {
+            dev_err(adv_ec_data->dev, "get sub-dev-nb failed! (%d)\n", ret);
+            return ret;
+    }
+    adv_ec_data->sub_dev_nb = nb;

'nb' is not a good choice for a variable name.

Quoted 1 line

+ ret = device_property_read_u32_array(adv_ec_data->dev,

"advantech,sub-dev",

drivers/mfd/ahc1ec0.c line 692

Quoted 2 lines
drivers/mfd/ahc1ec0.c new file +2 -0
+                                         sub_dev, nb);
+    if (ret < 0) {

Is '> 0' valid?

Quoted 1 line

+ dev_err(adv_ec_data->dev, "get sub-dev failed! (%d)\n", ret);

Please use proper error messages.

"Failed to read 'advantech,sub-dev' property"

Quoted 9 lines
drivers/mfd/ahc1ec0.c new file +9 -0
+            return ret;
+    }
+
+    for (i = 0; i < nb; i++) {
+            switch (sub_dev[i]) {
+            case ADVEC_SUBDEV_BRIGHTNESS:
+            case ADVEC_SUBDEV_EEPROM:
+            case ADVEC_SUBDEV_GPIO:
+            case ADVEC_SUBDEV_HWMON:

drivers/mfd/ahc1ec0.c

Quoted 10 lines
drivers/mfd/ahc1ec0.c new file +10 -0
+            case ADVEC_SUBDEV_LED:
+            case ADVEC_SUBDEV_WDT:
+                    adv_ec_data->sub_dev_mask |= BIT(sub_dev[i]);
+                    break;
+            default:
+                    dev_err(adv_ec_data->dev, "invalid prop value(%d)!\n",
+                            sub_dev[i]);
+            }
+    }
+    dev_info(adv_ec_data->dev, "sub-dev mask = 0x%x\n",

adv_ec_data->sub_dev_mask);

drivers/mfd/ahc1ec0.c line 721

Quoted 9 lines
drivers/mfd/ahc1ec0.c new file +9 -0
+
+    return 0;
+}
+
+static int adv_ec_probe(struct platform_device *pdev)
+{
+    int ret, i;
+    struct device *dev = &pdev->dev;
+    struct adv_ec_platform_data *adv_ec_data;

This is not platform data. This is driver data.

struct adv_ec_ddata *ddata;

Quoted 1 line

+ adv_ec_data = devm_kzalloc(dev, sizeof(struct adv_ec_platform_data),

GFP_KERNEL);

sizeof(*adv_ec_data)

Quoted 8 lines
drivers/mfd/ahc1ec0.c new file +8 -0
+    if (!adv_ec_data)
+            return -ENOMEM;
+
+    dev_set_drvdata(dev, adv_ec_data);
+    adv_ec_data->dev = dev;
+
+    ret = adv_ec_init_ec_data(adv_ec_data);
+    if (ret)

drivers/mfd/ahc1ec0.c line 741

Quoted 10 lines
drivers/mfd/ahc1ec0.c new file +10 -0
+            goto err_init_data;
+
+    ret = adv_ec_parse_prop(adv_ec_data);
+    if (ret)
+            goto err_prop;
+
+    /* check whether this EC has the following subdevices. */
+    for (i = 0; i < ARRAY_SIZE(adv_ec_sub_cells); i++) {
+            if (adv_ec_data->sub_dev_mask & BIT(i)) {
+                    ret = mfd_add_hotplug_devices(dev, &adv_ec_sub_cells[i], 1);

Why have you chosen to use hotplug here?

Quoted 1 line

+ dev_info(adv_ec_data->dev, "mfd_add_hotplug_devices[%d]

%s\n", i,

drivers/mfd/ahc1ec0.c line 748

Quoted 6 lines
drivers/mfd/ahc1ec0.c new file +6 -0
+                            adv_ec_sub_cells[i].name);
+                    if (ret)
+                            dev_err(dev, "failed to add %s subdevice: %d\n",
+                                    adv_ec_sub_cells[i].name, ret);
+            }
+    }

This is a mess!

Where are you pulling these devices from?

drivers/mfd/ahc1ec0.c line 760

Quoted 11 lines
drivers/mfd/ahc1ec0.c new file +11 -0
+    dev_info(adv_ec_data->dev, "Advantech EC probe done");
+
+    return 0;
+
+err_prop:
+    dev_err(dev, "failed to probe\n");
+
+err_init_data:
+    mutex_destroy(&adv_ec_data->lock);
+
+    dev_err(dev, "failed to init data\n");

You don't need 2 error messages in the error path.

drivers/mfd/ahc1ec0.c line 773

Quoted 12 lines
drivers/mfd/ahc1ec0.c new file +12 -0
+    return ret;
+}
+
+static int adv_ec_remove(struct platform_device *pdev)
+{
+    struct adv_ec_platform_data *adv_ec_data;
+
+    adv_ec_data = dev_get_drvdata(&pdev->dev);
+
+    mutex_destroy(&adv_ec_data->lock);
+
+    mfd_remove_devices(&pdev->dev);

If you don't use the hotplug variant, you can use devm_* and omit this.

drivers/mfd/ahc1ec0.c line 781

Quoted 7 lines
drivers/mfd/ahc1ec0.c new file +7 -0
+    return 0;
+}
+
+static const struct of_device_id adv_ec_of_match[] __maybe_unused = {
+    {
+            .compatible = "advantech,ahc1ec0",
+    },

Put this on one line please.

drivers/mfd/ahc1ec0.c line 787

Quoted 6 lines
drivers/mfd/ahc1ec0.c new file +6 -0
+    {}
+};
+MODULE_DEVICE_TABLE(of, adv_ec_of_match);
+
+static const struct acpi_device_id adv_ec_acpi_match[] __maybe_unused = {
+    {"AHC1EC0", 0},

Spaces inside the '{' and '}' please.

Quoted 1 line

+ {},

',' here, but not on the one above. Please be consistent.

Quoted 7 lines
drivers/mfd/ahc1ec0.c new file +7 -0
+};
+MODULE_DEVICE_TABLE(acpi, adv_ec_acpi_match);
+
+static struct platform_driver adv_ec_driver = {
+    .driver = {
+            .name = DRV_NAME,
+            .of_match_table = of_match_ptr(adv_ec_of_match),

drivers/mfd/ahc1ec0.c line 805

Quoted 10 lines
drivers/mfd/ahc1ec0.c new file +10 -0
+            .acpi_match_table = ACPI_PTR(adv_ec_acpi_match),
+    },
+    .probe = adv_ec_probe,
+    .remove = adv_ec_remove,
+};
+module_platform_driver(adv_ec_driver);
+
+MODULE_LICENSE("GPL");
+MODULE_ALIAS("platform:" DRV_NAME);
+MODULE_DESCRIPTION("Advantech Embedded Controller core driver.");

Name as in the header.

drivers/mfd/ahc1ec0.c line 808

Quoted 3 lines
drivers/mfd/ahc1ec0.c new file +3 -0
+MODULE_AUTHOR("Campion Kang <[email protected]>");
+MODULE_AUTHOR("Jianfeng Dai <[email protected]>");
+MODULE_VERSION("1.0");

Is this used?

include/linux/mfd/ahc1ec0.h line 7

Quoted 13 lines
include/linux/mfd/ahc1ec0.h new file +7 -0
new file mode 100644
index 000000000000..1b01e10c1fef
@@ -0,0 +1,276 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+#ifndef __LINUX_MFD_AHC1EC0_H
+#define __LINUX_MFD_AHC1EC0_H
+
+#include <linux/device.h>
+
+#define EC_COMMAND_PORT             0x29A /* EC I/O command

port */

include/linux/mfd/ahc1ec0.h

Quoted 3 lines
include/linux/mfd/ahc1ec0.h new file +3 -0
+#define EC_STATUS_PORT              0x299 /* EC I/O data port */
+
+#define EC_RETRY_UDELAY              200 /* EC command retry delay

in microseconds */

include/linux/mfd/ahc1ec0.h line 11

Quoted 1 line
include/linux/mfd/ahc1ec0.h new file +1 -0
+#define EC_MAX_TIMEOUT_COUNT        5000 /* EC command max retry

count */

include/linux/mfd/ahc1ec0.h line 12

Quoted 1 line
include/linux/mfd/ahc1ec0.h new file +1 -0
+#define EC_COMMAND_BIT_OBF          0x01 /* Bit 0 is for OBF ready

(Output buffer full) */

include/linux/mfd/ahc1ec0.h line 13

Quoted 1 line
include/linux/mfd/ahc1ec0.h new file +1 -0
+#define EC_COMMAND_BIT_IBF          0x02 /* Bit 1 is for IBF ready

(Input buffer full) */

include/linux/mfd/ahc1ec0.h

Quoted 3 lines
include/linux/mfd/ahc1ec0.h new file +3 -0
+
+/* Analog to digital converter command */
+#define EC_AD_INDEX_WRITE   0x15 /* Write ADC port number into index

*/

include/linux/mfd/ahc1ec0.h line 17

Quoted 1 line
include/linux/mfd/ahc1ec0.h new file +1 -0
+#define EC_AD_LSB_READ      0x16 /* Read ADC LSB value from ADC port

*/

include/linux/mfd/ahc1ec0.h line 18

Quoted 1 line
include/linux/mfd/ahc1ec0.h new file +1 -0
+#define EC_AD_MSB_READ      0x1F /* Read ADC MSB value from ADC

port */

Quoted 42 lines
include/linux/mfd/ahc1ec0.h new file +42 -0
+
+/* Voltage device ID */
+#define EC_DID_SMBOEM0      0x28 /* SMBUS/I2C. Smbus channel 0 */
+#define EC_DID_CMOSBAT      0x50 /* CMOS coin battery voltage */
+#define EC_DID_CMOSBAT_X2   0x51 /* CMOS coin battery voltage*2 */
+#define EC_DID_CMOSBAT_X10  0x52 /* CMOS coin battery voltage*10 */
+#define EC_DID_5VS0         0x56 /* 5VS0 voltage */
+#define EC_DID_5VS0_X2      0x57 /* 5VS0 voltage*2 */
+#define EC_DID_5VS0_X10     0x58 /* 5VS0 voltage*10 */
+#define EC_DID_5VS5         0x59 /* 5VS5 voltage */
+#define EC_DID_5VS5_X2      0x5A /* 5VS5 voltage*2 */
+#define EC_DID_5VS5_X10     0x5B /* 5VS5 voltage*10 */
+#define EC_DID_12VS0        0x62 /* 12VS0 voltage */
+#define EC_DID_12VS0_X2     0x63 /* 12VS0 voltage*2 */
+#define EC_DID_12VS0_X10    0x64 /* 12VS0 voltage*10 */
+#define EC_DID_VCOREA       0x65 /* CPU A core voltage */
+#define EC_DID_VCOREA_X2    0x66 /* CPU A core voltage*2 */
+#define EC_DID_VCOREA_X10   0x67 /* CPU A core voltage*10 */
+#define EC_DID_VCOREB       0x68 /* CPU B core voltage */
+#define EC_DID_VCOREB_X2    0x69 /* CPU B core voltage*2 */
+#define EC_DID_VCOREB_X10   0x6A /* CPU B core voltage*10 */
+#define EC_DID_DC           0x6B /* ADC. onboard voltage */
+#define EC_DID_DC_X2        0x6C /* ADC. onboard voltage*2 */
+#define EC_DID_DC_X10       0x6D /* ADC. onboard voltage*10 */
+
+/* Current device ID */
+#define EC_DID_CURRENT              0x74
+
+/* ACPI commands */
+#define EC_ACPI_RAM_READ            0x80
+#define EC_ACPI_RAM_WRITE           0x81
+
+/*
+ *  Dynamic control table commands
+ *  The table includes HW pin number, Device ID, and Pin polarity
+ */
+#define EC_TBL_WRITE_ITEM           0x20
+#define EC_TBL_GET_PIN              0x21
+#define EC_TBL_GET_DEVID            0x22
+#define EC_MAX_TBL_NUM              32
+
+/* LED Device ID table */

include/linux/mfd/ahc1ec0.h

Quoted 10 lines
include/linux/mfd/ahc1ec0.h new file +10 -0
+#define EC_DID_LED_RUN              0xE1
+#define EC_DID_LED_ERR              0xE2
+#define EC_DID_LED_SYS_RECOVERY     0xE3
+#define EC_DID_LED_D105_G           0xE4
+#define EC_DID_LED_D106_G           0xE5
+#define EC_DID_LED_D107_G           0xE6
+
+/* LED control HW RAM address 0xA0-0xAF */
+#define EC_HWRAM_LED_BASE_ADDR      0xA0
+#define EC_HWRAM_LED_PIN(N)         (EC_HWRAM_LED_BASE_ADDR

+ (4 * (N))) // N:0-3

include/linux/mfd/ahc1ec0.h line 71

Quoted 1 line
include/linux/mfd/ahc1ec0.h new file +1 -0
+#define EC_HWRAM_LED_CTRL_HIBYTE(N) (EC_HWRAM_LED_BASE_ADDR +

(4 * (N)) + 1)

include/linux/mfd/ahc1ec0.h line 72

Quoted 1 line
include/linux/mfd/ahc1ec0.h new file +1 -0
+#define EC_HWRAM_LED_CTRL_LOBYTE(N) (EC_HWRAM_LED_BASE_ADDR

+ (4 * (N)) + 2)

include/linux/mfd/ahc1ec0.h line 73

Quoted 1 line
include/linux/mfd/ahc1ec0.h new file +1 -0
+#define EC_HWRAM_LED_DEVICE_ID(N)   (EC_HWRAM_LED_BASE_ADDR

+ (4 * (N)) + 3)

Quoted 18 lines
include/linux/mfd/ahc1ec0.h new file +18 -0
+
+/* LED control bit */
+#define LED_CTRL_ENABLE_BIT()           BIT(4)
+#define LED_CTRL_INTCTL_BIT()           BIT(5)
+#define LED_CTRL_LEDBIT_MASK            (0x03FF << 6)
+#define LED_CTRL_POLARITY_MASK          (0x000F << 0)
+#define LED_CTRL_INTCTL_EXTERNAL        0
+#define LED_CTRL_INTCTL_INTERNAL        1
+
+#define LED_DISABLE  0x0
+#define LED_ON       0x1
+#define LED_FAST     0x3
+#define LED_NORMAL   0x5
+#define LED_SLOW     0x7
+#define LED_MANUAL   0xF
+
+#define LED_CTRL_LEDBIT_DISABLE     0x0000
+#define LED_CTRL_LEDBIT_ON          0x03FF

include/linux/mfd/ahc1ec0.h

Quoted 10 lines
include/linux/mfd/ahc1ec0.h new file +10 -0
+#define LED_CTRL_LEDBIT_FAST        0x02AA
+#define LED_CTRL_LEDBIT_NORMAL      0x0333
+#define LED_CTRL_LEDBIT_SLOW        0x03E0
+
+/* Get the device name */
+#define AMI_ADVANTECH_BOARD_ID_LENGTH       32
+
+/*
+ * Advantech Embedded Controller watchdog commands
+ * EC can send multi-stage watchdog event. System can setup watchdog

event

Quoted 28 lines
include/linux/mfd/ahc1ec0.h new file +28 -0
+ * independently to make up event sequence.
+ */
+#define EC_COMMANS_PORT_IBF_MASK    0x02
+#define EC_RESET_EVENT                              0x04
+#define     EC_WDT_START                            0x28
+#define     EC_WDT_STOP                                     0x29
+#define     EC_WDT_RESET                            0x2A
+#define     EC_WDT_BOOTTMEWDT_STOP          0x2B
+
+#define EC_HW_RAM                                   0x89
+
+#define EC_EVENT_FLAG                               0x57
+#define EC_ENABLE_DELAY_H                   0x58
+#define EC_ENABLE_DELAY_L                   0x59
+#define EC_POWER_BTN_TIME_H                 0x5A
+#define EC_POWER_BTN_TIME_L                 0x5B
+#define EC_RESET_DELAY_TIME_H               0x5E
+#define EC_RESET_DELAY_TIME_L               0x5F
+#define EC_PIN_DELAY_TIME_H                 0x60
+#define EC_PIN_DELAY_TIME_L                 0x61
+#define EC_SCI_DELAY_TIME_H                 0x62
+#define EC_SCI_DELAY_TIME_L                 0x63
+
+/* EC ACPI commands */
+#define EC_ACPI_DATA_READ                   0x80
+#define EC_ACPI_DATA_WRITE                  0x81
+
+/* Brightness ACPI Addr */

include/linux/mfd/ahc1ec0.h

Quoted 10 lines
include/linux/mfd/ahc1ec0.h new file +10 -0
+#define BRIGHTNESS_ACPI_ADDR                0x50
+
+/* EC HW RAM commands */
+#define EC_HW_EXTEND_RAM_READ               0x86
+#define EC_HW_EXTEND_RAM_WRITE              0x87
+#define     EC_HW_RAM_READ                          0x88
+#define EC_HW_RAM_WRITE                             0x89
+
+/* EC Smbus commands */
+#define EC_SMBUS_CHANNEL_SET                0x8A     /* Set selector number

(SMBUS channel) */

include/linux/mfd/ahc1ec0.h line 140

Quoted 1 line
include/linux/mfd/ahc1ec0.h new file +1 -0
+#define EC_SMBUS_ENABLE_I2C                 0x8C     /* Enable channel

I2C */

include/linux/mfd/ahc1ec0.h line 141

Quoted 1 line
include/linux/mfd/ahc1ec0.h new file +1 -0
+#define EC_SMBUS_DISABLE_I2C                0x8D     /* Disable channel I2C

*/

Quoted 24 lines
include/linux/mfd/ahc1ec0.h new file +24 -0
+
+/* Smbus transmit protocol */
+#define EC_SMBUS_PROTOCOL                   0x00
+
+/* SMBUS status */
+#define EC_SMBUS_STATUS                             0x01
+
+/* SMBUS device slave address (bit0 must be 0) */
+#define EC_SMBUS_SLV_ADDR                   0x02
+
+/* SMBUS device command */
+#define EC_SMBUS_CMD                                0x03
+
+/* 0x04-0x24 Data In read process, return data are stored in this address */
+#define EC_SMBUS_DATA                               0x04
+
+#define EC_SMBUS_DAT_OFFSET(n)      (EC_SMBUS_DATA + (n))
+
+/* SMBUS channel selector (0-4) */
+#define EC_SMBUS_CHANNEL                    0x2B
+
+/* EC SMBUS transmit Protocol code */
+#define SMBUS_QUICK_WRITE                   0x02 /* Write Quick Command */
+#define SMBUS_QUICK_READ                    0x03 /* Read Quick Command */

include/linux/mfd/ahc1ec0.h

Quoted 10 lines
include/linux/mfd/ahc1ec0.h new file +10 -0
+#define SMBUS_BYTE_SEND                             0x04 /* Send Byte */
+#define SMBUS_BYTE_RECEIVE                  0x05 /* Receive Byte */
+#define SMBUS_BYTE_WRITE                    0x06 /* Write Byte */
+#define SMBUS_BYTE_READ                             0x07 /* Read Byte */
+#define SMBUS_WORD_WRITE                    0x08 /* Write Word */
+#define SMBUS_WORD_READ                             0x09 /* Read Word */
+#define SMBUS_BLOCK_WRITE                   0x0A /* Write Block */
+#define SMBUS_BLOCK_READ                    0x0B /* Read Block */
+#define SMBUS_PROC_CALL                             0x0C /* Process Call */
+#define SMBUS_BLOCK_PROC_CALL               0x0D /* Write Block-Read Block

Process Call */

Quoted 14 lines
include/linux/mfd/ahc1ec0.h new file +14 -0
+#define SMBUS_I2C_READ_WRITE                0x0E /* I2C block Read-Write */
+#define SMBUS_I2C_WRITE_READ                0x0F /* I2C block Write-Read */
+
+/* GPIO control commands */
+#define EC_GPIO_INDEX_WRITE                 0x10
+#define EC_GPIO_STATUS_READ                 0x11
+#define EC_GPIO_STATUS_WRITE                0x12
+#define EC_GPIO_DIR_READ                    0x1D
+#define EC_GPIO_DIR_WRITE                   0x1E
+
+/* One Key Recovery commands */
+#define EC_ONE_KEY_FLAG                             0x9C
+
+/* ASG OEM commands */

include/linux/mfd/ahc1ec0.h

Quoted 10 lines
include/linux/mfd/ahc1ec0.h new file +10 -0
+#define EC_ASG_OEM                                  0xEA
+#define EC_ASG_OEM_READ                             0x00
+#define EC_ASG_OEM_WRITE                    0x01
+#define EC_OEM_POWER_STATUS_VIN1    0X10
+#define EC_OEM_POWER_STATUS_VIN2    0X11
+#define EC_OEM_POWER_STATUS_BAT1    0X12
+#define EC_OEM_POWER_STATUS_BAT2    0X13
+
+/* GPIO DEVICE ID */
+#define EC_DID_ALTGPIO_0                    0x10    /* 0x10 AltGpio0 User

define gpio */

include/linux/mfd/ahc1ec0.h line 200

Quoted 1 line
include/linux/mfd/ahc1ec0.h new file +1 -0
+#define EC_DID_ALTGPIO_1                    0x11    /* 0x11 AltGpio1 User

define gpio */

include/linux/mfd/ahc1ec0.h line 201

Quoted 1 line
include/linux/mfd/ahc1ec0.h new file +1 -0
+#define EC_DID_ALTGPIO_2                    0x12    /* 0x12 AltGpio2 User

define gpio */

include/linux/mfd/ahc1ec0.h line 202

Quoted 1 line
include/linux/mfd/ahc1ec0.h new file +1 -0
+#define EC_DID_ALTGPIO_3                    0x13    /* 0x13 AltGpio3 User

define gpio */

include/linux/mfd/ahc1ec0.h line 203

Quoted 1 line
include/linux/mfd/ahc1ec0.h new file +1 -0
+#define EC_DID_ALTGPIO_4                    0x14    /* 0x14 AltGpio4 User

define gpio */

include/linux/mfd/ahc1ec0.h line 204

Quoted 1 line
include/linux/mfd/ahc1ec0.h new file +1 -0
+#define EC_DID_ALTGPIO_5                    0x15    /* 0x15 AltGpio5 User

define gpio */

include/linux/mfd/ahc1ec0.h line 205

Quoted 1 line
include/linux/mfd/ahc1ec0.h new file +1 -0
+#define EC_DID_ALTGPIO_6                    0x16    /* 0x16 AltGpio6 User

define gpio */

include/linux/mfd/ahc1ec0.h line 206

Quoted 1 line
include/linux/mfd/ahc1ec0.h new file +1 -0
+#define EC_DID_ALTGPIO_7                    0x17    /* 0x17 AltGpio7 User

define gpio */

Quoted 30 lines
include/linux/mfd/ahc1ec0.h new file +30 -0
+
+/* Lmsensor Chip Register */
+#define NSLM96163_CHANNEL                   0x02
+
+/* NS_LM96163 address 0x98 */
+#define NSLM96163_ADDR                              0x98
+
+/* LM96163 index(0x00) Local Temperature (Signed MSB) */
+#define NSLM96163_LOC_TEMP                  0x00
+
+/* HWMON registers */
+#define INA266_REG_VOLTAGE          0x02    /* 1.25mV */
+#define INA266_REG_POWER            0x03    /* 25mW */
+#define INA266_REG_CURRENT          0x04    /* 1mA */
+
+struct ec_hw_pin_table {
+    unsigned int vbat[2];
+    unsigned int v5[2];
+    unsigned int v12[2];
+    unsigned int vcore[2];
+    unsigned int vdc[2];
+    unsigned int ec_current[2];
+    unsigned int power[2];
+};
+
+struct ec_dynamic_table {
+    unsigned char device_id;
+    unsigned char hw_pin_num;
+};
+

include/linux/mfd/ahc1ec0.h line 246

Quoted 10 lines
include/linux/mfd/ahc1ec0.h new file +10 -0
+struct ec_smbuso_em0 {
+    unsigned char hw_pin_num;
+};
+
+struct pled_hw_pin_tbl {
+    unsigned int pled[6];
+};
+
+struct adv_ec_platform_data {
+    char *bios_product_name;

Where is this used?

include/linux/mfd/ahc1ec0.h line 254

Quoted 8 lines
include/linux/mfd/ahc1ec0.h new file +8 -0
+    int sub_dev_nb;
+    u32 sub_dev_mask;
+    struct mutex lock;
+    struct device *dev;
+    struct class *adv_ec_class;
+
+    struct ec_dynamic_table *dym_tbl;
+};

Check whether all of these really need to be in device data i.e. check that they are all used in sub-devices.

include/linux/mfd/ahc1ec0.h line 255

Quoted 1 line
include/linux/mfd/ahc1ec0.h new file +1 -0
+int read_ad_value(struct adv_ec_platform_data *adv_ec_data, unsigned

char hwpin,

include/linux/mfd/ahc1ec0.h line 257

Quoted 2 lines
include/linux/mfd/ahc1ec0.h new file +2 -0
+                    unsigned char multi);
+int read_acpi_value(struct adv_ec_platform_data *adv_ec_data, unsigned

char addr,

include/linux/mfd/ahc1ec0.h line 259

Quoted 2 lines
include/linux/mfd/ahc1ec0.h new file +2 -0
+                    unsigned char *pvalue);
+int write_acpi_value(struct adv_ec_platform_data *adv_ec_data, unsigned

char addr,

include/linux/mfd/ahc1ec0.h line 261

Quoted 2 lines
include/linux/mfd/ahc1ec0.h new file +2 -0
+                    unsigned char value);
+int read_hw_ram(struct adv_ec_platform_data *adv_ec_data, unsigned char

addr,

include/linux/mfd/ahc1ec0.h line 263

Quoted 2 lines
include/linux/mfd/ahc1ec0.h new file +2 -0
+                    unsigned char *data);
+int write_hw_ram(struct adv_ec_platform_data *adv_ec_data, unsigned char

addr,

include/linux/mfd/ahc1ec0.h line 265

Quoted 2 lines
include/linux/mfd/ahc1ec0.h new file +2 -0
+                    unsigned char data);
+int write_hwram_command(struct adv_ec_platform_data *adv_ec_data,

unsigned char data);

include/linux/mfd/ahc1ec0.h line 266

Quoted 1 line
include/linux/mfd/ahc1ec0.h new file +1 -0
+int read_gpio_status(struct adv_ec_platform_data *adv_ec_data, unsigned

char PinNumber,

include/linux/mfd/ahc1ec0.h line 268

Quoted 2 lines
include/linux/mfd/ahc1ec0.h new file +2 -0
+                    unsigned char *pvalue);
+int write_gpio_status(struct adv_ec_platform_data *adv_ec_data, unsigned

char PinNumber,

include/linux/mfd/ahc1ec0.h line 270

Quoted 2 lines
include/linux/mfd/ahc1ec0.h new file +2 -0
+                    unsigned char value);
+int read_gpio_dir(struct adv_ec_platform_data *adv_ec_data, unsigned char

PinNumber,

include/linux/mfd/ahc1ec0.h line 272

Quoted 2 lines
include/linux/mfd/ahc1ec0.h new file +2 -0
+                    unsigned char *pvalue);
+int write_gpio_dir(struct adv_ec_platform_data *adv_ec_data, unsigned

char PinNumber,

include/linux/mfd/ahc1ec0.h line 276

Quoted 3 lines
include/linux/mfd/ahc1ec0.h new file +3 -0
+                    unsigned char value);
+
+#endif /* __LINUX_MFD_AHC1EC0_H */

--
Lee Jones [李琼斯]
Senior Technical Lead - Developer Services

Linaro.org │ Open source software for Arm SoCs
Follow Linaro: Facebook | Twitter | Blog
#11 lore raw

Re: [PATCH v6 4/6] mfd: ahc1ec0: Add support for Advantech embedded controller

In reply to Lee Jones

Please check [Campion] text in below as my reply.

Sorry, due to the mail was rejected by vger.kernel.org as SPAM before so I reply the mail late and had some test email before.

-----------------------------------------------------------------------------------------

Date:   Tue, 9 Mar 2021 16:07:55 +0000
From:   Lee Jones <[email protected]>
Quoted 1 line · Campion Kang

AHC1EC0 is the embedded controller driver for Advantech industrial

It would be nice to have the model number in the subject line.

[Campion] AHC1EC0 is its model name that used in Advantech device internal.
There is no external module name.

Drop "driver".

[Campion] OK

Quoted 2 lines

products. This provides sub-devices such as hwmon and watchdog, and also

"HWMON and Watchdog"

Quoted 1 line

expose functions for sub-devices to read/write the value to embedded

"exposes"

Quoted 24 lines

controller.

Changed since V5:
- Kconfig: add "AHC1EC0" string to clearly define the EC name
- fix the code according to reviewer's suggestion
- remove unnecessary header files
- change the structure name to lower case, align with others
naming

---
 drivers/mfd/Kconfig         |  10 +
 drivers/mfd/Makefile        |   2 +
 drivers/mfd/ahc1ec0.c       | 808 ++++++++++++++++++++++++++++++++++++
 include/linux/mfd/ahc1ec0.h | 276 ++++++++++++
 4 files changed, 1096 insertions(+)
 create mode 100644 drivers/mfd/ahc1ec0.c
 create mode 100644 include/linux/mfd/ahc1ec0.h
drivers/mfd/Kconfig +0 -0
index bdfce7b15621..7d5fb5c17d9a 100644

drivers/mfd/Kconfig config MFD_INTEL_M10_BMC · line 2158

Quoted 5 lines
drivers/mfd/Kconfig +2 -0
@@ -2154,5 +2154,15 @@ config MFD_INTEL_M10_BMC
 	  additional drivers must be enabled in order to use the functionality
 	  of the device.
 
+config MFD_AHC1EC0
+	tristate "Advantech AHC1EC0 Embedded Controller Module"

Please remove "Module"

drivers/mfd/Kconfig config MFD_INTEL_M10_BMC · line 2165

Quoted 7 lines
drivers/mfd/Kconfig +7 -0
+	depends on X86
+	select MFD_CORE
+	help
+	  This is the core function that for Advantech EC drivers. It
+	  includes the sub-devices such as hwmon, watchdog, etc. And also
+	  provides expose functions for sub-devices to read/write the value
+	  to embedded controller.

"This provides core functionality for the Advantech AHC1EC0 Embedded Controller (EC) along with registration for HWMON and Watchdog sub-devices. It also provides read and write APIs for communication with the EC."

[Campion] OK, thanks.

drivers/mfd/Makefile obj-$(CONFIG_MFD_KHADAS_MCU) += khadas-mcu.o · line 271

Quoted 11 lines
drivers/mfd/Kconfig +0 -0
 endmenu
 endif
drivers/mfd/Makefile +1 -0
index 14fdb188af02..a6af9d8825f4 100644
@@ -268,3 +268,5 @@ obj-$(CONFIG_MFD_KHADAS_MCU) 	+= khadas-mcu.o
 obj-$(CONFIG_SGI_MFD_IOC3)	+= ioc3.o
 obj-$(CONFIG_MFD_SIMPLE_MFD_I2C)	+= simple-mfd-i2c.o
 obj-$(CONFIG_MFD_INTEL_M10_BMC)   += intel-m10-bmc.o
+

You don't need to space this out.

drivers/mfd/ahc1ec0.c line 3

Quoted 10 lines
drivers/mfd/Makefile +1 -0
+obj-$(CONFIG_MFD_AHC1EC0)	+= ahc1ec0.o
drivers/mfd/ahc1ec0.c new file +3 -0
new file mode 100644
index 000000000000..015f4307a54e
@@ -0,0 +1,808 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Advantech embedded controller core driver AHC1EC0

Advantech AHC1EC0 Embedded Controller

drivers/mfd/ahc1ec0.c line 5

Quoted 1 line
drivers/mfd/ahc1ec0.c new file +1 -0
+ * Copyright 2020 Advantech IIoT Group

This is out of date.

drivers/mfd/ahc1ec0.c line 5

Quoted 1 line
drivers/mfd/ahc1ec0.c new file +1 -0
+ *

Drop this '\n' please.

Quoted 4 lines
drivers/mfd/ahc1ec0.c new file +4 -0
+ */
+
+#include <linux/acpi.h>
+#include <linux/delay.h>

drivers/mfd/ahc1ec0.c line 20

Quoted 10 lines
drivers/mfd/ahc1ec0.c new file +10 -0
+#include <linux/dmi.h>
+#include <linux/errno.h>
+#include <linux/mfd/ahc1ec0.h>
+#include <linux/mfd/core.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/of_platform.h>
+#include <linux/platform_device.h>
+
+#define DRV_NAME      "ahc1ec0"

Please don't do this. Just use the string in-place.

drivers/mfd/ahc1ec0.c line 30

Quoted 9 lines
drivers/mfd/ahc1ec0.c new file +9 -0
+enum {
+	ADVEC_SUBDEV_BRIGHTNESS = 0,
+	ADVEC_SUBDEV_EEPROM,
+	ADVEC_SUBDEV_GPIO,
+	ADVEC_SUBDEV_HWMON,
+	ADVEC_SUBDEV_LED,
+	ADVEC_SUBDEV_WDT,
+	ADVEC_SUBDEV_MAX,
+};

Are these arbitrary?
[Campion] cannot arbitrary there, due to it is a index to identify its number of sub device.

Quoted 60 lines
drivers/mfd/ahc1ec0.c new file +59 -0
+/* Wait IBF (Input Buffer Full) clear */
+static int ec_wait_write(void)
+{
+	int i;
+
+	for (i = 0; i < EC_MAX_TIMEOUT_COUNT; i++) {
+		if ((inb(EC_COMMAND_PORT) & EC_COMMAND_BIT_IBF) == 0)
+			return 0;
+
+		udelay(EC_RETRY_UDELAY);
+	}
+
+	return -ETIMEDOUT;
+}
 
+/* Wait OBF (Output Buffer Full) data ready */
+static int ec_wait_read(void)
+{
+	int i;
+
+	for (i = 0; i < EC_MAX_TIMEOUT_COUNT; i++) {
+		if ((inb(EC_COMMAND_PORT) & EC_COMMAND_BIT_OBF) != 0)
+			return 0;
+
+		udelay(EC_RETRY_UDELAY);
+	}
+
+	return -ETIMEDOUT;
+}
+
+/* Read data from EC HW RAM, the process is the following:
+ * Step 0. Wait IBF clear to send command
+ * Step 1. Send read command to EC command port
+ * Step 2. Wait IBF clear that means command is got by EC
+ * Step 3. Send read address to EC data port
+ * Step 4. Wait OBF data ready
+ * Step 5. Get data from EC data port
+ */
+int read_hw_ram(struct adv_ec_platform_data *adv_ec_data, unsigned char addr, unsigned char *data)
+{
+	int ret;
+
+	mutex_lock(&adv_ec_data->lock);
+
+	ret = ec_wait_write();
+	if (ret)
+		goto error;
+	outb(EC_HW_RAM_READ, EC_COMMAND_PORT);
+
+	ret = ec_wait_write();
+	if (ret)
+		goto error;
+	outb(addr, EC_STATUS_PORT);
+
+	ret = ec_wait_read();
+	if (ret)
+		goto error;
+	*data = inb(EC_STATUS_PORT);
+
+	mutex_unlock(&adv_ec_data->lock);

drivers/mfd/ahc1ec0.c line 101

Quoted 10 lines
drivers/mfd/ahc1ec0.c new file +10 -0
+
+	return ret;
+
+error:
+	mutex_unlock(&adv_ec_data->lock);
+	dev_err(adv_ec_data->dev, "%s: Wait for IBF or OBF too long. line: %d\n", __func__,
+	       __LINE__);
+
+	return ret;
+}

EXPORT?
[Campion] Yes, thanks.

Quoted 32 lines
drivers/mfd/ahc1ec0.c new file +32 -0
+/* Write data to EC HW RAM
+ * Step 0. Wait IBF clear to send command
+ * Step 1. Send write command to EC command port
+ * Step 2. Wait IBF clear that means command is got by EC
+ * Step 3. Send write address to EC data port
+ * Step 4. Wait IBF clear that means command is got by EC
+ * Step 5. Send data to EC data port
+ */
+int write_hw_ram(struct adv_ec_platform_data *adv_ec_data, unsigned char addr, unsigned char data)
+{
+	int ret;
+
+	mutex_lock(&adv_ec_data->lock);
+
+	ret = ec_wait_write();
+	if (ret)
+		goto error;
+	outb(EC_HW_RAM_WRITE, EC_COMMAND_PORT);
+
+	ret = ec_wait_write();
+	if (ret)
+		goto error;
+	outb(addr, EC_STATUS_PORT);
+
+	ret = ec_wait_write();
+	if (ret)
+		goto error;
+	outb(data, EC_STATUS_PORT);
+
+	mutex_unlock(&adv_ec_data->lock);
+
+	return 0;

drivers/mfd/ahc1ec0.c line 144

Quoted 10 lines
drivers/mfd/ahc1ec0.c new file +10 -0
+
+error:
+	mutex_unlock(&adv_ec_data->lock);
+
+	dev_err(adv_ec_data->dev, "%s: Wait for IBF or OBF too long. line: %d\n", __func__,
+	       __LINE__);
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(write_hw_ram);

If you're going to export these, they should be in their own namespace.

[Campion] Yes, i will rename it to ahc1ec_write_hw_ram().

Quoted 8 lines
drivers/mfd/ahc1ec0.c new file +8 -0
+/* Get dynamic control table */
+static int adv_get_dynamic_tab(struct adv_ec_platform_data *adv_ec_data)
+{
+	int i, ret;
+	unsigned char pin_tmp, device_id;
+
+	mutex_lock(&adv_ec_data->lock);
+

drivers/mfd/ahc1ec0.c line 163

Quoted 10 lines
drivers/mfd/ahc1ec0.c new file +10 -0
+	for (i = 0; i < EC_MAX_TBL_NUM; i++) {
+		adv_ec_data->dym_tbl[i].device_id = 0xff;
+		adv_ec_data->dym_tbl[i].hw_pin_num = 0xff;
+	}
+
+	for (i = 0; i < EC_MAX_TBL_NUM; i++) {
+		ret = ec_wait_write();
+		if (ret) {
+			dev_dbg(adv_ec_data->dev, "%s: ec_wait_write. line: %d\n", __func__,
+				__LINE__);

Please remove all of this debug code.
[Campion] OK.

drivers/mfd/ahc1ec0.c line 166

Quoted 3 lines
drivers/mfd/ahc1ec0.c new file +3 -0
+			goto error;
+		}
+		outb(EC_TBL_WRITE_ITEM, EC_COMMAND_PORT);

More basic commentary is required throughout I think. [Campion] OK.

Quoted 11 lines
drivers/mfd/ahc1ec0.c new file +11 -0
+		ret = ec_wait_write();
+		if (ret) {
+			dev_dbg(adv_ec_data->dev, "%s: ec_wait_write. line: %d\n", __func__,
+				__LINE__);
+			goto error;
+		}
+		outb(i, EC_STATUS_PORT);
+
+		ret = ec_wait_read();
+		if (ret) {
+			dev_dbg(adv_ec_data->dev, "%s: ec_wait_read. line: %d\n", __func__,

drivers/mfd/ahc1ec0.c line 188

Quoted 10 lines
drivers/mfd/ahc1ec0.c new file +10 -0
+				__LINE__);
+			goto error;
+		}
+
+		/*
+		 *  If item is defined, EC will return item number.
+		 *  If table item is not defined, EC will return 0xFF.
+		 */
+		pin_tmp = inb(EC_STATUS_PORT);
+		if (pin_tmp == 0xff) {

Please define all magic numbers.
[Campion] OK

Quoted 44 lines
drivers/mfd/ahc1ec0.c new file +44 -0
+			dev_dbg(adv_ec_data->dev, "%s: inb(EC_STATUS_PORT)=0x%02x != 0xff.\n",
+				__func__, pin_tmp);
+			goto pass;
+		}
+
+		ret = ec_wait_write();
+		if (ret) {
+			dev_dbg(adv_ec_data->dev, "%s: ec_wait_write. line: %d\n", __func__,
+				__LINE__);
+			goto error;
+		}
+		outb(EC_TBL_GET_PIN, EC_COMMAND_PORT);
+
+		ret = ec_wait_read();
+		if (ret) {
+			dev_dbg(adv_ec_data->dev, "%s: ec_wait_read. line: %d\n", __func__,
+				__LINE__);
+			goto error;
+		}
+		pin_tmp = inb(EC_STATUS_PORT) & 0xff;
+		if (pin_tmp == 0xff) {
+			dev_dbg(adv_ec_data->dev, "%s: pin_tmp(0x%02X). line: %d\n", __func__,
+				pin_tmp, __LINE__);
+			goto pass;
+		}
+
+		ret = ec_wait_write();
+		if (ret)
+			goto error;
+		outb(EC_TBL_GET_DEVID, EC_COMMAND_PORT);
+
+		ret = ec_wait_read();
+		if (ret) {
+			dev_dbg(adv_ec_data->dev, "%s: ec_wait_read. line: %d\n", __func__,
+				__LINE__);
+			goto error;
+		}
+		device_id = inb(EC_STATUS_PORT) & 0xff;
+
+		dev_dbg(adv_ec_data->dev, "%s: device_id=0x%02X. line: %d\n", __func__,
+			device_id, __LINE__);
+
+		adv_ec_data->dym_tbl[i].device_id = device_id;
+		adv_ec_data->dym_tbl[i].hw_pin_num = pin_tmp;

drivers/mfd/ahc1ec0.c line 242

Quoted 10 lines
drivers/mfd/ahc1ec0.c new file +10 -0
+	}
+
+pass:
+	mutex_unlock(&adv_ec_data->lock);
+	return 0;
+
+error:
+	mutex_unlock(&adv_ec_data->lock);
+	dev_err(adv_ec_data->dev, "%s: Wait for IBF or OBF too long. line: %d\n",
+		__func__, __LINE__);

Remove all _func_ and _LINE_ info. It's seldom helpful. [Campion] OK.

drivers/mfd/ahc1ec0.c line 246

Quoted 4 lines
drivers/mfd/ahc1ec0.c new file +4 -0
+	return ret;
+}
+
+int read_ad_value(struct adv_ec_platform_data *adv_ec_data, unsigned char hwpin,

What is 'ad'?
[Campion] it is for ADC. I will rename it to 'adc' that more clear.

Quoted 46 lines
drivers/mfd/ahc1ec0.c new file +46 -0
+		unsigned char multi)
+{
+	int ret;
+	u32 ret_val;
+	unsigned int LSB, MSB;
+
+	mutex_lock(&adv_ec_data->lock);
+	ret = ec_wait_write();
+	if (ret)
+		goto error;
+	outb(EC_AD_INDEX_WRITE, EC_COMMAND_PORT);
+
+	ret = ec_wait_write();
+	if (ret)
+		goto error;
+	outb(hwpin, EC_STATUS_PORT);
+
+	ret = ec_wait_read();
+	if (ret)
+		goto error;
+
+	if (inb(EC_STATUS_PORT) == 0xff) {
+		mutex_unlock(&adv_ec_data->lock);
+		return -1;
+	}
+
+	ret = ec_wait_write();
+	if (ret)
+		goto error;
+	outb(EC_AD_LSB_READ, EC_COMMAND_PORT);
+
+	ret = ec_wait_read();
+	if (ret)
+		goto error;
+	LSB = inb(EC_STATUS_PORT);
+
+	ret = ec_wait_write();
+	if (ret)
+		goto error;
+	outb(EC_AD_MSB_READ, EC_COMMAND_PORT);
+
+	ret = ec_wait_read();
+	if (ret)
+		goto error;
+	MSB = inb(EC_STATUS_PORT);
+	ret_val = ((MSB << 8) | LSB) & 0x03FF;

drivers/mfd/ahc1ec0.c line 302

Quoted 10 lines
drivers/mfd/ahc1ec0.c new file +10 -0
+	ret_val = ret_val * multi * 100;
+
+	mutex_unlock(&adv_ec_data->lock);
+	return ret_val;
+
+error:
+	mutex_unlock(&adv_ec_data->lock);
+
+	dev_warn(adv_ec_data->dev, "%s: Wait for IBF or OBF too long. line: %d\n", __func__,
+		__LINE__);

dev_err()
[Campion] OK.

Quoted 31 lines
drivers/mfd/ahc1ec0.c new file +31 -0
+	return ret;
+}
+EXPORT_SYMBOL_GPL(read_ad_value);
+
+int read_acpi_value(struct adv_ec_platform_data *adv_ec_data, unsigned char addr,
+		unsigned char *pvalue)
+{
+	int ret;
+	unsigned char value;
+
+	mutex_lock(&adv_ec_data->lock);
+
+	ret = ec_wait_write();
+	if (ret)
+		goto error;
+	outb(EC_ACPI_RAM_READ, EC_COMMAND_PORT);
+
+	ret = ec_wait_write();
+	if (ret)
+		goto error;
+	outb(addr, EC_STATUS_PORT);
+
+	ret = ec_wait_read();
+	if (ret)
+		goto error;
+	value = inb(EC_STATUS_PORT);
+	*pvalue = value;
+
+	mutex_unlock(&adv_ec_data->lock);
+
+	return 0;

drivers/mfd/ahc1ec0.c line 344

Quoted 10 lines
drivers/mfd/ahc1ec0.c new file +10 -0
+
+error:
+	mutex_unlock(&adv_ec_data->lock);
+
+	dev_warn(adv_ec_data->dev, "%s: Wait for IBF or OBF too long. line: %d\n", __func__,
+		__LINE__);
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(read_acpi_value);

Namespace.
[Campion] OK, rename to 'ahc1ec_read_acpi_value'

Quoted 23 lines
drivers/mfd/ahc1ec0.c new file +23 -0
+int write_acpi_value(struct adv_ec_platform_data *adv_ec_data, unsigned char addr,
+		unsigned char value)
+{
+	int ret;
+
+	mutex_lock(&adv_ec_data->lock);
+
+	ret = ec_wait_write();
+	if (ret)
+		goto error;
+	outb(EC_ACPI_DATA_WRITE, EC_COMMAND_PORT);
+
+	ret = ec_wait_write();
+	if (ret)
+		goto error;
+	outb(addr, EC_STATUS_PORT);
+
+	ret = ec_wait_write();
+	if (ret)
+		goto error;
+	outb(value, EC_STATUS_PORT);
+
+	mutex_unlock(&adv_ec_data->lock);

drivers/mfd/ahc1ec0.c line 378

Quoted 10 lines
drivers/mfd/ahc1ec0.c new file +10 -0
+	return 0;
+
+error:
+	mutex_unlock(&adv_ec_data->lock);
+
+	dev_warn(adv_ec_data->dev, "%s: Wait for IBF or OBF too long. line: %d\n", __func__,
+		__LINE__);
+
+	return ret;
+}

EXPORT?

I think this API (i.e. all of the functions above) should be moved into drivers/platform. They really don't have a place in MFD.

[Campion] this is a common function for upper HWMON and brightness control used.
So far this API only used by HWMON, but then it will be used by brightness in next stage. So i put this API here, OK?

Quoted 191 lines
drivers/mfd/ahc1ec0.c new file +191 -0
+int read_gpio_status(struct adv_ec_platform_data *adv_ec_data, unsigned char PinNumber,
+		unsigned char *pvalue)
+{
+	int ret;
+
+	unsigned char gpio_status_value;
+
+	mutex_lock(&adv_ec_data->lock);
+
+	ret = ec_wait_write();
+	if (ret)
+		goto error;
+	outb(EC_GPIO_INDEX_WRITE, EC_COMMAND_PORT);
+
+	ret = ec_wait_write();
+	if (ret)
+		goto error;
+	outb(PinNumber, EC_STATUS_PORT);
+
+	ret = ec_wait_read();
+	if (ret)
+		goto error;
+
+	if (inb(EC_STATUS_PORT) == 0xff) {
+		dev_err(adv_ec_data->dev, "%s: Read Pin Number error!!\n", __func__);
+		mutex_unlock(&adv_ec_data->lock);
+		return -1;
+	}
+
+	ret = ec_wait_write();
+	if (ret)
+		goto error;
+	outb(EC_GPIO_STATUS_READ, EC_COMMAND_PORT);
+
+	ret = ec_wait_read();
+	if (ret)
+		goto error;
+	gpio_status_value = inb(EC_STATUS_PORT);
+
+	*pvalue = gpio_status_value;
+	mutex_unlock(&adv_ec_data->lock);
+	return 0;
+
+error:
+	mutex_unlock(&adv_ec_data->lock);
+
+	dev_warn(adv_ec_data->dev, "%s: Wait for IBF or OBF too long. line: %d\n", __func__,
+		__LINE__);
+	return ret;
+}
+
+int write_gpio_status(struct adv_ec_platform_data *adv_ec_data, unsigned char PinNumber,
+		unsigned char value)
+{
+	int ret;
+
+	mutex_lock(&adv_ec_data->lock);
+
+	ret = ec_wait_write();
+	if (ret)
+		goto error;
+	outb(EC_GPIO_INDEX_WRITE, EC_COMMAND_PORT);
+
+	ret = ec_wait_write();
+	if (ret)
+		goto error;
+	outb(PinNumber, EC_STATUS_PORT);
+
+	ret = ec_wait_read();
+	if (ret)
+		goto error;
+
+	if (inb(EC_STATUS_PORT) == 0xff) {
+		mutex_unlock(&adv_ec_data->lock);
+		dev_err(adv_ec_data->dev, "%s: Read Pin Number error!!\n", __func__);
+		return -1;
+	}
+
+	ret = ec_wait_write();
+	if (ret)
+		goto error;
+	outb(EC_GPIO_STATUS_WRITE, EC_COMMAND_PORT);
+
+	ret = ec_wait_write();
+	if (ret)
+		goto error;
+	outb(value, EC_STATUS_PORT);
+
+	mutex_unlock(&adv_ec_data->lock);
+	return 0;
+
+error:
+	mutex_unlock(&adv_ec_data->lock);
+	dev_err(adv_ec_data->dev, "%s: Wait for IBF or OBF too long. line: %d", __func__,
+		__LINE__);
+
+	return ret;
+}
+
+int read_gpio_dir(struct adv_ec_platform_data *adv_ec_data, unsigned char PinNumber,
+		unsigned char *pvalue)
+{
+	int ret;
+	unsigned char gpio_dir_value;
+
+	mutex_lock(&adv_ec_data->lock);
+
+	ret = ec_wait_write();
+	if (ret)
+		goto error;
+	outb(EC_GPIO_INDEX_WRITE, EC_COMMAND_PORT);
+
+	ret = ec_wait_write();
+	if (ret)
+		goto error;
+	outb(PinNumber, EC_STATUS_PORT);
+
+	ret = ec_wait_read();
+	if (ret)
+		goto error;
+
+	if (inb(EC_STATUS_PORT) == 0xff) {
+		mutex_unlock(&adv_ec_data->lock);
+		dev_err(adv_ec_data->dev, "%s: Read Pin Number error!! line: %d\n", __func__,
+			__LINE__);
+		return -1;
+	}
+
+	ret = ec_wait_write();
+	if (ret)
+		goto error;
+	outb(EC_GPIO_DIR_READ, EC_COMMAND_PORT);
+
+	ret = ec_wait_read();
+	if (ret)
+		goto error;
+	gpio_dir_value = inb(EC_STATUS_PORT);
+	*pvalue = gpio_dir_value;
+
+	mutex_unlock(&adv_ec_data->lock);
+	return 0;
+
+error:
+	mutex_unlock(&adv_ec_data->lock);
+
+	dev_warn(adv_ec_data->dev, "%s: Wait for IBF or OBF too long. line: %d\n", __func__,
+			__LINE__);
+
+	return ret;
+}
+
+int write_gpio_dir(struct adv_ec_platform_data *adv_ec_data, unsigned char PinNumber,
+		unsigned char value)
+{
+	int ret;
+
+	mutex_lock(&adv_ec_data->lock);
+
+	ret = ec_wait_write();
+	if (ret)
+		goto error;
+	outb(EC_GPIO_INDEX_WRITE, EC_COMMAND_PORT);
+
+	ret = ec_wait_write();
+	if (ret)
+		goto error;
+	outb(PinNumber, EC_STATUS_PORT);
+
+	ret = ec_wait_read();
+	if (ret)
+		goto error;
+
+	if (inb(EC_STATUS_PORT) == 0xff) {
+		mutex_unlock(&adv_ec_data->lock);
+		dev_warn(adv_ec_data->dev, "%s: Read Pin Number error!! line: %d\n", __func__,
+			__LINE__);
+
+		return -1;
+	}
+
+	ret = ec_wait_write();
+	if (ret)
+		goto error;
+	outb(EC_GPIO_DIR_WRITE, EC_COMMAND_PORT);
+
+	ret = ec_wait_write();
+	if (ret)
+		goto error;
+	outb(value, EC_STATUS_PORT);
+
+	mutex_unlock(&adv_ec_data->lock);

drivers/mfd/ahc1ec0.c line 580

Quoted 10 lines
drivers/mfd/ahc1ec0.c new file +10 -0
+	return 0;
+
+error:
+	mutex_unlock(&adv_ec_data->lock);
+
+	dev_warn(adv_ec_data->dev, "%s: Wait for IBF or OBF too long. line: %d\n", __func__,
+			__LINE__);
+
+	return ret;
+}

All of the GPIO functions above should move into drivers/gpio.

[Campion] Due to it has a flow need to cowork with EC chip and firmware, it cannot be interrupt
by other functions. So it needs to keep in here.

Quoted 42 lines
drivers/mfd/ahc1ec0.c new file +42 -0
+int write_hwram_command(struct adv_ec_platform_data *adv_ec_data, unsigned char data)
+{
+	int ret;
+
+	mutex_lock(&adv_ec_data->lock);
+
+	ret = ec_wait_write();
+	if (ret)
+		goto error;
+	outb(data, EC_COMMAND_PORT);
+
+	mutex_unlock(&adv_ec_data->lock);
+	return 0;
+
+error:
+	mutex_unlock(&adv_ec_data->lock);
+
+	dev_warn(adv_ec_data->dev, "%s: Wait for IBF or OBF too long. line: %d\n", __func__,
+			__LINE__);
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(write_hwram_command);
+
+static int adv_ec_get_productname(struct adv_ec_platform_data *adv_ec_data, char *product)
+{
+	const char *vendor, *device;
+	int length = 0;
+
+	/* Check it is Advantech board */
+	vendor = dmi_get_system_info(DMI_SYS_VENDOR);
+	if (memcmp(vendor, "Advantech", sizeof("Advantech")) != 0)
+		return -ENODEV;
+
+	/* Get product model name */
+	device = dmi_get_system_info(DMI_PRODUCT_NAME);
+	if (device) {
+		while ((device[length] != ' ')
+			&& (length < AMI_ADVANTECH_BOARD_ID_LENGTH))
+			length++;
+		memset(product, 0, AMI_ADVANTECH_BOARD_ID_LENGTH);
+		memmove(product, device, length);

drivers/mfd/ahc1ec0.c line 633

Quoted 10 lines
drivers/mfd/ahc1ec0.c new file +10 -0
+
+		dev_info(adv_ec_data->dev, "BIOS Product Name = %s\n", product);
+
+		return 0;
+	}
+
+	dev_warn(adv_ec_data->dev, "This device is not Advantech Board (%s)!\n", product);
+
+	return -ENODEV;
+}

These should go into drivers/platform.

[Campion] This is a simple function to get module name from BIOS DMI table, it is not need to
access EC chip. But it can get once and other drivers can get this information, donot call DMI every time. Can it keep in here?

Quoted 7 lines
drivers/mfd/ahc1ec0.c new file +7 -0
+static const struct mfd_cell adv_ec_sub_cells[] = {
+	{ .name = "adv-ec-brightness", },
+	{ .name = "adv-ec-eeprom", },
+	{ .name = "adv-ec-gpio", },
+	{ .name = "ahc1ec0-hwmon", },
+	{ .name = "adv-ec-led", },
+	{ .name = "ahc1ec0-wdt", },

drivers/mfd/ahc1ec0.c line 651

Quoted 10 lines
drivers/mfd/ahc1ec0.c new file +10 -0
+};
+
+static int adv_ec_init_ec_data(struct adv_ec_platform_data *adv_ec_data)
+{
+	int ret;
+
+	adv_ec_data->sub_dev_mask = 0;
+	adv_ec_data->sub_dev_nb = 0;
+	adv_ec_data->dym_tbl = NULL;
+	adv_ec_data->bios_product_name = NULL;

Why are pre-initialising these?

[Campion] Just make sure they have empty pointer, but I will remove it.

drivers/mfd/ahc1ec0.c line 661

Quoted 9 lines
drivers/mfd/ahc1ec0.c new file +9 -0
+	mutex_init(&adv_ec_data->lock);
+
+	/* Get product name */
+	adv_ec_data->bios_product_name =
+		devm_kzalloc(adv_ec_data->dev, AMI_ADVANTECH_BOARD_ID_LENGTH, GFP_KERNEL);
+	if (!adv_ec_data->bios_product_name)
+		return -ENOMEM;
+
+	memset(adv_ec_data->bios_product_name, 0, AMI_ADVANTECH_BOARD_ID_LENGTH);

Why are you doing this?

[Campion] Just make sure the memory is null all

drivers/mfd/ahc1ec0.c line 671

Quoted 10 lines
drivers/mfd/ahc1ec0.c new file +10 -0
+	ret = adv_ec_get_productname(adv_ec_data, adv_ec_data->bios_product_name);
+	if (ret)
+		return ret;
+
+	/* Get pin table */
+	adv_ec_data->dym_tbl = devm_kzalloc(adv_ec_data->dev,
+					EC_MAX_TBL_NUM * sizeof(struct ec_dynamic_table),
+					GFP_KERNEL);
+	if (!adv_ec_data->dym_tbl)
+		return -ENOMEM;

What does a dynamic table do?

[Campion] The dynamic table is reterived from EC firmware according to different platform HW device.
it will based on dynamic table information to get HW pin definition based on its function. The pin value will retrive to calcute the value, for example, voltage value, vcore value.

drivers/mfd/ahc1ec0.c line 673

Quoted 1 line
drivers/mfd/ahc1ec0.c new file +1 -0
+	ret = adv_get_dynamic_tab(adv_ec_data);

return adv_get_dynamic_tab();

[Campion] OK

drivers/mfd/ahc1ec0.c line 683

Quoted 9 lines
drivers/mfd/ahc1ec0.c new file +9 -0
+	return ret;
+}
+
+static int adv_ec_parse_prop(struct adv_ec_platform_data *adv_ec_data)
+{
+	int i, ret;
+	u32 nb, sub_dev[ADVEC_SUBDEV_MAX];
+
+	ret = device_property_read_u32(adv_ec_data->dev, "advantech,sub-dev-nb", &nb);

Indexing devices is generally not a good strategy.

---------------------------------------------------------------------------
[Campion] Yes, I will remove it, just use the following that defined in ahc1ec0.yaml.

          I ever feedback related mail for "https://lore.kernel.org/linux-watchdog/[email protected]/t/#m5126adbc2453e3ab3e6bda717c257fab364b9f45". 
          But vger.kernel.org returned the mail to mail as spam mail. 
          I will modity it as the following, is it OK?
          examples:
            - |
          #include <dt-bindings/mfd/ahc1ec0-dt.h>
          ahc1ec0 {
                  compatible = "advantech,ahc1ec0";

                  advantech,hwmon-profile = <AHC1EC0_HWMON_PRO_UNO2271G>;
                  advantech,watchdog = true;
          };

----------------------------------------------------------------------------

drivers/mfd/ahc1ec0.c line 688

Quoted 5 lines
drivers/mfd/ahc1ec0.c new file +5 -0
+	if (ret < 0) {
+		dev_err(adv_ec_data->dev, "get sub-dev-nb failed! (%d)\n", ret);
+		return ret;
+	}
+	adv_ec_data->sub_dev_nb = nb;

'nb' is not a good choice for a variable name.

[Campion] I will remove it.

drivers/mfd/ahc1ec0.c line 692

Quoted 3 lines
drivers/mfd/ahc1ec0.c new file +3 -0
+	ret = device_property_read_u32_array(adv_ec_data->dev, "advantech,sub-dev",
+					     sub_dev, nb);
+	if (ret < 0) {

Is '> 0' valid?

[Campion] It will be remove it.

drivers/mfd/ahc1ec0.c line 693

Quoted 1 line
drivers/mfd/ahc1ec0.c new file +1 -0
+		dev_err(adv_ec_data->dev, "get sub-dev failed! (%d)\n", ret);

Please use proper error messages.

"Failed to read 'advantech,sub-dev' property"

[Campion] It will be removed according the above changed.

Quoted 18 lines
drivers/mfd/ahc1ec0.c new file +18 -0
+		return ret;
+	}
+
+	for (i = 0; i < nb; i++) {
+		switch (sub_dev[i]) {
+		case ADVEC_SUBDEV_BRIGHTNESS:
+		case ADVEC_SUBDEV_EEPROM:
+		case ADVEC_SUBDEV_GPIO:
+		case ADVEC_SUBDEV_HWMON:
+		case ADVEC_SUBDEV_LED:
+		case ADVEC_SUBDEV_WDT:
+			adv_ec_data->sub_dev_mask |= BIT(sub_dev[i]);
+			break;
+		default:
+			dev_err(adv_ec_data->dev, "invalid prop value(%d)!\n",
+				sub_dev[i]);
+		}
+	}

drivers/mfd/ahc1ec0.c line 721

Quoted 10 lines
drivers/mfd/ahc1ec0.c new file +10 -0
+	dev_info(adv_ec_data->dev, "sub-dev mask = 0x%x\n", adv_ec_data->sub_dev_mask);
+
+	return 0;
+}
+
+static int adv_ec_probe(struct platform_device *pdev)
+{
+	int ret, i;
+	struct device *dev = &pdev->dev;
+	struct adv_ec_platform_data *adv_ec_data;

This is not platform data. This is driver data.

struct adv_ec_ddata *ddata;

[Campion] OK, I modified it.

drivers/mfd/ahc1ec0.c line 723

Quoted 1 line
drivers/mfd/ahc1ec0.c new file +1 -0
+	adv_ec_data = devm_kzalloc(dev, sizeof(struct adv_ec_platform_data), GFP_KERNEL);

sizeof(*adv_ec_data)

[Campion] OK, also update ahc1ec0-wdt.c and ahc1ec0-hwmon.c.

Quoted 8 lines
drivers/mfd/ahc1ec0.c new file +8 -0
+	if (!adv_ec_data)
+		return -ENOMEM;
+
+	dev_set_drvdata(dev, adv_ec_data);
+	adv_ec_data->dev = dev;
+
+	ret = adv_ec_init_ec_data(adv_ec_data);
+	if (ret)

drivers/mfd/ahc1ec0.c line 741

Quoted 10 lines
drivers/mfd/ahc1ec0.c new file +10 -0
+		goto err_init_data;
+
+	ret = adv_ec_parse_prop(adv_ec_data);
+	if (ret)
+		goto err_prop;
+
+	/* check whether this EC has the following subdevices. */
+	for (i = 0; i < ARRAY_SIZE(adv_ec_sub_cells); i++) {
+		if (adv_ec_data->sub_dev_mask & BIT(i)) {
+			ret = mfd_add_hotplug_devices(dev, &adv_ec_sub_cells[i], 1);

Why have you chosen to use hotplug here?

[Campion] There is a information in BIOS ACPI table according to different device to decide
which function drivers need to be probe. May be a device has HWMON, it will dynamic to load HWMON driver, but other device may not.

drivers/mfd/ahc1ec0.c line 748

Quoted 7 lines
drivers/mfd/ahc1ec0.c new file +7 -0
+			dev_info(adv_ec_data->dev, "mfd_add_hotplug_devices[%d] %s\n", i,
+				adv_ec_sub_cells[i].name);
+			if (ret)
+				dev_err(dev, "failed to add %s subdevice: %d\n",
+					adv_ec_sub_cells[i].name, ret);
+		}
+	}

This is a mess!

Where are you pulling these devices from?

[Campion] decide which drivers need to mount from BIOS ACPI table. This drive would built in Linux Kernel.
I am not sure what's your meaning, can you describe more? Thank you

drivers/mfd/ahc1ec0.c line 760

Quoted 11 lines
drivers/mfd/ahc1ec0.c new file +11 -0
+	dev_info(adv_ec_data->dev, "Advantech EC probe done");
+
+	return 0;
+
+err_prop:
+	dev_err(dev, "failed to probe\n");
+
+err_init_data:
+	mutex_destroy(&adv_ec_data->lock);
+
+	dev_err(dev, "failed to init data\n");

You don't need 2 error messages in the error path.

[Campion] OK, remove one.

drivers/mfd/ahc1ec0.c line 773

Quoted 12 lines
drivers/mfd/ahc1ec0.c new file +12 -0
+	return ret;
+}
+
+static int adv_ec_remove(struct platform_device *pdev)
+{
+	struct adv_ec_platform_data *adv_ec_data;
+
+	adv_ec_data = dev_get_drvdata(&pdev->dev);
+
+	mutex_destroy(&adv_ec_data->lock);
+
+	mfd_remove_devices(&pdev->dev);

If you don't use the hotplug variant, you can use devm_* and omit this.

[Campion] got it.

drivers/mfd/ahc1ec0.c line 781

Quoted 7 lines
drivers/mfd/ahc1ec0.c new file +7 -0
+	return 0;
+}
+
+static const struct of_device_id adv_ec_of_match[] __maybe_unused = {
+	{
+		.compatible = "advantech,ahc1ec0",
+	},

Put this on one line please.

drivers/mfd/ahc1ec0.c line 787

Quoted 6 lines
drivers/mfd/ahc1ec0.c new file +6 -0
+	{}
+};
+MODULE_DEVICE_TABLE(of, adv_ec_of_match);
+
+static const struct acpi_device_id adv_ec_acpi_match[] __maybe_unused = {
+	{"AHC1EC0", 0},

Spaces inside the '{' and '}' please.

drivers/mfd/ahc1ec0.c line 788

Quoted 1 line
drivers/mfd/ahc1ec0.c new file +1 -0
+	{},

',' here, but not on the one above. Please be consistent.

Quoted 7 lines
drivers/mfd/ahc1ec0.c new file +7 -0
+};
+MODULE_DEVICE_TABLE(acpi, adv_ec_acpi_match);
+
+static struct platform_driver adv_ec_driver = {
+	.driver = {
+		.name = DRV_NAME,
+		.of_match_table = of_match_ptr(adv_ec_of_match),

drivers/mfd/ahc1ec0.c line 805

Quoted 10 lines
drivers/mfd/ahc1ec0.c new file +10 -0
+		.acpi_match_table = ACPI_PTR(adv_ec_acpi_match),
+	},
+	.probe = adv_ec_probe,
+	.remove = adv_ec_remove,
+};
+module_platform_driver(adv_ec_driver);
+
+MODULE_LICENSE("GPL");
+MODULE_ALIAS("platform:" DRV_NAME);
+MODULE_DESCRIPTION("Advantech Embedded Controller core driver.");

Name as in the header.

drivers/mfd/ahc1ec0.c line 808

Quoted 3 lines
drivers/mfd/ahc1ec0.c new file +3 -0
+MODULE_AUTHOR("Campion Kang <[email protected]>");
+MODULE_AUTHOR("Jianfeng Dai <[email protected]>");
+MODULE_VERSION("1.0");

Is this used?

[Campion] Yes, it is used. AceLan whom is Canonical Kernel RD was test this drivers for Advantech UNO device on Ubuntu Kernel.

Quoted 242 lines
include/linux/mfd/ahc1ec0.h new file +236 -0
new file mode 100644
index 000000000000..1b01e10c1fef
@@ -0,0 +1,276 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+#ifndef __LINUX_MFD_AHC1EC0_H
+#define __LINUX_MFD_AHC1EC0_H
+
+#include <linux/device.h>
+
+#define EC_COMMAND_PORT             0x29A /* EC I/O command port */
+#define EC_STATUS_PORT              0x299 /* EC I/O data port */
+
+#define EC_RETRY_UDELAY              200 /* EC command retry delay in microseconds */
+#define EC_MAX_TIMEOUT_COUNT        5000 /* EC command max retry count */
+#define EC_COMMAND_BIT_OBF          0x01 /* Bit 0 is for OBF ready (Output buffer full) */
+#define EC_COMMAND_BIT_IBF          0x02 /* Bit 1 is for IBF ready (Input buffer full) */
+
+/* Analog to digital converter command */
+#define EC_AD_INDEX_WRITE   0x15 /* Write ADC port number into index */
+#define EC_AD_LSB_READ      0x16 /* Read ADC LSB value from ADC port */
+#define EC_AD_MSB_READ      0x1F /* Read ADC MSB value from ADC port */
+
+/* Voltage device ID */
+#define EC_DID_SMBOEM0      0x28 /* SMBUS/I2C. Smbus channel 0 */
+#define EC_DID_CMOSBAT      0x50 /* CMOS coin battery voltage */
+#define EC_DID_CMOSBAT_X2   0x51 /* CMOS coin battery voltage*2 */
+#define EC_DID_CMOSBAT_X10  0x52 /* CMOS coin battery voltage*10 */
+#define EC_DID_5VS0         0x56 /* 5VS0 voltage */
+#define EC_DID_5VS0_X2      0x57 /* 5VS0 voltage*2 */
+#define EC_DID_5VS0_X10     0x58 /* 5VS0 voltage*10 */
+#define EC_DID_5VS5         0x59 /* 5VS5 voltage */
+#define EC_DID_5VS5_X2      0x5A /* 5VS5 voltage*2 */
+#define EC_DID_5VS5_X10     0x5B /* 5VS5 voltage*10 */
+#define EC_DID_12VS0        0x62 /* 12VS0 voltage */
+#define EC_DID_12VS0_X2     0x63 /* 12VS0 voltage*2 */
+#define EC_DID_12VS0_X10    0x64 /* 12VS0 voltage*10 */
+#define EC_DID_VCOREA       0x65 /* CPU A core voltage */
+#define EC_DID_VCOREA_X2    0x66 /* CPU A core voltage*2 */
+#define EC_DID_VCOREA_X10   0x67 /* CPU A core voltage*10 */
+#define EC_DID_VCOREB       0x68 /* CPU B core voltage */
+#define EC_DID_VCOREB_X2    0x69 /* CPU B core voltage*2 */
+#define EC_DID_VCOREB_X10   0x6A /* CPU B core voltage*10 */
+#define EC_DID_DC           0x6B /* ADC. onboard voltage */
+#define EC_DID_DC_X2        0x6C /* ADC. onboard voltage*2 */
+#define EC_DID_DC_X10       0x6D /* ADC. onboard voltage*10 */
+
+/* Current device ID */
+#define EC_DID_CURRENT              0x74
+
+/* ACPI commands */
+#define EC_ACPI_RAM_READ            0x80
+#define EC_ACPI_RAM_WRITE           0x81
+
+/*
+ *  Dynamic control table commands
+ *  The table includes HW pin number, Device ID, and Pin polarity
+ */
+#define EC_TBL_WRITE_ITEM           0x20
+#define EC_TBL_GET_PIN              0x21
+#define EC_TBL_GET_DEVID            0x22
+#define EC_MAX_TBL_NUM              32
+
+/* LED Device ID table */
+#define EC_DID_LED_RUN              0xE1
+#define EC_DID_LED_ERR              0xE2
+#define EC_DID_LED_SYS_RECOVERY     0xE3
+#define EC_DID_LED_D105_G           0xE4
+#define EC_DID_LED_D106_G           0xE5
+#define EC_DID_LED_D107_G           0xE6
+
+/* LED control HW RAM address 0xA0-0xAF */
+#define EC_HWRAM_LED_BASE_ADDR      0xA0
+#define EC_HWRAM_LED_PIN(N)         (EC_HWRAM_LED_BASE_ADDR + (4 * (N))) // N:0-3
+#define EC_HWRAM_LED_CTRL_HIBYTE(N) (EC_HWRAM_LED_BASE_ADDR + (4 * (N)) + 1)
+#define EC_HWRAM_LED_CTRL_LOBYTE(N) (EC_HWRAM_LED_BASE_ADDR + (4 * (N)) + 2)
+#define EC_HWRAM_LED_DEVICE_ID(N)   (EC_HWRAM_LED_BASE_ADDR + (4 * (N)) + 3)
+
+/* LED control bit */
+#define LED_CTRL_ENABLE_BIT()           BIT(4)
+#define LED_CTRL_INTCTL_BIT()           BIT(5)
+#define LED_CTRL_LEDBIT_MASK            (0x03FF << 6)
+#define LED_CTRL_POLARITY_MASK          (0x000F << 0)
+#define LED_CTRL_INTCTL_EXTERNAL        0
+#define LED_CTRL_INTCTL_INTERNAL        1
+
+#define LED_DISABLE  0x0
+#define LED_ON       0x1
+#define LED_FAST     0x3
+#define LED_NORMAL   0x5
+#define LED_SLOW     0x7
+#define LED_MANUAL   0xF
+
+#define LED_CTRL_LEDBIT_DISABLE	0x0000
+#define LED_CTRL_LEDBIT_ON		0x03FF
+#define LED_CTRL_LEDBIT_FAST	0x02AA
+#define LED_CTRL_LEDBIT_NORMAL	0x0333
+#define LED_CTRL_LEDBIT_SLOW	0x03E0
+
+/* Get the device name */
+#define AMI_ADVANTECH_BOARD_ID_LENGTH	32
+
+/*
+ * Advantech Embedded Controller watchdog commands
+ * EC can send multi-stage watchdog event. System can setup watchdog event
+ * independently to make up event sequence.
+ */
+#define EC_COMMANS_PORT_IBF_MASK	0x02
+#define EC_RESET_EVENT				0x04
+#define	EC_WDT_START				0x28
+#define	EC_WDT_STOP					0x29
+#define	EC_WDT_RESET				0x2A
+#define	EC_WDT_BOOTTMEWDT_STOP		0x2B
+
+#define EC_HW_RAM					0x89
+
+#define EC_EVENT_FLAG				0x57
+#define EC_ENABLE_DELAY_H			0x58
+#define EC_ENABLE_DELAY_L			0x59
+#define EC_POWER_BTN_TIME_H			0x5A
+#define EC_POWER_BTN_TIME_L			0x5B
+#define EC_RESET_DELAY_TIME_H		0x5E
+#define EC_RESET_DELAY_TIME_L		0x5F
+#define EC_PIN_DELAY_TIME_H			0x60
+#define EC_PIN_DELAY_TIME_L			0x61
+#define EC_SCI_DELAY_TIME_H			0x62
+#define EC_SCI_DELAY_TIME_L			0x63
+
+/* EC ACPI commands */
+#define EC_ACPI_DATA_READ			0x80
+#define EC_ACPI_DATA_WRITE			0x81
+
+/* Brightness ACPI Addr */
+#define BRIGHTNESS_ACPI_ADDR		0x50
+
+/* EC HW RAM commands */
+#define EC_HW_EXTEND_RAM_READ		0x86
+#define EC_HW_EXTEND_RAM_WRITE		0x87
+#define	EC_HW_RAM_READ				0x88
+#define EC_HW_RAM_WRITE				0x89
+
+/* EC Smbus commands */
+#define EC_SMBUS_CHANNEL_SET		0x8A	 /* Set selector number (SMBUS channel) */
+#define EC_SMBUS_ENABLE_I2C			0x8C	 /* Enable channel I2C */
+#define EC_SMBUS_DISABLE_I2C		0x8D	 /* Disable channel I2C */
+
+/* Smbus transmit protocol */
+#define EC_SMBUS_PROTOCOL			0x00
+
+/* SMBUS status */
+#define EC_SMBUS_STATUS				0x01
+
+/* SMBUS device slave address (bit0 must be 0) */
+#define EC_SMBUS_SLV_ADDR			0x02
+
+/* SMBUS device command */
+#define EC_SMBUS_CMD				0x03
+
+/* 0x04-0x24 Data In read process, return data are stored in this address */
+#define EC_SMBUS_DATA				0x04
+
+#define EC_SMBUS_DAT_OFFSET(n)	(EC_SMBUS_DATA + (n))
+
+/* SMBUS channel selector (0-4) */
+#define EC_SMBUS_CHANNEL			0x2B
+
+/* EC SMBUS transmit Protocol code */
+#define SMBUS_QUICK_WRITE			0x02 /* Write Quick Command */
+#define SMBUS_QUICK_READ			0x03 /* Read Quick Command */
+#define SMBUS_BYTE_SEND				0x04 /* Send Byte */
+#define SMBUS_BYTE_RECEIVE			0x05 /* Receive Byte */
+#define SMBUS_BYTE_WRITE			0x06 /* Write Byte */
+#define SMBUS_BYTE_READ				0x07 /* Read Byte */
+#define SMBUS_WORD_WRITE			0x08 /* Write Word */
+#define SMBUS_WORD_READ				0x09 /* Read Word */
+#define SMBUS_BLOCK_WRITE			0x0A /* Write Block */
+#define SMBUS_BLOCK_READ			0x0B /* Read Block */
+#define SMBUS_PROC_CALL				0x0C /* Process Call */
+#define SMBUS_BLOCK_PROC_CALL		0x0D /* Write Block-Read Block Process Call */
+#define SMBUS_I2C_READ_WRITE		0x0E /* I2C block Read-Write */
+#define SMBUS_I2C_WRITE_READ		0x0F /* I2C block Write-Read */
+
+/* GPIO control commands */
+#define EC_GPIO_INDEX_WRITE			0x10
+#define EC_GPIO_STATUS_READ			0x11
+#define EC_GPIO_STATUS_WRITE		0x12
+#define EC_GPIO_DIR_READ			0x1D
+#define EC_GPIO_DIR_WRITE			0x1E
+
+/* One Key Recovery commands */
+#define EC_ONE_KEY_FLAG				0x9C
+
+/* ASG OEM commands */
+#define EC_ASG_OEM					0xEA
+#define EC_ASG_OEM_READ				0x00
+#define EC_ASG_OEM_WRITE			0x01
+#define EC_OEM_POWER_STATUS_VIN1	0X10
+#define EC_OEM_POWER_STATUS_VIN2	0X11
+#define EC_OEM_POWER_STATUS_BAT1	0X12
+#define EC_OEM_POWER_STATUS_BAT2	0X13
+
+/* GPIO DEVICE ID */
+#define EC_DID_ALTGPIO_0			0x10    /* 0x10 AltGpio0 User define gpio */
+#define EC_DID_ALTGPIO_1			0x11    /* 0x11 AltGpio1 User define gpio */
+#define EC_DID_ALTGPIO_2			0x12    /* 0x12 AltGpio2 User define gpio */
+#define EC_DID_ALTGPIO_3			0x13    /* 0x13 AltGpio3 User define gpio */
+#define EC_DID_ALTGPIO_4			0x14    /* 0x14 AltGpio4 User define gpio */
+#define EC_DID_ALTGPIO_5			0x15    /* 0x15 AltGpio5 User define gpio */
+#define EC_DID_ALTGPIO_6			0x16    /* 0x16 AltGpio6 User define gpio */
+#define EC_DID_ALTGPIO_7			0x17    /* 0x17 AltGpio7 User define gpio */
+
+/* Lmsensor Chip Register */
+#define NSLM96163_CHANNEL			0x02
+
+/* NS_LM96163 address 0x98 */
+#define NSLM96163_ADDR				0x98
+
+/* LM96163 index(0x00) Local Temperature (Signed MSB) */
+#define NSLM96163_LOC_TEMP			0x00
+
+/* HWMON registers */
+#define INA266_REG_VOLTAGE          0x02    /* 1.25mV */
+#define INA266_REG_POWER            0x03    /* 25mW */
+#define INA266_REG_CURRENT          0x04    /* 1mA */
+
+struct ec_hw_pin_table {
+	unsigned int vbat[2];
+	unsigned int v5[2];
+	unsigned int v12[2];
+	unsigned int vcore[2];
+	unsigned int vdc[2];
+	unsigned int ec_current[2];
+	unsigned int power[2];
+};
+
+struct ec_dynamic_table {
+	unsigned char device_id;
+	unsigned char hw_pin_num;
+};
+

include/linux/mfd/ahc1ec0.h line 246

Quoted 10 lines
include/linux/mfd/ahc1ec0.h new file +10 -0
+struct ec_smbuso_em0 {
+	unsigned char hw_pin_num;
+};
+
+struct pled_hw_pin_tbl {
+	unsigned int pled[6];
+};
+
+struct adv_ec_platform_data {
+	char *bios_product_name;

Where is this used?

[Campion] Get the module name once and upper application can get it by EC driver.

include/linux/mfd/ahc1ec0.h line 254

Quoted 8 lines
include/linux/mfd/ahc1ec0.h new file +8 -0
+	int sub_dev_nb;
+	u32 sub_dev_mask;
+	struct mutex lock;
+	struct device *dev;
+	struct class *adv_ec_class;
+
+	struct ec_dynamic_table *dym_tbl;
+};

Check whether all of these really need to be in device data i.e. check that they are all used in sub-devices.

[Campion] It seems donot use sub_dev_nb and sub_dev_mask in device data, i will remove it.

Quoted 21 lines
include/linux/mfd/ahc1ec0.h new file +21 -0
+int read_ad_value(struct adv_ec_platform_data *adv_ec_data, unsigned char hwpin,
+			unsigned char multi);
+int read_acpi_value(struct adv_ec_platform_data *adv_ec_data, unsigned char addr,
+			unsigned char *pvalue);
+int write_acpi_value(struct adv_ec_platform_data *adv_ec_data, unsigned char addr,
+			unsigned char value);
+int read_hw_ram(struct adv_ec_platform_data *adv_ec_data, unsigned char addr,
+			unsigned char *data);
+int write_hw_ram(struct adv_ec_platform_data *adv_ec_data, unsigned char addr,
+			unsigned char data);
+int write_hwram_command(struct adv_ec_platform_data *adv_ec_data, unsigned char data);
+int read_gpio_status(struct adv_ec_platform_data *adv_ec_data, unsigned char PinNumber,
+			unsigned char *pvalue);
+int write_gpio_status(struct adv_ec_platform_data *adv_ec_data, unsigned char PinNumber,
+			unsigned char value);
+int read_gpio_dir(struct adv_ec_platform_data *adv_ec_data, unsigned char PinNumber,
+			unsigned char *pvalue);
+int write_gpio_dir(struct adv_ec_platform_data *adv_ec_data, unsigned char PinNumber,
+			unsigned char value);
+
+#endif /* __LINUX_MFD_AHC1EC0_H */
Signature · 4 lines
Lee Jones [李琼斯]
Senior Technical Lead - Developer Services
Linaro.org │ Open source software for Arm SoCs
Follow Linaro: Facebook | Twitter | Blog
#12 lore raw

Re: [PATCH v6 4/6] mfd: ahc1ec0: Add support for Advantech embedded controller

Quoted 2 lines · Campion Kang

Please check [Campion] text in below as my reply.

This is a mess. Please setup your mailer to quote correctly.

Quoted 6 lines

Sorry, due to the mail was rejected by vger.kernel.org as SPAM before so I reply the mail late and had some test email before.

-----------------------------------------------------------------------------------------

Date:   Tue, 9 Mar 2021 16:07:55 +0000
From:   Lee Jones <[email protected]>

[...]

Quoted 4 lines
Quoted 4 lines
drivers/mfd/ahc1ec0.c new file +4 -0
+enum {
+	ADVEC_SUBDEV_BRIGHTNESS = 0,
+	ADVEC_SUBDEV_EEPROM,
+	ADVEC_SUBDEV_GPIO,
Quoted 8 lines

drivers/mfd/ahc1ec0.c line 30

Quoted 5 lines
drivers/mfd/ahc1ec0.c new file +5 -0
+	ADVEC_SUBDEV_HWMON,
+	ADVEC_SUBDEV_LED,
+	ADVEC_SUBDEV_WDT,
+	ADVEC_SUBDEV_MAX,
+};

Are these arbitrary?
[Campion] cannot arbitrary there, due to it is a index to identify its number of sub device.

I'm asking what this is dictated by.

Are these ID/index values written into H/W?

[...]

Quoted 36 lines
Quoted 23 lines
drivers/mfd/ahc1ec0.c new file +23 -0
+int write_acpi_value(struct adv_ec_platform_data *adv_ec_data, unsigned char addr,
+		unsigned char value)
+{
+	int ret;
+
+	mutex_lock(&adv_ec_data->lock);
+
+	ret = ec_wait_write();
+	if (ret)
+		goto error;
+	outb(EC_ACPI_DATA_WRITE, EC_COMMAND_PORT);
+
+	ret = ec_wait_write();
+	if (ret)
+		goto error;
+	outb(addr, EC_STATUS_PORT);
+
+	ret = ec_wait_write();
+	if (ret)
+		goto error;
+	outb(value, EC_STATUS_PORT);
+
+	mutex_unlock(&adv_ec_data->lock);

drivers/mfd/ahc1ec0.c line 378

Quoted 10 lines
drivers/mfd/ahc1ec0.c new file +10 -0
+	return 0;
+
+error:
+	mutex_unlock(&adv_ec_data->lock);
+
+	dev_warn(adv_ec_data->dev, "%s: Wait for IBF or OBF too long. line: %d\n", __func__,
+		__LINE__);
+
+	return ret;
+}

EXPORT?

Quoted 6 lines

I think this API (i.e. all of the functions above) should be moved into drivers/platform. They really don't have a place in MFD.

[Campion] this is a common function for upper HWMON and brightness control used.
So far this API only used by HWMON, but then it will be used by brightness in next stage. So i put this API here, OK?

I think it belongs in drivers/platform. Take a look at some of the other Embedded Controller code that lives there.

Quoted 22 lines
Quoted 9 lines
drivers/mfd/ahc1ec0.c new file +9 -0
+int read_gpio_status(struct adv_ec_platform_data *adv_ec_data, unsigned char PinNumber,
+		unsigned char *pvalue)
+{
+}
+
+int write_gpio_status(struct adv_ec_platform_data *adv_ec_data, unsigned char PinNumber,
+		unsigned char value)
+{
+}

drivers/mfd/ahc1ec0.c line 558

Quoted 10 lines
drivers/mfd/ahc1ec0.c new file +10 -0
+
+int read_gpio_dir(struct adv_ec_platform_data *adv_ec_data, unsigned char PinNumber,
+		unsigned char *pvalue)
+{
+}
+
+int write_gpio_dir(struct adv_ec_platform_data *adv_ec_data, unsigned char PinNumber,
+		unsigned char value)
+{
+}

All of the GPIO functions above should move into drivers/gpio.

Quoted 2 lines

[Campion] Due to it has a flow need to cowork with EC chip and firmware, it cannot be interrupt
by other functions. So it needs to keep in here.

Please provide more details.

Quoted 55 lines
Quoted 42 lines
drivers/mfd/ahc1ec0.c new file +42 -0
+int write_hwram_command(struct adv_ec_platform_data *adv_ec_data, unsigned char data)
+{
+	int ret;
+
+	mutex_lock(&adv_ec_data->lock);
+
+	ret = ec_wait_write();
+	if (ret)
+		goto error;
+	outb(data, EC_COMMAND_PORT);
+
+	mutex_unlock(&adv_ec_data->lock);
+	return 0;
+
+error:
+	mutex_unlock(&adv_ec_data->lock);
+
+	dev_warn(adv_ec_data->dev, "%s: Wait for IBF or OBF too long. line: %d\n", __func__,
+			__LINE__);
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(write_hwram_command);
+
+static int adv_ec_get_productname(struct adv_ec_platform_data *adv_ec_data, char *product)
+{
+	const char *vendor, *device;
+	int length = 0;
+
+	/* Check it is Advantech board */
+	vendor = dmi_get_system_info(DMI_SYS_VENDOR);
+	if (memcmp(vendor, "Advantech", sizeof("Advantech")) != 0)
+		return -ENODEV;
+
+	/* Get product model name */
+	device = dmi_get_system_info(DMI_PRODUCT_NAME);
+	if (device) {
+		while ((device[length] != ' ')
+			&& (length < AMI_ADVANTECH_BOARD_ID_LENGTH))
+			length++;
+		memset(product, 0, AMI_ADVANTECH_BOARD_ID_LENGTH);
+		memmove(product, device, length);

drivers/mfd/ahc1ec0.c line 633

Quoted 10 lines
drivers/mfd/ahc1ec0.c new file +10 -0
+
+		dev_info(adv_ec_data->dev, "BIOS Product Name = %s\n", product);
+
+		return 0;
+	}
+
+	dev_warn(adv_ec_data->dev, "This device is not Advantech Board (%s)!\n", product);
+
+	return -ENODEV;
+}

These should go into drivers/platform.

Quoted 3 lines

[Campion] This is a simple function to get module name from BIOS DMI table, it is not need to
access EC chip. But it can get once and other drivers can get this information, donot call DMI every time. Can it keep in here?

I thought this driver was for the EC chip.

Quoted 20 lines
Quoted 7 lines
drivers/mfd/ahc1ec0.c new file +7 -0
+static const struct mfd_cell adv_ec_sub_cells[] = {
+	{ .name = "adv-ec-brightness", },
+	{ .name = "adv-ec-eeprom", },
+	{ .name = "adv-ec-gpio", },
+	{ .name = "ahc1ec0-hwmon", },
+	{ .name = "adv-ec-led", },
+	{ .name = "ahc1ec0-wdt", },

drivers/mfd/ahc1ec0.c line 651

Quoted 10 lines
drivers/mfd/ahc1ec0.c new file +10 -0
+};
+
+static int adv_ec_init_ec_data(struct adv_ec_platform_data *adv_ec_data)
+{
+	int ret;
+
+	adv_ec_data->sub_dev_mask = 0;
+	adv_ec_data->sub_dev_nb = 0;
+	adv_ec_data->dym_tbl = NULL;
+	adv_ec_data->bios_product_name = NULL;

Why are pre-initialising these?

Quoted 1 line

[Campion] Just make sure they have empty pointer, but I will remove it.

There's no need to do that if they are being allocated below.

Quoted 12 lines

drivers/mfd/ahc1ec0.c line 661

Quoted 9 lines
drivers/mfd/ahc1ec0.c new file +9 -0
+	mutex_init(&adv_ec_data->lock);
+
+	/* Get product name */
+	adv_ec_data->bios_product_name =
+		devm_kzalloc(adv_ec_data->dev, AMI_ADVANTECH_BOARD_ID_LENGTH, GFP_KERNEL);
+	if (!adv_ec_data->bios_product_name)
+		return -ENOMEM;
+
+	memset(adv_ec_data->bios_product_name, 0, AMI_ADVANTECH_BOARD_ID_LENGTH);

Why are you doing this?

Quoted 1 line

[Campion] Just make sure the memory is null all

devm_kzalloc() does that for you - that's what the 'z' means.

Quoted 38 lines

drivers/mfd/ahc1ec0.c line 671

Quoted 10 lines
drivers/mfd/ahc1ec0.c new file +10 -0
+	ret = adv_ec_get_productname(adv_ec_data, adv_ec_data->bios_product_name);
+	if (ret)
+		return ret;
+
+	/* Get pin table */
+	adv_ec_data->dym_tbl = devm_kzalloc(adv_ec_data->dev,
+					EC_MAX_TBL_NUM * sizeof(struct ec_dynamic_table),
+					GFP_KERNEL);
+	if (!adv_ec_data->dym_tbl)
+		return -ENOMEM;

What does a dynamic table do?

[Campion] The dynamic table is reterived from EC firmware according to different platform HW device.
it will based on dynamic table information to get HW pin definition based on its function. The pin value will retrive to calcute the value, for example, voltage value, vcore value.

drivers/mfd/ahc1ec0.c line 673

Quoted 1 line
drivers/mfd/ahc1ec0.c new file +1 -0
+	ret = adv_get_dynamic_tab(adv_ec_data);

return adv_get_dynamic_tab();

[Campion] OK

drivers/mfd/ahc1ec0.c line 683

Quoted 9 lines
drivers/mfd/ahc1ec0.c new file +9 -0
+	return ret;
+}
+
+static int adv_ec_parse_prop(struct adv_ec_platform_data *adv_ec_data)
+{
+	int i, ret;
+	u32 nb, sub_dev[ADVEC_SUBDEV_MAX];
+
+	ret = device_property_read_u32(adv_ec_data->dev, "advantech,sub-dev-nb", &nb);

Indexing devices is generally not a good strategy.

---------------------------------------------------------------------------
[Campion] Yes, I will remove it, just use the following that defined in ahc1ec0.yaml.

Quoted 11 lines
          I ever feedback related mail for "https://lore.kernel.org/linux-watchdog/[email protected]/t/#m5126adbc2453e3ab3e6bda717c257fab364b9f45". 
          But vger.kernel.org returned the mail to mail as spam mail. 
          I will modity it as the following, is it OK?
          examples:
            - |
          #include <dt-bindings/mfd/ahc1ec0-dt.h>
          ahc1ec0 {
                  compatible = "advantech,ahc1ec0";

                  advantech,hwmon-profile = <AHC1EC0_HWMON_PRO_UNO2271G>;
                  advantech,watchdog = true;

Shouldn't the watchdog be it's own sub-node?

Is that functionality not probably at all?

Surely it has it's own register set?

[...]

Quoted 10 lines

drivers/mfd/ahc1ec0.c line 741

Quoted 4 lines
drivers/mfd/ahc1ec0.c new file +4 -0
+	/* check whether this EC has the following subdevices. */
+	for (i = 0; i < ARRAY_SIZE(adv_ec_sub_cells); i++) {
+		if (adv_ec_data->sub_dev_mask & BIT(i)) {
+			ret = mfd_add_hotplug_devices(dev, &adv_ec_sub_cells[i], 1);

Why have you chosen to use hotplug here?

[Campion] There is a information in BIOS ACPI table according to different device to decide
which function drivers need to be probe. May be a device has HWMON, it will dynamic to load HWMON driver, but other device may not.

The only thing hotplug does is hard-code the platform ID.

It's more of a win if you can omit the mfd_remove_devices() call.

Quoted 10 lines

drivers/mfd/ahc1ec0.c line 748

Quoted 7 lines
drivers/mfd/ahc1ec0.c new file +7 -0
+			dev_info(adv_ec_data->dev, "mfd_add_hotplug_devices[%d] %s\n", i,
+				adv_ec_sub_cells[i].name);
+			if (ret)
+				dev_err(dev, "failed to add %s subdevice: %d\n",
+					adv_ec_sub_cells[i].name, ret);
+		}
+	}

This is a mess!

Quoted 4 lines

Where are you pulling these devices from?

[Campion] decide which drivers need to mount from BIOS ACPI table. This drive would built in Linux Kernel.
I am not sure what's your meaning, can you describe more? Thank you

I really don't like the sub_dev_mask idea.

Are the ACPI tables available?

[...]

Quoted 6 lines

include/linux/mfd/ahc1ec0.h line 246

Quoted 2 lines
include/linux/mfd/ahc1ec0.h new file +2 -0
+struct adv_ec_platform_data {
+	char *bios_product_name;

Where is this used?

[Campion] Get the module name once and upper application can get it by EC driver.

From DMI? What do you use it for? Debug prints or something else?

Signature · 4 lines
Lee Jones [李琼斯]
Senior Technical Lead - Developer Services
Linaro.org │ Open source software for Arm SoCs
Follow Linaro: Facebook | Twitter | Blog

A message that is not in the archive (sent off-list or not yet received): [email protected]

#14 lore raw

Re: [PATCH v6 4/6] mfd: ahc1ec0: Add support for Advantech embedded controller

Quoted 42 lines · Campion.Kang

Thanks a lot, please see below descriptions.

Quoted 40 lines

-----Original Message-----
From: Lee Jones <[email protected]>
Sent: Friday, March 19, 2021 10:15 PM
To: Campion.Kang <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH v6 4/6] mfd: ahc1ec0: Add support for Advantech embedded controller

Quoted 2 lines · Campion Kang

Please check [Campion] text in below as my reply.

This is a mess. Please setup your mailer to quote correctly.

Quoted 6 lines

Sorry, due to the mail was rejected by vger.kernel.org as SPAM before so I reply the mail late and had some test email before.

-----------------------------------------------------------------------------------------

Date:   Tue, 9 Mar 2021 16:07:55 +0000
From:   Lee Jones <[email protected]>

[...]

Quoted 12 lines

drivers/mfd/ahc1ec0.c line 30

Quoted 9 lines
drivers/mfd/ahc1ec0.c new file +9 -0
+enum {
+	ADVEC_SUBDEV_BRIGHTNESS = 0,
+	ADVEC_SUBDEV_EEPROM,
+	ADVEC_SUBDEV_GPIO,
+	ADVEC_SUBDEV_HWMON,
+	ADVEC_SUBDEV_LED,
+	ADVEC_SUBDEV_WDT,
+	ADVEC_SUBDEV_MAX,
+};

Are these arbitrary?
[Campion] cannot arbitrary there, due to it is a index to identify its number

Quoted 8 lines
Quoted 6 lines

of sub device.

I'm asking what this is dictated by.

Are these ID/index values written into H/W?

These index written in BIOS ACPI table.

Please consider renaming to make this clear.

ADVEC_ACPI_ID_{DEVICE}

Add a comment to express the importance of the order too.

[...]

Quoted 14 lines
Quoted 8 lines
Quoted 1 line
Quoted 1 line

+int write_gpio_dir(struct adv_ec_platform_data *adv_ec_data, unsigned

char PinNumber,

Quoted 6 lines

drivers/mfd/ahc1ec0.c line 378

Quoted 3 lines
drivers/mfd/ahc1ec0.c new file +3 -0
+		unsigned char value)
+{
+}

All of the GPIO functions above should move into drivers/gpio.

Quoted 5 lines
Quoted 1 line

[Campion] Due to it has a flow need to cowork with EC chip and firmware, it

cannot be interrupt

Quoted 1 line

by other functions. So it needs to keep in here.

Please provide more details.

Quoted 10 lines

These gpio functions are common used for gpio to adjust the gpio direction and access its status. It has a complete lower process with EC that cannot be interrupted likes others. The flow is:
0.mutex lock to get the EC access
1.it needs wait IBF (Input Buffer Full) to be clear and then can send the command
2.Send read command to EC command port
3.wait IBF clear that means command is got by EC
4.send read address to EC data port
5.wait OBF (Output Buffer Full) data ready
6.get data from EC data port
7.mutex unlock
So it cannot be interrupted as other EC lower access due to they use the same mutex to lock the flow.

That's fine. You can share a lock with the GPIO driver.

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

But vger.kernel.org returned the mail to mail as spam mail. I will modity it as the following, is it OK? examples:
- |
#include <dt-bindings/mfd/ahc1ec0-dt.h>
ahc1ec0 {
compatible = "advantech,ahc1ec0";

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

advantech,hwmon-profile =

<AHC1EC0_HWMON_PRO_UNO2271G>;

Quoted 1 line
                  advantech,watchdog = true;

Shouldn't the watchdog be it's own sub-node?

it doesnot need so far.

IMO, it's more clear than having a property.

[...]

Quoted 2 lines

[Campion.Kang] So far, some registers or settings are have default value. They can be adjusted by runtime. Is this ahc1ec0.yaml OK?

Rob has the final say on that.

Signature · 4 lines
Lee Jones [李琼斯]
Senior Technical Lead - Developer Services
Linaro.org │ Open source software for Arm SoCs
Follow Linaro: Facebook | Twitter | Blog
#15 lore raw

PATCH v6 5/6 hwmon: ahc1ec0-hwmon: Add sub-device hwmon for Advantech embedded controller

In reply to Campion Kang

This is one of sub-device driver for Advantech embedded controller AHC1EC0. This driver provides sysfs ABI for Advantech related applications to monitor the system status.

Changed since V5:
- remove unnecessary header files
- Using [devm_]hwmon_device_register_with_info() to register
hwmon driver based on reviewer's suggestion

---
 drivers/hwmon/Kconfig         |  10 +
 drivers/hwmon/Makefile        |   1 +
 drivers/hwmon/ahc1ec0-hwmon.c | 660 ++++++++++++++++++++++++++++++++++
 3 files changed, 671 insertions(+)
 create mode 100644 drivers/hwmon/ahc1ec0-hwmon.c
drivers/hwmon/Kconfig +10 -0
index 1ecf697d8d99..bfa007026679 100644
@@ -2139,6 +2139,16 @@ config SENSORS_INTEL_M10_BMC_HWMON
 	  sensors monitor various telemetry data of different components on the
 	  card, e.g. board temperature, FPGA core temperature/voltage/current.
 
+config SENSORS_AHC1EC0_HWMON
+	tristate "Advantech AHC1EC0 Hardware Monitor Function"
+	depends on MFD_AHC1EC0
+	help
+	  This driver provide support for the hardware monitoring functionality
+	  for Advantech AHC1EC0 embedded controller on the board.
+
+	  This driver provides the sysfs attributes for applications to monitor
+	  the system status, including system temperatures, voltages, current.
+
 if ACPI
 
 comment "ACPI drivers"
drivers/hwmon/Makefile +1 -0
index 09a86c5e1d29..0c37747e8c4f 100644
@@ -45,6 +45,7 @@ obj-$(CONFIG_SENSORS_ADT7411)	+= adt7411.o
 obj-$(CONFIG_SENSORS_ADT7462)	+= adt7462.o
 obj-$(CONFIG_SENSORS_ADT7470)	+= adt7470.o
 obj-$(CONFIG_SENSORS_ADT7475)	+= adt7475.o
+obj-$(CONFIG_SENSORS_AHC1EC0_HWMON)	+= ahc1ec0-hwmon.o
 obj-$(CONFIG_SENSORS_AMD_ENERGY) += amd_energy.o
 obj-$(CONFIG_SENSORS_APPLESMC)	+= applesmc.o
 obj-$(CONFIG_SENSORS_ARM_SCMI)	+= scmi-hwmon.o
drivers/hwmon/ahc1ec0-hwmon.c new file +660 -0

660 lines not on this page · raw message

Signature · 1 line
2.17.1
Tested-by Chia-Lin Kao (AceLan) on v6 5/6
#16 lore raw

Re: [PATCH v6 5/6] hwmon: ahc1ec0-hwmon: Add sub-device hwmon for Advantech embedded controller

Campion Kang <[email protected]> 於 2021年1月18日 週一 下午8:37寫道:

Quoted 718 lines

This is one of sub-device driver for Advantech embedded controller AHC1EC0. This driver provides sysfs ABI for Advantech related applications to monitor the system status.

Changed since V5:
- remove unnecessary header files
- Using [devm_]hwmon_device_register_with_info() to register
hwmon driver based on reviewer's suggestion

---
 drivers/hwmon/Kconfig         |  10 +
 drivers/hwmon/Makefile        |   1 +
 drivers/hwmon/ahc1ec0-hwmon.c | 660 ++++++++++++++++++++++++++++++++++
 3 files changed, 671 insertions(+)
 create mode 100644 drivers/hwmon/ahc1ec0-hwmon.c
drivers/hwmon/Kconfig +10 -0
index 1ecf697d8d99..bfa007026679 100644
@@ -2139,6 +2139,16 @@ config SENSORS_INTEL_M10_BMC_HWMON
          sensors monitor various telemetry data of different components on the
          card, e.g. board temperature, FPGA core temperature/voltage/current.
 
+config SENSORS_AHC1EC0_HWMON
+       tristate "Advantech AHC1EC0 Hardware Monitor Function"
+       depends on MFD_AHC1EC0
+       help
+         This driver provide support for the hardware monitoring functionality
+         for Advantech AHC1EC0 embedded controller on the board.
+
+         This driver provides the sysfs attributes for applications to monitor
+         the system status, including system temperatures, voltages, current.
+
 if ACPI
 
 comment "ACPI drivers"
drivers/hwmon/Makefile +1 -0
index 09a86c5e1d29..0c37747e8c4f 100644
@@ -45,6 +45,7 @@ obj-$(CONFIG_SENSORS_ADT7411) += adt7411.o
 obj-$(CONFIG_SENSORS_ADT7462)  += adt7462.o
 obj-$(CONFIG_SENSORS_ADT7470)  += adt7470.o
 obj-$(CONFIG_SENSORS_ADT7475)  += adt7475.o
+obj-$(CONFIG_SENSORS_AHC1EC0_HWMON)    += ahc1ec0-hwmon.o
 obj-$(CONFIG_SENSORS_AMD_ENERGY) += amd_energy.o
 obj-$(CONFIG_SENSORS_APPLESMC) += applesmc.o
 obj-$(CONFIG_SENSORS_ARM_SCMI) += scmi-hwmon.o
drivers/hwmon/ahc1ec0-hwmon.c new file +660 -1
new file mode 100644
index 000000000000..688f07e6a6e0
@@ -0,0 +1,660 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * HWMON Driver for Advantech Embedded Controller chip AHC1EC0
+ *
+ * Copyright 2020, Advantech IIoT Group
+ *
+ */
+
+#include <linux/device.h>
+#include <linux/errno.h>
+#include <linux/hwmon-sysfs.h>
+#include <linux/hwmon.h>
+#include <linux/mfd/ahc1ec0.h>
+#include <linux/module.h>
+#include <linux/platform_device.h>
+#include <linux/property.h>
+#include <linux/string.h>
+#include <linux/types.h>
+
+struct ec_hwmon_attrs {
+       const char              *name;
+       umode_t                 mode;
+       int (*read)(struct device *dev, long *val);
+};
+
+struct adv_hwmon_profile {
+       int offset;
+       unsigned long resolution, resolution_vin, resolution_sys, resolution_curr, resolution_power;
+       unsigned long r1, r1_vin, r1_sys, r1_curr, r1_power;
+       unsigned long r2, r2_vin, r2_sys, r2_curr, r2_power;
+       int hwmon_in_list_cnt;
+       int temp_list_cnt;
+       int *hwmon_in_list;
+       int *temp_list;
+};
+
+struct ec_hwmon_data {
+       struct device *dev;
+       struct device *hwmon_dev;
+       struct adv_ec_platform_data *adv_ec_data;
+       unsigned long temperature[3];
+       unsigned long ec_current[5];
+       unsigned long power[5];
+       unsigned long voltage[7];
+
+       struct ec_hw_pin_table pin_tbl;
+       struct ec_smbuso_em0 ec_smboem0;
+       struct adv_hwmon_profile *profile;
+};
+
+static int get_ec_in_vbat_input(struct device *dev, long *val);
+static int get_ec_in_v5_input(struct device *dev, long *val);
+static int get_ec_in_v12_input(struct device *dev, long *val);
+static int get_ec_in_vcore_input(struct device *dev, long *val);
+static int get_ec_current1_input(struct device *dev, long *val);
+static int get_ec_cpu_temp(struct device *dev, long *val);
+static int get_ec_sys_temp(struct device *dev, long *val);
+
+const struct ec_hwmon_attrs ec_hwmon_in_attr_template[] = {
+       {"VBAT",        0444, get_ec_in_vbat_input},    // in1
+       {"5VSB",        0444, get_ec_in_v5_input},      // in2
+       {"Vin",         0444, get_ec_in_v12_input},     // in3 (== in8)
+       {"VCORE",       0444, get_ec_in_vcore_input},   // in4
+       {"Vin1",        0444, NULL},    // in5
+       {"Vin2",        0444, NULL},    // in6
+       {"System Voltage", 0444, NULL}, // in7
+       {"Current",     0444, get_ec_current1_input},
+};
+
+const struct ec_hwmon_attrs ec_temp_attrs_template[] = {
+       {"CPU Temp",    0444, get_ec_cpu_temp},
+       {"System Temp", 0444, get_ec_sys_temp},
+};
+
+enum ec_hwmon_in_type {
+       EC_HWMON_IN_VBAT,
+       EC_HWMON_IN_5VSB,
+       EC_HWMON_IN_12V,
+       EC_HWMON_IN_VCORE,
+       EC_HWMON_IN_VIN1,
+       EC_HWMON_IN_VIN2,
+       EC_HWMON_IN_SYS_VOL,
+       EC_HWMON_IN_CURRENT,
+};
+
+enum ec_temp_type {
+       EC_TEMP_CPU,
+       EC_TEMP_SYS,
+};
+
+static int hwmon_in_list_0[] = {
+       EC_HWMON_IN_VBAT,
+       EC_HWMON_IN_5VSB,
+       EC_HWMON_IN_12V,
+       EC_HWMON_IN_VCORE,
+       EC_HWMON_IN_CURRENT,
+};
+
+static int hwmon_in_list_1[] = {
+       EC_HWMON_IN_VBAT,
+       EC_HWMON_IN_5VSB,
+       EC_HWMON_IN_12V,
+       EC_HWMON_IN_VCORE,
+};
+
+static int temp_list_0[] = {
+       EC_TEMP_CPU,
+};
+
+static int temp_list_1[] = {
+       EC_TEMP_CPU,
+       EC_TEMP_SYS,
+};
+
+static struct adv_hwmon_profile advec_profile[] = {
+       /*
+        * TPC-8100TR, TPC-651T-E3AE, TPC-1251T-E3AE, TPC-1551T-E3AE,
+        * TPC-1751T-E3AE, TPC-1051WP-E3AE, TPC-1551WP-E3AE, TPC-1581WP-433AE,
+        * TPC-1782H-433AE, UNO-1483G-434AE, UNO-2483G-434AE, UNO-3483G-374AE,
+        * UNO-2473G, UNO-2484G-6???AE, UNO-2484G-7???AE, UNO-3283G-674AE,
+        * UNO-3285G-674AE
+        * [0] AHC1EC0_HWMON_PRO_TEMPLATE
+        */
+       {
+               .resolution = 2929,
+               .r1 = 1912,
+               .r2 = 1000,
+               .offset = 0,
+               .hwmon_in_list_cnt = ARRAY_SIZE(hwmon_in_list_0),
+               .temp_list_cnt = ARRAY_SIZE(temp_list_0),
+               .hwmon_in_list = hwmon_in_list_0,
+               .temp_list = temp_list_0,
+       },
+       /*
+        * TPC-B500-6??AE, TPC-5???T-6??AE, TPC-5???W-6??AE, TPC-B200-???AE,
+        * TPC-2???T-???AE, TPC-2???W-???AE
+        * [1] AHC1EC0_HWMON_PRO_TPC5XXX
+        */
+       {
+               .resolution = 2929,
+               .r1 = 1912,
+               .r2 = 1000,
+               .offset = 0,
+               .hwmon_in_list_cnt = ARRAY_SIZE(hwmon_in_list_1),
+               .temp_list_cnt = ARRAY_SIZE(temp_list_0),
+               .hwmon_in_list = hwmon_in_list_1,
+               .temp_list = temp_list_0,
+       },
+       /* PR/VR4
+        * [2] AHC1EC0_HWMON_PRO_PRVR4
+        */
+       {
+               .resolution = 2929,
+               .r1 = 1912,
+               .r2 = 1000,
+               .offset = 0,
+               .hwmon_in_list_cnt = ARRAY_SIZE(hwmon_in_list_1),
+               .temp_list_cnt = ARRAY_SIZE(temp_list_1),
+               .hwmon_in_list = hwmon_in_list_1,
+               .temp_list = temp_list_1,
+       },
+       /* UNO-2271G-E22AE/E23AE/E022AE/E023AE,UNO-420
+        * [3] AHC1EC0_HWMON_PRO_UNO2271G
+        */
+       {
+               .resolution = 2929,
+               .r1 = 1912,
+               .r2 = 1000,
+               .offset = 0,
+               .hwmon_in_list_cnt = ARRAY_SIZE(hwmon_in_list_1),
+               .temp_list_cnt = ARRAY_SIZE(temp_list_0),
+               .hwmon_in_list = hwmon_in_list_1,
+               .temp_list = temp_list_0,
+       },
+};
+
+static void adv_ec_init_hwmon_profile(u32 profile, struct ec_hwmon_data *lmsensor_data)
+{
+       int i;
+       struct ec_hw_pin_table *ptbl = &lmsensor_data->pin_tbl;
+       struct adv_ec_platform_data *adv_ec_data = lmsensor_data->adv_ec_data;
+       struct ec_dynamic_table *dym_tbl = adv_ec_data->dym_tbl;
+
+       if (profile >= ARRAY_SIZE(advec_profile))
+               return;
+
+       lmsensor_data->profile = &advec_profile[profile];
+
+       for (i = 0; i < EC_MAX_TBL_NUM ; i++) {
+               switch (dym_tbl[i].device_id) {
+               case EC_DID_CMOSBAT:
+                       ptbl->vbat[0] = dym_tbl[i].hw_pin_num;
+                       ptbl->vbat[1] = 1;
+                       break;
+               case EC_DID_CMOSBAT_X2:
+                       ptbl->vbat[0] = dym_tbl[i].hw_pin_num;
+                       ptbl->vbat[1] = 2;
+                       break;
+               case EC_DID_CMOSBAT_X10:
+                       ptbl->vbat[0] = dym_tbl[i].hw_pin_num;
+                       ptbl->vbat[1] = 10;
+                       break;
+               case EC_DID_5VS0:
+               case EC_DID_5VS5:
+                       ptbl->v5[0] = dym_tbl[i].hw_pin_num;
+                       ptbl->v5[1] = 1;
+                       break;
+               case EC_DID_5VS0_X2:
+               case EC_DID_5VS5_X2:
+                       ptbl->v5[0] = dym_tbl[i].hw_pin_num;
+                       ptbl->v5[1] = 2;
+                       break;
+               case EC_DID_5VS0_X10:
+               case EC_DID_5VS5_X10:
+                       ptbl->v5[0] = dym_tbl[i].hw_pin_num;
+                       ptbl->v5[1] = 10;
+                       break;
+               case EC_DID_12VS0:
+                       ptbl->v12[0] = dym_tbl[i].hw_pin_num;
+                       ptbl->v12[1] = 1;
+                       break;
+               case EC_DID_12VS0_X2:
+                       ptbl->v12[0] = dym_tbl[i].hw_pin_num;
+                       ptbl->v12[1] = 2;
+                       break;
+               case EC_DID_12VS0_X10:
+                       ptbl->v12[0] = dym_tbl[i].hw_pin_num;
+                       ptbl->v12[1] = 10;
+                       break;
+               case EC_DID_VCOREA:
+               case EC_DID_VCOREB:
+                       ptbl->vcore[0] = dym_tbl[i].hw_pin_num;
+                       ptbl->vcore[1] = 1;
+                       break;
+               case EC_DID_VCOREA_X2:
+               case EC_DID_VCOREB_X2:
+                       ptbl->vcore[0] = dym_tbl[i].hw_pin_num;
+                       ptbl->vcore[1] = 2;
+                       break;
+               case EC_DID_VCOREA_X10:
+               case EC_DID_VCOREB_X10:
+                       ptbl->vcore[0] = dym_tbl[i].hw_pin_num;
+                       ptbl->vcore[1] = 10;
+                       break;
+               case EC_DID_DC:
+                       ptbl->vdc[0] = dym_tbl[i].hw_pin_num;
+                       ptbl->vdc[1] = 1;
+                       break;
+               case EC_DID_DC_X2:
+                       ptbl->vdc[0] = dym_tbl[i].hw_pin_num;
+                       ptbl->vdc[1] = 2;
+                       break;
+               case EC_DID_DC_X10:
+                       ptbl->vdc[0] = dym_tbl[i].hw_pin_num;
+                       ptbl->vdc[1] = 10;
+                       break;
+               case EC_DID_CURRENT:
+                       ptbl->ec_current[0] = dym_tbl[i].hw_pin_num;
+                       ptbl->ec_current[1] = 1;
+                       break;
+               case EC_DID_SMBOEM0:
+                       lmsensor_data->ec_smboem0.hw_pin_num = dym_tbl[i].hw_pin_num;
+                       break;
+               default:
+                       break;
+               }
+       }
+}
+
+static int get_ec_in_vbat_input(struct device *dev, long *val)
+{
+       unsigned int temp = 0;
+       unsigned long voltage = 0;
+       struct ec_hwmon_data *lmsensor_data = dev_get_drvdata(dev);
+
+       struct ec_hw_pin_table *ptbl = &lmsensor_data->pin_tbl;
+       struct adv_hwmon_profile *profile = lmsensor_data->profile;
+       struct adv_ec_platform_data *adv_ec_data = lmsensor_data->adv_ec_data;
+
+       temp = read_ad_value(adv_ec_data, ptbl->vbat[0], ptbl->vbat[1]);
+
+       if (profile->r2 != 0)
+               voltage = temp * (profile->r1 + profile->r2) / profile->r2;
+
+       if (profile->resolution != 0)
+               voltage =  temp * profile->resolution / 1000 / 1000;
+
+       if (profile->offset != 0)
+               voltage += (int)profile->offset * 100;
+
+       lmsensor_data->voltage[0] = 10 * voltage;
+
+       *val = lmsensor_data->voltage[0];
+       return 0;
+}
+
+static int get_ec_in_v5_input(struct device *dev, long *val)
+{
+       unsigned int temp;
+       unsigned long voltage = 0;
+       struct ec_hwmon_data *lmsensor_data = dev_get_drvdata(dev);
+
+       struct ec_hw_pin_table *ptbl = &lmsensor_data->pin_tbl;
+       struct adv_hwmon_profile *profile = lmsensor_data->profile;
+       struct adv_ec_platform_data *adv_ec_data = lmsensor_data->adv_ec_data;
+
+       temp = read_ad_value(adv_ec_data, ptbl->v5[0], ptbl->v5[1]);
+
+       if (profile->r2 != 0)
+               voltage = temp * (profile->r1 + profile->r2) / profile->r2;
+
+       if (profile->resolution != 0)
+               voltage =  temp * profile->resolution / 1000 / 1000;
+
+       if (profile->offset != 0)
+               voltage += (int)profile->offset * 100;
+
+       lmsensor_data->voltage[1] = 10 * voltage;
+
+       *val = lmsensor_data->voltage[1];
+       return 0;
+}
+
+static int get_ec_in_v12_input(struct device *dev, long *val)
+{
+       int temp;
+       unsigned long voltage = 0;
+       struct ec_hwmon_data *lmsensor_data = dev_get_drvdata(dev);
+
+       struct ec_hw_pin_table *ptbl = &lmsensor_data->pin_tbl;
+       struct adv_hwmon_profile *profile = lmsensor_data->profile;
+       struct adv_ec_platform_data *adv_ec_data = lmsensor_data->adv_ec_data;
+
+       temp = read_ad_value(adv_ec_data, ptbl->v12[0], ptbl->v12[1]);
+       if (temp == -1)
+               temp = read_ad_value(adv_ec_data, ptbl->vdc[0], ptbl->vdc[1]);
+
+       if (profile->r2 != 0)
+               voltage = temp * (profile->r1 + profile->r2) / profile->r2;
+
+       if (profile->resolution != 0)
+               voltage =  temp * profile->resolution / 1000 / 1000;
+
+       if (profile->offset != 0)
+               voltage += profile->offset * 100;
+
+       lmsensor_data->voltage[2] = 10 * voltage;
+
+       *val = lmsensor_data->voltage[2];
+       return 0;
+}
+
+static int get_ec_in_vcore_input(struct device *dev, long *val)
+{
+       int temp;
+       unsigned int voltage = 0;
+       struct ec_hwmon_data *lmsensor_data = dev_get_drvdata(dev);
+
+       struct ec_hw_pin_table *ptbl = &lmsensor_data->pin_tbl;
+       struct adv_hwmon_profile *profile = lmsensor_data->profile;
+       struct adv_ec_platform_data *adv_ec_data = lmsensor_data->adv_ec_data;
+
+       temp = read_ad_value(adv_ec_data, ptbl->vcore[0], ptbl->vcore[1]);
+
+       if (profile->r2 != 0)
+               voltage = temp * (profile->r1 + profile->r2) / profile->r2;
+
+       if (profile->resolution != 0)
+               voltage = temp * profile->resolution / 1000 / 1000;
+
+       if (profile->offset != 0)
+               voltage += profile->offset * 100;
+
+       lmsensor_data->voltage[3] = 10 * voltage;
+
+       *val = lmsensor_data->voltage[3];
+       return 0;
+}
+
+static int get_ec_current1_input(struct device *dev, long *val)
+{
+       int temp;
+       struct ec_hwmon_data *lmsensor_data = dev_get_drvdata(dev);
+
+       struct ec_hw_pin_table *ptbl = &lmsensor_data->pin_tbl;
+       struct adv_hwmon_profile *profile = lmsensor_data->profile;
+       struct adv_ec_platform_data *adv_ec_data = lmsensor_data->adv_ec_data;
+
+       temp = read_ad_value(adv_ec_data, ptbl->ec_current[0], ptbl->ec_current[1]);
+
+       if (profile->r2 != 0)
+               temp = temp * (profile->r1 + profile->r2) / profile->r2;
+
+       if (profile->resolution != 0)
+               temp = temp * profile->resolution / 1000 / 1000;
+
+       if (profile->offset != 0)
+               temp += profile->offset * 100;
+
+       lmsensor_data->ec_current[3] = 10 * temp;
+
+       *val = lmsensor_data->ec_current[3];
+       return 0;
+}
+
+static int get_ec_cpu_temp(struct device *dev, long *val)
+{
+       #define EC_ACPI_THERMAL1_REMOTE_TEMP 0x61
+
+       unsigned char value;
+       struct ec_hwmon_data *lmsensor_data = dev_get_drvdata(dev);
+       struct adv_ec_platform_data *adv_ec_data = lmsensor_data->adv_ec_data;
+
+       read_acpi_value(adv_ec_data, EC_ACPI_THERMAL1_REMOTE_TEMP, &value);
+       *val = 1000 * value;
+       return 0;
+}
+
+static int get_ec_sys_temp(struct device *dev, long *val)
+{
+       #define EC_ACPI_THERMAL1_LOCAL_TEMP 0x60
+
+       unsigned char value;
+       struct ec_hwmon_data *lmsensor_data = dev_get_drvdata(dev);
+       struct adv_ec_platform_data *adv_ec_data = lmsensor_data->adv_ec_data;
+
+       read_acpi_value(adv_ec_data, EC_ACPI_THERMAL1_LOCAL_TEMP, &value);
+       *val = 1000 * value;
+       return 0;
+}
+
+
+static int
+ahc1ec0_read_in(struct device *dev, u32 attr, int channel, long *val)
+{
+       struct ec_hwmon_data *lmsensor_data = dev_get_drvdata(dev);
+
+       if (!(lmsensor_data && lmsensor_data->profile))
+               return -EINVAL;
+
+       if (attr == hwmon_in_input &&
+               lmsensor_data->profile->hwmon_in_list_cnt > channel) {
+               int index = lmsensor_data->profile->hwmon_in_list[channel];
+               const struct ec_hwmon_attrs *ec_hwmon_attr = &ec_hwmon_in_attr_template[index];
+
+               return ec_hwmon_attr->read(dev, val);
+       }
+
+       return -EOPNOTSUPP;
+}
+
+static int
+ahc1ec0_read_temp(struct device *dev, u32 attr, int channel, long *val)
+{
+       struct ec_hwmon_data *lmsensor_data = dev_get_drvdata(dev);
+
+       if (!(lmsensor_data && lmsensor_data->profile))
+               return -EINVAL;
+
+       switch (attr) {
+       case hwmon_temp_input:
+               if (lmsensor_data->profile->temp_list_cnt > channel) {
+                       int index = lmsensor_data->profile->temp_list[channel];
+                       const struct ec_hwmon_attrs *devec_hwmon_attr =
+                               &ec_temp_attrs_template[index];
+
+                       return devec_hwmon_attr->read(dev, val);
+               }
+               return -EOPNOTSUPP;
+       case hwmon_temp_crit:
+               // both CPU temp and System temp are all this value
+               *val = 100000;
+               return 0;
+       default:
+               return -EOPNOTSUPP;
+       }
+}
+
+static int
+ahc1ec0_read_string(struct device *dev,
+                                       enum hwmon_sensor_types type,
+                                       u32 attr,
+                                       int channel,
+                                       const char **str)
+{
+       struct ec_hwmon_data *lmsensor_data = dev_get_drvdata(dev);
+
+       if (!(lmsensor_data && lmsensor_data->profile))
+               return -EINVAL;
+
+       if ((type == hwmon_in && attr == hwmon_in_label) &&
+               (lmsensor_data->profile->hwmon_in_list_cnt > channel)) {
+               int index = lmsensor_data->profile->hwmon_in_list[channel];
+               const struct ec_hwmon_attrs *ec_hwmon_attr = &ec_hwmon_in_attr_template[index];
+
+               *str = ec_hwmon_attr->name;
+               return 0;
+       }
+
+       if ((type == hwmon_temp && attr == hwmon_temp_label) &&
+               (lmsensor_data->profile->temp_list_cnt > channel)) {
+               int index = lmsensor_data->profile->temp_list[channel];
+               const struct ec_hwmon_attrs *ec_hwmon_attr = &ec_temp_attrs_template[index];
+
+               *str = ec_hwmon_attr->name;
+               return 0;
+       }
+
+       return -EOPNOTSUPP;
+}
+
+static int
+ahc1ec0_read(struct device *dev,
+                       enum hwmon_sensor_types type,
+                       u32 attr,
+                       int channel,
+                       long *val)
+{
+       switch (type) {
+       case hwmon_in:
+               return ahc1ec0_read_in(dev, attr, channel, val);
+       case hwmon_temp:
+               return ahc1ec0_read_temp(dev, attr, channel, val);
+       default:
+               return -EOPNOTSUPP;
+       }
+}
+
+static umode_t
+ec_hwmon_in_visible(const void *data, u32 attr, int channel)
+{
+       switch (attr) {
+       case hwmon_in_input:
+       case hwmon_in_label:
+               return 0444;
+       default:
+               return 0;
+       }
+}
+
+static umode_t
+ec_temp_in_visible(const void *data, u32 attr, int channel)
+{
+       switch (attr) {
+       case hwmon_temp_input:
+       case hwmon_temp_crit:
+       case hwmon_temp_label:
+               return 0444;
+       default:
+               return 0;
+       }
+}
+
+static umode_t
+ahc1ec0_is_visible(const void *data,
+                                       enum hwmon_sensor_types type, u32 attr, int channel)
+{
+       switch (type) {
+       case hwmon_in:
+               return ec_hwmon_in_visible(data, attr, channel);
+       case hwmon_temp:
+               return ec_temp_in_visible(data, attr, channel);
+       default:
+               return 0;
+       }
+}
+
+static const u32 ahc1ec0_in_config[] = {
+       HWMON_I_INPUT | HWMON_I_LABEL,
+       HWMON_I_INPUT | HWMON_I_LABEL,
+       HWMON_I_INPUT | HWMON_I_LABEL,
+       HWMON_I_INPUT | HWMON_I_LABEL,
+       0
+};
+
+static const struct hwmon_channel_info ahc1ec0_in = {
+       .type = hwmon_in,
+       .config = ahc1ec0_in_config,
+};
+
+static const u32 ahc1ec0_temp_config[] = {
+       HWMON_T_INPUT | HWMON_T_CRIT | HWMON_T_LABEL,
+       0
+};
+
+static const struct hwmon_channel_info ahc1ec0_temp = {
+       .type = hwmon_temp,
+       .config = ahc1ec0_temp_config,
+};
+
+static const struct hwmon_channel_info *ahc1ec0_info[] = {
+       &ahc1ec0_in,
+       &ahc1ec0_temp,
+       NULL
+};
+
+static const struct hwmon_ops ahc1ec0_hwmon_ops = {
+       .is_visible = ahc1ec0_is_visible,
+       .read = ahc1ec0_read,
+       .read_string = ahc1ec0_read_string,
+};
+
+static const struct hwmon_chip_info ahc1ec0_chip_info = {
+       .ops = &ahc1ec0_hwmon_ops,
+       .info = ahc1ec0_info,
+};
+
+static int adv_ec_hwmon_probe(struct platform_device *pdev)
+{
+       int ret;
+       u32 profile;
+       struct device *dev = &pdev->dev;
+       struct adv_ec_platform_data *adv_ec_data;
+       struct ec_hwmon_data *lmsensor_data;
+
+       adv_ec_data = dev_get_drvdata(dev->parent);
+       if (!adv_ec_data)
+               return -EINVAL;
+
+       ret = device_property_read_u32(dev->parent, "advantech,hwmon-profile", &profile);
+       if (ret < 0) {
+               dev_err(dev, "get hwmon-profile failed! (%d)", ret);
+               return ret;
+       }
+
+       if (!(profile < ARRAY_SIZE(advec_profile))) {
+               dev_err(dev, "not support hwmon profile(%d)!\n", profile);
+               return -EINVAL;
+       }
+
+       lmsensor_data = devm_kzalloc(dev, sizeof(struct ec_hwmon_data), GFP_KERNEL);
+       if (!lmsensor_data)
+               return -ENOMEM;
+
+       lmsensor_data->adv_ec_data = adv_ec_data;
+       lmsensor_data->dev = dev;
+       dev_set_drvdata(dev, lmsensor_data);
+
+       adv_ec_init_hwmon_profile(profile, lmsensor_data);
+
+       lmsensor_data->hwmon_dev  = devm_hwmon_device_register_with_info(dev,
+                       "ahc1ec0.hwmon", lmsensor_data, &ahc1ec0_chip_info, NULL);
+
+       return PTR_ERR_OR_ZERO(lmsensor_data->hwmon_dev);
+}
+
+static struct platform_driver adv_hwmon_drv = {
+       .driver = {
+               .name = "ahc1ec0-hwmon",
+       },
+       .probe = adv_ec_hwmon_probe,
+};
+module_platform_driver(adv_hwmon_drv);
+
+MODULE_LICENSE("Dual BSD/GPL");
+MODULE_ALIAS("platform:ahc1ec0-hwmon");
+MODULE_DESCRIPTION("Advantech Embedded Controller HWMON Driver.");
+MODULE_AUTHOR("Campion Kang <[email protected]>");
+MODULE_AUTHOR("Jianfeng Dai <[email protected]>");
+MODULE_VERSION("1.0");
--
Quoted 2 lines

2.17.1

maintainer
#17 lore raw

Re: [PATCH v6 5/6] hwmon: ahc1ec0-hwmon: Add sub-device hwmon for Advantech embedded controller

In reply to Campion Kang
Quoted 11 lines · Campion Kang

This is one of sub-device driver for Advantech embedded controller AHC1EC0. This driver provides sysfs ABI for Advantech related applications to monitor the system status.

Changed since V5:
- remove unnecessary header files
- Using [devm_]hwmon_device_register_with_info() to register
hwmon driver based on reviewer's suggestion

checkpatch --strict says:

total: 0 errors, 1 warnings, 10 checks, 683 lines checked

The MAINTAINERS warning is irrelevant, but there are a number of alignment and other style issues. Please fix those.

Quoted 4 lines
---
 drivers/hwmon/Kconfig         |  10 +
 drivers/hwmon/Makefile        |   1 +
 drivers/hwmon/ahc1ec0-hwmon.c | 660 ++++++++++++++++++++++++++++++++++

Documentation is missing.

Quoted 90 lines
 3 files changed, 671 insertions(+)
 create mode 100644 drivers/hwmon/ahc1ec0-hwmon.c
drivers/hwmon/Kconfig +10 -0
index 1ecf697d8d99..bfa007026679 100644
@@ -2139,6 +2139,16 @@ config SENSORS_INTEL_M10_BMC_HWMON
 	  sensors monitor various telemetry data of different components on the
 	  card, e.g. board temperature, FPGA core temperature/voltage/current.
 
+config SENSORS_AHC1EC0_HWMON
+	tristate "Advantech AHC1EC0 Hardware Monitor Function"
+	depends on MFD_AHC1EC0
+	help
+	  This driver provide support for the hardware monitoring functionality
+	  for Advantech AHC1EC0 embedded controller on the board.
+
+	  This driver provides the sysfs attributes for applications to monitor
+	  the system status, including system temperatures, voltages, current.
+
 if ACPI
 
 comment "ACPI drivers"
drivers/hwmon/Makefile +1 -0
index 09a86c5e1d29..0c37747e8c4f 100644
@@ -45,6 +45,7 @@ obj-$(CONFIG_SENSORS_ADT7411)	+= adt7411.o
 obj-$(CONFIG_SENSORS_ADT7462)	+= adt7462.o
 obj-$(CONFIG_SENSORS_ADT7470)	+= adt7470.o
 obj-$(CONFIG_SENSORS_ADT7475)	+= adt7475.o
+obj-$(CONFIG_SENSORS_AHC1EC0_HWMON)	+= ahc1ec0-hwmon.o
 obj-$(CONFIG_SENSORS_AMD_ENERGY) += amd_energy.o
 obj-$(CONFIG_SENSORS_APPLESMC)	+= applesmc.o
 obj-$(CONFIG_SENSORS_ARM_SCMI)	+= scmi-hwmon.o
drivers/hwmon/ahc1ec0-hwmon.c new file +48 -0
new file mode 100644
index 000000000000..688f07e6a6e0
@@ -0,0 +1,660 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * HWMON Driver for Advantech Embedded Controller chip AHC1EC0
+ *
+ * Copyright 2020, Advantech IIoT Group
+ *
+ */
+
+#include <linux/device.h>
+#include <linux/errno.h>
+#include <linux/hwmon-sysfs.h>
+#include <linux/hwmon.h>
+#include <linux/mfd/ahc1ec0.h>
+#include <linux/module.h>
+#include <linux/platform_device.h>
+#include <linux/property.h>
+#include <linux/string.h>
+#include <linux/types.h>
+
+struct ec_hwmon_attrs {
+	const char		*name;
+	umode_t			mode;
+	int (*read)(struct device *dev, long *val);
+};
+
+struct adv_hwmon_profile {
+	int offset;
+	unsigned long resolution, resolution_vin, resolution_sys, resolution_curr, resolution_power;
+	unsigned long r1, r1_vin, r1_sys, r1_curr, r1_power;
+	unsigned long r2, r2_vin, r2_sys, r2_curr, r2_power;
+	int hwmon_in_list_cnt;
+	int temp_list_cnt;
+	int *hwmon_in_list;
+	int *temp_list;
+};
+
+struct ec_hwmon_data {
+	struct device *dev;
+	struct device *hwmon_dev;
+	struct adv_ec_platform_data *adv_ec_data;
+	unsigned long temperature[3];
+	unsigned long ec_current[5];
+	unsigned long power[5];
+	unsigned long voltage[7];
+
+	struct ec_hw_pin_table pin_tbl;
+	struct ec_smbuso_em0 ec_smboem0;
+	struct adv_hwmon_profile *profile;

drivers/hwmon/ahc1ec0-hwmon.c line 58

Quoted 10 lines
drivers/hwmon/ahc1ec0-hwmon.c new file +10 -0
+};
+
+static int get_ec_in_vbat_input(struct device *dev, long *val);
+static int get_ec_in_v5_input(struct device *dev, long *val);
+static int get_ec_in_v12_input(struct device *dev, long *val);
+static int get_ec_in_vcore_input(struct device *dev, long *val);
+static int get_ec_current1_input(struct device *dev, long *val);
+static int get_ec_cpu_temp(struct device *dev, long *val);
+static int get_ec_sys_temp(struct device *dev, long *val);
+

Please reorder code to not require those declarations.

Quoted 53 lines
drivers/hwmon/ahc1ec0-hwmon.c new file +53 -0
+const struct ec_hwmon_attrs ec_hwmon_in_attr_template[] = {
+	{"VBAT",	0444, get_ec_in_vbat_input},	// in1
+	{"5VSB",	0444, get_ec_in_v5_input},	// in2
+	{"Vin",		0444, get_ec_in_v12_input},	// in3 (== in8)
+	{"VCORE",	0444, get_ec_in_vcore_input},	// in4
+	{"Vin1",	0444, NULL},	// in5
+	{"Vin2",	0444, NULL},	// in6
+	{"System Voltage", 0444, NULL},	// in7
+	{"Current",	0444, get_ec_current1_input},
+};
+
+const struct ec_hwmon_attrs ec_temp_attrs_template[] = {
+	{"CPU Temp",	0444, get_ec_cpu_temp},
+	{"System Temp",	0444, get_ec_sys_temp},
+};
+
+enum ec_hwmon_in_type {
+	EC_HWMON_IN_VBAT,
+	EC_HWMON_IN_5VSB,
+	EC_HWMON_IN_12V,
+	EC_HWMON_IN_VCORE,
+	EC_HWMON_IN_VIN1,
+	EC_HWMON_IN_VIN2,
+	EC_HWMON_IN_SYS_VOL,
+	EC_HWMON_IN_CURRENT,
+};
+
+enum ec_temp_type {
+	EC_TEMP_CPU,
+	EC_TEMP_SYS,
+};
+
+static int hwmon_in_list_0[] = {
+	EC_HWMON_IN_VBAT,
+	EC_HWMON_IN_5VSB,
+	EC_HWMON_IN_12V,
+	EC_HWMON_IN_VCORE,
+	EC_HWMON_IN_CURRENT,
+};
+
+static int hwmon_in_list_1[] = {
+	EC_HWMON_IN_VBAT,
+	EC_HWMON_IN_5VSB,
+	EC_HWMON_IN_12V,
+	EC_HWMON_IN_VCORE,
+};
+
+static int temp_list_0[] = {
+	EC_TEMP_CPU,
+};
+
+static int temp_list_1[] = {
+	EC_TEMP_CPU,

drivers/hwmon/ahc1ec0-hwmon.c line 121

Quoted 10 lines
drivers/hwmon/ahc1ec0-hwmon.c new file +10 -0
+	EC_TEMP_SYS,
+};
+
+static struct adv_hwmon_profile advec_profile[] = {
+	/*
+	 * TPC-8100TR, TPC-651T-E3AE, TPC-1251T-E3AE, TPC-1551T-E3AE,
+	 * TPC-1751T-E3AE, TPC-1051WP-E3AE, TPC-1551WP-E3AE, TPC-1581WP-433AE,
+	 * TPC-1782H-433AE, UNO-1483G-434AE, UNO-2483G-434AE, UNO-3483G-374AE,
+	 * UNO-2473G, UNO-2484G-6???AE, UNO-2484G-7???AE, UNO-3283G-674AE,
+	 * UNO-3285G-674AE

What is all this ? Please add a brief explanation (affected hardware ? chips ?)

Quoted 54 lines
drivers/hwmon/ahc1ec0-hwmon.c new file +54 -0
+	 * [0] AHC1EC0_HWMON_PRO_TEMPLATE
+	 */
+	{
+		.resolution = 2929,
+		.r1 = 1912,
+		.r2 = 1000,
+		.offset = 0,
+		.hwmon_in_list_cnt = ARRAY_SIZE(hwmon_in_list_0),
+		.temp_list_cnt = ARRAY_SIZE(temp_list_0),
+		.hwmon_in_list = hwmon_in_list_0,
+		.temp_list = temp_list_0,
+	},
+	/*
+	 * TPC-B500-6??AE, TPC-5???T-6??AE, TPC-5???W-6??AE, TPC-B200-???AE,
+	 * TPC-2???T-???AE, TPC-2???W-???AE
+	 * [1] AHC1EC0_HWMON_PRO_TPC5XXX
+	 */
+	{
+		.resolution = 2929,
+		.r1 = 1912,
+		.r2 = 1000,
+		.offset = 0,
+		.hwmon_in_list_cnt = ARRAY_SIZE(hwmon_in_list_1),
+		.temp_list_cnt = ARRAY_SIZE(temp_list_0),
+		.hwmon_in_list = hwmon_in_list_1,
+		.temp_list = temp_list_0,
+	},
+	/* PR/VR4
+	 * [2] AHC1EC0_HWMON_PRO_PRVR4
+	 */
+	{
+		.resolution = 2929,
+		.r1 = 1912,
+		.r2 = 1000,
+		.offset = 0,
+		.hwmon_in_list_cnt = ARRAY_SIZE(hwmon_in_list_1),
+		.temp_list_cnt = ARRAY_SIZE(temp_list_1),
+		.hwmon_in_list = hwmon_in_list_1,
+		.temp_list = temp_list_1,
+	},
+	/* UNO-2271G-E22AE/E23AE/E022AE/E023AE,UNO-420
+	 * [3] AHC1EC0_HWMON_PRO_UNO2271G
+	 */
+	{
+		.resolution = 2929,
+		.r1 = 1912,
+		.r2 = 1000,
+		.offset = 0,
+		.hwmon_in_list_cnt = ARRAY_SIZE(hwmon_in_list_1),
+		.temp_list_cnt = ARRAY_SIZE(temp_list_0),
+		.hwmon_in_list = hwmon_in_list_1,
+		.temp_list = temp_list_0,
+	},
+};

drivers/hwmon/ahc1ec0-hwmon.c line 185

Quoted 10 lines
drivers/hwmon/ahc1ec0-hwmon.c new file +10 -0
+
+static void adv_ec_init_hwmon_profile(u32 profile, struct ec_hwmon_data *lmsensor_data)
+{
+	int i;
+	struct ec_hw_pin_table *ptbl = &lmsensor_data->pin_tbl;
+	struct adv_ec_platform_data *adv_ec_data = lmsensor_data->adv_ec_data;
+	struct ec_dynamic_table *dym_tbl = adv_ec_data->dym_tbl;
+
+	if (profile >= ARRAY_SIZE(advec_profile))
+		return;

This can not happen. Please no unnecessary error checks.

Quoted 107 lines
drivers/hwmon/ahc1ec0-hwmon.c new file +107 -0
+
+	lmsensor_data->profile = &advec_profile[profile];
+
+	for (i = 0; i < EC_MAX_TBL_NUM ; i++) {
+		switch (dym_tbl[i].device_id) {
+		case EC_DID_CMOSBAT:
+			ptbl->vbat[0] = dym_tbl[i].hw_pin_num;
+			ptbl->vbat[1] = 1;
+			break;
+		case EC_DID_CMOSBAT_X2:
+			ptbl->vbat[0] = dym_tbl[i].hw_pin_num;
+			ptbl->vbat[1] = 2;
+			break;
+		case EC_DID_CMOSBAT_X10:
+			ptbl->vbat[0] = dym_tbl[i].hw_pin_num;
+			ptbl->vbat[1] = 10;
+			break;
+		case EC_DID_5VS0:
+		case EC_DID_5VS5:
+			ptbl->v5[0] = dym_tbl[i].hw_pin_num;
+			ptbl->v5[1] = 1;
+			break;
+		case EC_DID_5VS0_X2:
+		case EC_DID_5VS5_X2:
+			ptbl->v5[0] = dym_tbl[i].hw_pin_num;
+			ptbl->v5[1] = 2;
+			break;
+		case EC_DID_5VS0_X10:
+		case EC_DID_5VS5_X10:
+			ptbl->v5[0] = dym_tbl[i].hw_pin_num;
+			ptbl->v5[1] = 10;
+			break;
+		case EC_DID_12VS0:
+			ptbl->v12[0] = dym_tbl[i].hw_pin_num;
+			ptbl->v12[1] = 1;
+			break;
+		case EC_DID_12VS0_X2:
+			ptbl->v12[0] = dym_tbl[i].hw_pin_num;
+			ptbl->v12[1] = 2;
+			break;
+		case EC_DID_12VS0_X10:
+			ptbl->v12[0] = dym_tbl[i].hw_pin_num;
+			ptbl->v12[1] = 10;
+			break;
+		case EC_DID_VCOREA:
+		case EC_DID_VCOREB:
+			ptbl->vcore[0] = dym_tbl[i].hw_pin_num;
+			ptbl->vcore[1] = 1;
+			break;
+		case EC_DID_VCOREA_X2:
+		case EC_DID_VCOREB_X2:
+			ptbl->vcore[0] = dym_tbl[i].hw_pin_num;
+			ptbl->vcore[1] = 2;
+			break;
+		case EC_DID_VCOREA_X10:
+		case EC_DID_VCOREB_X10:
+			ptbl->vcore[0] = dym_tbl[i].hw_pin_num;
+			ptbl->vcore[1] = 10;
+			break;
+		case EC_DID_DC:
+			ptbl->vdc[0] = dym_tbl[i].hw_pin_num;
+			ptbl->vdc[1] = 1;
+			break;
+		case EC_DID_DC_X2:
+			ptbl->vdc[0] = dym_tbl[i].hw_pin_num;
+			ptbl->vdc[1] = 2;
+			break;
+		case EC_DID_DC_X10:
+			ptbl->vdc[0] = dym_tbl[i].hw_pin_num;
+			ptbl->vdc[1] = 10;
+			break;
+		case EC_DID_CURRENT:
+			ptbl->ec_current[0] = dym_tbl[i].hw_pin_num;
+			ptbl->ec_current[1] = 1;
+			break;
+		case EC_DID_SMBOEM0:
+			lmsensor_data->ec_smboem0.hw_pin_num = dym_tbl[i].hw_pin_num;
+			break;
+		default:
+			break;
+		}
+	}
+}
+
+static int get_ec_in_vbat_input(struct device *dev, long *val)
+{
+	unsigned int temp = 0;
+	unsigned long voltage = 0;
+	struct ec_hwmon_data *lmsensor_data = dev_get_drvdata(dev);
+
+	struct ec_hw_pin_table *ptbl = &lmsensor_data->pin_tbl;
+	struct adv_hwmon_profile *profile = lmsensor_data->profile;
+	struct adv_ec_platform_data *adv_ec_data = lmsensor_data->adv_ec_data;
+
+	temp = read_ad_value(adv_ec_data, ptbl->vbat[0], ptbl->vbat[1]);
+
+	if (profile->r2 != 0)
+		voltage = temp * (profile->r1 + profile->r2) / profile->r2;
+
+	if (profile->resolution != 0)
+		voltage =  temp * profile->resolution / 1000 / 1000;
+
+	if (profile->offset != 0)
+		voltage += (int)profile->offset * 100;
+
+	lmsensor_data->voltage[0] = 10 * voltage;
+

drivers/hwmon/ahc1ec0-hwmon.c line 302

Quoted 10 lines
drivers/hwmon/ahc1ec0-hwmon.c new file +10 -0
+	*val = lmsensor_data->voltage[0];
+	return 0;
+}
+
+static int get_ec_in_v5_input(struct device *dev, long *val)
+{
+	unsigned int temp;
+	unsigned long voltage = 0;
+	struct ec_hwmon_data *lmsensor_data = dev_get_drvdata(dev);
+

Please no empty lines in variable declarations.

Quoted 96 lines
drivers/hwmon/ahc1ec0-hwmon.c new file +96 -0
+	struct ec_hw_pin_table *ptbl = &lmsensor_data->pin_tbl;
+	struct adv_hwmon_profile *profile = lmsensor_data->profile;
+	struct adv_ec_platform_data *adv_ec_data = lmsensor_data->adv_ec_data;
+
+	temp = read_ad_value(adv_ec_data, ptbl->v5[0], ptbl->v5[1]);
+
+	if (profile->r2 != 0)
+		voltage = temp * (profile->r1 + profile->r2) / profile->r2;
+
+	if (profile->resolution != 0)
+		voltage =  temp * profile->resolution / 1000 / 1000;
+
+	if (profile->offset != 0)
+		voltage += (int)profile->offset * 100;
+
+	lmsensor_data->voltage[1] = 10 * voltage;
+
+	*val = lmsensor_data->voltage[1];
+	return 0;
+}
+
+static int get_ec_in_v12_input(struct device *dev, long *val)
+{
+	int temp;
+	unsigned long voltage = 0;
+	struct ec_hwmon_data *lmsensor_data = dev_get_drvdata(dev);
+
+	struct ec_hw_pin_table *ptbl = &lmsensor_data->pin_tbl;
+	struct adv_hwmon_profile *profile = lmsensor_data->profile;
+	struct adv_ec_platform_data *adv_ec_data = lmsensor_data->adv_ec_data;
+
+	temp = read_ad_value(adv_ec_data, ptbl->v12[0], ptbl->v12[1]);
+	if (temp == -1)
+		temp = read_ad_value(adv_ec_data, ptbl->vdc[0], ptbl->vdc[1]);
+
+	if (profile->r2 != 0)
+		voltage = temp * (profile->r1 + profile->r2) / profile->r2;
+
+	if (profile->resolution != 0)
+		voltage =  temp * profile->resolution / 1000 / 1000;
+
+	if (profile->offset != 0)
+		voltage += profile->offset * 100;
+
+	lmsensor_data->voltage[2] = 10 * voltage;
+
+	*val = lmsensor_data->voltage[2];
+	return 0;
+}
+
+static int get_ec_in_vcore_input(struct device *dev, long *val)
+{
+	int temp;
+	unsigned int voltage = 0;
+	struct ec_hwmon_data *lmsensor_data = dev_get_drvdata(dev);
+
+	struct ec_hw_pin_table *ptbl = &lmsensor_data->pin_tbl;
+	struct adv_hwmon_profile *profile = lmsensor_data->profile;
+	struct adv_ec_platform_data *adv_ec_data = lmsensor_data->adv_ec_data;
+
+	temp = read_ad_value(adv_ec_data, ptbl->vcore[0], ptbl->vcore[1]);
+
+	if (profile->r2 != 0)
+		voltage = temp * (profile->r1 + profile->r2) / profile->r2;
+
+	if (profile->resolution != 0)
+		voltage = temp * profile->resolution / 1000 / 1000;
+
+	if (profile->offset != 0)
+		voltage += profile->offset * 100;
+
+	lmsensor_data->voltage[3] = 10 * voltage;
+
+	*val = lmsensor_data->voltage[3];
+	return 0;
+}
+
+static int get_ec_current1_input(struct device *dev, long *val)
+{
+	int temp;
+	struct ec_hwmon_data *lmsensor_data = dev_get_drvdata(dev);
+
+	struct ec_hw_pin_table *ptbl = &lmsensor_data->pin_tbl;
+	struct adv_hwmon_profile *profile = lmsensor_data->profile;
+	struct adv_ec_platform_data *adv_ec_data = lmsensor_data->adv_ec_data;
+
+	temp = read_ad_value(adv_ec_data, ptbl->ec_current[0], ptbl->ec_current[1]);
+
+	if (profile->r2 != 0)
+		temp = temp * (profile->r1 + profile->r2) / profile->r2;
+
+	if (profile->resolution != 0)
+		temp = temp * profile->resolution / 1000 / 1000;
+
+	if (profile->offset != 0)
+		temp += profile->offset * 100;

drivers/hwmon/ahc1ec0-hwmon.c line 408

Quoted 10 lines
drivers/hwmon/ahc1ec0-hwmon.c new file +10 -0
+
+	lmsensor_data->ec_current[3] = 10 * temp;
+
+	*val = lmsensor_data->ec_current[3];
+	return 0;
+}
+
+static int get_ec_cpu_temp(struct device *dev, long *val)
+{
+	#define EC_ACPI_THERMAL1_REMOTE_TEMP 0x61

Pease move defines to top of file. Also, please use

#define<space>IDENTIFER<tab>value

drivers/hwmon/ahc1ec0-hwmon.c line 414

Quoted 6 lines
drivers/hwmon/ahc1ec0-hwmon.c new file +6 -0
+
+	unsigned char value;
+	struct ec_hwmon_data *lmsensor_data = dev_get_drvdata(dev);
+	struct adv_ec_platform_data *adv_ec_data = lmsensor_data->adv_ec_data;
+
+	read_acpi_value(adv_ec_data, EC_ACPI_THERMAL1_REMOTE_TEMP, &value);

This is not an appropriate function name for a device specific exported function. The function name should have a prefix.

Also, the error code from read_acpi_value needs to be checked and handled.

Quoted 15 lines
drivers/hwmon/ahc1ec0-hwmon.c new file +15 -0
+	*val = 1000 * value;
+	return 0;
+}
+
+static int get_ec_sys_temp(struct device *dev, long *val)
+{
+	#define EC_ACPI_THERMAL1_LOCAL_TEMP 0x60
+
+	unsigned char value;
+	struct ec_hwmon_data *lmsensor_data = dev_get_drvdata(dev);
+	struct adv_ec_platform_data *adv_ec_data = lmsensor_data->adv_ec_data;
+
+	read_acpi_value(adv_ec_data, EC_ACPI_THERMAL1_LOCAL_TEMP, &value);
+	*val = 1000 * value;
+	return 0;

drivers/hwmon/ahc1ec0-hwmon.c line 439

Quoted 10 lines
drivers/hwmon/ahc1ec0-hwmon.c new file +10 -0
+}
+
+
+static int
+ahc1ec0_read_in(struct device *dev, u32 attr, int channel, long *val)
+{
+	struct ec_hwmon_data *lmsensor_data = dev_get_drvdata(dev);
+
+	if (!(lmsensor_data && lmsensor_data->profile))
+		return -EINVAL;

This can not happen. Also, please don't use double negations.

drivers/hwmon/ahc1ec0-hwmon.c line 442

Quoted 3 lines
drivers/hwmon/ahc1ec0-hwmon.c new file +3 -0
+
+	if (attr == hwmon_in_input &&
+		lmsensor_data->profile->hwmon_in_list_cnt > channel) {

This is unnecessary. Again, please drop all those unnecessary checks.

Quoted 40 lines
drivers/hwmon/ahc1ec0-hwmon.c new file +40 -0
+		int index = lmsensor_data->profile->hwmon_in_list[channel];
+		const struct ec_hwmon_attrs *ec_hwmon_attr = &ec_hwmon_in_attr_template[index];
+
+		return ec_hwmon_attr->read(dev, val);
+	}
+
+	return -EOPNOTSUPP;
+}
+
+static int
+ahc1ec0_read_temp(struct device *dev, u32 attr, int channel, long *val)
+{
+	struct ec_hwmon_data *lmsensor_data = dev_get_drvdata(dev);
+
+	if (!(lmsensor_data && lmsensor_data->profile))
+		return -EINVAL;
+
+	switch (attr) {
+	case hwmon_temp_input:
+		if (lmsensor_data->profile->temp_list_cnt > channel) {
+			int index = lmsensor_data->profile->temp_list[channel];
+			const struct ec_hwmon_attrs *devec_hwmon_attr =
+				&ec_temp_attrs_template[index];
+
+			return devec_hwmon_attr->read(dev, val);
+		}
+		return -EOPNOTSUPP;
+	case hwmon_temp_crit:
+		// both CPU temp and System temp are all this value
+		*val = 100000;
+		return 0;
+	default:
+		return -EOPNOTSUPP;
+	}
+}
+
+static int
+ahc1ec0_read_string(struct device *dev,
+					enum hwmon_sensor_types type,
+					u32 attr,

drivers/hwmon/ahc1ec0-hwmon.c line 492

Quoted 10 lines
drivers/hwmon/ahc1ec0-hwmon.c new file +10 -0
+					int channel,
+					const char **str)
+{
+	struct ec_hwmon_data *lmsensor_data = dev_get_drvdata(dev);
+
+	if (!(lmsensor_data && lmsensor_data->profile))
+		return -EINVAL;
+
+	if ((type == hwmon_in && attr == hwmon_in_label) &&
+		(lmsensor_data->profile->hwmon_in_list_cnt > channel)) {

lmsensor_data->profile->hwmon_in_list_cnt > channel is unnecessary, and the () around it as well.

Quoted 15 lines
drivers/hwmon/ahc1ec0-hwmon.c new file +15 -0
+		int index = lmsensor_data->profile->hwmon_in_list[channel];
+		const struct ec_hwmon_attrs *ec_hwmon_attr = &ec_hwmon_in_attr_template[index];
+
+		*str = ec_hwmon_attr->name;
+		return 0;
+	}
+
+	if ((type == hwmon_temp && attr == hwmon_temp_label) &&
+		(lmsensor_data->profile->temp_list_cnt > channel)) {
+		int index = lmsensor_data->profile->temp_list[channel];
+		const struct ec_hwmon_attrs *ec_hwmon_attr = &ec_temp_attrs_template[index];
+
+		*str = ec_hwmon_attr->name;
+		return 0;
+	}

drivers/hwmon/ahc1ec0-hwmon.c line 517

Quoted 10 lines
drivers/hwmon/ahc1ec0-hwmon.c new file +10 -0
+
+	return -EOPNOTSUPP;
+}
+
+static int
+ahc1ec0_read(struct device *dev,
+			enum hwmon_sensor_types type,
+			u32 attr,
+			int channel,
+			long *val)

Too many line splits. Please reduce to minuimum required.

Quoted 11 lines
drivers/hwmon/ahc1ec0-hwmon.c new file +11 -0
+{
+	switch (type) {
+	case hwmon_in:
+		return ahc1ec0_read_in(dev, attr, channel, val);
+	case hwmon_temp:
+		return ahc1ec0_read_temp(dev, attr, channel, val);
+	default:
+		return -EOPNOTSUPP;
+	}
+}
+

drivers/hwmon/ahc1ec0-hwmon.c line 538

Quoted 10 lines
drivers/hwmon/ahc1ec0-hwmon.c new file +10 -0
+static umode_t
+ec_hwmon_in_visible(const void *data, u32 attr, int channel)
+{
+	switch (attr) {
+	case hwmon_in_input:
+	case hwmon_in_label:
+		return 0444;
+	default:
+		return 0;
+	}

The code needs to take channel into account, and return 0 if a voltage sensor is not supported.

drivers/hwmon/ahc1ec0-hwmon.c line 551

Quoted 13 lines
drivers/hwmon/ahc1ec0-hwmon.c new file +13 -0
+}
+
+static umode_t
+ec_temp_in_visible(const void *data, u32 attr, int channel)
+{
+	switch (attr) {
+	case hwmon_temp_input:
+	case hwmon_temp_crit:
+	case hwmon_temp_label:
+		return 0444;
+	default:
+		return 0;
+	}

The code needs to take channel into account, and return 0 if the second temperature sensor is not supported.

drivers/hwmon/ahc1ec0-hwmon.c line 556

Quoted 5 lines
drivers/hwmon/ahc1ec0-hwmon.c new file +5 -0
+}
+
+static umode_t
+ahc1ec0_is_visible(const void *data,
+					enum hwmon_sensor_types type, u32 attr, int channel)

Now this is a really odd (nd unnecessary) continuation line.

Quoted 16 lines
drivers/hwmon/ahc1ec0-hwmon.c new file +16 -0
+{
+	switch (type) {
+	case hwmon_in:
+		return ec_hwmon_in_visible(data, attr, channel);
+	case hwmon_temp:
+		return ec_temp_in_visible(data, attr, channel);
+	default:
+		return 0;
+	}
+}
+
+static const u32 ahc1ec0_in_config[] = {
+	HWMON_I_INPUT | HWMON_I_LABEL,
+	HWMON_I_INPUT | HWMON_I_LABEL,
+	HWMON_I_INPUT | HWMON_I_LABEL,
+	HWMON_I_INPUT | HWMON_I_LABEL,

drivers/hwmon/ahc1ec0-hwmon.c line 582

Quoted 10 lines
drivers/hwmon/ahc1ec0-hwmon.c new file +10 -0
+	0
+};
+
+static const struct hwmon_channel_info ahc1ec0_in = {
+	.type = hwmon_in,
+	.config = ahc1ec0_in_config,
+};
+
+static const u32 ahc1ec0_temp_config[] = {
+	HWMON_T_INPUT | HWMON_T_CRIT | HWMON_T_LABEL,

I don't think this works as intended. it only lists one temperature sensor, but the code clearly suggests that there can be two.

Quoted 34 lines
drivers/hwmon/ahc1ec0-hwmon.c new file +34 -0
+	0
+};
+
+static const struct hwmon_channel_info ahc1ec0_temp = {
+	.type = hwmon_temp,
+	.config = ahc1ec0_temp_config,
+};
+
+static const struct hwmon_channel_info *ahc1ec0_info[] = {
+	&ahc1ec0_in,
+	&ahc1ec0_temp,
+	NULL
+};
+
+static const struct hwmon_ops ahc1ec0_hwmon_ops = {
+	.is_visible = ahc1ec0_is_visible,
+	.read = ahc1ec0_read,
+	.read_string = ahc1ec0_read_string,
+};
+
+static const struct hwmon_chip_info ahc1ec0_chip_info = {
+	.ops = &ahc1ec0_hwmon_ops,
+	.info = ahc1ec0_info,
+};
+
+static int adv_ec_hwmon_probe(struct platform_device *pdev)
+{
+	int ret;
+	u32 profile;
+	struct device *dev = &pdev->dev;
+	struct adv_ec_platform_data *adv_ec_data;
+	struct ec_hwmon_data *lmsensor_data;
+
+	adv_ec_data = dev_get_drvdata(dev->parent);

drivers/hwmon/ahc1ec0-hwmon.c line 626

Quoted 10 lines
drivers/hwmon/ahc1ec0-hwmon.c new file +10 -0
+	if (!adv_ec_data)
+		return -EINVAL;
+
+	ret = device_property_read_u32(dev->parent, "advantech,hwmon-profile", &profile);
+	if (ret < 0) {
+		dev_err(dev, "get hwmon-profile failed! (%d)", ret);
+		return ret;
+	}
+
+	if (!(profile < ARRAY_SIZE(advec_profile))) {

Please no double negations.
if (profile >= ARRAY_SIZE(advec_profile))
is much easier to understand.

Quoted 34 lines
drivers/hwmon/ahc1ec0-hwmon.c new file +34 -0
+		dev_err(dev, "not support hwmon profile(%d)!\n", profile);
+		return -EINVAL;
+	}
+
+	lmsensor_data = devm_kzalloc(dev, sizeof(struct ec_hwmon_data), GFP_KERNEL);
+	if (!lmsensor_data)
+		return -ENOMEM;
+
+	lmsensor_data->adv_ec_data = adv_ec_data;
+	lmsensor_data->dev = dev;
+	dev_set_drvdata(dev, lmsensor_data);
+
+	adv_ec_init_hwmon_profile(profile, lmsensor_data);
+
+	lmsensor_data->hwmon_dev  = devm_hwmon_device_register_with_info(dev,
+			"ahc1ec0.hwmon", lmsensor_data, &ahc1ec0_chip_info, NULL);
+
+	return PTR_ERR_OR_ZERO(lmsensor_data->hwmon_dev);
+}
+
+static struct platform_driver adv_hwmon_drv = {
+	.driver = {
+		.name = "ahc1ec0-hwmon",
+	},
+	.probe = adv_ec_hwmon_probe,
+};
+module_platform_driver(adv_hwmon_drv);
+
+MODULE_LICENSE("Dual BSD/GPL");
+MODULE_ALIAS("platform:ahc1ec0-hwmon");
+MODULE_DESCRIPTION("Advantech Embedded Controller HWMON Driver.");
+MODULE_AUTHOR("Campion Kang <[email protected]>");
+MODULE_AUTHOR("Jianfeng Dai <[email protected]>");
+MODULE_VERSION("1.0");

A message that is not in the archive (sent off-list or not yet received): [email protected]

maintainer
#19 lore raw

Re: [PATCH v6 5/6] hwmon: ahc1ec0-hwmon: Add sub-device hwmon for Advantech embedded controller

Quoted 7 lines · Campion.Kang

Hello Guenter,

Thanks your review and suggestion. I will fix them.
One issue I am not clear, Documentation is missing, which document?
Did you mean this file Documentation\devicetree\bindings\mfd\ahc1ec0.yaml? If yes, it submit in this patch "[PATCH v6 3/6] dt-bindings: mfd: ahc1ec0.yaml: Add Advantech embedded controller - AHC1EC0". Would you kindly give me more information?

Per Documentation/hwmon/submitting-patches.rst:

3. New drivers
--------------
...
* Document the driver in Documentation/hwmon/<driver_name>.rst.

Please read and follow that document. It will save us both a lot of time.

Thanks,
Guenter

Quoted 819 lines

Best regards,
Campion

Quoted 816 lines

-----Original Message-----
From: Guenter Roeck <[email protected]> On Behalf Of Guenter Roeck
Sent: Sunday, January 24, 2021 12:35 AM
To: Campion.Kang <[email protected]>
Cc: Lee Jones <[email protected]>; Rob Herring <[email protected]>; [email protected]; [email protected]; Jean Delvare <[email protected]>; Wim Van Sebroeck <[email protected]>; [email protected]; [email protected]; AceLan Kao <[email protected]>
Subject: Re: [PATCH v6 5/6] hwmon: ahc1ec0-hwmon: Add sub-device hwmon for Advantech embedded controller

Quoted 11 lines · Campion Kang

This is one of sub-device driver for Advantech embedded controller AHC1EC0. This driver provides sysfs ABI for Advantech related applications to monitor the system status.

Changed since V5:
- remove unnecessary header files
- Using [devm_]hwmon_device_register_with_info() to register hwmon
driver based on reviewer's suggestion

checkpatch --strict says:

total: 0 errors, 1 warnings, 10 checks, 683 lines checked

The MAINTAINERS warning is irrelevant, but there are a number of alignment and other style issues. Please fix those.

Quoted 5 lines
---
 drivers/hwmon/Kconfig         |  10 +
 drivers/hwmon/Makefile        |   1 +
 drivers/hwmon/ahc1ec0-hwmon.c | 660

++++++++++++++++++++++++++++++++++

Documentation is missing.

drivers/hwmon/Kconfig config SENSORS_INTEL_M10_BMC_HWMON · line 2139

Quoted 9 lines
 3 files changed, 671 insertions(+)
 create mode 100644 drivers/hwmon/ahc1ec0-hwmon.c
drivers/hwmon/Kconfig index renamed from drivers/hwmon/Kconfig +0 -0

1ecf697d8d99..bfa007026679 100644

drivers/hwmon/Kconfig +0 -0
@@ -2139,6 +2139,16 @@ config SENSORS_INTEL_M10_BMC_HWMON
 	  sensors monitor various telemetry data of different components on

the

drivers/hwmon/Kconfig config SENSORS_INTEL_M10_BMC_HWMON · line 2140

Quoted 1 line
drivers/hwmon/Kconfig +0 -0
 	  card, e.g. board temperature, FPGA core

temperature/voltage/current.

drivers/hwmon/Kconfig config SENSORS_INTEL_M10_BMC_HWMON

Quoted 6 lines
drivers/hwmon/Kconfig +5 -0
+config SENSORS_AHC1EC0_HWMON
+	tristate "Advantech AHC1EC0 Hardware Monitor Function"
+	depends on MFD_AHC1EC0
+	help
+	  This driver provide support for the hardware monitoring

functionality

Quoted 43 lines
drivers/hwmon/Kconfig +5 -0
+	  for Advantech AHC1EC0 embedded controller on the board.
+
+	  This driver provides the sysfs attributes for applications to monitor
+	  the system status, including system temperatures, voltages, current.
+
 if ACPI
 
 comment "ACPI drivers"
drivers/hwmon/Makefile index renamed from drivers/hwmon/Makefile +0 -0

09a86c5e1d29..0c37747e8c4f 100644

drivers/hwmon/Makefile +1 -0
@@ -45,6 +45,7 @@ obj-$(CONFIG_SENSORS_ADT7411)	+= adt7411.o
 obj-$(CONFIG_SENSORS_ADT7462)	+= adt7462.o
 obj-$(CONFIG_SENSORS_ADT7470)	+= adt7470.o
 obj-$(CONFIG_SENSORS_ADT7475)	+= adt7475.o
+obj-$(CONFIG_SENSORS_AHC1EC0_HWMON)	+= ahc1ec0-hwmon.o
 obj-$(CONFIG_SENSORS_AMD_ENERGY) += amd_energy.o
 obj-$(CONFIG_SENSORS_APPLESMC)	+= applesmc.o
 obj-$(CONFIG_SENSORS_ARM_SCMI)	+= scmi-hwmon.o
(quoted diff) +0 -0

b/drivers/hwmon/ahc1ec0-hwmon.c new file mode 100644 index 000000000000..688f07e6a6e0

drivers/hwmon/ahc1ec0-hwmon.c new file +17 -0
@@ -0,0 +1,660 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * HWMON Driver for Advantech Embedded Controller chip AHC1EC0
+ *
+ * Copyright 2020, Advantech IIoT Group
+ *
+ */
+
+#include <linux/device.h>
+#include <linux/errno.h>
+#include <linux/hwmon-sysfs.h>
+#include <linux/hwmon.h>
+#include <linux/mfd/ahc1ec0.h>
+#include <linux/module.h>
+#include <linux/platform_device.h>
+#include <linux/property.h>
+#include <linux/string.h>

drivers/hwmon/ahc1ec0-hwmon.c line 27

Quoted 10 lines
drivers/hwmon/ahc1ec0-hwmon.c new file +10 -0
+#include <linux/types.h>
+
+struct ec_hwmon_attrs {
+	const char		*name;
+	umode_t			mode;
+	int (*read)(struct device *dev, long *val); };
+
+struct adv_hwmon_profile {
+	int offset;
+	unsigned long resolution, resolution_vin, resolution_sys,

resolution_curr, resolution_power;

Quoted 20 lines
drivers/hwmon/ahc1ec0-hwmon.c new file +20 -0
+	unsigned long r1, r1_vin, r1_sys, r1_curr, r1_power;
+	unsigned long r2, r2_vin, r2_sys, r2_curr, r2_power;
+	int hwmon_in_list_cnt;
+	int temp_list_cnt;
+	int *hwmon_in_list;
+	int *temp_list;
+};
+
+struct ec_hwmon_data {
+	struct device *dev;
+	struct device *hwmon_dev;
+	struct adv_ec_platform_data *adv_ec_data;
+	unsigned long temperature[3];
+	unsigned long ec_current[5];
+	unsigned long power[5];
+	unsigned long voltage[7];
+
+	struct ec_hw_pin_table pin_tbl;
+	struct ec_smbuso_em0 ec_smboem0;
+	struct adv_hwmon_profile *profile;

drivers/hwmon/ahc1ec0-hwmon.c line 58

Quoted 10 lines
drivers/hwmon/ahc1ec0-hwmon.c new file +10 -0
+};
+
+static int get_ec_in_vbat_input(struct device *dev, long *val);
+static int get_ec_in_v5_input(struct device *dev, long *val); static
+int get_ec_in_v12_input(struct device *dev, long *val); static int
+get_ec_in_vcore_input(struct device *dev, long *val); static int
+get_ec_current1_input(struct device *dev, long *val); static int
+get_ec_cpu_temp(struct device *dev, long *val); static int
+get_ec_sys_temp(struct device *dev, long *val);
+

Please reorder code to not require those declarations.

Quoted 50 lines
drivers/hwmon/ahc1ec0-hwmon.c new file +50 -0
+const struct ec_hwmon_attrs ec_hwmon_in_attr_template[] = {
+	{"VBAT",	0444, get_ec_in_vbat_input},	// in1
+	{"5VSB",	0444, get_ec_in_v5_input},	// in2
+	{"Vin",		0444, get_ec_in_v12_input},	// in3 (== in8)
+	{"VCORE",	0444, get_ec_in_vcore_input},	// in4
+	{"Vin1",	0444, NULL},	// in5
+	{"Vin2",	0444, NULL},	// in6
+	{"System Voltage", 0444, NULL},	// in7
+	{"Current",	0444, get_ec_current1_input},
+};
+
+const struct ec_hwmon_attrs ec_temp_attrs_template[] = {
+	{"CPU Temp",	0444, get_ec_cpu_temp},
+	{"System Temp",	0444, get_ec_sys_temp},
+};
+
+enum ec_hwmon_in_type {
+	EC_HWMON_IN_VBAT,
+	EC_HWMON_IN_5VSB,
+	EC_HWMON_IN_12V,
+	EC_HWMON_IN_VCORE,
+	EC_HWMON_IN_VIN1,
+	EC_HWMON_IN_VIN2,
+	EC_HWMON_IN_SYS_VOL,
+	EC_HWMON_IN_CURRENT,
+};
+
+enum ec_temp_type {
+	EC_TEMP_CPU,
+	EC_TEMP_SYS,
+};
+
+static int hwmon_in_list_0[] = {
+	EC_HWMON_IN_VBAT,
+	EC_HWMON_IN_5VSB,
+	EC_HWMON_IN_12V,
+	EC_HWMON_IN_VCORE,
+	EC_HWMON_IN_CURRENT,
+};
+
+static int hwmon_in_list_1[] = {
+	EC_HWMON_IN_VBAT,
+	EC_HWMON_IN_5VSB,
+	EC_HWMON_IN_12V,
+	EC_HWMON_IN_VCORE,
+};
+
+static int temp_list_0[] = {
+	EC_TEMP_CPU,
+};

drivers/hwmon/ahc1ec0-hwmon.c

Quoted 10 lines
drivers/hwmon/ahc1ec0-hwmon.c new file +10 -0
+
+static int temp_list_1[] = {
+	EC_TEMP_CPU,
+	EC_TEMP_SYS,
+};
+
+static struct adv_hwmon_profile advec_profile[] = {
+	/*
+	 * TPC-8100TR, TPC-651T-E3AE, TPC-1251T-E3AE, TPC-1551T-E3AE,
+	 * TPC-1751T-E3AE, TPC-1051WP-E3AE, TPC-1551WP-E3AE,

TPC-1581WP-433AE,

drivers/hwmon/ahc1ec0-hwmon.c line 118

Quoted 1 line
drivers/hwmon/ahc1ec0-hwmon.c new file +1 -0
+	 * TPC-1782H-433AE, UNO-1483G-434AE, UNO-2483G-434AE,

UNO-3483G-374AE,

drivers/hwmon/ahc1ec0-hwmon.c line 119

Quoted 1 line
drivers/hwmon/ahc1ec0-hwmon.c new file +1 -0
+	 * UNO-2473G, UNO-2484G-6???AE, UNO-2484G-7???AE,

UNO-3283G-674AE,

drivers/hwmon/ahc1ec0-hwmon.c line 121

Quoted 1 line
drivers/hwmon/ahc1ec0-hwmon.c new file +1 -0
+	 * UNO-3285G-674AE

What is all this ? Please add a brief explanation (affected hardware ? chips ?)

Quoted 4 lines
drivers/hwmon/ahc1ec0-hwmon.c new file +4 -0
+	 * [0] AHC1EC0_HWMON_PRO_TEMPLATE
+	 */
+	{
+		.resolution = 2929,

drivers/hwmon/ahc1ec0-hwmon.c

Quoted 10 lines
drivers/hwmon/ahc1ec0-hwmon.c new file +10 -0
+		.r1 = 1912,
+		.r2 = 1000,
+		.offset = 0,
+		.hwmon_in_list_cnt = ARRAY_SIZE(hwmon_in_list_0),
+		.temp_list_cnt = ARRAY_SIZE(temp_list_0),
+		.hwmon_in_list = hwmon_in_list_0,
+		.temp_list = temp_list_0,
+	},
+	/*
+	 * TPC-B500-6??AE, TPC-5???T-6??AE, TPC-5???W-6??AE,

TPC-B200-???AE,

Quoted 36 lines
drivers/hwmon/ahc1ec0-hwmon.c new file +36 -0
+	 * TPC-2???T-???AE, TPC-2???W-???AE
+	 * [1] AHC1EC0_HWMON_PRO_TPC5XXX
+	 */
+	{
+		.resolution = 2929,
+		.r1 = 1912,
+		.r2 = 1000,
+		.offset = 0,
+		.hwmon_in_list_cnt = ARRAY_SIZE(hwmon_in_list_1),
+		.temp_list_cnt = ARRAY_SIZE(temp_list_0),
+		.hwmon_in_list = hwmon_in_list_1,
+		.temp_list = temp_list_0,
+	},
+	/* PR/VR4
+	 * [2] AHC1EC0_HWMON_PRO_PRVR4
+	 */
+	{
+		.resolution = 2929,
+		.r1 = 1912,
+		.r2 = 1000,
+		.offset = 0,
+		.hwmon_in_list_cnt = ARRAY_SIZE(hwmon_in_list_1),
+		.temp_list_cnt = ARRAY_SIZE(temp_list_1),
+		.hwmon_in_list = hwmon_in_list_1,
+		.temp_list = temp_list_1,
+	},
+	/* UNO-2271G-E22AE/E23AE/E022AE/E023AE,UNO-420
+	 * [3] AHC1EC0_HWMON_PRO_UNO2271G
+	 */
+	{
+		.resolution = 2929,
+		.r1 = 1912,
+		.r2 = 1000,
+		.offset = 0,
+		.hwmon_in_list_cnt = ARRAY_SIZE(hwmon_in_list_1),
+		.temp_list_cnt = ARRAY_SIZE(temp_list_0),

drivers/hwmon/ahc1ec0-hwmon.c

Quoted 10 lines
drivers/hwmon/ahc1ec0-hwmon.c new file +10 -0
+		.hwmon_in_list = hwmon_in_list_1,
+		.temp_list = temp_list_0,
+	},
+};
+
+static void adv_ec_init_hwmon_profile(u32 profile, struct
+ec_hwmon_data *lmsensor_data) {
+	int i;
+	struct ec_hw_pin_table *ptbl = &lmsensor_data->pin_tbl;
+	struct adv_ec_platform_data *adv_ec_data =

lmsensor_data->adv_ec_data;

drivers/hwmon/ahc1ec0-hwmon.c line 185

Quoted 4 lines
drivers/hwmon/ahc1ec0-hwmon.c new file +4 -0
+	struct ec_dynamic_table *dym_tbl = adv_ec_data->dym_tbl;
+
+	if (profile >= ARRAY_SIZE(advec_profile))
+		return;

This can not happen. Please no unnecessary error checks.

Quoted 67 lines
drivers/hwmon/ahc1ec0-hwmon.c new file +67 -0
+
+	lmsensor_data->profile = &advec_profile[profile];
+
+	for (i = 0; i < EC_MAX_TBL_NUM ; i++) {
+		switch (dym_tbl[i].device_id) {
+		case EC_DID_CMOSBAT:
+			ptbl->vbat[0] = dym_tbl[i].hw_pin_num;
+			ptbl->vbat[1] = 1;
+			break;
+		case EC_DID_CMOSBAT_X2:
+			ptbl->vbat[0] = dym_tbl[i].hw_pin_num;
+			ptbl->vbat[1] = 2;
+			break;
+		case EC_DID_CMOSBAT_X10:
+			ptbl->vbat[0] = dym_tbl[i].hw_pin_num;
+			ptbl->vbat[1] = 10;
+			break;
+		case EC_DID_5VS0:
+		case EC_DID_5VS5:
+			ptbl->v5[0] = dym_tbl[i].hw_pin_num;
+			ptbl->v5[1] = 1;
+			break;
+		case EC_DID_5VS0_X2:
+		case EC_DID_5VS5_X2:
+			ptbl->v5[0] = dym_tbl[i].hw_pin_num;
+			ptbl->v5[1] = 2;
+			break;
+		case EC_DID_5VS0_X10:
+		case EC_DID_5VS5_X10:
+			ptbl->v5[0] = dym_tbl[i].hw_pin_num;
+			ptbl->v5[1] = 10;
+			break;
+		case EC_DID_12VS0:
+			ptbl->v12[0] = dym_tbl[i].hw_pin_num;
+			ptbl->v12[1] = 1;
+			break;
+		case EC_DID_12VS0_X2:
+			ptbl->v12[0] = dym_tbl[i].hw_pin_num;
+			ptbl->v12[1] = 2;
+			break;
+		case EC_DID_12VS0_X10:
+			ptbl->v12[0] = dym_tbl[i].hw_pin_num;
+			ptbl->v12[1] = 10;
+			break;
+		case EC_DID_VCOREA:
+		case EC_DID_VCOREB:
+			ptbl->vcore[0] = dym_tbl[i].hw_pin_num;
+			ptbl->vcore[1] = 1;
+			break;
+		case EC_DID_VCOREA_X2:
+		case EC_DID_VCOREB_X2:
+			ptbl->vcore[0] = dym_tbl[i].hw_pin_num;
+			ptbl->vcore[1] = 2;
+			break;
+		case EC_DID_VCOREA_X10:
+		case EC_DID_VCOREB_X10:
+			ptbl->vcore[0] = dym_tbl[i].hw_pin_num;
+			ptbl->vcore[1] = 10;
+			break;
+		case EC_DID_DC:
+			ptbl->vdc[0] = dym_tbl[i].hw_pin_num;
+			ptbl->vdc[1] = 1;
+			break;
+		case EC_DID_DC_X2:
+			ptbl->vdc[0] = dym_tbl[i].hw_pin_num;
+			ptbl->vdc[1] = 2;
+			break;

drivers/hwmon/ahc1ec0-hwmon.c

Quoted 10 lines
drivers/hwmon/ahc1ec0-hwmon.c new file +10 -0
+		case EC_DID_DC_X10:
+			ptbl->vdc[0] = dym_tbl[i].hw_pin_num;
+			ptbl->vdc[1] = 10;
+			break;
+		case EC_DID_CURRENT:
+			ptbl->ec_current[0] = dym_tbl[i].hw_pin_num;
+			ptbl->ec_current[1] = 1;
+			break;
+		case EC_DID_SMBOEM0:
+			lmsensor_data->ec_smboem0.hw_pin_num =

dym_tbl[i].hw_pin_num;

Quoted 29 lines
drivers/hwmon/ahc1ec0-hwmon.c new file +29 -0
+			break;
+		default:
+			break;
+		}
+	}
+}
+
+static int get_ec_in_vbat_input(struct device *dev, long *val) {
+	unsigned int temp = 0;
+	unsigned long voltage = 0;
+	struct ec_hwmon_data *lmsensor_data = dev_get_drvdata(dev);
+
+	struct ec_hw_pin_table *ptbl = &lmsensor_data->pin_tbl;
+	struct adv_hwmon_profile *profile = lmsensor_data->profile;
+	struct adv_ec_platform_data *adv_ec_data =
+lmsensor_data->adv_ec_data;
+
+	temp = read_ad_value(adv_ec_data, ptbl->vbat[0], ptbl->vbat[1]);
+
+	if (profile->r2 != 0)
+		voltage = temp * (profile->r1 + profile->r2) / profile->r2;
+
+	if (profile->resolution != 0)
+		voltage =  temp * profile->resolution / 1000 / 1000;
+
+	if (profile->offset != 0)
+		voltage += (int)profile->offset * 100;
+
+	lmsensor_data->voltage[0] = 10 * voltage;

drivers/hwmon/ahc1ec0-hwmon.c line 302

Quoted 10 lines
drivers/hwmon/ahc1ec0-hwmon.c new file +10 -0
+
+	*val = lmsensor_data->voltage[0];
+	return 0;
+}
+
+static int get_ec_in_v5_input(struct device *dev, long *val) {
+	unsigned int temp;
+	unsigned long voltage = 0;
+	struct ec_hwmon_data *lmsensor_data = dev_get_drvdata(dev);
+

Please no empty lines in variable declarations.

Quoted 97 lines
drivers/hwmon/ahc1ec0-hwmon.c new file +97 -0
+	struct ec_hw_pin_table *ptbl = &lmsensor_data->pin_tbl;
+	struct adv_hwmon_profile *profile = lmsensor_data->profile;
+	struct adv_ec_platform_data *adv_ec_data =
+lmsensor_data->adv_ec_data;
+
+	temp = read_ad_value(adv_ec_data, ptbl->v5[0], ptbl->v5[1]);
+
+	if (profile->r2 != 0)
+		voltage = temp * (profile->r1 + profile->r2) / profile->r2;
+
+	if (profile->resolution != 0)
+		voltage =  temp * profile->resolution / 1000 / 1000;
+
+	if (profile->offset != 0)
+		voltage += (int)profile->offset * 100;
+
+	lmsensor_data->voltage[1] = 10 * voltage;
+
+	*val = lmsensor_data->voltage[1];
+	return 0;
+}
+
+static int get_ec_in_v12_input(struct device *dev, long *val) {
+	int temp;
+	unsigned long voltage = 0;
+	struct ec_hwmon_data *lmsensor_data = dev_get_drvdata(dev);
+
+	struct ec_hw_pin_table *ptbl = &lmsensor_data->pin_tbl;
+	struct adv_hwmon_profile *profile = lmsensor_data->profile;
+	struct adv_ec_platform_data *adv_ec_data =
+lmsensor_data->adv_ec_data;
+
+	temp = read_ad_value(adv_ec_data, ptbl->v12[0], ptbl->v12[1]);
+	if (temp == -1)
+		temp = read_ad_value(adv_ec_data, ptbl->vdc[0], ptbl->vdc[1]);
+
+	if (profile->r2 != 0)
+		voltage = temp * (profile->r1 + profile->r2) / profile->r2;
+
+	if (profile->resolution != 0)
+		voltage =  temp * profile->resolution / 1000 / 1000;
+
+	if (profile->offset != 0)
+		voltage += profile->offset * 100;
+
+	lmsensor_data->voltage[2] = 10 * voltage;
+
+	*val = lmsensor_data->voltage[2];
+	return 0;
+}
+
+static int get_ec_in_vcore_input(struct device *dev, long *val) {
+	int temp;
+	unsigned int voltage = 0;
+	struct ec_hwmon_data *lmsensor_data = dev_get_drvdata(dev);
+
+	struct ec_hw_pin_table *ptbl = &lmsensor_data->pin_tbl;
+	struct adv_hwmon_profile *profile = lmsensor_data->profile;
+	struct adv_ec_platform_data *adv_ec_data =
+lmsensor_data->adv_ec_data;
+
+	temp = read_ad_value(adv_ec_data, ptbl->vcore[0], ptbl->vcore[1]);
+
+	if (profile->r2 != 0)
+		voltage = temp * (profile->r1 + profile->r2) / profile->r2;
+
+	if (profile->resolution != 0)
+		voltage = temp * profile->resolution / 1000 / 1000;
+
+	if (profile->offset != 0)
+		voltage += profile->offset * 100;
+
+	lmsensor_data->voltage[3] = 10 * voltage;
+
+	*val = lmsensor_data->voltage[3];
+	return 0;
+}
+
+static int get_ec_current1_input(struct device *dev, long *val) {
+	int temp;
+	struct ec_hwmon_data *lmsensor_data = dev_get_drvdata(dev);
+
+	struct ec_hw_pin_table *ptbl = &lmsensor_data->pin_tbl;
+	struct adv_hwmon_profile *profile = lmsensor_data->profile;
+	struct adv_ec_platform_data *adv_ec_data =
+lmsensor_data->adv_ec_data;
+
+	temp = read_ad_value(adv_ec_data, ptbl->ec_current[0],
+ptbl->ec_current[1]);
+
+	if (profile->r2 != 0)
+		temp = temp * (profile->r1 + profile->r2) / profile->r2;
+
+	if (profile->resolution != 0)
+		temp = temp * profile->resolution / 1000 / 1000;
+
+	if (profile->offset != 0)

drivers/hwmon/ahc1ec0-hwmon.c line 408

Quoted 10 lines
drivers/hwmon/ahc1ec0-hwmon.c new file +10 -0
+		temp += profile->offset * 100;
+
+	lmsensor_data->ec_current[3] = 10 * temp;
+
+	*val = lmsensor_data->ec_current[3];
+	return 0;
+}
+
+static int get_ec_cpu_temp(struct device *dev, long *val) {
+	#define EC_ACPI_THERMAL1_REMOTE_TEMP 0x61

Pease move defines to top of file. Also, please use

#define<space>IDENTIFER<tab>value

drivers/hwmon/ahc1ec0-hwmon.c

Quoted 7 lines
drivers/hwmon/ahc1ec0-hwmon.c new file +7 -0
+
+	unsigned char value;
+	struct ec_hwmon_data *lmsensor_data = dev_get_drvdata(dev);
+	struct adv_ec_platform_data *adv_ec_data =
+lmsensor_data->adv_ec_data;
+
+	read_acpi_value(adv_ec_data, EC_ACPI_THERMAL1_REMOTE_TEMP,

&value);

This is not an appropriate function name for a device specific exported function. The function name should have a prefix.

Also, the error code from read_acpi_value needs to be checked and handled.

drivers/hwmon/ahc1ec0-hwmon.c

Quoted 13 lines
drivers/hwmon/ahc1ec0-hwmon.c new file +13 -0
+	*val = 1000 * value;
+	return 0;
+}
+
+static int get_ec_sys_temp(struct device *dev, long *val) {
+	#define EC_ACPI_THERMAL1_LOCAL_TEMP 0x60
+
+	unsigned char value;
+	struct ec_hwmon_data *lmsensor_data = dev_get_drvdata(dev);
+	struct adv_ec_platform_data *adv_ec_data =
+lmsensor_data->adv_ec_data;
+
+	read_acpi_value(adv_ec_data, EC_ACPI_THERMAL1_LOCAL_TEMP,

&value);

drivers/hwmon/ahc1ec0-hwmon.c line 439

Quoted 12 lines
drivers/hwmon/ahc1ec0-hwmon.c new file +12 -0
+	*val = 1000 * value;
+	return 0;
+}
+
+
+static int
+ahc1ec0_read_in(struct device *dev, u32 attr, int channel, long *val)
+{
+	struct ec_hwmon_data *lmsensor_data = dev_get_drvdata(dev);
+
+	if (!(lmsensor_data && lmsensor_data->profile))
+		return -EINVAL;

This can not happen. Also, please don't use double negations.

drivers/hwmon/ahc1ec0-hwmon.c line 442

Quoted 3 lines
drivers/hwmon/ahc1ec0-hwmon.c new file +3 -0
+
+	if (attr == hwmon_in_input &&
+		lmsensor_data->profile->hwmon_in_list_cnt > channel) {

This is unnecessary. Again, please drop all those unnecessary checks.

Quoted 41 lines
drivers/hwmon/ahc1ec0-hwmon.c new file +41 -0
+		int index = lmsensor_data->profile->hwmon_in_list[channel];
+		const struct ec_hwmon_attrs *ec_hwmon_attr =
+&ec_hwmon_in_attr_template[index];
+
+		return ec_hwmon_attr->read(dev, val);
+	}
+
+	return -EOPNOTSUPP;
+}
+
+static int
+ahc1ec0_read_temp(struct device *dev, u32 attr, int channel, long
+*val) {
+	struct ec_hwmon_data *lmsensor_data = dev_get_drvdata(dev);
+
+	if (!(lmsensor_data && lmsensor_data->profile))
+		return -EINVAL;
+
+	switch (attr) {
+	case hwmon_temp_input:
+		if (lmsensor_data->profile->temp_list_cnt > channel) {
+			int index = lmsensor_data->profile->temp_list[channel];
+			const struct ec_hwmon_attrs *devec_hwmon_attr =
+				&ec_temp_attrs_template[index];
+
+			return devec_hwmon_attr->read(dev, val);
+		}
+		return -EOPNOTSUPP;
+	case hwmon_temp_crit:
+		// both CPU temp and System temp are all this value
+		*val = 100000;
+		return 0;
+	default:
+		return -EOPNOTSUPP;
+	}
+}
+
+static int
+ahc1ec0_read_string(struct device *dev,
+					enum hwmon_sensor_types type,
+					u32 attr,

drivers/hwmon/ahc1ec0-hwmon.c line 492

Quoted 10 lines
drivers/hwmon/ahc1ec0-hwmon.c new file +10 -0
+					int channel,
+					const char **str)
+{
+	struct ec_hwmon_data *lmsensor_data = dev_get_drvdata(dev);
+
+	if (!(lmsensor_data && lmsensor_data->profile))
+		return -EINVAL;
+
+	if ((type == hwmon_in && attr == hwmon_in_label) &&
+		(lmsensor_data->profile->hwmon_in_list_cnt > channel)) {

lmsensor_data->profile->hwmon_in_list_cnt > channel is unnecessary, and the () around it as well.

Quoted 17 lines
drivers/hwmon/ahc1ec0-hwmon.c new file +17 -0
+		int index = lmsensor_data->profile->hwmon_in_list[channel];
+		const struct ec_hwmon_attrs *ec_hwmon_attr =
+&ec_hwmon_in_attr_template[index];
+
+		*str = ec_hwmon_attr->name;
+		return 0;
+	}
+
+	if ((type == hwmon_temp && attr == hwmon_temp_label) &&
+		(lmsensor_data->profile->temp_list_cnt > channel)) {
+		int index = lmsensor_data->profile->temp_list[channel];
+		const struct ec_hwmon_attrs *ec_hwmon_attr =
+&ec_temp_attrs_template[index];
+
+		*str = ec_hwmon_attr->name;
+		return 0;
+	}

drivers/hwmon/ahc1ec0-hwmon.c line 517

Quoted 10 lines
drivers/hwmon/ahc1ec0-hwmon.c new file +10 -0
+
+	return -EOPNOTSUPP;
+}
+
+static int
+ahc1ec0_read(struct device *dev,
+			enum hwmon_sensor_types type,
+			u32 attr,
+			int channel,
+			long *val)

Too many line splits. Please reduce to minuimum required.

Quoted 10 lines
drivers/hwmon/ahc1ec0-hwmon.c new file +10 -0
+{
+	switch (type) {
+	case hwmon_in:
+		return ahc1ec0_read_in(dev, attr, channel, val);
+	case hwmon_temp:
+		return ahc1ec0_read_temp(dev, attr, channel, val);
+	default:
+		return -EOPNOTSUPP;
+	}
+}

drivers/hwmon/ahc1ec0-hwmon.c line 538

Quoted 10 lines
drivers/hwmon/ahc1ec0-hwmon.c new file +10 -0
+
+static umode_t
+ec_hwmon_in_visible(const void *data, u32 attr, int channel) {
+	switch (attr) {
+	case hwmon_in_input:
+	case hwmon_in_label:
+		return 0444;
+	default:
+		return 0;
+	}

The code needs to take channel into account, and return 0 if a voltage sensor is not supported.

drivers/hwmon/ahc1ec0-hwmon.c line 551

Quoted 12 lines
drivers/hwmon/ahc1ec0-hwmon.c new file +12 -0
+}
+
+static umode_t
+ec_temp_in_visible(const void *data, u32 attr, int channel) {
+	switch (attr) {
+	case hwmon_temp_input:
+	case hwmon_temp_crit:
+	case hwmon_temp_label:
+		return 0444;
+	default:
+		return 0;
+	}

The code needs to take channel into account, and return 0 if the second temperature sensor is not supported.

drivers/hwmon/ahc1ec0-hwmon.c

Quoted 5 lines
drivers/hwmon/ahc1ec0-hwmon.c new file +5 -0
+}
+
+static umode_t
+ahc1ec0_is_visible(const void *data,
+					enum hwmon_sensor_types type, u32 attr, int

channel)

Now this is a really odd (nd unnecessary) continuation line.

Quoted 16 lines
drivers/hwmon/ahc1ec0-hwmon.c new file +16 -0
+{
+	switch (type) {
+	case hwmon_in:
+		return ec_hwmon_in_visible(data, attr, channel);
+	case hwmon_temp:
+		return ec_temp_in_visible(data, attr, channel);
+	default:
+		return 0;
+	}
+}
+
+static const u32 ahc1ec0_in_config[] = {
+	HWMON_I_INPUT | HWMON_I_LABEL,
+	HWMON_I_INPUT | HWMON_I_LABEL,
+	HWMON_I_INPUT | HWMON_I_LABEL,
+	HWMON_I_INPUT | HWMON_I_LABEL,

drivers/hwmon/ahc1ec0-hwmon.c line 582

Quoted 10 lines
drivers/hwmon/ahc1ec0-hwmon.c new file +10 -0
+	0
+};
+
+static const struct hwmon_channel_info ahc1ec0_in = {
+	.type = hwmon_in,
+	.config = ahc1ec0_in_config,
+};
+
+static const u32 ahc1ec0_temp_config[] = {
+	HWMON_T_INPUT | HWMON_T_CRIT | HWMON_T_LABEL,

I don't think this works as intended. it only lists one temperature sensor, but the code clearly suggests that there can be two.

Quoted 27 lines
drivers/hwmon/ahc1ec0-hwmon.c new file +27 -0
+	0
+};
+
+static const struct hwmon_channel_info ahc1ec0_temp = {
+	.type = hwmon_temp,
+	.config = ahc1ec0_temp_config,
+};
+
+static const struct hwmon_channel_info *ahc1ec0_info[] = {
+	&ahc1ec0_in,
+	&ahc1ec0_temp,
+	NULL
+};
+
+static const struct hwmon_ops ahc1ec0_hwmon_ops = {
+	.is_visible = ahc1ec0_is_visible,
+	.read = ahc1ec0_read,
+	.read_string = ahc1ec0_read_string,
+};
+
+static const struct hwmon_chip_info ahc1ec0_chip_info = {
+	.ops = &ahc1ec0_hwmon_ops,
+	.info = ahc1ec0_info,
+};
+
+static int adv_ec_hwmon_probe(struct platform_device *pdev) {
+	int ret;

drivers/hwmon/ahc1ec0-hwmon.c

Quoted 10 lines
drivers/hwmon/ahc1ec0-hwmon.c new file +10 -0
+	u32 profile;
+	struct device *dev = &pdev->dev;
+	struct adv_ec_platform_data *adv_ec_data;
+	struct ec_hwmon_data *lmsensor_data;
+
+	adv_ec_data = dev_get_drvdata(dev->parent);
+	if (!adv_ec_data)
+		return -EINVAL;
+
+	ret = device_property_read_u32(dev->parent,

"advantech,hwmon-profile", &profile);

drivers/hwmon/ahc1ec0-hwmon.c line 626

Quoted 6 lines
drivers/hwmon/ahc1ec0-hwmon.c new file +6 -0
+	if (ret < 0) {
+		dev_err(dev, "get hwmon-profile failed! (%d)", ret);
+		return ret;
+	}
+
+	if (!(profile < ARRAY_SIZE(advec_profile))) {

Please no double negations.
if (profile >= ARRAY_SIZE(advec_profile)) is much easier to understand.

drivers/hwmon/ahc1ec0-hwmon.c

Quoted 5 lines
drivers/hwmon/ahc1ec0-hwmon.c new file +5 -0
+		dev_err(dev, "not support hwmon profile(%d)!\n", profile);
+		return -EINVAL;
+	}
+
+	lmsensor_data = devm_kzalloc(dev, sizeof(struct ec_hwmon_data),

GFP_KERNEL);

drivers/hwmon/ahc1ec0-hwmon.c

Quoted 10 lines
drivers/hwmon/ahc1ec0-hwmon.c new file +10 -0
+	if (!lmsensor_data)
+		return -ENOMEM;
+
+	lmsensor_data->adv_ec_data = adv_ec_data;
+	lmsensor_data->dev = dev;
+	dev_set_drvdata(dev, lmsensor_data);
+
+	adv_ec_init_hwmon_profile(profile, lmsensor_data);
+
+	lmsensor_data->hwmon_dev  =

devm_hwmon_device_register_with_info(dev,

drivers/hwmon/ahc1ec0-hwmon.c line 642

Quoted 1 line
drivers/hwmon/ahc1ec0-hwmon.c new file +1 -0
+			"ahc1ec0.hwmon", lmsensor_data, &ahc1ec0_chip_info,

NULL);

Quoted 5 lines
drivers/hwmon/ahc1ec0-hwmon.c new file +5 -0
+
+	return PTR_ERR_OR_ZERO(lmsensor_data->hwmon_dev);
+}
+
+static struct platform_driver adv_hwmon_drv = {

drivers/hwmon/ahc1ec0-hwmon.c

Quoted 10 lines
drivers/hwmon/ahc1ec0-hwmon.c new file +10 -0
+	.driver = {
+		.name = "ahc1ec0-hwmon",
+	},
+	.probe = adv_ec_hwmon_probe,
+};
+module_platform_driver(adv_hwmon_drv);
+
+MODULE_LICENSE("Dual BSD/GPL");
+MODULE_ALIAS("platform:ahc1ec0-hwmon");
+MODULE_DESCRIPTION("Advantech Embedded Controller HWMON

Driver.");

Quoted 3 lines
drivers/hwmon/ahc1ec0-hwmon.c new file +3 -0
+MODULE_AUTHOR("Campion Kang <[email protected]>");
+MODULE_AUTHOR("Jianfeng Dai <[email protected]>");
+MODULE_VERSION("1.0");
#20 lore raw

PATCH v6 6/6 watchdog: ahc1ec0-wdt: Add sub-device watchdog for Advantech embedded controller

In reply to Campion Kang

This is one of sub-device driver for Advantech embedded controller AHC1EC0. This driver provide watchdog functionality for Advantech related applications to restart the system.

Changed since V5:
- remove unnecessary header files
- bug fixed: reboot halt if watchdog enabled
- Kconfig: add "AHC1EC0" string to clearly define the EC name

---
 drivers/watchdog/Kconfig       |  11 ++
 drivers/watchdog/Makefile      |   1 +
 drivers/watchdog/ahc1ec0-wdt.c | 261 +++++++++++++++++++++++++++++++++
 3 files changed, 273 insertions(+)
 create mode 100644 drivers/watchdog/ahc1ec0-wdt.c
drivers/watchdog/Kconfig +11 -0
index 7ff941e71b79..1a27836883ac 100644
@@ -1636,6 +1636,17 @@ config NIC7018_WDT
 	  To compile this driver as a module, choose M here: the module will be
 	  called nic7018_wdt.
 
+config AHC1EC0_WDT
+	tristate "Advantech AHC1EC0 Watchdog Function"
+	depends on MFD_AHC1EC0
+	help
+	  This is sub-device for Advantech AHC1EC0 embedded controller.
+
+	  This driver provide watchdog functionality for Advantech related
+	  applications to restart the system.
+	  To compile thie driver as a module, choose M here: the module will be
+	  called ahc1ec0-wdt.
+
 # M68K Architecture
 
 config M54xx_WATCHDOG
drivers/watchdog/Makefile +1 -0
index 5c74ee19d441..7190811b1e50 100644
@@ -145,6 +145,7 @@ obj-$(CONFIG_INTEL_MID_WATCHDOG) += intel-mid_wdt.o
 obj-$(CONFIG_INTEL_MEI_WDT) += mei_wdt.o
 obj-$(CONFIG_NI903X_WDT) += ni903x_wdt.o
 obj-$(CONFIG_NIC7018_WDT) += nic7018_wdt.o
+obj-$(CONFIG_AHC1EC0_WDT) += ahc1ec0-wdt.o
 obj-$(CONFIG_MLX_WDT) += mlx_wdt.o
 
 # M68K Architecture
drivers/watchdog/ahc1ec0-wdt.c new file +261 -0

261 lines not on this page · raw message

Signature · 1 line
2.17.1
Tested-by Chia-Lin Kao (AceLan) on v6 6/6
#21 lore raw

Re: [PATCH v6 6/6] watchdog: ahc1ec0-wdt: Add sub-device watchdog for Advantech embedded controller

Campion Kang <[email protected]> 於 2021年1月18日 週一 下午8:39寫道:

Quoted 320 lines

This is one of sub-device driver for Advantech embedded controller AHC1EC0. This driver provide watchdog functionality for Advantech related applications to restart the system.

Changed since V5:
- remove unnecessary header files
- bug fixed: reboot halt if watchdog enabled
- Kconfig: add "AHC1EC0" string to clearly define the EC name

---
 drivers/watchdog/Kconfig       |  11 ++
 drivers/watchdog/Makefile      |   1 +
 drivers/watchdog/ahc1ec0-wdt.c | 261 +++++++++++++++++++++++++++++++++
 3 files changed, 273 insertions(+)
 create mode 100644 drivers/watchdog/ahc1ec0-wdt.c
drivers/watchdog/Kconfig +11 -0
index 7ff941e71b79..1a27836883ac 100644
@@ -1636,6 +1636,17 @@ config NIC7018_WDT
          To compile this driver as a module, choose M here: the module will be
          called nic7018_wdt.
 
+config AHC1EC0_WDT
+       tristate "Advantech AHC1EC0 Watchdog Function"
+       depends on MFD_AHC1EC0
+       help
+         This is sub-device for Advantech AHC1EC0 embedded controller.
+
+         This driver provide watchdog functionality for Advantech related
+         applications to restart the system.
+         To compile thie driver as a module, choose M here: the module will be
+         called ahc1ec0-wdt.
+
 # M68K Architecture
 
 config M54xx_WATCHDOG
drivers/watchdog/Makefile +1 -0
index 5c74ee19d441..7190811b1e50 100644
@@ -145,6 +145,7 @@ obj-$(CONFIG_INTEL_MID_WATCHDOG) += intel-mid_wdt.o
 obj-$(CONFIG_INTEL_MEI_WDT) += mei_wdt.o
 obj-$(CONFIG_NI903X_WDT) += ni903x_wdt.o
 obj-$(CONFIG_NIC7018_WDT) += nic7018_wdt.o
+obj-$(CONFIG_AHC1EC0_WDT) += ahc1ec0-wdt.o
 obj-$(CONFIG_MLX_WDT) += mlx_wdt.o
 
 # M68K Architecture
drivers/watchdog/ahc1ec0-wdt.c new file +261 -1
new file mode 100644
index 000000000000..4497b6106b24
@@ -0,0 +1,261 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Watchdog Driver for Advantech Embedded Controller chip AHC1EC0
+ *
+ * Copyright 2020, Advantech IIoT Group
+ *
+ */
+
+#include <linux/errno.h>
+#include <linux/kernel.h>
+#include <linux/mfd/ahc1ec0.h>
+#include <linux/module.h>
+#include <linux/notifier.h>
+#include <linux/platform_device.h>
+#include <linux/reboot.h>
+#include <linux/types.h>
+#include <linux/watchdog.h>
+
+#define DRV_NAME      "ahc1ec0-wdt"
+
+struct ec_wdt_data {
+       struct watchdog_device wdtdev;
+       struct adv_ec_platform_data *adv_ec_data;
+       struct notifier_block reboot_nb;
+       struct mutex lock_ioctl;
+       int is_enable;
+       int current_timeout;
+};
+
+#define EC_WDT_MIN_TIMEOUT 1   /* The watchdog devices minimum timeout value (in seconds). */
+#define EC_WDT_MAX_TIMEOUT 600  /* The watchdog devices maximum timeout value (in seconds) */
+#define EC_WDT_DEFAULT_TIMEOUT 45
+
+static int set_delay(struct adv_ec_platform_data *adv_ec_data, unsigned short delay_timeout_in_ms)
+{
+       if (write_hw_ram(adv_ec_data, EC_RESET_DELAY_TIME_L, delay_timeout_in_ms & 0x00FF)) {
+               pr_err("Failed to set Watchdog Retset Time Low byte.");
+               return -EINVAL;
+       }
+
+       if (write_hw_ram(adv_ec_data, EC_RESET_DELAY_TIME_H, (delay_timeout_in_ms & 0xFF00) >> 8)) {
+               pr_err("Failed to set Watchdog Retset Time Hight byte.");
+               return -EINVAL;
+       }
+
+       return 0;
+}
+
+static int advwdt_set_heartbeat(unsigned long t)
+{
+       if (t < 1 || t > 6553) {
+               pr_err("%s: the input timeout is out of range.",  __func__);
+               pr_err("Please choose valid data between 1 ~ 6553.");
+               return -EINVAL;
+       }
+
+       return (t * 10);
+}
+
+/* Notifier for system down */
+static int advwdt_notify_sys(struct notifier_block *nb, unsigned long code, void *data)
+{
+       if (code == SYS_DOWN || code == SYS_HALT) {
+               struct ec_wdt_data *ec_wdt_data;
+
+               ec_wdt_data = container_of(nb, struct ec_wdt_data, reboot_nb);
+               if (!ec_wdt_data)
+                       return NOTIFY_BAD;
+
+               /* Turn the WDT off */
+               if (write_hwram_command(ec_wdt_data->adv_ec_data, EC_WDT_STOP)) {
+                       pr_err("Failed to set Watchdog stop.");
+                       return -EINVAL;
+               }
+               ec_wdt_data->is_enable = 0;
+               pr_info("%s: notify sys shutdown", __func__);
+       }
+
+       return NOTIFY_DONE;
+}
+
+static int ec_wdt_start(struct watchdog_device *wdd)
+{
+       int ret;
+       int timeout, timeout_in_ms;
+       struct ec_wdt_data *ec_wdt_data = watchdog_get_drvdata(wdd);
+       struct adv_ec_platform_data *adv_ec_data;
+
+       dev_dbg(wdd->parent, "%s\n", __func__);
+
+       adv_ec_data = ec_wdt_data->adv_ec_data;
+       timeout = wdd->timeout; /* The watchdog devices timeout value (in seconds). */
+
+       mutex_lock(&ec_wdt_data->lock_ioctl);
+
+       timeout_in_ms = advwdt_set_heartbeat(timeout);
+       if (timeout_in_ms < 0) {
+               mutex_unlock(&ec_wdt_data->lock_ioctl);
+               return timeout_in_ms;
+       }
+
+       ret = set_delay(adv_ec_data, (unsigned short)(timeout_in_ms-1));
+       if (ret) {
+               dev_err(wdd->parent, "Failed to set Watchdog delay (ret=%x).\n", ret);
+               mutex_unlock(&ec_wdt_data->lock_ioctl);
+               return ret;
+       }
+       ret = write_hwram_command(adv_ec_data, EC_WDT_STOP);
+       ret = write_hwram_command(adv_ec_data, EC_WDT_START);
+       if (ret) {
+               dev_err(wdd->parent, "Failed to set Watchdog start (ret=%x).\n", ret);
+               mutex_unlock(&ec_wdt_data->lock_ioctl);
+               return ret;
+       }
+       ec_wdt_data->is_enable = 1;
+       ec_wdt_data->current_timeout = timeout_in_ms/10;
+
+       mutex_unlock(&ec_wdt_data->lock_ioctl);
+       return 0;
+}
+
+static int ec_wdt_stop(struct watchdog_device *wdd)
+{
+       int ret;
+       struct ec_wdt_data *ec_wdt_data = watchdog_get_drvdata(wdd);
+       struct adv_ec_platform_data *adv_ec_data;
+
+       dev_dbg(wdd->parent, "%s\n", __func__);
+
+       adv_ec_data = ec_wdt_data->adv_ec_data;
+
+       mutex_lock(&ec_wdt_data->lock_ioctl);
+       ret = write_hwram_command(adv_ec_data, EC_WDT_STOP);
+       mutex_unlock(&ec_wdt_data->lock_ioctl);
+       if (ret)
+               pr_err("Failed to set Watchdog stop.");
+       else
+               ec_wdt_data->is_enable = 0;
+
+       return ret;
+}
+
+static int ec_wdt_ping(struct watchdog_device *wdd)
+{
+       int ret;
+       struct ec_wdt_data *ec_wdt_data = watchdog_get_drvdata(wdd);
+       struct adv_ec_platform_data *adv_ec_data;
+
+       dev_dbg(wdd->parent, "%s\n", __func__);
+
+       adv_ec_data = ec_wdt_data->adv_ec_data;
+
+       mutex_lock(&ec_wdt_data->lock_ioctl);
+       ret = write_hwram_command(adv_ec_data, EC_WDT_RESET);
+       mutex_unlock(&ec_wdt_data->lock_ioctl);
+       if (ret) {
+               pr_err("Failed to set Watchdog reset.");
+               return -EINVAL;
+       }
+
+       return 0;
+}
+
+static int ec_wdt_set_timeout(struct watchdog_device *wdd,
+                               unsigned int timeout)
+{
+       dev_dbg(wdd->parent, "%s, timeout=%d\n", __func__, timeout);
+
+       wdd->timeout = timeout;
+
+       if (watchdog_active(wdd))
+               return ec_wdt_start(wdd);
+
+       return 0;
+}
+
+static const struct watchdog_info ec_watchdog_info = {
+       .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
+       .identity = "AHC1EC0 Watchdog",
+};
+
+static const struct watchdog_ops ec_watchdog_ops = {
+       .owner = THIS_MODULE,
+       .start = ec_wdt_start,
+       .stop = ec_wdt_stop,
+       .ping = ec_wdt_ping,
+       .set_timeout = ec_wdt_set_timeout,
+};
+
+static int adv_ec_wdt_probe(struct platform_device *pdev)
+{
+       int ret;
+       struct device *dev = &pdev->dev;
+       struct adv_ec_platform_data *adv_ec_data;
+       struct ec_wdt_data *ec_wdt_data;
+       struct watchdog_device *wdd;
+
+       dev_info(dev, "watchdog probe start\n");
+
+       adv_ec_data = dev_get_drvdata(dev->parent);
+       if (!adv_ec_data)
+               return -EINVAL;
+
+       ec_wdt_data = devm_kzalloc(dev, sizeof(struct ec_wdt_data), GFP_KERNEL);
+       if (!ec_wdt_data)
+               return -ENOMEM;
+
+       mutex_init(&ec_wdt_data->lock_ioctl);
+
+       ec_wdt_data->adv_ec_data = adv_ec_data;
+       wdd = &ec_wdt_data->wdtdev;
+
+       watchdog_init_timeout(&ec_wdt_data->wdtdev, 0, dev);
+
+       //watchdog_set_nowayout(&ec_wdt_data->wdtdev, WATCHDOG_NOWAYOUT);
+       watchdog_set_drvdata(&ec_wdt_data->wdtdev, ec_wdt_data);
+       platform_set_drvdata(pdev, ec_wdt_data);
+
+       wdd->info = &ec_watchdog_info;
+       wdd->ops = &ec_watchdog_ops;
+       wdd->min_timeout = EC_WDT_MIN_TIMEOUT;
+       wdd->max_timeout = EC_WDT_MAX_TIMEOUT;
+       wdd->parent = dev;
+
+       ec_wdt_data->wdtdev.timeout = EC_WDT_DEFAULT_TIMEOUT;
+       ec_wdt_data->is_enable = 0;
+       ec_wdt_data->current_timeout = EC_WDT_DEFAULT_TIMEOUT;
+
+       watchdog_stop_on_unregister(wdd);
+
+       ec_wdt_data->reboot_nb.notifier_call = advwdt_notify_sys;
+       ret = devm_register_reboot_notifier(dev, &ec_wdt_data->reboot_nb);
+       if (ret) {
+               dev_err(dev, "watchdog%d: Cannot register reboot notifier (%d)\n",
+                       wdd->id, ret);
+               return ret;
+       }
+
+       ret = devm_watchdog_register_device(dev, wdd);
+       if (ret != 0)
+               dev_err(dev, "watchdog_register_device() failed: %d\n",
+                       ret);
+
+       dev_info(dev, "watchdog register success\n");
+
+       return 0;
+}
+
+static struct platform_driver adv_wdt_drv = {
+       .driver = {
+               .name = DRV_NAME,
+       },
+       .probe = adv_ec_wdt_probe,
+};
+module_platform_driver(adv_wdt_drv);
+
+MODULE_LICENSE("GPL");
+MODULE_ALIAS("platform:" DRV_NAME);
+MODULE_DESCRIPTION("Advantech Embedded Controller Watchdog Driver.");
+MODULE_AUTHOR("Campion Kang <[email protected]>");
+MODULE_VERSION("1.0");
--
Quoted 2 lines

2.17.1

maintainer
#22 lore raw

Re: [PATCH v6 6/6] watchdog: ahc1ec0-wdt: Add sub-device watchdog for Advantech embedded controller

In reply to Campion Kang
Quoted 74 lines · Campion Kang

This is one of sub-device driver for Advantech embedded controller AHC1EC0. This driver provide watchdog functionality for Advantech related applications to restart the system.

Changed since V5:
- remove unnecessary header files
- bug fixed: reboot halt if watchdog enabled
- Kconfig: add "AHC1EC0" string to clearly define the EC name

---
 drivers/watchdog/Kconfig       |  11 ++
 drivers/watchdog/Makefile      |   1 +
 drivers/watchdog/ahc1ec0-wdt.c | 261 +++++++++++++++++++++++++++++++++
 3 files changed, 273 insertions(+)
 create mode 100644 drivers/watchdog/ahc1ec0-wdt.c
drivers/watchdog/Kconfig +11 -0
index 7ff941e71b79..1a27836883ac 100644
@@ -1636,6 +1636,17 @@ config NIC7018_WDT
 	  To compile this driver as a module, choose M here: the module will be
 	  called nic7018_wdt.
 
+config AHC1EC0_WDT
+	tristate "Advantech AHC1EC0 Watchdog Function"
+	depends on MFD_AHC1EC0
+	help
+	  This is sub-device for Advantech AHC1EC0 embedded controller.
+
+	  This driver provide watchdog functionality for Advantech related
+	  applications to restart the system.
+	  To compile thie driver as a module, choose M here: the module will be
+	  called ahc1ec0-wdt.
+
 # M68K Architecture
 
 config M54xx_WATCHDOG
drivers/watchdog/Makefile +1 -0
index 5c74ee19d441..7190811b1e50 100644
@@ -145,6 +145,7 @@ obj-$(CONFIG_INTEL_MID_WATCHDOG) += intel-mid_wdt.o
 obj-$(CONFIG_INTEL_MEI_WDT) += mei_wdt.o
 obj-$(CONFIG_NI903X_WDT) += ni903x_wdt.o
 obj-$(CONFIG_NIC7018_WDT) += nic7018_wdt.o
+obj-$(CONFIG_AHC1EC0_WDT) += ahc1ec0-wdt.o
 obj-$(CONFIG_MLX_WDT) += mlx_wdt.o
 
 # M68K Architecture
drivers/watchdog/ahc1ec0-wdt.c new file +17 -0
new file mode 100644
index 000000000000..4497b6106b24
@@ -0,0 +1,261 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Watchdog Driver for Advantech Embedded Controller chip AHC1EC0
+ *
+ * Copyright 2020, Advantech IIoT Group
+ *
+ */
+
+#include <linux/errno.h>
+#include <linux/kernel.h>
+#include <linux/mfd/ahc1ec0.h>
+#include <linux/module.h>
+#include <linux/notifier.h>
+#include <linux/platform_device.h>
+#include <linux/reboot.h>
+#include <linux/types.h>
+#include <linux/watchdog.h>

drivers/watchdog/ahc1ec0-wdt.c line 27

Quoted 10 lines
drivers/watchdog/ahc1ec0-wdt.c new file +10 -0
+
+#define DRV_NAME      "ahc1ec0-wdt"
+
+struct ec_wdt_data {
+	struct watchdog_device wdtdev;
+	struct adv_ec_platform_data *adv_ec_data;
+	struct notifier_block reboot_nb;
+	struct mutex lock_ioctl;
+	int is_enable;
+	int current_timeout;

is_enable and current_timeout are only written to and never read, and thus pointless.

drivers/watchdog/ahc1ec0-wdt.c line 36

Quoted 9 lines
drivers/watchdog/ahc1ec0-wdt.c new file +9 -0
+};
+
+#define EC_WDT_MIN_TIMEOUT 1	/* The watchdog devices minimum timeout value (in seconds). */
+#define EC_WDT_MAX_TIMEOUT 600  /* The watchdog devices maximum timeout value (in seconds) */
+#define EC_WDT_DEFAULT_TIMEOUT 45
+
+static int set_delay(struct adv_ec_platform_data *adv_ec_data, unsigned short delay_timeout_in_ms)
+{
+	if (write_hw_ram(adv_ec_data, EC_RESET_DELAY_TIME_L, delay_timeout_in_ms & 0x00FF)) {

write_hw_ram() is too generic for a device specific function. Please add a prefix.

drivers/watchdog/ahc1ec0-wdt.c line 37

Quoted 1 line
drivers/watchdog/ahc1ec0-wdt.c new file +1 -0
+		pr_err("Failed to set Watchdog Retset Time Low byte.");

Reset ?

Either case, please drop those error messages. If they happen they would likely be persistent and end up clogging the kernel log.

Quoted 8 lines
drivers/watchdog/ahc1ec0-wdt.c new file +8 -0
+		return -EINVAL;
+	}
+
+	if (write_hw_ram(adv_ec_data, EC_RESET_DELAY_TIME_H, (delay_timeout_in_ms & 0xFF00) >> 8)) {
+		pr_err("Failed to set Watchdog Retset Time Hight byte.");
+		return -EINVAL;
+	}
+

drivers/watchdog/ahc1ec0-wdt.c line 55

Quoted 10 lines
drivers/watchdog/ahc1ec0-wdt.c new file +10 -0
+	return 0;
+}
+
+static int advwdt_set_heartbeat(unsigned long t)
+{
+	if (t < 1 || t > 6553) {
+		pr_err("%s: the input timeout is out of range.",  __func__);
+		pr_err("Please choose valid data between 1 ~ 6553.");
+		return -EINVAL;
+	}

Unecessary check. The watchdog core performs limit checks. Repeating them does not add value.

drivers/watchdog/ahc1ec0-wdt.c line 57

Quoted 2 lines
drivers/watchdog/ahc1ec0-wdt.c new file +2 -0
+
+	return (t * 10);

Given that, this helper function is quite pointless (and misleading, since it doesn't set anything).

Quoted 13 lines
drivers/watchdog/ahc1ec0-wdt.c new file +13 -0
+}
+
+/* Notifier for system down */
+static int advwdt_notify_sys(struct notifier_block *nb, unsigned long code, void *data)
+{
+	if (code == SYS_DOWN || code == SYS_HALT) {
+		struct ec_wdt_data *ec_wdt_data;
+
+		ec_wdt_data = container_of(nb, struct ec_wdt_data, reboot_nb);
+		if (!ec_wdt_data)
+			return NOTIFY_BAD;
+
+		/* Turn the WDT off */

drivers/watchdog/ahc1ec0-wdt.c line 80

Quoted 10 lines
drivers/watchdog/ahc1ec0-wdt.c new file +10 -0
+		if (write_hwram_command(ec_wdt_data->adv_ec_data, EC_WDT_STOP)) {
+			pr_err("Failed to set Watchdog stop.");
+			return -EINVAL;
+		}
+		ec_wdt_data->is_enable = 0;
+		pr_info("%s: notify sys shutdown", __func__);
+	}
+
+	return NOTIFY_DONE;
+}

Please use the watchdog subsystem instead of declaring yor own notifier.

drivers/watchdog/ahc1ec0-wdt.c line 92

Quoted 12 lines
drivers/watchdog/ahc1ec0-wdt.c new file +12 -0
+
+static int ec_wdt_start(struct watchdog_device *wdd)
+{
+	int ret;
+	int timeout, timeout_in_ms;
+	struct ec_wdt_data *ec_wdt_data = watchdog_get_drvdata(wdd);
+	struct adv_ec_platform_data *adv_ec_data;
+
+	dev_dbg(wdd->parent, "%s\n", __func__);
+
+	adv_ec_data = ec_wdt_data->adv_ec_data;
+	timeout = wdd->timeout; /* The watchdog devices timeout value (in seconds). */

'timeout' is used just once. Just use wdd->timeout directly below.

drivers/watchdog/ahc1ec0-wdt.c line 94

Quoted 2 lines
drivers/watchdog/ahc1ec0-wdt.c new file +2 -0
+
+	mutex_lock(&ec_wdt_data->lock_ioctl);

The watchdog core ensures that there will be no more than one caller. What is this lock supposed to protect ?

drivers/watchdog/ahc1ec0-wdt.c line 96

Quoted 2 lines
drivers/watchdog/ahc1ec0-wdt.c new file +2 -0
+
+	timeout_in_ms = advwdt_set_heartbeat(timeout);

timeout_in_ms is the timeout in 10th of seconds. "_ms" is misleading.

drivers/watchdog/ahc1ec0-wdt.c line 102

Quoted 6 lines
drivers/watchdog/ahc1ec0-wdt.c new file +6 -0
+	if (timeout_in_ms < 0) {
+		mutex_unlock(&ec_wdt_data->lock_ioctl);
+		return timeout_in_ms;
+	}
+
+	ret = set_delay(adv_ec_data, (unsigned short)(timeout_in_ms-1));

Unnecessary type cast.

drivers/watchdog/ahc1ec0-wdt.c line 104

Quoted 2 lines
drivers/watchdog/ahc1ec0-wdt.c new file +2 -0
+	if (ret) {
+		dev_err(wdd->parent, "Failed to set Watchdog delay (ret=%x).\n", ret);

This driver is extremely noisy when it comes to error messages.

drivers/watchdog/ahc1ec0-wdt.c line 108

Quoted 4 lines
drivers/watchdog/ahc1ec0-wdt.c new file +4 -0
+		mutex_unlock(&ec_wdt_data->lock_ioctl);
+		return ret;
+	}
+	ret = write_hwram_command(adv_ec_data, EC_WDT_STOP);

Errors from this call are ignored. If that is on purpose, the assignment to 'ret' is unnecessary.

drivers/watchdog/ahc1ec0-wdt.c line 113

Quoted 5 lines
drivers/watchdog/ahc1ec0-wdt.c new file +5 -0
+	ret = write_hwram_command(adv_ec_data, EC_WDT_START);
+	if (ret) {
+		dev_err(wdd->parent, "Failed to set Watchdog start (ret=%x).\n", ret);
+		mutex_unlock(&ec_wdt_data->lock_ioctl);
+		return ret;

This should be handled with a goto to the end of the function. See error handling in coding style.

drivers/watchdog/ahc1ec0-wdt.c line 116

Quoted 3 lines
drivers/watchdog/ahc1ec0-wdt.c new file +3 -0
+	}
+	ec_wdt_data->is_enable = 1;
+	ec_wdt_data->current_timeout = timeout_in_ms/10;

The conversion from timeout -> timeout_in_ms -> timeout seems to be quite pointless.

Quoted 89 lines
drivers/watchdog/ahc1ec0-wdt.c new file +89 -0
+
+	mutex_unlock(&ec_wdt_data->lock_ioctl);
+	return 0;
+}
+
+static int ec_wdt_stop(struct watchdog_device *wdd)
+{
+	int ret;
+	struct ec_wdt_data *ec_wdt_data = watchdog_get_drvdata(wdd);
+	struct adv_ec_platform_data *adv_ec_data;
+
+	dev_dbg(wdd->parent, "%s\n", __func__);
+
+	adv_ec_data = ec_wdt_data->adv_ec_data;
+
+	mutex_lock(&ec_wdt_data->lock_ioctl);
+	ret = write_hwram_command(adv_ec_data, EC_WDT_STOP);
+	mutex_unlock(&ec_wdt_data->lock_ioctl);
+	if (ret)
+		pr_err("Failed to set Watchdog stop.");
+	else
+		ec_wdt_data->is_enable = 0;
+
+	return ret;
+}
+
+static int ec_wdt_ping(struct watchdog_device *wdd)
+{
+	int ret;
+	struct ec_wdt_data *ec_wdt_data = watchdog_get_drvdata(wdd);
+	struct adv_ec_platform_data *adv_ec_data;
+
+	dev_dbg(wdd->parent, "%s\n", __func__);
+
+	adv_ec_data = ec_wdt_data->adv_ec_data;
+
+	mutex_lock(&ec_wdt_data->lock_ioctl);
+	ret = write_hwram_command(adv_ec_data, EC_WDT_RESET);
+	mutex_unlock(&ec_wdt_data->lock_ioctl);
+	if (ret) {
+		pr_err("Failed to set Watchdog reset.");
+		return -EINVAL;
+	}
+
+	return 0;
+}
+
+static int ec_wdt_set_timeout(struct watchdog_device *wdd,
+				unsigned int timeout)
+{
+	dev_dbg(wdd->parent, "%s, timeout=%d\n", __func__, timeout);
+
+	wdd->timeout = timeout;
+
+	if (watchdog_active(wdd))
+		return ec_wdt_start(wdd);
+
+	return 0;
+}
+
+static const struct watchdog_info ec_watchdog_info = {
+	.options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
+	.identity = "AHC1EC0 Watchdog",
+};
+
+static const struct watchdog_ops ec_watchdog_ops = {
+	.owner = THIS_MODULE,
+	.start = ec_wdt_start,
+	.stop = ec_wdt_stop,
+	.ping = ec_wdt_ping,
+	.set_timeout = ec_wdt_set_timeout,
+};
+
+static int adv_ec_wdt_probe(struct platform_device *pdev)
+{
+	int ret;
+	struct device *dev = &pdev->dev;
+	struct adv_ec_platform_data *adv_ec_data;
+	struct ec_wdt_data *ec_wdt_data;
+	struct watchdog_device *wdd;
+
+	dev_info(dev, "watchdog probe start\n");
+
+	adv_ec_data = dev_get_drvdata(dev->parent);
+	if (!adv_ec_data)
+		return -EINVAL;
+
+	ec_wdt_data = devm_kzalloc(dev, sizeof(struct ec_wdt_data), GFP_KERNEL);
+	if (!ec_wdt_data)

drivers/watchdog/ahc1ec0-wdt.c line 215

Quoted 10 lines
drivers/watchdog/ahc1ec0-wdt.c new file +10 -0
+		return -ENOMEM;
+
+	mutex_init(&ec_wdt_data->lock_ioctl);
+
+	ec_wdt_data->adv_ec_data = adv_ec_data;
+	wdd = &ec_wdt_data->wdtdev;
+
+	watchdog_init_timeout(&ec_wdt_data->wdtdev, 0, dev);
+
+	//watchdog_set_nowayout(&ec_wdt_data->wdtdev, WATCHDOG_NOWAYOUT);

Please no C++ comments in the code.

Quoted 12 lines
drivers/watchdog/ahc1ec0-wdt.c new file +12 -0
+	watchdog_set_drvdata(&ec_wdt_data->wdtdev, ec_wdt_data);
+	platform_set_drvdata(pdev, ec_wdt_data);
+
+	wdd->info = &ec_watchdog_info;
+	wdd->ops = &ec_watchdog_ops;
+	wdd->min_timeout = EC_WDT_MIN_TIMEOUT;
+	wdd->max_timeout = EC_WDT_MAX_TIMEOUT;
+	wdd->parent = dev;
+
+	ec_wdt_data->wdtdev.timeout = EC_WDT_DEFAULT_TIMEOUT;
+	ec_wdt_data->is_enable = 0;
+	ec_wdt_data->current_timeout = EC_WDT_DEFAULT_TIMEOUT;

drivers/watchdog/ahc1ec0-wdt.c line 237

Quoted 10 lines
drivers/watchdog/ahc1ec0-wdt.c new file +10 -0
+
+	watchdog_stop_on_unregister(wdd);
+
+	ec_wdt_data->reboot_nb.notifier_call = advwdt_notify_sys;
+	ret = devm_register_reboot_notifier(dev, &ec_wdt_data->reboot_nb);
+	if (ret) {
+		dev_err(dev, "watchdog%d: Cannot register reboot notifier (%d)\n",
+			wdd->id, ret);
+		return ret;
+	}

Use watchdog_stop_on_reboot().

drivers/watchdog/ahc1ec0-wdt.c line 242

Quoted 5 lines
drivers/watchdog/ahc1ec0-wdt.c new file +5 -0
+
+	ret = devm_watchdog_register_device(dev, wdd);
+	if (ret != 0)
+		dev_err(dev, "watchdog_register_device() failed: %d\n",
+			ret);

And right afterwards "watchdog register success", and the error is ignored.

Quoted 22 lines
drivers/watchdog/ahc1ec0-wdt.c new file +19 -0
+
+	dev_info(dev, "watchdog register success\n");
+
+	return 0;
+}
+
+static struct platform_driver adv_wdt_drv = {
+	.driver = {
+		.name = DRV_NAME,
+	},
+	.probe = adv_ec_wdt_probe,
+};
+module_platform_driver(adv_wdt_drv);
+
+MODULE_LICENSE("GPL");
+MODULE_ALIAS("platform:" DRV_NAME);
+MODULE_DESCRIPTION("Advantech Embedded Controller Watchdog Driver.");
+MODULE_AUTHOR("Campion Kang <[email protected]>");
+MODULE_VERSION("1.0");
Signature · 1 line
2.17.1