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 13#include <linux/types.h> 14#include <linux/iio/types.h> 15 16struct iio_dev; 17struct iio_chan_spec; 18struct device; 19 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. 24 * @data: Data about the channel used by consumer. 25 */ 26struct iio_channel { 27 struct iio_dev *indio_dev; 28 const struct iio_chan_spec *channel; 29 void *data; 30}; 31 32/** 33 * iio_channel_get() - get description of all that is needed to access channel. 34 * @dev: Pointer to consumer device. Device name must match 35 * the name of the device as provided in the iio_map 36 * 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 */ 42struct iio_channel *iio_channel_get(struct device *dev, 43 const char *consumer_channel); 44 45/** 46 * iio_channel_release() - release channels obtained via iio_channel_get 47 * @chan: The channel to be released. 48 */ 49void iio_channel_release(struct iio_channel *chan); 50 51/** 52 * 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/** 79 * iio_channel_get_all() - get all channels associated with a client 80 * @dev: Pointer to consumer device. 81 * 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 */ 87struct iio_channel *iio_channel_get_all(struct device *dev); 88 89/** 90 * iio_channel_release_all() - reverse iio_channel_get_all 91 * @chan: Array of channels to be released. 92 */ 93void iio_channel_release_all(struct iio_channel *chan); 94 95/** 96 * devm_iio_channel_get_all() - Resource managed version of 97 * iio_channel_get_all(). 98 * @dev: Pointer to consumer device. 99 * 100 * Returns a pointer to negative errno if it is not able to get the iio channel 101 * otherwise returns an array of iio_channel structures terminated with one with 102 * null iio_dev pointer. 103 * 104 * This function is used by fairly generic consumers to get all the 105 * channels registered as having this consumer. 106 * 107 * The allocated iio channels are automatically released when the device is 108 * unbounded. 109 */ 110struct iio_channel *devm_iio_channel_get_all(struct device *dev); 111 112/** 113 * devm_iio_channel_release_all() - Resource managed version of 114 * iio_channel_release_all(). 115 * @dev: Pointer to consumer device for which resource 116 * is allocared. 117 * @chan: Array channel to be released. 118 */ 119void devm_iio_channel_release_all(struct device *dev, struct iio_channel *chan); 120 121struct iio_cb_buffer; 122/** 123 * iio_channel_get_all_cb() - register callback for triggered capture 124 * @dev: Pointer to client device. 125 * @cb: Callback function. 126 * @private: Private data passed to callback. 127 * 128 * NB right now we have no ability to mux data from multiple devices. 129 * So if the channels requested come from different devices this will 130 * fail. 131 */ 132struct iio_cb_buffer *iio_channel_get_all_cb(struct device *dev, 133 int (*cb)(const void *data, 134 void *private), 135 void *private); 136/** 137 * iio_channel_release_all_cb() - release and unregister the callback. 138 * @cb_buffer: The callback buffer that was allocated. 139 */ 140void iio_channel_release_all_cb(struct iio_cb_buffer *cb_buffer); 141 142/** 143 * iio_channel_start_all_cb() - start the flow of data through callback. 144 * @cb_buff: The callback buffer we are starting. 145 */ 146int iio_channel_start_all_cb(struct iio_cb_buffer *cb_buff); 147 148/** 149 * iio_channel_stop_all_cb() - stop the flow of data through the callback. 150 * @cb_buff: The callback buffer we are stopping. 151 */ 152void iio_channel_stop_all_cb(struct iio_cb_buffer *cb_buff); 153 154/** 155 * iio_channel_cb_get_channels() - get access to the underlying channels. 156 * @cb_buffer: The callback buffer from whom we want the channel 157 * information. 158 * 159 * This function allows one to obtain information about the channels. 160 * Whilst this may allow direct reading if all buffers are disabled, the 161 * primary aim is to allow drivers that are consuming a channel to query 162 * things like scaling of the channel. 163 */ 164struct iio_channel 165*iio_channel_cb_get_channels(const struct iio_cb_buffer *cb_buffer); 166 167/** 168 * iio_read_channel_raw() - read from a given channel 169 * @chan: The channel being queried. 170 * @val: Value read back. 171 * 172 * Note raw reads from iio channels are in adc counts and hence 173 * scale will need to be applied if standard units required. 174 */ 175int iio_read_channel_raw(struct iio_channel *chan, 176 int *val); 177 178/** 179 * iio_read_channel_average_raw() - read from a given channel 180 * @chan: The channel being queried. 181 * @val: Value read back. 182 * 183 * Note raw reads from iio channels are in adc counts and hence 184 * scale will need to be applied if standard units required. 185 * 186 * In opposit to the normal iio_read_channel_raw this function 187 * returns the average of multiple reads. 188 */ 189int iio_read_channel_average_raw(struct iio_channel *chan, int *val); 190 191/** 192 * iio_read_channel_processed() - read processed value from a given channel 193 * @chan: The channel being queried. 194 * @val: Value read back. 195 * 196 * Returns an error code or 0. 197 * 198 * This function will read a processed value from a channel. A processed value 199 * means that this value will have the correct unit and not some device internal 200 * representation. If the device does not support reporting a processed value 201 * the function will query the raw value and the channels scale and offset and 202 * do the appropriate transformation. 203 */ 204int iio_read_channel_processed(struct iio_channel *chan, int *val); 205 206/** 207 * iio_write_channel_raw() - write to a given channel 208 * @chan: The channel being queried. 209 * @val: Value being written. 210 * 211 * Note raw writes to iio channels are in dac counts and hence 212 * scale will need to be applied if standard units required. 213 */ 214int iio_write_channel_raw(struct iio_channel *chan, int val); 215 216/** 217 * iio_read_max_channel_raw() - read maximum available raw value from a given 218 * channel, i.e. the maximum possible value. 219 * @chan: The channel being queried. 220 * @val: Value read back. 221 * 222 * Note raw reads from iio channels are in adc counts and hence 223 * scale will need to be applied if standard units are required. 224 */ 225int iio_read_max_channel_raw(struct iio_channel *chan, int *val); 226 227/** 228 * iio_read_avail_channel_raw() - read available raw values from a given channel 229 * @chan: The channel being queried. 230 * @vals: Available values read back. 231 * @length: Number of entries in vals. 232 * 233 * Returns an error code, IIO_AVAIL_RANGE or IIO_AVAIL_LIST. 234 * 235 * For ranges, three vals are always returned; min, step and max. 236 * For lists, all the possible values are enumerated. 237 * 238 * Note raw available values from iio channels are in adc counts and 239 * hence scale will need to be applied if standard units are required. 240 */ 241int iio_read_avail_channel_raw(struct iio_channel *chan, 242 const int **vals, int *length); 243 244/** 245 * iio_get_channel_type() - get the type of a channel 246 * @channel: The channel being queried. 247 * @type: The type of the channel. 248 * 249 * returns the enum iio_chan_type of the channel 250 */ 251int iio_get_channel_type(struct iio_channel *channel, 252 enum iio_chan_type *type); 253 254/** 255 * iio_read_channel_offset() - read the offset value for a channel 256 * @chan: The channel being queried. 257 * @val: First part of value read back. 258 * @val2: Second part of value read back. 259 * 260 * Note returns a description of what is in val and val2, such 261 * as IIO_VAL_INT_PLUS_MICRO telling us we have a value of val 262 * + val2/1e6 263 */ 264int iio_read_channel_offset(struct iio_channel *chan, int *val, 265 int *val2); 266 267/** 268 * iio_read_channel_scale() - read the scale value for a channel 269 * @chan: The channel being queried. 270 * @val: First part of value read back. 271 * @val2: Second part of value read back. 272 * 273 * Note returns a description of what is in val and val2, such 274 * as IIO_VAL_INT_PLUS_MICRO telling us we have a value of val 275 * + val2/1e6 276 */ 277int iio_read_channel_scale(struct iio_channel *chan, int *val, 278 int *val2); 279 280/** 281 * iio_convert_raw_to_processed() - Converts a raw value to a processed value 282 * @chan: The channel being queried 283 * @raw: The raw IIO to convert 284 * @processed: The result of the conversion 285 * @scale: Scale factor to apply during the conversion 286 * 287 * Returns an error code or 0. 288 * 289 * This function converts a raw value to processed value for a specific channel. 290 * A raw value is the device internal representation of a sample and the value 291 * returned by iio_read_channel_raw, so the unit of that value is device 292 * depended. A processed value on the other hand is value has a normed unit 293 * according with the IIO specification. 294 * 295 * The scale factor allows to increase the precession of the returned value. For 296 * a scale factor of 1 the function will return the result in the normal IIO 297 * unit for the channel type. E.g. millivolt for voltage channels, if you want 298 * nanovolts instead pass 1000000 as the scale factor. 299 */ 300int iio_convert_raw_to_processed(struct iio_channel *chan, int raw, 301 int *processed, unsigned int scale); 302 303#endif 304