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

leds: leds-lp55xx: Convert mutex lock/unlock to guard API

Convert any entry of mutex lock/unlock to guard API and simplify code.
With the use of guard API, handling for selttest functions can be
greatly simplified.

Suggested-by: Markus Elfring <Markus.Elfring@web.de>
Signed-off-by: Christian Marangi <ansuelsmth@gmail.com>
Link: https://lore.kernel.org/r/20240626221520.2846-3-ansuelsmth@gmail.com
Signed-off-by: Lee Jones <lee@kernel.org>

authored by

Christian Marangi and committed by
Lee Jones
4137d94f 8eac0379

+64 -92
+3 -2
drivers/leds/leds-lp5521.c
··· 9 9 * Milo(Woogyom) Kim <milo.kim@ti.com> 10 10 */ 11 11 12 + #include <linux/cleanup.h> 12 13 #include <linux/delay.h> 13 14 #include <linux/firmware.h> 14 15 #include <linux/i2c.h> ··· 186 185 struct lp55xx_chip *chip = led->chip; 187 186 int ret; 188 187 189 - mutex_lock(&chip->lock); 188 + guard(mutex)(&chip->lock); 189 + 190 190 ret = lp5521_run_selftest(chip, buf); 191 - mutex_unlock(&chip->lock); 192 191 193 192 return sysfs_emit(buf, "%s\n", ret ? "FAIL" : "OK"); 194 193 }
+9 -16
drivers/leds/leds-lp5523.c
··· 9 9 * Milo(Woogyom) Kim <milo.kim@ti.com> 10 10 */ 11 11 12 + #include <linux/cleanup.h> 12 13 #include <linux/delay.h> 13 14 #include <linux/firmware.h> 14 15 #include <linux/i2c.h> ··· 189 188 int ret, pos = 0; 190 189 u8 status, adc, vdd, i; 191 190 192 - mutex_lock(&chip->lock); 191 + guard(mutex)(&chip->lock); 193 192 194 193 ret = lp55xx_read(chip, LP5523_REG_STATUS, &status); 195 194 if (ret < 0) 196 - goto fail; 195 + return sysfs_emit(buf, "FAIL\n"); 197 196 198 197 /* Check that ext clock is really in use if requested */ 199 198 if (pdata->clock_mode == LP55XX_CLOCK_EXT) { 200 199 if ((status & LP5523_EXT_CLK_USED) == 0) 201 - goto fail; 200 + return sysfs_emit(buf, "FAIL\n"); 202 201 } 203 202 204 203 /* Measure VDD (i.e. VBAT) first (channel 16 corresponds to VDD) */ ··· 206 205 usleep_range(3000, 6000); /* ADC conversion time is typically 2.7 ms */ 207 206 ret = lp55xx_read(chip, LP5523_REG_STATUS, &status); 208 207 if (ret < 0) 209 - goto fail; 208 + return sysfs_emit(buf, "FAIL\n"); 210 209 211 210 if (!(status & LP5523_LEDTEST_DONE)) 212 211 usleep_range(3000, 6000); /* Was not ready. Wait little bit */ 213 212 214 213 ret = lp55xx_read(chip, LP5523_REG_LED_TEST_ADC, &vdd); 215 214 if (ret < 0) 216 - goto fail; 215 + return sysfs_emit(buf, "FAIL\n"); 217 216 218 217 vdd--; /* There may be some fluctuation in measurement */ 219 218 ··· 236 235 usleep_range(3000, 6000); 237 236 ret = lp55xx_read(chip, LP5523_REG_STATUS, &status); 238 237 if (ret < 0) 239 - goto fail; 238 + return sysfs_emit(buf, "FAIL\n"); 240 239 241 240 if (!(status & LP5523_LEDTEST_DONE)) 242 241 usleep_range(3000, 6000); /* Was not ready. Wait. */ 243 242 244 243 ret = lp55xx_read(chip, LP5523_REG_LED_TEST_ADC, &adc); 245 244 if (ret < 0) 246 - goto fail; 245 + return sysfs_emit(buf, "FAIL\n"); 247 246 248 247 if (adc >= vdd || adc < LP5523_ADC_SHORTCIRC_LIM) 249 248 pos += sysfs_emit_at(buf, pos, "LED %d FAIL\n", ··· 257 256 led->led_current); 258 257 led++; 259 258 } 260 - if (pos == 0) 261 - pos = sysfs_emit(buf, "OK\n"); 262 - goto release_lock; 263 - fail: 264 - pos = sysfs_emit(buf, "FAIL\n"); 265 259 266 - release_lock: 267 - mutex_unlock(&chip->lock); 268 - 269 - return pos; 260 + return pos == 0 ? sysfs_emit(buf, "OK\n") : pos; 270 261 } 271 262 272 263 LP55XX_DEV_ATTR_ENGINE_MODE(1);
+7 -6
drivers/leds/leds-lp5562.c
··· 7 7 * Author: Milo(Woogyom) Kim <milo.kim@ti.com> 8 8 */ 9 9 10 + #include <linux/cleanup.h> 10 11 #include <linux/delay.h> 11 12 #include <linux/firmware.h> 12 13 #include <linux/i2c.h> ··· 172 171 }; 173 172 int ret; 174 173 175 - mutex_lock(&chip->lock); 174 + guard(mutex)(&chip->lock); 175 + 176 176 ret = lp55xx_write(chip, addr[led->chan_nr], led->brightness); 177 - mutex_unlock(&chip->lock); 178 177 179 178 return ret; 180 179 } ··· 269 268 if (mode > num_patterns || !ptn) 270 269 return -EINVAL; 271 270 272 - mutex_lock(&chip->lock); 271 + guard(mutex)(&chip->lock); 272 + 273 273 ret = lp5562_run_predef_led_pattern(chip, mode); 274 - mutex_unlock(&chip->lock); 275 274 276 275 if (ret) 277 276 return ret; ··· 321 320 return -EINVAL; 322 321 } 323 322 324 - mutex_lock(&chip->lock); 323 + guard(mutex)(&chip->lock); 324 + 325 325 lp55xx_update_bits(chip, LP5562_REG_ENG_SEL, mask, val); 326 - mutex_unlock(&chip->lock); 327 326 328 327 return len; 329 328 }
+5 -13
drivers/leds/leds-lp5569.c
··· 4 4 */ 5 5 6 6 #include <linux/bitfield.h> 7 + #include <linux/cleanup.h> 7 8 #include <linux/delay.h> 8 9 #include <linux/firmware.h> 9 10 #include <linux/i2c.h> ··· 397 396 struct lp55xx_chip *chip = led->chip; 398 397 int i, pos = 0; 399 398 400 - mutex_lock(&chip->lock); 399 + guard(mutex)(&chip->lock); 401 400 402 401 /* Test LED Open */ 403 402 pos = lp5569_led_open_test(led, buf); 404 403 if (pos < 0) 405 - goto fail; 404 + return sprintf(buf, "FAIL\n"); 406 405 407 406 /* Test LED Shorted */ 408 407 pos += lp5569_led_short_test(led, buf); 409 408 if (pos < 0) 410 - goto fail; 409 + return sprintf(buf, "FAIL\n"); 411 410 412 411 for (i = 0; i < chip->pdata->num_channels; i++) { 413 412 /* Restore current */ ··· 420 419 led++; 421 420 } 422 421 423 - if (pos == 0) 424 - pos = sysfs_emit(buf, "OK\n"); 425 - goto release_lock; 426 - fail: 427 - pos = sysfs_emit(buf, "FAIL\n"); 428 - 429 - release_lock: 430 - mutex_unlock(&chip->lock); 431 - 432 - return pos; 422 + return pos == 0 ? sysfs_emit(buf, "OK\n") : pos; 433 423 } 434 424 435 425 LP55XX_DEV_ATTR_ENGINE_MODE(1);
+40 -55
drivers/leds/leds-lp55xx-common.c
··· 10 10 */ 11 11 12 12 #include <linux/bitfield.h> 13 + #include <linux/cleanup.h> 13 14 #include <linux/clk.h> 14 15 #include <linux/delay.h> 15 16 #include <linux/firmware.h> ··· 273 272 const struct lp55xx_device_config *cfg = chip->cfg; 274 273 int ret; 275 274 276 - mutex_lock(&chip->lock); 275 + guard(mutex)(&chip->lock); 276 + 277 277 ret = lp55xx_write(chip, cfg->reg_led_pwm_base.addr + led->chan_nr, 278 278 led->brightness); 279 - mutex_unlock(&chip->lock); 280 279 return ret; 281 280 } 282 281 EXPORT_SYMBOL_GPL(lp55xx_led_brightness); ··· 288 287 int ret; 289 288 int i; 290 289 291 - mutex_lock(&chip->lock); 290 + guard(mutex)(&chip->lock); 291 + 292 292 for (i = 0; i < led->mc_cdev.num_colors; i++) { 293 293 ret = lp55xx_write(chip, 294 294 cfg->reg_led_pwm_base.addr + ··· 298 296 if (ret) 299 297 break; 300 298 } 301 - mutex_unlock(&chip->lock); 299 + 302 300 return ret; 303 301 } 304 302 EXPORT_SYMBOL_GPL(lp55xx_multicolor_brightness); ··· 406 404 if (!chip->cfg->set_led_current) 407 405 return len; 408 406 409 - mutex_lock(&chip->lock); 407 + guard(mutex)(&chip->lock); 408 + 410 409 chip->cfg->set_led_current(led, (u8)curr); 411 - mutex_unlock(&chip->lock); 412 410 413 411 return len; 414 412 } ··· 543 541 } 544 542 545 543 /* handling firmware data is chip dependent */ 546 - mutex_lock(&chip->lock); 547 - 548 - chip->engines[idx - 1].mode = LP55XX_ENGINE_LOAD; 549 - chip->fw = fw; 550 - if (chip->cfg->firmware_cb) 551 - chip->cfg->firmware_cb(chip); 552 - 553 - mutex_unlock(&chip->lock); 544 + scoped_guard(mutex, &chip->lock) { 545 + chip->engines[idx - 1].mode = LP55XX_ENGINE_LOAD; 546 + chip->fw = fw; 547 + if (chip->cfg->firmware_cb) 548 + chip->cfg->firmware_cb(chip); 549 + } 554 550 555 551 /* firmware should be released for other channel use */ 556 552 release_firmware(chip->fw); ··· 592 592 case LP55XX_ENGINE_1: 593 593 case LP55XX_ENGINE_2: 594 594 case LP55XX_ENGINE_3: 595 - mutex_lock(&chip->lock); 596 - chip->engine_idx = val; 597 - ret = lp55xx_request_firmware(chip); 598 - mutex_unlock(&chip->lock); 595 + scoped_guard(mutex, &chip->lock) { 596 + chip->engine_idx = val; 597 + ret = lp55xx_request_firmware(chip); 598 + } 599 599 break; 600 600 default: 601 601 dev_err(dev, "%lu: invalid engine index. (1, 2, 3)\n", val); ··· 634 634 return len; 635 635 } 636 636 637 - mutex_lock(&chip->lock); 637 + guard(mutex)(&chip->lock); 638 + 638 639 lp55xx_run_engine(chip, true); 639 - mutex_unlock(&chip->lock); 640 640 641 641 return len; 642 642 } ··· 673 673 const struct lp55xx_device_config *cfg = chip->cfg; 674 674 struct lp55xx_engine *engine = &chip->engines[nr - 1]; 675 675 676 - mutex_lock(&chip->lock); 676 + guard(mutex)(&chip->lock); 677 677 678 678 chip->engine_idx = nr; 679 679 ··· 689 689 engine->mode = LP55XX_ENGINE_DISABLED; 690 690 } 691 691 692 - mutex_unlock(&chip->lock); 693 - 694 692 return len; 695 693 } 696 694 EXPORT_SYMBOL_GPL(lp55xx_store_engine_mode); ··· 701 703 struct lp55xx_chip *chip = led->chip; 702 704 int ret; 703 705 704 - mutex_lock(&chip->lock); 706 + guard(mutex)(&chip->lock); 705 707 706 708 chip->engine_idx = nr; 707 709 lp55xx_load_engine(chip); 708 710 ret = lp55xx_update_program_memory(chip, buf, len); 709 - 710 - mutex_unlock(&chip->lock); 711 711 712 712 return ret; 713 713 } ··· 795 799 struct lp55xx_chip *chip = led->chip; 796 800 struct lp55xx_engine *engine = &chip->engines[nr - 1]; 797 801 u16 mux = 0; 798 - ssize_t ret; 799 802 800 803 if (lp55xx_mux_parse(chip, buf, &mux, len)) 801 804 return -EINVAL; 802 805 803 - mutex_lock(&chip->lock); 806 + guard(mutex)(&chip->lock); 804 807 805 808 chip->engine_idx = nr; 806 - ret = -EINVAL; 807 809 808 810 if (engine->mode != LP55XX_ENGINE_LOAD) 809 - goto leave; 811 + return -EINVAL; 810 812 811 813 if (lp55xx_load_mux(chip, mux, nr)) 812 - goto leave; 814 + return -EINVAL; 813 815 814 - ret = len; 815 - leave: 816 - mutex_unlock(&chip->lock); 817 - return ret; 816 + return len; 818 817 } 819 818 EXPORT_SYMBOL_GPL(lp55xx_store_engine_leds); 820 819 ··· 823 832 int ret; 824 833 u8 val; 825 834 826 - mutex_lock(&chip->lock); 835 + guard(mutex)(&chip->lock); 836 + 827 837 ret = lp55xx_read(chip, cfg->reg_master_fader_base.addr + nr - 1, &val); 828 - mutex_unlock(&chip->lock); 829 838 830 839 return ret ? ret : sysfs_emit(buf, "%u\n", val); 831 840 } ··· 847 856 if (val > 0xff) 848 857 return -EINVAL; 849 858 850 - mutex_lock(&chip->lock); 859 + guard(mutex)(&chip->lock); 860 + 851 861 ret = lp55xx_write(chip, cfg->reg_master_fader_base.addr + nr - 1, 852 862 (u8)val); 853 - mutex_unlock(&chip->lock); 854 863 855 864 return ret ? ret : len; 856 865 } ··· 866 875 int i, ret, pos = 0; 867 876 u8 val; 868 877 869 - mutex_lock(&chip->lock); 878 + guard(mutex)(&chip->lock); 870 879 871 880 for (i = 0; i < cfg->max_channel; i++) { 872 881 ret = lp55xx_read(chip, cfg->reg_led_ctrl_base.addr + i, &val); 873 882 if (ret) 874 - goto leave; 883 + return ret; 875 884 876 885 val = FIELD_GET(LP55xx_FADER_MAPPING_MASK, val); 877 886 if (val > FIELD_MAX(LP55xx_FADER_MAPPING_MASK)) { 878 - ret = -EINVAL; 879 - goto leave; 887 + return -EINVAL; 880 888 } 881 889 buf[pos++] = val + '0'; 882 890 } 883 891 buf[pos++] = '\n'; 884 - ret = pos; 885 - leave: 886 - mutex_unlock(&chip->lock); 887 - return ret; 892 + 893 + return pos; 888 894 } 889 895 EXPORT_SYMBOL_GPL(lp55xx_show_master_fader_leds); 890 896 ··· 897 909 898 910 n = min_t(int, len, cfg->max_channel); 899 911 900 - mutex_lock(&chip->lock); 912 + guard(mutex)(&chip->lock); 901 913 902 914 for (i = 0; i < n; i++) { 903 915 if (buf[i] >= '0' && buf[i] <= '3') { ··· 907 919 LP55xx_FADER_MAPPING_MASK, 908 920 val); 909 921 if (ret) 910 - goto leave; 922 + return ret; 911 923 } else { 912 - ret = -EINVAL; 913 - goto leave; 924 + return -EINVAL; 914 925 } 915 926 } 916 - ret = len; 917 - leave: 918 - mutex_unlock(&chip->lock); 919 - return ret; 927 + 928 + return len; 920 929 } 921 930 EXPORT_SYMBOL_GPL(lp55xx_store_master_fader_leds); 922 931