Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 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_ |
Lars-Peter Clausen | 88238fe | 2012-08-17 16:57:00 +0100 | [diff] [blame] | 11 | #define _IIO_INKERN_CONSUMER_H_ |
Greg Kroah-Hartman | 9f488ba | 2012-11-13 10:46:33 -0800 | [diff] [blame] | 12 | |
| 13 | #include <linux/types.h> |
Jonathan Cameron | 06458e2 | 2012-04-25 15:54:58 +0100 | [diff] [blame] | 14 | #include <linux/iio/types.h> |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 15 | |
| 16 | struct iio_dev; |
| 17 | struct iio_chan_spec; |
Guenter Roeck | ca7d98d | 2013-01-31 21:43:00 +0000 | [diff] [blame] | 18 | struct device; |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 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. |
Jonathan Cameron | 0464415 | 2012-06-30 20:06:00 +0100 | [diff] [blame] | 24 | * @data: Data about the channel used by consumer. |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 25 | */ |
| 26 | struct iio_channel { |
| 27 | struct iio_dev *indio_dev; |
| 28 | const struct iio_chan_spec *channel; |
Jonathan Cameron | 0464415 | 2012-06-30 20:06:00 +0100 | [diff] [blame] | 29 | void *data; |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 30 | }; |
| 31 | |
| 32 | /** |
| 33 | * iio_channel_get() - get description of all that is needed to access channel. |
Guenter Roeck | 5aa57f0 | 2013-02-04 20:26:00 +0000 | [diff] [blame] | 34 | * @dev: Pointer to consumer device. Device name must match |
| 35 | * the name of the device as provided in the iio_map |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 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 | */ |
Guenter Roeck | 5aa57f0 | 2013-02-04 20:26:00 +0000 | [diff] [blame] | 42 | struct iio_channel *iio_channel_get(struct device *dev, |
Jonathan Cameron | 314be14 | 2012-05-01 21:04:24 +0100 | [diff] [blame] | 43 | const char *consumer_channel); |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 44 | |
| 45 | /** |
Jonathan Cameron | 314be14 | 2012-05-01 21:04:24 +0100 | [diff] [blame] | 46 | * iio_channel_release() - release channels obtained via iio_channel_get |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 47 | * @chan: The channel to be released. |
| 48 | */ |
Jonathan Cameron | 314be14 | 2012-05-01 21:04:24 +0100 | [diff] [blame] | 49 | void iio_channel_release(struct iio_channel *chan); |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 50 | |
| 51 | /** |
Laxman Dewangan | 8bf872d | 2016-04-06 16:01:06 +0530 | [diff] [blame] | 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 | */ |
| 67 | struct 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 | */ |
| 76 | void devm_iio_channel_release(struct device *dev, struct iio_channel *chan); |
| 77 | |
| 78 | /** |
Jonathan Cameron | 314be14 | 2012-05-01 21:04:24 +0100 | [diff] [blame] | 79 | * iio_channel_get_all() - get all channels associated with a client |
Guenter Roeck | ca7d98d | 2013-01-31 21:43:00 +0000 | [diff] [blame] | 80 | * @dev: Pointer to consumer device. |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 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 | */ |
Guenter Roeck | ca7d98d | 2013-01-31 21:43:00 +0000 | [diff] [blame] | 87 | struct iio_channel *iio_channel_get_all(struct device *dev); |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 88 | |
| 89 | /** |
Jonathan Cameron | 314be14 | 2012-05-01 21:04:24 +0100 | [diff] [blame] | 90 | * iio_channel_release_all() - reverse iio_channel_get_all |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 91 | * @chan: Array of channels to be released. |
| 92 | */ |
Jonathan Cameron | 314be14 | 2012-05-01 21:04:24 +0100 | [diff] [blame] | 93 | void iio_channel_release_all(struct iio_channel *chan); |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 94 | |
Laxman Dewangan | efc2c01 | 2016-04-06 16:01:07 +0530 | [diff] [blame] | 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 | */ |
| 110 | struct 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 | */ |
| 119 | void devm_iio_channel_release_all(struct device *dev, struct iio_channel *chan); |
| 120 | |
Jonathan Cameron | 92d1079 | 2012-06-30 20:06:00 +0100 | [diff] [blame] | 121 | struct iio_cb_buffer; |
| 122 | /** |
| 123 | * iio_channel_get_all_cb() - register callback for triggered capture |
Guenter Roeck | ca7d98d | 2013-01-31 21:43:00 +0000 | [diff] [blame] | 124 | * @dev: Pointer to client device. |
Jonathan Cameron | 92d1079 | 2012-06-30 20:06:00 +0100 | [diff] [blame] | 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 | */ |
Guenter Roeck | ca7d98d | 2013-01-31 21:43:00 +0000 | [diff] [blame] | 132 | struct iio_cb_buffer *iio_channel_get_all_cb(struct device *dev, |
Lars-Peter Clausen | 5d65d92 | 2013-09-15 17:50:00 +0100 | [diff] [blame] | 133 | int (*cb)(const void *data, |
Jonathan Cameron | 92d1079 | 2012-06-30 20:06:00 +0100 | [diff] [blame] | 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 | */ |
| 140 | void 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 | */ |
| 146 | int 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 | */ |
| 152 | void 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. |
Cristina Opriceana | e52e951 | 2015-07-24 16:23:43 +0300 | [diff] [blame] | 156 | * @cb_buffer: The callback buffer from whom we want the channel |
Jonathan Cameron | 92d1079 | 2012-06-30 20:06:00 +0100 | [diff] [blame] | 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 | */ |
| 164 | struct iio_channel |
| 165 | *iio_channel_cb_get_channels(const struct iio_cb_buffer *cb_buffer); |
| 166 | |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 167 | /** |
Matt Ranostay | adca058 | 2016-08-19 20:17:02 -0700 | [diff] [blame] | 168 | * iio_channel_cb_get_iio_dev() - get access to the underlying device. |
| 169 | * @cb_buffer: The callback buffer from whom we want the device |
| 170 | * information. |
| 171 | * |
| 172 | * This function allows one to obtain information about the device. |
| 173 | * The primary aim is to allow drivers that are consuming a device to query |
| 174 | * things like current trigger. |
| 175 | */ |
| 176 | struct iio_dev |
| 177 | *iio_channel_cb_get_iio_dev(const struct iio_cb_buffer *cb_buffer); |
| 178 | |
| 179 | /** |
Jonathan Cameron | 314be14 | 2012-05-01 21:04:24 +0100 | [diff] [blame] | 180 | * iio_read_channel_raw() - read from a given channel |
Lars-Peter Clausen | 45f010b | 2012-09-17 13:17:00 +0100 | [diff] [blame] | 181 | * @chan: The channel being queried. |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 182 | * @val: Value read back. |
| 183 | * |
| 184 | * Note raw reads from iio channels are in adc counts and hence |
| 185 | * scale will need to be applied if standard units required. |
| 186 | */ |
Jonathan Cameron | 314be14 | 2012-05-01 21:04:24 +0100 | [diff] [blame] | 187 | int iio_read_channel_raw(struct iio_channel *chan, |
| 188 | int *val); |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 189 | |
| 190 | /** |
Sebastian Reichel | 476d4af | 2014-10-03 17:25:00 +0100 | [diff] [blame] | 191 | * iio_read_channel_average_raw() - read from a given channel |
| 192 | * @chan: The channel being queried. |
| 193 | * @val: Value read back. |
| 194 | * |
| 195 | * Note raw reads from iio channels are in adc counts and hence |
| 196 | * scale will need to be applied if standard units required. |
| 197 | * |
| 198 | * In opposit to the normal iio_read_channel_raw this function |
| 199 | * returns the average of multiple reads. |
| 200 | */ |
| 201 | int iio_read_channel_average_raw(struct iio_channel *chan, int *val); |
| 202 | |
| 203 | /** |
Lars-Peter Clausen | 48e44ce | 2012-09-17 13:17:00 +0100 | [diff] [blame] | 204 | * iio_read_channel_processed() - read processed value from a given channel |
| 205 | * @chan: The channel being queried. |
| 206 | * @val: Value read back. |
| 207 | * |
| 208 | * Returns an error code or 0. |
| 209 | * |
| 210 | * This function will read a processed value from a channel. A processed value |
| 211 | * means that this value will have the correct unit and not some device internal |
| 212 | * representation. If the device does not support reporting a processed value |
| 213 | * the function will query the raw value and the channels scale and offset and |
| 214 | * do the appropriate transformation. |
| 215 | */ |
| 216 | int iio_read_channel_processed(struct iio_channel *chan, int *val); |
| 217 | |
| 218 | /** |
Dmitry Eremin-Solenikov | f9380e7 | 2014-11-27 01:42:45 +0300 | [diff] [blame] | 219 | * iio_write_channel_raw() - write to a given channel |
| 220 | * @chan: The channel being queried. |
| 221 | * @val: Value being written. |
| 222 | * |
| 223 | * Note raw writes to iio channels are in dac counts and hence |
| 224 | * scale will need to be applied if standard units required. |
| 225 | */ |
| 226 | int iio_write_channel_raw(struct iio_channel *chan, int val); |
| 227 | |
| 228 | /** |
Peter Rosin | 00c5f80 | 2016-11-08 12:58:52 +0100 | [diff] [blame] | 229 | * iio_read_max_channel_raw() - read maximum available raw value from a given |
| 230 | * channel, i.e. the maximum possible value. |
| 231 | * @chan: The channel being queried. |
| 232 | * @val: Value read back. |
| 233 | * |
| 234 | * Note raw reads from iio channels are in adc counts and hence |
| 235 | * scale will need to be applied if standard units are required. |
| 236 | */ |
| 237 | int iio_read_max_channel_raw(struct iio_channel *chan, int *val); |
| 238 | |
| 239 | /** |
| 240 | * iio_read_avail_channel_raw() - read available raw values from a given channel |
| 241 | * @chan: The channel being queried. |
| 242 | * @vals: Available values read back. |
| 243 | * @length: Number of entries in vals. |
| 244 | * |
| 245 | * Returns an error code, IIO_AVAIL_RANGE or IIO_AVAIL_LIST. |
| 246 | * |
| 247 | * For ranges, three vals are always returned; min, step and max. |
| 248 | * For lists, all the possible values are enumerated. |
| 249 | * |
| 250 | * Note raw available values from iio channels are in adc counts and |
| 251 | * hence scale will need to be applied if standard units are required. |
| 252 | */ |
| 253 | int iio_read_avail_channel_raw(struct iio_channel *chan, |
| 254 | const int **vals, int *length); |
| 255 | |
| 256 | /** |
Jonathan Cameron | 314be14 | 2012-05-01 21:04:24 +0100 | [diff] [blame] | 257 | * iio_get_channel_type() - get the type of a channel |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 258 | * @channel: The channel being queried. |
| 259 | * @type: The type of the channel. |
| 260 | * |
| 261 | * returns the enum iio_chan_type of the channel |
| 262 | */ |
Jonathan Cameron | 314be14 | 2012-05-01 21:04:24 +0100 | [diff] [blame] | 263 | int iio_get_channel_type(struct iio_channel *channel, |
| 264 | enum iio_chan_type *type); |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 265 | |
| 266 | /** |
Matt Ranostay | 0023e67 | 2016-09-23 23:04:07 -0700 | [diff] [blame] | 267 | * iio_read_channel_offset() - read the offset value for a channel |
| 268 | * @chan: The channel being queried. |
| 269 | * @val: First part of value read back. |
| 270 | * @val2: Second part of value read back. |
| 271 | * |
| 272 | * Note returns a description of what is in val and val2, such |
| 273 | * as IIO_VAL_INT_PLUS_MICRO telling us we have a value of val |
| 274 | * + val2/1e6 |
| 275 | */ |
| 276 | int iio_read_channel_offset(struct iio_channel *chan, int *val, |
| 277 | int *val2); |
| 278 | |
| 279 | /** |
Jonathan Cameron | 314be14 | 2012-05-01 21:04:24 +0100 | [diff] [blame] | 280 | * iio_read_channel_scale() - read the scale value for a channel |
Lars-Peter Clausen | 45f010b | 2012-09-17 13:17:00 +0100 | [diff] [blame] | 281 | * @chan: The channel being queried. |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 282 | * @val: First part of value read back. |
| 283 | * @val2: Second part of value read back. |
| 284 | * |
| 285 | * Note returns a description of what is in val and val2, such |
| 286 | * as IIO_VAL_INT_PLUS_MICRO telling us we have a value of val |
| 287 | * + val2/1e6 |
| 288 | */ |
Jonathan Cameron | 314be14 | 2012-05-01 21:04:24 +0100 | [diff] [blame] | 289 | int iio_read_channel_scale(struct iio_channel *chan, int *val, |
| 290 | int *val2); |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 291 | |
Lars-Peter Clausen | 48e44ce | 2012-09-17 13:17:00 +0100 | [diff] [blame] | 292 | /** |
| 293 | * iio_convert_raw_to_processed() - Converts a raw value to a processed value |
| 294 | * @chan: The channel being queried |
| 295 | * @raw: The raw IIO to convert |
| 296 | * @processed: The result of the conversion |
| 297 | * @scale: Scale factor to apply during the conversion |
| 298 | * |
| 299 | * Returns an error code or 0. |
| 300 | * |
| 301 | * This function converts a raw value to processed value for a specific channel. |
| 302 | * A raw value is the device internal representation of a sample and the value |
| 303 | * returned by iio_read_channel_raw, so the unit of that value is device |
| 304 | * depended. A processed value on the other hand is value has a normed unit |
| 305 | * according with the IIO specification. |
| 306 | * |
| 307 | * The scale factor allows to increase the precession of the returned value. For |
| 308 | * a scale factor of 1 the function will return the result in the normal IIO |
| 309 | * unit for the channel type. E.g. millivolt for voltage channels, if you want |
Ivan T. Ivanov | 09546a3 | 2014-09-23 15:51:42 +0300 | [diff] [blame] | 310 | * nanovolts instead pass 1000000 as the scale factor. |
Lars-Peter Clausen | 48e44ce | 2012-09-17 13:17:00 +0100 | [diff] [blame] | 311 | */ |
| 312 | int iio_convert_raw_to_processed(struct iio_channel *chan, int raw, |
| 313 | int *processed, unsigned int scale); |
| 314 | |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 315 | #endif |