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
207 * struct iio_info - constant information about device
208 * @driver_module: module structure used to ensure correct
209 * ownership of chrdevs etc
210 * @num_interrupt_lines:number of physical interrupt lines from device
211 * @event_attrs: event control attributes
212 * @attrs: general purpose device attributes
213 * @read_raw: function to request a value from the device.
214 * mask specifies which value. Note 0 means a reading of
215 * the channel in question. Return value will specify the
216 * type of value returned by the device. val and val2 will
217 * contain the elements making up the returned value.
218 * @write_raw: function to write a value to the device.
219 * Parameters are the same as for read_raw.
220 * @read_event_config: find out if the event is enabled.
221 * @write_event_config: set if the event is enabled.
222 * @read_event_value: read a value associated with the event. Meaning
223 * is event dependant. event_code specifies which event.
224 * @write_event_value: write the value associate with the event.
225 * Meaning is event dependent.
228 struct module
*driver_module
;
229 int num_interrupt_lines
;
230 struct attribute_group
*event_attrs
;
231 const struct attribute_group
*attrs
;
233 int (*read_raw
)(struct iio_dev
*indio_dev
,
234 struct iio_chan_spec
const *chan
,
239 int (*write_raw
)(struct iio_dev
*indio_dev
,
240 struct iio_chan_spec
const *chan
,
245 int (*read_event_config
)(struct iio_dev
*indio_dev
,
248 int (*write_event_config
)(struct iio_dev
*indio_dev
,
252 int (*read_event_value
)(struct iio_dev
*indio_dev
,
255 int (*write_event_value
)(struct iio_dev
*indio_dev
,
261 * struct iio_dev - industrial I/O device
262 * @id: [INTERN] used to identify device internally
263 * @dev_data: [DRIVER] device specific data
264 * @modes: [DRIVER] operating modes supported by device
265 * @currentmode: [DRIVER] current operating mode
266 * @dev: [DRIVER] device structure, should be assigned a parent
268 * @event_interfaces: [INTERN] event chrdevs associated with interrupt lines
269 * @ring: [DRIVER] any ring buffer present
270 * @mlock: [INTERN] lock used to prevent simultaneous device state
272 * @available_scan_masks: [DRIVER] optional array of allowed bitmasks
273 * @trig: [INTERN] current device trigger (ring buffer modes)
274 * @pollfunc: [DRIVER] function run on trigger being received
275 * @channels: [DRIVER] channel specification structure table
276 * @num_channels: [DRIVER] number of chanels specified in @channels.
277 * @channel_attr_list: [INTERN] keep track of automatically created channel
279 * @name: [DRIVER] name of the device.
288 struct iio_event_interface
*event_interfaces
;
290 struct iio_ring_buffer
*ring
;
293 u32
*available_scan_masks
;
294 struct iio_trigger
*trig
;
295 struct iio_poll_func
*pollfunc
;
297 struct iio_chan_spec
const *channels
;
300 struct list_head channel_attr_list
;
302 const struct iio_info
*info
;
306 * iio_device_register() - register a device with the IIO subsystem
307 * @dev_info: Device structure filled by the device driver
309 int iio_device_register(struct iio_dev
*dev_info
);
312 * iio_device_unregister() - unregister a device from the IIO subsystem
313 * @dev_info: Device structure representing the device.
315 void iio_device_unregister(struct iio_dev
*dev_info
);
318 * iio_push_event() - try to add event to the list for userspace reading
319 * @dev_info: IIO device structure
320 * @ev_line: Which event line (hardware interrupt)
321 * @ev_code: What event
322 * @timestamp: When the event occurred
324 int iio_push_event(struct iio_dev
*dev_info
,
329 /* Used to distinguish between bipolar and unipolar scan elemenents.
330 * Whilst this may seem obvious, we may well want to change the representation
332 #define IIO_SIGNED(a) -(a)
333 #define IIO_UNSIGNED(a) (a)
335 extern dev_t iio_devt
;
336 extern struct bus_type iio_bus_type
;
339 * iio_put_device() - reference counted deallocation of struct device
340 * @dev: the iio_device containing the device
342 static inline void iio_put_device(struct iio_dev
*dev
)
345 put_device(&dev
->dev
);
349 * to_iio_dev() - get iio_dev for which we have the struct device
350 * @d: the struct device
352 static inline struct iio_dev
*to_iio_dev(struct device
*d
)
354 return container_of(d
, struct iio_dev
, dev
);
358 * iio_dev_get_devdata() - helper function gets device specific data
359 * @d: the iio_dev associated with the device
361 static inline void *iio_dev_get_devdata(struct iio_dev
*d
)
367 /* Can we make this smaller? */
368 #define IIO_ALIGN L1_CACHE_BYTES
370 * iio_allocate_device() - allocate an iio_dev from a driver
371 * @sizeof_priv: Space to allocate for private structure.
373 struct iio_dev
*iio_allocate_device(int sizeof_priv
);
375 static inline void *iio_priv(const struct iio_dev
*dev
)
377 return (char *)dev
+ ALIGN(sizeof(struct iio_dev
), IIO_ALIGN
);
380 static inline struct iio_dev
*iio_priv_to_dev(void *priv
)
382 return (struct iio_dev
*)((char *)priv
-
383 ALIGN(sizeof(struct iio_dev
), IIO_ALIGN
));
387 * iio_free_device() - free an iio_dev from a driver
388 * @dev: the iio_dev associated with the device
390 void iio_free_device(struct iio_dev
*dev
);
393 * iio_put() - internal module reference count reduce
398 * iio_get() - internal module reference count increase
403 * iio_device_get_chrdev_minor() - get an unused minor number
405 int iio_device_get_chrdev_minor(void);
406 void iio_device_free_chrdev_minor(int val
);
409 * iio_ring_enabled() - helper function to test if any form of ring is enabled
410 * @dev_info: IIO device info structure for device
412 static inline bool iio_ring_enabled(struct iio_dev
*dev_info
)
414 return dev_info
->currentmode
415 & (INDIO_RING_TRIGGERED
416 | INDIO_RING_HARDWARE_BUFFER
);
421 int iio_get_new_ida_val(struct ida
*this_ida
);
422 void iio_free_ida_val(struct ida
*this_ida
, int id
);
423 #endif /* _INDUSTRIAL_IO_H_ */