| 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> |
| Wentao Xu | c7769c0 | 2012-08-03 15:06:41 -0400 | [diff] [blame] | 43 | #include <linux/gpio.h> |
| Wentao Xu | dac9e60 | 2012-06-12 11:52:34 -0400 | [diff] [blame] | 44 | #include <linux/input/mpu3050.h> |
| 45 | #include <linux/regulator/consumer.h> |
| Joseph Lai | 631b16e | 2011-06-27 13:26:53 -0700 | [diff] [blame] | 46 | |
| Joseph Lai | 631b16e | 2011-06-27 13:26:53 -0700 | [diff] [blame] | 47 | #define MPU3050_CHIP_ID 0x69 |
| Joseph Lai | 631b16e | 2011-06-27 13:26:53 -0700 | [diff] [blame] | 48 | |
| 49 | #define MPU3050_AUTO_DELAY 1000 |
| 50 | |
| 51 | #define MPU3050_MIN_VALUE -32768 |
| 52 | #define MPU3050_MAX_VALUE 32767 |
| 53 | |
| Wentao Xu | c7769c0 | 2012-08-03 15:06:41 -0400 | [diff] [blame] | 54 | #define MPU3050_MIN_POLL_INTERVAL 1 |
| 55 | #define MPU3050_MAX_POLL_INTERVAL 250 |
| Heikki Krogerus | cd314fa | 2011-12-24 00:09:04 -0800 | [diff] [blame] | 56 | #define MPU3050_DEFAULT_POLL_INTERVAL 200 |
| 57 | #define MPU3050_DEFAULT_FS_RANGE 3 |
| 58 | |
| 59 | /* Register map */ |
| 60 | #define MPU3050_CHIP_ID_REG 0x00 |
| 61 | #define MPU3050_SMPLRT_DIV 0x15 |
| 62 | #define MPU3050_DLPF_FS_SYNC 0x16 |
| 63 | #define MPU3050_INT_CFG 0x17 |
| 64 | #define MPU3050_XOUT_H 0x1D |
| 65 | #define MPU3050_PWR_MGM 0x3E |
| 66 | #define MPU3050_PWR_MGM_POS 6 |
| 67 | |
| 68 | /* Register bits */ |
| 69 | |
| 70 | /* DLPF_FS_SYNC */ |
| 71 | #define MPU3050_EXT_SYNC_NONE 0x00 |
| 72 | #define MPU3050_EXT_SYNC_TEMP 0x20 |
| 73 | #define MPU3050_EXT_SYNC_GYROX 0x40 |
| 74 | #define MPU3050_EXT_SYNC_GYROY 0x60 |
| 75 | #define MPU3050_EXT_SYNC_GYROZ 0x80 |
| 76 | #define MPU3050_EXT_SYNC_ACCELX 0xA0 |
| 77 | #define MPU3050_EXT_SYNC_ACCELY 0xC0 |
| 78 | #define MPU3050_EXT_SYNC_ACCELZ 0xE0 |
| 79 | #define MPU3050_EXT_SYNC_MASK 0xE0 |
| 80 | #define MPU3050_FS_250DPS 0x00 |
| 81 | #define MPU3050_FS_500DPS 0x08 |
| 82 | #define MPU3050_FS_1000DPS 0x10 |
| 83 | #define MPU3050_FS_2000DPS 0x18 |
| 84 | #define MPU3050_FS_MASK 0x18 |
| 85 | #define MPU3050_DLPF_CFG_256HZ_NOLPF2 0x00 |
| 86 | #define MPU3050_DLPF_CFG_188HZ 0x01 |
| 87 | #define MPU3050_DLPF_CFG_98HZ 0x02 |
| 88 | #define MPU3050_DLPF_CFG_42HZ 0x03 |
| 89 | #define MPU3050_DLPF_CFG_20HZ 0x04 |
| 90 | #define MPU3050_DLPF_CFG_10HZ 0x05 |
| 91 | #define MPU3050_DLPF_CFG_5HZ 0x06 |
| 92 | #define MPU3050_DLPF_CFG_2100HZ_NOLPF 0x07 |
| 93 | #define MPU3050_DLPF_CFG_MASK 0x07 |
| 94 | /* INT_CFG */ |
| 95 | #define MPU3050_RAW_RDY_EN 0x01 |
| Wentao Xu | c7769c0 | 2012-08-03 15:06:41 -0400 | [diff] [blame] | 96 | #define MPU3050_MPU_RDY_EN 0x04 |
| 97 | #define MPU3050_LATCH_INT_EN 0x20 |
| 98 | #define MPU3050_OPEN_DRAIN 0x40 |
| 99 | #define MPU3050_ACTIVE_LOW 0x80 |
| Heikki Krogerus | cd314fa | 2011-12-24 00:09:04 -0800 | [diff] [blame] | 100 | /* PWR_MGM */ |
| 101 | #define MPU3050_PWR_MGM_PLL_X 0x01 |
| 102 | #define MPU3050_PWR_MGM_PLL_Y 0x02 |
| 103 | #define MPU3050_PWR_MGM_PLL_Z 0x03 |
| 104 | #define MPU3050_PWR_MGM_CLKSEL 0x07 |
| 105 | #define MPU3050_PWR_MGM_STBY_ZG 0x08 |
| 106 | #define MPU3050_PWR_MGM_STBY_YG 0x10 |
| 107 | #define MPU3050_PWR_MGM_STBY_XG 0x20 |
| 108 | #define MPU3050_PWR_MGM_SLEEP 0x40 |
| 109 | #define MPU3050_PWR_MGM_RESET 0x80 |
| 110 | #define MPU3050_PWR_MGM_MASK 0x40 |
| 111 | |
| Joseph Lai | 631b16e | 2011-06-27 13:26:53 -0700 | [diff] [blame] | 112 | struct axis_data { |
| 113 | s16 x; |
| 114 | s16 y; |
| 115 | s16 z; |
| 116 | }; |
| 117 | |
| 118 | struct mpu3050_sensor { |
| 119 | struct i2c_client *client; |
| 120 | struct device *dev; |
| 121 | struct input_dev *idev; |
| Wentao Xu | dac9e60 | 2012-06-12 11:52:34 -0400 | [diff] [blame] | 122 | struct mpu3050_gyro_platform_data *platform_data; |
| 123 | struct delayed_work input_work; |
| 124 | u32 use_poll; |
| Wentao Xu | c7769c0 | 2012-08-03 15:06:41 -0400 | [diff] [blame] | 125 | u32 poll_interval; |
| Wentao Xu | ee6f649 | 2012-08-15 19:41:43 -0400 | [diff] [blame] | 126 | u32 dlpf_index; |
| Joseph Lai | 631b16e | 2011-06-27 13:26:53 -0700 | [diff] [blame] | 127 | }; |
| 128 | |
| Wentao Xu | dac9e60 | 2012-06-12 11:52:34 -0400 | [diff] [blame] | 129 | struct sensor_regulator { |
| 130 | struct regulator *vreg; |
| 131 | const char *name; |
| 132 | u32 min_uV; |
| 133 | u32 max_uV; |
| 134 | }; |
| 135 | |
| 136 | struct sensor_regulator mpu_vreg[] = { |
| 137 | {NULL, "vdd", 2100000, 3600000}, |
| 138 | {NULL, "vlogic", 1800000, 1800000}, |
| 139 | }; |
| 140 | |
| Wentao Xu | ee6f649 | 2012-08-15 19:41:43 -0400 | [diff] [blame] | 141 | struct dlpf_cfg_tb { |
| 142 | u8 cfg; /* cfg index */ |
| 143 | u32 lpf_bw; /* low pass filter bandwidth in Hz */ |
| 144 | u32 sample_rate; /* analog sample rate in Khz, 1 or 8 */ |
| 145 | }; |
| 146 | |
| 147 | static struct dlpf_cfg_tb dlpf_table[] = { |
| 148 | {6, 5, 1}, |
| 149 | {5, 10, 1}, |
| 150 | {4, 20, 1}, |
| 151 | {3, 42, 1}, |
| 152 | {2, 98, 1}, |
| 153 | {1, 188, 1}, |
| 154 | {0, 256, 8}, |
| 155 | }; |
| 156 | |
| 157 | static u8 interval_to_dlpf_cfg(u32 interval) |
| 158 | { |
| 159 | u32 sample_rate = 1000 / interval; |
| 160 | u32 i; |
| 161 | |
| 162 | /* the filter bandwidth needs to be greater or |
| 163 | * equal to half of the sample rate |
| 164 | */ |
| 165 | for (i = 0; i < sizeof(dlpf_table)/sizeof(dlpf_table[0]); i++) { |
| 166 | if (dlpf_table[i].lpf_bw * 2 >= sample_rate) |
| 167 | return i; |
| 168 | } |
| 169 | |
| 170 | /* return the maximum possible */ |
| 171 | return --i; |
| 172 | } |
| 173 | |
| Wentao Xu | dac9e60 | 2012-06-12 11:52:34 -0400 | [diff] [blame] | 174 | static int mpu3050_config_regulator(struct i2c_client *client, bool on) |
| 175 | { |
| 176 | int rc = 0, i; |
| 177 | int num_reg = sizeof(mpu_vreg) / sizeof(struct sensor_regulator); |
| 178 | |
| 179 | if (on) { |
| 180 | for (i = 0; i < num_reg; i++) { |
| 181 | mpu_vreg[i].vreg = regulator_get(&client->dev, |
| 182 | mpu_vreg[i].name); |
| 183 | if (IS_ERR(mpu_vreg[i].vreg)) { |
| 184 | rc = PTR_ERR(mpu_vreg[i].vreg); |
| 185 | pr_err("%s:regulator get failed rc=%d\n", |
| 186 | __func__, rc); |
| 187 | goto error_vdd; |
| 188 | } |
| 189 | |
| 190 | if (regulator_count_voltages(mpu_vreg[i].vreg) > 0) { |
| 191 | rc = regulator_set_voltage(mpu_vreg[i].vreg, |
| 192 | mpu_vreg[i].min_uV, mpu_vreg[i].max_uV); |
| 193 | if (rc) { |
| 194 | pr_err("%s:set_voltage failed rc=%d\n", |
| 195 | __func__, rc); |
| 196 | regulator_put(mpu_vreg[i].vreg); |
| 197 | goto error_vdd; |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | rc = regulator_enable(mpu_vreg[i].vreg); |
| 202 | if (rc) { |
| 203 | pr_err("%s: regulator_enable failed rc =%d\n", |
| 204 | __func__, |
| 205 | rc); |
| 206 | |
| 207 | if (regulator_count_voltages( |
| 208 | mpu_vreg[i].vreg) > 0) { |
| 209 | regulator_set_voltage(mpu_vreg[i].vreg, |
| 210 | 0, mpu_vreg[i].max_uV); |
| 211 | } |
| 212 | regulator_put(mpu_vreg[i].vreg); |
| 213 | goto error_vdd; |
| 214 | } |
| 215 | } |
| 216 | return rc; |
| 217 | } else { |
| 218 | i = num_reg; |
| 219 | } |
| 220 | error_vdd: |
| 221 | while (--i >= 0) { |
| 222 | if (regulator_count_voltages(mpu_vreg[i].vreg) > 0) { |
| 223 | regulator_set_voltage(mpu_vreg[i].vreg, 0, |
| 224 | mpu_vreg[i].max_uV); |
| 225 | } |
| 226 | regulator_disable(mpu_vreg[i].vreg); |
| 227 | regulator_put(mpu_vreg[i].vreg); |
| 228 | } |
| 229 | return rc; |
| 230 | } |
| 231 | |
| Joseph Lai | 631b16e | 2011-06-27 13:26:53 -0700 | [diff] [blame] | 232 | /** |
| Wentao Xu | c7769c0 | 2012-08-03 15:06:41 -0400 | [diff] [blame] | 233 | * mpu3050_attr_get_polling_rate - get the sampling rate |
| 234 | */ |
| 235 | static ssize_t mpu3050_attr_get_polling_rate(struct device *dev, |
| 236 | struct device_attribute *attr, |
| 237 | char *buf) |
| 238 | { |
| 239 | int val; |
| 240 | struct mpu3050_sensor *sensor = dev_get_drvdata(dev); |
| 241 | val = sensor ? sensor->poll_interval : 0; |
| 242 | return snprintf(buf, 8, "%d\n", val); |
| 243 | } |
| 244 | |
| 245 | /** |
| 246 | * mpu3050_attr_set_polling_rate - set the sampling rate |
| 247 | */ |
| 248 | static ssize_t mpu3050_attr_set_polling_rate(struct device *dev, |
| 249 | struct device_attribute *attr, |
| 250 | const char *buf, size_t size) |
| 251 | { |
| 252 | struct mpu3050_sensor *sensor = dev_get_drvdata(dev); |
| 253 | unsigned long interval_ms; |
| Wentao Xu | ee6f649 | 2012-08-15 19:41:43 -0400 | [diff] [blame] | 254 | unsigned int dlpf_index; |
| 255 | u8 divider, reg; |
| 256 | int ret; |
| Wentao Xu | c7769c0 | 2012-08-03 15:06:41 -0400 | [diff] [blame] | 257 | |
| 258 | if (kstrtoul(buf, 10, &interval_ms)) |
| 259 | return -EINVAL; |
| 260 | if ((interval_ms < MPU3050_MIN_POLL_INTERVAL) || |
| 261 | (interval_ms > MPU3050_MAX_POLL_INTERVAL)) |
| 262 | return -EINVAL; |
| 263 | |
| Wentao Xu | ee6f649 | 2012-08-15 19:41:43 -0400 | [diff] [blame] | 264 | dlpf_index = interval_to_dlpf_cfg(interval_ms); |
| 265 | divider = interval_ms * dlpf_table[dlpf_index].sample_rate - 1; |
| Wentao Xu | c7769c0 | 2012-08-03 15:06:41 -0400 | [diff] [blame] | 266 | |
| Wentao Xu | ee6f649 | 2012-08-15 19:41:43 -0400 | [diff] [blame] | 267 | if (sensor->dlpf_index != dlpf_index) { |
| 268 | /* Set low pass filter and full scale */ |
| 269 | reg = dlpf_table[dlpf_index].cfg; |
| 270 | reg |= MPU3050_DEFAULT_FS_RANGE << 3; |
| 271 | reg |= MPU3050_EXT_SYNC_NONE << 5; |
| 272 | ret = i2c_smbus_write_byte_data(sensor->client, |
| 273 | MPU3050_DLPF_FS_SYNC, reg); |
| 274 | if (ret == 0) |
| 275 | sensor->dlpf_index = dlpf_index; |
| 276 | } |
| 277 | |
| 278 | if (sensor->poll_interval != interval_ms) { |
| 279 | /* Output frequency divider. The poll interval */ |
| 280 | ret = i2c_smbus_write_byte_data(sensor->client, |
| 281 | MPU3050_SMPLRT_DIV, divider); |
| 282 | if (ret == 0) |
| 283 | sensor->poll_interval = interval_ms; |
| 284 | } |
| Wentao Xu | c7769c0 | 2012-08-03 15:06:41 -0400 | [diff] [blame] | 285 | |
| 286 | return size; |
| 287 | } |
| 288 | |
| 289 | static struct device_attribute attributes[] = { |
| 290 | |
| Wentao Xu | 82e6305 | 2012-11-15 16:30:15 -0500 | [diff] [blame^] | 291 | __ATTR(pollrate_ms, 0664, |
| Wentao Xu | c7769c0 | 2012-08-03 15:06:41 -0400 | [diff] [blame] | 292 | mpu3050_attr_get_polling_rate, |
| 293 | mpu3050_attr_set_polling_rate), |
| 294 | }; |
| 295 | |
| 296 | static int create_sysfs_interfaces(struct device *dev) |
| 297 | { |
| 298 | int i; |
| 299 | int err; |
| 300 | for (i = 0; i < ARRAY_SIZE(attributes); i++) { |
| 301 | err = device_create_file(dev, attributes + i); |
| 302 | if (err) |
| 303 | goto error; |
| 304 | } |
| 305 | return 0; |
| 306 | |
| 307 | error: |
| 308 | for ( ; i >= 0; i--) |
| 309 | device_remove_file(dev, attributes + i); |
| 310 | dev_err(dev, "%s:Unable to create interface\n", __func__); |
| 311 | return err; |
| 312 | } |
| 313 | |
| 314 | static int remove_sysfs_interfaces(struct device *dev) |
| 315 | { |
| 316 | int i; |
| 317 | for (i = 0; i < ARRAY_SIZE(attributes); i++) |
| 318 | device_remove_file(dev, attributes + i); |
| 319 | return 0; |
| 320 | } |
| 321 | |
| 322 | /** |
| Joseph Lai | 631b16e | 2011-06-27 13:26:53 -0700 | [diff] [blame] | 323 | * mpu3050_xyz_read_reg - read the axes values |
| 324 | * @buffer: provide register addr and get register |
| 325 | * @length: length of register |
| 326 | * |
| 327 | * Reads the register values in one transaction or returns a negative |
| 328 | * error code on failure. |
| 329 | */ |
| 330 | static int mpu3050_xyz_read_reg(struct i2c_client *client, |
| 331 | u8 *buffer, int length) |
| 332 | { |
| 333 | /* |
| 334 | * Annoying we can't make this const because the i2c layer doesn't |
| 335 | * declare input buffers const. |
| 336 | */ |
| 337 | char cmd = MPU3050_XOUT_H; |
| 338 | struct i2c_msg msg[] = { |
| 339 | { |
| 340 | .addr = client->addr, |
| 341 | .flags = 0, |
| 342 | .len = 1, |
| 343 | .buf = &cmd, |
| 344 | }, |
| 345 | { |
| 346 | .addr = client->addr, |
| 347 | .flags = I2C_M_RD, |
| 348 | .len = length, |
| 349 | .buf = buffer, |
| 350 | }, |
| 351 | }; |
| 352 | |
| 353 | return i2c_transfer(client->adapter, msg, 2); |
| 354 | } |
| 355 | |
| 356 | /** |
| 357 | * mpu3050_read_xyz - get co-ordinates from device |
| 358 | * @client: i2c address of sensor |
| 359 | * @coords: co-ordinates to update |
| 360 | * |
| 361 | * Return the converted X Y and Z co-ordinates from the sensor device |
| 362 | */ |
| 363 | static void mpu3050_read_xyz(struct i2c_client *client, |
| 364 | struct axis_data *coords) |
| 365 | { |
| 366 | u16 buffer[3]; |
| 367 | |
| 368 | mpu3050_xyz_read_reg(client, (u8 *)buffer, 6); |
| 369 | coords->x = be16_to_cpu(buffer[0]); |
| 370 | coords->y = be16_to_cpu(buffer[1]); |
| 371 | coords->z = be16_to_cpu(buffer[2]); |
| 372 | dev_dbg(&client->dev, "%s: x %d, y %d, z %d\n", __func__, |
| 373 | coords->x, coords->y, coords->z); |
| 374 | } |
| 375 | |
| 376 | /** |
| 377 | * mpu3050_set_power_mode - set the power mode |
| 378 | * @client: i2c client for the sensor |
| 379 | * @val: value to switch on/off of power, 1: normal power, 0: low power |
| 380 | * |
| 381 | * Put device to normal-power mode or low-power mode. |
| 382 | */ |
| 383 | static void mpu3050_set_power_mode(struct i2c_client *client, u8 val) |
| 384 | { |
| 385 | u8 value; |
| 386 | |
| Wentao Xu | dac9e60 | 2012-06-12 11:52:34 -0400 | [diff] [blame] | 387 | if (val) { |
| 388 | mpu3050_config_regulator(client, 1); |
| 389 | udelay(10); |
| 390 | } |
| 391 | |
| Joseph Lai | 631b16e | 2011-06-27 13:26:53 -0700 | [diff] [blame] | 392 | value = i2c_smbus_read_byte_data(client, MPU3050_PWR_MGM); |
| 393 | value = (value & ~MPU3050_PWR_MGM_MASK) | |
| 394 | (((val << MPU3050_PWR_MGM_POS) & MPU3050_PWR_MGM_MASK) ^ |
| 395 | MPU3050_PWR_MGM_MASK); |
| 396 | i2c_smbus_write_byte_data(client, MPU3050_PWR_MGM, value); |
| Wentao Xu | dac9e60 | 2012-06-12 11:52:34 -0400 | [diff] [blame] | 397 | |
| 398 | if (!val) { |
| 399 | udelay(10); |
| 400 | mpu3050_config_regulator(client, 0); |
| 401 | } |
| Joseph Lai | 631b16e | 2011-06-27 13:26:53 -0700 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | /** |
| 405 | * mpu3050_input_open - called on input event open |
| 406 | * @input: input dev of opened device |
| 407 | * |
| 408 | * The input layer calls this function when input event is opened. The |
| 409 | * function will push the device to resume. Then, the device is ready |
| 410 | * to provide data. |
| 411 | */ |
| 412 | static int mpu3050_input_open(struct input_dev *input) |
| 413 | { |
| 414 | struct mpu3050_sensor *sensor = input_get_drvdata(input); |
| Heikki Krogerus | 3b51872 | 2011-12-23 23:57:09 -0800 | [diff] [blame] | 415 | int error; |
| Joseph Lai | 631b16e | 2011-06-27 13:26:53 -0700 | [diff] [blame] | 416 | |
| Wentao Xu | c7769c0 | 2012-08-03 15:06:41 -0400 | [diff] [blame] | 417 | pm_runtime_get_sync(sensor->dev); |
| Joseph Lai | 631b16e | 2011-06-27 13:26:53 -0700 | [diff] [blame] | 418 | |
| Heikki Krogerus | 3b51872 | 2011-12-23 23:57:09 -0800 | [diff] [blame] | 419 | /* Enable interrupts */ |
| 420 | error = i2c_smbus_write_byte_data(sensor->client, MPU3050_INT_CFG, |
| Wentao Xu | c7769c0 | 2012-08-03 15:06:41 -0400 | [diff] [blame] | 421 | MPU3050_ACTIVE_LOW | |
| 422 | MPU3050_OPEN_DRAIN | |
| 423 | MPU3050_RAW_RDY_EN); |
| Heikki Krogerus | 3b51872 | 2011-12-23 23:57:09 -0800 | [diff] [blame] | 424 | if (error < 0) { |
| 425 | pm_runtime_put(sensor->dev); |
| 426 | return error; |
| 427 | } |
| Wentao Xu | dac9e60 | 2012-06-12 11:52:34 -0400 | [diff] [blame] | 428 | if (sensor->use_poll) |
| 429 | schedule_delayed_work(&sensor->input_work, |
| Wentao Xu | c7769c0 | 2012-08-03 15:06:41 -0400 | [diff] [blame] | 430 | msecs_to_jiffies(sensor->poll_interval)); |
| Heikki Krogerus | 3b51872 | 2011-12-23 23:57:09 -0800 | [diff] [blame] | 431 | |
| Joseph Lai | 631b16e | 2011-06-27 13:26:53 -0700 | [diff] [blame] | 432 | return 0; |
| 433 | } |
| 434 | |
| 435 | /** |
| 436 | * mpu3050_input_close - called on input event close |
| 437 | * @input: input dev of closed device |
| 438 | * |
| 439 | * The input layer calls this function when input event is closed. The |
| 440 | * function will push the device to suspend. |
| 441 | */ |
| 442 | static void mpu3050_input_close(struct input_dev *input) |
| 443 | { |
| 444 | struct mpu3050_sensor *sensor = input_get_drvdata(input); |
| 445 | |
| Wentao Xu | dac9e60 | 2012-06-12 11:52:34 -0400 | [diff] [blame] | 446 | if (sensor->use_poll) |
| 447 | cancel_delayed_work_sync(&sensor->input_work); |
| 448 | |
| Joseph Lai | 631b16e | 2011-06-27 13:26:53 -0700 | [diff] [blame] | 449 | pm_runtime_put(sensor->dev); |
| 450 | } |
| 451 | |
| 452 | /** |
| 453 | * mpu3050_interrupt_thread - handle an IRQ |
| 454 | * @irq: interrupt numner |
| 455 | * @data: the sensor |
| 456 | * |
| 457 | * Called by the kernel single threaded after an interrupt occurs. Read |
| 458 | * the sensor data and generate an input event for it. |
| 459 | */ |
| 460 | static irqreturn_t mpu3050_interrupt_thread(int irq, void *data) |
| 461 | { |
| 462 | struct mpu3050_sensor *sensor = data; |
| 463 | struct axis_data axis; |
| 464 | |
| 465 | mpu3050_read_xyz(sensor->client, &axis); |
| 466 | |
| 467 | input_report_abs(sensor->idev, ABS_X, axis.x); |
| 468 | input_report_abs(sensor->idev, ABS_Y, axis.y); |
| 469 | input_report_abs(sensor->idev, ABS_Z, axis.z); |
| 470 | input_sync(sensor->idev); |
| 471 | |
| 472 | return IRQ_HANDLED; |
| 473 | } |
| 474 | |
| 475 | /** |
| Wentao Xu | dac9e60 | 2012-06-12 11:52:34 -0400 | [diff] [blame] | 476 | * mpu3050_input_work_fn - polling work |
| 477 | * @work: the work struct |
| 478 | * |
| 479 | * Called by the work queue; read sensor data and generate an input |
| 480 | * event |
| 481 | */ |
| 482 | static void mpu3050_input_work_fn(struct work_struct *work) |
| 483 | { |
| 484 | struct mpu3050_sensor *sensor; |
| 485 | struct axis_data axis; |
| 486 | |
| 487 | sensor = container_of((struct delayed_work *)work, |
| 488 | struct mpu3050_sensor, input_work); |
| 489 | |
| 490 | mpu3050_read_xyz(sensor->client, &axis); |
| 491 | |
| 492 | input_report_abs(sensor->idev, ABS_X, axis.x); |
| 493 | input_report_abs(sensor->idev, ABS_Y, axis.y); |
| 494 | input_report_abs(sensor->idev, ABS_Z, axis.z); |
| 495 | input_sync(sensor->idev); |
| 496 | |
| 497 | if (sensor->use_poll) |
| 498 | schedule_delayed_work(&sensor->input_work, |
| Wentao Xu | c7769c0 | 2012-08-03 15:06:41 -0400 | [diff] [blame] | 499 | msecs_to_jiffies(sensor->poll_interval)); |
| Wentao Xu | dac9e60 | 2012-06-12 11:52:34 -0400 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | /** |
| Heikki Krogerus | cd314fa | 2011-12-24 00:09:04 -0800 | [diff] [blame] | 503 | * mpu3050_hw_init - initialize hardware |
| 504 | * @sensor: the sensor |
| 505 | * |
| 506 | * Called during device probe; configures the sampling method. |
| 507 | */ |
| 508 | static int __devinit mpu3050_hw_init(struct mpu3050_sensor *sensor) |
| 509 | { |
| 510 | struct i2c_client *client = sensor->client; |
| 511 | int ret; |
| 512 | u8 reg; |
| 513 | |
| 514 | /* Reset */ |
| 515 | ret = i2c_smbus_write_byte_data(client, MPU3050_PWR_MGM, |
| 516 | MPU3050_PWR_MGM_RESET); |
| 517 | if (ret < 0) |
| 518 | return ret; |
| 519 | |
| 520 | ret = i2c_smbus_read_byte_data(client, MPU3050_PWR_MGM); |
| 521 | if (ret < 0) |
| 522 | return ret; |
| 523 | |
| 524 | ret &= ~MPU3050_PWR_MGM_CLKSEL; |
| 525 | ret |= MPU3050_PWR_MGM_PLL_Z; |
| 526 | ret = i2c_smbus_write_byte_data(client, MPU3050_PWR_MGM, ret); |
| 527 | if (ret < 0) |
| 528 | return ret; |
| 529 | |
| 530 | /* Output frequency divider. The poll interval */ |
| 531 | ret = i2c_smbus_write_byte_data(client, MPU3050_SMPLRT_DIV, |
| Wentao Xu | c7769c0 | 2012-08-03 15:06:41 -0400 | [diff] [blame] | 532 | sensor->poll_interval - 1); |
| Heikki Krogerus | cd314fa | 2011-12-24 00:09:04 -0800 | [diff] [blame] | 533 | if (ret < 0) |
| 534 | return ret; |
| 535 | |
| 536 | /* Set low pass filter and full scale */ |
| Wentao Xu | ee6f649 | 2012-08-15 19:41:43 -0400 | [diff] [blame] | 537 | reg = MPU3050_DLPF_CFG_42HZ; |
| 538 | reg |= MPU3050_DEFAULT_FS_RANGE << 3; |
| Heikki Krogerus | cd314fa | 2011-12-24 00:09:04 -0800 | [diff] [blame] | 539 | reg |= MPU3050_EXT_SYNC_NONE << 5; |
| 540 | ret = i2c_smbus_write_byte_data(client, MPU3050_DLPF_FS_SYNC, reg); |
| 541 | if (ret < 0) |
| 542 | return ret; |
| 543 | |
| 544 | return 0; |
| 545 | } |
| 546 | |
| 547 | /** |
| Joseph Lai | 631b16e | 2011-06-27 13:26:53 -0700 | [diff] [blame] | 548 | * mpu3050_probe - device detection callback |
| 549 | * @client: i2c client of found device |
| 550 | * @id: id match information |
| 551 | * |
| 552 | * The I2C layer calls us when it believes a sensor is present at this |
| 553 | * address. Probe to see if this is correct and to validate the device. |
| 554 | * |
| 555 | * If present install the relevant sysfs interfaces and input device. |
| 556 | */ |
| 557 | static int __devinit mpu3050_probe(struct i2c_client *client, |
| 558 | const struct i2c_device_id *id) |
| 559 | { |
| 560 | struct mpu3050_sensor *sensor; |
| 561 | struct input_dev *idev; |
| 562 | int ret; |
| 563 | int error; |
| 564 | |
| 565 | sensor = kzalloc(sizeof(struct mpu3050_sensor), GFP_KERNEL); |
| 566 | idev = input_allocate_device(); |
| 567 | if (!sensor || !idev) { |
| 568 | dev_err(&client->dev, "failed to allocate driver data\n"); |
| 569 | error = -ENOMEM; |
| 570 | goto err_free_mem; |
| 571 | } |
| 572 | |
| 573 | sensor->client = client; |
| 574 | sensor->dev = &client->dev; |
| 575 | sensor->idev = idev; |
| Wentao Xu | dac9e60 | 2012-06-12 11:52:34 -0400 | [diff] [blame] | 576 | sensor->platform_data = client->dev.platform_data; |
| Wentao Xu | c7769c0 | 2012-08-03 15:06:41 -0400 | [diff] [blame] | 577 | i2c_set_clientdata(client, sensor); |
| 578 | if (sensor->platform_data) { |
| 579 | u32 interval = sensor->platform_data->poll_interval; |
| 580 | |
| 581 | if ((interval < MPU3050_MIN_POLL_INTERVAL) || |
| 582 | (interval > MPU3050_MAX_POLL_INTERVAL)) |
| 583 | sensor->poll_interval = MPU3050_DEFAULT_POLL_INTERVAL; |
| 584 | else |
| 585 | sensor->poll_interval = interval; |
| 586 | } else { |
| 587 | sensor->poll_interval = MPU3050_DEFAULT_POLL_INTERVAL; |
| 588 | } |
| Joseph Lai | 631b16e | 2011-06-27 13:26:53 -0700 | [diff] [blame] | 589 | |
| 590 | mpu3050_set_power_mode(client, 1); |
| 591 | msleep(10); |
| 592 | |
| 593 | ret = i2c_smbus_read_byte_data(client, MPU3050_CHIP_ID_REG); |
| 594 | if (ret < 0) { |
| 595 | dev_err(&client->dev, "failed to detect device\n"); |
| 596 | error = -ENXIO; |
| 597 | goto err_free_mem; |
| 598 | } |
| 599 | |
| 600 | if (ret != MPU3050_CHIP_ID) { |
| 601 | dev_err(&client->dev, "unsupported chip id\n"); |
| 602 | error = -ENXIO; |
| 603 | goto err_free_mem; |
| 604 | } |
| 605 | |
| 606 | idev->name = "MPU3050"; |
| 607 | idev->id.bustype = BUS_I2C; |
| 608 | idev->dev.parent = &client->dev; |
| 609 | |
| 610 | idev->open = mpu3050_input_open; |
| 611 | idev->close = mpu3050_input_close; |
| 612 | |
| 613 | __set_bit(EV_ABS, idev->evbit); |
| 614 | input_set_abs_params(idev, ABS_X, |
| 615 | MPU3050_MIN_VALUE, MPU3050_MAX_VALUE, 0, 0); |
| 616 | input_set_abs_params(idev, ABS_Y, |
| 617 | MPU3050_MIN_VALUE, MPU3050_MAX_VALUE, 0, 0); |
| 618 | input_set_abs_params(idev, ABS_Z, |
| 619 | MPU3050_MIN_VALUE, MPU3050_MAX_VALUE, 0, 0); |
| 620 | |
| 621 | input_set_drvdata(idev, sensor); |
| 622 | |
| 623 | pm_runtime_set_active(&client->dev); |
| 624 | |
| Heikki Krogerus | cd314fa | 2011-12-24 00:09:04 -0800 | [diff] [blame] | 625 | error = mpu3050_hw_init(sensor); |
| 626 | if (error) |
| 627 | goto err_pm_set_suspended; |
| 628 | |
| Wentao Xu | dac9e60 | 2012-06-12 11:52:34 -0400 | [diff] [blame] | 629 | if (client->irq == 0) { |
| Wentao Xu | dac9e60 | 2012-06-12 11:52:34 -0400 | [diff] [blame] | 630 | sensor->use_poll = 1; |
| Wentao Xu | c7769c0 | 2012-08-03 15:06:41 -0400 | [diff] [blame] | 631 | INIT_DELAYED_WORK(&sensor->input_work, mpu3050_input_work_fn); |
| Wentao Xu | dac9e60 | 2012-06-12 11:52:34 -0400 | [diff] [blame] | 632 | } else { |
| 633 | sensor->use_poll = 0; |
| 634 | |
| Wentao Xu | c7769c0 | 2012-08-03 15:06:41 -0400 | [diff] [blame] | 635 | if (gpio_is_valid(sensor->platform_data->gpio_int)) { |
| 636 | /* configure interrupt gpio */ |
| 637 | ret = gpio_request(sensor->platform_data->gpio_int, |
| 638 | "gyro_gpio_int"); |
| 639 | if (ret) { |
| 640 | pr_err("%s: unable to request interrupt gpio %d\n", |
| 641 | __func__, |
| 642 | sensor->platform_data->gpio_int); |
| 643 | goto err_pm_set_suspended; |
| 644 | } |
| 645 | |
| 646 | ret = gpio_direction_input( |
| 647 | sensor->platform_data->gpio_int); |
| 648 | if (ret) { |
| 649 | pr_err("%s: unable to set direction for gpio %d\n", |
| 650 | __func__, sensor->platform_data->gpio_int); |
| 651 | goto err_free_gpio; |
| 652 | } |
| 653 | } |
| 654 | |
| Wentao Xu | dac9e60 | 2012-06-12 11:52:34 -0400 | [diff] [blame] | 655 | error = request_threaded_irq(client->irq, |
| Joseph Lai | 631b16e | 2011-06-27 13:26:53 -0700 | [diff] [blame] | 656 | NULL, mpu3050_interrupt_thread, |
| Wentao Xu | c7769c0 | 2012-08-03 15:06:41 -0400 | [diff] [blame] | 657 | IRQF_TRIGGER_FALLING, |
| Heikki Krogerus | 3b51872 | 2011-12-23 23:57:09 -0800 | [diff] [blame] | 658 | "mpu3050", sensor); |
| Wentao Xu | dac9e60 | 2012-06-12 11:52:34 -0400 | [diff] [blame] | 659 | if (error) { |
| 660 | dev_err(&client->dev, |
| 661 | "can't get IRQ %d, error %d\n", |
| 662 | client->irq, error); |
| 663 | goto err_pm_set_suspended; |
| 664 | } |
| Joseph Lai | 631b16e | 2011-06-27 13:26:53 -0700 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | error = input_register_device(idev); |
| 668 | if (error) { |
| 669 | dev_err(&client->dev, "failed to register input device\n"); |
| 670 | goto err_free_irq; |
| 671 | } |
| 672 | |
| Wentao Xu | c7769c0 | 2012-08-03 15:06:41 -0400 | [diff] [blame] | 673 | error = create_sysfs_interfaces(&client->dev); |
| 674 | if (error < 0) { |
| 675 | dev_err(&client->dev, "failed to create sysfs\n"); |
| 676 | goto err_input_cleanup; |
| 677 | } |
| 678 | |
| Joseph Lai | 631b16e | 2011-06-27 13:26:53 -0700 | [diff] [blame] | 679 | pm_runtime_enable(&client->dev); |
| 680 | pm_runtime_set_autosuspend_delay(&client->dev, MPU3050_AUTO_DELAY); |
| 681 | |
| 682 | return 0; |
| 683 | |
| Wentao Xu | c7769c0 | 2012-08-03 15:06:41 -0400 | [diff] [blame] | 684 | err_input_cleanup: |
| 685 | input_unregister_device(idev); |
| Joseph Lai | 631b16e | 2011-06-27 13:26:53 -0700 | [diff] [blame] | 686 | err_free_irq: |
| Wentao Xu | dac9e60 | 2012-06-12 11:52:34 -0400 | [diff] [blame] | 687 | if (client->irq > 0) |
| 688 | free_irq(client->irq, sensor); |
| Wentao Xu | c7769c0 | 2012-08-03 15:06:41 -0400 | [diff] [blame] | 689 | err_free_gpio: |
| 690 | if ((client->irq > 0) && |
| 691 | (gpio_is_valid(sensor->platform_data->gpio_int))) |
| 692 | gpio_free(sensor->platform_data->gpio_int); |
| Joseph Lai | 631b16e | 2011-06-27 13:26:53 -0700 | [diff] [blame] | 693 | err_pm_set_suspended: |
| 694 | pm_runtime_set_suspended(&client->dev); |
| 695 | err_free_mem: |
| Axel Lin | d9b830f | 2011-08-11 09:19:29 -0700 | [diff] [blame] | 696 | input_free_device(idev); |
| Joseph Lai | 631b16e | 2011-06-27 13:26:53 -0700 | [diff] [blame] | 697 | kfree(sensor); |
| 698 | return error; |
| 699 | } |
| 700 | |
| 701 | /** |
| 702 | * mpu3050_remove - remove a sensor |
| 703 | * @client: i2c client of sensor being removed |
| 704 | * |
| 705 | * Our sensor is going away, clean up the resources. |
| 706 | */ |
| 707 | static int __devexit mpu3050_remove(struct i2c_client *client) |
| 708 | { |
| 709 | struct mpu3050_sensor *sensor = i2c_get_clientdata(client); |
| 710 | |
| 711 | pm_runtime_disable(&client->dev); |
| 712 | pm_runtime_set_suspended(&client->dev); |
| 713 | |
| Wentao Xu | c7769c0 | 2012-08-03 15:06:41 -0400 | [diff] [blame] | 714 | if (client->irq) |
| 715 | free_irq(client->irq, sensor); |
| 716 | |
| 717 | remove_sysfs_interfaces(&client->dev); |
| Joseph Lai | 631b16e | 2011-06-27 13:26:53 -0700 | [diff] [blame] | 718 | input_unregister_device(sensor->idev); |
| Wentao Xu | c7769c0 | 2012-08-03 15:06:41 -0400 | [diff] [blame] | 719 | |
| Joseph Lai | 631b16e | 2011-06-27 13:26:53 -0700 | [diff] [blame] | 720 | kfree(sensor); |
| 721 | |
| 722 | return 0; |
| 723 | } |
| 724 | |
| 725 | #ifdef CONFIG_PM |
| 726 | /** |
| 727 | * mpu3050_suspend - called on device suspend |
| 728 | * @dev: device being suspended |
| 729 | * |
| 730 | * Put the device into sleep mode before we suspend the machine. |
| 731 | */ |
| 732 | static int mpu3050_suspend(struct device *dev) |
| 733 | { |
| 734 | struct i2c_client *client = to_i2c_client(dev); |
| 735 | |
| 736 | mpu3050_set_power_mode(client, 0); |
| 737 | |
| 738 | return 0; |
| 739 | } |
| 740 | |
| 741 | /** |
| 742 | * mpu3050_resume - called on device resume |
| 743 | * @dev: device being resumed |
| 744 | * |
| 745 | * Put the device into powered mode on resume. |
| 746 | */ |
| 747 | static int mpu3050_resume(struct device *dev) |
| 748 | { |
| 749 | struct i2c_client *client = to_i2c_client(dev); |
| 750 | |
| 751 | mpu3050_set_power_mode(client, 1); |
| 752 | msleep(100); /* wait for gyro chip resume */ |
| 753 | |
| 754 | return 0; |
| 755 | } |
| 756 | #endif |
| 757 | |
| 758 | static UNIVERSAL_DEV_PM_OPS(mpu3050_pm, mpu3050_suspend, mpu3050_resume, NULL); |
| 759 | |
| 760 | static const struct i2c_device_id mpu3050_ids[] = { |
| 761 | { "mpu3050", 0 }, |
| 762 | { } |
| 763 | }; |
| 764 | MODULE_DEVICE_TABLE(i2c, mpu3050_ids); |
| 765 | |
| Olof Johansson | e948981 | 2011-12-23 01:20:44 -0800 | [diff] [blame] | 766 | static const struct of_device_id mpu3050_of_match[] = { |
| 767 | { .compatible = "invn,mpu3050", }, |
| 768 | { }, |
| 769 | }; |
| 770 | MODULE_DEVICE_TABLE(of, mpu3050_of_match); |
| 771 | |
| Joseph Lai | 631b16e | 2011-06-27 13:26:53 -0700 | [diff] [blame] | 772 | static struct i2c_driver mpu3050_i2c_driver = { |
| 773 | .driver = { |
| 774 | .name = "mpu3050", |
| 775 | .owner = THIS_MODULE, |
| 776 | .pm = &mpu3050_pm, |
| Olof Johansson | e948981 | 2011-12-23 01:20:44 -0800 | [diff] [blame] | 777 | .of_match_table = mpu3050_of_match, |
| Joseph Lai | 631b16e | 2011-06-27 13:26:53 -0700 | [diff] [blame] | 778 | }, |
| 779 | .probe = mpu3050_probe, |
| 780 | .remove = __devexit_p(mpu3050_remove), |
| 781 | .id_table = mpu3050_ids, |
| 782 | }; |
| 783 | |
| Axel Lin | 1b92c1c | 2012-03-16 23:05:41 -0700 | [diff] [blame] | 784 | module_i2c_driver(mpu3050_i2c_driver); |
| Joseph Lai | 631b16e | 2011-06-27 13:26:53 -0700 | [diff] [blame] | 785 | |
| 786 | MODULE_AUTHOR("Wistron Corp."); |
| 787 | MODULE_DESCRIPTION("MPU3050 Tri-axis gyroscope driver"); |
| 788 | MODULE_LICENSE("GPL"); |