linux-input.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/3] mpu3050 updates
@ 2011-12-15 22:18 Alan Cox
  2011-12-15 22:18 ` [PATCH 1/3] input: mpu3050: Support for polled input device Alan Cox
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: Alan Cox @ 2011-12-15 22:18 UTC (permalink / raw)
  To: dmitry.torokhov, linux-input

Support polling mode, sampling configuration and make sure we are robustly
enabling interrupts when needed
---

Heikki Krogerus (3):
      input: mpu3050: Ensure we enable interrupts
      input: mpu3050: Configure the sampling method
      input: mpu3050: Support for polled input device


 drivers/input/misc/mpu3050.c |  296 +++++++++++++++++++++++++++++++++++-------
 1 files changed, 244 insertions(+), 52 deletions(-)


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH 1/3] input: mpu3050: Support for polled input device
  2011-12-15 22:18 [PATCH 0/3] mpu3050 updates Alan Cox
@ 2011-12-15 22:18 ` Alan Cox
  2011-12-16 10:52   ` Shubhrajyoti
  2011-12-28  5:07   ` Dmitry Torokhov
  2011-12-15 22:18 ` [PATCH 2/3] input: mpu3050: Configure the sampling method Alan Cox
  2011-12-15 22:18 ` [PATCH 3/3] input: mpu3050: Ensure we enable interrupts Alan Cox
  2 siblings, 2 replies; 8+ messages in thread
From: Alan Cox @ 2011-12-15 22:18 UTC (permalink / raw)
  To: dmitry.torokhov, linux-input

From: Heikki Krogerus <heikki.krogerus@linux.intel.com>

The driver does not need to depend on interrupts.

Signed-off-by: Heikki Krogerus <heikki.krogerus@linux.intel.com>
Signed-off-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
Signed-off-by: Alan Cox <alan@linux.intel.com>
---

 drivers/input/misc/mpu3050.c |  189 +++++++++++++++++++++++++++++++-----------
 1 files changed, 141 insertions(+), 48 deletions(-)


diff --git a/drivers/input/misc/mpu3050.c b/drivers/input/misc/mpu3050.c
index f71dc72..da47b10 100644
--- a/drivers/input/misc/mpu3050.c
+++ b/drivers/input/misc/mpu3050.c
@@ -37,6 +37,7 @@
 #include <linux/err.h>
 #include <linux/i2c.h>
 #include <linux/input.h>
+#include <linux/input-polldev.h>
 #include <linux/delay.h>
 #include <linux/slab.h>
 #include <linux/pm_runtime.h>
@@ -53,6 +54,8 @@
 #define MPU3050_MIN_VALUE	-32768
 #define MPU3050_MAX_VALUE	32767
 
+#define MPU3050_DEFAULT_POLL_INTERVAL	200
+
 struct axis_data {
 	s16 x;
 	s16 y;
@@ -63,6 +66,7 @@ struct mpu3050_sensor {
 	struct i2c_client *client;
 	struct device *dev;
 	struct input_dev *idev;
+	struct input_polled_dev *input_polled;
 };
 
 /**
@@ -168,6 +172,38 @@ static void mpu3050_input_close(struct input_dev *input)
 	pm_runtime_put(sensor->dev);
 }
 
+static void mpu3050_poll_open(struct input_polled_dev *ipoll_dev)
+{
+	struct mpu3050_sensor *sensor = ipoll_dev->private;
+
+	mpu3050_set_power_mode(sensor->client, 1);
+	msleep(100);  /* wait for gyro chip resume */
+}
+
+static void mpu3050_poll_close(struct input_polled_dev *ipoll_dev)
+{
+	struct mpu3050_sensor *sensor = ipoll_dev->private;
+
+	mpu3050_set_power_mode(sensor->client, 1);
+}
+
+static void mpu3050_report_xyz(struct mpu3050_sensor *sensor)
+{
+	struct axis_data axis;
+
+	mpu3050_read_xyz(sensor->client, &axis);
+
+	input_report_abs(sensor->idev, ABS_X, axis.x);
+	input_report_abs(sensor->idev, ABS_Y, axis.y);
+	input_report_abs(sensor->idev, ABS_Z, axis.z);
+	input_sync(sensor->idev);
+}
+
+static void mpu3050_poll(struct input_polled_dev *dev)
+{
+	mpu3050_report_xyz(dev->private);
+}
+
 /**
  *	mpu3050_interrupt_thread	-	handle an IRQ
  *	@irq: interrupt numner
@@ -178,17 +214,82 @@ static void mpu3050_input_close(struct input_dev *input)
  */
 static irqreturn_t mpu3050_interrupt_thread(int irq, void *data)
 {
-	struct mpu3050_sensor *sensor = data;
-	struct axis_data axis;
+	mpu3050_report_xyz(data);
 
-	mpu3050_read_xyz(sensor->client, &axis);
+	return IRQ_HANDLED;
+}
 
-	input_report_abs(sensor->idev, ABS_X, axis.x);
-	input_report_abs(sensor->idev, ABS_Y, axis.y);
-	input_report_abs(sensor->idev, ABS_Z, axis.z);
-	input_sync(sensor->idev);
+static void __devinit mpu3050_init_idev(struct mpu3050_sensor *sensor,
+						struct input_dev *idev)
+{
+	idev->name = "MPU3050";
+	idev->id.bustype = BUS_I2C;
+	idev->dev.parent = &sensor->client->dev;
 
-	return IRQ_HANDLED;
+	__set_bit(EV_ABS, idev->evbit);
+	input_set_abs_params(idev, ABS_X,
+			     MPU3050_MIN_VALUE, MPU3050_MAX_VALUE, 0, 0);
+	input_set_abs_params(idev, ABS_Y,
+			     MPU3050_MIN_VALUE, MPU3050_MAX_VALUE, 0, 0);
+	input_set_abs_params(idev, ABS_Z,
+			     MPU3050_MIN_VALUE, MPU3050_MAX_VALUE, 0, 0);
+}
+
+static int __devinit mpu3050_create_idev(struct mpu3050_sensor *sensor)
+{
+	struct input_dev *idev;
+	int err;
+
+	idev = input_allocate_device();
+	if (!idev)
+		return -ENODEV;
+
+	mpu3050_init_idev(sensor, idev);
+
+	idev->open = mpu3050_input_open;
+	idev->close = mpu3050_input_close;
+	input_set_drvdata(idev, sensor);
+
+	err = input_register_device(idev);
+	if (err) {
+		input_free_device(idev);
+		return err;
+	}
+
+	sensor->idev = idev;
+
+	return 0;
+}
+
+static int __devinit mpu3050_create_polled_idev(struct mpu3050_sensor *sensor)
+{
+	struct input_polled_dev *ipoll_dev;
+	int err;
+
+	ipoll_dev = input_allocate_polled_device();
+	if (!ipoll_dev)
+		return -ENOMEM;
+
+	ipoll_dev->private = sensor;
+	ipoll_dev->open = mpu3050_poll_open;
+	ipoll_dev->close = mpu3050_poll_close;
+	ipoll_dev->poll = mpu3050_poll;
+	ipoll_dev->poll_interval = MPU3050_DEFAULT_POLL_INTERVAL;
+	ipoll_dev->poll_interval_min = 10;
+	ipoll_dev->poll_interval_max = MPU3050_DEFAULT_POLL_INTERVAL;
+
+	mpu3050_init_idev(sensor, ipoll_dev->input);
+
+	err = input_register_polled_device(ipoll_dev);
+	if (err) {
+		input_free_polled_device(ipoll_dev);
+		return err;
+	}
+
+	sensor->input_polled = ipoll_dev;
+	sensor->idev = ipoll_dev->input;
+
+	return 0;
 }
 
 /**
@@ -205,21 +306,17 @@ static int __devinit mpu3050_probe(struct i2c_client *client,
 				   const struct i2c_device_id *id)
 {
 	struct mpu3050_sensor *sensor;
-	struct input_dev *idev;
 	int ret;
 	int error;
 
 	sensor = kzalloc(sizeof(struct mpu3050_sensor), GFP_KERNEL);
-	idev = input_allocate_device();
-	if (!sensor || !idev) {
+	if (!sensor) {
 		dev_err(&client->dev, "failed to allocate driver data\n");
-		error = -ENOMEM;
-		goto err_free_mem;
+		return -ENOMEM;
 	}
 
 	sensor->client = client;
 	sensor->dev = &client->dev;
-	sensor->idev = idev;
 
 	mpu3050_set_power_mode(client, 1);
 	msleep(10);
@@ -237,39 +334,31 @@ static int __devinit mpu3050_probe(struct i2c_client *client,
 		goto err_free_mem;
 	}
 
-	idev->name = "MPU3050";
-	idev->id.bustype = BUS_I2C;
-	idev->dev.parent = &client->dev;
-
-	idev->open = mpu3050_input_open;
-	idev->close = mpu3050_input_close;
-
-	__set_bit(EV_ABS, idev->evbit);
-	input_set_abs_params(idev, ABS_X,
-			     MPU3050_MIN_VALUE, MPU3050_MAX_VALUE, 0, 0);
-	input_set_abs_params(idev, ABS_Y,
-			     MPU3050_MIN_VALUE, MPU3050_MAX_VALUE, 0, 0);
-	input_set_abs_params(idev, ABS_Z,
-			     MPU3050_MIN_VALUE, MPU3050_MAX_VALUE, 0, 0);
-
-	input_set_drvdata(idev, sensor);
-
 	pm_runtime_set_active(&client->dev);
 
-	error = request_threaded_irq(client->irq,
-				     NULL, mpu3050_interrupt_thread,
-				     IRQF_TRIGGER_RISING,
-				     "mpu_int", sensor);
-	if (error) {
-		dev_err(&client->dev,
-			"can't get IRQ %d, error %d\n", client->irq, error);
-		goto err_pm_set_suspended;
-	}
-
-	error = input_register_device(idev);
-	if (error) {
-		dev_err(&client->dev, "failed to register input device\n");
-		goto err_free_irq;
+	if (client->irq > 0) {
+		error = mpu3050_create_idev(sensor);
+		if (error) {
+			dev_err(&client->dev, "failed to register input device\n");
+			goto err_free_irq;
+		}
+
+		error = request_threaded_irq(client->irq,
+				NULL, mpu3050_interrupt_thread,
+				IRQF_TRIGGER_RISING,
+				"mpu_int", sensor);
+		if (error) {
+			dev_err(&client->dev, "can't get IRQ %d, error %d\n",
+					client->irq, error);
+			goto err_pm_set_suspended;
+		}
+	} else {
+		error = mpu3050_create_polled_idev(sensor);
+		if (error) {
+			dev_err(&client->dev,
+				"failed to register polled input device");
+			goto err_pm_set_suspended;
+		}
 	}
 
 	pm_runtime_enable(&client->dev);
@@ -282,7 +371,6 @@ err_free_irq:
 err_pm_set_suspended:
 	pm_runtime_set_suspended(&client->dev);
 err_free_mem:
-	input_free_device(idev);
 	kfree(sensor);
 	return error;
 }
@@ -300,8 +388,13 @@ static int __devexit mpu3050_remove(struct i2c_client *client)
 	pm_runtime_disable(&client->dev);
 	pm_runtime_set_suspended(&client->dev);
 
-	free_irq(client->irq, sensor);
-	input_unregister_device(sensor->idev);
+	if (client->irq > 0) {
+		free_irq(client->irq, sensor);
+		input_unregister_device(sensor->idev);
+	} else {
+		input_unregister_polled_device(sensor->input_polled);
+		input_free_polled_device(sensor->input_polled);
+	}
 	kfree(sensor);
 
 	return 0;


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH 2/3] input: mpu3050: Configure the sampling method
  2011-12-15 22:18 [PATCH 0/3] mpu3050 updates Alan Cox
  2011-12-15 22:18 ` [PATCH 1/3] input: mpu3050: Support for polled input device Alan Cox
@ 2011-12-15 22:18 ` Alan Cox
  2011-12-24  8:07   ` Dmitry Torokhov
  2011-12-15 22:18 ` [PATCH 3/3] input: mpu3050: Ensure we enable interrupts Alan Cox
  2 siblings, 1 reply; 8+ messages in thread
From: Alan Cox @ 2011-12-15 22:18 UTC (permalink / raw)
  To: dmitry.torokhov, linux-input

From: Heikki Krogerus <heikki.krogerus@linux.intel.com>

This will improve the output of the sensor.

Signed-off-by: Heikki Krogerus <heikki.krogerus@linux.intel.com>
Signed-off-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
Signed-off-by: Alan Cox <alan@linux.intel.com>
---

 drivers/input/misc/mpu3050.c |  101 ++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 96 insertions(+), 5 deletions(-)


diff --git a/drivers/input/misc/mpu3050.c b/drivers/input/misc/mpu3050.c
index da47b10..89c7160 100644
--- a/drivers/input/misc/mpu3050.c
+++ b/drivers/input/misc/mpu3050.c
@@ -42,12 +42,7 @@
 #include <linux/slab.h>
 #include <linux/pm_runtime.h>
 
-#define MPU3050_CHIP_ID_REG	0x00
 #define MPU3050_CHIP_ID		0x69
-#define MPU3050_XOUT_H		0x1D
-#define MPU3050_PWR_MGM		0x3E
-#define MPU3050_PWR_MGM_POS	6
-#define MPU3050_PWR_MGM_MASK	0x40
 
 #define MPU3050_AUTO_DELAY	1000
 
@@ -55,6 +50,58 @@
 #define MPU3050_MAX_VALUE	32767
 
 #define MPU3050_DEFAULT_POLL_INTERVAL	200
+#define MPU3050_DEFAULT_FS_RANGE	3
+
+/* Register map */
+#define MPU3050_CHIP_ID_REG	0x00
+#define MPU3050_SMPLRT_DIV	0x15
+#define MPU3050_DLPF_FS_SYNC	0x16
+#define MPU3050_INT_CFG		0x17
+#define MPU3050_XOUT_H		0x1D
+#define MPU3050_PWR_MGM		0x3E
+#define MPU3050_PWR_MGM_POS	6
+
+/* Register bits */
+
+/* DLPF_FS_SYNC */
+#define MPU3050_EXT_SYNC_NONE		0x00
+#define MPU3050_EXT_SYNC_TEMP		0x20
+#define MPU3050_EXT_SYNC_GYROX		0x40
+#define MPU3050_EXT_SYNC_GYROY		0x60
+#define MPU3050_EXT_SYNC_GYROZ		0x80
+#define MPU3050_EXT_SYNC_ACCELX		0xA0
+#define MPU3050_EXT_SYNC_ACCELY		0xC0
+#define MPU3050_EXT_SYNC_ACCELZ		0xE0
+#define MPU3050_EXT_SYNC_MASK		0xE0
+#define MPU3050_FS_250DPS		0x00
+#define MPU3050_FS_500DPS		0x08
+#define MPU3050_FS_1000DPS		0x10
+#define MPU3050_FS_2000DPS		0x18
+#define MPU3050_FS_MASK			0x18
+#define MPU3050_DLPF_CFG_256HZ_NOLPF2	0x00
+#define MPU3050_DLPF_CFG_188HZ		0x01
+#define MPU3050_DLPF_CFG_98HZ		0x02
+#define MPU3050_DLPF_CFG_42HZ		0x03
+#define MPU3050_DLPF_CFG_20HZ		0x04
+#define MPU3050_DLPF_CFG_10HZ		0x05
+#define MPU3050_DLPF_CFG_5HZ		0x06
+#define MPU3050_DLPF_CFG_2100HZ_NOLPF	0x07
+#define MPU3050_DLPF_CFG_MASK		0x07
+/* INT_CFG */
+#define MPU3050_RAW_RDY_EN		0x01
+#define MPU3050_MPU_RDY_EN		0x02
+#define MPU3050_LATCH_INT_EN		0x04
+/* PWR_MGM */
+#define MPU3050_PWR_MGM_PLL_X		0x01
+#define MPU3050_PWR_MGM_PLL_Y		0x02
+#define MPU3050_PWR_MGM_PLL_Z		0x03
+#define MPU3050_PWR_MGM_CLKSEL		0x07
+#define MPU3050_PWR_MGM_STBY_ZG		0x08
+#define MPU3050_PWR_MGM_STBY_YG		0x10
+#define MPU3050_PWR_MGM_STBY_XG		0x20
+#define MPU3050_PWR_MGM_SLEEP		0x40
+#define MPU3050_PWR_MGM_RESET		0x80
+#define MPU3050_PWR_MGM_MASK		0x40
 
 struct axis_data {
 	s16 x;
@@ -219,6 +266,46 @@ static irqreturn_t mpu3050_interrupt_thread(int irq, void *data)
 	return IRQ_HANDLED;
 }
 
+/* Configures the sampling method */
+static int mpu3050_hw_init(struct mpu3050_sensor *sensor)
+{
+	struct i2c_client *client = sensor->client;
+	int ret;
+	u8 reg;
+
+	/* Reset */
+	ret = i2c_smbus_write_byte_data(client, MPU3050_PWR_MGM,
+					MPU3050_PWR_MGM_RESET);
+	if (ret < 0)
+		return ret;
+
+	ret = i2c_smbus_read_byte_data(client, MPU3050_PWR_MGM);
+	if (ret < 0)
+		return ret;
+
+	ret &= ~MPU3050_PWR_MGM_CLKSEL;
+	ret |= MPU3050_PWR_MGM_PLL_Z;
+	ret = i2c_smbus_write_byte_data(client, MPU3050_PWR_MGM, ret);
+	if (ret < 0)
+		return ret;
+
+	/* Output frequency divider. The poll interval */
+	ret = i2c_smbus_write_byte_data(client, MPU3050_SMPLRT_DIV,
+					MPU3050_DEFAULT_POLL_INTERVAL - 1);
+	if (ret < 0)
+		return ret;
+
+	/* Set low pass filter and full scale */
+	reg = MPU3050_DEFAULT_FS_RANGE;
+	reg |= MPU3050_DLPF_CFG_42HZ << 3;
+	reg |= MPU3050_EXT_SYNC_NONE << 5;
+	ret = i2c_smbus_write_byte_data(client, MPU3050_DLPF_FS_SYNC, reg);
+	if (ret < 0)
+		return ret;
+
+	return 0;
+}
+
 static void __devinit mpu3050_init_idev(struct mpu3050_sensor *sensor,
 						struct input_dev *idev)
 {
@@ -336,6 +423,10 @@ static int __devinit mpu3050_probe(struct i2c_client *client,
 
 	pm_runtime_set_active(&client->dev);
 
+	error = mpu3050_hw_init(sensor);
+	if (error)
+		goto err_free_mem;
+
 	if (client->irq > 0) {
 		error = mpu3050_create_idev(sensor);
 		if (error) {


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH 3/3] input: mpu3050: Ensure we enable interrupts
  2011-12-15 22:18 [PATCH 0/3] mpu3050 updates Alan Cox
  2011-12-15 22:18 ` [PATCH 1/3] input: mpu3050: Support for polled input device Alan Cox
  2011-12-15 22:18 ` [PATCH 2/3] input: mpu3050: Configure the sampling method Alan Cox
@ 2011-12-15 22:18 ` Alan Cox
  2011-12-24  8:09   ` Dmitry Torokhov
  2 siblings, 1 reply; 8+ messages in thread
From: Alan Cox @ 2011-12-15 22:18 UTC (permalink / raw)
  To: dmitry.torokhov, linux-input

From: Heikki Krogerus <heikki.krogerus@linux.intel.com>

This also changes the devname parameter delivered to
request_threaded_irq() from "mpu_int" to "mpu3050".

Signed-off-by: Heikki Krogerus <heikki.krogerus@linux.intel.com>
Signed-off-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
Signed-off-by: Alan Cox <alan@linux.intel.com>
---

 drivers/input/misc/mpu3050.c |   10 +++++++++-
 1 files changed, 9 insertions(+), 1 deletions(-)


diff --git a/drivers/input/misc/mpu3050.c b/drivers/input/misc/mpu3050.c
index 89c7160..cf42c78 100644
--- a/drivers/input/misc/mpu3050.c
+++ b/drivers/input/misc/mpu3050.c
@@ -437,12 +437,20 @@ static int __devinit mpu3050_probe(struct i2c_client *client,
 		error = request_threaded_irq(client->irq,
 				NULL, mpu3050_interrupt_thread,
 				IRQF_TRIGGER_RISING,
-				"mpu_int", sensor);
+				"mpu3050", sensor);
 		if (error) {
 			dev_err(&client->dev, "can't get IRQ %d, error %d\n",
 					client->irq, error);
 			goto err_pm_set_suspended;
 		}
+
+		/* Enable interrupts */
+		i2c_smbus_write_byte_data(sensor->client, MPU3050_INT_CFG,
+					MPU3050_LATCH_INT_EN
+					| MPU3050_RAW_RDY_EN
+					| MPU3050_MPU_RDY_EN);
+		if (ret < 0)
+			goto err_free_irq;
 	} else {
 		error = mpu3050_create_polled_idev(sensor);
 		if (error) {


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [PATCH 1/3] input: mpu3050: Support for polled input device
  2011-12-15 22:18 ` [PATCH 1/3] input: mpu3050: Support for polled input device Alan Cox
@ 2011-12-16 10:52   ` Shubhrajyoti
  2011-12-28  5:07   ` Dmitry Torokhov
  1 sibling, 0 replies; 8+ messages in thread
From: Shubhrajyoti @ 2011-12-16 10:52 UTC (permalink / raw)
  To: Alan Cox; +Cc: dmitry.torokhov, linux-input

Hi Some ,
Minor comments,

On Friday 16 December 2011 03:48 AM, Alan Cox wrote:
> From: Heikki Krogerus <heikki.krogerus@linux.intel.com>
>
> The driver does not need to depend on interrupts.
>
> Signed-off-by: Heikki Krogerus <heikki.krogerus@linux.intel.com>
> Signed-off-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
> Signed-off-by: Alan Cox <alan@linux.intel.com>
> ---
>
>  drivers/input/misc/mpu3050.c |  189 +++++++++++++++++++++++++++++++-----------
>  1 files changed, 141 insertions(+), 48 deletions(-)
>
>
> diff --git a/drivers/input/misc/mpu3050.c b/drivers/input/misc/mpu3050.c
> index f71dc72..da47b10 100644
> --- a/drivers/input/misc/mpu3050.c
> +++ b/drivers/input/misc/mpu3050.c
> @@ -37,6 +37,7 @@
>  #include <linux/err.h>
>  #include <linux/i2c.h>
>  #include <linux/input.h>
> +#include <linux/input-polldev.h>
>  #include <linux/delay.h>
>  #include <linux/slab.h>
>  #include <linux/pm_runtime.h>
> @@ -53,6 +54,8 @@
>  #define MPU3050_MIN_VALUE	-32768
>  #define MPU3050_MAX_VALUE	32767
>  
> +#define MPU3050_DEFAULT_POLL_INTERVAL	200
> +
>  struct axis_data {
>  	s16 x;
>  	s16 y;
> @@ -63,6 +66,7 @@ struct mpu3050_sensor {
>  	struct i2c_client *client;
>  	struct device *dev;
>  	struct input_dev *idev;
> +	struct input_polled_dev *input_polled;
>  };
>  
>  /**
> @@ -168,6 +172,38 @@ static void mpu3050_input_close(struct input_dev *input)
>  	pm_runtime_put(sensor->dev);
>  }
>  
> +static void mpu3050_poll_open(struct input_polled_dev *ipoll_dev)
> +{
> +	struct mpu3050_sensor *sensor = ipoll_dev->private;
> +
> +	mpu3050_set_power_mode(sensor->client, 1);
> +	msleep(100);  /* wait for gyro chip resume */
Could this be a macro.
> +}
> +
> +static void mpu3050_poll_close(struct input_polled_dev *ipoll_dev)
> +{
> +	struct mpu3050_sensor *sensor = ipoll_dev->private;
> +
> +	mpu3050_set_power_mode(sensor->client, 1);
What does this function do? did you intend to write 1?
> +}
> +
> +static void mpu3050_report_xyz(struct mpu3050_sensor *sensor)
> +{
> +	struct axis_data axis;
> +
> +	mpu3050_read_xyz(sensor->client, &axis);
> +
> +	input_report_abs(sensor->idev, ABS_X, axis.x);
> +	input_report_abs(sensor->idev, ABS_Y, axis.y);
> +	input_report_abs(sensor->idev, ABS_Z, axis.z);
> +	input_sync(sensor->idev);
> +}
> +
> +static void mpu3050_poll(struct input_polled_dev *dev)
> +{
> +	mpu3050_report_xyz(dev->private);
> +}
> +
>  /**
>   *	mpu3050_interrupt_thread	-	handle an IRQ
>   *	@irq: interrupt numner
> @@ -178,17 +214,82 @@ static void mpu3050_input_close(struct input_dev *input)
>   */
>  static irqreturn_t mpu3050_interrupt_thread(int irq, void *data)
>  {
> -	struct mpu3050_sensor *sensor = data;
> -	struct axis_data axis;
> +	mpu3050_report_xyz(data);
>  
> -	mpu3050_read_xyz(sensor->client, &axis);
> +	return IRQ_HANDLED;
> +}
>  
> -	input_report_abs(sensor->idev, ABS_X, axis.x);
> -	input_report_abs(sensor->idev, ABS_Y, axis.y);
> -	input_report_abs(sensor->idev, ABS_Z, axis.z);
> -	input_sync(sensor->idev);
> +static void __devinit mpu3050_init_idev(struct mpu3050_sensor *sensor,
> +						struct input_dev *idev)
> +{
> +	idev->name = "MPU3050";
> +	idev->id.bustype = BUS_I2C;
> +	idev->dev.parent = &sensor->client->dev;
>  
> -	return IRQ_HANDLED;
> +	__set_bit(EV_ABS, idev->evbit);
> +	input_set_abs_params(idev, ABS_X,
> +			     MPU3050_MIN_VALUE, MPU3050_MAX_VALUE, 0, 0);
> +	input_set_abs_params(idev, ABS_Y,
> +			     MPU3050_MIN_VALUE, MPU3050_MAX_VALUE, 0, 0);
> +	input_set_abs_params(idev, ABS_Z,
> +			     MPU3050_MIN_VALUE, MPU3050_MAX_VALUE, 0, 0);
> +}
> +
> +static int __devinit mpu3050_create_idev(struct mpu3050_sensor *sensor)
> +{
> +	struct input_dev *idev;
> +	int err;
> +
> +	idev = input_allocate_device();
> +	if (!idev)
> +		return -ENODEV;
Could this be _ENOMEM.
> +
> +	mpu3050_init_idev(sensor, idev);
> +
> +	idev->open = mpu3050_input_open;
> +	idev->close = mpu3050_input_close;
> +	input_set_drvdata(idev, sensor);
> +
> +	err = input_register_device(idev);
> +	if (err) {
> +		input_free_device(idev);
> +		return err;
> +	}
> +
> +	sensor->idev = idev;
> +
> +	return 0;
> +}
> +
> +static int __devinit mpu3050_create_polled_idev(struct mpu3050_sensor *sensor)
> +{
> +	struct input_polled_dev *ipoll_dev;
> +	int err;
> +
> +	ipoll_dev = input_allocate_polled_device();
> +	if (!ipoll_dev)
> +		return -ENOMEM;
> +
> +	ipoll_dev->private = sensor;
> +	ipoll_dev->open = mpu3050_poll_open;
> +	ipoll_dev->close = mpu3050_poll_close;
> +	ipoll_dev->poll = mpu3050_poll;
> +	ipoll_dev->poll_interval = MPU3050_DEFAULT_POLL_INTERVAL;
> +	ipoll_dev->poll_interval_min = 10;
> +	ipoll_dev->poll_interval_max = MPU3050_DEFAULT_POLL_INTERVAL;
> +
> +	mpu3050_init_idev(sensor, ipoll_dev->input);
> +
> +	err = input_register_polled_device(ipoll_dev);
> +	if (err) {
> +		input_free_polled_device(ipoll_dev);
> +		return err;
> +	}
> +
> +	sensor->input_polled = ipoll_dev;
> +	sensor->idev = ipoll_dev->input;
> +
> +	return 0;
>  }
>  
>  /**
> @@ -205,21 +306,17 @@ static int __devinit mpu3050_probe(struct i2c_client *client,
>  				   const struct i2c_device_id *id)
>  {
>  	struct mpu3050_sensor *sensor;
> -	struct input_dev *idev;
>  	int ret;
>  	int error;
>  
>  	sensor = kzalloc(sizeof(struct mpu3050_sensor), GFP_KERNEL);
> -	idev = input_allocate_device();
> -	if (!sensor || !idev) {
> +	if (!sensor) {
>  		dev_err(&client->dev, "failed to allocate driver data\n");
> -		error = -ENOMEM;
> -		goto err_free_mem;
> +		return -ENOMEM;
>  	}
>  
>  	sensor->client = client;
>  	sensor->dev = &client->dev;
> -	sensor->idev = idev;
>  
>  	mpu3050_set_power_mode(client, 1);
>  	msleep(10);
> @@ -237,39 +334,31 @@ static int __devinit mpu3050_probe(struct i2c_client *client,
>  		goto err_free_mem;
>  	}
>  
> -	idev->name = "MPU3050";
> -	idev->id.bustype = BUS_I2C;
> -	idev->dev.parent = &client->dev;
> -
> -	idev->open = mpu3050_input_open;
> -	idev->close = mpu3050_input_close;
> -
> -	__set_bit(EV_ABS, idev->evbit);
> -	input_set_abs_params(idev, ABS_X,
> -			     MPU3050_MIN_VALUE, MPU3050_MAX_VALUE, 0, 0);
> -	input_set_abs_params(idev, ABS_Y,
> -			     MPU3050_MIN_VALUE, MPU3050_MAX_VALUE, 0, 0);
> -	input_set_abs_params(idev, ABS_Z,
> -			     MPU3050_MIN_VALUE, MPU3050_MAX_VALUE, 0, 0);
> -
> -	input_set_drvdata(idev, sensor);
> -
>  	pm_runtime_set_active(&client->dev);
>  
> -	error = request_threaded_irq(client->irq,
> -				     NULL, mpu3050_interrupt_thread,
> -				     IRQF_TRIGGER_RISING,
> -				     "mpu_int", sensor);
> -	if (error) {
> -		dev_err(&client->dev,
> -			"can't get IRQ %d, error %d\n", client->irq, error);
> -		goto err_pm_set_suspended;
> -	}
> -
> -	error = input_register_device(idev);
> -	if (error) {
> -		dev_err(&client->dev, "failed to register input device\n");
> -		goto err_free_irq;
> +	if (client->irq > 0) {
> +		error = mpu3050_create_idev(sensor);
> +		if (error) {
> +			dev_err(&client->dev, "failed to register input device\n");
> +			goto err_free_irq;
> +		}
> +
> +		error = request_threaded_irq(client->irq,
> +				NULL, mpu3050_interrupt_thread,
> +				IRQF_TRIGGER_RISING,
> +				"mpu_int", sensor);
> +		if (error) {
> +			dev_err(&client->dev, "can't get IRQ %d, error %d\n",
> +					client->irq, error);
> +			goto err_pm_set_suspended;
> +		}
> +	} else {
> +		error = mpu3050_create_polled_idev(sensor);
> +		if (error) {
> +			dev_err(&client->dev,
> +				"failed to register polled input device");
> +			goto err_pm_set_suspended;
> +		}
>  	}
>  
>  	pm_runtime_enable(&client->dev);
> @@ -282,7 +371,6 @@ err_free_irq:
>  err_pm_set_suspended:
>  	pm_runtime_set_suspended(&client->dev);
>  err_free_mem:
> -	input_free_device(idev);
>  	kfree(sensor);
>  	return error;
>  }
> @@ -300,8 +388,13 @@ static int __devexit mpu3050_remove(struct i2c_client *client)
>  	pm_runtime_disable(&client->dev);
>  	pm_runtime_set_suspended(&client->dev);
>  
> -	free_irq(client->irq, sensor);
> -	input_unregister_device(sensor->idev);
> +	if (client->irq > 0) {
> +		free_irq(client->irq, sensor);
> +		input_unregister_device(sensor->idev);
> +	} else {
> +		input_unregister_polled_device(sensor->input_polled);
> +		input_free_polled_device(sensor->input_polled);
> +	}
>  	kfree(sensor);
>  
>  	return 0;
>
> --
> To unsubscribe from this list: send the line "unsubscribe linux-input" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH 2/3] input: mpu3050: Configure the sampling method
  2011-12-15 22:18 ` [PATCH 2/3] input: mpu3050: Configure the sampling method Alan Cox
@ 2011-12-24  8:07   ` Dmitry Torokhov
  0 siblings, 0 replies; 8+ messages in thread
From: Dmitry Torokhov @ 2011-12-24  8:07 UTC (permalink / raw)
  To: Alan Cox; +Cc: linux-input

Hi Alan,

On Thu, Dec 15, 2011 at 10:18:47PM +0000, Alan Cox wrote:
> From: Heikki Krogerus <heikki.krogerus@linux.intel.com>
> 
> This will improve the output of the sensor.
> 

...

>  
> +/* Configures the sampling method */
> +static int mpu3050_hw_init(struct mpu3050_sensor *sensor)

This should be __devinit.

...
> @@ -336,6 +423,10 @@ static int __devinit mpu3050_probe(struct i2c_client *client,
>  
>  	pm_runtime_set_active(&client->dev);
>  
> +	error = mpu3050_hw_init(sensor);
> +	if (error)
> +		goto err_free_mem;

We should go to err_pm_set_suspended, not err_free_mem.

I fixed it up locally.

Thanks.

-- 
Dmitry

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH 3/3] input: mpu3050: Ensure we enable interrupts
  2011-12-15 22:18 ` [PATCH 3/3] input: mpu3050: Ensure we enable interrupts Alan Cox
@ 2011-12-24  8:09   ` Dmitry Torokhov
  0 siblings, 0 replies; 8+ messages in thread
From: Dmitry Torokhov @ 2011-12-24  8:09 UTC (permalink / raw)
  To: Alan Cox; +Cc: linux-input

On Thu, Dec 15, 2011 at 10:18:59PM +0000, Alan Cox wrote:
> From: Heikki Krogerus <heikki.krogerus@linux.intel.com>
> 
> This also changes the devname parameter delivered to
> request_threaded_irq() from "mpu_int" to "mpu3050".
> 
> Signed-off-by: Heikki Krogerus <heikki.krogerus@linux.intel.com>
> Signed-off-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
> Signed-off-by: Alan Cox <alan@linux.intel.com>
> ---
> 
>  drivers/input/misc/mpu3050.c |   10 +++++++++-
>  1 files changed, 9 insertions(+), 1 deletions(-)
> 
> 
> diff --git a/drivers/input/misc/mpu3050.c b/drivers/input/misc/mpu3050.c
> index 89c7160..cf42c78 100644
> --- a/drivers/input/misc/mpu3050.c
> +++ b/drivers/input/misc/mpu3050.c
> @@ -437,12 +437,20 @@ static int __devinit mpu3050_probe(struct i2c_client *client,
>  		error = request_threaded_irq(client->irq,
>  				NULL, mpu3050_interrupt_thread,
>  				IRQF_TRIGGER_RISING,
> -				"mpu_int", sensor);
> +				"mpu3050", sensor);
>  		if (error) {
>  			dev_err(&client->dev, "can't get IRQ %d, error %d\n",
>  					client->irq, error);
>  			goto err_pm_set_suspended;
>  		}
> +
> +		/* Enable interrupts */
> +		i2c_smbus_write_byte_data(sensor->client, MPU3050_INT_CFG,
> +					MPU3050_LATCH_INT_EN
> +					| MPU3050_RAW_RDY_EN
> +					| MPU3050_MPU_RDY_EN);
> +		if (ret < 0)
> +			goto err_free_irq;

You are testing a random variable here. Also, this belongs in open()
method. I will apply the patch below.

Thanks.

-- 
Dmitry

Input: mpu3050 - ensure we enable interrupts

From: Heikki Krogerus <heikki.krogerus@linux.intel.com>

This also changes the devname parameter delivered to
request_threaded_irq() from "mpu_int" to "mpu3050".

Signed-off-by: Heikki Krogerus <heikki.krogerus@linux.intel.com>
Signed-off-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
Signed-off-by: Alan Cox <alan@linux.intel.com>
Signed-off-by: Dmitry Torokhov <dtor@mail.ru>
---

 drivers/input/misc/mpu3050.c |   13 ++++++++++++-
 1 files changed, 12 insertions(+), 1 deletions(-)


diff --git a/drivers/input/misc/mpu3050.c b/drivers/input/misc/mpu3050.c
index 28a7b19..76905ae 100644
--- a/drivers/input/misc/mpu3050.c
+++ b/drivers/input/misc/mpu3050.c
@@ -148,9 +148,20 @@ static void mpu3050_set_power_mode(struct i2c_client *client, u8 val)
 static int mpu3050_input_open(struct input_dev *input)
 {
 	struct mpu3050_sensor *sensor = input_get_drvdata(input);
+	int error;
 
 	pm_runtime_get(sensor->dev);
 
+	/* Enable interrupts */
+	error = i2c_smbus_write_byte_data(sensor->client, MPU3050_INT_CFG,
+					  MPU3050_LATCH_INT_EN |
+					  MPU3050_RAW_RDY_EN |
+					  MPU3050_MPU_RDY_EN);
+	if (error < 0) {
+		pm_runtime_put(sensor->dev);
+		return error;
+	}
+
 	return 0;
 }
 
@@ -259,7 +270,7 @@ static int __devinit mpu3050_probe(struct i2c_client *client,
 	error = request_threaded_irq(client->irq,
 				     NULL, mpu3050_interrupt_thread,
 				     IRQF_TRIGGER_RISING,
-				     "mpu_int", sensor);
+				     "mpu3050", sensor);
 	if (error) {
 		dev_err(&client->dev,
 			"can't get IRQ %d, error %d\n", client->irq, error);

^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [PATCH 1/3] input: mpu3050: Support for polled input device
  2011-12-15 22:18 ` [PATCH 1/3] input: mpu3050: Support for polled input device Alan Cox
  2011-12-16 10:52   ` Shubhrajyoti
@ 2011-12-28  5:07   ` Dmitry Torokhov
  1 sibling, 0 replies; 8+ messages in thread
From: Dmitry Torokhov @ 2011-12-28  5:07 UTC (permalink / raw)
  To: Alan Cox; +Cc: linux-input

Hi Alan,

On Thu, Dec 15, 2011 at 10:18:34PM +0000, Alan Cox wrote:
> From: Heikki Krogerus <heikki.krogerus@linux.intel.com>
> 
> The driver does not need to depend on interrupts.
> 

...

If you are using polled device infrastucture you need to select or
depend on it. Also:

> +
> +static void mpu3050_poll_close(struct input_polled_dev *ipoll_dev)
> +{
> +	struct mpu3050_sensor *sensor = ipoll_dev->private;
> +
> +	mpu3050_set_power_mode(sensor->client, 1);

I think you want 0 here.

> +	if (client->irq > 0) {
> +		error = mpu3050_create_idev(sensor);
> +		if (error) {
> +			dev_err(&client->dev, "failed to register input device\n");
> +			goto err_free_irq;

You are trying to free irq while you going to acquire it below...
> +		}
> +
> +		error = request_threaded_irq(client->irq,
> +				NULL, mpu3050_interrupt_thread,
> +				IRQF_TRIGGER_RISING,
> +				"mpu_int", sensor);

I think the patch below handles these issues.

-- 
Dmitry


Input: mpu3050 - Support for polled input device

From: Heikki Krogerus <heikki.krogerus@linux.intel.com>

The driver does not need to depend on interrupts.

Signed-off-by: Heikki Krogerus <heikki.krogerus@linux.intel.com>
Signed-off-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
Signed-off-by: Alan Cox <alan@linux.intel.com>
Signed-off-by: Dmitry Torokhov <dtor@mail.ru>
---

 drivers/input/misc/Kconfig   |    7 +
 drivers/input/misc/mpu3050.c |  268 +++++++++++++++++++++++++++++++++---------
 2 files changed, 219 insertions(+), 56 deletions(-)


diff --git a/drivers/input/misc/Kconfig b/drivers/input/misc/Kconfig
index 7b46781..f9c2b69 100644
--- a/drivers/input/misc/Kconfig
+++ b/drivers/input/misc/Kconfig
@@ -165,6 +165,13 @@ config INPUT_MPU3050
 	  To compile this driver as a module, choose M here: the
 	  module will be called mpu3050.
 
+config INPUT_MPU3050_POLLED_MODE
+	bool "Enable polling mode support"
+	depends on INPUT_MPU3050
+	select INPUT_POLLDEV
+	help
+	  Say Y here if you need gyroscope to work in polling mode.
+
 config INPUT_APANEL
 	tristate "Fujitsu Lifebook Application Panel buttons"
 	depends on X86 && I2C && LEDS_CLASS
diff --git a/drivers/input/misc/mpu3050.c b/drivers/input/misc/mpu3050.c
index 5403c57..d07eee1 100644
--- a/drivers/input/misc/mpu3050.c
+++ b/drivers/input/misc/mpu3050.c
@@ -37,6 +37,7 @@
 #include <linux/err.h>
 #include <linux/i2c.h>
 #include <linux/input.h>
+#include <linux/input-polldev.h>
 #include <linux/delay.h>
 #include <linux/slab.h>
 #include <linux/pm_runtime.h>
@@ -102,6 +103,8 @@
 #define MPU3050_PWR_MGM_RESET		0x80
 #define MPU3050_PWR_MGM_MASK		0x40
 
+#define MPU3050_DEFAULT_POLL_INTERVAL	200
+
 struct axis_data {
 	s16 x;
 	s16 y;
@@ -112,6 +115,9 @@ struct mpu3050_sensor {
 	struct i2c_client *client;
 	struct device *dev;
 	struct input_dev *idev;
+#ifdef CONFIG_INPUT_MPU3050_POLLED_MODE
+	struct input_polled_dev *input_polled;
+#endif
 };
 
 /**
@@ -169,6 +175,40 @@ static void mpu3050_read_xyz(struct i2c_client *client,
 }
 
 /**
+ *	mpu3050_report_xyz	-	read and report coordinates
+ *	@sensor: the sensor
+ *
+ *	Reads coordinates from the device and reports them to input
+ *	core.
+ */
+static void mpu3050_report_xyz(struct mpu3050_sensor *sensor)
+{
+	struct axis_data axis;
+
+	mpu3050_read_xyz(sensor->client, &axis);
+
+	input_report_abs(sensor->idev, ABS_X, axis.x);
+	input_report_abs(sensor->idev, ABS_Y, axis.y);
+	input_report_abs(sensor->idev, ABS_Z, axis.z);
+	input_sync(sensor->idev);
+}
+
+/**
+ *	mpu3050_interrupt_thread	-	handle an IRQ
+ *	@irq: interrupt number
+ *	@data: the sensor
+ *
+ *	Called by the kernel single threaded after an interrupt occurs. Read
+ *	the sensor data and generate an input event for it.
+ */
+static irqreturn_t mpu3050_interrupt_thread(int irq, void *data)
+{
+	mpu3050_report_xyz(data);
+
+	return IRQ_HANDLED;
+}
+
+/**
  *	mpu3050_set_power_mode	-	set the power mode
  *	@client: i2c client for the sensor
  *	@val: value to switch on/off of power, 1: normal power, 0: low power
@@ -228,30 +268,154 @@ static void mpu3050_input_close(struct input_dev *input)
 	pm_runtime_put(sensor->dev);
 }
 
+static void __devinit mpu3050_init_idev(struct mpu3050_sensor *sensor,
+						struct input_dev *idev)
+{
+	idev->name = "MPU3050";
+	idev->id.bustype = BUS_I2C;
+	idev->dev.parent = &sensor->client->dev;
+
+	__set_bit(EV_ABS, idev->evbit);
+	input_set_abs_params(idev, ABS_X,
+			     MPU3050_MIN_VALUE, MPU3050_MAX_VALUE, 0, 0);
+	input_set_abs_params(idev, ABS_Y,
+			     MPU3050_MIN_VALUE, MPU3050_MAX_VALUE, 0, 0);
+	input_set_abs_params(idev, ABS_Z,
+			     MPU3050_MIN_VALUE, MPU3050_MAX_VALUE, 0, 0);
+}
+
 /**
- *	mpu3050_interrupt_thread	-	handle an IRQ
- *	@irq: interrupt numner
- *	@data: the sensor
+ *	mpu3050_create_polled_idev - creates input device for the sensor
+ *	@sensor: the sensor
  *
- *	Called by the kernel single threaded after an interrupt occurs. Read
- *	the sensor data and generate an input event for it.
+ *	Called from mpu3050_probe() when setting up device in interrupt-driven
+ *	mode.
  */
-static irqreturn_t mpu3050_interrupt_thread(int irq, void *data)
+static int __devinit mpu3050_create_idev(struct mpu3050_sensor *sensor)
 {
-	struct mpu3050_sensor *sensor = data;
-	struct axis_data axis;
+	struct input_dev *idev;
+	int err;
 
-	mpu3050_read_xyz(sensor->client, &axis);
+	idev = input_allocate_device();
+	if (!idev)
+		return -ENODEV;
 
-	input_report_abs(sensor->idev, ABS_X, axis.x);
-	input_report_abs(sensor->idev, ABS_Y, axis.y);
-	input_report_abs(sensor->idev, ABS_Z, axis.z);
-	input_sync(sensor->idev);
+	mpu3050_init_idev(sensor, idev);
 
-	return IRQ_HANDLED;
+	idev->open = mpu3050_input_open;
+	idev->close = mpu3050_input_close;
+	input_set_drvdata(idev, sensor);
+
+	err = input_register_device(idev);
+	if (err) {
+		input_free_device(idev);
+		return err;
+	}
+
+	sensor->idev = idev;
+
+	return 0;
+}
+
+#ifdef CONFIG_INPUT_MPU3050_POLLED_MODE
+/**
+ *	mpu3050_poll_open	-	called when opening polled device
+ *	@ipoll_dev: polled input device being opened
+ *
+ *	The input layer calls this function when polled device is opened.
+ *	The function will cause the device to resume. Then, the device is
+ *	ready to provide data.
+ */
+static void mpu3050_poll_open(struct input_polled_dev *ipoll_dev)
+{
+	struct mpu3050_sensor *sensor = ipoll_dev->private;
+
+	mpu3050_set_power_mode(sensor->client, 1);
+	msleep(100);  /* wait for gyro chip resume */
+}
+
+/**
+ *	mpu3050_poll_close	-	called when closing polled device
+ *	@ipoll_dev: polled input device being closed
+ *
+ *	The input layer calls this function when polled device is closed.
+ *	The function will push the device to suspend.
+ */
+static void mpu3050_poll_close(struct input_polled_dev *ipoll_dev)
+{
+	struct mpu3050_sensor *sensor = ipoll_dev->private;
+
+	mpu3050_set_power_mode(sensor->client, 0);
 }
 
 /**
+ *	mpu3050_poll	-	polls the device and reports coordinated
+ *	@dev: device that is being polled
+ *
+ *	Called by polled device core at specified intervals.
+ */
+static void mpu3050_poll(struct input_polled_dev *dev)
+{
+	mpu3050_report_xyz(dev->private);
+}
+
+/**
+ *	mpu3050_create_polled_idev - creates polled input device for the sensor
+ *	@sensor: the sensor
+ *
+ *	Called from mpu3050_probe() when setting up device in polled mode.
+ */
+static int __devinit mpu3050_create_polled_idev(struct mpu3050_sensor *sensor)
+{
+	struct input_polled_dev *ipoll_dev;
+	int err;
+
+	ipoll_dev = input_allocate_polled_device();
+	if (!ipoll_dev)
+		return -ENOMEM;
+
+	sensor->input_polled = ipoll_dev;
+	sensor->idev = ipoll_dev->input;
+
+	ipoll_dev->private = sensor;
+	ipoll_dev->open = mpu3050_poll_open;
+	ipoll_dev->close = mpu3050_poll_close;
+	ipoll_dev->poll = mpu3050_poll;
+	ipoll_dev->poll_interval = MPU3050_DEFAULT_POLL_INTERVAL;
+	ipoll_dev->poll_interval_min = 10;
+	ipoll_dev->poll_interval_max = MPU3050_DEFAULT_POLL_INTERVAL;
+
+	mpu3050_init_idev(sensor, ipoll_dev->input);
+
+	err = input_register_polled_device(ipoll_dev);
+	if (err) {
+		input_free_polled_device(ipoll_dev);
+		return err;
+	}
+
+	return 0;
+}
+
+static void __devexit mpu3050_teardown_polled_idev(struct mpu3050_sensor *sensor)
+{
+	input_unregister_polled_device(sensor->input_polled);
+	input_free_polled_device(sensor->input_polled);
+}
+
+#else
+
+static inline int mpu3050_create_polled_idev(struct mpu3050_sensor *sensor)
+{
+	return -ENOSYS;
+}
+
+static inline void mpu3050_teardown_polled_idev(struct mpu3050_sensor *sensor)
+{
+}
+
+#endif
+
+/**
  *	mpu3050_hw_init	-	initialize hardware
  *	@sensor: the sensor
  *
@@ -310,21 +474,17 @@ static int __devinit mpu3050_probe(struct i2c_client *client,
 				   const struct i2c_device_id *id)
 {
 	struct mpu3050_sensor *sensor;
-	struct input_dev *idev;
 	int ret;
 	int error;
 
 	sensor = kzalloc(sizeof(struct mpu3050_sensor), GFP_KERNEL);
-	idev = input_allocate_device();
-	if (!sensor || !idev) {
+	if (!sensor) {
 		dev_err(&client->dev, "failed to allocate driver data\n");
-		error = -ENOMEM;
-		goto err_free_mem;
+		return -ENOMEM;
 	}
 
 	sensor->client = client;
 	sensor->dev = &client->dev;
-	sensor->idev = idev;
 
 	mpu3050_set_power_mode(client, 1);
 	msleep(10);
@@ -342,43 +502,35 @@ static int __devinit mpu3050_probe(struct i2c_client *client,
 		goto err_free_mem;
 	}
 
-	idev->name = "MPU3050";
-	idev->id.bustype = BUS_I2C;
-	idev->dev.parent = &client->dev;
-
-	idev->open = mpu3050_input_open;
-	idev->close = mpu3050_input_close;
-
-	__set_bit(EV_ABS, idev->evbit);
-	input_set_abs_params(idev, ABS_X,
-			     MPU3050_MIN_VALUE, MPU3050_MAX_VALUE, 0, 0);
-	input_set_abs_params(idev, ABS_Y,
-			     MPU3050_MIN_VALUE, MPU3050_MAX_VALUE, 0, 0);
-	input_set_abs_params(idev, ABS_Z,
-			     MPU3050_MIN_VALUE, MPU3050_MAX_VALUE, 0, 0);
-
-	input_set_drvdata(idev, sensor);
-
 	pm_runtime_set_active(&client->dev);
 
 	error = mpu3050_hw_init(sensor);
 	if (error)
 		goto err_pm_set_suspended;
 
-	error = request_threaded_irq(client->irq,
-				     NULL, mpu3050_interrupt_thread,
-				     IRQF_TRIGGER_RISING,
-				     "mpu3050", sensor);
-	if (error) {
-		dev_err(&client->dev,
-			"can't get IRQ %d, error %d\n", client->irq, error);
-		goto err_pm_set_suspended;
-	}
-
-	error = input_register_device(idev);
-	if (error) {
-		dev_err(&client->dev, "failed to register input device\n");
-		goto err_free_irq;
+	if (client->irq) {
+		error = mpu3050_create_idev(sensor);
+		if (error) {
+			dev_err(&client->dev, "failed to create input device\n");
+			goto err_pm_set_suspended;
+		}
+
+		error = request_threaded_irq(client->irq,
+				NULL, mpu3050_interrupt_thread,
+				IRQF_TRIGGER_RISING,
+				"mpu3050", sensor);
+		if (error) {
+			dev_err(&client->dev, "can't get IRQ %d, error %d\n",
+					client->irq, error);
+			goto err_destroy_input;
+		}
+	} else {
+		error = mpu3050_create_polled_idev(sensor);
+		if (error) {
+			dev_err(&client->dev,
+				"failed to create polled input device");
+			goto err_pm_set_suspended;
+		}
 	}
 
 	pm_runtime_enable(&client->dev);
@@ -386,12 +538,11 @@ static int __devinit mpu3050_probe(struct i2c_client *client,
 
 	return 0;
 
-err_free_irq:
-	free_irq(client->irq, sensor);
+err_destroy_input:
+	input_unregister_device(sensor->idev);
 err_pm_set_suspended:
 	pm_runtime_set_suspended(&client->dev);
 err_free_mem:
-	input_free_device(idev);
 	kfree(sensor);
 	return error;
 }
@@ -409,8 +560,13 @@ static int __devexit mpu3050_remove(struct i2c_client *client)
 	pm_runtime_disable(&client->dev);
 	pm_runtime_set_suspended(&client->dev);
 
-	free_irq(client->irq, sensor);
-	input_unregister_device(sensor->idev);
+	if (client->irq) {
+		free_irq(client->irq, sensor);
+		input_unregister_device(sensor->idev);
+	} else {
+		mpu3050_teardown_polled_idev(sensor);
+	}
+
 	kfree(sensor);
 
 	return 0;

^ permalink raw reply related	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2011-12-28  5:08 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-12-15 22:18 [PATCH 0/3] mpu3050 updates Alan Cox
2011-12-15 22:18 ` [PATCH 1/3] input: mpu3050: Support for polled input device Alan Cox
2011-12-16 10:52   ` Shubhrajyoti
2011-12-28  5:07   ` Dmitry Torokhov
2011-12-15 22:18 ` [PATCH 2/3] input: mpu3050: Configure the sampling method Alan Cox
2011-12-24  8:07   ` Dmitry Torokhov
2011-12-15 22:18 ` [PATCH 3/3] input: mpu3050: Ensure we enable interrupts Alan Cox
2011-12-24  8:09   ` Dmitry Torokhov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).