xref: /linux/include/linux/iio/adc/ad_sigma_delta.h (revision 7b0c9f8f)
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 #include <linux/iio/iio.h>
12 
13 enum ad_sigma_delta_mode {
14 	AD_SD_MODE_CONTINUOUS = 0,
15 	AD_SD_MODE_SINGLE = 1,
16 	AD_SD_MODE_IDLE = 2,
17 	AD_SD_MODE_POWERDOWN = 3,
18 };
19 
20 /**
21  * struct ad_sigma_delta_calib_data - Calibration data for Sigma Delta devices
22  * @mode: Calibration mode.
23  * @channel: Calibration channel.
24  */
25 struct ad_sd_calib_data {
26 	unsigned int mode;
27 	unsigned int channel;
28 };
29 
30 struct ad_sigma_delta;
31 struct device;
32 struct iio_dev;
33 
34 /**
35  * struct ad_sigma_delta_info - Sigma Delta driver specific callbacks and options
36  * @set_channel: Will be called to select the current channel, may be NULL.
37  * @append_status: Will be called to enable status append at the end of the sample, may be NULL.
38  * @set_mode: Will be called to select the current mode, may be NULL.
39  * @disable_all: Will be called to disable all channels, may be NULL.
40  * @postprocess_sample: Is called for each sampled data word, can be used to
41  *		modify or drop the sample data, it, may be NULL.
42  * @has_registers: true if the device has writable and readable registers, false
43  *		if there is just one read-only sample data shift register.
44  * @addr_shift: Shift of the register address in the communications register.
45  * @read_mask: Mask for the communications register having the read bit set.
46  * @status_ch_mask: Mask for the channel number stored in status register.
47  * @data_reg: Address of the data register, if 0 the default address of 0x3 will
48  *   be used.
49  * @irq_flags: flags for the interrupt used by the triggered buffer
50  * @num_slots: Number of sequencer slots
51  * @irq_line: IRQ for reading conversions. If 0, spi->irq will be used
52  */
53 struct ad_sigma_delta_info {
54 	int (*set_channel)(struct ad_sigma_delta *, unsigned int channel);
55 	int (*append_status)(struct ad_sigma_delta *, bool append);
56 	int (*set_mode)(struct ad_sigma_delta *, enum ad_sigma_delta_mode mode);
57 	int (*disable_all)(struct ad_sigma_delta *);
58 	int (*postprocess_sample)(struct ad_sigma_delta *, unsigned int raw_sample);
59 	bool has_registers;
60 	unsigned int addr_shift;
61 	unsigned int read_mask;
62 	unsigned int status_ch_mask;
63 	unsigned int data_reg;
64 	unsigned long irq_flags;
65 	unsigned int num_slots;
66 	int irq_line;
67 };
68 
69 /**
70  * struct ad_sigma_delta - Sigma Delta device struct
71  * @spi: The spi device associated with the Sigma Delta device.
72  * @trig: The IIO trigger associated with the Sigma Delta device.
73  *
74  * Most of the fields are private to the sigma delta library code and should not
75  * be accessed by individual drivers.
76  */
77 struct ad_sigma_delta {
78 	struct spi_device	*spi;
79 	struct iio_trigger	*trig;
80 
81 /* private: */
82 	struct completion	completion;
83 	bool			irq_dis;
84 
85 	bool			bus_locked;
86 	bool			keep_cs_asserted;
87 
88 	uint8_t			comm;
89 
90 	const struct ad_sigma_delta_info *info;
91 	unsigned int		active_slots;
92 	unsigned int		current_slot;
93 	unsigned int		num_slots;
94 	int		irq_line;
95 	bool			status_appended;
96 	/* map slots to channels in order to know what to expect from devices */
97 	unsigned int		*slots;
98 	uint8_t			*samples_buf;
99 
100 	/*
101 	 * DMA (thus cache coherency maintenance) requires the
102 	 * transfer buffers to live in their own cache lines.
103 	 * 'tx_buf' is up to 32 bits.
104 	 * 'rx_buf' is up to 32 bits per sample + 64 bit timestamp,
105 	 * rounded to 16 bytes to take into account padding.
106 	 */
107 	uint8_t				tx_buf[4] __aligned(IIO_DMA_MINALIGN);
108 	uint8_t				rx_buf[16] __aligned(8);
109 };
110 
ad_sigma_delta_set_channel(struct ad_sigma_delta * sd,unsigned int channel)111 static inline int ad_sigma_delta_set_channel(struct ad_sigma_delta *sd,
112 	unsigned int channel)
113 {
114 	if (sd->info->set_channel)
115 		return sd->info->set_channel(sd, channel);
116 
117 	return 0;
118 }
119 
ad_sigma_delta_append_status(struct ad_sigma_delta * sd,bool append)120 static inline int ad_sigma_delta_append_status(struct ad_sigma_delta *sd, bool append)
121 {
122 	int ret;
123 
124 	if (sd->info->append_status) {
125 		ret = sd->info->append_status(sd, append);
126 		if (ret < 0)
127 			return ret;
128 
129 		sd->status_appended = append;
130 	}
131 
132 	return 0;
133 }
134 
ad_sigma_delta_disable_all(struct ad_sigma_delta * sd)135 static inline int ad_sigma_delta_disable_all(struct ad_sigma_delta *sd)
136 {
137 	if (sd->info->disable_all)
138 		return sd->info->disable_all(sd);
139 
140 	return 0;
141 }
142 
ad_sigma_delta_set_mode(struct ad_sigma_delta * sd,unsigned int mode)143 static inline int ad_sigma_delta_set_mode(struct ad_sigma_delta *sd,
144 	unsigned int mode)
145 {
146 	if (sd->info->set_mode)
147 		return sd->info->set_mode(sd, mode);
148 
149 	return 0;
150 }
151 
ad_sigma_delta_postprocess_sample(struct ad_sigma_delta * sd,unsigned int raw_sample)152 static inline int ad_sigma_delta_postprocess_sample(struct ad_sigma_delta *sd,
153 	unsigned int raw_sample)
154 {
155 	if (sd->info->postprocess_sample)
156 		return sd->info->postprocess_sample(sd, raw_sample);
157 
158 	return 0;
159 }
160 
161 void ad_sd_set_comm(struct ad_sigma_delta *sigma_delta, uint8_t comm);
162 int ad_sd_write_reg(struct ad_sigma_delta *sigma_delta, unsigned int reg,
163 	unsigned int size, unsigned int val);
164 int ad_sd_read_reg(struct ad_sigma_delta *sigma_delta, unsigned int reg,
165 	unsigned int size, unsigned int *val);
166 
167 int ad_sd_reset(struct ad_sigma_delta *sigma_delta,
168 	unsigned int reset_length);
169 
170 int ad_sigma_delta_single_conversion(struct iio_dev *indio_dev,
171 	const struct iio_chan_spec *chan, int *val);
172 int ad_sd_calibrate(struct ad_sigma_delta *sigma_delta,
173 	unsigned int mode, unsigned int channel);
174 int ad_sd_calibrate_all(struct ad_sigma_delta *sigma_delta,
175 	const struct ad_sd_calib_data *cd, unsigned int n);
176 int ad_sd_init(struct ad_sigma_delta *sigma_delta, struct iio_dev *indio_dev,
177 	struct spi_device *spi, const struct ad_sigma_delta_info *info);
178 
179 int devm_ad_sd_setup_buffer_and_trigger(struct device *dev, struct iio_dev *indio_dev);
180 
181 int ad_sd_validate_trigger(struct iio_dev *indio_dev, struct iio_trigger *trig);
182 
183 #endif
184