ad7606.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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_CHANNEL(num, mask_sep, mask_type, mask_all) { \
  10. .type = IIO_VOLTAGE, \
  11. .indexed = 1, \
  12. .channel = num, \
  13. .address = num, \
  14. .info_mask_separate = mask_sep, \
  15. .info_mask_shared_by_type = mask_type, \
  16. .info_mask_shared_by_all = mask_all, \
  17. .scan_index = num, \
  18. .scan_type = { \
  19. .sign = 's', \
  20. .realbits = 16, \
  21. .storagebits = 16, \
  22. .endianness = IIO_CPU, \
  23. }, \
  24. }
  25. #define AD7605_CHANNEL(num) \
  26. AD760X_CHANNEL(num, BIT(IIO_CHAN_INFO_RAW), \
  27. BIT(IIO_CHAN_INFO_SCALE), 0)
  28. #define AD7606_CHANNEL(num) \
  29. AD760X_CHANNEL(num, BIT(IIO_CHAN_INFO_RAW), \
  30. BIT(IIO_CHAN_INFO_SCALE), \
  31. BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO))
  32. #define AD7616_CHANNEL(num) \
  33. AD760X_CHANNEL(num, BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),\
  34. 0, BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO))
  35. /**
  36. * struct ad7606_chip_info - chip specific information
  37. * @channels: channel specification
  38. * @num_channels: number of channels
  39. * @oversampling_avail pointer to the array which stores the available
  40. * oversampling ratios.
  41. * @oversampling_num number of elements stored in oversampling_avail array
  42. * @os_req_reset some devices require a reset to update oversampling
  43. * @init_delay_ms required delay in miliseconds for initialization
  44. * after a restart
  45. */
  46. struct ad7606_chip_info {
  47. const struct iio_chan_spec *channels;
  48. unsigned int num_channels;
  49. const unsigned int *oversampling_avail;
  50. unsigned int oversampling_num;
  51. bool os_req_reset;
  52. unsigned long init_delay_ms;
  53. };
  54. /**
  55. * struct ad7606_state - driver instance specific data
  56. * @dev pointer to kernel device
  57. * @chip_info entry in the table of chips that describes this device
  58. * @reg regulator info for the the power supply of the device
  59. * @bops bus operations (SPI or parallel)
  60. * @range voltage range selection, selects which scale to apply
  61. * @oversampling oversampling selection
  62. * @base_address address from where to read data in parallel operation
  63. * @sw_mode_en software mode enabled
  64. * @scale_avail pointer to the array which stores the available scales
  65. * @num_scales number of elements stored in the scale_avail array
  66. * @oversampling_avail pointer to the array which stores the available
  67. * oversampling ratios.
  68. * @num_os_ratios number of elements stored in oversampling_avail array
  69. * @write_scale pointer to the function which writes the scale
  70. * @write_os pointer to the function which writes the os
  71. * @lock protect sensor state from concurrent accesses to GPIOs
  72. * @gpio_convst GPIO descriptor for conversion start signal (CONVST)
  73. * @gpio_reset GPIO descriptor for device hard-reset
  74. * @gpio_range GPIO descriptor for range selection
  75. * @gpio_standby GPIO descriptor for stand-by signal (STBY),
  76. * controls power-down mode of device
  77. * @gpio_frstdata GPIO descriptor for reading from device when data
  78. * is being read on the first channel
  79. * @gpio_os GPIO descriptors to control oversampling on the device
  80. * @complete completion to indicate end of conversion
  81. * @trig The IIO trigger associated with the device.
  82. * @data buffer for reading data from the device
  83. * @d16 be16 buffer for reading data from the device
  84. */
  85. struct ad7606_state {
  86. struct device *dev;
  87. const struct ad7606_chip_info *chip_info;
  88. struct regulator *reg;
  89. const struct ad7606_bus_ops *bops;
  90. unsigned int range[16];
  91. unsigned int oversampling;
  92. void __iomem *base_address;
  93. bool sw_mode_en;
  94. const unsigned int *scale_avail;
  95. unsigned int num_scales;
  96. const unsigned int *oversampling_avail;
  97. unsigned int num_os_ratios;
  98. int (*write_scale)(struct iio_dev *indio_dev, int ch, int val);
  99. int (*write_os)(struct iio_dev *indio_dev, int val);
  100. struct mutex lock; /* protect sensor state */
  101. struct gpio_desc *gpio_convst;
  102. struct gpio_desc *gpio_reset;
  103. struct gpio_desc *gpio_range;
  104. struct gpio_desc *gpio_standby;
  105. struct gpio_desc *gpio_frstdata;
  106. struct gpio_descs *gpio_os;
  107. struct iio_trigger *trig;
  108. struct completion completion;
  109. /*
  110. * DMA (thus cache coherency maintenance) requires the
  111. * transfer buffers to live in their own cache lines.
  112. * 16 * 16-bit samples + 64-bit timestamp
  113. */
  114. unsigned short data[20] ____cacheline_aligned;
  115. __be16 d16[2];
  116. };
  117. /**
  118. * struct ad7606_bus_ops - driver bus operations
  119. * @read_block function pointer for reading blocks of data
  120. * @sw_mode_config: pointer to a function which configured the device
  121. * for software mode
  122. * @reg_read function pointer for reading spi register
  123. * @reg_write function pointer for writing spi register
  124. * @write_mask function pointer for write spi register with mask
  125. * @rd_wr_cmd pointer to the function which calculates the spi address
  126. */
  127. struct ad7606_bus_ops {
  128. /* more methods added in future? */
  129. int (*read_block)(struct device *dev, int num, void *data);
  130. int (*sw_mode_config)(struct iio_dev *indio_dev);
  131. int (*reg_read)(struct ad7606_state *st, unsigned int addr);
  132. int (*reg_write)(struct ad7606_state *st,
  133. unsigned int addr,
  134. unsigned int val);
  135. int (*write_mask)(struct ad7606_state *st,
  136. unsigned int addr,
  137. unsigned long mask,
  138. unsigned int val);
  139. u16 (*rd_wr_cmd)(int addr, char isWriteOp);
  140. };
  141. int ad7606_probe(struct device *dev, int irq, void __iomem *base_address,
  142. const char *name, unsigned int id,
  143. const struct ad7606_bus_ops *bops);
  144. enum ad7606_supported_device_ids {
  145. ID_AD7605_4,
  146. ID_AD7606_8,
  147. ID_AD7606_6,
  148. ID_AD7606_4,
  149. ID_AD7606B,
  150. ID_AD7616,
  151. };
  152. #ifdef CONFIG_PM_SLEEP
  153. extern const struct dev_pm_ops ad7606_pm_ops;
  154. #define AD7606_PM_OPS (&ad7606_pm_ops)
  155. #else
  156. #define AD7606_PM_OPS NULL
  157. #endif
  158. #endif /* IIO_ADC_AD7606_H_ */