Skip to contentSkip to conversation

hwmon: (max1619) Modernize driver

PATCH In review Guenter Roeck 2 participants 13 messages Jul 28, 2024 – Jul 29, 2024

Subsystems (2)
lore
Share & tools
lore

Guenter Roeck

author maintainer
#0 lore raw

v2 cover hwmon: (max1619) Modernize driver

Fix limit overflows, improve chip detection code, convert to use regmap, convert to use with_info hwmon API, and add support for update_interval sysfs attribute.

v2: Initialize chip as in original code
Separate 'alarms' attribute masking into its own patch Added Reviewed-by: tags

----------------------------------------------------------------
Guenter Roeck (7):
hwmon: (max1619) Clamp temperature range when writing limits
hwmon: (max1619) Reorder include files to alphabetic order
hwmon: (max1619) Mask valid alarm bits
hwmon: (max1619) Convert to use regmap
hwmon: (max1619) Convert to with_info API
hwmon: (max1619) Add support for update_interval attribute
hwmon: (max1619) Improve chip detection code

 Documentation/hwmon/max1619.rst |   4 -
 drivers/hwmon/max1619.c         | 495 +++++++++++++++++++++++-----------------
 2 files changed, 288 insertions(+), 211 deletions(-)
author maintainer
#1 lore raw

PATCH v2 1/7 hwmon: (max1619) Clamp temperature range when writing limits

Module test code reports underflows when writing sensor limits.

temp2_min: Suspected underflow: [min=-77000, read 101000, written -2147483648]
temp2_max: Suspected underflow: [min=-77000, read 101000, written -2147483648]
temp2_crit: Suspected underflow: [min=-77000, read 101000, written -2147483648]

Clamp temperature ranges when writing limits to fix the problem. While at it, use sign_extend32() when reading temperatures to make the code easier to understand.

---
v2: Added Reviewed-by: tag

 drivers/hwmon/max1619.c | 14 ++------------
 1 file changed, 2 insertions(+), 12 deletions(-)
drivers/hwmon/max1619.c +2 -12
index a89a519cf5d9..464f4c838394 100644
@@ -52,16 +52,6 @@ static const unsigned short normal_i2c[] = {
  * Conversions
  */
 
-static int temp_from_reg(int val)
-{
-	return (val & 0x80 ? val-0x100 : val) * 1000;
-}
-
-static int temp_to_reg(int val)
-{
-	return (val < 0 ? val+0x100*1000 : val) / 1000;
-}
-
 enum temp_index {
 	t_input1 = 0,
 	t_input2,
@@ -142,7 +132,7 @@ static ssize_t temp_show(struct device *dev, struct device_attribute *devattr,
 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 	struct max1619_data *data = max1619_update_device(dev);
 
-	return sprintf(buf, "%d\n", temp_from_reg(data->temp[attr->index]));
+	return sprintf(buf, "%d\n", sign_extend(data->temp[attr->index], 7) * 1000);
 }
 
 static ssize_t temp_store(struct device *dev,
@@ -158,7 +148,7 @@ static ssize_t temp_store(struct device *dev,
 		return err;
 
 	mutex_lock(&data->update_lock);
-	data->temp[attr->index] = temp_to_reg(val);
+	data->temp[attr->index] = DIV_ROUND_CLOSEST(clamp_val(val, -128000, 127000), 1000);
 	i2c_smbus_write_byte_data(client, regs_write[attr->index],
 				  data->temp[attr->index]);
 	mutex_unlock(&data->update_lock);
Signature · 1 line
2.39.2
author maintainer
#2 lore raw

PATCH v2 2/7 hwmon: (max1619) Reorder include files to alphabetic order

In reply to Guenter Roeck

Simplify maintenance by reordering include files to alphabetic order.

---
v2: Added Reviewed-by: tag

 drivers/hwmon/max1619.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
drivers/hwmon/max1619.c +6 -6
index 464f4c838394..8eb7d04bd2f5 100644
@@ -12,15 +12,15 @@
  *   http://pdfserv.maxim-ic.com/en/ds/MAX1619.pdf
  */
 
-#include <linux/module.h>
-#include <linux/init.h>
-#include <linux/slab.h>
-#include <linux/jiffies.h>
-#include <linux/i2c.h>
+#include <linux/err.h>
 #include <linux/hwmon.h>
 #include <linux/hwmon-sysfs.h>
-#include <linux/err.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/sysfs.h>
 
 static const unsigned short normal_i2c[] = {
Signature · 1 line
2.39.2
author maintainer
#3 lore raw

PATCH v2 3/7 hwmon: (max1619) Mask valid alarm bits

In reply to Guenter Roeck

Bit 0, 5, and 6 in the status register are reserved and, if set, do not indicate an alarm. Bit 7 is the 'busy' bit and also does not indicate an alarm. Mask the non-alarm bits to avoid reporting them to userspace.

---
v2: Added patch

 drivers/hwmon/max1619.c | 1 +
 1 file changed, 1 insertion(+)
drivers/hwmon/max1619.c +1 -0
index 8eb7d04bd2f5..5edc9bbbe299 100644
@@ -112,6 +112,7 @@ static struct max1619_data *max1619_update_device(struct device *dev)
 		config = i2c_smbus_read_byte_data(client, MAX1619_REG_R_CONFIG);
 		if (!(config & 0x20))
 			data->alarms ^= 0x02;
+		data->alarms &= 0x1e;
 
 		data->last_updated = jiffies;
 		data->valid = true;
Signature · 1 line
2.39.2
Reviewed-by on v2 3/7
#4 lore raw

Re: [PATCH v2 3/7] hwmon: (max1619) Mask valid alarm bits

Quoted 6 lines · Guenter Roeck

Bit 0, 5, and 6 in the status register are reserved and, if set, do not indicate an alarm. Bit 7 is the 'busy' bit and also does not indicate an alarm. Mask the non-alarm bits to avoid reporting them to userspace.

author maintainer
#5 lore raw

PATCH v2 4/7 hwmon: (max1619) Convert to use regmap

In reply to Guenter Roeck

Use regmap for local caching, to hide register read/write address differences, and for multi-byte operations. With this change, the driver specific lock is no longer necessary.

While at it, check errors seen when initializing the chip and bail out if chip initialization fails.

---
v2: Split masking of 'alarms' attribute into separate patch
Initialize chip to match original code

 Documentation/hwmon/max1619.rst |   4 -
 drivers/hwmon/max1619.c         | 259 +++++++++++++++++---------------
 2 files changed, 137 insertions(+), 126 deletions(-)
Documentation/hwmon/max1619.rst +0 -4
index e25956e70f73..b5fc175ae18d 100644
@@ -27,7 +27,3 @@ All temperature values are given in degrees Celsius. Resolution
 is 1.0 degree for the local temperature and for the remote temperature.
 
 Only the external sensor has high and low limits.
-
-The max1619 driver will not update its values more frequently than every
-other second; reading them more often will do no harm, but will return
-'old' values.
drivers/hwmon/max1619.c +137 -122

337 lines not on this page · raw message

Signature · 1 line
2.39.2
#6 lore raw

Re: [PATCH v2 4/7] hwmon: (max1619) Convert to use regmap

Quoted 11 lines · Guenter Roeck
drivers/hwmon/max1619.c +2 -7
-static void max1619_init_client(struct i2c_client *client)
+static int max1619_init_chip(struct regmap *regmap)
 {
-	u8 config;
+	int ret;
 
-	/*
-	 * Start the conversions.
-	 */
-	i2c_smbus_write_byte_data(client, MAX1619_REG_W_CONVRATE,
-				  5); /* 2 Hz */

drivers/hwmon/max1619.c static int max1619_detect(struct i2c_client *client, · line 222

Quoted 10 lines
drivers/hwmon/max1619.c +6 -4
-	config = i2c_smbus_read_byte_data(client, MAX1619_REG_R_CONFIG);
-	if (config & 0x40)
-		i2c_smbus_write_byte_data(client, MAX1619_REG_W_CONFIG,
-					  config & 0xBF); /* run */
+	ret = regmap_write(regmap, MAX1619_REG_CONVRATE, 5);	/* 2 Hz */
+	if (ret)
+		return ret;
+
+	/* Start conversions */
+	return regmap_set_bits(regmap, MAX1619_REG_CONFIG, 0x40);

Should be regmap_clear_bits()?

author maintainer
#7 lore raw

Re: [PATCH v2 4/7] hwmon: (max1619) Convert to use regmap

Quoted 16 lines · Tzung-Bi Shih
Quoted 15 lines · Guenter Roeck
drivers/hwmon/max1619.c +2 -11
-static void max1619_init_client(struct i2c_client *client)
+static int max1619_init_chip(struct regmap *regmap)
  {
-	u8 config;
+	int ret;
 
-	/*
-	 * Start the conversions.
-	 */
-	i2c_smbus_write_byte_data(client, MAX1619_REG_W_CONVRATE,
-				  5); /* 2 Hz */
-	config = i2c_smbus_read_byte_data(client, MAX1619_REG_R_CONFIG);
-	if (config & 0x40)
-		i2c_smbus_write_byte_data(client, MAX1619_REG_W_CONFIG,
-					  config & 0xBF); /* run */
Quoted 8 lines

drivers/hwmon/max1619.c static int max1619_detect(struct i2c_client *client, · line 222

Quoted 6 lines
drivers/hwmon/max1619.c +6 -0
+	ret = regmap_write(regmap, MAX1619_REG_CONVRATE, 5);	/* 2 Hz */
+	if (ret)
+		return ret;
+
+	/* Start conversions */
+	return regmap_set_bits(regmap, MAX1619_REG_CONFIG, 0x40);

Should be regmap_clear_bits()?

Sigh. yes, of course.

Thanks,
Guenter

Reviewed-by
#8 lore raw

Re: [PATCH v2 4/7] hwmon: (max1619) Convert to use regmap

Quoted 26 lines · Guenter Roeck
Quoted 15 lines · Tzung-Bi Shih
Quoted 14 lines · Guenter Roeck
drivers/hwmon/max1619.c +2 -11
-static void max1619_init_client(struct i2c_client *client)
+static int max1619_init_chip(struct regmap *regmap)
  {
-	u8 config;
+	int ret;
-	/*
-	 * Start the conversions.
-	 */
-	i2c_smbus_write_byte_data(client, MAX1619_REG_W_CONVRATE,
-				  5); /* 2 Hz */
-	config = i2c_smbus_read_byte_data(client, MAX1619_REG_R_CONFIG);
-	if (config & 0x40)
-		i2c_smbus_write_byte_data(client, MAX1619_REG_W_CONFIG,
-					  config & 0xBF); /* run */
Quoted 8 lines

drivers/hwmon/max1619.c static int max1619_detect(struct i2c_client *client, · line 222

Quoted 6 lines
drivers/hwmon/max1619.c +6 -0
+	ret = regmap_write(regmap, MAX1619_REG_CONVRATE, 5);	/* 2 Hz */
+	if (ret)
+		return ret;
+
+	/* Start conversions */
+	return regmap_set_bits(regmap, MAX1619_REG_CONFIG, 0x40);

Should be regmap_clear_bits()?

Quoted 1 line

Sigh. yes, of course.

With that,

author maintainer
#9 lore raw

Re: [PATCH v2 4/7] hwmon: (max1619) Convert to use regmap

Quoted 27 lines · Tzung-Bi Shih
Quoted 26 lines · Guenter Roeck
Quoted 23 lines · Tzung-Bi Shih
Quoted 10 lines · Guenter Roeck
drivers/hwmon/max1619.c +2 -7
-static void max1619_init_client(struct i2c_client *client)
+static int max1619_init_chip(struct regmap *regmap)
   {
-	u8 config;
+	int ret;
-	/*
-	 * Start the conversions.
-	 */
-	i2c_smbus_write_byte_data(client, MAX1619_REG_W_CONVRATE,
-				  5); /* 2 Hz */

drivers/hwmon/max1619.c static int max1619_detect(struct i2c_client *client, · line 222

Quoted 10 lines
drivers/hwmon/max1619.c +6 -4
-	config = i2c_smbus_read_byte_data(client, MAX1619_REG_R_CONFIG);
-	if (config & 0x40)
-		i2c_smbus_write_byte_data(client, MAX1619_REG_W_CONFIG,
-					  config & 0xBF); /* run */
+	ret = regmap_write(regmap, MAX1619_REG_CONVRATE, 5);	/* 2 Hz */
+	if (ret)
+		return ret;
+
+	/* Start conversions */
+	return regmap_set_bits(regmap, MAX1619_REG_CONFIG, 0x40);

Should be regmap_clear_bits()?

Quoted 5 lines
Quoted 1 line

Sigh. yes, of course.

With that,

Thanks!

Guenter

author maintainer
#10 lore raw

PATCH v2 5/7 hwmon: (max1619) Convert to with_info API

In reply to Guenter Roeck

Convert driver to with_info hwmon API to simplify the code and with it its maintainability.

---
v2: Added Reviewed-by: tag

 drivers/hwmon/max1619.c | 269 +++++++++++++++++++++++-----------------
 1 file changed, 157 insertions(+), 112 deletions(-)
drivers/hwmon/max1619.c +157 -112
index 3ecad847e597..fd84bcc789fa 100644
@@ -14,20 +14,14 @@
 
 #include <linux/err.h>
 #include <linux/hwmon.h>
-#include <linux/hwmon-sysfs.h>
 #include <linux/i2c.h>
 #include <linux/init.h>
 #include <linux/module.h>
 #include <linux/regmap.h>
-#include <linux/sysfs.h>
 
 static const unsigned short normal_i2c[] = {
 	0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b, 0x4c, 0x4d, 0x4e, I2C_CLIENT_END };
 
-/*
- * The MAX1619 registers
- */
-
 #define MAX1619_REG_LOCAL_TEMP		0x00
 #define MAX1619_REG_REMOTE_TEMP		0x01
 #define MAX1619_REG_STATUS		0x02
@@ -40,66 +34,6 @@ static const unsigned short normal_i2c[] = {
 #define MAX1619_REG_MAN_ID		0xFE
 #define MAX1619_REG_CHIP_ID		0xFF
 
-enum temp_index {
-	t_input1 = 0,
-	t_input2,
-	t_low2,
-	t_high2,
-	t_crit2,
-	t_hyst2,
-	t_num_regs
-};
-
-/*
- * Client data (each client gets its own)
- */
-
-static const u8 regs[t_num_regs] = {
-	[t_input1] = MAX1619_REG_LOCAL_TEMP,
-	[t_input2] = MAX1619_REG_REMOTE_TEMP,
-	[t_low2] = MAX1619_REG_REMOTE_LOW,
-	[t_high2] = MAX1619_REG_REMOTE_HIGH,
-	[t_crit2] = MAX1619_REG_REMOTE_CRIT,
-	[t_hyst2] = MAX1619_REG_REMOTE_CRIT_HYST,
-};
-
-/*
- * Sysfs stuff
- */
-
-static ssize_t temp_show(struct device *dev, struct device_attribute *devattr,
-			 char *buf)
-{
-	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
-	struct regmap *regmap = dev_get_drvdata(dev);
-	u32 temp;
-	int ret;
-
-	ret = regmap_read(regmap, regs[attr->index], &temp);
-	if (ret < 0)
-		return ret;
-
-	return sprintf(buf, "%d\n", sign_extend32(temp, 7) * 1000);
-}
-
-static ssize_t temp_store(struct device *dev,
-			  struct device_attribute *devattr, const char *buf,
-			  size_t count)
-{
-	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
-	struct regmap *regmap = dev_get_drvdata(dev);
-	long val;
-	int err = kstrtol(buf, 10, &val);
-	if (err)
-		return err;
-
-	val = DIV_ROUND_CLOSEST(clamp_val(val, -128000, 127000), 1000);
-	err = regmap_write(regmap, regs[attr->index], val);
-	if (err < 0)
-		return err;
-	return count;
-}
-
 static int get_alarms(struct regmap *regmap)
 {
 	static u32 regs[2] = { MAX1619_REG_STATUS, MAX1619_REG_CONFIG };
@@ -117,62 +51,175 @@ static int get_alarms(struct regmap *regmap)
 	return regdata[0] & 0x1e;
 }
 
-static ssize_t alarms_show(struct device *dev, struct device_attribute *attr,
-			   char *buf)
+static int max1619_temp_read(struct regmap *regmap, u32 attr, int channel, long *val)
 {
-	struct regmap *regmap = dev_get_drvdata(dev);
-	int alarms;
+	int reg = -1, alarm_bit = 0;
+	u32 temp;
+	int ret;
 
-	alarms = get_alarms(regmap);
-	if (alarms < 0)
-		return alarms;
-
-	return sprintf(buf, "%d\n", alarms);
+	switch (attr) {
+	case hwmon_temp_input:
+		reg = channel ? MAX1619_REG_REMOTE_TEMP : MAX1619_REG_LOCAL_TEMP;
+		break;
+	case hwmon_temp_min:
+		reg = MAX1619_REG_REMOTE_LOW;
+		break;
+	case hwmon_temp_max:
+		reg = MAX1619_REG_REMOTE_HIGH;
+		break;
+	case hwmon_temp_crit:
+		reg = MAX1619_REG_REMOTE_CRIT;
+		break;
+	case hwmon_temp_crit_hyst:
+		reg = MAX1619_REG_REMOTE_CRIT_HYST;
+		break;
+	case hwmon_temp_min_alarm:
+		alarm_bit = 3;
+		break;
+	case hwmon_temp_max_alarm:
+		alarm_bit = 4;
+		break;
+	case hwmon_temp_crit_alarm:
+		alarm_bit = 1;
+		break;
+	case hwmon_temp_fault:
+		alarm_bit = 2;
+		break;
+	default:
+		return -EOPNOTSUPP;
+	}
+	if (reg >= 0) {
+		ret = regmap_read(regmap, reg, &temp);
+		if (ret < 0)
+			return ret;
+		*val = sign_extend32(temp, 7) * 1000;
+	} else {
+		ret = get_alarms(regmap);
+		if (ret < 0)
+			return ret;
+		*val = !!(ret & BIT(alarm_bit));
+	}
+	return 0;
 }
 
-static ssize_t alarm_show(struct device *dev, struct device_attribute *attr,
-			  char *buf)
+static int max1619_chip_read(struct regmap *regmap, u32 attr, long *val)
 {
-	int bitnr = to_sensor_dev_attr(attr)->index;
-	struct regmap *regmap = dev_get_drvdata(dev);
 	int alarms;
 
-	alarms = get_alarms(regmap);
-	if (alarms < 0)
-		return alarms;
-
-	return sprintf(buf, "%d\n", (alarms >> bitnr) & 1);
+	switch (attr) {
+	case hwmon_chip_alarms:
+		alarms = get_alarms(regmap);
+		if (alarms < 0)
+			return alarms;
+		*val = alarms;
+		break;
+	default:
+		return -EOPNOTSUPP;
+	}
+	return 0;
 }
 
-static SENSOR_DEVICE_ATTR_RO(temp1_input, temp, t_input1);
-static SENSOR_DEVICE_ATTR_RO(temp2_input, temp, t_input2);
-static SENSOR_DEVICE_ATTR_RW(temp2_min, temp, t_low2);
-static SENSOR_DEVICE_ATTR_RW(temp2_max, temp, t_high2);
-static SENSOR_DEVICE_ATTR_RW(temp2_crit, temp, t_crit2);
-static SENSOR_DEVICE_ATTR_RW(temp2_crit_hyst, temp, t_hyst2);
+static int max1619_read(struct device *dev, enum hwmon_sensor_types type,
+			u32 attr, int channel, long *val)
+{
+	struct regmap *regmap = dev_get_drvdata(dev);
 
-static DEVICE_ATTR_RO(alarms);
-static SENSOR_DEVICE_ATTR_RO(temp2_crit_alarm, alarm, 1);
-static SENSOR_DEVICE_ATTR_RO(temp2_fault, alarm, 2);
-static SENSOR_DEVICE_ATTR_RO(temp2_min_alarm, alarm, 3);
-static SENSOR_DEVICE_ATTR_RO(temp2_max_alarm, alarm, 4);
+	switch (type) {
+	case hwmon_chip:
+		return max1619_chip_read(regmap, attr, val);
+	case hwmon_temp:
+		return max1619_temp_read(regmap, attr, channel, val);
+	default:
+		return -EOPNOTSUPP;
+	}
+}
 
-static struct attribute *max1619_attrs[] = {
-	&sensor_dev_attr_temp1_input.dev_attr.attr,
-	&sensor_dev_attr_temp2_input.dev_attr.attr,
-	&sensor_dev_attr_temp2_min.dev_attr.attr,
-	&sensor_dev_attr_temp2_max.dev_attr.attr,
-	&sensor_dev_attr_temp2_crit.dev_attr.attr,
-	&sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
+static int max1619_write(struct device *dev, enum hwmon_sensor_types type,
+			 u32 attr, int channel, long val)
+{
+	struct regmap *regmap = dev_get_drvdata(dev);
+	int reg;
 
-	&dev_attr_alarms.attr,
-	&sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
-	&sensor_dev_attr_temp2_fault.dev_attr.attr,
-	&sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
-	&sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
+	if (type != hwmon_temp)
+		return -EOPNOTSUPP;
+
+	switch (attr) {
+	case hwmon_temp_min:
+		reg = MAX1619_REG_REMOTE_LOW;
+		break;
+	case hwmon_temp_max:
+		reg = MAX1619_REG_REMOTE_HIGH;
+		break;
+	case hwmon_temp_crit:
+		reg = MAX1619_REG_REMOTE_CRIT;
+		break;
+	case hwmon_temp_crit_hyst:
+		reg = MAX1619_REG_REMOTE_CRIT_HYST;
+		break;
+	default:
+		return -EOPNOTSUPP;
+	}
+	val = DIV_ROUND_CLOSEST(clamp_val(val, -128000, 127000), 1000);
+	return regmap_write(regmap, reg, val);
+}
+
+static umode_t max1619_is_visible(const void *_data, enum hwmon_sensor_types type,
+				  u32 attr, int channel)
+{
+	switch (type) {
+	case hwmon_chip:
+		switch (attr) {
+		case hwmon_chip_alarms:
+			return 0444;
+		default:
+			break;
+		}
+		break;
+	case hwmon_temp:
+		switch (attr) {
+		case hwmon_temp_input:
+			return 0444;
+		case hwmon_temp_min:
+		case hwmon_temp_max:
+		case hwmon_temp_crit:
+		case hwmon_temp_crit_hyst:
+			return 0644;
+		case hwmon_temp_min_alarm:
+		case hwmon_temp_max_alarm:
+		case hwmon_temp_crit_alarm:
+		case hwmon_temp_fault:
+			return 0444;
+		default:
+			break;
+		}
+		break;
+	default:
+		break;
+	}
+	return 0;
+}
+
+static const struct hwmon_channel_info * const max1619_info[] = {
+	HWMON_CHANNEL_INFO(chip, HWMON_C_ALARMS),
+	HWMON_CHANNEL_INFO(temp,
+			   HWMON_T_INPUT,
+			   HWMON_T_INPUT | HWMON_T_MIN | HWMON_T_MAX |
+			   HWMON_T_CRIT | HWMON_T_CRIT_HYST |
+			   HWMON_T_MIN_ALARM | HWMON_T_MAX_ALARM |
+			   HWMON_T_CRIT_ALARM | HWMON_T_FAULT),
 	NULL
 };
-ATTRIBUTE_GROUPS(max1619);
+
+static const struct hwmon_ops max1619_hwmon_ops = {
+	.is_visible = max1619_is_visible,
+	.read = max1619_read,
+	.write = max1619_write,
+};
+
+static const struct hwmon_chip_info max1619_chip_info = {
+	.ops = &max1619_hwmon_ops,
+	.info = max1619_info,
+};
 
 /* Return 0 if detection is successful, -ENODEV otherwise */
 static int max1619_detect(struct i2c_client *client,
@@ -283,10 +330,8 @@ static int max1619_probe(struct i2c_client *client)
 	if (ret)
 		return ret;
 
-	hwmon_dev = devm_hwmon_device_register_with_groups(dev,
-							   client->name,
-							   regmap,
-							   max1619_groups);
+	hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name,
+							 regmap, &max1619_chip_info, NULL);
 	return PTR_ERR_OR_ZERO(hwmon_dev);
 }
 
Signature · 1 line
2.39.2
author maintainer
#11 lore raw

PATCH v2 6/7 hwmon: (max1619) Add support for update_interval attribute

In reply to Guenter Roeck

The chip supports reading and writing the conversion rate. Add support for the update_interval sysfs attribute.

---
v2: Added Reviewed-by: tag

 drivers/hwmon/max1619.c | 50 ++++++++++++++++++++++++++++++++++-------
 1 file changed, 42 insertions(+), 8 deletions(-)
drivers/hwmon/max1619.c +42 -8
index fd84bcc789fa..69d3156fa2ba 100644
@@ -18,6 +18,7 @@
 #include <linux/init.h>
 #include <linux/module.h>
 #include <linux/regmap.h>
+#include <linux/util_macros.h>
 
 static const unsigned short normal_i2c[] = {
 	0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b, 0x4c, 0x4d, 0x4e, I2C_CLIENT_END };
@@ -102,11 +103,20 @@ static int max1619_temp_read(struct regmap *regmap, u32 attr, int channel, long
 	return 0;
 }
 
+static u16 update_intervals[] = { 16000, 8000, 4000, 2000, 1000, 500, 250, 125 };
+
 static int max1619_chip_read(struct regmap *regmap, u32 attr, long *val)
 {
-	int alarms;
+	int alarms, ret;
+	u32 regval;
 
 	switch (attr) {
+	case hwmon_chip_update_interval:
+		ret = regmap_read(regmap, MAX1619_REG_CONVRATE, &regval);
+		if (ret < 0)
+			return ret;
+		*val = update_intervals[regval & 7];
+		break;
 	case hwmon_chip_alarms:
 		alarms = get_alarms(regmap);
 		if (alarms < 0)
@@ -134,14 +144,21 @@ static int max1619_read(struct device *dev, enum hwmon_sensor_types type,
 	}
 }
 
-static int max1619_write(struct device *dev, enum hwmon_sensor_types type,
-			 u32 attr, int channel, long val)
+static int max1619_chip_write(struct regmap *regmap, u32 attr, long val)
 {
-	struct regmap *regmap = dev_get_drvdata(dev);
-	int reg;
-
-	if (type != hwmon_temp)
+	switch (attr) {
+	case hwmon_chip_update_interval:
+		val = find_closest_descending(val, update_intervals, ARRAY_SIZE(update_intervals));
+		return regmap_write(regmap, MAX1619_REG_CONVRATE, val);
+	default:
 		return -EOPNOTSUPP;
+	}
+}
+
+static int max1619_temp_write(struct regmap *regmap,
+			      u32 attr, int channel, long val)
+{
+	int reg;
 
 	switch (attr) {
 	case hwmon_temp_min:
@@ -163,12 +180,29 @@ static int max1619_write(struct device *dev, enum hwmon_sensor_types type,
 	return regmap_write(regmap, reg, val);
 }
 
+static int max1619_write(struct device *dev, enum hwmon_sensor_types type,
+			 u32 attr, int channel, long val)
+{
+	struct regmap *regmap = dev_get_drvdata(dev);
+
+	switch (type) {
+	case hwmon_chip:
+		return max1619_chip_write(regmap, attr, val);
+	case hwmon_temp:
+		return max1619_temp_write(regmap, attr, channel, val);
+	default:
+		return -EOPNOTSUPP;
+	}
+}
+
 static umode_t max1619_is_visible(const void *_data, enum hwmon_sensor_types type,
 				  u32 attr, int channel)
 {
 	switch (type) {
 	case hwmon_chip:
 		switch (attr) {
+		case hwmon_chip_update_interval:
+			return 0644;
 		case hwmon_chip_alarms:
 			return 0444;
 		default:
@@ -200,7 +234,7 @@ static umode_t max1619_is_visible(const void *_data, enum hwmon_sensor_types typ
 }
 
 static const struct hwmon_channel_info * const max1619_info[] = {
-	HWMON_CHANNEL_INFO(chip, HWMON_C_ALARMS),
+	HWMON_CHANNEL_INFO(chip, HWMON_C_ALARMS | HWMON_C_UPDATE_INTERVAL),
 	HWMON_CHANNEL_INFO(temp,
 			   HWMON_T_INPUT,
 			   HWMON_T_INPUT | HWMON_T_MIN | HWMON_T_MAX |
Signature · 1 line
2.39.2
author maintainer
#12 lore raw

PATCH v2 7/7 hwmon: (max1619) Improve chip detection code

In reply to Guenter Roeck

Bail out immediately if reading any of the registers used for chip detection fails, or if it returns an unexpected value. Drop all log messages from detection code.

---
v2: Added Reviewed-by: tag

 drivers/hwmon/max1619.c | 32 ++++++++++++++------------------
 1 file changed, 14 insertions(+), 18 deletions(-)
drivers/hwmon/max1619.c +14 -18
index 69d3156fa2ba..26ffb21daa3d 100644
@@ -260,31 +260,27 @@ static int max1619_detect(struct i2c_client *client,
 			  struct i2c_board_info *info)
 {
 	struct i2c_adapter *adapter = client->adapter;
-	u8 reg_config, reg_convrate, reg_status, man_id, chip_id;
+	int regval;
 
 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
 		return -ENODEV;
 
-	/* detection */
-	reg_config = i2c_smbus_read_byte_data(client, MAX1619_REG_CONFIG);
-	reg_convrate = i2c_smbus_read_byte_data(client, MAX1619_REG_CONVRATE);
-	reg_status = i2c_smbus_read_byte_data(client, MAX1619_REG_STATUS);
-	if ((reg_config & 0x03) != 0x00
-	 || reg_convrate > 0x07 || (reg_status & 0x61) != 0x00) {
-		dev_dbg(&adapter->dev, "MAX1619 detection failed at 0x%02x\n",
-			client->addr);
+	regval = i2c_smbus_read_byte_data(client, MAX1619_REG_CONFIG);
+	if (regval < 0 || (regval & 0x03))
+		return -ENODEV;
+	regval = i2c_smbus_read_byte_data(client, MAX1619_REG_CONVRATE);
+	if (regval < 0 || regval > 0x07)
+		return -ENODEV;
+	regval = i2c_smbus_read_byte_data(client, MAX1619_REG_STATUS);
+	if (regval < 0 || (regval & 0x61))
 		return -ENODEV;
-	}
 
-	/* identification */
-	man_id = i2c_smbus_read_byte_data(client, MAX1619_REG_MAN_ID);
-	chip_id = i2c_smbus_read_byte_data(client, MAX1619_REG_CHIP_ID);
-	if (man_id != 0x4D || chip_id != 0x04) {
-		dev_info(&adapter->dev,
-			 "Unsupported chip (man_id=0x%02X, chip_id=0x%02X).\n",
-			 man_id, chip_id);
+	regval = i2c_smbus_read_byte_data(client, MAX1619_REG_MAN_ID);
+	if (regval != 0x4d)
+		return -ENODEV;
+	regval = i2c_smbus_read_byte_data(client, MAX1619_REG_CHIP_ID);
+	if (regval != 0x04)
 		return -ENODEV;
-	}
 
 	strscpy(info->type, "max1619", I2C_NAME_SIZE);
 
Signature · 1 line
2.39.2