bma220_spi.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. s8 buffer[16]; /* 3x8-bit channels + 5x8 padding + 8x8 timestamp */
  69. u8 tx_buf[2] ____cacheline_aligned;
  70. };
  71. static const struct iio_chan_spec bma220_channels[] = {
  72. BMA220_ACCEL_CHANNEL(0, BMA220_REG_ACCEL_X, X),
  73. BMA220_ACCEL_CHANNEL(1, BMA220_REG_ACCEL_Y, Y),
  74. BMA220_ACCEL_CHANNEL(2, BMA220_REG_ACCEL_Z, Z),
  75. IIO_CHAN_SOFT_TIMESTAMP(3),
  76. };
  77. static inline int bma220_read_reg(struct spi_device *spi, u8 reg)
  78. {
  79. return spi_w8r8(spi, reg | BMA220_READ_MASK);
  80. }
  81. static const unsigned long bma220_accel_scan_masks[] = {
  82. BIT(AXIS_X) | BIT(AXIS_Y) | BIT(AXIS_Z),
  83. 0
  84. };
  85. static irqreturn_t bma220_trigger_handler(int irq, void *p)
  86. {
  87. int ret;
  88. struct iio_poll_func *pf = p;
  89. struct iio_dev *indio_dev = pf->indio_dev;
  90. struct bma220_data *data = iio_priv(indio_dev);
  91. struct spi_device *spi = data->spi_device;
  92. mutex_lock(&data->lock);
  93. data->tx_buf[0] = BMA220_REG_ACCEL_X | BMA220_READ_MASK;
  94. ret = spi_write_then_read(spi, data->tx_buf, 1, data->buffer,
  95. ARRAY_SIZE(bma220_channels) - 1);
  96. if (ret < 0)
  97. goto err;
  98. iio_push_to_buffers_with_timestamp(indio_dev, data->buffer,
  99. pf->timestamp);
  100. err:
  101. mutex_unlock(&data->lock);
  102. iio_trigger_notify_done(indio_dev->trig);
  103. return IRQ_HANDLED;
  104. }
  105. static int bma220_read_raw(struct iio_dev *indio_dev,
  106. struct iio_chan_spec const *chan,
  107. int *val, int *val2, long mask)
  108. {
  109. int ret;
  110. u8 range_idx;
  111. struct bma220_data *data = iio_priv(indio_dev);
  112. switch (mask) {
  113. case IIO_CHAN_INFO_RAW:
  114. ret = bma220_read_reg(data->spi_device, chan->address);
  115. if (ret < 0)
  116. return -EINVAL;
  117. *val = sign_extend32(ret >> BMA220_DATA_SHIFT, 5);
  118. return IIO_VAL_INT;
  119. case IIO_CHAN_INFO_SCALE:
  120. ret = bma220_read_reg(data->spi_device, BMA220_REG_RANGE);
  121. if (ret < 0)
  122. return ret;
  123. range_idx = ret & BMA220_RANGE_MASK;
  124. *val = bma220_scale_table[range_idx][0];
  125. *val2 = bma220_scale_table[range_idx][1];
  126. return IIO_VAL_INT_PLUS_MICRO;
  127. }
  128. return -EINVAL;
  129. }
  130. static int bma220_write_raw(struct iio_dev *indio_dev,
  131. struct iio_chan_spec const *chan,
  132. int val, int val2, long mask)
  133. {
  134. int i;
  135. int ret;
  136. int index = -1;
  137. struct bma220_data *data = iio_priv(indio_dev);
  138. switch (mask) {
  139. case IIO_CHAN_INFO_SCALE:
  140. for (i = 0; i < ARRAY_SIZE(bma220_scale_table); i++)
  141. if (val == bma220_scale_table[i][0] &&
  142. val2 == bma220_scale_table[i][1]) {
  143. index = i;
  144. break;
  145. }
  146. if (index < 0)
  147. return -EINVAL;
  148. mutex_lock(&data->lock);
  149. data->tx_buf[0] = BMA220_REG_RANGE;
  150. data->tx_buf[1] = index;
  151. ret = spi_write(data->spi_device, data->tx_buf,
  152. sizeof(data->tx_buf));
  153. if (ret < 0)
  154. dev_err(&data->spi_device->dev,
  155. "failed to set measurement range\n");
  156. mutex_unlock(&data->lock);
  157. return 0;
  158. }
  159. return -EINVAL;
  160. }
  161. static const struct iio_info bma220_info = {
  162. .read_raw = bma220_read_raw,
  163. .write_raw = bma220_write_raw,
  164. .attrs = &bma220_attribute_group,
  165. };
  166. static int bma220_init(struct spi_device *spi)
  167. {
  168. int ret;
  169. ret = bma220_read_reg(spi, BMA220_REG_ID);
  170. if (ret != BMA220_CHIP_ID)
  171. return -ENODEV;
  172. /* Make sure the chip is powered on */
  173. ret = bma220_read_reg(spi, BMA220_REG_SUSPEND);
  174. if (ret < 0)
  175. return ret;
  176. else if (ret == BMA220_SUSPEND_WAKE)
  177. return bma220_read_reg(spi, BMA220_REG_SUSPEND);
  178. return 0;
  179. }
  180. static int bma220_deinit(struct spi_device *spi)
  181. {
  182. int ret;
  183. /* Make sure the chip is powered off */
  184. ret = bma220_read_reg(spi, BMA220_REG_SUSPEND);
  185. if (ret < 0)
  186. return ret;
  187. else if (ret == BMA220_SUSPEND_SLEEP)
  188. return bma220_read_reg(spi, BMA220_REG_SUSPEND);
  189. return 0;
  190. }
  191. static int bma220_probe(struct spi_device *spi)
  192. {
  193. int ret;
  194. struct iio_dev *indio_dev;
  195. struct bma220_data *data;
  196. indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*data));
  197. if (!indio_dev) {
  198. dev_err(&spi->dev, "iio allocation failed!\n");
  199. return -ENOMEM;
  200. }
  201. data = iio_priv(indio_dev);
  202. data->spi_device = spi;
  203. spi_set_drvdata(spi, indio_dev);
  204. mutex_init(&data->lock);
  205. indio_dev->dev.parent = &spi->dev;
  206. indio_dev->info = &bma220_info;
  207. indio_dev->name = BMA220_DEVICE_NAME;
  208. indio_dev->modes = INDIO_DIRECT_MODE;
  209. indio_dev->channels = bma220_channels;
  210. indio_dev->num_channels = ARRAY_SIZE(bma220_channels);
  211. indio_dev->available_scan_masks = bma220_accel_scan_masks;
  212. ret = bma220_init(data->spi_device);
  213. if (ret < 0)
  214. return ret;
  215. ret = iio_triggered_buffer_setup(indio_dev, iio_pollfunc_store_time,
  216. bma220_trigger_handler, NULL);
  217. if (ret < 0) {
  218. dev_err(&spi->dev, "iio triggered buffer setup failed\n");
  219. goto err_suspend;
  220. }
  221. ret = iio_device_register(indio_dev);
  222. if (ret < 0) {
  223. dev_err(&spi->dev, "iio_device_register failed\n");
  224. iio_triggered_buffer_cleanup(indio_dev);
  225. goto err_suspend;
  226. }
  227. return 0;
  228. err_suspend:
  229. return bma220_deinit(spi);
  230. }
  231. static int bma220_remove(struct spi_device *spi)
  232. {
  233. struct iio_dev *indio_dev = spi_get_drvdata(spi);
  234. iio_device_unregister(indio_dev);
  235. iio_triggered_buffer_cleanup(indio_dev);
  236. return bma220_deinit(spi);
  237. }
  238. #ifdef CONFIG_PM_SLEEP
  239. static int bma220_suspend(struct device *dev)
  240. {
  241. struct bma220_data *data =
  242. iio_priv(spi_get_drvdata(to_spi_device(dev)));
  243. /* The chip can be suspended/woken up by a simple register read. */
  244. return bma220_read_reg(data->spi_device, BMA220_REG_SUSPEND);
  245. }
  246. static int bma220_resume(struct device *dev)
  247. {
  248. struct bma220_data *data =
  249. iio_priv(spi_get_drvdata(to_spi_device(dev)));
  250. return bma220_read_reg(data->spi_device, BMA220_REG_SUSPEND);
  251. }
  252. static SIMPLE_DEV_PM_OPS(bma220_pm_ops, bma220_suspend, bma220_resume);
  253. #define BMA220_PM_OPS (&bma220_pm_ops)
  254. #else
  255. #define BMA220_PM_OPS NULL
  256. #endif
  257. static const struct spi_device_id bma220_spi_id[] = {
  258. {"bma220", 0},
  259. {}
  260. };
  261. static const struct acpi_device_id bma220_acpi_id[] = {
  262. {"BMA0220", 0},
  263. {}
  264. };
  265. MODULE_DEVICE_TABLE(spi, bma220_spi_id);
  266. static struct spi_driver bma220_driver = {
  267. .driver = {
  268. .name = "bma220_spi",
  269. .pm = BMA220_PM_OPS,
  270. .acpi_match_table = ACPI_PTR(bma220_acpi_id),
  271. },
  272. .probe = bma220_probe,
  273. .remove = bma220_remove,
  274. .id_table = bma220_spi_id,
  275. };
  276. module_spi_driver(bma220_driver);
  277. MODULE_AUTHOR("Tiberiu Breana <tiberiu.a.breana@intel.com>");
  278. MODULE_DESCRIPTION("BMA220 acceleration sensor driver");
  279. MODULE_LICENSE("GPL v2");