2019-05-29 22:17:56 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2013-02-02 08:26:00 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2012 Invensense, Inc.
|
|
|
|
*/
|
|
|
|
|
2020-02-19 22:39:57 +08:00
|
|
|
#include <linux/pm_runtime.h>
|
2013-02-02 08:26:00 +08:00
|
|
|
#include "inv_mpu_iio.h"
|
|
|
|
|
2020-02-19 22:39:53 +08:00
|
|
|
static unsigned int inv_scan_query_mpu6050(struct iio_dev *indio_dev)
|
2013-02-02 08:26:00 +08:00
|
|
|
{
|
|
|
|
struct inv_mpu6050_state *st = iio_priv(indio_dev);
|
2020-02-19 22:39:53 +08:00
|
|
|
unsigned int mask;
|
2013-02-02 08:26:00 +08:00
|
|
|
|
|
|
|
st->chip_config.gyro_fifo_enable =
|
|
|
|
test_bit(INV_MPU6050_SCAN_GYRO_X,
|
2016-02-18 23:53:12 +08:00
|
|
|
indio_dev->active_scan_mask) ||
|
|
|
|
test_bit(INV_MPU6050_SCAN_GYRO_Y,
|
|
|
|
indio_dev->active_scan_mask) ||
|
|
|
|
test_bit(INV_MPU6050_SCAN_GYRO_Z,
|
|
|
|
indio_dev->active_scan_mask);
|
2013-02-02 08:26:00 +08:00
|
|
|
|
|
|
|
st->chip_config.accl_fifo_enable =
|
|
|
|
test_bit(INV_MPU6050_SCAN_ACCL_X,
|
2016-02-18 23:53:12 +08:00
|
|
|
indio_dev->active_scan_mask) ||
|
|
|
|
test_bit(INV_MPU6050_SCAN_ACCL_Y,
|
|
|
|
indio_dev->active_scan_mask) ||
|
|
|
|
test_bit(INV_MPU6050_SCAN_ACCL_Z,
|
|
|
|
indio_dev->active_scan_mask);
|
2020-01-17 01:56:45 +08:00
|
|
|
|
|
|
|
st->chip_config.temp_fifo_enable =
|
|
|
|
test_bit(INV_MPU6050_SCAN_TEMP, indio_dev->active_scan_mask);
|
2020-02-19 22:39:53 +08:00
|
|
|
|
|
|
|
mask = 0;
|
|
|
|
if (st->chip_config.gyro_fifo_enable)
|
|
|
|
mask |= INV_MPU6050_SENSOR_GYRO;
|
|
|
|
if (st->chip_config.accl_fifo_enable)
|
|
|
|
mask |= INV_MPU6050_SENSOR_ACCL;
|
|
|
|
if (st->chip_config.temp_fifo_enable)
|
|
|
|
mask |= INV_MPU6050_SENSOR_TEMP;
|
|
|
|
|
|
|
|
return mask;
|
2013-02-02 08:26:00 +08:00
|
|
|
}
|
|
|
|
|
2020-02-19 22:39:53 +08:00
|
|
|
static unsigned int inv_scan_query_mpu9x50(struct iio_dev *indio_dev)
|
2019-09-16 17:42:09 +08:00
|
|
|
{
|
|
|
|
struct inv_mpu6050_state *st = iio_priv(indio_dev);
|
2020-02-19 22:39:53 +08:00
|
|
|
unsigned int mask;
|
2019-09-16 17:42:09 +08:00
|
|
|
|
2020-02-19 22:39:53 +08:00
|
|
|
mask = inv_scan_query_mpu6050(indio_dev);
|
2019-09-16 17:42:09 +08:00
|
|
|
|
|
|
|
/* no magnetometer if i2c auxiliary bus is used */
|
|
|
|
if (st->magn_disabled)
|
2020-02-19 22:39:53 +08:00
|
|
|
return mask;
|
2019-09-16 17:42:09 +08:00
|
|
|
|
|
|
|
st->chip_config.magn_fifo_enable =
|
|
|
|
test_bit(INV_MPU9X50_SCAN_MAGN_X,
|
|
|
|
indio_dev->active_scan_mask) ||
|
|
|
|
test_bit(INV_MPU9X50_SCAN_MAGN_Y,
|
|
|
|
indio_dev->active_scan_mask) ||
|
|
|
|
test_bit(INV_MPU9X50_SCAN_MAGN_Z,
|
|
|
|
indio_dev->active_scan_mask);
|
2020-02-19 22:39:53 +08:00
|
|
|
if (st->chip_config.magn_fifo_enable)
|
|
|
|
mask |= INV_MPU6050_SENSOR_MAGN;
|
|
|
|
|
|
|
|
return mask;
|
2019-09-16 17:42:09 +08:00
|
|
|
}
|
|
|
|
|
2020-02-19 22:39:53 +08:00
|
|
|
static unsigned int inv_scan_query(struct iio_dev *indio_dev)
|
2019-09-16 17:42:09 +08:00
|
|
|
{
|
|
|
|
struct inv_mpu6050_state *st = iio_priv(indio_dev);
|
|
|
|
|
|
|
|
switch (st->chip_type) {
|
2019-11-15 22:06:22 +08:00
|
|
|
case INV_MPU9150:
|
2019-09-16 17:42:09 +08:00
|
|
|
case INV_MPU9250:
|
|
|
|
case INV_MPU9255:
|
|
|
|
return inv_scan_query_mpu9x50(indio_dev);
|
|
|
|
default:
|
|
|
|
return inv_scan_query_mpu6050(indio_dev);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned int inv_compute_skip_samples(const struct inv_mpu6050_state *st)
|
|
|
|
{
|
|
|
|
unsigned int gyro_skip = 0;
|
|
|
|
unsigned int magn_skip = 0;
|
|
|
|
unsigned int skip_samples;
|
|
|
|
|
|
|
|
/* gyro first sample is out of specs, skip it */
|
|
|
|
if (st->chip_config.gyro_fifo_enable)
|
|
|
|
gyro_skip = 1;
|
|
|
|
|
|
|
|
/* mag first sample is always not ready, skip it */
|
|
|
|
if (st->chip_config.magn_fifo_enable)
|
|
|
|
magn_skip = 1;
|
|
|
|
|
|
|
|
/* compute first samples to skip */
|
|
|
|
skip_samples = gyro_skip;
|
|
|
|
if (magn_skip > skip_samples)
|
|
|
|
skip_samples = magn_skip;
|
|
|
|
|
|
|
|
return skip_samples;
|
|
|
|
}
|
|
|
|
|
2020-02-19 22:39:55 +08:00
|
|
|
int inv_mpu6050_prepare_fifo(struct inv_mpu6050_state *st, bool enable)
|
|
|
|
{
|
|
|
|
uint8_t d;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (enable) {
|
|
|
|
st->it_timestamp = 0;
|
|
|
|
/* reset FIFO */
|
|
|
|
d = st->chip_config.user_ctrl | INV_MPU6050_BIT_FIFO_RST;
|
|
|
|
ret = regmap_write(st->map, st->reg->user_ctrl, d);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
/* enable sensor output to FIFO */
|
|
|
|
d = 0;
|
|
|
|
if (st->chip_config.gyro_fifo_enable)
|
|
|
|
d |= INV_MPU6050_BITS_GYRO_OUT;
|
|
|
|
if (st->chip_config.accl_fifo_enable)
|
|
|
|
d |= INV_MPU6050_BIT_ACCEL_OUT;
|
|
|
|
if (st->chip_config.temp_fifo_enable)
|
|
|
|
d |= INV_MPU6050_BIT_TEMP_OUT;
|
|
|
|
if (st->chip_config.magn_fifo_enable)
|
|
|
|
d |= INV_MPU6050_BIT_SLAVE_0;
|
|
|
|
ret = regmap_write(st->map, st->reg->fifo_en, d);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
/* enable FIFO reading */
|
|
|
|
d = st->chip_config.user_ctrl | INV_MPU6050_BIT_FIFO_EN;
|
|
|
|
ret = regmap_write(st->map, st->reg->user_ctrl, d);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
/* enable interrupt */
|
|
|
|
ret = regmap_write(st->map, st->reg->int_enable,
|
|
|
|
INV_MPU6050_BIT_DATA_RDY_EN);
|
|
|
|
} else {
|
|
|
|
ret = regmap_write(st->map, st->reg->int_enable, 0);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
ret = regmap_write(st->map, st->reg->fifo_en, 0);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
/* restore user_ctrl for disabling FIFO reading */
|
|
|
|
ret = regmap_write(st->map, st->reg->user_ctrl,
|
|
|
|
st->chip_config.user_ctrl);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-02-02 08:26:00 +08:00
|
|
|
/**
|
|
|
|
* inv_mpu6050_set_enable() - enable chip functions.
|
|
|
|
* @indio_dev: Device driver instance.
|
|
|
|
* @enable: enable/disable
|
|
|
|
*/
|
|
|
|
static int inv_mpu6050_set_enable(struct iio_dev *indio_dev, bool enable)
|
|
|
|
{
|
|
|
|
struct inv_mpu6050_state *st = iio_priv(indio_dev);
|
2020-02-19 22:39:57 +08:00
|
|
|
struct device *pdev = regmap_get_device(st->map);
|
2020-02-19 22:39:53 +08:00
|
|
|
unsigned int scan;
|
2013-02-02 08:26:00 +08:00
|
|
|
int result;
|
|
|
|
|
|
|
|
if (enable) {
|
2020-02-19 22:39:57 +08:00
|
|
|
scan = inv_scan_query(indio_dev);
|
|
|
|
result = pm_runtime_get_sync(pdev);
|
|
|
|
if (result < 0) {
|
|
|
|
pm_runtime_put_noidle(pdev);
|
2013-02-02 08:26:00 +08:00
|
|
|
return result;
|
2020-02-19 22:39:57 +08:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* In case autosuspend didn't trigger, turn off first not
|
|
|
|
* required sensors.
|
|
|
|
*/
|
|
|
|
result = inv_mpu6050_switch_engine(st, false, ~scan);
|
|
|
|
if (result)
|
|
|
|
goto error_power_off;
|
2020-02-19 22:39:53 +08:00
|
|
|
result = inv_mpu6050_switch_engine(st, true, scan);
|
|
|
|
if (result)
|
|
|
|
goto error_power_off;
|
2019-09-16 17:42:09 +08:00
|
|
|
st->skip_samples = inv_compute_skip_samples(st);
|
2020-02-19 22:39:55 +08:00
|
|
|
result = inv_mpu6050_prepare_fifo(st, true);
|
2013-02-02 08:26:00 +08:00
|
|
|
if (result)
|
2020-02-19 22:39:57 +08:00
|
|
|
goto error_power_off;
|
2013-02-02 08:26:00 +08:00
|
|
|
} else {
|
2020-02-19 22:39:55 +08:00
|
|
|
result = inv_mpu6050_prepare_fifo(st, false);
|
2013-02-02 08:26:00 +08:00
|
|
|
if (result)
|
2018-04-23 18:33:30 +08:00
|
|
|
goto error_power_off;
|
2020-02-19 22:39:57 +08:00
|
|
|
pm_runtime_mark_last_busy(pdev);
|
|
|
|
pm_runtime_put_autosuspend(pdev);
|
2013-02-02 08:26:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2018-04-23 18:33:30 +08:00
|
|
|
|
|
|
|
error_power_off:
|
2020-02-19 22:39:57 +08:00
|
|
|
pm_runtime_put_autosuspend(pdev);
|
2018-04-23 18:33:30 +08:00
|
|
|
return result;
|
2013-02-02 08:26:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* inv_mpu_data_rdy_trigger_set_state() - set data ready interrupt state
|
|
|
|
* @trig: Trigger instance
|
|
|
|
* @state: Desired trigger state
|
|
|
|
*/
|
|
|
|
static int inv_mpu_data_rdy_trigger_set_state(struct iio_trigger *trig,
|
2016-02-18 23:53:12 +08:00
|
|
|
bool state)
|
2013-02-02 08:26:00 +08:00
|
|
|
{
|
2017-06-07 21:41:42 +08:00
|
|
|
struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
|
|
|
|
struct inv_mpu6050_state *st = iio_priv(indio_dev);
|
|
|
|
int result;
|
|
|
|
|
|
|
|
mutex_lock(&st->lock);
|
|
|
|
result = inv_mpu6050_set_enable(indio_dev, state);
|
|
|
|
mutex_unlock(&st->lock);
|
|
|
|
|
|
|
|
return result;
|
2013-02-02 08:26:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static const struct iio_trigger_ops inv_mpu_trigger_ops = {
|
|
|
|
.set_trigger_state = &inv_mpu_data_rdy_trigger_set_state,
|
|
|
|
};
|
|
|
|
|
2018-04-21 00:54:00 +08:00
|
|
|
int inv_mpu6050_probe_trigger(struct iio_dev *indio_dev, int irq_type)
|
2013-02-02 08:26:00 +08:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
struct inv_mpu6050_state *st = iio_priv(indio_dev);
|
|
|
|
|
2015-01-22 11:38:02 +08:00
|
|
|
st->trig = devm_iio_trigger_alloc(&indio_dev->dev,
|
|
|
|
"%s-dev%d",
|
|
|
|
indio_dev->name,
|
|
|
|
indio_dev->id);
|
2015-01-22 11:38:04 +08:00
|
|
|
if (!st->trig)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2016-02-12 19:44:44 +08:00
|
|
|
ret = devm_request_irq(&indio_dev->dev, st->irq,
|
2015-01-22 11:38:03 +08:00
|
|
|
&iio_trigger_generic_data_rdy_poll,
|
2018-04-21 00:54:00 +08:00
|
|
|
irq_type,
|
2015-01-22 11:38:03 +08:00
|
|
|
"inv_mpu",
|
|
|
|
st->trig);
|
2013-02-02 08:26:00 +08:00
|
|
|
if (ret)
|
2015-01-22 11:38:04 +08:00
|
|
|
return ret;
|
|
|
|
|
2016-02-12 19:44:44 +08:00
|
|
|
st->trig->dev.parent = regmap_get_device(st->map);
|
2013-02-02 08:26:00 +08:00
|
|
|
st->trig->ops = &inv_mpu_trigger_ops;
|
2013-03-25 16:58:00 +08:00
|
|
|
iio_trigger_set_drvdata(st->trig, indio_dev);
|
2015-01-22 11:38:04 +08:00
|
|
|
|
2018-04-23 18:33:31 +08:00
|
|
|
ret = devm_iio_trigger_register(&indio_dev->dev, st->trig);
|
2013-02-02 08:26:00 +08:00
|
|
|
if (ret)
|
2015-01-22 11:38:04 +08:00
|
|
|
return ret;
|
|
|
|
|
2014-08-23 04:48:00 +08:00
|
|
|
indio_dev->trig = iio_trigger_get(st->trig);
|
2013-02-02 08:26:00 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|