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_MPU6000: 252 case INV_MPU6050: 253 case INV_MPU9150: 254 /* 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. 258 */ 259 wom_bits = 0; 260 int_status = INV_MPU6050_BIT_RAW_DATA_RDY_INT; 261 goto data_ready_interrupt; 262 case INV_MPU6500: 263 case INV_MPU6515: 264 case INV_MPU6880: 265 case INV_MPU9250: 266 case INV_MPU9255: 267 wom_bits = INV_MPU6500_BIT_WOM_INT; 268 break; 269 default: 270 wom_bits = INV_ICM20608_BIT_WOM_INT; 271 break; 272 } 273 274 scoped_guard(mutex, &st->lock) { 275 /* ack interrupt and check status */ 276 result = regmap_read(st->map, st->reg->int_status, &int_status); 277 if (result) { 278 dev_err(regmap_get_device(st->map), "failed to ack interrupt\n"); 279 return IRQ_HANDLED; 280 } 281 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); 287 } 288 } 289 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); 295 } 296 297 return IRQ_HANDLED; 298 } 299 300 int inv_mpu6050_probe_trigger(struct iio_dev *indio_dev, int irq_type) 301 { 302 int ret; 303 struct inv_mpu6050_state *st = iio_priv(indio_dev); 304 305 st->trig = devm_iio_trigger_alloc(&indio_dev->dev, 306 "%s-dev%d", 307 indio_dev->name, 308 iio_device_id(indio_dev)); 309 if (!st->trig) 310 return -ENOMEM; 311 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); 317 if (ret) 318 return ret; 319 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); 323 324 ret = devm_iio_trigger_register(&indio_dev->dev, st->trig); 325 if (ret) 326 return ret; 327 328 indio_dev->trig = iio_trigger_get(st->trig); 329 330 return 0; 331 } 332