adis.h 6.6 KB

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