32 #include <linux/module.h>
38 #include <linux/i2c.h>
39 #include <linux/input.h>
41 #include <linux/slab.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
141 .addr = client->
addr,
163 mpu3050_xyz_read_reg(client, (
u8 *)buffer, 6);
167 dev_dbg(&client->
dev,
"%s: x %d, y %d, z %d\n", __func__,
168 coords->
x, coords->
y, coords->
z);
197 static int mpu3050_input_open(
struct input_dev *
input)
202 pm_runtime_get(sensor->
dev);
210 pm_runtime_put(sensor->
dev);
224 static void mpu3050_input_close(
struct input_dev *input)
228 pm_runtime_put(sensor->
dev);
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);
313 struct input_dev *
idev;
318 idev = input_allocate_device();
319 if (!sensor || !idev) {
320 dev_err(&client->
dev,
"failed to allocate driver data\n");
326 sensor->
dev = &client->
dev;
329 mpu3050_set_power_mode(client, 1);
334 dev_err(&client->
dev,
"failed to detect device\n");
340 dev_err(&client->
dev,
"unsupported chip id\n");
345 idev->name =
"MPU3050";
347 idev->dev.parent = &client->
dev;
349 idev->open = mpu3050_input_open;
350 idev->close = mpu3050_input_close;
353 input_set_abs_params(idev,
ABS_X,
355 input_set_abs_params(idev,
ABS_Y,
357 input_set_abs_params(idev,
ABS_Z,
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;
369 NULL, mpu3050_interrupt_thread,
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");
391 err_pm_set_suspended:
392 pm_runtime_set_suspended(&client->
dev);
394 input_free_device(idev);
409 pm_runtime_disable(&client->
dev);
410 pm_runtime_set_suspended(&client->
dev);
413 input_unregister_device(sensor->
idev);
426 static int mpu3050_suspend(
struct device *
dev)
430 mpu3050_set_power_mode(client, 0);
441 static int mpu3050_resume(
struct device *
dev)
445 mpu3050_set_power_mode(client, 1);
461 { .compatible =
"invn,mpu3050", },
466 static struct i2c_driver mpu3050_i2c_driver = {
471 .of_match_table = mpu3050_of_match,
473 .probe = mpu3050_probe,
475 .id_table = mpu3050_ids,