blob: e1e033d6a81f034b249f032235c393892dafd196 [file] [log] [blame]
Jonathan Camerone27d75d2012-02-15 19:48:01 +00001/*
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_
Lars-Peter Clausen88238fe2012-08-17 16:57:00 +010011#define _IIO_INKERN_CONSUMER_H_
Greg Kroah-Hartman9f488ba2012-11-13 10:46:33 -080012
13#include <linux/types.h>
Jonathan Cameron06458e22012-04-25 15:54:58 +010014#include <linux/iio/types.h>
Jonathan Camerone27d75d2012-02-15 19:48:01 +000015
16struct iio_dev;
17struct iio_chan_spec;
Guenter Roeckca7d98d2013-01-31 21:43:00 +000018struct device;
Jonathan Camerone27d75d2012-02-15 19:48:01 +000019
20/**
21 * struct iio_channel - everything needed for a consumer to use a channel
22 * @indio_dev: Device on which the channel exists.
23 * @channel: Full description of the channel.
Jonathan Cameron04644152012-06-30 20:06:00 +010024 * @data: Data about the channel used by consumer.
Jonathan Camerone27d75d2012-02-15 19:48:01 +000025 */
26struct iio_channel {
27 struct iio_dev *indio_dev;
28 const struct iio_chan_spec *channel;
Jonathan Cameron04644152012-06-30 20:06:00 +010029 void *data;
Jonathan Camerone27d75d2012-02-15 19:48:01 +000030};
31
32/**
33 * iio_channel_get() - get description of all that is needed to access channel.
Guenter Roeck5aa57f02013-02-04 20:26:00 +000034 * @dev: Pointer to consumer device. Device name must match
35 * the name of the device as provided in the iio_map
Jonathan Camerone27d75d2012-02-15 19:48:01 +000036 * with which the desired provider to consumer mapping
37 * was registered.
38 * @consumer_channel: Unique name to identify the channel on the consumer
39 * side. This typically describes the channels use within
40 * the consumer. E.g. 'battery_voltage'
41 */
Guenter Roeck5aa57f02013-02-04 20:26:00 +000042struct iio_channel *iio_channel_get(struct device *dev,
Jonathan Cameron314be142012-05-01 21:04:24 +010043 const char *consumer_channel);
Jonathan Camerone27d75d2012-02-15 19:48:01 +000044
45/**
Jonathan Cameron314be142012-05-01 21:04:24 +010046 * iio_channel_release() - release channels obtained via iio_channel_get
Jonathan Camerone27d75d2012-02-15 19:48:01 +000047 * @chan: The channel to be released.
48 */
Jonathan Cameron314be142012-05-01 21:04:24 +010049void iio_channel_release(struct iio_channel *chan);
Jonathan Camerone27d75d2012-02-15 19:48:01 +000050
51/**
Laxman Dewangan8bf872d2016-04-06 16:01:06 +053052 * devm_iio_channel_get() - Resource managed version of iio_channel_get().
53 * @dev: Pointer to consumer device. Device name must match
54 * the name of the device as provided in the iio_map
55 * with which the desired provider to consumer mapping
56 * was registered.
57 * @consumer_channel: Unique name to identify the channel on the consumer
58 * side. This typically describes the channels use within
59 * the consumer. E.g. 'battery_voltage'
60 *
61 * Returns a pointer to negative errno if it is not able to get the iio channel
62 * otherwise returns valid pointer for iio channel.
63 *
64 * The allocated iio channel is automatically released when the device is
65 * unbound.
66 */
67struct iio_channel *devm_iio_channel_get(struct device *dev,
68 const char *consumer_channel);
69/**
70 * devm_iio_channel_release() - Resource managed version of
71 * iio_channel_release().
72 * @dev: Pointer to consumer device for which resource
73 * is allocared.
74 * @chan: The channel to be released.
75 */
76void devm_iio_channel_release(struct device *dev, struct iio_channel *chan);
77
78/**
Jonathan Cameron314be142012-05-01 21:04:24 +010079 * iio_channel_get_all() - get all channels associated with a client
Guenter Roeckca7d98d2013-01-31 21:43:00 +000080 * @dev: Pointer to consumer device.
Jonathan Camerone27d75d2012-02-15 19:48:01 +000081 *
82 * Returns an array of iio_channel structures terminated with one with
83 * null iio_dev pointer.
84 * This function is used by fairly generic consumers to get all the
85 * channels registered as having this consumer.
86 */
Guenter Roeckca7d98d2013-01-31 21:43:00 +000087struct iio_channel *iio_channel_get_all(struct device *dev);
Jonathan Camerone27d75d2012-02-15 19:48:01 +000088
89/**
Jonathan Cameron314be142012-05-01 21:04:24 +010090 * iio_channel_release_all() - reverse iio_channel_get_all
Jonathan Camerone27d75d2012-02-15 19:48:01 +000091 * @chan: Array of channels to be released.
92 */
Jonathan Cameron314be142012-05-01 21:04:24 +010093void iio_channel_release_all(struct iio_channel *chan);
Jonathan Camerone27d75d2012-02-15 19:48:01 +000094
Jonathan Cameron92d10792012-06-30 20:06:00 +010095struct iio_cb_buffer;
96/**
97 * iio_channel_get_all_cb() - register callback for triggered capture
Guenter Roeckca7d98d2013-01-31 21:43:00 +000098 * @dev: Pointer to client device.
Jonathan Cameron92d10792012-06-30 20:06:00 +010099 * @cb: Callback function.
100 * @private: Private data passed to callback.
101 *
102 * NB right now we have no ability to mux data from multiple devices.
103 * So if the channels requested come from different devices this will
104 * fail.
105 */
Guenter Roeckca7d98d2013-01-31 21:43:00 +0000106struct iio_cb_buffer *iio_channel_get_all_cb(struct device *dev,
Lars-Peter Clausen5d65d922013-09-15 17:50:00 +0100107 int (*cb)(const void *data,
Jonathan Cameron92d10792012-06-30 20:06:00 +0100108 void *private),
109 void *private);
110/**
111 * iio_channel_release_all_cb() - release and unregister the callback.
112 * @cb_buffer: The callback buffer that was allocated.
113 */
114void iio_channel_release_all_cb(struct iio_cb_buffer *cb_buffer);
115
116/**
117 * iio_channel_start_all_cb() - start the flow of data through callback.
118 * @cb_buff: The callback buffer we are starting.
119 */
120int iio_channel_start_all_cb(struct iio_cb_buffer *cb_buff);
121
122/**
123 * iio_channel_stop_all_cb() - stop the flow of data through the callback.
124 * @cb_buff: The callback buffer we are stopping.
125 */
126void iio_channel_stop_all_cb(struct iio_cb_buffer *cb_buff);
127
128/**
129 * iio_channel_cb_get_channels() - get access to the underlying channels.
Cristina Opriceanae52e9512015-07-24 16:23:43 +0300130 * @cb_buffer: The callback buffer from whom we want the channel
Jonathan Cameron92d10792012-06-30 20:06:00 +0100131 * information.
132 *
133 * This function allows one to obtain information about the channels.
134 * Whilst this may allow direct reading if all buffers are disabled, the
135 * primary aim is to allow drivers that are consuming a channel to query
136 * things like scaling of the channel.
137 */
138struct iio_channel
139*iio_channel_cb_get_channels(const struct iio_cb_buffer *cb_buffer);
140
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000141/**
Jonathan Cameron314be142012-05-01 21:04:24 +0100142 * iio_read_channel_raw() - read from a given channel
Lars-Peter Clausen45f010b2012-09-17 13:17:00 +0100143 * @chan: The channel being queried.
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000144 * @val: Value read back.
145 *
146 * Note raw reads from iio channels are in adc counts and hence
147 * scale will need to be applied if standard units required.
148 */
Jonathan Cameron314be142012-05-01 21:04:24 +0100149int iio_read_channel_raw(struct iio_channel *chan,
150 int *val);
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000151
152/**
Sebastian Reichel476d4af2014-10-03 17:25:00 +0100153 * iio_read_channel_average_raw() - read from a given channel
154 * @chan: The channel being queried.
155 * @val: Value read back.
156 *
157 * Note raw reads from iio channels are in adc counts and hence
158 * scale will need to be applied if standard units required.
159 *
160 * In opposit to the normal iio_read_channel_raw this function
161 * returns the average of multiple reads.
162 */
163int iio_read_channel_average_raw(struct iio_channel *chan, int *val);
164
165/**
Lars-Peter Clausen48e44ce2012-09-17 13:17:00 +0100166 * iio_read_channel_processed() - read processed value from a given channel
167 * @chan: The channel being queried.
168 * @val: Value read back.
169 *
170 * Returns an error code or 0.
171 *
172 * This function will read a processed value from a channel. A processed value
173 * means that this value will have the correct unit and not some device internal
174 * representation. If the device does not support reporting a processed value
175 * the function will query the raw value and the channels scale and offset and
176 * do the appropriate transformation.
177 */
178int iio_read_channel_processed(struct iio_channel *chan, int *val);
179
180/**
Dmitry Eremin-Solenikovf9380e72014-11-27 01:42:45 +0300181 * iio_write_channel_raw() - write to a given channel
182 * @chan: The channel being queried.
183 * @val: Value being written.
184 *
185 * Note raw writes to iio channels are in dac counts and hence
186 * scale will need to be applied if standard units required.
187 */
188int iio_write_channel_raw(struct iio_channel *chan, int val);
189
190/**
Jonathan Cameron314be142012-05-01 21:04:24 +0100191 * iio_get_channel_type() - get the type of a channel
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000192 * @channel: The channel being queried.
193 * @type: The type of the channel.
194 *
195 * returns the enum iio_chan_type of the channel
196 */
Jonathan Cameron314be142012-05-01 21:04:24 +0100197int iio_get_channel_type(struct iio_channel *channel,
198 enum iio_chan_type *type);
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000199
200/**
Jonathan Cameron314be142012-05-01 21:04:24 +0100201 * iio_read_channel_scale() - read the scale value for a channel
Lars-Peter Clausen45f010b2012-09-17 13:17:00 +0100202 * @chan: The channel being queried.
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000203 * @val: First part of value read back.
204 * @val2: Second part of value read back.
205 *
206 * Note returns a description of what is in val and val2, such
207 * as IIO_VAL_INT_PLUS_MICRO telling us we have a value of val
208 * + val2/1e6
209 */
Jonathan Cameron314be142012-05-01 21:04:24 +0100210int iio_read_channel_scale(struct iio_channel *chan, int *val,
211 int *val2);
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000212
Lars-Peter Clausen48e44ce2012-09-17 13:17:00 +0100213/**
214 * iio_convert_raw_to_processed() - Converts a raw value to a processed value
215 * @chan: The channel being queried
216 * @raw: The raw IIO to convert
217 * @processed: The result of the conversion
218 * @scale: Scale factor to apply during the conversion
219 *
220 * Returns an error code or 0.
221 *
222 * This function converts a raw value to processed value for a specific channel.
223 * A raw value is the device internal representation of a sample and the value
224 * returned by iio_read_channel_raw, so the unit of that value is device
225 * depended. A processed value on the other hand is value has a normed unit
226 * according with the IIO specification.
227 *
228 * The scale factor allows to increase the precession of the returned value. For
229 * a scale factor of 1 the function will return the result in the normal IIO
230 * unit for the channel type. E.g. millivolt for voltage channels, if you want
Ivan T. Ivanov09546a32014-09-23 15:51:42 +0300231 * nanovolts instead pass 1000000 as the scale factor.
Lars-Peter Clausen48e44ce2012-09-17 13:17:00 +0100232 */
233int iio_convert_raw_to_processed(struct iio_channel *chan, int raw,
234 int *processed, unsigned int scale);
235
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000236#endif