hwmon: add Microchip EMC2101 driver
Subsystems (3)
v2 cover hwmon: add Microchip EMC2101 driver
The Microchip EMC2101 is a SMBus 2.0 fan controller with temperature monitoring.
It supports up to 1 fan, 1 internal temperature sensor, 1 external temperature sensor and an 8 entry look up table to create a programmable temperature response.
v2: multiple improvements:
- add emc2101.rst to index.rst.
- add missing documentation properties.
- Remove FAN_RPM_MIN definition.
- Rename FAN_FALSE_READ to FAN_MIN_READ.
- pwm_auto_point_temp_hyst_store(): simplify function.
- emc2101_fan_min_read(): add missing FAN_MIN_READ condition.
- emc2101_fan_min_write(): fix tach_count calculation.
- emc2101_init(): fix REG_TACH_MIN value.
Álvaro Fernández Rojas (3):
docs: hwmon: add emc2101.rst to docs
dt-bindings: hwmon: Add Microchip EMC2101 support
drivers: hwmon: add EMC2101 driver
.../bindings/hwmon/microchip,emc2101.yaml | 79 + Documentation/hwmon/emc2101.rst | 61 + Documentation/hwmon/index.rst | 1 + drivers/hwmon/Kconfig | 10 + drivers/hwmon/Makefile | 1 + drivers/hwmon/emc2101.c | 2175 +++++++++++++++++ 6 files changed, 2327 insertions(+) create mode 100644 Documentation/devicetree/bindings/hwmon/microchip,emc2101.yaml create mode 100644 Documentation/hwmon/emc2101.rst create mode 100644 drivers/hwmon/emc2101.c
Signature · 1 line
2.39.5
PATCH v2 1/3 docs: hwmon: add emc2101.rst to docs
Add description of emc2101 driver.
--- Documentation/hwmon/emc2101.rst | 61 +++++++++++++++++++++++++++++++++ Documentation/hwmon/index.rst | 1 + 2 files changed, 62 insertions(+) create mode 100644 Documentation/hwmon/emc2101.rst
v2: add emc2101 to index.rst
Documentation/hwmon/emc2101.rst new file +61 -0
@@ -0,0 +1,61 @@ +.. SPDX-License-Identifier: GPL-2.0-or-later + +Kernel driver emc2101 +===================== + +Supported chips: + Microchip EMC2101, EMC2101-R + + Addresses scanned: I2C 0x4c + + Prefixes: 'emc2101', emc2101-r' + + Datasheet: Publicly available at the Microchip website : + https://www.microchip.com/en-us/product/EMC2101 + +Description: +------------ +This driver implements support for Microchip EMC2101 RPM-based PWM Fan Controller. +The EMC2101 Fan Controller supports up to 1 controlled PWM fan based on an +external temperature diode. +Fan rotation speed is reported in RPM. +The driver supports the eight entries temperature look up table to automatically +adjust the fan speed. + +The driver provides the following sysfs interfaces in hwmon subsystem: + +========================= == =================================================== +fan1_div RW file for fan target duty cycle divider (0..255) +fan1_input RO file for TACH1 input (in RPM) +fan1_min RW file for TACH1 min RPM +fan1_min_alarm RO file for TACH1 min RPM alarm indication +fan1_spin_up_abort RW file for fan spin up abort on low RPM +fan1_spin_up_power RW file for fan spin up power (in percentage) +fan1_spin_up_time RW file for fan spin up time (in ms) +fan1_standby RW file for fan standby mode +pwm1 RW file for fan target duty cycle (0..63) +pwm1_auto_channels_temp RW file for fan temperature sensor (external, force) +pwm1_auto_point[1-8]_pwm RW files for look up table fan speed +pwm1_auto_point[1-8]_temp RW files for look up table temperature +pwm1_auto_point_temp_hyst RW file for look up table temperature hysteresis +pwm1_enable RW file for fan config (manual, look up table) +pwm1_freq RW file for fan target frequency +pwm1_mode RW file for pwm mode (DAC, PWM) +pwm1_polarity_invert RW file for fan polarity inversion +temp[1-3]_label RO files for temperature labels +temp1_input RO file for internal temperature +temp1_max RW file for max internal temperature +temp1_max_alarm RO file for max internal temperature alarm indication +temp2_crit RW file for crit external temperature +temp2_crit_alarm RO file for crit external temperature alarm indication +temp2_crit_hyst RW file for crit external temperature hysteresis +temp2_fault RO file for external temperature failure indication +temp2_input RO file for external temperature +temp2_max RW file for max external temperature +temp2_max_alarm RO file for max external temperature alarm indication +temp2_min RW file for min external temperature +temp2_min_alarm RO file for min external temperature alarm indication +temp2_type RW file for external temperature type (CPU, 2N3904) +temp3 RW file for forced temperature +update_interval RW file for temperature sensor update interval +========================= == ===================================================
Documentation/hwmon/index.rst +1 -0
@@ -69,6 +69,7 @@ Hardware Monitoring Kernel Drivers ds1621 ds620 emc1403 + emc2101 emc2103 emc2305 emc6w201
Signature · 1 line
2.39.5
PATCH v2 2/3 dt-bindings: hwmon: Add Microchip EMC2101 support
In reply to Álvaro Fernández RojasIntroduce yaml schema for Microchip emc2101 pwm fan controller with temperature monitoring.
--- .../bindings/hwmon/microchip,emc2101.yaml | 79 +++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 Documentation/devicetree/bindings/hwmon/microchip,emc2101.yaml
v2: add missing properties.
Documentation/devicetree/bindings/hwmon/microchip,emc2101.yaml new file +79 -0
@@ -0,0 +1,79 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/hwmon/microchip,emc2101.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Microchip EMC2101 SMBus compliant PWM fan controller + +maintainers: + - Álvaro Fernández Rojas <[email protected]> + +description: + Microchip EMC2101 pwm controller which supports up to 1 fan, 1 internal + temperature sensor, 1 external temperature sensor and an 8 entry look + up table to create a programmable temperature response. + +properties: + compatible: + enum: + - microchip,emc2101 + - microchip,emc2101-r + + reg: + maxItems: 1 + + '#address-cells': + const: 1 + + '#size-cells': + const: 0 + + '#pwm-cells': + const: 2 + description: | + Number of cells in a PWM specifier. + - cell 0: The PWM frequency + - cell 1: The PWM polarity: 0 or PWM_POLARITY_INVERTED + +patternProperties: + '^fan@0$': + $ref: fan-common.yaml# + unevaluatedProperties: false + properties: + reg: + description: + The fan number used to determine the associated PWM channel. + maxItems: 1 + + required: + - reg + +required: + - compatible + - reg + +additionalProperties: false + +examples: + - | + i2c { + #address-cells = <1>; + #size-cells = <0>; + + fan_controller: fan-controller@4c { + compatible = "microchip,emc2101"; + reg = <0x4c>; + + #address-cells = <1>; + #size-cells = <0>; + #pwm-cells = <2>; + + fan@0 { + reg = <0x0>; + pwms = <&fan_controller 5806 0>; + #cooling-cells = <2>; + }; + }; + }; +...
Signature · 1 line
2.39.5
Rob Herring (Arm)
maintainerRe: [PATCH v2 2/3] dt-bindings: hwmon: Add Microchip EMC2101 support
Quoted 11 lines · Álvaro Fernández Rojas
Introduce yaml schema for Microchip emc2101 pwm fan controller with temperature monitoring.
Signed-off-by Álvaro Fernández Rojas <[email protected]>--- .../bindings/hwmon/microchip,emc2101.yaml | 79 +++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 Documentation/devicetree/bindings/hwmon/microchip,emc2101.yamlv2: add missing properties.
My bot found errors running 'make dt_binding_check' on your patch:
yamllint warnings/errors:
dtschema/dtc warnings/errors:
/builds/robherring/dt-review-ci/linux/Documentation/devicetree/bindings/hwmon/microchip,emc2101.yaml: patternProperties: '^fan@0$' should not be valid under {'pattern': '^\\^[a-zA-Z0-9,\\-._#@]+\\$$'}
hint: Fixed strings belong in 'properties', not 'patternProperties'
from schema $id: http://devicetree.org/meta-schemas/keywords.yaml#
doc reference errors (make refcheckdocs):
See https://patchwork.ozlabs.org/project/devicetree-bindings/patch/[email protected]
The base for the series is generally the latest rc1. A different dependency should be noted in *this* patch.
If you already ran 'make dt_binding_check' and didn't see the above error(s), then make sure 'yamllint' is installed and dt-schema is up to date:
pip3 install dtschema --upgrade
Please check and re-submit after running the above command yourself. Note that DT_SCHEMA_FILES can be set to your schema file to speed up checking your schema. However, it must be unset to test all examples with your schema.
Re: [PATCH v2 2/3] dt-bindings: hwmon: Add Microchip EMC2101 support
In reply to Álvaro Fernández RojasQuoted 28 lines · Álvaro Fernández Rojas
Introduce yaml schema for Microchip emc2101 pwm fan controller with temperature monitoring.
Signed-off-by Álvaro Fernández Rojas <[email protected]>--- .../bindings/hwmon/microchip,emc2101.yaml | 79 +++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 Documentation/devicetree/bindings/hwmon/microchip,emc2101.yamlv2: add missing properties.
Documentation/devicetree/bindings/hwmon/microchip,emc2101.yaml new file +11
-0@@ -0,0 +1,79 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/hwmon/microchip,emc2101.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Microchip EMC2101 SMBus compliant PWM fan controller + +maintainers: + - Álvaro Fernández Rojas <[email protected]> +
Documentation/devicetree/bindings/hwmon/microchip,emc2101.yaml line 21
Quoted 10 lines
Documentation/devicetree/bindings/hwmon/microchip,emc2101.yaml new file +10
-0+description: + Microchip EMC2101 pwm controller which supports up to 1 fan, 1 internal + temperature sensor, 1 external temperature sensor and an 8 entry look + up table to create a programmable temperature response. + +properties: + compatible: + enum: + - microchip,emc2101 + - microchip,emc2101-r
Nothing in description explains what 'r' is.
Quoted 9 lines
Documentation/devicetree/bindings/hwmon/microchip,emc2101.yaml new file +9
-0+ + reg: + maxItems: 1 + + '#address-cells': + const: 1 + + '#size-cells': + const: 0
Documentation/devicetree/bindings/hwmon/microchip,emc2101.yaml line 40
Quoted 10 lines
Documentation/devicetree/bindings/hwmon/microchip,emc2101.yaml new file +10
-0+ + '#pwm-cells': + const: 2 + description: | + Number of cells in a PWM specifier. + - cell 0: The PWM frequency + - cell 1: The PWM polarity: 0 or PWM_POLARITY_INVERTED + +patternProperties: + '^fan@0$':
This is just 'fan', not a pattern. See other bidnings.
Documentation/devicetree/bindings/hwmon/microchip,emc2101.yaml line 46
Quoted 6 lines
Documentation/devicetree/bindings/hwmon/microchip,emc2101.yaml new file +6
-0+ $ref: fan-common.yaml# + unevaluatedProperties: false + properties: + reg: + description: + The fan number used to determine the associated PWM channel.
I can tell you: 0. Cannot be any other PWM channel.
Documentation/devicetree/bindings/hwmon/microchip,emc2101.yaml line 54
Quoted 8 lines
Documentation/devicetree/bindings/hwmon/microchip,emc2101.yaml new file +8
-0+ maxItems: 1 + + required: + - reg + +required: + - compatible + - reg
Best regards,
Krzysztof
PATCH 1/1 drivers: hwmon: add EMC2101 driver
In reply to Álvaro Fernández RojasThe Microchip EMC2101 is a SMBus 2.0 fan controller with temperature monitoring.
It supports up to 1 fan, 1 internal temperature sensor, 1 external temperature sensor and an 8 entry look up table to create a programmable temperature response.
--- drivers/hwmon/Kconfig | 10 + drivers/hwmon/Makefile | 1 + drivers/hwmon/emc2101.c | 2175 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 2186 insertions(+) create mode 100644 drivers/hwmon/emc2101.c
v2: multiple improvements:
- Remove FAN_RPM_MIN definition.
- Rename FAN_FALSE_READ to FAN_MIN_READ.
- pwm_auto_point_temp_hyst_store(): simplify function.
- emc2101_fan_min_read(): add missing FAN_MIN_READ condition.
- emc2101_fan_min_write(): fix tach_count calculation.
- emc2101_init(): fix REG_TACH_MIN value.
drivers/hwmon/Kconfig +10 -0
@@ -2002,6 +2002,16 @@ config SENSORS_EMC1403 Threshold values can be configured using sysfs. Data from the different diodes are accessible via sysfs. +config SENSORS_EMC2101 + tristate "SMSC EMC2101" + depends on I2C + help + If you say yes here you get support for the SMSC EMC2101 + fan controller chips. + + This driver can also be built as a module. If so, the module + will be called emc2101. + config SENSORS_EMC2103 tristate "SMSC EMC2103" depends on I2C
drivers/hwmon/Makefile +1 -0
@@ -73,6 +73,7 @@ obj-$(CONFIG_SENSORS_DRIVETEMP) += drivetemp.o obj-$(CONFIG_SENSORS_DS620) += ds620.o obj-$(CONFIG_SENSORS_DS1621) += ds1621.o obj-$(CONFIG_SENSORS_EMC1403) += emc1403.o +obj-$(CONFIG_SENSORS_EMC2101) += emc2101.o obj-$(CONFIG_SENSORS_EMC2103) += emc2103.o obj-$(CONFIG_SENSORS_EMC2305) += emc2305.o obj-$(CONFIG_SENSORS_EMC6W201) += emc6w201.o
drivers/hwmon/emc2101.c new file +2176 -0
2,176 lines not on this page · raw message
Signature · 1 line
2.39.5
Re: [PATCH] drivers: hwmon: add EMC2101 driver
Quoted 249 lines · Álvaro Fernández Rojas
The Microchip EMC2101 is a SMBus 2.0 fan controller with temperature monitoring.
It supports up to 1 fan, 1 internal temperature sensor, 1 external temperature sensor and an 8 entry look up table to create a programmable temperature response.Signed-off-by Álvaro Fernández Rojas <[email protected]>--- drivers/hwmon/Kconfig | 10 + drivers/hwmon/Makefile | 1 + drivers/hwmon/emc2101.c | 2175 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 2186 insertions(+) create mode 100644 drivers/hwmon/emc2101.cv2: multiple improvements:
- RemoveFAN_RPM_MINdefinition.
- RenameFAN_FALSE_READtoFAN_MIN_READ.
-pwm_auto_point_temp_hyst_store(): simplify function.
-emc2101_fan_min_read(): add missingFAN_MIN_READcondition.
-emc2101_fan_min_write(): fixtach_countcalculation.
-emc2101_init(): fixREG_TACH_MINvalue.drivers/hwmon/Kconfig +10
-0@@ -2002,6 +2002,16 @@ config SENSORS_EMC1403 Threshold values can be configured using sysfs. Data from the different diodes are accessible via sysfs. +config SENSORS_EMC2101 + tristate "SMSC EMC2101" + depends on I2C + help + If you say yes here you get support for the SMSC EMC2101 + fan controller chips. + + This driver can also be built as a module. If so, the module + will be called emc2101. + config SENSORS_EMC2103 tristate "SMSC EMC2103" depends on I2Cdrivers/hwmon/Makefile +1
-0@@ -73,6 +73,7 @@ obj-$(CONFIG_SENSORS_DRIVETEMP) += drivetemp.o obj-$(CONFIG_SENSORS_DS620) += ds620.o obj-$(CONFIG_SENSORS_DS1621) += ds1621.o obj-$(CONFIG_SENSORS_EMC1403) += emc1403.o +obj-$(CONFIG_SENSORS_EMC2101) += emc2101.o obj-$(CONFIG_SENSORS_EMC2103) += emc2103.o obj-$(CONFIG_SENSORS_EMC2305) += emc2305.o obj-$(CONFIG_SENSORS_EMC6W201) += emc6w201.odrivers/hwmon/emc2101.c new file +188
-0@@ -0,0 +1,2176 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* +* Driver for Microchip EMC2101 fan controller. +* +* Copyright 2025 Álvaro Fernández Rojas <[email protected]> +*/ + +#include <linux/err.h> +#include <linux/hwmon.h> +#include <linux/hwmon-sysfs.h> +#include <linux/i2c.h> +#include <linux/init.h> +#include <linux/jiffies.h> +#include <linux/module.h> +#include <linux/mutex.h> +#include <linux/slab.h> +#include <linux/util_macros.h> + +#define REG_TEMP_INT 0x00 +#define REG_TEMP_EXT_HI 0x01 +#define REG_STATUS 0x02 +#define ADC_BUSY BIT(7) +#define TEMP_INT_HIGH BIT(6) +#define EEPROM_ERROR BIT(5) +#define TEMP_EXT_HIGH BIT(4) +#define TEMP_EXT_LOW BIT(3) +#define TEMP_EXT_FAULT BIT(2) +#define TEMP_EXT_CRIT BIT(1) +#define TACH_LOW BIT(0) +#define REG_CONFIG 0x03 +#define ALERT_IRQ_ACK BIT(7) +#define FAN_STANDBY_ENABLE BIT(6) +#define FAN_STANDBY_MODE BIT(5) +#define FAN_MODE_DAC BIT(4) +#define SMBUS_TOUT_DISABLE BIT(3) +#define PIN_FUNC_TACH BIT(2) +#define TEMP_EXT_CRIT_UNLOCK BIT(1) +#define PIN_ASSERT_3_EXC BIT(0) +#define REG_CONV_RATE 0x04 +#define CONV_RATE_MASK 0xf +#define REG_TEMP_INT_MAX 0x05 +#define REG_TEMP_EXT_MAX_HI 0x07 +#define REG_TEMP_EXT_MIN_HI 0x08 +#define REG_TEMP_EXT_FORCE 0x0c +#define REG_ONE_SHOT 0x0f +#define REG_TEMP_EXT_LO 0x10 +#define REG_SCRATCHPAD_1 0x11 +#define REG_SCRATCHPAD_2 0x12 +#define REG_TEMP_EXT_MAX_LO 0x13 +#define REG_TEMP_EXT_MIN_LO 0x14 +#define REG_ALERT_MASK 0x16 +#define IRQ_TEMP_INT_MAX_DISABLE BIT(6) +#define IRQ_TEMP_EXT_MAX_DISABLE BIT(4) +#define IRQ_TEMP_EXT_MIN_DISABLE BIT(3) +#define IRQ_TEMP_EXT_CRIT_DISABLE BIT(1) +#define IRQ_TACH_MIN_DISABLE BIT(0) +#define REG_EXT_IDEALITY 0x17 +#define EXT_IDEALITY_START 9846 +#define EXT_IDEALITY_STEP 13 +#define EXT_IDEALITY_VAL (x) (EXT_IDEALITY_START + \ + ((x) * EXT_IDEALITY_STEP)) +#define EXT_IDEALITY_MASK 0x3f +#define REG_BETA_COMP 0x18 +#define BETA_COMP_AUTO BIT(3) +#define BETA_COMP_DISABLE 7 +#define BETA_COMP_2_33 6 +#define BETA_COMP_1_00 5 +#define BETA_COMP_0_43 4 +#define BETA_COMP_0_33 3 +#define BETA_COMP_0_25 2 +#define BETA_COMP_0_18 1 +#define BETA_COMP_0_11 0 +#define BETA_COMP_MASK 0x7 +#define REG_TEMP_EXT_CRIT 0x19 +#define REG_TEMP_EXT_CRIT_HYST 0x21 +#define REG_TACH_LO 0x46 +#define REG_TACH_HI 0x47 +#define REG_TACH_MIN_LO 0x48 +#define REG_TACH_MIN_HI 0x49 +#define REG_FAN_CONFIG 0x4a +#define FAN_EXT_TEMP_FORCE BIT(6) +#define FAN_LUT_DISABLE BIT(5) +#define FAN_POL_INV BIT(4) +#define FAN_CLK_SEL BIT(3) +#define FAN_CLK_OVR BIT(2) +#define TACH_FALSE_READ_DISABLE BIT(0) +#define TACH_FALSE_READ_MASK 0x3 +#define REG_FAN_SPIN 0x4b +#define FAN_SPIN_UP_ABORT BIT(5) +#define FAN_SPIN_UP_POWER_SHIFT 3 +#define FAN_SPIN_UP_POWER_100 (3 << FAN_SPIN_UP_POWER_SHIFT) +#define FAN_SPIN_UP_POWER_75 (2 << FAN_SPIN_UP_POWER_SHIFT) +#define FAN_SPIN_UP_POWER_50 (1 << FAN_SPIN_UP_POWER_SHIFT) +#define FAN_SPIN_UP_POWER_0 (0 << FAN_SPIN_UP_POWER_SHIFT) +#define FAN_SPIN_UP_POWER_MASK (0x3 << FAN_SPIN_UP_POWER_SHIFT) +#define FAN_SPIN_UP_TIME_3200 7 +#define FAN_SPIN_UP_TIME_1600 6 +#define FAN_SPIN_UP_TIME_800 5 +#define FAN_SPIN_UP_TIME_400 4 +#define FAN_SPIN_UP_TIME_200 3 +#define FAN_SPIN_UP_TIME_100 2 +#define FAN_SPIN_UP_TIME_50 1 +#define FAN_SPIN_UP_TIME_0 0 +#define FAN_SPIN_UP_TIME_MASK 0x7 +#define REG_FAN_SET 0x4c +#define FAN_SET_MASK 0x3f +#define REG_PWM_FREQ 0x4d +#define PWM_FREQ_MASK 0x1f +#define REG_PWM_FREQ_DIV 0x4e +#define REG_FAN_LUT_HYST 0x4f +#define FAN_LUT_HYST_MASK 0x1f +#define REG_FAN_LUT_TEMP (x) (0x50 + (0x2 * (x))) +/* Write only with FAN_LUT_DISABLE */ +#define FAN_LUT_TEMP_MASK 0x7f +#define REG_FAN_LUT_SPEED (x) (0x51 + (0x2 * (x))) +/* Write only with FAN_LUT_DISABLE */ +#define FAN_LUT_SPEED_MASK 0x3f +#define REG_AVG_FILTER 0xbf +#define FILTER_SHIFT 1 +#define FILTER_L2 (3 << FILTER_SHIFT) +#define FILTER_L1 (1 << FILTER_SHIFT) +#define FILTER_NONE (0 << FILTER_SHIFT) +#define FILTER_MASK (0x3 << FILTER_SHIFT) +#define ALERT_PIN_TEMP_COMP BIT(0) +#define REG_PRODUCT_ID 0xfd +#define REG_MANUFACTURER_ID 0xfe +#define REG_REVISION 0xff + +#define CLK_FREQ_ALT 1400 +#define CLK_FREQ_BASE 360000 + +#define FAN_LUT_COUNT 8 +#define FAN_LUT_HYST_DEF 4 +#define FAN_LUT_HYST_MIN 0 +#define FAN_LUT_HYST_MAX 31 +#define FAN_MIN_READ 0xffff +#define FAN_RPM_FACTOR 5400000 + +#define MANUFACTURER_ID 0x5d + +#define TEMP_EXT_HI_FAULT 0x7f +#define TEMP_EXT_LO_FAULT_OPEN 0x00 +#define TEMP_EXT_LO_FAULT_SHORT 0xe0 + +#define TEMP_LO_FRAC 125 +#define TEMP_LO_SHIFT 5 +#define TEMP_LO_MASK (0x3 << TEMP_LO_SHIFT) + +#define TEMP_MIN -64 +#define TEMP_MAX 127 +#define TEMP_MAX_FRAC 750 + +enum emc2101_fan_spin_up_abort { +EMC2101_FAN_SPIN_ABORT_DISABLE =0 , +EMC2101_FAN_SPIN_ABORT_ENABLE +}; + +enum emc2101_fan_standby { +EMC2101_FAN_STBY_DISABLE =0 , +EMC2101_FAN_STBY_ENABLE +}; + +enum emc2101_mode { +EMC2101_MODE_PWM =0 , +EMC2101_MODE_DAC +}; + +enum ecm2101_product_id { +EMC2101 =0x16 , +EMC2101_R =0x28 +}; + +enum emc2101_pwm_enable { +EMC2101_PWM_MANUAL =1 , +EMC2101_PWM_LUT =2 +}; + +enum emc2101_pwm_polarity { +EMC2101_POL_NORMAL =0 , +EMC2101_POL_INVERTED +}; + +enum emc2101_temp_channels { +EMC2101_TC_INT =0 , +EMC2101_TC_EXT , +EMC2101_TC_FORCE , +EMC2101_TC_NUM +};
drivers/hwmon/emc2101.c line 198
Quoted 10 lines
drivers/hwmon/emc2101.c new file +10
-0+ +enum emc2101_temp_diode { +EMC2101_TD_CPU =1 , +EMC2101_TD_2N3904 =2 +}; + +struct emc2101_data { +struct i2c_client *client; +struct device *dev; +struct mutex mutex;
Add a comment describing what you are protecting here. It looks so far like you could just use regmap and drop the mutex, but I didn't check thoroughly.
Quoted 1 line
drivers/hwmon/emc2101.c new file +1
-0+};
...
drivers/hwmon/emc2101.c line 2091
Quoted 12 lines
drivers/hwmon/emc2101.c new file +12
-0+ +static int emc2101_probe (struct i2c_client *client) +{ +struct i2c_adapter *adapter = client->adapter; +struct device *dev = &client->dev; +struct emc2101_data *data; +struct device *hwmon_dev; + +if (!i2c_check_functionality (adapter,I2C_FUNC_SMBUS_BYTE_DATA )) +return -EIO ; + + data =devm_kzalloc (dev,sizeof (struct emc2101_data ),GFP_KERNEL );
sizeof(*)
Quoted 4 lines
drivers/hwmon/emc2101.c new file +4
-0+if (!data) +return -ENOMEM ; + + data->client = client;
drivers/hwmon/emc2101.c line 2105
Quoted 10 lines
drivers/hwmon/emc2101.c new file +10
-0+ data->dev = dev; +mutex_init (&data->mutex); + + hwmon_dev =devm_hwmon_device_register_with_info (dev, client->name, data, + &emc2101_chip_info, + emc2101_hwmon_groups); +if (IS_ERR (hwmon_dev)) +return PTR_ERR (hwmon_dev); + +dev_info (dev,"%s: sensor '%s'\n" ,dev_name (hwmon_dev), client->name);
Drivers should be silent oon success. We already know that this probed based on sysfs.
Quoted 37 lines
drivers/hwmon/emc2101.c new file +37
-0+ +return emc2101_init (data); +} + +static int emc2101_detect (struct i2c_client *client,struct i2c_board_info *info) +{ +struct i2c_adapter *adapter = client->adapter; +s32 manufacturer, product, revision; +struct device *dev = &adapter->dev; + +if (!i2c_check_functionality (adapter,I2C_FUNC_SMBUS_BYTE_DATA )) +return -ENODEV ; + + manufacturer =i2c_smbus_read_byte_data (client,REG_MANUFACTURER_ID ); +if (manufacturer !=MANUFACTURER_ID ) +return -ENODEV ; + + product =i2c_smbus_read_byte_data (client,REG_PRODUCT_ID ); +switch (product) { +case EMC2101 : +strscpy (info->type,"emc2101" ,I2C_NAME_SIZE ); +break ; +case EMC2101_R : +strscpy (info->type,"emc2101-r" ,I2C_NAME_SIZE ); +break ; +default : +return -ENODEV ; + } + + revision =i2c_smbus_read_byte_data (client,REG_REVISION ); + +dev_info (dev,"Found %s at 0x%02x (rev 0x%02x).\n" , + info->type, client->addr, revision); + +return 0 ; +} +
drivers/hwmon/emc2101.c line 2152
Quoted 10 lines
drivers/hwmon/emc2101.c new file +10
-0+static const struct i2c_device_id emc2101_ids[] = { + {"emc2101" }, + {"emc2101-r" }, + { } +}; +MODULE_DEVICE_TABLE (i2c, emc2101_ids); + +static const struct of_device_id emc2101_of_match_table[] = { + { .compatible ="microchip,emc2101" , }, + { .compatible ="microchip,emc2101-r" , },
Devices are compatible then? Express it in the bindings and drop this entry.
drivers/hwmon/emc2101.c line 2160
Quoted 8 lines
drivers/hwmon/emc2101.c new file +8
-0+ { }, +}; +MODULE_DEVICE_TABLE (of, emc2101_of_match_table); + +static const unsigned short emc2101_address_list[] = { +0x4c ,I2C_CLIENT_END +}; +
Best regards,
Krzysztof
Re: [PATCH] drivers: hwmon: add EMC2101 driver
Hi Krzysztof,
Quoted 257 lines · Krzysztof Kozlowski
Quoted 255 lines · Álvaro Fernández Rojas
The Microchip EMC2101 is a SMBus 2.0 fan controller with temperature monitoring.
It supports up to 1 fan, 1 internal temperature sensor, 1 external temperature sensor and an 8 entry look up table to create a programmable temperature response.Signed-off-by Álvaro Fernández Rojas <[email protected]>--- drivers/hwmon/Kconfig | 10 + drivers/hwmon/Makefile | 1 + drivers/hwmon/emc2101.c | 2175 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 2186 insertions(+) create mode 100644 drivers/hwmon/emc2101.cv2: multiple improvements:
- RemoveFAN_RPM_MINdefinition.
- RenameFAN_FALSE_READtoFAN_MIN_READ.
-pwm_auto_point_temp_hyst_store(): simplify function.
-emc2101_fan_min_read(): add missingFAN_MIN_READcondition.
-emc2101_fan_min_write(): fixtach_countcalculation.
-emc2101_init(): fixREG_TACH_MINvalue.drivers/hwmon/Kconfig +10
-0@@ -2002,6 +2002,16 @@ config SENSORS_EMC1403 Threshold values can be configured using sysfs. Data from the different diodes are accessible via sysfs. +config SENSORS_EMC2101 + tristate "SMSC EMC2101" + depends on I2C + help + If you say yes here you get support for the SMSC EMC2101 + fan controller chips. + + This driver can also be built as a module. If so, the module + will be called emc2101. + config SENSORS_EMC2103 tristate "SMSC EMC2103" depends on I2Cdrivers/hwmon/Makefile +1
-0@@ -73,6 +73,7 @@ obj-$(CONFIG_SENSORS_DRIVETEMP) += drivetemp.o obj-$(CONFIG_SENSORS_DS620) += ds620.o obj-$(CONFIG_SENSORS_DS1621) += ds1621.o obj-$(CONFIG_SENSORS_EMC1403) += emc1403.o +obj-$(CONFIG_SENSORS_EMC2101) += emc2101.o obj-$(CONFIG_SENSORS_EMC2103) += emc2103.o obj-$(CONFIG_SENSORS_EMC2305) += emc2305.o obj-$(CONFIG_SENSORS_EMC6W201) += emc6w201.odrivers/hwmon/emc2101.c new file +194
-0@@ -0,0 +1,2176 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* +* Driver for Microchip EMC2101 fan controller. +* +* Copyright 2025 Álvaro Fernández Rojas <[email protected]> +*/ + +#include <linux/err.h> +#include <linux/hwmon.h> +#include <linux/hwmon-sysfs.h> +#include <linux/i2c.h> +#include <linux/init.h> +#include <linux/jiffies.h> +#include <linux/module.h> +#include <linux/mutex.h> +#include <linux/slab.h> +#include <linux/util_macros.h> + +#define REG_TEMP_INT 0x00 +#define REG_TEMP_EXT_HI 0x01 +#define REG_STATUS 0x02 +#define ADC_BUSY BIT(7) +#define TEMP_INT_HIGH BIT(6) +#define EEPROM_ERROR BIT(5) +#define TEMP_EXT_HIGH BIT(4) +#define TEMP_EXT_LOW BIT(3) +#define TEMP_EXT_FAULT BIT(2) +#define TEMP_EXT_CRIT BIT(1) +#define TACH_LOW BIT(0) +#define REG_CONFIG 0x03 +#define ALERT_IRQ_ACK BIT(7) +#define FAN_STANDBY_ENABLE BIT(6) +#define FAN_STANDBY_MODE BIT(5) +#define FAN_MODE_DAC BIT(4) +#define SMBUS_TOUT_DISABLE BIT(3) +#define PIN_FUNC_TACH BIT(2) +#define TEMP_EXT_CRIT_UNLOCK BIT(1) +#define PIN_ASSERT_3_EXC BIT(0) +#define REG_CONV_RATE 0x04 +#define CONV_RATE_MASK 0xf +#define REG_TEMP_INT_MAX 0x05 +#define REG_TEMP_EXT_MAX_HI 0x07 +#define REG_TEMP_EXT_MIN_HI 0x08 +#define REG_TEMP_EXT_FORCE 0x0c +#define REG_ONE_SHOT 0x0f +#define REG_TEMP_EXT_LO 0x10 +#define REG_SCRATCHPAD_1 0x11 +#define REG_SCRATCHPAD_2 0x12 +#define REG_TEMP_EXT_MAX_LO 0x13 +#define REG_TEMP_EXT_MIN_LO 0x14 +#define REG_ALERT_MASK 0x16 +#define IRQ_TEMP_INT_MAX_DISABLE BIT(6) +#define IRQ_TEMP_EXT_MAX_DISABLE BIT(4) +#define IRQ_TEMP_EXT_MIN_DISABLE BIT(3) +#define IRQ_TEMP_EXT_CRIT_DISABLE BIT(1) +#define IRQ_TACH_MIN_DISABLE BIT(0) +#define REG_EXT_IDEALITY 0x17 +#define EXT_IDEALITY_START 9846 +#define EXT_IDEALITY_STEP 13 +#define EXT_IDEALITY_VAL (x) (EXT_IDEALITY_START + \ + ((x) * EXT_IDEALITY_STEP)) +#define EXT_IDEALITY_MASK 0x3f +#define REG_BETA_COMP 0x18 +#define BETA_COMP_AUTO BIT(3) +#define BETA_COMP_DISABLE 7 +#define BETA_COMP_2_33 6 +#define BETA_COMP_1_00 5 +#define BETA_COMP_0_43 4 +#define BETA_COMP_0_33 3 +#define BETA_COMP_0_25 2 +#define BETA_COMP_0_18 1 +#define BETA_COMP_0_11 0 +#define BETA_COMP_MASK 0x7 +#define REG_TEMP_EXT_CRIT 0x19 +#define REG_TEMP_EXT_CRIT_HYST 0x21 +#define REG_TACH_LO 0x46 +#define REG_TACH_HI 0x47 +#define REG_TACH_MIN_LO 0x48 +#define REG_TACH_MIN_HI 0x49 +#define REG_FAN_CONFIG 0x4a +#define FAN_EXT_TEMP_FORCE BIT(6) +#define FAN_LUT_DISABLE BIT(5) +#define FAN_POL_INV BIT(4) +#define FAN_CLK_SEL BIT(3) +#define FAN_CLK_OVR BIT(2) +#define TACH_FALSE_READ_DISABLE BIT(0) +#define TACH_FALSE_READ_MASK 0x3 +#define REG_FAN_SPIN 0x4b +#define FAN_SPIN_UP_ABORT BIT(5) +#define FAN_SPIN_UP_POWER_SHIFT 3 +#define FAN_SPIN_UP_POWER_100 (3 << FAN_SPIN_UP_POWER_SHIFT) +#define FAN_SPIN_UP_POWER_75 (2 << FAN_SPIN_UP_POWER_SHIFT) +#define FAN_SPIN_UP_POWER_50 (1 << FAN_SPIN_UP_POWER_SHIFT) +#define FAN_SPIN_UP_POWER_0 (0 << FAN_SPIN_UP_POWER_SHIFT) +#define FAN_SPIN_UP_POWER_MASK (0x3 << FAN_SPIN_UP_POWER_SHIFT) +#define FAN_SPIN_UP_TIME_3200 7 +#define FAN_SPIN_UP_TIME_1600 6 +#define FAN_SPIN_UP_TIME_800 5 +#define FAN_SPIN_UP_TIME_400 4 +#define FAN_SPIN_UP_TIME_200 3 +#define FAN_SPIN_UP_TIME_100 2 +#define FAN_SPIN_UP_TIME_50 1 +#define FAN_SPIN_UP_TIME_0 0 +#define FAN_SPIN_UP_TIME_MASK 0x7 +#define REG_FAN_SET 0x4c +#define FAN_SET_MASK 0x3f +#define REG_PWM_FREQ 0x4d +#define PWM_FREQ_MASK 0x1f +#define REG_PWM_FREQ_DIV 0x4e +#define REG_FAN_LUT_HYST 0x4f +#define FAN_LUT_HYST_MASK 0x1f +#define REG_FAN_LUT_TEMP (x) (0x50 + (0x2 * (x))) +/* Write only with FAN_LUT_DISABLE */ +#define FAN_LUT_TEMP_MASK 0x7f +#define REG_FAN_LUT_SPEED (x) (0x51 + (0x2 * (x))) +/* Write only with FAN_LUT_DISABLE */ +#define FAN_LUT_SPEED_MASK 0x3f +#define REG_AVG_FILTER 0xbf +#define FILTER_SHIFT 1 +#define FILTER_L2 (3 << FILTER_SHIFT) +#define FILTER_L1 (1 << FILTER_SHIFT) +#define FILTER_NONE (0 << FILTER_SHIFT) +#define FILTER_MASK (0x3 << FILTER_SHIFT) +#define ALERT_PIN_TEMP_COMP BIT(0) +#define REG_PRODUCT_ID 0xfd +#define REG_MANUFACTURER_ID 0xfe +#define REG_REVISION 0xff + +#define CLK_FREQ_ALT 1400 +#define CLK_FREQ_BASE 360000 + +#define FAN_LUT_COUNT 8 +#define FAN_LUT_HYST_DEF 4 +#define FAN_LUT_HYST_MIN 0 +#define FAN_LUT_HYST_MAX 31 +#define FAN_MIN_READ 0xffff +#define FAN_RPM_FACTOR 5400000 + +#define MANUFACTURER_ID 0x5d + +#define TEMP_EXT_HI_FAULT 0x7f +#define TEMP_EXT_LO_FAULT_OPEN 0x00 +#define TEMP_EXT_LO_FAULT_SHORT 0xe0 + +#define TEMP_LO_FRAC 125 +#define TEMP_LO_SHIFT 5 +#define TEMP_LO_MASK (0x3 << TEMP_LO_SHIFT) + +#define TEMP_MIN -64 +#define TEMP_MAX 127 +#define TEMP_MAX_FRAC 750 + +enum emc2101_fan_spin_up_abort { +EMC2101_FAN_SPIN_ABORT_DISABLE =0 , +EMC2101_FAN_SPIN_ABORT_ENABLE +}; + +enum emc2101_fan_standby { +EMC2101_FAN_STBY_DISABLE =0 , +EMC2101_FAN_STBY_ENABLE +}; + +enum emc2101_mode { +EMC2101_MODE_PWM =0 , +EMC2101_MODE_DAC +}; + +enum ecm2101_product_id { +EMC2101 =0x16 , +EMC2101_R =0x28 +}; + +enum emc2101_pwm_enable { +EMC2101_PWM_MANUAL =1 , +EMC2101_PWM_LUT =2 +}; + +enum emc2101_pwm_polarity { +EMC2101_POL_NORMAL =0 , +EMC2101_POL_INVERTED +}; + +enum emc2101_temp_channels { +EMC2101_TC_INT =0 , +EMC2101_TC_EXT , +EMC2101_TC_FORCE , +EMC2101_TC_NUM +}; + +enum emc2101_temp_diode { +EMC2101_TD_CPU =1 , +EMC2101_TD_2N3904 =2 +}; +
Quoted 8 lines
drivers/hwmon/emc2101.c line 198
Quoted 4 lines
drivers/hwmon/emc2101.c new file +4
-0+struct emc2101_data { +struct i2c_client *client; +struct device *dev; +struct mutex mutex;Add a comment describing what you are protecting here. It looks so far like you could just use regmap and drop the mutex, but I didn't check thoroughly.
The EMC2101 datasheet is explicit about the Look Up Table registers (REG_FAN_LUT_TEMP and REG_FAN_LUT_SPEED) being RO if FAN_LUT_DISABLE isn't set, so I believe that we need the mutex even if we switch to regmap.
I will add an explanation with that in the next version. Should I still switch the implementation to regmap considering that we need a mutex?
Quoted 100 lines
Quoted 2 lines
drivers/hwmon/emc2101.c new file +1
-0+};...
drivers/hwmon/emc2101.c line 2091
Quoted 12 lines
drivers/hwmon/emc2101.c new file +12
-0+ +static int emc2101_probe (struct i2c_client *client) +{ +struct i2c_adapter *adapter = client->adapter; +struct device *dev = &client->dev; +struct emc2101_data *data; +struct device *hwmon_dev; + +if (!i2c_check_functionality (adapter,I2C_FUNC_SMBUS_BYTE_DATA )) +return -EIO ; + + data =devm_kzalloc (dev,sizeof (struct emc2101_data ),GFP_KERNEL );
sizeof(*)Quoted 4 lines
drivers/hwmon/emc2101.c new file +4
-0+if (!data) +return -ENOMEM ; + + data->client = client;drivers/hwmon/emc2101.c line 2105
Quoted 10 lines
drivers/hwmon/emc2101.c new file +10
-0+ data->dev = dev; +mutex_init (&data->mutex); + + hwmon_dev =devm_hwmon_device_register_with_info (dev, client->name, data, + &emc2101_chip_info, + emc2101_hwmon_groups); +if (IS_ERR (hwmon_dev)) +return PTR_ERR (hwmon_dev); + +dev_info (dev,"%s: sensor '%s'\n" ,dev_name (hwmon_dev), client->name);Drivers should be silent oon success. We already know that this probed based on sysfs.
Quoted 37 lines
drivers/hwmon/emc2101.c new file +37
-0+ +return emc2101_init (data); +} + +static int emc2101_detect (struct i2c_client *client,struct i2c_board_info *info) +{ +struct i2c_adapter *adapter = client->adapter; +s32 manufacturer, product, revision; +struct device *dev = &adapter->dev; + +if (!i2c_check_functionality (adapter,I2C_FUNC_SMBUS_BYTE_DATA )) +return -ENODEV ; + + manufacturer =i2c_smbus_read_byte_data (client,REG_MANUFACTURER_ID ); +if (manufacturer !=MANUFACTURER_ID ) +return -ENODEV ; + + product =i2c_smbus_read_byte_data (client,REG_PRODUCT_ID ); +switch (product) { +case EMC2101 : +strscpy (info->type,"emc2101" ,I2C_NAME_SIZE ); +break ; +case EMC2101_R : +strscpy (info->type,"emc2101-r" ,I2C_NAME_SIZE ); +break ; +default : +return -ENODEV ; + } + + revision =i2c_smbus_read_byte_data (client,REG_REVISION ); + +dev_info (dev,"Found %s at 0x%02x (rev 0x%02x).\n" , + info->type, client->addr, revision); + +return 0 ; +} +drivers/hwmon/emc2101.c line 2152
Quoted 10 lines
drivers/hwmon/emc2101.c new file +10
-0+static const struct i2c_device_id emc2101_ids[] = { + {"emc2101" }, + {"emc2101-r" }, + { } +}; +MODULE_DEVICE_TABLE (i2c, emc2101_ids); + +static const struct of_device_id emc2101_of_match_table[] = { + { .compatible ="microchip,emc2101" , }, + { .compatible ="microchip,emc2101-r" , },Devices are compatible then? Express it in the bindings and drop this entry.
drivers/hwmon/emc2101.c line 2160
Quoted 8 lines
drivers/hwmon/emc2101.c new file +8
-0+ { }, +}; +MODULE_DEVICE_TABLE (of, emc2101_of_match_table); + +static const unsigned short emc2101_address_list[] = { +0x4c ,I2C_CLIENT_END +}; +Best regards,
Krzysztof
Guenter Roeck
maintainerRe: [PATCH] drivers: hwmon: add EMC2101 driver
Quoted 5 lines · Álvaro Fernández Rojas
Quoted 4 lines
Add a comment describing what you are protecting here. It looks so far like you could just use regmap and drop the mutex, but I didn't check thoroughly.
Quoted 8 lines
The EMC2101 datasheet is explicit about the Look Up Table registers (
REG_FAN_LUT_TEMPandREG_FAN_LUT_SPEED) being RO ifFAN_LUT_DISABLEisn't set, so I believe that we need the mutex even if we switch to regmap.
I will add an explanation with that in the next version. Should I still switch the implementation to regmap considering that we need a mutex?
What does r/w vs. r/o register access have to do with locking ?
Guenter
Re: [PATCH] drivers: hwmon: add EMC2101 driver
Hi Guenter,
Quoted 15 lines · Guenter Roeck
Quoted 5 lines · Álvaro Fernández Rojas
Quoted 4 lines
Add a comment describing what you are protecting here. It looks so far like you could just use regmap and drop the mutex, but I didn't check thoroughly.
Quoted 8 lines
The EMC2101 datasheet is explicit about the Look Up Table registers (
REG_FAN_LUT_TEMPandREG_FAN_LUT_SPEED) being RO ifFAN_LUT_DISABLEisn't set, so I believe that we need the mutex even if we switch to regmap.
I will add an explanation with that in the next version. Should I still switch the implementation to regmap considering that we need a mutex?
Quoted 1 line
What does r/w vs. r/o register access have to do with locking ?
FAN_LUT_DISABLE bit can be set or cleared when pwm_enable is modified, but we need it to be set in order to be able to modify REG_FAN_LUT_TEMP and REG_FAN_LUT_SPEED registers.
If FAN_LUT_DISABLE bit isn't set, any attempt to modify REG_FAN_LUT_TEMP and REG_FAN_LUT_SPEED registers won't work because they would be RO.
Therefore, we need a lock to ensure that FAN_LUT_DISABLE is kept while modifying those registers.
Please, take a look at emc2101_pwm_enable_write() and __pwm_auto_point_temp_store().
Quoted 2 lines
Guenter
Best regards,
Álvaro.
Guenter Roeck
maintainerRe: [PATCH] drivers: hwmon: add EMC2101 driver
Quoted 20 lines · Álvaro Fernández Rojas
Hi Guenter,
Quoted 15 lines · Guenter Roeck
Quoted 5 lines · Álvaro Fernández Rojas
Quoted 4 lines
Add a comment describing what you are protecting here. It looks so far like you could just use regmap and drop the mutex, but I didn't check thoroughly.
Quoted 8 lines
The EMC2101 datasheet is explicit about the Look Up Table registers (
REG_FAN_LUT_TEMPandREG_FAN_LUT_SPEED) being RO ifFAN_LUT_DISABLEisn't set, so I believe that we need the mutex even if we switch to regmap.
I will add an explanation with that in the next version. Should I still switch the implementation to regmap considering that we need a mutex?Quoted 1 line
What does r/w vs. r/o register access have to do with locking ?
Quoted 11 lines
FAN_LUT_DISABLEbit can be set or cleared whenpwm_enableis modified, but we need it to be set in order to be able to modifyREG_FAN_LUT_TEMPandREG_FAN_LUT_SPEEDregisters.
IfFAN_LUT_DISABLEbit isn't set, any attempt to modifyREG_FAN_LUT_TEMPandREG_FAN_LUT_SPEEDregisters won't work because they would be RO.
Therefore, we need a lock to ensure thatFAN_LUT_DISABLEis kept while modifying those registers.
Please, take a look atemc2101_pwm_enable_write()and__pwm_auto_point_temp_store().
Thanks for the clarification. Please add that as comment into the code.
Regmap is still valuable even if local locking is needed. It provides caching for non-volatile registers, and it provides various useful API functions (for example for bit value changes) which you otherwise have to implement by hand.
Guenter