RE: [RFC PATCH 9/9] iio: imu: inv_mpu6050: Fix deadlock between i2c adapter lock and mpu lock
From: Ge Gao
Date: Fri Feb 19 2016 - 15:32:29 EST
"there was one comment before that power off will cause the the bypass i2c lines to disable for some MPU chips which this driver can support."
This is right. I remember there was some code about it earlier trying to take advantage of the secondary bus without enabling the MPU chip itself. This is impossible. The secondary bus interface is part of the chip and it will not function without enabling the chip. Without enabling gyro/accel engine, enabling the chip alone takes very little power. It is not worth the trouble to do that.
Thanks,
Ge
-----Original Message-----
From: Srinivas Pandruvada [mailto:srinivas.pandruvada@xxxxxxxxxxxxxxx]
Sent: Thursday, February 18, 2016 10:17 AM
To: Daniel Baluta; jic23@xxxxxxxxxx
Cc: knaack.h@xxxxxx; lars@xxxxxxxxxx; pmeerw@xxxxxxxxxx; linux-iio@xxxxxxxxxxxxxxx; linux-kernel@xxxxxxxxxxxxxxx; wsa@xxxxxxxxxxxxx; linux-i2c@xxxxxxxxxxxxxxx; lucas.demarchi@xxxxxxxxx; Ge Gao; adi.reus@xxxxxxxxx; cmo@xxxxxxxxxxx; mwelling@xxxxxxxx
Subject: Re: [RFC PATCH 9/9] iio: imu: inv_mpu6050: Fix deadlock between i2c adapter lock and mpu lock
On Thu, 2016-02-18 at 17:53 +0200, Daniel Baluta wrote:
> From: Adriana Reus <adriana.reus@xxxxxxxxx>
>
> This deadlock occurs if the accel/gyro and the sensor on the auxiliary
> I2C (in my setup it's an ak8975) are working at the same time.
>
> Scenario:
>
> ÂÂÂÂÂÂT1 T2
> ÂÂÂÂÂ==== ÂÂÂÂÂÂÂ====
> inv_mpu6050_read_fifoÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂaux sensor op (eg.
> ak8975_read_raw)
> ÂÂÂÂÂÂÂÂ|ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ|
> mutex_lock(&indio_dev->mlock)ÂÂÂÂÂÂÂÂÂÂÂi2c_transfer
> ÂÂÂÂÂÂÂÂ|ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ| i2c transactionÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ
> i2c adapter lock
> ÂÂÂÂÂÂÂÂ|ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ| i2c adapter lockÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ
> i2c_mux_master_xfer
> ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ|
> ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂinv_mpu6050_select_bypass
> ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ|
> ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂmutex_lock(&indio_dev->mlock)
>
> When we operate on an mpu sensor the order of locking is mpu lock
> followed by the i2c adapter lock. However, when we operate the
> auxiliary sensor the order of locking is the other way around.
>
> In order to avoid this enable the bypass mux bit once in the beginning
> and remove the select/deselect_bypass functions.
>
> This patch moves the bypass enabling in a separate function that is
> called once at probe and removes the functionality from
> inv_mpu_select/deselect_bypass.
>
> Another advantage of this approach is that power-wise the mpu chip
> isn't powered up at each auxiliary sensor i2c transaction so if only
> the compass is used this would be more power efficient.
There was one comment before that power off will cause the the bypass i2c lines to disable for some MPU chips which this driver can support.
So get some confirmation from Invensense folks that this is OK for all MPU chips.
Thanks,
Srinivas
>
> Signed-off-by: Adriana Reus <adriana.reus@xxxxxxxxx>
> Signed-off-by: Daniel Baluta <daniel.baluta@xxxxxxxxx>
> ---
> Âdrivers/iio/imu/inv_mpu6050/inv_mpu_i2c.c | 88 ++++++---------------
> ----------
> Â1 file changed, 15 insertions(+), 73 deletions(-)
>
> diff --git a/drivers/iio/imu/inv_mpu6050/inv_mpu_i2c.c
> b/drivers/iio/imu/inv_mpu6050/inv_mpu_i2c.c
> index 71bdaa3..8065355 100644
> --- a/drivers/iio/imu/inv_mpu6050/inv_mpu_i2c.c
> +++ b/drivers/iio/imu/inv_mpu6050/inv_mpu_i2c.c
> @@ -25,82 +25,25 @@ static const struct regmap_config
> inv_mpu_regmap_config = {
> Â .val_bits = 8,
> Â};
> Â
> -/*
> - * The i2c read/write needs to happen in unlocked mode. As the parent
> - * adapter is common. If we use locked versions, it will fail as
> - * the mux adapter will lock the parent i2c adapter, while calling
> - * select/deselect functions.
> - */
> -static int inv_mpu6050_write_reg_unlocked(struct i2c_client *client,
> - ÂÂu8 reg, u8 d)
> +static int inv_mpu6050_bypass_en(struct iio_dev *indio_dev)
> Â{
> - int ret;
> - u8 buf[2] = {reg, d};
> - struct i2c_msg msg[1] = {
> - {
> - .addr = client->addr,
> - .flags = 0,
> - .len = sizeof(buf),
> - .buf = buf,
> - }
> - };
> -
> - ret = __i2c_transfer(client->adapter, msg, 1);
> - if (ret != 1)
> - return ret;
> -
> - return 0;
> -}
> -
> -static int inv_mpu6050_select_bypass(struct i2c_adapter *adap, void
> *mux_priv,
> - ÂÂÂÂÂu32 chan_id)
> -{
> - struct i2c_client *client = mux_priv;
> - struct iio_dev *indio_dev = dev_get_drvdata(&client->dev);
> Â struct inv_mpu6050_state *st = iio_priv(indio_dev);
> Â int ret = 0;
> Â
> - /* Use the same mutex which was used everywhere to protect
> power-op */
> - mutex_lock(&indio_dev->mlock);
> - if (!st->powerup_count) {
> - ret = inv_mpu6050_write_reg_unlocked(client,
> - ÂÂÂÂÂst->reg-
> >pwr_mgmt_1, 0);
> - if (ret)
> - goto write_error;
> + ret = inv_mpu6050_set_power_itg(st, true);
> + if (ret)
> + return ret;
> Â
> - msleep(INV_MPU6050_REG_UP_TIME);
> - }
> - if (!ret) {
> - st->powerup_count++;
> - ret = inv_mpu6050_write_reg_unlocked(client,
> - ÂÂÂÂÂst->reg-
> >int_pin_cfg,
> - ÂÂÂÂÂINV_MPU6050_INT
> _PIN_CFG |
> - ÂÂÂÂÂINV_MPU6050_BIT
> _BYPASS_EN);
> + ret = regmap_write(st->map,
> + ÂÂÂst->reg->int_pin_cfg,
> + ÂÂÂINV_MPU6050_INT_PIN_CFG |
> + ÂÂÂINV_MPU6050_BIT_BYPASS_EN);
> + if (ret) {
> + inv_mpu6050_set_power_itg(st, false);
> + return ret;
> Â }
> -write_error:
> - mutex_unlock(&indio_dev->mlock);
> Â
> - return ret;
> -}
> -
> -static int inv_mpu6050_deselect_bypass(struct i2c_adapter *adap,
> - ÂÂÂÂÂÂÂvoid *mux_priv, u32 chan_id)
> -{
> - struct i2c_client *client = mux_priv;
> - struct iio_dev *indio_dev = dev_get_drvdata(&client->dev);
> - struct inv_mpu6050_state *st = iio_priv(indio_dev);
> -
> - mutex_lock(&indio_dev->mlock);
> - /* It doesn't really mattter, if any of the calls fails */
> - inv_mpu6050_write_reg_unlocked(client, st->reg->int_pin_cfg,
> - ÂÂÂÂÂÂÂINV_MPU6050_INT_PIN_CFG);
> - st->powerup_count--;
> - if (!st->powerup_count)
> - inv_mpu6050_write_reg_unlocked(client, st->reg-
> >pwr_mgmt_1,
> - ÂÂÂÂÂÂÂINV_MPU6050_BIT_SLEEP
> );
> - mutex_unlock(&indio_dev->mlock);
> -
> - return 0;
> + return inv_mpu6050_set_power_itg(st, false);
> Â}
> Â
> Â/**
> @@ -129,7 +72,8 @@ static int inv_mpu_probe(struct i2c_client *client,
> Â return PTR_ERR(regmap);
> Â }
> Â
> - result = inv_mpu_core_probe(regmap, client->irq, name,
> NULL);
> + result = inv_mpu_core_probe(regmap, client->irq, name,
> + ÂÂÂÂinv_mpu6050_bypass_en);
> Â if (result < 0)
> Â return result;
> Â
> @@ -137,9 +81,7 @@ static int inv_mpu_probe(struct i2c_client *client,
> Â st->mux_adapter = i2c_add_mux_adapter(client->adapter,
> Â ÂÂÂÂÂÂ&client->dev,
> Â ÂÂÂÂÂÂclient,
> - ÂÂÂÂÂÂ0, 0, 0,
> - ÂÂÂÂÂÂinv_mpu6050_select_byp
> ass,
> - ÂÂÂÂÂÂinv_mpu6050_deselect_b
> ypass);
> + ÂÂÂÂÂÂ0, 0, 0, NULL, NULL);
> Â if (!st->mux_adapter) {
> Â result = -ENODEV;
> Â goto out_unreg_device;