]> Git Repo - linux.git/blob - drivers/iio/common/st_sensors/st_sensors_trigger.c
efi/libstub: Rewrite file I/O routine
[linux.git] / drivers / iio / common / st_sensors / st_sensors_trigger.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * STMicroelectronics sensors trigger library driver
4  *
5  * Copyright 2012-2013 STMicroelectronics Inc.
6  *
7  * Denis Ciocca <[email protected]>
8  */
9
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/slab.h>
13 #include <linux/iio/iio.h>
14 #include <linux/iio/trigger.h>
15 #include <linux/interrupt.h>
16 #include <linux/regmap.h>
17 #include <linux/iio/common/st_sensors.h>
18 #include "st_sensors_core.h"
19
20 /**
21  * st_sensors_new_samples_available() - check if more samples came in
22  * @indio_dev: IIO device reference.
23  * @sdata: Sensor data.
24  *
25  * returns:
26  * 0 - no new samples available
27  * 1 - new samples available
28  * negative - error or unknown
29  */
30 static int st_sensors_new_samples_available(struct iio_dev *indio_dev,
31                                             struct st_sensor_data *sdata)
32 {
33         int ret, status;
34
35         /* How would I know if I can't check it? */
36         if (!sdata->sensor_settings->drdy_irq.stat_drdy.addr)
37                 return -EINVAL;
38
39         /* No scan mask, no interrupt */
40         if (!indio_dev->active_scan_mask)
41                 return 0;
42
43         ret = regmap_read(sdata->regmap,
44                           sdata->sensor_settings->drdy_irq.stat_drdy.addr,
45                           &status);
46         if (ret < 0) {
47                 dev_err(sdata->dev,
48                         "error checking samples available\n");
49                 return ret;
50         }
51
52         if (status & sdata->sensor_settings->drdy_irq.stat_drdy.mask)
53                 return 1;
54
55         return 0;
56 }
57
58 /**
59  * st_sensors_irq_handler() - top half of the IRQ-based triggers
60  * @irq: irq number
61  * @p: private handler data
62  */
63 static irqreturn_t st_sensors_irq_handler(int irq, void *p)
64 {
65         struct iio_trigger *trig = p;
66         struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
67         struct st_sensor_data *sdata = iio_priv(indio_dev);
68
69         /* Get the time stamp as close in time as possible */
70         sdata->hw_timestamp = iio_get_time_ns(indio_dev);
71         return IRQ_WAKE_THREAD;
72 }
73
74 /**
75  * st_sensors_irq_thread() - bottom half of the IRQ-based triggers
76  * @irq: irq number
77  * @p: private handler data
78  */
79 static irqreturn_t st_sensors_irq_thread(int irq, void *p)
80 {
81         struct iio_trigger *trig = p;
82         struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
83         struct st_sensor_data *sdata = iio_priv(indio_dev);
84
85         /*
86          * If this trigger is backed by a hardware interrupt and we have a
87          * status register, check if this IRQ came from us. Notice that
88          * we will process also if st_sensors_new_samples_available()
89          * returns negative: if we can't check status, then poll
90          * unconditionally.
91          */
92         if (sdata->hw_irq_trigger &&
93             st_sensors_new_samples_available(indio_dev, sdata)) {
94                 iio_trigger_poll_chained(p);
95         } else {
96                 dev_dbg(sdata->dev, "spurious IRQ\n");
97                 return IRQ_NONE;
98         }
99
100         /*
101          * If we have proper level IRQs the handler will be re-entered if
102          * the line is still active, so return here and come back in through
103          * the top half if need be.
104          */
105         if (!sdata->edge_irq)
106                 return IRQ_HANDLED;
107
108         /*
109          * If we are using edge IRQs, new samples arrived while processing
110          * the IRQ and those may be missed unless we pick them here, so poll
111          * again. If the sensor delivery frequency is very high, this thread
112          * turns into a polled loop handler.
113          */
114         while (sdata->hw_irq_trigger &&
115                st_sensors_new_samples_available(indio_dev, sdata)) {
116                 dev_dbg(sdata->dev, "more samples came in during polling\n");
117                 sdata->hw_timestamp = iio_get_time_ns(indio_dev);
118                 iio_trigger_poll_chained(p);
119         }
120
121         return IRQ_HANDLED;
122 }
123
124 int st_sensors_allocate_trigger(struct iio_dev *indio_dev,
125                                 const struct iio_trigger_ops *trigger_ops)
126 {
127         struct st_sensor_data *sdata = iio_priv(indio_dev);
128         unsigned long irq_trig;
129         int err;
130
131         sdata->trig = iio_trigger_alloc("%s-trigger", indio_dev->name);
132         if (sdata->trig == NULL) {
133                 dev_err(&indio_dev->dev, "failed to allocate iio trigger.\n");
134                 return -ENOMEM;
135         }
136
137         iio_trigger_set_drvdata(sdata->trig, indio_dev);
138         sdata->trig->ops = trigger_ops;
139         sdata->trig->dev.parent = sdata->dev;
140
141         irq_trig = irqd_get_trigger_type(irq_get_irq_data(sdata->irq));
142         /*
143          * If the IRQ is triggered on falling edge, we need to mark the
144          * interrupt as active low, if the hardware supports this.
145          */
146         switch(irq_trig) {
147         case IRQF_TRIGGER_FALLING:
148         case IRQF_TRIGGER_LOW:
149                 if (!sdata->sensor_settings->drdy_irq.addr_ihl) {
150                         dev_err(&indio_dev->dev,
151                                 "falling/low specified for IRQ "
152                                 "but hardware supports only rising/high: "
153                                 "will request rising/high\n");
154                         if (irq_trig == IRQF_TRIGGER_FALLING)
155                                 irq_trig = IRQF_TRIGGER_RISING;
156                         if (irq_trig == IRQF_TRIGGER_LOW)
157                                 irq_trig = IRQF_TRIGGER_HIGH;
158                 } else {
159                         /* Set up INT active low i.e. falling edge */
160                         err = st_sensors_write_data_with_mask(indio_dev,
161                                 sdata->sensor_settings->drdy_irq.addr_ihl,
162                                 sdata->sensor_settings->drdy_irq.mask_ihl, 1);
163                         if (err < 0)
164                                 goto iio_trigger_free;
165                         dev_info(&indio_dev->dev,
166                                  "interrupts on the falling edge or "
167                                  "active low level\n");
168                 }
169                 break;
170         case IRQF_TRIGGER_RISING:
171                 dev_info(&indio_dev->dev,
172                          "interrupts on the rising edge\n");
173                 break;
174         case IRQF_TRIGGER_HIGH:
175                 dev_info(&indio_dev->dev,
176                          "interrupts active high level\n");
177                 break;
178         default:
179                 /* This is the most preferred mode, if possible */
180                 dev_err(&indio_dev->dev,
181                         "unsupported IRQ trigger specified (%lx), enforce "
182                         "rising edge\n", irq_trig);
183                 irq_trig = IRQF_TRIGGER_RISING;
184         }
185
186         /* Tell the interrupt handler that we're dealing with edges */
187         if (irq_trig == IRQF_TRIGGER_FALLING ||
188             irq_trig == IRQF_TRIGGER_RISING)
189                 sdata->edge_irq = true;
190         else
191                 /*
192                  * If we're not using edges (i.e. level interrupts) we
193                  * just mask off the IRQ, handle one interrupt, then
194                  * if the line is still low, we return to the
195                  * interrupt handler top half again and start over.
196                  */
197                 irq_trig |= IRQF_ONESHOT;
198
199         /*
200          * If the interrupt pin is Open Drain, by definition this
201          * means that the interrupt line may be shared with other
202          * peripherals. But to do this we also need to have a status
203          * register and mask to figure out if this sensor was firing
204          * the IRQ or not, so we can tell the interrupt handle that
205          * it was "our" interrupt.
206          */
207         if (sdata->int_pin_open_drain &&
208             sdata->sensor_settings->drdy_irq.stat_drdy.addr)
209                 irq_trig |= IRQF_SHARED;
210
211         err = request_threaded_irq(sdata->irq,
212                                    st_sensors_irq_handler,
213                                    st_sensors_irq_thread,
214                                    irq_trig,
215                                    sdata->trig->name,
216                                    sdata->trig);
217         if (err) {
218                 dev_err(&indio_dev->dev, "failed to request trigger IRQ.\n");
219                 goto iio_trigger_free;
220         }
221
222         err = iio_trigger_register(sdata->trig);
223         if (err < 0) {
224                 dev_err(&indio_dev->dev, "failed to register iio trigger.\n");
225                 goto iio_trigger_register_error;
226         }
227         indio_dev->trig = iio_trigger_get(sdata->trig);
228
229         return 0;
230
231 iio_trigger_register_error:
232         free_irq(sdata->irq, sdata->trig);
233 iio_trigger_free:
234         iio_trigger_free(sdata->trig);
235         return err;
236 }
237 EXPORT_SYMBOL(st_sensors_allocate_trigger);
238
239 void st_sensors_deallocate_trigger(struct iio_dev *indio_dev)
240 {
241         struct st_sensor_data *sdata = iio_priv(indio_dev);
242
243         iio_trigger_unregister(sdata->trig);
244         free_irq(sdata->irq, sdata->trig);
245         iio_trigger_free(sdata->trig);
246 }
247 EXPORT_SYMBOL(st_sensors_deallocate_trigger);
248
249 int st_sensors_validate_device(struct iio_trigger *trig,
250                                struct iio_dev *indio_dev)
251 {
252         struct iio_dev *indio = iio_trigger_get_drvdata(trig);
253
254         if (indio != indio_dev)
255                 return -EINVAL;
256
257         return 0;
258 }
259 EXPORT_SYMBOL(st_sensors_validate_device);
260
261 MODULE_AUTHOR("Denis Ciocca <[email protected]>");
262 MODULE_DESCRIPTION("STMicroelectronics ST-sensors trigger");
263 MODULE_LICENSE("GPL v2");
This page took 0.073602 seconds and 4 git commands to generate.