Joseph Lai | 631b16e | 2011-06-27 13:26:53 -0700 | [diff] [blame] | 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); |
Heikki Krogerus | 3b51872 | 2011-12-23 23:57:09 -0800 | [diff] [blame^] | 151 | int error; |
Joseph Lai | 631b16e | 2011-06-27 13:26:53 -0700 | [diff] [blame] | 152 | |
| 153 | pm_runtime_get(sensor->dev); |
| 154 | |
Heikki Krogerus | 3b51872 | 2011-12-23 23:57:09 -0800 | [diff] [blame^] | 155 | /* Enable interrupts */ |
| 156 | error = i2c_smbus_write_byte_data(sensor->client, MPU3050_INT_CFG, |
| 157 | MPU3050_LATCH_INT_EN | |
| 158 | MPU3050_RAW_RDY_EN | |
| 159 | MPU3050_MPU_RDY_EN); |
| 160 | if (error < 0) { |
| 161 | pm_runtime_put(sensor->dev); |
| 162 | return error; |
| 163 | } |
| 164 | |
Joseph Lai | 631b16e | 2011-06-27 13:26:53 -0700 | [diff] [blame] | 165 | return 0; |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * mpu3050_input_close - called on input event close |
| 170 | * @input: input dev of closed device |
| 171 | * |
| 172 | * The input layer calls this function when input event is closed. The |
| 173 | * function will push the device to suspend. |
| 174 | */ |
| 175 | static void mpu3050_input_close(struct input_dev *input) |
| 176 | { |
| 177 | struct mpu3050_sensor *sensor = input_get_drvdata(input); |
| 178 | |
| 179 | pm_runtime_put(sensor->dev); |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * mpu3050_interrupt_thread - handle an IRQ |
| 184 | * @irq: interrupt numner |
| 185 | * @data: the sensor |
| 186 | * |
| 187 | * Called by the kernel single threaded after an interrupt occurs. Read |
| 188 | * the sensor data and generate an input event for it. |
| 189 | */ |
| 190 | static irqreturn_t mpu3050_interrupt_thread(int irq, void *data) |
| 191 | { |
| 192 | struct mpu3050_sensor *sensor = data; |
| 193 | struct axis_data axis; |
| 194 | |
| 195 | mpu3050_read_xyz(sensor->client, &axis); |
| 196 | |
| 197 | input_report_abs(sensor->idev, ABS_X, axis.x); |
| 198 | input_report_abs(sensor->idev, ABS_Y, axis.y); |
| 199 | input_report_abs(sensor->idev, ABS_Z, axis.z); |
| 200 | input_sync(sensor->idev); |
| 201 | |
| 202 | return IRQ_HANDLED; |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * mpu3050_probe - device detection callback |
| 207 | * @client: i2c client of found device |
| 208 | * @id: id match information |
| 209 | * |
| 210 | * The I2C layer calls us when it believes a sensor is present at this |
| 211 | * address. Probe to see if this is correct and to validate the device. |
| 212 | * |
| 213 | * If present install the relevant sysfs interfaces and input device. |
| 214 | */ |
| 215 | static int __devinit mpu3050_probe(struct i2c_client *client, |
| 216 | const struct i2c_device_id *id) |
| 217 | { |
| 218 | struct mpu3050_sensor *sensor; |
| 219 | struct input_dev *idev; |
| 220 | int ret; |
| 221 | int error; |
| 222 | |
| 223 | sensor = kzalloc(sizeof(struct mpu3050_sensor), GFP_KERNEL); |
| 224 | idev = input_allocate_device(); |
| 225 | if (!sensor || !idev) { |
| 226 | dev_err(&client->dev, "failed to allocate driver data\n"); |
| 227 | error = -ENOMEM; |
| 228 | goto err_free_mem; |
| 229 | } |
| 230 | |
| 231 | sensor->client = client; |
| 232 | sensor->dev = &client->dev; |
| 233 | sensor->idev = idev; |
| 234 | |
| 235 | mpu3050_set_power_mode(client, 1); |
| 236 | msleep(10); |
| 237 | |
| 238 | ret = i2c_smbus_read_byte_data(client, MPU3050_CHIP_ID_REG); |
| 239 | if (ret < 0) { |
| 240 | dev_err(&client->dev, "failed to detect device\n"); |
| 241 | error = -ENXIO; |
| 242 | goto err_free_mem; |
| 243 | } |
| 244 | |
| 245 | if (ret != MPU3050_CHIP_ID) { |
| 246 | dev_err(&client->dev, "unsupported chip id\n"); |
| 247 | error = -ENXIO; |
| 248 | goto err_free_mem; |
| 249 | } |
| 250 | |
| 251 | idev->name = "MPU3050"; |
| 252 | idev->id.bustype = BUS_I2C; |
| 253 | idev->dev.parent = &client->dev; |
| 254 | |
| 255 | idev->open = mpu3050_input_open; |
| 256 | idev->close = mpu3050_input_close; |
| 257 | |
| 258 | __set_bit(EV_ABS, idev->evbit); |
| 259 | input_set_abs_params(idev, ABS_X, |
| 260 | MPU3050_MIN_VALUE, MPU3050_MAX_VALUE, 0, 0); |
| 261 | input_set_abs_params(idev, ABS_Y, |
| 262 | MPU3050_MIN_VALUE, MPU3050_MAX_VALUE, 0, 0); |
| 263 | input_set_abs_params(idev, ABS_Z, |
| 264 | MPU3050_MIN_VALUE, MPU3050_MAX_VALUE, 0, 0); |
| 265 | |
| 266 | input_set_drvdata(idev, sensor); |
| 267 | |
| 268 | pm_runtime_set_active(&client->dev); |
| 269 | |
| 270 | error = request_threaded_irq(client->irq, |
| 271 | NULL, mpu3050_interrupt_thread, |
| 272 | IRQF_TRIGGER_RISING, |
Heikki Krogerus | 3b51872 | 2011-12-23 23:57:09 -0800 | [diff] [blame^] | 273 | "mpu3050", sensor); |
Joseph Lai | 631b16e | 2011-06-27 13:26:53 -0700 | [diff] [blame] | 274 | if (error) { |
| 275 | dev_err(&client->dev, |
| 276 | "can't get IRQ %d, error %d\n", client->irq, error); |
| 277 | goto err_pm_set_suspended; |
| 278 | } |
| 279 | |
| 280 | error = input_register_device(idev); |
| 281 | if (error) { |
| 282 | dev_err(&client->dev, "failed to register input device\n"); |
| 283 | goto err_free_irq; |
| 284 | } |
| 285 | |
| 286 | pm_runtime_enable(&client->dev); |
| 287 | pm_runtime_set_autosuspend_delay(&client->dev, MPU3050_AUTO_DELAY); |
| 288 | |
| 289 | return 0; |
| 290 | |
| 291 | err_free_irq: |
| 292 | free_irq(client->irq, sensor); |
| 293 | err_pm_set_suspended: |
| 294 | pm_runtime_set_suspended(&client->dev); |
| 295 | err_free_mem: |
Axel Lin | d9b830f | 2011-08-11 09:19:29 -0700 | [diff] [blame] | 296 | input_free_device(idev); |
Joseph Lai | 631b16e | 2011-06-27 13:26:53 -0700 | [diff] [blame] | 297 | kfree(sensor); |
| 298 | return error; |
| 299 | } |
| 300 | |
| 301 | /** |
| 302 | * mpu3050_remove - remove a sensor |
| 303 | * @client: i2c client of sensor being removed |
| 304 | * |
| 305 | * Our sensor is going away, clean up the resources. |
| 306 | */ |
| 307 | static int __devexit mpu3050_remove(struct i2c_client *client) |
| 308 | { |
| 309 | struct mpu3050_sensor *sensor = i2c_get_clientdata(client); |
| 310 | |
| 311 | pm_runtime_disable(&client->dev); |
| 312 | pm_runtime_set_suspended(&client->dev); |
| 313 | |
| 314 | free_irq(client->irq, sensor); |
| 315 | input_unregister_device(sensor->idev); |
| 316 | kfree(sensor); |
| 317 | |
| 318 | return 0; |
| 319 | } |
| 320 | |
| 321 | #ifdef CONFIG_PM |
| 322 | /** |
| 323 | * mpu3050_suspend - called on device suspend |
| 324 | * @dev: device being suspended |
| 325 | * |
| 326 | * Put the device into sleep mode before we suspend the machine. |
| 327 | */ |
| 328 | static int mpu3050_suspend(struct device *dev) |
| 329 | { |
| 330 | struct i2c_client *client = to_i2c_client(dev); |
| 331 | |
| 332 | mpu3050_set_power_mode(client, 0); |
| 333 | |
| 334 | return 0; |
| 335 | } |
| 336 | |
| 337 | /** |
| 338 | * mpu3050_resume - called on device resume |
| 339 | * @dev: device being resumed |
| 340 | * |
| 341 | * Put the device into powered mode on resume. |
| 342 | */ |
| 343 | static int mpu3050_resume(struct device *dev) |
| 344 | { |
| 345 | struct i2c_client *client = to_i2c_client(dev); |
| 346 | |
| 347 | mpu3050_set_power_mode(client, 1); |
| 348 | msleep(100); /* wait for gyro chip resume */ |
| 349 | |
| 350 | return 0; |
| 351 | } |
| 352 | #endif |
| 353 | |
| 354 | static UNIVERSAL_DEV_PM_OPS(mpu3050_pm, mpu3050_suspend, mpu3050_resume, NULL); |
| 355 | |
| 356 | static const struct i2c_device_id mpu3050_ids[] = { |
| 357 | { "mpu3050", 0 }, |
| 358 | { } |
| 359 | }; |
| 360 | MODULE_DEVICE_TABLE(i2c, mpu3050_ids); |
| 361 | |
Olof Johansson | e948981 | 2011-12-23 01:20:44 -0800 | [diff] [blame] | 362 | static const struct of_device_id mpu3050_of_match[] = { |
| 363 | { .compatible = "invn,mpu3050", }, |
| 364 | { }, |
| 365 | }; |
| 366 | MODULE_DEVICE_TABLE(of, mpu3050_of_match); |
| 367 | |
Joseph Lai | 631b16e | 2011-06-27 13:26:53 -0700 | [diff] [blame] | 368 | static struct i2c_driver mpu3050_i2c_driver = { |
| 369 | .driver = { |
| 370 | .name = "mpu3050", |
| 371 | .owner = THIS_MODULE, |
| 372 | .pm = &mpu3050_pm, |
Olof Johansson | e948981 | 2011-12-23 01:20:44 -0800 | [diff] [blame] | 373 | .of_match_table = mpu3050_of_match, |
Joseph Lai | 631b16e | 2011-06-27 13:26:53 -0700 | [diff] [blame] | 374 | }, |
| 375 | .probe = mpu3050_probe, |
| 376 | .remove = __devexit_p(mpu3050_remove), |
| 377 | .id_table = mpu3050_ids, |
| 378 | }; |
| 379 | |
| 380 | static int __init mpu3050_init(void) |
| 381 | { |
| 382 | return i2c_add_driver(&mpu3050_i2c_driver); |
| 383 | } |
| 384 | module_init(mpu3050_init); |
| 385 | |
| 386 | static void __exit mpu3050_exit(void) |
| 387 | { |
| 388 | i2c_del_driver(&mpu3050_i2c_driver); |
| 389 | } |
| 390 | module_exit(mpu3050_exit); |
| 391 | |
| 392 | MODULE_AUTHOR("Wistron Corp."); |
| 393 | MODULE_DESCRIPTION("MPU3050 Tri-axis gyroscope driver"); |
| 394 | MODULE_LICENSE("GPL"); |