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

iio: adc: ad799x: do not use internal iio_dev lock

'mlock' was being grabbed when setting the device frequency. In order to
not introduce any functional change a new lock is added. With that in
mind, the lock also needs to be grabbed in the places where 'mlock' is
since it was also being used to protect st->config against the current
device state.

On the other places the lock was being used, we can just drop
it since we are only doing one i2c bus read/write which is already
safe.

While at it, properly include "mutex.h" for mutex related APIs.

Signed-off-by: Nuno Sá <nuno.sa@analog.com>
Link: https://lore.kernel.org/r/20221004134909.1692021-2-nuno.sa@analog.com
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>

authored by

Nuno Sá and committed by
Jonathan Cameron
8f347c56 3a258747

+14 -6
+14 -6
drivers/iio/adc/ad799x.c
··· 28 28 #include <linux/types.h> 29 29 #include <linux/err.h> 30 30 #include <linux/module.h> 31 + #include <linux/mutex.h> 31 32 #include <linux/bitops.h> 32 33 33 34 #include <linux/iio/iio.h> ··· 126 125 const struct ad799x_chip_config *chip_config; 127 126 struct regulator *reg; 128 127 struct regulator *vref; 128 + /* lock to protect against multiple access to the device */ 129 + struct mutex lock; 129 130 unsigned id; 130 131 u16 config; 131 132 ··· 293 290 ret = iio_device_claim_direct_mode(indio_dev); 294 291 if (ret) 295 292 return ret; 293 + mutex_lock(&st->lock); 296 294 ret = ad799x_scan_direct(st, chan->scan_index); 295 + mutex_unlock(&st->lock); 297 296 iio_device_release_direct_mode(indio_dev); 298 297 299 298 if (ret < 0) ··· 356 351 if (ret) 357 352 return ret; 358 353 359 - mutex_lock(&indio_dev->mlock); 354 + mutex_lock(&st->lock); 355 + 360 356 ret = i2c_smbus_read_byte_data(st->client, AD7998_CYCLE_TMR_REG); 361 357 if (ret < 0) 362 358 goto error_ret_mutex; ··· 379 373 ret = len; 380 374 381 375 error_ret_mutex: 382 - mutex_unlock(&indio_dev->mlock); 376 + mutex_unlock(&st->lock); 383 377 384 378 return ret; 385 379 } ··· 413 407 if (ret) 414 408 return ret; 415 409 410 + mutex_lock(&st->lock); 411 + 416 412 if (state) 417 413 st->config |= BIT(chan->scan_index) << AD799X_CHANNEL_SHIFT; 418 414 else ··· 426 418 st->config &= ~AD7998_ALERT_EN; 427 419 428 420 ret = ad799x_write_config(st, st->config); 421 + mutex_unlock(&st->lock); 429 422 iio_device_release_direct_mode(indio_dev); 430 423 return ret; 431 424 } ··· 463 454 if (val < 0 || val > GENMASK(chan->scan_type.realbits - 1, 0)) 464 455 return -EINVAL; 465 456 466 - mutex_lock(&indio_dev->mlock); 467 457 ret = i2c_smbus_write_word_swapped(st->client, 468 458 ad799x_threshold_reg(chan, dir, info), 469 459 val << chan->scan_type.shift); 470 - mutex_unlock(&indio_dev->mlock); 471 460 472 461 return ret; 473 462 } ··· 480 473 int ret; 481 474 struct ad799x_state *st = iio_priv(indio_dev); 482 475 483 - mutex_lock(&indio_dev->mlock); 484 476 ret = i2c_smbus_read_word_swapped(st->client, 485 477 ad799x_threshold_reg(chan, dir, info)); 486 - mutex_unlock(&indio_dev->mlock); 487 478 if (ret < 0) 488 479 return ret; 489 480 *val = (ret >> chan->scan_type.shift) & ··· 868 863 if (ret) 869 864 goto error_cleanup_ring; 870 865 } 866 + 867 + mutex_init(&st->lock); 868 + 871 869 ret = iio_device_register(indio_dev); 872 870 if (ret) 873 871 goto error_cleanup_ring;