xref: /OK3568_Linux_fs/u-boot/include/adc.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Copyright (C) 2015 Samsung Electronics
3*4882a593Smuzhiyun  * Przemyslaw Marczak <p.marczak@samsung.com>
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * SPDX-License-Identifier:	GPL-2.0+
6*4882a593Smuzhiyun  */
7*4882a593Smuzhiyun 
8*4882a593Smuzhiyun #ifndef _ADC_H_
9*4882a593Smuzhiyun #define _ADC_H_
10*4882a593Smuzhiyun 
11*4882a593Smuzhiyun /* ADC_CHANNEL() - ADC channel bit mask, to select only required channels */
12*4882a593Smuzhiyun #define ADC_CHANNEL(x)		(1 << x)
13*4882a593Smuzhiyun 
14*4882a593Smuzhiyun /* The last possible selected channel with 32-bit mask */
15*4882a593Smuzhiyun #define ADC_MAX_CHANNEL		31
16*4882a593Smuzhiyun 
17*4882a593Smuzhiyun /**
18*4882a593Smuzhiyun  * adc_data_format: define the ADC output data format, can be useful when
19*4882a593Smuzhiyun  * the device's input Voltage range is bipolar.
20*4882a593Smuzhiyun  * - ADC_DATA_FORMAT_BIN - binary offset
21*4882a593Smuzhiyun  * - ADC_DATA_FORMAT_2S  - two's complement
22*4882a593Smuzhiyun  *
23*4882a593Smuzhiyun  * Note: Device's driver should fill the 'data_format' field of its uclass's
24*4882a593Smuzhiyun  * platform data using one of the above data format types.
25*4882a593Smuzhiyun  */
26*4882a593Smuzhiyun enum adc_data_format {
27*4882a593Smuzhiyun 	ADC_DATA_FORMAT_BIN,
28*4882a593Smuzhiyun 	ADC_DATA_FORMAT_2S,
29*4882a593Smuzhiyun };
30*4882a593Smuzhiyun 
31*4882a593Smuzhiyun /**
32*4882a593Smuzhiyun  * struct adc_channel - structure to hold channel conversion data.
33*4882a593Smuzhiyun  * Useful to keep the result of a multi-channel conversion output.
34*4882a593Smuzhiyun  *
35*4882a593Smuzhiyun  * @id   - channel id
36*4882a593Smuzhiyun  * @data - channel conversion data
37*4882a593Smuzhiyun  */
38*4882a593Smuzhiyun struct adc_channel {
39*4882a593Smuzhiyun 	int id;
40*4882a593Smuzhiyun 	unsigned int data;
41*4882a593Smuzhiyun };
42*4882a593Smuzhiyun 
43*4882a593Smuzhiyun /**
44*4882a593Smuzhiyun  * struct adc_uclass_platdata - basic ADC info
45*4882a593Smuzhiyun  *
46*4882a593Smuzhiyun  * Note: The positive/negative reference Voltage is only a name and it doesn't
47*4882a593Smuzhiyun  * provide an information about the value polarity. It is possible, for both
48*4882a593Smuzhiyun  * values to be a negative or positive. For this purpose the uclass's platform
49*4882a593Smuzhiyun  * data provides a bool fields: 'vdd/vss_supply_is_negative'. This is useful,
50*4882a593Smuzhiyun  * since the regulator API returns only a positive Voltage values.
51*4882a593Smuzhiyun  *
52*4882a593Smuzhiyun  * To get the reference Voltage values with polarity, use functions:
53*4882a593Smuzhiyun  * - adc_vdd_value()
54*4882a593Smuzhiyun  * - adc_vss_value()
55*4882a593Smuzhiyun  * Those are useful for some cases of ADC's references, e.g.:
56*4882a593Smuzhiyun  * * Vdd: +3.3V; Vss: -3.3V -> 6.6 Vdiff
57*4882a593Smuzhiyun  * * Vdd: +3.3V; Vss: +0.3V -> 3.0 Vdiff
58*4882a593Smuzhiyun  * * Vdd: +3.3V; Vss:  0.0V -> 3.3 Vdiff
59*4882a593Smuzhiyun  * The last one is usually standard and doesn't require the fdt polarity info.
60*4882a593Smuzhiyun  *
61*4882a593Smuzhiyun  * For more informations read binding info:
62*4882a593Smuzhiyun  * - doc/device-tree-bindings/adc/adc.txt
63*4882a593Smuzhiyun  *
64*4882a593Smuzhiyun  * @data_mask              - conversion output data mask
65*4882a593Smuzhiyun  * @data_timeout_us        - single channel conversion timeout
66*4882a593Smuzhiyun  * @multidata_timeout_us   - multi channel conversion timeout
67*4882a593Smuzhiyun  * @channel_mask           - bit mask of available channels [0:31]
68*4882a593Smuzhiyun  * @vdd_supply             - positive reference Voltage supply (regulator)
69*4882a593Smuzhiyun  * @vss_supply             - negative reference Voltage supply (regulator)
70*4882a593Smuzhiyun  * @vdd_polarity_negative  - positive reference Voltage has negative polarity
71*4882a593Smuzhiyun  * @vss_polarity_negative  - negative reference Voltage has negative polarity
72*4882a593Smuzhiyun  * @vdd_microvolts         - positive reference Voltage value
73*4882a593Smuzhiyun  * @vss_microvolts         - negative reference Voltage value
74*4882a593Smuzhiyun  */
75*4882a593Smuzhiyun struct adc_uclass_platdata {
76*4882a593Smuzhiyun 	int data_format;
77*4882a593Smuzhiyun 	unsigned int data_mask;
78*4882a593Smuzhiyun 	unsigned int data_timeout_us;
79*4882a593Smuzhiyun 	unsigned int multidata_timeout_us;
80*4882a593Smuzhiyun 	unsigned int channel_mask;
81*4882a593Smuzhiyun 	struct udevice *vdd_supply;
82*4882a593Smuzhiyun 	struct udevice *vss_supply;
83*4882a593Smuzhiyun 	bool vdd_polarity_negative;
84*4882a593Smuzhiyun 	bool vss_polarity_negative;
85*4882a593Smuzhiyun 	int vdd_microvolts;
86*4882a593Smuzhiyun 	int vss_microvolts;
87*4882a593Smuzhiyun };
88*4882a593Smuzhiyun 
89*4882a593Smuzhiyun /**
90*4882a593Smuzhiyun  * struct adc_ops - ADC device operations for single/multi-channel operation.
91*4882a593Smuzhiyun  */
92*4882a593Smuzhiyun struct adc_ops {
93*4882a593Smuzhiyun 	/**
94*4882a593Smuzhiyun 	 * start_channel() - start conversion with its default parameters
95*4882a593Smuzhiyun 	 *                   for the given channel number.
96*4882a593Smuzhiyun 	 *
97*4882a593Smuzhiyun 	 * @dev:          ADC device to init
98*4882a593Smuzhiyun 	 * @channel:      analog channel number
99*4882a593Smuzhiyun 	 * @return:       0 if OK, -ve on error
100*4882a593Smuzhiyun 	 */
101*4882a593Smuzhiyun 	int (*start_channel)(struct udevice *dev, int channel);
102*4882a593Smuzhiyun 
103*4882a593Smuzhiyun 	/**
104*4882a593Smuzhiyun 	 * start_channels() - start conversion with its default parameters
105*4882a593Smuzhiyun 	 *                    for the channel numbers selected by the bit mask.
106*4882a593Smuzhiyun 	 *
107*4882a593Smuzhiyun 	 * This is optional, useful when the hardware supports multichannel
108*4882a593Smuzhiyun 	 * conversion by the single software trigger.
109*4882a593Smuzhiyun 	 *
110*4882a593Smuzhiyun 	 * @dev:          ADC device to init
111*4882a593Smuzhiyun 	 * @channel_mask: bit mask of selected analog channels
112*4882a593Smuzhiyun 	 * @return:       0 if OK, -ve on error
113*4882a593Smuzhiyun 	 */
114*4882a593Smuzhiyun 	int (*start_channels)(struct udevice *dev, unsigned int channel_mask);
115*4882a593Smuzhiyun 
116*4882a593Smuzhiyun 	/**
117*4882a593Smuzhiyun 	 * channel_data() - get conversion output data for the given channel.
118*4882a593Smuzhiyun 	 *
119*4882a593Smuzhiyun 	 * Note: The implementation of this function should only check, that
120*4882a593Smuzhiyun 	 * the conversion data is available at the call time. If the hardware
121*4882a593Smuzhiyun 	 * requires some delay to get the data, then this function should
122*4882a593Smuzhiyun 	 * return with -EBUSY value. The ADC API will call it in a loop,
123*4882a593Smuzhiyun 	 * until the data is available or the timeout expires. The maximum
124*4882a593Smuzhiyun 	 * timeout for this operation is defined by the field 'data_timeout_us'
125*4882a593Smuzhiyun 	 * in ADC uclasses platform data structure.
126*4882a593Smuzhiyun 	 *
127*4882a593Smuzhiyun 	 * @dev:          ADC device to trigger
128*4882a593Smuzhiyun 	 * @channel:      selected analog channel number
129*4882a593Smuzhiyun 	 * @data:         returned pointer to selected channel's output data
130*4882a593Smuzhiyun 	 * @return:       0 if OK, -EBUSY if busy, and other negative on error
131*4882a593Smuzhiyun 	 */
132*4882a593Smuzhiyun 	int (*channel_data)(struct udevice *dev, int channel,
133*4882a593Smuzhiyun 			    unsigned int *data);
134*4882a593Smuzhiyun 
135*4882a593Smuzhiyun 	/**
136*4882a593Smuzhiyun 	 * channels_data() - get conversion data for the selected channels.
137*4882a593Smuzhiyun 	 *
138*4882a593Smuzhiyun 	 * This is optional, useful when multichannel conversion is supported
139*4882a593Smuzhiyun 	 * by the hardware, by the single software trigger.
140*4882a593Smuzhiyun 	 *
141*4882a593Smuzhiyun 	 * For the proper implementation, please look at the 'Note' for the
142*4882a593Smuzhiyun 	 * above method. The only difference is in used timeout value, which
143*4882a593Smuzhiyun 	 * is defined by field 'multidata_timeout_us'.
144*4882a593Smuzhiyun 	 *
145*4882a593Smuzhiyun 	 * @dev:          ADC device to trigger
146*4882a593Smuzhiyun 	 * @channel_mask: bit mask of selected analog channels
147*4882a593Smuzhiyun 	 * @channels:     returned pointer to array of output data for channels
148*4882a593Smuzhiyun 	 *                selected by the given mask
149*4882a593Smuzhiyun 	 * @return:       0 if OK, -ve on error
150*4882a593Smuzhiyun 	 */
151*4882a593Smuzhiyun 	int (*channels_data)(struct udevice *dev, unsigned int channel_mask,
152*4882a593Smuzhiyun 			     struct adc_channel *channels);
153*4882a593Smuzhiyun 
154*4882a593Smuzhiyun 	/**
155*4882a593Smuzhiyun 	 * stop() - stop conversion of the given ADC device
156*4882a593Smuzhiyun 	 *
157*4882a593Smuzhiyun 	 * @dev:          ADC device to stop
158*4882a593Smuzhiyun 	 * @return:       0 if OK, -ve on error
159*4882a593Smuzhiyun 	 */
160*4882a593Smuzhiyun 	int (*stop)(struct udevice *dev);
161*4882a593Smuzhiyun };
162*4882a593Smuzhiyun 
163*4882a593Smuzhiyun /**
164*4882a593Smuzhiyun  * adc_start_channel() - start conversion for given device/channel and exit.
165*4882a593Smuzhiyun  *
166*4882a593Smuzhiyun  * @dev:     ADC device
167*4882a593Smuzhiyun  * @channel: analog channel number
168*4882a593Smuzhiyun  * @return:  0 if OK, -ve on error
169*4882a593Smuzhiyun  */
170*4882a593Smuzhiyun int adc_start_channel(struct udevice *dev, int channel);
171*4882a593Smuzhiyun 
172*4882a593Smuzhiyun /**
173*4882a593Smuzhiyun  * adc_start_channels() - start conversion for given device/channels and exit.
174*4882a593Smuzhiyun  *
175*4882a593Smuzhiyun  * Note:
176*4882a593Smuzhiyun  * To use this function, device must implement method: start_channels().
177*4882a593Smuzhiyun  *
178*4882a593Smuzhiyun  * @dev:          ADC device to start
179*4882a593Smuzhiyun  * @channel_mask: channel selection - a bit mask
180*4882a593Smuzhiyun  * @channel_mask: bit mask of analog channels
181*4882a593Smuzhiyun  * @return:       0 if OK, -ve on error
182*4882a593Smuzhiyun  */
183*4882a593Smuzhiyun int adc_start_channels(struct udevice *dev, unsigned int channel_mask);
184*4882a593Smuzhiyun 
185*4882a593Smuzhiyun /**
186*4882a593Smuzhiyun  * adc_channel_data() - get conversion data for the given device channel number.
187*4882a593Smuzhiyun  *
188*4882a593Smuzhiyun  * @dev:     ADC device to read
189*4882a593Smuzhiyun  * @channel: analog channel number
190*4882a593Smuzhiyun  * @data:    pointer to returned channel's data
191*4882a593Smuzhiyun  * @return:  0 if OK, -ve on error
192*4882a593Smuzhiyun  */
193*4882a593Smuzhiyun int adc_channel_data(struct udevice *dev, int channel, unsigned int *data);
194*4882a593Smuzhiyun 
195*4882a593Smuzhiyun /**
196*4882a593Smuzhiyun  * adc_channels_data() - get conversion data for the channels selected by mask
197*4882a593Smuzhiyun  *
198*4882a593Smuzhiyun  * Note:
199*4882a593Smuzhiyun  * To use this function, device must implement methods:
200*4882a593Smuzhiyun  * - start_channels()
201*4882a593Smuzhiyun  * - channels_data()
202*4882a593Smuzhiyun  *
203*4882a593Smuzhiyun  * @dev:          ADC device to read
204*4882a593Smuzhiyun  * @channel_mask: channel selection - a bit mask
205*4882a593Smuzhiyun  * @channels:     pointer to structure array of returned data for each channel
206*4882a593Smuzhiyun  * @return:       0 if OK, -ve on error
207*4882a593Smuzhiyun  */
208*4882a593Smuzhiyun int adc_channels_data(struct udevice *dev, unsigned int channel_mask,
209*4882a593Smuzhiyun 		      struct adc_channel *channels);
210*4882a593Smuzhiyun 
211*4882a593Smuzhiyun /**
212*4882a593Smuzhiyun  * adc_data_mask() - get data mask (ADC resolution bitmask) for given ADC device
213*4882a593Smuzhiyun  *
214*4882a593Smuzhiyun  * This can be used if adc uclass platform data is filled.
215*4882a593Smuzhiyun  *
216*4882a593Smuzhiyun  * @dev:       ADC device to check
217*4882a593Smuzhiyun  * @data_mask: pointer to the returned data bitmask
218*4882a593Smuzhiyun  * @return: 0 if OK, -ve on error
219*4882a593Smuzhiyun  */
220*4882a593Smuzhiyun int adc_data_mask(struct udevice *dev, unsigned int *data_mask);
221*4882a593Smuzhiyun 
222*4882a593Smuzhiyun /**
223*4882a593Smuzhiyun  * adc_channel_single_shot() - get output data of conversion for the ADC
224*4882a593Smuzhiyun  * device's channel. This function searches for the device with the given name,
225*4882a593Smuzhiyun  * starts the given channel conversion and returns the output data.
226*4882a593Smuzhiyun  *
227*4882a593Smuzhiyun  * Note: To use this function, device must implement metods:
228*4882a593Smuzhiyun  * - start_channel()
229*4882a593Smuzhiyun  * - channel_data()
230*4882a593Smuzhiyun  *
231*4882a593Smuzhiyun  * @name:    device's name to search
232*4882a593Smuzhiyun  * @channel: device's input channel to init
233*4882a593Smuzhiyun  * @data:    pointer to conversion output data
234*4882a593Smuzhiyun  * @return:  0 if OK, -ve on error
235*4882a593Smuzhiyun  */
236*4882a593Smuzhiyun int adc_channel_single_shot(const char *name, int channel, unsigned int *data);
237*4882a593Smuzhiyun 
238*4882a593Smuzhiyun /**
239*4882a593Smuzhiyun  * adc_channels_single_shot() - get ADC conversion output data for the selected
240*4882a593Smuzhiyun  * device's channels. This function searches for the device by the given name,
241*4882a593Smuzhiyun  * starts the selected channels conversion and returns the output data as array
242*4882a593Smuzhiyun  * of type 'struct adc_channel'.
243*4882a593Smuzhiyun  *
244*4882a593Smuzhiyun  * Note: This function can be used if device implements one of ADC's single
245*4882a593Smuzhiyun  * or multi-channel operation API. If multi-channel operation is not supported,
246*4882a593Smuzhiyun  * then each selected channel is triggered by the sequence start/data in a loop.
247*4882a593Smuzhiyun  *
248*4882a593Smuzhiyun  * @name:         device's name to search
249*4882a593Smuzhiyun  * @channel_mask: channel selection - a bit mask
250*4882a593Smuzhiyun  * @channels:     pointer to conversion output data for the selected channels
251*4882a593Smuzhiyun  * @return:       0 if OK, -ve on error
252*4882a593Smuzhiyun  */
253*4882a593Smuzhiyun int adc_channels_single_shot(const char *name, unsigned int channel_mask,
254*4882a593Smuzhiyun 			     struct adc_channel *channels);
255*4882a593Smuzhiyun 
256*4882a593Smuzhiyun /**
257*4882a593Smuzhiyun  * adc_vdd_value() - get the ADC device's positive reference Voltage value
258*4882a593Smuzhiyun  *
259*4882a593Smuzhiyun  * Note: Depending on bool value 'vdd_supply_is_negative' of platform data,
260*4882a593Smuzhiyun  * the returned uV value can be negative, and it's not an error.
261*4882a593Smuzhiyun  *
262*4882a593Smuzhiyun  * @dev:     ADC device to check
263*4882a593Smuzhiyun  * @uV:      Voltage value with polarization sign (uV)
264*4882a593Smuzhiyun  * @return:  0 on success or -ve on error
265*4882a593Smuzhiyun */
266*4882a593Smuzhiyun int adc_vdd_value(struct udevice *dev, int *uV);
267*4882a593Smuzhiyun 
268*4882a593Smuzhiyun /**
269*4882a593Smuzhiyun  * adc_vss_value() - get the ADC device's negative reference Voltage value
270*4882a593Smuzhiyun  *
271*4882a593Smuzhiyun  * Note: Depending on bool value 'vdd_supply_is_negative' of platform data,
272*4882a593Smuzhiyun  * the returned uV value can be negative, and it's not an error.
273*4882a593Smuzhiyun  *
274*4882a593Smuzhiyun  * @dev:     ADC device to check
275*4882a593Smuzhiyun  * @uV:      Voltage value with polarization sign (uV)
276*4882a593Smuzhiyun  * @return:  0 on success or -ve on error
277*4882a593Smuzhiyun */
278*4882a593Smuzhiyun int adc_vss_value(struct udevice *dev, int *uV);
279*4882a593Smuzhiyun 
280*4882a593Smuzhiyun /**
281*4882a593Smuzhiyun  * adc_stop() - stop operation for given ADC device.
282*4882a593Smuzhiyun  *
283*4882a593Smuzhiyun  * @dev:     ADC device to stop
284*4882a593Smuzhiyun  * @return:  0 if OK, -ve on error
285*4882a593Smuzhiyun  */
286*4882a593Smuzhiyun int adc_stop(struct udevice *dev);
287*4882a593Smuzhiyun 
288*4882a593Smuzhiyun #endif
289