xref: /OK3568_Linux_fs/kernel/include/linux/iio/iio.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0-only */
2*4882a593Smuzhiyun 
3*4882a593Smuzhiyun /* The industrial I/O core
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (c) 2008 Jonathan Cameron
6*4882a593Smuzhiyun  */
7*4882a593Smuzhiyun #ifndef _INDUSTRIAL_IO_H_
8*4882a593Smuzhiyun #define _INDUSTRIAL_IO_H_
9*4882a593Smuzhiyun 
10*4882a593Smuzhiyun #include <linux/device.h>
11*4882a593Smuzhiyun #include <linux/cdev.h>
12*4882a593Smuzhiyun #include <linux/iio/types.h>
13*4882a593Smuzhiyun #include <linux/of.h>
14*4882a593Smuzhiyun /* IIO TODO LIST */
15*4882a593Smuzhiyun /*
16*4882a593Smuzhiyun  * Provide means of adjusting timer accuracy.
17*4882a593Smuzhiyun  * Currently assumes nano seconds.
18*4882a593Smuzhiyun  */
19*4882a593Smuzhiyun 
20*4882a593Smuzhiyun enum iio_shared_by {
21*4882a593Smuzhiyun 	IIO_SEPARATE,
22*4882a593Smuzhiyun 	IIO_SHARED_BY_TYPE,
23*4882a593Smuzhiyun 	IIO_SHARED_BY_DIR,
24*4882a593Smuzhiyun 	IIO_SHARED_BY_ALL
25*4882a593Smuzhiyun };
26*4882a593Smuzhiyun 
27*4882a593Smuzhiyun enum iio_endian {
28*4882a593Smuzhiyun 	IIO_CPU,
29*4882a593Smuzhiyun 	IIO_BE,
30*4882a593Smuzhiyun 	IIO_LE,
31*4882a593Smuzhiyun };
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun struct iio_chan_spec;
34*4882a593Smuzhiyun struct iio_dev;
35*4882a593Smuzhiyun 
36*4882a593Smuzhiyun /**
37*4882a593Smuzhiyun  * struct iio_chan_spec_ext_info - Extended channel info attribute
38*4882a593Smuzhiyun  * @name:	Info attribute name
39*4882a593Smuzhiyun  * @shared:	Whether this attribute is shared between all channels.
40*4882a593Smuzhiyun  * @read:	Read callback for this info attribute, may be NULL.
41*4882a593Smuzhiyun  * @write:	Write callback for this info attribute, may be NULL.
42*4882a593Smuzhiyun  * @private:	Data private to the driver.
43*4882a593Smuzhiyun  */
44*4882a593Smuzhiyun struct iio_chan_spec_ext_info {
45*4882a593Smuzhiyun 	const char *name;
46*4882a593Smuzhiyun 	enum iio_shared_by shared;
47*4882a593Smuzhiyun 	ssize_t (*read)(struct iio_dev *, uintptr_t private,
48*4882a593Smuzhiyun 			struct iio_chan_spec const *, char *buf);
49*4882a593Smuzhiyun 	ssize_t (*write)(struct iio_dev *, uintptr_t private,
50*4882a593Smuzhiyun 			 struct iio_chan_spec const *, const char *buf,
51*4882a593Smuzhiyun 			 size_t len);
52*4882a593Smuzhiyun 	uintptr_t private;
53*4882a593Smuzhiyun };
54*4882a593Smuzhiyun 
55*4882a593Smuzhiyun /**
56*4882a593Smuzhiyun  * struct iio_enum - Enum channel info attribute
57*4882a593Smuzhiyun  * @items:	An array of strings.
58*4882a593Smuzhiyun  * @num_items:	Length of the item array.
59*4882a593Smuzhiyun  * @set:	Set callback function, may be NULL.
60*4882a593Smuzhiyun  * @get:	Get callback function, may be NULL.
61*4882a593Smuzhiyun  *
62*4882a593Smuzhiyun  * The iio_enum struct can be used to implement enum style channel attributes.
63*4882a593Smuzhiyun  * Enum style attributes are those which have a set of strings which map to
64*4882a593Smuzhiyun  * unsigned integer values. The IIO enum helper code takes care of mapping
65*4882a593Smuzhiyun  * between value and string as well as generating a "_available" file which
66*4882a593Smuzhiyun  * contains a list of all available items. The set callback will be called when
67*4882a593Smuzhiyun  * the attribute is updated. The last parameter is the index to the newly
68*4882a593Smuzhiyun  * activated item. The get callback will be used to query the currently active
69*4882a593Smuzhiyun  * item and is supposed to return the index for it.
70*4882a593Smuzhiyun  */
71*4882a593Smuzhiyun struct iio_enum {
72*4882a593Smuzhiyun 	const char * const *items;
73*4882a593Smuzhiyun 	unsigned int num_items;
74*4882a593Smuzhiyun 	int (*set)(struct iio_dev *, const struct iio_chan_spec *, unsigned int);
75*4882a593Smuzhiyun 	int (*get)(struct iio_dev *, const struct iio_chan_spec *);
76*4882a593Smuzhiyun };
77*4882a593Smuzhiyun 
78*4882a593Smuzhiyun ssize_t iio_enum_available_read(struct iio_dev *indio_dev,
79*4882a593Smuzhiyun 	uintptr_t priv, const struct iio_chan_spec *chan, char *buf);
80*4882a593Smuzhiyun ssize_t iio_enum_read(struct iio_dev *indio_dev,
81*4882a593Smuzhiyun 	uintptr_t priv, const struct iio_chan_spec *chan, char *buf);
82*4882a593Smuzhiyun ssize_t iio_enum_write(struct iio_dev *indio_dev,
83*4882a593Smuzhiyun 	uintptr_t priv, const struct iio_chan_spec *chan, const char *buf,
84*4882a593Smuzhiyun 	size_t len);
85*4882a593Smuzhiyun 
86*4882a593Smuzhiyun /**
87*4882a593Smuzhiyun  * IIO_ENUM() - Initialize enum extended channel attribute
88*4882a593Smuzhiyun  * @_name:	Attribute name
89*4882a593Smuzhiyun  * @_shared:	Whether the attribute is shared between all channels
90*4882a593Smuzhiyun  * @_e:		Pointer to an iio_enum struct
91*4882a593Smuzhiyun  *
92*4882a593Smuzhiyun  * This should usually be used together with IIO_ENUM_AVAILABLE()
93*4882a593Smuzhiyun  */
94*4882a593Smuzhiyun #define IIO_ENUM(_name, _shared, _e) \
95*4882a593Smuzhiyun { \
96*4882a593Smuzhiyun 	.name = (_name), \
97*4882a593Smuzhiyun 	.shared = (_shared), \
98*4882a593Smuzhiyun 	.read = iio_enum_read, \
99*4882a593Smuzhiyun 	.write = iio_enum_write, \
100*4882a593Smuzhiyun 	.private = (uintptr_t)(_e), \
101*4882a593Smuzhiyun }
102*4882a593Smuzhiyun 
103*4882a593Smuzhiyun /**
104*4882a593Smuzhiyun  * IIO_ENUM_AVAILABLE() - Initialize enum available extended channel attribute
105*4882a593Smuzhiyun  * @_name:	Attribute name ("_available" will be appended to the name)
106*4882a593Smuzhiyun  * @_e:		Pointer to an iio_enum struct
107*4882a593Smuzhiyun  *
108*4882a593Smuzhiyun  * Creates a read only attribute which lists all the available enum items in a
109*4882a593Smuzhiyun  * space separated list. This should usually be used together with IIO_ENUM()
110*4882a593Smuzhiyun  */
111*4882a593Smuzhiyun #define IIO_ENUM_AVAILABLE(_name, _e) \
112*4882a593Smuzhiyun { \
113*4882a593Smuzhiyun 	.name = (_name "_available"), \
114*4882a593Smuzhiyun 	.shared = IIO_SHARED_BY_TYPE, \
115*4882a593Smuzhiyun 	.read = iio_enum_available_read, \
116*4882a593Smuzhiyun 	.private = (uintptr_t)(_e), \
117*4882a593Smuzhiyun }
118*4882a593Smuzhiyun 
119*4882a593Smuzhiyun /**
120*4882a593Smuzhiyun  * struct iio_mount_matrix - iio mounting matrix
121*4882a593Smuzhiyun  * @rotation: 3 dimensional space rotation matrix defining sensor alignment with
122*4882a593Smuzhiyun  *            main hardware
123*4882a593Smuzhiyun  */
124*4882a593Smuzhiyun struct iio_mount_matrix {
125*4882a593Smuzhiyun 	const char *rotation[9];
126*4882a593Smuzhiyun };
127*4882a593Smuzhiyun 
128*4882a593Smuzhiyun ssize_t iio_show_mount_matrix(struct iio_dev *indio_dev, uintptr_t priv,
129*4882a593Smuzhiyun 			      const struct iio_chan_spec *chan, char *buf);
130*4882a593Smuzhiyun int iio_read_mount_matrix(struct device *dev, const char *propname,
131*4882a593Smuzhiyun 			  struct iio_mount_matrix *matrix);
132*4882a593Smuzhiyun 
133*4882a593Smuzhiyun typedef const struct iio_mount_matrix *
134*4882a593Smuzhiyun 	(iio_get_mount_matrix_t)(const struct iio_dev *indio_dev,
135*4882a593Smuzhiyun 				 const struct iio_chan_spec *chan);
136*4882a593Smuzhiyun 
137*4882a593Smuzhiyun /**
138*4882a593Smuzhiyun  * IIO_MOUNT_MATRIX() - Initialize mount matrix extended channel attribute
139*4882a593Smuzhiyun  * @_shared:	Whether the attribute is shared between all channels
140*4882a593Smuzhiyun  * @_get:	Pointer to an iio_get_mount_matrix_t accessor
141*4882a593Smuzhiyun  */
142*4882a593Smuzhiyun #define IIO_MOUNT_MATRIX(_shared, _get) \
143*4882a593Smuzhiyun { \
144*4882a593Smuzhiyun 	.name = "mount_matrix", \
145*4882a593Smuzhiyun 	.shared = (_shared), \
146*4882a593Smuzhiyun 	.read = iio_show_mount_matrix, \
147*4882a593Smuzhiyun 	.private = (uintptr_t)(_get), \
148*4882a593Smuzhiyun }
149*4882a593Smuzhiyun 
150*4882a593Smuzhiyun /**
151*4882a593Smuzhiyun  * struct iio_event_spec - specification for a channel event
152*4882a593Smuzhiyun  * @type:		    Type of the event
153*4882a593Smuzhiyun  * @dir:		    Direction of the event
154*4882a593Smuzhiyun  * @mask_separate:	    Bit mask of enum iio_event_info values. Attributes
155*4882a593Smuzhiyun  *			    set in this mask will be registered per channel.
156*4882a593Smuzhiyun  * @mask_shared_by_type:    Bit mask of enum iio_event_info values. Attributes
157*4882a593Smuzhiyun  *			    set in this mask will be shared by channel type.
158*4882a593Smuzhiyun  * @mask_shared_by_dir:	    Bit mask of enum iio_event_info values. Attributes
159*4882a593Smuzhiyun  *			    set in this mask will be shared by channel type and
160*4882a593Smuzhiyun  *			    direction.
161*4882a593Smuzhiyun  * @mask_shared_by_all:	    Bit mask of enum iio_event_info values. Attributes
162*4882a593Smuzhiyun  *			    set in this mask will be shared by all channels.
163*4882a593Smuzhiyun  */
164*4882a593Smuzhiyun struct iio_event_spec {
165*4882a593Smuzhiyun 	enum iio_event_type type;
166*4882a593Smuzhiyun 	enum iio_event_direction dir;
167*4882a593Smuzhiyun 	unsigned long mask_separate;
168*4882a593Smuzhiyun 	unsigned long mask_shared_by_type;
169*4882a593Smuzhiyun 	unsigned long mask_shared_by_dir;
170*4882a593Smuzhiyun 	unsigned long mask_shared_by_all;
171*4882a593Smuzhiyun };
172*4882a593Smuzhiyun 
173*4882a593Smuzhiyun /**
174*4882a593Smuzhiyun  * struct iio_chan_spec - specification of a single channel
175*4882a593Smuzhiyun  * @type:		What type of measurement is the channel making.
176*4882a593Smuzhiyun  * @channel:		What number do we wish to assign the channel.
177*4882a593Smuzhiyun  * @channel2:		If there is a second number for a differential
178*4882a593Smuzhiyun  *			channel then this is it. If modified is set then the
179*4882a593Smuzhiyun  *			value here specifies the modifier.
180*4882a593Smuzhiyun  * @address:		Driver specific identifier.
181*4882a593Smuzhiyun  * @scan_index:		Monotonic index to give ordering in scans when read
182*4882a593Smuzhiyun  *			from a buffer.
183*4882a593Smuzhiyun  * @scan_type:		struct describing the scan type
184*4882a593Smuzhiyun  * @scan_type.sign:		's' or 'u' to specify signed or unsigned
185*4882a593Smuzhiyun  * @scan_type.realbits:		Number of valid bits of data
186*4882a593Smuzhiyun  * @scan_type.storagebits:	Realbits + padding
187*4882a593Smuzhiyun  * @scan_type.shift:		Shift right by this before masking out
188*4882a593Smuzhiyun  *				realbits.
189*4882a593Smuzhiyun  * @scan_type.repeat:		Number of times real/storage bits repeats.
190*4882a593Smuzhiyun  *				When the repeat element is more than 1, then
191*4882a593Smuzhiyun  *				the type element in sysfs will show a repeat
192*4882a593Smuzhiyun  *				value. Otherwise, the number of repetitions
193*4882a593Smuzhiyun  *				is omitted.
194*4882a593Smuzhiyun  * @scan_type.endianness:	little or big endian
195*4882a593Smuzhiyun  * @info_mask_separate: What information is to be exported that is specific to
196*4882a593Smuzhiyun  *			this channel.
197*4882a593Smuzhiyun  * @info_mask_separate_available: What availability information is to be
198*4882a593Smuzhiyun  *			exported that is specific to this channel.
199*4882a593Smuzhiyun  * @info_mask_shared_by_type: What information is to be exported that is shared
200*4882a593Smuzhiyun  *			by all channels of the same type.
201*4882a593Smuzhiyun  * @info_mask_shared_by_type_available: What availability information is to be
202*4882a593Smuzhiyun  *			exported that is shared by all channels of the same
203*4882a593Smuzhiyun  *			type.
204*4882a593Smuzhiyun  * @info_mask_shared_by_dir: What information is to be exported that is shared
205*4882a593Smuzhiyun  *			by all channels of the same direction.
206*4882a593Smuzhiyun  * @info_mask_shared_by_dir_available: What availability information is to be
207*4882a593Smuzhiyun  *			exported that is shared by all channels of the same
208*4882a593Smuzhiyun  *			direction.
209*4882a593Smuzhiyun  * @info_mask_shared_by_all: What information is to be exported that is shared
210*4882a593Smuzhiyun  *			by all channels.
211*4882a593Smuzhiyun  * @info_mask_shared_by_all_available: What availability information is to be
212*4882a593Smuzhiyun  *			exported that is shared by all channels.
213*4882a593Smuzhiyun  * @event_spec:		Array of events which should be registered for this
214*4882a593Smuzhiyun  *			channel.
215*4882a593Smuzhiyun  * @num_event_specs:	Size of the event_spec array.
216*4882a593Smuzhiyun  * @ext_info:		Array of extended info attributes for this channel.
217*4882a593Smuzhiyun  *			The array is NULL terminated, the last element should
218*4882a593Smuzhiyun  *			have its name field set to NULL.
219*4882a593Smuzhiyun  * @extend_name:	Allows labeling of channel attributes with an
220*4882a593Smuzhiyun  *			informative name. Note this has no effect codes etc,
221*4882a593Smuzhiyun  *			unlike modifiers.
222*4882a593Smuzhiyun  * @datasheet_name:	A name used in in-kernel mapping of channels. It should
223*4882a593Smuzhiyun  *			correspond to the first name that the channel is referred
224*4882a593Smuzhiyun  *			to by in the datasheet (e.g. IND), or the nearest
225*4882a593Smuzhiyun  *			possible compound name (e.g. IND-INC).
226*4882a593Smuzhiyun  * @modified:		Does a modifier apply to this channel. What these are
227*4882a593Smuzhiyun  *			depends on the channel type.  Modifier is set in
228*4882a593Smuzhiyun  *			channel2. Examples are IIO_MOD_X for axial sensors about
229*4882a593Smuzhiyun  *			the 'x' axis.
230*4882a593Smuzhiyun  * @indexed:		Specify the channel has a numerical index. If not,
231*4882a593Smuzhiyun  *			the channel index number will be suppressed for sysfs
232*4882a593Smuzhiyun  *			attributes but not for event codes.
233*4882a593Smuzhiyun  * @output:		Channel is output.
234*4882a593Smuzhiyun  * @differential:	Channel is differential.
235*4882a593Smuzhiyun  */
236*4882a593Smuzhiyun struct iio_chan_spec {
237*4882a593Smuzhiyun 	enum iio_chan_type	type;
238*4882a593Smuzhiyun 	int			channel;
239*4882a593Smuzhiyun 	int			channel2;
240*4882a593Smuzhiyun 	unsigned long		address;
241*4882a593Smuzhiyun 	int			scan_index;
242*4882a593Smuzhiyun 	struct {
243*4882a593Smuzhiyun 		char	sign;
244*4882a593Smuzhiyun 		u8	realbits;
245*4882a593Smuzhiyun 		u8	storagebits;
246*4882a593Smuzhiyun 		u8	shift;
247*4882a593Smuzhiyun 		u8	repeat;
248*4882a593Smuzhiyun 		enum iio_endian endianness;
249*4882a593Smuzhiyun 	} scan_type;
250*4882a593Smuzhiyun 	long			info_mask_separate;
251*4882a593Smuzhiyun 	long			info_mask_separate_available;
252*4882a593Smuzhiyun 	long			info_mask_shared_by_type;
253*4882a593Smuzhiyun 	long			info_mask_shared_by_type_available;
254*4882a593Smuzhiyun 	long			info_mask_shared_by_dir;
255*4882a593Smuzhiyun 	long			info_mask_shared_by_dir_available;
256*4882a593Smuzhiyun 	long			info_mask_shared_by_all;
257*4882a593Smuzhiyun 	long			info_mask_shared_by_all_available;
258*4882a593Smuzhiyun 	const struct iio_event_spec *event_spec;
259*4882a593Smuzhiyun 	unsigned int		num_event_specs;
260*4882a593Smuzhiyun 	const struct iio_chan_spec_ext_info *ext_info;
261*4882a593Smuzhiyun 	const char		*extend_name;
262*4882a593Smuzhiyun 	const char		*datasheet_name;
263*4882a593Smuzhiyun 	unsigned		modified:1;
264*4882a593Smuzhiyun 	unsigned		indexed:1;
265*4882a593Smuzhiyun 	unsigned		output:1;
266*4882a593Smuzhiyun 	unsigned		differential:1;
267*4882a593Smuzhiyun };
268*4882a593Smuzhiyun 
269*4882a593Smuzhiyun 
270*4882a593Smuzhiyun /**
271*4882a593Smuzhiyun  * iio_channel_has_info() - Checks whether a channel supports a info attribute
272*4882a593Smuzhiyun  * @chan: The channel to be queried
273*4882a593Smuzhiyun  * @type: Type of the info attribute to be checked
274*4882a593Smuzhiyun  *
275*4882a593Smuzhiyun  * Returns true if the channels supports reporting values for the given info
276*4882a593Smuzhiyun  * attribute type, false otherwise.
277*4882a593Smuzhiyun  */
iio_channel_has_info(const struct iio_chan_spec * chan,enum iio_chan_info_enum type)278*4882a593Smuzhiyun static inline bool iio_channel_has_info(const struct iio_chan_spec *chan,
279*4882a593Smuzhiyun 	enum iio_chan_info_enum type)
280*4882a593Smuzhiyun {
281*4882a593Smuzhiyun 	return (chan->info_mask_separate & BIT(type)) |
282*4882a593Smuzhiyun 		(chan->info_mask_shared_by_type & BIT(type)) |
283*4882a593Smuzhiyun 		(chan->info_mask_shared_by_dir & BIT(type)) |
284*4882a593Smuzhiyun 		(chan->info_mask_shared_by_all & BIT(type));
285*4882a593Smuzhiyun }
286*4882a593Smuzhiyun 
287*4882a593Smuzhiyun /**
288*4882a593Smuzhiyun  * iio_channel_has_available() - Checks if a channel has an available attribute
289*4882a593Smuzhiyun  * @chan: The channel to be queried
290*4882a593Smuzhiyun  * @type: Type of the available attribute to be checked
291*4882a593Smuzhiyun  *
292*4882a593Smuzhiyun  * Returns true if the channel supports reporting available values for the
293*4882a593Smuzhiyun  * given attribute type, false otherwise.
294*4882a593Smuzhiyun  */
iio_channel_has_available(const struct iio_chan_spec * chan,enum iio_chan_info_enum type)295*4882a593Smuzhiyun static inline bool iio_channel_has_available(const struct iio_chan_spec *chan,
296*4882a593Smuzhiyun 					     enum iio_chan_info_enum type)
297*4882a593Smuzhiyun {
298*4882a593Smuzhiyun 	return (chan->info_mask_separate_available & BIT(type)) |
299*4882a593Smuzhiyun 		(chan->info_mask_shared_by_type_available & BIT(type)) |
300*4882a593Smuzhiyun 		(chan->info_mask_shared_by_dir_available & BIT(type)) |
301*4882a593Smuzhiyun 		(chan->info_mask_shared_by_all_available & BIT(type));
302*4882a593Smuzhiyun }
303*4882a593Smuzhiyun 
304*4882a593Smuzhiyun #define IIO_CHAN_SOFT_TIMESTAMP(_si) {					\
305*4882a593Smuzhiyun 	.type = IIO_TIMESTAMP,						\
306*4882a593Smuzhiyun 	.channel = -1,							\
307*4882a593Smuzhiyun 	.scan_index = _si,						\
308*4882a593Smuzhiyun 	.scan_type = {							\
309*4882a593Smuzhiyun 		.sign = 's',						\
310*4882a593Smuzhiyun 		.realbits = 64,					\
311*4882a593Smuzhiyun 		.storagebits = 64,					\
312*4882a593Smuzhiyun 		},							\
313*4882a593Smuzhiyun }
314*4882a593Smuzhiyun 
315*4882a593Smuzhiyun s64 iio_get_time_ns(const struct iio_dev *indio_dev);
316*4882a593Smuzhiyun unsigned int iio_get_time_res(const struct iio_dev *indio_dev);
317*4882a593Smuzhiyun 
318*4882a593Smuzhiyun /* Device operating modes */
319*4882a593Smuzhiyun #define INDIO_DIRECT_MODE		0x01
320*4882a593Smuzhiyun #define INDIO_BUFFER_TRIGGERED		0x02
321*4882a593Smuzhiyun #define INDIO_BUFFER_SOFTWARE		0x04
322*4882a593Smuzhiyun #define INDIO_BUFFER_HARDWARE		0x08
323*4882a593Smuzhiyun #define INDIO_EVENT_TRIGGERED		0x10
324*4882a593Smuzhiyun #define INDIO_HARDWARE_TRIGGERED	0x20
325*4882a593Smuzhiyun 
326*4882a593Smuzhiyun #define INDIO_ALL_BUFFER_MODES					\
327*4882a593Smuzhiyun 	(INDIO_BUFFER_TRIGGERED | INDIO_BUFFER_HARDWARE | INDIO_BUFFER_SOFTWARE)
328*4882a593Smuzhiyun 
329*4882a593Smuzhiyun #define INDIO_ALL_TRIGGERED_MODES	\
330*4882a593Smuzhiyun 	(INDIO_BUFFER_TRIGGERED		\
331*4882a593Smuzhiyun 	 | INDIO_EVENT_TRIGGERED	\
332*4882a593Smuzhiyun 	 | INDIO_HARDWARE_TRIGGERED)
333*4882a593Smuzhiyun 
334*4882a593Smuzhiyun #define INDIO_MAX_RAW_ELEMENTS		4
335*4882a593Smuzhiyun 
336*4882a593Smuzhiyun struct iio_trigger; /* forward declaration */
337*4882a593Smuzhiyun 
338*4882a593Smuzhiyun /**
339*4882a593Smuzhiyun  * struct iio_info - constant information about device
340*4882a593Smuzhiyun  * @event_attrs:	event control attributes
341*4882a593Smuzhiyun  * @attrs:		general purpose device attributes
342*4882a593Smuzhiyun  * @read_raw:		function to request a value from the device.
343*4882a593Smuzhiyun  *			mask specifies which value. Note 0 means a reading of
344*4882a593Smuzhiyun  *			the channel in question.  Return value will specify the
345*4882a593Smuzhiyun  *			type of value returned by the device. val and val2 will
346*4882a593Smuzhiyun  *			contain the elements making up the returned value.
347*4882a593Smuzhiyun  * @read_raw_multi:	function to return values from the device.
348*4882a593Smuzhiyun  *			mask specifies which value. Note 0 means a reading of
349*4882a593Smuzhiyun  *			the channel in question.  Return value will specify the
350*4882a593Smuzhiyun  *			type of value returned by the device. vals pointer
351*4882a593Smuzhiyun  *			contain the elements making up the returned value.
352*4882a593Smuzhiyun  *			max_len specifies maximum number of elements
353*4882a593Smuzhiyun  *			vals pointer can contain. val_len is used to return
354*4882a593Smuzhiyun  *			length of valid elements in vals.
355*4882a593Smuzhiyun  * @read_avail:		function to return the available values from the device.
356*4882a593Smuzhiyun  *			mask specifies which value. Note 0 means the available
357*4882a593Smuzhiyun  *			values for the channel in question.  Return value
358*4882a593Smuzhiyun  *			specifies if a IIO_AVAIL_LIST or a IIO_AVAIL_RANGE is
359*4882a593Smuzhiyun  *			returned in vals. The type of the vals are returned in
360*4882a593Smuzhiyun  *			type and the number of vals is returned in length. For
361*4882a593Smuzhiyun  *			ranges, there are always three vals returned; min, step
362*4882a593Smuzhiyun  *			and max. For lists, all possible values are enumerated.
363*4882a593Smuzhiyun  * @write_raw:		function to write a value to the device.
364*4882a593Smuzhiyun  *			Parameters are the same as for read_raw.
365*4882a593Smuzhiyun  * @write_raw_get_fmt:	callback function to query the expected
366*4882a593Smuzhiyun  *			format/precision. If not set by the driver, write_raw
367*4882a593Smuzhiyun  *			returns IIO_VAL_INT_PLUS_MICRO.
368*4882a593Smuzhiyun  * @read_event_config:	find out if the event is enabled.
369*4882a593Smuzhiyun  * @write_event_config:	set if the event is enabled.
370*4882a593Smuzhiyun  * @read_event_value:	read a configuration value associated with the event.
371*4882a593Smuzhiyun  * @write_event_value:	write a configuration value for the event.
372*4882a593Smuzhiyun  * @validate_trigger:	function to validate the trigger when the
373*4882a593Smuzhiyun  *			current trigger gets changed.
374*4882a593Smuzhiyun  * @update_scan_mode:	function to configure device and scan buffer when
375*4882a593Smuzhiyun  *			channels have changed
376*4882a593Smuzhiyun  * @debugfs_reg_access:	function to read or write register value of device
377*4882a593Smuzhiyun  * @of_xlate:		function pointer to obtain channel specifier index.
378*4882a593Smuzhiyun  *			When #iio-cells is greater than '0', the driver could
379*4882a593Smuzhiyun  *			provide a custom of_xlate function that reads the
380*4882a593Smuzhiyun  *			*args* and returns the appropriate index in registered
381*4882a593Smuzhiyun  *			IIO channels array.
382*4882a593Smuzhiyun  * @hwfifo_set_watermark: function pointer to set the current hardware
383*4882a593Smuzhiyun  *			fifo watermark level; see hwfifo_* entries in
384*4882a593Smuzhiyun  *			Documentation/ABI/testing/sysfs-bus-iio for details on
385*4882a593Smuzhiyun  *			how the hardware fifo operates
386*4882a593Smuzhiyun  * @hwfifo_flush_to_buffer: function pointer to flush the samples stored
387*4882a593Smuzhiyun  *			in the hardware fifo to the device buffer. The driver
388*4882a593Smuzhiyun  *			should not flush more than count samples. The function
389*4882a593Smuzhiyun  *			must return the number of samples flushed, 0 if no
390*4882a593Smuzhiyun  *			samples were flushed or a negative integer if no samples
391*4882a593Smuzhiyun  *			were flushed and there was an error.
392*4882a593Smuzhiyun  **/
393*4882a593Smuzhiyun struct iio_info {
394*4882a593Smuzhiyun 	const struct attribute_group	*event_attrs;
395*4882a593Smuzhiyun 	const struct attribute_group	*attrs;
396*4882a593Smuzhiyun 
397*4882a593Smuzhiyun 	int (*read_raw)(struct iio_dev *indio_dev,
398*4882a593Smuzhiyun 			struct iio_chan_spec const *chan,
399*4882a593Smuzhiyun 			int *val,
400*4882a593Smuzhiyun 			int *val2,
401*4882a593Smuzhiyun 			long mask);
402*4882a593Smuzhiyun 
403*4882a593Smuzhiyun 	int (*read_raw_multi)(struct iio_dev *indio_dev,
404*4882a593Smuzhiyun 			struct iio_chan_spec const *chan,
405*4882a593Smuzhiyun 			int max_len,
406*4882a593Smuzhiyun 			int *vals,
407*4882a593Smuzhiyun 			int *val_len,
408*4882a593Smuzhiyun 			long mask);
409*4882a593Smuzhiyun 
410*4882a593Smuzhiyun 	int (*read_avail)(struct iio_dev *indio_dev,
411*4882a593Smuzhiyun 			  struct iio_chan_spec const *chan,
412*4882a593Smuzhiyun 			  const int **vals,
413*4882a593Smuzhiyun 			  int *type,
414*4882a593Smuzhiyun 			  int *length,
415*4882a593Smuzhiyun 			  long mask);
416*4882a593Smuzhiyun 
417*4882a593Smuzhiyun 	int (*write_raw)(struct iio_dev *indio_dev,
418*4882a593Smuzhiyun 			 struct iio_chan_spec const *chan,
419*4882a593Smuzhiyun 			 int val,
420*4882a593Smuzhiyun 			 int val2,
421*4882a593Smuzhiyun 			 long mask);
422*4882a593Smuzhiyun 
423*4882a593Smuzhiyun 	int (*write_raw_get_fmt)(struct iio_dev *indio_dev,
424*4882a593Smuzhiyun 			 struct iio_chan_spec const *chan,
425*4882a593Smuzhiyun 			 long mask);
426*4882a593Smuzhiyun 
427*4882a593Smuzhiyun 	int (*read_event_config)(struct iio_dev *indio_dev,
428*4882a593Smuzhiyun 				 const struct iio_chan_spec *chan,
429*4882a593Smuzhiyun 				 enum iio_event_type type,
430*4882a593Smuzhiyun 				 enum iio_event_direction dir);
431*4882a593Smuzhiyun 
432*4882a593Smuzhiyun 	int (*write_event_config)(struct iio_dev *indio_dev,
433*4882a593Smuzhiyun 				  const struct iio_chan_spec *chan,
434*4882a593Smuzhiyun 				  enum iio_event_type type,
435*4882a593Smuzhiyun 				  enum iio_event_direction dir,
436*4882a593Smuzhiyun 				  int state);
437*4882a593Smuzhiyun 
438*4882a593Smuzhiyun 	int (*read_event_value)(struct iio_dev *indio_dev,
439*4882a593Smuzhiyun 				const struct iio_chan_spec *chan,
440*4882a593Smuzhiyun 				enum iio_event_type type,
441*4882a593Smuzhiyun 				enum iio_event_direction dir,
442*4882a593Smuzhiyun 				enum iio_event_info info, int *val, int *val2);
443*4882a593Smuzhiyun 
444*4882a593Smuzhiyun 	int (*write_event_value)(struct iio_dev *indio_dev,
445*4882a593Smuzhiyun 				 const struct iio_chan_spec *chan,
446*4882a593Smuzhiyun 				 enum iio_event_type type,
447*4882a593Smuzhiyun 				 enum iio_event_direction dir,
448*4882a593Smuzhiyun 				 enum iio_event_info info, int val, int val2);
449*4882a593Smuzhiyun 
450*4882a593Smuzhiyun 	int (*validate_trigger)(struct iio_dev *indio_dev,
451*4882a593Smuzhiyun 				struct iio_trigger *trig);
452*4882a593Smuzhiyun 	int (*update_scan_mode)(struct iio_dev *indio_dev,
453*4882a593Smuzhiyun 				const unsigned long *scan_mask);
454*4882a593Smuzhiyun 	int (*debugfs_reg_access)(struct iio_dev *indio_dev,
455*4882a593Smuzhiyun 				  unsigned reg, unsigned writeval,
456*4882a593Smuzhiyun 				  unsigned *readval);
457*4882a593Smuzhiyun 	int (*of_xlate)(struct iio_dev *indio_dev,
458*4882a593Smuzhiyun 			const struct of_phandle_args *iiospec);
459*4882a593Smuzhiyun 	int (*hwfifo_set_watermark)(struct iio_dev *indio_dev, unsigned val);
460*4882a593Smuzhiyun 	int (*hwfifo_flush_to_buffer)(struct iio_dev *indio_dev,
461*4882a593Smuzhiyun 				      unsigned count);
462*4882a593Smuzhiyun };
463*4882a593Smuzhiyun 
464*4882a593Smuzhiyun /**
465*4882a593Smuzhiyun  * struct iio_buffer_setup_ops - buffer setup related callbacks
466*4882a593Smuzhiyun  * @preenable:		[DRIVER] function to run prior to marking buffer enabled
467*4882a593Smuzhiyun  * @postenable:		[DRIVER] function to run after marking buffer enabled
468*4882a593Smuzhiyun  * @predisable:		[DRIVER] function to run prior to marking buffer
469*4882a593Smuzhiyun  *			disabled
470*4882a593Smuzhiyun  * @postdisable:	[DRIVER] function to run after marking buffer disabled
471*4882a593Smuzhiyun  * @validate_scan_mask: [DRIVER] function callback to check whether a given
472*4882a593Smuzhiyun  *			scan mask is valid for the device.
473*4882a593Smuzhiyun  */
474*4882a593Smuzhiyun struct iio_buffer_setup_ops {
475*4882a593Smuzhiyun 	int (*preenable)(struct iio_dev *);
476*4882a593Smuzhiyun 	int (*postenable)(struct iio_dev *);
477*4882a593Smuzhiyun 	int (*predisable)(struct iio_dev *);
478*4882a593Smuzhiyun 	int (*postdisable)(struct iio_dev *);
479*4882a593Smuzhiyun 	bool (*validate_scan_mask)(struct iio_dev *indio_dev,
480*4882a593Smuzhiyun 				   const unsigned long *scan_mask);
481*4882a593Smuzhiyun };
482*4882a593Smuzhiyun 
483*4882a593Smuzhiyun /**
484*4882a593Smuzhiyun  * struct iio_dev - industrial I/O device
485*4882a593Smuzhiyun  * @id:			[INTERN] used to identify device internally
486*4882a593Smuzhiyun  * @driver_module:	[INTERN] used to make it harder to undercut users
487*4882a593Smuzhiyun  * @modes:		[DRIVER] operating modes supported by device
488*4882a593Smuzhiyun  * @currentmode:	[DRIVER] current operating mode
489*4882a593Smuzhiyun  * @dev:		[DRIVER] device structure, should be assigned a parent
490*4882a593Smuzhiyun  *			and owner
491*4882a593Smuzhiyun  * @buffer:		[DRIVER] any buffer present
492*4882a593Smuzhiyun  * @scan_bytes:		[INTERN] num bytes captured to be fed to buffer demux
493*4882a593Smuzhiyun  * @mlock:		[INTERN] lock used to prevent simultaneous device state
494*4882a593Smuzhiyun  *			changes
495*4882a593Smuzhiyun  * @available_scan_masks: [DRIVER] optional array of allowed bitmasks
496*4882a593Smuzhiyun  * @masklength:		[INTERN] the length of the mask established from
497*4882a593Smuzhiyun  *			channels
498*4882a593Smuzhiyun  * @active_scan_mask:	[INTERN] union of all scan masks requested by buffers
499*4882a593Smuzhiyun  * @scan_timestamp:	[INTERN] set if any buffers have requested timestamp
500*4882a593Smuzhiyun  * @scan_index_timestamp:[INTERN] cache of the index to the timestamp
501*4882a593Smuzhiyun  * @trig:		[INTERN] current device trigger (buffer modes)
502*4882a593Smuzhiyun  * @trig_readonly:	[INTERN] mark the current trigger immutable
503*4882a593Smuzhiyun  * @pollfunc:		[DRIVER] function run on trigger being received
504*4882a593Smuzhiyun  * @pollfunc_event:	[DRIVER] function run on events trigger being received
505*4882a593Smuzhiyun  * @channels:		[DRIVER] channel specification structure table
506*4882a593Smuzhiyun  * @num_channels:	[DRIVER] number of channels specified in @channels.
507*4882a593Smuzhiyun  * @name:		[DRIVER] name of the device.
508*4882a593Smuzhiyun  * @label:              [DRIVER] unique name to identify which device this is
509*4882a593Smuzhiyun  * @info:		[DRIVER] callbacks and constant info from driver
510*4882a593Smuzhiyun  * @clock_id:		[INTERN] timestamping clock posix identifier
511*4882a593Smuzhiyun  * @info_exist_lock:	[INTERN] lock to prevent use during removal
512*4882a593Smuzhiyun  * @setup_ops:		[DRIVER] callbacks to call before and after buffer
513*4882a593Smuzhiyun  *			enable/disable
514*4882a593Smuzhiyun  * @chrdev:		[INTERN] associated character device
515*4882a593Smuzhiyun  * @groups:		[INTERN] attribute groups
516*4882a593Smuzhiyun  * @groupcounter:	[INTERN] index of next attribute group
517*4882a593Smuzhiyun  * @flags:		[INTERN] file ops related flags including busy flag.
518*4882a593Smuzhiyun  * @priv:		[DRIVER] reference to driver's private information
519*4882a593Smuzhiyun  *			**MUST** be accessed **ONLY** via iio_priv() helper
520*4882a593Smuzhiyun  */
521*4882a593Smuzhiyun struct iio_dev {
522*4882a593Smuzhiyun 	int				id;
523*4882a593Smuzhiyun 	struct module			*driver_module;
524*4882a593Smuzhiyun 
525*4882a593Smuzhiyun 	int				modes;
526*4882a593Smuzhiyun 	int				currentmode;
527*4882a593Smuzhiyun 	struct device			dev;
528*4882a593Smuzhiyun 
529*4882a593Smuzhiyun 	struct iio_buffer		*buffer;
530*4882a593Smuzhiyun 	int				scan_bytes;
531*4882a593Smuzhiyun 	struct mutex			mlock;
532*4882a593Smuzhiyun 
533*4882a593Smuzhiyun 	const unsigned long		*available_scan_masks;
534*4882a593Smuzhiyun 	unsigned			masklength;
535*4882a593Smuzhiyun 	const unsigned long		*active_scan_mask;
536*4882a593Smuzhiyun 	bool				scan_timestamp;
537*4882a593Smuzhiyun 	unsigned			scan_index_timestamp;
538*4882a593Smuzhiyun 	struct iio_trigger		*trig;
539*4882a593Smuzhiyun 	bool				trig_readonly;
540*4882a593Smuzhiyun 	struct iio_poll_func		*pollfunc;
541*4882a593Smuzhiyun 	struct iio_poll_func		*pollfunc_event;
542*4882a593Smuzhiyun 
543*4882a593Smuzhiyun 	struct iio_chan_spec const	*channels;
544*4882a593Smuzhiyun 	int				num_channels;
545*4882a593Smuzhiyun 
546*4882a593Smuzhiyun 	const char			*name;
547*4882a593Smuzhiyun 	const char			*label;
548*4882a593Smuzhiyun 	const struct iio_info		*info;
549*4882a593Smuzhiyun 	clockid_t			clock_id;
550*4882a593Smuzhiyun 	struct mutex			info_exist_lock;
551*4882a593Smuzhiyun 	const struct iio_buffer_setup_ops	*setup_ops;
552*4882a593Smuzhiyun 	struct cdev			chrdev;
553*4882a593Smuzhiyun #define IIO_MAX_GROUPS 6
554*4882a593Smuzhiyun 	const struct attribute_group	*groups[IIO_MAX_GROUPS + 1];
555*4882a593Smuzhiyun 	int				groupcounter;
556*4882a593Smuzhiyun 
557*4882a593Smuzhiyun 	unsigned long			flags;
558*4882a593Smuzhiyun 	void				*priv;
559*4882a593Smuzhiyun };
560*4882a593Smuzhiyun 
561*4882a593Smuzhiyun const struct iio_chan_spec
562*4882a593Smuzhiyun *iio_find_channel_from_si(struct iio_dev *indio_dev, int si);
563*4882a593Smuzhiyun /**
564*4882a593Smuzhiyun  * iio_device_register() - register a device with the IIO subsystem
565*4882a593Smuzhiyun  * @indio_dev:		Device structure filled by the device driver
566*4882a593Smuzhiyun  **/
567*4882a593Smuzhiyun #define iio_device_register(indio_dev) \
568*4882a593Smuzhiyun 	__iio_device_register((indio_dev), THIS_MODULE)
569*4882a593Smuzhiyun int __iio_device_register(struct iio_dev *indio_dev, struct module *this_mod);
570*4882a593Smuzhiyun void iio_device_unregister(struct iio_dev *indio_dev);
571*4882a593Smuzhiyun /**
572*4882a593Smuzhiyun  * devm_iio_device_register - Resource-managed iio_device_register()
573*4882a593Smuzhiyun  * @dev:	Device to allocate iio_dev for
574*4882a593Smuzhiyun  * @indio_dev:	Device structure filled by the device driver
575*4882a593Smuzhiyun  *
576*4882a593Smuzhiyun  * Managed iio_device_register.  The IIO device registered with this
577*4882a593Smuzhiyun  * function is automatically unregistered on driver detach. This function
578*4882a593Smuzhiyun  * calls iio_device_register() internally. Refer to that function for more
579*4882a593Smuzhiyun  * information.
580*4882a593Smuzhiyun  *
581*4882a593Smuzhiyun  * RETURNS:
582*4882a593Smuzhiyun  * 0 on success, negative error number on failure.
583*4882a593Smuzhiyun  */
584*4882a593Smuzhiyun #define devm_iio_device_register(dev, indio_dev) \
585*4882a593Smuzhiyun 	__devm_iio_device_register((dev), (indio_dev), THIS_MODULE)
586*4882a593Smuzhiyun int __devm_iio_device_register(struct device *dev, struct iio_dev *indio_dev,
587*4882a593Smuzhiyun 			       struct module *this_mod);
588*4882a593Smuzhiyun int iio_push_event(struct iio_dev *indio_dev, u64 ev_code, s64 timestamp);
589*4882a593Smuzhiyun int iio_device_claim_direct_mode(struct iio_dev *indio_dev);
590*4882a593Smuzhiyun void iio_device_release_direct_mode(struct iio_dev *indio_dev);
591*4882a593Smuzhiyun 
592*4882a593Smuzhiyun extern struct bus_type iio_bus_type;
593*4882a593Smuzhiyun 
594*4882a593Smuzhiyun /**
595*4882a593Smuzhiyun  * iio_device_put() - reference counted deallocation of struct device
596*4882a593Smuzhiyun  * @indio_dev: IIO device structure containing the device
597*4882a593Smuzhiyun  **/
iio_device_put(struct iio_dev * indio_dev)598*4882a593Smuzhiyun static inline void iio_device_put(struct iio_dev *indio_dev)
599*4882a593Smuzhiyun {
600*4882a593Smuzhiyun 	if (indio_dev)
601*4882a593Smuzhiyun 		put_device(&indio_dev->dev);
602*4882a593Smuzhiyun }
603*4882a593Smuzhiyun 
604*4882a593Smuzhiyun /**
605*4882a593Smuzhiyun  * iio_device_get_clock() - Retrieve current timestamping clock for the device
606*4882a593Smuzhiyun  * @indio_dev: IIO device structure containing the device
607*4882a593Smuzhiyun  */
iio_device_get_clock(const struct iio_dev * indio_dev)608*4882a593Smuzhiyun static inline clockid_t iio_device_get_clock(const struct iio_dev *indio_dev)
609*4882a593Smuzhiyun {
610*4882a593Smuzhiyun 	return indio_dev->clock_id;
611*4882a593Smuzhiyun }
612*4882a593Smuzhiyun 
613*4882a593Smuzhiyun int iio_device_set_clock(struct iio_dev *indio_dev, clockid_t clock_id);
614*4882a593Smuzhiyun 
615*4882a593Smuzhiyun /**
616*4882a593Smuzhiyun  * dev_to_iio_dev() - Get IIO device struct from a device struct
617*4882a593Smuzhiyun  * @dev: 		The device embedded in the IIO device
618*4882a593Smuzhiyun  *
619*4882a593Smuzhiyun  * Note: The device must be a IIO device, otherwise the result is undefined.
620*4882a593Smuzhiyun  */
dev_to_iio_dev(struct device * dev)621*4882a593Smuzhiyun static inline struct iio_dev *dev_to_iio_dev(struct device *dev)
622*4882a593Smuzhiyun {
623*4882a593Smuzhiyun 	return container_of(dev, struct iio_dev, dev);
624*4882a593Smuzhiyun }
625*4882a593Smuzhiyun 
626*4882a593Smuzhiyun /**
627*4882a593Smuzhiyun  * iio_device_get() - increment reference count for the device
628*4882a593Smuzhiyun  * @indio_dev: 		IIO device structure
629*4882a593Smuzhiyun  *
630*4882a593Smuzhiyun  * Returns: The passed IIO device
631*4882a593Smuzhiyun  **/
iio_device_get(struct iio_dev * indio_dev)632*4882a593Smuzhiyun static inline struct iio_dev *iio_device_get(struct iio_dev *indio_dev)
633*4882a593Smuzhiyun {
634*4882a593Smuzhiyun 	return indio_dev ? dev_to_iio_dev(get_device(&indio_dev->dev)) : NULL;
635*4882a593Smuzhiyun }
636*4882a593Smuzhiyun 
637*4882a593Smuzhiyun /**
638*4882a593Smuzhiyun  * iio_device_set_parent() - assign parent device to the IIO device object
639*4882a593Smuzhiyun  * @indio_dev: 		IIO device structure
640*4882a593Smuzhiyun  * @parent:		reference to parent device object
641*4882a593Smuzhiyun  *
642*4882a593Smuzhiyun  * This utility must be called between IIO device allocation
643*4882a593Smuzhiyun  * (via devm_iio_device_alloc()) & IIO device registration
644*4882a593Smuzhiyun  * (via iio_device_register() and devm_iio_device_register())).
645*4882a593Smuzhiyun  * By default, the device allocation will also assign a parent device to
646*4882a593Smuzhiyun  * the IIO device object. In cases where devm_iio_device_alloc() is used,
647*4882a593Smuzhiyun  * sometimes the parent device must be different than the device used to
648*4882a593Smuzhiyun  * manage the allocation.
649*4882a593Smuzhiyun  * In that case, this helper should be used to change the parent, hence the
650*4882a593Smuzhiyun  * requirement to call this between allocation & registration.
651*4882a593Smuzhiyun  **/
iio_device_set_parent(struct iio_dev * indio_dev,struct device * parent)652*4882a593Smuzhiyun static inline void iio_device_set_parent(struct iio_dev *indio_dev,
653*4882a593Smuzhiyun 					 struct device *parent)
654*4882a593Smuzhiyun {
655*4882a593Smuzhiyun 	indio_dev->dev.parent = parent;
656*4882a593Smuzhiyun }
657*4882a593Smuzhiyun 
658*4882a593Smuzhiyun /**
659*4882a593Smuzhiyun  * iio_device_set_drvdata() - Set device driver data
660*4882a593Smuzhiyun  * @indio_dev: IIO device structure
661*4882a593Smuzhiyun  * @data: Driver specific data
662*4882a593Smuzhiyun  *
663*4882a593Smuzhiyun  * Allows to attach an arbitrary pointer to an IIO device, which can later be
664*4882a593Smuzhiyun  * retrieved by iio_device_get_drvdata().
665*4882a593Smuzhiyun  */
iio_device_set_drvdata(struct iio_dev * indio_dev,void * data)666*4882a593Smuzhiyun static inline void iio_device_set_drvdata(struct iio_dev *indio_dev, void *data)
667*4882a593Smuzhiyun {
668*4882a593Smuzhiyun 	dev_set_drvdata(&indio_dev->dev, data);
669*4882a593Smuzhiyun }
670*4882a593Smuzhiyun 
671*4882a593Smuzhiyun /**
672*4882a593Smuzhiyun  * iio_device_get_drvdata() - Get device driver data
673*4882a593Smuzhiyun  * @indio_dev: IIO device structure
674*4882a593Smuzhiyun  *
675*4882a593Smuzhiyun  * Returns the data previously set with iio_device_set_drvdata()
676*4882a593Smuzhiyun  */
iio_device_get_drvdata(const struct iio_dev * indio_dev)677*4882a593Smuzhiyun static inline void *iio_device_get_drvdata(const struct iio_dev *indio_dev)
678*4882a593Smuzhiyun {
679*4882a593Smuzhiyun 	return dev_get_drvdata(&indio_dev->dev);
680*4882a593Smuzhiyun }
681*4882a593Smuzhiyun 
682*4882a593Smuzhiyun /* Can we make this smaller? */
683*4882a593Smuzhiyun #define IIO_ALIGN L1_CACHE_BYTES
684*4882a593Smuzhiyun struct iio_dev *iio_device_alloc(struct device *parent, int sizeof_priv);
685*4882a593Smuzhiyun 
686*4882a593Smuzhiyun /* The information at the returned address is guaranteed to be cacheline aligned */
iio_priv(const struct iio_dev * indio_dev)687*4882a593Smuzhiyun static inline void *iio_priv(const struct iio_dev *indio_dev)
688*4882a593Smuzhiyun {
689*4882a593Smuzhiyun 	return indio_dev->priv;
690*4882a593Smuzhiyun }
691*4882a593Smuzhiyun 
692*4882a593Smuzhiyun void iio_device_free(struct iio_dev *indio_dev);
693*4882a593Smuzhiyun struct iio_dev *devm_iio_device_alloc(struct device *parent, int sizeof_priv);
694*4882a593Smuzhiyun __printf(2, 3)
695*4882a593Smuzhiyun struct iio_trigger *devm_iio_trigger_alloc(struct device *dev,
696*4882a593Smuzhiyun 					   const char *fmt, ...);
697*4882a593Smuzhiyun /**
698*4882a593Smuzhiyun  * iio_buffer_enabled() - helper function to test if the buffer is enabled
699*4882a593Smuzhiyun  * @indio_dev:		IIO device structure for device
700*4882a593Smuzhiyun  **/
iio_buffer_enabled(struct iio_dev * indio_dev)701*4882a593Smuzhiyun static inline bool iio_buffer_enabled(struct iio_dev *indio_dev)
702*4882a593Smuzhiyun {
703*4882a593Smuzhiyun 	return indio_dev->currentmode
704*4882a593Smuzhiyun 		& (INDIO_BUFFER_TRIGGERED | INDIO_BUFFER_HARDWARE |
705*4882a593Smuzhiyun 		   INDIO_BUFFER_SOFTWARE);
706*4882a593Smuzhiyun }
707*4882a593Smuzhiyun 
708*4882a593Smuzhiyun /**
709*4882a593Smuzhiyun  * iio_get_debugfs_dentry() - helper function to get the debugfs_dentry
710*4882a593Smuzhiyun  * @indio_dev:		IIO device structure for device
711*4882a593Smuzhiyun  **/
712*4882a593Smuzhiyun #if defined(CONFIG_DEBUG_FS)
713*4882a593Smuzhiyun struct dentry *iio_get_debugfs_dentry(struct iio_dev *indio_dev);
714*4882a593Smuzhiyun #else
iio_get_debugfs_dentry(struct iio_dev * indio_dev)715*4882a593Smuzhiyun static inline struct dentry *iio_get_debugfs_dentry(struct iio_dev *indio_dev)
716*4882a593Smuzhiyun {
717*4882a593Smuzhiyun 	return NULL;
718*4882a593Smuzhiyun }
719*4882a593Smuzhiyun #endif
720*4882a593Smuzhiyun 
721*4882a593Smuzhiyun ssize_t iio_format_value(char *buf, unsigned int type, int size, int *vals);
722*4882a593Smuzhiyun 
723*4882a593Smuzhiyun int iio_str_to_fixpoint(const char *str, int fract_mult, int *integer,
724*4882a593Smuzhiyun 	int *fract);
725*4882a593Smuzhiyun 
726*4882a593Smuzhiyun /**
727*4882a593Smuzhiyun  * IIO_DEGREE_TO_RAD() - Convert degree to rad
728*4882a593Smuzhiyun  * @deg: A value in degree
729*4882a593Smuzhiyun  *
730*4882a593Smuzhiyun  * Returns the given value converted from degree to rad
731*4882a593Smuzhiyun  */
732*4882a593Smuzhiyun #define IIO_DEGREE_TO_RAD(deg) (((deg) * 314159ULL + 9000000ULL) / 18000000ULL)
733*4882a593Smuzhiyun 
734*4882a593Smuzhiyun /**
735*4882a593Smuzhiyun  * IIO_RAD_TO_DEGREE() - Convert rad to degree
736*4882a593Smuzhiyun  * @rad: A value in rad
737*4882a593Smuzhiyun  *
738*4882a593Smuzhiyun  * Returns the given value converted from rad to degree
739*4882a593Smuzhiyun  */
740*4882a593Smuzhiyun #define IIO_RAD_TO_DEGREE(rad) \
741*4882a593Smuzhiyun 	(((rad) * 18000000ULL + 314159ULL / 2) / 314159ULL)
742*4882a593Smuzhiyun 
743*4882a593Smuzhiyun /**
744*4882a593Smuzhiyun  * IIO_G_TO_M_S_2() - Convert g to meter / second**2
745*4882a593Smuzhiyun  * @g: A value in g
746*4882a593Smuzhiyun  *
747*4882a593Smuzhiyun  * Returns the given value converted from g to meter / second**2
748*4882a593Smuzhiyun  */
749*4882a593Smuzhiyun #define IIO_G_TO_M_S_2(g) ((g) * 980665ULL / 100000ULL)
750*4882a593Smuzhiyun 
751*4882a593Smuzhiyun /**
752*4882a593Smuzhiyun  * IIO_M_S_2_TO_G() - Convert meter / second**2 to g
753*4882a593Smuzhiyun  * @ms2: A value in meter / second**2
754*4882a593Smuzhiyun  *
755*4882a593Smuzhiyun  * Returns the given value converted from meter / second**2 to g
756*4882a593Smuzhiyun  */
757*4882a593Smuzhiyun #define IIO_M_S_2_TO_G(ms2) (((ms2) * 100000ULL + 980665ULL / 2) / 980665ULL)
758*4882a593Smuzhiyun 
759*4882a593Smuzhiyun #endif /* _INDUSTRIAL_IO_H_ */
760