PageRenderTime 25ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/input/misc/mpu3050.c

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