126c0a9375f26cd949afb14ac2fae7e3938a7744
[firefly-linux-kernel-4.4.55.git] / include / linux / iio / consumer.h
1 /*
2  * Industrial I/O in kernel consumer interface
3  *
4  * Copyright (c) 2011 Jonathan Cameron
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License version 2 as published by
8  * the Free Software Foundation.
9  */
10 #ifndef _IIO_INKERN_CONSUMER_H_
11 #define _IIO_INKERN_CONSUMER_H_
12 #include <linux/iio/types.h>
13
14 struct iio_dev;
15 struct iio_chan_spec;
16
17 /**
18  * struct iio_channel - everything needed for a consumer to use a channel
19  * @indio_dev:          Device on which the channel exists.
20  * @channel:            Full description of the channel.
21  * @data:               Data about the channel used by consumer.
22  */
23 struct iio_channel {
24         struct iio_dev *indio_dev;
25         const struct iio_chan_spec *channel;
26         void *data;
27 };
28
29 /**
30  * iio_channel_get() - get description of all that is needed to access channel.
31  * @name:               Unique name of the device as provided in the iio_map
32  *                      with which the desired provider to consumer mapping
33  *                      was registered.
34  * @consumer_channel:   Unique name to identify the channel on the consumer
35  *                      side. This typically describes the channels use within
36  *                      the consumer. E.g. 'battery_voltage'
37  */
38 struct iio_channel *iio_channel_get(const char *name,
39                                     const char *consumer_channel);
40
41 /**
42  * iio_channel_release() - release channels obtained via iio_channel_get
43  * @chan:               The channel to be released.
44  */
45 void iio_channel_release(struct iio_channel *chan);
46
47 /**
48  * iio_channel_get_all() - get all channels associated with a client
49  * @name:               name of consumer device.
50  *
51  * Returns an array of iio_channel structures terminated with one with
52  * null iio_dev pointer.
53  * This function is used by fairly generic consumers to get all the
54  * channels registered as having this consumer.
55  */
56 struct iio_channel *iio_channel_get_all(const char *name);
57
58 /**
59  * iio_channel_release_all() - reverse iio_channel_get_all
60  * @chan:               Array of channels to be released.
61  */
62 void iio_channel_release_all(struct iio_channel *chan);
63
64 struct iio_cb_buffer;
65 /**
66  * iio_channel_get_all_cb() - register callback for triggered capture
67  * @name:               Name of client device.
68  * @cb:                 Callback function.
69  * @private:            Private data passed to callback.
70  *
71  * NB right now we have no ability to mux data from multiple devices.
72  * So if the channels requested come from different devices this will
73  * fail.
74  */
75 struct iio_cb_buffer *iio_channel_get_all_cb(const char *name,
76                                              int (*cb)(u8 *data,
77                                                        void *private),
78                                              void *private);
79 /**
80  * iio_channel_release_all_cb() - release and unregister the callback.
81  * @cb_buffer:          The callback buffer that was allocated.
82  */
83 void iio_channel_release_all_cb(struct iio_cb_buffer *cb_buffer);
84
85 /**
86  * iio_channel_start_all_cb() - start the flow of data through callback.
87  * @cb_buff:            The callback buffer we are starting.
88  */
89 int iio_channel_start_all_cb(struct iio_cb_buffer *cb_buff);
90
91 /**
92  * iio_channel_stop_all_cb() - stop the flow of data through the callback.
93  * @cb_buff:            The callback buffer we are stopping.
94  */
95 void iio_channel_stop_all_cb(struct iio_cb_buffer *cb_buff);
96
97 /**
98  * iio_channel_cb_get_channels() - get access to the underlying channels.
99  * @cb_buff:            The callback buffer from whom we want the channel
100  *                      information.
101  *
102  * This function allows one to obtain information about the channels.
103  * Whilst this may allow direct reading if all buffers are disabled, the
104  * primary aim is to allow drivers that are consuming a channel to query
105  * things like scaling of the channel.
106  */
107 struct iio_channel
108 *iio_channel_cb_get_channels(const struct iio_cb_buffer *cb_buffer);
109
110 /**
111  * iio_read_channel_raw() - read from a given channel
112  * @chan:               The channel being queried.
113  * @val:                Value read back.
114  *
115  * Note raw reads from iio channels are in adc counts and hence
116  * scale will need to be applied if standard units required.
117  */
118 int iio_read_channel_raw(struct iio_channel *chan,
119                          int *val);
120
121 /**
122  * iio_read_channel_processed() - read processed value from a given channel
123  * @chan:               The channel being queried.
124  * @val:                Value read back.
125  *
126  * Returns an error code or 0.
127  *
128  * This function will read a processed value from a channel. A processed value
129  * means that this value will have the correct unit and not some device internal
130  * representation. If the device does not support reporting a processed value
131  * the function will query the raw value and the channels scale and offset and
132  * do the appropriate transformation.
133  */
134 int iio_read_channel_processed(struct iio_channel *chan, int *val);
135
136 /**
137  * iio_get_channel_type() - get the type of a channel
138  * @channel:            The channel being queried.
139  * @type:               The type of the channel.
140  *
141  * returns the enum iio_chan_type of the channel
142  */
143 int iio_get_channel_type(struct iio_channel *channel,
144                          enum iio_chan_type *type);
145
146 /**
147  * iio_read_channel_scale() - read the scale value for a channel
148  * @chan:               The channel being queried.
149  * @val:                First part of value read back.
150  * @val2:               Second part of value read back.
151  *
152  * Note returns a description of what is in val and val2, such
153  * as IIO_VAL_INT_PLUS_MICRO telling us we have a value of val
154  * + val2/1e6
155  */
156 int iio_read_channel_scale(struct iio_channel *chan, int *val,
157                            int *val2);
158
159 /**
160  * iio_convert_raw_to_processed() - Converts a raw value to a processed value
161  * @chan:               The channel being queried
162  * @raw:                The raw IIO to convert
163  * @processed:          The result of the conversion
164  * @scale:              Scale factor to apply during the conversion
165  *
166  * Returns an error code or 0.
167  *
168  * This function converts a raw value to processed value for a specific channel.
169  * A raw value is the device internal representation of a sample and the value
170  * returned by iio_read_channel_raw, so the unit of that value is device
171  * depended. A processed value on the other hand is value has a normed unit
172  * according with the IIO specification.
173  *
174  * The scale factor allows to increase the precession of the returned value. For
175  * a scale factor of 1 the function will return the result in the normal IIO
176  * unit for the channel type. E.g. millivolt for voltage channels, if you want
177  * nanovolts instead pass 1000 as the scale factor.
178  */
179 int iio_convert_raw_to_processed(struct iio_channel *chan, int raw,
180         int *processed, unsigned int scale);
181
182 #endif