staging: iio: squash chrdev handler remains into users.
[deliverable/linux.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 #include <linux/irq.h>
17 #include "sysfs.h"
18
19 /* IIO TODO LIST */
20 /*
21 * Provide means of adjusting timer accuracy.
22 * Currently assumes nano seconds.
23 */
24
25 enum iio_chan_type {
26 /* real channel types */
27 IIO_IN,
28 IIO_OUT,
29 IIO_CURRENT,
30 IIO_POWER,
31 IIO_ACCEL,
32 IIO_IN_DIFF,
33 IIO_GYRO,
34 IIO_MAGN,
35 IIO_LIGHT,
36 IIO_INTENSITY,
37 IIO_PROXIMITY,
38 IIO_TEMP,
39 IIO_INCLI,
40 IIO_ROT,
41 IIO_ANGL,
42 IIO_TIMESTAMP,
43 };
44
45 #define IIO_MOD_X 0
46 #define IIO_MOD_LIGHT_BOTH 0
47 #define IIO_MOD_Y 1
48 #define IIO_MOD_LIGHT_IR 1
49 #define IIO_MOD_Z 2
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
58
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,
75 };
76
77 /**
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
86 * from a buffer.
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
91 * realbits.
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,
97 * unlike modifiers.
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
103 * the 'x' axis.
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.
108 */
109 struct iio_chan_spec {
110 enum iio_chan_type type;
111 int channel;
112 int channel2;
113 unsigned long address;
114 int scan_index;
115 struct {
116 char sign;
117 u8 realbits;
118 u8 storagebits;
119 u8 shift;
120 } scan_type;
121 long info_mask;
122 long event_mask;
123 char *extend_name;
124 unsigned processed_val:1;
125 unsigned modified:1;
126 unsigned indexed:1;
127 };
128
129 #define IIO_ST(si, rb, sb, sh) \
130 { .sign = si, .realbits = rb, .storagebits = sb, .shift = sh }
131
132 #define IIO_CHAN(_type, _mod, _indexed, _proc, _name, _chan, _chan2, \
133 _inf_mask, _address, _si, _stype, _event_mask) \
134 { .type = _type, \
135 .modified = _mod, \
136 .indexed = _indexed, \
137 .processed_val = _proc, \
138 .extend_name = _name, \
139 .channel = _chan, \
140 .channel2 = _chan2, \
141 .info_mask = _inf_mask, \
142 .address = _address, \
143 .scan_index = _si, \
144 .scan_type = _stype, \
145 .event_mask = _event_mask }
146
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) }
150
151 /**
152 * iio_get_time_ns() - utility function to get a time stamp for events etc
153 **/
154 static inline s64 iio_get_time_ns(void)
155 {
156 struct timespec ts;
157 /*
158 * calls getnstimeofday.
159 * If hrtimers then up to ns accurate, if not microsecond.
160 */
161 ktime_get_real_ts(&ts);
162
163 return timespec_to_ns(&ts);
164 }
165
166 /* Device operating modes */
167 #define INDIO_DIRECT_MODE 0x01
168 #define INDIO_RING_TRIGGERED 0x02
169 #define INDIO_RING_HARDWARE_BUFFER 0x08
170
171 #define INDIO_ALL_RING_MODES (INDIO_RING_TRIGGERED | INDIO_RING_HARDWARE_BUFFER)
172
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
178
179 struct iio_trigger; /* forward declaration */
180 struct iio_dev;
181
182 /**
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.
207 **/
208 struct iio_info {
209 struct module *driver_module;
210 int num_interrupt_lines;
211 struct attribute_group *event_attrs;
212 const struct attribute_group *attrs;
213
214 int (*read_raw)(struct iio_dev *indio_dev,
215 struct iio_chan_spec const *chan,
216 int *val,
217 int *val2,
218 long mask);
219
220 int (*write_raw)(struct iio_dev *indio_dev,
221 struct iio_chan_spec const *chan,
222 int val,
223 int val2,
224 long mask);
225
226 int (*write_raw_get_fmt)(struct iio_dev *indio_dev,
227 struct iio_chan_spec const *chan,
228 long mask);
229
230 int (*read_event_config)(struct iio_dev *indio_dev,
231 int event_code);
232
233 int (*write_event_config)(struct iio_dev *indio_dev,
234 int event_code,
235 int state);
236
237 int (*read_event_value)(struct iio_dev *indio_dev,
238 int event_code,
239 int *val);
240 int (*write_event_value)(struct iio_dev *indio_dev,
241 int event_code,
242 int val);
243 int (*validate_trigger)(struct iio_dev *indio_dev,
244 struct iio_trigger *trig);
245
246 };
247
248 int iio_event_getfd(struct iio_dev *indio_dev);
249 /**
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
255 * and owner
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
259 * changes
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
266 * attributes.
267 * @name: [DRIVER] name of the device.
268 **/
269 struct iio_dev {
270 int id;
271
272 int modes;
273 int currentmode;
274 struct device dev;
275
276 struct iio_event_interface *event_interfaces;
277
278 struct iio_ring_buffer *ring;
279 struct mutex mlock;
280
281 u32 *available_scan_masks;
282 struct iio_trigger *trig;
283 struct iio_poll_func *pollfunc;
284
285 struct iio_chan_spec const *channels;
286 int num_channels;
287
288 struct list_head channel_attr_list;
289 const char *name;
290 const struct iio_info *info;
291 };
292
293 /**
294 * iio_device_register() - register a device with the IIO subsystem
295 * @dev_info: Device structure filled by the device driver
296 **/
297 int iio_device_register(struct iio_dev *dev_info);
298
299 /**
300 * iio_device_unregister() - unregister a device from the IIO subsystem
301 * @dev_info: Device structure representing the device.
302 **/
303 void iio_device_unregister(struct iio_dev *dev_info);
304
305 /**
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
311 **/
312 int iio_push_event(struct iio_dev *dev_info,
313 int ev_line,
314 int ev_code,
315 s64 timestamp);
316
317 extern struct bus_type iio_bus_type;
318
319 /**
320 * iio_put_device() - reference counted deallocation of struct device
321 * @dev: the iio_device containing the device
322 **/
323 static inline void iio_put_device(struct iio_dev *dev)
324 {
325 if (dev)
326 put_device(&dev->dev);
327 };
328
329 /* Can we make this smaller? */
330 #define IIO_ALIGN L1_CACHE_BYTES
331 /**
332 * iio_allocate_device() - allocate an iio_dev from a driver
333 * @sizeof_priv: Space to allocate for private structure.
334 **/
335 struct iio_dev *iio_allocate_device(int sizeof_priv);
336
337 static inline void *iio_priv(const struct iio_dev *dev)
338 {
339 return (char *)dev + ALIGN(sizeof(struct iio_dev), IIO_ALIGN);
340 }
341
342 static inline struct iio_dev *iio_priv_to_dev(void *priv)
343 {
344 return (struct iio_dev *)((char *)priv -
345 ALIGN(sizeof(struct iio_dev), IIO_ALIGN));
346 }
347
348 /**
349 * iio_free_device() - free an iio_dev from a driver
350 * @dev: the iio_dev associated with the device
351 **/
352 void iio_free_device(struct iio_dev *dev);
353
354 /**
355 * iio_ring_enabled() - helper function to test if any form of ring is enabled
356 * @dev_info: IIO device info structure for device
357 **/
358 static inline bool iio_ring_enabled(struct iio_dev *dev_info)
359 {
360 return dev_info->currentmode
361 & (INDIO_RING_TRIGGERED
362 | INDIO_RING_HARDWARE_BUFFER);
363 };
364
365 #endif /* _INDUSTRIAL_IO_H_ */
This page took 0.049606 seconds and 5 git commands to generate.