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> |
Richard Liu | a1d406a | 2013-04-14 13:46:48 -0700 | [diff] [blame] | 46 | #include <linux/of_gpio.h> |
| 47 | #include <mach/gpiomux.h> |
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 | 04f4cc9 | 2012-08-15 19:41:43 -0400 | [diff] [blame] | 126 | u32 dlpf_index; |
Richard Liu | a1d406a | 2013-04-14 13:46:48 -0700 | [diff] [blame] | 127 | u32 enable_gpio; |
Xiaocheng Li | 05adbd8 | 2013-06-30 17:21:58 -0700 | [diff] [blame] | 128 | u32 enable; |
Joseph Lai | 631b16e | 2011-06-27 13:26:53 -0700 | [diff] [blame] | 129 | }; |
| 130 | |
Wentao Xu | dac9e60 | 2012-06-12 11:52:34 -0400 | [diff] [blame] | 131 | struct sensor_regulator { |
| 132 | struct regulator *vreg; |
| 133 | const char *name; |
| 134 | u32 min_uV; |
| 135 | u32 max_uV; |
| 136 | }; |
| 137 | |
| 138 | struct sensor_regulator mpu_vreg[] = { |
| 139 | {NULL, "vdd", 2100000, 3600000}, |
| 140 | {NULL, "vlogic", 1800000, 1800000}, |
| 141 | }; |
| 142 | |
Richard Liu | a1d406a | 2013-04-14 13:46:48 -0700 | [diff] [blame] | 143 | static const int mpu3050_chip_ids[] = { |
| 144 | 0x68, |
| 145 | 0x69, |
| 146 | }; |
| 147 | |
Wentao Xu | 04f4cc9 | 2012-08-15 19:41:43 -0400 | [diff] [blame] | 148 | struct dlpf_cfg_tb { |
| 149 | u8 cfg; /* cfg index */ |
| 150 | u32 lpf_bw; /* low pass filter bandwidth in Hz */ |
| 151 | u32 sample_rate; /* analog sample rate in Khz, 1 or 8 */ |
| 152 | }; |
| 153 | |
| 154 | static struct dlpf_cfg_tb dlpf_table[] = { |
| 155 | {6, 5, 1}, |
| 156 | {5, 10, 1}, |
| 157 | {4, 20, 1}, |
| 158 | {3, 42, 1}, |
| 159 | {2, 98, 1}, |
| 160 | {1, 188, 1}, |
| 161 | {0, 256, 8}, |
| 162 | }; |
| 163 | |
| 164 | static u8 interval_to_dlpf_cfg(u32 interval) |
| 165 | { |
| 166 | u32 sample_rate = 1000 / interval; |
| 167 | u32 i; |
| 168 | |
| 169 | /* the filter bandwidth needs to be greater or |
| 170 | * equal to half of the sample rate |
| 171 | */ |
| 172 | for (i = 0; i < sizeof(dlpf_table)/sizeof(dlpf_table[0]); i++) { |
| 173 | if (dlpf_table[i].lpf_bw * 2 >= sample_rate) |
| 174 | return i; |
| 175 | } |
| 176 | |
| 177 | /* return the maximum possible */ |
| 178 | return --i; |
| 179 | } |
| 180 | |
Wentao Xu | dac9e60 | 2012-06-12 11:52:34 -0400 | [diff] [blame] | 181 | static int mpu3050_config_regulator(struct i2c_client *client, bool on) |
| 182 | { |
| 183 | int rc = 0, i; |
| 184 | int num_reg = sizeof(mpu_vreg) / sizeof(struct sensor_regulator); |
| 185 | |
| 186 | if (on) { |
| 187 | for (i = 0; i < num_reg; i++) { |
| 188 | mpu_vreg[i].vreg = regulator_get(&client->dev, |
| 189 | mpu_vreg[i].name); |
| 190 | if (IS_ERR(mpu_vreg[i].vreg)) { |
| 191 | rc = PTR_ERR(mpu_vreg[i].vreg); |
| 192 | pr_err("%s:regulator get failed rc=%d\n", |
| 193 | __func__, rc); |
Wentao Xu | bf00598 | 2012-12-12 11:28:21 -0500 | [diff] [blame] | 194 | mpu_vreg[i].vreg = NULL; |
Wentao Xu | dac9e60 | 2012-06-12 11:52:34 -0400 | [diff] [blame] | 195 | goto error_vdd; |
| 196 | } |
| 197 | |
| 198 | if (regulator_count_voltages(mpu_vreg[i].vreg) > 0) { |
| 199 | rc = regulator_set_voltage(mpu_vreg[i].vreg, |
| 200 | mpu_vreg[i].min_uV, mpu_vreg[i].max_uV); |
| 201 | if (rc) { |
| 202 | pr_err("%s:set_voltage failed rc=%d\n", |
| 203 | __func__, rc); |
| 204 | regulator_put(mpu_vreg[i].vreg); |
Wentao Xu | bf00598 | 2012-12-12 11:28:21 -0500 | [diff] [blame] | 205 | mpu_vreg[i].vreg = NULL; |
Wentao Xu | dac9e60 | 2012-06-12 11:52:34 -0400 | [diff] [blame] | 206 | goto error_vdd; |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | rc = regulator_enable(mpu_vreg[i].vreg); |
| 211 | if (rc) { |
| 212 | pr_err("%s: regulator_enable failed rc =%d\n", |
| 213 | __func__, |
| 214 | rc); |
| 215 | |
| 216 | if (regulator_count_voltages( |
| 217 | mpu_vreg[i].vreg) > 0) { |
| 218 | regulator_set_voltage(mpu_vreg[i].vreg, |
| 219 | 0, mpu_vreg[i].max_uV); |
| 220 | } |
| 221 | regulator_put(mpu_vreg[i].vreg); |
Wentao Xu | bf00598 | 2012-12-12 11:28:21 -0500 | [diff] [blame] | 222 | mpu_vreg[i].vreg = NULL; |
Wentao Xu | dac9e60 | 2012-06-12 11:52:34 -0400 | [diff] [blame] | 223 | goto error_vdd; |
| 224 | } |
| 225 | } |
| 226 | return rc; |
| 227 | } else { |
| 228 | i = num_reg; |
| 229 | } |
| 230 | error_vdd: |
| 231 | while (--i >= 0) { |
Wentao Xu | bf00598 | 2012-12-12 11:28:21 -0500 | [diff] [blame] | 232 | if (!IS_ERR_OR_NULL(mpu_vreg[i].vreg)) { |
| 233 | if (regulator_count_voltages( |
| 234 | mpu_vreg[i].vreg) > 0) { |
| 235 | regulator_set_voltage(mpu_vreg[i].vreg, 0, |
Wentao Xu | dac9e60 | 2012-06-12 11:52:34 -0400 | [diff] [blame] | 236 | mpu_vreg[i].max_uV); |
Wentao Xu | bf00598 | 2012-12-12 11:28:21 -0500 | [diff] [blame] | 237 | } |
| 238 | regulator_disable(mpu_vreg[i].vreg); |
| 239 | regulator_put(mpu_vreg[i].vreg); |
| 240 | mpu_vreg[i].vreg = NULL; |
Wentao Xu | dac9e60 | 2012-06-12 11:52:34 -0400 | [diff] [blame] | 241 | } |
Wentao Xu | dac9e60 | 2012-06-12 11:52:34 -0400 | [diff] [blame] | 242 | } |
| 243 | return rc; |
| 244 | } |
| 245 | |
Joseph Lai | 631b16e | 2011-06-27 13:26:53 -0700 | [diff] [blame] | 246 | /** |
Wentao Xu | c7769c0 | 2012-08-03 15:06:41 -0400 | [diff] [blame] | 247 | * mpu3050_attr_get_polling_rate - get the sampling rate |
| 248 | */ |
| 249 | static ssize_t mpu3050_attr_get_polling_rate(struct device *dev, |
| 250 | struct device_attribute *attr, |
| 251 | char *buf) |
| 252 | { |
| 253 | int val; |
| 254 | struct mpu3050_sensor *sensor = dev_get_drvdata(dev); |
| 255 | val = sensor ? sensor->poll_interval : 0; |
| 256 | return snprintf(buf, 8, "%d\n", val); |
| 257 | } |
| 258 | |
| 259 | /** |
| 260 | * mpu3050_attr_set_polling_rate - set the sampling rate |
| 261 | */ |
| 262 | static ssize_t mpu3050_attr_set_polling_rate(struct device *dev, |
| 263 | struct device_attribute *attr, |
| 264 | const char *buf, size_t size) |
| 265 | { |
| 266 | struct mpu3050_sensor *sensor = dev_get_drvdata(dev); |
| 267 | unsigned long interval_ms; |
Wentao Xu | 04f4cc9 | 2012-08-15 19:41:43 -0400 | [diff] [blame] | 268 | unsigned int dlpf_index; |
| 269 | u8 divider, reg; |
| 270 | int ret; |
Wentao Xu | c7769c0 | 2012-08-03 15:06:41 -0400 | [diff] [blame] | 271 | |
| 272 | if (kstrtoul(buf, 10, &interval_ms)) |
| 273 | return -EINVAL; |
| 274 | if ((interval_ms < MPU3050_MIN_POLL_INTERVAL) || |
| 275 | (interval_ms > MPU3050_MAX_POLL_INTERVAL)) |
| 276 | return -EINVAL; |
| 277 | |
Wentao Xu | 04f4cc9 | 2012-08-15 19:41:43 -0400 | [diff] [blame] | 278 | dlpf_index = interval_to_dlpf_cfg(interval_ms); |
| 279 | divider = interval_ms * dlpf_table[dlpf_index].sample_rate - 1; |
Wentao Xu | c7769c0 | 2012-08-03 15:06:41 -0400 | [diff] [blame] | 280 | |
Wentao Xu | 04f4cc9 | 2012-08-15 19:41:43 -0400 | [diff] [blame] | 281 | if (sensor->dlpf_index != dlpf_index) { |
| 282 | /* Set low pass filter and full scale */ |
| 283 | reg = dlpf_table[dlpf_index].cfg; |
| 284 | reg |= MPU3050_DEFAULT_FS_RANGE << 3; |
| 285 | reg |= MPU3050_EXT_SYNC_NONE << 5; |
| 286 | ret = i2c_smbus_write_byte_data(sensor->client, |
| 287 | MPU3050_DLPF_FS_SYNC, reg); |
| 288 | if (ret == 0) |
| 289 | sensor->dlpf_index = dlpf_index; |
| 290 | } |
| 291 | |
| 292 | if (sensor->poll_interval != interval_ms) { |
| 293 | /* Output frequency divider. The poll interval */ |
| 294 | ret = i2c_smbus_write_byte_data(sensor->client, |
| 295 | MPU3050_SMPLRT_DIV, divider); |
| 296 | if (ret == 0) |
| 297 | sensor->poll_interval = interval_ms; |
| 298 | } |
Wentao Xu | c7769c0 | 2012-08-03 15:06:41 -0400 | [diff] [blame] | 299 | |
| 300 | return size; |
| 301 | } |
| 302 | |
Xiaocheng Li | 05adbd8 | 2013-06-30 17:21:58 -0700 | [diff] [blame] | 303 | /** |
| 304 | * Set/get enable function is just needed by sensor HAL. |
Xiaocheng Li | 05adbd8 | 2013-06-30 17:21:58 -0700 | [diff] [blame] | 305 | */ |
| 306 | |
| 307 | static ssize_t mpu3050_attr_set_enable(struct device *dev, |
| 308 | struct device_attribute *attr, |
| 309 | const char *buf, size_t count) |
| 310 | { |
| 311 | struct mpu3050_sensor *sensor = dev_get_drvdata(dev); |
| 312 | unsigned long val; |
| 313 | |
| 314 | if (kstrtoul(buf, 10, &val)) |
| 315 | return -EINVAL; |
Xiaocheng Li | b4cc7df | 2013-07-17 11:46:40 -0700 | [diff] [blame] | 316 | sensor->enable = (u32)val == 0 ? 0 : 1; |
| 317 | if (sensor->enable) { |
| 318 | pm_runtime_get_sync(sensor->dev); |
| 319 | gpio_set_value(sensor->enable_gpio, 1); |
| 320 | if (sensor->use_poll) |
| 321 | schedule_delayed_work(&sensor->input_work, |
| 322 | msecs_to_jiffies(sensor->poll_interval)); |
Oliver Wang | 4b325d7 | 2013-08-12 17:30:12 +0800 | [diff] [blame] | 323 | else { |
Xiaocheng Li | b4cc7df | 2013-07-17 11:46:40 -0700 | [diff] [blame] | 324 | i2c_smbus_write_byte_data(sensor->client, |
| 325 | MPU3050_INT_CFG, |
| 326 | MPU3050_ACTIVE_LOW | |
| 327 | MPU3050_OPEN_DRAIN | |
| 328 | MPU3050_RAW_RDY_EN); |
Oliver Wang | 4b325d7 | 2013-08-12 17:30:12 +0800 | [diff] [blame] | 329 | enable_irq(sensor->client->irq); |
| 330 | } |
Xiaocheng Li | b4cc7df | 2013-07-17 11:46:40 -0700 | [diff] [blame] | 331 | } else { |
| 332 | if (sensor->use_poll) |
| 333 | cancel_delayed_work_sync(&sensor->input_work); |
Oliver Wang | 4b325d7 | 2013-08-12 17:30:12 +0800 | [diff] [blame] | 334 | else |
| 335 | disable_irq(sensor->client->irq); |
Xiaocheng Li | b4cc7df | 2013-07-17 11:46:40 -0700 | [diff] [blame] | 336 | gpio_set_value(sensor->enable_gpio, 0); |
| 337 | pm_runtime_put(sensor->dev); |
| 338 | } |
Xiaocheng Li | 05adbd8 | 2013-06-30 17:21:58 -0700 | [diff] [blame] | 339 | return count; |
| 340 | } |
| 341 | |
| 342 | static ssize_t mpu3050_attr_get_enable(struct device *dev, |
| 343 | struct device_attribute *attr, char *buf) |
| 344 | { |
| 345 | struct mpu3050_sensor *sensor = dev_get_drvdata(dev); |
| 346 | |
| 347 | return snprintf(buf, 4, "%d\n", sensor->enable); |
| 348 | } |
| 349 | |
Wentao Xu | c7769c0 | 2012-08-03 15:06:41 -0400 | [diff] [blame] | 350 | static struct device_attribute attributes[] = { |
Wentao Xu | 75a5e4b | 2012-11-15 16:30:15 -0500 | [diff] [blame] | 351 | __ATTR(pollrate_ms, 0664, |
Wentao Xu | c7769c0 | 2012-08-03 15:06:41 -0400 | [diff] [blame] | 352 | mpu3050_attr_get_polling_rate, |
| 353 | mpu3050_attr_set_polling_rate), |
Richard Liu | ce2264d | 2013-04-19 13:35:46 -0700 | [diff] [blame] | 354 | __ATTR(enable, 0644, |
Xiaocheng Li | 05adbd8 | 2013-06-30 17:21:58 -0700 | [diff] [blame] | 355 | mpu3050_attr_get_enable, |
| 356 | mpu3050_attr_set_enable), |
Wentao Xu | c7769c0 | 2012-08-03 15:06:41 -0400 | [diff] [blame] | 357 | }; |
| 358 | |
| 359 | static int create_sysfs_interfaces(struct device *dev) |
| 360 | { |
| 361 | int i; |
| 362 | int err; |
| 363 | for (i = 0; i < ARRAY_SIZE(attributes); i++) { |
| 364 | err = device_create_file(dev, attributes + i); |
| 365 | if (err) |
| 366 | goto error; |
| 367 | } |
| 368 | return 0; |
| 369 | |
| 370 | error: |
| 371 | for ( ; i >= 0; i--) |
| 372 | device_remove_file(dev, attributes + i); |
| 373 | dev_err(dev, "%s:Unable to create interface\n", __func__); |
| 374 | return err; |
| 375 | } |
| 376 | |
| 377 | static int remove_sysfs_interfaces(struct device *dev) |
| 378 | { |
| 379 | int i; |
| 380 | for (i = 0; i < ARRAY_SIZE(attributes); i++) |
| 381 | device_remove_file(dev, attributes + i); |
| 382 | return 0; |
| 383 | } |
| 384 | |
| 385 | /** |
Joseph Lai | 631b16e | 2011-06-27 13:26:53 -0700 | [diff] [blame] | 386 | * mpu3050_xyz_read_reg - read the axes values |
| 387 | * @buffer: provide register addr and get register |
| 388 | * @length: length of register |
| 389 | * |
| 390 | * Reads the register values in one transaction or returns a negative |
| 391 | * error code on failure. |
| 392 | */ |
| 393 | static int mpu3050_xyz_read_reg(struct i2c_client *client, |
| 394 | u8 *buffer, int length) |
| 395 | { |
| 396 | /* |
| 397 | * Annoying we can't make this const because the i2c layer doesn't |
| 398 | * declare input buffers const. |
| 399 | */ |
| 400 | char cmd = MPU3050_XOUT_H; |
| 401 | struct i2c_msg msg[] = { |
| 402 | { |
| 403 | .addr = client->addr, |
| 404 | .flags = 0, |
| 405 | .len = 1, |
| 406 | .buf = &cmd, |
| 407 | }, |
| 408 | { |
| 409 | .addr = client->addr, |
| 410 | .flags = I2C_M_RD, |
| 411 | .len = length, |
| 412 | .buf = buffer, |
| 413 | }, |
| 414 | }; |
| 415 | |
| 416 | return i2c_transfer(client->adapter, msg, 2); |
| 417 | } |
| 418 | |
| 419 | /** |
| 420 | * mpu3050_read_xyz - get co-ordinates from device |
| 421 | * @client: i2c address of sensor |
| 422 | * @coords: co-ordinates to update |
| 423 | * |
| 424 | * Return the converted X Y and Z co-ordinates from the sensor device |
| 425 | */ |
| 426 | static void mpu3050_read_xyz(struct i2c_client *client, |
| 427 | struct axis_data *coords) |
| 428 | { |
| 429 | u16 buffer[3]; |
| 430 | |
| 431 | mpu3050_xyz_read_reg(client, (u8 *)buffer, 6); |
| 432 | coords->x = be16_to_cpu(buffer[0]); |
| 433 | coords->y = be16_to_cpu(buffer[1]); |
| 434 | coords->z = be16_to_cpu(buffer[2]); |
| 435 | dev_dbg(&client->dev, "%s: x %d, y %d, z %d\n", __func__, |
| 436 | coords->x, coords->y, coords->z); |
| 437 | } |
| 438 | |
| 439 | /** |
| 440 | * mpu3050_set_power_mode - set the power mode |
| 441 | * @client: i2c client for the sensor |
| 442 | * @val: value to switch on/off of power, 1: normal power, 0: low power |
| 443 | * |
| 444 | * Put device to normal-power mode or low-power mode. |
| 445 | */ |
| 446 | static void mpu3050_set_power_mode(struct i2c_client *client, u8 val) |
| 447 | { |
| 448 | u8 value; |
Richard Liu | a1d406a | 2013-04-14 13:46:48 -0700 | [diff] [blame] | 449 | struct mpu3050_sensor *sensor = i2c_get_clientdata(client); |
Joseph Lai | 631b16e | 2011-06-27 13:26:53 -0700 | [diff] [blame] | 450 | |
Wentao Xu | dac9e60 | 2012-06-12 11:52:34 -0400 | [diff] [blame] | 451 | if (val) { |
| 452 | mpu3050_config_regulator(client, 1); |
| 453 | udelay(10); |
Richard Liu | a1d406a | 2013-04-14 13:46:48 -0700 | [diff] [blame] | 454 | gpio_set_value(sensor->enable_gpio, 1); |
Oliver Wang | 4b325d7 | 2013-08-12 17:30:12 +0800 | [diff] [blame] | 455 | msleep(60); |
Wentao Xu | dac9e60 | 2012-06-12 11:52:34 -0400 | [diff] [blame] | 456 | } |
| 457 | |
Joseph Lai | 631b16e | 2011-06-27 13:26:53 -0700 | [diff] [blame] | 458 | value = i2c_smbus_read_byte_data(client, MPU3050_PWR_MGM); |
| 459 | value = (value & ~MPU3050_PWR_MGM_MASK) | |
| 460 | (((val << MPU3050_PWR_MGM_POS) & MPU3050_PWR_MGM_MASK) ^ |
| 461 | MPU3050_PWR_MGM_MASK); |
| 462 | i2c_smbus_write_byte_data(client, MPU3050_PWR_MGM, value); |
Wentao Xu | dac9e60 | 2012-06-12 11:52:34 -0400 | [diff] [blame] | 463 | |
| 464 | if (!val) { |
| 465 | udelay(10); |
Richard Liu | a1d406a | 2013-04-14 13:46:48 -0700 | [diff] [blame] | 466 | gpio_set_value(sensor->enable_gpio, 0); |
| 467 | udelay(10); |
Wentao Xu | dac9e60 | 2012-06-12 11:52:34 -0400 | [diff] [blame] | 468 | mpu3050_config_regulator(client, 0); |
| 469 | } |
Joseph Lai | 631b16e | 2011-06-27 13:26:53 -0700 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | /** |
| 473 | * mpu3050_input_open - called on input event open |
| 474 | * @input: input dev of opened device |
| 475 | * |
| 476 | * The input layer calls this function when input event is opened. The |
| 477 | * function will push the device to resume. Then, the device is ready |
| 478 | * to provide data. |
| 479 | */ |
| 480 | static int mpu3050_input_open(struct input_dev *input) |
| 481 | { |
| 482 | struct mpu3050_sensor *sensor = input_get_drvdata(input); |
Heikki Krogerus | 3b51872 | 2011-12-23 23:57:09 -0800 | [diff] [blame] | 483 | int error; |
Joseph Lai | 631b16e | 2011-06-27 13:26:53 -0700 | [diff] [blame] | 484 | |
Wentao Xu | c7769c0 | 2012-08-03 15:06:41 -0400 | [diff] [blame] | 485 | pm_runtime_get_sync(sensor->dev); |
Joseph Lai | 631b16e | 2011-06-27 13:26:53 -0700 | [diff] [blame] | 486 | |
Heikki Krogerus | 3b51872 | 2011-12-23 23:57:09 -0800 | [diff] [blame] | 487 | /* Enable interrupts */ |
| 488 | error = i2c_smbus_write_byte_data(sensor->client, MPU3050_INT_CFG, |
Wentao Xu | c7769c0 | 2012-08-03 15:06:41 -0400 | [diff] [blame] | 489 | MPU3050_ACTIVE_LOW | |
| 490 | MPU3050_OPEN_DRAIN | |
| 491 | MPU3050_RAW_RDY_EN); |
Heikki Krogerus | 3b51872 | 2011-12-23 23:57:09 -0800 | [diff] [blame] | 492 | if (error < 0) { |
| 493 | pm_runtime_put(sensor->dev); |
| 494 | return error; |
| 495 | } |
Wentao Xu | dac9e60 | 2012-06-12 11:52:34 -0400 | [diff] [blame] | 496 | if (sensor->use_poll) |
| 497 | schedule_delayed_work(&sensor->input_work, |
Wentao Xu | c7769c0 | 2012-08-03 15:06:41 -0400 | [diff] [blame] | 498 | msecs_to_jiffies(sensor->poll_interval)); |
Heikki Krogerus | 3b51872 | 2011-12-23 23:57:09 -0800 | [diff] [blame] | 499 | |
Joseph Lai | 631b16e | 2011-06-27 13:26:53 -0700 | [diff] [blame] | 500 | return 0; |
| 501 | } |
| 502 | |
| 503 | /** |
| 504 | * mpu3050_input_close - called on input event close |
| 505 | * @input: input dev of closed device |
| 506 | * |
| 507 | * The input layer calls this function when input event is closed. The |
| 508 | * function will push the device to suspend. |
| 509 | */ |
| 510 | static void mpu3050_input_close(struct input_dev *input) |
| 511 | { |
| 512 | struct mpu3050_sensor *sensor = input_get_drvdata(input); |
| 513 | |
Wentao Xu | dac9e60 | 2012-06-12 11:52:34 -0400 | [diff] [blame] | 514 | if (sensor->use_poll) |
| 515 | cancel_delayed_work_sync(&sensor->input_work); |
| 516 | |
Joseph Lai | 631b16e | 2011-06-27 13:26:53 -0700 | [diff] [blame] | 517 | pm_runtime_put(sensor->dev); |
| 518 | } |
| 519 | |
| 520 | /** |
| 521 | * mpu3050_interrupt_thread - handle an IRQ |
| 522 | * @irq: interrupt numner |
| 523 | * @data: the sensor |
| 524 | * |
| 525 | * Called by the kernel single threaded after an interrupt occurs. Read |
| 526 | * the sensor data and generate an input event for it. |
| 527 | */ |
| 528 | static irqreturn_t mpu3050_interrupt_thread(int irq, void *data) |
| 529 | { |
| 530 | struct mpu3050_sensor *sensor = data; |
| 531 | struct axis_data axis; |
| 532 | |
| 533 | mpu3050_read_xyz(sensor->client, &axis); |
| 534 | |
| 535 | input_report_abs(sensor->idev, ABS_X, axis.x); |
| 536 | input_report_abs(sensor->idev, ABS_Y, axis.y); |
| 537 | input_report_abs(sensor->idev, ABS_Z, axis.z); |
| 538 | input_sync(sensor->idev); |
| 539 | |
| 540 | return IRQ_HANDLED; |
| 541 | } |
| 542 | |
| 543 | /** |
Wentao Xu | dac9e60 | 2012-06-12 11:52:34 -0400 | [diff] [blame] | 544 | * mpu3050_input_work_fn - polling work |
| 545 | * @work: the work struct |
| 546 | * |
| 547 | * Called by the work queue; read sensor data and generate an input |
| 548 | * event |
| 549 | */ |
| 550 | static void mpu3050_input_work_fn(struct work_struct *work) |
| 551 | { |
| 552 | struct mpu3050_sensor *sensor; |
| 553 | struct axis_data axis; |
| 554 | |
| 555 | sensor = container_of((struct delayed_work *)work, |
| 556 | struct mpu3050_sensor, input_work); |
| 557 | |
| 558 | mpu3050_read_xyz(sensor->client, &axis); |
| 559 | |
| 560 | input_report_abs(sensor->idev, ABS_X, axis.x); |
| 561 | input_report_abs(sensor->idev, ABS_Y, axis.y); |
| 562 | input_report_abs(sensor->idev, ABS_Z, axis.z); |
| 563 | input_sync(sensor->idev); |
| 564 | |
| 565 | if (sensor->use_poll) |
| 566 | schedule_delayed_work(&sensor->input_work, |
Wentao Xu | c7769c0 | 2012-08-03 15:06:41 -0400 | [diff] [blame] | 567 | msecs_to_jiffies(sensor->poll_interval)); |
Wentao Xu | dac9e60 | 2012-06-12 11:52:34 -0400 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | /** |
Heikki Krogerus | cd314fa | 2011-12-24 00:09:04 -0800 | [diff] [blame] | 571 | * mpu3050_hw_init - initialize hardware |
| 572 | * @sensor: the sensor |
| 573 | * |
| 574 | * Called during device probe; configures the sampling method. |
| 575 | */ |
| 576 | static int __devinit mpu3050_hw_init(struct mpu3050_sensor *sensor) |
| 577 | { |
| 578 | struct i2c_client *client = sensor->client; |
| 579 | int ret; |
| 580 | u8 reg; |
| 581 | |
| 582 | /* Reset */ |
| 583 | ret = i2c_smbus_write_byte_data(client, MPU3050_PWR_MGM, |
| 584 | MPU3050_PWR_MGM_RESET); |
| 585 | if (ret < 0) |
| 586 | return ret; |
| 587 | |
| 588 | ret = i2c_smbus_read_byte_data(client, MPU3050_PWR_MGM); |
| 589 | if (ret < 0) |
| 590 | return ret; |
| 591 | |
| 592 | ret &= ~MPU3050_PWR_MGM_CLKSEL; |
| 593 | ret |= MPU3050_PWR_MGM_PLL_Z; |
| 594 | ret = i2c_smbus_write_byte_data(client, MPU3050_PWR_MGM, ret); |
| 595 | if (ret < 0) |
| 596 | return ret; |
| 597 | |
| 598 | /* Output frequency divider. The poll interval */ |
| 599 | ret = i2c_smbus_write_byte_data(client, MPU3050_SMPLRT_DIV, |
Wentao Xu | c7769c0 | 2012-08-03 15:06:41 -0400 | [diff] [blame] | 600 | sensor->poll_interval - 1); |
Heikki Krogerus | cd314fa | 2011-12-24 00:09:04 -0800 | [diff] [blame] | 601 | if (ret < 0) |
| 602 | return ret; |
| 603 | |
| 604 | /* Set low pass filter and full scale */ |
Wentao Xu | 04f4cc9 | 2012-08-15 19:41:43 -0400 | [diff] [blame] | 605 | reg = MPU3050_DLPF_CFG_42HZ; |
| 606 | reg |= MPU3050_DEFAULT_FS_RANGE << 3; |
Heikki Krogerus | cd314fa | 2011-12-24 00:09:04 -0800 | [diff] [blame] | 607 | reg |= MPU3050_EXT_SYNC_NONE << 5; |
| 608 | ret = i2c_smbus_write_byte_data(client, MPU3050_DLPF_FS_SYNC, reg); |
| 609 | if (ret < 0) |
| 610 | return ret; |
| 611 | |
| 612 | return 0; |
| 613 | } |
Richard Liu | a1d406a | 2013-04-14 13:46:48 -0700 | [diff] [blame] | 614 | #ifdef CONFIG_OF |
| 615 | static int mpu3050_parse_dt(struct device *dev, |
| 616 | struct mpu3050_gyro_platform_data *pdata) |
| 617 | { |
| 618 | int rc = 0; |
| 619 | |
| 620 | rc = of_property_read_u32(dev->of_node, "invn,poll-interval", |
| 621 | &pdata->poll_interval); |
| 622 | if (rc) { |
| 623 | dev_err(dev, "Failed to read poll-interval\n"); |
| 624 | return rc; |
| 625 | } |
| 626 | |
| 627 | /* check gpio_int later, if it is invalid, just use poll */ |
| 628 | pdata->gpio_int = of_get_named_gpio_flags(dev->of_node, |
| 629 | "invn,gpio-int", 0, NULL); |
| 630 | |
| 631 | pdata->gpio_en = of_get_named_gpio_flags(dev->of_node, |
| 632 | "invn,gpio-en", 0, NULL); |
| 633 | if (!gpio_is_valid(pdata->gpio_en)) |
| 634 | return -EINVAL; |
| 635 | |
| 636 | return 0; |
| 637 | } |
| 638 | #else |
| 639 | static int mpu3050_parse_dt(struct device *dev, |
| 640 | struct mpu3050_gyro_platform_data *pdata) |
| 641 | { |
| 642 | return -EINVAL; |
| 643 | } |
| 644 | #endif |
Heikki Krogerus | cd314fa | 2011-12-24 00:09:04 -0800 | [diff] [blame] | 645 | |
| 646 | /** |
Joseph Lai | 631b16e | 2011-06-27 13:26:53 -0700 | [diff] [blame] | 647 | * mpu3050_probe - device detection callback |
| 648 | * @client: i2c client of found device |
| 649 | * @id: id match information |
| 650 | * |
| 651 | * The I2C layer calls us when it believes a sensor is present at this |
| 652 | * address. Probe to see if this is correct and to validate the device. |
| 653 | * |
| 654 | * If present install the relevant sysfs interfaces and input device. |
| 655 | */ |
| 656 | static int __devinit mpu3050_probe(struct i2c_client *client, |
| 657 | const struct i2c_device_id *id) |
| 658 | { |
| 659 | struct mpu3050_sensor *sensor; |
| 660 | struct input_dev *idev; |
Richard Liu | a1d406a | 2013-04-14 13:46:48 -0700 | [diff] [blame] | 661 | struct mpu3050_gyro_platform_data *pdata; |
Joseph Lai | 631b16e | 2011-06-27 13:26:53 -0700 | [diff] [blame] | 662 | int ret; |
| 663 | int error; |
Richard Liu | a1d406a | 2013-04-14 13:46:48 -0700 | [diff] [blame] | 664 | u32 i; |
Joseph Lai | 631b16e | 2011-06-27 13:26:53 -0700 | [diff] [blame] | 665 | |
| 666 | sensor = kzalloc(sizeof(struct mpu3050_sensor), GFP_KERNEL); |
| 667 | idev = input_allocate_device(); |
| 668 | if (!sensor || !idev) { |
| 669 | dev_err(&client->dev, "failed to allocate driver data\n"); |
| 670 | error = -ENOMEM; |
| 671 | goto err_free_mem; |
| 672 | } |
| 673 | |
| 674 | sensor->client = client; |
| 675 | sensor->dev = &client->dev; |
| 676 | sensor->idev = idev; |
Wentao Xu | c7769c0 | 2012-08-03 15:06:41 -0400 | [diff] [blame] | 677 | i2c_set_clientdata(client, sensor); |
Richard Liu | a1d406a | 2013-04-14 13:46:48 -0700 | [diff] [blame] | 678 | |
| 679 | if (client->dev.of_node) { |
| 680 | pdata = devm_kzalloc(&client->dev, |
| 681 | sizeof(struct mpu3050_gyro_platform_data), GFP_KERNEL); |
| 682 | if (!pdata) { |
| 683 | dev_err(&client->dev, "Failed to allcated memory\n"); |
| 684 | error = -ENOMEM; |
| 685 | goto err_free_mem; |
| 686 | } |
| 687 | ret = mpu3050_parse_dt(&client->dev, pdata); |
| 688 | if (ret) { |
| 689 | dev_err(&client->dev, "Failed to parse device tree\n"); |
| 690 | error = ret; |
| 691 | goto err_free_mem; |
| 692 | } |
| 693 | } else |
| 694 | pdata = client->dev.platform_data; |
| 695 | sensor->platform_data = pdata; |
| 696 | |
Wentao Xu | c7769c0 | 2012-08-03 15:06:41 -0400 | [diff] [blame] | 697 | if (sensor->platform_data) { |
| 698 | u32 interval = sensor->platform_data->poll_interval; |
Richard Liu | a1d406a | 2013-04-14 13:46:48 -0700 | [diff] [blame] | 699 | sensor->enable_gpio = sensor->platform_data->gpio_en; |
Wentao Xu | c7769c0 | 2012-08-03 15:06:41 -0400 | [diff] [blame] | 700 | |
| 701 | if ((interval < MPU3050_MIN_POLL_INTERVAL) || |
| 702 | (interval > MPU3050_MAX_POLL_INTERVAL)) |
| 703 | sensor->poll_interval = MPU3050_DEFAULT_POLL_INTERVAL; |
| 704 | else |
| 705 | sensor->poll_interval = interval; |
| 706 | } else { |
| 707 | sensor->poll_interval = MPU3050_DEFAULT_POLL_INTERVAL; |
Richard Liu | a1d406a | 2013-04-14 13:46:48 -0700 | [diff] [blame] | 708 | sensor->enable_gpio = -EINVAL; |
| 709 | } |
| 710 | |
| 711 | if (gpio_is_valid(sensor->enable_gpio)) { |
| 712 | ret = gpio_request(sensor->enable_gpio, "GYRO_EN_PM"); |
| 713 | gpio_direction_output(sensor->enable_gpio, 1); |
Wentao Xu | c7769c0 | 2012-08-03 15:06:41 -0400 | [diff] [blame] | 714 | } |
Joseph Lai | 631b16e | 2011-06-27 13:26:53 -0700 | [diff] [blame] | 715 | |
| 716 | mpu3050_set_power_mode(client, 1); |
Joseph Lai | 631b16e | 2011-06-27 13:26:53 -0700 | [diff] [blame] | 717 | |
| 718 | ret = i2c_smbus_read_byte_data(client, MPU3050_CHIP_ID_REG); |
| 719 | if (ret < 0) { |
| 720 | dev_err(&client->dev, "failed to detect device\n"); |
| 721 | error = -ENXIO; |
| 722 | goto err_free_mem; |
| 723 | } |
| 724 | |
Richard Liu | a1d406a | 2013-04-14 13:46:48 -0700 | [diff] [blame] | 725 | for (i = 0; i < ARRAY_SIZE(mpu3050_chip_ids); i++) |
| 726 | if (ret == mpu3050_chip_ids[i]) |
| 727 | break; |
| 728 | |
| 729 | if (i == ARRAY_SIZE(mpu3050_chip_ids)) { |
Joseph Lai | 631b16e | 2011-06-27 13:26:53 -0700 | [diff] [blame] | 730 | dev_err(&client->dev, "unsupported chip id\n"); |
| 731 | error = -ENXIO; |
| 732 | goto err_free_mem; |
| 733 | } |
| 734 | |
| 735 | idev->name = "MPU3050"; |
| 736 | idev->id.bustype = BUS_I2C; |
| 737 | idev->dev.parent = &client->dev; |
| 738 | |
| 739 | idev->open = mpu3050_input_open; |
| 740 | idev->close = mpu3050_input_close; |
| 741 | |
Richard Liu | ce2264d | 2013-04-19 13:35:46 -0700 | [diff] [blame] | 742 | input_set_capability(idev, EV_ABS, ABS_MISC); |
Joseph Lai | 631b16e | 2011-06-27 13:26:53 -0700 | [diff] [blame] | 743 | input_set_abs_params(idev, ABS_X, |
| 744 | MPU3050_MIN_VALUE, MPU3050_MAX_VALUE, 0, 0); |
| 745 | input_set_abs_params(idev, ABS_Y, |
| 746 | MPU3050_MIN_VALUE, MPU3050_MAX_VALUE, 0, 0); |
| 747 | input_set_abs_params(idev, ABS_Z, |
| 748 | MPU3050_MIN_VALUE, MPU3050_MAX_VALUE, 0, 0); |
| 749 | |
| 750 | input_set_drvdata(idev, sensor); |
| 751 | |
| 752 | pm_runtime_set_active(&client->dev); |
| 753 | |
Heikki Krogerus | cd314fa | 2011-12-24 00:09:04 -0800 | [diff] [blame] | 754 | error = mpu3050_hw_init(sensor); |
| 755 | if (error) |
| 756 | goto err_pm_set_suspended; |
| 757 | |
Wentao Xu | dac9e60 | 2012-06-12 11:52:34 -0400 | [diff] [blame] | 758 | if (client->irq == 0) { |
Wentao Xu | dac9e60 | 2012-06-12 11:52:34 -0400 | [diff] [blame] | 759 | sensor->use_poll = 1; |
Wentao Xu | c7769c0 | 2012-08-03 15:06:41 -0400 | [diff] [blame] | 760 | INIT_DELAYED_WORK(&sensor->input_work, mpu3050_input_work_fn); |
Wentao Xu | dac9e60 | 2012-06-12 11:52:34 -0400 | [diff] [blame] | 761 | } else { |
| 762 | sensor->use_poll = 0; |
| 763 | |
Wentao Xu | c7769c0 | 2012-08-03 15:06:41 -0400 | [diff] [blame] | 764 | if (gpio_is_valid(sensor->platform_data->gpio_int)) { |
| 765 | /* configure interrupt gpio */ |
| 766 | ret = gpio_request(sensor->platform_data->gpio_int, |
| 767 | "gyro_gpio_int"); |
| 768 | if (ret) { |
| 769 | pr_err("%s: unable to request interrupt gpio %d\n", |
| 770 | __func__, |
| 771 | sensor->platform_data->gpio_int); |
| 772 | goto err_pm_set_suspended; |
| 773 | } |
| 774 | |
| 775 | ret = gpio_direction_input( |
| 776 | sensor->platform_data->gpio_int); |
| 777 | if (ret) { |
| 778 | pr_err("%s: unable to set direction for gpio %d\n", |
| 779 | __func__, sensor->platform_data->gpio_int); |
| 780 | goto err_free_gpio; |
| 781 | } |
Richard Liu | a1d406a | 2013-04-14 13:46:48 -0700 | [diff] [blame] | 782 | client->irq = gpio_to_irq( |
| 783 | sensor->platform_data->gpio_int); |
| 784 | } else { |
| 785 | ret = -EINVAL; |
| 786 | goto err_pm_set_suspended; |
Wentao Xu | c7769c0 | 2012-08-03 15:06:41 -0400 | [diff] [blame] | 787 | } |
| 788 | |
Wentao Xu | dac9e60 | 2012-06-12 11:52:34 -0400 | [diff] [blame] | 789 | error = request_threaded_irq(client->irq, |
Joseph Lai | 631b16e | 2011-06-27 13:26:53 -0700 | [diff] [blame] | 790 | NULL, mpu3050_interrupt_thread, |
Wentao Xu | c7769c0 | 2012-08-03 15:06:41 -0400 | [diff] [blame] | 791 | IRQF_TRIGGER_FALLING, |
Heikki Krogerus | 3b51872 | 2011-12-23 23:57:09 -0800 | [diff] [blame] | 792 | "mpu3050", sensor); |
Wentao Xu | dac9e60 | 2012-06-12 11:52:34 -0400 | [diff] [blame] | 793 | if (error) { |
| 794 | dev_err(&client->dev, |
| 795 | "can't get IRQ %d, error %d\n", |
| 796 | client->irq, error); |
| 797 | goto err_pm_set_suspended; |
| 798 | } |
Oliver Wang | 2f4baed | 2013-08-20 16:07:26 +0800 | [diff] [blame] | 799 | disable_irq(client->irq); |
Joseph Lai | 631b16e | 2011-06-27 13:26:53 -0700 | [diff] [blame] | 800 | } |
| 801 | |
| 802 | error = input_register_device(idev); |
| 803 | if (error) { |
| 804 | dev_err(&client->dev, "failed to register input device\n"); |
| 805 | goto err_free_irq; |
| 806 | } |
| 807 | |
Richard Liu | a1d406a | 2013-04-14 13:46:48 -0700 | [diff] [blame] | 808 | error = create_sysfs_interfaces(&idev->dev); |
Wentao Xu | c7769c0 | 2012-08-03 15:06:41 -0400 | [diff] [blame] | 809 | if (error < 0) { |
| 810 | dev_err(&client->dev, "failed to create sysfs\n"); |
| 811 | goto err_input_cleanup; |
| 812 | } |
| 813 | |
Xiaocheng Li | b4cc7df | 2013-07-17 11:46:40 -0700 | [diff] [blame] | 814 | pm_runtime_enable(&client->dev); |
| 815 | pm_runtime_set_autosuspend_delay(&client->dev, MPU3050_AUTO_DELAY); |
Joseph Lai | 631b16e | 2011-06-27 13:26:53 -0700 | [diff] [blame] | 816 | |
| 817 | return 0; |
| 818 | |
Wentao Xu | c7769c0 | 2012-08-03 15:06:41 -0400 | [diff] [blame] | 819 | err_input_cleanup: |
| 820 | input_unregister_device(idev); |
Joseph Lai | 631b16e | 2011-06-27 13:26:53 -0700 | [diff] [blame] | 821 | err_free_irq: |
Wentao Xu | dac9e60 | 2012-06-12 11:52:34 -0400 | [diff] [blame] | 822 | if (client->irq > 0) |
| 823 | free_irq(client->irq, sensor); |
Wentao Xu | c7769c0 | 2012-08-03 15:06:41 -0400 | [diff] [blame] | 824 | err_free_gpio: |
| 825 | if ((client->irq > 0) && |
| 826 | (gpio_is_valid(sensor->platform_data->gpio_int))) |
| 827 | gpio_free(sensor->platform_data->gpio_int); |
Joseph Lai | 631b16e | 2011-06-27 13:26:53 -0700 | [diff] [blame] | 828 | err_pm_set_suspended: |
| 829 | pm_runtime_set_suspended(&client->dev); |
| 830 | err_free_mem: |
Axel Lin | d9b830f | 2011-08-11 09:19:29 -0700 | [diff] [blame] | 831 | input_free_device(idev); |
Joseph Lai | 631b16e | 2011-06-27 13:26:53 -0700 | [diff] [blame] | 832 | kfree(sensor); |
| 833 | return error; |
| 834 | } |
| 835 | |
| 836 | /** |
| 837 | * mpu3050_remove - remove a sensor |
| 838 | * @client: i2c client of sensor being removed |
| 839 | * |
| 840 | * Our sensor is going away, clean up the resources. |
| 841 | */ |
| 842 | static int __devexit mpu3050_remove(struct i2c_client *client) |
| 843 | { |
| 844 | struct mpu3050_sensor *sensor = i2c_get_clientdata(client); |
| 845 | |
| 846 | pm_runtime_disable(&client->dev); |
| 847 | pm_runtime_set_suspended(&client->dev); |
| 848 | |
Wentao Xu | c7769c0 | 2012-08-03 15:06:41 -0400 | [diff] [blame] | 849 | if (client->irq) |
| 850 | free_irq(client->irq, sensor); |
| 851 | |
| 852 | remove_sysfs_interfaces(&client->dev); |
Richard Liu | a1d406a | 2013-04-14 13:46:48 -0700 | [diff] [blame] | 853 | if (gpio_is_valid(sensor->enable_gpio)) |
| 854 | gpio_free(sensor->enable_gpio); |
Joseph Lai | 631b16e | 2011-06-27 13:26:53 -0700 | [diff] [blame] | 855 | input_unregister_device(sensor->idev); |
Wentao Xu | c7769c0 | 2012-08-03 15:06:41 -0400 | [diff] [blame] | 856 | |
Joseph Lai | 631b16e | 2011-06-27 13:26:53 -0700 | [diff] [blame] | 857 | kfree(sensor); |
| 858 | |
| 859 | return 0; |
| 860 | } |
| 861 | |
| 862 | #ifdef CONFIG_PM |
| 863 | /** |
| 864 | * mpu3050_suspend - called on device suspend |
| 865 | * @dev: device being suspended |
| 866 | * |
| 867 | * Put the device into sleep mode before we suspend the machine. |
| 868 | */ |
| 869 | static int mpu3050_suspend(struct device *dev) |
| 870 | { |
| 871 | struct i2c_client *client = to_i2c_client(dev); |
Oliver Wang | 4b325d7 | 2013-08-12 17:30:12 +0800 | [diff] [blame] | 872 | struct mpu3050_sensor *sensor = i2c_get_clientdata(client); |
| 873 | |
| 874 | if (!sensor->use_poll) |
| 875 | disable_irq(client->irq); |
Joseph Lai | 631b16e | 2011-06-27 13:26:53 -0700 | [diff] [blame] | 876 | |
| 877 | mpu3050_set_power_mode(client, 0); |
| 878 | |
| 879 | return 0; |
| 880 | } |
| 881 | |
| 882 | /** |
| 883 | * mpu3050_resume - called on device resume |
| 884 | * @dev: device being resumed |
| 885 | * |
| 886 | * Put the device into powered mode on resume. |
| 887 | */ |
| 888 | static int mpu3050_resume(struct device *dev) |
| 889 | { |
| 890 | struct i2c_client *client = to_i2c_client(dev); |
Oliver Wang | 4b325d7 | 2013-08-12 17:30:12 +0800 | [diff] [blame] | 891 | struct mpu3050_sensor *sensor = i2c_get_clientdata(client); |
Joseph Lai | 631b16e | 2011-06-27 13:26:53 -0700 | [diff] [blame] | 892 | |
| 893 | mpu3050_set_power_mode(client, 1); |
Oliver Wang | 4b325d7 | 2013-08-12 17:30:12 +0800 | [diff] [blame] | 894 | |
| 895 | if (!sensor->use_poll) |
| 896 | enable_irq(client->irq); |
Joseph Lai | 631b16e | 2011-06-27 13:26:53 -0700 | [diff] [blame] | 897 | |
| 898 | return 0; |
| 899 | } |
| 900 | #endif |
| 901 | |
| 902 | static UNIVERSAL_DEV_PM_OPS(mpu3050_pm, mpu3050_suspend, mpu3050_resume, NULL); |
| 903 | |
| 904 | static const struct i2c_device_id mpu3050_ids[] = { |
| 905 | { "mpu3050", 0 }, |
| 906 | { } |
| 907 | }; |
| 908 | MODULE_DEVICE_TABLE(i2c, mpu3050_ids); |
| 909 | |
Olof Johansson | e948981 | 2011-12-23 01:20:44 -0800 | [diff] [blame] | 910 | static const struct of_device_id mpu3050_of_match[] = { |
| 911 | { .compatible = "invn,mpu3050", }, |
| 912 | { }, |
| 913 | }; |
| 914 | MODULE_DEVICE_TABLE(of, mpu3050_of_match); |
| 915 | |
Joseph Lai | 631b16e | 2011-06-27 13:26:53 -0700 | [diff] [blame] | 916 | static struct i2c_driver mpu3050_i2c_driver = { |
| 917 | .driver = { |
| 918 | .name = "mpu3050", |
| 919 | .owner = THIS_MODULE, |
| 920 | .pm = &mpu3050_pm, |
Olof Johansson | e948981 | 2011-12-23 01:20:44 -0800 | [diff] [blame] | 921 | .of_match_table = mpu3050_of_match, |
Joseph Lai | 631b16e | 2011-06-27 13:26:53 -0700 | [diff] [blame] | 922 | }, |
| 923 | .probe = mpu3050_probe, |
| 924 | .remove = __devexit_p(mpu3050_remove), |
| 925 | .id_table = mpu3050_ids, |
| 926 | }; |
| 927 | |
Axel Lin | 1b92c1c | 2012-03-16 23:05:41 -0700 | [diff] [blame] | 928 | module_i2c_driver(mpu3050_i2c_driver); |
Joseph Lai | 631b16e | 2011-06-27 13:26:53 -0700 | [diff] [blame] | 929 | |
| 930 | MODULE_AUTHOR("Wistron Corp."); |
| 931 | MODULE_DESCRIPTION("MPU3050 Tri-axis gyroscope driver"); |
| 932 | MODULE_LICENSE("GPL"); |