mpu3050.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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_REG 0x00
  43. #define MPU3050_CHIP_ID 0x69
  44. #define MPU3050_XOUT_H 0x1D
  45. #define MPU3050_PWR_MGM 0x3E
  46. #define MPU3050_PWR_MGM_POS 6
  47. #define MPU3050_PWR_MGM_MASK 0x40
  48. #define MPU3050_AUTO_DELAY 1000
  49. #define MPU3050_MIN_VALUE -32768
  50. #define MPU3050_MAX_VALUE 32767
  51. struct axis_data {
  52. s16 x;
  53. s16 y;
  54. s16 z;
  55. };
  56. struct mpu3050_sensor {
  57. struct i2c_client *client;
  58. struct device *dev;
  59. struct input_dev *idev;
  60. };
  61. /**
  62. * mpu3050_xyz_read_reg - read the axes values
  63. * @buffer: provide register addr and get register
  64. * @length: length of register
  65. *
  66. * Reads the register values in one transaction or returns a negative
  67. * error code on failure.
  68. */
  69. static int mpu3050_xyz_read_reg(struct i2c_client *client,
  70. u8 *buffer, int length)
  71. {
  72. /*
  73. * Annoying we can't make this const because the i2c layer doesn't
  74. * declare input buffers const.
  75. */
  76. char cmd = MPU3050_XOUT_H;
  77. struct i2c_msg msg[] = {
  78. {
  79. .addr = client->addr,
  80. .flags = 0,
  81. .len = 1,
  82. .buf = &cmd,
  83. },
  84. {
  85. .addr = client->addr,
  86. .flags = I2C_M_RD,
  87. .len = length,
  88. .buf = buffer,
  89. },
  90. };
  91. return i2c_transfer(client->adapter, msg, 2);
  92. }
  93. /**
  94. * mpu3050_read_xyz - get co-ordinates from device
  95. * @client: i2c address of sensor
  96. * @coords: co-ordinates to update
  97. *
  98. * Return the converted X Y and Z co-ordinates from the sensor device
  99. */
  100. static void mpu3050_read_xyz(struct i2c_client *client,
  101. struct axis_data *coords)
  102. {
  103. u16 buffer[3];
  104. mpu3050_xyz_read_reg(client, (u8 *)buffer, 6);
  105. coords->x = be16_to_cpu(buffer[0]);
  106. coords->y = be16_to_cpu(buffer[1]);
  107. coords->z = be16_to_cpu(buffer[2]);
  108. dev_dbg(&client->dev, "%s: x %d, y %d, z %d\n", __func__,
  109. coords->x, coords->y, coords->z);
  110. }
  111. /**
  112. * mpu3050_set_power_mode - set the power mode
  113. * @client: i2c client for the sensor
  114. * @val: value to switch on/off of power, 1: normal power, 0: low power
  115. *
  116. * Put device to normal-power mode or low-power mode.
  117. */
  118. static void mpu3050_set_power_mode(struct i2c_client *client, u8 val)
  119. {
  120. u8 value;
  121. value = i2c_smbus_read_byte_data(client, MPU3050_PWR_MGM);
  122. value = (value & ~MPU3050_PWR_MGM_MASK) |
  123. (((val << MPU3050_PWR_MGM_POS) & MPU3050_PWR_MGM_MASK) ^
  124. MPU3050_PWR_MGM_MASK);
  125. i2c_smbus_write_byte_data(client, MPU3050_PWR_MGM, value);
  126. }
  127. /**
  128. * mpu3050_input_open - called on input event open
  129. * @input: input dev of opened device
  130. *
  131. * The input layer calls this function when input event is opened. The
  132. * function will push the device to resume. Then, the device is ready
  133. * to provide data.
  134. */
  135. static int mpu3050_input_open(struct input_dev *input)
  136. {
  137. struct mpu3050_sensor *sensor = input_get_drvdata(input);
  138. pm_runtime_get(sensor->dev);
  139. return 0;
  140. }
  141. /**
  142. * mpu3050_input_close - called on input event close
  143. * @input: input dev of closed device
  144. *
  145. * The input layer calls this function when input event is closed. The
  146. * function will push the device to suspend.
  147. */
  148. static void mpu3050_input_close(struct input_dev *input)
  149. {
  150. struct mpu3050_sensor *sensor = input_get_drvdata(input);
  151. pm_runtime_put(sensor->dev);
  152. }
  153. /**
  154. * mpu3050_interrupt_thread - handle an IRQ
  155. * @irq: interrupt numner
  156. * @data: the sensor
  157. *
  158. * Called by the kernel single threaded after an interrupt occurs. Read
  159. * the sensor data and generate an input event for it.
  160. */
  161. static irqreturn_t mpu3050_interrupt_thread(int irq, void *data)
  162. {
  163. struct mpu3050_sensor *sensor = data;
  164. struct axis_data axis;
  165. mpu3050_read_xyz(sensor->client, &axis);
  166. input_report_abs(sensor->idev, ABS_X, axis.x);
  167. input_report_abs(sensor->idev, ABS_Y, axis.y);
  168. input_report_abs(sensor->idev, ABS_Z, axis.z);
  169. input_sync(sensor->idev);
  170. return IRQ_HANDLED;
  171. }
  172. /**
  173. * mpu3050_probe - device detection callback
  174. * @client: i2c client of found device
  175. * @id: id match information
  176. *
  177. * The I2C layer calls us when it believes a sensor is present at this
  178. * address. Probe to see if this is correct and to validate the device.
  179. *
  180. * If present install the relevant sysfs interfaces and input device.
  181. */
  182. static int __devinit mpu3050_probe(struct i2c_client *client,
  183. const struct i2c_device_id *id)
  184. {
  185. struct mpu3050_sensor *sensor;
  186. struct input_dev *idev;
  187. int ret;
  188. int error;
  189. sensor = kzalloc(sizeof(struct mpu3050_sensor), GFP_KERNEL);
  190. idev = input_allocate_device();
  191. if (!sensor || !idev) {
  192. dev_err(&client->dev, "failed to allocate driver data\n");
  193. error = -ENOMEM;
  194. goto err_free_mem;
  195. }
  196. sensor->client = client;
  197. sensor->dev = &client->dev;
  198. sensor->idev = idev;
  199. mpu3050_set_power_mode(client, 1);
  200. msleep(10);
  201. ret = i2c_smbus_read_byte_data(client, MPU3050_CHIP_ID_REG);
  202. if (ret < 0) {
  203. dev_err(&client->dev, "failed to detect device\n");
  204. error = -ENXIO;
  205. goto err_free_mem;
  206. }
  207. if (ret != MPU3050_CHIP_ID) {
  208. dev_err(&client->dev, "unsupported chip id\n");
  209. error = -ENXIO;
  210. goto err_free_mem;
  211. }
  212. idev->name = "MPU3050";
  213. idev->id.bustype = BUS_I2C;
  214. idev->dev.parent = &client->dev;
  215. idev->open = mpu3050_input_open;
  216. idev->close = mpu3050_input_close;
  217. __set_bit(EV_ABS, idev->evbit);
  218. input_set_abs_params(idev, ABS_X,
  219. MPU3050_MIN_VALUE, MPU3050_MAX_VALUE, 0, 0);
  220. input_set_abs_params(idev, ABS_Y,
  221. MPU3050_MIN_VALUE, MPU3050_MAX_VALUE, 0, 0);
  222. input_set_abs_params(idev, ABS_Z,
  223. MPU3050_MIN_VALUE, MPU3050_MAX_VALUE, 0, 0);
  224. input_set_drvdata(idev, sensor);
  225. pm_runtime_set_active(&client->dev);
  226. error = request_threaded_irq(client->irq,
  227. NULL, mpu3050_interrupt_thread,
  228. IRQF_TRIGGER_RISING,
  229. "mpu_int", sensor);
  230. if (error) {
  231. dev_err(&client->dev,
  232. "can't get IRQ %d, error %d\n", client->irq, error);
  233. goto err_pm_set_suspended;
  234. }
  235. error = input_register_device(idev);
  236. if (error) {
  237. dev_err(&client->dev, "failed to register input device\n");
  238. goto err_free_irq;
  239. }
  240. pm_runtime_enable(&client->dev);
  241. pm_runtime_set_autosuspend_delay(&client->dev, MPU3050_AUTO_DELAY);
  242. return 0;
  243. err_free_irq:
  244. free_irq(client->irq, sensor);
  245. err_pm_set_suspended:
  246. pm_runtime_set_suspended(&client->dev);
  247. err_free_mem:
  248. input_unregister_device(idev);
  249. kfree(sensor);
  250. return error;
  251. }
  252. /**
  253. * mpu3050_remove - remove a sensor
  254. * @client: i2c client of sensor being removed
  255. *
  256. * Our sensor is going away, clean up the resources.
  257. */
  258. static int __devexit mpu3050_remove(struct i2c_client *client)
  259. {
  260. struct mpu3050_sensor *sensor = i2c_get_clientdata(client);
  261. pm_runtime_disable(&client->dev);
  262. pm_runtime_set_suspended(&client->dev);
  263. free_irq(client->irq, sensor);
  264. input_unregister_device(sensor->idev);
  265. kfree(sensor);
  266. return 0;
  267. }
  268. #ifdef CONFIG_PM
  269. /**
  270. * mpu3050_suspend - called on device suspend
  271. * @dev: device being suspended
  272. *
  273. * Put the device into sleep mode before we suspend the machine.
  274. */
  275. static int mpu3050_suspend(struct device *dev)
  276. {
  277. struct i2c_client *client = to_i2c_client(dev);
  278. mpu3050_set_power_mode(client, 0);
  279. return 0;
  280. }
  281. /**
  282. * mpu3050_resume - called on device resume
  283. * @dev: device being resumed
  284. *
  285. * Put the device into powered mode on resume.
  286. */
  287. static int mpu3050_resume(struct device *dev)
  288. {
  289. struct i2c_client *client = to_i2c_client(dev);
  290. mpu3050_set_power_mode(client, 1);
  291. msleep(100); /* wait for gyro chip resume */
  292. return 0;
  293. }
  294. #endif
  295. static UNIVERSAL_DEV_PM_OPS(mpu3050_pm, mpu3050_suspend, mpu3050_resume, NULL);
  296. static const struct i2c_device_id mpu3050_ids[] = {
  297. { "mpu3050", 0 },
  298. { }
  299. };
  300. MODULE_DEVICE_TABLE(i2c, mpu3050_ids);
  301. static struct i2c_driver mpu3050_i2c_driver = {
  302. .driver = {
  303. .name = "mpu3050",
  304. .owner = THIS_MODULE,
  305. .pm = &mpu3050_pm,
  306. },
  307. .probe = mpu3050_probe,
  308. .remove = __devexit_p(mpu3050_remove),
  309. .id_table = mpu3050_ids,
  310. };
  311. static int __init mpu3050_init(void)
  312. {
  313. return i2c_add_driver(&mpu3050_i2c_driver);
  314. }
  315. module_init(mpu3050_init);
  316. static void __exit mpu3050_exit(void)
  317. {
  318. i2c_del_driver(&mpu3050_i2c_driver);
  319. }
  320. module_exit(mpu3050_exit);
  321. MODULE_AUTHOR("Wistron Corp.");
  322. MODULE_DESCRIPTION("MPU3050 Tri-axis gyroscope driver");
  323. MODULE_LICENSE("GPL");