bma220_spi.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /**
  2. * BMA220 Digital triaxial acceleration sensor driver
  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. #include <linux/acpi.h>
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/iio/buffer.h>
  14. #include <linux/iio/iio.h>
  15. #include <linux/iio/sysfs.h>
  16. #include <linux/spi/spi.h>
  17. #include <linux/iio/trigger_consumer.h>
  18. #include <linux/iio/triggered_buffer.h>
  19. #define BMA220_REG_ID 0x00
  20. #define BMA220_REG_ACCEL_X 0x02
  21. #define BMA220_REG_ACCEL_Y 0x03
  22. #define BMA220_REG_ACCEL_Z 0x04
  23. #define BMA220_REG_RANGE 0x11
  24. #define BMA220_REG_SUSPEND 0x18
  25. #define BMA220_CHIP_ID 0xDD
  26. #define BMA220_READ_MASK 0x80
  27. #define BMA220_RANGE_MASK 0x03
  28. #define BMA220_DATA_SHIFT 2
  29. #define BMA220_SUSPEND_SLEEP 0xFF
  30. #define BMA220_SUSPEND_WAKE 0x00
  31. #define BMA220_DEVICE_NAME "bma220"
  32. #define BMA220_SCALE_AVAILABLE "0.623 1.248 2.491 4.983"
  33. #define BMA220_ACCEL_CHANNEL(index, reg, axis) { \
  34. .type = IIO_ACCEL, \
  35. .address = reg, \
  36. .modified = 1, \
  37. .channel2 = IIO_MOD_##axis, \
  38. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  39. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
  40. .scan_index = index, \
  41. .scan_type = { \
  42. .sign = 's', \
  43. .realbits = 6, \
  44. .storagebits = 8, \
  45. .shift = BMA220_DATA_SHIFT, \
  46. .endianness = IIO_CPU, \
  47. }, \
  48. }
  49. enum bma220_axis {
  50. AXIS_X,
  51. AXIS_Y,
  52. AXIS_Z,
  53. };
  54. static IIO_CONST_ATTR(in_accel_scale_available, BMA220_SCALE_AVAILABLE);
  55. static struct attribute *bma220_attributes[] = {
  56. &iio_const_attr_in_accel_scale_available.dev_attr.attr,
  57. NULL,
  58. };
  59. static const struct attribute_group bma220_attribute_group = {
  60. .attrs = bma220_attributes,
  61. };
  62. static const int bma220_scale_table[][4] = {
  63. {0, 623000}, {1, 248000}, {2, 491000}, {4, 983000}
  64. };
  65. struct bma220_data {
  66. struct spi_device *spi_device;
  67. struct mutex lock;
  68. struct {
  69. s8 chans[3];
  70. /* Ensure timestamp is naturally aligned. */
  71. s64 timestamp __aligned(8);
  72. } scan;
  73. u8 tx_buf[2] ____cacheline_aligned;
  74. };
  75. static const struct iio_chan_spec bma220_channels[] = {
  76. BMA220_ACCEL_CHANNEL(0, BMA220_REG_ACCEL_X, X),
  77. BMA220_ACCEL_CHANNEL(1, BMA220_REG_ACCEL_Y, Y),
  78. BMA220_ACCEL_CHANNEL(2, BMA220_REG_ACCEL_Z, Z),
  79. IIO_CHAN_SOFT_TIMESTAMP(3),
  80. };
  81. static inline int bma220_read_reg(struct spi_device *spi, u8 reg)
  82. {
  83. return spi_w8r8(spi, reg | BMA220_READ_MASK);
  84. }
  85. static const unsigned long bma220_accel_scan_masks[] = {
  86. BIT(AXIS_X) | BIT(AXIS_Y) | BIT(AXIS_Z),
  87. 0
  88. };
  89. static irqreturn_t bma220_trigger_handler(int irq, void *p)
  90. {
  91. int ret;
  92. struct iio_poll_func *pf = p;
  93. struct iio_dev *indio_dev = pf->indio_dev;
  94. struct bma220_data *data = iio_priv(indio_dev);
  95. struct spi_device *spi = data->spi_device;
  96. mutex_lock(&data->lock);
  97. data->tx_buf[0] = BMA220_REG_ACCEL_X | BMA220_READ_MASK;
  98. ret = spi_write_then_read(spi, data->tx_buf, 1, &data->scan.chans,
  99. ARRAY_SIZE(bma220_channels) - 1);
  100. if (ret < 0)
  101. goto err;
  102. iio_push_to_buffers_with_timestamp(indio_dev, &data->scan,
  103. pf->timestamp);
  104. err:
  105. mutex_unlock(&data->lock);
  106. iio_trigger_notify_done(indio_dev->trig);
  107. return IRQ_HANDLED;
  108. }
  109. static int bma220_read_raw(struct iio_dev *indio_dev,
  110. struct iio_chan_spec const *chan,
  111. int *val, int *val2, long mask)
  112. {
  113. int ret;
  114. u8 range_idx;
  115. struct bma220_data *data = iio_priv(indio_dev);
  116. switch (mask) {
  117. case IIO_CHAN_INFO_RAW:
  118. ret = bma220_read_reg(data->spi_device, chan->address);
  119. if (ret < 0)
  120. return -EINVAL;
  121. *val = sign_extend32(ret >> BMA220_DATA_SHIFT, 5);
  122. return IIO_VAL_INT;
  123. case IIO_CHAN_INFO_SCALE:
  124. ret = bma220_read_reg(data->spi_device, BMA220_REG_RANGE);
  125. if (ret < 0)
  126. return ret;
  127. range_idx = ret & BMA220_RANGE_MASK;
  128. *val = bma220_scale_table[range_idx][0];
  129. *val2 = bma220_scale_table[range_idx][1];
  130. return IIO_VAL_INT_PLUS_MICRO;
  131. }
  132. return -EINVAL;
  133. }
  134. static int bma220_write_raw(struct iio_dev *indio_dev,
  135. struct iio_chan_spec const *chan,
  136. int val, int val2, long mask)
  137. {
  138. int i;
  139. int ret;
  140. int index = -1;
  141. struct bma220_data *data = iio_priv(indio_dev);
  142. switch (mask) {
  143. case IIO_CHAN_INFO_SCALE:
  144. for (i = 0; i < ARRAY_SIZE(bma220_scale_table); i++)
  145. if (val == bma220_scale_table[i][0] &&
  146. val2 == bma220_scale_table[i][1]) {
  147. index = i;
  148. break;
  149. }
  150. if (index < 0)
  151. return -EINVAL;
  152. mutex_lock(&data->lock);
  153. data->tx_buf[0] = BMA220_REG_RANGE;
  154. data->tx_buf[1] = index;
  155. ret = spi_write(data->spi_device, data->tx_buf,
  156. sizeof(data->tx_buf));
  157. if (ret < 0)
  158. dev_err(&data->spi_device->dev,
  159. "failed to set measurement range\n");
  160. mutex_unlock(&data->lock);
  161. return 0;
  162. }
  163. return -EINVAL;
  164. }
  165. static const struct iio_info bma220_info = {
  166. .driver_module = THIS_MODULE,
  167. .read_raw = bma220_read_raw,
  168. .write_raw = bma220_write_raw,
  169. .attrs = &bma220_attribute_group,
  170. };
  171. static int bma220_init(struct spi_device *spi)
  172. {
  173. int ret;
  174. ret = bma220_read_reg(spi, BMA220_REG_ID);
  175. if (ret != BMA220_CHIP_ID)
  176. return -ENODEV;
  177. /* Make sure the chip is powered on */
  178. ret = bma220_read_reg(spi, BMA220_REG_SUSPEND);
  179. if (ret < 0)
  180. return ret;
  181. else if (ret == BMA220_SUSPEND_WAKE)
  182. return bma220_read_reg(spi, BMA220_REG_SUSPEND);
  183. return 0;
  184. }
  185. static int bma220_deinit(struct spi_device *spi)
  186. {
  187. int ret;
  188. /* Make sure the chip is powered off */
  189. ret = bma220_read_reg(spi, BMA220_REG_SUSPEND);
  190. if (ret < 0)
  191. return ret;
  192. else if (ret == BMA220_SUSPEND_SLEEP)
  193. return bma220_read_reg(spi, BMA220_REG_SUSPEND);
  194. return 0;
  195. }
  196. static int bma220_probe(struct spi_device *spi)
  197. {
  198. int ret;
  199. struct iio_dev *indio_dev;
  200. struct bma220_data *data;
  201. indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*data));
  202. if (!indio_dev) {
  203. dev_err(&spi->dev, "iio allocation failed!\n");
  204. return -ENOMEM;
  205. }
  206. data = iio_priv(indio_dev);
  207. data->spi_device = spi;
  208. spi_set_drvdata(spi, indio_dev);
  209. mutex_init(&data->lock);
  210. indio_dev->dev.parent = &spi->dev;
  211. indio_dev->info = &bma220_info;
  212. indio_dev->name = BMA220_DEVICE_NAME;
  213. indio_dev->modes = INDIO_DIRECT_MODE;
  214. indio_dev->channels = bma220_channels;
  215. indio_dev->num_channels = ARRAY_SIZE(bma220_channels);
  216. indio_dev->available_scan_masks = bma220_accel_scan_masks;
  217. ret = bma220_init(data->spi_device);
  218. if (ret < 0)
  219. return ret;
  220. ret = iio_triggered_buffer_setup(indio_dev, iio_pollfunc_store_time,
  221. bma220_trigger_handler, NULL);
  222. if (ret < 0) {
  223. dev_err(&spi->dev, "iio triggered buffer setup failed\n");
  224. goto err_suspend;
  225. }
  226. ret = iio_device_register(indio_dev);
  227. if (ret < 0) {
  228. dev_err(&spi->dev, "iio_device_register failed\n");
  229. iio_triggered_buffer_cleanup(indio_dev);
  230. goto err_suspend;
  231. }
  232. return 0;
  233. err_suspend:
  234. return bma220_deinit(spi);
  235. }
  236. static int bma220_remove(struct spi_device *spi)
  237. {
  238. struct iio_dev *indio_dev = spi_get_drvdata(spi);
  239. iio_device_unregister(indio_dev);
  240. iio_triggered_buffer_cleanup(indio_dev);
  241. return bma220_deinit(spi);
  242. }
  243. #ifdef CONFIG_PM_SLEEP
  244. static int bma220_suspend(struct device *dev)
  245. {
  246. struct bma220_data *data =
  247. iio_priv(spi_get_drvdata(to_spi_device(dev)));
  248. /* The chip can be suspended/woken up by a simple register read. */
  249. return bma220_read_reg(data->spi_device, BMA220_REG_SUSPEND);
  250. }
  251. static int bma220_resume(struct device *dev)
  252. {
  253. struct bma220_data *data =
  254. iio_priv(spi_get_drvdata(to_spi_device(dev)));
  255. return bma220_read_reg(data->spi_device, BMA220_REG_SUSPEND);
  256. }
  257. static SIMPLE_DEV_PM_OPS(bma220_pm_ops, bma220_suspend, bma220_resume);
  258. #define BMA220_PM_OPS (&bma220_pm_ops)
  259. #else
  260. #define BMA220_PM_OPS NULL
  261. #endif
  262. static const struct spi_device_id bma220_spi_id[] = {
  263. {"bma220", 0},
  264. {}
  265. };
  266. static const struct acpi_device_id bma220_acpi_id[] = {
  267. {"BMA0220", 0},
  268. {}
  269. };
  270. MODULE_DEVICE_TABLE(spi, bma220_spi_id);
  271. static struct spi_driver bma220_driver = {
  272. .driver = {
  273. .name = "bma220_spi",
  274. .pm = BMA220_PM_OPS,
  275. .acpi_match_table = ACPI_PTR(bma220_acpi_id),
  276. },
  277. .probe = bma220_probe,
  278. .remove = bma220_remove,
  279. .id_table = bma220_spi_id,
  280. };
  281. module_spi_driver(bma220_driver);
  282. MODULE_AUTHOR("Tiberiu Breana <tiberiu.a.breana@intel.com>");
  283. MODULE_DESCRIPTION("BMA220 acceleration sensor driver");
  284. MODULE_LICENSE("GPL v2");