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>
16 #include <linux/irq.h>
21 * Provide means of adjusting timer accuracy.
22 * Currently assumes nano seconds.
26 /* real channel types */
46 #define IIO_MOD_LIGHT_BOTH 0
48 #define IIO_MOD_LIGHT_IR 1
50 #define IIO_MOD_X_AND_Y 3
51 #define IIO_MOD_X_ANX_Z 4
52 #define IIO_MOD_Y_AND_Z 5
53 #define IIO_MOD_X_AND_Y_AND_Z 6
54 #define IIO_MOD_X_OR_Y 7
55 #define IIO_MOD_X_OR_Z 8
56 #define IIO_MOD_Y_OR_Z 9
57 #define IIO_MOD_X_OR_Y_OR_Z 10
59 /* Could add the raw attributes as well - allowing buffer only devices */
60 enum iio_chan_info_enum
{
61 IIO_CHAN_INFO_SCALE_SHARED
,
62 IIO_CHAN_INFO_SCALE_SEPARATE
,
63 IIO_CHAN_INFO_OFFSET_SHARED
,
64 IIO_CHAN_INFO_OFFSET_SEPARATE
,
65 IIO_CHAN_INFO_CALIBSCALE_SHARED
,
66 IIO_CHAN_INFO_CALIBSCALE_SEPARATE
,
67 IIO_CHAN_INFO_CALIBBIAS_SHARED
,
68 IIO_CHAN_INFO_CALIBBIAS_SEPARATE
,
69 IIO_CHAN_INFO_PEAK_SHARED
,
70 IIO_CHAN_INFO_PEAK_SEPARATE
,
71 IIO_CHAN_INFO_PEAK_SCALE_SHARED
,
72 IIO_CHAN_INFO_PEAK_SCALE_SEPARATE
,
73 IIO_CHAN_INFO_QUADRATURE_CORRECTION_RAW_SHARED
,
74 IIO_CHAN_INFO_QUADRATURE_CORRECTION_RAW_SEPARATE
,
78 * struct iio_chan_spec - specification of a single channel
79 * @type: What type of measurement is the channel making.
80 * @channel: What number or name do we wish to asign the channel.
81 * @channel2: If there is a second number for a differential
82 * channel then this is it. If modified is set then the
83 * value here specifies the modifier.
84 * @address: Driver specific identifier.
85 * @scan_index: Monotonic index to give ordering in scans when read
87 * @scan_type: Sign: 's' or 'u' to specify signed or unsigned
88 * realbits: Number of valid bits of data
89 * storage_bits: Realbits + padding
90 * shift: Shift right by this before masking out
92 * @info_mask: What information is to be exported about this channel.
93 * This includes calibbias, scale etc.
94 * @event_mask: What events can this channel produce.
95 * @extend_name: Allows labeling of channel attributes with an
96 * informative name. Note this has no effect codes etc,
98 * @processed_val: Flag to specify the data access attribute should be
99 * *_input rather than *_raw.
100 * @modified: Does a modifier apply to this channel. What these are
101 * depends on the channel type. Modifier is set in
102 * channel2. Examples are IIO_MOD_X for axial sensors about
104 * @indexed: Specify the channel has a numerical index. If not,
105 * the value in channel will be suppressed for attribute
106 * but not for event codes. Typically set it to 0 when
107 * the index is false.
109 struct iio_chan_spec
{
110 enum iio_chan_type type
;
113 unsigned long address
;
124 unsigned processed_val
:1;
129 #define IIO_ST(si, rb, sb, sh) \
130 { .sign = si, .realbits = rb, .storagebits = sb, .shift = sh }
132 #define IIO_CHAN(_type, _mod, _indexed, _proc, _name, _chan, _chan2, \
133 _inf_mask, _address, _si, _stype, _event_mask) \
136 .indexed = _indexed, \
137 .processed_val = _proc, \
138 .extend_name = _name, \
140 .channel2 = _chan2, \
141 .info_mask = _inf_mask, \
142 .address = _address, \
144 .scan_type = _stype, \
145 .event_mask = _event_mask }
147 #define IIO_CHAN_SOFT_TIMESTAMP(_si) \
148 { .type = IIO_TIMESTAMP, .channel = -1, \
149 .scan_index = _si, .scan_type = IIO_ST('s', 64, 64, 0) }
152 * iio_get_time_ns() - utility function to get a time stamp for events etc
154 static inline s64
iio_get_time_ns(void)
158 * calls getnstimeofday.
159 * If hrtimers then up to ns accurate, if not microsecond.
161 ktime_get_real_ts(&ts
);
163 return timespec_to_ns(&ts
);
166 /* Device operating modes */
167 #define INDIO_DIRECT_MODE 0x01
168 #define INDIO_RING_TRIGGERED 0x02
169 #define INDIO_RING_HARDWARE_BUFFER 0x08
171 #define INDIO_ALL_RING_MODES (INDIO_RING_TRIGGERED | INDIO_RING_HARDWARE_BUFFER)
173 /* Vast majority of this is set by the industrialio subsystem on a
174 * call to iio_device_register. */
175 #define IIO_VAL_INT 1
176 #define IIO_VAL_INT_PLUS_MICRO 2
177 #define IIO_VAL_INT_PLUS_NANO 3
179 struct iio_trigger
; /* forward declaration */
183 * struct iio_info - constant information about device
184 * @driver_module: module structure used to ensure correct
185 * ownership of chrdevs etc
186 * @num_interrupt_lines:number of physical interrupt lines from device
187 * @event_attrs: event control attributes
188 * @attrs: general purpose device attributes
189 * @read_raw: function to request a value from the device.
190 * mask specifies which value. Note 0 means a reading of
191 * the channel in question. Return value will specify the
192 * type of value returned by the device. val and val2 will
193 * contain the elements making up the returned value.
194 * @write_raw: function to write a value to the device.
195 * Parameters are the same as for read_raw.
196 * @write_raw_get_fmt: callback function to query the expected
197 * format/precision. If not set by the driver, write_raw
198 * returns IIO_VAL_INT_PLUS_MICRO.
199 * @read_event_config: find out if the event is enabled.
200 * @write_event_config: set if the event is enabled.
201 * @read_event_value: read a value associated with the event. Meaning
202 * is event dependant. event_code specifies which event.
203 * @write_event_value: write the value associate with the event.
204 * Meaning is event dependent.
205 * @validate_trigger: function to validate the trigger when the
206 * current trigger gets changed.
209 struct module
*driver_module
;
210 int num_interrupt_lines
;
211 struct attribute_group
*event_attrs
;
212 const struct attribute_group
*attrs
;
214 int (*read_raw
)(struct iio_dev
*indio_dev
,
215 struct iio_chan_spec
const *chan
,
220 int (*write_raw
)(struct iio_dev
*indio_dev
,
221 struct iio_chan_spec
const *chan
,
226 int (*write_raw_get_fmt
)(struct iio_dev
*indio_dev
,
227 struct iio_chan_spec
const *chan
,
230 int (*read_event_config
)(struct iio_dev
*indio_dev
,
233 int (*write_event_config
)(struct iio_dev
*indio_dev
,
237 int (*read_event_value
)(struct iio_dev
*indio_dev
,
240 int (*write_event_value
)(struct iio_dev
*indio_dev
,
243 int (*validate_trigger
)(struct iio_dev
*indio_dev
,
244 struct iio_trigger
*trig
);
248 int iio_event_getfd(struct iio_dev
*indio_dev
);
250 * struct iio_dev - industrial I/O device
251 * @id: [INTERN] used to identify device internally
252 * @modes: [DRIVER] operating modes supported by device
253 * @currentmode: [DRIVER] current operating mode
254 * @dev: [DRIVER] device structure, should be assigned a parent
256 * @event_interfaces: [INTERN] event chrdevs associated with interrupt lines
257 * @ring: [DRIVER] any ring buffer present
258 * @mlock: [INTERN] lock used to prevent simultaneous device state
260 * @available_scan_masks: [DRIVER] optional array of allowed bitmasks
261 * @trig: [INTERN] current device trigger (ring buffer modes)
262 * @pollfunc: [DRIVER] function run on trigger being received
263 * @channels: [DRIVER] channel specification structure table
264 * @num_channels: [DRIVER] number of chanels specified in @channels.
265 * @channel_attr_list: [INTERN] keep track of automatically created channel
267 * @name: [DRIVER] name of the device.
276 struct iio_event_interface
*event_interfaces
;
278 struct iio_ring_buffer
*ring
;
281 u32
*available_scan_masks
;
282 struct iio_trigger
*trig
;
283 struct iio_poll_func
*pollfunc
;
285 struct iio_chan_spec
const *channels
;
288 struct list_head channel_attr_list
;
290 const struct iio_info
*info
;
294 * iio_device_register() - register a device with the IIO subsystem
295 * @dev_info: Device structure filled by the device driver
297 int iio_device_register(struct iio_dev
*dev_info
);
300 * iio_device_unregister() - unregister a device from the IIO subsystem
301 * @dev_info: Device structure representing the device.
303 void iio_device_unregister(struct iio_dev
*dev_info
);
306 * iio_push_event() - try to add event to the list for userspace reading
307 * @dev_info: IIO device structure
308 * @ev_line: Which event line (hardware interrupt)
309 * @ev_code: What event
310 * @timestamp: When the event occurred
312 int iio_push_event(struct iio_dev
*dev_info
,
317 extern struct bus_type iio_bus_type
;
320 * iio_put_device() - reference counted deallocation of struct device
321 * @dev: the iio_device containing the device
323 static inline void iio_put_device(struct iio_dev
*dev
)
326 put_device(&dev
->dev
);
329 /* Can we make this smaller? */
330 #define IIO_ALIGN L1_CACHE_BYTES
332 * iio_allocate_device() - allocate an iio_dev from a driver
333 * @sizeof_priv: Space to allocate for private structure.
335 struct iio_dev
*iio_allocate_device(int sizeof_priv
);
337 static inline void *iio_priv(const struct iio_dev
*dev
)
339 return (char *)dev
+ ALIGN(sizeof(struct iio_dev
), IIO_ALIGN
);
342 static inline struct iio_dev
*iio_priv_to_dev(void *priv
)
344 return (struct iio_dev
*)((char *)priv
-
345 ALIGN(sizeof(struct iio_dev
), IIO_ALIGN
));
349 * iio_free_device() - free an iio_dev from a driver
350 * @dev: the iio_dev associated with the device
352 void iio_free_device(struct iio_dev
*dev
);
355 * iio_ring_enabled() - helper function to test if any form of ring is enabled
356 * @dev_info: IIO device info structure for device
358 static inline bool iio_ring_enabled(struct iio_dev
*dev_info
)
360 return dev_info
->currentmode
361 & (INDIO_RING_TRIGGERED
362 | INDIO_RING_HARDWARE_BUFFER
);
365 #endif /* _INDUSTRIAL_IO_H_ */