Skip to contentSkip to conversation

Add Nuvoton NCT7904 hwmon driver

PATCH Superseded Vadim V. Vlasov 2 participants 6 messages Feb 24, 2015 – Feb 26, 2015 Hardware monitoring

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Add Nuvoton NCT7904 hwmon driver

Tested on Kontron COMe-cSE6 board

---
 drivers/hwmon/Kconfig   |  10 +
 drivers/hwmon/Makefile  |   1 +
 drivers/hwmon/nct7904.c | 551
++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 562 insertions(+)
 create mode 100644 drivers/hwmon/nct7904.c
drivers/hwmon/Kconfig +10 -0
index 110fade..d0d5988 100644
@@ -1145,6 +1145,16 @@ config SENSORS_NCT7802
 	  This driver can also be built as a module.  If so, the module
 	  will be called nct7802.
 
+config SENSORS_NCT7904
+	tristate "Nuvoton NCT7904"
+	depends on X86 && I2C
+	help
+	  If you say yes here you get support for the Nuvoton NCT7904
+	  hardware monitoring chip, including manual fan speed control.
+
+	  This driver can also be built as a module.  If so, the module
+	  will be called nct7904.
+
 config SENSORS_PCF8591
 	tristate "Philips PCF8591 ADC/DAC"
 	depends on I2C
drivers/hwmon/Makefile +1 -0
index 6c94147..b4a40f1 100644
@@ -120,6 +120,7 @@ obj-$(CONFIG_SENSORS_MENF21BMC_HWMON) +menf21bmc_hwmon.o
 obj-$(CONFIG_SENSORS_NCT6683)	+= nct6683.o
 obj-$(CONFIG_SENSORS_NCT6775)	+= nct6775.o
 obj-$(CONFIG_SENSORS_NCT7802)	+= nct7802.o
+obj-$(CONFIG_SENSORS_NCT7904)	+= nct7904.o
 obj-$(CONFIG_SENSORS_NTC_THERMISTOR)	+= ntc_thermistor.o
 obj-$(CONFIG_SENSORS_PC87360)	+= pc87360.o
 obj-$(CONFIG_SENSORS_PC87427)	+= pc87427.o
drivers/hwmon/nct7904.c new file +123 -0

123 lines not on this page · raw message

*/
+static int nct7904_read_reg16(struct nct7904_data *data,
+ unsigned bank, unsigned reg, u8 *buf)
+{
+ struct i2c_client *client = data->client;
+ int ret;
+
+ if (bank > BANK_MAX || reg > 254)
+ return -EINVAL;
+ buf[0] = buf[1] = 0;
+
+ mutex_lock(&data->lock);
+ if (data->bank_sel != bank) {
+ ret = i2c_smbus_write_byte_data(client, BANK_SEL_REG, bank);
+ if (ret < 0) {
+ data->bank_sel = -1;
+ goto out;
+ }
+ data->bank_sel = bank;
+ }
+ ret = i2c_smbus_read_byte_data(client, reg);
+ if (ret >= 0) {
+ buf[0] = ret;
+ ret = i2c_smbus_read_byte_data(client, reg + 1);
+ if (ret >= 0) {
+ buf[1] = ret;
+ ret = 0;
+ }
+ }
+
+out:
+ mutex_unlock(&data->lock);
+ return ret;
+}
+
+/* Write 1-byte register. Returns 0 or -ERRNO on error. */ +static int nct7904_write_reg(struct nct7904_data *data,
+ unsigned bank, unsigned reg, u8 val)
+{
+ struct i2c_client *client = data->client;
+ int ret;
+
+ if (bank > BANK_MAX || reg > 255)
+ return -EINVAL;
+ mutex_lock(&data->lock);
+ if (data->bank_sel != bank) {
+ ret = i2c_smbus_write_byte_data(client, BANK_SEL_REG, bank);
+ if (ret < 0) {
+ data->bank_sel = -1;
+ goto out;
+ }
+ data->bank_sel = bank;
+ }
+ ret = i2c_smbus_write_byte_data(client, reg, val);
+
+out:
+ mutex_unlock(&data->lock);
+ return ret;
+}
+
+/* FANIN ATTR */
+static ssize_t
+show_fan(struct device *dev,
+ struct device_attribute *devattr, char *buf)
+{
+ int index = to_sensor_dev_attr(devattr)->index;
+ struct nct7904_data *data = dev_get_drvdata(dev);
+ int ret;
+ u8 regs[2];
+ unsigned cnt, rpm;
+
+ ret = nct7904_read_reg16(data, BANK_0, FANIN1_HV_REG + index * 2,
regs);
+ if (ret < 0)
+ return ret;
+ cnt = (regs[0] << 5) | (regs[1] & 0x1f);
+ if (cnt = 0x1fff)
+ rpm = 0;
+ else
+ rpm = 1350000 / cnt;
+ return sprintf(buf, "%d\n", rpm);
+}
+
+#define FANIN_ATTR(i) \
+ SENSOR_ATTR(fan##i##_input, S_IRUGO, show_fan, NULL, i - 1)
+
+static struct sensor_device_attribute nct7904_fanin_attrs[] = {
+ FANIN_ATTR(1),
+ FANIN_ATTR(2),
+ FANIN_ATTR(3),
+ FANIN_ATTR(4),
+ FANIN_ATTR(5),
+ FANIN_ATTR(6),
+ FANIN_ATTR(7),
+ FANIN_ATTR(8),
+ FANIN_ATTR(9),
+ FANIN_ATTR(10),
+ FANIN_ATTR(11),
+ FANIN_ATTR(12),
+};
+
+
+/* VSEN ATTR */
+static ssize_t
+show_voltage(struct device *dev,
+ struct device_attribute *devattr, char *buf)
+{
+ int index = to_sensor_dev_attr(devattr)->index;
+ struct nct7904_data *data = dev_get_drvdata(dev);
+ int ret;
+ u8 regs[2];
+ s8 t_hi;
+ unsigned cnt;
+ int volt;
+
+ ret = nct7904_read_reg16(data, BANK_0, VSEN1_HV_REG + index * 2,
regs);
+ if (ret < 0)
+ return ret;
+ cnt = (regs[0] << 3) | (regs[1] & 0x7);
+ if (index < 14)
+ volt = cnt * 2; /* 0.002V scale */
+ else if (index = 17) {
+ /* not a voltage but local temp (signed) */
+ t_hi = regs[0];
+ volt = ((t_hi << 3) | (regs[1] & 0x7)) * 125;
+ } else
+ volt = cnt * 6; /* 0.006V scale */
+
+ return sprintf(buf, "%d\n", volt);
+}
+
+#define VSEN_ATTR(i) \
+ SENSOR_ATTR(in##i##_input, S_IRUGO, show_voltage, NULL, i - 1)
+
+#define VSEN_ATTR_NAME(name, i) \
+ SENSOR_ATTR(name##_input, S_IRUGO, show_voltage, NULL, i - 1)
+
+static struct sensor_device_attribute nct7904_vsen_attrs[] = {
+ VSEN_ATTR(1),
+ VSEN_ATTR(2),
+ VSEN_ATTR(3),
+ VSEN_ATTR(4),
+ VSEN_ATTR(5),
+ VSEN_ATTR(6),
+ VSEN_ATTR(7),
+ VSEN_ATTR(8),
+ VSEN_ATTR(9),
+ VSEN_ATTR(10),
+ VSEN_ATTR(11),
+ VSEN_ATTR(12),
+ VSEN_ATTR(13),
+ VSEN_ATTR(14),
+ VSEN_ATTR(15), /* 3VDD */
+ VSEN_ATTR(16), /* VBAT */
+ VSEN_ATTR_NAME(in20, 17), /* 3VSB */
+ VSEN_ATTR_NAME(temp1, 18), /* local temp */
+ VSEN_ATTR_NAME(in17, 19),
+ VSEN_ATTR_NAME(in18, 20),
+ VSEN_ATTR_NAME(in19, 21),
+};
+
+/* CPU_TEMP ATTR */
+static ssize_t
+show_tcpu(struct device *dev,
+ struct device_attribute *devattr, char *buf)
+{
+ int index = to_sensor_dev_attr(devattr)->index;
+ struct nct7904_data *data = dev_get_drvdata(dev);
+ int ret;
+ s8 regs[2]; /* Signed! */
+ int temp;
+
+ ret = nct7904_read_reg16(data, BANK_0, T_CPU1_HV_REG + index * 2,
regs);
+ if (ret < 0)
+ return ret;
+ temp = ((regs[0] << 3) | (regs[1] & 0x7)) * 125;
+ return sprintf(buf, "%d\n", temp);
+}
+
+/* "temp1_input" reserved for local temp */
+#define T_CPU_ATTR(i) \
+ SENSOR_ATTR(temp##i##_input, S_IRUGO, show_tcpu, NULL, i - 2)
+
+static struct sensor_device_attribute nct7904_tcpu_attrs[] = {
+ T_CPU_ATTR(2),
+ T_CPU_ATTR(3),
+ T_CPU_ATTR(4),
+ T_CPU_ATTR(5),
+ T_CPU_ATTR(6),
+ T_CPU_ATTR(7),
+ T_CPU_ATTR(8),
+ T_CPU_ATTR(9),
+};
+
+/************************PWM ATTR******************************************/
+static ssize_t
+store_pwm(struct device *dev,
+ struct device_attribute *devattr, const char *buf, size_t count)
+{
+ int index = to_sensor_dev_attr(devattr)->index;
+ struct nct7904_data *data = dev_get_drvdata(dev);
+ unsigned long val;
+ int ret;
+
+ if (kstrtoul(buf, 10, &val) < 0)
+ return -EINVAL;
+ if (val > 255)
+ return -EINVAL;
+
+ ret = nct7904_write_reg(data, BANK_3, FANCTL1_OUT_REG + index, val);
+
+ return ret ? ret : count;
+}
+
+static ssize_t
+show_pwm(struct device *dev, struct device_attribute *devattr, char *buf)
+{
+ int index = to_sensor_dev_attr(devattr)->index;
+ struct nct7904_data *data = dev_get_drvdata(dev);
+ int val;
+
+ val = nct7904_read_reg(data, BANK_3, FANCTL1_OUT_REG + index);
+ if (val < 0)
+ return val;
+
+ return sprintf(buf, "%d\n", val);
+}
+
+static ssize_t
+store_mode(struct device *dev,
+ struct device_attribute *devattr, const char *buf, size_t count)
+{
+ int index = to_sensor_dev_attr(devattr)->index;
+ struct nct7904_data *data = dev_get_drvdata(dev);
+ unsigned long val;
+ int ret;
+
+ if (kstrtoul(buf, 10, &val) < 0)
+ return -EINVAL;
+ switch (val) {
+ case 0:
+ case 1:
+ case 2:
+ case 4:
+ case 8:
+ break;
+ default:
+ return -EINVAL;
+ }
+
+ ret = nct7904_write_reg(data, BANK_3, FANCTL1_FMR_REG + index, val);
+
+ return ret ? ret : count;
+}
+
+static ssize_t
+show_mode(struct device *dev, struct device_attribute *devattr, char *buf)
+{
+ int index = to_sensor_dev_attr(devattr)->index;
+ struct nct7904_data *data = dev_get_drvdata(dev);
+ int val;
+
+ val = nct7904_read_reg(data, BANK_3, FANCTL1_FMR_REG + index);
+ if (val < 0)
+ return val;
+
+ return sprintf(buf, "%d\n", val);
+}
+
+#define FANCTL_ATTRS(index) \
+ SENSOR_ATTR(fan##index##_pwm, S_IRUGO | S_IWUSR, \
+ show_pwm, store_pwm, index - 1), \
+ SENSOR_ATTR(fan##index##_mode, S_IRUGO | S_IWUSR, \
+ show_mode, store_mode, index - 1)
+
+/* 2 attributes per channel: pwm and mode */
+static struct sensor_device_attribute nct7904_fanctl_attrs[] = {
+ FANCTL_ATTRS(1),
+ FANCTL_ATTRS(2),
+ FANCTL_ATTRS(3),
+ FANCTL_ATTRS(4),
+};
+
+
+static struct attribute *nct7904_attrs
+ [FANIN_MAX + VSEN_MAX + 2*FANCTL_MAX + TCPU_MAX + TEMP_MAX + 1];
+ATTRIBUTE_GROUPS(nct7904);
+
+/* Return 0 if detection is successful, -ENODEV otherwise */ +static int
+nct7904_detect(struct i2c_client *client, struct i2c_board_info *info) +{
+ struct i2c_adapter *adapter = client->adapter;
+
+ if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_READ_BYTE)
+ && !i2c_check_functionality(adapter,
+ I2C_FUNC_SMBUS_WRITE_BYTE_DATA))
+ return -ENODEV;
+
+ /* Determine the chip type. */
+ if (!(i2c_smbus_read_byte_data(client, VENDOR_ID_REG) = NUVOTON_ID)
+ || !(i2c_smbus_read_byte_data(client, CHIP_ID_REG) = NCT7904_ID))
+ return -ENODEV;
+
+ strlcpy(info->type, "nct7904", I2C_NAME_SIZE);
+
+ return 0;
+}
+
+static int
+nct7904_probe(struct i2c_client *client, const struct i2c_device_id *id)
+{
+ struct nct7904_data *data;
+ int ret;
+ int i, j;
+ u32 mask;
+ u8 buf[2];
+
+ data = devm_kzalloc(&client->dev,
+ sizeof(struct nct7904_data), GFP_KERNEL);
+ if (!data)
+ return -ENOMEM;
+
+ data->client = client;
+ mutex_init(&data->lock);
+ data->bank_sel = -1;
+ i2c_set_clientdata(client, data);
+
+ /* Setup attributes. (TODO: skip unused sensors) */
+ /* FANIN attributes */
+ ret = nct7904_read_reg16(data, BANK_0, FANIN_CTRL0_REG, buf);
+ if (ret)
+ goto err1;
+ mask = buf[0] | (buf[1] << 8);
+
+ for (i = 0, j = 0; j < FANIN_MAX; j++) {
+ if (mask & (1 << j))
+ nct7904_attrs[i++] + &nct7904_fanin_attrs[j].dev_attr.attr;
+ }
+
+ /* VSEN attributes */
+ mask = 0;
+ ret = nct7904_read_reg16(data, BANK_0, VT_ADC_CTRL0_REG, buf);
+ if (ret = 0)
+ mask = buf[0] | (buf[1] << 8);
+ ret = nct7904_read_reg(data, BANK_0, VT_ADC_CTRL2_REG);
+ if (ret >= 0)
+ mask |= (ret << 16);
+ for (j = 0; j < VSEN_MAX; j++) {
+ if (mask & (1 << j))
+ nct7904_attrs[i++] + &nct7904_vsen_attrs[j].dev_attr.attr;
+ }
+
+ /* CPU_TEMP attributes */
+ ret = nct7904_read_reg16(data, BANK_0, DTS_T_CTRL0_REG, buf);
+ if (ret)
+ goto err1;
+ mask = (buf[0] & 0xf) | (buf[1] << 4);
+ for (j = 0; j < TCPU_MAX; j++) {
+ if (mask & (1 << j))
+ nct7904_attrs[i++] + &nct7904_tcpu_attrs[j].dev_attr.attr;
+ }
+
+ /* Fan control */
+ for (j = 0; j < FANCTL_MAX; j++) {
+ nct7904_attrs[i++] = &nct7904_fanctl_attrs[2*j].dev_attr.attr;
+ nct7904_attrs[i++] = &nct7904_fanctl_attrs[2*j+1].dev_attr.attr;
+ }
+
+ data->hwmon_dev = devm_hwmon_device_register_with_groups(&client->dev,
+ client->name, data, nct7904_groups);
+ if (IS_ERR(data->hwmon_dev)) {
+ ret = PTR_ERR(data->hwmon_dev);
+ goto err1;
+ }
+
+ dev_info(&client->dev, "NCT7904 chip version 0x%x\n",
+ nct7904_read_reg(data, 0, VERSION_ID_REG));
+
+ return 0;
+
+err1:
+ return ret;
+}
+
+static int
+nct7904_remove(struct i2c_client *client)
+{
+ return 0;
+}
+
+
+static const struct i2c_device_id nct7904_id[] = {
+ {"nct7904", 0},
+ {}
+};
+
+static struct i2c_driver nct7904_driver = {
+ .class = I2C_CLASS_HWMON,
+ .driver = {
+ .name = "nct7904",
+ },
+ .probe = nct7904_probe,
+ .remove = nct7904_remove,
+ .id_table = nct7904_id,
+ .detect = nct7904_detect,
+ .address_list = normal_i2c,
+};
+
+static int __init
+nct7904_init(void)
+{
+ return i2c_add_driver(&nct7904_driver);
+}
+
+static void __exit
+nct7904_exit(void)
+{
+ i2c_del_driver(&nct7904_driver);
+}
+
+module_init(nct7904_init);
+module_exit(nct7904_exit);
+
+MODULE_AUTHOR("Vadim V. Vlasov <[email protected]>"); +MODULE_DESCRIPTION("Hwmon driver for NUVOTON NCT7904"); +MODULE_LICENSE("GPL");

Signature · 9 lines
1.9.3




_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors
maintainer
#1 lore raw
Quoted 3 lines · Vadim V. Vlasov

Tested on Kontron COMe-cSE6 board

Hi Vadim,

Unfortunately, your patch is corrupted.

Please run the patch through checkpatch --strict (that also tells you where it is corrupted) and fix what it complains about. Additional (preliminary) review comments below.

Thanks,
Guenter

Quoted 137 lines
---
 drivers/hwmon/Kconfig   |  10 +
 drivers/hwmon/Makefile  |   1 +
 drivers/hwmon/nct7904.c | 551
++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 562 insertions(+)
 create mode 100644 drivers/hwmon/nct7904.c
drivers/hwmon/Kconfig +10 -0
index 110fade..d0d5988 100644
@@ -1145,6 +1145,16 @@ config SENSORS_NCT7802
 	  This driver can also be built as a module.  If so, the module
 	  will be called nct7802.
 
+config SENSORS_NCT7904
+	tristate "Nuvoton NCT7904"
+	depends on X86 && I2C
+	help
+	  If you say yes here you get support for the Nuvoton NCT7904
+	  hardware monitoring chip, including manual fan speed control.
+
+	  This driver can also be built as a module.  If so, the module
+	  will be called nct7904.
+
 config SENSORS_PCF8591
 	tristate "Philips PCF8591 ADC/DAC"
 	depends on I2C
drivers/hwmon/Makefile +1 -0
index 6c94147..b4a40f1 100644
@@ -120,6 +120,7 @@ obj-$(CONFIG_SENSORS_MENF21BMC_HWMON) +> menf21bmc_hwmon.o
 obj-$(CONFIG_SENSORS_NCT6683)	+= nct6683.o
 obj-$(CONFIG_SENSORS_NCT6775)	+= nct6775.o
 obj-$(CONFIG_SENSORS_NCT7802)	+= nct7802.o
+obj-$(CONFIG_SENSORS_NCT7904)	+= nct7904.o
 obj-$(CONFIG_SENSORS_NTC_THERMISTOR)	+= ntc_thermistor.o
 obj-$(CONFIG_SENSORS_PC87360)	+= pc87360.o
 obj-$(CONFIG_SENSORS_PC87427)	+= pc87427.o
drivers/hwmon/nct7904.c new file +90 -0
new file mode 100644
index 0000000..84e524b
@@ -0,0 +1,551 @@
+/*
+ * nct7904.c - driver for Nuvoton NCT7904D.
+ *
+ * Copyright (c) 2015 Kontron
+ * Author: Vadim V. Vlasov <[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/module.h>
+#include <linux/init.h>
+#include <linux/slab.h>
+#include <linux/i2c.h>
+#include <linux/hwmon.h>
+#include <linux/hwmon-sysfs.h>
+#include <linux/spinlock.h>
+#include <linux/delay.h>
+
+#define VENDOR_ID_REG		0x7A	/* Any bank */
+#define NUVOTON_ID		0x50
+#define CHIP_ID_REG		0x7B	/* Any bank */
+#define NCT7904_ID		0xC5
+#define VERSION_ID_REG		0x7C	/* Any bank */
+
+#define BANK_SEL_REG		0xFF
+#define BANK_0			0x00
+#define BANK_1			0x01
+#define BANK_2			0x02
+#define BANK_3			0x03
+#define BANK_4			0x04
+#define BANK_MAX		0x04
+
+#define FANIN_MAX		12	/* Counted from 1 */
+#define VSEN_MAX		21	/* VSEN1..14, 3VDD, VBAT, V3VSB,
+					   LTD (not a voltage), VSEN17..19 */
+#define FANCTL_MAX		4	/* Counted from 1 */
+#define TCPU_MAX		8	/* Counted from 1 */
+#define TEMP_MAX		4	/* Counted from 1 */
+
+#define VT_ADC_CTRL0_REG	0x20	/* Bank 0 */
+#define VT_ADC_CTRL1_REG	0x21	/* Bank 0 */
+#define VT_ADC_CTRL2_REG	0x22	/* Bank 0 */
+#define FANIN_CTRL0_REG		0x24
+#define FANIN_CTRL1_REG		0x25
+#define DTS_T_CTRL0_REG		0x26
+#define DTS_T_CTRL1_REG		0x27
+#define VT_ADC_MD_REG		0x2E
+
+#define VSEN1_HV_REG		0x40	/* Bank 0; 2 regs (HV/LV) per sensor */
+#define TEMP_CH1_HV_REG		0x42	/* Bank 0; same as VSEN2_HV */
+#define FANIN1_HV_REG		0x80	/* Bank 0; 2 regs (HV/LV) per sensor */
+#define T_CPU1_HV_REG		0xA0	/* Bank 0; 2 regs (HV/LV) per sensor */
+
+#define PRTS_REG		0x03	/* Bank 2 */
+#define FANCTL1_FMR_REG		0x00	/* Bank 3; 1 reg per channel */
+#define FANCTL1_OUT_REG		0x10	/* Bank 3; 1 reg per channel */
+
+
+const unsigned short normal_i2c[] = {
+	0x2d, 0x2e, I2C_CLIENT_END
+};
+
+
+struct nct7904_data {
+	struct i2c_client *client;
+	struct device *hwmon_dev;
+	struct mutex lock;
+	int bank_sel;
+};
+
+/*
+ * Implemented features:
+ * - Fan inputs (rpm)
+ * - Voltage sensors (mV)
+ * - DTS/TSI and local temperature sensors (degree Celsius)
+ * - Fan PWM control
+ * - Setting Fan to manual mode
+ *
+ * Not implemented features:
+ * - SmartFan control
+ * - Watchdog
+ * - GPIO

drivers/hwmon/nct7904.c line 100

Quoted 10 lines
drivers/hwmon/nct7904.c new file +10 -0
+ * - external temperature sensors
+ * - SMI
+ * - min/max values
+ * - many other...
+ */
+
+/* Access functions */
+/* Read 1-byte register. Returns unsigned reg or -ERRNO on error. */
+static int nct7904_read_reg(struct nct7904_data *data,
+	unsigned bank, unsigned reg)

In practice you never access a register without its page. Consider merging <bank, register> into a 16-bit value and decode it here.

This way a register define would automatically include the page, which in turn would reduce the risk of accicdentially accessing a register from the wrong page.

drivers/hwmon/nct7904.c line 106

Quoted 6 lines
drivers/hwmon/nct7904.c new file +6 -0
+{
+	struct i2c_client *client = data->client;
+	int ret;
+
+	if (bank > BANK_MAX || reg > 255)
+		return -EINVAL;

Unnecessary check. You should trust your code. If the code is buggy, fix it. Besides, it does not (and can not) validate that you are accsssing register on the correct page, which is much more likely to be wrong.

drivers/hwmon/nct7904.c line 115

Quoted 9 lines
drivers/hwmon/nct7904.c new file +9 -0
+	mutex_lock(&data->lock);
+	if (data->bank_sel != bank) {
+		ret = i2c_smbus_write_byte_data(client, BANK_SEL_REG, bank);
+		if (ret < 0) {
+			data->bank_sel = -1;
+			goto out;
+		}
+		data->bank_sel = bank;
+	}

Please factor out the function to select the bank; the code is implemented three times.

drivers/hwmon/nct7904.c

Quoted 11 lines
drivers/hwmon/nct7904.c new file +8 -0
+	ret = i2c_smbus_read_byte_data(client, reg);
+
+out:
+	mutex_unlock(&data->lock);
+	return ret;
+}
+
+/* Read 2-byte register into the buffer. Returns 0 or -ERRNO on error.

*/

drivers/hwmon/nct7904.c new file +2 -0
+static int nct7904_read_reg16(struct nct7904_data *data,
+	unsigned bank, unsigned reg, u8 *buf)

Please return the value itself as int, combined with the error code. Given the oddity of high/low value calculation, consider adding the number of valid bits (ie 3 or 11 for an 11-bit register, whatever you prefer) as parameter.

drivers/hwmon/nct7904.c line 106

Quoted 6 lines
drivers/hwmon/nct7904.c new file +6 -0
+{
+	struct i2c_client *client = data->client;
+	int ret;
+
+	if (bank > BANK_MAX || reg > 254)
+		return -EINVAL;

Same as above.

Quoted 25 lines
drivers/hwmon/nct7904.c new file +25 -0
+	buf[0] = buf[1] = 0;
+
+	mutex_lock(&data->lock);
+	if (data->bank_sel != bank) {
+		ret = i2c_smbus_write_byte_data(client, BANK_SEL_REG, bank);
+		if (ret < 0) {
+			data->bank_sel = -1;
+			goto out;
+		}
+		data->bank_sel = bank;
+	}
+	ret = i2c_smbus_read_byte_data(client, reg);
+	if (ret >= 0) {
+		buf[0] = ret;
+		ret = i2c_smbus_read_byte_data(client, reg + 1);
+		if (ret >= 0) {
+			buf[1] = ret;
+			ret = 0;
+		}
+	}
+
+out:
+	mutex_unlock(&data->lock);
+	return ret;
+}

drivers/hwmon/nct7904.c

Quoted 10 lines
drivers/hwmon/nct7904.c new file +10 -0
+
+/* Write 1-byte register. Returns 0 or -ERRNO on error. */
+static int nct7904_write_reg(struct nct7904_data *data,
+	unsigned bank, unsigned reg, u8 val)
+{
+	struct i2c_client *client = data->client;
+	int ret;
+
+	if (bank > BANK_MAX || reg > 255)
+		return -EINVAL;

Same as above.

Quoted 10 lines
drivers/hwmon/nct7904.c new file +10 -0
+	mutex_lock(&data->lock);
+	if (data->bank_sel != bank) {
+		ret = i2c_smbus_write_byte_data(client, BANK_SEL_REG, bank);
+		if (ret < 0) {
+			data->bank_sel = -1;
+			goto out;
+		}
+		data->bank_sel = bank;
+	}
+	ret = i2c_smbus_write_byte_data(client, reg, val);

drivers/hwmon/nct7904.c

Quoted 10 lines
drivers/hwmon/nct7904.c new file +10 -0
+
+out:
+	mutex_unlock(&data->lock);
+	return ret;
+}
+
+/* FANIN ATTR */
+static ssize_t
+show_fan(struct device *dev,
+	struct device_attribute *devattr, char *buf)

You are quite inconsistent with multi-line function declarations. Above you use

static int func()

Please be consistent.

Quoted 9 lines
drivers/hwmon/nct7904.c new file +8 -0
+{
+	int index = to_sensor_dev_attr(devattr)->index;
+	struct nct7904_data *data = dev_get_drvdata(dev);
+	int ret;
+	u8 regs[2];
+	unsigned cnt, rpm;
+
+	ret = nct7904_read_reg16(data, BANK_0, FANIN1_HV_REG + index * 2,

regs);

drivers/hwmon/nct7904.c

Quoted 8 lines
drivers/hwmon/nct7904.c new file +8 -0
+	if (ret < 0)
+		return ret;
+	cnt = (regs[0] << 5) | (regs[1] & 0x1f);
+	if (cnt = 0x1fff)
+		rpm = 0;
+	else
+		rpm = 1350000 / cnt;
+	return sprintf(buf, "%d\n", rpm);

rpm is unsigned. %u.

drivers/hwmon/nct7904.c line 122

Quoted 5 lines
drivers/hwmon/nct7904.c new file +5 -0
+}
+
+#define FANIN_ATTR(i)	\
+	SENSOR_ATTR(fan##i##_input, S_IRUGO, show_fan, NULL, i - 1)
+

Please just add SENSOR_ATTR() below.

Quoted 31 lines
drivers/hwmon/nct7904.c new file +30 -0
+static struct sensor_device_attribute nct7904_fanin_attrs[] = {
+	FANIN_ATTR(1),
+	FANIN_ATTR(2),
+	FANIN_ATTR(3),
+	FANIN_ATTR(4),
+	FANIN_ATTR(5),
+	FANIN_ATTR(6),
+	FANIN_ATTR(7),
+	FANIN_ATTR(8),
+	FANIN_ATTR(9),
+	FANIN_ATTR(10),
+	FANIN_ATTR(11),
+	FANIN_ATTR(12),
+};
+
+
+/* VSEN ATTR */
+static ssize_t
+show_voltage(struct device *dev,
+	struct device_attribute *devattr, char *buf)
+{
+	int index = to_sensor_dev_attr(devattr)->index;
+	struct nct7904_data *data = dev_get_drvdata(dev);
+	int ret;
+	u8 regs[2];
+	s8 t_hi;
+	unsigned cnt;
+	int volt;
+
+	ret = nct7904_read_reg16(data, BANK_0, VSEN1_HV_REG + index * 2,

regs);

drivers/hwmon/nct7904.c

Quoted 9 lines
drivers/hwmon/nct7904.c new file +9 -0
+	if (ret < 0)
+		return ret;
+	cnt = (regs[0] << 3) | (regs[1] & 0x7);
+	if (index < 14)
+		volt = cnt * 2; /* 0.002V scale */
+	else if (index = 17) {
+		/* not a voltage but local temp (signed) */
+		t_hi = regs[0];
+		volt = ((t_hi << 3) | (regs[1] & 0x7)) * 125;

This should be a separate function. It doesn't make sense to have it here just because it is in the same group of registers.

drivers/hwmon/nct7904.c

Quoted 12 lines
drivers/hwmon/nct7904.c new file +12 -0
+	} else
+		volt = cnt * 6; /* 0.006V scale */
+
+	return sprintf(buf, "%d\n", volt);
+}
+
+#define VSEN_ATTR(i)	\
+	SENSOR_ATTR(in##i##_input, S_IRUGO, show_voltage, NULL, i - 1)
+
+#define VSEN_ATTR_NAME(name, i)	\
+	SENSOR_ATTR(name##_input, S_IRUGO, show_voltage, NULL, i - 1)
+

Please open code all sensors declarations.

Quoted 12 lines
drivers/hwmon/nct7904.c new file +12 -0
+static struct sensor_device_attribute nct7904_vsen_attrs[] = {
+	VSEN_ATTR(1),
+	VSEN_ATTR(2),
+	VSEN_ATTR(3),
+	VSEN_ATTR(4),
+	VSEN_ATTR(5),
+	VSEN_ATTR(6),
+	VSEN_ATTR(7),
+	VSEN_ATTR(8),
+	VSEN_ATTR(9),
+	VSEN_ATTR(10),
+	VSEN_ATTR(11),

drivers/hwmon/nct7904.c

Quoted 10 lines
drivers/hwmon/nct7904.c new file +10 -0
+	VSEN_ATTR(12),
+	VSEN_ATTR(13),
+	VSEN_ATTR(14),
+	VSEN_ATTR(15),			/* 3VDD */
+	VSEN_ATTR(16),			/* VBAT */
+	VSEN_ATTR_NAME(in20, 17),	/* 3VSB */
+	VSEN_ATTR_NAME(temp1, 18),	/* local temp */
+	VSEN_ATTR_NAME(in17, 19),
+	VSEN_ATTR_NAME(in18, 20),
+	VSEN_ATTR_NAME(in19, 21),

Why the odd order ? I assume it has to do with the bit order in one of the configuration registers, but you should explain that.

drivers/hwmon/nct7904.c

Quoted 11 lines
drivers/hwmon/nct7904.c new file +11 -0
+};
+
+/* CPU_TEMP ATTR */
+static ssize_t
+show_tcpu(struct device *dev,
+	struct device_attribute *devattr, char *buf)
+{
+	int index = to_sensor_dev_attr(devattr)->index;
+	struct nct7904_data *data = dev_get_drvdata(dev);
+	int ret;
+	s8 regs[2]; /* Signed! */

yes, but only one of the two registers, so this is a bit awkward.

If you have nct7904_read_reg16 return a 16-bit value, which would be unsigned, you can use sign_extend32() to convert the result to a signed value.

Quoted 4 lines
drivers/hwmon/nct7904.c new file +3 -0
+	int temp;
+
+	ret = nct7904_read_reg16(data, BANK_0, T_CPU1_HV_REG + index * 2,

regs);

Here is one corruption.

Quoted 71 lines
drivers/hwmon/nct7904.c new file +22 -0
+	if (ret < 0)
+		return ret;
+	temp = ((regs[0] << 3) | (regs[1] & 0x7)) * 125;
+	return sprintf(buf, "%d\n", temp);
+}
+
+/* "temp1_input" reserved for local temp */
+#define T_CPU_ATTR(i)	\
+	SENSOR_ATTR(temp##i##_input, S_IRUGO, show_tcpu, NULL, i - 2)
+
+static struct sensor_device_attribute nct7904_tcpu_attrs[] = {
+	T_CPU_ATTR(2),
+	T_CPU_ATTR(3),
+	T_CPU_ATTR(4),
+	T_CPU_ATTR(5),
+	T_CPU_ATTR(6),
+	T_CPU_ATTR(7),
+	T_CPU_ATTR(8),
+	T_CPU_ATTR(9),
+};
+
+/************************PWM

ATTR******************************************/

drivers/hwmon/nct7904.c new file +21 -0
+static ssize_t
+store_pwm(struct device *dev,
+	  struct device_attribute *devattr, const char *buf, size_t count)
+{
+	int index = to_sensor_dev_attr(devattr)->index;
+	struct nct7904_data *data = dev_get_drvdata(dev);
+	unsigned long val;
+	int ret;
+
+	if (kstrtoul(buf, 10, &val) < 0)
+		return -EINVAL;
+	if (val > 255)
+		return -EINVAL;
+
+	ret = nct7904_write_reg(data, BANK_3, FANCTL1_OUT_REG + index, val);
+
+	return ret ? ret : count;
+}
+
+static ssize_t
+show_pwm(struct device *dev, struct device_attribute *devattr, char

*buf)

drivers/hwmon/nct7904.c new file +26 -0
+{
+	int index = to_sensor_dev_attr(devattr)->index;
+	struct nct7904_data *data = dev_get_drvdata(dev);
+	int val;
+
+	val = nct7904_read_reg(data, BANK_3, FANCTL1_OUT_REG + index);
+	if (val < 0)
+		return val;
+
+	return sprintf(buf, "%d\n", val);
+}
+
+static ssize_t
+store_mode(struct device *dev,
+	  struct device_attribute *devattr, const char *buf, size_t count)
+{
+	int index = to_sensor_dev_attr(devattr)->index;
+	struct nct7904_data *data = dev_get_drvdata(dev);
+	unsigned long val;
+	int ret;
+
+	if (kstrtoul(buf, 10, &val) < 0)
+		return -EINVAL;
+	switch (val) {
+	case 0:
+	case 1:

drivers/hwmon/nct7904.c

Quoted 10 lines
drivers/hwmon/nct7904.c new file +10 -0
+	case 2:
+	case 4:
+	case 8:
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	ret = nct7904_write_reg(data, BANK_3, FANCTL1_FMR_REG + index, val);
+

This is really combining two configurations into one, the fan control mode (manual or automatic), and its association with a temperature source as selected in bank 3 registers 0x09..0x0c. If you want to be able to configure that, it will need to be two separate attributes, one for the mode and one for the temperature source.

Configuring automatic mode without also providing the means to configure registers 0x09..0x0c doesn't really make much sense.

Quoted 37 lines
drivers/hwmon/nct7904.c new file +5 -0
+	return ret ? ret : count;
+}
+
+static ssize_t
+show_mode(struct device *dev, struct device_attribute *devattr, char

*buf)

drivers/hwmon/nct7904.c new file +31 -0
+{
+	int index = to_sensor_dev_attr(devattr)->index;
+	struct nct7904_data *data = dev_get_drvdata(dev);
+	int val;
+
+	val = nct7904_read_reg(data, BANK_3, FANCTL1_FMR_REG + index);
+	if (val < 0)
+		return val;
+
+	return sprintf(buf, "%d\n", val);
+}
+
+#define FANCTL_ATTRS(index)	\
+	SENSOR_ATTR(fan##index##_pwm, S_IRUGO | S_IWUSR,	\
+		show_pwm, store_pwm, index - 1),		\
+	SENSOR_ATTR(fan##index##_mode, S_IRUGO | S_IWUSR,	\
+		show_mode, store_mode, index - 1)
+
+/* 2 attributes per channel: pwm and mode */
+static struct sensor_device_attribute nct7904_fanctl_attrs[] = {
+	FANCTL_ATTRS(1),
+	FANCTL_ATTRS(2),
+	FANCTL_ATTRS(3),
+	FANCTL_ATTRS(4),
+};
+
+
+static struct attribute *nct7904_attrs
+	[FANIN_MAX + VSEN_MAX + 2*FANCTL_MAX + TCPU_MAX + TEMP_MAX + 1];
+ATTRIBUTE_GROUPS(nct7904);
+

drivers/hwmon/nct7904.c

Quoted 10 lines
drivers/hwmon/nct7904.c new file +10 -0
+/* Return 0 if detection is successful, -ENODEV otherwise */
+static int
+nct7904_detect(struct i2c_client *client, struct i2c_board_info *info)
+{
+	struct i2c_adapter *adapter = client->adapter;
+
+	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_READ_BYTE)
+	    && !i2c_check_functionality(adapter,
+					I2C_FUNC_SMBUS_WRITE_BYTE_DATA))
+		return -ENODEV;

You only need to call i2c_check_functionality once, with
I2C_FUNC_SMBUS_READ_BYTE | I2C_FUNC_SMBUS_WRITE_BYTE_DATA
as parameter.

drivers/hwmon/nct7904.c

Quoted 6 lines
drivers/hwmon/nct7904.c new file +6 -0
+
+	/* Determine the chip type. */
+	if (!(i2c_smbus_read_byte_data(client, VENDOR_ID_REG) = NUVOTON_ID)
+	    || !(i2c_smbus_read_byte_data(client, CHIP_ID_REG) = NCT7904_ID))
+		return -ENODEV;
+

Please also check the upper 4 bit of the device ID register (named as VERSION_ID_REG in your code), and that the upper 5 bit of the bank select register are 0, to improve detection accuracy.

Quoted 14 lines
drivers/hwmon/nct7904.c new file +7 -0
+	strlcpy(info->type, "nct7904", I2C_NAME_SIZE);
+
+	return 0;
+}
+
+static int
+nct7904_probe(struct i2c_client *client, const struct i2c_device_id

*id)

drivers/hwmon/nct7904.c new file +6 -0
+{
+	struct nct7904_data *data;
+	int ret;
+	int i, j;
+	u32 mask;
+	u8 buf[2];

drivers/hwmon/nct7904.c

Quoted 10 lines
drivers/hwmon/nct7904.c new file +10 -0
+
+	data = devm_kzalloc(&client->dev,
+			sizeof(struct nct7904_data), GFP_KERNEL);
+	if (!data)
+		return -ENOMEM;
+
+	data->client = client;
+	mutex_init(&data->lock);
+	data->bank_sel = -1;
+	i2c_set_clientdata(client, data);

You don't read data from clientdata, so you don't need to save it there.

drivers/hwmon/nct7904.c

Quoted 2 lines
drivers/hwmon/nct7904.c new file +2 -0
+
+	/* Setup attributes. (TODO: skip unused sensors) */

Is this still TODO ?

drivers/hwmon/nct7904.c

Quoted 4 lines
drivers/hwmon/nct7904.c new file +4 -0
+	/* FANIN attributes */
+	ret = nct7904_read_reg16(data, BANK_0, FANIN_CTRL0_REG, buf);
+	if (ret)
+		goto err1;

Unnecessary goto.

Quoted 9 lines
drivers/hwmon/nct7904.c new file +9 -0
+	mask = buf[0] | (buf[1] << 8);
+
+	for (i = 0, j = 0; j < FANIN_MAX; j++) {
+		if (mask & (1 << j))
+			nct7904_attrs[i++] > +				&nct7904_fanin_attrs[j].dev_attr.attr;
+	}
+
+	/* VSEN attributes */
+	mask = 0;

drivers/hwmon/nct7904.c

Quoted 10 lines
drivers/hwmon/nct7904.c new file +10 -0
+	ret = nct7904_read_reg16(data, BANK_0, VT_ADC_CTRL0_REG, buf);
+	if (ret = 0)
+		mask = buf[0] | (buf[1] << 8);
+	ret = nct7904_read_reg(data, BANK_0, VT_ADC_CTRL2_REG);
+	if (ret >= 0)
+		mask |= (ret << 16);
+	for (j = 0; j < VSEN_MAX; j++) {
+		if (mask & (1 << j))
+			nct7904_attrs[i++] > +				&nct7904_vsen_attrs[j].dev_attr.attr;
+	}

Please use is_visible() to determine if a sensor is configured or not, and use the commonly used means to declare the list of sensors. You might consider using separate groups for the different sensor types to simplify that task.

drivers/hwmon/nct7904.c

Quoted 5 lines
drivers/hwmon/nct7904.c new file +5 -0
+
+	/* CPU_TEMP attributes */
+	ret = nct7904_read_reg16(data, BANK_0, DTS_T_CTRL0_REG, buf);
+	if (ret)
+		goto err1;

Unnecessary goto. Just return ret.

Quoted 4 lines
drivers/hwmon/nct7904.c new file +4 -0
+	mask = (buf[0] & 0xf) | (buf[1] << 4);
+	for (j = 0; j < TCPU_MAX; j++) {
+		if (mask & (1 << j))
+			nct7904_attrs[i++] > +				&nct7904_tcpu_attrs[j].dev_attr.attr;

drivers/hwmon/nct7904.c

Quoted 10 lines
drivers/hwmon/nct7904.c new file +10 -0
+	}
+
+	/* Fan control */
+	for (j = 0; j < FANCTL_MAX; j++) {
+		nct7904_attrs[i++] = &nct7904_fanctl_attrs[2*j].dev_attr.attr;
+		nct7904_attrs[i++] = &nct7904_fanctl_attrs[2*j+1].dev_attr.attr;
+	}
+
+	data->hwmon_dev = devm_hwmon_device_register_with_groups(&client->dev,
+				client->name, data, nct7904_groups);

Storing this information in struct nct7904_data is unnecessary. Please keep hwmon_dev local.

drivers/hwmon/nct7904.c

Quoted 3 lines
drivers/hwmon/nct7904.c new file +3 -0
+	if (IS_ERR(data->hwmon_dev)) {
+		ret = PTR_ERR(data->hwmon_dev);
+		goto err1;

This goto is unnecessary.

drivers/hwmon/nct7904.c

Quoted 4 lines
drivers/hwmon/nct7904.c new file +4 -0
+	}
+
+	dev_info(&client->dev, "NCT7904 chip version 0x%x\n",
+		 nct7904_read_reg(data, 0, VERSION_ID_REG));

This is really the device ID register, and the version number is in the lower 4 bits.

Either case, please consider removing this message. Ultimately it is just noise and irrelevant for the user. Then you can just return PTR_ERR_OR_ZERO(hwmon_dev), as many other drivers do.

drivers/hwmon/nct7904.c

Quoted 12 lines
drivers/hwmon/nct7904.c new file +12 -0
+
+	return 0;
+
+err1:
+	return ret;
+}
+
+static int
+nct7904_remove(struct i2c_client *client)
+{
+	return 0;
+}

This function does not do anything and therefore is not necessary.

Quoted 24 lines
drivers/hwmon/nct7904.c new file +24 -0
+
+
+static const struct i2c_device_id nct7904_id[] = {
+	{"nct7904", 0},
+	{}
+};
+
+static struct i2c_driver nct7904_driver = {
+	.class = I2C_CLASS_HWMON,
+	.driver = {
+		.name = "nct7904",
+	},
+	.probe = nct7904_probe,
+	.remove = nct7904_remove,
+	.id_table = nct7904_id,
+	.detect = nct7904_detect,
+	.address_list = normal_i2c,
+};
+
+static int __init
+nct7904_init(void)
+{
+	return i2c_add_driver(&nct7904_driver);
+}

drivers/hwmon/nct7904.c

Quoted 10 lines
drivers/hwmon/nct7904.c new file +10 -0
+
+static void __exit
+nct7904_exit(void)
+{
+	i2c_del_driver(&nct7904_driver);
+}
+
+module_init(nct7904_init);
+module_exit(nct7904_exit);
+

Please use module_i2c_driver().

Quoted 8 lines
drivers/hwmon/nct7904.c new file +3 -0
+MODULE_AUTHOR("Vadim V. Vlasov <[email protected]>");
+MODULE_DESCRIPTION("Hwmon driver for NUVOTON NCT7904");
+MODULE_LICENSE("GPL");
Signature · 1 line
1.9.3

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

#2 lore raw
In reply to Vadim V. Vlasov

Hi Guenter,
Thank you for the comments.

Please find the new version of the patch and my comments below.

Thanks.
Vadim.

Quoted 10 lines · Guenter Roeck

On Tue, Feb 24, 2015 at 07:09:48PM +0300, Vadim V. Vlasov wrote:
...

Review

Quoted 4 lines
(quoted diff) +4 -0
+/* Access functions */
+/* Read 1-byte register. Returns unsigned reg or -ERRNO on error. */
+static int nct7904_read_reg(struct nct7904_data *data,
+	unsigned bank, unsigned reg)

In practice you never access a register without its page. Consider merging <bank, register> into a 16-bit value and decode it here.

I don't think it is a good idea.

Quoted 4 lines

This way a register define would automatically include the page, which in turn would reduce the risk of accicdentially accessing a register from the wrong page.

You wrote that I'm supposed to trust my code :)

Quoted 13 lines

Review

Quoted 7 lines
(quoted diff) +6 -0
+static ssize_t
+store_mode(struct device *dev,
+	  struct device_attribute *devattr, const char *buf, size_t count)
+{
[...]
+	ret = nct7904_write_reg(data, BANK_3, FANCTL1_FMR_REG + index, val);
+

This is really combining two configurations into one, the fan control mode (manual or automatic), and its association with a temperature source as selected in bank 3 registers 0x09..0x0c. If you want to be able to configure that, it will need to be two separate attributes, one for the mode and one for the temperature source.

Quoted 2 lines

Configuring automatic mode without also providing the means to configure registers 0x09..0x0c doesn't really make much sense.

No, I don't want to be able to configure automatic mode. I presume that BIOS would do that better. The only thing I want to provide is the ability to turn a fan into manual mode, play with RPM and return it back to whatever automatic settings were provided by BIOS.

From: "Vadim V. Vlasov" <[email protected]>
Date: Thu, 26 Feb 2015 18:05:01 +0300
Subject: [PATCH] Add Nuvoton NCT7904 hwmon driver

---
 drivers/hwmon/Kconfig   |  10 +
 drivers/hwmon/Makefile  |   1 +
 drivers/hwmon/nct7904.c | 607 ++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 618 insertions(+)
 create mode 100644 drivers/hwmon/nct7904.c
drivers/hwmon/Kconfig +10 -0
index 110fade..d0d5988 100644
@@ -1145,6 +1145,16 @@ config SENSORS_NCT7802
 	  This driver can also be built as a module.  If so, the module
 	  will be called nct7802.
 
+config SENSORS_NCT7904
+	tristate "Nuvoton NCT7904"
+	depends on X86 && I2C
+	help
+	  If you say yes here you get support for the Nuvoton NCT7904
+	  hardware monitoring chip, including manual fan speed control.
+
+	  This driver can also be built as a module.  If so, the module
+	  will be called nct7904.
+
 config SENSORS_PCF8591
 	tristate "Philips PCF8591 ADC/DAC"
 	depends on I2C
drivers/hwmon/Makefile +1 -0
index 6c94147..b4a40f1 100644
@@ -120,6 +120,7 @@ obj-$(CONFIG_SENSORS_MENF21BMC_HWMON) += menf21bmc_hwmon.o
 obj-$(CONFIG_SENSORS_NCT6683)	+= nct6683.o
 obj-$(CONFIG_SENSORS_NCT6775)	+= nct6775.o
 obj-$(CONFIG_SENSORS_NCT7802)	+= nct7802.o
+obj-$(CONFIG_SENSORS_NCT7904)	+= nct7904.o
 obj-$(CONFIG_SENSORS_NTC_THERMISTOR)	+= ntc_thermistor.o
 obj-$(CONFIG_SENSORS_PC87360)	+= pc87360.o
 obj-$(CONFIG_SENSORS_PC87427)	+= pc87427.o
drivers/hwmon/nct7904.c new file +607 -0

607 lines not on this page · raw message

Signature · 9 lines
1.9.3




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

Hi Guenter,
Thank you for the comments.

Please find the new version of the patch and my comments below.

Thanks.
Vadim.

Quoted 10 lines · Guenter Roeck

On Tue, Feb 24, 2015 at 07:09:48PM +0300, Vadim V. Vlasov wrote:
...

drivers/hwmon/nct7904.c line 100

Quoted 4 lines
drivers/hwmon/nct7904.c new file +4 -0
+/* Access functions */
+/* Read 1-byte register. Returns unsigned reg or -ERRNO on error. */
+static int nct7904_read_reg(struct nct7904_data *data,
+	unsigned bank, unsigned reg)

In practice you never access a register without its page. Consider merging <bank, register> into a 16-bit value and decode it here.

Quoted 1 line

I don't think it is a good idea.

Your call.

Quoted 7 lines
Quoted 4 lines

This way a register define would automatically include the page, which in turn would reduce the risk of accicdentially accessing a register from the wrong page.

You wrote that I'm supposed to trust my code :)

Different scope. Besides, the above isn't caught by your checks. Separating page from register makes the checks even less useful.

Quoted 13 lines
Quoted 13 lines

Review

Quoted 7 lines
(quoted diff) +6 -0
+static ssize_t
+store_mode(struct device *dev,
+	  struct device_attribute *devattr, const char *buf, size_t count)
+{
[...]
+	ret = nct7904_write_reg(data, BANK_3, FANCTL1_FMR_REG + index, val);
+

This is really combining two configurations into one, the fan control mode (manual or automatic), and its association with a temperature source as selected in bank 3 registers 0x09..0x0c. If you want to be able to configure that, it will need to be two separate attributes, one for the mode and one for the temperature source.

Quoted 8 lines
Quoted 2 lines

Configuring automatic mode without also providing the means to configure registers 0x09..0x0c doesn't really make much sense.

No, I don't want to be able to configure automatic mode. I presume that BIOS would do that better. The only thing I want to provide is the ability to turn a fan into manual mode, play with RPM and return it back to whatever automatic settings were provided by BIOS.

I seem to be missing something. The mode attribute as you wrote it does configure manual vs. automatic mode, and in your code you also associate the automatic mode with a temperature source. Sorry, I don't see how it would make sense to let the user configure which temperature source to use without making sure that this temperature source points to something useful.

I don't see what you changed in the patch that follows. Please provide a change log. I still see

total: 0 errors, 1 warnings, 27 checks, 630 lines checked

when running the patch through checkpatch, so for sure you did not fix those. Please follow coding style guidelines in respect to continuation lines, and please don't use more than one empty line.

Please do not send new patch revisions as response to a previous patch revision. That just makes it difficult to track.

Thanks,
Guenter

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

#4 lore raw
In reply to Vadim V. Vlasov
Quoted 24 lines · Guenter Roeck
Quoted 13 lines · Vadim V. Vlasov
Quoted 13 lines

Review

Quoted 7 lines
(quoted diff) +6 -0
+static ssize_t
+store_mode(struct device *dev,
+	  struct device_attribute *devattr, const char *buf, size_t count)
+{
[...]
+	ret = nct7904_write_reg(data, BANK_3, FANCTL1_FMR_REG + index, val);
+

This is really combining two configurations into one, the fan control mode (manual or automatic), and its association with a temperature source as selected in bank 3 registers 0x09..0x0c. If you want to be able to configure that, it will need to be two separate attributes, one for the mode and one for the temperature source.

Quoted 8 lines
Quoted 2 lines

Configuring automatic mode without also providing the means to configure registers 0x09..0x0c doesn't really make much sense.

No, I don't want to be able to configure automatic mode. I presume that BIOS would do that better. The only thing I want to provide is the ability to turn a fan into manual mode, play with RPM and return it back to whatever automatic settings were provided by BIOS.

Quoted 6 lines

I seem to be missing something. The mode attribute as you wrote it does configure manual vs. automatic mode, and in your code you also associate the automatic mode with a temperature source. Sorry, I don't see how it would make sense to let the user configure which temperature source to use without making sure that this temperature source points to something useful.

I see the following use scenario:
- the user reads fanX_mode which specifies temperature source for automatic mode, and remembers it;
- the user writes 0 into the mode to set manual control;
- manually increases the fan speed (or decreases it to make the fan silent);
- after some time he restores automatic mode to what he has remembered.

Tweaking the values in smartfan configuration is not supposed and is not supported, yet.

Do you think that saving initial mode values and allowing only to set either 0 or the saved value is a better solution?

Quoted 2 lines

I don't see what you changed in the patch that follows. Please provide a change log.

1. hwmon_dev removed from nct7904_data
2. 4 groups of sensors are registered instead of just one, is_visible() with appropriate masks is used for configuring 3 of the groups
3. bank_lock/release() are separate functions
4. redundant checks of bank number removed
5. nct7904_read_reg16() returns two registers in big-endian format
6. sensor attributes are initialized using common macros (SENSOR_DEVICE_ATTR())
7. local temperature reading is a separate function rather than a special case in voltage reading
8. fixed multi-line function declaration style
9. other minor changes that you suggested.

Quoted 3 lines

I still see

total: 0 errors, 1 warnings, 27 checks, 630 lines checked

OK. The warning is:
WARNING: added, moved or deleted file(s), does MAINTAINERS need
updating?
I'm not sure what to do about it.

The checks:

CHECK: Please don't use multiple blank lines (7 instances)
Two blank lines separate 'logical sections' of the code. I can fix it if this is considered undesirable.

CHECK: Alignment should match open parenthesis (19 instances)
The 'CodingStyle' does not require such alignment.
You suggested to make multi-line declarations just consistent and I did so.

CHECK: struct mutex definition without comment
No idea about this one.

Thanks.
Vadim.

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

maintainer
#5 lore raw
In reply to Vadim V. Vlasov
Quoted 40 lines · Vadim V. Vlasov
Quoted 24 lines · Guenter Roeck
Quoted 13 lines · Vadim V. Vlasov
Quoted 13 lines

Review

Quoted 7 lines
(quoted diff) +6 -0
+static ssize_t
+store_mode(struct device *dev,
+	  struct device_attribute *devattr, const char *buf, size_t count)
+{
[...]
+	ret = nct7904_write_reg(data, BANK_3, FANCTL1_FMR_REG + index, val);
+

This is really combining two configurations into one, the fan control mode (manual or automatic), and its association with a temperature source as selected in bank 3 registers 0x09..0x0c. If you want to be able to configure that, it will need to be two separate attributes, one for the mode and one for the temperature source.

Quoted 8 lines
Quoted 2 lines

Configuring automatic mode without also providing the means to configure registers 0x09..0x0c doesn't really make much sense.

No, I don't want to be able to configure automatic mode. I presume that BIOS would do that better. The only thing I want to provide is the ability to turn a fan into manual mode, play with RPM and return it back to whatever automatic settings were provided by BIOS.

Quoted 6 lines

I seem to be missing something. The mode attribute as you wrote it does configure manual vs. automatic mode, and in your code you also associate the automatic mode with a temperature source. Sorry, I don't see how it would make sense to let the user configure which temperature source to use without making sure that this temperature source points to something useful.

I see the following use scenario:
- the user reads fanX_mode which specifies temperature source for automatic mode, and remembers it;
- the user writes 0 into the mode to set manual control;
- manually increases the fan speed (or decreases it to make the fan silent);
- after some time he restores automatic mode to what he has remembered.

Quoted 6 lines

Tweaking the values in smartfan configuration is not supposed and is not supported, yet.

Do you think that saving initial mode values and allowing only to set either 0 or the saved value is a better solution?

Yes; you can not make assumptions like that. Since you don't let the user configure the temperature source itself, you should also reject setting automatic mode if it was not originally enabled.

Quoted 17 lines
Quoted 2 lines

I don't see what you changed in the patch that follows. Please provide a change log.

1. hwmon_dev removed from nct7904_data
2. 4 groups of sensors are registered instead of just one, is_visible() with appropriate masks is used for configuring 3 of the groups
3. bank_lock/release() are separate functions
4. redundant checks of bank number removed
5. nct7904_read_reg16() returns two registers in big-endian format
6. sensor attributes are initialized using common macros (SENSOR_DEVICE_ATTR())
7. local temperature reading is a separate function rather than a special case in voltage reading
8. fixed multi-line function declaration style
9. other minor changes that you suggested.

Thanks a lot for the information. Please add to changelog for the next version of the patch (after the '---' line).

Quoted 9 lines
Quoted 3 lines

I still see

total: 0 errors, 1 warnings, 27 checks, 630 lines checked

OK. The warning is:
WARNING: added, moved or deleted file(s), does MAINTAINERS need
updating?
I'm not sure what to do about it.

Your call. Ignore it, or if you want to spend the time add yourself as maintainer for this driver.

Quoted 6 lines

The checks:

CHECK: Please don't use multiple blank lines (7 instances)
Two blank lines separate 'logical sections' of the code. I can fix it if this is considered undesirable.

Yes, it is considered undesirable.

Quoted 5 lines

CHECK: Alignment should match open parenthesis (19 instances)
The 'CodingStyle' does not require such alignment.
You suggested to make multi-line declarations just consistent and I did so.

Not in the patch that was attached; that is where majority of the messages comes from.

Quoted 3 lines

CHECK: struct mutex definition without comment
No idea about this one.

Ignore it; I consider this one to be noise.

Thanks,
Guenter

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