Skip to contentSkip to conversation

iio: ak8975: Add Ak8975 magnetometer sensor

PATCH In review [email protected] 4 participants 12 messages Sep 2, 2010 – Sep 4, 2010

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 iio: ak8975: Add Ak8975 magnetometer sensor

From: Andrew Chew <[email protected]>

This is for the Asahi Kasei AK8975 3-axis magnetometer. It resides within the magnetometer section of the IIO subsystem, and implements the raw magn_x,y,z attributes, as well as x,y,z factory calibration attributes.

---
 drivers/staging/iio/magnetometer/Kconfig  |   11 +
 drivers/staging/iio/magnetometer/Makefile |    1 +
 drivers/staging/iio/magnetometer/ak8975.c |  517 +++++++++++++++++++++++++++++
 3 files changed, 529 insertions(+), 0 deletions(-)
 create mode 100644 drivers/staging/iio/magnetometer/ak8975.c
drivers/staging/iio/magnetometer/Kconfig +11 -0
index d014450..00e1204 100644
@@ -3,6 +3,17 @@
 #
 comment "Magnetometer sensors"
 
+config SENSORS_AK8975
+	tristate "Asahi Kasei AK8975 3-Axis Magnetometer"
+	default n
+	depends on I2C
+	help
+	  Say yes here to build support for Asahi Kasei AK8975 3-Axis
+	  Magnetometer.
+
+	  To compile this driver as a module, choose M here: the module
+	  will be called ak8975.
+
 config SENSORS_HMC5843
 	tristate "Honeywell HMC5843 3-Axis Magnetometer"
 	depends on I2C
drivers/staging/iio/magnetometer/Makefile +1 -0
index f9bfb2e..e3dbaa4 100644
@@ -2,4 +2,5 @@
 # Makefile for industrial I/O Magnetometer sensors
 #
 
+obj-$(CONFIG_SENSORS_AK8975)	:= ak8975.o
 obj-$(CONFIG_SENSORS_HMC5843)	+= hmc5843.o
drivers/staging/iio/magnetometer/ak8975.c new file +517 -0

517 lines not on this page · raw message

Signature · 1 line
1.7.0.4
#1 lore raw
Quoted 3 lines

+/*
+ * Shows the device's mode. 0 = off, 1 = on.
+ */

Should this not be handled by runtime pm nowdays ?

drivers/staging/iio/magnetometer/ak8975.c line 245

Quoted 4 lines
drivers/staging/iio/magnetometer/ak8975.c new file +4 -0
+	if ((oval < 0) || (oval > 1)) {
+		dev_err(dev, "mode value is not supported\n");
+		return -EINVAL;
+	}

ulong cannot be < 0 and doesn't need all the brackets

drivers/staging/iio/magnetometer/ak8975.c line 333

Quoted 8 lines
drivers/staging/iio/magnetometer/ak8975.c new file +8 -0
+	/* Wait for the conversion to complete. */
+	while (timeout_ms) {
+		msleep(AK8975_CONVERSION_DONE_POLL_TIME);
+		state = (gpio_get_value(data->eoc_gpio) ? 1 : 0);
+		if (state)
+			break;
+		timeout_ms -= AK8975_CONVERSION_DONE_POLL_TIME;
+	}

This makes some fairly specific wiring assumptions about how the ak8975 is configured. I'm looking at the ak8974 driver in our tree and also wondering if they can be combined sanely.

drivers/staging/iio/magnetometer/ak8975.c line 342

Quoted 4 lines
drivers/staging/iio/magnetometer/ak8975.c new file +4 -0
+	status = ak8975_read_data(client, AK8975_REG_ST1, 1, &read_status);
+	if (!status) {
+		dev_err(&client->dev, "Error in reading ST1\n");
+		return false;

I would have expected these to return a meaningful error code not 0 ?

drivers/staging/iio/magnetometer/ak8975.c line 381

Quoted 7 lines
drivers/staging/iio/magnetometer/ak8975.c new file +7 -0
+static IIO_DEVICE_ATTR(mode, S_IRUGO | S_IWUSR, show_mode, store_mode, 0);
+static IIO_DEVICE_ATTR(magn_x_calibscale, S_IRUGO, show_calibscale, NULL, 0);
+static IIO_DEVICE_ATTR(magn_y_calibscale, S_IRUGO, show_calibscale, NULL, 1);
+static IIO_DEVICE_ATTR(magn_z_calibscale, S_IRUGO, show_calibscale, NULL, 2);
+static IIO_DEV_ATTR_MAGN_X(show_raw, AK8975_REG_HXL);
+static IIO_DEV_ATTR_MAGN_Y(show_raw, AK8975_REG_HYL);
+static IIO_DEV_ATTR_MAGN_Z(show_raw, AK8975_REG_HZL);

This seems odd as an interface as it's raw when the maths to provide non-raw (and thus abstract and easy for user space) data is trivial enough to do in kernel

(but then I still suspect it should jusst be an input device of course)

Quoted 12 lines
drivers/staging/iio/magnetometer/ak8975.c new file +12 -0
+static int ak8975_probe(struct i2c_client *client,
+			const struct i2c_device_id *id)
+{
+	struct ak8975_data *data;
+	int err;
+
+	/* Allocate our device context. */
+	data = kzalloc(sizeof(struct ak8975_data), GFP_KERNEL);
+	if (!data) {
+		dev_err(&client->dev, "Memory allocation fails\n");
+		err = -ENOMEM;
+		goto exit;

drivers/staging/iio/magnetometer/ak8975.c line 419

Quoted 10 lines
drivers/staging/iio/magnetometer/ak8975.c new file +10 -0
+	}
+
+	i2c_set_clientdata(client, data);
+	data->client = client;
+
+	mutex_init(&data->lock);
+
+	/* Grab and set up the supplied GPIO. */
+	data->eoc_irq = client->irq;
+	data->eoc_gpio = irq_to_gpio(client->irq);

It may not be via a GPIO. Better to do the GPIO handling in platform abstraction or accept passing IRQ and no GPIO value to mean "just use the IRQ". Ie do all the gpio foo if (data->eoc_gpio) { ... }

drivers/staging/iio/magnetometer/ak8975.c line 432

Quoted 13 lines
drivers/staging/iio/magnetometer/ak8975.c new file +13 -0
+
+	err = gpio_request(data->eoc_gpio, "ak_8975");
+	if (err < 0) {
+		dev_err(&client->dev, "failed to request GPIO %d, error %d\n",
+			data->eoc_gpio, err);
+		goto exit_free;
+	}
+
+	err = gpio_direction_input(data->eoc_gpio);
+	if (err < 0) {
+		dev_err(&client->dev, "Failed to configure input direction for"
+			" GPIO %d, error %d\n", data->eoc_gpio, err);
+		gpio_free(data->eoc_gpio);

This frees the GPIO twice ?

Looks basically sound to me.

Alan

#2 lore raw
Quoted 7 lines
Quoted 1 line

+static IIO_DEVICE_ATTR(mode, S_IRUGO | S_IWUSR, show_mode,

store_mode, 0);

Quoted 1 line

+static IIO_DEVICE_ATTR(magn_x_calibscale, S_IRUGO,

show_calibscale, NULL, 0);

Quoted 1 line

+static IIO_DEVICE_ATTR(magn_y_calibscale, S_IRUGO,

show_calibscale, NULL, 1);

Quoted 1 line

+static IIO_DEVICE_ATTR(magn_z_calibscale, S_IRUGO,

Quoted 8 lines

show_calibscale, NULL, 2);

drivers/staging/iio/magnetometer/ak8975.c line 381

Quoted 3 lines
drivers/staging/iio/magnetometer/ak8975.c new file +3 -0
+static IIO_DEV_ATTR_MAGN_X(show_raw, AK8975_REG_HXL);
+static IIO_DEV_ATTR_MAGN_Y(show_raw, AK8975_REG_HYL);
+static IIO_DEV_ATTR_MAGN_Z(show_raw, AK8975_REG_HZL);

This seems odd as an interface as it's raw when the maths to provide non-raw (and thus abstract and easy for user space) data is trivial enough to do in kernel

IIO guys want to do normalization maths above the kernel-level magnetometer IIO layer. This interface came before me, so I'm just following current conventions.-- To unsubscribe from this list: send the line "unsubscribe linux-i2c" in
the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#3 lore raw
Quoted 16 lines · On Thu, 2 Sep 2010 15:16:20 -0700 Andrew Chew <[email protected]> wrote:
Quoted 7 lines
Quoted 1 line

+static IIO_DEVICE_ATTR(mode, S_IRUGO | S_IWUSR, show_mode,

store_mode, 0);

Quoted 1 line

+static IIO_DEVICE_ATTR(magn_x_calibscale, S_IRUGO,

show_calibscale, NULL, 0);

Quoted 1 line

+static IIO_DEVICE_ATTR(magn_y_calibscale, S_IRUGO,

show_calibscale, NULL, 1);

Quoted 1 line

+static IIO_DEVICE_ATTR(magn_z_calibscale, S_IRUGO,

Quoted 8 lines

show_calibscale, NULL, 2);

drivers/staging/iio/magnetometer/ak8975.c line 381

Quoted 3 lines
drivers/staging/iio/magnetometer/ak8975.c new file +3 -0
+static IIO_DEV_ATTR_MAGN_X(show_raw, AK8975_REG_HXL);
+static IIO_DEV_ATTR_MAGN_Y(show_raw, AK8975_REG_HYL);
+static IIO_DEV_ATTR_MAGN_Z(show_raw, AK8975_REG_HZL);

This seems odd as an interface as it's raw when the maths to provide non-raw (and thus abstract and easy for user space) data is trivial enough to do in kernel

Quoted 1 line

IIO guys want to do normalization maths above the kernel-level magnetometer IIO layer. This interface came before me, so I'm just following current conventions.

That will make a generic IIO <-> input bridge very hard to do right. I can see why IIO wants to do that but you need both if so and this also needs discussion.

#4 lore raw

Before I dive blindly in please note I have only taken a quick look as away from a computer with a decent size screen until tomorrow evening. I'll take a closer look at the driver then.

Quoted 19 lines · Alan Cox
Quoted 16 lines · On Thu, 2 Sep 2010 15:16:20 -0700 Andrew Chew <[email protected]> wrote:
Quoted 15 lines
Quoted 1 line

+static IIO_DEVICE_ATTR(mode, S_IRUGO | S_IWUSR, show_mode,

store_mode, 0);

Quoted 1 line

+static IIO_DEVICE_ATTR(magn_x_calibscale, S_IRUGO,

show_calibscale, NULL, 0);

Quoted 1 line

+static IIO_DEVICE_ATTR(magn_y_calibscale, S_IRUGO,

show_calibscale, NULL, 1);

Quoted 1 line

+static IIO_DEVICE_ATTR(magn_z_calibscale, S_IRUGO,

show_calibscale, NULL, 2);

drivers/staging/iio/magnetometer/ak8975.c line 381

Quoted 3 lines
drivers/staging/iio/magnetometer/ak8975.c new file +3 -0
+static IIO_DEV_ATTR_MAGN_X(show_raw, AK8975_REG_HXL);
+static IIO_DEV_ATTR_MAGN_Y(show_raw, AK8975_REG_HYL);
+static IIO_DEV_ATTR_MAGN_Z(show_raw, AK8975_REG_HZL);

This seems odd as an interface as it's raw when the maths to provide non-raw (and thus abstract and easy for user space) data is trivial enough to do in kernel

Quoted 6 lines
Quoted 2 lines

IIO guys want to do normalization maths above the kernel-level magnetometer IIO layer. This interface came before me, so I'm just following current conventions.

That will make a generic IIO <-> input bridge very hard to do right. I can see why IIO wants to do that but you need both if so and this also needs discussion.

Note that, though there are few drivers using it (just the tsl2563 iirc) we do allow the hwmon style _input syntax for values in the standard units for the device type. If you are going to use the raw option then, assuming linear conversion, at the very least magn_scale should be provided to tell userspace how to do the conversion. If the bridge to input is done in userspace then it is trivial to apply this before passing into uinput. For background info, the bridge approach currently relies on implementation of the 'buffered' iio interface which uses chrdevs rather than the hwmon like 'simple' interface provided here. It would be a bit of a nightmare to bridge from sysfs (if it could even be done?) Typically all drivers start with the sysfs interface and the buffered one is only added if needed by someone.

Note calibscale is meant for internally applied values. That is ones applied actually on the device (looking at what you have here, my choice of naming wasn't ideal, anyone have a better naming suggestion). The value used to do raw to standard unit conversion in userspace is magn_x_scale or equivalent.
I think based on the comment in your code that you also need to provide magn_x_offset. Looks like our documentation could do to be a little clearer. Rather tediously these are both computed floating point values here. Perhaps we will need to think further on that interface (this is the first time we have hit this particular situation)

The raw option is principally there because we share the attributes with the buffer access (there performance dictates doing as little as possible to the data on the way through.) Admittedly our overheads are way too high at the moment for other reasons but the intent is there. If you are intending to add this interface later then keeping to the _raw attributes makes for a more consistent choice (though as long as the scale and bias are there for the buffer you could use the processed value...)

Thanks,

Jonathan

#5 lore raw
Quoted 67 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 1 line

+static IIO_DEVICE_ATTR(mode, S_IRUGO | S_IWUSR, show_mode,=20

store_mode, 0);

Quoted 1 line

+static IIO_DEVICE_ATTR(magn_x_calibscale, S_IRUGO,=20

show_calibscale, NULL, 0);

Quoted 1 line

+static IIO_DEVICE_ATTR(magn_y_calibscale, S_IRUGO,=20

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

show_calibscale, NULL, 1);

Quoted 1 line

+static IIO_DEVICE_ATTR(magn_z_calibscale, S_IRUGO,=20

show_calibscale, NULL, 2);

drivers/staging/iio/magnetometer/ak8975.c line 381

Quoted 3 lines
drivers/staging/iio/magnetometer/ak8975.c new file +3 -0
+static IIO_DEV_ATTR_MAGN_X(show_raw, AK8975_REG_HXL);
+static IIO_DEV_ATTR_MAGN_Y(show_raw, AK8975_REG_HYL);
+static IIO_DEV_ATTR_MAGN_Z(show_raw, AK8975_REG_HZL);

This seems odd as an interface as it's raw when the maths=20

to provide

Quoted 1 line
Quoted 1 line
Quoted 1 line

non-raw (and thus abstract and easy for user space) data=20

is trivial

Quoted 3 lines
Quoted 3 lines
Quoted 1 line

enough to do in kernel

IIO guys want to do normalization maths above the=20

kernel-level magnetometer IIO layer.=20

Quoted 1 line
Quoted 1 line

This interface came before me, so I'm just following=20

current conventions.

Quoted 2 lines

=20
That will make a generic IIO <-> input bridge very hard to=20

do right. I

Quoted 1 line

can see why IIO wants to do that but you need both if so=20

and this also

Quoted 1 line

needs discussion.

=20
Note that, though there are few drivers using it (just the=20 tsl2563 iirc)
we do allow the hwmon style _input syntax for values in the=20 standard units
for the device type. If you are going to use the raw option then, assuming linear conversion, at the very least magn_scale=20 should be provided
to tell userspace how to do the conversion. If the bridge to input is done in userspace then it is trivial to apply this before passing into uinput. For background info, the bridge approach currently relies on implementation of the 'buffered' iio interface which uses chrdevs rather than the hwmon like 'simple' interface provided here. It would be a bit of a nightmare to bridge from sysfs (if it could even be done?) Typically all drivers start with the sysfs interface and the buffered one is only added if needed by someone. =20
Note calibscale is meant for internally applied values. That=20 is ones applied
actually on the device (looking at what you have here, my=20 choice of naming
wasn't ideal, anyone have a better naming suggestion). The=20 value used to do raw to
standard unit conversion in userspace is magn_x_scale or equivalent. I think based on the comment in your code that you also need=20 to provide
magn_x_offset. Looks like our documentation could do to be a=20 little clearer.
Rather tediously these are both computed floating point=20 values here. Perhaps
we will need to think further on that interface (this is the=20 first time
we have hit this particular situation)
=20
The raw option is principally there because we share the=20 attributes with the
buffer access (there performance dictates doing as little as=20 possible to the
data on the way through.) Admittedly our overheads are way=20

Quoted 8 lines

too high at the
moment for other reasons but the intent is there. If you are=20 intending to
add this interface later then keeping to the _raw attributes=20 makes for a
more consistent choice (though as long as the scale and bias=20 are there for
the buffer you could use the processed value...)

Yes, the little documentation that's there in sysfs-class-iio doesn't giv= e me very much guidance on how to do this properly.

So what I'm getting from your comments is that I'm using the calib attrib= ute wrong (I wasn't even sure I named this correctly...can we publish ALL= =20known attributes in magnet.h, so we can standardize on these attribute= =20names through the magnet.h macros?). The ASA sensitivity adjustment, = then, should be left completely internal to the driver, and not emitted t= hrough any known attribute?

And if we were to emit non-raw values (or provide the scale attributes), = what's the exact name of the attributes? And what units are we expecting= ? Gauss? Tesla? I don't see that documented anywhere.
-------------------------------------------------------------------------=
----------
This email message is for the sole use of the intended recipient(s) and m= ay contain
confidential information. Any unauthorized review, use, disclosure or di= stribution
is prohibited. If you are not the intended recipient, please contact the= =20sender by
reply email and destroy all copies of the original message.
-------------------------------------------------------------------------=
----------

#6 lore raw
Quoted 76 lines · Andrew Chew
Quoted 62 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 1 line

+static IIO_DEVICE_ATTR(mode, S_IRUGO | S_IWUSR, show_mode,

store_mode, 0);

Quoted 1 line

+static IIO_DEVICE_ATTR(magn_x_calibscale, S_IRUGO,

show_calibscale, NULL, 0);

Quoted 1 line

+static IIO_DEVICE_ATTR(magn_y_calibscale, S_IRUGO,

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

show_calibscale, NULL, 1);

Quoted 1 line

+static IIO_DEVICE_ATTR(magn_z_calibscale, S_IRUGO,

show_calibscale, NULL, 2);

drivers/staging/iio/magnetometer/ak8975.c line 381

Quoted 3 lines
drivers/staging/iio/magnetometer/ak8975.c new file +3 -0
+static IIO_DEV_ATTR_MAGN_X(show_raw, AK8975_REG_HXL);
+static IIO_DEV_ATTR_MAGN_Y(show_raw, AK8975_REG_HYL);
+static IIO_DEV_ATTR_MAGN_Z(show_raw, AK8975_REG_HZL);

This seems odd as an interface as it's raw when the maths

to provide

Quoted 1 line
Quoted 1 line
Quoted 1 line

non-raw (and thus abstract and easy for user space) data

is trivial

Quoted 3 lines
Quoted 3 lines
Quoted 1 line

enough to do in kernel

IIO guys want to do normalization maths above the

kernel-level magnetometer IIO layer.

Quoted 1 line
Quoted 1 line

This interface came before me, so I'm just following

current conventions.

Quoted 2 lines

That will make a generic IIO <-> input bridge very hard to

do right. I

Quoted 1 line

can see why IIO wants to do that but you need both if so

and this also

Quoted 1 line

needs discussion.

Note that, though there are few drivers using it (just the tsl2563 iirc)
we do allow the hwmon style _input syntax for values in the standard units
for the device type. If you are going to use the raw option then, assuming linear conversion, at the very least magn_scale should be provided
to tell userspace how to do the conversion. If the bridge to input is done in userspace then it is trivial to apply this before passing into uinput. For background info, the bridge approach currently relies on implementation of the 'buffered' iio interface which uses chrdevs rather than the hwmon like 'simple' interface provided here. It would be a bit of a nightmare to bridge from sysfs (if it could even be done?) Typically all drivers start with the sysfs interface and the buffered one is only added if needed by someone.

Note calibscale is meant for internally applied values. That is ones applied
actually on the device (looking at what you have here, my choice of naming
wasn't ideal, anyone have a better naming suggestion). The value used to do raw to
standard unit conversion in userspace is magn_x_scale or equivalent. I think based on the comment in your code that you also need to provide
magn_x_offset. Looks like our documentation could do to be a little clearer.
Rather tediously these are both computed floating point values here. Perhaps
we will need to think further on that interface (this is the first time
we have hit this particular situation)

Quoted 13 lines

The raw option is principally there because we share the attributes with the
buffer access (there performance dictates doing as little as possible to the
data on the way through.) Admittedly our overheads are way too high at the
moment for other reasons but the intent is there. If you are intending to
add this interface later then keeping to the _raw attributes makes for a
more consistent choice (though as long as the scale and bias are there for
the buffer you could use the processed value...)

Quoted 2 lines

Yes, the little documentation that's there in sysfs-class-iio doesn't give me very much guidance on how to do this properly.

That document relies on people generalizing the attributes across the various sensor types. The documents are iirc pretty complete for in0_raw and similar. Perhaps it is necessary to lay it out in full.

Quoted 4 lines

So what I'm getting from your comments is that I'm using the calib attribute wrong (I wasn't even sure I named this correctly...can we publish ALL known attributes in magnet.h,

Definitely. The usual principal is that anything obviously general or that has turned up in a number of drivers should go in the relevant header. We add these as and when they are first used.

Quoted 4 lines

so we can standardize on
these attribute names through the magnet.h macros?). The ASA sensitivity adjustment, then, should be left completely internal to the driver, and not emitted through any known attribute?

if I understand your comment correctly userspace needs to know that number to convert from the magn_raw values to standard units. Hence it must be exported. My point is that the calib attributes are used for devices that do their calibration offsets and scales internally. If it is a value userspace needs to apply then the correct parameters are scale and offset. In the majority of devices see so far these are fixed for all instances of the same part. That isn't true here.

Quoted 3 lines

And if we were to emit non-raw values (or provide the scale
attributes), what's theexact name of the attributes? And what units are we expecting? Gauss? Tesla? I don't see that documented anywhere.

That is documented. Gauss is the chosen standard for magnetometers. I'll assume the conversion factors are channel dependent in which case you need

magn_x_raw, magn_x_scale, magn_x_offset

note again, we have documented everything for some device types. At the time, repeating for all of them was considered redundant, but perhaps we do need to do this for the added clarity.

The value in Gauss = (magn_x_raw + magn_x_offset)*magn_x_scale

(in your case the formula is nicer with magn_x_scale provided first, but we had to pick one option and the above is what we went with.

Jonathan

Quoted 7 lines

-----------------------------------------------------------------------------------
This email message is for the sole use of the intended recipient(s) and may contain confidential information. Any unauthorized review, use, disclosure or distribution is prohibited. If you are not the intended recipient, please contact the sender by reply email and destroy all copies of the original message.
-----------------------------------------------------------------------------------

#7 lore raw
Quoted 11 lines

if I understand your comment correctly userspace needs to know that number to convert from the magn_raw values to standard units. Hence it must be exported. My point is that the calib attributes are used for devices that do their calibration offsets and scales internally. If it is a value userspace needs to apply then the correct parameters are scale and offset. In the majority of devices see so far these are fixed for all instances of the same part. That isn't true here.

Quoted 3 lines

=20
And if we were to emit non-raw values (or provide the scale attributes), what's theexact name of the attributes? And=20

Quoted 13 lines

what units are we expecting? Gauss? Tesla? I don't see=20 that documented anywhere.
That is documented. Gauss is the chosen standard for=20 magnetometers. I'll assume the
conversion factors are channel dependent in which case you need =20
magn_x_raw, magn_x_scale, magn_x_offset
=20
note again, we have documented everything for some device types. At the time, repeating for all of them was considered redundant, but perhaps we do need to do this for the added clarity.
=20
The value in Gauss =3D (magn_x_raw + magn_x_offset)*magn_x_scale

The conversion formula doesn't have an offset, actually, so the order in = which the offset and scale are applied don't really matter to me.

In any case, I think I got it now. I'm going to report the raw sensor va= lue, and the scale factor for each axis to convert to gauss.

I assume when you say magn_x_scale, you really mean magn_x_gain? Because= =20I don't see magn_x_scale defined in magnet.h. Or should I call the at= tribute magn_x_scale anyway, and not use the IIO_DEV_ATTR macro for gain?=

-------------------------------------------------------------------------=
----------
This email message is for the sole use of the intended recipient(s) and m= ay contain
confidential information. Any unauthorized review, use, disclosure or di= stribution
is prohibited. If you are not the intended recipient, please contact the= =20sender by
reply email and destroy all copies of the original message.
-------------------------------------------------------------------------=
----------

#8 lore raw
Quoted 23 lines · Andrew Chew
Quoted 23 lines

if I understand your comment correctly userspace needs to know that number to convert from the magn_raw values to standard units. Hence it must be exported. My point is that the calib attributes are used for devices that do their calibration offsets and scales internally. If it is a value userspace needs to apply then the correct parameters are scale and offset. In the majority of devices see so far these are fixed for all instances of the same part. That isn't true here.

Quoted 3 lines

And if we were to emit non-raw values (or provide the scale attributes), what's theexact name of the attributes? And

what units are we expecting? Gauss? Tesla? I don't see that documented anywhere.
That is documented. Gauss is the chosen standard for magnetometers. I'll assume the
conversion factors are channel dependent in which case you need

magn_x_raw, magn_x_scale, magn_x_offset

note again, we have documented everything for some device types. At the time, repeating for all of them was considered redundant, but perhaps we do need to do this for the added clarity.

Quoted 5 lines
Quoted 1 line

The value in Gauss = (magn_x_raw + magn_x_offset)*magn_x_scale

The conversion formula doesn't have an offset, actually, so the order in which the offset and scale are applied don't really matter to me.

Cool. I misread your comment (lots of brackets and I was clearly half asleep ;)

Quoted 8 lines

In any case, I think I got it now. I'm going to report the raw sensor value, and the scale factor for each axis to convert to gauss.

I assume when you say magn_x_scale, you really mean magn_x_gain? Because I don't see magn_x_scale defined in magnet.h. Or should I call the attribute magn_x_scale anyway, and not use the IIO_DEV_ATTR macro for gain?

Gah. Take the documentation first.
Looks like the magnetometer macros slipped through the net - mainly because no one was using them so they shouldn't have been there in the first place.

Please can you add suitable macros for _scale? You can also kill the _gain ones if you like, or I can add that to the next set of clean up patches. Killing the gain ones should be in a separate patch but it'll obviously cause merge issues hence it is easier for you to do both.
The only provider of such an attribute is imu/adis16400_core.c and that one is constant and shared across the axes so it uses IIO_CONST_ATTR(magn_scale,...)

Thanks for pointing this out.

Jonathan

-----------------------------------------------------------------------------------

Quoted 6 lines

This email message is for the sole use of the intended recipient(s) and may contain confidential information. Any unauthorized review, use, disclosure or distribution is prohibited. If you are not the intended recipient, please contact the sender by reply email and destroy all copies of the original message.
-----------------------------------------------------------------------------------

#9 lore raw
In reply to Alan Cox
Quoted 26 lines

-----Original Message-----
From: [email protected] [mailto:linux-iio- [email protected]] On Behalf Of ext Alan Cox
Sent: 03 September, 2010 01:20
To: [email protected]
Cc: [email protected]; [email protected]; linux- [email protected]; akpm-de/[email protected]; [email protected]; [email protected]
Subject: Re: [PATCH 1/1] iio: ak8975: Add Ak8975 magnetometer sensor

Quoted 3 lines

+/*
+ * Shows the device's mode. 0 = off, 1 = on.
+ */

Should this not be handled by runtime pm nowdays ?

drivers/staging/iio/magnetometer/ak8975.c line 245

Quoted 4 lines
drivers/staging/iio/magnetometer/ak8975.c new file +4 -0
+	if ((oval < 0) || (oval > 1)) {
+		dev_err(dev, "mode value is not supported\n");
+		return -EINVAL;
+	}

ulong cannot be < 0 and doesn't need all the brackets

Quoted 12 lines

drivers/staging/iio/magnetometer/ak8975.c line 333

Quoted 8 lines
drivers/staging/iio/magnetometer/ak8975.c new file +8 -0
+	/* Wait for the conversion to complete. */
+	while (timeout_ms) {
+		msleep(AK8975_CONVERSION_DONE_POLL_TIME);
+		state = (gpio_get_value(data->eoc_gpio) ? 1 : 0);
+		if (state)
+			break;
+		timeout_ms -= AK8975_CONVERSION_DONE_POLL_TIME;
+	}

This makes some fairly specific wiring assumptions about how the ak8975 is configured. I'm looking at the ak8974 driver in our tree and also wondering if they can be combined sanely.

With ak8974 chip, it is possible to have similar functionality without interrupt pin. This is most probably true also for ak8975 chip. It is not good to assume that everyone uses interrupt pin if the same functionally can be achieved another way. I mean polling via I2C instead of checking GPIO state after the sleep.

Based on the this driver it seems that ak8974 and ak8975 are quite similar, but also there are many differences like different register map. Maybe combining these two makes implementation just messy.

Quoted 82 lines

Review

Quoted 2 lines
(quoted diff) +1 -0
+	status = ak8975_read_data(client, AK8975_REG_ST1, 1,

&read_status);

drivers/staging/iio/magnetometer/ak8975.c line 342

Quoted 3 lines
drivers/staging/iio/magnetometer/ak8975.c new file +3 -0
+	if (!status) {
+		dev_err(&client->dev, "Error in reading ST1\n");
+		return false;

I would have expected these to return a meaningful error code not 0 ?

Quoted 1 line

+static IIO_DEVICE_ATTR(mode, S_IRUGO | S_IWUSR, show_mode,

store_mode, 0);

Quoted 1 line

+static IIO_DEVICE_ATTR(magn_x_calibscale, S_IRUGO, show_calibscale,

NULL, 0);

Quoted 1 line

+static IIO_DEVICE_ATTR(magn_y_calibscale, S_IRUGO, show_calibscale,

NULL, 1);

Quoted 1 line

+static IIO_DEVICE_ATTR(magn_z_calibscale, S_IRUGO, show_calibscale,

NULL, 2);

drivers/staging/iio/magnetometer/ak8975.c line 381

Quoted 3 lines
drivers/staging/iio/magnetometer/ak8975.c new file +3 -0
+static IIO_DEV_ATTR_MAGN_X(show_raw, AK8975_REG_HXL);
+static IIO_DEV_ATTR_MAGN_Y(show_raw, AK8975_REG_HYL);
+static IIO_DEV_ATTR_MAGN_Z(show_raw, AK8975_REG_HZL);

This seems odd as an interface as it's raw when the maths to provide non-raw (and thus abstract and easy for user space) data is trivial enough to do in kernel

(but then I still suspect it should jusst be an input device of course)

Quoted 12 lines
drivers/staging/iio/magnetometer/ak8975.c new file +12 -0
+static int ak8975_probe(struct i2c_client *client,
+			const struct i2c_device_id *id)
+{
+	struct ak8975_data *data;
+	int err;
+
+	/* Allocate our device context. */
+	data = kzalloc(sizeof(struct ak8975_data), GFP_KERNEL);
+	if (!data) {
+		dev_err(&client->dev, "Memory allocation fails\n");
+		err = -ENOMEM;
+		goto exit;

drivers/staging/iio/magnetometer/ak8975.c line 419

Quoted 10 lines
drivers/staging/iio/magnetometer/ak8975.c new file +10 -0
+	}
+
+	i2c_set_clientdata(client, data);
+	data->client = client;
+
+	mutex_init(&data->lock);
+
+	/* Grab and set up the supplied GPIO. */
+	data->eoc_irq = client->irq;
+	data->eoc_gpio = irq_to_gpio(client->irq);

It may not be via a GPIO. Better to do the GPIO handling in platform abstraction or accept passing IRQ and no GPIO value to mean "just use the
IRQ". Ie do all the gpio foo if (data->eoc_gpio) { ... }

drivers/staging/iio/magnetometer/ak8975.c

Quoted 4 lines
drivers/staging/iio/magnetometer/ak8975.c new file +4 -0
+
+	err = gpio_request(data->eoc_gpio, "ak_8975");
+	if (err < 0) {
+		dev_err(&client->dev, "failed to request GPIO %d, error

%d\n",

drivers/staging/iio/magnetometer/ak8975.c

Quoted 7 lines
drivers/staging/iio/magnetometer/ak8975.c new file +7 -0
+			data->eoc_gpio, err);
+		goto exit_free;
+	}
+
+	err = gpio_direction_input(data->eoc_gpio);
+	if (err < 0) {
+		dev_err(&client->dev, "Failed to configure input direction

for"

Review

Quoted 2 lines
(quoted diff) +2 -0
+			" GPIO %d, error %d\n", data->eoc_gpio, err);
+		gpio_free(data->eoc_gpio);

This frees the GPIO twice ?

Looks basically sound to me.

Alan

--
To unsubscribe from this list: send the line "unsubscribe linux-iio" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#10 lore raw
Quoted 39 lines · On 09/03/10 06:18, [email protected] wrote:
Quoted 26 lines

-----Original Message-----
From: [email protected] [mailto:linux-iio- [email protected]] On Behalf Of ext Alan Cox
Sent: 03 September, 2010 01:20
To: [email protected]
Cc: [email protected]; [email protected]; linux- [email protected]; [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH 1/1] iio: ak8975: Add Ak8975 magnetometer sensor

Quoted 3 lines

+/*
+ * Shows the device's mode. 0 = off, 1 = on.
+ */

Should this not be handled by runtime pm nowdays ?

drivers/staging/iio/magnetometer/ak8975.c line 245

Quoted 4 lines
drivers/staging/iio/magnetometer/ak8975.c new file +4 -0
+	if ((oval < 0) || (oval > 1)) {
+		dev_err(dev, "mode value is not supported\n");
+		return -EINVAL;
+	}

ulong cannot be < 0 and doesn't need all the brackets

Quoted 12 lines

drivers/staging/iio/magnetometer/ak8975.c line 333

Quoted 8 lines
drivers/staging/iio/magnetometer/ak8975.c new file +8 -0
+	/* Wait for the conversion to complete. */
+	while (timeout_ms) {
+		msleep(AK8975_CONVERSION_DONE_POLL_TIME);
+		state = (gpio_get_value(data->eoc_gpio) ? 1 : 0);
+		if (state)
+			break;
+		timeout_ms -= AK8975_CONVERSION_DONE_POLL_TIME;
+	}

This makes some fairly specific wiring assumptions about how the ak8975 is configured. I'm looking at the ak8974 driver in our tree and also wondering if they can be combined sanely.

Quoted 5 lines

With ak8974 chip, it is possible to have similar functionality without interrupt pin. This is most probably true also for ak8975 chip. It is not good to assume that everyone uses interrupt pin if the same functionally can be achieved another way. I mean polling via I2C instead of checking GPIO state after the sleep.

Of course this can be done, but it's up to Andrew to decide whether he wants to. I think the usual principal of writing only what people currently need applies here. Perhaps a comment in the code to point out this could be done is a sensible compromise?

Quoted 83 lines

Based on the this driver it seems that ak8974 and ak8975 are quite similar, but also there are many differences like different register map. Maybe combining these two makes implementation just messy.

Quoted 77 lines

Review

Quoted 2 lines
(quoted diff) +1 -0
+	status = ak8975_read_data(client, AK8975_REG_ST1, 1,

&read_status);

drivers/staging/iio/magnetometer/ak8975.c line 342

Quoted 3 lines
drivers/staging/iio/magnetometer/ak8975.c new file +3 -0
+	if (!status) {
+		dev_err(&client->dev, "Error in reading ST1\n");
+		return false;

I would have expected these to return a meaningful error code not 0 ?

Quoted 1 line

+static IIO_DEVICE_ATTR(mode, S_IRUGO | S_IWUSR, show_mode,

store_mode, 0);

Quoted 1 line

+static IIO_DEVICE_ATTR(magn_x_calibscale, S_IRUGO, show_calibscale,

NULL, 0);

Quoted 1 line

+static IIO_DEVICE_ATTR(magn_y_calibscale, S_IRUGO, show_calibscale,

NULL, 1);

Quoted 1 line

+static IIO_DEVICE_ATTR(magn_z_calibscale, S_IRUGO, show_calibscale,

NULL, 2);

drivers/staging/iio/magnetometer/ak8975.c line 381

Quoted 3 lines
drivers/staging/iio/magnetometer/ak8975.c new file +3 -0
+static IIO_DEV_ATTR_MAGN_X(show_raw, AK8975_REG_HXL);
+static IIO_DEV_ATTR_MAGN_Y(show_raw, AK8975_REG_HYL);
+static IIO_DEV_ATTR_MAGN_Z(show_raw, AK8975_REG_HZL);

This seems odd as an interface as it's raw when the maths to provide non-raw (and thus abstract and easy for user space) data is trivial enough to do in kernel

(but then I still suspect it should jusst be an input device of course)

Quoted 12 lines
drivers/staging/iio/magnetometer/ak8975.c new file +12 -0
+static int ak8975_probe(struct i2c_client *client,
+			const struct i2c_device_id *id)
+{
+	struct ak8975_data *data;
+	int err;
+
+	/* Allocate our device context. */
+	data = kzalloc(sizeof(struct ak8975_data), GFP_KERNEL);
+	if (!data) {
+		dev_err(&client->dev, "Memory allocation fails\n");
+		err = -ENOMEM;
+		goto exit;

drivers/staging/iio/magnetometer/ak8975.c line 419

Quoted 10 lines
drivers/staging/iio/magnetometer/ak8975.c new file +10 -0
+	}
+
+	i2c_set_clientdata(client, data);
+	data->client = client;
+
+	mutex_init(&data->lock);
+
+	/* Grab and set up the supplied GPIO. */
+	data->eoc_irq = client->irq;
+	data->eoc_gpio = irq_to_gpio(client->irq);

It may not be via a GPIO. Better to do the GPIO handling in platform abstraction or accept passing IRQ and no GPIO value to mean "just use the
IRQ". Ie do all the gpio foo if (data->eoc_gpio) { ... }

drivers/staging/iio/magnetometer/ak8975.c

Quoted 4 lines
drivers/staging/iio/magnetometer/ak8975.c new file +4 -0
+
+	err = gpio_request(data->eoc_gpio, "ak_8975");
+	if (err < 0) {
+		dev_err(&client->dev, "failed to request GPIO %d, error

%d\n",

drivers/staging/iio/magnetometer/ak8975.c

Quoted 7 lines
drivers/staging/iio/magnetometer/ak8975.c new file +7 -0
+			data->eoc_gpio, err);
+		goto exit_free;
+	}
+
+	err = gpio_direction_input(data->eoc_gpio);
+	if (err < 0) {
+		dev_err(&client->dev, "Failed to configure input direction

for"

Review

Quoted 2 lines
(quoted diff) +2 -0
+			" GPIO %d, error %d\n", data->eoc_gpio, err);
+		gpio_free(data->eoc_gpio);

This frees the GPIO twice ?

Looks basically sound to me.

Alan

#11 lore raw
Quoted 61 lines

-----Original Message-----
From: ext Jonathan Cameron [mailto:jic23-KWPb1pKIrIJaa/[email protected]]
Sent: 03 September, 2010 10:23
To: Onkalo Samu.P (Nokia-MS/Tampere)
Cc: alan-qBU/[email protected]; [email protected]; linux- [email protected]; [email protected]; linux- [email protected]; akpm-de/[email protected]; [email protected]; [email protected]
Subject: Re: [PATCH 1/1] iio: ak8975: Add Ak8975 magnetometer sensor

Quoted 26 lines · On 09/03/10 06:18, [email protected] wrote:
Quoted 26 lines

-----Original Message-----
From: [email protected] [mailto:linux-iio- [email protected]] On Behalf Of ext Alan Cox
Sent: 03 September, 2010 01:20
To: [email protected]
Cc: [email protected]; [email protected]; linux- [email protected]; akpm-de/[email protected]; [email protected]; [email protected]
Subject: Re: [PATCH 1/1] iio: ak8975: Add Ak8975 magnetometer sensor

Quoted 3 lines

+/*
+ * Shows the device's mode. 0 = off, 1 = on.
+ */

Should this not be handled by runtime pm nowdays ?

drivers/staging/iio/magnetometer/ak8975.c line 245

Quoted 4 lines
drivers/staging/iio/magnetometer/ak8975.c new file +4 -0
+	if ((oval < 0) || (oval > 1)) {
+		dev_err(dev, "mode value is not supported\n");
+		return -EINVAL;
+	}

ulong cannot be < 0 and doesn't need all the brackets

Quoted 10 lines
Quoted 10 lines

drivers/staging/iio/magnetometer/ak8975.c line 333

Quoted 8 lines
drivers/staging/iio/magnetometer/ak8975.c new file +8 -0
+	/* Wait for the conversion to complete. */
+	while (timeout_ms) {
+		msleep(AK8975_CONVERSION_DONE_POLL_TIME);
+		state = (gpio_get_value(data->eoc_gpio) ? 1 : 0);
+		if (state)
+			break;
+		timeout_ms -= AK8975_CONVERSION_DONE_POLL_TIME;
+	}

This makes some fairly specific wiring assumptions about how the

ak8975

Quoted 4 lines
Quoted 2 lines

is configured. I'm looking at the ak8974 driver in our tree and also wondering if they can be combined sanely.

With ak8974 chip, it is possible to have similar functionality without

interrupt

Quoted 1 line

pin. This is most probably true also for ak8975 chip. It is not good

to assume

Quoted 1 line

that everyone uses interrupt pin if the same functionally can be

achieved

Quoted 1 line

another way. I mean polling via I2C instead of checking GPIO state

after the

Quoted 8 lines
Quoted 1 line

sleep.

Of course this can be done, but it's up to Andrew to decide whether he wants to.
I think the usual principal of writing only what people currently need applies
here. Perhaps a comment in the code to point out this could be done is a sensible
compromise?

Sure. And the one who needs it may freely send patches.

Quoted 86 lines
Quoted 2 lines

Based on the this driver it seems that ak8974 and ak8975 are quite

similar, but

Quoted 1 line

also there are many differences like different register map. Maybe

combining

Quoted 29 lines

these two makes implementation just messy.

Quoted 26 lines

Review

Quoted 2 lines
(quoted diff) +1 -0
+	status = ak8975_read_data(client, AK8975_REG_ST1, 1,

&read_status);

drivers/staging/iio/magnetometer/ak8975.c line 342

Quoted 3 lines
drivers/staging/iio/magnetometer/ak8975.c new file +3 -0
+	if (!status) {
+		dev_err(&client->dev, "Error in reading ST1\n");
+		return false;

I would have expected these to return a meaningful error code not 0 ?

Quoted 1 line

+static IIO_DEVICE_ATTR(mode, S_IRUGO | S_IWUSR, show_mode,

store_mode, 0);

Quoted 1 line

+static IIO_DEVICE_ATTR(magn_x_calibscale, S_IRUGO, show_calibscale,

NULL, 0);

Quoted 1 line

+static IIO_DEVICE_ATTR(magn_y_calibscale, S_IRUGO, show_calibscale,

NULL, 1);

Quoted 1 line

+static IIO_DEVICE_ATTR(magn_z_calibscale, S_IRUGO, show_calibscale,

NULL, 2);

drivers/staging/iio/magnetometer/ak8975.c line 381

Quoted 3 lines
drivers/staging/iio/magnetometer/ak8975.c new file +3 -0
+static IIO_DEV_ATTR_MAGN_X(show_raw, AK8975_REG_HXL);
+static IIO_DEV_ATTR_MAGN_Y(show_raw, AK8975_REG_HYL);
+static IIO_DEV_ATTR_MAGN_Z(show_raw, AK8975_REG_HZL);

This seems odd as an interface as it's raw when the maths to provide non-raw (and thus abstract and easy for user space) data is trivial enough to do in kernel

(but then I still suspect it should jusst be an input device of

course)

Quoted 51 lines
Quoted 51 lines
Quoted 13 lines
drivers/staging/iio/magnetometer/ak8975.c new file +12 -0
+static int ak8975_probe(struct i2c_client *client,
+			const struct i2c_device_id *id)
+{
+	struct ak8975_data *data;
+	int err;
+
+	/* Allocate our device context. */
+	data = kzalloc(sizeof(struct ak8975_data), GFP_KERNEL);
+	if (!data) {
+		dev_err(&client->dev, "Memory allocation fails\n");
+		err = -ENOMEM;
+		goto exit;

drivers/staging/iio/magnetometer/ak8975.c line 419

Quoted 10 lines
drivers/staging/iio/magnetometer/ak8975.c new file +10 -0
+	}
+
+	i2c_set_clientdata(client, data);
+	data->client = client;
+
+	mutex_init(&data->lock);
+
+	/* Grab and set up the supplied GPIO. */
+	data->eoc_irq = client->irq;
+	data->eoc_gpio = irq_to_gpio(client->irq);

It may not be via a GPIO. Better to do the GPIO handling in platform abstraction or accept passing IRQ and no GPIO value to mean "just use the
IRQ". Ie do all the gpio foo if (data->eoc_gpio) { ... }

drivers/staging/iio/magnetometer/ak8975.c

Quoted 4 lines
drivers/staging/iio/magnetometer/ak8975.c new file +4 -0
+
+	err = gpio_request(data->eoc_gpio, "ak_8975");
+	if (err < 0) {
+		dev_err(&client->dev, "failed to request GPIO %d, error

%d\n",

drivers/staging/iio/magnetometer/ak8975.c

Quoted 7 lines
drivers/staging/iio/magnetometer/ak8975.c new file +7 -0
+			data->eoc_gpio, err);
+		goto exit_free;
+	}
+
+	err = gpio_direction_input(data->eoc_gpio);
+	if (err < 0) {
+		dev_err(&client->dev, "Failed to configure input direction

for"

Review

Quoted 2 lines
(quoted diff) +2 -0
+			" GPIO %d, error %d\n", data->eoc_gpio, err);
+		gpio_free(data->eoc_gpio);

This frees the GPIO twice ?

Looks basically sound to me.

Alan