linux/drivers/staging/iio/iio.h
<<
>>
Prefs
   1/* 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>
  15#include "sysfs.h"
  16#include "chrdev.h"
  17
  18/* IIO TODO LIST */
  19/* Static device specific elements (conversion factors etc)
  20 * should be exported via sysfs
  21 *
  22 * Provide means of adjusting timer accuracy.
  23 * Currently assumes nano seconds.
  24 */
  25
  26/* Event interface flags */
  27#define IIO_BUSY_BIT_POS 1
  28
  29struct iio_dev;
  30
  31/**
  32 * iio_get_time_ns() - utility function to get a time stamp for events etc
  33 **/
  34static inline s64 iio_get_time_ns(void)
  35{
  36        struct timespec ts;
  37        /*
  38         * calls getnstimeofday.
  39         * If hrtimers then up to ns accurate, if not microsecond.
  40         */
  41        ktime_get_real_ts(&ts);
  42
  43        return timespec_to_ns(&ts);
  44}
  45
  46/**
  47 * iio_add_event_to_list() - Wraps adding to event lists
  48 * @el:         the list element of the event to be handled.
  49 * @head:       the list associated with the event handler being used.
  50 *
  51 * Does reference counting to allow shared handlers.
  52 **/
  53void iio_add_event_to_list(struct iio_event_handler_list *el,
  54                           struct list_head *head);
  55
  56/**
  57 * iio_remove_event_from_list() - Wraps removing from event list
  58 * @el:         element to be removed
  59 * @head:       associate list head for the interrupt handler.
  60 *
  61 * Does reference counting to allow shared handlers.
  62 **/
  63void iio_remove_event_from_list(struct iio_event_handler_list *el,
  64                                struct list_head *head);
  65
  66/* Device operating modes */
  67#define INDIO_DIRECT_MODE               0x01
  68#define INDIO_RING_TRIGGERED            0x02
  69#define INDIO_RING_HARDWARE_BUFFER      0x08
  70
  71#define INDIO_ALL_RING_MODES (INDIO_RING_TRIGGERED | INDIO_RING_HARDWARE_BUFFER)
  72
  73/* Vast majority of this is set by the industrialio subsystem on a
  74 * call to iio_device_register. */
  75
  76/**
  77 * struct iio_dev - industrial I/O device
  78 * @id:                 [INTERN] used to identify device internally
  79 * @dev_data:           [DRIVER] device specific data
  80 * @modes:              [DRIVER] operating modes supported by device
  81 * @currentmode:        [DRIVER] current operating mode
  82 * @dev:                [DRIVER] device structure, should be assigned a parent
  83 *                      and owner
  84 * @attrs:              [DRIVER] general purpose device attributes
  85 * @driver_module:      [DRIVER] module structure used to ensure correct
  86 *                      ownership of chrdevs etc
  87 * @num_interrupt_lines:[DRIVER] number of physical interrupt lines from device
  88 * @interrupts:         [INTERN] interrupt line specific event lists etc
  89 * @event_attrs:        [DRIVER] event control attributes
  90 * @event_conf_attrs:   [DRIVER] event configuration attributes
  91 * @event_interfaces:   [INTERN] event chrdevs associated with interrupt lines
  92 * @ring:               [DRIVER] any ring buffer present
  93 * @mlock:              [INTERN] lock used to prevent simultaneous device state
  94 *                      changes
  95 * @scan_el_attrs:      [DRIVER] control of scan elements if that scan mode
  96 *                      control method is used
  97 * @scan_count: [INTERN] the number of elements in the current scan mode
  98 * @scan_mask:          [INTERN] bitmask used in masking scan mode elements
  99 * @scan_timestamp:     [INTERN] does the scan mode include a timestamp
 100 * @trig:               [INTERN] current device trigger (ring buffer modes)
 101 * @pollfunc:           [DRIVER] function run on trigger being recieved
 102 **/
 103struct iio_dev {
 104        int                             id;
 105        void                            *dev_data;
 106        int                             modes;
 107        int                             currentmode;
 108        struct device                   dev;
 109        const struct attribute_group    *attrs;
 110        struct module                   *driver_module;
 111
 112        int                             num_interrupt_lines;
 113        struct iio_interrupt            **interrupts;
 114        struct attribute_group          *event_attrs;
 115        struct attribute_group          *event_conf_attrs;
 116
 117        struct iio_event_interface      *event_interfaces;
 118
 119        struct iio_ring_buffer          *ring;
 120        struct mutex                    mlock;
 121
 122        struct attribute_group          *scan_el_attrs;
 123        int                             scan_count;
 124
 125        u16                             scan_mask;
 126        bool                            scan_timestamp;
 127        struct iio_trigger              *trig;
 128        struct iio_poll_func            *pollfunc;
 129};
 130
 131/*
 132 * These are mainly provided to allow for a change of implementation if a device
 133 * has a large number of scan elements
 134 */
 135#define IIO_MAX_SCAN_LENGTH 15
 136
 137static inline int iio_scan_mask_query(struct iio_dev *dev_info, int bit)
 138{
 139        if (bit > IIO_MAX_SCAN_LENGTH)
 140                return -EINVAL;
 141        else
 142                return !!(dev_info->scan_mask & (1 << bit));
 143};
 144
 145static inline int iio_scan_mask_set(struct iio_dev *dev_info, int bit)
 146{
 147        if (bit > IIO_MAX_SCAN_LENGTH)
 148                return -EINVAL;
 149        dev_info->scan_mask |= (1 << bit);
 150        dev_info->scan_count++;
 151        return 0;
 152};
 153
 154static inline int iio_scan_mask_clear(struct iio_dev *dev_info, int bit)
 155{
 156        if (bit > IIO_MAX_SCAN_LENGTH)
 157                return -EINVAL;
 158        dev_info->scan_mask &= ~(1 << bit);
 159        dev_info->scan_count--;
 160        return 0;
 161};
 162
 163/**
 164 * iio_scan_mask_count_to_right() - how many scan elements occur before here
 165 * @dev_info: the iio_device whose scan mode we are querying
 166 * @bit: which number scan element is this
 167 **/
 168static inline int iio_scan_mask_count_to_right(struct iio_dev *dev_info,
 169int bit)
 170{
 171        int count = 0;
 172        int mask = (1 << bit);
 173        if (bit > IIO_MAX_SCAN_LENGTH)
 174                return -EINVAL;
 175        while (mask) {
 176                mask >>= 1;
 177                if (mask & dev_info->scan_mask)
 178                        count++;
 179        }
 180
 181        return count;
 182}
 183
 184/**
 185 * iio_device_register() - register a device with the IIO subsystem
 186 * @dev_info:           Device structure filled by the device driver
 187 **/
 188int iio_device_register(struct iio_dev *dev_info);
 189
 190/**
 191 * iio_device_unregister() - unregister a device from the IIO subsystem
 192 * @dev_info:           Device structure representing the device.
 193 **/
 194void iio_device_unregister(struct iio_dev *dev_info);
 195
 196/**
 197 * struct iio_interrupt - wrapper used to allow easy handling of multiple
 198 *                      physical interrupt lines
 199 * @dev_info:           the iio device for which the is an interrupt line
 200 * @line_number:        associated line number
 201 * @id:                 idr allocated unique id number
 202 * @irq:                associate interrupt number
 203 * @ev_list:            event handler list for associated events
 204 * @ev_list_lock:       ensure only one access to list at a time
 205 **/
 206struct iio_interrupt {
 207        struct iio_dev                  *dev_info;
 208        int                             line_number;
 209        int                             id;
 210        int                             irq;
 211        struct list_head                ev_list;
 212        spinlock_t                      ev_list_lock;
 213};
 214
 215#define to_iio_interrupt(i) container_of(i, struct iio_interrupt, ev_list)
 216
 217/**
 218 * iio_register_interrupt_line() - Tell IIO about interrupt lines
 219 *
 220 * @irq:                Typically provided via platform data
 221 * @dev_info:           IIO device info structure for device
 222 * @line_number:        Which interrupt line of the device is this?
 223 * @type:               Interrupt type (e.g. edge triggered etc)
 224 * @name:               Identifying name.
 225 **/
 226int iio_register_interrupt_line(unsigned int                    irq,
 227                                struct iio_dev                  *dev_info,
 228                                int                             line_number,
 229                                unsigned long                   type,
 230                                const char                      *name);
 231
 232void iio_unregister_interrupt_line(struct iio_dev *dev_info,
 233                                   int line_number);
 234
 235
 236
 237/**
 238 * iio_push_event() - try to add event to the list for userspace reading
 239 * @dev_info:           IIO device structure
 240 * @ev_line:            Which event line (hardware interrupt)
 241 * @ev_code:            What event
 242 * @timestamp:          When the event occured
 243 **/
 244int iio_push_event(struct iio_dev *dev_info,
 245                  int ev_line,
 246                  int ev_code,
 247                  s64 timestamp);
 248
 249/**
 250 * struct iio_work_cont - container for when singleton handler case matters
 251 * @ws:                 [DEVICE]work_struct when not only possible event
 252 * @ws_nocheck:         [DEVICE]work_struct when only possible event
 253 * @address:            [DEVICE]associated register address
 254 * @mask:               [DEVICE]associated mask for identifying event source
 255 * @st:                 [DEVICE]device specific state information
 256 **/
 257struct iio_work_cont {
 258        struct work_struct      ws;
 259        struct work_struct      ws_nocheck;
 260        int                     address;
 261        int                     mask;
 262        void                    *st;
 263};
 264
 265#define to_iio_work_cont_check(_ws)                     \
 266        container_of(_ws, struct iio_work_cont, ws)
 267
 268#define to_iio_work_cont_no_check(_ws)                          \
 269        container_of(_ws, struct iio_work_cont, ws_nocheck)
 270
 271/**
 272 * iio_init_work_cont() - intiialize the elements of a work container
 273 * @cont: the work container
 274 * @_checkfunc: function called when there are multiple possible int sources
 275 * @_nocheckfunc: function for when there is only one int source
 276 * @_add: driver dependant, typically a register address
 277 * @_mask: driver dependant, typically a bit mask for a register
 278 * @_st: driver dependant, typically pointer to a device state structure
 279 **/
 280static inline void
 281iio_init_work_cont(struct iio_work_cont *cont,
 282                   void (*_checkfunc)(struct work_struct *),
 283                   void (*_nocheckfunc)(struct work_struct *),
 284                   int _add, int _mask, void *_st)
 285{
 286        INIT_WORK(&(cont)->ws, _checkfunc);
 287        INIT_WORK(&(cont)->ws_nocheck, _nocheckfunc);
 288        cont->address = _add;
 289        cont->mask = _mask;
 290        cont->st = _st;
 291}
 292/**
 293 * __iio_push_event() tries to add an event to the list associated with a chrdev
 294 * @ev_int:             the event interface to which we are pushing the event
 295 * @ev_code:            the outgoing event code
 296 * @timestamp:          timestamp of the event
 297 * @shared_pointer_p:   the shared event pointer
 298 **/
 299int __iio_push_event(struct iio_event_interface *ev_int,
 300                    int ev_code,
 301                    s64 timestamp,
 302                    struct iio_shared_ev_pointer*
 303                    shared_pointer_p);
 304/**
 305 * __iio_change_event() change an event code in case of event escallation
 306 * @ev:                 the evnet to be changed
 307 * @ev_code:            new event code
 308 * @timestamp:          new timestamp
 309 **/
 310void __iio_change_event(struct iio_detected_event_list *ev,
 311                        int ev_code,
 312                        s64 timestamp);
 313
 314/**
 315 * iio_setup_ev_int() Configure an event interface (chrdev)
 316 * @name:               name used for resulting sysfs directory etc.
 317 * @ev_int:             interface we are configuring
 318 * @owner:              module that is responsible for registering this ev_int
 319 * @dev:                device whose ev_int this is
 320 **/
 321int iio_setup_ev_int(struct iio_event_interface *ev_int,
 322                     const char *name,
 323                     struct module *owner,
 324                     struct device *dev);
 325
 326void iio_free_ev_int(struct iio_event_interface *ev_int);
 327
 328/**
 329 * iio_allocate_chrdev() - Allocate a chrdev
 330 * @handler:    struct that contains relevant file handling for chrdev
 331 * @dev_info:   iio_dev for which chrdev is being created
 332 **/
 333int iio_allocate_chrdev(struct iio_handler *handler, struct iio_dev *dev_info);
 334void iio_deallocate_chrdev(struct iio_handler *handler);
 335
 336/* Used to distinguish between bipolar and unipolar scan elemenents.
 337 * Whilst this may seem obvious, we may well want to change the representation
 338 * in the future!*/
 339#define IIO_SIGNED(a) -(a)
 340#define IIO_UNSIGNED(a) (a)
 341
 342extern dev_t iio_devt;
 343extern struct class iio_class;
 344
 345/**
 346 * iio_put_device() - reference counted deallocated of struct device
 347 * @dev: the iio_device containing the device
 348 **/
 349static inline void iio_put_device(struct iio_dev *dev)
 350{
 351        if (dev)
 352                put_device(&dev->dev);
 353};
 354
 355/**
 356 * to_iio_dev() - get iio_dev for which we have have the struct device
 357 * @d: the struct device
 358 **/
 359static inline struct iio_dev *to_iio_dev(struct device *d)
 360{
 361        return container_of(d, struct iio_dev, dev);
 362};
 363
 364/**
 365 * iio_dev_get_devdata() - helper function gets device specific data
 366 * @d: the iio_dev associated with the device
 367 **/
 368static inline void *iio_dev_get_devdata(struct iio_dev *d)
 369{
 370        return d->dev_data;
 371}
 372
 373/**
 374 * iio_allocate_device() - allocate an iio_dev from a driver
 375 **/
 376struct iio_dev *iio_allocate_device(void);
 377
 378/**
 379 * iio_free_device() - free an iio_dev from a driver
 380 **/
 381void iio_free_device(struct iio_dev *dev);
 382
 383/**
 384 * iio_put() - internal module reference count reduce
 385 **/
 386void iio_put(void);
 387
 388/**
 389 * iio_get() - internal module reference count increase
 390 **/
 391void iio_get(void);
 392
 393/* Ring buffer related */
 394int iio_device_get_chrdev_minor(void);
 395void iio_device_free_chrdev_minor(int val);
 396
 397/**
 398 * iio_ring_enabled() helper function to test if any form of ring enabled
 399 **/
 400static inline bool iio_ring_enabled(struct iio_dev *dev_info)
 401{
 402        return dev_info->currentmode
 403                & (INDIO_RING_TRIGGERED
 404                   | INDIO_RING_HARDWARE_BUFFER);
 405};
 406
 407struct idr;
 408
 409int iio_get_new_idr_val(struct idr *this_idr);
 410void iio_free_idr_val(struct idr *this_idr, int id);
 411#endif /* _INDUSTRIAL_IO_H_ */
 412