staging:iio: rework of attribute registration.
[deliverable/linux.git] / drivers / staging / iio / iio.h
CommitLineData
7d438178 1
847ec80b
JC
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>
847ec80b
JC
16
17/* IIO TODO LIST */
751a3700 18/*
847ec80b
JC
19 * Provide means of adjusting timer accuracy.
20 * Currently assumes nano seconds.
21 */
22
1e8dfcc6
JC
23enum iio_data_type {
24 IIO_RAW,
25 IIO_PROCESSED,
26};
27
1d892719 28enum iio_chan_type {
1d892719 29 /* real channel types */
c6fc8062 30 IIO_VOLTAGE,
faf290e8
MH
31 IIO_CURRENT,
32 IIO_POWER,
1d892719 33 IIO_ACCEL,
c6fc8062 34 IIO_VOLTAGE_DIFF,
1d892719
JC
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
c6fc8062
JC
47/* Nasty hack to avoid massive churn */
48#define IIO_IN IIO_VOLTAGE
49#define IIO_IN_DIFF IIO_VOLTAGE_DIFF
50
330c6c57
JC
51enum iio_modifier {
52 IIO_NO_MOD,
53 IIO_MOD_X,
54 IIO_MOD_Y,
55 IIO_MOD_Z,
56 IIO_MOD_X_AND_Y,
57 IIO_MOD_X_ANX_Z,
58 IIO_MOD_Y_AND_Z,
59 IIO_MOD_X_AND_Y_AND_Z,
60 IIO_MOD_X_OR_Y,
61 IIO_MOD_X_OR_Z,
62 IIO_MOD_Y_OR_Z,
63 IIO_MOD_X_OR_Y_OR_Z,
64 IIO_MOD_LIGHT_BOTH,
65 IIO_MOD_LIGHT_IR,
66};
1d892719
JC
67
68/* Could add the raw attributes as well - allowing buffer only devices */
69enum iio_chan_info_enum {
70 IIO_CHAN_INFO_SCALE_SHARED,
71 IIO_CHAN_INFO_SCALE_SEPARATE,
72 IIO_CHAN_INFO_OFFSET_SHARED,
73 IIO_CHAN_INFO_OFFSET_SEPARATE,
74 IIO_CHAN_INFO_CALIBSCALE_SHARED,
75 IIO_CHAN_INFO_CALIBSCALE_SEPARATE,
76 IIO_CHAN_INFO_CALIBBIAS_SHARED,
eb7fea53
JC
77 IIO_CHAN_INFO_CALIBBIAS_SEPARATE,
78 IIO_CHAN_INFO_PEAK_SHARED,
79 IIO_CHAN_INFO_PEAK_SEPARATE,
80 IIO_CHAN_INFO_PEAK_SCALE_SHARED,
81 IIO_CHAN_INFO_PEAK_SCALE_SEPARATE,
7d438178
JC
82 IIO_CHAN_INFO_QUADRATURE_CORRECTION_RAW_SHARED,
83 IIO_CHAN_INFO_QUADRATURE_CORRECTION_RAW_SEPARATE,
1d892719
JC
84};
85
8310b86c
JC
86enum iio_endian {
87 IIO_CPU,
88 IIO_BE,
89 IIO_LE,
90};
91
1d892719
JC
92/**
93 * struct iio_chan_spec - specification of a single channel
94 * @type: What type of measurement is the channel making.
95 * @channel: What number or name do we wish to asign the channel.
96 * @channel2: If there is a second number for a differential
97 * channel then this is it. If modified is set then the
98 * value here specifies the modifier.
99 * @address: Driver specific identifier.
100 * @scan_index: Monotonic index to give ordering in scans when read
101 * from a buffer.
102 * @scan_type: Sign: 's' or 'u' to specify signed or unsigned
103 * realbits: Number of valid bits of data
104 * storage_bits: Realbits + padding
105 * shift: Shift right by this before masking out
106 * realbits.
8310b86c 107 * endianness: little or big endian
1d892719
JC
108 * @info_mask: What information is to be exported about this channel.
109 * This includes calibbias, scale etc.
110 * @event_mask: What events can this channel produce.
111 * @extend_name: Allows labeling of channel attributes with an
112 * informative name. Note this has no effect codes etc,
113 * unlike modifiers.
114 * @processed_val: Flag to specify the data access attribute should be
115 * *_input rather than *_raw.
116 * @modified: Does a modifier apply to this channel. What these are
117 * depends on the channel type. Modifier is set in
118 * channel2. Examples are IIO_MOD_X for axial sensors about
119 * the 'x' axis.
120 * @indexed: Specify the channel has a numerical index. If not,
121 * the value in channel will be suppressed for attribute
122 * but not for event codes. Typically set it to 0 when
123 * the index is false.
1d892719
JC
124 */
125struct iio_chan_spec {
126 enum iio_chan_type type;
127 int channel;
128 int channel2;
129 unsigned long address;
130 int scan_index;
131 struct {
132 char sign;
133 u8 realbits;
134 u8 storagebits;
135 u8 shift;
8310b86c 136 enum iio_endian endianness;
1d892719 137 } scan_type;
1c5e6a3f
MH
138 long info_mask;
139 long event_mask;
140 char *extend_name;
1d892719
JC
141 unsigned processed_val:1;
142 unsigned modified:1;
143 unsigned indexed:1;
c6fc8062 144 unsigned output:1;
1d892719 145};
df9c1c42 146
1d892719
JC
147#define IIO_ST(si, rb, sb, sh) \
148 { .sign = si, .realbits = rb, .storagebits = sb, .shift = sh }
149
c6fc8062
JC
150/* Macro assumes input channels */
151#define IIO_CHAN(_type, _mod, _indexed, _proc, _name, _chan, _chan2, \
aaf370db 152 _inf_mask, _address, _si, _stype, _event_mask) \
1d892719 153 { .type = _type, \
c6fc8062 154 .output = 0, \
1d892719
JC
155 .modified = _mod, \
156 .indexed = _indexed, \
157 .processed_val = _proc, \
158 .extend_name = _name, \
159 .channel = _chan, \
160 .channel2 = _chan2, \
161 .info_mask = _inf_mask, \
162 .address = _address, \
163 .scan_index = _si, \
164 .scan_type = _stype, \
aaf370db 165 .event_mask = _event_mask }
1d892719
JC
166
167#define IIO_CHAN_SOFT_TIMESTAMP(_si) \
168 { .type = IIO_TIMESTAMP, .channel = -1, \
169 .scan_index = _si, .scan_type = IIO_ST('s', 64, 64, 0) }
170
847ec80b
JC
171/**
172 * iio_get_time_ns() - utility function to get a time stamp for events etc
173 **/
174static inline s64 iio_get_time_ns(void)
175{
176 struct timespec ts;
177 /*
178 * calls getnstimeofday.
179 * If hrtimers then up to ns accurate, if not microsecond.
180 */
181 ktime_get_real_ts(&ts);
182
183 return timespec_to_ns(&ts);
184}
185
847ec80b
JC
186/* Device operating modes */
187#define INDIO_DIRECT_MODE 0x01
188#define INDIO_RING_TRIGGERED 0x02
189#define INDIO_RING_HARDWARE_BUFFER 0x08
190
191#define INDIO_ALL_RING_MODES (INDIO_RING_TRIGGERED | INDIO_RING_HARDWARE_BUFFER)
192
193/* Vast majority of this is set by the industrialio subsystem on a
194 * call to iio_device_register. */
1d892719
JC
195#define IIO_VAL_INT 1
196#define IIO_VAL_INT_PLUS_MICRO 2
71646e2c 197#define IIO_VAL_INT_PLUS_NANO 3
6fe8135f 198
43a4360e 199struct iio_trigger; /* forward declaration */
7ae8cf62 200struct iio_dev;
43a4360e 201
6fe8135f
JC
202/**
203 * struct iio_info - constant information about device
204 * @driver_module: module structure used to ensure correct
205 * ownership of chrdevs etc
6fe8135f
JC
206 * @event_attrs: event control attributes
207 * @attrs: general purpose device attributes
208 * @read_raw: function to request a value from the device.
209 * mask specifies which value. Note 0 means a reading of
210 * the channel in question. Return value will specify the
211 * type of value returned by the device. val and val2 will
212 * contain the elements making up the returned value.
213 * @write_raw: function to write a value to the device.
214 * Parameters are the same as for read_raw.
5c04af04
MH
215 * @write_raw_get_fmt: callback function to query the expected
216 * format/precision. If not set by the driver, write_raw
217 * returns IIO_VAL_INT_PLUS_MICRO.
6fe8135f
JC
218 * @read_event_config: find out if the event is enabled.
219 * @write_event_config: set if the event is enabled.
220 * @read_event_value: read a value associated with the event. Meaning
221 * is event dependant. event_code specifies which event.
222 * @write_event_value: write the value associate with the event.
223 * Meaning is event dependent.
43a4360e
MH
224 * @validate_trigger: function to validate the trigger when the
225 * current trigger gets changed.
6fe8135f
JC
226 **/
227struct iio_info {
228 struct module *driver_module;
6fe8135f
JC
229 struct attribute_group *event_attrs;
230 const struct attribute_group *attrs;
231
232 int (*read_raw)(struct iio_dev *indio_dev,
233 struct iio_chan_spec const *chan,
234 int *val,
235 int *val2,
236 long mask);
237
238 int (*write_raw)(struct iio_dev *indio_dev,
239 struct iio_chan_spec const *chan,
240 int val,
241 int val2,
242 long mask);
243
5c04af04
MH
244 int (*write_raw_get_fmt)(struct iio_dev *indio_dev,
245 struct iio_chan_spec const *chan,
246 long mask);
247
6fe8135f 248 int (*read_event_config)(struct iio_dev *indio_dev,
330c6c57 249 u64 event_code);
6fe8135f
JC
250
251 int (*write_event_config)(struct iio_dev *indio_dev,
330c6c57 252 u64 event_code,
6fe8135f
JC
253 int state);
254
255 int (*read_event_value)(struct iio_dev *indio_dev,
330c6c57 256 u64 event_code,
6fe8135f
JC
257 int *val);
258 int (*write_event_value)(struct iio_dev *indio_dev,
330c6c57 259 u64 event_code,
6fe8135f 260 int val);
43a4360e
MH
261 int (*validate_trigger)(struct iio_dev *indio_dev,
262 struct iio_trigger *trig);
263
6fe8135f
JC
264};
265
847ec80b
JC
266/**
267 * struct iio_dev - industrial I/O device
268 * @id: [INTERN] used to identify device internally
847ec80b
JC
269 * @modes: [DRIVER] operating modes supported by device
270 * @currentmode: [DRIVER] current operating mode
271 * @dev: [DRIVER] device structure, should be assigned a parent
272 * and owner
5aa96188 273 * @event_interface: [INTERN] event chrdevs associated with interrupt lines
847ec80b
JC
274 * @ring: [DRIVER] any ring buffer present
275 * @mlock: [INTERN] lock used to prevent simultaneous device state
276 * changes
e5c003ae 277 * @available_scan_masks: [DRIVER] optional array of allowed bitmasks
32b5eeca
JC
278 * @masklength: [INTERN] the length of the mask established from
279 * channels
847ec80b 280 * @trig: [INTERN] current device trigger (ring buffer modes)
25985edc 281 * @pollfunc: [DRIVER] function run on trigger being received
1d892719
JC
282 * @channels: [DRIVER] channel specification structure table
283 * @num_channels: [DRIVER] number of chanels specified in @channels.
284 * @channel_attr_list: [INTERN] keep track of automatically created channel
1a25e592 285 * attributes
26d25ae3 286 * @chan_attr_group: [INTERN] group for all attrs in base directory
1d892719 287 * @name: [DRIVER] name of the device.
1a25e592
JC
288 * @info: [DRIVER] callbacks and constant info from driver
289 * @chrdev: [INTERN] associated character device
26d25ae3
JC
290 * @groups: [INTERN] attribute groups
291 * @groupcounter: [INTERN] index of next attribute group
847ec80b
JC
292 **/
293struct iio_dev {
294 int id;
4024bc73 295
847ec80b
JC
296 int modes;
297 int currentmode;
298 struct device dev;
847ec80b 299
5aa96188 300 struct iio_event_interface *event_interface;
847ec80b
JC
301
302 struct iio_ring_buffer *ring;
303 struct mutex mlock;
304
32b5eeca
JC
305 unsigned long *available_scan_masks;
306 unsigned masklength;
847ec80b
JC
307 struct iio_trigger *trig;
308 struct iio_poll_func *pollfunc;
1d892719 309
1a25e592
JC
310 struct iio_chan_spec const *channels;
311 int num_channels;
1d892719 312
1a25e592 313 struct list_head channel_attr_list;
26d25ae3 314 struct attribute_group chan_attr_group;
1a25e592
JC
315 const char *name;
316 const struct iio_info *info;
317 struct cdev chrdev;
26d25ae3
JC
318#define IIO_MAX_GROUPS 6
319 const struct attribute_group *groups[IIO_MAX_GROUPS + 1];
320 int groupcounter;
847ec80b
JC
321};
322
847ec80b
JC
323/**
324 * iio_device_register() - register a device with the IIO subsystem
325 * @dev_info: Device structure filled by the device driver
326 **/
327int iio_device_register(struct iio_dev *dev_info);
328
329/**
330 * iio_device_unregister() - unregister a device from the IIO subsystem
331 * @dev_info: Device structure representing the device.
332 **/
333void iio_device_unregister(struct iio_dev *dev_info);
334
847ec80b
JC
335/**
336 * iio_push_event() - try to add event to the list for userspace reading
337 * @dev_info: IIO device structure
847ec80b 338 * @ev_code: What event
4c572605 339 * @timestamp: When the event occurred
847ec80b 340 **/
330c6c57 341int iio_push_event(struct iio_dev *dev_info, u64 ev_code, s64 timestamp);
847ec80b 342
5aaaeba8 343extern struct bus_type iio_bus_type;
847ec80b
JC
344
345/**
4c572605 346 * iio_put_device() - reference counted deallocation of struct device
847ec80b
JC
347 * @dev: the iio_device containing the device
348 **/
349static inline void iio_put_device(struct iio_dev *dev)
350{
351 if (dev)
352 put_device(&dev->dev);
353};
354
6f7c8ee5
JC
355/* Can we make this smaller? */
356#define IIO_ALIGN L1_CACHE_BYTES
847ec80b
JC
357/**
358 * iio_allocate_device() - allocate an iio_dev from a driver
6f7c8ee5 359 * @sizeof_priv: Space to allocate for private structure.
847ec80b 360 **/
6f7c8ee5
JC
361struct iio_dev *iio_allocate_device(int sizeof_priv);
362
363static inline void *iio_priv(const struct iio_dev *dev)
364{
365 return (char *)dev + ALIGN(sizeof(struct iio_dev), IIO_ALIGN);
366}
367
368static inline struct iio_dev *iio_priv_to_dev(void *priv)
369{
370 return (struct iio_dev *)((char *)priv -
371 ALIGN(sizeof(struct iio_dev), IIO_ALIGN));
372}
847ec80b
JC
373
374/**
375 * iio_free_device() - free an iio_dev from a driver
4c572605 376 * @dev: the iio_dev associated with the device
847ec80b
JC
377 **/
378void iio_free_device(struct iio_dev *dev);
379
847ec80b 380/**
4c572605
RD
381 * iio_ring_enabled() - helper function to test if any form of ring is enabled
382 * @dev_info: IIO device info structure for device
847ec80b
JC
383 **/
384static inline bool iio_ring_enabled(struct iio_dev *dev_info)
385{
386 return dev_info->currentmode
387 & (INDIO_RING_TRIGGERED
388 | INDIO_RING_HARDWARE_BUFFER);
389};
390
847ec80b 391#endif /* _INDUSTRIAL_IO_H_ */
This page took 0.262449 seconds and 5 git commands to generate.