adis.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. * Common library for ADIS16XXX devices
  3. *
  4. * Copyright 2012 Analog Devices Inc.
  5. * Author: Lars-Peter Clausen <lars@metafoo.de>
  6. *
  7. * Licensed under the GPL-2 or later.
  8. */
  9. #ifndef __IIO_ADIS_H__
  10. #define __IIO_ADIS_H__
  11. #include <linux/spi/spi.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/iio/types.h>
  14. #define ADIS_WRITE_REG(reg) ((0x80 | (reg)))
  15. #define ADIS_READ_REG(reg) ((reg) & 0x7f)
  16. #define ADIS_PAGE_SIZE 0x80
  17. #define ADIS_REG_PAGE_ID 0x00
  18. struct adis;
  19. /**
  20. * struct adis_data - ADIS chip variant specific data
  21. * @read_delay: SPI delay for read operations in us
  22. * @write_delay: SPI delay for write operations in us
  23. * @glob_cmd_reg: Register address of the GLOB_CMD register
  24. * @msc_ctrl_reg: Register address of the MSC_CTRL register
  25. * @diag_stat_reg: Register address of the DIAG_STAT register
  26. * @status_error_msgs: Array of error messgaes
  27. * @status_error_mask:
  28. */
  29. struct adis_data {
  30. unsigned int read_delay;
  31. unsigned int write_delay;
  32. unsigned int glob_cmd_reg;
  33. unsigned int msc_ctrl_reg;
  34. unsigned int diag_stat_reg;
  35. unsigned int self_test_mask;
  36. unsigned int startup_delay;
  37. const char * const *status_error_msgs;
  38. unsigned int status_error_mask;
  39. int (*enable_irq)(struct adis *adis, bool enable);
  40. bool has_paging;
  41. };
  42. struct adis {
  43. struct spi_device *spi;
  44. struct iio_trigger *trig;
  45. const struct adis_data *data;
  46. struct mutex txrx_lock;
  47. struct spi_message msg;
  48. struct spi_transfer *xfer;
  49. unsigned int current_page;
  50. void *buffer;
  51. uint8_t tx[10] ____cacheline_aligned;
  52. uint8_t rx[4];
  53. };
  54. int adis_init(struct adis *adis, struct iio_dev *indio_dev,
  55. struct spi_device *spi, const struct adis_data *data);
  56. int adis_reset(struct adis *adis);
  57. int adis_write_reg(struct adis *adis, unsigned int reg,
  58. unsigned int val, unsigned int size);
  59. int adis_read_reg(struct adis *adis, unsigned int reg,
  60. unsigned int *val, unsigned int size);
  61. /**
  62. * adis_write_reg_8() - Write single byte to a register
  63. * @adis: The adis device
  64. * @reg: The address of the register to be written
  65. * @value: The value to write
  66. */
  67. static inline int adis_write_reg_8(struct adis *adis, unsigned int reg,
  68. uint8_t val)
  69. {
  70. return adis_write_reg(adis, reg, val, 1);
  71. }
  72. /**
  73. * adis_write_reg_16() - Write 2 bytes to a pair of registers
  74. * @adis: The adis device
  75. * @reg: The address of the lower of the two registers
  76. * @value: Value to be written
  77. */
  78. static inline int adis_write_reg_16(struct adis *adis, unsigned int reg,
  79. uint16_t val)
  80. {
  81. return adis_write_reg(adis, reg, val, 2);
  82. }
  83. /**
  84. * adis_write_reg_32() - write 4 bytes to four registers
  85. * @adis: The adis device
  86. * @reg: The address of the lower of the four register
  87. * @value: Value to be written
  88. */
  89. static inline int adis_write_reg_32(struct adis *adis, unsigned int reg,
  90. uint32_t val)
  91. {
  92. return adis_write_reg(adis, reg, val, 4);
  93. }
  94. /**
  95. * adis_read_reg_16() - read 2 bytes from a 16-bit register
  96. * @adis: The adis device
  97. * @reg: The address of the lower of the two registers
  98. * @val: The value read back from the device
  99. */
  100. static inline int adis_read_reg_16(struct adis *adis, unsigned int reg,
  101. uint16_t *val)
  102. {
  103. unsigned int tmp;
  104. int ret;
  105. ret = adis_read_reg(adis, reg, &tmp, 2);
  106. *val = tmp;
  107. return ret;
  108. }
  109. /**
  110. * adis_read_reg_32() - read 4 bytes from a 32-bit register
  111. * @adis: The adis device
  112. * @reg: The address of the lower of the two registers
  113. * @val: The value read back from the device
  114. */
  115. static inline int adis_read_reg_32(struct adis *adis, unsigned int reg,
  116. uint32_t *val)
  117. {
  118. unsigned int tmp;
  119. int ret;
  120. ret = adis_read_reg(adis, reg, &tmp, 4);
  121. *val = tmp;
  122. return ret;
  123. }
  124. int adis_enable_irq(struct adis *adis, bool enable);
  125. int adis_check_status(struct adis *adis);
  126. int adis_initial_startup(struct adis *adis);
  127. int adis_single_conversion(struct iio_dev *indio_dev,
  128. const struct iio_chan_spec *chan, unsigned int error_mask,
  129. int *val);
  130. #define ADIS_VOLTAGE_CHAN(addr, si, chan, name, info_all, bits) { \
  131. .type = IIO_VOLTAGE, \
  132. .indexed = 1, \
  133. .channel = (chan), \
  134. .extend_name = name, \
  135. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
  136. BIT(IIO_CHAN_INFO_SCALE), \
  137. .info_mask_shared_by_all = info_all, \
  138. .address = (addr), \
  139. .scan_index = (si), \
  140. .scan_type = { \
  141. .sign = 'u', \
  142. .realbits = (bits), \
  143. .storagebits = 16, \
  144. .endianness = IIO_BE, \
  145. }, \
  146. }
  147. #define ADIS_SUPPLY_CHAN(addr, si, info_all, bits) \
  148. ADIS_VOLTAGE_CHAN(addr, si, 0, "supply", info_all, bits)
  149. #define ADIS_AUX_ADC_CHAN(addr, si, info_all, bits) \
  150. ADIS_VOLTAGE_CHAN(addr, si, 1, NULL, info_all, bits)
  151. #define ADIS_TEMP_CHAN(addr, si, info_all, bits) { \
  152. .type = IIO_TEMP, \
  153. .indexed = 1, \
  154. .channel = 0, \
  155. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
  156. BIT(IIO_CHAN_INFO_SCALE) | \
  157. BIT(IIO_CHAN_INFO_OFFSET), \
  158. .info_mask_shared_by_all = info_all, \
  159. .address = (addr), \
  160. .scan_index = (si), \
  161. .scan_type = { \
  162. .sign = 'u', \
  163. .realbits = (bits), \
  164. .storagebits = 16, \
  165. .endianness = IIO_BE, \
  166. }, \
  167. }
  168. #define ADIS_MOD_CHAN(_type, mod, addr, si, info_sep, info_all, bits) { \
  169. .type = (_type), \
  170. .modified = 1, \
  171. .channel2 = IIO_MOD_ ## mod, \
  172. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
  173. info_sep, \
  174. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
  175. .info_mask_shared_by_all = info_all, \
  176. .address = (addr), \
  177. .scan_index = (si), \
  178. .scan_type = { \
  179. .sign = 's', \
  180. .realbits = (bits), \
  181. .storagebits = 16, \
  182. .endianness = IIO_BE, \
  183. }, \
  184. }
  185. #define ADIS_ACCEL_CHAN(mod, addr, si, info_sep, info_all, bits) \
  186. ADIS_MOD_CHAN(IIO_ACCEL, mod, addr, si, info_sep, info_all, bits)
  187. #define ADIS_GYRO_CHAN(mod, addr, si, info_sep, info_all, bits) \
  188. ADIS_MOD_CHAN(IIO_ANGL_VEL, mod, addr, si, info_sep, info_all, bits)
  189. #define ADIS_INCLI_CHAN(mod, addr, si, info_sep, info_all, bits) \
  190. ADIS_MOD_CHAN(IIO_INCLI, mod, addr, si, info_sep, info_all, bits)
  191. #define ADIS_ROT_CHAN(mod, addr, si, info_sep, info_all, bits) \
  192. ADIS_MOD_CHAN(IIO_ROT, mod, addr, si, info_sep, info_all, bits)
  193. #ifdef CONFIG_IIO_ADIS_LIB_BUFFER
  194. int adis_setup_buffer_and_trigger(struct adis *adis,
  195. struct iio_dev *indio_dev, irqreturn_t (*trigger_handler)(int, void *));
  196. void adis_cleanup_buffer_and_trigger(struct adis *adis,
  197. struct iio_dev *indio_dev);
  198. int adis_probe_trigger(struct adis *adis, struct iio_dev *indio_dev);
  199. void adis_remove_trigger(struct adis *adis);
  200. int adis_update_scan_mode(struct iio_dev *indio_dev,
  201. const unsigned long *scan_mask);
  202. #else /* CONFIG_IIO_BUFFER */
  203. static inline int adis_setup_buffer_and_trigger(struct adis *adis,
  204. struct iio_dev *indio_dev, irqreturn_t (*trigger_handler)(int, void *))
  205. {
  206. return 0;
  207. }
  208. static inline void adis_cleanup_buffer_and_trigger(struct adis *adis,
  209. struct iio_dev *indio_dev)
  210. {
  211. }
  212. static inline int adis_probe_trigger(struct adis *adis,
  213. struct iio_dev *indio_dev)
  214. {
  215. return 0;
  216. }
  217. static inline void adis_remove_trigger(struct adis *adis)
  218. {
  219. }
  220. #define adis_update_scan_mode NULL
  221. #endif /* CONFIG_IIO_BUFFER */
  222. #ifdef CONFIG_DEBUG_FS
  223. int adis_debugfs_reg_access(struct iio_dev *indio_dev,
  224. unsigned int reg, unsigned int writeval, unsigned int *readval);
  225. #else
  226. #define adis_debugfs_reg_access NULL
  227. #endif
  228. #endif