1 // SPDX-License-Identifier: GPL-2.0-only
3 * STMicroelectronics sensors trigger library driver
5 * Copyright 2012-2013 STMicroelectronics Inc.
7 * Denis Ciocca <denis.ciocca@st.com>
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/iio/common/st_sensors.h>
17 #include "st_sensors_core.h"
20 * st_sensors_new_samples_available() - check if more samples came in
22 * 0 - no new samples available
23 * 1 - new samples available
24 * negative - error or unknown
26 static int st_sensors_new_samples_available(struct iio_dev *indio_dev,
27 struct st_sensor_data *sdata)
32 /* How would I know if I can't check it? */
33 if (!sdata->sensor_settings->drdy_irq.stat_drdy.addr)
36 /* No scan mask, no interrupt */
37 if (!indio_dev->active_scan_mask)
40 ret = sdata->tf->read_byte(&sdata->tb, sdata->dev,
41 sdata->sensor_settings->drdy_irq.stat_drdy.addr,
45 "error checking samples available\n");
49 if (status & sdata->sensor_settings->drdy_irq.stat_drdy.mask)
56 * st_sensors_irq_handler() - top half of the IRQ-based triggers
58 * @p: private handler data
60 static irqreturn_t st_sensors_irq_handler(int irq, void *p)
62 struct iio_trigger *trig = p;
63 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
64 struct st_sensor_data *sdata = iio_priv(indio_dev);
66 /* Get the time stamp as close in time as possible */
67 sdata->hw_timestamp = iio_get_time_ns(indio_dev);
68 return IRQ_WAKE_THREAD;
72 * st_sensors_irq_thread() - bottom half of the IRQ-based triggers
74 * @p: private handler data
76 static irqreturn_t st_sensors_irq_thread(int irq, void *p)
78 struct iio_trigger *trig = p;
79 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
80 struct st_sensor_data *sdata = iio_priv(indio_dev);
83 * If this trigger is backed by a hardware interrupt and we have a
84 * status register, check if this IRQ came from us. Notice that
85 * we will process also if st_sensors_new_samples_available()
86 * returns negative: if we can't check status, then poll
89 if (sdata->hw_irq_trigger &&
90 st_sensors_new_samples_available(indio_dev, sdata)) {
91 iio_trigger_poll_chained(p);
93 dev_dbg(sdata->dev, "spurious IRQ\n");
98 * If we have proper level IRQs the handler will be re-entered if
99 * the line is still active, so return here and come back in through
100 * the top half if need be.
102 if (!sdata->edge_irq)
106 * If we are using edge IRQs, new samples arrived while processing
107 * the IRQ and those may be missed unless we pick them here, so poll
108 * again. If the sensor delivery frequency is very high, this thread
109 * turns into a polled loop handler.
111 while (sdata->hw_irq_trigger &&
112 st_sensors_new_samples_available(indio_dev, sdata)) {
113 dev_dbg(sdata->dev, "more samples came in during polling\n");
114 sdata->hw_timestamp = iio_get_time_ns(indio_dev);
115 iio_trigger_poll_chained(p);
121 int st_sensors_allocate_trigger(struct iio_dev *indio_dev,
122 const struct iio_trigger_ops *trigger_ops)
125 struct st_sensor_data *sdata = iio_priv(indio_dev);
126 unsigned long irq_trig;
128 sdata->trig = iio_trigger_alloc("%s-trigger", indio_dev->name);
129 if (sdata->trig == NULL) {
130 dev_err(&indio_dev->dev, "failed to allocate iio trigger.\n");
134 iio_trigger_set_drvdata(sdata->trig, indio_dev);
135 sdata->trig->ops = trigger_ops;
136 sdata->trig->dev.parent = sdata->dev;
138 irq = sdata->get_irq_data_ready(indio_dev);
139 irq_trig = irqd_get_trigger_type(irq_get_irq_data(irq));
141 * If the IRQ is triggered on falling edge, we need to mark the
142 * interrupt as active low, if the hardware supports this.
145 case IRQF_TRIGGER_FALLING:
146 case IRQF_TRIGGER_LOW:
147 if (!sdata->sensor_settings->drdy_irq.addr_ihl) {
148 dev_err(&indio_dev->dev,
149 "falling/low specified for IRQ "
150 "but hardware supports only rising/high: "
151 "will request rising/high\n");
152 if (irq_trig == IRQF_TRIGGER_FALLING)
153 irq_trig = IRQF_TRIGGER_RISING;
154 if (irq_trig == IRQF_TRIGGER_LOW)
155 irq_trig = IRQF_TRIGGER_HIGH;
157 /* Set up INT active low i.e. falling edge */
158 err = st_sensors_write_data_with_mask(indio_dev,
159 sdata->sensor_settings->drdy_irq.addr_ihl,
160 sdata->sensor_settings->drdy_irq.mask_ihl, 1);
162 goto iio_trigger_free;
163 dev_info(&indio_dev->dev,
164 "interrupts on the falling edge or "
165 "active low level\n");
168 case IRQF_TRIGGER_RISING:
169 dev_info(&indio_dev->dev,
170 "interrupts on the rising edge\n");
172 case IRQF_TRIGGER_HIGH:
173 dev_info(&indio_dev->dev,
174 "interrupts active high level\n");
177 /* This is the most preferred mode, if possible */
178 dev_err(&indio_dev->dev,
179 "unsupported IRQ trigger specified (%lx), enforce "
180 "rising edge\n", irq_trig);
181 irq_trig = IRQF_TRIGGER_RISING;
184 /* Tell the interrupt handler that we're dealing with edges */
185 if (irq_trig == IRQF_TRIGGER_FALLING ||
186 irq_trig == IRQF_TRIGGER_RISING)
187 sdata->edge_irq = true;
190 * If we're not using edges (i.e. level interrupts) we
191 * just mask off the IRQ, handle one interrupt, then
192 * if the line is still low, we return to the
193 * interrupt handler top half again and start over.
195 irq_trig |= IRQF_ONESHOT;
198 * If the interrupt pin is Open Drain, by definition this
199 * means that the interrupt line may be shared with other
200 * peripherals. But to do this we also need to have a status
201 * register and mask to figure out if this sensor was firing
202 * the IRQ or not, so we can tell the interrupt handle that
203 * it was "our" interrupt.
205 if (sdata->int_pin_open_drain &&
206 sdata->sensor_settings->drdy_irq.stat_drdy.addr)
207 irq_trig |= IRQF_SHARED;
209 err = request_threaded_irq(sdata->get_irq_data_ready(indio_dev),
210 st_sensors_irq_handler,
211 st_sensors_irq_thread,
216 dev_err(&indio_dev->dev, "failed to request trigger IRQ.\n");
217 goto iio_trigger_free;
220 err = iio_trigger_register(sdata->trig);
222 dev_err(&indio_dev->dev, "failed to register iio trigger.\n");
223 goto iio_trigger_register_error;
225 indio_dev->trig = iio_trigger_get(sdata->trig);
229 iio_trigger_register_error:
230 free_irq(sdata->get_irq_data_ready(indio_dev), sdata->trig);
232 iio_trigger_free(sdata->trig);
235 EXPORT_SYMBOL(st_sensors_allocate_trigger);
237 void st_sensors_deallocate_trigger(struct iio_dev *indio_dev)
239 struct st_sensor_data *sdata = iio_priv(indio_dev);
241 iio_trigger_unregister(sdata->trig);
242 free_irq(sdata->get_irq_data_ready(indio_dev), sdata->trig);
243 iio_trigger_free(sdata->trig);
245 EXPORT_SYMBOL(st_sensors_deallocate_trigger);
247 int st_sensors_validate_device(struct iio_trigger *trig,
248 struct iio_dev *indio_dev)
250 struct iio_dev *indio = iio_trigger_get_drvdata(trig);
252 if (indio != indio_dev)
257 EXPORT_SYMBOL(st_sensors_validate_device);
259 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
260 MODULE_DESCRIPTION("STMicroelectronics ST-sensors trigger");
261 MODULE_LICENSE("GPL v2");