Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
mpu3050.c
Go to the documentation of this file.
1 /*
2  * MPU3050 Tri-axis gyroscope driver
3  *
4  * Copyright (C) 2011 Wistron Co.Ltd
5  * Joseph Lai <[email protected]>
6  *
7  * Trimmed down by Alan Cox <[email protected]> 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>
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 /* Register map */
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 /* Register bits */
64 
65 /* DLPF_FS_SYNC */
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 /* INT_CFG */
90 #define MPU3050_RAW_RDY_EN 0x01
91 #define MPU3050_MPU_RDY_EN 0x02
92 #define MPU3050_LATCH_INT_EN 0x04
93 /* PWR_MGM */
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 
105 struct axis_data {
109 };
110 
113  struct device *dev;
114  struct input_dev *idev;
115 };
116 
125 static int mpu3050_xyz_read_reg(struct i2c_client *client,
126  u8 *buffer, int length)
127 {
128  /*
129  * Annoying we can't make this const because the i2c layer doesn't
130  * declare input buffers const.
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 
158 static 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 
178 static 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) |
187 }
188 
197 static 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  /* Enable interrupts */
209  if (error < 0) {
210  pm_runtime_put(sensor->dev);
211  return error;
212  }
213 
214  return 0;
215 }
216 
224 static 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 
239 static 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 
260 static int __devinit mpu3050_hw_init(struct mpu3050_sensor *sensor)
261 {
262  struct i2c_client *client = sensor->client;
263  int ret;
264  u8 reg;
265 
266  /* Reset */
269  if (ret < 0)
270  return ret;
271 
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  /* Output frequency divider. The poll interval */
285  if (ret < 0)
286  return ret;
287 
288  /* Set low pass filter and full scale */
290  reg |= MPU3050_DLPF_CFG_42HZ << 3;
291  reg |= MPU3050_EXT_SYNC_NONE << 5;
293  if (ret < 0)
294  return ret;
295 
296  return 0;
297 }
298 
309 static int __devinit 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 
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,
355  input_set_abs_params(idev, ABS_Y,
357  input_set_abs_params(idev, ABS_Z,
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,
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);
386 
387  return 0;
388 
389 err_free_irq:
390  free_irq(client->irq, sensor);
391 err_pm_set_suspended:
392  pm_runtime_set_suspended(&client->dev);
393 err_free_mem:
394  input_free_device(idev);
395  kfree(sensor);
396  return error;
397 }
398 
405 static int __devexit 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 
426 static 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 
441 static 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); /* wait for gyro chip resume */
447 
448  return 0;
449 }
450 #endif
451 
452 static UNIVERSAL_DEV_PM_OPS(mpu3050_pm, mpu3050_suspend, mpu3050_resume, NULL);
453 
454 static const struct i2c_device_id mpu3050_ids[] = {
455  { "mpu3050", 0 },
456  { }
457 };
458 MODULE_DEVICE_TABLE(i2c, mpu3050_ids);
459 
460 static const struct of_device_id mpu3050_of_match[] = {
461  { .compatible = "invn,mpu3050", },
462  { },
463 };
464 MODULE_DEVICE_TABLE(of, mpu3050_of_match);
465 
466 static 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 = __devexit_p(mpu3050_remove),
475  .id_table = mpu3050_ids,
476 };
477 
478 module_i2c_driver(mpu3050_i2c_driver);
479 
480 MODULE_AUTHOR("Wistron Corp.");
481 MODULE_DESCRIPTION("MPU3050 Tri-axis gyroscope driver");
482 MODULE_LICENSE("GPL");