Skip to contentSkip to conversation

hwmon: (mlxreg-fan) Add support for fan capability registers

PATCH Superseded Vadim Pasternak 2 participants 4 messages Mar 18, 2019 Hardware monitoring

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hwmon: (mlxreg-fan) Add support for fan capability registers

Add support for fan capability registers in order to distinct between the systems which have minor fan configuration differences. This reduces the amount of code used to describe such systems. The capability registers provides system specific information about the number of physically connected tachometers and system specific fan speed scale parameter.
For example one system can be equipped with twelve fan tachometers, while the other with for example, eight or six. Or one system should use default fan speed divider value, while the other has a scale parameter defined in hardware, which should be used for divider setting.
Reading this information from the capability registers allows to use the same fan structure for the systems with the such differences.

---
 drivers/hwmon/mlxreg-fan.c | 78 +++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 73 insertions(+), 5 deletions(-)
drivers/hwmon/mlxreg-fan.c +73 -5
index db8c6de0b6a0..5a4d5348516a 100644
@@ -27,7 +27,10 @@
 #define MLXREG_FAN_SPEED_MAX			(MLXREG_FAN_MAX_STATE * 2)
 #define MLXREG_FAN_SPEED_MIN_LEVEL		2	/* 20 percent */
 #define MLXREG_FAN_TACHO_SAMPLES_PER_PULSE_DEF	44
-#define MLXREG_FAN_TACHO_DIVIDER_DEF		1132
+#define MLXREG_FAN_TACHO_DIVIDER_MIN		283
+#define MLXREG_FAN_TACHO_DIVIDER_DEF		(MLXREG_FAN_TACHO_DIVIDER_MIN \
+						 * 4)
+#define MLXREG_FAN_TACHO_DIVIDER_SCALE_MAX	64
 /*
  * FAN datasheet defines the formula for RPM calculations as RPM = 15/t-high.
  * The logic in a programmable device measures the time t-high by sampling the
@@ -360,12 +363,57 @@ static const struct thermal_cooling_device_ops mlxreg_fan_cooling_ops = {
 	.set_cur_state	= mlxreg_fan_set_cur_state,
 };
 
+static int mlxreg_fan_connect_verify(struct mlxreg_fan *fan,
+				     struct mlxreg_core_data *data,
+				     bool *connected)
+{
+	u32 regval;
+	int err;
+
+	err = regmap_read(fan->regmap, data->capability, &regval);
+	if (err) {
+		dev_err(fan->dev, "Failed to query capability register 0x%08x\n",
+			data->capability);
+		return err;
+	}
+
+	*connected = (regval & data->bit) ? true : false;
+
+	return 0;
+}
+
+static int mlxreg_fan_speed_divider_get(struct mlxreg_fan *fan,
+					struct mlxreg_core_data *data)
+{
+	u32 regval;
+	int err;
+
+	err = regmap_read(fan->regmap, data->capability, &regval);
+	if (err) {
+		dev_err(fan->dev, "Failed to query capability register 0x%08x\n",
+			data->capability);
+		return err;
+	}
+
+	/*
+	 * Set divider value according to the capability register, in case it
+	 * contains valid value. Otherwise use default value. The purpose of
+	 * this validation is to protect against the old hardware, in which
+	 * this register can be un-initialized.
+	 */
+	if (regval > 0 && regval <= MLXREG_FAN_TACHO_DIVIDER_SCALE_MAX)
+		fan->divider = regval * MLXREG_FAN_TACHO_DIVIDER_MIN;
+
+	return 0;
+}
+
 static int mlxreg_fan_config(struct mlxreg_fan *fan,
 			     struct mlxreg_core_platform_data *pdata)
 {
 	struct mlxreg_core_data *data = pdata->data;
-	bool configured = false;
+	bool configured = false, connected = false;
 	int tacho_num = 0, i;
+	int err;
 
 	fan->samples = MLXREG_FAN_TACHO_SAMPLES_PER_PULSE_DEF;
 	fan->divider = MLXREG_FAN_TACHO_DIVIDER_DEF;
@@ -376,6 +424,18 @@ static int mlxreg_fan_config(struct mlxreg_fan *fan,
 					data->label);
 				return -EINVAL;
 			}
+
+			if (data->capability) {
+				err = mlxreg_fan_connect_verify(fan, data,
+								&connected);
+				if (err)
+					return err;
+				if (!connected) {
+					tacho_num++;
+					continue;
+				}
+			}
+
 			fan->tacho[tacho_num].reg = data->reg;
 			fan->tacho[tacho_num].mask = data->mask;
 			fan->tacho[tacho_num++].connected = true;
@@ -394,13 +454,21 @@ static int mlxreg_fan_config(struct mlxreg_fan *fan,
 				return -EINVAL;
 			}
 			/* Validate that conf parameters are not zeros. */
-			if (!data->mask || !data->bit) {
+			if (!data->mask && !data->bit && !data->capability) {
 				dev_err(fan->dev, "invalid conf entry params: %s\n",
 					data->label);
 				return -EINVAL;
 			}
-			fan->samples = data->mask;
-			fan->divider = data->bit;
+			if (data->capability) {
+				err = mlxreg_fan_speed_divider_get(fan, data);
+				if (err)
+					return err;
+			} else {
+				if (data->mask)
+					fan->samples = data->mask;
+				if (data->bit)
+					fan->divider = data->bit;
+			}
 			configured = true;
 		} else {
 			dev_err(fan->dev, "invalid label: %s\n", data->label);
Signature · 1 line
2.11.0
maintainer
#1 lore raw
Quoted 24 lines · Vadim Pasternak

Add support for fan capability registers in order to distinct between the systems which have minor fan configuration differences. This reduces the amount of code used to describe such systems. The capability registers provides system specific information about the number of physically connected tachometers and system specific fan speed scale parameter.
For example one system can be equipped with twelve fan tachometers, while the other with for example, eight or six. Or one system should use default fan speed divider value, while the other has a scale parameter defined in hardware, which should be used for divider setting.
Reading this information from the capability registers allows to use the same fan structure for the systems with the such differences.

---
  drivers/hwmon/mlxreg-fan.c | 78 +++++++++++++++++++++++++++++++++++++++++++---
  1 file changed, 73 insertions(+), 5 deletions(-)
drivers/hwmon/mlxreg-fan.c +0 -0
index db8c6de0b6a0..5a4d5348516a 100644

drivers/hwmon/mlxreg-fan.c line 32

Quoted 7 lines
drivers/hwmon/mlxreg-fan.c +3 -1
@@ -27,7 +27,10 @@
  #define MLXREG_FAN_SPEED_MAX			(MLXREG_FAN_MAX_STATE * 2)
  #define MLXREG_FAN_SPEED_MIN_LEVEL		2	/* 20 percent */
  #define MLXREG_FAN_TACHO_SAMPLES_PER_PULSE_DEF	44
-#define MLXREG_FAN_TACHO_DIVIDER_DEF		1132
+#define MLXREG_FAN_TACHO_DIVIDER_MIN		283
+#define MLXREG_FAN_TACHO_DIVIDER_DEF		(MLXREG_FAN_TACHO_DIVIDER_MIN \
+						 * 4)

This is where the unnecessary MLXREG_FAN prefix really starts to hurt.

Quoted 16 lines
drivers/hwmon/mlxreg-fan.c +9 -0
+#define MLXREG_FAN_TACHO_DIVIDER_SCALE_MAX	64
  /*
   * FAN datasheet defines the formula for RPM calculations as RPM = 15/t-high.
   * The logic in a programmable device measures the time t-high by sampling the
@@ -360,12 +363,57 @@ static const struct thermal_cooling_device_ops mlxreg_fan_cooling_ops = {
  	.set_cur_state	= mlxreg_fan_set_cur_state,
  };
 
+static int mlxreg_fan_connect_verify(struct mlxreg_fan *fan,
+				     struct mlxreg_core_data *data,
+				     bool *connected)
+{
+	u32 regval;
+	int err;
+
+	err = regmap_read(fan->regmap, data->capability, &regval);

drivers/hwmon/mlxreg-fan.c static const struct thermal_cooling_device_ops mlxreg_fan_cooling_ops = { · line 383

Quoted 10 lines
drivers/hwmon/mlxreg-fan.c +10 -0
+	if (err) {
+		dev_err(fan->dev, "Failed to query capability register 0x%08x\n",
+			data->capability);
+		return err;
+	}
+
+	*connected = (regval & data->bit) ? true : false;
+
+	return 0;
+}

This function could be simplified by returning an int, negative for error or 0/1.

	return !!(regval & data->bit);
Even if not,
	*connected = regval & data->bit;
would do or, if you want to be explicit,
	*connected = !!(regval & data->bit);
Quoted 9 lines
drivers/hwmon/mlxreg-fan.c +9 -0
+
+static int mlxreg_fan_speed_divider_get(struct mlxreg_fan *fan,
+					struct mlxreg_core_data *data)
+{
+	u32 regval;
+	int err;
+
+	err = regmap_read(fan->regmap, data->capability, &regval);
+	if (err) {

drivers/hwmon/mlxreg-fan.c static const struct thermal_cooling_device_ops mlxreg_fan_cooling_ops = { · line 402

Quoted 10 lines
drivers/hwmon/mlxreg-fan.c +10 -0
+		dev_err(fan->dev, "Failed to query capability register 0x%08x\n",
+			data->capability);
+		return err;
+	}
+
+	/*
+	 * Set divider value according to the capability register, in case it
+	 * contains valid value. Otherwise use default value. The purpose of
+	 * this validation is to protect against the old hardware, in which
+	 * this register can be un-initialized.

un-initialized -> random ? "can return zero" might be a better description if that is what it is.

Quoted 62 lines
drivers/hwmon/mlxreg-fan.c +32 -3
+	 */
+	if (regval > 0 && regval <= MLXREG_FAN_TACHO_DIVIDER_SCALE_MAX)
+		fan->divider = regval * MLXREG_FAN_TACHO_DIVIDER_MIN;
+
+	return 0;
+}
+
  static int mlxreg_fan_config(struct mlxreg_fan *fan,
  			     struct mlxreg_core_platform_data *pdata)
  {
  	struct mlxreg_core_data *data = pdata->data;
-	bool configured = false;
+	bool configured = false, connected = false;
  	int tacho_num = 0, i;
+	int err;
 
  	fan->samples = MLXREG_FAN_TACHO_SAMPLES_PER_PULSE_DEF;
  	fan->divider = MLXREG_FAN_TACHO_DIVIDER_DEF;
@@ -376,6 +424,18 @@ static int mlxreg_fan_config(struct mlxreg_fan *fan,
  					data->label);
  				return -EINVAL;
  			}
+
+			if (data->capability) {
+				err = mlxreg_fan_connect_verify(fan, data,
+								&connected);
+				if (err)
+					return err;
+				if (!connected) {
+					tacho_num++;
+					continue;
+				}
+			}
+
  			fan->tacho[tacho_num].reg = data->reg;
  			fan->tacho[tacho_num].mask = data->mask;
  			fan->tacho[tacho_num++].connected = true;
@@ -394,13 +454,21 @@ static int mlxreg_fan_config(struct mlxreg_fan *fan,
  				return -EINVAL;
  			}
  			/* Validate that conf parameters are not zeros. */
-			if (!data->mask || !data->bit) {
+			if (!data->mask && !data->bit && !data->capability) {
  				dev_err(fan->dev, "invalid conf entry params: %s\n",
  					data->label);
  				return -EINVAL;
  			} > -			fan->samples = data->mask;
-			fan->divider = data->bit;
+			if (data->capability) {
+				err = mlxreg_fan_speed_divider_get(fan, data);
+				if (err)
+					return err;
+			} else {
+				if (data->mask)
+					fan->samples = data->mask;
+				if (data->bit)
+					fan->divider = data->bit;
+			}
  			configured = true;
  		} else {
  			dev_err(fan->dev, "invalid label: %s\n", data->label);
#2 lore raw
Quoted 40 lines

-----Original Message-----
From: Guenter Roeck <[email protected]> On Behalf Of Guenter Roeck
Sent: Monday, March 18, 2019 4:18 PM
To: Vadim Pasternak <[email protected]>
Cc: [email protected]
Subject: Re: [PATCH v1 hwmon-next] hwmon: (mlxreg-fan) Add support for fan capability registers

Quoted 14 lines · Vadim Pasternak

Add support for fan capability registers in order to distinct between the systems which have minor fan configuration differences. This reduces the amount of code used to describe such systems. The capability registers provides system specific information about the number of physically connected tachometers and system specific fan speed scale parameter.
For example one system can be equipped with twelve fan tachometers, while the other with for example, eight or six. Or one system should use default fan speed divider value, while the other has a scale parameter defined in hardware, which should be used for divider setting.
Reading this information from the capability registers allows to use the same fan structure for the systems with the such differences.

Quoted 3 lines
---
  drivers/hwmon/mlxreg-fan.c | 78

+++++++++++++++++++++++++++++++++++++++++++---

drivers/hwmon/mlxreg-fan.c line 27

Quoted 8 lines
  1 file changed, 73 insertions(+), 5 deletions(-)
drivers/hwmon/mlxreg-fan.c +0 -0
index db8c6de0b6a0..5a4d5348516a 100644
@@ -27,7 +27,10 @@
  #define MLXREG_FAN_SPEED_MAX
	(MLXREG_FAN_MAX_STATE * 2)

drivers/hwmon/mlxreg-fan.c line 28

Quoted 1 line
drivers/hwmon/mlxreg-fan.c +0 -0
  #define MLXREG_FAN_SPEED_MIN_LEVEL		2	/* 20 percent

*/

Quoted 8 lines

drivers/hwmon/mlxreg-fan.c

Quoted 4 lines
drivers/hwmon/mlxreg-fan.c +2 -1
  #define MLXREG_FAN_TACHO_SAMPLES_PER_PULSE_DEF	44
-#define MLXREG_FAN_TACHO_DIVIDER_DEF		1132
+#define MLXREG_FAN_TACHO_DIVIDER_MIN		283
+#define MLXREG_FAN_TACHO_DIVIDER_DEF
	(MLXREG_FAN_TACHO_DIVIDER_MIN \

drivers/hwmon/mlxreg-fan.c line 32

Quoted 1 line
drivers/hwmon/mlxreg-fan.c +1 -0
+						 * 4)

This is where the unnecessary MLXREG_FAN prefix really starts to hurt.

Hi Guenter,

Thank you for quick review.
I just wanted to keep all the names with the same prefix. Maybe it would be better to keep this convention and just make names shorter by replacing
s/DIVIDER/DIV ?

#define MLXREG_FAN_TACHO_DIV_MIN 283
#define MLXREG_FAN_TACHO_DIV_DEF (MLXREG_FAN_TACHO_DIV_MIN * 4)
#define MLXREG_FAN_TACHO_DIV_SCALE_MAX 64

Quoted 128 lines

drivers/hwmon/mlxreg-fan.c

Quoted 4 lines
drivers/hwmon/mlxreg-fan.c +1 -0
+#define MLXREG_FAN_TACHO_DIVIDER_SCALE_MAX	64
  /*
   * FAN datasheet defines the formula for RPM calculations as RPM = 15/t-

high.

Quoted 2 lines
drivers/hwmon/mlxreg-fan.c +0 -0
   * The logic in a programmable device measures the time t-high by

sampling the @@ -360,12 +363,57 @@ static const struct

thermal_cooling_device_ops mlxreg_fan_cooling_ops = {

drivers/hwmon/mlxreg-fan.c static const struct thermal_cooling_device_ops mlxreg_fan_cooling_ops = {

Quoted 13 lines
  	.set_cur_state	= mlxreg_fan_set_cur_state,
  };
drivers/hwmon/mlxreg-fan.c +10 -0
+static int mlxreg_fan_connect_verify(struct mlxreg_fan *fan,
+				     struct mlxreg_core_data *data,
+				     bool *connected)
+{
+	u32 regval;
+	int err;
+
+	err = regmap_read(fan->regmap, data->capability, &regval);
+	if (err) {
+		dev_err(fan->dev, "Failed to query capability register

0x%08x\n",

drivers/hwmon/mlxreg-fan.c static const struct thermal_cooling_device_ops mlxreg_fan_cooling_ops = { · line 383

Quoted 8 lines
drivers/hwmon/mlxreg-fan.c +8 -0
+			data->capability);
+		return err;
+	}
+
+	*connected = (regval & data->bit) ? true : false;
+
+	return 0;
+}

This function could be simplified by returning an int, negative for error or 0/1.

	return !!(regval & data->bit);
Even if not,
	*connected = regval & data->bit;
would do or, if you want to be explicit,
	*connected = !!(regval & data->bit);

drivers/hwmon/mlxreg-fan.c static const struct thermal_cooling_device_ops mlxreg_fan_cooling_ops = {

Quoted 10 lines
drivers/hwmon/mlxreg-fan.c +10 -0
+
+static int mlxreg_fan_speed_divider_get(struct mlxreg_fan *fan,
+					struct mlxreg_core_data *data)
+{
+	u32 regval;
+	int err;
+
+	err = regmap_read(fan->regmap, data->capability, &regval);
+	if (err) {
+		dev_err(fan->dev, "Failed to query capability register

0x%08x\n",

drivers/hwmon/mlxreg-fan.c static const struct thermal_cooling_device_ops mlxreg_fan_cooling_ops = { · line 402

Quoted 9 lines
drivers/hwmon/mlxreg-fan.c +9 -0
+			data->capability);
+		return err;
+	}
+
+	/*
+	 * Set divider value according to the capability register, in case it
+	 * contains valid value. Otherwise use default value. The purpose of
+	 * this validation is to protect against the old hardware, in which
+	 * this register can be un-initialized.

un-initialized -> random ? "can return zero" might be a better description if that is what it is.

Quoted 8 lines
drivers/hwmon/mlxreg-fan.c +7 -0
+	 */
+	if (regval > 0 && regval <= MLXREG_FAN_TACHO_DIVIDER_SCALE_MAX)
+		fan->divider = regval * MLXREG_FAN_TACHO_DIVIDER_MIN;
+
+	return 0;
+}
+
  static int mlxreg_fan_config(struct mlxreg_fan *fan,

drivers/hwmon/mlxreg-fan.c static const struct thermal_cooling_device_ops mlxreg_fan_cooling_ops = {

Quoted 10 lines
drivers/hwmon/mlxreg-fan.c +2 -1
  			     struct mlxreg_core_platform_data *pdata)
  {
  	struct mlxreg_core_data *data = pdata->data;
-	bool configured = false;
+	bool configured = false, connected = false;
  	int tacho_num = 0, i;
+	int err;
 
  	fan->samples = MLXREG_FAN_TACHO_SAMPLES_PER_PULSE_DEF;
  	fan->divider = MLXREG_FAN_TACHO_DIVIDER_DEF; @@ -376,6

+424,18 @@

Quoted 9 lines

static int mlxreg_fan_config(struct mlxreg_fan *fan,
data->label);
return -EINVAL;
}

drivers/hwmon/mlxreg-fan.c +5 -0
+
+			if (data->capability) {
+				err = mlxreg_fan_connect_verify(fan, data,
+								&connected);
+				if (err)

drivers/hwmon/mlxreg-fan.c static int mlxreg_fan_config(struct mlxreg_fan *fan,

Quoted 10 lines
drivers/hwmon/mlxreg-fan.c +7 -0
+					return err;
+				if (!connected) {
+					tacho_num++;
+					continue;
+				}
+			}
+
  			fan->tacho[tacho_num].reg = data->reg;
  			fan->tacho[tacho_num].mask = data->mask;
  			fan->tacho[tacho_num++].connected = true; @@ -

394,13 +454,21 @@

drivers/hwmon/mlxreg-fan.c static int mlxreg_fan_config(struct mlxreg_fan *fan,

Quoted 7 lines

static int mlxreg_fan_config(struct mlxreg_fan *fan,
return -EINVAL;
}
/* Validate that conf parameters are not zeros. */

drivers/hwmon/mlxreg-fan.c +1 -1
-			if (!data->mask || !data->bit) {
+			if (!data->mask && !data->bit && !data->capability) {
  				dev_err(fan->dev, "invalid conf entry params:

%s\n",

Quoted 18 lines
  					data->label);
  				return -EINVAL;
  			} > -			fan->samples = data->mask;
drivers/hwmon/mlxreg-fan.c +10 -1
-			fan->divider = data->bit;
+			if (data->capability) {
+				err = mlxreg_fan_speed_divider_get(fan, data);
+				if (err)
+					return err;
+			} else {
+				if (data->mask)
+					fan->samples = data->mask;
+				if (data->bit)
+					fan->divider = data->bit;
+			}
  			configured = true;
  		} else {
  			dev_err(fan->dev, "invalid label: %s\n", data->label);
maintainer
#3 lore raw
Quoted 57 lines · Vadim Pasternak
Quoted 40 lines

-----Original Message-----
From: Guenter Roeck <[email protected]> On Behalf Of Guenter Roeck
Sent: Monday, March 18, 2019 4:18 PM
To: Vadim Pasternak <[email protected]>
Cc: [email protected]
Subject: Re: [PATCH v1 hwmon-next] hwmon: (mlxreg-fan) Add support for fan capability registers

Quoted 14 lines · Vadim Pasternak

Add support for fan capability registers in order to distinct between the systems which have minor fan configuration differences. This reduces the amount of code used to describe such systems. The capability registers provides system specific information about the number of physically connected tachometers and system specific fan speed scale parameter.
For example one system can be equipped with twelve fan tachometers, while the other with for example, eight or six. Or one system should use default fan speed divider value, while the other has a scale parameter defined in hardware, which should be used for divider setting.
Reading this information from the capability registers allows to use the same fan structure for the systems with the such differences.

Quoted 3 lines
---
   drivers/hwmon/mlxreg-fan.c | 78

+++++++++++++++++++++++++++++++++++++++++++---

drivers/hwmon/mlxreg-fan.c line 27

Quoted 8 lines
   1 file changed, 73 insertions(+), 5 deletions(-)
drivers/hwmon/mlxreg-fan.c +0 -0
index db8c6de0b6a0..5a4d5348516a 100644
@@ -27,7 +27,10 @@
   #define MLXREG_FAN_SPEED_MAX
	(MLXREG_FAN_MAX_STATE * 2)

drivers/hwmon/mlxreg-fan.c line 28

Quoted 1 line
drivers/hwmon/mlxreg-fan.c +0 -0
   #define MLXREG_FAN_SPEED_MIN_LEVEL		2	/* 20 percent

*/

Quoted 8 lines

drivers/hwmon/mlxreg-fan.c

Quoted 4 lines
drivers/hwmon/mlxreg-fan.c +2 -1
   #define MLXREG_FAN_TACHO_SAMPLES_PER_PULSE_DEF	44
-#define MLXREG_FAN_TACHO_DIVIDER_DEF		1132
+#define MLXREG_FAN_TACHO_DIVIDER_MIN		283
+#define MLXREG_FAN_TACHO_DIVIDER_DEF
	(MLXREG_FAN_TACHO_DIVIDER_MIN \

drivers/hwmon/mlxreg-fan.c line 32

Quoted 1 line
drivers/hwmon/mlxreg-fan.c +1 -0
+						 * 4)

This is where the unnecessary MLXREG_FAN prefix really starts to hurt.

Hi Guenter,

Thank you for quick review.
I just wanted to keep all the names with the same prefix. Maybe it would be better to keep this convention and just make names shorter by replacing
s/DIVIDER/DIV ?

Quoted 4 lines

#define MLXREG_FAN_TACHO_DIV_MIN 283
#define MLXREG_FAN_TACHO_DIV_DEF (MLXREG_FAN_TACHO_DIV_MIN * 4)
#define MLXREG_FAN_TACHO_DIV_SCALE_MAX 64

Sure, that would work. Just something to consider for future drivers - a prefix of, say, MLX instead of MLXREG_FAN would have been sufficient. I am not suggesting to change that now, though.

Guenter

Quoted 129 lines
Quoted 128 lines

drivers/hwmon/mlxreg-fan.c

Quoted 4 lines
drivers/hwmon/mlxreg-fan.c +1 -0
+#define MLXREG_FAN_TACHO_DIVIDER_SCALE_MAX	64
   /*
    * FAN datasheet defines the formula for RPM calculations as RPM = 15/t-

high.

Quoted 2 lines
drivers/hwmon/mlxreg-fan.c +0 -0
    * The logic in a programmable device measures the time t-high by

sampling the @@ -360,12 +363,57 @@ static const struct

thermal_cooling_device_ops mlxreg_fan_cooling_ops = {

drivers/hwmon/mlxreg-fan.c static const struct thermal_cooling_device_ops mlxreg_fan_cooling_ops = {

Quoted 13 lines
   	.set_cur_state	= mlxreg_fan_set_cur_state,
   };
drivers/hwmon/mlxreg-fan.c +10 -0
+static int mlxreg_fan_connect_verify(struct mlxreg_fan *fan,
+				     struct mlxreg_core_data *data,
+				     bool *connected)
+{
+	u32 regval;
+	int err;
+
+	err = regmap_read(fan->regmap, data->capability, &regval);
+	if (err) {
+		dev_err(fan->dev, "Failed to query capability register

0x%08x\n",

drivers/hwmon/mlxreg-fan.c static const struct thermal_cooling_device_ops mlxreg_fan_cooling_ops = { · line 383

Quoted 8 lines
drivers/hwmon/mlxreg-fan.c +8 -0
+			data->capability);
+		return err;
+	}
+
+	*connected = (regval & data->bit) ? true : false;
+
+	return 0;
+}

This function could be simplified by returning an int, negative for error or 0/1.

	return !!(regval & data->bit);
Even if not,
	*connected = regval & data->bit;
would do or, if you want to be explicit,
	*connected = !!(regval & data->bit);

drivers/hwmon/mlxreg-fan.c static const struct thermal_cooling_device_ops mlxreg_fan_cooling_ops = {

Quoted 10 lines
drivers/hwmon/mlxreg-fan.c +10 -0
+
+static int mlxreg_fan_speed_divider_get(struct mlxreg_fan *fan,
+					struct mlxreg_core_data *data)
+{
+	u32 regval;
+	int err;
+
+	err = regmap_read(fan->regmap, data->capability, &regval);
+	if (err) {
+		dev_err(fan->dev, "Failed to query capability register

0x%08x\n",

drivers/hwmon/mlxreg-fan.c static const struct thermal_cooling_device_ops mlxreg_fan_cooling_ops = { · line 402

Quoted 9 lines
drivers/hwmon/mlxreg-fan.c +9 -0
+			data->capability);
+		return err;
+	}
+
+	/*
+	 * Set divider value according to the capability register, in case it
+	 * contains valid value. Otherwise use default value. The purpose of
+	 * this validation is to protect against the old hardware, in which
+	 * this register can be un-initialized.

un-initialized -> random ? "can return zero" might be a better description if that is what it is.

Quoted 8 lines
drivers/hwmon/mlxreg-fan.c +7 -0
+	 */
+	if (regval > 0 && regval <= MLXREG_FAN_TACHO_DIVIDER_SCALE_MAX)
+		fan->divider = regval * MLXREG_FAN_TACHO_DIVIDER_MIN;
+
+	return 0;
+}
+
   static int mlxreg_fan_config(struct mlxreg_fan *fan,

drivers/hwmon/mlxreg-fan.c static const struct thermal_cooling_device_ops mlxreg_fan_cooling_ops = {

Quoted 10 lines
drivers/hwmon/mlxreg-fan.c +2 -1
   			     struct mlxreg_core_platform_data *pdata)
   {
   	struct mlxreg_core_data *data = pdata->data;
-	bool configured = false;
+	bool configured = false, connected = false;
   	int tacho_num = 0, i;
+	int err;
 
   	fan->samples = MLXREG_FAN_TACHO_SAMPLES_PER_PULSE_DEF;
   	fan->divider = MLXREG_FAN_TACHO_DIVIDER_DEF; @@ -376,6

+424,18 @@

Quoted 9 lines

static int mlxreg_fan_config(struct mlxreg_fan *fan,

   					data->label);
   				return -EINVAL;
   			}
drivers/hwmon/mlxreg-fan.c +5 -0
+
+			if (data->capability) {
+				err = mlxreg_fan_connect_verify(fan, data,
+								&connected);
+				if (err)

drivers/hwmon/mlxreg-fan.c static int mlxreg_fan_config(struct mlxreg_fan *fan,

Quoted 10 lines
drivers/hwmon/mlxreg-fan.c +7 -0
+					return err;
+				if (!connected) {
+					tacho_num++;
+					continue;
+				}
+			}
+
   			fan->tacho[tacho_num].reg = data->reg;
   			fan->tacho[tacho_num].mask = data->mask;
   			fan->tacho[tacho_num++].connected = true; @@ -

394,13 +454,21 @@

drivers/hwmon/mlxreg-fan.c static int mlxreg_fan_config(struct mlxreg_fan *fan,

Quoted 7 lines

static int mlxreg_fan_config(struct mlxreg_fan *fan,

   				return -EINVAL;
   			}
   			/* Validate that conf parameters are not zeros. */
drivers/hwmon/mlxreg-fan.c +1 -1
-			if (!data->mask || !data->bit) {
+			if (!data->mask && !data->bit && !data->capability) {
   				dev_err(fan->dev, "invalid conf entry params:

%s\n",

Quoted 18 lines
   					data->label);
   				return -EINVAL;
   			} > -			fan->samples = data->mask;
drivers/hwmon/mlxreg-fan.c +10 -1
-			fan->divider = data->bit;
+			if (data->capability) {
+				err = mlxreg_fan_speed_divider_get(fan, data);
+				if (err)
+					return err;
+			} else {
+				if (data->mask)
+					fan->samples = data->mask;
+				if (data->bit)
+					fan->divider = data->bit;
+			}
   			configured = true;
   		} else {
   			dev_err(fan->dev, "invalid label: %s\n", data->label);