WIP FPC-III support
[linux/fpc-iii.git] / drivers / iio / common / st_sensors / st_sensors_trigger.c
blob0507283bd4c1dedd78780f9c20dcca39beef0748
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * STMicroelectronics sensors trigger library driver
5 * Copyright 2012-2013 STMicroelectronics Inc.
7 * Denis Ciocca <denis.ciocca@st.com>
8 */
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"
20 /**
21 * st_sensors_new_samples_available() - check if more samples came in
22 * @indio_dev: IIO device reference.
23 * @sdata: Sensor data.
25 * returns:
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)
33 int ret, status;
35 /* How would I know if I can't check it? */
36 if (!sdata->sensor_settings->drdy_irq.stat_drdy.addr)
37 return -EINVAL;
39 /* No scan mask, no interrupt */
40 if (!indio_dev->active_scan_mask)
41 return 0;
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, "error checking samples available\n");
48 return ret;
51 if (status & sdata->sensor_settings->drdy_irq.stat_drdy.mask)
52 return 1;
54 return 0;
57 /**
58 * st_sensors_irq_handler() - top half of the IRQ-based triggers
59 * @irq: irq number
60 * @p: private handler data
62 static irqreturn_t st_sensors_irq_handler(int irq, void *p)
64 struct iio_trigger *trig = p;
65 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
66 struct st_sensor_data *sdata = iio_priv(indio_dev);
68 /* Get the time stamp as close in time as possible */
69 sdata->hw_timestamp = iio_get_time_ns(indio_dev);
70 return IRQ_WAKE_THREAD;
73 /**
74 * st_sensors_irq_thread() - bottom half of the IRQ-based triggers
75 * @irq: irq number
76 * @p: private handler data
78 static irqreturn_t st_sensors_irq_thread(int irq, void *p)
80 struct iio_trigger *trig = p;
81 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
82 struct st_sensor_data *sdata = iio_priv(indio_dev);
85 * If this trigger is backed by a hardware interrupt and we have a
86 * status register, check if this IRQ came from us. Notice that
87 * we will process also if st_sensors_new_samples_available()
88 * returns negative: if we can't check status, then poll
89 * unconditionally.
91 if (sdata->hw_irq_trigger &&
92 st_sensors_new_samples_available(indio_dev, sdata)) {
93 iio_trigger_poll_chained(p);
94 } else {
95 dev_dbg(sdata->dev, "spurious IRQ\n");
96 return IRQ_NONE;
100 * If we have proper level IRQs the handler will be re-entered if
101 * the line is still active, so return here and come back in through
102 * the top half if need be.
104 if (!sdata->edge_irq)
105 return IRQ_HANDLED;
108 * If we are using edge IRQs, new samples arrived while processing
109 * the IRQ and those may be missed unless we pick them here, so poll
110 * again. If the sensor delivery frequency is very high, this thread
111 * turns into a polled loop handler.
113 while (sdata->hw_irq_trigger &&
114 st_sensors_new_samples_available(indio_dev, sdata)) {
115 dev_dbg(sdata->dev, "more samples came in during polling\n");
116 sdata->hw_timestamp = iio_get_time_ns(indio_dev);
117 iio_trigger_poll_chained(p);
120 return IRQ_HANDLED;
123 int st_sensors_allocate_trigger(struct iio_dev *indio_dev,
124 const struct iio_trigger_ops *trigger_ops)
126 struct st_sensor_data *sdata = iio_priv(indio_dev);
127 unsigned long irq_trig;
128 int err;
130 sdata->trig = iio_trigger_alloc("%s-trigger", indio_dev->name);
131 if (sdata->trig == NULL) {
132 dev_err(&indio_dev->dev, "failed to allocate iio trigger.\n");
133 return -ENOMEM;
136 iio_trigger_set_drvdata(sdata->trig, indio_dev);
137 sdata->trig->ops = trigger_ops;
138 sdata->trig->dev.parent = sdata->dev;
140 irq_trig = irqd_get_trigger_type(irq_get_irq_data(sdata->irq));
142 * If the IRQ is triggered on falling edge, we need to mark the
143 * interrupt as active low, if the hardware supports this.
145 switch(irq_trig) {
146 case IRQF_TRIGGER_FALLING:
147 case IRQF_TRIGGER_LOW:
148 if (!sdata->sensor_settings->drdy_irq.addr_ihl) {
149 dev_err(&indio_dev->dev,
150 "falling/low specified for IRQ but hardware supports only rising/high: will request rising/high\n");
151 if (irq_trig == IRQF_TRIGGER_FALLING)
152 irq_trig = IRQF_TRIGGER_RISING;
153 if (irq_trig == IRQF_TRIGGER_LOW)
154 irq_trig = IRQF_TRIGGER_HIGH;
155 } else {
156 /* Set up INT active low i.e. falling edge */
157 err = st_sensors_write_data_with_mask(indio_dev,
158 sdata->sensor_settings->drdy_irq.addr_ihl,
159 sdata->sensor_settings->drdy_irq.mask_ihl, 1);
160 if (err < 0)
161 goto iio_trigger_free;
162 dev_info(&indio_dev->dev,
163 "interrupts on the falling edge or active low level\n");
165 break;
166 case IRQF_TRIGGER_RISING:
167 dev_info(&indio_dev->dev,
168 "interrupts on the rising edge\n");
169 break;
170 case IRQF_TRIGGER_HIGH:
171 dev_info(&indio_dev->dev,
172 "interrupts active high level\n");
173 break;
174 default:
175 /* This is the most preferred mode, if possible */
176 dev_err(&indio_dev->dev,
177 "unsupported IRQ trigger specified (%lx), enforce rising edge\n", irq_trig);
178 irq_trig = IRQF_TRIGGER_RISING;
181 /* Tell the interrupt handler that we're dealing with edges */
182 if (irq_trig == IRQF_TRIGGER_FALLING ||
183 irq_trig == IRQF_TRIGGER_RISING)
184 sdata->edge_irq = true;
185 else
187 * If we're not using edges (i.e. level interrupts) we
188 * just mask off the IRQ, handle one interrupt, then
189 * if the line is still low, we return to the
190 * interrupt handler top half again and start over.
192 irq_trig |= IRQF_ONESHOT;
195 * If the interrupt pin is Open Drain, by definition this
196 * means that the interrupt line may be shared with other
197 * peripherals. But to do this we also need to have a status
198 * register and mask to figure out if this sensor was firing
199 * the IRQ or not, so we can tell the interrupt handle that
200 * it was "our" interrupt.
202 if (sdata->int_pin_open_drain &&
203 sdata->sensor_settings->drdy_irq.stat_drdy.addr)
204 irq_trig |= IRQF_SHARED;
206 err = request_threaded_irq(sdata->irq,
207 st_sensors_irq_handler,
208 st_sensors_irq_thread,
209 irq_trig,
210 sdata->trig->name,
211 sdata->trig);
212 if (err) {
213 dev_err(&indio_dev->dev, "failed to request trigger IRQ.\n");
214 goto iio_trigger_free;
217 err = iio_trigger_register(sdata->trig);
218 if (err < 0) {
219 dev_err(&indio_dev->dev, "failed to register iio trigger.\n");
220 goto iio_trigger_register_error;
222 indio_dev->trig = iio_trigger_get(sdata->trig);
224 return 0;
226 iio_trigger_register_error:
227 free_irq(sdata->irq, sdata->trig);
228 iio_trigger_free:
229 iio_trigger_free(sdata->trig);
230 return err;
232 EXPORT_SYMBOL(st_sensors_allocate_trigger);
234 void st_sensors_deallocate_trigger(struct iio_dev *indio_dev)
236 struct st_sensor_data *sdata = iio_priv(indio_dev);
238 iio_trigger_unregister(sdata->trig);
239 free_irq(sdata->irq, sdata->trig);
240 iio_trigger_free(sdata->trig);
242 EXPORT_SYMBOL(st_sensors_deallocate_trigger);
244 int st_sensors_validate_device(struct iio_trigger *trig,
245 struct iio_dev *indio_dev)
247 struct iio_dev *indio = iio_trigger_get_drvdata(trig);
249 if (indio != indio_dev)
250 return -EINVAL;
252 return 0;
254 EXPORT_SYMBOL(st_sensors_validate_device);
256 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
257 MODULE_DESCRIPTION("STMicroelectronics ST-sensors trigger");
258 MODULE_LICENSE("GPL v2");