* [PATCH 0/6] pwm: New abstraction and userspace API
@ 2024-07-08 10:52 Uwe Kleine-König
2024-07-08 10:52 ` [PATCH 1/6] pwm: Add more locking Uwe Kleine-König
` (5 more replies)
0 siblings, 6 replies; 17+ messages in thread
From: Uwe Kleine-König @ 2024-07-08 10:52 UTC (permalink / raw)
To: linux-pwm
Cc: Steven Rostedt, Masami Hiramatsu, Mathieu Desnoyers,
linux-trace-kernel, Michael Hennerich, Nuno Sá,
Fabrice Gasnier, Maxime Coquelin, Alexandre Torgue, linux-stm32,
linux-arm-kernel
Hello,
this series implements a new abstraction to model the output waveform of
a PWM. The main improvement is that it defines a duty_offset instead of
a polarity and so allows to model more wave forms. The motivation for
this is that we need a PWM channel to have an offset compared to another
channel from the same chip, that is something like that:
__ __ __
/ \_______________/ \_______________/ \_________
^ __ ^ __ ^ __
____/ \_______________/ \_______________/ \_____
^ ^ ^
The kernel API for that is still missing (so it cannot be used yet from
the iio driver we intend to use it), but there is a userspace API that
makes use of it.
This is actually the 2nd series that implements a userspace API using a
chardev, the userspace lib from the last iteration is updated
accordingly. See
https://git.kernel.org/pub/scm/linux/kernel/git/ukleinek/libpwm.git
The series bases on top of
https://git.kernel.org/pub/scm/linux/kernel/git/ukleinek/linux.git pwm/for-next
plus the patch
pwm: Make info in traces about affected pwm more useful
available at
https://lore.kernel.org/linux-pwm/20240705211452.1157967-2-u.kleine-koenig@baylibre.com/
.
Unfortunately this requires a new set of callbacks for lowlevel drivers.
In this series axi-pwmgen and stm32 are converted accordingly.
The documentation situation is not optimal yet, and I expect that the
locking patch triggers a lockdep warning for the meson driver. This is
however a false positive and a problem that needs addressing in the clk
subsystem.
Looking forward to your feedback,
Uwe Kleine-König
Uwe Kleine-König (6):
pwm: Add more locking
pwm: New abstraction for PWM waveforms
pwm: Add support for pwmchip devices for faster and easier userspace
access
pwm: Add tracing for waveform callbacks
pwm: axi-pwmgen: Implementation of the waveform callbacks
pwm: stm32: Implementation of the waveform callbacks
drivers/pwm/core.c | 678 +++++++++++++++++++++++++++++++++--
drivers/pwm/pwm-axi-pwmgen.c | 148 +++++---
drivers/pwm/pwm-stm32.c | 605 +++++++++++++++++++------------
include/linux/pwm.h | 51 +++
include/trace/events/pwm.h | 134 ++++++-
include/uapi/linux/pwm.h | 24 ++
6 files changed, 1334 insertions(+), 306 deletions(-)
create mode 100644 include/uapi/linux/pwm.h
base-commit: 120a528213b6693214e3cbc24a9c3052a4b1024b
prerequisite-patch-id: 0e21153cd012f41ba9db52357fd08219af53e26c
--
2.43.0
^ permalink raw reply [flat|nested] 17+ messages in thread
* [PATCH 1/6] pwm: Add more locking
2024-07-08 10:52 [PATCH 0/6] pwm: New abstraction and userspace API Uwe Kleine-König
@ 2024-07-08 10:52 ` Uwe Kleine-König
2024-07-08 10:52 ` [PATCH 2/6] pwm: New abstraction for PWM waveforms Uwe Kleine-König
` (4 subsequent siblings)
5 siblings, 0 replies; 17+ messages in thread
From: Uwe Kleine-König @ 2024-07-08 10:52 UTC (permalink / raw)
To: linux-pwm
This ensures that a pwm_chip that has no corresponding driver isn't used
and that a driver doesn't go away while a callback is still running.
In the presence of device links this isn't necessary yet (so this is no
fix) but for pwm character device support this is needed.
To not serialize all pwm_apply_state() calls, this introduces a per chip
lock. An additional complication is that for atomic chips a mutex cannot
be used (as pwm_apply_atomic() must not sleep) and a spinlock cannot be
held while calling an operation for a sleeping chip. So depending on the
chip being atomic or not a spinlock or a mutex is used.
Signed-off-by: Uwe Kleine-König <u.kleine-koenig@baylibre.com>
---
drivers/pwm/core.c | 103 +++++++++++++++++++++++++++++++++++++++-----
include/linux/pwm.h | 13 ++++++
2 files changed, 105 insertions(+), 11 deletions(-)
diff --git a/drivers/pwm/core.c b/drivers/pwm/core.c
index 8acbcf5b6673..c31e12e76495 100644
--- a/drivers/pwm/core.c
+++ b/drivers/pwm/core.c
@@ -31,6 +31,24 @@ static DEFINE_MUTEX(pwm_lock);
static DEFINE_IDR(pwm_chips);
+static void pwmchip_lock(struct pwm_chip *chip)
+{
+ if (chip->atomic)
+ spin_lock(&chip->atomic_lock);
+ else
+ mutex_lock(&chip->nonatomic_lock);
+}
+
+static void pwmchip_unlock(struct pwm_chip *chip)
+{
+ if (chip->atomic)
+ spin_unlock(&chip->atomic_lock);
+ else
+ mutex_unlock(&chip->nonatomic_lock);
+}
+
+DEFINE_GUARD(pwmchip, struct pwm_chip *, pwmchip_lock(_T), pwmchip_unlock(_T))
+
static void pwm_apply_debug(struct pwm_device *pwm,
const struct pwm_state *state)
{
@@ -220,6 +238,7 @@ static int __pwm_apply(struct pwm_device *pwm, const struct pwm_state *state)
int pwm_apply_might_sleep(struct pwm_device *pwm, const struct pwm_state *state)
{
int err;
+ struct pwm_chip *chip = pwm->chip;
/*
* Some lowlevel driver's implementations of .apply() make use of
@@ -230,7 +249,12 @@ int pwm_apply_might_sleep(struct pwm_device *pwm, const struct pwm_state *state)
*/
might_sleep();
- if (IS_ENABLED(CONFIG_PWM_DEBUG) && pwm->chip->atomic) {
+ guard(pwmchip)(chip);
+
+ if (!chip->operational)
+ return -ENODEV;
+
+ if (IS_ENABLED(CONFIG_PWM_DEBUG) && chip->atomic) {
/*
* Catch any drivers that have been marked as atomic but
* that will sleep anyway.
@@ -254,9 +278,16 @@ EXPORT_SYMBOL_GPL(pwm_apply_might_sleep);
*/
int pwm_apply_atomic(struct pwm_device *pwm, const struct pwm_state *state)
{
- WARN_ONCE(!pwm->chip->atomic,
+ struct pwm_chip *chip = pwm->chip;
+
+ WARN_ONCE(!chip->atomic,
"sleeping PWM driver used in atomic context\n");
+ guard(pwmchip)(chip);
+
+ if (!chip->operational)
+ return -ENODEV;
+
return __pwm_apply(pwm, state);
}
EXPORT_SYMBOL_GPL(pwm_apply_atomic);
@@ -328,15 +359,22 @@ EXPORT_SYMBOL_GPL(pwm_adjust_config);
int pwm_capture(struct pwm_device *pwm, struct pwm_capture *result,
unsigned long timeout)
{
- if (!pwm || !pwm->chip->ops)
+ struct pwm_chip *chip;
+
+ if (!pwm || !(chip = pwm->chip)->ops)
return -EINVAL;
- if (!pwm->chip->ops->capture)
+ if (!chip->ops->capture)
return -ENOSYS;
guard(mutex)(&pwm_lock);
- return pwm->chip->ops->capture(pwm->chip, pwm, result, timeout);
+ guard(pwmchip)(chip);
+
+ if (!chip->operational)
+ return -ENODEV;
+
+ return chip->ops->capture(pwm->chip, pwm, result, timeout);
}
EXPORT_SYMBOL_GPL(pwm_capture);
@@ -369,6 +407,14 @@ static int pwm_device_request(struct pwm_device *pwm, const char *label)
if (test_bit(PWMF_REQUESTED, &pwm->flags))
return -EBUSY;
+ /*
+ * This function is called while holding pwm_lock. As .operational only
+ * changes while holding this lock, checking it here without holding the
+ * chip lock is fine.
+ */
+ if (!chip->operational)
+ return -ENODEV;
+
if (!try_module_get(chip->owner))
return -ENODEV;
@@ -397,7 +443,9 @@ static int pwm_device_request(struct pwm_device *pwm, const char *label)
*/
struct pwm_state state = { 0, };
- err = ops->get_state(chip, pwm, &state);
+ scoped_guard(pwmchip, chip)
+ err = ops->get_state(chip, pwm, &state);
+
trace_pwm_get(pwm, &state, err);
if (!err)
@@ -1021,6 +1069,7 @@ struct pwm_chip *pwmchip_alloc(struct device *parent, unsigned int npwm, size_t
chip->npwm = npwm;
chip->uses_pwmchip_alloc = true;
+ chip->operational = false;
pwmchip_dev = &chip->dev;
device_initialize(pwmchip_dev);
@@ -1126,6 +1175,11 @@ int __pwmchip_add(struct pwm_chip *chip, struct module *owner)
chip->owner = owner;
+ if (chip->atomic)
+ spin_lock_init(&chip->atomic_lock);
+ else
+ mutex_init(&chip->nonatomic_lock);
+
guard(mutex)(&pwm_lock);
ret = idr_alloc(&pwm_chips, chip, 0, 0, GFP_KERNEL);
@@ -1139,6 +1193,9 @@ int __pwmchip_add(struct pwm_chip *chip, struct module *owner)
if (IS_ENABLED(CONFIG_OF))
of_pwmchip_add(chip);
+ scoped_guard(pwmchip, chip)
+ chip->operational = true;
+
ret = device_add(&chip->dev);
if (ret)
goto err_device_add;
@@ -1146,6 +1203,9 @@ int __pwmchip_add(struct pwm_chip *chip, struct module *owner)
return 0;
err_device_add:
+ scoped_guard(pwmchip, chip)
+ chip->operational = false;
+
if (IS_ENABLED(CONFIG_OF))
of_pwmchip_remove(chip);
@@ -1165,11 +1225,27 @@ void pwmchip_remove(struct pwm_chip *chip)
{
pwmchip_sysfs_unexport(chip);
- if (IS_ENABLED(CONFIG_OF))
- of_pwmchip_remove(chip);
+ scoped_guard(mutex, &pwm_lock) {
+ unsigned int i;
+
+ scoped_guard(pwmchip, chip)
+ chip->operational = false;
+
+ for (i = 0; i < chip->npwm; ++i) {
+ struct pwm_device *pwm = &chip->pwms[i];
+
+ if (test_and_clear_bit(PWMF_REQUESTED, &pwm->flags)) {
+ dev_alert(&chip->dev, "Freeing requested PWM #%u\n", i);
+ if (pwm->chip->ops->free)
+ pwm->chip->ops->free(pwm->chip, pwm);
+ }
+ }
+
+ if (IS_ENABLED(CONFIG_OF))
+ of_pwmchip_remove(chip);
- scoped_guard(mutex, &pwm_lock)
idr_remove(&pwm_chips, chip->id);
+ }
device_del(&chip->dev);
}
@@ -1539,12 +1615,17 @@ void pwm_put(struct pwm_device *pwm)
guard(mutex)(&pwm_lock);
- if (!test_and_clear_bit(PWMF_REQUESTED, &pwm->flags)) {
+ /*
+ * If the chip isn't operational, PWMF_REQUESTED was already cleared. So
+ * don't warn in this case. This can only happen if a consumer called
+ * pwm_put() twice.
+ */
+ if (chip->operational && !test_and_clear_bit(PWMF_REQUESTED, &pwm->flags)) {
pr_warn("PWM device already freed\n");
return;
}
- if (chip->ops->free)
+ if (chip->operational && chip->ops->free)
pwm->chip->ops->free(pwm->chip, pwm);
pwm->label = NULL;
diff --git a/include/linux/pwm.h b/include/linux/pwm.h
index f8c2dc12dbd3..5176dfebfbfd 100644
--- a/include/linux/pwm.h
+++ b/include/linux/pwm.h
@@ -275,6 +275,9 @@ struct pwm_ops {
* @of_xlate: request a PWM device given a device tree PWM specifier
* @atomic: can the driver's ->apply() be called in atomic context
* @uses_pwmchip_alloc: signals if pwmchip_allow was used to allocate this chip
+ * @operational: signals if the chip can be used (or is already deregistered)
+ * @nonatomic_lock: mutex for nonatomic chips
+ * @atomic_lock: mutex for atomic chips
* @pwms: array of PWM devices allocated by the framework
*/
struct pwm_chip {
@@ -290,6 +293,16 @@ struct pwm_chip {
/* only used internally by the PWM framework */
bool uses_pwmchip_alloc;
+ bool operational;
+ union {
+ /*
+ * depending on the chip being atomic or not either the mutex or
+ * the spinlock is used. It protects .operational and
+ * synchronizes calls to the .ops->apply and .ops->get_state()
+ */
+ struct mutex nonatomic_lock;
+ struct spinlock atomic_lock;
+ };
struct pwm_device pwms[] __counted_by(npwm);
};
--
2.43.0
^ permalink raw reply related [flat|nested] 17+ messages in thread
* [PATCH 2/6] pwm: New abstraction for PWM waveforms
2024-07-08 10:52 [PATCH 0/6] pwm: New abstraction and userspace API Uwe Kleine-König
2024-07-08 10:52 ` [PATCH 1/6] pwm: Add more locking Uwe Kleine-König
@ 2024-07-08 10:52 ` Uwe Kleine-König
2024-07-08 18:12 ` Trevor Gamblin
2024-07-08 10:52 ` [PATCH 3/6] pwm: Add support for pwmchip devices for faster and easier userspace access Uwe Kleine-König
` (3 subsequent siblings)
5 siblings, 1 reply; 17+ messages in thread
From: Uwe Kleine-König @ 2024-07-08 10:52 UTC (permalink / raw)
To: linux-pwm
Cc: Michael Hennerich, Nuno Sá, Fabrice Gasnier, Maxime Coquelin,
Alexandre Torgue, linux-stm32, linux-arm-kernel
Up to now the configuration of a PWM setting is decribed exclusively by
a struct pwm_state which contains information about period, duty_cycle,
polarity and if the PWM is enabled. (There is another member usage_power
which doesn't completely fit into pwm_state, I ignore it here for
simplicity.)
Instead of a polarity the new abstraction has a member duty_offset that
defines when the rising edge happens after the period start. This is
more general, as with a pwm_state the rising edge can only happen at the
period's start or such that the falling edge is at the end of the period
(i.e. duty_offset == 0 or duty_offset == period_lengh - duty_length).
A disabled PWM is modeled by .period_length = 0. In my eyes this is a
nice usage of that otherwise unusable setting, as it doesn't define
anything about the future which matches the fact that consumers should
consider the state of the output as undefined and it's just there to say
"No further requirements about the output, you can save some power.".
Further I renamed period and duty_cycle to period_length and
duty_length. In the past there was confusion from time to time about
duty_cycle being measured in nanoseconds because people expected a
percentage of period instead. With "length" as suffix the semantic
should be more obvious to people unfamiliar with the pwm subsystem.
period is renamed period_length for consistency.
The API for consumers doesn't change yet, but lowlevel drivers can
implement callbacks that work with pwm_waveforms instead of pwm_states.
A new thing about these callbacks is that the calculation of hardware
settings needed to implement a certain waveform is separated from
actually writing these settings. The motivation for that is that this
allows a consumer to query the hardware capabilities without actually
modifying the hardware state.
The rounding rules that are expected to be implemented in the
round_waveform_tohw() are: First pick the biggest possible period not
bigger than wf->period_length. For that period pick the biggest possible
duty setting not bigger than wf->duty_length. Third pick the biggest
possible offset not bigger than wf->duty_offset. If the requested period
is too small for the hardware, it's expected that a setting with the
minimal period and duty_length = duty_offset = 0 is returned and this
fact is signaled by a return value of 1.
Signed-off-by: Uwe Kleine-König <u.kleine-koenig@baylibre.com>
---
drivers/pwm/core.c | 194 +++++++++++++++++++++++++++++++++++++++-----
include/linux/pwm.h | 35 ++++++++
2 files changed, 208 insertions(+), 21 deletions(-)
diff --git a/drivers/pwm/core.c b/drivers/pwm/core.c
index c31e12e76495..8e68481a7b33 100644
--- a/drivers/pwm/core.c
+++ b/drivers/pwm/core.c
@@ -49,6 +49,72 @@ static void pwmchip_unlock(struct pwm_chip *chip)
DEFINE_GUARD(pwmchip, struct pwm_chip *, pwmchip_lock(_T), pwmchip_unlock(_T))
+static void pwm_wf2state(const struct pwm_waveform *wf, struct pwm_state *state)
+{
+ if (wf->period_length) {
+ if (wf->duty_length + wf->duty_offset < wf->period_length)
+ *state = (struct pwm_state){
+ .enabled = true,
+ .polarity = PWM_POLARITY_NORMAL,
+ .period = wf->period_length,
+ .duty_cycle = wf->duty_length,
+ };
+ else
+ *state = (struct pwm_state){
+ .enabled = true,
+ .polarity = PWM_POLARITY_INVERSED,
+ .period = wf->period_length,
+ .duty_cycle = wf->period_length - wf->duty_length,
+ };
+ } else {
+ *state = (struct pwm_state){
+ .enabled = false,
+ };
+ }
+}
+
+static void pwm_state2wf(const struct pwm_state *state, struct pwm_waveform *wf)
+{
+ if (state->enabled) {
+ if (state->polarity == PWM_POLARITY_NORMAL)
+ *wf = (struct pwm_waveform){
+ .period_length = state->period,
+ .duty_length = state->duty_cycle,
+ .duty_offset = 0,
+ };
+ else
+ *wf = (struct pwm_waveform){
+ .period_length = state->period,
+ .duty_length = state->period - state->duty_cycle,
+ .duty_offset = state->duty_cycle,
+ };
+ } else {
+ *wf = (struct pwm_waveform){
+ .period_length = 0,
+ };
+ }
+}
+
+static int pwm_check_rounding(const struct pwm_waveform *wf,
+ const struct pwm_waveform *wf_rounded)
+{
+ if (!wf->period_length)
+ return 0;
+
+ if (wf->period_length < wf_rounded->period_length)
+ return 1;
+
+ if (wf->duty_length < wf_rounded->duty_length)
+ return 1;
+
+ if (wf->duty_offset < wf_rounded->duty_offset)
+ return 1;
+
+ return 0;
+}
+
+#define WFHWSIZE 20
+
static void pwm_apply_debug(struct pwm_device *pwm,
const struct pwm_state *state)
{
@@ -182,6 +248,7 @@ static bool pwm_state_valid(const struct pwm_state *state)
static int __pwm_apply(struct pwm_device *pwm, const struct pwm_state *state)
{
struct pwm_chip *chip;
+ const struct pwm_ops *ops;
int err;
if (!pwm || !state)
@@ -205,6 +272,7 @@ static int __pwm_apply(struct pwm_device *pwm, const struct pwm_state *state)
}
chip = pwm->chip;
+ ops = chip->ops;
if (state->period == pwm->state.period &&
state->duty_cycle == pwm->state.duty_cycle &&
@@ -213,18 +281,59 @@ static int __pwm_apply(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 (err)
- return err;
+ if (ops->write_waveform) {
+ struct pwm_waveform wf;
+ char wfhw[WFHWSIZE];
- pwm->state = *state;
+ BUG_ON(WFHWSIZE < ops->sizeof_wfhw);
- /*
- * only do this after pwm->state was applied as some
- * implementations of .get_state depend on this
- */
- pwm_apply_debug(pwm, state);
+ pwm_state2wf(state, &wf);
+
+ /*
+ * XXX The rounding is wrong here for states with inverted
+ * polarity. While .apply() rounds down duty_cycle (which
+ * represents the time from the start of the period to the inner
+ * edge), .round_waveform_tohw() rounds down the time the PWM is
+ * high.
+ */
+
+ err = ops->round_waveform_tohw(chip, pwm, &wf, &wfhw);
+ if (err)
+ return err;
+
+ if (IS_ENABLED(PWM_DEBUG)) {
+ struct pwm_waveform wf_rounded;
+
+ err = ops->round_waveform_fromhw(chip, pwm, &wfhw, &wf_rounded);
+ if (err)
+ return err;
+
+ if (pwm_check_rounding(&wf, &wf_rounded))
+ dev_err(&chip->dev, "Wrong rounding: requested %llu/%llu [+%llu], result %llu/%llu [+%llu]\n",
+ wf.duty_length, wf.period_length, wf.duty_offset,
+ wf_rounded.duty_length, wf_rounded.period_length, wf_rounded.duty_offset);
+ }
+
+ err = ops->write_waveform(chip, pwm, &wfhw);
+ if (err)
+ return err;
+
+ pwm->state = *state;
+
+ } else {
+ err = ops->apply(chip, pwm, state);
+ trace_pwm_apply(pwm, state, err);
+ if (err)
+ return err;
+
+ 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;
}
@@ -292,6 +401,41 @@ int pwm_apply_atomic(struct pwm_device *pwm, const struct pwm_state *state)
}
EXPORT_SYMBOL_GPL(pwm_apply_atomic);
+static int pwm_get_state_hw(struct pwm_device *pwm, struct pwm_state *state)
+{
+ struct pwm_chip *chip = pwm->chip;
+ const struct pwm_ops *ops = chip->ops;
+ int ret = -EOPNOTSUPP;
+
+ if (ops->read_waveform) {
+ char wfhw[WFHWSIZE];
+ struct pwm_waveform wf;
+
+ BUG_ON(WFHWSIZE < ops->sizeof_wfhw);
+
+ scoped_guard(pwmchip, chip) {
+
+ ret = ops->read_waveform(chip, pwm, &wfhw);
+ if (ret)
+ return ret;
+
+ ret = ops->round_waveform_fromhw(chip, pwm, &wfhw, &wf);
+ if (ret)
+ return ret;
+ }
+
+ pwm_wf2state(&wf, state);
+
+ } else if (ops->get_state) {
+ scoped_guard(pwmchip, chip)
+ ret = ops->get_state(chip, pwm, state);
+
+ trace_pwm_get(pwm, state, ret);
+ }
+
+ return ret;
+}
+
/**
* pwm_adjust_config() - adjust the current PWM config to the PWM arguments
* @pwm: PWM device
@@ -433,7 +577,7 @@ static int pwm_device_request(struct pwm_device *pwm, const char *label)
}
}
- if (ops->get_state) {
+ if (ops->read_waveform || ops->get_state) {
/*
* Zero-initialize state because most drivers are unaware of
* .usage_power. The other members of state are supposed to be
@@ -443,11 +587,7 @@ static int pwm_device_request(struct pwm_device *pwm, const char *label)
*/
struct pwm_state state = { 0, };
- scoped_guard(pwmchip, chip)
- err = ops->get_state(chip, pwm, &state);
-
- trace_pwm_get(pwm, &state, err);
-
+ err = pwm_get_state_hw(pwm, &state);
if (!err)
pwm->state = state;
@@ -1134,12 +1274,24 @@ static bool pwm_ops_check(const struct pwm_chip *chip)
{
const struct pwm_ops *ops = chip->ops;
- if (!ops->apply)
- return false;
+ if (ops->write_waveform) {
+ if (!ops->round_waveform_tohw ||
+ !ops->round_waveform_fromhw ||
+ !ops->write_waveform)
+ return false;
- if (IS_ENABLED(CONFIG_PWM_DEBUG) && !ops->get_state)
- dev_warn(pwmchip_parent(chip),
- "Please implement the .get_state() callback\n");
+ if (WFHWSIZE < ops->sizeof_wfhw) {
+ dev_warn(pwmchip_parent(chip), "WFHWSIZE < %zu\n", ops->sizeof_wfhw);
+ return false;
+ }
+ } else {
+ if (!ops->apply)
+ return false;
+
+ if (IS_ENABLED(CONFIG_PWM_DEBUG) && !ops->get_state)
+ dev_warn(pwmchip_parent(chip),
+ "Please implement the .get_state() callback\n");
+ }
return true;
}
diff --git a/include/linux/pwm.h b/include/linux/pwm.h
index 5176dfebfbfd..b5dff2a99038 100644
--- a/include/linux/pwm.h
+++ b/include/linux/pwm.h
@@ -49,6 +49,30 @@ enum {
PWMF_EXPORTED = 1,
};
+/*
+ * struct pwm_waveform - description of a PWM waveform
+ * @period_length: PWM period
+ * @duty_length: PWM duty cycle
+ * @duty_offset: offset of the rising edge from the period's start
+ *
+ * This is a representation of a PWM waveform alternative to struct pwm_state
+ * below. It's more expressive than struct pwm_state as it contains a
+ * duty_offset and so can represent offsets other than $period - $duty_cycle
+ * which is done using .polarity = PWM_POLARITY_INVERSED. Note there is no
+ * explicit bool for enabled. A "disabled" PWM is represented by .period = 0.
+ *
+ * Note that the behaviour of a "disabled" PWM is undefined. Depending on the
+ * hardware's capabilities it might drive the active or inactive level, go
+ * high-z or even continue to toggle.
+ *
+ * The unit for all three members is nanoseconds.
+ */
+struct pwm_waveform {
+ u64 period_length;
+ u64 duty_length;
+ u64 duty_offset;
+};
+
/*
* struct pwm_state - state of a PWM channel
* @period: PWM period (in nanoseconds)
@@ -259,6 +283,17 @@ struct pwm_ops {
void (*free)(struct pwm_chip *chip, struct pwm_device *pwm);
int (*capture)(struct pwm_chip *chip, struct pwm_device *pwm,
struct pwm_capture *result, unsigned long timeout);
+
+ size_t sizeof_wfhw;
+ int (*round_waveform_tohw)(struct pwm_chip *chip, struct pwm_device *pwm,
+ const struct pwm_waveform *wf, void *wfhw);
+ int (*round_waveform_fromhw)(struct pwm_chip *chip, struct pwm_device *pwm,
+ const void *wfhw, struct pwm_waveform *wf);
+ int (*read_waveform)(struct pwm_chip *chip, struct pwm_device *pwm,
+ void *wfhw);
+ int (*write_waveform)(struct pwm_chip *chip, struct pwm_device *pwm,
+ const void *wfhw);
+
int (*apply)(struct pwm_chip *chip, struct pwm_device *pwm,
const struct pwm_state *state);
int (*get_state)(struct pwm_chip *chip, struct pwm_device *pwm,
--
2.43.0
^ permalink raw reply related [flat|nested] 17+ messages in thread
* [PATCH 3/6] pwm: Add support for pwmchip devices for faster and easier userspace access
2024-07-08 10:52 [PATCH 0/6] pwm: New abstraction and userspace API Uwe Kleine-König
2024-07-08 10:52 ` [PATCH 1/6] pwm: Add more locking Uwe Kleine-König
2024-07-08 10:52 ` [PATCH 2/6] pwm: New abstraction for PWM waveforms Uwe Kleine-König
@ 2024-07-08 10:52 ` Uwe Kleine-König
2024-07-08 18:13 ` Trevor Gamblin
2024-07-09 9:37 ` Nuno Sá
2024-07-08 10:52 ` [PATCH 4/6] pwm: Add tracing for waveform callbacks Uwe Kleine-König
` (2 subsequent siblings)
5 siblings, 2 replies; 17+ messages in thread
From: Uwe Kleine-König @ 2024-07-08 10:52 UTC (permalink / raw)
To: linux-pwm
With this change each pwmchip can be accessed from userspace via a
character device. Compared to the sysfs-API this is faster (on a
stm32mp157 applying a new configuration takes approx 25% only) and
allows to pass the whole configuration in a single ioctl allowing atomic
application.
Thanks to Randy Dunlap for pointing out a missing kernel-doc
description.
Signed-off-by: Uwe Kleine-König <u.kleine-koenig@baylibre.com>
---
drivers/pwm/core.c | 367 +++++++++++++++++++++++++++++++++++++--
include/linux/pwm.h | 3 +
include/uapi/linux/pwm.h | 24 +++
3 files changed, 379 insertions(+), 15 deletions(-)
create mode 100644 include/uapi/linux/pwm.h
diff --git a/drivers/pwm/core.c b/drivers/pwm/core.c
index 8e68481a7b33..d64c033c4cb2 100644
--- a/drivers/pwm/core.c
+++ b/drivers/pwm/core.c
@@ -23,6 +23,8 @@
#include <dt-bindings/pwm/pwm.h>
+#include <uapi/linux/pwm.h>
+
#define CREATE_TRACE_POINTS
#include <trace/events/pwm.h>
@@ -95,6 +97,29 @@ static void pwm_state2wf(const struct pwm_state *state, struct pwm_waveform *wf)
}
}
+static int pwmwfcmp(const struct pwm_waveform *a, const struct pwm_waveform *b)
+{
+ if (a->period_length > b->period_length)
+ return 1;
+
+ if (a->period_length < b->period_length)
+ return -1;
+
+ if (a->duty_length > b->duty_length)
+ return 1;
+
+ if (a->duty_length < b->duty_length)
+ return -1;
+
+ if (a->duty_offset > b->duty_offset)
+ return 1;
+
+ if (a->duty_offset < b->duty_offset)
+ return -1;
+
+ return 0;
+}
+
static int pwm_check_rounding(const struct pwm_waveform *wf,
const struct pwm_waveform *wf_rounded)
{
@@ -115,6 +140,127 @@ static int pwm_check_rounding(const struct pwm_waveform *wf,
#define WFHWSIZE 20
+static int pwm_get_waveform(struct pwm_device *pwm,
+ struct pwm_waveform *wf)
+{
+ struct pwm_chip *chip = pwm->chip;
+ const struct pwm_ops *ops = chip->ops;
+ char wfhw[WFHWSIZE];
+ int err;
+
+ BUG_ON(WFHWSIZE < ops->sizeof_wfhw);
+
+ guard(pwmchip)(chip);
+
+ if (!chip->operational)
+ return -ENODEV;
+
+ err = ops->read_waveform(chip, pwm, &wfhw);
+ if (err)
+ return err;
+
+ return ops->round_waveform_fromhw(chip, pwm, &wfhw, wf);
+}
+
+/* Called with the pwmchip lock held */
+static int __pwm_set_waveform(struct pwm_device *pwm,
+ const struct pwm_waveform *wf,
+ bool exact)
+{
+ struct pwm_chip *chip = pwm->chip;
+ const struct pwm_ops *ops = chip->ops;
+ char wfhw[WFHWSIZE];
+ struct pwm_waveform wf_rounded;
+ int err;
+
+ BUG_ON(WFHWSIZE < ops->sizeof_wfhw);
+
+ if (wf->period_length &&
+ (wf->duty_length > wf->period_length ||
+ wf->duty_offset >= wf->period_length))
+ return -EINVAL;
+
+ err = ops->round_waveform_tohw(chip, pwm, wf, &wfhw);
+ if (err)
+ return err;
+
+ if ((IS_ENABLED(CONFIG_PWM_DEBUG) || exact) && wf->period_length) {
+ err = ops->round_waveform_fromhw(chip, pwm, &wfhw, &wf_rounded);
+ if (err)
+ return err;
+
+ if (IS_ENABLED(CONFIG_PWM_DEBUG) && pwm_check_rounding(wf, &wf_rounded))
+ dev_err(&chip->dev, "Wrong rounding: requested %llu/%llu [+%llu], result %llu/%llu [+%llu]\n",
+ wf->duty_length, wf->period_length, wf->duty_offset,
+ wf_rounded.duty_length, wf_rounded.period_length, wf_rounded.duty_offset);
+
+ if (exact && pwmwfcmp(wf, &wf_rounded)) {
+ dev_dbg(&chip->dev, "Requested no rounding, but %llu/%llu [+%llu] -> %llu/%llu [+%llu]\n",
+ wf->duty_length, wf->period_length, wf->duty_offset,
+ wf_rounded.duty_length, wf_rounded.period_length, wf_rounded.duty_offset);
+
+ return 1;
+ }
+ }
+
+ err = ops->write_waveform(chip, pwm, &wfhw);
+ if (err)
+ return err;
+
+ /* update .state */
+ pwm_wf2state(wf, &pwm->state);
+
+ if (IS_ENABLED(CONFIG_PWM_DEBUG) && ops->read_waveform && wf->period_length) {
+ struct pwm_waveform wf_set;
+
+ err = ops->read_waveform(chip, pwm, &wfhw);
+ if (err)
+ /* maybe ignore? */
+ return err;
+
+ err = ops->round_waveform_fromhw(chip, pwm, &wfhw, &wf_set);
+ if (err)
+ /* maybe ignore? */
+ return err;
+
+ if (pwmwfcmp(&wf_set, &wf_rounded) != 0)
+ dev_err(&chip->dev,
+ "Unexpected setting: requested %llu/%llu [+%llu], expected %llu/%llu [+%llu], set %llu/%llu [+%llu]\n",
+ wf->duty_length, wf->period_length, wf->duty_offset,
+ wf_rounded.duty_length, wf_rounded.period_length, wf_rounded.duty_offset,
+ wf_set.duty_length, wf_set.period_length, wf_set.duty_offset);
+ }
+ return 0;
+}
+
+static int pwm_set_waveform_might_sleep(struct pwm_device *pwm,
+ struct pwm_waveform *wf, bool exact)
+{
+ struct pwm_chip *chip = pwm->chip;
+ int err;
+
+ might_sleep();
+
+ guard(pwmchip)(chip);
+
+ if (!chip->operational)
+ return -ENODEV;
+
+ if (IS_ENABLED(CONFIG_PWM_DEBUG) && chip->atomic) {
+ /*
+ * Catch any drivers that have been marked as atomic but
+ * that will sleep anyway.
+ */
+ non_block_start();
+ err = __pwm_set_waveform(pwm, wf, exact);
+ non_block_end();
+ } else {
+ err = __pwm_set_waveform(pwm, wf, exact);
+ }
+
+ return err;
+}
+
static void pwm_apply_debug(struct pwm_device *pwm,
const struct pwm_state *state)
{
@@ -301,19 +447,6 @@ static int __pwm_apply(struct pwm_device *pwm, const struct pwm_state *state)
if (err)
return err;
- if (IS_ENABLED(PWM_DEBUG)) {
- struct pwm_waveform wf_rounded;
-
- err = ops->round_waveform_fromhw(chip, pwm, &wfhw, &wf_rounded);
- if (err)
- return err;
-
- if (pwm_check_rounding(&wf, &wf_rounded))
- dev_err(&chip->dev, "Wrong rounding: requested %llu/%llu [+%llu], result %llu/%llu [+%llu]\n",
- wf.duty_length, wf.period_length, wf.duty_offset,
- wf_rounded.duty_length, wf_rounded.period_length, wf_rounded.duty_offset);
- }
-
err = ops->write_waveform(chip, pwm, &wfhw);
if (err)
return err;
@@ -1296,6 +1429,197 @@ static bool pwm_ops_check(const struct pwm_chip *chip)
return true;
}
+struct pwm_cdev_data {
+ struct pwm_chip *chip;
+ struct pwm_device *pwm[];
+};
+
+static int pwm_cdev_open(struct inode *inode, struct file *file)
+{
+ struct pwm_chip *chip = container_of(inode->i_cdev, struct pwm_chip, cdev);
+ struct pwm_cdev_data *cdata;
+
+ guard(mutex)(&pwm_lock);
+
+ if (!chip->operational)
+ return -ENXIO;
+
+ cdata = kzalloc(struct_size(cdata, pwm, chip->npwm), GFP_KERNEL);
+ if (!cdata)
+ return -ENOMEM;
+
+ cdata->chip = chip;
+
+ file->private_data = cdata;
+
+ return nonseekable_open(inode, file);
+}
+
+static int pwm_cdev_release(struct inode *inode, struct file *file)
+{
+ struct pwm_cdev_data *cdata = file->private_data;
+ unsigned int i;
+
+ for (i = 0; i < cdata->chip->npwm; ++i) {
+ if (cdata->pwm[i])
+ pwm_put(cdata->pwm[i]);
+ }
+ kfree(cdata);
+
+ return 0;
+}
+
+static int pwm_cdev_request(struct pwm_cdev_data *cdata, unsigned int hwpwm)
+{
+ struct pwm_chip *chip = cdata->chip;
+
+ if (hwpwm >= chip->npwm)
+ return -EINVAL;
+
+ if (!cdata->pwm[hwpwm]) {
+ struct pwm_device *pwm = &chip->pwms[hwpwm];
+ int ret;
+
+ ret = pwm_device_request(pwm, "pwm-cdev");
+ if (ret < 0)
+ return ret;
+
+ cdata->pwm[hwpwm] = pwm;
+ }
+
+ return 0;
+}
+
+static int pwm_cdev_free(struct pwm_cdev_data *cdata, unsigned int hwpwm)
+{
+ struct pwm_chip *chip = cdata->chip;
+
+ if (hwpwm >= chip->npwm)
+ return -EINVAL;
+
+ if (cdata->pwm[hwpwm]) {
+ struct pwm_device *pwm = cdata->pwm[hwpwm];
+
+ pwm_put(pwm);
+
+ cdata->pwm[hwpwm] = NULL;
+ }
+
+ return 0;
+}
+
+static long pwm_cdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
+{
+ int ret = 0;
+ struct pwm_cdev_data *cdata = file->private_data;
+ struct pwm_chip *chip = cdata->chip;
+
+ guard(mutex)(&pwm_lock);
+
+ if (!chip->operational)
+ return -ENODEV;
+
+ switch (cmd) {
+ case PWM_IOCTL_GET_NUM_PWMS:
+ return chip->npwm;
+
+ case PWM_IOCTL_REQUEST:
+ {
+ unsigned int hwpwm;
+
+ ret = get_user(hwpwm, (unsigned int __user *)arg);
+ if (ret)
+ return ret;
+
+ return pwm_cdev_request(cdata, hwpwm);
+ }
+
+ case PWM_IOCTL_FREE:
+ {
+ unsigned int hwpwm;
+
+ ret = get_user(hwpwm, (unsigned int __user *)arg);
+ if (ret)
+ return ret;
+
+ return pwm_cdev_free(cdata, hwpwm);
+ }
+
+ case PWM_IOCTL_GETWF:
+ {
+ struct pwmchip_waveform cwf;
+ struct pwm_waveform wf;
+ struct pwm_device *pwm;
+
+ ret = copy_from_user(&cwf, (struct pwmchip_waveform __user *)arg, sizeof(cwf));
+ if (ret)
+ return -EFAULT;
+
+ ret = pwm_cdev_request(cdata, cwf.hwpwm);
+ if (ret)
+ return ret;
+
+ pwm = cdata->pwm[cwf.hwpwm];
+
+ ret = pwm_get_waveform(pwm, &wf);
+ if (ret)
+ return ret;
+
+ cwf.period_length = wf.period_length;
+ cwf.duty_length = wf.duty_length;
+ cwf.duty_offset = wf.duty_offset;
+
+ return copy_to_user((struct pwmchip_waveform __user *)arg, &cwf, sizeof(cwf));
+ }
+ break;
+
+ case PWM_IOCTL_SETROUNDEDWF:
+ case PWM_IOCTL_SETEXACTWF:
+ {
+ struct pwmchip_waveform cwf;
+ struct pwm_waveform wf;
+ struct pwm_device *pwm;
+
+ ret = copy_from_user(&cwf, (struct pwmchip_waveform __user *)arg, sizeof(cwf));
+ if (ret)
+ return -EFAULT;
+
+ if (cwf.period_length > 0 &&
+ (cwf.duty_length > cwf.period_length ||
+ cwf.duty_offset >= cwf.period_length))
+ return -EINVAL;
+
+ ret = pwm_cdev_request(cdata, cwf.hwpwm);
+ if (ret)
+ return ret;
+
+ pwm = cdata->pwm[cwf.hwpwm];
+
+ wf = (struct pwm_waveform){
+ .period_length = cwf.period_length,
+ .duty_length = cwf.duty_length,
+ .duty_offset = cwf.duty_offset,
+ };
+
+ return pwm_set_waveform_might_sleep(pwm, &wf, cmd == PWM_IOCTL_SETEXACTWF);
+ }
+ break;
+
+ default:
+ return -ENOTTY;
+ }
+}
+
+static const struct file_operations pwm_cdev_fileops = {
+ .open = pwm_cdev_open,
+ .release = pwm_cdev_release,
+ .owner = THIS_MODULE,
+ .llseek = no_llseek,
+ .unlocked_ioctl = pwm_cdev_ioctl,
+};
+
+static dev_t pwm_devt;
+
/**
* __pwmchip_add() - register a new PWM chip
* @chip: the PWM chip to add
@@ -1348,7 +1672,13 @@ int __pwmchip_add(struct pwm_chip *chip, struct module *owner)
scoped_guard(pwmchip, chip)
chip->operational = true;
- ret = device_add(&chip->dev);
+ if (chip->id < 256 && chip->ops->write_waveform)
+ chip->dev.devt = MKDEV(MAJOR(pwm_devt), chip->id);
+
+ cdev_init(&chip->cdev, &pwm_cdev_fileops);
+ chip->cdev.owner = owner;
+
+ ret = cdev_device_add(&chip->cdev, &chip->dev);
if (ret)
goto err_device_add;
@@ -1399,7 +1729,7 @@ void pwmchip_remove(struct pwm_chip *chip)
idr_remove(&pwm_chips, chip->id);
}
- device_del(&chip->dev);
+ cdev_device_del(&chip->cdev, &chip->dev);
}
EXPORT_SYMBOL_GPL(pwmchip_remove);
@@ -1943,9 +2273,16 @@ static int __init pwm_init(void)
{
int ret;
+ ret = alloc_chrdev_region(&pwm_devt, 0, 256, "pwm");
+ if (ret) {
+ pr_warn("Failed to initialize chrdev region for PWM usage\n");
+ return ret;
+ }
+
ret = class_register(&pwm_class);
if (ret) {
pr_err("Failed to initialize PWM class (%pe)\n", ERR_PTR(ret));
+ unregister_chrdev_region(pwm_devt, 256);
return ret;
}
diff --git a/include/linux/pwm.h b/include/linux/pwm.h
index b5dff2a99038..3e503a28f5f7 100644
--- a/include/linux/pwm.h
+++ b/include/linux/pwm.h
@@ -2,6 +2,7 @@
#ifndef __LINUX_PWM_H
#define __LINUX_PWM_H
+#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/err.h>
#include <linux/module.h>
@@ -303,6 +304,7 @@ struct pwm_ops {
/**
* struct pwm_chip - abstract a PWM controller
* @dev: device providing the PWMs
+ * @cdev: &struct cdev for this device
* @ops: callbacks for this PWM controller
* @owner: module providing this chip
* @id: unique number of this PWM chip
@@ -317,6 +319,7 @@ struct pwm_ops {
*/
struct pwm_chip {
struct device dev;
+ struct cdev cdev;
const struct pwm_ops *ops;
struct module *owner;
unsigned int id;
diff --git a/include/uapi/linux/pwm.h b/include/uapi/linux/pwm.h
new file mode 100644
index 000000000000..1ecf2e033b62
--- /dev/null
+++ b/include/uapi/linux/pwm.h
@@ -0,0 +1,24 @@
+/* SPDX-License-Identifier: GPL-2.0-only WITH Linux-syscall-note */
+
+#ifndef _UAPI_PWM_H_
+#define _UAPI_PWM_H_
+
+#include <linux/ioctl.h>
+#include <linux/types.h>
+
+struct pwmchip_waveform {
+ unsigned int hwpwm;
+ __u64 period_length;
+ __u64 duty_length;
+ __u64 duty_offset;
+};
+
+#define PWM_IOCTL_GET_NUM_PWMS _IO(0x75, 0)
+#define PWM_IOCTL_REQUEST _IOW(0x75, 1, unsigned int)
+#define PWM_IOCTL_FREE _IOW(0x75, 2, unsigned int)
+#define PWM_IOCTL_ROUNDWF _IOWR(0x75, 3, struct pwmchip_waveform)
+#define PWM_IOCTL_GETWF _IOWR(0x75, 4, struct pwmchip_waveform)
+#define PWM_IOCTL_SETROUNDEDWF _IOW(0x75, 5, struct pwmchip_waveform)
+#define PWM_IOCTL_SETEXACTWF _IOW(0x75, 6, struct pwmchip_waveform)
+
+#endif /* _UAPI_PWM_H_ */
--
2.43.0
^ permalink raw reply related [flat|nested] 17+ messages in thread
* [PATCH 4/6] pwm: Add tracing for waveform callbacks
2024-07-08 10:52 [PATCH 0/6] pwm: New abstraction and userspace API Uwe Kleine-König
` (2 preceding siblings ...)
2024-07-08 10:52 ` [PATCH 3/6] pwm: Add support for pwmchip devices for faster and easier userspace access Uwe Kleine-König
@ 2024-07-08 10:52 ` Uwe Kleine-König
2024-07-08 18:14 ` Trevor Gamblin
2024-07-08 10:52 ` [PATCH 5/6] pwm: axi-pwmgen: Implementation of the " Uwe Kleine-König
2024-07-08 10:52 ` [PATCH 6/6] pwm: stm32: " Uwe Kleine-König
5 siblings, 1 reply; 17+ messages in thread
From: Uwe Kleine-König @ 2024-07-08 10:52 UTC (permalink / raw)
To: linux-pwm
Cc: Steven Rostedt, Masami Hiramatsu, Mathieu Desnoyers,
linux-trace-kernel
---
drivers/pwm/core.c | 68 ++++++++++++++++---
include/trace/events/pwm.h | 134 ++++++++++++++++++++++++++++++++++---
2 files changed, 183 insertions(+), 19 deletions(-)
diff --git a/drivers/pwm/core.c b/drivers/pwm/core.c
index d64c033c4cb2..a2320ae77220 100644
--- a/drivers/pwm/core.c
+++ b/drivers/pwm/core.c
@@ -138,6 +138,52 @@ static int pwm_check_rounding(const struct pwm_waveform *wf,
return 0;
}
+static int pwm_round_waveform_tohw(struct pwm_chip *chip, struct pwm_device *pwm,
+ const struct pwm_waveform *wf, void *wfhw)
+{
+ const struct pwm_ops *ops = chip->ops;
+ int ret;
+
+ ret = ops->round_waveform_tohw(chip, pwm, wf, wfhw);
+ trace_pwm_round_waveform_tohw(pwm, wf, wfhw, ret);
+
+ return ret;
+}
+
+static int pwm_round_waveform_fromhw(struct pwm_chip *chip, struct pwm_device *pwm,
+ const void *wfhw, struct pwm_waveform *wf)
+{
+ const struct pwm_ops *ops = chip->ops;
+ int ret;
+
+ ret = ops->round_waveform_fromhw(chip, pwm, wfhw, wf);
+ trace_pwm_round_waveform_fromhw(pwm, wfhw, wf, ret);
+
+ return ret;
+}
+
+static int pwm_read_waveform(struct pwm_chip *chip, struct pwm_device *pwm, void *wfhw)
+{
+ const struct pwm_ops *ops = chip->ops;
+ int ret;
+
+ ret = ops->read_waveform(chip, pwm, wfhw);
+ trace_pwm_read_waveform(pwm, wfhw, ret);
+
+ return ret;
+}
+
+static int pwm_write_waveform(struct pwm_chip *chip, struct pwm_device *pwm, const void *wfhw)
+{
+ const struct pwm_ops *ops = chip->ops;
+ int ret;
+
+ ret = ops->write_waveform(chip, pwm, wfhw);
+ trace_pwm_write_waveform(pwm, wfhw, ret);
+
+ return ret;
+}
+
#define WFHWSIZE 20
static int pwm_get_waveform(struct pwm_device *pwm,
@@ -155,11 +201,11 @@ static int pwm_get_waveform(struct pwm_device *pwm,
if (!chip->operational)
return -ENODEV;
- err = ops->read_waveform(chip, pwm, &wfhw);
+ err = pwm_read_waveform(chip, pwm, &wfhw);
if (err)
return err;
- return ops->round_waveform_fromhw(chip, pwm, &wfhw, wf);
+ return pwm_round_waveform_fromhw(chip, pwm, &wfhw, wf);
}
/* Called with the pwmchip lock held */
@@ -180,12 +226,12 @@ static int __pwm_set_waveform(struct pwm_device *pwm,
wf->duty_offset >= wf->period_length))
return -EINVAL;
- err = ops->round_waveform_tohw(chip, pwm, wf, &wfhw);
+ err = pwm_round_waveform_tohw(chip, pwm, wf, &wfhw);
if (err)
return err;
if ((IS_ENABLED(CONFIG_PWM_DEBUG) || exact) && wf->period_length) {
- err = ops->round_waveform_fromhw(chip, pwm, &wfhw, &wf_rounded);
+ err = pwm_round_waveform_fromhw(chip, pwm, &wfhw, &wf_rounded);
if (err)
return err;
@@ -203,7 +249,7 @@ static int __pwm_set_waveform(struct pwm_device *pwm,
}
}
- err = ops->write_waveform(chip, pwm, &wfhw);
+ err = pwm_write_waveform(chip, pwm, &wfhw);
if (err)
return err;
@@ -213,12 +259,12 @@ static int __pwm_set_waveform(struct pwm_device *pwm,
if (IS_ENABLED(CONFIG_PWM_DEBUG) && ops->read_waveform && wf->period_length) {
struct pwm_waveform wf_set;
- err = ops->read_waveform(chip, pwm, &wfhw);
+ err = pwm_read_waveform(chip, pwm, &wfhw);
if (err)
/* maybe ignore? */
return err;
- err = ops->round_waveform_fromhw(chip, pwm, &wfhw, &wf_set);
+ err = pwm_round_waveform_fromhw(chip, pwm, &wfhw, &wf_set);
if (err)
/* maybe ignore? */
return err;
@@ -443,11 +489,11 @@ static int __pwm_apply(struct pwm_device *pwm, const struct pwm_state *state)
* high.
*/
- err = ops->round_waveform_tohw(chip, pwm, &wf, &wfhw);
+ err = pwm_round_waveform_tohw(chip, pwm, &wf, &wfhw);
if (err)
return err;
- err = ops->write_waveform(chip, pwm, &wfhw);
+ err = pwm_write_waveform(chip, pwm, &wfhw);
if (err)
return err;
@@ -548,11 +594,11 @@ static int pwm_get_state_hw(struct pwm_device *pwm, struct pwm_state *state)
scoped_guard(pwmchip, chip) {
- ret = ops->read_waveform(chip, pwm, &wfhw);
+ ret = pwm_read_waveform(chip, pwm, &wfhw);
if (ret)
return ret;
- ret = ops->round_waveform_fromhw(chip, pwm, &wfhw, &wf);
+ ret = pwm_round_waveform_fromhw(chip, pwm, &wfhw, &wf);
if (ret)
return ret;
}
diff --git a/include/trace/events/pwm.h b/include/trace/events/pwm.h
index 8022701c446d..3b5b20d2aff0 100644
--- a/include/trace/events/pwm.h
+++ b/include/trace/events/pwm.h
@@ -8,15 +8,135 @@
#include <linux/pwm.h>
#include <linux/tracepoint.h>
+#define TP_PROTO_pwm(args...) \
+ TP_PROTO(struct pwm_device *pwm, args)
+
+#define TP_ARGS_pwm(args...) \
+ TP_ARGS(pwm, args)
+
+#define TP_STRUCT__entry_pwm(args...) \
+ TP_STRUCT__entry( \
+ __field(unsigned int, chipid) \
+ __field(unsigned int, hwpwm) \
+ args)
+
+#define TP_fast_assign_pwm(args...) \
+ TP_fast_assign( \
+ __entry->chipid = pwm->chip->id; \
+ __entry->hwpwm = pwm->hwpwm; \
+ args)
+
+#define TP_printk_pwm(fmt, args...) \
+ TP_printk("pwmchip%u.%u: " fmt, __entry->chipid, __entry->hwpwm, args)
+
+#define __field_pwmwf(wf) \
+ __field(u64, wf ## _period_length) \
+ __field(u64, wf ## _duty_length) \
+ __field(u64, wf ## _duty_offset) \
+
+#define fast_assign_pwmwf(wf) \
+ __entry->wf ## _period_length = wf->period_length; \
+ __entry->wf ## _duty_length = wf->duty_length; \
+ __entry->wf ## _duty_offset = wf->duty_offset
+
+#define printk_pwmwf_format(wf) \
+ "%lld/%lld [+%lld]"
+
+#define printk_pwmwf_formatargs(wf) \
+ __entry->wf ## _duty_length, __entry->wf ## _period_length, __entry->wf ## _duty_offset
+
+TRACE_EVENT(pwm_round_waveform_tohw,
+
+ TP_PROTO_pwm(const struct pwm_waveform *wf, void *wfhw, int err),
+
+ TP_ARGS_pwm(wf, wfhw, err),
+
+ TP_STRUCT__entry_pwm(
+ __field_pwmwf(wf)
+ __field(void *, wfhw)
+ __field(int, err)
+ ),
+
+ TP_fast_assign_pwm(
+ fast_assign_pwmwf(wf);
+ __entry->wfhw = wfhw;
+ __entry->err = err;
+ ),
+
+ TP_printk_pwm(printk_pwmwf_format(wf) " > %p err=%d",
+ printk_pwmwf_formatargs(wf), __entry->wfhw, __entry->err)
+);
+
+TRACE_EVENT(pwm_round_waveform_fromhw,
+
+ TP_PROTO_pwm(const void *wfhw, struct pwm_waveform *wf, int err),
+
+ TP_ARGS_pwm(wfhw, wf, err),
+
+ TP_STRUCT__entry_pwm(
+ __field(const void *, wfhw)
+ __field_pwmwf(wf)
+ __field(int, err)
+ ),
+
+ TP_fast_assign_pwm(
+ __entry->wfhw = wfhw;
+ fast_assign_pwmwf(wf);
+ __entry->err = err;
+ ),
+
+ TP_printk_pwm("%p > " printk_pwmwf_format(wf) " err=%d",
+ __entry->wfhw, printk_pwmwf_formatargs(wf), __entry->err)
+);
+
+TRACE_EVENT(pwm_read_waveform,
+
+ TP_PROTO_pwm(void *wfhw, int err),
+
+ TP_ARGS_pwm(wfhw, err),
+
+ TP_STRUCT__entry_pwm(
+ __field(void *, wfhw)
+ __field(int, err)
+ ),
+
+ TP_fast_assign_pwm(
+ __entry->wfhw = wfhw;
+ __entry->err = err;
+ ),
+
+ TP_printk_pwm("%p err=%d",
+ __entry->wfhw, __entry->err)
+);
+
+TRACE_EVENT(pwm_write_waveform,
+
+ TP_PROTO_pwm(const void *wfhw, int err),
+
+ TP_ARGS_pwm(wfhw, err),
+
+ TP_STRUCT__entry_pwm(
+ __field(const void *, wfhw)
+ __field(int, err)
+ ),
+
+ TP_fast_assign_pwm(
+ __entry->wfhw = wfhw;
+ __entry->err = err;
+ ),
+
+ TP_printk_pwm("%p err=%d",
+ __entry->wfhw, __entry->err)
+);
+
+
DECLARE_EVENT_CLASS(pwm,
TP_PROTO(struct pwm_device *pwm, const struct pwm_state *state, int err),
TP_ARGS(pwm, state, err),
- TP_STRUCT__entry(
- __field(unsigned int, chipid)
- __field(unsigned int, hwpwm)
+ TP_STRUCT__entry_pwm(
__field(u64, period)
__field(u64, duty_cycle)
__field(enum pwm_polarity, polarity)
@@ -24,9 +144,7 @@ DECLARE_EVENT_CLASS(pwm,
__field(int, err)
),
- TP_fast_assign(
- __entry->chipid = pwm->chip->id;
- __entry->hwpwm = pwm->hwpwm;
+ TP_fast_assign_pwm(
__entry->period = state->period;
__entry->duty_cycle = state->duty_cycle;
__entry->polarity = state->polarity;
@@ -34,8 +152,8 @@ DECLARE_EVENT_CLASS(pwm,
__entry->err = err;
),
- TP_printk("pwmchip%u.%u: period=%llu duty_cycle=%llu polarity=%d enabled=%d err=%d",
- __entry->chipid, __entry->hwpwm, __entry->period, __entry->duty_cycle,
+ TP_printk_pwm("period=%llu duty_cycle=%llu polarity=%d enabled=%d err=%d",
+ __entry->period, __entry->duty_cycle,
__entry->polarity, __entry->enabled, __entry->err)
);
--
2.43.0
^ permalink raw reply related [flat|nested] 17+ messages in thread
* [PATCH 5/6] pwm: axi-pwmgen: Implementation of the waveform callbacks
2024-07-08 10:52 [PATCH 0/6] pwm: New abstraction and userspace API Uwe Kleine-König
` (3 preceding siblings ...)
2024-07-08 10:52 ` [PATCH 4/6] pwm: Add tracing for waveform callbacks Uwe Kleine-König
@ 2024-07-08 10:52 ` Uwe Kleine-König
2024-07-08 18:08 ` Trevor Gamblin
2024-07-08 10:52 ` [PATCH 6/6] pwm: stm32: " Uwe Kleine-König
5 siblings, 1 reply; 17+ messages in thread
From: Uwe Kleine-König @ 2024-07-08 10:52 UTC (permalink / raw)
To: linux-pwm; +Cc: Michael Hennerich, Nuno Sá
Convert the axi-pwmgen driver to use the new callbacks for hardware
programming.
Signed-off-by: Uwe Kleine-König <u.kleine-koenig@baylibre.com>
---
drivers/pwm/pwm-axi-pwmgen.c | 148 ++++++++++++++++++++++++-----------
1 file changed, 102 insertions(+), 46 deletions(-)
diff --git a/drivers/pwm/pwm-axi-pwmgen.c b/drivers/pwm/pwm-axi-pwmgen.c
index aac4f395497b..757ae037d5d6 100644
--- a/drivers/pwm/pwm-axi-pwmgen.c
+++ b/drivers/pwm/pwm-axi-pwmgen.c
@@ -23,6 +23,7 @@
#include <linux/err.h>
#include <linux/fpga/adi-axi-common.h>
#include <linux/io.h>
+#include <linux/minmax.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/pwm.h>
@@ -53,81 +54,136 @@ static const struct regmap_config axi_pwmgen_regmap_config = {
.val_bits = 32,
};
-static int axi_pwmgen_apply(struct pwm_chip *chip, struct pwm_device *pwm,
- const struct pwm_state *state)
+/* This represents a hardware configuration for one channel */
+struct axi_pwmgen_waveform {
+ u32 period_cnt;
+ u32 duty_cycle_cnt;
+ u32 duty_offset_cnt;
+};
+
+static int axi_pwmgen_round_waveform_tohw(struct pwm_chip *chip,
+ struct pwm_device *pwm,
+ const struct pwm_waveform *wf,
+ void *_wfhw)
{
+ struct axi_pwmgen_waveform *wfhw = _wfhw;
+ struct axi_pwmgen_ddata *ddata = pwmchip_get_drvdata(chip);
+
+ if (wf->period_length == 0) {
+ *wfhw = (struct axi_pwmgen_waveform){
+ .period_cnt = 0,
+ .duty_cycle_cnt = 0,
+ .duty_offset_cnt = 0,
+ };
+ } else {
+ /* With ddata->clk_rate_hz < NSEC_PER_SEC this won't overflow. */
+ wfhw->period_cnt = min_t(u64, mul_u64_u32_div(wf->period_length, ddata->clk_rate_hz, NSEC_PER_SEC), U32_MAX);
+
+ if (wfhw->period_cnt == 0) {
+ /*
+ * The specified period is too short for the hardware.
+ * Let's round .duty_cycle down to 0 to get a (somewhat)
+ * valid result.
+ */
+ wfhw->period_cnt = 1;
+ wfhw->duty_cycle_cnt = 0;
+ wfhw->duty_offset_cnt = 0;
+ } else {
+ wfhw->duty_cycle_cnt = min_t(u64, mul_u64_u32_div(wf->duty_length, ddata->clk_rate_hz, NSEC_PER_SEC), U32_MAX);
+ wfhw->duty_offset_cnt = min_t(u64, mul_u64_u32_div(wf->duty_offset, ddata->clk_rate_hz, NSEC_PER_SEC), U32_MAX);
+ }
+ }
+
+ dev_dbg(&chip->dev, "pwm#%u: %lld/%lld [+%lld] @%lu -> PERIOD: %08x, DUTY: %08x, OFFSET: %08x\n",
+ pwm->hwpwm, wf->duty_length, wf->period_length, wf->duty_offset,
+ ddata->clk_rate_hz, wfhw->period_cnt, wfhw->duty_cycle_cnt, wfhw->duty_offset_cnt);
+
+ return 0;
+}
+
+static int axi_pwmgen_round_waveform_fromhw(struct pwm_chip *chip, struct pwm_device *pwm,
+ const void *_wfhw, struct pwm_waveform *wf)
+{
+ const struct axi_pwmgen_waveform *wfhw = _wfhw;
+ struct axi_pwmgen_ddata *ddata = pwmchip_get_drvdata(chip);
+
+ wf->period_length = DIV64_U64_ROUND_UP((u64)wfhw->period_cnt * NSEC_PER_SEC,
+ ddata->clk_rate_hz);
+
+ wf->duty_length = DIV64_U64_ROUND_UP((u64)wfhw->duty_cycle_cnt * NSEC_PER_SEC,
+ ddata->clk_rate_hz);
+
+ wf->duty_offset = DIV64_U64_ROUND_UP((u64)wfhw->duty_offset_cnt * NSEC_PER_SEC,
+ ddata->clk_rate_hz);
+
+ return 0;
+}
+
+static int axi_pwmgen_write_waveform(struct pwm_chip *chip,
+ struct pwm_device *pwm,
+ const void *_wfhw)
+{
+ const struct axi_pwmgen_waveform *wfhw = _wfhw;
struct axi_pwmgen_ddata *ddata = pwmchip_get_drvdata(chip);
- unsigned int ch = pwm->hwpwm;
struct regmap *regmap = ddata->regmap;
- u64 period_cnt, duty_cnt;
+ unsigned int ch = pwm->hwpwm;
int ret;
- if (state->polarity != PWM_POLARITY_NORMAL)
- return -EINVAL;
+ ret = regmap_write(regmap, AXI_PWMGEN_CHX_PERIOD(ch), wfhw->period_cnt);
+ if (ret)
+ return ret;
- if (state->enabled) {
- period_cnt = mul_u64_u64_div_u64(state->period, ddata->clk_rate_hz, NSEC_PER_SEC);
- if (period_cnt > UINT_MAX)
- period_cnt = UINT_MAX;
+ ret = regmap_write(regmap, AXI_PWMGEN_CHX_DUTY(ch), wfhw->duty_cycle_cnt);
+ if (ret)
+ return ret;
- if (period_cnt == 0)
- return -EINVAL;
-
- ret = regmap_write(regmap, AXI_PWMGEN_CHX_PERIOD(ch), period_cnt);
- if (ret)
- return ret;
-
- duty_cnt = mul_u64_u64_div_u64(state->duty_cycle, ddata->clk_rate_hz, NSEC_PER_SEC);
- if (duty_cnt > UINT_MAX)
- duty_cnt = UINT_MAX;
-
- ret = regmap_write(regmap, AXI_PWMGEN_CHX_DUTY(ch), duty_cnt);
- if (ret)
- return ret;
- } else {
- ret = regmap_write(regmap, AXI_PWMGEN_CHX_PERIOD(ch), 0);
- if (ret)
- return ret;
-
- ret = regmap_write(regmap, AXI_PWMGEN_CHX_DUTY(ch), 0);
- if (ret)
- return ret;
- }
+ ret = regmap_write(regmap, AXI_PWMGEN_CHX_OFFSET(ch), wfhw->duty_offset_cnt);
+ if (ret)
+ return ret;
return regmap_write(regmap, AXI_PWMGEN_REG_CONFIG, AXI_PWMGEN_LOAD_CONFIG);
}
-static int axi_pwmgen_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
- struct pwm_state *state)
+static int axi_pwmgen_read_waveform(struct pwm_chip *chip,
+ struct pwm_device *pwm,
+ void *_wfhw)
{
+ struct axi_pwmgen_waveform *wfhw = _wfhw;
struct axi_pwmgen_ddata *ddata = pwmchip_get_drvdata(chip);
struct regmap *regmap = ddata->regmap;
unsigned int ch = pwm->hwpwm;
- u32 cnt;
int ret;
- ret = regmap_read(regmap, AXI_PWMGEN_CHX_PERIOD(ch), &cnt);
+ ret = regmap_read(regmap, AXI_PWMGEN_CHX_PERIOD(ch), &wfhw->period_cnt);
if (ret)
return ret;
- state->enabled = cnt != 0;
-
- state->period = DIV_ROUND_UP_ULL((u64)cnt * NSEC_PER_SEC, ddata->clk_rate_hz);
-
- ret = regmap_read(regmap, AXI_PWMGEN_CHX_DUTY(ch), &cnt);
+ ret = regmap_read(regmap, AXI_PWMGEN_CHX_DUTY(ch), &wfhw->duty_cycle_cnt);
if (ret)
return ret;
- state->duty_cycle = DIV_ROUND_UP_ULL((u64)cnt * NSEC_PER_SEC, ddata->clk_rate_hz);
+ ret = regmap_read(regmap, AXI_PWMGEN_CHX_OFFSET(ch), &wfhw->duty_offset_cnt);
+ if (ret)
+ return ret;
- state->polarity = PWM_POLARITY_NORMAL;
+ if (wfhw->duty_cycle_cnt > wfhw->period_cnt)
+ wfhw->duty_cycle_cnt = wfhw->period_cnt;
+
+ /* XXX: is this the actual behaviour of the hardware? */
+ if (wfhw->duty_offset_cnt >= wfhw->period_cnt) {
+ wfhw->duty_cycle_cnt = 0;
+ wfhw->duty_offset_cnt = 0;
+ }
return 0;
}
static const struct pwm_ops axi_pwmgen_pwm_ops = {
- .apply = axi_pwmgen_apply,
- .get_state = axi_pwmgen_get_state,
+ .sizeof_wfhw = sizeof(struct axi_pwmgen_waveform),
+ .round_waveform_tohw = axi_pwmgen_round_waveform_tohw,
+ .round_waveform_fromhw = axi_pwmgen_round_waveform_fromhw,
+ .read_waveform = axi_pwmgen_read_waveform,
+ .write_waveform = axi_pwmgen_write_waveform,
};
static int axi_pwmgen_setup(struct regmap *regmap, struct device *dev)
--
2.43.0
^ permalink raw reply related [flat|nested] 17+ messages in thread
* [PATCH 6/6] pwm: stm32: Implementation of the waveform callbacks
2024-07-08 10:52 [PATCH 0/6] pwm: New abstraction and userspace API Uwe Kleine-König
` (4 preceding siblings ...)
2024-07-08 10:52 ` [PATCH 5/6] pwm: axi-pwmgen: Implementation of the " Uwe Kleine-König
@ 2024-07-08 10:52 ` Uwe Kleine-König
5 siblings, 0 replies; 17+ messages in thread
From: Uwe Kleine-König @ 2024-07-08 10:52 UTC (permalink / raw)
To: linux-pwm
Cc: Fabrice Gasnier, Maxime Coquelin, Alexandre Torgue, linux-stm32,
linux-arm-kernel
Convert the stm32 pwm driver to use the new callbacks for hardware
programming.
Signed-off-by: Uwe Kleine-König <u.kleine-koenig@baylibre.com>
---
drivers/pwm/pwm-stm32.c | 605 +++++++++++++++++++++++++---------------
1 file changed, 384 insertions(+), 221 deletions(-)
diff --git a/drivers/pwm/pwm-stm32.c b/drivers/pwm/pwm-stm32.c
index fd754a99cf2e..4dedfabce63b 100644
--- a/drivers/pwm/pwm-stm32.c
+++ b/drivers/pwm/pwm-stm32.c
@@ -51,6 +51,384 @@ static u32 active_channels(struct stm32_pwm *dev)
return ccer & TIM_CCER_CCXE;
}
+struct stm32_pwm_waveform {
+ u32 ccer;
+ u32 psc;
+ u32 arr;
+ u32 ccr;
+};
+
+static int stm32_pwm_round_waveform_tohw(struct pwm_chip *chip,
+ struct pwm_device *pwm,
+ const struct pwm_waveform *wf,
+ void *_wfhw)
+{
+ struct stm32_pwm_waveform *wfhw = _wfhw;
+ struct stm32_pwm *priv = to_stm32_pwm_dev(chip);
+ unsigned int ch = pwm->hwpwm;
+ unsigned long rate;
+ u64 ccr, duty;
+ int ret;
+
+ if (wf->period_length == 0) {
+ *wfhw = (struct stm32_pwm_waveform){
+ .ccer = 0,
+ };
+
+ return 0;
+ }
+
+ ret = clk_enable(priv->clk);
+ if (ret)
+ return ret;
+
+ wfhw->ccer = TIM_CCER_CCxE(ch + 1);
+ if (priv->have_complementary_output)
+ wfhw->ccer = TIM_CCER_CCxNE(ch);
+
+ rate = clk_get_rate(priv->clk);
+
+ if (active_channels(priv) & ~(1 << ch * 4)) {
+ u64 arr;
+
+ /*
+ * Other channels are already enabled, so the configured PSC and
+ * ARR must be used for this channel, too.
+ */
+ ret = regmap_read(priv->regmap, TIM_PSC, &wfhw->psc);
+ if (ret)
+ goto out;
+
+ ret = regmap_read(priv->regmap, TIM_ARR, &wfhw->arr);
+ if (ret)
+ goto out;
+
+ /*
+ * calculate the best value for ARR for the given PSC, refuse if
+ * the resulting period gets bigger than the requested one.
+ */
+ arr = mul_u64_u64_div_u64(wf->period_length, rate,
+ (u64)NSEC_PER_SEC * (wfhw->psc + 1));
+ if (arr <= wfhw->arr) {
+ /*
+ * requested period is small than the currently
+ * configured and unchangable period, report back the smallest
+ * possible period, i.e. the current state; Initialize
+ * ccr to anything valid.
+ */
+ wfhw->ccr = 0;
+ ret = 1;
+ goto out;
+ }
+
+ } else {
+ /*
+ * .probe() asserted that clk_get_rate() is not bigger than 1 GHz, so
+ * the calculations here won't overflow.
+ * First we need to find the minimal value for prescaler such that
+ *
+ * period_ns * clkrate
+ * ------------------------------ < max_arr + 1
+ * NSEC_PER_SEC * (prescaler + 1)
+ *
+ * This equation is equivalent to
+ *
+ * period_ns * clkrate
+ * ---------------------------- < prescaler + 1
+ * NSEC_PER_SEC * (max_arr + 1)
+ *
+ * Using integer division and knowing that the right hand side is
+ * integer, this is further equivalent to
+ *
+ * (period_ns * clkrate) // (NSEC_PER_SEC * (max_arr + 1)) ≤ prescaler
+ */
+ u64 psc = mul_u64_u64_div_u64(wf->period_length, rate,
+ (u64)NSEC_PER_SEC * ((u64)priv->max_arr + 1));
+ u64 arr;
+
+ wfhw->psc = min_t(u64, psc, MAX_TIM_PSC);
+
+ arr = mul_u64_u64_div_u64(wf->period_length, rate,
+ (u64)NSEC_PER_SEC * (wfhw->psc + 1));
+ if (!arr) {
+ /*
+ * requested period is too small, report back the smallest
+ * possible period, i.e. ARR = 0. The only valid CCR
+ * value is then zero, too.
+ */
+ wfhw->arr = 0;
+ wfhw->ccr = 0;
+ ret = 1;
+ goto out;
+ }
+
+ /*
+ * ARR is limited intentionally to values less than
+ * priv->max_arr to allow 100% duty cycle.
+ */
+ wfhw->arr = min_t(u64, arr, priv->max_arr) - 1;
+ }
+
+ duty = mul_u64_u64_div_u64(wf->duty_length, rate,
+ (u64)NSEC_PER_SEC * (wfhw->psc + 1));
+ duty = min_t(u64, duty, wfhw->arr + 1);
+
+ if (wf->duty_length && wf->duty_offset &&
+ wf->duty_length + wf->duty_offset >= wf->period_length) {
+ wfhw->ccer |= TIM_CCER_CCxP(ch + 1);
+ if (priv->have_complementary_output)
+ wfhw->ccer |= TIM_CCER_CCxNP(ch + 1);
+
+ ccr = wfhw->arr + 1 - duty;
+ } else {
+ ccr = duty;
+ }
+
+ wfhw->ccr = min_t(u64, ccr, wfhw->arr + 1);
+
+ dev_dbg(&chip->dev, "pwm#%u: %lld/%lld [+%lld] @%lu -> CCER: %08x, PSC: %08x, ARR: %08x, CCR: %08x\n",
+ pwm->hwpwm, wf->duty_length, wf->period_length, wf->duty_offset,
+ rate, wfhw->ccer, wfhw->psc, wfhw->arr, wfhw->ccr);
+
+out:
+ clk_disable(priv->clk);
+
+ return ret;
+}
+
+/*
+ * This should be moved to lib/math/div64.c. Currently there are some changes
+ * pending to mul_u64_u64_div_u64. Uwe will care for that when the dust settles.
+ */
+static u64 stm32_pwm_mul_u64_u64_div_u64_roundup(u64 a, u64 b, u64 c)
+{
+ u64 res = mul_u64_u64_div_u64(a, b, c);
+ /* Those multiplications might overflow but it doesn't matter */
+ u64 rem = a * b - c * res;
+
+ if (rem)
+ res += 1;
+
+ return res;
+}
+
+static int stm32_pwm_round_waveform_fromhw(struct pwm_chip *chip,
+ struct pwm_device *pwm,
+ const void *_wfhw,
+ struct pwm_waveform *wf)
+{
+ const struct stm32_pwm_waveform *wfhw = _wfhw;
+ struct stm32_pwm *priv = to_stm32_pwm_dev(chip);
+ unsigned int ch = pwm->hwpwm;
+
+ if (wfhw->ccer & TIM_CCER_CCxE(ch + 1)) {
+ unsigned long rate = clk_get_rate(priv->clk);
+ u64 ccr_ns;
+
+ /* The result doesn't overflow for rate >= 15259 */
+ wf->period_length = stm32_pwm_mul_u64_u64_div_u64_roundup(((u64)wfhw->psc + 1) * (wfhw->arr + 1), NSEC_PER_SEC, rate);
+
+ ccr_ns = stm32_pwm_mul_u64_u64_div_u64_roundup(((u64)wfhw->psc + 1) * wfhw->ccr, NSEC_PER_SEC, rate);
+
+ if (wfhw->ccer & TIM_CCER_CCxP(ch + 1)) {
+ wf->duty_length =
+ stm32_pwm_mul_u64_u64_div_u64_roundup(((u64)wfhw->psc + 1) * (wfhw->arr + 1 - wfhw->ccr),
+ NSEC_PER_SEC, rate);
+
+ wf->duty_offset = ccr_ns;
+ } else {
+ wf->duty_length = ccr_ns;
+ wf->duty_offset = 0;
+ }
+ } else {
+ *wf = (struct pwm_waveform){
+ .period_length = 0,
+ };
+ }
+
+ return 0;
+}
+
+static int stm32_pwm_read_waveform(struct pwm_chip *chip,
+ struct pwm_device *pwm,
+ void *_wfhw)
+{
+ struct stm32_pwm_waveform *wfhw = _wfhw;
+ struct stm32_pwm *priv = to_stm32_pwm_dev(chip);
+ unsigned int ch = pwm->hwpwm;
+ int ret;
+
+ ret = clk_enable(priv->clk);
+ if (ret)
+ return ret;
+
+ ret = regmap_read(priv->regmap, TIM_CCER, &wfhw->ccer);
+ if (ret)
+ goto out;
+
+ if (wfhw->ccer & TIM_CCER_CCxE(ch + 1)) {
+ ret = regmap_read(priv->regmap, TIM_PSC, &wfhw->psc);
+ if (ret)
+ goto out;
+
+ ret = regmap_read(priv->regmap, TIM_ARR, &wfhw->arr);
+ if (ret)
+ goto out;
+
+ if (wfhw->arr == U32_MAX)
+ wfhw->arr -= 1;
+
+ ret = regmap_read(priv->regmap, TIM_CCRx(ch + 1), &wfhw->ccr);
+ if (ret)
+ goto out;
+
+ if (wfhw->ccr > wfhw->arr + 1)
+ wfhw->ccr = wfhw->arr + 1;
+ }
+
+out:
+ clk_disable(priv->clk);
+
+ return ret;
+}
+
+static int stm32_pwm_write_waveform(struct pwm_chip *chip,
+ struct pwm_device *pwm,
+ const void *_wfhw)
+{
+ const struct stm32_pwm_waveform *wfhw = _wfhw;
+ struct stm32_pwm *priv = to_stm32_pwm_dev(chip);
+ unsigned int ch = pwm->hwpwm;
+ int ret;
+
+ ret = clk_enable(priv->clk);
+ if (ret)
+ return ret;
+
+ if (wfhw->ccer & TIM_CCER_CCxE(ch + 1)) {
+ u32 ccer, mask;
+ unsigned shift;
+ u32 ccmr;
+
+ ret = regmap_read(priv->regmap, TIM_CCER, &ccer);
+ if (ret)
+ goto out;
+
+ /* If there are other channels enabled, don't update PSC and ARR */
+ if (ccer & ~TIM_CCER_CCxE(ch + 1) & TIM_CCER_CCXE) {
+ u32 psc, arr;
+
+ ret = regmap_read(priv->regmap, TIM_PSC, &psc);
+ if (ret)
+ goto out;
+
+ if (psc != wfhw->psc) {
+ ret = -EBUSY;
+ goto out;
+ }
+
+ regmap_read(priv->regmap, TIM_ARR, &arr);
+ if (ret)
+ goto out;
+
+ if (arr != wfhw->arr) {
+ ret = -EBUSY;
+ goto out;
+ }
+ } else {
+ ret = regmap_write(priv->regmap, TIM_PSC, wfhw->psc);
+ if (ret)
+ goto out;
+
+ ret = regmap_write(priv->regmap, TIM_ARR, wfhw->arr);
+ if (ret)
+ goto out;
+
+ ret = regmap_set_bits(priv->regmap, TIM_CR1, TIM_CR1_ARPE);
+ if (ret)
+ goto out;
+
+ }
+
+ /* set polarity */
+ mask = TIM_CCER_CCxP(ch + 1) | TIM_CCER_CCxNP(ch + 1);
+ ret = regmap_update_bits(priv->regmap, TIM_CCER, mask, wfhw->ccer);
+ if (ret)
+ goto out;
+
+ ret = regmap_write(priv->regmap, TIM_CCRx(ch + 1), wfhw->ccr);
+ if (ret)
+ goto out;
+
+ /* Configure output mode */
+ shift = (ch & 0x1) * CCMR_CHANNEL_SHIFT;
+ ccmr = (TIM_CCMR_PE | TIM_CCMR_M1) << shift;
+ mask = CCMR_CHANNEL_MASK << shift;
+
+ if (ch < 2)
+ ret = regmap_update_bits(priv->regmap, TIM_CCMR1, mask, ccmr);
+ else
+ ret = regmap_update_bits(priv->regmap, TIM_CCMR2, mask, ccmr);
+ if (ret)
+ goto out;
+
+ ret = regmap_set_bits(priv->regmap, TIM_BDTR, TIM_BDTR_MOE);
+ if (ret)
+ goto out;
+
+ if (!(ccer & TIM_CCER_CCxE(ch + 1))) {
+ mask = TIM_CCER_CCxE(ch + 1) | TIM_CCER_CCxNE(ch + 1);
+
+ ret = clk_enable(priv->clk);
+ if (ret)
+ goto out;
+
+ ccer = (ccer & ~mask) | (wfhw->ccer & mask);
+ regmap_write(priv->regmap, TIM_CCER, ccer);
+
+ /* Make sure that registers are updated */
+ regmap_set_bits(priv->regmap, TIM_EGR, TIM_EGR_UG);
+
+ /* Enable controller */
+ regmap_set_bits(priv->regmap, TIM_CR1, TIM_CR1_CEN);
+ }
+
+ } else {
+ /* disable channel */
+ u32 mask, ccer;
+
+ mask = TIM_CCER_CCxE(ch + 1);
+ if (priv->have_complementary_output)
+ mask |= TIM_CCER_CCxNE(ch + 1);
+
+ ret = regmap_read(priv->regmap, TIM_CCER, &ccer);
+ if (ret)
+ goto out;
+
+ if (ccer & mask) {
+ ccer = ccer & ~mask;
+
+ ret = regmap_write(priv->regmap, TIM_CCER, ccer);
+ if (ret)
+ goto out;
+
+ if (!(ccer & TIM_CCER_CCXE)) {
+ /* When all channels are disabled, we can disable the controller */
+ ret = regmap_clear_bits(priv->regmap, TIM_CR1, TIM_CR1_CEN);
+ if (ret)
+ goto out;
+ }
+
+ clk_disable(priv->clk);
+ }
+ }
+
+out:
+ clk_disable(priv->clk);
+
+ return ret;
+}
+
#define TIM_CCER_CC12P (TIM_CCER_CC1P | TIM_CCER_CC2P)
#define TIM_CCER_CC12E (TIM_CCER_CC1E | TIM_CCER_CC2E)
#define TIM_CCER_CC34P (TIM_CCER_CC3P | TIM_CCER_CC4P)
@@ -308,228 +686,13 @@ static int stm32_pwm_capture(struct pwm_chip *chip, struct pwm_device *pwm,
return ret;
}
-static int stm32_pwm_config(struct stm32_pwm *priv, unsigned int ch,
- u64 duty_ns, u64 period_ns)
-{
- unsigned long long prd, dty;
- unsigned long long prescaler;
- u32 ccmr, mask, shift;
-
- /*
- * .probe() asserted that clk_get_rate() is not bigger than 1 GHz, so
- * the calculations here won't overflow.
- * First we need to find the minimal value for prescaler such that
- *
- * period_ns * clkrate
- * ------------------------------ < max_arr + 1
- * NSEC_PER_SEC * (prescaler + 1)
- *
- * This equation is equivalent to
- *
- * period_ns * clkrate
- * ---------------------------- < prescaler + 1
- * NSEC_PER_SEC * (max_arr + 1)
- *
- * Using integer division and knowing that the right hand side is
- * integer, this is further equivalent to
- *
- * (period_ns * clkrate) // (NSEC_PER_SEC * (max_arr + 1)) ≤ prescaler
- */
-
- prescaler = mul_u64_u64_div_u64(period_ns, clk_get_rate(priv->clk),
- (u64)NSEC_PER_SEC * ((u64)priv->max_arr + 1));
- if (prescaler > MAX_TIM_PSC)
- return -EINVAL;
-
- prd = mul_u64_u64_div_u64(period_ns, clk_get_rate(priv->clk),
- (u64)NSEC_PER_SEC * (prescaler + 1));
- if (!prd)
- return -EINVAL;
-
- /*
- * All channels share the same prescaler and counter so when two
- * channels are active at the same time we can't change them
- */
- if (active_channels(priv) & ~(1 << ch * 4)) {
- u32 psc, arr;
-
- regmap_read(priv->regmap, TIM_PSC, &psc);
- regmap_read(priv->regmap, TIM_ARR, &arr);
-
- if ((psc != prescaler) || (arr != prd - 1))
- return -EBUSY;
- }
-
- regmap_write(priv->regmap, TIM_PSC, prescaler);
- regmap_write(priv->regmap, TIM_ARR, prd - 1);
- regmap_set_bits(priv->regmap, TIM_CR1, TIM_CR1_ARPE);
-
- /* Calculate the duty cycles */
- dty = mul_u64_u64_div_u64(duty_ns, clk_get_rate(priv->clk),
- (u64)NSEC_PER_SEC * (prescaler + 1));
-
- regmap_write(priv->regmap, TIM_CCRx(ch + 1), dty);
-
- /* Configure output mode */
- shift = (ch & 0x1) * CCMR_CHANNEL_SHIFT;
- ccmr = (TIM_CCMR_PE | TIM_CCMR_M1) << shift;
- mask = CCMR_CHANNEL_MASK << shift;
-
- if (ch < 2)
- regmap_update_bits(priv->regmap, TIM_CCMR1, mask, ccmr);
- else
- regmap_update_bits(priv->regmap, TIM_CCMR2, mask, ccmr);
-
- regmap_set_bits(priv->regmap, TIM_BDTR, TIM_BDTR_MOE);
-
- return 0;
-}
-
-static int stm32_pwm_set_polarity(struct stm32_pwm *priv, unsigned int ch,
- enum pwm_polarity polarity)
-{
- u32 mask;
-
- mask = TIM_CCER_CCxP(ch + 1);
- if (priv->have_complementary_output)
- mask |= TIM_CCER_CCxNP(ch + 1);
-
- regmap_update_bits(priv->regmap, TIM_CCER, mask,
- polarity == PWM_POLARITY_NORMAL ? 0 : mask);
-
- return 0;
-}
-
-static int stm32_pwm_enable(struct stm32_pwm *priv, unsigned int ch)
-{
- u32 mask;
- int ret;
-
- ret = clk_enable(priv->clk);
- if (ret)
- return ret;
-
- /* Enable channel */
- mask = TIM_CCER_CCxE(ch + 1);
- if (priv->have_complementary_output)
- mask |= TIM_CCER_CCxNE(ch);
-
- regmap_set_bits(priv->regmap, TIM_CCER, mask);
-
- /* Make sure that registers are updated */
- regmap_set_bits(priv->regmap, TIM_EGR, TIM_EGR_UG);
-
- /* Enable controller */
- regmap_set_bits(priv->regmap, TIM_CR1, TIM_CR1_CEN);
-
- return 0;
-}
-
-static void stm32_pwm_disable(struct stm32_pwm *priv, unsigned int ch)
-{
- u32 mask;
-
- /* Disable channel */
- mask = TIM_CCER_CCxE(ch + 1);
- if (priv->have_complementary_output)
- mask |= TIM_CCER_CCxNE(ch + 1);
-
- regmap_clear_bits(priv->regmap, TIM_CCER, mask);
-
- /* When all channels are disabled, we can disable the controller */
- if (!active_channels(priv))
- regmap_clear_bits(priv->regmap, TIM_CR1, TIM_CR1_CEN);
-
- clk_disable(priv->clk);
-}
-
-static int stm32_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
- const struct pwm_state *state)
-{
- bool enabled;
- struct stm32_pwm *priv = to_stm32_pwm_dev(chip);
- int ret;
-
- enabled = pwm->state.enabled;
-
- if (!state->enabled) {
- if (enabled)
- stm32_pwm_disable(priv, pwm->hwpwm);
- return 0;
- }
-
- if (state->polarity != pwm->state.polarity)
- stm32_pwm_set_polarity(priv, pwm->hwpwm, state->polarity);
-
- ret = stm32_pwm_config(priv, pwm->hwpwm,
- state->duty_cycle, state->period);
- if (ret)
- return ret;
-
- if (!enabled && state->enabled)
- ret = stm32_pwm_enable(priv, pwm->hwpwm);
-
- return ret;
-}
-
-static int stm32_pwm_apply_locked(struct pwm_chip *chip, struct pwm_device *pwm,
- const struct pwm_state *state)
-{
- struct stm32_pwm *priv = to_stm32_pwm_dev(chip);
- int ret;
-
- /* protect common prescaler for all active channels */
- mutex_lock(&priv->lock);
- ret = stm32_pwm_apply(chip, pwm, state);
- mutex_unlock(&priv->lock);
-
- return ret;
-}
-
-static int stm32_pwm_get_state(struct pwm_chip *chip,
- struct pwm_device *pwm, struct pwm_state *state)
-{
- struct stm32_pwm *priv = to_stm32_pwm_dev(chip);
- int ch = pwm->hwpwm;
- unsigned long rate;
- u32 ccer, psc, arr, ccr;
- u64 dty, prd;
- int ret;
-
- mutex_lock(&priv->lock);
-
- ret = regmap_read(priv->regmap, TIM_CCER, &ccer);
- if (ret)
- goto out;
-
- state->enabled = ccer & TIM_CCER_CCxE(ch + 1);
- state->polarity = (ccer & TIM_CCER_CCxP(ch + 1)) ?
- PWM_POLARITY_INVERSED : PWM_POLARITY_NORMAL;
- ret = regmap_read(priv->regmap, TIM_PSC, &psc);
- if (ret)
- goto out;
- ret = regmap_read(priv->regmap, TIM_ARR, &arr);
- if (ret)
- goto out;
- ret = regmap_read(priv->regmap, TIM_CCRx(ch + 1), &ccr);
- if (ret)
- goto out;
-
- rate = clk_get_rate(priv->clk);
-
- prd = (u64)NSEC_PER_SEC * (psc + 1) * (arr + 1);
- state->period = DIV_ROUND_UP_ULL(prd, rate);
- dty = (u64)NSEC_PER_SEC * (psc + 1) * ccr;
- state->duty_cycle = DIV_ROUND_UP_ULL(dty, rate);
-
-out:
- mutex_unlock(&priv->lock);
- return ret;
-}
-
static const struct pwm_ops stm32pwm_ops = {
- .apply = stm32_pwm_apply_locked,
- .get_state = stm32_pwm_get_state,
+ .sizeof_wfhw = sizeof(struct stm32_pwm_waveform),
+ .round_waveform_tohw = stm32_pwm_round_waveform_tohw,
+ .round_waveform_fromhw = stm32_pwm_round_waveform_fromhw,
+ .read_waveform = stm32_pwm_read_waveform,
+ .write_waveform = stm32_pwm_write_waveform,
+
.capture = IS_ENABLED(CONFIG_DMA_ENGINE) ? stm32_pwm_capture : NULL,
};
--
2.43.0
^ permalink raw reply related [flat|nested] 17+ messages in thread
* Re: [PATCH 5/6] pwm: axi-pwmgen: Implementation of the waveform callbacks
2024-07-08 10:52 ` [PATCH 5/6] pwm: axi-pwmgen: Implementation of the " Uwe Kleine-König
@ 2024-07-08 18:08 ` Trevor Gamblin
0 siblings, 0 replies; 17+ messages in thread
From: Trevor Gamblin @ 2024-07-08 18:08 UTC (permalink / raw)
To: Uwe Kleine-König, linux-pwm; +Cc: Michael Hennerich, Nuno Sá
On 2024-07-08 6:52 a.m., Uwe Kleine-König wrote:
> Convert the axi-pwmgen driver to use the new callbacks for hardware
> programming.
>
> Signed-off-by: Uwe Kleine-König <u.kleine-koenig@baylibre.com>
Tested-by: Trevor Gamblin <tgamblin@baylibre.com>
> ---
> drivers/pwm/pwm-axi-pwmgen.c | 148 ++++++++++++++++++++++++-----------
> 1 file changed, 102 insertions(+), 46 deletions(-)
>
> diff --git a/drivers/pwm/pwm-axi-pwmgen.c b/drivers/pwm/pwm-axi-pwmgen.c
> index aac4f395497b..757ae037d5d6 100644
> --- a/drivers/pwm/pwm-axi-pwmgen.c
> +++ b/drivers/pwm/pwm-axi-pwmgen.c
> @@ -23,6 +23,7 @@
> #include <linux/err.h>
> #include <linux/fpga/adi-axi-common.h>
> #include <linux/io.h>
> +#include <linux/minmax.h>
> #include <linux/module.h>
> #include <linux/platform_device.h>
> #include <linux/pwm.h>
> @@ -53,81 +54,136 @@ static const struct regmap_config axi_pwmgen_regmap_config = {
> .val_bits = 32,
> };
>
> -static int axi_pwmgen_apply(struct pwm_chip *chip, struct pwm_device *pwm,
> - const struct pwm_state *state)
> +/* This represents a hardware configuration for one channel */
> +struct axi_pwmgen_waveform {
> + u32 period_cnt;
> + u32 duty_cycle_cnt;
> + u32 duty_offset_cnt;
> +};
> +
> +static int axi_pwmgen_round_waveform_tohw(struct pwm_chip *chip,
> + struct pwm_device *pwm,
> + const struct pwm_waveform *wf,
> + void *_wfhw)
> {
> + struct axi_pwmgen_waveform *wfhw = _wfhw;
> + struct axi_pwmgen_ddata *ddata = pwmchip_get_drvdata(chip);
> +
> + if (wf->period_length == 0) {
> + *wfhw = (struct axi_pwmgen_waveform){
> + .period_cnt = 0,
> + .duty_cycle_cnt = 0,
> + .duty_offset_cnt = 0,
> + };
> + } else {
> + /* With ddata->clk_rate_hz < NSEC_PER_SEC this won't overflow. */
> + wfhw->period_cnt = min_t(u64, mul_u64_u32_div(wf->period_length, ddata->clk_rate_hz, NSEC_PER_SEC), U32_MAX);
> +
> + if (wfhw->period_cnt == 0) {
> + /*
> + * The specified period is too short for the hardware.
> + * Let's round .duty_cycle down to 0 to get a (somewhat)
> + * valid result.
> + */
> + wfhw->period_cnt = 1;
> + wfhw->duty_cycle_cnt = 0;
> + wfhw->duty_offset_cnt = 0;
> + } else {
> + wfhw->duty_cycle_cnt = min_t(u64, mul_u64_u32_div(wf->duty_length, ddata->clk_rate_hz, NSEC_PER_SEC), U32_MAX);
> + wfhw->duty_offset_cnt = min_t(u64, mul_u64_u32_div(wf->duty_offset, ddata->clk_rate_hz, NSEC_PER_SEC), U32_MAX);
> + }
> + }
> +
> + dev_dbg(&chip->dev, "pwm#%u: %lld/%lld [+%lld] @%lu -> PERIOD: %08x, DUTY: %08x, OFFSET: %08x\n",
> + pwm->hwpwm, wf->duty_length, wf->period_length, wf->duty_offset,
> + ddata->clk_rate_hz, wfhw->period_cnt, wfhw->duty_cycle_cnt, wfhw->duty_offset_cnt);
> +
> + return 0;
> +}
> +
> +static int axi_pwmgen_round_waveform_fromhw(struct pwm_chip *chip, struct pwm_device *pwm,
> + const void *_wfhw, struct pwm_waveform *wf)
> +{
> + const struct axi_pwmgen_waveform *wfhw = _wfhw;
> + struct axi_pwmgen_ddata *ddata = pwmchip_get_drvdata(chip);
> +
> + wf->period_length = DIV64_U64_ROUND_UP((u64)wfhw->period_cnt * NSEC_PER_SEC,
> + ddata->clk_rate_hz);
> +
> + wf->duty_length = DIV64_U64_ROUND_UP((u64)wfhw->duty_cycle_cnt * NSEC_PER_SEC,
> + ddata->clk_rate_hz);
> +
> + wf->duty_offset = DIV64_U64_ROUND_UP((u64)wfhw->duty_offset_cnt * NSEC_PER_SEC,
> + ddata->clk_rate_hz);
> +
> + return 0;
> +}
> +
> +static int axi_pwmgen_write_waveform(struct pwm_chip *chip,
> + struct pwm_device *pwm,
> + const void *_wfhw)
> +{
> + const struct axi_pwmgen_waveform *wfhw = _wfhw;
> struct axi_pwmgen_ddata *ddata = pwmchip_get_drvdata(chip);
> - unsigned int ch = pwm->hwpwm;
> struct regmap *regmap = ddata->regmap;
> - u64 period_cnt, duty_cnt;
> + unsigned int ch = pwm->hwpwm;
> int ret;
>
> - if (state->polarity != PWM_POLARITY_NORMAL)
> - return -EINVAL;
> + ret = regmap_write(regmap, AXI_PWMGEN_CHX_PERIOD(ch), wfhw->period_cnt);
> + if (ret)
> + return ret;
>
> - if (state->enabled) {
> - period_cnt = mul_u64_u64_div_u64(state->period, ddata->clk_rate_hz, NSEC_PER_SEC);
> - if (period_cnt > UINT_MAX)
> - period_cnt = UINT_MAX;
> + ret = regmap_write(regmap, AXI_PWMGEN_CHX_DUTY(ch), wfhw->duty_cycle_cnt);
> + if (ret)
> + return ret;
>
> - if (period_cnt == 0)
> - return -EINVAL;
> -
> - ret = regmap_write(regmap, AXI_PWMGEN_CHX_PERIOD(ch), period_cnt);
> - if (ret)
> - return ret;
> -
> - duty_cnt = mul_u64_u64_div_u64(state->duty_cycle, ddata->clk_rate_hz, NSEC_PER_SEC);
> - if (duty_cnt > UINT_MAX)
> - duty_cnt = UINT_MAX;
> -
> - ret = regmap_write(regmap, AXI_PWMGEN_CHX_DUTY(ch), duty_cnt);
> - if (ret)
> - return ret;
> - } else {
> - ret = regmap_write(regmap, AXI_PWMGEN_CHX_PERIOD(ch), 0);
> - if (ret)
> - return ret;
> -
> - ret = regmap_write(regmap, AXI_PWMGEN_CHX_DUTY(ch), 0);
> - if (ret)
> - return ret;
> - }
> + ret = regmap_write(regmap, AXI_PWMGEN_CHX_OFFSET(ch), wfhw->duty_offset_cnt);
> + if (ret)
> + return ret;
>
> return regmap_write(regmap, AXI_PWMGEN_REG_CONFIG, AXI_PWMGEN_LOAD_CONFIG);
> }
>
> -static int axi_pwmgen_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
> - struct pwm_state *state)
> +static int axi_pwmgen_read_waveform(struct pwm_chip *chip,
> + struct pwm_device *pwm,
> + void *_wfhw)
> {
> + struct axi_pwmgen_waveform *wfhw = _wfhw;
> struct axi_pwmgen_ddata *ddata = pwmchip_get_drvdata(chip);
> struct regmap *regmap = ddata->regmap;
> unsigned int ch = pwm->hwpwm;
> - u32 cnt;
> int ret;
>
> - ret = regmap_read(regmap, AXI_PWMGEN_CHX_PERIOD(ch), &cnt);
> + ret = regmap_read(regmap, AXI_PWMGEN_CHX_PERIOD(ch), &wfhw->period_cnt);
> if (ret)
> return ret;
>
> - state->enabled = cnt != 0;
> -
> - state->period = DIV_ROUND_UP_ULL((u64)cnt * NSEC_PER_SEC, ddata->clk_rate_hz);
> -
> - ret = regmap_read(regmap, AXI_PWMGEN_CHX_DUTY(ch), &cnt);
> + ret = regmap_read(regmap, AXI_PWMGEN_CHX_DUTY(ch), &wfhw->duty_cycle_cnt);
> if (ret)
> return ret;
>
> - state->duty_cycle = DIV_ROUND_UP_ULL((u64)cnt * NSEC_PER_SEC, ddata->clk_rate_hz);
> + ret = regmap_read(regmap, AXI_PWMGEN_CHX_OFFSET(ch), &wfhw->duty_offset_cnt);
> + if (ret)
> + return ret;
>
> - state->polarity = PWM_POLARITY_NORMAL;
> + if (wfhw->duty_cycle_cnt > wfhw->period_cnt)
> + wfhw->duty_cycle_cnt = wfhw->period_cnt;
> +
> + /* XXX: is this the actual behaviour of the hardware? */
> + if (wfhw->duty_offset_cnt >= wfhw->period_cnt) {
> + wfhw->duty_cycle_cnt = 0;
> + wfhw->duty_offset_cnt = 0;
> + }
>
> return 0;
> }
>
> static const struct pwm_ops axi_pwmgen_pwm_ops = {
> - .apply = axi_pwmgen_apply,
> - .get_state = axi_pwmgen_get_state,
> + .sizeof_wfhw = sizeof(struct axi_pwmgen_waveform),
> + .round_waveform_tohw = axi_pwmgen_round_waveform_tohw,
> + .round_waveform_fromhw = axi_pwmgen_round_waveform_fromhw,
> + .read_waveform = axi_pwmgen_read_waveform,
> + .write_waveform = axi_pwmgen_write_waveform,
> };
>
> static int axi_pwmgen_setup(struct regmap *regmap, struct device *dev)
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: [PATCH 2/6] pwm: New abstraction for PWM waveforms
2024-07-08 10:52 ` [PATCH 2/6] pwm: New abstraction for PWM waveforms Uwe Kleine-König
@ 2024-07-08 18:12 ` Trevor Gamblin
0 siblings, 0 replies; 17+ messages in thread
From: Trevor Gamblin @ 2024-07-08 18:12 UTC (permalink / raw)
To: Uwe Kleine-König, linux-pwm
Cc: Michael Hennerich, Nuno Sá, Fabrice Gasnier, Maxime Coquelin,
Alexandre Torgue, linux-stm32, linux-arm-kernel
On 2024-07-08 6:52 a.m., Uwe Kleine-König wrote:
> Up to now the configuration of a PWM setting is decribed exclusively by
> a struct pwm_state which contains information about period, duty_cycle,
> polarity and if the PWM is enabled. (There is another member usage_power
> which doesn't completely fit into pwm_state, I ignore it here for
> simplicity.)
>
> Instead of a polarity the new abstraction has a member duty_offset that
> defines when the rising edge happens after the period start. This is
> more general, as with a pwm_state the rising edge can only happen at the
> period's start or such that the falling edge is at the end of the period
> (i.e. duty_offset == 0 or duty_offset == period_lengh - duty_length).
>
> A disabled PWM is modeled by .period_length = 0. In my eyes this is a
> nice usage of that otherwise unusable setting, as it doesn't define
> anything about the future which matches the fact that consumers should
> consider the state of the output as undefined and it's just there to say
> "No further requirements about the output, you can save some power.".
>
> Further I renamed period and duty_cycle to period_length and
> duty_length. In the past there was confusion from time to time about
> duty_cycle being measured in nanoseconds because people expected a
> percentage of period instead. With "length" as suffix the semantic
> should be more obvious to people unfamiliar with the pwm subsystem.
> period is renamed period_length for consistency.
>
> The API for consumers doesn't change yet, but lowlevel drivers can
> implement callbacks that work with pwm_waveforms instead of pwm_states.
> A new thing about these callbacks is that the calculation of hardware
> settings needed to implement a certain waveform is separated from
> actually writing these settings. The motivation for that is that this
> allows a consumer to query the hardware capabilities without actually
> modifying the hardware state.
>
> The rounding rules that are expected to be implemented in the
> round_waveform_tohw() are: First pick the biggest possible period not
> bigger than wf->period_length. For that period pick the biggest possible
> duty setting not bigger than wf->duty_length. Third pick the biggest
> possible offset not bigger than wf->duty_offset. If the requested period
> is too small for the hardware, it's expected that a setting with the
> minimal period and duty_length = duty_offset = 0 is returned and this
> fact is signaled by a return value of 1.
>
> Signed-off-by: Uwe Kleine-König <u.kleine-koenig@baylibre.com>
Reviewed-by: Trevor Gamblin <tgamblin@baylibre.com>
> ---
> drivers/pwm/core.c | 194 +++++++++++++++++++++++++++++++++++++++-----
> include/linux/pwm.h | 35 ++++++++
> 2 files changed, 208 insertions(+), 21 deletions(-)
>
> diff --git a/drivers/pwm/core.c b/drivers/pwm/core.c
> index c31e12e76495..8e68481a7b33 100644
> --- a/drivers/pwm/core.c
> +++ b/drivers/pwm/core.c
> @@ -49,6 +49,72 @@ static void pwmchip_unlock(struct pwm_chip *chip)
>
> DEFINE_GUARD(pwmchip, struct pwm_chip *, pwmchip_lock(_T), pwmchip_unlock(_T))
>
> +static void pwm_wf2state(const struct pwm_waveform *wf, struct pwm_state *state)
> +{
> + if (wf->period_length) {
> + if (wf->duty_length + wf->duty_offset < wf->period_length)
> + *state = (struct pwm_state){
> + .enabled = true,
> + .polarity = PWM_POLARITY_NORMAL,
> + .period = wf->period_length,
> + .duty_cycle = wf->duty_length,
> + };
> + else
> + *state = (struct pwm_state){
> + .enabled = true,
> + .polarity = PWM_POLARITY_INVERSED,
> + .period = wf->period_length,
> + .duty_cycle = wf->period_length - wf->duty_length,
> + };
> + } else {
> + *state = (struct pwm_state){
> + .enabled = false,
> + };
> + }
> +}
> +
> +static void pwm_state2wf(const struct pwm_state *state, struct pwm_waveform *wf)
> +{
> + if (state->enabled) {
> + if (state->polarity == PWM_POLARITY_NORMAL)
> + *wf = (struct pwm_waveform){
> + .period_length = state->period,
> + .duty_length = state->duty_cycle,
> + .duty_offset = 0,
> + };
> + else
> + *wf = (struct pwm_waveform){
> + .period_length = state->period,
> + .duty_length = state->period - state->duty_cycle,
> + .duty_offset = state->duty_cycle,
> + };
> + } else {
> + *wf = (struct pwm_waveform){
> + .period_length = 0,
> + };
> + }
> +}
> +
> +static int pwm_check_rounding(const struct pwm_waveform *wf,
> + const struct pwm_waveform *wf_rounded)
> +{
> + if (!wf->period_length)
> + return 0;
> +
> + if (wf->period_length < wf_rounded->period_length)
> + return 1;
> +
> + if (wf->duty_length < wf_rounded->duty_length)
> + return 1;
> +
> + if (wf->duty_offset < wf_rounded->duty_offset)
> + return 1;
> +
> + return 0;
> +}
> +
> +#define WFHWSIZE 20
> +
> static void pwm_apply_debug(struct pwm_device *pwm,
> const struct pwm_state *state)
> {
> @@ -182,6 +248,7 @@ static bool pwm_state_valid(const struct pwm_state *state)
> static int __pwm_apply(struct pwm_device *pwm, const struct pwm_state *state)
> {
> struct pwm_chip *chip;
> + const struct pwm_ops *ops;
> int err;
>
> if (!pwm || !state)
> @@ -205,6 +272,7 @@ static int __pwm_apply(struct pwm_device *pwm, const struct pwm_state *state)
> }
>
> chip = pwm->chip;
> + ops = chip->ops;
>
> if (state->period == pwm->state.period &&
> state->duty_cycle == pwm->state.duty_cycle &&
> @@ -213,18 +281,59 @@ static int __pwm_apply(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 (err)
> - return err;
> + if (ops->write_waveform) {
> + struct pwm_waveform wf;
> + char wfhw[WFHWSIZE];
>
> - pwm->state = *state;
> + BUG_ON(WFHWSIZE < ops->sizeof_wfhw);
>
> - /*
> - * only do this after pwm->state was applied as some
> - * implementations of .get_state depend on this
> - */
> - pwm_apply_debug(pwm, state);
> + pwm_state2wf(state, &wf);
> +
> + /*
> + * XXX The rounding is wrong here for states with inverted
> + * polarity. While .apply() rounds down duty_cycle (which
> + * represents the time from the start of the period to the inner
> + * edge), .round_waveform_tohw() rounds down the time the PWM is
> + * high.
> + */
> +
> + err = ops->round_waveform_tohw(chip, pwm, &wf, &wfhw);
> + if (err)
> + return err;
> +
> + if (IS_ENABLED(PWM_DEBUG)) {
> + struct pwm_waveform wf_rounded;
> +
> + err = ops->round_waveform_fromhw(chip, pwm, &wfhw, &wf_rounded);
> + if (err)
> + return err;
> +
> + if (pwm_check_rounding(&wf, &wf_rounded))
> + dev_err(&chip->dev, "Wrong rounding: requested %llu/%llu [+%llu], result %llu/%llu [+%llu]\n",
> + wf.duty_length, wf.period_length, wf.duty_offset,
> + wf_rounded.duty_length, wf_rounded.period_length, wf_rounded.duty_offset);
> + }
> +
> + err = ops->write_waveform(chip, pwm, &wfhw);
> + if (err)
> + return err;
> +
> + pwm->state = *state;
> +
> + } else {
> + err = ops->apply(chip, pwm, state);
> + trace_pwm_apply(pwm, state, err);
> + if (err)
> + return err;
> +
> + 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;
> }
> @@ -292,6 +401,41 @@ int pwm_apply_atomic(struct pwm_device *pwm, const struct pwm_state *state)
> }
> EXPORT_SYMBOL_GPL(pwm_apply_atomic);
>
> +static int pwm_get_state_hw(struct pwm_device *pwm, struct pwm_state *state)
> +{
> + struct pwm_chip *chip = pwm->chip;
> + const struct pwm_ops *ops = chip->ops;
> + int ret = -EOPNOTSUPP;
> +
> + if (ops->read_waveform) {
> + char wfhw[WFHWSIZE];
> + struct pwm_waveform wf;
> +
> + BUG_ON(WFHWSIZE < ops->sizeof_wfhw);
> +
> + scoped_guard(pwmchip, chip) {
> +
> + ret = ops->read_waveform(chip, pwm, &wfhw);
> + if (ret)
> + return ret;
> +
> + ret = ops->round_waveform_fromhw(chip, pwm, &wfhw, &wf);
> + if (ret)
> + return ret;
> + }
> +
> + pwm_wf2state(&wf, state);
> +
> + } else if (ops->get_state) {
> + scoped_guard(pwmchip, chip)
> + ret = ops->get_state(chip, pwm, state);
> +
> + trace_pwm_get(pwm, state, ret);
> + }
> +
> + return ret;
> +}
> +
> /**
> * pwm_adjust_config() - adjust the current PWM config to the PWM arguments
> * @pwm: PWM device
> @@ -433,7 +577,7 @@ static int pwm_device_request(struct pwm_device *pwm, const char *label)
> }
> }
>
> - if (ops->get_state) {
> + if (ops->read_waveform || ops->get_state) {
> /*
> * Zero-initialize state because most drivers are unaware of
> * .usage_power. The other members of state are supposed to be
> @@ -443,11 +587,7 @@ static int pwm_device_request(struct pwm_device *pwm, const char *label)
> */
> struct pwm_state state = { 0, };
>
> - scoped_guard(pwmchip, chip)
> - err = ops->get_state(chip, pwm, &state);
> -
> - trace_pwm_get(pwm, &state, err);
> -
> + err = pwm_get_state_hw(pwm, &state);
> if (!err)
> pwm->state = state;
>
> @@ -1134,12 +1274,24 @@ static bool pwm_ops_check(const struct pwm_chip *chip)
> {
> const struct pwm_ops *ops = chip->ops;
>
> - if (!ops->apply)
> - return false;
> + if (ops->write_waveform) {
> + if (!ops->round_waveform_tohw ||
> + !ops->round_waveform_fromhw ||
> + !ops->write_waveform)
> + return false;
>
> - if (IS_ENABLED(CONFIG_PWM_DEBUG) && !ops->get_state)
> - dev_warn(pwmchip_parent(chip),
> - "Please implement the .get_state() callback\n");
> + if (WFHWSIZE < ops->sizeof_wfhw) {
> + dev_warn(pwmchip_parent(chip), "WFHWSIZE < %zu\n", ops->sizeof_wfhw);
> + return false;
> + }
> + } else {
> + if (!ops->apply)
> + return false;
> +
> + if (IS_ENABLED(CONFIG_PWM_DEBUG) && !ops->get_state)
> + dev_warn(pwmchip_parent(chip),
> + "Please implement the .get_state() callback\n");
> + }
>
> return true;
> }
> diff --git a/include/linux/pwm.h b/include/linux/pwm.h
> index 5176dfebfbfd..b5dff2a99038 100644
> --- a/include/linux/pwm.h
> +++ b/include/linux/pwm.h
> @@ -49,6 +49,30 @@ enum {
> PWMF_EXPORTED = 1,
> };
>
> +/*
> + * struct pwm_waveform - description of a PWM waveform
> + * @period_length: PWM period
> + * @duty_length: PWM duty cycle
> + * @duty_offset: offset of the rising edge from the period's start
> + *
> + * This is a representation of a PWM waveform alternative to struct pwm_state
> + * below. It's more expressive than struct pwm_state as it contains a
> + * duty_offset and so can represent offsets other than $period - $duty_cycle
> + * which is done using .polarity = PWM_POLARITY_INVERSED. Note there is no
> + * explicit bool for enabled. A "disabled" PWM is represented by .period = 0.
> + *
> + * Note that the behaviour of a "disabled" PWM is undefined. Depending on the
> + * hardware's capabilities it might drive the active or inactive level, go
> + * high-z or even continue to toggle.
> + *
> + * The unit for all three members is nanoseconds.
> + */
> +struct pwm_waveform {
> + u64 period_length;
> + u64 duty_length;
> + u64 duty_offset;
> +};
> +
> /*
> * struct pwm_state - state of a PWM channel
> * @period: PWM period (in nanoseconds)
> @@ -259,6 +283,17 @@ struct pwm_ops {
> void (*free)(struct pwm_chip *chip, struct pwm_device *pwm);
> int (*capture)(struct pwm_chip *chip, struct pwm_device *pwm,
> struct pwm_capture *result, unsigned long timeout);
> +
> + size_t sizeof_wfhw;
> + int (*round_waveform_tohw)(struct pwm_chip *chip, struct pwm_device *pwm,
> + const struct pwm_waveform *wf, void *wfhw);
> + int (*round_waveform_fromhw)(struct pwm_chip *chip, struct pwm_device *pwm,
> + const void *wfhw, struct pwm_waveform *wf);
> + int (*read_waveform)(struct pwm_chip *chip, struct pwm_device *pwm,
> + void *wfhw);
> + int (*write_waveform)(struct pwm_chip *chip, struct pwm_device *pwm,
> + const void *wfhw);
> +
> int (*apply)(struct pwm_chip *chip, struct pwm_device *pwm,
> const struct pwm_state *state);
> int (*get_state)(struct pwm_chip *chip, struct pwm_device *pwm,
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: [PATCH 3/6] pwm: Add support for pwmchip devices for faster and easier userspace access
2024-07-08 10:52 ` [PATCH 3/6] pwm: Add support for pwmchip devices for faster and easier userspace access Uwe Kleine-König
@ 2024-07-08 18:13 ` Trevor Gamblin
2024-07-09 9:37 ` Nuno Sá
1 sibling, 0 replies; 17+ messages in thread
From: Trevor Gamblin @ 2024-07-08 18:13 UTC (permalink / raw)
To: Uwe Kleine-König, linux-pwm
On 2024-07-08 6:52 a.m., Uwe Kleine-König wrote:
> With this change each pwmchip can be accessed from userspace via a
> character device. Compared to the sysfs-API this is faster (on a
> stm32mp157 applying a new configuration takes approx 25% only) and
> allows to pass the whole configuration in a single ioctl allowing atomic
> application.
>
> Thanks to Randy Dunlap for pointing out a missing kernel-doc
> description.
>
> Signed-off-by: Uwe Kleine-König <u.kleine-koenig@baylibre.com>
Reviewed-by: Trevor Gamblin <tgamblin@baylibre.com>
> ---
> drivers/pwm/core.c | 367 +++++++++++++++++++++++++++++++++++++--
> include/linux/pwm.h | 3 +
> include/uapi/linux/pwm.h | 24 +++
> 3 files changed, 379 insertions(+), 15 deletions(-)
> create mode 100644 include/uapi/linux/pwm.h
>
> diff --git a/drivers/pwm/core.c b/drivers/pwm/core.c
> index 8e68481a7b33..d64c033c4cb2 100644
> --- a/drivers/pwm/core.c
> +++ b/drivers/pwm/core.c
> @@ -23,6 +23,8 @@
>
> #include <dt-bindings/pwm/pwm.h>
>
> +#include <uapi/linux/pwm.h>
> +
> #define CREATE_TRACE_POINTS
> #include <trace/events/pwm.h>
>
> @@ -95,6 +97,29 @@ static void pwm_state2wf(const struct pwm_state *state, struct pwm_waveform *wf)
> }
> }
>
> +static int pwmwfcmp(const struct pwm_waveform *a, const struct pwm_waveform *b)
> +{
> + if (a->period_length > b->period_length)
> + return 1;
> +
> + if (a->period_length < b->period_length)
> + return -1;
> +
> + if (a->duty_length > b->duty_length)
> + return 1;
> +
> + if (a->duty_length < b->duty_length)
> + return -1;
> +
> + if (a->duty_offset > b->duty_offset)
> + return 1;
> +
> + if (a->duty_offset < b->duty_offset)
> + return -1;
> +
> + return 0;
> +}
> +
> static int pwm_check_rounding(const struct pwm_waveform *wf,
> const struct pwm_waveform *wf_rounded)
> {
> @@ -115,6 +140,127 @@ static int pwm_check_rounding(const struct pwm_waveform *wf,
>
> #define WFHWSIZE 20
>
> +static int pwm_get_waveform(struct pwm_device *pwm,
> + struct pwm_waveform *wf)
> +{
> + struct pwm_chip *chip = pwm->chip;
> + const struct pwm_ops *ops = chip->ops;
> + char wfhw[WFHWSIZE];
> + int err;
> +
> + BUG_ON(WFHWSIZE < ops->sizeof_wfhw);
> +
> + guard(pwmchip)(chip);
> +
> + if (!chip->operational)
> + return -ENODEV;
> +
> + err = ops->read_waveform(chip, pwm, &wfhw);
> + if (err)
> + return err;
> +
> + return ops->round_waveform_fromhw(chip, pwm, &wfhw, wf);
> +}
> +
> +/* Called with the pwmchip lock held */
> +static int __pwm_set_waveform(struct pwm_device *pwm,
> + const struct pwm_waveform *wf,
> + bool exact)
> +{
> + struct pwm_chip *chip = pwm->chip;
> + const struct pwm_ops *ops = chip->ops;
> + char wfhw[WFHWSIZE];
> + struct pwm_waveform wf_rounded;
> + int err;
> +
> + BUG_ON(WFHWSIZE < ops->sizeof_wfhw);
> +
> + if (wf->period_length &&
> + (wf->duty_length > wf->period_length ||
> + wf->duty_offset >= wf->period_length))
> + return -EINVAL;
> +
> + err = ops->round_waveform_tohw(chip, pwm, wf, &wfhw);
> + if (err)
> + return err;
> +
> + if ((IS_ENABLED(CONFIG_PWM_DEBUG) || exact) && wf->period_length) {
> + err = ops->round_waveform_fromhw(chip, pwm, &wfhw, &wf_rounded);
> + if (err)
> + return err;
> +
> + if (IS_ENABLED(CONFIG_PWM_DEBUG) && pwm_check_rounding(wf, &wf_rounded))
> + dev_err(&chip->dev, "Wrong rounding: requested %llu/%llu [+%llu], result %llu/%llu [+%llu]\n",
> + wf->duty_length, wf->period_length, wf->duty_offset,
> + wf_rounded.duty_length, wf_rounded.period_length, wf_rounded.duty_offset);
> +
> + if (exact && pwmwfcmp(wf, &wf_rounded)) {
> + dev_dbg(&chip->dev, "Requested no rounding, but %llu/%llu [+%llu] -> %llu/%llu [+%llu]\n",
> + wf->duty_length, wf->period_length, wf->duty_offset,
> + wf_rounded.duty_length, wf_rounded.period_length, wf_rounded.duty_offset);
> +
> + return 1;
> + }
> + }
> +
> + err = ops->write_waveform(chip, pwm, &wfhw);
> + if (err)
> + return err;
> +
> + /* update .state */
> + pwm_wf2state(wf, &pwm->state);
> +
> + if (IS_ENABLED(CONFIG_PWM_DEBUG) && ops->read_waveform && wf->period_length) {
> + struct pwm_waveform wf_set;
> +
> + err = ops->read_waveform(chip, pwm, &wfhw);
> + if (err)
> + /* maybe ignore? */
> + return err;
> +
> + err = ops->round_waveform_fromhw(chip, pwm, &wfhw, &wf_set);
> + if (err)
> + /* maybe ignore? */
> + return err;
> +
> + if (pwmwfcmp(&wf_set, &wf_rounded) != 0)
> + dev_err(&chip->dev,
> + "Unexpected setting: requested %llu/%llu [+%llu], expected %llu/%llu [+%llu], set %llu/%llu [+%llu]\n",
> + wf->duty_length, wf->period_length, wf->duty_offset,
> + wf_rounded.duty_length, wf_rounded.period_length, wf_rounded.duty_offset,
> + wf_set.duty_length, wf_set.period_length, wf_set.duty_offset);
> + }
> + return 0;
> +}
> +
> +static int pwm_set_waveform_might_sleep(struct pwm_device *pwm,
> + struct pwm_waveform *wf, bool exact)
> +{
> + struct pwm_chip *chip = pwm->chip;
> + int err;
> +
> + might_sleep();
> +
> + guard(pwmchip)(chip);
> +
> + if (!chip->operational)
> + return -ENODEV;
> +
> + if (IS_ENABLED(CONFIG_PWM_DEBUG) && chip->atomic) {
> + /*
> + * Catch any drivers that have been marked as atomic but
> + * that will sleep anyway.
> + */
> + non_block_start();
> + err = __pwm_set_waveform(pwm, wf, exact);
> + non_block_end();
> + } else {
> + err = __pwm_set_waveform(pwm, wf, exact);
> + }
> +
> + return err;
> +}
> +
> static void pwm_apply_debug(struct pwm_device *pwm,
> const struct pwm_state *state)
> {
> @@ -301,19 +447,6 @@ static int __pwm_apply(struct pwm_device *pwm, const struct pwm_state *state)
> if (err)
> return err;
>
> - if (IS_ENABLED(PWM_DEBUG)) {
> - struct pwm_waveform wf_rounded;
> -
> - err = ops->round_waveform_fromhw(chip, pwm, &wfhw, &wf_rounded);
> - if (err)
> - return err;
> -
> - if (pwm_check_rounding(&wf, &wf_rounded))
> - dev_err(&chip->dev, "Wrong rounding: requested %llu/%llu [+%llu], result %llu/%llu [+%llu]\n",
> - wf.duty_length, wf.period_length, wf.duty_offset,
> - wf_rounded.duty_length, wf_rounded.period_length, wf_rounded.duty_offset);
> - }
> -
> err = ops->write_waveform(chip, pwm, &wfhw);
> if (err)
> return err;
> @@ -1296,6 +1429,197 @@ static bool pwm_ops_check(const struct pwm_chip *chip)
> return true;
> }
>
> +struct pwm_cdev_data {
> + struct pwm_chip *chip;
> + struct pwm_device *pwm[];
> +};
> +
> +static int pwm_cdev_open(struct inode *inode, struct file *file)
> +{
> + struct pwm_chip *chip = container_of(inode->i_cdev, struct pwm_chip, cdev);
> + struct pwm_cdev_data *cdata;
> +
> + guard(mutex)(&pwm_lock);
> +
> + if (!chip->operational)
> + return -ENXIO;
> +
> + cdata = kzalloc(struct_size(cdata, pwm, chip->npwm), GFP_KERNEL);
> + if (!cdata)
> + return -ENOMEM;
> +
> + cdata->chip = chip;
> +
> + file->private_data = cdata;
> +
> + return nonseekable_open(inode, file);
> +}
> +
> +static int pwm_cdev_release(struct inode *inode, struct file *file)
> +{
> + struct pwm_cdev_data *cdata = file->private_data;
> + unsigned int i;
> +
> + for (i = 0; i < cdata->chip->npwm; ++i) {
> + if (cdata->pwm[i])
> + pwm_put(cdata->pwm[i]);
> + }
> + kfree(cdata);
> +
> + return 0;
> +}
> +
> +static int pwm_cdev_request(struct pwm_cdev_data *cdata, unsigned int hwpwm)
> +{
> + struct pwm_chip *chip = cdata->chip;
> +
> + if (hwpwm >= chip->npwm)
> + return -EINVAL;
> +
> + if (!cdata->pwm[hwpwm]) {
> + struct pwm_device *pwm = &chip->pwms[hwpwm];
> + int ret;
> +
> + ret = pwm_device_request(pwm, "pwm-cdev");
> + if (ret < 0)
> + return ret;
> +
> + cdata->pwm[hwpwm] = pwm;
> + }
> +
> + return 0;
> +}
> +
> +static int pwm_cdev_free(struct pwm_cdev_data *cdata, unsigned int hwpwm)
> +{
> + struct pwm_chip *chip = cdata->chip;
> +
> + if (hwpwm >= chip->npwm)
> + return -EINVAL;
> +
> + if (cdata->pwm[hwpwm]) {
> + struct pwm_device *pwm = cdata->pwm[hwpwm];
> +
> + pwm_put(pwm);
> +
> + cdata->pwm[hwpwm] = NULL;
> + }
> +
> + return 0;
> +}
> +
> +static long pwm_cdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
> +{
> + int ret = 0;
> + struct pwm_cdev_data *cdata = file->private_data;
> + struct pwm_chip *chip = cdata->chip;
> +
> + guard(mutex)(&pwm_lock);
> +
> + if (!chip->operational)
> + return -ENODEV;
> +
> + switch (cmd) {
> + case PWM_IOCTL_GET_NUM_PWMS:
> + return chip->npwm;
> +
> + case PWM_IOCTL_REQUEST:
> + {
> + unsigned int hwpwm;
> +
> + ret = get_user(hwpwm, (unsigned int __user *)arg);
> + if (ret)
> + return ret;
> +
> + return pwm_cdev_request(cdata, hwpwm);
> + }
> +
> + case PWM_IOCTL_FREE:
> + {
> + unsigned int hwpwm;
> +
> + ret = get_user(hwpwm, (unsigned int __user *)arg);
> + if (ret)
> + return ret;
> +
> + return pwm_cdev_free(cdata, hwpwm);
> + }
> +
> + case PWM_IOCTL_GETWF:
> + {
> + struct pwmchip_waveform cwf;
> + struct pwm_waveform wf;
> + struct pwm_device *pwm;
> +
> + ret = copy_from_user(&cwf, (struct pwmchip_waveform __user *)arg, sizeof(cwf));
> + if (ret)
> + return -EFAULT;
> +
> + ret = pwm_cdev_request(cdata, cwf.hwpwm);
> + if (ret)
> + return ret;
> +
> + pwm = cdata->pwm[cwf.hwpwm];
> +
> + ret = pwm_get_waveform(pwm, &wf);
> + if (ret)
> + return ret;
> +
> + cwf.period_length = wf.period_length;
> + cwf.duty_length = wf.duty_length;
> + cwf.duty_offset = wf.duty_offset;
> +
> + return copy_to_user((struct pwmchip_waveform __user *)arg, &cwf, sizeof(cwf));
> + }
> + break;
> +
> + case PWM_IOCTL_SETROUNDEDWF:
> + case PWM_IOCTL_SETEXACTWF:
> + {
> + struct pwmchip_waveform cwf;
> + struct pwm_waveform wf;
> + struct pwm_device *pwm;
> +
> + ret = copy_from_user(&cwf, (struct pwmchip_waveform __user *)arg, sizeof(cwf));
> + if (ret)
> + return -EFAULT;
> +
> + if (cwf.period_length > 0 &&
> + (cwf.duty_length > cwf.period_length ||
> + cwf.duty_offset >= cwf.period_length))
> + return -EINVAL;
> +
> + ret = pwm_cdev_request(cdata, cwf.hwpwm);
> + if (ret)
> + return ret;
> +
> + pwm = cdata->pwm[cwf.hwpwm];
> +
> + wf = (struct pwm_waveform){
> + .period_length = cwf.period_length,
> + .duty_length = cwf.duty_length,
> + .duty_offset = cwf.duty_offset,
> + };
> +
> + return pwm_set_waveform_might_sleep(pwm, &wf, cmd == PWM_IOCTL_SETEXACTWF);
> + }
> + break;
> +
> + default:
> + return -ENOTTY;
> + }
> +}
> +
> +static const struct file_operations pwm_cdev_fileops = {
> + .open = pwm_cdev_open,
> + .release = pwm_cdev_release,
> + .owner = THIS_MODULE,
> + .llseek = no_llseek,
> + .unlocked_ioctl = pwm_cdev_ioctl,
> +};
> +
> +static dev_t pwm_devt;
> +
> /**
> * __pwmchip_add() - register a new PWM chip
> * @chip: the PWM chip to add
> @@ -1348,7 +1672,13 @@ int __pwmchip_add(struct pwm_chip *chip, struct module *owner)
> scoped_guard(pwmchip, chip)
> chip->operational = true;
>
> - ret = device_add(&chip->dev);
> + if (chip->id < 256 && chip->ops->write_waveform)
> + chip->dev.devt = MKDEV(MAJOR(pwm_devt), chip->id);
> +
> + cdev_init(&chip->cdev, &pwm_cdev_fileops);
> + chip->cdev.owner = owner;
> +
> + ret = cdev_device_add(&chip->cdev, &chip->dev);
> if (ret)
> goto err_device_add;
>
> @@ -1399,7 +1729,7 @@ void pwmchip_remove(struct pwm_chip *chip)
> idr_remove(&pwm_chips, chip->id);
> }
>
> - device_del(&chip->dev);
> + cdev_device_del(&chip->cdev, &chip->dev);
> }
> EXPORT_SYMBOL_GPL(pwmchip_remove);
>
> @@ -1943,9 +2273,16 @@ static int __init pwm_init(void)
> {
> int ret;
>
> + ret = alloc_chrdev_region(&pwm_devt, 0, 256, "pwm");
> + if (ret) {
> + pr_warn("Failed to initialize chrdev region for PWM usage\n");
> + return ret;
> + }
> +
> ret = class_register(&pwm_class);
> if (ret) {
> pr_err("Failed to initialize PWM class (%pe)\n", ERR_PTR(ret));
> + unregister_chrdev_region(pwm_devt, 256);
> return ret;
> }
>
> diff --git a/include/linux/pwm.h b/include/linux/pwm.h
> index b5dff2a99038..3e503a28f5f7 100644
> --- a/include/linux/pwm.h
> +++ b/include/linux/pwm.h
> @@ -2,6 +2,7 @@
> #ifndef __LINUX_PWM_H
> #define __LINUX_PWM_H
>
> +#include <linux/cdev.h>
> #include <linux/device.h>
> #include <linux/err.h>
> #include <linux/module.h>
> @@ -303,6 +304,7 @@ struct pwm_ops {
> /**
> * struct pwm_chip - abstract a PWM controller
> * @dev: device providing the PWMs
> + * @cdev: &struct cdev for this device
> * @ops: callbacks for this PWM controller
> * @owner: module providing this chip
> * @id: unique number of this PWM chip
> @@ -317,6 +319,7 @@ struct pwm_ops {
> */
> struct pwm_chip {
> struct device dev;
> + struct cdev cdev;
> const struct pwm_ops *ops;
> struct module *owner;
> unsigned int id;
> diff --git a/include/uapi/linux/pwm.h b/include/uapi/linux/pwm.h
> new file mode 100644
> index 000000000000..1ecf2e033b62
> --- /dev/null
> +++ b/include/uapi/linux/pwm.h
> @@ -0,0 +1,24 @@
> +/* SPDX-License-Identifier: GPL-2.0-only WITH Linux-syscall-note */
> +
> +#ifndef _UAPI_PWM_H_
> +#define _UAPI_PWM_H_
> +
> +#include <linux/ioctl.h>
> +#include <linux/types.h>
> +
> +struct pwmchip_waveform {
> + unsigned int hwpwm;
> + __u64 period_length;
> + __u64 duty_length;
> + __u64 duty_offset;
> +};
> +
> +#define PWM_IOCTL_GET_NUM_PWMS _IO(0x75, 0)
> +#define PWM_IOCTL_REQUEST _IOW(0x75, 1, unsigned int)
> +#define PWM_IOCTL_FREE _IOW(0x75, 2, unsigned int)
> +#define PWM_IOCTL_ROUNDWF _IOWR(0x75, 3, struct pwmchip_waveform)
> +#define PWM_IOCTL_GETWF _IOWR(0x75, 4, struct pwmchip_waveform)
> +#define PWM_IOCTL_SETROUNDEDWF _IOW(0x75, 5, struct pwmchip_waveform)
> +#define PWM_IOCTL_SETEXACTWF _IOW(0x75, 6, struct pwmchip_waveform)
> +
> +#endif /* _UAPI_PWM_H_ */
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: [PATCH 4/6] pwm: Add tracing for waveform callbacks
2024-07-08 10:52 ` [PATCH 4/6] pwm: Add tracing for waveform callbacks Uwe Kleine-König
@ 2024-07-08 18:14 ` Trevor Gamblin
2024-07-09 6:54 ` Uwe Kleine-König
0 siblings, 1 reply; 17+ messages in thread
From: Trevor Gamblin @ 2024-07-08 18:14 UTC (permalink / raw)
To: Uwe Kleine-König, linux-pwm
Cc: Steven Rostedt, Masami Hiramatsu, Mathieu Desnoyers,
linux-trace-kernel
Missing a sign-off?
On 2024-07-08 6:52 a.m., Uwe Kleine-König wrote:
> ---
> drivers/pwm/core.c | 68 ++++++++++++++++---
> include/trace/events/pwm.h | 134 ++++++++++++++++++++++++++++++++++---
> 2 files changed, 183 insertions(+), 19 deletions(-)
>
> diff --git a/drivers/pwm/core.c b/drivers/pwm/core.c
> index d64c033c4cb2..a2320ae77220 100644
> --- a/drivers/pwm/core.c
> +++ b/drivers/pwm/core.c
> @@ -138,6 +138,52 @@ static int pwm_check_rounding(const struct pwm_waveform *wf,
> return 0;
> }
>
> +static int pwm_round_waveform_tohw(struct pwm_chip *chip, struct pwm_device *pwm,
> + const struct pwm_waveform *wf, void *wfhw)
> +{
> + const struct pwm_ops *ops = chip->ops;
> + int ret;
> +
> + ret = ops->round_waveform_tohw(chip, pwm, wf, wfhw);
> + trace_pwm_round_waveform_tohw(pwm, wf, wfhw, ret);
> +
> + return ret;
> +}
> +
> +static int pwm_round_waveform_fromhw(struct pwm_chip *chip, struct pwm_device *pwm,
> + const void *wfhw, struct pwm_waveform *wf)
> +{
> + const struct pwm_ops *ops = chip->ops;
> + int ret;
> +
> + ret = ops->round_waveform_fromhw(chip, pwm, wfhw, wf);
> + trace_pwm_round_waveform_fromhw(pwm, wfhw, wf, ret);
> +
> + return ret;
> +}
> +
> +static int pwm_read_waveform(struct pwm_chip *chip, struct pwm_device *pwm, void *wfhw)
> +{
> + const struct pwm_ops *ops = chip->ops;
> + int ret;
> +
> + ret = ops->read_waveform(chip, pwm, wfhw);
> + trace_pwm_read_waveform(pwm, wfhw, ret);
> +
> + return ret;
> +}
> +
> +static int pwm_write_waveform(struct pwm_chip *chip, struct pwm_device *pwm, const void *wfhw)
> +{
> + const struct pwm_ops *ops = chip->ops;
> + int ret;
> +
> + ret = ops->write_waveform(chip, pwm, wfhw);
> + trace_pwm_write_waveform(pwm, wfhw, ret);
> +
> + return ret;
> +}
> +
> #define WFHWSIZE 20
>
> static int pwm_get_waveform(struct pwm_device *pwm,
> @@ -155,11 +201,11 @@ static int pwm_get_waveform(struct pwm_device *pwm,
> if (!chip->operational)
> return -ENODEV;
>
> - err = ops->read_waveform(chip, pwm, &wfhw);
> + err = pwm_read_waveform(chip, pwm, &wfhw);
> if (err)
> return err;
>
> - return ops->round_waveform_fromhw(chip, pwm, &wfhw, wf);
> + return pwm_round_waveform_fromhw(chip, pwm, &wfhw, wf);
> }
>
> /* Called with the pwmchip lock held */
> @@ -180,12 +226,12 @@ static int __pwm_set_waveform(struct pwm_device *pwm,
> wf->duty_offset >= wf->period_length))
> return -EINVAL;
>
> - err = ops->round_waveform_tohw(chip, pwm, wf, &wfhw);
> + err = pwm_round_waveform_tohw(chip, pwm, wf, &wfhw);
> if (err)
> return err;
>
> if ((IS_ENABLED(CONFIG_PWM_DEBUG) || exact) && wf->period_length) {
> - err = ops->round_waveform_fromhw(chip, pwm, &wfhw, &wf_rounded);
> + err = pwm_round_waveform_fromhw(chip, pwm, &wfhw, &wf_rounded);
> if (err)
> return err;
>
> @@ -203,7 +249,7 @@ static int __pwm_set_waveform(struct pwm_device *pwm,
> }
> }
>
> - err = ops->write_waveform(chip, pwm, &wfhw);
> + err = pwm_write_waveform(chip, pwm, &wfhw);
> if (err)
> return err;
>
> @@ -213,12 +259,12 @@ static int __pwm_set_waveform(struct pwm_device *pwm,
> if (IS_ENABLED(CONFIG_PWM_DEBUG) && ops->read_waveform && wf->period_length) {
> struct pwm_waveform wf_set;
>
> - err = ops->read_waveform(chip, pwm, &wfhw);
> + err = pwm_read_waveform(chip, pwm, &wfhw);
> if (err)
> /* maybe ignore? */
> return err;
>
> - err = ops->round_waveform_fromhw(chip, pwm, &wfhw, &wf_set);
> + err = pwm_round_waveform_fromhw(chip, pwm, &wfhw, &wf_set);
> if (err)
> /* maybe ignore? */
> return err;
> @@ -443,11 +489,11 @@ static int __pwm_apply(struct pwm_device *pwm, const struct pwm_state *state)
> * high.
> */
>
> - err = ops->round_waveform_tohw(chip, pwm, &wf, &wfhw);
> + err = pwm_round_waveform_tohw(chip, pwm, &wf, &wfhw);
> if (err)
> return err;
>
> - err = ops->write_waveform(chip, pwm, &wfhw);
> + err = pwm_write_waveform(chip, pwm, &wfhw);
> if (err)
> return err;
>
> @@ -548,11 +594,11 @@ static int pwm_get_state_hw(struct pwm_device *pwm, struct pwm_state *state)
>
> scoped_guard(pwmchip, chip) {
>
> - ret = ops->read_waveform(chip, pwm, &wfhw);
> + ret = pwm_read_waveform(chip, pwm, &wfhw);
> if (ret)
> return ret;
>
> - ret = ops->round_waveform_fromhw(chip, pwm, &wfhw, &wf);
> + ret = pwm_round_waveform_fromhw(chip, pwm, &wfhw, &wf);
> if (ret)
> return ret;
> }
> diff --git a/include/trace/events/pwm.h b/include/trace/events/pwm.h
> index 8022701c446d..3b5b20d2aff0 100644
> --- a/include/trace/events/pwm.h
> +++ b/include/trace/events/pwm.h
> @@ -8,15 +8,135 @@
> #include <linux/pwm.h>
> #include <linux/tracepoint.h>
>
> +#define TP_PROTO_pwm(args...) \
> + TP_PROTO(struct pwm_device *pwm, args)
> +
> +#define TP_ARGS_pwm(args...) \
> + TP_ARGS(pwm, args)
> +
> +#define TP_STRUCT__entry_pwm(args...) \
> + TP_STRUCT__entry( \
> + __field(unsigned int, chipid) \
> + __field(unsigned int, hwpwm) \
> + args)
> +
> +#define TP_fast_assign_pwm(args...) \
> + TP_fast_assign( \
> + __entry->chipid = pwm->chip->id; \
> + __entry->hwpwm = pwm->hwpwm; \
> + args)
> +
> +#define TP_printk_pwm(fmt, args...) \
> + TP_printk("pwmchip%u.%u: " fmt, __entry->chipid, __entry->hwpwm, args)
> +
> +#define __field_pwmwf(wf) \
> + __field(u64, wf ## _period_length) \
> + __field(u64, wf ## _duty_length) \
> + __field(u64, wf ## _duty_offset) \
> +
> +#define fast_assign_pwmwf(wf) \
> + __entry->wf ## _period_length = wf->period_length; \
> + __entry->wf ## _duty_length = wf->duty_length; \
> + __entry->wf ## _duty_offset = wf->duty_offset
> +
> +#define printk_pwmwf_format(wf) \
> + "%lld/%lld [+%lld]"
> +
> +#define printk_pwmwf_formatargs(wf) \
> + __entry->wf ## _duty_length, __entry->wf ## _period_length, __entry->wf ## _duty_offset
> +
> +TRACE_EVENT(pwm_round_waveform_tohw,
> +
> + TP_PROTO_pwm(const struct pwm_waveform *wf, void *wfhw, int err),
> +
> + TP_ARGS_pwm(wf, wfhw, err),
> +
> + TP_STRUCT__entry_pwm(
> + __field_pwmwf(wf)
> + __field(void *, wfhw)
> + __field(int, err)
> + ),
> +
> + TP_fast_assign_pwm(
> + fast_assign_pwmwf(wf);
> + __entry->wfhw = wfhw;
> + __entry->err = err;
> + ),
> +
> + TP_printk_pwm(printk_pwmwf_format(wf) " > %p err=%d",
> + printk_pwmwf_formatargs(wf), __entry->wfhw, __entry->err)
> +);
> +
> +TRACE_EVENT(pwm_round_waveform_fromhw,
> +
> + TP_PROTO_pwm(const void *wfhw, struct pwm_waveform *wf, int err),
> +
> + TP_ARGS_pwm(wfhw, wf, err),
> +
> + TP_STRUCT__entry_pwm(
> + __field(const void *, wfhw)
> + __field_pwmwf(wf)
> + __field(int, err)
> + ),
> +
> + TP_fast_assign_pwm(
> + __entry->wfhw = wfhw;
> + fast_assign_pwmwf(wf);
> + __entry->err = err;
> + ),
> +
> + TP_printk_pwm("%p > " printk_pwmwf_format(wf) " err=%d",
> + __entry->wfhw, printk_pwmwf_formatargs(wf), __entry->err)
> +);
> +
> +TRACE_EVENT(pwm_read_waveform,
> +
> + TP_PROTO_pwm(void *wfhw, int err),
> +
> + TP_ARGS_pwm(wfhw, err),
> +
> + TP_STRUCT__entry_pwm(
> + __field(void *, wfhw)
> + __field(int, err)
> + ),
> +
> + TP_fast_assign_pwm(
> + __entry->wfhw = wfhw;
> + __entry->err = err;
> + ),
> +
> + TP_printk_pwm("%p err=%d",
> + __entry->wfhw, __entry->err)
> +);
> +
> +TRACE_EVENT(pwm_write_waveform,
> +
> + TP_PROTO_pwm(const void *wfhw, int err),
> +
> + TP_ARGS_pwm(wfhw, err),
> +
> + TP_STRUCT__entry_pwm(
> + __field(const void *, wfhw)
> + __field(int, err)
> + ),
> +
> + TP_fast_assign_pwm(
> + __entry->wfhw = wfhw;
> + __entry->err = err;
> + ),
> +
> + TP_printk_pwm("%p err=%d",
> + __entry->wfhw, __entry->err)
> +);
> +
> +
> DECLARE_EVENT_CLASS(pwm,
>
> TP_PROTO(struct pwm_device *pwm, const struct pwm_state *state, int err),
>
> TP_ARGS(pwm, state, err),
>
> - TP_STRUCT__entry(
> - __field(unsigned int, chipid)
> - __field(unsigned int, hwpwm)
> + TP_STRUCT__entry_pwm(
> __field(u64, period)
> __field(u64, duty_cycle)
> __field(enum pwm_polarity, polarity)
> @@ -24,9 +144,7 @@ DECLARE_EVENT_CLASS(pwm,
> __field(int, err)
> ),
>
> - TP_fast_assign(
> - __entry->chipid = pwm->chip->id;
> - __entry->hwpwm = pwm->hwpwm;
> + TP_fast_assign_pwm(
> __entry->period = state->period;
> __entry->duty_cycle = state->duty_cycle;
> __entry->polarity = state->polarity;
> @@ -34,8 +152,8 @@ DECLARE_EVENT_CLASS(pwm,
> __entry->err = err;
> ),
>
> - TP_printk("pwmchip%u.%u: period=%llu duty_cycle=%llu polarity=%d enabled=%d err=%d",
> - __entry->chipid, __entry->hwpwm, __entry->period, __entry->duty_cycle,
> + TP_printk_pwm("period=%llu duty_cycle=%llu polarity=%d enabled=%d err=%d",
> + __entry->period, __entry->duty_cycle,
> __entry->polarity, __entry->enabled, __entry->err)
>
> );
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: [PATCH 4/6] pwm: Add tracing for waveform callbacks
2024-07-08 18:14 ` Trevor Gamblin
@ 2024-07-09 6:54 ` Uwe Kleine-König
0 siblings, 0 replies; 17+ messages in thread
From: Uwe Kleine-König @ 2024-07-09 6:54 UTC (permalink / raw)
To: Trevor Gamblin
Cc: linux-pwm, Steven Rostedt, Masami Hiramatsu, Mathieu Desnoyers,
linux-trace-kernel
[-- Attachment #1: Type: text/plain, Size: 153 bytes --]
On Mon, Jul 08, 2024 at 02:14:09PM -0400, Trevor Gamblin wrote:
> Missing a sign-off?
Yes, and a commit log.
Thanks for your review!
Best regards
Uwe
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: [PATCH 3/6] pwm: Add support for pwmchip devices for faster and easier userspace access
2024-07-08 10:52 ` [PATCH 3/6] pwm: Add support for pwmchip devices for faster and easier userspace access Uwe Kleine-König
2024-07-08 18:13 ` Trevor Gamblin
@ 2024-07-09 9:37 ` Nuno Sá
2024-07-12 9:48 ` Uwe Kleine-König
1 sibling, 1 reply; 17+ messages in thread
From: Nuno Sá @ 2024-07-09 9:37 UTC (permalink / raw)
To: Uwe Kleine-König, linux-pwm
On Mon, 2024-07-08 at 12:52 +0200, Uwe Kleine-König wrote:
> With this change each pwmchip can be accessed from userspace via a
> character device. Compared to the sysfs-API this is faster (on a
> stm32mp157 applying a new configuration takes approx 25% only) and
> allows to pass the whole configuration in a single ioctl allowing atomic
> application.
>
> Thanks to Randy Dunlap for pointing out a missing kernel-doc
> description.
>
> Signed-off-by: Uwe Kleine-König <u.kleine-koenig@baylibre.com>
> ---
I didn't looked very carefully at the patch but one thing did caught my
attention
...
> +
> +struct pwmchip_waveform {
> + unsigned int hwpwm;
> + __u64 period_length;
> + __u64 duty_length;
> + __u64 duty_offset;
> +};
> +
I do not think we should have holes in the struct given this is an userspace
interface.
One other thing is how likely is this struct to grow? If that is expected we
should probably think in adding some __reserved__ parameters or maybe to modify
the interface so we could make use of:
https://elixir.bootlin.com/linux/latest/source/include/linux/uaccess.h#L348
Like wrapping struct pwmchip_waveform in another struct with an extra member
forcing userspace to specify pwmchip_waveform size. But I agree it's a bit
awkward and ugly (but it could be hidden in libpwm).
Anyways, if this is not likely to change disregard all the compatibility
thingy...
- Nuno Sá
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: [PATCH 3/6] pwm: Add support for pwmchip devices for faster and easier userspace access
2024-07-09 9:37 ` Nuno Sá
@ 2024-07-12 9:48 ` Uwe Kleine-König
2024-07-12 11:01 ` Nuno Sá
0 siblings, 1 reply; 17+ messages in thread
From: Uwe Kleine-König @ 2024-07-12 9:48 UTC (permalink / raw)
To: Nuno Sá; +Cc: linux-pwm
[-- Attachment #1: Type: text/plain, Size: 2099 bytes --]
On Tue, Jul 09, 2024 at 11:37:13AM +0200, Nuno Sá wrote:
> On Mon, 2024-07-08 at 12:52 +0200, Uwe Kleine-König wrote:
> > With this change each pwmchip can be accessed from userspace via a
> > character device. Compared to the sysfs-API this is faster (on a
> > stm32mp157 applying a new configuration takes approx 25% only) and
> > allows to pass the whole configuration in a single ioctl allowing atomic
> > application.
> >
> > Thanks to Randy Dunlap for pointing out a missing kernel-doc
> > description.
> >
> > Signed-off-by: Uwe Kleine-König <u.kleine-koenig@baylibre.com>
> > ---
>
> I didn't looked very carefully at the patch but one thing did caught my
> attention
>
> ...
>
> > +
> > +struct pwmchip_waveform {
> > + unsigned int hwpwm;
> > + __u64 period_length;
> > + __u64 duty_length;
> > + __u64 duty_offset;
> > +};
> > +
>
> I do not think we should have holes in the struct given this is an userspace
> interface.
Ack, will add explicit padding (and a check that it is zeroed).
> One other thing is how likely is this struct to grow?
I don't expect it to grow. Extensions I could imagine only concern
things like:
- request the currently running period to be completed
- block until the hardware is programmed
and these don't fit into pwmchip_waveform and would require a different
ioctl command and parameter struct anyhow.
> If that is expected we should probably think in adding some
> __reserved__ parameters or maybe to modify the interface so we could
> make use of:
>
> https://elixir.bootlin.com/linux/latest/source/include/linux/uaccess.h#L348
>
> Like wrapping struct pwmchip_waveform in another struct with an extra member
> forcing userspace to specify pwmchip_waveform size. But I agree it's a bit
> awkward and ugly (but it could be hidden in libpwm).
The size is already encoded in the ioctl request constants. So I think
we're set to use copy_struct_from_user() if my expectation about
pwmchip_waveform not growing turns out to be wrong.
Best regards and thanks for your feedback,
Uwe
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: [PATCH 3/6] pwm: Add support for pwmchip devices for faster and easier userspace access
2024-07-12 9:48 ` Uwe Kleine-König
@ 2024-07-12 11:01 ` Nuno Sá
2024-07-12 16:28 ` Uwe Kleine-König
0 siblings, 1 reply; 17+ messages in thread
From: Nuno Sá @ 2024-07-12 11:01 UTC (permalink / raw)
To: Uwe Kleine-König; +Cc: linux-pwm
On Fri, 2024-07-12 at 11:48 +0200, Uwe Kleine-König wrote:
> On Tue, Jul 09, 2024 at 11:37:13AM +0200, Nuno Sá wrote:
> > On Mon, 2024-07-08 at 12:52 +0200, Uwe Kleine-König wrote:
> > > With this change each pwmchip can be accessed from userspace via a
> > > character device. Compared to the sysfs-API this is faster (on a
> > > stm32mp157 applying a new configuration takes approx 25% only) and
> > > allows to pass the whole configuration in a single ioctl allowing atomic
> > > application.
> > >
> > > Thanks to Randy Dunlap for pointing out a missing kernel-doc
> > > description.
> > >
> > > Signed-off-by: Uwe Kleine-König <u.kleine-koenig@baylibre.com>
> > > ---
> >
> > I didn't looked very carefully at the patch but one thing did caught my
> > attention
> >
> > ...
> >
> > > +
> > > +struct pwmchip_waveform {
> > > + unsigned int hwpwm;
> > > + __u64 period_length;
> > > + __u64 duty_length;
> > > + __u64 duty_offset;
> > > +};
> > > +
> >
> > I do not think we should have holes in the struct given this is an userspace
> > interface.
>
> Ack, will add explicit padding (and a check that it is zeroed).
>
Why not having the __u64 coming first :)? It also save you 4 bytes (yeah, should
not make a difference)
> > One other thing is how likely is this struct to grow?
>
> I don't expect it to grow. Extensions I could imagine only concern
> things like:
>
> - request the currently running period to be completed
> - block until the hardware is programmed
>
> and these don't fit into pwmchip_waveform and would require a different
> ioctl command and parameter struct anyhow.
>
> > If that is expected we should probably think in adding some
> > __reserved__ parameters or maybe to modify the interface so we could
> > make use of:
> >
> > https://elixir.bootlin.com/linux/latest/source/include/linux/uaccess.h#L348
> >
> > Like wrapping struct pwmchip_waveform in another struct with an extra member
> > forcing userspace to specify pwmchip_waveform size. But I agree it's a bit
> > awkward and ugly (but it could be hidden in libpwm).
>
> The size is already encoded in the ioctl request constants. So I think
> we're set to use copy_struct_from_user() if my expectation about
> pwmchip_waveform not growing turns out to be wrong.
>
Oh, indeed. I had to go and remember the IO* macros...
- Nuno Sá
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: [PATCH 3/6] pwm: Add support for pwmchip devices for faster and easier userspace access
2024-07-12 11:01 ` Nuno Sá
@ 2024-07-12 16:28 ` Uwe Kleine-König
2024-07-12 17:20 ` Nuno Sá
0 siblings, 1 reply; 17+ messages in thread
From: Uwe Kleine-König @ 2024-07-12 16:28 UTC (permalink / raw)
To: Nuno Sá; +Cc: linux-pwm
[-- Attachment #1: Type: text/plain, Size: 2762 bytes --]
On Fri, Jul 12, 2024 at 01:01:04PM +0200, Nuno Sá wrote:
> On Fri, 2024-07-12 at 11:48 +0200, Uwe Kleine-König wrote:
> > On Tue, Jul 09, 2024 at 11:37:13AM +0200, Nuno Sá wrote:
> > > On Mon, 2024-07-08 at 12:52 +0200, Uwe Kleine-König wrote:
> > > > With this change each pwmchip can be accessed from userspace via a
> > > > character device. Compared to the sysfs-API this is faster (on a
> > > > stm32mp157 applying a new configuration takes approx 25% only) and
> > > > allows to pass the whole configuration in a single ioctl allowing atomic
> > > > application.
> > > >
> > > > Thanks to Randy Dunlap for pointing out a missing kernel-doc
> > > > description.
> > > >
> > > > Signed-off-by: Uwe Kleine-König <u.kleine-koenig@baylibre.com>
> > > > ---
> > >
> > > I didn't looked very carefully at the patch but one thing did caught my
> > > attention
> > >
> > > ...
> > >
> > > > +
> > > > +struct pwmchip_waveform {
> > > > + unsigned int hwpwm;
> > > > + __u64 period_length;
> > > > + __u64 duty_length;
> > > > + __u64 duty_offset;
> > > > +};
> > > > +
> > >
> > > I do not think we should have holes in the struct given this is an userspace
> > > interface.
> >
> > Ack, will add explicit padding (and a check that it is zeroed).
> >
>
> Why not having the __u64 coming first :)? It also save you 4 bytes (yeah, should
> not make a difference)
Well no. First conceptually hwpwm should come first and second there is
https://www.kernel.org/doc/html/latest/process/botching-up-ioctls.html
which recommends:
Align everything to the natural size and use explicit padding.
32-bit platforms don’t necessarily align 64-bit values to 64-bit
boundaries, but 64-bit platforms do. So we always need padding
to the natural size to get this right.
> > > If that is expected we should probably think in adding some
> > > __reserved__ parameters or maybe to modify the interface so we could
> > > make use of:
> > >
> > > https://elixir.bootlin.com/linux/latest/source/include/linux/uaccess.h#L348
> > >
> > > Like wrapping struct pwmchip_waveform in another struct with an extra member
> > > forcing userspace to specify pwmchip_waveform size. But I agree it's a bit
> > > awkward and ugly (but it could be hidden in libpwm).
> >
> > The size is already encoded in the ioctl request constants. So I think
> > we're set to use copy_struct_from_user() if my expectation about
> > pwmchip_waveform not growing turns out to be wrong.
> >
>
> Oh, indeed. I had to go and remember the IO* macros...
I did that recently and fixed a thing I considered wrong/unintuitive:
https://lore.kernel.org/all/20240712093524.905556-2-u.kleine-koenig@baylibre.com/
Best regards
Uwe
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: [PATCH 3/6] pwm: Add support for pwmchip devices for faster and easier userspace access
2024-07-12 16:28 ` Uwe Kleine-König
@ 2024-07-12 17:20 ` Nuno Sá
0 siblings, 0 replies; 17+ messages in thread
From: Nuno Sá @ 2024-07-12 17:20 UTC (permalink / raw)
To: Uwe Kleine-König; +Cc: linux-pwm
On Fri, 2024-07-12 at 18:28 +0200, Uwe Kleine-König wrote:
> On Fri, Jul 12, 2024 at 01:01:04PM +0200, Nuno Sá wrote:
> > On Fri, 2024-07-12 at 11:48 +0200, Uwe Kleine-König wrote:
> > > On Tue, Jul 09, 2024 at 11:37:13AM +0200, Nuno Sá wrote:
> > > > On Mon, 2024-07-08 at 12:52 +0200, Uwe Kleine-König wrote:
> > > > > With this change each pwmchip can be accessed from userspace via a
> > > > > character device. Compared to the sysfs-API this is faster (on a
> > > > > stm32mp157 applying a new configuration takes approx 25% only) and
> > > > > allows to pass the whole configuration in a single ioctl allowing atomic
> > > > > application.
> > > > >
> > > > > Thanks to Randy Dunlap for pointing out a missing kernel-doc
> > > > > description.
> > > > >
> > > > > Signed-off-by: Uwe Kleine-König <u.kleine-koenig@baylibre.com>
> > > > > ---
> > > >
> > > > I didn't looked very carefully at the patch but one thing did caught my
> > > > attention
> > > >
> > > > ...
> > > >
> > > > > +
> > > > > +struct pwmchip_waveform {
> > > > > + unsigned int hwpwm;
> > > > > + __u64 period_length;
> > > > > + __u64 duty_length;
> > > > > + __u64 duty_offset;
> > > > > +};
> > > > > +
> > > >
> > > > I do not think we should have holes in the struct given this is an userspace
> > > > interface.
> > >
> > > Ack, will add explicit padding (and a check that it is zeroed).
> > >
> >
> > Why not having the __u64 coming first :)? It also save you 4 bytes (yeah, should
> > not make a difference)
>
> Well no. First conceptually hwpwm should come first and second there is
> https://www.kernel.org/doc/html/latest/process/botching-up-ioctls.html
> which recommends:
>
> Align everything to the natural size and use explicit padding.
> 32-bit platforms don’t necessarily align 64-bit values to 64-bit
> boundaries, but 64-bit platforms do. So we always need padding
> to the natural size to get this right.
>
Right, my bad! The third point about struct sizes is also valid and would fail with
my suggestion.
Thanks!
- Nuno Sá
> > >
^ permalink raw reply [flat|nested] 17+ messages in thread
end of thread, other threads:[~2024-07-12 17:20 UTC | newest]
Thread overview: 17+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-07-08 10:52 [PATCH 0/6] pwm: New abstraction and userspace API Uwe Kleine-König
2024-07-08 10:52 ` [PATCH 1/6] pwm: Add more locking Uwe Kleine-König
2024-07-08 10:52 ` [PATCH 2/6] pwm: New abstraction for PWM waveforms Uwe Kleine-König
2024-07-08 18:12 ` Trevor Gamblin
2024-07-08 10:52 ` [PATCH 3/6] pwm: Add support for pwmchip devices for faster and easier userspace access Uwe Kleine-König
2024-07-08 18:13 ` Trevor Gamblin
2024-07-09 9:37 ` Nuno Sá
2024-07-12 9:48 ` Uwe Kleine-König
2024-07-12 11:01 ` Nuno Sá
2024-07-12 16:28 ` Uwe Kleine-König
2024-07-12 17:20 ` Nuno Sá
2024-07-08 10:52 ` [PATCH 4/6] pwm: Add tracing for waveform callbacks Uwe Kleine-König
2024-07-08 18:14 ` Trevor Gamblin
2024-07-09 6:54 ` Uwe Kleine-König
2024-07-08 10:52 ` [PATCH 5/6] pwm: axi-pwmgen: Implementation of the " Uwe Kleine-König
2024-07-08 18:08 ` Trevor Gamblin
2024-07-08 10:52 ` [PATCH 6/6] pwm: stm32: " Uwe Kleine-König
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox