Skip to content

Commit 650af6c

Browse files
ukleinekUwe Kleine-König
authored andcommitted
pwm: Use guards for pwm_lock instead of explicity mutex_lock + mutex_unlock
With the compiler caring for unlocking the mutex several functions can be simplified. Benefit from that. Signed-off-by: Uwe Kleine-König <[email protected]> Link: https://lore.kernel.org/r/2102fe8189bdf1f02ff3785b551a69be27a65af4.1719520143.git.u.kleine-koenig@baylibre.com Signed-off-by: Uwe Kleine-König <[email protected]>
1 parent 44ee951 commit 650af6c

File tree

1 file changed

+15
-37
lines changed

1 file changed

+15
-37
lines changed

drivers/pwm/core.c

Lines changed: 15 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -293,19 +293,15 @@ EXPORT_SYMBOL_GPL(pwm_adjust_config);
293293
int pwm_capture(struct pwm_device *pwm, struct pwm_capture *result,
294294
unsigned long timeout)
295295
{
296-
int err;
297-
298296
if (!pwm || !pwm->chip->ops)
299297
return -EINVAL;
300298

301299
if (!pwm->chip->ops->capture)
302300
return -ENOSYS;
303301

304-
mutex_lock(&pwm_lock);
305-
err = pwm->chip->ops->capture(pwm->chip, pwm, result, timeout);
306-
mutex_unlock(&pwm_lock);
302+
guard(mutex)(&pwm_lock);
307303

308-
return err;
304+
return pwm->chip->ops->capture(pwm->chip, pwm, result, timeout);
309305
}
310306
EXPORT_SYMBOL_GPL(pwm_capture);
311307

@@ -317,19 +313,15 @@ static struct pwm_chip *pwmchip_find_by_name(const char *name)
317313
if (!name)
318314
return NULL;
319315

320-
mutex_lock(&pwm_lock);
316+
guard(mutex)(&pwm_lock);
321317

322318
idr_for_each_entry_ul(&pwm_chips, chip, tmp, id) {
323319
const char *chip_name = dev_name(pwmchip_parent(chip));
324320

325-
if (chip_name && strcmp(chip_name, name) == 0) {
326-
mutex_unlock(&pwm_lock);
321+
if (chip_name && strcmp(chip_name, name) == 0)
327322
return chip;
328-
}
329323
}
330324

331-
mutex_unlock(&pwm_lock);
332-
333325
return NULL;
334326
}
335327

@@ -406,14 +398,14 @@ static struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
406398
if (!chip || index >= chip->npwm)
407399
return ERR_PTR(-EINVAL);
408400

409-
mutex_lock(&pwm_lock);
401+
guard(mutex)(&pwm_lock);
402+
410403
pwm = &chip->pwms[index];
411404

412405
err = pwm_device_request(pwm, label);
413406
if (err < 0)
414-
pwm = ERR_PTR(err);
407+
return ERR_PTR(err);
415408

416-
mutex_unlock(&pwm_lock);
417409
return pwm;
418410
}
419411

@@ -1102,11 +1094,11 @@ int __pwmchip_add(struct pwm_chip *chip, struct module *owner)
11021094

11031095
chip->owner = owner;
11041096

1105-
mutex_lock(&pwm_lock);
1097+
guard(mutex)(&pwm_lock);
11061098

11071099
ret = idr_alloc(&pwm_chips, chip, 0, 0, GFP_KERNEL);
11081100
if (ret < 0)
1109-
goto err_idr_alloc;
1101+
return ret;
11101102

11111103
chip->id = ret;
11121104

@@ -1119,18 +1111,13 @@ int __pwmchip_add(struct pwm_chip *chip, struct module *owner)
11191111
if (ret)
11201112
goto err_device_add;
11211113

1122-
mutex_unlock(&pwm_lock);
1123-
11241114
return 0;
11251115

11261116
err_device_add:
11271117
if (IS_ENABLED(CONFIG_OF))
11281118
of_pwmchip_remove(chip);
11291119

11301120
idr_remove(&pwm_chips, chip->id);
1131-
err_idr_alloc:
1132-
1133-
mutex_unlock(&pwm_lock);
11341121

11351122
return ret;
11361123
}
@@ -1149,11 +1136,8 @@ void pwmchip_remove(struct pwm_chip *chip)
11491136
if (IS_ENABLED(CONFIG_OF))
11501137
of_pwmchip_remove(chip);
11511138

1152-
mutex_lock(&pwm_lock);
1153-
1154-
idr_remove(&pwm_chips, chip->id);
1155-
1156-
mutex_unlock(&pwm_lock);
1139+
scoped_guard(mutex, &pwm_lock)
1140+
idr_remove(&pwm_chips, chip->id);
11571141

11581142
device_del(&chip->dev);
11591143
}
@@ -1209,15 +1193,11 @@ static struct pwm_chip *fwnode_to_pwmchip(struct fwnode_handle *fwnode)
12091193
struct pwm_chip *chip;
12101194
unsigned long id, tmp;
12111195

1212-
mutex_lock(&pwm_lock);
1196+
guard(mutex)(&pwm_lock);
12131197

12141198
idr_for_each_entry_ul(&pwm_chips, chip, tmp, id)
1215-
if (pwmchip_parent(chip) && device_match_fwnode(pwmchip_parent(chip), fwnode)) {
1216-
mutex_unlock(&pwm_lock);
1199+
if (pwmchip_parent(chip) && device_match_fwnode(pwmchip_parent(chip), fwnode))
12171200
return chip;
1218-
}
1219-
1220-
mutex_unlock(&pwm_lock);
12211201

12221202
return ERR_PTR(-EPROBE_DEFER);
12231203
}
@@ -1532,11 +1512,11 @@ void pwm_put(struct pwm_device *pwm)
15321512

15331513
chip = pwm->chip;
15341514

1535-
mutex_lock(&pwm_lock);
1515+
guard(mutex)(&pwm_lock);
15361516

15371517
if (!test_and_clear_bit(PWMF_REQUESTED, &pwm->flags)) {
15381518
pr_warn("PWM device already freed\n");
1539-
goto out;
1519+
return;
15401520
}
15411521

15421522
if (chip->ops->free)
@@ -1547,8 +1527,6 @@ void pwm_put(struct pwm_device *pwm)
15471527
put_device(&chip->dev);
15481528

15491529
module_put(chip->owner);
1550-
out:
1551-
mutex_unlock(&pwm_lock);
15521530
}
15531531
EXPORT_SYMBOL_GPL(pwm_put);
15541532

0 commit comments

Comments
 (0)