blob: d3da652ee532c059f86d93944627dcc09f97bec7 [file] [log] [blame]
Joseph Lai631b16e2011-06-27 13:26:53 -07001/*
2 * MPU3050 Tri-axis gyroscope driver
3 *
4 * Copyright (C) 2011 Wistron Co.Ltd
5 * Joseph Lai <joseph_lai@wistron.com>
6 *
7 * Trimmed down by Alan Cox <alan@linux.intel.com> to produce this version
8 *
9 * This is a 'lite' version of the driver, while we consider the right way
10 * to present the other features to user space. In particular it requires the
11 * device has an IRQ, and it only provides an input interface, so is not much
12 * use for device orientation. A fuller version is available from the Meego
13 * tree.
14 *
15 * This program is based on bma023.c.
16 *
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; version 2 of the License.
20 *
21 * This program is distributed in the hope that it will be useful, but
22 * WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 * General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License along
27 * with this program; if not, write to the Free Software Foundation, Inc.,
28 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
29 *
30 */
31
32#include <linux/module.h>
33#include <linux/init.h>
34#include <linux/interrupt.h>
35#include <linux/platform_device.h>
36#include <linux/mutex.h>
37#include <linux/err.h>
38#include <linux/i2c.h>
39#include <linux/input.h>
40#include <linux/delay.h>
41#include <linux/slab.h>
42#include <linux/pm_runtime.h>
Wentao Xuc7769c02012-08-03 15:06:41 -040043#include <linux/gpio.h>
Wentao Xudac9e602012-06-12 11:52:34 -040044#include <linux/input/mpu3050.h>
45#include <linux/regulator/consumer.h>
Joseph Lai631b16e2011-06-27 13:26:53 -070046
Joseph Lai631b16e2011-06-27 13:26:53 -070047#define MPU3050_CHIP_ID 0x69
Joseph Lai631b16e2011-06-27 13:26:53 -070048
49#define MPU3050_AUTO_DELAY 1000
50
51#define MPU3050_MIN_VALUE -32768
52#define MPU3050_MAX_VALUE 32767
53
Wentao Xuc7769c02012-08-03 15:06:41 -040054#define MPU3050_MIN_POLL_INTERVAL 1
55#define MPU3050_MAX_POLL_INTERVAL 250
Heikki Krogeruscd314fa2011-12-24 00:09:04 -080056#define MPU3050_DEFAULT_POLL_INTERVAL 200
57#define MPU3050_DEFAULT_FS_RANGE 3
58
59/* Register map */
60#define MPU3050_CHIP_ID_REG 0x00
61#define MPU3050_SMPLRT_DIV 0x15
62#define MPU3050_DLPF_FS_SYNC 0x16
63#define MPU3050_INT_CFG 0x17
64#define MPU3050_XOUT_H 0x1D
65#define MPU3050_PWR_MGM 0x3E
66#define MPU3050_PWR_MGM_POS 6
67
68/* Register bits */
69
70/* DLPF_FS_SYNC */
71#define MPU3050_EXT_SYNC_NONE 0x00
72#define MPU3050_EXT_SYNC_TEMP 0x20
73#define MPU3050_EXT_SYNC_GYROX 0x40
74#define MPU3050_EXT_SYNC_GYROY 0x60
75#define MPU3050_EXT_SYNC_GYROZ 0x80
76#define MPU3050_EXT_SYNC_ACCELX 0xA0
77#define MPU3050_EXT_SYNC_ACCELY 0xC0
78#define MPU3050_EXT_SYNC_ACCELZ 0xE0
79#define MPU3050_EXT_SYNC_MASK 0xE0
80#define MPU3050_FS_250DPS 0x00
81#define MPU3050_FS_500DPS 0x08
82#define MPU3050_FS_1000DPS 0x10
83#define MPU3050_FS_2000DPS 0x18
84#define MPU3050_FS_MASK 0x18
85#define MPU3050_DLPF_CFG_256HZ_NOLPF2 0x00
86#define MPU3050_DLPF_CFG_188HZ 0x01
87#define MPU3050_DLPF_CFG_98HZ 0x02
88#define MPU3050_DLPF_CFG_42HZ 0x03
89#define MPU3050_DLPF_CFG_20HZ 0x04
90#define MPU3050_DLPF_CFG_10HZ 0x05
91#define MPU3050_DLPF_CFG_5HZ 0x06
92#define MPU3050_DLPF_CFG_2100HZ_NOLPF 0x07
93#define MPU3050_DLPF_CFG_MASK 0x07
94/* INT_CFG */
95#define MPU3050_RAW_RDY_EN 0x01
Wentao Xuc7769c02012-08-03 15:06:41 -040096#define MPU3050_MPU_RDY_EN 0x04
97#define MPU3050_LATCH_INT_EN 0x20
98#define MPU3050_OPEN_DRAIN 0x40
99#define MPU3050_ACTIVE_LOW 0x80
Heikki Krogeruscd314fa2011-12-24 00:09:04 -0800100/* PWR_MGM */
101#define MPU3050_PWR_MGM_PLL_X 0x01
102#define MPU3050_PWR_MGM_PLL_Y 0x02
103#define MPU3050_PWR_MGM_PLL_Z 0x03
104#define MPU3050_PWR_MGM_CLKSEL 0x07
105#define MPU3050_PWR_MGM_STBY_ZG 0x08
106#define MPU3050_PWR_MGM_STBY_YG 0x10
107#define MPU3050_PWR_MGM_STBY_XG 0x20
108#define MPU3050_PWR_MGM_SLEEP 0x40
109#define MPU3050_PWR_MGM_RESET 0x80
110#define MPU3050_PWR_MGM_MASK 0x40
111
Joseph Lai631b16e2011-06-27 13:26:53 -0700112struct axis_data {
113 s16 x;
114 s16 y;
115 s16 z;
116};
117
118struct mpu3050_sensor {
119 struct i2c_client *client;
120 struct device *dev;
121 struct input_dev *idev;
Wentao Xudac9e602012-06-12 11:52:34 -0400122 struct mpu3050_gyro_platform_data *platform_data;
123 struct delayed_work input_work;
124 u32 use_poll;
Wentao Xuc7769c02012-08-03 15:06:41 -0400125 u32 poll_interval;
Joseph Lai631b16e2011-06-27 13:26:53 -0700126};
127
Wentao Xudac9e602012-06-12 11:52:34 -0400128struct sensor_regulator {
129 struct regulator *vreg;
130 const char *name;
131 u32 min_uV;
132 u32 max_uV;
133};
134
135struct sensor_regulator mpu_vreg[] = {
136 {NULL, "vdd", 2100000, 3600000},
137 {NULL, "vlogic", 1800000, 1800000},
138};
139
140static int mpu3050_config_regulator(struct i2c_client *client, bool on)
141{
142 int rc = 0, i;
143 int num_reg = sizeof(mpu_vreg) / sizeof(struct sensor_regulator);
144
145 if (on) {
146 for (i = 0; i < num_reg; i++) {
147 mpu_vreg[i].vreg = regulator_get(&client->dev,
148 mpu_vreg[i].name);
149 if (IS_ERR(mpu_vreg[i].vreg)) {
150 rc = PTR_ERR(mpu_vreg[i].vreg);
151 pr_err("%s:regulator get failed rc=%d\n",
152 __func__, rc);
153 goto error_vdd;
154 }
155
156 if (regulator_count_voltages(mpu_vreg[i].vreg) > 0) {
157 rc = regulator_set_voltage(mpu_vreg[i].vreg,
158 mpu_vreg[i].min_uV, mpu_vreg[i].max_uV);
159 if (rc) {
160 pr_err("%s:set_voltage failed rc=%d\n",
161 __func__, rc);
162 regulator_put(mpu_vreg[i].vreg);
163 goto error_vdd;
164 }
165 }
166
167 rc = regulator_enable(mpu_vreg[i].vreg);
168 if (rc) {
169 pr_err("%s: regulator_enable failed rc =%d\n",
170 __func__,
171 rc);
172
173 if (regulator_count_voltages(
174 mpu_vreg[i].vreg) > 0) {
175 regulator_set_voltage(mpu_vreg[i].vreg,
176 0, mpu_vreg[i].max_uV);
177 }
178 regulator_put(mpu_vreg[i].vreg);
179 goto error_vdd;
180 }
181 }
182 return rc;
183 } else {
184 i = num_reg;
185 }
186error_vdd:
187 while (--i >= 0) {
188 if (regulator_count_voltages(mpu_vreg[i].vreg) > 0) {
189 regulator_set_voltage(mpu_vreg[i].vreg, 0,
190 mpu_vreg[i].max_uV);
191 }
192 regulator_disable(mpu_vreg[i].vreg);
193 regulator_put(mpu_vreg[i].vreg);
194 }
195 return rc;
196}
197
Joseph Lai631b16e2011-06-27 13:26:53 -0700198/**
Wentao Xuc7769c02012-08-03 15:06:41 -0400199 * mpu3050_attr_get_polling_rate - get the sampling rate
200 */
201static ssize_t mpu3050_attr_get_polling_rate(struct device *dev,
202 struct device_attribute *attr,
203 char *buf)
204{
205 int val;
206 struct mpu3050_sensor *sensor = dev_get_drvdata(dev);
207 val = sensor ? sensor->poll_interval : 0;
208 return snprintf(buf, 8, "%d\n", val);
209}
210
211/**
212 * mpu3050_attr_set_polling_rate - set the sampling rate
213 */
214static ssize_t mpu3050_attr_set_polling_rate(struct device *dev,
215 struct device_attribute *attr,
216 const char *buf, size_t size)
217{
218 struct mpu3050_sensor *sensor = dev_get_drvdata(dev);
219 unsigned long interval_ms;
220
221 if (kstrtoul(buf, 10, &interval_ms))
222 return -EINVAL;
223 if ((interval_ms < MPU3050_MIN_POLL_INTERVAL) ||
224 (interval_ms > MPU3050_MAX_POLL_INTERVAL))
225 return -EINVAL;
226
227 if (sensor)
228 sensor->poll_interval = interval_ms;
229
230 /* Output frequency divider. The poll interval */
231 i2c_smbus_write_byte_data(sensor->client, MPU3050_SMPLRT_DIV,
232 interval_ms - 1);
233
234 return size;
235}
236
237static struct device_attribute attributes[] = {
238
239 __ATTR(pollrate_ms, 0666,
240 mpu3050_attr_get_polling_rate,
241 mpu3050_attr_set_polling_rate),
242};
243
244static int create_sysfs_interfaces(struct device *dev)
245{
246 int i;
247 int err;
248 for (i = 0; i < ARRAY_SIZE(attributes); i++) {
249 err = device_create_file(dev, attributes + i);
250 if (err)
251 goto error;
252 }
253 return 0;
254
255error:
256 for ( ; i >= 0; i--)
257 device_remove_file(dev, attributes + i);
258 dev_err(dev, "%s:Unable to create interface\n", __func__);
259 return err;
260}
261
262static int remove_sysfs_interfaces(struct device *dev)
263{
264 int i;
265 for (i = 0; i < ARRAY_SIZE(attributes); i++)
266 device_remove_file(dev, attributes + i);
267 return 0;
268}
269
270/**
Joseph Lai631b16e2011-06-27 13:26:53 -0700271 * mpu3050_xyz_read_reg - read the axes values
272 * @buffer: provide register addr and get register
273 * @length: length of register
274 *
275 * Reads the register values in one transaction or returns a negative
276 * error code on failure.
277 */
278static int mpu3050_xyz_read_reg(struct i2c_client *client,
279 u8 *buffer, int length)
280{
281 /*
282 * Annoying we can't make this const because the i2c layer doesn't
283 * declare input buffers const.
284 */
285 char cmd = MPU3050_XOUT_H;
286 struct i2c_msg msg[] = {
287 {
288 .addr = client->addr,
289 .flags = 0,
290 .len = 1,
291 .buf = &cmd,
292 },
293 {
294 .addr = client->addr,
295 .flags = I2C_M_RD,
296 .len = length,
297 .buf = buffer,
298 },
299 };
300
301 return i2c_transfer(client->adapter, msg, 2);
302}
303
304/**
305 * mpu3050_read_xyz - get co-ordinates from device
306 * @client: i2c address of sensor
307 * @coords: co-ordinates to update
308 *
309 * Return the converted X Y and Z co-ordinates from the sensor device
310 */
311static void mpu3050_read_xyz(struct i2c_client *client,
312 struct axis_data *coords)
313{
314 u16 buffer[3];
315
316 mpu3050_xyz_read_reg(client, (u8 *)buffer, 6);
317 coords->x = be16_to_cpu(buffer[0]);
318 coords->y = be16_to_cpu(buffer[1]);
319 coords->z = be16_to_cpu(buffer[2]);
320 dev_dbg(&client->dev, "%s: x %d, y %d, z %d\n", __func__,
321 coords->x, coords->y, coords->z);
322}
323
324/**
325 * mpu3050_set_power_mode - set the power mode
326 * @client: i2c client for the sensor
327 * @val: value to switch on/off of power, 1: normal power, 0: low power
328 *
329 * Put device to normal-power mode or low-power mode.
330 */
331static void mpu3050_set_power_mode(struct i2c_client *client, u8 val)
332{
333 u8 value;
334
Wentao Xudac9e602012-06-12 11:52:34 -0400335 if (val) {
336 mpu3050_config_regulator(client, 1);
337 udelay(10);
338 }
339
Joseph Lai631b16e2011-06-27 13:26:53 -0700340 value = i2c_smbus_read_byte_data(client, MPU3050_PWR_MGM);
341 value = (value & ~MPU3050_PWR_MGM_MASK) |
342 (((val << MPU3050_PWR_MGM_POS) & MPU3050_PWR_MGM_MASK) ^
343 MPU3050_PWR_MGM_MASK);
344 i2c_smbus_write_byte_data(client, MPU3050_PWR_MGM, value);
Wentao Xudac9e602012-06-12 11:52:34 -0400345
346 if (!val) {
347 udelay(10);
348 mpu3050_config_regulator(client, 0);
349 }
Joseph Lai631b16e2011-06-27 13:26:53 -0700350}
351
352/**
353 * mpu3050_input_open - called on input event open
354 * @input: input dev of opened device
355 *
356 * The input layer calls this function when input event is opened. The
357 * function will push the device to resume. Then, the device is ready
358 * to provide data.
359 */
360static int mpu3050_input_open(struct input_dev *input)
361{
362 struct mpu3050_sensor *sensor = input_get_drvdata(input);
Heikki Krogerus3b518722011-12-23 23:57:09 -0800363 int error;
Joseph Lai631b16e2011-06-27 13:26:53 -0700364
Wentao Xuc7769c02012-08-03 15:06:41 -0400365 pm_runtime_get_sync(sensor->dev);
Joseph Lai631b16e2011-06-27 13:26:53 -0700366
Heikki Krogerus3b518722011-12-23 23:57:09 -0800367 /* Enable interrupts */
368 error = i2c_smbus_write_byte_data(sensor->client, MPU3050_INT_CFG,
Wentao Xuc7769c02012-08-03 15:06:41 -0400369 MPU3050_ACTIVE_LOW |
370 MPU3050_OPEN_DRAIN |
371 MPU3050_RAW_RDY_EN);
Heikki Krogerus3b518722011-12-23 23:57:09 -0800372 if (error < 0) {
373 pm_runtime_put(sensor->dev);
374 return error;
375 }
Wentao Xudac9e602012-06-12 11:52:34 -0400376 if (sensor->use_poll)
377 schedule_delayed_work(&sensor->input_work,
Wentao Xuc7769c02012-08-03 15:06:41 -0400378 msecs_to_jiffies(sensor->poll_interval));
Heikki Krogerus3b518722011-12-23 23:57:09 -0800379
Joseph Lai631b16e2011-06-27 13:26:53 -0700380 return 0;
381}
382
383/**
384 * mpu3050_input_close - called on input event close
385 * @input: input dev of closed device
386 *
387 * The input layer calls this function when input event is closed. The
388 * function will push the device to suspend.
389 */
390static void mpu3050_input_close(struct input_dev *input)
391{
392 struct mpu3050_sensor *sensor = input_get_drvdata(input);
393
Wentao Xudac9e602012-06-12 11:52:34 -0400394 if (sensor->use_poll)
395 cancel_delayed_work_sync(&sensor->input_work);
396
Joseph Lai631b16e2011-06-27 13:26:53 -0700397 pm_runtime_put(sensor->dev);
398}
399
400/**
401 * mpu3050_interrupt_thread - handle an IRQ
402 * @irq: interrupt numner
403 * @data: the sensor
404 *
405 * Called by the kernel single threaded after an interrupt occurs. Read
406 * the sensor data and generate an input event for it.
407 */
408static irqreturn_t mpu3050_interrupt_thread(int irq, void *data)
409{
410 struct mpu3050_sensor *sensor = data;
411 struct axis_data axis;
412
413 mpu3050_read_xyz(sensor->client, &axis);
414
415 input_report_abs(sensor->idev, ABS_X, axis.x);
416 input_report_abs(sensor->idev, ABS_Y, axis.y);
417 input_report_abs(sensor->idev, ABS_Z, axis.z);
418 input_sync(sensor->idev);
419
420 return IRQ_HANDLED;
421}
422
423/**
Wentao Xudac9e602012-06-12 11:52:34 -0400424 * mpu3050_input_work_fn - polling work
425 * @work: the work struct
426 *
427 * Called by the work queue; read sensor data and generate an input
428 * event
429 */
430static void mpu3050_input_work_fn(struct work_struct *work)
431{
432 struct mpu3050_sensor *sensor;
433 struct axis_data axis;
434
435 sensor = container_of((struct delayed_work *)work,
436 struct mpu3050_sensor, input_work);
437
438 mpu3050_read_xyz(sensor->client, &axis);
439
440 input_report_abs(sensor->idev, ABS_X, axis.x);
441 input_report_abs(sensor->idev, ABS_Y, axis.y);
442 input_report_abs(sensor->idev, ABS_Z, axis.z);
443 input_sync(sensor->idev);
444
445 if (sensor->use_poll)
446 schedule_delayed_work(&sensor->input_work,
Wentao Xuc7769c02012-08-03 15:06:41 -0400447 msecs_to_jiffies(sensor->poll_interval));
Wentao Xudac9e602012-06-12 11:52:34 -0400448}
449
450/**
Heikki Krogeruscd314fa2011-12-24 00:09:04 -0800451 * mpu3050_hw_init - initialize hardware
452 * @sensor: the sensor
453 *
454 * Called during device probe; configures the sampling method.
455 */
456static int __devinit mpu3050_hw_init(struct mpu3050_sensor *sensor)
457{
458 struct i2c_client *client = sensor->client;
459 int ret;
460 u8 reg;
461
462 /* Reset */
463 ret = i2c_smbus_write_byte_data(client, MPU3050_PWR_MGM,
464 MPU3050_PWR_MGM_RESET);
465 if (ret < 0)
466 return ret;
467
468 ret = i2c_smbus_read_byte_data(client, MPU3050_PWR_MGM);
469 if (ret < 0)
470 return ret;
471
472 ret &= ~MPU3050_PWR_MGM_CLKSEL;
473 ret |= MPU3050_PWR_MGM_PLL_Z;
474 ret = i2c_smbus_write_byte_data(client, MPU3050_PWR_MGM, ret);
475 if (ret < 0)
476 return ret;
477
478 /* Output frequency divider. The poll interval */
479 ret = i2c_smbus_write_byte_data(client, MPU3050_SMPLRT_DIV,
Wentao Xuc7769c02012-08-03 15:06:41 -0400480 sensor->poll_interval - 1);
Heikki Krogeruscd314fa2011-12-24 00:09:04 -0800481 if (ret < 0)
482 return ret;
483
484 /* Set low pass filter and full scale */
485 reg = MPU3050_DEFAULT_FS_RANGE;
486 reg |= MPU3050_DLPF_CFG_42HZ << 3;
487 reg |= MPU3050_EXT_SYNC_NONE << 5;
488 ret = i2c_smbus_write_byte_data(client, MPU3050_DLPF_FS_SYNC, reg);
489 if (ret < 0)
490 return ret;
491
492 return 0;
493}
494
495/**
Joseph Lai631b16e2011-06-27 13:26:53 -0700496 * mpu3050_probe - device detection callback
497 * @client: i2c client of found device
498 * @id: id match information
499 *
500 * The I2C layer calls us when it believes a sensor is present at this
501 * address. Probe to see if this is correct and to validate the device.
502 *
503 * If present install the relevant sysfs interfaces and input device.
504 */
505static int __devinit mpu3050_probe(struct i2c_client *client,
506 const struct i2c_device_id *id)
507{
508 struct mpu3050_sensor *sensor;
509 struct input_dev *idev;
510 int ret;
511 int error;
512
513 sensor = kzalloc(sizeof(struct mpu3050_sensor), GFP_KERNEL);
514 idev = input_allocate_device();
515 if (!sensor || !idev) {
516 dev_err(&client->dev, "failed to allocate driver data\n");
517 error = -ENOMEM;
518 goto err_free_mem;
519 }
520
521 sensor->client = client;
522 sensor->dev = &client->dev;
523 sensor->idev = idev;
Wentao Xudac9e602012-06-12 11:52:34 -0400524 sensor->platform_data = client->dev.platform_data;
Wentao Xuc7769c02012-08-03 15:06:41 -0400525 i2c_set_clientdata(client, sensor);
526 if (sensor->platform_data) {
527 u32 interval = sensor->platform_data->poll_interval;
528
529 if ((interval < MPU3050_MIN_POLL_INTERVAL) ||
530 (interval > MPU3050_MAX_POLL_INTERVAL))
531 sensor->poll_interval = MPU3050_DEFAULT_POLL_INTERVAL;
532 else
533 sensor->poll_interval = interval;
534 } else {
535 sensor->poll_interval = MPU3050_DEFAULT_POLL_INTERVAL;
536 }
Joseph Lai631b16e2011-06-27 13:26:53 -0700537
538 mpu3050_set_power_mode(client, 1);
539 msleep(10);
540
541 ret = i2c_smbus_read_byte_data(client, MPU3050_CHIP_ID_REG);
542 if (ret < 0) {
543 dev_err(&client->dev, "failed to detect device\n");
544 error = -ENXIO;
545 goto err_free_mem;
546 }
547
548 if (ret != MPU3050_CHIP_ID) {
549 dev_err(&client->dev, "unsupported chip id\n");
550 error = -ENXIO;
551 goto err_free_mem;
552 }
553
554 idev->name = "MPU3050";
555 idev->id.bustype = BUS_I2C;
556 idev->dev.parent = &client->dev;
557
558 idev->open = mpu3050_input_open;
559 idev->close = mpu3050_input_close;
560
561 __set_bit(EV_ABS, idev->evbit);
562 input_set_abs_params(idev, ABS_X,
563 MPU3050_MIN_VALUE, MPU3050_MAX_VALUE, 0, 0);
564 input_set_abs_params(idev, ABS_Y,
565 MPU3050_MIN_VALUE, MPU3050_MAX_VALUE, 0, 0);
566 input_set_abs_params(idev, ABS_Z,
567 MPU3050_MIN_VALUE, MPU3050_MAX_VALUE, 0, 0);
568
569 input_set_drvdata(idev, sensor);
570
571 pm_runtime_set_active(&client->dev);
572
Heikki Krogeruscd314fa2011-12-24 00:09:04 -0800573 error = mpu3050_hw_init(sensor);
574 if (error)
575 goto err_pm_set_suspended;
576
Wentao Xudac9e602012-06-12 11:52:34 -0400577 if (client->irq == 0) {
Wentao Xudac9e602012-06-12 11:52:34 -0400578 sensor->use_poll = 1;
Wentao Xuc7769c02012-08-03 15:06:41 -0400579 INIT_DELAYED_WORK(&sensor->input_work, mpu3050_input_work_fn);
Wentao Xudac9e602012-06-12 11:52:34 -0400580 } else {
581 sensor->use_poll = 0;
582
Wentao Xuc7769c02012-08-03 15:06:41 -0400583 if (gpio_is_valid(sensor->platform_data->gpio_int)) {
584 /* configure interrupt gpio */
585 ret = gpio_request(sensor->platform_data->gpio_int,
586 "gyro_gpio_int");
587 if (ret) {
588 pr_err("%s: unable to request interrupt gpio %d\n",
589 __func__,
590 sensor->platform_data->gpio_int);
591 goto err_pm_set_suspended;
592 }
593
594 ret = gpio_direction_input(
595 sensor->platform_data->gpio_int);
596 if (ret) {
597 pr_err("%s: unable to set direction for gpio %d\n",
598 __func__, sensor->platform_data->gpio_int);
599 goto err_free_gpio;
600 }
601 }
602
Wentao Xudac9e602012-06-12 11:52:34 -0400603 error = request_threaded_irq(client->irq,
Joseph Lai631b16e2011-06-27 13:26:53 -0700604 NULL, mpu3050_interrupt_thread,
Wentao Xuc7769c02012-08-03 15:06:41 -0400605 IRQF_TRIGGER_FALLING,
Heikki Krogerus3b518722011-12-23 23:57:09 -0800606 "mpu3050", sensor);
Wentao Xudac9e602012-06-12 11:52:34 -0400607 if (error) {
608 dev_err(&client->dev,
609 "can't get IRQ %d, error %d\n",
610 client->irq, error);
611 goto err_pm_set_suspended;
612 }
Joseph Lai631b16e2011-06-27 13:26:53 -0700613 }
614
615 error = input_register_device(idev);
616 if (error) {
617 dev_err(&client->dev, "failed to register input device\n");
618 goto err_free_irq;
619 }
620
Wentao Xuc7769c02012-08-03 15:06:41 -0400621 error = create_sysfs_interfaces(&client->dev);
622 if (error < 0) {
623 dev_err(&client->dev, "failed to create sysfs\n");
624 goto err_input_cleanup;
625 }
626
Joseph Lai631b16e2011-06-27 13:26:53 -0700627 pm_runtime_enable(&client->dev);
628 pm_runtime_set_autosuspend_delay(&client->dev, MPU3050_AUTO_DELAY);
629
630 return 0;
631
Wentao Xuc7769c02012-08-03 15:06:41 -0400632err_input_cleanup:
633 input_unregister_device(idev);
Joseph Lai631b16e2011-06-27 13:26:53 -0700634err_free_irq:
Wentao Xudac9e602012-06-12 11:52:34 -0400635 if (client->irq > 0)
636 free_irq(client->irq, sensor);
Wentao Xuc7769c02012-08-03 15:06:41 -0400637err_free_gpio:
638 if ((client->irq > 0) &&
639 (gpio_is_valid(sensor->platform_data->gpio_int)))
640 gpio_free(sensor->platform_data->gpio_int);
Joseph Lai631b16e2011-06-27 13:26:53 -0700641err_pm_set_suspended:
642 pm_runtime_set_suspended(&client->dev);
643err_free_mem:
Axel Lind9b830f2011-08-11 09:19:29 -0700644 input_free_device(idev);
Joseph Lai631b16e2011-06-27 13:26:53 -0700645 kfree(sensor);
646 return error;
647}
648
649/**
650 * mpu3050_remove - remove a sensor
651 * @client: i2c client of sensor being removed
652 *
653 * Our sensor is going away, clean up the resources.
654 */
655static int __devexit mpu3050_remove(struct i2c_client *client)
656{
657 struct mpu3050_sensor *sensor = i2c_get_clientdata(client);
658
659 pm_runtime_disable(&client->dev);
660 pm_runtime_set_suspended(&client->dev);
661
Wentao Xuc7769c02012-08-03 15:06:41 -0400662 if (client->irq)
663 free_irq(client->irq, sensor);
664
665 remove_sysfs_interfaces(&client->dev);
Joseph Lai631b16e2011-06-27 13:26:53 -0700666 input_unregister_device(sensor->idev);
Wentao Xuc7769c02012-08-03 15:06:41 -0400667
Joseph Lai631b16e2011-06-27 13:26:53 -0700668 kfree(sensor);
669
670 return 0;
671}
672
673#ifdef CONFIG_PM
674/**
675 * mpu3050_suspend - called on device suspend
676 * @dev: device being suspended
677 *
678 * Put the device into sleep mode before we suspend the machine.
679 */
680static int mpu3050_suspend(struct device *dev)
681{
682 struct i2c_client *client = to_i2c_client(dev);
683
684 mpu3050_set_power_mode(client, 0);
685
686 return 0;
687}
688
689/**
690 * mpu3050_resume - called on device resume
691 * @dev: device being resumed
692 *
693 * Put the device into powered mode on resume.
694 */
695static int mpu3050_resume(struct device *dev)
696{
697 struct i2c_client *client = to_i2c_client(dev);
698
699 mpu3050_set_power_mode(client, 1);
700 msleep(100); /* wait for gyro chip resume */
701
702 return 0;
703}
704#endif
705
706static UNIVERSAL_DEV_PM_OPS(mpu3050_pm, mpu3050_suspend, mpu3050_resume, NULL);
707
708static const struct i2c_device_id mpu3050_ids[] = {
709 { "mpu3050", 0 },
710 { }
711};
712MODULE_DEVICE_TABLE(i2c, mpu3050_ids);
713
Olof Johanssone9489812011-12-23 01:20:44 -0800714static const struct of_device_id mpu3050_of_match[] = {
715 { .compatible = "invn,mpu3050", },
716 { },
717};
718MODULE_DEVICE_TABLE(of, mpu3050_of_match);
719
Joseph Lai631b16e2011-06-27 13:26:53 -0700720static struct i2c_driver mpu3050_i2c_driver = {
721 .driver = {
722 .name = "mpu3050",
723 .owner = THIS_MODULE,
724 .pm = &mpu3050_pm,
Olof Johanssone9489812011-12-23 01:20:44 -0800725 .of_match_table = mpu3050_of_match,
Joseph Lai631b16e2011-06-27 13:26:53 -0700726 },
727 .probe = mpu3050_probe,
728 .remove = __devexit_p(mpu3050_remove),
729 .id_table = mpu3050_ids,
730};
731
Axel Lin1b92c1c2012-03-16 23:05:41 -0700732module_i2c_driver(mpu3050_i2c_driver);
Joseph Lai631b16e2011-06-27 13:26:53 -0700733
734MODULE_AUTHOR("Wistron Corp.");
735MODULE_DESCRIPTION("MPU3050 Tri-axis gyroscope driver");
736MODULE_LICENSE("GPL");