2 * MPU3050 Tri-axis gyroscope driver
4 * Copyright (C) 2011 Wistron Co.Ltd
5 * Joseph Lai <joseph_lai@wistron.com>
7 * Trimmed down by Alan Cox <alan@linux.intel.com> to produce this version
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
15 * This program is based on bma023.c.
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.
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.
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.
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>
44 #define MPU3050_CHIP_ID 0x69
46 #define MPU3050_AUTO_DELAY 1000
48 #define MPU3050_MIN_VALUE -32768
49 #define MPU3050_MAX_VALUE 32767
51 #define MPU3050_DEFAULT_POLL_INTERVAL 200
52 #define MPU3050_DEFAULT_FS_RANGE 3
55 #define MPU3050_CHIP_ID_REG 0x00
56 #define MPU3050_SMPLRT_DIV 0x15
57 #define MPU3050_DLPF_FS_SYNC 0x16
58 #define MPU3050_INT_CFG 0x17
59 #define MPU3050_XOUT_H 0x1D
60 #define MPU3050_PWR_MGM 0x3E
61 #define MPU3050_PWR_MGM_POS 6
66 #define MPU3050_EXT_SYNC_NONE 0x00
67 #define MPU3050_EXT_SYNC_TEMP 0x20
68 #define MPU3050_EXT_SYNC_GYROX 0x40
69 #define MPU3050_EXT_SYNC_GYROY 0x60
70 #define MPU3050_EXT_SYNC_GYROZ 0x80
71 #define MPU3050_EXT_SYNC_ACCELX 0xA0
72 #define MPU3050_EXT_SYNC_ACCELY 0xC0
73 #define MPU3050_EXT_SYNC_ACCELZ 0xE0
74 #define MPU3050_EXT_SYNC_MASK 0xE0
75 #define MPU3050_FS_250DPS 0x00
76 #define MPU3050_FS_500DPS 0x08
77 #define MPU3050_FS_1000DPS 0x10
78 #define MPU3050_FS_2000DPS 0x18
79 #define MPU3050_FS_MASK 0x18
80 #define MPU3050_DLPF_CFG_256HZ_NOLPF2 0x00
81 #define MPU3050_DLPF_CFG_188HZ 0x01
82 #define MPU3050_DLPF_CFG_98HZ 0x02
83 #define MPU3050_DLPF_CFG_42HZ 0x03
84 #define MPU3050_DLPF_CFG_20HZ 0x04
85 #define MPU3050_DLPF_CFG_10HZ 0x05
86 #define MPU3050_DLPF_CFG_5HZ 0x06
87 #define MPU3050_DLPF_CFG_2100HZ_NOLPF 0x07
88 #define MPU3050_DLPF_CFG_MASK 0x07
90 #define MPU3050_RAW_RDY_EN 0x01
91 #define MPU3050_MPU_RDY_EN 0x02
92 #define MPU3050_LATCH_INT_EN 0x04
94 #define MPU3050_PWR_MGM_PLL_X 0x01
95 #define MPU3050_PWR_MGM_PLL_Y 0x02
96 #define MPU3050_PWR_MGM_PLL_Z 0x03
97 #define MPU3050_PWR_MGM_CLKSEL 0x07
98 #define MPU3050_PWR_MGM_STBY_ZG 0x08
99 #define MPU3050_PWR_MGM_STBY_YG 0x10
100 #define MPU3050_PWR_MGM_STBY_XG 0x20
101 #define MPU3050_PWR_MGM_SLEEP 0x40
102 #define MPU3050_PWR_MGM_RESET 0x80
103 #define MPU3050_PWR_MGM_MASK 0x40
111 struct mpu3050_sensor
{
112 struct i2c_client
*client
;
114 struct input_dev
*idev
;
118 * mpu3050_xyz_read_reg - read the axes values
119 * @buffer: provide register addr and get register
120 * @length: length of register
122 * Reads the register values in one transaction or returns a negative
123 * error code on failure.
125 static int mpu3050_xyz_read_reg(struct i2c_client
*client
,
126 u8
*buffer
, int length
)
129 * Annoying we can't make this const because the i2c layer doesn't
130 * declare input buffers const.
132 char cmd
= MPU3050_XOUT_H
;
133 struct i2c_msg msg
[] = {
135 .addr
= client
->addr
,
141 .addr
= client
->addr
,
148 return i2c_transfer(client
->adapter
, msg
, 2);
152 * mpu3050_read_xyz - get co-ordinates from device
153 * @client: i2c address of sensor
154 * @coords: co-ordinates to update
156 * Return the converted X Y and Z co-ordinates from the sensor device
158 static void mpu3050_read_xyz(struct i2c_client
*client
,
159 struct axis_data
*coords
)
163 mpu3050_xyz_read_reg(client
, (u8
*)buffer
, 6);
164 coords
->x
= be16_to_cpu(buffer
[0]);
165 coords
->y
= be16_to_cpu(buffer
[1]);
166 coords
->z
= be16_to_cpu(buffer
[2]);
167 dev_dbg(&client
->dev
, "%s: x %d, y %d, z %d\n", __func__
,
168 coords
->x
, coords
->y
, coords
->z
);
172 * mpu3050_set_power_mode - set the power mode
173 * @client: i2c client for the sensor
174 * @val: value to switch on/off of power, 1: normal power, 0: low power
176 * Put device to normal-power mode or low-power mode.
178 static void mpu3050_set_power_mode(struct i2c_client
*client
, u8 val
)
182 value
= i2c_smbus_read_byte_data(client
, MPU3050_PWR_MGM
);
183 value
= (value
& ~MPU3050_PWR_MGM_MASK
) |
184 (((val
<< MPU3050_PWR_MGM_POS
) & MPU3050_PWR_MGM_MASK
) ^
185 MPU3050_PWR_MGM_MASK
);
186 i2c_smbus_write_byte_data(client
, MPU3050_PWR_MGM
, value
);
190 * mpu3050_input_open - called on input event open
191 * @input: input dev of opened device
193 * The input layer calls this function when input event is opened. The
194 * function will push the device to resume. Then, the device is ready
197 static int mpu3050_input_open(struct input_dev
*input
)
199 struct mpu3050_sensor
*sensor
= input_get_drvdata(input
);
202 pm_runtime_get(sensor
->dev
);
204 /* Enable interrupts */
205 error
= i2c_smbus_write_byte_data(sensor
->client
, MPU3050_INT_CFG
,
206 MPU3050_LATCH_INT_EN
|
210 pm_runtime_put(sensor
->dev
);
218 * mpu3050_input_close - called on input event close
219 * @input: input dev of closed device
221 * The input layer calls this function when input event is closed. The
222 * function will push the device to suspend.
224 static void mpu3050_input_close(struct input_dev
*input
)
226 struct mpu3050_sensor
*sensor
= input_get_drvdata(input
);
228 pm_runtime_put(sensor
->dev
);
232 * mpu3050_interrupt_thread - handle an IRQ
233 * @irq: interrupt numner
236 * Called by the kernel single threaded after an interrupt occurs. Read
237 * the sensor data and generate an input event for it.
239 static irqreturn_t
mpu3050_interrupt_thread(int irq
, void *data
)
241 struct mpu3050_sensor
*sensor
= data
;
242 struct axis_data axis
;
244 mpu3050_read_xyz(sensor
->client
, &axis
);
246 input_report_abs(sensor
->idev
, ABS_X
, axis
.x
);
247 input_report_abs(sensor
->idev
, ABS_Y
, axis
.y
);
248 input_report_abs(sensor
->idev
, ABS_Z
, axis
.z
);
249 input_sync(sensor
->idev
);
255 * mpu3050_hw_init - initialize hardware
256 * @sensor: the sensor
258 * Called during device probe; configures the sampling method.
260 static int __devinit
mpu3050_hw_init(struct mpu3050_sensor
*sensor
)
262 struct i2c_client
*client
= sensor
->client
;
267 ret
= i2c_smbus_write_byte_data(client
, MPU3050_PWR_MGM
,
268 MPU3050_PWR_MGM_RESET
);
272 ret
= i2c_smbus_read_byte_data(client
, MPU3050_PWR_MGM
);
276 ret
&= ~MPU3050_PWR_MGM_CLKSEL
;
277 ret
|= MPU3050_PWR_MGM_PLL_Z
;
278 ret
= i2c_smbus_write_byte_data(client
, MPU3050_PWR_MGM
, ret
);
282 /* Output frequency divider. The poll interval */
283 ret
= i2c_smbus_write_byte_data(client
, MPU3050_SMPLRT_DIV
,
284 MPU3050_DEFAULT_POLL_INTERVAL
- 1);
288 /* Set low pass filter and full scale */
289 reg
= MPU3050_DEFAULT_FS_RANGE
;
290 reg
|= MPU3050_DLPF_CFG_42HZ
<< 3;
291 reg
|= MPU3050_EXT_SYNC_NONE
<< 5;
292 ret
= i2c_smbus_write_byte_data(client
, MPU3050_DLPF_FS_SYNC
, reg
);
300 * mpu3050_probe - device detection callback
301 * @client: i2c client of found device
302 * @id: id match information
304 * The I2C layer calls us when it believes a sensor is present at this
305 * address. Probe to see if this is correct and to validate the device.
307 * If present install the relevant sysfs interfaces and input device.
309 static int __devinit
mpu3050_probe(struct i2c_client
*client
,
310 const struct i2c_device_id
*id
)
312 struct mpu3050_sensor
*sensor
;
313 struct input_dev
*idev
;
317 sensor
= kzalloc(sizeof(struct mpu3050_sensor
), GFP_KERNEL
);
318 idev
= input_allocate_device();
319 if (!sensor
|| !idev
) {
320 dev_err(&client
->dev
, "failed to allocate driver data\n");
325 sensor
->client
= client
;
326 sensor
->dev
= &client
->dev
;
329 mpu3050_set_power_mode(client
, 1);
332 ret
= i2c_smbus_read_byte_data(client
, MPU3050_CHIP_ID_REG
);
334 dev_err(&client
->dev
, "failed to detect device\n");
339 if (ret
!= MPU3050_CHIP_ID
) {
340 dev_err(&client
->dev
, "unsupported chip id\n");
345 idev
->name
= "MPU3050";
346 idev
->id
.bustype
= BUS_I2C
;
347 idev
->dev
.parent
= &client
->dev
;
349 idev
->open
= mpu3050_input_open
;
350 idev
->close
= mpu3050_input_close
;
352 __set_bit(EV_ABS
, idev
->evbit
);
353 input_set_abs_params(idev
, ABS_X
,
354 MPU3050_MIN_VALUE
, MPU3050_MAX_VALUE
, 0, 0);
355 input_set_abs_params(idev
, ABS_Y
,
356 MPU3050_MIN_VALUE
, MPU3050_MAX_VALUE
, 0, 0);
357 input_set_abs_params(idev
, ABS_Z
,
358 MPU3050_MIN_VALUE
, MPU3050_MAX_VALUE
, 0, 0);
360 input_set_drvdata(idev
, sensor
);
362 pm_runtime_set_active(&client
->dev
);
364 error
= mpu3050_hw_init(sensor
);
366 goto err_pm_set_suspended
;
368 error
= request_threaded_irq(client
->irq
,
369 NULL
, mpu3050_interrupt_thread
,
373 dev_err(&client
->dev
,
374 "can't get IRQ %d, error %d\n", client
->irq
, error
);
375 goto err_pm_set_suspended
;
378 error
= input_register_device(idev
);
380 dev_err(&client
->dev
, "failed to register input device\n");
384 pm_runtime_enable(&client
->dev
);
385 pm_runtime_set_autosuspend_delay(&client
->dev
, MPU3050_AUTO_DELAY
);
390 free_irq(client
->irq
, sensor
);
391 err_pm_set_suspended
:
392 pm_runtime_set_suspended(&client
->dev
);
394 input_free_device(idev
);
400 * mpu3050_remove - remove a sensor
401 * @client: i2c client of sensor being removed
403 * Our sensor is going away, clean up the resources.
405 static int __devexit
mpu3050_remove(struct i2c_client
*client
)
407 struct mpu3050_sensor
*sensor
= i2c_get_clientdata(client
);
409 pm_runtime_disable(&client
->dev
);
410 pm_runtime_set_suspended(&client
->dev
);
412 free_irq(client
->irq
, sensor
);
413 input_unregister_device(sensor
->idev
);
421 * mpu3050_suspend - called on device suspend
422 * @dev: device being suspended
424 * Put the device into sleep mode before we suspend the machine.
426 static int mpu3050_suspend(struct device
*dev
)
428 struct i2c_client
*client
= to_i2c_client(dev
);
430 mpu3050_set_power_mode(client
, 0);
436 * mpu3050_resume - called on device resume
437 * @dev: device being resumed
439 * Put the device into powered mode on resume.
441 static int mpu3050_resume(struct device
*dev
)
443 struct i2c_client
*client
= to_i2c_client(dev
);
445 mpu3050_set_power_mode(client
, 1);
446 msleep(100); /* wait for gyro chip resume */
452 static UNIVERSAL_DEV_PM_OPS(mpu3050_pm
, mpu3050_suspend
, mpu3050_resume
, NULL
);
454 static const struct i2c_device_id mpu3050_ids
[] = {
458 MODULE_DEVICE_TABLE(i2c
, mpu3050_ids
);
460 static const struct of_device_id mpu3050_of_match
[] = {
461 { .compatible
= "invn,mpu3050", },
464 MODULE_DEVICE_TABLE(of
, mpu3050_of_match
);
466 static struct i2c_driver mpu3050_i2c_driver
= {
469 .owner
= THIS_MODULE
,
471 .of_match_table
= mpu3050_of_match
,
473 .probe
= mpu3050_probe
,
474 .remove
= __devexit_p(mpu3050_remove
),
475 .id_table
= mpu3050_ids
,
478 module_i2c_driver(mpu3050_i2c_driver
);
480 MODULE_AUTHOR("Wistron Corp.");
481 MODULE_DESCRIPTION("MPU3050 Tri-axis gyroscope driver");
482 MODULE_LICENSE("GPL");