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/interrupt.h>
34#include <linux/platform_device.h>
35#include <linux/mutex.h>
36#include <linux/err.h>
37#include <linux/i2c.h>
38#include <linux/input.h>
39#include <linux/delay.h>
40#include <linux/slab.h>
41#include <linux/pm_runtime.h>
42
43#define MPU3050_CHIP_ID 0x69
44
45#define MPU3050_AUTO_DELAY 1000
46
47#define MPU3050_MIN_VALUE -32768
48#define MPU3050_MAX_VALUE 32767
49
50#define MPU3050_DEFAULT_POLL_INTERVAL 200
51#define MPU3050_DEFAULT_FS_RANGE 3
52
53
54#define MPU3050_CHIP_ID_REG 0x00
55#define MPU3050_SMPLRT_DIV 0x15
56#define MPU3050_DLPF_FS_SYNC 0x16
57#define MPU3050_INT_CFG 0x17
58#define MPU3050_XOUT_H 0x1D
59#define MPU3050_PWR_MGM 0x3E
60#define MPU3050_PWR_MGM_POS 6
61
62
63
64
65#define MPU3050_EXT_SYNC_NONE 0x00
66#define MPU3050_EXT_SYNC_TEMP 0x20
67#define MPU3050_EXT_SYNC_GYROX 0x40
68#define MPU3050_EXT_SYNC_GYROY 0x60
69#define MPU3050_EXT_SYNC_GYROZ 0x80
70#define MPU3050_EXT_SYNC_ACCELX 0xA0
71#define MPU3050_EXT_SYNC_ACCELY 0xC0
72#define MPU3050_EXT_SYNC_ACCELZ 0xE0
73#define MPU3050_EXT_SYNC_MASK 0xE0
74#define MPU3050_FS_250DPS 0x00
75#define MPU3050_FS_500DPS 0x08
76#define MPU3050_FS_1000DPS 0x10
77#define MPU3050_FS_2000DPS 0x18
78#define MPU3050_FS_MASK 0x18
79#define MPU3050_DLPF_CFG_256HZ_NOLPF2 0x00
80#define MPU3050_DLPF_CFG_188HZ 0x01
81#define MPU3050_DLPF_CFG_98HZ 0x02
82#define MPU3050_DLPF_CFG_42HZ 0x03
83#define MPU3050_DLPF_CFG_20HZ 0x04
84#define MPU3050_DLPF_CFG_10HZ 0x05
85#define MPU3050_DLPF_CFG_5HZ 0x06
86#define MPU3050_DLPF_CFG_2100HZ_NOLPF 0x07
87#define MPU3050_DLPF_CFG_MASK 0x07
88
89#define MPU3050_RAW_RDY_EN 0x01
90#define MPU3050_MPU_RDY_EN 0x02
91#define MPU3050_LATCH_INT_EN 0x04
92
93#define MPU3050_PWR_MGM_PLL_X 0x01
94#define MPU3050_PWR_MGM_PLL_Y 0x02
95#define MPU3050_PWR_MGM_PLL_Z 0x03
96#define MPU3050_PWR_MGM_CLKSEL 0x07
97#define MPU3050_PWR_MGM_STBY_ZG 0x08
98#define MPU3050_PWR_MGM_STBY_YG 0x10
99#define MPU3050_PWR_MGM_STBY_XG 0x20
100#define MPU3050_PWR_MGM_SLEEP 0x40
101#define MPU3050_PWR_MGM_RESET 0x80
102#define MPU3050_PWR_MGM_MASK 0x40
103
104struct axis_data {
105 s16 x;
106 s16 y;
107 s16 z;
108};
109
110struct mpu3050_sensor {
111 struct i2c_client *client;
112 struct device *dev;
113 struct input_dev *idev;
114};
115
116
117
118
119
120
121
122
123
124static int mpu3050_xyz_read_reg(struct i2c_client *client,
125 u8 *buffer, int length)
126{
127
128
129
130
131 char cmd = MPU3050_XOUT_H;
132 struct i2c_msg msg[] = {
133 {
134 .addr = client->addr,
135 .flags = 0,
136 .len = 1,
137 .buf = &cmd,
138 },
139 {
140 .addr = client->addr,
141 .flags = I2C_M_RD,
142 .len = length,
143 .buf = buffer,
144 },
145 };
146
147 return i2c_transfer(client->adapter, msg, 2);
148}
149
150
151
152
153
154
155
156
157static void mpu3050_read_xyz(struct i2c_client *client,
158 struct axis_data *coords)
159{
160 u16 buffer[3];
161
162 mpu3050_xyz_read_reg(client, (u8 *)buffer, 6);
163 coords->x = be16_to_cpu(buffer[0]);
164 coords->y = be16_to_cpu(buffer[1]);
165 coords->z = be16_to_cpu(buffer[2]);
166 dev_dbg(&client->dev, "%s: x %d, y %d, z %d\n", __func__,
167 coords->x, coords->y, coords->z);
168}
169
170
171
172
173
174
175
176
177static void mpu3050_set_power_mode(struct i2c_client *client, u8 val)
178{
179 u8 value;
180
181 value = i2c_smbus_read_byte_data(client, MPU3050_PWR_MGM);
182 value = (value & ~MPU3050_PWR_MGM_MASK) |
183 (((val << MPU3050_PWR_MGM_POS) & MPU3050_PWR_MGM_MASK) ^
184 MPU3050_PWR_MGM_MASK);
185 i2c_smbus_write_byte_data(client, MPU3050_PWR_MGM, value);
186}
187
188
189
190
191
192
193
194
195
196static int mpu3050_input_open(struct input_dev *input)
197{
198 struct mpu3050_sensor *sensor = input_get_drvdata(input);
199 int error;
200
201 pm_runtime_get(sensor->dev);
202
203
204 error = i2c_smbus_write_byte_data(sensor->client, MPU3050_INT_CFG,
205 MPU3050_LATCH_INT_EN |
206 MPU3050_RAW_RDY_EN |
207 MPU3050_MPU_RDY_EN);
208 if (error < 0) {
209 pm_runtime_put(sensor->dev);
210 return error;
211 }
212
213 return 0;
214}
215
216
217
218
219
220
221
222
223static void mpu3050_input_close(struct input_dev *input)
224{
225 struct mpu3050_sensor *sensor = input_get_drvdata(input);
226
227 pm_runtime_put(sensor->dev);
228}
229
230
231
232
233
234
235
236
237
238static irqreturn_t mpu3050_interrupt_thread(int irq, void *data)
239{
240 struct mpu3050_sensor *sensor = data;
241 struct axis_data axis;
242
243 mpu3050_read_xyz(sensor->client, &axis);
244
245 input_report_abs(sensor->idev, ABS_X, axis.x);
246 input_report_abs(sensor->idev, ABS_Y, axis.y);
247 input_report_abs(sensor->idev, ABS_Z, axis.z);
248 input_sync(sensor->idev);
249
250 return IRQ_HANDLED;
251}
252
253
254
255
256
257
258
259static int mpu3050_hw_init(struct mpu3050_sensor *sensor)
260{
261 struct i2c_client *client = sensor->client;
262 int ret;
263 u8 reg;
264
265
266 ret = i2c_smbus_write_byte_data(client, MPU3050_PWR_MGM,
267 MPU3050_PWR_MGM_RESET);
268 if (ret < 0)
269 return ret;
270
271 ret = i2c_smbus_read_byte_data(client, MPU3050_PWR_MGM);
272 if (ret < 0)
273 return ret;
274
275 ret &= ~MPU3050_PWR_MGM_CLKSEL;
276 ret |= MPU3050_PWR_MGM_PLL_Z;
277 ret = i2c_smbus_write_byte_data(client, MPU3050_PWR_MGM, ret);
278 if (ret < 0)
279 return ret;
280
281
282 ret = i2c_smbus_write_byte_data(client, MPU3050_SMPLRT_DIV,
283 MPU3050_DEFAULT_POLL_INTERVAL - 1);
284 if (ret < 0)
285 return ret;
286
287
288 reg = MPU3050_DEFAULT_FS_RANGE;
289 reg |= MPU3050_DLPF_CFG_42HZ << 3;
290 reg |= MPU3050_EXT_SYNC_NONE << 5;
291 ret = i2c_smbus_write_byte_data(client, MPU3050_DLPF_FS_SYNC, reg);
292 if (ret < 0)
293 return ret;
294
295 return 0;
296}
297
298
299
300
301
302
303
304
305
306
307
308static int mpu3050_probe(struct i2c_client *client,
309 const struct i2c_device_id *id)
310{
311 struct mpu3050_sensor *sensor;
312 struct input_dev *idev;
313 int ret;
314 int error;
315
316 sensor = kzalloc(sizeof(struct mpu3050_sensor), GFP_KERNEL);
317 idev = input_allocate_device();
318 if (!sensor || !idev) {
319 dev_err(&client->dev, "failed to allocate driver data\n");
320 error = -ENOMEM;
321 goto err_free_mem;
322 }
323
324 sensor->client = client;
325 sensor->dev = &client->dev;
326 sensor->idev = idev;
327
328 mpu3050_set_power_mode(client, 1);
329 msleep(10);
330
331 ret = i2c_smbus_read_byte_data(client, MPU3050_CHIP_ID_REG);
332 if (ret < 0) {
333 dev_err(&client->dev, "failed to detect device\n");
334 error = -ENXIO;
335 goto err_free_mem;
336 }
337
338 if (ret != MPU3050_CHIP_ID) {
339 dev_err(&client->dev, "unsupported chip id\n");
340 error = -ENXIO;
341 goto err_free_mem;
342 }
343
344 idev->name = "MPU3050";
345 idev->id.bustype = BUS_I2C;
346 idev->dev.parent = &client->dev;
347
348 idev->open = mpu3050_input_open;
349 idev->close = mpu3050_input_close;
350
351 __set_bit(EV_ABS, idev->evbit);
352 input_set_abs_params(idev, ABS_X,
353 MPU3050_MIN_VALUE, MPU3050_MAX_VALUE, 0, 0);
354 input_set_abs_params(idev, ABS_Y,
355 MPU3050_MIN_VALUE, MPU3050_MAX_VALUE, 0, 0);
356 input_set_abs_params(idev, ABS_Z,
357 MPU3050_MIN_VALUE, MPU3050_MAX_VALUE, 0, 0);
358
359 input_set_drvdata(idev, sensor);
360
361 pm_runtime_set_active(&client->dev);
362
363 error = mpu3050_hw_init(sensor);
364 if (error)
365 goto err_pm_set_suspended;
366
367 error = request_threaded_irq(client->irq,
368 NULL, mpu3050_interrupt_thread,
369 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
370 "mpu3050", sensor);
371 if (error) {
372 dev_err(&client->dev,
373 "can't get IRQ %d, error %d\n", client->irq, error);
374 goto err_pm_set_suspended;
375 }
376
377 error = input_register_device(idev);
378 if (error) {
379 dev_err(&client->dev, "failed to register input device\n");
380 goto err_free_irq;
381 }
382
383 pm_runtime_enable(&client->dev);
384 pm_runtime_set_autosuspend_delay(&client->dev, MPU3050_AUTO_DELAY);
385 i2c_set_clientdata(client, sensor);
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 .pm = &mpu3050_pm,
470 .of_match_table = mpu3050_of_match,
471 },
472 .probe = mpu3050_probe,
473 .remove = mpu3050_remove,
474 .id_table = mpu3050_ids,
475};
476
477module_i2c_driver(mpu3050_i2c_driver);
478
479MODULE_AUTHOR("Wistron Corp.");
480MODULE_DESCRIPTION("MPU3050 Tri-axis gyroscope driver");
481MODULE_LICENSE("GPL");
482