Skip to contentSkip to conversation

input: add driver for Bosch Sensortec's BMA150 accelerometer

PATCH Superseded Eric Andersson 3 participants 5 messages Jun 23, 2011 – Jun 27, 2011

Subsystems (2)

v2 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v2 cover input: add driver for Bosch Sensortec's BMA150 accelerometer

Hi,

This is v2 of the bma150 driver for Bosch sensortec's digital accelerometers BMA150 and SMB380.

So, what happend since last time:
* community review: fixed smbus locking.
* community review: removed pointless ifs.
* community review: removed struct bma150acc.
* community review: use human readable values for the sysfs interface.
* community review: removed atomic_t for delay.
* Updated header with SMB380 information.
* Use standard format to print accelerometer data, i.e. (x,y,z).
* Updated string format of sysfs parameters to: value1 [value2] value3...
* Add enable/disable as sysfs parameter.
* Added sysfs-i2c-bma150 ABI documentation.

Best regards,
Eric

http://www.unixphere.com

---

Eric Andersson (1):
input: add driver for Bosch Sensortec's BMA150 accelerometer

 Documentation/ABI/testing/sysfs-i2c-bma150 |   71 +++
 drivers/input/misc/Kconfig                 |   10 +
 drivers/input/misc/Makefile                |    1 +
 drivers/input/misc/bma150.c                |  684 ++++++++++++++++++++++++++++
 4 files changed, 766 insertions(+), 0 deletions(-)
 create mode 100644 Documentation/ABI/testing/sysfs-i2c-bma150
 create mode 100644 drivers/input/misc/bma150.c
Signature · 1 line
1.7.3.4
#1 lore raw

PATCH v2 1/1 input: add driver for Bosch Sensortec's BMA150 accelerometer

---
 Documentation/ABI/testing/sysfs-i2c-bma150 |   71 +++
 drivers/input/misc/Kconfig                 |   10 +
 drivers/input/misc/Makefile                |    1 +
 drivers/input/misc/bma150.c                |  684 ++++++++++++++++++++++++++++
 4 files changed, 766 insertions(+), 0 deletions(-)
 create mode 100644 Documentation/ABI/testing/sysfs-i2c-bma150
 create mode 100644 drivers/input/misc/bma150.c
Documentation/ABI/testing/sysfs-i2c-bma150 new file +71 -0
new file mode 100644
index 0000000..16ed552
@@ -0,0 +1,71 @@
+What:		/sys/bus/i2c/devices/<busnum>-<devaddr>/range
+Date:		May 2011
+Contact:	Eric Andersson <[email protected]>
+Description:	This is used to select the full scale acceleration range. The
+		values represent the range given as +/- G.
+		Possible values are: 2, 4, 8.
+
+		Reading: returns the current acceleration range.
+
+		Writing: sets a new acceleration range.
+
+
+What:		/sys/bus/i2c/devices/<busnum>-<devaddr>/bandwidth
+Date:		May 2011
+Contact:	Eric Andersson <[email protected]>
+Description:	This is used to setup the digital filtering on ADC output data.
+		The values represent the bandwidth given in Hz.
+		Possible values are: 25, 50, 100, 190, 375, 750, 1500
+
+		Reading: returns the current bandwidth.
+
+		Writing: sets a new bandwidth.
+
+
+What:		/sys/bus/i2c/devices/<busnum>-<devaddr>/mode
+Date:		May 2011
+Contact:	Eric Andersson <[email protected]>
+Description:	This is used to set the operational mode of the chip. Possible
+		values are:
+			normal - Sets the sensor in full running mode.
+			sleep  - Sets the sensor in deep sleep.
+			wakeup - Sets the sensor to low-power mode using
+				 sequential sleep period.
+
+		Reading: returns the current operational mode.
+
+		Writing: sets a new operational mode.
+
+
+What:		/sys/bus/i2c/devices/<busnum>-<devaddr>/value
+Date:		May 2011
+Contact:	Eric Andersson <[email protected]>
+Description:	This is used to get the current acceleration values for each
+		axis. The values are represented as (x,y,z), where each axis can
+		hold a value between -512 and 511.
+
+		Reading: returns the current acceleration values.
+
+
+What:		/sys/bus/i2c/devices/<busnum>-<devaddr>/delay
+Date:		May 2011
+Contact:	Eric Andersson <[email protected]>
+Description:	This is used to select the polling rate of the driver. The
+		value is represented in ms and can be between 0 and 200. Any
+		value higher than the maximum will result in the maximum.
+
+		Reading: returns the current poll rate.
+
+		Writing: sets a new poll rate.
+
+
+What:		/sys/bus/i2c/devices/<busnum>-<devaddr>/enable
+Date:		May 2011
+Contact:	Eric Andersson <[email protected]>
+Description:	This is used to enable and disable the chip. The chip will only
+		be disabled if there are no input device users.
+
+		Reading: returns 1 if chip is enabled and 0 if not.
+
+		Writing: write 1 to enable and 0 to disable the chip.
+
drivers/input/misc/Kconfig +10 -0
index 45dc6aa..9da47fd 100644
@@ -478,4 +478,14 @@ config INPUT_XEN_KBDDEV_FRONTEND
 	  To compile this driver as a module, choose M here: the
 	  module will be called xen-kbdfront.
 
+config INPUT_BMA150
+	tristate "BMA150 acceleration sensor support"
+	depends on I2C
+	help
+	  Say Y here if you have Bosch Sensortec's BMA150 acceleration
+	  sensor hooked to an I2C bus.
+
+	  To compile this driver as a module, choose M here: the
+	  module will be called bma150.
+
 endif
drivers/input/misc/Makefile +1 -0
index 38efb2c..9b13e0e 100644
@@ -45,4 +45,5 @@ obj-$(CONFIG_INPUT_WISTRON_BTNS)	+= wistron_btns.o
 obj-$(CONFIG_INPUT_WM831X_ON)		+= wm831x-on.o
 obj-$(CONFIG_INPUT_XEN_KBDDEV_FRONTEND)	+= xen-kbdfront.o
 obj-$(CONFIG_INPUT_YEALINK)		+= yealink.o
+obj-$(CONFIG_INPUT_BMA150)		+= bma150.o
 
drivers/input/misc/bma150.c new file +684 -0

684 lines not on this page · raw message

Signature · 1 line
1.7.3.4
#2 lore raw

Documentation/ABI/testing/sysfs-i2c-bma150 line 10

Quoted 7 lines
Documentation/ABI/testing/sysfs-i2c-bma150 new file +7 -0
+Description:	This is used to select the full scale acceleration range. The
+		values represent the range given as +/- G.
+		Possible values are: 2, 4, 8.
+
+		Reading: returns the current acceleration range.
+
+		Writing: sets a new acceleration range.

As there is no way to know the valid values I suspect it ought to pick nearest inclusive for others <=8 ?

Similarly for the others

Documentation/ABI/testing/sysfs-i2c-bma150 line 37

Quoted 8 lines
Documentation/ABI/testing/sysfs-i2c-bma150 new file +8 -0
+			normal - Sets the sensor in full running mode.
+			sleep  - Sets the sensor in deep sleep.
+			wakeup - Sets the sensor to low-power mode using
+				 sequential sleep period.
+
+		Reading: returns the current operational mode.
+
+		Writing: sets a new operational mode.

We have runtime_pm for this (and in fact to switch from the bma023 driver I posted to yours we'd need runtime pm)

Documentation/ABI/testing/sysfs-i2c-bma150 line 47

Quoted 10 lines
Documentation/ABI/testing/sysfs-i2c-bma150 new file +10 -0
+
+
+What:		/sys/bus/i2c/devices/<busnum>-<devaddr>/value
+Date:		May 2011
+Contact:	Eric Andersson <[email protected]>
+Description:	This is used to get the current acceleration values for each
+		axis. The values are represented as (x,y,z), where each axis can
+		hold a value between -512 and 511.
+
+		Reading: returns the current acceleration values.

This was nacked in the bma023 driver by the IIO folks - and Dmitry pointed out you can do this without a sysfs hack. The trick is to do an initial poll in input_open at which point the ioctl query for the position will have data that is current and open/ioctl/close works and providing we go nail that into other drivers that need that kind of use the API is generic and input event based.

Documentation/ABI/testing/sysfs-i2c-bma150 line 54

Quoted 7 lines
Documentation/ABI/testing/sysfs-i2c-bma150 new file +7 -0
+
+
+What:		/sys/bus/i2c/devices/<busnum>-<devaddr>/delay
+Date:		May 2011
+Contact:	Eric Andersson <[email protected]>
+Description:	This is used to select the polling rate of the driver. The
+		value is represented in ms and can be between 0 and 200. Any

0-200 whats ?

Documentation/ABI/testing/sysfs-i2c-bma150 line 66

Quoted 5 lines
Documentation/ABI/testing/sysfs-i2c-bma150 new file +5 -0
+What:		/sys/bus/i2c/devices/<busnum>-<devaddr>/enable
+Date:		May 2011
+Contact:	Eric Andersson <[email protected]>
+Description:	This is used to enable and disable the chip. The chip will only
+		be disabled if there are no input device users.

How does this differ from the power one and why is it needed, why doesn't it disable when not in use ? How does this interact with runtime pm

Quoted 10 lines
drivers/input/misc/bma150.c new file +9 -0
+static int bma150_set_bandwidth(struct i2c_client *client, unsigned char bw)
+{
+	s32 ret;
+	unsigned char data;
+	struct bma150_data *bma150 = i2c_get_clientdata(client);
+
+	mutex_lock(&bma150->mutex);
+	ret = i2c_smbus_read_byte_data(client, BMA150_BANDWIDTH_REG);
+	if (ret < 0)

drivers/input/misc/bma150.c line 211

Quoted 10 lines
drivers/input/misc/bma150.c new file +10 -0
+		goto error;
+
+	data = (ret & ~BMA150_BANDWIDTH_MSK) |
+		((bw << BMA150_BANDWIDTH_POS) & BMA150_BANDWIDTH_MSK);
+
+	ret = i2c_smbus_write_byte_data(client, BMA150_BANDWIDTH_REG, data);
+error:
+	mutex_unlock(&bma150->mutex);
+	return ret;
+}

A lot of remarkably similar functions

drivers/input/misc/bma150.c line 261

Quoted 5 lines
drivers/input/misc/bma150.c new file +5 -0
+	mutex_lock(&bma150->mutex);
+	bma150->x = x;
+	bma150->y = y;
+	bma150->z = z;
+	mutex_unlock(&bma150->mutex);

This locking would go away if you dropped the un-needed sysfs node

drivers/input/misc/bma150.c line 536

Quoted 10 lines
drivers/input/misc/bma150.c new file +9 -0
+static int bma150_open(struct input_dev *inputdev)
+{
+	struct bma150_data *dev = input_get_drvdata(inputdev);
+
+	if (!dev->sysfs_enable)
+		return bma150_start_polling(inputdev);
+
+	return 0;
+}

Most bma023 users will be IRQ based, so this driver would need chunks extracting from the other bma023 driver submission for that to be handled.

drivers/input/misc/bma150.c line 543

Quoted 7 lines
drivers/input/misc/bma150.c new file +7 -0
+
+static void bma150_close(struct input_dev *inputdev)
+{
+	struct bma150_data *dev = input_get_drvdata(inputdev);
+
+	if (!dev->sysfs_enable)
+		bma150_stop_polling(inputdev);

What locks sysfs_enable ?

Quoted 1 line

From the Intel side to use your driver instead of bma023 we'd need IRQ

support and runtime pm but otherwise it looks basically ok. I really don't see how to reconcile proper power management with all your sysfs enables and power bits though ?

maintainer
#3 lore raw
Quoted 9 lines · Alan Cox
Quoted 9 lines
(quoted diff) +9 -0
+
+
+What:		/sys/bus/i2c/devices/<busnum>-<devaddr>/value
+Date:		May 2011
+Contact:	Eric Andersson <[email protected]>
+Description:	This is used to get the current acceleration values for each
+		axis. The values are represented as (x,y,z), where each axis can
+		hold a value between -512 and 511.
+
Quoted 9 lines

Review

Quoted 1 line
(quoted diff) +1 -0
+		Reading: returns the current acceleration values.

This was nacked in the bma023 driver by the IIO folks - and Dmitry pointed out you can do this without a sysfs hack. The trick is to do an initial poll in input_open at which point the ioctl query for the position will have data that is current and open/ioctl/close works and providing we go nail that into other drivers that need that kind of use the API is generic and input event based.

Right, I think the patch below will do what is needed for all polled devices.

Thanks.

Signature · 16 lines
Dmitry


Input: polldev - immediately poll device upon opening

To allow open/ioctl(EVIOCGABS)/close use pattern for polled devices read
the device in context of open() call instead of offloading the first read
to a workqueue. This will ensure that once call to open() returns device
would have cached reasonably recent axis values that can be retrieved via
appropriate ioctl.

Signed-off-by: Dmitry Torokhov <[email protected]>
---

 drivers/input/input-polldev.c |    6 ++++--
 1 files changed, 4 insertions(+), 2 deletions(-)
drivers/input/input-polldev.c +4 -2
index b1aabde..b253973 100644
@@ -49,8 +49,10 @@ static int input_open_polled_device(struct input_dev *input)
 		dev->open(dev);
 
 	/* Only start polling if polling is enabled */
-	if (dev->poll_interval > 0)
-		queue_delayed_work(system_freezable_wq, &dev->work, 0);
+	if (dev->poll_interval > 0) {
+		dev->poll(dev);
+		input_polldev_queue_work(dev);
+	}
 
 	return 0;
 }
#4 lore raw
In reply to Alan Cox
Quoted 10 lines · Alan Cox

Documentation/ABI/testing/sysfs-i2c-bma150 line 10

Quoted 7 lines
Documentation/ABI/testing/sysfs-i2c-bma150 new file +7 -0
+Description:	This is used to select the full scale acceleration range. The
+		values represent the range given as +/- G.
+		Possible values are: 2, 4, 8.
+
+		Reading: returns the current acceleration range.
+
+		Writing: sets a new acceleration range.

As there is no way to know the valid values I suspect it ought to pick nearest inclusive for others <=8 ?

Hmm.. I am not sure what you mean. The valid values can be retreived with a "cat range". Could you please clarify what you mean?

Quoted 11 lines

Documentation/ABI/testing/sysfs-i2c-bma150 line 37

Quoted 8 lines
Documentation/ABI/testing/sysfs-i2c-bma150 new file +8 -0
+			normal - Sets the sensor in full running mode.
+			sleep  - Sets the sensor in deep sleep.
+			wakeup - Sets the sensor to low-power mode using
+				 sequential sleep period.
+
+		Reading: returns the current operational mode.
+
+		Writing: sets a new operational mode.

We have runtime_pm for this (and in fact to switch from the bma023 driver I posted to yours we'd need runtime pm)

Sure, that is probably a good idea.

Quoted 9 lines
Quoted 9 lines
Documentation/ABI/testing/sysfs-i2c-bma150 new file +9 -0
+
+
+What:		/sys/bus/i2c/devices/<busnum>-<devaddr>/value
+Date:		May 2011
+Contact:	Eric Andersson <[email protected]>
+Description:	This is used to get the current acceleration values for each
+		axis. The values are represented as (x,y,z), where each axis can
+		hold a value between -512 and 511.
+
Quoted 8 lines

Documentation/ABI/testing/sysfs-i2c-bma150 line 47

Quoted 1 line
Documentation/ABI/testing/sysfs-i2c-bma150 new file +1 -0
+		Reading: returns the current acceleration values.

This was nacked in the bma023 driver by the IIO folks - and Dmitry pointed out you can do this without a sysfs hack. The trick is to do an initial poll in input_open at which point the ioctl query for the position will have data that is current and open/ioctl/close works and providing we go nail that into other drivers that need that kind of use the API is generic and input event based.

Ok. Thanks for pointing that out and thanks Dmitry for the patch! I'll look in to it!

Quoted 9 lines

Documentation/ABI/testing/sysfs-i2c-bma150 line 54

Quoted 7 lines
Documentation/ABI/testing/sysfs-i2c-bma150 new file +7 -0
+
+
+What:		/sys/bus/i2c/devices/<busnum>-<devaddr>/delay
+Date:		May 2011
+Contact:	Eric Andersson <[email protected]>
+Description:	This is used to select the polling rate of the driver. The
+		value is represented in ms and can be between 0 and 200. Any

0-200 whats ?

The value is in milliseconds which is stated in the text, but I can rephrase it to make it more apperent.

Quoted 9 lines

Documentation/ABI/testing/sysfs-i2c-bma150 line 66

Quoted 6 lines
Documentation/ABI/testing/sysfs-i2c-bma150 new file +5 -0
+What:		/sys/bus/i2c/devices/<busnum>-<devaddr>/enable
+Date:		May 2011
+Contact:	Eric Andersson <[email protected]>
+Description:	This is used to enable and disable the chip. The chip will only
+		be disabled if there are no input device users.

How does this differ from the power one and why is it needed, why doesn't it disable when not in use ? How does this interact with runtime pm

This is to be able to put the chip to sleep by using sysfs. As you probably know there are applications interfacing this driver only through sysfs by using the value-parameter. Without this parameter one needs to open the input device for the chip to be enabled. However, if one use the open/ioctl/close-functionality you pointed out this is not needed anymore.

Quoted 14 lines
Quoted 14 lines
drivers/input/misc/bma150.c new file +13 -0
+static int bma150_set_bandwidth(struct i2c_client *client, unsigned char bw)
+{
+	s32 ret;
+	unsigned char data;
+	struct bma150_data *bma150 = i2c_get_clientdata(client);
+
+	mutex_lock(&bma150->mutex);
+	ret = i2c_smbus_read_byte_data(client, BMA150_BANDWIDTH_REG);
+	if (ret < 0)
+		goto error;
+
+	data = (ret & ~BMA150_BANDWIDTH_MSK) |
+		((bw << BMA150_BANDWIDTH_POS) & BMA150_BANDWIDTH_MSK);
Quoted 8 lines

drivers/input/misc/bma150.c line 211

Quoted 6 lines
drivers/input/misc/bma150.c new file +6 -0
+
+	ret = i2c_smbus_write_byte_data(client, BMA150_BANDWIDTH_REG, data);
+error:
+	mutex_unlock(&bma150->mutex);
+	return ret;
+}

A lot of remarkably similar functions

I saw that you abstracted these methods in bma023. However, since you have a lot more sysfs parameters I would prefer to keep it as is in this version due to increased readability (IMHO).

Quoted 7 lines

drivers/input/misc/bma150.c line 261

Quoted 5 lines
drivers/input/misc/bma150.c new file +5 -0
+	mutex_lock(&bma150->mutex);
+	bma150->x = x;
+	bma150->y = y;
+	bma150->z = z;
+	mutex_unlock(&bma150->mutex);

This locking would go away if you dropped the un-needed sysfs node

I'll fix for v3.

Quoted 13 lines

drivers/input/misc/bma150.c line 536

Quoted 10 lines
drivers/input/misc/bma150.c new file +9 -0
+static int bma150_open(struct input_dev *inputdev)
+{
+	struct bma150_data *dev = input_get_drvdata(inputdev);
+
+	if (!dev->sysfs_enable)
+		return bma150_start_polling(inputdev);
+
+	return 0;
+}

Most bma023 users will be IRQ based, so this driver would need chunks extracting from the other bma023 driver submission for that to be handled.

As mentioned in my previous patch, using IRQ might not be a good idea. The reason for this is that the interrupt is triggered every 333 us which would create a heavy load on the system.

I have also verified this with Bosch Sensortec who recommends to not use IRQ for this chip driver.

Quoted 9 lines

drivers/input/misc/bma150.c line 543

Quoted 7 lines
drivers/input/misc/bma150.c new file +7 -0
+
+static void bma150_close(struct input_dev *inputdev)
+{
+	struct bma150_data *dev = input_get_drvdata(inputdev);
+
+	if (!dev->sysfs_enable)
+		bma150_stop_polling(inputdev);

What locks sysfs_enable ?

This will go away when the enable node is removed.

Signature · 4 lines
Best regards,
 Eric

http://www.unixphere.com