diff options
author | Joseph Lai <joseph_lai@wistron.com> | 2011-06-27 16:26:53 -0400 |
---|---|---|
committer | Dmitry Torokhov <dmitry.torokhov@gmail.com> | 2011-06-29 03:26:17 -0400 |
commit | 631b16e81eab82e2894425a94c3fc14bf21adb26 (patch) | |
tree | 0cc4e293087f15a273af5158c1e8e3f69087e2f8 /drivers/input/misc | |
parent | 7c40952295db64867a45938b860a217b622cc3ed (diff) |
Input: add a driver to support InvenSense mpu3050 gyroscope chip
This driver is registered as an input device. An IRQ is required in this
basic driver configuration.
Signed-off-by: Joseph Lai <joseph_lai@wistron.com>
[Cleaned up PM_RUNTIME defines]
Signed-off-by: Alan Cox <alan@linux.intel.com>
[dtor@mail.ru: consolidated PM methods, some code rearrangement]
Signed-off-by: Dmitry Torokhov <dtor@mail.ru>
Diffstat (limited to 'drivers/input/misc')
-rw-r--r-- | drivers/input/misc/Kconfig | 10 | ||||
-rw-r--r-- | drivers/input/misc/Makefile | 1 | ||||
-rw-r--r-- | drivers/input/misc/mpu3050.c | 376 |
3 files changed, 387 insertions, 0 deletions
diff --git a/drivers/input/misc/Kconfig b/drivers/input/misc/Kconfig index 0f22918ad9ce..6fdce4b86856 100644 --- a/drivers/input/misc/Kconfig +++ b/drivers/input/misc/Kconfig | |||
@@ -111,6 +111,16 @@ config INPUT_MMA8450 | |||
111 | To compile this driver as a module, choose M here: the | 111 | To compile this driver as a module, choose M here: the |
112 | module will be called mma8450. | 112 | module will be called mma8450. |
113 | 113 | ||
114 | config INPUT_MPU3050 | ||
115 | tristate "MPU3050 Triaxial gyroscope sensor" | ||
116 | depends on I2C | ||
117 | help | ||
118 | Say Y here if you want to support InvenSense MPU3050 | ||
119 | connected via an I2C bus. | ||
120 | |||
121 | To compile this driver as a module, choose M here: the | ||
122 | module will be called mpu3050. | ||
123 | |||
114 | config INPUT_APANEL | 124 | config INPUT_APANEL |
115 | tristate "Fujitsu Lifebook Application Panel buttons" | 125 | tristate "Fujitsu Lifebook Application Panel buttons" |
116 | depends on X86 && I2C && LEDS_CLASS | 126 | depends on X86 && I2C && LEDS_CLASS |
diff --git a/drivers/input/misc/Makefile b/drivers/input/misc/Makefile index 99953c3c442c..b7e227aa117a 100644 --- a/drivers/input/misc/Makefile +++ b/drivers/input/misc/Makefile | |||
@@ -28,6 +28,7 @@ obj-$(CONFIG_INPUT_KEYSPAN_REMOTE) += keyspan_remote.o | |||
28 | obj-$(CONFIG_INPUT_M68K_BEEP) += m68kspkr.o | 28 | obj-$(CONFIG_INPUT_M68K_BEEP) += m68kspkr.o |
29 | obj-$(CONFIG_INPUT_MAX8925_ONKEY) += max8925_onkey.o | 29 | obj-$(CONFIG_INPUT_MAX8925_ONKEY) += max8925_onkey.o |
30 | obj-$(CONFIG_INPUT_MMA8450) += mma8450.o | 30 | obj-$(CONFIG_INPUT_MMA8450) += mma8450.o |
31 | obj-$(CONFIG_INPUT_MPU3050) += mpu3050.o | ||
31 | obj-$(CONFIG_INPUT_PCAP) += pcap_keys.o | 32 | obj-$(CONFIG_INPUT_PCAP) += pcap_keys.o |
32 | obj-$(CONFIG_INPUT_PCF50633_PMU) += pcf50633-input.o | 33 | obj-$(CONFIG_INPUT_PCF50633_PMU) += pcf50633-input.o |
33 | obj-$(CONFIG_INPUT_PCF8574) += pcf8574_keypad.o | 34 | obj-$(CONFIG_INPUT_PCF8574) += pcf8574_keypad.o |
diff --git a/drivers/input/misc/mpu3050.c b/drivers/input/misc/mpu3050.c new file mode 100644 index 000000000000..b95fac15b2ea --- /dev/null +++ b/drivers/input/misc/mpu3050.c | |||
@@ -0,0 +1,376 @@ | |||
1 | /* | ||
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> | ||
43 | |||
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 | ||
50 | |||
51 | #define MPU3050_AUTO_DELAY 1000 | ||
52 | |||
53 | #define MPU3050_MIN_VALUE -32768 | ||
54 | #define MPU3050_MAX_VALUE 32767 | ||
55 | |||
56 | struct axis_data { | ||
57 | s16 x; | ||
58 | s16 y; | ||
59 | s16 z; | ||
60 | }; | ||
61 | |||
62 | struct mpu3050_sensor { | ||
63 | struct i2c_client *client; | ||
64 | struct device *dev; | ||
65 | struct input_dev *idev; | ||
66 | }; | ||
67 | |||
68 | /** | ||
69 | * mpu3050_xyz_read_reg - read the axes values | ||
70 | * @buffer: provide register addr and get register | ||
71 | * @length: length of register | ||
72 | * | ||
73 | * Reads the register values in one transaction or returns a negative | ||
74 | * error code on failure. | ||
75 | */ | ||
76 | static int mpu3050_xyz_read_reg(struct i2c_client *client, | ||
77 | u8 *buffer, int length) | ||
78 | { | ||
79 | /* | ||
80 | * Annoying we can't make this const because the i2c layer doesn't | ||
81 | * declare input buffers const. | ||
82 | */ | ||
83 | char cmd = MPU3050_XOUT_H; | ||
84 | struct i2c_msg msg[] = { | ||
85 | { | ||
86 | .addr = client->addr, | ||
87 | .flags = 0, | ||
88 | .len = 1, | ||
89 | .buf = &cmd, | ||
90 | }, | ||
91 | { | ||
92 | .addr = client->addr, | ||
93 | .flags = I2C_M_RD, | ||
94 | .len = length, | ||
95 | .buf = buffer, | ||
96 | }, | ||
97 | }; | ||
98 | |||
99 | return i2c_transfer(client->adapter, msg, 2); | ||
100 | } | ||
101 | |||
102 | /** | ||
103 | * mpu3050_read_xyz - get co-ordinates from device | ||
104 | * @client: i2c address of sensor | ||
105 | * @coords: co-ordinates to update | ||
106 | * | ||
107 | * Return the converted X Y and Z co-ordinates from the sensor device | ||
108 | */ | ||
109 | static void mpu3050_read_xyz(struct i2c_client *client, | ||
110 | struct axis_data *coords) | ||
111 | { | ||
112 | u16 buffer[3]; | ||
113 | |||
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); | ||
120 | } | ||
121 | |||
122 | /** | ||
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 | ||
126 | * | ||
127 | * Put device to normal-power mode or low-power mode. | ||
128 | */ | ||
129 | static void mpu3050_set_power_mode(struct i2c_client *client, u8 val) | ||
130 | { | ||
131 | u8 value; | ||
132 | |||
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); | ||
138 | } | ||
139 | |||
140 | /** | ||
141 | * mpu3050_input_open - called on input event open | ||
142 | * @input: input dev of opened device | ||
143 | * | ||
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. | ||
147 | */ | ||
148 | static int mpu3050_input_open(struct input_dev *input) | ||
149 | { | ||
150 | struct mpu3050_sensor *sensor = input_get_drvdata(input); | ||
151 | |||
152 | pm_runtime_get(sensor->dev); | ||
153 | |||
154 | return 0; | ||
155 | } | ||
156 | |||
157 | /** | ||
158 | * mpu3050_input_close - called on input event close | ||
159 | * @input: input dev of closed device | ||
160 | * | ||
161 | * The input layer calls this function when input event is closed. The | ||
162 | * function will push the device to suspend. | ||
163 | */ | ||
164 | static void mpu3050_input_close(struct input_dev *input) | ||
165 | { | ||
166 | struct mpu3050_sensor *sensor = input_get_drvdata(input); | ||
167 | |||
168 | pm_runtime_put(sensor->dev); | ||
169 | } | ||
170 | |||
171 | /** | ||
172 | * mpu3050_interrupt_thread - handle an IRQ | ||
173 | * @irq: interrupt numner | ||
174 | * @data: the sensor | ||
175 | * | ||
176 | * Called by the kernel single threaded after an interrupt occurs. Read | ||
177 | * the sensor data and generate an input event for it. | ||
178 | */ | ||
179 | static irqreturn_t mpu3050_interrupt_thread(int irq, void *data) | ||
180 | { | ||
181 | struct mpu3050_sensor *sensor = data; | ||
182 | struct axis_data axis; | ||
183 | |||
184 | mpu3050_read_xyz(sensor->client, &axis); | ||
185 | |||
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); | ||
190 | |||
191 | return IRQ_HANDLED; | ||
192 | } | ||
193 | |||
194 | /** | ||
195 | * mpu3050_probe - device detection callback | ||
196 | * @client: i2c client of found device | ||
197 | * @id: id match information | ||
198 | * | ||
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. | ||
201 | * | ||
202 | * If present install the relevant sysfs interfaces and input device. | ||
203 | */ | ||
204 | static int __devinit mpu3050_probe(struct i2c_client *client, | ||
205 | const struct i2c_device_id *id) | ||
206 | { | ||
207 | struct mpu3050_sensor *sensor; | ||
208 | struct input_dev *idev; | ||
209 | int ret; | ||
210 | int error; | ||
211 | |||
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; | ||
218 | } | ||
219 | |||
220 | sensor->client = client; | ||
221 | sensor->dev = &client->dev; | ||
222 | sensor->idev = idev; | ||
223 | |||
224 | mpu3050_set_power_mode(client, 1); | ||
225 | msleep(10); | ||
226 | |||
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; | ||
232 | } | ||
233 | |||
234 | if (ret != MPU3050_CHIP_ID) { | ||
235 | dev_err(&client->dev, "unsupported chip id\n"); | ||
236 | error = -ENXIO; | ||
237 | goto err_free_mem; | ||
238 | } | ||
239 | |||
240 | idev->name = "MPU3050"; | ||
241 | idev->id.bustype = BUS_I2C; | ||
242 | idev->dev.parent = &client->dev; | ||
243 | |||
244 | idev->open = mpu3050_input_open; | ||
245 | idev->close = mpu3050_input_close; | ||
246 | |||
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); | ||
254 | |||
255 | input_set_drvdata(idev, sensor); | ||
256 | |||
257 | pm_runtime_set_active(&client->dev); | ||
258 | |||
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; | ||
267 | } | ||
268 | |||
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; | ||
273 | } | ||
274 | |||
275 | pm_runtime_enable(&client->dev); | ||
276 | pm_runtime_set_autosuspend_delay(&client->dev, MPU3050_AUTO_DELAY); | ||
277 | |||
278 | return 0; | ||
279 | |||
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_unregister_device(idev); | ||
286 | kfree(sensor); | ||
287 | return error; | ||
288 | } | ||
289 | |||
290 | /** | ||
291 | * mpu3050_remove - remove a sensor | ||
292 | * @client: i2c client of sensor being removed | ||
293 | * | ||
294 | * Our sensor is going away, clean up the resources. | ||
295 | */ | ||
296 | static int __devexit mpu3050_remove(struct i2c_client *client) | ||
297 | { | ||
298 | struct mpu3050_sensor *sensor = i2c_get_clientdata(client); | ||
299 | |||
300 | pm_runtime_disable(&client->dev); | ||
301 | pm_runtime_set_suspended(&client->dev); | ||
302 | |||
303 | free_irq(client->irq, sensor); | ||
304 | input_unregister_device(sensor->idev); | ||
305 | kfree(sensor); | ||
306 | |||
307 | return 0; | ||
308 | } | ||
309 | |||
310 | #ifdef CONFIG_PM | ||
311 | /** | ||
312 | * mpu3050_suspend - called on device suspend | ||
313 | * @dev: device being suspended | ||
314 | * | ||
315 | * Put the device into sleep mode before we suspend the machine. | ||
316 | */ | ||
317 | static int mpu3050_suspend(struct device *dev) | ||
318 | { | ||
319 | struct i2c_client *client = to_i2c_client(dev); | ||
320 | |||
321 | mpu3050_set_power_mode(client, 0); | ||
322 | |||
323 | return 0; | ||
324 | } | ||
325 | |||
326 | /** | ||
327 | * mpu3050_resume - called on device resume | ||
328 | * @dev: device being resumed | ||
329 | * | ||
330 | * Put the device into powered mode on resume. | ||
331 | */ | ||
332 | static int mpu3050_resume(struct device *dev) | ||
333 | { | ||
334 | struct i2c_client *client = to_i2c_client(dev); | ||
335 | |||
336 | mpu3050_set_power_mode(client, 1); | ||
337 | msleep(100); /* wait for gyro chip resume */ | ||
338 | |||
339 | return 0; | ||
340 | } | ||
341 | #endif | ||
342 | |||
343 | static UNIVERSAL_DEV_PM_OPS(mpu3050_pm, mpu3050_suspend, mpu3050_resume, NULL); | ||
344 | |||
345 | static const struct i2c_device_id mpu3050_ids[] = { | ||
346 | { "mpu3050", 0 }, | ||
347 | { } | ||
348 | }; | ||
349 | MODULE_DEVICE_TABLE(i2c, mpu3050_ids); | ||
350 | |||
351 | static struct i2c_driver mpu3050_i2c_driver = { | ||
352 | .driver = { | ||
353 | .name = "mpu3050", | ||
354 | .owner = THIS_MODULE, | ||
355 | .pm = &mpu3050_pm, | ||
356 | }, | ||
357 | .probe = mpu3050_probe, | ||
358 | .remove = __devexit_p(mpu3050_remove), | ||
359 | .id_table = mpu3050_ids, | ||
360 | }; | ||
361 | |||
362 | static int __init mpu3050_init(void) | ||
363 | { | ||
364 | return i2c_add_driver(&mpu3050_i2c_driver); | ||
365 | } | ||
366 | module_init(mpu3050_init); | ||
367 | |||
368 | static void __exit mpu3050_exit(void) | ||
369 | { | ||
370 | i2c_del_driver(&mpu3050_i2c_driver); | ||
371 | } | ||
372 | module_exit(mpu3050_exit); | ||
373 | |||
374 | MODULE_AUTHOR("Wistron Corp."); | ||
375 | MODULE_DESCRIPTION("MPU3050 Tri-axis gyroscope driver"); | ||
376 | MODULE_LICENSE("GPL"); | ||