1
2
3
4
5
6
7
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/iio/types.h>
16
17
18
19
20
21
22enum iio_chan_info_enum {
23 IIO_CHAN_INFO_RAW = 0,
24 IIO_CHAN_INFO_PROCESSED,
25 IIO_CHAN_INFO_SCALE,
26 IIO_CHAN_INFO_OFFSET,
27 IIO_CHAN_INFO_CALIBSCALE,
28 IIO_CHAN_INFO_CALIBBIAS,
29 IIO_CHAN_INFO_PEAK,
30 IIO_CHAN_INFO_PEAK_SCALE,
31 IIO_CHAN_INFO_QUADRATURE_CORRECTION_RAW,
32 IIO_CHAN_INFO_AVERAGE_RAW,
33 IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY,
34 IIO_CHAN_INFO_SAMP_FREQ,
35 IIO_CHAN_INFO_FREQUENCY,
36 IIO_CHAN_INFO_PHASE,
37 IIO_CHAN_INFO_HARDWAREGAIN,
38 IIO_CHAN_INFO_HYSTERESIS,
39 IIO_CHAN_INFO_INT_TIME,
40};
41
42enum iio_shared_by {
43 IIO_SEPARATE,
44 IIO_SHARED_BY_TYPE,
45 IIO_SHARED_BY_DIR,
46 IIO_SHARED_BY_ALL
47};
48
49enum iio_endian {
50 IIO_CPU,
51 IIO_BE,
52 IIO_LE,
53};
54
55struct iio_chan_spec;
56struct iio_dev;
57
58
59
60
61
62
63
64
65
66struct iio_chan_spec_ext_info {
67 const char *name;
68 enum iio_shared_by shared;
69 ssize_t (*read)(struct iio_dev *, uintptr_t private,
70 struct iio_chan_spec const *, char *buf);
71 ssize_t (*write)(struct iio_dev *, uintptr_t private,
72 struct iio_chan_spec const *, const char *buf,
73 size_t len);
74 uintptr_t private;
75};
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93struct iio_enum {
94 const char * const *items;
95 unsigned int num_items;
96 int (*set)(struct iio_dev *, const struct iio_chan_spec *, unsigned int);
97 int (*get)(struct iio_dev *, const struct iio_chan_spec *);
98};
99
100ssize_t iio_enum_available_read(struct iio_dev *indio_dev,
101 uintptr_t priv, const struct iio_chan_spec *chan, char *buf);
102ssize_t iio_enum_read(struct iio_dev *indio_dev,
103 uintptr_t priv, const struct iio_chan_spec *chan, char *buf);
104ssize_t iio_enum_write(struct iio_dev *indio_dev,
105 uintptr_t priv, const struct iio_chan_spec *chan, const char *buf,
106 size_t len);
107
108
109
110
111
112
113
114
115
116#define IIO_ENUM(_name, _shared, _e) \
117{ \
118 .name = (_name), \
119 .shared = (_shared), \
120 .read = iio_enum_read, \
121 .write = iio_enum_write, \
122 .private = (uintptr_t)(_e), \
123}
124
125
126
127
128
129
130
131
132
133#define IIO_ENUM_AVAILABLE(_name, _e) \
134{ \
135 .name = (_name "_available"), \
136 .shared = IIO_SHARED_BY_TYPE, \
137 .read = iio_enum_available_read, \
138 .private = (uintptr_t)(_e), \
139}
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155struct iio_event_spec {
156 enum iio_event_type type;
157 enum iio_event_direction dir;
158 unsigned long mask_separate;
159 unsigned long mask_shared_by_type;
160 unsigned long mask_shared_by_dir;
161 unsigned long mask_shared_by_all;
162};
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211struct iio_chan_spec {
212 enum iio_chan_type type;
213 int channel;
214 int channel2;
215 unsigned long address;
216 int scan_index;
217 struct {
218 char sign;
219 u8 realbits;
220 u8 storagebits;
221 u8 shift;
222 enum iio_endian endianness;
223 } scan_type;
224 long info_mask_separate;
225 long info_mask_shared_by_type;
226 long info_mask_shared_by_dir;
227 long info_mask_shared_by_all;
228 const struct iio_event_spec *event_spec;
229 unsigned int num_event_specs;
230 const struct iio_chan_spec_ext_info *ext_info;
231 const char *extend_name;
232 const char *datasheet_name;
233 unsigned modified:1;
234 unsigned indexed:1;
235 unsigned output:1;
236 unsigned differential:1;
237};
238
239
240
241
242
243
244
245
246
247
248static inline bool iio_channel_has_info(const struct iio_chan_spec *chan,
249 enum iio_chan_info_enum type)
250{
251 return (chan->info_mask_separate & BIT(type)) |
252 (chan->info_mask_shared_by_type & BIT(type)) |
253 (chan->info_mask_shared_by_dir & BIT(type)) |
254 (chan->info_mask_shared_by_all & BIT(type));
255}
256
257#define IIO_CHAN_SOFT_TIMESTAMP(_si) { \
258 .type = IIO_TIMESTAMP, \
259 .channel = -1, \
260 .scan_index = _si, \
261 .scan_type = { \
262 .sign = 's', \
263 .realbits = 64, \
264 .storagebits = 64, \
265 }, \
266}
267
268
269
270
271static inline s64 iio_get_time_ns(void)
272{
273 struct timespec ts;
274
275
276
277
278 ktime_get_real_ts(&ts);
279
280 return timespec_to_ns(&ts);
281}
282
283
284#define INDIO_DIRECT_MODE 0x01
285#define INDIO_BUFFER_TRIGGERED 0x02
286#define INDIO_BUFFER_HARDWARE 0x08
287
288#define INDIO_ALL_BUFFER_MODES \
289 (INDIO_BUFFER_TRIGGERED | INDIO_BUFFER_HARDWARE)
290
291struct iio_trigger;
292struct iio_dev;
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320struct iio_info {
321 struct module *driver_module;
322 struct attribute_group *event_attrs;
323 const struct attribute_group *attrs;
324
325 int (*read_raw)(struct iio_dev *indio_dev,
326 struct iio_chan_spec const *chan,
327 int *val,
328 int *val2,
329 long mask);
330
331 int (*write_raw)(struct iio_dev *indio_dev,
332 struct iio_chan_spec const *chan,
333 int val,
334 int val2,
335 long mask);
336
337 int (*write_raw_get_fmt)(struct iio_dev *indio_dev,
338 struct iio_chan_spec const *chan,
339 long mask);
340
341 int (*read_event_config)(struct iio_dev *indio_dev,
342 const struct iio_chan_spec *chan,
343 enum iio_event_type type,
344 enum iio_event_direction dir);
345
346 int (*write_event_config)(struct iio_dev *indio_dev,
347 const struct iio_chan_spec *chan,
348 enum iio_event_type type,
349 enum iio_event_direction dir,
350 int state);
351
352 int (*read_event_value)(struct iio_dev *indio_dev,
353 const struct iio_chan_spec *chan,
354 enum iio_event_type type,
355 enum iio_event_direction dir,
356 enum iio_event_info info, int *val, int *val2);
357
358 int (*write_event_value)(struct iio_dev *indio_dev,
359 const struct iio_chan_spec *chan,
360 enum iio_event_type type,
361 enum iio_event_direction dir,
362 enum iio_event_info info, int val, int val2);
363
364 int (*validate_trigger)(struct iio_dev *indio_dev,
365 struct iio_trigger *trig);
366 int (*update_scan_mode)(struct iio_dev *indio_dev,
367 const unsigned long *scan_mask);
368 int (*debugfs_reg_access)(struct iio_dev *indio_dev,
369 unsigned reg, unsigned writeval,
370 unsigned *readval);
371};
372
373
374
375
376
377
378
379
380
381
382
383struct iio_buffer_setup_ops {
384 int (*preenable)(struct iio_dev *);
385 int (*postenable)(struct iio_dev *);
386 int (*predisable)(struct iio_dev *);
387 int (*postdisable)(struct iio_dev *);
388 bool (*validate_scan_mask)(struct iio_dev *indio_dev,
389 const unsigned long *scan_mask);
390};
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430struct iio_dev {
431 int id;
432
433 int modes;
434 int currentmode;
435 struct device dev;
436
437 struct iio_event_interface *event_interface;
438
439 struct iio_buffer *buffer;
440 struct list_head buffer_list;
441 int scan_bytes;
442 struct mutex mlock;
443
444 const unsigned long *available_scan_masks;
445 unsigned masklength;
446 const unsigned long *active_scan_mask;
447 bool scan_timestamp;
448 unsigned scan_index_timestamp;
449 struct iio_trigger *trig;
450 struct iio_poll_func *pollfunc;
451
452 struct iio_chan_spec const *channels;
453 int num_channels;
454
455 struct list_head channel_attr_list;
456 struct attribute_group chan_attr_group;
457 const char *name;
458 const struct iio_info *info;
459 struct mutex info_exist_lock;
460 const struct iio_buffer_setup_ops *setup_ops;
461 struct cdev chrdev;
462#define IIO_MAX_GROUPS 6
463 const struct attribute_group *groups[IIO_MAX_GROUPS + 1];
464 int groupcounter;
465
466 unsigned long flags;
467#if defined(CONFIG_DEBUG_FS)
468 struct dentry *debugfs_dentry;
469 unsigned cached_reg_addr;
470#endif
471};
472
473const struct iio_chan_spec
474*iio_find_channel_from_si(struct iio_dev *indio_dev, int si);
475int iio_device_register(struct iio_dev *indio_dev);
476void iio_device_unregister(struct iio_dev *indio_dev);
477int devm_iio_device_register(struct device *dev, struct iio_dev *indio_dev);
478void devm_iio_device_unregister(struct device *dev, struct iio_dev *indio_dev);
479int iio_push_event(struct iio_dev *indio_dev, u64 ev_code, s64 timestamp);
480
481extern struct bus_type iio_bus_type;
482
483
484
485
486
487static inline void iio_device_put(struct iio_dev *indio_dev)
488{
489 if (indio_dev)
490 put_device(&indio_dev->dev);
491}
492
493
494
495
496
497
498
499static inline struct iio_dev *dev_to_iio_dev(struct device *dev)
500{
501 return container_of(dev, struct iio_dev, dev);
502}
503
504
505
506
507
508
509
510static inline struct iio_dev *iio_device_get(struct iio_dev *indio_dev)
511{
512 return indio_dev ? dev_to_iio_dev(get_device(&indio_dev->dev)) : NULL;
513}
514
515
516
517
518
519
520
521
522
523
524static inline void iio_device_set_drvdata(struct iio_dev *indio_dev, void *data)
525{
526 dev_set_drvdata(&indio_dev->dev, data);
527}
528
529
530
531
532
533
534
535static inline void *iio_device_get_drvdata(struct iio_dev *indio_dev)
536{
537 return dev_get_drvdata(&indio_dev->dev);
538}
539
540
541#define IIO_ALIGN L1_CACHE_BYTES
542struct iio_dev *iio_device_alloc(int sizeof_priv);
543
544static inline void *iio_priv(const struct iio_dev *indio_dev)
545{
546 return (char *)indio_dev + ALIGN(sizeof(struct iio_dev), IIO_ALIGN);
547}
548
549static inline struct iio_dev *iio_priv_to_dev(void *priv)
550{
551 return (struct iio_dev *)((char *)priv -
552 ALIGN(sizeof(struct iio_dev), IIO_ALIGN));
553}
554
555void iio_device_free(struct iio_dev *indio_dev);
556struct iio_dev *devm_iio_device_alloc(struct device *dev, int sizeof_priv);
557void devm_iio_device_free(struct device *dev, struct iio_dev *indio_dev);
558struct iio_trigger *devm_iio_trigger_alloc(struct device *dev,
559 const char *fmt, ...);
560void devm_iio_trigger_free(struct device *dev, struct iio_trigger *iio_trig);
561
562
563
564
565
566static inline bool iio_buffer_enabled(struct iio_dev *indio_dev)
567{
568 return indio_dev->currentmode
569 & (INDIO_BUFFER_TRIGGERED | INDIO_BUFFER_HARDWARE);
570}
571
572
573
574
575
576#if defined(CONFIG_DEBUG_FS)
577static inline struct dentry *iio_get_debugfs_dentry(struct iio_dev *indio_dev)
578{
579 return indio_dev->debugfs_dentry;
580}
581#else
582static inline struct dentry *iio_get_debugfs_dentry(struct iio_dev *indio_dev)
583{
584 return NULL;
585}
586#endif
587
588int iio_str_to_fixpoint(const char *str, int fract_mult, int *integer,
589 int *fract);
590
591
592
593
594
595
596
597#define IIO_DEGREE_TO_RAD(deg) (((deg) * 314159ULL + 9000000ULL) / 18000000ULL)
598
599
600
601
602
603
604
605#define IIO_G_TO_M_S_2(g) ((g) * 980665ULL / 100000ULL)
606
607#endif
608