Skip to contentSkip to conversation

pwm: Add DesignWare PWM Controller Driver

PATCH Superseded Jarkko Nikula 3 participants 5 messages Apr 8, 2020 – Apr 9, 2020 PWM

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 pwm: Add DesignWare PWM Controller Driver

Introduce driver for Synopsys DesignWare PWM Controller used on Intel Elkhart Lake.

Initial implementation is done by Felipe Balbi while he was working at Intel with later changes from Raymond Tan and me.

---
 drivers/pwm/Kconfig   |   9 ++
 drivers/pwm/Makefile  |   1 +
 drivers/pwm/pwm-dwc.c | 328 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 338 insertions(+)
 create mode 100644 drivers/pwm/pwm-dwc.c
drivers/pwm/Kconfig +9 -0
index 30190beeb6e9..cfded9efdb17 100644
@@ -150,6 +150,15 @@ config PWM_CROS_EC
 	  PWM driver for exposing a PWM attached to the ChromeOS Embedded
 	  Controller.
 
+config PWM_DWC
+	tristate "DesignWare PWM Controller"
+	depends on PCI
+	help
+	  PWM driver for Synopsys DWC PWM Controller attached to a PCI bus.
+
+	  To compile this driver as a module, choose M here: the module
+	  will be called pwm-dwc.
+
 config PWM_EP93XX
 	tristate "Cirrus Logic EP93xx PWM support"
 	depends on ARCH_EP93XX
drivers/pwm/Makefile +1 -0
index 9a475073dafc..eb52012f0e79 100644
@@ -13,6 +13,7 @@ obj-$(CONFIG_PWM_BRCMSTB)	+= pwm-brcmstb.o
 obj-$(CONFIG_PWM_CLPS711X)	+= pwm-clps711x.o
 obj-$(CONFIG_PWM_CRC)		+= pwm-crc.o
 obj-$(CONFIG_PWM_CROS_EC)	+= pwm-cros-ec.o
+obj-$(CONFIG_PWM_DWC)		+= pwm-dwc.o
 obj-$(CONFIG_PWM_EP93XX)	+= pwm-ep93xx.o
 obj-$(CONFIG_PWM_FSL_FTM)	+= pwm-fsl-ftm.o
 obj-$(CONFIG_PWM_HIBVT)		+= pwm-hibvt.o
drivers/pwm/pwm-dwc.c new file +328 -0

328 lines not on this page · raw message

Signature · 1 line
2.25.1
#1 lore raw

Hi Jarkko,

Quoted 9 lines · Jarkko Nikula

Introduce driver for Synopsys DesignWare PWM Controller used on Intel Elkhart Lake.

Initial implementation is done by Felipe Balbi while he was working at Intel with later changes from Raymond Tan and me.

Thanks for sending this. FWIW:

Signature · 1 line
balbi
#2 lore raw
In reply to Jarkko Nikula

Hello Jarkko,

Quoted 54 lines · Jarkko Nikula

Introduce driver for Synopsys DesignWare PWM Controller used on Intel Elkhart Lake.

Initial implementation is done by Felipe Balbi while he was working at Intel with later changes from Raymond Tan and me.

---
 drivers/pwm/Kconfig   |   9 ++
 drivers/pwm/Makefile  |   1 +
 drivers/pwm/pwm-dwc.c | 328 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 338 insertions(+)
 create mode 100644 drivers/pwm/pwm-dwc.c
drivers/pwm/Kconfig +9 -0
index 30190beeb6e9..cfded9efdb17 100644
@@ -150,6 +150,15 @@ config PWM_CROS_EC
 	  PWM driver for exposing a PWM attached to the ChromeOS Embedded
 	  Controller.
 
+config PWM_DWC
+	tristate "DesignWare PWM Controller"
+	depends on PCI
+	help
+	  PWM driver for Synopsys DWC PWM Controller attached to a PCI bus.
+
+	  To compile this driver as a module, choose M here: the module
+	  will be called pwm-dwc.
+
 config PWM_EP93XX
 	tristate "Cirrus Logic EP93xx PWM support"
 	depends on ARCH_EP93XX
drivers/pwm/Makefile +1 -0
index 9a475073dafc..eb52012f0e79 100644
@@ -13,6 +13,7 @@ obj-$(CONFIG_PWM_BRCMSTB)	+= pwm-brcmstb.o
 obj-$(CONFIG_PWM_CLPS711X)	+= pwm-clps711x.o
 obj-$(CONFIG_PWM_CRC)		+= pwm-crc.o
 obj-$(CONFIG_PWM_CROS_EC)	+= pwm-cros-ec.o
+obj-$(CONFIG_PWM_DWC)		+= pwm-dwc.o
 obj-$(CONFIG_PWM_EP93XX)	+= pwm-ep93xx.o
 obj-$(CONFIG_PWM_FSL_FTM)	+= pwm-fsl-ftm.o
 obj-$(CONFIG_PWM_HIBVT)		+= pwm-hibvt.o
drivers/pwm/pwm-dwc.c new file +0 -0
new file mode 100644
index 000000000000..962d504c1281

drivers/pwm/pwm-dwc.c line 7

Quoted 7 lines
drivers/pwm/pwm-dwc.c new file +7 -0
@@ -0,0 +1,328 @@
+// SPDX-License-Identifier: GPL-2.0
+/**
+ * DesignWare PWM Controller driver
+ *
+ * Copyright (C) 2018-2020 Intel Corporation
+ *
+ * Author: Felipe Balbi

Is there a publicly available reference manual available? If so, adding a link here would be great.

Quoted 36 lines
drivers/pwm/pwm-dwc.c new file +36 -0
+ */
+
+#include <linux/bitops.h>
+#include <linux/export.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/pci.h>
+#include <linux/pm_runtime.h>
+#include <linux/pwm.h>
+#include <linux/mutex.h>
+#include <linux/pm_runtime.h>
+
+#define DWC_TIM_LD_CNT(n)	((n) * 0x14)
+#define DWC_TIM_LD_CNT2(n)	(((n) * 4) + 0xb0)
+#define DWC_TIM_CUR_VAL(n)	(((n) * 0x14) + 0x04)
+#define DWC_TIM_CTRL(n)		(((n) * 0x14) + 0x08)
+#define DWC_TIM_EOI(n)		(((n) * 0x14) + 0x0c)
+#define DWC_TIM_INT_STS(n)	(((n) * 0x14) + 0x10)
+
+#define DWC_TIMERS_INT_STS	0xa0
+#define DWC_TIMERS_EOI		0xa4
+#define DWC_TIMERS_RAW_INT_STS	0xa8
+#define DWC_TIMERS_COMP_VERSION	0xac
+
+#define DWC_TIMERS_TOTAL	8
+
+/* Timer Control Register */
+#define DWC_TIM_CTRL_EN		BIT(0)
+#define DWC_TIM_CTRL_MODE	BIT(1)
+#define DWC_TIM_CTRL_MODE_FREE	(0 << 1)
+#define DWC_TIM_CTRL_MODE_USER	(1 << 1)
+#define DWC_TIM_CTRL_INT_MASK	BIT(2)
+#define DWC_TIM_CTRL_PWM	BIT(3)
+
+struct dwc_pwm_driver_data {
+	unsigned long clk_period_ns;

drivers/pwm/pwm-dwc.c line 53

Quoted 10 lines
drivers/pwm/pwm-dwc.c new file +10 -0
+	int npwm;
+};
+
+struct dwc_pwm {
+	struct pwm_chip chip;
+	struct device *dev;
+	struct mutex lock;
+
+	unsigned long clk_period_ns;
+	unsigned int version;

version is a write-only variable, this can be dropped.

Quoted 5 lines
drivers/pwm/pwm-dwc.c new file +5 -0
+
+	void __iomem *base;
+
+	u32 saved_registers[24];
+};

drivers/pwm/pwm-dwc.c line 68

Quoted 10 lines
drivers/pwm/pwm-dwc.c new file +10 -0
+#define to_dwc_pwm(p)	(container_of((p), struct dwc_pwm, chip))
+
+static inline u32 dwc_pwm_readl(void __iomem *base, u32 offset)
+{
+	return readl(base + offset);
+}
+
+static inline void dwc_pwm_writel(void __iomem *base, u32 offset, u32 value)
+{
+	writel(value, base + offset);

I think it's unfortunate that the order of parameters is different for dwc_pwm_writel and writel.

drivers/pwm/pwm-dwc.c line 80

Quoted 12 lines
drivers/pwm/pwm-dwc.c new file +12 -0
+}
+
+static void __dwc_pwm_configure(struct dwc_pwm *dwc, int pwm,
+				unsigned int duty_ns,
+				unsigned int period_ns)
+{
+	u32 ctrl;
+	u32 high;
+	u32 low;
+
+	high = DIV_ROUND_CLOSEST(duty_ns, dwc->clk_period_ns) - 1;
+	low = DIV_ROUND_CLOSEST(period_ns - duty_ns, dwc->clk_period_ns) - 1;

Have you tested your driver with CONFIG_PWM_DEBUG? I would expect that using ROUND_CLOSEST triggers

.apply is supposed to round down period

in some situations.

Maybe soon duty_ns and period_ns will become 64 bit integers. I suggest to already cope for this today and introduce proper range checking.

Quoted 15 lines
drivers/pwm/pwm-dwc.c new file +15 -0
+	dwc_pwm_writel(dwc->base, DWC_TIM_LD_CNT(pwm), low);
+	dwc_pwm_writel(dwc->base, DWC_TIM_LD_CNT2(pwm), high);
+
+	ctrl = DWC_TIM_CTRL_MODE_USER | DWC_TIM_CTRL_PWM;
+	dwc_pwm_writel(dwc->base, DWC_TIM_CTRL(pwm), ctrl);
+}
+
+static u32 __dwc_pwm_duty_ns(struct dwc_pwm *dwc, int pwm)
+{
+	u32 duty;
+
+	duty = dwc_pwm_readl(dwc->base, DWC_TIM_LD_CNT2(pwm));
+	duty += 1;
+	duty *= dwc->clk_period_ns;
+

drivers/pwm/pwm-dwc.c line 106

Quoted 10 lines
drivers/pwm/pwm-dwc.c new file +10 -0
+	return duty;
+}
+
+static u32 __dwc_pwm_period_ns(struct dwc_pwm *dwc, int pwm, u32 duty)
+{
+	u32 period;
+
+	period = dwc_pwm_readl(dwc->base, DWC_TIM_LD_CNT(pwm));
+	period += 1;
+	period *= dwc->clk_period_ns;

Can this overflow? (Hmm, I wonder what should be done about this as .get_state() returns void.)

Quoted 13 lines
drivers/pwm/pwm-dwc.c new file +13 -0
+	period += duty;
+
+	return period;
+}
+
+static bool __dwc_pwm_is_enabled(struct dwc_pwm *dwc, int pwm)
+{
+	return !!(dwc_pwm_readl(dwc->base,
+				DWC_TIM_CTRL(pwm)) & DWC_TIM_CTRL_EN);
+}
+
+static void __dwc_pwm_set_enable(struct dwc_pwm *dwc, int pwm, int enabled)
+{

drivers/pwm/pwm-dwc.c line 129

Quoted 10 lines
drivers/pwm/pwm-dwc.c new file +10 -0
+	u32 reg;
+
+	reg = dwc_pwm_readl(dwc->base, DWC_TIM_CTRL(pwm));
+
+	if (enabled)
+		reg |= DWC_TIM_CTRL_EN;
+	else
+		reg &= ~DWC_TIM_CTRL_EN;
+
+	dwc_pwm_writel(dwc->base, DWC_TIM_CTRL(pwm), reg);

What happens to the output if the EN bit is cleared? Does the hardware complete the currently running period? If not, does the output "freeze" or get inactive?

drivers/pwm/pwm-dwc.c line 138

Quoted 9 lines
drivers/pwm/pwm-dwc.c new file +9 -0
+}
+
+static void __dwc_pwm_configure_timer(struct dwc_pwm *dwc,
+				      struct pwm_device *pwm,
+				      const struct pwm_state *state)
+{
+	__dwc_pwm_set_enable(dwc, pwm->hwpwm, false);
+	__dwc_pwm_configure(dwc, pwm->hwpwm, state->duty_cycle,
+			    state->period);

Is it necessary to stop the hardware before writing the counter registers? Or is this only done to prevent race conditions that yield unexpected cycles?

drivers/pwm/pwm-dwc.c line 150

Quoted 12 lines
drivers/pwm/pwm-dwc.c new file +12 -0
+	__dwc_pwm_set_enable(dwc, pwm->hwpwm, state->enabled);
+}
+
+static int dwc_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
+			 const struct pwm_state *state)
+{
+	struct dwc_pwm *dwc = to_dwc_pwm(chip);
+
+	mutex_lock(&dwc->lock);
+
+	if (state->enabled) {
+		if (!pwm_is_enabled(pwm))

If pwm_is_enabled() would be changed to call .get_state() you'd run in a dead lock. Please don't call PWM API functions in driver callbacks.

drivers/pwm/pwm-dwc.c line 158

Quoted 8 lines
drivers/pwm/pwm-dwc.c new file +8 -0
+			pm_runtime_get_sync(dwc->dev);
+		__dwc_pwm_configure_timer(dwc, pwm, state);
+	} else {
+		if (pwm_is_enabled(pwm)) {
+			__dwc_pwm_set_enable(dwc, pwm->hwpwm, false);
+			pm_runtime_put_sync(dwc->dev);
+		}
+	}

You don't check state->polarity here.

Quoted 8 lines
drivers/pwm/pwm-dwc.c new file +8 -0
+
+	mutex_unlock(&dwc->lock);
+
+	return 0;
+}
+
+static void dwc_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
+			      struct pwm_state *state)

drivers/pwm/pwm-dwc.c line 176

Quoted 10 lines
drivers/pwm/pwm-dwc.c new file +10 -0
+{
+	struct dwc_pwm *dwc = to_dwc_pwm(chip);
+
+	pm_runtime_get_sync(dwc->dev);
+
+	mutex_lock(&dwc->lock);
+	state->enabled = __dwc_pwm_is_enabled(dwc, pwm->hwpwm);
+	state->duty_cycle = __dwc_pwm_duty_ns(dwc, pwm->hwpwm);
+	state->period = __dwc_pwm_period_ns(dwc, pwm->hwpwm,
+					    state->duty_cycle);

I wonder if doing the math in .get_state() directly instead of introducing a function with IMHO strange semantics that is only used once would be easier to understand.

drivers/pwm/pwm-dwc.c line 185

Quoted 9 lines
drivers/pwm/pwm-dwc.c new file +9 -0
+	mutex_unlock(&dwc->lock);
+
+	pm_runtime_put_sync(dwc->dev);
+}
+
+static const struct pwm_ops dwc_pwm_ops = {
+	.apply		= dwc_pwm_apply,
+	.get_state	= dwc_pwm_get_state,
+	.owner		= THIS_MODULE,

I personally don't like aligning the = in structure initializers. There seem to be different tastes involved. If you don't care, please use just one space before and after the = please.

Quoted 12 lines
drivers/pwm/pwm-dwc.c new file +12 -0
+};
+
+static int dwc_pwm_probe(struct pci_dev *pci, const struct pci_device_id *id)
+{
+	struct dwc_pwm_driver_data *data;
+	struct dwc_pwm *dwc;
+	struct device *dev;
+	int ret;
+	int i;
+
+	data = (struct dwc_pwm_driver_data *) id->driver_data;
+	dev = &pci->dev;

drivers/pwm/pwm-dwc.c line 207

Quoted 10 lines
drivers/pwm/pwm-dwc.c new file +10 -0
+
+	dwc = devm_kzalloc(&pci->dev, sizeof(*dwc), GFP_KERNEL);
+	if (!dwc)
+		return -ENOMEM;
+
+	dwc->dev = dev;
+	dwc->clk_period_ns = data->clk_period_ns;
+
+	ret = pcim_enable_device(pci);
+	if (ret)

Error message here ..

drivers/pwm/pwm-dwc.c line 213

Quoted 6 lines
drivers/pwm/pwm-dwc.c new file +6 -0
+		return ret;
+
+	pci_set_master(pci);
+
+	ret = pcim_iomap_regions(pci, BIT(0), pci_name(pci));
+	if (ret)

.. and here ..

drivers/pwm/pwm-dwc.c line 217

Quoted 4 lines
drivers/pwm/pwm-dwc.c new file +4 -0
+		return ret;
+
+	dwc->base = pcim_iomap_table(pci)[0];
+	if (!dwc->base)

.. and here?

drivers/pwm/pwm-dwc.c line 227

Quoted 10 lines
drivers/pwm/pwm-dwc.c new file +10 -0
+		return -ENOMEM;
+
+	dwc->version = dwc_pwm_readl(dwc->base, DWC_TIMERS_COMP_VERSION);
+
+	/* mask all interrupts and disable all timers */
+	for (i = 0; i < data->npwm; i++) {
+		dwc_pwm_writel(dwc->base, DWC_TIM_CTRL(i), 0);
+		dwc_pwm_writel(dwc->base, DWC_TIM_LD_CNT(i), 0);
+		dwc_pwm_writel(dwc->base, DWC_TIM_CUR_VAL(i), 0);
+	}

Does "disable all timers" result in stopping all outputs? If so, please don't to allow taking over running hardware from the boot loader.

drivers/pwm/pwm-dwc.c line 229

Quoted 1 line
drivers/pwm/pwm-dwc.c new file +1 -0
+	mutex_init(&dwc->lock);

What does this mutex protect? I think it's save to assume that calls to .get_state() and .apply_state() for a single channel are serialized by the core, so locking might not be necessary at the driver level.

Quoted 17 lines
drivers/pwm/pwm-dwc.c new file +17 -0
+	pci_set_drvdata(pci, dwc);
+
+	dwc->chip.dev = dev;
+	dwc->chip.ops = &dwc_pwm_ops;
+	dwc->chip.npwm = data->npwm;
+	dwc->chip.base = -1;
+
+	ret = pwmchip_add(&dwc->chip);
+	if (ret)
+		return ret;
+
+	pm_runtime_put(dev);
+	pm_runtime_allow(dev);
+
+	return 0;
+}
+

drivers/pwm/pwm-dwc.c line 256

Quoted 10 lines
drivers/pwm/pwm-dwc.c new file +10 -0
+static void dwc_pwm_remove(struct pci_dev *pci)
+{
+	struct dwc_pwm *dwc = pci_get_drvdata(pci);
+	int i;
+
+	pm_runtime_forbid(&pci->dev);
+	pm_runtime_get_noresume(&pci->dev);
+
+	for (i = 0; i < dwc->chip.npwm; i++)
+		pwm_disable(&dwc->chip.pwms[i]);

You're not supposed to call pwm_disable() here.

Quoted 10 lines
drivers/pwm/pwm-dwc.c new file +10 -0
+
+	pwmchip_remove(&dwc->chip);
+}
+
+#ifdef CONFIG_PM_SLEEP
+static int dwc_pwm_suspend(struct device *dev)
+{
+	struct pci_dev *pdev = container_of(dev, struct pci_dev, dev);
+	struct dwc_pwm *dwc = pci_get_drvdata(pdev);
+	int i, index_base;

drivers/pwm/pwm-dwc.c line 276

Quoted 10 lines
drivers/pwm/pwm-dwc.c new file +10 -0
+
+	for (i = 0; i < DWC_TIMERS_TOTAL; i++) {
+		index_base = i * 3;
+		dwc->saved_registers[index_base] =
+			dwc_pwm_readl(dwc->base, DWC_TIM_LD_CNT(i));
+		dwc->saved_registers[index_base+1] =
+			dwc_pwm_readl(dwc->base, DWC_TIM_LD_CNT2(i));
+		dwc->saved_registers[index_base+2] =
+			dwc_pwm_readl(dwc->base, DWC_TIM_CTRL(i));
+	}

Does sleeping stop the output? If so (I think) you should prevent going to sleep if the consumers didn't stop the output.

Quoted 7 lines
drivers/pwm/pwm-dwc.c new file +7 -0
+	return 0;
+}
+
+static int dwc_pwm_resume(struct device *dev)
+{
+	struct pci_dev *pdev = container_of(dev, struct pci_dev, dev);
+	struct dwc_pwm *dwc = pci_get_drvdata(pdev);

drivers/pwm/pwm-dwc.c line 294

Quoted 10 lines
drivers/pwm/pwm-dwc.c new file +10 -0
+	int i, index_base;
+
+	for (i = 0; i < DWC_TIMERS_TOTAL; i++) {
+		index_base = i * 3;
+		dwc_pwm_writel(dwc->base, DWC_TIM_LD_CNT(i),
+			       dwc->saved_registers[index_base]);
+		dwc_pwm_writel(dwc->base, DWC_TIM_LD_CNT2(i),
+			       dwc->saved_registers[index_base+1]);
+		dwc_pwm_writel(dwc->base, DWC_TIM_CTRL(i),
+			       dwc->saved_registers[index_base+2]);

I think this code would benefit from saved_registers having a better type. Something like

	struct {
		u32 cnt;
		u32 cnt2;
		u32 ctrl;
	} saved_registers[DWC_TIMERS_TOTAL];
Quoted 5 lines
drivers/pwm/pwm-dwc.c new file +5 -0
+	}
+
+	return 0;
+}
+#endif

drivers/pwm/pwm-dwc.c line 309

Quoted 10 lines
drivers/pwm/pwm-dwc.c new file +10 -0
+
+static SIMPLE_DEV_PM_OPS(dwc_pwm_pm_ops, dwc_pwm_suspend, dwc_pwm_resume);
+
+static const struct dwc_pwm_driver_data ehl_driver_data = {
+	.npwm = 8,
+	.clk_period_ns = 10,
+};
+
+static const struct pci_device_id dwc_pwm_id_table[] = {
+	{ PCI_VDEVICE(INTEL, 0x4bb7), (kernel_ulong_t) &ehl_driver_data },

If there is only a single device type, you could better hard code (with cpp symbols) the values contained in ehl_driver_data.

Quoted 9 lines
drivers/pwm/pwm-dwc.c new file +9 -0
+	{  }	/* Terminating Entry */
+};
+MODULE_DEVICE_TABLE(pci, dwc_pwm_id_table);
+
+static struct pci_driver dwc_pwm_driver = {
+	.name		= "pwm-dwc",
+	.probe		= dwc_pwm_probe,
+	.remove		= dwc_pwm_remove,
+	.id_table	= dwc_pwm_id_table,

drivers/pwm/pwm-dwc.c line 328

Quoted 10 lines
drivers/pwm/pwm-dwc.c new file +10 -0
+	.driver = {
+		.pm = &dwc_pwm_pm_ops,
+	},
+};
+
+module_pci_driver(dwc_pwm_driver);
+
+MODULE_AUTHOR("Felipe Balbi>");
+MODULE_DESCRIPTION("DesignWare PWM Controller");
+MODULE_LICENSE("GPL v2");

Is "GPL v2" still the one that should be used? I thought "GPL" was the one to use.

Best regards
Uwe

Signature · 2 lines
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | https://www.pengutronix.de/ |
#3 lore raw

Hi

Quoted 8 lines · Uwe Kleine-König
Quoted 8 lines
drivers/pwm/pwm-dwc.c new file +2 -0
new file mode 100644
index 000000000000..962d504c1281
@@ -0,0 +1,328 @@
+// SPDX-License-Identifier: GPL-2.0
+/**
Quoted 9 lines

drivers/pwm/pwm-dwc.c line 7

Quoted 5 lines
drivers/pwm/pwm-dwc.c new file +5 -0
+ * DesignWare PWM Controller driver
+ *
+ * Copyright (C) 2018-2020 Intel Corporation
+ *
+ * Author: Felipe Balbi

Is there a publicly available reference manual available? If so, adding a link here would be great.

Point taken as well as for other trivial ones. Replies to more complex ones below.

Quoted 14 lines

drivers/pwm/pwm-dwc.c line 80

Quoted 10 lines
drivers/pwm/pwm-dwc.c new file +10 -0
+static void __dwc_pwm_configure(struct dwc_pwm *dwc, int pwm,
+				unsigned int duty_ns,
+				unsigned int period_ns)
+{
+	u32 ctrl;
+	u32 high;
+	u32 low;
+
+	high = DIV_ROUND_CLOSEST(duty_ns, dwc->clk_period_ns) - 1;
+	low = DIV_ROUND_CLOSEST(period_ns - duty_ns, dwc->clk_period_ns) - 1;

Have you tested your driver with CONFIG_PWM_DEBUG? I would expect that using ROUND_CLOSEST triggers

Quoted 4 lines

.apply is supposed to round down period

in some situations.

Hmm.. in fact not tested, nor near the and over the boundaries. Will check.

Quoted 3 lines

Maybe soon duty_ns and period_ns will become 64 bit integers. I suggest to already cope for this today and introduce proper range checking.

Ah, good to know.

Quoted 16 lines

drivers/pwm/pwm-dwc.c line 129

Quoted 12 lines
drivers/pwm/pwm-dwc.c new file +12 -0
+static void __dwc_pwm_set_enable(struct dwc_pwm *dwc, int pwm, int enabled)
+{
+	u32 reg;
+
+	reg = dwc_pwm_readl(dwc->base, DWC_TIM_CTRL(pwm));
+
+	if (enabled)
+		reg |= DWC_TIM_CTRL_EN;
+	else
+		reg &= ~DWC_TIM_CTRL_EN;
+
+	dwc_pwm_writel(dwc->base, DWC_TIM_CTRL(pwm), reg);

What happens to the output if the EN bit is cleared? Does the hardware complete the currently running period? If not, does the output "freeze" or get inactive?

...

Quoted 5 lines

Is it necessary to stop the hardware before writing the counter registers? Or is this only done to prevent race conditions that yield unexpected cycles?

Good questions. I need to spend time in the lab next week and debug :-)

Quoted 4 lines
Quoted 4 lines
drivers/pwm/pwm-dwc.c new file +4 -0
+static int dwc_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
+			 const struct pwm_state *state)
+{
+	struct dwc_pwm *dwc = to_dwc_pwm(chip);
Quoted 9 lines

drivers/pwm/pwm-dwc.c line 150

Quoted 5 lines
drivers/pwm/pwm-dwc.c new file +5 -0
+
+	mutex_lock(&dwc->lock);
+
+	if (state->enabled) {
+		if (!pwm_is_enabled(pwm))

If pwm_is_enabled() would be changed to call .get_state() you'd run in a dead lock. Please don't call PWM API functions in driver callbacks.

Oh. I guess we copied this from pwm-lpss.c and pwm-mxs.c.

Quoted 11 lines

drivers/pwm/pwm-dwc.c line 158

Quoted 8 lines
drivers/pwm/pwm-dwc.c new file +8 -0
+			pm_runtime_get_sync(dwc->dev);
+		__dwc_pwm_configure_timer(dwc, pwm, state);
+	} else {
+		if (pwm_is_enabled(pwm)) {
+			__dwc_pwm_set_enable(dwc, pwm->hwpwm, false);
+			pm_runtime_put_sync(dwc->dev);
+		}
+	}

You don't check state->polarity here.

If I recall correctly I didn't see the polarity bit in HW but will check. Should the driver emulate it by recalculating duty etc since I didn't see quickly from PWM core can a driver with apply() callback report support for polarity?

Quoted 8 lines
Quoted 8 lines
drivers/pwm/pwm-dwc.c new file +8 -0
+static void dwc_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
+			      struct pwm_state *state)
+{
+	struct dwc_pwm *dwc = to_dwc_pwm(chip);
+
+	pm_runtime_get_sync(dwc->dev);
+
+	mutex_lock(&dwc->lock);
Quoted 9 lines

drivers/pwm/pwm-dwc.c line 176

Quoted 4 lines
drivers/pwm/pwm-dwc.c new file +4 -0
+	state->enabled = __dwc_pwm_is_enabled(dwc, pwm->hwpwm);
+	state->duty_cycle = __dwc_pwm_duty_ns(dwc, pwm->hwpwm);
+	state->period = __dwc_pwm_period_ns(dwc, pwm->hwpwm,
+					    state->duty_cycle);

I wonder if doing the math in .get_state() directly instead of introducing a function with IMHO strange semantics that is only used once would be easier to understand.

Maybe. Will check how does it look that way.

Quoted 12 lines

drivers/pwm/pwm-dwc.c line 227

Quoted 8 lines
drivers/pwm/pwm-dwc.c new file +8 -0
+	dwc->version = dwc_pwm_readl(dwc->base, DWC_TIMERS_COMP_VERSION);
+
+	/* mask all interrupts and disable all timers */
+	for (i = 0; i < data->npwm; i++) {
+		dwc_pwm_writel(dwc->base, DWC_TIM_CTRL(i), 0);
+		dwc_pwm_writel(dwc->base, DWC_TIM_LD_CNT(i), 0);
+		dwc_pwm_writel(dwc->base, DWC_TIM_CUR_VAL(i), 0);
+	}

Does "disable all timers" result in stopping all outputs? If so, please don't to allow taking over running hardware from the boot loader.

Very good point. We probably didn't think about that.

Quoted 6 lines

drivers/pwm/pwm-dwc.c line 229

Quoted 1 line
drivers/pwm/pwm-dwc.c new file +1 -0
+	mutex_init(&dwc->lock);

What does this mutex protect? I think it's save to assume that calls to .get_state() and .apply_state() for a single channel are serialized by the core, so locking might not be necessary at the driver level.

That would be good. Will check.

Quoted 11 lines
Quoted 11 lines
drivers/pwm/pwm-dwc.c new file +11 -0
+#ifdef CONFIG_PM_SLEEP
+static int dwc_pwm_suspend(struct device *dev)
+{
+	struct pci_dev *pdev = container_of(dev, struct pci_dev, dev);
+	struct dwc_pwm *dwc = pci_get_drvdata(pdev);
+	int i, index_base;
+
+	for (i = 0; i < DWC_TIMERS_TOTAL; i++) {
+		index_base = i * 3;
+		dwc->saved_registers[index_base] =
+			dwc_pwm_readl(dwc->base, DWC_TIM_LD_CNT(i));
Quoted 9 lines

drivers/pwm/pwm-dwc.c line 276

Quoted 5 lines
drivers/pwm/pwm-dwc.c new file +5 -0
+		dwc->saved_registers[index_base+1] =
+			dwc_pwm_readl(dwc->base, DWC_TIM_LD_CNT2(i));
+		dwc->saved_registers[index_base+2] =
+			dwc_pwm_readl(dwc->base, DWC_TIM_CTRL(i));
+	}

Does sleeping stop the output? If so (I think) you should prevent going to sleep if the consumers didn't stop the output.

I believe stopping the activity is an expectation during suspend but don't have the knowledge here. So in general PWM consumers should stop the PWM in their suspend handlers, e.g. in display panel drivers etc, rather than blindly stop it here? How about when PWM is enabled from sysfs?

Signature · 1 line
Jarkko
#4 lore raw

Hi Jarkko,

Quoted 21 lines · Jarkko Nikula
Quoted 3 lines

Maybe soon duty_ns and period_ns will become 64 bit integers. I suggest to already cope for this today and introduce proper range checking.

Ah, good to know.

Quoted 8 lines
Quoted 8 lines
drivers/pwm/pwm-dwc.c new file +8 -0
+static void __dwc_pwm_set_enable(struct dwc_pwm *dwc, int pwm, int enabled)
+{
+	u32 reg;
+
+	reg = dwc_pwm_readl(dwc->base, DWC_TIM_CTRL(pwm));
+
+	if (enabled)
+		reg |= DWC_TIM_CTRL_EN;
Quoted 8 lines

drivers/pwm/pwm-dwc.c line 129

Quoted 4 lines
drivers/pwm/pwm-dwc.c new file +4 -0
+	else
+		reg &= ~DWC_TIM_CTRL_EN;
+
+	dwc_pwm_writel(dwc->base, DWC_TIM_CTRL(pwm), reg);

What happens to the output if the EN bit is cleared? Does the hardware complete the currently running period? If not, does the output "freeze" or get inactive?

Quoted 7 lines

...

Quoted 4 lines

Is it necessary to stop the hardware before writing the counter registers? Or is this only done to prevent race conditions that yield unexpected cycles?

Good questions. I need to spend time in the lab next week and debug :-)

So I assume this means modifying registers while the hardware is running at least isn't prohibited in the reference manual. Note this question was more out of interest and I think with the register semantics an atomic update probably doesn't work in a sane way anyhow. (The background is that with some controllers you can update while the PWM is running but this might result in bogus output and you can discuss felicitously about which is the right approach to handle such hardware. I just wanted to know if this hardware has potential for this discussion ;-) As far as I understood your hardware you would get bogus output for quite sure and so disable + reconfigure + enable seems sane.)

Quoted 13 lines
Quoted 4 lines
Quoted 4 lines
drivers/pwm/pwm-dwc.c new file +4 -0
+static int dwc_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
+			 const struct pwm_state *state)
+{
+	struct dwc_pwm *dwc = to_dwc_pwm(chip);
Quoted 8 lines

drivers/pwm/pwm-dwc.c line 150

Quoted 5 lines
drivers/pwm/pwm-dwc.c new file +5 -0
+
+	mutex_lock(&dwc->lock);
+
+	if (state->enabled) {
+		if (!pwm_is_enabled(pwm))

If pwm_is_enabled() would be changed to call .get_state() you'd run in a dead lock. Please don't call PWM API functions in driver callbacks.

Quoted 1 line

Oh. I guess we copied this from pwm-lpss.c and pwm-mxs.c.

But at least those don't use locking ...

Quoted 11 lines
Quoted 10 lines

drivers/pwm/pwm-dwc.c line 158

Quoted 8 lines
drivers/pwm/pwm-dwc.c new file +8 -0
+			pm_runtime_get_sync(dwc->dev);
+		__dwc_pwm_configure_timer(dwc, pwm, state);
+	} else {
+		if (pwm_is_enabled(pwm)) {
+			__dwc_pwm_set_enable(dwc, pwm->hwpwm, false);
+			pm_runtime_put_sync(dwc->dev);
+		}
+	}

You don't check state->polarity here.

Quoted 4 lines

If I recall correctly I didn't see the polarity bit in HW but will check. Should the driver emulate it by recalculating duty etc since I didn't see quickly from PWM core can a driver with apply() callback report support for polarity?

The usual thing is

	if (state->polarity != PWM_POLARITY_NORMAL)
		return -EINVAL;

in .apply. (Maybe the return code isn't the same everywhere.)

Quoted 13 lines
Quoted 4 lines
Quoted 4 lines
drivers/pwm/pwm-dwc.c new file +4 -0
+	for (i = 0; i < DWC_TIMERS_TOTAL; i++) {
+		index_base = i * 3;
+		dwc->saved_registers[index_base] =
+			dwc_pwm_readl(dwc->base, DWC_TIM_LD_CNT(i));
Quoted 8 lines

drivers/pwm/pwm-dwc.c line 276

Quoted 5 lines
drivers/pwm/pwm-dwc.c new file +5 -0
+		dwc->saved_registers[index_base+1] =
+			dwc_pwm_readl(dwc->base, DWC_TIM_LD_CNT2(i));
+		dwc->saved_registers[index_base+2] =
+			dwc_pwm_readl(dwc->base, DWC_TIM_CTRL(i));
+	}

Does sleeping stop the output? If so (I think) you should prevent going to sleep if the consumers didn't stop the output.

Quoted 4 lines

I believe stopping the activity is an expectation during suspend but don't have the knowledge here. So in general PWM consumers should stop the PWM in their suspend handlers, e.g. in display panel drivers etc, rather than blindly stop it here?

Yes.

Quoted 1 line

How about when PWM is enabled from sysfs?

sysfs isn't different here in a relevant way. The key idea is: Don't stop the output unless the consumer requested it.

Best regards
Uwe

Signature · 2 lines
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | https://www.pengutronix.de/ |