ad_sigma_delta.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * Support code for Analog Devices Sigma-Delta ADCs
  3. *
  4. * Copyright 2012 Analog Devices Inc.
  5. * Author: Lars-Peter Clausen <lars@metafoo.de>
  6. *
  7. * Licensed under the GPL-2.
  8. */
  9. #ifndef __AD_SIGMA_DELTA_H__
  10. #define __AD_SIGMA_DELTA_H__
  11. enum ad_sigma_delta_mode {
  12. AD_SD_MODE_CONTINUOUS = 0,
  13. AD_SD_MODE_SINGLE = 1,
  14. AD_SD_MODE_IDLE = 2,
  15. AD_SD_MODE_POWERDOWN = 3,
  16. };
  17. /**
  18. * struct ad_sigma_delta_calib_data - Calibration data for Sigma Delta devices
  19. * @mode: Calibration mode.
  20. * @channel: Calibration channel.
  21. */
  22. struct ad_sd_calib_data {
  23. unsigned int mode;
  24. unsigned int channel;
  25. };
  26. struct ad_sigma_delta;
  27. struct iio_dev;
  28. /**
  29. * struct ad_sigma_delta_info - Sigma Delta driver specific callbacks and options
  30. * @set_channel: Will be called to select the current channel, may be NULL.
  31. * @set_mode: Will be called to select the current mode, may be NULL.
  32. * @postprocess_sample: Is called for each sampled data word, can be used to
  33. * modify or drop the sample data, it, may be NULL.
  34. * @has_registers: true if the device has writable and readable registers, false
  35. * if there is just one read-only sample data shift register.
  36. * @addr_shift: Shift of the register address in the communications register.
  37. * @read_mask: Mask for the communications register having the read bit set.
  38. */
  39. struct ad_sigma_delta_info {
  40. int (*set_channel)(struct ad_sigma_delta *, unsigned int channel);
  41. int (*set_mode)(struct ad_sigma_delta *, enum ad_sigma_delta_mode mode);
  42. int (*postprocess_sample)(struct ad_sigma_delta *, unsigned int raw_sample);
  43. bool has_registers;
  44. unsigned int addr_shift;
  45. unsigned int read_mask;
  46. };
  47. /**
  48. * struct ad_sigma_delta - Sigma Delta device struct
  49. * @spi: The spi device associated with the Sigma Delta device.
  50. * @trig: The IIO trigger associated with the Sigma Delta device.
  51. *
  52. * Most of the fields are private to the sigma delta library code and should not
  53. * be accessed by individual drivers.
  54. */
  55. struct ad_sigma_delta {
  56. struct spi_device *spi;
  57. struct iio_trigger *trig;
  58. /* private: */
  59. struct completion completion;
  60. bool irq_dis;
  61. bool bus_locked;
  62. uint8_t comm;
  63. const struct ad_sigma_delta_info *info;
  64. /*
  65. * DMA (thus cache coherency maintenance) requires the
  66. * transfer buffers to live in their own cache lines.
  67. */
  68. uint8_t data[4] ____cacheline_aligned;
  69. };
  70. static inline int ad_sigma_delta_set_channel(struct ad_sigma_delta *sd,
  71. unsigned int channel)
  72. {
  73. if (sd->info->set_channel)
  74. return sd->info->set_channel(sd, channel);
  75. return 0;
  76. }
  77. static inline int ad_sigma_delta_set_mode(struct ad_sigma_delta *sd,
  78. unsigned int mode)
  79. {
  80. if (sd->info->set_mode)
  81. return sd->info->set_mode(sd, mode);
  82. return 0;
  83. }
  84. static inline int ad_sigma_delta_postprocess_sample(struct ad_sigma_delta *sd,
  85. unsigned int raw_sample)
  86. {
  87. if (sd->info->postprocess_sample)
  88. return sd->info->postprocess_sample(sd, raw_sample);
  89. return 0;
  90. }
  91. void ad_sd_set_comm(struct ad_sigma_delta *sigma_delta, uint8_t comm);
  92. int ad_sd_write_reg(struct ad_sigma_delta *sigma_delta, unsigned int reg,
  93. unsigned int size, unsigned int val);
  94. int ad_sd_read_reg(struct ad_sigma_delta *sigma_delta, unsigned int reg,
  95. unsigned int size, unsigned int *val);
  96. int ad_sd_reset(struct ad_sigma_delta *sigma_delta,
  97. unsigned int reset_length);
  98. int ad_sigma_delta_single_conversion(struct iio_dev *indio_dev,
  99. const struct iio_chan_spec *chan, int *val);
  100. int ad_sd_calibrate_all(struct ad_sigma_delta *sigma_delta,
  101. const struct ad_sd_calib_data *cd, unsigned int n);
  102. int ad_sd_init(struct ad_sigma_delta *sigma_delta, struct iio_dev *indio_dev,
  103. struct spi_device *spi, const struct ad_sigma_delta_info *info);
  104. int ad_sd_setup_buffer_and_trigger(struct iio_dev *indio_dev);
  105. void ad_sd_cleanup_buffer_and_trigger(struct iio_dev *indio_dev);
  106. int ad_sd_validate_trigger(struct iio_dev *indio_dev, struct iio_trigger *trig);
  107. #define __AD_SD_CHANNEL(_si, _channel1, _channel2, _address, _bits, \
  108. _storagebits, _shift, _extend_name, _type) \
  109. { \
  110. .type = (_type), \
  111. .differential = (_channel2 == -1 ? 0 : 1), \
  112. .indexed = 1, \
  113. .channel = (_channel1), \
  114. .channel2 = (_channel2), \
  115. .address = (_address), \
  116. .extend_name = (_extend_name), \
  117. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
  118. BIT(IIO_CHAN_INFO_OFFSET), \
  119. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
  120. .scan_index = (_si), \
  121. .scan_type = { \
  122. .sign = 'u', \
  123. .realbits = (_bits), \
  124. .storagebits = (_storagebits), \
  125. .shift = (_shift), \
  126. .endianness = IIO_BE, \
  127. }, \
  128. }
  129. #define AD_SD_DIFF_CHANNEL(_si, _channel1, _channel2, _address, _bits, \
  130. _storagebits, _shift) \
  131. __AD_SD_CHANNEL(_si, _channel1, _channel2, _address, _bits, \
  132. _storagebits, _shift, NULL, IIO_VOLTAGE)
  133. #define AD_SD_SHORTED_CHANNEL(_si, _channel, _address, _bits, \
  134. _storagebits, _shift) \
  135. __AD_SD_CHANNEL(_si, _channel, _channel, _address, _bits, \
  136. _storagebits, _shift, "shorted", IIO_VOLTAGE)
  137. #define AD_SD_CHANNEL(_si, _channel, _address, _bits, \
  138. _storagebits, _shift) \
  139. __AD_SD_CHANNEL(_si, _channel, -1, _address, _bits, \
  140. _storagebits, _shift, NULL, IIO_VOLTAGE)
  141. #define AD_SD_TEMP_CHANNEL(_si, _address, _bits, _storagebits, _shift) \
  142. __AD_SD_CHANNEL(_si, 0, -1, _address, _bits, \
  143. _storagebits, _shift, NULL, IIO_TEMP)
  144. #define AD_SD_SUPPLY_CHANNEL(_si, _channel, _address, _bits, _storagebits, \
  145. _shift) \
  146. __AD_SD_CHANNEL(_si, _channel, -1, _address, _bits, \
  147. _storagebits, _shift, "supply", IIO_VOLTAGE)
  148. #endif