Commit 3e5d4a6e authored by Martin Blumenstingl's avatar Martin Blumenstingl Committed by Greg Kroah-Hartman

pwm: meson: Use the spin-lock only to protect register modifications

[ Upstream commit f173747f ]

Holding the spin-lock for all of the code in meson_pwm_apply() can
result in a "BUG: scheduling while atomic". This can happen because
clk_get_rate() (which is called from meson_pwm_calc()) may sleep.
Only hold the spin-lock when modifying registers to solve this.

The reason why we need a spin-lock in the driver is because the
REG_MISC_AB register is shared between the two channels provided by one
PWM controller. The only functions where REG_MISC_AB is modified are
meson_pwm_enable() and meson_pwm_disable() so the register reads/writes
in there need to be protected by the spin-lock.

The original code also used the spin-lock to protect the values in
struct meson_pwm_channel. This could be necessary if two consumers can
use the same PWM channel. However, PWM core doesn't allow this so we
don't need to protect the values in struct meson_pwm_channel with a
lock.

Fixes: 211ed630 ("pwm: Add support for Meson PWM Controller")
Signed-off-by: default avatarMartin Blumenstingl <martin.blumenstingl@googlemail.com>
Reviewed-by: default avatarUwe Kleine-König <u.kleine-koenig@pengutronix.de>
Reviewed-by: default avatarNeil Armstrong <narmstrong@baylibre.com>
Signed-off-by: default avatarThierry Reding <thierry.reding@gmail.com>
Signed-off-by: default avatarSasha Levin <sashal@kernel.org>
parent ffbb6164
...@@ -111,6 +111,10 @@ struct meson_pwm { ...@@ -111,6 +111,10 @@ struct meson_pwm {
const struct meson_pwm_data *data; const struct meson_pwm_data *data;
void __iomem *base; void __iomem *base;
u8 inverter_mask; u8 inverter_mask;
/*
* Protects register (write) access to the REG_MISC_AB register
* that is shared between the two PWMs.
*/
spinlock_t lock; spinlock_t lock;
}; };
...@@ -235,6 +239,7 @@ static void meson_pwm_enable(struct meson_pwm *meson, ...@@ -235,6 +239,7 @@ static void meson_pwm_enable(struct meson_pwm *meson,
{ {
u32 value, clk_shift, clk_enable, enable; u32 value, clk_shift, clk_enable, enable;
unsigned int offset; unsigned int offset;
unsigned long flags;
switch (id) { switch (id) {
case 0: case 0:
...@@ -255,6 +260,8 @@ static void meson_pwm_enable(struct meson_pwm *meson, ...@@ -255,6 +260,8 @@ static void meson_pwm_enable(struct meson_pwm *meson,
return; return;
} }
spin_lock_irqsave(&meson->lock, flags);
value = readl(meson->base + REG_MISC_AB); value = readl(meson->base + REG_MISC_AB);
value &= ~(MISC_CLK_DIV_MASK << clk_shift); value &= ~(MISC_CLK_DIV_MASK << clk_shift);
value |= channel->pre_div << clk_shift; value |= channel->pre_div << clk_shift;
...@@ -267,11 +274,14 @@ static void meson_pwm_enable(struct meson_pwm *meson, ...@@ -267,11 +274,14 @@ static void meson_pwm_enable(struct meson_pwm *meson,
value = readl(meson->base + REG_MISC_AB); value = readl(meson->base + REG_MISC_AB);
value |= enable; value |= enable;
writel(value, meson->base + REG_MISC_AB); writel(value, meson->base + REG_MISC_AB);
spin_unlock_irqrestore(&meson->lock, flags);
} }
static void meson_pwm_disable(struct meson_pwm *meson, unsigned int id) static void meson_pwm_disable(struct meson_pwm *meson, unsigned int id)
{ {
u32 value, enable; u32 value, enable;
unsigned long flags;
switch (id) { switch (id) {
case 0: case 0:
...@@ -286,9 +296,13 @@ static void meson_pwm_disable(struct meson_pwm *meson, unsigned int id) ...@@ -286,9 +296,13 @@ static void meson_pwm_disable(struct meson_pwm *meson, unsigned int id)
return; return;
} }
spin_lock_irqsave(&meson->lock, flags);
value = readl(meson->base + REG_MISC_AB); value = readl(meson->base + REG_MISC_AB);
value &= ~enable; value &= ~enable;
writel(value, meson->base + REG_MISC_AB); writel(value, meson->base + REG_MISC_AB);
spin_unlock_irqrestore(&meson->lock, flags);
} }
static int meson_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, static int meson_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
...@@ -296,19 +310,16 @@ static int meson_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, ...@@ -296,19 +310,16 @@ static int meson_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
{ {
struct meson_pwm_channel *channel = pwm_get_chip_data(pwm); struct meson_pwm_channel *channel = pwm_get_chip_data(pwm);
struct meson_pwm *meson = to_meson_pwm(chip); struct meson_pwm *meson = to_meson_pwm(chip);
unsigned long flags;
int err = 0; int err = 0;
if (!state) if (!state)
return -EINVAL; return -EINVAL;
spin_lock_irqsave(&meson->lock, flags);
if (!state->enabled) { if (!state->enabled) {
meson_pwm_disable(meson, pwm->hwpwm); meson_pwm_disable(meson, pwm->hwpwm);
channel->state.enabled = false; channel->state.enabled = false;
goto unlock; return 0;
} }
if (state->period != channel->state.period || if (state->period != channel->state.period ||
...@@ -329,7 +340,7 @@ static int meson_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, ...@@ -329,7 +340,7 @@ static int meson_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
err = meson_pwm_calc(meson, channel, pwm->hwpwm, err = meson_pwm_calc(meson, channel, pwm->hwpwm,
state->duty_cycle, state->period); state->duty_cycle, state->period);
if (err < 0) if (err < 0)
goto unlock; return err;
channel->state.polarity = state->polarity; channel->state.polarity = state->polarity;
channel->state.period = state->period; channel->state.period = state->period;
...@@ -341,9 +352,7 @@ static int meson_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, ...@@ -341,9 +352,7 @@ static int meson_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
channel->state.enabled = true; channel->state.enabled = true;
} }
unlock: return 0;
spin_unlock_irqrestore(&meson->lock, flags);
return err;
} }
static void meson_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm, static void meson_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment