1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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>
43
44#define MPU3050_CHIP_ID 0x69
45
46#define MPU3050_AUTO_DELAY 1000
47
48#define MPU3050_MIN_VALUE -32768
49#define MPU3050_MAX_VALUE 32767
50
51#define MPU3050_DEFAULT_POLL_INTERVAL 200
52#define MPU3050_DEFAULT_FS_RANGE 3
53
54
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
62
63
64
65
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
89
90#define MPU3050_RAW_RDY_EN 0x01
91#define MPU3050_MPU_RDY_EN 0x02
92#define MPU3050_LATCH_INT_EN 0x04
93
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
104
105struct axis_data {
106 s16 x;
107 s16 y;
108 s16 z;
109};
110
111struct mpu3050_sensor {
112 struct i2c_client *client;
113 struct device *dev;
114 struct input_dev *idev;
115};
116
117
118
119
120
121
122
123
124
125static int mpu3050_xyz_read_reg(struct i2c_client *client,
126 u8 *buffer, int length)
127{
128
129
130
131
132 char cmd = MPU3050_XOUT_H;
133 struct i2c_msg msg[] = {
134 {
135 .addr = client->addr,
136 .flags = 0,
137 .len = 1,
138 .buf = &cmd,
139 },
140 {
141 .addr = client->addr,
142 .flags = I2C_M_RD,
143 .len = length,
144 .buf = buffer,
145 },
146 };
147
148 return i2c_transfer(client->adapter, msg, 2);
149}
150
151
152
153
154
155
156
157
158static void mpu3050_read_xyz(struct i2c_client *client,
159 struct axis_data *coords)
160{
161 u16 buffer[3];
162
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);
169}
170
171
172
173
174
175
176
177
178static void mpu3050_set_power_mode(struct i2c_client *client, u8 val)
179{
180 u8 value;
181
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);
187}
188
189
190
191
192
193
194
195
196
197static int mpu3050_input_open(struct input_dev *input)
198{
199 struct mpu3050_sensor *sensor = input_get_drvdata(input);
200 int error;
201
202 pm_runtime_get(sensor->dev);
203
204
205 error = i2c_smbus_write_byte_data(sensor->client, MPU3050_INT_CFG,
206 MPU3050_LATCH_INT_EN |
207 MPU3050_RAW_RDY_EN |
208 MPU3050_MPU_RDY_EN);
209 if (error < 0) {
210 pm_runtime_put(sensor->dev);
211 return error;
212 }
213
214 return 0;
215}
216
217
218
219
220
221
222
223
224static void mpu3050_input_close(struct input_dev *input)
225{
226 struct mpu3050_sensor *sensor = input_get_drvdata(input);
227
228 pm_runtime_put(sensor->dev);
229}
230
231
232
233
234
235
236
237
238
239static irqreturn_t mpu3050_interrupt_thread(int irq, void *data)
240{
241 struct mpu3050_sensor *sensor = data;
242 struct axis_data axis;
243
244 mpu3050_read_xyz(sensor->client, &axis);
245
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);
250
251 return IRQ_HANDLED;
252}
253
254
255
256
257
258
259
260static int mpu3050_hw_init(struct mpu3050_sensor *sensor)
261{
262 struct i2c_client *client = sensor->client;
263 int ret;
264 u8 reg;
265
266
267 ret = i2c_smbus_write_byte_data(client, MPU3050_PWR_MGM,
268 MPU3050_PWR_MGM_RESET);
269 if (ret < 0)
270 return ret;
271
272 ret = i2c_smbus_read_byte_data(client, MPU3050_PWR_MGM);
273 if (ret < 0)
274 return ret;
275
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);
279 if (ret < 0)
280 return ret;
281
282
283 ret = i2c_smbus_write_byte_data(client, MPU3050_SMPLRT_DIV,
284 MPU3050_DEFAULT_POLL_INTERVAL - 1);
285 if (ret < 0)
286 return ret;
287
288
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);
293 if (ret < 0)
294 return ret;
295
296 return 0;
297}
298
299
300
301
302
303
304
305
306
307
308
309static int mpu3050_probe(struct i2c_client *client,
310 const struct i2c_device_id *id)
311{
312 struct mpu3050_sensor *sensor;
313 struct input_dev *idev;
314 int ret;
315 int error;
316
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");
321 error = -ENOMEM;
322 goto err_free_mem;
323 }
324
325 sensor->client = client;
326 sensor->dev = &client->dev;
327 sensor->idev = idev;
328
329 mpu3050_set_power_mode(client, 1);
330 msleep(10);
331
332 ret = i2c_smbus_read_byte_data(client, MPU3050_CHIP_ID_REG);
333 if (ret < 0) {
334 dev_err(&client->dev, "failed to detect device\n");
335 error = -ENXIO;
336 goto err_free_mem;
337 }
338
339 if (ret != MPU3050_CHIP_ID) {
340 dev_err(&client->dev, "unsupported chip id\n");
341 error = -ENXIO;
342 goto err_free_mem;
343 }
344
345 idev->name = "MPU3050";
346 idev->id.bustype = BUS_I2C;
347 idev->dev.parent = &client->dev;
348
349 idev->open = mpu3050_input_open;
350 idev->close = mpu3050_input_close;
351
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);
359
360 input_set_drvdata(idev, sensor);
361
362 pm_runtime_set_active(&client->dev);
363
364 error = mpu3050_hw_init(sensor);
365 if (error)
366 goto err_pm_set_suspended;
367
368 error = request_threaded_irq(client->irq,
369 NULL, mpu3050_interrupt_thread,
370 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
371 "mpu3050", sensor);
372 if (error) {
373 dev_err(&client->dev,
374 "can't get IRQ %d, error %d\n", client->irq, error);
375 goto err_pm_set_suspended;
376 }
377
378 error = input_register_device(idev);
379 if (error) {
380 dev_err(&client->dev, "failed to register input device\n");
381 goto err_free_irq;
382 }
383
384 pm_runtime_enable(&client->dev);
385 pm_runtime_set_autosuspend_delay(&client->dev, MPU3050_AUTO_DELAY);
386
387 return 0;
388
389err_free_irq:
390 free_irq(client->irq, sensor);
391err_pm_set_suspended:
392 pm_runtime_set_suspended(&client->dev);
393err_free_mem:
394 input_free_device(idev);
395 kfree(sensor);
396 return error;
397}
398
399
400
401
402
403
404
405static int mpu3050_remove(struct i2c_client *client)
406{
407 struct mpu3050_sensor *sensor = i2c_get_clientdata(client);
408
409 pm_runtime_disable(&client->dev);
410 pm_runtime_set_suspended(&client->dev);
411
412 free_irq(client->irq, sensor);
413 input_unregister_device(sensor->idev);
414 kfree(sensor);
415
416 return 0;
417}
418
419#ifdef CONFIG_PM
420
421
422
423
424
425
426static int mpu3050_suspend(struct device *dev)
427{
428 struct i2c_client *client = to_i2c_client(dev);
429
430 mpu3050_set_power_mode(client, 0);
431
432 return 0;
433}
434
435
436
437
438
439
440
441static int mpu3050_resume(struct device *dev)
442{
443 struct i2c_client *client = to_i2c_client(dev);
444
445 mpu3050_set_power_mode(client, 1);
446 msleep(100);
447
448 return 0;
449}
450#endif
451
452static UNIVERSAL_DEV_PM_OPS(mpu3050_pm, mpu3050_suspend, mpu3050_resume, NULL);
453
454static const struct i2c_device_id mpu3050_ids[] = {
455 { "mpu3050", 0 },
456 { }
457};
458MODULE_DEVICE_TABLE(i2c, mpu3050_ids);
459
460static const struct of_device_id mpu3050_of_match[] = {
461 { .compatible = "invn,mpu3050", },
462 { },
463};
464MODULE_DEVICE_TABLE(of, mpu3050_of_match);
465
466static struct i2c_driver mpu3050_i2c_driver = {
467 .driver = {
468 .name = "mpu3050",
469 .owner = THIS_MODULE,
470 .pm = &mpu3050_pm,
471 .of_match_table = mpu3050_of_match,
472 },
473 .probe = mpu3050_probe,
474 .remove = mpu3050_remove,
475 .id_table = mpu3050_ids,
476};
477
478module_i2c_driver(mpu3050_i2c_driver);
479
480MODULE_AUTHOR("Wistron Corp.");
481MODULE_DESCRIPTION("MPU3050 Tri-axis gyroscope driver");
482MODULE_LICENSE("GPL");
483