Skip to contentSkip to conversation

hwmon: Driver for Maxim MAX6697 and compatibles

PATCH In review Guenter Roeck 3 participants 3 messages Jan 15, 2013 1 message not in the archive

Subsystems (4)
lore
Share & tools
lore

Guenter Roeck

author maintainer
#0 lore raw

PATCH v3 1/1 hwmon: Driver for Maxim MAX6697 and compatibles

Add support for MAX6581, MAX6602, MAX6622, MAX6636, MAX6689, MAX6693, MAX6694, MAX6697, MAX6698, and MAX6699 temperature sensors

---
v2:
- Add suppport for platform data and devicetree based chip initialization
- Drop S_IRUGOWU macro: s/S_IRUGOWU/S_IRUGO | S_IWUSR/
v3:
- Clarify devicetree documentation and platform data, indicating that all configuration information must be specified
- Fix alert-mask and over-temperature-mask values in devicetree example
- Align bit masks in configuration data with tempX index values
- Fix memset size in initialization code
- Handle extended temperature range for MAX6581 correctly
- Make data caching time dependent on chip type and configuration data
- Fix have_ext bit map for MAX6581
- Sort entries in max6697_chip_data[] array alphabetically
- Fix race condition when reading alarms
- Define and use constants for second index in data->temp array
- Rename show_temp11 to show_temp_input. Drop second index (always zero)
- Use DIV_ROUND_CLOSEST to convert milli-degrees to degrees in set_temp
- Handle error return in set_temp correctly
- Make max6697_attributes a two-dimensional array to reduce risk of errors when accessing it
- Drop temp1_fault as it is never used (local temperature sensor can not be faulty)
- Fix reading extended-range-enable property
- If no configuration data is specified, read extended range and resistance cancellation configuration from chip
- Drop some unnecessary comments
- Include linux/types.h in platform_data/max6697.h
- Drop unused define MAX6697_MAX_CONFIG_REG
- Add comments to fields in struct max6697_platform_data
- Add support to configure beta compensation on MAX6693 and MAX6694

 Documentation/devicetree/bindings/i2c/max6697.txt |   64 ++
 Documentation/hwmon/max6697                       |   58 ++
 drivers/hwmon/Kconfig                             |   11 +
 drivers/hwmon/Makefile                            |    1 +
 drivers/hwmon/max6697.c                           |  730 +++++++++++++++++++++
 include/linux/platform_data/max6697.h             |   36 +
 6 files changed, 900 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/i2c/max6697.txt
 create mode 100644 Documentation/hwmon/max6697
 create mode 100644 drivers/hwmon/max6697.c
 create mode 100644 include/linux/platform_data/max6697.h
Documentation/devicetree/bindings/i2c/max6697.txt new file +64 -0
new file mode 100644
index 0000000..c224b06
@@ -0,0 +1,64 @@
+max6697 properties
+
+Required properties:
+- compatible:
+	Should be one of
+		maxim,max6581
+		maxim,max6602
+		maxim,max6622
+		maxim,max6636
+		maxim,max6689
+		maxim,max6693
+		maxim,max6694
+		maxim,max6697
+		maxim,max6698
+		maxim,max6699
+- reg: I2C address
+
+Optional properties:
+
+- smbus-timeout-disable
+	Set to disable SMBus timeout. If not specified, SMBus timeout will be
+	enabled.
+- extended-range-enable
+	Only valid for MAX6581. Set to enable extended temperature range.
+	Extended temperature will be disabled if not specified.
+- beta-compensation-enable
+	Only valid for MAX6693 and MX6694. Set to enable beta compensation on
+	remote temperature channel 1.
+	beta compensation will be disabled if not specified.
+- alert-mask
+	Alert bit mask. Alert disabled for bits set.
+	Select bit 0 for local temperature, bit 1..7 for remote temperatures.
+	If not specified, alert will be enabled for all channels.
+- over-temperature-mask
+	Over-temperature bit mask. Over-temperature reporting disabled for
+	bits set.
+	Select bit 0 for local temperature, bit 1..7 for remote temperatures.
+	If not specified, over-temperature reporting will be enabled for all
+	channels.
+- resistance-cancellation
+	Boolean for all chips other than MAX6581. Set to enable resistance
+	cancellation on remote temperature channel 1.
+	For MAX6581, resistance cancellation enabled for all channels if
+	specified as boolean, otherwise as per bit mask specified.
+	Only supported for remote temperatures (bit 1..7).
+	If not specified, resistance cancellation will be disabled for all
+	channels.
+- transistor-ideality
+	For MAX6581 only. Two values; first is bit mask, second is ideality
+	select value as per MAX6581 data sheet. Select bit 1..7 for remote
+	channels.
+	Transistor ideality will be initialized to default (1.008) if not
+	specified.
+
+Example:
+
+temp-sensor@1a {
+	compatible = "maxim,max6697";
+	reg = <0x1a>;
+	smbus-timeout-disable;
+	resistance-cancellation;
+	alert-mask = <0x72>;
+	over-temperature-mask = <0x7f>;
+};
Documentation/hwmon/max6697 new file +58 -0

58 lines not on this page · raw message

drivers/hwmon/Kconfig +11 -0
index 32f238f..ef57572 100644
@@ -854,6 +854,17 @@ config SENSORS_MAX6650
 	  This driver can also be built as a module.  If so, the module
 	  will be called max6650.
 
+config SENSORS_MAX6697
+	tristate "Maxim MAX6697 and compatibles"
+	depends on I2C
+	help
+	  If you say yes here you get support for MAX6581, MAX6602, MAX6622,
+	  MAX6636, MAX6689, MAX6693, MAX6694, MAX6697, MAX6698, and MAX6699
+	  temperature sensor chips.
+
+	  This driver can also be built as a module.  If so, the module
+	  will be called max6697.
+
 config SENSORS_MCP3021
 	tristate "Microchip MCP3021 and compatibles"
 	depends on I2C
drivers/hwmon/Makefile +1 -0
index 5da2874..a37a82c 100644
@@ -99,6 +99,7 @@ obj-$(CONFIG_SENSORS_MAX197)	+= max197.o
 obj-$(CONFIG_SENSORS_MAX6639)	+= max6639.o
 obj-$(CONFIG_SENSORS_MAX6642)	+= max6642.o
 obj-$(CONFIG_SENSORS_MAX6650)	+= max6650.o
+obj-$(CONFIG_SENSORS_MAX6697)	+= max6697.o
 obj-$(CONFIG_SENSORS_MC13783_ADC)+= mc13783-adc.o
 obj-$(CONFIG_SENSORS_MCP3021)	+= mcp3021.o
 obj-$(CONFIG_SENSORS_NTC_THERMISTOR)	+= ntc_thermistor.o
drivers/hwmon/max6697.c new file +730 -0

730 lines not on this page · raw message

include/linux/platform_data/max6697.h new file +36 -0
new file mode 100644
index 0000000..c362098
@@ -0,0 +1,36 @@
+/*
+ * max6697.h
+ *     Copyright (c) 2012 Guenter Roeck <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ */
+
+#ifndef MAX6697_H
+#define MAX6697_H
+
+#include <linux/types.h>
+
+/*
+ * For all bit masks:
+ * bit 0:    local temperature
+ * bit 1..7: remote temperatures
+ */
+struct max6697_platform_data {
+	bool smbus_timeout_disable;	/* set to disable smbus timeouts */
+	bool extended_range_enable;	/* set to enable extended temp range */
+	bool beta_compensation;		/* set to enable beta compensation */
+	u8 alert_mask;			/* set bit to 1 to disable alert */
+	u8 over_temperature_mask;	/* set bit to 1 to disable */
+	u8 resistance_cancellation;	/* set bit to 0 to disable
+					 * bit mask for MAX6581,
+					 * boolean for other chips
+					 */
+	u8 ideality_mask;		/* set bit to 0 to disable */
+	u8 ideality_value;		/* transistor ideality as per
+					 * MAX6581 datasheet
+					 */
+};
+
+#endif /* MAX6697_H */
Signature · 1 line
1.7.9.7
#1 lore raw

Hi Guenter,

Quoted 27 lines · Guenter Roeck

Add support for MAX6581, MAX6602, MAX6622, MAX6636, MAX6689, MAX6693, MAX6694, MAX6697, MAX6698, and MAX6699 temperature sensors

---
v2:
- Add suppport for platform data and devicetree based chip initialization
- Drop S_IRUGOWU macro: s/S_IRUGOWU/S_IRUGO | S_IWUSR/
v3:
- Clarify devicetree documentation and platform data, indicating that all configuration information must be specified
- Fix alert-mask and over-temperature-mask values in devicetree example
- Align bit masks in configuration data with tempX index values
- Fix memset size in initialization code
- Handle extended temperature range for MAX6581 correctly
- Make data caching time dependent on chip type and configuration data
- Fix have_ext bit map for MAX6581
- Sort entries in max6697_chip_data[] array alphabetically
- Fix race condition when reading alarms
- Define and use constants for second index in data->temp array
- Rename show_temp11 to show_temp_input. Drop second index (always zero)
- Use DIV_ROUND_CLOSEST to convert milli-degrees to degrees in set_temp
- Handle error return in set_temp correctly
- Make max6697_attributes a two-dimensional array to reduce risk of errors when accessing it
- Drop temp1_fault as it is never used (local temperature sensor can not be faulty)

Quoted 8 lines

- Fix reading extended-range-enable property
- If no configuration data is specified, read extended range and resistance cancellation configuration from chip
- Drop some unnecessary comments
- Include linux/types.h in platform_data/max6697.h
- Drop unused define MAX6697_MAX_CONFIG_REG
- Add comments to fields in struct max6697_platform_data
- Add support to configure beta compensation on MAX6693 and MAX6694

Thanks for the update. I have a few more comments:

Documentation/devicetree/bindings/i2c/max6697.txt line 29

Quoted 10 lines
Documentation/devicetree/bindings/i2c/max6697.txt new file +7 -0
(...)
+- extended-range-enable
+	Only valid for MAX6581. Set to enable extended temperature range.
+	Extended temperature will be disabled if not specified.
+- beta-compensation-enable
+	Only valid for MAX6693 and MX6694. Set to enable beta compensation on
+	remote temperature channel 1.
+	beta compensation will be disabled if not specified.

Capital B.

Quoted 5 lines

(...)

Documentation/devicetree/bindings/i2c/max6697.txt new file +4 -0
+static ssize_t show_temp_input(struct device *dev,
+			       struct device_attribute *devattr, char *buf)
+{
+	int index = to_sensor_dev_attr(devattr)->index;

Documentation/devicetree/bindings/i2c/max6697.txt

Quoted 10 lines
Documentation/devicetree/bindings/i2c/max6697.txt new file +10 -0
+	struct max6697_data *data = max6697_update_device(dev);
+	int temp;
+
+	if (IS_ERR(data))
+		return PTR_ERR(data);
+
+	temp = data->temp[index][MAX6697_TEMP_INPUT];
+	temp <<= 3;
+	temp |= data->temp[index][MAX6697_TEMP_EXT] >> 5;
+	temp -= data->temp_offset << 3;

You could save one shifting by reordering the operations.

Quoted 12 lines
Documentation/devicetree/bindings/i2c/max6697.txt new file +11 -0
+
+	return sprintf(buf, "%d\n", temp * 125);
+}
(...)
+static ssize_t set_temp(struct device *dev,
+			struct device_attribute *devattr,
+			const char *buf, size_t count)
+{
+	int nr = to_sensor_dev_attr_2(devattr)->nr;
+	int index = to_sensor_dev_attr_2(devattr)->index;
+	struct i2c_client *client = to_i2c_client(dev);
+	struct max6697_data *data = i2c_get_clientdata(client);

Documentation/devicetree/bindings/i2c/max6697.txt

Quoted 10 lines
Documentation/devicetree/bindings/i2c/max6697.txt new file +10 -0
+	long temp;
+	int ret;
+
+	ret = kstrtol(buf, 10, &temp);
+	if (ret < 0)
+		return ret;
+
+	mutex_lock(&data->update_lock);
+	temp = clamp_val(DIV_ROUND_CLOSEST(temp, 1000), -data->temp_offset,
+			 127);

This 127 is OK for all chips but the MAX6851. I agree that the temperature data format table in the datasheet looks wrong, but the description of the EXTRANGE configuration bit description sheds some light:

"Extended-Range Enable Bit. Set bit 1 to logic 1 to set the temperature and limit data range to -64°C to +191°C. Set bit 1 to logic 0 to set the range to 0°C to +255°C."

Quoted 19 lines
Documentation/devicetree/bindings/i2c/max6697.txt new file +18 -0
+	temp += data->temp_offset;
+	data->temp[nr][index] = temp;
+	ret = i2c_smbus_write_byte_data(client,
+					index == 2 ? MAX6697_REG_MAX[nr]
+						   : MAX6697_REG_CRIT[nr],
+					temp);
+	mutex_unlock(&data->update_lock);
+
+	return ret < 0 ? ret : count;
+}
(...)
+static int max6697_init_chip(struct i2c_client *client)
+{
+	struct max6697_data *data = i2c_get_clientdata(client);
+	struct max6697_platform_data *pdata = dev_get_platdata(&client->dev);
+	struct max6697_platform_data p;
+	const struct max6697_chip_data *chip = data->chip;
+	int factor = chip->channels;
+	int ret;

Documentation/devicetree/bindings/i2c/max6697.txt

Quoted 10 lines
Documentation/devicetree/bindings/i2c/max6697.txt new file +10 -0
+	u8 reg;
+
+	/*
+	 * Don't touch configuration if neither platform data nor OF
+	 * configuration was specified. If that is the case, use the
+	 * current chip configuration.
+	 */
+	if (!pdata && !client->dev.of_node) {
+		reg = i2c_smbus_read_byte_data(client, MAX6697_REG_CONFIG);
+		if (reg < 0)

reg is unsigned. I don't get why gcc doesn't complain...

Documentation/devicetree/bindings/i2c/max6697.txt

Quoted 4 lines
Documentation/devicetree/bindings/i2c/max6697.txt new file +4 -0
+			return reg;
+		if (data->type == max6581) {
+			data->temp_offset =
+			  (reg & MAX6581_CONF_EXTENDED) ? 64 : 0;

data->temp_offset is 0 at this point so an "if" construct should be more efficient.

Documentation/devicetree/bindings/i2c/max6697.txt

Quoted 2 lines
Documentation/devicetree/bindings/i2c/max6697.txt new file +2 -0
+			reg = i2c_smbus_read_byte_data(client,
+						       MAX6581_REG_RESISTANCE);

reg is unsigned.

Quoted 24 lines
Documentation/devicetree/bindings/i2c/max6697.txt new file +9 -0
+			if (reg < 0)
+				return reg;
+			factor += hweight8(reg);
+		} else {
+			if (reg & MAX6697_CONF_RESISTANCE)
+				factor++;
+		}
+		goto done;
+	}
(...)
include/linux/platform_data/max6697.h new file +11 -0
@@ -0,0 +1,36 @@
+/*
+ * max6697.h
+ *     Copyright (c) 2012 Guenter Roeck <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ */
+
+#ifndef MAX6697_H
+#define MAX6697_H

include/linux/platform_data/max6697.h line 21

Quoted 10 lines
include/linux/platform_data/max6697.h new file +10 -0
+
+#include <linux/types.h>
+
+/*
+ * For all bit masks:
+ * bit 0:    local temperature
+ * bit 1..7: remote temperatures
+ */
+struct max6697_platform_data {
+	bool smbus_timeout_disable;	/* set to disable smbus timeouts */

"SMBus"

Signature · 1 line
Jean Delvare
#3 lore raw
In reply to a message not in the archive
Quoted 126 lines · Jean Delvare

Hi Guenter,

Quoted 27 lines · Guenter Roeck

Add support for MAX6581, MAX6602, MAX6622, MAX6636, MAX6689, MAX6693, MAX6694, MAX6697, MAX6698, and MAX6699 temperature sensors

---
v2:
- Add suppport for platform data and devicetree based chip initialization
- Drop S_IRUGOWU macro: s/S_IRUGOWU/S_IRUGO | S_IWUSR/
v3:
- Clarify devicetree documentation and platform data, indicating that all configuration information must be specified
- Fix alert-mask and over-temperature-mask values in devicetree example
- Align bit masks in configuration data with tempX index values
- Fix memset size in initialization code
- Handle extended temperature range for MAX6581 correctly
- Make data caching time dependent on chip type and configuration data
- Fix have_ext bit map for MAX6581
- Sort entries in max6697_chip_data[] array alphabetically
- Fix race condition when reading alarms
- Define and use constants for second index in data->temp array
- Rename show_temp11 to show_temp_input. Drop second index (always zero)
- Use DIV_ROUND_CLOSEST to convert milli-degrees to degrees in set_temp
- Handle error return in set_temp correctly
- Make max6697_attributes a two-dimensional array to reduce risk of errors when accessing it
- Drop temp1_fault as it is never used (local temperature sensor can not be faulty)

Quoted 8 lines

- Fix reading extended-range-enable property
- If no configuration data is specified, read extended range and resistance cancellation configuration from chip
- Drop some unnecessary comments
- Include linux/types.h in platform_data/max6697.h
- Drop unused define MAX6697_MAX_CONFIG_REG
- Add comments to fields in struct max6697_platform_data
- Add support to configure beta compensation on MAX6693 and MAX6694

Thanks for the update. I have a few more comments:

Documentation/devicetree/bindings/i2c/max6697.txt line 29

Quoted 10 lines
Documentation/devicetree/bindings/i2c/max6697.txt new file +7 -0
(...)
+- extended-range-enable
+	Only valid for MAX6581. Set to enable extended temperature range.
+	Extended temperature will be disabled if not specified.
+- beta-compensation-enable
+	Only valid for MAX6693 and MX6694. Set to enable beta compensation on
+	remote temperature channel 1.
+	beta compensation will be disabled if not specified.

Capital B.

Quoted 5 lines

(...)

Documentation/devicetree/bindings/i2c/max6697.txt new file +4 -0
+static ssize_t show_temp_input(struct device *dev,
+			       struct device_attribute *devattr, char *buf)
+{
+	int index = to_sensor_dev_attr(devattr)->index;

Documentation/devicetree/bindings/i2c/max6697.txt

Quoted 10 lines
Documentation/devicetree/bindings/i2c/max6697.txt new file +10 -0
+	struct max6697_data *data = max6697_update_device(dev);
+	int temp;
+
+	if (IS_ERR(data))
+		return PTR_ERR(data);
+
+	temp = data->temp[index][MAX6697_TEMP_INPUT];
+	temp <<= 3;
+	temp |= data->temp[index][MAX6697_TEMP_EXT] >> 5;
+	temp -= data->temp_offset << 3;

You could save one shifting by reordering the operations.

Quoted 12 lines
Documentation/devicetree/bindings/i2c/max6697.txt new file +11 -0
+
+	return sprintf(buf, "%d\n", temp * 125);
+}
(...)
+static ssize_t set_temp(struct device *dev,
+			struct device_attribute *devattr,
+			const char *buf, size_t count)
+{
+	int nr = to_sensor_dev_attr_2(devattr)->nr;
+	int index = to_sensor_dev_attr_2(devattr)->index;
+	struct i2c_client *client = to_i2c_client(dev);
+	struct max6697_data *data = i2c_get_clientdata(client);

Documentation/devicetree/bindings/i2c/max6697.txt

Quoted 10 lines
Documentation/devicetree/bindings/i2c/max6697.txt new file +10 -0
+	long temp;
+	int ret;
+
+	ret = kstrtol(buf, 10, &temp);
+	if (ret < 0)
+		return ret;
+
+	mutex_lock(&data->update_lock);
+	temp = clamp_val(DIV_ROUND_CLOSEST(temp, 1000), -data->temp_offset,
+			 127);

This 127 is OK for all chips but the MAX6851. I agree that the temperature data format table in the datasheet looks wrong, but the description of the EXTRANGE configuration bit description sheds some light:

"Extended-Range Enable Bit. Set bit 1 to logic 1 to set the temperature and limit data range to -64°C to +191°C. Set bit 1 to logic 0 to set the range to 0°C to +255°C."

Quoted 23 lines
Documentation/devicetree/bindings/i2c/max6697.txt new file +22 -0
+	temp += data->temp_offset;
+	data->temp[nr][index] = temp;
+	ret = i2c_smbus_write_byte_data(client,
+					index == 2 ? MAX6697_REG_MAX[nr]
+						   : MAX6697_REG_CRIT[nr],
+					temp);
+	mutex_unlock(&data->update_lock);
+
+	return ret < 0 ? ret : count;
+}
(...)
+static int max6697_init_chip(struct i2c_client *client)
+{
+	struct max6697_data *data = i2c_get_clientdata(client);
+	struct max6697_platform_data *pdata = dev_get_platdata(&client->dev);
+	struct max6697_platform_data p;
+	const struct max6697_chip_data *chip = data->chip;
+	int factor = chip->channels;
+	int ret;
+	u8 reg;
+
+	/*
+	 * Don't touch configuration if neither platform data nor OF
Quoted 9 lines

Documentation/devicetree/bindings/i2c/max6697.txt

Quoted 6 lines
Documentation/devicetree/bindings/i2c/max6697.txt new file +6 -0
+	 * configuration was specified. If that is the case, use the
+	 * current chip configuration.
+	 */
+	if (!pdata && !client->dev.of_node) {
+		reg = i2c_smbus_read_byte_data(client, MAX6697_REG_CONFIG);
+		if (reg < 0)

reg is unsigned. I don't get why gcc doesn't complain...

It does complain if one compiles with W=1 ... I keep forgetting to do that.

Thanks a lot again!

Guenter

Quoted 53 lines

Documentation/devicetree/bindings/i2c/max6697.txt

Quoted 4 lines
Documentation/devicetree/bindings/i2c/max6697.txt new file +4 -0
+			return reg;
+		if (data->type == max6581) {
+			data->temp_offset =
+			  (reg & MAX6581_CONF_EXTENDED) ? 64 : 0;

data->temp_offset is 0 at this point so an "if" construct should be more efficient.

Documentation/devicetree/bindings/i2c/max6697.txt

Quoted 2 lines
Documentation/devicetree/bindings/i2c/max6697.txt new file +2 -0
+			reg = i2c_smbus_read_byte_data(client,
+						       MAX6581_REG_RESISTANCE);

reg is unsigned.

Quoted 24 lines
Documentation/devicetree/bindings/i2c/max6697.txt new file +9 -0
+			if (reg < 0)
+				return reg;
+			factor += hweight8(reg);
+		} else {
+			if (reg & MAX6697_CONF_RESISTANCE)
+				factor++;
+		}
+		goto done;
+	}
(...)
include/linux/platform_data/max6697.h new file +11 -0
@@ -0,0 +1,36 @@
+/*
+ * max6697.h
+ *     Copyright (c) 2012 Guenter Roeck <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ */
+
+#ifndef MAX6697_H
+#define MAX6697_H

include/linux/platform_data/max6697.h line 21

Quoted 10 lines
include/linux/platform_data/max6697.h new file +10 -0
+
+#include <linux/types.h>
+
+/*
+ * For all bit masks:
+ * bit 0:    local temperature
+ * bit 1..7: remote temperatures
+ */
+struct max6697_platform_data {
+	bool smbus_timeout_disable;	/* set to disable smbus timeouts */

"SMBus"

Signature · 1 line
Jean Delvare