xref: /linux-6.15/include/linux/iio/consumer.h (revision bc5378fc)
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  */
22 struct iio_channel {
23 	struct iio_dev *indio_dev;
24 	const struct iio_chan_spec *channel;
25 };
26 
27 /**
28  * iio_channel_get() - get description of all that is needed to access channel.
29  * @name:		Unique name of the device as provided in the iio_map
30  *			with which the desired provider to consumer mapping
31  *			was registered.
32  * @consumer_channel:	Unique name to identify the channel on the consumer
33  *			side. This typically describes the channels use within
34  *			the consumer. E.g. 'battery_voltage'
35  */
36 struct iio_channel *iio_channel_get(const char *name,
37 				    const char *consumer_channel);
38 
39 /**
40  * iio_channel_release() - release channels obtained via iio_channel_get
41  * @chan:		The channel to be released.
42  */
43 void iio_channel_release(struct iio_channel *chan);
44 
45 /**
46  * iio_channel_get_all() - get all channels associated with a client
47  * @name:		name of consumer device.
48  *
49  * Returns an array of iio_channel structures terminated with one with
50  * null iio_dev pointer.
51  * This function is used by fairly generic consumers to get all the
52  * channels registered as having this consumer.
53  */
54 struct iio_channel *iio_channel_get_all(const char *name);
55 
56 /**
57  * iio_channel_release_all() - reverse iio_channel_get_all
58  * @chan:		Array of channels to be released.
59  */
60 void iio_channel_release_all(struct iio_channel *chan);
61 
62 /**
63  * iio_read_channel_raw() - read from a given channel
64  * @chan:		The channel being queried.
65  * @val:		Value read back.
66  *
67  * Note raw reads from iio channels are in adc counts and hence
68  * scale will need to be applied if standard units required.
69  */
70 int iio_read_channel_raw(struct iio_channel *chan,
71 			 int *val);
72 
73 /**
74  * iio_read_channel_processed() - read processed value from a given channel
75  * @chan:		The channel being queried.
76  * @val:		Value read back.
77  *
78  * Returns an error code or 0.
79  *
80  * This function will read a processed value from a channel. A processed value
81  * means that this value will have the correct unit and not some device internal
82  * representation. If the device does not support reporting a processed value
83  * the function will query the raw value and the channels scale and offset and
84  * do the appropriate transformation.
85  */
86 int iio_read_channel_processed(struct iio_channel *chan, int *val);
87 
88 /**
89  * iio_get_channel_type() - get the type of a channel
90  * @channel:		The channel being queried.
91  * @type:		The type of the channel.
92  *
93  * returns the enum iio_chan_type of the channel
94  */
95 int iio_get_channel_type(struct iio_channel *channel,
96 			 enum iio_chan_type *type);
97 
98 /**
99  * iio_read_channel_scale() - read the scale value for a channel
100  * @chan:		The channel being queried.
101  * @val:		First part of value read back.
102  * @val2:		Second part of value read back.
103  *
104  * Note returns a description of what is in val and val2, such
105  * as IIO_VAL_INT_PLUS_MICRO telling us we have a value of val
106  * + val2/1e6
107  */
108 int iio_read_channel_scale(struct iio_channel *chan, int *val,
109 			   int *val2);
110 
111 /**
112  * iio_convert_raw_to_processed() - Converts a raw value to a processed value
113  * @chan:		The channel being queried
114  * @raw:		The raw IIO to convert
115  * @processed:		The result of the conversion
116  * @scale:		Scale factor to apply during the conversion
117  *
118  * Returns an error code or 0.
119  *
120  * This function converts a raw value to processed value for a specific channel.
121  * A raw value is the device internal representation of a sample and the value
122  * returned by iio_read_channel_raw, so the unit of that value is device
123  * depended. A processed value on the other hand is value has a normed unit
124  * according with the IIO specification.
125  *
126  * The scale factor allows to increase the precession of the returned value. For
127  * a scale factor of 1 the function will return the result in the normal IIO
128  * unit for the channel type. E.g. millivolt for voltage channels, if you want
129  * nanovolts instead pass 1000 as the scale factor.
130  */
131 int iio_convert_raw_to_processed(struct iio_channel *chan, int raw,
132 	int *processed, unsigned int scale);
133 
134 #endif
135