Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

iio: imu: inv_mpu6050: refactor aux read/write to use shared xfer logic

Refactors inv_mpu_aux_read() and inv_mpu_aux_write() to extract the common
I2C transfer sequence into inv_mpu_i2c_master_xfer(), which now handles
starting and stopping the I2C master, waiting for completion, disabling
SLV0, and checking for NACK errors.

This refactoring removes code duplication and improves maintainability.
No functional changes are intended.

Signed-off-by: Isabella Caselli <bellacaselli20@gmail.com>
Co-developed-by: Rodrigo Michelassi <rodrigo.michelassi@usp.br>
Signed-off-by: Rodrigo Michelassi <rodrigo.michelassi@usp.br>
Acked-by: Jean-Baptiste Maneyrol <jean-baptiste.maneyrol@tdk.com>
Link: https://patch.msgid.link/20250507184539.54658-1-bellacaselli20@gmail.com
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>

authored by

Isabella Caselli and committed by
Jonathan Cameron
fe03825b 1a862799

+20 -36
+20 -36
drivers/iio/imu/inv_mpu6050/inv_mpu_aux.c
··· 14 14 /* 15 15 * i2c master auxiliary bus transfer function. 16 16 * Requires the i2c operations to be correctly setup before. 17 + * Disables SLV0 and checks for NACK status internally. 18 + * Assumes that only SLV0 is used for transfers. 17 19 */ 18 20 static int inv_mpu_i2c_master_xfer(const struct inv_mpu6050_state *st) 19 21 { ··· 25 23 uint8_t d; 26 24 unsigned int user_ctrl; 27 25 int ret; 26 + unsigned int status; 28 27 29 28 /* set sample rate */ 30 29 d = INV_MPU6050_FIFO_RATE_TO_DIVIDER(freq); ··· 54 51 if (ret) 55 52 goto error_restore_rate; 56 53 54 + /* disable i2c slave */ 55 + ret = regmap_write(st->map, INV_MPU6050_REG_I2C_SLV_CTRL(0), 0); 56 + if (ret) 57 + goto error_disable_i2c; 58 + 59 + /* check i2c status */ 60 + ret = regmap_read(st->map, INV_MPU6050_REG_I2C_MST_STATUS, &status); 61 + if (ret) 62 + return ret; 63 + 64 + if (status & INV_MPU6050_BIT_I2C_SLV0_NACK) 65 + return -EIO; 66 + 57 67 return 0; 58 68 59 69 error_stop_i2c: 60 70 regmap_write(st->map, st->reg->user_ctrl, st->chip_config.user_ctrl); 61 71 error_restore_rate: 62 72 regmap_write(st->map, st->reg->sample_rate_div, st->chip_config.divider); 73 + error_disable_i2c: 74 + regmap_write(st->map, INV_MPU6050_REG_I2C_SLV_CTRL(0), 0); 63 75 return ret; 64 76 } 65 77 ··· 135 117 int inv_mpu_aux_read(const struct inv_mpu6050_state *st, uint8_t addr, 136 118 uint8_t reg, uint8_t *val, size_t size) 137 119 { 138 - unsigned int status; 139 120 int ret; 140 121 141 122 if (size > 0x0F) ··· 153 136 if (ret) 154 137 return ret; 155 138 156 - /* do i2c xfer */ 139 + /* do i2c xfer, disable i2c slave and check status*/ 157 140 ret = inv_mpu_i2c_master_xfer(st); 158 141 if (ret) 159 - goto error_disable_i2c; 160 - 161 - /* disable i2c slave */ 162 - ret = regmap_write(st->map, INV_MPU6050_REG_I2C_SLV_CTRL(0), 0); 163 - if (ret) 164 - goto error_disable_i2c; 165 - 166 - /* check i2c status */ 167 - ret = regmap_read(st->map, INV_MPU6050_REG_I2C_MST_STATUS, &status); 168 - if (ret) 169 142 return ret; 170 - if (status & INV_MPU6050_BIT_I2C_SLV0_NACK) 171 - return -EIO; 172 143 173 144 /* read data in registers */ 174 145 return regmap_bulk_read(st->map, INV_MPU6050_REG_EXT_SENS_DATA, 175 146 val, size); 176 - 177 - error_disable_i2c: 178 - regmap_write(st->map, INV_MPU6050_REG_I2C_SLV_CTRL(0), 0); 179 - return ret; 180 147 } 181 148 182 149 /** ··· 175 174 int inv_mpu_aux_write(const struct inv_mpu6050_state *st, uint8_t addr, 176 175 uint8_t reg, uint8_t val) 177 176 { 178 - unsigned int status; 179 177 int ret; 180 178 181 179 /* setup i2c SLV0 control: i2c addr, register, value, enable + size */ ··· 192 192 if (ret) 193 193 return ret; 194 194 195 - /* do i2c xfer */ 195 + /* do i2c xfer, disable i2c slave and check status*/ 196 196 ret = inv_mpu_i2c_master_xfer(st); 197 197 if (ret) 198 - goto error_disable_i2c; 199 - 200 - /* disable i2c slave */ 201 - ret = regmap_write(st->map, INV_MPU6050_REG_I2C_SLV_CTRL(0), 0); 202 - if (ret) 203 - goto error_disable_i2c; 204 - 205 - /* check i2c status */ 206 - ret = regmap_read(st->map, INV_MPU6050_REG_I2C_MST_STATUS, &status); 207 - if (ret) 208 198 return ret; 209 - if (status & INV_MPU6050_BIT_I2C_SLV0_NACK) 210 - return -EIO; 211 199 212 200 return 0; 213 - 214 - error_disable_i2c: 215 - regmap_write(st->map, INV_MPU6050_REG_I2C_SLV_CTRL(0), 0); 216 - return ret; 217 201 }