1 /* SPDX-License-Identifier: GPL-2.0-only */
2
3 /* The industrial I/O core
4 *
5 * Copyright (c) 2008 Jonathan Cameron
6 */
7 #ifndef _INDUSTRIAL_IO_H_
8 #define _INDUSTRIAL_IO_H_
9
10 #include <linux/device.h>
11 #include <linux/cdev.h>
12 #include <linux/iio/types.h>
13 #include <linux/of.h>
14 /* IIO TODO LIST */
15 /*
16 * Provide means of adjusting timer accuracy.
17 * Currently assumes nano seconds.
18 */
19
20 enum iio_shared_by {
21 IIO_SEPARATE,
22 IIO_SHARED_BY_TYPE,
23 IIO_SHARED_BY_DIR,
24 IIO_SHARED_BY_ALL
25 };
26
27 enum iio_endian {
28 IIO_CPU,
29 IIO_BE,
30 IIO_LE,
31 };
32
33 struct iio_chan_spec;
34 struct iio_dev;
35
36 /**
37 * struct iio_chan_spec_ext_info - Extended channel info attribute
38 * @name: Info attribute name
39 * @shared: Whether this attribute is shared between all channels.
40 * @read: Read callback for this info attribute, may be NULL.
41 * @write: Write callback for this info attribute, may be NULL.
42 * @private: Data private to the driver.
43 */
44 struct iio_chan_spec_ext_info {
45 const char *name;
46 enum iio_shared_by shared;
47 ssize_t (*read)(struct iio_dev *, uintptr_t private,
48 struct iio_chan_spec const *, char *buf);
49 ssize_t (*write)(struct iio_dev *, uintptr_t private,
50 struct iio_chan_spec const *, const char *buf,
51 size_t len);
52 uintptr_t private;
53 };
54
55 /**
56 * struct iio_enum - Enum channel info attribute
57 * @items: An array of strings.
58 * @num_items: Length of the item array.
59 * @set: Set callback function, may be NULL.
60 * @get: Get callback function, may be NULL.
61 *
62 * The iio_enum struct can be used to implement enum style channel attributes.
63 * Enum style attributes are those which have a set of strings which map to
64 * unsigned integer values. The IIO enum helper code takes care of mapping
65 * between value and string as well as generating a "_available" file which
66 * contains a list of all available items. The set callback will be called when
67 * the attribute is updated. The last parameter is the index to the newly
68 * activated item. The get callback will be used to query the currently active
69 * item and is supposed to return the index for it.
70 */
71 struct iio_enum {
72 const char * const *items;
73 unsigned int num_items;
74 int (*set)(struct iio_dev *, const struct iio_chan_spec *, unsigned int);
75 int (*get)(struct iio_dev *, const struct iio_chan_spec *);
76 };
77
78 ssize_t iio_enum_available_read(struct iio_dev *indio_dev,
79 uintptr_t priv, const struct iio_chan_spec *chan, char *buf);
80 ssize_t iio_enum_read(struct iio_dev *indio_dev,
81 uintptr_t priv, const struct iio_chan_spec *chan, char *buf);
82 ssize_t iio_enum_write(struct iio_dev *indio_dev,
83 uintptr_t priv, const struct iio_chan_spec *chan, const char *buf,
84 size_t len);
85
86 /**
87 * IIO_ENUM() - Initialize enum extended channel attribute
88 * @_name: Attribute name
89 * @_shared: Whether the attribute is shared between all channels
90 * @_e: Pointer to an iio_enum struct
91 *
92 * This should usually be used together with IIO_ENUM_AVAILABLE()
93 */
94 #define IIO_ENUM(_name, _shared, _e) \
95 { \
96 .name = (_name), \
97 .shared = (_shared), \
98 .read = iio_enum_read, \
99 .write = iio_enum_write, \
100 .private = (uintptr_t)(_e), \
101 }
102
103 /**
104 * IIO_ENUM_AVAILABLE() - Initialize enum available extended channel attribute
105 * @_name: Attribute name ("_available" will be appended to the name)
106 * @_e: Pointer to an iio_enum struct
107 *
108 * Creates a read only attribute which lists all the available enum items in a
109 * space separated list. This should usually be used together with IIO_ENUM()
110 */
111 #define IIO_ENUM_AVAILABLE(_name, _e) \
112 { \
113 .name = (_name "_available"), \
114 .shared = IIO_SHARED_BY_TYPE, \
115 .read = iio_enum_available_read, \
116 .private = (uintptr_t)(_e), \
117 }
118
119 /**
120 * struct iio_mount_matrix - iio mounting matrix
121 * @rotation: 3 dimensional space rotation matrix defining sensor alignment with
122 * main hardware
123 */
124 struct iio_mount_matrix {
125 const char *rotation[9];
126 };
127
128 ssize_t iio_show_mount_matrix(struct iio_dev *indio_dev, uintptr_t priv,
129 const struct iio_chan_spec *chan, char *buf);
130 int iio_read_mount_matrix(struct device *dev, struct iio_mount_matrix *matrix);
131
132 typedef const struct iio_mount_matrix *
133 (iio_get_mount_matrix_t)(const struct iio_dev *indio_dev,
134 const struct iio_chan_spec *chan);
135
136 /**
137 * IIO_MOUNT_MATRIX() - Initialize mount matrix extended channel attribute
138 * @_shared: Whether the attribute is shared between all channels
139 * @_get: Pointer to an iio_get_mount_matrix_t accessor
140 */
141 #define IIO_MOUNT_MATRIX(_shared, _get) \
142 { \
143 .name = "mount_matrix", \
144 .shared = (_shared), \
145 .read = iio_show_mount_matrix, \
146 .private = (uintptr_t)(_get), \
147 }
148
149 /**
150 * struct iio_event_spec - specification for a channel event
151 * @type: Type of the event
152 * @dir: Direction of the event
153 * @mask_separate: Bit mask of enum iio_event_info values. Attributes
154 * set in this mask will be registered per channel.
155 * @mask_shared_by_type: Bit mask of enum iio_event_info values. Attributes
156 * set in this mask will be shared by channel type.
157 * @mask_shared_by_dir: Bit mask of enum iio_event_info values. Attributes
158 * set in this mask will be shared by channel type and
159 * direction.
160 * @mask_shared_by_all: Bit mask of enum iio_event_info values. Attributes
161 * set in this mask will be shared by all channels.
162 */
163 struct iio_event_spec {
164 enum iio_event_type type;
165 enum iio_event_direction dir;
166 unsigned long mask_separate;
167 unsigned long mask_shared_by_type;
168 unsigned long mask_shared_by_dir;
169 unsigned long mask_shared_by_all;
170 };
171
172 /**
173 * struct iio_chan_spec - specification of a single channel
174 * @type: What type of measurement is the channel making.
175 * @channel: What number do we wish to assign the channel.
176 * @channel2: If there is a second number for a differential
177 * channel then this is it. If modified is set then the
178 * value here specifies the modifier.
179 * @address: Driver specific identifier.
180 * @scan_index: Monotonic index to give ordering in scans when read
181 * from a buffer.
182 * @scan_type: struct describing the scan type
183 * @scan_type.sign: 's' or 'u' to specify signed or unsigned
184 * @scan_type.realbits: Number of valid bits of data
185 * @scan_type.storagebits: Realbits + padding
186 * @scan_type.shift: Shift right by this before masking out
187 * realbits.
188 * @scan_type.repeat: Number of times real/storage bits repeats.
189 * When the repeat element is more than 1, then
190 * the type element in sysfs will show a repeat
191 * value. Otherwise, the number of repetitions
192 * is omitted.
193 * @scan_type.endianness: little or big endian
194 * @info_mask_separate: What information is to be exported that is specific to
195 * this channel.
196 * @info_mask_separate_available: What availability information is to be
197 * exported that is specific to this channel.
198 * @info_mask_shared_by_type: What information is to be exported that is shared
199 * by all channels of the same type.
200 * @info_mask_shared_by_type_available: What availability information is to be
201 * exported that is shared by all channels of the same
202 * type.
203 * @info_mask_shared_by_dir: What information is to be exported that is shared
204 * by all channels of the same direction.
205 * @info_mask_shared_by_dir_available: What availability information is to be
206 * exported that is shared by all channels of the same
207 * direction.
208 * @info_mask_shared_by_all: What information is to be exported that is shared
209 * by all channels.
210 * @info_mask_shared_by_all_available: What availability information is to be
211 * exported that is shared by all channels.
212 * @event_spec: Array of events which should be registered for this
213 * channel.
214 * @num_event_specs: Size of the event_spec array.
215 * @ext_info: Array of extended info attributes for this channel.
216 * The array is NULL terminated, the last element should
217 * have its name field set to NULL.
218 * @extend_name: Allows labeling of channel attributes with an
219 * informative name. Note this has no effect codes etc,
220 * unlike modifiers.
221 * @datasheet_name: A name used in in-kernel mapping of channels. It should
222 * correspond to the first name that the channel is referred
223 * to by in the datasheet (e.g. IND), or the nearest
224 * possible compound name (e.g. IND-INC).
225 * @modified: Does a modifier apply to this channel. What these are
226 * depends on the channel type. Modifier is set in
227 * channel2. Examples are IIO_MOD_X for axial sensors about
228 * the 'x' axis.
229 * @indexed: Specify the channel has a numerical index. If not,
230 * the channel index number will be suppressed for sysfs
231 * attributes but not for event codes.
232 * @output: Channel is output.
233 * @differential: Channel is differential.
234 */
235 struct iio_chan_spec {
236 enum iio_chan_type type;
237 int channel;
238 int channel2;
239 unsigned long address;
240 int scan_index;
241 struct {
242 char sign;
243 u8 realbits;
244 u8 storagebits;
245 u8 shift;
246 u8 repeat;
247 enum iio_endian endianness;
248 } scan_type;
249 long info_mask_separate;
250 long info_mask_separate_available;
251 long info_mask_shared_by_type;
252 long info_mask_shared_by_type_available;
253 long info_mask_shared_by_dir;
254 long info_mask_shared_by_dir_available;
255 long info_mask_shared_by_all;
256 long info_mask_shared_by_all_available;
257 const struct iio_event_spec *event_spec;
258 unsigned int num_event_specs;
259 const struct iio_chan_spec_ext_info *ext_info;
260 const char *extend_name;
261 const char *datasheet_name;
262 unsigned modified:1;
263 unsigned indexed:1;
264 unsigned output:1;
265 unsigned differential:1;
266 };
267
268
269 /**
270 * iio_channel_has_info() - Checks whether a channel supports a info attribute
271 * @chan: The channel to be queried
272 * @type: Type of the info attribute to be checked
273 *
274 * Returns true if the channels supports reporting values for the given info
275 * attribute type, false otherwise.
276 */
iio_channel_has_info(const struct iio_chan_spec * chan,enum iio_chan_info_enum type)277 static inline bool iio_channel_has_info(const struct iio_chan_spec *chan,
278 enum iio_chan_info_enum type)
279 {
280 return (chan->info_mask_separate & BIT(type)) |
281 (chan->info_mask_shared_by_type & BIT(type)) |
282 (chan->info_mask_shared_by_dir & BIT(type)) |
283 (chan->info_mask_shared_by_all & BIT(type));
284 }
285
286 /**
287 * iio_channel_has_available() - Checks if a channel has an available attribute
288 * @chan: The channel to be queried
289 * @type: Type of the available attribute to be checked
290 *
291 * Returns true if the channel supports reporting available values for the
292 * given attribute type, false otherwise.
293 */
iio_channel_has_available(const struct iio_chan_spec * chan,enum iio_chan_info_enum type)294 static inline bool iio_channel_has_available(const struct iio_chan_spec *chan,
295 enum iio_chan_info_enum type)
296 {
297 return (chan->info_mask_separate_available & BIT(type)) |
298 (chan->info_mask_shared_by_type_available & BIT(type)) |
299 (chan->info_mask_shared_by_dir_available & BIT(type)) |
300 (chan->info_mask_shared_by_all_available & BIT(type));
301 }
302
303 #define IIO_CHAN_SOFT_TIMESTAMP(_si) { \
304 .type = IIO_TIMESTAMP, \
305 .channel = -1, \
306 .scan_index = _si, \
307 .scan_type = { \
308 .sign = 's', \
309 .realbits = 64, \
310 .storagebits = 64, \
311 }, \
312 }
313
314 s64 iio_get_time_ns(const struct iio_dev *indio_dev);
315 unsigned int iio_get_time_res(const struct iio_dev *indio_dev);
316
317 /* Device operating modes */
318 #define INDIO_DIRECT_MODE 0x01
319 #define INDIO_BUFFER_TRIGGERED 0x02
320 #define INDIO_BUFFER_SOFTWARE 0x04
321 #define INDIO_BUFFER_HARDWARE 0x08
322 #define INDIO_EVENT_TRIGGERED 0x10
323 #define INDIO_HARDWARE_TRIGGERED 0x20
324
325 #define INDIO_ALL_BUFFER_MODES \
326 (INDIO_BUFFER_TRIGGERED | INDIO_BUFFER_HARDWARE | INDIO_BUFFER_SOFTWARE)
327
328 #define INDIO_ALL_TRIGGERED_MODES \
329 (INDIO_BUFFER_TRIGGERED \
330 | INDIO_EVENT_TRIGGERED \
331 | INDIO_HARDWARE_TRIGGERED)
332
333 #define INDIO_MAX_RAW_ELEMENTS 4
334
335 struct iio_trigger; /* forward declaration */
336
337 /**
338 * struct iio_info - constant information about device
339 * @event_attrs: event control attributes
340 * @attrs: general purpose device attributes
341 * @read_raw: function to request a value from the device.
342 * mask specifies which value. Note 0 means a reading of
343 * the channel in question. Return value will specify the
344 * type of value returned by the device. val and val2 will
345 * contain the elements making up the returned value.
346 * @read_raw_multi: function to return values from the device.
347 * mask specifies which value. Note 0 means a reading of
348 * the channel in question. Return value will specify the
349 * type of value returned by the device. vals pointer
350 * contain the elements making up the returned value.
351 * max_len specifies maximum number of elements
352 * vals pointer can contain. val_len is used to return
353 * length of valid elements in vals.
354 * @read_avail: function to return the available values from the device.
355 * mask specifies which value. Note 0 means the available
356 * values for the channel in question. Return value
357 * specifies if a IIO_AVAIL_LIST or a IIO_AVAIL_RANGE is
358 * returned in vals. The type of the vals are returned in
359 * type and the number of vals is returned in length. For
360 * ranges, there are always three vals returned; min, step
361 * and max. For lists, all possible values are enumerated.
362 * @write_raw: function to write a value to the device.
363 * Parameters are the same as for read_raw.
364 * @read_label: function to request label name for a specified label,
365 * for better channel identification.
366 * @write_raw_get_fmt: callback function to query the expected
367 * format/precision. If not set by the driver, write_raw
368 * returns IIO_VAL_INT_PLUS_MICRO.
369 * @read_event_config: find out if the event is enabled.
370 * @write_event_config: set if the event is enabled.
371 * @read_event_value: read a configuration value associated with the event.
372 * @write_event_value: write a configuration value for the event.
373 * @validate_trigger: function to validate the trigger when the
374 * current trigger gets changed.
375 * @update_scan_mode: function to configure device and scan buffer when
376 * channels have changed
377 * @debugfs_reg_access: function to read or write register value of device
378 * @of_xlate: function pointer to obtain channel specifier index.
379 * When #iio-cells is greater than '0', the driver could
380 * provide a custom of_xlate function that reads the
381 * *args* and returns the appropriate index in registered
382 * IIO channels array.
383 * @hwfifo_set_watermark: function pointer to set the current hardware
384 * fifo watermark level; see hwfifo_* entries in
385 * Documentation/ABI/testing/sysfs-bus-iio for details on
386 * how the hardware fifo operates
387 * @hwfifo_flush_to_buffer: function pointer to flush the samples stored
388 * in the hardware fifo to the device buffer. The driver
389 * should not flush more than count samples. The function
390 * must return the number of samples flushed, 0 if no
391 * samples were flushed or a negative integer if no samples
392 * were flushed and there was an error.
393 **/
394 struct iio_info {
395 const struct attribute_group *event_attrs;
396 const struct attribute_group *attrs;
397
398 int (*read_raw)(struct iio_dev *indio_dev,
399 struct iio_chan_spec const *chan,
400 int *val,
401 int *val2,
402 long mask);
403
404 int (*read_raw_multi)(struct iio_dev *indio_dev,
405 struct iio_chan_spec const *chan,
406 int max_len,
407 int *vals,
408 int *val_len,
409 long mask);
410
411 int (*read_avail)(struct iio_dev *indio_dev,
412 struct iio_chan_spec const *chan,
413 const int **vals,
414 int *type,
415 int *length,
416 long mask);
417
418 int (*write_raw)(struct iio_dev *indio_dev,
419 struct iio_chan_spec const *chan,
420 int val,
421 int val2,
422 long mask);
423
424 int (*read_label)(struct iio_dev *indio_dev,
425 struct iio_chan_spec const *chan,
426 char *label);
427
428 int (*write_raw_get_fmt)(struct iio_dev *indio_dev,
429 struct iio_chan_spec const *chan,
430 long mask);
431
432 int (*read_event_config)(struct iio_dev *indio_dev,
433 const struct iio_chan_spec *chan,
434 enum iio_event_type type,
435 enum iio_event_direction dir);
436
437 int (*write_event_config)(struct iio_dev *indio_dev,
438 const struct iio_chan_spec *chan,
439 enum iio_event_type type,
440 enum iio_event_direction dir,
441 int state);
442
443 int (*read_event_value)(struct iio_dev *indio_dev,
444 const struct iio_chan_spec *chan,
445 enum iio_event_type type,
446 enum iio_event_direction dir,
447 enum iio_event_info info, int *val, int *val2);
448
449 int (*write_event_value)(struct iio_dev *indio_dev,
450 const struct iio_chan_spec *chan,
451 enum iio_event_type type,
452 enum iio_event_direction dir,
453 enum iio_event_info info, int val, int val2);
454
455 int (*validate_trigger)(struct iio_dev *indio_dev,
456 struct iio_trigger *trig);
457 int (*update_scan_mode)(struct iio_dev *indio_dev,
458 const unsigned long *scan_mask);
459 int (*debugfs_reg_access)(struct iio_dev *indio_dev,
460 unsigned reg, unsigned writeval,
461 unsigned *readval);
462 int (*of_xlate)(struct iio_dev *indio_dev,
463 const struct of_phandle_args *iiospec);
464 int (*hwfifo_set_watermark)(struct iio_dev *indio_dev, unsigned val);
465 int (*hwfifo_flush_to_buffer)(struct iio_dev *indio_dev,
466 unsigned count);
467 };
468
469 /**
470 * struct iio_buffer_setup_ops - buffer setup related callbacks
471 * @preenable: [DRIVER] function to run prior to marking buffer enabled
472 * @postenable: [DRIVER] function to run after marking buffer enabled
473 * @predisable: [DRIVER] function to run prior to marking buffer
474 * disabled
475 * @postdisable: [DRIVER] function to run after marking buffer disabled
476 * @validate_scan_mask: [DRIVER] function callback to check whether a given
477 * scan mask is valid for the device.
478 */
479 struct iio_buffer_setup_ops {
480 int (*preenable)(struct iio_dev *);
481 int (*postenable)(struct iio_dev *);
482 int (*predisable)(struct iio_dev *);
483 int (*postdisable)(struct iio_dev *);
484 bool (*validate_scan_mask)(struct iio_dev *indio_dev,
485 const unsigned long *scan_mask);
486 };
487
488 /**
489 * struct iio_dev - industrial I/O device
490 * @modes: [DRIVER] operating modes supported by device
491 * @currentmode: [DRIVER] current operating mode
492 * @dev: [DRIVER] device structure, should be assigned a parent
493 * and owner
494 * @buffer: [DRIVER] any buffer present
495 * @scan_bytes: [INTERN] num bytes captured to be fed to buffer demux
496 * @mlock: [INTERN] lock used to prevent simultaneous device state
497 * changes
498 * @available_scan_masks: [DRIVER] optional array of allowed bitmasks
499 * @masklength: [INTERN] the length of the mask established from
500 * channels
501 * @active_scan_mask: [INTERN] union of all scan masks requested by buffers
502 * @scan_timestamp: [INTERN] set if any buffers have requested timestamp
503 * @trig: [INTERN] current device trigger (buffer modes)
504 * @pollfunc: [DRIVER] function run on trigger being received
505 * @pollfunc_event: [DRIVER] function run on events trigger being received
506 * @channels: [DRIVER] channel specification structure table
507 * @num_channels: [DRIVER] number of channels specified in @channels.
508 * @name: [DRIVER] name of the device.
509 * @label: [DRIVER] unique name to identify which device this is
510 * @info: [DRIVER] callbacks and constant info from driver
511 * @setup_ops: [DRIVER] callbacks to call before and after buffer
512 * enable/disable
513 * @priv: [DRIVER] reference to driver's private information
514 * **MUST** be accessed **ONLY** via iio_priv() helper
515 */
516 struct iio_dev {
517 int modes;
518 int currentmode;
519 struct device dev;
520
521 struct iio_buffer *buffer;
522 int scan_bytes;
523 struct mutex mlock;
524
525 const unsigned long *available_scan_masks;
526 unsigned masklength;
527 const unsigned long *active_scan_mask;
528 bool scan_timestamp;
529 struct iio_trigger *trig;
530 struct iio_poll_func *pollfunc;
531 struct iio_poll_func *pollfunc_event;
532
533 struct iio_chan_spec const *channels;
534 int num_channels;
535
536 const char *name;
537 const char *label;
538 const struct iio_info *info;
539 const struct iio_buffer_setup_ops *setup_ops;
540
541 void *priv;
542 };
543
544 int iio_device_id(struct iio_dev *indio_dev);
545
546 const struct iio_chan_spec
547 *iio_find_channel_from_si(struct iio_dev *indio_dev, int si);
548 /**
549 * iio_device_register() - register a device with the IIO subsystem
550 * @indio_dev: Device structure filled by the device driver
551 **/
552 #define iio_device_register(indio_dev) \
553 __iio_device_register((indio_dev), THIS_MODULE)
554 int __iio_device_register(struct iio_dev *indio_dev, struct module *this_mod);
555 void iio_device_unregister(struct iio_dev *indio_dev);
556 /**
557 * devm_iio_device_register - Resource-managed iio_device_register()
558 * @dev: Device to allocate iio_dev for
559 * @indio_dev: Device structure filled by the device driver
560 *
561 * Managed iio_device_register. The IIO device registered with this
562 * function is automatically unregistered on driver detach. This function
563 * calls iio_device_register() internally. Refer to that function for more
564 * information.
565 *
566 * RETURNS:
567 * 0 on success, negative error number on failure.
568 */
569 #define devm_iio_device_register(dev, indio_dev) \
570 __devm_iio_device_register((dev), (indio_dev), THIS_MODULE)
571 int __devm_iio_device_register(struct device *dev, struct iio_dev *indio_dev,
572 struct module *this_mod);
573 int iio_push_event(struct iio_dev *indio_dev, u64 ev_code, s64 timestamp);
574 int iio_device_claim_direct_mode(struct iio_dev *indio_dev);
575 void iio_device_release_direct_mode(struct iio_dev *indio_dev);
576
577 extern struct bus_type iio_bus_type;
578
579 /**
580 * iio_device_put() - reference counted deallocation of struct device
581 * @indio_dev: IIO device structure containing the device
582 **/
iio_device_put(struct iio_dev * indio_dev)583 static inline void iio_device_put(struct iio_dev *indio_dev)
584 {
585 if (indio_dev)
586 put_device(&indio_dev->dev);
587 }
588
589 clockid_t iio_device_get_clock(const struct iio_dev *indio_dev);
590 int iio_device_set_clock(struct iio_dev *indio_dev, clockid_t clock_id);
591
592 /**
593 * dev_to_iio_dev() - Get IIO device struct from a device struct
594 * @dev: The device embedded in the IIO device
595 *
596 * Note: The device must be a IIO device, otherwise the result is undefined.
597 */
dev_to_iio_dev(struct device * dev)598 static inline struct iio_dev *dev_to_iio_dev(struct device *dev)
599 {
600 return container_of(dev, struct iio_dev, dev);
601 }
602
603 /**
604 * iio_device_get() - increment reference count for the device
605 * @indio_dev: IIO device structure
606 *
607 * Returns: The passed IIO device
608 **/
iio_device_get(struct iio_dev * indio_dev)609 static inline struct iio_dev *iio_device_get(struct iio_dev *indio_dev)
610 {
611 return indio_dev ? dev_to_iio_dev(get_device(&indio_dev->dev)) : NULL;
612 }
613
614 /**
615 * iio_device_set_parent() - assign parent device to the IIO device object
616 * @indio_dev: IIO device structure
617 * @parent: reference to parent device object
618 *
619 * This utility must be called between IIO device allocation
620 * (via devm_iio_device_alloc()) & IIO device registration
621 * (via iio_device_register() and devm_iio_device_register())).
622 * By default, the device allocation will also assign a parent device to
623 * the IIO device object. In cases where devm_iio_device_alloc() is used,
624 * sometimes the parent device must be different than the device used to
625 * manage the allocation.
626 * In that case, this helper should be used to change the parent, hence the
627 * requirement to call this between allocation & registration.
628 **/
iio_device_set_parent(struct iio_dev * indio_dev,struct device * parent)629 static inline void iio_device_set_parent(struct iio_dev *indio_dev,
630 struct device *parent)
631 {
632 indio_dev->dev.parent = parent;
633 }
634
635 /**
636 * iio_device_set_drvdata() - Set device driver data
637 * @indio_dev: IIO device structure
638 * @data: Driver specific data
639 *
640 * Allows to attach an arbitrary pointer to an IIO device, which can later be
641 * retrieved by iio_device_get_drvdata().
642 */
iio_device_set_drvdata(struct iio_dev * indio_dev,void * data)643 static inline void iio_device_set_drvdata(struct iio_dev *indio_dev, void *data)
644 {
645 dev_set_drvdata(&indio_dev->dev, data);
646 }
647
648 /**
649 * iio_device_get_drvdata() - Get device driver data
650 * @indio_dev: IIO device structure
651 *
652 * Returns the data previously set with iio_device_set_drvdata()
653 */
iio_device_get_drvdata(const struct iio_dev * indio_dev)654 static inline void *iio_device_get_drvdata(const struct iio_dev *indio_dev)
655 {
656 return dev_get_drvdata(&indio_dev->dev);
657 }
658
659 /* Can we make this smaller? */
660 #define IIO_ALIGN L1_CACHE_BYTES
661 struct iio_dev *iio_device_alloc(struct device *parent, int sizeof_priv);
662
663 /* The information at the returned address is guaranteed to be cacheline aligned */
iio_priv(const struct iio_dev * indio_dev)664 static inline void *iio_priv(const struct iio_dev *indio_dev)
665 {
666 return indio_dev->priv;
667 }
668
669 void iio_device_free(struct iio_dev *indio_dev);
670 struct iio_dev *devm_iio_device_alloc(struct device *parent, int sizeof_priv);
671 __printf(2, 3)
672 struct iio_trigger *devm_iio_trigger_alloc(struct device *parent,
673 const char *fmt, ...);
674 /**
675 * iio_buffer_enabled() - helper function to test if the buffer is enabled
676 * @indio_dev: IIO device structure for device
677 **/
iio_buffer_enabled(struct iio_dev * indio_dev)678 static inline bool iio_buffer_enabled(struct iio_dev *indio_dev)
679 {
680 return indio_dev->currentmode
681 & (INDIO_BUFFER_TRIGGERED | INDIO_BUFFER_HARDWARE |
682 INDIO_BUFFER_SOFTWARE);
683 }
684
685 /**
686 * iio_get_debugfs_dentry() - helper function to get the debugfs_dentry
687 * @indio_dev: IIO device structure for device
688 **/
689 #if defined(CONFIG_DEBUG_FS)
690 struct dentry *iio_get_debugfs_dentry(struct iio_dev *indio_dev);
691 #else
iio_get_debugfs_dentry(struct iio_dev * indio_dev)692 static inline struct dentry *iio_get_debugfs_dentry(struct iio_dev *indio_dev)
693 {
694 return NULL;
695 }
696 #endif
697
698 ssize_t iio_format_value(char *buf, unsigned int type, int size, int *vals);
699
700 int iio_str_to_fixpoint(const char *str, int fract_mult, int *integer,
701 int *fract);
702
703 /**
704 * IIO_DEGREE_TO_RAD() - Convert degree to rad
705 * @deg: A value in degree
706 *
707 * Returns the given value converted from degree to rad
708 */
709 #define IIO_DEGREE_TO_RAD(deg) (((deg) * 314159ULL + 9000000ULL) / 18000000ULL)
710
711 /**
712 * IIO_RAD_TO_DEGREE() - Convert rad to degree
713 * @rad: A value in rad
714 *
715 * Returns the given value converted from rad to degree
716 */
717 #define IIO_RAD_TO_DEGREE(rad) \
718 (((rad) * 18000000ULL + 314159ULL / 2) / 314159ULL)
719
720 /**
721 * IIO_G_TO_M_S_2() - Convert g to meter / second**2
722 * @g: A value in g
723 *
724 * Returns the given value converted from g to meter / second**2
725 */
726 #define IIO_G_TO_M_S_2(g) ((g) * 980665ULL / 100000ULL)
727
728 /**
729 * IIO_M_S_2_TO_G() - Convert meter / second**2 to g
730 * @ms2: A value in meter / second**2
731 *
732 * Returns the given value converted from meter / second**2 to g
733 */
734 #define IIO_M_S_2_TO_G(ms2) (((ms2) * 100000ULL + 980665ULL / 2) / 980665ULL)
735
736 #endif /* _INDUSTRIAL_IO_H_ */
737