Skip to contentSkip to conversation

hwmon: max34409 driver added

PATCH In review andybeg 2 participants 2 messages Feb 15, 2023

Subsystems (2)
lore
Share & tools
lore

andybeg

author
#0 lore raw

PATCH 1/1 hwmon: max34409 driver added

Documentation/hwmon/max34409.rst new file +23 -0
new file mode 100644
index 000000000000..91779c6a9163
@@ -0,0 +1,23 @@
+Kernel driver max34409
+=====================
+
+Supported chips:
+  * Analog Devices MAX34409
+    Prefix: 'max34409'
+    Datasheet: https://www.analog.com/media/en/technical-documentation/data-sheets/MAX34408-MAX34409.pdf
+
+Author: Andrey Kononov <[email protected]>
+
+
+Description
+-----------
+
+This driver for SMBus Dual/Quad Current Monitor MaximIntegrated MAX34409
+
+
+Usage Notes
+-----------
+
+This driver does not auto-detect devices. You will have to instantiate the
+devices explicitly. Please see Documentation/i2c/instantiating-devices.rst
+for details.
drivers/hwmon/Kconfig +7 -0
index 3176c33af6c6..de412f7dcad8 100644
@@ -1088,6 +1088,13 @@ config SENSORS_MAX31760
 	  This driver can also be built as a module. If so, the module
 	  will be called max31760.
 
+config SENSORS_MAX3440X
+	tristate "Maxim max3440x SMBus Dual/Quad Current Monitor"
+	depends on I2C
+	help
+	  Say yes here to build support for Maxim family of SMBus Dual/Quad Current Monitors.
+	  This driver is mutually exclusive with the HWMON version.
+
 config SENSORS_MAX6620
 	tristate "Maxim MAX6620 fan controller"
 	depends on I2C
drivers/hwmon/Makefile +1 -0
index e2e4e87b282f..a4e24d2b03c1 100644
@@ -141,6 +141,7 @@ obj-$(CONFIG_SENSORS_MAX197)	+= max197.o
 obj-$(CONFIG_SENSORS_MAX31722)	+= max31722.o
 obj-$(CONFIG_SENSORS_MAX31730)	+= max31730.o
 obj-$(CONFIG_SENSORS_MAX31760)  += max31760.o
+obj-$(CONFIG_SENSORS_MAX3440X)  += max3440x.o
 obj-$(CONFIG_SENSORS_MAX6620)	+= max6620.o
 obj-$(CONFIG_SENSORS_MAX6621)	+= max6621.o
 obj-$(CONFIG_SENSORS_MAX6639)	+= max6639.o
drivers/hwmon/max3440x.c new file +213 -0

213 lines not on this page · raw message

+MODULE_LICENSE("GPL");

Signature · 1 line
2.34.1
maintainer
#1 lore raw

On Wed, Feb 15, 2023 at 08:26:12PM +0300, andybeg wrote:

Subject should be something like: "Add driver for MAX34409"

checkpatch reports:

total: 0 errors, 8 warnings, 12 checks, 256 lines checked

Please fix.

Quoted 34 lines
Documentation/hwmon/max34409.rst new file +23 -0
new file mode 100644
index 000000000000..91779c6a9163
@@ -0,0 +1,23 @@
+Kernel driver max34409
+=====================
+
+Supported chips:
+  * Analog Devices MAX34409
+    Prefix: 'max34409'
+    Datasheet: https://www.analog.com/media/en/technical-documentation/data-sheets/MAX34408-MAX34409.pdf
+
+Author: Andrey Kononov <[email protected]>
+
+
+Description
+-----------
+
+This driver for SMBus Dual/Quad Current Monitor MaximIntegrated MAX34409
+
+
+Usage Notes
+-----------
+
+This driver does not auto-detect devices. You will have to instantiate the
+devices explicitly. Please see Documentation/i2c/instantiating-devices.rst
+for details.
drivers/hwmon/Kconfig +0 -0
index 3176c33af6c6..de412f7dcad8 100644

drivers/hwmon/Kconfig config SENSORS_MAX31760 · line 1091

Quoted 4 lines
drivers/hwmon/Kconfig +1 -0
@@ -1088,6 +1088,13 @@ config SENSORS_MAX31760
 	  This driver can also be built as a module. If so, the module
 	  will be called max31760.
 
+config SENSORS_MAX3440X

X -> 9

This driver for sure does not and never will cover all of MAX3440[0-9].

Same for the code itself.

Quoted 27 lines
drivers/hwmon/Kconfig +6 -0
+	tristate "Maxim max3440x SMBus Dual/Quad Current Monitor"
+	depends on I2C
+	help
+	  Say yes here to build support for Maxim family of SMBus Dual/Quad Current Monitors.
+	  This driver is mutually exclusive with the HWMON version.
+
 config SENSORS_MAX6620
 	tristate "Maxim MAX6620 fan controller"
 	depends on I2C
drivers/hwmon/Makefile +1 -0
index e2e4e87b282f..a4e24d2b03c1 100644
@@ -141,6 +141,7 @@ obj-$(CONFIG_SENSORS_MAX197)	+= max197.o
 obj-$(CONFIG_SENSORS_MAX31722)	+= max31722.o
 obj-$(CONFIG_SENSORS_MAX31730)	+= max31730.o
 obj-$(CONFIG_SENSORS_MAX31760)  += max31760.o
+obj-$(CONFIG_SENSORS_MAX3440X)  += max3440x.o
 obj-$(CONFIG_SENSORS_MAX6620)	+= max6620.o
 obj-$(CONFIG_SENSORS_MAX6621)	+= max6621.o
 obj-$(CONFIG_SENSORS_MAX6639)	+= max6639.o
drivers/hwmon/max3440x.c new file +0 -0
new file mode 100644
index 000000000000..b62c34f9425c

drivers/hwmon/max3440x.c line 4

Quoted 4 lines
drivers/hwmon/max3440x.c new file +4 -0
@@ -0,0 +1,213 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+*
+*/

Completely useless comment.

drivers/hwmon/max3440x.c line 15

Quoted 11 lines
drivers/hwmon/max3440x.c new file +11 -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>
+#include <linux/mutex.h>
+#include <linux/sysfs.h>

Alphabetic include file order, please.

Quoted 11 lines
drivers/hwmon/max3440x.c new file +11 -0
+
+/*
+ * Registers description.
+ */
+#define MAX3440X_STATUS             0x00
+#define MAX3440X_CONTROL            0x01
+#define MAX3440X_OCDELAY            0x02
+#define MAX3440X_SDDELAY            0x03
+#define MAX3440X_ADC1               0x04	/* readonly */
+#define MAX3440X_ADC2               0x05	/* readonly */
+#define MAX3440X_ADC3               0x06	/* readonly */

drivers/hwmon/max3440x.c line 36

Quoted 10 lines
drivers/hwmon/max3440x.c new file +10 -0
+#define MAX3440X_ADC4               0x07	/* readonly */
+#define MAX3440X_OCT1               0x08
+#define MAX3440X_OCT2               0x09
+#define MAX3440X_OCT3               0x0A
+#define MAX3440X_OCT4               0x0B
+#define MAX3440X_DID                0x0C	/* readonly */
+#define MAX3440X_DCYY               0x0D	/* readonly */
+#define MAX3440X_DCWW               0x0E    /* readonly */
+
+//maximal current in mA throw RSENSE, that can be measured. see datasheet table 18

No C++ comments, please.

drivers/hwmon/max3440x.c line 40

Quoted 4 lines
drivers/hwmon/max3440x.c new file +4 -0
+static unsigned short imax[4];
+module_param_array(imax, short, NULL, 0);
+MODULE_PARM_DESC(imax,
+		 "maximal current in mA throw RSENSE, that can be measured. see datasheet table 18");

This is not an appropriate way to set limits or to provide scaling. The driver should assume a default for Rsense (such as 1 mOhm), and possibly provide the ability (via devicetree properties) to configure actual rsense values. Anything else should be done using sensors3.conf.

drivers/hwmon/max3440x.c line 44

Quoted 4 lines
drivers/hwmon/max3440x.c new file +4 -0
+struct max3440x_data {
+	struct i2c_client *client;
+	struct device *hwmon_dev;
+	const char *name;

'name' and 'hwmon_dev' are unused.

drivers/hwmon/max3440x.c line 48

Quoted 4 lines
drivers/hwmon/max3440x.c new file +4 -0
+
+   struct mutex update_lock;
+   bool valid;
+

valid is never read and pointless.

Obviously some formatting problems. See checkpatch output.

drivers/hwmon/max3440x.c line 50

Quoted 2 lines
drivers/hwmon/max3440x.c new file +2 -0
+	u16 adc[4];
+	u8 oct[4];

'oct' is unused. I assume this is supposed to be for "Over Current Threshold", which should be implemented as curr[1-4]_max sysfs attributes.

drivers/hwmon/max3440x.c line 57

Quoted 7 lines
drivers/hwmon/max3440x.c new file +7 -0
+};
+
+static const char * const input_names[] = {
+	[MAX3440X_ADC1]	=	"curr1",
+	[MAX3440X_ADC2]	=	"curr2",
+	[MAX3440X_ADC3]	=	"curr3",
+	[MAX3440X_ADC4]	=	"curr4",

Those names have no value. It is obvious that curr1_input is curr1.

drivers/hwmon/max3440x.c line 64

Quoted 7 lines
drivers/hwmon/max3440x.c new file +7 -0
+};
+
+static void max3440x_init_client(struct max3440x_data *data,
+				struct i2c_client *client)
+{
+	u8 status;
+   u16 val = 0;

Pointless initialization

drivers/hwmon/max3440x.c line 67

Quoted 3 lines
drivers/hwmon/max3440x.c new file +3 -0
+	/*
+	 * Start the conversions.
+	 */

There is nothing in the datasheet suggesting that it would be necessary to read the status register to start conversions.

drivers/hwmon/max3440x.c line 68

Quoted 1 line
drivers/hwmon/max3440x.c new file +1 -0
+	status = i2c_smbus_read_byte_data(client, MAX3440X_STATUS);

Please do not ignore errors. Anyway, "status" is neither used nor saved, and reading it is thus just pointless.

drivers/hwmon/max3440x.c line 77

Quoted 9 lines
drivers/hwmon/max3440x.c new file +9 -0
+
+val = (u16)i2c_smbus_read_byte_data(client, MAX3440X_ADC1);
+	data->adc[0] = DIV_ROUND_CLOSEST((imax[0] * val), 256);
+	val = i2c_smbus_read_byte_data(client, MAX3440X_ADC2);
+	data->adc[1] = DIV_ROUND_CLOSEST((imax[1] * val), 256);
+	val = i2c_smbus_read_byte_data(client, MAX3440X_ADC3);
+	data->adc[2] = DIV_ROUND_CLOSEST((imax[2] * val), 256);
+	val = i2c_smbus_read_byte_data(client, MAX3440X_ADC4);
+	data->adc[3] = DIV_ROUND_CLOSEST((imax[3] * val), 256);

This function for the most part duplicates max3440x_update_device() and does not add any value.

drivers/hwmon/max3440x.c line 90

Quoted 13 lines
drivers/hwmon/max3440x.c new file +13 -0
+}
+
+static struct max3440x_data *max3440x_update_device(struct device *dev)
+{
+	struct max3440x_data *data = dev_get_drvdata(dev);
+	struct i2c_client *client = data->client;
+	u16 val;
+
+	mutex_lock(&data->update_lock);
+
+	dev_dbg(dev, "Updating max3440 data.\n");
+	val = (u16)i2c_smbus_read_byte_data(client,
+				MAX3440X_ADC1);

i2c_smbus_read_byte_data() returns a negative error code. Please do not ignore.

Quoted 11 lines
drivers/hwmon/max3440x.c new file +11 -0
+	data->adc[0] = DIV_ROUND_CLOSEST((imax[0] * val), 256);
+	val =  (u16)i2c_smbus_read_byte_data(client,
+				MAX3440X_ADC2);
+	data->adc[1] = DIV_ROUND_CLOSEST((imax[1] * val), 256);
+	val = (u16)i2c_smbus_read_byte_data(client,
+				MAX3440X_ADC3);
+	data->adc[2] = DIV_ROUND_CLOSEST((imax[2] * val), 256);
+	val = (u16)i2c_smbus_read_byte_data(client,
+				MAX3440X_ADC4);
+	data->adc[3] = DIV_ROUND_CLOSEST((imax[3] * val), 256);
+

drivers/hwmon/max3440x.c line 111

Quoted 10 lines
drivers/hwmon/max3440x.c new file +10 -0
+	data->valid = 1;
+	mutex_unlock(&data->update_lock);
+
+	return data;
+}
+static ssize_t adc1_show(struct device *dev,
+			     struct device_attribute *attr, char *buf)
+{
+	struct sensor_device_attribute *attr2 = to_sensor_dev_attr(attr);
+	struct max3440x_data *data = max3440x_update_device(dev);

Reading all sensors to report one is a waste of i2c bandwidth. Please drop max3440x_update_device() entirely and only read the data for the sensor which is reported.

Quoted 33 lines
drivers/hwmon/max3440x.c new file +33 -0
+
+	return sprintf(buf, "%d\n", data->adc[0]);
+}
+static ssize_t adc2_show(struct device *dev,
+			     struct device_attribute *attr, char *buf)
+{
+	struct sensor_device_attribute *attr2 = to_sensor_dev_attr(attr);
+	struct max3440x_data *data = max3440x_update_device(dev);
+
+	return sprintf(buf, "%d\n", data->adc[1]);
+}
+static ssize_t adc3_show(struct device *dev,
+			     struct device_attribute *attr, char *buf)
+{
+	struct sensor_device_attribute *attr2 = to_sensor_dev_attr(attr);
+	struct max3440x_data *data = max3440x_update_device(dev);
+
+	return sprintf(buf, "%d\n", data->adc[2]);
+}
+static ssize_t adc4_show(struct device *dev,
+			     struct device_attribute *attr, char *buf)
+{
+	struct sensor_device_attribute *attr2 = to_sensor_dev_attr(attr);
+	struct max3440x_data *data = max3440x_update_device(dev);
+
+	return sprintf(buf, "%d\n", data->adc[3]);
+}
+
+static ssize_t label_show(struct device *dev,
+			  struct device_attribute *devattr, char *buf)
+{
+	return sprintf(buf, "%s\n",
+		       input_names[to_sensor_dev_attr(devattr)->index]);

drivers/hwmon/max3440x.c line 154

Quoted 10 lines
drivers/hwmon/max3440x.c new file +10 -0
+}
+
+static SENSOR_DEVICE_ATTR_RO(curr1_input, adc1, 0);
+static SENSOR_DEVICE_ATTR_RO(curr1_label, label, MAX3440X_ADC1);
+static SENSOR_DEVICE_ATTR_RO(curr2_input, adc2, 0);
+static SENSOR_DEVICE_ATTR_RO(curr2_label, label, MAX3440X_ADC2);
+static SENSOR_DEVICE_ATTR_RO(curr3_input, adc3, 0);
+static SENSOR_DEVICE_ATTR_RO(curr3_label, label, MAX3440X_ADC3);
+static SENSOR_DEVICE_ATTR_RO(curr4_input, adc4, 0);
+static SENSOR_DEVICE_ATTR_RO(curr4_label, label, MAX3440X_ADC4);

Not that it matters because you should use the _with_info API, but the last parameter of SENSOR_DEVICE_ATTR_{RO,RW} is supposed to be the index, which is supposed to be used by the called function to determine which of the sensors should be accessed. This means that separate functions are not necessary.

Quoted 25 lines
drivers/hwmon/max3440x.c new file +25 -0
+
+static struct attribute *max3440x_attrs[] = {
+	&sensor_dev_attr_curr1_input.dev_attr.attr,
+	&sensor_dev_attr_curr1_label.dev_attr.attr,
+	&sensor_dev_attr_curr2_input.dev_attr.attr,
+	&sensor_dev_attr_curr2_label.dev_attr.attr,
+	&sensor_dev_attr_curr3_input.dev_attr.attr,
+	&sensor_dev_attr_curr3_label.dev_attr.attr,
+	&sensor_dev_attr_curr4_input.dev_attr.attr,
+	&sensor_dev_attr_curr4_label.dev_attr.attr,
+	NULL
+};
+
+ATTRIBUTE_GROUPS(max3440x);
+
+static int max3440x_probe(struct i2c_client *client,
+			 const struct i2c_device_id *id)
+{
+	struct device *dev = &client->dev;
+	struct max3440x_data *data;
+	struct device *hwmon_dev;
+
+	data = devm_kzalloc(dev, sizeof(struct max3440x_data), GFP_KERNEL);
+	if (!data)
+		return -ENOMEM;

drivers/hwmon/max3440x.c line 189

Quoted 10 lines
drivers/hwmon/max3440x.c new file +10 -0
+
+	data->client = client;
+	mutex_init(&data->update_lock);
+
+	/* Initialize the MAX3440x chip */
+	max3440x_init_client(data, client);
+
+	hwmon_dev = devm_hwmon_device_register_with_groups(&client->dev,
+							   client->name, data,
+							   max3440x_groups);

Please rewrite to use devm_hwmon_device_register_with_info().

Quoted 14 lines
drivers/hwmon/max3440x.c new file +14 -0
+	return PTR_ERR_OR_ZERO(hwmon_dev);
+}
+
+
+static const struct i2c_device_id max3440x_id[] = {
+	{ "max34409", 0 },
+	{ }
+};
+
+MODULE_DEVICE_TABLE(i2c, max3440x_id);
+
+static const struct i2c_driver max3440x_driver = {
+	.class = I2C_CLASS_HWMON,
+	.driver = {

drivers/hwmon/max3440x.c line 213

Quoted 10 lines
drivers/hwmon/max3440x.c new file +10 -0
+		.name = "max3440x",
+	},
+	.probe = max3440x_probe,
+	.id_table	= max3440x_id,
+};
+
+module_i2c_driver(max3440x_driver);
+
+MODULE_AUTHOR("Andrey Kononov");
+MODULE_DESCRIPTION("I2C adc driver");

There is an emphasis on adc, not on hardware monitoring. The limit registers / attributes are not implemented, and neither is status reporting, making its value as hwmon driver quite limited. Are you sure you want/need a hardware monitoring driver ? If this is just used as an ADC and not as current monitor it may be better to implement it as an IIO driver.

Guenter

Quoted 3 lines

+MODULE_LICENSE("GPL");

Signature · 1 line
2.34.1