Skip to contentSkip to conversation

PWM: add support for ->apply, polarity and STM32

PATCH In review Ahmad Fatoum 3 participants 19 messages Mar 30, 2020 – Mar 31, 2020

Subsystems (8)
lore
Share & tools
lore
#0 lore raw

cover PWM: add support for ->apply, polarity and STM32

Hi,

- Port Linux apply API to make porting future PWM drivers easier
- Remove duplicated PWM state, so device variables can be used to query and influence actual configuration
- Add support for PWM polarity
- Add support for STM32

Cheers,
Ahmad Fatoum (12):
led: pwm: always initialize PWM LEDs as inactive
PWM: core: remove FLAG_ENABLED
PWM: core: remove ineffectual pwm_{set,get}_duty_cycle
PWM: core: group PWM state into new struct pwm_state
PWM: core: remove old PWM API in favor of Linux ->apply
PWM: core: retire pwm_set_period
PWM: core: apply initial state in of_pwm_request
video: backlight-pwm: use new pwm_apply_state API
led: pwm: use new pwm_apply_state API
PWM: core: add apply API support for polarity
of: introduce of_property_count_elems_of_size
PWM: add support for STM32

 arch/arm/dts/stm32mp151.dtsi     |  12 +
 drivers/led/led-pwm.c            |  19 +-
 drivers/mfd/Kconfig              |   7 +
 drivers/mfd/Makefile             |   1 +
 drivers/mfd/stm32-timers.c       |  74 ++++++
 drivers/of/base.c                |  32 +++
 drivers/pwm/Kconfig              |   6 +
 drivers/pwm/Makefile             |   1 +
 drivers/pwm/core.c               | 178 ++++++++++----
 drivers/pwm/pwm-imx.c            |  32 +--
 drivers/pwm/pwm-mxs.c            |  37 ++-
 drivers/pwm/pwm-stm32.c          | 400 +++++++++++++++++++++++++++++++
 drivers/pwm/pxa_pwm.c            |  93 +++----
 drivers/video/backlight-pwm.c    |  13 +-
 include/linux/mfd/stm32-timers.h |  97 ++++++++
 include/of.h                     |   8 +
 include/pwm.h                    |  62 +++--
 17 files changed, 917 insertions(+), 155 deletions(-)
 create mode 100644 drivers/mfd/stm32-timers.c
 create mode 100644 drivers/pwm/pwm-stm32.c
 create mode 100644 include/linux/mfd/stm32-timers.h
Signature · 7 lines
2.20.1


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#1 lore raw

PATCH 1/12 led: pwm: always initialize PWM LEDs as inactive

While the active-low property is respected when setting brightness, it's ignored when initializing the LED as off in the probe function and thus LEDs are active on startup. Fix this.

Fixes 769eb5e7bb ("led: pwm: support active-low property")
---
 drivers/led/led-pwm.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/led/led-pwm.c +1 -1
index 8a358dde88b3..4935572ec2d2 100644
@@ -73,7 +73,7 @@ static int led_pwm_of_probe(struct device_d *dev)
 
 		pwmled->led.set = led_pwm_set;
 
-		pwm_config(pwmled->pwm, 0, pwmled->period);
+		led_pwm_set(&pwmled->led, 0);
 		pwm_enable(pwmled->pwm);
 
 		ret = led_register(&pwmled->led);
Signature · 7 lines
2.20.1


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#2 lore raw

PATCH 2/12 PWM: core: remove FLAG_ENABLED

In reply to Ahmad Fatoum

From: Ahmad Fatoum <[email protected]>

In preparation for moving to a struct pwm_state like Linux does, turn the flag into a variable.

---
 drivers/pwm/core.c | 12 ++++++++----
 include/pwm.h      |  2 ++
 2 files changed, 10 insertions(+), 4 deletions(-)
drivers/pwm/core.c +8 -4
index c8016999f0d2..cae23c98ee2d 100644
@@ -24,7 +24,6 @@ struct pwm_device {
 	struct			pwm_chip *chip;
 	unsigned long		flags;
 #define FLAG_REQUESTED	0
-#define FLAG_ENABLED	1
 	struct list_head	node;
 	struct device_d		*hwdev;
 	struct device_d		dev;
@@ -283,8 +282,10 @@ int pwm_enable(struct pwm_device *pwm)
 {
 	pwm->p_enable = 1;
 
-	if (!test_and_set_bit(FLAG_ENABLED, &pwm->flags))
+	if (!pwm->chip->p_enable) {
+		pwm->chip->p_enable = 1;
 		return pwm->chip->ops->enable(pwm->chip);
+	}
 
 	return 0;
 }
@@ -297,7 +298,10 @@ void pwm_disable(struct pwm_device *pwm)
 {
 	pwm->p_enable = 0;
 
-	if (test_and_clear_bit(FLAG_ENABLED, &pwm->flags))
-		pwm->chip->ops->disable(pwm->chip);
+	if (!pwm->chip->p_enable)
+		return;
+
+	pwm->chip->p_enable = 0;
+	pwm->chip->ops->disable(pwm->chip);
 }
 EXPORT_SYMBOL_GPL(pwm_disable);
include/pwm.h +2 -0
index ca01f5b53d1c..98af1299748a 100644
@@ -63,6 +63,7 @@ struct pwm_ops {
  * @ops: The callbacks for this PWM
  * @duty_ns: The duty cycle of the PWM, in nano-seconds
  * @period_ns: The period of the PWM, in nano-seconds
+ * @p_enable: whether the PWM is enabled
  */
 struct pwm_chip {
 	int			id;
@@ -70,6 +71,7 @@ struct pwm_chip {
 	const struct pwm_ops	*ops;
 	int			duty_ns;
 	int			period_ns;
+	int			p_enable;
 };
 
 int pwmchip_add(struct pwm_chip *chip, struct device_d *dev);
Signature · 7 lines
2.20.1


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#3 lore raw

PATCH 3/12 PWM: core: remove ineffectual pwm_{set,get}_duty_cycle

In reply to Ahmad Fatoum

From: Ahmad Fatoum <[email protected]>

The setter sets a value unused anywhere, but in the getter. As the functions are unused, just drop them. There's pwm_config that can be used for this instead.

---
 drivers/pwm/core.c | 10 ----------
 include/pwm.h      |  2 --
 2 files changed, 12 deletions(-)
drivers/pwm/core.c +0 -10
index cae23c98ee2d..eaf7de18295e 100644
@@ -265,16 +265,6 @@ unsigned int pwm_get_period(struct pwm_device *pwm)
 	return pwm->period_ns;
 }
 
-void pwm_set_duty_cycle(struct pwm_device *pwm, unsigned int duty_ns)
-{
-	pwm->duty_ns = duty_ns;
-}
-
-unsigned int pwm_get_duty_cycle(struct pwm_device *pwm)
-{
-	return pwm->duty_ns;
-}
-
 /*
  * pwm_enable - start a PWM output toggling
  */
include/pwm.h +0 -2
index 98af1299748a..9897e86545c9 100644
@@ -34,8 +34,6 @@ void pwm_disable(struct pwm_device *pwm);
 
 void pwm_set_period(struct pwm_device *pwm, unsigned int period);
 unsigned int pwm_get_period(struct pwm_device *pwm);
-void pwm_set_duty_cycle(struct pwm_device *pwm, unsigned int duty);
-unsigned int pwm_get_duty_cycle(struct pwm_device *pwm);
 
 struct pwm_chip;
 
Signature · 7 lines
2.20.1


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#4 lore raw

PATCH 4/12 PWM: core: group PWM state into new struct pwm_state

In reply to Ahmad Fatoum

From: Ahmad Fatoum <[email protected]>

As a prerequisite for moving to the new apply API, we need to group all state into one struct that we can apply at once. Prepare for this.

---
 drivers/pwm/core.c | 34 ++++++++++++++++------------------
 include/pwm.h      | 21 +++++++++++++++------
 2 files changed, 31 insertions(+), 24 deletions(-)
drivers/pwm/core.c +16 -18
index eaf7de18295e..8d3bfa3bb259 100644
@@ -28,9 +28,7 @@ struct pwm_device {
 	struct device_d		*hwdev;
 	struct device_d		dev;
 
-	unsigned int		duty_ns;
-	unsigned int		period_ns;
-	unsigned int		p_enable;
+	struct pwm_state	params;
 };
 
 static LIST_HEAD(pwm_list);
@@ -51,7 +49,7 @@ static int set_duty_period_ns(struct param_d *p, void *priv)
 {
 	struct pwm_device *pwm = priv;
 
-	pwm_config(pwm, pwm->chip->duty_ns, pwm->chip->period_ns);
+	pwm_config(pwm, pwm->params.duty_ns, pwm->params.period_ns);
 
 	return 0;
 }
@@ -60,7 +58,7 @@ static int set_enable(struct param_d *p, void *priv)
 {
 	struct pwm_device *pwm = priv;
 
-	if (pwm->p_enable)
+	if (pwm->params.p_enable)
 		pwm_enable(pwm);
 	else
 		pwm_disable(pwm);
@@ -99,17 +97,17 @@ int pwmchip_add(struct pwm_chip *chip, struct device_d *dev)
 	list_add_tail(&pwm->node, &pwm_list);
 
 	p = dev_add_param_uint32(&pwm->dev, "duty_ns", set_duty_period_ns,
-			NULL, &pwm->chip->duty_ns, "%u", pwm);
+			NULL, &pwm->params.duty_ns, "%u", pwm);
 	if (IS_ERR(p))
 		return PTR_ERR(p);
 
 	p = dev_add_param_uint32(&pwm->dev, "period_ns", set_duty_period_ns,
-			NULL, &pwm->chip->period_ns, "%u", pwm);
+			NULL, &pwm->params.period_ns, "%u", pwm);
 	if (IS_ERR(p))
 		return PTR_ERR(p);
 
 	p = dev_add_param_bool(&pwm->dev, "enable", set_enable,
-			NULL, &pwm->p_enable, pwm);
+			NULL, &pwm->params.p_enable, pwm);
 	if (IS_ERR(p))
 		return PTR_ERR(p);
 
@@ -242,8 +240,8 @@ EXPORT_SYMBOL_GPL(pwm_free);
  */
 int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns)
 {
-	pwm->chip->duty_ns = duty_ns;
-	pwm->chip->period_ns = period_ns;
+	pwm->chip->state.duty_ns = duty_ns;
+	pwm->chip->state.period_ns = period_ns;
 
 	if (period_ns == 0)
 		return -EINVAL;
@@ -257,12 +255,12 @@ EXPORT_SYMBOL_GPL(pwm_config);
 
 void pwm_set_period(struct pwm_device *pwm, unsigned int period_ns)
 {
-	pwm->period_ns = period_ns;
+	pwm->chip->state.period_ns = period_ns;
 }
 
 unsigned int pwm_get_period(struct pwm_device *pwm)
 {
-	return pwm->period_ns;
+	return pwm->chip->state.period_ns;
 }
 
 /*
@@ -270,10 +268,10 @@ unsigned int pwm_get_period(struct pwm_device *pwm)
  */
 int pwm_enable(struct pwm_device *pwm)
 {
-	pwm->p_enable = 1;
+	pwm->params.p_enable = 1;
 
-	if (!pwm->chip->p_enable) {
-		pwm->chip->p_enable = 1;
+	if (!pwm->chip->state.p_enable) {
+		pwm->chip->state.p_enable = 1;
 		return pwm->chip->ops->enable(pwm->chip);
 	}
 
@@ -286,12 +284,12 @@ EXPORT_SYMBOL_GPL(pwm_enable);
  */
 void pwm_disable(struct pwm_device *pwm)
 {
-	pwm->p_enable = 0;
+	pwm->params.p_enable = 0;
 
-	if (!pwm->chip->p_enable)
+	if (!pwm->chip->state.p_enable)
 		return;
 
-	pwm->chip->p_enable = 0;
+	pwm->chip->state.p_enable = 0;
 	pwm->chip->ops->disable(pwm->chip);
 }
 EXPORT_SYMBOL_GPL(pwm_disable);
include/pwm.h +15 -6
index 9897e86545c9..c0ce414cb536 100644
@@ -37,6 +37,18 @@ unsigned int pwm_get_period(struct pwm_device *pwm);
 
 struct pwm_chip;
 
+/*
+ * struct pwm_state - state of a PWM channel
+ * @period_ns: PWM period (in nanoseconds)
+ * @duty_ns: PWM duty cycle (in nanoseconds)
+ * @p_enable: PWM enabled status
+ */
+struct pwm_state {
+	unsigned int period_ns;
+	unsigned int duty_ns;
+	unsigned int p_enable;
+};
+
 /**
  * struct pwm_ops - PWM operations
  * @request: optional hook for requesting a PWM
@@ -59,17 +71,14 @@ struct pwm_ops {
  * @id: The id of this pwm
  * @devname: unique identifier for this pwm
  * @ops: The callbacks for this PWM
- * @duty_ns: The duty cycle of the PWM, in nano-seconds
- * @period_ns: The period of the PWM, in nano-seconds
- * @p_enable: whether the PWM is enabled
+ * @state: current state of the PWM
  */
 struct pwm_chip {
 	int			id;
 	const char		*devname;
 	const struct pwm_ops	*ops;
-	int			duty_ns;
-	int			period_ns;
-	int			p_enable;
+
+	struct pwm_state	state;
 };
 
 int pwmchip_add(struct pwm_chip *chip, struct device_d *dev);
Signature · 7 lines
2.20.1


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#5 lore raw

PATCH 5/12 PWM: core: remove old PWM API in favor of Linux ->apply

In reply to Ahmad Fatoum

Linux has the new atomic PWM API in addition to the old one for backward compatibility. We only have three PWM drivers in here, so port them over to the new ->apply API.

---
 drivers/pwm/core.c    | 66 +++++++++++++++++++++---------
 drivers/pwm/pwm-imx.c | 32 ++++++---------
 drivers/pwm/pwm-mxs.c | 37 +++++++----------
 drivers/pwm/pxa_pwm.c | 93 +++++++++++++++++++++++--------------------
 include/pwm.h         | 48 ++++++++++++++--------
 5 files changed, 156 insertions(+), 120 deletions(-)
drivers/pwm/core.c +47 -19
index 8d3bfa3bb259..89b9756d0a2d 100644
@@ -49,9 +49,7 @@ static int set_duty_period_ns(struct param_d *p, void *priv)
 {
 	struct pwm_device *pwm = priv;
 
-	pwm_config(pwm, pwm->params.duty_ns, pwm->params.period_ns);
-
-	return 0;
+	return pwm_apply_state(pwm, &pwm->params);
 }
 
 static int set_enable(struct param_d *p, void *priv)
@@ -235,21 +233,50 @@ void pwm_free(struct pwm_device *pwm)
 }
 EXPORT_SYMBOL_GPL(pwm_free);
 
+void pwm_get_state(const struct pwm_device *pwm,
+		   struct pwm_state *state)
+{
+	*state = pwm->chip->state;
+}
+EXPORT_SYMBOL_GPL(pwm_get_state);
+
+int pwm_apply_state(struct pwm_device *pwm, const struct pwm_state *state)
+{
+	struct pwm_chip *chip = pwm->chip;
+	int ret = -EINVAL;
+
+	if (state->period_ns == 0)
+		goto err;
+
+	if (state->duty_ns > state->period_ns)
+		goto err;
+
+	ret = chip->ops->apply(chip, state);
+err:
+	if (ret == 0)
+		chip->state = *state;
+
+	pwm->params = chip->state;
+	return ret;
+}
+
 /*
  * pwm_config - change a PWM device configuration
  */
 int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns)
 {
-	pwm->chip->state.duty_ns = duty_ns;
-	pwm->chip->state.period_ns = period_ns;
+	struct pwm_state state;
 
-	if (period_ns == 0)
+	if (duty_ns < 0 || period_ns < 0)
 		return -EINVAL;
 
-	if (duty_ns > period_ns)
-		return -EINVAL;
+	pwm_get_state(pwm, &state);
+	if (state.duty_ns == duty_ns && state.period_ns == period_ns)
+		return 0;
 
-	return pwm->chip->ops->config(pwm->chip, duty_ns, period_ns);
+	state.duty_ns = duty_ns;
+	state.period_ns = period_ns;
+	return pwm_apply_state(pwm, &state);
 }
 EXPORT_SYMBOL_GPL(pwm_config);
 
@@ -268,14 +295,14 @@ unsigned int pwm_get_period(struct pwm_device *pwm)
  */
 int pwm_enable(struct pwm_device *pwm)
 {
-	pwm->params.p_enable = 1;
+	struct pwm_state state;
 
-	if (!pwm->chip->state.p_enable) {
-		pwm->chip->state.p_enable = 1;
-		return pwm->chip->ops->enable(pwm->chip);
-	}
+	pwm_get_state(pwm, &state);
+	if (state.p_enable)
+		return 0;
 
-	return 0;
+	state.p_enable = true;
+	return pwm_apply_state(pwm, &state);
 }
 EXPORT_SYMBOL_GPL(pwm_enable);
 
@@ -284,12 +311,13 @@ EXPORT_SYMBOL_GPL(pwm_enable);
  */
 void pwm_disable(struct pwm_device *pwm)
 {
-	pwm->params.p_enable = 0;
+	struct pwm_state state;
 
-	if (!pwm->chip->state.p_enable)
+	pwm_get_state(pwm, &state);
+	if (!state.p_enable)
 		return;
 
-	pwm->chip->state.p_enable = 0;
-	pwm->chip->ops->disable(pwm->chip);
+	state.p_enable = false;
+	pwm_apply_state(pwm, &state);
 }
 EXPORT_SYMBOL_GPL(pwm_disable);
drivers/pwm/pwm-imx.c +13 -19
index b620e502f262..8407b2f5e1bd 100644
@@ -155,37 +155,31 @@ static void imx_pwm_set_enable_v2(struct pwm_chip *chip, bool enable)
 	writel(val, imx->mmio_base + MX3_PWMCR);
 }
 
-static int imx_pwm_config(struct pwm_chip *chip,
-		int duty_ns, int period_ns)
+static int imx_pwm_apply(struct pwm_chip *chip, const struct pwm_state *state)
 {
 	struct imx_chip *imx = to_imx_chip(chip);
+	bool enabled;
 	int ret;
 
-	ret = imx->config(chip, duty_ns, period_ns);
+	enabled = chip->state.p_enable;
 
-	return ret;
-}
+	if (enabled && !state->p_enable) {
+		imx->set_enable(chip, false);
+		return 0;
+	}
 
-static int imx_pwm_enable(struct pwm_chip *chip)
-{
-	struct imx_chip *imx = to_imx_chip(chip);
+	ret = imx->config(chip, state->duty_ns, state->period_ns);
+	if (ret)
+		return ret;
 
-	imx->set_enable(chip, true);
+	if (!enabled && state->p_enable)
+		imx->set_enable(chip, true);
 
 	return 0;
 }
 
-static void imx_pwm_disable(struct pwm_chip *chip)
-{
-	struct imx_chip *imx = to_imx_chip(chip);
-
-	imx->set_enable(chip, false);
-}
-
 static struct pwm_ops imx_pwm_ops = {
-	.enable = imx_pwm_enable,
-	.disable = imx_pwm_disable,
-	.config = imx_pwm_config,
+	.apply = imx_pwm_apply,
 };
 
 struct imx_pwm_data {
drivers/pwm/pwm-mxs.c +15 -22
index e72f1dbcb0df..a06040ac3268 100644
@@ -52,18 +52,26 @@ struct mxs_pwm {
 
 #define to_mxs_pwm_chip(_chip) container_of(_chip, struct mxs_pwm_chip, chip)
 
-static int mxs_pwm_config(struct pwm_chip *chip, int duty_ns, int period_ns)
+static int mxs_pwm_apply(struct pwm_chip *chip, const struct pwm_state *state)
 {
 	struct mxs_pwm_chip *mxs = to_mxs_pwm_chip(chip);
 	int div = 0;
 	unsigned int period_cycles, duty_cycles;
 	unsigned long rate;
 	unsigned long long c;
+	bool enabled;
+
+	enabled = chip->state.p_enable;
+
+	if (enabled && !state->p_enable) {
+		writel(1 << mxs->chip.id, mxs->mxs->base + PWM_CTRL + CLR);
+		return 0;
+	}
 
 	rate = clk_get_rate(mxs->mxs->clk);
 	while (1) {
 		c = rate / cdiv[div];
-		c = c * period_ns;
+		c = c * state->period_ns;
 		do_div(c, 1000000000);
 		if (c < PERIOD_PERIOD_MAX)
 			break;
@@ -73,8 +81,8 @@ static int mxs_pwm_config(struct pwm_chip *chip, int duty_ns, int period_ns)
 	}
 
 	period_cycles = c;
-	c *= duty_ns;
-	do_div(c, period_ns);
+	c *= state->duty_ns;
+	do_div(c, state->period_ns);
 	duty_cycles = c;
 
 	writel(duty_cycles << 16,
@@ -83,29 +91,14 @@ static int mxs_pwm_config(struct pwm_chip *chip, int duty_ns, int period_ns)
 	       PERIOD_INACTIVE_LOW | PERIOD_CDIV(div),
 			mxs->mxs->base + PWM_PERIOD0 + mxs->chip.id * 0x20);
 
-	return 0;
-}
-
-static int mxs_pwm_enable(struct pwm_chip *chip)
-{
-	struct mxs_pwm_chip *mxs = to_mxs_pwm_chip(chip);
-
-	writel(1 << mxs->chip.id, mxs->mxs->base + PWM_CTRL + SET);
+	if (!enabled && state->p_enable)
+		writel(1 << mxs->chip.id, mxs->mxs->base + PWM_CTRL + SET);
 
 	return 0;
 }
 
-static void mxs_pwm_disable(struct pwm_chip *chip)
-{
-	struct mxs_pwm_chip *mxs = to_mxs_pwm_chip(chip);
-
-	writel(1 << mxs->chip.id, mxs->mxs->base + PWM_CTRL + CLR);
-}
-
 static struct pwm_ops mxs_pwm_ops = {
-	.config = mxs_pwm_config,
-	.enable = mxs_pwm_enable,
-	.disable = mxs_pwm_disable,
+	.apply = mxs_pwm_apply,
 };
 
 static int mxs_pwm_probe(struct device_d *dev)
drivers/pwm/pxa_pwm.c +50 -43

129 lines not on this page · raw message

include/pwm.h +31 -17

76 lines not on this page · raw message

Signature · 7 lines
2.20.1


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#6 lore raw

PATCH 6/12 PWM: core: retire pwm_set_period

In reply to Ahmad Fatoum

From: Ahmad Fatoum <[email protected]>

Client code can use pwm_apply_state or pwm_config to set period as well as duty cycle. Having a pwm_set_period thus doesn't add any value.

---
 drivers/pwm/core.c | 7 +------
 include/pwm.h      | 1 -
 2 files changed, 1 insertion(+), 7 deletions(-)
drivers/pwm/core.c +1 -6
index 89b9756d0a2d..b42b3092a3f8 100644
@@ -213,7 +213,7 @@ struct pwm_device *of_pwm_request(struct device_node *np, const char *con_id)
 	}
 
 	if (args.args_count > 1)
-		pwm_set_period(pwm, args.args[1]);
+		pwm->chip->state.period_ns = args.args[1];
 
 	ret = __pwm_request(pwm);
 	if (ret)
@@ -280,11 +280,6 @@ int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns)
 }
 EXPORT_SYMBOL_GPL(pwm_config);
 
-void pwm_set_period(struct pwm_device *pwm, unsigned int period_ns)
-{
-	pwm->chip->state.period_ns = period_ns;
-}
-
 unsigned int pwm_get_period(struct pwm_device *pwm)
 {
 	return pwm->chip->state.period_ns;
include/pwm.h +0 -1
index 7431ecfb42bf..36b1eb8131fb 100644
@@ -49,7 +49,6 @@ int pwm_enable(struct pwm_device *pwm);
  */
 void pwm_disable(struct pwm_device *pwm);
 
-void pwm_set_period(struct pwm_device *pwm, unsigned int period);
 unsigned int pwm_get_period(struct pwm_device *pwm);
 
 struct pwm_chip;
Signature · 7 lines
2.20.1


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#7 lore raw

PATCH 7/12 PWM: core: apply initial state in of_pwm_request

In reply to Ahmad Fatoum

This functions prepares a state that can later be tweaked and applied to the PWM device with pwm_apply_state(). This is a convenient function that first retrieves the current PWM state and the replaces the period with the reference values defined in pwm->args.
Once the function returns, you can adjust the ->enabled and ->duty_cycle fields according to your needs before calling pwm_apply_state().

---
 drivers/pwm/core.c | 70 +++++++++++++++++++++++++++++++++++++++++++---
 include/pwm.h      |  6 ++++
 2 files changed, 72 insertions(+), 4 deletions(-)
drivers/pwm/core.c +66 -4
index b42b3092a3f8..9206fc0b9eda 100644
@@ -20,6 +20,23 @@
 #include <linux/list.h>
 #include <linux/err.h>
 
+/**
+ * struct pwm_args - board-dependent PWM arguments
+ * @period_ns: reference period
+ *
+ * This structure describes board-dependent arguments attached to a PWM
+ * device. These arguments are usually retrieved from the PWM lookup table or
+ * device tree.
+ *
+ * Do not confuse this with the PWM state: PWM arguments represent the initial
+ * configuration that users want to use on this PWM device rather than the
+ * current PWM hardware state.
+ */
+
+struct pwm_args {
+	unsigned int period_ns;
+};
+
 struct pwm_device {
 	struct			pwm_chip *chip;
 	unsigned long		flags;
@@ -29,6 +46,7 @@ struct pwm_device {
 	struct device_d		dev;
 
 	struct pwm_state	params;
+	struct pwm_args		args;
 };
 
 static LIST_HEAD(pwm_list);
@@ -194,6 +212,7 @@ struct pwm_device *of_pwm_request(struct device_node *np, const char *con_id)
 	struct of_phandle_args args;
 	int index = 0;
 	struct pwm_device *pwm;
+	struct pwm_state state;
 	int ret;
 
 	if (con_id)
@@ -213,11 +232,17 @@ struct pwm_device *of_pwm_request(struct device_node *np, const char *con_id)
 	}
 
 	if (args.args_count > 1)
-		pwm->chip->state.period_ns = args.args[1];
+		pwm->args.period_ns = args.args[1];
 
 	ret = __pwm_request(pwm);
 	if (ret)
-		return ERR_PTR(-ret);
+		return ERR_PTR(ret);
+
+	pwm_init_state(pwm, &state);
+
+	ret = pwm_apply_state(pwm, &state);
+	if (ret)
+		return ERR_PTR(ret);
 
 	return pwm;
 }
@@ -233,13 +258,50 @@ void pwm_free(struct pwm_device *pwm)
 }
 EXPORT_SYMBOL_GPL(pwm_free);
 
-void pwm_get_state(const struct pwm_device *pwm,
-		   struct pwm_state *state)
+void pwm_get_state(const struct pwm_device *pwm, struct pwm_state *state)
 {
 	*state = pwm->chip->state;
 }
 EXPORT_SYMBOL_GPL(pwm_get_state);
 
+static void pwm_get_args(const struct pwm_device *pwm, struct pwm_args *args)
+{
+	*args = pwm->args;
+}
+
+/**
+ * pwm_init_state() - prepare a new state to be applied with pwm_apply_state()
+ * @pwm: PWM device
+ * @state: state to fill with the prepared PWM state
+ *
+ * This functions prepares a state that can later be tweaked and applied
+ * to the PWM device with pwm_apply_state(). This is a convenient function
+ * that first retrieves the current PWM state and the replaces the period
+ * with the reference values defined in pwm->args.
+ * Once the function returns, you can adjust the ->enabled and ->duty_cycle
+ * fields according to your needs before calling pwm_apply_state().
+ *
+ * ->duty_cycle is initially set to zero to avoid cases where the current
+ * ->duty_cycle value exceed the pwm_args->period one, which would trigger
+ * an error if the user calls pwm_apply_state() without adjusting ->duty_cycle
+ * first.
+ */
+void pwm_init_state(const struct pwm_device *pwm,
+		    struct pwm_state *state)
+{
+	struct pwm_args args;
+
+	/* First get the current state. */
+	pwm_get_state(pwm, state);
+
+	/* Then fill it with the reference config */
+	pwm_get_args(pwm, &args);
+
+	state->period_ns = args.period_ns;
+	state->duty_ns = 0;
+}
+EXPORT_SYMBOL_GPL(pwm_init_state);
+
 int pwm_apply_state(struct pwm_device *pwm, const struct pwm_state *state)
 {
 	struct pwm_chip *chip = pwm->chip;
include/pwm.h +6 -0
index 36b1eb8131fb..67ea0f9bcb05 100644
@@ -29,6 +29,12 @@ struct pwm_device *of_pwm_request(struct device_node *np, const char *con_id);
  */
 void pwm_free(struct pwm_device *pwm);
 
+/*
+ * pwm_init_state - prepare a new state from device tree args
+ */
+void pwm_init_state(const struct pwm_device *pwm,
+		    struct pwm_state *state);
+
 /*
  * pwm_config - change a PWM device configuration
  */
Signature · 7 lines
2.20.1


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#8 lore raw

PATCH 8/12 video: backlight-pwm: use new pwm_apply_state API

In reply to Ahmad Fatoum

From: Ahmad Fatoum <[email protected]>

Use pwm_apply_state we can avoid having to store PWM state in the instance structure and in future we have an easy way to support new parameters like inverted duty cycle.

---
 drivers/video/backlight-pwm.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)
drivers/video/backlight-pwm.c +8 -5
index 9111a42d7544..8b6494dac929 100644
@@ -33,7 +33,6 @@ struct pwm_backlight {
 	struct backlight_device backlight;
 	struct pwm_device *pwm;
 	struct regulator *power;
-	uint32_t period;
 	unsigned int *levels;
 	int enable_gpio;
 	int enable_active_high;
@@ -91,13 +90,16 @@ static int backlight_pwm_disable(struct pwm_backlight *pwm_backlight)
 static int compute_duty_cycle(struct pwm_backlight *pwm_backlight, int brightness)
 {
 	int duty_cycle;
+	struct pwm_state state;
+
+	pwm_get_state(pwm_backlight->pwm, &state);
 
 	if (pwm_backlight->levels)
 		duty_cycle = pwm_backlight->levels[brightness];
 	else
 		duty_cycle = brightness;
 
-	return duty_cycle * pwm_backlight->period / pwm_backlight->scale;
+	return duty_cycle * state.period_ns / pwm_backlight->scale;
 }
 
 static int backlight_pwm_set(struct backlight_device *backlight,
@@ -105,9 +107,11 @@ static int backlight_pwm_set(struct backlight_device *backlight,
 {
 	struct pwm_backlight *pwm_backlight = container_of(backlight,
 			struct pwm_backlight, backlight);
+	struct pwm_state state;
 
-	pwm_config(pwm_backlight->pwm, compute_duty_cycle(pwm_backlight, brightness),
-		   pwm_backlight->period);
+	pwm_get_state(pwm_backlight->pwm, &state);
+	state.duty_ns = compute_duty_cycle(pwm_backlight, brightness);
+	pwm_apply_state(pwm_backlight->pwm, &state);
 
 	if (brightness)
 		return backlight_pwm_enable(pwm_backlight);
@@ -192,7 +196,6 @@ static int backlight_pwm_of_probe(struct device_d *dev)
 
 	pwm_backlight = xzalloc(sizeof(*pwm_backlight));
 	pwm_backlight->pwm = pwm;
-	pwm_backlight->period = pwm_get_period(pwm);
 
 	ret = pwm_backlight_parse_dt(dev, pwm_backlight);
 	if (ret)
Signature · 7 lines
2.20.1


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
maintainer
#9 lore raw

Re: [PATCH 08/12] video: backlight-pwm: use new pwm_apply_state API

Quoted 42 lines · Ahmad Fatoum

From: Ahmad Fatoum <[email protected]>

Use pwm_apply_state we can avoid having to store PWM state in the instance structure and in future we have an easy way to support new parameters like inverted duty cycle.

---
 drivers/video/backlight-pwm.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)
drivers/video/backlight-pwm.c +4 -2
index 9111a42d7544..8b6494dac929 100644
@@ -33,7 +33,6 @@ struct pwm_backlight {
 	struct backlight_device backlight;
 	struct pwm_device *pwm;
 	struct regulator *power;
-	uint32_t period;
 	unsigned int *levels;
 	int enable_gpio;
 	int enable_active_high;
@@ -91,13 +90,16 @@ static int backlight_pwm_disable(struct pwm_backlight *pwm_backlight)
 static int compute_duty_cycle(struct pwm_backlight *pwm_backlight, int brightness)
 {
 	int duty_cycle;
+	struct pwm_state state;
+
+	pwm_get_state(pwm_backlight->pwm, &state);
 
 	if (pwm_backlight->levels)
 		duty_cycle = pwm_backlight->levels[brightness];
 	else
 		duty_cycle = brightness;
 
-	return duty_cycle * pwm_backlight->period / pwm_backlight->scale;
+	return duty_cycle * state.period_ns / pwm_backlight->scale;
 }
 
 static int backlight_pwm_set(struct backlight_device *backlight,

drivers/video/backlight-pwm.c static int backlight_pwm_set(struct backlight_device *backlight, · line 112

Quoted 8 lines
drivers/video/backlight-pwm.c +2 -2
@@ -105,9 +107,11 @@ static int backlight_pwm_set(struct backlight_device *backlight,
 {
 	struct pwm_backlight *pwm_backlight = container_of(backlight,
 			struct pwm_backlight, backlight);
+	struct pwm_state state;
 
-	pwm_config(pwm_backlight->pwm, compute_duty_cycle(pwm_backlight, brightness),
-		   pwm_backlight->period);
+	pwm_get_state(pwm_backlight->pwm, &state);

You read the current pwm state here...

drivers/video/backlight-pwm.c static int backlight_pwm_set(struct backlight_device *backlight, · line 113

Quoted 1 line
drivers/video/backlight-pwm.c +1 -0
+	state.duty_ns = compute_duty_cycle(pwm_backlight, brightness);

and once again in compute_duty_cycle(). I think it would be nicer to reorganize this a bit, maybe pass the state to compute_duty_cycle.

drivers/video/backlight-pwm.c static int backlight_pwm_set(struct backlight_device *backlight, · line 117

Quoted 4 lines
drivers/video/backlight-pwm.c +1 -0
+	pwm_apply_state(pwm_backlight->pwm, &state);
 
 	if (brightness)
 		return backlight_pwm_enable(pwm_backlight);

I would assume that if you switch to the pwm_apply_state API then you do it entirely. backlight_pwm_enable() still uses the old API to enable the PWM.

Sascha

Signature · 9 lines
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#10 lore raw

Re: [PATCH 08/12] video: backlight-pwm: use new pwm_apply_state API

Hi,

Quoted 59 lines · Sascha Hauer
Quoted 42 lines · Ahmad Fatoum

From: Ahmad Fatoum <[email protected]>

Use pwm_apply_state we can avoid having to store PWM state in the instance structure and in future we have an easy way to support new parameters like inverted duty cycle.

---
 drivers/video/backlight-pwm.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)
drivers/video/backlight-pwm.c +4 -2
index 9111a42d7544..8b6494dac929 100644
@@ -33,7 +33,6 @@ struct pwm_backlight {
 	struct backlight_device backlight;
 	struct pwm_device *pwm;
 	struct regulator *power;
-	uint32_t period;
 	unsigned int *levels;
 	int enable_gpio;
 	int enable_active_high;
@@ -91,13 +90,16 @@ static int backlight_pwm_disable(struct pwm_backlight *pwm_backlight)
 static int compute_duty_cycle(struct pwm_backlight *pwm_backlight, int brightness)
 {
 	int duty_cycle;
+	struct pwm_state state;
+
+	pwm_get_state(pwm_backlight->pwm, &state);
 
 	if (pwm_backlight->levels)
 		duty_cycle = pwm_backlight->levels[brightness];
 	else
 		duty_cycle = brightness;
 
-	return duty_cycle * pwm_backlight->period / pwm_backlight->scale;
+	return duty_cycle * state.period_ns / pwm_backlight->scale;
 }
 
 static int backlight_pwm_set(struct backlight_device *backlight,

drivers/video/backlight-pwm.c static int backlight_pwm_set(struct backlight_device *backlight, · line 112

Quoted 8 lines
drivers/video/backlight-pwm.c +2 -2
@@ -105,9 +107,11 @@ static int backlight_pwm_set(struct backlight_device *backlight,
 {
 	struct pwm_backlight *pwm_backlight = container_of(backlight,
 			struct pwm_backlight, backlight);
+	struct pwm_state state;
 
-	pwm_config(pwm_backlight->pwm, compute_duty_cycle(pwm_backlight, brightness),
-		   pwm_backlight->period);
+	pwm_get_state(pwm_backlight->pwm, &state);

You read the current pwm state here...

drivers/video/backlight-pwm.c static int backlight_pwm_set(struct backlight_device *backlight, · line 113

Quoted 1 line
drivers/video/backlight-pwm.c +1 -0
+	state.duty_ns = compute_duty_cycle(pwm_backlight, brightness);

and once again in compute_duty_cycle(). I think it would be nicer to reorganize this a bit, maybe pass the state to compute_duty_cycle.

Quoted 8 lines

drivers/video/backlight-pwm.c static int backlight_pwm_set(struct backlight_device *backlight, · line 117

Quoted 4 lines
drivers/video/backlight-pwm.c +1 -0
+	pwm_apply_state(pwm_backlight->pwm, &state);
 
 	if (brightness)
 		return backlight_pwm_enable(pwm_backlight);

I would assume that if you switch to the pwm_apply_state API then you do it entirely. backlight_pwm_enable() still uses the old API to enable the PWM.

Ineed. Rest of patches should apply and build cleanly without this and the last one. If they're ok, can you apply them and I respin only those two?

Cheers
Ahmad

Quoted 3 lines

Sascha

Signature · 9 lines
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
maintainer
#11 lore raw

Re: [PATCH 08/12] video: backlight-pwm: use new pwm_apply_state API

Quoted 71 lines · Ahmad Fatoum

Hi,

Quoted 59 lines · Sascha Hauer
Quoted 42 lines · Ahmad Fatoum

From: Ahmad Fatoum <[email protected]>

Use pwm_apply_state we can avoid having to store PWM state in the instance structure and in future we have an easy way to support new parameters like inverted duty cycle.

---
 drivers/video/backlight-pwm.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)
drivers/video/backlight-pwm.c +4 -2
index 9111a42d7544..8b6494dac929 100644
@@ -33,7 +33,6 @@ struct pwm_backlight {
 	struct backlight_device backlight;
 	struct pwm_device *pwm;
 	struct regulator *power;
-	uint32_t period;
 	unsigned int *levels;
 	int enable_gpio;
 	int enable_active_high;
@@ -91,13 +90,16 @@ static int backlight_pwm_disable(struct pwm_backlight *pwm_backlight)
 static int compute_duty_cycle(struct pwm_backlight *pwm_backlight, int brightness)
 {
 	int duty_cycle;
+	struct pwm_state state;
+
+	pwm_get_state(pwm_backlight->pwm, &state);
 
 	if (pwm_backlight->levels)
 		duty_cycle = pwm_backlight->levels[brightness];
 	else
 		duty_cycle = brightness;
 
-	return duty_cycle * pwm_backlight->period / pwm_backlight->scale;
+	return duty_cycle * state.period_ns / pwm_backlight->scale;
 }
 
 static int backlight_pwm_set(struct backlight_device *backlight,

drivers/video/backlight-pwm.c static int backlight_pwm_set(struct backlight_device *backlight, · line 112

Quoted 8 lines
drivers/video/backlight-pwm.c +2 -2
@@ -105,9 +107,11 @@ static int backlight_pwm_set(struct backlight_device *backlight,
 {
 	struct pwm_backlight *pwm_backlight = container_of(backlight,
 			struct pwm_backlight, backlight);
+	struct pwm_state state;
 
-	pwm_config(pwm_backlight->pwm, compute_duty_cycle(pwm_backlight, brightness),
-		   pwm_backlight->period);
+	pwm_get_state(pwm_backlight->pwm, &state);

You read the current pwm state here...

drivers/video/backlight-pwm.c static int backlight_pwm_set(struct backlight_device *backlight, · line 113

Quoted 1 line
drivers/video/backlight-pwm.c +1 -0
+	state.duty_ns = compute_duty_cycle(pwm_backlight, brightness);

and once again in compute_duty_cycle(). I think it would be nicer to reorganize this a bit, maybe pass the state to compute_duty_cycle.

Quoted 8 lines

drivers/video/backlight-pwm.c static int backlight_pwm_set(struct backlight_device *backlight, · line 117

Quoted 4 lines
drivers/video/backlight-pwm.c +1 -0
+	pwm_apply_state(pwm_backlight->pwm, &state);
 
 	if (brightness)
 		return backlight_pwm_enable(pwm_backlight);

I would assume that if you switch to the pwm_apply_state API then you do it entirely. backlight_pwm_enable() still uses the old API to enable the PWM.

Quoted 2 lines

Ineed. Rest of patches should apply and build cleanly without this and the last one. If they're ok, can you apply them and I respin only those two?

Just did that.

Sascha

Signature · 9 lines
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#12 lore raw

PATCH 9/12 led: pwm: use new pwm_apply_state API

In reply to Ahmad Fatoum

From: Ahmad Fatoum <[email protected]>

To support PWM_POLARITY_INVERTED for PWM LEDs, we need to to use the apply API. Do so.

---
 drivers/led/led-pwm.c | 17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)
drivers/led/led-pwm.c +10 -7
index 4935572ec2d2..19d9d4d48a3d 100644
@@ -29,22 +29,27 @@ struct pwmled {
 	bool active_low;
 	struct led led;
 	struct pwm_device *pwm;
-	uint32_t period;
 };
 
 static void led_pwm_set(struct led *led, unsigned int brightness)
 {
 	struct pwmled *pwmled = container_of(led, struct pwmled, led);
-	unsigned long long duty =  pwmled->period;
+	unsigned long long duty;
+	struct pwm_state state;
 	unsigned int max = pwmled->led.max_value;
 
-        duty *= brightness;
+	pwm_get_state(pwmled->pwm, &state);
+
+	duty = state.period_ns * brightness;
         do_div(duty, max);
 
 	if (pwmled->active_low)
-		duty = pwmled->period - duty;
+		duty = state.period_ns - duty;
+
+	state.p_enable = true;
+	state.duty_ns = duty;
 
-	pwm_config(pwmled->pwm, duty, pwmled->period);
+	pwm_apply_state(pwmled->pwm, &state);
 }
 
 static int led_pwm_of_probe(struct device_d *dev)
@@ -68,13 +73,11 @@ static int led_pwm_of_probe(struct device_d *dev)
 		if (ret)
 			return ret;
 
-		pwmled->period = pwm_get_period(pwmled->pwm);
 		pwmled->active_low = of_property_read_bool(child, "active-low");
 
 		pwmled->led.set = led_pwm_set;
 
 		led_pwm_set(&pwmled->led, 0);
-		pwm_enable(pwmled->pwm);
 
 		ret = led_register(&pwmled->led);
 		if (ret)
Signature · 7 lines
2.20.1


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#13 lore raw

PATCH 10/12 PWM: core: add apply API support for polarity

In reply to Ahmad Fatoum

From: Ahmad Fatoum <[email protected]>

Some PWM chips support outputting an inverted PWM signal. Add API support for this.

---
 drivers/pwm/core.c | 21 +++++++++++++++++----
 include/pwm.h      |  6 ++++++
 2 files changed, 23 insertions(+), 4 deletions(-)
drivers/pwm/core.c +17 -4
index 9206fc0b9eda..05dad93e5ce3 100644
@@ -23,6 +23,7 @@
 /**
  * struct pwm_args - board-dependent PWM arguments
  * @period_ns: reference period
+ * @polarity: reference polarity
  *
  * This structure describes board-dependent arguments attached to a PWM
  * device. These arguments are usually retrieved from the PWM lookup table or
@@ -35,6 +36,7 @@
 
 struct pwm_args {
 	unsigned int period_ns;
+	unsigned int polarity;
 };
 
 struct pwm_device {
@@ -63,7 +65,7 @@ static struct pwm_device *_find_pwm(const char *devname)
 	return NULL;
 }
 
-static int set_duty_period_ns(struct param_d *p, void *priv)
+static int apply_params(struct param_d *p, void *priv)
 {
 	struct pwm_device *pwm = priv;
 
@@ -112,12 +114,12 @@ int pwmchip_add(struct pwm_chip *chip, struct device_d *dev)
 
 	list_add_tail(&pwm->node, &pwm_list);
 
-	p = dev_add_param_uint32(&pwm->dev, "duty_ns", set_duty_period_ns,
+	p = dev_add_param_uint32(&pwm->dev, "duty_ns", apply_params,
 			NULL, &pwm->params.duty_ns, "%u", pwm);
 	if (IS_ERR(p))
 		return PTR_ERR(p);
 
-	p = dev_add_param_uint32(&pwm->dev, "period_ns", set_duty_period_ns,
+	p = dev_add_param_uint32(&pwm->dev, "period_ns", apply_params,
 			NULL, &pwm->params.period_ns, "%u", pwm);
 	if (IS_ERR(p))
 		return PTR_ERR(p);
@@ -127,6 +129,11 @@ int pwmchip_add(struct pwm_chip *chip, struct device_d *dev)
 	if (IS_ERR(p))
 		return PTR_ERR(p);
 
+	p = dev_add_param_bool(&pwm->dev, "inverted", apply_params,
+			       NULL, &pwm->params.polarity, pwm);
+	if (IS_ERR(p))
+		return PTR_ERR(p);
+
 	return 0;
 }
 EXPORT_SYMBOL_GPL(pwmchip_add);
@@ -234,6 +241,11 @@ struct pwm_device *of_pwm_request(struct device_node *np, const char *con_id)
 	if (args.args_count > 1)
 		pwm->args.period_ns = args.args[1];
 
+	pwm->args.polarity = PWM_POLARITY_NORMAL;
+
+	if (args.args_count > 2 && args.args[2] & PWM_POLARITY_INVERTED)
+		pwm->args.polarity = PWM_POLARITY_INVERTED;
+
 	ret = __pwm_request(pwm);
 	if (ret)
 		return ERR_PTR(ret);
@@ -277,7 +289,7 @@ static void pwm_get_args(const struct pwm_device *pwm, struct pwm_args *args)
  * This functions prepares a state that can later be tweaked and applied
  * to the PWM device with pwm_apply_state(). This is a convenient function
  * that first retrieves the current PWM state and the replaces the period
- * with the reference values defined in pwm->args.
+ * and polarity fields with the reference values defined in pwm->args.
  * Once the function returns, you can adjust the ->enabled and ->duty_cycle
  * fields according to your needs before calling pwm_apply_state().
  *
@@ -298,6 +310,7 @@ void pwm_init_state(const struct pwm_device *pwm,
 	pwm_get_args(pwm, &args);
 
 	state->period_ns = args.period_ns;
+	state->polarity = args.polarity;
 	state->duty_ns = 0;
 }
 EXPORT_SYMBOL_GPL(pwm_init_state);
include/pwm.h +6 -0
index 67ea0f9bcb05..b67ab13d2e2d 100644
@@ -2,18 +2,24 @@
 #ifndef __PWM_H
 #define __PWM_H
 
+#include <dt-bindings/pwm/pwm.h>
+
 struct pwm_device;
 struct device_d;
 
+#define	PWM_POLARITY_NORMAL	0
+
 /*
  * struct pwm_state - state of a PWM channel
  * @period_ns: PWM period (in nanoseconds)
  * @duty_ns: PWM duty cycle (in nanoseconds)
+ * @polarity: PWM polarity
  * @p_enable: PWM enabled status
  */
 struct pwm_state {
 	unsigned int period_ns;
 	unsigned int duty_ns;
+	unsigned int polarity;
 	unsigned int p_enable;
 };
 
Signature · 7 lines
2.20.1


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#14 lore raw

PATCH 11/12 of: introduce of_property_count_elems_of_size

In reply to Ahmad Fatoum

From: Ahmad Fatoum <[email protected]>

Import the Linux helper, so code using it may be more easily ported.

---
 drivers/of/base.c | 32 ++++++++++++++++++++++++++++++++
 include/of.h      |  8 ++++++++
 2 files changed, 40 insertions(+)
drivers/of/base.c +32 -0
index 6d13b66a0e3e..346d72e1e09e 100644
@@ -738,6 +738,38 @@ int of_property_read_u32_index(const struct device_node *np,
 }
 EXPORT_SYMBOL_GPL(of_property_read_u32_index);
 
+/**
+ * of_property_count_elems_of_size - Count the number of elements in a property
+ *
+ * @np:		device node from which the property value is to be read.
+ * @propname:	name of the property to be searched.
+ * @elem_size:	size of the individual element
+ *
+ * Search for a property in a device node and count the number of elements of
+ * size elem_size in it. Returns number of elements on sucess, -EINVAL if the
+ * property does not exist or its length does not match a multiple of elem_size
+ * and -ENODATA if the property does not have a value.
+ */
+int of_property_count_elems_of_size(const struct device_node *np,
+				const char *propname, int elem_size)
+{
+	struct property *prop = of_find_property(np, propname, NULL);
+
+	if (!prop)
+		return -EINVAL;
+	if (!prop->value)
+		return -ENODATA;
+
+	if (prop->length % elem_size != 0) {
+		pr_err("size of %s in node %pOF is not a multiple of %d\n",
+		       propname, np, elem_size);
+		return -EINVAL;
+	}
+
+	return prop->length / elem_size;
+}
+EXPORT_SYMBOL_GPL(of_property_count_elems_of_size);
+
 /**
  * of_property_read_u8_array - Find and read an array of u8 from a property.
  *
include/of.h +8 -0
index 8d6c018b73ec..eea5abae459b 100644
@@ -186,6 +186,8 @@ extern struct device_node *of_find_node_by_reproducible_name(struct device_node
 extern int of_property_read_u32_index(const struct device_node *np,
 				       const char *propname,
 				       u32 index, u32 *out_value);
+extern int of_property_count_elems_of_size(const struct device_node *np,
+				const char *propname, int elem_size);
 extern int of_property_read_u8_array(const struct device_node *np,
 			const char *propname, u8 *out_values, size_t sz);
 extern int of_property_read_u16_array(const struct device_node *np,
@@ -425,6 +427,12 @@ static inline int of_property_read_u32_index(const struct device_node *np,
 	return -ENOSYS;
 }
 
+static inline int of_property_count_elems_of_size(const struct device_node *np,
+			const char *propname, int elem_size)
+{
+	return -ENOSYS;
+}
+
 static inline int of_property_read_u8_array(const struct device_node *np,
 				const char *propname, u8 *out_values, size_t sz)
 {
Signature · 7 lines
2.20.1


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#15 lore raw

PATCH 12/12 PWM: add support for STM32

In reply to Ahmad Fatoum

From: Ahmad Fatoum <[email protected]>

This driver adds support for PWM driver on STM32 platform based on the Linux v5.4 support.

---
 arch/arm/dts/stm32mp151.dtsi     |  12 +
 drivers/mfd/Kconfig              |   7 +
 drivers/mfd/Makefile             |   1 +
 drivers/mfd/stm32-timers.c       |  74 ++++++
 drivers/pwm/Kconfig              |   6 +
 drivers/pwm/Makefile             |   1 +
 drivers/pwm/pwm-stm32.c          | 400 +++++++++++++++++++++++++++++++
 include/linux/mfd/stm32-timers.h |  97 ++++++++
 8 files changed, 598 insertions(+)
 create mode 100644 drivers/mfd/stm32-timers.c
 create mode 100644 drivers/pwm/pwm-stm32.c
 create mode 100644 include/linux/mfd/stm32-timers.h
arch/arm/dts/stm32mp151.dtsi +12 -0
index 2a70a747e76e..a0870c446f45 100644
@@ -21,6 +21,18 @@
 		mmc0 = &sdmmc1;
 		mmc1 = &sdmmc2;
 		mmc2 = &sdmmc3;
+		pwm1 = &{/soc/timer@44000000/pwm};
+		pwm2 = &{/soc/timer@40000000/pwm};
+		pwm3 = &{/soc/timer@40001000/pwm};
+		pwm4 = &{/soc/timer@40002000/pwm};
+		pwm5 = &{/soc/timer@40003000/pwm};
+		pwm8 = &{/soc/timer@44001000/pwm};
+		pwm12 = &{/soc/timer@40006000/pwm};
+		pwm13 = &{/soc/timer@40007000/pwm};
+		pwm14 = &{/soc/timer@40008000/pwm};
+		pwm15 = &{/soc/timer@44006000/pwm};
+		pwm16 = &{/soc/timer@44007000/pwm};
+		pwm17 = &{/soc/timer@44008000/pwm};
 	};
 
 	psci {
drivers/mfd/Kconfig +7 -0
index f4cc71ef0ec8..ddf117712e77 100644
@@ -82,4 +82,11 @@ config SMSC_SUPERIO
        help
          Select this to probe for IO-port connected SMSC Super I/O chips.
 
+config MFD_STM32_TIMERS
+	bool "STM32 Timers"
+	depends on ARCH_STM32MP
+	help
+	  Select this to get regmap support for the timer blocks on STM32
+	  MCUs and MPUs.
+
 endmenu
drivers/mfd/Makefile +1 -0
index 0c24493e3d86..a3b296a803f8 100644
@@ -15,3 +15,4 @@ obj-$(CONFIG_MFD_STPMIC1)	+= stpmic1.o
 obj-$(CONFIG_MFD_SUPERIO)	+= superio.o
 obj-$(CONFIG_FINTEK_SUPERIO)	+= fintek-superio.o
 obj-$(CONFIG_SMSC_SUPERIO)	+= smsc-superio.o
+obj-$(CONFIG_MFD_STM32_TIMERS)	+= stm32-timers.o
drivers/mfd/stm32-timers.c new file +74 -0

74 lines not on this page · raw message

drivers/pwm/Kconfig +6 -0
index 97c3deff107b..9268aac9122a 100644
@@ -28,4 +28,10 @@ config PWM_MXS
 	help
 	  This enables PWM support for Freescale i.MX23/i.MX28 SoCs
 
+config PWM_STM32
+	bool "STM32 PWM Support"
+	depends on ARCH_STM32MP
+	help
+	  This enables PWM support for STM32 MCUs and MPUs.
+
 endif
drivers/pwm/Makefile +1 -0
index 46865a24eeb4..c0a27becefcd 100644
@@ -2,3 +2,4 @@ obj-$(CONFIG_PWM)		+= core.o
 obj-$(CONFIG_PWM_PXA)		+= pxa_pwm.o
 obj-$(CONFIG_PWM_IMX)		+= pwm-imx.o
 obj-$(CONFIG_PWM_MXS)		+= pwm-mxs.o
+obj-$(CONFIG_PWM_STM32)		+= pwm-stm32.o
drivers/pwm/pwm-stm32.c new file +400 -0

400 lines not on this page · raw message

include/linux/mfd/stm32-timers.h new file +97 -0

97 lines not on this page · raw message

Signature · 7 lines
2.20.1


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
maintainer
#16 lore raw

Re: [PATCH 12/12] PWM: add support for STM32

Quoted 30 lines · Ahmad Fatoum

From: Ahmad Fatoum <[email protected]>

This driver adds support for PWM driver on STM32 platform based on the Linux v5.4 support.

---
 arch/arm/dts/stm32mp151.dtsi     |  12 +
 drivers/mfd/Kconfig              |   7 +
 drivers/mfd/Makefile             |   1 +
 drivers/mfd/stm32-timers.c       |  74 ++++++
 drivers/pwm/Kconfig              |   6 +
 drivers/pwm/Makefile             |   1 +
 drivers/pwm/pwm-stm32.c          | 400 +++++++++++++++++++++++++++++++
 include/linux/mfd/stm32-timers.h |  97 ++++++++
 8 files changed, 598 insertions(+)
 create mode 100644 drivers/mfd/stm32-timers.c
 create mode 100644 drivers/pwm/pwm-stm32.c
 create mode 100644 include/linux/mfd/stm32-timers.h
arch/arm/dts/stm32mp151.dtsi +2 -0
index 2a70a747e76e..a0870c446f45 100644
@@ -21,6 +21,18 @@
 		mmc0 = &sdmmc1;
 		mmc1 = &sdmmc2;
 		mmc2 = &sdmmc3;
+		pwm1 = &{/soc/timer@44000000/pwm};
+		pwm2 = &{/soc/timer@40000000/pwm};

arch/arm/dts/stm32mp151.dtsi line 35

Quoted 10 lines
arch/arm/dts/stm32mp151.dtsi +10 -0
+		pwm3 = &{/soc/timer@40001000/pwm};
+		pwm4 = &{/soc/timer@40002000/pwm};
+		pwm5 = &{/soc/timer@40003000/pwm};
+		pwm8 = &{/soc/timer@44001000/pwm};
+		pwm12 = &{/soc/timer@40006000/pwm};
+		pwm13 = &{/soc/timer@40007000/pwm};
+		pwm14 = &{/soc/timer@40008000/pwm};
+		pwm15 = &{/soc/timer@44006000/pwm};
+		pwm16 = &{/soc/timer@44007000/pwm};
+		pwm17 = &{/soc/timer@44008000/pwm};

The other aliases start counting at 0. Why not the PWMs?

Sascha

Signature · 9 lines
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#17 lore raw

Re: [PATCH 12/12] PWM: add support for STM32

Hello,

Quoted 6 lines · Sascha Hauer
Quoted 6 lines
arch/arm/dts/stm32mp151.dtsi +6 -0
+		pwm1 = &{/soc/timer@44000000/pwm};
+		pwm2 = &{/soc/timer@40000000/pwm};
+		pwm3 = &{/soc/timer@40001000/pwm};
+		pwm4 = &{/soc/timer@40002000/pwm};
+		pwm5 = &{/soc/timer@40003000/pwm};
+		pwm8 = &{/soc/timer@44001000/pwm};
Quoted 8 lines

arch/arm/dts/stm32mp151.dtsi line 35

Quoted 6 lines
arch/arm/dts/stm32mp151.dtsi +6 -0
+		pwm12 = &{/soc/timer@40006000/pwm};
+		pwm13 = &{/soc/timer@40007000/pwm};
+		pwm14 = &{/soc/timer@40008000/pwm};
+		pwm15 = &{/soc/timer@44006000/pwm};
+		pwm16 = &{/soc/timer@44007000/pwm};
+		pwm17 = &{/soc/timer@44008000/pwm};

The other aliases start counting at 0. Why not the PWMs?

I named them to match the SoC data sheet,

The final device names are e.g. pwm12ch2, corresponding to TIM12_CH2 in the SoC datasheet. It's IMO the best user experience, because otherwise you'll always have to look at the aliases in barebox to determine, which PWM corresponds to what's printed in the schematics.

Cheers
Amad

Signature · 9 lines
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
maintainer
#18 lore raw

Re: [PATCH 12/12] PWM: add support for STM32

Quoted 18 lines · Ahmad Fatoum

Hello,

Quoted 6 lines · Sascha Hauer
Quoted 6 lines
arch/arm/dts/stm32mp151.dtsi +6 -0
+		pwm1 = &{/soc/timer@44000000/pwm};
+		pwm2 = &{/soc/timer@40000000/pwm};
+		pwm3 = &{/soc/timer@40001000/pwm};
+		pwm4 = &{/soc/timer@40002000/pwm};
+		pwm5 = &{/soc/timer@40003000/pwm};
+		pwm8 = &{/soc/timer@44001000/pwm};
Quoted 8 lines

arch/arm/dts/stm32mp151.dtsi line 35

Quoted 6 lines
arch/arm/dts/stm32mp151.dtsi +6 -0
+		pwm12 = &{/soc/timer@40006000/pwm};
+		pwm13 = &{/soc/timer@40007000/pwm};
+		pwm14 = &{/soc/timer@40008000/pwm};
+		pwm15 = &{/soc/timer@44006000/pwm};
+		pwm16 = &{/soc/timer@44007000/pwm};
+		pwm17 = &{/soc/timer@44008000/pwm};

The other aliases start counting at 0. Why not the PWMs?

Quoted 6 lines

I named them to match the SoC data sheet,

The final device names are e.g. pwm12ch2, corresponding to TIM12_CH2 in the SoC datasheet. It's IMO the best user experience, because otherwise you'll always have to look at the aliases in barebox to determine, which PWM corresponds to what's printed in the schematics.

ok.

Sascha

Signature · 9 lines
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox