]>
Commit | Line | Data |
---|---|---|
631b16e8 JL |
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 | ||
631b16e8 | 44 | #define MPU3050_CHIP_ID 0x69 |
631b16e8 JL |
45 | |
46 | #define MPU3050_AUTO_DELAY 1000 | |
47 | ||
48 | #define MPU3050_MIN_VALUE -32768 | |
49 | #define MPU3050_MAX_VALUE 32767 | |
50 | ||
cd314fa6 HK |
51 | #define MPU3050_DEFAULT_POLL_INTERVAL 200 |
52 | #define MPU3050_DEFAULT_FS_RANGE 3 | |
53 | ||
54 | /* Register map */ | |
55 | #define MPU3050_CHIP_ID_REG 0x00 | |
56 | #define MPU3050_SMPLRT_DIV 0x15 | |
57 | #define MPU3050_DLPF_FS_SYNC 0x16 | |
58 | #define MPU3050_INT_CFG 0x17 | |
59 | #define MPU3050_XOUT_H 0x1D | |
60 | #define MPU3050_PWR_MGM 0x3E | |
61 | #define MPU3050_PWR_MGM_POS 6 | |
62 | ||
63 | /* Register bits */ | |
64 | ||
65 | /* DLPF_FS_SYNC */ | |
66 | #define MPU3050_EXT_SYNC_NONE 0x00 | |
67 | #define MPU3050_EXT_SYNC_TEMP 0x20 | |
68 | #define MPU3050_EXT_SYNC_GYROX 0x40 | |
69 | #define MPU3050_EXT_SYNC_GYROY 0x60 | |
70 | #define MPU3050_EXT_SYNC_GYROZ 0x80 | |
71 | #define MPU3050_EXT_SYNC_ACCELX 0xA0 | |
72 | #define MPU3050_EXT_SYNC_ACCELY 0xC0 | |
73 | #define MPU3050_EXT_SYNC_ACCELZ 0xE0 | |
74 | #define MPU3050_EXT_SYNC_MASK 0xE0 | |
75 | #define MPU3050_FS_250DPS 0x00 | |
76 | #define MPU3050_FS_500DPS 0x08 | |
77 | #define MPU3050_FS_1000DPS 0x10 | |
78 | #define MPU3050_FS_2000DPS 0x18 | |
79 | #define MPU3050_FS_MASK 0x18 | |
80 | #define MPU3050_DLPF_CFG_256HZ_NOLPF2 0x00 | |
81 | #define MPU3050_DLPF_CFG_188HZ 0x01 | |
82 | #define MPU3050_DLPF_CFG_98HZ 0x02 | |
83 | #define MPU3050_DLPF_CFG_42HZ 0x03 | |
84 | #define MPU3050_DLPF_CFG_20HZ 0x04 | |
85 | #define MPU3050_DLPF_CFG_10HZ 0x05 | |
86 | #define MPU3050_DLPF_CFG_5HZ 0x06 | |
87 | #define MPU3050_DLPF_CFG_2100HZ_NOLPF 0x07 | |
88 | #define MPU3050_DLPF_CFG_MASK 0x07 | |
89 | /* INT_CFG */ | |
90 | #define MPU3050_RAW_RDY_EN 0x01 | |
91 | #define MPU3050_MPU_RDY_EN 0x02 | |
92 | #define MPU3050_LATCH_INT_EN 0x04 | |
93 | /* PWR_MGM */ | |
94 | #define MPU3050_PWR_MGM_PLL_X 0x01 | |
95 | #define MPU3050_PWR_MGM_PLL_Y 0x02 | |
96 | #define MPU3050_PWR_MGM_PLL_Z 0x03 | |
97 | #define MPU3050_PWR_MGM_CLKSEL 0x07 | |
98 | #define MPU3050_PWR_MGM_STBY_ZG 0x08 | |
99 | #define MPU3050_PWR_MGM_STBY_YG 0x10 | |
100 | #define MPU3050_PWR_MGM_STBY_XG 0x20 | |
101 | #define MPU3050_PWR_MGM_SLEEP 0x40 | |
102 | #define MPU3050_PWR_MGM_RESET 0x80 | |
103 | #define MPU3050_PWR_MGM_MASK 0x40 | |
104 | ||
631b16e8 JL |
105 | struct axis_data { |
106 | s16 x; | |
107 | s16 y; | |
108 | s16 z; | |
109 | }; | |
110 | ||
111 | struct mpu3050_sensor { | |
112 | struct i2c_client *client; | |
113 | struct device *dev; | |
114 | struct input_dev *idev; | |
115 | }; | |
116 | ||
117 | /** | |
118 | * mpu3050_xyz_read_reg - read the axes values | |
119 | * @buffer: provide register addr and get register | |
120 | * @length: length of register | |
121 | * | |
122 | * Reads the register values in one transaction or returns a negative | |
123 | * error code on failure. | |
124 | */ | |
125 | static int mpu3050_xyz_read_reg(struct i2c_client *client, | |
126 | u8 *buffer, int length) | |
127 | { | |
128 | /* | |
129 | * Annoying we can't make this const because the i2c layer doesn't | |
130 | * declare input buffers const. | |
131 | */ | |
132 | char cmd = MPU3050_XOUT_H; | |
133 | struct i2c_msg msg[] = { | |
134 | { | |
135 | .addr = client->addr, | |
136 | .flags = 0, | |
137 | .len = 1, | |
138 | .buf = &cmd, | |
139 | }, | |
140 | { | |
141 | .addr = client->addr, | |
142 | .flags = I2C_M_RD, | |
143 | .len = length, | |
144 | .buf = buffer, | |
145 | }, | |
146 | }; | |
147 | ||
148 | return i2c_transfer(client->adapter, msg, 2); | |
149 | } | |
150 | ||
151 | /** | |
152 | * mpu3050_read_xyz - get co-ordinates from device | |
153 | * @client: i2c address of sensor | |
154 | * @coords: co-ordinates to update | |
155 | * | |
156 | * Return the converted X Y and Z co-ordinates from the sensor device | |
157 | */ | |
158 | static void mpu3050_read_xyz(struct i2c_client *client, | |
159 | struct axis_data *coords) | |
160 | { | |
161 | u16 buffer[3]; | |
162 | ||
163 | mpu3050_xyz_read_reg(client, (u8 *)buffer, 6); | |
164 | coords->x = be16_to_cpu(buffer[0]); | |
165 | coords->y = be16_to_cpu(buffer[1]); | |
166 | coords->z = be16_to_cpu(buffer[2]); | |
167 | dev_dbg(&client->dev, "%s: x %d, y %d, z %d\n", __func__, | |
168 | coords->x, coords->y, coords->z); | |
169 | } | |
170 | ||
171 | /** | |
172 | * mpu3050_set_power_mode - set the power mode | |
173 | * @client: i2c client for the sensor | |
174 | * @val: value to switch on/off of power, 1: normal power, 0: low power | |
175 | * | |
176 | * Put device to normal-power mode or low-power mode. | |
177 | */ | |
178 | static void mpu3050_set_power_mode(struct i2c_client *client, u8 val) | |
179 | { | |
180 | u8 value; | |
181 | ||
182 | value = i2c_smbus_read_byte_data(client, MPU3050_PWR_MGM); | |
183 | value = (value & ~MPU3050_PWR_MGM_MASK) | | |
184 | (((val << MPU3050_PWR_MGM_POS) & MPU3050_PWR_MGM_MASK) ^ | |
185 | MPU3050_PWR_MGM_MASK); | |
186 | i2c_smbus_write_byte_data(client, MPU3050_PWR_MGM, value); | |
187 | } | |
188 | ||
189 | /** | |
190 | * mpu3050_input_open - called on input event open | |
191 | * @input: input dev of opened device | |
192 | * | |
193 | * The input layer calls this function when input event is opened. The | |
194 | * function will push the device to resume. Then, the device is ready | |
195 | * to provide data. | |
196 | */ | |
197 | static int mpu3050_input_open(struct input_dev *input) | |
198 | { | |
199 | struct mpu3050_sensor *sensor = input_get_drvdata(input); | |
3b518724 | 200 | int error; |
631b16e8 JL |
201 | |
202 | pm_runtime_get(sensor->dev); | |
203 | ||
3b518724 HK |
204 | /* Enable interrupts */ |
205 | error = i2c_smbus_write_byte_data(sensor->client, MPU3050_INT_CFG, | |
206 | MPU3050_LATCH_INT_EN | | |
207 | MPU3050_RAW_RDY_EN | | |
208 | MPU3050_MPU_RDY_EN); | |
209 | if (error < 0) { | |
210 | pm_runtime_put(sensor->dev); | |
211 | return error; | |
212 | } | |
213 | ||
631b16e8 JL |
214 | return 0; |
215 | } | |
216 | ||
217 | /** | |
218 | * mpu3050_input_close - called on input event close | |
219 | * @input: input dev of closed device | |
220 | * | |
221 | * The input layer calls this function when input event is closed. The | |
222 | * function will push the device to suspend. | |
223 | */ | |
224 | static void mpu3050_input_close(struct input_dev *input) | |
225 | { | |
226 | struct mpu3050_sensor *sensor = input_get_drvdata(input); | |
227 | ||
228 | pm_runtime_put(sensor->dev); | |
229 | } | |
230 | ||
231 | /** | |
232 | * mpu3050_interrupt_thread - handle an IRQ | |
233 | * @irq: interrupt numner | |
234 | * @data: the sensor | |
235 | * | |
236 | * Called by the kernel single threaded after an interrupt occurs. Read | |
237 | * the sensor data and generate an input event for it. | |
238 | */ | |
239 | static irqreturn_t mpu3050_interrupt_thread(int irq, void *data) | |
240 | { | |
241 | struct mpu3050_sensor *sensor = data; | |
242 | struct axis_data axis; | |
243 | ||
244 | mpu3050_read_xyz(sensor->client, &axis); | |
245 | ||
246 | input_report_abs(sensor->idev, ABS_X, axis.x); | |
247 | input_report_abs(sensor->idev, ABS_Y, axis.y); | |
248 | input_report_abs(sensor->idev, ABS_Z, axis.z); | |
249 | input_sync(sensor->idev); | |
250 | ||
251 | return IRQ_HANDLED; | |
252 | } | |
253 | ||
cd314fa6 HK |
254 | /** |
255 | * mpu3050_hw_init - initialize hardware | |
256 | * @sensor: the sensor | |
257 | * | |
258 | * Called during device probe; configures the sampling method. | |
259 | */ | |
260 | static int __devinit mpu3050_hw_init(struct mpu3050_sensor *sensor) | |
261 | { | |
262 | struct i2c_client *client = sensor->client; | |
263 | int ret; | |
264 | u8 reg; | |
265 | ||
266 | /* Reset */ | |
267 | ret = i2c_smbus_write_byte_data(client, MPU3050_PWR_MGM, | |
268 | MPU3050_PWR_MGM_RESET); | |
269 | if (ret < 0) | |
270 | return ret; | |
271 | ||
272 | ret = i2c_smbus_read_byte_data(client, MPU3050_PWR_MGM); | |
273 | if (ret < 0) | |
274 | return ret; | |
275 | ||
276 | ret &= ~MPU3050_PWR_MGM_CLKSEL; | |
277 | ret |= MPU3050_PWR_MGM_PLL_Z; | |
278 | ret = i2c_smbus_write_byte_data(client, MPU3050_PWR_MGM, ret); | |
279 | if (ret < 0) | |
280 | return ret; | |
281 | ||
282 | /* Output frequency divider. The poll interval */ | |
283 | ret = i2c_smbus_write_byte_data(client, MPU3050_SMPLRT_DIV, | |
284 | MPU3050_DEFAULT_POLL_INTERVAL - 1); | |
285 | if (ret < 0) | |
286 | return ret; | |
287 | ||
288 | /* Set low pass filter and full scale */ | |
289 | reg = MPU3050_DEFAULT_FS_RANGE; | |
290 | reg |= MPU3050_DLPF_CFG_42HZ << 3; | |
291 | reg |= MPU3050_EXT_SYNC_NONE << 5; | |
292 | ret = i2c_smbus_write_byte_data(client, MPU3050_DLPF_FS_SYNC, reg); | |
293 | if (ret < 0) | |
294 | return ret; | |
295 | ||
296 | return 0; | |
297 | } | |
298 | ||
631b16e8 JL |
299 | /** |
300 | * mpu3050_probe - device detection callback | |
301 | * @client: i2c client of found device | |
302 | * @id: id match information | |
303 | * | |
304 | * The I2C layer calls us when it believes a sensor is present at this | |
305 | * address. Probe to see if this is correct and to validate the device. | |
306 | * | |
307 | * If present install the relevant sysfs interfaces and input device. | |
308 | */ | |
309 | static int __devinit mpu3050_probe(struct i2c_client *client, | |
310 | const struct i2c_device_id *id) | |
311 | { | |
312 | struct mpu3050_sensor *sensor; | |
313 | struct input_dev *idev; | |
314 | int ret; | |
315 | int error; | |
316 | ||
317 | sensor = kzalloc(sizeof(struct mpu3050_sensor), GFP_KERNEL); | |
318 | idev = input_allocate_device(); | |
319 | if (!sensor || !idev) { | |
320 | dev_err(&client->dev, "failed to allocate driver data\n"); | |
321 | error = -ENOMEM; | |
322 | goto err_free_mem; | |
323 | } | |
324 | ||
325 | sensor->client = client; | |
326 | sensor->dev = &client->dev; | |
327 | sensor->idev = idev; | |
328 | ||
329 | mpu3050_set_power_mode(client, 1); | |
330 | msleep(10); | |
331 | ||
332 | ret = i2c_smbus_read_byte_data(client, MPU3050_CHIP_ID_REG); | |
333 | if (ret < 0) { | |
334 | dev_err(&client->dev, "failed to detect device\n"); | |
335 | error = -ENXIO; | |
336 | goto err_free_mem; | |
337 | } | |
338 | ||
339 | if (ret != MPU3050_CHIP_ID) { | |
340 | dev_err(&client->dev, "unsupported chip id\n"); | |
341 | error = -ENXIO; | |
342 | goto err_free_mem; | |
343 | } | |
344 | ||
345 | idev->name = "MPU3050"; | |
346 | idev->id.bustype = BUS_I2C; | |
347 | idev->dev.parent = &client->dev; | |
348 | ||
349 | idev->open = mpu3050_input_open; | |
350 | idev->close = mpu3050_input_close; | |
351 | ||
352 | __set_bit(EV_ABS, idev->evbit); | |
353 | input_set_abs_params(idev, ABS_X, | |
354 | MPU3050_MIN_VALUE, MPU3050_MAX_VALUE, 0, 0); | |
355 | input_set_abs_params(idev, ABS_Y, | |
356 | MPU3050_MIN_VALUE, MPU3050_MAX_VALUE, 0, 0); | |
357 | input_set_abs_params(idev, ABS_Z, | |
358 | MPU3050_MIN_VALUE, MPU3050_MAX_VALUE, 0, 0); | |
359 | ||
360 | input_set_drvdata(idev, sensor); | |
361 | ||
362 | pm_runtime_set_active(&client->dev); | |
363 | ||
cd314fa6 HK |
364 | error = mpu3050_hw_init(sensor); |
365 | if (error) | |
366 | goto err_pm_set_suspended; | |
367 | ||
631b16e8 JL |
368 | error = request_threaded_irq(client->irq, |
369 | NULL, mpu3050_interrupt_thread, | |
f31ad406 | 370 | IRQF_TRIGGER_RISING | IRQF_ONESHOT, |
3b518724 | 371 | "mpu3050", sensor); |
631b16e8 JL |
372 | if (error) { |
373 | dev_err(&client->dev, | |
374 | "can't get IRQ %d, error %d\n", client->irq, error); | |
375 | goto err_pm_set_suspended; | |
376 | } | |
377 | ||
378 | error = input_register_device(idev); | |
379 | if (error) { | |
380 | dev_err(&client->dev, "failed to register input device\n"); | |
381 | goto err_free_irq; | |
382 | } | |
383 | ||
384 | pm_runtime_enable(&client->dev); | |
385 | pm_runtime_set_autosuspend_delay(&client->dev, MPU3050_AUTO_DELAY); | |
386 | ||
387 | return 0; | |
388 | ||
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: | |
d9b830fa | 394 | input_free_device(idev); |
631b16e8 JL |
395 | kfree(sensor); |
396 | return error; | |
397 | } | |
398 | ||
399 | /** | |
400 | * mpu3050_remove - remove a sensor | |
401 | * @client: i2c client of sensor being removed | |
402 | * | |
403 | * Our sensor is going away, clean up the resources. | |
404 | */ | |
405 | static int __devexit mpu3050_remove(struct i2c_client *client) | |
406 | { | |
407 | struct mpu3050_sensor *sensor = i2c_get_clientdata(client); | |
408 | ||
409 | pm_runtime_disable(&client->dev); | |
410 | pm_runtime_set_suspended(&client->dev); | |
411 | ||
412 | free_irq(client->irq, sensor); | |
413 | input_unregister_device(sensor->idev); | |
414 | kfree(sensor); | |
415 | ||
416 | return 0; | |
417 | } | |
418 | ||
419 | #ifdef CONFIG_PM | |
420 | /** | |
421 | * mpu3050_suspend - called on device suspend | |
422 | * @dev: device being suspended | |
423 | * | |
424 | * Put the device into sleep mode before we suspend the machine. | |
425 | */ | |
426 | static int mpu3050_suspend(struct device *dev) | |
427 | { | |
428 | struct i2c_client *client = to_i2c_client(dev); | |
429 | ||
430 | mpu3050_set_power_mode(client, 0); | |
431 | ||
432 | return 0; | |
433 | } | |
434 | ||
435 | /** | |
436 | * mpu3050_resume - called on device resume | |
437 | * @dev: device being resumed | |
438 | * | |
439 | * Put the device into powered mode on resume. | |
440 | */ | |
441 | static int mpu3050_resume(struct device *dev) | |
442 | { | |
443 | struct i2c_client *client = to_i2c_client(dev); | |
444 | ||
445 | mpu3050_set_power_mode(client, 1); | |
446 | msleep(100); /* wait for gyro chip resume */ | |
447 | ||
448 | return 0; | |
449 | } | |
450 | #endif | |
451 | ||
452 | static UNIVERSAL_DEV_PM_OPS(mpu3050_pm, mpu3050_suspend, mpu3050_resume, NULL); | |
453 | ||
454 | static const struct i2c_device_id mpu3050_ids[] = { | |
455 | { "mpu3050", 0 }, | |
456 | { } | |
457 | }; | |
458 | MODULE_DEVICE_TABLE(i2c, mpu3050_ids); | |
459 | ||
e948981a OJ |
460 | static const struct of_device_id mpu3050_of_match[] = { |
461 | { .compatible = "invn,mpu3050", }, | |
462 | { }, | |
463 | }; | |
464 | MODULE_DEVICE_TABLE(of, mpu3050_of_match); | |
465 | ||
631b16e8 JL |
466 | static struct i2c_driver mpu3050_i2c_driver = { |
467 | .driver = { | |
468 | .name = "mpu3050", | |
469 | .owner = THIS_MODULE, | |
470 | .pm = &mpu3050_pm, | |
e948981a | 471 | .of_match_table = mpu3050_of_match, |
631b16e8 JL |
472 | }, |
473 | .probe = mpu3050_probe, | |
1cb0aa88 | 474 | .remove = mpu3050_remove, |
631b16e8 JL |
475 | .id_table = mpu3050_ids, |
476 | }; | |
477 | ||
1b92c1cf | 478 | module_i2c_driver(mpu3050_i2c_driver); |
631b16e8 JL |
479 | |
480 | MODULE_AUTHOR("Wistron Corp."); | |
481 | MODULE_DESCRIPTION("MPU3050 Tri-axis gyroscope driver"); | |
482 | MODULE_LICENSE("GPL"); |