xref: /linux/drivers/iio/imu/inv_mpu6050/inv_mpu_trigger.c (revision c5288cda69ee2d8607f5026bd599a5cebf0ee783)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) 2012 Invensense, Inc.
4 */
5 
6 #include <linux/pm_runtime.h>
7 
8 #include <linux/iio/common/inv_sensors_timestamp.h>
9 #include <linux/iio/events.h>
10 
11 #include "inv_mpu_iio.h"
12 
13 static unsigned int inv_scan_query_mpu6050(struct iio_dev *indio_dev)
14 {
15 	struct inv_mpu6050_state  *st = iio_priv(indio_dev);
16 	unsigned int mask;
17 
18 	/*
19 	 * If the MPU6050 is just used as a trigger, then the scan mask
20 	 * is not allocated so we simply enable the temperature channel
21 	 * as a dummy and bail out.
22 	 */
23 	if (!indio_dev->active_scan_mask) {
24 		st->chip_config.temp_fifo_enable = true;
25 		return INV_MPU6050_SENSOR_TEMP;
26 	}
27 
28 	st->chip_config.gyro_fifo_enable =
29 		test_bit(INV_MPU6050_SCAN_GYRO_X,
30 			 indio_dev->active_scan_mask) ||
31 		test_bit(INV_MPU6050_SCAN_GYRO_Y,
32 			 indio_dev->active_scan_mask) ||
33 		test_bit(INV_MPU6050_SCAN_GYRO_Z,
34 			 indio_dev->active_scan_mask);
35 
36 	st->chip_config.accl_fifo_enable =
37 		test_bit(INV_MPU6050_SCAN_ACCL_X,
38 			 indio_dev->active_scan_mask) ||
39 		test_bit(INV_MPU6050_SCAN_ACCL_Y,
40 			 indio_dev->active_scan_mask) ||
41 		test_bit(INV_MPU6050_SCAN_ACCL_Z,
42 			 indio_dev->active_scan_mask);
43 
44 	st->chip_config.temp_fifo_enable =
45 		test_bit(INV_MPU6050_SCAN_TEMP, indio_dev->active_scan_mask);
46 
47 	mask = 0;
48 	if (st->chip_config.gyro_fifo_enable)
49 		mask |= INV_MPU6050_SENSOR_GYRO;
50 	if (st->chip_config.accl_fifo_enable)
51 		mask |= INV_MPU6050_SENSOR_ACCL;
52 	if (st->chip_config.temp_fifo_enable)
53 		mask |= INV_MPU6050_SENSOR_TEMP;
54 
55 	return mask;
56 }
57 
58 static unsigned int inv_scan_query_mpu9x50(struct iio_dev *indio_dev)
59 {
60 	struct inv_mpu6050_state *st = iio_priv(indio_dev);
61 	unsigned int mask;
62 
63 	mask = inv_scan_query_mpu6050(indio_dev);
64 
65 	/* no magnetometer if i2c auxiliary bus is used */
66 	if (st->magn_disabled)
67 		return mask;
68 
69 	st->chip_config.magn_fifo_enable =
70 		test_bit(INV_MPU9X50_SCAN_MAGN_X,
71 			 indio_dev->active_scan_mask) ||
72 		test_bit(INV_MPU9X50_SCAN_MAGN_Y,
73 			 indio_dev->active_scan_mask) ||
74 		test_bit(INV_MPU9X50_SCAN_MAGN_Z,
75 			 indio_dev->active_scan_mask);
76 	if (st->chip_config.magn_fifo_enable)
77 		mask |= INV_MPU6050_SENSOR_MAGN;
78 
79 	return mask;
80 }
81 
82 static unsigned int inv_scan_query(struct iio_dev *indio_dev)
83 {
84 	struct inv_mpu6050_state *st = iio_priv(indio_dev);
85 
86 	switch (st->chip_type) {
87 	case INV_MPU9150:
88 	case INV_MPU9250:
89 	case INV_MPU9255:
90 		return inv_scan_query_mpu9x50(indio_dev);
91 	default:
92 		return inv_scan_query_mpu6050(indio_dev);
93 	}
94 }
95 
96 static unsigned int inv_compute_skip_samples(const struct inv_mpu6050_state *st)
97 {
98 	unsigned int skip_samples = 0;
99 
100 	/* mag first sample is always not ready, skip it */
101 	if (st->chip_config.magn_fifo_enable)
102 		skip_samples = 1;
103 
104 	return skip_samples;
105 }
106 
107 int inv_mpu6050_prepare_fifo(struct inv_mpu6050_state *st, bool enable)
108 {
109 	uint8_t d;
110 	int ret;
111 
112 	if (enable) {
113 		/* reset timestamping */
114 		inv_sensors_timestamp_reset(&st->timestamp);
115 		inv_sensors_timestamp_apply_odr(&st->timestamp, 0, 0, 0);
116 		/* reset FIFO */
117 		d = st->chip_config.user_ctrl | INV_MPU6050_BIT_FIFO_RST;
118 		ret = regmap_write(st->map, st->reg->user_ctrl, d);
119 		if (ret)
120 			return ret;
121 		/* enable sensor output to FIFO */
122 		d = 0;
123 		if (st->chip_config.gyro_fifo_enable)
124 			d |= INV_MPU6050_BITS_GYRO_OUT;
125 		if (st->chip_config.accl_fifo_enable)
126 			d |= INV_MPU6050_BIT_ACCEL_OUT;
127 		if (st->chip_config.temp_fifo_enable)
128 			d |= INV_MPU6050_BIT_TEMP_OUT;
129 		if (st->chip_config.magn_fifo_enable)
130 			d |= INV_MPU6050_BIT_SLAVE_0;
131 		ret = regmap_write(st->map, st->reg->fifo_en, d);
132 		if (ret)
133 			return ret;
134 		/* enable FIFO reading */
135 		d = st->chip_config.user_ctrl | INV_MPU6050_BIT_FIFO_EN;
136 		ret = regmap_write(st->map, st->reg->user_ctrl, d);
137 		if (ret)
138 			return ret;
139 		/* enable data interrupt */
140 		ret = regmap_update_bits(st->map, st->reg->int_enable,
141 				INV_MPU6050_BIT_DATA_RDY_EN, INV_MPU6050_BIT_DATA_RDY_EN);
142 	} else {
143 		/* disable data interrupt */
144 		ret = regmap_update_bits(st->map, st->reg->int_enable,
145 				INV_MPU6050_BIT_DATA_RDY_EN, 0);
146 		if (ret)
147 			return ret;
148 		ret = regmap_write(st->map, st->reg->fifo_en, 0);
149 		if (ret)
150 			return ret;
151 		/* restore user_ctrl for disabling FIFO reading */
152 		ret = regmap_write(st->map, st->reg->user_ctrl,
153 				   st->chip_config.user_ctrl);
154 	}
155 
156 	return ret;
157 }
158 
159 /**
160  *  inv_mpu6050_set_enable() - enable chip functions.
161  *  @indio_dev:	Device driver instance.
162  *  @enable: enable/disable
163  */
164 static int inv_mpu6050_set_enable(struct iio_dev *indio_dev, bool enable)
165 {
166 	struct inv_mpu6050_state *st = iio_priv(indio_dev);
167 	struct device *pdev = regmap_get_device(st->map);
168 	unsigned int scan;
169 	int result;
170 
171 	if (enable) {
172 		scan = inv_scan_query(indio_dev);
173 		result = pm_runtime_resume_and_get(pdev);
174 		if (result)
175 			return result;
176 		/*
177 		 * In case autosuspend didn't trigger, turn off first not
178 		 * required sensors excepted WoM
179 		 */
180 		result = inv_mpu6050_switch_engine(st, false, ~scan & ~INV_MPU6050_SENSOR_WOM);
181 		if (result)
182 			goto error_power_off;
183 		result = inv_mpu6050_switch_engine(st, true, scan);
184 		if (result)
185 			goto error_power_off;
186 		st->skip_samples = inv_compute_skip_samples(st);
187 		result = inv_mpu6050_prepare_fifo(st, true);
188 		if (result)
189 			goto error_power_off;
190 	} else {
191 		st->chip_config.gyro_fifo_enable = 0;
192 		st->chip_config.accl_fifo_enable = 0;
193 		st->chip_config.temp_fifo_enable = 0;
194 		st->chip_config.magn_fifo_enable = 0;
195 		result = inv_mpu6050_prepare_fifo(st, false);
196 		if (result)
197 			goto error_power_off;
198 		pm_runtime_mark_last_busy(pdev);
199 		pm_runtime_put_autosuspend(pdev);
200 	}
201 
202 	return 0;
203 
204 error_power_off:
205 	pm_runtime_put_autosuspend(pdev);
206 	return result;
207 }
208 
209 /**
210  * inv_mpu_data_rdy_trigger_set_state() - set data ready interrupt state
211  * @trig: Trigger instance
212  * @state: Desired trigger state
213  */
214 static int inv_mpu_data_rdy_trigger_set_state(struct iio_trigger *trig,
215 					      bool state)
216 {
217 	struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
218 	struct inv_mpu6050_state *st = iio_priv(indio_dev);
219 	int result;
220 
221 	mutex_lock(&st->lock);
222 	result = inv_mpu6050_set_enable(indio_dev, state);
223 	mutex_unlock(&st->lock);
224 
225 	return result;
226 }
227 
228 static const struct iio_trigger_ops inv_mpu_trigger_ops = {
229 	.set_trigger_state = &inv_mpu_data_rdy_trigger_set_state,
230 };
231 
232 static irqreturn_t inv_mpu6050_interrupt_timestamp(int irq, void *p)
233 {
234 	struct iio_dev *indio_dev = p;
235 	struct inv_mpu6050_state *st = iio_priv(indio_dev);
236 
237 	st->it_timestamp = iio_get_time_ns(indio_dev);
238 
239 	return IRQ_WAKE_THREAD;
240 }
241 
242 static irqreturn_t inv_mpu6050_interrupt_handle(int irq, void *p)
243 {
244 	struct iio_dev *indio_dev = p;
245 	struct inv_mpu6050_state *st = iio_priv(indio_dev);
246 	unsigned int int_status, wom_bits;
247 	u64 ev_code;
248 	int result;
249 
250 	switch (st->chip_type) {
251 	case INV_MPU6050:
252 	case INV_MPU6500:
253 	case INV_MPU6515:
254 	case INV_MPU6880:
255 	case INV_MPU6000:
256 	case INV_MPU9150:
257 	case INV_MPU9250:
258 	case INV_MPU9255:
259 		wom_bits = INV_MPU6500_BIT_WOM_INT;
260 		break;
261 	default:
262 		wom_bits = INV_ICM20608_BIT_WOM_INT;
263 		break;
264 	}
265 
266 	scoped_guard(mutex, &st->lock) {
267 		/* ack interrupt and check status */
268 		result = regmap_read(st->map, st->reg->int_status, &int_status);
269 		if (result) {
270 			dev_err(regmap_get_device(st->map), "failed to ack interrupt\n");
271 			return IRQ_HANDLED;
272 		}
273 
274 		/* handle WoM event */
275 		if (st->chip_config.wom_en && (int_status & wom_bits)) {
276 			ev_code = IIO_MOD_EVENT_CODE(IIO_ACCEL, 0, IIO_MOD_X_OR_Y_OR_Z,
277 						     IIO_EV_TYPE_ROC, IIO_EV_DIR_RISING);
278 			iio_push_event(indio_dev, ev_code, st->it_timestamp);
279 		}
280 	}
281 
282 	/* handle raw data interrupt */
283 	if (int_status & INV_MPU6050_BIT_RAW_DATA_RDY_INT) {
284 		indio_dev->pollfunc->timestamp = st->it_timestamp;
285 		iio_trigger_poll_nested(st->trig);
286 	}
287 
288 	return IRQ_HANDLED;
289 }
290 
291 int inv_mpu6050_probe_trigger(struct iio_dev *indio_dev, int irq_type)
292 {
293 	int ret;
294 	struct inv_mpu6050_state *st = iio_priv(indio_dev);
295 
296 	st->trig = devm_iio_trigger_alloc(&indio_dev->dev,
297 					  "%s-dev%d",
298 					  indio_dev->name,
299 					  iio_device_id(indio_dev));
300 	if (!st->trig)
301 		return -ENOMEM;
302 
303 	ret = devm_request_threaded_irq(&indio_dev->dev, st->irq,
304 					&inv_mpu6050_interrupt_timestamp,
305 					&inv_mpu6050_interrupt_handle,
306 					irq_type, "inv_mpu", indio_dev);
307 	if (ret)
308 		return ret;
309 
310 	st->trig->dev.parent = regmap_get_device(st->map);
311 	st->trig->ops = &inv_mpu_trigger_ops;
312 	iio_trigger_set_drvdata(st->trig, indio_dev);
313 
314 	ret = devm_iio_trigger_register(&indio_dev->dev, st->trig);
315 	if (ret)
316 		return ret;
317 
318 	indio_dev->trig = iio_trigger_get(st->trig);
319 
320 	return 0;
321 }
322