OSDN Git Service

Merge tag 'soc-fsl-fix-v5.1' of git://git.kernel.org/pub/scm/linux/kernel/git/leo...
[uclinux-h8/linux.git] / include / linux / iio / adc / ad_sigma_delta.h
1 /*
2  * Support code for Analog Devices Sigma-Delta ADCs
3  *
4  * Copyright 2012 Analog Devices Inc.
5  *  Author: Lars-Peter Clausen <lars@metafoo.de>
6  *
7  * Licensed under the GPL-2.
8  */
9 #ifndef __AD_SIGMA_DELTA_H__
10 #define __AD_SIGMA_DELTA_H__
11
12 enum ad_sigma_delta_mode {
13         AD_SD_MODE_CONTINUOUS = 0,
14         AD_SD_MODE_SINGLE = 1,
15         AD_SD_MODE_IDLE = 2,
16         AD_SD_MODE_POWERDOWN = 3,
17 };
18
19 /**
20  * struct ad_sigma_delta_calib_data - Calibration data for Sigma Delta devices
21  * @mode: Calibration mode.
22  * @channel: Calibration channel.
23  */
24 struct ad_sd_calib_data {
25         unsigned int mode;
26         unsigned int channel;
27 };
28
29 struct ad_sigma_delta;
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  */
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 };
54
55 /**
56  * struct ad_sigma_delta - Sigma Delta device struct
57  * @spi: The spi device associated with the Sigma Delta device.
58  * @trig: The IIO trigger associated with the Sigma Delta device.
59  *
60  * Most of the fields are private to the sigma delta library code and should not
61  * be accessed by individual drivers.
62  */
63 struct ad_sigma_delta {
64         struct spi_device       *spi;
65         struct iio_trigger      *trig;
66
67 /* private: */
68         struct completion       completion;
69         bool                    irq_dis;
70
71         bool                    bus_locked;
72         bool                    keep_cs_asserted;
73
74         uint8_t                 comm;
75
76         const struct ad_sigma_delta_info *info;
77
78         /*
79          * DMA (thus cache coherency maintenance) requires the
80          * transfer buffers to live in their own cache lines.
81          */
82         uint8_t                         data[4] ____cacheline_aligned;
83 };
84
85 static inline int ad_sigma_delta_set_channel(struct ad_sigma_delta *sd,
86         unsigned int channel)
87 {
88         if (sd->info->set_channel)
89                 return sd->info->set_channel(sd, channel);
90
91         return 0;
92 }
93
94 static inline int ad_sigma_delta_set_mode(struct ad_sigma_delta *sd,
95         unsigned int mode)
96 {
97         if (sd->info->set_mode)
98                 return sd->info->set_mode(sd, mode);
99
100         return 0;
101 }
102
103 static inline int ad_sigma_delta_postprocess_sample(struct ad_sigma_delta *sd,
104         unsigned int raw_sample)
105 {
106         if (sd->info->postprocess_sample)
107                 return sd->info->postprocess_sample(sd, raw_sample);
108
109         return 0;
110 }
111
112 void ad_sd_set_comm(struct ad_sigma_delta *sigma_delta, uint8_t comm);
113 int ad_sd_write_reg(struct ad_sigma_delta *sigma_delta, unsigned int reg,
114         unsigned int size, unsigned int val);
115 int ad_sd_read_reg(struct ad_sigma_delta *sigma_delta, unsigned int reg,
116         unsigned int size, unsigned int *val);
117
118 int ad_sd_reset(struct ad_sigma_delta *sigma_delta,
119         unsigned int reset_length);
120
121 int ad_sigma_delta_single_conversion(struct iio_dev *indio_dev,
122         const struct iio_chan_spec *chan, int *val);
123 int ad_sd_calibrate_all(struct ad_sigma_delta *sigma_delta,
124         const struct ad_sd_calib_data *cd, unsigned int n);
125 int ad_sd_init(struct ad_sigma_delta *sigma_delta, struct iio_dev *indio_dev,
126         struct spi_device *spi, const struct ad_sigma_delta_info *info);
127
128 int ad_sd_setup_buffer_and_trigger(struct iio_dev *indio_dev);
129 void ad_sd_cleanup_buffer_and_trigger(struct iio_dev *indio_dev);
130
131 int ad_sd_validate_trigger(struct iio_dev *indio_dev, struct iio_trigger *trig);
132
133 #define __AD_SD_CHANNEL(_si, _channel1, _channel2, _address, _bits, \
134         _storagebits, _shift, _extend_name, _type, _mask_all) \
135         { \
136                 .type = (_type), \
137                 .differential = (_channel2 == -1 ? 0 : 1), \
138                 .indexed = 1, \
139                 .channel = (_channel1), \
140                 .channel2 = (_channel2), \
141                 .address = (_address), \
142                 .extend_name = (_extend_name), \
143                 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
144                         BIT(IIO_CHAN_INFO_OFFSET), \
145                 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
146                 .info_mask_shared_by_all = _mask_all, \
147                 .scan_index = (_si), \
148                 .scan_type = { \
149                         .sign = 'u', \
150                         .realbits = (_bits), \
151                         .storagebits = (_storagebits), \
152                         .shift = (_shift), \
153                         .endianness = IIO_BE, \
154                 }, \
155         }
156
157 #define AD_SD_DIFF_CHANNEL(_si, _channel1, _channel2, _address, _bits, \
158         _storagebits, _shift) \
159         __AD_SD_CHANNEL(_si, _channel1, _channel2, _address, _bits, \
160                 _storagebits, _shift, NULL, IIO_VOLTAGE, \
161                 BIT(IIO_CHAN_INFO_SAMP_FREQ))
162
163 #define AD_SD_SHORTED_CHANNEL(_si, _channel, _address, _bits, \
164         _storagebits, _shift) \
165         __AD_SD_CHANNEL(_si, _channel, _channel, _address, _bits, \
166                 _storagebits, _shift, "shorted", IIO_VOLTAGE, \
167                 BIT(IIO_CHAN_INFO_SAMP_FREQ))
168
169 #define AD_SD_CHANNEL(_si, _channel, _address, _bits, \
170         _storagebits, _shift) \
171         __AD_SD_CHANNEL(_si, _channel, -1, _address, _bits, \
172                 _storagebits, _shift, NULL, IIO_VOLTAGE, \
173                  BIT(IIO_CHAN_INFO_SAMP_FREQ))
174
175 #define AD_SD_CHANNEL_NO_SAMP_FREQ(_si, _channel, _address, _bits, \
176         _storagebits, _shift) \
177         __AD_SD_CHANNEL(_si, _channel, -1, _address, _bits, \
178                 _storagebits, _shift, NULL, IIO_VOLTAGE, 0)
179
180 #define AD_SD_TEMP_CHANNEL(_si, _address, _bits, _storagebits, _shift) \
181         __AD_SD_CHANNEL(_si, 0, -1, _address, _bits, \
182                 _storagebits, _shift, NULL, IIO_TEMP, \
183                 BIT(IIO_CHAN_INFO_SAMP_FREQ))
184
185 #define AD_SD_SUPPLY_CHANNEL(_si, _channel, _address, _bits, _storagebits, \
186         _shift) \
187         __AD_SD_CHANNEL(_si, _channel, -1, _address, _bits, \
188                 _storagebits, _shift, "supply", IIO_VOLTAGE, \
189                 BIT(IIO_CHAN_INFO_SAMP_FREQ))
190
191 #endif