mma8450.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Driver for Freescale's 3-Axis Accelerometer MMA8450
  4. *
  5. * Copyright (C) 2011 Freescale Semiconductor, Inc. All Rights Reserved.
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/module.h>
  9. #include <linux/slab.h>
  10. #include <linux/delay.h>
  11. #include <linux/i2c.h>
  12. #include <linux/input.h>
  13. #include <linux/mod_devicetable.h>
  14. #define MMA8450_DRV_NAME "mma8450"
  15. #define MODE_CHANGE_DELAY_MS 100
  16. #define POLL_INTERVAL 100
  17. #define POLL_INTERVAL_MAX 500
  18. /* register definitions */
  19. #define MMA8450_STATUS 0x00
  20. #define MMA8450_STATUS_ZXYDR 0x08
  21. #define MMA8450_OUT_X8 0x01
  22. #define MMA8450_OUT_Y8 0x02
  23. #define MMA8450_OUT_Z8 0x03
  24. #define MMA8450_OUT_X_LSB 0x05
  25. #define MMA8450_OUT_X_MSB 0x06
  26. #define MMA8450_OUT_Y_LSB 0x07
  27. #define MMA8450_OUT_Y_MSB 0x08
  28. #define MMA8450_OUT_Z_LSB 0x09
  29. #define MMA8450_OUT_Z_MSB 0x0a
  30. #define MMA8450_XYZ_DATA_CFG 0x16
  31. #define MMA8450_CTRL_REG1 0x38
  32. #define MMA8450_CTRL_REG2 0x39
  33. #define MMA8450_ID 0xc6
  34. #define MMA8450_WHO_AM_I 0x0f
  35. static int mma8450_read(struct i2c_client *c, unsigned int off)
  36. {
  37. int ret;
  38. ret = i2c_smbus_read_byte_data(c, off);
  39. if (ret < 0)
  40. dev_err(&c->dev,
  41. "failed to read register 0x%02x, error %d\n",
  42. off, ret);
  43. return ret;
  44. }
  45. static int mma8450_write(struct i2c_client *c, unsigned int off, u8 v)
  46. {
  47. int error;
  48. error = i2c_smbus_write_byte_data(c, off, v);
  49. if (error < 0) {
  50. dev_err(&c->dev,
  51. "failed to write to register 0x%02x, error %d\n",
  52. off, error);
  53. return error;
  54. }
  55. return 0;
  56. }
  57. static int mma8450_read_block(struct i2c_client *c, unsigned int off,
  58. u8 *buf, size_t size)
  59. {
  60. int err;
  61. err = i2c_smbus_read_i2c_block_data(c, off, size, buf);
  62. if (err < 0) {
  63. dev_err(&c->dev,
  64. "failed to read block data at 0x%02x, error %d\n",
  65. MMA8450_OUT_X_LSB, err);
  66. return err;
  67. }
  68. return 0;
  69. }
  70. static void mma8450_poll(struct input_dev *input)
  71. {
  72. struct i2c_client *c = input_get_drvdata(input);
  73. int x, y, z;
  74. int ret;
  75. u8 buf[6];
  76. ret = mma8450_read(c, MMA8450_STATUS);
  77. if (ret < 0)
  78. return;
  79. if (!(ret & MMA8450_STATUS_ZXYDR))
  80. return;
  81. ret = mma8450_read_block(c, MMA8450_OUT_X_LSB, buf, sizeof(buf));
  82. if (ret < 0)
  83. return;
  84. x = ((int)(s8)buf[1] << 4) | (buf[0] & 0xf);
  85. y = ((int)(s8)buf[3] << 4) | (buf[2] & 0xf);
  86. z = ((int)(s8)buf[5] << 4) | (buf[4] & 0xf);
  87. input_report_abs(input, ABS_X, x);
  88. input_report_abs(input, ABS_Y, y);
  89. input_report_abs(input, ABS_Z, z);
  90. input_sync(input);
  91. }
  92. /* Initialize the MMA8450 chip */
  93. static int mma8450_open(struct input_dev *input)
  94. {
  95. struct i2c_client *c = input_get_drvdata(input);
  96. int err;
  97. /* enable all events from X/Y/Z, no FIFO */
  98. err = mma8450_write(c, MMA8450_XYZ_DATA_CFG, 0x07);
  99. if (err)
  100. return err;
  101. /*
  102. * Sleep mode poll rate - 50Hz
  103. * System output data rate - 400Hz
  104. * Full scale selection - Active, +/- 2G
  105. */
  106. err = mma8450_write(c, MMA8450_CTRL_REG1, 0x01);
  107. if (err)
  108. return err;
  109. msleep(MODE_CHANGE_DELAY_MS);
  110. return 0;
  111. }
  112. static void mma8450_close(struct input_dev *input)
  113. {
  114. struct i2c_client *c = input_get_drvdata(input);
  115. mma8450_write(c, MMA8450_CTRL_REG1, 0x00);
  116. mma8450_write(c, MMA8450_CTRL_REG2, 0x01);
  117. }
  118. /*
  119. * I2C init/probing/exit functions
  120. */
  121. static int mma8450_probe(struct i2c_client *c)
  122. {
  123. struct i2c_adapter *adapter = c->adapter;
  124. struct input_dev *input;
  125. int err, client_id;
  126. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE |
  127. I2C_FUNC_SMBUS_BYTE_DATA))
  128. return dev_err_probe(&c->dev, -EINVAL,
  129. "I2C adapter doesn't support SMBUS BYTE");
  130. client_id = i2c_smbus_read_byte_data(c, MMA8450_WHO_AM_I);
  131. if (client_id != MMA8450_ID)
  132. return dev_err_probe(&c->dev, -EINVAL,
  133. "unexpected chip ID 0x%x (vs 0x%x)\n",
  134. client_id, MMA8450_ID);
  135. input = devm_input_allocate_device(&c->dev);
  136. if (!input)
  137. return -ENOMEM;
  138. input_set_drvdata(input, c);
  139. input->name = MMA8450_DRV_NAME;
  140. input->id.bustype = BUS_I2C;
  141. input->open = mma8450_open;
  142. input->close = mma8450_close;
  143. input_set_abs_params(input, ABS_X, -2048, 2047, 32, 32);
  144. input_set_abs_params(input, ABS_Y, -2048, 2047, 32, 32);
  145. input_set_abs_params(input, ABS_Z, -2048, 2047, 32, 32);
  146. err = input_setup_polling(input, mma8450_poll);
  147. if (err) {
  148. dev_err(&c->dev, "failed to set up polling\n");
  149. return err;
  150. }
  151. input_set_poll_interval(input, POLL_INTERVAL);
  152. input_set_max_poll_interval(input, POLL_INTERVAL_MAX);
  153. err = input_register_device(input);
  154. if (err) {
  155. dev_err(&c->dev, "failed to register input device\n");
  156. return err;
  157. }
  158. return 0;
  159. }
  160. static const struct i2c_device_id mma8450_id[] = {
  161. { MMA8450_DRV_NAME },
  162. { }
  163. };
  164. MODULE_DEVICE_TABLE(i2c, mma8450_id);
  165. static const struct of_device_id mma8450_dt_ids[] = {
  166. { .compatible = "fsl,mma8450", },
  167. { /* sentinel */ }
  168. };
  169. MODULE_DEVICE_TABLE(of, mma8450_dt_ids);
  170. static struct i2c_driver mma8450_driver = {
  171. .driver = {
  172. .name = MMA8450_DRV_NAME,
  173. .of_match_table = mma8450_dt_ids,
  174. },
  175. .probe = mma8450_probe,
  176. .id_table = mma8450_id,
  177. };
  178. module_i2c_driver(mma8450_driver);
  179. MODULE_AUTHOR("Freescale Semiconductor, Inc.");
  180. MODULE_DESCRIPTION("MMA8450 3-Axis Accelerometer Driver");
  181. MODULE_LICENSE("GPL");