9ca89c7d56930522b871d2b34bd00dc12013f7aa
[deliverable/linux.git] / drivers / staging / iio / iio.h
1 /* The industrial I/O core
2 *
3 * Copyright (c) 2008 Jonathan Cameron
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 as published by
7 * the Free Software Foundation.
8 */
9
10 #ifndef _INDUSTRIAL_IO_H_
11 #define _INDUSTRIAL_IO_H_
12
13 #include <linux/device.h>
14 #include <linux/cdev.h>
15 #include <linux/irq.h>
16 #include "sysfs.h"
17 #include "chrdev.h"
18
19 /* IIO TODO LIST */
20 /*
21 * Provide means of adjusting timer accuracy.
22 * Currently assumes nano seconds.
23 */
24
25 /* Event interface flags */
26 #define IIO_BUSY_BIT_POS 1
27
28 /* naughty temporary hack to match these against the event version
29 - need to flattern these together */
30 enum iio_chan_type {
31 /* real channel types */
32 IIO_IN,
33 IIO_CURRENT,
34 IIO_POWER,
35 IIO_ACCEL,
36 IIO_IN_DIFF,
37 IIO_GYRO,
38 IIO_MAGN,
39 IIO_LIGHT,
40 IIO_INTENSITY,
41 IIO_PROXIMITY,
42 IIO_TEMP,
43 IIO_INCLI,
44 IIO_ROT,
45 IIO_ANGL,
46 IIO_TIMESTAMP,
47 };
48
49 #define IIO_MOD_X 0
50 #define IIO_MOD_LIGHT_BOTH 0
51 #define IIO_MOD_Y 1
52 #define IIO_MOD_LIGHT_IR 1
53 #define IIO_MOD_Z 2
54 #define IIO_MOD_X_AND_Y 3
55 #define IIO_MOD_X_ANX_Z 4
56 #define IIO_MOD_Y_AND_Z 5
57 #define IIO_MOD_X_AND_Y_AND_Z 6
58 #define IIO_MOD_X_OR_Y 7
59 #define IIO_MOD_X_OR_Z 8
60 #define IIO_MOD_Y_OR_Z 9
61 #define IIO_MOD_X_OR_Y_OR_Z 10
62
63 /* Could add the raw attributes as well - allowing buffer only devices */
64 enum iio_chan_info_enum {
65 IIO_CHAN_INFO_SCALE_SHARED,
66 IIO_CHAN_INFO_SCALE_SEPARATE,
67 IIO_CHAN_INFO_OFFSET_SHARED,
68 IIO_CHAN_INFO_OFFSET_SEPARATE,
69 IIO_CHAN_INFO_CALIBSCALE_SHARED,
70 IIO_CHAN_INFO_CALIBSCALE_SEPARATE,
71 IIO_CHAN_INFO_CALIBBIAS_SHARED,
72 IIO_CHAN_INFO_CALIBBIAS_SEPARATE,
73 IIO_CHAN_INFO_PEAK_SHARED,
74 IIO_CHAN_INFO_PEAK_SEPARATE,
75 IIO_CHAN_INFO_PEAK_SCALE_SHARED,
76 IIO_CHAN_INFO_PEAK_SCALE_SEPARATE,
77 };
78
79 /**
80 * struct iio_chan_spec - specification of a single channel
81 * @type: What type of measurement is the channel making.
82 * @channel: What number or name do we wish to asign the channel.
83 * @channel2: If there is a second number for a differential
84 * channel then this is it. If modified is set then the
85 * value here specifies the modifier.
86 * @address: Driver specific identifier.
87 * @scan_index: Monotonic index to give ordering in scans when read
88 * from a buffer.
89 * @scan_type: Sign: 's' or 'u' to specify signed or unsigned
90 * realbits: Number of valid bits of data
91 * storage_bits: Realbits + padding
92 * shift: Shift right by this before masking out
93 * realbits.
94 * @info_mask: What information is to be exported about this channel.
95 * This includes calibbias, scale etc.
96 * @event_mask: What events can this channel produce.
97 * @extend_name: Allows labeling of channel attributes with an
98 * informative name. Note this has no effect codes etc,
99 * unlike modifiers.
100 * @processed_val: Flag to specify the data access attribute should be
101 * *_input rather than *_raw.
102 * @modified: Does a modifier apply to this channel. What these are
103 * depends on the channel type. Modifier is set in
104 * channel2. Examples are IIO_MOD_X for axial sensors about
105 * the 'x' axis.
106 * @indexed: Specify the channel has a numerical index. If not,
107 * the value in channel will be suppressed for attribute
108 * but not for event codes. Typically set it to 0 when
109 * the index is false.
110 */
111 struct iio_chan_spec {
112 enum iio_chan_type type;
113 int channel;
114 int channel2;
115 unsigned long address;
116 int scan_index;
117 struct {
118 char sign;
119 u8 realbits;
120 u8 storagebits;
121 u8 shift;
122 } scan_type;
123 const long info_mask;
124 const long event_mask;
125 const char *extend_name;
126 unsigned processed_val:1;
127 unsigned modified:1;
128 unsigned indexed:1;
129 };
130 /* Meant for internal use only */
131 void __iio_device_attr_deinit(struct device_attribute *dev_attr);
132 int __iio_device_attr_init(struct device_attribute *dev_attr,
133 const char *postfix,
134 struct iio_chan_spec const *chan,
135 ssize_t (*readfunc)(struct device *dev,
136 struct device_attribute *attr,
137 char *buf),
138 ssize_t (*writefunc)(struct device *dev,
139 struct device_attribute *attr,
140 const char *buf,
141 size_t len),
142 bool generic);
143 #define IIO_ST(si, rb, sb, sh) \
144 { .sign = si, .realbits = rb, .storagebits = sb, .shift = sh }
145
146 #define IIO_CHAN(_type, _mod, _indexed, _proc, _name, _chan, _chan2, \
147 _inf_mask, _address, _si, _stype, _event_mask) \
148 { .type = _type, \
149 .modified = _mod, \
150 .indexed = _indexed, \
151 .processed_val = _proc, \
152 .extend_name = _name, \
153 .channel = _chan, \
154 .channel2 = _chan2, \
155 .info_mask = _inf_mask, \
156 .address = _address, \
157 .scan_index = _si, \
158 .scan_type = _stype, \
159 .event_mask = _event_mask }
160
161 #define IIO_CHAN_SOFT_TIMESTAMP(_si) \
162 { .type = IIO_TIMESTAMP, .channel = -1, \
163 .scan_index = _si, .scan_type = IIO_ST('s', 64, 64, 0) }
164
165 int __iio_add_chan_devattr(const char *postfix,
166 const char *group,
167 struct iio_chan_spec const *chan,
168 ssize_t (*func)(struct device *dev,
169 struct device_attribute *attr,
170 char *buf),
171 ssize_t (*writefunc)(struct device *dev,
172 struct device_attribute *attr,
173 const char *buf,
174 size_t len),
175 int mask,
176 bool generic,
177 struct device *dev,
178 struct list_head *attr_list);
179 /**
180 * iio_get_time_ns() - utility function to get a time stamp for events etc
181 **/
182 static inline s64 iio_get_time_ns(void)
183 {
184 struct timespec ts;
185 /*
186 * calls getnstimeofday.
187 * If hrtimers then up to ns accurate, if not microsecond.
188 */
189 ktime_get_real_ts(&ts);
190
191 return timespec_to_ns(&ts);
192 }
193
194 /* Device operating modes */
195 #define INDIO_DIRECT_MODE 0x01
196 #define INDIO_RING_TRIGGERED 0x02
197 #define INDIO_RING_HARDWARE_BUFFER 0x08
198
199 #define INDIO_ALL_RING_MODES (INDIO_RING_TRIGGERED | INDIO_RING_HARDWARE_BUFFER)
200
201 /* Vast majority of this is set by the industrialio subsystem on a
202 * call to iio_device_register. */
203 #define IIO_VAL_INT 1
204 #define IIO_VAL_INT_PLUS_MICRO 2
205 #define IIO_VAL_INT_PLUS_NANO 3
206
207 struct iio_trigger; /* forward declaration */
208
209 /**
210 * struct iio_info - constant information about device
211 * @driver_module: module structure used to ensure correct
212 * ownership of chrdevs etc
213 * @num_interrupt_lines:number of physical interrupt lines from device
214 * @event_attrs: event control attributes
215 * @attrs: general purpose device attributes
216 * @read_raw: function to request a value from the device.
217 * mask specifies which value. Note 0 means a reading of
218 * the channel in question. Return value will specify the
219 * type of value returned by the device. val and val2 will
220 * contain the elements making up the returned value.
221 * @write_raw: function to write a value to the device.
222 * Parameters are the same as for read_raw.
223 * @write_raw_get_fmt: callback function to query the expected
224 * format/precision. If not set by the driver, write_raw
225 * returns IIO_VAL_INT_PLUS_MICRO.
226 * @read_event_config: find out if the event is enabled.
227 * @write_event_config: set if the event is enabled.
228 * @read_event_value: read a value associated with the event. Meaning
229 * is event dependant. event_code specifies which event.
230 * @write_event_value: write the value associate with the event.
231 * Meaning is event dependent.
232 * @validate_trigger: function to validate the trigger when the
233 * current trigger gets changed.
234 **/
235 struct iio_info {
236 struct module *driver_module;
237 int num_interrupt_lines;
238 struct attribute_group *event_attrs;
239 const struct attribute_group *attrs;
240
241 int (*read_raw)(struct iio_dev *indio_dev,
242 struct iio_chan_spec const *chan,
243 int *val,
244 int *val2,
245 long mask);
246
247 int (*write_raw)(struct iio_dev *indio_dev,
248 struct iio_chan_spec const *chan,
249 int val,
250 int val2,
251 long mask);
252
253 int (*write_raw_get_fmt)(struct iio_dev *indio_dev,
254 struct iio_chan_spec const *chan,
255 long mask);
256
257 int (*read_event_config)(struct iio_dev *indio_dev,
258 int event_code);
259
260 int (*write_event_config)(struct iio_dev *indio_dev,
261 int event_code,
262 int state);
263
264 int (*read_event_value)(struct iio_dev *indio_dev,
265 int event_code,
266 int *val);
267 int (*write_event_value)(struct iio_dev *indio_dev,
268 int event_code,
269 int val);
270 int (*validate_trigger)(struct iio_dev *indio_dev,
271 struct iio_trigger *trig);
272
273 };
274
275 /**
276 * struct iio_dev - industrial I/O device
277 * @id: [INTERN] used to identify device internally
278 * @dev_data: [DRIVER] device specific data
279 * @modes: [DRIVER] operating modes supported by device
280 * @currentmode: [DRIVER] current operating mode
281 * @dev: [DRIVER] device structure, should be assigned a parent
282 * and owner
283 * @event_interfaces: [INTERN] event chrdevs associated with interrupt lines
284 * @ring: [DRIVER] any ring buffer present
285 * @mlock: [INTERN] lock used to prevent simultaneous device state
286 * changes
287 * @available_scan_masks: [DRIVER] optional array of allowed bitmasks
288 * @trig: [INTERN] current device trigger (ring buffer modes)
289 * @pollfunc: [DRIVER] function run on trigger being received
290 * @channels: [DRIVER] channel specification structure table
291 * @num_channels: [DRIVER] number of chanels specified in @channels.
292 * @channel_attr_list: [INTERN] keep track of automatically created channel
293 * attributes.
294 * @name: [DRIVER] name of the device.
295 **/
296 struct iio_dev {
297 int id;
298 void *dev_data;
299 int modes;
300 int currentmode;
301 struct device dev;
302
303 struct iio_event_interface *event_interfaces;
304
305 struct iio_ring_buffer *ring;
306 struct mutex mlock;
307
308 u32 *available_scan_masks;
309 struct iio_trigger *trig;
310 struct iio_poll_func *pollfunc;
311
312 struct iio_chan_spec const *channels;
313 int num_channels;
314
315 struct list_head channel_attr_list;
316 const char *name;
317 const struct iio_info *info;
318 };
319
320 /**
321 * iio_device_register() - register a device with the IIO subsystem
322 * @dev_info: Device structure filled by the device driver
323 **/
324 int iio_device_register(struct iio_dev *dev_info);
325
326 /**
327 * iio_device_unregister() - unregister a device from the IIO subsystem
328 * @dev_info: Device structure representing the device.
329 **/
330 void iio_device_unregister(struct iio_dev *dev_info);
331
332 /**
333 * iio_push_event() - try to add event to the list for userspace reading
334 * @dev_info: IIO device structure
335 * @ev_line: Which event line (hardware interrupt)
336 * @ev_code: What event
337 * @timestamp: When the event occurred
338 **/
339 int iio_push_event(struct iio_dev *dev_info,
340 int ev_line,
341 int ev_code,
342 s64 timestamp);
343
344 /* Used to distinguish between bipolar and unipolar scan elemenents.
345 * Whilst this may seem obvious, we may well want to change the representation
346 * in the future!*/
347 #define IIO_SIGNED(a) -(a)
348 #define IIO_UNSIGNED(a) (a)
349
350 extern dev_t iio_devt;
351 extern struct bus_type iio_bus_type;
352
353 /**
354 * iio_put_device() - reference counted deallocation of struct device
355 * @dev: the iio_device containing the device
356 **/
357 static inline void iio_put_device(struct iio_dev *dev)
358 {
359 if (dev)
360 put_device(&dev->dev);
361 };
362
363 /**
364 * to_iio_dev() - get iio_dev for which we have the struct device
365 * @d: the struct device
366 **/
367 static inline struct iio_dev *to_iio_dev(struct device *d)
368 {
369 return container_of(d, struct iio_dev, dev);
370 };
371
372 /**
373 * iio_dev_get_devdata() - helper function gets device specific data
374 * @d: the iio_dev associated with the device
375 **/
376 static inline void *iio_dev_get_devdata(struct iio_dev *d)
377 {
378 return d->dev_data;
379 }
380
381
382 /* Can we make this smaller? */
383 #define IIO_ALIGN L1_CACHE_BYTES
384 /**
385 * iio_allocate_device() - allocate an iio_dev from a driver
386 * @sizeof_priv: Space to allocate for private structure.
387 **/
388 struct iio_dev *iio_allocate_device(int sizeof_priv);
389
390 static inline void *iio_priv(const struct iio_dev *dev)
391 {
392 return (char *)dev + ALIGN(sizeof(struct iio_dev), IIO_ALIGN);
393 }
394
395 static inline struct iio_dev *iio_priv_to_dev(void *priv)
396 {
397 return (struct iio_dev *)((char *)priv -
398 ALIGN(sizeof(struct iio_dev), IIO_ALIGN));
399 }
400
401 /**
402 * iio_free_device() - free an iio_dev from a driver
403 * @dev: the iio_dev associated with the device
404 **/
405 void iio_free_device(struct iio_dev *dev);
406
407 /**
408 * iio_put() - internal module reference count reduce
409 **/
410 void iio_put(void);
411
412 /**
413 * iio_get() - internal module reference count increase
414 **/
415 void iio_get(void);
416
417 /**
418 * iio_device_get_chrdev_minor() - get an unused minor number
419 **/
420 int iio_device_get_chrdev_minor(void);
421 void iio_device_free_chrdev_minor(int val);
422
423 /**
424 * iio_ring_enabled() - helper function to test if any form of ring is enabled
425 * @dev_info: IIO device info structure for device
426 **/
427 static inline bool iio_ring_enabled(struct iio_dev *dev_info)
428 {
429 return dev_info->currentmode
430 & (INDIO_RING_TRIGGERED
431 | INDIO_RING_HARDWARE_BUFFER);
432 };
433
434 struct ida;
435
436 int iio_get_new_ida_val(struct ida *this_ida);
437 void iio_free_ida_val(struct ida *this_ida, int id);
438 #endif /* _INDUSTRIAL_IO_H_ */
This page took 0.039865 seconds and 4 git commands to generate.