1d2912cb1SThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-only */
206458e27SJonathan Cameron
306458e27SJonathan Cameron /* The industrial I/O core
406458e27SJonathan Cameron *
506458e27SJonathan Cameron * Copyright (c) 2008 Jonathan Cameron
606458e27SJonathan Cameron */
706458e27SJonathan Cameron #ifndef _INDUSTRIAL_IO_H_
806458e27SJonathan Cameron #define _INDUSTRIAL_IO_H_
906458e27SJonathan Cameron
10*63fc5352SDavid Lechner #include <linux/align.h>
1106458e27SJonathan Cameron #include <linux/device.h>
1206458e27SJonathan Cameron #include <linux/cdev.h>
13d795e38dSJonathan Cameron #include <linux/compiler_types.h>
14fa19c303SDavid Lechner #include <linux/minmax.h>
1512c4efe3SJonathan Cameron #include <linux/slab.h>
1606458e27SJonathan Cameron #include <linux/iio/types.h>
1706458e27SJonathan Cameron /* IIO TODO LIST */
1806458e27SJonathan Cameron /*
1906458e27SJonathan Cameron * Provide means of adjusting timer accuracy.
2006458e27SJonathan Cameron * Currently assumes nano seconds.
2106458e27SJonathan Cameron */
2206458e27SJonathan Cameron
231e64b9c5SNuno Sá struct fwnode_reference_args;
2410f09307SNuno Sá
253704432fSJonathan Cameron enum iio_shared_by {
263704432fSJonathan Cameron IIO_SEPARATE,
27c006ec83SJonathan Cameron IIO_SHARED_BY_TYPE,
28c006ec83SJonathan Cameron IIO_SHARED_BY_DIR,
29c006ec83SJonathan Cameron IIO_SHARED_BY_ALL
303704432fSJonathan Cameron };
313704432fSJonathan Cameron
3206458e27SJonathan Cameron enum iio_endian {
3306458e27SJonathan Cameron IIO_CPU,
3406458e27SJonathan Cameron IIO_BE,
3506458e27SJonathan Cameron IIO_LE,
3606458e27SJonathan Cameron };
3706458e27SJonathan Cameron
3806458e27SJonathan Cameron struct iio_chan_spec;
3906458e27SJonathan Cameron struct iio_dev;
4006458e27SJonathan Cameron
4106458e27SJonathan Cameron /**
4206458e27SJonathan Cameron * struct iio_chan_spec_ext_info - Extended channel info attribute
4306458e27SJonathan Cameron * @name: Info attribute name
4406458e27SJonathan Cameron * @shared: Whether this attribute is shared between all channels.
4506458e27SJonathan Cameron * @read: Read callback for this info attribute, may be NULL.
4606458e27SJonathan Cameron * @write: Write callback for this info attribute, may be NULL.
47fc6d1139SMichael Hennerich * @private: Data private to the driver.
4806458e27SJonathan Cameron */
4906458e27SJonathan Cameron struct iio_chan_spec_ext_info {
5006458e27SJonathan Cameron const char *name;
513704432fSJonathan Cameron enum iio_shared_by shared;
52fc6d1139SMichael Hennerich ssize_t (*read)(struct iio_dev *, uintptr_t private,
53fc6d1139SMichael Hennerich struct iio_chan_spec const *, char *buf);
54fc6d1139SMichael Hennerich ssize_t (*write)(struct iio_dev *, uintptr_t private,
55fc6d1139SMichael Hennerich struct iio_chan_spec const *, const char *buf,
56fc6d1139SMichael Hennerich size_t len);
57fc6d1139SMichael Hennerich uintptr_t private;
5806458e27SJonathan Cameron };
5906458e27SJonathan Cameron
6006458e27SJonathan Cameron /**
615212cc8aSLars-Peter Clausen * struct iio_enum - Enum channel info attribute
62f4c34939SPeter Meerwald * @items: An array of strings.
63f4c34939SPeter Meerwald * @num_items: Length of the item array.
64f4c34939SPeter Meerwald * @set: Set callback function, may be NULL.
65f4c34939SPeter Meerwald * @get: Get callback function, may be NULL.
665212cc8aSLars-Peter Clausen *
675212cc8aSLars-Peter Clausen * The iio_enum struct can be used to implement enum style channel attributes.
685212cc8aSLars-Peter Clausen * Enum style attributes are those which have a set of strings which map to
695212cc8aSLars-Peter Clausen * unsigned integer values. The IIO enum helper code takes care of mapping
705212cc8aSLars-Peter Clausen * between value and string as well as generating a "_available" file which
715212cc8aSLars-Peter Clausen * contains a list of all available items. The set callback will be called when
725212cc8aSLars-Peter Clausen * the attribute is updated. The last parameter is the index to the newly
735212cc8aSLars-Peter Clausen * activated item. The get callback will be used to query the currently active
745212cc8aSLars-Peter Clausen * item and is supposed to return the index for it.
755212cc8aSLars-Peter Clausen */
765212cc8aSLars-Peter Clausen struct iio_enum {
775212cc8aSLars-Peter Clausen const char * const *items;
785212cc8aSLars-Peter Clausen unsigned int num_items;
795212cc8aSLars-Peter Clausen int (*set)(struct iio_dev *, const struct iio_chan_spec *, unsigned int);
805212cc8aSLars-Peter Clausen int (*get)(struct iio_dev *, const struct iio_chan_spec *);
815212cc8aSLars-Peter Clausen };
825212cc8aSLars-Peter Clausen
835212cc8aSLars-Peter Clausen ssize_t iio_enum_available_read(struct iio_dev *indio_dev,
845212cc8aSLars-Peter Clausen uintptr_t priv, const struct iio_chan_spec *chan, char *buf);
855212cc8aSLars-Peter Clausen ssize_t iio_enum_read(struct iio_dev *indio_dev,
865212cc8aSLars-Peter Clausen uintptr_t priv, const struct iio_chan_spec *chan, char *buf);
875212cc8aSLars-Peter Clausen ssize_t iio_enum_write(struct iio_dev *indio_dev,
885212cc8aSLars-Peter Clausen uintptr_t priv, const struct iio_chan_spec *chan, const char *buf,
895212cc8aSLars-Peter Clausen size_t len);
905212cc8aSLars-Peter Clausen
915212cc8aSLars-Peter Clausen /**
925212cc8aSLars-Peter Clausen * IIO_ENUM() - Initialize enum extended channel attribute
935212cc8aSLars-Peter Clausen * @_name: Attribute name
945212cc8aSLars-Peter Clausen * @_shared: Whether the attribute is shared between all channels
95d25b3808SPeter Meerwald * @_e: Pointer to an iio_enum struct
965212cc8aSLars-Peter Clausen *
975212cc8aSLars-Peter Clausen * This should usually be used together with IIO_ENUM_AVAILABLE()
985212cc8aSLars-Peter Clausen */
995212cc8aSLars-Peter Clausen #define IIO_ENUM(_name, _shared, _e) \
1005212cc8aSLars-Peter Clausen { \
1015212cc8aSLars-Peter Clausen .name = (_name), \
1025212cc8aSLars-Peter Clausen .shared = (_shared), \
1035212cc8aSLars-Peter Clausen .read = iio_enum_read, \
1045212cc8aSLars-Peter Clausen .write = iio_enum_write, \
1055212cc8aSLars-Peter Clausen .private = (uintptr_t)(_e), \
1065212cc8aSLars-Peter Clausen }
1075212cc8aSLars-Peter Clausen
1085212cc8aSLars-Peter Clausen /**
1095212cc8aSLars-Peter Clausen * IIO_ENUM_AVAILABLE() - Initialize enum available extended channel attribute
1105212cc8aSLars-Peter Clausen * @_name: Attribute name ("_available" will be appended to the name)
111ffc7c517SAntoniu Miclaus * @_shared: Whether the attribute is shared between all channels
112d25b3808SPeter Meerwald * @_e: Pointer to an iio_enum struct
1135212cc8aSLars-Peter Clausen *
114d25b3808SPeter Meerwald * Creates a read only attribute which lists all the available enum items in a
1155212cc8aSLars-Peter Clausen * space separated list. This should usually be used together with IIO_ENUM()
1165212cc8aSLars-Peter Clausen */
117ffc7c517SAntoniu Miclaus #define IIO_ENUM_AVAILABLE(_name, _shared, _e) \
1185212cc8aSLars-Peter Clausen { \
1195212cc8aSLars-Peter Clausen .name = (_name "_available"), \
120ffc7c517SAntoniu Miclaus .shared = _shared, \
1215212cc8aSLars-Peter Clausen .read = iio_enum_available_read, \
1225212cc8aSLars-Peter Clausen .private = (uintptr_t)(_e), \
1235212cc8aSLars-Peter Clausen }
1245212cc8aSLars-Peter Clausen
1255212cc8aSLars-Peter Clausen /**
126dfc57732SGregor Boirie * struct iio_mount_matrix - iio mounting matrix
127dfc57732SGregor Boirie * @rotation: 3 dimensional space rotation matrix defining sensor alignment with
128dfc57732SGregor Boirie * main hardware
129dfc57732SGregor Boirie */
130dfc57732SGregor Boirie struct iio_mount_matrix {
131dfc57732SGregor Boirie const char *rotation[9];
132dfc57732SGregor Boirie };
133dfc57732SGregor Boirie
134dfc57732SGregor Boirie ssize_t iio_show_mount_matrix(struct iio_dev *indio_dev, uintptr_t priv,
135dfc57732SGregor Boirie const struct iio_chan_spec *chan, char *buf);
136b892770aSAndy Shevchenko int iio_read_mount_matrix(struct device *dev, struct iio_mount_matrix *matrix);
137dfc57732SGregor Boirie
138dfc57732SGregor Boirie typedef const struct iio_mount_matrix *
139dfc57732SGregor Boirie (iio_get_mount_matrix_t)(const struct iio_dev *indio_dev,
140dfc57732SGregor Boirie const struct iio_chan_spec *chan);
141dfc57732SGregor Boirie
142dfc57732SGregor Boirie /**
143dfc57732SGregor Boirie * IIO_MOUNT_MATRIX() - Initialize mount matrix extended channel attribute
144dfc57732SGregor Boirie * @_shared: Whether the attribute is shared between all channels
145dfc57732SGregor Boirie * @_get: Pointer to an iio_get_mount_matrix_t accessor
146dfc57732SGregor Boirie */
147dfc57732SGregor Boirie #define IIO_MOUNT_MATRIX(_shared, _get) \
148dfc57732SGregor Boirie { \
149dfc57732SGregor Boirie .name = "mount_matrix", \
150dfc57732SGregor Boirie .shared = (_shared), \
151dfc57732SGregor Boirie .read = iio_show_mount_matrix, \
152dfc57732SGregor Boirie .private = (uintptr_t)(_get), \
153dfc57732SGregor Boirie }
154dfc57732SGregor Boirie
155dfc57732SGregor Boirie /**
156b4e3ac0aSLars-Peter Clausen * struct iio_event_spec - specification for a channel event
157b4e3ac0aSLars-Peter Clausen * @type: Type of the event
158b4e3ac0aSLars-Peter Clausen * @dir: Direction of the event
159b4e3ac0aSLars-Peter Clausen * @mask_separate: Bit mask of enum iio_event_info values. Attributes
160b4e3ac0aSLars-Peter Clausen * set in this mask will be registered per channel.
161b4e3ac0aSLars-Peter Clausen * @mask_shared_by_type: Bit mask of enum iio_event_info values. Attributes
162b4e3ac0aSLars-Peter Clausen * set in this mask will be shared by channel type.
163b4e3ac0aSLars-Peter Clausen * @mask_shared_by_dir: Bit mask of enum iio_event_info values. Attributes
164b4e3ac0aSLars-Peter Clausen * set in this mask will be shared by channel type and
165b4e3ac0aSLars-Peter Clausen * direction.
166b4e3ac0aSLars-Peter Clausen * @mask_shared_by_all: Bit mask of enum iio_event_info values. Attributes
167b4e3ac0aSLars-Peter Clausen * set in this mask will be shared by all channels.
168b4e3ac0aSLars-Peter Clausen */
169b4e3ac0aSLars-Peter Clausen struct iio_event_spec {
170b4e3ac0aSLars-Peter Clausen enum iio_event_type type;
171b4e3ac0aSLars-Peter Clausen enum iio_event_direction dir;
172b4e3ac0aSLars-Peter Clausen unsigned long mask_separate;
173b4e3ac0aSLars-Peter Clausen unsigned long mask_shared_by_type;
174b4e3ac0aSLars-Peter Clausen unsigned long mask_shared_by_dir;
175b4e3ac0aSLars-Peter Clausen unsigned long mask_shared_by_all;
176b4e3ac0aSLars-Peter Clausen };
177b4e3ac0aSLars-Peter Clausen
178b4e3ac0aSLars-Peter Clausen /**
179fd7179ecSDavid Lechner * struct iio_scan_type - specification for channel data format in buffer
180fd7179ecSDavid Lechner * @sign: 's' or 'u' to specify signed or unsigned
181fd7179ecSDavid Lechner * @realbits: Number of valid bits of data
182fd7179ecSDavid Lechner * @storagebits: Realbits + padding
183fd7179ecSDavid Lechner * @shift: Shift right by this before masking out realbits.
184fd7179ecSDavid Lechner * @repeat: Number of times real/storage bits repeats. When the
185fd7179ecSDavid Lechner * repeat element is more than 1, then the type element in
186fd7179ecSDavid Lechner * sysfs will show a repeat value. Otherwise, the number
187fd7179ecSDavid Lechner * of repetitions is omitted.
188fd7179ecSDavid Lechner * @endianness: little or big endian
189fd7179ecSDavid Lechner */
190fd7179ecSDavid Lechner struct iio_scan_type {
191fd7179ecSDavid Lechner char sign;
192fd7179ecSDavid Lechner u8 realbits;
193fd7179ecSDavid Lechner u8 storagebits;
194fd7179ecSDavid Lechner u8 shift;
195fd7179ecSDavid Lechner u8 repeat;
196fd7179ecSDavid Lechner enum iio_endian endianness;
197fd7179ecSDavid Lechner };
198fd7179ecSDavid Lechner
199fd7179ecSDavid Lechner /**
20006458e27SJonathan Cameron * struct iio_chan_spec - specification of a single channel
20106458e27SJonathan Cameron * @type: What type of measurement is the channel making.
202a16561c6SPeter Meerwald * @channel: What number do we wish to assign the channel.
20306458e27SJonathan Cameron * @channel2: If there is a second number for a differential
20406458e27SJonathan Cameron * channel then this is it. If modified is set then the
20506458e27SJonathan Cameron * value here specifies the modifier.
20606458e27SJonathan Cameron * @address: Driver specific identifier.
20706458e27SJonathan Cameron * @scan_index: Monotonic index to give ordering in scans when read
20806458e27SJonathan Cameron * from a buffer.
209d8f2bb50SDavid Lechner * @scan_type: struct describing the scan type - mutually exclusive
210d8f2bb50SDavid Lechner * with ext_scan_type.
211d8f2bb50SDavid Lechner * @ext_scan_type: Used in rare cases where there is more than one scan
212d8f2bb50SDavid Lechner * format for a channel. When this is used, the flag
213d8f2bb50SDavid Lechner * has_ext_scan_type must be set and the driver must
214d8f2bb50SDavid Lechner * implement get_current_scan_type in struct iio_info.
215d8f2bb50SDavid Lechner * @num_ext_scan_type: Number of elements in ext_scan_type.
2168655cc49SJonathan Cameron * @info_mask_separate: What information is to be exported that is specific to
2178655cc49SJonathan Cameron * this channel.
21851239600SJonathan Cameron * @info_mask_separate_available: What availability information is to be
21951239600SJonathan Cameron * exported that is specific to this channel.
2208655cc49SJonathan Cameron * @info_mask_shared_by_type: What information is to be exported that is shared
2218655cc49SJonathan Cameron * by all channels of the same type.
22251239600SJonathan Cameron * @info_mask_shared_by_type_available: What availability information is to be
22351239600SJonathan Cameron * exported that is shared by all channels of the same
22451239600SJonathan Cameron * type.
225c006ec83SJonathan Cameron * @info_mask_shared_by_dir: What information is to be exported that is shared
226c006ec83SJonathan Cameron * by all channels of the same direction.
22751239600SJonathan Cameron * @info_mask_shared_by_dir_available: What availability information is to be
22851239600SJonathan Cameron * exported that is shared by all channels of the same
22951239600SJonathan Cameron * direction.
230c006ec83SJonathan Cameron * @info_mask_shared_by_all: What information is to be exported that is shared
231c006ec83SJonathan Cameron * by all channels.
23251239600SJonathan Cameron * @info_mask_shared_by_all_available: What availability information is to be
23351239600SJonathan Cameron * exported that is shared by all channels.
234b4e3ac0aSLars-Peter Clausen * @event_spec: Array of events which should be registered for this
235b4e3ac0aSLars-Peter Clausen * channel.
236b4e3ac0aSLars-Peter Clausen * @num_event_specs: Size of the event_spec array.
23706458e27SJonathan Cameron * @ext_info: Array of extended info attributes for this channel.
23806458e27SJonathan Cameron * The array is NULL terminated, the last element should
2399cc113bcSPeter Meerwald * have its name field set to NULL.
24006458e27SJonathan Cameron * @extend_name: Allows labeling of channel attributes with an
24106458e27SJonathan Cameron * informative name. Note this has no effect codes etc,
24206458e27SJonathan Cameron * unlike modifiers.
243123627adSMarijn Suijten * This field is deprecated in favour of providing
244123627adSMarijn Suijten * iio_info->read_label() to override the label, which
245123627adSMarijn Suijten * unlike @extend_name does not affect sysfs filenames.
24617879488SPeter Meerwald * @datasheet_name: A name used in in-kernel mapping of channels. It should
24706458e27SJonathan Cameron * correspond to the first name that the channel is referred
24806458e27SJonathan Cameron * to by in the datasheet (e.g. IND), or the nearest
24906458e27SJonathan Cameron * possible compound name (e.g. IND-INC).
25006458e27SJonathan Cameron * @modified: Does a modifier apply to this channel. What these are
25106458e27SJonathan Cameron * depends on the channel type. Modifier is set in
25206458e27SJonathan Cameron * channel2. Examples are IIO_MOD_X for axial sensors about
25306458e27SJonathan Cameron * the 'x' axis.
25406458e27SJonathan Cameron * @indexed: Specify the channel has a numerical index. If not,
255a16561c6SPeter Meerwald * the channel index number will be suppressed for sysfs
256a16561c6SPeter Meerwald * attributes but not for event codes.
257c3668a0fSPeter Meerwald * @output: Channel is output.
25806458e27SJonathan Cameron * @differential: Channel is differential.
259d8f2bb50SDavid Lechner * @has_ext_scan_type: True if ext_scan_type is used instead of scan_type.
26006458e27SJonathan Cameron */
26106458e27SJonathan Cameron struct iio_chan_spec {
26206458e27SJonathan Cameron enum iio_chan_type type;
26306458e27SJonathan Cameron int channel;
26406458e27SJonathan Cameron int channel2;
26506458e27SJonathan Cameron unsigned long address;
26606458e27SJonathan Cameron int scan_index;
267d8f2bb50SDavid Lechner union {
268fd7179ecSDavid Lechner struct iio_scan_type scan_type;
269d8f2bb50SDavid Lechner struct {
270d8f2bb50SDavid Lechner const struct iio_scan_type *ext_scan_type;
271d8f2bb50SDavid Lechner unsigned int num_ext_scan_type;
272d8f2bb50SDavid Lechner };
273d8f2bb50SDavid Lechner };
2748655cc49SJonathan Cameron long info_mask_separate;
27551239600SJonathan Cameron long info_mask_separate_available;
2768655cc49SJonathan Cameron long info_mask_shared_by_type;
27751239600SJonathan Cameron long info_mask_shared_by_type_available;
278c006ec83SJonathan Cameron long info_mask_shared_by_dir;
27951239600SJonathan Cameron long info_mask_shared_by_dir_available;
280c006ec83SJonathan Cameron long info_mask_shared_by_all;
28151239600SJonathan Cameron long info_mask_shared_by_all_available;
282b4e3ac0aSLars-Peter Clausen const struct iio_event_spec *event_spec;
283b4e3ac0aSLars-Peter Clausen unsigned int num_event_specs;
28406458e27SJonathan Cameron const struct iio_chan_spec_ext_info *ext_info;
28506458e27SJonathan Cameron const char *extend_name;
28606458e27SJonathan Cameron const char *datasheet_name;
2878fa714caSAndy Shevchenko unsigned int modified:1;
2888fa714caSAndy Shevchenko unsigned int indexed:1;
2898fa714caSAndy Shevchenko unsigned int output:1;
2908fa714caSAndy Shevchenko unsigned int differential:1;
2918fa714caSAndy Shevchenko unsigned int has_ext_scan_type:1;
29206458e27SJonathan Cameron };
29306458e27SJonathan Cameron
29448e44ce0SLars-Peter Clausen
29548e44ce0SLars-Peter Clausen /**
29648e44ce0SLars-Peter Clausen * iio_channel_has_info() - Checks whether a channel supports a info attribute
29748e44ce0SLars-Peter Clausen * @chan: The channel to be queried
29848e44ce0SLars-Peter Clausen * @type: Type of the info attribute to be checked
29948e44ce0SLars-Peter Clausen *
30048e44ce0SLars-Peter Clausen * Returns true if the channels supports reporting values for the given info
30148e44ce0SLars-Peter Clausen * attribute type, false otherwise.
30248e44ce0SLars-Peter Clausen */
iio_channel_has_info(const struct iio_chan_spec * chan,enum iio_chan_info_enum type)30348e44ce0SLars-Peter Clausen static inline bool iio_channel_has_info(const struct iio_chan_spec *chan,
30448e44ce0SLars-Peter Clausen enum iio_chan_info_enum type)
30548e44ce0SLars-Peter Clausen {
3061c297a66SAlexandre Belloni return (chan->info_mask_separate & BIT(type)) |
307c006ec83SJonathan Cameron (chan->info_mask_shared_by_type & BIT(type)) |
308c006ec83SJonathan Cameron (chan->info_mask_shared_by_dir & BIT(type)) |
309c006ec83SJonathan Cameron (chan->info_mask_shared_by_all & BIT(type));
31048e44ce0SLars-Peter Clausen }
31148e44ce0SLars-Peter Clausen
31200c5f80cSPeter Rosin /**
31300c5f80cSPeter Rosin * iio_channel_has_available() - Checks if a channel has an available attribute
31400c5f80cSPeter Rosin * @chan: The channel to be queried
31500c5f80cSPeter Rosin * @type: Type of the available attribute to be checked
31600c5f80cSPeter Rosin *
31700c5f80cSPeter Rosin * Returns true if the channel supports reporting available values for the
31800c5f80cSPeter Rosin * given attribute type, false otherwise.
31900c5f80cSPeter Rosin */
iio_channel_has_available(const struct iio_chan_spec * chan,enum iio_chan_info_enum type)32000c5f80cSPeter Rosin static inline bool iio_channel_has_available(const struct iio_chan_spec *chan,
32100c5f80cSPeter Rosin enum iio_chan_info_enum type)
32200c5f80cSPeter Rosin {
32300c5f80cSPeter Rosin return (chan->info_mask_separate_available & BIT(type)) |
32400c5f80cSPeter Rosin (chan->info_mask_shared_by_type_available & BIT(type)) |
32500c5f80cSPeter Rosin (chan->info_mask_shared_by_dir_available & BIT(type)) |
32600c5f80cSPeter Rosin (chan->info_mask_shared_by_all_available & BIT(type));
32700c5f80cSPeter Rosin }
32800c5f80cSPeter Rosin
32907d4655bSJonathan Cameron #define IIO_CHAN_SOFT_TIMESTAMP(_si) { \
33007d4655bSJonathan Cameron .type = IIO_TIMESTAMP, \
33107d4655bSJonathan Cameron .channel = -1, \
33207d4655bSJonathan Cameron .scan_index = _si, \
33307d4655bSJonathan Cameron .scan_type = { \
33407d4655bSJonathan Cameron .sign = 's', \
33507d4655bSJonathan Cameron .realbits = 64, \
33607d4655bSJonathan Cameron .storagebits = 64, \
33707d4655bSJonathan Cameron }, \
33807d4655bSJonathan Cameron }
33906458e27SJonathan Cameron
340bc2b7dabSGregor Boirie s64 iio_get_time_ns(const struct iio_dev *indio_dev);
34106458e27SJonathan Cameron
342ac3e62f5SMiquel Raynal /*
343ac3e62f5SMiquel Raynal * Device operating modes
344ac3e62f5SMiquel Raynal * @INDIO_DIRECT_MODE: There is an access to either:
345ac3e62f5SMiquel Raynal * a) The last single value available for devices that do not provide
346ac3e62f5SMiquel Raynal * on-demand reads.
347ac3e62f5SMiquel Raynal * b) A new value after performing an on-demand read otherwise.
348ac3e62f5SMiquel Raynal * On most devices, this is a single-shot read. On some devices with data
349ac3e62f5SMiquel Raynal * streams without an 'on-demand' function, this might also be the 'last value'
350ac3e62f5SMiquel Raynal * feature. Above all, this mode internally means that we are not in any of the
351ac3e62f5SMiquel Raynal * other modes, and sysfs reads should work.
352ac3e62f5SMiquel Raynal * Device drivers should inform the core if they support this mode.
353ac3e62f5SMiquel Raynal * @INDIO_BUFFER_TRIGGERED: Common mode when dealing with kfifo buffers.
354ac3e62f5SMiquel Raynal * It indicates that an explicit trigger is required. This requests the core to
355ac3e62f5SMiquel Raynal * attach a poll function when enabling the buffer, which is indicated by the
356ac3e62f5SMiquel Raynal * _TRIGGERED suffix.
357ac3e62f5SMiquel Raynal * The core will ensure this mode is set when registering a triggered buffer
358ac3e62f5SMiquel Raynal * with iio_triggered_buffer_setup().
359ac3e62f5SMiquel Raynal * @INDIO_BUFFER_SOFTWARE: Another kfifo buffer mode, but not event triggered.
360ac3e62f5SMiquel Raynal * No poll function can be attached because there is no triggered infrastructure
361ac3e62f5SMiquel Raynal * we can use to cause capture. There is a kfifo that the driver will fill, but
362ac3e62f5SMiquel Raynal * not "only one scan at a time". Typically, hardware will have a buffer that
363ac3e62f5SMiquel Raynal * can hold multiple scans. Software may read one or more scans at a single time
364ac3e62f5SMiquel Raynal * and push the available data to a Kfifo. This means the core will not attach
365ac3e62f5SMiquel Raynal * any poll function when enabling the buffer.
366ac3e62f5SMiquel Raynal * The core will ensure this mode is set when registering a simple kfifo buffer
367ac3e62f5SMiquel Raynal * with devm_iio_kfifo_buffer_setup().
368ac3e62f5SMiquel Raynal * @INDIO_BUFFER_HARDWARE: For specific hardware, if unsure do not use this mode.
369ac3e62f5SMiquel Raynal * Same as above but this time the buffer is not a kfifo where we have direct
370ac3e62f5SMiquel Raynal * access to the data. Instead, the consumer driver must access the data through
371ac3e62f5SMiquel Raynal * non software visible channels (or DMA when there is no demux possible in
372ac3e62f5SMiquel Raynal * software)
373ac3e62f5SMiquel Raynal * The core will ensure this mode is set when registering a dmaengine buffer
374ac3e62f5SMiquel Raynal * with devm_iio_dmaengine_buffer_setup().
375ac3e62f5SMiquel Raynal * @INDIO_EVENT_TRIGGERED: Very unusual mode.
376ac3e62f5SMiquel Raynal * Triggers usually refer to an external event which will start data capture.
377ac3e62f5SMiquel Raynal * Here it is kind of the opposite as, a particular state of the data might
378ac3e62f5SMiquel Raynal * produce an event which can be considered as an event. We don't necessarily
379ac3e62f5SMiquel Raynal * have access to the data itself, but to the event produced. For example, this
380ac3e62f5SMiquel Raynal * can be a threshold detector. The internal path of this mode is very close to
381ac3e62f5SMiquel Raynal * the INDIO_BUFFER_TRIGGERED mode.
382ac3e62f5SMiquel Raynal * The core will ensure this mode is set when registering a triggered event.
383ac3e62f5SMiquel Raynal * @INDIO_HARDWARE_TRIGGERED: Very unusual mode.
384ac3e62f5SMiquel Raynal * Here, triggers can result in data capture and can be routed to multiple
385ac3e62f5SMiquel Raynal * hardware components, which make them close to regular triggers in the way
386ac3e62f5SMiquel Raynal * they must be managed by the core, but without the entire interrupts/poll
387ac3e62f5SMiquel Raynal * functions burden. Interrupts are irrelevant as the data flow is hardware
388ac3e62f5SMiquel Raynal * mediated and distributed.
389ac3e62f5SMiquel Raynal */
39006458e27SJonathan Cameron #define INDIO_DIRECT_MODE 0x01
39106458e27SJonathan Cameron #define INDIO_BUFFER_TRIGGERED 0x02
39203af03adSKarol Wrona #define INDIO_BUFFER_SOFTWARE 0x04
39306458e27SJonathan Cameron #define INDIO_BUFFER_HARDWARE 0x08
394735ad074SVladimir Barinov #define INDIO_EVENT_TRIGGERED 0x10
395d89e119aSBenjamin Gaignard #define INDIO_HARDWARE_TRIGGERED 0x20
39606458e27SJonathan Cameron
39706458e27SJonathan Cameron #define INDIO_ALL_BUFFER_MODES \
39803af03adSKarol Wrona (INDIO_BUFFER_TRIGGERED | INDIO_BUFFER_HARDWARE | INDIO_BUFFER_SOFTWARE)
39906458e27SJonathan Cameron
400d89e119aSBenjamin Gaignard #define INDIO_ALL_TRIGGERED_MODES \
401d89e119aSBenjamin Gaignard (INDIO_BUFFER_TRIGGERED \
402d89e119aSBenjamin Gaignard | INDIO_EVENT_TRIGGERED \
403d89e119aSBenjamin Gaignard | INDIO_HARDWARE_TRIGGERED)
404d89e119aSBenjamin Gaignard
4059fbfb4b3SSrinivas Pandruvada #define INDIO_MAX_RAW_ELEMENTS 4
4069fbfb4b3SSrinivas Pandruvada
40708f01cc1SGerald Loacker struct iio_val_int_plus_micro {
40808f01cc1SGerald Loacker int integer;
40908f01cc1SGerald Loacker int micro;
41008f01cc1SGerald Loacker };
41108f01cc1SGerald Loacker
41206458e27SJonathan Cameron struct iio_trigger; /* forward declaration */
41306458e27SJonathan Cameron
41406458e27SJonathan Cameron /**
41506458e27SJonathan Cameron * struct iio_info - constant information about device
41606458e27SJonathan Cameron * @event_attrs: event control attributes
41706458e27SJonathan Cameron * @attrs: general purpose device attributes
41806458e27SJonathan Cameron * @read_raw: function to request a value from the device.
41906458e27SJonathan Cameron * mask specifies which value. Note 0 means a reading of
42006458e27SJonathan Cameron * the channel in question. Return value will specify the
42106458e27SJonathan Cameron * type of value returned by the device. val and val2 will
42206458e27SJonathan Cameron * contain the elements making up the returned value.
4239fbfb4b3SSrinivas Pandruvada * @read_raw_multi: function to return values from the device.
4249fbfb4b3SSrinivas Pandruvada * mask specifies which value. Note 0 means a reading of
4259fbfb4b3SSrinivas Pandruvada * the channel in question. Return value will specify the
4269fbfb4b3SSrinivas Pandruvada * type of value returned by the device. vals pointer
4279fbfb4b3SSrinivas Pandruvada * contain the elements making up the returned value.
4289fbfb4b3SSrinivas Pandruvada * max_len specifies maximum number of elements
4299fbfb4b3SSrinivas Pandruvada * vals pointer can contain. val_len is used to return
4309fbfb4b3SSrinivas Pandruvada * length of valid elements in vals.
43151239600SJonathan Cameron * @read_avail: function to return the available values from the device.
43251239600SJonathan Cameron * mask specifies which value. Note 0 means the available
43351239600SJonathan Cameron * values for the channel in question. Return value
43451239600SJonathan Cameron * specifies if a IIO_AVAIL_LIST or a IIO_AVAIL_RANGE is
43551239600SJonathan Cameron * returned in vals. The type of the vals are returned in
43651239600SJonathan Cameron * type and the number of vals is returned in length. For
43751239600SJonathan Cameron * ranges, there are always three vals returned; min, step
43851239600SJonathan Cameron * and max. For lists, all possible values are enumerated.
43906458e27SJonathan Cameron * @write_raw: function to write a value to the device.
44006458e27SJonathan Cameron * Parameters are the same as for read_raw.
4411d4ef9b3SCristian Pop * @read_label: function to request label name for a specified label,
4421d4ef9b3SCristian Pop * for better channel identification.
44306458e27SJonathan Cameron * @write_raw_get_fmt: callback function to query the expected
44406458e27SJonathan Cameron * format/precision. If not set by the driver, write_raw
44506458e27SJonathan Cameron * returns IIO_VAL_INT_PLUS_MICRO.
44606458e27SJonathan Cameron * @read_event_config: find out if the event is enabled.
44706458e27SJonathan Cameron * @write_event_config: set if the event is enabled.
448cb955852SLars-Peter Clausen * @read_event_value: read a configuration value associated with the event.
449cb955852SLars-Peter Clausen * @write_event_value: write a configuration value for the event.
45059872793SDavid Lechner * @read_event_label: function to request label name for a specified label,
45159872793SDavid Lechner * for better event identification.
45206458e27SJonathan Cameron * @validate_trigger: function to validate the trigger when the
45306458e27SJonathan Cameron * current trigger gets changed.
454d8f2bb50SDavid Lechner * @get_current_scan_type: must be implemented by drivers that use ext_scan_type
455d8f2bb50SDavid Lechner * in the channel spec to return the index of the currently
456d8f2bb50SDavid Lechner * active ext_scan type for a channel.
457c3668a0fSPeter Meerwald * @update_scan_mode: function to configure device and scan buffer when
458c3668a0fSPeter Meerwald * channels have changed
459c3668a0fSPeter Meerwald * @debugfs_reg_access: function to read or write register value of device
4601e64b9c5SNuno Sá * @fwnode_xlate: fwnode based function pointer to obtain channel specifier index.
461f4f4673bSOctavian Purdila * @hwfifo_set_watermark: function pointer to set the current hardware
462f4f4673bSOctavian Purdila * fifo watermark level; see hwfifo_* entries in
463f4f4673bSOctavian Purdila * Documentation/ABI/testing/sysfs-bus-iio for details on
464f4f4673bSOctavian Purdila * how the hardware fifo operates
465f4f4673bSOctavian Purdila * @hwfifo_flush_to_buffer: function pointer to flush the samples stored
466f4f4673bSOctavian Purdila * in the hardware fifo to the device buffer. The driver
467f4f4673bSOctavian Purdila * should not flush more than count samples. The function
468f4f4673bSOctavian Purdila * must return the number of samples flushed, 0 if no
469f4f4673bSOctavian Purdila * samples were flushed or a negative integer if no samples
470f4f4673bSOctavian Purdila * were flushed and there was an error.
47106458e27SJonathan Cameron **/
47206458e27SJonathan Cameron struct iio_info {
473a9a0d64aSBhumika Goyal const struct attribute_group *event_attrs;
47406458e27SJonathan Cameron const struct attribute_group *attrs;
47506458e27SJonathan Cameron
47606458e27SJonathan Cameron int (*read_raw)(struct iio_dev *indio_dev,
47706458e27SJonathan Cameron struct iio_chan_spec const *chan,
47806458e27SJonathan Cameron int *val,
47906458e27SJonathan Cameron int *val2,
48006458e27SJonathan Cameron long mask);
48106458e27SJonathan Cameron
4829fbfb4b3SSrinivas Pandruvada int (*read_raw_multi)(struct iio_dev *indio_dev,
4839fbfb4b3SSrinivas Pandruvada struct iio_chan_spec const *chan,
4849fbfb4b3SSrinivas Pandruvada int max_len,
4859fbfb4b3SSrinivas Pandruvada int *vals,
4869fbfb4b3SSrinivas Pandruvada int *val_len,
4879fbfb4b3SSrinivas Pandruvada long mask);
4889fbfb4b3SSrinivas Pandruvada
48951239600SJonathan Cameron int (*read_avail)(struct iio_dev *indio_dev,
49051239600SJonathan Cameron struct iio_chan_spec const *chan,
49151239600SJonathan Cameron const int **vals,
49251239600SJonathan Cameron int *type,
49351239600SJonathan Cameron int *length,
49451239600SJonathan Cameron long mask);
49551239600SJonathan Cameron
49606458e27SJonathan Cameron int (*write_raw)(struct iio_dev *indio_dev,
49706458e27SJonathan Cameron struct iio_chan_spec const *chan,
49806458e27SJonathan Cameron int val,
49906458e27SJonathan Cameron int val2,
50006458e27SJonathan Cameron long mask);
50106458e27SJonathan Cameron
5021d4ef9b3SCristian Pop int (*read_label)(struct iio_dev *indio_dev,
5031d4ef9b3SCristian Pop struct iio_chan_spec const *chan,
5041d4ef9b3SCristian Pop char *label);
5051d4ef9b3SCristian Pop
50606458e27SJonathan Cameron int (*write_raw_get_fmt)(struct iio_dev *indio_dev,
50706458e27SJonathan Cameron struct iio_chan_spec const *chan,
50806458e27SJonathan Cameron long mask);
50906458e27SJonathan Cameron
51006458e27SJonathan Cameron int (*read_event_config)(struct iio_dev *indio_dev,
511b4e3ac0aSLars-Peter Clausen const struct iio_chan_spec *chan,
512b4e3ac0aSLars-Peter Clausen enum iio_event_type type,
513b4e3ac0aSLars-Peter Clausen enum iio_event_direction dir);
514b4e3ac0aSLars-Peter Clausen
515cb955852SLars-Peter Clausen int (*write_event_config)(struct iio_dev *indio_dev,
516b4e3ac0aSLars-Peter Clausen const struct iio_chan_spec *chan,
517b4e3ac0aSLars-Peter Clausen enum iio_event_type type,
518b4e3ac0aSLars-Peter Clausen enum iio_event_direction dir,
519b4b42f28SJulien Stephan bool state);
520b4e3ac0aSLars-Peter Clausen
521cb955852SLars-Peter Clausen int (*read_event_value)(struct iio_dev *indio_dev,
522b4e3ac0aSLars-Peter Clausen const struct iio_chan_spec *chan,
523b4e3ac0aSLars-Peter Clausen enum iio_event_type type,
524b4e3ac0aSLars-Peter Clausen enum iio_event_direction dir,
525b4e3ac0aSLars-Peter Clausen enum iio_event_info info, int *val, int *val2);
526b4e3ac0aSLars-Peter Clausen
527cb955852SLars-Peter Clausen int (*write_event_value)(struct iio_dev *indio_dev,
528b4e3ac0aSLars-Peter Clausen const struct iio_chan_spec *chan,
529b4e3ac0aSLars-Peter Clausen enum iio_event_type type,
530b4e3ac0aSLars-Peter Clausen enum iio_event_direction dir,
531b4e3ac0aSLars-Peter Clausen enum iio_event_info info, int val, int val2);
532b4e3ac0aSLars-Peter Clausen
53359872793SDavid Lechner int (*read_event_label)(struct iio_dev *indio_dev,
53459872793SDavid Lechner struct iio_chan_spec const *chan,
53559872793SDavid Lechner enum iio_event_type type,
53659872793SDavid Lechner enum iio_event_direction dir,
53759872793SDavid Lechner char *label);
53859872793SDavid Lechner
53906458e27SJonathan Cameron int (*validate_trigger)(struct iio_dev *indio_dev,
54006458e27SJonathan Cameron struct iio_trigger *trig);
541d8f2bb50SDavid Lechner int (*get_current_scan_type)(const struct iio_dev *indio_dev,
542d8f2bb50SDavid Lechner const struct iio_chan_spec *chan);
54306458e27SJonathan Cameron int (*update_scan_mode)(struct iio_dev *indio_dev,
54406458e27SJonathan Cameron const unsigned long *scan_mask);
54506458e27SJonathan Cameron int (*debugfs_reg_access)(struct iio_dev *indio_dev,
5468fa714caSAndy Shevchenko unsigned int reg, unsigned int writeval,
5478fa714caSAndy Shevchenko unsigned int *readval);
5481e64b9c5SNuno Sá int (*fwnode_xlate)(struct iio_dev *indio_dev,
5491e64b9c5SNuno Sá const struct fwnode_reference_args *iiospec);
5508fa714caSAndy Shevchenko int (*hwfifo_set_watermark)(struct iio_dev *indio_dev, unsigned int val);
551f4f4673bSOctavian Purdila int (*hwfifo_flush_to_buffer)(struct iio_dev *indio_dev,
5528fa714caSAndy Shevchenko unsigned int count);
55306458e27SJonathan Cameron };
55406458e27SJonathan Cameron
55506458e27SJonathan Cameron /**
55606458e27SJonathan Cameron * struct iio_buffer_setup_ops - buffer setup related callbacks
55706458e27SJonathan Cameron * @preenable: [DRIVER] function to run prior to marking buffer enabled
55806458e27SJonathan Cameron * @postenable: [DRIVER] function to run after marking buffer enabled
55906458e27SJonathan Cameron * @predisable: [DRIVER] function to run prior to marking buffer
56006458e27SJonathan Cameron * disabled
56106458e27SJonathan Cameron * @postdisable: [DRIVER] function to run after marking buffer disabled
562939546d1SLars-Peter Clausen * @validate_scan_mask: [DRIVER] function callback to check whether a given
563939546d1SLars-Peter Clausen * scan mask is valid for the device.
56406458e27SJonathan Cameron */
56506458e27SJonathan Cameron struct iio_buffer_setup_ops {
56606458e27SJonathan Cameron int (*preenable)(struct iio_dev *);
56706458e27SJonathan Cameron int (*postenable)(struct iio_dev *);
56806458e27SJonathan Cameron int (*predisable)(struct iio_dev *);
56906458e27SJonathan Cameron int (*postdisable)(struct iio_dev *);
570939546d1SLars-Peter Clausen bool (*validate_scan_mask)(struct iio_dev *indio_dev,
571939546d1SLars-Peter Clausen const unsigned long *scan_mask);
57206458e27SJonathan Cameron };
57306458e27SJonathan Cameron
57406458e27SJonathan Cameron /**
57506458e27SJonathan Cameron * struct iio_dev - industrial I/O device
576a8b6d670SMiquel Raynal * @modes: [DRIVER] bitmask listing all the operating modes
577a8b6d670SMiquel Raynal * supported by the IIO device. This list should be
578a8b6d670SMiquel Raynal * initialized before registering the IIO device. It can
579a8b6d670SMiquel Raynal * also be filed up by the IIO core, as a result of
580a8b6d670SMiquel Raynal * enabling particular features in the driver
581a8b6d670SMiquel Raynal * (see iio_triggered_event_setup()).
58206458e27SJonathan Cameron * @dev: [DRIVER] device structure, should be assigned a parent
58306458e27SJonathan Cameron * and owner
58406458e27SJonathan Cameron * @buffer: [DRIVER] any buffer present
58506458e27SJonathan Cameron * @scan_bytes: [INTERN] num bytes captured to be fed to buffer demux
5868a76356eSMatti Vaittinen * @available_scan_masks: [DRIVER] optional array of allowed bitmasks. Sort the
5878a76356eSMatti Vaittinen * array in order of preference, the most preferred
5888a76356eSMatti Vaittinen * masks first.
58906458e27SJonathan Cameron * @masklength: [INTERN] the length of the mask established from
59006458e27SJonathan Cameron * channels
59106458e27SJonathan Cameron * @active_scan_mask: [INTERN] union of all scan masks requested by buffers
59206458e27SJonathan Cameron * @scan_timestamp: [INTERN] set if any buffers have requested timestamp
59306458e27SJonathan Cameron * @trig: [INTERN] current device trigger (buffer modes)
59406458e27SJonathan Cameron * @pollfunc: [DRIVER] function run on trigger being received
595735ad074SVladimir Barinov * @pollfunc_event: [DRIVER] function run on events trigger being received
59606458e27SJonathan Cameron * @channels: [DRIVER] channel specification structure table
59717879488SPeter Meerwald * @num_channels: [DRIVER] number of channels specified in @channels.
59806458e27SJonathan Cameron * @name: [DRIVER] name of the device.
5992c3d0c9fSPhil Reid * @label: [DRIVER] unique name to identify which device this is
60006458e27SJonathan Cameron * @info: [DRIVER] callbacks and constant info from driver
60106458e27SJonathan Cameron * @setup_ops: [DRIVER] callbacks to call before and after buffer
60206458e27SJonathan Cameron * enable/disable
6036d4ebd56SAlexandru Ardelean * @priv: [DRIVER] reference to driver's private information
6046d4ebd56SAlexandru Ardelean * **MUST** be accessed **ONLY** via iio_priv() helper
60506458e27SJonathan Cameron */
60606458e27SJonathan Cameron struct iio_dev {
60706458e27SJonathan Cameron int modes;
60806458e27SJonathan Cameron struct device dev;
60906458e27SJonathan Cameron
61006458e27SJonathan Cameron struct iio_buffer *buffer;
61106458e27SJonathan Cameron int scan_bytes;
61206458e27SJonathan Cameron
61306458e27SJonathan Cameron const unsigned long *available_scan_masks;
6148fa714caSAndy Shevchenko unsigned int __private masklength;
61506458e27SJonathan Cameron const unsigned long *active_scan_mask;
6169351bbb1SVasileios Amoiridis bool __private scan_timestamp;
61706458e27SJonathan Cameron struct iio_trigger *trig;
61806458e27SJonathan Cameron struct iio_poll_func *pollfunc;
619735ad074SVladimir Barinov struct iio_poll_func *pollfunc_event;
62006458e27SJonathan Cameron
62106458e27SJonathan Cameron struct iio_chan_spec const *channels;
62206458e27SJonathan Cameron int num_channels;
62306458e27SJonathan Cameron
62406458e27SJonathan Cameron const char *name;
6252c3d0c9fSPhil Reid const char *label;
62606458e27SJonathan Cameron const struct iio_info *info;
62706458e27SJonathan Cameron const struct iio_buffer_setup_ops *setup_ops;
62806458e27SJonathan Cameron
62920fd1383SJonathan Cameron void *__private priv;
63006458e27SJonathan Cameron };
63106458e27SJonathan Cameron
63215ea2878SJonathan Cameron int iio_device_id(struct iio_dev *indio_dev);
6338c576f87SMiquel Raynal int iio_device_get_current_mode(struct iio_dev *indio_dev);
6342f53b4adSMiquel Raynal bool iio_buffer_enabled(struct iio_dev *indio_dev);
63515ea2878SJonathan Cameron
63606458e27SJonathan Cameron const struct iio_chan_spec
63706458e27SJonathan Cameron *iio_find_channel_from_si(struct iio_dev *indio_dev, int si);
63863b19547SJonathan Cameron /**
63963b19547SJonathan Cameron * iio_device_register() - register a device with the IIO subsystem
64063b19547SJonathan Cameron * @indio_dev: Device structure filled by the device driver
64163b19547SJonathan Cameron **/
64208e2e51fSTobin C. Harding #define iio_device_register(indio_dev) \
64308e2e51fSTobin C. Harding __iio_device_register((indio_dev), THIS_MODULE)
64463b19547SJonathan Cameron int __iio_device_register(struct iio_dev *indio_dev, struct module *this_mod);
64506458e27SJonathan Cameron void iio_device_unregister(struct iio_dev *indio_dev);
64663b19547SJonathan Cameron /**
64763b19547SJonathan Cameron * devm_iio_device_register - Resource-managed iio_device_register()
64863b19547SJonathan Cameron * @dev: Device to allocate iio_dev for
64963b19547SJonathan Cameron * @indio_dev: Device structure filled by the device driver
65063b19547SJonathan Cameron *
65163b19547SJonathan Cameron * Managed iio_device_register. The IIO device registered with this
65263b19547SJonathan Cameron * function is automatically unregistered on driver detach. This function
65363b19547SJonathan Cameron * calls iio_device_register() internally. Refer to that function for more
65463b19547SJonathan Cameron * information.
65563b19547SJonathan Cameron *
65663b19547SJonathan Cameron * RETURNS:
65763b19547SJonathan Cameron * 0 on success, negative error number on failure.
65863b19547SJonathan Cameron */
65963b19547SJonathan Cameron #define devm_iio_device_register(dev, indio_dev) \
660a0747914SLars Engebretsen __devm_iio_device_register((dev), (indio_dev), THIS_MODULE)
66163b19547SJonathan Cameron int __devm_iio_device_register(struct device *dev, struct iio_dev *indio_dev,
66263b19547SJonathan Cameron struct module *this_mod);
66306458e27SJonathan Cameron int iio_push_event(struct iio_dev *indio_dev, u64 ev_code, s64 timestamp);
6645d1dff5bSJonathan Cameron bool __iio_device_claim_direct(struct iio_dev *indio_dev);
6655d1dff5bSJonathan Cameron void __iio_device_release_direct(struct iio_dev *indio_dev);
6661dae0cb7SJonathan Cameron
6671dae0cb7SJonathan Cameron /*
668d795e38dSJonathan Cameron * Helper functions that allow claim and release of direct mode
669d795e38dSJonathan Cameron * in a fashion that doesn't generate many false positives from sparse.
670d795e38dSJonathan Cameron * Note this must remain static inline in the header so that sparse
671d795e38dSJonathan Cameron * can see the __acquire() marking. Revisit when sparse supports
672d795e38dSJonathan Cameron * __cond_acquires()
673d795e38dSJonathan Cameron */
iio_device_claim_direct(struct iio_dev * indio_dev)674d795e38dSJonathan Cameron static inline bool iio_device_claim_direct(struct iio_dev *indio_dev)
675d795e38dSJonathan Cameron {
6765d1dff5bSJonathan Cameron if (!__iio_device_claim_direct(indio_dev))
677d795e38dSJonathan Cameron return false;
678d795e38dSJonathan Cameron
679d795e38dSJonathan Cameron __acquire(iio_dev);
680d795e38dSJonathan Cameron
681d795e38dSJonathan Cameron return true;
682d795e38dSJonathan Cameron }
683d795e38dSJonathan Cameron
iio_device_release_direct(struct iio_dev * indio_dev)684d795e38dSJonathan Cameron static inline void iio_device_release_direct(struct iio_dev *indio_dev)
685d795e38dSJonathan Cameron {
6865d1dff5bSJonathan Cameron __iio_device_release_direct(indio_dev);
687d795e38dSJonathan Cameron __release(indio_dev);
688d795e38dSJonathan Cameron }
689d795e38dSJonathan Cameron
6900a856542SNuno Sá int iio_device_claim_buffer_mode(struct iio_dev *indio_dev);
6910a856542SNuno Sá void iio_device_release_buffer_mode(struct iio_dev *indio_dev);
69206458e27SJonathan Cameron
69389b1b86fSRicardo B. Marliere extern const struct bus_type iio_bus_type;
69406458e27SJonathan Cameron
69506458e27SJonathan Cameron /**
6967cbb7537SLars-Peter Clausen * iio_device_put() - reference counted deallocation of struct device
6977dcd7b60SPeter Meerwald * @indio_dev: IIO device structure containing the device
69806458e27SJonathan Cameron **/
iio_device_put(struct iio_dev * indio_dev)6997cbb7537SLars-Peter Clausen static inline void iio_device_put(struct iio_dev *indio_dev)
70006458e27SJonathan Cameron {
70106458e27SJonathan Cameron if (indio_dev)
70206458e27SJonathan Cameron put_device(&indio_dev->dev);
7036898eb89SPeter Meerwald }
70406458e27SJonathan Cameron
70562a486c4SJonathan Cameron clockid_t iio_device_get_clock(const struct iio_dev *indio_dev);
70669f0793eSGwendal Grignou int iio_device_set_clock(struct iio_dev *indio_dev, clockid_t clock_id);
70769f0793eSGwendal Grignou
708bc2b7dabSGregor Boirie /**
7097a7913f1SLars-Peter Clausen * dev_to_iio_dev() - Get IIO device struct from a device struct
7107a7913f1SLars-Peter Clausen * @dev: The device embedded in the IIO device
7117a7913f1SLars-Peter Clausen *
7127a7913f1SLars-Peter Clausen * Note: The device must be a IIO device, otherwise the result is undefined.
7137a7913f1SLars-Peter Clausen */
dev_to_iio_dev(struct device * dev)7147a7913f1SLars-Peter Clausen static inline struct iio_dev *dev_to_iio_dev(struct device *dev)
7157a7913f1SLars-Peter Clausen {
7167a7913f1SLars-Peter Clausen return container_of(dev, struct iio_dev, dev);
7177a7913f1SLars-Peter Clausen }
7187a7913f1SLars-Peter Clausen
719e4e8b776SLars-Peter Clausen /**
720e4e8b776SLars-Peter Clausen * iio_device_get() - increment reference count for the device
721e4e8b776SLars-Peter Clausen * @indio_dev: IIO device structure
722e4e8b776SLars-Peter Clausen *
723e4e8b776SLars-Peter Clausen * Returns: The passed IIO device
724e4e8b776SLars-Peter Clausen **/
iio_device_get(struct iio_dev * indio_dev)725e4e8b776SLars-Peter Clausen static inline struct iio_dev *iio_device_get(struct iio_dev *indio_dev)
726e4e8b776SLars-Peter Clausen {
727e4e8b776SLars-Peter Clausen return indio_dev ? dev_to_iio_dev(get_device(&indio_dev->dev)) : NULL;
728e4e8b776SLars-Peter Clausen }
729e4e8b776SLars-Peter Clausen
730f5d01793SAlexandru Ardelean /**
731f5d01793SAlexandru Ardelean * iio_device_set_parent() - assign parent device to the IIO device object
732f5d01793SAlexandru Ardelean * @indio_dev: IIO device structure
733f5d01793SAlexandru Ardelean * @parent: reference to parent device object
734f5d01793SAlexandru Ardelean *
735f5d01793SAlexandru Ardelean * This utility must be called between IIO device allocation
736f5d01793SAlexandru Ardelean * (via devm_iio_device_alloc()) & IIO device registration
73778ff97ebSMauro Carvalho Chehab * (via iio_device_register() and devm_iio_device_register())).
738f5d01793SAlexandru Ardelean * By default, the device allocation will also assign a parent device to
739f5d01793SAlexandru Ardelean * the IIO device object. In cases where devm_iio_device_alloc() is used,
740f5d01793SAlexandru Ardelean * sometimes the parent device must be different than the device used to
741f5d01793SAlexandru Ardelean * manage the allocation.
742f5d01793SAlexandru Ardelean * In that case, this helper should be used to change the parent, hence the
743f5d01793SAlexandru Ardelean * requirement to call this between allocation & registration.
744f5d01793SAlexandru Ardelean **/
iio_device_set_parent(struct iio_dev * indio_dev,struct device * parent)745f5d01793SAlexandru Ardelean static inline void iio_device_set_parent(struct iio_dev *indio_dev,
746f5d01793SAlexandru Ardelean struct device *parent)
747f5d01793SAlexandru Ardelean {
748f5d01793SAlexandru Ardelean indio_dev->dev.parent = parent;
749f5d01793SAlexandru Ardelean }
7502d66f389SLars-Peter Clausen
7512d66f389SLars-Peter Clausen /**
7522d66f389SLars-Peter Clausen * iio_device_set_drvdata() - Set device driver data
7532d66f389SLars-Peter Clausen * @indio_dev: IIO device structure
7542d66f389SLars-Peter Clausen * @data: Driver specific data
7552d66f389SLars-Peter Clausen *
7562d66f389SLars-Peter Clausen * Allows to attach an arbitrary pointer to an IIO device, which can later be
7572d66f389SLars-Peter Clausen * retrieved by iio_device_get_drvdata().
7582d66f389SLars-Peter Clausen */
iio_device_set_drvdata(struct iio_dev * indio_dev,void * data)7592d66f389SLars-Peter Clausen static inline void iio_device_set_drvdata(struct iio_dev *indio_dev, void *data)
7602d66f389SLars-Peter Clausen {
7612d66f389SLars-Peter Clausen dev_set_drvdata(&indio_dev->dev, data);
7622d66f389SLars-Peter Clausen }
7632d66f389SLars-Peter Clausen
7642d66f389SLars-Peter Clausen /**
7652d66f389SLars-Peter Clausen * iio_device_get_drvdata() - Get device driver data
7662d66f389SLars-Peter Clausen * @indio_dev: IIO device structure
7672d66f389SLars-Peter Clausen *
7682d66f389SLars-Peter Clausen * Returns the data previously set with iio_device_set_drvdata()
7692d66f389SLars-Peter Clausen */
iio_device_get_drvdata(const struct iio_dev * indio_dev)77018d56385SJonathan Cameron static inline void *iio_device_get_drvdata(const struct iio_dev *indio_dev)
7712d66f389SLars-Peter Clausen {
7722d66f389SLars-Peter Clausen return dev_get_drvdata(&indio_dev->dev);
7732d66f389SLars-Peter Clausen }
7742d66f389SLars-Peter Clausen
77512c4efe3SJonathan Cameron /*
77612c4efe3SJonathan Cameron * Used to ensure the iio_priv() structure is aligned to allow that structure
77712c4efe3SJonathan Cameron * to in turn include IIO_DMA_MINALIGN'd elements such as buffers which
77812c4efe3SJonathan Cameron * must not share cachelines with the rest of the structure, thus making
77912c4efe3SJonathan Cameron * them safe for use with non-coherent DMA.
780fa19c303SDavid Lechner *
781fa19c303SDavid Lechner * A number of drivers also use this on buffers that include a 64-bit timestamp
782fa19c303SDavid Lechner * that is used with iio_push_to_buffer_with_ts(). Therefore, in the case where
783fa19c303SDavid Lechner * DMA alignment is not sufficient for proper timestamp alignment, we align to
784fa19c303SDavid Lechner * 8 bytes instead.
78512c4efe3SJonathan Cameron */
786fa19c303SDavid Lechner #define IIO_DMA_MINALIGN MAX(ARCH_DMA_MINALIGN, sizeof(s64))
787fa19c303SDavid Lechner
788*63fc5352SDavid Lechner #define __IIO_DECLARE_BUFFER_WITH_TS(type, name, count) \
789*63fc5352SDavid Lechner type name[ALIGN((count), sizeof(s64) / sizeof(type)) + sizeof(s64) / sizeof(type)]
790*63fc5352SDavid Lechner
791*63fc5352SDavid Lechner /**
792*63fc5352SDavid Lechner * IIO_DECLARE_BUFFER_WITH_TS() - Declare a buffer with timestamp
793*63fc5352SDavid Lechner * @type: element type of the buffer
794*63fc5352SDavid Lechner * @name: identifier name of the buffer
795*63fc5352SDavid Lechner * @count: number of elements in the buffer
796*63fc5352SDavid Lechner *
797*63fc5352SDavid Lechner * Declares a buffer that is safe to use with iio_push_to_buffer_with_ts(). In
798*63fc5352SDavid Lechner * addition to allocating enough space for @count elements of @type, it also
799*63fc5352SDavid Lechner * allocates space for a s64 timestamp at the end of the buffer and ensures
800*63fc5352SDavid Lechner * proper alignment of the timestamp.
801*63fc5352SDavid Lechner */
802*63fc5352SDavid Lechner #define IIO_DECLARE_BUFFER_WITH_TS(type, name, count) \
803*63fc5352SDavid Lechner __IIO_DECLARE_BUFFER_WITH_TS(type, name, count) __aligned(sizeof(s64))
804*63fc5352SDavid Lechner
805*63fc5352SDavid Lechner /**
806*63fc5352SDavid Lechner * IIO_DECLARE_DMA_BUFFER_WITH_TS() - Declare a DMA-aligned buffer with timestamp
807*63fc5352SDavid Lechner * @type: element type of the buffer
808*63fc5352SDavid Lechner * @name: identifier name of the buffer
809*63fc5352SDavid Lechner * @count: number of elements in the buffer
810*63fc5352SDavid Lechner *
811*63fc5352SDavid Lechner * Same as IIO_DECLARE_BUFFER_WITH_TS(), but is uses __aligned(IIO_DMA_MINALIGN)
812*63fc5352SDavid Lechner * to ensure that the buffer doesn't share cachelines with anything that comes
813*63fc5352SDavid Lechner * before it in a struct. This should not be used for stack-allocated buffers
814*63fc5352SDavid Lechner * as stack memory cannot generally be used for DMA.
815*63fc5352SDavid Lechner */
816*63fc5352SDavid Lechner #define IIO_DECLARE_DMA_BUFFER_WITH_TS(type, name, count) \
817*63fc5352SDavid Lechner __IIO_DECLARE_BUFFER_WITH_TS(type, name, count) __aligned(IIO_DMA_MINALIGN)
818*63fc5352SDavid Lechner
81978289b4aSAlexandru Ardelean struct iio_dev *iio_device_alloc(struct device *parent, int sizeof_priv);
82006458e27SJonathan Cameron
8216d4ebd56SAlexandru Ardelean /* The information at the returned address is guaranteed to be cacheline aligned */
iio_priv(const struct iio_dev * indio_dev)82206458e27SJonathan Cameron static inline void *iio_priv(const struct iio_dev *indio_dev)
82306458e27SJonathan Cameron {
8249a5a2483SAndy Shevchenko return ACCESS_PRIVATE(indio_dev, priv);
82506458e27SJonathan Cameron }
82606458e27SJonathan Cameron
8277cbb7537SLars-Peter Clausen void iio_device_free(struct iio_dev *indio_dev);
82878289b4aSAlexandru Ardelean struct iio_dev *devm_iio_device_alloc(struct device *parent, int sizeof_priv);
82906458e27SJonathan Cameron
830bc72d938SDmitry Rokosov #define devm_iio_trigger_alloc(parent, fmt, ...) \
831bc72d938SDmitry Rokosov __devm_iio_trigger_alloc((parent), THIS_MODULE, (fmt), ##__VA_ARGS__)
832bc72d938SDmitry Rokosov __printf(3, 4)
833bc72d938SDmitry Rokosov struct iio_trigger *__devm_iio_trigger_alloc(struct device *parent,
834bc72d938SDmitry Rokosov struct module *this_mod,
835bc72d938SDmitry Rokosov const char *fmt, ...);
83606458e27SJonathan Cameron /**
83706458e27SJonathan Cameron * iio_get_debugfs_dentry() - helper function to get the debugfs_dentry
8387dcd7b60SPeter Meerwald * @indio_dev: IIO device structure for device
83906458e27SJonathan Cameron **/
84006458e27SJonathan Cameron #if defined(CONFIG_DEBUG_FS)
84196fb1b67SAlexandru Ardelean struct dentry *iio_get_debugfs_dentry(struct iio_dev *indio_dev);
84206458e27SJonathan Cameron #else
iio_get_debugfs_dentry(struct iio_dev * indio_dev)84306458e27SJonathan Cameron static inline struct dentry *iio_get_debugfs_dentry(struct iio_dev *indio_dev)
84406458e27SJonathan Cameron {
84506458e27SJonathan Cameron return NULL;
8466898eb89SPeter Meerwald }
84706458e27SJonathan Cameron #endif
84806458e27SJonathan Cameron
8492837efdcSDenis Benato /**
8502837efdcSDenis Benato * iio_device_suspend_triggering() - suspend trigger attached to an iio_dev
8512837efdcSDenis Benato * @indio_dev: iio_dev associated with the device that will have triggers suspended
8522837efdcSDenis Benato *
8532837efdcSDenis Benato * Return 0 if successful, negative otherwise
8542837efdcSDenis Benato **/
8552837efdcSDenis Benato int iio_device_suspend_triggering(struct iio_dev *indio_dev);
8562837efdcSDenis Benato
8572837efdcSDenis Benato /**
8582837efdcSDenis Benato * iio_device_resume_triggering() - resume trigger attached to an iio_dev
8592837efdcSDenis Benato * that was previously suspended with iio_device_suspend_triggering()
8602837efdcSDenis Benato * @indio_dev: iio_dev associated with the device that will have triggers resumed
8612837efdcSDenis Benato *
8622837efdcSDenis Benato * Return 0 if successful, negative otherwise
8632837efdcSDenis Benato **/
8642837efdcSDenis Benato int iio_device_resume_triggering(struct iio_dev *indio_dev);
8652837efdcSDenis Benato
86602eae0bbSHans de Goede #ifdef CONFIG_ACPI
86702eae0bbSHans de Goede bool iio_read_acpi_mount_matrix(struct device *dev,
86802eae0bbSHans de Goede struct iio_mount_matrix *orientation,
86902eae0bbSHans de Goede char *acpi_method);
870dc60de4eSAndy Shevchenko const char *iio_get_acpi_device_name_and_data(struct device *dev, const void **data);
87102eae0bbSHans de Goede #else
iio_read_acpi_mount_matrix(struct device * dev,struct iio_mount_matrix * orientation,char * acpi_method)87202eae0bbSHans de Goede static inline bool iio_read_acpi_mount_matrix(struct device *dev,
87302eae0bbSHans de Goede struct iio_mount_matrix *orientation,
87402eae0bbSHans de Goede char *acpi_method)
87502eae0bbSHans de Goede {
87602eae0bbSHans de Goede return false;
87702eae0bbSHans de Goede }
878dc60de4eSAndy Shevchenko static inline const char *
iio_get_acpi_device_name_and_data(struct device * dev,const void ** data)879dc60de4eSAndy Shevchenko iio_get_acpi_device_name_and_data(struct device *dev, const void **data)
880dc60de4eSAndy Shevchenko {
881dc60de4eSAndy Shevchenko return NULL;
882dc60de4eSAndy Shevchenko }
88302eae0bbSHans de Goede #endif
iio_get_acpi_device_name(struct device * dev)884dc60de4eSAndy Shevchenko static inline const char *iio_get_acpi_device_name(struct device *dev)
885dc60de4eSAndy Shevchenko {
886dc60de4eSAndy Shevchenko return iio_get_acpi_device_name_and_data(dev, NULL);
887dc60de4eSAndy Shevchenko }
88802eae0bbSHans de Goede
889d8f2bb50SDavid Lechner /**
890d8f2bb50SDavid Lechner * iio_get_current_scan_type - Get the current scan type for a channel
891d8f2bb50SDavid Lechner * @indio_dev: the IIO device to get the scan type for
892d8f2bb50SDavid Lechner * @chan: the channel to get the scan type for
893d8f2bb50SDavid Lechner *
894d8f2bb50SDavid Lechner * Most devices only have one scan type per channel and can just access it
895d8f2bb50SDavid Lechner * directly without calling this function. Core IIO code and drivers that
896d8f2bb50SDavid Lechner * implement ext_scan_type in the channel spec should use this function to
897d8f2bb50SDavid Lechner * get the current scan type for a channel.
898d8f2bb50SDavid Lechner *
899d8f2bb50SDavid Lechner * Returns: the current scan type for the channel or error.
900d8f2bb50SDavid Lechner */
901d8f2bb50SDavid Lechner static inline const struct iio_scan_type
iio_get_current_scan_type(const struct iio_dev * indio_dev,const struct iio_chan_spec * chan)902d8f2bb50SDavid Lechner *iio_get_current_scan_type(const struct iio_dev *indio_dev,
903d8f2bb50SDavid Lechner const struct iio_chan_spec *chan)
904d8f2bb50SDavid Lechner {
905d8f2bb50SDavid Lechner int ret;
906d8f2bb50SDavid Lechner
907d8f2bb50SDavid Lechner if (chan->has_ext_scan_type) {
908d8f2bb50SDavid Lechner ret = indio_dev->info->get_current_scan_type(indio_dev, chan);
909d8f2bb50SDavid Lechner if (ret < 0)
910d8f2bb50SDavid Lechner return ERR_PTR(ret);
911d8f2bb50SDavid Lechner
912d8f2bb50SDavid Lechner if (ret >= chan->num_ext_scan_type)
913d8f2bb50SDavid Lechner return ERR_PTR(-EINVAL);
914d8f2bb50SDavid Lechner
915d8f2bb50SDavid Lechner return &chan->ext_scan_type[ret];
916d8f2bb50SDavid Lechner }
917d8f2bb50SDavid Lechner
918d8f2bb50SDavid Lechner return &chan->scan_type;
919d8f2bb50SDavid Lechner }
920d8f2bb50SDavid Lechner
921de79583fSNuno Sa /**
922de79583fSNuno Sa * iio_get_masklength - Get length of the channels mask
923de79583fSNuno Sa * @indio_dev: the IIO device to get the masklength for
924de79583fSNuno Sa */
iio_get_masklength(const struct iio_dev * indio_dev)925de79583fSNuno Sa static inline unsigned int iio_get_masklength(const struct iio_dev *indio_dev)
926de79583fSNuno Sa {
927f44e94afSNuno Sa return ACCESS_PRIVATE(indio_dev, masklength);
928de79583fSNuno Sa }
929de79583fSNuno Sa
930d092b686SJulien Stephan int iio_active_scan_mask_index(struct iio_dev *indio_dev);
931d092b686SJulien Stephan
932de79583fSNuno Sa /**
933de79583fSNuno Sa * iio_for_each_active_channel - Iterated over active channels
934de79583fSNuno Sa * @indio_dev: the IIO device
935de79583fSNuno Sa * @chan: Holds the index of the enabled channel
936de79583fSNuno Sa */
937de79583fSNuno Sa #define iio_for_each_active_channel(indio_dev, chan) \
938de79583fSNuno Sa for_each_set_bit((chan), (indio_dev)->active_scan_mask, \
939de79583fSNuno Sa iio_get_masklength(indio_dev))
940de79583fSNuno Sa
9417d2c2acaSAndrew F. Davis ssize_t iio_format_value(char *buf, unsigned int type, int size, int *vals);
9427d2c2acaSAndrew F. Davis
9436807d721SLars-Peter Clausen int iio_str_to_fixpoint(const char *str, int fract_mult, int *integer,
9446807d721SLars-Peter Clausen int *fract);
9456807d721SLars-Peter Clausen
9469dbf8ccdSLars-Peter Clausen /**
9479dbf8ccdSLars-Peter Clausen * IIO_DEGREE_TO_RAD() - Convert degree to rad
9489dbf8ccdSLars-Peter Clausen * @deg: A value in degree
9499dbf8ccdSLars-Peter Clausen *
9509dbf8ccdSLars-Peter Clausen * Returns the given value converted from degree to rad
9519dbf8ccdSLars-Peter Clausen */
9529dbf8ccdSLars-Peter Clausen #define IIO_DEGREE_TO_RAD(deg) (((deg) * 314159ULL + 9000000ULL) / 18000000ULL)
9539dbf8ccdSLars-Peter Clausen
9549dbf8ccdSLars-Peter Clausen /**
955c689a923SLars-Peter Clausen * IIO_RAD_TO_DEGREE() - Convert rad to degree
956c689a923SLars-Peter Clausen * @rad: A value in rad
957c689a923SLars-Peter Clausen *
958c689a923SLars-Peter Clausen * Returns the given value converted from rad to degree
959c689a923SLars-Peter Clausen */
960c689a923SLars-Peter Clausen #define IIO_RAD_TO_DEGREE(rad) \
961c689a923SLars-Peter Clausen (((rad) * 18000000ULL + 314159ULL / 2) / 314159ULL)
962c689a923SLars-Peter Clausen
963c689a923SLars-Peter Clausen /**
9649dbf8ccdSLars-Peter Clausen * IIO_G_TO_M_S_2() - Convert g to meter / second**2
9659dbf8ccdSLars-Peter Clausen * @g: A value in g
9669dbf8ccdSLars-Peter Clausen *
9679dbf8ccdSLars-Peter Clausen * Returns the given value converted from g to meter / second**2
9689dbf8ccdSLars-Peter Clausen */
9699dbf8ccdSLars-Peter Clausen #define IIO_G_TO_M_S_2(g) ((g) * 980665ULL / 100000ULL)
9709dbf8ccdSLars-Peter Clausen
971c689a923SLars-Peter Clausen /**
972c689a923SLars-Peter Clausen * IIO_M_S_2_TO_G() - Convert meter / second**2 to g
973c689a923SLars-Peter Clausen * @ms2: A value in meter / second**2
974c689a923SLars-Peter Clausen *
975c689a923SLars-Peter Clausen * Returns the given value converted from meter / second**2 to g
976c689a923SLars-Peter Clausen */
977c689a923SLars-Peter Clausen #define IIO_M_S_2_TO_G(ms2) (((ms2) * 100000ULL + 980665ULL / 2) / 980665ULL)
978c689a923SLars-Peter Clausen
97906458e27SJonathan Cameron #endif /* _INDUSTRIAL_IO_H_ */
980