Skip to contentSkip to conversation

Add i.MX7ULP EVK PWM backlight support

PATCH Superseded Anson Huang 3 participants 14 messages Mar 22, 2019 – Mar 26, 2019

Subsystems (4)

v9 of 12 Superseded Latest is v12 →

lore
Share & tools
lore
#0 lore raw

v9 cover Add i.MX7ULP EVK PWM backlight support

i.MX7ULP EVK board has MIPI-DSI display, its backlight is supplied by TPM PWM module, this patch set enables i.MX7ULP TPM PWM driver support and also add backlight support for MIPI-DSI display.

Changes since V8:
- ONLY change the pwm driver patch.

Anson Huang (5):
dt-bindings: pwm: Add i.MX TPM PWM binding
pwm: Add i.MX TPM PWM driver support
ARM: imx_v6_v7_defconfig: Add TPM PWM support by default
ARM: dts: imx7ulp: Add pwm0 support
ARM: dts: imx7ulp-evk: Add backlight support

 .../devicetree/bindings/pwm/imx-tpm-pwm.txt        |  22 +
 arch/arm/boot/dts/imx7ulp-evk.dts                  |  21 +
 arch/arm/boot/dts/imx7ulp.dtsi                     |  10 +
 arch/arm/configs/imx_v6_v7_defconfig               |   1 +
 drivers/pwm/Kconfig                                |  11 +
 drivers/pwm/Makefile                               |   1 +
 drivers/pwm/pwm-imx-tpm.c                          | 518 +++++++++++++++++++++
 7 files changed, 584 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/pwm/imx-tpm-pwm.txt
 create mode 100644 drivers/pwm/pwm-imx-tpm.c
Signature · 1 line
2.7.4
#1 lore raw

PATCH v9 1/5 dt-bindings: pwm: Add i.MX TPM PWM binding

Add i.MX TPM(Low Power Timer/Pulse Width Modulation Module) PWM binding.

---
No changes.

---
 .../devicetree/bindings/pwm/imx-tpm-pwm.txt        | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/pwm/imx-tpm-pwm.txt
Documentation/devicetree/bindings/pwm/imx-tpm-pwm.txt new file +22 -0
new file mode 100644
index 0000000..94f1ad5
@@ -0,0 +1,22 @@
+Freescale i.MX TPM PWM controller
+
+Required properties:
+- compatible : Should be "fsl,imx-tpm".
+- reg: Physical base address and length of the controller's registers.
+- #pwm-cells: Should be 3. See pwm.txt in this directory for a description of the cells format.
+- clocks : The clock provided by the SoC to drive the PWM.
+- interrupts: The interrupt for the PWM controller.
+
+Note: The TPM counter and period counter are shared between multiple channels, so all channels
+should use same period setting.
+
+Example:
+
+pwm0: tpm@40250000 {
+	compatible = "fsl,imx-tpm";
+	reg = <0x40250000 0x1000>;
+	assigned-clocks = <&clks IMX7ULP_CLK_LPTPM4>;
+	assigned-clock-parents = <&clks IMX7ULP_CLK_SOSC_BUS_CLK>;
+	clocks = <&clks IMX7ULP_CLK_LPTPM4>;
+	#pwm-cells = <3>;
+};
Signature · 1 line
2.7.4
#7 lore raw

PATCH v9 2/5 pwm: Add i.MX TPM PWM driver support

In reply to Anson Huang

i.MX7ULP has TPM(Low Power Timer/Pulse Width Modulation Module) inside, it can support multiple PWM channels, all the channels share same counter and period setting, but each channel can configure its duty and polarity independently.

There are several TPM modules in i.MX7ULP, the number of channels in TPM modules are different, it can be read from each TPM module's PARAM register.

---
Changes since V8:
- add more limitation notes for period/duty update un-atomic limitations;
- add waiting for period/duty update actually applied to HW;
- move the duty update into period update function to make them to be updated as together as possiable;
- don't allow PS change if counter is running;
- save channel polarity settings and return it directly when .get_state is called, as the HW polarity setting could be impacted by enable status.

---
 drivers/pwm/Kconfig       |  11 +
 drivers/pwm/Makefile      |   1 +
 drivers/pwm/pwm-imx-tpm.c | 518 ++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 530 insertions(+)
 create mode 100644 drivers/pwm/pwm-imx-tpm.c
drivers/pwm/Kconfig +11 -0
index 54f8238..3ea0391 100644
@@ -210,6 +210,17 @@ config PWM_IMX27
 	  To compile this driver as a module, choose M here: the module
 	  will be called pwm-imx27.
 
+config PWM_IMX_TPM
+	tristate "i.MX TPM PWM support"
+	depends on ARCH_MXC || COMPILE_TEST
+	depends on HAVE_CLK && HAS_IOMEM
+	help
+	  Generic PWM framework driver for i.MX7ULP TPM module, TPM's full
+	  name is Low Power Timer/Pulse Width Modulation Module.
+
+	  To compile this driver as a module, choose M here: the module
+	  will be called pwm-imx-tpm.
+
 config PWM_JZ4740
 	tristate "Ingenic JZ47xx PWM support"
 	depends on MACH_INGENIC
drivers/pwm/Makefile +1 -0
index 448825e..c368599 100644
@@ -19,6 +19,7 @@ obj-$(CONFIG_PWM_HIBVT)		+= pwm-hibvt.o
 obj-$(CONFIG_PWM_IMG)		+= pwm-img.o
 obj-$(CONFIG_PWM_IMX1)		+= pwm-imx1.o
 obj-$(CONFIG_PWM_IMX27)		+= pwm-imx27.o
+obj-$(CONFIG_PWM_IMX_TPM)	+= pwm-imx-tpm.o
 obj-$(CONFIG_PWM_JZ4740)	+= pwm-jz4740.o
 obj-$(CONFIG_PWM_LP3943)	+= pwm-lp3943.o
 obj-$(CONFIG_PWM_LPC18XX_SCT)	+= pwm-lpc18xx-sct.o
drivers/pwm/pwm-imx-tpm.c new file +518 -0

518 lines not on this page · raw message

Signature · 1 line
2.7.4
#11 lore raw

PATCH v9 3/5 ARM: imx_v6_v7_defconfig: Add TPM PWM support by default

In reply to Anson Huang

Select CONFIG_PWM_IMX_TPM by default to support i.MX7ULP TPM PWM.

---
No changes.

---
 arch/arm/configs/imx_v6_v7_defconfig | 1 +
 1 file changed, 1 insertion(+)
arch/arm/configs/imx_v6_v7_defconfig +1 -0
index 1872dac..2a35806 100644
@@ -400,6 +400,7 @@ CONFIG_MPL3115=y
 CONFIG_PWM=y
 CONFIG_PWM_FSL_FTM=y
 CONFIG_PWM_IMX=y
+CONFIG_PWM_IMX_TPM=y
 CONFIG_NVMEM_IMX_OCOTP=y
 CONFIG_NVMEM_VF610_OCOTP=y
 CONFIG_TEE=y
Signature · 1 line
2.7.4
#12 lore raw

PATCH v9 4/5 ARM: dts: imx7ulp: Add pwm0 support

In reply to Anson Huang

Add i.MX7ULP EVK board PWM0 support.

---
No changes;

---
 arch/arm/boot/dts/imx7ulp.dtsi | 10 ++++++++++
 1 file changed, 10 insertions(+)
arch/arm/boot/dts/imx7ulp.dtsi +10 -0
index eb349fd..15d04fb 100644
@@ -124,6 +124,16 @@
 			status = "disabled";
 		};
 
+		pwm0: pwm@40250000 {
+			compatible = "fsl,imx-tpm";
+			reg = <0x40250000 0x1000>;
+			assigned-clocks = <&pcc2 IMX7ULP_CLK_LPTPM4>;
+			assigned-clock-parents = <&scg1 IMX7ULP_CLK_SOSC_BUS_CLK>;
+			clocks = <&pcc2 IMX7ULP_CLK_LPTPM4>;
+			#pwm-cells = <3>;
+			status = "disabled";
+		};
+
 		tpm5: tpm@40260000 {
 			compatible = "fsl,imx7ulp-tpm";
 			reg = <0x40260000 0x1000>;
Signature · 1 line
2.7.4
#13 lore raw

PATCH v9 5/5 ARM: dts: imx7ulp-evk: Add backlight support

In reply to Anson Huang

This patch adds i.MX7ULP EVK board MIPI-DSI backlight support.

---
No changes.

---
 arch/arm/boot/dts/imx7ulp-evk.dts | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
arch/arm/boot/dts/imx7ulp-evk.dts +21 -0
index a09026a..7c44ffa 100644
@@ -8,6 +8,7 @@
 /dts-v1/;
 
 #include "imx7ulp.dtsi"
+#include <dt-bindings/pwm/pwm.h>
 
 / {
 	model = "NXP i.MX7ULP EVK";
@@ -22,6 +23,14 @@
 		reg = <0x60000000 0x40000000>;
 	};
 
+	backlight {
+		compatible = "pwm-backlight";
+		pwms = <&pwm0 1 50000 0>;
+		brightness-levels = <0 20 25 30 35 40 100>;
+		default-brightness-level = <6>;
+		status = "okay";
+	};
+
 	reg_vsd_3v3: regulator-vsd-3v3 {
 		compatible = "regulator-fixed";
 		regulator-name = "VSD_3V3";
@@ -40,6 +49,12 @@
 	status = "okay";
 };
 
+&pwm0 {
+	pinctrl-names = "default";
+	pinctrl-0 = <&pinctrl_pwm0>;
+	status = "okay";
+};
+
 &usdhc0 {
 	pinctrl-names = "default";
 	pinctrl-0 = <&pinctrl_usdhc0>;
@@ -57,6 +72,12 @@
 		bias-pull-up;
 	};
 
+	pinctrl_pwm0: pwm0grp {
+		fsl,pins = <
+			IMX7ULP_PAD_PTF2__TPM4_CH1	0x2
+		>;
+	};
+
 	pinctrl_usdhc0: usdhc0grp {
 		fsl,pins = <
 			IMX7ULP_PAD_PTD1__SDHC0_CMD	0x43
Signature · 1 line
2.7.4
#8 lore raw

Re: [PATCH V9 2/5] pwm: Add i.MX TPM PWM driver support

In reply to Anson Huang
Quoted 73 lines · Anson Huang

i.MX7ULP has TPM(Low Power Timer/Pulse Width Modulation Module) inside, it can support multiple PWM channels, all the channels share same counter and period setting, but each channel can configure its duty and polarity independently.

There are several TPM modules in i.MX7ULP, the number of channels in TPM modules are different, it can be read from each TPM module's PARAM register.

---
Changes since V8:
- add more limitation notes for period/duty update un-atomic limitations;
- add waiting for period/duty update actually applied to HW;
- move the duty update into period update function to make them to be updated as together as possiable;
- don't allow PS change if counter is running;
- save channel polarity settings and return it directly when .get_state is called, as the HW polarity setting could be impacted by enable status.

---
 drivers/pwm/Kconfig       |  11 +
 drivers/pwm/Makefile      |   1 +
 drivers/pwm/pwm-imx-tpm.c | 518 ++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 530 insertions(+)
 create mode 100644 drivers/pwm/pwm-imx-tpm.c
drivers/pwm/Kconfig +11 -0
index 54f8238..3ea0391 100644
@@ -210,6 +210,17 @@ config PWM_IMX27
 	  To compile this driver as a module, choose M here: the module
 	  will be called pwm-imx27.
 
+config PWM_IMX_TPM
+	tristate "i.MX TPM PWM support"
+	depends on ARCH_MXC || COMPILE_TEST
+	depends on HAVE_CLK && HAS_IOMEM
+	help
+	  Generic PWM framework driver for i.MX7ULP TPM module, TPM's full
+	  name is Low Power Timer/Pulse Width Modulation Module.
+
+	  To compile this driver as a module, choose M here: the module
+	  will be called pwm-imx-tpm.
+
 config PWM_JZ4740
 	tristate "Ingenic JZ47xx PWM support"
 	depends on MACH_INGENIC
drivers/pwm/Makefile +1 -0
index 448825e..c368599 100644
@@ -19,6 +19,7 @@ obj-$(CONFIG_PWM_HIBVT)		+= pwm-hibvt.o
 obj-$(CONFIG_PWM_IMG)		+= pwm-img.o
 obj-$(CONFIG_PWM_IMX1)		+= pwm-imx1.o
 obj-$(CONFIG_PWM_IMX27)		+= pwm-imx27.o
+obj-$(CONFIG_PWM_IMX_TPM)	+= pwm-imx-tpm.o
 obj-$(CONFIG_PWM_JZ4740)	+= pwm-jz4740.o
 obj-$(CONFIG_PWM_LP3943)	+= pwm-lp3943.o
 obj-$(CONFIG_PWM_LPC18XX_SCT)	+= pwm-lpc18xx-sct.o
drivers/pwm/pwm-imx-tpm.c new file +7 -0
new file mode 100644
index 0000000..58af0915
@@ -0,0 +1,518 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright 2018-2019 NXP.
+ *
+ * Limitations:
+ * - The TPM counter and period counter are shared between
+ *   multiple channels, so all channels should use same period

drivers/pwm/pwm-imx-tpm.c line 17

Quoted 10 lines
drivers/pwm/pwm-imx-tpm.c new file +10 -0
+ *   settings.
+ * - Changes to polarity cannot be latched at the time of the
+ *   next period start.
+ * - The period and duty changes are NOT atomic, if new period and
+ *   new duty are requested simultaneously when counter is running,
+ *   there could be a small window of running old duty with new
+ *   period, as the period is updated before duty in this driver, the
+ *   probability is very low, ONLY happen when the TPM counter changes
+ *   from MOD to zero between the consecutive update of period and
+ *   duty.

The window that this bug triggers is small, but if it does, the window where the invalid combination is applied, isn't small---it's one complete period if I'm not mistaken. So I'd write:

- Changing period and duty cycle together isn't atomic. With the wrong timing it might happen that a period is produced with old duty cycle but new period settings.

Quoted 27 lines
drivers/pwm/pwm-imx-tpm.c new file +27 -0
+ */
+
+#include <linux/bitfield.h>
+#include <linux/bitops.h>
+#include <linux/clk.h>
+#include <linux/err.h>
+#include <linux/io.h>
+#include <linux/log2.h>
+#include <linux/module.h>
+#include <linux/of.h>
+#include <linux/of_address.h>
+#include <linux/platform_device.h>
+#include <linux/pwm.h>
+#include <linux/slab.h>
+
+#define PWM_IMX_TPM_PARAM	0x4
+#define PWM_IMX_TPM_GLOBAL	0x8
+#define PWM_IMX_TPM_SC		0x10
+#define PWM_IMX_TPM_CNT		0x14
+#define PWM_IMX_TPM_MOD		0x18
+#define PWM_IMX_TPM_CnSC(n)	(0x20 + (n) * 0x8)
+#define PWM_IMX_TPM_CnV(n)	(0x24 + (n) * 0x8)
+
+#define PWM_IMX_TPM_PARAM_CHAN			GENMASK(7, 0)
+
+#define PWM_IMX_TPM_SC_PS			GENMASK(2, 0)
+#define PWM_IMX_TPM_SC_CMOD			GENMASK(4, 3)

drivers/pwm/pwm-imx-tpm.c line 54

Quoted 10 lines
drivers/pwm/pwm-imx-tpm.c new file +10 -0
+#define PWM_IMX_TPM_SC_CMOD_INC_EVERY_CLK	FIELD_PREP(PWM_IMX_TPM_SC_CMOD, 1)
+#define PWM_IMX_TPM_SC_CPWMS			BIT(5)
+
+#define PWM_IMX_TPM_CnSC_CHF	BIT(7)
+#define PWM_IMX_TPM_CnSC_MSB	BIT(5)
+#define PWM_IMX_TPM_CnSC_MSA	BIT(4)
+
+/*
+ * The reference manual describes this field as two separate bits. The
+ * samantic of the two bits isn't orthogonal though, so they are treated

s/samantic/semantic/

Quoted 109 lines
drivers/pwm/pwm-imx-tpm.c new file +109 -0
+ * together as a 2-bit field here.
+ */
+#define PWM_IMX_TPM_CnSC_ELS	GENMASK(3, 2)
+#define PWM_IMX_TPM_CnSC_ELS_POLARITY_INVERSED	0x1
+#define PWM_IMX_TPM_CnSC_ELS_INVERSED	FIELD_PREP(PWM_IMX_TPM_CnSC_ELS, 1)
+#define PWM_IMX_TPM_CnSC_ELS_NORMAL	FIELD_PREP(PWM_IMX_TPM_CnSC_ELS, 2)
+
+
+#define PWM_IMX_TPM_MOD_WIDTH	16
+#define PWM_IMX_TPM_MOD_MOD	GENMASK(PWM_IMX_TPM_MOD_WIDTH - 1, 0)
+
+struct imx_tpm_pwm_chip {
+	struct pwm_chip chip;
+	struct clk *clk;
+	void __iomem *base;
+	struct mutex lock;
+	u32 user_count;
+	u32 enable_count;
+	u32 real_period;
+};
+
+struct imx_tpm_pwm_param {
+	u8 prescale;
+	u32 mod;
+	u32 val;
+};
+
+struct imx_tpm_pwm_channel {
+	enum pwm_polarity polarity;
+};
+
+static inline struct imx_tpm_pwm_chip *to_imx_tpm_pwm_chip(struct pwm_chip *chip)
+{
+	return container_of(chip, struct imx_tpm_pwm_chip, chip);
+}
+
+static int pwm_imx_tpm_round_state(struct pwm_chip *chip,
+				   struct imx_tpm_pwm_param *p,
+				   struct pwm_state *state,
+				   struct pwm_state *real_state)
+{
+	struct imx_tpm_pwm_chip *tpm = to_imx_tpm_pwm_chip(chip);
+	u32 rate, prescale, period_count, clock_unit;
+	u64 tmp;
+
+	rate = clk_get_rate(tpm->clk);
+	tmp = (u64)state->period * rate;
+	clock_unit = DIV_ROUND_CLOSEST_ULL(tmp, NSEC_PER_SEC);
+	if (clock_unit <= PWM_IMX_TPM_MOD_MOD)
+		prescale = 0;
+	else
+		prescale = ilog2(clock_unit) + 1 - PWM_IMX_TPM_MOD_WIDTH;
+
+	if ((!FIELD_FIT(PWM_IMX_TPM_SC_PS, prescale)))
+		return -ERANGE;
+	p->prescale = prescale;
+
+	period_count = (clock_unit + ((1 << prescale) >> 1)) >> prescale;
+	p->mod = period_count;
+
+	/* calculate real period HW can support */
+	tmp = (u64)period_count << prescale;
+	tmp *= NSEC_PER_SEC;
+	real_state->period = DIV_ROUND_CLOSEST_ULL(tmp, rate);
+
+	/*
+	 * if eventually the PWM output is inactive, either
+	 * duty cycle is 0 or status is disabled, need to
+	 * make sure the output pin is inactive.
+	 */
+	if (!state->enabled)
+		real_state->duty_cycle = 0;
+	else
+		real_state->duty_cycle = state->duty_cycle;
+
+	tmp = (u64)p->mod * real_state->duty_cycle;
+	p->val = DIV_ROUND_CLOSEST_ULL(tmp, real_state->period);
+
+	real_state->polarity = state->polarity;
+	real_state->enabled = state->enabled;
+
+	return 0;
+}
+
+/* this function is supposed to be called with mutex hold */
+static int pwm_imx_tpm_setup_period_duty(struct pwm_chip *chip,
+					 struct pwm_device *pwm,
+					 struct imx_tpm_pwm_param *p)
+{
+	struct imx_tpm_pwm_chip *tpm = to_imx_tpm_pwm_chip(chip);
+	unsigned long timeout;
+	u32 val, cmod, cur_prescale;
+
+	val = readl(tpm->base + PWM_IMX_TPM_SC);
+	cmod = FIELD_GET(PWM_IMX_TPM_SC_CMOD, val);
+	cur_prescale = FIELD_GET(PWM_IMX_TPM_SC_PS, val);
+	if (cmod && cur_prescale != p->prescale)
+		return -EBUSY;
+
+	/* set TPM counter prescale */
+	val &= ~PWM_IMX_TPM_SC_PS;
+	val |= FIELD_PREP(PWM_IMX_TPM_SC_PS, p->prescale);
+	writel(val, tpm->base + PWM_IMX_TPM_SC);
+
+	/*
+	 * set period count:
+	 * if (CMOD[1:0] = 0:0) then MOD register is updated when MOD
+	 * register is written.
+	 *

drivers/pwm/pwm-imx-tpm.c line 173

Quoted 10 lines
drivers/pwm/pwm-imx-tpm.c new file +10 -0
+	 * if (CMOD[1:0] ≠ 0:0), then MOD register is updated according
+	 * to the CPWMS bit, that is:
+	 *
+	 * if the selected mode is not CPWM then MOD register is updated
+	 * after MOD register was written and the TPM counter changes from
+	 * MOD to zero.
+	 *
+	 * if the selected mode is CPWM then MOD register is updated after
+	 * MOD register was written and the TPM counter changes from MOD
+	 * to (MOD – 1).

Given that the driver doesn't make use of CPWM, this comment could be simplified. I'd write:

	/*
	 * If the PWM is enabled (CMOD[1:0] ≠ 2b00), the period length
	 * is latched into hardware when the next period starts.
	 */

This is even true for the (here unused) CPWM mode. (The reference manual isn't entirely clear here IMHO. I assume if MOD == 4000 and CNT == 2001 then MOD is then changed to 2000, the currently running period is completed with a length of 4000 prescaled clk cycles?!)

Quoted 12 lines
drivers/pwm/pwm-imx-tpm.c new file +12 -0
+	 */
+	writel(p->mod, tpm->base + PWM_IMX_TPM_MOD);
+
+	/*
+	 * set channel value:
+	 * if (CMOD[1:0] = 0:0) then CnV register is updated when CnV
+	 * register is written.
+	 *
+	 * if (CMOD[1:0] ≠ 0:0), then CnV register is updated according
+	 * to the selected mode, that is:
+	 *
+	 * if the selected mode is output compare then CnV register is

drivers/pwm/pwm-imx-tpm.c line 195

Quoted 10 lines
drivers/pwm/pwm-imx-tpm.c new file +10 -0
+	 * updated on the next TPM counter increment (end of the prescaler
+	 * counting) after CnV register was written.
+	 *
+	 * if the selected mode is EPWM then CnV register is updated after
+	 * CnV register was written and the TPM counter changes from MOD
+	 * to zero.
+	 *
+	 * if the selected mode is CPWM then CnV register is updated after
+	 * CnV register was written and the TPM counter changes from MOD
+	 * to (MOD – 1).

This is similar to the above too verbose and covers stuff that is not relevant for this driver. Also the used wording is not obvious if you don't look into the reference manual.

Quoted 42 lines
drivers/pwm/pwm-imx-tpm.c new file +41 -0
+	 */
+	writel(p->val, tpm->base + PWM_IMX_TPM_CnV(pwm->hwpwm));
+
+	/* make sure MOD & CnV registers are updated */
+	timeout = jiffies + msecs_to_jiffies(tpm->real_period /
+					     NSEC_PER_MSEC + 1);
+	while (readl(tpm->base + PWM_IMX_TPM_MOD) != p->mod
+	       || readl(tpm->base + PWM_IMX_TPM_CnV(pwm->hwpwm))
+	       != p->val) {
+		if (time_after(jiffies, timeout))
+			return -ETIME;
+		cpu_relax();
+	}
+
+	return 0;
+}
[...]
+static int pwm_imx_tpm_apply(struct pwm_chip *chip,
+			     struct pwm_device *pwm,
+			     struct pwm_state *state)
+{
+	struct imx_tpm_pwm_chip *tpm = to_imx_tpm_pwm_chip(chip);
+	struct imx_tpm_pwm_param param;
+	struct pwm_state real_state;
+	int ret;
+
+	ret = pwm_imx_tpm_round_state(chip, &param, state, &real_state);
+	if (ret)
+		return -EINVAL;
+
+	mutex_lock(&tpm->lock);
+
+	/*
+	 * TPM counter is shared by multiple channels, so
+	 * prescale and period can NOT be modified when
+	 * there are multiple channels in use with different
+	 * period settings.
+	 */
+	if (real_state.period != tpm->real_period) {
+		if (tpm->user_count > 1) {
+			ret = -EBUSY;
+			goto exit;

drivers/pwm/pwm-imx-tpm.c

Quoted 10 lines
drivers/pwm/pwm-imx-tpm.c new file +10 -0
+		}
+
+		ret = pwm_imx_tpm_setup_period_duty(chip, pwm, &param);
+		if (ret)
+			goto exit;
+
+		tpm->real_period = real_state.period;
+	}
+
+	ret = pwm_imx_tpm_apply_hw(chip, pwm, &real_state);

It's unintuitive here that both pwm_imx_tpm_setup_period_duty and pwm_imx_tpm_apply_hw (potentially) configure the duty cycle. I didn't thought it to an end, but maybe this could be optimised?

drivers/pwm/pwm-imx-tpm.c line 374

Quoted 12 lines
drivers/pwm/pwm-imx-tpm.c new file +12 -0
+exit:
+	mutex_unlock(&tpm->lock);
+
+	return ret;
+}
+
+static int pwm_imx_tpm_request(struct pwm_chip *chip, struct pwm_device *pwm)
+{
+	struct imx_tpm_pwm_chip *tpm = to_imx_tpm_pwm_chip(chip);
+	struct imx_tpm_pwm_channel *chan;
+
+	chan = devm_kzalloc(chip->dev, sizeof(*chan), GFP_KERNEL);

There is no advantage in using the devm variant here as the requested memory is freed in .free anyhow. So this only adds additional memory foodprint and runtime overhead.

Quoted 12 lines
drivers/pwm/pwm-imx-tpm.c new file +12 -0
+	if (!chan)
+		return -ENOMEM;
+
+	pwm_set_chip_data(pwm, chan);
+
+	mutex_lock(&tpm->lock);
+	tpm->user_count++;
+	mutex_unlock(&tpm->lock);
+
+	return 0;
+}
+

drivers/pwm/pwm-imx-tpm.c line 396

Quoted 10 lines
drivers/pwm/pwm-imx-tpm.c new file +10 -0
+static void pwm_imx_tpm_free(struct pwm_chip *chip, struct pwm_device *pwm)
+{
+	struct imx_tpm_pwm_chip *tpm = to_imx_tpm_pwm_chip(chip);
+
+	mutex_lock(&tpm->lock);
+	tpm->user_count--;
+	mutex_unlock(&tpm->lock);
+
+	devm_kfree(chip->dev, pwm_get_chip_data(pwm));
+	pwm_set_chip_data(pwm, NULL);

The call to pwm_set_chip_data could better be done in the PWM core.

Quoted 1 line

+}

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

RE: [PATCH V9 2/5] pwm: Add i.MX TPM PWM driver support

Hi, Uwe

Best Regards!
Anson Huang

Quoted 109 lines

-----Original Message-----
From: Uwe Kleine-König [mailto:[email protected]]
Sent: 2019年3月25日 17:30
To: Anson Huang <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Leonard Crestez <[email protected]>; Robin Gong <[email protected]>; [email protected]; linux- [email protected]; [email protected]; linux-arm- [email protected]; [email protected]; dl-linux-imx <[email protected]>
Subject: Re: [PATCH V9 2/5] pwm: Add i.MX TPM PWM driver support

Quoted 5 lines · Anson Huang

i.MX7ULP has TPM(Low Power Timer/Pulse Width Modulation Module) inside, it can support multiple PWM channels, all the channels share same counter and period setting, but each channel can configure its duty and polarity independently.

Quoted 8 lines

There are several TPM modules in i.MX7ULP, the number of channels in TPM modules are different, it can be read from each TPM module's PARAM register.

---
Changes since V8:
- add more limitation notes for period/duty update un-atomic

limitations;

Quoted 2 lines

- add waiting for period/duty update actually applied to HW;
- move the duty update into period update function to make them to

be updated

Quoted 3 lines

as together as possiable;
- don't allow PS change if counter is running;
- save channel polarity settings and return it directly when .get_state

is called,

Quoted 14 lines

as the HW polarity setting could be impacted by enable status.

---
 drivers/pwm/Kconfig       |  11 +
 drivers/pwm/Makefile      |   1 +
 drivers/pwm/pwm-imx-tpm.c | 518
++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 530 insertions(+)
 create mode 100644 drivers/pwm/pwm-imx-tpm.c
drivers/pwm/Kconfig index renamed from drivers/pwm/Kconfig +0 -0

54f8238..3ea0391 100644

drivers/pwm/Kconfig config PWM_IMX27 · line 218

Quoted 9 lines
drivers/pwm/Kconfig +6 -0
@@ -210,6 +210,17 @@ config PWM_IMX27
 	  To compile this driver as a module, choose M here: the module
 	  will be called pwm-imx27.
 
+config PWM_IMX_TPM
+	tristate "i.MX TPM PWM support"
+	depends on ARCH_MXC || COMPILE_TEST
+	depends on HAVE_CLK && HAS_IOMEM
+	help
+	  Generic PWM framework driver for i.MX7ULP TPM module, TPM's

full

drivers/pwm/Makefile obj-$(CONFIG_PWM_HIBVT) += pwm-

Quoted 13 lines
drivers/pwm/Kconfig +5 -0
+	  name is Low Power Timer/Pulse Width Modulation Module.
+
+	  To compile this driver as a module, choose M here: the module
+	  will be called pwm-imx-tpm.
+
 config PWM_JZ4740
 	tristate "Ingenic JZ47xx PWM support"
 	depends on MACH_INGENIC
drivers/pwm/Makefile index renamed from drivers/pwm/Makefile +0 -0

448825e..c368599 100644

drivers/pwm/Makefile +0 -0
@@ -19,6 +19,7 @@ obj-$(CONFIG_PWM_HIBVT)		+= pwm-

hibvt.o

drivers/pwm/pwm-imx-tpm.c

Quoted 8 lines
drivers/pwm/Makefile +1 -0
 obj-$(CONFIG_PWM_IMG)		+= pwm-img.o
 obj-$(CONFIG_PWM_IMX1)		+= pwm-imx1.o
 obj-$(CONFIG_PWM_IMX27)		+= pwm-imx27.o
+obj-$(CONFIG_PWM_IMX_TPM)	+= pwm-imx-tpm.o
 obj-$(CONFIG_PWM_JZ4740)	+= pwm-jz4740.o
 obj-$(CONFIG_PWM_LP3943)	+= pwm-lp3943.o
 obj-$(CONFIG_PWM_LPC18XX_SCT)	+= pwm-lpc18xx-sct.o
drivers/pwm/pwm-imx-tpm.c +0 -0

new

Quoted 11 lines

file mode 100644 index 0000000..58af0915

drivers/pwm/pwm-imx-tpm.c new file +7 -0
@@ -0,0 +1,518 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright 2018-2019 NXP.
+ *
+ * Limitations:
+ * - The TPM counter and period counter are shared between
+ *   multiple channels, so all channels should use same period

drivers/pwm/pwm-imx-tpm.c line 17

Quoted 10 lines
drivers/pwm/pwm-imx-tpm.c new file +10 -0
+ *   settings.
+ * - Changes to polarity cannot be latched at the time of the
+ *   next period start.
+ * - The period and duty changes are NOT atomic, if new period and
+ *   new duty are requested simultaneously when counter is running,
+ *   there could be a small window of running old duty with new
+ *   period, as the period is updated before duty in this driver, the
+ *   probability is very low, ONLY happen when the TPM counter changes
+ *   from MOD to zero between the consecutive update of period and
+ *   duty.

The window that this bug triggers is small, but if it does, the window where the invalid combination is applied, isn't small---it's one complete period if I'm not mistaken. So I'd write:

Quoted 4 lines

- Changing period and duty cycle together isn't atomic. With the wrong timing it might happen that a period is produced with old duty cycle but new period settings.

OK, thanks.

Quoted 171 lines
Quoted 14 lines
drivers/pwm/pwm-imx-tpm.c new file +14 -0
+ */
+
+#include <linux/bitfield.h>
+#include <linux/bitops.h>
+#include <linux/clk.h>
+#include <linux/err.h>
+#include <linux/io.h>
+#include <linux/log2.h>
+#include <linux/module.h>
+#include <linux/of.h>
+#include <linux/of_address.h>
+#include <linux/platform_device.h>
+#include <linux/pwm.h>
+#include <linux/slab.h>

drivers/pwm/pwm-imx-tpm.c

Quoted 10 lines
drivers/pwm/pwm-imx-tpm.c new file +10 -0
+
+#define PWM_IMX_TPM_PARAM	0x4
+#define PWM_IMX_TPM_GLOBAL	0x8
+#define PWM_IMX_TPM_SC		0x10
+#define PWM_IMX_TPM_CNT		0x14
+#define PWM_IMX_TPM_MOD		0x18
+#define PWM_IMX_TPM_CnSC(n)	(0x20 + (n) * 0x8)
+#define PWM_IMX_TPM_CnV(n)	(0x24 + (n) * 0x8)
+
+#define PWM_IMX_TPM_PARAM_CHAN			GENMASK(7,

0)

drivers/pwm/pwm-imx-tpm.c

Quoted 4 lines
drivers/pwm/pwm-imx-tpm.c new file +4 -0
+
+#define PWM_IMX_TPM_SC_PS			GENMASK(2, 0)
+#define PWM_IMX_TPM_SC_CMOD			GENMASK(4, 3)
+#define PWM_IMX_TPM_SC_CMOD_INC_EVERY_CLK
	FIELD_PREP(PWM_IMX_TPM_SC_CMOD, 1)

drivers/pwm/pwm-imx-tpm.c

Quoted 11 lines
drivers/pwm/pwm-imx-tpm.c new file +11 -0
+#define PWM_IMX_TPM_SC_CPWMS			BIT(5)
+
+#define PWM_IMX_TPM_CnSC_CHF	BIT(7)
+#define PWM_IMX_TPM_CnSC_MSB	BIT(5)
+#define PWM_IMX_TPM_CnSC_MSA	BIT(4)
+
+/*
+ * The reference manual describes this field as two separate bits.
+The
+ * samantic of the two bits isn't orthogonal though, so they are
+treated

s/samantic/semantic/

drivers/pwm/pwm-imx-tpm.c

Quoted 5 lines
drivers/pwm/pwm-imx-tpm.c new file +5 -0
+ * together as a 2-bit field here.
+ */
+#define PWM_IMX_TPM_CnSC_ELS	GENMASK(3, 2)
+#define PWM_IMX_TPM_CnSC_ELS_POLARITY_INVERSED	0x1
+#define PWM_IMX_TPM_CnSC_ELS_INVERSED
	FIELD_PREP(PWM_IMX_TPM_CnSC_ELS, 1)

drivers/pwm/pwm-imx-tpm.c line 62

Quoted 1 line
drivers/pwm/pwm-imx-tpm.c new file +1 -0
+#define PWM_IMX_TPM_CnSC_ELS_NORMAL
	FIELD_PREP(PWM_IMX_TPM_CnSC_ELS, 2)

drivers/pwm/pwm-imx-tpm.c

Quoted 4 lines
drivers/pwm/pwm-imx-tpm.c new file +4 -0
+
+
+#define PWM_IMX_TPM_MOD_WIDTH	16
+#define PWM_IMX_TPM_MOD_MOD
	GENMASK(PWM_IMX_TPM_MOD_WIDTH - 1, 0)
Quoted 31 lines
drivers/pwm/pwm-imx-tpm.c new file +31 -0
+
+struct imx_tpm_pwm_chip {
+	struct pwm_chip chip;
+	struct clk *clk;
+	void __iomem *base;
+	struct mutex lock;
+	u32 user_count;
+	u32 enable_count;
+	u32 real_period;
+};
+
+struct imx_tpm_pwm_param {
+	u8 prescale;
+	u32 mod;
+	u32 val;
+};
+
+struct imx_tpm_pwm_channel {
+	enum pwm_polarity polarity;
+};
+
+static inline struct imx_tpm_pwm_chip *to_imx_tpm_pwm_chip(struct
+pwm_chip *chip) {
+	return container_of(chip, struct imx_tpm_pwm_chip, chip); }
+
+static int pwm_imx_tpm_round_state(struct pwm_chip *chip,
+				   struct imx_tpm_pwm_param *p,
+				   struct pwm_state *state,
+				   struct pwm_state *real_state)
+{
+	struct imx_tpm_pwm_chip *tpm = to_imx_tpm_pwm_chip(chip);

drivers/pwm/pwm-imx-tpm.c

Quoted 10 lines
drivers/pwm/pwm-imx-tpm.c new file +10 -0
+	u32 rate, prescale, period_count, clock_unit;
+	u64 tmp;
+
+	rate = clk_get_rate(tpm->clk);
+	tmp = (u64)state->period * rate;
+	clock_unit = DIV_ROUND_CLOSEST_ULL(tmp, NSEC_PER_SEC);
+	if (clock_unit <= PWM_IMX_TPM_MOD_MOD)
+		prescale = 0;
+	else
+		prescale = ilog2(clock_unit) + 1 -

PWM_IMX_TPM_MOD_WIDTH;

Quoted 56 lines
drivers/pwm/pwm-imx-tpm.c new file +56 -0
+
+	if ((!FIELD_FIT(PWM_IMX_TPM_SC_PS, prescale)))
+		return -ERANGE;
+	p->prescale = prescale;
+
+	period_count = (clock_unit + ((1 << prescale) >> 1)) >> prescale;
+	p->mod = period_count;
+
+	/* calculate real period HW can support */
+	tmp = (u64)period_count << prescale;
+	tmp *= NSEC_PER_SEC;
+	real_state->period = DIV_ROUND_CLOSEST_ULL(tmp, rate);
+
+	/*
+	 * if eventually the PWM output is inactive, either
+	 * duty cycle is 0 or status is disabled, need to
+	 * make sure the output pin is inactive.
+	 */
+	if (!state->enabled)
+		real_state->duty_cycle = 0;
+	else
+		real_state->duty_cycle = state->duty_cycle;
+
+	tmp = (u64)p->mod * real_state->duty_cycle;
+	p->val = DIV_ROUND_CLOSEST_ULL(tmp, real_state->period);
+
+	real_state->polarity = state->polarity;
+	real_state->enabled = state->enabled;
+
+	return 0;
+}
+
+/* this function is supposed to be called with mutex hold */ static
+int pwm_imx_tpm_setup_period_duty(struct pwm_chip *chip,
+					 struct pwm_device *pwm,
+					 struct imx_tpm_pwm_param *p)
+{
+	struct imx_tpm_pwm_chip *tpm = to_imx_tpm_pwm_chip(chip);
+	unsigned long timeout;
+	u32 val, cmod, cur_prescale;
+
+	val = readl(tpm->base + PWM_IMX_TPM_SC);
+	cmod = FIELD_GET(PWM_IMX_TPM_SC_CMOD, val);
+	cur_prescale = FIELD_GET(PWM_IMX_TPM_SC_PS, val);
+	if (cmod && cur_prescale != p->prescale)
+		return -EBUSY;
+
+	/* set TPM counter prescale */
+	val &= ~PWM_IMX_TPM_SC_PS;
+	val |= FIELD_PREP(PWM_IMX_TPM_SC_PS, p->prescale);
+	writel(val, tpm->base + PWM_IMX_TPM_SC);
+
+	/*
+	 * set period count:
+	 * if (CMOD[1:0] = 0:0) then MOD register is updated when MOD
+	 * register is written.

drivers/pwm/pwm-imx-tpm.c

Quoted 10 lines
drivers/pwm/pwm-imx-tpm.c new file +10 -0
+	 *
+	 * if (CMOD[1:0] ≠ 0:0), then MOD register is updated according
+	 * to the CPWMS bit, that is:
+	 *
+	 * if the selected mode is not CPWM then MOD register is updated
+	 * after MOD register was written and the TPM counter changes from
+	 * MOD to zero.
+	 *
+	 * if the selected mode is CPWM then MOD register is updated after
+	 * MOD register was written and the TPM counter changes from

MOD

drivers/pwm/pwm-imx-tpm.c line 174

Quoted 1 line
drivers/pwm/pwm-imx-tpm.c new file +1 -0
+	 * to (MOD – 1).

Given that the driver doesn't make use of CPWM, this comment could be simplified. I'd write:

Quoted 5 lines
	/*
	 * If the PWM is enabled (CMOD[1:0] ≠ 2b00), the period length
	 * is latched into hardware when the next period starts.
	 */

OK, thanks.

Quoted 4 lines

This is even true for the (here unused) CPWM mode. (The reference manual isn't entirely clear here IMHO. I assume if MOD == 4000 and CNT == 2001 then MOD is then changed to 2000, the currently running period is completed with a length of 4000 prescaled clk cycles?!)

Based on my understanding from RM, I think so.

Quoted 19 lines
Quoted 19 lines
drivers/pwm/pwm-imx-tpm.c new file +18 -0
+	 */
+	writel(p->mod, tpm->base + PWM_IMX_TPM_MOD);
+
+	/*
+	 * set channel value:
+	 * if (CMOD[1:0] = 0:0) then CnV register is updated when CnV
+	 * register is written.
+	 *
+	 * if (CMOD[1:0] ≠ 0:0), then CnV register is updated according
+	 * to the selected mode, that is:
+	 *
+	 * if the selected mode is output compare then CnV register is
+	 * updated on the next TPM counter increment (end of the prescaler
+	 * counting) after CnV register was written.
+	 *
+	 * if the selected mode is EPWM then CnV register is updated after
+	 * CnV register was written and the TPM counter changes from MOD
+	 * to zero.
Quoted 8 lines

drivers/pwm/pwm-imx-tpm.c line 195

Quoted 4 lines
drivers/pwm/pwm-imx-tpm.c new file +4 -0
+	 *
+	 * if the selected mode is CPWM then CnV register is updated after
+	 * CnV register was written and the TPM counter changes from MOD
+	 * to (MOD – 1).

This is similar to the above too verbose and covers stuff that is not relevant for this driver. Also the used wording is not obvious if you don't look into the reference manual.

OK, thanks.

Quoted 47 lines
Quoted 36 lines
drivers/pwm/pwm-imx-tpm.c new file +34 -0
+	 */
+	writel(p->val, tpm->base + PWM_IMX_TPM_CnV(pwm->hwpwm));
+
+	/* make sure MOD & CnV registers are updated */
+	timeout = jiffies + msecs_to_jiffies(tpm->real_period /
+					     NSEC_PER_MSEC + 1);
+	while (readl(tpm->base + PWM_IMX_TPM_MOD) != p->mod
+	       || readl(tpm->base + PWM_IMX_TPM_CnV(pwm->hwpwm))
+	       != p->val) {
+		if (time_after(jiffies, timeout))
+			return -ETIME;
+		cpu_relax();
+	}
+
+	return 0;
+}
[...]
+static int pwm_imx_tpm_apply(struct pwm_chip *chip,
+			     struct pwm_device *pwm,
+			     struct pwm_state *state)
+{
+	struct imx_tpm_pwm_chip *tpm = to_imx_tpm_pwm_chip(chip);
+	struct imx_tpm_pwm_param param;
+	struct pwm_state real_state;
+	int ret;
+
+	ret = pwm_imx_tpm_round_state(chip, &param, state, &real_state);
+	if (ret)
+		return -EINVAL;
+
+	mutex_lock(&tpm->lock);
+
+	/*
+	 * TPM counter is shared by multiple channels, so
+	 * prescale and period can NOT be modified when

drivers/pwm/pwm-imx-tpm.c

Quoted 10 lines
drivers/pwm/pwm-imx-tpm.c new file +10 -0
+	 * there are multiple channels in use with different
+	 * period settings.
+	 */
+	if (real_state.period != tpm->real_period) {
+		if (tpm->user_count > 1) {
+			ret = -EBUSY;
+			goto exit;
+		}
+
+		ret = pwm_imx_tpm_setup_period_duty(chip, pwm,

&param);

Quoted 11 lines

drivers/pwm/pwm-imx-tpm.c line 361

Quoted 7 lines
drivers/pwm/pwm-imx-tpm.c new file +7 -0
+		if (ret)
+			goto exit;
+
+		tpm->real_period = real_state.period;
+	}
+
+	ret = pwm_imx_tpm_apply_hw(chip, pwm, &real_state);

It's unintuitive here that both pwm_imx_tpm_setup_period_duty and pwm_imx_tpm_apply_hw (potentially) configure the duty cycle. I didn't thought it to an end, but maybe this could be optimised?

This is also my concern when implementing it, since period needs to be configured before duty, and we want to put these 2 configurations as close as possible, so for the period change, I also configure the duty together, but for normal use cases, period does NOT change, so we also need to consider duty change ONLY case, that is why I put a current duty and requested duty check in the pwm_imx_tpm_apply_hw() function.

         if (state->duty_cycle != c.duty_cycle) {
                 /* set duty counter */
                 tmp = readl(tpm->base + PWM_IMX_TPM_MOD) & PWM_IMX_TPM_MOD_MOD;
                 tmp *= state->duty_cycle;
                 val = DIV_ROUND_CLOSEST_ULL(tmp, state->period);
                 writel(val, tpm->base + PWM_IMX_TPM_CnV(pwm->hwpwm));

We can also put all the configurations together in 1 function, but that also introduce some confusion I think, current implementation separate the period settings (for all channels) and other settings (for each channel) in 2 function, it is just because that the duty change is better to be put as close as period change, so I did it this way. Maybe add some comments for it is acceptable?

Quoted 17 lines

drivers/pwm/pwm-imx-tpm.c line 374

Quoted 13 lines
drivers/pwm/pwm-imx-tpm.c new file +12 -0
+exit:
+	mutex_unlock(&tpm->lock);
+
+	return ret;
+}
+
+static int pwm_imx_tpm_request(struct pwm_chip *chip, struct
+pwm_device *pwm) {
+	struct imx_tpm_pwm_chip *tpm = to_imx_tpm_pwm_chip(chip);
+	struct imx_tpm_pwm_channel *chan;
+
+	chan = devm_kzalloc(chip->dev, sizeof(*chan), GFP_KERNEL);

There is no advantage in using the devm variant here as the requested memory is freed in .free anyhow. So this only adds additional memory foodprint and runtime overhead.

Makes sense, I will use kzalloc directly, looks like other pwm driver(drivers/pwm/pwm-samsung.c) also has such "issue".

Quoted 15 lines
Quoted 4 lines
drivers/pwm/pwm-imx-tpm.c new file +3 -0
+	if (!chan)
+		return -ENOMEM;
+

drivers/pwm/pwm-imx-tpm.c

Quoted 10 lines
drivers/pwm/pwm-imx-tpm.c new file +10 -0
+	pwm_set_chip_data(pwm, chan);
+
+	mutex_lock(&tpm->lock);
+	tpm->user_count++;
+	mutex_unlock(&tpm->lock);
+
+	return 0;
+}
+
+static void pwm_imx_tpm_free(struct pwm_chip *chip, struct

pwm_device

Quoted 11 lines

drivers/pwm/pwm-imx-tpm.c line 396

Quoted 9 lines
drivers/pwm/pwm-imx-tpm.c new file +9 -0
+*pwm) {
+	struct imx_tpm_pwm_chip *tpm = to_imx_tpm_pwm_chip(chip);
+
+	mutex_lock(&tpm->lock);
+	tpm->user_count--;
+	mutex_unlock(&tpm->lock);
+
+	devm_kfree(chip->dev, pwm_get_chip_data(pwm));
+	pwm_set_chip_data(pwm, NULL);

The call to pwm_set_chip_data could better be done in the PWM core.

You meant doing a new patch to add it in PWM core.c after ->free call? If yes, then I think this should be another topic, as many other pwm drivers also call it in their own driver, maybe it can be improved by another patch?

Thanks,
Anson.

Quoted 11 lines
Quoted 2 lines

+}

--
Pengutronix e.K. | Uwe Kleine-König |
Industrial Linux Solutions |
https://eur01.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.p
engutronix.de%2F&amp;data=02%7C01%7Canson.huang%40nxp.com%7Ca7 1937c9d5e84033309008d6b1048c3a%7C686ea1d3bc2b4c6fa92cd99c5c30163 5%7C0%7C0%7C636891030423087284&amp;sdata=a3xsu9iaAGvfUYv%2FNo6 T5Uvw6k%2F5VbyI2cFzsrnA4IM%3D&amp;reserved=0 |

maintainer
#2 lore raw

Re: [PATCH V9 1/5] dt-bindings: pwm: Add i.MX TPM PWM binding

In reply to Anson Huang
Quoted 5 lines · Anson Huang

Add i.MX TPM(Low Power Timer/Pulse Width Modulation Module) PWM binding.

---
No changes.

v9? I don't recall seeing any previous versions.

Quoted 11 lines
---
 .../devicetree/bindings/pwm/imx-tpm-pwm.txt        | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/pwm/imx-tpm-pwm.txt
Documentation/devicetree/bindings/pwm/imx-tpm-pwm.txt new file +0 -0
new file mode 100644
index 0000000..94f1ad5

Documentation/devicetree/bindings/pwm/imx-tpm-pwm.txt line 4

Quoted 4 lines
Documentation/devicetree/bindings/pwm/imx-tpm-pwm.txt new file +4 -0
@@ -0,0 +1,22 @@
+Freescale i.MX TPM PWM controller
+
+Required properties:
+- compatible : Should be "fsl,imx-tpm".

Needs to be SoC specific.

Documentation/devicetree/bindings/pwm/imx-tpm-pwm.txt line 15

Quoted 11 lines
Documentation/devicetree/bindings/pwm/imx-tpm-pwm.txt new file +11 -0
+- reg: Physical base address and length of the controller's registers.
+- #pwm-cells: Should be 3. See pwm.txt in this directory for a description of the cells format.
+- clocks : The clock provided by the SoC to drive the PWM.
+- interrupts: The interrupt for the PWM controller.
+
+Note: The TPM counter and period counter are shared between multiple channels, so all channels
+should use same period setting.
+
+Example:
+
+pwm0: tpm@40250000 {

pwm@...

#3 lore raw

Re: [PATCH V9 1/5] dt-bindings: pwm: Add i.MX TPM PWM binding

Quoted 8 lines · Rob Herring
Quoted 5 lines · Anson Huang

Add i.MX TPM(Low Power Timer/Pulse Width Modulation Module) PWM binding.

---
No changes.

v9? I don't recall seeing any previous versions.

Not sure it actually matters, but you were on Cc: for v7 and v8 at least:

https://www.spinics.net/lists/linux-pwm/msg09300.html
https://www.spinics.net/lists/linux-pwm/msg09321.html

Best regards
Uwe

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

RE: [PATCH V9 1/5] dt-bindings: pwm: Add i.MX TPM PWM binding

In reply to Rob Herring

Hi, Rob

Best Regards!
Anson Huang

Quoted 15 lines

-----Original Message-----
From: Rob Herring [mailto:[email protected]]
Sent: 2019年3月26日 4:42
To: Anson Huang <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Leonard Crestez <[email protected]>; Robin Gong <[email protected]>; [email protected]; linux- [email protected]; [email protected]; linux-arm- [email protected]; [email protected]; u.kleine- [email protected]; dl-linux-imx <[email protected]>
Subject: Re: [PATCH V9 1/5] dt-bindings: pwm: Add i.MX TPM PWM binding

On Fri, Mar 22, 2019 at 01:48:05AM +0000, Anson Huang wrote:

Quoted 8 lines
Quoted 1 line

Add i.MX TPM(Low Power Timer/Pulse Width Modulation Module) PWM

binding.

Quoted 4 lines

---
No changes.

v9? I don't recall seeing any previous versions.

I checked my mailbox, you are in the "to" list in every version. The first version's subject is:
[PATCH 1/5] dt-bindings: pwm: Add i.MX TPM PWM binding...

Quoted 8 lines
Quoted 3 lines
---
 .../devicetree/bindings/pwm/imx-tpm-pwm.txt        | 22

++++++++++++++++++++++

Quoted 4 lines
 1 file changed, 22 insertions(+)
 create mode 100644

Documentation/devicetree/bindings/pwm/imx-tpm-pwm.txt

Quoted 13 lines

Documentation/devicetree/bindings/pwm/imx-tpm-pwm.txt line 4

Quoted 11 lines
Documentation/devicetree/bindings/pwm/imx-tpm-pwm.txt new file +4 -0
new file mode 100644
index 0000000..94f1ad5
@@ -0,0 +1,22 @@
+Freescale i.MX TPM PWM controller
+
+Required properties:
+- compatible : Should be "fsl,imx-tpm".

Needs to be SoC specific.

OK, will change it to "fsl,imx7ulp-tpm" in next verson.

Quoted 4 lines

Documentation/devicetree/bindings/pwm/imx-tpm-pwm.txt line 6

Quoted 3 lines
Documentation/devicetree/bindings/pwm/imx-tpm-pwm.txt new file +2 -0
+- reg: Physical base address and length of the controller's registers.
+- #pwm-cells: Should be 3. See pwm.txt in this directory for a description

of the cells format.

Quoted 11 lines

Documentation/devicetree/bindings/pwm/imx-tpm-pwm.txt line 15

Quoted 9 lines
Documentation/devicetree/bindings/pwm/imx-tpm-pwm.txt new file +9 -0
+- clocks : The clock provided by the SoC to drive the PWM.
+- interrupts: The interrupt for the PWM controller.
+
+Note: The TPM counter and period counter are shared between multiple
+channels, so all channels should use same period setting.
+
+Example:
+
+pwm0: tpm@40250000 {

pwm@...

I forgot to update the example in DT binding, sorry for that, will update it according to below dts node:

                 pwm0: pwm@40250000 {

Anson.
_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

#6 lore raw

RE: [PATCH V9 1/5] dt-bindings: pwm: Add i.MX TPM PWM binding

Hi, Rob

Best Regards!
Anson Huang

Quoted 69 lines

-----Original Message-----
From: Anson Huang
Sent: 2019年3月26日 8:56
To: 'Rob Herring' <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Leonard Crestez <[email protected]>; Robin Gong <[email protected]>; [email protected]; linux- [email protected]; [email protected]; linux-arm- [email protected]; [email protected]; u.kleine- [email protected]; dl-linux-imx <[email protected]>
Subject: RE: [PATCH V9 1/5] dt-bindings: pwm: Add i.MX TPM PWM binding

Hi, Rob

Best Regards!
Anson Huang

Quoted 15 lines

-----Original Message-----
From: Rob Herring [mailto:[email protected]]
Sent: 2019年3月26日 4:42
To: Anson Huang <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Leonard Crestez <[email protected]>; Robin Gong <[email protected]>; [email protected]; linux- [email protected]; [email protected]; linux-arm- [email protected]; [email protected]; u.kleine- [email protected]; dl-linux-imx <[email protected]>
Subject: Re: [PATCH V9 1/5] dt-bindings: pwm: Add i.MX TPM PWM binding

On Fri, Mar 22, 2019 at 01:48:05AM +0000, Anson Huang wrote:

Quoted 8 lines
Quoted 1 line

Add i.MX TPM(Low Power Timer/Pulse Width Modulation Module) PWM

binding.

Quoted 4 lines

---
No changes.

v9? I don't recall seeing any previous versions.

I checked my mailbox, you are in the "to" list in every version. The first version's subject is:
[PATCH 1/5] dt-bindings: pwm: Add i.MX TPM PWM binding...

Quoted 8 lines
Quoted 3 lines
---
 .../devicetree/bindings/pwm/imx-tpm-pwm.txt        | 22

++++++++++++++++++++++

Quoted 4 lines
 1 file changed, 22 insertions(+)
 create mode 100644

Documentation/devicetree/bindings/pwm/imx-tpm-pwm.txt

Quoted 13 lines

Documentation/devicetree/bindings/pwm/imx-tpm-pwm.txt line 4

Quoted 11 lines
Documentation/devicetree/bindings/pwm/imx-tpm-pwm.txt new file +4 -0
new file mode 100644
index 0000000..94f1ad5
@@ -0,0 +1,22 @@
+Freescale i.MX TPM PWM controller
+
+Required properties:
+- compatible : Should be "fsl,imx-tpm".

Needs to be SoC specific.

Quoted 1 line

OK, will change it to "fsl,imx7ulp-tpm" in next verson.

As " fsl,imx7ulp-tpm " is already used for timer, TPM can work as a timer or PWM, so I have to use " fsl,imx7ulp-pwm" in the V10 patch I just sent.

Anson.

Quoted 24 lines
Quoted 6 lines

Documentation/devicetree/bindings/pwm/imx-tpm-pwm.txt line 7

Quoted 5 lines
Documentation/devicetree/bindings/pwm/imx-tpm-pwm.txt new file +3 -0
+- reg: Physical base address and length of the controller's registers.
+- #pwm-cells: Should be 3. See pwm.txt in this directory for a
+description

of the cells format.

Quoted 11 lines

Documentation/devicetree/bindings/pwm/imx-tpm-pwm.txt line 15

Quoted 9 lines
Documentation/devicetree/bindings/pwm/imx-tpm-pwm.txt new file +9 -0
+- clocks : The clock provided by the SoC to drive the PWM.
+- interrupts: The interrupt for the PWM controller.
+
+Note: The TPM counter and period counter are shared between
+multiple channels, so all channels should use same period setting.
+
+Example:
+
+pwm0: tpm@40250000 {

pwm@...

I forgot to update the example in DT binding, sorry for that, will update it according to below dts node:

                 pwm0: pwm@40250000 {

Anson.

#10 lore raw

RE: [PATCH V9 2/5] pwm: Add i.MX TPM PWM driver support

In reply to Anson Huang

Hi, Uwe

Best Regards!
Anson Huang

Quoted 429 lines

-----Original Message-----
From: Anson Huang
Sent: 2019年3月25日 19:59
To: 'Uwe Kleine-König' <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Leonard Crestez <[email protected]>; Robin Gong <[email protected]>; [email protected]; linux- [email protected]; [email protected]; linux-arm- [email protected]; [email protected]; dl-linux-imx <[email protected]>
Subject: RE: [PATCH V9 2/5] pwm: Add i.MX TPM PWM driver support

Hi, Uwe

Best Regards!
Anson Huang

Quoted 110 lines

-----Original Message-----
From: Uwe Kleine-König [mailto:[email protected]]
Sent: 2019年3月25日 17:30
To: Anson Huang <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Leonard Crestez <[email protected]>; Robin Gong <[email protected]>; [email protected]; linux- [email protected]; [email protected]; linux-arm- [email protected]; [email protected]; dl-linux-imx <[email protected]>
Subject: Re: [PATCH V9 2/5] pwm: Add i.MX TPM PWM driver support

Quoted 5 lines · Anson Huang

i.MX7ULP has TPM(Low Power Timer/Pulse Width Modulation Module) inside, it can support multiple PWM channels, all the channels share same counter and period setting, but each channel can configure its duty and polarity independently.

Quoted 8 lines

There are several TPM modules in i.MX7ULP, the number of channels in TPM modules are different, it can be read from each TPM module's PARAM register.

---
Changes since V8:
- add more limitation notes for period/duty update un-atomic

limitations;

Quoted 2 lines

- add waiting for period/duty update actually applied to HW;
- move the duty update into period update function to make them to

be updated

Quoted 4 lines

as together as possiable;
- don't allow PS change if counter is running;
- save channel polarity settings and return it directly when
.get_state

is called,

Quoted 14 lines

as the HW polarity setting could be impacted by enable status.

---
 drivers/pwm/Kconfig       |  11 +
 drivers/pwm/Makefile      |   1 +
 drivers/pwm/pwm-imx-tpm.c | 518
++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 530 insertions(+)
 create mode 100644 drivers/pwm/pwm-imx-tpm.c
drivers/pwm/Kconfig index renamed from drivers/pwm/Kconfig +0 -0

54f8238..3ea0391 100644

drivers/pwm/Kconfig config PWM_IMX27 · line 218

Quoted 9 lines
drivers/pwm/Kconfig +6 -0
@@ -210,6 +210,17 @@ config PWM_IMX27
 	  To compile this driver as a module, choose M here: the module
 	  will be called pwm-imx27.
 
+config PWM_IMX_TPM
+	tristate "i.MX TPM PWM support"
+	depends on ARCH_MXC || COMPILE_TEST
+	depends on HAVE_CLK && HAS_IOMEM
+	help
+	  Generic PWM framework driver for i.MX7ULP TPM module, TPM's

full

drivers/pwm/Makefile obj-$(CONFIG_PWM_HIBVT) += pwm-

Quoted 13 lines
drivers/pwm/Kconfig +5 -0
+	  name is Low Power Timer/Pulse Width Modulation Module.
+
+	  To compile this driver as a module, choose M here: the module
+	  will be called pwm-imx-tpm.
+
 config PWM_JZ4740
 	tristate "Ingenic JZ47xx PWM support"
 	depends on MACH_INGENIC
drivers/pwm/Makefile index renamed from drivers/pwm/Makefile +0 -0

448825e..c368599 100644

drivers/pwm/Makefile +0 -0
@@ -19,6 +19,7 @@ obj-$(CONFIG_PWM_HIBVT)		+= pwm-

hibvt.o

drivers/pwm/pwm-imx-tpm.c

Quoted 8 lines
drivers/pwm/Makefile +1 -0
 obj-$(CONFIG_PWM_IMG)		+= pwm-img.o
 obj-$(CONFIG_PWM_IMX1)		+= pwm-imx1.o
 obj-$(CONFIG_PWM_IMX27)		+= pwm-imx27.o
+obj-$(CONFIG_PWM_IMX_TPM)	+= pwm-imx-tpm.o
 obj-$(CONFIG_PWM_JZ4740)	+= pwm-jz4740.o
 obj-$(CONFIG_PWM_LP3943)	+= pwm-lp3943.o
 obj-$(CONFIG_PWM_LPC18XX_SCT)	+= pwm-lpc18xx-sct.o
drivers/pwm/pwm-imx-tpm.c +0 -0

new

Quoted 11 lines

file mode 100644 index 0000000..58af0915

drivers/pwm/pwm-imx-tpm.c new file +7 -0
@@ -0,0 +1,518 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright 2018-2019 NXP.
+ *
+ * Limitations:
+ * - The TPM counter and period counter are shared between
+ *   multiple channels, so all channels should use same period

drivers/pwm/pwm-imx-tpm.c line 17

Quoted 10 lines
drivers/pwm/pwm-imx-tpm.c new file +10 -0
+ *   settings.
+ * - Changes to polarity cannot be latched at the time of the
+ *   next period start.
+ * - The period and duty changes are NOT atomic, if new period and
+ *   new duty are requested simultaneously when counter is running,
+ *   there could be a small window of running old duty with new
+ *   period, as the period is updated before duty in this driver, the
+ *   probability is very low, ONLY happen when the TPM counter changes
+ *   from MOD to zero between the consecutive update of period and
+ *   duty.

The window that this bug triggers is small, but if it does, the window where the invalid combination is applied, isn't small---it's one complete period if I'm not mistaken. So I'd write:

Quoted 4 lines

- Changing period and duty cycle together isn't atomic. With the wrong timing it might happen that a period is produced with old duty cycle but new period settings.

OK, thanks.

Quoted 25 lines
Quoted 14 lines
drivers/pwm/pwm-imx-tpm.c new file +14 -0
+ */
+
+#include <linux/bitfield.h>
+#include <linux/bitops.h>
+#include <linux/clk.h>
+#include <linux/err.h>
+#include <linux/io.h>
+#include <linux/log2.h>
+#include <linux/module.h>
+#include <linux/of.h>
+#include <linux/of_address.h>
+#include <linux/platform_device.h>
+#include <linux/pwm.h>
+#include <linux/slab.h>

drivers/pwm/pwm-imx-tpm.c

Quoted 10 lines
drivers/pwm/pwm-imx-tpm.c new file +10 -0
+
+#define PWM_IMX_TPM_PARAM	0x4
+#define PWM_IMX_TPM_GLOBAL	0x8
+#define PWM_IMX_TPM_SC		0x10
+#define PWM_IMX_TPM_CNT		0x14
+#define PWM_IMX_TPM_MOD		0x18
+#define PWM_IMX_TPM_CnSC(n)	(0x20 + (n) * 0x8)
+#define PWM_IMX_TPM_CnV(n)	(0x24 + (n) * 0x8)
+
+#define PWM_IMX_TPM_PARAM_CHAN			GENMASK(7,

0)

Quoted 3 lines

drivers/pwm/pwm-imx-tpm.c

Quoted 3 lines
drivers/pwm/pwm-imx-tpm.c new file +3 -0
+
+#define PWM_IMX_TPM_SC_PS			GENMASK(2, 0)
+#define PWM_IMX_TPM_SC_CMOD			GENMASK(4,

3)

Quoted 142 lines

drivers/pwm/pwm-imx-tpm.c line 45

Quoted 1 line
drivers/pwm/pwm-imx-tpm.c new file +1 -0
+#define PWM_IMX_TPM_SC_CMOD_INC_EVERY_CLK
	FIELD_PREP(PWM_IMX_TPM_SC_CMOD, 1)

drivers/pwm/pwm-imx-tpm.c

Quoted 11 lines
drivers/pwm/pwm-imx-tpm.c new file +11 -0
+#define PWM_IMX_TPM_SC_CPWMS			BIT(5)
+
+#define PWM_IMX_TPM_CnSC_CHF	BIT(7)
+#define PWM_IMX_TPM_CnSC_MSB	BIT(5)
+#define PWM_IMX_TPM_CnSC_MSA	BIT(4)
+
+/*
+ * The reference manual describes this field as two separate bits.
+The
+ * samantic of the two bits isn't orthogonal though, so they are
+treated

s/samantic/semantic/

drivers/pwm/pwm-imx-tpm.c

Quoted 5 lines
drivers/pwm/pwm-imx-tpm.c new file +5 -0
+ * together as a 2-bit field here.
+ */
+#define PWM_IMX_TPM_CnSC_ELS	GENMASK(3, 2)
+#define PWM_IMX_TPM_CnSC_ELS_POLARITY_INVERSED	0x1
+#define PWM_IMX_TPM_CnSC_ELS_INVERSED
	FIELD_PREP(PWM_IMX_TPM_CnSC_ELS, 1)

drivers/pwm/pwm-imx-tpm.c line 62

Quoted 1 line
drivers/pwm/pwm-imx-tpm.c new file +1 -0
+#define PWM_IMX_TPM_CnSC_ELS_NORMAL
	FIELD_PREP(PWM_IMX_TPM_CnSC_ELS, 2)

drivers/pwm/pwm-imx-tpm.c

Quoted 4 lines
drivers/pwm/pwm-imx-tpm.c new file +4 -0
+
+
+#define PWM_IMX_TPM_MOD_WIDTH	16
+#define PWM_IMX_TPM_MOD_MOD
	GENMASK(PWM_IMX_TPM_MOD_WIDTH - 1, 0)
Quoted 30 lines
drivers/pwm/pwm-imx-tpm.c new file +30 -0
+
+struct imx_tpm_pwm_chip {
+	struct pwm_chip chip;
+	struct clk *clk;
+	void __iomem *base;
+	struct mutex lock;
+	u32 user_count;
+	u32 enable_count;
+	u32 real_period;
+};
+
+struct imx_tpm_pwm_param {
+	u8 prescale;
+	u32 mod;
+	u32 val;
+};
+
+struct imx_tpm_pwm_channel {
+	enum pwm_polarity polarity;
+};
+
+static inline struct imx_tpm_pwm_chip *to_imx_tpm_pwm_chip(struct
+pwm_chip *chip) {
+	return container_of(chip, struct imx_tpm_pwm_chip, chip); }
+
+static int pwm_imx_tpm_round_state(struct pwm_chip *chip,
+				   struct imx_tpm_pwm_param *p,
+				   struct pwm_state *state,
+				   struct pwm_state *real_state) {
+	struct imx_tpm_pwm_chip *tpm = to_imx_tpm_pwm_chip(chip);

drivers/pwm/pwm-imx-tpm.c

Quoted 10 lines
drivers/pwm/pwm-imx-tpm.c new file +10 -0
+	u32 rate, prescale, period_count, clock_unit;
+	u64 tmp;
+
+	rate = clk_get_rate(tpm->clk);
+	tmp = (u64)state->period * rate;
+	clock_unit = DIV_ROUND_CLOSEST_ULL(tmp, NSEC_PER_SEC);
+	if (clock_unit <= PWM_IMX_TPM_MOD_MOD)
+		prescale = 0;
+	else
+		prescale = ilog2(clock_unit) + 1 -

PWM_IMX_TPM_MOD_WIDTH;

Quoted 56 lines
drivers/pwm/pwm-imx-tpm.c new file +56 -0
+
+	if ((!FIELD_FIT(PWM_IMX_TPM_SC_PS, prescale)))
+		return -ERANGE;
+	p->prescale = prescale;
+
+	period_count = (clock_unit + ((1 << prescale) >> 1)) >> prescale;
+	p->mod = period_count;
+
+	/* calculate real period HW can support */
+	tmp = (u64)period_count << prescale;
+	tmp *= NSEC_PER_SEC;
+	real_state->period = DIV_ROUND_CLOSEST_ULL(tmp, rate);
+
+	/*
+	 * if eventually the PWM output is inactive, either
+	 * duty cycle is 0 or status is disabled, need to
+	 * make sure the output pin is inactive.
+	 */
+	if (!state->enabled)
+		real_state->duty_cycle = 0;
+	else
+		real_state->duty_cycle = state->duty_cycle;
+
+	tmp = (u64)p->mod * real_state->duty_cycle;
+	p->val = DIV_ROUND_CLOSEST_ULL(tmp, real_state->period);
+
+	real_state->polarity = state->polarity;
+	real_state->enabled = state->enabled;
+
+	return 0;
+}
+
+/* this function is supposed to be called with mutex hold */ static
+int pwm_imx_tpm_setup_period_duty(struct pwm_chip *chip,
+					 struct pwm_device *pwm,
+					 struct imx_tpm_pwm_param *p)
+{
+	struct imx_tpm_pwm_chip *tpm = to_imx_tpm_pwm_chip(chip);
+	unsigned long timeout;
+	u32 val, cmod, cur_prescale;
+
+	val = readl(tpm->base + PWM_IMX_TPM_SC);
+	cmod = FIELD_GET(PWM_IMX_TPM_SC_CMOD, val);
+	cur_prescale = FIELD_GET(PWM_IMX_TPM_SC_PS, val);
+	if (cmod && cur_prescale != p->prescale)
+		return -EBUSY;
+
+	/* set TPM counter prescale */
+	val &= ~PWM_IMX_TPM_SC_PS;
+	val |= FIELD_PREP(PWM_IMX_TPM_SC_PS, p->prescale);
+	writel(val, tpm->base + PWM_IMX_TPM_SC);
+
+	/*
+	 * set period count:
+	 * if (CMOD[1:0] = 0:0) then MOD register is updated when MOD
+	 * register is written.

drivers/pwm/pwm-imx-tpm.c

Quoted 10 lines
drivers/pwm/pwm-imx-tpm.c new file +10 -0
+	 *
+	 * if (CMOD[1:0] ≠ 0:0), then MOD register is updated according
+	 * to the CPWMS bit, that is:
+	 *
+	 * if the selected mode is not CPWM then MOD register is updated
+	 * after MOD register was written and the TPM counter changes from
+	 * MOD to zero.
+	 *
+	 * if the selected mode is CPWM then MOD register is updated after
+	 * MOD register was written and the TPM counter changes from

MOD

drivers/pwm/pwm-imx-tpm.c line 173

Quoted 1 line
drivers/pwm/pwm-imx-tpm.c new file +1 -0
+	 * to (MOD – 1).

Given that the driver doesn't make use of CPWM, this comment could be simplified. I'd write:

Quoted 5 lines
	/*
	 * If the PWM is enabled (CMOD[1:0] ≠ 2b00), the period length
	 * is latched into hardware when the next period starts.
	 */

OK, thanks.

Quoted 4 lines

This is even true for the (here unused) CPWM mode. (The reference manual isn't entirely clear here IMHO. I assume if MOD == 4000 and CNT == 2001 then MOD is then changed to 2000, the currently running period is completed with a length of 4000 prescaled clk cycles?!)

Based on my understanding from RM, I think so.

Quoted 19 lines
Quoted 19 lines
drivers/pwm/pwm-imx-tpm.c new file +18 -0
+	 */
+	writel(p->mod, tpm->base + PWM_IMX_TPM_MOD);
+
+	/*
+	 * set channel value:
+	 * if (CMOD[1:0] = 0:0) then CnV register is updated when CnV
+	 * register is written.
+	 *
+	 * if (CMOD[1:0] ≠ 0:0), then CnV register is updated according
+	 * to the selected mode, that is:
+	 *
+	 * if the selected mode is output compare then CnV register is
+	 * updated on the next TPM counter increment (end of the prescaler
+	 * counting) after CnV register was written.
+	 *
+	 * if the selected mode is EPWM then CnV register is updated after
+	 * CnV register was written and the TPM counter changes from MOD
+	 * to zero.
Quoted 8 lines

drivers/pwm/pwm-imx-tpm.c line 195

Quoted 4 lines
drivers/pwm/pwm-imx-tpm.c new file +4 -0
+	 *
+	 * if the selected mode is CPWM then CnV register is updated after
+	 * CnV register was written and the TPM counter changes from MOD
+	 * to (MOD – 1).

This is similar to the above too verbose and covers stuff that is not relevant for this driver. Also the used wording is not obvious if you don't look into the reference manual.

OK, thanks.

Quoted 47 lines
Quoted 36 lines
drivers/pwm/pwm-imx-tpm.c new file +34 -0
+	 */
+	writel(p->val, tpm->base + PWM_IMX_TPM_CnV(pwm->hwpwm));
+
+	/* make sure MOD & CnV registers are updated */
+	timeout = jiffies + msecs_to_jiffies(tpm->real_period /
+					     NSEC_PER_MSEC + 1);
+	while (readl(tpm->base + PWM_IMX_TPM_MOD) != p->mod
+	       || readl(tpm->base + PWM_IMX_TPM_CnV(pwm->hwpwm))
+	       != p->val) {
+		if (time_after(jiffies, timeout))
+			return -ETIME;
+		cpu_relax();
+	}
+
+	return 0;
+}
[...]
+static int pwm_imx_tpm_apply(struct pwm_chip *chip,
+			     struct pwm_device *pwm,
+			     struct pwm_state *state)
+{
+	struct imx_tpm_pwm_chip *tpm = to_imx_tpm_pwm_chip(chip);
+	struct imx_tpm_pwm_param param;
+	struct pwm_state real_state;
+	int ret;
+
+	ret = pwm_imx_tpm_round_state(chip, &param, state, &real_state);
+	if (ret)
+		return -EINVAL;
+
+	mutex_lock(&tpm->lock);
+
+	/*
+	 * TPM counter is shared by multiple channels, so
+	 * prescale and period can NOT be modified when

drivers/pwm/pwm-imx-tpm.c

Quoted 10 lines
drivers/pwm/pwm-imx-tpm.c new file +10 -0
+	 * there are multiple channels in use with different
+	 * period settings.
+	 */
+	if (real_state.period != tpm->real_period) {
+		if (tpm->user_count > 1) {
+			ret = -EBUSY;
+			goto exit;
+		}
+
+		ret = pwm_imx_tpm_setup_period_duty(chip, pwm,

&param);

Quoted 11 lines

drivers/pwm/pwm-imx-tpm.c line 361

Quoted 7 lines
drivers/pwm/pwm-imx-tpm.c new file +7 -0
+		if (ret)
+			goto exit;
+
+		tpm->real_period = real_state.period;
+	}
+
+	ret = pwm_imx_tpm_apply_hw(chip, pwm, &real_state);

It's unintuitive here that both pwm_imx_tpm_setup_period_duty and pwm_imx_tpm_apply_hw (potentially) configure the duty cycle. I didn't thought it to an end, but maybe this could be optimised?

This is also my concern when implementing it, since period needs to be configured before duty, and we want to put these 2 configurations as close as possible, so for the period change, I also configure the duty together, but for normal use cases, period does NOT change, so we also need to consider duty change ONLY case, that is why I put a current duty and requested duty check in the pwm_imx_tpm_apply_hw() function.

         if (state->duty_cycle != c.duty_cycle) {
                 /* set duty counter */
                 tmp = readl(tpm->base + PWM_IMX_TPM_MOD) &
PWM_IMX_TPM_MOD_MOD;
                 tmp *= state->duty_cycle;
                 val = DIV_ROUND_CLOSEST_ULL(tmp, state->period);
                 writel(val, tpm->base + PWM_IMX_TPM_CnV(pwm->hwpwm));
Quoted 6 lines

We can also put all the configurations together in 1 function, but that also introduce some confusion I think, current implementation separate the period settings (for all channels) and other settings (for each channel) in 2 function, it is just because that the duty change is better to be put as close as period change, so I did it this way. Maybe add some comments for it is acceptable?

I just sent out V10 patch set to remove the pwm_imx_tpm_setup_period_duty() function, and put all the configurations in pwm_imx_tpm_apply_hw() function, the defect introduced would be a slightly latency between period update and duty update, it is trivial I think, but it can avoid the duplicated code/function of setting duty.

Thanks.
Anson.

Quoted 64 lines
Quoted 19 lines
Quoted 5 lines
drivers/pwm/pwm-imx-tpm.c new file +2 -0
+exit:
+	mutex_unlock(&tpm->lock);

drivers/pwm/pwm-imx-tpm.c line 374

Quoted 10 lines
drivers/pwm/pwm-imx-tpm.c new file +10 -0
+
+	return ret;
+}
+
+static int pwm_imx_tpm_request(struct pwm_chip *chip, struct
+pwm_device *pwm) {
+	struct imx_tpm_pwm_chip *tpm = to_imx_tpm_pwm_chip(chip);
+	struct imx_tpm_pwm_channel *chan;
+
+	chan = devm_kzalloc(chip->dev, sizeof(*chan), GFP_KERNEL);

There is no advantage in using the devm variant here as the requested memory is freed in .free anyhow. So this only adds additional memory foodprint and runtime overhead.

Makes sense, I will use kzalloc directly, looks like other pwm driver(drivers/pwm/pwm-samsung.c) also has such "issue".

Quoted 15 lines
Quoted 4 lines
drivers/pwm/pwm-imx-tpm.c new file +3 -0
+	if (!chan)
+		return -ENOMEM;
+

drivers/pwm/pwm-imx-tpm.c

Quoted 10 lines
drivers/pwm/pwm-imx-tpm.c new file +10 -0
+	pwm_set_chip_data(pwm, chan);
+
+	mutex_lock(&tpm->lock);
+	tpm->user_count++;
+	mutex_unlock(&tpm->lock);
+
+	return 0;
+}
+
+static void pwm_imx_tpm_free(struct pwm_chip *chip, struct

pwm_device

Quoted 11 lines

drivers/pwm/pwm-imx-tpm.c line 396

Quoted 9 lines
drivers/pwm/pwm-imx-tpm.c new file +9 -0
+*pwm) {
+	struct imx_tpm_pwm_chip *tpm = to_imx_tpm_pwm_chip(chip);
+
+	mutex_lock(&tpm->lock);
+	tpm->user_count--;
+	mutex_unlock(&tpm->lock);
+
+	devm_kfree(chip->dev, pwm_get_chip_data(pwm));
+	pwm_set_chip_data(pwm, NULL);

The call to pwm_set_chip_data could better be done in the PWM core.

You meant doing a new patch to add it in PWM core.c after ->free call? If yes, then I think this should be another topic, as many other pwm drivers also call it in their own driver, maybe it can be improved by another patch?

Thanks,
Anson.

Quoted 7 lines
Quoted 2 lines

+}

--
Pengutronix e.K. | Uwe Kleine-König |
Industrial Linux Solutions |

Quoted 8 lines

https://eur01.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.p

Quoted 1 line

engutronix.de%2F&amp;data=02%7C01%7Canson.huang%40nxp.com%7Ca7

Quoted 1 line

1937c9d5e84033309008d6b1048c3a%7C686ea1d3bc2b4c6fa92cd99c5c30163

Quoted 1 line

5%7C0%7C0%7C636891030423087284&amp;sdata=a3xsu9iaAGvfUYv%2FNo6

Quoted 1 line

T5Uvw6k%2F5VbyI2cFzsrnA4IM%3D&amp;reserved=0 |

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

maintainer
#4 lore raw

Re: [PATCH V9 1/5] dt-bindings: pwm: Add i.MX TPM PWM binding

In reply to Uwe Kleine-König
Quoted 9 lines · Uwe Kleine-König
Quoted 7 lines · Rob Herring
Quoted 5 lines · Anson Huang

Add i.MX TPM(Low Power Timer/Pulse Width Modulation Module) PWM binding.

---
No changes.

Quoted 7 lines
Quoted 1 line

v9? I don't recall seeing any previous versions.

Not sure it actually matters, but you were on Cc: for v7 and v8 at least:

https://www.spinics.net/lists/linux-pwm/msg09300.html
https://www.spinics.net/lists/linux-pwm/msg09321.html

10 versions now in 2 weeks is why. Reviewers do get busy and/or go on vacation. Please slow down your pace and give people time to review.

Rob