1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2012 Invensense, Inc.
6 #include <linux/pm_runtime.h>
7 #include "inv_mpu_iio.h"
9 static unsigned int inv_scan_query_mpu6050(struct iio_dev *indio_dev)
11 struct inv_mpu6050_state *st = iio_priv(indio_dev);
15 * If the MPU6050 is just used as a trigger, then the scan mask
16 * is not allocated so we simply enable the temperature channel
17 * as a dummy and bail out.
19 if (!indio_dev->active_scan_mask) {
20 st->chip_config.temp_fifo_enable = true;
21 return INV_MPU6050_SENSOR_TEMP;
24 st->chip_config.gyro_fifo_enable =
25 test_bit(INV_MPU6050_SCAN_GYRO_X,
26 indio_dev->active_scan_mask) ||
27 test_bit(INV_MPU6050_SCAN_GYRO_Y,
28 indio_dev->active_scan_mask) ||
29 test_bit(INV_MPU6050_SCAN_GYRO_Z,
30 indio_dev->active_scan_mask);
32 st->chip_config.accl_fifo_enable =
33 test_bit(INV_MPU6050_SCAN_ACCL_X,
34 indio_dev->active_scan_mask) ||
35 test_bit(INV_MPU6050_SCAN_ACCL_Y,
36 indio_dev->active_scan_mask) ||
37 test_bit(INV_MPU6050_SCAN_ACCL_Z,
38 indio_dev->active_scan_mask);
40 st->chip_config.temp_fifo_enable =
41 test_bit(INV_MPU6050_SCAN_TEMP, indio_dev->active_scan_mask);
44 if (st->chip_config.gyro_fifo_enable)
45 mask |= INV_MPU6050_SENSOR_GYRO;
46 if (st->chip_config.accl_fifo_enable)
47 mask |= INV_MPU6050_SENSOR_ACCL;
48 if (st->chip_config.temp_fifo_enable)
49 mask |= INV_MPU6050_SENSOR_TEMP;
54 static unsigned int inv_scan_query_mpu9x50(struct iio_dev *indio_dev)
56 struct inv_mpu6050_state *st = iio_priv(indio_dev);
59 mask = inv_scan_query_mpu6050(indio_dev);
61 /* no magnetometer if i2c auxiliary bus is used */
62 if (st->magn_disabled)
65 st->chip_config.magn_fifo_enable =
66 test_bit(INV_MPU9X50_SCAN_MAGN_X,
67 indio_dev->active_scan_mask) ||
68 test_bit(INV_MPU9X50_SCAN_MAGN_Y,
69 indio_dev->active_scan_mask) ||
70 test_bit(INV_MPU9X50_SCAN_MAGN_Z,
71 indio_dev->active_scan_mask);
72 if (st->chip_config.magn_fifo_enable)
73 mask |= INV_MPU6050_SENSOR_MAGN;
78 static unsigned int inv_scan_query(struct iio_dev *indio_dev)
80 struct inv_mpu6050_state *st = iio_priv(indio_dev);
82 switch (st->chip_type) {
86 return inv_scan_query_mpu9x50(indio_dev);
88 return inv_scan_query_mpu6050(indio_dev);
92 static unsigned int inv_compute_skip_samples(const struct inv_mpu6050_state *st)
94 unsigned int skip_samples = 0;
96 /* mag first sample is always not ready, skip it */
97 if (st->chip_config.magn_fifo_enable)
103 int inv_mpu6050_prepare_fifo(struct inv_mpu6050_state *st, bool enable)
109 st->it_timestamp = 0;
111 d = st->chip_config.user_ctrl | INV_MPU6050_BIT_FIFO_RST;
112 ret = regmap_write(st->map, st->reg->user_ctrl, d);
115 /* enable sensor output to FIFO */
117 if (st->chip_config.gyro_fifo_enable)
118 d |= INV_MPU6050_BITS_GYRO_OUT;
119 if (st->chip_config.accl_fifo_enable)
120 d |= INV_MPU6050_BIT_ACCEL_OUT;
121 if (st->chip_config.temp_fifo_enable)
122 d |= INV_MPU6050_BIT_TEMP_OUT;
123 if (st->chip_config.magn_fifo_enable)
124 d |= INV_MPU6050_BIT_SLAVE_0;
125 ret = regmap_write(st->map, st->reg->fifo_en, d);
128 /* enable FIFO reading */
129 d = st->chip_config.user_ctrl | INV_MPU6050_BIT_FIFO_EN;
130 ret = regmap_write(st->map, st->reg->user_ctrl, d);
133 /* enable interrupt */
134 ret = regmap_write(st->map, st->reg->int_enable,
135 INV_MPU6050_BIT_DATA_RDY_EN);
137 ret = regmap_write(st->map, st->reg->int_enable, 0);
140 ret = regmap_write(st->map, st->reg->fifo_en, 0);
143 /* restore user_ctrl for disabling FIFO reading */
144 ret = regmap_write(st->map, st->reg->user_ctrl,
145 st->chip_config.user_ctrl);
152 * inv_mpu6050_set_enable() - enable chip functions.
153 * @indio_dev: Device driver instance.
154 * @enable: enable/disable
156 static int inv_mpu6050_set_enable(struct iio_dev *indio_dev, bool enable)
158 struct inv_mpu6050_state *st = iio_priv(indio_dev);
159 struct device *pdev = regmap_get_device(st->map);
164 scan = inv_scan_query(indio_dev);
165 result = pm_runtime_resume_and_get(pdev);
169 * In case autosuspend didn't trigger, turn off first not
172 result = inv_mpu6050_switch_engine(st, false, ~scan);
174 goto error_power_off;
175 result = inv_mpu6050_switch_engine(st, true, scan);
177 goto error_power_off;
178 st->skip_samples = inv_compute_skip_samples(st);
179 result = inv_mpu6050_prepare_fifo(st, true);
181 goto error_power_off;
183 result = inv_mpu6050_prepare_fifo(st, false);
185 goto error_power_off;
186 pm_runtime_mark_last_busy(pdev);
187 pm_runtime_put_autosuspend(pdev);
193 pm_runtime_put_autosuspend(pdev);
198 * inv_mpu_data_rdy_trigger_set_state() - set data ready interrupt state
199 * @trig: Trigger instance
200 * @state: Desired trigger state
202 static int inv_mpu_data_rdy_trigger_set_state(struct iio_trigger *trig,
205 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
206 struct inv_mpu6050_state *st = iio_priv(indio_dev);
209 mutex_lock(&st->lock);
210 result = inv_mpu6050_set_enable(indio_dev, state);
211 mutex_unlock(&st->lock);
216 static const struct iio_trigger_ops inv_mpu_trigger_ops = {
217 .set_trigger_state = &inv_mpu_data_rdy_trigger_set_state,
220 int inv_mpu6050_probe_trigger(struct iio_dev *indio_dev, int irq_type)
223 struct inv_mpu6050_state *st = iio_priv(indio_dev);
225 st->trig = devm_iio_trigger_alloc(&indio_dev->dev,
228 iio_device_id(indio_dev));
232 ret = devm_request_irq(&indio_dev->dev, st->irq,
233 &iio_trigger_generic_data_rdy_poll,
240 st->trig->dev.parent = regmap_get_device(st->map);
241 st->trig->ops = &inv_mpu_trigger_ops;
242 iio_trigger_set_drvdata(st->trig, indio_dev);
244 ret = devm_iio_trigger_register(&indio_dev->dev, st->trig);
248 indio_dev->trig = iio_trigger_get(st->trig);