blob: 7a6ce4d0fb7bde721c278f7be59de58ec2693d87 [file] [log] [blame]
Jonathan Cameron847ec802009-08-18 18:06:19 +01001/* 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>
Jonathan Cameron72b38e32011-05-18 14:40:59 +010015#include <linux/irq.h>
Jonathan Cameron847ec802009-08-18 18:06:19 +010016#include "sysfs.h"
17#include "chrdev.h"
18
19/* IIO TODO LIST */
Jonathan Cameron751a3702010-06-26 12:54:19 +010020/*
Jonathan Cameron847ec802009-08-18 18:06:19 +010021 * 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
Jonathan Cameron1d892712011-05-18 14:40:51 +010028/* naughty temporary hack to match these against the event version
29 - need to flattern these together */
30enum iio_chan_type {
Jonathan Cameron1d892712011-05-18 14:40:51 +010031 /* real channel types */
32 IIO_IN,
Michael Hennerichae191782011-06-10 15:40:48 +020033 IIO_OUT,
Michael Hennerichfaf290e2011-05-18 14:42:02 +010034 IIO_CURRENT,
35 IIO_POWER,
Jonathan Cameron1d892712011-05-18 14:40:51 +010036 IIO_ACCEL,
37 IIO_IN_DIFF,
38 IIO_GYRO,
39 IIO_MAGN,
40 IIO_LIGHT,
41 IIO_INTENSITY,
42 IIO_PROXIMITY,
43 IIO_TEMP,
44 IIO_INCLI,
45 IIO_ROT,
46 IIO_ANGL,
47 IIO_TIMESTAMP,
48};
49
50#define IIO_MOD_X 0
51#define IIO_MOD_LIGHT_BOTH 0
52#define IIO_MOD_Y 1
53#define IIO_MOD_LIGHT_IR 1
54#define IIO_MOD_Z 2
55#define IIO_MOD_X_AND_Y 3
56#define IIO_MOD_X_ANX_Z 4
57#define IIO_MOD_Y_AND_Z 5
58#define IIO_MOD_X_AND_Y_AND_Z 6
59#define IIO_MOD_X_OR_Y 7
60#define IIO_MOD_X_OR_Z 8
61#define IIO_MOD_Y_OR_Z 9
62#define IIO_MOD_X_OR_Y_OR_Z 10
63
64/* Could add the raw attributes as well - allowing buffer only devices */
65enum iio_chan_info_enum {
66 IIO_CHAN_INFO_SCALE_SHARED,
67 IIO_CHAN_INFO_SCALE_SEPARATE,
68 IIO_CHAN_INFO_OFFSET_SHARED,
69 IIO_CHAN_INFO_OFFSET_SEPARATE,
70 IIO_CHAN_INFO_CALIBSCALE_SHARED,
71 IIO_CHAN_INFO_CALIBSCALE_SEPARATE,
72 IIO_CHAN_INFO_CALIBBIAS_SHARED,
Jonathan Cameroneb7fea52011-05-18 14:41:57 +010073 IIO_CHAN_INFO_CALIBBIAS_SEPARATE,
74 IIO_CHAN_INFO_PEAK_SHARED,
75 IIO_CHAN_INFO_PEAK_SEPARATE,
76 IIO_CHAN_INFO_PEAK_SCALE_SHARED,
77 IIO_CHAN_INFO_PEAK_SCALE_SEPARATE,
Jonathan Cameron1d892712011-05-18 14:40:51 +010078};
79
80/**
81 * struct iio_chan_spec - specification of a single channel
82 * @type: What type of measurement is the channel making.
83 * @channel: What number or name do we wish to asign the channel.
84 * @channel2: If there is a second number for a differential
85 * channel then this is it. If modified is set then the
86 * value here specifies the modifier.
87 * @address: Driver specific identifier.
88 * @scan_index: Monotonic index to give ordering in scans when read
89 * from a buffer.
90 * @scan_type: Sign: 's' or 'u' to specify signed or unsigned
91 * realbits: Number of valid bits of data
92 * storage_bits: Realbits + padding
93 * shift: Shift right by this before masking out
94 * realbits.
95 * @info_mask: What information is to be exported about this channel.
96 * This includes calibbias, scale etc.
97 * @event_mask: What events can this channel produce.
98 * @extend_name: Allows labeling of channel attributes with an
99 * informative name. Note this has no effect codes etc,
100 * unlike modifiers.
101 * @processed_val: Flag to specify the data access attribute should be
102 * *_input rather than *_raw.
103 * @modified: Does a modifier apply to this channel. What these are
104 * depends on the channel type. Modifier is set in
105 * channel2. Examples are IIO_MOD_X for axial sensors about
106 * the 'x' axis.
107 * @indexed: Specify the channel has a numerical index. If not,
108 * the value in channel will be suppressed for attribute
109 * but not for event codes. Typically set it to 0 when
110 * the index is false.
Jonathan Cameron1d892712011-05-18 14:40:51 +0100111 */
112struct iio_chan_spec {
113 enum iio_chan_type type;
114 int channel;
115 int channel2;
116 unsigned long address;
117 int scan_index;
118 struct {
119 char sign;
120 u8 realbits;
121 u8 storagebits;
122 u8 shift;
123 } scan_type;
124 const long info_mask;
125 const long event_mask;
126 const char *extend_name;
127 unsigned processed_val:1;
128 unsigned modified:1;
129 unsigned indexed:1;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100130};
131/* Meant for internal use only */
132void __iio_device_attr_deinit(struct device_attribute *dev_attr);
133int __iio_device_attr_init(struct device_attribute *dev_attr,
134 const char *postfix,
135 struct iio_chan_spec const *chan,
136 ssize_t (*readfunc)(struct device *dev,
137 struct device_attribute *attr,
138 char *buf),
139 ssize_t (*writefunc)(struct device *dev,
140 struct device_attribute *attr,
141 const char *buf,
142 size_t len),
143 bool generic);
144#define IIO_ST(si, rb, sb, sh) \
145 { .sign = si, .realbits = rb, .storagebits = sb, .shift = sh }
146
147#define IIO_CHAN(_type, _mod, _indexed, _proc, _name, _chan, _chan2, \
Jonathan Cameronaaf370d2011-05-18 14:41:16 +0100148 _inf_mask, _address, _si, _stype, _event_mask) \
Jonathan Cameron1d892712011-05-18 14:40:51 +0100149 { .type = _type, \
150 .modified = _mod, \
151 .indexed = _indexed, \
152 .processed_val = _proc, \
153 .extend_name = _name, \
154 .channel = _chan, \
155 .channel2 = _chan2, \
156 .info_mask = _inf_mask, \
157 .address = _address, \
158 .scan_index = _si, \
159 .scan_type = _stype, \
Jonathan Cameronaaf370d2011-05-18 14:41:16 +0100160 .event_mask = _event_mask }
Jonathan Cameron1d892712011-05-18 14:40:51 +0100161
162#define IIO_CHAN_SOFT_TIMESTAMP(_si) \
163 { .type = IIO_TIMESTAMP, .channel = -1, \
164 .scan_index = _si, .scan_type = IIO_ST('s', 64, 64, 0) }
165
166int __iio_add_chan_devattr(const char *postfix,
167 const char *group,
168 struct iio_chan_spec const *chan,
169 ssize_t (*func)(struct device *dev,
170 struct device_attribute *attr,
171 char *buf),
172 ssize_t (*writefunc)(struct device *dev,
173 struct device_attribute *attr,
174 const char *buf,
175 size_t len),
176 int mask,
177 bool generic,
178 struct device *dev,
179 struct list_head *attr_list);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100180/**
181 * iio_get_time_ns() - utility function to get a time stamp for events etc
182 **/
183static inline s64 iio_get_time_ns(void)
184{
185 struct timespec ts;
186 /*
187 * calls getnstimeofday.
188 * If hrtimers then up to ns accurate, if not microsecond.
189 */
190 ktime_get_real_ts(&ts);
191
192 return timespec_to_ns(&ts);
193}
194
Jonathan Cameron847ec802009-08-18 18:06:19 +0100195/* Device operating modes */
196#define INDIO_DIRECT_MODE 0x01
197#define INDIO_RING_TRIGGERED 0x02
198#define INDIO_RING_HARDWARE_BUFFER 0x08
199
200#define INDIO_ALL_RING_MODES (INDIO_RING_TRIGGERED | INDIO_RING_HARDWARE_BUFFER)
201
202/* Vast majority of this is set by the industrialio subsystem on a
203 * call to iio_device_register. */
Jonathan Cameron1d892712011-05-18 14:40:51 +0100204#define IIO_VAL_INT 1
205#define IIO_VAL_INT_PLUS_MICRO 2
Michael Hennerich71646e22011-06-27 13:07:07 +0100206#define IIO_VAL_INT_PLUS_NANO 3
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100207
Michael Hennerich43a43602011-06-27 13:07:09 +0100208struct iio_trigger; /* forward declaration */
209
Jonathan Cameron847ec802009-08-18 18:06:19 +0100210/**
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100211 * struct iio_info - constant information about device
212 * @driver_module: module structure used to ensure correct
Jonathan Cameron847ec802009-08-18 18:06:19 +0100213 * ownership of chrdevs etc
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100214 * @num_interrupt_lines:number of physical interrupt lines from device
215 * @event_attrs: event control attributes
216 * @attrs: general purpose device attributes
217 * @read_raw: function to request a value from the device.
Jonathan Cameron1d892712011-05-18 14:40:51 +0100218 * mask specifies which value. Note 0 means a reading of
219 * the channel in question. Return value will specify the
220 * type of value returned by the device. val and val2 will
221 * contain the elements making up the returned value.
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100222 * @write_raw: function to write a value to the device.
Jonathan Cameron1d892712011-05-18 14:40:51 +0100223 * Parameters are the same as for read_raw.
Michael Hennerich5c04af02011-06-27 13:07:10 +0100224 * @write_raw_get_fmt: callback function to query the expected
225 * format/precision. If not set by the driver, write_raw
226 * returns IIO_VAL_INT_PLUS_MICRO.
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100227 * @read_event_config: find out if the event is enabled.
228 * @write_event_config: set if the event is enabled.
229 * @read_event_value: read a value associated with the event. Meaning
Jonathan Cameron1d892712011-05-18 14:40:51 +0100230 * is event dependant. event_code specifies which event.
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100231 * @write_event_value: write the value associate with the event.
Jonathan Cameron1d892712011-05-18 14:40:51 +0100232 * Meaning is event dependent.
Michael Hennerich43a43602011-06-27 13:07:09 +0100233 * @validate_trigger: function to validate the trigger when the
234 * current trigger gets changed.
Jonathan Cameron847ec802009-08-18 18:06:19 +0100235 **/
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100236struct iio_info {
Jonathan Cameron847ec802009-08-18 18:06:19 +0100237 struct module *driver_module;
Jonathan Cameron847ec802009-08-18 18:06:19 +0100238 int num_interrupt_lines;
Jonathan Cameron847ec802009-08-18 18:06:19 +0100239 struct attribute_group *event_attrs;
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100240 const struct attribute_group *attrs;
Jonathan Cameron847ec802009-08-18 18:06:19 +0100241
Jonathan Cameron1d892712011-05-18 14:40:51 +0100242 int (*read_raw)(struct iio_dev *indio_dev,
243 struct iio_chan_spec const *chan,
244 int *val,
245 int *val2,
246 long mask);
247
248 int (*write_raw)(struct iio_dev *indio_dev,
249 struct iio_chan_spec const *chan,
250 int val,
251 int val2,
252 long mask);
253
Michael Hennerich5c04af02011-06-27 13:07:10 +0100254 int (*write_raw_get_fmt)(struct iio_dev *indio_dev,
255 struct iio_chan_spec const *chan,
256 long mask);
257
Jonathan Cameron1d892712011-05-18 14:40:51 +0100258 int (*read_event_config)(struct iio_dev *indio_dev,
259 int event_code);
260
261 int (*write_event_config)(struct iio_dev *indio_dev,
262 int event_code,
Jonathan Cameron1d892712011-05-18 14:40:51 +0100263 int state);
264
265 int (*read_event_value)(struct iio_dev *indio_dev,
266 int event_code,
267 int *val);
268 int (*write_event_value)(struct iio_dev *indio_dev,
269 int event_code,
270 int val);
Michael Hennerich43a43602011-06-27 13:07:09 +0100271 int (*validate_trigger)(struct iio_dev *indio_dev,
272 struct iio_trigger *trig);
273
Jonathan Cameron847ec802009-08-18 18:06:19 +0100274};
275
Jonathan Cameron847ec802009-08-18 18:06:19 +0100276/**
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100277 * struct iio_dev - industrial I/O device
278 * @id: [INTERN] used to identify device internally
279 * @dev_data: [DRIVER] device specific data
280 * @modes: [DRIVER] operating modes supported by device
281 * @currentmode: [DRIVER] current operating mode
282 * @dev: [DRIVER] device structure, should be assigned a parent
283 * and owner
284 * @event_interfaces: [INTERN] event chrdevs associated with interrupt lines
285 * @ring: [DRIVER] any ring buffer present
286 * @mlock: [INTERN] lock used to prevent simultaneous device state
287 * changes
288 * @available_scan_masks: [DRIVER] optional array of allowed bitmasks
289 * @trig: [INTERN] current device trigger (ring buffer modes)
290 * @pollfunc: [DRIVER] function run on trigger being received
291 * @channels: [DRIVER] channel specification structure table
292 * @num_channels: [DRIVER] number of chanels specified in @channels.
293 * @channel_attr_list: [INTERN] keep track of automatically created channel
294 * attributes.
295 * @name: [DRIVER] name of the device.
296 **/
297struct iio_dev {
298 int id;
299 void *dev_data;
300 int modes;
301 int currentmode;
302 struct device dev;
303
304 struct iio_event_interface *event_interfaces;
305
306 struct iio_ring_buffer *ring;
307 struct mutex mlock;
308
309 u32 *available_scan_masks;
310 struct iio_trigger *trig;
311 struct iio_poll_func *pollfunc;
312
313 struct iio_chan_spec const *channels;
314 int num_channels;
315
316 struct list_head channel_attr_list;
317 const char *name;
318 const struct iio_info *info;
319};
320
321/**
Jonathan Cameron847ec802009-08-18 18:06:19 +0100322 * iio_device_register() - register a device with the IIO subsystem
323 * @dev_info: Device structure filled by the device driver
324 **/
325int iio_device_register(struct iio_dev *dev_info);
326
327/**
328 * iio_device_unregister() - unregister a device from the IIO subsystem
329 * @dev_info: Device structure representing the device.
330 **/
331void iio_device_unregister(struct iio_dev *dev_info);
332
333/**
Jonathan Cameron847ec802009-08-18 18:06:19 +0100334 * iio_push_event() - try to add event to the list for userspace reading
335 * @dev_info: IIO device structure
336 * @ev_line: Which event line (hardware interrupt)
337 * @ev_code: What event
Randy Dunlap4c572602009-10-04 19:34:02 -0700338 * @timestamp: When the event occurred
Jonathan Cameron847ec802009-08-18 18:06:19 +0100339 **/
340int iio_push_event(struct iio_dev *dev_info,
341 int ev_line,
342 int ev_code,
343 s64 timestamp);
344
Jonathan Cameron847ec802009-08-18 18:06:19 +0100345/* Used to distinguish between bipolar and unipolar scan elemenents.
346 * Whilst this may seem obvious, we may well want to change the representation
347 * in the future!*/
348#define IIO_SIGNED(a) -(a)
349#define IIO_UNSIGNED(a) (a)
350
351extern dev_t iio_devt;
Jonathan Cameron5aaaeba2010-05-04 14:43:00 +0100352extern struct bus_type iio_bus_type;
Jonathan Cameron847ec802009-08-18 18:06:19 +0100353
354/**
Randy Dunlap4c572602009-10-04 19:34:02 -0700355 * iio_put_device() - reference counted deallocation of struct device
Jonathan Cameron847ec802009-08-18 18:06:19 +0100356 * @dev: the iio_device containing the device
357 **/
358static inline void iio_put_device(struct iio_dev *dev)
359{
360 if (dev)
361 put_device(&dev->dev);
362};
363
364/**
Randy Dunlap4c572602009-10-04 19:34:02 -0700365 * to_iio_dev() - get iio_dev for which we have the struct device
Jonathan Cameron847ec802009-08-18 18:06:19 +0100366 * @d: the struct device
367 **/
368static inline struct iio_dev *to_iio_dev(struct device *d)
369{
370 return container_of(d, struct iio_dev, dev);
371};
372
373/**
374 * iio_dev_get_devdata() - helper function gets device specific data
375 * @d: the iio_dev associated with the device
376 **/
377static inline void *iio_dev_get_devdata(struct iio_dev *d)
378{
379 return d->dev_data;
380}
381
Jonathan Cameron6f7c8ee2011-04-15 18:55:56 +0100382
383/* Can we make this smaller? */
384#define IIO_ALIGN L1_CACHE_BYTES
Jonathan Cameron847ec802009-08-18 18:06:19 +0100385/**
386 * iio_allocate_device() - allocate an iio_dev from a driver
Jonathan Cameron6f7c8ee2011-04-15 18:55:56 +0100387 * @sizeof_priv: Space to allocate for private structure.
Jonathan Cameron847ec802009-08-18 18:06:19 +0100388 **/
Jonathan Cameron6f7c8ee2011-04-15 18:55:56 +0100389struct iio_dev *iio_allocate_device(int sizeof_priv);
390
391static inline void *iio_priv(const struct iio_dev *dev)
392{
393 return (char *)dev + ALIGN(sizeof(struct iio_dev), IIO_ALIGN);
394}
395
396static inline struct iio_dev *iio_priv_to_dev(void *priv)
397{
398 return (struct iio_dev *)((char *)priv -
399 ALIGN(sizeof(struct iio_dev), IIO_ALIGN));
400}
Jonathan Cameron847ec802009-08-18 18:06:19 +0100401
402/**
403 * iio_free_device() - free an iio_dev from a driver
Randy Dunlap4c572602009-10-04 19:34:02 -0700404 * @dev: the iio_dev associated with the device
Jonathan Cameron847ec802009-08-18 18:06:19 +0100405 **/
406void iio_free_device(struct iio_dev *dev);
407
408/**
409 * iio_put() - internal module reference count reduce
410 **/
411void iio_put(void);
412
413/**
414 * iio_get() - internal module reference count increase
415 **/
416void iio_get(void);
417
Jonathan Cameron751a3702010-06-26 12:54:19 +0100418/**
419 * iio_device_get_chrdev_minor() - get an unused minor number
420 **/
Jonathan Cameron847ec802009-08-18 18:06:19 +0100421int iio_device_get_chrdev_minor(void);
422void iio_device_free_chrdev_minor(int val);
423
424/**
Randy Dunlap4c572602009-10-04 19:34:02 -0700425 * iio_ring_enabled() - helper function to test if any form of ring is enabled
426 * @dev_info: IIO device info structure for device
Jonathan Cameron847ec802009-08-18 18:06:19 +0100427 **/
428static inline bool iio_ring_enabled(struct iio_dev *dev_info)
429{
430 return dev_info->currentmode
431 & (INDIO_RING_TRIGGERED
432 | INDIO_RING_HARDWARE_BUFFER);
433};
434
Jonathan Cameronb156cf72010-09-04 17:54:43 +0100435struct ida;
Jonathan Cameron847ec802009-08-18 18:06:19 +0100436
Jonathan Cameronb156cf72010-09-04 17:54:43 +0100437int iio_get_new_ida_val(struct ida *this_ida);
438void iio_free_ida_val(struct ida *this_ida, int id);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100439#endif /* _INDUSTRIAL_IO_H_ */