2 /* The industrial I/O core
4 * Copyright (c) 2008 Jonathan Cameron
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published by
8 * the Free Software Foundation.
11 #ifndef _INDUSTRIAL_IO_H_
12 #define _INDUSTRIAL_IO_H_
14 #include <linux/device.h>
15 #include <linux/cdev.h>
19 * Provide means of adjusting timer accuracy.
20 * Currently assumes nano seconds.
29 /* real channel types */
47 /* Nasty hack to avoid massive churn */
48 #define IIO_IN IIO_VOLTAGE
49 #define IIO_IN_DIFF IIO_VOLTAGE_DIFF
52 #define IIO_MOD_LIGHT_BOTH 0
54 #define IIO_MOD_LIGHT_IR 1
56 #define IIO_MOD_X_AND_Y 3
57 #define IIO_MOD_X_ANX_Z 4
58 #define IIO_MOD_Y_AND_Z 5
59 #define IIO_MOD_X_AND_Y_AND_Z 6
60 #define IIO_MOD_X_OR_Y 7
61 #define IIO_MOD_X_OR_Z 8
62 #define IIO_MOD_Y_OR_Z 9
63 #define IIO_MOD_X_OR_Y_OR_Z 10
65 /* Could add the raw attributes as well - allowing buffer only devices */
66 enum iio_chan_info_enum
{
67 IIO_CHAN_INFO_SCALE_SHARED
,
68 IIO_CHAN_INFO_SCALE_SEPARATE
,
69 IIO_CHAN_INFO_OFFSET_SHARED
,
70 IIO_CHAN_INFO_OFFSET_SEPARATE
,
71 IIO_CHAN_INFO_CALIBSCALE_SHARED
,
72 IIO_CHAN_INFO_CALIBSCALE_SEPARATE
,
73 IIO_CHAN_INFO_CALIBBIAS_SHARED
,
74 IIO_CHAN_INFO_CALIBBIAS_SEPARATE
,
75 IIO_CHAN_INFO_PEAK_SHARED
,
76 IIO_CHAN_INFO_PEAK_SEPARATE
,
77 IIO_CHAN_INFO_PEAK_SCALE_SHARED
,
78 IIO_CHAN_INFO_PEAK_SCALE_SEPARATE
,
79 IIO_CHAN_INFO_QUADRATURE_CORRECTION_RAW_SHARED
,
80 IIO_CHAN_INFO_QUADRATURE_CORRECTION_RAW_SEPARATE
,
90 * struct iio_chan_spec - specification of a single channel
91 * @type: What type of measurement is the channel making.
92 * @channel: What number or name do we wish to asign the channel.
93 * @channel2: If there is a second number for a differential
94 * channel then this is it. If modified is set then the
95 * value here specifies the modifier.
96 * @address: Driver specific identifier.
97 * @scan_index: Monotonic index to give ordering in scans when read
99 * @scan_type: Sign: 's' or 'u' to specify signed or unsigned
100 * realbits: Number of valid bits of data
101 * storage_bits: Realbits + padding
102 * shift: Shift right by this before masking out
104 * endianness: little or big endian
105 * @info_mask: What information is to be exported about this channel.
106 * This includes calibbias, scale etc.
107 * @event_mask: What events can this channel produce.
108 * @extend_name: Allows labeling of channel attributes with an
109 * informative name. Note this has no effect codes etc,
111 * @processed_val: Flag to specify the data access attribute should be
112 * *_input rather than *_raw.
113 * @modified: Does a modifier apply to this channel. What these are
114 * depends on the channel type. Modifier is set in
115 * channel2. Examples are IIO_MOD_X for axial sensors about
117 * @indexed: Specify the channel has a numerical index. If not,
118 * the value in channel will be suppressed for attribute
119 * but not for event codes. Typically set it to 0 when
120 * the index is false.
122 struct iio_chan_spec
{
123 enum iio_chan_type type
;
126 unsigned long address
;
133 enum iio_endian endianness
;
138 unsigned processed_val
:1;
144 #define IIO_ST(si, rb, sb, sh) \
145 { .sign = si, .realbits = rb, .storagebits = sb, .shift = sh }
147 /* Macro assumes input channels */
148 #define IIO_CHAN(_type, _mod, _indexed, _proc, _name, _chan, _chan2, \
149 _inf_mask, _address, _si, _stype, _event_mask) \
153 .indexed = _indexed, \
154 .processed_val = _proc, \
155 .extend_name = _name, \
157 .channel2 = _chan2, \
158 .info_mask = _inf_mask, \
159 .address = _address, \
161 .scan_type = _stype, \
162 .event_mask = _event_mask }
164 #define IIO_CHAN_SOFT_TIMESTAMP(_si) \
165 { .type = IIO_TIMESTAMP, .channel = -1, \
166 .scan_index = _si, .scan_type = IIO_ST('s', 64, 64, 0) }
169 * iio_get_time_ns() - utility function to get a time stamp for events etc
171 static inline s64
iio_get_time_ns(void)
175 * calls getnstimeofday.
176 * If hrtimers then up to ns accurate, if not microsecond.
178 ktime_get_real_ts(&ts
);
180 return timespec_to_ns(&ts
);
183 /* Device operating modes */
184 #define INDIO_DIRECT_MODE 0x01
185 #define INDIO_RING_TRIGGERED 0x02
186 #define INDIO_RING_HARDWARE_BUFFER 0x08
188 #define INDIO_ALL_RING_MODES (INDIO_RING_TRIGGERED | INDIO_RING_HARDWARE_BUFFER)
190 /* Vast majority of this is set by the industrialio subsystem on a
191 * call to iio_device_register. */
192 #define IIO_VAL_INT 1
193 #define IIO_VAL_INT_PLUS_MICRO 2
194 #define IIO_VAL_INT_PLUS_NANO 3
196 struct iio_trigger
; /* forward declaration */
200 * struct iio_info - constant information about device
201 * @driver_module: module structure used to ensure correct
202 * ownership of chrdevs etc
203 * @event_attrs: event control attributes
204 * @attrs: general purpose device attributes
205 * @read_raw: function to request a value from the device.
206 * mask specifies which value. Note 0 means a reading of
207 * the channel in question. Return value will specify the
208 * type of value returned by the device. val and val2 will
209 * contain the elements making up the returned value.
210 * @write_raw: function to write a value to the device.
211 * Parameters are the same as for read_raw.
212 * @write_raw_get_fmt: callback function to query the expected
213 * format/precision. If not set by the driver, write_raw
214 * returns IIO_VAL_INT_PLUS_MICRO.
215 * @read_event_config: find out if the event is enabled.
216 * @write_event_config: set if the event is enabled.
217 * @read_event_value: read a value associated with the event. Meaning
218 * is event dependant. event_code specifies which event.
219 * @write_event_value: write the value associate with the event.
220 * Meaning is event dependent.
221 * @validate_trigger: function to validate the trigger when the
222 * current trigger gets changed.
225 struct module
*driver_module
;
226 struct attribute_group
*event_attrs
;
227 const struct attribute_group
*attrs
;
229 int (*read_raw
)(struct iio_dev
*indio_dev
,
230 struct iio_chan_spec
const *chan
,
235 int (*write_raw
)(struct iio_dev
*indio_dev
,
236 struct iio_chan_spec
const *chan
,
241 int (*write_raw_get_fmt
)(struct iio_dev
*indio_dev
,
242 struct iio_chan_spec
const *chan
,
245 int (*read_event_config
)(struct iio_dev
*indio_dev
,
248 int (*write_event_config
)(struct iio_dev
*indio_dev
,
252 int (*read_event_value
)(struct iio_dev
*indio_dev
,
255 int (*write_event_value
)(struct iio_dev
*indio_dev
,
258 int (*validate_trigger
)(struct iio_dev
*indio_dev
,
259 struct iio_trigger
*trig
);
264 * struct iio_dev - industrial I/O device
265 * @id: [INTERN] used to identify device internally
266 * @modes: [DRIVER] operating modes supported by device
267 * @currentmode: [DRIVER] current operating mode
268 * @dev: [DRIVER] device structure, should be assigned a parent
270 * @event_interface: [INTERN] event chrdevs associated with interrupt lines
271 * @ring: [DRIVER] any ring buffer present
272 * @mlock: [INTERN] lock used to prevent simultaneous device state
274 * @available_scan_masks: [DRIVER] optional array of allowed bitmasks
275 * @trig: [INTERN] current device trigger (ring buffer modes)
276 * @pollfunc: [DRIVER] function run on trigger being received
277 * @channels: [DRIVER] channel specification structure table
278 * @num_channels: [DRIVER] number of chanels specified in @channels.
279 * @channel_attr_list: [INTERN] keep track of automatically created channel
281 * @name: [DRIVER] name of the device.
282 * @info: [DRIVER] callbacks and constant info from driver
283 * @chrdev: [INTERN] associated character device
292 struct iio_event_interface
*event_interface
;
294 struct iio_ring_buffer
*ring
;
297 u32
*available_scan_masks
;
298 struct iio_trigger
*trig
;
299 struct iio_poll_func
*pollfunc
;
301 struct iio_chan_spec
const *channels
;
304 struct list_head channel_attr_list
;
306 const struct iio_info
*info
;
311 * iio_device_register() - register a device with the IIO subsystem
312 * @dev_info: Device structure filled by the device driver
314 int iio_device_register(struct iio_dev
*dev_info
);
317 * iio_device_unregister() - unregister a device from the IIO subsystem
318 * @dev_info: Device structure representing the device.
320 void iio_device_unregister(struct iio_dev
*dev_info
);
323 * iio_push_event() - try to add event to the list for userspace reading
324 * @dev_info: IIO device structure
325 * @ev_code: What event
326 * @timestamp: When the event occurred
328 int iio_push_event(struct iio_dev
*dev_info
, int ev_code
, s64 timestamp
);
330 extern struct bus_type iio_bus_type
;
333 * iio_put_device() - reference counted deallocation of struct device
334 * @dev: the iio_device containing the device
336 static inline void iio_put_device(struct iio_dev
*dev
)
339 put_device(&dev
->dev
);
342 /* Can we make this smaller? */
343 #define IIO_ALIGN L1_CACHE_BYTES
345 * iio_allocate_device() - allocate an iio_dev from a driver
346 * @sizeof_priv: Space to allocate for private structure.
348 struct iio_dev
*iio_allocate_device(int sizeof_priv
);
350 static inline void *iio_priv(const struct iio_dev
*dev
)
352 return (char *)dev
+ ALIGN(sizeof(struct iio_dev
), IIO_ALIGN
);
355 static inline struct iio_dev
*iio_priv_to_dev(void *priv
)
357 return (struct iio_dev
*)((char *)priv
-
358 ALIGN(sizeof(struct iio_dev
), IIO_ALIGN
));
362 * iio_free_device() - free an iio_dev from a driver
363 * @dev: the iio_dev associated with the device
365 void iio_free_device(struct iio_dev
*dev
);
368 * iio_ring_enabled() - helper function to test if any form of ring is enabled
369 * @dev_info: IIO device info structure for device
371 static inline bool iio_ring_enabled(struct iio_dev
*dev_info
)
373 return dev_info
->currentmode
374 & (INDIO_RING_TRIGGERED
375 | INDIO_RING_HARDWARE_BUFFER
);
378 #endif /* _INDUSTRIAL_IO_H_ */