Merge branch 'for-linus' of git://neil.brown.name/md
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / staging / iio / trigger.h
1 /* The industrial I/O core, trigger handling functions
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 #ifndef _IIO_TRIGGER_H_
10 #define _IIO_TRIGGER_H_
11 #define IIO_TRIGGER_NAME_LENGTH 20
12 #define IIO_TRIGGER_ID_PREFIX "iio:trigger"
13 #define IIO_TRIGGER_ID_FORMAT IIO_TRIGGER_ID_PREFIX "%d"
14
15
16 /**
17 * struct iio_trigger - industrial I/O trigger device
18 *
19 * @id: [INTERN] unique id number
20 * @name: [DRIVER] unique name
21 * @dev: [DRIVER] associated device (if relevant)
22 * @private_data: [DRIVER] device specific data
23 * @list: [INTERN] used in maintenance of global trigger list
24 * @alloc_list: [DRIVER] used for driver specific trigger list
25 * @pollfunc_list_lock: [INTERN] protection of the polling function list
26 * @pollfunc_list: [INTERN] list of functions to run on trigger.
27 * @control_attrs: [DRIVER] sysfs attributes relevant to trigger type
28 * @timestamp: [INTERN] timestamp usesd by some trigs (e.g. datardy)
29 * @owner: [DRIVER] used to monitor usage count of the trigger.
30 * @use_count: use count for the trigger
31 * @set_trigger_state: [DRIVER] switch on/off the trigger on demand
32 * @try_reenable: function to reenable the trigger when the
33 * use count is zero (may be NULL)
34 **/
35 struct iio_trigger {
36 int id;
37 const char *name;
38 struct device dev;
39
40 void *private_data;
41 struct list_head list;
42 struct list_head alloc_list;
43 spinlock_t pollfunc_list_lock;
44 struct list_head pollfunc_list;
45 const struct attribute_group *control_attrs;
46 s64 timestamp;
47 struct module *owner;
48 int use_count;
49
50 int (*set_trigger_state)(struct iio_trigger *trig, bool state);
51 int (*try_reenable)(struct iio_trigger *trig);
52 };
53
54 static inline struct iio_trigger *to_iio_trigger(struct device *d)
55 {
56 return container_of(d, struct iio_trigger, dev);
57 };
58
59 static inline void iio_put_trigger(struct iio_trigger *trig)
60 {
61 put_device(&trig->dev);
62 module_put(trig->owner);
63 };
64
65 static inline void iio_get_trigger(struct iio_trigger *trig)
66 {
67 __module_get(trig->owner);
68 get_device(&trig->dev);
69 };
70
71 /**
72 * iio_trigger_read_name() - sysfs access function to get the trigger name
73 * @dev: the system device
74 * @attr: device attributes for the device
75 * @buf: output buffer to store the trigger name
76 **/
77 ssize_t iio_trigger_read_name(struct device *dev,
78 struct device_attribute *attr,
79 char *buf);
80
81 #define IIO_TRIGGER_NAME_ATTR DEVICE_ATTR(name, S_IRUGO, \
82 iio_trigger_read_name, \
83 NULL);
84
85 /**
86 * iio_trigger_find_by_name() - search global trigger list
87 * @name: trigger name to search for
88 * @len: trigger name string length to compare
89 **/
90 struct iio_trigger *iio_trigger_find_by_name(const char *name, size_t len);
91
92 /**
93 * iio_trigger_register() - register a trigger with the IIO core
94 * @trig_info: trigger to be registered
95 **/
96 int iio_trigger_register(struct iio_trigger *trig_info);
97
98 /**
99 * iio_trigger_unregister() - unregister a trigger from the core
100 * @trig_info: trigger to be unregistered
101 **/
102 void iio_trigger_unregister(struct iio_trigger *trig_info);
103
104 /**
105 * iio_trigger_attach_poll_func() - add a function pair to be run on trigger
106 * @trig: trigger to which the function pair are being added
107 * @pf: poll function pair
108 **/
109 int iio_trigger_attach_poll_func(struct iio_trigger *trig,
110 struct iio_poll_func *pf);
111
112 /**
113 * iio_trigger_dettach_poll_func() - remove function pair from those to be
114 * run on trigger
115 * @trig: trigger from which the function is being removed
116 * @pf: poll function pair
117 **/
118 int iio_trigger_dettach_poll_func(struct iio_trigger *trig,
119 struct iio_poll_func *pf);
120
121 /**
122 * iio_trigger_poll() - called on a trigger occurring
123 * @trig: trigger which occurred
124 *
125 * Typically called in relevant hardware interrupt handler.
126 **/
127 void iio_trigger_poll(struct iio_trigger *trig);
128 void iio_trigger_notify_done(struct iio_trigger *trig);
129
130 /**
131 * struct iio_poll_func - poll function pair
132 *
133 * @list: associate this with a triggers pollfunc_list
134 * @private_data: data specific to device (passed into poll func)
135 * @poll_func_immediate: function in here is run first. They should be
136 * extremely lightweight. Typically used for latch
137 * control on sensor supporting it.
138 * @poll_func_main: function in here is run after all immediates.
139 * Reading from sensor etc typically involves
140 * scheduling from here.
141 *
142 * The two stage approach used here is only important when multiple sensors are
143 * being triggered by a single trigger. This really comes into its own with
144 * simultaneous sampling devices where a simple latch command can be used to
145 * make the device store the values on all inputs.
146 **/
147 struct iio_poll_func {
148 struct list_head list;
149 void *private_data;
150 void (*poll_func_immediate)(struct iio_dev *indio_dev);
151 void (*poll_func_main)(struct iio_dev *private_data);
152
153 };
154
155 struct iio_trigger *iio_allocate_trigger(void);
156
157 void iio_free_trigger(struct iio_trigger *trig);
158
159
160 #endif /* _IIO_TRIGGER_H_ */