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>
10 #include "inv_mpu_iio.h"
12 static unsigned int inv_scan_query_mpu6050(struct iio_dev *indio_dev)
14 struct inv_mpu6050_state *st = iio_priv(indio_dev);
18 * If the MPU6050 is just used as a trigger, then the scan mask
19 * is not allocated so we simply enable the temperature channel
20 * as a dummy and bail out.
22 if (!indio_dev->active_scan_mask) {
23 st->chip_config.temp_fifo_enable = true;
24 return INV_MPU6050_SENSOR_TEMP;
27 st->chip_config.gyro_fifo_enable =
28 test_bit(INV_MPU6050_SCAN_GYRO_X,
29 indio_dev->active_scan_mask) ||
30 test_bit(INV_MPU6050_SCAN_GYRO_Y,
31 indio_dev->active_scan_mask) ||
32 test_bit(INV_MPU6050_SCAN_GYRO_Z,
33 indio_dev->active_scan_mask);
35 st->chip_config.accl_fifo_enable =
36 test_bit(INV_MPU6050_SCAN_ACCL_X,
37 indio_dev->active_scan_mask) ||
38 test_bit(INV_MPU6050_SCAN_ACCL_Y,
39 indio_dev->active_scan_mask) ||
40 test_bit(INV_MPU6050_SCAN_ACCL_Z,
41 indio_dev->active_scan_mask);
43 st->chip_config.temp_fifo_enable =
44 test_bit(INV_MPU6050_SCAN_TEMP, indio_dev->active_scan_mask);
47 if (st->chip_config.gyro_fifo_enable)
48 mask |= INV_MPU6050_SENSOR_GYRO;
49 if (st->chip_config.accl_fifo_enable)
50 mask |= INV_MPU6050_SENSOR_ACCL;
51 if (st->chip_config.temp_fifo_enable)
52 mask |= INV_MPU6050_SENSOR_TEMP;
57 static unsigned int inv_scan_query_mpu9x50(struct iio_dev *indio_dev)
59 struct inv_mpu6050_state *st = iio_priv(indio_dev);
62 mask = inv_scan_query_mpu6050(indio_dev);
64 /* no magnetometer if i2c auxiliary bus is used */
65 if (st->magn_disabled)
68 st->chip_config.magn_fifo_enable =
69 test_bit(INV_MPU9X50_SCAN_MAGN_X,
70 indio_dev->active_scan_mask) ||
71 test_bit(INV_MPU9X50_SCAN_MAGN_Y,
72 indio_dev->active_scan_mask) ||
73 test_bit(INV_MPU9X50_SCAN_MAGN_Z,
74 indio_dev->active_scan_mask);
75 if (st->chip_config.magn_fifo_enable)
76 mask |= INV_MPU6050_SENSOR_MAGN;
81 static unsigned int inv_scan_query(struct iio_dev *indio_dev)
83 struct inv_mpu6050_state *st = iio_priv(indio_dev);
85 switch (st->chip_type) {
89 return inv_scan_query_mpu9x50(indio_dev);
91 return inv_scan_query_mpu6050(indio_dev);
95 static unsigned int inv_compute_skip_samples(const struct inv_mpu6050_state *st)
97 unsigned int skip_samples = 0;
99 /* mag first sample is always not ready, skip it */
100 if (st->chip_config.magn_fifo_enable)
106 int inv_mpu6050_prepare_fifo(struct inv_mpu6050_state *st, bool enable)
112 /* reset timestamping */
113 inv_sensors_timestamp_reset(&st->timestamp);
115 d = st->chip_config.user_ctrl | INV_MPU6050_BIT_FIFO_RST;
116 ret = regmap_write(st->map, st->reg->user_ctrl, d);
119 /* enable sensor output to FIFO */
121 if (st->chip_config.gyro_fifo_enable)
122 d |= INV_MPU6050_BITS_GYRO_OUT;
123 if (st->chip_config.accl_fifo_enable)
124 d |= INV_MPU6050_BIT_ACCEL_OUT;
125 if (st->chip_config.temp_fifo_enable)
126 d |= INV_MPU6050_BIT_TEMP_OUT;
127 if (st->chip_config.magn_fifo_enable)
128 d |= INV_MPU6050_BIT_SLAVE_0;
129 ret = regmap_write(st->map, st->reg->fifo_en, d);
132 /* enable FIFO reading */
133 d = st->chip_config.user_ctrl | INV_MPU6050_BIT_FIFO_EN;
134 ret = regmap_write(st->map, st->reg->user_ctrl, d);
137 /* enable interrupt */
138 ret = regmap_write(st->map, st->reg->int_enable,
139 INV_MPU6050_BIT_DATA_RDY_EN);
141 ret = regmap_write(st->map, st->reg->int_enable, 0);
144 ret = regmap_write(st->map, st->reg->fifo_en, 0);
147 /* restore user_ctrl for disabling FIFO reading */
148 ret = regmap_write(st->map, st->reg->user_ctrl,
149 st->chip_config.user_ctrl);
156 * inv_mpu6050_set_enable() - enable chip functions.
157 * @indio_dev: Device driver instance.
158 * @enable: enable/disable
160 static int inv_mpu6050_set_enable(struct iio_dev *indio_dev, bool enable)
162 struct inv_mpu6050_state *st = iio_priv(indio_dev);
163 struct device *pdev = regmap_get_device(st->map);
168 scan = inv_scan_query(indio_dev);
169 result = pm_runtime_resume_and_get(pdev);
173 * In case autosuspend didn't trigger, turn off first not
176 result = inv_mpu6050_switch_engine(st, false, ~scan);
178 goto error_power_off;
179 result = inv_mpu6050_switch_engine(st, true, scan);
181 goto error_power_off;
182 st->skip_samples = inv_compute_skip_samples(st);
183 result = inv_mpu6050_prepare_fifo(st, true);
185 goto error_power_off;
187 result = inv_mpu6050_prepare_fifo(st, false);
189 goto error_power_off;
190 pm_runtime_mark_last_busy(pdev);
191 pm_runtime_put_autosuspend(pdev);
197 pm_runtime_put_autosuspend(pdev);
202 * inv_mpu_data_rdy_trigger_set_state() - set data ready interrupt state
203 * @trig: Trigger instance
204 * @state: Desired trigger state
206 static int inv_mpu_data_rdy_trigger_set_state(struct iio_trigger *trig,
209 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
210 struct inv_mpu6050_state *st = iio_priv(indio_dev);
213 mutex_lock(&st->lock);
214 result = inv_mpu6050_set_enable(indio_dev, state);
215 mutex_unlock(&st->lock);
220 static const struct iio_trigger_ops inv_mpu_trigger_ops = {
221 .set_trigger_state = &inv_mpu_data_rdy_trigger_set_state,
224 int inv_mpu6050_probe_trigger(struct iio_dev *indio_dev, int irq_type)
227 struct inv_mpu6050_state *st = iio_priv(indio_dev);
229 st->trig = devm_iio_trigger_alloc(&indio_dev->dev,
232 iio_device_id(indio_dev));
236 ret = devm_request_irq(&indio_dev->dev, st->irq,
237 &iio_trigger_generic_data_rdy_poll,
244 st->trig->dev.parent = regmap_get_device(st->map);
245 st->trig->ops = &inv_mpu_trigger_ops;
246 iio_trigger_set_drvdata(st->trig, indio_dev);
248 ret = devm_iio_trigger_register(&indio_dev->dev, st->trig);
252 indio_dev->trig = iio_trigger_get(st->trig);