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>
22 * Provide means of adjusting timer accuracy.
23 * Currently assumes nano seconds.
27 /* real channel types */
47 #define IIO_MOD_LIGHT_BOTH 0
49 #define IIO_MOD_LIGHT_IR 1
51 #define IIO_MOD_X_AND_Y 3
52 #define IIO_MOD_X_ANX_Z 4
53 #define IIO_MOD_Y_AND_Z 5
54 #define IIO_MOD_X_AND_Y_AND_Z 6
55 #define IIO_MOD_X_OR_Y 7
56 #define IIO_MOD_X_OR_Z 8
57 #define IIO_MOD_Y_OR_Z 9
58 #define IIO_MOD_X_OR_Y_OR_Z 10
60 /* Could add the raw attributes as well - allowing buffer only devices */
61 enum iio_chan_info_enum
{
62 IIO_CHAN_INFO_SCALE_SHARED
,
63 IIO_CHAN_INFO_SCALE_SEPARATE
,
64 IIO_CHAN_INFO_OFFSET_SHARED
,
65 IIO_CHAN_INFO_OFFSET_SEPARATE
,
66 IIO_CHAN_INFO_CALIBSCALE_SHARED
,
67 IIO_CHAN_INFO_CALIBSCALE_SEPARATE
,
68 IIO_CHAN_INFO_CALIBBIAS_SHARED
,
69 IIO_CHAN_INFO_CALIBBIAS_SEPARATE
,
70 IIO_CHAN_INFO_PEAK_SHARED
,
71 IIO_CHAN_INFO_PEAK_SEPARATE
,
72 IIO_CHAN_INFO_PEAK_SCALE_SHARED
,
73 IIO_CHAN_INFO_PEAK_SCALE_SEPARATE
,
74 IIO_CHAN_INFO_QUADRATURE_CORRECTION_RAW_SHARED
,
75 IIO_CHAN_INFO_QUADRATURE_CORRECTION_RAW_SEPARATE
,
79 * struct iio_chan_spec - specification of a single channel
80 * @type: What type of measurement is the channel making.
81 * @channel: What number or name do we wish to asign the channel.
82 * @channel2: If there is a second number for a differential
83 * channel then this is it. If modified is set then the
84 * value here specifies the modifier.
85 * @address: Driver specific identifier.
86 * @scan_index: Monotonic index to give ordering in scans when read
88 * @scan_type: Sign: 's' or 'u' to specify signed or unsigned
89 * realbits: Number of valid bits of data
90 * storage_bits: Realbits + padding
91 * shift: Shift right by this before masking out
93 * @info_mask: What information is to be exported about this channel.
94 * This includes calibbias, scale etc.
95 * @event_mask: What events can this channel produce.
96 * @extend_name: Allows labeling of channel attributes with an
97 * informative name. Note this has no effect codes etc,
99 * @processed_val: Flag to specify the data access attribute should be
100 * *_input rather than *_raw.
101 * @modified: Does a modifier apply to this channel. What these are
102 * depends on the channel type. Modifier is set in
103 * channel2. Examples are IIO_MOD_X for axial sensors about
105 * @indexed: Specify the channel has a numerical index. If not,
106 * the value in channel will be suppressed for attribute
107 * but not for event codes. Typically set it to 0 when
108 * the index is false.
110 struct iio_chan_spec
{
111 enum iio_chan_type type
;
114 unsigned long address
;
125 unsigned processed_val
:1;
130 #define IIO_ST(si, rb, sb, sh) \
131 { .sign = si, .realbits = rb, .storagebits = sb, .shift = sh }
133 #define IIO_CHAN(_type, _mod, _indexed, _proc, _name, _chan, _chan2, \
134 _inf_mask, _address, _si, _stype, _event_mask) \
137 .indexed = _indexed, \
138 .processed_val = _proc, \
139 .extend_name = _name, \
141 .channel2 = _chan2, \
142 .info_mask = _inf_mask, \
143 .address = _address, \
145 .scan_type = _stype, \
146 .event_mask = _event_mask }
148 #define IIO_CHAN_SOFT_TIMESTAMP(_si) \
149 { .type = IIO_TIMESTAMP, .channel = -1, \
150 .scan_index = _si, .scan_type = IIO_ST('s', 64, 64, 0) }
153 * iio_get_time_ns() - utility function to get a time stamp for events etc
155 static inline s64
iio_get_time_ns(void)
159 * calls getnstimeofday.
160 * If hrtimers then up to ns accurate, if not microsecond.
162 ktime_get_real_ts(&ts
);
164 return timespec_to_ns(&ts
);
167 /* Device operating modes */
168 #define INDIO_DIRECT_MODE 0x01
169 #define INDIO_RING_TRIGGERED 0x02
170 #define INDIO_RING_HARDWARE_BUFFER 0x08
172 #define INDIO_ALL_RING_MODES (INDIO_RING_TRIGGERED | INDIO_RING_HARDWARE_BUFFER)
174 /* Vast majority of this is set by the industrialio subsystem on a
175 * call to iio_device_register. */
176 #define IIO_VAL_INT 1
177 #define IIO_VAL_INT_PLUS_MICRO 2
178 #define IIO_VAL_INT_PLUS_NANO 3
180 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
);
249 * struct iio_dev - industrial I/O device
250 * @id: [INTERN] used to identify device internally
251 * @modes: [DRIVER] operating modes supported by device
252 * @currentmode: [DRIVER] current operating mode
253 * @dev: [DRIVER] device structure, should be assigned a parent
255 * @event_interfaces: [INTERN] event chrdevs associated with interrupt lines
256 * @ring: [DRIVER] any ring buffer present
257 * @mlock: [INTERN] lock used to prevent simultaneous device state
259 * @available_scan_masks: [DRIVER] optional array of allowed bitmasks
260 * @trig: [INTERN] current device trigger (ring buffer modes)
261 * @pollfunc: [DRIVER] function run on trigger being received
262 * @channels: [DRIVER] channel specification structure table
263 * @num_channels: [DRIVER] number of chanels specified in @channels.
264 * @channel_attr_list: [INTERN] keep track of automatically created channel
266 * @name: [DRIVER] name of the device.
275 struct iio_event_interface
*event_interfaces
;
277 struct iio_ring_buffer
*ring
;
280 u32
*available_scan_masks
;
281 struct iio_trigger
*trig
;
282 struct iio_poll_func
*pollfunc
;
284 struct iio_chan_spec
const *channels
;
287 struct list_head channel_attr_list
;
289 const struct iio_info
*info
;
293 * iio_device_register() - register a device with the IIO subsystem
294 * @dev_info: Device structure filled by the device driver
296 int iio_device_register(struct iio_dev
*dev_info
);
299 * iio_device_unregister() - unregister a device from the IIO subsystem
300 * @dev_info: Device structure representing the device.
302 void iio_device_unregister(struct iio_dev
*dev_info
);
305 * iio_push_event() - try to add event to the list for userspace reading
306 * @dev_info: IIO device structure
307 * @ev_line: Which event line (hardware interrupt)
308 * @ev_code: What event
309 * @timestamp: When the event occurred
311 int iio_push_event(struct iio_dev
*dev_info
,
316 extern struct bus_type iio_bus_type
;
319 * iio_put_device() - reference counted deallocation of struct device
320 * @dev: the iio_device containing the device
322 static inline void iio_put_device(struct iio_dev
*dev
)
325 put_device(&dev
->dev
);
328 /* Can we make this smaller? */
329 #define IIO_ALIGN L1_CACHE_BYTES
331 * iio_allocate_device() - allocate an iio_dev from a driver
332 * @sizeof_priv: Space to allocate for private structure.
334 struct iio_dev
*iio_allocate_device(int sizeof_priv
);
336 static inline void *iio_priv(const struct iio_dev
*dev
)
338 return (char *)dev
+ ALIGN(sizeof(struct iio_dev
), IIO_ALIGN
);
341 static inline struct iio_dev
*iio_priv_to_dev(void *priv
)
343 return (struct iio_dev
*)((char *)priv
-
344 ALIGN(sizeof(struct iio_dev
), IIO_ALIGN
));
348 * iio_free_device() - free an iio_dev from a driver
349 * @dev: the iio_dev associated with the device
351 void iio_free_device(struct iio_dev
*dev
);
354 * iio_ring_enabled() - helper function to test if any form of ring is enabled
355 * @dev_info: IIO device info structure for device
357 static inline bool iio_ring_enabled(struct iio_dev
*dev_info
)
359 return dev_info
->currentmode
360 & (INDIO_RING_TRIGGERED
361 | INDIO_RING_HARDWARE_BUFFER
);
364 #endif /* _INDUSTRIAL_IO_H_ */