Skip to contentSkip to conversation

hwmon: add driver for ADT7411 voltage &

PATCH In review Wolfram Sang 2 participants 2 messages Feb 8, 2010 – Feb 9, 2010

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 hwmon: add driver for ADT7411 voltage &

Add basic support for the ADT7411. Reads out all conversion results (via I2C, SPI yet missing) and allows some on-the-fly configuration. Tested with a custom board.

---

Changes since V2:

* put all cache handling into adt7411_show_input()
* removed adt7411_read_vdd which became obsolete
* now vref is cached instead of vdd to minimize calculations
* forced update when configuration bits change
* fixed minor issues pointed out by Jean

Changes since V1:

* simplified reading 10-bit, no retries
* modifying a configuration bit is now protected
* no initial configuration is performed except START (which is what the other ADT drivers do)
* when vdd is the reference, use a cached value for it
* added a detect mechanism
* style issues and typos found by Jean were fixed

 Documentation/hwmon/adt7411 |   42 +++++
 drivers/hwmon/Kconfig       |   10 ++
 drivers/hwmon/Makefile      |    1 +
 drivers/hwmon/adt7411.c     |  357 +++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 410 insertions(+), 0 deletions(-)
 create mode 100644 Documentation/hwmon/adt7411
 create mode 100644 drivers/hwmon/adt7411.c
Documentation/hwmon/adt7411 new file +41 -0
new file mode 100644
index 0000000..1632960
@@ -0,0 +1,42 @@
+Kernel driver adt7411
+==========+
+Supported chips:
+  * Analog Devices ADT7411
+    Prefix: 'adt7411'
+    Addresses scanned: 0x48, 0x4a, 0x4b
+    Datasheet: Publicly available at the Analog Devices website
+
+Author: Wolfram Sang (based on adt7470 by Darrick J. Wong)
+
+Description
+-----------
+
+This driver implements support for the Analog Devices ADT7411 chip. There may
+be other chips that implement this interface.
+
+The ADT7411 can use an I2C/SMBus compatible 2-wire interface or an
+SPI-compatible 4-wire interface. It provides a 10-bit analog to digital
+converter which measures 1 temperature, vdd and 8 input voltages. It has an
+internal temperature sensor, but an external one can also be connected (one
+loses 2 inputs then). There are high- and low-limit registers for all inputs.
+
+Check the datasheet for details.
+
+sysfs-Interface
+---------------
+
+in0_input	- vdd voltage input
+in[1-8]_input	- analog 1-8 input
+temp1_input	- temperature input
+
+Besides standard interfaces, this driver adds (0 = off, 1 = on):
+
+  adc_ref_vdd	- Use vdd as reference instead of 2.25 V
+  fast_sampling	- Sample at 22.5 kHz instead of 1.4 kHz, but drop filters
+  no_average	- Turn off averaging over 16 samples
+
+Notes
+-----
+
+SPI, external temperature sensor and limit registers are not supported yet.
drivers/hwmon/Kconfig +10 -0
index 68cf877..c91dc98 100644
@@ -170,6 +170,16 @@ config SENSORS_ADM9240
 	  This driver can also be built as a module.  If so, the module
 	  will be called adm9240.
 
+config SENSORS_ADT7411
+	tristate "Analog Devices ADT7411"
+	depends on I2C && EXPERIMENTAL
+	help
+	  If you say yes here you get support for the Analog Devices
+	  ADT7411 voltage and temperature monitoring chip.
+
+	  This driver can also be built as a module. If so, the module
+	  will be called adt7411.
+
 config SENSORS_ADT7462
 	tristate "Analog Devices ADT7462"
 	depends on I2C && EXPERIMENTAL
drivers/hwmon/Makefile +1 -0
index 4bc215c..5fe67bf 100644
@@ -29,6 +29,7 @@ obj-$(CONFIG_SENSORS_ADM1029)	+= adm1029.o
 obj-$(CONFIG_SENSORS_ADM1031)	+= adm1031.o
 obj-$(CONFIG_SENSORS_ADM9240)	+= adm9240.o
 obj-$(CONFIG_SENSORS_ADS7828)	+= ads7828.o
+obj-$(CONFIG_SENSORS_ADT7411)	+= adt7411.o
 obj-$(CONFIG_SENSORS_ADT7462)	+= adt7462.o
 obj-$(CONFIG_SENSORS_ADT7470)	+= adt7470.o
 obj-$(CONFIG_SENSORS_ADT7473)	+= adt7473.o
drivers/hwmon/adt7411.c new file +357 -0

357 lines not on this page · raw message

Signature · 7 lines
1.6.5


_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors
#1 lore raw

Hi Wolfram,

Quoted 7 lines · Wolfram Sang

Add basic support for the ADT7411. Reads out all conversion results (via I2C, SPI yet missing) and allows some on-the-fly configuration. Tested with a custom board.

Patch applied, thanks.

Still, I think there is a locking issue in your driver, which I would like you to address with an incremental patch. The problem is in function adt7411_show_input():

Quoted 28 lines
drivers/hwmon/adt7411.c new file +28 -0
+static ssize_t adt7411_show_input(struct device *dev,
+				  struct device_attribute *attr, char *buf)
+{
+	int nr = to_sensor_dev_attr(attr)->index;
+	struct i2c_client *client = to_i2c_client(dev);
+	struct adt7411_data *data = i2c_get_clientdata(client);
+	int val;
+	u8 lsb_reg, lsb_shift;
+
+	if (time_after_eq(jiffies, data->next_update)) {
+		val = i2c_smbus_read_byte_data(client, ADT7411_REG_CFG3);
+		if (val < 0)
+			return val;
+		data->ref_is_vdd = val & ADT7411_CFG3_REF_VDD;
+
+		if (data->ref_is_vdd) {
+			val = adt7411_read_10_bit(client,
+					ADT7411_REG_INT_TEMP_VDD_LSB,
+					ADT7411_REG_VDD_MSB, 2);
+			if (val < 0)
+				return val;
+
+			data->vref_cached = val * 7000 / 1024;
+		} else {
+			data->vref_cached = 2250;
+		}
+
+		data->next_update = jiffies + HZ;

drivers/hwmon/adt7411.c line 173

Quoted 10 lines
drivers/hwmon/adt7411.c new file +10 -0
+	}
+
+	lsb_reg = ADT7411_REG_EXT_TEMP_AIN14_LSB + (nr >> 2);
+	lsb_shift = 2 * (nr & 0x03);
+	val = adt7411_read_10_bit(client, lsb_reg,
+			ADT7411_REG_EXT_TEMP_AIN1_MSB + nr, lsb_shift);
+
+	return val < 0 ? val :
+			sprintf(buf, "%u\n", val * data->vref_cached / 1024);
+}

You cache the vref in data->ref_is_vdd (for no good reason BTW) and data->vref_cached. The freshness of the cache is stored in data->next_update. Now, what would happen if two readers access this function simultaneously at a time the cache needs to be updated? Both will read registers ADT7411_REG_CFG3, and possibly ADT7411_REG_INT_TEMP_VDD_LSB and ADT7411_REG_VDD_MSB, to refresh the cache. This is both slow (voiding the point of having a cache) and
unsafe: both might try to set data->vref_cached and data->vref_cached, which isn't guaranteed to succeed because these aren't atomic types. Even reading data->vref_cached at the end of this function is not guaranteed to succeed in the absence of locking.

So I would like you to surround all access to the cached values and their timestamp with locking, as is done in many other hwmon drivers. And you might want to stop caching data->ref_is_vdd while you're there...

Signature · 6 lines
Jean Delvare

_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors