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 <denis.ciocca@st.com>
8 */
9
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"
18
19 /**
20 * st_sensors_new_samples_available() - check if more samples came in
21 * @indio_dev: IIO device reference.
22 * @sdata: Sensor data.
23 *
24 * returns:
25 * false - no new samples available or read error
26 * true - new samples available
27 */
st_sensors_new_samples_available(struct iio_dev * indio_dev,struct st_sensor_data * sdata)28 static bool st_sensors_new_samples_available(struct iio_dev *indio_dev,
29 struct st_sensor_data *sdata)
30 {
31 int ret, status;
32
33 /* How would I know if I can't check it? */
34 if (!sdata->sensor_settings->drdy_irq.stat_drdy.addr)
35 return true;
36
37 /* No scan mask, no interrupt */
38 if (!indio_dev->active_scan_mask)
39 return false;
40
41 ret = regmap_read(sdata->regmap,
42 sdata->sensor_settings->drdy_irq.stat_drdy.addr,
43 &status);
44 if (ret < 0) {
45 dev_err(indio_dev->dev.parent,
46 "error checking samples available\n");
47 return false;
48 }
49
50 return !!(status & sdata->sensor_settings->drdy_irq.stat_drdy.mask);
51 }
52
53 /**
54 * st_sensors_irq_handler() - top half of the IRQ-based triggers
55 * @irq: irq number
56 * @p: private handler data
57 */
st_sensors_irq_handler(int irq,void * p)58 static irqreturn_t st_sensors_irq_handler(int irq, void *p)
59 {
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);
63
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;
67 }
68
69 /**
70 * st_sensors_irq_thread() - bottom half of the IRQ-based triggers
71 * @irq: irq number
72 * @p: private handler data
73 */
st_sensors_irq_thread(int irq,void * p)74 static irqreturn_t st_sensors_irq_thread(int irq, void *p)
75 {
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);
79
80 /*
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
85 * unconditionally.
86 */
87 if (sdata->hw_irq_trigger &&
88 st_sensors_new_samples_available(indio_dev, sdata)) {
89 iio_trigger_poll_chained(p);
90 } else {
91 dev_dbg(indio_dev->dev.parent, "spurious IRQ\n");
92 return IRQ_NONE;
93 }
94
95 /*
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.
99 */
100 if (!sdata->edge_irq)
101 return IRQ_HANDLED;
102
103 /*
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.
108 */
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);
115 }
116
117 return IRQ_HANDLED;
118 }
119
st_sensors_allocate_trigger(struct iio_dev * indio_dev,const struct iio_trigger_ops * trigger_ops)120 int st_sensors_allocate_trigger(struct iio_dev *indio_dev,
121 const struct iio_trigger_ops *trigger_ops)
122 {
123 struct st_sensor_data *sdata = iio_priv(indio_dev);
124 struct device *parent = indio_dev->dev.parent;
125 unsigned long irq_trig;
126 int err;
127
128 sdata->trig = devm_iio_trigger_alloc(parent, "%s-trigger",
129 indio_dev->name);
130 if (sdata->trig == NULL) {
131 dev_err(&indio_dev->dev, "failed to allocate iio trigger.\n");
132 return -ENOMEM;
133 }
134
135 iio_trigger_set_drvdata(sdata->trig, indio_dev);
136 sdata->trig->ops = trigger_ops;
137
138 irq_trig = irqd_get_trigger_type(irq_get_irq_data(sdata->irq));
139 /*
140 * If the IRQ is triggered on falling edge, we need to mark the
141 * interrupt as active low, if the hardware supports this.
142 */
143 switch(irq_trig) {
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;
153 } else {
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);
158 if (err < 0)
159 return err;
160 dev_info(&indio_dev->dev,
161 "interrupts on the falling edge or active low level\n");
162 }
163 break;
164 case IRQF_TRIGGER_RISING:
165 dev_info(&indio_dev->dev,
166 "interrupts on the rising edge\n");
167 break;
168 case IRQF_TRIGGER_HIGH:
169 dev_info(&indio_dev->dev,
170 "interrupts active high level\n");
171 break;
172 default:
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;
177 }
178
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");
185 return -EOPNOTSUPP;
186 }
187 sdata->edge_irq = true;
188 } else {
189 /*
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.
194 */
195 irq_trig |= IRQF_ONESHOT;
196 }
197
198 /*
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.
205 */
206 if (sdata->int_pin_open_drain &&
207 sdata->sensor_settings->drdy_irq.stat_drdy.addr)
208 irq_trig |= IRQF_SHARED;
209
210 err = devm_request_threaded_irq(parent,
211 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 return err;
220 }
221
222 err = devm_iio_trigger_register(parent, sdata->trig);
223 if (err < 0) {
224 dev_err(&indio_dev->dev, "failed to register iio trigger.\n");
225 return err;
226 }
227 indio_dev->trig = iio_trigger_get(sdata->trig);
228
229 return 0;
230 }
231 EXPORT_SYMBOL(st_sensors_allocate_trigger);
232
st_sensors_validate_device(struct iio_trigger * trig,struct iio_dev * indio_dev)233 int st_sensors_validate_device(struct iio_trigger *trig,
234 struct iio_dev *indio_dev)
235 {
236 struct iio_dev *indio = iio_trigger_get_drvdata(trig);
237
238 if (indio != indio_dev)
239 return -EINVAL;
240
241 return 0;
242 }
243 EXPORT_SYMBOL(st_sensors_validate_device);
244
245 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
246 MODULE_DESCRIPTION("STMicroelectronics ST-sensors trigger");
247 MODULE_LICENSE("GPL v2");
248