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