blob: 1537866e67e720ddf1335fda52ce84681beffd3d [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>
Richard Liua1d406a2013-04-14 13:46:48 -070046#include <linux/of_gpio.h>
47#include <mach/gpiomux.h>
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;
Richard Liua1d406a2013-04-14 13:46:48 -0700127 u32 enable_gpio;
Xiaocheng Li05adbd82013-06-30 17:21:58 -0700128 u32 enable;
Joseph Lai631b16e2011-06-27 13:26:53 -0700129};
130
Wentao Xudac9e602012-06-12 11:52:34 -0400131struct sensor_regulator {
132 struct regulator *vreg;
133 const char *name;
134 u32 min_uV;
135 u32 max_uV;
136};
137
138struct sensor_regulator mpu_vreg[] = {
139 {NULL, "vdd", 2100000, 3600000},
140 {NULL, "vlogic", 1800000, 1800000},
141};
142
Richard Liua1d406a2013-04-14 13:46:48 -0700143static const int mpu3050_chip_ids[] = {
144 0x68,
145 0x69,
146};
147
Wentao Xu04f4cc92012-08-15 19:41:43 -0400148struct dlpf_cfg_tb {
149 u8 cfg; /* cfg index */
150 u32 lpf_bw; /* low pass filter bandwidth in Hz */
151 u32 sample_rate; /* analog sample rate in Khz, 1 or 8 */
152};
153
154static struct dlpf_cfg_tb dlpf_table[] = {
155 {6, 5, 1},
156 {5, 10, 1},
157 {4, 20, 1},
158 {3, 42, 1},
159 {2, 98, 1},
160 {1, 188, 1},
161 {0, 256, 8},
162};
163
164static u8 interval_to_dlpf_cfg(u32 interval)
165{
166 u32 sample_rate = 1000 / interval;
167 u32 i;
168
169 /* the filter bandwidth needs to be greater or
170 * equal to half of the sample rate
171 */
172 for (i = 0; i < sizeof(dlpf_table)/sizeof(dlpf_table[0]); i++) {
173 if (dlpf_table[i].lpf_bw * 2 >= sample_rate)
174 return i;
175 }
176
177 /* return the maximum possible */
178 return --i;
179}
180
Wentao Xudac9e602012-06-12 11:52:34 -0400181static int mpu3050_config_regulator(struct i2c_client *client, bool on)
182{
183 int rc = 0, i;
184 int num_reg = sizeof(mpu_vreg) / sizeof(struct sensor_regulator);
185
186 if (on) {
187 for (i = 0; i < num_reg; i++) {
188 mpu_vreg[i].vreg = regulator_get(&client->dev,
189 mpu_vreg[i].name);
190 if (IS_ERR(mpu_vreg[i].vreg)) {
191 rc = PTR_ERR(mpu_vreg[i].vreg);
192 pr_err("%s:regulator get failed rc=%d\n",
193 __func__, rc);
Wentao Xubf005982012-12-12 11:28:21 -0500194 mpu_vreg[i].vreg = NULL;
Wentao Xudac9e602012-06-12 11:52:34 -0400195 goto error_vdd;
196 }
197
198 if (regulator_count_voltages(mpu_vreg[i].vreg) > 0) {
199 rc = regulator_set_voltage(mpu_vreg[i].vreg,
200 mpu_vreg[i].min_uV, mpu_vreg[i].max_uV);
201 if (rc) {
202 pr_err("%s:set_voltage failed rc=%d\n",
203 __func__, rc);
204 regulator_put(mpu_vreg[i].vreg);
Wentao Xubf005982012-12-12 11:28:21 -0500205 mpu_vreg[i].vreg = NULL;
Wentao Xudac9e602012-06-12 11:52:34 -0400206 goto error_vdd;
207 }
208 }
209
210 rc = regulator_enable(mpu_vreg[i].vreg);
211 if (rc) {
212 pr_err("%s: regulator_enable failed rc =%d\n",
213 __func__,
214 rc);
215
216 if (regulator_count_voltages(
217 mpu_vreg[i].vreg) > 0) {
218 regulator_set_voltage(mpu_vreg[i].vreg,
219 0, mpu_vreg[i].max_uV);
220 }
221 regulator_put(mpu_vreg[i].vreg);
Wentao Xubf005982012-12-12 11:28:21 -0500222 mpu_vreg[i].vreg = NULL;
Wentao Xudac9e602012-06-12 11:52:34 -0400223 goto error_vdd;
224 }
225 }
226 return rc;
227 } else {
228 i = num_reg;
229 }
230error_vdd:
231 while (--i >= 0) {
Wentao Xubf005982012-12-12 11:28:21 -0500232 if (!IS_ERR_OR_NULL(mpu_vreg[i].vreg)) {
233 if (regulator_count_voltages(
234 mpu_vreg[i].vreg) > 0) {
235 regulator_set_voltage(mpu_vreg[i].vreg, 0,
Wentao Xudac9e602012-06-12 11:52:34 -0400236 mpu_vreg[i].max_uV);
Wentao Xubf005982012-12-12 11:28:21 -0500237 }
238 regulator_disable(mpu_vreg[i].vreg);
239 regulator_put(mpu_vreg[i].vreg);
240 mpu_vreg[i].vreg = NULL;
Wentao Xudac9e602012-06-12 11:52:34 -0400241 }
Wentao Xudac9e602012-06-12 11:52:34 -0400242 }
243 return rc;
244}
245
Joseph Lai631b16e2011-06-27 13:26:53 -0700246/**
Wentao Xuc7769c02012-08-03 15:06:41 -0400247 * mpu3050_attr_get_polling_rate - get the sampling rate
248 */
249static ssize_t mpu3050_attr_get_polling_rate(struct device *dev,
250 struct device_attribute *attr,
251 char *buf)
252{
253 int val;
254 struct mpu3050_sensor *sensor = dev_get_drvdata(dev);
255 val = sensor ? sensor->poll_interval : 0;
256 return snprintf(buf, 8, "%d\n", val);
257}
258
259/**
260 * mpu3050_attr_set_polling_rate - set the sampling rate
261 */
262static ssize_t mpu3050_attr_set_polling_rate(struct device *dev,
263 struct device_attribute *attr,
264 const char *buf, size_t size)
265{
266 struct mpu3050_sensor *sensor = dev_get_drvdata(dev);
267 unsigned long interval_ms;
Wentao Xu04f4cc92012-08-15 19:41:43 -0400268 unsigned int dlpf_index;
269 u8 divider, reg;
270 int ret;
Wentao Xuc7769c02012-08-03 15:06:41 -0400271
272 if (kstrtoul(buf, 10, &interval_ms))
273 return -EINVAL;
274 if ((interval_ms < MPU3050_MIN_POLL_INTERVAL) ||
275 (interval_ms > MPU3050_MAX_POLL_INTERVAL))
276 return -EINVAL;
277
Wentao Xu04f4cc92012-08-15 19:41:43 -0400278 dlpf_index = interval_to_dlpf_cfg(interval_ms);
279 divider = interval_ms * dlpf_table[dlpf_index].sample_rate - 1;
Wentao Xuc7769c02012-08-03 15:06:41 -0400280
Wentao Xu04f4cc92012-08-15 19:41:43 -0400281 if (sensor->dlpf_index != dlpf_index) {
282 /* Set low pass filter and full scale */
283 reg = dlpf_table[dlpf_index].cfg;
284 reg |= MPU3050_DEFAULT_FS_RANGE << 3;
285 reg |= MPU3050_EXT_SYNC_NONE << 5;
286 ret = i2c_smbus_write_byte_data(sensor->client,
287 MPU3050_DLPF_FS_SYNC, reg);
288 if (ret == 0)
289 sensor->dlpf_index = dlpf_index;
290 }
291
292 if (sensor->poll_interval != interval_ms) {
293 /* Output frequency divider. The poll interval */
294 ret = i2c_smbus_write_byte_data(sensor->client,
295 MPU3050_SMPLRT_DIV, divider);
296 if (ret == 0)
297 sensor->poll_interval = interval_ms;
298 }
Wentao Xuc7769c02012-08-03 15:06:41 -0400299
300 return size;
301}
302
Xiaocheng Li05adbd82013-06-30 17:21:58 -0700303/**
304 * Set/get enable function is just needed by sensor HAL.
Xiaocheng Li05adbd82013-06-30 17:21:58 -0700305 */
306
307static ssize_t mpu3050_attr_set_enable(struct device *dev,
308 struct device_attribute *attr,
309 const char *buf, size_t count)
310{
311 struct mpu3050_sensor *sensor = dev_get_drvdata(dev);
312 unsigned long val;
313
314 if (kstrtoul(buf, 10, &val))
315 return -EINVAL;
Xiaocheng Lib4cc7df2013-07-17 11:46:40 -0700316 sensor->enable = (u32)val == 0 ? 0 : 1;
317 if (sensor->enable) {
318 pm_runtime_get_sync(sensor->dev);
319 gpio_set_value(sensor->enable_gpio, 1);
320 if (sensor->use_poll)
321 schedule_delayed_work(&sensor->input_work,
322 msecs_to_jiffies(sensor->poll_interval));
Oliver Wang4b325d72013-08-12 17:30:12 +0800323 else {
Xiaocheng Lib4cc7df2013-07-17 11:46:40 -0700324 i2c_smbus_write_byte_data(sensor->client,
325 MPU3050_INT_CFG,
326 MPU3050_ACTIVE_LOW |
327 MPU3050_OPEN_DRAIN |
328 MPU3050_RAW_RDY_EN);
Oliver Wang4b325d72013-08-12 17:30:12 +0800329 enable_irq(sensor->client->irq);
330 }
Xiaocheng Lib4cc7df2013-07-17 11:46:40 -0700331 } else {
332 if (sensor->use_poll)
333 cancel_delayed_work_sync(&sensor->input_work);
Oliver Wang4b325d72013-08-12 17:30:12 +0800334 else
335 disable_irq(sensor->client->irq);
Xiaocheng Lib4cc7df2013-07-17 11:46:40 -0700336 gpio_set_value(sensor->enable_gpio, 0);
337 pm_runtime_put(sensor->dev);
338 }
Xiaocheng Li05adbd82013-06-30 17:21:58 -0700339 return count;
340}
341
342static ssize_t mpu3050_attr_get_enable(struct device *dev,
343 struct device_attribute *attr, char *buf)
344{
345 struct mpu3050_sensor *sensor = dev_get_drvdata(dev);
346
347 return snprintf(buf, 4, "%d\n", sensor->enable);
348}
349
Wentao Xuc7769c02012-08-03 15:06:41 -0400350static struct device_attribute attributes[] = {
Wentao Xu75a5e4b2012-11-15 16:30:15 -0500351 __ATTR(pollrate_ms, 0664,
Wentao Xuc7769c02012-08-03 15:06:41 -0400352 mpu3050_attr_get_polling_rate,
353 mpu3050_attr_set_polling_rate),
Richard Liuce2264d2013-04-19 13:35:46 -0700354 __ATTR(enable, 0644,
Xiaocheng Li05adbd82013-06-30 17:21:58 -0700355 mpu3050_attr_get_enable,
356 mpu3050_attr_set_enable),
Wentao Xuc7769c02012-08-03 15:06:41 -0400357};
358
359static int create_sysfs_interfaces(struct device *dev)
360{
361 int i;
362 int err;
363 for (i = 0; i < ARRAY_SIZE(attributes); i++) {
364 err = device_create_file(dev, attributes + i);
365 if (err)
366 goto error;
367 }
368 return 0;
369
370error:
371 for ( ; i >= 0; i--)
372 device_remove_file(dev, attributes + i);
373 dev_err(dev, "%s:Unable to create interface\n", __func__);
374 return err;
375}
376
377static int remove_sysfs_interfaces(struct device *dev)
378{
379 int i;
380 for (i = 0; i < ARRAY_SIZE(attributes); i++)
381 device_remove_file(dev, attributes + i);
382 return 0;
383}
384
385/**
Joseph Lai631b16e2011-06-27 13:26:53 -0700386 * mpu3050_xyz_read_reg - read the axes values
387 * @buffer: provide register addr and get register
388 * @length: length of register
389 *
390 * Reads the register values in one transaction or returns a negative
391 * error code on failure.
392 */
393static int mpu3050_xyz_read_reg(struct i2c_client *client,
394 u8 *buffer, int length)
395{
396 /*
397 * Annoying we can't make this const because the i2c layer doesn't
398 * declare input buffers const.
399 */
400 char cmd = MPU3050_XOUT_H;
401 struct i2c_msg msg[] = {
402 {
403 .addr = client->addr,
404 .flags = 0,
405 .len = 1,
406 .buf = &cmd,
407 },
408 {
409 .addr = client->addr,
410 .flags = I2C_M_RD,
411 .len = length,
412 .buf = buffer,
413 },
414 };
415
416 return i2c_transfer(client->adapter, msg, 2);
417}
418
419/**
420 * mpu3050_read_xyz - get co-ordinates from device
421 * @client: i2c address of sensor
422 * @coords: co-ordinates to update
423 *
424 * Return the converted X Y and Z co-ordinates from the sensor device
425 */
426static void mpu3050_read_xyz(struct i2c_client *client,
427 struct axis_data *coords)
428{
429 u16 buffer[3];
430
431 mpu3050_xyz_read_reg(client, (u8 *)buffer, 6);
432 coords->x = be16_to_cpu(buffer[0]);
433 coords->y = be16_to_cpu(buffer[1]);
434 coords->z = be16_to_cpu(buffer[2]);
435 dev_dbg(&client->dev, "%s: x %d, y %d, z %d\n", __func__,
436 coords->x, coords->y, coords->z);
437}
438
439/**
440 * mpu3050_set_power_mode - set the power mode
441 * @client: i2c client for the sensor
442 * @val: value to switch on/off of power, 1: normal power, 0: low power
443 *
444 * Put device to normal-power mode or low-power mode.
445 */
446static void mpu3050_set_power_mode(struct i2c_client *client, u8 val)
447{
448 u8 value;
Richard Liua1d406a2013-04-14 13:46:48 -0700449 struct mpu3050_sensor *sensor = i2c_get_clientdata(client);
Joseph Lai631b16e2011-06-27 13:26:53 -0700450
Wentao Xudac9e602012-06-12 11:52:34 -0400451 if (val) {
452 mpu3050_config_regulator(client, 1);
453 udelay(10);
Richard Liua1d406a2013-04-14 13:46:48 -0700454 gpio_set_value(sensor->enable_gpio, 1);
Oliver Wang4b325d72013-08-12 17:30:12 +0800455 msleep(60);
Wentao Xudac9e602012-06-12 11:52:34 -0400456 }
457
Joseph Lai631b16e2011-06-27 13:26:53 -0700458 value = i2c_smbus_read_byte_data(client, MPU3050_PWR_MGM);
459 value = (value & ~MPU3050_PWR_MGM_MASK) |
460 (((val << MPU3050_PWR_MGM_POS) & MPU3050_PWR_MGM_MASK) ^
461 MPU3050_PWR_MGM_MASK);
462 i2c_smbus_write_byte_data(client, MPU3050_PWR_MGM, value);
Wentao Xudac9e602012-06-12 11:52:34 -0400463
464 if (!val) {
465 udelay(10);
Richard Liua1d406a2013-04-14 13:46:48 -0700466 gpio_set_value(sensor->enable_gpio, 0);
467 udelay(10);
Wentao Xudac9e602012-06-12 11:52:34 -0400468 mpu3050_config_regulator(client, 0);
469 }
Joseph Lai631b16e2011-06-27 13:26:53 -0700470}
471
472/**
473 * mpu3050_input_open - called on input event open
474 * @input: input dev of opened device
475 *
476 * The input layer calls this function when input event is opened. The
477 * function will push the device to resume. Then, the device is ready
478 * to provide data.
479 */
480static int mpu3050_input_open(struct input_dev *input)
481{
482 struct mpu3050_sensor *sensor = input_get_drvdata(input);
Heikki Krogerus3b518722011-12-23 23:57:09 -0800483 int error;
Joseph Lai631b16e2011-06-27 13:26:53 -0700484
Wentao Xuc7769c02012-08-03 15:06:41 -0400485 pm_runtime_get_sync(sensor->dev);
Joseph Lai631b16e2011-06-27 13:26:53 -0700486
Heikki Krogerus3b518722011-12-23 23:57:09 -0800487 /* Enable interrupts */
488 error = i2c_smbus_write_byte_data(sensor->client, MPU3050_INT_CFG,
Wentao Xuc7769c02012-08-03 15:06:41 -0400489 MPU3050_ACTIVE_LOW |
490 MPU3050_OPEN_DRAIN |
491 MPU3050_RAW_RDY_EN);
Heikki Krogerus3b518722011-12-23 23:57:09 -0800492 if (error < 0) {
493 pm_runtime_put(sensor->dev);
494 return error;
495 }
Wentao Xudac9e602012-06-12 11:52:34 -0400496 if (sensor->use_poll)
497 schedule_delayed_work(&sensor->input_work,
Wentao Xuc7769c02012-08-03 15:06:41 -0400498 msecs_to_jiffies(sensor->poll_interval));
Heikki Krogerus3b518722011-12-23 23:57:09 -0800499
Joseph Lai631b16e2011-06-27 13:26:53 -0700500 return 0;
501}
502
503/**
504 * mpu3050_input_close - called on input event close
505 * @input: input dev of closed device
506 *
507 * The input layer calls this function when input event is closed. The
508 * function will push the device to suspend.
509 */
510static void mpu3050_input_close(struct input_dev *input)
511{
512 struct mpu3050_sensor *sensor = input_get_drvdata(input);
513
Wentao Xudac9e602012-06-12 11:52:34 -0400514 if (sensor->use_poll)
515 cancel_delayed_work_sync(&sensor->input_work);
516
Joseph Lai631b16e2011-06-27 13:26:53 -0700517 pm_runtime_put(sensor->dev);
518}
519
520/**
521 * mpu3050_interrupt_thread - handle an IRQ
522 * @irq: interrupt numner
523 * @data: the sensor
524 *
525 * Called by the kernel single threaded after an interrupt occurs. Read
526 * the sensor data and generate an input event for it.
527 */
528static irqreturn_t mpu3050_interrupt_thread(int irq, void *data)
529{
530 struct mpu3050_sensor *sensor = data;
531 struct axis_data axis;
532
533 mpu3050_read_xyz(sensor->client, &axis);
534
535 input_report_abs(sensor->idev, ABS_X, axis.x);
536 input_report_abs(sensor->idev, ABS_Y, axis.y);
537 input_report_abs(sensor->idev, ABS_Z, axis.z);
538 input_sync(sensor->idev);
539
540 return IRQ_HANDLED;
541}
542
543/**
Wentao Xudac9e602012-06-12 11:52:34 -0400544 * mpu3050_input_work_fn - polling work
545 * @work: the work struct
546 *
547 * Called by the work queue; read sensor data and generate an input
548 * event
549 */
550static void mpu3050_input_work_fn(struct work_struct *work)
551{
552 struct mpu3050_sensor *sensor;
553 struct axis_data axis;
554
555 sensor = container_of((struct delayed_work *)work,
556 struct mpu3050_sensor, input_work);
557
558 mpu3050_read_xyz(sensor->client, &axis);
559
560 input_report_abs(sensor->idev, ABS_X, axis.x);
561 input_report_abs(sensor->idev, ABS_Y, axis.y);
562 input_report_abs(sensor->idev, ABS_Z, axis.z);
563 input_sync(sensor->idev);
564
565 if (sensor->use_poll)
566 schedule_delayed_work(&sensor->input_work,
Wentao Xuc7769c02012-08-03 15:06:41 -0400567 msecs_to_jiffies(sensor->poll_interval));
Wentao Xudac9e602012-06-12 11:52:34 -0400568}
569
570/**
Heikki Krogeruscd314fa2011-12-24 00:09:04 -0800571 * mpu3050_hw_init - initialize hardware
572 * @sensor: the sensor
573 *
574 * Called during device probe; configures the sampling method.
575 */
576static int __devinit mpu3050_hw_init(struct mpu3050_sensor *sensor)
577{
578 struct i2c_client *client = sensor->client;
579 int ret;
580 u8 reg;
581
582 /* Reset */
583 ret = i2c_smbus_write_byte_data(client, MPU3050_PWR_MGM,
584 MPU3050_PWR_MGM_RESET);
585 if (ret < 0)
586 return ret;
587
588 ret = i2c_smbus_read_byte_data(client, MPU3050_PWR_MGM);
589 if (ret < 0)
590 return ret;
591
592 ret &= ~MPU3050_PWR_MGM_CLKSEL;
593 ret |= MPU3050_PWR_MGM_PLL_Z;
594 ret = i2c_smbus_write_byte_data(client, MPU3050_PWR_MGM, ret);
595 if (ret < 0)
596 return ret;
597
598 /* Output frequency divider. The poll interval */
599 ret = i2c_smbus_write_byte_data(client, MPU3050_SMPLRT_DIV,
Wentao Xuc7769c02012-08-03 15:06:41 -0400600 sensor->poll_interval - 1);
Heikki Krogeruscd314fa2011-12-24 00:09:04 -0800601 if (ret < 0)
602 return ret;
603
604 /* Set low pass filter and full scale */
Wentao Xu04f4cc92012-08-15 19:41:43 -0400605 reg = MPU3050_DLPF_CFG_42HZ;
606 reg |= MPU3050_DEFAULT_FS_RANGE << 3;
Heikki Krogeruscd314fa2011-12-24 00:09:04 -0800607 reg |= MPU3050_EXT_SYNC_NONE << 5;
608 ret = i2c_smbus_write_byte_data(client, MPU3050_DLPF_FS_SYNC, reg);
609 if (ret < 0)
610 return ret;
611
612 return 0;
613}
Richard Liua1d406a2013-04-14 13:46:48 -0700614#ifdef CONFIG_OF
615static int mpu3050_parse_dt(struct device *dev,
616 struct mpu3050_gyro_platform_data *pdata)
617{
618 int rc = 0;
619
620 rc = of_property_read_u32(dev->of_node, "invn,poll-interval",
621 &pdata->poll_interval);
622 if (rc) {
623 dev_err(dev, "Failed to read poll-interval\n");
624 return rc;
625 }
626
627 /* check gpio_int later, if it is invalid, just use poll */
628 pdata->gpio_int = of_get_named_gpio_flags(dev->of_node,
629 "invn,gpio-int", 0, NULL);
630
631 pdata->gpio_en = of_get_named_gpio_flags(dev->of_node,
632 "invn,gpio-en", 0, NULL);
633 if (!gpio_is_valid(pdata->gpio_en))
634 return -EINVAL;
635
636 return 0;
637}
638#else
639static int mpu3050_parse_dt(struct device *dev,
640 struct mpu3050_gyro_platform_data *pdata)
641{
642 return -EINVAL;
643}
644#endif
Heikki Krogeruscd314fa2011-12-24 00:09:04 -0800645
646/**
Joseph Lai631b16e2011-06-27 13:26:53 -0700647 * mpu3050_probe - device detection callback
648 * @client: i2c client of found device
649 * @id: id match information
650 *
651 * The I2C layer calls us when it believes a sensor is present at this
652 * address. Probe to see if this is correct and to validate the device.
653 *
654 * If present install the relevant sysfs interfaces and input device.
655 */
656static int __devinit mpu3050_probe(struct i2c_client *client,
657 const struct i2c_device_id *id)
658{
659 struct mpu3050_sensor *sensor;
660 struct input_dev *idev;
Richard Liua1d406a2013-04-14 13:46:48 -0700661 struct mpu3050_gyro_platform_data *pdata;
Joseph Lai631b16e2011-06-27 13:26:53 -0700662 int ret;
663 int error;
Richard Liua1d406a2013-04-14 13:46:48 -0700664 u32 i;
Joseph Lai631b16e2011-06-27 13:26:53 -0700665
666 sensor = kzalloc(sizeof(struct mpu3050_sensor), GFP_KERNEL);
667 idev = input_allocate_device();
668 if (!sensor || !idev) {
669 dev_err(&client->dev, "failed to allocate driver data\n");
670 error = -ENOMEM;
671 goto err_free_mem;
672 }
673
674 sensor->client = client;
675 sensor->dev = &client->dev;
676 sensor->idev = idev;
Wentao Xuc7769c02012-08-03 15:06:41 -0400677 i2c_set_clientdata(client, sensor);
Richard Liua1d406a2013-04-14 13:46:48 -0700678
679 if (client->dev.of_node) {
680 pdata = devm_kzalloc(&client->dev,
681 sizeof(struct mpu3050_gyro_platform_data), GFP_KERNEL);
682 if (!pdata) {
683 dev_err(&client->dev, "Failed to allcated memory\n");
684 error = -ENOMEM;
685 goto err_free_mem;
686 }
687 ret = mpu3050_parse_dt(&client->dev, pdata);
688 if (ret) {
689 dev_err(&client->dev, "Failed to parse device tree\n");
690 error = ret;
691 goto err_free_mem;
692 }
693 } else
694 pdata = client->dev.platform_data;
695 sensor->platform_data = pdata;
696
Wentao Xuc7769c02012-08-03 15:06:41 -0400697 if (sensor->platform_data) {
698 u32 interval = sensor->platform_data->poll_interval;
Richard Liua1d406a2013-04-14 13:46:48 -0700699 sensor->enable_gpio = sensor->platform_data->gpio_en;
Wentao Xuc7769c02012-08-03 15:06:41 -0400700
701 if ((interval < MPU3050_MIN_POLL_INTERVAL) ||
702 (interval > MPU3050_MAX_POLL_INTERVAL))
703 sensor->poll_interval = MPU3050_DEFAULT_POLL_INTERVAL;
704 else
705 sensor->poll_interval = interval;
706 } else {
707 sensor->poll_interval = MPU3050_DEFAULT_POLL_INTERVAL;
Richard Liua1d406a2013-04-14 13:46:48 -0700708 sensor->enable_gpio = -EINVAL;
709 }
710
711 if (gpio_is_valid(sensor->enable_gpio)) {
712 ret = gpio_request(sensor->enable_gpio, "GYRO_EN_PM");
713 gpio_direction_output(sensor->enable_gpio, 1);
Wentao Xuc7769c02012-08-03 15:06:41 -0400714 }
Joseph Lai631b16e2011-06-27 13:26:53 -0700715
716 mpu3050_set_power_mode(client, 1);
Joseph Lai631b16e2011-06-27 13:26:53 -0700717
718 ret = i2c_smbus_read_byte_data(client, MPU3050_CHIP_ID_REG);
719 if (ret < 0) {
720 dev_err(&client->dev, "failed to detect device\n");
721 error = -ENXIO;
722 goto err_free_mem;
723 }
724
Richard Liua1d406a2013-04-14 13:46:48 -0700725 for (i = 0; i < ARRAY_SIZE(mpu3050_chip_ids); i++)
726 if (ret == mpu3050_chip_ids[i])
727 break;
728
729 if (i == ARRAY_SIZE(mpu3050_chip_ids)) {
Joseph Lai631b16e2011-06-27 13:26:53 -0700730 dev_err(&client->dev, "unsupported chip id\n");
731 error = -ENXIO;
732 goto err_free_mem;
733 }
734
735 idev->name = "MPU3050";
736 idev->id.bustype = BUS_I2C;
737 idev->dev.parent = &client->dev;
738
739 idev->open = mpu3050_input_open;
740 idev->close = mpu3050_input_close;
741
Richard Liuce2264d2013-04-19 13:35:46 -0700742 input_set_capability(idev, EV_ABS, ABS_MISC);
Joseph Lai631b16e2011-06-27 13:26:53 -0700743 input_set_abs_params(idev, ABS_X,
744 MPU3050_MIN_VALUE, MPU3050_MAX_VALUE, 0, 0);
745 input_set_abs_params(idev, ABS_Y,
746 MPU3050_MIN_VALUE, MPU3050_MAX_VALUE, 0, 0);
747 input_set_abs_params(idev, ABS_Z,
748 MPU3050_MIN_VALUE, MPU3050_MAX_VALUE, 0, 0);
749
750 input_set_drvdata(idev, sensor);
751
752 pm_runtime_set_active(&client->dev);
753
Heikki Krogeruscd314fa2011-12-24 00:09:04 -0800754 error = mpu3050_hw_init(sensor);
755 if (error)
756 goto err_pm_set_suspended;
757
Wentao Xudac9e602012-06-12 11:52:34 -0400758 if (client->irq == 0) {
Wentao Xudac9e602012-06-12 11:52:34 -0400759 sensor->use_poll = 1;
Wentao Xuc7769c02012-08-03 15:06:41 -0400760 INIT_DELAYED_WORK(&sensor->input_work, mpu3050_input_work_fn);
Wentao Xudac9e602012-06-12 11:52:34 -0400761 } else {
762 sensor->use_poll = 0;
763
Wentao Xuc7769c02012-08-03 15:06:41 -0400764 if (gpio_is_valid(sensor->platform_data->gpio_int)) {
765 /* configure interrupt gpio */
766 ret = gpio_request(sensor->platform_data->gpio_int,
767 "gyro_gpio_int");
768 if (ret) {
769 pr_err("%s: unable to request interrupt gpio %d\n",
770 __func__,
771 sensor->platform_data->gpio_int);
772 goto err_pm_set_suspended;
773 }
774
775 ret = gpio_direction_input(
776 sensor->platform_data->gpio_int);
777 if (ret) {
778 pr_err("%s: unable to set direction for gpio %d\n",
779 __func__, sensor->platform_data->gpio_int);
780 goto err_free_gpio;
781 }
Richard Liua1d406a2013-04-14 13:46:48 -0700782 client->irq = gpio_to_irq(
783 sensor->platform_data->gpio_int);
784 } else {
785 ret = -EINVAL;
786 goto err_pm_set_suspended;
Wentao Xuc7769c02012-08-03 15:06:41 -0400787 }
788
Wentao Xudac9e602012-06-12 11:52:34 -0400789 error = request_threaded_irq(client->irq,
Joseph Lai631b16e2011-06-27 13:26:53 -0700790 NULL, mpu3050_interrupt_thread,
Wentao Xuc7769c02012-08-03 15:06:41 -0400791 IRQF_TRIGGER_FALLING,
Heikki Krogerus3b518722011-12-23 23:57:09 -0800792 "mpu3050", sensor);
Wentao Xudac9e602012-06-12 11:52:34 -0400793 if (error) {
794 dev_err(&client->dev,
795 "can't get IRQ %d, error %d\n",
796 client->irq, error);
797 goto err_pm_set_suspended;
798 }
Oliver Wang2f4baed2013-08-20 16:07:26 +0800799 disable_irq(client->irq);
Joseph Lai631b16e2011-06-27 13:26:53 -0700800 }
801
802 error = input_register_device(idev);
803 if (error) {
804 dev_err(&client->dev, "failed to register input device\n");
805 goto err_free_irq;
806 }
807
Richard Liua1d406a2013-04-14 13:46:48 -0700808 error = create_sysfs_interfaces(&idev->dev);
Wentao Xuc7769c02012-08-03 15:06:41 -0400809 if (error < 0) {
810 dev_err(&client->dev, "failed to create sysfs\n");
811 goto err_input_cleanup;
812 }
813
Xiaocheng Lib4cc7df2013-07-17 11:46:40 -0700814 pm_runtime_enable(&client->dev);
815 pm_runtime_set_autosuspend_delay(&client->dev, MPU3050_AUTO_DELAY);
Joseph Lai631b16e2011-06-27 13:26:53 -0700816
817 return 0;
818
Wentao Xuc7769c02012-08-03 15:06:41 -0400819err_input_cleanup:
820 input_unregister_device(idev);
Joseph Lai631b16e2011-06-27 13:26:53 -0700821err_free_irq:
Wentao Xudac9e602012-06-12 11:52:34 -0400822 if (client->irq > 0)
823 free_irq(client->irq, sensor);
Wentao Xuc7769c02012-08-03 15:06:41 -0400824err_free_gpio:
825 if ((client->irq > 0) &&
826 (gpio_is_valid(sensor->platform_data->gpio_int)))
827 gpio_free(sensor->platform_data->gpio_int);
Joseph Lai631b16e2011-06-27 13:26:53 -0700828err_pm_set_suspended:
829 pm_runtime_set_suspended(&client->dev);
830err_free_mem:
Axel Lind9b830f2011-08-11 09:19:29 -0700831 input_free_device(idev);
Joseph Lai631b16e2011-06-27 13:26:53 -0700832 kfree(sensor);
833 return error;
834}
835
836/**
837 * mpu3050_remove - remove a sensor
838 * @client: i2c client of sensor being removed
839 *
840 * Our sensor is going away, clean up the resources.
841 */
842static int __devexit mpu3050_remove(struct i2c_client *client)
843{
844 struct mpu3050_sensor *sensor = i2c_get_clientdata(client);
845
846 pm_runtime_disable(&client->dev);
847 pm_runtime_set_suspended(&client->dev);
848
Wentao Xuc7769c02012-08-03 15:06:41 -0400849 if (client->irq)
850 free_irq(client->irq, sensor);
851
852 remove_sysfs_interfaces(&client->dev);
Richard Liua1d406a2013-04-14 13:46:48 -0700853 if (gpio_is_valid(sensor->enable_gpio))
854 gpio_free(sensor->enable_gpio);
Joseph Lai631b16e2011-06-27 13:26:53 -0700855 input_unregister_device(sensor->idev);
Wentao Xuc7769c02012-08-03 15:06:41 -0400856
Joseph Lai631b16e2011-06-27 13:26:53 -0700857 kfree(sensor);
858
859 return 0;
860}
861
862#ifdef CONFIG_PM
863/**
864 * mpu3050_suspend - called on device suspend
865 * @dev: device being suspended
866 *
867 * Put the device into sleep mode before we suspend the machine.
868 */
869static int mpu3050_suspend(struct device *dev)
870{
871 struct i2c_client *client = to_i2c_client(dev);
Oliver Wang4b325d72013-08-12 17:30:12 +0800872 struct mpu3050_sensor *sensor = i2c_get_clientdata(client);
873
874 if (!sensor->use_poll)
875 disable_irq(client->irq);
Joseph Lai631b16e2011-06-27 13:26:53 -0700876
877 mpu3050_set_power_mode(client, 0);
878
879 return 0;
880}
881
882/**
883 * mpu3050_resume - called on device resume
884 * @dev: device being resumed
885 *
886 * Put the device into powered mode on resume.
887 */
888static int mpu3050_resume(struct device *dev)
889{
890 struct i2c_client *client = to_i2c_client(dev);
Oliver Wang4b325d72013-08-12 17:30:12 +0800891 struct mpu3050_sensor *sensor = i2c_get_clientdata(client);
Joseph Lai631b16e2011-06-27 13:26:53 -0700892
893 mpu3050_set_power_mode(client, 1);
Oliver Wang4b325d72013-08-12 17:30:12 +0800894
895 if (!sensor->use_poll)
896 enable_irq(client->irq);
Joseph Lai631b16e2011-06-27 13:26:53 -0700897
898 return 0;
899}
900#endif
901
902static UNIVERSAL_DEV_PM_OPS(mpu3050_pm, mpu3050_suspend, mpu3050_resume, NULL);
903
904static const struct i2c_device_id mpu3050_ids[] = {
905 { "mpu3050", 0 },
906 { }
907};
908MODULE_DEVICE_TABLE(i2c, mpu3050_ids);
909
Olof Johanssone9489812011-12-23 01:20:44 -0800910static const struct of_device_id mpu3050_of_match[] = {
911 { .compatible = "invn,mpu3050", },
912 { },
913};
914MODULE_DEVICE_TABLE(of, mpu3050_of_match);
915
Joseph Lai631b16e2011-06-27 13:26:53 -0700916static struct i2c_driver mpu3050_i2c_driver = {
917 .driver = {
918 .name = "mpu3050",
919 .owner = THIS_MODULE,
920 .pm = &mpu3050_pm,
Olof Johanssone9489812011-12-23 01:20:44 -0800921 .of_match_table = mpu3050_of_match,
Joseph Lai631b16e2011-06-27 13:26:53 -0700922 },
923 .probe = mpu3050_probe,
924 .remove = __devexit_p(mpu3050_remove),
925 .id_table = mpu3050_ids,
926};
927
Axel Lin1b92c1c2012-03-16 23:05:41 -0700928module_i2c_driver(mpu3050_i2c_driver);
Joseph Lai631b16e2011-06-27 13:26:53 -0700929
930MODULE_AUTHOR("Wistron Corp.");
931MODULE_DESCRIPTION("MPU3050 Tri-axis gyroscope driver");
932MODULE_LICENSE("GPL");