Skip to contentSkip to conversation

input: add driver for Bosch Sensortec's BMA150 accelerometer

PATCH Superseded Eric Andersson 2 participants 5 messages May 31, 2011 – Jun 9, 2011

Subsystems (2)

v1 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

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

---
 drivers/input/misc/Kconfig  |   10 +
 drivers/input/misc/Makefile |    1 +
 drivers/input/misc/bma150.c |  554 +++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 565 insertions(+), 0 deletions(-)
 create mode 100644 drivers/input/misc/bma150.c
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 +554 -0

554 lines not on this page · raw message

Signature · 1 line
1.7.3.4
#1 lore raw

See the driver I posted some time ago. It's in rather better state than this one and as it's been through most of the review process and also handles the BMA023 and SMB380 so I think would be a better place to start from. I'll report that driver in a moment.

Alan

Some of the first immediately obvious questions though - in particular the lack of clear locking ...

drivers/input/misc/bma150.c line 71

Quoted 5 lines
drivers/input/misc/bma150.c new file +5 -0
+struct bma150acc {
+	s16	x,
+		y,
+		z;
+};

Why does this need to be a struct ?

drivers/input/misc/bma150.c line 92

Quoted 4 lines
drivers/input/misc/bma150.c new file +4 -0
+	if ((mode != BMA150_MODE_NORMAL) &&
+	    (mode != BMA150_MODE_SLEEP) &&
+	    (mode != BMA150_MODE_WAKE_UP))
+		return -EINVAL;

How can any other mode get passed ?

Quoted 12 lines
drivers/input/misc/bma150.c new file +12 -0
+
+	data1 = i2c_smbus_read_byte_data(client, BMA150_WAKE_UP_REG);
+	if (data1 < 0)
+		return ret;
+
+	data1 = (data1 & ~BMA150_WAKE_UP_MSK) |
+		((mode << BMA150_WAKE_UP_POS) & BMA150_WAKE_UP_MSK);
+
+	data2 = i2c_smbus_read_byte_data(client, BMA150_SLEEP_REG);
+	if (data2 < 0)
+		return ret;
+

drivers/input/misc/bma150.c line 114

Quoted 10 lines
drivers/input/misc/bma150.c new file +10 -0
+	data2 = (data2 & ~BMA150_SLEEP_MSK) |
+		(((mode>>1) << BMA150_SLEEP_POS) & BMA150_SLEEP_MSK);
+
+	ret = i2c_smbus_write_byte_data(client, BMA150_WAKE_UP_REG, data1);
+	if (ret < 0)
+		return ret;
+
+	ret = i2c_smbus_write_byte_data(client, BMA150_SLEEP_REG, data2);
+	if (ret < 0)
+		return ret;

What locks this SMBUS transaction against others

drivers/input/misc/bma150.c line 129

Quoted 7 lines
drivers/input/misc/bma150.c new file +7 -0
+static int bma150_set_range(struct i2c_client *client, unsigned char range)
+{
+	int ret;
+	unsigned char data;
+
+	if (range > BMA150_RANGE_8G)
+		return -EINVAL;

This should be actual values not a register range

drivers/input/misc/bma150.c line 138

Quoted 9 lines
drivers/input/misc/bma150.c new file +9 -0
+
+	data = i2c_smbus_read_byte_data(client, BMA150_RANGE_REG);
+	if (data < 0)
+		return ret;
+
+	data = (data & ~BMA150_RANGE_MSK) |
+		((range << BMA150_RANGE_POS) & BMA150_RANGE_MSK);
+
+	ret = i2c_smbus_write_byte_data(client, BMA150_RANGE_REG, data);

What locks this ?

drivers/input/misc/bma150.c line 119

Quoted 4 lines
drivers/input/misc/bma150.c new file +4 -0
+	if (ret < 0)
+		return ret;
+
+	return 0;

Why this pointless if ?

drivers/input/misc/bma150.c line 155

Quoted 13 lines
drivers/input/misc/bma150.c new file +13 -0
+}
+
+static int bma150_get_range(struct i2c_client *client, unsigned char *range)
+{
+	int ret;
+	unsigned char data;
+
+	data = i2c_smbus_read_byte_data(client, BMA150_RANGE_REG);
+	if (data < 0)
+		return ret;
+
+	*range = (data & BMA150_RANGE_MSK) >> BMA150_RANGE_POS;
+	return 0;

See comments above

drivers/input/misc/bma150.c line 158

Quoted 3 lines
drivers/input/misc/bma150.c new file +3 -0
+}
+
+static int bma150_set_bandwidth(struct i2c_client *client, unsigned char bw)

Similar problem

Quoted 9 lines
drivers/input/misc/bma150.c new file +9 -0
+static int bma150_read_accel_xyz(struct i2c_client *client,
+		struct bma150acc *acc)
+{
+	unsigned char data[6];
+	int ret = i2c_smbus_read_i2c_block_data(client,
+			BMA150_ACC_X_LSB_REG, 6, data);
+	if (ret != 6)
+		return -EIO;
+

drivers/input/misc/bma150.c line 211

Quoted 10 lines
drivers/input/misc/bma150.c new file +10 -0
+	acc->x = ((0xC0 & data[0]) >> 6) | (data[1] << 2);
+	acc->y = ((0xC0 & data[2]) >> 6) | (data[3] << 2);
+	acc->z = ((0xC0 & data[4]) >> 6) | (data[5] << 2);
+
+	/* sign extension */
+	acc->x = (s16) (acc->x << 6) >> 6;
+	acc->y = (s16) (acc->y << 6) >> 6;
+	acc->z = (s16) (acc->z << 6) >> 6;
+
+	return 0;

Why the separate function and struct

drivers/input/misc/bma150.c line 215

Quoted 4 lines
drivers/input/misc/bma150.c new file +4 -0
+}
+
+static void bma150_work_func(struct work_struct *work)
+{

Threaded IRQ ?

drivers/input/misc/bma150.c line 244

Quoted 7 lines
drivers/input/misc/bma150.c new file +7 -0
+static ssize_t bma150_mode_show(struct device *dev,
+		struct device_attribute *attr, char *buf)
+{
+	struct i2c_client *client = to_i2c_client(dev);
+	struct bma150_data *bma150 = i2c_get_clientdata(client);
+
+	return sprintf(buf, "%d\n", bma150->mode);

API definition ?

drivers/input/misc/bma150.c line 478

Quoted 3 lines
drivers/input/misc/bma150.c new file +3 -0
+	schedule_delayed_work(&data->work,
+			msecs_to_jiffies(atomic_read(&data->delay)));
+

Why the atomic read ?

And there are a ton more issues unfixed in this driver

#2 lore raw
Quoted 4 lines · On May 31, 2011 18:23 "Alan Cox" <[email protected]> wrote:

See the driver I posted some time ago. It's in rather better state than this one and as it's been through most of the review process and also handles the BMA023 and SMB380 so I think would be a better place to start from. I'll report that driver in a moment.

I wasn't aware of that patch! Thanks for pointing it out! Regarding BMA023 and SMB380, these are also supported by this driver, which should probably have been stated in the Kconfig file.

Quoted 2 lines

Some of the first immediately obvious questions though - in particular the lack of clear locking ...

Thanks for reviewing!

Dimitry, I guess it's up to you to decide which version of the driver you prefer before we go through another round in the review process.

Best regards,
Eric

http://www.unixphere.com

#3 lore raw
In reply to Alan Cox

Hi Alan,

Once again, thanks for reviewing! See comments below.

Quoted 4 lines · Alan Cox

See the driver I posted some time ago. It's in rather better state than this one and as it's been through most of the review process and also handles the BMA023 and SMB380 so I think would be a better place to start from. I'll report that driver in a moment.

I have confirmed with Bosch Sensortec that BMA150, SMB380 and BMA023 are all fully compatible. BMA150 and SMB380 are the same chip and differs only in packaging.
However, the name BMA023 is not an official product name and there is no public datasheet available for this chip. Hence, the preferred name for this driver is BMA150 (bma150.c).
I will make sure to state in the Kconfig that this driver supports both BMA150 and SMB380 for the next version.

Quoted 7 lines

drivers/input/misc/bma150.c line 71

Quoted 5 lines
drivers/input/misc/bma150.c new file +5 -0
+struct bma150acc {
+	s16	x,
+		y,
+		z;
+};

Why does this need to be a struct ?

It doesn't, but I see it as increased readability. Actually, you have the same type of struct in the BMA023 driver you sent me. Anyways, I can remove it.

Quoted 6 lines

drivers/input/misc/bma150.c line 92

Quoted 4 lines
drivers/input/misc/bma150.c new file +4 -0
+	if ((mode != BMA150_MODE_NORMAL) &&
+	    (mode != BMA150_MODE_SLEEP) &&
+	    (mode != BMA150_MODE_WAKE_UP))
+		return -EINVAL;

How can any other mode get passed ?

They can't and shouldn't. See chapter 7 of the BMA150 datasheet.

Quoted 15 lines
Quoted 15 lines
drivers/input/misc/bma150.c new file +15 -0
+	data1 = i2c_smbus_read_byte_data(client, BMA150_WAKE_UP_REG);
+	if (data1 < 0)
+		return ret;
+
+	data1 = (data1 & ~BMA150_WAKE_UP_MSK) |
+		((mode << BMA150_WAKE_UP_POS) & BMA150_WAKE_UP_MSK);
+
+	data2 = i2c_smbus_read_byte_data(client, BMA150_SLEEP_REG);
+	if (data2 < 0)
+		return ret;
+
+	data2 = (data2 & ~BMA150_SLEEP_MSK) |
+		(((mode>>1) << BMA150_SLEEP_POS) & BMA150_SLEEP_MSK);
+
+	ret = i2c_smbus_write_byte_data(client, BMA150_WAKE_UP_REG, data1);
Quoted 8 lines

drivers/input/misc/bma150.c line 114

Quoted 6 lines
drivers/input/misc/bma150.c new file +6 -0
+	if (ret < 0)
+		return ret;
+
+	ret = i2c_smbus_write_byte_data(client, BMA150_SLEEP_REG, data2);
+	if (ret < 0)
+		return ret;

What locks this SMBUS transaction against others

True! I will look over the locking for the next version.

Quoted 9 lines

drivers/input/misc/bma150.c line 129

Quoted 7 lines
drivers/input/misc/bma150.c new file +7 -0
+static int bma150_set_range(struct i2c_client *client, unsigned char range)
+{
+	int ret;
+	unsigned char data;
+
+	if (range > BMA150_RANGE_8G)
+		return -EINVAL;

This should be actual values not a register range

It is an actual value. See chapter 3.1.2 in the BMA150 datasheet where the acceleration range values are defined.

Quoted 10 lines

drivers/input/misc/bma150.c line 138

Quoted 8 lines
drivers/input/misc/bma150.c new file +8 -0
+	data = i2c_smbus_read_byte_data(client, BMA150_RANGE_REG);
+	if (data < 0)
+		return ret;
+
+	data = (data & ~BMA150_RANGE_MSK) |
+		((range << BMA150_RANGE_POS) & BMA150_RANGE_MSK);
+
+	ret = i2c_smbus_write_byte_data(client, BMA150_RANGE_REG, data);

What locks this ?

I'll fix it!

Quoted 6 lines

drivers/input/misc/bma150.c line 119

Quoted 4 lines
drivers/input/misc/bma150.c new file +4 -0
+	if (ret < 0)
+		return ret;
+
+	return 0;

Why this pointless if ?

Oops! I will fix this!

Quoted 34 lines

drivers/input/misc/bma150.c line 155

Quoted 11 lines
drivers/input/misc/bma150.c new file +11 -0
+static int bma150_get_range(struct i2c_client *client, unsigned char *range)
+{
+	int ret;
+	unsigned char data;
+
+	data = i2c_smbus_read_byte_data(client, BMA150_RANGE_REG);
+	if (data < 0)
+		return ret;
+
+	*range = (data & BMA150_RANGE_MSK) >> BMA150_RANGE_POS;
+	return 0;

See comments above

drivers/input/misc/bma150.c line 158

Quoted 3 lines
drivers/input/misc/bma150.c new file +3 -0
+}
+
+static int bma150_set_bandwidth(struct i2c_client *client, unsigned char bw)

Similar problem

Quoted 13 lines
drivers/input/misc/bma150.c new file +13 -0
+static int bma150_read_accel_xyz(struct i2c_client *client,
+		struct bma150acc *acc)
+{
+	unsigned char data[6];
+	int ret = i2c_smbus_read_i2c_block_data(client,
+			BMA150_ACC_X_LSB_REG, 6, data);
+	if (ret != 6)
+		return -EIO;
+
+	acc->x = ((0xC0 & data[0]) >> 6) | (data[1] << 2);
+	acc->y = ((0xC0 & data[2]) >> 6) | (data[3] << 2);
+	acc->z = ((0xC0 & data[4]) >> 6) | (data[5] << 2);
+
Quoted 8 lines

drivers/input/misc/bma150.c line 211

Quoted 6 lines
drivers/input/misc/bma150.c new file +6 -0
+	/* sign extension */
+	acc->x = (s16) (acc->x << 6) >> 6;
+	acc->y = (s16) (acc->y << 6) >> 6;
+	acc->z = (s16) (acc->z << 6) >> 6;
+
+	return 0;

Why the separate function and struct

You are right! I will merge this with the worker function.

Quoted 6 lines

drivers/input/misc/bma150.c line 215

Quoted 4 lines
drivers/input/misc/bma150.c new file +4 -0
+}
+
+static void bma150_work_func(struct work_struct *work)
+{

Threaded IRQ ?

Actually no. The reason for this is that the interrupt is triggered every 333 us. This creates a pretty heavy load. By using delayed_work we can better control the sampling rate and have it dynamically configured by using sysfs.

Quoted 9 lines

drivers/input/misc/bma150.c line 244

Quoted 7 lines
drivers/input/misc/bma150.c new file +7 -0
+static ssize_t bma150_mode_show(struct device *dev,
+		struct device_attribute *attr, char *buf)
+{
+	struct i2c_client *client = to_i2c_client(dev);
+	struct bma150_data *bma150 = i2c_get_clientdata(client);
+
+	return sprintf(buf, "%d\n", bma150->mode);

API definition ?

Roger that! I will add it to the next version.

Quoted 6 lines

drivers/input/misc/bma150.c line 478

Quoted 4 lines
drivers/input/misc/bma150.c new file +3 -0
+	schedule_delayed_work(&data->work,
+			msecs_to_jiffies(atomic_read(&data->delay)));
+

Why the atomic read ?

Sure, I'll remove it.

Quoted 1 line

And there are a ton more issues unfixed in this driver

If you could be more precise I will be glad to fix any additional remarks!

I will send an updated version of the driver once I've fixed it!

Best regards,
Eric

http://www.unixphere.com

#4 lore raw
Quoted 2 lines
Quoted 1 line

What locks this SMBUS transaction against others

True! I will look over the locking for the next version.

I have a suspicion the two drivers are distant relatives actually - the bugs look similar !

Quoted 10 lines
Quoted 10 lines

drivers/input/misc/bma150.c line 129

Quoted 8 lines
drivers/input/misc/bma150.c new file +7 -0
+static int bma150_set_range(struct i2c_client *client, unsigned char range)
+{
+	int ret;
+	unsigned char data;
+
+	if (range > BMA150_RANGE_8G)
+		return -EINVAL;

This should be actual values not a register range

Quoted 2 lines

It is an actual value. See chapter 3.1.2 in the BMA150 datasheet where the acceleration range values are defined.

It should specify the units and be in meaningful ones not register values (I got moaned at for that in my submission too!)

Quoted 1 line

I will send an updated version of the driver once I've fixed it!

Cool - I really don't care btw which driver we end up with a mix of both, I just need it to work on our platform too.

Dunno where Dmitry has gone - has he vanished ?