1
2
3
4
5
6
7
8
9
10#include <linux/kernel.h>
11#include <linux/module.h>
12#include <linux/iio/iio.h>
13#include <linux/iio/trigger.h>
14#include <linux/interrupt.h>
15#include <linux/regmap.h>
16#include <linux/iio/common/st_sensors.h>
17#include "st_sensors_core.h"
18
19
20
21
22
23
24
25
26
27
28static bool st_sensors_new_samples_available(struct iio_dev *indio_dev,
29 struct st_sensor_data *sdata)
30{
31 int ret, status;
32
33
34 if (!sdata->sensor_settings->drdy_irq.stat_drdy.addr)
35 return true;
36
37
38 if (!indio_dev->active_scan_mask)
39 return false;
40
41 ret = regmap_read(sdata->regmap,
42 sdata->sensor_settings->drdy_irq.stat_drdy.addr,
43 &status);
44 if (ret < 0) {
45 dev_err(sdata->dev, "error checking samples available\n");
46 return false;
47 }
48
49 return !!(status & sdata->sensor_settings->drdy_irq.stat_drdy.mask);
50}
51
52
53
54
55
56
57static irqreturn_t st_sensors_irq_handler(int irq, void *p)
58{
59 struct iio_trigger *trig = p;
60 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
61 struct st_sensor_data *sdata = iio_priv(indio_dev);
62
63
64 sdata->hw_timestamp = iio_get_time_ns(indio_dev);
65 return IRQ_WAKE_THREAD;
66}
67
68
69
70
71
72
73static irqreturn_t st_sensors_irq_thread(int irq, void *p)
74{
75 struct iio_trigger *trig = p;
76 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
77 struct st_sensor_data *sdata = iio_priv(indio_dev);
78
79
80
81
82
83
84
85
86 if (sdata->hw_irq_trigger &&
87 st_sensors_new_samples_available(indio_dev, sdata)) {
88 iio_trigger_poll_chained(p);
89 } else {
90 dev_dbg(sdata->dev, "spurious IRQ\n");
91 return IRQ_NONE;
92 }
93
94
95
96
97
98
99 if (!sdata->edge_irq)
100 return IRQ_HANDLED;
101
102
103
104
105
106
107
108 while (sdata->hw_irq_trigger &&
109 st_sensors_new_samples_available(indio_dev, sdata)) {
110 dev_dbg(sdata->dev, "more samples came in during polling\n");
111 sdata->hw_timestamp = iio_get_time_ns(indio_dev);
112 iio_trigger_poll_chained(p);
113 }
114
115 return IRQ_HANDLED;
116}
117
118int st_sensors_allocate_trigger(struct iio_dev *indio_dev,
119 const struct iio_trigger_ops *trigger_ops)
120{
121 struct st_sensor_data *sdata = iio_priv(indio_dev);
122 unsigned long irq_trig;
123 int err;
124
125 sdata->trig = iio_trigger_alloc(sdata->dev, "%s-trigger",
126 indio_dev->name);
127 if (sdata->trig == NULL) {
128 dev_err(&indio_dev->dev, "failed to allocate iio trigger.\n");
129 return -ENOMEM;
130 }
131
132 iio_trigger_set_drvdata(sdata->trig, indio_dev);
133 sdata->trig->ops = trigger_ops;
134
135 irq_trig = irqd_get_trigger_type(irq_get_irq_data(sdata->irq));
136
137
138
139
140 switch(irq_trig) {
141 case IRQF_TRIGGER_FALLING:
142 case IRQF_TRIGGER_LOW:
143 if (!sdata->sensor_settings->drdy_irq.addr_ihl) {
144 dev_err(&indio_dev->dev,
145 "falling/low specified for IRQ but hardware supports only rising/high: will request rising/high\n");
146 if (irq_trig == IRQF_TRIGGER_FALLING)
147 irq_trig = IRQF_TRIGGER_RISING;
148 if (irq_trig == IRQF_TRIGGER_LOW)
149 irq_trig = IRQF_TRIGGER_HIGH;
150 } else {
151
152 err = st_sensors_write_data_with_mask(indio_dev,
153 sdata->sensor_settings->drdy_irq.addr_ihl,
154 sdata->sensor_settings->drdy_irq.mask_ihl, 1);
155 if (err < 0)
156 goto iio_trigger_free;
157 dev_info(&indio_dev->dev,
158 "interrupts on the falling edge or active low level\n");
159 }
160 break;
161 case IRQF_TRIGGER_RISING:
162 dev_info(&indio_dev->dev,
163 "interrupts on the rising edge\n");
164 break;
165 case IRQF_TRIGGER_HIGH:
166 dev_info(&indio_dev->dev,
167 "interrupts active high level\n");
168 break;
169 default:
170
171 dev_err(&indio_dev->dev,
172 "unsupported IRQ trigger specified (%lx), enforce rising edge\n", irq_trig);
173 irq_trig = IRQF_TRIGGER_RISING;
174 }
175
176
177 if (irq_trig == IRQF_TRIGGER_FALLING ||
178 irq_trig == IRQF_TRIGGER_RISING) {
179 if (!sdata->sensor_settings->drdy_irq.stat_drdy.addr) {
180 dev_err(&indio_dev->dev,
181 "edge IRQ not supported w/o stat register.\n");
182 err = -EOPNOTSUPP;
183 goto iio_trigger_free;
184 }
185 sdata->edge_irq = true;
186 } else {
187
188
189
190
191
192
193 irq_trig |= IRQF_ONESHOT;
194 }
195
196
197
198
199
200
201
202
203
204 if (sdata->int_pin_open_drain &&
205 sdata->sensor_settings->drdy_irq.stat_drdy.addr)
206 irq_trig |= IRQF_SHARED;
207
208 err = request_threaded_irq(sdata->irq,
209 st_sensors_irq_handler,
210 st_sensors_irq_thread,
211 irq_trig,
212 sdata->trig->name,
213 sdata->trig);
214 if (err) {
215 dev_err(&indio_dev->dev, "failed to request trigger IRQ.\n");
216 goto iio_trigger_free;
217 }
218
219 err = iio_trigger_register(sdata->trig);
220 if (err < 0) {
221 dev_err(&indio_dev->dev, "failed to register iio trigger.\n");
222 goto iio_trigger_register_error;
223 }
224 indio_dev->trig = iio_trigger_get(sdata->trig);
225
226 return 0;
227
228iio_trigger_register_error:
229 free_irq(sdata->irq, sdata->trig);
230iio_trigger_free:
231 iio_trigger_free(sdata->trig);
232 return err;
233}
234EXPORT_SYMBOL(st_sensors_allocate_trigger);
235
236void st_sensors_deallocate_trigger(struct iio_dev *indio_dev)
237{
238 struct st_sensor_data *sdata = iio_priv(indio_dev);
239
240 iio_trigger_unregister(sdata->trig);
241 free_irq(sdata->irq, sdata->trig);
242 iio_trigger_free(sdata->trig);
243}
244EXPORT_SYMBOL(st_sensors_deallocate_trigger);
245
246int st_sensors_validate_device(struct iio_trigger *trig,
247 struct iio_dev *indio_dev)
248{
249 struct iio_dev *indio = iio_trigger_get_drvdata(trig);
250
251 if (indio != indio_dev)
252 return -EINVAL;
253
254 return 0;
255}
256EXPORT_SYMBOL(st_sensors_validate_device);
257
258MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
259MODULE_DESCRIPTION("STMicroelectronics ST-sensors trigger");
260MODULE_LICENSE("GPL v2");
261