Skip to content

Commit 7fc0168

Browse files
jmaneyrol-invnjic23
authored andcommitted
iio: imu: inv_mpu6050: clean set_power_itg and fix usage
Rewrite set_power_itg. Failing when turning power off is no more decreasing the counter now and sleeping only happens when effectively turning the chip on. Fix also usage in init function (setting power on one time is sufficient to ensure chip is effectively on). Signed-off-by: Jean-Baptiste Maneyrol <jmaneyrol@invensense.com> Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
1 parent 918f958 commit 7fc0168

File tree

1 file changed

+24
-20
lines changed

1 file changed

+24
-20
lines changed

drivers/iio/imu/inv_mpu6050/inv_mpu_core.c

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -191,26 +191,29 @@ int inv_mpu6050_switch_engine(struct inv_mpu6050_state *st, bool en, u32 mask)
191191

192192
int inv_mpu6050_set_power_itg(struct inv_mpu6050_state *st, bool power_on)
193193
{
194-
int result = 0;
194+
int result;
195195

196196
if (power_on) {
197-
if (!st->powerup_count)
197+
if (!st->powerup_count) {
198198
result = regmap_write(st->map, st->reg->pwr_mgmt_1, 0);
199-
if (!result)
200-
st->powerup_count++;
199+
if (result)
200+
return result;
201+
usleep_range(INV_MPU6050_REG_UP_TIME_MIN,
202+
INV_MPU6050_REG_UP_TIME_MAX);
203+
}
204+
st->powerup_count++;
201205
} else {
202-
st->powerup_count--;
203-
if (!st->powerup_count)
206+
if (st->powerup_count == 1) {
204207
result = regmap_write(st->map, st->reg->pwr_mgmt_1,
205208
INV_MPU6050_BIT_SLEEP);
209+
if (result)
210+
return result;
211+
}
212+
st->powerup_count--;
206213
}
207214

208-
if (result)
209-
return result;
210-
211-
if (power_on)
212-
usleep_range(INV_MPU6050_REG_UP_TIME_MIN,
213-
INV_MPU6050_REG_UP_TIME_MAX);
215+
dev_dbg(regmap_get_device(st->map), "set power %d, count=%u\n",
216+
power_on, st->powerup_count);
214217

215218
return 0;
216219
}
@@ -856,28 +859,29 @@ static int inv_check_and_setup_chip(struct inv_mpu6050_state *st)
856859
msleep(INV_MPU6050_POWER_UP_TIME);
857860

858861
/*
859-
* toggle power state. After reset, the sleep bit could be on
860-
* or off depending on the OTP settings. Toggling power would
862+
* Turn power on. After reset, the sleep bit could be on
863+
* or off depending on the OTP settings. Turning power on
861864
* make it in a definite state as well as making the hardware
862865
* state align with the software state
863866
*/
864-
result = inv_mpu6050_set_power_itg(st, false);
865-
if (result)
866-
return result;
867867
result = inv_mpu6050_set_power_itg(st, true);
868868
if (result)
869869
return result;
870870

871871
result = inv_mpu6050_switch_engine(st, false,
872872
INV_MPU6050_BIT_PWR_ACCL_STBY);
873873
if (result)
874-
return result;
874+
goto error_power_off;
875875
result = inv_mpu6050_switch_engine(st, false,
876876
INV_MPU6050_BIT_PWR_GYRO_STBY);
877877
if (result)
878-
return result;
878+
goto error_power_off;
879879

880-
return 0;
880+
return inv_mpu6050_set_power_itg(st, false);
881+
882+
error_power_off:
883+
inv_mpu6050_set_power_itg(st, false);
884+
return result;
881885
}
882886

883887
int inv_mpu_core_probe(struct regmap *regmap, int irq, const char *name,

0 commit comments

Comments
 (0)