blob: 519b7e4d88e27b342c50c1259bfdea16982fa0c2 [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.
305 * Normally, the open function does all the initialization
306 * and power work. And close undo that open does.
307 * Just keeping the function simple.
308 */
309
310static ssize_t mpu3050_attr_set_enable(struct device *dev,
311 struct device_attribute *attr,
312 const char *buf, size_t count)
313{
314 struct mpu3050_sensor *sensor = dev_get_drvdata(dev);
315 unsigned long val;
316
317 if (kstrtoul(buf, 10, &val))
318 return -EINVAL;
319 sensor->enable = (u32)val;
320
321 return count;
322}
323
324static ssize_t mpu3050_attr_get_enable(struct device *dev,
325 struct device_attribute *attr, char *buf)
326{
327 struct mpu3050_sensor *sensor = dev_get_drvdata(dev);
328
329 return snprintf(buf, 4, "%d\n", sensor->enable);
330}
331
Wentao Xuc7769c02012-08-03 15:06:41 -0400332static struct device_attribute attributes[] = {
Wentao Xu75a5e4b2012-11-15 16:30:15 -0500333 __ATTR(pollrate_ms, 0664,
Wentao Xuc7769c02012-08-03 15:06:41 -0400334 mpu3050_attr_get_polling_rate,
335 mpu3050_attr_set_polling_rate),
Richard Liuce2264d2013-04-19 13:35:46 -0700336 __ATTR(enable, 0644,
Xiaocheng Li05adbd82013-06-30 17:21:58 -0700337 mpu3050_attr_get_enable,
338 mpu3050_attr_set_enable),
Wentao Xuc7769c02012-08-03 15:06:41 -0400339};
340
341static int create_sysfs_interfaces(struct device *dev)
342{
343 int i;
344 int err;
345 for (i = 0; i < ARRAY_SIZE(attributes); i++) {
346 err = device_create_file(dev, attributes + i);
347 if (err)
348 goto error;
349 }
350 return 0;
351
352error:
353 for ( ; i >= 0; i--)
354 device_remove_file(dev, attributes + i);
355 dev_err(dev, "%s:Unable to create interface\n", __func__);
356 return err;
357}
358
359static int remove_sysfs_interfaces(struct device *dev)
360{
361 int i;
362 for (i = 0; i < ARRAY_SIZE(attributes); i++)
363 device_remove_file(dev, attributes + i);
364 return 0;
365}
366
367/**
Joseph Lai631b16e2011-06-27 13:26:53 -0700368 * mpu3050_xyz_read_reg - read the axes values
369 * @buffer: provide register addr and get register
370 * @length: length of register
371 *
372 * Reads the register values in one transaction or returns a negative
373 * error code on failure.
374 */
375static int mpu3050_xyz_read_reg(struct i2c_client *client,
376 u8 *buffer, int length)
377{
378 /*
379 * Annoying we can't make this const because the i2c layer doesn't
380 * declare input buffers const.
381 */
382 char cmd = MPU3050_XOUT_H;
383 struct i2c_msg msg[] = {
384 {
385 .addr = client->addr,
386 .flags = 0,
387 .len = 1,
388 .buf = &cmd,
389 },
390 {
391 .addr = client->addr,
392 .flags = I2C_M_RD,
393 .len = length,
394 .buf = buffer,
395 },
396 };
397
398 return i2c_transfer(client->adapter, msg, 2);
399}
400
401/**
402 * mpu3050_read_xyz - get co-ordinates from device
403 * @client: i2c address of sensor
404 * @coords: co-ordinates to update
405 *
406 * Return the converted X Y and Z co-ordinates from the sensor device
407 */
408static void mpu3050_read_xyz(struct i2c_client *client,
409 struct axis_data *coords)
410{
411 u16 buffer[3];
412
413 mpu3050_xyz_read_reg(client, (u8 *)buffer, 6);
414 coords->x = be16_to_cpu(buffer[0]);
415 coords->y = be16_to_cpu(buffer[1]);
416 coords->z = be16_to_cpu(buffer[2]);
417 dev_dbg(&client->dev, "%s: x %d, y %d, z %d\n", __func__,
418 coords->x, coords->y, coords->z);
419}
420
421/**
422 * mpu3050_set_power_mode - set the power mode
423 * @client: i2c client for the sensor
424 * @val: value to switch on/off of power, 1: normal power, 0: low power
425 *
426 * Put device to normal-power mode or low-power mode.
427 */
428static void mpu3050_set_power_mode(struct i2c_client *client, u8 val)
429{
430 u8 value;
Richard Liua1d406a2013-04-14 13:46:48 -0700431 struct mpu3050_sensor *sensor = i2c_get_clientdata(client);
Joseph Lai631b16e2011-06-27 13:26:53 -0700432
Wentao Xudac9e602012-06-12 11:52:34 -0400433 if (val) {
434 mpu3050_config_regulator(client, 1);
435 udelay(10);
Richard Liua1d406a2013-04-14 13:46:48 -0700436 gpio_set_value(sensor->enable_gpio, 1);
Wentao Xudac9e602012-06-12 11:52:34 -0400437 }
438
Joseph Lai631b16e2011-06-27 13:26:53 -0700439 value = i2c_smbus_read_byte_data(client, MPU3050_PWR_MGM);
440 value = (value & ~MPU3050_PWR_MGM_MASK) |
441 (((val << MPU3050_PWR_MGM_POS) & MPU3050_PWR_MGM_MASK) ^
442 MPU3050_PWR_MGM_MASK);
443 i2c_smbus_write_byte_data(client, MPU3050_PWR_MGM, value);
Wentao Xudac9e602012-06-12 11:52:34 -0400444
445 if (!val) {
446 udelay(10);
Richard Liua1d406a2013-04-14 13:46:48 -0700447 gpio_set_value(sensor->enable_gpio, 0);
448 udelay(10);
Wentao Xudac9e602012-06-12 11:52:34 -0400449 mpu3050_config_regulator(client, 0);
450 }
Joseph Lai631b16e2011-06-27 13:26:53 -0700451}
452
453/**
454 * mpu3050_input_open - called on input event open
455 * @input: input dev of opened device
456 *
457 * The input layer calls this function when input event is opened. The
458 * function will push the device to resume. Then, the device is ready
459 * to provide data.
460 */
461static int mpu3050_input_open(struct input_dev *input)
462{
463 struct mpu3050_sensor *sensor = input_get_drvdata(input);
Heikki Krogerus3b518722011-12-23 23:57:09 -0800464 int error;
Joseph Lai631b16e2011-06-27 13:26:53 -0700465
Wentao Xuc7769c02012-08-03 15:06:41 -0400466 pm_runtime_get_sync(sensor->dev);
Joseph Lai631b16e2011-06-27 13:26:53 -0700467
Heikki Krogerus3b518722011-12-23 23:57:09 -0800468 /* Enable interrupts */
469 error = i2c_smbus_write_byte_data(sensor->client, MPU3050_INT_CFG,
Wentao Xuc7769c02012-08-03 15:06:41 -0400470 MPU3050_ACTIVE_LOW |
471 MPU3050_OPEN_DRAIN |
472 MPU3050_RAW_RDY_EN);
Heikki Krogerus3b518722011-12-23 23:57:09 -0800473 if (error < 0) {
474 pm_runtime_put(sensor->dev);
475 return error;
476 }
Wentao Xudac9e602012-06-12 11:52:34 -0400477 if (sensor->use_poll)
478 schedule_delayed_work(&sensor->input_work,
Wentao Xuc7769c02012-08-03 15:06:41 -0400479 msecs_to_jiffies(sensor->poll_interval));
Heikki Krogerus3b518722011-12-23 23:57:09 -0800480
Joseph Lai631b16e2011-06-27 13:26:53 -0700481 return 0;
482}
483
484/**
485 * mpu3050_input_close - called on input event close
486 * @input: input dev of closed device
487 *
488 * The input layer calls this function when input event is closed. The
489 * function will push the device to suspend.
490 */
491static void mpu3050_input_close(struct input_dev *input)
492{
493 struct mpu3050_sensor *sensor = input_get_drvdata(input);
494
Wentao Xudac9e602012-06-12 11:52:34 -0400495 if (sensor->use_poll)
496 cancel_delayed_work_sync(&sensor->input_work);
497
Joseph Lai631b16e2011-06-27 13:26:53 -0700498 pm_runtime_put(sensor->dev);
499}
500
501/**
502 * mpu3050_interrupt_thread - handle an IRQ
503 * @irq: interrupt numner
504 * @data: the sensor
505 *
506 * Called by the kernel single threaded after an interrupt occurs. Read
507 * the sensor data and generate an input event for it.
508 */
509static irqreturn_t mpu3050_interrupt_thread(int irq, void *data)
510{
511 struct mpu3050_sensor *sensor = data;
512 struct axis_data axis;
513
514 mpu3050_read_xyz(sensor->client, &axis);
515
516 input_report_abs(sensor->idev, ABS_X, axis.x);
517 input_report_abs(sensor->idev, ABS_Y, axis.y);
518 input_report_abs(sensor->idev, ABS_Z, axis.z);
519 input_sync(sensor->idev);
520
521 return IRQ_HANDLED;
522}
523
524/**
Wentao Xudac9e602012-06-12 11:52:34 -0400525 * mpu3050_input_work_fn - polling work
526 * @work: the work struct
527 *
528 * Called by the work queue; read sensor data and generate an input
529 * event
530 */
531static void mpu3050_input_work_fn(struct work_struct *work)
532{
533 struct mpu3050_sensor *sensor;
534 struct axis_data axis;
535
536 sensor = container_of((struct delayed_work *)work,
537 struct mpu3050_sensor, input_work);
538
539 mpu3050_read_xyz(sensor->client, &axis);
540
541 input_report_abs(sensor->idev, ABS_X, axis.x);
542 input_report_abs(sensor->idev, ABS_Y, axis.y);
543 input_report_abs(sensor->idev, ABS_Z, axis.z);
544 input_sync(sensor->idev);
545
546 if (sensor->use_poll)
547 schedule_delayed_work(&sensor->input_work,
Wentao Xuc7769c02012-08-03 15:06:41 -0400548 msecs_to_jiffies(sensor->poll_interval));
Wentao Xudac9e602012-06-12 11:52:34 -0400549}
550
551/**
Heikki Krogeruscd314fa2011-12-24 00:09:04 -0800552 * mpu3050_hw_init - initialize hardware
553 * @sensor: the sensor
554 *
555 * Called during device probe; configures the sampling method.
556 */
557static int __devinit mpu3050_hw_init(struct mpu3050_sensor *sensor)
558{
559 struct i2c_client *client = sensor->client;
560 int ret;
561 u8 reg;
562
563 /* Reset */
564 ret = i2c_smbus_write_byte_data(client, MPU3050_PWR_MGM,
565 MPU3050_PWR_MGM_RESET);
566 if (ret < 0)
567 return ret;
568
569 ret = i2c_smbus_read_byte_data(client, MPU3050_PWR_MGM);
570 if (ret < 0)
571 return ret;
572
573 ret &= ~MPU3050_PWR_MGM_CLKSEL;
574 ret |= MPU3050_PWR_MGM_PLL_Z;
575 ret = i2c_smbus_write_byte_data(client, MPU3050_PWR_MGM, ret);
576 if (ret < 0)
577 return ret;
578
579 /* Output frequency divider. The poll interval */
580 ret = i2c_smbus_write_byte_data(client, MPU3050_SMPLRT_DIV,
Wentao Xuc7769c02012-08-03 15:06:41 -0400581 sensor->poll_interval - 1);
Heikki Krogeruscd314fa2011-12-24 00:09:04 -0800582 if (ret < 0)
583 return ret;
584
585 /* Set low pass filter and full scale */
Wentao Xu04f4cc92012-08-15 19:41:43 -0400586 reg = MPU3050_DLPF_CFG_42HZ;
587 reg |= MPU3050_DEFAULT_FS_RANGE << 3;
Heikki Krogeruscd314fa2011-12-24 00:09:04 -0800588 reg |= MPU3050_EXT_SYNC_NONE << 5;
589 ret = i2c_smbus_write_byte_data(client, MPU3050_DLPF_FS_SYNC, reg);
590 if (ret < 0)
591 return ret;
592
593 return 0;
594}
Richard Liua1d406a2013-04-14 13:46:48 -0700595#ifdef CONFIG_OF
596static int mpu3050_parse_dt(struct device *dev,
597 struct mpu3050_gyro_platform_data *pdata)
598{
599 int rc = 0;
600
601 rc = of_property_read_u32(dev->of_node, "invn,poll-interval",
602 &pdata->poll_interval);
603 if (rc) {
604 dev_err(dev, "Failed to read poll-interval\n");
605 return rc;
606 }
607
608 /* check gpio_int later, if it is invalid, just use poll */
609 pdata->gpio_int = of_get_named_gpio_flags(dev->of_node,
610 "invn,gpio-int", 0, NULL);
611
612 pdata->gpio_en = of_get_named_gpio_flags(dev->of_node,
613 "invn,gpio-en", 0, NULL);
614 if (!gpio_is_valid(pdata->gpio_en))
615 return -EINVAL;
616
617 return 0;
618}
619#else
620static int mpu3050_parse_dt(struct device *dev,
621 struct mpu3050_gyro_platform_data *pdata)
622{
623 return -EINVAL;
624}
625#endif
Heikki Krogeruscd314fa2011-12-24 00:09:04 -0800626
627/**
Joseph Lai631b16e2011-06-27 13:26:53 -0700628 * mpu3050_probe - device detection callback
629 * @client: i2c client of found device
630 * @id: id match information
631 *
632 * The I2C layer calls us when it believes a sensor is present at this
633 * address. Probe to see if this is correct and to validate the device.
634 *
635 * If present install the relevant sysfs interfaces and input device.
636 */
637static int __devinit mpu3050_probe(struct i2c_client *client,
638 const struct i2c_device_id *id)
639{
640 struct mpu3050_sensor *sensor;
641 struct input_dev *idev;
Richard Liua1d406a2013-04-14 13:46:48 -0700642 struct mpu3050_gyro_platform_data *pdata;
Joseph Lai631b16e2011-06-27 13:26:53 -0700643 int ret;
644 int error;
Richard Liua1d406a2013-04-14 13:46:48 -0700645 u32 i;
Joseph Lai631b16e2011-06-27 13:26:53 -0700646
647 sensor = kzalloc(sizeof(struct mpu3050_sensor), GFP_KERNEL);
648 idev = input_allocate_device();
649 if (!sensor || !idev) {
650 dev_err(&client->dev, "failed to allocate driver data\n");
651 error = -ENOMEM;
652 goto err_free_mem;
653 }
654
655 sensor->client = client;
656 sensor->dev = &client->dev;
657 sensor->idev = idev;
Wentao Xuc7769c02012-08-03 15:06:41 -0400658 i2c_set_clientdata(client, sensor);
Richard Liua1d406a2013-04-14 13:46:48 -0700659
660 if (client->dev.of_node) {
661 pdata = devm_kzalloc(&client->dev,
662 sizeof(struct mpu3050_gyro_platform_data), GFP_KERNEL);
663 if (!pdata) {
664 dev_err(&client->dev, "Failed to allcated memory\n");
665 error = -ENOMEM;
666 goto err_free_mem;
667 }
668 ret = mpu3050_parse_dt(&client->dev, pdata);
669 if (ret) {
670 dev_err(&client->dev, "Failed to parse device tree\n");
671 error = ret;
672 goto err_free_mem;
673 }
674 } else
675 pdata = client->dev.platform_data;
676 sensor->platform_data = pdata;
677
Wentao Xuc7769c02012-08-03 15:06:41 -0400678 if (sensor->platform_data) {
679 u32 interval = sensor->platform_data->poll_interval;
Richard Liua1d406a2013-04-14 13:46:48 -0700680 sensor->enable_gpio = sensor->platform_data->gpio_en;
Wentao Xuc7769c02012-08-03 15:06:41 -0400681
682 if ((interval < MPU3050_MIN_POLL_INTERVAL) ||
683 (interval > MPU3050_MAX_POLL_INTERVAL))
684 sensor->poll_interval = MPU3050_DEFAULT_POLL_INTERVAL;
685 else
686 sensor->poll_interval = interval;
687 } else {
688 sensor->poll_interval = MPU3050_DEFAULT_POLL_INTERVAL;
Richard Liua1d406a2013-04-14 13:46:48 -0700689 sensor->enable_gpio = -EINVAL;
690 }
691
692 if (gpio_is_valid(sensor->enable_gpio)) {
693 ret = gpio_request(sensor->enable_gpio, "GYRO_EN_PM");
694 gpio_direction_output(sensor->enable_gpio, 1);
Wentao Xuc7769c02012-08-03 15:06:41 -0400695 }
Joseph Lai631b16e2011-06-27 13:26:53 -0700696
697 mpu3050_set_power_mode(client, 1);
698 msleep(10);
699
700 ret = i2c_smbus_read_byte_data(client, MPU3050_CHIP_ID_REG);
701 if (ret < 0) {
702 dev_err(&client->dev, "failed to detect device\n");
703 error = -ENXIO;
704 goto err_free_mem;
705 }
706
Richard Liua1d406a2013-04-14 13:46:48 -0700707 for (i = 0; i < ARRAY_SIZE(mpu3050_chip_ids); i++)
708 if (ret == mpu3050_chip_ids[i])
709 break;
710
711 if (i == ARRAY_SIZE(mpu3050_chip_ids)) {
Joseph Lai631b16e2011-06-27 13:26:53 -0700712 dev_err(&client->dev, "unsupported chip id\n");
713 error = -ENXIO;
714 goto err_free_mem;
715 }
716
717 idev->name = "MPU3050";
718 idev->id.bustype = BUS_I2C;
719 idev->dev.parent = &client->dev;
720
721 idev->open = mpu3050_input_open;
722 idev->close = mpu3050_input_close;
723
Richard Liuce2264d2013-04-19 13:35:46 -0700724 input_set_capability(idev, EV_ABS, ABS_MISC);
Joseph Lai631b16e2011-06-27 13:26:53 -0700725 input_set_abs_params(idev, ABS_X,
726 MPU3050_MIN_VALUE, MPU3050_MAX_VALUE, 0, 0);
727 input_set_abs_params(idev, ABS_Y,
728 MPU3050_MIN_VALUE, MPU3050_MAX_VALUE, 0, 0);
729 input_set_abs_params(idev, ABS_Z,
730 MPU3050_MIN_VALUE, MPU3050_MAX_VALUE, 0, 0);
731
732 input_set_drvdata(idev, sensor);
733
734 pm_runtime_set_active(&client->dev);
735
Heikki Krogeruscd314fa2011-12-24 00:09:04 -0800736 error = mpu3050_hw_init(sensor);
737 if (error)
738 goto err_pm_set_suspended;
739
Wentao Xudac9e602012-06-12 11:52:34 -0400740 if (client->irq == 0) {
Wentao Xudac9e602012-06-12 11:52:34 -0400741 sensor->use_poll = 1;
Wentao Xuc7769c02012-08-03 15:06:41 -0400742 INIT_DELAYED_WORK(&sensor->input_work, mpu3050_input_work_fn);
Wentao Xudac9e602012-06-12 11:52:34 -0400743 } else {
744 sensor->use_poll = 0;
745
Wentao Xuc7769c02012-08-03 15:06:41 -0400746 if (gpio_is_valid(sensor->platform_data->gpio_int)) {
747 /* configure interrupt gpio */
748 ret = gpio_request(sensor->platform_data->gpio_int,
749 "gyro_gpio_int");
750 if (ret) {
751 pr_err("%s: unable to request interrupt gpio %d\n",
752 __func__,
753 sensor->platform_data->gpio_int);
754 goto err_pm_set_suspended;
755 }
756
757 ret = gpio_direction_input(
758 sensor->platform_data->gpio_int);
759 if (ret) {
760 pr_err("%s: unable to set direction for gpio %d\n",
761 __func__, sensor->platform_data->gpio_int);
762 goto err_free_gpio;
763 }
Richard Liua1d406a2013-04-14 13:46:48 -0700764 client->irq = gpio_to_irq(
765 sensor->platform_data->gpio_int);
766 } else {
767 ret = -EINVAL;
768 goto err_pm_set_suspended;
Wentao Xuc7769c02012-08-03 15:06:41 -0400769 }
770
Wentao Xudac9e602012-06-12 11:52:34 -0400771 error = request_threaded_irq(client->irq,
Joseph Lai631b16e2011-06-27 13:26:53 -0700772 NULL, mpu3050_interrupt_thread,
Wentao Xuc7769c02012-08-03 15:06:41 -0400773 IRQF_TRIGGER_FALLING,
Heikki Krogerus3b518722011-12-23 23:57:09 -0800774 "mpu3050", sensor);
Wentao Xudac9e602012-06-12 11:52:34 -0400775 if (error) {
776 dev_err(&client->dev,
777 "can't get IRQ %d, error %d\n",
778 client->irq, error);
779 goto err_pm_set_suspended;
780 }
Joseph Lai631b16e2011-06-27 13:26:53 -0700781 }
782
783 error = input_register_device(idev);
784 if (error) {
785 dev_err(&client->dev, "failed to register input device\n");
786 goto err_free_irq;
787 }
788
Richard Liua1d406a2013-04-14 13:46:48 -0700789 error = create_sysfs_interfaces(&idev->dev);
Wentao Xuc7769c02012-08-03 15:06:41 -0400790 if (error < 0) {
791 dev_err(&client->dev, "failed to create sysfs\n");
792 goto err_input_cleanup;
793 }
794
Richard Liua1d406a2013-04-14 13:46:48 -0700795 if (sensor->use_poll)
796 schedule_delayed_work(&sensor->input_work,
797 msecs_to_jiffies(sensor->poll_interval));
798 else
799 i2c_smbus_write_byte_data(sensor->client, MPU3050_INT_CFG,
800 MPU3050_ACTIVE_LOW |
801 MPU3050_OPEN_DRAIN |
802 MPU3050_RAW_RDY_EN);
Joseph Lai631b16e2011-06-27 13:26:53 -0700803
804 return 0;
805
Wentao Xuc7769c02012-08-03 15:06:41 -0400806err_input_cleanup:
807 input_unregister_device(idev);
Joseph Lai631b16e2011-06-27 13:26:53 -0700808err_free_irq:
Wentao Xudac9e602012-06-12 11:52:34 -0400809 if (client->irq > 0)
810 free_irq(client->irq, sensor);
Wentao Xuc7769c02012-08-03 15:06:41 -0400811err_free_gpio:
812 if ((client->irq > 0) &&
813 (gpio_is_valid(sensor->platform_data->gpio_int)))
814 gpio_free(sensor->platform_data->gpio_int);
Joseph Lai631b16e2011-06-27 13:26:53 -0700815err_pm_set_suspended:
816 pm_runtime_set_suspended(&client->dev);
817err_free_mem:
Axel Lind9b830f2011-08-11 09:19:29 -0700818 input_free_device(idev);
Joseph Lai631b16e2011-06-27 13:26:53 -0700819 kfree(sensor);
820 return error;
821}
822
823/**
824 * mpu3050_remove - remove a sensor
825 * @client: i2c client of sensor being removed
826 *
827 * Our sensor is going away, clean up the resources.
828 */
829static int __devexit mpu3050_remove(struct i2c_client *client)
830{
831 struct mpu3050_sensor *sensor = i2c_get_clientdata(client);
832
833 pm_runtime_disable(&client->dev);
834 pm_runtime_set_suspended(&client->dev);
835
Wentao Xuc7769c02012-08-03 15:06:41 -0400836 if (client->irq)
837 free_irq(client->irq, sensor);
838
839 remove_sysfs_interfaces(&client->dev);
Richard Liua1d406a2013-04-14 13:46:48 -0700840 if (gpio_is_valid(sensor->enable_gpio))
841 gpio_free(sensor->enable_gpio);
Joseph Lai631b16e2011-06-27 13:26:53 -0700842 input_unregister_device(sensor->idev);
Wentao Xuc7769c02012-08-03 15:06:41 -0400843
Joseph Lai631b16e2011-06-27 13:26:53 -0700844 kfree(sensor);
845
846 return 0;
847}
848
849#ifdef CONFIG_PM
850/**
851 * mpu3050_suspend - called on device suspend
852 * @dev: device being suspended
853 *
854 * Put the device into sleep mode before we suspend the machine.
855 */
856static int mpu3050_suspend(struct device *dev)
857{
858 struct i2c_client *client = to_i2c_client(dev);
859
860 mpu3050_set_power_mode(client, 0);
861
862 return 0;
863}
864
865/**
866 * mpu3050_resume - called on device resume
867 * @dev: device being resumed
868 *
869 * Put the device into powered mode on resume.
870 */
871static int mpu3050_resume(struct device *dev)
872{
873 struct i2c_client *client = to_i2c_client(dev);
874
875 mpu3050_set_power_mode(client, 1);
876 msleep(100); /* wait for gyro chip resume */
877
878 return 0;
879}
880#endif
881
882static UNIVERSAL_DEV_PM_OPS(mpu3050_pm, mpu3050_suspend, mpu3050_resume, NULL);
883
884static const struct i2c_device_id mpu3050_ids[] = {
885 { "mpu3050", 0 },
886 { }
887};
888MODULE_DEVICE_TABLE(i2c, mpu3050_ids);
889
Olof Johanssone9489812011-12-23 01:20:44 -0800890static const struct of_device_id mpu3050_of_match[] = {
891 { .compatible = "invn,mpu3050", },
892 { },
893};
894MODULE_DEVICE_TABLE(of, mpu3050_of_match);
895
Joseph Lai631b16e2011-06-27 13:26:53 -0700896static struct i2c_driver mpu3050_i2c_driver = {
897 .driver = {
898 .name = "mpu3050",
899 .owner = THIS_MODULE,
900 .pm = &mpu3050_pm,
Olof Johanssone9489812011-12-23 01:20:44 -0800901 .of_match_table = mpu3050_of_match,
Joseph Lai631b16e2011-06-27 13:26:53 -0700902 },
903 .probe = mpu3050_probe,
904 .remove = __devexit_p(mpu3050_remove),
905 .id_table = mpu3050_ids,
906};
907
Axel Lin1b92c1c2012-03-16 23:05:41 -0700908module_i2c_driver(mpu3050_i2c_driver);
Joseph Lai631b16e2011-06-27 13:26:53 -0700909
910MODULE_AUTHOR("Wistron Corp.");
911MODULE_DESCRIPTION("MPU3050 Tri-axis gyroscope driver");
912MODULE_LICENSE("GPL");