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/regmap.h>
17 #include <linux/iio/common/st_sensors.h>
18 #include "st_sensors_core.h"
21 * st_sensors_new_samples_available() - check if more samples came in
22 * @indio_dev: IIO device reference.
23 * @sdata: Sensor data.
26 * 0 - no new samples available
27 * 1 - new samples available
28 * negative - error or unknown
30 static int st_sensors_new_samples_available(struct iio_dev
*indio_dev
,
31 struct st_sensor_data
*sdata
)
35 /* How would I know if I can't check it? */
36 if (!sdata
->sensor_settings
->drdy_irq
.stat_drdy
.addr
)
39 /* No scan mask, no interrupt */
40 if (!indio_dev
->active_scan_mask
)
43 ret
= regmap_read(sdata
->regmap
,
44 sdata
->sensor_settings
->drdy_irq
.stat_drdy
.addr
,
48 "error checking samples available\n");
52 if (status
& sdata
->sensor_settings
->drdy_irq
.stat_drdy
.mask
)
59 * st_sensors_irq_handler() - top half of the IRQ-based triggers
61 * @p: private handler data
63 static irqreturn_t
st_sensors_irq_handler(int irq
, void *p
)
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
);
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
;
75 * st_sensors_irq_thread() - bottom half of the IRQ-based triggers
77 * @p: private handler data
79 static irqreturn_t
st_sensors_irq_thread(int irq
, void *p
)
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
);
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
92 if (sdata
->hw_irq_trigger
&&
93 st_sensors_new_samples_available(indio_dev
, sdata
)) {
94 iio_trigger_poll_chained(p
);
96 dev_dbg(sdata
->dev
, "spurious IRQ\n");
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.
105 if (!sdata
->edge_irq
)
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.
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
);
124 int st_sensors_allocate_trigger(struct iio_dev
*indio_dev
,
125 const struct iio_trigger_ops
*trigger_ops
)
127 struct st_sensor_data
*sdata
= iio_priv(indio_dev
);
128 unsigned long irq_trig
;
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");
137 iio_trigger_set_drvdata(sdata
->trig
, indio_dev
);
138 sdata
->trig
->ops
= trigger_ops
;
139 sdata
->trig
->dev
.parent
= sdata
->dev
;
141 irq_trig
= irqd_get_trigger_type(irq_get_irq_data(sdata
->irq
));
143 * If the IRQ is triggered on falling edge, we need to mark the
144 * interrupt as active low, if the hardware supports this.
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
;
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);
164 goto iio_trigger_free
;
165 dev_info(&indio_dev
->dev
,
166 "interrupts on the falling edge or "
167 "active low level\n");
170 case IRQF_TRIGGER_RISING
:
171 dev_info(&indio_dev
->dev
,
172 "interrupts on the rising edge\n");
174 case IRQF_TRIGGER_HIGH
:
175 dev_info(&indio_dev
->dev
,
176 "interrupts active high level\n");
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
;
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;
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.
197 irq_trig
|= IRQF_ONESHOT
;
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.
207 if (sdata
->int_pin_open_drain
&&
208 sdata
->sensor_settings
->drdy_irq
.stat_drdy
.addr
)
209 irq_trig
|= IRQF_SHARED
;
211 err
= request_threaded_irq(sdata
->irq
,
212 st_sensors_irq_handler
,
213 st_sensors_irq_thread
,
218 dev_err(&indio_dev
->dev
, "failed to request trigger IRQ.\n");
219 goto iio_trigger_free
;
222 err
= iio_trigger_register(sdata
->trig
);
224 dev_err(&indio_dev
->dev
, "failed to register iio trigger.\n");
225 goto iio_trigger_register_error
;
227 indio_dev
->trig
= iio_trigger_get(sdata
->trig
);
231 iio_trigger_register_error
:
232 free_irq(sdata
->irq
, sdata
->trig
);
234 iio_trigger_free(sdata
->trig
);
237 EXPORT_SYMBOL(st_sensors_allocate_trigger
);
239 void st_sensors_deallocate_trigger(struct iio_dev
*indio_dev
)
241 struct st_sensor_data
*sdata
= iio_priv(indio_dev
);
243 iio_trigger_unregister(sdata
->trig
);
244 free_irq(sdata
->irq
, sdata
->trig
);
245 iio_trigger_free(sdata
->trig
);
247 EXPORT_SYMBOL(st_sensors_deallocate_trigger
);
249 int st_sensors_validate_device(struct iio_trigger
*trig
,
250 struct iio_dev
*indio_dev
)
252 struct iio_dev
*indio
= iio_trigger_get_drvdata(trig
);
254 if (indio
!= indio_dev
)
259 EXPORT_SYMBOL(st_sensors_validate_device
);
261 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
262 MODULE_DESCRIPTION("STMicroelectronics ST-sensors trigger");
263 MODULE_LICENSE("GPL v2");