Skip to contentSkip to conversation

Add a driver to support InvenSense mpu3050 gyroscope chip.

PATCH In review Alan Cox 5 participants 9 messages Apr 13, 2011

Subsystems (2)
lore
Share & tools
lore

Alan Cox

author
#0 lore raw

PATCH 1/1 Add a driver to support InvenSense mpu3050 gyroscope chip.

From: Joseph Lai <[email protected]>

This driver is registered as an input device. An IRQ is required in this basic driver configuration.

[Cleaned up PM_RUNTIME defines]

---

 drivers/input/misc/Kconfig   |   10 +
 drivers/input/misc/Makefile  |    1 
 drivers/input/misc/mpu3050.c |  422 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 433 insertions(+), 0 deletions(-)
 create mode 100644 drivers/input/misc/mpu3050.c
drivers/input/misc/Kconfig +10 -0
index 406a730..4f403ad 100644
@@ -477,4 +477,14 @@ config INPUT_BMA023
 	  To compile this driver as a module, choose M here: the
 	  module will be called bma023.
 
+config INPUT_MPU3050
+	tristate "MPU3050 Triaxial gyroscope sensor"
+	depends on I2C
+	help
+	  Say Y here if you want to support InvenSense MPU3050
+	  connected via an I2C bus.
+
+	  To compile this driver as a module, choose M here: the
+	  module will be called mpu3050.
+
 endif
drivers/input/misc/Makefile +1 -0
index b7100c2..1cc2132 100644
@@ -28,6 +28,7 @@ obj-$(CONFIG_INPUT_IXP4XX_BEEPER)	+= ixp4xx-beeper.o
 obj-$(CONFIG_INPUT_KEYSPAN_REMOTE)	+= keyspan_remote.o
 obj-$(CONFIG_INPUT_M68K_BEEP)		+= m68kspkr.o
 obj-$(CONFIG_INPUT_MAX8925_ONKEY)	+= max8925_onkey.o
+obj-$(CONFIG_INPUT_MPU3050)		+= mpu3050.o
 obj-$(CONFIG_INPUT_PCAP)		+= pcap_keys.o
 obj-$(CONFIG_INPUT_PCF50633_PMU)	+= pcf50633-input.o
 obj-$(CONFIG_INPUT_PCF8574)		+= pcf8574_keypad.o
drivers/input/misc/mpu3050.c new file +422 -0

422 lines not on this page · raw message

#1 lore raw
Quoted 4 lines · Alan Cox

From: Joseph Lai <[email protected]>

This driver is registered as an input device. An IRQ is required in this basic driver configuration.

For others, description of changes is at the top of mpu3050.c Basically, driver has been stripped of sysfs interfaces entirely to aid merging.

Couple of trivial nitpicks/comments inline.

...

Quoted 5 lines
drivers/input/misc/mpu3050.c new file +5 -0
+struct mpu3050_sensor {
+	struct i2c_client *client;
+	struct device *dev;
+	struct input_dev *idev;
+	struct mutex lock;

drivers/input/misc/mpu3050.c line 76

Quoted 10 lines
drivers/input/misc/mpu3050.c new file +10 -0
+};
+
+/**
+ *	mpu3050_xyz_read_reg	-	read the axes values
+ *	@buffer: provide register addr and get register
+ *	@length: length of register
+ *
+ *	Reads the register values in one transaction or returns a negative
+ *	error code on failure/
+ */

Could just fix the length in this. You only ever use it with the value 6. Hardly seems worth it's own function. I guess this may be because it gets used for other things in the full version? If so it doesn't do any harm here so might as well leave it alone.

Quoted 25 lines
drivers/input/misc/mpu3050.c new file +25 -0
+static int mpu3050_xyz_read_reg(struct i2c_client *client,
+			       u8 *buffer, int length)
+{
+	struct i2c_msg msg[] = {
+		{
+			.addr = client->addr,
+			.flags = 0,
+			.len = 1,
+			.buf = buffer,
+		},
+		{
+			.addr = client->addr,
+			.flags = I2C_M_RD,
+			.len = length,
+			.buf = buffer,
+		},
+	};
+	return i2c_transfer(client->adapter, msg, 2);
+}
+
+/**
+ *	mpu3050_read_xyz	-	get co-ordinates from device
+ *	@client: i2c address of sensor
+ *	@coords: co-ordinates to update
+ *

drivers/input/misc/mpu3050.c line 111

Quoted 10 lines
drivers/input/misc/mpu3050.c new file +10 -0
+ *	Return the converted X Y and Z co-ordinates from the sensor device
+ */
+static void mpu3050_read_xyz(struct i2c_client *client,
+				struct axis_data *coords)
+{
+	u8 buffer[6];
+	buffer[0] = MPU3050_XOUT_H;
+	mpu3050_xyz_read_reg(client, buffer, 6);
+	coords->x = buffer[0];
+	coords->x = coords->x << 8 | buffer[1];

Better to use le16_tocpu or similar rather than
hand rolling these.

Quoted 8 lines
drivers/input/misc/mpu3050.c new file +8 -0
+	coords->y = buffer[2];
+	coords->y = coords->y << 8 | buffer[3];
+	coords->z = buffer[4];
+	coords->z = coords->z << 8 | buffer[5];
+	dev_dbg(&client->dev, "%s: x %d, y %d, z %d\n", __func__,
+					coords->x, coords->y, coords->z);
+}
+

...

#2 lore raw
Quoted 4 lines

Could just fix the length in this. You only ever use it with the value 6. Hardly seems worth it's own function. I guess this may be because it gets used for other things in the full version? If so it doesn't do any harm here so might as well leave it alone.

In its current form, but there may well be future reasons for people to update that depending upon their application of the device.

Quoted 8 lines

drivers/input/misc/mpu3050.c line 111

Quoted 5 lines
drivers/input/misc/mpu3050.c new file +5 -0
+	u8 buffer[6];
+	buffer[0] = MPU3050_XOUT_H;
+	mpu3050_xyz_read_reg(client, buffer, 6);
+	coords->x = buffer[0];
+	coords->x = coords->x << 8 | buffer[1];

Better to use le16_tocpu or similar rather than
hand rolling these.

le16_to_cpu takes an u16 or similar input which means buffer then has to be a u16[] array for alignment which means you need a separate input and output buffer or to do uglies setting XOUT_H

(Normally I'd agree with you but in this case I'm unconvinced)

Alan

#3 lore raw
Quoted 7 lines · Alan Cox
Quoted 4 lines

Could just fix the length in this. You only ever use it with the value 6. Hardly seems worth it's own function. I guess this may be because it gets used for other things in the full version? If so it doesn't do any harm here so might as well leave it alone.

In its current form, but there may well be future reasons for people to update that depending upon their application of the device.

It's conceivable, but seems to me that calling a function called xyz_read_reg to do anything other than ready x y and z would be somewhat odd.

Quoted 10 lines
Quoted 9 lines

drivers/input/misc/mpu3050.c line 111

Quoted 6 lines
drivers/input/misc/mpu3050.c new file +5 -0
+	u8 buffer[6];
+	buffer[0] = MPU3050_XOUT_H;
+	mpu3050_xyz_read_reg(client, buffer, 6);
+	coords->x = buffer[0];
+	coords->x = coords->x << 8 | buffer[1];

Better to use le16_tocpu or similar rather than
hand rolling these.

Quoted 3 lines

le16_to_cpu takes an u16 or similar input which means buffer then has to be a u16[] array for alignment which means you need a separate input and output buffer or to do uglies setting XOUT_H

Given the specific nature of that read you could just have an appropriate u8 inside the function. Would to my mind give more readable code, particularly as you could indeed then have a u16 array. Could just mark the u8 array as __attribute((aligned(2))) (I think) as a simpler alternative.

Oops, I can think of a few cases in my driver where I haven't enforced that... Time for some fixes...

Quoted 4 lines

(Normally I'd agree with you but in this case I'm unconvinced)

Alan

#4 lore raw
In reply to Alan Cox

Alan Cox wrote on 2011-04-13:

Quoted 14 lines
Quoted 4 lines

Could just fix the length in this. You only ever use it with the value
6. Hardly seems worth it's own function. I guess this may be because it gets used for other things in the full version? If so it doesn't do any harm here so might as well leave it alone.

In its current form, but there may well be future reasons for people to update that depending upon their application of the device.

Quoted 6 lines
Quoted 5 lines
drivers/input/misc/mpu3050.c new file +5 -0
+   u8 buffer[6];
+   buffer[0] = MPU3050_XOUT_H;
+   mpu3050_xyz_read_reg(client, buffer, 6);
+   coords->x = buffer[0];
+   coords->x = coords->x << 8 | buffer[1];
Quoted 5 lines
Quoted 1 line

Better to use le16_tocpu or similar rather than hand rolling these.

le16_to_cpu takes an u16 or similar input which means buffer then has to be a u16[] array for alignment which means you need a separate input and output buffer or to do uglies setting XOUT_H

How about get_unaligned_be16(&buffer[0]) ?

Quoted 3 lines

(Normally I'd agree with you but in this case I'm unconvinced)

Alan

Greetings,
Michael

--
Analog Devices GmbH Wilhelm-Wagenfeld-Str. 6 80807 Muenchen
Sitz der Gesellschaft: Muenchen; Registergericht: Muenchen HRB 40368; Geschaeftsfuehrer:Dr.Carsten Suckrow, Thomas Wessel, William A. Martin, Margaret Seif

#6 lore raw
In reply to Alan Cox

Hi Alan,

Quoted 1 line

+#define MPU3050_XOUT_H 0x1D

Is this via the FIFO mentioned in the product brief, or directly to the sensor register. A quick look for a datasheet didn't find a I2C register listing...

Quoted 9 lines
drivers/input/misc/mpu3050.c new file +9 -0
+static int mpu3050_xyz_read_reg(struct i2c_client *client,
+			       u8 *buffer, int length)
+{
+	struct i2c_msg msg[] = {
+		{
+			.addr = client->addr,
+			.flags = 0,
+			.len = 1,
+			.buf = buffer,

drivers/input/misc/mpu3050.c line 95

Quoted 10 lines
drivers/input/misc/mpu3050.c new file +10 -0
+		},
+		{
+			.addr = client->addr,
+			.flags = I2C_M_RD,
+			.len = length,
+			.buf = buffer,
+		},
+	};
+	return i2c_transfer(client->adapter, msg, 2);
+}

This assumes a number of things, mainly that the interrupt latency is fast enough that the 'NEXT' measurement does not overwrite the 'CURRENT' sample whilst it is being read. This could lead to corrupt values.

Without a detailed datasheet for the MPU3050 I can't say for certain; but usually there is another register which is set when a measurement is taken. This can be cleared before and accessed after reading the XYZ values to confirm that they have not been corrupted.

Also some I2C hosts (such as the Intel SCH) don't support I2C block read/writes, and thus the access will be broken into separate reads/writes and be much slower.

Hope I'm not speaking out of turn,
Simon.

#5 lore raw
In reply to Hennerich, Michael
Quoted 15 lines · Michael

Alan Cox wrote on 2011-04-13:

Quoted 14 lines
Quoted 4 lines

Could just fix the length in this. You only ever use it with the value 6. Hardly seems worth it's own function. I guess this may be because it gets used for other things in the full version? If so it doesn't do any harm here so might as well leave it alone.

In its current form, but there may well be future reasons for people to update that depending upon their application of the device.

Quoted 6 lines
Quoted 5 lines
drivers/input/misc/mpu3050.c new file +5 -0
+   u8 buffer[6];
+   buffer[0] = MPU3050_XOUT_H;
+   mpu3050_xyz_read_reg(client, buffer, 6);
+   coords->x = buffer[0];
+   coords->x = coords->x << 8 | buffer[1];
Quoted 7 lines
Quoted 5 lines
Quoted 1 line

Better to use le16_tocpu or similar rather than hand rolling these.

le16_to_cpu takes an u16 or similar input which means buffer then has to be a u16[] array for alignment which means you need a separate input and output buffer or to do uglies setting XOUT_H

How about get_unaligned_be16(&buffer[0]) ?

I never knew about that - yes that looks ideal.

New patch to follow shortly with some of this tidied up as per Jonathans suggestion too

#7 lore raw
In reply to [email protected]
Quoted 4 lines

Without a detailed datasheet for the MPU3050 I can't say for certain; but usually there is another register which is set when a measurement is taken. This can be cleared before and accessed after reading the XYZ values to confirm that they have not been corrupted.

I don't believe the datasheet for the MPU3050 is public, but it's a smart device rather than a simple interface.

Quoted 3 lines

Also some I2C hosts (such as the Intel SCH) don't support I2C block read/writes, and thus the access will be broken into separate reads/writes and be much slower.

Yes - but for those that can do it in one, it's a win to do so.

Quoted 1 line

Hope I'm not speaking out of turn

Nope - all good points to double check.

Alan

#8 lore raw
Quoted 7 lines
Quoted 4 lines

Without a detailed datasheet for the MPU3050 I can't say for certain; but usually there is another register which is set when a measurement is taken. This can be cleared before and accessed after reading the XYZ values to confirm that they have not been corrupted.

I don't believe the datasheet for the MPU3050 is public, but it's a smart device rather than a simple interface.

So are you confirming that the XYZ values can't be corrupted? Even if the sample rate is set to max...

Maybe a note so others don't copy the driver format to implement 'dumb' devices.

Simon