1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2012 Invensense, Inc.
6 #include <linux/pm_runtime.h>
8 #include <linux/iio/common/inv_sensors_timestamp.h>
9 #include <linux/iio/events.h>
11 #include "inv_mpu_iio.h"
13 static unsigned int inv_scan_query_mpu6050(struct iio_dev *indio_dev)
15 struct inv_mpu6050_state *st = iio_priv(indio_dev);
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.
23 if (!indio_dev->active_scan_mask) {
24 st->chip_config.temp_fifo_enable = true;
25 return INV_MPU6050_SENSOR_TEMP;
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);
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);
44 st->chip_config.temp_fifo_enable =
45 test_bit(INV_MPU6050_SCAN_TEMP, indio_dev->active_scan_mask);
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;
58 static unsigned int inv_scan_query_mpu9x50(struct iio_dev *indio_dev)
60 struct inv_mpu6050_state *st = iio_priv(indio_dev);
63 mask = inv_scan_query_mpu6050(indio_dev);
65 /* no magnetometer if i2c auxiliary bus is used */
66 if (st->magn_disabled)
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;
82 static unsigned int inv_scan_query(struct iio_dev *indio_dev)
84 struct inv_mpu6050_state *st = iio_priv(indio_dev);
86 switch (st->chip_type) {
90 return inv_scan_query_mpu9x50(indio_dev);
92 return inv_scan_query_mpu6050(indio_dev);
96 static unsigned int inv_compute_skip_samples(const struct inv_mpu6050_state *st)
98 unsigned int skip_samples = 0;
100 /* mag first sample is always not ready, skip it */
101 if (st->chip_config.magn_fifo_enable)
107 int inv_mpu6050_prepare_fifo(struct inv_mpu6050_state *st, bool enable)
113 /* reset timestamping */
114 inv_sensors_timestamp_reset(&st->timestamp);
115 inv_sensors_timestamp_apply_odr(&st->timestamp, 0, 0, 0);
117 d = st->chip_config.user_ctrl | INV_MPU6050_BIT_FIFO_RST;
118 ret = regmap_write(st->map, st->reg->user_ctrl, d);
121 /* enable sensor output to FIFO */
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);
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);
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);
143 /* disable data interrupt */
144 ret = regmap_update_bits(st->map, st->reg->int_enable,
145 INV_MPU6050_BIT_DATA_RDY_EN, 0);
148 ret = regmap_write(st->map, st->reg->fifo_en, 0);
151 /* restore user_ctrl for disabling FIFO reading */
152 ret = regmap_write(st->map, st->reg->user_ctrl,
153 st->chip_config.user_ctrl);
160 * inv_mpu6050_set_enable() - enable chip functions.
161 * @indio_dev: Device driver instance.
162 * @enable: enable/disable
164 static int inv_mpu6050_set_enable(struct iio_dev *indio_dev, bool enable)
166 struct inv_mpu6050_state *st = iio_priv(indio_dev);
167 struct device *pdev = regmap_get_device(st->map);
172 scan = inv_scan_query(indio_dev);
173 result = pm_runtime_resume_and_get(pdev);
177 * In case autosuspend didn't trigger, turn off first not
178 * required sensors excepted WoM
180 result = inv_mpu6050_switch_engine(st, false, ~scan & ~INV_MPU6050_SENSOR_WOM);
182 goto error_power_off;
183 result = inv_mpu6050_switch_engine(st, true, scan);
185 goto error_power_off;
186 st->skip_samples = inv_compute_skip_samples(st);
187 result = inv_mpu6050_prepare_fifo(st, true);
189 goto error_power_off;
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);
197 goto error_power_off;
198 pm_runtime_mark_last_busy(pdev);
199 pm_runtime_put_autosuspend(pdev);
205 pm_runtime_put_autosuspend(pdev);
210 * inv_mpu_data_rdy_trigger_set_state() - set data ready interrupt state
211 * @trig: Trigger instance
212 * @state: Desired trigger state
214 static int inv_mpu_data_rdy_trigger_set_state(struct iio_trigger *trig,
217 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
218 struct inv_mpu6050_state *st = iio_priv(indio_dev);
221 mutex_lock(&st->lock);
222 result = inv_mpu6050_set_enable(indio_dev, state);
223 mutex_unlock(&st->lock);
228 static const struct iio_trigger_ops inv_mpu_trigger_ops = {
229 .set_trigger_state = &inv_mpu_data_rdy_trigger_set_state,
232 static irqreturn_t inv_mpu6050_interrupt_timestamp(int irq, void *p)
234 struct iio_dev *indio_dev = p;
235 struct inv_mpu6050_state *st = iio_priv(indio_dev);
237 st->it_timestamp = iio_get_time_ns(indio_dev);
239 return IRQ_WAKE_THREAD;
242 static irqreturn_t inv_mpu6050_interrupt_handle(int irq, void *p)
244 struct iio_dev *indio_dev = p;
245 struct inv_mpu6050_state *st = iio_priv(indio_dev);
246 unsigned int int_status, wom_bits;
250 switch (st->chip_type) {
255 * WoM is not supported and interrupt status read seems to be broken for
256 * some chips. Since data ready is the only interrupt, bypass interrupt
257 * status read and always assert data ready bit.
260 int_status = INV_MPU6050_BIT_RAW_DATA_RDY_INT;
261 goto data_ready_interrupt;
267 wom_bits = INV_MPU6500_BIT_WOM_INT;
270 wom_bits = INV_ICM20608_BIT_WOM_INT;
274 scoped_guard(mutex, &st->lock) {
275 /* ack interrupt and check status */
276 result = regmap_read(st->map, st->reg->int_status, &int_status);
278 dev_err(regmap_get_device(st->map), "failed to ack interrupt\n");
282 /* handle WoM event */
283 if (st->chip_config.wom_en && (int_status & wom_bits)) {
284 ev_code = IIO_MOD_EVENT_CODE(IIO_ACCEL, 0, IIO_MOD_X_OR_Y_OR_Z,
285 IIO_EV_TYPE_ROC, IIO_EV_DIR_RISING);
286 iio_push_event(indio_dev, ev_code, st->it_timestamp);
290 data_ready_interrupt:
291 /* handle raw data interrupt */
292 if (int_status & INV_MPU6050_BIT_RAW_DATA_RDY_INT) {
293 indio_dev->pollfunc->timestamp = st->it_timestamp;
294 iio_trigger_poll_nested(st->trig);
300 int inv_mpu6050_probe_trigger(struct iio_dev *indio_dev, int irq_type)
303 struct inv_mpu6050_state *st = iio_priv(indio_dev);
305 st->trig = devm_iio_trigger_alloc(&indio_dev->dev,
308 iio_device_id(indio_dev));
312 irq_type |= IRQF_ONESHOT;
313 ret = devm_request_threaded_irq(&indio_dev->dev, st->irq,
314 &inv_mpu6050_interrupt_timestamp,
315 &inv_mpu6050_interrupt_handle,
316 irq_type, "inv_mpu", indio_dev);
320 st->trig->dev.parent = regmap_get_device(st->map);
321 st->trig->ops = &inv_mpu_trigger_ops;
322 iio_trigger_set_drvdata(st->trig, indio_dev);
324 ret = devm_iio_trigger_register(&indio_dev->dev, st->trig);
328 indio_dev->trig = iio_trigger_get(st->trig);