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/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"
20 * st_sensors_new_samples_available() - check if more samples came in
21 * @indio_dev: IIO device reference.
22 * @sdata: Sensor data.
25 * false - no new samples available or read error
26 * true - new samples available
28 static bool st_sensors_new_samples_available(struct iio_dev *indio_dev,
29 struct st_sensor_data *sdata)
33 /* How would I know if I can't check it? */
34 if (!sdata->sensor_settings->drdy_irq.stat_drdy.addr)
37 /* No scan mask, no interrupt */
38 if (!indio_dev->active_scan_mask)
41 ret = regmap_read(sdata->regmap,
42 sdata->sensor_settings->drdy_irq.stat_drdy.addr,
45 dev_err(indio_dev->dev.parent,
46 "error checking samples available\n");
50 return !!(status & sdata->sensor_settings->drdy_irq.stat_drdy.mask);
54 * st_sensors_irq_handler() - top half of the IRQ-based triggers
56 * @p: private handler data
58 static irqreturn_t st_sensors_irq_handler(int irq, void *p)
60 struct iio_trigger *trig = p;
61 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
62 struct st_sensor_data *sdata = iio_priv(indio_dev);
64 /* Get the time stamp as close in time as possible */
65 sdata->hw_timestamp = iio_get_time_ns(indio_dev);
66 return IRQ_WAKE_THREAD;
70 * st_sensors_irq_thread() - bottom half of the IRQ-based triggers
72 * @p: private handler data
74 static irqreturn_t st_sensors_irq_thread(int irq, void *p)
76 struct iio_trigger *trig = p;
77 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
78 struct st_sensor_data *sdata = iio_priv(indio_dev);
81 * If this trigger is backed by a hardware interrupt and we have a
82 * status register, check if this IRQ came from us. Notice that
83 * we will process also if st_sensors_new_samples_available()
84 * returns negative: if we can't check status, then poll
87 if (sdata->hw_irq_trigger &&
88 st_sensors_new_samples_available(indio_dev, sdata)) {
89 iio_trigger_poll_chained(p);
91 dev_dbg(indio_dev->dev.parent, "spurious IRQ\n");
96 * If we have proper level IRQs the handler will be re-entered if
97 * the line is still active, so return here and come back in through
98 * the top half if need be.
100 if (!sdata->edge_irq)
104 * If we are using edge IRQs, new samples arrived while processing
105 * the IRQ and those may be missed unless we pick them here, so poll
106 * again. If the sensor delivery frequency is very high, this thread
107 * turns into a polled loop handler.
109 while (sdata->hw_irq_trigger &&
110 st_sensors_new_samples_available(indio_dev, sdata)) {
111 dev_dbg(indio_dev->dev.parent,
112 "more samples came in during polling\n");
113 sdata->hw_timestamp = iio_get_time_ns(indio_dev);
114 iio_trigger_poll_chained(p);
120 int st_sensors_allocate_trigger(struct iio_dev *indio_dev,
121 const struct iio_trigger_ops *trigger_ops)
123 struct st_sensor_data *sdata = iio_priv(indio_dev);
124 struct device *parent = indio_dev->dev.parent;
125 unsigned long irq_trig;
128 sdata->trig = devm_iio_trigger_alloc(parent, "%s-trigger",
130 if (sdata->trig == NULL) {
131 dev_err(&indio_dev->dev, "failed to allocate iio trigger.\n");
135 iio_trigger_set_drvdata(sdata->trig, indio_dev);
136 sdata->trig->ops = trigger_ops;
138 irq_trig = irqd_get_trigger_type(irq_get_irq_data(sdata->irq));
140 * If the IRQ is triggered on falling edge, we need to mark the
141 * interrupt as active low, if the hardware supports this.
144 case IRQF_TRIGGER_FALLING:
145 case IRQF_TRIGGER_LOW:
146 if (!sdata->sensor_settings->drdy_irq.addr_ihl) {
147 dev_err(&indio_dev->dev,
148 "falling/low specified for IRQ but hardware supports only rising/high: will request rising/high\n");
149 if (irq_trig == IRQF_TRIGGER_FALLING)
150 irq_trig = IRQF_TRIGGER_RISING;
151 if (irq_trig == IRQF_TRIGGER_LOW)
152 irq_trig = IRQF_TRIGGER_HIGH;
154 /* Set up INT active low i.e. falling edge */
155 err = st_sensors_write_data_with_mask(indio_dev,
156 sdata->sensor_settings->drdy_irq.addr_ihl,
157 sdata->sensor_settings->drdy_irq.mask_ihl, 1);
160 dev_info(&indio_dev->dev,
161 "interrupts on the falling edge or active low level\n");
164 case IRQF_TRIGGER_RISING:
165 dev_info(&indio_dev->dev,
166 "interrupts on the rising edge\n");
168 case IRQF_TRIGGER_HIGH:
169 dev_info(&indio_dev->dev,
170 "interrupts active high level\n");
173 /* This is the most preferred mode, if possible */
174 dev_err(&indio_dev->dev,
175 "unsupported IRQ trigger specified (%lx), enforce rising edge\n", irq_trig);
176 irq_trig = IRQF_TRIGGER_RISING;
179 /* Tell the interrupt handler that we're dealing with edges */
180 if (irq_trig == IRQF_TRIGGER_FALLING ||
181 irq_trig == IRQF_TRIGGER_RISING) {
182 if (!sdata->sensor_settings->drdy_irq.stat_drdy.addr) {
183 dev_err(&indio_dev->dev,
184 "edge IRQ not supported w/o stat register.\n");
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;
199 * If the interrupt pin is Open Drain, by definition this
200 * means that the interrupt line may be shared with other
201 * peripherals. But to do this we also need to have a status
202 * register and mask to figure out if this sensor was firing
203 * the IRQ or not, so we can tell the interrupt handle that
204 * it was "our" interrupt.
206 if (sdata->int_pin_open_drain &&
207 sdata->sensor_settings->drdy_irq.stat_drdy.addr)
208 irq_trig |= IRQF_SHARED;
210 err = devm_request_threaded_irq(parent,
212 st_sensors_irq_handler,
213 st_sensors_irq_thread,
218 dev_err(&indio_dev->dev, "failed to request trigger IRQ.\n");
222 err = devm_iio_trigger_register(parent, sdata->trig);
224 dev_err(&indio_dev->dev, "failed to register iio trigger.\n");
227 indio_dev->trig = iio_trigger_get(sdata->trig);
231 EXPORT_SYMBOL(st_sensors_allocate_trigger);
233 int st_sensors_validate_device(struct iio_trigger *trig,
234 struct iio_dev *indio_dev)
236 struct iio_dev *indio = iio_trigger_get_drvdata(trig);
238 if (indio != indio_dev)
243 EXPORT_SYMBOL(st_sensors_validate_device);
245 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
246 MODULE_DESCRIPTION("STMicroelectronics ST-sensors trigger");
247 MODULE_LICENSE("GPL v2");