ad7606.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * AD7606 ADC driver
  4. *
  5. * Copyright 2011 Analog Devices Inc.
  6. */
  7. #ifndef IIO_ADC_AD7606_H_
  8. #define IIO_ADC_AD7606_H_
  9. #define AD760X_MAX_CHANNELS 16
  10. #define AD7616_CONFIGURATION_REGISTER 0x02
  11. #define AD7616_OS_MASK GENMASK(4, 2)
  12. #define AD7616_BURST_MODE BIT(6)
  13. #define AD7616_SEQEN_MODE BIT(5)
  14. #define AD7616_RANGE_CH_A_ADDR_OFF 0x04
  15. #define AD7616_RANGE_CH_B_ADDR_OFF 0x06
  16. /*
  17. * Range of channels from a group are stored in 2 registers.
  18. * 0, 1, 2, 3 in a register followed by 4, 5, 6, 7 in second register.
  19. * For channels from second group(8-15) the order is the same, only with
  20. * an offset of 2 for register address.
  21. */
  22. #define AD7616_RANGE_CH_ADDR(ch) ((ch) >> 2)
  23. /* The range of the channel is stored in 2 bits */
  24. #define AD7616_RANGE_CH_MSK(ch) (0b11 << (((ch) & 0b11) * 2))
  25. #define AD7616_RANGE_CH_MODE(ch, mode) ((mode) << ((((ch) & 0b11)) * 2))
  26. #define AD7606_CONFIGURATION_REGISTER 0x02
  27. #define AD7606_SINGLE_DOUT 0x00
  28. /*
  29. * Range for AD7606B channels are stored in registers starting with address 0x3.
  30. * Each register stores range for 2 channels(4 bits per channel).
  31. */
  32. #define AD7606_RANGE_CH_MSK(ch) (GENMASK(3, 0) << (4 * ((ch) & 0x1)))
  33. #define AD7606_RANGE_CH_MODE(ch, mode) \
  34. ((GENMASK(3, 0) & (mode)) << (4 * ((ch) & 0x1)))
  35. #define AD7606_RANGE_CH_ADDR(ch) (0x03 + ((ch) >> 1))
  36. #define AD7606_OS_MODE 0x08
  37. #define AD7606_CALIB_GAIN(ch) (0x09 + (ch))
  38. #define AD7606_CALIB_GAIN_MASK GENMASK(5, 0)
  39. #define AD7606_CALIB_OFFSET(ch) (0x11 + (ch))
  40. #define AD7606_CALIB_PHASE(ch) (0x19 + (ch))
  41. struct ad7606_state;
  42. typedef int (*ad7606_scale_setup_cb_t)(struct iio_dev *indio_dev,
  43. struct iio_chan_spec *chan);
  44. typedef int (*ad7606_sw_setup_cb_t)(struct iio_dev *indio_dev);
  45. /**
  46. * struct ad7606_chip_info - chip specific information
  47. * @max_samplerate: maximum supported sample rate
  48. * @name: device name
  49. * @bits: data width in bits
  50. * @num_adc_channels: the number of physical voltage inputs
  51. * @scale_setup_cb: callback to setup the scales for each channel
  52. * @sw_setup_cb: callback to setup the software mode if available.
  53. * @oversampling_avail: pointer to the array which stores the available
  54. * oversampling ratios.
  55. * @oversampling_num: number of elements stored in oversampling_avail array
  56. * @os_req_reset: some devices require a reset to update oversampling
  57. * @init_delay_ms: required delay in milliseconds for initialization
  58. * after a restart
  59. * @offload_storagebits: storage bits used by the offload hw implementation
  60. * @calib_gain_avail: chip supports gain calibration
  61. * @calib_offset_avail: pointer to offset calibration range/limits array
  62. * @calib_phase_avail: pointer to phase calibration range/limits array
  63. */
  64. struct ad7606_chip_info {
  65. unsigned int max_samplerate;
  66. const char *name;
  67. unsigned int bits;
  68. unsigned int num_adc_channels;
  69. ad7606_scale_setup_cb_t scale_setup_cb;
  70. ad7606_sw_setup_cb_t sw_setup_cb;
  71. const unsigned int *oversampling_avail;
  72. unsigned int oversampling_num;
  73. bool os_req_reset;
  74. unsigned long init_delay_ms;
  75. u8 offload_storagebits;
  76. bool calib_gain_avail;
  77. const int *calib_offset_avail;
  78. const int (*calib_phase_avail)[2];
  79. };
  80. /**
  81. * struct ad7606_chan_info - channel configuration
  82. * @scale_avail: pointer to the array which stores the available scales
  83. * @num_scales: number of elements stored in the scale_avail array
  84. * @range: voltage range selection, selects which scale to apply
  85. * @reg_offset: offset for the register value, to be applied when
  86. * writing the value of 'range' to the register value
  87. * @r_gain: gain resistor value in ohms, to be set to match the
  88. * external r_filter value
  89. */
  90. struct ad7606_chan_info {
  91. #define AD760X_MAX_SCALES 16
  92. const unsigned int (*scale_avail)[2];
  93. unsigned int num_scales;
  94. unsigned int range;
  95. unsigned int reg_offset;
  96. unsigned int r_gain;
  97. };
  98. /**
  99. * struct ad7606_state - driver instance specific data
  100. * @dev: pointer to kernel device
  101. * @chip_info: entry in the table of chips that describes this device
  102. * @bops: bus operations (SPI or parallel)
  103. * @chan_info: scale configuration for channels
  104. * @oversampling: oversampling selection
  105. * @cnvst_pwm: pointer to the PWM device connected to the cnvst pin
  106. * @base_address: address from where to read data in parallel operation
  107. * @sw_mode_en: software mode enabled
  108. * @oversampling_avail: pointer to the array which stores the available
  109. * oversampling ratios.
  110. * @num_os_ratios: number of elements stored in oversampling_avail array
  111. * @back: pointer to the iio_backend structure, if used
  112. * @write_scale: pointer to the function which writes the scale
  113. * @write_os: pointer to the function which writes the os
  114. * @lock: protect sensor state from concurrent accesses to GPIOs
  115. * @gpio_convst: GPIO descriptor for conversion start signal (CONVST)
  116. * @gpio_reset: GPIO descriptor for device hard-reset
  117. * @gpio_range: GPIO descriptor for range selection
  118. * @gpio_standby: GPIO descriptor for stand-by signal (STBY),
  119. * controls power-down mode of device
  120. * @gpio_frstdata: GPIO descriptor for reading from device when data
  121. * is being read on the first channel
  122. * @gpio_os: GPIO descriptors to control oversampling on the device
  123. * @trig: The IIO trigger associated with the device.
  124. * @completion: completion to indicate end of conversion
  125. * @data: buffer for reading data from the device
  126. * @offload_en: SPI offload enabled
  127. * @bus_data: bus-specific variables
  128. * @d16: be16 buffer for reading data from the device
  129. */
  130. struct ad7606_state {
  131. struct device *dev;
  132. const struct ad7606_chip_info *chip_info;
  133. const struct ad7606_bus_ops *bops;
  134. struct ad7606_chan_info chan_info[AD760X_MAX_CHANNELS];
  135. unsigned int oversampling;
  136. struct pwm_device *cnvst_pwm;
  137. void __iomem *base_address;
  138. bool sw_mode_en;
  139. const unsigned int *oversampling_avail;
  140. unsigned int num_os_ratios;
  141. struct iio_backend *back;
  142. int (*write_scale)(struct iio_dev *indio_dev, int ch, int val);
  143. int (*write_os)(struct iio_dev *indio_dev, int val);
  144. struct mutex lock; /* protect sensor state */
  145. struct gpio_desc *gpio_convst;
  146. struct gpio_desc *gpio_reset;
  147. struct gpio_desc *gpio_range;
  148. struct gpio_desc *gpio_standby;
  149. struct gpio_desc *gpio_frstdata;
  150. struct gpio_descs *gpio_os;
  151. struct iio_trigger *trig;
  152. struct completion completion;
  153. bool offload_en;
  154. void *bus_data;
  155. /*
  156. * DMA (thus cache coherency maintenance) may require the
  157. * transfer buffers to live in their own cache lines.
  158. * 16 * 16-bit samples for AD7616
  159. * 8 * 32-bit samples for AD7616C-18 (and similar)
  160. */
  161. struct {
  162. union {
  163. u16 buf16[16];
  164. u32 buf32[8];
  165. };
  166. aligned_s64 timestamp;
  167. } data __aligned(IIO_DMA_MINALIGN);
  168. __be16 d16[2];
  169. };
  170. /**
  171. * struct ad7606_bus_ops - driver bus operations
  172. * @iio_backend_config: function pointer for configuring the iio_backend for
  173. * the compatibles that use it
  174. * @read_block: function pointer for reading blocks of data
  175. * @sw_mode_config: pointer to a function which configured the device
  176. * for software mode
  177. * @offload_config: function pointer for configuring offload support,
  178. * where any
  179. * @reg_read: function pointer for reading spi register
  180. * @reg_write: function pointer for writing spi register
  181. * @update_scan_mode: function pointer for handling the calls to iio_info's
  182. * update_scan mode when enabling/disabling channels.
  183. * @rd_wr_cmd: pointer to the function which calculates the spi address
  184. */
  185. struct ad7606_bus_ops {
  186. /* more methods added in future? */
  187. int (*iio_backend_config)(struct device *dev, struct iio_dev *indio_dev);
  188. int (*offload_config)(struct device *dev, struct iio_dev *indio_dev);
  189. int (*read_block)(struct device *dev, int num, void *data);
  190. int (*sw_mode_config)(struct iio_dev *indio_dev);
  191. int (*reg_read)(struct ad7606_state *st, unsigned int addr);
  192. int (*reg_write)(struct ad7606_state *st,
  193. unsigned int addr,
  194. unsigned int val);
  195. int (*update_scan_mode)(struct iio_dev *indio_dev, const unsigned long *scan_mask);
  196. u16 (*rd_wr_cmd)(int addr, char is_write_op);
  197. };
  198. /**
  199. * struct ad7606_bus_info - aggregate ad7606_chip_info and ad7606_bus_ops
  200. * @chip_info: entry in the table of chips that describes this device
  201. * @bops: bus operations (SPI or parallel)
  202. */
  203. struct ad7606_bus_info {
  204. const struct ad7606_chip_info *chip_info;
  205. const struct ad7606_bus_ops *bops;
  206. };
  207. int ad7606_probe(struct device *dev, int irq, void __iomem *base_address,
  208. const struct ad7606_chip_info *info,
  209. const struct ad7606_bus_ops *bops);
  210. int ad7606_reset(struct ad7606_state *st);
  211. int ad7606_pwm_set_swing(struct ad7606_state *st);
  212. int ad7606_pwm_set_low(struct ad7606_state *st);
  213. extern const struct ad7606_chip_info ad7605_4_info;
  214. extern const struct ad7606_chip_info ad7606_8_info;
  215. extern const struct ad7606_chip_info ad7606_6_info;
  216. extern const struct ad7606_chip_info ad7606_4_info;
  217. extern const struct ad7606_chip_info ad7606b_info;
  218. extern const struct ad7606_chip_info ad7606c_16_info;
  219. extern const struct ad7606_chip_info ad7606c_18_info;
  220. extern const struct ad7606_chip_info ad7607_info;
  221. extern const struct ad7606_chip_info ad7608_info;
  222. extern const struct ad7606_chip_info ad7609_info;
  223. extern const struct ad7606_chip_info ad7616_info;
  224. #ifdef CONFIG_PM_SLEEP
  225. extern const struct dev_pm_ops ad7606_pm_ops;
  226. #define AD7606_PM_OPS (&ad7606_pm_ops)
  227. #else
  228. #define AD7606_PM_OPS NULL
  229. #endif
  230. #endif /* IIO_ADC_AD7606_H_ */