1 /* The industrial I/O core
3 * Copyright (c) 2008 Jonathan Cameron
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.
10 #ifndef _INDUSTRIAL_IO_H_
11 #define _INDUSTRIAL_IO_H_
13 #include <linux/device.h>
14 #include <linux/cdev.h>
15 #include <linux/irq.h>
21 * Provide means of adjusting timer accuracy.
22 * Currently assumes nano seconds.
25 /* Event interface flags */
26 #define IIO_BUSY_BIT_POS 1
28 /* naughty temporary hack to match these against the event version
29 - need to flattern these together */
31 /* real channel types */
50 #define IIO_MOD_LIGHT_BOTH 0
52 #define IIO_MOD_LIGHT_IR 1
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
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
,
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
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
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,
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
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.
111 struct iio_chan_spec
{
112 enum iio_chan_type type
;
115 unsigned long address
;
123 const long info_mask
;
124 const long event_mask
;
125 const char *extend_name
;
126 unsigned processed_val
:1;
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
,
134 struct iio_chan_spec
const *chan
,
135 ssize_t (*readfunc
)(struct device
*dev
,
136 struct device_attribute
*attr
,
138 ssize_t (*writefunc
)(struct device
*dev
,
139 struct device_attribute
*attr
,
143 #define IIO_ST(si, rb, sb, sh) \
144 { .sign = si, .realbits = rb, .storagebits = sb, .shift = sh }
146 #define IIO_CHAN(_type, _mod, _indexed, _proc, _name, _chan, _chan2, \
147 _inf_mask, _address, _si, _stype, _event_mask) \
150 .indexed = _indexed, \
151 .processed_val = _proc, \
152 .extend_name = _name, \
154 .channel2 = _chan2, \
155 .info_mask = _inf_mask, \
156 .address = _address, \
158 .scan_type = _stype, \
159 .event_mask = _event_mask }
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) }
165 int __iio_add_chan_devattr(const char *postfix
,
167 struct iio_chan_spec
const *chan
,
168 ssize_t (*func
)(struct device
*dev
,
169 struct device_attribute
*attr
,
171 ssize_t (*writefunc
)(struct device
*dev
,
172 struct device_attribute
*attr
,
178 struct list_head
*attr_list
);
180 * iio_get_time_ns() - utility function to get a time stamp for events etc
182 static inline s64
iio_get_time_ns(void)
186 * calls getnstimeofday.
187 * If hrtimers then up to ns accurate, if not microsecond.
189 ktime_get_real_ts(&ts
);
191 return timespec_to_ns(&ts
);
194 /* Device operating modes */
195 #define INDIO_DIRECT_MODE 0x01
196 #define INDIO_RING_TRIGGERED 0x02
197 #define INDIO_RING_HARDWARE_BUFFER 0x08
199 #define INDIO_ALL_RING_MODES (INDIO_RING_TRIGGERED | INDIO_RING_HARDWARE_BUFFER)
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
207 struct iio_trigger
; /* forward declaration */
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 * @read_event_config: find out if the event is enabled.
224 * @write_event_config: set if the event is enabled.
225 * @read_event_value: read a value associated with the event. Meaning
226 * is event dependant. event_code specifies which event.
227 * @write_event_value: write the value associate with the event.
228 * Meaning is event dependent.
229 * @validate_trigger: function to validate the trigger when the
230 * current trigger gets changed.
233 struct module
*driver_module
;
234 int num_interrupt_lines
;
235 struct attribute_group
*event_attrs
;
236 const struct attribute_group
*attrs
;
238 int (*read_raw
)(struct iio_dev
*indio_dev
,
239 struct iio_chan_spec
const *chan
,
244 int (*write_raw
)(struct iio_dev
*indio_dev
,
245 struct iio_chan_spec
const *chan
,
250 int (*read_event_config
)(struct iio_dev
*indio_dev
,
253 int (*write_event_config
)(struct iio_dev
*indio_dev
,
257 int (*read_event_value
)(struct iio_dev
*indio_dev
,
260 int (*write_event_value
)(struct iio_dev
*indio_dev
,
263 int (*validate_trigger
)(struct iio_dev
*indio_dev
,
264 struct iio_trigger
*trig
);
269 * struct iio_dev - industrial I/O device
270 * @id: [INTERN] used to identify device internally
271 * @dev_data: [DRIVER] device specific data
272 * @modes: [DRIVER] operating modes supported by device
273 * @currentmode: [DRIVER] current operating mode
274 * @dev: [DRIVER] device structure, should be assigned a parent
276 * @event_interfaces: [INTERN] event chrdevs associated with interrupt lines
277 * @ring: [DRIVER] any ring buffer present
278 * @mlock: [INTERN] lock used to prevent simultaneous device state
280 * @available_scan_masks: [DRIVER] optional array of allowed bitmasks
281 * @trig: [INTERN] current device trigger (ring buffer modes)
282 * @pollfunc: [DRIVER] function run on trigger being received
283 * @channels: [DRIVER] channel specification structure table
284 * @num_channels: [DRIVER] number of chanels specified in @channels.
285 * @channel_attr_list: [INTERN] keep track of automatically created channel
287 * @name: [DRIVER] name of the device.
296 struct iio_event_interface
*event_interfaces
;
298 struct iio_ring_buffer
*ring
;
301 u32
*available_scan_masks
;
302 struct iio_trigger
*trig
;
303 struct iio_poll_func
*pollfunc
;
305 struct iio_chan_spec
const *channels
;
308 struct list_head channel_attr_list
;
310 const struct iio_info
*info
;
314 * iio_device_register() - register a device with the IIO subsystem
315 * @dev_info: Device structure filled by the device driver
317 int iio_device_register(struct iio_dev
*dev_info
);
320 * iio_device_unregister() - unregister a device from the IIO subsystem
321 * @dev_info: Device structure representing the device.
323 void iio_device_unregister(struct iio_dev
*dev_info
);
326 * iio_push_event() - try to add event to the list for userspace reading
327 * @dev_info: IIO device structure
328 * @ev_line: Which event line (hardware interrupt)
329 * @ev_code: What event
330 * @timestamp: When the event occurred
332 int iio_push_event(struct iio_dev
*dev_info
,
337 /* Used to distinguish between bipolar and unipolar scan elemenents.
338 * Whilst this may seem obvious, we may well want to change the representation
340 #define IIO_SIGNED(a) -(a)
341 #define IIO_UNSIGNED(a) (a)
343 extern dev_t iio_devt
;
344 extern struct bus_type iio_bus_type
;
347 * iio_put_device() - reference counted deallocation of struct device
348 * @dev: the iio_device containing the device
350 static inline void iio_put_device(struct iio_dev
*dev
)
353 put_device(&dev
->dev
);
357 * to_iio_dev() - get iio_dev for which we have the struct device
358 * @d: the struct device
360 static inline struct iio_dev
*to_iio_dev(struct device
*d
)
362 return container_of(d
, struct iio_dev
, dev
);
366 * iio_dev_get_devdata() - helper function gets device specific data
367 * @d: the iio_dev associated with the device
369 static inline void *iio_dev_get_devdata(struct iio_dev
*d
)
375 /* Can we make this smaller? */
376 #define IIO_ALIGN L1_CACHE_BYTES
378 * iio_allocate_device() - allocate an iio_dev from a driver
379 * @sizeof_priv: Space to allocate for private structure.
381 struct iio_dev
*iio_allocate_device(int sizeof_priv
);
383 static inline void *iio_priv(const struct iio_dev
*dev
)
385 return (char *)dev
+ ALIGN(sizeof(struct iio_dev
), IIO_ALIGN
);
388 static inline struct iio_dev
*iio_priv_to_dev(void *priv
)
390 return (struct iio_dev
*)((char *)priv
-
391 ALIGN(sizeof(struct iio_dev
), IIO_ALIGN
));
395 * iio_free_device() - free an iio_dev from a driver
396 * @dev: the iio_dev associated with the device
398 void iio_free_device(struct iio_dev
*dev
);
401 * iio_put() - internal module reference count reduce
406 * iio_get() - internal module reference count increase
411 * iio_device_get_chrdev_minor() - get an unused minor number
413 int iio_device_get_chrdev_minor(void);
414 void iio_device_free_chrdev_minor(int val
);
417 * iio_ring_enabled() - helper function to test if any form of ring is enabled
418 * @dev_info: IIO device info structure for device
420 static inline bool iio_ring_enabled(struct iio_dev
*dev_info
)
422 return dev_info
->currentmode
423 & (INDIO_RING_TRIGGERED
424 | INDIO_RING_HARDWARE_BUFFER
);
429 int iio_get_new_ida_val(struct ida
*this_ida
);
430 void iio_free_ida_val(struct ida
*this_ida
, int id
);
431 #endif /* _INDUSTRIAL_IO_H_ */