Skip to contentSkip to conversation

hwmon: Driver for Maxim MAX31790

PATCH In review Il Han 2 participants 16 messages Jul 29, 2015 – Aug 30, 2015

Subsystems (2)
lore
Share & tools
lore

Il Han

author
#0 lore raw

PATCH 1/1 hwmon: Driver for Maxim MAX31790

The driver supports the Maxim MAX31790.

---
 drivers/hwmon/Kconfig    |   9 +
 drivers/hwmon/Makefile   |   1 +
 drivers/hwmon/max31790.c | 806 +++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 816 insertions(+)
 create mode 100644 drivers/hwmon/max31790.c
drivers/hwmon/Kconfig +9 -0
index 7c65b73..fcd1c93 100644
@@ -840,6 +840,15 @@ config SENSORS_MAX6697
 	  This driver can also be built as a module.  If so, the module
 	  will be called max6697.
 
+config SENSORS_MAX31790
+	tristate "Maxim MAX31790 sensor chip"
+	depends on I2C
+	help
+	  If you say yes here you get support for the MAX31790 sensor chips.
+
+	  This driver can also be built as a module.  If so, the module
+	  will be called max31790.
+
 config SENSORS_HTU21
 	tristate "Measurement Specialties HTU21D humidity/temperature sensors"
 	depends on I2C
drivers/hwmon/Makefile +1 -0
index 9e0f3dd..01855ee 100644
@@ -115,6 +115,7 @@ 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_MAX31790)	+= max31790.o
 obj-$(CONFIG_SENSORS_MC13783_ADC)+= mc13783-adc.o
 obj-$(CONFIG_SENSORS_MCP3021)	+= mcp3021.o
 obj-$(CONFIG_SENSORS_MENF21BMC_HWMON) += menf21bmc_hwmon.o
drivers/hwmon/max31790.c new file +798 -0

798 lines not on this page · raw message

Signature · 7 lines
2.4.1


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

Hi,

Quoted 3 lines · Il Han

The driver supports the Maxim MAX31790.

Can you provide the output of i2cdump for this chip ? It will help module testing the driver.

Further comments below.

Thanks,
Guenter

Quoted 61 lines
---
  drivers/hwmon/Kconfig    |   9 +
  drivers/hwmon/Makefile   |   1 +
  drivers/hwmon/max31790.c | 806 +++++++++++++++++++++++++++++++++++++++++++++++
  3 files changed, 816 insertions(+)

create mode 100644 drivers/hwmon/max31790.c

drivers/hwmon/Kconfig +9 -0
index 7c65b73..fcd1c93 100644
@@ -840,6 +840,15 @@ config SENSORS_MAX6697
  	  This driver can also be built as a module.  If so, the module
  	  will be called max6697.
 
+config SENSORS_MAX31790
+	tristate "Maxim MAX31790 sensor chip"
+	depends on I2C
+	help
+	  If you say yes here you get support for the MAX31790 sensor chips.
+
+	  This driver can also be built as a module.  If so, the module
+	  will be called max31790.
+
  config SENSORS_HTU21
  	tristate "Measurement Specialties HTU21D humidity/temperature sensors"
  	depends on I2C
drivers/hwmon/Makefile +1 -0
index 9e0f3dd..01855ee 100644
@@ -115,6 +115,7 @@ 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_MAX31790)	+= max31790.o
  obj-$(CONFIG_SENSORS_MC13783_ADC)+= mc13783-adc.o
  obj-$(CONFIG_SENSORS_MCP3021)	+= mcp3021.o
  obj-$(CONFIG_SENSORS_MENF21BMC_HWMON) += menf21bmc_hwmon.o
drivers/hwmon/max31790.c new file +16 -0
new file mode 100644
index 0000000..8c4ff08
@@ -0,0 +1,806 @@
+/*
+ * max31790.c - Part of lm_sensors, Linux kernel modules for hardware
+ *             monitoring.
+ *
+ * (C) 2015 by Il Han <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */

drivers/hwmon/max31790.c line 26

Quoted 10 lines
drivers/hwmon/max31790.c new file +10 -0
+
+#include <linux/module.h>
+#include <linux/init.h>
+#include <linux/slab.h>
+#include <linux/jiffies.h>
+#include <linux/i2c.h>
+#include <linux/hwmon.h>
+#include <linux/hwmon-sysfs.h>
+#include <linux/err.h>
+

Please list include files in alphabetic order. Not strictly required, but reduces potential conflicts later on.

drivers/hwmon/max31790.c line 27

Quoted 1 line
drivers/hwmon/max31790.c new file +1 -0
+enum chips { max31790 };

Not needed; the driver only supports a single chip.

drivers/hwmon/max31790.c line 31

Quoted 4 lines
drivers/hwmon/max31790.c new file +4 -0
+
+/* MAX31790 registers */
+#define MAX31790_REG_GLOBAL_CONFIG	0x00
+#define MAX31790_REG_PWM_FREQ		0x01

Not used. Please drop or implement pwmX_freq.

Quoted 34 lines
drivers/hwmon/max31790.c new file +34 -0
+#define MAX31790_REG_FAN_CONFIG(ch)	(0x02 + (ch))
+#define MAX31790_REG_FAN_DYNAMICS(ch)	(0x08 + (ch))
+#define MAX31790_REG_FAN_FAULT_STATUS2	0x10
+#define MAX31790_REG_FAN_FAULT_STATUS1	0x11
+#define MAX31790_REG_FAN_FAULT_MASK2	0x12
+#define MAX31790_REG_FAN_FAULT_MASK1	0x13
+#define MAX31790_REG_TACH_COUNT(ch)	(0x18 + (ch) * 2)
+#define MAX31790_REG_PWM_DUTY_CYCLE(ch)	(0x30 + (ch) * 2)
+#define MAX31790_REG_PWMOUT(ch)		(0x40 + (ch) * 2)
+#define MAX31790_REG_TARGET_COUNT(ch)	(0x50 + (ch) * 2)
+#define MAX31790_REG_WINDOW(ch)		(0x60 + (ch))
+
+/* Global Config register bits */
+#define MAX31790_GL_CFG_STANDBY		0x80
+#define MAX31790_GL_CFG_RESET		0x40
+#define MAX31790_GL_CFG_BUS_TIMEOUT	0x20
+#define MAX31790_GL_CFG_OSC		0x08
+#define MAX31790_GL_CFG_WD_SHIFT	1
+#define MAX31790_GL_CFG_WD_MASK		0x06
+#define MAX31790_GL_CFG_WD_DISABLE	0x00
+#define MAX31790_GL_CFG_WD_5S		0x02
+#define MAX31790_GL_CFG_WD_10S		0x04
+#define MAX31790_GL_CFG_WD_30S		0x06
+#define MAX31790_GL_CFG_WD_STATUS	0x01
+
+/* Fan Config register bits */
+#define MAX31790_FAN_CFG_MODE_MASK	0x80
+#define MAX31790_FAN_CFG_PWM_MODE	0x00
+#define MAX31790_FAN_CFG_RPM_MODE	0x80
+#define MAX31790_FAN_CFG_MONITOR_ONLY	0x10
+#define MAX31790_FAN_CFG_TACH_INPUT_EN	0x08
+#define MAX31790_FAN_CFG_LOCKED_ROTOR	0x04
+#define MAX31790_FAN_CFG_TACH_INPUT	0x01
+

drivers/hwmon/max31790.c line 75

Quoted 10 lines
drivers/hwmon/max31790.c new file +10 -0
+/* Fan Dynamics register bits */
+#define MAX31790_FAN_DYNAMICS_SR(reg)	(((reg) >> 5) & 0x7)
+
+/* number of tachometer pulses per revolution */
+#define MAX31790_TACHOMETER_PULSE	2
+
+#define NR_CHANNEL			6
+
+#define FAN_RPM_MIN			480
+#define FAN_RPM_MAX			30000

Where do those values come from ?

drivers/hwmon/max31790.c line 83

Quoted 8 lines
drivers/hwmon/max31790.c new file +8 -0
+
+/*
+ * Client data (each client gets its own)
+ */
+struct max31790_data {
+	struct i2c_client *client;
+	struct mutex update_lock;
+	char valid; /* zero until following fields are valid */

Please use bool.

Quoted 27 lines
drivers/hwmon/max31790.c new file +27 -0
+	unsigned long last_updated; /* in jiffies */
+
+	/* register values */
+	u8 global_config;
+	u8 fan_config[NR_CHANNEL];
+	u8 fan_dynamics[NR_CHANNEL];
+	u8 fault_status2;
+	u8 fault_status1;
+	u8 fault_mask2;
+	u8 fault_mask1;
+	u16 tach[NR_CHANNEL];
+	u16 pwm[NR_CHANNEL];
+	u16 target_count[NR_CHANNEL];
+};
+
+static struct max31790_data *max31790_update_device(struct device *dev)
+{
+	struct max31790_data *data = dev_get_drvdata(dev);
+	struct i2c_client *client = data->client;
+	int i;
+
+	mutex_lock(&data->update_lock);
+
+	if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
+		data->global_config = i2c_smbus_read_byte_data(client,
+				MAX31790_REG_GLOBAL_CONFIG);
+		data->fault_status1 = i2c_smbus_read_byte_data(client,

drivers/hwmon/max31790.c line 120

Quoted 10 lines
drivers/hwmon/max31790.c new file +10 -0
+				MAX31790_REG_FAN_FAULT_STATUS1);
+		data->fault_mask1 = i2c_smbus_read_byte_data(client,
+				MAX31790_REG_FAN_FAULT_MASK1);
+		for (i = 0; i < NR_CHANNEL; i++) {
+			data->fan_config[i] = i2c_smbus_read_byte_data(client,
+					MAX31790_REG_FAN_CONFIG(i));
+			data->fan_dynamics[i] = i2c_smbus_read_byte_data(client,
+					MAX31790_REG_FAN_DYNAMICS(i));
+			data->tach[i] = swab16(i2c_smbus_read_word_data(client,
+					MAX31790_REG_TACH_COUNT(i)));

Consider using i2c_smbus_read_word_data_swapped().

drivers/hwmon/max31790.c line 124

Quoted 4 lines
drivers/hwmon/max31790.c new file +4 -0
+			data->pwm[i] = swab16(i2c_smbus_read_word_data(client,
+					MAX31790_REG_PWMOUT(i)));
+			data->target_count[i] > +					swab16(i2c_smbus_read_word_data(client,
+					MAX31790_REG_TARGET_COUNT(i)));

Several of those attributes do not change values and can be cached continuously, ie only have to be read once.

Quoted 30 lines
drivers/hwmon/max31790.c new file +30 -0
+		}
+
+		data->last_updated = jiffies;
+		data->valid = 1;
+	}
+
+	mutex_unlock(&data->update_lock);
+
+	return data;
+}
+
+int get_speed_range(unsigned char fan_dynamics)
+{
+	int sr;
+
+	switch (MAX31790_FAN_DYNAMICS_SR(fan_dynamics)) {
+	case 0x0:
+		sr = 1;
+		break;
+	case 0x1:
+		sr = 2;
+		break;
+	case 0x2:
+		sr = 4;
+		break;
+	case 0x3:
+		sr = 8;
+		break;
+	case 0x4:
+		sr = 16;

drivers/hwmon/max31790.c line 164

Quoted 10 lines
drivers/hwmon/max31790.c new file +10 -0
+		break;
+	case 0x5:
+	case 0x6:
+	case 0x7:
+		sr = 32;
+		break;
+	default:
+		return -EINVAL;
+	}
+

Given that the sr range is 3 bit, an array would be simpler.

static const u8 sr[8] = { 1, 2, 4, 8, 16, 32, 32, 32 };

u8 get_speed_range(u8 fan_dynamics)

{
	return sr[MAX31790_FAN_DYNAMICS_SR(fan_dynamics)];
}

It might be useful to report the configured speed range as fanX_div, and make it configurable.

drivers/hwmon/max31790.c line 169

Quoted 5 lines
drivers/hwmon/max31790.c new file +5 -0
+	return sr;
+}
+
+static ssize_t get_fan(struct device *dev,
+		struct device_attribute *devattr, char *buf)

Please use standard continuation line alignment (align with '(' on the previous line).

drivers/hwmon/max31790.c line 177

Quoted 8 lines
drivers/hwmon/max31790.c new file +8 -0
+{
+	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
+	struct max31790_data *data = max31790_update_device(dev);
+	int np = MAX31790_TACHOMETER_PULSE;
+	int sr = get_speed_range(data->fan_dynamics[attr->index]);
+	int rpm;
+
+	rpm = (60 * sr * 8192) / (np * (data->tach[attr->index] >> 5));

You use direct values for everything but the pulse count. Might as well just use '2' for that as well. It is quite useless, though, since

	(2 * (data->tach[attr->index] >> 5))
is identical to
	(data->tach[attr->index] >> 4)

which you might as well use directly.

I would suggest to perform the calculation in a separate function, such as rmp_from_reg() and rpm_to_reg(), to avoid repetition in get_fan() and {get,set}_fan_target().

Quoted 79 lines
drivers/hwmon/max31790.c new file +79 -0
+	return sprintf(buf, "%d\n", rpm);
+}
+
+static ssize_t get_fan_target(struct device *dev,
+		struct device_attribute *devattr, char *buf)
+{
+	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
+	struct max31790_data *data = max31790_update_device(dev);
+	int np = MAX31790_TACHOMETER_PULSE;
+	int sr = get_speed_range(data->fan_dynamics[attr->index]);
+	int rpm;
+
+	rpm = (60 * sr * 8192) / (np * (data->target_count[attr->index] >> 5));
+
+	return sprintf(buf, "%d\n", rpm);
+}
+
+static ssize_t set_fan_target(struct device *dev,
+		struct device_attribute *devattr, const char *buf, size_t count)
+{
+	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
+	struct i2c_client *client = to_i2c_client(dev);
+	struct max31790_data *data = i2c_get_clientdata(client);
+	int np = MAX31790_TACHOMETER_PULSE;
+	int sr;
+	unsigned long rpm;
+	int target_count;
+	int err;
+
+	err = kstrtoul(buf, 10, &rpm);
+	if (err)
+		return err;
+
+	mutex_lock(&data->update_lock);
+
+	rpm = clamp_val(rpm, FAN_RPM_MIN, FAN_RPM_MAX);
+	sr = get_speed_range(data->fan_dynamics[attr->index]);
+	target_count = (60 * sr * 8192) / (np * rpm);
+	target_count = clamp_val(target_count, 0x0, 0x7FF);
+
+	data->target_count[attr->index] = (u16)target_count << 5;
+
+	i2c_smbus_write_word_data(client,
+			MAX31790_REG_TARGET_COUNT(attr->index),
+			swab16(data->target_count[attr->index]));
+
+	mutex_unlock(&data->update_lock);
+
+	return count;
+}
+
+static ssize_t get_pwm(struct device *dev,
+		struct device_attribute *devattr, char *buf)
+{
+	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
+	struct max31790_data *data = max31790_update_device(dev);
+	int pwm;
+
+	pwm = data->pwm[attr->index] >> 7;
+
+	return sprintf(buf, "%d\n", pwm);
+}
+
+static ssize_t set_pwm(struct device *dev,
+		struct device_attribute *devattr, const char *buf, size_t count)
+{
+	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
+	struct i2c_client *client = to_i2c_client(dev);
+	struct max31790_data *data = i2c_get_clientdata(client);
+	unsigned long pwm;
+	int err;
+
+	err = kstrtoul(buf, 10, &pwm);
+	if (err)
+		return err;
+
+	pwm = clamp_val(pwm, 0x0, 0x1FF);
+
+	mutex_lock(&data->update_lock);

drivers/hwmon/max31790.c line 266

Quoted 10 lines
drivers/hwmon/max31790.c new file +10 -0
+	data->pwm[attr->index] = (u16)pwm << 7;
+	i2c_smbus_write_word_data(client,
+			MAX31790_REG_PWMOUT(attr->index),
+			swab16(data->pwm[attr->index]));
+
+	mutex_unlock(&data->update_lock);
+
+	return count;
+}
+

Valid range for pwmX is 0..255. Please report this range and adjust reported / configured values accordingly.

Quoted 30 lines
drivers/hwmon/max31790.c new file +30 -0
+static ssize_t get_reset(struct device *dev,
+		struct device_attribute *devattr, char *buf)
+{
+	struct max31790_data *data = max31790_update_device(dev);
+	int reset;
+
+	if (data->global_config & MAX31790_GL_CFG_RESET)
+		reset = 1;
+	else
+		reset = 0;
+
+	return sprintf(buf, "%d\n", reset);
+}
+
+static ssize_t set_reset(struct device *dev,
+		struct device_attribute *devattr, const char *buf, size_t count)
+{
+	struct i2c_client *client = to_i2c_client(dev);
+	struct max31790_data *data = i2c_get_clientdata(client);
+	unsigned long reset;
+	int err;
+
+	err = kstrtoul(buf, 10, &reset);
+	if (err)
+		return err;
+
+	mutex_lock(&data->update_lock);
+
+	if (reset)
+		data->global_config |= MAX31790_GL_CFG_RESET;

drivers/hwmon/max31790.c line 306

Quoted 10 lines
drivers/hwmon/max31790.c new file +10 -0
+	else
+		data->global_config &= ~MAX31790_GL_CFG_RESET;
+
+	i2c_smbus_write_byte_data(client,
+			MAX31790_REG_GLOBAL_CONFIG, data->global_config);
+
+	mutex_unlock(&data->update_lock);
+
+	return count;
+}

There should be no reason to expose this configuration bit. It is not common to implement chip resets this way. Please drop.

Quoted 31 lines
drivers/hwmon/max31790.c new file +31 -0
+
+static ssize_t get_standby(struct device *dev,
+		struct device_attribute *devattr, char *buf)
+{
+	struct max31790_data *data = max31790_update_device(dev);
+	int standby;
+
+	if (data->global_config & MAX31790_GL_CFG_STANDBY)
+		standby = 1;
+	else
+		standby = 0;
+
+	return sprintf(buf, "%d\n", standby);
+}
+
+static ssize_t set_standby(struct device *dev,
+		struct device_attribute *devattr, const char *buf, size_t count)
+{
+	struct i2c_client *client = to_i2c_client(dev);
+	struct max31790_data *data = i2c_get_clientdata(client);
+	unsigned long standby;
+	int err;
+
+	err = kstrtoul(buf, 10, &standby);
+	if (err)
+		return err;
+
+	mutex_lock(&data->update_lock);
+
+	if (standby)
+		data->global_config |= MAX31790_GL_CFG_STANDBY;

drivers/hwmon/max31790.c line 347

Quoted 10 lines
drivers/hwmon/max31790.c new file +10 -0
+	else
+		data->global_config &= ~MAX31790_GL_CFG_STANDBY;
+
+	i2c_smbus_write_byte_data(client,
+			MAX31790_REG_GLOBAL_CONFIG, data->global_config);
+
+	mutex_unlock(&data->update_lock);
+
+	return count;
+}

Same here. If you want to support standby more, please use PM functions.

Quoted 31 lines
drivers/hwmon/max31790.c new file +31 -0
+
+static ssize_t get_bus_timeout(struct device *dev,
+		struct device_attribute *devattr, char *buf)
+{
+	struct max31790_data *data = max31790_update_device(dev);
+	int bus_timeout;
+
+	if (data->global_config & MAX31790_GL_CFG_BUS_TIMEOUT)
+		bus_timeout = 0;
+	else
+		bus_timeout = 1;
+
+	return sprintf(buf, "%d\n", bus_timeout);
+}
+
+static ssize_t set_bus_timeout(struct device *dev,
+		struct device_attribute *devattr, const char *buf, size_t count)
+{
+	struct i2c_client *client = to_i2c_client(dev);
+	struct max31790_data *data = i2c_get_clientdata(client);
+	unsigned long bus_timeout;
+	int err;
+
+	err = kstrtoul(buf, 10, &bus_timeout);
+	if (err)
+		return err;
+
+	mutex_lock(&data->update_lock);
+
+	if (bus_timeout)
+		data->global_config &= ~MAX31790_GL_CFG_BUS_TIMEOUT;

drivers/hwmon/max31790.c line 388

Quoted 10 lines
drivers/hwmon/max31790.c new file +10 -0
+	else
+		data->global_config |= MAX31790_GL_CFG_BUS_TIMEOUT;
+
+	i2c_smbus_write_byte_data(client,
+			MAX31790_REG_GLOBAL_CONFIG, data->global_config);
+
+	mutex_unlock(&data->update_lock);
+
+	return count;
+}

This is, if at all, a driver configuration flag. Either use platform data, devicetree data, or a module parameter, but not a sysfs entry.

Quoted 31 lines
drivers/hwmon/max31790.c new file +31 -0
+
+static ssize_t get_watchdog(struct device *dev,
+		struct device_attribute *devattr, char *buf)
+{
+	struct max31790_data *data = max31790_update_device(dev);
+	int watchdog;
+
+	watchdog = (data->global_config & MAX31790_GL_CFG_WD_MASK)
+		>> MAX31790_GL_CFG_WD_SHIFT;
+
+	return sprintf(buf, "%d\n", watchdog);
+}
+
+static ssize_t set_watchdog(struct device *dev,
+		struct device_attribute *devattr, const char *buf, size_t count)
+{
+	struct i2c_client *client = to_i2c_client(dev);
+	struct max31790_data *data = i2c_get_clientdata(client);
+	unsigned long watchdog;
+	int err;
+
+	err = kstrtoul(buf, 10, &watchdog);
+	if (err)
+		return err;
+
+	mutex_lock(&data->update_lock);
+
+	if (watchdog < 0x0 || watchdog > 0x3)
+		return -EINVAL;
+
+	data->global_config > +		((data->global_config & ~MAX31790_GL_CFG_WD_MASK)

drivers/hwmon/max31790.c line 429

Quoted 10 lines
drivers/hwmon/max31790.c new file +10 -0
+		 | ((watchdog << MAX31790_GL_CFG_WD_SHIFT)
+			 & MAX31790_GL_CFG_WD_MASK));
+
+	i2c_smbus_write_byte_data(client,
+			MAX31790_REG_GLOBAL_CONFIG, data->global_config);
+
+	mutex_unlock(&data->update_lock);
+
+	return count;
+}

Same here. I would sugegst to not make this (and the bus timeout bit) configurable at all, though, unless you have a good reason to do so. If some special configuration is needed, it is supposed to be set by the BIOS or ROM monitor.

Quoted 39 lines
drivers/hwmon/max31790.c new file +39 -0
+
+static ssize_t get_fan_mode(struct device *dev,
+		struct device_attribute *devattr, char *buf)
+{
+	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
+	struct max31790_data *data = max31790_update_device(dev);
+	int mode;
+
+	if (data->fan_config[attr->index] & MAX31790_FAN_CFG_RPM_MODE)
+		mode = 1;
+	else
+		mode = 0;
+
+	return sprintf(buf, "%d\n", mode);
+}
+
+static ssize_t set_fan_mode(struct device *dev,
+		struct device_attribute *devattr, const char *buf, size_t count)
+{
+	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
+	struct i2c_client *client = to_i2c_client(dev);
+	struct max31790_data *data = i2c_get_clientdata(client);
+	unsigned long mode;
+	int err;
+
+	err = kstrtoul(buf, 10, &mode);
+	if (err)
+		return err;
+
+	mutex_lock(&data->update_lock);
+
+	if (mode)
+		data->fan_config[attr->index] > +			(data->fan_config[attr->index]
+			 & ~MAX31790_FAN_CFG_MODE_MASK)
+			| MAX31790_FAN_CFG_RPM_MODE;
+	else
+		data->fan_config[attr->index] > +			(data->fan_config[attr->index]
+			 & ~MAX31790_FAN_CFG_MODE_MASK)
+			| MAX31790_FAN_CFG_PWM_MODE;

drivers/hwmon/max31790.c line 478

Quoted 10 lines
drivers/hwmon/max31790.c new file +10 -0
+
+	i2c_smbus_write_byte_data(client,
+			MAX31790_REG_FAN_CONFIG(attr->index),
+			data->fan_config[attr->index]);
+
+	mutex_unlock(&data->update_lock);
+
+	return count;
+}
+

This is expected to be controlled with the pwmX_enable attribute.

pwmX_enable = 0: disabled. In this case, I would assume set bit 4 to 1. pwmX_enable = 1: manual mode. Set bit 7 to 1.
pwmX_enable = 2: rpm mode. Select desired fan speed with the fanX_target
attribute.

Quoted 5 lines
drivers/hwmon/max31790.c new file +5 -0
+static ssize_t get_tach_enable(struct device *dev,
+		struct device_attribute *devattr, char *buf)
+{
+	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
+	struct max31790_data *data = max31790_update_device(dev);

drivers/hwmon/max31790.c line 493

Quoted 10 lines
drivers/hwmon/max31790.c new file +10 -0
+	int tach_enable;
+
+	if (data->fan_config[attr->index] & MAX31790_FAN_CFG_TACH_INPUT_EN)
+		tach_enable = 1;
+	else
+		tach_enable = 0;
+
+	return sprintf(buf, "%d\n", tach_enable);
+}
+

This is again a configuration flag and should not be exposed as sysfs attribute.

Quoted 33 lines
drivers/hwmon/max31790.c new file +33 -0
+static ssize_t set_tach_enable(struct device *dev,
+		struct device_attribute *devattr, const char *buf, size_t count)
+{
+	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
+	struct i2c_client *client = to_i2c_client(dev);
+	struct max31790_data *data = i2c_get_clientdata(client);
+	unsigned long tach_enable;
+	int err;
+
+	err = kstrtoul(buf, 10, &tach_enable);
+	if (err)
+		return err;
+
+	mutex_lock(&data->update_lock);
+
+	if (tach_enable)
+		data->fan_config[attr->index]
+			|= MAX31790_FAN_CFG_TACH_INPUT_EN;
+	else
+		data->fan_config[attr->index]
+			&= ~MAX31790_FAN_CFG_TACH_INPUT_EN;
+
+	i2c_smbus_write_byte_data(client,
+			MAX31790_REG_FAN_CONFIG(attr->index),
+			data->fan_config[attr->index]);
+
+	mutex_unlock(&data->update_lock);
+
+	return count;
+}
+
+static ssize_t get_fan_fault(struct device *dev,
+		struct device_attribute *devattr, char *buf)

drivers/hwmon/max31790.c line 536

Quoted 10 lines
drivers/hwmon/max31790.c new file +10 -0
+{
+	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
+	struct max31790_data *data = max31790_update_device(dev);
+	int fault;
+
+	if (data->fault_status1 & (1 << attr->index))
+		fault = 1;
+	else
+		fault = 0;
+
	fault = !!(data->fault_status1 & (1 << attr->index));
Quoted 18 lines
drivers/hwmon/max31790.c new file +18 -0
+	return sprintf(buf, "%d\n", fault);
+}
+
+static ssize_t set_fan_fault(struct device *dev,
+		struct device_attribute *devattr, const char *buf, size_t count)
+{
+	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
+	struct i2c_client *client = to_i2c_client(dev);
+	struct max31790_data *data = i2c_get_clientdata(client);
+	unsigned long fault;
+	int err;
+
+	err = kstrtoul(buf, 10, &fault);
+	if (err)
+		return err;
+
+	mutex_lock(&data->update_lock);
+

drivers/hwmon/max31790.c line 564

Quoted 10 lines
drivers/hwmon/max31790.c new file +10 -0
+	if (fault)
+		data->fault_status1 > +			(data->fault_status1 | (1 << attr->index));
+	else
+		data->fault_status1 > +			(data->fault_status1 & ~(1 << attr->index));
+
+	i2c_smbus_write_byte_data(client,
+			MAX31790_REG_FAN_FAULT_STATUS1, data->fault_status1);
+
+	mutex_unlock(&data->update_lock);
+

fanX_fault is suppsed to be read-only. There is no manual fault reset. The idea is that the flag is cleared on read; if the fault condition is still present, the fault bit will be set again.

Quoted 35 lines
drivers/hwmon/max31790.c new file +35 -0
+	return count;
+}
+
+static ssize_t get_fan_mask(struct device *dev,
+		struct device_attribute *devattr, char *buf)
+{
+	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
+	struct max31790_data *data = max31790_update_device(dev);
+	int mask;
+
+	if (data->fault_mask1 & (1 << attr->index))
+		mask = 1;
+	else
+		mask = 0;
+
+	return sprintf(buf, "%d\n", mask);
+}
+
+static ssize_t set_fan_mask(struct device *dev,
+		struct device_attribute *devattr, const char *buf, size_t count)
+{
+	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
+	struct i2c_client *client = to_i2c_client(dev);
+	struct max31790_data *data = i2c_get_clientdata(client);
+	unsigned long mask;
+	int err;
+
+	err = kstrtoul(buf, 10, &mask);
+	if (err)
+		return err;
+
+	mutex_lock(&data->update_lock);
+
+	if (mask)
+		data->fault_mask1 > +			(data->fault_mask1 | (1 << attr->index));

drivers/hwmon/max31790.c line 609

Quoted 10 lines
drivers/hwmon/max31790.c new file +10 -0
+	else
+		data->fault_mask1 > +			(data->fault_mask1 & ~(1 << attr->index));
+
+	i2c_smbus_write_byte_data(client,
+			MAX31790_REG_FAN_FAULT_MASK1, data->fault_mask1);
+
+	mutex_unlock(&data->update_lock);
+
+	return count;
+}

Yet another set of configuration flags.

Quoted 161 lines
drivers/hwmon/max31790.c new file +161 -0
+
+static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, get_fan, NULL, 0);
+static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, get_fan, NULL, 1);
+static SENSOR_DEVICE_ATTR(fan3_input, S_IRUGO, get_fan, NULL, 2);
+static SENSOR_DEVICE_ATTR(fan4_input, S_IRUGO, get_fan, NULL, 3);
+static SENSOR_DEVICE_ATTR(fan5_input, S_IRUGO, get_fan, NULL, 4);
+static SENSOR_DEVICE_ATTR(fan6_input, S_IRUGO, get_fan, NULL, 5);
+
+static SENSOR_DEVICE_ATTR(fan1_target, S_IWUSR | S_IRUGO,
+		get_fan_target, set_fan_target, 0);
+static SENSOR_DEVICE_ATTR(fan2_target, S_IWUSR | S_IRUGO,
+		get_fan_target, set_fan_target, 1);
+static SENSOR_DEVICE_ATTR(fan3_target, S_IWUSR | S_IRUGO,
+		get_fan_target, set_fan_target, 2);
+static SENSOR_DEVICE_ATTR(fan4_target, S_IWUSR | S_IRUGO,
+		get_fan_target, set_fan_target, 3);
+static SENSOR_DEVICE_ATTR(fan5_target, S_IWUSR | S_IRUGO,
+		get_fan_target, set_fan_target, 4);
+static SENSOR_DEVICE_ATTR(fan6_target, S_IWUSR | S_IRUGO,
+		get_fan_target, set_fan_target, 5);
+
+static SENSOR_DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, get_pwm, set_pwm, 0);
+static SENSOR_DEVICE_ATTR(pwm2, S_IWUSR | S_IRUGO, get_pwm, set_pwm, 1);
+static SENSOR_DEVICE_ATTR(pwm3, S_IWUSR | S_IRUGO, get_pwm, set_pwm, 2);
+static SENSOR_DEVICE_ATTR(pwm4, S_IWUSR | S_IRUGO, get_pwm, set_pwm, 3);
+static SENSOR_DEVICE_ATTR(pwm5, S_IWUSR | S_IRUGO, get_pwm, set_pwm, 4);
+static SENSOR_DEVICE_ATTR(pwm6, S_IWUSR | S_IRUGO, get_pwm, set_pwm, 5);
+
+static DEVICE_ATTR(reset, S_IWUSR | S_IRUGO, get_reset, set_reset);
+static DEVICE_ATTR(standby, S_IWUSR | S_IRUGO, get_standby, set_standby);
+static DEVICE_ATTR(bus_timeout, S_IWUSR | S_IRUGO,
+		get_bus_timeout, set_bus_timeout);
+static DEVICE_ATTR(watchdog, S_IWUSR | S_IRUGO, get_watchdog, set_watchdog);
+
+static SENSOR_DEVICE_ATTR(fan1_mode, S_IWUSR | S_IRUGO,
+		get_fan_mode, set_fan_mode, 0);
+static SENSOR_DEVICE_ATTR(fan2_mode, S_IWUSR | S_IRUGO,
+		get_fan_mode, set_fan_mode, 1);
+static SENSOR_DEVICE_ATTR(fan3_mode, S_IWUSR | S_IRUGO,
+		get_fan_mode, set_fan_mode, 2);
+static SENSOR_DEVICE_ATTR(fan4_mode, S_IWUSR | S_IRUGO,
+		get_fan_mode, set_fan_mode, 3);
+static SENSOR_DEVICE_ATTR(fan5_mode, S_IWUSR | S_IRUGO,
+		get_fan_mode, set_fan_mode, 4);
+static SENSOR_DEVICE_ATTR(fan6_mode, S_IWUSR | S_IRUGO,
+		get_fan_mode, set_fan_mode, 5);
+
+static SENSOR_DEVICE_ATTR(fan1_tach_enable, S_IWUSR | S_IRUGO,
+		get_tach_enable, set_tach_enable, 0);
+static SENSOR_DEVICE_ATTR(fan2_tach_enable, S_IWUSR | S_IRUGO,
+		get_tach_enable, set_tach_enable, 1);
+static SENSOR_DEVICE_ATTR(fan3_tach_enable, S_IWUSR | S_IRUGO,
+		get_tach_enable, set_tach_enable, 2);
+static SENSOR_DEVICE_ATTR(fan4_tach_enable, S_IWUSR | S_IRUGO,
+		get_tach_enable, set_tach_enable, 3);
+static SENSOR_DEVICE_ATTR(fan5_tach_enable, S_IWUSR | S_IRUGO,
+		get_tach_enable, set_tach_enable, 4);
+static SENSOR_DEVICE_ATTR(fan6_tach_enable, S_IWUSR | S_IRUGO,
+		get_tach_enable, set_tach_enable, 5);
+
+static SENSOR_DEVICE_ATTR(fan1_fault, S_IWUSR | S_IRUGO,
+		get_fan_fault, set_fan_fault, 0);
+static SENSOR_DEVICE_ATTR(fan2_fault, S_IWUSR | S_IRUGO,
+		get_fan_fault, set_fan_fault, 1);
+static SENSOR_DEVICE_ATTR(fan3_fault, S_IWUSR | S_IRUGO,
+		get_fan_fault, set_fan_fault, 2);
+static SENSOR_DEVICE_ATTR(fan4_fault, S_IWUSR | S_IRUGO,
+		get_fan_fault, set_fan_fault, 3);
+static SENSOR_DEVICE_ATTR(fan5_fault, S_IWUSR | S_IRUGO,
+		get_fan_fault, set_fan_fault, 4);
+static SENSOR_DEVICE_ATTR(fan6_fault, S_IWUSR | S_IRUGO,
+		get_fan_fault, set_fan_fault, 5);
+
+static SENSOR_DEVICE_ATTR(fan1_mask, S_IWUSR | S_IRUGO,
+		get_fan_mask, set_fan_mask, 0);
+static SENSOR_DEVICE_ATTR(fan2_mask, S_IWUSR | S_IRUGO,
+		get_fan_mask, set_fan_mask, 1);
+static SENSOR_DEVICE_ATTR(fan3_mask, S_IWUSR | S_IRUGO,
+		get_fan_mask, set_fan_mask, 2);
+static SENSOR_DEVICE_ATTR(fan4_mask, S_IWUSR | S_IRUGO,
+		get_fan_mask, set_fan_mask, 3);
+static SENSOR_DEVICE_ATTR(fan5_mask, S_IWUSR | S_IRUGO,
+		get_fan_mask, set_fan_mask, 4);
+static SENSOR_DEVICE_ATTR(fan6_mask, S_IWUSR | S_IRUGO,
+		get_fan_mask, set_fan_mask, 5);
+
+static struct attribute *max31790_attrs[] = {
+	&sensor_dev_attr_fan1_input.dev_attr.attr,
+	&sensor_dev_attr_fan2_input.dev_attr.attr,
+	&sensor_dev_attr_fan3_input.dev_attr.attr,
+	&sensor_dev_attr_fan4_input.dev_attr.attr,
+	&sensor_dev_attr_fan5_input.dev_attr.attr,
+	&sensor_dev_attr_fan6_input.dev_attr.attr,
+
+	&sensor_dev_attr_fan1_target.dev_attr.attr,
+	&sensor_dev_attr_fan2_target.dev_attr.attr,
+	&sensor_dev_attr_fan3_target.dev_attr.attr,
+	&sensor_dev_attr_fan4_target.dev_attr.attr,
+	&sensor_dev_attr_fan5_target.dev_attr.attr,
+	&sensor_dev_attr_fan6_target.dev_attr.attr,
+
+	&sensor_dev_attr_pwm1.dev_attr.attr,
+	&sensor_dev_attr_pwm2.dev_attr.attr,
+	&sensor_dev_attr_pwm3.dev_attr.attr,
+	&sensor_dev_attr_pwm4.dev_attr.attr,
+	&sensor_dev_attr_pwm5.dev_attr.attr,
+	&sensor_dev_attr_pwm6.dev_attr.attr,
+
+	&dev_attr_reset.attr,
+	&dev_attr_standby.attr,
+	&dev_attr_bus_timeout.attr,
+	&dev_attr_watchdog.attr,
+
+	&sensor_dev_attr_fan1_mode.dev_attr.attr,
+	&sensor_dev_attr_fan2_mode.dev_attr.attr,
+	&sensor_dev_attr_fan3_mode.dev_attr.attr,
+	&sensor_dev_attr_fan4_mode.dev_attr.attr,
+	&sensor_dev_attr_fan5_mode.dev_attr.attr,
+	&sensor_dev_attr_fan6_mode.dev_attr.attr,
+
+	&sensor_dev_attr_fan1_tach_enable.dev_attr.attr,
+	&sensor_dev_attr_fan2_tach_enable.dev_attr.attr,
+	&sensor_dev_attr_fan3_tach_enable.dev_attr.attr,
+	&sensor_dev_attr_fan4_tach_enable.dev_attr.attr,
+	&sensor_dev_attr_fan5_tach_enable.dev_attr.attr,
+	&sensor_dev_attr_fan6_tach_enable.dev_attr.attr,
+
+	&sensor_dev_attr_fan1_fault.dev_attr.attr,
+	&sensor_dev_attr_fan2_fault.dev_attr.attr,
+	&sensor_dev_attr_fan3_fault.dev_attr.attr,
+	&sensor_dev_attr_fan4_fault.dev_attr.attr,
+	&sensor_dev_attr_fan5_fault.dev_attr.attr,
+	&sensor_dev_attr_fan6_fault.dev_attr.attr,
+
+	&sensor_dev_attr_fan1_mask.dev_attr.attr,
+	&sensor_dev_attr_fan2_mask.dev_attr.attr,
+	&sensor_dev_attr_fan3_mask.dev_attr.attr,
+	&sensor_dev_attr_fan4_mask.dev_attr.attr,
+	&sensor_dev_attr_fan5_mask.dev_attr.attr,
+	&sensor_dev_attr_fan6_mask.dev_attr.attr,
+	NULL
+};
+ATTRIBUTE_GROUPS(max31790);
+
+static int max31790_probe(struct i2c_client *client,
+		const struct i2c_device_id *id)
+{
+	struct i2c_adapter *adapter = client->adapter;
+	struct device *dev = &client->dev;
+	struct max31790_data *data;
+	struct device *hwmon_dev;
+
+	if (!i2c_check_functionality(adapter,
+			I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA))
+		return -ENODEV;
+
+	data = devm_kzalloc(dev, sizeof(struct max31790_data), GFP_KERNEL);
+	if (!data)
+		return -ENOMEM;
+
+	data->client = client;

drivers/hwmon/max31790.c line 780

Quoted 10 lines
drivers/hwmon/max31790.c new file +10 -0
+	mutex_init(&data->update_lock);
+
+	hwmon_dev = devm_hwmon_device_register_with_groups(dev,
+			client->name, data, max31790_groups);
+
+	return PTR_ERR_OR_ZERO(hwmon_dev);
+}
+
+static const struct i2c_device_id max31790_id[] = {
+	{ "max31790", max31790 },

You don't use the 'max31790' in the probe function (nor would there be a reason to do so).

Quoted 9 lines
drivers/hwmon/max31790.c new file +8 -0
+	{ }
+};
+MODULE_DEVICE_TABLE(i2c, max31790_id);
+
+static struct i2c_driver max31790_driver = {
+	.class		= I2C_CLASS_HWMON,
+	.probe		= max31790_probe,
+	.driver = {

drivers/hwmon/max31790.c line 798

Quoted 10 lines
drivers/hwmon/max31790.c new file +10 -0
+		.name	= "max31790",
+	},
+	.id_table	= max31790_id,
+};
+
+module_i2c_driver(max31790_driver);
+
+MODULE_AUTHOR("Il Han <[email protected]>");
+MODULE_DESCRIPTION("MAX31790 sensor driver");
+MODULE_LICENSE("GPL");

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

maintainer
#2 lore raw
In reply to Il Han
Quoted 7 lines · Il Han

Hi,

Thank you for your review.
I am modifying the driver on your advice.
I am going to commit it soon again.
I also added the comments below.

Please don't top-post.

Comments inline.

Thanks,
Guenter

[ ... ]

Quoted 13 lines
    Given that the sr range is 3 bit, an array would be simpler.

    static const u8 sr[8] = { 1, 2, 4, 8, 16, 32, 32, 32 };

    u8 get_speed_range(u8 fan_dynamics)
    {
             return sr[MAX31790_FAN_DYNAMICS_SR(fan_dynamics)];
    }

OK.

Quoted 7 lines

It might be useful to report the configured speed range as fanX_div, and make it configurable.

But it's not a divisor.
So I added fanX_period so that it is configurable, instead of the fanX_div.

... which is not a standard attribute and thus not acceptable. Maybe we need to determine how to map the value from one to another in a meaningful way.

Quoted 10 lines

You use direct values for everything but the pulse count. Might as well just use '2' for that as well. It is quite useless, though, since
(2 * (data->tach[attr->index] >> 5))
is identical to
(data->tach[attr->index] >> 4)
which you might as well use directly.

But, isn't there a case that the number of tachometer pulse per revolution is not 2?

See Documentation/hwmon/sysfs-interface.

Quoted 7 lines

Valid range for pwmX is 0..255. Please report this range and adjust reported / configured values accordingly.

In this chip, the range for PWM is 0..511. (9 bits of the PWMOUT Target Duty Cycle register.)

Again, see Documentation/hwmon/sysfs-interface. You have to stick with the ABI and adjust internally. Make the sysfs attribute 0..255 and write, for example, (written value * 2) into the chip.

Quoted 5 lines

+
+static const struct i2c_device_id max31790_id[] = {
+ { "max31790", max31790 },

Quoted 9 lines

You don't use the 'max31790' in the probe function (nor would there be a reason to do so).

I'm sorry. I don't understand what you've said.
I refered to source code of many other drivers to understand it. But I didn't catch that.
Could you let me know what you mean?

Drivers which don't support more than one chip just have something like

static const struct i2c_device_id max31790_id[] = {
	{ "max31790", 0 },
	{ }
};
Quoted 2 lines

Doesn't it register the device with the name, client->name?

Sure, but that doesn't mean you need the enum (which you don't use anyway).

Thanks,
Guenter

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

author
#3 lore raw

PATCH 1/1 hwmon: Driver for Maxim MAX31790

In reply to Il Han

The driver supports the Maxim MAX31790.

---
 drivers/hwmon/Kconfig    |   9 +
 drivers/hwmon/Makefile   |   1 +
 drivers/hwmon/max31790.c | 437 +++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 447 insertions(+)
 create mode 100644 drivers/hwmon/max31790.c
drivers/hwmon/Kconfig +9 -0
index 7c65b73..fcd1c93 100644
@@ -840,6 +840,15 @@ config SENSORS_MAX6697
 	  This driver can also be built as a module.  If so, the module
 	  will be called max6697.
 
+config SENSORS_MAX31790
+	tristate "Maxim MAX31790 sensor chip"
+	depends on I2C
+	help
+	  If you say yes here you get support for the MAX31790 sensor chips.
+
+	  This driver can also be built as a module.  If so, the module
+	  will be called max31790.
+
 config SENSORS_HTU21
 	tristate "Measurement Specialties HTU21D humidity/temperature sensors"
 	depends on I2C
drivers/hwmon/Makefile +1 -0
index 9e0f3dd..01855ee 100644
@@ -115,6 +115,7 @@ 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_MAX31790)	+= max31790.o
 obj-$(CONFIG_SENSORS_MC13783_ADC)+= mc13783-adc.o
 obj-$(CONFIG_SENSORS_MCP3021)	+= mcp3021.o
 obj-$(CONFIG_SENSORS_MENF21BMC_HWMON) += menf21bmc_hwmon.o
drivers/hwmon/max31790.c new file +433 -0

433 lines not on this page · raw message

Signature · 7 lines
2.4.1


_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors
maintainer
#4 lore raw
In reply to Il Han

Hi,

Quoted 10 lines · Il Han

The driver supports the Maxim MAX31790.

---
  drivers/hwmon/Kconfig    |   9 +
  drivers/hwmon/Makefile   |   1 +
  drivers/hwmon/max31790.c | 437 +++++++++++++++++++++++++++++++++++++++++++++++
  3 files changed, 447 insertions(+)

create mode 100644 drivers/hwmon/max31790.c

Please also provide Documentation/hwmon/max31790.

Quoted 5 lines
drivers/hwmon/Kconfig +0 -0
index 7c65b73..fcd1c93 100644

drivers/hwmon/Kconfig config SENSORS_MAX6697 · line 848

Quoted 9 lines
drivers/hwmon/Kconfig +6 -0
@@ -840,6 +840,15 @@ config SENSORS_MAX6697
  	  This driver can also be built as a module.  If so, the module
  	  will be called max6697.
 
+config SENSORS_MAX31790
+	tristate "Maxim MAX31790 sensor chip"
+	depends on I2C
+	help
+	  If you say yes here you get support for the MAX31790 sensor chips.
+

s/chips/chip/

I always like using the description from the datasheet, which is "6-Channel PWM-Output Fan RPM Controller".

Quoted 58 lines
drivers/hwmon/Kconfig +3 -0
+	  This driver can also be built as a module.  If so, the module
+	  will be called max31790.
+
  config SENSORS_HTU21
  	tristate "Measurement Specialties HTU21D humidity/temperature sensors"
  	depends on I2C
drivers/hwmon/Makefile +1 -0
index 9e0f3dd..01855ee 100644
@@ -115,6 +115,7 @@ 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_MAX31790)	+= max31790.o
  obj-$(CONFIG_SENSORS_MC13783_ADC)+= mc13783-adc.o
  obj-$(CONFIG_SENSORS_MCP3021)	+= mcp3021.o
  obj-$(CONFIG_SENSORS_MENF21BMC_HWMON) += menf21bmc_hwmon.o
drivers/hwmon/max31790.c new file +34 -0
new file mode 100644
index 0000000..39e5162
@@ -0,0 +1,437 @@
+/*
+ * max31790.c - Part of lm_sensors, Linux kernel modules for hardware
+ *             monitoring.
+ *
+ * (C) 2015 by Il Han <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#include <linux/err.h>
+#include <linux/hwmon.h>
+#include <linux/hwmon-sysfs.h>
+#include <linux/i2c.h>
+#include <linux/init.h>
+#include <linux/jiffies.h>
+#include <linux/module.h>
+#include <linux/slab.h>
+
+/* MAX31790 registers */
+#define MAX31790_REG_GLOBAL_CONFIG	0x00
+#define MAX31790_REG_FAN_CONFIG(ch)	(0x02 + (ch))
+#define MAX31790_REG_FAN_DYNAMICS(ch)	(0x08 + (ch))
+#define MAX31790_REG_FAN_FAULT_STATUS1	0x11
+#define MAX31790_REG_TACH_COUNT(ch)	(0x18 + (ch) * 2)
+#define MAX31790_REG_PWM_DUTY_CYCLE(ch)	(0x30 + (ch) * 2)
+#define MAX31790_REG_PWMOUT(ch)		(0x40 + (ch) * 2)

drivers/hwmon/max31790.c line 44

Quoted 10 lines
drivers/hwmon/max31790.c new file +10 -0
+#define MAX31790_REG_TARGET_COUNT(ch)	(0x50 + (ch) * 2)
+
+/* Fan Config register bits */
+#define MAX31790_FAN_CFG_RPM_MODE	0x80
+#define MAX31790_FAN_CFG_TACH_INPUT_EN	0x08
+
+/* Fan Dynamics register bits */
+#define SR_FROM_REG(reg)		(((reg) >> 5) & 0x7)
+
+#define RPM_FROM_REG(reg, sr)		((60 * sr * 8192) / (reg >> 4))

Please use (reg) and (sr). Also, reg >> 4 can be 0, so you'll need to add a check and return, say, FAN_RPM_MAX, if that is that case. Something like

#define RPM_FROM_REG(reg, sr)	(((reg) >> 4) ? \
				 ((60 * (sr) * 8192) / ((reg) >> 4)) : \
				 FAN_RPM_MAX)

drivers/hwmon/max31790.c line 46

Quoted 2 lines
drivers/hwmon/max31790.c new file +2 -0
+#define RPM_TO_REG(rpm, sr)		((60 * sr * 8192) / (rpm * 2))
+

(sr), (reg)

Quoted 31 lines
drivers/hwmon/max31790.c new file +31 -0
+#define NR_CHANNEL			6
+
+#define FAN_RPM_MIN			120
+#define FAN_RPM_MAX			7864320
+
+/*
+ * Client data (each client gets its own)
+ */
+struct max31790_data {
+	struct i2c_client *client;
+	struct mutex update_lock;
+	bool valid; /* zero until following fields are valid */
+	unsigned long last_updated; /* in jiffies */
+
+	/* register values */
+	u8 fan_config[NR_CHANNEL];
+	u8 fan_dynamics[NR_CHANNEL];
+	u8 fault_status1;
+	u16 tach[NR_CHANNEL];
+	u16 pwm[NR_CHANNEL];
+	u16 target_count[NR_CHANNEL];
+};
+
+static struct max31790_data *max31790_update_device(struct device *dev)
+{
+	struct max31790_data *data = dev_get_drvdata(dev);
+	struct i2c_client *client = data->client;
+	int i;
+
+	mutex_lock(&data->update_lock);
+

drivers/hwmon/max31790.c

Quoted 10 lines
drivers/hwmon/max31790.c new file +10 -0
+	if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
+		data->fault_status1 = i2c_smbus_read_byte_data(client,
+				MAX31790_REG_FAN_FAULT_STATUS1);
+		for (i = 0; i < NR_CHANNEL; i++) {
+			data->tach[i] = i2c_smbus_read_word_swapped(client,
+					MAX31790_REG_TACH_COUNT(i));
+			data->pwm[i] = i2c_smbus_read_word_swapped(client,
+					MAX31790_REG_PWMOUT(i));
+			data->target_count[i] > +					i2c_smbus_read_word_swapped(client,
+					MAX31790_REG_TARGET_COUNT(i));

No error checking, no error return ?
That means errors will translate into 0xffff or 0xff and produce really odd results.

drivers/hwmon/max31790.c line 128

Quoted 4 lines
drivers/hwmon/max31790.c new file +4 -0
+		}
+
+		data->last_updated = jiffies;
+		data->valid = 1;
			= true;
Quoted 64 lines
drivers/hwmon/max31790.c new file +64 -0
+	}
+
+	mutex_unlock(&data->update_lock);
+
+	return data;
+}
+
+static const u8 sr[8] = { 1, 2, 4, 8, 16, 32, 32, 32 };
+
+u8 get_speed_range(u8 fan_dynamics)
+{
+	return sr[SR_FROM_REG(fan_dynamics)];
+}
+
+static ssize_t get_fan(struct device *dev,
+		       struct device_attribute *devattr, char *buf)
+{
+	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
+	struct max31790_data *data = max31790_update_device(dev);
+	int sr = get_speed_range(data->fan_dynamics[attr->index]);
+	int rpm;
+
+	rpm = RPM_FROM_REG(data->tach[attr->index], sr);
+
+	return sprintf(buf, "%d\n", rpm);
+}
+
+static ssize_t get_fan_target(struct device *dev,
+			      struct device_attribute *devattr, char *buf)
+{
+	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
+	struct max31790_data *data = max31790_update_device(dev);
+	int sr = get_speed_range(data->fan_dynamics[attr->index]);
+	int rpm;
+
+	rpm = RPM_FROM_REG(data->target_count[attr->index], sr);
+
+	return sprintf(buf, "%d\n", rpm);
+}
+
+static ssize_t set_fan_target(struct device *dev,
+			      struct device_attribute *devattr,
+			      const char *buf, size_t count)
+{
+	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
+	struct i2c_client *client = to_i2c_client(dev);
+	struct max31790_data *data = i2c_get_clientdata(client);
+	int sr;
+	int target_count;
+	unsigned long rpm;
+	int err;
+
+	err = kstrtoul(buf, 10, &rpm);
+	if (err)
+		return err;
+
+	mutex_lock(&data->update_lock);
+
+	rpm = clamp_val(rpm, FAN_RPM_MIN, FAN_RPM_MAX);
+	sr = get_speed_range(data->fan_dynamics[attr->index]);
+	target_count = RPM_TO_REG(rpm, sr);
+	target_count = clamp_val(target_count, 0x1, 0x7FF);
+
+	data->target_count[attr->index] = (u16)target_count << 5;

drivers/hwmon/max31790.c line 228

Quoted 10 lines
drivers/hwmon/max31790.c new file +10 -0
+
+	i2c_smbus_write_word_swapped(client,
+			MAX31790_REG_TARGET_COUNT(attr->index),
+			data->target_count[attr->index]);
+
+	mutex_unlock(&data->update_lock);
+
+	return count;
+}
+

Would it make sense to auto-adjust the speed range
based on the desired target speed (or possibly the measured speed) ? This might make sense if target_count is too large or too small.

Quoted 18 lines
drivers/hwmon/max31790.c new file +18 -0
+static ssize_t get_pwm(struct device *dev,
+		       struct device_attribute *devattr, char *buf)
+{
+	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
+	struct max31790_data *data = max31790_update_device(dev);
+	int pwm;
+
+	pwm = data->pwm[attr->index] >> 8;
+
+	return sprintf(buf, "%d\n", pwm);
+}
+
+static ssize_t set_pwm(struct device *dev,
+		       struct device_attribute *devattr,
+		       const char *buf, size_t count)
+{
+	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
+	struct i2c_client *client = to_i2c_client(dev);

drivers/hwmon/max31790.c line 255

Quoted 10 lines
drivers/hwmon/max31790.c new file +10 -0
+	struct max31790_data *data = i2c_get_clientdata(client);
+	unsigned long pwm;
+	int err;
+
+	err = kstrtoul(buf, 10, &pwm);
+	if (err)
+		return err;
+
+	pwm = clamp_val(pwm, 0, 255);
+

The pwm value range is part of the ABI; specifying a larger value is not valid. Please make this

	if (pwm > 255)
		return -EINVAL;

drivers/hwmon/max31790.c

Quoted 3 lines
drivers/hwmon/max31790.c new file +3 -0
+	mutex_lock(&data->update_lock);
+
+	data->pwm[attr->index] = (u16)pwm << 8;

Typecast not needed.

Quoted 35 lines
drivers/hwmon/max31790.c new file +35 -0
+	i2c_smbus_write_word_swapped(client,
+			MAX31790_REG_PWMOUT(attr->index),
+			data->pwm[attr->index]);
+
+	mutex_unlock(&data->update_lock);
+
+	return count;
+}
+
+static ssize_t get_pwm_enable(struct device *dev,
+			      struct device_attribute *devattr, char *buf)
+{
+	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
+	struct max31790_data *data = max31790_update_device(dev);
+	int mode;
+
+	if (data->fan_config[attr->index] & MAX31790_FAN_CFG_RPM_MODE)
+		mode = 2;
+	else if (data->fan_config[attr->index] & MAX31790_FAN_CFG_TACH_INPUT_EN)
+		mode = 1;
+	else
+		mode = 0;
+
+	return sprintf(buf, "%d\n", mode);
+}
+
+static ssize_t set_pwm_enable(struct device *dev,
+			      struct device_attribute *devattr,
+			      const char *buf, size_t count)
+{
+	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
+	struct i2c_client *client = to_i2c_client(dev);
+	struct max31790_data *data = i2c_get_clientdata(client);
+	unsigned long mode;
+	int err;

drivers/hwmon/max31790.c

Quoted 10 lines
drivers/hwmon/max31790.c new file +10 -0
+
+	err = kstrtoul(buf, 10, &mode);
+	if (err)
+		return err;
+
+	switch (mode) {
+	case 0:
+		data->fan_config[attr->index] > +			data->fan_config[attr->index]
+			& ~(MAX31790_FAN_CFG_TACH_INPUT_EN
+			| MAX31790_FAN_CFG_RPM_MODE);

Please align the "|" with "(" above to improve readability.

drivers/hwmon/max31790.c line 246

Quoted 5 lines
drivers/hwmon/max31790.c new file +5 -0
+		break;
+	case 1:
+		data->fan_config[attr->index] > +			(data->fan_config[attr->index]
+			| MAX31790_FAN_CFG_TACH_INPUT_EN)
+			& ~MAX31790_FAN_CFG_RPM_MODE;

Same here.

drivers/hwmon/max31790.c line 251

Quoted 5 lines
drivers/hwmon/max31790.c new file +5 -0
+		break;
+	case 2:
+		data->fan_config[attr->index] > +			(data->fan_config[attr->index]
+			| MAX31790_FAN_CFG_TACH_INPUT_EN)
+			| MAX31790_FAN_CFG_RPM_MODE;

The ( ) in this expression don't make sense.

Quoted 25 lines
drivers/hwmon/max31790.c new file +25 -0
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	mutex_lock(&data->update_lock);
+
+	i2c_smbus_write_byte_data(client,
+			MAX31790_REG_FAN_CONFIG(attr->index),
+			data->fan_config[attr->index]);
+
+	mutex_unlock(&data->update_lock);
+
+	return count;
+}
+
+static ssize_t get_fan_fault(struct device *dev,
+			     struct device_attribute *devattr, char *buf)
+{
+	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
+	struct max31790_data *data = max31790_update_device(dev);
+	int fault;
+
+	fault = !!(data->fault_status1 & (1 << attr->index));
+

drivers/hwmon/max31790.c line 548

Quoted 10 lines
drivers/hwmon/max31790.c new file +10 -0
+	return sprintf(buf, "%d\n", fault);
+}
+
+static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, get_fan, NULL, 0);
+static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, get_fan, NULL, 1);
+static SENSOR_DEVICE_ATTR(fan3_input, S_IRUGO, get_fan, NULL, 2);
+static SENSOR_DEVICE_ATTR(fan4_input, S_IRUGO, get_fan, NULL, 3);
+static SENSOR_DEVICE_ATTR(fan5_input, S_IRUGO, get_fan, NULL, 4);
+static SENSOR_DEVICE_ATTR(fan6_input, S_IRUGO, get_fan, NULL, 5);
+

The chip supports reporting fan speed and fault status for 12 fans. Any reason for not supporting the others ?

I understand this is associated with bit 4 of the configuration registers. It might make sense to read the status of that bit and determine if the respective pin is used for fan speed measurement or for pwm control.

Quoted 80 lines
drivers/hwmon/max31790.c new file +80 -0
+static SENSOR_DEVICE_ATTR(fan1_target, S_IWUSR | S_IRUGO,
+		get_fan_target, set_fan_target, 0);
+static SENSOR_DEVICE_ATTR(fan2_target, S_IWUSR | S_IRUGO,
+		get_fan_target, set_fan_target, 1);
+static SENSOR_DEVICE_ATTR(fan3_target, S_IWUSR | S_IRUGO,
+		get_fan_target, set_fan_target, 2);
+static SENSOR_DEVICE_ATTR(fan4_target, S_IWUSR | S_IRUGO,
+		get_fan_target, set_fan_target, 3);
+static SENSOR_DEVICE_ATTR(fan5_target, S_IWUSR | S_IRUGO,
+		get_fan_target, set_fan_target, 4);
+static SENSOR_DEVICE_ATTR(fan6_target, S_IWUSR | S_IRUGO,
+		get_fan_target, set_fan_target, 5);
+
+static SENSOR_DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, get_pwm, set_pwm, 0);
+static SENSOR_DEVICE_ATTR(pwm2, S_IWUSR | S_IRUGO, get_pwm, set_pwm, 1);
+static SENSOR_DEVICE_ATTR(pwm3, S_IWUSR | S_IRUGO, get_pwm, set_pwm, 2);
+static SENSOR_DEVICE_ATTR(pwm4, S_IWUSR | S_IRUGO, get_pwm, set_pwm, 3);
+static SENSOR_DEVICE_ATTR(pwm5, S_IWUSR | S_IRUGO, get_pwm, set_pwm, 4);
+static SENSOR_DEVICE_ATTR(pwm6, S_IWUSR | S_IRUGO, get_pwm, set_pwm, 5);
+
+static SENSOR_DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
+		get_pwm_enable, set_pwm_enable, 0);
+static SENSOR_DEVICE_ATTR(pwm2_enable, S_IWUSR | S_IRUGO,
+		get_pwm_enable, set_pwm_enable, 1);
+static SENSOR_DEVICE_ATTR(pwm3_enable, S_IWUSR | S_IRUGO,
+		get_pwm_enable, set_pwm_enable, 2);
+static SENSOR_DEVICE_ATTR(pwm4_enable, S_IWUSR | S_IRUGO,
+		get_pwm_enable, set_pwm_enable, 3);
+static SENSOR_DEVICE_ATTR(pwm5_enable, S_IWUSR | S_IRUGO,
+		get_pwm_enable, set_pwm_enable, 4);
+static SENSOR_DEVICE_ATTR(pwm6_enable, S_IWUSR | S_IRUGO,
+		get_pwm_enable, set_pwm_enable, 5);
+
+static SENSOR_DEVICE_ATTR(fan1_fault, S_IRUGO, get_fan_fault, NULL, 0);
+static SENSOR_DEVICE_ATTR(fan2_fault, S_IRUGO, get_fan_fault, NULL, 1);
+static SENSOR_DEVICE_ATTR(fan3_fault, S_IRUGO, get_fan_fault, NULL, 2);
+static SENSOR_DEVICE_ATTR(fan4_fault, S_IRUGO, get_fan_fault, NULL, 3);
+static SENSOR_DEVICE_ATTR(fan5_fault, S_IRUGO, get_fan_fault, NULL, 4);
+static SENSOR_DEVICE_ATTR(fan6_fault, S_IRUGO, get_fan_fault, NULL, 5);
+
+static struct attribute *max31790_attrs[] = {
+	&sensor_dev_attr_fan1_input.dev_attr.attr,
+	&sensor_dev_attr_fan2_input.dev_attr.attr,
+	&sensor_dev_attr_fan3_input.dev_attr.attr,
+	&sensor_dev_attr_fan4_input.dev_attr.attr,
+	&sensor_dev_attr_fan5_input.dev_attr.attr,
+	&sensor_dev_attr_fan6_input.dev_attr.attr,
+
+	&sensor_dev_attr_fan1_target.dev_attr.attr,
+	&sensor_dev_attr_fan2_target.dev_attr.attr,
+	&sensor_dev_attr_fan3_target.dev_attr.attr,
+	&sensor_dev_attr_fan4_target.dev_attr.attr,
+	&sensor_dev_attr_fan5_target.dev_attr.attr,
+	&sensor_dev_attr_fan6_target.dev_attr.attr,
+
+	&sensor_dev_attr_pwm1.dev_attr.attr,
+	&sensor_dev_attr_pwm2.dev_attr.attr,
+	&sensor_dev_attr_pwm3.dev_attr.attr,
+	&sensor_dev_attr_pwm4.dev_attr.attr,
+	&sensor_dev_attr_pwm5.dev_attr.attr,
+	&sensor_dev_attr_pwm6.dev_attr.attr,
+
+	&sensor_dev_attr_pwm1_enable.dev_attr.attr,
+	&sensor_dev_attr_pwm2_enable.dev_attr.attr,
+	&sensor_dev_attr_pwm3_enable.dev_attr.attr,
+	&sensor_dev_attr_pwm4_enable.dev_attr.attr,
+	&sensor_dev_attr_pwm5_enable.dev_attr.attr,
+	&sensor_dev_attr_pwm6_enable.dev_attr.attr,
+
+	&sensor_dev_attr_fan1_fault.dev_attr.attr,
+	&sensor_dev_attr_fan2_fault.dev_attr.attr,
+	&sensor_dev_attr_fan3_fault.dev_attr.attr,
+	&sensor_dev_attr_fan4_fault.dev_attr.attr,
+	&sensor_dev_attr_fan5_fault.dev_attr.attr,
+	&sensor_dev_attr_fan6_fault.dev_attr.attr,
+	NULL
+};
+ATTRIBUTE_GROUPS(max31790);
+
+static int max31790_init_client(struct max31790_data *data,

drivers/hwmon/max31790.c line 777

Quoted 10 lines
drivers/hwmon/max31790.c new file +10 -0
+				struct i2c_client *client)
+{
+	int i;
+
+	for (i = 0; i < NR_CHANNEL; i++) {
+		data->fan_config[i] = i2c_smbus_read_byte_data(client,
+				MAX31790_REG_FAN_CONFIG(i));
+		data->fan_dynamics[i] = i2c_smbus_read_byte_data(client,
+				MAX31790_REG_FAN_DYNAMICS(i));
+	}

No error checking, no error return ?

drivers/hwmon/max31790.c line 378

Quoted 2 lines
drivers/hwmon/max31790.c new file +2 -0
+
+	return 0;

Then why return a value in the first place ?

You might consider assing some error check here. The chip does not provide ID registers, meaning it is quite easily possible to assign a wrong chip ID.
This would give at least some protection.

It might be interesting to see what happens if you attach the driver to some random other chip, or to an address with no chip at all.

Quoted 46 lines
drivers/hwmon/max31790.c new file +45 -0
+}
+
+static int max31790_probe(struct i2c_client *client,
+			  const struct i2c_device_id *id)
+{
+	struct i2c_adapter *adapter = client->adapter;
+	struct device *dev = &client->dev;
+	struct max31790_data *data;
+	struct device *hwmon_dev;
+	int err;
+
+	if (!i2c_check_functionality(adapter,
+			I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA))
+		return -ENODEV;
+
+	data = devm_kzalloc(dev, sizeof(struct max31790_data), GFP_KERNEL);
+	if (!data)
+		return -ENOMEM;
+
+	data->client = client;
+	mutex_init(&data->update_lock);
+
+	/*
+	 * Initialize the max31790 chip
+	 */
+	err = max31790_init_client(data, client);
+	if (err)
+		return err;
+
+	hwmon_dev = devm_hwmon_device_register_with_groups(dev,
+			client->name, data, max31790_groups);
+
+	return PTR_ERR_OR_ZERO(hwmon_dev);
+}
+
+static const struct i2c_device_id max31790_id[] = {
+	{ "max31790", 0 },
+	{ }
+};
+MODULE_DEVICE_TABLE(i2c, max31790_id);
+
+static struct i2c_driver max31790_driver = {
+	.class		= I2C_CLASS_HWMON,
+	.probe		= max31790_probe,
+	.driver = {

drivers/hwmon/max31790.c line 798

Quoted 10 lines
drivers/hwmon/max31790.c new file +10 -0
+		.name	= "max31790",
+	},
+	.id_table	= max31790_id,
+};
+
+module_i2c_driver(max31790_driver);
+
+MODULE_AUTHOR("Il Han <[email protected]>");
+MODULE_DESCRIPTION("MAX31790 sensor driver");
+MODULE_LICENSE("GPL");

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

author
#5 lore raw

PATCH 1/1 hwmon: Driver for Maxim MAX31790

In reply to Il Han

The driver supports the Maxim MAX31790.

---
 Documentation/hwmon/max31790 |  35 +++
 drivers/hwmon/Kconfig        |   9 +
 drivers/hwmon/Makefile       |   1 +
 drivers/hwmon/max31790.c     | 557 +++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 602 insertions(+)
 create mode 100644 Documentation/hwmon/max31790
 create mode 100644 drivers/hwmon/max31790.c
Documentation/hwmon/max31790 new file +35 -0
new file mode 100644
index 0000000..a86c888
@@ -0,0 +1,35 @@
+Kernel driver max31790
+===========
+
+Supported chips:
+  * Maxim MAX31790
+    Prefix: 'max31790'
+    Addresses scanned: -
+    Datasheet: http://pdfserv.maximintegrated.com/en/ds/MAX31790.pdf
+
+Author: Il Han <[email protected]>
+
+
+Description
+-----------
+
+The MAX31790 controls the speeds of up to six fans using six independent
+PWM outputs. The desired fan speeds (or PWM duty cycles) are written
+through the I2C interface. The outputs drive "4-wire" fans directly,
+or can be used to modulate the fan's power terminals using an external
+pass transistor.
+
+Tachometer inputs monitor fan tachometer logic outputs for precise (+/-1%)
+monitoring and control of fan RPM as well as detection of fan failure.
+Six pins are dedicated tachometer inputs. Any of the six PWM outputs can
+also be configured to serve as tachometer inputs.
+
+
+Sysfs entries
+----------------
+
+fan[1-12]_input    RO  fan tachometer speed in RPM
+fan[1-12]_fault    RO  fan experienced fault
+fan[1-6]_target    RW  desired fan speed in RPM
+pwm[1-6]_enable    RW  regulator mode, 0=disabled, 1=manual mode, 2=rpm mode
+pwm[1-6]           RW  fan target duty cycle (0-255)
drivers/hwmon/Kconfig +9 -0
index 7c65b73..fcd1c93 100644
@@ -840,6 +840,15 @@ config SENSORS_MAX6697
 	  This driver can also be built as a module.  If so, the module
 	  will be called max6697.
 
+config SENSORS_MAX31790
+	tristate "Maxim MAX31790 sensor chip"
+	depends on I2C
+	help
+	  If you say yes here you get support for the MAX31790 sensor chips.
+
+	  This driver can also be built as a module.  If so, the module
+	  will be called max31790.
+
 config SENSORS_HTU21
 	tristate "Measurement Specialties HTU21D humidity/temperature sensors"
 	depends on I2C
drivers/hwmon/Makefile +1 -0
index 9e0f3dd..01855ee 100644
@@ -115,6 +115,7 @@ 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_MAX31790)	+= max31790.o
 obj-$(CONFIG_SENSORS_MC13783_ADC)+= mc13783-adc.o
 obj-$(CONFIG_SENSORS_MCP3021)	+= mcp3021.o
 obj-$(CONFIG_SENSORS_MENF21BMC_HWMON) += menf21bmc_hwmon.o
drivers/hwmon/max31790.c new file +553 -0

553 lines not on this page · raw message

Signature · 7 lines
2.4.1


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

PATCH 1/1 hwmon: Driver for Maxim MAX31790

In reply to Il Han

The driver supports the Maxim MAX31790.

---
 Documentation/hwmon/max31790 |  37 +++
 drivers/hwmon/Kconfig        |  10 +
 drivers/hwmon/Makefile       |   1 +
 drivers/hwmon/max31790.c     | 556 +++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 604 insertions(+)
 create mode 100644 Documentation/hwmon/max31790
 create mode 100644 drivers/hwmon/max31790.c
Documentation/hwmon/max31790 new file +37 -0
new file mode 100644
index 0000000..2e50c53
@@ -0,0 +1,37 @@
+Kernel driver max31790
+===========
+
+Supported chips:
+  * Maxim MAX31790
+    Prefix: 'max31790'
+    Addresses scanned: -
+    Datasheet: http://pdfserv.maximintegrated.com/en/ds/MAX31790.pdf
+
+Author: Il Han <[email protected]>
+
+
+Description
+-----------
+
+This driver implements support for the Maxim MAX31790 chip.
+
+The MAX31790 controls the speeds of up to six fans using six independent
+PWM outputs. The desired fan speeds (or PWM duty cycles) are written
+through the I2C interface. The outputs drive "4-wire" fans directly,
+or can be used to modulate the fan's power terminals using an external
+pass transistor.
+
+Tachometer inputs monitor fan tachometer logic outputs for precise (+/-1%)
+monitoring and control of fan RPM as well as detection of fan failure.
+Six pins are dedicated tachometer inputs. Any of the six PWM outputs can
+also be configured to serve as tachometer inputs.
+
+
+Sysfs entries
+----------------
+
+fan[1-12]_input    RO  fan tachometer speed in RPM
+fan[1-12]_fault    RO  fan experienced fault
+fan[1-6]_target    RW  desired fan speed in RPM
+pwm[1-6]_enable    RW  regulator mode, 0=disabled, 1=manual mode, 2=rpm mode
+pwm[1-6]           RW  fan target duty cycle (0-255)
drivers/hwmon/Kconfig +10 -0
index 7c65b73..39775f9 100644
@@ -840,6 +840,16 @@ config SENSORS_MAX6697
 	  This driver can also be built as a module.  If so, the module
 	  will be called max6697.
 
+config SENSORS_MAX31790
+	tristate "Maxim MAX31790 sensor chip"
+	depends on I2C
+	help
+	  If you say yes here you get support for 6-Channel PWM-Output
+	  Fan RPM Controller.
+
+	  This driver can also be built as a module.  If so, the module
+	  will be called max31790.
+
 config SENSORS_HTU21
 	tristate "Measurement Specialties HTU21D humidity/temperature sensors"
 	depends on I2C
drivers/hwmon/Makefile +1 -0
index 9e0f3dd..01855ee 100644
@@ -115,6 +115,7 @@ 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_MAX31790)	+= max31790.o
 obj-$(CONFIG_SENSORS_MC13783_ADC)+= mc13783-adc.o
 obj-$(CONFIG_SENSORS_MCP3021)	+= mcp3021.o
 obj-$(CONFIG_SENSORS_MENF21BMC_HWMON) += menf21bmc_hwmon.o
drivers/hwmon/max31790.c new file +552 -0

552 lines not on this page · raw message

Signature · 7 lines
2.4.1


_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors
maintainer
#7 lore raw
In reply to Il Han
Quoted 561 lines · Il Han

The driver supports the Maxim MAX31790.

---
  Documentation/hwmon/max31790 |  37 +++
  drivers/hwmon/Kconfig        |  10 +
  drivers/hwmon/Makefile       |   1 +
  drivers/hwmon/max31790.c     | 556 +++++++++++++++++++++++++++++++++++++++++++
  4 files changed, 604 insertions(+)

create mode 100644 Documentation/hwmon/max31790
create mode 100644 drivers/hwmon/max31790.c

Documentation/hwmon/max31790 new file +37 -0
new file mode 100644
index 0000000..2e50c53
@@ -0,0 +1,37 @@
+Kernel driver max31790
+===========
+
+Supported chips:
+  * Maxim MAX31790
+    Prefix: 'max31790'
+    Addresses scanned: -
+    Datasheet: http://pdfserv.maximintegrated.com/en/ds/MAX31790.pdf
+
+Author: Il Han <[email protected]>
+
+
+Description
+-----------
+
+This driver implements support for the Maxim MAX31790 chip.
+
+The MAX31790 controls the speeds of up to six fans using six independent
+PWM outputs. The desired fan speeds (or PWM duty cycles) are written
+through the I2C interface. The outputs drive "4-wire" fans directly,
+or can be used to modulate the fan's power terminals using an external
+pass transistor.
+
+Tachometer inputs monitor fan tachometer logic outputs for precise (+/-1%)
+monitoring and control of fan RPM as well as detection of fan failure.
+Six pins are dedicated tachometer inputs. Any of the six PWM outputs can
+also be configured to serve as tachometer inputs.
+
+
+Sysfs entries
+----------------
+
+fan[1-12]_input    RO  fan tachometer speed in RPM
+fan[1-12]_fault    RO  fan experienced fault
+fan[1-6]_target    RW  desired fan speed in RPM
+pwm[1-6]_enable    RW  regulator mode, 0=disabled, 1=manual mode, 2=rpm mode
+pwm[1-6]           RW  fan target duty cycle (0-255)
drivers/hwmon/Kconfig +10 -0
index 7c65b73..39775f9 100644
@@ -840,6 +840,16 @@ config SENSORS_MAX6697
  	  This driver can also be built as a module.  If so, the module
  	  will be called max6697.
 
+config SENSORS_MAX31790
+	tristate "Maxim MAX31790 sensor chip"
+	depends on I2C
+	help
+	  If you say yes here you get support for 6-Channel PWM-Output
+	  Fan RPM Controller.
+
+	  This driver can also be built as a module.  If so, the module
+	  will be called max31790.
+
  config SENSORS_HTU21
  	tristate "Measurement Specialties HTU21D humidity/temperature sensors"
  	depends on I2C
drivers/hwmon/Makefile +1 -0
index 9e0f3dd..01855ee 100644
@@ -115,6 +115,7 @@ 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_MAX31790)	+= max31790.o
  obj-$(CONFIG_SENSORS_MC13783_ADC)+= mc13783-adc.o
  obj-$(CONFIG_SENSORS_MCP3021)	+= mcp3021.o
  obj-$(CONFIG_SENSORS_MENF21BMC_HWMON) += menf21bmc_hwmon.o
drivers/hwmon/max31790.c new file +467 -0
new file mode 100644
index 0000000..7f4c4a1
@@ -0,0 +1,556 @@
+/*
+ * max31790.c - Part of lm_sensors, Linux kernel modules for hardware
+ *             monitoring.
+ *
+ * (C) 2015 by Il Han <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#include <linux/err.h>
+#include <linux/hwmon.h>
+#include <linux/hwmon-sysfs.h>
+#include <linux/i2c.h>
+#include <linux/init.h>
+#include <linux/jiffies.h>
+#include <linux/module.h>
+#include <linux/slab.h>
+
+/* MAX31790 registers */
+#define MAX31790_REG_GLOBAL_CONFIG	0x00
+#define MAX31790_REG_FAN_CONFIG(ch)	(0x02 + (ch))
+#define MAX31790_REG_FAN_DYNAMICS(ch)	(0x08 + (ch))
+#define MAX31790_REG_FAN_FAULT_STATUS2	0x10
+#define MAX31790_REG_FAN_FAULT_STATUS1	0x11
+#define MAX31790_REG_TACH_COUNT(ch)	(0x18 + (ch) * 2)
+#define MAX31790_REG_PWM_DUTY_CYCLE(ch)	(0x30 + (ch) * 2)
+#define MAX31790_REG_PWMOUT(ch)		(0x40 + (ch) * 2)
+#define MAX31790_REG_TARGET_COUNT(ch)	(0x50 + (ch) * 2)
+
+/* Fan Config register bits */
+#define MAX31790_FAN_CFG_RPM_MODE	0x80
+#define MAX31790_FAN_CFG_TACH_INPUT_EN	0x08
+
+/* Fan Dynamics register bits */
+#define MAX31790_FAN_DYN_SR_SHIFT	5
+#define MAX31790_FAN_DYN_SR_MASK	0xE0
+#define SR_FROM_REG(reg)		(((reg) & MAX31790_FAN_DYN_SR_MASK) \
+					 >> MAX31790_FAN_DYN_SR_SHIFT)
+
+#define FAN_RPM_MIN			120
+#define FAN_RPM_MAX			7864320
+
+#define RPM_FROM_REG(reg, sr)		(((reg) >> 4) ? \
+					 ((60 * (sr) * 8192) / ((reg) >> 4)) : \
+					 FAN_RPM_MAX)
+#define RPM_TO_REG(rpm, sr)		((60 * (sr) * 8192) / ((rpm) * 2))
+
+#define NR_CHANNEL			6
+
+/*
+ * Client data (each client gets its own)
+ */
+struct max31790_data {
+	struct i2c_client *client;
+	struct mutex update_lock;
+	bool valid; /* zero until following fields are valid */
+	unsigned long last_updated; /* in jiffies */
+
+	/* register values */
+	u8 fan_config[NR_CHANNEL];
+	u8 fan_dynamics[NR_CHANNEL];
+	u16 fault_status;
+	u16 tach[NR_CHANNEL * 2];
+	u16 pwm[NR_CHANNEL];
+	u16 target_count[NR_CHANNEL];
+};
+
+static struct max31790_data *max31790_update_device(struct device *dev)
+{
+	struct max31790_data *data = dev_get_drvdata(dev);
+	struct i2c_client *client = data->client;
+	struct max31790_data *ret = data;
+	int i;
+	int rv;
+
+	mutex_lock(&data->update_lock);
+
+	if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
+		rv = i2c_smbus_read_byte_data(client,
+				MAX31790_REG_FAN_FAULT_STATUS1);
+		if (rv < 0)
+			goto abort;
+		data->fault_status = ((data->fault_status & 0x0FC0)
+				      | (rv & 0x3F));
+
+		rv = i2c_smbus_read_byte_data(client,
+				MAX31790_REG_FAN_FAULT_STATUS2);
+		if (rv < 0)
+			goto abort;
+		data->fault_status = ((data->fault_status & 0x003F)
+				      | ((rv & 0x3F) << 6));
+
+		for (i = 0; i < NR_CHANNEL; i++) {
+			rv = i2c_smbus_read_word_swapped(client,
+					MAX31790_REG_TACH_COUNT(i));
+			if (rv < 0)
+				goto abort;
+			data->tach[i] = rv;
+
+			rv = i2c_smbus_read_word_swapped(client,
+					MAX31790_REG_TACH_COUNT(NR_CHANNEL+i));
+			if (rv < 0)
+				goto abort;
+			data->tach[NR_CHANNEL+i] = rv;
+
+			rv = i2c_smbus_read_word_swapped(client,
+					MAX31790_REG_PWMOUT(i));
+			if (rv < 0)
+				goto abort;
+			data->pwm[i] = rv;
+
+			rv = i2c_smbus_read_word_swapped(client,
+					MAX31790_REG_TARGET_COUNT(i));
+			if (rv < 0)
+				goto abort;
+			data->target_count[i] = rv;
+		}
+
+		data->last_updated = jiffies;
+		data->valid = 1;
+	}
+	goto done;
+
+abort:
+	data->valid = 0;
+	ret = ERR_PTR(rv);
+
+done:
+	mutex_unlock(&data->update_lock);
+
+	return ret;
+}
+
+static const u8 tach_period[8] = { 1, 2, 4, 8, 16, 32, 32, 32 };
+
+static u8 get_tach_period(u8 fan_dynamics)
+{
+	return tach_period[SR_FROM_REG(fan_dynamics)];
+}
+
+static u8 bits_for_tach_period(int rpm)
+{
+	u8 bits;
+
+	if (rpm < 500)
+		bits = 0x0;
+	else if (rpm < 1000)
+		bits = 0x1;
+	else if (rpm < 2000)
+		bits = 0x2;
+	else if (rpm < 4000)
+		bits = 0x3;
+	else if (rpm < 8000)
+		bits = 0x4;
+	else
+		bits = 0x5;
+
+	return bits;
+}
+
+static ssize_t get_fan(struct device *dev,
+		       struct device_attribute *devattr, char *buf)
+{
+	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
+	struct max31790_data *data = max31790_update_device(dev);
+	int sr = get_tach_period(data->fan_dynamics[attr->index]);
+	int rpm;
+
+	if (IS_ERR(data))
+		return PTR_ERR(data);
+
+	rpm = RPM_FROM_REG(data->tach[attr->index], sr);
+
+	return sprintf(buf, "%d\n", rpm);
+}
+
+static ssize_t get_fan_target(struct device *dev,
+			      struct device_attribute *devattr, char *buf)
+{
+	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
+	struct max31790_data *data = max31790_update_device(dev);
+	int sr = get_tach_period(data->fan_dynamics[attr->index]);
+	int rpm;
+
+	if (IS_ERR(data))
+		return PTR_ERR(data);
+
+	rpm = RPM_FROM_REG(data->target_count[attr->index], sr);
+
+	return sprintf(buf, "%d\n", rpm);
+}
+
+static ssize_t set_fan_target(struct device *dev,
+			      struct device_attribute *devattr,
+			      const char *buf, size_t count)
+{
+	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
+	struct i2c_client *client = to_i2c_client(dev);
+	struct max31790_data *data = i2c_get_clientdata(client);
+	u8 bits;
+	int sr;
+	int target_count;
+	unsigned long rpm;
+	int err;
+
+	err = kstrtoul(buf, 10, &rpm);
+	if (err)
+		return err;
+
+	mutex_lock(&data->update_lock);
+
+	rpm = clamp_val(rpm, FAN_RPM_MIN, FAN_RPM_MAX);
+	bits = bits_for_tach_period(rpm);
+	data->fan_dynamics[attr->index] > +			((data->fan_dynamics[attr->index]
+			  & ~MAX31790_FAN_DYN_SR_MASK)
+			 | (bits << MAX31790_FAN_DYN_SR_SHIFT));
+	i2c_smbus_write_byte_data(client,
+			MAX31790_REG_FAN_DYNAMICS(attr->index),
+			data->fan_dynamics[attr->index]);
+
+	sr = get_tach_period(data->fan_dynamics[attr->index]);
+	target_count = RPM_TO_REG(rpm, sr);
+	target_count = clamp_val(target_count, 0x1, 0x7FF);
+
+	data->target_count[attr->index] = target_count << 5;
+
+	i2c_smbus_write_word_swapped(client,
+			MAX31790_REG_TARGET_COUNT(attr->index),
+			data->target_count[attr->index]);
+
+	mutex_unlock(&data->update_lock);
+
+	return count;
+}
+
+static ssize_t get_pwm(struct device *dev,
+		       struct device_attribute *devattr, char *buf)
+{
+	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
+	struct max31790_data *data = max31790_update_device(dev);
+	int pwm;
+
+	if (IS_ERR(data))
+		return PTR_ERR(data);
+
+	pwm = data->pwm[attr->index] >> 8;
+
+	return sprintf(buf, "%d\n", pwm);
+}
+
+static ssize_t set_pwm(struct device *dev,
+		       struct device_attribute *devattr,
+		       const char *buf, size_t count)
+{
+	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
+	struct i2c_client *client = to_i2c_client(dev);
+	struct max31790_data *data = i2c_get_clientdata(client);
+	unsigned long pwm;
+	int err;
+
+	err = kstrtoul(buf, 10, &pwm);
+	if (err)
+		return err;
+
+	if (pwm > 255)
+		return -EINVAL;
+
+	mutex_lock(&data->update_lock);
+
+	data->pwm[attr->index] = pwm << 8;
+	i2c_smbus_write_word_swapped(client,
+			MAX31790_REG_PWMOUT(attr->index),
+			data->pwm[attr->index]);
+
+	mutex_unlock(&data->update_lock);
+
+	return count;
+}
+
+static ssize_t get_pwm_enable(struct device *dev,
+			      struct device_attribute *devattr, char *buf)
+{
+	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
+	struct max31790_data *data = max31790_update_device(dev);
+	int mode;
+
+	if (IS_ERR(data))
+		return PTR_ERR(data);
+
+	if (data->fan_config[attr->index] & MAX31790_FAN_CFG_RPM_MODE)
+		mode = 2;
+	else if (data->fan_config[attr->index] & MAX31790_FAN_CFG_TACH_INPUT_EN)
+		mode = 1;
+	else
+		mode = 0;
+
+	return sprintf(buf, "%d\n", mode);
+}
+
+static ssize_t set_pwm_enable(struct device *dev,
+			      struct device_attribute *devattr,
+			      const char *buf, size_t count)
+{
+	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
+	struct i2c_client *client = to_i2c_client(dev);
+	struct max31790_data *data = i2c_get_clientdata(client);
+	unsigned long mode;
+	int err;
+
+	err = kstrtoul(buf, 10, &mode);
+	if (err)
+		return err;
+
+	switch (mode) {
+	case 0:
+		data->fan_config[attr->index] > +			data->fan_config[attr->index]
+			& ~(MAX31790_FAN_CFG_TACH_INPUT_EN
+			    | MAX31790_FAN_CFG_RPM_MODE);
+		break;
+	case 1:
+		data->fan_config[attr->index] > +			(data->fan_config[attr->index]
+			 | MAX31790_FAN_CFG_TACH_INPUT_EN)
+			& ~MAX31790_FAN_CFG_RPM_MODE;
+		break;
+	case 2:
+		data->fan_config[attr->index] > +			data->fan_config[attr->index]
+			| MAX31790_FAN_CFG_TACH_INPUT_EN
+			| MAX31790_FAN_CFG_RPM_MODE;
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	mutex_lock(&data->update_lock);
+
+	i2c_smbus_write_byte_data(client,
+			MAX31790_REG_FAN_CONFIG(attr->index),
+			data->fan_config[attr->index]);
+
+	mutex_unlock(&data->update_lock);
+
+	return count;
+}
+
+static ssize_t get_fan_fault(struct device *dev,
+			     struct device_attribute *devattr, char *buf)
+{
+	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
+	struct max31790_data *data = max31790_update_device(dev);
+	int fault;
+
+	if (IS_ERR(data))
+		return PTR_ERR(data);
+
+	fault = !!(data->fault_status & (1 << attr->index));
+
+	return sprintf(buf, "%d\n", fault);
+}
+
+static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, get_fan, NULL, 0);
+static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, get_fan, NULL, 1);
+static SENSOR_DEVICE_ATTR(fan3_input, S_IRUGO, get_fan, NULL, 2);
+static SENSOR_DEVICE_ATTR(fan4_input, S_IRUGO, get_fan, NULL, 3);
+static SENSOR_DEVICE_ATTR(fan5_input, S_IRUGO, get_fan, NULL, 4);
+static SENSOR_DEVICE_ATTR(fan6_input, S_IRUGO, get_fan, NULL, 5);
+static SENSOR_DEVICE_ATTR(fan7_input, S_IRUGO, get_fan, NULL, 6);
+static SENSOR_DEVICE_ATTR(fan8_input, S_IRUGO, get_fan, NULL, 7);
+static SENSOR_DEVICE_ATTR(fan9_input, S_IRUGO, get_fan, NULL, 8);
+static SENSOR_DEVICE_ATTR(fan10_input, S_IRUGO, get_fan, NULL, 9);
+static SENSOR_DEVICE_ATTR(fan11_input, S_IRUGO, get_fan, NULL, 10);
+static SENSOR_DEVICE_ATTR(fan12_input, S_IRUGO, get_fan, NULL, 11);
+
+static SENSOR_DEVICE_ATTR(fan1_target, S_IWUSR | S_IRUGO,
+		get_fan_target, set_fan_target, 0);
+static SENSOR_DEVICE_ATTR(fan2_target, S_IWUSR | S_IRUGO,
+		get_fan_target, set_fan_target, 1);
+static SENSOR_DEVICE_ATTR(fan3_target, S_IWUSR | S_IRUGO,
+		get_fan_target, set_fan_target, 2);
+static SENSOR_DEVICE_ATTR(fan4_target, S_IWUSR | S_IRUGO,
+		get_fan_target, set_fan_target, 3);
+static SENSOR_DEVICE_ATTR(fan5_target, S_IWUSR | S_IRUGO,
+		get_fan_target, set_fan_target, 4);
+static SENSOR_DEVICE_ATTR(fan6_target, S_IWUSR | S_IRUGO,
+		get_fan_target, set_fan_target, 5);
+
+static SENSOR_DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, get_pwm, set_pwm, 0);
+static SENSOR_DEVICE_ATTR(pwm2, S_IWUSR | S_IRUGO, get_pwm, set_pwm, 1);
+static SENSOR_DEVICE_ATTR(pwm3, S_IWUSR | S_IRUGO, get_pwm, set_pwm, 2);
+static SENSOR_DEVICE_ATTR(pwm4, S_IWUSR | S_IRUGO, get_pwm, set_pwm, 3);
+static SENSOR_DEVICE_ATTR(pwm5, S_IWUSR | S_IRUGO, get_pwm, set_pwm, 4);
+static SENSOR_DEVICE_ATTR(pwm6, S_IWUSR | S_IRUGO, get_pwm, set_pwm, 5);
+
+static SENSOR_DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
+		get_pwm_enable, set_pwm_enable, 0);
+static SENSOR_DEVICE_ATTR(pwm2_enable, S_IWUSR | S_IRUGO,
+		get_pwm_enable, set_pwm_enable, 1);
+static SENSOR_DEVICE_ATTR(pwm3_enable, S_IWUSR | S_IRUGO,
+		get_pwm_enable, set_pwm_enable, 2);
+static SENSOR_DEVICE_ATTR(pwm4_enable, S_IWUSR | S_IRUGO,
+		get_pwm_enable, set_pwm_enable, 3);
+static SENSOR_DEVICE_ATTR(pwm5_enable, S_IWUSR | S_IRUGO,
+		get_pwm_enable, set_pwm_enable, 4);
+static SENSOR_DEVICE_ATTR(pwm6_enable, S_IWUSR | S_IRUGO,
+		get_pwm_enable, set_pwm_enable, 5);
+
+static SENSOR_DEVICE_ATTR(fan1_fault, S_IRUGO, get_fan_fault, NULL, 0);
+static SENSOR_DEVICE_ATTR(fan2_fault, S_IRUGO, get_fan_fault, NULL, 1);
+static SENSOR_DEVICE_ATTR(fan3_fault, S_IRUGO, get_fan_fault, NULL, 2);
+static SENSOR_DEVICE_ATTR(fan4_fault, S_IRUGO, get_fan_fault, NULL, 3);
+static SENSOR_DEVICE_ATTR(fan5_fault, S_IRUGO, get_fan_fault, NULL, 4);
+static SENSOR_DEVICE_ATTR(fan6_fault, S_IRUGO, get_fan_fault, NULL, 5);
+static SENSOR_DEVICE_ATTR(fan7_fault, S_IRUGO, get_fan_fault, NULL, 6);
+static SENSOR_DEVICE_ATTR(fan8_fault, S_IRUGO, get_fan_fault, NULL, 7);
+static SENSOR_DEVICE_ATTR(fan9_fault, S_IRUGO, get_fan_fault, NULL, 8);
+static SENSOR_DEVICE_ATTR(fan10_fault, S_IRUGO, get_fan_fault, NULL, 9);
+static SENSOR_DEVICE_ATTR(fan11_fault, S_IRUGO, get_fan_fault, NULL, 10);
+static SENSOR_DEVICE_ATTR(fan12_fault, S_IRUGO, get_fan_fault, NULL, 11);
+
+static struct attribute *max31790_attrs[] = {
+	&sensor_dev_attr_fan1_input.dev_attr.attr,
+	&sensor_dev_attr_fan2_input.dev_attr.attr,
+	&sensor_dev_attr_fan3_input.dev_attr.attr,
+	&sensor_dev_attr_fan4_input.dev_attr.attr,
+	&sensor_dev_attr_fan5_input.dev_attr.attr,
+	&sensor_dev_attr_fan6_input.dev_attr.attr,
+	&sensor_dev_attr_fan7_input.dev_attr.attr,
+	&sensor_dev_attr_fan8_input.dev_attr.attr,
+	&sensor_dev_attr_fan9_input.dev_attr.attr,
+	&sensor_dev_attr_fan10_input.dev_attr.attr,
+	&sensor_dev_attr_fan11_input.dev_attr.attr,
+	&sensor_dev_attr_fan12_input.dev_attr.attr,
+
+	&sensor_dev_attr_fan1_target.dev_attr.attr,
+	&sensor_dev_attr_fan2_target.dev_attr.attr,
+	&sensor_dev_attr_fan3_target.dev_attr.attr,
+	&sensor_dev_attr_fan4_target.dev_attr.attr,
+	&sensor_dev_attr_fan5_target.dev_attr.attr,
+	&sensor_dev_attr_fan6_target.dev_attr.attr,
+
+	&sensor_dev_attr_pwm1.dev_attr.attr,
+	&sensor_dev_attr_pwm2.dev_attr.attr,
+	&sensor_dev_attr_pwm3.dev_attr.attr,
+	&sensor_dev_attr_pwm4.dev_attr.attr,
+	&sensor_dev_attr_pwm5.dev_attr.attr,
+	&sensor_dev_attr_pwm6.dev_attr.attr,
+
+	&sensor_dev_attr_pwm1_enable.dev_attr.attr,
+	&sensor_dev_attr_pwm2_enable.dev_attr.attr,
+	&sensor_dev_attr_pwm3_enable.dev_attr.attr,
+	&sensor_dev_attr_pwm4_enable.dev_attr.attr,
+	&sensor_dev_attr_pwm5_enable.dev_attr.attr,
+	&sensor_dev_attr_pwm6_enable.dev_attr.attr,
+
+	&sensor_dev_attr_fan1_fault.dev_attr.attr,
+	&sensor_dev_attr_fan2_fault.dev_attr.attr,
+	&sensor_dev_attr_fan3_fault.dev_attr.attr,
+	&sensor_dev_attr_fan4_fault.dev_attr.attr,
+	&sensor_dev_attr_fan5_fault.dev_attr.attr,
+	&sensor_dev_attr_fan6_fault.dev_attr.attr,

drivers/hwmon/max31790.c line 477

Quoted 10 lines
drivers/hwmon/max31790.c new file +10 -0
+	&sensor_dev_attr_fan7_fault.dev_attr.attr,
+	&sensor_dev_attr_fan8_fault.dev_attr.attr,
+	&sensor_dev_attr_fan9_fault.dev_attr.attr,
+	&sensor_dev_attr_fan10_fault.dev_attr.attr,
+	&sensor_dev_attr_fan11_fault.dev_attr.attr,
+	&sensor_dev_attr_fan12_fault.dev_attr.attr,
+	NULL
+};
+ATTRIBUTE_GROUPS(max31790);
+

The idea here would be to use the is_visible callback to determine if an attribute should be visible or not. This would apply to the pwm attributes and to the fan[7-12] attributes. Essentially, depending on the chip configuration, either the fan attributes or the pwm attributes should be visible for a given fan/pwm control, but not both.

Example: Either pwm1, pwm1_enable, and fan1_target, or fan7_input and fan7_fault, but not both.

Thanks,
Guenter

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

author
#8 lore raw

PATCH 1/1 hwmon: Driver for Maxim MAX31790

In reply to Il Han

The driver supports the Maxim MAX31790.

---
 Documentation/hwmon/max31790 |  37 +++
 drivers/hwmon/Kconfig        |  10 +
 drivers/hwmon/Makefile       |   1 +
 drivers/hwmon/max31790.c     | 620 +++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 668 insertions(+)
 create mode 100644 Documentation/hwmon/max31790
 create mode 100644 drivers/hwmon/max31790.c
Documentation/hwmon/max31790 new file +37 -0
new file mode 100644
index 0000000..855e624
@@ -0,0 +1,37 @@
+Kernel driver max31790
+===========
+
+Supported chips:
+  * Maxim MAX31790
+    Prefix: 'max31790'
+    Addresses scanned: -
+    Datasheet: http://pdfserv.maximintegrated.com/en/ds/MAX31790.pdf
+
+Author: Il Han <[email protected]>
+
+
+Description
+-----------
+
+This driver implements support for the Maxim MAX31790 chip.
+
+The MAX31790 controls the speeds of up to six fans using six independent
+PWM outputs. The desired fan speeds (or PWM duty cycles) are written
+through the I2C interface. The outputs drive "4-wire" fans directly,
+or can be used to modulate the fan's power terminals using an external
+pass transistor.
+
+Tachometer inputs monitor fan tachometer logic outputs for precise (+/-1%)
+monitoring and control of fan RPM as well as detection of fan failure.
+Six pins are dedicated tachometer inputs. Any of the six PWM outputs can
+also be configured to serve as tachometer inputs.
+
+
+Sysfs entries
+-------------
+
+fan[1-12]_input    RO  fan tachometer speed in RPM
+fan[1-12]_fault    RO  fan experienced fault
+fan[1-6]_target    RW  desired fan speed in RPM
+pwm[1-6]_enable    RW  regulator mode, 0=disabled, 1=manual mode, 2=rpm mode
+pwm[1-6]           RW  fan target duty cycle (0-255)
drivers/hwmon/Kconfig +10 -0
index 7c65b73..39775f9 100644
@@ -840,6 +840,16 @@ config SENSORS_MAX6697
 	  This driver can also be built as a module.  If so, the module
 	  will be called max6697.
 
+config SENSORS_MAX31790
+	tristate "Maxim MAX31790 sensor chip"
+	depends on I2C
+	help
+	  If you say yes here you get support for 6-Channel PWM-Output
+	  Fan RPM Controller.
+
+	  This driver can also be built as a module.  If so, the module
+	  will be called max31790.
+
 config SENSORS_HTU21
 	tristate "Measurement Specialties HTU21D humidity/temperature sensors"
 	depends on I2C
drivers/hwmon/Makefile +1 -0
index 9e0f3dd..01855ee 100644
@@ -115,6 +115,7 @@ 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_MAX31790)	+= max31790.o
 obj-$(CONFIG_SENSORS_MC13783_ADC)+= mc13783-adc.o
 obj-$(CONFIG_SENSORS_MCP3021)	+= mcp3021.o
 obj-$(CONFIG_SENSORS_MENF21BMC_HWMON) += menf21bmc_hwmon.o
drivers/hwmon/max31790.c new file +615 -0

615 lines not on this page · raw message

Signature · 7 lines
2.4.1


_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors
maintainer
#9 lore raw
In reply to Il Han
Quoted 3 lines · Il Han

The driver supports the Maxim MAX31790.

Hi,

couple of comments below. Sorry for not getting back earlier.

It would be nice if you could add the patch version number to your submissions (Use 'PATCH v4' instead of 'PATCH', for example). Also, it would be nice if you could include a change log.

Given the complexity of the chip, I would really like to implement a module test for it. To do that, I would need a register dump. Any chance you could provide it ?

Thanks,
Guenter

Quoted 175 lines
---
  Documentation/hwmon/max31790 |  37 +++
  drivers/hwmon/Kconfig        |  10 +
  drivers/hwmon/Makefile       |   1 +
  drivers/hwmon/max31790.c     | 620 +++++++++++++++++++++++++++++++++++++++++++
  4 files changed, 668 insertions(+)

create mode 100644 Documentation/hwmon/max31790
create mode 100644 drivers/hwmon/max31790.c

Documentation/hwmon/max31790 new file +37 -0
new file mode 100644
index 0000000..855e624
@@ -0,0 +1,37 @@
+Kernel driver max31790
+===========
+
+Supported chips:
+  * Maxim MAX31790
+    Prefix: 'max31790'
+    Addresses scanned: -
+    Datasheet: http://pdfserv.maximintegrated.com/en/ds/MAX31790.pdf
+
+Author: Il Han <[email protected]>
+
+
+Description
+-----------
+
+This driver implements support for the Maxim MAX31790 chip.
+
+The MAX31790 controls the speeds of up to six fans using six independent
+PWM outputs. The desired fan speeds (or PWM duty cycles) are written
+through the I2C interface. The outputs drive "4-wire" fans directly,
+or can be used to modulate the fan's power terminals using an external
+pass transistor.
+
+Tachometer inputs monitor fan tachometer logic outputs for precise (+/-1%)
+monitoring and control of fan RPM as well as detection of fan failure.
+Six pins are dedicated tachometer inputs. Any of the six PWM outputs can
+also be configured to serve as tachometer inputs.
+
+
+Sysfs entries
+-------------
+
+fan[1-12]_input    RO  fan tachometer speed in RPM
+fan[1-12]_fault    RO  fan experienced fault
+fan[1-6]_target    RW  desired fan speed in RPM
+pwm[1-6]_enable    RW  regulator mode, 0=disabled, 1=manual mode, 2=rpm mode
+pwm[1-6]           RW  fan target duty cycle (0-255)
drivers/hwmon/Kconfig +10 -0
index 7c65b73..39775f9 100644
@@ -840,6 +840,16 @@ config SENSORS_MAX6697
  	  This driver can also be built as a module.  If so, the module
  	  will be called max6697.
 
+config SENSORS_MAX31790
+	tristate "Maxim MAX31790 sensor chip"
+	depends on I2C
+	help
+	  If you say yes here you get support for 6-Channel PWM-Output
+	  Fan RPM Controller.
+
+	  This driver can also be built as a module.  If so, the module
+	  will be called max31790.
+
  config SENSORS_HTU21
  	tristate "Measurement Specialties HTU21D humidity/temperature sensors"
  	depends on I2C
drivers/hwmon/Makefile +1 -0
index 9e0f3dd..01855ee 100644
@@ -115,6 +115,7 @@ 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_MAX31790)	+= max31790.o
  obj-$(CONFIG_SENSORS_MC13783_ADC)+= mc13783-adc.o
  obj-$(CONFIG_SENSORS_MCP3021)	+= mcp3021.o
  obj-$(CONFIG_SENSORS_MENF21BMC_HWMON) += menf21bmc_hwmon.o
drivers/hwmon/max31790.c new file +84 -0
new file mode 100644
index 0000000..6a8ea88
@@ -0,0 +1,620 @@
+/*
+ * max31790.c - Part of lm_sensors, Linux kernel modules for hardware
+ *             monitoring.
+ *
+ * (C) 2015 by Il Han <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#include <linux/err.h>
+#include <linux/hwmon.h>
+#include <linux/hwmon-sysfs.h>
+#include <linux/i2c.h>
+#include <linux/init.h>
+#include <linux/jiffies.h>
+#include <linux/module.h>
+#include <linux/slab.h>
+
+/* MAX31790 registers */
+#define MAX31790_REG_GLOBAL_CONFIG	0x00
+#define MAX31790_REG_FAN_CONFIG(ch)	(0x02 + (ch))
+#define MAX31790_REG_FAN_DYNAMICS(ch)	(0x08 + (ch))
+#define MAX31790_REG_FAN_FAULT_STATUS2	0x10
+#define MAX31790_REG_FAN_FAULT_STATUS1	0x11
+#define MAX31790_REG_TACH_COUNT(ch)	(0x18 + (ch) * 2)
+#define MAX31790_REG_PWM_DUTY_CYCLE(ch)	(0x30 + (ch) * 2)
+#define MAX31790_REG_PWMOUT(ch)		(0x40 + (ch) * 2)
+#define MAX31790_REG_TARGET_COUNT(ch)	(0x50 + (ch) * 2)
+
+/* Fan Config register bits */
+#define MAX31790_FAN_CFG_RPM_MODE	0x80
+#define MAX31790_FAN_CFG_TACH_INPUT_EN	0x08
+#define MAX31790_FAN_CFG_TACH_INPUT	0x01
+
+/* Fan Dynamics register bits */
+#define MAX31790_FAN_DYN_SR_SHIFT	5
+#define MAX31790_FAN_DYN_SR_MASK	0xE0
+#define SR_FROM_REG(reg)		(((reg) & MAX31790_FAN_DYN_SR_MASK) \
+					 >> MAX31790_FAN_DYN_SR_SHIFT)
+
+#define FAN_RPM_MIN			120
+#define FAN_RPM_MAX			7864320
+
+#define RPM_FROM_REG(reg, sr)		(((reg) >> 4) ? \
+					 ((60 * (sr) * 8192) / ((reg) >> 4)) : \
+					 FAN_RPM_MAX)
+#define RPM_TO_REG(rpm, sr)		((60 * (sr) * 8192) / ((rpm) * 2))
+
+#define NR_CHANNEL			6
+
+/*
+ * Client data (each client gets its own)
+ */
+struct max31790_data {
+	struct i2c_client *client;
+	struct mutex update_lock;
+	bool valid; /* zero until following fields are valid */
+	unsigned long last_updated; /* in jiffies */
+
+	/* register values */
+	u8 fan_config[NR_CHANNEL];
+	u8 fan_dynamics[NR_CHANNEL];
+	u16 fault_status;
+	u16 tach[NR_CHANNEL*2];
+	u16 pwm[NR_CHANNEL];
+	u16 target_count[NR_CHANNEL];
+};
+
+static struct max31790_data *max31790_update_device(struct device *dev)
+{
+	struct max31790_data *data = dev_get_drvdata(dev);
+	struct i2c_client *client = data->client;
+	struct max31790_data *ret = data;
+	int i;
+	int rv;
+

drivers/hwmon/max31790.c line 94

Quoted 10 lines
drivers/hwmon/max31790.c new file +10 -0
+	mutex_lock(&data->update_lock);
+
+	if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
+		rv = i2c_smbus_read_byte_data(client,
+				MAX31790_REG_FAN_FAULT_STATUS1);
+		if (rv < 0)
+			goto abort;
+		data->fault_status = ((data->fault_status & 0x0FC0)
+				      | (rv & 0x3F));
+

You can simplify the code by just writing to fault status,

		data->fault_status = rv & 0x3F;

drivers/hwmon/max31790.c line 100

Quoted 6 lines
drivers/hwmon/max31790.c new file +6 -0
+		rv = i2c_smbus_read_byte_data(client,
+				MAX31790_REG_FAN_FAULT_STATUS2);
+		if (rv < 0)
+			goto abort;
+		data->fault_status = ((data->fault_status & 0x003F)
+				      | ((rv & 0x3F) << 6));

... and oring in the rest of the fault status.

		data->fault_status |= (rv & 0x3F) << 6;

drivers/hwmon/max31790.c line 109

Quoted 9 lines
drivers/hwmon/max31790.c new file +9 -0
+
+		for (i = 0; i < NR_CHANNEL; i++) {
+			rv = i2c_smbus_read_word_swapped(client,
+					MAX31790_REG_TACH_COUNT(i));
+			if (rv < 0)
+				goto abort;
+			data->tach[i] = rv;
+
+			if (data->fan_dynamics[i]

I think this should be fan_config[i].

drivers/hwmon/max31790.c line 112

Quoted 3 lines
drivers/hwmon/max31790.c new file +3 -0
+			    & MAX31790_FAN_CFG_TACH_INPUT) {
+				rv = i2c_smbus_read_word_swapped(client,
+					MAX31790_REG_TACH_COUNT(NR_CHANNEL+i));

Please use spaces before and after '+'.

drivers/hwmon/max31790.c line 115

Quoted 3 lines
drivers/hwmon/max31790.c new file +3 -0
+				if (rv < 0)
+					goto abort;
+				data->tach[NR_CHANNEL+i] = rv;

Spaces.

drivers/hwmon/max31790.c line 128

Quoted 13 lines
drivers/hwmon/max31790.c new file +13 -0
+			}
+
+			rv = i2c_smbus_read_word_swapped(client,
+					MAX31790_REG_PWMOUT(i));
+			if (rv < 0)
+				goto abort;
+			data->pwm[i] = rv;
+
+			rv = i2c_smbus_read_word_swapped(client,
+					MAX31790_REG_TARGET_COUNT(i));
+			if (rv < 0)
+				goto abort;
+			data->target_count[i] = rv;

How about reading pwm and target_count only if pwm is enabled for the fan ?

drivers/hwmon/max31790.c line 128

Quoted 4 lines
drivers/hwmon/max31790.c new file +4 -0
+		}
+
+		data->last_updated = jiffies;
+		data->valid = 1;
			= true;

drivers/hwmon/max31790.c line 137

Quoted 5 lines
drivers/hwmon/max31790.c new file +5 -0
+	}
+	goto done;
+
+abort:
+	data->valid = 0;
			= false;
Quoted 95 lines
drivers/hwmon/max31790.c new file +95 -0
+	ret = ERR_PTR(rv);
+
+done:
+	mutex_unlock(&data->update_lock);
+
+	return ret;
+}
+
+static const u8 tach_period[8] = { 1, 2, 4, 8, 16, 32, 32, 32 };
+
+static u8 get_tach_period(u8 fan_dynamics)
+{
+	return tach_period[SR_FROM_REG(fan_dynamics)];
+}
+
+static u8 bits_for_tach_period(int rpm)
+{
+	u8 bits;
+
+	if (rpm < 500)
+		bits = 0x0;
+	else if (rpm < 1000)
+		bits = 0x1;
+	else if (rpm < 2000)
+		bits = 0x2;
+	else if (rpm < 4000)
+		bits = 0x3;
+	else if (rpm < 8000)
+		bits = 0x4;
+	else
+		bits = 0x5;
+
+	return bits;
+}
+
+static ssize_t get_fan(struct device *dev,
+		       struct device_attribute *devattr, char *buf)
+{
+	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
+	struct max31790_data *data = max31790_update_device(dev);
+	int sr = get_tach_period(data->fan_dynamics[attr->index]);
+	int rpm;
+
+	if (IS_ERR(data))
+		return PTR_ERR(data);
+
+	rpm = RPM_FROM_REG(data->tach[attr->index], sr);
+
+	return sprintf(buf, "%d\n", rpm);
+}
+
+static ssize_t get_fan_target(struct device *dev,
+			      struct device_attribute *devattr, char *buf)
+{
+	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
+	struct max31790_data *data = max31790_update_device(dev);
+	int sr = get_tach_period(data->fan_dynamics[attr->index]);
+	int rpm;
+
+	if (IS_ERR(data))
+		return PTR_ERR(data);
+
+	rpm = RPM_FROM_REG(data->target_count[attr->index], sr);
+
+	return sprintf(buf, "%d\n", rpm);
+}
+
+static ssize_t set_fan_target(struct device *dev,
+			      struct device_attribute *devattr,
+			      const char *buf, size_t count)
+{
+	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
+	struct i2c_client *client = to_i2c_client(dev);
+	struct max31790_data *data = i2c_get_clientdata(client);
+	u8 bits;
+	int sr;
+	int target_count;
+	unsigned long rpm;
+	int err;
+
+	err = kstrtoul(buf, 10, &rpm);
+	if (err)
+		return err;
+
+	mutex_lock(&data->update_lock);
+
+	rpm = clamp_val(rpm, FAN_RPM_MIN, FAN_RPM_MAX);
+	bits = bits_for_tach_period(rpm);
+	data->fan_dynamics[attr->index] > +			((data->fan_dynamics[attr->index]
+			  & ~MAX31790_FAN_DYN_SR_MASK)
+			 | (bits << MAX31790_FAN_DYN_SR_SHIFT));
+	i2c_smbus_write_byte_data(client,
+			MAX31790_REG_FAN_DYNAMICS(attr->index),
+			data->fan_dynamics[attr->index]);
+

drivers/hwmon/max31790.c line 242

Quoted 10 lines
drivers/hwmon/max31790.c new file +10 -0
+	sr = get_tach_period(data->fan_dynamics[attr->index]);
+	target_count = RPM_TO_REG(rpm, sr);
+	target_count = clamp_val(target_count, 0x1, 0x7FF);
+
+	data->target_count[attr->index] = target_count << 5;
+
+	i2c_smbus_write_word_swapped(client,
+			MAX31790_REG_TARGET_COUNT(attr->index),
+			data->target_count[attr->index]);
+

You don't implement any error checking on writes. Is this on purpose ?

Quoted 239 lines
drivers/hwmon/max31790.c new file +239 -0
+	mutex_unlock(&data->update_lock);
+
+	return count;
+}
+
+static ssize_t get_pwm(struct device *dev,
+		       struct device_attribute *devattr, char *buf)
+{
+	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
+	struct max31790_data *data = max31790_update_device(dev);
+	int pwm;
+
+	if (IS_ERR(data))
+		return PTR_ERR(data);
+
+	pwm = data->pwm[attr->index] >> 8;
+
+	return sprintf(buf, "%d\n", pwm);
+}
+
+static ssize_t set_pwm(struct device *dev,
+		       struct device_attribute *devattr,
+		       const char *buf, size_t count)
+{
+	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
+	struct i2c_client *client = to_i2c_client(dev);
+	struct max31790_data *data = i2c_get_clientdata(client);
+	unsigned long pwm;
+	int err;
+
+	err = kstrtoul(buf, 10, &pwm);
+	if (err)
+		return err;
+
+	if (pwm > 255)
+		return -EINVAL;
+
+	mutex_lock(&data->update_lock);
+
+	data->pwm[attr->index] = pwm << 8;
+	i2c_smbus_write_word_swapped(client,
+			MAX31790_REG_PWMOUT(attr->index),
+			data->pwm[attr->index]);
+
+	mutex_unlock(&data->update_lock);
+
+	return count;
+}
+
+static ssize_t get_pwm_enable(struct device *dev,
+			      struct device_attribute *devattr, char *buf)
+{
+	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
+	struct max31790_data *data = max31790_update_device(dev);
+	int mode;
+
+	if (IS_ERR(data))
+		return PTR_ERR(data);
+
+	if (data->fan_config[attr->index] & MAX31790_FAN_CFG_RPM_MODE)
+		mode = 2;
+	else if (data->fan_config[attr->index] & MAX31790_FAN_CFG_TACH_INPUT_EN)
+		mode = 1;
+	else
+		mode = 0;
+
+	return sprintf(buf, "%d\n", mode);
+}
+
+static ssize_t set_pwm_enable(struct device *dev,
+			      struct device_attribute *devattr,
+			      const char *buf, size_t count)
+{
+	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
+	struct i2c_client *client = to_i2c_client(dev);
+	struct max31790_data *data = i2c_get_clientdata(client);
+	unsigned long mode;
+	int err;
+
+	err = kstrtoul(buf, 10, &mode);
+	if (err)
+		return err;
+
+	switch (mode) {
+	case 0:
+		data->fan_config[attr->index] > +			data->fan_config[attr->index]
+			& ~(MAX31790_FAN_CFG_TACH_INPUT_EN
+			    | MAX31790_FAN_CFG_RPM_MODE);
+		break;
+	case 1:
+		data->fan_config[attr->index] > +			(data->fan_config[attr->index]
+			 | MAX31790_FAN_CFG_TACH_INPUT_EN)
+			& ~MAX31790_FAN_CFG_RPM_MODE;
+		break;
+	case 2:
+		data->fan_config[attr->index] > +			data->fan_config[attr->index]
+			| MAX31790_FAN_CFG_TACH_INPUT_EN
+			| MAX31790_FAN_CFG_RPM_MODE;
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	mutex_lock(&data->update_lock);
+
+	i2c_smbus_write_byte_data(client,
+			MAX31790_REG_FAN_CONFIG(attr->index),
+			data->fan_config[attr->index]);
+
+	mutex_unlock(&data->update_lock);
+
+	return count;
+}
+
+static ssize_t get_fan_fault(struct device *dev,
+			     struct device_attribute *devattr, char *buf)
+{
+	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
+	struct max31790_data *data = max31790_update_device(dev);
+	int fault;
+
+	if (IS_ERR(data))
+		return PTR_ERR(data);
+
+	fault = !!(data->fault_status & (1 << attr->index));
+
+	return sprintf(buf, "%d\n", fault);
+}
+
+static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, get_fan, NULL, 0);
+static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, get_fan, NULL, 1);
+static SENSOR_DEVICE_ATTR(fan3_input, S_IRUGO, get_fan, NULL, 2);
+static SENSOR_DEVICE_ATTR(fan4_input, S_IRUGO, get_fan, NULL, 3);
+static SENSOR_DEVICE_ATTR(fan5_input, S_IRUGO, get_fan, NULL, 4);
+static SENSOR_DEVICE_ATTR(fan6_input, S_IRUGO, get_fan, NULL, 5);
+static SENSOR_DEVICE_ATTR(fan7_input, S_IRUGO, get_fan, NULL, 6);
+static SENSOR_DEVICE_ATTR(fan8_input, S_IRUGO, get_fan, NULL, 7);
+static SENSOR_DEVICE_ATTR(fan9_input, S_IRUGO, get_fan, NULL, 8);
+static SENSOR_DEVICE_ATTR(fan10_input, S_IRUGO, get_fan, NULL, 9);
+static SENSOR_DEVICE_ATTR(fan11_input, S_IRUGO, get_fan, NULL, 10);
+static SENSOR_DEVICE_ATTR(fan12_input, S_IRUGO, get_fan, NULL, 11);
+
+static SENSOR_DEVICE_ATTR(fan1_target, S_IWUSR | S_IRUGO,
+		get_fan_target, set_fan_target, 0);
+static SENSOR_DEVICE_ATTR(fan2_target, S_IWUSR | S_IRUGO,
+		get_fan_target, set_fan_target, 1);
+static SENSOR_DEVICE_ATTR(fan3_target, S_IWUSR | S_IRUGO,
+		get_fan_target, set_fan_target, 2);
+static SENSOR_DEVICE_ATTR(fan4_target, S_IWUSR | S_IRUGO,
+		get_fan_target, set_fan_target, 3);
+static SENSOR_DEVICE_ATTR(fan5_target, S_IWUSR | S_IRUGO,
+		get_fan_target, set_fan_target, 4);
+static SENSOR_DEVICE_ATTR(fan6_target, S_IWUSR | S_IRUGO,
+		get_fan_target, set_fan_target, 5);
+
+static SENSOR_DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, get_pwm, set_pwm, 0);
+static SENSOR_DEVICE_ATTR(pwm2, S_IWUSR | S_IRUGO, get_pwm, set_pwm, 1);
+static SENSOR_DEVICE_ATTR(pwm3, S_IWUSR | S_IRUGO, get_pwm, set_pwm, 2);
+static SENSOR_DEVICE_ATTR(pwm4, S_IWUSR | S_IRUGO, get_pwm, set_pwm, 3);
+static SENSOR_DEVICE_ATTR(pwm5, S_IWUSR | S_IRUGO, get_pwm, set_pwm, 4);
+static SENSOR_DEVICE_ATTR(pwm6, S_IWUSR | S_IRUGO, get_pwm, set_pwm, 5);
+
+static SENSOR_DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
+		get_pwm_enable, set_pwm_enable, 0);
+static SENSOR_DEVICE_ATTR(pwm2_enable, S_IWUSR | S_IRUGO,
+		get_pwm_enable, set_pwm_enable, 1);
+static SENSOR_DEVICE_ATTR(pwm3_enable, S_IWUSR | S_IRUGO,
+		get_pwm_enable, set_pwm_enable, 2);
+static SENSOR_DEVICE_ATTR(pwm4_enable, S_IWUSR | S_IRUGO,
+		get_pwm_enable, set_pwm_enable, 3);
+static SENSOR_DEVICE_ATTR(pwm5_enable, S_IWUSR | S_IRUGO,
+		get_pwm_enable, set_pwm_enable, 4);
+static SENSOR_DEVICE_ATTR(pwm6_enable, S_IWUSR | S_IRUGO,
+		get_pwm_enable, set_pwm_enable, 5);
+
+static SENSOR_DEVICE_ATTR(fan1_fault, S_IRUGO, get_fan_fault, NULL, 0);
+static SENSOR_DEVICE_ATTR(fan2_fault, S_IRUGO, get_fan_fault, NULL, 1);
+static SENSOR_DEVICE_ATTR(fan3_fault, S_IRUGO, get_fan_fault, NULL, 2);
+static SENSOR_DEVICE_ATTR(fan4_fault, S_IRUGO, get_fan_fault, NULL, 3);
+static SENSOR_DEVICE_ATTR(fan5_fault, S_IRUGO, get_fan_fault, NULL, 4);
+static SENSOR_DEVICE_ATTR(fan6_fault, S_IRUGO, get_fan_fault, NULL, 5);
+static SENSOR_DEVICE_ATTR(fan7_fault, S_IRUGO, get_fan_fault, NULL, 6);
+static SENSOR_DEVICE_ATTR(fan8_fault, S_IRUGO, get_fan_fault, NULL, 7);
+static SENSOR_DEVICE_ATTR(fan9_fault, S_IRUGO, get_fan_fault, NULL, 8);
+static SENSOR_DEVICE_ATTR(fan10_fault, S_IRUGO, get_fan_fault, NULL, 9);
+static SENSOR_DEVICE_ATTR(fan11_fault, S_IRUGO, get_fan_fault, NULL, 10);
+static SENSOR_DEVICE_ATTR(fan12_fault, S_IRUGO, get_fan_fault, NULL, 11);
+
+static struct attribute *max31790_attrs[] = {
+	&sensor_dev_attr_fan1_input.dev_attr.attr,
+	&sensor_dev_attr_fan2_input.dev_attr.attr,
+	&sensor_dev_attr_fan3_input.dev_attr.attr,
+	&sensor_dev_attr_fan4_input.dev_attr.attr,
+	&sensor_dev_attr_fan5_input.dev_attr.attr,
+	&sensor_dev_attr_fan6_input.dev_attr.attr,
+	&sensor_dev_attr_fan7_input.dev_attr.attr,
+	&sensor_dev_attr_fan8_input.dev_attr.attr,
+	&sensor_dev_attr_fan9_input.dev_attr.attr,
+	&sensor_dev_attr_fan10_input.dev_attr.attr,
+	&sensor_dev_attr_fan11_input.dev_attr.attr,
+	&sensor_dev_attr_fan12_input.dev_attr.attr,
+
+	&sensor_dev_attr_fan1_target.dev_attr.attr,
+	&sensor_dev_attr_fan2_target.dev_attr.attr,
+	&sensor_dev_attr_fan3_target.dev_attr.attr,
+	&sensor_dev_attr_fan4_target.dev_attr.attr,
+	&sensor_dev_attr_fan5_target.dev_attr.attr,
+	&sensor_dev_attr_fan6_target.dev_attr.attr,
+
+	&sensor_dev_attr_pwm1.dev_attr.attr,
+	&sensor_dev_attr_pwm2.dev_attr.attr,
+	&sensor_dev_attr_pwm3.dev_attr.attr,
+	&sensor_dev_attr_pwm4.dev_attr.attr,
+	&sensor_dev_attr_pwm5.dev_attr.attr,
+	&sensor_dev_attr_pwm6.dev_attr.attr,
+
+	&sensor_dev_attr_pwm1_enable.dev_attr.attr,
+	&sensor_dev_attr_pwm2_enable.dev_attr.attr,
+	&sensor_dev_attr_pwm3_enable.dev_attr.attr,
+	&sensor_dev_attr_pwm4_enable.dev_attr.attr,
+	&sensor_dev_attr_pwm5_enable.dev_attr.attr,
+	&sensor_dev_attr_pwm6_enable.dev_attr.attr,
+
+	&sensor_dev_attr_fan1_fault.dev_attr.attr,
+	&sensor_dev_attr_fan2_fault.dev_attr.attr,
+	&sensor_dev_attr_fan3_fault.dev_attr.attr,
+	&sensor_dev_attr_fan4_fault.dev_attr.attr,
+	&sensor_dev_attr_fan5_fault.dev_attr.attr,
+	&sensor_dev_attr_fan6_fault.dev_attr.attr,
+	&sensor_dev_attr_fan7_fault.dev_attr.attr,
+	&sensor_dev_attr_fan8_fault.dev_attr.attr,
+	&sensor_dev_attr_fan9_fault.dev_attr.attr,
+	&sensor_dev_attr_fan10_fault.dev_attr.attr,
+	&sensor_dev_attr_fan11_fault.dev_attr.attr,
+	&sensor_dev_attr_fan12_fault.dev_attr.attr,
+	NULL
+};
+
+static umode_t max31790_attrs_visible(struct kobject *kobj,

drivers/hwmon/max31790.c line 765

Quoted 10 lines
drivers/hwmon/max31790.c new file +10 -0
+				     struct attribute *a, int n)
+{
+	struct device *dev = container_of(kobj, struct device, kobj);
+	struct max31790_data *data = dev_get_drvdata(dev);
+	struct i2c_client *client = data->client;
+	struct device_attribute *devattr > +			container_of(a, struct device_attribute, attr);
+	int index = to_sensor_dev_attr(devattr)->index % NR_CHANNEL;
+	u8 fan_config;
+
+

Please no double empty lines.

Quoted 29 lines
drivers/hwmon/max31790.c new file +29 -0
+	if (devattr = &sensor_dev_attr_fan7_input.dev_attr
+	    || devattr = &sensor_dev_attr_fan8_input.dev_attr
+	    || devattr = &sensor_dev_attr_fan9_input.dev_attr
+	    || devattr = &sensor_dev_attr_fan10_input.dev_attr
+	    || devattr = &sensor_dev_attr_fan11_input.dev_attr
+	    || devattr = &sensor_dev_attr_fan12_input.dev_attr
+	    || devattr = &sensor_dev_attr_fan7_fault.dev_attr
+	    || devattr = &sensor_dev_attr_fan8_fault.dev_attr
+	    || devattr = &sensor_dev_attr_fan9_fault.dev_attr
+	    || devattr = &sensor_dev_attr_fan10_fault.dev_attr
+	    || devattr = &sensor_dev_attr_fan11_fault.dev_attr
+	    || devattr = &sensor_dev_attr_fan12_fault.dev_attr) {
+		fan_config = i2c_smbus_read_byte_data(client,
+				MAX31790_REG_FAN_CONFIG(index));
+		if (!(fan_config & MAX31790_FAN_CFG_TACH_INPUT))
+			return 0;
+	} else if (devattr = &sensor_dev_attr_pwm1.dev_attr
+		   || devattr = &sensor_dev_attr_pwm2.dev_attr
+		   || devattr = &sensor_dev_attr_pwm3.dev_attr
+		   || devattr = &sensor_dev_attr_pwm4.dev_attr
+		   || devattr = &sensor_dev_attr_pwm5.dev_attr
+		   || devattr = &sensor_dev_attr_pwm6.dev_attr
+		   || devattr = &sensor_dev_attr_pwm1_enable.dev_attr
+		   || devattr = &sensor_dev_attr_pwm2_enable.dev_attr
+		   || devattr = &sensor_dev_attr_pwm3_enable.dev_attr
+		   || devattr = &sensor_dev_attr_pwm4_enable.dev_attr
+		   || devattr = &sensor_dev_attr_pwm5_enable.dev_attr
+		   || devattr = &sensor_dev_attr_pwm6_enable.dev_attr
+		   || devattr = &sensor_dev_attr_fan1_target.dev_attr

drivers/hwmon/max31790.c line 530

Quoted 10 lines
drivers/hwmon/max31790.c new file +10 -0
+		   || devattr = &sensor_dev_attr_fan2_target.dev_attr
+		   || devattr = &sensor_dev_attr_fan3_target.dev_attr
+		   || devattr = &sensor_dev_attr_fan4_target.dev_attr
+		   || devattr = &sensor_dev_attr_fan5_target.dev_attr
+		   || devattr = &sensor_dev_attr_fan6_target.dev_attr) {
+		fan_config = i2c_smbus_read_byte_data(client,
+				MAX31790_REG_FAN_CONFIG(index));
+		if (fan_config & MAX31790_FAN_CFG_TACH_INPUT)
+			return 0;
+	}

Please consider the following:

- re-arrange the attributes to list unconditional attributes first, followed by conditional fan attributes, followed by pwm attributes.
- change above code to

	fan_config = data->fan_config[index];

	if (n >= NR_CHANNEL * 2 && n < NR_CHANNEL * 4 &&
	    !(fan_config & MAX31790_FAN_CFG_TACH_INPUT))
			return 0;
	if (n >= NR_CHANNEL * 4 && (fan_config & MAX31790_FAN_CFG_TACH_INPUT))
		return 0;
Quoted 76 lines
drivers/hwmon/max31790.c new file +75 -0
+
+	return a->mode;
+}
+
+static const struct attribute_group max31790_group = {
+	.attrs = max31790_attrs,
+	.is_visible = max31790_attrs_visible,
+};
+__ATTRIBUTE_GROUPS(max31790);
+
+static int max31790_init_client(struct i2c_client *client,
+				struct max31790_data *data)
+{
+	int i, rv;
+
+	for (i = 0; i < NR_CHANNEL; i++) {
+		rv = i2c_smbus_read_byte_data(client,
+				MAX31790_REG_FAN_CONFIG(i));
+		if (rv < 0)
+			return rv;
+		data->fan_config[i] = rv;
+
+		rv = i2c_smbus_read_byte_data(client,
+				MAX31790_REG_FAN_DYNAMICS(i));
+		if (rv < 0)
+			return rv;
+		data->fan_dynamics[i] = rv;
+	}
+
+	return 0;
+}
+
+static int max31790_probe(struct i2c_client *client,
+			  const struct i2c_device_id *id)
+{
+	struct i2c_adapter *adapter = client->adapter;
+	struct device *dev = &client->dev;
+	struct max31790_data *data;
+	struct device *hwmon_dev;
+	int err;
+
+	if (!i2c_check_functionality(adapter,
+			I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA))
+		return -ENODEV;
+
+	data = devm_kzalloc(dev, sizeof(struct max31790_data), GFP_KERNEL);
+	if (!data)
+		return -ENOMEM;
+
+	data->client = client;
+	mutex_init(&data->update_lock);
+
+	/*
+	 * Initialize the max31790 chip
+	 */
+	err = max31790_init_client(client, data);
+	if (err)
+		return err;
+
+	hwmon_dev = devm_hwmon_device_register_with_groups(dev,
+			client->name, data, max31790_groups);
+
+	return PTR_ERR_OR_ZERO(hwmon_dev);
+}
+
+static const struct i2c_device_id max31790_id[] = {
+	{ "max31790", 0 },
+	{ }
+};
+MODULE_DEVICE_TABLE(i2c, max31790_id);
+
+static struct i2c_driver max31790_driver = {
+	.class		= I2C_CLASS_HWMON,
+	.probe		= max31790_probe,
+	.driver = {

drivers/hwmon/max31790.c line 615

Quoted 10 lines
drivers/hwmon/max31790.c new file +10 -0
+		.name	= "max31790",
+	},
+	.id_table	= max31790_id,
+};
+
+module_i2c_driver(max31790_driver);
+
+MODULE_AUTHOR("Il Han <[email protected]>");
+MODULE_DESCRIPTION("MAX31790 sensor driver");
+MODULE_LICENSE("GPL");

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

author
#10 lore raw

PATCH 1/1 hwmon: Driver for Maxim MAX31790

In reply to Il Han

The driver supports the Maxim MAX31790.

---
 Documentation/hwmon/max31790 |  37 +++
 drivers/hwmon/Kconfig        |  10 +
 drivers/hwmon/Makefile       |   1 +
 drivers/hwmon/max31790.c     | 603 +++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 651 insertions(+)
 create mode 100644 Documentation/hwmon/max31790
 create mode 100644 drivers/hwmon/max31790.c
Documentation/hwmon/max31790 new file +37 -0
new file mode 100644
index 0000000..855e624
@@ -0,0 +1,37 @@
+Kernel driver max31790
+===========
+
+Supported chips:
+  * Maxim MAX31790
+    Prefix: 'max31790'
+    Addresses scanned: -
+    Datasheet: http://pdfserv.maximintegrated.com/en/ds/MAX31790.pdf
+
+Author: Il Han <[email protected]>
+
+
+Description
+-----------
+
+This driver implements support for the Maxim MAX31790 chip.
+
+The MAX31790 controls the speeds of up to six fans using six independent
+PWM outputs. The desired fan speeds (or PWM duty cycles) are written
+through the I2C interface. The outputs drive "4-wire" fans directly,
+or can be used to modulate the fan's power terminals using an external
+pass transistor.
+
+Tachometer inputs monitor fan tachometer logic outputs for precise (+/-1%)
+monitoring and control of fan RPM as well as detection of fan failure.
+Six pins are dedicated tachometer inputs. Any of the six PWM outputs can
+also be configured to serve as tachometer inputs.
+
+
+Sysfs entries
+-------------
+
+fan[1-12]_input    RO  fan tachometer speed in RPM
+fan[1-12]_fault    RO  fan experienced fault
+fan[1-6]_target    RW  desired fan speed in RPM
+pwm[1-6]_enable    RW  regulator mode, 0=disabled, 1=manual mode, 2=rpm mode
+pwm[1-6]           RW  fan target duty cycle (0-255)
drivers/hwmon/Kconfig +10 -0
index 7c65b73..39775f9 100644
@@ -840,6 +840,16 @@ config SENSORS_MAX6697
 	  This driver can also be built as a module.  If so, the module
 	  will be called max6697.
 
+config SENSORS_MAX31790
+	tristate "Maxim MAX31790 sensor chip"
+	depends on I2C
+	help
+	  If you say yes here you get support for 6-Channel PWM-Output
+	  Fan RPM Controller.
+
+	  This driver can also be built as a module.  If so, the module
+	  will be called max31790.
+
 config SENSORS_HTU21
 	tristate "Measurement Specialties HTU21D humidity/temperature sensors"
 	depends on I2C
drivers/hwmon/Makefile +1 -0
index 9e0f3dd..01855ee 100644
@@ -115,6 +115,7 @@ 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_MAX31790)	+= max31790.o
 obj-$(CONFIG_SENSORS_MC13783_ADC)+= mc13783-adc.o
 obj-$(CONFIG_SENSORS_MCP3021)	+= mcp3021.o
 obj-$(CONFIG_SENSORS_MENF21BMC_HWMON) += menf21bmc_hwmon.o
drivers/hwmon/max31790.c new file +598 -0

598 lines not on this page · raw message

Signature · 7 lines
2.4.1


_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors
maintainer
#11 lore raw
In reply to Il Han
Quoted 294 lines · Il Han

The driver supports the Maxim MAX31790.

---
  Documentation/hwmon/max31790 |  37 +++
  drivers/hwmon/Kconfig        |  10 +
  drivers/hwmon/Makefile       |   1 +
  drivers/hwmon/max31790.c     | 603 +++++++++++++++++++++++++++++++++++++++++++
  4 files changed, 651 insertions(+)

create mode 100644 Documentation/hwmon/max31790
create mode 100644 drivers/hwmon/max31790.c

Documentation/hwmon/max31790 new file +37 -0
new file mode 100644
index 0000000..855e624
@@ -0,0 +1,37 @@
+Kernel driver max31790
+===========
+
+Supported chips:
+  * Maxim MAX31790
+    Prefix: 'max31790'
+    Addresses scanned: -
+    Datasheet: http://pdfserv.maximintegrated.com/en/ds/MAX31790.pdf
+
+Author: Il Han <[email protected]>
+
+
+Description
+-----------
+
+This driver implements support for the Maxim MAX31790 chip.
+
+The MAX31790 controls the speeds of up to six fans using six independent
+PWM outputs. The desired fan speeds (or PWM duty cycles) are written
+through the I2C interface. The outputs drive "4-wire" fans directly,
+or can be used to modulate the fan's power terminals using an external
+pass transistor.
+
+Tachometer inputs monitor fan tachometer logic outputs for precise (+/-1%)
+monitoring and control of fan RPM as well as detection of fan failure.
+Six pins are dedicated tachometer inputs. Any of the six PWM outputs can
+also be configured to serve as tachometer inputs.
+
+
+Sysfs entries
+-------------
+
+fan[1-12]_input    RO  fan tachometer speed in RPM
+fan[1-12]_fault    RO  fan experienced fault
+fan[1-6]_target    RW  desired fan speed in RPM
+pwm[1-6]_enable    RW  regulator mode, 0=disabled, 1=manual mode, 2=rpm mode
+pwm[1-6]           RW  fan target duty cycle (0-255)
drivers/hwmon/Kconfig +10 -0
index 7c65b73..39775f9 100644
@@ -840,6 +840,16 @@ config SENSORS_MAX6697
  	  This driver can also be built as a module.  If so, the module
  	  will be called max6697.
 
+config SENSORS_MAX31790
+	tristate "Maxim MAX31790 sensor chip"
+	depends on I2C
+	help
+	  If you say yes here you get support for 6-Channel PWM-Output
+	  Fan RPM Controller.
+
+	  This driver can also be built as a module.  If so, the module
+	  will be called max31790.
+
  config SENSORS_HTU21
  	tristate "Measurement Specialties HTU21D humidity/temperature sensors"
  	depends on I2C
drivers/hwmon/Makefile +1 -0
index 9e0f3dd..01855ee 100644
@@ -115,6 +115,7 @@ 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_MAX31790)	+= max31790.o
  obj-$(CONFIG_SENSORS_MC13783_ADC)+= mc13783-adc.o
  obj-$(CONFIG_SENSORS_MCP3021)	+= mcp3021.o
  obj-$(CONFIG_SENSORS_MENF21BMC_HWMON) += menf21bmc_hwmon.o
drivers/hwmon/max31790.c new file +200 -0
new file mode 100644
index 0000000..d9b0003
@@ -0,0 +1,603 @@
+/*
+ * max31790.c - Part of lm_sensors, Linux kernel modules for hardware
+ *             monitoring.
+ *
+ * (C) 2015 by Il Han <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#include <linux/err.h>
+#include <linux/hwmon.h>
+#include <linux/hwmon-sysfs.h>
+#include <linux/i2c.h>
+#include <linux/init.h>
+#include <linux/jiffies.h>
+#include <linux/module.h>
+#include <linux/slab.h>
+
+/* MAX31790 registers */
+#define MAX31790_REG_GLOBAL_CONFIG	0x00
+#define MAX31790_REG_FAN_CONFIG(ch)	(0x02 + (ch))
+#define MAX31790_REG_FAN_DYNAMICS(ch)	(0x08 + (ch))
+#define MAX31790_REG_FAN_FAULT_STATUS2	0x10
+#define MAX31790_REG_FAN_FAULT_STATUS1	0x11
+#define MAX31790_REG_TACH_COUNT(ch)	(0x18 + (ch) * 2)
+#define MAX31790_REG_PWM_DUTY_CYCLE(ch)	(0x30 + (ch) * 2)
+#define MAX31790_REG_PWMOUT(ch)		(0x40 + (ch) * 2)
+#define MAX31790_REG_TARGET_COUNT(ch)	(0x50 + (ch) * 2)
+
+/* Fan Config register bits */
+#define MAX31790_FAN_CFG_RPM_MODE	0x80
+#define MAX31790_FAN_CFG_TACH_INPUT_EN	0x08
+#define MAX31790_FAN_CFG_TACH_INPUT	0x01
+
+/* Fan Dynamics register bits */
+#define MAX31790_FAN_DYN_SR_SHIFT	5
+#define MAX31790_FAN_DYN_SR_MASK	0xE0
+#define SR_FROM_REG(reg)		(((reg) & MAX31790_FAN_DYN_SR_MASK) \
+					 >> MAX31790_FAN_DYN_SR_SHIFT)
+
+#define FAN_RPM_MIN			120
+#define FAN_RPM_MAX			7864320
+
+#define RPM_FROM_REG(reg, sr)		(((reg) >> 4) ? \
+					 ((60 * (sr) * 8192) / ((reg) >> 4)) : \
+					 FAN_RPM_MAX)
+#define RPM_TO_REG(rpm, sr)		((60 * (sr) * 8192) / ((rpm) * 2))
+
+#define NR_CHANNEL			6
+
+/*
+ * Client data (each client gets its own)
+ */
+struct max31790_data {
+	struct i2c_client *client;
+	struct mutex update_lock;
+	bool valid; /* zero until following fields are valid */
+	unsigned long last_updated; /* in jiffies */
+
+	/* register values */
+	u8 fan_config[NR_CHANNEL];
+	u8 fan_dynamics[NR_CHANNEL];
+	u16 fault_status;
+	u16 tach[NR_CHANNEL * 2];
+	u16 pwm[NR_CHANNEL];
+	u16 target_count[NR_CHANNEL];
+};
+
+static struct max31790_data *max31790_update_device(struct device *dev)
+{
+	struct max31790_data *data = dev_get_drvdata(dev);
+	struct i2c_client *client = data->client;
+	struct max31790_data *ret = data;
+	int i;
+	int rv;
+
+	mutex_lock(&data->update_lock);
+
+	if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
+		rv = i2c_smbus_read_byte_data(client,
+				MAX31790_REG_FAN_FAULT_STATUS1);
+		if (rv < 0)
+			goto abort;
+		data->fault_status = rv & 0x3F;
+
+		rv = i2c_smbus_read_byte_data(client,
+				MAX31790_REG_FAN_FAULT_STATUS2);
+		if (rv < 0)
+			goto abort;
+		data->fault_status |= (rv & 0x3F) << 6;
+
+		for (i = 0; i < NR_CHANNEL; i++) {
+			rv = i2c_smbus_read_word_swapped(client,
+					MAX31790_REG_TACH_COUNT(i));
+			if (rv < 0)
+				goto abort;
+			data->tach[i] = rv;
+
+			if (data->fan_config[i]
+			    & MAX31790_FAN_CFG_TACH_INPUT) {
+				rv = i2c_smbus_read_word_swapped(client,
+					MAX31790_REG_TACH_COUNT(NR_CHANNEL
+								+ i));
+				if (rv < 0)
+					goto abort;
+				data->tach[NR_CHANNEL + i] = rv;
+			} else {
+				rv = i2c_smbus_read_word_swapped(client,
+						MAX31790_REG_PWMOUT(i));
+				if (rv < 0)
+					goto abort;
+				data->pwm[i] = rv;
+
+				rv = i2c_smbus_read_word_swapped(client,
+						MAX31790_REG_TARGET_COUNT(i));
+				if (rv < 0)
+					goto abort;
+				data->target_count[i] = rv;
+			}
+		}
+
+		data->last_updated = jiffies;
+		data->valid = true;
+	}
+	goto done;
+
+abort:
+	data->valid = false;
+	ret = ERR_PTR(rv);
+
+done:
+	mutex_unlock(&data->update_lock);
+
+	return ret;
+}
+
+static const u8 tach_period[8] = { 1, 2, 4, 8, 16, 32, 32, 32 };
+
+static u8 get_tach_period(u8 fan_dynamics)
+{
+	return tach_period[SR_FROM_REG(fan_dynamics)];
+}
+
+static u8 bits_for_tach_period(int rpm)
+{
+	u8 bits;
+
+	if (rpm < 500)
+		bits = 0x0;
+	else if (rpm < 1000)
+		bits = 0x1;
+	else if (rpm < 2000)
+		bits = 0x2;
+	else if (rpm < 4000)
+		bits = 0x3;
+	else if (rpm < 8000)
+		bits = 0x4;
+	else
+		bits = 0x5;
+
+	return bits;
+}
+
+static ssize_t get_fan(struct device *dev,
+		       struct device_attribute *devattr, char *buf)
+{
+	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
+	struct max31790_data *data = max31790_update_device(dev);
+	int sr = get_tach_period(data->fan_dynamics[attr->index]);
+	int rpm;
+
+	if (IS_ERR(data))
+		return PTR_ERR(data);
+
+	rpm = RPM_FROM_REG(data->tach[attr->index], sr);
+
+	return sprintf(buf, "%d\n", rpm);
+}
+
+static ssize_t get_fan_target(struct device *dev,
+			      struct device_attribute *devattr, char *buf)
+{
+	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
+	struct max31790_data *data = max31790_update_device(dev);
+	int sr = get_tach_period(data->fan_dynamics[attr->index]);
+	int rpm;
+
+	if (IS_ERR(data))
+		return PTR_ERR(data);
+
+	rpm = RPM_FROM_REG(data->target_count[attr->index], sr);
+

drivers/hwmon/max31790.c line 210

Quoted 10 lines
drivers/hwmon/max31790.c new file +10 -0
+	return sprintf(buf, "%d\n", rpm);
+}
+
+static ssize_t set_fan_target(struct device *dev,
+			      struct device_attribute *devattr,
+			      const char *buf, size_t count)
+{
+	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
+	struct i2c_client *client = to_i2c_client(dev);
+	struct max31790_data *data = i2c_get_clientdata(client);

Did you test those set functions ?

This should be

	struct max31790_data *data = dev_get_drvdata(dev);
	struct i2c_client *client = data->client;

The above code crashes, since dev is not an i2c client device.

Guenter

Quoted 379 lines
drivers/hwmon/max31790.c new file +378 -0
+	u8 bits;
+	int sr;
+	int target_count;
+	unsigned long rpm;
+	int err;
+
+	err = kstrtoul(buf, 10, &rpm);
+	if (err)
+		return err;
+
+	mutex_lock(&data->update_lock);
+
+	rpm = clamp_val(rpm, FAN_RPM_MIN, FAN_RPM_MAX);
+	bits = bits_for_tach_period(rpm);
+	data->fan_dynamics[attr->index] > +			((data->fan_dynamics[attr->index]
+			  & ~MAX31790_FAN_DYN_SR_MASK)
+			 | (bits << MAX31790_FAN_DYN_SR_SHIFT));
+	err = i2c_smbus_write_byte_data(client,
+			MAX31790_REG_FAN_DYNAMICS(attr->index),
+			data->fan_dynamics[attr->index]);
+
+	if (err < 0) {
+		mutex_unlock(&data->update_lock);
+		return err;
+	}
+
+	sr = get_tach_period(data->fan_dynamics[attr->index]);
+	target_count = RPM_TO_REG(rpm, sr);
+	target_count = clamp_val(target_count, 0x1, 0x7FF);
+
+	data->target_count[attr->index] = target_count << 5;
+
+	err = i2c_smbus_write_word_swapped(client,
+			MAX31790_REG_TARGET_COUNT(attr->index),
+			data->target_count[attr->index]);
+
+	mutex_unlock(&data->update_lock);
+
+	if (err < 0)
+		return err;
+
+	return count;
+}
+
+static ssize_t get_pwm(struct device *dev,
+		       struct device_attribute *devattr, char *buf)
+{
+	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
+	struct max31790_data *data = max31790_update_device(dev);
+	int pwm;
+
+	if (IS_ERR(data))
+		return PTR_ERR(data);
+
+	pwm = data->pwm[attr->index] >> 8;
+
+	return sprintf(buf, "%d\n", pwm);
+}
+
+static ssize_t set_pwm(struct device *dev,
+		       struct device_attribute *devattr,
+		       const char *buf, size_t count)
+{
+	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
+	struct i2c_client *client = to_i2c_client(dev);
+	struct max31790_data *data = i2c_get_clientdata(client);
+	unsigned long pwm;
+	int err;
+
+	err = kstrtoul(buf, 10, &pwm);
+	if (err)
+		return err;
+
+	if (pwm > 255)
+		return -EINVAL;
+
+	mutex_lock(&data->update_lock);
+
+	data->pwm[attr->index] = pwm << 8;
+	err = i2c_smbus_write_word_swapped(client,
+			MAX31790_REG_PWMOUT(attr->index),
+			data->pwm[attr->index]);
+
+	mutex_unlock(&data->update_lock);
+
+	if (err < 0)
+		return err;
+
+	return count;
+}
+
+static ssize_t get_pwm_enable(struct device *dev,
+			      struct device_attribute *devattr, char *buf)
+{
+	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
+	struct max31790_data *data = max31790_update_device(dev);
+	int mode;
+
+	if (IS_ERR(data))
+		return PTR_ERR(data);
+
+	if (data->fan_config[attr->index] & MAX31790_FAN_CFG_RPM_MODE)
+		mode = 2;
+	else if (data->fan_config[attr->index] & MAX31790_FAN_CFG_TACH_INPUT_EN)
+		mode = 1;
+	else
+		mode = 0;
+
+	return sprintf(buf, "%d\n", mode);
+}
+
+static ssize_t set_pwm_enable(struct device *dev,
+			      struct device_attribute *devattr,
+			      const char *buf, size_t count)
+{
+	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
+	struct i2c_client *client = to_i2c_client(dev);
+	struct max31790_data *data = i2c_get_clientdata(client);
+	unsigned long mode;
+	int err;
+
+	err = kstrtoul(buf, 10, &mode);
+	if (err)
+		return err;
+
+	switch (mode) {
+	case 0:
+		data->fan_config[attr->index] > +			data->fan_config[attr->index]
+			& ~(MAX31790_FAN_CFG_TACH_INPUT_EN
+			    | MAX31790_FAN_CFG_RPM_MODE);
+		break;
+	case 1:
+		data->fan_config[attr->index] > +			(data->fan_config[attr->index]
+			 | MAX31790_FAN_CFG_TACH_INPUT_EN)
+			& ~MAX31790_FAN_CFG_RPM_MODE;
+		break;
+	case 2:
+		data->fan_config[attr->index] > +			data->fan_config[attr->index]
+			| MAX31790_FAN_CFG_TACH_INPUT_EN
+			| MAX31790_FAN_CFG_RPM_MODE;
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	mutex_lock(&data->update_lock);
+
+	err = i2c_smbus_write_byte_data(client,
+			MAX31790_REG_FAN_CONFIG(attr->index),
+			data->fan_config[attr->index]);
+
+	mutex_unlock(&data->update_lock);
+
+	if (err < 0)
+		return err;
+
+	return count;
+}
+
+static ssize_t get_fan_fault(struct device *dev,
+			     struct device_attribute *devattr, char *buf)
+{
+	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
+	struct max31790_data *data = max31790_update_device(dev);
+	int fault;
+
+	if (IS_ERR(data))
+		return PTR_ERR(data);
+
+	fault = !!(data->fault_status & (1 << attr->index));
+
+	return sprintf(buf, "%d\n", fault);
+}
+
+static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, get_fan, NULL, 0);
+static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, get_fan, NULL, 1);
+static SENSOR_DEVICE_ATTR(fan3_input, S_IRUGO, get_fan, NULL, 2);
+static SENSOR_DEVICE_ATTR(fan4_input, S_IRUGO, get_fan, NULL, 3);
+static SENSOR_DEVICE_ATTR(fan5_input, S_IRUGO, get_fan, NULL, 4);
+static SENSOR_DEVICE_ATTR(fan6_input, S_IRUGO, get_fan, NULL, 5);
+
+static SENSOR_DEVICE_ATTR(fan1_fault, S_IRUGO, get_fan_fault, NULL, 0);
+static SENSOR_DEVICE_ATTR(fan2_fault, S_IRUGO, get_fan_fault, NULL, 1);
+static SENSOR_DEVICE_ATTR(fan3_fault, S_IRUGO, get_fan_fault, NULL, 2);
+static SENSOR_DEVICE_ATTR(fan4_fault, S_IRUGO, get_fan_fault, NULL, 3);
+static SENSOR_DEVICE_ATTR(fan5_fault, S_IRUGO, get_fan_fault, NULL, 4);
+static SENSOR_DEVICE_ATTR(fan6_fault, S_IRUGO, get_fan_fault, NULL, 5);
+
+static SENSOR_DEVICE_ATTR(fan7_input, S_IRUGO, get_fan, NULL, 6);
+static SENSOR_DEVICE_ATTR(fan8_input, S_IRUGO, get_fan, NULL, 7);
+static SENSOR_DEVICE_ATTR(fan9_input, S_IRUGO, get_fan, NULL, 8);
+static SENSOR_DEVICE_ATTR(fan10_input, S_IRUGO, get_fan, NULL, 9);
+static SENSOR_DEVICE_ATTR(fan11_input, S_IRUGO, get_fan, NULL, 10);
+static SENSOR_DEVICE_ATTR(fan12_input, S_IRUGO, get_fan, NULL, 11);
+
+static SENSOR_DEVICE_ATTR(fan7_fault, S_IRUGO, get_fan_fault, NULL, 6);
+static SENSOR_DEVICE_ATTR(fan8_fault, S_IRUGO, get_fan_fault, NULL, 7);
+static SENSOR_DEVICE_ATTR(fan9_fault, S_IRUGO, get_fan_fault, NULL, 8);
+static SENSOR_DEVICE_ATTR(fan10_fault, S_IRUGO, get_fan_fault, NULL, 9);
+static SENSOR_DEVICE_ATTR(fan11_fault, S_IRUGO, get_fan_fault, NULL, 10);
+static SENSOR_DEVICE_ATTR(fan12_fault, S_IRUGO, get_fan_fault, NULL, 11);
+
+static SENSOR_DEVICE_ATTR(fan1_target, S_IWUSR | S_IRUGO,
+		get_fan_target, set_fan_target, 0);
+static SENSOR_DEVICE_ATTR(fan2_target, S_IWUSR | S_IRUGO,
+		get_fan_target, set_fan_target, 1);
+static SENSOR_DEVICE_ATTR(fan3_target, S_IWUSR | S_IRUGO,
+		get_fan_target, set_fan_target, 2);
+static SENSOR_DEVICE_ATTR(fan4_target, S_IWUSR | S_IRUGO,
+		get_fan_target, set_fan_target, 3);
+static SENSOR_DEVICE_ATTR(fan5_target, S_IWUSR | S_IRUGO,
+		get_fan_target, set_fan_target, 4);
+static SENSOR_DEVICE_ATTR(fan6_target, S_IWUSR | S_IRUGO,
+		get_fan_target, set_fan_target, 5);
+
+static SENSOR_DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, get_pwm, set_pwm, 0);
+static SENSOR_DEVICE_ATTR(pwm2, S_IWUSR | S_IRUGO, get_pwm, set_pwm, 1);
+static SENSOR_DEVICE_ATTR(pwm3, S_IWUSR | S_IRUGO, get_pwm, set_pwm, 2);
+static SENSOR_DEVICE_ATTR(pwm4, S_IWUSR | S_IRUGO, get_pwm, set_pwm, 3);
+static SENSOR_DEVICE_ATTR(pwm5, S_IWUSR | S_IRUGO, get_pwm, set_pwm, 4);
+static SENSOR_DEVICE_ATTR(pwm6, S_IWUSR | S_IRUGO, get_pwm, set_pwm, 5);
+
+static SENSOR_DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
+		get_pwm_enable, set_pwm_enable, 0);
+static SENSOR_DEVICE_ATTR(pwm2_enable, S_IWUSR | S_IRUGO,
+		get_pwm_enable, set_pwm_enable, 1);
+static SENSOR_DEVICE_ATTR(pwm3_enable, S_IWUSR | S_IRUGO,
+		get_pwm_enable, set_pwm_enable, 2);
+static SENSOR_DEVICE_ATTR(pwm4_enable, S_IWUSR | S_IRUGO,
+		get_pwm_enable, set_pwm_enable, 3);
+static SENSOR_DEVICE_ATTR(pwm5_enable, S_IWUSR | S_IRUGO,
+		get_pwm_enable, set_pwm_enable, 4);
+static SENSOR_DEVICE_ATTR(pwm6_enable, S_IWUSR | S_IRUGO,
+		get_pwm_enable, set_pwm_enable, 5);
+
+static struct attribute *max31790_attrs[] = {
+	&sensor_dev_attr_fan1_input.dev_attr.attr,
+	&sensor_dev_attr_fan2_input.dev_attr.attr,
+	&sensor_dev_attr_fan3_input.dev_attr.attr,
+	&sensor_dev_attr_fan4_input.dev_attr.attr,
+	&sensor_dev_attr_fan5_input.dev_attr.attr,
+	&sensor_dev_attr_fan6_input.dev_attr.attr,
+
+	&sensor_dev_attr_fan1_fault.dev_attr.attr,
+	&sensor_dev_attr_fan2_fault.dev_attr.attr,
+	&sensor_dev_attr_fan3_fault.dev_attr.attr,
+	&sensor_dev_attr_fan4_fault.dev_attr.attr,
+	&sensor_dev_attr_fan5_fault.dev_attr.attr,
+	&sensor_dev_attr_fan6_fault.dev_attr.attr,
+
+	&sensor_dev_attr_fan7_input.dev_attr.attr,
+	&sensor_dev_attr_fan8_input.dev_attr.attr,
+	&sensor_dev_attr_fan9_input.dev_attr.attr,
+	&sensor_dev_attr_fan10_input.dev_attr.attr,
+	&sensor_dev_attr_fan11_input.dev_attr.attr,
+	&sensor_dev_attr_fan12_input.dev_attr.attr,
+
+	&sensor_dev_attr_fan7_fault.dev_attr.attr,
+	&sensor_dev_attr_fan8_fault.dev_attr.attr,
+	&sensor_dev_attr_fan9_fault.dev_attr.attr,
+	&sensor_dev_attr_fan10_fault.dev_attr.attr,
+	&sensor_dev_attr_fan11_fault.dev_attr.attr,
+	&sensor_dev_attr_fan12_fault.dev_attr.attr,
+
+	&sensor_dev_attr_fan1_target.dev_attr.attr,
+	&sensor_dev_attr_fan2_target.dev_attr.attr,
+	&sensor_dev_attr_fan3_target.dev_attr.attr,
+	&sensor_dev_attr_fan4_target.dev_attr.attr,
+	&sensor_dev_attr_fan5_target.dev_attr.attr,
+	&sensor_dev_attr_fan6_target.dev_attr.attr,
+
+	&sensor_dev_attr_pwm1.dev_attr.attr,
+	&sensor_dev_attr_pwm2.dev_attr.attr,
+	&sensor_dev_attr_pwm3.dev_attr.attr,
+	&sensor_dev_attr_pwm4.dev_attr.attr,
+	&sensor_dev_attr_pwm5.dev_attr.attr,
+	&sensor_dev_attr_pwm6.dev_attr.attr,
+
+	&sensor_dev_attr_pwm1_enable.dev_attr.attr,
+	&sensor_dev_attr_pwm2_enable.dev_attr.attr,
+	&sensor_dev_attr_pwm3_enable.dev_attr.attr,
+	&sensor_dev_attr_pwm4_enable.dev_attr.attr,
+	&sensor_dev_attr_pwm5_enable.dev_attr.attr,
+	&sensor_dev_attr_pwm6_enable.dev_attr.attr,
+	NULL
+};
+
+static umode_t max31790_attrs_visible(struct kobject *kobj,
+				     struct attribute *a, int n)
+{
+	struct device *dev = container_of(kobj, struct device, kobj);
+	struct max31790_data *data = dev_get_drvdata(dev);
+	struct device_attribute *devattr > +			container_of(a, struct device_attribute, attr);
+	int index = to_sensor_dev_attr(devattr)->index % NR_CHANNEL;
+	u8 fan_config;
+
+	fan_config = data->fan_config[index];
+
+	if (n >= NR_CHANNEL * 2 && n < NR_CHANNEL * 4 &&
+	    !(fan_config & MAX31790_FAN_CFG_TACH_INPUT))
+		return 0;
+	if (n >= NR_CHANNEL * 4 && (fan_config & MAX31790_FAN_CFG_TACH_INPUT))
+		return 0;
+
+	return a->mode;
+}
+
+static const struct attribute_group max31790_group = {
+	.attrs = max31790_attrs,
+	.is_visible = max31790_attrs_visible,
+};
+__ATTRIBUTE_GROUPS(max31790);
+
+static int max31790_init_client(struct i2c_client *client,
+				struct max31790_data *data)
+{
+	int i, rv;
+
+	for (i = 0; i < NR_CHANNEL; i++) {
+		rv = i2c_smbus_read_byte_data(client,
+				MAX31790_REG_FAN_CONFIG(i));
+		if (rv < 0)
+			return rv;
+		data->fan_config[i] = rv;
+
+		rv = i2c_smbus_read_byte_data(client,
+				MAX31790_REG_FAN_DYNAMICS(i));
+		if (rv < 0)
+			return rv;
+		data->fan_dynamics[i] = rv;
+	}
+
+	return 0;
+}
+
+static int max31790_probe(struct i2c_client *client,
+			  const struct i2c_device_id *id)
+{
+	struct i2c_adapter *adapter = client->adapter;
+	struct device *dev = &client->dev;
+	struct max31790_data *data;
+	struct device *hwmon_dev;
+	int err;
+
+	if (!i2c_check_functionality(adapter,
+			I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA))
+		return -ENODEV;
+
+	data = devm_kzalloc(dev, sizeof(struct max31790_data), GFP_KERNEL);
+	if (!data)
+		return -ENOMEM;
+
+	data->client = client;
+	mutex_init(&data->update_lock);
+
+	/*
+	 * Initialize the max31790 chip
+	 */
+	err = max31790_init_client(client, data);
+	if (err)
+		return err;
+
+	hwmon_dev = devm_hwmon_device_register_with_groups(dev,
+			client->name, data, max31790_groups);
+
+	return PTR_ERR_OR_ZERO(hwmon_dev);
+}
+
+static const struct i2c_device_id max31790_id[] = {
+	{ "max31790", 0 },
+	{ }
+};
+MODULE_DEVICE_TABLE(i2c, max31790_id);
+
+static struct i2c_driver max31790_driver = {
+	.class		= I2C_CLASS_HWMON,
+	.probe		= max31790_probe,
+	.driver = {

drivers/hwmon/max31790.c line 598

Quoted 10 lines
drivers/hwmon/max31790.c new file +10 -0
+		.name	= "max31790",
+	},
+	.id_table	= max31790_id,
+};
+
+module_i2c_driver(max31790_driver);
+
+MODULE_AUTHOR("Il Han <[email protected]>");
+MODULE_DESCRIPTION("MAX31790 sensor driver");
+MODULE_LICENSE("GPL");

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

author
#12 lore raw
In reply to Il Han

2015-08-25 23:52 GMT+09:00 Guenter Roeck <[email protected]>:

Quoted 18 lines · Il Han
Quoted 7 lines

The driver supports the Maxim MAX31790.

---

drivers/hwmon/max31790.c line 200

Quoted 7 lines
drivers/hwmon/max31790.c new file +7 -0
+static ssize_t set_fan_target(struct device *dev,
+                             struct device_attribute *devattr,
+                             const char *buf, size_t count)
+{
+       struct sensor_device_attribute *attr >> to_sensor_dev_attr(devattr);
+       struct i2c_client *client = to_i2c_client(dev);
+       struct max31790_data *data = i2c_get_clientdata(client);

Did you test those set functions ?

Quoted 10 lines

This should be

        struct max31790_data *data = dev_get_drvdata(dev);
        struct i2c_client *client = data->client;

The above code crashes, since dev is not an i2c client device.

Guenter

I tested them hundreds of times, but not on the latest kernel version. So I am going to modify them.

Thanks,
--
Il Han

Quoted 37 lines

+ u8 bits;

Quoted 35 lines
drivers/hwmon/max31790.c new file +35 -0
+       int sr;
+       int target_count;
+       unsigned long rpm;
+       int err;
+
+       err = kstrtoul(buf, 10, &rpm);
+       if (err)
+               return err;
+
+       mutex_lock(&data->update_lock);
+
+       rpm = clamp_val(rpm, FAN_RPM_MIN, FAN_RPM_MAX);
+       bits = bits_for_tach_period(rpm);
+       data->fan_dynamics[attr->index] >> +                       ((data->fan_dynamics[attr->index]
+                         & ~MAX31790_FAN_DYN_SR_MASK)
+                        | (bits << MAX31790_FAN_DYN_SR_SHIFT));
+       err = i2c_smbus_write_byte_data(client,
+                       MAX31790_REG_FAN_DYNAMICS(attr->index),
+                       data->fan_dynamics[attr->index]);
+
+       if (err < 0) {
+               mutex_unlock(&data->update_lock);
+               return err;
+       }
+
+       sr = get_tach_period(data->fan_dynamics[attr->index]);
+       target_count = RPM_TO_REG(rpm, sr);
+       target_count = clamp_val(target_count, 0x1, 0x7FF);
+
+       data->target_count[attr->index] = target_count << 5;
+
+       err = i2c_smbus_write_word_swapped(client,
+                       MAX31790_REG_TARGET_COUNT(attr->index),
+                       data->target_count[attr->index]);
+
Quoted 9 lines

drivers/hwmon/max31790.c line 265

Quoted 8 lines
drivers/hwmon/max31790.c new file +7 -0
+       mutex_unlock(&data->update_lock);
+
+       if (err < 0)
+               return err;
+
+       return count;
+}

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

maintainer
#13 lore raw
In reply to Il Han
Quoted 25 lines · Il Han

2015-08-25 23:52 GMT+09:00 Guenter Roeck <[email protected] <mailto:[email protected]>>:

On 08/24/2015 10:52 PM, Il Han wrote:

The driver supports the Maxim MAX31790.

Signed-off-by Il Han <[email protected] <mailto:[email protected]>>

---

+static ssize_t set_fan_target(struct device *dev,
+ struct device_attribute *devattr,
+ const char *buf, size_t count)
+{
+ struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
+ struct i2c_client *client = to_i2c_client(dev);
+ struct max31790_data *data = i2c_get_clientdata(client);

Did you test those set functions ?

This should be

struct max31790_data *data = dev_get_drvdata(dev); struct i2c_client *client = data->client;

Quoted 8 lines

The above code crashes, since dev is not an i2c client device.

Guenter

I tested them hundreds of times, but not on the latest kernel version. So I am going to modify them.

It has nothing to do with the kernel version, but with using the devm_ function to register the hwmon device.

Guenter

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

author
#14 lore raw
In reply to Il Han

2015-08-26 13:15 GMT+09:00 Guenter Roeck <[email protected]>:

Quoted 34 lines · Il Han
Quoted 33 lines

2015-08-25 23:52 GMT+09:00 Guenter Roeck <[email protected] <mailto:
[email protected]>>:

On 08/24/2015 10:52 PM, Il Han wrote:

The driver supports the Maxim MAX31790.

[email protected]>>
---

+static ssize_t set_fan_target(struct device *dev,
+ struct device_attribute *devattr,
+ const char *buf, size_t count)
+{
+ struct sensor_device_attribute *attr >> to_sensor_dev_attr(devattr);
+ struct i2c_client *client = to_i2c_client(dev);
+ struct max31790_data *data = i2c_get_clientdata(client);

Did you test those set functions ?

This should be

struct max31790_data *data = dev_get_drvdata(dev); struct i2c_client *client = data->client;

The above code crashes, since dev is not an i2c client device.

Guenter

Quoted 8 lines
Quoted 4 lines

I tested them hundreds of times, but not on the latest kernel version. So I am going to modify them.

It has nothing to do with the kernel version, but with using the devm_

function
to register the hwmon device.

Right... So I had to modify the _probe function on the other kernel version.

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

author
#15 lore raw

PATCH 1/1 hwmon: Driver for Maxim MAX31790

In reply to Il Han

The driver supports the Maxim MAX31790.

---
 Documentation/hwmon/max31790 |  37 +++
 drivers/hwmon/Kconfig        |  10 +
 drivers/hwmon/Makefile       |   1 +
 drivers/hwmon/max31790.c     | 603 +++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 651 insertions(+)
 create mode 100644 Documentation/hwmon/max31790
 create mode 100644 drivers/hwmon/max31790.c
Documentation/hwmon/max31790 new file +37 -0
new file mode 100644
index 0000000..855e624
@@ -0,0 +1,37 @@
+Kernel driver max31790
+===========
+
+Supported chips:
+  * Maxim MAX31790
+    Prefix: 'max31790'
+    Addresses scanned: -
+    Datasheet: http://pdfserv.maximintegrated.com/en/ds/MAX31790.pdf
+
+Author: Il Han <[email protected]>
+
+
+Description
+-----------
+
+This driver implements support for the Maxim MAX31790 chip.
+
+The MAX31790 controls the speeds of up to six fans using six independent
+PWM outputs. The desired fan speeds (or PWM duty cycles) are written
+through the I2C interface. The outputs drive "4-wire" fans directly,
+or can be used to modulate the fan's power terminals using an external
+pass transistor.
+
+Tachometer inputs monitor fan tachometer logic outputs for precise (+/-1%)
+monitoring and control of fan RPM as well as detection of fan failure.
+Six pins are dedicated tachometer inputs. Any of the six PWM outputs can
+also be configured to serve as tachometer inputs.
+
+
+Sysfs entries
+-------------
+
+fan[1-12]_input    RO  fan tachometer speed in RPM
+fan[1-12]_fault    RO  fan experienced fault
+fan[1-6]_target    RW  desired fan speed in RPM
+pwm[1-6]_enable    RW  regulator mode, 0=disabled, 1=manual mode, 2=rpm mode
+pwm[1-6]           RW  fan target duty cycle (0-255)
drivers/hwmon/Kconfig +10 -0
index 7c65b73..39775f9 100644
@@ -840,6 +840,16 @@ config SENSORS_MAX6697
 	  This driver can also be built as a module.  If so, the module
 	  will be called max6697.
 
+config SENSORS_MAX31790
+	tristate "Maxim MAX31790 sensor chip"
+	depends on I2C
+	help
+	  If you say yes here you get support for 6-Channel PWM-Output
+	  Fan RPM Controller.
+
+	  This driver can also be built as a module.  If so, the module
+	  will be called max31790.
+
 config SENSORS_HTU21
 	tristate "Measurement Specialties HTU21D humidity/temperature sensors"
 	depends on I2C
drivers/hwmon/Makefile +1 -0
index 9e0f3dd..01855ee 100644
@@ -115,6 +115,7 @@ 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_MAX31790)	+= max31790.o
 obj-$(CONFIG_SENSORS_MC13783_ADC)+= mc13783-adc.o
 obj-$(CONFIG_SENSORS_MCP3021)	+= mcp3021.o
 obj-$(CONFIG_SENSORS_MENF21BMC_HWMON) += menf21bmc_hwmon.o
drivers/hwmon/max31790.c new file +598 -0

598 lines not on this page · raw message

Signature · 7 lines
2.4.1


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