blob: 5d6a19a3f8baa55cba62ce37c6662f9bab7eeb38 [file] [log] [blame]
Joseph Lai631b16e2011-06-27 13:26:53 -07001/*
2 * MPU3050 Tri-axis gyroscope driver
3 *
4 * Copyright (C) 2011 Wistron Co.Ltd
5 * Joseph Lai <joseph_lai@wistron.com>
6 *
7 * Trimmed down by Alan Cox <alan@linux.intel.com> to produce this version
8 *
9 * This is a 'lite' version of the driver, while we consider the right way
10 * to present the other features to user space. In particular it requires the
11 * device has an IRQ, and it only provides an input interface, so is not much
12 * use for device orientation. A fuller version is available from the Meego
13 * tree.
14 *
15 * This program is based on bma023.c.
16 *
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; version 2 of the License.
20 *
21 * This program is distributed in the hope that it will be useful, but
22 * WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 * General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License along
27 * with this program; if not, write to the Free Software Foundation, Inc.,
28 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
29 *
30 */
31
32#include <linux/module.h>
33#include <linux/init.h>
34#include <linux/interrupt.h>
35#include <linux/platform_device.h>
36#include <linux/mutex.h>
37#include <linux/err.h>
38#include <linux/i2c.h>
39#include <linux/input.h>
40#include <linux/delay.h>
41#include <linux/slab.h>
42#include <linux/pm_runtime.h>
Wentao Xuc7769c02012-08-03 15:06:41 -040043#include <linux/gpio.h>
Wentao Xudac9e602012-06-12 11:52:34 -040044#include <linux/input/mpu3050.h>
45#include <linux/regulator/consumer.h>
Joseph Lai631b16e2011-06-27 13:26:53 -070046
Joseph Lai631b16e2011-06-27 13:26:53 -070047#define MPU3050_CHIP_ID 0x69
Joseph Lai631b16e2011-06-27 13:26:53 -070048
49#define MPU3050_AUTO_DELAY 1000
50
51#define MPU3050_MIN_VALUE -32768
52#define MPU3050_MAX_VALUE 32767
53
Wentao Xuc7769c02012-08-03 15:06:41 -040054#define MPU3050_MIN_POLL_INTERVAL 1
55#define MPU3050_MAX_POLL_INTERVAL 250
Heikki Krogeruscd314fa2011-12-24 00:09:04 -080056#define MPU3050_DEFAULT_POLL_INTERVAL 200
57#define MPU3050_DEFAULT_FS_RANGE 3
58
59/* Register map */
60#define MPU3050_CHIP_ID_REG 0x00
61#define MPU3050_SMPLRT_DIV 0x15
62#define MPU3050_DLPF_FS_SYNC 0x16
63#define MPU3050_INT_CFG 0x17
64#define MPU3050_XOUT_H 0x1D
65#define MPU3050_PWR_MGM 0x3E
66#define MPU3050_PWR_MGM_POS 6
67
68/* Register bits */
69
70/* DLPF_FS_SYNC */
71#define MPU3050_EXT_SYNC_NONE 0x00
72#define MPU3050_EXT_SYNC_TEMP 0x20
73#define MPU3050_EXT_SYNC_GYROX 0x40
74#define MPU3050_EXT_SYNC_GYROY 0x60
75#define MPU3050_EXT_SYNC_GYROZ 0x80
76#define MPU3050_EXT_SYNC_ACCELX 0xA0
77#define MPU3050_EXT_SYNC_ACCELY 0xC0
78#define MPU3050_EXT_SYNC_ACCELZ 0xE0
79#define MPU3050_EXT_SYNC_MASK 0xE0
80#define MPU3050_FS_250DPS 0x00
81#define MPU3050_FS_500DPS 0x08
82#define MPU3050_FS_1000DPS 0x10
83#define MPU3050_FS_2000DPS 0x18
84#define MPU3050_FS_MASK 0x18
85#define MPU3050_DLPF_CFG_256HZ_NOLPF2 0x00
86#define MPU3050_DLPF_CFG_188HZ 0x01
87#define MPU3050_DLPF_CFG_98HZ 0x02
88#define MPU3050_DLPF_CFG_42HZ 0x03
89#define MPU3050_DLPF_CFG_20HZ 0x04
90#define MPU3050_DLPF_CFG_10HZ 0x05
91#define MPU3050_DLPF_CFG_5HZ 0x06
92#define MPU3050_DLPF_CFG_2100HZ_NOLPF 0x07
93#define MPU3050_DLPF_CFG_MASK 0x07
94/* INT_CFG */
95#define MPU3050_RAW_RDY_EN 0x01
Wentao Xuc7769c02012-08-03 15:06:41 -040096#define MPU3050_MPU_RDY_EN 0x04
97#define MPU3050_LATCH_INT_EN 0x20
98#define MPU3050_OPEN_DRAIN 0x40
99#define MPU3050_ACTIVE_LOW 0x80
Heikki Krogeruscd314fa2011-12-24 00:09:04 -0800100/* PWR_MGM */
101#define MPU3050_PWR_MGM_PLL_X 0x01
102#define MPU3050_PWR_MGM_PLL_Y 0x02
103#define MPU3050_PWR_MGM_PLL_Z 0x03
104#define MPU3050_PWR_MGM_CLKSEL 0x07
105#define MPU3050_PWR_MGM_STBY_ZG 0x08
106#define MPU3050_PWR_MGM_STBY_YG 0x10
107#define MPU3050_PWR_MGM_STBY_XG 0x20
108#define MPU3050_PWR_MGM_SLEEP 0x40
109#define MPU3050_PWR_MGM_RESET 0x80
110#define MPU3050_PWR_MGM_MASK 0x40
111
Joseph Lai631b16e2011-06-27 13:26:53 -0700112struct axis_data {
113 s16 x;
114 s16 y;
115 s16 z;
116};
117
118struct mpu3050_sensor {
119 struct i2c_client *client;
120 struct device *dev;
121 struct input_dev *idev;
Wentao Xudac9e602012-06-12 11:52:34 -0400122 struct mpu3050_gyro_platform_data *platform_data;
123 struct delayed_work input_work;
124 u32 use_poll;
Wentao Xuc7769c02012-08-03 15:06:41 -0400125 u32 poll_interval;
Wentao Xuee6f6492012-08-15 19:41:43 -0400126 u32 dlpf_index;
Wentao Xua7b33f12013-01-18 18:42:59 -0500127 atomic_t enabled;
Joseph Lai631b16e2011-06-27 13:26:53 -0700128};
129
Wentao Xudac9e602012-06-12 11:52:34 -0400130struct sensor_regulator {
131 struct regulator *vreg;
132 const char *name;
133 u32 min_uV;
134 u32 max_uV;
135};
136
137struct sensor_regulator mpu_vreg[] = {
138 {NULL, "vdd", 2100000, 3600000},
139 {NULL, "vlogic", 1800000, 1800000},
140};
141
Wentao Xuee6f6492012-08-15 19:41:43 -0400142struct dlpf_cfg_tb {
143 u8 cfg; /* cfg index */
144 u32 lpf_bw; /* low pass filter bandwidth in Hz */
145 u32 sample_rate; /* analog sample rate in Khz, 1 or 8 */
146};
147
148static struct dlpf_cfg_tb dlpf_table[] = {
149 {6, 5, 1},
150 {5, 10, 1},
151 {4, 20, 1},
152 {3, 42, 1},
153 {2, 98, 1},
154 {1, 188, 1},
155 {0, 256, 8},
156};
157
Wentao Xua7b33f12013-01-18 18:42:59 -0500158static void mpu3050_set_power_mode(struct i2c_client *client, u8 val);
159static int mpu3050_start(struct mpu3050_sensor *sensor);
160static void mpu3050_stop(struct mpu3050_sensor *sensor);
161
Wentao Xuee6f6492012-08-15 19:41:43 -0400162static u8 interval_to_dlpf_cfg(u32 interval)
163{
164 u32 sample_rate = 1000 / interval;
165 u32 i;
166
167 /* the filter bandwidth needs to be greater or
168 * equal to half of the sample rate
169 */
170 for (i = 0; i < sizeof(dlpf_table)/sizeof(dlpf_table[0]); i++) {
171 if (dlpf_table[i].lpf_bw * 2 >= sample_rate)
172 return i;
173 }
174
175 /* return the maximum possible */
176 return --i;
177}
178
Wentao Xudac9e602012-06-12 11:52:34 -0400179static int mpu3050_config_regulator(struct i2c_client *client, bool on)
180{
181 int rc = 0, i;
182 int num_reg = sizeof(mpu_vreg) / sizeof(struct sensor_regulator);
183
184 if (on) {
185 for (i = 0; i < num_reg; i++) {
186 mpu_vreg[i].vreg = regulator_get(&client->dev,
187 mpu_vreg[i].name);
188 if (IS_ERR(mpu_vreg[i].vreg)) {
189 rc = PTR_ERR(mpu_vreg[i].vreg);
190 pr_err("%s:regulator get failed rc=%d\n",
191 __func__, rc);
Wentao Xu7a69d3b2012-12-12 11:28:21 -0500192 mpu_vreg[i].vreg = NULL;
Wentao Xudac9e602012-06-12 11:52:34 -0400193 goto error_vdd;
194 }
195
196 if (regulator_count_voltages(mpu_vreg[i].vreg) > 0) {
197 rc = regulator_set_voltage(mpu_vreg[i].vreg,
198 mpu_vreg[i].min_uV, mpu_vreg[i].max_uV);
199 if (rc) {
200 pr_err("%s:set_voltage failed rc=%d\n",
201 __func__, rc);
202 regulator_put(mpu_vreg[i].vreg);
Wentao Xu7a69d3b2012-12-12 11:28:21 -0500203 mpu_vreg[i].vreg = NULL;
Wentao Xudac9e602012-06-12 11:52:34 -0400204 goto error_vdd;
205 }
206 }
207
208 rc = regulator_enable(mpu_vreg[i].vreg);
209 if (rc) {
210 pr_err("%s: regulator_enable failed rc =%d\n",
211 __func__,
212 rc);
213
214 if (regulator_count_voltages(
215 mpu_vreg[i].vreg) > 0) {
216 regulator_set_voltage(mpu_vreg[i].vreg,
217 0, mpu_vreg[i].max_uV);
218 }
219 regulator_put(mpu_vreg[i].vreg);
Wentao Xu7a69d3b2012-12-12 11:28:21 -0500220 mpu_vreg[i].vreg = NULL;
Wentao Xudac9e602012-06-12 11:52:34 -0400221 goto error_vdd;
222 }
223 }
224 return rc;
225 } else {
226 i = num_reg;
227 }
228error_vdd:
229 while (--i >= 0) {
Wentao Xu7a69d3b2012-12-12 11:28:21 -0500230 if (!IS_ERR_OR_NULL(mpu_vreg[i].vreg)) {
231 if (regulator_count_voltages(
232 mpu_vreg[i].vreg) > 0) {
233 regulator_set_voltage(mpu_vreg[i].vreg, 0,
Wentao Xudac9e602012-06-12 11:52:34 -0400234 mpu_vreg[i].max_uV);
Wentao Xu7a69d3b2012-12-12 11:28:21 -0500235 }
236 regulator_disable(mpu_vreg[i].vreg);
237 regulator_put(mpu_vreg[i].vreg);
238 mpu_vreg[i].vreg = NULL;
Wentao Xudac9e602012-06-12 11:52:34 -0400239 }
Wentao Xudac9e602012-06-12 11:52:34 -0400240 }
241 return rc;
242}
243
Joseph Lai631b16e2011-06-27 13:26:53 -0700244/**
Wentao Xuc7769c02012-08-03 15:06:41 -0400245 * mpu3050_attr_get_polling_rate - get the sampling rate
246 */
247static ssize_t mpu3050_attr_get_polling_rate(struct device *dev,
248 struct device_attribute *attr,
249 char *buf)
250{
251 int val;
252 struct mpu3050_sensor *sensor = dev_get_drvdata(dev);
253 val = sensor ? sensor->poll_interval : 0;
254 return snprintf(buf, 8, "%d\n", val);
255}
256
257/**
258 * mpu3050_attr_set_polling_rate - set the sampling rate
259 */
260static ssize_t mpu3050_attr_set_polling_rate(struct device *dev,
261 struct device_attribute *attr,
262 const char *buf, size_t size)
263{
264 struct mpu3050_sensor *sensor = dev_get_drvdata(dev);
265 unsigned long interval_ms;
Wentao Xuee6f6492012-08-15 19:41:43 -0400266 unsigned int dlpf_index;
267 u8 divider, reg;
268 int ret;
Wentao Xuc7769c02012-08-03 15:06:41 -0400269
270 if (kstrtoul(buf, 10, &interval_ms))
271 return -EINVAL;
272 if ((interval_ms < MPU3050_MIN_POLL_INTERVAL) ||
273 (interval_ms > MPU3050_MAX_POLL_INTERVAL))
274 return -EINVAL;
275
Wentao Xuee6f6492012-08-15 19:41:43 -0400276 dlpf_index = interval_to_dlpf_cfg(interval_ms);
277 divider = interval_ms * dlpf_table[dlpf_index].sample_rate - 1;
Wentao Xuc7769c02012-08-03 15:06:41 -0400278
Wentao Xuee6f6492012-08-15 19:41:43 -0400279 if (sensor->dlpf_index != dlpf_index) {
280 /* Set low pass filter and full scale */
281 reg = dlpf_table[dlpf_index].cfg;
282 reg |= MPU3050_DEFAULT_FS_RANGE << 3;
283 reg |= MPU3050_EXT_SYNC_NONE << 5;
284 ret = i2c_smbus_write_byte_data(sensor->client,
285 MPU3050_DLPF_FS_SYNC, reg);
286 if (ret == 0)
287 sensor->dlpf_index = dlpf_index;
288 }
289
290 if (sensor->poll_interval != interval_ms) {
291 /* Output frequency divider. The poll interval */
292 ret = i2c_smbus_write_byte_data(sensor->client,
293 MPU3050_SMPLRT_DIV, divider);
294 if (ret == 0)
295 sensor->poll_interval = interval_ms;
296 }
Wentao Xuc7769c02012-08-03 15:06:41 -0400297
298 return size;
299}
300
Wentao Xua7b33f12013-01-18 18:42:59 -0500301
302static ssize_t mpu3050_attr_get_enable(struct device *dev,
303 struct device_attribute *attr, char *buf)
304{
305 struct mpu3050_sensor *sensor = dev_get_drvdata(dev);
306 int val = atomic_read(&sensor->enabled);
307 return snprintf(buf, sizeof(val) + 2, "%d\n", val);
308}
309
310
311static ssize_t mpu3050_attr_set_enable(struct device *dev,
312 struct device_attribute *attr,
313 const char *buf, size_t size)
314{
315 struct mpu3050_sensor *sensor = dev_get_drvdata(dev);
316 unsigned long val;
317
318 if (kstrtoul(buf, 10, &val))
319 return -EINVAL;
320
321 if (val)
322 mpu3050_start(sensor);
323 else
324 mpu3050_stop(sensor);
325
326 return size;
327}
328
Wentao Xuc7769c02012-08-03 15:06:41 -0400329static struct device_attribute attributes[] = {
330
Wentao Xu82e63052012-11-15 16:30:15 -0500331 __ATTR(pollrate_ms, 0664,
Wentao Xuc7769c02012-08-03 15:06:41 -0400332 mpu3050_attr_get_polling_rate,
333 mpu3050_attr_set_polling_rate),
Wentao Xua7b33f12013-01-18 18:42:59 -0500334 __ATTR(enable, 0664,
335 mpu3050_attr_get_enable,
336 mpu3050_attr_set_enable),
Wentao Xuc7769c02012-08-03 15:06:41 -0400337};
338
Wentao Xua7b33f12013-01-18 18:42:59 -0500339
Wentao Xuc7769c02012-08-03 15:06:41 -0400340static int create_sysfs_interfaces(struct device *dev)
341{
342 int i;
343 int err;
344 for (i = 0; i < ARRAY_SIZE(attributes); i++) {
345 err = device_create_file(dev, attributes + i);
346 if (err)
347 goto error;
348 }
349 return 0;
350
351error:
352 for ( ; i >= 0; i--)
353 device_remove_file(dev, attributes + i);
354 dev_err(dev, "%s:Unable to create interface\n", __func__);
355 return err;
356}
357
358static int remove_sysfs_interfaces(struct device *dev)
359{
360 int i;
361 for (i = 0; i < ARRAY_SIZE(attributes); i++)
362 device_remove_file(dev, attributes + i);
363 return 0;
364}
365
366/**
Joseph Lai631b16e2011-06-27 13:26:53 -0700367 * mpu3050_xyz_read_reg - read the axes values
368 * @buffer: provide register addr and get register
369 * @length: length of register
370 *
371 * Reads the register values in one transaction or returns a negative
372 * error code on failure.
373 */
374static int mpu3050_xyz_read_reg(struct i2c_client *client,
375 u8 *buffer, int length)
376{
377 /*
378 * Annoying we can't make this const because the i2c layer doesn't
379 * declare input buffers const.
380 */
381 char cmd = MPU3050_XOUT_H;
382 struct i2c_msg msg[] = {
383 {
384 .addr = client->addr,
385 .flags = 0,
386 .len = 1,
387 .buf = &cmd,
388 },
389 {
390 .addr = client->addr,
391 .flags = I2C_M_RD,
392 .len = length,
393 .buf = buffer,
394 },
395 };
396
397 return i2c_transfer(client->adapter, msg, 2);
398}
399
400/**
401 * mpu3050_read_xyz - get co-ordinates from device
402 * @client: i2c address of sensor
403 * @coords: co-ordinates to update
404 *
405 * Return the converted X Y and Z co-ordinates from the sensor device
406 */
407static void mpu3050_read_xyz(struct i2c_client *client,
408 struct axis_data *coords)
409{
410 u16 buffer[3];
411
412 mpu3050_xyz_read_reg(client, (u8 *)buffer, 6);
413 coords->x = be16_to_cpu(buffer[0]);
414 coords->y = be16_to_cpu(buffer[1]);
415 coords->z = be16_to_cpu(buffer[2]);
416 dev_dbg(&client->dev, "%s: x %d, y %d, z %d\n", __func__,
417 coords->x, coords->y, coords->z);
418}
419
420/**
421 * mpu3050_set_power_mode - set the power mode
422 * @client: i2c client for the sensor
423 * @val: value to switch on/off of power, 1: normal power, 0: low power
424 *
425 * Put device to normal-power mode or low-power mode.
426 */
427static void mpu3050_set_power_mode(struct i2c_client *client, u8 val)
428{
429 u8 value;
430
Wentao Xudac9e602012-06-12 11:52:34 -0400431 if (val) {
432 mpu3050_config_regulator(client, 1);
433 udelay(10);
434 }
435
Joseph Lai631b16e2011-06-27 13:26:53 -0700436 value = i2c_smbus_read_byte_data(client, MPU3050_PWR_MGM);
437 value = (value & ~MPU3050_PWR_MGM_MASK) |
438 (((val << MPU3050_PWR_MGM_POS) & MPU3050_PWR_MGM_MASK) ^
439 MPU3050_PWR_MGM_MASK);
440 i2c_smbus_write_byte_data(client, MPU3050_PWR_MGM, value);
Wentao Xudac9e602012-06-12 11:52:34 -0400441
442 if (!val) {
443 udelay(10);
444 mpu3050_config_regulator(client, 0);
445 }
Joseph Lai631b16e2011-06-27 13:26:53 -0700446}
447
448/**
Wentao Xua7b33f12013-01-18 18:42:59 -0500449 * mpu3050_start - called when sensor is enabled via sysfs
450 * @sensor: the sensor
Joseph Lai631b16e2011-06-27 13:26:53 -0700451 *
Wentao Xua7b33f12013-01-18 18:42:59 -0500452 * The function gets called when the sensor is enabled via sysfs.
453 * Interrupts will be enabled and the device will be ready to provide data.
454 *
Joseph Lai631b16e2011-06-27 13:26:53 -0700455 */
Wentao Xua7b33f12013-01-18 18:42:59 -0500456static int mpu3050_start(struct mpu3050_sensor *sensor)
Joseph Lai631b16e2011-06-27 13:26:53 -0700457{
Heikki Krogerus3b518722011-12-23 23:57:09 -0800458 int error;
Joseph Lai631b16e2011-06-27 13:26:53 -0700459
Wentao Xuc7769c02012-08-03 15:06:41 -0400460 pm_runtime_get_sync(sensor->dev);
Joseph Lai631b16e2011-06-27 13:26:53 -0700461
Heikki Krogerus3b518722011-12-23 23:57:09 -0800462 /* Enable interrupts */
463 error = i2c_smbus_write_byte_data(sensor->client, MPU3050_INT_CFG,
Wentao Xuc7769c02012-08-03 15:06:41 -0400464 MPU3050_ACTIVE_LOW |
465 MPU3050_OPEN_DRAIN |
466 MPU3050_RAW_RDY_EN);
Heikki Krogerus3b518722011-12-23 23:57:09 -0800467 if (error < 0) {
468 pm_runtime_put(sensor->dev);
469 return error;
470 }
Wentao Xudac9e602012-06-12 11:52:34 -0400471 if (sensor->use_poll)
472 schedule_delayed_work(&sensor->input_work,
Wentao Xuc7769c02012-08-03 15:06:41 -0400473 msecs_to_jiffies(sensor->poll_interval));
Heikki Krogerus3b518722011-12-23 23:57:09 -0800474
Joseph Lai631b16e2011-06-27 13:26:53 -0700475 return 0;
476}
477
478/**
Wentao Xua7b33f12013-01-18 18:42:59 -0500479 * mpu3050_stop - called when sensor is disabled via sysfs
480 * @sensor: the sensor
Joseph Lai631b16e2011-06-27 13:26:53 -0700481 *
Wentao Xua7b33f12013-01-18 18:42:59 -0500482 * The function gets called when the sensor is disabled via sysfs.
483 * Device will be pushed to suspend mode.
484 *
Joseph Lai631b16e2011-06-27 13:26:53 -0700485 */
Wentao Xua7b33f12013-01-18 18:42:59 -0500486static void mpu3050_stop(struct mpu3050_sensor *sensor)
Joseph Lai631b16e2011-06-27 13:26:53 -0700487{
Wentao Xudac9e602012-06-12 11:52:34 -0400488 if (sensor->use_poll)
489 cancel_delayed_work_sync(&sensor->input_work);
490
Joseph Lai631b16e2011-06-27 13:26:53 -0700491 pm_runtime_put(sensor->dev);
492}
493
494/**
495 * mpu3050_interrupt_thread - handle an IRQ
496 * @irq: interrupt numner
497 * @data: the sensor
498 *
499 * Called by the kernel single threaded after an interrupt occurs. Read
500 * the sensor data and generate an input event for it.
501 */
502static irqreturn_t mpu3050_interrupt_thread(int irq, void *data)
503{
504 struct mpu3050_sensor *sensor = data;
505 struct axis_data axis;
506
507 mpu3050_read_xyz(sensor->client, &axis);
508
509 input_report_abs(sensor->idev, ABS_X, axis.x);
510 input_report_abs(sensor->idev, ABS_Y, axis.y);
511 input_report_abs(sensor->idev, ABS_Z, axis.z);
512 input_sync(sensor->idev);
513
514 return IRQ_HANDLED;
515}
516
517/**
Wentao Xudac9e602012-06-12 11:52:34 -0400518 * mpu3050_input_work_fn - polling work
519 * @work: the work struct
520 *
521 * Called by the work queue; read sensor data and generate an input
522 * event
523 */
524static void mpu3050_input_work_fn(struct work_struct *work)
525{
526 struct mpu3050_sensor *sensor;
527 struct axis_data axis;
528
529 sensor = container_of((struct delayed_work *)work,
530 struct mpu3050_sensor, input_work);
531
532 mpu3050_read_xyz(sensor->client, &axis);
533
534 input_report_abs(sensor->idev, ABS_X, axis.x);
535 input_report_abs(sensor->idev, ABS_Y, axis.y);
536 input_report_abs(sensor->idev, ABS_Z, axis.z);
537 input_sync(sensor->idev);
538
539 if (sensor->use_poll)
540 schedule_delayed_work(&sensor->input_work,
Wentao Xuc7769c02012-08-03 15:06:41 -0400541 msecs_to_jiffies(sensor->poll_interval));
Wentao Xudac9e602012-06-12 11:52:34 -0400542}
543
544/**
Heikki Krogeruscd314fa2011-12-24 00:09:04 -0800545 * mpu3050_hw_init - initialize hardware
546 * @sensor: the sensor
547 *
548 * Called during device probe; configures the sampling method.
549 */
550static int __devinit mpu3050_hw_init(struct mpu3050_sensor *sensor)
551{
552 struct i2c_client *client = sensor->client;
553 int ret;
554 u8 reg;
555
556 /* Reset */
557 ret = i2c_smbus_write_byte_data(client, MPU3050_PWR_MGM,
558 MPU3050_PWR_MGM_RESET);
559 if (ret < 0)
560 return ret;
561
562 ret = i2c_smbus_read_byte_data(client, MPU3050_PWR_MGM);
563 if (ret < 0)
564 return ret;
565
566 ret &= ~MPU3050_PWR_MGM_CLKSEL;
567 ret |= MPU3050_PWR_MGM_PLL_Z;
568 ret = i2c_smbus_write_byte_data(client, MPU3050_PWR_MGM, ret);
569 if (ret < 0)
570 return ret;
571
572 /* Output frequency divider. The poll interval */
573 ret = i2c_smbus_write_byte_data(client, MPU3050_SMPLRT_DIV,
Wentao Xuc7769c02012-08-03 15:06:41 -0400574 sensor->poll_interval - 1);
Heikki Krogeruscd314fa2011-12-24 00:09:04 -0800575 if (ret < 0)
576 return ret;
577
578 /* Set low pass filter and full scale */
Wentao Xuee6f6492012-08-15 19:41:43 -0400579 reg = MPU3050_DLPF_CFG_42HZ;
580 reg |= MPU3050_DEFAULT_FS_RANGE << 3;
Heikki Krogeruscd314fa2011-12-24 00:09:04 -0800581 reg |= MPU3050_EXT_SYNC_NONE << 5;
582 ret = i2c_smbus_write_byte_data(client, MPU3050_DLPF_FS_SYNC, reg);
583 if (ret < 0)
584 return ret;
585
586 return 0;
587}
588
589/**
Joseph Lai631b16e2011-06-27 13:26:53 -0700590 * mpu3050_probe - device detection callback
591 * @client: i2c client of found device
592 * @id: id match information
593 *
594 * The I2C layer calls us when it believes a sensor is present at this
595 * address. Probe to see if this is correct and to validate the device.
596 *
597 * If present install the relevant sysfs interfaces and input device.
598 */
599static int __devinit mpu3050_probe(struct i2c_client *client,
600 const struct i2c_device_id *id)
601{
602 struct mpu3050_sensor *sensor;
603 struct input_dev *idev;
604 int ret;
605 int error;
606
607 sensor = kzalloc(sizeof(struct mpu3050_sensor), GFP_KERNEL);
608 idev = input_allocate_device();
609 if (!sensor || !idev) {
610 dev_err(&client->dev, "failed to allocate driver data\n");
611 error = -ENOMEM;
612 goto err_free_mem;
613 }
614
615 sensor->client = client;
616 sensor->dev = &client->dev;
617 sensor->idev = idev;
Wentao Xudac9e602012-06-12 11:52:34 -0400618 sensor->platform_data = client->dev.platform_data;
Wentao Xuc7769c02012-08-03 15:06:41 -0400619 i2c_set_clientdata(client, sensor);
620 if (sensor->platform_data) {
621 u32 interval = sensor->platform_data->poll_interval;
622
623 if ((interval < MPU3050_MIN_POLL_INTERVAL) ||
624 (interval > MPU3050_MAX_POLL_INTERVAL))
625 sensor->poll_interval = MPU3050_DEFAULT_POLL_INTERVAL;
626 else
627 sensor->poll_interval = interval;
628 } else {
629 sensor->poll_interval = MPU3050_DEFAULT_POLL_INTERVAL;
630 }
Joseph Lai631b16e2011-06-27 13:26:53 -0700631
632 mpu3050_set_power_mode(client, 1);
Wentao Xua7b33f12013-01-18 18:42:59 -0500633 atomic_set(&sensor->enabled, 1);
Joseph Lai631b16e2011-06-27 13:26:53 -0700634 msleep(10);
635
636 ret = i2c_smbus_read_byte_data(client, MPU3050_CHIP_ID_REG);
637 if (ret < 0) {
638 dev_err(&client->dev, "failed to detect device\n");
639 error = -ENXIO;
640 goto err_free_mem;
641 }
642
643 if (ret != MPU3050_CHIP_ID) {
644 dev_err(&client->dev, "unsupported chip id\n");
645 error = -ENXIO;
646 goto err_free_mem;
647 }
648
649 idev->name = "MPU3050";
650 idev->id.bustype = BUS_I2C;
651 idev->dev.parent = &client->dev;
652
Joseph Lai631b16e2011-06-27 13:26:53 -0700653 __set_bit(EV_ABS, idev->evbit);
654 input_set_abs_params(idev, ABS_X,
655 MPU3050_MIN_VALUE, MPU3050_MAX_VALUE, 0, 0);
656 input_set_abs_params(idev, ABS_Y,
657 MPU3050_MIN_VALUE, MPU3050_MAX_VALUE, 0, 0);
658 input_set_abs_params(idev, ABS_Z,
659 MPU3050_MIN_VALUE, MPU3050_MAX_VALUE, 0, 0);
660
661 input_set_drvdata(idev, sensor);
662
663 pm_runtime_set_active(&client->dev);
664
Heikki Krogeruscd314fa2011-12-24 00:09:04 -0800665 error = mpu3050_hw_init(sensor);
666 if (error)
667 goto err_pm_set_suspended;
668
Wentao Xudac9e602012-06-12 11:52:34 -0400669 if (client->irq == 0) {
Wentao Xudac9e602012-06-12 11:52:34 -0400670 sensor->use_poll = 1;
Wentao Xuc7769c02012-08-03 15:06:41 -0400671 INIT_DELAYED_WORK(&sensor->input_work, mpu3050_input_work_fn);
Wentao Xudac9e602012-06-12 11:52:34 -0400672 } else {
673 sensor->use_poll = 0;
674
Wentao Xuc7769c02012-08-03 15:06:41 -0400675 if (gpio_is_valid(sensor->platform_data->gpio_int)) {
676 /* configure interrupt gpio */
677 ret = gpio_request(sensor->platform_data->gpio_int,
678 "gyro_gpio_int");
679 if (ret) {
680 pr_err("%s: unable to request interrupt gpio %d\n",
681 __func__,
682 sensor->platform_data->gpio_int);
683 goto err_pm_set_suspended;
684 }
685
686 ret = gpio_direction_input(
687 sensor->platform_data->gpio_int);
688 if (ret) {
689 pr_err("%s: unable to set direction for gpio %d\n",
690 __func__, sensor->platform_data->gpio_int);
691 goto err_free_gpio;
692 }
693 }
694
Wentao Xudac9e602012-06-12 11:52:34 -0400695 error = request_threaded_irq(client->irq,
Joseph Lai631b16e2011-06-27 13:26:53 -0700696 NULL, mpu3050_interrupt_thread,
Wentao Xuc7769c02012-08-03 15:06:41 -0400697 IRQF_TRIGGER_FALLING,
Heikki Krogerus3b518722011-12-23 23:57:09 -0800698 "mpu3050", sensor);
Wentao Xudac9e602012-06-12 11:52:34 -0400699 if (error) {
700 dev_err(&client->dev,
701 "can't get IRQ %d, error %d\n",
702 client->irq, error);
703 goto err_pm_set_suspended;
704 }
Joseph Lai631b16e2011-06-27 13:26:53 -0700705 }
706
707 error = input_register_device(idev);
708 if (error) {
709 dev_err(&client->dev, "failed to register input device\n");
710 goto err_free_irq;
711 }
712
Wentao Xuc7769c02012-08-03 15:06:41 -0400713 error = create_sysfs_interfaces(&client->dev);
714 if (error < 0) {
715 dev_err(&client->dev, "failed to create sysfs\n");
716 goto err_input_cleanup;
717 }
718
Joseph Lai631b16e2011-06-27 13:26:53 -0700719 pm_runtime_enable(&client->dev);
720 pm_runtime_set_autosuspend_delay(&client->dev, MPU3050_AUTO_DELAY);
721
722 return 0;
723
Wentao Xuc7769c02012-08-03 15:06:41 -0400724err_input_cleanup:
725 input_unregister_device(idev);
Joseph Lai631b16e2011-06-27 13:26:53 -0700726err_free_irq:
Wentao Xudac9e602012-06-12 11:52:34 -0400727 if (client->irq > 0)
728 free_irq(client->irq, sensor);
Wentao Xuc7769c02012-08-03 15:06:41 -0400729err_free_gpio:
730 if ((client->irq > 0) &&
731 (gpio_is_valid(sensor->platform_data->gpio_int)))
732 gpio_free(sensor->platform_data->gpio_int);
Joseph Lai631b16e2011-06-27 13:26:53 -0700733err_pm_set_suspended:
734 pm_runtime_set_suspended(&client->dev);
735err_free_mem:
Axel Lind9b830f2011-08-11 09:19:29 -0700736 input_free_device(idev);
Joseph Lai631b16e2011-06-27 13:26:53 -0700737 kfree(sensor);
738 return error;
739}
740
741/**
742 * mpu3050_remove - remove a sensor
743 * @client: i2c client of sensor being removed
744 *
745 * Our sensor is going away, clean up the resources.
746 */
747static int __devexit mpu3050_remove(struct i2c_client *client)
748{
749 struct mpu3050_sensor *sensor = i2c_get_clientdata(client);
750
751 pm_runtime_disable(&client->dev);
752 pm_runtime_set_suspended(&client->dev);
753
Wentao Xuc7769c02012-08-03 15:06:41 -0400754 if (client->irq)
755 free_irq(client->irq, sensor);
756
757 remove_sysfs_interfaces(&client->dev);
Joseph Lai631b16e2011-06-27 13:26:53 -0700758 input_unregister_device(sensor->idev);
Wentao Xuc7769c02012-08-03 15:06:41 -0400759
Joseph Lai631b16e2011-06-27 13:26:53 -0700760 kfree(sensor);
761
762 return 0;
763}
764
765#ifdef CONFIG_PM
766/**
767 * mpu3050_suspend - called on device suspend
768 * @dev: device being suspended
769 *
770 * Put the device into sleep mode before we suspend the machine.
771 */
772static int mpu3050_suspend(struct device *dev)
773{
774 struct i2c_client *client = to_i2c_client(dev);
Wentao Xua7b33f12013-01-18 18:42:59 -0500775 struct mpu3050_sensor *sensor = i2c_get_clientdata(client);
776 if (atomic_cmpxchg(&sensor->enabled, 1, 0)) {
777 mpu3050_set_power_mode (sensor->client, 0);
778 }
Joseph Lai631b16e2011-06-27 13:26:53 -0700779
780 return 0;
781}
782
783/**
784 * mpu3050_resume - called on device resume
785 * @dev: device being resumed
786 *
787 * Put the device into powered mode on resume.
788 */
789static int mpu3050_resume(struct device *dev)
790{
791 struct i2c_client *client = to_i2c_client(dev);
Wentao Xua7b33f12013-01-18 18:42:59 -0500792 struct mpu3050_sensor *sensor = i2c_get_clientdata(client);
793 if (!atomic_cmpxchg(&sensor->enabled, 0, 1)) {
794 mpu3050_set_power_mode (sensor->client, 1);
795 }
Joseph Lai631b16e2011-06-27 13:26:53 -0700796 msleep(100); /* wait for gyro chip resume */
797
798 return 0;
799}
800#endif
801
802static UNIVERSAL_DEV_PM_OPS(mpu3050_pm, mpu3050_suspend, mpu3050_resume, NULL);
803
804static const struct i2c_device_id mpu3050_ids[] = {
805 { "mpu3050", 0 },
806 { }
807};
808MODULE_DEVICE_TABLE(i2c, mpu3050_ids);
809
Olof Johanssone9489812011-12-23 01:20:44 -0800810static const struct of_device_id mpu3050_of_match[] = {
811 { .compatible = "invn,mpu3050", },
812 { },
813};
814MODULE_DEVICE_TABLE(of, mpu3050_of_match);
815
Joseph Lai631b16e2011-06-27 13:26:53 -0700816static struct i2c_driver mpu3050_i2c_driver = {
817 .driver = {
818 .name = "mpu3050",
819 .owner = THIS_MODULE,
820 .pm = &mpu3050_pm,
Olof Johanssone9489812011-12-23 01:20:44 -0800821 .of_match_table = mpu3050_of_match,
Joseph Lai631b16e2011-06-27 13:26:53 -0700822 },
823 .probe = mpu3050_probe,
824 .remove = __devexit_p(mpu3050_remove),
825 .id_table = mpu3050_ids,
826};
827
Axel Lin1b92c1c2012-03-16 23:05:41 -0700828module_i2c_driver(mpu3050_i2c_driver);
Joseph Lai631b16e2011-06-27 13:26:53 -0700829
830MODULE_AUTHOR("Wistron Corp.");
831MODULE_DESCRIPTION("MPU3050 Tri-axis gyroscope driver");
832MODULE_LICENSE("GPL");