blob: 8b6e172c64e421788b4f1ba869f943749da4ac35 [file] [log] [blame]
Joseph Lai631b16e2011-06-27 13:26:53 -07001/*
2 * MPU3050 Tri-axis gyroscope driver
3 *
4 * Copyright (C) 2011 Wistron Co.Ltd
5 * Joseph Lai <joseph_lai@wistron.com>
6 *
7 * Trimmed down by Alan Cox <alan@linux.intel.com> 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>
Wentao Xudac9e602012-06-12 11:52:34 -040043#include <linux/input/mpu3050.h>
44#include <linux/regulator/consumer.h>
Joseph Lai631b16e2011-06-27 13:26:53 -070045
Joseph Lai631b16e2011-06-27 13:26:53 -070046#define MPU3050_CHIP_ID 0x69
Joseph Lai631b16e2011-06-27 13:26:53 -070047
48#define MPU3050_AUTO_DELAY 1000
49
50#define MPU3050_MIN_VALUE -32768
51#define MPU3050_MAX_VALUE 32767
52
Heikki Krogeruscd314fa2011-12-24 00:09:04 -080053#define MPU3050_DEFAULT_POLL_INTERVAL 200
54#define MPU3050_DEFAULT_FS_RANGE 3
55
56/* Register map */
57#define MPU3050_CHIP_ID_REG 0x00
58#define MPU3050_SMPLRT_DIV 0x15
59#define MPU3050_DLPF_FS_SYNC 0x16
60#define MPU3050_INT_CFG 0x17
61#define MPU3050_XOUT_H 0x1D
62#define MPU3050_PWR_MGM 0x3E
63#define MPU3050_PWR_MGM_POS 6
64
65/* Register bits */
66
67/* DLPF_FS_SYNC */
68#define MPU3050_EXT_SYNC_NONE 0x00
69#define MPU3050_EXT_SYNC_TEMP 0x20
70#define MPU3050_EXT_SYNC_GYROX 0x40
71#define MPU3050_EXT_SYNC_GYROY 0x60
72#define MPU3050_EXT_SYNC_GYROZ 0x80
73#define MPU3050_EXT_SYNC_ACCELX 0xA0
74#define MPU3050_EXT_SYNC_ACCELY 0xC0
75#define MPU3050_EXT_SYNC_ACCELZ 0xE0
76#define MPU3050_EXT_SYNC_MASK 0xE0
77#define MPU3050_FS_250DPS 0x00
78#define MPU3050_FS_500DPS 0x08
79#define MPU3050_FS_1000DPS 0x10
80#define MPU3050_FS_2000DPS 0x18
81#define MPU3050_FS_MASK 0x18
82#define MPU3050_DLPF_CFG_256HZ_NOLPF2 0x00
83#define MPU3050_DLPF_CFG_188HZ 0x01
84#define MPU3050_DLPF_CFG_98HZ 0x02
85#define MPU3050_DLPF_CFG_42HZ 0x03
86#define MPU3050_DLPF_CFG_20HZ 0x04
87#define MPU3050_DLPF_CFG_10HZ 0x05
88#define MPU3050_DLPF_CFG_5HZ 0x06
89#define MPU3050_DLPF_CFG_2100HZ_NOLPF 0x07
90#define MPU3050_DLPF_CFG_MASK 0x07
91/* INT_CFG */
92#define MPU3050_RAW_RDY_EN 0x01
93#define MPU3050_MPU_RDY_EN 0x02
94#define MPU3050_LATCH_INT_EN 0x04
95/* PWR_MGM */
96#define MPU3050_PWR_MGM_PLL_X 0x01
97#define MPU3050_PWR_MGM_PLL_Y 0x02
98#define MPU3050_PWR_MGM_PLL_Z 0x03
99#define MPU3050_PWR_MGM_CLKSEL 0x07
100#define MPU3050_PWR_MGM_STBY_ZG 0x08
101#define MPU3050_PWR_MGM_STBY_YG 0x10
102#define MPU3050_PWR_MGM_STBY_XG 0x20
103#define MPU3050_PWR_MGM_SLEEP 0x40
104#define MPU3050_PWR_MGM_RESET 0x80
105#define MPU3050_PWR_MGM_MASK 0x40
106
Joseph Lai631b16e2011-06-27 13:26:53 -0700107struct axis_data {
108 s16 x;
109 s16 y;
110 s16 z;
111};
112
113struct mpu3050_sensor {
114 struct i2c_client *client;
115 struct device *dev;
116 struct input_dev *idev;
Wentao Xudac9e602012-06-12 11:52:34 -0400117 struct mpu3050_gyro_platform_data *platform_data;
118 struct delayed_work input_work;
119 u32 use_poll;
Joseph Lai631b16e2011-06-27 13:26:53 -0700120};
121
Wentao Xudac9e602012-06-12 11:52:34 -0400122struct sensor_regulator {
123 struct regulator *vreg;
124 const char *name;
125 u32 min_uV;
126 u32 max_uV;
127};
128
129struct sensor_regulator mpu_vreg[] = {
130 {NULL, "vdd", 2100000, 3600000},
131 {NULL, "vlogic", 1800000, 1800000},
132};
133
134static int mpu3050_config_regulator(struct i2c_client *client, bool on)
135{
136 int rc = 0, i;
137 int num_reg = sizeof(mpu_vreg) / sizeof(struct sensor_regulator);
138
139 if (on) {
140 for (i = 0; i < num_reg; i++) {
141 mpu_vreg[i].vreg = regulator_get(&client->dev,
142 mpu_vreg[i].name);
143 if (IS_ERR(mpu_vreg[i].vreg)) {
144 rc = PTR_ERR(mpu_vreg[i].vreg);
145 pr_err("%s:regulator get failed rc=%d\n",
146 __func__, rc);
147 goto error_vdd;
148 }
149
150 if (regulator_count_voltages(mpu_vreg[i].vreg) > 0) {
151 rc = regulator_set_voltage(mpu_vreg[i].vreg,
152 mpu_vreg[i].min_uV, mpu_vreg[i].max_uV);
153 if (rc) {
154 pr_err("%s:set_voltage failed rc=%d\n",
155 __func__, rc);
156 regulator_put(mpu_vreg[i].vreg);
157 goto error_vdd;
158 }
159 }
160
161 rc = regulator_enable(mpu_vreg[i].vreg);
162 if (rc) {
163 pr_err("%s: regulator_enable failed rc =%d\n",
164 __func__,
165 rc);
166
167 if (regulator_count_voltages(
168 mpu_vreg[i].vreg) > 0) {
169 regulator_set_voltage(mpu_vreg[i].vreg,
170 0, mpu_vreg[i].max_uV);
171 }
172 regulator_put(mpu_vreg[i].vreg);
173 goto error_vdd;
174 }
175 }
176 return rc;
177 } else {
178 i = num_reg;
179 }
180error_vdd:
181 while (--i >= 0) {
182 if (regulator_count_voltages(mpu_vreg[i].vreg) > 0) {
183 regulator_set_voltage(mpu_vreg[i].vreg, 0,
184 mpu_vreg[i].max_uV);
185 }
186 regulator_disable(mpu_vreg[i].vreg);
187 regulator_put(mpu_vreg[i].vreg);
188 }
189 return rc;
190}
191
Joseph Lai631b16e2011-06-27 13:26:53 -0700192/**
193 * mpu3050_xyz_read_reg - read the axes values
194 * @buffer: provide register addr and get register
195 * @length: length of register
196 *
197 * Reads the register values in one transaction or returns a negative
198 * error code on failure.
199 */
200static int mpu3050_xyz_read_reg(struct i2c_client *client,
201 u8 *buffer, int length)
202{
203 /*
204 * Annoying we can't make this const because the i2c layer doesn't
205 * declare input buffers const.
206 */
207 char cmd = MPU3050_XOUT_H;
208 struct i2c_msg msg[] = {
209 {
210 .addr = client->addr,
211 .flags = 0,
212 .len = 1,
213 .buf = &cmd,
214 },
215 {
216 .addr = client->addr,
217 .flags = I2C_M_RD,
218 .len = length,
219 .buf = buffer,
220 },
221 };
222
223 return i2c_transfer(client->adapter, msg, 2);
224}
225
226/**
227 * mpu3050_read_xyz - get co-ordinates from device
228 * @client: i2c address of sensor
229 * @coords: co-ordinates to update
230 *
231 * Return the converted X Y and Z co-ordinates from the sensor device
232 */
233static void mpu3050_read_xyz(struct i2c_client *client,
234 struct axis_data *coords)
235{
236 u16 buffer[3];
237
238 mpu3050_xyz_read_reg(client, (u8 *)buffer, 6);
239 coords->x = be16_to_cpu(buffer[0]);
240 coords->y = be16_to_cpu(buffer[1]);
241 coords->z = be16_to_cpu(buffer[2]);
242 dev_dbg(&client->dev, "%s: x %d, y %d, z %d\n", __func__,
243 coords->x, coords->y, coords->z);
244}
245
246/**
247 * mpu3050_set_power_mode - set the power mode
248 * @client: i2c client for the sensor
249 * @val: value to switch on/off of power, 1: normal power, 0: low power
250 *
251 * Put device to normal-power mode or low-power mode.
252 */
253static void mpu3050_set_power_mode(struct i2c_client *client, u8 val)
254{
255 u8 value;
256
Wentao Xudac9e602012-06-12 11:52:34 -0400257 if (val) {
258 mpu3050_config_regulator(client, 1);
259 udelay(10);
260 }
261
Joseph Lai631b16e2011-06-27 13:26:53 -0700262 value = i2c_smbus_read_byte_data(client, MPU3050_PWR_MGM);
263 value = (value & ~MPU3050_PWR_MGM_MASK) |
264 (((val << MPU3050_PWR_MGM_POS) & MPU3050_PWR_MGM_MASK) ^
265 MPU3050_PWR_MGM_MASK);
266 i2c_smbus_write_byte_data(client, MPU3050_PWR_MGM, value);
Wentao Xudac9e602012-06-12 11:52:34 -0400267
268 if (!val) {
269 udelay(10);
270 mpu3050_config_regulator(client, 0);
271 }
Joseph Lai631b16e2011-06-27 13:26:53 -0700272}
273
274/**
275 * mpu3050_input_open - called on input event open
276 * @input: input dev of opened device
277 *
278 * The input layer calls this function when input event is opened. The
279 * function will push the device to resume. Then, the device is ready
280 * to provide data.
281 */
282static int mpu3050_input_open(struct input_dev *input)
283{
284 struct mpu3050_sensor *sensor = input_get_drvdata(input);
Heikki Krogerus3b518722011-12-23 23:57:09 -0800285 int error;
Joseph Lai631b16e2011-06-27 13:26:53 -0700286
287 pm_runtime_get(sensor->dev);
288
Heikki Krogerus3b518722011-12-23 23:57:09 -0800289 /* Enable interrupts */
290 error = i2c_smbus_write_byte_data(sensor->client, MPU3050_INT_CFG,
291 MPU3050_LATCH_INT_EN |
292 MPU3050_RAW_RDY_EN |
293 MPU3050_MPU_RDY_EN);
294 if (error < 0) {
295 pm_runtime_put(sensor->dev);
296 return error;
297 }
Wentao Xudac9e602012-06-12 11:52:34 -0400298 if (sensor->use_poll)
299 schedule_delayed_work(&sensor->input_work,
300 msecs_to_jiffies(MPU3050_DEFAULT_POLL_INTERVAL));
Heikki Krogerus3b518722011-12-23 23:57:09 -0800301
Joseph Lai631b16e2011-06-27 13:26:53 -0700302 return 0;
303}
304
305/**
306 * mpu3050_input_close - called on input event close
307 * @input: input dev of closed device
308 *
309 * The input layer calls this function when input event is closed. The
310 * function will push the device to suspend.
311 */
312static void mpu3050_input_close(struct input_dev *input)
313{
314 struct mpu3050_sensor *sensor = input_get_drvdata(input);
315
Wentao Xudac9e602012-06-12 11:52:34 -0400316 if (sensor->use_poll)
317 cancel_delayed_work_sync(&sensor->input_work);
318
Joseph Lai631b16e2011-06-27 13:26:53 -0700319 pm_runtime_put(sensor->dev);
320}
321
322/**
323 * mpu3050_interrupt_thread - handle an IRQ
324 * @irq: interrupt numner
325 * @data: the sensor
326 *
327 * Called by the kernel single threaded after an interrupt occurs. Read
328 * the sensor data and generate an input event for it.
329 */
330static irqreturn_t mpu3050_interrupt_thread(int irq, void *data)
331{
332 struct mpu3050_sensor *sensor = data;
333 struct axis_data axis;
334
335 mpu3050_read_xyz(sensor->client, &axis);
336
337 input_report_abs(sensor->idev, ABS_X, axis.x);
338 input_report_abs(sensor->idev, ABS_Y, axis.y);
339 input_report_abs(sensor->idev, ABS_Z, axis.z);
340 input_sync(sensor->idev);
341
342 return IRQ_HANDLED;
343}
344
345/**
Wentao Xudac9e602012-06-12 11:52:34 -0400346 * mpu3050_input_work_fn - polling work
347 * @work: the work struct
348 *
349 * Called by the work queue; read sensor data and generate an input
350 * event
351 */
352static void mpu3050_input_work_fn(struct work_struct *work)
353{
354 struct mpu3050_sensor *sensor;
355 struct axis_data axis;
356
357 sensor = container_of((struct delayed_work *)work,
358 struct mpu3050_sensor, input_work);
359
360 mpu3050_read_xyz(sensor->client, &axis);
361
362 input_report_abs(sensor->idev, ABS_X, axis.x);
363 input_report_abs(sensor->idev, ABS_Y, axis.y);
364 input_report_abs(sensor->idev, ABS_Z, axis.z);
365 input_sync(sensor->idev);
366
367 if (sensor->use_poll)
368 schedule_delayed_work(&sensor->input_work,
369 msecs_to_jiffies(MPU3050_DEFAULT_POLL_INTERVAL));
370}
371
372/**
Heikki Krogeruscd314fa2011-12-24 00:09:04 -0800373 * mpu3050_hw_init - initialize hardware
374 * @sensor: the sensor
375 *
376 * Called during device probe; configures the sampling method.
377 */
378static int __devinit mpu3050_hw_init(struct mpu3050_sensor *sensor)
379{
380 struct i2c_client *client = sensor->client;
381 int ret;
382 u8 reg;
383
384 /* Reset */
385 ret = i2c_smbus_write_byte_data(client, MPU3050_PWR_MGM,
386 MPU3050_PWR_MGM_RESET);
387 if (ret < 0)
388 return ret;
389
390 ret = i2c_smbus_read_byte_data(client, MPU3050_PWR_MGM);
391 if (ret < 0)
392 return ret;
393
394 ret &= ~MPU3050_PWR_MGM_CLKSEL;
395 ret |= MPU3050_PWR_MGM_PLL_Z;
396 ret = i2c_smbus_write_byte_data(client, MPU3050_PWR_MGM, ret);
397 if (ret < 0)
398 return ret;
399
400 /* Output frequency divider. The poll interval */
401 ret = i2c_smbus_write_byte_data(client, MPU3050_SMPLRT_DIV,
402 MPU3050_DEFAULT_POLL_INTERVAL - 1);
403 if (ret < 0)
404 return ret;
405
406 /* Set low pass filter and full scale */
407 reg = MPU3050_DEFAULT_FS_RANGE;
408 reg |= MPU3050_DLPF_CFG_42HZ << 3;
409 reg |= MPU3050_EXT_SYNC_NONE << 5;
410 ret = i2c_smbus_write_byte_data(client, MPU3050_DLPF_FS_SYNC, reg);
411 if (ret < 0)
412 return ret;
413
414 return 0;
415}
416
417/**
Joseph Lai631b16e2011-06-27 13:26:53 -0700418 * mpu3050_probe - device detection callback
419 * @client: i2c client of found device
420 * @id: id match information
421 *
422 * The I2C layer calls us when it believes a sensor is present at this
423 * address. Probe to see if this is correct and to validate the device.
424 *
425 * If present install the relevant sysfs interfaces and input device.
426 */
427static int __devinit mpu3050_probe(struct i2c_client *client,
428 const struct i2c_device_id *id)
429{
430 struct mpu3050_sensor *sensor;
431 struct input_dev *idev;
432 int ret;
433 int error;
434
435 sensor = kzalloc(sizeof(struct mpu3050_sensor), GFP_KERNEL);
436 idev = input_allocate_device();
437 if (!sensor || !idev) {
438 dev_err(&client->dev, "failed to allocate driver data\n");
439 error = -ENOMEM;
440 goto err_free_mem;
441 }
442
443 sensor->client = client;
444 sensor->dev = &client->dev;
445 sensor->idev = idev;
Wentao Xudac9e602012-06-12 11:52:34 -0400446 sensor->platform_data = client->dev.platform_data;
Joseph Lai631b16e2011-06-27 13:26:53 -0700447
448 mpu3050_set_power_mode(client, 1);
449 msleep(10);
450
451 ret = i2c_smbus_read_byte_data(client, MPU3050_CHIP_ID_REG);
452 if (ret < 0) {
453 dev_err(&client->dev, "failed to detect device\n");
454 error = -ENXIO;
455 goto err_free_mem;
456 }
457
458 if (ret != MPU3050_CHIP_ID) {
459 dev_err(&client->dev, "unsupported chip id\n");
460 error = -ENXIO;
461 goto err_free_mem;
462 }
463
464 idev->name = "MPU3050";
465 idev->id.bustype = BUS_I2C;
466 idev->dev.parent = &client->dev;
467
468 idev->open = mpu3050_input_open;
469 idev->close = mpu3050_input_close;
470
471 __set_bit(EV_ABS, idev->evbit);
472 input_set_abs_params(idev, ABS_X,
473 MPU3050_MIN_VALUE, MPU3050_MAX_VALUE, 0, 0);
474 input_set_abs_params(idev, ABS_Y,
475 MPU3050_MIN_VALUE, MPU3050_MAX_VALUE, 0, 0);
476 input_set_abs_params(idev, ABS_Z,
477 MPU3050_MIN_VALUE, MPU3050_MAX_VALUE, 0, 0);
478
479 input_set_drvdata(idev, sensor);
480
481 pm_runtime_set_active(&client->dev);
482
Heikki Krogeruscd314fa2011-12-24 00:09:04 -0800483 error = mpu3050_hw_init(sensor);
484 if (error)
485 goto err_pm_set_suspended;
486
Wentao Xudac9e602012-06-12 11:52:34 -0400487 if (client->irq == 0) {
488 INIT_DELAYED_WORK(&sensor->input_work, mpu3050_input_work_fn);
489 sensor->use_poll = 1;
490 } else {
491 sensor->use_poll = 0;
492
493 error = request_threaded_irq(client->irq,
Joseph Lai631b16e2011-06-27 13:26:53 -0700494 NULL, mpu3050_interrupt_thread,
495 IRQF_TRIGGER_RISING,
Heikki Krogerus3b518722011-12-23 23:57:09 -0800496 "mpu3050", sensor);
Wentao Xudac9e602012-06-12 11:52:34 -0400497 if (error) {
498 dev_err(&client->dev,
499 "can't get IRQ %d, error %d\n",
500 client->irq, error);
501 goto err_pm_set_suspended;
502 }
Joseph Lai631b16e2011-06-27 13:26:53 -0700503 }
504
505 error = input_register_device(idev);
506 if (error) {
507 dev_err(&client->dev, "failed to register input device\n");
508 goto err_free_irq;
509 }
510
511 pm_runtime_enable(&client->dev);
512 pm_runtime_set_autosuspend_delay(&client->dev, MPU3050_AUTO_DELAY);
513
514 return 0;
515
516err_free_irq:
Wentao Xudac9e602012-06-12 11:52:34 -0400517 if (client->irq > 0)
518 free_irq(client->irq, sensor);
Joseph Lai631b16e2011-06-27 13:26:53 -0700519err_pm_set_suspended:
520 pm_runtime_set_suspended(&client->dev);
521err_free_mem:
Axel Lind9b830f2011-08-11 09:19:29 -0700522 input_free_device(idev);
Joseph Lai631b16e2011-06-27 13:26:53 -0700523 kfree(sensor);
524 return error;
525}
526
527/**
528 * mpu3050_remove - remove a sensor
529 * @client: i2c client of sensor being removed
530 *
531 * Our sensor is going away, clean up the resources.
532 */
533static int __devexit mpu3050_remove(struct i2c_client *client)
534{
535 struct mpu3050_sensor *sensor = i2c_get_clientdata(client);
536
537 pm_runtime_disable(&client->dev);
538 pm_runtime_set_suspended(&client->dev);
539
540 free_irq(client->irq, sensor);
541 input_unregister_device(sensor->idev);
542 kfree(sensor);
543
544 return 0;
545}
546
547#ifdef CONFIG_PM
548/**
549 * mpu3050_suspend - called on device suspend
550 * @dev: device being suspended
551 *
552 * Put the device into sleep mode before we suspend the machine.
553 */
554static int mpu3050_suspend(struct device *dev)
555{
556 struct i2c_client *client = to_i2c_client(dev);
557
558 mpu3050_set_power_mode(client, 0);
559
560 return 0;
561}
562
563/**
564 * mpu3050_resume - called on device resume
565 * @dev: device being resumed
566 *
567 * Put the device into powered mode on resume.
568 */
569static int mpu3050_resume(struct device *dev)
570{
571 struct i2c_client *client = to_i2c_client(dev);
572
573 mpu3050_set_power_mode(client, 1);
574 msleep(100); /* wait for gyro chip resume */
575
576 return 0;
577}
578#endif
579
580static UNIVERSAL_DEV_PM_OPS(mpu3050_pm, mpu3050_suspend, mpu3050_resume, NULL);
581
582static const struct i2c_device_id mpu3050_ids[] = {
583 { "mpu3050", 0 },
584 { }
585};
586MODULE_DEVICE_TABLE(i2c, mpu3050_ids);
587
Olof Johanssone9489812011-12-23 01:20:44 -0800588static const struct of_device_id mpu3050_of_match[] = {
589 { .compatible = "invn,mpu3050", },
590 { },
591};
592MODULE_DEVICE_TABLE(of, mpu3050_of_match);
593
Joseph Lai631b16e2011-06-27 13:26:53 -0700594static struct i2c_driver mpu3050_i2c_driver = {
595 .driver = {
596 .name = "mpu3050",
597 .owner = THIS_MODULE,
598 .pm = &mpu3050_pm,
Olof Johanssone9489812011-12-23 01:20:44 -0800599 .of_match_table = mpu3050_of_match,
Joseph Lai631b16e2011-06-27 13:26:53 -0700600 },
601 .probe = mpu3050_probe,
602 .remove = __devexit_p(mpu3050_remove),
603 .id_table = mpu3050_ids,
604};
605
Axel Lin1b92c1c2012-03-16 23:05:41 -0700606module_i2c_driver(mpu3050_i2c_driver);
Joseph Lai631b16e2011-06-27 13:26:53 -0700607
608MODULE_AUTHOR("Wistron Corp.");
609MODULE_DESCRIPTION("MPU3050 Tri-axis gyroscope driver");
610MODULE_LICENSE("GPL");