blob: 6c64a57cdb0b113d807551808d22b0221c0fafd0 [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 Xuc7769c02012-08-03 15:06:41 -040043#include <linux/gpio.h>
Wentao Xudac9e602012-06-12 11:52:34 -040044#include <linux/input/mpu3050.h>
45#include <linux/regulator/consumer.h>
Joseph Lai631b16e2011-06-27 13:26:53 -070046
Joseph Lai631b16e2011-06-27 13:26:53 -070047#define MPU3050_CHIP_ID 0x69
Joseph Lai631b16e2011-06-27 13:26:53 -070048
49#define MPU3050_AUTO_DELAY 1000
50
51#define MPU3050_MIN_VALUE -32768
52#define MPU3050_MAX_VALUE 32767
53
Wentao Xuc7769c02012-08-03 15:06:41 -040054#define MPU3050_MIN_POLL_INTERVAL 1
55#define MPU3050_MAX_POLL_INTERVAL 250
Heikki Krogeruscd314fa2011-12-24 00:09:04 -080056#define MPU3050_DEFAULT_POLL_INTERVAL 200
57#define MPU3050_DEFAULT_FS_RANGE 3
58
59/* Register map */
60#define MPU3050_CHIP_ID_REG 0x00
61#define MPU3050_SMPLRT_DIV 0x15
62#define MPU3050_DLPF_FS_SYNC 0x16
63#define MPU3050_INT_CFG 0x17
64#define MPU3050_XOUT_H 0x1D
65#define MPU3050_PWR_MGM 0x3E
66#define MPU3050_PWR_MGM_POS 6
67
68/* Register bits */
69
70/* DLPF_FS_SYNC */
71#define MPU3050_EXT_SYNC_NONE 0x00
72#define MPU3050_EXT_SYNC_TEMP 0x20
73#define MPU3050_EXT_SYNC_GYROX 0x40
74#define MPU3050_EXT_SYNC_GYROY 0x60
75#define MPU3050_EXT_SYNC_GYROZ 0x80
76#define MPU3050_EXT_SYNC_ACCELX 0xA0
77#define MPU3050_EXT_SYNC_ACCELY 0xC0
78#define MPU3050_EXT_SYNC_ACCELZ 0xE0
79#define MPU3050_EXT_SYNC_MASK 0xE0
80#define MPU3050_FS_250DPS 0x00
81#define MPU3050_FS_500DPS 0x08
82#define MPU3050_FS_1000DPS 0x10
83#define MPU3050_FS_2000DPS 0x18
84#define MPU3050_FS_MASK 0x18
85#define MPU3050_DLPF_CFG_256HZ_NOLPF2 0x00
86#define MPU3050_DLPF_CFG_188HZ 0x01
87#define MPU3050_DLPF_CFG_98HZ 0x02
88#define MPU3050_DLPF_CFG_42HZ 0x03
89#define MPU3050_DLPF_CFG_20HZ 0x04
90#define MPU3050_DLPF_CFG_10HZ 0x05
91#define MPU3050_DLPF_CFG_5HZ 0x06
92#define MPU3050_DLPF_CFG_2100HZ_NOLPF 0x07
93#define MPU3050_DLPF_CFG_MASK 0x07
94/* INT_CFG */
95#define MPU3050_RAW_RDY_EN 0x01
Wentao Xuc7769c02012-08-03 15:06:41 -040096#define MPU3050_MPU_RDY_EN 0x04
97#define MPU3050_LATCH_INT_EN 0x20
98#define MPU3050_OPEN_DRAIN 0x40
99#define MPU3050_ACTIVE_LOW 0x80
Heikki Krogeruscd314fa2011-12-24 00:09:04 -0800100/* PWR_MGM */
101#define MPU3050_PWR_MGM_PLL_X 0x01
102#define MPU3050_PWR_MGM_PLL_Y 0x02
103#define MPU3050_PWR_MGM_PLL_Z 0x03
104#define MPU3050_PWR_MGM_CLKSEL 0x07
105#define MPU3050_PWR_MGM_STBY_ZG 0x08
106#define MPU3050_PWR_MGM_STBY_YG 0x10
107#define MPU3050_PWR_MGM_STBY_XG 0x20
108#define MPU3050_PWR_MGM_SLEEP 0x40
109#define MPU3050_PWR_MGM_RESET 0x80
110#define MPU3050_PWR_MGM_MASK 0x40
111
Joseph Lai631b16e2011-06-27 13:26:53 -0700112struct axis_data {
113 s16 x;
114 s16 y;
115 s16 z;
116};
117
118struct mpu3050_sensor {
119 struct i2c_client *client;
120 struct device *dev;
121 struct input_dev *idev;
Wentao Xudac9e602012-06-12 11:52:34 -0400122 struct mpu3050_gyro_platform_data *platform_data;
123 struct delayed_work input_work;
124 u32 use_poll;
Wentao Xuc7769c02012-08-03 15:06:41 -0400125 u32 poll_interval;
Wentao Xu04f4cc92012-08-15 19:41:43 -0400126 u32 dlpf_index;
Joseph Lai631b16e2011-06-27 13:26:53 -0700127};
128
Wentao Xudac9e602012-06-12 11:52:34 -0400129struct sensor_regulator {
130 struct regulator *vreg;
131 const char *name;
132 u32 min_uV;
133 u32 max_uV;
134};
135
136struct sensor_regulator mpu_vreg[] = {
137 {NULL, "vdd", 2100000, 3600000},
138 {NULL, "vlogic", 1800000, 1800000},
139};
140
Wentao Xu04f4cc92012-08-15 19:41:43 -0400141struct dlpf_cfg_tb {
142 u8 cfg; /* cfg index */
143 u32 lpf_bw; /* low pass filter bandwidth in Hz */
144 u32 sample_rate; /* analog sample rate in Khz, 1 or 8 */
145};
146
147static struct dlpf_cfg_tb dlpf_table[] = {
148 {6, 5, 1},
149 {5, 10, 1},
150 {4, 20, 1},
151 {3, 42, 1},
152 {2, 98, 1},
153 {1, 188, 1},
154 {0, 256, 8},
155};
156
157static u8 interval_to_dlpf_cfg(u32 interval)
158{
159 u32 sample_rate = 1000 / interval;
160 u32 i;
161
162 /* the filter bandwidth needs to be greater or
163 * equal to half of the sample rate
164 */
165 for (i = 0; i < sizeof(dlpf_table)/sizeof(dlpf_table[0]); i++) {
166 if (dlpf_table[i].lpf_bw * 2 >= sample_rate)
167 return i;
168 }
169
170 /* return the maximum possible */
171 return --i;
172}
173
Wentao Xudac9e602012-06-12 11:52:34 -0400174static int mpu3050_config_regulator(struct i2c_client *client, bool on)
175{
176 int rc = 0, i;
177 int num_reg = sizeof(mpu_vreg) / sizeof(struct sensor_regulator);
178
179 if (on) {
180 for (i = 0; i < num_reg; i++) {
181 mpu_vreg[i].vreg = regulator_get(&client->dev,
182 mpu_vreg[i].name);
183 if (IS_ERR(mpu_vreg[i].vreg)) {
184 rc = PTR_ERR(mpu_vreg[i].vreg);
185 pr_err("%s:regulator get failed rc=%d\n",
186 __func__, rc);
Wentao Xubf005982012-12-12 11:28:21 -0500187 mpu_vreg[i].vreg = NULL;
Wentao Xudac9e602012-06-12 11:52:34 -0400188 goto error_vdd;
189 }
190
191 if (regulator_count_voltages(mpu_vreg[i].vreg) > 0) {
192 rc = regulator_set_voltage(mpu_vreg[i].vreg,
193 mpu_vreg[i].min_uV, mpu_vreg[i].max_uV);
194 if (rc) {
195 pr_err("%s:set_voltage failed rc=%d\n",
196 __func__, rc);
197 regulator_put(mpu_vreg[i].vreg);
Wentao Xubf005982012-12-12 11:28:21 -0500198 mpu_vreg[i].vreg = NULL;
Wentao Xudac9e602012-06-12 11:52:34 -0400199 goto error_vdd;
200 }
201 }
202
203 rc = regulator_enable(mpu_vreg[i].vreg);
204 if (rc) {
205 pr_err("%s: regulator_enable failed rc =%d\n",
206 __func__,
207 rc);
208
209 if (regulator_count_voltages(
210 mpu_vreg[i].vreg) > 0) {
211 regulator_set_voltage(mpu_vreg[i].vreg,
212 0, mpu_vreg[i].max_uV);
213 }
214 regulator_put(mpu_vreg[i].vreg);
Wentao Xubf005982012-12-12 11:28:21 -0500215 mpu_vreg[i].vreg = NULL;
Wentao Xudac9e602012-06-12 11:52:34 -0400216 goto error_vdd;
217 }
218 }
219 return rc;
220 } else {
221 i = num_reg;
222 }
223error_vdd:
224 while (--i >= 0) {
Wentao Xubf005982012-12-12 11:28:21 -0500225 if (!IS_ERR_OR_NULL(mpu_vreg[i].vreg)) {
226 if (regulator_count_voltages(
227 mpu_vreg[i].vreg) > 0) {
228 regulator_set_voltage(mpu_vreg[i].vreg, 0,
Wentao Xudac9e602012-06-12 11:52:34 -0400229 mpu_vreg[i].max_uV);
Wentao Xubf005982012-12-12 11:28:21 -0500230 }
231 regulator_disable(mpu_vreg[i].vreg);
232 regulator_put(mpu_vreg[i].vreg);
233 mpu_vreg[i].vreg = NULL;
Wentao Xudac9e602012-06-12 11:52:34 -0400234 }
Wentao Xudac9e602012-06-12 11:52:34 -0400235 }
236 return rc;
237}
238
Joseph Lai631b16e2011-06-27 13:26:53 -0700239/**
Wentao Xuc7769c02012-08-03 15:06:41 -0400240 * mpu3050_attr_get_polling_rate - get the sampling rate
241 */
242static ssize_t mpu3050_attr_get_polling_rate(struct device *dev,
243 struct device_attribute *attr,
244 char *buf)
245{
246 int val;
247 struct mpu3050_sensor *sensor = dev_get_drvdata(dev);
248 val = sensor ? sensor->poll_interval : 0;
249 return snprintf(buf, 8, "%d\n", val);
250}
251
252/**
253 * mpu3050_attr_set_polling_rate - set the sampling rate
254 */
255static ssize_t mpu3050_attr_set_polling_rate(struct device *dev,
256 struct device_attribute *attr,
257 const char *buf, size_t size)
258{
259 struct mpu3050_sensor *sensor = dev_get_drvdata(dev);
260 unsigned long interval_ms;
Wentao Xu04f4cc92012-08-15 19:41:43 -0400261 unsigned int dlpf_index;
262 u8 divider, reg;
263 int ret;
Wentao Xuc7769c02012-08-03 15:06:41 -0400264
265 if (kstrtoul(buf, 10, &interval_ms))
266 return -EINVAL;
267 if ((interval_ms < MPU3050_MIN_POLL_INTERVAL) ||
268 (interval_ms > MPU3050_MAX_POLL_INTERVAL))
269 return -EINVAL;
270
Wentao Xu04f4cc92012-08-15 19:41:43 -0400271 dlpf_index = interval_to_dlpf_cfg(interval_ms);
272 divider = interval_ms * dlpf_table[dlpf_index].sample_rate - 1;
Wentao Xuc7769c02012-08-03 15:06:41 -0400273
Wentao Xu04f4cc92012-08-15 19:41:43 -0400274 if (sensor->dlpf_index != dlpf_index) {
275 /* Set low pass filter and full scale */
276 reg = dlpf_table[dlpf_index].cfg;
277 reg |= MPU3050_DEFAULT_FS_RANGE << 3;
278 reg |= MPU3050_EXT_SYNC_NONE << 5;
279 ret = i2c_smbus_write_byte_data(sensor->client,
280 MPU3050_DLPF_FS_SYNC, reg);
281 if (ret == 0)
282 sensor->dlpf_index = dlpf_index;
283 }
284
285 if (sensor->poll_interval != interval_ms) {
286 /* Output frequency divider. The poll interval */
287 ret = i2c_smbus_write_byte_data(sensor->client,
288 MPU3050_SMPLRT_DIV, divider);
289 if (ret == 0)
290 sensor->poll_interval = interval_ms;
291 }
Wentao Xuc7769c02012-08-03 15:06:41 -0400292
293 return size;
294}
295
296static struct device_attribute attributes[] = {
297
Wentao Xu75a5e4b2012-11-15 16:30:15 -0500298 __ATTR(pollrate_ms, 0664,
Wentao Xuc7769c02012-08-03 15:06:41 -0400299 mpu3050_attr_get_polling_rate,
300 mpu3050_attr_set_polling_rate),
301};
302
303static int create_sysfs_interfaces(struct device *dev)
304{
305 int i;
306 int err;
307 for (i = 0; i < ARRAY_SIZE(attributes); i++) {
308 err = device_create_file(dev, attributes + i);
309 if (err)
310 goto error;
311 }
312 return 0;
313
314error:
315 for ( ; i >= 0; i--)
316 device_remove_file(dev, attributes + i);
317 dev_err(dev, "%s:Unable to create interface\n", __func__);
318 return err;
319}
320
321static int remove_sysfs_interfaces(struct device *dev)
322{
323 int i;
324 for (i = 0; i < ARRAY_SIZE(attributes); i++)
325 device_remove_file(dev, attributes + i);
326 return 0;
327}
328
329/**
Joseph Lai631b16e2011-06-27 13:26:53 -0700330 * mpu3050_xyz_read_reg - read the axes values
331 * @buffer: provide register addr and get register
332 * @length: length of register
333 *
334 * Reads the register values in one transaction or returns a negative
335 * error code on failure.
336 */
337static int mpu3050_xyz_read_reg(struct i2c_client *client,
338 u8 *buffer, int length)
339{
340 /*
341 * Annoying we can't make this const because the i2c layer doesn't
342 * declare input buffers const.
343 */
344 char cmd = MPU3050_XOUT_H;
345 struct i2c_msg msg[] = {
346 {
347 .addr = client->addr,
348 .flags = 0,
349 .len = 1,
350 .buf = &cmd,
351 },
352 {
353 .addr = client->addr,
354 .flags = I2C_M_RD,
355 .len = length,
356 .buf = buffer,
357 },
358 };
359
360 return i2c_transfer(client->adapter, msg, 2);
361}
362
363/**
364 * mpu3050_read_xyz - get co-ordinates from device
365 * @client: i2c address of sensor
366 * @coords: co-ordinates to update
367 *
368 * Return the converted X Y and Z co-ordinates from the sensor device
369 */
370static void mpu3050_read_xyz(struct i2c_client *client,
371 struct axis_data *coords)
372{
373 u16 buffer[3];
374
375 mpu3050_xyz_read_reg(client, (u8 *)buffer, 6);
376 coords->x = be16_to_cpu(buffer[0]);
377 coords->y = be16_to_cpu(buffer[1]);
378 coords->z = be16_to_cpu(buffer[2]);
379 dev_dbg(&client->dev, "%s: x %d, y %d, z %d\n", __func__,
380 coords->x, coords->y, coords->z);
381}
382
383/**
384 * mpu3050_set_power_mode - set the power mode
385 * @client: i2c client for the sensor
386 * @val: value to switch on/off of power, 1: normal power, 0: low power
387 *
388 * Put device to normal-power mode or low-power mode.
389 */
390static void mpu3050_set_power_mode(struct i2c_client *client, u8 val)
391{
392 u8 value;
393
Wentao Xudac9e602012-06-12 11:52:34 -0400394 if (val) {
395 mpu3050_config_regulator(client, 1);
396 udelay(10);
397 }
398
Joseph Lai631b16e2011-06-27 13:26:53 -0700399 value = i2c_smbus_read_byte_data(client, MPU3050_PWR_MGM);
400 value = (value & ~MPU3050_PWR_MGM_MASK) |
401 (((val << MPU3050_PWR_MGM_POS) & MPU3050_PWR_MGM_MASK) ^
402 MPU3050_PWR_MGM_MASK);
403 i2c_smbus_write_byte_data(client, MPU3050_PWR_MGM, value);
Wentao Xudac9e602012-06-12 11:52:34 -0400404
405 if (!val) {
406 udelay(10);
407 mpu3050_config_regulator(client, 0);
408 }
Joseph Lai631b16e2011-06-27 13:26:53 -0700409}
410
411/**
412 * mpu3050_input_open - called on input event open
413 * @input: input dev of opened device
414 *
415 * The input layer calls this function when input event is opened. The
416 * function will push the device to resume. Then, the device is ready
417 * to provide data.
418 */
419static int mpu3050_input_open(struct input_dev *input)
420{
421 struct mpu3050_sensor *sensor = input_get_drvdata(input);
Heikki Krogerus3b518722011-12-23 23:57:09 -0800422 int error;
Joseph Lai631b16e2011-06-27 13:26:53 -0700423
Wentao Xuc7769c02012-08-03 15:06:41 -0400424 pm_runtime_get_sync(sensor->dev);
Joseph Lai631b16e2011-06-27 13:26:53 -0700425
Heikki Krogerus3b518722011-12-23 23:57:09 -0800426 /* Enable interrupts */
427 error = i2c_smbus_write_byte_data(sensor->client, MPU3050_INT_CFG,
Wentao Xuc7769c02012-08-03 15:06:41 -0400428 MPU3050_ACTIVE_LOW |
429 MPU3050_OPEN_DRAIN |
430 MPU3050_RAW_RDY_EN);
Heikki Krogerus3b518722011-12-23 23:57:09 -0800431 if (error < 0) {
432 pm_runtime_put(sensor->dev);
433 return error;
434 }
Wentao Xudac9e602012-06-12 11:52:34 -0400435 if (sensor->use_poll)
436 schedule_delayed_work(&sensor->input_work,
Wentao Xuc7769c02012-08-03 15:06:41 -0400437 msecs_to_jiffies(sensor->poll_interval));
Heikki Krogerus3b518722011-12-23 23:57:09 -0800438
Joseph Lai631b16e2011-06-27 13:26:53 -0700439 return 0;
440}
441
442/**
443 * mpu3050_input_close - called on input event close
444 * @input: input dev of closed device
445 *
446 * The input layer calls this function when input event is closed. The
447 * function will push the device to suspend.
448 */
449static void mpu3050_input_close(struct input_dev *input)
450{
451 struct mpu3050_sensor *sensor = input_get_drvdata(input);
452
Wentao Xudac9e602012-06-12 11:52:34 -0400453 if (sensor->use_poll)
454 cancel_delayed_work_sync(&sensor->input_work);
455
Joseph Lai631b16e2011-06-27 13:26:53 -0700456 pm_runtime_put(sensor->dev);
457}
458
459/**
460 * mpu3050_interrupt_thread - handle an IRQ
461 * @irq: interrupt numner
462 * @data: the sensor
463 *
464 * Called by the kernel single threaded after an interrupt occurs. Read
465 * the sensor data and generate an input event for it.
466 */
467static irqreturn_t mpu3050_interrupt_thread(int irq, void *data)
468{
469 struct mpu3050_sensor *sensor = data;
470 struct axis_data axis;
471
472 mpu3050_read_xyz(sensor->client, &axis);
473
474 input_report_abs(sensor->idev, ABS_X, axis.x);
475 input_report_abs(sensor->idev, ABS_Y, axis.y);
476 input_report_abs(sensor->idev, ABS_Z, axis.z);
477 input_sync(sensor->idev);
478
479 return IRQ_HANDLED;
480}
481
482/**
Wentao Xudac9e602012-06-12 11:52:34 -0400483 * mpu3050_input_work_fn - polling work
484 * @work: the work struct
485 *
486 * Called by the work queue; read sensor data and generate an input
487 * event
488 */
489static void mpu3050_input_work_fn(struct work_struct *work)
490{
491 struct mpu3050_sensor *sensor;
492 struct axis_data axis;
493
494 sensor = container_of((struct delayed_work *)work,
495 struct mpu3050_sensor, input_work);
496
497 mpu3050_read_xyz(sensor->client, &axis);
498
499 input_report_abs(sensor->idev, ABS_X, axis.x);
500 input_report_abs(sensor->idev, ABS_Y, axis.y);
501 input_report_abs(sensor->idev, ABS_Z, axis.z);
502 input_sync(sensor->idev);
503
504 if (sensor->use_poll)
505 schedule_delayed_work(&sensor->input_work,
Wentao Xuc7769c02012-08-03 15:06:41 -0400506 msecs_to_jiffies(sensor->poll_interval));
Wentao Xudac9e602012-06-12 11:52:34 -0400507}
508
509/**
Heikki Krogeruscd314fa2011-12-24 00:09:04 -0800510 * mpu3050_hw_init - initialize hardware
511 * @sensor: the sensor
512 *
513 * Called during device probe; configures the sampling method.
514 */
515static int __devinit mpu3050_hw_init(struct mpu3050_sensor *sensor)
516{
517 struct i2c_client *client = sensor->client;
518 int ret;
519 u8 reg;
520
521 /* Reset */
522 ret = i2c_smbus_write_byte_data(client, MPU3050_PWR_MGM,
523 MPU3050_PWR_MGM_RESET);
524 if (ret < 0)
525 return ret;
526
527 ret = i2c_smbus_read_byte_data(client, MPU3050_PWR_MGM);
528 if (ret < 0)
529 return ret;
530
531 ret &= ~MPU3050_PWR_MGM_CLKSEL;
532 ret |= MPU3050_PWR_MGM_PLL_Z;
533 ret = i2c_smbus_write_byte_data(client, MPU3050_PWR_MGM, ret);
534 if (ret < 0)
535 return ret;
536
537 /* Output frequency divider. The poll interval */
538 ret = i2c_smbus_write_byte_data(client, MPU3050_SMPLRT_DIV,
Wentao Xuc7769c02012-08-03 15:06:41 -0400539 sensor->poll_interval - 1);
Heikki Krogeruscd314fa2011-12-24 00:09:04 -0800540 if (ret < 0)
541 return ret;
542
543 /* Set low pass filter and full scale */
Wentao Xu04f4cc92012-08-15 19:41:43 -0400544 reg = MPU3050_DLPF_CFG_42HZ;
545 reg |= MPU3050_DEFAULT_FS_RANGE << 3;
Heikki Krogeruscd314fa2011-12-24 00:09:04 -0800546 reg |= MPU3050_EXT_SYNC_NONE << 5;
547 ret = i2c_smbus_write_byte_data(client, MPU3050_DLPF_FS_SYNC, reg);
548 if (ret < 0)
549 return ret;
550
551 return 0;
552}
553
554/**
Joseph Lai631b16e2011-06-27 13:26:53 -0700555 * mpu3050_probe - device detection callback
556 * @client: i2c client of found device
557 * @id: id match information
558 *
559 * The I2C layer calls us when it believes a sensor is present at this
560 * address. Probe to see if this is correct and to validate the device.
561 *
562 * If present install the relevant sysfs interfaces and input device.
563 */
564static int __devinit mpu3050_probe(struct i2c_client *client,
565 const struct i2c_device_id *id)
566{
567 struct mpu3050_sensor *sensor;
568 struct input_dev *idev;
569 int ret;
570 int error;
571
572 sensor = kzalloc(sizeof(struct mpu3050_sensor), GFP_KERNEL);
573 idev = input_allocate_device();
574 if (!sensor || !idev) {
575 dev_err(&client->dev, "failed to allocate driver data\n");
576 error = -ENOMEM;
577 goto err_free_mem;
578 }
579
580 sensor->client = client;
581 sensor->dev = &client->dev;
582 sensor->idev = idev;
Wentao Xudac9e602012-06-12 11:52:34 -0400583 sensor->platform_data = client->dev.platform_data;
Wentao Xuc7769c02012-08-03 15:06:41 -0400584 i2c_set_clientdata(client, sensor);
585 if (sensor->platform_data) {
586 u32 interval = sensor->platform_data->poll_interval;
587
588 if ((interval < MPU3050_MIN_POLL_INTERVAL) ||
589 (interval > MPU3050_MAX_POLL_INTERVAL))
590 sensor->poll_interval = MPU3050_DEFAULT_POLL_INTERVAL;
591 else
592 sensor->poll_interval = interval;
593 } else {
594 sensor->poll_interval = MPU3050_DEFAULT_POLL_INTERVAL;
595 }
Joseph Lai631b16e2011-06-27 13:26:53 -0700596
597 mpu3050_set_power_mode(client, 1);
598 msleep(10);
599
600 ret = i2c_smbus_read_byte_data(client, MPU3050_CHIP_ID_REG);
601 if (ret < 0) {
602 dev_err(&client->dev, "failed to detect device\n");
603 error = -ENXIO;
604 goto err_free_mem;
605 }
606
607 if (ret != MPU3050_CHIP_ID) {
608 dev_err(&client->dev, "unsupported chip id\n");
609 error = -ENXIO;
610 goto err_free_mem;
611 }
612
613 idev->name = "MPU3050";
614 idev->id.bustype = BUS_I2C;
615 idev->dev.parent = &client->dev;
616
617 idev->open = mpu3050_input_open;
618 idev->close = mpu3050_input_close;
619
620 __set_bit(EV_ABS, idev->evbit);
621 input_set_abs_params(idev, ABS_X,
622 MPU3050_MIN_VALUE, MPU3050_MAX_VALUE, 0, 0);
623 input_set_abs_params(idev, ABS_Y,
624 MPU3050_MIN_VALUE, MPU3050_MAX_VALUE, 0, 0);
625 input_set_abs_params(idev, ABS_Z,
626 MPU3050_MIN_VALUE, MPU3050_MAX_VALUE, 0, 0);
627
628 input_set_drvdata(idev, sensor);
629
630 pm_runtime_set_active(&client->dev);
631
Heikki Krogeruscd314fa2011-12-24 00:09:04 -0800632 error = mpu3050_hw_init(sensor);
633 if (error)
634 goto err_pm_set_suspended;
635
Wentao Xudac9e602012-06-12 11:52:34 -0400636 if (client->irq == 0) {
Wentao Xudac9e602012-06-12 11:52:34 -0400637 sensor->use_poll = 1;
Wentao Xuc7769c02012-08-03 15:06:41 -0400638 INIT_DELAYED_WORK(&sensor->input_work, mpu3050_input_work_fn);
Wentao Xudac9e602012-06-12 11:52:34 -0400639 } else {
640 sensor->use_poll = 0;
641
Wentao Xuc7769c02012-08-03 15:06:41 -0400642 if (gpio_is_valid(sensor->platform_data->gpio_int)) {
643 /* configure interrupt gpio */
644 ret = gpio_request(sensor->platform_data->gpio_int,
645 "gyro_gpio_int");
646 if (ret) {
647 pr_err("%s: unable to request interrupt gpio %d\n",
648 __func__,
649 sensor->platform_data->gpio_int);
650 goto err_pm_set_suspended;
651 }
652
653 ret = gpio_direction_input(
654 sensor->platform_data->gpio_int);
655 if (ret) {
656 pr_err("%s: unable to set direction for gpio %d\n",
657 __func__, sensor->platform_data->gpio_int);
658 goto err_free_gpio;
659 }
660 }
661
Wentao Xudac9e602012-06-12 11:52:34 -0400662 error = request_threaded_irq(client->irq,
Joseph Lai631b16e2011-06-27 13:26:53 -0700663 NULL, mpu3050_interrupt_thread,
Wentao Xuc7769c02012-08-03 15:06:41 -0400664 IRQF_TRIGGER_FALLING,
Heikki Krogerus3b518722011-12-23 23:57:09 -0800665 "mpu3050", sensor);
Wentao Xudac9e602012-06-12 11:52:34 -0400666 if (error) {
667 dev_err(&client->dev,
668 "can't get IRQ %d, error %d\n",
669 client->irq, error);
670 goto err_pm_set_suspended;
671 }
Joseph Lai631b16e2011-06-27 13:26:53 -0700672 }
673
674 error = input_register_device(idev);
675 if (error) {
676 dev_err(&client->dev, "failed to register input device\n");
677 goto err_free_irq;
678 }
679
Wentao Xuc7769c02012-08-03 15:06:41 -0400680 error = create_sysfs_interfaces(&client->dev);
681 if (error < 0) {
682 dev_err(&client->dev, "failed to create sysfs\n");
683 goto err_input_cleanup;
684 }
685
Joseph Lai631b16e2011-06-27 13:26:53 -0700686 pm_runtime_enable(&client->dev);
687 pm_runtime_set_autosuspend_delay(&client->dev, MPU3050_AUTO_DELAY);
688
689 return 0;
690
Wentao Xuc7769c02012-08-03 15:06:41 -0400691err_input_cleanup:
692 input_unregister_device(idev);
Joseph Lai631b16e2011-06-27 13:26:53 -0700693err_free_irq:
Wentao Xudac9e602012-06-12 11:52:34 -0400694 if (client->irq > 0)
695 free_irq(client->irq, sensor);
Wentao Xuc7769c02012-08-03 15:06:41 -0400696err_free_gpio:
697 if ((client->irq > 0) &&
698 (gpio_is_valid(sensor->platform_data->gpio_int)))
699 gpio_free(sensor->platform_data->gpio_int);
Joseph Lai631b16e2011-06-27 13:26:53 -0700700err_pm_set_suspended:
701 pm_runtime_set_suspended(&client->dev);
702err_free_mem:
Axel Lind9b830f2011-08-11 09:19:29 -0700703 input_free_device(idev);
Joseph Lai631b16e2011-06-27 13:26:53 -0700704 kfree(sensor);
705 return error;
706}
707
708/**
709 * mpu3050_remove - remove a sensor
710 * @client: i2c client of sensor being removed
711 *
712 * Our sensor is going away, clean up the resources.
713 */
714static int __devexit mpu3050_remove(struct i2c_client *client)
715{
716 struct mpu3050_sensor *sensor = i2c_get_clientdata(client);
717
718 pm_runtime_disable(&client->dev);
719 pm_runtime_set_suspended(&client->dev);
720
Wentao Xuc7769c02012-08-03 15:06:41 -0400721 if (client->irq)
722 free_irq(client->irq, sensor);
723
724 remove_sysfs_interfaces(&client->dev);
Joseph Lai631b16e2011-06-27 13:26:53 -0700725 input_unregister_device(sensor->idev);
Wentao Xuc7769c02012-08-03 15:06:41 -0400726
Joseph Lai631b16e2011-06-27 13:26:53 -0700727 kfree(sensor);
728
729 return 0;
730}
731
732#ifdef CONFIG_PM
733/**
734 * mpu3050_suspend - called on device suspend
735 * @dev: device being suspended
736 *
737 * Put the device into sleep mode before we suspend the machine.
738 */
739static int mpu3050_suspend(struct device *dev)
740{
741 struct i2c_client *client = to_i2c_client(dev);
742
743 mpu3050_set_power_mode(client, 0);
744
745 return 0;
746}
747
748/**
749 * mpu3050_resume - called on device resume
750 * @dev: device being resumed
751 *
752 * Put the device into powered mode on resume.
753 */
754static int mpu3050_resume(struct device *dev)
755{
756 struct i2c_client *client = to_i2c_client(dev);
757
758 mpu3050_set_power_mode(client, 1);
759 msleep(100); /* wait for gyro chip resume */
760
761 return 0;
762}
763#endif
764
765static UNIVERSAL_DEV_PM_OPS(mpu3050_pm, mpu3050_suspend, mpu3050_resume, NULL);
766
767static const struct i2c_device_id mpu3050_ids[] = {
768 { "mpu3050", 0 },
769 { }
770};
771MODULE_DEVICE_TABLE(i2c, mpu3050_ids);
772
Olof Johanssone9489812011-12-23 01:20:44 -0800773static const struct of_device_id mpu3050_of_match[] = {
774 { .compatible = "invn,mpu3050", },
775 { },
776};
777MODULE_DEVICE_TABLE(of, mpu3050_of_match);
778
Joseph Lai631b16e2011-06-27 13:26:53 -0700779static struct i2c_driver mpu3050_i2c_driver = {
780 .driver = {
781 .name = "mpu3050",
782 .owner = THIS_MODULE,
783 .pm = &mpu3050_pm,
Olof Johanssone9489812011-12-23 01:20:44 -0800784 .of_match_table = mpu3050_of_match,
Joseph Lai631b16e2011-06-27 13:26:53 -0700785 },
786 .probe = mpu3050_probe,
787 .remove = __devexit_p(mpu3050_remove),
788 .id_table = mpu3050_ids,
789};
790
Axel Lin1b92c1c2012-03-16 23:05:41 -0700791module_i2c_driver(mpu3050_i2c_driver);
Joseph Lai631b16e2011-06-27 13:26:53 -0700792
793MODULE_AUTHOR("Wistron Corp.");
794MODULE_DESCRIPTION("MPU3050 Tri-axis gyroscope driver");
795MODULE_LICENSE("GPL");