blob: ded2b6e6442a2cd1e7b73545811f23dc3dac0e35 [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));
323 else
324 i2c_smbus_write_byte_data(sensor->client,
325 MPU3050_INT_CFG,
326 MPU3050_ACTIVE_LOW |
327 MPU3050_OPEN_DRAIN |
328 MPU3050_RAW_RDY_EN);
329 } else {
330 if (sensor->use_poll)
331 cancel_delayed_work_sync(&sensor->input_work);
332 gpio_set_value(sensor->enable_gpio, 0);
333 pm_runtime_put(sensor->dev);
334 }
Xiaocheng Li05adbd82013-06-30 17:21:58 -0700335 return count;
336}
337
338static ssize_t mpu3050_attr_get_enable(struct device *dev,
339 struct device_attribute *attr, char *buf)
340{
341 struct mpu3050_sensor *sensor = dev_get_drvdata(dev);
342
343 return snprintf(buf, 4, "%d\n", sensor->enable);
344}
345
Wentao Xuc7769c02012-08-03 15:06:41 -0400346static struct device_attribute attributes[] = {
Wentao Xu75a5e4b2012-11-15 16:30:15 -0500347 __ATTR(pollrate_ms, 0664,
Wentao Xuc7769c02012-08-03 15:06:41 -0400348 mpu3050_attr_get_polling_rate,
349 mpu3050_attr_set_polling_rate),
Richard Liuce2264d2013-04-19 13:35:46 -0700350 __ATTR(enable, 0644,
Xiaocheng Li05adbd82013-06-30 17:21:58 -0700351 mpu3050_attr_get_enable,
352 mpu3050_attr_set_enable),
Wentao Xuc7769c02012-08-03 15:06:41 -0400353};
354
355static int create_sysfs_interfaces(struct device *dev)
356{
357 int i;
358 int err;
359 for (i = 0; i < ARRAY_SIZE(attributes); i++) {
360 err = device_create_file(dev, attributes + i);
361 if (err)
362 goto error;
363 }
364 return 0;
365
366error:
367 for ( ; i >= 0; i--)
368 device_remove_file(dev, attributes + i);
369 dev_err(dev, "%s:Unable to create interface\n", __func__);
370 return err;
371}
372
373static int remove_sysfs_interfaces(struct device *dev)
374{
375 int i;
376 for (i = 0; i < ARRAY_SIZE(attributes); i++)
377 device_remove_file(dev, attributes + i);
378 return 0;
379}
380
381/**
Joseph Lai631b16e2011-06-27 13:26:53 -0700382 * mpu3050_xyz_read_reg - read the axes values
383 * @buffer: provide register addr and get register
384 * @length: length of register
385 *
386 * Reads the register values in one transaction or returns a negative
387 * error code on failure.
388 */
389static int mpu3050_xyz_read_reg(struct i2c_client *client,
390 u8 *buffer, int length)
391{
392 /*
393 * Annoying we can't make this const because the i2c layer doesn't
394 * declare input buffers const.
395 */
396 char cmd = MPU3050_XOUT_H;
397 struct i2c_msg msg[] = {
398 {
399 .addr = client->addr,
400 .flags = 0,
401 .len = 1,
402 .buf = &cmd,
403 },
404 {
405 .addr = client->addr,
406 .flags = I2C_M_RD,
407 .len = length,
408 .buf = buffer,
409 },
410 };
411
412 return i2c_transfer(client->adapter, msg, 2);
413}
414
415/**
416 * mpu3050_read_xyz - get co-ordinates from device
417 * @client: i2c address of sensor
418 * @coords: co-ordinates to update
419 *
420 * Return the converted X Y and Z co-ordinates from the sensor device
421 */
422static void mpu3050_read_xyz(struct i2c_client *client,
423 struct axis_data *coords)
424{
425 u16 buffer[3];
426
427 mpu3050_xyz_read_reg(client, (u8 *)buffer, 6);
428 coords->x = be16_to_cpu(buffer[0]);
429 coords->y = be16_to_cpu(buffer[1]);
430 coords->z = be16_to_cpu(buffer[2]);
431 dev_dbg(&client->dev, "%s: x %d, y %d, z %d\n", __func__,
432 coords->x, coords->y, coords->z);
433}
434
435/**
436 * mpu3050_set_power_mode - set the power mode
437 * @client: i2c client for the sensor
438 * @val: value to switch on/off of power, 1: normal power, 0: low power
439 *
440 * Put device to normal-power mode or low-power mode.
441 */
442static void mpu3050_set_power_mode(struct i2c_client *client, u8 val)
443{
444 u8 value;
Richard Liua1d406a2013-04-14 13:46:48 -0700445 struct mpu3050_sensor *sensor = i2c_get_clientdata(client);
Joseph Lai631b16e2011-06-27 13:26:53 -0700446
Wentao Xudac9e602012-06-12 11:52:34 -0400447 if (val) {
448 mpu3050_config_regulator(client, 1);
449 udelay(10);
Richard Liua1d406a2013-04-14 13:46:48 -0700450 gpio_set_value(sensor->enable_gpio, 1);
Wentao Xudac9e602012-06-12 11:52:34 -0400451 }
452
Joseph Lai631b16e2011-06-27 13:26:53 -0700453 value = i2c_smbus_read_byte_data(client, MPU3050_PWR_MGM);
454 value = (value & ~MPU3050_PWR_MGM_MASK) |
455 (((val << MPU3050_PWR_MGM_POS) & MPU3050_PWR_MGM_MASK) ^
456 MPU3050_PWR_MGM_MASK);
457 i2c_smbus_write_byte_data(client, MPU3050_PWR_MGM, value);
Wentao Xudac9e602012-06-12 11:52:34 -0400458
459 if (!val) {
460 udelay(10);
Richard Liua1d406a2013-04-14 13:46:48 -0700461 gpio_set_value(sensor->enable_gpio, 0);
462 udelay(10);
Wentao Xudac9e602012-06-12 11:52:34 -0400463 mpu3050_config_regulator(client, 0);
464 }
Joseph Lai631b16e2011-06-27 13:26:53 -0700465}
466
467/**
468 * mpu3050_input_open - called on input event open
469 * @input: input dev of opened device
470 *
471 * The input layer calls this function when input event is opened. The
472 * function will push the device to resume. Then, the device is ready
473 * to provide data.
474 */
475static int mpu3050_input_open(struct input_dev *input)
476{
477 struct mpu3050_sensor *sensor = input_get_drvdata(input);
Heikki Krogerus3b518722011-12-23 23:57:09 -0800478 int error;
Joseph Lai631b16e2011-06-27 13:26:53 -0700479
Wentao Xuc7769c02012-08-03 15:06:41 -0400480 pm_runtime_get_sync(sensor->dev);
Joseph Lai631b16e2011-06-27 13:26:53 -0700481
Heikki Krogerus3b518722011-12-23 23:57:09 -0800482 /* Enable interrupts */
483 error = i2c_smbus_write_byte_data(sensor->client, MPU3050_INT_CFG,
Wentao Xuc7769c02012-08-03 15:06:41 -0400484 MPU3050_ACTIVE_LOW |
485 MPU3050_OPEN_DRAIN |
486 MPU3050_RAW_RDY_EN);
Heikki Krogerus3b518722011-12-23 23:57:09 -0800487 if (error < 0) {
488 pm_runtime_put(sensor->dev);
489 return error;
490 }
Wentao Xudac9e602012-06-12 11:52:34 -0400491 if (sensor->use_poll)
492 schedule_delayed_work(&sensor->input_work,
Wentao Xuc7769c02012-08-03 15:06:41 -0400493 msecs_to_jiffies(sensor->poll_interval));
Heikki Krogerus3b518722011-12-23 23:57:09 -0800494
Joseph Lai631b16e2011-06-27 13:26:53 -0700495 return 0;
496}
497
498/**
499 * mpu3050_input_close - called on input event close
500 * @input: input dev of closed device
501 *
502 * The input layer calls this function when input event is closed. The
503 * function will push the device to suspend.
504 */
505static void mpu3050_input_close(struct input_dev *input)
506{
507 struct mpu3050_sensor *sensor = input_get_drvdata(input);
508
Wentao Xudac9e602012-06-12 11:52:34 -0400509 if (sensor->use_poll)
510 cancel_delayed_work_sync(&sensor->input_work);
511
Joseph Lai631b16e2011-06-27 13:26:53 -0700512 pm_runtime_put(sensor->dev);
513}
514
515/**
516 * mpu3050_interrupt_thread - handle an IRQ
517 * @irq: interrupt numner
518 * @data: the sensor
519 *
520 * Called by the kernel single threaded after an interrupt occurs. Read
521 * the sensor data and generate an input event for it.
522 */
523static irqreturn_t mpu3050_interrupt_thread(int irq, void *data)
524{
525 struct mpu3050_sensor *sensor = data;
526 struct axis_data axis;
527
528 mpu3050_read_xyz(sensor->client, &axis);
529
530 input_report_abs(sensor->idev, ABS_X, axis.x);
531 input_report_abs(sensor->idev, ABS_Y, axis.y);
532 input_report_abs(sensor->idev, ABS_Z, axis.z);
533 input_sync(sensor->idev);
534
535 return IRQ_HANDLED;
536}
537
538/**
Wentao Xudac9e602012-06-12 11:52:34 -0400539 * mpu3050_input_work_fn - polling work
540 * @work: the work struct
541 *
542 * Called by the work queue; read sensor data and generate an input
543 * event
544 */
545static void mpu3050_input_work_fn(struct work_struct *work)
546{
547 struct mpu3050_sensor *sensor;
548 struct axis_data axis;
549
550 sensor = container_of((struct delayed_work *)work,
551 struct mpu3050_sensor, input_work);
552
553 mpu3050_read_xyz(sensor->client, &axis);
554
555 input_report_abs(sensor->idev, ABS_X, axis.x);
556 input_report_abs(sensor->idev, ABS_Y, axis.y);
557 input_report_abs(sensor->idev, ABS_Z, axis.z);
558 input_sync(sensor->idev);
559
560 if (sensor->use_poll)
561 schedule_delayed_work(&sensor->input_work,
Wentao Xuc7769c02012-08-03 15:06:41 -0400562 msecs_to_jiffies(sensor->poll_interval));
Wentao Xudac9e602012-06-12 11:52:34 -0400563}
564
565/**
Heikki Krogeruscd314fa2011-12-24 00:09:04 -0800566 * mpu3050_hw_init - initialize hardware
567 * @sensor: the sensor
568 *
569 * Called during device probe; configures the sampling method.
570 */
571static int __devinit mpu3050_hw_init(struct mpu3050_sensor *sensor)
572{
573 struct i2c_client *client = sensor->client;
574 int ret;
575 u8 reg;
576
577 /* Reset */
578 ret = i2c_smbus_write_byte_data(client, MPU3050_PWR_MGM,
579 MPU3050_PWR_MGM_RESET);
580 if (ret < 0)
581 return ret;
582
583 ret = i2c_smbus_read_byte_data(client, MPU3050_PWR_MGM);
584 if (ret < 0)
585 return ret;
586
587 ret &= ~MPU3050_PWR_MGM_CLKSEL;
588 ret |= MPU3050_PWR_MGM_PLL_Z;
589 ret = i2c_smbus_write_byte_data(client, MPU3050_PWR_MGM, ret);
590 if (ret < 0)
591 return ret;
592
593 /* Output frequency divider. The poll interval */
594 ret = i2c_smbus_write_byte_data(client, MPU3050_SMPLRT_DIV,
Wentao Xuc7769c02012-08-03 15:06:41 -0400595 sensor->poll_interval - 1);
Heikki Krogeruscd314fa2011-12-24 00:09:04 -0800596 if (ret < 0)
597 return ret;
598
599 /* Set low pass filter and full scale */
Wentao Xu04f4cc92012-08-15 19:41:43 -0400600 reg = MPU3050_DLPF_CFG_42HZ;
601 reg |= MPU3050_DEFAULT_FS_RANGE << 3;
Heikki Krogeruscd314fa2011-12-24 00:09:04 -0800602 reg |= MPU3050_EXT_SYNC_NONE << 5;
603 ret = i2c_smbus_write_byte_data(client, MPU3050_DLPF_FS_SYNC, reg);
604 if (ret < 0)
605 return ret;
606
607 return 0;
608}
Richard Liua1d406a2013-04-14 13:46:48 -0700609#ifdef CONFIG_OF
610static int mpu3050_parse_dt(struct device *dev,
611 struct mpu3050_gyro_platform_data *pdata)
612{
613 int rc = 0;
614
615 rc = of_property_read_u32(dev->of_node, "invn,poll-interval",
616 &pdata->poll_interval);
617 if (rc) {
618 dev_err(dev, "Failed to read poll-interval\n");
619 return rc;
620 }
621
622 /* check gpio_int later, if it is invalid, just use poll */
623 pdata->gpio_int = of_get_named_gpio_flags(dev->of_node,
624 "invn,gpio-int", 0, NULL);
625
626 pdata->gpio_en = of_get_named_gpio_flags(dev->of_node,
627 "invn,gpio-en", 0, NULL);
628 if (!gpio_is_valid(pdata->gpio_en))
629 return -EINVAL;
630
631 return 0;
632}
633#else
634static int mpu3050_parse_dt(struct device *dev,
635 struct mpu3050_gyro_platform_data *pdata)
636{
637 return -EINVAL;
638}
639#endif
Heikki Krogeruscd314fa2011-12-24 00:09:04 -0800640
641/**
Joseph Lai631b16e2011-06-27 13:26:53 -0700642 * mpu3050_probe - device detection callback
643 * @client: i2c client of found device
644 * @id: id match information
645 *
646 * The I2C layer calls us when it believes a sensor is present at this
647 * address. Probe to see if this is correct and to validate the device.
648 *
649 * If present install the relevant sysfs interfaces and input device.
650 */
651static int __devinit mpu3050_probe(struct i2c_client *client,
652 const struct i2c_device_id *id)
653{
654 struct mpu3050_sensor *sensor;
655 struct input_dev *idev;
Richard Liua1d406a2013-04-14 13:46:48 -0700656 struct mpu3050_gyro_platform_data *pdata;
Joseph Lai631b16e2011-06-27 13:26:53 -0700657 int ret;
658 int error;
Richard Liua1d406a2013-04-14 13:46:48 -0700659 u32 i;
Joseph Lai631b16e2011-06-27 13:26:53 -0700660
661 sensor = kzalloc(sizeof(struct mpu3050_sensor), GFP_KERNEL);
662 idev = input_allocate_device();
663 if (!sensor || !idev) {
664 dev_err(&client->dev, "failed to allocate driver data\n");
665 error = -ENOMEM;
666 goto err_free_mem;
667 }
668
669 sensor->client = client;
670 sensor->dev = &client->dev;
671 sensor->idev = idev;
Wentao Xuc7769c02012-08-03 15:06:41 -0400672 i2c_set_clientdata(client, sensor);
Richard Liua1d406a2013-04-14 13:46:48 -0700673
674 if (client->dev.of_node) {
675 pdata = devm_kzalloc(&client->dev,
676 sizeof(struct mpu3050_gyro_platform_data), GFP_KERNEL);
677 if (!pdata) {
678 dev_err(&client->dev, "Failed to allcated memory\n");
679 error = -ENOMEM;
680 goto err_free_mem;
681 }
682 ret = mpu3050_parse_dt(&client->dev, pdata);
683 if (ret) {
684 dev_err(&client->dev, "Failed to parse device tree\n");
685 error = ret;
686 goto err_free_mem;
687 }
688 } else
689 pdata = client->dev.platform_data;
690 sensor->platform_data = pdata;
691
Wentao Xuc7769c02012-08-03 15:06:41 -0400692 if (sensor->platform_data) {
693 u32 interval = sensor->platform_data->poll_interval;
Richard Liua1d406a2013-04-14 13:46:48 -0700694 sensor->enable_gpio = sensor->platform_data->gpio_en;
Wentao Xuc7769c02012-08-03 15:06:41 -0400695
696 if ((interval < MPU3050_MIN_POLL_INTERVAL) ||
697 (interval > MPU3050_MAX_POLL_INTERVAL))
698 sensor->poll_interval = MPU3050_DEFAULT_POLL_INTERVAL;
699 else
700 sensor->poll_interval = interval;
701 } else {
702 sensor->poll_interval = MPU3050_DEFAULT_POLL_INTERVAL;
Richard Liua1d406a2013-04-14 13:46:48 -0700703 sensor->enable_gpio = -EINVAL;
704 }
705
706 if (gpio_is_valid(sensor->enable_gpio)) {
707 ret = gpio_request(sensor->enable_gpio, "GYRO_EN_PM");
708 gpio_direction_output(sensor->enable_gpio, 1);
Wentao Xuc7769c02012-08-03 15:06:41 -0400709 }
Joseph Lai631b16e2011-06-27 13:26:53 -0700710
711 mpu3050_set_power_mode(client, 1);
712 msleep(10);
713
714 ret = i2c_smbus_read_byte_data(client, MPU3050_CHIP_ID_REG);
715 if (ret < 0) {
716 dev_err(&client->dev, "failed to detect device\n");
717 error = -ENXIO;
718 goto err_free_mem;
719 }
720
Richard Liua1d406a2013-04-14 13:46:48 -0700721 for (i = 0; i < ARRAY_SIZE(mpu3050_chip_ids); i++)
722 if (ret == mpu3050_chip_ids[i])
723 break;
724
725 if (i == ARRAY_SIZE(mpu3050_chip_ids)) {
Joseph Lai631b16e2011-06-27 13:26:53 -0700726 dev_err(&client->dev, "unsupported chip id\n");
727 error = -ENXIO;
728 goto err_free_mem;
729 }
730
731 idev->name = "MPU3050";
732 idev->id.bustype = BUS_I2C;
733 idev->dev.parent = &client->dev;
734
735 idev->open = mpu3050_input_open;
736 idev->close = mpu3050_input_close;
737
Richard Liuce2264d2013-04-19 13:35:46 -0700738 input_set_capability(idev, EV_ABS, ABS_MISC);
Joseph Lai631b16e2011-06-27 13:26:53 -0700739 input_set_abs_params(idev, ABS_X,
740 MPU3050_MIN_VALUE, MPU3050_MAX_VALUE, 0, 0);
741 input_set_abs_params(idev, ABS_Y,
742 MPU3050_MIN_VALUE, MPU3050_MAX_VALUE, 0, 0);
743 input_set_abs_params(idev, ABS_Z,
744 MPU3050_MIN_VALUE, MPU3050_MAX_VALUE, 0, 0);
745
746 input_set_drvdata(idev, sensor);
747
748 pm_runtime_set_active(&client->dev);
749
Heikki Krogeruscd314fa2011-12-24 00:09:04 -0800750 error = mpu3050_hw_init(sensor);
751 if (error)
752 goto err_pm_set_suspended;
753
Wentao Xudac9e602012-06-12 11:52:34 -0400754 if (client->irq == 0) {
Wentao Xudac9e602012-06-12 11:52:34 -0400755 sensor->use_poll = 1;
Wentao Xuc7769c02012-08-03 15:06:41 -0400756 INIT_DELAYED_WORK(&sensor->input_work, mpu3050_input_work_fn);
Wentao Xudac9e602012-06-12 11:52:34 -0400757 } else {
758 sensor->use_poll = 0;
759
Wentao Xuc7769c02012-08-03 15:06:41 -0400760 if (gpio_is_valid(sensor->platform_data->gpio_int)) {
761 /* configure interrupt gpio */
762 ret = gpio_request(sensor->platform_data->gpio_int,
763 "gyro_gpio_int");
764 if (ret) {
765 pr_err("%s: unable to request interrupt gpio %d\n",
766 __func__,
767 sensor->platform_data->gpio_int);
768 goto err_pm_set_suspended;
769 }
770
771 ret = gpio_direction_input(
772 sensor->platform_data->gpio_int);
773 if (ret) {
774 pr_err("%s: unable to set direction for gpio %d\n",
775 __func__, sensor->platform_data->gpio_int);
776 goto err_free_gpio;
777 }
Richard Liua1d406a2013-04-14 13:46:48 -0700778 client->irq = gpio_to_irq(
779 sensor->platform_data->gpio_int);
780 } else {
781 ret = -EINVAL;
782 goto err_pm_set_suspended;
Wentao Xuc7769c02012-08-03 15:06:41 -0400783 }
784
Wentao Xudac9e602012-06-12 11:52:34 -0400785 error = request_threaded_irq(client->irq,
Joseph Lai631b16e2011-06-27 13:26:53 -0700786 NULL, mpu3050_interrupt_thread,
Wentao Xuc7769c02012-08-03 15:06:41 -0400787 IRQF_TRIGGER_FALLING,
Heikki Krogerus3b518722011-12-23 23:57:09 -0800788 "mpu3050", sensor);
Wentao Xudac9e602012-06-12 11:52:34 -0400789 if (error) {
790 dev_err(&client->dev,
791 "can't get IRQ %d, error %d\n",
792 client->irq, error);
793 goto err_pm_set_suspended;
794 }
Joseph Lai631b16e2011-06-27 13:26:53 -0700795 }
796
797 error = input_register_device(idev);
798 if (error) {
799 dev_err(&client->dev, "failed to register input device\n");
800 goto err_free_irq;
801 }
802
Richard Liua1d406a2013-04-14 13:46:48 -0700803 error = create_sysfs_interfaces(&idev->dev);
Wentao Xuc7769c02012-08-03 15:06:41 -0400804 if (error < 0) {
805 dev_err(&client->dev, "failed to create sysfs\n");
806 goto err_input_cleanup;
807 }
808
Xiaocheng Lib4cc7df2013-07-17 11:46:40 -0700809 pm_runtime_enable(&client->dev);
810 pm_runtime_set_autosuspend_delay(&client->dev, MPU3050_AUTO_DELAY);
Joseph Lai631b16e2011-06-27 13:26:53 -0700811
812 return 0;
813
Wentao Xuc7769c02012-08-03 15:06:41 -0400814err_input_cleanup:
815 input_unregister_device(idev);
Joseph Lai631b16e2011-06-27 13:26:53 -0700816err_free_irq:
Wentao Xudac9e602012-06-12 11:52:34 -0400817 if (client->irq > 0)
818 free_irq(client->irq, sensor);
Wentao Xuc7769c02012-08-03 15:06:41 -0400819err_free_gpio:
820 if ((client->irq > 0) &&
821 (gpio_is_valid(sensor->platform_data->gpio_int)))
822 gpio_free(sensor->platform_data->gpio_int);
Joseph Lai631b16e2011-06-27 13:26:53 -0700823err_pm_set_suspended:
824 pm_runtime_set_suspended(&client->dev);
825err_free_mem:
Axel Lind9b830f2011-08-11 09:19:29 -0700826 input_free_device(idev);
Joseph Lai631b16e2011-06-27 13:26:53 -0700827 kfree(sensor);
828 return error;
829}
830
831/**
832 * mpu3050_remove - remove a sensor
833 * @client: i2c client of sensor being removed
834 *
835 * Our sensor is going away, clean up the resources.
836 */
837static int __devexit mpu3050_remove(struct i2c_client *client)
838{
839 struct mpu3050_sensor *sensor = i2c_get_clientdata(client);
840
841 pm_runtime_disable(&client->dev);
842 pm_runtime_set_suspended(&client->dev);
843
Wentao Xuc7769c02012-08-03 15:06:41 -0400844 if (client->irq)
845 free_irq(client->irq, sensor);
846
847 remove_sysfs_interfaces(&client->dev);
Richard Liua1d406a2013-04-14 13:46:48 -0700848 if (gpio_is_valid(sensor->enable_gpio))
849 gpio_free(sensor->enable_gpio);
Joseph Lai631b16e2011-06-27 13:26:53 -0700850 input_unregister_device(sensor->idev);
Wentao Xuc7769c02012-08-03 15:06:41 -0400851
Joseph Lai631b16e2011-06-27 13:26:53 -0700852 kfree(sensor);
853
854 return 0;
855}
856
857#ifdef CONFIG_PM
858/**
859 * mpu3050_suspend - called on device suspend
860 * @dev: device being suspended
861 *
862 * Put the device into sleep mode before we suspend the machine.
863 */
864static int mpu3050_suspend(struct device *dev)
865{
866 struct i2c_client *client = to_i2c_client(dev);
867
868 mpu3050_set_power_mode(client, 0);
869
870 return 0;
871}
872
873/**
874 * mpu3050_resume - called on device resume
875 * @dev: device being resumed
876 *
877 * Put the device into powered mode on resume.
878 */
879static int mpu3050_resume(struct device *dev)
880{
881 struct i2c_client *client = to_i2c_client(dev);
882
883 mpu3050_set_power_mode(client, 1);
884 msleep(100); /* wait for gyro chip resume */
885
886 return 0;
887}
888#endif
889
890static UNIVERSAL_DEV_PM_OPS(mpu3050_pm, mpu3050_suspend, mpu3050_resume, NULL);
891
892static const struct i2c_device_id mpu3050_ids[] = {
893 { "mpu3050", 0 },
894 { }
895};
896MODULE_DEVICE_TABLE(i2c, mpu3050_ids);
897
Olof Johanssone9489812011-12-23 01:20:44 -0800898static const struct of_device_id mpu3050_of_match[] = {
899 { .compatible = "invn,mpu3050", },
900 { },
901};
902MODULE_DEVICE_TABLE(of, mpu3050_of_match);
903
Joseph Lai631b16e2011-06-27 13:26:53 -0700904static struct i2c_driver mpu3050_i2c_driver = {
905 .driver = {
906 .name = "mpu3050",
907 .owner = THIS_MODULE,
908 .pm = &mpu3050_pm,
Olof Johanssone9489812011-12-23 01:20:44 -0800909 .of_match_table = mpu3050_of_match,
Joseph Lai631b16e2011-06-27 13:26:53 -0700910 },
911 .probe = mpu3050_probe,
912 .remove = __devexit_p(mpu3050_remove),
913 .id_table = mpu3050_ids,
914};
915
Axel Lin1b92c1c2012-03-16 23:05:41 -0700916module_i2c_driver(mpu3050_i2c_driver);
Joseph Lai631b16e2011-06-27 13:26:53 -0700917
918MODULE_AUTHOR("Wistron Corp.");
919MODULE_DESCRIPTION("MPU3050 Tri-axis gyroscope driver");
920MODULE_LICENSE("GPL");