Merge branch 'akpm' (patches from Andrew)
[linux-2.6/btrfs-unstable.git] / drivers / input / misc / mpu3050.c
blobf088db31cfc7c2f82ce937c0f015a9976ca1eb5e
1 /*
2 * MPU3050 Tri-axis gyroscope driver
4 * Copyright (C) 2011 Wistron Co.Ltd
5 * Joseph Lai <joseph_lai@wistron.com>
7 * Trimmed down by Alan Cox <alan@linux.intel.com> to produce this version
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.
15 * This program is based on bma023.c.
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.
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.
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.
32 #include <linux/module.h>
33 #include <linux/interrupt.h>
34 #include <linux/platform_device.h>
35 #include <linux/mutex.h>
36 #include <linux/err.h>
37 #include <linux/i2c.h>
38 #include <linux/input.h>
39 #include <linux/delay.h>
40 #include <linux/slab.h>
41 #include <linux/pm_runtime.h>
43 #define MPU3050_CHIP_ID 0x69
45 #define MPU3050_AUTO_DELAY 1000
47 #define MPU3050_MIN_VALUE -32768
48 #define MPU3050_MAX_VALUE 32767
50 #define MPU3050_DEFAULT_POLL_INTERVAL 200
51 #define MPU3050_DEFAULT_FS_RANGE 3
53 /* Register map */
54 #define MPU3050_CHIP_ID_REG 0x00
55 #define MPU3050_SMPLRT_DIV 0x15
56 #define MPU3050_DLPF_FS_SYNC 0x16
57 #define MPU3050_INT_CFG 0x17
58 #define MPU3050_XOUT_H 0x1D
59 #define MPU3050_PWR_MGM 0x3E
60 #define MPU3050_PWR_MGM_POS 6
62 /* Register bits */
64 /* DLPF_FS_SYNC */
65 #define MPU3050_EXT_SYNC_NONE 0x00
66 #define MPU3050_EXT_SYNC_TEMP 0x20
67 #define MPU3050_EXT_SYNC_GYROX 0x40
68 #define MPU3050_EXT_SYNC_GYROY 0x60
69 #define MPU3050_EXT_SYNC_GYROZ 0x80
70 #define MPU3050_EXT_SYNC_ACCELX 0xA0
71 #define MPU3050_EXT_SYNC_ACCELY 0xC0
72 #define MPU3050_EXT_SYNC_ACCELZ 0xE0
73 #define MPU3050_EXT_SYNC_MASK 0xE0
74 #define MPU3050_FS_250DPS 0x00
75 #define MPU3050_FS_500DPS 0x08
76 #define MPU3050_FS_1000DPS 0x10
77 #define MPU3050_FS_2000DPS 0x18
78 #define MPU3050_FS_MASK 0x18
79 #define MPU3050_DLPF_CFG_256HZ_NOLPF2 0x00
80 #define MPU3050_DLPF_CFG_188HZ 0x01
81 #define MPU3050_DLPF_CFG_98HZ 0x02
82 #define MPU3050_DLPF_CFG_42HZ 0x03
83 #define MPU3050_DLPF_CFG_20HZ 0x04
84 #define MPU3050_DLPF_CFG_10HZ 0x05
85 #define MPU3050_DLPF_CFG_5HZ 0x06
86 #define MPU3050_DLPF_CFG_2100HZ_NOLPF 0x07
87 #define MPU3050_DLPF_CFG_MASK 0x07
88 /* INT_CFG */
89 #define MPU3050_RAW_RDY_EN 0x01
90 #define MPU3050_MPU_RDY_EN 0x02
91 #define MPU3050_LATCH_INT_EN 0x04
92 /* PWR_MGM */
93 #define MPU3050_PWR_MGM_PLL_X 0x01
94 #define MPU3050_PWR_MGM_PLL_Y 0x02
95 #define MPU3050_PWR_MGM_PLL_Z 0x03
96 #define MPU3050_PWR_MGM_CLKSEL 0x07
97 #define MPU3050_PWR_MGM_STBY_ZG 0x08
98 #define MPU3050_PWR_MGM_STBY_YG 0x10
99 #define MPU3050_PWR_MGM_STBY_XG 0x20
100 #define MPU3050_PWR_MGM_SLEEP 0x40
101 #define MPU3050_PWR_MGM_RESET 0x80
102 #define MPU3050_PWR_MGM_MASK 0x40
104 struct axis_data {
105 s16 x;
106 s16 y;
107 s16 z;
110 struct mpu3050_sensor {
111 struct i2c_client *client;
112 struct device *dev;
113 struct input_dev *idev;
117 * mpu3050_xyz_read_reg - read the axes values
118 * @buffer: provide register addr and get register
119 * @length: length of register
121 * Reads the register values in one transaction or returns a negative
122 * error code on failure.
124 static int mpu3050_xyz_read_reg(struct i2c_client *client,
125 u8 *buffer, int length)
128 * Annoying we can't make this const because the i2c layer doesn't
129 * declare input buffers const.
131 char cmd = MPU3050_XOUT_H;
132 struct i2c_msg msg[] = {
134 .addr = client->addr,
135 .flags = 0,
136 .len = 1,
137 .buf = &cmd,
140 .addr = client->addr,
141 .flags = I2C_M_RD,
142 .len = length,
143 .buf = buffer,
147 return i2c_transfer(client->adapter, msg, 2);
151 * mpu3050_read_xyz - get co-ordinates from device
152 * @client: i2c address of sensor
153 * @coords: co-ordinates to update
155 * Return the converted X Y and Z co-ordinates from the sensor device
157 static void mpu3050_read_xyz(struct i2c_client *client,
158 struct axis_data *coords)
160 u16 buffer[3];
162 mpu3050_xyz_read_reg(client, (u8 *)buffer, 6);
163 coords->x = be16_to_cpu(buffer[0]);
164 coords->y = be16_to_cpu(buffer[1]);
165 coords->z = be16_to_cpu(buffer[2]);
166 dev_dbg(&client->dev, "%s: x %d, y %d, z %d\n", __func__,
167 coords->x, coords->y, coords->z);
171 * mpu3050_set_power_mode - set the power mode
172 * @client: i2c client for the sensor
173 * @val: value to switch on/off of power, 1: normal power, 0: low power
175 * Put device to normal-power mode or low-power mode.
177 static void mpu3050_set_power_mode(struct i2c_client *client, u8 val)
179 u8 value;
181 value = i2c_smbus_read_byte_data(client, MPU3050_PWR_MGM);
182 value = (value & ~MPU3050_PWR_MGM_MASK) |
183 (((val << MPU3050_PWR_MGM_POS) & MPU3050_PWR_MGM_MASK) ^
184 MPU3050_PWR_MGM_MASK);
185 i2c_smbus_write_byte_data(client, MPU3050_PWR_MGM, value);
189 * mpu3050_input_open - called on input event open
190 * @input: input dev of opened device
192 * The input layer calls this function when input event is opened. The
193 * function will push the device to resume. Then, the device is ready
194 * to provide data.
196 static int mpu3050_input_open(struct input_dev *input)
198 struct mpu3050_sensor *sensor = input_get_drvdata(input);
199 int error;
201 pm_runtime_get(sensor->dev);
203 /* Enable interrupts */
204 error = i2c_smbus_write_byte_data(sensor->client, MPU3050_INT_CFG,
205 MPU3050_LATCH_INT_EN |
206 MPU3050_RAW_RDY_EN |
207 MPU3050_MPU_RDY_EN);
208 if (error < 0) {
209 pm_runtime_put(sensor->dev);
210 return error;
213 return 0;
217 * mpu3050_input_close - called on input event close
218 * @input: input dev of closed device
220 * The input layer calls this function when input event is closed. The
221 * function will push the device to suspend.
223 static void mpu3050_input_close(struct input_dev *input)
225 struct mpu3050_sensor *sensor = input_get_drvdata(input);
227 pm_runtime_put(sensor->dev);
231 * mpu3050_interrupt_thread - handle an IRQ
232 * @irq: interrupt numner
233 * @data: the sensor
235 * Called by the kernel single threaded after an interrupt occurs. Read
236 * the sensor data and generate an input event for it.
238 static irqreturn_t mpu3050_interrupt_thread(int irq, void *data)
240 struct mpu3050_sensor *sensor = data;
241 struct axis_data axis;
243 mpu3050_read_xyz(sensor->client, &axis);
245 input_report_abs(sensor->idev, ABS_X, axis.x);
246 input_report_abs(sensor->idev, ABS_Y, axis.y);
247 input_report_abs(sensor->idev, ABS_Z, axis.z);
248 input_sync(sensor->idev);
250 return IRQ_HANDLED;
254 * mpu3050_hw_init - initialize hardware
255 * @sensor: the sensor
257 * Called during device probe; configures the sampling method.
259 static int mpu3050_hw_init(struct mpu3050_sensor *sensor)
261 struct i2c_client *client = sensor->client;
262 int ret;
263 u8 reg;
265 /* Reset */
266 ret = i2c_smbus_write_byte_data(client, MPU3050_PWR_MGM,
267 MPU3050_PWR_MGM_RESET);
268 if (ret < 0)
269 return ret;
271 ret = i2c_smbus_read_byte_data(client, MPU3050_PWR_MGM);
272 if (ret < 0)
273 return ret;
275 ret &= ~MPU3050_PWR_MGM_CLKSEL;
276 ret |= MPU3050_PWR_MGM_PLL_Z;
277 ret = i2c_smbus_write_byte_data(client, MPU3050_PWR_MGM, ret);
278 if (ret < 0)
279 return ret;
281 /* Output frequency divider. The poll interval */
282 ret = i2c_smbus_write_byte_data(client, MPU3050_SMPLRT_DIV,
283 MPU3050_DEFAULT_POLL_INTERVAL - 1);
284 if (ret < 0)
285 return ret;
287 /* Set low pass filter and full scale */
288 reg = MPU3050_DEFAULT_FS_RANGE;
289 reg |= MPU3050_DLPF_CFG_42HZ << 3;
290 reg |= MPU3050_EXT_SYNC_NONE << 5;
291 ret = i2c_smbus_write_byte_data(client, MPU3050_DLPF_FS_SYNC, reg);
292 if (ret < 0)
293 return ret;
295 return 0;
299 * mpu3050_probe - device detection callback
300 * @client: i2c client of found device
301 * @id: id match information
303 * The I2C layer calls us when it believes a sensor is present at this
304 * address. Probe to see if this is correct and to validate the device.
306 * If present install the relevant sysfs interfaces and input device.
308 static int mpu3050_probe(struct i2c_client *client,
309 const struct i2c_device_id *id)
311 struct mpu3050_sensor *sensor;
312 struct input_dev *idev;
313 int ret;
314 int error;
316 sensor = kzalloc(sizeof(struct mpu3050_sensor), GFP_KERNEL);
317 idev = input_allocate_device();
318 if (!sensor || !idev) {
319 dev_err(&client->dev, "failed to allocate driver data\n");
320 error = -ENOMEM;
321 goto err_free_mem;
324 sensor->client = client;
325 sensor->dev = &client->dev;
326 sensor->idev = idev;
328 mpu3050_set_power_mode(client, 1);
329 msleep(10);
331 ret = i2c_smbus_read_byte_data(client, MPU3050_CHIP_ID_REG);
332 if (ret < 0) {
333 dev_err(&client->dev, "failed to detect device\n");
334 error = -ENXIO;
335 goto err_free_mem;
338 if (ret != MPU3050_CHIP_ID) {
339 dev_err(&client->dev, "unsupported chip id\n");
340 error = -ENXIO;
341 goto err_free_mem;
344 idev->name = "MPU3050";
345 idev->id.bustype = BUS_I2C;
346 idev->dev.parent = &client->dev;
348 idev->open = mpu3050_input_open;
349 idev->close = mpu3050_input_close;
351 __set_bit(EV_ABS, idev->evbit);
352 input_set_abs_params(idev, ABS_X,
353 MPU3050_MIN_VALUE, MPU3050_MAX_VALUE, 0, 0);
354 input_set_abs_params(idev, ABS_Y,
355 MPU3050_MIN_VALUE, MPU3050_MAX_VALUE, 0, 0);
356 input_set_abs_params(idev, ABS_Z,
357 MPU3050_MIN_VALUE, MPU3050_MAX_VALUE, 0, 0);
359 input_set_drvdata(idev, sensor);
361 pm_runtime_set_active(&client->dev);
363 error = mpu3050_hw_init(sensor);
364 if (error)
365 goto err_pm_set_suspended;
367 error = request_threaded_irq(client->irq,
368 NULL, mpu3050_interrupt_thread,
369 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
370 "mpu3050", sensor);
371 if (error) {
372 dev_err(&client->dev,
373 "can't get IRQ %d, error %d\n", client->irq, error);
374 goto err_pm_set_suspended;
377 error = input_register_device(idev);
378 if (error) {
379 dev_err(&client->dev, "failed to register input device\n");
380 goto err_free_irq;
383 pm_runtime_enable(&client->dev);
384 pm_runtime_set_autosuspend_delay(&client->dev, MPU3050_AUTO_DELAY);
385 i2c_set_clientdata(client, sensor);
387 return 0;
389 err_free_irq:
390 free_irq(client->irq, sensor);
391 err_pm_set_suspended:
392 pm_runtime_set_suspended(&client->dev);
393 err_free_mem:
394 input_free_device(idev);
395 kfree(sensor);
396 return error;
400 * mpu3050_remove - remove a sensor
401 * @client: i2c client of sensor being removed
403 * Our sensor is going away, clean up the resources.
405 static int mpu3050_remove(struct i2c_client *client)
407 struct mpu3050_sensor *sensor = i2c_get_clientdata(client);
409 pm_runtime_disable(&client->dev);
410 pm_runtime_set_suspended(&client->dev);
412 free_irq(client->irq, sensor);
413 input_unregister_device(sensor->idev);
414 kfree(sensor);
416 return 0;
419 #ifdef CONFIG_PM
421 * mpu3050_suspend - called on device suspend
422 * @dev: device being suspended
424 * Put the device into sleep mode before we suspend the machine.
426 static int mpu3050_suspend(struct device *dev)
428 struct i2c_client *client = to_i2c_client(dev);
430 mpu3050_set_power_mode(client, 0);
432 return 0;
436 * mpu3050_resume - called on device resume
437 * @dev: device being resumed
439 * Put the device into powered mode on resume.
441 static int mpu3050_resume(struct device *dev)
443 struct i2c_client *client = to_i2c_client(dev);
445 mpu3050_set_power_mode(client, 1);
446 msleep(100); /* wait for gyro chip resume */
448 return 0;
450 #endif
452 static UNIVERSAL_DEV_PM_OPS(mpu3050_pm, mpu3050_suspend, mpu3050_resume, NULL);
454 static const struct i2c_device_id mpu3050_ids[] = {
455 { "mpu3050", 0 },
458 MODULE_DEVICE_TABLE(i2c, mpu3050_ids);
460 static const struct of_device_id mpu3050_of_match[] = {
461 { .compatible = "invn,mpu3050", },
462 { },
464 MODULE_DEVICE_TABLE(of, mpu3050_of_match);
466 static struct i2c_driver mpu3050_i2c_driver = {
467 .driver = {
468 .name = "mpu3050",
469 .pm = &mpu3050_pm,
470 .of_match_table = mpu3050_of_match,
472 .probe = mpu3050_probe,
473 .remove = mpu3050_remove,
474 .id_table = mpu3050_ids,
477 module_i2c_driver(mpu3050_i2c_driver);
479 MODULE_AUTHOR("Wistron Corp.");
480 MODULE_DESCRIPTION("MPU3050 Tri-axis gyroscope driver");
481 MODULE_LICENSE("GPL");