Commit 384461ab authored by Uwe Kleine-König's avatar Uwe Kleine-König Committed by Thierry Reding
Browse files

pwm: Manage owner assignment implicitly for drivers



Instead of requiring each driver to care for assigning the owner member
of struct pwm_ops, handle that implicitly using a macro. Note that the
owner member has to be moved to struct pwm_chip, as the ops structure
usually lives in read-only memory and so cannot be modified.

The upside is that new low level drivers cannot forget the assignment and
save one line each. The pwm-crc driver didn't assign .owner, that's not
a problem in practice though as the driver cannot be compiled as a
module.

Acked-by: Andy Shevchenko <andy.shevchenko@gmail.com> # Intel LPSS
Reviewed-by: Florian Fainelli <florian.fainelli@broadcom.com> # pwm-{bcm,brcm}*.c
Acked-by: Jernej Skrabec <jernej.skrabec@gmail.com> # sun4i
Acked-by: default avatarAndi Shyti <andi.shyti@kernel.org>
Acked-by: Nobuhiro Iwamatsu <nobuhiro1.iwamatsu@toshiba.co.jp> # pwm-visconti
Acked-by: Heiko Stuebner <heiko@sntech.de> # pwm-rockchip
Acked-by: Michael Walle <michael@walle.cc> # pwm-sl28cpld
Acked-by: Neil Armstrong <neil.armstrong@linaro.org> # pwm-meson
Reviewed-by: default avatarLinus Walleij <linus.walleij@linaro.org>
Link: https://lore.kernel.org/r/20230804142707.412137-2-u.kleine-koenig@pengutronix.de


Signed-off-by: default avatarUwe Kleine-König <u.kleine-koenig@pengutronix.de>
Signed-off-by: default avatarThierry Reding <thierry.reding@gmail.com>
parent 7a3663c2
Loading
Loading
Loading
Loading
+0 −1
Original line number Diff line number Diff line
@@ -756,7 +756,6 @@ static const struct pwm_ops mvebu_pwm_ops = {
	.free = mvebu_pwm_free,
	.get_state = mvebu_pwm_get_state,
	.apply = mvebu_pwm_apply,
	.owner = THIS_MODULE,
};

static void __maybe_unused mvebu_pwm_suspend(struct mvebu_gpio_chip *mvchip)
+0 −1
Original line number Diff line number Diff line
@@ -1580,7 +1580,6 @@ static const struct pwm_ops ti_sn_pwm_ops = {
	.free = ti_sn_pwm_free,
	.apply = ti_sn_pwm_apply,
	.get_state = ti_sn_pwm_get_state,
	.owner = THIS_MODULE,
};

static int ti_sn_pwm_probe(struct auxiliary_device *adev,
+0 −1
Original line number Diff line number Diff line
@@ -1085,7 +1085,6 @@ static const struct pwm_ops lpg_pwm_ops = {
	.request = lpg_pwm_request,
	.apply = lpg_pwm_apply,
	.get_state = lpg_pwm_get_state,
	.owner = THIS_MODULE,
};

static int lpg_add_pwm(struct lpg *lpg)
+14 −10
Original line number Diff line number Diff line
@@ -89,13 +89,13 @@ static int pwm_device_request(struct pwm_device *pwm, const char *label)
	if (test_bit(PWMF_REQUESTED, &pwm->flags))
		return -EBUSY;

	if (!try_module_get(pwm->chip->ops->owner))
	if (!try_module_get(pwm->chip->owner))
		return -ENODEV;

	if (pwm->chip->ops->request) {
		err = pwm->chip->ops->request(pwm->chip, pwm);
		if (err) {
			module_put(pwm->chip->ops->owner);
			module_put(pwm->chip->owner);
			return err;
		}
	}
@@ -253,14 +253,16 @@ static bool pwm_ops_check(const struct pwm_chip *chip)
}

/**
 * pwmchip_add() - register a new PWM chip
 * __pwmchip_add() - register a new PWM chip
 * @chip: the PWM chip to add
 * @owner: reference to the module providing the chip.
 *
 * Register a new PWM chip.
 * Register a new PWM chip. @owner is supposed to be THIS_MODULE, use the
 * pwmchip_add wrapper to do this right.
 *
 * Returns: 0 on success or a negative error code on failure.
 */
int pwmchip_add(struct pwm_chip *chip)
int __pwmchip_add(struct pwm_chip *chip, struct module *owner)
{
	struct pwm_device *pwm;
	unsigned int i;
@@ -272,6 +274,8 @@ int pwmchip_add(struct pwm_chip *chip)
	if (!pwm_ops_check(chip))
		return -EINVAL;

	chip->owner = owner;

	chip->pwms = kcalloc(chip->npwm, sizeof(*pwm), GFP_KERNEL);
	if (!chip->pwms)
		return -ENOMEM;
@@ -306,7 +310,7 @@ int pwmchip_add(struct pwm_chip *chip)

	return 0;
}
EXPORT_SYMBOL_GPL(pwmchip_add);
EXPORT_SYMBOL_GPL(__pwmchip_add);

/**
 * pwmchip_remove() - remove a PWM chip
@@ -338,17 +342,17 @@ static void devm_pwmchip_remove(void *data)
	pwmchip_remove(chip);
}

int devm_pwmchip_add(struct device *dev, struct pwm_chip *chip)
int __devm_pwmchip_add(struct device *dev, struct pwm_chip *chip, struct module *owner)
{
	int ret;

	ret = pwmchip_add(chip);
	ret = __pwmchip_add(chip, owner);
	if (ret)
		return ret;

	return devm_add_action_or_reset(dev, devm_pwmchip_remove, chip);
}
EXPORT_SYMBOL_GPL(devm_pwmchip_add);
EXPORT_SYMBOL_GPL(__devm_pwmchip_add);

/**
 * pwm_request_from_chip() - request a PWM device relative to a PWM chip
@@ -979,7 +983,7 @@ void pwm_put(struct pwm_device *pwm)
	pwm_set_chip_data(pwm, NULL);
	pwm->label = NULL;

	module_put(pwm->chip->ops->owner);
	module_put(pwm->chip->owner);
out:
	mutex_unlock(&pwm_lock);
}
+0 −1
Original line number Diff line number Diff line
@@ -181,7 +181,6 @@ static int ab8500_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
static const struct pwm_ops ab8500_pwm_ops = {
	.apply = ab8500_pwm_apply,
	.get_state = ab8500_pwm_get_state,
	.owner = THIS_MODULE,
};

static int ab8500_pwm_probe(struct platform_device *pdev)
Loading