All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v3 0/9] hwmon: New hwmon registration API
@ 2016-07-25  3:32 Guenter Roeck
  2016-07-25  3:32 ` [PATCH v3 1/9] hwmon: (core) Order include files alphabetically Guenter Roeck
                   ` (8 more replies)
  0 siblings, 9 replies; 26+ messages in thread
From: Guenter Roeck @ 2016-07-25  3:32 UTC (permalink / raw)
  To: Jean Delvare
  Cc: Jonathan Cameron, Zhang Rui, Eduardo Valentin, Punit Agrawal,
	linux-pm, linux-iio, linux-hwmon, linux-kernel, Guenter Roeck

Up to now, each hwmon driver has to implement its own sysfs attributes.
This requires a lot of template code, and distracts from the driver's
core function to read and write chip registers.

To be able to reduce driver complexity, move sensor attribute handling
and thermal zone registration into the hwmon core. By using the new API,
driver size is typically reduced by 20-50% depending on driver complexity
and the number of sysfs attributes supported.

The first patch of the series is preparatory; it reorders include files
in the hwmon core to make it easier to add or remove individual include
files.

The second patch of the series introduces the API as well as support
for temperature sensor attributes. Subsequent patches introduce support
for voltage, current, power, energy, humidity, fan speed, and basic pwm
attributes.

The series was tested by converting several drivers (lm75, lm90, tmp102,
tmp421, ltc4245, nct7904, max31790) to the new API. Testing was done with
real chips as well as with the hwmon driver module test code available
at https://github.com/groeck/module-tests.

v3:
- Thermal registration depends on IS_REACHABLE(CONFIG_THERMAL) and
  CONFIG_THERMAL_OF.
v2:
- Add patch 1/9 (order include files alphabetically).
- Add patch 9/9 (pwm support).
- Document callback function parameters of struct hwmon_ops in
  include/linux/hwmon.h.
- Clarify that the is_visible() callback is mandatory.
- If an attribute has no template string, treat it as invisible, not as
  error. Affected are virtual attributes such as HWMON_C_REGISTER_TZ.
- Initialize device attribute read/write callback functions unconditionally.
- Cosmetic changes, including typo fixes and added newlines for readability.

^ permalink raw reply	[flat|nested] 26+ messages in thread

* [PATCH v3 1/9] hwmon: (core) Order include files alphabetically
  2016-07-25  3:32 [PATCH v3 0/9] hwmon: New hwmon registration API Guenter Roeck
@ 2016-07-25  3:32 ` Guenter Roeck
  2016-09-27 21:13   ` Jean Delvare
  2016-07-25  3:32 ` [PATCH v3 2/9] hwmon: (core) New hwmon registration API Guenter Roeck
                   ` (7 subsequent siblings)
  8 siblings, 1 reply; 26+ messages in thread
From: Guenter Roeck @ 2016-07-25  3:32 UTC (permalink / raw)
  To: Jean Delvare
  Cc: Jonathan Cameron, Zhang Rui, Eduardo Valentin, Punit Agrawal,
	linux-pm, linux-iio, linux-hwmon, linux-kernel, Guenter Roeck

Ordering include files alphabetically makes it easier to add new ones.
Stop including linux/spinlock.h and linux/kdev_t.h since both are not
needed.

Reviewed-by: Jonathan Cameron <jic23@kernel.org>
Signed-off-by: Guenter Roeck <linux@roeck-us.net>
---
v3: No change
v2: Added patch

 drivers/hwmon/hwmon.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)

diff --git a/drivers/hwmon/hwmon.c b/drivers/hwmon/hwmon.c
index a26c385a435b..649a68d119b4 100644
--- a/drivers/hwmon/hwmon.c
+++ b/drivers/hwmon/hwmon.c
@@ -12,16 +12,14 @@
 
 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
 
-#include <linux/module.h>
 #include <linux/device.h>
 #include <linux/err.h>
-#include <linux/slab.h>
-#include <linux/kdev_t.h>
-#include <linux/idr.h>
-#include <linux/hwmon.h>
 #include <linux/gfp.h>
-#include <linux/spinlock.h>
+#include <linux/hwmon.h>
+#include <linux/idr.h>
+#include <linux/module.h>
 #include <linux/pci.h>
+#include <linux/slab.h>
 #include <linux/string.h>
 
 #define HWMON_ID_PREFIX "hwmon"
-- 
2.5.0

^ permalink raw reply related	[flat|nested] 26+ messages in thread

* [PATCH v3 2/9] hwmon: (core) New hwmon registration API
  2016-07-25  3:32 [PATCH v3 0/9] hwmon: New hwmon registration API Guenter Roeck
  2016-07-25  3:32 ` [PATCH v3 1/9] hwmon: (core) Order include files alphabetically Guenter Roeck
@ 2016-07-25  3:32 ` Guenter Roeck
  2016-08-12  8:16     ` Keerthy
  2016-10-04  8:45     ` Jean Delvare
  2016-07-25  3:32 ` [PATCH v3 3/9] hwmon: (core) Add voltage attribute support to new API Guenter Roeck
                   ` (6 subsequent siblings)
  8 siblings, 2 replies; 26+ messages in thread
From: Guenter Roeck @ 2016-07-25  3:32 UTC (permalink / raw)
  To: Jean Delvare
  Cc: Jonathan Cameron, Zhang Rui, Eduardo Valentin, Punit Agrawal,
	linux-pm, linux-iio, linux-hwmon, linux-kernel, Guenter Roeck

Up to now, each hwmon driver has to implement its own sysfs attributes.
This requires a lot of template code, and distracts from the driver's core
function to read and write chip registers.

To be able to reduce driver complexity, move sensor attribute handling
and thermal zone registration into hwmon core. By using the new API,
driver code and data size is typically reduced by 20-70%, depending
on driver complexity and the number of sysfs attributes supported.

With this patch, the new API only supports thermal sensors. Support for
other sensor types will be added with subsequent patches.

Acked-by: Punit Agrawal <punit.agrawal@arm.com>
Reviewed-by: Jonathan Cameron <jic23@kernel.org>
Signed-off-by: Guenter Roeck <linux@roeck-us.net>
---
v3:
- Thermal registration depends on IS_REACHABLE(CONFIG_THERMAL) and
  CONFIG_THERMAL_OF.
v2:
- Document callback function parameters of struct hwmon_ops in
  include/linux/hwmon.h.
- Clarify that the is_visible() callback is mandatory.
- Initialize device attribute read/write callback functions unconditionally.
- If an attribute has no template string, treat it as invisible, not as
  error. Affected are virtual attributes such as HWMON_C_REGISTER_TZ.
- Added newline to improve readability.

Review comments not addressed:
- Stick with u32 for attribute masks. We could use u64, but it is currently
  not needed, and changing it later would be straightforward.
- Do not use for_each_set_bit() to walk attribute masks.
  for_each_set_bit() expects a pointer to an unsigned long as argument,
  which would make it difficult to switch to u64 attribute masks if/when
  needed.

 drivers/hwmon/hwmon.c | 485 +++++++++++++++++++++++++++++++++++++++++++++++---
 include/linux/hwmon.h | 148 +++++++++++++++
 2 files changed, 606 insertions(+), 27 deletions(-)

diff --git a/drivers/hwmon/hwmon.c b/drivers/hwmon/hwmon.c
index 649a68d119b4..3e4cc442a089 100644
--- a/drivers/hwmon/hwmon.c
+++ b/drivers/hwmon/hwmon.c
@@ -12,6 +12,7 @@
 
 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
 
+#include <linux/bitops.h>
 #include <linux/device.h>
 #include <linux/err.h>
 #include <linux/gfp.h>
@@ -21,6 +22,7 @@
 #include <linux/pci.h>
 #include <linux/slab.h>
 #include <linux/string.h>
+#include <linux/thermal.h>
 
 #define HWMON_ID_PREFIX "hwmon"
 #define HWMON_ID_FORMAT HWMON_ID_PREFIX "%d"
@@ -28,9 +30,35 @@
 struct hwmon_device {
 	const char *name;
 	struct device dev;
+	const struct hwmon_chip_info *chip;
+
+	struct attribute_group group;
+	const struct attribute_group **groups;
 };
+
 #define to_hwmon_device(d) container_of(d, struct hwmon_device, dev)
 
+struct hwmon_device_attribute {
+	struct device_attribute dev_attr;
+	const struct hwmon_ops *ops;
+	enum hwmon_sensor_types type;
+	u32 attr;
+	int index;
+};
+
+#define to_hwmon_attr(d) \
+	container_of(d, struct hwmon_device_attribute, dev_attr)
+
+/*
+ * Thermal zone information
+ * In addition to the reference to the hwmon device,
+ * also provides the sensor index.
+ */
+struct hwmon_thermal_data {
+	struct hwmon_device *hwdev;	/* Reference to hwmon device */
+	int index;			/* sensor index */
+};
+
 static ssize_t
 show_name(struct device *dev, struct device_attribute *attr, char *buf)
 {
@@ -78,25 +106,286 @@ static struct class hwmon_class = {
 
 static DEFINE_IDA(hwmon_ida);
 
-/**
- * hwmon_device_register_with_groups - register w/ hwmon
- * @dev: the parent device
- * @name: hwmon name attribute
- * @drvdata: driver data to attach to created device
- * @groups: List of attribute groups to create
- *
- * hwmon_device_unregister() must be called when the device is no
- * longer needed.
- *
- * Returns the pointer to the new device.
- */
-struct device *
-hwmon_device_register_with_groups(struct device *dev, const char *name,
-				  void *drvdata,
-				  const struct attribute_group **groups)
+/* Thermal zone handling */
+
+#if IS_REACHABLE(CONFIG_THERMAL) && defined(CONFIG_THERMAL_OF)
+static int hwmon_thermal_get_temp(void *data, int *temp)
+{
+	struct hwmon_thermal_data *tdata = data;
+	struct hwmon_device *hwdev = tdata->hwdev;
+	int ret;
+	long t;
+
+	ret = hwdev->chip->ops->read(&hwdev->dev, hwmon_temp, hwmon_temp_input,
+				     tdata->index, &t);
+	if (ret < 0)
+		return ret;
+
+	*temp = t;
+
+	return 0;
+}
+
+static struct thermal_zone_of_device_ops hwmon_thermal_ops = {
+	.get_temp = hwmon_thermal_get_temp,
+};
+
+static int hwmon_thermal_add_sensor(struct device *dev,
+				    struct hwmon_device *hwdev, int index)
+{
+	struct hwmon_thermal_data *tdata;
+
+	tdata = devm_kzalloc(dev, sizeof(*tdata), GFP_KERNEL);
+	if (!tdata)
+		return -ENOMEM;
+
+	tdata->hwdev = hwdev;
+	tdata->index = index;
+
+	devm_thermal_zone_of_sensor_register(&hwdev->dev, index, tdata,
+					     &hwmon_thermal_ops);
+
+	return 0;
+}
+#else
+static int hwmon_thermal_add_sensor(struct device *dev,
+				    struct hwmon_device *hwdev, int index)
+{
+	return 0;
+}
+#endif /* IS_REACHABLE(CONFIG_THERMAL) && defined(CONFIG_THERMAL_OF) */
+
+/* sysfs attribute management */
+
+static ssize_t hwmon_attr_show(struct device *dev,
+			       struct device_attribute *devattr, char *buf)
+{
+	struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
+	long val;
+	int ret;
+
+	ret = hattr->ops->read(dev, hattr->type, hattr->attr, hattr->index,
+			       &val);
+	if (ret < 0)
+		return ret;
+
+	return sprintf(buf, "%ld\n", val);
+}
+
+static ssize_t hwmon_attr_store(struct device *dev,
+				struct device_attribute *devattr,
+				const char *buf, size_t count)
+{
+	struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
+	long val;
+	int ret;
+
+	ret = kstrtol(buf, 10, &val);
+	if (ret < 0)
+		return ret;
+
+	ret = hattr->ops->write(dev, hattr->type, hattr->attr, hattr->index,
+				val);
+	if (ret < 0)
+		return ret;
+
+	return count;
+}
+
+static int hwmon_attr_base(enum hwmon_sensor_types type)
+{
+	if (type == hwmon_in)
+		return 0;
+	return 1;
+}
+
+static struct attribute *hwmon_genattr(struct device *dev,
+				       const void *drvdata,
+				       enum hwmon_sensor_types type,
+				       u32 attr,
+				       int index,
+				       const char *template,
+				       const struct hwmon_ops *ops)
+{
+	struct hwmon_device_attribute *hattr;
+	struct device_attribute *dattr;
+	struct attribute *a;
+	umode_t mode;
+	char *name;
+
+	/* The attribute is invisible if there is no template string */
+	if (!template)
+		return ERR_PTR(-ENOENT);
+
+	mode = ops->is_visible(drvdata, type, attr, index);
+	if (!mode)
+		return ERR_PTR(-ENOENT);
+
+	if ((mode & S_IRUGO) && !ops->read)
+		return ERR_PTR(-EINVAL);
+	if ((mode & S_IWUGO) && !ops->write)
+		return ERR_PTR(-EINVAL);
+
+	if (type == hwmon_chip) {
+		name = (char *)template;
+	} else {
+		name = devm_kzalloc(dev, strlen(template) + 16, GFP_KERNEL);
+		if (!name)
+			return ERR_PTR(-ENOMEM);
+		scnprintf(name, strlen(template) + 16, template,
+			  index + hwmon_attr_base(type));
+	}
+
+	hattr = devm_kzalloc(dev, sizeof(*hattr), GFP_KERNEL);
+	if (!hattr)
+		return ERR_PTR(-ENOMEM);
+
+	hattr->type = type;
+	hattr->attr = attr;
+	hattr->index = index;
+	hattr->ops = ops;
+
+	dattr = &hattr->dev_attr;
+	dattr->show = hwmon_attr_show;
+	dattr->store = hwmon_attr_store;
+
+	a = &dattr->attr;
+	sysfs_attr_init(a);
+	a->name = name;
+	a->mode = mode;
+
+	return a;
+}
+
+static const char * const hwmon_chip_attr_templates[] = {
+	[hwmon_chip_temp_reset_history] = "temp_reset_history",
+	[hwmon_chip_update_interval] = "update_interval",
+	[hwmon_chip_alarms] = "alarms",
+};
+
+static const char * const hwmon_temp_attr_templates[] = {
+	[hwmon_temp_input] = "temp%d_input",
+	[hwmon_temp_type] = "temp%d_type",
+	[hwmon_temp_lcrit] = "temp%d_lcrit",
+	[hwmon_temp_lcrit_hyst] = "temp%d_lcrit_hyst",
+	[hwmon_temp_min] = "temp%d_min",
+	[hwmon_temp_min_hyst] = "temp%d_min_hyst",
+	[hwmon_temp_max] = "temp%d_max",
+	[hwmon_temp_max_hyst] = "temp%d_max_hyst",
+	[hwmon_temp_crit] = "temp%d_crit",
+	[hwmon_temp_crit_hyst] = "temp%d_crit_hyst",
+	[hwmon_temp_emergency] = "temp%d_emergency",
+	[hwmon_temp_emergency_hyst] = "temp%d_emergency_hyst",
+	[hwmon_temp_alarm] = "temp%d_alarm",
+	[hwmon_temp_lcrit_alarm] = "temp%d_lcrit_alarm",
+	[hwmon_temp_min_alarm] = "temp%d_min_alarm",
+	[hwmon_temp_max_alarm] = "temp%d_max_alarm",
+	[hwmon_temp_crit_alarm] = "temp%d_crit_alarm",
+	[hwmon_temp_emergency_alarm] = "temp%d_emergency_alarm",
+	[hwmon_temp_fault] = "temp%d_fault",
+	[hwmon_temp_offset] = "temp%d_offset",
+	[hwmon_temp_label] = "temp%d_label",
+	[hwmon_temp_lowest] = "temp%d_lowest",
+	[hwmon_temp_highest] = "temp%d_highest",
+	[hwmon_temp_reset_history] = "temp%d_reset_history",
+};
+
+static const char * const *__templates[] = {
+	[hwmon_chip] = hwmon_chip_attr_templates,
+	[hwmon_temp] = hwmon_temp_attr_templates,
+};
+
+static const int __templates_size[] = {
+	[hwmon_chip] = ARRAY_SIZE(hwmon_chip_attr_templates),
+	[hwmon_temp] = ARRAY_SIZE(hwmon_temp_attr_templates),
+};
+
+static int hwmon_num_channel_attrs(const struct hwmon_channel_info *info)
+{
+	int i, n;
+
+	for (i = n = 0; info->config[i]; i++)
+		n += hweight32(info->config[i]);
+
+	return n;
+}
+
+static int hwmon_genattrs(struct device *dev,
+			  const void *drvdata,
+			  struct attribute **attrs,
+			  const struct hwmon_ops *ops,
+			  const struct hwmon_channel_info *info)
+{
+	const char * const *templates;
+	int template_size;
+	int i, aindex = 0;
+
+	if (info->type >= ARRAY_SIZE(__templates))
+		return -EINVAL;
+
+	templates = __templates[info->type];
+	template_size = __templates_size[info->type];
+
+	for (i = 0; info->config[i]; i++) {
+		u32 attr_mask = info->config[i];
+		u32 attr;
+
+		while (attr_mask) {
+			struct attribute *a;
+
+			attr = __ffs(attr_mask);
+			attr_mask &= ~BIT(attr);
+			if (attr >= template_size)
+				return -EINVAL;
+			a = hwmon_genattr(dev, drvdata, info->type, attr, i,
+					  templates[attr], ops);
+			if (IS_ERR(a)) {
+				if (PTR_ERR(a) != -ENOENT)
+					return PTR_ERR(a);
+				continue;
+			}
+			attrs[aindex++] = a;
+		}
+	}
+	return aindex;
+}
+
+static struct attribute **
+__hwmon_create_attrs(struct device *dev, const void *drvdata,
+		     const struct hwmon_chip_info *chip)
+{
+	int ret, i, aindex = 0, nattrs = 0;
+	struct attribute **attrs;
+
+	for (i = 0; chip->info[i]; i++)
+		nattrs += hwmon_num_channel_attrs(chip->info[i]);
+
+	if (nattrs == 0)
+		return ERR_PTR(-EINVAL);
+
+	attrs = devm_kcalloc(dev, nattrs + 1, sizeof(*attrs), GFP_KERNEL);
+	if (!attrs)
+		return ERR_PTR(-ENOMEM);
+
+	for (i = 0; chip->info[i]; i++) {
+		ret = hwmon_genattrs(dev, drvdata, &attrs[aindex], chip->ops,
+				     chip->info[i]);
+		if (ret < 0)
+			return ERR_PTR(ret);
+		aindex += ret;
+	}
+
+	return attrs;
+}
+
+static struct device *
+__hwmon_device_register(struct device *dev, const char *name, void *drvdata,
+			const struct hwmon_chip_info *chip,
+			const struct attribute_group **groups)
 {
 	struct hwmon_device *hwdev;
-	int err, id;
+	struct device *hdev;
+	int i, j, err, id;
 
 	/* Do not accept invalid characters in hwmon name attribute */
 	if (name && (!strlen(name) || strpbrk(name, "-* \t\n")))
@@ -112,28 +401,128 @@ hwmon_device_register_with_groups(struct device *dev, const char *name,
 		goto ida_remove;
 	}
 
+	hdev = &hwdev->dev;
+
+	if (chip && chip->ops->is_visible) {
+		struct attribute **attrs;
+		int ngroups = 2;
+
+		if (groups)
+			for (i = 0; groups[i]; i++)
+				ngroups++;
+
+		hwdev->groups = devm_kcalloc(dev, ngroups, sizeof(*groups),
+					     GFP_KERNEL);
+		if (!hwdev->groups)
+			return ERR_PTR(-ENOMEM);
+
+		attrs = __hwmon_create_attrs(dev, drvdata, chip);
+		if (IS_ERR(attrs)) {
+			err = PTR_ERR(attrs);
+			goto free_hwmon;
+		}
+
+		hwdev->group.attrs = attrs;
+		ngroups = 0;
+		hwdev->groups[ngroups++] = &hwdev->group;
+
+		if (groups) {
+			for (i = 0; groups[i]; i++)
+				hwdev->groups[ngroups++] = groups[i];
+		}
+
+		hdev->groups = hwdev->groups;
+	} else {
+		hdev->groups = groups;
+	}
+
 	hwdev->name = name;
-	hwdev->dev.class = &hwmon_class;
-	hwdev->dev.parent = dev;
-	hwdev->dev.groups = groups;
-	hwdev->dev.of_node = dev ? dev->of_node : NULL;
-	dev_set_drvdata(&hwdev->dev, drvdata);
-	dev_set_name(&hwdev->dev, HWMON_ID_FORMAT, id);
-	err = device_register(&hwdev->dev);
+	hdev->class = &hwmon_class;
+	hdev->parent = dev;
+	hdev->of_node = dev ? dev->of_node : NULL;
+	hwdev->chip = chip;
+	dev_set_drvdata(hdev, drvdata);
+	dev_set_name(hdev, HWMON_ID_FORMAT, id);
+	err = device_register(hdev);
 	if (err)
-		goto free;
+		goto free_hwmon;
+
+	if (chip && chip->ops->is_visible && chip->ops->read &&
+	    chip->info[0]->type == hwmon_chip &&
+	    (chip->info[0]->config[0] & HWMON_C_REGISTER_TZ)) {
+		const struct hwmon_channel_info **info = chip->info;
+
+		for (i = 1; info[i]; i++) {
+			if (info[i]->type != hwmon_temp)
+				continue;
+
+			for (j = 0; info[i]->config[j]; j++) {
+				if (!chip->ops->is_visible(drvdata, hwmon_temp,
+							   hwmon_temp_input, j))
+					continue;
+				if (info[i]->config[j] & HWMON_T_INPUT)
+					hwmon_thermal_add_sensor(dev, hwdev, j);
+			}
+		}
+	}
 
-	return &hwdev->dev;
+	return hdev;
 
-free:
+free_hwmon:
 	kfree(hwdev);
 ida_remove:
 	ida_simple_remove(&hwmon_ida, id);
 	return ERR_PTR(err);
 }
+
+/**
+ * hwmon_device_register_with_groups - register w/ hwmon
+ * @dev: the parent device
+ * @name: hwmon name attribute
+ * @drvdata: driver data to attach to created device
+ * @groups: List of attribute groups to create
+ *
+ * hwmon_device_unregister() must be called when the device is no
+ * longer needed.
+ *
+ * Returns the pointer to the new device.
+ */
+struct device *
+hwmon_device_register_with_groups(struct device *dev, const char *name,
+				  void *drvdata,
+				  const struct attribute_group **groups)
+{
+	return __hwmon_device_register(dev, name, drvdata, NULL, groups);
+}
 EXPORT_SYMBOL_GPL(hwmon_device_register_with_groups);
 
 /**
+ * hwmon_device_register_with_info - register w/ hwmon
+ * @dev: the parent device
+ * @name: hwmon name attribute
+ * @drvdata: driver data to attach to created device
+ * @info: Pointer to hwmon chip information
+ * @groups - pointer to list of driver specific attribute groups
+ *
+ * hwmon_device_unregister() must be called when the device is no
+ * longer needed.
+ *
+ * Returns the pointer to the new device.
+ */
+struct device *
+hwmon_device_register_with_info(struct device *dev, const char *name,
+				void *drvdata,
+				const struct hwmon_chip_info *chip,
+				const struct attribute_group **groups)
+{
+	if (chip && (!chip->ops || !chip->info))
+		return ERR_PTR(-EINVAL);
+
+	return __hwmon_device_register(dev, name, drvdata, chip, groups);
+}
+EXPORT_SYMBOL_GPL(hwmon_device_register_with_info);
+
+/**
  * hwmon_device_register - register w/ hwmon
  * @dev: the device to register
  *
@@ -211,6 +600,48 @@ error:
 }
 EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_groups);
 
+/**
+ * devm_hwmon_device_register_with_info - register w/ hwmon
+ * @dev: the parent device
+ * @name: hwmon name attribute
+ * @drvdata: driver data to attach to created device
+ * @info: Pointer to hwmon chip information
+ * @groups - pointer to list of driver specific attribute groups
+ *
+ * Returns the pointer to the new device. The new device is automatically
+ * unregistered with the parent device.
+ */
+struct device *
+devm_hwmon_device_register_with_info(struct device *dev, const char *name,
+				     void *drvdata,
+				     const struct hwmon_chip_info *chip,
+				     const struct attribute_group **groups)
+{
+	struct device **ptr, *hwdev;
+
+	if (!dev)
+		return ERR_PTR(-EINVAL);
+
+	ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL);
+	if (!ptr)
+		return ERR_PTR(-ENOMEM);
+
+	hwdev = hwmon_device_register_with_info(dev, name, drvdata, chip,
+						groups);
+	if (IS_ERR(hwdev))
+		goto error;
+
+	*ptr = hwdev;
+	devres_add(dev, ptr);
+
+	return hwdev;
+
+error:
+	devres_free(ptr);
+	return hwdev;
+}
+EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_info);
+
 static int devm_hwmon_match(struct device *dev, void *res, void *data)
 {
 	struct device **hwdev = res;
diff --git a/include/linux/hwmon.h b/include/linux/hwmon.h
index 09354f6c1d63..52e56d71d742 100644
--- a/include/linux/hwmon.h
+++ b/include/linux/hwmon.h
@@ -14,9 +14,147 @@
 #ifndef _HWMON_H_
 #define _HWMON_H_
 
+#include <linux/bitops.h>
+
 struct device;
 struct attribute_group;
 
+enum hwmon_sensor_types {
+	hwmon_chip,
+	hwmon_temp,
+	hwmon_in,
+	hwmon_curr,
+	hwmon_power,
+	hwmon_energy,
+};
+
+enum hwmon_chip_attributes {
+	hwmon_chip_temp_reset_history,
+	hwmon_chip_register_tz,
+	hwmon_chip_update_interval,
+	hwmon_chip_alarms,
+};
+
+#define HWMON_C_TEMP_RESET_HISTORY	BIT(hwmon_chip_temp_reset_history)
+#define HWMON_C_IN_RESET_HISTORY	BIT(hwmon_chip_in_reset_history)
+#define HWMON_C_REGISTER_TZ		BIT(hwmon_chip_register_tz)
+#define HWMON_C_UPDATE_INTERVAL		BIT(hwmon_chip_update_interval)
+#define HWMON_C_ALARMS			BIT(hwmon_chip_alarms)
+
+enum hwmon_temp_attributes {
+	hwmon_temp_input = 0,
+	hwmon_temp_type,
+	hwmon_temp_lcrit,
+	hwmon_temp_lcrit_hyst,
+	hwmon_temp_min,
+	hwmon_temp_min_hyst,
+	hwmon_temp_max,
+	hwmon_temp_max_hyst,
+	hwmon_temp_crit,
+	hwmon_temp_crit_hyst,
+	hwmon_temp_emergency,
+	hwmon_temp_emergency_hyst,
+	hwmon_temp_alarm,
+	hwmon_temp_lcrit_alarm,
+	hwmon_temp_min_alarm,
+	hwmon_temp_max_alarm,
+	hwmon_temp_crit_alarm,
+	hwmon_temp_emergency_alarm,
+	hwmon_temp_fault,
+	hwmon_temp_offset,
+	hwmon_temp_label,
+	hwmon_temp_lowest,
+	hwmon_temp_highest,
+	hwmon_temp_reset_history,
+};
+
+#define HWMON_T_INPUT		BIT(hwmon_temp_input)
+#define HWMON_T_TYPE		BIT(hwmon_temp_type)
+#define HWMON_T_LCRIT		BIT(hwmon_temp_lcrit)
+#define HWMON_T_LCRIT_HYST	BIT(hwmon_temp_lcrit_hyst)
+#define HWMON_T_MIN		BIT(hwmon_temp_min)
+#define HWMON_T_MIN_HYST	BIT(hwmon_temp_min_hyst)
+#define HWMON_T_MAX		BIT(hwmon_temp_max)
+#define HWMON_T_MAX_HYST	BIT(hwmon_temp_max_hyst)
+#define HWMON_T_CRIT		BIT(hwmon_temp_crit)
+#define HWMON_T_CRIT_HYST	BIT(hwmon_temp_crit_hyst)
+#define HWMON_T_EMERGENCY	BIT(hwmon_temp_emergency)
+#define HWMON_T_EMERGENCY_HYST	BIT(hwmon_temp_emergency_hyst)
+#define HWMON_T_MIN_ALARM	BIT(hwmon_temp_min_alarm)
+#define HWMON_T_MAX_ALARM	BIT(hwmon_temp_max_alarm)
+#define HWMON_T_CRIT_ALARM	BIT(hwmon_temp_crit_alarm)
+#define HWMON_T_EMERGENCY_ALARM	BIT(hwmon_temp_emergency_alarm)
+#define HWMON_T_FAULT		BIT(hwmon_temp_fault)
+#define HWMON_T_OFFSET		BIT(hwmon_temp_offset)
+#define HWMON_T_LABEL		BIT(hwmon_temp_label)
+#define HWMON_T_LOWEST		BIT(hwmon_temp_lowest)
+#define HWMON_T_HIGHEST		BIT(hwmon_temp_highest)
+#define HWMON_T_RESET_HISTORY	BIT(hwmon_temp_reset_history)
+
+/**
+ * struct hwmon_ops - hwmon device operations
+ * @is_visible: Callback to return attribute visibility. Mandatory.
+ *		Parameters are:
+ *		@const void *drvdata:
+ *			Pointer to driver-private data structure passed
+ *			as argument to hwmon_device_register_with_info().
+ *		@type:	Sensor type
+ *		@attr:	Sensor attribute
+ *		@channel:
+ *			Channel number
+ *		The function returns the file permissions.
+ *		If the return value is 0, no attribute will be created.
+ * @read:       Read callback. Optional. If not provided, attributes
+ *		will not be readable.
+ *		Parameters are:
+ *		@dev:	Pointer to hardware monitoring device
+ *		@type:	Sensor type
+ *		@attr:	Sensor attribute
+ *		@channel:
+ *			Channel number
+ *		@val:	Pointer to returned value
+ *		The function returns 0 on success or a negative error number.
+ * @write:	Write callback. Optional. If not provided, attributes
+ *		will not be writable.
+ *		Parameters are:
+ *		@dev:	Pointer to hardware monitoring device
+ *		@type:	Sensor type
+ *		@attr:	Sensor attribute
+ *		@channel:
+ *			Channel number
+ *		@val:	Value to write
+ *		The function returns 0 on success or a negative error number.
+ */
+struct hwmon_ops {
+	umode_t (*is_visible)(const void *drvdata, enum hwmon_sensor_types type,
+			      u32 attr, int channel);
+	int (*read)(struct device *dev, enum hwmon_sensor_types type,
+		    u32 attr, int channel, long *val);
+	int (*write)(struct device *dev, enum hwmon_sensor_types type,
+		     u32 attr, int channel, long val);
+};
+
+/**
+ * Channel information
+ * @type:	Channel type.
+ * @config:	Pointer to NULL-terminated list of channel parameters.
+ *		Use for per-channel attributes.
+ */
+struct hwmon_channel_info {
+	enum hwmon_sensor_types type;
+	const u32 *config;
+};
+
+/**
+ * Chip configuration
+ * @ops:	Pointer to hwmon operations.
+ * @info:	Null-terminated list of channel information.
+ */
+struct hwmon_chip_info {
+	const struct hwmon_ops *ops;
+	const struct hwmon_channel_info **info;
+};
+
 struct device *hwmon_device_register(struct device *dev);
 struct device *
 hwmon_device_register_with_groups(struct device *dev, const char *name,
@@ -26,6 +164,16 @@ struct device *
 devm_hwmon_device_register_with_groups(struct device *dev, const char *name,
 				       void *drvdata,
 				       const struct attribute_group **groups);
+struct device *
+hwmon_device_register_with_info(struct device *dev,
+				const char *name, void *drvdata,
+				const struct hwmon_chip_info *info,
+				const struct attribute_group **groups);
+struct device *
+devm_hwmon_device_register_with_info(struct device *dev,
+				     const char *name, void *drvdata,
+				     const struct hwmon_chip_info *info,
+				     const struct attribute_group **groups);
 
 void hwmon_device_unregister(struct device *dev);
 void devm_hwmon_device_unregister(struct device *dev);
-- 
2.5.0

^ permalink raw reply related	[flat|nested] 26+ messages in thread

* [PATCH v3 3/9] hwmon: (core) Add voltage attribute support to new API
  2016-07-25  3:32 [PATCH v3 0/9] hwmon: New hwmon registration API Guenter Roeck
  2016-07-25  3:32 ` [PATCH v3 1/9] hwmon: (core) Order include files alphabetically Guenter Roeck
  2016-07-25  3:32 ` [PATCH v3 2/9] hwmon: (core) New hwmon registration API Guenter Roeck
@ 2016-07-25  3:32 ` Guenter Roeck
  2016-07-25  3:32   ` Guenter Roeck
                   ` (5 subsequent siblings)
  8 siblings, 0 replies; 26+ messages in thread
From: Guenter Roeck @ 2016-07-25  3:32 UTC (permalink / raw)
  To: Jean Delvare
  Cc: Jonathan Cameron, Zhang Rui, Eduardo Valentin, Punit Agrawal,
	linux-pm, linux-iio, linux-hwmon, linux-kernel, Guenter Roeck

Acked-by: Punit Agrawal <punit.agrawal@arm.com>
Reviewed-by: Jonathan Cameron <jic23@kernel.org>
Signed-off-by: Guenter Roeck <linux@roeck-us.net>
---
v3: No change
v2: No change

 drivers/hwmon/hwmon.c | 21 +++++++++++++++++++++
 include/linux/hwmon.h | 35 +++++++++++++++++++++++++++++++++++
 2 files changed, 56 insertions(+)

diff --git a/drivers/hwmon/hwmon.c b/drivers/hwmon/hwmon.c
index 3e4cc442a089..7697dfa92558 100644
--- a/drivers/hwmon/hwmon.c
+++ b/drivers/hwmon/hwmon.c
@@ -259,6 +259,7 @@ static struct attribute *hwmon_genattr(struct device *dev,
 
 static const char * const hwmon_chip_attr_templates[] = {
 	[hwmon_chip_temp_reset_history] = "temp_reset_history",
+	[hwmon_chip_in_reset_history] = "in_reset_history",
 	[hwmon_chip_update_interval] = "update_interval",
 	[hwmon_chip_alarms] = "alarms",
 };
@@ -290,14 +291,34 @@ static const char * const hwmon_temp_attr_templates[] = {
 	[hwmon_temp_reset_history] = "temp%d_reset_history",
 };
 
+static const char * const hwmon_in_attr_templates[] = {
+	[hwmon_in_input] = "in%d_input",
+	[hwmon_in_min] = "in%d_min",
+	[hwmon_in_max] = "in%d_max",
+	[hwmon_in_lcrit] = "in%d_lcrit",
+	[hwmon_in_crit] = "in%d_crit",
+	[hwmon_in_average] = "in%d_average",
+	[hwmon_in_lowest] = "in%d_lowest",
+	[hwmon_in_highest] = "in%d_highest",
+	[hwmon_in_reset_history] = "in%d_reset_history",
+	[hwmon_in_label] = "in%d_label",
+	[hwmon_in_alarm] = "in%d_alarm",
+	[hwmon_in_min_alarm] = "in%d_min_alarm",
+	[hwmon_in_max_alarm] = "in%d_max_alarm",
+	[hwmon_in_lcrit_alarm] = "in%d_lcrit_alarm",
+	[hwmon_in_crit_alarm] = "in%d_crit_alarm",
+};
+
 static const char * const *__templates[] = {
 	[hwmon_chip] = hwmon_chip_attr_templates,
 	[hwmon_temp] = hwmon_temp_attr_templates,
+	[hwmon_in] = hwmon_in_attr_templates,
 };
 
 static const int __templates_size[] = {
 	[hwmon_chip] = ARRAY_SIZE(hwmon_chip_attr_templates),
 	[hwmon_temp] = ARRAY_SIZE(hwmon_temp_attr_templates),
+	[hwmon_in] = ARRAY_SIZE(hwmon_in_attr_templates),
 };
 
 static int hwmon_num_channel_attrs(const struct hwmon_channel_info *info)
diff --git a/include/linux/hwmon.h b/include/linux/hwmon.h
index 52e56d71d742..a01b8e3fc6f3 100644
--- a/include/linux/hwmon.h
+++ b/include/linux/hwmon.h
@@ -30,6 +30,7 @@ enum hwmon_sensor_types {
 
 enum hwmon_chip_attributes {
 	hwmon_chip_temp_reset_history,
+	hwmon_chip_in_reset_history,
 	hwmon_chip_register_tz,
 	hwmon_chip_update_interval,
 	hwmon_chip_alarms,
@@ -91,6 +92,40 @@ enum hwmon_temp_attributes {
 #define HWMON_T_HIGHEST		BIT(hwmon_temp_highest)
 #define HWMON_T_RESET_HISTORY	BIT(hwmon_temp_reset_history)
 
+enum hwmon_in_attributes {
+	hwmon_in_input,
+	hwmon_in_min,
+	hwmon_in_max,
+	hwmon_in_lcrit,
+	hwmon_in_crit,
+	hwmon_in_average,
+	hwmon_in_lowest,
+	hwmon_in_highest,
+	hwmon_in_reset_history,
+	hwmon_in_label,
+	hwmon_in_alarm,
+	hwmon_in_min_alarm,
+	hwmon_in_max_alarm,
+	hwmon_in_lcrit_alarm,
+	hwmon_in_crit_alarm,
+};
+
+#define HWMON_I_INPUT		BIT(hwmon_in_input)
+#define HWMON_I_MIN		BIT(hwmon_in_min)
+#define HWMON_I_MAX		BIT(hwmon_in_max)
+#define HWMON_I_LCRIT		BIT(hwmon_in_lcrit)
+#define HWMON_I_CRIT		BIT(hwmon_in_crit)
+#define HWMON_I_AVERAGE		BIT(hwmon_in_average)
+#define HWMON_I_LOWEST		BIT(hwmon_in_lowest)
+#define HWMON_I_HIGHEST		BIT(hwmon_in_highest)
+#define HWMON_I_RESET_HISTORY	BIT(hwmon_in_reset_history)
+#define HWMON_I_LABEL		BIT(hwmon_in_label)
+#define HWMON_I_ALARM		BIT(hwmon_in_alarm)
+#define HWMON_I_MIN_ALARM	BIT(hwmon_in_min_alarm)
+#define HWMON_I_MAX_ALARM	BIT(hwmon_in_max_alarm)
+#define HWMON_I_LCRIT_ALARM	BIT(hwmon_in_lcrit_alarm)
+#define HWMON_I_CRIT_ALARM	BIT(hwmon_in_crit_alarm)
+
 /**
  * struct hwmon_ops - hwmon device operations
  * @is_visible: Callback to return attribute visibility. Mandatory.
-- 
2.5.0

^ permalink raw reply related	[flat|nested] 26+ messages in thread

* [PATCH v3 4/9] hwmon: (core) Add current attribute support to new API
@ 2016-07-25  3:32   ` Guenter Roeck
  0 siblings, 0 replies; 26+ messages in thread
From: Guenter Roeck @ 2016-07-25  3:32 UTC (permalink / raw)
  To: Jean Delvare
  Cc: Jonathan Cameron, Zhang Rui, Eduardo Valentin, Punit Agrawal,
	linux-pm, linux-iio, linux-hwmon, linux-kernel, Guenter Roeck

Acked-by: Punit Agrawal <punit.agrawal@arm.com>
Reviewed-by: Jonathan Cameron <jic23@kernel.org>
Signed-off-by: Guenter Roeck <linux@roeck-us.net>
---
v3: No change
v2: No change

 drivers/hwmon/hwmon.c | 21 +++++++++++++++++++++
 include/linux/hwmon.h | 36 ++++++++++++++++++++++++++++++++++++
 2 files changed, 57 insertions(+)

diff --git a/drivers/hwmon/hwmon.c b/drivers/hwmon/hwmon.c
index 7697dfa92558..9229229a99a5 100644
--- a/drivers/hwmon/hwmon.c
+++ b/drivers/hwmon/hwmon.c
@@ -260,6 +260,7 @@ static struct attribute *hwmon_genattr(struct device *dev,
 static const char * const hwmon_chip_attr_templates[] = {
 	[hwmon_chip_temp_reset_history] = "temp_reset_history",
 	[hwmon_chip_in_reset_history] = "in_reset_history",
+	[hwmon_chip_curr_reset_history] = "curr_reset_history",
 	[hwmon_chip_update_interval] = "update_interval",
 	[hwmon_chip_alarms] = "alarms",
 };
@@ -309,16 +310,36 @@ static const char * const hwmon_in_attr_templates[] = {
 	[hwmon_in_crit_alarm] = "in%d_crit_alarm",
 };
 
+static const char * const hwmon_curr_attr_templates[] = {
+	[hwmon_curr_input] = "curr%d_input",
+	[hwmon_curr_min] = "curr%d_min",
+	[hwmon_curr_max] = "curr%d_max",
+	[hwmon_curr_lcrit] = "curr%d_lcrit",
+	[hwmon_curr_crit] = "curr%d_crit",
+	[hwmon_curr_average] = "curr%d_average",
+	[hwmon_curr_lowest] = "curr%d_lowest",
+	[hwmon_curr_highest] = "curr%d_highest",
+	[hwmon_curr_reset_history] = "curr%d_reset_history",
+	[hwmon_curr_label] = "curr%d_label",
+	[hwmon_curr_alarm] = "curr%d_alarm",
+	[hwmon_curr_min_alarm] = "curr%d_min_alarm",
+	[hwmon_curr_max_alarm] = "curr%d_max_alarm",
+	[hwmon_curr_lcrit_alarm] = "curr%d_lcrit_alarm",
+	[hwmon_curr_crit_alarm] = "curr%d_crit_alarm",
+};
+
 static const char * const *__templates[] = {
 	[hwmon_chip] = hwmon_chip_attr_templates,
 	[hwmon_temp] = hwmon_temp_attr_templates,
 	[hwmon_in] = hwmon_in_attr_templates,
+	[hwmon_curr] = hwmon_curr_attr_templates,
 };
 
 static const int __templates_size[] = {
 	[hwmon_chip] = ARRAY_SIZE(hwmon_chip_attr_templates),
 	[hwmon_temp] = ARRAY_SIZE(hwmon_temp_attr_templates),
 	[hwmon_in] = ARRAY_SIZE(hwmon_in_attr_templates),
+	[hwmon_curr] = ARRAY_SIZE(hwmon_curr_attr_templates),
 };
 
 static int hwmon_num_channel_attrs(const struct hwmon_channel_info *info)
diff --git a/include/linux/hwmon.h b/include/linux/hwmon.h
index a01b8e3fc6f3..8781c2253b1d 100644
--- a/include/linux/hwmon.h
+++ b/include/linux/hwmon.h
@@ -31,6 +31,7 @@ enum hwmon_sensor_types {
 enum hwmon_chip_attributes {
 	hwmon_chip_temp_reset_history,
 	hwmon_chip_in_reset_history,
+	hwmon_chip_curr_reset_history,
 	hwmon_chip_register_tz,
 	hwmon_chip_update_interval,
 	hwmon_chip_alarms,
@@ -38,6 +39,7 @@ enum hwmon_chip_attributes {
 
 #define HWMON_C_TEMP_RESET_HISTORY	BIT(hwmon_chip_temp_reset_history)
 #define HWMON_C_IN_RESET_HISTORY	BIT(hwmon_chip_in_reset_history)
+#define HWMON_C_CURR_RESET_HISTORY	BIT(hwmon_chip_curr_reset_history)
 #define HWMON_C_REGISTER_TZ		BIT(hwmon_chip_register_tz)
 #define HWMON_C_UPDATE_INTERVAL		BIT(hwmon_chip_update_interval)
 #define HWMON_C_ALARMS			BIT(hwmon_chip_alarms)
@@ -126,6 +128,40 @@ enum hwmon_in_attributes {
 #define HWMON_I_LCRIT_ALARM	BIT(hwmon_in_lcrit_alarm)
 #define HWMON_I_CRIT_ALARM	BIT(hwmon_in_crit_alarm)
 
+enum hwmon_curr_attributes {
+	hwmon_curr_input,
+	hwmon_curr_min,
+	hwmon_curr_max,
+	hwmon_curr_lcrit,
+	hwmon_curr_crit,
+	hwmon_curr_average,
+	hwmon_curr_lowest,
+	hwmon_curr_highest,
+	hwmon_curr_reset_history,
+	hwmon_curr_label,
+	hwmon_curr_alarm,
+	hwmon_curr_min_alarm,
+	hwmon_curr_max_alarm,
+	hwmon_curr_lcrit_alarm,
+	hwmon_curr_crit_alarm,
+};
+
+#define HWMON_C_INPUT		BIT(hwmon_curr_input)
+#define HWMON_C_MIN		BIT(hwmon_curr_min)
+#define HWMON_C_MAX		BIT(hwmon_curr_max)
+#define HWMON_C_LCRIT		BIT(hwmon_curr_lcrit)
+#define HWMON_C_CRIT		BIT(hwmon_curr_crit)
+#define HWMON_C_AVERAGE		BIT(hwmon_curr_average)
+#define HWMON_C_LOWEST		BIT(hwmon_curr_lowest)
+#define HWMON_C_HIGHEST		BIT(hwmon_curr_highest)
+#define HWMON_C_RESET_HISTORY	BIT(hwmon_curr_reset_history)
+#define HWMON_C_LABEL		BIT(hwmon_curr_label)
+#define HWMON_C_ALARM		BIT(hwmon_curr_alarm)
+#define HWMON_C_MIN_ALARM	BIT(hwmon_curr_min_alarm)
+#define HWMON_C_MAX_ALARM	BIT(hwmon_curr_max_alarm)
+#define HWMON_C_LCRIT_ALARM	BIT(hwmon_curr_lcrit_alarm)
+#define HWMON_C_CRIT_ALARM	BIT(hwmon_curr_crit_alarm)
+
 /**
  * struct hwmon_ops - hwmon device operations
  * @is_visible: Callback to return attribute visibility. Mandatory.
-- 
2.5.0

^ permalink raw reply related	[flat|nested] 26+ messages in thread

* [PATCH v3 4/9] hwmon: (core) Add current attribute support to new API
@ 2016-07-25  3:32   ` Guenter Roeck
  0 siblings, 0 replies; 26+ messages in thread
From: Guenter Roeck @ 2016-07-25  3:32 UTC (permalink / raw)
  To: Jean Delvare
  Cc: Jonathan Cameron, Zhang Rui, Eduardo Valentin, Punit Agrawal,
	linux-pm-u79uwXL29TY76Z2rM5mHXA,
	linux-iio-u79uwXL29TY76Z2rM5mHXA,
	linux-hwmon-u79uwXL29TY76Z2rM5mHXA,
	linux-kernel-u79uwXL29TY76Z2rM5mHXA, Guenter Roeck

Acked-by: Punit Agrawal <punit.agrawal-5wv7dgnIgG8@public.gmane.org>
Reviewed-by: Jonathan Cameron <jic23-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org>
Signed-off-by: Guenter Roeck <linux-0h96xk9xTtrk1uMJSBkQmQ@public.gmane.org>
---
v3: No change
v2: No change

 drivers/hwmon/hwmon.c | 21 +++++++++++++++++++++
 include/linux/hwmon.h | 36 ++++++++++++++++++++++++++++++++++++
 2 files changed, 57 insertions(+)

diff --git a/drivers/hwmon/hwmon.c b/drivers/hwmon/hwmon.c
index 7697dfa92558..9229229a99a5 100644
--- a/drivers/hwmon/hwmon.c
+++ b/drivers/hwmon/hwmon.c
@@ -260,6 +260,7 @@ static struct attribute *hwmon_genattr(struct device *dev,
 static const char * const hwmon_chip_attr_templates[] = {
 	[hwmon_chip_temp_reset_history] = "temp_reset_history",
 	[hwmon_chip_in_reset_history] = "in_reset_history",
+	[hwmon_chip_curr_reset_history] = "curr_reset_history",
 	[hwmon_chip_update_interval] = "update_interval",
 	[hwmon_chip_alarms] = "alarms",
 };
@@ -309,16 +310,36 @@ static const char * const hwmon_in_attr_templates[] = {
 	[hwmon_in_crit_alarm] = "in%d_crit_alarm",
 };
 
+static const char * const hwmon_curr_attr_templates[] = {
+	[hwmon_curr_input] = "curr%d_input",
+	[hwmon_curr_min] = "curr%d_min",
+	[hwmon_curr_max] = "curr%d_max",
+	[hwmon_curr_lcrit] = "curr%d_lcrit",
+	[hwmon_curr_crit] = "curr%d_crit",
+	[hwmon_curr_average] = "curr%d_average",
+	[hwmon_curr_lowest] = "curr%d_lowest",
+	[hwmon_curr_highest] = "curr%d_highest",
+	[hwmon_curr_reset_history] = "curr%d_reset_history",
+	[hwmon_curr_label] = "curr%d_label",
+	[hwmon_curr_alarm] = "curr%d_alarm",
+	[hwmon_curr_min_alarm] = "curr%d_min_alarm",
+	[hwmon_curr_max_alarm] = "curr%d_max_alarm",
+	[hwmon_curr_lcrit_alarm] = "curr%d_lcrit_alarm",
+	[hwmon_curr_crit_alarm] = "curr%d_crit_alarm",
+};
+
 static const char * const *__templates[] = {
 	[hwmon_chip] = hwmon_chip_attr_templates,
 	[hwmon_temp] = hwmon_temp_attr_templates,
 	[hwmon_in] = hwmon_in_attr_templates,
+	[hwmon_curr] = hwmon_curr_attr_templates,
 };
 
 static const int __templates_size[] = {
 	[hwmon_chip] = ARRAY_SIZE(hwmon_chip_attr_templates),
 	[hwmon_temp] = ARRAY_SIZE(hwmon_temp_attr_templates),
 	[hwmon_in] = ARRAY_SIZE(hwmon_in_attr_templates),
+	[hwmon_curr] = ARRAY_SIZE(hwmon_curr_attr_templates),
 };
 
 static int hwmon_num_channel_attrs(const struct hwmon_channel_info *info)
diff --git a/include/linux/hwmon.h b/include/linux/hwmon.h
index a01b8e3fc6f3..8781c2253b1d 100644
--- a/include/linux/hwmon.h
+++ b/include/linux/hwmon.h
@@ -31,6 +31,7 @@ enum hwmon_sensor_types {
 enum hwmon_chip_attributes {
 	hwmon_chip_temp_reset_history,
 	hwmon_chip_in_reset_history,
+	hwmon_chip_curr_reset_history,
 	hwmon_chip_register_tz,
 	hwmon_chip_update_interval,
 	hwmon_chip_alarms,
@@ -38,6 +39,7 @@ enum hwmon_chip_attributes {
 
 #define HWMON_C_TEMP_RESET_HISTORY	BIT(hwmon_chip_temp_reset_history)
 #define HWMON_C_IN_RESET_HISTORY	BIT(hwmon_chip_in_reset_history)
+#define HWMON_C_CURR_RESET_HISTORY	BIT(hwmon_chip_curr_reset_history)
 #define HWMON_C_REGISTER_TZ		BIT(hwmon_chip_register_tz)
 #define HWMON_C_UPDATE_INTERVAL		BIT(hwmon_chip_update_interval)
 #define HWMON_C_ALARMS			BIT(hwmon_chip_alarms)
@@ -126,6 +128,40 @@ enum hwmon_in_attributes {
 #define HWMON_I_LCRIT_ALARM	BIT(hwmon_in_lcrit_alarm)
 #define HWMON_I_CRIT_ALARM	BIT(hwmon_in_crit_alarm)
 
+enum hwmon_curr_attributes {
+	hwmon_curr_input,
+	hwmon_curr_min,
+	hwmon_curr_max,
+	hwmon_curr_lcrit,
+	hwmon_curr_crit,
+	hwmon_curr_average,
+	hwmon_curr_lowest,
+	hwmon_curr_highest,
+	hwmon_curr_reset_history,
+	hwmon_curr_label,
+	hwmon_curr_alarm,
+	hwmon_curr_min_alarm,
+	hwmon_curr_max_alarm,
+	hwmon_curr_lcrit_alarm,
+	hwmon_curr_crit_alarm,
+};
+
+#define HWMON_C_INPUT		BIT(hwmon_curr_input)
+#define HWMON_C_MIN		BIT(hwmon_curr_min)
+#define HWMON_C_MAX		BIT(hwmon_curr_max)
+#define HWMON_C_LCRIT		BIT(hwmon_curr_lcrit)
+#define HWMON_C_CRIT		BIT(hwmon_curr_crit)
+#define HWMON_C_AVERAGE		BIT(hwmon_curr_average)
+#define HWMON_C_LOWEST		BIT(hwmon_curr_lowest)
+#define HWMON_C_HIGHEST		BIT(hwmon_curr_highest)
+#define HWMON_C_RESET_HISTORY	BIT(hwmon_curr_reset_history)
+#define HWMON_C_LABEL		BIT(hwmon_curr_label)
+#define HWMON_C_ALARM		BIT(hwmon_curr_alarm)
+#define HWMON_C_MIN_ALARM	BIT(hwmon_curr_min_alarm)
+#define HWMON_C_MAX_ALARM	BIT(hwmon_curr_max_alarm)
+#define HWMON_C_LCRIT_ALARM	BIT(hwmon_curr_lcrit_alarm)
+#define HWMON_C_CRIT_ALARM	BIT(hwmon_curr_crit_alarm)
+
 /**
  * struct hwmon_ops - hwmon device operations
  * @is_visible: Callback to return attribute visibility. Mandatory.
-- 
2.5.0

^ permalink raw reply related	[flat|nested] 26+ messages in thread

* [PATCH v3 5/9] hwmon: (core) Add power attribute support to new API
  2016-07-25  3:32 [PATCH v3 0/9] hwmon: New hwmon registration API Guenter Roeck
                   ` (3 preceding siblings ...)
  2016-07-25  3:32   ` Guenter Roeck
@ 2016-07-25  3:32 ` Guenter Roeck
  2016-07-25  3:32 ` [PATCH v3 6/9] hwmon: (core) Add energy and humidity " Guenter Roeck
                   ` (3 subsequent siblings)
  8 siblings, 0 replies; 26+ messages in thread
From: Guenter Roeck @ 2016-07-25  3:32 UTC (permalink / raw)
  To: Jean Delvare
  Cc: Jonathan Cameron, Zhang Rui, Eduardo Valentin, Punit Agrawal,
	linux-pm, linux-iio, linux-hwmon, linux-kernel, Guenter Roeck

Acked-by: Punit Agrawal <punit.agrawal@arm.com>
Reviewed-by: Jonathan Cameron <jic23@kernel.org>
Signed-off-by: Guenter Roeck <linux@roeck-us.net>
---
v3: No change
v2: No change

 drivers/hwmon/hwmon.c | 30 ++++++++++++++++++++++++++++
 include/linux/hwmon.h | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 84 insertions(+)

diff --git a/drivers/hwmon/hwmon.c b/drivers/hwmon/hwmon.c
index 9229229a99a5..85f4e27548cb 100644
--- a/drivers/hwmon/hwmon.c
+++ b/drivers/hwmon/hwmon.c
@@ -261,6 +261,7 @@ static const char * const hwmon_chip_attr_templates[] = {
 	[hwmon_chip_temp_reset_history] = "temp_reset_history",
 	[hwmon_chip_in_reset_history] = "in_reset_history",
 	[hwmon_chip_curr_reset_history] = "curr_reset_history",
+	[hwmon_chip_power_reset_history] = "power_reset_history",
 	[hwmon_chip_update_interval] = "update_interval",
 	[hwmon_chip_alarms] = "alarms",
 };
@@ -328,11 +329,39 @@ static const char * const hwmon_curr_attr_templates[] = {
 	[hwmon_curr_crit_alarm] = "curr%d_crit_alarm",
 };
 
+static const char * const hwmon_power_attr_templates[] = {
+	[hwmon_power_average] = "power%d_average",
+	[hwmon_power_average_interval] = "power%d_average_interval",
+	[hwmon_power_average_interval_max] = "power%d_interval_max",
+	[hwmon_power_average_interval_min] = "power%d_interval_min",
+	[hwmon_power_average_highest] = "power%d_average_highest",
+	[hwmon_power_average_lowest] = "power%d_average_lowest",
+	[hwmon_power_average_max] = "power%d_average_max",
+	[hwmon_power_average_min] = "power%d_average_min",
+	[hwmon_power_input] = "power%d_input",
+	[hwmon_power_input_highest] = "power%d_input_highest",
+	[hwmon_power_input_lowest] = "power%d_input_lowest",
+	[hwmon_power_reset_history] = "power%d_reset_history",
+	[hwmon_power_accuracy] = "power%d_accuracy",
+	[hwmon_power_cap] = "power%d_cap",
+	[hwmon_power_cap_hyst] = "power%d_cap_hyst",
+	[hwmon_power_cap_max] = "power%d_cap_max",
+	[hwmon_power_cap_min] = "power%d_cap_min",
+	[hwmon_power_max] = "power%d_max",
+	[hwmon_power_crit] = "power%d_crit",
+	[hwmon_power_label] = "power%d_label",
+	[hwmon_power_alarm] = "power%d_alarm",
+	[hwmon_power_cap_alarm] = "power%d_cap_alarm",
+	[hwmon_power_max_alarm] = "power%d_max_alarm",
+	[hwmon_power_crit_alarm] = "power%d_crit_alarm",
+};
+
 static const char * const *__templates[] = {
 	[hwmon_chip] = hwmon_chip_attr_templates,
 	[hwmon_temp] = hwmon_temp_attr_templates,
 	[hwmon_in] = hwmon_in_attr_templates,
 	[hwmon_curr] = hwmon_curr_attr_templates,
+	[hwmon_power] = hwmon_power_attr_templates,
 };
 
 static const int __templates_size[] = {
@@ -340,6 +369,7 @@ static const int __templates_size[] = {
 	[hwmon_temp] = ARRAY_SIZE(hwmon_temp_attr_templates),
 	[hwmon_in] = ARRAY_SIZE(hwmon_in_attr_templates),
 	[hwmon_curr] = ARRAY_SIZE(hwmon_curr_attr_templates),
+	[hwmon_power] = ARRAY_SIZE(hwmon_power_attr_templates),
 };
 
 static int hwmon_num_channel_attrs(const struct hwmon_channel_info *info)
diff --git a/include/linux/hwmon.h b/include/linux/hwmon.h
index 8781c2253b1d..d7e432ef7c2a 100644
--- a/include/linux/hwmon.h
+++ b/include/linux/hwmon.h
@@ -32,6 +32,7 @@ enum hwmon_chip_attributes {
 	hwmon_chip_temp_reset_history,
 	hwmon_chip_in_reset_history,
 	hwmon_chip_curr_reset_history,
+	hwmon_chip_power_reset_history,
 	hwmon_chip_register_tz,
 	hwmon_chip_update_interval,
 	hwmon_chip_alarms,
@@ -40,6 +41,7 @@ enum hwmon_chip_attributes {
 #define HWMON_C_TEMP_RESET_HISTORY	BIT(hwmon_chip_temp_reset_history)
 #define HWMON_C_IN_RESET_HISTORY	BIT(hwmon_chip_in_reset_history)
 #define HWMON_C_CURR_RESET_HISTORY	BIT(hwmon_chip_curr_reset_history)
+#define HWMON_C_POWER_RESET_HISTORY	BIT(hwmon_chip_power_reset_history)
 #define HWMON_C_REGISTER_TZ		BIT(hwmon_chip_register_tz)
 #define HWMON_C_UPDATE_INTERVAL		BIT(hwmon_chip_update_interval)
 #define HWMON_C_ALARMS			BIT(hwmon_chip_alarms)
@@ -162,6 +164,58 @@ enum hwmon_curr_attributes {
 #define HWMON_C_LCRIT_ALARM	BIT(hwmon_curr_lcrit_alarm)
 #define HWMON_C_CRIT_ALARM	BIT(hwmon_curr_crit_alarm)
 
+enum hwmon_power_attributes {
+	hwmon_power_average,
+	hwmon_power_average_interval,
+	hwmon_power_average_interval_max,
+	hwmon_power_average_interval_min,
+	hwmon_power_average_highest,
+	hwmon_power_average_lowest,
+	hwmon_power_average_max,
+	hwmon_power_average_min,
+	hwmon_power_input,
+	hwmon_power_input_highest,
+	hwmon_power_input_lowest,
+	hwmon_power_reset_history,
+	hwmon_power_accuracy,
+	hwmon_power_cap,
+	hwmon_power_cap_hyst,
+	hwmon_power_cap_max,
+	hwmon_power_cap_min,
+	hwmon_power_max,
+	hwmon_power_crit,
+	hwmon_power_label,
+	hwmon_power_alarm,
+	hwmon_power_cap_alarm,
+	hwmon_power_max_alarm,
+	hwmon_power_crit_alarm,
+};
+
+#define HWMON_P_AVERAGE			BIT(hwmon_power_average)
+#define HWMON_P_AVERAGE_INTERVAL	BIT(hwmon_power_average_interval)
+#define HWMON_P_AVERAGE_INTERVAL_MAX	BIT(hwmon_power_average_interval_max)
+#define HWMON_P_AVERAGE_INTERVAL_MIN	BIT(hwmon_power_average_interval_min)
+#define HWMON_P_AVERAGE_HIGHEST		BIT(hwmon_power_average_highest)
+#define HWMON_P_AVERAGE_LOWEST		BIT(hwmon_power_average_lowest)
+#define HWMON_P_AVERAGE_MAX		BIT(hwmon_power_average_max)
+#define HWMON_P_AVERAGE_MIN		BIT(hwmon_power_average_min)
+#define HWMON_P_INPUT			BIT(hwmon_power_input)
+#define HWMON_P_INPUT_HIGHEST		BIT(hwmon_power_input_highest)
+#define HWMON_P_INPUT_LOWEST		BIT(hwmon_power_input_lowest)
+#define HWMON_P_RESET_HISTORY		BIT(hwmon_power_reset_history)
+#define HWMON_P_ACCURACY		BIT(hwmon_power_accuracy)
+#define HWMON_P_CAP			BIT(hwmon_power_cap)
+#define HWMON_P_CAP_HYST		BIT(hwmon_power_cap_hyst)
+#define HWMON_P_CAP_MAX			BIT(hwmon_power_cap_max)
+#define HWMON_P_CAP_MIN			BIT(hwmon_power_cap_min)
+#define HWMON_P_MAX			BIT(hwmon_power_max)
+#define HWMON_P_CRIT			BIT(hwmon_power_crit)
+#define HWMON_P_LABEL			BIT(hwmon_power_label)
+#define HWMON_P_ALARM			BIT(hwmon_power_alarm)
+#define HWMON_P_CAP_ALARM		BIT(hwmon_power_cap_alarm)
+#define HWMON_P_MAX_ALARM		BIT(hwmon_power_max_alarm)
+#define HWMON_P_CRIT_ALARM		BIT(hwmon_power_crit_alarm)
+
 /**
  * struct hwmon_ops - hwmon device operations
  * @is_visible: Callback to return attribute visibility. Mandatory.
-- 
2.5.0

^ permalink raw reply related	[flat|nested] 26+ messages in thread

* [PATCH v3 6/9] hwmon: (core) Add energy and humidity attribute support to new API
  2016-07-25  3:32 [PATCH v3 0/9] hwmon: New hwmon registration API Guenter Roeck
                   ` (4 preceding siblings ...)
  2016-07-25  3:32 ` [PATCH v3 5/9] hwmon: (core) Add power " Guenter Roeck
@ 2016-07-25  3:32 ` Guenter Roeck
  2016-07-25  3:32 ` [PATCH v3 7/9] hwmon: (core) Add fan " Guenter Roeck
                   ` (2 subsequent siblings)
  8 siblings, 0 replies; 26+ messages in thread
From: Guenter Roeck @ 2016-07-25  3:32 UTC (permalink / raw)
  To: Jean Delvare
  Cc: Jonathan Cameron, Zhang Rui, Eduardo Valentin, Punit Agrawal,
	linux-pm, linux-iio, linux-hwmon, linux-kernel, Guenter Roeck

Acked-by: Punit Agrawal <punit.agrawal@arm.com>
Reviewed-by: Jonathan Cameron <jic23@kernel.org>
Signed-off-by: Guenter Roeck <linux@roeck-us.net>
---
v3: No change

 drivers/hwmon/hwmon.c | 20 ++++++++++++++++++++
 include/linux/hwmon.h | 29 +++++++++++++++++++++++++++++
 2 files changed, 49 insertions(+)

diff --git a/drivers/hwmon/hwmon.c b/drivers/hwmon/hwmon.c
index 85f4e27548cb..2dd4e0acfe12 100644
--- a/drivers/hwmon/hwmon.c
+++ b/drivers/hwmon/hwmon.c
@@ -356,12 +356,30 @@ static const char * const hwmon_power_attr_templates[] = {
 	[hwmon_power_crit_alarm] = "power%d_crit_alarm",
 };
 
+static const char * const hwmon_energy_attr_templates[] = {
+	[hwmon_energy_input] = "energy%d_input",
+	[hwmon_energy_label] = "energy%d_label",
+};
+
+static const char * const hwmon_humidity_attr_templates[] = {
+	[hwmon_humidity_input] = "humidity%d_input",
+	[hwmon_humidity_label] = "humidity%d_label",
+	[hwmon_humidity_min] = "humidity%d_min",
+	[hwmon_humidity_min_hyst] = "humidity%d_min_hyst",
+	[hwmon_humidity_max] = "humidity%d_max",
+	[hwmon_humidity_max_hyst] = "humidity%d_max_hyst",
+	[hwmon_humidity_alarm] = "humidity%d_alarm",
+	[hwmon_humidity_fault] = "humidity%d_fault",
+};
+
 static const char * const *__templates[] = {
 	[hwmon_chip] = hwmon_chip_attr_templates,
 	[hwmon_temp] = hwmon_temp_attr_templates,
 	[hwmon_in] = hwmon_in_attr_templates,
 	[hwmon_curr] = hwmon_curr_attr_templates,
 	[hwmon_power] = hwmon_power_attr_templates,
+	[hwmon_energy] = hwmon_energy_attr_templates,
+	[hwmon_humidity] = hwmon_humidity_attr_templates,
 };
 
 static const int __templates_size[] = {
@@ -370,6 +388,8 @@ static const int __templates_size[] = {
 	[hwmon_in] = ARRAY_SIZE(hwmon_in_attr_templates),
 	[hwmon_curr] = ARRAY_SIZE(hwmon_curr_attr_templates),
 	[hwmon_power] = ARRAY_SIZE(hwmon_power_attr_templates),
+	[hwmon_energy] = ARRAY_SIZE(hwmon_energy_attr_templates),
+	[hwmon_humidity] = ARRAY_SIZE(hwmon_humidity_attr_templates),
 };
 
 static int hwmon_num_channel_attrs(const struct hwmon_channel_info *info)
diff --git a/include/linux/hwmon.h b/include/linux/hwmon.h
index d7e432ef7c2a..57d92f1d779b 100644
--- a/include/linux/hwmon.h
+++ b/include/linux/hwmon.h
@@ -26,6 +26,7 @@ enum hwmon_sensor_types {
 	hwmon_curr,
 	hwmon_power,
 	hwmon_energy,
+	hwmon_humidity,
 };
 
 enum hwmon_chip_attributes {
@@ -216,6 +217,34 @@ enum hwmon_power_attributes {
 #define HWMON_P_MAX_ALARM		BIT(hwmon_power_max_alarm)
 #define HWMON_P_CRIT_ALARM		BIT(hwmon_power_crit_alarm)
 
+enum hwmon_energy_attributes {
+	hwmon_energy_input,
+	hwmon_energy_label,
+};
+
+#define HWMON_E_INPUT			BIT(hwmon_energy_input)
+#define HWMON_E_LABEL			BIT(hwmon_energy_label)
+
+enum hwmon_humidity_attributes {
+	hwmon_humidity_input,
+	hwmon_humidity_label,
+	hwmon_humidity_min,
+	hwmon_humidity_min_hyst,
+	hwmon_humidity_max,
+	hwmon_humidity_max_hyst,
+	hwmon_humidity_alarm,
+	hwmon_humidity_fault,
+};
+
+#define HWMON_H_INPUT			BIT(hwmon_humidity_input)
+#define HWMON_H_LABEL			BIT(hwmon_humidity_label)
+#define HWMON_H_MIN			BIT(hwmon_humidity_min)
+#define HWMON_H_MIN_HYST		BIT(hwmon_humidity_min_hyst)
+#define HWMON_H_MAX			BIT(hwmon_humidity_max)
+#define HWMON_H_MAX_HYST		BIT(hwmon_humidity_max_hyst)
+#define HWMON_H_ALARM			BIT(hwmon_humidity_alarm)
+#define HWMON_H_FAULT			BIT(hwmon_humidity_fault)
+
 /**
  * struct hwmon_ops - hwmon device operations
  * @is_visible: Callback to return attribute visibility. Mandatory.
-- 
2.5.0

^ permalink raw reply related	[flat|nested] 26+ messages in thread

* [PATCH v3 7/9] hwmon: (core) Add fan attribute support to new API
  2016-07-25  3:32 [PATCH v3 0/9] hwmon: New hwmon registration API Guenter Roeck
                   ` (5 preceding siblings ...)
  2016-07-25  3:32 ` [PATCH v3 6/9] hwmon: (core) Add energy and humidity " Guenter Roeck
@ 2016-07-25  3:32 ` Guenter Roeck
  2016-07-25  3:32   ` Guenter Roeck
  2016-07-25  3:32   ` Guenter Roeck
  8 siblings, 0 replies; 26+ messages in thread
From: Guenter Roeck @ 2016-07-25  3:32 UTC (permalink / raw)
  To: Jean Delvare
  Cc: Jonathan Cameron, Zhang Rui, Eduardo Valentin, Punit Agrawal,
	linux-pm, linux-iio, linux-hwmon, linux-kernel, Guenter Roeck

Acked-by: Punit Agrawal <punit.agrawal@arm.com>
Reviewed-by: Jonathan Cameron <jic23@kernel.org>
Signed-off-by: Guenter Roeck <linux@roeck-us.net>
---
v3: No change
v2: No change

 drivers/hwmon/hwmon.c | 16 ++++++++++++++++
 include/linux/hwmon.h | 27 +++++++++++++++++++++++++++
 2 files changed, 43 insertions(+)

diff --git a/drivers/hwmon/hwmon.c b/drivers/hwmon/hwmon.c
index 2dd4e0acfe12..4729d4742ab7 100644
--- a/drivers/hwmon/hwmon.c
+++ b/drivers/hwmon/hwmon.c
@@ -372,6 +372,20 @@ static const char * const hwmon_humidity_attr_templates[] = {
 	[hwmon_humidity_fault] = "humidity%d_fault",
 };
 
+static const char * const hwmon_fan_attr_templates[] = {
+	[hwmon_fan_input] = "fan%d_input",
+	[hwmon_fan_label] = "fan%d_label",
+	[hwmon_fan_min] = "fan%d_min",
+	[hwmon_fan_max] = "fan%d_max",
+	[hwmon_fan_div] = "fan%d_div",
+	[hwmon_fan_pulses] = "fan%d_pulses",
+	[hwmon_fan_target] = "fan%d_target",
+	[hwmon_fan_alarm] = "fan%d_alarm",
+	[hwmon_fan_min_alarm] = "fan%d_min_alarm",
+	[hwmon_fan_max_alarm] = "fan%d_max_alarm",
+	[hwmon_fan_fault] = "fan%d_fault",
+};
+
 static const char * const *__templates[] = {
 	[hwmon_chip] = hwmon_chip_attr_templates,
 	[hwmon_temp] = hwmon_temp_attr_templates,
@@ -380,6 +394,7 @@ static const char * const *__templates[] = {
 	[hwmon_power] = hwmon_power_attr_templates,
 	[hwmon_energy] = hwmon_energy_attr_templates,
 	[hwmon_humidity] = hwmon_humidity_attr_templates,
+	[hwmon_fan] = hwmon_fan_attr_templates,
 };
 
 static const int __templates_size[] = {
@@ -390,6 +405,7 @@ static const int __templates_size[] = {
 	[hwmon_power] = ARRAY_SIZE(hwmon_power_attr_templates),
 	[hwmon_energy] = ARRAY_SIZE(hwmon_energy_attr_templates),
 	[hwmon_humidity] = ARRAY_SIZE(hwmon_humidity_attr_templates),
+	[hwmon_fan] = ARRAY_SIZE(hwmon_fan_attr_templates),
 };
 
 static int hwmon_num_channel_attrs(const struct hwmon_channel_info *info)
diff --git a/include/linux/hwmon.h b/include/linux/hwmon.h
index 57d92f1d779b..74e89d45ff67 100644
--- a/include/linux/hwmon.h
+++ b/include/linux/hwmon.h
@@ -27,6 +27,7 @@ enum hwmon_sensor_types {
 	hwmon_power,
 	hwmon_energy,
 	hwmon_humidity,
+	hwmon_fan,
 };
 
 enum hwmon_chip_attributes {
@@ -245,6 +246,32 @@ enum hwmon_humidity_attributes {
 #define HWMON_H_ALARM			BIT(hwmon_humidity_alarm)
 #define HWMON_H_FAULT			BIT(hwmon_humidity_fault)
 
+enum hwmon_fan_attributes {
+	hwmon_fan_input,
+	hwmon_fan_label,
+	hwmon_fan_min,
+	hwmon_fan_max,
+	hwmon_fan_div,
+	hwmon_fan_pulses,
+	hwmon_fan_target,
+	hwmon_fan_alarm,
+	hwmon_fan_min_alarm,
+	hwmon_fan_max_alarm,
+	hwmon_fan_fault,
+};
+
+#define HWMON_F_INPUT			BIT(hwmon_fan_input)
+#define HWMON_F_LABEL			BIT(hwmon_fan_label)
+#define HWMON_F_MIN			BIT(hwmon_fan_min)
+#define HWMON_F_MAX			BIT(hwmon_fan_max)
+#define HWMON_F_DIV			BIT(hwmon_fan_div)
+#define HWMON_F_PULSES			BIT(hwmon_fan_pulses)
+#define HWMON_F_TARGET			BIT(hwmon_fan_target)
+#define HWMON_F_ALARM			BIT(hwmon_fan_alarm)
+#define HWMON_F_MIN_ALARM		BIT(hwmon_fan_min_alarm)
+#define HWMON_F_MAX_ALARM		BIT(hwmon_fan_max_alarm)
+#define HWMON_F_FAULT			BIT(hwmon_fan_fault)
+
 /**
  * struct hwmon_ops - hwmon device operations
  * @is_visible: Callback to return attribute visibility. Mandatory.
-- 
2.5.0

^ permalink raw reply related	[flat|nested] 26+ messages in thread

* [PATCH v3 8/9] hwmon: (core) Document new kernel API
@ 2016-07-25  3:32   ` Guenter Roeck
  0 siblings, 0 replies; 26+ messages in thread
From: Guenter Roeck @ 2016-07-25  3:32 UTC (permalink / raw)
  To: Jean Delvare
  Cc: Jonathan Cameron, Zhang Rui, Eduardo Valentin, Punit Agrawal,
	linux-pm, linux-iio, linux-hwmon, linux-kernel, Guenter Roeck

Describe the new registration API function as well as the data
structures it requires.

Acked-by: Punit Agrawal <punit.agrawal@arm.com>
Reviewed-by: Jonathan Cameron <jic23@kernel.org>
Signed-off-by: Guenter Roeck <linux@roeck-us.net>
---
v3: No change
v2: Fixed typos

 Documentation/hwmon/hwmon-kernel-api.txt | 229 ++++++++++++++++++++++++++++++-
 1 file changed, 227 insertions(+), 2 deletions(-)

diff --git a/Documentation/hwmon/hwmon-kernel-api.txt b/Documentation/hwmon/hwmon-kernel-api.txt
index 2ecdbfc85ecf..f60a29ce7592 100644
--- a/Documentation/hwmon/hwmon-kernel-api.txt
+++ b/Documentation/hwmon/hwmon-kernel-api.txt
@@ -34,6 +34,19 @@ devm_hwmon_device_register_with_groups(struct device *dev,
 				       const char *name, void *drvdata,
 				       const struct attribute_group **groups);
 
+struct device *
+hwmon_device_register_with_info(struct device *dev,
+				const char *name, void *drvdata,
+				const struct hwmon_chip_info *info,
+				const struct attribute_group **groups);
+
+struct device *
+devm_hwmon_device_register_with_info(struct device *dev,
+				     const char *name,
+				     void *drvdata,
+				     const struct hwmon_chip_info *info,
+				     const struct attribute_group **groups);
+
 void hwmon_device_unregister(struct device *dev);
 void devm_hwmon_device_unregister(struct device *dev);
 
@@ -60,15 +73,227 @@ devm_hwmon_device_register_with_groups is similar to
 hwmon_device_register_with_groups. However, it is device managed, meaning the
 hwmon device does not have to be removed explicitly by the removal function.
 
+hwmon_device_register_with_info is the most comprehensive and preferred means
+to register a hardware monitoring device. It creates the standard sysfs
+attributes in the hardware monitoring core, letting the driver focus on reading
+from and writing to the chip instead of having to bother with sysfs attributes.
+Its parameters are described in more detail below.
+
+devm_hwmon_device_register_with_info is similar to
+hwmon_device_register_with_info. However, it is device managed, meaning the
+hwmon device does not have to be removed explicitly by the removal function.
+
 hwmon_device_unregister deregisters a registered hardware monitoring device.
 The parameter of this function is the pointer to the registered hardware
 monitoring device structure. This function must be called from the driver
 remove function if the hardware monitoring device was registered with
-hwmon_device_register or with hwmon_device_register_with_groups.
+hwmon_device_register, hwmon_device_register_with_groups, or
+hwmon_device_register_with_info.
 
 devm_hwmon_device_unregister does not normally have to be called. It is only
 needed for error handling, and only needed if the driver probe fails after
-the call to devm_hwmon_device_register_with_groups.
+the call to devm_hwmon_device_register_with_groups and if the automatic
+(device managed) removal would be too late.
+
+Using devm_hwmon_device_register_with_info()
+--------------------------------------------
+
+hwmon_device_register_with_info() registers a hardware monitoring device.
+The parameters to this function are
+
+struct device *dev	Pointer to parent device
+const char *name	Device name
+void *drvdata		Driver private data
+const struct hwmon_chip_info *info
+			Pointer to chip description.
+const struct attribute_group **groups
+			Null-terminated list of additional sysfs attribute
+			groups.
+
+This function returns a pointer to the created hardware monitoring device
+on success and a negative error code for failure.
+
+The hwmon_chip_info structure looks as follows.
+
+struct hwmon_chip_info {
+	const struct hwmon_ops *ops;
+	const struct hwmon_channel_info **info;
+};
+
+It contains the following fields:
+
+* ops:	Pointer to device operations.
+* info: NULL-terminated list of device channel descriptors.
+
+The list of hwmon operations is defined as:
+
+struct hwmon_ops {
+	umode_t (*is_visible)(const void *, enum hwmon_sensor_types type,
+			      u32 attr, int);
+	int (*read)(struct device *, enum hwmon_sensor_types type,
+		    u32 attr, int, long *);
+	int (*write)(struct device *, enum hwmon_sensor_types type,
+		     u32 attr, int, long);
+};
+
+It defines the following operations.
+
+* is_visible: Pointer to a function to return the file mode for each supported
+  attribute. This function is mandatory.
+
+* read: Pointer to a function for reading a value from the chip. This function
+  is optional, but must be provided if any readable attributes exist.
+
+* write: Pointer to a function for writing a value to the chip. This function is
+  optional, but must be provided if any writeable attributes exist.
+
+Each sensor channel is described with struct hwmon_channel_info, which is
+defined as follows.
+
+struct hwmon_channel_info {
+	enum hwmon_sensor_types type;
+	u32 *config;
+};
+
+It contains following fields:
+
+* type: The hardware monitoring sensor type.
+  Supported sensor types are
+  * hwmon_chip		A virtual sensor type, used to describe attributes
+			which apply to the entire chip.
+  * hwmon_temp		Temperature sensor
+  * hwmon_in		Voltage sensor
+  * hwmon_curr		Current sensor
+  * hwmon_power		Power sensor
+  * hwmon_energy	Energy sensor
+  * hwmon_humidity	Humidity sensor
+  * hwmon_fan		Fan speed sensor
+
+* config: Pointer to a 0-terminated list of configuration values for each
+  sensor of the given type. Each value is a combination of bit values
+  describing the attributes supposed by a single sensor.
+
+As an example, here is the complete description file for a LM75 compatible
+sensor chip. The chip has a single temperature sensor. The driver wants to
+register with the thermal subsystem (HWMON_C_REGISTER_TZ), and it supports
+the update_interval attribute (HWMON_C_UPDATE_INTERVAL). The chip supports
+reading the temperature (HWMON_T_INPUT), it has a maximum temperature
+register (HWMON_T_MAX) as well as a maximum temperature hysteresis register
+(HWMON_T_MAX_HYST).
+
+static const u32 lm75_chip_config[] = {
+	HWMON_C_REGISTER_TZ | HWMON_C_UPDATE_INTERVAL,
+	0
+};
+
+static const struct hwmon_channel_info lm75_chip = {
+	.type = hwmon_chip,
+	.config = lm75_chip_config,
+};
+
+static const u32 lm75_temp_config[] = {
+	HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_MAX_HYST,
+	0
+};
+
+static const struct hwmon_channel_info lm75_temp = {
+	.type = hwmon_temp,
+	.config = lm75_temp_config,
+};
+
+static const struct hwmon_channel_info *lm75_info[] = {
+	&lm75_chip,
+	&lm75_temp,
+	NULL
+};
+
+static const struct hwmon_ops lm75_hwmon_ops = {
+	.is_visible = lm75_is_visible,
+	.read = lm75_read,
+	.write = lm75_write,
+};
+
+static const struct hwmon_chip_info lm75_chip_info = {
+	.ops = &lm75_hwmon_ops,
+	.info = lm75_info,
+};
+
+A complete list of bit values indicating individual attribute support
+is defined in include/linux/hwmon.h. Definition prefixes are as follows.
+
+HWMON_C_xxxx	Chip attributes, for use with hwmon_chip.
+HWMON_T_xxxx	Temperature attributes, for use with hwmon_temp.
+HWMON_I_xxxx	Voltage attributes, for use with hwmon_in.
+HWMON_C_xxxx	Current attributes, for use with hwmon_curr.
+		Notice the prefix overlap with chip attributes.
+HWMON_P_xxxx	Power attributes, for use with hwmon_power.
+HWMON_E_xxxx	Energy attributes, for use with hwmon_energy.
+HWMON_H_xxxx	Humidity attributes, for use with hwmon_humidity.
+HWMON_F_xxxx	Fan speed attributes, for use with hwmon_fan.
+
+Driver callback functions
+-------------------------
+
+Each driver provides is_visible, read, and write functions. Parameters
+and return values for those functions are as follows.
+
+umode_t is_visible_func(const void *data, enum hwmon_sensor_types type,
+			u32 attr, int channel)
+
+Parameters:
+	data:	Pointer to device private data structure.
+	type:	The sensor type.
+	attr:	Attribute identifier associated with a specific attribute.
+		For example, the attribute value for HWMON_T_INPUT would be
+		hwmon_temp_input. For complete mappings of bit fields to
+		attribute values please see include/linux/hwmon.h.
+	channel:The sensor channel number.
+
+Return value:
+	The file mode for this attribute. Typically, this will be 0 (the
+	attribute will not be created), S_IRUGO, or 'S_IRUGO | S_IWUSR'.
+
+int read_func(struct device *dev, enum hwmon_sensor_types type,
+	      u32 attr, int channel, long *val)
+
+Parameters:
+	dev:	Pointer to the hardware monitoring device.
+	type:	The sensor type.
+	attr:	Attribute identifier associated with a specific attribute.
+		For example, the attribute value for HWMON_T_INPUT would be
+		hwmon_temp_input. For complete mappings please see
+		include/linux/hwmon.h.
+	channel:The sensor channel number.
+	val:	Pointer to attribute value.
+
+Return value:
+	0 on success, a negative error number otherwise.
+
+int write_func(struct device *dev, enum hwmon_sensor_types type,
+	       u32 attr, int channel, long val)
+
+Parameters:
+	dev:	Pointer to the hardware monitoring device.
+	type:	The sensor type.
+	attr:	Attribute identifier associated with a specific attribute.
+		For example, the attribute value for HWMON_T_INPUT would be
+		hwmon_temp_input. For complete mappings please see
+		include/linux/hwmon.h.
+	channel:The sensor channel number.
+	val:	The value to write to the chip.
+
+Return value:
+	0 on success, a negative error number otherwise.
+
+
+Driver-provided sysfs attributes
+--------------------------------
+
+If the hardware monitoring device is registered with
+hwmon_device_register_with_info or devm_hwmon_device_register_with_info,
+it is most likely not necessary to provide sysfs attributes. Only non-standard
+sysfs attributes need to be provided when one of those registration functions
+is used.
 
 The header file linux/hwmon-sysfs.h provides a number of useful macros to
 declare and use hardware monitoring sysfs attributes.
-- 
2.5.0

^ permalink raw reply related	[flat|nested] 26+ messages in thread

* [PATCH v3 8/9] hwmon: (core) Document new kernel API
@ 2016-07-25  3:32   ` Guenter Roeck
  0 siblings, 0 replies; 26+ messages in thread
From: Guenter Roeck @ 2016-07-25  3:32 UTC (permalink / raw)
  To: Jean Delvare
  Cc: Jonathan Cameron, Zhang Rui, Eduardo Valentin, Punit Agrawal,
	linux-pm-u79uwXL29TY76Z2rM5mHXA,
	linux-iio-u79uwXL29TY76Z2rM5mHXA,
	linux-hwmon-u79uwXL29TY76Z2rM5mHXA,
	linux-kernel-u79uwXL29TY76Z2rM5mHXA, Guenter Roeck

Describe the new registration API function as well as the data
structures it requires.

Acked-by: Punit Agrawal <punit.agrawal-5wv7dgnIgG8@public.gmane.org>
Reviewed-by: Jonathan Cameron <jic23-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org>
Signed-off-by: Guenter Roeck <linux-0h96xk9xTtrk1uMJSBkQmQ@public.gmane.org>
---
v3: No change
v2: Fixed typos

 Documentation/hwmon/hwmon-kernel-api.txt | 229 ++++++++++++++++++++++++++++++-
 1 file changed, 227 insertions(+), 2 deletions(-)

diff --git a/Documentation/hwmon/hwmon-kernel-api.txt b/Documentation/hwmon/hwmon-kernel-api.txt
index 2ecdbfc85ecf..f60a29ce7592 100644
--- a/Documentation/hwmon/hwmon-kernel-api.txt
+++ b/Documentation/hwmon/hwmon-kernel-api.txt
@@ -34,6 +34,19 @@ devm_hwmon_device_register_with_groups(struct device *dev,
 				       const char *name, void *drvdata,
 				       const struct attribute_group **groups);
 
+struct device *
+hwmon_device_register_with_info(struct device *dev,
+				const char *name, void *drvdata,
+				const struct hwmon_chip_info *info,
+				const struct attribute_group **groups);
+
+struct device *
+devm_hwmon_device_register_with_info(struct device *dev,
+				     const char *name,
+				     void *drvdata,
+				     const struct hwmon_chip_info *info,
+				     const struct attribute_group **groups);
+
 void hwmon_device_unregister(struct device *dev);
 void devm_hwmon_device_unregister(struct device *dev);
 
@@ -60,15 +73,227 @@ devm_hwmon_device_register_with_groups is similar to
 hwmon_device_register_with_groups. However, it is device managed, meaning the
 hwmon device does not have to be removed explicitly by the removal function.
 
+hwmon_device_register_with_info is the most comprehensive and preferred means
+to register a hardware monitoring device. It creates the standard sysfs
+attributes in the hardware monitoring core, letting the driver focus on reading
+from and writing to the chip instead of having to bother with sysfs attributes.
+Its parameters are described in more detail below.
+
+devm_hwmon_device_register_with_info is similar to
+hwmon_device_register_with_info. However, it is device managed, meaning the
+hwmon device does not have to be removed explicitly by the removal function.
+
 hwmon_device_unregister deregisters a registered hardware monitoring device.
 The parameter of this function is the pointer to the registered hardware
 monitoring device structure. This function must be called from the driver
 remove function if the hardware monitoring device was registered with
-hwmon_device_register or with hwmon_device_register_with_groups.
+hwmon_device_register, hwmon_device_register_with_groups, or
+hwmon_device_register_with_info.
 
 devm_hwmon_device_unregister does not normally have to be called. It is only
 needed for error handling, and only needed if the driver probe fails after
-the call to devm_hwmon_device_register_with_groups.
+the call to devm_hwmon_device_register_with_groups and if the automatic
+(device managed) removal would be too late.
+
+Using devm_hwmon_device_register_with_info()
+--------------------------------------------
+
+hwmon_device_register_with_info() registers a hardware monitoring device.
+The parameters to this function are
+
+struct device *dev	Pointer to parent device
+const char *name	Device name
+void *drvdata		Driver private data
+const struct hwmon_chip_info *info
+			Pointer to chip description.
+const struct attribute_group **groups
+			Null-terminated list of additional sysfs attribute
+			groups.
+
+This function returns a pointer to the created hardware monitoring device
+on success and a negative error code for failure.
+
+The hwmon_chip_info structure looks as follows.
+
+struct hwmon_chip_info {
+	const struct hwmon_ops *ops;
+	const struct hwmon_channel_info **info;
+};
+
+It contains the following fields:
+
+* ops:	Pointer to device operations.
+* info: NULL-terminated list of device channel descriptors.
+
+The list of hwmon operations is defined as:
+
+struct hwmon_ops {
+	umode_t (*is_visible)(const void *, enum hwmon_sensor_types type,
+			      u32 attr, int);
+	int (*read)(struct device *, enum hwmon_sensor_types type,
+		    u32 attr, int, long *);
+	int (*write)(struct device *, enum hwmon_sensor_types type,
+		     u32 attr, int, long);
+};
+
+It defines the following operations.
+
+* is_visible: Pointer to a function to return the file mode for each supported
+  attribute. This function is mandatory.
+
+* read: Pointer to a function for reading a value from the chip. This function
+  is optional, but must be provided if any readable attributes exist.
+
+* write: Pointer to a function for writing a value to the chip. This function is
+  optional, but must be provided if any writeable attributes exist.
+
+Each sensor channel is described with struct hwmon_channel_info, which is
+defined as follows.
+
+struct hwmon_channel_info {
+	enum hwmon_sensor_types type;
+	u32 *config;
+};
+
+It contains following fields:
+
+* type: The hardware monitoring sensor type.
+  Supported sensor types are
+  * hwmon_chip		A virtual sensor type, used to describe attributes
+			which apply to the entire chip.
+  * hwmon_temp		Temperature sensor
+  * hwmon_in		Voltage sensor
+  * hwmon_curr		Current sensor
+  * hwmon_power		Power sensor
+  * hwmon_energy	Energy sensor
+  * hwmon_humidity	Humidity sensor
+  * hwmon_fan		Fan speed sensor
+
+* config: Pointer to a 0-terminated list of configuration values for each
+  sensor of the given type. Each value is a combination of bit values
+  describing the attributes supposed by a single sensor.
+
+As an example, here is the complete description file for a LM75 compatible
+sensor chip. The chip has a single temperature sensor. The driver wants to
+register with the thermal subsystem (HWMON_C_REGISTER_TZ), and it supports
+the update_interval attribute (HWMON_C_UPDATE_INTERVAL). The chip supports
+reading the temperature (HWMON_T_INPUT), it has a maximum temperature
+register (HWMON_T_MAX) as well as a maximum temperature hysteresis register
+(HWMON_T_MAX_HYST).
+
+static const u32 lm75_chip_config[] = {
+	HWMON_C_REGISTER_TZ | HWMON_C_UPDATE_INTERVAL,
+	0
+};
+
+static const struct hwmon_channel_info lm75_chip = {
+	.type = hwmon_chip,
+	.config = lm75_chip_config,
+};
+
+static const u32 lm75_temp_config[] = {
+	HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_MAX_HYST,
+	0
+};
+
+static const struct hwmon_channel_info lm75_temp = {
+	.type = hwmon_temp,
+	.config = lm75_temp_config,
+};
+
+static const struct hwmon_channel_info *lm75_info[] = {
+	&lm75_chip,
+	&lm75_temp,
+	NULL
+};
+
+static const struct hwmon_ops lm75_hwmon_ops = {
+	.is_visible = lm75_is_visible,
+	.read = lm75_read,
+	.write = lm75_write,
+};
+
+static const struct hwmon_chip_info lm75_chip_info = {
+	.ops = &lm75_hwmon_ops,
+	.info = lm75_info,
+};
+
+A complete list of bit values indicating individual attribute support
+is defined in include/linux/hwmon.h. Definition prefixes are as follows.
+
+HWMON_C_xxxx	Chip attributes, for use with hwmon_chip.
+HWMON_T_xxxx	Temperature attributes, for use with hwmon_temp.
+HWMON_I_xxxx	Voltage attributes, for use with hwmon_in.
+HWMON_C_xxxx	Current attributes, for use with hwmon_curr.
+		Notice the prefix overlap with chip attributes.
+HWMON_P_xxxx	Power attributes, for use with hwmon_power.
+HWMON_E_xxxx	Energy attributes, for use with hwmon_energy.
+HWMON_H_xxxx	Humidity attributes, for use with hwmon_humidity.
+HWMON_F_xxxx	Fan speed attributes, for use with hwmon_fan.
+
+Driver callback functions
+-------------------------
+
+Each driver provides is_visible, read, and write functions. Parameters
+and return values for those functions are as follows.
+
+umode_t is_visible_func(const void *data, enum hwmon_sensor_types type,
+			u32 attr, int channel)
+
+Parameters:
+	data:	Pointer to device private data structure.
+	type:	The sensor type.
+	attr:	Attribute identifier associated with a specific attribute.
+		For example, the attribute value for HWMON_T_INPUT would be
+		hwmon_temp_input. For complete mappings of bit fields to
+		attribute values please see include/linux/hwmon.h.
+	channel:The sensor channel number.
+
+Return value:
+	The file mode for this attribute. Typically, this will be 0 (the
+	attribute will not be created), S_IRUGO, or 'S_IRUGO | S_IWUSR'.
+
+int read_func(struct device *dev, enum hwmon_sensor_types type,
+	      u32 attr, int channel, long *val)
+
+Parameters:
+	dev:	Pointer to the hardware monitoring device.
+	type:	The sensor type.
+	attr:	Attribute identifier associated with a specific attribute.
+		For example, the attribute value for HWMON_T_INPUT would be
+		hwmon_temp_input. For complete mappings please see
+		include/linux/hwmon.h.
+	channel:The sensor channel number.
+	val:	Pointer to attribute value.
+
+Return value:
+	0 on success, a negative error number otherwise.
+
+int write_func(struct device *dev, enum hwmon_sensor_types type,
+	       u32 attr, int channel, long val)
+
+Parameters:
+	dev:	Pointer to the hardware monitoring device.
+	type:	The sensor type.
+	attr:	Attribute identifier associated with a specific attribute.
+		For example, the attribute value for HWMON_T_INPUT would be
+		hwmon_temp_input. For complete mappings please see
+		include/linux/hwmon.h.
+	channel:The sensor channel number.
+	val:	The value to write to the chip.
+
+Return value:
+	0 on success, a negative error number otherwise.
+
+
+Driver-provided sysfs attributes
+--------------------------------
+
+If the hardware monitoring device is registered with
+hwmon_device_register_with_info or devm_hwmon_device_register_with_info,
+it is most likely not necessary to provide sysfs attributes. Only non-standard
+sysfs attributes need to be provided when one of those registration functions
+is used.
 
 The header file linux/hwmon-sysfs.h provides a number of useful macros to
 declare and use hardware monitoring sysfs attributes.
-- 
2.5.0

^ permalink raw reply related	[flat|nested] 26+ messages in thread

* [PATCH v3 9/9] hwmon: (core) Add basic pwm attribute support to new API
@ 2016-07-25  3:32   ` Guenter Roeck
  0 siblings, 0 replies; 26+ messages in thread
From: Guenter Roeck @ 2016-07-25  3:32 UTC (permalink / raw)
  To: Jean Delvare
  Cc: Jonathan Cameron, Zhang Rui, Eduardo Valentin, Punit Agrawal,
	linux-pm, linux-iio, linux-hwmon, linux-kernel, Guenter Roeck

Add basic pwm attribute support (no auto attributes) to new API.

Reviewed-by: Jonathan Cameron <jic23@kernel.org>
Signed-off-by: Guenter Roeck <linux@roeck-us.net>
---
v2: No change
v2: Added patch

 Documentation/hwmon/hwmon-kernel-api.txt |  2 ++
 drivers/hwmon/hwmon.c                    |  9 +++++++++
 include/linux/hwmon.h                    | 13 +++++++++++++
 3 files changed, 24 insertions(+)

diff --git a/Documentation/hwmon/hwmon-kernel-api.txt b/Documentation/hwmon/hwmon-kernel-api.txt
index f60a29ce7592..ef9d74947f5c 100644
--- a/Documentation/hwmon/hwmon-kernel-api.txt
+++ b/Documentation/hwmon/hwmon-kernel-api.txt
@@ -168,6 +168,7 @@ It contains following fields:
   * hwmon_energy	Energy sensor
   * hwmon_humidity	Humidity sensor
   * hwmon_fan		Fan speed sensor
+  * hwmon_pwm		PWM control
 
 * config: Pointer to a 0-terminated list of configuration values for each
   sensor of the given type. Each value is a combination of bit values
@@ -230,6 +231,7 @@ HWMON_P_xxxx	Power attributes, for use with hwmon_power.
 HWMON_E_xxxx	Energy attributes, for use with hwmon_energy.
 HWMON_H_xxxx	Humidity attributes, for use with hwmon_humidity.
 HWMON_F_xxxx	Fan speed attributes, for use with hwmon_fan.
+HWMON_PWM_xxxx	PWM control attributes, for use with hwmon_pwm.
 
 Driver callback functions
 -------------------------
diff --git a/drivers/hwmon/hwmon.c b/drivers/hwmon/hwmon.c
index 4729d4742ab7..fade170977e8 100644
--- a/drivers/hwmon/hwmon.c
+++ b/drivers/hwmon/hwmon.c
@@ -386,6 +386,13 @@ static const char * const hwmon_fan_attr_templates[] = {
 	[hwmon_fan_fault] = "fan%d_fault",
 };
 
+static const char * const hwmon_pwm_attr_templates[] = {
+	[hwmon_pwm_input] = "pwm%d",
+	[hwmon_pwm_enable] = "pwm%d_enable",
+	[hwmon_pwm_mode] = "pwm%d_mode",
+	[hwmon_pwm_freq] = "pwm%d_freq",
+};
+
 static const char * const *__templates[] = {
 	[hwmon_chip] = hwmon_chip_attr_templates,
 	[hwmon_temp] = hwmon_temp_attr_templates,
@@ -395,6 +402,7 @@ static const char * const *__templates[] = {
 	[hwmon_energy] = hwmon_energy_attr_templates,
 	[hwmon_humidity] = hwmon_humidity_attr_templates,
 	[hwmon_fan] = hwmon_fan_attr_templates,
+	[hwmon_pwm] = hwmon_pwm_attr_templates,
 };
 
 static const int __templates_size[] = {
@@ -406,6 +414,7 @@ static const int __templates_size[] = {
 	[hwmon_energy] = ARRAY_SIZE(hwmon_energy_attr_templates),
 	[hwmon_humidity] = ARRAY_SIZE(hwmon_humidity_attr_templates),
 	[hwmon_fan] = ARRAY_SIZE(hwmon_fan_attr_templates),
+	[hwmon_pwm] = ARRAY_SIZE(hwmon_pwm_attr_templates),
 };
 
 static int hwmon_num_channel_attrs(const struct hwmon_channel_info *info)
diff --git a/include/linux/hwmon.h b/include/linux/hwmon.h
index 74e89d45ff67..9d2f8bde7d12 100644
--- a/include/linux/hwmon.h
+++ b/include/linux/hwmon.h
@@ -28,6 +28,7 @@ enum hwmon_sensor_types {
 	hwmon_energy,
 	hwmon_humidity,
 	hwmon_fan,
+	hwmon_pwm,
 };
 
 enum hwmon_chip_attributes {
@@ -272,6 +273,18 @@ enum hwmon_fan_attributes {
 #define HWMON_F_MAX_ALARM		BIT(hwmon_fan_max_alarm)
 #define HWMON_F_FAULT			BIT(hwmon_fan_fault)
 
+enum hwmon_pwm_attributes {
+	hwmon_pwm_input,
+	hwmon_pwm_enable,
+	hwmon_pwm_mode,
+	hwmon_pwm_freq,
+};
+
+#define HWMON_PWM_INPUT			BIT(hwmon_pwm_input)
+#define HWMON_PWM_ENABLE		BIT(hwmon_pwm_enable)
+#define HWMON_PWM_MODE			BIT(hwmon_pwm_mode)
+#define HWMON_PWM_FREQ			BIT(hwmon_pwm_freq)
+
 /**
  * struct hwmon_ops - hwmon device operations
  * @is_visible: Callback to return attribute visibility. Mandatory.
-- 
2.5.0


^ permalink raw reply related	[flat|nested] 26+ messages in thread

* [PATCH v3 9/9] hwmon: (core) Add basic pwm attribute support to new API
@ 2016-07-25  3:32   ` Guenter Roeck
  0 siblings, 0 replies; 26+ messages in thread
From: Guenter Roeck @ 2016-07-25  3:32 UTC (permalink / raw)
  To: Jean Delvare
  Cc: Jonathan Cameron, Zhang Rui, Eduardo Valentin, Punit Agrawal,
	linux-pm-u79uwXL29TY76Z2rM5mHXA,
	linux-iio-u79uwXL29TY76Z2rM5mHXA,
	linux-hwmon-u79uwXL29TY76Z2rM5mHXA,
	linux-kernel-u79uwXL29TY76Z2rM5mHXA, Guenter Roeck

Add basic pwm attribute support (no auto attributes) to new API.

Reviewed-by: Jonathan Cameron <jic23-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org>
Signed-off-by: Guenter Roeck <linux-0h96xk9xTtrk1uMJSBkQmQ@public.gmane.org>
---
v2: No change
v2: Added patch

 Documentation/hwmon/hwmon-kernel-api.txt |  2 ++
 drivers/hwmon/hwmon.c                    |  9 +++++++++
 include/linux/hwmon.h                    | 13 +++++++++++++
 3 files changed, 24 insertions(+)

diff --git a/Documentation/hwmon/hwmon-kernel-api.txt b/Documentation/hwmon/hwmon-kernel-api.txt
index f60a29ce7592..ef9d74947f5c 100644
--- a/Documentation/hwmon/hwmon-kernel-api.txt
+++ b/Documentation/hwmon/hwmon-kernel-api.txt
@@ -168,6 +168,7 @@ It contains following fields:
   * hwmon_energy	Energy sensor
   * hwmon_humidity	Humidity sensor
   * hwmon_fan		Fan speed sensor
+  * hwmon_pwm		PWM control
 
 * config: Pointer to a 0-terminated list of configuration values for each
   sensor of the given type. Each value is a combination of bit values
@@ -230,6 +231,7 @@ HWMON_P_xxxx	Power attributes, for use with hwmon_power.
 HWMON_E_xxxx	Energy attributes, for use with hwmon_energy.
 HWMON_H_xxxx	Humidity attributes, for use with hwmon_humidity.
 HWMON_F_xxxx	Fan speed attributes, for use with hwmon_fan.
+HWMON_PWM_xxxx	PWM control attributes, for use with hwmon_pwm.
 
 Driver callback functions
 -------------------------
diff --git a/drivers/hwmon/hwmon.c b/drivers/hwmon/hwmon.c
index 4729d4742ab7..fade170977e8 100644
--- a/drivers/hwmon/hwmon.c
+++ b/drivers/hwmon/hwmon.c
@@ -386,6 +386,13 @@ static const char * const hwmon_fan_attr_templates[] = {
 	[hwmon_fan_fault] = "fan%d_fault",
 };
 
+static const char * const hwmon_pwm_attr_templates[] = {
+	[hwmon_pwm_input] = "pwm%d",
+	[hwmon_pwm_enable] = "pwm%d_enable",
+	[hwmon_pwm_mode] = "pwm%d_mode",
+	[hwmon_pwm_freq] = "pwm%d_freq",
+};
+
 static const char * const *__templates[] = {
 	[hwmon_chip] = hwmon_chip_attr_templates,
 	[hwmon_temp] = hwmon_temp_attr_templates,
@@ -395,6 +402,7 @@ static const char * const *__templates[] = {
 	[hwmon_energy] = hwmon_energy_attr_templates,
 	[hwmon_humidity] = hwmon_humidity_attr_templates,
 	[hwmon_fan] = hwmon_fan_attr_templates,
+	[hwmon_pwm] = hwmon_pwm_attr_templates,
 };
 
 static const int __templates_size[] = {
@@ -406,6 +414,7 @@ static const int __templates_size[] = {
 	[hwmon_energy] = ARRAY_SIZE(hwmon_energy_attr_templates),
 	[hwmon_humidity] = ARRAY_SIZE(hwmon_humidity_attr_templates),
 	[hwmon_fan] = ARRAY_SIZE(hwmon_fan_attr_templates),
+	[hwmon_pwm] = ARRAY_SIZE(hwmon_pwm_attr_templates),
 };
 
 static int hwmon_num_channel_attrs(const struct hwmon_channel_info *info)
diff --git a/include/linux/hwmon.h b/include/linux/hwmon.h
index 74e89d45ff67..9d2f8bde7d12 100644
--- a/include/linux/hwmon.h
+++ b/include/linux/hwmon.h
@@ -28,6 +28,7 @@ enum hwmon_sensor_types {
 	hwmon_energy,
 	hwmon_humidity,
 	hwmon_fan,
+	hwmon_pwm,
 };
 
 enum hwmon_chip_attributes {
@@ -272,6 +273,18 @@ enum hwmon_fan_attributes {
 #define HWMON_F_MAX_ALARM		BIT(hwmon_fan_max_alarm)
 #define HWMON_F_FAULT			BIT(hwmon_fan_fault)
 
+enum hwmon_pwm_attributes {
+	hwmon_pwm_input,
+	hwmon_pwm_enable,
+	hwmon_pwm_mode,
+	hwmon_pwm_freq,
+};
+
+#define HWMON_PWM_INPUT			BIT(hwmon_pwm_input)
+#define HWMON_PWM_ENABLE		BIT(hwmon_pwm_enable)
+#define HWMON_PWM_MODE			BIT(hwmon_pwm_mode)
+#define HWMON_PWM_FREQ			BIT(hwmon_pwm_freq)
+
 /**
  * struct hwmon_ops - hwmon device operations
  * @is_visible: Callback to return attribute visibility. Mandatory.
-- 
2.5.0

^ permalink raw reply related	[flat|nested] 26+ messages in thread

* Re: [PATCH v3 2/9] hwmon: (core) New hwmon registration API
  2016-07-25  3:32 ` [PATCH v3 2/9] hwmon: (core) New hwmon registration API Guenter Roeck
@ 2016-08-12  8:16     ` Keerthy
  2016-10-04  8:45     ` Jean Delvare
  1 sibling, 0 replies; 26+ messages in thread
From: Keerthy @ 2016-08-12  8:16 UTC (permalink / raw)
  To: Guenter Roeck, Jean Delvare
  Cc: Jonathan Cameron, Zhang Rui, Eduardo Valentin, Punit Agrawal,
	linux-pm, linux-iio, linux-hwmon, linux-kernel, linux-omap, R,
	Vignesh

On Monday 25 July 2016 09:02 AM, Guenter Roeck wrote:
> Up to now, each hwmon driver has to implement its own sysfs attributes.
> This requires a lot of template code, and distracts from the driver's core
> function to read and write chip registers.
>
> To be able to reduce driver complexity, move sensor attribute handling
> and thermal zone registration into hwmon core. By using the new API,
> driver code and data size is typically reduced by 20-70%, depending
> on driver complexity and the number of sysfs attributes supported.
>
> With this patch, the new API only supports thermal sensors. Support for
> other sensor types will be added with subsequent patches.
>
Hi Guenter,

Seems like this patch introduces a cycling dependency between
thermal_sys and hwmon:

DEPMOD 4.8.0-rc1-next-20160811-00001-g2f84cf7854e7
depmod: ERROR: Found 2 modules in dependency cycles!
depmod: ERROR: Cycle detected: hwmon -> thermal_sys -> hwmon

As reported by R, Vignesh <vigneshr@ti.com>.

- Keerthy

> Acked-by: Punit Agrawal <punit.agrawal@arm.com>
> Reviewed-by: Jonathan Cameron <jic23@kernel.org>
> Signed-off-by: Guenter Roeck <linux@roeck-us.net>
> ---
> v3:
> - Thermal registration depends on IS_REACHABLE(CONFIG_THERMAL) and
>    CONFIG_THERMAL_OF.
> v2:
> - Document callback function parameters of struct hwmon_ops in
>    include/linux/hwmon.h.
> - Clarify that the is_visible() callback is mandatory.
> - Initialize device attribute read/write callback functions unconditionally.
> - If an attribute has no template string, treat it as invisible, not as
>    error. Affected are virtual attributes such as HWMON_C_REGISTER_TZ.
> - Added newline to improve readability.
>
> Review comments not addressed:
> - Stick with u32 for attribute masks. We could use u64, but it is currently
>    not needed, and changing it later would be straightforward.
> - Do not use for_each_set_bit() to walk attribute masks.
>    for_each_set_bit() expects a pointer to an unsigned long as argument,
>    which would make it difficult to switch to u64 attribute masks if/when
>    needed.
>
>   drivers/hwmon/hwmon.c | 485 +++++++++++++++++++++++++++++++++++++++++++++++---
>   include/linux/hwmon.h | 148 +++++++++++++++
>   2 files changed, 606 insertions(+), 27 deletions(-)
>
> diff --git a/drivers/hwmon/hwmon.c b/drivers/hwmon/hwmon.c
> index 649a68d119b4..3e4cc442a089 100644
> --- a/drivers/hwmon/hwmon.c
> +++ b/drivers/hwmon/hwmon.c
> @@ -12,6 +12,7 @@
>
>   #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
>
> +#include <linux/bitops.h>
>   #include <linux/device.h>
>   #include <linux/err.h>
>   #include <linux/gfp.h>
> @@ -21,6 +22,7 @@
>   #include <linux/pci.h>
>   #include <linux/slab.h>
>   #include <linux/string.h>
> +#include <linux/thermal.h>
>
>   #define HWMON_ID_PREFIX "hwmon"
>   #define HWMON_ID_FORMAT HWMON_ID_PREFIX "%d"
> @@ -28,9 +30,35 @@
>   struct hwmon_device {
>   	const char *name;
>   	struct device dev;
> +	const struct hwmon_chip_info *chip;
> +
> +	struct attribute_group group;
> +	const struct attribute_group **groups;
>   };
> +
>   #define to_hwmon_device(d) container_of(d, struct hwmon_device, dev)
>
> +struct hwmon_device_attribute {
> +	struct device_attribute dev_attr;
> +	const struct hwmon_ops *ops;
> +	enum hwmon_sensor_types type;
> +	u32 attr;
> +	int index;
> +};
> +
> +#define to_hwmon_attr(d) \
> +	container_of(d, struct hwmon_device_attribute, dev_attr)
> +
> +/*
> + * Thermal zone information
> + * In addition to the reference to the hwmon device,
> + * also provides the sensor index.
> + */
> +struct hwmon_thermal_data {
> +	struct hwmon_device *hwdev;	/* Reference to hwmon device */
> +	int index;			/* sensor index */
> +};
> +
>   static ssize_t
>   show_name(struct device *dev, struct device_attribute *attr, char *buf)
>   {
> @@ -78,25 +106,286 @@ static struct class hwmon_class = {
>
>   static DEFINE_IDA(hwmon_ida);
>
> -/**
> - * hwmon_device_register_with_groups - register w/ hwmon
> - * @dev: the parent device
> - * @name: hwmon name attribute
> - * @drvdata: driver data to attach to created device
> - * @groups: List of attribute groups to create
> - *
> - * hwmon_device_unregister() must be called when the device is no
> - * longer needed.
> - *
> - * Returns the pointer to the new device.
> - */
> -struct device *
> -hwmon_device_register_with_groups(struct device *dev, const char *name,
> -				  void *drvdata,
> -				  const struct attribute_group **groups)
> +/* Thermal zone handling */
> +
> +#if IS_REACHABLE(CONFIG_THERMAL) && defined(CONFIG_THERMAL_OF)
> +static int hwmon_thermal_get_temp(void *data, int *temp)
> +{
> +	struct hwmon_thermal_data *tdata = data;
> +	struct hwmon_device *hwdev = tdata->hwdev;
> +	int ret;
> +	long t;
> +
> +	ret = hwdev->chip->ops->read(&hwdev->dev, hwmon_temp, hwmon_temp_input,
> +				     tdata->index, &t);
> +	if (ret < 0)
> +		return ret;
> +
> +	*temp = t;
> +
> +	return 0;
> +}
> +
> +static struct thermal_zone_of_device_ops hwmon_thermal_ops = {
> +	.get_temp = hwmon_thermal_get_temp,
> +};
> +
> +static int hwmon_thermal_add_sensor(struct device *dev,
> +				    struct hwmon_device *hwdev, int index)
> +{
> +	struct hwmon_thermal_data *tdata;
> +
> +	tdata = devm_kzalloc(dev, sizeof(*tdata), GFP_KERNEL);
> +	if (!tdata)
> +		return -ENOMEM;
> +
> +	tdata->hwdev = hwdev;
> +	tdata->index = index;
> +
> +	devm_thermal_zone_of_sensor_register(&hwdev->dev, index, tdata,
> +					     &hwmon_thermal_ops);
> +
> +	return 0;
> +}
> +#else
> +static int hwmon_thermal_add_sensor(struct device *dev,
> +				    struct hwmon_device *hwdev, int index)
> +{
> +	return 0;
> +}
> +#endif /* IS_REACHABLE(CONFIG_THERMAL) && defined(CONFIG_THERMAL_OF) */
> +
> +/* sysfs attribute management */
> +
> +static ssize_t hwmon_attr_show(struct device *dev,
> +			       struct device_attribute *devattr, char *buf)
> +{
> +	struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
> +	long val;
> +	int ret;
> +
> +	ret = hattr->ops->read(dev, hattr->type, hattr->attr, hattr->index,
> +			       &val);
> +	if (ret < 0)
> +		return ret;
> +
> +	return sprintf(buf, "%ld\n", val);
> +}
> +
> +static ssize_t hwmon_attr_store(struct device *dev,
> +				struct device_attribute *devattr,
> +				const char *buf, size_t count)
> +{
> +	struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
> +	long val;
> +	int ret;
> +
> +	ret = kstrtol(buf, 10, &val);
> +	if (ret < 0)
> +		return ret;
> +
> +	ret = hattr->ops->write(dev, hattr->type, hattr->attr, hattr->index,
> +				val);
> +	if (ret < 0)
> +		return ret;
> +
> +	return count;
> +}
> +
> +static int hwmon_attr_base(enum hwmon_sensor_types type)
> +{
> +	if (type == hwmon_in)
> +		return 0;
> +	return 1;
> +}
> +
> +static struct attribute *hwmon_genattr(struct device *dev,
> +				       const void *drvdata,
> +				       enum hwmon_sensor_types type,
> +				       u32 attr,
> +				       int index,
> +				       const char *template,
> +				       const struct hwmon_ops *ops)
> +{
> +	struct hwmon_device_attribute *hattr;
> +	struct device_attribute *dattr;
> +	struct attribute *a;
> +	umode_t mode;
> +	char *name;
> +
> +	/* The attribute is invisible if there is no template string */
> +	if (!template)
> +		return ERR_PTR(-ENOENT);
> +
> +	mode = ops->is_visible(drvdata, type, attr, index);
> +	if (!mode)
> +		return ERR_PTR(-ENOENT);
> +
> +	if ((mode & S_IRUGO) && !ops->read)
> +		return ERR_PTR(-EINVAL);
> +	if ((mode & S_IWUGO) && !ops->write)
> +		return ERR_PTR(-EINVAL);
> +
> +	if (type == hwmon_chip) {
> +		name = (char *)template;
> +	} else {
> +		name = devm_kzalloc(dev, strlen(template) + 16, GFP_KERNEL);
> +		if (!name)
> +			return ERR_PTR(-ENOMEM);
> +		scnprintf(name, strlen(template) + 16, template,
> +			  index + hwmon_attr_base(type));
> +	}
> +
> +	hattr = devm_kzalloc(dev, sizeof(*hattr), GFP_KERNEL);
> +	if (!hattr)
> +		return ERR_PTR(-ENOMEM);
> +
> +	hattr->type = type;
> +	hattr->attr = attr;
> +	hattr->index = index;
> +	hattr->ops = ops;
> +
> +	dattr = &hattr->dev_attr;
> +	dattr->show = hwmon_attr_show;
> +	dattr->store = hwmon_attr_store;
> +
> +	a = &dattr->attr;
> +	sysfs_attr_init(a);
> +	a->name = name;
> +	a->mode = mode;
> +
> +	return a;
> +}
> +
> +static const char * const hwmon_chip_attr_templates[] = {
> +	[hwmon_chip_temp_reset_history] = "temp_reset_history",
> +	[hwmon_chip_update_interval] = "update_interval",
> +	[hwmon_chip_alarms] = "alarms",
> +};
> +
> +static const char * const hwmon_temp_attr_templates[] = {
> +	[hwmon_temp_input] = "temp%d_input",
> +	[hwmon_temp_type] = "temp%d_type",
> +	[hwmon_temp_lcrit] = "temp%d_lcrit",
> +	[hwmon_temp_lcrit_hyst] = "temp%d_lcrit_hyst",
> +	[hwmon_temp_min] = "temp%d_min",
> +	[hwmon_temp_min_hyst] = "temp%d_min_hyst",
> +	[hwmon_temp_max] = "temp%d_max",
> +	[hwmon_temp_max_hyst] = "temp%d_max_hyst",
> +	[hwmon_temp_crit] = "temp%d_crit",
> +	[hwmon_temp_crit_hyst] = "temp%d_crit_hyst",
> +	[hwmon_temp_emergency] = "temp%d_emergency",
> +	[hwmon_temp_emergency_hyst] = "temp%d_emergency_hyst",
> +	[hwmon_temp_alarm] = "temp%d_alarm",
> +	[hwmon_temp_lcrit_alarm] = "temp%d_lcrit_alarm",
> +	[hwmon_temp_min_alarm] = "temp%d_min_alarm",
> +	[hwmon_temp_max_alarm] = "temp%d_max_alarm",
> +	[hwmon_temp_crit_alarm] = "temp%d_crit_alarm",
> +	[hwmon_temp_emergency_alarm] = "temp%d_emergency_alarm",
> +	[hwmon_temp_fault] = "temp%d_fault",
> +	[hwmon_temp_offset] = "temp%d_offset",
> +	[hwmon_temp_label] = "temp%d_label",
> +	[hwmon_temp_lowest] = "temp%d_lowest",
> +	[hwmon_temp_highest] = "temp%d_highest",
> +	[hwmon_temp_reset_history] = "temp%d_reset_history",
> +};
> +
> +static const char * const *__templates[] = {
> +	[hwmon_chip] = hwmon_chip_attr_templates,
> +	[hwmon_temp] = hwmon_temp_attr_templates,
> +};
> +
> +static const int __templates_size[] = {
> +	[hwmon_chip] = ARRAY_SIZE(hwmon_chip_attr_templates),
> +	[hwmon_temp] = ARRAY_SIZE(hwmon_temp_attr_templates),
> +};
> +
> +static int hwmon_num_channel_attrs(const struct hwmon_channel_info *info)
> +{
> +	int i, n;
> +
> +	for (i = n = 0; info->config[i]; i++)
> +		n += hweight32(info->config[i]);
> +
> +	return n;
> +}
> +
> +static int hwmon_genattrs(struct device *dev,
> +			  const void *drvdata,
> +			  struct attribute **attrs,
> +			  const struct hwmon_ops *ops,
> +			  const struct hwmon_channel_info *info)
> +{
> +	const char * const *templates;
> +	int template_size;
> +	int i, aindex = 0;
> +
> +	if (info->type >= ARRAY_SIZE(__templates))
> +		return -EINVAL;
> +
> +	templates = __templates[info->type];
> +	template_size = __templates_size[info->type];
> +
> +	for (i = 0; info->config[i]; i++) {
> +		u32 attr_mask = info->config[i];
> +		u32 attr;
> +
> +		while (attr_mask) {
> +			struct attribute *a;
> +
> +			attr = __ffs(attr_mask);
> +			attr_mask &= ~BIT(attr);
> +			if (attr >= template_size)
> +				return -EINVAL;
> +			a = hwmon_genattr(dev, drvdata, info->type, attr, i,
> +					  templates[attr], ops);
> +			if (IS_ERR(a)) {
> +				if (PTR_ERR(a) != -ENOENT)
> +					return PTR_ERR(a);
> +				continue;
> +			}
> +			attrs[aindex++] = a;
> +		}
> +	}
> +	return aindex;
> +}
> +
> +static struct attribute **
> +__hwmon_create_attrs(struct device *dev, const void *drvdata,
> +		     const struct hwmon_chip_info *chip)
> +{
> +	int ret, i, aindex = 0, nattrs = 0;
> +	struct attribute **attrs;
> +
> +	for (i = 0; chip->info[i]; i++)
> +		nattrs += hwmon_num_channel_attrs(chip->info[i]);
> +
> +	if (nattrs == 0)
> +		return ERR_PTR(-EINVAL);
> +
> +	attrs = devm_kcalloc(dev, nattrs + 1, sizeof(*attrs), GFP_KERNEL);
> +	if (!attrs)
> +		return ERR_PTR(-ENOMEM);
> +
> +	for (i = 0; chip->info[i]; i++) {
> +		ret = hwmon_genattrs(dev, drvdata, &attrs[aindex], chip->ops,
> +				     chip->info[i]);
> +		if (ret < 0)
> +			return ERR_PTR(ret);
> +		aindex += ret;
> +	}
> +
> +	return attrs;
> +}
> +
> +static struct device *
> +__hwmon_device_register(struct device *dev, const char *name, void *drvdata,
> +			const struct hwmon_chip_info *chip,
> +			const struct attribute_group **groups)
>   {
>   	struct hwmon_device *hwdev;
> -	int err, id;
> +	struct device *hdev;
> +	int i, j, err, id;
>
>   	/* Do not accept invalid characters in hwmon name attribute */
>   	if (name && (!strlen(name) || strpbrk(name, "-* \t\n")))
> @@ -112,28 +401,128 @@ hwmon_device_register_with_groups(struct device *dev, const char *name,
>   		goto ida_remove;
>   	}
>
> +	hdev = &hwdev->dev;
> +
> +	if (chip && chip->ops->is_visible) {
> +		struct attribute **attrs;
> +		int ngroups = 2;
> +
> +		if (groups)
> +			for (i = 0; groups[i]; i++)
> +				ngroups++;
> +
> +		hwdev->groups = devm_kcalloc(dev, ngroups, sizeof(*groups),
> +					     GFP_KERNEL);
> +		if (!hwdev->groups)
> +			return ERR_PTR(-ENOMEM);
> +
> +		attrs = __hwmon_create_attrs(dev, drvdata, chip);
> +		if (IS_ERR(attrs)) {
> +			err = PTR_ERR(attrs);
> +			goto free_hwmon;
> +		}
> +
> +		hwdev->group.attrs = attrs;
> +		ngroups = 0;
> +		hwdev->groups[ngroups++] = &hwdev->group;
> +
> +		if (groups) {
> +			for (i = 0; groups[i]; i++)
> +				hwdev->groups[ngroups++] = groups[i];
> +		}
> +
> +		hdev->groups = hwdev->groups;
> +	} else {
> +		hdev->groups = groups;
> +	}
> +
>   	hwdev->name = name;
> -	hwdev->dev.class = &hwmon_class;
> -	hwdev->dev.parent = dev;
> -	hwdev->dev.groups = groups;
> -	hwdev->dev.of_node = dev ? dev->of_node : NULL;
> -	dev_set_drvdata(&hwdev->dev, drvdata);
> -	dev_set_name(&hwdev->dev, HWMON_ID_FORMAT, id);
> -	err = device_register(&hwdev->dev);
> +	hdev->class = &hwmon_class;
> +	hdev->parent = dev;
> +	hdev->of_node = dev ? dev->of_node : NULL;
> +	hwdev->chip = chip;
> +	dev_set_drvdata(hdev, drvdata);
> +	dev_set_name(hdev, HWMON_ID_FORMAT, id);
> +	err = device_register(hdev);
>   	if (err)
> -		goto free;
> +		goto free_hwmon;
> +
> +	if (chip && chip->ops->is_visible && chip->ops->read &&
> +	    chip->info[0]->type == hwmon_chip &&
> +	    (chip->info[0]->config[0] & HWMON_C_REGISTER_TZ)) {
> +		const struct hwmon_channel_info **info = chip->info;
> +
> +		for (i = 1; info[i]; i++) {
> +			if (info[i]->type != hwmon_temp)
> +				continue;
> +
> +			for (j = 0; info[i]->config[j]; j++) {
> +				if (!chip->ops->is_visible(drvdata, hwmon_temp,
> +							   hwmon_temp_input, j))
> +					continue;
> +				if (info[i]->config[j] & HWMON_T_INPUT)
> +					hwmon_thermal_add_sensor(dev, hwdev, j);
> +			}
> +		}
> +	}
>
> -	return &hwdev->dev;
> +	return hdev;
>
> -free:
> +free_hwmon:
>   	kfree(hwdev);
>   ida_remove:
>   	ida_simple_remove(&hwmon_ida, id);
>   	return ERR_PTR(err);
>   }
> +
> +/**
> + * hwmon_device_register_with_groups - register w/ hwmon
> + * @dev: the parent device
> + * @name: hwmon name attribute
> + * @drvdata: driver data to attach to created device
> + * @groups: List of attribute groups to create
> + *
> + * hwmon_device_unregister() must be called when the device is no
> + * longer needed.
> + *
> + * Returns the pointer to the new device.
> + */
> +struct device *
> +hwmon_device_register_with_groups(struct device *dev, const char *name,
> +				  void *drvdata,
> +				  const struct attribute_group **groups)
> +{
> +	return __hwmon_device_register(dev, name, drvdata, NULL, groups);
> +}
>   EXPORT_SYMBOL_GPL(hwmon_device_register_with_groups);
>
>   /**
> + * hwmon_device_register_with_info - register w/ hwmon
> + * @dev: the parent device
> + * @name: hwmon name attribute
> + * @drvdata: driver data to attach to created device
> + * @info: Pointer to hwmon chip information
> + * @groups - pointer to list of driver specific attribute groups
> + *
> + * hwmon_device_unregister() must be called when the device is no
> + * longer needed.
> + *
> + * Returns the pointer to the new device.
> + */
> +struct device *
> +hwmon_device_register_with_info(struct device *dev, const char *name,
> +				void *drvdata,
> +				const struct hwmon_chip_info *chip,
> +				const struct attribute_group **groups)
> +{
> +	if (chip && (!chip->ops || !chip->info))
> +		return ERR_PTR(-EINVAL);
> +
> +	return __hwmon_device_register(dev, name, drvdata, chip, groups);
> +}
> +EXPORT_SYMBOL_GPL(hwmon_device_register_with_info);
> +
> +/**
>    * hwmon_device_register - register w/ hwmon
>    * @dev: the device to register
>    *
> @@ -211,6 +600,48 @@ error:
>   }
>   EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_groups);
>
> +/**
> + * devm_hwmon_device_register_with_info - register w/ hwmon
> + * @dev: the parent device
> + * @name: hwmon name attribute
> + * @drvdata: driver data to attach to created device
> + * @info: Pointer to hwmon chip information
> + * @groups - pointer to list of driver specific attribute groups
> + *
> + * Returns the pointer to the new device. The new device is automatically
> + * unregistered with the parent device.
> + */
> +struct device *
> +devm_hwmon_device_register_with_info(struct device *dev, const char *name,
> +				     void *drvdata,
> +				     const struct hwmon_chip_info *chip,
> +				     const struct attribute_group **groups)
> +{
> +	struct device **ptr, *hwdev;
> +
> +	if (!dev)
> +		return ERR_PTR(-EINVAL);
> +
> +	ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL);
> +	if (!ptr)
> +		return ERR_PTR(-ENOMEM);
> +
> +	hwdev = hwmon_device_register_with_info(dev, name, drvdata, chip,
> +						groups);
> +	if (IS_ERR(hwdev))
> +		goto error;
> +
> +	*ptr = hwdev;
> +	devres_add(dev, ptr);
> +
> +	return hwdev;
> +
> +error:
> +	devres_free(ptr);
> +	return hwdev;
> +}
> +EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_info);
> +
>   static int devm_hwmon_match(struct device *dev, void *res, void *data)
>   {
>   	struct device **hwdev = res;
> diff --git a/include/linux/hwmon.h b/include/linux/hwmon.h
> index 09354f6c1d63..52e56d71d742 100644
> --- a/include/linux/hwmon.h
> +++ b/include/linux/hwmon.h
> @@ -14,9 +14,147 @@
>   #ifndef _HWMON_H_
>   #define _HWMON_H_
>
> +#include <linux/bitops.h>
> +
>   struct device;
>   struct attribute_group;
>
> +enum hwmon_sensor_types {
> +	hwmon_chip,
> +	hwmon_temp,
> +	hwmon_in,
> +	hwmon_curr,
> +	hwmon_power,
> +	hwmon_energy,
> +};
> +
> +enum hwmon_chip_attributes {
> +	hwmon_chip_temp_reset_history,
> +	hwmon_chip_register_tz,
> +	hwmon_chip_update_interval,
> +	hwmon_chip_alarms,
> +};
> +
> +#define HWMON_C_TEMP_RESET_HISTORY	BIT(hwmon_chip_temp_reset_history)
> +#define HWMON_C_IN_RESET_HISTORY	BIT(hwmon_chip_in_reset_history)
> +#define HWMON_C_REGISTER_TZ		BIT(hwmon_chip_register_tz)
> +#define HWMON_C_UPDATE_INTERVAL		BIT(hwmon_chip_update_interval)
> +#define HWMON_C_ALARMS			BIT(hwmon_chip_alarms)
> +
> +enum hwmon_temp_attributes {
> +	hwmon_temp_input = 0,
> +	hwmon_temp_type,
> +	hwmon_temp_lcrit,
> +	hwmon_temp_lcrit_hyst,
> +	hwmon_temp_min,
> +	hwmon_temp_min_hyst,
> +	hwmon_temp_max,
> +	hwmon_temp_max_hyst,
> +	hwmon_temp_crit,
> +	hwmon_temp_crit_hyst,
> +	hwmon_temp_emergency,
> +	hwmon_temp_emergency_hyst,
> +	hwmon_temp_alarm,
> +	hwmon_temp_lcrit_alarm,
> +	hwmon_temp_min_alarm,
> +	hwmon_temp_max_alarm,
> +	hwmon_temp_crit_alarm,
> +	hwmon_temp_emergency_alarm,
> +	hwmon_temp_fault,
> +	hwmon_temp_offset,
> +	hwmon_temp_label,
> +	hwmon_temp_lowest,
> +	hwmon_temp_highest,
> +	hwmon_temp_reset_history,
> +};
> +
> +#define HWMON_T_INPUT		BIT(hwmon_temp_input)
> +#define HWMON_T_TYPE		BIT(hwmon_temp_type)
> +#define HWMON_T_LCRIT		BIT(hwmon_temp_lcrit)
> +#define HWMON_T_LCRIT_HYST	BIT(hwmon_temp_lcrit_hyst)
> +#define HWMON_T_MIN		BIT(hwmon_temp_min)
> +#define HWMON_T_MIN_HYST	BIT(hwmon_temp_min_hyst)
> +#define HWMON_T_MAX		BIT(hwmon_temp_max)
> +#define HWMON_T_MAX_HYST	BIT(hwmon_temp_max_hyst)
> +#define HWMON_T_CRIT		BIT(hwmon_temp_crit)
> +#define HWMON_T_CRIT_HYST	BIT(hwmon_temp_crit_hyst)
> +#define HWMON_T_EMERGENCY	BIT(hwmon_temp_emergency)
> +#define HWMON_T_EMERGENCY_HYST	BIT(hwmon_temp_emergency_hyst)
> +#define HWMON_T_MIN_ALARM	BIT(hwmon_temp_min_alarm)
> +#define HWMON_T_MAX_ALARM	BIT(hwmon_temp_max_alarm)
> +#define HWMON_T_CRIT_ALARM	BIT(hwmon_temp_crit_alarm)
> +#define HWMON_T_EMERGENCY_ALARM	BIT(hwmon_temp_emergency_alarm)
> +#define HWMON_T_FAULT		BIT(hwmon_temp_fault)
> +#define HWMON_T_OFFSET		BIT(hwmon_temp_offset)
> +#define HWMON_T_LABEL		BIT(hwmon_temp_label)
> +#define HWMON_T_LOWEST		BIT(hwmon_temp_lowest)
> +#define HWMON_T_HIGHEST		BIT(hwmon_temp_highest)
> +#define HWMON_T_RESET_HISTORY	BIT(hwmon_temp_reset_history)
> +
> +/**
> + * struct hwmon_ops - hwmon device operations
> + * @is_visible: Callback to return attribute visibility. Mandatory.
> + *		Parameters are:
> + *		@const void *drvdata:
> + *			Pointer to driver-private data structure passed
> + *			as argument to hwmon_device_register_with_info().
> + *		@type:	Sensor type
> + *		@attr:	Sensor attribute
> + *		@channel:
> + *			Channel number
> + *		The function returns the file permissions.
> + *		If the return value is 0, no attribute will be created.
> + * @read:       Read callback. Optional. If not provided, attributes
> + *		will not be readable.
> + *		Parameters are:
> + *		@dev:	Pointer to hardware monitoring device
> + *		@type:	Sensor type
> + *		@attr:	Sensor attribute
> + *		@channel:
> + *			Channel number
> + *		@val:	Pointer to returned value
> + *		The function returns 0 on success or a negative error number.
> + * @write:	Write callback. Optional. If not provided, attributes
> + *		will not be writable.
> + *		Parameters are:
> + *		@dev:	Pointer to hardware monitoring device
> + *		@type:	Sensor type
> + *		@attr:	Sensor attribute
> + *		@channel:
> + *			Channel number
> + *		@val:	Value to write
> + *		The function returns 0 on success or a negative error number.
> + */
> +struct hwmon_ops {
> +	umode_t (*is_visible)(const void *drvdata, enum hwmon_sensor_types type,
> +			      u32 attr, int channel);
> +	int (*read)(struct device *dev, enum hwmon_sensor_types type,
> +		    u32 attr, int channel, long *val);
> +	int (*write)(struct device *dev, enum hwmon_sensor_types type,
> +		     u32 attr, int channel, long val);
> +};
> +
> +/**
> + * Channel information
> + * @type:	Channel type.
> + * @config:	Pointer to NULL-terminated list of channel parameters.
> + *		Use for per-channel attributes.
> + */
> +struct hwmon_channel_info {
> +	enum hwmon_sensor_types type;
> +	const u32 *config;
> +};
> +
> +/**
> + * Chip configuration
> + * @ops:	Pointer to hwmon operations.
> + * @info:	Null-terminated list of channel information.
> + */
> +struct hwmon_chip_info {
> +	const struct hwmon_ops *ops;
> +	const struct hwmon_channel_info **info;
> +};
> +
>   struct device *hwmon_device_register(struct device *dev);
>   struct device *
>   hwmon_device_register_with_groups(struct device *dev, const char *name,
> @@ -26,6 +164,16 @@ struct device *
>   devm_hwmon_device_register_with_groups(struct device *dev, const char *name,
>   				       void *drvdata,
>   				       const struct attribute_group **groups);
> +struct device *
> +hwmon_device_register_with_info(struct device *dev,
> +				const char *name, void *drvdata,
> +				const struct hwmon_chip_info *info,
> +				const struct attribute_group **groups);
> +struct device *
> +devm_hwmon_device_register_with_info(struct device *dev,
> +				     const char *name, void *drvdata,
> +				     const struct hwmon_chip_info *info,
> +				     const struct attribute_group **groups);
>
>   void hwmon_device_unregister(struct device *dev);
>   void devm_hwmon_device_unregister(struct device *dev);
>

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH v3 2/9] hwmon: (core) New hwmon registration API
@ 2016-08-12  8:16     ` Keerthy
  0 siblings, 0 replies; 26+ messages in thread
From: Keerthy @ 2016-08-12  8:16 UTC (permalink / raw)
  To: Guenter Roeck, Jean Delvare
  Cc: Jonathan Cameron, Zhang Rui, Eduardo Valentin, Punit Agrawal,
	linux-pm, linux-iio, linux-hwmon, linux-kernel, linux-omap, R,
	Vignesh

On Monday 25 July 2016 09:02 AM, Guenter Roeck wrote:
> Up to now, each hwmon driver has to implement its own sysfs attributes.
> This requires a lot of template code, and distracts from the driver's core
> function to read and write chip registers.
>
> To be able to reduce driver complexity, move sensor attribute handling
> and thermal zone registration into hwmon core. By using the new API,
> driver code and data size is typically reduced by 20-70%, depending
> on driver complexity and the number of sysfs attributes supported.
>
> With this patch, the new API only supports thermal sensors. Support for
> other sensor types will be added with subsequent patches.
>
Hi Guenter,

Seems like this patch introduces a cycling dependency between
thermal_sys and hwmon:

DEPMOD 4.8.0-rc1-next-20160811-00001-g2f84cf7854e7
depmod: ERROR: Found 2 modules in dependency cycles!
depmod: ERROR: Cycle detected: hwmon -> thermal_sys -> hwmon

As reported by R, Vignesh <vigneshr@ti.com>.

- Keerthy

> Acked-by: Punit Agrawal <punit.agrawal@arm.com>
> Reviewed-by: Jonathan Cameron <jic23@kernel.org>
> Signed-off-by: Guenter Roeck <linux@roeck-us.net>
> ---
> v3:
> - Thermal registration depends on IS_REACHABLE(CONFIG_THERMAL) and
>    CONFIG_THERMAL_OF.
> v2:
> - Document callback function parameters of struct hwmon_ops in
>    include/linux/hwmon.h.
> - Clarify that the is_visible() callback is mandatory.
> - Initialize device attribute read/write callback functions unconditionally.
> - If an attribute has no template string, treat it as invisible, not as
>    error. Affected are virtual attributes such as HWMON_C_REGISTER_TZ.
> - Added newline to improve readability.
>
> Review comments not addressed:
> - Stick with u32 for attribute masks. We could use u64, but it is currently
>    not needed, and changing it later would be straightforward.
> - Do not use for_each_set_bit() to walk attribute masks.
>    for_each_set_bit() expects a pointer to an unsigned long as argument,
>    which would make it difficult to switch to u64 attribute masks if/when
>    needed.
>
>   drivers/hwmon/hwmon.c | 485 +++++++++++++++++++++++++++++++++++++++++++++++---
>   include/linux/hwmon.h | 148 +++++++++++++++
>   2 files changed, 606 insertions(+), 27 deletions(-)
>
> diff --git a/drivers/hwmon/hwmon.c b/drivers/hwmon/hwmon.c
> index 649a68d119b4..3e4cc442a089 100644
> --- a/drivers/hwmon/hwmon.c
> +++ b/drivers/hwmon/hwmon.c
> @@ -12,6 +12,7 @@
>
>   #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
>
> +#include <linux/bitops.h>
>   #include <linux/device.h>
>   #include <linux/err.h>
>   #include <linux/gfp.h>
> @@ -21,6 +22,7 @@
>   #include <linux/pci.h>
>   #include <linux/slab.h>
>   #include <linux/string.h>
> +#include <linux/thermal.h>
>
>   #define HWMON_ID_PREFIX "hwmon"
>   #define HWMON_ID_FORMAT HWMON_ID_PREFIX "%d"
> @@ -28,9 +30,35 @@
>   struct hwmon_device {
>   	const char *name;
>   	struct device dev;
> +	const struct hwmon_chip_info *chip;
> +
> +	struct attribute_group group;
> +	const struct attribute_group **groups;
>   };
> +
>   #define to_hwmon_device(d) container_of(d, struct hwmon_device, dev)
>
> +struct hwmon_device_attribute {
> +	struct device_attribute dev_attr;
> +	const struct hwmon_ops *ops;
> +	enum hwmon_sensor_types type;
> +	u32 attr;
> +	int index;
> +};
> +
> +#define to_hwmon_attr(d) \
> +	container_of(d, struct hwmon_device_attribute, dev_attr)
> +
> +/*
> + * Thermal zone information
> + * In addition to the reference to the hwmon device,
> + * also provides the sensor index.
> + */
> +struct hwmon_thermal_data {
> +	struct hwmon_device *hwdev;	/* Reference to hwmon device */
> +	int index;			/* sensor index */
> +};
> +
>   static ssize_t
>   show_name(struct device *dev, struct device_attribute *attr, char *buf)
>   {
> @@ -78,25 +106,286 @@ static struct class hwmon_class = {
>
>   static DEFINE_IDA(hwmon_ida);
>
> -/**
> - * hwmon_device_register_with_groups - register w/ hwmon
> - * @dev: the parent device
> - * @name: hwmon name attribute
> - * @drvdata: driver data to attach to created device
> - * @groups: List of attribute groups to create
> - *
> - * hwmon_device_unregister() must be called when the device is no
> - * longer needed.
> - *
> - * Returns the pointer to the new device.
> - */
> -struct device *
> -hwmon_device_register_with_groups(struct device *dev, const char *name,
> -				  void *drvdata,
> -				  const struct attribute_group **groups)
> +/* Thermal zone handling */
> +
> +#if IS_REACHABLE(CONFIG_THERMAL) && defined(CONFIG_THERMAL_OF)
> +static int hwmon_thermal_get_temp(void *data, int *temp)
> +{
> +	struct hwmon_thermal_data *tdata = data;
> +	struct hwmon_device *hwdev = tdata->hwdev;
> +	int ret;
> +	long t;
> +
> +	ret = hwdev->chip->ops->read(&hwdev->dev, hwmon_temp, hwmon_temp_input,
> +				     tdata->index, &t);
> +	if (ret < 0)
> +		return ret;
> +
> +	*temp = t;
> +
> +	return 0;
> +}
> +
> +static struct thermal_zone_of_device_ops hwmon_thermal_ops = {
> +	.get_temp = hwmon_thermal_get_temp,
> +};
> +
> +static int hwmon_thermal_add_sensor(struct device *dev,
> +				    struct hwmon_device *hwdev, int index)
> +{
> +	struct hwmon_thermal_data *tdata;
> +
> +	tdata = devm_kzalloc(dev, sizeof(*tdata), GFP_KERNEL);
> +	if (!tdata)
> +		return -ENOMEM;
> +
> +	tdata->hwdev = hwdev;
> +	tdata->index = index;
> +
> +	devm_thermal_zone_of_sensor_register(&hwdev->dev, index, tdata,
> +					     &hwmon_thermal_ops);
> +
> +	return 0;
> +}
> +#else
> +static int hwmon_thermal_add_sensor(struct device *dev,
> +				    struct hwmon_device *hwdev, int index)
> +{
> +	return 0;
> +}
> +#endif /* IS_REACHABLE(CONFIG_THERMAL) && defined(CONFIG_THERMAL_OF) */
> +
> +/* sysfs attribute management */
> +
> +static ssize_t hwmon_attr_show(struct device *dev,
> +			       struct device_attribute *devattr, char *buf)
> +{
> +	struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
> +	long val;
> +	int ret;
> +
> +	ret = hattr->ops->read(dev, hattr->type, hattr->attr, hattr->index,
> +			       &val);
> +	if (ret < 0)
> +		return ret;
> +
> +	return sprintf(buf, "%ld\n", val);
> +}
> +
> +static ssize_t hwmon_attr_store(struct device *dev,
> +				struct device_attribute *devattr,
> +				const char *buf, size_t count)
> +{
> +	struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
> +	long val;
> +	int ret;
> +
> +	ret = kstrtol(buf, 10, &val);
> +	if (ret < 0)
> +		return ret;
> +
> +	ret = hattr->ops->write(dev, hattr->type, hattr->attr, hattr->index,
> +				val);
> +	if (ret < 0)
> +		return ret;
> +
> +	return count;
> +}
> +
> +static int hwmon_attr_base(enum hwmon_sensor_types type)
> +{
> +	if (type == hwmon_in)
> +		return 0;
> +	return 1;
> +}
> +
> +static struct attribute *hwmon_genattr(struct device *dev,
> +				       const void *drvdata,
> +				       enum hwmon_sensor_types type,
> +				       u32 attr,
> +				       int index,
> +				       const char *template,
> +				       const struct hwmon_ops *ops)
> +{
> +	struct hwmon_device_attribute *hattr;
> +	struct device_attribute *dattr;
> +	struct attribute *a;
> +	umode_t mode;
> +	char *name;
> +
> +	/* The attribute is invisible if there is no template string */
> +	if (!template)
> +		return ERR_PTR(-ENOENT);
> +
> +	mode = ops->is_visible(drvdata, type, attr, index);
> +	if (!mode)
> +		return ERR_PTR(-ENOENT);
> +
> +	if ((mode & S_IRUGO) && !ops->read)
> +		return ERR_PTR(-EINVAL);
> +	if ((mode & S_IWUGO) && !ops->write)
> +		return ERR_PTR(-EINVAL);
> +
> +	if (type == hwmon_chip) {
> +		name = (char *)template;
> +	} else {
> +		name = devm_kzalloc(dev, strlen(template) + 16, GFP_KERNEL);
> +		if (!name)
> +			return ERR_PTR(-ENOMEM);
> +		scnprintf(name, strlen(template) + 16, template,
> +			  index + hwmon_attr_base(type));
> +	}
> +
> +	hattr = devm_kzalloc(dev, sizeof(*hattr), GFP_KERNEL);
> +	if (!hattr)
> +		return ERR_PTR(-ENOMEM);
> +
> +	hattr->type = type;
> +	hattr->attr = attr;
> +	hattr->index = index;
> +	hattr->ops = ops;
> +
> +	dattr = &hattr->dev_attr;
> +	dattr->show = hwmon_attr_show;
> +	dattr->store = hwmon_attr_store;
> +
> +	a = &dattr->attr;
> +	sysfs_attr_init(a);
> +	a->name = name;
> +	a->mode = mode;
> +
> +	return a;
> +}
> +
> +static const char * const hwmon_chip_attr_templates[] = {
> +	[hwmon_chip_temp_reset_history] = "temp_reset_history",
> +	[hwmon_chip_update_interval] = "update_interval",
> +	[hwmon_chip_alarms] = "alarms",
> +};
> +
> +static const char * const hwmon_temp_attr_templates[] = {
> +	[hwmon_temp_input] = "temp%d_input",
> +	[hwmon_temp_type] = "temp%d_type",
> +	[hwmon_temp_lcrit] = "temp%d_lcrit",
> +	[hwmon_temp_lcrit_hyst] = "temp%d_lcrit_hyst",
> +	[hwmon_temp_min] = "temp%d_min",
> +	[hwmon_temp_min_hyst] = "temp%d_min_hyst",
> +	[hwmon_temp_max] = "temp%d_max",
> +	[hwmon_temp_max_hyst] = "temp%d_max_hyst",
> +	[hwmon_temp_crit] = "temp%d_crit",
> +	[hwmon_temp_crit_hyst] = "temp%d_crit_hyst",
> +	[hwmon_temp_emergency] = "temp%d_emergency",
> +	[hwmon_temp_emergency_hyst] = "temp%d_emergency_hyst",
> +	[hwmon_temp_alarm] = "temp%d_alarm",
> +	[hwmon_temp_lcrit_alarm] = "temp%d_lcrit_alarm",
> +	[hwmon_temp_min_alarm] = "temp%d_min_alarm",
> +	[hwmon_temp_max_alarm] = "temp%d_max_alarm",
> +	[hwmon_temp_crit_alarm] = "temp%d_crit_alarm",
> +	[hwmon_temp_emergency_alarm] = "temp%d_emergency_alarm",
> +	[hwmon_temp_fault] = "temp%d_fault",
> +	[hwmon_temp_offset] = "temp%d_offset",
> +	[hwmon_temp_label] = "temp%d_label",
> +	[hwmon_temp_lowest] = "temp%d_lowest",
> +	[hwmon_temp_highest] = "temp%d_highest",
> +	[hwmon_temp_reset_history] = "temp%d_reset_history",
> +};
> +
> +static const char * const *__templates[] = {
> +	[hwmon_chip] = hwmon_chip_attr_templates,
> +	[hwmon_temp] = hwmon_temp_attr_templates,
> +};
> +
> +static const int __templates_size[] = {
> +	[hwmon_chip] = ARRAY_SIZE(hwmon_chip_attr_templates),
> +	[hwmon_temp] = ARRAY_SIZE(hwmon_temp_attr_templates),
> +};
> +
> +static int hwmon_num_channel_attrs(const struct hwmon_channel_info *info)
> +{
> +	int i, n;
> +
> +	for (i = n = 0; info->config[i]; i++)
> +		n += hweight32(info->config[i]);
> +
> +	return n;
> +}
> +
> +static int hwmon_genattrs(struct device *dev,
> +			  const void *drvdata,
> +			  struct attribute **attrs,
> +			  const struct hwmon_ops *ops,
> +			  const struct hwmon_channel_info *info)
> +{
> +	const char * const *templates;
> +	int template_size;
> +	int i, aindex = 0;
> +
> +	if (info->type >= ARRAY_SIZE(__templates))
> +		return -EINVAL;
> +
> +	templates = __templates[info->type];
> +	template_size = __templates_size[info->type];
> +
> +	for (i = 0; info->config[i]; i++) {
> +		u32 attr_mask = info->config[i];
> +		u32 attr;
> +
> +		while (attr_mask) {
> +			struct attribute *a;
> +
> +			attr = __ffs(attr_mask);
> +			attr_mask &= ~BIT(attr);
> +			if (attr >= template_size)
> +				return -EINVAL;
> +			a = hwmon_genattr(dev, drvdata, info->type, attr, i,
> +					  templates[attr], ops);
> +			if (IS_ERR(a)) {
> +				if (PTR_ERR(a) != -ENOENT)
> +					return PTR_ERR(a);
> +				continue;
> +			}
> +			attrs[aindex++] = a;
> +		}
> +	}
> +	return aindex;
> +}
> +
> +static struct attribute **
> +__hwmon_create_attrs(struct device *dev, const void *drvdata,
> +		     const struct hwmon_chip_info *chip)
> +{
> +	int ret, i, aindex = 0, nattrs = 0;
> +	struct attribute **attrs;
> +
> +	for (i = 0; chip->info[i]; i++)
> +		nattrs += hwmon_num_channel_attrs(chip->info[i]);
> +
> +	if (nattrs == 0)
> +		return ERR_PTR(-EINVAL);
> +
> +	attrs = devm_kcalloc(dev, nattrs + 1, sizeof(*attrs), GFP_KERNEL);
> +	if (!attrs)
> +		return ERR_PTR(-ENOMEM);
> +
> +	for (i = 0; chip->info[i]; i++) {
> +		ret = hwmon_genattrs(dev, drvdata, &attrs[aindex], chip->ops,
> +				     chip->info[i]);
> +		if (ret < 0)
> +			return ERR_PTR(ret);
> +		aindex += ret;
> +	}
> +
> +	return attrs;
> +}
> +
> +static struct device *
> +__hwmon_device_register(struct device *dev, const char *name, void *drvdata,
> +			const struct hwmon_chip_info *chip,
> +			const struct attribute_group **groups)
>   {
>   	struct hwmon_device *hwdev;
> -	int err, id;
> +	struct device *hdev;
> +	int i, j, err, id;
>
>   	/* Do not accept invalid characters in hwmon name attribute */
>   	if (name && (!strlen(name) || strpbrk(name, "-* \t\n")))
> @@ -112,28 +401,128 @@ hwmon_device_register_with_groups(struct device *dev, const char *name,
>   		goto ida_remove;
>   	}
>
> +	hdev = &hwdev->dev;
> +
> +	if (chip && chip->ops->is_visible) {
> +		struct attribute **attrs;
> +		int ngroups = 2;
> +
> +		if (groups)
> +			for (i = 0; groups[i]; i++)
> +				ngroups++;
> +
> +		hwdev->groups = devm_kcalloc(dev, ngroups, sizeof(*groups),
> +					     GFP_KERNEL);
> +		if (!hwdev->groups)
> +			return ERR_PTR(-ENOMEM);
> +
> +		attrs = __hwmon_create_attrs(dev, drvdata, chip);
> +		if (IS_ERR(attrs)) {
> +			err = PTR_ERR(attrs);
> +			goto free_hwmon;
> +		}
> +
> +		hwdev->group.attrs = attrs;
> +		ngroups = 0;
> +		hwdev->groups[ngroups++] = &hwdev->group;
> +
> +		if (groups) {
> +			for (i = 0; groups[i]; i++)
> +				hwdev->groups[ngroups++] = groups[i];
> +		}
> +
> +		hdev->groups = hwdev->groups;
> +	} else {
> +		hdev->groups = groups;
> +	}
> +
>   	hwdev->name = name;
> -	hwdev->dev.class = &hwmon_class;
> -	hwdev->dev.parent = dev;
> -	hwdev->dev.groups = groups;
> -	hwdev->dev.of_node = dev ? dev->of_node : NULL;
> -	dev_set_drvdata(&hwdev->dev, drvdata);
> -	dev_set_name(&hwdev->dev, HWMON_ID_FORMAT, id);
> -	err = device_register(&hwdev->dev);
> +	hdev->class = &hwmon_class;
> +	hdev->parent = dev;
> +	hdev->of_node = dev ? dev->of_node : NULL;
> +	hwdev->chip = chip;
> +	dev_set_drvdata(hdev, drvdata);
> +	dev_set_name(hdev, HWMON_ID_FORMAT, id);
> +	err = device_register(hdev);
>   	if (err)
> -		goto free;
> +		goto free_hwmon;
> +
> +	if (chip && chip->ops->is_visible && chip->ops->read &&
> +	    chip->info[0]->type == hwmon_chip &&
> +	    (chip->info[0]->config[0] & HWMON_C_REGISTER_TZ)) {
> +		const struct hwmon_channel_info **info = chip->info;
> +
> +		for (i = 1; info[i]; i++) {
> +			if (info[i]->type != hwmon_temp)
> +				continue;
> +
> +			for (j = 0; info[i]->config[j]; j++) {
> +				if (!chip->ops->is_visible(drvdata, hwmon_temp,
> +							   hwmon_temp_input, j))
> +					continue;
> +				if (info[i]->config[j] & HWMON_T_INPUT)
> +					hwmon_thermal_add_sensor(dev, hwdev, j);
> +			}
> +		}
> +	}
>
> -	return &hwdev->dev;
> +	return hdev;
>
> -free:
> +free_hwmon:
>   	kfree(hwdev);
>   ida_remove:
>   	ida_simple_remove(&hwmon_ida, id);
>   	return ERR_PTR(err);
>   }
> +
> +/**
> + * hwmon_device_register_with_groups - register w/ hwmon
> + * @dev: the parent device
> + * @name: hwmon name attribute
> + * @drvdata: driver data to attach to created device
> + * @groups: List of attribute groups to create
> + *
> + * hwmon_device_unregister() must be called when the device is no
> + * longer needed.
> + *
> + * Returns the pointer to the new device.
> + */
> +struct device *
> +hwmon_device_register_with_groups(struct device *dev, const char *name,
> +				  void *drvdata,
> +				  const struct attribute_group **groups)
> +{
> +	return __hwmon_device_register(dev, name, drvdata, NULL, groups);
> +}
>   EXPORT_SYMBOL_GPL(hwmon_device_register_with_groups);
>
>   /**
> + * hwmon_device_register_with_info - register w/ hwmon
> + * @dev: the parent device
> + * @name: hwmon name attribute
> + * @drvdata: driver data to attach to created device
> + * @info: Pointer to hwmon chip information
> + * @groups - pointer to list of driver specific attribute groups
> + *
> + * hwmon_device_unregister() must be called when the device is no
> + * longer needed.
> + *
> + * Returns the pointer to the new device.
> + */
> +struct device *
> +hwmon_device_register_with_info(struct device *dev, const char *name,
> +				void *drvdata,
> +				const struct hwmon_chip_info *chip,
> +				const struct attribute_group **groups)
> +{
> +	if (chip && (!chip->ops || !chip->info))
> +		return ERR_PTR(-EINVAL);
> +
> +	return __hwmon_device_register(dev, name, drvdata, chip, groups);
> +}
> +EXPORT_SYMBOL_GPL(hwmon_device_register_with_info);
> +
> +/**
>    * hwmon_device_register - register w/ hwmon
>    * @dev: the device to register
>    *
> @@ -211,6 +600,48 @@ error:
>   }
>   EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_groups);
>
> +/**
> + * devm_hwmon_device_register_with_info - register w/ hwmon
> + * @dev: the parent device
> + * @name: hwmon name attribute
> + * @drvdata: driver data to attach to created device
> + * @info: Pointer to hwmon chip information
> + * @groups - pointer to list of driver specific attribute groups
> + *
> + * Returns the pointer to the new device. The new device is automatically
> + * unregistered with the parent device.
> + */
> +struct device *
> +devm_hwmon_device_register_with_info(struct device *dev, const char *name,
> +				     void *drvdata,
> +				     const struct hwmon_chip_info *chip,
> +				     const struct attribute_group **groups)
> +{
> +	struct device **ptr, *hwdev;
> +
> +	if (!dev)
> +		return ERR_PTR(-EINVAL);
> +
> +	ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL);
> +	if (!ptr)
> +		return ERR_PTR(-ENOMEM);
> +
> +	hwdev = hwmon_device_register_with_info(dev, name, drvdata, chip,
> +						groups);
> +	if (IS_ERR(hwdev))
> +		goto error;
> +
> +	*ptr = hwdev;
> +	devres_add(dev, ptr);
> +
> +	return hwdev;
> +
> +error:
> +	devres_free(ptr);
> +	return hwdev;
> +}
> +EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_info);
> +
>   static int devm_hwmon_match(struct device *dev, void *res, void *data)
>   {
>   	struct device **hwdev = res;
> diff --git a/include/linux/hwmon.h b/include/linux/hwmon.h
> index 09354f6c1d63..52e56d71d742 100644
> --- a/include/linux/hwmon.h
> +++ b/include/linux/hwmon.h
> @@ -14,9 +14,147 @@
>   #ifndef _HWMON_H_
>   #define _HWMON_H_
>
> +#include <linux/bitops.h>
> +
>   struct device;
>   struct attribute_group;
>
> +enum hwmon_sensor_types {
> +	hwmon_chip,
> +	hwmon_temp,
> +	hwmon_in,
> +	hwmon_curr,
> +	hwmon_power,
> +	hwmon_energy,
> +};
> +
> +enum hwmon_chip_attributes {
> +	hwmon_chip_temp_reset_history,
> +	hwmon_chip_register_tz,
> +	hwmon_chip_update_interval,
> +	hwmon_chip_alarms,
> +};
> +
> +#define HWMON_C_TEMP_RESET_HISTORY	BIT(hwmon_chip_temp_reset_history)
> +#define HWMON_C_IN_RESET_HISTORY	BIT(hwmon_chip_in_reset_history)
> +#define HWMON_C_REGISTER_TZ		BIT(hwmon_chip_register_tz)
> +#define HWMON_C_UPDATE_INTERVAL		BIT(hwmon_chip_update_interval)
> +#define HWMON_C_ALARMS			BIT(hwmon_chip_alarms)
> +
> +enum hwmon_temp_attributes {
> +	hwmon_temp_input = 0,
> +	hwmon_temp_type,
> +	hwmon_temp_lcrit,
> +	hwmon_temp_lcrit_hyst,
> +	hwmon_temp_min,
> +	hwmon_temp_min_hyst,
> +	hwmon_temp_max,
> +	hwmon_temp_max_hyst,
> +	hwmon_temp_crit,
> +	hwmon_temp_crit_hyst,
> +	hwmon_temp_emergency,
> +	hwmon_temp_emergency_hyst,
> +	hwmon_temp_alarm,
> +	hwmon_temp_lcrit_alarm,
> +	hwmon_temp_min_alarm,
> +	hwmon_temp_max_alarm,
> +	hwmon_temp_crit_alarm,
> +	hwmon_temp_emergency_alarm,
> +	hwmon_temp_fault,
> +	hwmon_temp_offset,
> +	hwmon_temp_label,
> +	hwmon_temp_lowest,
> +	hwmon_temp_highest,
> +	hwmon_temp_reset_history,
> +};
> +
> +#define HWMON_T_INPUT		BIT(hwmon_temp_input)
> +#define HWMON_T_TYPE		BIT(hwmon_temp_type)
> +#define HWMON_T_LCRIT		BIT(hwmon_temp_lcrit)
> +#define HWMON_T_LCRIT_HYST	BIT(hwmon_temp_lcrit_hyst)
> +#define HWMON_T_MIN		BIT(hwmon_temp_min)
> +#define HWMON_T_MIN_HYST	BIT(hwmon_temp_min_hyst)
> +#define HWMON_T_MAX		BIT(hwmon_temp_max)
> +#define HWMON_T_MAX_HYST	BIT(hwmon_temp_max_hyst)
> +#define HWMON_T_CRIT		BIT(hwmon_temp_crit)
> +#define HWMON_T_CRIT_HYST	BIT(hwmon_temp_crit_hyst)
> +#define HWMON_T_EMERGENCY	BIT(hwmon_temp_emergency)
> +#define HWMON_T_EMERGENCY_HYST	BIT(hwmon_temp_emergency_hyst)
> +#define HWMON_T_MIN_ALARM	BIT(hwmon_temp_min_alarm)
> +#define HWMON_T_MAX_ALARM	BIT(hwmon_temp_max_alarm)
> +#define HWMON_T_CRIT_ALARM	BIT(hwmon_temp_crit_alarm)
> +#define HWMON_T_EMERGENCY_ALARM	BIT(hwmon_temp_emergency_alarm)
> +#define HWMON_T_FAULT		BIT(hwmon_temp_fault)
> +#define HWMON_T_OFFSET		BIT(hwmon_temp_offset)
> +#define HWMON_T_LABEL		BIT(hwmon_temp_label)
> +#define HWMON_T_LOWEST		BIT(hwmon_temp_lowest)
> +#define HWMON_T_HIGHEST		BIT(hwmon_temp_highest)
> +#define HWMON_T_RESET_HISTORY	BIT(hwmon_temp_reset_history)
> +
> +/**
> + * struct hwmon_ops - hwmon device operations
> + * @is_visible: Callback to return attribute visibility. Mandatory.
> + *		Parameters are:
> + *		@const void *drvdata:
> + *			Pointer to driver-private data structure passed
> + *			as argument to hwmon_device_register_with_info().
> + *		@type:	Sensor type
> + *		@attr:	Sensor attribute
> + *		@channel:
> + *			Channel number
> + *		The function returns the file permissions.
> + *		If the return value is 0, no attribute will be created.
> + * @read:       Read callback. Optional. If not provided, attributes
> + *		will not be readable.
> + *		Parameters are:
> + *		@dev:	Pointer to hardware monitoring device
> + *		@type:	Sensor type
> + *		@attr:	Sensor attribute
> + *		@channel:
> + *			Channel number
> + *		@val:	Pointer to returned value
> + *		The function returns 0 on success or a negative error number.
> + * @write:	Write callback. Optional. If not provided, attributes
> + *		will not be writable.
> + *		Parameters are:
> + *		@dev:	Pointer to hardware monitoring device
> + *		@type:	Sensor type
> + *		@attr:	Sensor attribute
> + *		@channel:
> + *			Channel number
> + *		@val:	Value to write
> + *		The function returns 0 on success or a negative error number.
> + */
> +struct hwmon_ops {
> +	umode_t (*is_visible)(const void *drvdata, enum hwmon_sensor_types type,
> +			      u32 attr, int channel);
> +	int (*read)(struct device *dev, enum hwmon_sensor_types type,
> +		    u32 attr, int channel, long *val);
> +	int (*write)(struct device *dev, enum hwmon_sensor_types type,
> +		     u32 attr, int channel, long val);
> +};
> +
> +/**
> + * Channel information
> + * @type:	Channel type.
> + * @config:	Pointer to NULL-terminated list of channel parameters.
> + *		Use for per-channel attributes.
> + */
> +struct hwmon_channel_info {
> +	enum hwmon_sensor_types type;
> +	const u32 *config;
> +};
> +
> +/**
> + * Chip configuration
> + * @ops:	Pointer to hwmon operations.
> + * @info:	Null-terminated list of channel information.
> + */
> +struct hwmon_chip_info {
> +	const struct hwmon_ops *ops;
> +	const struct hwmon_channel_info **info;
> +};
> +
>   struct device *hwmon_device_register(struct device *dev);
>   struct device *
>   hwmon_device_register_with_groups(struct device *dev, const char *name,
> @@ -26,6 +164,16 @@ struct device *
>   devm_hwmon_device_register_with_groups(struct device *dev, const char *name,
>   				       void *drvdata,
>   				       const struct attribute_group **groups);
> +struct device *
> +hwmon_device_register_with_info(struct device *dev,
> +				const char *name, void *drvdata,
> +				const struct hwmon_chip_info *info,
> +				const struct attribute_group **groups);
> +struct device *
> +devm_hwmon_device_register_with_info(struct device *dev,
> +				     const char *name, void *drvdata,
> +				     const struct hwmon_chip_info *info,
> +				     const struct attribute_group **groups);
>
>   void hwmon_device_unregister(struct device *dev);
>   void devm_hwmon_device_unregister(struct device *dev);
>

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH v3 2/9] hwmon: (core) New hwmon registration API
  2016-08-12  8:16     ` Keerthy
  (?)
@ 2016-08-12 12:40     ` Guenter Roeck
  -1 siblings, 0 replies; 26+ messages in thread
From: Guenter Roeck @ 2016-08-12 12:40 UTC (permalink / raw)
  To: Keerthy, Jean Delvare
  Cc: Jonathan Cameron, Zhang Rui, Eduardo Valentin, Punit Agrawal,
	linux-pm, linux-iio, linux-hwmon, linux-kernel, linux-omap, R,
	Vignesh

On 08/12/2016 01:16 AM, Keerthy wrote:
> On Monday 25 July 2016 09:02 AM, Guenter Roeck wrote:
>> Up to now, each hwmon driver has to implement its own sysfs attributes.
>> This requires a lot of template code, and distracts from the driver's core
>> function to read and write chip registers.
>>
>> To be able to reduce driver complexity, move sensor attribute handling
>> and thermal zone registration into hwmon core. By using the new API,
>> driver code and data size is typically reduced by 20-70%, depending
>> on driver complexity and the number of sysfs attributes supported.
>>
>> With this patch, the new API only supports thermal sensors. Support for
>> other sensor types will be added with subsequent patches.
>>
> Hi Guenter,
>
> Seems like this patch introduces a cycling dependency between
> thermal_sys and hwmon:
>
> DEPMOD 4.8.0-rc1-next-20160811-00001-g2f84cf7854e7
> depmod: ERROR: Found 2 modules in dependency cycles!
> depmod: ERROR: Cycle detected: hwmon -> thermal_sys -> hwmon
>
> As reported by R, Vignesh <vigneshr@ti.com>.
>

Hmm ... interesting. Thanks for letting me know. I'll have to think about that.

Guenter


^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH v3 1/9] hwmon: (core) Order include files alphabetically
  2016-07-25  3:32 ` [PATCH v3 1/9] hwmon: (core) Order include files alphabetically Guenter Roeck
@ 2016-09-27 21:13   ` Jean Delvare
  0 siblings, 0 replies; 26+ messages in thread
From: Jean Delvare @ 2016-09-27 21:13 UTC (permalink / raw)
  To: Guenter Roeck
  Cc: Jonathan Cameron, Zhang Rui, Eduardo Valentin, Punit Agrawal,
	linux-pm, linux-iio, linux-hwmon, linux-kernel

On dim., 2016-07-24 at 20:32 -0700, Guenter Roeck wrote:
> Ordering include files alphabetically makes it easier to add new ones.
> Stop including linux/spinlock.h and linux/kdev_t.h since both are not
> needed.
> 
> Reviewed-by: Jonathan Cameron <jic23@kernel.org>
> Signed-off-by: Guenter Roeck <linux@roeck-us.net>
> ---
> v3: No change
> v2: Added patch
> 
>  drivers/hwmon/hwmon.c | 10 ++++------
>  1 file changed, 4 insertions(+), 6 deletions(-)
> (...)

Reviewed-by: Jean Delvare <jdelvare@suse.de>

-- 
Jean Delvare
SUSE L3 Support

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH v3 2/9] hwmon: (core) New hwmon registration API
@ 2016-10-04  8:45     ` Jean Delvare
  0 siblings, 0 replies; 26+ messages in thread
From: Jean Delvare @ 2016-10-04  8:45 UTC (permalink / raw)
  To: Guenter Roeck
  Cc: Jonathan Cameron, Zhang Rui, Eduardo Valentin, Punit Agrawal,
	linux-pm, linux-iio, linux-hwmon, linux-kernel

Hi Guenter,

I see this patch is upstream now, but I had started reviewing it and
maybe some of my comments are still relevant.

It's not meant to be a complete review, which is why I had not sent it
yet :-(

Also I did not follow the first iterations of this patchset so my
apologies if I raise points which have already been discussed.

May I ask if any other subsystem is already doing anything like that?

FYI I gave a presentation about the hwmon device registration API
evolution last week at Kernel Recipes [1] in Paris and mentioned this
proposal.

[1] https://kernel-recipes.org/en/2016/

On dim., 2016-07-24 at 20:32 -0700, Guenter Roeck wrote:
> Up to now, each hwmon driver has to implement its own sysfs attributes.
> This requires a lot of template code, and distracts from the driver's core
> function to read and write chip registers.
> 
> To be able to reduce driver complexity, move sensor attribute handling
> and thermal zone registration into hwmon core. By using the new API,
> driver code and data size is typically reduced by 20-70%, depending
> on driver complexity and the number of sysfs attributes supported.

I looked at the diffstats for the drivers you have already converted and
couldn't see any such huge improvement... Some drivers appear to be even
larger after conversion?

> With this patch, the new API only supports thermal sensors. Support for
> other sensor types will be added with subsequent patches.
> 
> Acked-by: Punit Agrawal <punit.agrawal@arm.com>
> Reviewed-by: Jonathan Cameron <jic23@kernel.org>
> Signed-off-by: Guenter Roeck <linux@roeck-us.net>
> ---
> v3:
> - Thermal registration depends on IS_REACHABLE(CONFIG_THERMAL) and
>   CONFIG_THERMAL_OF.
> v2:
> - Document callback function parameters of struct hwmon_ops in
>   include/linux/hwmon.h.
> - Clarify that the is_visible() callback is mandatory.
> - Initialize device attribute read/write callback functions unconditionally.
> - If an attribute has no template string, treat it as invisible, not as
>   error. Affected are virtual attributes such as HWMON_C_REGISTER_TZ.
> - Added newline to improve readability.
> 
> Review comments not addressed:
> - Stick with u32 for attribute masks. We could use u64, but it is currently
>   not needed, and changing it later would be straightforward.
> - Do not use for_each_set_bit() to walk attribute masks.
>   for_each_set_bit() expects a pointer to an unsigned long as argument,
>   which would make it difficult to switch to u64 attribute masks if/when
>   needed.
> 
>  drivers/hwmon/hwmon.c | 485 +++++++++++++++++++++++++++++++++++++++++++++++---
>  include/linux/hwmon.h | 148 +++++++++++++++
>  2 files changed, 606 insertions(+), 27 deletions(-)
> 
> diff --git a/drivers/hwmon/hwmon.c b/drivers/hwmon/hwmon.c
> index 649a68d119b4..3e4cc442a089 100644
> --- a/drivers/hwmon/hwmon.c
> +++ b/drivers/hwmon/hwmon.c

I believe it would make sense to add you copyright at the top of the
file. That's a pretty big change with lots of code.

> @@ -12,6 +12,7 @@
>  
>  #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
>  
> +#include <linux/bitops.h>
>  #include <linux/device.h>
>  #include <linux/err.h>
>  #include <linux/gfp.h>
> @@ -21,6 +22,7 @@
>  #include <linux/pci.h>
>  #include <linux/slab.h>
>  #include <linux/string.h>
> +#include <linux/thermal.h>
>  
>  #define HWMON_ID_PREFIX "hwmon"
>  #define HWMON_ID_FORMAT HWMON_ID_PREFIX "%d"
> @@ -28,9 +30,35 @@
>  struct hwmon_device {
>  	const char *name;
>  	struct device dev;
> +	const struct hwmon_chip_info *chip;
> +
> +	struct attribute_group group;
> +	const struct attribute_group **groups;
>  };
> +
>  #define to_hwmon_device(d) container_of(d, struct hwmon_device, dev)
>  
> +struct hwmon_device_attribute {
> +	struct device_attribute dev_attr;
> +	const struct hwmon_ops *ops;
> +	enum hwmon_sensor_types type;
> +	u32 attr;
> +	int index;
> +};
> +
> +#define to_hwmon_attr(d) \
> +	container_of(d, struct hwmon_device_attribute, dev_attr)
> +
> +/*
> + * Thermal zone information
> + * In addition to the reference to the hwmon device,
> + * also provides the sensor index.
> + */
> +struct hwmon_thermal_data {
> +	struct hwmon_device *hwdev;	/* Reference to hwmon device */
> +	int index;			/* sensor index */
> +};
> +
>  static ssize_t
>  show_name(struct device *dev, struct device_attribute *attr, char *buf)
>  {
> @@ -78,25 +106,286 @@ static struct class hwmon_class = {
>  
>  static DEFINE_IDA(hwmon_ida);
>  
> -/**
> - * hwmon_device_register_with_groups - register w/ hwmon
> - * @dev: the parent device
> - * @name: hwmon name attribute
> - * @drvdata: driver data to attach to created device
> - * @groups: List of attribute groups to create
> - *
> - * hwmon_device_unregister() must be called when the device is no
> - * longer needed.
> - *
> - * Returns the pointer to the new device.
> - */
> -struct device *
> -hwmon_device_register_with_groups(struct device *dev, const char *name,
> -				  void *drvdata,
> -				  const struct attribute_group **groups)
> +/* Thermal zone handling */
> +
> +#if IS_REACHABLE(CONFIG_THERMAL) && defined(CONFIG_THERMAL_OF)
> +static int hwmon_thermal_get_temp(void *data, int *temp)
> +{
> +	struct hwmon_thermal_data *tdata = data;
> +	struct hwmon_device *hwdev = tdata->hwdev;
> +	int ret;
> +	long t;
> +
> +	ret = hwdev->chip->ops->read(&hwdev->dev, hwmon_temp, hwmon_temp_input,
> +				     tdata->index, &t);
> +	if (ret < 0)
> +		return ret;
> +
> +	*temp = t;
> +
> +	return 0;
> +}
> +
> +static struct thermal_zone_of_device_ops hwmon_thermal_ops = {
> +	.get_temp = hwmon_thermal_get_temp,
> +};
> +
> +static int hwmon_thermal_add_sensor(struct device *dev,
> +				    struct hwmon_device *hwdev, int index)
> +{
> +	struct hwmon_thermal_data *tdata;
> +
> +	tdata = devm_kzalloc(dev, sizeof(*tdata), GFP_KERNEL);
> +	if (!tdata)
> +		return -ENOMEM;
> +
> +	tdata->hwdev = hwdev;
> +	tdata->index = index;
> +
> +	devm_thermal_zone_of_sensor_register(&hwdev->dev, index, tdata,
> +					     &hwmon_thermal_ops);
> +
> +	return 0;
> +}
> +#else
> +static int hwmon_thermal_add_sensor(struct device *dev,
> +				    struct hwmon_device *hwdev, int index)
> +{
> +	return 0;
> +}
> +#endif /* IS_REACHABLE(CONFIG_THERMAL) && defined(CONFIG_THERMAL_OF) */
> +
> +/* sysfs attribute management */
> +
> +static ssize_t hwmon_attr_show(struct device *dev,
> +			       struct device_attribute *devattr, char *buf)
> +{
> +	struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
> +	long val;
> +	int ret;
> +
> +	ret = hattr->ops->read(dev, hattr->type, hattr->attr, hattr->index,
> +			       &val);
> +	if (ret < 0)
> +		return ret;
> +
> +	return sprintf(buf, "%ld\n", val);
> +}
> +
> +static ssize_t hwmon_attr_store(struct device *dev,
> +				struct device_attribute *devattr,
> +				const char *buf, size_t count)
> +{
> +	struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
> +	long val;
> +	int ret;
> +
> +	ret = kstrtol(buf, 10, &val);
> +	if (ret < 0)
> +		return ret;
> +
> +	ret = hattr->ops->write(dev, hattr->type, hattr->attr, hattr->index,
> +				val);
> +	if (ret < 0)
> +		return ret;
> +
> +	return count;
> +}
> +
> +static int hwmon_attr_base(enum hwmon_sensor_types type)
> +{
> +	if (type == hwmon_in)
> +		return 0;
> +	return 1;
> +}
> +
> +static struct attribute *hwmon_genattr(struct device *dev,
> +				       const void *drvdata,
> +				       enum hwmon_sensor_types type,
> +				       u32 attr,
> +				       int index,
> +				       const char *template,
> +				       const struct hwmon_ops *ops)
> +{
> +	struct hwmon_device_attribute *hattr;
> +	struct device_attribute *dattr;
> +	struct attribute *a;
> +	umode_t mode;
> +	char *name;
> +
> +	/* The attribute is invisible if there is no template string */
> +	if (!template)
> +		return ERR_PTR(-ENOENT);
> +
> +	mode = ops->is_visible(drvdata, type, attr, index);

Any reason why you don't simply attach the provided ->is_visible to the
attribute group and let the driver core do the work?

> +	if (!mode)
> +		return ERR_PTR(-ENOENT);
> +
> +	if ((mode & S_IRUGO) && !ops->read)
> +		return ERR_PTR(-EINVAL);
> +	if ((mode & S_IWUGO) && !ops->write)
> +		return ERR_PTR(-EINVAL);
> +
> +	if (type == hwmon_chip) {

This needs a comment. It's not obvious what "hwmon_chip" is supposed to
represent. From what I understand, maybe "hwmon_unique" would be a more
appropriate name.

> +		name = (char *)template;
> +	} else {
> +		name = devm_kzalloc(dev, strlen(template) + 16, GFP_KERNEL);
> +		if (!name)
> +			return ERR_PTR(-ENOMEM);
> +		scnprintf(name, strlen(template) + 16, template,
> +			  index + hwmon_attr_base(type));
> +	}
> +
> +	hattr = devm_kzalloc(dev, sizeof(*hattr), GFP_KERNEL);
> +	if (!hattr)
> +		return ERR_PTR(-ENOMEM);

So basically you are doing 1 or 2 memory allocations for every
attribute? Looks quite bad from a memory fragmentation
perspective :-( Not to mention performance. Given that you have all the
data at hand before you start, can't you preallocate an array with the
right number of hattr and pick from it? That would at least solve half
of the problem.

Something similar for string allocation may work too, although it's
somewhat more complex due to the variable length. But I think the
abituguru3 driver is doing it right, so maybe you can too.

> +
> +	hattr->type = type;
> +	hattr->attr = attr;
> +	hattr->index = index;
> +	hattr->ops = ops;
> +
> +	dattr = &hattr->dev_attr;
> +	dattr->show = hwmon_attr_show;
> +	dattr->store = hwmon_attr_store;
> +
> +	a = &dattr->attr;
> +	sysfs_attr_init(a);
> +	a->name = name;
> +	a->mode = mode;
> +
> +	return a;
> +}
> +
> +static const char * const hwmon_chip_attr_templates[] = {
> +	[hwmon_chip_temp_reset_history] = "temp_reset_history",
> +	[hwmon_chip_update_interval] = "update_interval",
> +	[hwmon_chip_alarms] = "alarms",
> +};
> +
> +static const char * const hwmon_temp_attr_templates[] = {
> +	[hwmon_temp_input] = "temp%d_input",
> +	[hwmon_temp_type] = "temp%d_type",
> +	[hwmon_temp_lcrit] = "temp%d_lcrit",
> +	[hwmon_temp_lcrit_hyst] = "temp%d_lcrit_hyst",
> +	[hwmon_temp_min] = "temp%d_min",
> +	[hwmon_temp_min_hyst] = "temp%d_min_hyst",
> +	[hwmon_temp_max] = "temp%d_max",
> +	[hwmon_temp_max_hyst] = "temp%d_max_hyst",
> +	[hwmon_temp_crit] = "temp%d_crit",
> +	[hwmon_temp_crit_hyst] = "temp%d_crit_hyst",
> +	[hwmon_temp_emergency] = "temp%d_emergency",
> +	[hwmon_temp_emergency_hyst] = "temp%d_emergency_hyst",
> +	[hwmon_temp_alarm] = "temp%d_alarm",
> +	[hwmon_temp_lcrit_alarm] = "temp%d_lcrit_alarm",
> +	[hwmon_temp_min_alarm] = "temp%d_min_alarm",
> +	[hwmon_temp_max_alarm] = "temp%d_max_alarm",
> +	[hwmon_temp_crit_alarm] = "temp%d_crit_alarm",
> +	[hwmon_temp_emergency_alarm] = "temp%d_emergency_alarm",
> +	[hwmon_temp_fault] = "temp%d_fault",
> +	[hwmon_temp_offset] = "temp%d_offset",
> +	[hwmon_temp_label] = "temp%d_label",
> +	[hwmon_temp_lowest] = "temp%d_lowest",
> +	[hwmon_temp_highest] = "temp%d_highest",
> +	[hwmon_temp_reset_history] = "temp%d_reset_history",
> +};
> +

This starts looking a lot like libsensors' code ;-)

> +static const char * const *__templates[] = {
> +	[hwmon_chip] = hwmon_chip_attr_templates,
> +	[hwmon_temp] = hwmon_temp_attr_templates,
> +};
> +
> +static const int __templates_size[] = {
> +	[hwmon_chip] = ARRAY_SIZE(hwmon_chip_attr_templates),
> +	[hwmon_temp] = ARRAY_SIZE(hwmon_temp_attr_templates),
> +};
> +
> +static int hwmon_num_channel_attrs(const struct hwmon_channel_info *info)
> +{
> +	int i, n;
> +
> +	for (i = n = 0; info->config[i]; i++)
> +		n += hweight32(info->config[i]);
> +
> +	return n;
> +}
> +
> +static int hwmon_genattrs(struct device *dev,
> +			  const void *drvdata,
> +			  struct attribute **attrs,
> +			  const struct hwmon_ops *ops,
> +			  const struct hwmon_channel_info *info)
> +{
> +	const char * const *templates;
> +	int template_size;
> +	int i, aindex = 0;
> +
> +	if (info->type >= ARRAY_SIZE(__templates))
> +		return -EINVAL;
> +
> +	templates = __templates[info->type];
> +	template_size = __templates_size[info->type];
> +
> +	for (i = 0; info->config[i]; i++) {
> +		u32 attr_mask = info->config[i];
> +		u32 attr;
> +
> +		while (attr_mask) {
> +			struct attribute *a;
> +
> +			attr = __ffs(attr_mask);
> +			attr_mask &= ~BIT(attr);
> +			if (attr >= template_size)
> +				return -EINVAL;
> +			a = hwmon_genattr(dev, drvdata, info->type, attr, i,
> +					  templates[attr], ops);
> +			if (IS_ERR(a)) {
> +				if (PTR_ERR(a) != -ENOENT)
> +					return PTR_ERR(a);
> +				continue;
> +			}
> +			attrs[aindex++] = a;
> +		}
> +	}
> +	return aindex;
> +}
> +
> +static struct attribute **
> +__hwmon_create_attrs(struct device *dev, const void *drvdata,
> +		     const struct hwmon_chip_info *chip)
> +{
> +	int ret, i, aindex = 0, nattrs = 0;
> +	struct attribute **attrs;
> +
> +	for (i = 0; chip->info[i]; i++)
> +		nattrs += hwmon_num_channel_attrs(chip->info[i]);
> +
> +	if (nattrs == 0)
> +		return ERR_PTR(-EINVAL);
> +
> +	attrs = devm_kcalloc(dev, nattrs + 1, sizeof(*attrs), GFP_KERNEL);
> +	if (!attrs)
> +		return ERR_PTR(-ENOMEM);
> +
> +	for (i = 0; chip->info[i]; i++) {
> +		ret = hwmon_genattrs(dev, drvdata, &attrs[aindex], chip->ops,
> +				     chip->info[i]);
> +		if (ret < 0)
> +			return ERR_PTR(ret);
> +		aindex += ret;
> +	}
> +
> +	return attrs;
> +}
> +
> +static struct device *
> +__hwmon_device_register(struct device *dev, const char *name, void *drvdata,
> +			const struct hwmon_chip_info *chip,
> +			const struct attribute_group **groups)
>  {
>  	struct hwmon_device *hwdev;
> -	int err, id;
> +	struct device *hdev;
> +	int i, j, err, id;
>  
>  	/* Do not accept invalid characters in hwmon name attribute */
>  	if (name && (!strlen(name) || strpbrk(name, "-* \t\n")))
> @@ -112,28 +401,128 @@ hwmon_device_register_with_groups(struct device *dev, const char *name,
>  		goto ida_remove;
>  	}
>  
> +	hdev = &hwdev->dev;
> +
> +	if (chip && chip->ops->is_visible) {

is_visible is documented as mandatory, so why would this check be needed
(and not result in an error if failed?)

> +		struct attribute **attrs;
> +		int ngroups = 2;

I'd appreciate a comment here.

> +
> +		if (groups)
> +			for (i = 0; groups[i]; i++)
> +				ngroups++;
> +
> +		hwdev->groups = devm_kcalloc(dev, ngroups, sizeof(*groups),
> +					     GFP_KERNEL);
> +		if (!hwdev->groups)
> +			return ERR_PTR(-ENOMEM);
> +
> +		attrs = __hwmon_create_attrs(dev, drvdata, chip);
> +		if (IS_ERR(attrs)) {
> +			err = PTR_ERR(attrs);
> +			goto free_hwmon;
> +		}
> +
> +		hwdev->group.attrs = attrs;
> +		ngroups = 0;
> +		hwdev->groups[ngroups++] = &hwdev->group;
> +
> +		if (groups) {
> +			for (i = 0; groups[i]; i++)
> +				hwdev->groups[ngroups++] = groups[i];
> +		}
> +
> +		hdev->groups = hwdev->groups;
> +	} else {
> +		hdev->groups = groups;
> +	}
> +
>  	hwdev->name = name;
> -	hwdev->dev.class = &hwmon_class;
> -	hwdev->dev.parent = dev;
> -	hwdev->dev.groups = groups;
> -	hwdev->dev.of_node = dev ? dev->of_node : NULL;
> -	dev_set_drvdata(&hwdev->dev, drvdata);
> -	dev_set_name(&hwdev->dev, HWMON_ID_FORMAT, id);
> -	err = device_register(&hwdev->dev);
> +	hdev->class = &hwmon_class;
> +	hdev->parent = dev;
> +	hdev->of_node = dev ? dev->of_node : NULL;
> +	hwdev->chip = chip;
> +	dev_set_drvdata(hdev, drvdata);
> +	dev_set_name(hdev, HWMON_ID_FORMAT, id);
> +	err = device_register(hdev);
>  	if (err)
> -		goto free;
> +		goto free_hwmon;
> +
> +	if (chip && chip->ops->is_visible && chip->ops->read &&
> +	    chip->info[0]->type == hwmon_chip &&
> +	    (chip->info[0]->config[0] & HWMON_C_REGISTER_TZ)) {
> +		const struct hwmon_channel_info **info = chip->info;
> +
> +		for (i = 1; info[i]; i++) {
> +			if (info[i]->type != hwmon_temp)
> +				continue;
> +
> +			for (j = 0; info[i]->config[j]; j++) {
> +				if (!chip->ops->is_visible(drvdata, hwmon_temp,
> +							   hwmon_temp_input, j))
> +					continue;
> +				if (info[i]->config[j] & HWMON_T_INPUT)
> +					hwmon_thermal_add_sensor(dev, hwdev, j);
> +			}
> +		}
> +	}
>  
> -	return &hwdev->dev;
> +	return hdev;
>  
> -free:
> +free_hwmon:
>  	kfree(hwdev);
>  ida_remove:
>  	ida_simple_remove(&hwmon_ida, id);
>  	return ERR_PTR(err);
>  }
> +
> +/**
> + * hwmon_device_register_with_groups - register w/ hwmon
> + * @dev: the parent device
> + * @name: hwmon name attribute
> + * @drvdata: driver data to attach to created device
> + * @groups: List of attribute groups to create
> + *
> + * hwmon_device_unregister() must be called when the device is no
> + * longer needed.
> + *
> + * Returns the pointer to the new device.
> + */
> +struct device *
> +hwmon_device_register_with_groups(struct device *dev, const char *name,
> +				  void *drvdata,
> +				  const struct attribute_group **groups)
> +{
> +	return __hwmon_device_register(dev, name, drvdata, NULL, groups);
> +}
>  EXPORT_SYMBOL_GPL(hwmon_device_register_with_groups);
>  
>  /**
> + * hwmon_device_register_with_info - register w/ hwmon
> + * @dev: the parent device
> + * @name: hwmon name attribute
> + * @drvdata: driver data to attach to created device
> + * @info: Pointer to hwmon chip information
> + * @groups - pointer to list of driver specific attribute groups

I don't find this clear. "driver specific" doesn't really tell me what
it is. My first reaction when reviewing the changes from an API
perspective was "the attributes would be generated from the
hwmon_chip_info, so why are we still passing them as a parameter?" Now I
seem to understand that "groups" here is meant for extra attributes
which aren't part of standard sysfs interface. This would be NULL for
most drivers, right?

To clear up the confusion, what about renaming this parameter to
"extra_groups"? And maybe rewording "driver specific" to "non-standard"?

As a side note I am wondering if it would be reasonable to limit it to a
single group, instead of a NULL-terminated array of groups. This would
make the code a little more efficient.

> + *
> + * hwmon_device_unregister() must be called when the device is no
> + * longer needed.
> + *
> + * Returns the pointer to the new device.
> + */
> +struct device *
> +hwmon_device_register_with_info(struct device *dev, const char *name,
> +				void *drvdata,
> +				const struct hwmon_chip_info *chip,
> +				const struct attribute_group **groups)
> +{
> +	if (chip && (!chip->ops || !chip->info))
> +		return ERR_PTR(-EINVAL);
> +
> +	return __hwmon_device_register(dev, name, drvdata, chip, groups);
> +}
> +EXPORT_SYMBOL_GPL(hwmon_device_register_with_info);
> +
> +/**
>   * hwmon_device_register - register w/ hwmon
>   * @dev: the device to register
>   *
> @@ -211,6 +600,48 @@ error:
>  }
>  EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_groups);
>  
> +/**
> + * devm_hwmon_device_register_with_info - register w/ hwmon
> + * @dev: the parent device
> + * @name: hwmon name attribute
> + * @drvdata: driver data to attach to created device
> + * @info: Pointer to hwmon chip information
> + * @groups - pointer to list of driver specific attribute groups
> + *
> + * Returns the pointer to the new device. The new device is automatically
> + * unregistered with the parent device.
> + */
> +struct device *
> +devm_hwmon_device_register_with_info(struct device *dev, const char *name,
> +				     void *drvdata,
> +				     const struct hwmon_chip_info *chip,
> +				     const struct attribute_group **groups)
> +{
> +	struct device **ptr, *hwdev;
> +
> +	if (!dev)
> +		return ERR_PTR(-EINVAL);
> +
> +	ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL);
> +	if (!ptr)
> +		return ERR_PTR(-ENOMEM);
> +
> +	hwdev = hwmon_device_register_with_info(dev, name, drvdata, chip,
> +						groups);
> +	if (IS_ERR(hwdev))
> +		goto error;
> +
> +	*ptr = hwdev;
> +	devres_add(dev, ptr);
> +
> +	return hwdev;
> +
> +error:
> +	devres_free(ptr);
> +	return hwdev;
> +}
> +EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_info);
> +
>  static int devm_hwmon_match(struct device *dev, void *res, void *data)
>  {
>  	struct device **hwdev = res;
> diff --git a/include/linux/hwmon.h b/include/linux/hwmon.h
> index 09354f6c1d63..52e56d71d742 100644
> --- a/include/linux/hwmon.h
> +++ b/include/linux/hwmon.h
> @@ -14,9 +14,147 @@
>  #ifndef _HWMON_H_
>  #define _HWMON_H_
>  
> +#include <linux/bitops.h>
> +
>  struct device;
>  struct attribute_group;
>  
> +enum hwmon_sensor_types {
> +	hwmon_chip,
> +	hwmon_temp,
> +	hwmon_in,
> +	hwmon_curr,
> +	hwmon_power,
> +	hwmon_energy,
> +};
> +
> +enum hwmon_chip_attributes {
> +	hwmon_chip_temp_reset_history,
> +	hwmon_chip_register_tz,
> +	hwmon_chip_update_interval,
> +	hwmon_chip_alarms,
> +};
> +
> +#define HWMON_C_TEMP_RESET_HISTORY	BIT(hwmon_chip_temp_reset_history)
> +#define HWMON_C_IN_RESET_HISTORY	BIT(hwmon_chip_in_reset_history)

Ideally this one should go in next patch ("Add voltage attribute support
to new API".)

> +#define HWMON_C_REGISTER_TZ		BIT(hwmon_chip_register_tz)
> +#define HWMON_C_UPDATE_INTERVAL		BIT(hwmon_chip_update_interval)
> +#define HWMON_C_ALARMS			BIT(hwmon_chip_alarms)
> +
> +enum hwmon_temp_attributes {
> +	hwmon_temp_input = 0,
> +	hwmon_temp_type,
> +	hwmon_temp_lcrit,
> +	hwmon_temp_lcrit_hyst,
> +	hwmon_temp_min,
> +	hwmon_temp_min_hyst,
> +	hwmon_temp_max,
> +	hwmon_temp_max_hyst,
> +	hwmon_temp_crit,
> +	hwmon_temp_crit_hyst,
> +	hwmon_temp_emergency,
> +	hwmon_temp_emergency_hyst,
> +	hwmon_temp_alarm,
> +	hwmon_temp_lcrit_alarm,
> +	hwmon_temp_min_alarm,
> +	hwmon_temp_max_alarm,
> +	hwmon_temp_crit_alarm,
> +	hwmon_temp_emergency_alarm,
> +	hwmon_temp_fault,
> +	hwmon_temp_offset,
> +	hwmon_temp_label,
> +	hwmon_temp_lowest,
> +	hwmon_temp_highest,
> +	hwmon_temp_reset_history,
> +};
> +
> +#define HWMON_T_INPUT		BIT(hwmon_temp_input)
> +#define HWMON_T_TYPE		BIT(hwmon_temp_type)
> +#define HWMON_T_LCRIT		BIT(hwmon_temp_lcrit)
> +#define HWMON_T_LCRIT_HYST	BIT(hwmon_temp_lcrit_hyst)
> +#define HWMON_T_MIN		BIT(hwmon_temp_min)
> +#define HWMON_T_MIN_HYST	BIT(hwmon_temp_min_hyst)
> +#define HWMON_T_MAX		BIT(hwmon_temp_max)
> +#define HWMON_T_MAX_HYST	BIT(hwmon_temp_max_hyst)
> +#define HWMON_T_CRIT		BIT(hwmon_temp_crit)
> +#define HWMON_T_CRIT_HYST	BIT(hwmon_temp_crit_hyst)
> +#define HWMON_T_EMERGENCY	BIT(hwmon_temp_emergency)
> +#define HWMON_T_EMERGENCY_HYST	BIT(hwmon_temp_emergency_hyst)
> +#define HWMON_T_MIN_ALARM	BIT(hwmon_temp_min_alarm)
> +#define HWMON_T_MAX_ALARM	BIT(hwmon_temp_max_alarm)
> +#define HWMON_T_CRIT_ALARM	BIT(hwmon_temp_crit_alarm)
> +#define HWMON_T_EMERGENCY_ALARM	BIT(hwmon_temp_emergency_alarm)
> +#define HWMON_T_FAULT		BIT(hwmon_temp_fault)
> +#define HWMON_T_OFFSET		BIT(hwmon_temp_offset)
> +#define HWMON_T_LABEL		BIT(hwmon_temp_label)
> +#define HWMON_T_LOWEST		BIT(hwmon_temp_lowest)
> +#define HWMON_T_HIGHEST		BIT(hwmon_temp_highest)
> +#define HWMON_T_RESET_HISTORY	BIT(hwmon_temp_reset_history)
> +
> +/**
> + * struct hwmon_ops - hwmon device operations
> + * @is_visible: Callback to return attribute visibility. Mandatory.
> + *		Parameters are:
> + *		@const void *drvdata:
> + *			Pointer to driver-private data structure passed
> + *			as argument to hwmon_device_register_with_info().
> + *		@type:	Sensor type
> + *		@attr:	Sensor attribute
> + *		@channel:
> + *			Channel number
> + *		The function returns the file permissions.
> + *		If the return value is 0, no attribute will be created.
> + * @read:       Read callback. Optional. If not provided, attributes
> + *		will not be readable.
> + *		Parameters are:
> + *		@dev:	Pointer to hardware monitoring device
> + *		@type:	Sensor type
> + *		@attr:	Sensor attribute
> + *		@channel:
> + *			Channel number
> + *		@val:	Pointer to returned value
> + *		The function returns 0 on success or a negative error number.
> + * @write:	Write callback. Optional. If not provided, attributes
> + *		will not be writable.
> + *		Parameters are:
> + *		@dev:	Pointer to hardware monitoring device
> + *		@type:	Sensor type
> + *		@attr:	Sensor attribute
> + *		@channel:
> + *			Channel number
> + *		@val:	Value to write
> + *		The function returns 0 on success or a negative error number.
> + */
> +struct hwmon_ops {
> +	umode_t (*is_visible)(const void *drvdata, enum hwmon_sensor_types type,
> +			      u32 attr, int channel);
> +	int (*read)(struct device *dev, enum hwmon_sensor_types type,
> +		    u32 attr, int channel, long *val);
> +	int (*write)(struct device *dev, enum hwmon_sensor_types type,
> +		     u32 attr, int channel, long val);
> +};
> +
> +/**
> + * Channel information
> + * @type:	Channel type.
> + * @config:	Pointer to NULL-terminated list of channel parameters.
> + *		Use for per-channel attributes.
> + */
> +struct hwmon_channel_info {
> +	enum hwmon_sensor_types type;
> +	const u32 *config;
> +};
> +
> +/**
> + * Chip configuration
> + * @ops:	Pointer to hwmon operations.
> + * @info:	Null-terminated list of channel information.
> + */
> +struct hwmon_chip_info {
> +	const struct hwmon_ops *ops;
> +	const struct hwmon_channel_info **info;
> +};
> +
>  struct device *hwmon_device_register(struct device *dev);
>  struct device *
>  hwmon_device_register_with_groups(struct device *dev, const char *name,
> @@ -26,6 +164,16 @@ struct device *
>  devm_hwmon_device_register_with_groups(struct device *dev, const char *name,
>  				       void *drvdata,
>  				       const struct attribute_group **groups);
> +struct device *
> +hwmon_device_register_with_info(struct device *dev,
> +				const char *name, void *drvdata,
> +				const struct hwmon_chip_info *info,
> +				const struct attribute_group **groups);
> +struct device *
> +devm_hwmon_device_register_with_info(struct device *dev,
> +				     const char *name, void *drvdata,
> +				     const struct hwmon_chip_info *info,
> +				     const struct attribute_group **groups);
>  
>  void hwmon_device_unregister(struct device *dev);
>  void devm_hwmon_device_unregister(struct device *dev);

This is adding a 4th and 5th way to register a hwmon device. Starts
being a bit messy... Do you have any plans to get rid of some of the
other functions to make things more consistent and efficient?

-- 
Jean Delvare
SUSE L3 Support

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH v3 2/9] hwmon: (core) New hwmon registration API
@ 2016-10-04  8:45     ` Jean Delvare
  0 siblings, 0 replies; 26+ messages in thread
From: Jean Delvare @ 2016-10-04  8:45 UTC (permalink / raw)
  To: Guenter Roeck
  Cc: Jonathan Cameron, Zhang Rui, Eduardo Valentin, Punit Agrawal,
	linux-pm-u79uwXL29TY76Z2rM5mHXA,
	linux-iio-u79uwXL29TY76Z2rM5mHXA,
	linux-hwmon-u79uwXL29TY76Z2rM5mHXA,
	linux-kernel-u79uwXL29TY76Z2rM5mHXA

Hi Guenter,

I see this patch is upstream now, but I had started reviewing it and
maybe some of my comments are still relevant.

It's not meant to be a complete review, which is why I had not sent it
yet :-(

Also I did not follow the first iterations of this patchset so my
apologies if I raise points which have already been discussed.

May I ask if any other subsystem is already doing anything like that?

FYI I gave a presentation about the hwmon device registration API
evolution last week at Kernel Recipes [1] in Paris and mentioned this
proposal.

[1] https://kernel-recipes.org/en/2016/

On dim., 2016-07-24 at 20:32 -0700, Guenter Roeck wrote:
> Up to now, each hwmon driver has to implement its own sysfs attributes.
> This requires a lot of template code, and distracts from the driver's core
> function to read and write chip registers.
> 
> To be able to reduce driver complexity, move sensor attribute handling
> and thermal zone registration into hwmon core. By using the new API,
> driver code and data size is typically reduced by 20-70%, depending
> on driver complexity and the number of sysfs attributes supported.

I looked at the diffstats for the drivers you have already converted and
couldn't see any such huge improvement... Some drivers appear to be even
larger after conversion?

> With this patch, the new API only supports thermal sensors. Support for
> other sensor types will be added with subsequent patches.
> 
> Acked-by: Punit Agrawal <punit.agrawal-5wv7dgnIgG8@public.gmane.org>
> Reviewed-by: Jonathan Cameron <jic23-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org>
> Signed-off-by: Guenter Roeck <linux-0h96xk9xTtrk1uMJSBkQmQ@public.gmane.org>
> ---
> v3:
> - Thermal registration depends on IS_REACHABLE(CONFIG_THERMAL) and
>   CONFIG_THERMAL_OF.
> v2:
> - Document callback function parameters of struct hwmon_ops in
>   include/linux/hwmon.h.
> - Clarify that the is_visible() callback is mandatory.
> - Initialize device attribute read/write callback functions unconditionally.
> - If an attribute has no template string, treat it as invisible, not as
>   error. Affected are virtual attributes such as HWMON_C_REGISTER_TZ.
> - Added newline to improve readability.
> 
> Review comments not addressed:
> - Stick with u32 for attribute masks. We could use u64, but it is currently
>   not needed, and changing it later would be straightforward.
> - Do not use for_each_set_bit() to walk attribute masks.
>   for_each_set_bit() expects a pointer to an unsigned long as argument,
>   which would make it difficult to switch to u64 attribute masks if/when
>   needed.
> 
>  drivers/hwmon/hwmon.c | 485 +++++++++++++++++++++++++++++++++++++++++++++++---
>  include/linux/hwmon.h | 148 +++++++++++++++
>  2 files changed, 606 insertions(+), 27 deletions(-)
> 
> diff --git a/drivers/hwmon/hwmon.c b/drivers/hwmon/hwmon.c
> index 649a68d119b4..3e4cc442a089 100644
> --- a/drivers/hwmon/hwmon.c
> +++ b/drivers/hwmon/hwmon.c

I believe it would make sense to add you copyright at the top of the
file. That's a pretty big change with lots of code.

> @@ -12,6 +12,7 @@
>  
>  #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
>  
> +#include <linux/bitops.h>
>  #include <linux/device.h>
>  #include <linux/err.h>
>  #include <linux/gfp.h>
> @@ -21,6 +22,7 @@
>  #include <linux/pci.h>
>  #include <linux/slab.h>
>  #include <linux/string.h>
> +#include <linux/thermal.h>
>  
>  #define HWMON_ID_PREFIX "hwmon"
>  #define HWMON_ID_FORMAT HWMON_ID_PREFIX "%d"
> @@ -28,9 +30,35 @@
>  struct hwmon_device {
>  	const char *name;
>  	struct device dev;
> +	const struct hwmon_chip_info *chip;
> +
> +	struct attribute_group group;
> +	const struct attribute_group **groups;
>  };
> +
>  #define to_hwmon_device(d) container_of(d, struct hwmon_device, dev)
>  
> +struct hwmon_device_attribute {
> +	struct device_attribute dev_attr;
> +	const struct hwmon_ops *ops;
> +	enum hwmon_sensor_types type;
> +	u32 attr;
> +	int index;
> +};
> +
> +#define to_hwmon_attr(d) \
> +	container_of(d, struct hwmon_device_attribute, dev_attr)
> +
> +/*
> + * Thermal zone information
> + * In addition to the reference to the hwmon device,
> + * also provides the sensor index.
> + */
> +struct hwmon_thermal_data {
> +	struct hwmon_device *hwdev;	/* Reference to hwmon device */
> +	int index;			/* sensor index */
> +};
> +
>  static ssize_t
>  show_name(struct device *dev, struct device_attribute *attr, char *buf)
>  {
> @@ -78,25 +106,286 @@ static struct class hwmon_class = {
>  
>  static DEFINE_IDA(hwmon_ida);
>  
> -/**
> - * hwmon_device_register_with_groups - register w/ hwmon
> - * @dev: the parent device
> - * @name: hwmon name attribute
> - * @drvdata: driver data to attach to created device
> - * @groups: List of attribute groups to create
> - *
> - * hwmon_device_unregister() must be called when the device is no
> - * longer needed.
> - *
> - * Returns the pointer to the new device.
> - */
> -struct device *
> -hwmon_device_register_with_groups(struct device *dev, const char *name,
> -				  void *drvdata,
> -				  const struct attribute_group **groups)
> +/* Thermal zone handling */
> +
> +#if IS_REACHABLE(CONFIG_THERMAL) && defined(CONFIG_THERMAL_OF)
> +static int hwmon_thermal_get_temp(void *data, int *temp)
> +{
> +	struct hwmon_thermal_data *tdata = data;
> +	struct hwmon_device *hwdev = tdata->hwdev;
> +	int ret;
> +	long t;
> +
> +	ret = hwdev->chip->ops->read(&hwdev->dev, hwmon_temp, hwmon_temp_input,
> +				     tdata->index, &t);
> +	if (ret < 0)
> +		return ret;
> +
> +	*temp = t;
> +
> +	return 0;
> +}
> +
> +static struct thermal_zone_of_device_ops hwmon_thermal_ops = {
> +	.get_temp = hwmon_thermal_get_temp,
> +};
> +
> +static int hwmon_thermal_add_sensor(struct device *dev,
> +				    struct hwmon_device *hwdev, int index)
> +{
> +	struct hwmon_thermal_data *tdata;
> +
> +	tdata = devm_kzalloc(dev, sizeof(*tdata), GFP_KERNEL);
> +	if (!tdata)
> +		return -ENOMEM;
> +
> +	tdata->hwdev = hwdev;
> +	tdata->index = index;
> +
> +	devm_thermal_zone_of_sensor_register(&hwdev->dev, index, tdata,
> +					     &hwmon_thermal_ops);
> +
> +	return 0;
> +}
> +#else
> +static int hwmon_thermal_add_sensor(struct device *dev,
> +				    struct hwmon_device *hwdev, int index)
> +{
> +	return 0;
> +}
> +#endif /* IS_REACHABLE(CONFIG_THERMAL) && defined(CONFIG_THERMAL_OF) */
> +
> +/* sysfs attribute management */
> +
> +static ssize_t hwmon_attr_show(struct device *dev,
> +			       struct device_attribute *devattr, char *buf)
> +{
> +	struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
> +	long val;
> +	int ret;
> +
> +	ret = hattr->ops->read(dev, hattr->type, hattr->attr, hattr->index,
> +			       &val);
> +	if (ret < 0)
> +		return ret;
> +
> +	return sprintf(buf, "%ld\n", val);
> +}
> +
> +static ssize_t hwmon_attr_store(struct device *dev,
> +				struct device_attribute *devattr,
> +				const char *buf, size_t count)
> +{
> +	struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
> +	long val;
> +	int ret;
> +
> +	ret = kstrtol(buf, 10, &val);
> +	if (ret < 0)
> +		return ret;
> +
> +	ret = hattr->ops->write(dev, hattr->type, hattr->attr, hattr->index,
> +				val);
> +	if (ret < 0)
> +		return ret;
> +
> +	return count;
> +}
> +
> +static int hwmon_attr_base(enum hwmon_sensor_types type)
> +{
> +	if (type == hwmon_in)
> +		return 0;
> +	return 1;
> +}
> +
> +static struct attribute *hwmon_genattr(struct device *dev,
> +				       const void *drvdata,
> +				       enum hwmon_sensor_types type,
> +				       u32 attr,
> +				       int index,
> +				       const char *template,
> +				       const struct hwmon_ops *ops)
> +{
> +	struct hwmon_device_attribute *hattr;
> +	struct device_attribute *dattr;
> +	struct attribute *a;
> +	umode_t mode;
> +	char *name;
> +
> +	/* The attribute is invisible if there is no template string */
> +	if (!template)
> +		return ERR_PTR(-ENOENT);
> +
> +	mode = ops->is_visible(drvdata, type, attr, index);

Any reason why you don't simply attach the provided ->is_visible to the
attribute group and let the driver core do the work?

> +	if (!mode)
> +		return ERR_PTR(-ENOENT);
> +
> +	if ((mode & S_IRUGO) && !ops->read)
> +		return ERR_PTR(-EINVAL);
> +	if ((mode & S_IWUGO) && !ops->write)
> +		return ERR_PTR(-EINVAL);
> +
> +	if (type == hwmon_chip) {

This needs a comment. It's not obvious what "hwmon_chip" is supposed to
represent. From what I understand, maybe "hwmon_unique" would be a more
appropriate name.

> +		name = (char *)template;
> +	} else {
> +		name = devm_kzalloc(dev, strlen(template) + 16, GFP_KERNEL);
> +		if (!name)
> +			return ERR_PTR(-ENOMEM);
> +		scnprintf(name, strlen(template) + 16, template,
> +			  index + hwmon_attr_base(type));
> +	}
> +
> +	hattr = devm_kzalloc(dev, sizeof(*hattr), GFP_KERNEL);
> +	if (!hattr)
> +		return ERR_PTR(-ENOMEM);

So basically you are doing 1 or 2 memory allocations for every
attribute? Looks quite bad from a memory fragmentation
perspective :-( Not to mention performance. Given that you have all the
data at hand before you start, can't you preallocate an array with the
right number of hattr and pick from it? That would at least solve half
of the problem.

Something similar for string allocation may work too, although it's
somewhat more complex due to the variable length. But I think the
abituguru3 driver is doing it right, so maybe you can too.

> +
> +	hattr->type = type;
> +	hattr->attr = attr;
> +	hattr->index = index;
> +	hattr->ops = ops;
> +
> +	dattr = &hattr->dev_attr;
> +	dattr->show = hwmon_attr_show;
> +	dattr->store = hwmon_attr_store;
> +
> +	a = &dattr->attr;
> +	sysfs_attr_init(a);
> +	a->name = name;
> +	a->mode = mode;
> +
> +	return a;
> +}
> +
> +static const char * const hwmon_chip_attr_templates[] = {
> +	[hwmon_chip_temp_reset_history] = "temp_reset_history",
> +	[hwmon_chip_update_interval] = "update_interval",
> +	[hwmon_chip_alarms] = "alarms",
> +};
> +
> +static const char * const hwmon_temp_attr_templates[] = {
> +	[hwmon_temp_input] = "temp%d_input",
> +	[hwmon_temp_type] = "temp%d_type",
> +	[hwmon_temp_lcrit] = "temp%d_lcrit",
> +	[hwmon_temp_lcrit_hyst] = "temp%d_lcrit_hyst",
> +	[hwmon_temp_min] = "temp%d_min",
> +	[hwmon_temp_min_hyst] = "temp%d_min_hyst",
> +	[hwmon_temp_max] = "temp%d_max",
> +	[hwmon_temp_max_hyst] = "temp%d_max_hyst",
> +	[hwmon_temp_crit] = "temp%d_crit",
> +	[hwmon_temp_crit_hyst] = "temp%d_crit_hyst",
> +	[hwmon_temp_emergency] = "temp%d_emergency",
> +	[hwmon_temp_emergency_hyst] = "temp%d_emergency_hyst",
> +	[hwmon_temp_alarm] = "temp%d_alarm",
> +	[hwmon_temp_lcrit_alarm] = "temp%d_lcrit_alarm",
> +	[hwmon_temp_min_alarm] = "temp%d_min_alarm",
> +	[hwmon_temp_max_alarm] = "temp%d_max_alarm",
> +	[hwmon_temp_crit_alarm] = "temp%d_crit_alarm",
> +	[hwmon_temp_emergency_alarm] = "temp%d_emergency_alarm",
> +	[hwmon_temp_fault] = "temp%d_fault",
> +	[hwmon_temp_offset] = "temp%d_offset",
> +	[hwmon_temp_label] = "temp%d_label",
> +	[hwmon_temp_lowest] = "temp%d_lowest",
> +	[hwmon_temp_highest] = "temp%d_highest",
> +	[hwmon_temp_reset_history] = "temp%d_reset_history",
> +};
> +

This starts looking a lot like libsensors' code ;-)

> +static const char * const *__templates[] = {
> +	[hwmon_chip] = hwmon_chip_attr_templates,
> +	[hwmon_temp] = hwmon_temp_attr_templates,
> +};
> +
> +static const int __templates_size[] = {
> +	[hwmon_chip] = ARRAY_SIZE(hwmon_chip_attr_templates),
> +	[hwmon_temp] = ARRAY_SIZE(hwmon_temp_attr_templates),
> +};
> +
> +static int hwmon_num_channel_attrs(const struct hwmon_channel_info *info)
> +{
> +	int i, n;
> +
> +	for (i = n = 0; info->config[i]; i++)
> +		n += hweight32(info->config[i]);
> +
> +	return n;
> +}
> +
> +static int hwmon_genattrs(struct device *dev,
> +			  const void *drvdata,
> +			  struct attribute **attrs,
> +			  const struct hwmon_ops *ops,
> +			  const struct hwmon_channel_info *info)
> +{
> +	const char * const *templates;
> +	int template_size;
> +	int i, aindex = 0;
> +
> +	if (info->type >= ARRAY_SIZE(__templates))
> +		return -EINVAL;
> +
> +	templates = __templates[info->type];
> +	template_size = __templates_size[info->type];
> +
> +	for (i = 0; info->config[i]; i++) {
> +		u32 attr_mask = info->config[i];
> +		u32 attr;
> +
> +		while (attr_mask) {
> +			struct attribute *a;
> +
> +			attr = __ffs(attr_mask);
> +			attr_mask &= ~BIT(attr);
> +			if (attr >= template_size)
> +				return -EINVAL;
> +			a = hwmon_genattr(dev, drvdata, info->type, attr, i,
> +					  templates[attr], ops);
> +			if (IS_ERR(a)) {
> +				if (PTR_ERR(a) != -ENOENT)
> +					return PTR_ERR(a);
> +				continue;
> +			}
> +			attrs[aindex++] = a;
> +		}
> +	}
> +	return aindex;
> +}
> +
> +static struct attribute **
> +__hwmon_create_attrs(struct device *dev, const void *drvdata,
> +		     const struct hwmon_chip_info *chip)
> +{
> +	int ret, i, aindex = 0, nattrs = 0;
> +	struct attribute **attrs;
> +
> +	for (i = 0; chip->info[i]; i++)
> +		nattrs += hwmon_num_channel_attrs(chip->info[i]);
> +
> +	if (nattrs == 0)
> +		return ERR_PTR(-EINVAL);
> +
> +	attrs = devm_kcalloc(dev, nattrs + 1, sizeof(*attrs), GFP_KERNEL);
> +	if (!attrs)
> +		return ERR_PTR(-ENOMEM);
> +
> +	for (i = 0; chip->info[i]; i++) {
> +		ret = hwmon_genattrs(dev, drvdata, &attrs[aindex], chip->ops,
> +				     chip->info[i]);
> +		if (ret < 0)
> +			return ERR_PTR(ret);
> +		aindex += ret;
> +	}
> +
> +	return attrs;
> +}
> +
> +static struct device *
> +__hwmon_device_register(struct device *dev, const char *name, void *drvdata,
> +			const struct hwmon_chip_info *chip,
> +			const struct attribute_group **groups)
>  {
>  	struct hwmon_device *hwdev;
> -	int err, id;
> +	struct device *hdev;
> +	int i, j, err, id;
>  
>  	/* Do not accept invalid characters in hwmon name attribute */
>  	if (name && (!strlen(name) || strpbrk(name, "-* \t\n")))
> @@ -112,28 +401,128 @@ hwmon_device_register_with_groups(struct device *dev, const char *name,
>  		goto ida_remove;
>  	}
>  
> +	hdev = &hwdev->dev;
> +
> +	if (chip && chip->ops->is_visible) {

is_visible is documented as mandatory, so why would this check be needed
(and not result in an error if failed?)

> +		struct attribute **attrs;
> +		int ngroups = 2;

I'd appreciate a comment here.

> +
> +		if (groups)
> +			for (i = 0; groups[i]; i++)
> +				ngroups++;
> +
> +		hwdev->groups = devm_kcalloc(dev, ngroups, sizeof(*groups),
> +					     GFP_KERNEL);
> +		if (!hwdev->groups)
> +			return ERR_PTR(-ENOMEM);
> +
> +		attrs = __hwmon_create_attrs(dev, drvdata, chip);
> +		if (IS_ERR(attrs)) {
> +			err = PTR_ERR(attrs);
> +			goto free_hwmon;
> +		}
> +
> +		hwdev->group.attrs = attrs;
> +		ngroups = 0;
> +		hwdev->groups[ngroups++] = &hwdev->group;
> +
> +		if (groups) {
> +			for (i = 0; groups[i]; i++)
> +				hwdev->groups[ngroups++] = groups[i];
> +		}
> +
> +		hdev->groups = hwdev->groups;
> +	} else {
> +		hdev->groups = groups;
> +	}
> +
>  	hwdev->name = name;
> -	hwdev->dev.class = &hwmon_class;
> -	hwdev->dev.parent = dev;
> -	hwdev->dev.groups = groups;
> -	hwdev->dev.of_node = dev ? dev->of_node : NULL;
> -	dev_set_drvdata(&hwdev->dev, drvdata);
> -	dev_set_name(&hwdev->dev, HWMON_ID_FORMAT, id);
> -	err = device_register(&hwdev->dev);
> +	hdev->class = &hwmon_class;
> +	hdev->parent = dev;
> +	hdev->of_node = dev ? dev->of_node : NULL;
> +	hwdev->chip = chip;
> +	dev_set_drvdata(hdev, drvdata);
> +	dev_set_name(hdev, HWMON_ID_FORMAT, id);
> +	err = device_register(hdev);
>  	if (err)
> -		goto free;
> +		goto free_hwmon;
> +
> +	if (chip && chip->ops->is_visible && chip->ops->read &&
> +	    chip->info[0]->type == hwmon_chip &&
> +	    (chip->info[0]->config[0] & HWMON_C_REGISTER_TZ)) {
> +		const struct hwmon_channel_info **info = chip->info;
> +
> +		for (i = 1; info[i]; i++) {
> +			if (info[i]->type != hwmon_temp)
> +				continue;
> +
> +			for (j = 0; info[i]->config[j]; j++) {
> +				if (!chip->ops->is_visible(drvdata, hwmon_temp,
> +							   hwmon_temp_input, j))
> +					continue;
> +				if (info[i]->config[j] & HWMON_T_INPUT)
> +					hwmon_thermal_add_sensor(dev, hwdev, j);
> +			}
> +		}
> +	}
>  
> -	return &hwdev->dev;
> +	return hdev;
>  
> -free:
> +free_hwmon:
>  	kfree(hwdev);
>  ida_remove:
>  	ida_simple_remove(&hwmon_ida, id);
>  	return ERR_PTR(err);
>  }
> +
> +/**
> + * hwmon_device_register_with_groups - register w/ hwmon
> + * @dev: the parent device
> + * @name: hwmon name attribute
> + * @drvdata: driver data to attach to created device
> + * @groups: List of attribute groups to create
> + *
> + * hwmon_device_unregister() must be called when the device is no
> + * longer needed.
> + *
> + * Returns the pointer to the new device.
> + */
> +struct device *
> +hwmon_device_register_with_groups(struct device *dev, const char *name,
> +				  void *drvdata,
> +				  const struct attribute_group **groups)
> +{
> +	return __hwmon_device_register(dev, name, drvdata, NULL, groups);
> +}
>  EXPORT_SYMBOL_GPL(hwmon_device_register_with_groups);
>  
>  /**
> + * hwmon_device_register_with_info - register w/ hwmon
> + * @dev: the parent device
> + * @name: hwmon name attribute
> + * @drvdata: driver data to attach to created device
> + * @info: Pointer to hwmon chip information
> + * @groups - pointer to list of driver specific attribute groups

I don't find this clear. "driver specific" doesn't really tell me what
it is. My first reaction when reviewing the changes from an API
perspective was "the attributes would be generated from the
hwmon_chip_info, so why are we still passing them as a parameter?" Now I
seem to understand that "groups" here is meant for extra attributes
which aren't part of standard sysfs interface. This would be NULL for
most drivers, right?

To clear up the confusion, what about renaming this parameter to
"extra_groups"? And maybe rewording "driver specific" to "non-standard"?

As a side note I am wondering if it would be reasonable to limit it to a
single group, instead of a NULL-terminated array of groups. This would
make the code a little more efficient.

> + *
> + * hwmon_device_unregister() must be called when the device is no
> + * longer needed.
> + *
> + * Returns the pointer to the new device.
> + */
> +struct device *
> +hwmon_device_register_with_info(struct device *dev, const char *name,
> +				void *drvdata,
> +				const struct hwmon_chip_info *chip,
> +				const struct attribute_group **groups)
> +{
> +	if (chip && (!chip->ops || !chip->info))
> +		return ERR_PTR(-EINVAL);
> +
> +	return __hwmon_device_register(dev, name, drvdata, chip, groups);
> +}
> +EXPORT_SYMBOL_GPL(hwmon_device_register_with_info);
> +
> +/**
>   * hwmon_device_register - register w/ hwmon
>   * @dev: the device to register
>   *
> @@ -211,6 +600,48 @@ error:
>  }
>  EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_groups);
>  
> +/**
> + * devm_hwmon_device_register_with_info - register w/ hwmon
> + * @dev: the parent device
> + * @name: hwmon name attribute
> + * @drvdata: driver data to attach to created device
> + * @info: Pointer to hwmon chip information
> + * @groups - pointer to list of driver specific attribute groups
> + *
> + * Returns the pointer to the new device. The new device is automatically
> + * unregistered with the parent device.
> + */
> +struct device *
> +devm_hwmon_device_register_with_info(struct device *dev, const char *name,
> +				     void *drvdata,
> +				     const struct hwmon_chip_info *chip,
> +				     const struct attribute_group **groups)
> +{
> +	struct device **ptr, *hwdev;
> +
> +	if (!dev)
> +		return ERR_PTR(-EINVAL);
> +
> +	ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL);
> +	if (!ptr)
> +		return ERR_PTR(-ENOMEM);
> +
> +	hwdev = hwmon_device_register_with_info(dev, name, drvdata, chip,
> +						groups);
> +	if (IS_ERR(hwdev))
> +		goto error;
> +
> +	*ptr = hwdev;
> +	devres_add(dev, ptr);
> +
> +	return hwdev;
> +
> +error:
> +	devres_free(ptr);
> +	return hwdev;
> +}
> +EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_info);
> +
>  static int devm_hwmon_match(struct device *dev, void *res, void *data)
>  {
>  	struct device **hwdev = res;
> diff --git a/include/linux/hwmon.h b/include/linux/hwmon.h
> index 09354f6c1d63..52e56d71d742 100644
> --- a/include/linux/hwmon.h
> +++ b/include/linux/hwmon.h
> @@ -14,9 +14,147 @@
>  #ifndef _HWMON_H_
>  #define _HWMON_H_
>  
> +#include <linux/bitops.h>
> +
>  struct device;
>  struct attribute_group;
>  
> +enum hwmon_sensor_types {
> +	hwmon_chip,
> +	hwmon_temp,
> +	hwmon_in,
> +	hwmon_curr,
> +	hwmon_power,
> +	hwmon_energy,
> +};
> +
> +enum hwmon_chip_attributes {
> +	hwmon_chip_temp_reset_history,
> +	hwmon_chip_register_tz,
> +	hwmon_chip_update_interval,
> +	hwmon_chip_alarms,
> +};
> +
> +#define HWMON_C_TEMP_RESET_HISTORY	BIT(hwmon_chip_temp_reset_history)
> +#define HWMON_C_IN_RESET_HISTORY	BIT(hwmon_chip_in_reset_history)

Ideally this one should go in next patch ("Add voltage attribute support
to new API".)

> +#define HWMON_C_REGISTER_TZ		BIT(hwmon_chip_register_tz)
> +#define HWMON_C_UPDATE_INTERVAL		BIT(hwmon_chip_update_interval)
> +#define HWMON_C_ALARMS			BIT(hwmon_chip_alarms)
> +
> +enum hwmon_temp_attributes {
> +	hwmon_temp_input = 0,
> +	hwmon_temp_type,
> +	hwmon_temp_lcrit,
> +	hwmon_temp_lcrit_hyst,
> +	hwmon_temp_min,
> +	hwmon_temp_min_hyst,
> +	hwmon_temp_max,
> +	hwmon_temp_max_hyst,
> +	hwmon_temp_crit,
> +	hwmon_temp_crit_hyst,
> +	hwmon_temp_emergency,
> +	hwmon_temp_emergency_hyst,
> +	hwmon_temp_alarm,
> +	hwmon_temp_lcrit_alarm,
> +	hwmon_temp_min_alarm,
> +	hwmon_temp_max_alarm,
> +	hwmon_temp_crit_alarm,
> +	hwmon_temp_emergency_alarm,
> +	hwmon_temp_fault,
> +	hwmon_temp_offset,
> +	hwmon_temp_label,
> +	hwmon_temp_lowest,
> +	hwmon_temp_highest,
> +	hwmon_temp_reset_history,
> +};
> +
> +#define HWMON_T_INPUT		BIT(hwmon_temp_input)
> +#define HWMON_T_TYPE		BIT(hwmon_temp_type)
> +#define HWMON_T_LCRIT		BIT(hwmon_temp_lcrit)
> +#define HWMON_T_LCRIT_HYST	BIT(hwmon_temp_lcrit_hyst)
> +#define HWMON_T_MIN		BIT(hwmon_temp_min)
> +#define HWMON_T_MIN_HYST	BIT(hwmon_temp_min_hyst)
> +#define HWMON_T_MAX		BIT(hwmon_temp_max)
> +#define HWMON_T_MAX_HYST	BIT(hwmon_temp_max_hyst)
> +#define HWMON_T_CRIT		BIT(hwmon_temp_crit)
> +#define HWMON_T_CRIT_HYST	BIT(hwmon_temp_crit_hyst)
> +#define HWMON_T_EMERGENCY	BIT(hwmon_temp_emergency)
> +#define HWMON_T_EMERGENCY_HYST	BIT(hwmon_temp_emergency_hyst)
> +#define HWMON_T_MIN_ALARM	BIT(hwmon_temp_min_alarm)
> +#define HWMON_T_MAX_ALARM	BIT(hwmon_temp_max_alarm)
> +#define HWMON_T_CRIT_ALARM	BIT(hwmon_temp_crit_alarm)
> +#define HWMON_T_EMERGENCY_ALARM	BIT(hwmon_temp_emergency_alarm)
> +#define HWMON_T_FAULT		BIT(hwmon_temp_fault)
> +#define HWMON_T_OFFSET		BIT(hwmon_temp_offset)
> +#define HWMON_T_LABEL		BIT(hwmon_temp_label)
> +#define HWMON_T_LOWEST		BIT(hwmon_temp_lowest)
> +#define HWMON_T_HIGHEST		BIT(hwmon_temp_highest)
> +#define HWMON_T_RESET_HISTORY	BIT(hwmon_temp_reset_history)
> +
> +/**
> + * struct hwmon_ops - hwmon device operations
> + * @is_visible: Callback to return attribute visibility. Mandatory.
> + *		Parameters are:
> + *		@const void *drvdata:
> + *			Pointer to driver-private data structure passed
> + *			as argument to hwmon_device_register_with_info().
> + *		@type:	Sensor type
> + *		@attr:	Sensor attribute
> + *		@channel:
> + *			Channel number
> + *		The function returns the file permissions.
> + *		If the return value is 0, no attribute will be created.
> + * @read:       Read callback. Optional. If not provided, attributes
> + *		will not be readable.
> + *		Parameters are:
> + *		@dev:	Pointer to hardware monitoring device
> + *		@type:	Sensor type
> + *		@attr:	Sensor attribute
> + *		@channel:
> + *			Channel number
> + *		@val:	Pointer to returned value
> + *		The function returns 0 on success or a negative error number.
> + * @write:	Write callback. Optional. If not provided, attributes
> + *		will not be writable.
> + *		Parameters are:
> + *		@dev:	Pointer to hardware monitoring device
> + *		@type:	Sensor type
> + *		@attr:	Sensor attribute
> + *		@channel:
> + *			Channel number
> + *		@val:	Value to write
> + *		The function returns 0 on success or a negative error number.
> + */
> +struct hwmon_ops {
> +	umode_t (*is_visible)(const void *drvdata, enum hwmon_sensor_types type,
> +			      u32 attr, int channel);
> +	int (*read)(struct device *dev, enum hwmon_sensor_types type,
> +		    u32 attr, int channel, long *val);
> +	int (*write)(struct device *dev, enum hwmon_sensor_types type,
> +		     u32 attr, int channel, long val);
> +};
> +
> +/**
> + * Channel information
> + * @type:	Channel type.
> + * @config:	Pointer to NULL-terminated list of channel parameters.
> + *		Use for per-channel attributes.
> + */
> +struct hwmon_channel_info {
> +	enum hwmon_sensor_types type;
> +	const u32 *config;
> +};
> +
> +/**
> + * Chip configuration
> + * @ops:	Pointer to hwmon operations.
> + * @info:	Null-terminated list of channel information.
> + */
> +struct hwmon_chip_info {
> +	const struct hwmon_ops *ops;
> +	const struct hwmon_channel_info **info;
> +};
> +
>  struct device *hwmon_device_register(struct device *dev);
>  struct device *
>  hwmon_device_register_with_groups(struct device *dev, const char *name,
> @@ -26,6 +164,16 @@ struct device *
>  devm_hwmon_device_register_with_groups(struct device *dev, const char *name,
>  				       void *drvdata,
>  				       const struct attribute_group **groups);
> +struct device *
> +hwmon_device_register_with_info(struct device *dev,
> +				const char *name, void *drvdata,
> +				const struct hwmon_chip_info *info,
> +				const struct attribute_group **groups);
> +struct device *
> +devm_hwmon_device_register_with_info(struct device *dev,
> +				     const char *name, void *drvdata,
> +				     const struct hwmon_chip_info *info,
> +				     const struct attribute_group **groups);
>  
>  void hwmon_device_unregister(struct device *dev);
>  void devm_hwmon_device_unregister(struct device *dev);

This is adding a 4th and 5th way to register a hwmon device. Starts
being a bit messy... Do you have any plans to get rid of some of the
other functions to make things more consistent and efficient?

-- 
Jean Delvare
SUSE L3 Support

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH v3 2/9] hwmon: (core) New hwmon registration API
@ 2016-10-04 19:37       ` Guenter Roeck
  0 siblings, 0 replies; 26+ messages in thread
From: Guenter Roeck @ 2016-10-04 19:37 UTC (permalink / raw)
  To: Jean Delvare
  Cc: Jonathan Cameron, Zhang Rui, Eduardo Valentin, Punit Agrawal,
	linux-pm, linux-iio, linux-hwmon, linux-kernel

Hi Jean,

On Tue, Oct 04, 2016 at 10:45:59AM +0200, Jean Delvare wrote:
> Hi Guenter,
> 
> I see this patch is upstream now, but I had started reviewing it and
> maybe some of my comments are still relevant.
> 
As always, I appreciate the feedback. I'll be happy to submit follow-up
patches to address any concerns.

> It's not meant to be a complete review, which is why I had not sent it
> yet :-(
> 
> Also I did not follow the first iterations of this patchset so my
> apologies if I raise points which have already been discussed.
> 
> May I ask if any other subsystem is already doing anything like that?
> 
Depends what you mean with "anything like that". The API is inspired by
the iio API and a little by the thermal API. The details are probably
unique, though.

> FYI I gave a presentation about the hwmon device registration API
> evolution last week at Kernel Recipes [1] in Paris and mentioned this
> proposal.
> 
> [1] https://kernel-recipes.org/en/2016/
> 
> On dim., 2016-07-24 at 20:32 -0700, Guenter Roeck wrote:
> > Up to now, each hwmon driver has to implement its own sysfs attributes.
> > This requires a lot of template code, and distracts from the driver's core
> > function to read and write chip registers.
> > 
> > To be able to reduce driver complexity, move sensor attribute handling
> > and thermal zone registration into hwmon core. By using the new API,
> > driver code and data size is typically reduced by 20-70%, depending
> > on driver complexity and the number of sysfs attributes supported.
> 
> I looked at the diffstats for the drivers you have already converted and
> couldn't see any such huge improvement... Some drivers appear to be even
> larger after conversion?
> 
The above refers to code and data sizes.

> > With this patch, the new API only supports thermal sensors. Support for
> > other sensor types will be added with subsequent patches.
> > 
> > Acked-by: Punit Agrawal <punit.agrawal@arm.com>
> > Reviewed-by: Jonathan Cameron <jic23@kernel.org>
> > Signed-off-by: Guenter Roeck <linux@roeck-us.net>
> > ---
> > v3:
> > - Thermal registration depends on IS_REACHABLE(CONFIG_THERMAL) and
> >   CONFIG_THERMAL_OF.
> > v2:
> > - Document callback function parameters of struct hwmon_ops in
> >   include/linux/hwmon.h.
> > - Clarify that the is_visible() callback is mandatory.
> > - Initialize device attribute read/write callback functions unconditionally.
> > - If an attribute has no template string, treat it as invisible, not as
> >   error. Affected are virtual attributes such as HWMON_C_REGISTER_TZ.
> > - Added newline to improve readability.
> > 
> > Review comments not addressed:
> > - Stick with u32 for attribute masks. We could use u64, but it is currently
> >   not needed, and changing it later would be straightforward.
> > - Do not use for_each_set_bit() to walk attribute masks.
> >   for_each_set_bit() expects a pointer to an unsigned long as argument,
> >   which would make it difficult to switch to u64 attribute masks if/when
> >   needed.
> > 
> >  drivers/hwmon/hwmon.c | 485 +++++++++++++++++++++++++++++++++++++++++++++++---
> >  include/linux/hwmon.h | 148 +++++++++++++++
> >  2 files changed, 606 insertions(+), 27 deletions(-)
> > 
> > diff --git a/drivers/hwmon/hwmon.c b/drivers/hwmon/hwmon.c
> > index 649a68d119b4..3e4cc442a089 100644
> > --- a/drivers/hwmon/hwmon.c
> > +++ b/drivers/hwmon/hwmon.c
> 
> I believe it would make sense to add you copyright at the top of the
> file. That's a pretty big change with lots of code.
> 
I don't really feel too strong about that. But, sure, can do.

> > @@ -12,6 +12,7 @@
> >  
> >  #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
> >  
> > +#include <linux/bitops.h>
> >  #include <linux/device.h>
> >  #include <linux/err.h>
> >  #include <linux/gfp.h>
> > @@ -21,6 +22,7 @@
> >  #include <linux/pci.h>
> >  #include <linux/slab.h>
> >  #include <linux/string.h>
> > +#include <linux/thermal.h>
> >  
> >  #define HWMON_ID_PREFIX "hwmon"
> >  #define HWMON_ID_FORMAT HWMON_ID_PREFIX "%d"
> > @@ -28,9 +30,35 @@
> >  struct hwmon_device {
> >  	const char *name;
> >  	struct device dev;
> > +	const struct hwmon_chip_info *chip;
> > +
> > +	struct attribute_group group;
> > +	const struct attribute_group **groups;
> >  };
> > +
> >  #define to_hwmon_device(d) container_of(d, struct hwmon_device, dev)
> >  
> > +struct hwmon_device_attribute {
> > +	struct device_attribute dev_attr;
> > +	const struct hwmon_ops *ops;
> > +	enum hwmon_sensor_types type;
> > +	u32 attr;
> > +	int index;
> > +};
> > +
> > +#define to_hwmon_attr(d) \
> > +	container_of(d, struct hwmon_device_attribute, dev_attr)
> > +
> > +/*
> > + * Thermal zone information
> > + * In addition to the reference to the hwmon device,
> > + * also provides the sensor index.
> > + */
> > +struct hwmon_thermal_data {
> > +	struct hwmon_device *hwdev;	/* Reference to hwmon device */
> > +	int index;			/* sensor index */
> > +};
> > +
> >  static ssize_t
> >  show_name(struct device *dev, struct device_attribute *attr, char *buf)
> >  {
> > @@ -78,25 +106,286 @@ static struct class hwmon_class = {
> >  
> >  static DEFINE_IDA(hwmon_ida);
> >  
> > -/**
> > - * hwmon_device_register_with_groups - register w/ hwmon
> > - * @dev: the parent device
> > - * @name: hwmon name attribute
> > - * @drvdata: driver data to attach to created device
> > - * @groups: List of attribute groups to create
> > - *
> > - * hwmon_device_unregister() must be called when the device is no
> > - * longer needed.
> > - *
> > - * Returns the pointer to the new device.
> > - */
> > -struct device *
> > -hwmon_device_register_with_groups(struct device *dev, const char *name,
> > -				  void *drvdata,
> > -				  const struct attribute_group **groups)
> > +/* Thermal zone handling */
> > +
> > +#if IS_REACHABLE(CONFIG_THERMAL) && defined(CONFIG_THERMAL_OF)
> > +static int hwmon_thermal_get_temp(void *data, int *temp)
> > +{
> > +	struct hwmon_thermal_data *tdata = data;
> > +	struct hwmon_device *hwdev = tdata->hwdev;
> > +	int ret;
> > +	long t;
> > +
> > +	ret = hwdev->chip->ops->read(&hwdev->dev, hwmon_temp, hwmon_temp_input,
> > +				     tdata->index, &t);
> > +	if (ret < 0)
> > +		return ret;
> > +
> > +	*temp = t;
> > +
> > +	return 0;
> > +}
> > +
> > +static struct thermal_zone_of_device_ops hwmon_thermal_ops = {
> > +	.get_temp = hwmon_thermal_get_temp,
> > +};
> > +
> > +static int hwmon_thermal_add_sensor(struct device *dev,
> > +				    struct hwmon_device *hwdev, int index)
> > +{
> > +	struct hwmon_thermal_data *tdata;
> > +
> > +	tdata = devm_kzalloc(dev, sizeof(*tdata), GFP_KERNEL);
> > +	if (!tdata)
> > +		return -ENOMEM;
> > +
> > +	tdata->hwdev = hwdev;
> > +	tdata->index = index;
> > +
> > +	devm_thermal_zone_of_sensor_register(&hwdev->dev, index, tdata,
> > +					     &hwmon_thermal_ops);
> > +
> > +	return 0;
> > +}
> > +#else
> > +static int hwmon_thermal_add_sensor(struct device *dev,
> > +				    struct hwmon_device *hwdev, int index)
> > +{
> > +	return 0;
> > +}
> > +#endif /* IS_REACHABLE(CONFIG_THERMAL) && defined(CONFIG_THERMAL_OF) */
> > +
> > +/* sysfs attribute management */
> > +
> > +static ssize_t hwmon_attr_show(struct device *dev,
> > +			       struct device_attribute *devattr, char *buf)
> > +{
> > +	struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
> > +	long val;
> > +	int ret;
> > +
> > +	ret = hattr->ops->read(dev, hattr->type, hattr->attr, hattr->index,
> > +			       &val);
> > +	if (ret < 0)
> > +		return ret;
> > +
> > +	return sprintf(buf, "%ld\n", val);
> > +}
> > +
> > +static ssize_t hwmon_attr_store(struct device *dev,
> > +				struct device_attribute *devattr,
> > +				const char *buf, size_t count)
> > +{
> > +	struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
> > +	long val;
> > +	int ret;
> > +
> > +	ret = kstrtol(buf, 10, &val);
> > +	if (ret < 0)
> > +		return ret;
> > +
> > +	ret = hattr->ops->write(dev, hattr->type, hattr->attr, hattr->index,
> > +				val);
> > +	if (ret < 0)
> > +		return ret;
> > +
> > +	return count;
> > +}
> > +
> > +static int hwmon_attr_base(enum hwmon_sensor_types type)
> > +{
> > +	if (type == hwmon_in)
> > +		return 0;
> > +	return 1;
> > +}
> > +
> > +static struct attribute *hwmon_genattr(struct device *dev,
> > +				       const void *drvdata,
> > +				       enum hwmon_sensor_types type,
> > +				       u32 attr,
> > +				       int index,
> > +				       const char *template,
> > +				       const struct hwmon_ops *ops)
> > +{
> > +	struct hwmon_device_attribute *hattr;
> > +	struct device_attribute *dattr;
> > +	struct attribute *a;
> > +	umode_t mode;
> > +	char *name;
> > +
> > +	/* The attribute is invisible if there is no template string */
> > +	if (!template)
> > +		return ERR_PTR(-ENOENT);
> > +
> > +	mode = ops->is_visible(drvdata, type, attr, index);
> 
> Any reason why you don't simply attach the provided ->is_visible to the
> attribute group and let the driver core do the work?
> 
Parameter are all different
	umode_t (*is_visible)(const void *drvdata, enum hwmon_sensor_types type,
	                              u32 attr, int channel);
vs.
	umode_t (*is_visible)(struct kobject *, struct attribute *, int);

> > +		return ERR_PTR(-ENOENT);
> > +
> > +	if ((mode & S_IRUGO) && !ops->read)
> > +		return ERR_PTR(-EINVAL);
> > +	if ((mode & S_IWUGO) && !ops->write)
> > +		return ERR_PTR(-EINVAL);
> > +
> > +	if (type == hwmon_chip) {
> 
> This needs a comment. It's not obvious what "hwmon_chip" is supposed to
> represent. From what I understand, maybe "hwmon_unique" would be a more
> appropriate name.
> 
Those are meant to be for attributes which apply to the entire chip.
Not sure if 'hwmon_unique' would reflect that better.
>From the documentation:
	"A virtual sensor type, used to describe attributes
	 which apply to the entire chip"

Do you have an idea for a better description ?

> > +		name = (char *)template;
> > +	} else {
> > +		name = devm_kzalloc(dev, strlen(template) + 16, GFP_KERNEL);
> > +		if (!name)
> > +			return ERR_PTR(-ENOMEM);
> > +		scnprintf(name, strlen(template) + 16, template,
> > +			  index + hwmon_attr_base(type));
> > +	}
> > +
> > +	hattr = devm_kzalloc(dev, sizeof(*hattr), GFP_KERNEL);
> > +	if (!hattr)
> > +		return ERR_PTR(-ENOMEM);
> 
> So basically you are doing 1 or 2 memory allocations for every
> attribute? Looks quite bad from a memory fragmentation
> perspective :-( Not to mention performance. Given that you have all the
> data at hand before you start, can't you preallocate an array with the
> right number of hattr and pick from it? That would at least solve half
> of the problem.
> 
> Something similar for string allocation may work too, although it's
> somewhat more complex due to the variable length. But I think the
> abituguru3 driver is doing it right, so maybe you can too.
> 
I'll look into it.

> > +
> > +	hattr->type = type;
> > +	hattr->attr = attr;
> > +	hattr->index = index;
> > +	hattr->ops = ops;
> > +
> > +	dattr = &hattr->dev_attr;
> > +	dattr->show = hwmon_attr_show;
> > +	dattr->store = hwmon_attr_store;
> > +
> > +	a = &dattr->attr;
> > +	sysfs_attr_init(a);
> > +	a->name = name;
> > +	a->mode = mode;
> > +
> > +	return a;
> > +}
> > +
> > +static const char * const hwmon_chip_attr_templates[] = {
> > +	[hwmon_chip_temp_reset_history] = "temp_reset_history",
> > +	[hwmon_chip_update_interval] = "update_interval",
> > +	[hwmon_chip_alarms] = "alarms",
> > +};
> > +
> > +static const char * const hwmon_temp_attr_templates[] = {
> > +	[hwmon_temp_input] = "temp%d_input",
> > +	[hwmon_temp_type] = "temp%d_type",
> > +	[hwmon_temp_lcrit] = "temp%d_lcrit",
> > +	[hwmon_temp_lcrit_hyst] = "temp%d_lcrit_hyst",
> > +	[hwmon_temp_min] = "temp%d_min",
> > +	[hwmon_temp_min_hyst] = "temp%d_min_hyst",
> > +	[hwmon_temp_max] = "temp%d_max",
> > +	[hwmon_temp_max_hyst] = "temp%d_max_hyst",
> > +	[hwmon_temp_crit] = "temp%d_crit",
> > +	[hwmon_temp_crit_hyst] = "temp%d_crit_hyst",
> > +	[hwmon_temp_emergency] = "temp%d_emergency",
> > +	[hwmon_temp_emergency_hyst] = "temp%d_emergency_hyst",
> > +	[hwmon_temp_alarm] = "temp%d_alarm",
> > +	[hwmon_temp_lcrit_alarm] = "temp%d_lcrit_alarm",
> > +	[hwmon_temp_min_alarm] = "temp%d_min_alarm",
> > +	[hwmon_temp_max_alarm] = "temp%d_max_alarm",
> > +	[hwmon_temp_crit_alarm] = "temp%d_crit_alarm",
> > +	[hwmon_temp_emergency_alarm] = "temp%d_emergency_alarm",
> > +	[hwmon_temp_fault] = "temp%d_fault",
> > +	[hwmon_temp_offset] = "temp%d_offset",
> > +	[hwmon_temp_label] = "temp%d_label",
> > +	[hwmon_temp_lowest] = "temp%d_lowest",
> > +	[hwmon_temp_highest] = "temp%d_highest",
> > +	[hwmon_temp_reset_history] = "temp%d_reset_history",
> > +};
> > +
> 
> This starts looking a lot like libsensors' code ;-)
> 
> > +static const char * const *__templates[] = {
> > +	[hwmon_chip] = hwmon_chip_attr_templates,
> > +	[hwmon_temp] = hwmon_temp_attr_templates,
> > +};
> > +
> > +static const int __templates_size[] = {
> > +	[hwmon_chip] = ARRAY_SIZE(hwmon_chip_attr_templates),
> > +	[hwmon_temp] = ARRAY_SIZE(hwmon_temp_attr_templates),
> > +};
> > +
> > +static int hwmon_num_channel_attrs(const struct hwmon_channel_info *info)
> > +{
> > +	int i, n;
> > +
> > +	for (i = n = 0; info->config[i]; i++)
> > +		n += hweight32(info->config[i]);
> > +
> > +	return n;
> > +}
> > +
> > +static int hwmon_genattrs(struct device *dev,
> > +			  const void *drvdata,
> > +			  struct attribute **attrs,
> > +			  const struct hwmon_ops *ops,
> > +			  const struct hwmon_channel_info *info)
> > +{
> > +	const char * const *templates;
> > +	int template_size;
> > +	int i, aindex = 0;
> > +
> > +	if (info->type >= ARRAY_SIZE(__templates))
> > +		return -EINVAL;
> > +
> > +	templates = __templates[info->type];
> > +	template_size = __templates_size[info->type];
> > +
> > +	for (i = 0; info->config[i]; i++) {
> > +		u32 attr_mask = info->config[i];
> > +		u32 attr;
> > +
> > +		while (attr_mask) {
> > +			struct attribute *a;
> > +
> > +			attr = __ffs(attr_mask);
> > +			attr_mask &= ~BIT(attr);
> > +			if (attr >= template_size)
> > +				return -EINVAL;
> > +			a = hwmon_genattr(dev, drvdata, info->type, attr, i,
> > +					  templates[attr], ops);
> > +			if (IS_ERR(a)) {
> > +				if (PTR_ERR(a) != -ENOENT)
> > +					return PTR_ERR(a);
> > +				continue;
> > +			}
> > +			attrs[aindex++] = a;
> > +		}
> > +	}
> > +	return aindex;
> > +}
> > +
> > +static struct attribute **
> > +__hwmon_create_attrs(struct device *dev, const void *drvdata,
> > +		     const struct hwmon_chip_info *chip)
> > +{
> > +	int ret, i, aindex = 0, nattrs = 0;
> > +	struct attribute **attrs;
> > +
> > +	for (i = 0; chip->info[i]; i++)
> > +		nattrs += hwmon_num_channel_attrs(chip->info[i]);
> > +
> > +	if (nattrs == 0)
> > +		return ERR_PTR(-EINVAL);
> > +
> > +	attrs = devm_kcalloc(dev, nattrs + 1, sizeof(*attrs), GFP_KERNEL);
> > +	if (!attrs)
> > +		return ERR_PTR(-ENOMEM);
> > +
> > +	for (i = 0; chip->info[i]; i++) {
> > +		ret = hwmon_genattrs(dev, drvdata, &attrs[aindex], chip->ops,
> > +				     chip->info[i]);
> > +		if (ret < 0)
> > +			return ERR_PTR(ret);
> > +		aindex += ret;
> > +	}
> > +
> > +	return attrs;
> > +}
> > +
> > +static struct device *
> > +__hwmon_device_register(struct device *dev, const char *name, void *drvdata,
> > +			const struct hwmon_chip_info *chip,
> > +			const struct attribute_group **groups)
> >  {
> >  	struct hwmon_device *hwdev;
> > -	int err, id;
> > +	struct device *hdev;
> > +	int i, j, err, id;
> >  
> >  	/* Do not accept invalid characters in hwmon name attribute */
> >  	if (name && (!strlen(name) || strpbrk(name, "-* \t\n")))
> > @@ -112,28 +401,128 @@ hwmon_device_register_with_groups(struct device *dev, const char *name,
> >  		goto ida_remove;
> >  	}
> >  
> > +	hdev = &hwdev->dev;
> > +
> > +	if (chip && chip->ops->is_visible) {
> 
> is_visible is documented as mandatory, so why would this check be needed
> (and not result in an error if failed?)
> 
Let me check. I don't recall if the documentation is at fault or the
code.

> > +		struct attribute **attrs;
> > +		int ngroups = 2;
> 
> I'd appreciate a comment here.
> 
Sure.

> > +
> > +		if (groups)
> > +			for (i = 0; groups[i]; i++)
> > +				ngroups++;
> > +
> > +		hwdev->groups = devm_kcalloc(dev, ngroups, sizeof(*groups),
> > +					     GFP_KERNEL);
> > +		if (!hwdev->groups)
> > +			return ERR_PTR(-ENOMEM);
> > +
> > +		attrs = __hwmon_create_attrs(dev, drvdata, chip);
> > +		if (IS_ERR(attrs)) {
> > +			err = PTR_ERR(attrs);
> > +			goto free_hwmon;
> > +		}
> > +
> > +		hwdev->group.attrs = attrs;
> > +		ngroups = 0;
> > +		hwdev->groups[ngroups++] = &hwdev->group;
> > +
> > +		if (groups) {
> > +			for (i = 0; groups[i]; i++)
> > +				hwdev->groups[ngroups++] = groups[i];
> > +		}
> > +
> > +		hdev->groups = hwdev->groups;
> > +	} else {
> > +		hdev->groups = groups;
> > +	}
> > +
> >  	hwdev->name = name;
> > -	hwdev->dev.class = &hwmon_class;
> > -	hwdev->dev.parent = dev;
> > -	hwdev->dev.groups = groups;
> > -	hwdev->dev.of_node = dev ? dev->of_node : NULL;
> > -	dev_set_drvdata(&hwdev->dev, drvdata);
> > -	dev_set_name(&hwdev->dev, HWMON_ID_FORMAT, id);
> > -	err = device_register(&hwdev->dev);
> > +	hdev->class = &hwmon_class;
> > +	hdev->parent = dev;
> > +	hdev->of_node = dev ? dev->of_node : NULL;
> > +	hwdev->chip = chip;
> > +	dev_set_drvdata(hdev, drvdata);
> > +	dev_set_name(hdev, HWMON_ID_FORMAT, id);
> > +	err = device_register(hdev);
> >  	if (err)
> > -		goto free;
> > +		goto free_hwmon;
> > +
> > +	if (chip && chip->ops->is_visible && chip->ops->read &&
> > +	    chip->info[0]->type == hwmon_chip &&
> > +	    (chip->info[0]->config[0] & HWMON_C_REGISTER_TZ)) {
> > +		const struct hwmon_channel_info **info = chip->info;
> > +
> > +		for (i = 1; info[i]; i++) {
> > +			if (info[i]->type != hwmon_temp)
> > +				continue;
> > +
> > +			for (j = 0; info[i]->config[j]; j++) {
> > +				if (!chip->ops->is_visible(drvdata, hwmon_temp,
> > +							   hwmon_temp_input, j))
> > +					continue;
> > +				if (info[i]->config[j] & HWMON_T_INPUT)
> > +					hwmon_thermal_add_sensor(dev, hwdev, j);
> > +			}
> > +		}
> > +	}
> >  
> > -	return &hwdev->dev;
> > +	return hdev;
> >  
> > -free:
> > +free_hwmon:
> >  	kfree(hwdev);
> >  ida_remove:
> >  	ida_simple_remove(&hwmon_ida, id);
> >  	return ERR_PTR(err);
> >  }
> > +
> > +/**
> > + * hwmon_device_register_with_groups - register w/ hwmon
> > + * @dev: the parent device
> > + * @name: hwmon name attribute
> > + * @drvdata: driver data to attach to created device
> > + * @groups: List of attribute groups to create
> > + *
> > + * hwmon_device_unregister() must be called when the device is no
> > + * longer needed.
> > + *
> > + * Returns the pointer to the new device.
> > + */
> > +struct device *
> > +hwmon_device_register_with_groups(struct device *dev, const char *name,
> > +				  void *drvdata,
> > +				  const struct attribute_group **groups)
> > +{
> > +	return __hwmon_device_register(dev, name, drvdata, NULL, groups);
> > +}
> >  EXPORT_SYMBOL_GPL(hwmon_device_register_with_groups);
> >  
> >  /**
> > + * hwmon_device_register_with_info - register w/ hwmon
> > + * @dev: the parent device
> > + * @name: hwmon name attribute
> > + * @drvdata: driver data to attach to created device
> > + * @info: Pointer to hwmon chip information
> > + * @groups - pointer to list of driver specific attribute groups
> 
> I don't find this clear. "driver specific" doesn't really tell me what
> it is. My first reaction when reviewing the changes from an API
> perspective was "the attributes would be generated from the
> hwmon_chip_info, so why are we still passing them as a parameter?" Now I
> seem to understand that "groups" here is meant for extra attributes
> which aren't part of standard sysfs interface. This would be NULL for
> most drivers, right?
> 
Correct.

> To clear up the confusion, what about renaming this parameter to
> "extra_groups"? And maybe rewording "driver specific" to "non-standard"?
> 
No problem with that.

> As a side note I am wondering if it would be reasonable to limit it to a
> single group, instead of a NULL-terminated array of groups. This would
> make the code a little more efficient.
> 
The underlying code is all the same; hwmon_device_register_with_groups()
also calls __hwmon_device_register(). I would prefer to keep the code as
common as possible.

> > + *
> > + * hwmon_device_unregister() must be called when the device is no
> > + * longer needed.
> > + *
> > + * Returns the pointer to the new device.
> > + */
> > +struct device *
> > +hwmon_device_register_with_info(struct device *dev, const char *name,
> > +				void *drvdata,
> > +				const struct hwmon_chip_info *chip,
> > +				const struct attribute_group **groups)
> > +{
> > +	if (chip && (!chip->ops || !chip->info))
> > +		return ERR_PTR(-EINVAL);
> > +
> > +	return __hwmon_device_register(dev, name, drvdata, chip, groups);
> > +}
> > +EXPORT_SYMBOL_GPL(hwmon_device_register_with_info);
> > +
> > +/**
> >   * hwmon_device_register - register w/ hwmon
> >   * @dev: the device to register
> >   *
> > @@ -211,6 +600,48 @@ error:
> >  }
> >  EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_groups);
> >  
> > +/**
> > + * devm_hwmon_device_register_with_info - register w/ hwmon
> > + * @dev: the parent device
> > + * @name: hwmon name attribute
> > + * @drvdata: driver data to attach to created device
> > + * @info: Pointer to hwmon chip information
> > + * @groups - pointer to list of driver specific attribute groups
> > + *
> > + * Returns the pointer to the new device. The new device is automatically
> > + * unregistered with the parent device.
> > + */
> > +struct device *
> > +devm_hwmon_device_register_with_info(struct device *dev, const char *name,
> > +				     void *drvdata,
> > +				     const struct hwmon_chip_info *chip,
> > +				     const struct attribute_group **groups)
> > +{
> > +	struct device **ptr, *hwdev;
> > +
> > +	if (!dev)
> > +		return ERR_PTR(-EINVAL);
> > +
> > +	ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL);
> > +	if (!ptr)
> > +		return ERR_PTR(-ENOMEM);
> > +
> > +	hwdev = hwmon_device_register_with_info(dev, name, drvdata, chip,
> > +						groups);
> > +	if (IS_ERR(hwdev))
> > +		goto error;
> > +
> > +	*ptr = hwdev;
> > +	devres_add(dev, ptr);
> > +
> > +	return hwdev;
> > +
> > +error:
> > +	devres_free(ptr);
> > +	return hwdev;
> > +}
> > +EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_info);
> > +
> >  static int devm_hwmon_match(struct device *dev, void *res, void *data)
> >  {
> >  	struct device **hwdev = res;
> > diff --git a/include/linux/hwmon.h b/include/linux/hwmon.h
> > index 09354f6c1d63..52e56d71d742 100644
> > --- a/include/linux/hwmon.h
> > +++ b/include/linux/hwmon.h
> > @@ -14,9 +14,147 @@
> >  #ifndef _HWMON_H_
> >  #define _HWMON_H_
> >  
> > +#include <linux/bitops.h>
> > +
> >  struct device;
> >  struct attribute_group;
> >  
> > +enum hwmon_sensor_types {
> > +	hwmon_chip,
> > +	hwmon_temp,
> > +	hwmon_in,
> > +	hwmon_curr,
> > +	hwmon_power,
> > +	hwmon_energy,
> > +};
> > +
> > +enum hwmon_chip_attributes {
> > +	hwmon_chip_temp_reset_history,
> > +	hwmon_chip_register_tz,
> > +	hwmon_chip_update_interval,
> > +	hwmon_chip_alarms,
> > +};
> > +
> > +#define HWMON_C_TEMP_RESET_HISTORY	BIT(hwmon_chip_temp_reset_history)
> > +#define HWMON_C_IN_RESET_HISTORY	BIT(hwmon_chip_in_reset_history)
> 
> Ideally this one should go in next patch ("Add voltage attribute support
> to new API".)
> 
> > +#define HWMON_C_REGISTER_TZ		BIT(hwmon_chip_register_tz)
> > +#define HWMON_C_UPDATE_INTERVAL		BIT(hwmon_chip_update_interval)
> > +#define HWMON_C_ALARMS			BIT(hwmon_chip_alarms)
> > +
> > +enum hwmon_temp_attributes {
> > +	hwmon_temp_input = 0,
> > +	hwmon_temp_type,
> > +	hwmon_temp_lcrit,
> > +	hwmon_temp_lcrit_hyst,
> > +	hwmon_temp_min,
> > +	hwmon_temp_min_hyst,
> > +	hwmon_temp_max,
> > +	hwmon_temp_max_hyst,
> > +	hwmon_temp_crit,
> > +	hwmon_temp_crit_hyst,
> > +	hwmon_temp_emergency,
> > +	hwmon_temp_emergency_hyst,
> > +	hwmon_temp_alarm,
> > +	hwmon_temp_lcrit_alarm,
> > +	hwmon_temp_min_alarm,
> > +	hwmon_temp_max_alarm,
> > +	hwmon_temp_crit_alarm,
> > +	hwmon_temp_emergency_alarm,
> > +	hwmon_temp_fault,
> > +	hwmon_temp_offset,
> > +	hwmon_temp_label,
> > +	hwmon_temp_lowest,
> > +	hwmon_temp_highest,
> > +	hwmon_temp_reset_history,
> > +};
> > +
> > +#define HWMON_T_INPUT		BIT(hwmon_temp_input)
> > +#define HWMON_T_TYPE		BIT(hwmon_temp_type)
> > +#define HWMON_T_LCRIT		BIT(hwmon_temp_lcrit)
> > +#define HWMON_T_LCRIT_HYST	BIT(hwmon_temp_lcrit_hyst)
> > +#define HWMON_T_MIN		BIT(hwmon_temp_min)
> > +#define HWMON_T_MIN_HYST	BIT(hwmon_temp_min_hyst)
> > +#define HWMON_T_MAX		BIT(hwmon_temp_max)
> > +#define HWMON_T_MAX_HYST	BIT(hwmon_temp_max_hyst)
> > +#define HWMON_T_CRIT		BIT(hwmon_temp_crit)
> > +#define HWMON_T_CRIT_HYST	BIT(hwmon_temp_crit_hyst)
> > +#define HWMON_T_EMERGENCY	BIT(hwmon_temp_emergency)
> > +#define HWMON_T_EMERGENCY_HYST	BIT(hwmon_temp_emergency_hyst)
> > +#define HWMON_T_MIN_ALARM	BIT(hwmon_temp_min_alarm)
> > +#define HWMON_T_MAX_ALARM	BIT(hwmon_temp_max_alarm)
> > +#define HWMON_T_CRIT_ALARM	BIT(hwmon_temp_crit_alarm)
> > +#define HWMON_T_EMERGENCY_ALARM	BIT(hwmon_temp_emergency_alarm)
> > +#define HWMON_T_FAULT		BIT(hwmon_temp_fault)
> > +#define HWMON_T_OFFSET		BIT(hwmon_temp_offset)
> > +#define HWMON_T_LABEL		BIT(hwmon_temp_label)
> > +#define HWMON_T_LOWEST		BIT(hwmon_temp_lowest)
> > +#define HWMON_T_HIGHEST		BIT(hwmon_temp_highest)
> > +#define HWMON_T_RESET_HISTORY	BIT(hwmon_temp_reset_history)
> > +
> > +/**
> > + * struct hwmon_ops - hwmon device operations
> > + * @is_visible: Callback to return attribute visibility. Mandatory.
> > + *		Parameters are:
> > + *		@const void *drvdata:
> > + *			Pointer to driver-private data structure passed
> > + *			as argument to hwmon_device_register_with_info().
> > + *		@type:	Sensor type
> > + *		@attr:	Sensor attribute
> > + *		@channel:
> > + *			Channel number
> > + *		The function returns the file permissions.
> > + *		If the return value is 0, no attribute will be created.
> > + * @read:       Read callback. Optional. If not provided, attributes
> > + *		will not be readable.
> > + *		Parameters are:
> > + *		@dev:	Pointer to hardware monitoring device
> > + *		@type:	Sensor type
> > + *		@attr:	Sensor attribute
> > + *		@channel:
> > + *			Channel number
> > + *		@val:	Pointer to returned value
> > + *		The function returns 0 on success or a negative error number.
> > + * @write:	Write callback. Optional. If not provided, attributes
> > + *		will not be writable.
> > + *		Parameters are:
> > + *		@dev:	Pointer to hardware monitoring device
> > + *		@type:	Sensor type
> > + *		@attr:	Sensor attribute
> > + *		@channel:
> > + *			Channel number
> > + *		@val:	Value to write
> > + *		The function returns 0 on success or a negative error number.
> > + */
> > +struct hwmon_ops {
> > +	umode_t (*is_visible)(const void *drvdata, enum hwmon_sensor_types type,
> > +			      u32 attr, int channel);
> > +	int (*read)(struct device *dev, enum hwmon_sensor_types type,
> > +		    u32 attr, int channel, long *val);
> > +	int (*write)(struct device *dev, enum hwmon_sensor_types type,
> > +		     u32 attr, int channel, long val);
> > +};
> > +
> > +/**
> > + * Channel information
> > + * @type:	Channel type.
> > + * @config:	Pointer to NULL-terminated list of channel parameters.
> > + *		Use for per-channel attributes.
> > + */
> > +struct hwmon_channel_info {
> > +	enum hwmon_sensor_types type;
> > +	const u32 *config;
> > +};
> > +
> > +/**
> > + * Chip configuration
> > + * @ops:	Pointer to hwmon operations.
> > + * @info:	Null-terminated list of channel information.
> > + */
> > +struct hwmon_chip_info {
> > +	const struct hwmon_ops *ops;
> > +	const struct hwmon_channel_info **info;
> > +};
> > +
> >  struct device *hwmon_device_register(struct device *dev);
> >  struct device *
> >  hwmon_device_register_with_groups(struct device *dev, const char *name,
> > @@ -26,6 +164,16 @@ struct device *
> >  devm_hwmon_device_register_with_groups(struct device *dev, const char *name,
> >  				       void *drvdata,
> >  				       const struct attribute_group **groups);
> > +struct device *
> > +hwmon_device_register_with_info(struct device *dev,
> > +				const char *name, void *drvdata,
> > +				const struct hwmon_chip_info *info,
> > +				const struct attribute_group **groups);
> > +struct device *
> > +devm_hwmon_device_register_with_info(struct device *dev,
> > +				     const char *name, void *drvdata,
> > +				     const struct hwmon_chip_info *info,
> > +				     const struct attribute_group **groups);
> >  
> >  void hwmon_device_unregister(struct device *dev);
> >  void devm_hwmon_device_unregister(struct device *dev);
> 
> This is adding a 4th and 5th way to register a hwmon device. Starts
> being a bit messy... Do you have any plans to get rid of some of the
> other functions to make things more consistent and efficient?
> 
Would be nice, but then someone would have to spend the time cleaning
up the old drivers to replace the old API, and for the most part we would
not be able to test the result. Are you sure that is worth the risk ?

Thanks,
Guenter

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH v3 2/9] hwmon: (core) New hwmon registration API
@ 2016-10-04 19:37       ` Guenter Roeck
  0 siblings, 0 replies; 26+ messages in thread
From: Guenter Roeck @ 2016-10-04 19:37 UTC (permalink / raw)
  To: Jean Delvare
  Cc: Jonathan Cameron, Zhang Rui, Eduardo Valentin, Punit Agrawal,
	linux-pm-u79uwXL29TY76Z2rM5mHXA,
	linux-iio-u79uwXL29TY76Z2rM5mHXA,
	linux-hwmon-u79uwXL29TY76Z2rM5mHXA,
	linux-kernel-u79uwXL29TY76Z2rM5mHXA

Hi Jean,

On Tue, Oct 04, 2016 at 10:45:59AM +0200, Jean Delvare wrote:
> Hi Guenter,
> 
> I see this patch is upstream now, but I had started reviewing it and
> maybe some of my comments are still relevant.
> 
As always, I appreciate the feedback. I'll be happy to submit follow-up
patches to address any concerns.

> It's not meant to be a complete review, which is why I had not sent it
> yet :-(
> 
> Also I did not follow the first iterations of this patchset so my
> apologies if I raise points which have already been discussed.
> 
> May I ask if any other subsystem is already doing anything like that?
> 
Depends what you mean with "anything like that". The API is inspired by
the iio API and a little by the thermal API. The details are probably
unique, though.

> FYI I gave a presentation about the hwmon device registration API
> evolution last week at Kernel Recipes [1] in Paris and mentioned this
> proposal.
> 
> [1] https://kernel-recipes.org/en/2016/
> 
> On dim., 2016-07-24 at 20:32 -0700, Guenter Roeck wrote:
> > Up to now, each hwmon driver has to implement its own sysfs attributes.
> > This requires a lot of template code, and distracts from the driver's core
> > function to read and write chip registers.
> > 
> > To be able to reduce driver complexity, move sensor attribute handling
> > and thermal zone registration into hwmon core. By using the new API,
> > driver code and data size is typically reduced by 20-70%, depending
> > on driver complexity and the number of sysfs attributes supported.
> 
> I looked at the diffstats for the drivers you have already converted and
> couldn't see any such huge improvement... Some drivers appear to be even
> larger after conversion?
> 
The above refers to code and data sizes.

> > With this patch, the new API only supports thermal sensors. Support for
> > other sensor types will be added with subsequent patches.
> > 
> > Acked-by: Punit Agrawal <punit.agrawal-5wv7dgnIgG8@public.gmane.org>
> > Reviewed-by: Jonathan Cameron <jic23-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org>
> > Signed-off-by: Guenter Roeck <linux-0h96xk9xTtrk1uMJSBkQmQ@public.gmane.org>
> > ---
> > v3:
> > - Thermal registration depends on IS_REACHABLE(CONFIG_THERMAL) and
> >   CONFIG_THERMAL_OF.
> > v2:
> > - Document callback function parameters of struct hwmon_ops in
> >   include/linux/hwmon.h.
> > - Clarify that the is_visible() callback is mandatory.
> > - Initialize device attribute read/write callback functions unconditionally.
> > - If an attribute has no template string, treat it as invisible, not as
> >   error. Affected are virtual attributes such as HWMON_C_REGISTER_TZ.
> > - Added newline to improve readability.
> > 
> > Review comments not addressed:
> > - Stick with u32 for attribute masks. We could use u64, but it is currently
> >   not needed, and changing it later would be straightforward.
> > - Do not use for_each_set_bit() to walk attribute masks.
> >   for_each_set_bit() expects a pointer to an unsigned long as argument,
> >   which would make it difficult to switch to u64 attribute masks if/when
> >   needed.
> > 
> >  drivers/hwmon/hwmon.c | 485 +++++++++++++++++++++++++++++++++++++++++++++++---
> >  include/linux/hwmon.h | 148 +++++++++++++++
> >  2 files changed, 606 insertions(+), 27 deletions(-)
> > 
> > diff --git a/drivers/hwmon/hwmon.c b/drivers/hwmon/hwmon.c
> > index 649a68d119b4..3e4cc442a089 100644
> > --- a/drivers/hwmon/hwmon.c
> > +++ b/drivers/hwmon/hwmon.c
> 
> I believe it would make sense to add you copyright at the top of the
> file. That's a pretty big change with lots of code.
> 
I don't really feel too strong about that. But, sure, can do.

> > @@ -12,6 +12,7 @@
> >  
> >  #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
> >  
> > +#include <linux/bitops.h>
> >  #include <linux/device.h>
> >  #include <linux/err.h>
> >  #include <linux/gfp.h>
> > @@ -21,6 +22,7 @@
> >  #include <linux/pci.h>
> >  #include <linux/slab.h>
> >  #include <linux/string.h>
> > +#include <linux/thermal.h>
> >  
> >  #define HWMON_ID_PREFIX "hwmon"
> >  #define HWMON_ID_FORMAT HWMON_ID_PREFIX "%d"
> > @@ -28,9 +30,35 @@
> >  struct hwmon_device {
> >  	const char *name;
> >  	struct device dev;
> > +	const struct hwmon_chip_info *chip;
> > +
> > +	struct attribute_group group;
> > +	const struct attribute_group **groups;
> >  };
> > +
> >  #define to_hwmon_device(d) container_of(d, struct hwmon_device, dev)
> >  
> > +struct hwmon_device_attribute {
> > +	struct device_attribute dev_attr;
> > +	const struct hwmon_ops *ops;
> > +	enum hwmon_sensor_types type;
> > +	u32 attr;
> > +	int index;
> > +};
> > +
> > +#define to_hwmon_attr(d) \
> > +	container_of(d, struct hwmon_device_attribute, dev_attr)
> > +
> > +/*
> > + * Thermal zone information
> > + * In addition to the reference to the hwmon device,
> > + * also provides the sensor index.
> > + */
> > +struct hwmon_thermal_data {
> > +	struct hwmon_device *hwdev;	/* Reference to hwmon device */
> > +	int index;			/* sensor index */
> > +};
> > +
> >  static ssize_t
> >  show_name(struct device *dev, struct device_attribute *attr, char *buf)
> >  {
> > @@ -78,25 +106,286 @@ static struct class hwmon_class = {
> >  
> >  static DEFINE_IDA(hwmon_ida);
> >  
> > -/**
> > - * hwmon_device_register_with_groups - register w/ hwmon
> > - * @dev: the parent device
> > - * @name: hwmon name attribute
> > - * @drvdata: driver data to attach to created device
> > - * @groups: List of attribute groups to create
> > - *
> > - * hwmon_device_unregister() must be called when the device is no
> > - * longer needed.
> > - *
> > - * Returns the pointer to the new device.
> > - */
> > -struct device *
> > -hwmon_device_register_with_groups(struct device *dev, const char *name,
> > -				  void *drvdata,
> > -				  const struct attribute_group **groups)
> > +/* Thermal zone handling */
> > +
> > +#if IS_REACHABLE(CONFIG_THERMAL) && defined(CONFIG_THERMAL_OF)
> > +static int hwmon_thermal_get_temp(void *data, int *temp)
> > +{
> > +	struct hwmon_thermal_data *tdata = data;
> > +	struct hwmon_device *hwdev = tdata->hwdev;
> > +	int ret;
> > +	long t;
> > +
> > +	ret = hwdev->chip->ops->read(&hwdev->dev, hwmon_temp, hwmon_temp_input,
> > +				     tdata->index, &t);
> > +	if (ret < 0)
> > +		return ret;
> > +
> > +	*temp = t;
> > +
> > +	return 0;
> > +}
> > +
> > +static struct thermal_zone_of_device_ops hwmon_thermal_ops = {
> > +	.get_temp = hwmon_thermal_get_temp,
> > +};
> > +
> > +static int hwmon_thermal_add_sensor(struct device *dev,
> > +				    struct hwmon_device *hwdev, int index)
> > +{
> > +	struct hwmon_thermal_data *tdata;
> > +
> > +	tdata = devm_kzalloc(dev, sizeof(*tdata), GFP_KERNEL);
> > +	if (!tdata)
> > +		return -ENOMEM;
> > +
> > +	tdata->hwdev = hwdev;
> > +	tdata->index = index;
> > +
> > +	devm_thermal_zone_of_sensor_register(&hwdev->dev, index, tdata,
> > +					     &hwmon_thermal_ops);
> > +
> > +	return 0;
> > +}
> > +#else
> > +static int hwmon_thermal_add_sensor(struct device *dev,
> > +				    struct hwmon_device *hwdev, int index)
> > +{
> > +	return 0;
> > +}
> > +#endif /* IS_REACHABLE(CONFIG_THERMAL) && defined(CONFIG_THERMAL_OF) */
> > +
> > +/* sysfs attribute management */
> > +
> > +static ssize_t hwmon_attr_show(struct device *dev,
> > +			       struct device_attribute *devattr, char *buf)
> > +{
> > +	struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
> > +	long val;
> > +	int ret;
> > +
> > +	ret = hattr->ops->read(dev, hattr->type, hattr->attr, hattr->index,
> > +			       &val);
> > +	if (ret < 0)
> > +		return ret;
> > +
> > +	return sprintf(buf, "%ld\n", val);
> > +}
> > +
> > +static ssize_t hwmon_attr_store(struct device *dev,
> > +				struct device_attribute *devattr,
> > +				const char *buf, size_t count)
> > +{
> > +	struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
> > +	long val;
> > +	int ret;
> > +
> > +	ret = kstrtol(buf, 10, &val);
> > +	if (ret < 0)
> > +		return ret;
> > +
> > +	ret = hattr->ops->write(dev, hattr->type, hattr->attr, hattr->index,
> > +				val);
> > +	if (ret < 0)
> > +		return ret;
> > +
> > +	return count;
> > +}
> > +
> > +static int hwmon_attr_base(enum hwmon_sensor_types type)
> > +{
> > +	if (type == hwmon_in)
> > +		return 0;
> > +	return 1;
> > +}
> > +
> > +static struct attribute *hwmon_genattr(struct device *dev,
> > +				       const void *drvdata,
> > +				       enum hwmon_sensor_types type,
> > +				       u32 attr,
> > +				       int index,
> > +				       const char *template,
> > +				       const struct hwmon_ops *ops)
> > +{
> > +	struct hwmon_device_attribute *hattr;
> > +	struct device_attribute *dattr;
> > +	struct attribute *a;
> > +	umode_t mode;
> > +	char *name;
> > +
> > +	/* The attribute is invisible if there is no template string */
> > +	if (!template)
> > +		return ERR_PTR(-ENOENT);
> > +
> > +	mode = ops->is_visible(drvdata, type, attr, index);
> 
> Any reason why you don't simply attach the provided ->is_visible to the
> attribute group and let the driver core do the work?
> 
Parameter are all different
	umode_t (*is_visible)(const void *drvdata, enum hwmon_sensor_types type,
	                              u32 attr, int channel);
vs.
	umode_t (*is_visible)(struct kobject *, struct attribute *, int);

> > +		return ERR_PTR(-ENOENT);
> > +
> > +	if ((mode & S_IRUGO) && !ops->read)
> > +		return ERR_PTR(-EINVAL);
> > +	if ((mode & S_IWUGO) && !ops->write)
> > +		return ERR_PTR(-EINVAL);
> > +
> > +	if (type == hwmon_chip) {
> 
> This needs a comment. It's not obvious what "hwmon_chip" is supposed to
> represent. From what I understand, maybe "hwmon_unique" would be a more
> appropriate name.
> 
Those are meant to be for attributes which apply to the entire chip.
Not sure if 'hwmon_unique' would reflect that better.
>From the documentation:
	"A virtual sensor type, used to describe attributes
	 which apply to the entire chip"

Do you have an idea for a better description ?

> > +		name = (char *)template;
> > +	} else {
> > +		name = devm_kzalloc(dev, strlen(template) + 16, GFP_KERNEL);
> > +		if (!name)
> > +			return ERR_PTR(-ENOMEM);
> > +		scnprintf(name, strlen(template) + 16, template,
> > +			  index + hwmon_attr_base(type));
> > +	}
> > +
> > +	hattr = devm_kzalloc(dev, sizeof(*hattr), GFP_KERNEL);
> > +	if (!hattr)
> > +		return ERR_PTR(-ENOMEM);
> 
> So basically you are doing 1 or 2 memory allocations for every
> attribute? Looks quite bad from a memory fragmentation
> perspective :-( Not to mention performance. Given that you have all the
> data at hand before you start, can't you preallocate an array with the
> right number of hattr and pick from it? That would at least solve half
> of the problem.
> 
> Something similar for string allocation may work too, although it's
> somewhat more complex due to the variable length. But I think the
> abituguru3 driver is doing it right, so maybe you can too.
> 
I'll look into it.

> > +
> > +	hattr->type = type;
> > +	hattr->attr = attr;
> > +	hattr->index = index;
> > +	hattr->ops = ops;
> > +
> > +	dattr = &hattr->dev_attr;
> > +	dattr->show = hwmon_attr_show;
> > +	dattr->store = hwmon_attr_store;
> > +
> > +	a = &dattr->attr;
> > +	sysfs_attr_init(a);
> > +	a->name = name;
> > +	a->mode = mode;
> > +
> > +	return a;
> > +}
> > +
> > +static const char * const hwmon_chip_attr_templates[] = {
> > +	[hwmon_chip_temp_reset_history] = "temp_reset_history",
> > +	[hwmon_chip_update_interval] = "update_interval",
> > +	[hwmon_chip_alarms] = "alarms",
> > +};
> > +
> > +static const char * const hwmon_temp_attr_templates[] = {
> > +	[hwmon_temp_input] = "temp%d_input",
> > +	[hwmon_temp_type] = "temp%d_type",
> > +	[hwmon_temp_lcrit] = "temp%d_lcrit",
> > +	[hwmon_temp_lcrit_hyst] = "temp%d_lcrit_hyst",
> > +	[hwmon_temp_min] = "temp%d_min",
> > +	[hwmon_temp_min_hyst] = "temp%d_min_hyst",
> > +	[hwmon_temp_max] = "temp%d_max",
> > +	[hwmon_temp_max_hyst] = "temp%d_max_hyst",
> > +	[hwmon_temp_crit] = "temp%d_crit",
> > +	[hwmon_temp_crit_hyst] = "temp%d_crit_hyst",
> > +	[hwmon_temp_emergency] = "temp%d_emergency",
> > +	[hwmon_temp_emergency_hyst] = "temp%d_emergency_hyst",
> > +	[hwmon_temp_alarm] = "temp%d_alarm",
> > +	[hwmon_temp_lcrit_alarm] = "temp%d_lcrit_alarm",
> > +	[hwmon_temp_min_alarm] = "temp%d_min_alarm",
> > +	[hwmon_temp_max_alarm] = "temp%d_max_alarm",
> > +	[hwmon_temp_crit_alarm] = "temp%d_crit_alarm",
> > +	[hwmon_temp_emergency_alarm] = "temp%d_emergency_alarm",
> > +	[hwmon_temp_fault] = "temp%d_fault",
> > +	[hwmon_temp_offset] = "temp%d_offset",
> > +	[hwmon_temp_label] = "temp%d_label",
> > +	[hwmon_temp_lowest] = "temp%d_lowest",
> > +	[hwmon_temp_highest] = "temp%d_highest",
> > +	[hwmon_temp_reset_history] = "temp%d_reset_history",
> > +};
> > +
> 
> This starts looking a lot like libsensors' code ;-)
> 
> > +static const char * const *__templates[] = {
> > +	[hwmon_chip] = hwmon_chip_attr_templates,
> > +	[hwmon_temp] = hwmon_temp_attr_templates,
> > +};
> > +
> > +static const int __templates_size[] = {
> > +	[hwmon_chip] = ARRAY_SIZE(hwmon_chip_attr_templates),
> > +	[hwmon_temp] = ARRAY_SIZE(hwmon_temp_attr_templates),
> > +};
> > +
> > +static int hwmon_num_channel_attrs(const struct hwmon_channel_info *info)
> > +{
> > +	int i, n;
> > +
> > +	for (i = n = 0; info->config[i]; i++)
> > +		n += hweight32(info->config[i]);
> > +
> > +	return n;
> > +}
> > +
> > +static int hwmon_genattrs(struct device *dev,
> > +			  const void *drvdata,
> > +			  struct attribute **attrs,
> > +			  const struct hwmon_ops *ops,
> > +			  const struct hwmon_channel_info *info)
> > +{
> > +	const char * const *templates;
> > +	int template_size;
> > +	int i, aindex = 0;
> > +
> > +	if (info->type >= ARRAY_SIZE(__templates))
> > +		return -EINVAL;
> > +
> > +	templates = __templates[info->type];
> > +	template_size = __templates_size[info->type];
> > +
> > +	for (i = 0; info->config[i]; i++) {
> > +		u32 attr_mask = info->config[i];
> > +		u32 attr;
> > +
> > +		while (attr_mask) {
> > +			struct attribute *a;
> > +
> > +			attr = __ffs(attr_mask);
> > +			attr_mask &= ~BIT(attr);
> > +			if (attr >= template_size)
> > +				return -EINVAL;
> > +			a = hwmon_genattr(dev, drvdata, info->type, attr, i,
> > +					  templates[attr], ops);
> > +			if (IS_ERR(a)) {
> > +				if (PTR_ERR(a) != -ENOENT)
> > +					return PTR_ERR(a);
> > +				continue;
> > +			}
> > +			attrs[aindex++] = a;
> > +		}
> > +	}
> > +	return aindex;
> > +}
> > +
> > +static struct attribute **
> > +__hwmon_create_attrs(struct device *dev, const void *drvdata,
> > +		     const struct hwmon_chip_info *chip)
> > +{
> > +	int ret, i, aindex = 0, nattrs = 0;
> > +	struct attribute **attrs;
> > +
> > +	for (i = 0; chip->info[i]; i++)
> > +		nattrs += hwmon_num_channel_attrs(chip->info[i]);
> > +
> > +	if (nattrs == 0)
> > +		return ERR_PTR(-EINVAL);
> > +
> > +	attrs = devm_kcalloc(dev, nattrs + 1, sizeof(*attrs), GFP_KERNEL);
> > +	if (!attrs)
> > +		return ERR_PTR(-ENOMEM);
> > +
> > +	for (i = 0; chip->info[i]; i++) {
> > +		ret = hwmon_genattrs(dev, drvdata, &attrs[aindex], chip->ops,
> > +				     chip->info[i]);
> > +		if (ret < 0)
> > +			return ERR_PTR(ret);
> > +		aindex += ret;
> > +	}
> > +
> > +	return attrs;
> > +}
> > +
> > +static struct device *
> > +__hwmon_device_register(struct device *dev, const char *name, void *drvdata,
> > +			const struct hwmon_chip_info *chip,
> > +			const struct attribute_group **groups)
> >  {
> >  	struct hwmon_device *hwdev;
> > -	int err, id;
> > +	struct device *hdev;
> > +	int i, j, err, id;
> >  
> >  	/* Do not accept invalid characters in hwmon name attribute */
> >  	if (name && (!strlen(name) || strpbrk(name, "-* \t\n")))
> > @@ -112,28 +401,128 @@ hwmon_device_register_with_groups(struct device *dev, const char *name,
> >  		goto ida_remove;
> >  	}
> >  
> > +	hdev = &hwdev->dev;
> > +
> > +	if (chip && chip->ops->is_visible) {
> 
> is_visible is documented as mandatory, so why would this check be needed
> (and not result in an error if failed?)
> 
Let me check. I don't recall if the documentation is at fault or the
code.

> > +		struct attribute **attrs;
> > +		int ngroups = 2;
> 
> I'd appreciate a comment here.
> 
Sure.

> > +
> > +		if (groups)
> > +			for (i = 0; groups[i]; i++)
> > +				ngroups++;
> > +
> > +		hwdev->groups = devm_kcalloc(dev, ngroups, sizeof(*groups),
> > +					     GFP_KERNEL);
> > +		if (!hwdev->groups)
> > +			return ERR_PTR(-ENOMEM);
> > +
> > +		attrs = __hwmon_create_attrs(dev, drvdata, chip);
> > +		if (IS_ERR(attrs)) {
> > +			err = PTR_ERR(attrs);
> > +			goto free_hwmon;
> > +		}
> > +
> > +		hwdev->group.attrs = attrs;
> > +		ngroups = 0;
> > +		hwdev->groups[ngroups++] = &hwdev->group;
> > +
> > +		if (groups) {
> > +			for (i = 0; groups[i]; i++)
> > +				hwdev->groups[ngroups++] = groups[i];
> > +		}
> > +
> > +		hdev->groups = hwdev->groups;
> > +	} else {
> > +		hdev->groups = groups;
> > +	}
> > +
> >  	hwdev->name = name;
> > -	hwdev->dev.class = &hwmon_class;
> > -	hwdev->dev.parent = dev;
> > -	hwdev->dev.groups = groups;
> > -	hwdev->dev.of_node = dev ? dev->of_node : NULL;
> > -	dev_set_drvdata(&hwdev->dev, drvdata);
> > -	dev_set_name(&hwdev->dev, HWMON_ID_FORMAT, id);
> > -	err = device_register(&hwdev->dev);
> > +	hdev->class = &hwmon_class;
> > +	hdev->parent = dev;
> > +	hdev->of_node = dev ? dev->of_node : NULL;
> > +	hwdev->chip = chip;
> > +	dev_set_drvdata(hdev, drvdata);
> > +	dev_set_name(hdev, HWMON_ID_FORMAT, id);
> > +	err = device_register(hdev);
> >  	if (err)
> > -		goto free;
> > +		goto free_hwmon;
> > +
> > +	if (chip && chip->ops->is_visible && chip->ops->read &&
> > +	    chip->info[0]->type == hwmon_chip &&
> > +	    (chip->info[0]->config[0] & HWMON_C_REGISTER_TZ)) {
> > +		const struct hwmon_channel_info **info = chip->info;
> > +
> > +		for (i = 1; info[i]; i++) {
> > +			if (info[i]->type != hwmon_temp)
> > +				continue;
> > +
> > +			for (j = 0; info[i]->config[j]; j++) {
> > +				if (!chip->ops->is_visible(drvdata, hwmon_temp,
> > +							   hwmon_temp_input, j))
> > +					continue;
> > +				if (info[i]->config[j] & HWMON_T_INPUT)
> > +					hwmon_thermal_add_sensor(dev, hwdev, j);
> > +			}
> > +		}
> > +	}
> >  
> > -	return &hwdev->dev;
> > +	return hdev;
> >  
> > -free:
> > +free_hwmon:
> >  	kfree(hwdev);
> >  ida_remove:
> >  	ida_simple_remove(&hwmon_ida, id);
> >  	return ERR_PTR(err);
> >  }
> > +
> > +/**
> > + * hwmon_device_register_with_groups - register w/ hwmon
> > + * @dev: the parent device
> > + * @name: hwmon name attribute
> > + * @drvdata: driver data to attach to created device
> > + * @groups: List of attribute groups to create
> > + *
> > + * hwmon_device_unregister() must be called when the device is no
> > + * longer needed.
> > + *
> > + * Returns the pointer to the new device.
> > + */
> > +struct device *
> > +hwmon_device_register_with_groups(struct device *dev, const char *name,
> > +				  void *drvdata,
> > +				  const struct attribute_group **groups)
> > +{
> > +	return __hwmon_device_register(dev, name, drvdata, NULL, groups);
> > +}
> >  EXPORT_SYMBOL_GPL(hwmon_device_register_with_groups);
> >  
> >  /**
> > + * hwmon_device_register_with_info - register w/ hwmon
> > + * @dev: the parent device
> > + * @name: hwmon name attribute
> > + * @drvdata: driver data to attach to created device
> > + * @info: Pointer to hwmon chip information
> > + * @groups - pointer to list of driver specific attribute groups
> 
> I don't find this clear. "driver specific" doesn't really tell me what
> it is. My first reaction when reviewing the changes from an API
> perspective was "the attributes would be generated from the
> hwmon_chip_info, so why are we still passing them as a parameter?" Now I
> seem to understand that "groups" here is meant for extra attributes
> which aren't part of standard sysfs interface. This would be NULL for
> most drivers, right?
> 
Correct.

> To clear up the confusion, what about renaming this parameter to
> "extra_groups"? And maybe rewording "driver specific" to "non-standard"?
> 
No problem with that.

> As a side note I am wondering if it would be reasonable to limit it to a
> single group, instead of a NULL-terminated array of groups. This would
> make the code a little more efficient.
> 
The underlying code is all the same; hwmon_device_register_with_groups()
also calls __hwmon_device_register(). I would prefer to keep the code as
common as possible.

> > + *
> > + * hwmon_device_unregister() must be called when the device is no
> > + * longer needed.
> > + *
> > + * Returns the pointer to the new device.
> > + */
> > +struct device *
> > +hwmon_device_register_with_info(struct device *dev, const char *name,
> > +				void *drvdata,
> > +				const struct hwmon_chip_info *chip,
> > +				const struct attribute_group **groups)
> > +{
> > +	if (chip && (!chip->ops || !chip->info))
> > +		return ERR_PTR(-EINVAL);
> > +
> > +	return __hwmon_device_register(dev, name, drvdata, chip, groups);
> > +}
> > +EXPORT_SYMBOL_GPL(hwmon_device_register_with_info);
> > +
> > +/**
> >   * hwmon_device_register - register w/ hwmon
> >   * @dev: the device to register
> >   *
> > @@ -211,6 +600,48 @@ error:
> >  }
> >  EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_groups);
> >  
> > +/**
> > + * devm_hwmon_device_register_with_info - register w/ hwmon
> > + * @dev: the parent device
> > + * @name: hwmon name attribute
> > + * @drvdata: driver data to attach to created device
> > + * @info: Pointer to hwmon chip information
> > + * @groups - pointer to list of driver specific attribute groups
> > + *
> > + * Returns the pointer to the new device. The new device is automatically
> > + * unregistered with the parent device.
> > + */
> > +struct device *
> > +devm_hwmon_device_register_with_info(struct device *dev, const char *name,
> > +				     void *drvdata,
> > +				     const struct hwmon_chip_info *chip,
> > +				     const struct attribute_group **groups)
> > +{
> > +	struct device **ptr, *hwdev;
> > +
> > +	if (!dev)
> > +		return ERR_PTR(-EINVAL);
> > +
> > +	ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL);
> > +	if (!ptr)
> > +		return ERR_PTR(-ENOMEM);
> > +
> > +	hwdev = hwmon_device_register_with_info(dev, name, drvdata, chip,
> > +						groups);
> > +	if (IS_ERR(hwdev))
> > +		goto error;
> > +
> > +	*ptr = hwdev;
> > +	devres_add(dev, ptr);
> > +
> > +	return hwdev;
> > +
> > +error:
> > +	devres_free(ptr);
> > +	return hwdev;
> > +}
> > +EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_info);
> > +
> >  static int devm_hwmon_match(struct device *dev, void *res, void *data)
> >  {
> >  	struct device **hwdev = res;
> > diff --git a/include/linux/hwmon.h b/include/linux/hwmon.h
> > index 09354f6c1d63..52e56d71d742 100644
> > --- a/include/linux/hwmon.h
> > +++ b/include/linux/hwmon.h
> > @@ -14,9 +14,147 @@
> >  #ifndef _HWMON_H_
> >  #define _HWMON_H_
> >  
> > +#include <linux/bitops.h>
> > +
> >  struct device;
> >  struct attribute_group;
> >  
> > +enum hwmon_sensor_types {
> > +	hwmon_chip,
> > +	hwmon_temp,
> > +	hwmon_in,
> > +	hwmon_curr,
> > +	hwmon_power,
> > +	hwmon_energy,
> > +};
> > +
> > +enum hwmon_chip_attributes {
> > +	hwmon_chip_temp_reset_history,
> > +	hwmon_chip_register_tz,
> > +	hwmon_chip_update_interval,
> > +	hwmon_chip_alarms,
> > +};
> > +
> > +#define HWMON_C_TEMP_RESET_HISTORY	BIT(hwmon_chip_temp_reset_history)
> > +#define HWMON_C_IN_RESET_HISTORY	BIT(hwmon_chip_in_reset_history)
> 
> Ideally this one should go in next patch ("Add voltage attribute support
> to new API".)
> 
> > +#define HWMON_C_REGISTER_TZ		BIT(hwmon_chip_register_tz)
> > +#define HWMON_C_UPDATE_INTERVAL		BIT(hwmon_chip_update_interval)
> > +#define HWMON_C_ALARMS			BIT(hwmon_chip_alarms)
> > +
> > +enum hwmon_temp_attributes {
> > +	hwmon_temp_input = 0,
> > +	hwmon_temp_type,
> > +	hwmon_temp_lcrit,
> > +	hwmon_temp_lcrit_hyst,
> > +	hwmon_temp_min,
> > +	hwmon_temp_min_hyst,
> > +	hwmon_temp_max,
> > +	hwmon_temp_max_hyst,
> > +	hwmon_temp_crit,
> > +	hwmon_temp_crit_hyst,
> > +	hwmon_temp_emergency,
> > +	hwmon_temp_emergency_hyst,
> > +	hwmon_temp_alarm,
> > +	hwmon_temp_lcrit_alarm,
> > +	hwmon_temp_min_alarm,
> > +	hwmon_temp_max_alarm,
> > +	hwmon_temp_crit_alarm,
> > +	hwmon_temp_emergency_alarm,
> > +	hwmon_temp_fault,
> > +	hwmon_temp_offset,
> > +	hwmon_temp_label,
> > +	hwmon_temp_lowest,
> > +	hwmon_temp_highest,
> > +	hwmon_temp_reset_history,
> > +};
> > +
> > +#define HWMON_T_INPUT		BIT(hwmon_temp_input)
> > +#define HWMON_T_TYPE		BIT(hwmon_temp_type)
> > +#define HWMON_T_LCRIT		BIT(hwmon_temp_lcrit)
> > +#define HWMON_T_LCRIT_HYST	BIT(hwmon_temp_lcrit_hyst)
> > +#define HWMON_T_MIN		BIT(hwmon_temp_min)
> > +#define HWMON_T_MIN_HYST	BIT(hwmon_temp_min_hyst)
> > +#define HWMON_T_MAX		BIT(hwmon_temp_max)
> > +#define HWMON_T_MAX_HYST	BIT(hwmon_temp_max_hyst)
> > +#define HWMON_T_CRIT		BIT(hwmon_temp_crit)
> > +#define HWMON_T_CRIT_HYST	BIT(hwmon_temp_crit_hyst)
> > +#define HWMON_T_EMERGENCY	BIT(hwmon_temp_emergency)
> > +#define HWMON_T_EMERGENCY_HYST	BIT(hwmon_temp_emergency_hyst)
> > +#define HWMON_T_MIN_ALARM	BIT(hwmon_temp_min_alarm)
> > +#define HWMON_T_MAX_ALARM	BIT(hwmon_temp_max_alarm)
> > +#define HWMON_T_CRIT_ALARM	BIT(hwmon_temp_crit_alarm)
> > +#define HWMON_T_EMERGENCY_ALARM	BIT(hwmon_temp_emergency_alarm)
> > +#define HWMON_T_FAULT		BIT(hwmon_temp_fault)
> > +#define HWMON_T_OFFSET		BIT(hwmon_temp_offset)
> > +#define HWMON_T_LABEL		BIT(hwmon_temp_label)
> > +#define HWMON_T_LOWEST		BIT(hwmon_temp_lowest)
> > +#define HWMON_T_HIGHEST		BIT(hwmon_temp_highest)
> > +#define HWMON_T_RESET_HISTORY	BIT(hwmon_temp_reset_history)
> > +
> > +/**
> > + * struct hwmon_ops - hwmon device operations
> > + * @is_visible: Callback to return attribute visibility. Mandatory.
> > + *		Parameters are:
> > + *		@const void *drvdata:
> > + *			Pointer to driver-private data structure passed
> > + *			as argument to hwmon_device_register_with_info().
> > + *		@type:	Sensor type
> > + *		@attr:	Sensor attribute
> > + *		@channel:
> > + *			Channel number
> > + *		The function returns the file permissions.
> > + *		If the return value is 0, no attribute will be created.
> > + * @read:       Read callback. Optional. If not provided, attributes
> > + *		will not be readable.
> > + *		Parameters are:
> > + *		@dev:	Pointer to hardware monitoring device
> > + *		@type:	Sensor type
> > + *		@attr:	Sensor attribute
> > + *		@channel:
> > + *			Channel number
> > + *		@val:	Pointer to returned value
> > + *		The function returns 0 on success or a negative error number.
> > + * @write:	Write callback. Optional. If not provided, attributes
> > + *		will not be writable.
> > + *		Parameters are:
> > + *		@dev:	Pointer to hardware monitoring device
> > + *		@type:	Sensor type
> > + *		@attr:	Sensor attribute
> > + *		@channel:
> > + *			Channel number
> > + *		@val:	Value to write
> > + *		The function returns 0 on success or a negative error number.
> > + */
> > +struct hwmon_ops {
> > +	umode_t (*is_visible)(const void *drvdata, enum hwmon_sensor_types type,
> > +			      u32 attr, int channel);
> > +	int (*read)(struct device *dev, enum hwmon_sensor_types type,
> > +		    u32 attr, int channel, long *val);
> > +	int (*write)(struct device *dev, enum hwmon_sensor_types type,
> > +		     u32 attr, int channel, long val);
> > +};
> > +
> > +/**
> > + * Channel information
> > + * @type:	Channel type.
> > + * @config:	Pointer to NULL-terminated list of channel parameters.
> > + *		Use for per-channel attributes.
> > + */
> > +struct hwmon_channel_info {
> > +	enum hwmon_sensor_types type;
> > +	const u32 *config;
> > +};
> > +
> > +/**
> > + * Chip configuration
> > + * @ops:	Pointer to hwmon operations.
> > + * @info:	Null-terminated list of channel information.
> > + */
> > +struct hwmon_chip_info {
> > +	const struct hwmon_ops *ops;
> > +	const struct hwmon_channel_info **info;
> > +};
> > +
> >  struct device *hwmon_device_register(struct device *dev);
> >  struct device *
> >  hwmon_device_register_with_groups(struct device *dev, const char *name,
> > @@ -26,6 +164,16 @@ struct device *
> >  devm_hwmon_device_register_with_groups(struct device *dev, const char *name,
> >  				       void *drvdata,
> >  				       const struct attribute_group **groups);
> > +struct device *
> > +hwmon_device_register_with_info(struct device *dev,
> > +				const char *name, void *drvdata,
> > +				const struct hwmon_chip_info *info,
> > +				const struct attribute_group **groups);
> > +struct device *
> > +devm_hwmon_device_register_with_info(struct device *dev,
> > +				     const char *name, void *drvdata,
> > +				     const struct hwmon_chip_info *info,
> > +				     const struct attribute_group **groups);
> >  
> >  void hwmon_device_unregister(struct device *dev);
> >  void devm_hwmon_device_unregister(struct device *dev);
> 
> This is adding a 4th and 5th way to register a hwmon device. Starts
> being a bit messy... Do you have any plans to get rid of some of the
> other functions to make things more consistent and efficient?
> 
Would be nice, but then someone would have to spend the time cleaning
up the old drivers to replace the old API, and for the most part we would
not be able to test the result. Are you sure that is worth the risk ?

Thanks,
Guenter

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH v3 2/9] hwmon: (core) New hwmon registration API
  2016-10-04 19:37       ` Guenter Roeck
  (?)
@ 2016-10-07 12:32       ` Jean Delvare
  2016-10-10 23:48           ` Guenter Roeck
  2016-10-16 18:20         ` Guenter Roeck
  -1 siblings, 2 replies; 26+ messages in thread
From: Jean Delvare @ 2016-10-07 12:32 UTC (permalink / raw)
  To: Guenter Roeck
  Cc: Jonathan Cameron, Zhang Rui, Eduardo Valentin, Punit Agrawal,
	linux-pm, linux-iio, linux-hwmon, linux-kernel

Hi Guenter,

On Tue, 4 Oct 2016 12:37:13 -0700, Guenter Roeck wrote:
> On Tue, Oct 04, 2016 at 10:45:59AM +0200, Jean Delvare wrote:
> > I see this patch is upstream now, but I had started reviewing it and
> > maybe some of my comments are still relevant.
> > 
> As always, I appreciate the feedback. I'll be happy to submit follow-up
> patches to address any concerns.
> 
> > It's not meant to be a complete review, which is why I had not sent it
> > yet :-(
> > 
> > Also I did not follow the first iterations of this patchset so my
> > apologies if I raise points which have already been discussed.
> > 
> > May I ask if any other subsystem is already doing anything like that?
> > 
> Depends what you mean with "anything like that". The API is inspired by
> the iio API and a little by the thermal API. The details are probably
> unique, though.

I meant the idea of describing the device capabilities and letting the
core code generate the device attributes (or anything else) based on
that data. The benefits are obvious, but it has a complexity and
run-time cost so I am wondering how popular this approach is.

> > FYI I gave a presentation about the hwmon device registration API
> > evolution last week at Kernel Recipes [1] in Paris and mentioned this
> > proposal.
> > 
> > [1] https://kernel-recipes.org/en/2016/
> > 
> > On dim., 2016-07-24 at 20:32 -0700, Guenter Roeck wrote:
> > > Up to now, each hwmon driver has to implement its own sysfs attributes.
> > > This requires a lot of template code, and distracts from the driver's core
> > > function to read and write chip registers.
> > > 
> > > To be able to reduce driver complexity, move sensor attribute handling
> > > and thermal zone registration into hwmon core. By using the new API,
> > > driver code and data size is typically reduced by 20-70%, depending
> > > on driver complexity and the number of sysfs attributes supported.
> > 
> > I looked at the diffstats for the drivers you have already converted and
> > couldn't see any such huge improvement... Some drivers appear to be even
> > larger after conversion?
> > 
> The above refers to code and data sizes.

You mean at the binary level?

Have you considered the case where several devices exist for the same
driver? Static attributes are shared between devices, but with the
generated ones this is no longer the case, right?

> > > (...)
> > > +static struct attribute *hwmon_genattr(struct device *dev,
> > > +				       const void *drvdata,
> > > +				       enum hwmon_sensor_types type,
> > > +				       u32 attr,
> > > +				       int index,
> > > +				       const char *template,
> > > +				       const struct hwmon_ops *ops)
> > > +{
> > > +	struct hwmon_device_attribute *hattr;
> > > +	struct device_attribute *dattr;
> > > +	struct attribute *a;
> > > +	umode_t mode;
> > > +	char *name;
> > > +
> > > +	/* The attribute is invisible if there is no template string */
> > > +	if (!template)
> > > +		return ERR_PTR(-ENOENT);
> > > +
> > > +	mode = ops->is_visible(drvdata, type, attr, index);
> > 
> > Any reason why you don't simply attach the provided ->is_visible to the
> > attribute group and let the driver core do the work?
> > 
> Parameter are all different
> 	umode_t (*is_visible)(const void *drvdata, enum hwmon_sensor_types type,
> 	                              u32 attr, int channel);
> vs.
> 	umode_t (*is_visible)(struct kobject *, struct attribute *, int);

But this is the consequence of how you implemented it, not the reason?

Now I seem to understand that delegating the check to the driver core
would make it happen later, which means you would have to instantiate
all attributes, even if some are never going to be used? So this is an
optimization?

But this prevents sharing the attributes between devices, as pointed
out above. Well, I guess you can't have it all. I don't know what is
the more important.

> > > +		return ERR_PTR(-ENOENT);
> > > +
> > > +	if ((mode & S_IRUGO) && !ops->read)
> > > +		return ERR_PTR(-EINVAL);
> > > +	if ((mode & S_IWUGO) && !ops->write)
> > > +		return ERR_PTR(-EINVAL);
> > > +
> > > +	if (type == hwmon_chip) {
> > 
> > This needs a comment. It's not obvious what "hwmon_chip" is supposed to
> > represent. From what I understand, maybe "hwmon_unique" would be a more
> > appropriate name.
> > 
> Those are meant to be for attributes which apply to the entire chip.

Not really. As you implemented it, it is meant for attributes which are
not specific to an input or output in particular. That is, attributes
which do not include a channel number. "update_interval" and
"alarms" (which I'd like to get rid of, btw) apply to the entire chip,
but "temp_reset_history" does not.

> Not sure if 'hwmon_unique' would reflect that better.
> From the documentation:
> 	"A virtual sensor type, used to describe attributes
> 	 which apply to the entire chip"
> 
> Do you have an idea for a better description ?

For one thing I would rename hwmon_chip_attr_templates to
hwmon_chip_attrs (or whatever, without _templates.) They are used
directly, as opposed to all other hwmon_*_attr_templates strings which
must be generated as needed.

This is the reason why the term "unique" came to my mind: the absence
of %d in the name makes this array special. If we can't find a name
that expresses that, I think a comment should be added to explain it.

As for the description in the documentation, what about:

	"A virtual sensor type, used to describe attributes
	which are not bound to a specific input or output"

"input or output" could be shortened as "channel" but I think spelling
it out is more explicit.

> > > +		name = (char *)template;
> > > +	} else {
> > > +		name = devm_kzalloc(dev, strlen(template) + 16, GFP_KERNEL);
> > > +		if (!name)
> > > +			return ERR_PTR(-ENOMEM);
> > > +		scnprintf(name, strlen(template) + 16, template,
> > > +			  index + hwmon_attr_base(type));
> > > +	}
> > > +
> > > +	hattr = devm_kzalloc(dev, sizeof(*hattr), GFP_KERNEL);
> > > +	if (!hattr)
> > > +		return ERR_PTR(-ENOMEM);
> > 
> > So basically you are doing 1 or 2 memory allocations for every
> > attribute? Looks quite bad from a memory fragmentation
> > perspective :-( Not to mention performance. Given that you have all the
> > data at hand before you start, can't you preallocate an array with the
> > right number of hattr and pick from it? That would at least solve half
> > of the problem.

FTR I took a quick look at the iio code and there seems to be something
like the idea above implemented in iio_device_register_sysfs(). But
attributes themselves as instantiated by iio_device_register_sysfs()
are still allocated individually. But hey I'm not familiar with the iio
code anyway, I'm sure you know it better than I do.

> > Something similar for string allocation may work too, although it's
> > somewhat more complex due to the variable length. But I think the
> > abituguru3 driver is doing it right, so maybe you can too.
>
> I'll look into it.

> > (...)
> > As a side note I am wondering if it would be reasonable to limit it to a
> > single group, instead of a NULL-terminated array of groups. This would
> > make the code a little more efficient.
> > 
> The underlying code is all the same; hwmon_device_register_with_groups()
> also calls __hwmon_device_register(). I would prefer to keep the code as
> common as possible.

OK, that makes sense. Maybe it can be revisited when/if we manage to
remove some of the old registration methods.

> > > (...)
> > > @@ -26,6 +164,16 @@ struct device *
> > >  devm_hwmon_device_register_with_groups(struct device *dev, const char *name,
> > >  				       void *drvdata,
> > >  				       const struct attribute_group **groups);
> > > +struct device *
> > > +hwmon_device_register_with_info(struct device *dev,
> > > +				const char *name, void *drvdata,
> > > +				const struct hwmon_chip_info *info,
> > > +				const struct attribute_group **groups);
> > > +struct device *
> > > +devm_hwmon_device_register_with_info(struct device *dev,
> > > +				     const char *name, void *drvdata,
> > > +				     const struct hwmon_chip_info *info,
> > > +				     const struct attribute_group **groups);
> > >  
> > >  void hwmon_device_unregister(struct device *dev);
> > >  void devm_hwmon_device_unregister(struct device *dev);
> > 
> > This is adding a 4th and 5th way to register a hwmon device. Starts
> > being a bit messy... Do you have any plans to get rid of some of the
> > other functions to make things more consistent and efficient?
>
> Would be nice, but then someone would have to spend the time cleaning
> up the old drivers to replace the old API, and for the most part we would
> not be able to test the result. Are you sure that is worth the risk ?

What I had in mind was rather along the lines of marking the function
as __deprecated, adding a run-time notice message when it is called,
and let whoever uses these driver contribute the fix. Call me an
optimistic :-) There are 54 drivers still using
hwmon_device_register(), out of 157.

-- 
Jean Delvare
SUSE L3 Support

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH v3 2/9] hwmon: (core) New hwmon registration API
  2016-10-07 12:32       ` Jean Delvare
@ 2016-10-10 23:48           ` Guenter Roeck
  2016-10-16 18:20         ` Guenter Roeck
  1 sibling, 0 replies; 26+ messages in thread
From: Guenter Roeck @ 2016-10-10 23:48 UTC (permalink / raw)
  To: Jean Delvare
  Cc: Jonathan Cameron, Zhang Rui, Eduardo Valentin, Punit Agrawal,
	linux-pm, linux-iio, linux-hwmon, linux-kernel

On Fri, Oct 07, 2016 at 02:32:13PM +0200, Jean Delvare wrote:
> Hi Guenter,
> 
> On Tue, 4 Oct 2016 12:37:13 -0700, Guenter Roeck wrote:
> > On Tue, Oct 04, 2016 at 10:45:59AM +0200, Jean Delvare wrote:
> > > I see this patch is upstream now, but I had started reviewing it and
> > > maybe some of my comments are still relevant.
> > > 
> > As always, I appreciate the feedback. I'll be happy to submit follow-up
> > patches to address any concerns.
> > 
> > > It's not meant to be a complete review, which is why I had not sent it
> > > yet :-(
> > > 
> > > Also I did not follow the first iterations of this patchset so my
> > > apologies if I raise points which have already been discussed.
> > > 
> > > May I ask if any other subsystem is already doing anything like that?
> > > 
> > Depends what you mean with "anything like that". The API is inspired by
> > the iio API and a little by the thermal API. The details are probably
> > unique, though.
> 
> I meant the idea of describing the device capabilities and letting the
> core code generate the device attributes (or anything else) based on
> that data. The benefits are obvious, but it has a complexity and
> run-time cost so I am wondering how popular this approach is.
> 

The goal of this patch series was to provide abstraction between driver
and userspace ABI. I think this is quite common in the kernel.

I am not sure I undersatand the run-time cost concern. The main run-time
cost occurs during device instantiation and only happens once. Other
abstraction APIs in the kernel have a much higher runtime cost, and
instantiation tends to be quite costly as well.

The new API has the addd benefit of reducing driver size, in some cases
significantly. Maybe I am off, but I considered this important, which is
why I mentioned it. Maybe I should not have mentioned it at all, and
instead have focused on abstraction alone.

> You mean at the binary level?
> 
> Have you considered the case where several devices exist for the same
> driver? Static attributes are shared between devices, but with the
> generated ones this is no longer the case, right?
> 
As mentioned above, I considered static sizes to be more important.
Sure, there are situations where multiple instances of a driver are
loaded, but those are not typically memory size limited. But, again,
I guess maybe I should not have mentioned driver size impact at all.

> > > > +
> > > > +	mode = ops->is_visible(drvdata, type, attr, index);
> > > 
> > > Any reason why you don't simply attach the provided ->is_visible to the
> > > attribute group and let the driver core do the work?
> > > 
> > Parameter are all different
> > 	umode_t (*is_visible)(const void *drvdata, enum hwmon_sensor_types type,
> > 	                              u32 attr, int channel);
> > vs.
> > 	umode_t (*is_visible)(struct kobject *, struct attribute *, int);
> 
> But this is the consequence of how you implemented it, not the reason?
> 
Not really. The drivers are now abstracted from the ABI and don't know anything
about it. Effectively I would need a shim is_visible function to convert the
sysfs parameters into driver function parameters. That should still be possible,
I just don't immediately see the benefits.

> Now I seem to understand that delegating the check to the driver core
> would make it happen later, which means you would have to instantiate
> all attributes, even if some are never going to be used? So this is an
> optimization?
> 
Not for that purpose. I didn't even get to that point because I did not see
a value in the shim function mentioned above.

> But this prevents sharing the attributes between devices, as pointed
> out above. Well, I guess you can't have it all. I don't know what is
> the more important.
> 
> > > > +		return ERR_PTR(-ENOENT);
> > > > +
> > > > +	if ((mode & S_IRUGO) && !ops->read)
> > > > +		return ERR_PTR(-EINVAL);
> > > > +	if ((mode & S_IWUGO) && !ops->write)
> > > > +		return ERR_PTR(-EINVAL);
> > > > +
> > > > +	if (type == hwmon_chip) {
> > > 
> > > This needs a comment. It's not obvious what "hwmon_chip" is supposed to
> > > represent. From what I understand, maybe "hwmon_unique" would be a more
> > > appropriate name.
> > > 
> > Those are meant to be for attributes which apply to the entire chip.
> 
> Not really. As you implemented it, it is meant for attributes which are
> not specific to an input or output in particular. That is, attributes
> which do not include a channel number. "update_interval" and
> "alarms" (which I'd like to get rid of, btw) apply to the entire chip,
> but "temp_reset_history" does not.
> 
> > Not sure if 'hwmon_unique' would reflect that better.
> > From the documentation:
> > 	"A virtual sensor type, used to describe attributes
> > 	 which apply to the entire chip"
> > 
> > Do you have an idea for a better description ?
> 
> For one thing I would rename hwmon_chip_attr_templates to
> hwmon_chip_attrs (or whatever, without _templates.) They are used
> directly, as opposed to all other hwmon_*_attr_templates strings which
> must be generated as needed.
> 
> This is the reason why the term "unique" came to my mind: the absence
> of %d in the name makes this array special. If we can't find a name
> that expresses that, I think a comment should be added to explain it.
> 
> As for the description in the documentation, what about:
> 
> 	"A virtual sensor type, used to describe attributes
> 	which are not bound to a specific input or output"
> 
> "input or output" could be shortened as "channel" but I think spelling
> it out is more explicit.
> 
I have no problem with that.

> > > > +		name = (char *)template;
> > > > +	} else {
> > > > +		name = devm_kzalloc(dev, strlen(template) + 16, GFP_KERNEL);
> > > > +		if (!name)
> > > > +			return ERR_PTR(-ENOMEM);
> > > > +		scnprintf(name, strlen(template) + 16, template,
> > > > +			  index + hwmon_attr_base(type));
> > > > +	}
> > > > +
> > > > +	hattr = devm_kzalloc(dev, sizeof(*hattr), GFP_KERNEL);
> > > > +	if (!hattr)
> > > > +		return ERR_PTR(-ENOMEM);
> > > 
> > > So basically you are doing 1 or 2 memory allocations for every
> > > attribute? Looks quite bad from a memory fragmentation
> > > perspective :-( Not to mention performance. Given that you have all the
> > > data at hand before you start, can't you preallocate an array with the
> > > right number of hattr and pick from it? That would at least solve half
> > > of the problem.
> 
> FTR I took a quick look at the iio code and there seems to be something
> like the idea above implemented in iio_device_register_sysfs(). But
> attributes themselves as instantiated by iio_device_register_sysfs()
> are still allocated individually. But hey I'm not familiar with the iio
> code anyway, I'm sure you know it better than I do.
> 
> > > Something similar for string allocation may work too, although it's
> > > somewhat more complex due to the variable length. But I think the
> > > abituguru3 driver is doing it right, so maybe you can too.
> >
> > I'll look into it.
> 
> > > (...)
> > > As a side note I am wondering if it would be reasonable to limit it to a
> > > single group, instead of a NULL-terminated array of groups. This would
> > > make the code a little more efficient.
> > > 
> > The underlying code is all the same; hwmon_device_register_with_groups()
> > also calls __hwmon_device_register(). I would prefer to keep the code as
> > common as possible.
> 
> OK, that makes sense. Maybe it can be revisited when/if we manage to
> remove some of the old registration methods.
> 
> > > > (...)
> > > > @@ -26,6 +164,16 @@ struct device *
> > > >  devm_hwmon_device_register_with_groups(struct device *dev, const char *name,
> > > >  				       void *drvdata,
> > > >  				       const struct attribute_group **groups);
> > > > +struct device *
> > > > +hwmon_device_register_with_info(struct device *dev,
> > > > +				const char *name, void *drvdata,
> > > > +				const struct hwmon_chip_info *info,
> > > > +				const struct attribute_group **groups);
> > > > +struct device *
> > > > +devm_hwmon_device_register_with_info(struct device *dev,
> > > > +				     const char *name, void *drvdata,
> > > > +				     const struct hwmon_chip_info *info,
> > > > +				     const struct attribute_group **groups);
> > > >  
> > > >  void hwmon_device_unregister(struct device *dev);
> > > >  void devm_hwmon_device_unregister(struct device *dev);
> > > 
> > > This is adding a 4th and 5th way to register a hwmon device. Starts
> > > being a bit messy... Do you have any plans to get rid of some of the
> > > other functions to make things more consistent and efficient?
> >
> > Would be nice, but then someone would have to spend the time cleaning
> > up the old drivers to replace the old API, and for the most part we would
> > not be able to test the result. Are you sure that is worth the risk ?
> 
> What I had in mind was rather along the lines of marking the function
> as __deprecated, adding a run-time notice message when it is called,
> and let whoever uses these driver contribute the fix. Call me an
> optimistic :-) There are 54 drivers still using
> hwmon_device_register(), out of 157.
> 
For most of those testing was not possible, otherwise I would have converted
them by now.

I am not sure about deprecated; doesn't that mean a failure to compile with
-Werror ? That would not help much.

Thanks,
Guenter

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH v3 2/9] hwmon: (core) New hwmon registration API
@ 2016-10-10 23:48           ` Guenter Roeck
  0 siblings, 0 replies; 26+ messages in thread
From: Guenter Roeck @ 2016-10-10 23:48 UTC (permalink / raw)
  To: Jean Delvare
  Cc: Jonathan Cameron, Zhang Rui, Eduardo Valentin, Punit Agrawal,
	linux-pm-u79uwXL29TY76Z2rM5mHXA,
	linux-iio-u79uwXL29TY76Z2rM5mHXA,
	linux-hwmon-u79uwXL29TY76Z2rM5mHXA,
	linux-kernel-u79uwXL29TY76Z2rM5mHXA

On Fri, Oct 07, 2016 at 02:32:13PM +0200, Jean Delvare wrote:
> Hi Guenter,
> 
> On Tue, 4 Oct 2016 12:37:13 -0700, Guenter Roeck wrote:
> > On Tue, Oct 04, 2016 at 10:45:59AM +0200, Jean Delvare wrote:
> > > I see this patch is upstream now, but I had started reviewing it and
> > > maybe some of my comments are still relevant.
> > > 
> > As always, I appreciate the feedback. I'll be happy to submit follow-up
> > patches to address any concerns.
> > 
> > > It's not meant to be a complete review, which is why I had not sent it
> > > yet :-(
> > > 
> > > Also I did not follow the first iterations of this patchset so my
> > > apologies if I raise points which have already been discussed.
> > > 
> > > May I ask if any other subsystem is already doing anything like that?
> > > 
> > Depends what you mean with "anything like that". The API is inspired by
> > the iio API and a little by the thermal API. The details are probably
> > unique, though.
> 
> I meant the idea of describing the device capabilities and letting the
> core code generate the device attributes (or anything else) based on
> that data. The benefits are obvious, but it has a complexity and
> run-time cost so I am wondering how popular this approach is.
> 

The goal of this patch series was to provide abstraction between driver
and userspace ABI. I think this is quite common in the kernel.

I am not sure I undersatand the run-time cost concern. The main run-time
cost occurs during device instantiation and only happens once. Other
abstraction APIs in the kernel have a much higher runtime cost, and
instantiation tends to be quite costly as well.

The new API has the addd benefit of reducing driver size, in some cases
significantly. Maybe I am off, but I considered this important, which is
why I mentioned it. Maybe I should not have mentioned it at all, and
instead have focused on abstraction alone.

> You mean at the binary level?
> 
> Have you considered the case where several devices exist for the same
> driver? Static attributes are shared between devices, but with the
> generated ones this is no longer the case, right?
> 
As mentioned above, I considered static sizes to be more important.
Sure, there are situations where multiple instances of a driver are
loaded, but those are not typically memory size limited. But, again,
I guess maybe I should not have mentioned driver size impact at all.

> > > > +
> > > > +	mode = ops->is_visible(drvdata, type, attr, index);
> > > 
> > > Any reason why you don't simply attach the provided ->is_visible to the
> > > attribute group and let the driver core do the work?
> > > 
> > Parameter are all different
> > 	umode_t (*is_visible)(const void *drvdata, enum hwmon_sensor_types type,
> > 	                              u32 attr, int channel);
> > vs.
> > 	umode_t (*is_visible)(struct kobject *, struct attribute *, int);
> 
> But this is the consequence of how you implemented it, not the reason?
> 
Not really. The drivers are now abstracted from the ABI and don't know anything
about it. Effectively I would need a shim is_visible function to convert the
sysfs parameters into driver function parameters. That should still be possible,
I just don't immediately see the benefits.

> Now I seem to understand that delegating the check to the driver core
> would make it happen later, which means you would have to instantiate
> all attributes, even if some are never going to be used? So this is an
> optimization?
> 
Not for that purpose. I didn't even get to that point because I did not see
a value in the shim function mentioned above.

> But this prevents sharing the attributes between devices, as pointed
> out above. Well, I guess you can't have it all. I don't know what is
> the more important.
> 
> > > > +		return ERR_PTR(-ENOENT);
> > > > +
> > > > +	if ((mode & S_IRUGO) && !ops->read)
> > > > +		return ERR_PTR(-EINVAL);
> > > > +	if ((mode & S_IWUGO) && !ops->write)
> > > > +		return ERR_PTR(-EINVAL);
> > > > +
> > > > +	if (type == hwmon_chip) {
> > > 
> > > This needs a comment. It's not obvious what "hwmon_chip" is supposed to
> > > represent. From what I understand, maybe "hwmon_unique" would be a more
> > > appropriate name.
> > > 
> > Those are meant to be for attributes which apply to the entire chip.
> 
> Not really. As you implemented it, it is meant for attributes which are
> not specific to an input or output in particular. That is, attributes
> which do not include a channel number. "update_interval" and
> "alarms" (which I'd like to get rid of, btw) apply to the entire chip,
> but "temp_reset_history" does not.
> 
> > Not sure if 'hwmon_unique' would reflect that better.
> > From the documentation:
> > 	"A virtual sensor type, used to describe attributes
> > 	 which apply to the entire chip"
> > 
> > Do you have an idea for a better description ?
> 
> For one thing I would rename hwmon_chip_attr_templates to
> hwmon_chip_attrs (or whatever, without _templates.) They are used
> directly, as opposed to all other hwmon_*_attr_templates strings which
> must be generated as needed.
> 
> This is the reason why the term "unique" came to my mind: the absence
> of %d in the name makes this array special. If we can't find a name
> that expresses that, I think a comment should be added to explain it.
> 
> As for the description in the documentation, what about:
> 
> 	"A virtual sensor type, used to describe attributes
> 	which are not bound to a specific input or output"
> 
> "input or output" could be shortened as "channel" but I think spelling
> it out is more explicit.
> 
I have no problem with that.

> > > > +		name = (char *)template;
> > > > +	} else {
> > > > +		name = devm_kzalloc(dev, strlen(template) + 16, GFP_KERNEL);
> > > > +		if (!name)
> > > > +			return ERR_PTR(-ENOMEM);
> > > > +		scnprintf(name, strlen(template) + 16, template,
> > > > +			  index + hwmon_attr_base(type));
> > > > +	}
> > > > +
> > > > +	hattr = devm_kzalloc(dev, sizeof(*hattr), GFP_KERNEL);
> > > > +	if (!hattr)
> > > > +		return ERR_PTR(-ENOMEM);
> > > 
> > > So basically you are doing 1 or 2 memory allocations for every
> > > attribute? Looks quite bad from a memory fragmentation
> > > perspective :-( Not to mention performance. Given that you have all the
> > > data at hand before you start, can't you preallocate an array with the
> > > right number of hattr and pick from it? That would at least solve half
> > > of the problem.
> 
> FTR I took a quick look at the iio code and there seems to be something
> like the idea above implemented in iio_device_register_sysfs(). But
> attributes themselves as instantiated by iio_device_register_sysfs()
> are still allocated individually. But hey I'm not familiar with the iio
> code anyway, I'm sure you know it better than I do.
> 
> > > Something similar for string allocation may work too, although it's
> > > somewhat more complex due to the variable length. But I think the
> > > abituguru3 driver is doing it right, so maybe you can too.
> >
> > I'll look into it.
> 
> > > (...)
> > > As a side note I am wondering if it would be reasonable to limit it to a
> > > single group, instead of a NULL-terminated array of groups. This would
> > > make the code a little more efficient.
> > > 
> > The underlying code is all the same; hwmon_device_register_with_groups()
> > also calls __hwmon_device_register(). I would prefer to keep the code as
> > common as possible.
> 
> OK, that makes sense. Maybe it can be revisited when/if we manage to
> remove some of the old registration methods.
> 
> > > > (...)
> > > > @@ -26,6 +164,16 @@ struct device *
> > > >  devm_hwmon_device_register_with_groups(struct device *dev, const char *name,
> > > >  				       void *drvdata,
> > > >  				       const struct attribute_group **groups);
> > > > +struct device *
> > > > +hwmon_device_register_with_info(struct device *dev,
> > > > +				const char *name, void *drvdata,
> > > > +				const struct hwmon_chip_info *info,
> > > > +				const struct attribute_group **groups);
> > > > +struct device *
> > > > +devm_hwmon_device_register_with_info(struct device *dev,
> > > > +				     const char *name, void *drvdata,
> > > > +				     const struct hwmon_chip_info *info,
> > > > +				     const struct attribute_group **groups);
> > > >  
> > > >  void hwmon_device_unregister(struct device *dev);
> > > >  void devm_hwmon_device_unregister(struct device *dev);
> > > 
> > > This is adding a 4th and 5th way to register a hwmon device. Starts
> > > being a bit messy... Do you have any plans to get rid of some of the
> > > other functions to make things more consistent and efficient?
> >
> > Would be nice, but then someone would have to spend the time cleaning
> > up the old drivers to replace the old API, and for the most part we would
> > not be able to test the result. Are you sure that is worth the risk ?
> 
> What I had in mind was rather along the lines of marking the function
> as __deprecated, adding a run-time notice message when it is called,
> and let whoever uses these driver contribute the fix. Call me an
> optimistic :-) There are 54 drivers still using
> hwmon_device_register(), out of 157.
> 
For most of those testing was not possible, otherwise I would have converted
them by now.

I am not sure about deprecated; doesn't that mean a failure to compile with
-Werror ? That would not help much.

Thanks,
Guenter

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH v3 2/9] hwmon: (core) New hwmon registration API
  2016-10-10 23:48           ` Guenter Roeck
  (?)
@ 2016-10-11 12:29           ` Jean Delvare
  -1 siblings, 0 replies; 26+ messages in thread
From: Jean Delvare @ 2016-10-11 12:29 UTC (permalink / raw)
  To: Guenter Roeck
  Cc: Jonathan Cameron, Zhang Rui, Eduardo Valentin, Punit Agrawal,
	linux-pm, linux-iio, linux-hwmon, linux-kernel

Hi Guenter,

On Mon, 10 Oct 2016 16:48:22 -0700, Guenter Roeck wrote:
> On Fri, Oct 07, 2016 at 02:32:13PM +0200, Jean Delvare wrote:
> > On Tue, 4 Oct 2016 12:37:13 -0700, Guenter Roeck wrote:
> > > On Tue, Oct 04, 2016 at 10:45:59AM +0200, Jean Delvare wrote:
> > > > May I ask if any other subsystem is already doing anything like that?
> > > > 
> > > Depends what you mean with "anything like that". The API is inspired by
> > > the iio API and a little by the thermal API. The details are probably
> > > unique, though.
> > 
> > I meant the idea of describing the device capabilities and letting the
> > core code generate the device attributes (or anything else) based on
> > that data. The benefits are obvious, but it has a complexity and
> > run-time cost so I am wondering how popular this approach is.
> 
> The goal of this patch series was to provide abstraction between driver
> and userspace ABI. I think this is quite common in the kernel.

Yes, you are right. I am so used to the (wrong) way hwmon did that I
did not realize all other subsystems already provided some form of
abstraction for their device drivers. Even i2c does, so I should know.

> I am not sure I undersatand the run-time cost concern. The main run-time
> cost occurs during device instantiation and only happens once. Other
> abstraction APIs in the kernel have a much higher runtime cost, and
> instantiation tends to be quite costly as well.

I'm not too worried about this one, except for the memory allocations
as mentioned previously.

I am more concerned about the per-sysfs read overhead. Compared to the
old model, you have another indirection call for each access. And for
each driver, you end up with a single function to read from all
attributes. You will inevitably end up with a huge switch/case
statement to figure out what value to return. This smells like linear
search? Not sure if the compiler can optimize it. I was also wondering
how cache-friendly such a large function can be.

But anyway, I didn't measure anything, it's pure speculation on my
side. And it's probably irrelevant as your change has many benefits
anyway. And I wouldn't even ask myself the question if things had been
implemented right in the first place. So just ignore me ^^

> The new API has the addd benefit of reducing driver size, in some cases
> significantly. Maybe I am off, but I considered this important, which is
> why I mentioned it. Maybe I should not have mentioned it at all, and
> instead have focused on abstraction alone.

You were very right mentioning it, I mention it every time a patch of
mine reduces binary size. That's one of the actual benefits of the
change, no reason to silent it.

> > You mean at the binary level?
> > 
> > Have you considered the case where several devices exist for the same
> > driver? Static attributes are shared between devices, but with the
> > generated ones this is no longer the case, right?
>
> As mentioned above, I considered static sizes to be more important.
> Sure, there are situations where multiple instances of a driver are
> loaded, but those are not typically memory size limited. But, again,
> I guess maybe I should not have mentioned driver size impact at all.

I did not make any measurement, and I won't take the time to make them.
So I have no idea how the various costs compare to each other. Which in
turn means I don't have any point here, and we can go with what you
have ;-)

Really I was only commenting out loud while reviewing. Doesn't mean
much...

> > > > (...)
> > > > This is adding a 4th and 5th way to register a hwmon device. Starts
> > > > being a bit messy... Do you have any plans to get rid of some of the
> > > > other functions to make things more consistent and efficient?
> > >
> > > Would be nice, but then someone would have to spend the time cleaning
> > > up the old drivers to replace the old API, and for the most part we would
> > > not be able to test the result. Are you sure that is worth the risk ?
> > 
> > What I had in mind was rather along the lines of marking the function
> > as __deprecated, adding a run-time notice message when it is called,
> > and let whoever uses these driver contribute the fix. Call me an
> > optimistic :-) There are 54 drivers still using
> > hwmon_device_register(), out of 157.
>
> For most of those testing was not possible, otherwise I would have converted
> them by now.
> 
> I am not sure about deprecated; doesn't that mean a failure to compile with
> -Werror ? That would not help much.

It causes gcc to generate a warning, so I guess it would indeed break
with -Werror. But this option isn't enabled by default, and I doubt
anyone can actually build a general-purpose kernel with that option
enabled. If it's a custom kernel then maybe there's one hwmon driver
enabled, and if that one needs conversion, then you've just found your
tester ;-)

If you are worried about build-time __deprecated, you can also just
spit a message in the logs when hwmon_device_register() is called.
Won't break anything and may get you some tester, or even someone to
convert the driver for you.

But then again, you are pretty much in charge of the hwmon subsystem by
now (and you are doing that very well, thank you very much) so I'm
really only making suggestions, which you are totally free to ignore if
they seem irrelevant to you.

-- 
Jean Delvare
SUSE L3 Support

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH v3 2/9] hwmon: (core) New hwmon registration API
  2016-10-07 12:32       ` Jean Delvare
  2016-10-10 23:48           ` Guenter Roeck
@ 2016-10-16 18:20         ` Guenter Roeck
  1 sibling, 0 replies; 26+ messages in thread
From: Guenter Roeck @ 2016-10-16 18:20 UTC (permalink / raw)
  To: Jean Delvare
  Cc: Jonathan Cameron, Zhang Rui, Eduardo Valentin, Punit Agrawal,
	linux-pm, linux-iio, linux-hwmon, linux-kernel

Hi Jean,

On 10/07/2016 05:32 AM, Jean Delvare wrote:
[ ... ]
>
>>>> +		name = (char *)template;
>>>> +	} else {
>>>> +		name = devm_kzalloc(dev, strlen(template) + 16, GFP_KERNEL);
>>>> +		if (!name)
>>>> +			return ERR_PTR(-ENOMEM);
>>>> +		scnprintf(name, strlen(template) + 16, template,
>>>> +			  index + hwmon_attr_base(type));
>>>> +	}
>>>> +
>>>> +	hattr = devm_kzalloc(dev, sizeof(*hattr), GFP_KERNEL);
>>>> +	if (!hattr)
>>>> +		return ERR_PTR(-ENOMEM);
>>>
>>> So basically you are doing 1 or 2 memory allocations for every
>>> attribute? Looks quite bad from a memory fragmentation
>>> perspective :-( Not to mention performance. Given that you have all the
>>> data at hand before you start, can't you preallocate an array with the
>>> right number of hattr and pick from it? That would at least solve half
>>> of the problem.
>
> FTR I took a quick look at the iio code and there seems to be something
> like the idea above implemented in iio_device_register_sysfs(). But
> attributes themselves as instantiated by iio_device_register_sysfs()
> are still allocated individually. But hey I'm not familiar with the iio
> code anyway, I'm sure you know it better than I do.
>
>>> Something similar for string allocation may work too, although it's
>>> somewhat more complex due to the variable length. But I think the
>>> abituguru3 driver is doing it right, so maybe you can too.
>>
>> I'll look into it.
>

Merging name allocation and hattr allocation turned out to be easy by adding
the name to struct hwmon_device_attribute. However, allocating an array of
struct hwmon_device_attribute for all attributes in one go is more difficult.
Main issue is that we _don't_ really know how many attributes are going to be
created; we only know the ceiling, which is calculated in __hwmon_create_attrs().
That number is currently used to allocate the array of attributes. The extra
pointers did not seem that important there (to me). However, allocating an
array of struct hwmon_device_attribute would result in memory being allocated
for non-existing attributes as well (ie those for which is_visible returns 0),
and that would at least potentially be much more. We can either accept that,
or I would have to implement a second pass over the attributes to determine
how many are actually implemented, or we could leave the per-attribute
allocation unchanged.

Thoughts ?

Thanks,
Guenter


^ permalink raw reply	[flat|nested] 26+ messages in thread

end of thread, other threads:[~2016-10-16 18:20 UTC | newest]

Thread overview: 26+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-07-25  3:32 [PATCH v3 0/9] hwmon: New hwmon registration API Guenter Roeck
2016-07-25  3:32 ` [PATCH v3 1/9] hwmon: (core) Order include files alphabetically Guenter Roeck
2016-09-27 21:13   ` Jean Delvare
2016-07-25  3:32 ` [PATCH v3 2/9] hwmon: (core) New hwmon registration API Guenter Roeck
2016-08-12  8:16   ` Keerthy
2016-08-12  8:16     ` Keerthy
2016-08-12 12:40     ` Guenter Roeck
2016-10-04  8:45   ` Jean Delvare
2016-10-04  8:45     ` Jean Delvare
2016-10-04 19:37     ` Guenter Roeck
2016-10-04 19:37       ` Guenter Roeck
2016-10-07 12:32       ` Jean Delvare
2016-10-10 23:48         ` Guenter Roeck
2016-10-10 23:48           ` Guenter Roeck
2016-10-11 12:29           ` Jean Delvare
2016-10-16 18:20         ` Guenter Roeck
2016-07-25  3:32 ` [PATCH v3 3/9] hwmon: (core) Add voltage attribute support to new API Guenter Roeck
2016-07-25  3:32 ` [PATCH v3 4/9] hwmon: (core) Add current " Guenter Roeck
2016-07-25  3:32   ` Guenter Roeck
2016-07-25  3:32 ` [PATCH v3 5/9] hwmon: (core) Add power " Guenter Roeck
2016-07-25  3:32 ` [PATCH v3 6/9] hwmon: (core) Add energy and humidity " Guenter Roeck
2016-07-25  3:32 ` [PATCH v3 7/9] hwmon: (core) Add fan " Guenter Roeck
2016-07-25  3:32 ` [PATCH v3 8/9] hwmon: (core) Document new kernel API Guenter Roeck
2016-07-25  3:32   ` Guenter Roeck
2016-07-25  3:32 ` [PATCH v3 9/9] hwmon: (core) Add basic pwm attribute support to new API Guenter Roeck
2016-07-25  3:32   ` Guenter Roeck

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.