]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/staging/iio/iio.h
staging:iio:various move default scan mask setting after ring register or remove
[mirror_ubuntu-bionic-kernel.git] / drivers / staging / iio / iio.h
1
2 /* The industrial I/O core
3 *
4 * Copyright (c) 2008 Jonathan Cameron
5 *
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.
9 */
10
11 #ifndef _INDUSTRIAL_IO_H_
12 #define _INDUSTRIAL_IO_H_
13
14 #include <linux/device.h>
15 #include <linux/cdev.h>
16
17 /* IIO TODO LIST */
18 /*
19 * Provide means of adjusting timer accuracy.
20 * Currently assumes nano seconds.
21 */
22
23 enum iio_data_type {
24 IIO_RAW,
25 IIO_PROCESSED,
26 };
27
28 enum iio_chan_type {
29 /* real channel types */
30 IIO_VOLTAGE,
31 IIO_CURRENT,
32 IIO_POWER,
33 IIO_ACCEL,
34 IIO_VOLTAGE_DIFF,
35 IIO_GYRO,
36 IIO_MAGN,
37 IIO_LIGHT,
38 IIO_INTENSITY,
39 IIO_PROXIMITY,
40 IIO_TEMP,
41 IIO_INCLI,
42 IIO_ROT,
43 IIO_ANGL,
44 IIO_TIMESTAMP,
45 };
46
47 /* Nasty hack to avoid massive churn */
48 #define IIO_IN IIO_VOLTAGE
49 #define IIO_IN_DIFF IIO_VOLTAGE_DIFF
50
51 #define IIO_MOD_X 0
52 #define IIO_MOD_LIGHT_BOTH 0
53 #define IIO_MOD_Y 1
54 #define IIO_MOD_LIGHT_IR 1
55 #define IIO_MOD_Z 2
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
64
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,
81 };
82
83 enum iio_endian {
84 IIO_CPU,
85 IIO_BE,
86 IIO_LE,
87 };
88
89 /**
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
98 * from a buffer.
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
103 * realbits.
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,
110 * unlike modifiers.
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
116 * the 'x' axis.
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.
121 */
122 struct iio_chan_spec {
123 enum iio_chan_type type;
124 int channel;
125 int channel2;
126 unsigned long address;
127 int scan_index;
128 struct {
129 char sign;
130 u8 realbits;
131 u8 storagebits;
132 u8 shift;
133 enum iio_endian endianness;
134 } scan_type;
135 long info_mask;
136 long event_mask;
137 char *extend_name;
138 unsigned processed_val:1;
139 unsigned modified:1;
140 unsigned indexed:1;
141 unsigned output:1;
142 };
143
144 #define IIO_ST(si, rb, sb, sh) \
145 { .sign = si, .realbits = rb, .storagebits = sb, .shift = sh }
146
147 /* Macro assumes input channels */
148 #define IIO_CHAN(_type, _mod, _indexed, _proc, _name, _chan, _chan2, \
149 _inf_mask, _address, _si, _stype, _event_mask) \
150 { .type = _type, \
151 .output = 0, \
152 .modified = _mod, \
153 .indexed = _indexed, \
154 .processed_val = _proc, \
155 .extend_name = _name, \
156 .channel = _chan, \
157 .channel2 = _chan2, \
158 .info_mask = _inf_mask, \
159 .address = _address, \
160 .scan_index = _si, \
161 .scan_type = _stype, \
162 .event_mask = _event_mask }
163
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) }
167
168 /**
169 * iio_get_time_ns() - utility function to get a time stamp for events etc
170 **/
171 static inline s64 iio_get_time_ns(void)
172 {
173 struct timespec ts;
174 /*
175 * calls getnstimeofday.
176 * If hrtimers then up to ns accurate, if not microsecond.
177 */
178 ktime_get_real_ts(&ts);
179
180 return timespec_to_ns(&ts);
181 }
182
183 /* Device operating modes */
184 #define INDIO_DIRECT_MODE 0x01
185 #define INDIO_RING_TRIGGERED 0x02
186 #define INDIO_RING_HARDWARE_BUFFER 0x08
187
188 #define INDIO_ALL_RING_MODES (INDIO_RING_TRIGGERED | INDIO_RING_HARDWARE_BUFFER)
189
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
195
196 struct iio_trigger; /* forward declaration */
197 struct iio_dev;
198
199 /**
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.
223 **/
224 struct iio_info {
225 struct module *driver_module;
226 struct attribute_group *event_attrs;
227 const struct attribute_group *attrs;
228
229 int (*read_raw)(struct iio_dev *indio_dev,
230 struct iio_chan_spec const *chan,
231 int *val,
232 int *val2,
233 long mask);
234
235 int (*write_raw)(struct iio_dev *indio_dev,
236 struct iio_chan_spec const *chan,
237 int val,
238 int val2,
239 long mask);
240
241 int (*write_raw_get_fmt)(struct iio_dev *indio_dev,
242 struct iio_chan_spec const *chan,
243 long mask);
244
245 int (*read_event_config)(struct iio_dev *indio_dev,
246 int event_code);
247
248 int (*write_event_config)(struct iio_dev *indio_dev,
249 int event_code,
250 int state);
251
252 int (*read_event_value)(struct iio_dev *indio_dev,
253 int event_code,
254 int *val);
255 int (*write_event_value)(struct iio_dev *indio_dev,
256 int event_code,
257 int val);
258 int (*validate_trigger)(struct iio_dev *indio_dev,
259 struct iio_trigger *trig);
260
261 };
262
263 /**
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
269 * and owner
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
273 * changes
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
280 * attributes
281 * @name: [DRIVER] name of the device.
282 * @info: [DRIVER] callbacks and constant info from driver
283 * @chrdev: [INTERN] associated character device
284 **/
285 struct iio_dev {
286 int id;
287
288 int modes;
289 int currentmode;
290 struct device dev;
291
292 struct iio_event_interface *event_interface;
293
294 struct iio_ring_buffer *ring;
295 struct mutex mlock;
296
297 u32 *available_scan_masks;
298 struct iio_trigger *trig;
299 struct iio_poll_func *pollfunc;
300
301 struct iio_chan_spec const *channels;
302 int num_channels;
303
304 struct list_head channel_attr_list;
305 const char *name;
306 const struct iio_info *info;
307 struct cdev chrdev;
308 };
309
310 /**
311 * iio_device_register() - register a device with the IIO subsystem
312 * @dev_info: Device structure filled by the device driver
313 **/
314 int iio_device_register(struct iio_dev *dev_info);
315
316 /**
317 * iio_device_unregister() - unregister a device from the IIO subsystem
318 * @dev_info: Device structure representing the device.
319 **/
320 void iio_device_unregister(struct iio_dev *dev_info);
321
322 /**
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
327 **/
328 int iio_push_event(struct iio_dev *dev_info, int ev_code, s64 timestamp);
329
330 extern struct bus_type iio_bus_type;
331
332 /**
333 * iio_put_device() - reference counted deallocation of struct device
334 * @dev: the iio_device containing the device
335 **/
336 static inline void iio_put_device(struct iio_dev *dev)
337 {
338 if (dev)
339 put_device(&dev->dev);
340 };
341
342 /* Can we make this smaller? */
343 #define IIO_ALIGN L1_CACHE_BYTES
344 /**
345 * iio_allocate_device() - allocate an iio_dev from a driver
346 * @sizeof_priv: Space to allocate for private structure.
347 **/
348 struct iio_dev *iio_allocate_device(int sizeof_priv);
349
350 static inline void *iio_priv(const struct iio_dev *dev)
351 {
352 return (char *)dev + ALIGN(sizeof(struct iio_dev), IIO_ALIGN);
353 }
354
355 static inline struct iio_dev *iio_priv_to_dev(void *priv)
356 {
357 return (struct iio_dev *)((char *)priv -
358 ALIGN(sizeof(struct iio_dev), IIO_ALIGN));
359 }
360
361 /**
362 * iio_free_device() - free an iio_dev from a driver
363 * @dev: the iio_dev associated with the device
364 **/
365 void iio_free_device(struct iio_dev *dev);
366
367 /**
368 * iio_ring_enabled() - helper function to test if any form of ring is enabled
369 * @dev_info: IIO device info structure for device
370 **/
371 static inline bool iio_ring_enabled(struct iio_dev *dev_info)
372 {
373 return dev_info->currentmode
374 & (INDIO_RING_TRIGGERED
375 | INDIO_RING_HARDWARE_BUFFER);
376 };
377
378 #endif /* _INDUSTRIAL_IO_H_ */