1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3 * Support code for Analog Devices Sigma-Delta ADCs
4 *
5 * Copyright 2012 Analog Devices Inc.
6 * Author: Lars-Peter Clausen <lars@metafoo.de>
7 */
8 #ifndef __AD_SIGMA_DELTA_H__
9 #define __AD_SIGMA_DELTA_H__
10
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 /**
19 * struct ad_sigma_delta_calib_data - Calibration data for Sigma Delta devices
20 * @mode: Calibration mode.
21 * @channel: Calibration channel.
22 */
23 struct ad_sd_calib_data {
24 unsigned int mode;
25 unsigned int channel;
26 };
27
28 struct ad_sigma_delta;
29 struct iio_dev;
30
31 /**
32 * struct ad_sigma_delta_info - Sigma Delta driver specific callbacks and options
33 * @set_channel: Will be called to select the current channel, may be NULL.
34 * @set_mode: Will be called to select the current mode, may be NULL.
35 * @postprocess_sample: Is called for each sampled data word, can be used to
36 * modify or drop the sample data, it, may be NULL.
37 * @has_registers: true if the device has writable and readable registers, false
38 * if there is just one read-only sample data shift register.
39 * @addr_shift: Shift of the register address in the communications register.
40 * @read_mask: Mask for the communications register having the read bit set.
41 * @data_reg: Address of the data register, if 0 the default address of 0x3 will
42 * be used.
43 * @irq_flags: flags for the interrupt used by the triggered buffer
44 */
45 struct ad_sigma_delta_info {
46 int (*set_channel)(struct ad_sigma_delta *, unsigned int channel);
47 int (*set_mode)(struct ad_sigma_delta *, enum ad_sigma_delta_mode mode);
48 int (*postprocess_sample)(struct ad_sigma_delta *, unsigned int raw_sample);
49 bool has_registers;
50 unsigned int addr_shift;
51 unsigned int read_mask;
52 unsigned int data_reg;
53 unsigned long irq_flags;
54 };
55
56 /**
57 * struct ad_sigma_delta - Sigma Delta device struct
58 * @spi: The spi device associated with the Sigma Delta device.
59 * @trig: The IIO trigger associated with the Sigma Delta device.
60 *
61 * Most of the fields are private to the sigma delta library code and should not
62 * be accessed by individual drivers.
63 */
64 struct ad_sigma_delta {
65 struct spi_device *spi;
66 struct iio_trigger *trig;
67
68 /* private: */
69 struct completion completion;
70 bool irq_dis;
71
72 bool bus_locked;
73 bool keep_cs_asserted;
74
75 uint8_t comm;
76
77 const struct ad_sigma_delta_info *info;
78
79 /*
80 * DMA (thus cache coherency maintenance) requires the
81 * transfer buffers to live in their own cache lines.
82 * 'tx_buf' is up to 32 bits.
83 * 'rx_buf' is up to 32 bits per sample + 64 bit timestamp,
84 * rounded to 16 bytes to take into account padding.
85 */
86 uint8_t tx_buf[4] ____cacheline_aligned;
87 uint8_t rx_buf[16] __aligned(8);
88 };
89
ad_sigma_delta_set_channel(struct ad_sigma_delta * sd,unsigned int channel)90 static inline int ad_sigma_delta_set_channel(struct ad_sigma_delta *sd,
91 unsigned int channel)
92 {
93 if (sd->info->set_channel)
94 return sd->info->set_channel(sd, channel);
95
96 return 0;
97 }
98
ad_sigma_delta_set_mode(struct ad_sigma_delta * sd,unsigned int mode)99 static inline int ad_sigma_delta_set_mode(struct ad_sigma_delta *sd,
100 unsigned int mode)
101 {
102 if (sd->info->set_mode)
103 return sd->info->set_mode(sd, mode);
104
105 return 0;
106 }
107
ad_sigma_delta_postprocess_sample(struct ad_sigma_delta * sd,unsigned int raw_sample)108 static inline int ad_sigma_delta_postprocess_sample(struct ad_sigma_delta *sd,
109 unsigned int raw_sample)
110 {
111 if (sd->info->postprocess_sample)
112 return sd->info->postprocess_sample(sd, raw_sample);
113
114 return 0;
115 }
116
117 void ad_sd_set_comm(struct ad_sigma_delta *sigma_delta, uint8_t comm);
118 int ad_sd_write_reg(struct ad_sigma_delta *sigma_delta, unsigned int reg,
119 unsigned int size, unsigned int val);
120 int ad_sd_read_reg(struct ad_sigma_delta *sigma_delta, unsigned int reg,
121 unsigned int size, unsigned int *val);
122
123 int ad_sd_reset(struct ad_sigma_delta *sigma_delta,
124 unsigned int reset_length);
125
126 int ad_sigma_delta_single_conversion(struct iio_dev *indio_dev,
127 const struct iio_chan_spec *chan, int *val);
128 int ad_sd_calibrate(struct ad_sigma_delta *sigma_delta,
129 unsigned int mode, unsigned int channel);
130 int ad_sd_calibrate_all(struct ad_sigma_delta *sigma_delta,
131 const struct ad_sd_calib_data *cd, unsigned int n);
132 int ad_sd_init(struct ad_sigma_delta *sigma_delta, struct iio_dev *indio_dev,
133 struct spi_device *spi, const struct ad_sigma_delta_info *info);
134
135 int ad_sd_setup_buffer_and_trigger(struct iio_dev *indio_dev);
136 void ad_sd_cleanup_buffer_and_trigger(struct iio_dev *indio_dev);
137
138 int ad_sd_validate_trigger(struct iio_dev *indio_dev, struct iio_trigger *trig);
139
140 #endif
141