mma8450.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * Driver for Freescale's 3-Axis Accelerometer MMA8450
  3. *
  4. * Copyright (C) 2011 Freescale Semiconductor, Inc. All Rights Reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20. #include <linux/kernel.h>
  21. #include <linux/module.h>
  22. #include <linux/slab.h>
  23. #include <linux/delay.h>
  24. #include <linux/i2c.h>
  25. #include <linux/input-polldev.h>
  26. #include <linux/of_device.h>
  27. #define MMA8450_DRV_NAME "mma8450"
  28. #define MODE_CHANGE_DELAY_MS 100
  29. #define POLL_INTERVAL 100
  30. #define POLL_INTERVAL_MAX 500
  31. /* register definitions */
  32. #define MMA8450_STATUS 0x00
  33. #define MMA8450_STATUS_ZXYDR 0x08
  34. #define MMA8450_OUT_X8 0x01
  35. #define MMA8450_OUT_Y8 0x02
  36. #define MMA8450_OUT_Z8 0x03
  37. #define MMA8450_OUT_X_LSB 0x05
  38. #define MMA8450_OUT_X_MSB 0x06
  39. #define MMA8450_OUT_Y_LSB 0x07
  40. #define MMA8450_OUT_Y_MSB 0x08
  41. #define MMA8450_OUT_Z_LSB 0x09
  42. #define MMA8450_OUT_Z_MSB 0x0a
  43. #define MMA8450_XYZ_DATA_CFG 0x16
  44. #define MMA8450_CTRL_REG1 0x38
  45. #define MMA8450_CTRL_REG2 0x39
  46. /* mma8450 status */
  47. struct mma8450 {
  48. struct i2c_client *client;
  49. struct input_polled_dev *idev;
  50. };
  51. static int mma8450_read(struct mma8450 *m, unsigned off)
  52. {
  53. struct i2c_client *c = m->client;
  54. int ret;
  55. ret = i2c_smbus_read_byte_data(c, off);
  56. if (ret < 0)
  57. dev_err(&c->dev,
  58. "failed to read register 0x%02x, error %d\n",
  59. off, ret);
  60. return ret;
  61. }
  62. static int mma8450_write(struct mma8450 *m, unsigned off, u8 v)
  63. {
  64. struct i2c_client *c = m->client;
  65. int error;
  66. error = i2c_smbus_write_byte_data(c, off, v);
  67. if (error < 0) {
  68. dev_err(&c->dev,
  69. "failed to write to register 0x%02x, error %d\n",
  70. off, error);
  71. return error;
  72. }
  73. return 0;
  74. }
  75. static int mma8450_read_block(struct mma8450 *m, unsigned off,
  76. u8 *buf, size_t size)
  77. {
  78. struct i2c_client *c = m->client;
  79. int err;
  80. err = i2c_smbus_read_i2c_block_data(c, off, size, buf);
  81. if (err < 0) {
  82. dev_err(&c->dev,
  83. "failed to read block data at 0x%02x, error %d\n",
  84. MMA8450_OUT_X_LSB, err);
  85. return err;
  86. }
  87. return 0;
  88. }
  89. static void mma8450_poll(struct input_polled_dev *dev)
  90. {
  91. struct mma8450 *m = dev->private;
  92. int x, y, z;
  93. int ret;
  94. u8 buf[6];
  95. ret = mma8450_read(m, MMA8450_STATUS);
  96. if (ret < 0)
  97. return;
  98. if (!(ret & MMA8450_STATUS_ZXYDR))
  99. return;
  100. ret = mma8450_read_block(m, MMA8450_OUT_X_LSB, buf, sizeof(buf));
  101. if (ret < 0)
  102. return;
  103. x = ((int)(s8)buf[1] << 4) | (buf[0] & 0xf);
  104. y = ((int)(s8)buf[3] << 4) | (buf[2] & 0xf);
  105. z = ((int)(s8)buf[5] << 4) | (buf[4] & 0xf);
  106. input_report_abs(dev->input, ABS_X, x);
  107. input_report_abs(dev->input, ABS_Y, y);
  108. input_report_abs(dev->input, ABS_Z, z);
  109. input_sync(dev->input);
  110. }
  111. /* Initialize the MMA8450 chip */
  112. static void mma8450_open(struct input_polled_dev *dev)
  113. {
  114. struct mma8450 *m = dev->private;
  115. int err;
  116. /* enable all events from X/Y/Z, no FIFO */
  117. err = mma8450_write(m, MMA8450_XYZ_DATA_CFG, 0x07);
  118. if (err)
  119. return;
  120. /*
  121. * Sleep mode poll rate - 50Hz
  122. * System output data rate - 400Hz
  123. * Full scale selection - Active, +/- 2G
  124. */
  125. err = mma8450_write(m, MMA8450_CTRL_REG1, 0x01);
  126. if (err < 0)
  127. return;
  128. msleep(MODE_CHANGE_DELAY_MS);
  129. }
  130. static void mma8450_close(struct input_polled_dev *dev)
  131. {
  132. struct mma8450 *m = dev->private;
  133. mma8450_write(m, MMA8450_CTRL_REG1, 0x00);
  134. mma8450_write(m, MMA8450_CTRL_REG2, 0x01);
  135. }
  136. /*
  137. * I2C init/probing/exit functions
  138. */
  139. static int mma8450_probe(struct i2c_client *c,
  140. const struct i2c_device_id *id)
  141. {
  142. struct input_polled_dev *idev;
  143. struct mma8450 *m;
  144. int err;
  145. m = devm_kzalloc(&c->dev, sizeof(*m), GFP_KERNEL);
  146. if (!m)
  147. return -ENOMEM;
  148. idev = devm_input_allocate_polled_device(&c->dev);
  149. if (!idev)
  150. return -ENOMEM;
  151. m->client = c;
  152. m->idev = idev;
  153. idev->private = m;
  154. idev->input->name = MMA8450_DRV_NAME;
  155. idev->input->id.bustype = BUS_I2C;
  156. idev->poll = mma8450_poll;
  157. idev->poll_interval = POLL_INTERVAL;
  158. idev->poll_interval_max = POLL_INTERVAL_MAX;
  159. idev->open = mma8450_open;
  160. idev->close = mma8450_close;
  161. __set_bit(EV_ABS, idev->input->evbit);
  162. input_set_abs_params(idev->input, ABS_X, -2048, 2047, 32, 32);
  163. input_set_abs_params(idev->input, ABS_Y, -2048, 2047, 32, 32);
  164. input_set_abs_params(idev->input, ABS_Z, -2048, 2047, 32, 32);
  165. err = input_register_polled_device(idev);
  166. if (err) {
  167. dev_err(&c->dev, "failed to register polled input device\n");
  168. return err;
  169. }
  170. i2c_set_clientdata(c, m);
  171. return 0;
  172. }
  173. static const struct i2c_device_id mma8450_id[] = {
  174. { MMA8450_DRV_NAME, 0 },
  175. { },
  176. };
  177. MODULE_DEVICE_TABLE(i2c, mma8450_id);
  178. static const struct of_device_id mma8450_dt_ids[] = {
  179. { .compatible = "fsl,mma8450", },
  180. { /* sentinel */ }
  181. };
  182. MODULE_DEVICE_TABLE(of, mma8450_dt_ids);
  183. static struct i2c_driver mma8450_driver = {
  184. .driver = {
  185. .name = MMA8450_DRV_NAME,
  186. .of_match_table = mma8450_dt_ids,
  187. },
  188. .probe = mma8450_probe,
  189. .id_table = mma8450_id,
  190. };
  191. module_i2c_driver(mma8450_driver);
  192. MODULE_AUTHOR("Freescale Semiconductor, Inc.");
  193. MODULE_DESCRIPTION("MMA8450 3-Axis Accelerometer Driver");
  194. MODULE_LICENSE("GPL");