Staging: hv: mousevsc: Cleanup and properly implement reportdesc_callback()
[zen-stable.git] / drivers / input / misc / mpu3050.c
blobf71dc728da58adcad257da28f053b98fc29fa3fa
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/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>
44 #define MPU3050_CHIP_ID_REG 0x00
45 #define MPU3050_CHIP_ID 0x69
46 #define MPU3050_XOUT_H 0x1D
47 #define MPU3050_PWR_MGM 0x3E
48 #define MPU3050_PWR_MGM_POS 6
49 #define MPU3050_PWR_MGM_MASK 0x40
51 #define MPU3050_AUTO_DELAY 1000
53 #define MPU3050_MIN_VALUE -32768
54 #define MPU3050_MAX_VALUE 32767
56 struct axis_data {
57 s16 x;
58 s16 y;
59 s16 z;
62 struct mpu3050_sensor {
63 struct i2c_client *client;
64 struct device *dev;
65 struct input_dev *idev;
68 /**
69 * mpu3050_xyz_read_reg - read the axes values
70 * @buffer: provide register addr and get register
71 * @length: length of register
73 * Reads the register values in one transaction or returns a negative
74 * error code on failure.
76 static int mpu3050_xyz_read_reg(struct i2c_client *client,
77 u8 *buffer, int length)
80 * Annoying we can't make this const because the i2c layer doesn't
81 * declare input buffers const.
83 char cmd = MPU3050_XOUT_H;
84 struct i2c_msg msg[] = {
86 .addr = client->addr,
87 .flags = 0,
88 .len = 1,
89 .buf = &cmd,
92 .addr = client->addr,
93 .flags = I2C_M_RD,
94 .len = length,
95 .buf = buffer,
99 return i2c_transfer(client->adapter, msg, 2);
103 * mpu3050_read_xyz - get co-ordinates from device
104 * @client: i2c address of sensor
105 * @coords: co-ordinates to update
107 * Return the converted X Y and Z co-ordinates from the sensor device
109 static void mpu3050_read_xyz(struct i2c_client *client,
110 struct axis_data *coords)
112 u16 buffer[3];
114 mpu3050_xyz_read_reg(client, (u8 *)buffer, 6);
115 coords->x = be16_to_cpu(buffer[0]);
116 coords->y = be16_to_cpu(buffer[1]);
117 coords->z = be16_to_cpu(buffer[2]);
118 dev_dbg(&client->dev, "%s: x %d, y %d, z %d\n", __func__,
119 coords->x, coords->y, coords->z);
123 * mpu3050_set_power_mode - set the power mode
124 * @client: i2c client for the sensor
125 * @val: value to switch on/off of power, 1: normal power, 0: low power
127 * Put device to normal-power mode or low-power mode.
129 static void mpu3050_set_power_mode(struct i2c_client *client, u8 val)
131 u8 value;
133 value = i2c_smbus_read_byte_data(client, MPU3050_PWR_MGM);
134 value = (value & ~MPU3050_PWR_MGM_MASK) |
135 (((val << MPU3050_PWR_MGM_POS) & MPU3050_PWR_MGM_MASK) ^
136 MPU3050_PWR_MGM_MASK);
137 i2c_smbus_write_byte_data(client, MPU3050_PWR_MGM, value);
141 * mpu3050_input_open - called on input event open
142 * @input: input dev of opened device
144 * The input layer calls this function when input event is opened. The
145 * function will push the device to resume. Then, the device is ready
146 * to provide data.
148 static int mpu3050_input_open(struct input_dev *input)
150 struct mpu3050_sensor *sensor = input_get_drvdata(input);
152 pm_runtime_get(sensor->dev);
154 return 0;
158 * mpu3050_input_close - called on input event close
159 * @input: input dev of closed device
161 * The input layer calls this function when input event is closed. The
162 * function will push the device to suspend.
164 static void mpu3050_input_close(struct input_dev *input)
166 struct mpu3050_sensor *sensor = input_get_drvdata(input);
168 pm_runtime_put(sensor->dev);
172 * mpu3050_interrupt_thread - handle an IRQ
173 * @irq: interrupt numner
174 * @data: the sensor
176 * Called by the kernel single threaded after an interrupt occurs. Read
177 * the sensor data and generate an input event for it.
179 static irqreturn_t mpu3050_interrupt_thread(int irq, void *data)
181 struct mpu3050_sensor *sensor = data;
182 struct axis_data axis;
184 mpu3050_read_xyz(sensor->client, &axis);
186 input_report_abs(sensor->idev, ABS_X, axis.x);
187 input_report_abs(sensor->idev, ABS_Y, axis.y);
188 input_report_abs(sensor->idev, ABS_Z, axis.z);
189 input_sync(sensor->idev);
191 return IRQ_HANDLED;
195 * mpu3050_probe - device detection callback
196 * @client: i2c client of found device
197 * @id: id match information
199 * The I2C layer calls us when it believes a sensor is present at this
200 * address. Probe to see if this is correct and to validate the device.
202 * If present install the relevant sysfs interfaces and input device.
204 static int __devinit mpu3050_probe(struct i2c_client *client,
205 const struct i2c_device_id *id)
207 struct mpu3050_sensor *sensor;
208 struct input_dev *idev;
209 int ret;
210 int error;
212 sensor = kzalloc(sizeof(struct mpu3050_sensor), GFP_KERNEL);
213 idev = input_allocate_device();
214 if (!sensor || !idev) {
215 dev_err(&client->dev, "failed to allocate driver data\n");
216 error = -ENOMEM;
217 goto err_free_mem;
220 sensor->client = client;
221 sensor->dev = &client->dev;
222 sensor->idev = idev;
224 mpu3050_set_power_mode(client, 1);
225 msleep(10);
227 ret = i2c_smbus_read_byte_data(client, MPU3050_CHIP_ID_REG);
228 if (ret < 0) {
229 dev_err(&client->dev, "failed to detect device\n");
230 error = -ENXIO;
231 goto err_free_mem;
234 if (ret != MPU3050_CHIP_ID) {
235 dev_err(&client->dev, "unsupported chip id\n");
236 error = -ENXIO;
237 goto err_free_mem;
240 idev->name = "MPU3050";
241 idev->id.bustype = BUS_I2C;
242 idev->dev.parent = &client->dev;
244 idev->open = mpu3050_input_open;
245 idev->close = mpu3050_input_close;
247 __set_bit(EV_ABS, idev->evbit);
248 input_set_abs_params(idev, ABS_X,
249 MPU3050_MIN_VALUE, MPU3050_MAX_VALUE, 0, 0);
250 input_set_abs_params(idev, ABS_Y,
251 MPU3050_MIN_VALUE, MPU3050_MAX_VALUE, 0, 0);
252 input_set_abs_params(idev, ABS_Z,
253 MPU3050_MIN_VALUE, MPU3050_MAX_VALUE, 0, 0);
255 input_set_drvdata(idev, sensor);
257 pm_runtime_set_active(&client->dev);
259 error = request_threaded_irq(client->irq,
260 NULL, mpu3050_interrupt_thread,
261 IRQF_TRIGGER_RISING,
262 "mpu_int", sensor);
263 if (error) {
264 dev_err(&client->dev,
265 "can't get IRQ %d, error %d\n", client->irq, error);
266 goto err_pm_set_suspended;
269 error = input_register_device(idev);
270 if (error) {
271 dev_err(&client->dev, "failed to register input device\n");
272 goto err_free_irq;
275 pm_runtime_enable(&client->dev);
276 pm_runtime_set_autosuspend_delay(&client->dev, MPU3050_AUTO_DELAY);
278 return 0;
280 err_free_irq:
281 free_irq(client->irq, sensor);
282 err_pm_set_suspended:
283 pm_runtime_set_suspended(&client->dev);
284 err_free_mem:
285 input_free_device(idev);
286 kfree(sensor);
287 return error;
291 * mpu3050_remove - remove a sensor
292 * @client: i2c client of sensor being removed
294 * Our sensor is going away, clean up the resources.
296 static int __devexit mpu3050_remove(struct i2c_client *client)
298 struct mpu3050_sensor *sensor = i2c_get_clientdata(client);
300 pm_runtime_disable(&client->dev);
301 pm_runtime_set_suspended(&client->dev);
303 free_irq(client->irq, sensor);
304 input_unregister_device(sensor->idev);
305 kfree(sensor);
307 return 0;
310 #ifdef CONFIG_PM
312 * mpu3050_suspend - called on device suspend
313 * @dev: device being suspended
315 * Put the device into sleep mode before we suspend the machine.
317 static int mpu3050_suspend(struct device *dev)
319 struct i2c_client *client = to_i2c_client(dev);
321 mpu3050_set_power_mode(client, 0);
323 return 0;
327 * mpu3050_resume - called on device resume
328 * @dev: device being resumed
330 * Put the device into powered mode on resume.
332 static int mpu3050_resume(struct device *dev)
334 struct i2c_client *client = to_i2c_client(dev);
336 mpu3050_set_power_mode(client, 1);
337 msleep(100); /* wait for gyro chip resume */
339 return 0;
341 #endif
343 static UNIVERSAL_DEV_PM_OPS(mpu3050_pm, mpu3050_suspend, mpu3050_resume, NULL);
345 static const struct i2c_device_id mpu3050_ids[] = {
346 { "mpu3050", 0 },
349 MODULE_DEVICE_TABLE(i2c, mpu3050_ids);
351 static struct i2c_driver mpu3050_i2c_driver = {
352 .driver = {
353 .name = "mpu3050",
354 .owner = THIS_MODULE,
355 .pm = &mpu3050_pm,
357 .probe = mpu3050_probe,
358 .remove = __devexit_p(mpu3050_remove),
359 .id_table = mpu3050_ids,
362 static int __init mpu3050_init(void)
364 return i2c_add_driver(&mpu3050_i2c_driver);
366 module_init(mpu3050_init);
368 static void __exit mpu3050_exit(void)
370 i2c_del_driver(&mpu3050_i2c_driver);
372 module_exit(mpu3050_exit);
374 MODULE_AUTHOR("Wistron Corp.");
375 MODULE_DESCRIPTION("MPU3050 Tri-axis gyroscope driver");
376 MODULE_LICENSE("GPL");