• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/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"
19 
20 /**
21  * st_sensors_new_samples_available() - check if more samples came in
22  * @indio_dev: IIO device reference.
23  * @sdata: Sensor data.
24  *
25  * returns:
26  * false - no new samples available or read error
27  * true - new samples available
28  */
st_sensors_new_samples_available(struct iio_dev * indio_dev,struct st_sensor_data * sdata)29 static bool st_sensors_new_samples_available(struct iio_dev *indio_dev,
30 					     struct st_sensor_data *sdata)
31 {
32 	int ret, status;
33 
34 	/* How would I know if I can't check it? */
35 	if (!sdata->sensor_settings->drdy_irq.stat_drdy.addr)
36 		return true;
37 
38 	/* No scan mask, no interrupt */
39 	if (!indio_dev->active_scan_mask)
40 		return false;
41 
42 	ret = regmap_read(sdata->regmap,
43 			  sdata->sensor_settings->drdy_irq.stat_drdy.addr,
44 			  &status);
45 	if (ret < 0) {
46 		dev_err(sdata->dev, "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(sdata->dev, "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(sdata->dev, "more samples came in during polling\n");
112 		sdata->hw_timestamp = iio_get_time_ns(indio_dev);
113 		iio_trigger_poll_chained(p);
114 	}
115 
116 	return IRQ_HANDLED;
117 }
118 
st_sensors_allocate_trigger(struct iio_dev * indio_dev,const struct iio_trigger_ops * trigger_ops)119 int st_sensors_allocate_trigger(struct iio_dev *indio_dev,
120 				const struct iio_trigger_ops *trigger_ops)
121 {
122 	struct st_sensor_data *sdata = iio_priv(indio_dev);
123 	unsigned long irq_trig;
124 	int err;
125 
126 	sdata->trig = iio_trigger_alloc("%s-trigger", indio_dev->name);
127 	if (sdata->trig == NULL) {
128 		dev_err(&indio_dev->dev, "failed to allocate iio trigger.\n");
129 		return -ENOMEM;
130 	}
131 
132 	iio_trigger_set_drvdata(sdata->trig, indio_dev);
133 	sdata->trig->ops = trigger_ops;
134 	sdata->trig->dev.parent = sdata->dev;
135 
136 	irq_trig = irqd_get_trigger_type(irq_get_irq_data(sdata->irq));
137 	/*
138 	 * If the IRQ is triggered on falling edge, we need to mark the
139 	 * interrupt as active low, if the hardware supports this.
140 	 */
141 	switch(irq_trig) {
142 	case IRQF_TRIGGER_FALLING:
143 	case IRQF_TRIGGER_LOW:
144 		if (!sdata->sensor_settings->drdy_irq.addr_ihl) {
145 			dev_err(&indio_dev->dev,
146 				"falling/low specified for IRQ but hardware supports only rising/high: will request rising/high\n");
147 			if (irq_trig == IRQF_TRIGGER_FALLING)
148 				irq_trig = IRQF_TRIGGER_RISING;
149 			if (irq_trig == IRQF_TRIGGER_LOW)
150 				irq_trig = IRQF_TRIGGER_HIGH;
151 		} else {
152 			/* Set up INT active low i.e. falling edge */
153 			err = st_sensors_write_data_with_mask(indio_dev,
154 				sdata->sensor_settings->drdy_irq.addr_ihl,
155 				sdata->sensor_settings->drdy_irq.mask_ihl, 1);
156 			if (err < 0)
157 				goto iio_trigger_free;
158 			dev_info(&indio_dev->dev,
159 				 "interrupts on the falling edge or active low level\n");
160 		}
161 		break;
162 	case IRQF_TRIGGER_RISING:
163 		dev_info(&indio_dev->dev,
164 			 "interrupts on the rising edge\n");
165 		break;
166 	case IRQF_TRIGGER_HIGH:
167 		dev_info(&indio_dev->dev,
168 			 "interrupts active high level\n");
169 		break;
170 	default:
171 		/* This is the most preferred mode, if possible */
172 		dev_err(&indio_dev->dev,
173 			"unsupported IRQ trigger specified (%lx), enforce rising edge\n", irq_trig);
174 		irq_trig = IRQF_TRIGGER_RISING;
175 	}
176 
177 	/* Tell the interrupt handler that we're dealing with edges */
178 	if (irq_trig == IRQF_TRIGGER_FALLING ||
179 	    irq_trig == IRQF_TRIGGER_RISING) {
180 		if (!sdata->sensor_settings->drdy_irq.stat_drdy.addr) {
181 			dev_err(&indio_dev->dev,
182 				"edge IRQ not supported w/o stat register.\n");
183 			err = -EOPNOTSUPP;
184 			goto iio_trigger_free;
185 		}
186 		sdata->edge_irq = true;
187 	} else {
188 		/*
189 		 * If we're not using edges (i.e. level interrupts) we
190 		 * just mask off the IRQ, handle one interrupt, then
191 		 * if the line is still low, we return to the
192 		 * interrupt handler top half again and start over.
193 		 */
194 		irq_trig |= IRQF_ONESHOT;
195 	}
196 
197 	/*
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.
204 	 */
205 	if (sdata->int_pin_open_drain &&
206 	    sdata->sensor_settings->drdy_irq.stat_drdy.addr)
207 		irq_trig |= IRQF_SHARED;
208 
209 	err = request_threaded_irq(sdata->irq,
210 				   st_sensors_irq_handler,
211 				   st_sensors_irq_thread,
212 				   irq_trig,
213 				   sdata->trig->name,
214 				   sdata->trig);
215 	if (err) {
216 		dev_err(&indio_dev->dev, "failed to request trigger IRQ.\n");
217 		goto iio_trigger_free;
218 	}
219 
220 	err = iio_trigger_register(sdata->trig);
221 	if (err < 0) {
222 		dev_err(&indio_dev->dev, "failed to register iio trigger.\n");
223 		goto iio_trigger_register_error;
224 	}
225 	indio_dev->trig = iio_trigger_get(sdata->trig);
226 
227 	return 0;
228 
229 iio_trigger_register_error:
230 	free_irq(sdata->irq, sdata->trig);
231 iio_trigger_free:
232 	iio_trigger_free(sdata->trig);
233 	return err;
234 }
235 EXPORT_SYMBOL(st_sensors_allocate_trigger);
236 
st_sensors_deallocate_trigger(struct iio_dev * indio_dev)237 void st_sensors_deallocate_trigger(struct iio_dev *indio_dev)
238 {
239 	struct st_sensor_data *sdata = iio_priv(indio_dev);
240 
241 	iio_trigger_unregister(sdata->trig);
242 	free_irq(sdata->irq, sdata->trig);
243 	iio_trigger_free(sdata->trig);
244 }
245 EXPORT_SYMBOL(st_sensors_deallocate_trigger);
246 
st_sensors_validate_device(struct iio_trigger * trig,struct iio_dev * indio_dev)247 int st_sensors_validate_device(struct iio_trigger *trig,
248 			       struct iio_dev *indio_dev)
249 {
250 	struct iio_dev *indio = iio_trigger_get_drvdata(trig);
251 
252 	if (indio != indio_dev)
253 		return -EINVAL;
254 
255 	return 0;
256 }
257 EXPORT_SYMBOL(st_sensors_validate_device);
258 
259 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
260 MODULE_DESCRIPTION("STMicroelectronics ST-sensors trigger");
261 MODULE_LICENSE("GPL v2");
262