Skip to content

Commit

Permalink
ODROID: Revert "pwm: Drop support for legacy drivers"
Browse files Browse the repository at this point in the history
This reverts commit 0829c35.

Change-Id: Ib3d3db65304aef8e1d5251ef3b2ee3d302a2504f
  • Loading branch information
tobetter committed Jan 10, 2023
1 parent 083fd9d commit deef6ff
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 2 deletions.
85 changes: 83 additions & 2 deletions drivers/pwm/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,18 @@ EXPORT_SYMBOL_GPL(pwm_get_chip_data);

static bool pwm_ops_check(const struct pwm_chip *chip)
{

const struct pwm_ops *ops = chip->ops;

/* driver supports legacy, non-atomic operation */
if (ops->config && ops->enable && ops->disable) {
if (IS_ENABLED(CONFIG_PWM_DEBUG))
dev_warn(chip->dev,
"Driver needs updating to atomic API\n");

return true;
}

if (!ops->apply)
return false;

Expand Down Expand Up @@ -548,6 +558,73 @@ static void pwm_apply_state_debug(struct pwm_device *pwm,
}
}

static int pwm_apply_legacy(struct pwm_chip *chip, struct pwm_device *pwm,
const struct pwm_state *state)
{
int err;
struct pwm_state initial_state = pwm->state;

if (state->polarity != pwm->state.polarity) {
if (!chip->ops->set_polarity)
return -EINVAL;

/*
* Changing the polarity of a running PWM is only allowed when
* the PWM driver implements ->apply().
*/
if (pwm->state.enabled) {
chip->ops->disable(chip, pwm);

/*
* Update pwm->state already here in case
* .set_polarity() or another callback depend on that.
*/
pwm->state.enabled = false;
}

err = chip->ops->set_polarity(chip, pwm, state->polarity);
if (err)
goto rollback;

pwm->state.polarity = state->polarity;
}

if (!state->enabled) {
if (pwm->state.enabled)
chip->ops->disable(chip, pwm);

return 0;
}

/*
* We cannot skip calling ->config even if state->period ==
* pwm->state.period && state->duty_cycle == pwm->state.duty_cycle
* because we might have exited early in the last call to
* pwm_apply_state because of !state->enabled and so the two values in
* pwm->state might not be configured in hardware.
*/
err = chip->ops->config(pwm->chip, pwm,
state->duty_cycle,
state->period);
if (err)
goto rollback;

pwm->state.period = state->period;
pwm->state.duty_cycle = state->duty_cycle;

if (!pwm->state.enabled) {
err = chip->ops->enable(chip, pwm);
if (err)
goto rollback;
}

return 0;

rollback:
pwm->state = initial_state;
return err;
}

/**
* pwm_apply_state() - atomically apply a new state to a PWM device
* @pwm: PWM device
Expand Down Expand Up @@ -580,8 +657,12 @@ int pwm_apply_state(struct pwm_device *pwm, const struct pwm_state *state)
state->usage_power == pwm->state.usage_power)
return 0;

err = chip->ops->apply(chip, pwm, state);
trace_pwm_apply(pwm, state, err);
if (chip->ops->apply) {
err = chip->ops->apply(chip, pwm, state);
trace_pwm_apply(pwm, state, err);
} else {
err = pwm_apply_legacy(chip, pwm, state);
}
if (err)
return err;

Expand Down
12 changes: 12 additions & 0 deletions include/linux/pwm.h
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,10 @@ struct pwm_capture {
* called once per PWM device when the PWM chip is
* registered.
* @owner: helps prevent removal of modules exporting active PWMs
* @config: configure duty cycles and period length for this PWM
* @set_polarity: configure the polarity of this PWM
* @enable: enable PWM output toggling
* @disable: disable PWM output toggling
*/
struct pwm_ops {
int (*request)(struct pwm_chip *chip, struct pwm_device *pwm);
Expand All @@ -279,6 +283,14 @@ struct pwm_ops {
int (*get_state)(struct pwm_chip *chip, struct pwm_device *pwm,
struct pwm_state *state);
struct module *owner;

/* Only used by legacy drivers */
int (*config)(struct pwm_chip *chip, struct pwm_device *pwm,
int duty_ns, int period_ns);
int (*set_polarity)(struct pwm_chip *chip, struct pwm_device *pwm,
enum pwm_polarity polarity);
int (*enable)(struct pwm_chip *chip, struct pwm_device *pwm);
void (*disable)(struct pwm_chip *chip, struct pwm_device *pwm);
};

/**
Expand Down

1 comment on commit deef6ff

@ginkage
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You do not need to revert this if you apply #41

Please sign in to comment.