diff options
| author | Rui Miguel Silva <rmfrfs@gmail.com> | 2017-05-12 16:16:15 -0400 |
|---|---|---|
| committer | Greg Kroah-Hartman <gregkh@linuxfoundation.org> | 2017-05-16 08:23:31 -0400 |
| commit | 50b7c322cfc8b44a36b1373dab2177db23e3282c (patch) | |
| tree | cca951e511cf8d6c1f229c0a938adb390a738c19 | |
| parent | f0d39a179b9cd38c739acfc4f92720a0c79e1d66 (diff) | |
staging: typec: fusb302: refactor resume retry mechanism
The i2c functions need to test the pm_suspend state and do, if needed, some
retry before i2c operations. This code was repeated 4x.
To isolate this, create a new function to check suspend state and call it in
every need place.
As at it, move the error message from pr_err to dev_err.
Signed-off-by: Rui Miguel Silva <rmfrfs@gmail.com>
Acked-by: Yueyao Zhu <yueyao.zhu@gmail.com>
Reviewed-by: Guenter Roeck <linux@roeck-us.net>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
| -rw-r--r-- | drivers/staging/typec/fusb302/fusb302.c | 70 |
1 files changed, 36 insertions, 34 deletions
diff --git a/drivers/staging/typec/fusb302/fusb302.c b/drivers/staging/typec/fusb302/fusb302.c index 7b0b9e51016d..4a356e509fe4 100644 --- a/drivers/staging/typec/fusb302/fusb302.c +++ b/drivers/staging/typec/fusb302/fusb302.c | |||
| @@ -264,22 +264,36 @@ static void fusb302_debugfs_exit(const struct fusb302_chip *chip) { } | |||
| 264 | 264 | ||
| 265 | #define FUSB302_RESUME_RETRY 10 | 265 | #define FUSB302_RESUME_RETRY 10 |
| 266 | #define FUSB302_RESUME_RETRY_SLEEP 50 | 266 | #define FUSB302_RESUME_RETRY_SLEEP 50 |
| 267 | static int fusb302_i2c_write(struct fusb302_chip *chip, | 267 | |
| 268 | u8 address, u8 data) | 268 | static bool fusb302_is_suspended(struct fusb302_chip *chip) |
| 269 | { | 269 | { |
| 270 | int retry_cnt; | 270 | int retry_cnt; |
| 271 | int ret = 0; | ||
| 272 | 271 | ||
| 273 | atomic_set(&chip->i2c_busy, 1); | ||
| 274 | for (retry_cnt = 0; retry_cnt < FUSB302_RESUME_RETRY; retry_cnt++) { | 272 | for (retry_cnt = 0; retry_cnt < FUSB302_RESUME_RETRY; retry_cnt++) { |
| 275 | if (atomic_read(&chip->pm_suspend)) { | 273 | if (atomic_read(&chip->pm_suspend)) { |
| 276 | pr_err("fusb302_i2c: pm suspend, retry %d/%d\n", | 274 | dev_err(chip->dev, "i2c: pm suspend, retry %d/%d\n", |
| 277 | retry_cnt + 1, FUSB302_RESUME_RETRY); | 275 | retry_cnt + 1, FUSB302_RESUME_RETRY); |
| 278 | msleep(FUSB302_RESUME_RETRY_SLEEP); | 276 | msleep(FUSB302_RESUME_RETRY_SLEEP); |
| 279 | } else { | 277 | } else { |
| 280 | break; | 278 | return false; |
| 281 | } | 279 | } |
| 282 | } | 280 | } |
| 281 | |||
| 282 | return true; | ||
| 283 | } | ||
| 284 | |||
| 285 | static int fusb302_i2c_write(struct fusb302_chip *chip, | ||
| 286 | u8 address, u8 data) | ||
| 287 | { | ||
| 288 | int ret = 0; | ||
| 289 | |||
| 290 | atomic_set(&chip->i2c_busy, 1); | ||
| 291 | |||
| 292 | if (fusb302_is_suspended(chip)) { | ||
| 293 | atomic_set(&chip->i2c_busy, 0); | ||
| 294 | return -ETIMEDOUT; | ||
| 295 | } | ||
| 296 | |||
| 283 | ret = i2c_smbus_write_byte_data(chip->i2c_client, address, data); | 297 | ret = i2c_smbus_write_byte_data(chip->i2c_client, address, data); |
| 284 | if (ret < 0) | 298 | if (ret < 0) |
| 285 | fusb302_log(chip, "cannot write 0x%02x to 0x%02x, ret=%d", | 299 | fusb302_log(chip, "cannot write 0x%02x to 0x%02x, ret=%d", |
| @@ -292,21 +306,17 @@ static int fusb302_i2c_write(struct fusb302_chip *chip, | |||
| 292 | static int fusb302_i2c_block_write(struct fusb302_chip *chip, u8 address, | 306 | static int fusb302_i2c_block_write(struct fusb302_chip *chip, u8 address, |
| 293 | u8 length, const u8 *data) | 307 | u8 length, const u8 *data) |
| 294 | { | 308 | { |
| 295 | int retry_cnt; | ||
| 296 | int ret = 0; | 309 | int ret = 0; |
| 297 | 310 | ||
| 298 | if (length <= 0) | 311 | if (length <= 0) |
| 299 | return ret; | 312 | return ret; |
| 300 | atomic_set(&chip->i2c_busy, 1); | 313 | atomic_set(&chip->i2c_busy, 1); |
| 301 | for (retry_cnt = 0; retry_cnt < FUSB302_RESUME_RETRY; retry_cnt++) { | 314 | |
| 302 | if (atomic_read(&chip->pm_suspend)) { | 315 | if (fusb302_is_suspended(chip)) { |
| 303 | pr_err("fusb302_i2c: pm suspend, retry %d/%d\n", | 316 | atomic_set(&chip->i2c_busy, 0); |
| 304 | retry_cnt + 1, FUSB302_RESUME_RETRY); | 317 | return -ETIMEDOUT; |
| 305 | msleep(FUSB302_RESUME_RETRY_SLEEP); | ||
| 306 | } else { | ||
| 307 | break; | ||
| 308 | } | ||
| 309 | } | 318 | } |
| 319 | |||
| 310 | ret = i2c_smbus_write_i2c_block_data(chip->i2c_client, address, | 320 | ret = i2c_smbus_write_i2c_block_data(chip->i2c_client, address, |
| 311 | length, data); | 321 | length, data); |
| 312 | if (ret < 0) | 322 | if (ret < 0) |
| @@ -320,19 +330,15 @@ static int fusb302_i2c_block_write(struct fusb302_chip *chip, u8 address, | |||
| 320 | static int fusb302_i2c_read(struct fusb302_chip *chip, | 330 | static int fusb302_i2c_read(struct fusb302_chip *chip, |
| 321 | u8 address, u8 *data) | 331 | u8 address, u8 *data) |
| 322 | { | 332 | { |
| 323 | int retry_cnt; | ||
| 324 | int ret = 0; | 333 | int ret = 0; |
| 325 | 334 | ||
| 326 | atomic_set(&chip->i2c_busy, 1); | 335 | atomic_set(&chip->i2c_busy, 1); |
| 327 | for (retry_cnt = 0; retry_cnt < FUSB302_RESUME_RETRY; retry_cnt++) { | 336 | |
| 328 | if (atomic_read(&chip->pm_suspend)) { | 337 | if (fusb302_is_suspended(chip)) { |
| 329 | pr_err("fusb302_i2c: pm suspend, retry %d/%d\n", | 338 | atomic_set(&chip->i2c_busy, 0); |
| 330 | retry_cnt + 1, FUSB302_RESUME_RETRY); | 339 | return -ETIMEDOUT; |
| 331 | msleep(FUSB302_RESUME_RETRY_SLEEP); | ||
| 332 | } else { | ||
| 333 | break; | ||
| 334 | } | ||
| 335 | } | 340 | } |
| 341 | |||
| 336 | ret = i2c_smbus_read_byte_data(chip->i2c_client, address); | 342 | ret = i2c_smbus_read_byte_data(chip->i2c_client, address); |
| 337 | *data = (u8)ret; | 343 | *data = (u8)ret; |
| 338 | if (ret < 0) | 344 | if (ret < 0) |
| @@ -345,21 +351,17 @@ static int fusb302_i2c_read(struct fusb302_chip *chip, | |||
| 345 | static int fusb302_i2c_block_read(struct fusb302_chip *chip, u8 address, | 351 | static int fusb302_i2c_block_read(struct fusb302_chip *chip, u8 address, |
| 346 | u8 length, u8 *data) | 352 | u8 length, u8 *data) |
| 347 | { | 353 | { |
| 348 | int retry_cnt; | ||
| 349 | int ret = 0; | 354 | int ret = 0; |
| 350 | 355 | ||
| 351 | if (length <= 0) | 356 | if (length <= 0) |
| 352 | return ret; | 357 | return ret; |
| 353 | atomic_set(&chip->i2c_busy, 1); | 358 | atomic_set(&chip->i2c_busy, 1); |
| 354 | for (retry_cnt = 0; retry_cnt < FUSB302_RESUME_RETRY; retry_cnt++) { | 359 | |
| 355 | if (atomic_read(&chip->pm_suspend)) { | 360 | if (fusb302_is_suspended(chip)) { |
| 356 | pr_err("fusb302_i2c: pm suspend, retry %d/%d\n", | 361 | atomic_set(&chip->i2c_busy, 0); |
| 357 | retry_cnt + 1, FUSB302_RESUME_RETRY); | 362 | return -ETIMEDOUT; |
| 358 | msleep(FUSB302_RESUME_RETRY_SLEEP); | ||
| 359 | } else { | ||
| 360 | break; | ||
| 361 | } | ||
| 362 | } | 363 | } |
| 364 | |||
| 363 | ret = i2c_smbus_read_i2c_block_data(chip->i2c_client, address, | 365 | ret = i2c_smbus_read_i2c_block_data(chip->i2c_client, address, |
| 364 | length, data); | 366 | length, data); |
| 365 | if (ret < 0) { | 367 | if (ret < 0) { |
