mma7660.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /**
  2. * Freescale MMA7660FC 3-Axis Accelerometer
  3. *
  4. * Copyright (c) 2016, Intel Corporation.
  5. *
  6. * This file is subject to the terms and conditions of version 2 of
  7. * the GNU General Public License. See the file COPYING in the main
  8. * directory of this archive for more details.
  9. *
  10. * IIO driver for Freescale MMA7660FC; 7-bit I2C address: 0x4c.
  11. */
  12. #include <linux/acpi.h>
  13. #include <linux/i2c.h>
  14. #include <linux/module.h>
  15. #include <linux/iio/iio.h>
  16. #include <linux/iio/sysfs.h>
  17. #define MMA7660_DRIVER_NAME "mma7660"
  18. #define MMA7660_REG_XOUT 0x00
  19. #define MMA7660_REG_YOUT 0x01
  20. #define MMA7660_REG_ZOUT 0x02
  21. #define MMA7660_REG_OUT_BIT_ALERT BIT(6)
  22. #define MMA7660_REG_MODE 0x07
  23. #define MMA7660_REG_MODE_BIT_MODE BIT(0)
  24. #define MMA7660_REG_MODE_BIT_TON BIT(2)
  25. #define MMA7660_I2C_READ_RETRIES 5
  26. /*
  27. * The accelerometer has one measurement range:
  28. *
  29. * -1.5g - +1.5g (6-bit, signed)
  30. *
  31. * scale = (1.5 + 1.5) * 9.81 / (2^6 - 1) = 0.467142857
  32. */
  33. #define MMA7660_SCALE_AVAIL "0.467142857"
  34. static const int mma7660_nscale = 467142857;
  35. #define MMA7660_CHANNEL(reg, axis) { \
  36. .type = IIO_ACCEL, \
  37. .address = reg, \
  38. .modified = 1, \
  39. .channel2 = IIO_MOD_##axis, \
  40. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  41. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
  42. }
  43. static const struct iio_chan_spec mma7660_channels[] = {
  44. MMA7660_CHANNEL(MMA7660_REG_XOUT, X),
  45. MMA7660_CHANNEL(MMA7660_REG_YOUT, Y),
  46. MMA7660_CHANNEL(MMA7660_REG_ZOUT, Z),
  47. };
  48. enum mma7660_mode {
  49. MMA7660_MODE_STANDBY,
  50. MMA7660_MODE_ACTIVE
  51. };
  52. struct mma7660_data {
  53. struct i2c_client *client;
  54. struct mutex lock;
  55. enum mma7660_mode mode;
  56. };
  57. static IIO_CONST_ATTR(in_accel_scale_available, MMA7660_SCALE_AVAIL);
  58. static struct attribute *mma7660_attributes[] = {
  59. &iio_const_attr_in_accel_scale_available.dev_attr.attr,
  60. NULL,
  61. };
  62. static const struct attribute_group mma7660_attribute_group = {
  63. .attrs = mma7660_attributes
  64. };
  65. static int mma7660_set_mode(struct mma7660_data *data,
  66. enum mma7660_mode mode)
  67. {
  68. int ret;
  69. struct i2c_client *client = data->client;
  70. if (mode == data->mode)
  71. return 0;
  72. ret = i2c_smbus_read_byte_data(client, MMA7660_REG_MODE);
  73. if (ret < 0) {
  74. dev_err(&client->dev, "failed to read sensor mode\n");
  75. return ret;
  76. }
  77. if (mode == MMA7660_MODE_ACTIVE) {
  78. ret &= ~MMA7660_REG_MODE_BIT_TON;
  79. ret |= MMA7660_REG_MODE_BIT_MODE;
  80. } else {
  81. ret &= ~MMA7660_REG_MODE_BIT_TON;
  82. ret &= ~MMA7660_REG_MODE_BIT_MODE;
  83. }
  84. ret = i2c_smbus_write_byte_data(client, MMA7660_REG_MODE, ret);
  85. if (ret < 0) {
  86. dev_err(&client->dev, "failed to change sensor mode\n");
  87. return ret;
  88. }
  89. data->mode = mode;
  90. return ret;
  91. }
  92. static int mma7660_read_accel(struct mma7660_data *data, u8 address)
  93. {
  94. int ret, retries = MMA7660_I2C_READ_RETRIES;
  95. struct i2c_client *client = data->client;
  96. /*
  97. * Read data. If the Alert bit is set, the register was read at
  98. * the same time as the device was attempting to update the content.
  99. * The solution is to read the register again. Do this only
  100. * MMA7660_I2C_READ_RETRIES times to avoid spending too much time
  101. * in the kernel.
  102. */
  103. do {
  104. ret = i2c_smbus_read_byte_data(client, address);
  105. if (ret < 0) {
  106. dev_err(&client->dev, "register read failed\n");
  107. return ret;
  108. }
  109. } while (retries-- > 0 && ret & MMA7660_REG_OUT_BIT_ALERT);
  110. if (ret & MMA7660_REG_OUT_BIT_ALERT) {
  111. dev_err(&client->dev, "all register read retries failed\n");
  112. return -ETIMEDOUT;
  113. }
  114. return ret;
  115. }
  116. static int mma7660_read_raw(struct iio_dev *indio_dev,
  117. struct iio_chan_spec const *chan,
  118. int *val, int *val2, long mask)
  119. {
  120. struct mma7660_data *data = iio_priv(indio_dev);
  121. int ret;
  122. switch (mask) {
  123. case IIO_CHAN_INFO_RAW:
  124. mutex_lock(&data->lock);
  125. ret = mma7660_read_accel(data, chan->address);
  126. mutex_unlock(&data->lock);
  127. if (ret < 0)
  128. return ret;
  129. *val = sign_extend32(ret, 5);
  130. return IIO_VAL_INT;
  131. case IIO_CHAN_INFO_SCALE:
  132. *val = 0;
  133. *val2 = mma7660_nscale;
  134. return IIO_VAL_INT_PLUS_NANO;
  135. default:
  136. return -EINVAL;
  137. }
  138. return -EINVAL;
  139. }
  140. static const struct iio_info mma7660_info = {
  141. .driver_module = THIS_MODULE,
  142. .read_raw = mma7660_read_raw,
  143. .attrs = &mma7660_attribute_group,
  144. };
  145. static int mma7660_probe(struct i2c_client *client,
  146. const struct i2c_device_id *id)
  147. {
  148. int ret;
  149. struct iio_dev *indio_dev;
  150. struct mma7660_data *data;
  151. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
  152. if (!indio_dev) {
  153. dev_err(&client->dev, "iio allocation failed!\n");
  154. return -ENOMEM;
  155. }
  156. data = iio_priv(indio_dev);
  157. data->client = client;
  158. i2c_set_clientdata(client, indio_dev);
  159. mutex_init(&data->lock);
  160. data->mode = MMA7660_MODE_STANDBY;
  161. indio_dev->dev.parent = &client->dev;
  162. indio_dev->info = &mma7660_info;
  163. indio_dev->name = MMA7660_DRIVER_NAME;
  164. indio_dev->modes = INDIO_DIRECT_MODE;
  165. indio_dev->channels = mma7660_channels;
  166. indio_dev->num_channels = ARRAY_SIZE(mma7660_channels);
  167. ret = mma7660_set_mode(data, MMA7660_MODE_ACTIVE);
  168. if (ret < 0)
  169. return ret;
  170. ret = iio_device_register(indio_dev);
  171. if (ret < 0) {
  172. dev_err(&client->dev, "device_register failed\n");
  173. mma7660_set_mode(data, MMA7660_MODE_STANDBY);
  174. }
  175. return ret;
  176. }
  177. static int mma7660_remove(struct i2c_client *client)
  178. {
  179. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  180. iio_device_unregister(indio_dev);
  181. return mma7660_set_mode(iio_priv(indio_dev), MMA7660_MODE_STANDBY);
  182. }
  183. #ifdef CONFIG_PM_SLEEP
  184. static int mma7660_suspend(struct device *dev)
  185. {
  186. struct mma7660_data *data;
  187. data = iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
  188. return mma7660_set_mode(data, MMA7660_MODE_STANDBY);
  189. }
  190. static int mma7660_resume(struct device *dev)
  191. {
  192. struct mma7660_data *data;
  193. data = iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
  194. return mma7660_set_mode(data, MMA7660_MODE_ACTIVE);
  195. }
  196. static SIMPLE_DEV_PM_OPS(mma7660_pm_ops, mma7660_suspend, mma7660_resume);
  197. #define MMA7660_PM_OPS (&mma7660_pm_ops)
  198. #else
  199. #define MMA7660_PM_OPS NULL
  200. #endif
  201. static const struct i2c_device_id mma7660_i2c_id[] = {
  202. {"mma7660", 0},
  203. {}
  204. };
  205. MODULE_DEVICE_TABLE(i2c, mma7660_i2c_id);
  206. static const struct of_device_id mma7660_of_match[] = {
  207. { .compatible = "fsl,mma7660" },
  208. { }
  209. };
  210. MODULE_DEVICE_TABLE(of, mma7660_of_match);
  211. static const struct acpi_device_id mma7660_acpi_id[] = {
  212. {"MMA7660", 0},
  213. {}
  214. };
  215. MODULE_DEVICE_TABLE(acpi, mma7660_acpi_id);
  216. static struct i2c_driver mma7660_driver = {
  217. .driver = {
  218. .name = "mma7660",
  219. .pm = MMA7660_PM_OPS,
  220. .of_match_table = mma7660_of_match,
  221. .acpi_match_table = ACPI_PTR(mma7660_acpi_id),
  222. },
  223. .probe = mma7660_probe,
  224. .remove = mma7660_remove,
  225. .id_table = mma7660_i2c_id,
  226. };
  227. module_i2c_driver(mma7660_driver);
  228. MODULE_AUTHOR("Constantin Musca <constantin.musca@intel.com>");
  229. MODULE_DESCRIPTION("Freescale MMA7660FC 3-Axis Accelerometer driver");
  230. MODULE_LICENSE("GPL v2");