Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-only */
0002 /* The industrial I/O core, trigger handling functions
0003  *
0004  * Copyright (c) 2008 Jonathan Cameron
0005  */
0006 #include <linux/irq.h>
0007 #include <linux/module.h>
0008 #include <linux/atomic.h>
0009 
0010 #ifndef _IIO_TRIGGER_H_
0011 #define _IIO_TRIGGER_H_
0012 
0013 #ifdef CONFIG_IIO_TRIGGER
0014 struct iio_subirq {
0015     bool enabled;
0016 };
0017 
0018 struct iio_dev;
0019 struct iio_trigger;
0020 
0021 /**
0022  * struct iio_trigger_ops - operations structure for an iio_trigger.
0023  * @set_trigger_state:  switch on/off the trigger on demand
0024  * @reenable:       function to reenable the trigger when the
0025  *          use count is zero (may be NULL)
0026  * @validate_device:    function to validate the device when the
0027  *          current trigger gets changed.
0028  *
0029  * This is typically static const within a driver and shared by
0030  * instances of a given device.
0031  **/
0032 struct iio_trigger_ops {
0033     int (*set_trigger_state)(struct iio_trigger *trig, bool state);
0034     void (*reenable)(struct iio_trigger *trig);
0035     int (*validate_device)(struct iio_trigger *trig,
0036                    struct iio_dev *indio_dev);
0037 };
0038 
0039 
0040 /**
0041  * struct iio_trigger - industrial I/O trigger device
0042  * @ops:        [DRIVER] operations structure
0043  * @owner:      [INTERN] owner of this driver module
0044  * @id:         [INTERN] unique id number
0045  * @name:       [DRIVER] unique name
0046  * @dev:        [DRIVER] associated device (if relevant)
0047  * @list:       [INTERN] used in maintenance of global trigger list
0048  * @alloc_list:     [DRIVER] used for driver specific trigger list
0049  * @use_count:      [INTERN] use count for the trigger.
0050  * @subirq_chip:    [INTERN] associate 'virtual' irq chip.
0051  * @subirq_base:    [INTERN] base number for irqs provided by trigger.
0052  * @subirqs:        [INTERN] information about the 'child' irqs.
0053  * @pool:       [INTERN] bitmap of irqs currently in use.
0054  * @pool_lock:      [INTERN] protection of the irq pool.
0055  * @attached_own_device:[INTERN] if we are using our own device as trigger,
0056  *          i.e. if we registered a poll function to the same
0057  *          device as the one providing the trigger.
0058  * @reenable_work:  [INTERN] work item used to ensure reenable can sleep.
0059  **/
0060 struct iio_trigger {
0061     const struct iio_trigger_ops    *ops;
0062     struct module           *owner;
0063     int             id;
0064     const char          *name;
0065     struct device           dev;
0066 
0067     struct list_head        list;
0068     struct list_head        alloc_list;
0069     atomic_t            use_count;
0070 
0071     struct irq_chip         subirq_chip;
0072     int             subirq_base;
0073 
0074     struct iio_subirq subirqs[CONFIG_IIO_CONSUMERS_PER_TRIGGER];
0075     unsigned long pool[BITS_TO_LONGS(CONFIG_IIO_CONSUMERS_PER_TRIGGER)];
0076     struct mutex            pool_lock;
0077     bool                attached_own_device;
0078     struct work_struct      reenable_work;
0079 };
0080 
0081 
0082 static inline struct iio_trigger *to_iio_trigger(struct device *d)
0083 {
0084     return container_of(d, struct iio_trigger, dev);
0085 }
0086 
0087 static inline void iio_trigger_put(struct iio_trigger *trig)
0088 {
0089     module_put(trig->owner);
0090     put_device(&trig->dev);
0091 }
0092 
0093 static inline struct iio_trigger *iio_trigger_get(struct iio_trigger *trig)
0094 {
0095     get_device(&trig->dev);
0096 
0097     WARN_ONCE(list_empty(&trig->list),
0098           "Getting non-registered iio trigger %s is prohibited\n",
0099           trig->name);
0100 
0101     __module_get(trig->owner);
0102 
0103     return trig;
0104 }
0105 
0106 /**
0107  * iio_trigger_set_drvdata() - Set trigger driver data
0108  * @trig: IIO trigger structure
0109  * @data: Driver specific data
0110  *
0111  * Allows to attach an arbitrary pointer to an IIO trigger, which can later be
0112  * retrieved by iio_trigger_get_drvdata().
0113  */
0114 static inline void iio_trigger_set_drvdata(struct iio_trigger *trig, void *data)
0115 {
0116     dev_set_drvdata(&trig->dev, data);
0117 }
0118 
0119 /**
0120  * iio_trigger_get_drvdata() - Get trigger driver data
0121  * @trig: IIO trigger structure
0122  *
0123  * Returns the data previously set with iio_trigger_set_drvdata()
0124  */
0125 static inline void *iio_trigger_get_drvdata(struct iio_trigger *trig)
0126 {
0127     return dev_get_drvdata(&trig->dev);
0128 }
0129 
0130 /**
0131  * iio_trigger_register() - register a trigger with the IIO core
0132  * @trig_info:  trigger to be registered
0133  **/
0134 int iio_trigger_register(struct iio_trigger *trig_info);
0135 
0136 int devm_iio_trigger_register(struct device *dev,
0137                   struct iio_trigger *trig_info);
0138 
0139 /**
0140  * iio_trigger_unregister() - unregister a trigger from the core
0141  * @trig_info:  trigger to be unregistered
0142  **/
0143 void iio_trigger_unregister(struct iio_trigger *trig_info);
0144 
0145 /**
0146  * iio_trigger_set_immutable() - set an immutable trigger on destination
0147  *
0148  * @indio_dev: IIO device structure containing the device
0149  * @trig: trigger to assign to device
0150  *
0151  **/
0152 int iio_trigger_set_immutable(struct iio_dev *indio_dev, struct iio_trigger *trig);
0153 
0154 /**
0155  * iio_trigger_poll() - called on a trigger occurring
0156  * @trig:   trigger which occurred
0157  *
0158  * Typically called in relevant hardware interrupt handler.
0159  **/
0160 void iio_trigger_poll(struct iio_trigger *trig);
0161 void iio_trigger_poll_chained(struct iio_trigger *trig);
0162 
0163 irqreturn_t iio_trigger_generic_data_rdy_poll(int irq, void *private);
0164 
0165 #define iio_trigger_alloc(parent, fmt, ...) \
0166     __iio_trigger_alloc((parent), THIS_MODULE, (fmt), ##__VA_ARGS__)
0167 
0168 __printf(3, 4)
0169 struct iio_trigger *__iio_trigger_alloc(struct device *parent,
0170                     struct module *this_mod,
0171                     const char *fmt, ...);
0172 void iio_trigger_free(struct iio_trigger *trig);
0173 
0174 /**
0175  * iio_trigger_using_own() - tells us if we use our own HW trigger ourselves
0176  * @indio_dev:  device to check
0177  */
0178 bool iio_trigger_using_own(struct iio_dev *indio_dev);
0179 
0180 int iio_trigger_validate_own_device(struct iio_trigger *trig,
0181                      struct iio_dev *indio_dev);
0182 
0183 #else
0184 struct iio_trigger;
0185 struct iio_trigger_ops;
0186 #endif
0187 #endif /* _IIO_TRIGGER_H_ */