Skip to contentSkip to conversation

hwmon: (pmbus/max31785) refactor inter-access delay handling

PATCH Superseded Pradhan, Sanman 2 participants 7 messages Mar 21 Hardware monitoring

v5 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v5 cover hwmon: (pmbus/max31785) refactor inter-access delay handling

From: Sanman Pradhan <[email protected]>

This v5 series addresses the feedback on v4. The timestamp spinlock approach was dropped in favor of fixing the missing PMBus transaction serialization in regulator voltage paths.

Patch 1 adds the missing mutex_lock(&data->update_lock) to the PMBus regulator voltage operations get_voltage, set_voltage, and list_voltage. These paths perform multi-step PMBus transactions that must not be interleaved with concurrent PMBus users, since page selection and the subsequent register access operate on shared core state.

Patch 2 exports pmbus_wait() and pmbus_update_ts() so PMBus device drivers performing raw I2C transfers outside the core helpers can keep the PMBus delay bookkeeping in sync. PMBUS_OP_WRITE and PMBUS_OP_PAGE_CHANGE are moved to pmbus.h for use by device drivers.

Patch 3 refactors the max31785 fan controller driver to use the PMBus core access_delay mechanism instead of driver-local PMBus timing wrappers. The raw i2c_transfer() in max31785_read_long_data() is bracketed with pmbus_wait() and pmbus_update_ts() so the PMBus core timing state remains consistent.

Patch 4 fixes a pre-existing bug in max31785_read_long_data() where partial i2c_transfer() completion was treated as success. If only the write message completes, the read buffer is left uninitialized and may be interpreted as valid data.

v5:
- Dropped the spinlock patch entirely.
- Added a new patch to serialize the missing regulator voltage paths with update_lock.
- Updated the export patch to remove the spinlock-based timestamp handling.
- Kept the max31785 access_delay refactor unchanged from v4.
- Added a new patch to treat partial i2c_transfer() completion in max31785_read_long_data() as an error.

v4:
- Introduced a spinlock to protect 64-bit next_access_backoff accesses on 32-bit architectures (dropped in v5).
- Standardized all commits and signatures on [email protected].

v3:
- Dropped the u16 -> u8 wrapper type fix (already applied upstream).
- Fixed a comment style nit in max31785_read_long_data().

v2:
- Export pmbus_wait() and pmbus_update_ts() from the core.
- Use core timing helpers in max31785 to replace local boilerplate.

Sanman Pradhan (4):
hwmon: (pmbus) add missing mutex_lock in regulator ops
hwmon: (pmbus) export pmbus_wait and pmbus_update_ts
hwmon: (pmbus/max31785) use access_delay for PMBus-mediated accesses
hwmon: (pmbus/max31785) check for partial i2c_transfer in
read_long_data

 drivers/hwmon/pmbus/max31785.c   | 197 ++++++++++---------------------
 drivers/hwmon/pmbus/pmbus.h      |   6 +
 drivers/hwmon/pmbus/pmbus_core.c |  55 ++++++---
 3 files changed, 109 insertions(+), 149 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

PATCH v5 1/4 hwmon: (pmbus) add missing mutex_lock in regulator ops

From: Sanman Pradhan <[email protected]>

The regulator voltage operations pmbus_regulator_get_voltage(), pmbus_regulator_set_voltage(), and pmbus_regulator_list_voltage() call PMBus access helpers without holding update_lock. These helpers perform multi-step PMBus transactions involving shared core state such as page selection and transaction timing. Without serialization, a concurrent PMBus access can interleave with those operations and cause reads from or writes to the wrong rail.

For set_voltage(), this is particularly dangerous because the VOUT_COMMAND write could be directed to the wrong regulator output.

Add mutex_lock/unlock around the affected regulator voltage paths, following the pattern already used by other PMBus regulator operations such as _pmbus_regulator_on_off() and pmbus_regulator_get_status().

---
v5:
- New patch in the series. Adds the missing update_lock mutex to the three regulator voltage ops that were missing serialization.

---
 drivers/hwmon/pmbus/pmbus_core.c | 46 ++++++++++++++++++++++++--------
 1 file changed, 35 insertions(+), 11 deletions(-)
drivers/hwmon/pmbus/pmbus_core.c +35 -11
index 4d7634ee61484..3dad455448d05 100644
@@ -3181,7 +3181,9 @@ static int pmbus_regulator_get_voltage(struct regulator_dev *rdev)
 		.convert = true,
 	};
 
+	mutex_lock(&data->update_lock);
 	s.data = _pmbus_read_word_data(client, s.page, 0xff, PMBUS_READ_VOUT);
+	mutex_unlock(&data->update_lock);
 	if (s.data < 0)
 		return s.data;
 
@@ -3202,16 +3204,23 @@ static int pmbus_regulator_set_voltage(struct regulator_dev *rdev, int min_uv,
 	};
 	int val = DIV_ROUND_CLOSEST(min_uv, 1000); /* convert to mV */
 	int low, high;
+	int ret;
 
 	*selector = 0;
 
+	mutex_lock(&data->update_lock);
+
 	low = pmbus_regulator_get_low_margin(client, s.page);
-	if (low < 0)
-		return low;
+	if (low < 0) {
+		ret = low;
+		goto unlock;
+	}
 
 	high = pmbus_regulator_get_high_margin(client, s.page);
-	if (high < 0)
-		return high;
+	if (high < 0) {
+		ret = high;
+		goto unlock;
+	}
 
 	/* Make sure we are within margins */
 	if (low > val)
@@ -3221,7 +3230,11 @@ static int pmbus_regulator_set_voltage(struct regulator_dev *rdev, int min_uv,
 
 	val = pmbus_data2reg(data, &s, val);
 
-	return _pmbus_write_word_data(client, s.page, PMBUS_VOUT_COMMAND, (u16)val);
+	ret = _pmbus_write_word_data(client, s.page, PMBUS_VOUT_COMMAND, (u16)val);
+
+unlock:
+	mutex_unlock(&data->update_lock);
+	return ret;
 }
 
 static int pmbus_regulator_list_voltage(struct regulator_dev *rdev,
@@ -3231,6 +3244,7 @@ static int pmbus_regulator_list_voltage(struct regulator_dev *rdev,
 	struct i2c_client *client = to_i2c_client(dev->parent);
 	struct pmbus_data *data = i2c_get_clientdata(client);
 	int val, low, high;
+	int ret;
 
 	if (data->flags & PMBUS_VOUT_PROTECTED)
 		return 0;
@@ -3243,18 +3257,28 @@ static int pmbus_regulator_list_voltage(struct regulator_dev *rdev,
 	val = DIV_ROUND_CLOSEST(rdev->desc->min_uV +
 				(rdev->desc->uV_step * selector), 1000); /* convert to mV */
 
+	mutex_lock(&data->update_lock);
+
 	low = pmbus_regulator_get_low_margin(client, rdev_get_id(rdev));
-	if (low < 0)
-		return low;
+	if (low < 0) {
+		ret = low;
+		goto unlock;
+	}
 
 	high = pmbus_regulator_get_high_margin(client, rdev_get_id(rdev));
-	if (high < 0)
-		return high;
+	if (high < 0) {
+		ret = high;
+		goto unlock;
+	}
 
 	if (val >= low && val <= high)
-		return val * 1000; /* unit is uV */
+		ret = val * 1000; /* unit is uV */
+	else
+		ret = 0;
 
-	return 0;
+unlock:
+	mutex_unlock(&data->update_lock);
+	return ret;
 }
 
 const struct regulator_ops pmbus_regulator_ops = {
Signature · 1 line
2.34.1
maintainer
#2 lore raw

Re: [PATCH v5 1/4] hwmon: (pmbus) add missing mutex_lock in regulator ops

Quoted 31 lines · Sanman

From: Sanman Pradhan <[email protected]>

The regulator voltage operations pmbus_regulator_get_voltage(), pmbus_regulator_set_voltage(), and pmbus_regulator_list_voltage() call PMBus access helpers without holding update_lock. These helpers perform multi-step PMBus transactions involving shared core state such as page selection and transaction timing. Without serialization, a concurrent PMBus access can interleave with those operations and cause reads from or writes to the wrong rail.

For set_voltage(), this is particularly dangerous because the VOUT_COMMAND write could be directed to the wrong regulator output.

Add mutex_lock/unlock around the affected regulator voltage paths, following the pattern already used by other PMBus regulator operations such as _pmbus_regulator_on_off() and pmbus_regulator_get_status().

---
v5:
- New patch in the series. Adds the missing update_lock mutex to the three regulator voltage ops that were missing serialization.

---
  drivers/hwmon/pmbus/pmbus_core.c | 46 ++++++++++++++++++++++++--------
  1 file changed, 35 insertions(+), 11 deletions(-)
drivers/hwmon/pmbus/pmbus_core.c +0 -0
index 4d7634ee61484..3dad455448d05 100644

drivers/hwmon/pmbus/pmbus_core.c static int pmbus_regulator_get_voltage(struct regulator_dev *rdev) · line 3186

Quoted 6 lines
drivers/hwmon/pmbus/pmbus_core.c +2 -0
@@ -3181,7 +3181,9 @@ static int pmbus_regulator_get_voltage(struct regulator_dev *rdev)
  		.convert = true,
  	};
 
+	mutex_lock(&data->update_lock);
  	s.data = _pmbus_read_word_data(client, s.page, 0xff, PMBUS_READ_VOUT);
+	mutex_unlock(&data->update_lock);

I guess that would have been too easy. Here is the AI review feedback:

https://sashiko.dev/#/patchset/20260321005206.6350-1-sanman.pradhan%40hpe.com

Apparently this causes a lock recursion. We'll have to make sure that regulator_notifier_call_chain() is called outside the pmbus lock.

The only way I can imagine this to work would be with a worker. Instead of calling pmbus_regulator_notify() directly from pmbus_fault_handler(), we could store pending events in struct pmbus_data (one event per page) and trigger a worker. The worker would then walk through the event list and call pmbus_regulator_notify() outside the lock for each pending event.

Of course it isn't that simple because the list of events can be updated while the worker is running, but that should be solvable.

Either case, I am not sure if we want / need to handle this with the current patch series. The other patches look ok and should be ready to apply. WDYT ?

Thanks,
Guenter

#3 lore raw

Re: [PATCH v5 1/4] hwmon: (pmbus) add missing mutex_lock in regulator ops

From: Sanman Pradhan <[email protected]>

Thanks for the review, that makes sense.

Agreed, Patch 1 should be dropped from this series for now rather than trying to solve the notifier/locking interaction here.

I'll resend the remaining patches as a v6 3-patch series:
- export pmbus_wait() and pmbus_update_ts()
- max31785: use access_delay for PMBus-mediated accesses
- max31785: check for partial i2c_transfer() in read_long_data()

Thank you.

Regards,
Sanman Pradhan

#4 lore raw

PATCH v5 2/4 hwmon: (pmbus) export pmbus_wait and pmbus_update_ts

In reply to Pradhan, Sanman

From: Sanman Pradhan <[email protected]>

Export pmbus_wait() and pmbus_update_ts() so that PMBus device drivers which perform raw I2C transfers outside the core helpers can keep the PMBus core delay bookkeeping in sync.

Move PMBUS_OP_WRITE and PMBUS_OP_PAGE_CHANGE from pmbus_core.c to pmbus.h so device drivers can pass the correct operation type flags to pmbus_update_ts().

This is needed by the max31785 driver, which performs raw i2c_transfer() calls for its 4-byte extended fan speed reads that cannot use the standard PMBus word read path.

---
v5:
- Removed spinlock usage; pmbus_wait() and pmbus_update_ts() now rely solely on update_lock serialization.

v4:
- No changes to logic; standardized on the Juniper email address to resolve the From/SOB mismatch.

v3:
- No changes to this patch in this version.

v2:
- New patch in the series.
- Export pmbus_wait() and pmbus_update_ts() to the PMBUS namespace.
- Relocate PMBUS_OP_* bitmasks to the subsystem header.

---
 drivers/hwmon/pmbus/pmbus.h      | 6 ++++++
 drivers/hwmon/pmbus/pmbus_core.c | 9 +++++----
 2 files changed, 11 insertions(+), 4 deletions(-)
drivers/hwmon/pmbus/pmbus.h +6 -0
index 3ddcb742d289e..deb556971a726 100644
@@ -424,6 +424,10 @@ enum vrm_version { vr11 = 0, vr12, vr13, imvp9, amd625mv, nvidia195mv };
 #define PMBUS_REV_12 0x22	/* PMBus revision 1.2 */
 #define PMBUS_REV_13 0x33	/* PMBus revision 1.3 */
 
+/* Operation type flags for pmbus_update_ts */
+#define PMBUS_OP_WRITE		BIT(0)
+#define PMBUS_OP_PAGE_CHANGE	BIT(1)
+
 struct pmbus_driver_info {
 	int pages;		/* Total number of pages */
 	u8 phases[PMBUS_PAGES];	/* Number of phases per page */
@@ -541,6 +545,8 @@ int pmbus_regulator_init_cb(struct regulator_dev *rdev,
 
 void pmbus_clear_cache(struct i2c_client *client);
 void pmbus_set_update(struct i2c_client *client, u8 reg, bool update);
+void pmbus_wait(struct i2c_client *client);
+void pmbus_update_ts(struct i2c_client *client, int op);
 int pmbus_set_page(struct i2c_client *client, int page, int phase);
 int pmbus_read_word_data(struct i2c_client *client, int page, int phase,
 			 u8 reg);
drivers/hwmon/pmbus/pmbus_core.c +5 -4
index 3dad455448d05..9db5a53fd97c8 100644
@@ -36,8 +36,7 @@
  * The type of operation used for picking the delay between
  * successive pmbus operations.
  */
-#define PMBUS_OP_WRITE		BIT(0)
-#define PMBUS_OP_PAGE_CHANGE	BIT(1)
+/* PMBUS_OP_WRITE and PMBUS_OP_PAGE_CHANGE are defined in pmbus.h */
 
 static int wp = -1;
 module_param(wp, int, 0444);
@@ -173,7 +172,7 @@ void pmbus_set_update(struct i2c_client *client, u8 reg, bool update)
 EXPORT_SYMBOL_NS_GPL(pmbus_set_update, "PMBUS");
 
 /* Some chips need a delay between accesses. */
-static void pmbus_wait(struct i2c_client *client)
+void pmbus_wait(struct i2c_client *client)
 {
 	struct pmbus_data *data = i2c_get_clientdata(client);
 	s64 delay = ktime_us_delta(data->next_access_backoff, ktime_get());
@@ -181,9 +180,10 @@ static void pmbus_wait(struct i2c_client *client)
 	if (delay > 0)
 		fsleep(delay);
 }
+EXPORT_SYMBOL_NS_GPL(pmbus_wait, "PMBUS");
 
 /* Sets the last operation timestamp for pmbus_wait */
-static void pmbus_update_ts(struct i2c_client *client, int op)
+void pmbus_update_ts(struct i2c_client *client, int op)
 {
 	struct pmbus_data *data = i2c_get_clientdata(client);
 	const struct pmbus_driver_info *info = data->info;
@@ -197,6 +197,7 @@ static void pmbus_update_ts(struct i2c_client *client, int op)
 	if (delay > 0)
 		data->next_access_backoff = ktime_add_us(ktime_get(), delay);
 }
+EXPORT_SYMBOL_NS_GPL(pmbus_update_ts, "PMBUS");
 
 int pmbus_set_page(struct i2c_client *client, int page, int phase)
 {
Signature · 1 line
2.34.1
#5 lore raw

PATCH v5 3/4 hwmon: (pmbus/max31785) use access_delay for PMBus-mediated accesses

In reply to Pradhan, Sanman

From: Sanman Pradhan <[email protected]>

The MAX31785 driver currently uses driver-local wrappers around PMBus core accesses to enforce a 250us inter-access delay needed to work around occasional NACKs from the device. This duplicates the PMBus core delay mechanism already provided by pmbus_driver_info.access_delay and adds unnecessary complexity.

Replace the PMBus wrapper approach with access_delay for normal PMBus-mediated accesses, while keeping the minimal local delay handling needed for raw pre-probe SMBus operations.

For the raw i2c_transfer() long-read path, use pmbus_wait() and pmbus_update_ts() to keep the PMBus core timing state consistent with the raw transfer.

Also:
- allow PMBUS_FAN_CONFIG_12 physical-page accesses to fall back to the PMBus core, while remapping only virtual pages
- use pmbus_update_fan() directly for fan configuration updates
- use the delayed raw read helper for MFR_REVISION during probe
- add a final max31785_wait() before pmbus_do_probe() to bridge the timing gap between pre-probe accesses and PMBus core registration
- rename 'virtual' to 'vpage', 'driver_data' to 'data', and drop the unused to_max31785_data() macro

---
v5:
- No changes to this patch in this version.

v4:
- Standardized on the Juniper email address to resolve the From/SOB mismatch.

v3:
- Added an explicit max31785_wait() before pmbus_do_probe() to ensure proper timing spacing during the handover to the PMBus core.

v2:
- Replaced local usleep_range() with core pmbus_wait() and pmbus_update_ts() in the raw long-read path.
- Updated read_byte_data() to allow core fallback for physical pages.

---
 drivers/hwmon/pmbus/max31785.c | 191 +++++++++++----------------------
 1 file changed, 60 insertions(+), 131 deletions(-)
drivers/hwmon/pmbus/max31785.c +60 -131
index 50073fe0c5e88..260aa8fb50f13 100644
@@ -31,8 +31,6 @@ struct max31785_data {
 	struct pmbus_driver_info info;
 };
 
-#define to_max31785_data(x)  container_of(x, struct max31785_data, info)
-
 /*
  * MAX31785 Driver Workaround
  *
@@ -40,9 +38,8 @@ struct max31785_data {
  * These issues are not indicated by the device itself, except for occasional
  * NACK responses during master transactions. No error bits are set in STATUS_BYTE.
  *
- * To address this, we introduce a delay of 250us between consecutive accesses
- * to the fan controller. This delay helps mitigate communication problems by
- * allowing sufficient time between accesses.
+ * Keep minimal local delay handling for raw pre-probe SMBus accesses.
+ * Normal PMBus-mediated accesses use pmbus_driver_info.access_delay instead.
  */
 static inline void max31785_wait(const struct max31785_data *data)
 {
@@ -54,89 +51,40 @@ static inline void max31785_wait(const struct max31785_data *data)
 }
 
 static int max31785_i2c_write_byte_data(struct i2c_client *client,
-					struct max31785_data *driver_data,
-					int command, u8 data)
+					struct max31785_data *data,
+					int command, u8 value)
 {
 	int rc;
 
-	max31785_wait(driver_data);
-	rc = i2c_smbus_write_byte_data(client, command, data);
-	driver_data->access = ktime_get();
+	max31785_wait(data);
+	rc = i2c_smbus_write_byte_data(client, command, value);
+	data->access = ktime_get();
 	return rc;
 }
 
 static int max31785_i2c_read_word_data(struct i2c_client *client,
-				       struct max31785_data *driver_data,
+				       struct max31785_data *data,
 				       int command)
 {
 	int rc;
 
-	max31785_wait(driver_data);
+	max31785_wait(data);
 	rc = i2c_smbus_read_word_data(client, command);
-	driver_data->access = ktime_get();
-	return rc;
-}
-
-static int _max31785_read_byte_data(struct i2c_client *client,
-				    struct max31785_data *driver_data,
-				    int page, int command)
-{
-	int rc;
-
-	max31785_wait(driver_data);
-	rc = pmbus_read_byte_data(client, page, command);
-	driver_data->access = ktime_get();
-	return rc;
-}
-
-static int _max31785_write_byte_data(struct i2c_client *client,
-				     struct max31785_data *driver_data,
-				     int page, int command, u16 data)
-{
-	int rc;
-
-	max31785_wait(driver_data);
-	rc = pmbus_write_byte_data(client, page, command, data);
-	driver_data->access = ktime_get();
-	return rc;
-}
-
-static int _max31785_read_word_data(struct i2c_client *client,
-				    struct max31785_data *driver_data,
-				    int page, int phase, int command)
-{
-	int rc;
-
-	max31785_wait(driver_data);
-	rc = pmbus_read_word_data(client, page, phase, command);
-	driver_data->access = ktime_get();
-	return rc;
-}
-
-static int _max31785_write_word_data(struct i2c_client *client,
-				     struct max31785_data *driver_data,
-				     int page, int command, u16 data)
-{
-	int rc;
-
-	max31785_wait(driver_data);
-	rc = pmbus_write_word_data(client, page, command, data);
-	driver_data->access = ktime_get();
+	data->access = ktime_get();
 	return rc;
 }
 
 static int max31785_read_byte_data(struct i2c_client *client, int page, int reg)
 {
-	const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
-	struct max31785_data *driver_data = to_max31785_data(info);
-
 	switch (reg) {
 	case PMBUS_VOUT_MODE:
 		return -ENOTSUPP;
 	case PMBUS_FAN_CONFIG_12:
-		return _max31785_read_byte_data(client, driver_data,
-						page - MAX31785_NR_PAGES,
-						reg);
+		if (page < MAX31785_NR_PAGES)
+			return -ENODATA;
+		return pmbus_read_byte_data(client,
+					    page - MAX31785_NR_PAGES,
+					    reg);
 	}
 
 	return -ENODATA;
@@ -178,7 +126,21 @@ static int max31785_read_long_data(struct i2c_client *client, int page,
 	if (rc < 0)
 		return rc;
 
+	/*
+	 * Ensure the raw transfer is properly spaced from the
+	 * preceding PMBus transaction.
+	 */
+	pmbus_wait(client);
+
 	rc = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
+
+	/*
+	 * Update PMBus core timing state for the raw transfer, even on error.
+	 * Pass 0 as the operation mask since this is a raw read, intentionally
+	 * neither PMBUS_OP_WRITE nor PMBUS_OP_PAGE_CHANGE.
+	 */
+	pmbus_update_ts(client, 0);
+
 	if (rc < 0)
 		return rc;
 
@@ -203,19 +165,16 @@ static int max31785_get_pwm(struct i2c_client *client, int page)
 	return rv;
 }
 
-static int max31785_get_pwm_mode(struct i2c_client *client,
-				 struct max31785_data *driver_data, int page)
+static int max31785_get_pwm_mode(struct i2c_client *client, int page)
 {
 	int config;
 	int command;
 
-	config = _max31785_read_byte_data(client, driver_data, page,
-					  PMBUS_FAN_CONFIG_12);
+	config = pmbus_read_byte_data(client, page, PMBUS_FAN_CONFIG_12);
 	if (config < 0)
 		return config;
 
-	command = _max31785_read_word_data(client, driver_data, page, 0xff,
-					   PMBUS_FAN_COMMAND_1);
+	command = pmbus_read_word_data(client, page, 0xff, PMBUS_FAN_COMMAND_1);
 	if (command < 0)
 		return command;
 
@@ -233,8 +192,6 @@ static int max31785_get_pwm_mode(struct i2c_client *client,
 static int max31785_read_word_data(struct i2c_client *client, int page,
 				   int phase, int reg)
 {
-	const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
-	struct max31785_data *driver_data = to_max31785_data(info);
 	u32 val;
 	int rv;
 
@@ -263,7 +220,7 @@ static int max31785_read_word_data(struct i2c_client *client, int page,
 		rv = max31785_get_pwm(client, page);
 		break;
 	case PMBUS_VIRT_PWM_ENABLE_1:
-		rv = max31785_get_pwm_mode(client, driver_data, page);
+		rv = max31785_get_pwm_mode(client, page);
 		break;
 	default:
 		rv = -ENODATA;
@@ -294,35 +251,7 @@ static inline u32 max31785_scale_pwm(u32 sensor_val)
 	return (sensor_val * 100) / 255;
 }
 
-static int max31785_update_fan(struct i2c_client *client,
-			       struct max31785_data *driver_data, int page,
-			       u8 config, u8 mask, u16 command)
-{
-	int from, rv;
-	u8 to;
-
-	from = _max31785_read_byte_data(client, driver_data, page,
-					PMBUS_FAN_CONFIG_12);
-	if (from < 0)
-		return from;
-
-	to = (from & ~mask) | (config & mask);
-
-	if (to != from) {
-		rv = _max31785_write_byte_data(client, driver_data, page,
-					       PMBUS_FAN_CONFIG_12, to);
-		if (rv < 0)
-			return rv;
-	}
-
-	rv = _max31785_write_word_data(client, driver_data, page,
-				       PMBUS_FAN_COMMAND_1, command);
-
-	return rv;
-}
-
-static int max31785_pwm_enable(struct i2c_client *client,
-			       struct max31785_data *driver_data, int page,
+static int max31785_pwm_enable(struct i2c_client *client, int page,
 			       u16 word)
 {
 	int config = 0;
@@ -351,23 +280,20 @@ static int max31785_pwm_enable(struct i2c_client *client,
 		return -EINVAL;
 	}
 
-	return max31785_update_fan(client, driver_data, page, config,
-				   PB_FAN_1_RPM, rate);
+	return pmbus_update_fan(client, page, 0, config,
+			       PB_FAN_1_RPM, rate);
 }
 
 static int max31785_write_word_data(struct i2c_client *client, int page,
 				    int reg, u16 word)
 {
-	const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
-	struct max31785_data *driver_data = to_max31785_data(info);
-
 	switch (reg) {
 	case PMBUS_VIRT_PWM_1:
-		return max31785_update_fan(client, driver_data, page, 0,
-					   PB_FAN_1_RPM,
-					   max31785_scale_pwm(word));
+		return pmbus_update_fan(client, page, 0, 0,
+				       PB_FAN_1_RPM,
+				       max31785_scale_pwm(word));
 	case PMBUS_VIRT_PWM_ENABLE_1:
-		return max31785_pwm_enable(client, driver_data, page, word);
+		return max31785_pwm_enable(client, page, word);
 	default:
 		break;
 	}
@@ -391,6 +317,7 @@ static const struct pmbus_driver_info max31785_info = {
 	.read_byte_data = max31785_read_byte_data,
 	.read_word_data = max31785_read_word_data,
 	.write_byte = max31785_write_byte,
+	.access_delay = MAX31785_WAIT_DELAY_US,
 
 	/* RPM */
 	.format[PSC_FAN] = direct,
@@ -438,29 +365,29 @@ static const struct pmbus_driver_info max31785_info = {
 };
 
 static int max31785_configure_dual_tach(struct i2c_client *client,
-					struct pmbus_driver_info *info)
+					struct max31785_data *data)
 {
+	struct pmbus_driver_info *info = &data->info;
 	int ret;
 	int i;
-	struct max31785_data *driver_data = to_max31785_data(info);
 
 	for (i = 0; i < MAX31785_NR_FAN_PAGES; i++) {
-		ret = max31785_i2c_write_byte_data(client, driver_data,
+		ret = max31785_i2c_write_byte_data(client, data,
 						   PMBUS_PAGE, i);
 		if (ret < 0)
 			return ret;
 
-		ret = max31785_i2c_read_word_data(client, driver_data,
+		ret = max31785_i2c_read_word_data(client, data,
 						  MFR_FAN_CONFIG);
 		if (ret < 0)
 			return ret;
 
 		if (ret & MFR_FAN_CONFIG_DUAL_TACH) {
-			int virtual = MAX31785_NR_PAGES + i;
+			int vpage = MAX31785_NR_PAGES + i;
 
-			info->pages = virtual + 1;
-			info->func[virtual] |= PMBUS_HAVE_FAN12;
-			info->func[virtual] |= PMBUS_PAGE_VIRTUAL;
+			info->pages = vpage + 1;
+			info->func[vpage] |= PMBUS_HAVE_FAN12;
+			info->func[vpage] |= PMBUS_PAGE_VIRTUAL;
 		}
 	}
 
@@ -471,7 +398,7 @@ static int max31785_probe(struct i2c_client *client)
 {
 	struct device *dev = &client->dev;
 	struct pmbus_driver_info *info;
-	struct max31785_data *driver_data;
+	struct max31785_data *data;
 	bool dual_tach = false;
 	int ret;
 
@@ -480,20 +407,20 @@ static int max31785_probe(struct i2c_client *client)
 				     I2C_FUNC_SMBUS_WORD_DATA))
 		return -ENODEV;
 
-	driver_data = devm_kzalloc(dev, sizeof(struct max31785_data), GFP_KERNEL);
-	if (!driver_data)
+	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
+	if (!data)
 		return -ENOMEM;
 
-	info = &driver_data->info;
-	driver_data->access = ktime_get();
+	data->access = ktime_get();
+	info = &data->info;
 	*info = max31785_info;
 
-	ret = max31785_i2c_write_byte_data(client, driver_data,
-					   PMBUS_PAGE, 255);
+	ret = max31785_i2c_write_byte_data(client, data,
+					   PMBUS_PAGE, 0xff);
 	if (ret < 0)
 		return ret;
 
-	ret = i2c_smbus_read_word_data(client, MFR_REVISION);
+	ret = max31785_i2c_read_word_data(client, data, MFR_REVISION);
 	if (ret < 0)
 		return ret;
 
@@ -509,11 +436,13 @@ static int max31785_probe(struct i2c_client *client)
 	}
 
 	if (dual_tach) {
-		ret = max31785_configure_dual_tach(client, info);
+		ret = max31785_configure_dual_tach(client, data);
 		if (ret < 0)
 			return ret;
 	}
 
+	max31785_wait(data);
+
 	return pmbus_do_probe(client, info);
 }
 
Signature · 1 line
2.34.1
#6 lore raw

PATCH v5 4/4 hwmon: (pmbus/max31785) check for partial i2c_transfer in read_long_data

In reply to Pradhan, Sanman

From: Sanman Pradhan <[email protected]>

i2c_transfer() returns the number of messages successfully transferred, not only a negative errno on failure. When called with two messages (write command byte followed by a read of the 4-byte response), a return value of 1 means the command write succeeded but the read did not complete. In that case, rspbuf remains uninitialized and must not be interpreted as valid data.

Treat any return value other than ARRAY_SIZE(msg) as an error, and return -EIO for partial completion. Also return 0 on success instead of the message count, since the caller only needs to distinguish success from failure.

---
v5:
- New patch in the series. Fixes a pre-existing bug where partial i2c_transfer() returns left rspbuf uninitialized.

---
 drivers/hwmon/pmbus/max31785.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/hwmon/pmbus/max31785.c +3 -3
index 260aa8fb50f13..3caa76bcbeb5e 100644
@@ -141,13 +141,13 @@ static int max31785_read_long_data(struct i2c_client *client, int page,
 	 */
 	pmbus_update_ts(client, 0);
 
-	if (rc < 0)
-		return rc;
+	if (rc != ARRAY_SIZE(msg))
+		return rc < 0 ? rc : -EIO;
 
 	*data = (rspbuf[0] << (0 * 8)) | (rspbuf[1] << (1 * 8)) |
 		(rspbuf[2] << (2 * 8)) | (rspbuf[3] << (3 * 8));
 
-	return rc;
+	return 0;
 }
 
 static int max31785_get_pwm(struct i2c_client *client, int page)
Signature · 1 line
2.34.1