Skip to contentSkip to conversation

IIO in kernel interfaces (pull)

PATCH Superseded Jonathan Cameron 5 participants 14 messages Nov 7, 2011 – Nov 11, 2011

Subsystems (4)

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Jonathan Cameron

author maintainer
#0 lore raw

v4 cover IIO in kernel interfaces (pull)

Dear All,

Changes since V3 are all in processing of raw values to give sensible hwmon outputs in response to Guenter's comments.

Also rebased on the new version of the IIO core.

Note this is just the association infrastructure and the pull interfaces. Thus we can handle drivers polling for values. Doing push interfaces (interrupt triggered more or less) requires a lot more of the IIO intrastructure to be in place and so will have to wait a little while. This is available for the staging tree but will will require the second large part of IIO to move out of staging (triggered and buffered capture) plus needs a little more work and a lot more testing. As the cover letter for the push interface epxlains there are some complexities of the interaction of push and pull interfaces on the same physical device. These are independent of the external interfaces so shouldn't block this support merging.

The final patch is not intended for merging, but rather acts as an example of the board configuration needed.

Since V2:

Fixes as per Guenter's comments on the iio_hwmon driver. Only one that differred slightly from Guenters suggestion is the value calculation where I reordered things to reduce the chance of overflow as well as casting to longs. Thanks Guenter!

Since V1:
Uses the bus's list of devices instead of having a local one.
(thanks to Lars-Peter).

Added are interfaces for getting all channels mapped to a particular device.

Hwmon driver is now vaguely complete - hence cc'd hwmon. If people are happy I'd expect this to ultimately end up in drivers/hmwon. Right now it is acting as a test driver for these interfaces.

Thanks and all comments welcome. I've added hwmon to the cc list this time round.

V1 message:

There are obviously some rough corners in here that will need cleaning up. For now I've just put this out there to see if anyone radically disagrees on the direction. It sits on top the recent rfc to move first bit of IIO out of staging with a few buglets fixed. Best bet if anyone wants to test is to pull from:

https://github.com/jic23/linux-iio/tree/outofstaging

which now also includes these patches.

Intereresting patches are 3 and 4. 5 gives a trivial example of a driver using this (hwmon driver that only takes first matching channel and sticks it out as in1_input - breaks all sorts elements of the hwmon interface spec.)

For now I've gone with Mark Brown's suggestion of a datasheet_name for finding the channels on the device. Patch 2 hacks this name into the max1363 driver. I'll probably put a version matching on channel number and type in at a later date.

Here we just have a pull interface. Push is considerably harder to do and needs quite a lot more of the IIO infrastructure to be in place (triggers / buffers etc). Events obviously require IIO event handling to be there. Hence all of that will have to wait until those elements have in of themselves been posted for review.

It is pretty clear to me that hwmon interface for starters needs the ability to say - 'give me all mappings that correspond to me'.

This is what I intend to add next followed by some utility functions to make it easy to match the hwmon interface.

At that point I'll propose the hwmon driver goes into drivers/hwmon (subject to the underlying iio stuff merging).

Fun fun fun. Thanks to Linus an Mark for their input on this. Hope this is roughly what you guys were looking for.

Also on my list to do is to check this very thoroughly for any possible race conditions around the removal of the underlying device.

Jonathan

Jonathan Cameron (5):
IIO: core: add datasheet_name to chan_spec IIO:ADC:max1363 add datasheet_name entries.
IIO:CORE add in kernel interface mapping and getting IIO channels. IIO:hwmon interface client driver.
stargate2: example of map configuration for iio to hwmon example.

 arch/arm/mach-pxa/stargate2.c  |   23 ++++
 drivers/Makefile               |    2 +-
 drivers/iio/Kconfig            |    8 ++
 drivers/iio/Makefile           |    2 +
 drivers/iio/adc/max1363_core.c |    2 +
 drivers/iio/iio.c              |  264 +++++++++++++++++++++++++++++++++++++++-
 drivers/iio/iio_hwmon.c        |  227 ++++++++++++++++++++++++++++++++++
 drivers/iio/inkern.c           |   20 +++
 include/linux/iio/iio.h        |    7 +-
 include/linux/iio/inkern.h     |   95 ++++++++++++++
 10 files changed, 643 insertions(+), 7 deletions(-)
 create mode 100644 drivers/iio/iio_hwmon.c
 create mode 100644 drivers/iio/inkern.c
 create mode 100644 include/linux/iio/inkern.h
Signature · 1 line
1.7.7.2
author maintainer
#1 lore raw

PATCH v4 1/5 IIO: core: add datasheet_name to chan_spec

From: Jonathan Cameron <[email protected]>

This allows for matching against the name given on a datasheet, however silly/inconsistent it might be.

Useful for in kernel interfaces.

---
 include/linux/iio/iio.h |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)
include/linux/iio/iio.h +1 -0
index 4367d82..36a50b2 100644
@@ -122,6 +122,7 @@ struct iio_chan_spec {
 	} scan_type;
 	long                    info_mask;
 	char			*extend_name;
+	const char		*datasheet_name;
 	unsigned		processed_val:1;
 	unsigned		modified:1;
 	unsigned		indexed:1;
Signature · 1 line
1.7.7.2
author maintainer
#2 lore raw

PATCH v4 2/5 IIO:ADC:max1363 add datasheet_name entries.

In reply to Jonathan Cameron

From: Jonathan Cameron <[email protected]>

Kind of obvious for this device but useful
for testing purposes.

---
 drivers/iio/adc/max1363_core.c |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)
drivers/iio/adc/max1363_core.c +2 -0
index f21ec63..603969b 100644
@@ -252,6 +252,7 @@ static const enum max1363_modes max1363_mode_list[] = {
 		.channel = num,						\
 		.address = addr,					\
 		.info_mask = IIO_CHAN_INFO_SCALE_SHARED_BIT,		\
+		.datasheet_name = "AIN"#num				\
 	}								\
 
 /* bipolar channel */
@@ -264,6 +265,7 @@ static const enum max1363_modes max1363_mode_list[] = {
 		.channel2 = num2,					\
 		.address = addr,					\
 		.info_mask = IIO_CHAN_INFO_SCALE_SHARED_BIT,		\
+		.datasheet_name = "AIN"#num"-AIN"#num2			\
 	}
 
 
Signature · 1 line
1.7.7.2
author maintainer
#3 lore raw

PATCH v4 3/5 IIO:CORE add in kernel interface mapping and getting IIO channels.

In reply to Jonathan Cameron

From: Jonathan Cameron <[email protected]>

Two elements here:
* Map as defined in include/linux/iio/inkern.h
* Matching code to actually get the iio_dev and channel that we want from the global list of IIO devices.

---
 drivers/Makefile           |    2 +-
 drivers/iio/Makefile       |    1 +
 drivers/iio/iio.c          |  264 +++++++++++++++++++++++++++++++++++++++++++-
 drivers/iio/inkern.c       |   20 ++++
 include/linux/iio/iio.h    |    6 +-
 include/linux/iio/inkern.h |   95 ++++++++++++++++
 6 files changed, 381 insertions(+), 7 deletions(-)
drivers/Makefile +1 -1
index 216bba4..cf3d1f7 100644
@@ -134,4 +134,4 @@ obj-$(CONFIG_HYPERV)		+= hv/
 
 obj-$(CONFIG_PM_DEVFREQ)	+= devfreq/
 
-obj-$(CONFIG_IIO)		+= iio/
+obj-y				+= iio/
drivers/iio/Makefile +1 -0
index db3c426..cfb588a 100644
@@ -1,6 +1,7 @@
 #
 # Makefile for the Industrial I/O subsystem
 #
+obj-y = inkern.o
 
 obj-$(CONFIG_IIO) += iio.o
 industrialio-y := core.o
drivers/iio/iio.c +259 -5

284 lines not on this page · raw message

drivers/iio/inkern.c new file +20 -0
new file mode 100644
index 0000000..b7c2788
@@ -0,0 +1,20 @@
+/* The industrial I/O core in kernel channel mapping
+ *
+ * Copyright (c) 2011 Jonathan Cameron
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 as published by
+ * the Free Software Foundation.
+ */
+#include <linux/iio/inkern.h>
+#include <linux/err.h>
+
+LIST_HEAD(iio_map_list);
+EXPORT_SYMBOL_GPL(iio_map_list);
+void iio_map_array_register(struct iio_map *map, int nummaps)
+{
+	int i;
+	for (i = 0; i < nummaps; i++)
+		list_add(&map[i].l, &iio_map_list);
+}
+EXPORT_SYMBOL(iio_map_array_register);
include/linux/iio/iio.h +5 -1
index 36a50b2..875704d 100644
@@ -147,7 +147,10 @@ struct iio_dev;
  * @write_raw_get_fmt:	callback function to query the expected
  *			format/precision. If not set by the driver, write_raw
  *			returns IIO_VAL_INT_PLUS_MICRO.
- **/
+ * @get_hardware_id:	obtain device relating to hardware. Typically based on
+ *			the parent device (actual hardware).  Note that if
+ *			not specified then iio_dev.dev->parent is used.
+ */
 struct iio_info {
 	struct module			*driver_module;
 	const struct attribute_group	*attrs;
@@ -167,6 +170,7 @@ struct iio_info {
 	int (*write_raw_get_fmt)(struct iio_dev *indio_dev,
 			 struct iio_chan_spec const *chan,
 			 long mask);
+	struct device *(*get_hardware_id)(struct iio_dev *indio_dev);
 };
 
 /**
include/linux/iio/inkern.h new file +95 -0

95 lines not on this page · raw message

Signature · 1 line
1.7.7.2
Reviewed-by on v4 3/5
#4 lore raw

Re: [PATCH 3/5] IIO:CORE add in kernel interface mapping and getting IIO channels.

Quoted 6 lines · Jonathan Cameron

From: Jonathan Cameron <[email protected]>

Two elements here:
* Map as defined in include/linux/iio/inkern.h
* Matching code to actually get the iio_dev and channel that we want from the global list of IIO devices.

I've not read it thoroughly but this certainly this looks sensible from an external interface point of view, I'd expect that issues can be worked through incrementally.

Quoted 1 line

+extern struct list_head iio_map_list;

It's surprising that this is part of the external interface of IIO rather than something hidden within the implementation.

#5 lore raw

Re: [PATCH 3/5] IIO:CORE add in kernel interface mapping and getting IIO channels.

Quoted 8 lines · Mark Brown
Quoted 6 lines · Jonathan Cameron

From: Jonathan Cameron <[email protected]>

Two elements here:
* Map as defined in include/linux/iio/inkern.h
* Matching code to actually get the iio_dev and channel that we want from the global list of IIO devices.

Quoted 5 lines

I've not read it thoroughly but this certainly this looks sensible from an external interface point of view, I'd expect that issues can be worked through incrementally.

Thanks.

Quoted 5 lines
Quoted 2 lines

+extern struct list_head iio_map_list;

It's surprising that this is part of the external interface of IIO rather than something hidden within the implementation.

You are quite correct. I was being lazy and had that one element that didn't fit in any headers. I'lll add a trivial inkern-core.h to handle that one bit.

Acked-by on v4 3/5
#6 lore raw

Re: [PATCH 3/5] IIO:CORE add in kernel interface mapping and getting IIO channels.

In reply to Jonathan Cameron
Quoted 8 lines · Jonathan Cameron

From: Jonathan Cameron <[email protected]>

Two elements here:
* Map as defined in include/linux/iio/inkern.h
* Matching code to actually get the iio_dev and channel that we want from the global list of IIO devices.

Overall this is what we want:

Thanks,
Linus Walleij

author maintainer
#7 lore raw

PATCH v4 4/5 IIO:hwmon interface client driver.

In reply to Jonathan Cameron

From: Jonathan Cameron <[email protected]>

Should move to drivers/hwmon once people are happy with it.

Minimal support of simple in, curr and temp attributes so far.

---
 drivers/iio/Kconfig     |    8 ++
 drivers/iio/Makefile    |    1 +
 drivers/iio/iio_hwmon.c |  227 +++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 236 insertions(+), 0 deletions(-)
drivers/iio/Kconfig +8 -0
index 308bc97..c2f0970 100644
@@ -11,6 +11,14 @@ menuconfig IIO
 
 if IIO
 
+config IIO_HWMON
+       tristate "Hwmon driver that uses channels specified via iio maps"
+       depends on HWMON
+       help
+	  This is a platform driver that in combination with a suitable
+	  map allows IIO devices to provide  basic hwmon functionality
+	  for those channels specified in the map.
+
 source "drivers/iio/adc/Kconfig"
 source "drivers/iio/imu/Kconfig"
 source "drivers/iio/light/Kconfig"
drivers/iio/Makefile +1 -0
index cfb588a..5f9c01a 100644
@@ -6,6 +6,7 @@ obj-y = inkern.o
 obj-$(CONFIG_IIO) += iio.o
 industrialio-y := core.o
 
+obj-$(CONFIG_IIO_HWMON) += iio_hwmon.o
 obj-y += adc/
 obj-y += imu/
 obj-y += light/
drivers/iio/iio_hwmon.c new file +227 -0

227 lines not on this page · raw message

Signature · 1 line
1.7.7.2
#8 lore raw

Re: [PATCH 4/5] IIO:hwmon interface client driver.

Hi Jonathan,

Quoted 241 lines · Jonathan Cameron

From: Jonathan Cameron <[email protected]>

Should move to drivers/hwmon once people are happy with it.

Minimal support of simple in, curr and temp attributes so far.

---
 drivers/iio/Kconfig     |    8 ++
 drivers/iio/Makefile    |    1 +
 drivers/iio/iio_hwmon.c |  227 +++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 236 insertions(+), 0 deletions(-)
drivers/iio/Kconfig +8 -0
index 308bc97..c2f0970 100644
@@ -11,6 +11,14 @@ menuconfig IIO
 
 if IIO
 
+config IIO_HWMON
+       tristate "Hwmon driver that uses channels specified via iio maps"
+       depends on HWMON
+       help
+	  This is a platform driver that in combination with a suitable
+	  map allows IIO devices to provide  basic hwmon functionality
+	  for those channels specified in the map.
+
 source "drivers/iio/adc/Kconfig"
 source "drivers/iio/imu/Kconfig"
 source "drivers/iio/light/Kconfig"
drivers/iio/Makefile +1 -0
index cfb588a..5f9c01a 100644
@@ -6,6 +6,7 @@ obj-y = inkern.o
 obj-$(CONFIG_IIO) += iio.o
 industrialio-y := core.o
 
+obj-$(CONFIG_IIO_HWMON) += iio_hwmon.o
 obj-y += adc/
 obj-y += imu/
 obj-y += light/
drivers/iio/iio_hwmon.c new file +190 -0
new file mode 100644
index 0000000..4bd7651
@@ -0,0 +1,227 @@
+/* Hwmon client for industrial I/O devices
+ *
+ * Copyright (c) 2011 Jonathan Cameron
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 as published by
+ * the Free Software Foundation.
+ *
+ * Limited functionality currently supported.
+ */
+
+#include <linux/kernel.h>
+#include <linux/slab.h>
+#include <linux/module.h>
+#include <linux/err.h>
+#include <linux/platform_device.h>
+#include <linux/iio/inkern.h>
+#include <linux/hwmon.h>
+#include <linux/hwmon-sysfs.h>
+
+/**
+ * struct iio_hwmon_state - device instance state
+ * @channels:		filled with null terminated array of channels from iio
+ * @num_channels:	number of channels in channels (saves counting twice)
+ * @hwmon_dev:		associated hwmon device
+ * @attr_group:	the group of attributes
+ * @attrs:		null terminated array of attribute pointers.
+ */
+struct iio_hwmon_state {
+	struct iio_channel **channels;
+	int num_channels;
+	struct device *hwmon_dev;
+	struct attribute_group attr_group;
+	struct attribute **attrs;
+};
+
+/*
+ * Assumes that IIO and hwmon operate in the same base units.
+ * This is supposed to be true, but needs verification for
+ * new channel types.
+ */
+static ssize_t iio_hwmon_read_val(struct device *dev,
+				  struct device_attribute *attr,
+				  char *buf)
+{
+	long result;
+	int val, ret, scaleint, scalepart;
+	struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
+	struct iio_hwmon_state *state = dev_get_drvdata(dev);
+
+	/*
+	 * No locking between this pair, so theoretically possible
+	 * the scale has changed.
+	 */
+	ret = iio_read_channel_raw(state->channels[sattr->index],
+				   &val);
+	if (ret < 0)
+		return ret;
+
+	ret = iio_read_channel_scale(state->channels[sattr->index],
+				     &scaleint, &scalepart);
+	if (ret < 0)
+		return ret;
+	switch (ret) {
+	case IIO_VAL_INT:
+		result = val * scaleint;
+		break;
+	case IIO_VAL_INT_PLUS_MICRO:
+		result = (long)val * (long)scaleint +
+			div_s64((s64)val * (s64)scalepart, 1000000LL);
+		break;
+	case IIO_VAL_INT_PLUS_NANO:
+		result = (long)val * (long)scaleint +
+			div_s64((s64)val * (s64)scalepart, 1000000000LL);
+		break;
+	default:
+		return -EINVAL;
+	}
+	return sprintf(buf, "%ld\n", result);
+}
+
+static void iio_hwmon_free_attrs(struct iio_hwmon_state *st)
+{
+	int i;
+	struct sensor_device_attribute *a;
+	for (i = 0; i < st->num_channels; i++)
+		if (st->attrs[i]) {
+			a = to_sensor_dev_attr(
+				container_of(st->attrs[i],
+					     struct device_attribute,
+					     attr));
+			kfree(a);
+		}
+}
+
+static int __devinit iio_hwmon_probe(struct platform_device *pdev)
+{
+	struct iio_hwmon_state *st;
+	struct sensor_device_attribute *a;
+	int ret, i;
+	int in_i = 1, temp_i = 1, curr_i = 1;
+
+	st = kzalloc(sizeof(*st), GFP_KERNEL);
+	if (st == NULL) {
+		ret = -ENOMEM;
+		goto error_ret;
+	}
+
+	st->channels = iio_channel_get_all(&pdev->dev, NULL);
+	if (IS_ERR(st->channels)) {
+		ret = PTR_ERR(st->channels);
+		goto error_free_state;
+	}
+
+	/* count how many attributes we have */
+	while (st->channels[st->num_channels])
+		st->num_channels++;
+
+	st->attrs = kzalloc(sizeof(st->attrs) * (st->num_channels + 1),
+			    GFP_KERNEL);
+	if (st->attrs == NULL) {
+		ret = -ENOMEM;
+		goto error_release_channels;
+	}
+	for (i = 0; i < st->num_channels; i++) {
+		a = kzalloc(sizeof(*a), GFP_KERNEL);
+		if (a == NULL) {
+			ret = -ENOMEM;
+			goto error_free_attrs;
+		}
+
+		sysfs_attr_init(&a->dev_attr.attr);
+		switch (iio_get_channel_type(st->channels[i])) {
+		case IIO_VOLTAGE:
+			a->dev_attr.attr.name = kasprintf(GFP_KERNEL,
+							  "in%d_input",
+							  in_i++);
+			break;
+		case IIO_TEMP:
+			a->dev_attr.attr.name = kasprintf(GFP_KERNEL,
+							  "temp%d_input",
+							  temp_i++);
+			break;
+		case IIO_CURRENT:
+			a->dev_attr.attr.name = kasprintf(GFP_KERNEL,
+							  "curr%d_input",
+							  curr_i++);
+			break;
+		default:
+			ret = -EINVAL;
+			kfree(a);
+			goto error_free_attrs;
+		}
+		if (a->dev_attr.attr.name == NULL) {
+			kfree(a);
+			ret = -ENOMEM;
+			goto error_free_attrs;
+		}
+		a->dev_attr.show = iio_hwmon_read_val;
+		a->dev_attr.attr.mode = S_IRUGO;
+		a->index = i;
+		st->attrs[i] = &a->dev_attr.attr;
+	}
+
+	st->attr_group.attrs = st->attrs;
+	platform_set_drvdata(pdev, st);
+	ret = sysfs_create_group(&pdev->dev.kobj, &st->attr_group);
+	if (ret < 0)
+		goto error_free_attrs;
+
+	st->hwmon_dev = hwmon_device_register(&pdev->dev);
+	if (IS_ERR(st->hwmon_dev)) {
+		ret = PTR_ERR(st->hwmon_dev);
+		goto error_remove_group;
+	}
+	return 0;
+
+error_remove_group:
+	sysfs_remove_group(&pdev->dev.kobj, &st->attr_group);
+error_free_attrs:
+	iio_hwmon_free_attrs(st);
+	kfree(st->attrs);
+error_release_channels:
+	iio_channel_release_all(st->channels);
+error_free_state:
+	kfree(st);
+error_ret:
+	return ret;
+}
+

drivers/iio/iio_hwmon.c line 200

Quoted 10 lines
drivers/iio/iio_hwmon.c new file +10 -0
+static int __devexit iio_hwmon_remove(struct platform_device *pdev)
+{
+	struct iio_hwmon_state *st = platform_get_drvdata(pdev);
+
+	hwmon_device_unregister(st->hwmon_dev);
+	sysfs_remove_group(&pdev->dev.kobj, &st->attr_group);
+	iio_hwmon_free_attrs(st);
+	kfree(st->attrs);
+	iio_channel_release_all(st->channels);
+

Either I missed it before, or I am missing something now ... should there be kfree(st) here ?

Thanks,
Guenter

Quoted 27 lines
drivers/iio/iio_hwmon.c new file +27 -0
+	return 0;
+}
+
+static struct platform_driver __refdata iio_hwmon_driver = {
+	.driver = {
+		.name = "iio_hwmon",
+		.owner = THIS_MODULE,
+	},
+	.probe = iio_hwmon_probe,
+	.remove = __devexit_p(iio_hwmon_remove),
+};
+
+static int iio_inkern_init(void)
+{
+	return platform_driver_register(&iio_hwmon_driver);
+}
+module_init(iio_inkern_init);
+
+static void iio_inkern_exit(void)
+{
+	platform_driver_unregister(&iio_hwmon_driver);
+}
+module_exit(iio_inkern_exit);
+
+MODULE_AUTHOR("Jonathan Cameron <[email protected]>");
+MODULE_DESCRIPTION("IIO to hwmon driver");
+MODULE_LICENSE("GPL v2");
author maintainer
#9 lore raw

Re: [PATCH 4/5] IIO:hwmon interface client driver.

Quoted 14 lines

drivers/iio/iio_hwmon.c line 200

Quoted 10 lines
drivers/iio/iio_hwmon.c new file +10 -0
+static int __devexit iio_hwmon_remove(struct platform_device *pdev)
+{
+	struct iio_hwmon_state *st = platform_get_drvdata(pdev);
+
+	hwmon_device_unregister(st->hwmon_dev);
+	sysfs_remove_group(&pdev->dev.kobj, &st->attr_group);
+	iio_hwmon_free_attrs(st);
+	kfree(st->attrs);
+	iio_channel_release_all(st->channels);
+

Either I missed it before, or I am missing something now ... should there be kfree(st) here ?

Quoted 2 lines

Thanks,
Guenter

Gah! you are quite right. Good spot. I'll fix that one up and repost this individual patch.

author maintainer
#10 lore raw

PATCH 1/1 IIO:hwmon interface client driver.

From: Jonathan Cameron <[email protected]>

Should move to drivers/hwmon once people are happy with it.

Minimal support of simple in, curr and temp attributes so far.

---
Fixed up the missing free Guenter spotted.
Thanks!

 drivers/iio/Kconfig     |    8 ++
 drivers/iio/Makefile    |    1 +
 drivers/iio/iio_hwmon.c |  228 +++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 237 insertions(+), 0 deletions(-)
 create mode 100644 drivers/iio/iio_hwmon.c
drivers/iio/Kconfig +8 -0
index 308bc97..c2f0970 100644
@@ -11,6 +11,14 @@ menuconfig IIO
 
 if IIO
 
+config IIO_HWMON
+       tristate "Hwmon driver that uses channels specified via iio maps"
+       depends on HWMON
+       help
+	  This is a platform driver that in combination with a suitable
+	  map allows IIO devices to provide  basic hwmon functionality
+	  for those channels specified in the map.
+
 source "drivers/iio/adc/Kconfig"
 source "drivers/iio/imu/Kconfig"
 source "drivers/iio/light/Kconfig"
drivers/iio/Makefile +1 -0
index cfb588a..5f9c01a 100644
@@ -6,6 +6,7 @@ obj-y = inkern.o
 obj-$(CONFIG_IIO) += iio.o
 industrialio-y := core.o
 
+obj-$(CONFIG_IIO_HWMON) += iio_hwmon.o
 obj-y += adc/
 obj-y += imu/
 obj-y += light/
drivers/iio/iio_hwmon.c new file +228 -0

228 lines not on this page · raw message

Signature · 1 line
1.7.7.2
#11 lore raw

Re: [PATCH] IIO:hwmon interface client driver.

Hi Jonathan,

I know I must be getting annoying, but somehow it seems I just keep finding something whenever I look at the code again.

Quoted 135 lines · Jonathan Cameron

From: Jonathan Cameron <[email protected]>

Should move to drivers/hwmon once people are happy with it.

Minimal support of simple in, curr and temp attributes so far.

---
Fixed up the missing free Guenter spotted.
Thanks!

 drivers/iio/Kconfig     |    8 ++
 drivers/iio/Makefile    |    1 +
 drivers/iio/iio_hwmon.c |  228 +++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 237 insertions(+), 0 deletions(-)
 create mode 100644 drivers/iio/iio_hwmon.c
drivers/iio/Kconfig +8 -0
index 308bc97..c2f0970 100644
@@ -11,6 +11,14 @@ menuconfig IIO
 
 if IIO
 
+config IIO_HWMON
+       tristate "Hwmon driver that uses channels specified via iio maps"
+       depends on HWMON
+       help
+	  This is a platform driver that in combination with a suitable
+	  map allows IIO devices to provide  basic hwmon functionality
+	  for those channels specified in the map.
+
 source "drivers/iio/adc/Kconfig"
 source "drivers/iio/imu/Kconfig"
 source "drivers/iio/light/Kconfig"
drivers/iio/Makefile +1 -0
index cfb588a..5f9c01a 100644
@@ -6,6 +6,7 @@ obj-y = inkern.o
 obj-$(CONFIG_IIO) += iio.o
 industrialio-y := core.o
 
+obj-$(CONFIG_IIO_HWMON) += iio_hwmon.o
 obj-y += adc/
 obj-y += imu/
 obj-y += light/
drivers/iio/iio_hwmon.c new file +81 -0
new file mode 100644
index 0000000..880ce6d
@@ -0,0 +1,228 @@
+/* Hwmon client for industrial I/O devices
+ *
+ * Copyright (c) 2011 Jonathan Cameron
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 as published by
+ * the Free Software Foundation.
+ *
+ * Limited functionality currently supported.
+ */
+
+#include <linux/kernel.h>
+#include <linux/slab.h>
+#include <linux/module.h>
+#include <linux/err.h>
+#include <linux/platform_device.h>
+#include <linux/iio/inkern.h>
+#include <linux/hwmon.h>
+#include <linux/hwmon-sysfs.h>
+
+/**
+ * struct iio_hwmon_state - device instance state
+ * @channels:		filled with null terminated array of channels from iio
+ * @num_channels:	number of channels in channels (saves counting twice)
+ * @hwmon_dev:		associated hwmon device
+ * @attr_group:	the group of attributes
+ * @attrs:		null terminated array of attribute pointers.
+ */
+struct iio_hwmon_state {
+	struct iio_channel **channels;
+	int num_channels;
+	struct device *hwmon_dev;
+	struct attribute_group attr_group;
+	struct attribute **attrs;
+};
+
+/*
+ * Assumes that IIO and hwmon operate in the same base units.
+ * This is supposed to be true, but needs verification for
+ * new channel types.
+ */
+static ssize_t iio_hwmon_read_val(struct device *dev,
+				  struct device_attribute *attr,
+				  char *buf)
+{
+	long result;
+	int val, ret, scaleint, scalepart;
+	struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
+	struct iio_hwmon_state *state = dev_get_drvdata(dev);
+
+	/*
+	 * No locking between this pair, so theoretically possible
+	 * the scale has changed.
+	 */
+	ret = iio_read_channel_raw(state->channels[sattr->index],
+				   &val);
+	if (ret < 0)
+		return ret;
+
+	ret = iio_read_channel_scale(state->channels[sattr->index],
+				     &scaleint, &scalepart);
+	if (ret < 0)
+		return ret;
+	switch (ret) {
+	case IIO_VAL_INT:
+		result = val * scaleint;
+		break;
+	case IIO_VAL_INT_PLUS_MICRO:
+		result = (long)val * (long)scaleint +
+			div_s64((s64)val * (s64)scalepart, 1000000LL);
+		break;
+	case IIO_VAL_INT_PLUS_NANO:
+		result = (long)val * (long)scaleint +
+			div_s64((s64)val * (s64)scalepart, 1000000000LL);
+		break;
+	default:
+		return -EINVAL;
+	}
+	return sprintf(buf, "%ld\n", result);
+}
+

drivers/iio/iio_hwmon.c line 91

Quoted 10 lines
drivers/iio/iio_hwmon.c new file +10 -0
+static void iio_hwmon_free_attrs(struct iio_hwmon_state *st)
+{
+	int i;
+	struct sensor_device_attribute *a;
+	for (i = 0; i < st->num_channels; i++)
+		if (st->attrs[i]) {
+			a = to_sensor_dev_attr(
+				container_of(st->attrs[i],
+					     struct device_attribute,
+					     attr));

I finally looked into kasprintf ... which as it turns out allocates memory. Yet, there is no kfree(a->dev_attr.attr.name).

Thanks,
Guenter

author maintainer
#12 lore raw

Re: [PATCH] IIO:hwmon interface client driver.

Quoted 4 lines · Guenter Roeck

Hi Jonathan,

I know I must be getting annoying, but somehow it seems I just keep finding something whenever I look at the code again.

Not at all. It's becoming clear I wrote some extremely slapdash code. Sorry I have been wasting your time with this!

Quoted 156 lines · Jonathan Cameron
Quoted 135 lines

From: Jonathan Cameron <[email protected]>

Should move to drivers/hwmon once people are happy with it.

Minimal support of simple in, curr and temp attributes so far.

---
Fixed up the missing free Guenter spotted.
Thanks!

 drivers/iio/Kconfig     |    8 ++
 drivers/iio/Makefile    |    1 +
 drivers/iio/iio_hwmon.c |  228 +++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 237 insertions(+), 0 deletions(-)
 create mode 100644 drivers/iio/iio_hwmon.c
drivers/iio/Kconfig +8 -0
index 308bc97..c2f0970 100644
@@ -11,6 +11,14 @@ menuconfig IIO
 
 if IIO
 
+config IIO_HWMON
+       tristate "Hwmon driver that uses channels specified via iio maps"
+       depends on HWMON
+       help
+	  This is a platform driver that in combination with a suitable
+	  map allows IIO devices to provide  basic hwmon functionality
+	  for those channels specified in the map.
+
 source "drivers/iio/adc/Kconfig"
 source "drivers/iio/imu/Kconfig"
 source "drivers/iio/light/Kconfig"
drivers/iio/Makefile +1 -0
index cfb588a..5f9c01a 100644
@@ -6,6 +6,7 @@ obj-y = inkern.o
 obj-$(CONFIG_IIO) += iio.o
 industrialio-y := core.o
 
+obj-$(CONFIG_IIO_HWMON) += iio_hwmon.o
 obj-y += adc/
 obj-y += imu/
 obj-y += light/
drivers/iio/iio_hwmon.c new file +81 -0
new file mode 100644
index 0000000..880ce6d
@@ -0,0 +1,228 @@
+/* Hwmon client for industrial I/O devices
+ *
+ * Copyright (c) 2011 Jonathan Cameron
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 as published by
+ * the Free Software Foundation.
+ *
+ * Limited functionality currently supported.
+ */
+
+#include <linux/kernel.h>
+#include <linux/slab.h>
+#include <linux/module.h>
+#include <linux/err.h>
+#include <linux/platform_device.h>
+#include <linux/iio/inkern.h>
+#include <linux/hwmon.h>
+#include <linux/hwmon-sysfs.h>
+
+/**
+ * struct iio_hwmon_state - device instance state
+ * @channels:		filled with null terminated array of channels from iio
+ * @num_channels:	number of channels in channels (saves counting twice)
+ * @hwmon_dev:		associated hwmon device
+ * @attr_group:	the group of attributes
+ * @attrs:		null terminated array of attribute pointers.
+ */
+struct iio_hwmon_state {
+	struct iio_channel **channels;
+	int num_channels;
+	struct device *hwmon_dev;
+	struct attribute_group attr_group;
+	struct attribute **attrs;
+};
+
+/*
+ * Assumes that IIO and hwmon operate in the same base units.
+ * This is supposed to be true, but needs verification for
+ * new channel types.
+ */
+static ssize_t iio_hwmon_read_val(struct device *dev,
+				  struct device_attribute *attr,
+				  char *buf)
+{
+	long result;
+	int val, ret, scaleint, scalepart;
+	struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
+	struct iio_hwmon_state *state = dev_get_drvdata(dev);
+
+	/*
+	 * No locking between this pair, so theoretically possible
+	 * the scale has changed.
+	 */
+	ret = iio_read_channel_raw(state->channels[sattr->index],
+				   &val);
+	if (ret < 0)
+		return ret;
+
+	ret = iio_read_channel_scale(state->channels[sattr->index],
+				     &scaleint, &scalepart);
+	if (ret < 0)
+		return ret;
+	switch (ret) {
+	case IIO_VAL_INT:
+		result = val * scaleint;
+		break;
+	case IIO_VAL_INT_PLUS_MICRO:
+		result = (long)val * (long)scaleint +
+			div_s64((s64)val * (s64)scalepart, 1000000LL);
+		break;
+	case IIO_VAL_INT_PLUS_NANO:
+		result = (long)val * (long)scaleint +
+			div_s64((s64)val * (s64)scalepart, 1000000000LL);
+		break;
+	default:
+		return -EINVAL;
+	}
+	return sprintf(buf, "%ld\n", result);
+}
+

drivers/iio/iio_hwmon.c line 91

Quoted 10 lines
drivers/iio/iio_hwmon.c new file +10 -0
+static void iio_hwmon_free_attrs(struct iio_hwmon_state *st)
+{
+	int i;
+	struct sensor_device_attribute *a;
+	for (i = 0; i < st->num_channels; i++)
+		if (st->attrs[i]) {
+			a = to_sensor_dev_attr(
+				container_of(st->attrs[i],
+					     struct device_attribute,
+					     attr));

I finally looked into kasprintf ... which as it turns out allocates memory. Yet, there is no kfree(a->dev_attr.attr.name).

Thanks,
Guenter

author maintainer
#13 lore raw

PATCH v4 5/5 stargate2: example of map configuration for iio to hwmon example.

In reply to Jonathan Cameron

From: Jonathan Cameron <[email protected]>

Do not commit.

---
 arch/arm/mach-pxa/stargate2.c |   23 +++++++++++++++++++++++
 1 files changed, 23 insertions(+), 0 deletions(-)
arch/arm/mach-pxa/stargate2.c +23 -0
index 4c9a48b..25a69c7 100644
@@ -54,6 +54,8 @@
 #include <linux/mfd/da903x.h>
 #include <linux/sht15.h>
 
+#include <linux/iio/inkern.h>
+
 #include "devices.h"
 #include "generic.h"
 
@@ -406,6 +408,24 @@ static struct i2c_pxa_platform_data i2c_pdata = {
 	.fast_mode = 1,
 };
 
+static struct platform_device iio_hwmon_test = {
+	.name = "iio_hwmon",
+};
+
+static struct iio_map adc_map[] = {
+	{
+		.adc_dev_name = "0-0035",
+		.adc_channel_label = "AIN1",
+		.consumer_dev = &iio_hwmon_test.dev,
+		.consumer_channel = "testchan1",
+	}, {
+		.adc_dev_name = "0-0035",
+		.adc_channel_label = "AIN2",
+		.consumer_dev = &iio_hwmon_test.dev,
+		.consumer_channel = "testchan2",
+	},
+};
+
 static void __init imote2_stargate2_init(void)
 {
 
@@ -423,6 +443,8 @@ static void __init imote2_stargate2_init(void)
 
 	pxa27x_set_i2c_power_info(&i2c_pwr_pdata);
 	pxa_set_i2c_info(&i2c_pdata);
+
+	iio_map_array_register(adc_map, ARRAY_SIZE(adc_map));
 }
 
 #ifdef CONFIG_MACH_INTELMOTE2
@@ -971,6 +993,7 @@ static struct platform_device *stargate2_devices[] = {
 	&stargate2_sram,
 	&smc91x_device,
 	&sht15,
+	&iio_hwmon_test,
 };
 
 static void __init stargate2_init(void)
Signature · 1 line
1.7.7.2