Skip to contentSkip to conversation

hwmon: adt7x10: Refactor to use with_info API

PATCH In review Guenter Roeck 2 participants 9 messages Dec 23, 2021 – Feb 7, 2022 Hardware monitoring

lore
Share & tools
lore

Guenter Roeck

author maintainer
#0 lore raw

PATCH v4 1/6 hwmon: adt7x10: Refactor to use with_info API

V1 -> V2:
* add device managed action for restoring config
* merge multiple small related patches into a single patch that converts the driver to use devm_hwmon_device_register_with_info
* switch to devm_request_threaded_irq after switching to devm_hwmon_device_register_with_info to make sure that it is impossible for the interrupt handler to access the freed hwmon device
* drop core driver remove callback

V2 -> V3:
* merge patch that passes name from i2c driver into the devm_hwmon_device_register_with_info patch

v3 -> v4:
* Use regmap to hide chip specifics and to cache register values
* Various minor changes and fixes
* With the use of regmap, the bus device (bus_dev) is no longer needed, and the patch introducing it was dropped
* Hysteresis value calculations depend on two values: The associated register value and the hysteresis itself. All calculations must be protected to ensure that one value isn't changed during calculations. Add the missing locks to both the hysteresis read and write functions.
* Restoring the original configuration is only necessary if it was actually changed. Only call devm_add_action_or_reset() if that is the case. This also lets us drop the associated check in the action function.
* Use enum to index ADT7X10_REG_TEMP[]
* Check all attributes in is_visible function explicitly. While this is strictly speaking not necessary (the mode for unsupported attributes should not be requested), I find the explicit checks easier to understand and less error prone.
* Drop linux/hwmon-sysfs.h include and add missing linux/device.h include
* Squash patches 6/7 (pass hwinfo dev to irq handler) and patch 7/7 (use hwmon_notify_event) into a single patch; otherwise bus_dev would still be needed temporarily.

Note: This version of the series was module tested for ADT7410, but not

       on real hardware, and not for ADT7310/7320/7420.
author maintainer
#1 lore raw

PATCH v4 1/6 hwmon: adt7x10: Convert to use regmap

Using regmap lets us use the regmap subsystem for SPI vs. I2C register accesses. It lets us hide access differences in backend code and lets the common code just access registers without knowing their size. We can also use regmap for register caching.

---
 drivers/hwmon/Kconfig   |   1 +
 drivers/hwmon/adt7310.c |  88 +++++++++++++---
 drivers/hwmon/adt7410.c |  77 ++++++++++----
 drivers/hwmon/adt7x10.c | 225 +++++++++++++++-------------------------
 drivers/hwmon/adt7x10.h |  10 +-
 5 files changed, 213 insertions(+), 188 deletions(-)
drivers/hwmon/Kconfig +1 -0
index 64bd3dfba2c4..b33b6934eeb7 100644
@@ -174,6 +174,7 @@ config SENSORS_ADM9240
 
 config SENSORS_ADT7X10
 	tristate
+	select REGMAP
 	help
 	  This module contains common code shared by the ADT7310/ADT7320 and
 	  ADT7410/ADT7420 temperature monitoring chip drivers.
drivers/hwmon/adt7310.c +71 -17

127 lines not on this page · raw message

drivers/hwmon/adt7410.c +58 -19

101 lines not on this page · raw message

drivers/hwmon/adt7x10.c +81 -144

383 lines not on this page · raw message

drivers/hwmon/adt7x10.h +2 -8
index a1ae682eb32e..55ff08bfe946 100644
@@ -17,15 +17,9 @@
 
 struct device;
 
-struct adt7x10_ops {
-	int (*read_byte)(struct device *, u8 reg);
-	int (*write_byte)(struct device *, u8 reg, u8 data);
-	int (*read_word)(struct device *, u8 reg);
-	int (*write_word)(struct device *, u8 reg, u16 data);
-};
-
 int adt7x10_probe(struct device *dev, const char *name, int irq,
-	const struct adt7x10_ops *ops);
+		  struct regmap *regmap);
+
 void adt7x10_remove(struct device *dev, int irq);
 
 #ifdef CONFIG_PM_SLEEP
Signature · 1 line
2.33.0
author maintainer
#2 lore raw

PATCH v4 2/6 hwmon: (adt7x10) Add device managed action for restoring config

In reply to Guenter Roeck

From: Cosmin Tanislav <[email protected]>

To simplify the core driver remove function.

[groeck: Adjust to use regmap; only register restore function if needed]
---
 drivers/hwmon/adt7x10.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)
drivers/hwmon/adt7x10.c +11 -5
index 05dd48b707b4..47ce1a88a03f 100644
@@ -296,6 +296,13 @@ static const struct attribute_group adt7x10_group = {
 	.attrs = adt7x10_attributes,
 };
 
+static void adt7x10_restore_config(void *private)
+{
+	struct adt7x10_data *data = private;
+
+	regmap_write(data->regmap, ADT7X10_CONFIG, data->oldconfig);
+}
+
 int adt7x10_probe(struct device *dev, const char *name, int irq,
 		  struct regmap *regmap)
 {
@@ -333,13 +340,16 @@ int adt7x10_probe(struct device *dev, const char *name, int irq,
 		ret = regmap_write(regmap, ADT7X10_CONFIG, data->config);
 		if (ret)
 			return ret;
+		ret = devm_add_action_or_reset(dev, adt7x10_restore_config, data);
+		if (ret)
+			return ret;
 	}
 	dev_dbg(dev, "Config %02x\n", data->config);
 
 	/* Register sysfs hooks */
 	ret = sysfs_create_group(&dev->kobj, &adt7x10_group);
 	if (ret)
-		goto exit_restore;
+		return ret;
 
 	/*
 	 * The I2C device will already have it's own 'name' attribute, but for
@@ -375,8 +385,6 @@ int adt7x10_probe(struct device *dev, const char *name, int irq,
 		device_remove_file(dev, &dev_attr_name);
 exit_remove:
 	sysfs_remove_group(&dev->kobj, &adt7x10_group);
-exit_restore:
-	regmap_write(regmap, ADT7X10_CONFIG, data->oldconfig);
 	return ret;
 }
 EXPORT_SYMBOL_GPL(adt7x10_probe);
@@ -392,8 +400,6 @@ void adt7x10_remove(struct device *dev, int irq)
 	if (data->name)
 		device_remove_file(dev, &dev_attr_name);
 	sysfs_remove_group(&dev->kobj, &adt7x10_group);
-	if (data->oldconfig != data->config)
-		regmap_write(data->regmap, ADT7X10_CONFIG, data->oldconfig);
 }
 EXPORT_SYMBOL_GPL(adt7x10_remove);
 
Signature · 1 line
2.33.0
author maintainer
#3 lore raw

PATCH v4 3/6 hwmon: (adt7x10) Use devm_hwmon_device_register_with_info

In reply to Guenter Roeck

From: Cosmin Tanislav <[email protected]>

Describe the only available channel, implement read, write and is_visible callbacks.
Also, pass name to core driver for the i2c device so that it can be used to register hwmon device.

---
 drivers/hwmon/adt7410.c |   2 +-
 drivers/hwmon/adt7x10.c | 249 ++++++++++++++++++++--------------------
 2 files changed, 125 insertions(+), 126 deletions(-)
drivers/hwmon/adt7410.c +1 -1
index b1f4497dca05..81950a079c2f 100644
@@ -84,7 +84,7 @@ static int adt7410_i2c_probe(struct i2c_client *client)
 	if (IS_ERR(regmap))
 		return PTR_ERR(regmap);
 
-	return adt7x10_probe(&client->dev, NULL, client->irq, regmap);
+	return adt7x10_probe(&client->dev, client->name, client->irq, regmap);
 }
 
 static int adt7410_i2c_remove(struct i2c_client *client)
drivers/hwmon/adt7x10.c +124 -125

379 lines not on this page · raw message

Signature · 1 line
2.33.0
author maintainer
#4 lore raw

PATCH v4 4/6 hwmon: (adt7x10) Use devm_request_threaded_irq

In reply to Guenter Roeck

From: Cosmin Tanislav <[email protected]>

To simplify the core driver remove function.

---
 drivers/hwmon/adt7x10.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
drivers/hwmon/adt7x10.c +5 -5
index 9482fd8fff41..147c28b24167 100644
@@ -384,9 +384,11 @@ int adt7x10_probe(struct device *dev, const char *name, int irq,
 		return PTR_ERR(hdev);
 
 	if (irq > 0) {
-		ret = request_threaded_irq(irq, NULL, adt7x10_irq_handler,
-				IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
-				dev_name(dev), dev);
+		ret = devm_request_threaded_irq(dev, irq, NULL,
+						adt7x10_irq_handler,
+						IRQF_TRIGGER_FALLING |
+						IRQF_ONESHOT,
+						dev_name(dev), dev);
 		if (ret)
 			return ret;
 	}
@@ -397,8 +399,6 @@ EXPORT_SYMBOL_GPL(adt7x10_probe);
 
 void adt7x10_remove(struct device *dev, int irq)
 {
-	if (irq > 0)
-		free_irq(irq, dev);
 }
 EXPORT_SYMBOL_GPL(adt7x10_remove);
 
Signature · 1 line
2.33.0
author maintainer
#5 lore raw

PATCH v4 5/6 hwmon: (adt7x10) Remove empty driver removal callback

In reply to Guenter Roeck

From: Cosmin Tanislav <[email protected]>

Not used to do anything anymore.

---
 drivers/hwmon/adt7310.c | 7 -------
 drivers/hwmon/adt7410.c | 7 -------
 drivers/hwmon/adt7x10.c | 5 -----
 drivers/hwmon/adt7x10.h | 2 --
 4 files changed, 21 deletions(-)
drivers/hwmon/adt7310.c +0 -7
index a83092470bce..1efc0bdcceab 100644
@@ -142,12 +142,6 @@ static int adt7310_spi_probe(struct spi_device *spi)
 			     regmap);
 }
 
-static int adt7310_spi_remove(struct spi_device *spi)
-{
-	adt7x10_remove(&spi->dev, spi->irq);
-	return 0;
-}
-
 static const struct spi_device_id adt7310_id[] = {
 	{ "adt7310", 0 },
 	{ "adt7320", 0 },
@@ -161,7 +155,6 @@ static struct spi_driver adt7310_driver = {
 		.pm	= ADT7X10_DEV_PM_OPS,
 	},
 	.probe		= adt7310_spi_probe,
-	.remove		= adt7310_spi_remove,
 	.id_table	= adt7310_id,
 };
 module_spi_driver(adt7310_driver);
drivers/hwmon/adt7410.c +0 -7
index 81950a079c2f..aede5baca7b9 100644
@@ -87,12 +87,6 @@ static int adt7410_i2c_probe(struct i2c_client *client)
 	return adt7x10_probe(&client->dev, client->name, client->irq, regmap);
 }
 
-static int adt7410_i2c_remove(struct i2c_client *client)
-{
-	adt7x10_remove(&client->dev, client->irq);
-	return 0;
-}
-
 static const struct i2c_device_id adt7410_ids[] = {
 	{ "adt7410", 0 },
 	{ "adt7420", 0 },
@@ -107,7 +101,6 @@ static struct i2c_driver adt7410_driver = {
 		.pm	= ADT7X10_DEV_PM_OPS,
 	},
 	.probe_new	= adt7410_i2c_probe,
-	.remove		= adt7410_i2c_remove,
 	.id_table	= adt7410_ids,
 	.address_list	= I2C_ADDRS(0x48, 0x49, 0x4a, 0x4b),
 };
drivers/hwmon/adt7x10.c +0 -5
index 147c28b24167..ea8cd918bc22 100644
@@ -397,11 +397,6 @@ int adt7x10_probe(struct device *dev, const char *name, int irq,
 }
 EXPORT_SYMBOL_GPL(adt7x10_probe);
 
-void adt7x10_remove(struct device *dev, int irq)
-{
-}
-EXPORT_SYMBOL_GPL(adt7x10_remove);
-
 #ifdef CONFIG_PM_SLEEP
 
 static int adt7x10_suspend(struct device *dev)
drivers/hwmon/adt7x10.h +0 -2
index 55ff08bfe946..ba22c32c8355 100644
@@ -20,8 +20,6 @@ struct device;
 int adt7x10_probe(struct device *dev, const char *name, int irq,
 		  struct regmap *regmap);
 
-void adt7x10_remove(struct device *dev, int irq);
-
 #ifdef CONFIG_PM_SLEEP
 extern const struct dev_pm_ops adt7x10_dev_pm_ops;
 #define ADT7X10_DEV_PM_OPS (&adt7x10_dev_pm_ops)
Signature · 1 line
2.33.0
author maintainer
#6 lore raw

PATCH v4 6/6 hwmon: (adt7x10) Use hwmon_notify_event

In reply to Guenter Roeck

From: Cosmin Tanislav <[email protected]>

The hwmon subsystem provides means of notifying userspace about events. Use it.

[groeck: Pass hwmon device to interrupt handler]
---
 drivers/hwmon/adt7x10.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/hwmon/adt7x10.c +4 -4
index ea8cd918bc22..ce54bffab2ec 100644
@@ -87,11 +87,11 @@ static irqreturn_t adt7x10_irq_handler(int irq, void *private)
 		return IRQ_HANDLED;
 
 	if (status & ADT7X10_STAT_T_HIGH)
-		sysfs_notify(&dev->kobj, NULL, "temp1_max_alarm");
+		hwmon_notify_event(dev, hwmon_temp, hwmon_temp_max_alarm, 0);
 	if (status & ADT7X10_STAT_T_LOW)
-		sysfs_notify(&dev->kobj, NULL, "temp1_min_alarm");
+		hwmon_notify_event(dev, hwmon_temp, hwmon_temp_min_alarm, 0);
 	if (status & ADT7X10_STAT_T_CRIT)
-		sysfs_notify(&dev->kobj, NULL, "temp1_crit_alarm");
+		hwmon_notify_event(dev, hwmon_temp, hwmon_temp_crit_alarm, 0);
 
 	return IRQ_HANDLED;
 }
@@ -388,7 +388,7 @@ int adt7x10_probe(struct device *dev, const char *name, int irq,
 						adt7x10_irq_handler,
 						IRQF_TRIGGER_FALLING |
 						IRQF_ONESHOT,
-						dev_name(dev), dev);
+						dev_name(dev), hdev);
 		if (ret)
 			return ret;
 	}
Signature · 1 line
2.33.0
Tested-by Reviewed-by on v4 1/6
#7 lore raw
In reply to Guenter Roeck

Everything seems to function fine after the regmap conversion. Thank you for taking the time to work on this.

Quoted 38 lines · Guenter Roeck

V1 -> V2:
* add device managed action for restoring config
* merge multiple small related patches into a single patch that converts the driver to use devm_hwmon_device_register_with_info
* switch to devm_request_threaded_irq after switching to devm_hwmon_device_register_with_info to make sure that it is impossible for the interrupt handler to access the freed hwmon device
* drop core driver remove callback

V2 -> V3:
* merge patch that passes name from i2c driver into the devm_hwmon_device_register_with_info patch

v3 -> v4:
* Use regmap to hide chip specifics and to cache register values
* Various minor changes and fixes
* With the use of regmap, the bus device (bus_dev) is no longer needed, and the patch introducing it was dropped
* Hysteresis value calculations depend on two values: The associated register value and the hysteresis itself. All calculations must be protected to ensure that one value isn't changed during calculations. Add the missing locks to both the hysteresis read and write functions.
* Restoring the original configuration is only necessary if it was actually changed. Only call devm_add_action_or_reset() if that is the case. This also lets us drop the associated check in the action function.
* Use enum to index ADT7X10_REG_TEMP[]
* Check all attributes in is_visible function explicitly. While this is strictly speaking not necessary (the mode for unsupported attributes should not be requested), I find the explicit checks easier to understand and less error prone.
* Drop linux/hwmon-sysfs.h include and add missing linux/device.h include
* Squash patches 6/7 (pass hwinfo dev to irq handler) and patch 7/7 (use hwmon_notify_event) into a single patch; otherwise bus_dev would still be needed temporarily.

Note: This version of the series was module tested for ADT7410, but not

        on real hardware, and not for ADT7310/7320/7420.