adis.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. bool self_test_no_autoclear;
  37. unsigned int startup_delay;
  38. const char * const *status_error_msgs;
  39. unsigned int status_error_mask;
  40. int (*enable_irq)(struct adis *adis, bool enable);
  41. bool has_paging;
  42. };
  43. struct adis {
  44. struct spi_device *spi;
  45. struct iio_trigger *trig;
  46. const struct adis_data *data;
  47. struct mutex txrx_lock;
  48. struct spi_message msg;
  49. struct spi_transfer *xfer;
  50. unsigned int current_page;
  51. void *buffer;
  52. uint8_t tx[10] ____cacheline_aligned;
  53. uint8_t rx[4];
  54. };
  55. int adis_init(struct adis *adis, struct iio_dev *indio_dev,
  56. struct spi_device *spi, const struct adis_data *data);
  57. int adis_reset(struct adis *adis);
  58. int adis_write_reg(struct adis *adis, unsigned int reg,
  59. unsigned int val, unsigned int size);
  60. int adis_read_reg(struct adis *adis, unsigned int reg,
  61. unsigned int *val, unsigned int size);
  62. /**
  63. * adis_write_reg_8() - Write single byte to a register
  64. * @adis: The adis device
  65. * @reg: The address of the register to be written
  66. * @value: The value to write
  67. */
  68. static inline int adis_write_reg_8(struct adis *adis, unsigned int reg,
  69. uint8_t val)
  70. {
  71. return adis_write_reg(adis, reg, val, 1);
  72. }
  73. /**
  74. * adis_write_reg_16() - Write 2 bytes to a pair of registers
  75. * @adis: The adis device
  76. * @reg: The address of the lower of the two registers
  77. * @value: Value to be written
  78. */
  79. static inline int adis_write_reg_16(struct adis *adis, unsigned int reg,
  80. uint16_t val)
  81. {
  82. return adis_write_reg(adis, reg, val, 2);
  83. }
  84. /**
  85. * adis_write_reg_32() - write 4 bytes to four registers
  86. * @adis: The adis device
  87. * @reg: The address of the lower of the four register
  88. * @value: Value to be written
  89. */
  90. static inline int adis_write_reg_32(struct adis *adis, unsigned int reg,
  91. uint32_t val)
  92. {
  93. return adis_write_reg(adis, reg, val, 4);
  94. }
  95. /**
  96. * adis_read_reg_16() - read 2 bytes from a 16-bit register
  97. * @adis: The adis device
  98. * @reg: The address of the lower of the two registers
  99. * @val: The value read back from the device
  100. */
  101. static inline int adis_read_reg_16(struct adis *adis, unsigned int reg,
  102. uint16_t *val)
  103. {
  104. unsigned int tmp;
  105. int ret;
  106. ret = adis_read_reg(adis, reg, &tmp, 2);
  107. *val = tmp;
  108. return ret;
  109. }
  110. /**
  111. * adis_read_reg_32() - read 4 bytes from a 32-bit register
  112. * @adis: The adis device
  113. * @reg: The address of the lower of the two registers
  114. * @val: The value read back from the device
  115. */
  116. static inline int adis_read_reg_32(struct adis *adis, unsigned int reg,
  117. uint32_t *val)
  118. {
  119. unsigned int tmp;
  120. int ret;
  121. ret = adis_read_reg(adis, reg, &tmp, 4);
  122. *val = tmp;
  123. return ret;
  124. }
  125. int adis_enable_irq(struct adis *adis, bool enable);
  126. int adis_check_status(struct adis *adis);
  127. int adis_initial_startup(struct adis *adis);
  128. int adis_single_conversion(struct iio_dev *indio_dev,
  129. const struct iio_chan_spec *chan, unsigned int error_mask,
  130. int *val);
  131. #define ADIS_VOLTAGE_CHAN(addr, si, chan, name, info_all, bits) { \
  132. .type = IIO_VOLTAGE, \
  133. .indexed = 1, \
  134. .channel = (chan), \
  135. .extend_name = name, \
  136. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
  137. BIT(IIO_CHAN_INFO_SCALE), \
  138. .info_mask_shared_by_all = info_all, \
  139. .address = (addr), \
  140. .scan_index = (si), \
  141. .scan_type = { \
  142. .sign = 'u', \
  143. .realbits = (bits), \
  144. .storagebits = 16, \
  145. .endianness = IIO_BE, \
  146. }, \
  147. }
  148. #define ADIS_SUPPLY_CHAN(addr, si, info_all, bits) \
  149. ADIS_VOLTAGE_CHAN(addr, si, 0, "supply", info_all, bits)
  150. #define ADIS_AUX_ADC_CHAN(addr, si, info_all, bits) \
  151. ADIS_VOLTAGE_CHAN(addr, si, 1, NULL, info_all, bits)
  152. #define ADIS_TEMP_CHAN(addr, si, info_all, bits) { \
  153. .type = IIO_TEMP, \
  154. .indexed = 1, \
  155. .channel = 0, \
  156. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
  157. BIT(IIO_CHAN_INFO_SCALE) | \
  158. BIT(IIO_CHAN_INFO_OFFSET), \
  159. .info_mask_shared_by_all = info_all, \
  160. .address = (addr), \
  161. .scan_index = (si), \
  162. .scan_type = { \
  163. .sign = 'u', \
  164. .realbits = (bits), \
  165. .storagebits = 16, \
  166. .endianness = IIO_BE, \
  167. }, \
  168. }
  169. #define ADIS_MOD_CHAN(_type, mod, addr, si, info_sep, info_all, bits) { \
  170. .type = (_type), \
  171. .modified = 1, \
  172. .channel2 = IIO_MOD_ ## mod, \
  173. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
  174. info_sep, \
  175. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
  176. .info_mask_shared_by_all = info_all, \
  177. .address = (addr), \
  178. .scan_index = (si), \
  179. .scan_type = { \
  180. .sign = 's', \
  181. .realbits = (bits), \
  182. .storagebits = 16, \
  183. .endianness = IIO_BE, \
  184. }, \
  185. }
  186. #define ADIS_ACCEL_CHAN(mod, addr, si, info_sep, info_all, bits) \
  187. ADIS_MOD_CHAN(IIO_ACCEL, mod, addr, si, info_sep, info_all, bits)
  188. #define ADIS_GYRO_CHAN(mod, addr, si, info_sep, info_all, bits) \
  189. ADIS_MOD_CHAN(IIO_ANGL_VEL, mod, addr, si, info_sep, info_all, bits)
  190. #define ADIS_INCLI_CHAN(mod, addr, si, info_sep, info_all, bits) \
  191. ADIS_MOD_CHAN(IIO_INCLI, mod, addr, si, info_sep, info_all, bits)
  192. #define ADIS_ROT_CHAN(mod, addr, si, info_sep, info_all, bits) \
  193. ADIS_MOD_CHAN(IIO_ROT, mod, addr, si, info_sep, info_all, bits)
  194. #ifdef CONFIG_IIO_ADIS_LIB_BUFFER
  195. int adis_setup_buffer_and_trigger(struct adis *adis,
  196. struct iio_dev *indio_dev, irqreturn_t (*trigger_handler)(int, void *));
  197. void adis_cleanup_buffer_and_trigger(struct adis *adis,
  198. struct iio_dev *indio_dev);
  199. int adis_probe_trigger(struct adis *adis, struct iio_dev *indio_dev);
  200. void adis_remove_trigger(struct adis *adis);
  201. int adis_update_scan_mode(struct iio_dev *indio_dev,
  202. const unsigned long *scan_mask);
  203. #else /* CONFIG_IIO_BUFFER */
  204. static inline int adis_setup_buffer_and_trigger(struct adis *adis,
  205. struct iio_dev *indio_dev, irqreturn_t (*trigger_handler)(int, void *))
  206. {
  207. return 0;
  208. }
  209. static inline void adis_cleanup_buffer_and_trigger(struct adis *adis,
  210. struct iio_dev *indio_dev)
  211. {
  212. }
  213. static inline int adis_probe_trigger(struct adis *adis,
  214. struct iio_dev *indio_dev)
  215. {
  216. return 0;
  217. }
  218. static inline void adis_remove_trigger(struct adis *adis)
  219. {
  220. }
  221. #define adis_update_scan_mode NULL
  222. #endif /* CONFIG_IIO_BUFFER */
  223. #ifdef CONFIG_DEBUG_FS
  224. int adis_debugfs_reg_access(struct iio_dev *indio_dev,
  225. unsigned int reg, unsigned int writeval, unsigned int *readval);
  226. #else
  227. #define adis_debugfs_reg_access NULL
  228. #endif
  229. #endif