adxl345_core.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * ADXL345 3-Axis Digital Accelerometer IIO core driver
  3. *
  4. * Copyright (c) 2017 Eva Rachel Retuya <eraretuya@gmail.com>
  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. #include <linux/module.h>
  11. #include <linux/regmap.h>
  12. #include <linux/iio/iio.h>
  13. #include "adxl345.h"
  14. #define ADXL345_REG_DEVID 0x00
  15. #define ADXL345_REG_POWER_CTL 0x2D
  16. #define ADXL345_REG_DATA_FORMAT 0x31
  17. #define ADXL345_REG_DATAX0 0x32
  18. #define ADXL345_REG_DATAY0 0x34
  19. #define ADXL345_REG_DATAZ0 0x36
  20. #define ADXL345_REG_DATA_AXIS(index) \
  21. (ADXL345_REG_DATAX0 + (index) * sizeof(__le16))
  22. #define ADXL345_POWER_CTL_MEASURE BIT(3)
  23. #define ADXL345_POWER_CTL_STANDBY 0x00
  24. #define ADXL345_DATA_FORMAT_FULL_RES BIT(3) /* Up to 13-bits resolution */
  25. #define ADXL345_DATA_FORMAT_2G 0
  26. #define ADXL345_DATA_FORMAT_4G 1
  27. #define ADXL345_DATA_FORMAT_8G 2
  28. #define ADXL345_DATA_FORMAT_16G 3
  29. #define ADXL345_DEVID 0xE5
  30. /*
  31. * In full-resolution mode, scale factor is maintained at ~4 mg/LSB
  32. * in all g ranges.
  33. *
  34. * At +/- 16g with 13-bit resolution, scale is computed as:
  35. * (16 + 16) * 9.81 / (2^13 - 1) = 0.0383
  36. */
  37. static const int adxl345_uscale = 38300;
  38. struct adxl345_data {
  39. struct regmap *regmap;
  40. u8 data_range;
  41. };
  42. #define ADXL345_CHANNEL(index, axis) { \
  43. .type = IIO_ACCEL, \
  44. .modified = 1, \
  45. .channel2 = IIO_MOD_##axis, \
  46. .address = index, \
  47. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  48. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
  49. }
  50. static const struct iio_chan_spec adxl345_channels[] = {
  51. ADXL345_CHANNEL(0, X),
  52. ADXL345_CHANNEL(1, Y),
  53. ADXL345_CHANNEL(2, Z),
  54. };
  55. static int adxl345_read_raw(struct iio_dev *indio_dev,
  56. struct iio_chan_spec const *chan,
  57. int *val, int *val2, long mask)
  58. {
  59. struct adxl345_data *data = iio_priv(indio_dev);
  60. __le16 accel;
  61. int ret;
  62. switch (mask) {
  63. case IIO_CHAN_INFO_RAW:
  64. /*
  65. * Data is stored in adjacent registers:
  66. * ADXL345_REG_DATA(X0/Y0/Z0) contain the least significant byte
  67. * and ADXL345_REG_DATA(X0/Y0/Z0) + 1 the most significant byte
  68. */
  69. ret = regmap_bulk_read(data->regmap,
  70. ADXL345_REG_DATA_AXIS(chan->address),
  71. &accel, sizeof(accel));
  72. if (ret < 0)
  73. return ret;
  74. *val = sign_extend32(le16_to_cpu(accel), 12);
  75. return IIO_VAL_INT;
  76. case IIO_CHAN_INFO_SCALE:
  77. *val = 0;
  78. *val2 = adxl345_uscale;
  79. return IIO_VAL_INT_PLUS_MICRO;
  80. }
  81. return -EINVAL;
  82. }
  83. static const struct iio_info adxl345_info = {
  84. .driver_module = THIS_MODULE,
  85. .read_raw = adxl345_read_raw,
  86. };
  87. int adxl345_core_probe(struct device *dev, struct regmap *regmap,
  88. const char *name)
  89. {
  90. struct adxl345_data *data;
  91. struct iio_dev *indio_dev;
  92. u32 regval;
  93. int ret;
  94. ret = regmap_read(regmap, ADXL345_REG_DEVID, &regval);
  95. if (ret < 0) {
  96. dev_err(dev, "Error reading device ID: %d\n", ret);
  97. return ret;
  98. }
  99. if (regval != ADXL345_DEVID) {
  100. dev_err(dev, "Invalid device ID: %x, expected %x\n",
  101. regval, ADXL345_DEVID);
  102. return -ENODEV;
  103. }
  104. indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
  105. if (!indio_dev)
  106. return -ENOMEM;
  107. data = iio_priv(indio_dev);
  108. dev_set_drvdata(dev, indio_dev);
  109. data->regmap = regmap;
  110. /* Enable full-resolution mode */
  111. data->data_range = ADXL345_DATA_FORMAT_FULL_RES;
  112. ret = regmap_write(data->regmap, ADXL345_REG_DATA_FORMAT,
  113. data->data_range);
  114. if (ret < 0) {
  115. dev_err(dev, "Failed to set data range: %d\n", ret);
  116. return ret;
  117. }
  118. indio_dev->dev.parent = dev;
  119. indio_dev->name = name;
  120. indio_dev->info = &adxl345_info;
  121. indio_dev->modes = INDIO_DIRECT_MODE;
  122. indio_dev->channels = adxl345_channels;
  123. indio_dev->num_channels = ARRAY_SIZE(adxl345_channels);
  124. /* Enable measurement mode */
  125. ret = regmap_write(data->regmap, ADXL345_REG_POWER_CTL,
  126. ADXL345_POWER_CTL_MEASURE);
  127. if (ret < 0) {
  128. dev_err(dev, "Failed to enable measurement mode: %d\n", ret);
  129. return ret;
  130. }
  131. ret = iio_device_register(indio_dev);
  132. if (ret < 0) {
  133. dev_err(dev, "iio_device_register failed: %d\n", ret);
  134. regmap_write(data->regmap, ADXL345_REG_POWER_CTL,
  135. ADXL345_POWER_CTL_STANDBY);
  136. }
  137. return ret;
  138. }
  139. EXPORT_SYMBOL_GPL(adxl345_core_probe);
  140. int adxl345_core_remove(struct device *dev)
  141. {
  142. struct iio_dev *indio_dev = dev_get_drvdata(dev);
  143. struct adxl345_data *data = iio_priv(indio_dev);
  144. iio_device_unregister(indio_dev);
  145. return regmap_write(data->regmap, ADXL345_REG_POWER_CTL,
  146. ADXL345_POWER_CTL_STANDBY);
  147. }
  148. EXPORT_SYMBOL_GPL(adxl345_core_remove);
  149. MODULE_AUTHOR("Eva Rachel Retuya <eraretuya@gmail.com>");
  150. MODULE_DESCRIPTION("ADXL345 3-Axis Digital Accelerometer core driver");
  151. MODULE_LICENSE("GPL v2");