kxsd9.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /*
  2. * kxsd9.c simple support for the Kionix KXSD9 3D
  3. * accelerometer.
  4. *
  5. * Copyright (c) 2008-2009 Jonathan Cameron <jic23@kernel.org>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * The i2c interface is very similar, so shouldn't be a problem once
  12. * I have a suitable wire made up.
  13. *
  14. * TODO: Support the motion detector
  15. * Uses register address incrementing so could have a
  16. * heavily optimized ring buffer access function.
  17. */
  18. #include <linux/device.h>
  19. #include <linux/kernel.h>
  20. #include <linux/spi/spi.h>
  21. #include <linux/sysfs.h>
  22. #include <linux/slab.h>
  23. #include <linux/module.h>
  24. #include <linux/iio/iio.h>
  25. #include <linux/iio/sysfs.h>
  26. #define KXSD9_REG_X 0x00
  27. #define KXSD9_REG_Y 0x02
  28. #define KXSD9_REG_Z 0x04
  29. #define KXSD9_REG_AUX 0x06
  30. #define KXSD9_REG_RESET 0x0a
  31. #define KXSD9_REG_CTRL_C 0x0c
  32. #define KXSD9_FS_MASK 0x03
  33. #define KXSD9_REG_CTRL_B 0x0d
  34. #define KXSD9_REG_CTRL_A 0x0e
  35. #define KXSD9_READ(a) (0x80 | (a))
  36. #define KXSD9_WRITE(a) (a)
  37. #define KXSD9_STATE_RX_SIZE 2
  38. #define KXSD9_STATE_TX_SIZE 2
  39. /**
  40. * struct kxsd9_state - device related storage
  41. * @buf_lock: protect the rx and tx buffers.
  42. * @us: spi device
  43. * @rx: single rx buffer storage
  44. * @tx: single tx buffer storage
  45. **/
  46. struct kxsd9_state {
  47. struct mutex buf_lock;
  48. struct spi_device *us;
  49. u8 rx[KXSD9_STATE_RX_SIZE] ____cacheline_aligned;
  50. u8 tx[KXSD9_STATE_TX_SIZE];
  51. };
  52. #define KXSD9_SCALE_2G "0.011978"
  53. #define KXSD9_SCALE_4G "0.023927"
  54. #define KXSD9_SCALE_6G "0.035934"
  55. #define KXSD9_SCALE_8G "0.047853"
  56. /* reverse order */
  57. static const int kxsd9_micro_scales[4] = { 47853, 35934, 23927, 11978 };
  58. static int kxsd9_write_scale(struct iio_dev *indio_dev, int micro)
  59. {
  60. int ret, i;
  61. struct kxsd9_state *st = iio_priv(indio_dev);
  62. bool foundit = false;
  63. for (i = 0; i < 4; i++)
  64. if (micro == kxsd9_micro_scales[i]) {
  65. foundit = true;
  66. break;
  67. }
  68. if (!foundit)
  69. return -EINVAL;
  70. mutex_lock(&st->buf_lock);
  71. ret = spi_w8r8(st->us, KXSD9_READ(KXSD9_REG_CTRL_C));
  72. if (ret)
  73. goto error_ret;
  74. st->tx[0] = KXSD9_WRITE(KXSD9_REG_CTRL_C);
  75. st->tx[1] = (ret & ~KXSD9_FS_MASK) | i;
  76. ret = spi_write(st->us, st->tx, 2);
  77. error_ret:
  78. mutex_unlock(&st->buf_lock);
  79. return ret;
  80. }
  81. static int kxsd9_read(struct iio_dev *indio_dev, u8 address)
  82. {
  83. int ret;
  84. struct kxsd9_state *st = iio_priv(indio_dev);
  85. struct spi_transfer xfers[] = {
  86. {
  87. .bits_per_word = 8,
  88. .len = 1,
  89. .delay_usecs = 200,
  90. .tx_buf = st->tx,
  91. }, {
  92. .bits_per_word = 8,
  93. .len = 2,
  94. .rx_buf = st->rx,
  95. },
  96. };
  97. mutex_lock(&st->buf_lock);
  98. st->tx[0] = KXSD9_READ(address);
  99. ret = spi_sync_transfer(st->us, xfers, ARRAY_SIZE(xfers));
  100. if (ret)
  101. return ret;
  102. return (((u16)(st->rx[0])) << 8) | (st->rx[1] & 0xF0);
  103. }
  104. static IIO_CONST_ATTR(accel_scale_available,
  105. KXSD9_SCALE_2G " "
  106. KXSD9_SCALE_4G " "
  107. KXSD9_SCALE_6G " "
  108. KXSD9_SCALE_8G);
  109. static struct attribute *kxsd9_attributes[] = {
  110. &iio_const_attr_accel_scale_available.dev_attr.attr,
  111. NULL,
  112. };
  113. static int kxsd9_write_raw(struct iio_dev *indio_dev,
  114. struct iio_chan_spec const *chan,
  115. int val,
  116. int val2,
  117. long mask)
  118. {
  119. int ret = -EINVAL;
  120. if (mask == IIO_CHAN_INFO_SCALE) {
  121. /* Check no integer component */
  122. if (val)
  123. return -EINVAL;
  124. ret = kxsd9_write_scale(indio_dev, val2);
  125. }
  126. return ret;
  127. }
  128. static int kxsd9_read_raw(struct iio_dev *indio_dev,
  129. struct iio_chan_spec const *chan,
  130. int *val, int *val2, long mask)
  131. {
  132. int ret = -EINVAL;
  133. struct kxsd9_state *st = iio_priv(indio_dev);
  134. switch (mask) {
  135. case IIO_CHAN_INFO_RAW:
  136. ret = kxsd9_read(indio_dev, chan->address);
  137. if (ret < 0)
  138. goto error_ret;
  139. *val = ret;
  140. break;
  141. case IIO_CHAN_INFO_SCALE:
  142. ret = spi_w8r8(st->us, KXSD9_READ(KXSD9_REG_CTRL_C));
  143. if (ret)
  144. goto error_ret;
  145. *val2 = kxsd9_micro_scales[ret & KXSD9_FS_MASK];
  146. ret = IIO_VAL_INT_PLUS_MICRO;
  147. break;
  148. }
  149. error_ret:
  150. return ret;
  151. };
  152. #define KXSD9_ACCEL_CHAN(axis) \
  153. { \
  154. .type = IIO_ACCEL, \
  155. .modified = 1, \
  156. .channel2 = IIO_MOD_##axis, \
  157. .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT | \
  158. IIO_CHAN_INFO_SCALE_SHARED_BIT, \
  159. .address = KXSD9_REG_##axis, \
  160. }
  161. static const struct iio_chan_spec kxsd9_channels[] = {
  162. KXSD9_ACCEL_CHAN(X), KXSD9_ACCEL_CHAN(Y), KXSD9_ACCEL_CHAN(Z),
  163. {
  164. .type = IIO_VOLTAGE,
  165. .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT,
  166. .indexed = 1,
  167. .address = KXSD9_REG_AUX,
  168. }
  169. };
  170. static const struct attribute_group kxsd9_attribute_group = {
  171. .attrs = kxsd9_attributes,
  172. };
  173. static int kxsd9_power_up(struct kxsd9_state *st)
  174. {
  175. int ret;
  176. st->tx[0] = 0x0d;
  177. st->tx[1] = 0x40;
  178. ret = spi_write(st->us, st->tx, 2);
  179. if (ret)
  180. return ret;
  181. st->tx[0] = 0x0c;
  182. st->tx[1] = 0x9b;
  183. return spi_write(st->us, st->tx, 2);
  184. };
  185. static const struct iio_info kxsd9_info = {
  186. .read_raw = &kxsd9_read_raw,
  187. .write_raw = &kxsd9_write_raw,
  188. .attrs = &kxsd9_attribute_group,
  189. .driver_module = THIS_MODULE,
  190. };
  191. static int kxsd9_probe(struct spi_device *spi)
  192. {
  193. struct iio_dev *indio_dev;
  194. struct kxsd9_state *st;
  195. int ret;
  196. indio_dev = iio_device_alloc(sizeof(*st));
  197. if (indio_dev == NULL) {
  198. ret = -ENOMEM;
  199. goto error_ret;
  200. }
  201. st = iio_priv(indio_dev);
  202. spi_set_drvdata(spi, indio_dev);
  203. st->us = spi;
  204. mutex_init(&st->buf_lock);
  205. indio_dev->channels = kxsd9_channels;
  206. indio_dev->num_channels = ARRAY_SIZE(kxsd9_channels);
  207. indio_dev->name = spi_get_device_id(spi)->name;
  208. indio_dev->dev.parent = &spi->dev;
  209. indio_dev->info = &kxsd9_info;
  210. indio_dev->modes = INDIO_DIRECT_MODE;
  211. spi->mode = SPI_MODE_0;
  212. spi_setup(spi);
  213. kxsd9_power_up(st);
  214. ret = iio_device_register(indio_dev);
  215. if (ret)
  216. goto error_free_dev;
  217. return 0;
  218. error_free_dev:
  219. iio_device_free(indio_dev);
  220. error_ret:
  221. return ret;
  222. }
  223. static int kxsd9_remove(struct spi_device *spi)
  224. {
  225. iio_device_unregister(spi_get_drvdata(spi));
  226. iio_device_free(spi_get_drvdata(spi));
  227. return 0;
  228. }
  229. static const struct spi_device_id kxsd9_id[] = {
  230. {"kxsd9", 0},
  231. { },
  232. };
  233. MODULE_DEVICE_TABLE(spi, kxsd9_id);
  234. static struct spi_driver kxsd9_driver = {
  235. .driver = {
  236. .name = "kxsd9",
  237. .owner = THIS_MODULE,
  238. },
  239. .probe = kxsd9_probe,
  240. .remove = kxsd9_remove,
  241. .id_table = kxsd9_id,
  242. };
  243. module_spi_driver(kxsd9_driver);
  244. MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
  245. MODULE_DESCRIPTION("Kionix KXSD9 SPI driver");
  246. MODULE_LICENSE("GPL v2");