mpu3050.c 12 KB

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