Skip to contentSkip to conversation

Accelerometer

lm-sensors Jonathan Cameron 2 participants 3 messages Aug 11, 2009 – Aug 13, 2009

lore
Share & tools
lore
#0 lore raw
Quoted 10 lines · Kalhan Trisal
Quoted 1 line

From 0d9b356e19170242fcbb746744b7ccb5156211eb Mon Sep 17 00:00:00 2001

From: Kalhan Trisal <[email protected]>
Date: Tue, 11 Aug 2009 11:12:02 -0400
Subject: [PATCH] STMicroeletronics LIS331DL three-axis digital accelerometer

This driver provides support for the LIS3331DL accelerometer accelerometer, connected to I2C. The accelerometer data is readable via sys/class/hwmon.

Please read the various previous threads on where accelerometers should go in the kernel. One of the biggest elements of them is that hwmon is not the right place. They typically aren't being used for low update rate monitoring of hardware. One option is IIO but thats still waiting for someone to have time to review the core. Take a look at the lis3l02dq driver posted to lkml to see how this sort of device would be handled (including the interrupts etc).

Anyhow, other than the where to put it issue, I have a few nitpicking comments and queries below.

Quoted 88 lines
---
 drivers/hwmon/Kconfig    |    9 ++
 drivers/hwmon/Makefile   |    1 +
 drivers/hwmon/lis331dl.c |  283 ++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 293 insertions(+), 0 deletions(-)
 create mode 100755 drivers/hwmon/lis331dl.c
drivers/hwmon/Kconfig +9 -0
index 2d50166..ac6117a 100644
@@ -975,6 +975,15 @@ config SENSORS_LIS3LV02D
 	  Say Y here if you have an applicable laptop and want to experience
 	  the awesome power of lis3lv02d.
 
+config SENSORS_LIS331DL
+	tristate "STMicroeletronics LIS331DL three-axis digital accelerometer"
+	depends on I2C_MRST
+	default n
+	help
+	  This driver provides support for the LIS3331DL accelerometer
+	  accelerometer, connected or I2C. The accelerometer data is readable via
+	  sys/class/hwmon.
+
 config SENSORS_LIS3_SPI
 	tristate "STMicroeletronics LIS3LV02Dx three-axis digital accelerometer (SPI)"
 	depends on !ACPI && SPI_MASTER && INPUT
drivers/hwmon/Makefile +1 -0
index b793dce..3b1e424 100644
@@ -89,6 +89,7 @@ obj-$(CONFIG_SENSORS_VT8231)	+= vt8231.o
 obj-$(CONFIG_SENSORS_W83627EHF)	+= w83627ehf.o
 obj-$(CONFIG_SENSORS_W83L785TS)	+= w83l785ts.o
 obj-$(CONFIG_SENSORS_W83L786NG)	+= w83l786ng.o
+obj-$(CONFIG_SENSORS_LIS331DL)	+= lis331dl.o
 
 ifeq ($(CONFIG_HWMON_DEBUG_CHIP),y)
 EXTRA_CFLAGS += -DDEBUG
drivers/hwmon/lis331dl.c new file +43 -0
new file mode 100755
index 0000000..9b999a3
@@ -0,0 +1,283 @@
+/*
+ * lis331dl.c - ST LIS331DL  Accelerometer Driver
+ *
+ * Copyright (C) 2009 Intel Corp
+ *
+ *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ *
+ */
+
+#include <linux/module.h>
+#include <linux/init.h>
+#include <linux/slab.h>
+#include <linux/i2c.h>
+#include <linux/hwmon.h>
+#include <linux/hwmon-sysfs.h>
+#include <linux/hwmon-vid.h>
+#include <linux/err.h>
+#include <linux/delay.h>
+#include <linux/mutex.h>
+#include <linux/sysfs.h>
+
+
+MODULE_AUTHOR("Kalhan Trisal <[email protected]");
+MODULE_DESCRIPTION("STMacroelectronics LIS331DL Accelerometer Driver");
+MODULE_LICENSE("GPL v2");
+
+#define ACCEL_DATA_RATE_100HZ 0
+#define ACCEL_DATA_RATE_400HZ 1
+#define ACCEL_POWER_MODE_DOWN 0

drivers/hwmon/lis331dl.c line 53

Quoted 10 lines
drivers/hwmon/lis331dl.c new file +10 -0
+#define ACCEL_POWER_MODE_ACTIVE 1
+#define ACCEL_NORMAL_MODE 0
+#define ACCEL_MEMORY_REBOOT 1
+
+/* internal return values */
+
+struct acclero_data {
+	struct device *hwmon_dev;
+	struct mutex update_lock;
+};

The above structure has rather a general name, likely to lead to a clash in the future. The same is true of a number of the functions.

drivers/hwmon/lis331dl.c line 61

Quoted 8 lines
drivers/hwmon/lis331dl.c new file +8 -0
+static unsigned int i2c_write_current_data(struct i2c_client *client,
+					unsigned int reg, unsigned int value)
+{
+	int ret_val;
+
+	ret_val = i2c_smbus_write_byte_data(client, reg, value);
+	return ret_val;
+}

Why bother with above. Doesn't seem to being anything? Presumably it was to allow for later inclusion of spi support, but as is you might as well call the smbus command directly.

drivers/hwmon/lis331dl.c line 71

Quoted 10 lines
drivers/hwmon/lis331dl.c new file +10 -0
+static ssize_t rate_show(struct device *dev,
+			struct device_attribute *attr, char *buf)
+{
+	struct i2c_client *client = to_i2c_client(dev);
+	int ret_val, val;
+
+	val = i2c_smbus_read_byte_data(client, 0x20);
+	ret_val = (val & 0x80); /* 1= 400HZ 0= 100HZ */
+	if (ret_val = 0x80)
+		ret_val = 1;

Having carefully added definitions for this above, why not use them?

drivers/hwmon/lis331dl.c line 73

Quoted 2 lines
drivers/hwmon/lis331dl.c new file +2 -0
+	return sprintf(buf, "%d\n", ret_val);
+

odd spacing.

drivers/hwmon/lis331dl.c line 74

Quoted 1 line
drivers/hwmon/lis331dl.c new file +1 -0
+}

Not clear what state is refering to. Looking at the data sheet I can see that it is power down control. Do you want userspace access to this, or should it be done via the fine grained power management stuff that is working its way into the kernel?

drivers/hwmon/lis331dl.c line 80

Quoted 6 lines
drivers/hwmon/lis331dl.c new file +6 -0
+static ssize_t state_show(struct device *dev,
+			struct device_attribute *attr, char *buf)
+{
+	struct i2c_client *client = to_i2c_client(dev);
+	int ret_val, val;
+

Definitions for register addresses would make this easier to read.

drivers/hwmon/lis331dl.c line 87

Quoted 7 lines
drivers/hwmon/lis331dl.c new file +7 -0
+	val = i2c_smbus_read_byte_data(client, 0x20);
+	ret_val = (val & 0x40);
+	if (ret_val = 0x40)
+		ret_val = 1;
+	return sprintf(buf, "%d\n", ret_val);
+}
+

Up to you whether you do a single access point for all 3 readings (scan across them) or individual reads. Guess which makes sense depends on the application.

drivers/hwmon/lis331dl.c line 92

Quoted 5 lines
drivers/hwmon/lis331dl.c new file +5 -0
+static ssize_t xyz_pos_show(struct device *dev,
+				struct device_attribute *attr, char *buf)
+{
+	int x, y, z;
+	struct i2c_client *client = to_i2c_client(dev);

I haven't read the data sheet thoroughly, but is the device capable of doing more complex reads? (would be nice to cut down the bus load of this given the device is running at maybe 400 Hz).
Table 14 of the data sheet seems to hint this can be done. I guess it would also be dependant on what your i2c adapter supports.

Quoted 6 lines
drivers/hwmon/lis331dl.c new file +6 -0
+	x = i2c_smbus_read_byte_data(client, 0x29);
+	y = i2c_smbus_read_byte_data(client, 0x2B);
+	z = i2c_smbus_read_byte_data(client, 0x2D);
+	return sprintf(buf, "%d, %d, %d \n", x, y, z);
+}
+

drivers/hwmon/lis331dl.c line 108

Quoted 10 lines
drivers/hwmon/lis331dl.c new file +10 -0
+static ssize_t rate_store(struct device *dev,
+		struct device_attribute *attr, const char *buf, size_t count)
+{
+	struct i2c_client *client = to_i2c_client(dev);
+	struct acclero_data *data = i2c_get_clientdata(client);
+	unsigned int ret_val, set_val;
+	unsigned long val;
+
+	if (strict_strtoul(buf, 10, &val))
+		return -EINVAL;

Why bother locking for updates without protecting the reads as well? Given I'm guessing a given i2c write is atomic, I'm not sure you gain anything.

Quoted 35 lines
drivers/hwmon/lis331dl.c new file +35 -0
+	ret_val = i2c_smbus_read_byte_data(client, 0x20);
+
+	mutex_lock(&data->update_lock);
+	if (val = ACCEL_DATA_RATE_100HZ)
+		set_val = (ret_val & 0x7F); /* setting the 8th bit to 0 */
+	else if (val = ACCEL_DATA_RATE_400HZ)
+		set_val = (ret_val | (1 << 7));
+	else
+		goto invarg;
+
+	i2c_write_current_data(client, 0x20, set_val);
+	mutex_unlock(&data->update_lock);
+	return count;
+invarg:
+	mutex_unlock(&data->update_lock);
+	return -EINVAL;
+}
+
+static ssize_t state_store(struct device *dev,
+		struct device_attribute *attr, const  char *buf, size_t count)
+{
+	struct i2c_client *client = to_i2c_client(dev);
+	struct acclero_data *data = i2c_get_clientdata(client);
+	unsigned int ret_val, set_val;
+	unsigned long val;
+
+	if (strict_strtoul(buf, 10, &val))
+		return -EINVAL;
+	ret_val = i2c_smbus_read_byte_data(client, 0x20);
+
+	mutex_lock(&data->update_lock);
+	if (val = ACCEL_POWER_MODE_DOWN)
+		set_val = ret_val & 0xBF; /* if value id 0 */
+	else if (val = ACCEL_POWER_MODE_ACTIVE)
+		set_val = (ret_val | (1<<6)); /* if value is 1 */

drivers/hwmon/lis331dl.c line 153

Quoted 10 lines
drivers/hwmon/lis331dl.c new file +10 -0
+	else
+		goto invarg;
+
+	i2c_write_current_data(client, 0x20, set_val);
+	mutex_unlock(&data->update_lock);
+	return count;
+invarg:
+	mutex_unlock(&data->update_lock);
+	return -EINVAL;
+}

Why do you want this exposed to userspace? To my reading the primary purpose in doing this is to reset any changes to the various calibrated parameters. Exporting to userspace might make sense if you were also exporting interfaces for them (HPcoeff for example) Even then, I'm not sure providing access to this isn't more confusing than ideal.

Quoted 23 lines
drivers/hwmon/lis331dl.c new file +23 -0
+static ssize_t reboot_mem_store(struct device *dev,
+		struct device_attribute *attr, const char *buf, size_t count)
+{
+	struct i2c_client *client = to_i2c_client(dev);
+	struct acclero_data *data = i2c_get_clientdata(client);
+	unsigned int ret_val, set_val;
+	unsigned long val;
+
+	if (strict_strtoul(buf, 10, &val))
+		return -EINVAL;
+	ret_val = i2c_smbus_read_byte_data(client, 0x21);
+	if (val = ACCEL_MEMORY_REBOOT) {
+		mutex_lock(&data->update_lock);
+		set_val = (ret_val | (1 << 6)); /* setting the 6th  bit */
+		i2c_write_current_data(client, 0x21, set_val);
+		mutex_unlock(&data->update_lock);
+	} else
+		return -EINVAL;
+	return count;
+}
+
+static DEVICE_ATTR(data_rate, S_IRUGO | S_IWUSR, rate_show, rate_store);
+static DEVICE_ATTR(power_state, S_IRUGO | S_IWUSR, state_show, state_store);

drivers/hwmon/lis331dl.c line 186

Quoted 10 lines
drivers/hwmon/lis331dl.c new file +10 -0
+static DEVICE_ATTR(reboot_mem, S_IWUSR, NULL, reboot_mem_store);
+static DEVICE_ATTR(position, S_IRUGO, xyz_pos_show, NULL);
+
+static struct attribute *mid_att_acclero[] = {
+	&dev_attr_data_rate.attr,
+	&dev_attr_power_state.attr,
+	&dev_attr_reboot_mem.attr,
+	&dev_attr_position.attr,
+	NULL
+};

This is a little odd, why have a device with an attribute group of the same name? I think this will lead to a somewhat unusual structure in sysfs.

drivers/hwmon/lis331dl.c line 193

Quoted 7 lines
drivers/hwmon/lis331dl.c new file +7 -0
+static struct attribute_group m_acclero_gr = {
+	.name = "lis331dl",
+	.attrs = mid_att_acclero
+};
+
+static void accel_set_default_config(struct i2c_client *client)
+{

This could at very least do with a comment explaining what default you have selected.

Quoted 42 lines
drivers/hwmon/lis331dl.c new file +42 -0
+	i2c_write_current_data(client, 0x20, 0x47);
+}
+
+static int  lis331dl_probe(struct i2c_client *client,
+					const struct i2c_device_id *id)
+{
+	int res;
+	struct acclero_data *data;
+
+	data = kzalloc(sizeof(struct acclero_data), GFP_KERNEL);
+	if (data = NULL) {
+		printk(KERN_WARNING "lis331dl: Memory initi failed \n");
+		return -ENOMEM;
+	}
+	mutex_init(&data->update_lock);
+	i2c_set_clientdata(client, data);
+
+	res = sysfs_create_group(&client->dev.kobj, &m_acclero_gr);
+	if (res) {
+		printk(KERN_WARNING "lis331dl: Sysfs  group failed!!\n");
+		goto acclero_error1;
+	}
+	data->hwmon_dev = hwmon_device_register(&client->dev);
+	if (IS_ERR(data->hwmon_dev)) {
+		res = PTR_ERR(data->hwmon_dev);
+		data->hwmon_dev = NULL;
+		sysfs_remove_group(&client->dev.kobj, &m_acclero_gr);
+		printk(KERN_WARNING "lis331dl: unable to register \
+						hwmon device\n");
+		goto acclero_error1;
+	}
+	accel_set_default_config(client);
+
+	dev_info(&client->dev, "%s lis331dl:  Accelerometer chip \
+							foundn", client->name);
+	return res;
+
+acclero_error1:
+	i2c_set_clientdata(client, NULL);
+	kfree(data);
+	return res;
+}

drivers/hwmon/lis331dl.c line 245

Quoted 10 lines
drivers/hwmon/lis331dl.c new file +10 -0
+
+static int lis331dl_remove(struct i2c_client *client)
+{
+	struct acclero_data *data = i2c_get_clientdata(client);
+
+	hwmon_device_unregister(data->hwmon_dev);
+	sysfs_remove_group(&client->dev.kobj, &m_acclero_gr);
+	kfree(data);
+	return 0;
+}

That's a very odd name for i2c device. Shouldn't it be lis33ld1 or similar?

Quoted 21 lines
drivers/hwmon/lis331dl.c new file +21 -0
+static struct i2c_device_id lis331dl_id[] = {
+	{ "i2c_accel", 0 },
+	{ }
+};
+
+MODULE_DEVICE_TABLE(i2c, lis331dl_id);
+
+static struct i2c_driver lis331dl_driver = {
+	.driver = {
+	.name = "lis331dl",
+	},
+	.probe = lis331dl_probe,
+	.remove = lis331dl_remove,
+	.id_table = lis331dl_id,
+};
+
+static int __init sensor_lis331dl_init(void)
+{
+	int res;
+
+	res = i2c_add_driver(&lis331dl_driver);

drivers/hwmon/lis331dl.c line 276

Quoted 10 lines
drivers/hwmon/lis331dl.c new file +10 -0
+	return res;
+}
+
+static void  __exit sensor_lis331dl_exit(void)
+{
+	i2c_del_driver(&lis331dl_driver);
+}
+
+module_init(sensor_lis331dl_init);
+module_exit(sensor_lis331dl_exit);

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

#1 lore raw
Quoted 2 lines · Kalhan

Thanks Jonathan.
Please find my comments inline.

Hi Kalhan,

Few responses below. For reference, IIO (which might be a more suitable location than hwmon) is going to go into the staging tree shortly.

If you are interested, I'd be happy to do a conversion if you'd be willing to test against the hardware. To support the current functionality should be straight forward

...

Quoted 14 lines

Review

Quoted 8 lines
(quoted diff) +8 -0
+static unsigned int i2c_write_current_data(struct i2c_client *client,
+                                     unsigned int reg, unsigned int value)
+{
+     int ret_val;
+
+     ret_val = i2c_smbus_write_byte_data(client, reg, value);
+     return ret_val;
+}

Why bother with above. Doesn't seem to being anything? Presumably it was to allow for later inclusion of spi support, but as is you might as well call the smbus command directly.

Quoted 1 line
Quoted 1 line

yes can be done but for future support/enhancement let's keep it as modular as possible.

But it's not modular unless you make it take the say a struct acclero_data (with the i2c_client as a member). Thus when you add spi support, you can use function pointers to call the relevant write function. As is, it adds a module that doesn't do anything.

...

Quoted 9 lines

Not clear what state is refering to. Looking at the data sheet I can see that it is power down control. Do you want userspace access to this, or should it be done via the fine grained power management stuff that is working its way into the kernel?

Quoted 3 lines
Quoted 3 lines

in mids the Apps. manager are intelligent to check if no apps are going to use the device they can power down the device, power management can do this, but we decided that exposing this to app manager will be more effective as we can put the device in active mode only when needed.

There's been some recent work on runtime power management.
I've only been vaguely following it, but I believe the intention is to allow for putting individual devices to sleep much as you are here.

http://lkml.org/lkml/2009/8/9/128

Having read the above posting, it appears that the i2c bus may need support for this as well. Perhaps your state sysfs interface is the best way to go in the meantime.

Quoted 7 lines

Up to you whether you do a single access point for all 3 readings (scan across them) or individual reads. Guess which makes sense depends on the application.

Quoted 2 lines
Quoted 2 lines

thought about this but apps want all the data at a given time stamp, this also many not be accurate but by calling individual x,y,z this seems to be more accurate at given time stamp what is the current position.

Make sense, though the average dma based i2c controller is going to make knowing when a transfer occured complex, particularly with the 3 separate transfers used below.

Quoted 13 lines

Review

Quoted 5 lines
(quoted diff) +5 -0
+static ssize_t xyz_pos_show(struct device *dev,
+                             struct device_attribute *attr, char *buf)
+{
+     int x, y, z;
+     struct i2c_client *client = to_i2c_client(dev);

I haven't read the data sheet thoroughly, but is the device capable of doing more complex reads? (would be nice to cut down the bus load of this given the device is running at maybe 400 Hz).
Table 14 of the data sheet seems to hint this can be done. I guess it would also be dependant on what your i2c adapter supports.

Quoted 2 lines
Quoted 2 lines

Agree It is adapter dependent, will put the note here saying that if the adapter support multiple register read simultaneously then that also can be performed.

That's fair enough.

Quoted 11 lines
Quoted 11 lines
(quoted diff) +11 -0
+     x = i2c_smbus_read_byte_data(client, 0x29);
+     y = i2c_smbus_read_byte_data(client, 0x2B);
+     z = i2c_smbus_read_byte_data(client, 0x2D);
+     return sprintf(buf, "%d, %d, %d \n", x, y, z);
+}
+
+static ssize_t rate_store(struct device *dev,
+             struct device_attribute *attr, const char *buf, size_t count)
+{
+     struct i2c_client *client = to_i2c_client(dev);
+     struct acclero_data *data = i2c_get_clientdata(client);
Quoted 11 lines

Review

Quoted 5 lines
(quoted diff) +5 -0
+     unsigned int ret_val, set_val;
+     unsigned long val;
+
+     if (strict_strtoul(buf, 10, &val))
+             return -EINVAL;

Why bother locking for updates without protecting the reads as well? Given I'm guessing a given i2c write is atomic, I'm not sure you gain anything.

Quoted 1 line
Quoted 1 line

will remove mutex

Might be better to just move it to cover the read as well. Although I don't think there are any cases here that could lead to an undesirable state I haven't read the data sheet thoroughly enough to be sure there aren't any that might be added in the future.

Quoted 44 lines
Quoted 44 lines
(quoted diff) +43 -0
+     ret_val = i2c_smbus_read_byte_data(client, 0x20);
+
+     mutex_lock(&data->update_lock);
+     if (val = ACCEL_DATA_RATE_100HZ)
+             set_val = (ret_val & 0x7F); /* setting the 8th bit to 0 */
+     else if (val = ACCEL_DATA_RATE_400HZ)
+             set_val = (ret_val | (1 << 7));
+     else
+             goto invarg;
+
+     i2c_write_current_data(client, 0x20, set_val);
+     mutex_unlock(&data->update_lock);
+     return count;
+invarg:
+     mutex_unlock(&data->update_lock);
+     return -EINVAL;
+}
 
+static ssize_t state_store(struct device *dev,
+             struct device_attribute *attr, const  char *buf, size_t count)
+{
+     struct i2c_client *client = to_i2c_client(dev);
+     struct acclero_data *data = i2c_get_clientdata(client);
+     unsigned int ret_val, set_val;
+     unsigned long val;
+
+     if (strict_strtoul(buf, 10, &val))
+             return -EINVAL;
+     ret_val = i2c_smbus_read_byte_data(client, 0x20);
+
+     mutex_lock(&data->update_lock);
+     if (val = ACCEL_POWER_MODE_DOWN)
+             set_val = ret_val & 0xBF; /* if value id 0 */
+     else if (val = ACCEL_POWER_MODE_ACTIVE)
+             set_val = (ret_val | (1<<6)); /* if value is 1 */
+     else
+             goto invarg;
+
+     i2c_write_current_data(client, 0x20, set_val);
+     mutex_unlock(&data->update_lock);
+     return count;
+invarg:
+     mutex_unlock(&data->update_lock);
+     return -EINVAL;
Quoted 13 lines
Quoted 1 line

+}

Why do you want this exposed to userspace? To my reading the primary purpose in doing this is to reset any changes to the various calibrated parameters. Exporting to userspace might make sense if you were also exporting interfaces for them (HPcoeff for example) Even then, I'm not sure providing access to this isn't more confusing than ideal.

Quoted 4 lines
Quoted 4 lines

this is exposed to user space if the application manager is sensing that the values
from the device are inconsistent, then best way is to reboot the device. This option is only for sensor application manager. I felt it will be useful when device misbehaves and can be rectified dynamically by rebooting the device.

Does this actually happen? If so definitely worth adding some documentation of the circumstances as this would be an out and out hardware bug and might trip other people up. In general I'd be loath to have such a command exported to userspace, but if it's needed to work round a hardware problem, fair enough. I'd have thought the principle reason for having the support in the hardware was to ensure a known consistent state on driver load rather than to fix problems during use.

Quoted 29 lines
Quoted 29 lines
(quoted diff) +28 -0
+static ssize_t reboot_mem_store(struct device *dev,
+             struct device_attribute *attr, const char *buf, size_t count)
+{
+     struct i2c_client *client = to_i2c_client(dev);
+     struct acclero_data *data = i2c_get_clientdata(client);
+     unsigned int ret_val, set_val;
+     unsigned long val;
+
+     if (strict_strtoul(buf, 10, &val))
+             return -EINVAL;
+     ret_val = i2c_smbus_read_byte_data(client, 0x21);
+     if (val = ACCEL_MEMORY_REBOOT) {
+             mutex_lock(&data->update_lock);
+             set_val = (ret_val | (1 << 6)); /* setting the 6th  bit */
+             i2c_write_current_data(client, 0x21, set_val);
+             mutex_unlock(&data->update_lock);
+     } else
+             return -EINVAL;
+     return count;
+}
+
+static DEVICE_ATTR(data_rate, S_IRUGO | S_IWUSR, rate_show, rate_store);
+static DEVICE_ATTR(power_state, S_IRUGO | S_IWUSR, state_show, state_store);
+static DEVICE_ATTR(reboot_mem, S_IWUSR, NULL, reboot_mem_store);
+static DEVICE_ATTR(position, S_IRUGO, xyz_pos_show, NULL);
+
+static struct attribute *mid_att_acclero[] = {
+     &dev_attr_data_rate.attr,
Quoted 10 lines

Review

Quoted 5 lines
(quoted diff) +5 -0
+     &dev_attr_power_state.attr,
+     &dev_attr_reboot_mem.attr,
+     &dev_attr_position.attr,
+     NULL
+};

This is a little odd, why have a device with an attribute group of the same name? I think this will lead to a somewhat unusual structure in sysfs.

Quoted 1 line
Quoted 1 line

I am not clear here, does it mean that we should rename it as accelerometer.

No, don't give it any name at all. Then all the sysfs files should appear in /sys/class/hwmon/device0/device/ rather than adding an additional directory layer.

Quoted 5 lines

Review

Quoted 5 lines
(quoted diff) +4 -0
+static struct attribute_group m_acclero_gr = {
+     .name = "lis331dl",
+     .attrs = mid_att_acclero
+};

...

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

#2 lore raw
In reply to Jonathan Cameron

Thanks Jonathan.
Please find my comments inline.

Thanks
Kalhan

-----Original Message-----
From: Jonathan Cameron [mailto:[email protected]]
Sent: Tuesday, August 11, 2009 6:35 PM
To: Trisal, Kalhan
Cc: [email protected]; [email protected]
Subject: Re: [lm-sensors] Accelerometer driver for STMicroeletronics-LIS331DL-three-axis-digital

Quoted 10 lines · Kalhan Trisal
Quoted 1 line

From 0d9b356e19170242fcbb746744b7ccb5156211eb Mon Sep 17 00:00:00 2001

From: Kalhan Trisal <[email protected]>
Date: Tue, 11 Aug 2009 11:12:02 -0400
Subject: [PATCH] STMicroeletronics LIS331DL three-axis digital accelerometer

This driver provides support for the LIS3331DL accelerometer accelerometer, connected to I2C. The accelerometer data is readable via sys/class/hwmon.

Please read the various previous threads on where accelerometers should go in the kernel. One of the biggest elements of them is that hwmon is not the right place. They typically aren't being used for low update rate monitoring of hardware. One option is IIO but thats still waiting for someone to have time to review the core. Take a look at the lis3l02dq driver posted to lkml to see how this sort of device would be handled (including the interrupts etc).

Anyhow, other than the where to put it issue, I have a few nitpicking comments and queries below.

Quoted 88 lines
---
 drivers/hwmon/Kconfig    |    9 ++
 drivers/hwmon/Makefile   |    1 +
 drivers/hwmon/lis331dl.c |  283 ++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 293 insertions(+), 0 deletions(-)
 create mode 100755 drivers/hwmon/lis331dl.c
drivers/hwmon/Kconfig +9 -0
index 2d50166..ac6117a 100644
@@ -975,6 +975,15 @@ config SENSORS_LIS3LV02D
        Say Y here if you have an applicable laptop and want to experience
        the awesome power of lis3lv02d.
 
+config SENSORS_LIS331DL
+     tristate "STMicroeletronics LIS331DL three-axis digital accelerometer"
+     depends on I2C_MRST
+     default n
+     help
+       This driver provides support for the LIS3331DL accelerometer
+       accelerometer, connected or I2C. The accelerometer data is readable via
+       sys/class/hwmon.
+
 config SENSORS_LIS3_SPI
      tristate "STMicroeletronics LIS3LV02Dx three-axis digital accelerometer (SPI)"
      depends on !ACPI && SPI_MASTER && INPUT
drivers/hwmon/Makefile +1 -0
index b793dce..3b1e424 100644
@@ -89,6 +89,7 @@ obj-$(CONFIG_SENSORS_VT8231)        += vt8231.o
 obj-$(CONFIG_SENSORS_W83627EHF)      += w83627ehf.o
 obj-$(CONFIG_SENSORS_W83L785TS)      += w83l785ts.o
 obj-$(CONFIG_SENSORS_W83L786NG)      += w83l786ng.o
+obj-$(CONFIG_SENSORS_LIS331DL)       += lis331dl.o
 
 ifeq ($(CONFIG_HWMON_DEBUG_CHIP),y)
 EXTRA_CFLAGS += -DDEBUG
drivers/hwmon/lis331dl.c new file +43 -0
new file mode 100755
index 0000000..9b999a3
@@ -0,0 +1,283 @@
+/*
+ * lis331dl.c - ST LIS331DL  Accelerometer Driver
+ *
+ * Copyright (C) 2009 Intel Corp
+ *
+ *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ *
+ */
+
+#include <linux/module.h>
+#include <linux/init.h>
+#include <linux/slab.h>
+#include <linux/i2c.h>
+#include <linux/hwmon.h>
+#include <linux/hwmon-sysfs.h>
+#include <linux/hwmon-vid.h>
+#include <linux/err.h>
+#include <linux/delay.h>
+#include <linux/mutex.h>
+#include <linux/sysfs.h>
+
+
+MODULE_AUTHOR("Kalhan Trisal <[email protected]");
+MODULE_DESCRIPTION("STMacroelectronics LIS331DL Accelerometer Driver");
+MODULE_LICENSE("GPL v2");
+
+#define ACCEL_DATA_RATE_100HZ 0
+#define ACCEL_DATA_RATE_400HZ 1
+#define ACCEL_POWER_MODE_DOWN 0

drivers/hwmon/lis331dl.c line 53

Quoted 10 lines
drivers/hwmon/lis331dl.c new file +10 -0
+#define ACCEL_POWER_MODE_ACTIVE 1
+#define ACCEL_NORMAL_MODE 0
+#define ACCEL_MEMORY_REBOOT 1
+
+/* internal return values */
+
+struct acclero_data {
+     struct device *hwmon_dev;
+     struct mutex update_lock;
+};

The above structure has rather a general name, likely to lead to a clash in the future. The same is true of a number of the functions.

drivers/hwmon/lis331dl.c

Quoted 10 lines
Quoted 1 line

will rename to device specific structure.

drivers/hwmon/lis331dl.c new file +8 -0
+static unsigned int i2c_write_current_data(struct i2c_client *client,
+                                     unsigned int reg, unsigned int value)
+{
+     int ret_val;
+
+     ret_val = i2c_smbus_write_byte_data(client, reg, value);
+     return ret_val;
+}

Why bother with above. Doesn't seem to being anything? Presumably it was to allow for later inclusion of spi support, but as is you might as well call the smbus command directly.

drivers/hwmon/lis331dl.c

Quoted 12 lines
Quoted 1 line

yes can be done but for future support/enhancement let's keep it as modular as possible.

drivers/hwmon/lis331dl.c new file +10 -0
+static ssize_t rate_show(struct device *dev,
+                     struct device_attribute *attr, char *buf)
+{
+     struct i2c_client *client = to_i2c_client(dev);
+     int ret_val, val;
+
+     val = i2c_smbus_read_byte_data(client, 0x20);
+     ret_val = (val & 0x80); /* 1= 400HZ 0= 100HZ */
+     if (ret_val = 0x80)
+             ret_val = 1;

Having carefully added definitions for this above, why not use them?

drivers/hwmon/lis331dl.c

Quoted 4 lines
Quoted 1 line

will do this change in next patch.

drivers/hwmon/lis331dl.c new file +2 -0
+     return sprintf(buf, "%d\n", ret_val);
+

odd spacing.

Quoted 2 lines
Quoted 1 line

will rectify this.

+}

Not clear what state is refering to. Looking at the data sheet I can see that it is power down control. Do you want userspace access to this, or should it be done via the fine grained power management stuff that is working its way into the kernel?

drivers/hwmon/lis331dl.c

Quoted 10 lines
Quoted 3 lines

in mids the Apps. manager are intelligent to check if no apps are going to use the device they can power down the device, power management can do this, but we decided that exposing this to app manager will be more effective as we can put the device in active mode only when needed.

drivers/hwmon/lis331dl.c new file +6 -0
+static ssize_t state_show(struct device *dev,
+                     struct device_attribute *attr, char *buf)
+{
+     struct i2c_client *client = to_i2c_client(dev);
+     int ret_val, val;
+

Definitions for register addresses would make this easier to read.

drivers/hwmon/lis331dl.c

Quoted 9 lines
Quoted 1 line

taken will do the change in next patch

drivers/hwmon/lis331dl.c new file +7 -0
+     val = i2c_smbus_read_byte_data(client, 0x20);
+     ret_val = (val & 0x40);
+     if (ret_val = 0x40)
+             ret_val = 1;
+     return sprintf(buf, "%d\n", ret_val);
+}
+

Up to you whether you do a single access point for all 3 readings (scan across them) or individual reads. Guess which makes sense depends on the application.

drivers/hwmon/lis331dl.c

Quoted 8 lines
Quoted 2 lines

thought about this but apps want all the data at a given time stamp, this also many not be accurate but by calling individual x,y,z this seems to be more accurate at given time stamp what is the current position.

drivers/hwmon/lis331dl.c new file +5 -0
+static ssize_t xyz_pos_show(struct device *dev,
+                             struct device_attribute *attr, char *buf)
+{
+     int x, y, z;
+     struct i2c_client *client = to_i2c_client(dev);

I haven't read the data sheet thoroughly, but is the device capable of doing more complex reads? (would be nice to cut down the bus load of this given the device is running at maybe 400 Hz).
Table 14 of the data sheet seems to hint this can be done. I guess it would also be dependant on what your i2c adapter supports.

Quoted 9 lines
Quoted 2 lines

Agree It is adapter dependent, will put the note here saying that if the adapter support multiple register read simultaneously then that also can be performed.

drivers/hwmon/lis331dl.c new file +6 -0
+     x = i2c_smbus_read_byte_data(client, 0x29);
+     y = i2c_smbus_read_byte_data(client, 0x2B);
+     z = i2c_smbus_read_byte_data(client, 0x2D);
+     return sprintf(buf, "%d, %d, %d \n", x, y, z);
+}
+

drivers/hwmon/lis331dl.c

Quoted 10 lines
drivers/hwmon/lis331dl.c new file +10 -0
+static ssize_t rate_store(struct device *dev,
+             struct device_attribute *attr, const char *buf, size_t count)
+{
+     struct i2c_client *client = to_i2c_client(dev);
+     struct acclero_data *data = i2c_get_clientdata(client);
+     unsigned int ret_val, set_val;
+     unsigned long val;
+
+     if (strict_strtoul(buf, 10, &val))
+             return -EINVAL;

Why bother locking for updates without protecting the reads as well? Given I'm guessing a given i2c write is atomic, I'm not sure you gain anything.

Quoted 37 lines
Quoted 1 line

will remove mutex

drivers/hwmon/lis331dl.c new file +35 -0
+     ret_val = i2c_smbus_read_byte_data(client, 0x20);
+
+     mutex_lock(&data->update_lock);
+     if (val = ACCEL_DATA_RATE_100HZ)
+             set_val = (ret_val & 0x7F); /* setting the 8th bit to 0 */
+     else if (val = ACCEL_DATA_RATE_400HZ)
+             set_val = (ret_val | (1 << 7));
+     else
+             goto invarg;
+
+     i2c_write_current_data(client, 0x20, set_val);
+     mutex_unlock(&data->update_lock);
+     return count;
+invarg:
+     mutex_unlock(&data->update_lock);
+     return -EINVAL;
+}
+
+static ssize_t state_store(struct device *dev,
+             struct device_attribute *attr, const  char *buf, size_t count)
+{
+     struct i2c_client *client = to_i2c_client(dev);
+     struct acclero_data *data = i2c_get_clientdata(client);
+     unsigned int ret_val, set_val;
+     unsigned long val;
+
+     if (strict_strtoul(buf, 10, &val))
+             return -EINVAL;
+     ret_val = i2c_smbus_read_byte_data(client, 0x20);
+
+     mutex_lock(&data->update_lock);
+     if (val = ACCEL_POWER_MODE_DOWN)
+             set_val = ret_val & 0xBF; /* if value id 0 */
+     else if (val = ACCEL_POWER_MODE_ACTIVE)
+             set_val = (ret_val | (1<<6)); /* if value is 1 */

drivers/hwmon/lis331dl.c

Quoted 10 lines
drivers/hwmon/lis331dl.c new file +10 -0
+     else
+             goto invarg;
+
+     i2c_write_current_data(client, 0x20, set_val);
+     mutex_unlock(&data->update_lock);
+     return count;
+invarg:
+     mutex_unlock(&data->update_lock);
+     return -EINVAL;
+}

Why do you want this exposed to userspace? To my reading the primary purpose in doing this is to reset any changes to the various calibrated parameters. Exporting to userspace might make sense if you were also exporting interfaces for them (HPcoeff for example) Even then, I'm not sure providing access to this isn't more confusing than ideal.

Quoted 28 lines
Quoted 4 lines

this is exposed to user space if the application manager is sensing that the values
from the device are inconsistent, then best way is to reboot the device. This option is only for sensor application manager. I felt it will be useful when device misbehaves and can be rectified dynamically by rebooting the device.

drivers/hwmon/lis331dl.c new file +23 -0
+static ssize_t reboot_mem_store(struct device *dev,
+             struct device_attribute *attr, const char *buf, size_t count)
+{
+     struct i2c_client *client = to_i2c_client(dev);
+     struct acclero_data *data = i2c_get_clientdata(client);
+     unsigned int ret_val, set_val;
+     unsigned long val;
+
+     if (strict_strtoul(buf, 10, &val))
+             return -EINVAL;
+     ret_val = i2c_smbus_read_byte_data(client, 0x21);
+     if (val = ACCEL_MEMORY_REBOOT) {
+             mutex_lock(&data->update_lock);
+             set_val = (ret_val | (1 << 6)); /* setting the 6th  bit */
+             i2c_write_current_data(client, 0x21, set_val);
+             mutex_unlock(&data->update_lock);
+     } else
+             return -EINVAL;
+     return count;
+}
+
+static DEVICE_ATTR(data_rate, S_IRUGO | S_IWUSR, rate_show, rate_store);
+static DEVICE_ATTR(power_state, S_IRUGO | S_IWUSR, state_show, state_store);

drivers/hwmon/lis331dl.c

Quoted 10 lines
drivers/hwmon/lis331dl.c new file +10 -0
+static DEVICE_ATTR(reboot_mem, S_IWUSR, NULL, reboot_mem_store);
+static DEVICE_ATTR(position, S_IRUGO, xyz_pos_show, NULL);
+
+static struct attribute *mid_att_acclero[] = {
+     &dev_attr_data_rate.attr,
+     &dev_attr_power_state.attr,
+     &dev_attr_reboot_mem.attr,
+     &dev_attr_position.attr,
+     NULL
+};

This is a little odd, why have a device with an attribute group of the same name? I think this will lead to a somewhat unusual structure in sysfs.

drivers/hwmon/lis331dl.c

Quoted 9 lines
Quoted 1 line

I am not clear here, does it mean that we should rename it as accelerometer.

drivers/hwmon/lis331dl.c new file +7 -0
+static struct attribute_group m_acclero_gr = {
+     .name = "lis331dl",
+     .attrs = mid_att_acclero
+};
+
+static void accel_set_default_config(struct i2c_client *client)
+{

This could at very least do with a comment explaining what default you have selected.

Quoted 44 lines
Quoted 1 line

Accepted will do that.

drivers/hwmon/lis331dl.c new file +42 -0
+     i2c_write_current_data(client, 0x20, 0x47);
+}
+
+static int  lis331dl_probe(struct i2c_client *client,
+                                     const struct i2c_device_id *id)
+{
+     int res;
+     struct acclero_data *data;
+
+     data = kzalloc(sizeof(struct acclero_data), GFP_KERNEL);
+     if (data = NULL) {
+             printk(KERN_WARNING "lis331dl: Memory initi failed \n");
+             return -ENOMEM;
+     }
+     mutex_init(&data->update_lock);
+     i2c_set_clientdata(client, data);
+
+     res = sysfs_create_group(&client->dev.kobj, &m_acclero_gr);
+     if (res) {
+             printk(KERN_WARNING "lis331dl: Sysfs  group failed!!\n");
+             goto acclero_error1;
+     }
+     data->hwmon_dev = hwmon_device_register(&client->dev);
+     if (IS_ERR(data->hwmon_dev)) {
+             res = PTR_ERR(data->hwmon_dev);
+             data->hwmon_dev = NULL;
+             sysfs_remove_group(&client->dev.kobj, &m_acclero_gr);
+             printk(KERN_WARNING "lis331dl: unable to register \
+                                             hwmon device\n");
+             goto acclero_error1;
+     }
+     accel_set_default_config(client);
+
+     dev_info(&client->dev, "%s lis331dl:  Accelerometer chip \
+                                                     foundn", client->name);
+     return res;
+
+acclero_error1:
+     i2c_set_clientdata(client, NULL);
+     kfree(data);
+     return res;
+}

drivers/hwmon/lis331dl.c

Quoted 10 lines
drivers/hwmon/lis331dl.c new file +10 -0
+
+static int lis331dl_remove(struct i2c_client *client)
+{
+     struct acclero_data *data = i2c_get_clientdata(client);
+
+     hwmon_device_unregister(data->hwmon_dev);
+     sysfs_remove_group(&client->dev.kobj, &m_acclero_gr);
+     kfree(data);
+     return 0;
+}

That's a very odd name for i2c device. Shouldn't it be lis33ld1 or similar?

Quoted 23 lines
Quoted 1 line

Accepted will do the change.

drivers/hwmon/lis331dl.c new file +21 -0
+static struct i2c_device_id lis331dl_id[] = {
+     { "i2c_accel", 0 },
+     { }
+};
+
+MODULE_DEVICE_TABLE(i2c, lis331dl_id);
+
+static struct i2c_driver lis331dl_driver = {
+     .driver = {
+     .name = "lis331dl",
+     },
+     .probe = lis331dl_probe,
+     .remove = lis331dl_remove,
+     .id_table = lis331dl_id,
+};
+
+static int __init sensor_lis331dl_init(void)
+{
+     int res;
+
+     res = i2c_add_driver(&lis331dl_driver);

drivers/hwmon/lis331dl.c

Quoted 10 lines
drivers/hwmon/lis331dl.c new file +10 -0
+     return res;
+}
+
+static void  __exit sensor_lis331dl_exit(void)
+{
+     i2c_del_driver(&lis331dl_driver);
+}
+
+module_init(sensor_lis331dl_init);
+module_exit(sensor_lis331dl_exit);

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