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_icm42600: use guard() to release mutexes

Replace explicit mutex_lock() and mutex_unlock() with the guard() macro
for cleaner and safer mutex handling.

Signed-off-by: Sean Nyekjaer <sean@geanix.com>
Reviewed-by: David Lechner <dlechner@baylibre.com>
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>

authored by

Sean Nyekjaer and committed by
Jonathan Cameron
661facba ee8fc402

+56 -96
+8 -16
drivers/iio/imu/inv_icm42600/inv_icm42600_accel.c
··· 561 561 conf.fs = idx / 2; 562 562 563 563 pm_runtime_get_sync(dev); 564 - mutex_lock(&st->lock); 565 564 566 - ret = inv_icm42600_set_accel_conf(st, &conf, NULL); 565 + scoped_guard(mutex, &st->lock) 566 + ret = inv_icm42600_set_accel_conf(st, &conf, NULL); 567 567 568 - mutex_unlock(&st->lock); 569 568 pm_runtime_put_autosuspend(dev); 570 569 571 570 return ret; ··· 985 986 unsigned int val) 986 987 { 987 988 struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev); 988 - int ret; 989 989 990 - mutex_lock(&st->lock); 990 + guard(mutex)(&st->lock); 991 991 992 992 st->fifo.watermark.accel = val; 993 - ret = inv_icm42600_buffer_update_watermark(st); 994 - 995 - mutex_unlock(&st->lock); 996 - 997 - return ret; 993 + return inv_icm42600_buffer_update_watermark(st); 998 994 } 999 995 1000 996 static int inv_icm42600_accel_hwfifo_flush(struct iio_dev *indio_dev, ··· 1001 1007 if (count == 0) 1002 1008 return 0; 1003 1009 1004 - mutex_lock(&st->lock); 1010 + guard(mutex)(&st->lock); 1005 1011 1006 1012 ret = inv_icm42600_buffer_hwfifo_flush(st, count); 1007 - if (!ret) 1008 - ret = st->fifo.nb.accel; 1013 + if (ret) 1014 + return ret; 1009 1015 1010 - mutex_unlock(&st->lock); 1011 - 1012 - return ret; 1016 + return st->fifo.nb.accel; 1013 1017 } 1014 1018 1015 1019 static int inv_icm42600_accel_read_event_config(struct iio_dev *indio_dev,
+18 -27
drivers/iio/imu/inv_icm42600/inv_icm42600_buffer.c
··· 283 283 284 284 pm_runtime_get_sync(dev); 285 285 286 - mutex_lock(&st->lock); 286 + guard(mutex)(&st->lock); 287 287 inv_sensors_timestamp_reset(ts); 288 - mutex_unlock(&st->lock); 289 288 290 289 return 0; 291 290 } ··· 298 299 struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev); 299 300 int ret; 300 301 301 - mutex_lock(&st->lock); 302 + guard(mutex)(&st->lock); 302 303 303 - /* exit if FIFO is already on */ 304 304 if (st->fifo.on) { 305 - ret = 0; 306 - goto out_on; 305 + st->fifo.on++; 306 + return 0; 307 307 } 308 308 309 309 /* set FIFO threshold interrupt */ 310 310 ret = regmap_set_bits(st->map, INV_ICM42600_REG_INT_SOURCE0, 311 311 INV_ICM42600_INT_SOURCE0_FIFO_THS_INT1_EN); 312 312 if (ret) 313 - goto out_unlock; 313 + return ret; 314 314 315 315 /* flush FIFO data */ 316 316 ret = regmap_write(st->map, INV_ICM42600_REG_SIGNAL_PATH_RESET, 317 317 INV_ICM42600_SIGNAL_PATH_RESET_FIFO_FLUSH); 318 318 if (ret) 319 - goto out_unlock; 319 + return ret; 320 320 321 321 /* set FIFO in streaming mode */ 322 322 ret = regmap_write(st->map, INV_ICM42600_REG_FIFO_CONFIG, 323 323 INV_ICM42600_FIFO_CONFIG_STREAM); 324 324 if (ret) 325 - goto out_unlock; 325 + return ret; 326 326 327 327 /* workaround: first read of FIFO count after reset is always 0 */ 328 328 ret = regmap_bulk_read(st->map, INV_ICM42600_REG_FIFO_COUNT, st->buffer, 2); 329 329 if (ret) 330 - goto out_unlock; 330 + return ret; 331 331 332 - out_on: 333 - /* increase FIFO on counter */ 334 332 st->fifo.on++; 335 - out_unlock: 336 - mutex_unlock(&st->lock); 337 - return ret; 333 + 334 + return 0; 338 335 } 339 336 340 337 static int inv_icm42600_buffer_predisable(struct iio_dev *indio_dev) ··· 338 343 struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev); 339 344 int ret; 340 345 341 - mutex_lock(&st->lock); 346 + guard(mutex)(&st->lock); 342 347 343 - /* exit if there are several sensors using the FIFO */ 344 348 if (st->fifo.on > 1) { 345 - ret = 0; 346 - goto out_off; 349 + st->fifo.on--; 350 + return 0; 347 351 } 348 352 349 353 /* set FIFO in bypass mode */ 350 354 ret = regmap_write(st->map, INV_ICM42600_REG_FIFO_CONFIG, 351 355 INV_ICM42600_FIFO_CONFIG_BYPASS); 352 356 if (ret) 353 - goto out_unlock; 357 + return ret; 354 358 355 359 /* flush FIFO data */ 356 360 ret = regmap_write(st->map, INV_ICM42600_REG_SIGNAL_PATH_RESET, 357 361 INV_ICM42600_SIGNAL_PATH_RESET_FIFO_FLUSH); 358 362 if (ret) 359 - goto out_unlock; 363 + return ret; 360 364 361 365 /* disable FIFO threshold interrupt */ 362 366 ret = regmap_clear_bits(st->map, INV_ICM42600_REG_INT_SOURCE0, 363 367 INV_ICM42600_INT_SOURCE0_FIFO_THS_INT1_EN); 364 368 if (ret) 365 - goto out_unlock; 369 + return ret; 366 370 367 - out_off: 368 - /* decrease FIFO on counter */ 369 371 st->fifo.on--; 370 - out_unlock: 371 - mutex_unlock(&st->lock); 372 - return ret; 372 + 373 + return 0; 373 374 } 374 375 375 376 static int inv_icm42600_buffer_postdisable(struct iio_dev *indio_dev)
+22 -37
drivers/iio/imu/inv_icm42600/inv_icm42600_core.c
··· 439 439 unsigned int writeval, unsigned int *readval) 440 440 { 441 441 struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev); 442 - int ret; 443 442 444 - mutex_lock(&st->lock); 443 + guard(mutex)(&st->lock); 445 444 446 445 if (readval) 447 - ret = regmap_read(st->map, reg, readval); 448 - else 449 - ret = regmap_write(st->map, reg, writeval); 446 + return regmap_read(st->map, reg, readval); 450 447 451 - mutex_unlock(&st->lock); 452 - 453 - return ret; 448 + return regmap_write(st->map, reg, writeval); 454 449 } 455 450 456 451 static int inv_icm42600_set_conf(struct inv_icm42600_state *st, ··· 815 820 struct device *accel_dev; 816 821 bool wakeup; 817 822 int accel_conf; 818 - int ret = 0; 823 + int ret; 819 824 820 - mutex_lock(&st->lock); 825 + guard(mutex)(&st->lock); 821 826 822 827 st->suspended.gyro = st->conf.gyro.mode; 823 828 st->suspended.accel = st->conf.accel.mode; 824 829 st->suspended.temp = st->conf.temp_en; 825 830 if (pm_runtime_suspended(dev)) 826 - goto out_unlock; 831 + return 0; 827 832 828 833 /* disable FIFO data streaming */ 829 834 if (st->fifo.on) { 830 835 ret = regmap_write(st->map, INV_ICM42600_REG_FIFO_CONFIG, 831 836 INV_ICM42600_FIFO_CONFIG_BYPASS); 832 837 if (ret) 833 - goto out_unlock; 838 + return ret; 834 839 } 835 840 836 841 /* keep chip on and wake-up capable if APEX and wakeup on */ ··· 846 851 if (st->apex.wom.enable) { 847 852 ret = inv_icm42600_disable_wom(st); 848 853 if (ret) 849 - goto out_unlock; 854 + return ret; 850 855 } 851 856 accel_conf = INV_ICM42600_SENSOR_MODE_OFF; 852 857 } ··· 854 859 ret = inv_icm42600_set_pwr_mgmt0(st, INV_ICM42600_SENSOR_MODE_OFF, 855 860 accel_conf, false, NULL); 856 861 if (ret) 857 - goto out_unlock; 862 + return ret; 858 863 859 864 /* disable vddio regulator if chip is sleeping */ 860 865 if (!wakeup) 861 866 regulator_disable(st->vddio_supply); 862 867 863 - out_unlock: 864 - mutex_unlock(&st->lock); 865 - return ret; 868 + return 0; 866 869 } 867 870 868 871 /* ··· 874 881 struct inv_icm42600_sensor_state *accel_st = iio_priv(st->indio_accel); 875 882 struct device *accel_dev; 876 883 bool wakeup; 877 - int ret = 0; 884 + int ret; 878 885 879 - mutex_lock(&st->lock); 886 + guard(mutex)(&st->lock); 880 887 881 888 if (pm_runtime_suspended(dev)) 882 - goto out_unlock; 889 + return 0; 883 890 884 891 /* check wakeup capability */ 885 892 accel_dev = &st->indio_accel->dev; ··· 891 898 } else { 892 899 ret = inv_icm42600_enable_regulator_vddio(st); 893 900 if (ret) 894 - goto out_unlock; 901 + return ret; 895 902 } 896 903 897 904 /* restore sensors state */ ··· 899 906 st->suspended.accel, 900 907 st->suspended.temp, NULL); 901 908 if (ret) 902 - goto out_unlock; 909 + return ret; 903 910 904 911 /* restore APEX features if disabled */ 905 912 if (!wakeup && st->apex.wom.enable) { 906 913 ret = inv_icm42600_enable_wom(st); 907 914 if (ret) 908 - goto out_unlock; 915 + return ret; 909 916 } 910 917 911 918 /* restore FIFO data streaming */ ··· 916 923 INV_ICM42600_FIFO_CONFIG_STREAM); 917 924 } 918 925 919 - out_unlock: 920 - mutex_unlock(&st->lock); 921 - return ret; 926 + return 0; 922 927 } 923 928 924 929 /* Runtime suspend will turn off sensors that are enabled by iio devices. */ ··· 925 934 struct inv_icm42600_state *st = dev_get_drvdata(dev); 926 935 int ret; 927 936 928 - mutex_lock(&st->lock); 937 + guard(mutex)(&st->lock); 929 938 930 939 /* disable all sensors */ 931 940 ret = inv_icm42600_set_pwr_mgmt0(st, INV_ICM42600_SENSOR_MODE_OFF, 932 941 INV_ICM42600_SENSOR_MODE_OFF, false, 933 942 NULL); 934 943 if (ret) 935 - goto error_unlock; 944 + return ret; 936 945 937 946 regulator_disable(st->vddio_supply); 938 947 939 - error_unlock: 940 - mutex_unlock(&st->lock); 941 - return ret; 948 + return 0; 942 949 } 943 950 944 951 /* Sensors are enabled by iio devices, no need to turn them back on here. */ 945 952 static int inv_icm42600_runtime_resume(struct device *dev) 946 953 { 947 954 struct inv_icm42600_state *st = dev_get_drvdata(dev); 948 - int ret; 949 955 950 - mutex_lock(&st->lock); 956 + guard(mutex)(&st->lock); 951 957 952 - ret = inv_icm42600_enable_regulator_vddio(st); 953 - 954 - mutex_unlock(&st->lock); 955 - return ret; 958 + return inv_icm42600_enable_regulator_vddio(st); 956 959 } 957 960 958 961 EXPORT_NS_GPL_DEV_PM_OPS(inv_icm42600_pm_ops, IIO_ICM42600) = {
+8 -16
drivers/iio/imu/inv_icm42600/inv_icm42600_gyro.c
··· 277 277 conf.fs = idx / 2; 278 278 279 279 pm_runtime_get_sync(dev); 280 - mutex_lock(&st->lock); 281 280 282 - ret = inv_icm42600_set_gyro_conf(st, &conf, NULL); 281 + scoped_guard(mutex, &st->lock) 282 + ret = inv_icm42600_set_gyro_conf(st, &conf, NULL); 283 283 284 - mutex_unlock(&st->lock); 285 284 pm_runtime_put_autosuspend(dev); 286 285 287 286 return ret; ··· 687 688 unsigned int val) 688 689 { 689 690 struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev); 690 - int ret; 691 691 692 - mutex_lock(&st->lock); 692 + guard(mutex)(&st->lock); 693 693 694 694 st->fifo.watermark.gyro = val; 695 - ret = inv_icm42600_buffer_update_watermark(st); 696 - 697 - mutex_unlock(&st->lock); 698 - 699 - return ret; 695 + return inv_icm42600_buffer_update_watermark(st); 700 696 } 701 697 702 698 static int inv_icm42600_gyro_hwfifo_flush(struct iio_dev *indio_dev, ··· 703 709 if (count == 0) 704 710 return 0; 705 711 706 - mutex_lock(&st->lock); 712 + guard(mutex)(&st->lock); 707 713 708 714 ret = inv_icm42600_buffer_hwfifo_flush(st, count); 709 - if (!ret) 710 - ret = st->fifo.nb.gyro; 715 + if (ret) 716 + return ret; 711 717 712 - mutex_unlock(&st->lock); 713 - 714 - return ret; 718 + return st->fifo.nb.gyro; 715 719 } 716 720 717 721 static const struct iio_info inv_icm42600_gyro_info = {