kxsd9.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. struct spi_message msg;
  84. int ret;
  85. struct kxsd9_state *st = iio_priv(indio_dev);
  86. struct spi_transfer xfers[] = {
  87. {
  88. .bits_per_word = 8,
  89. .len = 1,
  90. .delay_usecs = 200,
  91. .tx_buf = st->tx,
  92. }, {
  93. .bits_per_word = 8,
  94. .len = 2,
  95. .rx_buf = st->rx,
  96. },
  97. };
  98. mutex_lock(&st->buf_lock);
  99. st->tx[0] = KXSD9_READ(address);
  100. spi_message_init(&msg);
  101. spi_message_add_tail(&xfers[0], &msg);
  102. spi_message_add_tail(&xfers[1], &msg);
  103. ret = spi_sync(st->us, &msg);
  104. if (ret)
  105. return ret;
  106. return (((u16)(st->rx[0])) << 8) | (st->rx[1] & 0xF0);
  107. }
  108. static IIO_CONST_ATTR(accel_scale_available,
  109. KXSD9_SCALE_2G " "
  110. KXSD9_SCALE_4G " "
  111. KXSD9_SCALE_6G " "
  112. KXSD9_SCALE_8G);
  113. static struct attribute *kxsd9_attributes[] = {
  114. &iio_const_attr_accel_scale_available.dev_attr.attr,
  115. NULL,
  116. };
  117. static int kxsd9_write_raw(struct iio_dev *indio_dev,
  118. struct iio_chan_spec const *chan,
  119. int val,
  120. int val2,
  121. long mask)
  122. {
  123. int ret = -EINVAL;
  124. if (mask == IIO_CHAN_INFO_SCALE) {
  125. /* Check no integer component */
  126. if (val)
  127. return -EINVAL;
  128. ret = kxsd9_write_scale(indio_dev, val2);
  129. }
  130. return ret;
  131. }
  132. static int kxsd9_read_raw(struct iio_dev *indio_dev,
  133. struct iio_chan_spec const *chan,
  134. int *val, int *val2, long mask)
  135. {
  136. int ret = -EINVAL;
  137. struct kxsd9_state *st = iio_priv(indio_dev);
  138. switch (mask) {
  139. case IIO_CHAN_INFO_RAW:
  140. ret = kxsd9_read(indio_dev, chan->address);
  141. if (ret < 0)
  142. goto error_ret;
  143. *val = ret;
  144. break;
  145. case IIO_CHAN_INFO_SCALE:
  146. ret = spi_w8r8(st->us, KXSD9_READ(KXSD9_REG_CTRL_C));
  147. if (ret)
  148. goto error_ret;
  149. *val2 = kxsd9_micro_scales[ret & KXSD9_FS_MASK];
  150. ret = IIO_VAL_INT_PLUS_MICRO;
  151. break;
  152. }
  153. error_ret:
  154. return ret;
  155. };
  156. #define KXSD9_ACCEL_CHAN(axis) \
  157. { \
  158. .type = IIO_ACCEL, \
  159. .modified = 1, \
  160. .channel2 = IIO_MOD_##axis, \
  161. .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT | \
  162. IIO_CHAN_INFO_SCALE_SHARED_BIT, \
  163. .address = KXSD9_REG_##axis, \
  164. }
  165. static const struct iio_chan_spec kxsd9_channels[] = {
  166. KXSD9_ACCEL_CHAN(X), KXSD9_ACCEL_CHAN(Y), KXSD9_ACCEL_CHAN(Z),
  167. {
  168. .type = IIO_VOLTAGE,
  169. .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT,
  170. .indexed = 1,
  171. .address = KXSD9_REG_AUX,
  172. }
  173. };
  174. static const struct attribute_group kxsd9_attribute_group = {
  175. .attrs = kxsd9_attributes,
  176. };
  177. static int kxsd9_power_up(struct kxsd9_state *st)
  178. {
  179. int ret;
  180. st->tx[0] = 0x0d;
  181. st->tx[1] = 0x40;
  182. ret = spi_write(st->us, st->tx, 2);
  183. if (ret)
  184. return ret;
  185. st->tx[0] = 0x0c;
  186. st->tx[1] = 0x9b;
  187. return spi_write(st->us, st->tx, 2);
  188. };
  189. static const struct iio_info kxsd9_info = {
  190. .read_raw = &kxsd9_read_raw,
  191. .write_raw = &kxsd9_write_raw,
  192. .attrs = &kxsd9_attribute_group,
  193. .driver_module = THIS_MODULE,
  194. };
  195. static int kxsd9_probe(struct spi_device *spi)
  196. {
  197. struct iio_dev *indio_dev;
  198. struct kxsd9_state *st;
  199. int ret;
  200. indio_dev = iio_device_alloc(sizeof(*st));
  201. if (indio_dev == NULL) {
  202. ret = -ENOMEM;
  203. goto error_ret;
  204. }
  205. st = iio_priv(indio_dev);
  206. spi_set_drvdata(spi, indio_dev);
  207. st->us = spi;
  208. mutex_init(&st->buf_lock);
  209. indio_dev->channels = kxsd9_channels;
  210. indio_dev->num_channels = ARRAY_SIZE(kxsd9_channels);
  211. indio_dev->name = spi_get_device_id(spi)->name;
  212. indio_dev->dev.parent = &spi->dev;
  213. indio_dev->info = &kxsd9_info;
  214. indio_dev->modes = INDIO_DIRECT_MODE;
  215. spi->mode = SPI_MODE_0;
  216. spi_setup(spi);
  217. kxsd9_power_up(st);
  218. ret = iio_device_register(indio_dev);
  219. if (ret)
  220. goto error_free_dev;
  221. return 0;
  222. error_free_dev:
  223. iio_device_free(indio_dev);
  224. error_ret:
  225. return ret;
  226. }
  227. static int kxsd9_remove(struct spi_device *spi)
  228. {
  229. iio_device_unregister(spi_get_drvdata(spi));
  230. iio_device_free(spi_get_drvdata(spi));
  231. return 0;
  232. }
  233. static const struct spi_device_id kxsd9_id[] = {
  234. {"kxsd9", 0},
  235. { },
  236. };
  237. MODULE_DEVICE_TABLE(spi, kxsd9_id);
  238. static struct spi_driver kxsd9_driver = {
  239. .driver = {
  240. .name = "kxsd9",
  241. .owner = THIS_MODULE,
  242. },
  243. .probe = kxsd9_probe,
  244. .remove = kxsd9_remove,
  245. .id_table = kxsd9_id,
  246. };
  247. module_spi_driver(kxsd9_driver);
  248. MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
  249. MODULE_DESCRIPTION("Kionix KXSD9 SPI driver");
  250. MODULE_LICENSE("GPL v2");