Commit 62928315 authored by Uwe Kleine-König's avatar Uwe Kleine-König

pwm: Reorder symbols in core.c

This moves the functions called by pwm consumers above the functions
called by pwm providers. When character device support is added later
this is hooked into the chip registration functions. As the needed
callbacks are a kind of consumer and make use of the consumer functions,
having this order is more natural and prevents having to add
declarations for static functions.

Also move the global variables for pwm tables to the respective
functions to have them properly grouped.

Link: https://lore.kernel.org/r/eed83de07bdfb69b5ceba0b9aed757ee612dea8f.1706182805.git.u.kleine-koenig@pengutronix.deSigned-off-by: default avatarUwe Kleine-König <u.kleine-koenig@pengutronix.de>
parent bdc585f9
...@@ -24,256 +24,358 @@ ...@@ -24,256 +24,358 @@
#define CREATE_TRACE_POINTS #define CREATE_TRACE_POINTS
#include <trace/events/pwm.h> #include <trace/events/pwm.h>
static DEFINE_MUTEX(pwm_lookup_lock);
static LIST_HEAD(pwm_lookup_list);
/* protects access to pwm_chips */ /* protects access to pwm_chips */
static DEFINE_MUTEX(pwm_lock); static DEFINE_MUTEX(pwm_lock);
static DEFINE_IDR(pwm_chips); static DEFINE_IDR(pwm_chips);
static struct pwm_chip *pwmchip_find_by_name(const char *name) static void pwm_apply_debug(struct pwm_device *pwm,
const struct pwm_state *state)
{ {
struct pwm_chip *chip; struct pwm_state *last = &pwm->last;
unsigned long id, tmp; struct pwm_chip *chip = pwm->chip;
struct pwm_state s1 = { 0 }, s2 = { 0 };
if (!name) int err;
return NULL;
mutex_lock(&pwm_lock);
idr_for_each_entry_ul(&pwm_chips, chip, tmp, id) { if (!IS_ENABLED(CONFIG_PWM_DEBUG))
const char *chip_name = dev_name(chip->dev); return;
if (chip_name && strcmp(chip_name, name) == 0) { /* No reasonable diagnosis possible without .get_state() */
mutex_unlock(&pwm_lock); if (!chip->ops->get_state)
return chip; return;
}
}
mutex_unlock(&pwm_lock); /*
* *state was just applied. Read out the hardware state and do some
* checks.
*/
return NULL; err = chip->ops->get_state(chip, pwm, &s1);
} trace_pwm_get(pwm, &s1, err);
if (err)
/* If that failed there isn't much to debug */
return;
static int pwm_device_request(struct pwm_device *pwm, const char *label) /*
{ * The lowlevel driver either ignored .polarity (which is a bug) or as
int err; * best effort inverted .polarity and fixed .duty_cycle respectively.
struct pwm_chip *chip = pwm->chip; * Undo this inversion and fixup for further tests.
const struct pwm_ops *ops = chip->ops; */
if (s1.enabled && s1.polarity != state->polarity) {
s2.polarity = state->polarity;
s2.duty_cycle = s1.period - s1.duty_cycle;
s2.period = s1.period;
s2.enabled = s1.enabled;
} else {
s2 = s1;
}
if (test_bit(PWMF_REQUESTED, &pwm->flags)) if (s2.polarity != state->polarity &&
return -EBUSY; state->duty_cycle < state->period)
dev_warn(chip->dev, ".apply ignored .polarity\n");
if (!try_module_get(chip->owner)) if (state->enabled &&
return -ENODEV; last->polarity == state->polarity &&
last->period > s2.period &&
last->period <= state->period)
dev_warn(chip->dev,
".apply didn't pick the best available period (requested: %llu, applied: %llu, possible: %llu)\n",
state->period, s2.period, last->period);
if (ops->request) { if (state->enabled && state->period < s2.period)
err = ops->request(chip, pwm); dev_warn(chip->dev,
if (err) { ".apply is supposed to round down period (requested: %llu, applied: %llu)\n",
module_put(chip->owner); state->period, s2.period);
return err;
}
}
if (ops->get_state) { if (state->enabled &&
/* last->polarity == state->polarity &&
* Zero-initialize state because most drivers are unaware of last->period == s2.period &&
* .usage_power. The other members of state are supposed to be last->duty_cycle > s2.duty_cycle &&
* set by lowlevel drivers. We still initialize the whole last->duty_cycle <= state->duty_cycle)
* structure for simplicity even though this might paper over dev_warn(chip->dev,
* faulty implementations of .get_state(). ".apply didn't pick the best available duty cycle (requested: %llu/%llu, applied: %llu/%llu, possible: %llu/%llu)\n",
*/ state->duty_cycle, state->period,
struct pwm_state state = { 0, }; s2.duty_cycle, s2.period,
last->duty_cycle, last->period);
err = ops->get_state(chip, pwm, &state); if (state->enabled && state->duty_cycle < s2.duty_cycle)
trace_pwm_get(pwm, &state, err); dev_warn(chip->dev,
".apply is supposed to round down duty_cycle (requested: %llu/%llu, applied: %llu/%llu)\n",
state->duty_cycle, state->period,
s2.duty_cycle, s2.period);
if (!err) if (!state->enabled && s2.enabled && s2.duty_cycle > 0)
pwm->state = state; dev_warn(chip->dev,
"requested disabled, but yielded enabled with duty > 0\n");
if (IS_ENABLED(CONFIG_PWM_DEBUG)) /* reapply the state that the driver reported being configured. */
pwm->last = pwm->state; err = chip->ops->apply(chip, pwm, &s1);
trace_pwm_apply(pwm, &s1, err);
if (err) {
*last = s1;
dev_err(chip->dev, "failed to reapply current setting\n");
return;
} }
set_bit(PWMF_REQUESTED, &pwm->flags); *last = (struct pwm_state){ 0 };
pwm->label = label; err = chip->ops->get_state(chip, pwm, last);
trace_pwm_get(pwm, last, err);
if (err)
return;
return 0; /* reapplication of the current state should give an exact match */
if (s1.enabled != last->enabled ||
s1.polarity != last->polarity ||
(s1.enabled && s1.period != last->period) ||
(s1.enabled && s1.duty_cycle != last->duty_cycle)) {
dev_err(chip->dev,
".apply is not idempotent (ena=%d pol=%d %llu/%llu) -> (ena=%d pol=%d %llu/%llu)\n",
s1.enabled, s1.polarity, s1.duty_cycle, s1.period,
last->enabled, last->polarity, last->duty_cycle,
last->period);
}
} }
struct pwm_device * /**
of_pwm_xlate_with_flags(struct pwm_chip *chip, const struct of_phandle_args *args) * __pwm_apply() - atomically apply a new state to a PWM device
* @pwm: PWM device
* @state: new state to apply
*/
static int __pwm_apply(struct pwm_device *pwm, const struct pwm_state *state)
{ {
struct pwm_device *pwm; struct pwm_chip *chip;
int err;
/* period in the second cell and flags in the third cell are optional */ if (!pwm || !state || !state->period ||
if (args->args_count < 1) state->duty_cycle > state->period)
return ERR_PTR(-EINVAL); return -EINVAL;
pwm = pwm_request_from_chip(chip, args->args[0], NULL); chip = pwm->chip;
if (IS_ERR(pwm))
return pwm;
if (args->args_count > 1) if (state->period == pwm->state.period &&
pwm->args.period = args->args[1]; state->duty_cycle == pwm->state.duty_cycle &&
state->polarity == pwm->state.polarity &&
state->enabled == pwm->state.enabled &&
state->usage_power == pwm->state.usage_power)
return 0;
pwm->args.polarity = PWM_POLARITY_NORMAL; err = chip->ops->apply(chip, pwm, state);
if (args->args_count > 2 && args->args[2] & PWM_POLARITY_INVERTED) trace_pwm_apply(pwm, state, err);
pwm->args.polarity = PWM_POLARITY_INVERSED; if (err)
return err;
return pwm; pwm->state = *state;
/*
* only do this after pwm->state was applied as some
* implementations of .get_state depend on this
*/
pwm_apply_debug(pwm, state);
return 0;
} }
EXPORT_SYMBOL_GPL(of_pwm_xlate_with_flags);
struct pwm_device * /**
of_pwm_single_xlate(struct pwm_chip *chip, const struct of_phandle_args *args) * pwm_apply_might_sleep() - atomically apply a new state to a PWM device
* Cannot be used in atomic context.
* @pwm: PWM device
* @state: new state to apply
*/
int pwm_apply_might_sleep(struct pwm_device *pwm, const struct pwm_state *state)
{ {
struct pwm_device *pwm; int err;
pwm = pwm_request_from_chip(chip, 0, NULL);
if (IS_ERR(pwm))
return pwm;
if (args->args_count > 1) /*
pwm->args.period = args->args[0]; * Some lowlevel driver's implementations of .apply() make use of
* mutexes, also with some drivers only returning when the new
* configuration is active calling pwm_apply_might_sleep() from atomic context
* is a bad idea. So make it explicit that calling this function might
* sleep.
*/
might_sleep();
pwm->args.polarity = PWM_POLARITY_NORMAL; if (IS_ENABLED(CONFIG_PWM_DEBUG) && pwm->chip->atomic) {
if (args->args_count > 1 && args->args[1] & PWM_POLARITY_INVERTED) /*
pwm->args.polarity = PWM_POLARITY_INVERSED; * Catch any drivers that have been marked as atomic but
* that will sleep anyway.
*/
non_block_start();
err = __pwm_apply(pwm, state);
non_block_end();
} else {
err = __pwm_apply(pwm, state);
}
return pwm; return err;
} }
EXPORT_SYMBOL_GPL(of_pwm_single_xlate); EXPORT_SYMBOL_GPL(pwm_apply_might_sleep);
static void of_pwmchip_add(struct pwm_chip *chip) /**
* pwm_apply_atomic() - apply a new state to a PWM device from atomic context
* Not all PWM devices support this function, check with pwm_might_sleep().
* @pwm: PWM device
* @state: new state to apply
*/
int pwm_apply_atomic(struct pwm_device *pwm, const struct pwm_state *state)
{ {
if (!chip->dev || !chip->dev->of_node) WARN_ONCE(!pwm->chip->atomic,
return; "sleeping PWM driver used in atomic context\n");
if (!chip->of_xlate)
chip->of_xlate = of_pwm_xlate_with_flags;
of_node_get(chip->dev->of_node); return __pwm_apply(pwm, state);
} }
EXPORT_SYMBOL_GPL(pwm_apply_atomic);
static void of_pwmchip_remove(struct pwm_chip *chip) /**
* pwm_adjust_config() - adjust the current PWM config to the PWM arguments
* @pwm: PWM device
*
* This function will adjust the PWM config to the PWM arguments provided
* by the DT or PWM lookup table. This is particularly useful to adapt
* the bootloader config to the Linux one.
*/
int pwm_adjust_config(struct pwm_device *pwm)
{ {
if (chip->dev) struct pwm_state state;
of_node_put(chip->dev->of_node); struct pwm_args pargs;
}
static bool pwm_ops_check(const struct pwm_chip *chip) pwm_get_args(pwm, &pargs);
{ pwm_get_state(pwm, &state);
const struct pwm_ops *ops = chip->ops;
if (!ops->apply) /*
return false; * If the current period is zero it means that either the PWM driver
* does not support initial state retrieval or the PWM has not yet
* been configured.
*
* In either case, we setup the new period and polarity, and assign a
* duty cycle of 0.
*/
if (!state.period) {
state.duty_cycle = 0;
state.period = pargs.period;
state.polarity = pargs.polarity;
if (IS_ENABLED(CONFIG_PWM_DEBUG) && !ops->get_state) return pwm_apply_might_sleep(pwm, &state);
dev_warn(chip->dev, }
"Please implement the .get_state() callback\n");
return true; /*
* Adjust the PWM duty cycle/period based on the period value provided
* in PWM args.
*/
if (pargs.period != state.period) {
u64 dutycycle = (u64)state.duty_cycle * pargs.period;
do_div(dutycycle, state.period);
state.duty_cycle = dutycycle;
state.period = pargs.period;
}
/*
* If the polarity changed, we should also change the duty cycle.
*/
if (pargs.polarity != state.polarity) {
state.polarity = pargs.polarity;
state.duty_cycle = state.period - state.duty_cycle;
}
return pwm_apply_might_sleep(pwm, &state);
} }
EXPORT_SYMBOL_GPL(pwm_adjust_config);
/** /**
* __pwmchip_add() - register a new PWM chip * pwm_capture() - capture and report a PWM signal
* @chip: the PWM chip to add * @pwm: PWM device
* @owner: reference to the module providing the chip. * @result: structure to fill with capture result
* * @timeout: time to wait, in milliseconds, before giving up on capture
* 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. * Returns: 0 on success or a negative error code on failure.
*/ */
int __pwmchip_add(struct pwm_chip *chip, struct module *owner) int pwm_capture(struct pwm_device *pwm, struct pwm_capture *result,
unsigned long timeout)
{ {
unsigned int i; int err;
int ret;
if (!chip || !chip->dev || !chip->ops || !chip->npwm) if (!pwm || !pwm->chip->ops)
return -EINVAL; return -EINVAL;
if (!pwm_ops_check(chip)) if (!pwm->chip->ops->capture)
return -EINVAL; return -ENOSYS;
chip->owner = owner; mutex_lock(&pwm_lock);
err = pwm->chip->ops->capture(pwm->chip, pwm, result, timeout);
mutex_unlock(&pwm_lock);
chip->pwms = kcalloc(chip->npwm, sizeof(*chip->pwms), GFP_KERNEL); return err;
if (!chip->pwms) }
return -ENOMEM; EXPORT_SYMBOL_GPL(pwm_capture);
mutex_lock(&pwm_lock); static struct pwm_chip *pwmchip_find_by_name(const char *name)
{
struct pwm_chip *chip;
unsigned long id, tmp;
ret = idr_alloc(&pwm_chips, chip, 0, 0, GFP_KERNEL); if (!name)
if (ret < 0) { return NULL;
mutex_unlock(&pwm_lock);
kfree(chip->pwms);
return ret;
}
chip->id = ret; mutex_lock(&pwm_lock);
for (i = 0; i < chip->npwm; i++) { idr_for_each_entry_ul(&pwm_chips, chip, tmp, id) {
struct pwm_device *pwm = &chip->pwms[i]; const char *chip_name = dev_name(chip->dev);
pwm->chip = chip; if (chip_name && strcmp(chip_name, name) == 0) {
pwm->hwpwm = i; mutex_unlock(&pwm_lock);
return chip;
}
} }
mutex_unlock(&pwm_lock); mutex_unlock(&pwm_lock);
if (IS_ENABLED(CONFIG_OF)) return NULL;
of_pwmchip_add(chip);
pwmchip_sysfs_export(chip);
return 0;
} }
EXPORT_SYMBOL_GPL(__pwmchip_add);
/** static int pwm_device_request(struct pwm_device *pwm, const char *label)
* pwmchip_remove() - remove a PWM chip
* @chip: the PWM chip to remove
*
* Removes a PWM chip.
*/
void pwmchip_remove(struct pwm_chip *chip)
{ {
pwmchip_sysfs_unexport(chip); int err;
struct pwm_chip *chip = pwm->chip;
if (IS_ENABLED(CONFIG_OF)) const struct pwm_ops *ops = chip->ops;
of_pwmchip_remove(chip);
mutex_lock(&pwm_lock); if (test_bit(PWMF_REQUESTED, &pwm->flags))
return -EBUSY;
idr_remove(&pwm_chips, chip->id); if (!try_module_get(chip->owner))
return -ENODEV;
mutex_unlock(&pwm_lock); if (ops->request) {
err = ops->request(chip, pwm);
if (err) {
module_put(chip->owner);
return err;
}
}
kfree(chip->pwms); if (ops->get_state) {
} /*
EXPORT_SYMBOL_GPL(pwmchip_remove); * Zero-initialize state because most drivers are unaware of
* .usage_power. The other members of state are supposed to be
* set by lowlevel drivers. We still initialize the whole
* structure for simplicity even though this might paper over
* faulty implementations of .get_state().
*/
struct pwm_state state = { 0, };
static void devm_pwmchip_remove(void *data) err = ops->get_state(chip, pwm, &state);
{ trace_pwm_get(pwm, &state, err);
struct pwm_chip *chip = data;
pwmchip_remove(chip); if (!err)
} pwm->state = state;
int __devm_pwmchip_add(struct device *dev, struct pwm_chip *chip, struct module *owner) if (IS_ENABLED(CONFIG_PWM_DEBUG))
{ pwm->last = pwm->state;
int ret; }
ret = __pwmchip_add(chip, owner); set_bit(PWMF_REQUESTED, &pwm->flags);
if (ret) pwm->label = label;
return ret;
return devm_add_action_or_reset(dev, devm_pwmchip_remove, chip); return 0;
} }
EXPORT_SYMBOL_GPL(__devm_pwmchip_add);
/** /**
* pwm_request_from_chip() - request a PWM device relative to a PWM chip * pwm_request_from_chip() - request a PWM device relative to a PWM chip
...@@ -307,301 +409,179 @@ struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip, ...@@ -307,301 +409,179 @@ struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
} }
EXPORT_SYMBOL_GPL(pwm_request_from_chip); EXPORT_SYMBOL_GPL(pwm_request_from_chip);
static void pwm_apply_debug(struct pwm_device *pwm,
const struct pwm_state *state)
{
struct pwm_state *last = &pwm->last;
struct pwm_chip *chip = pwm->chip;
struct pwm_state s1 = { 0 }, s2 = { 0 };
int err;
if (!IS_ENABLED(CONFIG_PWM_DEBUG))
return;
/* No reasonable diagnosis possible without .get_state() */
if (!chip->ops->get_state)
return;
/*
* *state was just applied. Read out the hardware state and do some
* checks.
*/
err = chip->ops->get_state(chip, pwm, &s1);
trace_pwm_get(pwm, &s1, err);
if (err)
/* If that failed there isn't much to debug */
return;
/*
* The lowlevel driver either ignored .polarity (which is a bug) or as
* best effort inverted .polarity and fixed .duty_cycle respectively.
* Undo this inversion and fixup for further tests.
*/
if (s1.enabled && s1.polarity != state->polarity) {
s2.polarity = state->polarity;
s2.duty_cycle = s1.period - s1.duty_cycle;
s2.period = s1.period;
s2.enabled = s1.enabled;
} else {
s2 = s1;
}
if (s2.polarity != state->polarity &&
state->duty_cycle < state->period)
dev_warn(chip->dev, ".apply ignored .polarity\n");
if (state->enabled &&
last->polarity == state->polarity &&
last->period > s2.period &&
last->period <= state->period)
dev_warn(chip->dev,
".apply didn't pick the best available period (requested: %llu, applied: %llu, possible: %llu)\n",
state->period, s2.period, last->period);
if (state->enabled && state->period < s2.period)
dev_warn(chip->dev,
".apply is supposed to round down period (requested: %llu, applied: %llu)\n",
state->period, s2.period);
if (state->enabled &&
last->polarity == state->polarity &&
last->period == s2.period &&
last->duty_cycle > s2.duty_cycle &&
last->duty_cycle <= state->duty_cycle)
dev_warn(chip->dev,
".apply didn't pick the best available duty cycle (requested: %llu/%llu, applied: %llu/%llu, possible: %llu/%llu)\n",
state->duty_cycle, state->period,
s2.duty_cycle, s2.period,
last->duty_cycle, last->period);
if (state->enabled && state->duty_cycle < s2.duty_cycle)
dev_warn(chip->dev,
".apply is supposed to round down duty_cycle (requested: %llu/%llu, applied: %llu/%llu)\n",
state->duty_cycle, state->period,
s2.duty_cycle, s2.period);
if (!state->enabled && s2.enabled && s2.duty_cycle > 0)
dev_warn(chip->dev,
"requested disabled, but yielded enabled with duty > 0\n");
/* reapply the state that the driver reported being configured. */
err = chip->ops->apply(chip, pwm, &s1);
trace_pwm_apply(pwm, &s1, err);
if (err) {
*last = s1;
dev_err(chip->dev, "failed to reapply current setting\n");
return;
}
*last = (struct pwm_state){ 0 }; struct pwm_device *
err = chip->ops->get_state(chip, pwm, last); of_pwm_xlate_with_flags(struct pwm_chip *chip, const struct of_phandle_args *args)
trace_pwm_get(pwm, last, err); {
if (err) struct pwm_device *pwm;
return;
/* reapplication of the current state should give an exact match */ /* period in the second cell and flags in the third cell are optional */
if (s1.enabled != last->enabled || if (args->args_count < 1)
s1.polarity != last->polarity || return ERR_PTR(-EINVAL);
(s1.enabled && s1.period != last->period) ||
(s1.enabled && s1.duty_cycle != last->duty_cycle)) {
dev_err(chip->dev,
".apply is not idempotent (ena=%d pol=%d %llu/%llu) -> (ena=%d pol=%d %llu/%llu)\n",
s1.enabled, s1.polarity, s1.duty_cycle, s1.period,
last->enabled, last->polarity, last->duty_cycle,
last->period);
}
}
/** pwm = pwm_request_from_chip(chip, args->args[0], NULL);
* __pwm_apply() - atomically apply a new state to a PWM device if (IS_ERR(pwm))
* @pwm: PWM device return pwm;
* @state: new state to apply
*/
static int __pwm_apply(struct pwm_device *pwm, const struct pwm_state *state)
{
struct pwm_chip *chip;
int err;
if (!pwm || !state || !state->period || if (args->args_count > 1)
state->duty_cycle > state->period) pwm->args.period = args->args[1];
return -EINVAL;
chip = pwm->chip; pwm->args.polarity = PWM_POLARITY_NORMAL;
if (args->args_count > 2 && args->args[2] & PWM_POLARITY_INVERTED)
pwm->args.polarity = PWM_POLARITY_INVERSED;
if (state->period == pwm->state.period && return pwm;
state->duty_cycle == pwm->state.duty_cycle && }
state->polarity == pwm->state.polarity && EXPORT_SYMBOL_GPL(of_pwm_xlate_with_flags);
state->enabled == pwm->state.enabled &&
state->usage_power == pwm->state.usage_power)
return 0;
err = chip->ops->apply(chip, pwm, state); struct pwm_device *
trace_pwm_apply(pwm, state, err); of_pwm_single_xlate(struct pwm_chip *chip, const struct of_phandle_args *args)
if (err) {
return err; struct pwm_device *pwm;
pwm->state = *state; pwm = pwm_request_from_chip(chip, 0, NULL);
if (IS_ERR(pwm))
return pwm;
/* if (args->args_count > 1)
* only do this after pwm->state was applied as some pwm->args.period = args->args[0];
* implementations of .get_state depend on this
*/
pwm_apply_debug(pwm, state);
return 0; pwm->args.polarity = PWM_POLARITY_NORMAL;
if (args->args_count > 1 && args->args[1] & PWM_POLARITY_INVERTED)
pwm->args.polarity = PWM_POLARITY_INVERSED;
return pwm;
} }
EXPORT_SYMBOL_GPL(of_pwm_single_xlate);
/** static void of_pwmchip_add(struct pwm_chip *chip)
* pwm_apply_might_sleep() - atomically apply a new state to a PWM device
* Cannot be used in atomic context.
* @pwm: PWM device
* @state: new state to apply
*/
int pwm_apply_might_sleep(struct pwm_device *pwm, const struct pwm_state *state)
{ {
int err; if (!chip->dev || !chip->dev->of_node)
return;
/* if (!chip->of_xlate)
* Some lowlevel driver's implementations of .apply() make use of chip->of_xlate = of_pwm_xlate_with_flags;
* mutexes, also with some drivers only returning when the new
* configuration is active calling pwm_apply_might_sleep() from atomic context
* is a bad idea. So make it explicit that calling this function might
* sleep.
*/
might_sleep();
if (IS_ENABLED(CONFIG_PWM_DEBUG) && pwm->chip->atomic) { of_node_get(chip->dev->of_node);
/* }
* Catch any drivers that have been marked as atomic but
* that will sleep anyway.
*/
non_block_start();
err = __pwm_apply(pwm, state);
non_block_end();
} else {
err = __pwm_apply(pwm, state);
}
return err; static void of_pwmchip_remove(struct pwm_chip *chip)
{
if (chip->dev)
of_node_put(chip->dev->of_node);
} }
EXPORT_SYMBOL_GPL(pwm_apply_might_sleep);
/** static bool pwm_ops_check(const struct pwm_chip *chip)
* pwm_apply_atomic() - apply a new state to a PWM device from atomic context
* Not all PWM devices support this function, check with pwm_might_sleep().
* @pwm: PWM device
* @state: new state to apply
*/
int pwm_apply_atomic(struct pwm_device *pwm, const struct pwm_state *state)
{ {
WARN_ONCE(!pwm->chip->atomic, const struct pwm_ops *ops = chip->ops;
"sleeping PWM driver used in atomic context\n");
return __pwm_apply(pwm, state); if (!ops->apply)
return false;
if (IS_ENABLED(CONFIG_PWM_DEBUG) && !ops->get_state)
dev_warn(chip->dev,
"Please implement the .get_state() callback\n");
return true;
} }
EXPORT_SYMBOL_GPL(pwm_apply_atomic);
/** /**
* pwm_capture() - capture and report a PWM signal * __pwmchip_add() - register a new PWM chip
* @pwm: PWM device * @chip: the PWM chip to add
* @result: structure to fill with capture result * @owner: reference to the module providing the chip.
* @timeout: time to wait, in milliseconds, before giving up on capture *
* 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. * Returns: 0 on success or a negative error code on failure.
*/ */
int pwm_capture(struct pwm_device *pwm, struct pwm_capture *result, int __pwmchip_add(struct pwm_chip *chip, struct module *owner)
unsigned long timeout)
{ {
int err; unsigned int i;
int ret;
if (!pwm || !pwm->chip->ops) if (!chip || !chip->dev || !chip->ops || !chip->npwm)
return -EINVAL; return -EINVAL;
if (!pwm->chip->ops->capture) if (!pwm_ops_check(chip))
return -ENOSYS; return -EINVAL;
chip->owner = owner;
chip->pwms = kcalloc(chip->npwm, sizeof(*chip->pwms), GFP_KERNEL);
if (!chip->pwms)
return -ENOMEM;
mutex_lock(&pwm_lock); mutex_lock(&pwm_lock);
err = pwm->chip->ops->capture(pwm->chip, pwm, result, timeout);
ret = idr_alloc(&pwm_chips, chip, 0, 0, GFP_KERNEL);
if (ret < 0) {
mutex_unlock(&pwm_lock);
kfree(chip->pwms);
return ret;
}
chip->id = ret;
for (i = 0; i < chip->npwm; i++) {
struct pwm_device *pwm = &chip->pwms[i];
pwm->chip = chip;
pwm->hwpwm = i;
}
mutex_unlock(&pwm_lock); mutex_unlock(&pwm_lock);
return err; if (IS_ENABLED(CONFIG_OF))
of_pwmchip_add(chip);
pwmchip_sysfs_export(chip);
return 0;
} }
EXPORT_SYMBOL_GPL(pwm_capture); EXPORT_SYMBOL_GPL(__pwmchip_add);
/** /**
* pwm_adjust_config() - adjust the current PWM config to the PWM arguments * pwmchip_remove() - remove a PWM chip
* @pwm: PWM device * @chip: the PWM chip to remove
* *
* This function will adjust the PWM config to the PWM arguments provided * Removes a PWM chip.
* by the DT or PWM lookup table. This is particularly useful to adapt
* the bootloader config to the Linux one.
*/ */
int pwm_adjust_config(struct pwm_device *pwm) void pwmchip_remove(struct pwm_chip *chip)
{ {
struct pwm_state state; pwmchip_sysfs_unexport(chip);
struct pwm_args pargs;
pwm_get_args(pwm, &pargs);
pwm_get_state(pwm, &state);
/*
* If the current period is zero it means that either the PWM driver
* does not support initial state retrieval or the PWM has not yet
* been configured.
*
* In either case, we setup the new period and polarity, and assign a
* duty cycle of 0.
*/
if (!state.period) {
state.duty_cycle = 0;
state.period = pargs.period;
state.polarity = pargs.polarity;
return pwm_apply_might_sleep(pwm, &state); if (IS_ENABLED(CONFIG_OF))
} of_pwmchip_remove(chip);
/* mutex_lock(&pwm_lock);
* Adjust the PWM duty cycle/period based on the period value provided
* in PWM args.
*/
if (pargs.period != state.period) {
u64 dutycycle = (u64)state.duty_cycle * pargs.period;
do_div(dutycycle, state.period); idr_remove(&pwm_chips, chip->id);
state.duty_cycle = dutycycle;
state.period = pargs.period;
}
/* mutex_unlock(&pwm_lock);
* If the polarity changed, we should also change the duty cycle.
*/
if (pargs.polarity != state.polarity) {
state.polarity = pargs.polarity;
state.duty_cycle = state.period - state.duty_cycle;
}
return pwm_apply_might_sleep(pwm, &state); kfree(chip->pwms);
} }
EXPORT_SYMBOL_GPL(pwm_adjust_config); EXPORT_SYMBOL_GPL(pwmchip_remove);
static struct pwm_chip *fwnode_to_pwmchip(struct fwnode_handle *fwnode) static void devm_pwmchip_remove(void *data)
{ {
struct pwm_chip *chip; struct pwm_chip *chip = data;
unsigned long id, tmp;
mutex_lock(&pwm_lock); pwmchip_remove(chip);
}
idr_for_each_entry_ul(&pwm_chips, chip, tmp, id) int __devm_pwmchip_add(struct device *dev, struct pwm_chip *chip, struct module *owner)
if (chip->dev && device_match_fwnode(chip->dev, fwnode)) { {
mutex_unlock(&pwm_lock); int ret;
return chip;
}
mutex_unlock(&pwm_lock); ret = __pwmchip_add(chip, owner);
if (ret)
return ret;
return ERR_PTR(-EPROBE_DEFER); return devm_add_action_or_reset(dev, devm_pwmchip_remove, chip);
} }
EXPORT_SYMBOL_GPL(__devm_pwmchip_add);
static struct device_link *pwm_device_link_add(struct device *dev, static struct device_link *pwm_device_link_add(struct device *dev,
struct pwm_device *pwm) struct pwm_device *pwm)
...@@ -629,6 +609,24 @@ static struct device_link *pwm_device_link_add(struct device *dev, ...@@ -629,6 +609,24 @@ static struct device_link *pwm_device_link_add(struct device *dev,
return dl; return dl;
} }
static struct pwm_chip *fwnode_to_pwmchip(struct fwnode_handle *fwnode)
{
struct pwm_chip *chip;
unsigned long id, tmp;
mutex_lock(&pwm_lock);
idr_for_each_entry_ul(&pwm_chips, chip, tmp, id)
if (chip->dev && device_match_fwnode(chip->dev, fwnode)) {
mutex_unlock(&pwm_lock);
return chip;
}
mutex_unlock(&pwm_lock);
return ERR_PTR(-EPROBE_DEFER);
}
/** /**
* of_pwm_get() - request a PWM via the PWM framework * of_pwm_get() - request a PWM via the PWM framework
* @dev: device for PWM consumer * @dev: device for PWM consumer
...@@ -763,6 +761,9 @@ static struct pwm_device *acpi_pwm_get(const struct fwnode_handle *fwnode) ...@@ -763,6 +761,9 @@ static struct pwm_device *acpi_pwm_get(const struct fwnode_handle *fwnode)
return pwm; return pwm;
} }
static DEFINE_MUTEX(pwm_lookup_lock);
static LIST_HEAD(pwm_lookup_list);
/** /**
* pwm_add_table() - register PWM device consumers * pwm_add_table() - register PWM device consumers
* @table: array of consumers to register * @table: array of consumers to register
......
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