Skip to contentSkip to conversation

Add i.MX7ULP EVK PWM backlight support

PATCH Superseded Anson Huang 2 participants 12 messages Mar 13, 2019 – Mar 14, 2019

Subsystems (4)

v2 of 12 Superseded Latest is v12 →

lore
Share & tools
lore
#0 lore raw

v2 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.

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        |  23 ++
 arch/arm/boot/dts/imx7ulp-evk.dts                  |  20 ++
 arch/arm/boot/dts/imx7ulp.dtsi                     |  11 +
 arch/arm/configs/imx_v6_v7_defconfig               |   1 +
 drivers/pwm/Kconfig                                |   9 +
 drivers/pwm/Makefile                               |   1 +
 drivers/pwm/pwm-imx-tpm.c                          | 277 +++++++++++++++++++++
 7 files changed, 342 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 v2 1/5 dt-bindings: pwm: Add i.MX TPM PWM binding

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

---
Changes since V1:
- remove optional property which is no need now.

---
 Documentation/devicetree/bindings/pwm/imx-tpm-pwm.txt | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/pwm/imx-tpm-pwm.txt
Documentation/devicetree/bindings/pwm/imx-tpm-pwm.txt new file +19 -0
new file mode 100644
index 0000000..d47b8fb
@@ -0,0 +1,19 @@
+Freescale i.MX TPM PWM controller
+
+Required properties:
+- compatible : Should be "fsl,imx-tpm-pwm".
+- reg: Physical base address and length of the controller's registers.
+- #pwm-cells: Should be 2. 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.
+
+Example:
+
+pwm0: tpm@40250000 {
+	compatible = "fsl,imx-tpm-pwm";
+	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 = <2>;
+};
Signature · 1 line
2.7.4
#2 lore raw

PATCH v2 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, add TPM PWM driver support.

---
Changes since V1:
- improve coding style, function name's prefix;
- improve Kconfig's help info;
- use .apply instead for .enable/.disable/.config etc. to simply the code;
- improve clock operation, make clock enabled during probe phase and ONLY disabled when suspend, as register read/write need to sync with clock, keeping it enabled makes the register read/write simple;
- improve prescale calculation;
- add error message print during probe for ioremap and clk get;

---
 drivers/pwm/Kconfig       |  10 ++
 drivers/pwm/Makefile      |   1 +
 drivers/pwm/pwm-imx-tpm.c | 332 ++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 343 insertions(+)
 create mode 100644 drivers/pwm/pwm-imx-tpm.c
drivers/pwm/Kconfig +10 -0
index a8f47df..c1cbb43 100644
@@ -201,6 +201,16 @@ config PWM_IMX
 	  To compile this driver as a module, choose M here: the module
 	  will be called pwm-imx.
 
+config PWM_IMX_TPM
+	tristate "i.MX TPM PWM support"
+	depends on ARCH_MXC
+	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 9c676a0..64e036c 100644
@@ -18,6 +18,7 @@ obj-$(CONFIG_PWM_FSL_FTM)	+= pwm-fsl-ftm.o
 obj-$(CONFIG_PWM_HIBVT)		+= pwm-hibvt.o
 obj-$(CONFIG_PWM_IMG)		+= pwm-img.o
 obj-$(CONFIG_PWM_IMX)		+= pwm-imx.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 +332 -0

332 lines not on this page · raw message

Signature · 1 line
2.7.4
#3 lore raw

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

Quoted 26 lines · Anson Huang

i.MX7ULP has TPM(Low Power Timer/Pulse Width Modulation Module) inside, add TPM PWM driver support.

---
Changes since V1:
- improve coding style, function name's prefix;
- improve Kconfig's help info;
- use .apply instead for .enable/.disable/.config etc. to simply the code;
- improve clock operation, make clock enabled during probe phase and ONLY disabled when suspend, as register read/write need to sync with clock, keeping it enabled makes the register read/write simple;
- improve prescale calculation;
- add error message print during probe for ioremap and clk get;

---
 drivers/pwm/Kconfig       |  10 ++
 drivers/pwm/Makefile      |   1 +
 drivers/pwm/pwm-imx-tpm.c | 332 ++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 343 insertions(+)
 create mode 100644 drivers/pwm/pwm-imx-tpm.c
drivers/pwm/Kconfig +0 -0
index a8f47df..c1cbb43 100644

drivers/pwm/Kconfig config PWM_IMX · line 206

Quoted 6 lines
drivers/pwm/Kconfig +3 -0
@@ -201,6 +201,16 @@ config PWM_IMX
 	  To compile this driver as a module, choose M here: the module
 	  will be called pwm-imx.
 
+config PWM_IMX_TPM
+	tristate "i.MX TPM PWM support"
+	depends on ARCH_MXC

Can you please make this

	depends on ARCH_MXC || COMPILE_TEST
	depends on HAVE_CLK && HAS_IOMEM
Quoted 42 lines
drivers/pwm/Kconfig +7 -0
+	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 9c676a0..64e036c 100644
@@ -18,6 +18,7 @@ obj-$(CONFIG_PWM_FSL_FTM)	+= pwm-fsl-ftm.o
 obj-$(CONFIG_PWM_HIBVT)		+= pwm-hibvt.o
 obj-$(CONFIG_PWM_IMG)		+= pwm-img.o
 obj-$(CONFIG_PWM_IMX)		+= pwm-imx.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 +14 -0
new file mode 100644
index 0000000..8c1a1ba
@@ -0,0 +1,332 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright 2018-2019 NXP.
+ */
+
+#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>

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

Quoted 10 lines
drivers/pwm/pwm-imx-tpm.c new file +10 -0
+#include <linux/pwm.h>
+#include <linux/slab.h>
+#include <linux/spinlock.h>
+
+#define TPM_GLOBAL	0x8
+#define TPM_SC		0x10
+#define TPM_CNT		0x14
+#define TPM_MOD		0x18
+#define TPM_C0SC	0x20
+#define TPM_C0V		0x24

PWM_IMX_TPM_COV etc. please

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

Quoted 3 lines
drivers/pwm/pwm-imx-tpm.c new file +3 -0
+
+#define TPM_SC_CMOD_SHIFT	3
+#define TPM_SC_CMOD_MASK	(0x3 << TPM_SC_CMOD_SHIFT)

If you use the macros that are documented in <linux/bitops.h> you don't need these MASK and SHIFT stuff.

Quoted 5 lines
drivers/pwm/pwm-imx-tpm.c new file +5 -0
+#define TPM_SC_CPWMS		BIT(5)
+
+#define TPM_CnSC_CHF		BIT(7)
+#define TPM_CnSC_MSnB		BIT(5)
+#define TPM_CnSC_MSnA		BIT(4)

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

Quoted 10 lines
drivers/pwm/pwm-imx-tpm.c new file +10 -0
+#define TPM_CnSC_ELSnB		BIT(3)
+#define TPM_CnSC_ELSnA		BIT(2)
+
+#define TPM_SC_PS_MASK		0x7
+#define TPM_MOD_MOD_MASK	0xffff
+
+#define TPM_COUNT_MAX		0xffff
+
+#define TPM_CHn_ADDR_OFFSET	0x8
+#define TPM_DEFAULT_PWM_CHANNEL_NUM	2

Is this better called "..._MAX_..." instead of "..._DEFAULT_..."? This is used as array size below and when reading

	u32 chn_config[TPM_DEFAULT_PWM_CHANNEL_NUM];

I wonder if the actual number of PWMs can be bigger than the default.

Quoted 15 lines
drivers/pwm/pwm-imx-tpm.c new file +15 -0
+struct imx_tpm_pwm_chip {
+	struct pwm_chip chip;
+	struct clk *clk;
+	void __iomem *base;
+	spinlock_t lock;
+	u32 chn_config[TPM_DEFAULT_PWM_CHANNEL_NUM];
+	bool chn_status[TPM_DEFAULT_PWM_CHANNEL_NUM];
+};
+
+#define to_imx_tpm_pwm_chip(_chip)	container_of(_chip, struct imx_tpm_pwm_chip, chip)
+
+static void imx_tpm_pwm_config_counter(struct pwm_chip *chip, u32 period)
+{
+	struct imx_tpm_pwm_chip *tpm = to_imx_tpm_pwm_chip(chip);
+	unsigned int period_cnt;

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

Quoted 10 lines
drivers/pwm/pwm-imx-tpm.c new file +10 -0
+	u32 val, div;
+	u64 tmp;
+
+	tmp = clk_get_rate(tpm->clk);
+	tmp *= period;
+	val = DIV_ROUND_CLOSEST_ULL(tmp, NSEC_PER_SEC);
+	if (val < TPM_COUNT_MAX)
+		div = 0;
+	else
+		div = ilog2(roundup_pow_of_two(val / TPM_COUNT_MAX));

Are you sure you have to divide by TPM_COUNT_MAX and not by (TPM_COUNT_MAX + 1)?

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

Quoted 4 lines
drivers/pwm/pwm-imx-tpm.c new file +4 -0
+	if (div > TPM_SC_PS_MASK) {
+		dev_err(chip->dev,
+			"failed to find valid prescale value!\n");
+		return;

I think you should handle this failure and make imx_tpm_pwm_apply() fail.

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

Quoted 6 lines
drivers/pwm/pwm-imx-tpm.c new file +6 -0
+	}
+	/* set TPM counter prescale */
+	val = readl(tpm->base + TPM_SC);
+	val &= ~TPM_SC_PS_MASK;
+	val |= div;
+	writel(val, tpm->base + TPM_SC);

According to the documentation PS can only be written if the counter is disabled, I think this isn't ensured here.

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

Quoted 4 lines
drivers/pwm/pwm-imx-tpm.c new file +4 -0
+	/* set period counter */
+	do_div(tmp, NSEC_PER_SEC);
+	period_cnt = DIV_ROUND_CLOSEST_ULL(tmp, 1 << div);
+	writel(period_cnt & TPM_MOD_MOD_MASK, tpm->base + TPM_MOD);

If there is already a value pending in the MOD register, another write to it is ignored. A comment, why this cannot happen, would be appropriate. (Note, I'm not sure this cannot happen.)

Quoted 11 lines
drivers/pwm/pwm-imx-tpm.c new file +11 -0
+}
+
+static void imx_tpm_pwm_config(struct pwm_chip *chip,
+			       struct pwm_device *pwm,
+			       struct pwm_state *state)
+{
+	struct imx_tpm_pwm_chip *tpm = to_imx_tpm_pwm_chip(chip);
+	static bool tpm_cnt_initialized;
+	unsigned int duty_cnt;
+	u32 val;
+	u64 tmp;

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

Quoted 10 lines
drivers/pwm/pwm-imx-tpm.c new file +10 -0
+
+	/*
+	 * TPM counter is shared by multi channels, let's make it to be
+	 * ONLY first channel can config TPM counter's precale and period
+	 * count.
+	 */
+	if (!tpm_cnt_initialized) {
+		imx_tpm_pwm_config_counter(chip, state->period);
+		tpm_cnt_initialized = true;
+	}

So the period can only be configured once. That is not as good as it could be. You should allow a change whenever there is exactly one PWM in use.

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

Quoted 4 lines
drivers/pwm/pwm-imx-tpm.c new file +4 -0
+	/* set duty counter */
+	tmp = readl(tpm->base + TPM_MOD) & TPM_MOD_MOD_MASK;
+	tmp *= state->duty_cycle;
+	duty_cnt = DIV_ROUND_CLOSEST_ULL(tmp, state->period);

Uah, you use state->period here even though for the 2nd PWM the Divider might not be setup appropriately.

Quoted 9 lines

[...]

drivers/pwm/pwm-imx-tpm.c new file +8 -0
+static int imx_tpm_pwm_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 pwm_state curstate;
+	unsigned long flags;
+
+	imx_tpm_pwm_get_state(chip, pwm, &curstate);

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

Quoted 10 lines
drivers/pwm/pwm-imx-tpm.c new file +10 -0
+
+	spin_lock_irqsave(&tpm->lock, flags);
+
+	if (state->period != curstate.period ||
+	    state->duty_cycle != curstate.duty_cycle ||
+	    state->polarity != curstate.polarity)
+		imx_tpm_pwm_config(chip, pwm, state);
+
+	if (state->enabled != curstate.enabled)
+		imx_tpm_pwm_enable(chip, pwm, state->enabled);

This is wrong. This sequence:

	pwm_apply_state(pwm, { .duty_cycle = 0, .period = 10000, .enabled = true });
	pwm_apply_state(pwm, { .duty_cycle = 10000, .period = 10000, .enabled = false });

must keep the output pin low which isn't guaranteed here.

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

Quoted 5 lines
drivers/pwm/pwm-imx-tpm.c new file +5 -0
+
+	spin_unlock_irqrestore(&tpm->lock, flags);
+
+	return 0;
+}

I didn't look in depth through the complete driver yet, but there is IIRC still some feedback on v1 that wasn't addressed in v2 (because v2 was sent before the last feedback). So I will look in more depth in v3 (assuming it comes late enough address the concerns from this mail).

Best regards
Uwe

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

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

Quoted 4 lines · Uwe Kleine-König

I didn't look in depth through the complete driver yet, but there is IIRC still some feedback on v1 that wasn't addressed in v2 (because v2 was sent before the last feedback). So I will look in more depth in v3 (assuming it comes late enough address the concerns from this mail).

I just noticed v3 in my inbox. So my concerns here are not addressed. So I will wait for v4.

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 V2 2/5] pwm: Add i.MX TPM PWM driver support

In reply to Uwe Kleine-König

Hi, Uwe

Best Regards!
Anson Huang

Quoted 48 lines

-----Original Message-----
From: Uwe Kleine-König [mailto:[email protected]]
Sent: 2019年3月14日 17:17
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]>; [email protected]; [email protected]; Robin Gong <[email protected]>; [email protected]; [email protected]; [email protected]; linux- [email protected]; dl-linux-imx <[email protected]>
Subject: Re: [PATCH V2 2/5] pwm: Add i.MX TPM PWM driver support

Quoted 9 lines · Anson Huang

i.MX7ULP has TPM(Low Power Timer/Pulse Width Modulation Module) inside, add TPM PWM driver support.

---
Changes since V1:
- improve coding style, function name's prefix;
- improve Kconfig's help info;
- use .apply instead for .enable/.disable/.config etc. to simply the

code;

Quoted 1 line

- improve clock operation, make clock enabled during probe phase

and ONLY disabled

Quoted 1 line

when suspend, as register read/write need to sync with clock,

keeping it enabled

Quoted 19 lines

makes the register read/write simple;
- improve prescale calculation;
- add error message print during probe for ioremap and clk get;

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

a8f47df..c1cbb43 100644

drivers/pwm/Kconfig +0 -0
@@ -201,6 +201,16 @@ config PWM_IMX
 	  To compile this driver as a module, choose M here: the module
 	  will be called pwm-imx.
 
Quoted 9 lines

drivers/pwm/Kconfig config PWM_IMX · line 206

Quoted 3 lines
drivers/pwm/Kconfig +3 -0
+config PWM_IMX_TPM
+	tristate "i.MX TPM PWM support"
+	depends on ARCH_MXC

Can you please make this

	depends on ARCH_MXC || COMPILE_TEST
	depends on HAVE_CLK && HAS_IOMEM

OK.

Quoted 47 lines

drivers/pwm/Kconfig config PWM_IMX · line 208

Quoted 2 lines
drivers/pwm/Kconfig +2 -0
+	help
+	  Generic PWM framework driver for i.MX7ULP TPM module, TPM's

full

drivers/pwm/pwm-imx-tpm.c

Quoted 21 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

9c676a0..64e036c 100644

drivers/pwm/Makefile +1 -0
@@ -18,6 +18,7 @@ obj-$(CONFIG_PWM_FSL_FTM)	+= pwm-fsl-ftm.o
 obj-$(CONFIG_PWM_HIBVT)		+= pwm-hibvt.o
 obj-$(CONFIG_PWM_IMG)		+= pwm-img.o
 obj-$(CONFIG_PWM_IMX)		+= pwm-imx.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 22 lines

file mode 100644 index 0000000..8c1a1ba

drivers/pwm/pwm-imx-tpm.c new file +18 -0
@@ -0,0 +1,332 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright 2018-2019 NXP.
+ */
+
+#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>
+#include <linux/spinlock.h>
+
Quoted 8 lines

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

Quoted 6 lines
drivers/pwm/pwm-imx-tpm.c new file +6 -0
+#define TPM_GLOBAL	0x8
+#define TPM_SC		0x10
+#define TPM_CNT		0x14
+#define TPM_MOD		0x18
+#define TPM_C0SC	0x20
+#define TPM_C0V		0x24

PWM_IMX_TPM_COV etc. please

I thought with IMX_ as prefix should be enough, but OK, I can change it to PWM_IMX_

Quoted 8 lines

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

Quoted 4 lines
drivers/pwm/pwm-imx-tpm.c new file +3 -0
+
+#define TPM_SC_CMOD_SHIFT	3
+#define TPM_SC_CMOD_MASK	(0x3 << TPM_SC_CMOD_SHIFT)

If you use the macros that are documented in <linux/bitops.h> you don't need these MASK and SHIFT stuff.

Already use GENMASK(h,l) in V3.

Quoted 19 lines
Quoted 5 lines
drivers/pwm/pwm-imx-tpm.c new file +5 -0
+#define TPM_SC_CPWMS		BIT(5)
+
+#define TPM_CnSC_CHF		BIT(7)
+#define TPM_CnSC_MSnB		BIT(5)
+#define TPM_CnSC_MSnA		BIT(4)

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

Quoted 10 lines
drivers/pwm/pwm-imx-tpm.c new file +10 -0
+#define TPM_CnSC_ELSnB		BIT(3)
+#define TPM_CnSC_ELSnA		BIT(2)
+
+#define TPM_SC_PS_MASK		0x7
+#define TPM_MOD_MOD_MASK	0xffff
+
+#define TPM_COUNT_MAX		0xffff
+
+#define TPM_CHn_ADDR_OFFSET	0x8
+#define TPM_DEFAULT_PWM_CHANNEL_NUM	2

Is this better called "..._MAX_..." instead of "..._DEFAULT_..."? This is used as array size below and when reading

Quoted 3 lines
	u32 chn_config[TPM_DEFAULT_PWM_CHANNEL_NUM];

I wonder if the actual number of PWMs can be bigger than the default.

OK, will use _MAX_ for this, I double checked i.MX7ULP RM, looks like some TPM modules have ONLY 2 channel, but some have 6 channels, so I will change it to 6.

Quoted 22 lines

drivers/pwm/pwm-imx-tpm.c

Quoted 11 lines
drivers/pwm/pwm-imx-tpm.c new file +10 -0
+struct imx_tpm_pwm_chip {
+	struct pwm_chip chip;
+	struct clk *clk;
+	void __iomem *base;
+	spinlock_t lock;
+	u32 chn_config[TPM_DEFAULT_PWM_CHANNEL_NUM];
+	bool chn_status[TPM_DEFAULT_PWM_CHANNEL_NUM];
+};
+
+#define to_imx_tpm_pwm_chip(_chip)	container_of(_chip, struct

imx_tpm_pwm_chip, chip)

Quoted 10 lines
drivers/pwm/pwm-imx-tpm.c new file +10 -0
+
+static void imx_tpm_pwm_config_counter(struct pwm_chip *chip, u32
+period) {
+	struct imx_tpm_pwm_chip *tpm = to_imx_tpm_pwm_chip(chip);
+	unsigned int period_cnt;
+	u32 val, div;
+	u64 tmp;
+
+	tmp = clk_get_rate(tpm->clk);
+	tmp *= period;
Quoted 8 lines

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

Quoted 5 lines
drivers/pwm/pwm-imx-tpm.c new file +5 -0
+	val = DIV_ROUND_CLOSEST_ULL(tmp, NSEC_PER_SEC);
+	if (val < TPM_COUNT_MAX)
+		div = 0;
+	else
+		div = ilog2(roundup_pow_of_two(val / TPM_COUNT_MAX));

Are you sure you have to divide by TPM_COUNT_MAX and not by (TPM_COUNT_MAX + 1)?

Ah, yes, when counter reach the TPM_COUNT_MAX, next value will from 0 in counting up mode we used, will use TPM_COUNT_MAX + 1.

Quoted 7 lines

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

Quoted 5 lines
drivers/pwm/pwm-imx-tpm.c new file +4 -0
+	if (div > TPM_SC_PS_MASK) {
+		dev_err(chip->dev,
+			"failed to find valid prescale value!\n");
+		return;

I think you should handle this failure and make imx_tpm_pwm_apply() fail.

OK.

Quoted 10 lines

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

Quoted 7 lines
drivers/pwm/pwm-imx-tpm.c new file +6 -0
+	}
+	/* set TPM counter prescale */
+	val = readl(tpm->base + TPM_SC);
+	val &= ~TPM_SC_PS_MASK;
+	val |= div;
+	writel(val, tpm->base + TPM_SC);

According to the documentation PS can only be written if the counter is disabled, I think this isn't ensured here.

OK, I will make sure counter is disabled before writing it.

Quoted 10 lines

drivers/pwm/pwm-imx-tpm.c

Quoted 5 lines
drivers/pwm/pwm-imx-tpm.c new file +4 -0
+	/* set period counter */
+	do_div(tmp, NSEC_PER_SEC);
+	period_cnt = DIV_ROUND_CLOSEST_ULL(tmp, 1 << div);
+	writel(period_cnt & TPM_MOD_MOD_MASK, tpm->base +

TPM_MOD);

If there is already a value pending in the MOD register, another write to it is ignored. A comment, why this cannot happen, would be appropriate. (Note, I'm not sure this cannot happen.)

Currently, we ONLY initialize the MOD register ONCE, so it does NOT have this case, if we make the MOD register can be updated, then maybe I can read back the register value after writing the new value, to make sure the register value is what we want to write?

Quoted 17 lines
Quoted 17 lines
drivers/pwm/pwm-imx-tpm.c new file +16 -0
+}
+
+static void imx_tpm_pwm_config(struct pwm_chip *chip,
+			       struct pwm_device *pwm,
+			       struct pwm_state *state)
+{
+	struct imx_tpm_pwm_chip *tpm = to_imx_tpm_pwm_chip(chip);
+	static bool tpm_cnt_initialized;
+	unsigned int duty_cnt;
+	u32 val;
+	u64 tmp;
+
+	/*
+	 * TPM counter is shared by multi channels, let's make it to be
+	 * ONLY first channel can config TPM counter's precale and period
+	 * count.
Quoted 8 lines

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

Quoted 5 lines
drivers/pwm/pwm-imx-tpm.c new file +5 -0
+	 */
+	if (!tpm_cnt_initialized) {
+		imx_tpm_pwm_config_counter(chip, state->period);
+		tpm_cnt_initialized = true;
+	}

So the period can only be configured once. That is not as good as it could be. You should allow a change whenever there is exactly one PWM in use.

OK, maybe I can add check for other channels' statue here, and allow the period update if ONLY 1 channel is enabled.

Quoted 8 lines

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

Quoted 5 lines
drivers/pwm/pwm-imx-tpm.c new file +4 -0
+	/* set duty counter */
+	tmp = readl(tpm->base + TPM_MOD) & TPM_MOD_MOD_MASK;
+	tmp *= state->duty_cycle;
+	duty_cnt = DIV_ROUND_CLOSEST_ULL(tmp, state->period);

Uah, you use state->period here even though for the 2nd PWM the Divider might not be setup appropriately.

I think that is 1 limitation here, the dts should make sure the period used for different channels are same or at least they can share same divider, otherwise, what if multiple channels can NOT find a divider good for every channel? How to deal with this case?

Quoted 24 lines
Quoted 3 lines

[...]
+static int imx_tpm_pwm_apply(struct pwm_chip *chip, struct pwm_device

*pwm,

Quoted 7 lines
drivers/pwm/pwm-imx-tpm.c new file +7 -0
+			  struct pwm_state *state)
+{
+	struct imx_tpm_pwm_chip *tpm = to_imx_tpm_pwm_chip(chip);
+	struct pwm_state curstate;
+	unsigned long flags;
+
+	imx_tpm_pwm_get_state(chip, pwm, &curstate);

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

Quoted 10 lines
drivers/pwm/pwm-imx-tpm.c new file +10 -0
+
+	spin_lock_irqsave(&tpm->lock, flags);
+
+	if (state->period != curstate.period ||
+	    state->duty_cycle != curstate.duty_cycle ||
+	    state->polarity != curstate.polarity)
+		imx_tpm_pwm_config(chip, pwm, state);
+
+	if (state->enabled != curstate.enabled)
+		imx_tpm_pwm_enable(chip, pwm, state->enabled);

This is wrong. This sequence:

Quoted 6 lines
	pwm_apply_state(pwm, { .duty_cycle = 0, .period = 10000, .enabled =
true });
	pwm_apply_state(pwm, { .duty_cycle = 10000, .period =

10000, .enabled = false });

must keep the output pin low which isn't guaranteed here.

So you mean for every .apply operation, the channel MUST be disabled first, then config it, then enable it?

Quoted 11 lines

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

Quoted 6 lines
drivers/pwm/pwm-imx-tpm.c new file +5 -0
+
+	spin_unlock_irqrestore(&tpm->lock, flags);
+
+	return 0;
+}

I didn't look in depth through the complete driver yet, but there is IIRC still some feedback on v1 that wasn't addressed in v2 (because v2 was sent before the last feedback). So I will look in more depth in v3 (assuming it comes late enough address the concerns from this mail).

Thanks, will address all comments before sending out V4.

Anson.

Quoted 4 lines

Best regards
Uwe

Quoted 8 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%7C8a 834d252f5b4b7c446b08d6a85dde9e%7C686ea1d3bc2b4c6fa92cd99c5c30163 5%7C0%7C0%7C636881518529747245&amp;sdata=ozlYfyVaAgMP6JL%2FxS% 2FThygVfpqvuyvL7AVpJkHZRtw%3D&amp;reserved=0 |

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

#6 lore raw

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

Hello,

Quoted 24 lines · Anson Huang
Quoted 15 lines · Anson Huang
Quoted 14 lines
drivers/pwm/pwm-imx-tpm.c new file +14 -0
+static void imx_tpm_pwm_config(struct pwm_chip *chip,
+			       struct pwm_device *pwm,
+			       struct pwm_state *state)
+{
+	struct imx_tpm_pwm_chip *tpm = to_imx_tpm_pwm_chip(chip);
+	static bool tpm_cnt_initialized;
+	unsigned int duty_cnt;
+	u32 val;
+	u64 tmp;
+
+	/*
+	 * TPM counter is shared by multi channels, let's make it to be
+	 * ONLY first channel can config TPM counter's precale and period
+	 * count.
Quoted 8 lines

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

Quoted 5 lines
drivers/pwm/pwm-imx-tpm.c new file +5 -0
+	 */
+	if (!tpm_cnt_initialized) {
+		imx_tpm_pwm_config_counter(chip, state->period);
+		tpm_cnt_initialized = true;
+	}

So the period can only be configured once. That is not as good as it could be. You should allow a change whenever there is exactly one PWM in use.

Quoted 2 lines

OK, maybe I can add check for other channels' statue here, and allow the period update if ONLY 1 channel is enabled.

See how the SiFive patch that I already pointed out solves this same problem.

Quoted 8 lines
Quoted 7 lines

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

Quoted 4 lines
drivers/pwm/pwm-imx-tpm.c new file +4 -0
+	/* set duty counter */
+	tmp = readl(tpm->base + TPM_MOD) & TPM_MOD_MOD_MASK;
+	tmp *= state->duty_cycle;
+	duty_cnt = DIV_ROUND_CLOSEST_ULL(tmp, state->period);

Uah, you use state->period here even though for the 2nd PWM the Divider might not be setup appropriately.

Quoted 4 lines

I think that is 1 limitation here, the dts should make sure the period used for different channels are same or at least they can share same divider, otherwise, what if multiple channels can NOT find a divider good for every channel? How to deal with this case?

You should return -ERANGE or -EINVAL for the calls that cannot be satisfied.

Quoted 27 lines
Quoted 27 lines
Quoted 9 lines

[...]

drivers/pwm/pwm-imx-tpm.c new file +8 -0
+static int imx_tpm_pwm_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 pwm_state curstate;
+	unsigned long flags;
+
+	imx_tpm_pwm_get_state(chip, pwm, &curstate);

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

Quoted 10 lines
drivers/pwm/pwm-imx-tpm.c new file +10 -0
+
+	spin_lock_irqsave(&tpm->lock, flags);
+
+	if (state->period != curstate.period ||
+	    state->duty_cycle != curstate.duty_cycle ||
+	    state->polarity != curstate.polarity)
+		imx_tpm_pwm_config(chip, pwm, state);
+
+	if (state->enabled != curstate.enabled)
+		imx_tpm_pwm_enable(chip, pwm, state->enabled);

This is wrong. This sequence:

	pwm_apply_state(pwm, { .duty_cycle = 0, .period = 10000, .enabled =
true });
	pwm_apply_state(pwm, { .duty_cycle = 10000, .period =

10000, .enabled = false });

Quoted 4 lines
Quoted 1 line

must keep the output pin low which isn't guaranteed here.

So you mean for every .apply operation, the channel MUST be disabled first, then config it, then enable it?

No. I only say that you should not configure the new period and duty cycle if in the end the hardware should be disabled. Always disabling is wrong, too.

Best regards
Uwe

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

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

Hi, Uwe

Best Regards!
Anson Huang

Quoted 38 lines

-----Original Message-----
From: Uwe Kleine-König [mailto:[email protected]]
Sent: 2019年3月14日 18:01
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]>; [email protected]; [email protected]; Robin Gong <[email protected]>; [email protected]; [email protected]; [email protected]; linux- [email protected]; dl-linux-imx <[email protected]>
Subject: Re: [PATCH V2 2/5] pwm: Add i.MX TPM PWM driver support

Hello,

Quoted 13 lines · Anson Huang
Quoted 13 lines · Anson Huang
Quoted 12 lines
drivers/pwm/pwm-imx-tpm.c new file +12 -0
+static void imx_tpm_pwm_config(struct pwm_chip *chip,
+			       struct pwm_device *pwm,
+			       struct pwm_state *state) {
+	struct imx_tpm_pwm_chip *tpm = to_imx_tpm_pwm_chip(chip);
+	static bool tpm_cnt_initialized;
+	unsigned int duty_cnt;
+	u32 val;
+	u64 tmp;
+
+	/*
+	 * TPM counter is shared by multi channels, let's make it to be
+	 * ONLY first channel can config TPM counter's precale and period
Quoted 8 lines
Quoted 8 lines

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

Quoted 6 lines
drivers/pwm/pwm-imx-tpm.c new file +6 -0
+	 * count.
+	 */
+	if (!tpm_cnt_initialized) {
+		imx_tpm_pwm_config_counter(chip, state->period);
+		tpm_cnt_initialized = true;
+	}

So the period can only be configured once. That is not as good as it could

Quoted 7 lines

be.

Quoted 4 lines
Quoted 1 line

You should allow a change whenever there is exactly one PWM in use.

OK, maybe I can add check for other channels' statue here, and allow the period update if ONLY 1 channel is enabled.

See how the SiFive patch that I already pointed out solves this same problem.

Thanks, I will add a user_count to determine how many channels are used.

Quoted 9 lines
Quoted 9 lines
Quoted 8 lines

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

Quoted 5 lines
drivers/pwm/pwm-imx-tpm.c new file +4 -0
+	/* set duty counter */
+	tmp = readl(tpm->base + TPM_MOD) & TPM_MOD_MOD_MASK;
+	tmp *= state->duty_cycle;
+	duty_cnt = DIV_ROUND_CLOSEST_ULL(tmp, state->period);

Uah, you use state->period here even though for the 2nd PWM the Divider might not be setup appropriately.

Quoted 6 lines
Quoted 4 lines

I think that is 1 limitation here, the dts should make sure the period used for different channels are same or at least they can share same divider, otherwise, what if multiple channels can NOT find a divider good for every channel? How to deal with this case?

You should return -ERANGE or -EINVAL for the calls that cannot be satisfied.

In my latest implementation, I have added the period setting check at the beginning of the .apply, as the limitation states, all channels should use same period settings, so the period here is same for all channels, once it is different, the .apply call will return failed and the duty cycle setting will be skipped as well.

232 if (state->period != curstate.period) {
233 /*

234                  * TPM counter is shared by multiple channels, so the prescale
235                  * and period can NOT be modified when any other channel is used.
236                  */

237 if (tpm->user_count != 1)
238 return -EBUSY;
239 ret = imx_tpm_pwm_config_counter(chip, state->period);
240 if (ret)
241 return ret;
242 }
243
244 if (!state->enabled)
245 duty_cycle = 0;
246
247 if (state->duty_cycle != curstate.duty_cycle ||
248 state->polarity != curstate.polarity)
249 imx_tpm_pwm_config(chip, pwm,
250 state->period, duty_cycle, state->polarity);

Quoted 29 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

[...]
+static int imx_tpm_pwm_apply(struct pwm_chip *chip, struct

pwm_device *pwm,

Quoted 25 lines
Quoted 25 lines
Quoted 7 lines
drivers/pwm/pwm-imx-tpm.c new file +7 -0
+			  struct pwm_state *state)
+{
+	struct imx_tpm_pwm_chip *tpm = to_imx_tpm_pwm_chip(chip);
+	struct pwm_state curstate;
+	unsigned long flags;
+
+	imx_tpm_pwm_get_state(chip, pwm, &curstate);

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

Quoted 10 lines
drivers/pwm/pwm-imx-tpm.c new file +10 -0
+
+	spin_lock_irqsave(&tpm->lock, flags);
+
+	if (state->period != curstate.period ||
+	    state->duty_cycle != curstate.duty_cycle ||
+	    state->polarity != curstate.polarity)
+		imx_tpm_pwm_config(chip, pwm, state);
+
+	if (state->enabled != curstate.enabled)
+		imx_tpm_pwm_enable(chip, pwm, state->enabled);

This is wrong. This sequence:

	pwm_apply_state(pwm, { .duty_cycle = 0, .period = 10000, .enabled =
true });
	pwm_apply_state(pwm, { .duty_cycle = 10000, .period = 10000,

.enabled = false });

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

must keep the output pin low which isn't guaranteed here.

So you mean for every .apply operation, the channel MUST be disabled first, then config it, then enable it?

No. I only say that you should not configure the new period and duty cycle if in the end the hardware should be disabled. Always disabling is wrong, too.

Understand now, I will check the state->enable first, if it is false, I will set the duty_cycle to 0 before configuring the channel.

Anson.

Quoted 11 lines

Best regards
Uwe

--
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%7C6f3 0bb40fb9d460cd73a08d6a863f4c9%7C686ea1d3bc2b4c6fa92cd99c5c301635 %7C0%7C0%7C636881544600449875&amp;sdata=SRBTn%2BcquzRRRLrzOTG vlC5WNvof28J0%2B77iSxtFVYA%3D&amp;reserved=0 |

#8 lore raw

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

In reply to Uwe Kleine-König

Hi, Uwe

Best Regards!
Anson Huang

Quoted 48 lines

-----Original Message-----
From: Uwe Kleine-König [mailto:[email protected]]
Sent: 2019年3月14日 17:17
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]>; [email protected]; [email protected]; Robin Gong <[email protected]>; [email protected]; [email protected]; [email protected]; linux- [email protected]; dl-linux-imx <[email protected]>
Subject: Re: [PATCH V2 2/5] pwm: Add i.MX TPM PWM driver support

Quoted 9 lines · Anson Huang

i.MX7ULP has TPM(Low Power Timer/Pulse Width Modulation Module) inside, add TPM PWM driver support.

---
Changes since V1:
- improve coding style, function name's prefix;
- improve Kconfig's help info;
- use .apply instead for .enable/.disable/.config etc. to simply the

code;

Quoted 1 line

- improve clock operation, make clock enabled during probe phase

and ONLY disabled

Quoted 1 line

when suspend, as register read/write need to sync with clock,

keeping it enabled

Quoted 19 lines

makes the register read/write simple;
- improve prescale calculation;
- add error message print during probe for ioremap and clk get;

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

a8f47df..c1cbb43 100644

drivers/pwm/Kconfig +0 -0
@@ -201,6 +201,16 @@ config PWM_IMX
 	  To compile this driver as a module, choose M here: the module
 	  will be called pwm-imx.
 
Quoted 8 lines

drivers/pwm/Kconfig config PWM_IMX · line 206

Quoted 3 lines
drivers/pwm/Kconfig +3 -0
+config PWM_IMX_TPM
+	tristate "i.MX TPM PWM support"
+	depends on ARCH_MXC

Can you please make this

	depends on ARCH_MXC || COMPILE_TEST
	depends on HAVE_CLK && HAS_IOMEM

Will fix it in V4.

Quoted 48 lines

drivers/pwm/Kconfig config PWM_IMX · line 208

Quoted 3 lines
drivers/pwm/Kconfig +2 -0
+	help
+	  Generic PWM framework driver for i.MX7ULP TPM module, TPM's

full

drivers/pwm/pwm-imx-tpm.c

Quoted 21 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

9c676a0..64e036c 100644

drivers/pwm/Makefile +1 -0
@@ -18,6 +18,7 @@ obj-$(CONFIG_PWM_FSL_FTM)	+= pwm-fsl-ftm.o
 obj-$(CONFIG_PWM_HIBVT)		+= pwm-hibvt.o
 obj-$(CONFIG_PWM_IMG)		+= pwm-img.o
 obj-$(CONFIG_PWM_IMX)		+= pwm-imx.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 22 lines

file mode 100644 index 0000000..8c1a1ba

drivers/pwm/pwm-imx-tpm.c new file +18 -0
@@ -0,0 +1,332 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright 2018-2019 NXP.
+ */
+
+#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>
+#include <linux/spinlock.h>
+
Quoted 8 lines

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

Quoted 6 lines
drivers/pwm/pwm-imx-tpm.c new file +6 -0
+#define TPM_GLOBAL	0x8
+#define TPM_SC		0x10
+#define TPM_CNT		0x14
+#define TPM_MOD		0x18
+#define TPM_C0SC	0x20
+#define TPM_C0V		0x24

PWM_IMX_TPM_COV etc. please

Will fix it in V4.

Quoted 7 lines

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

Quoted 4 lines
drivers/pwm/pwm-imx-tpm.c new file +3 -0
+
+#define TPM_SC_CMOD_SHIFT	3
+#define TPM_SC_CMOD_MASK	(0x3 << TPM_SC_CMOD_SHIFT)

If you use the macros that are documented in <linux/bitops.h> you don't need these MASK and SHIFT stuff.

I will use below in V4:
#define PWM_IMX_TPM_SC_CMOD GENMASK(4, 3)
#define PWM_IMX_TPM_SC_CMOD_INC_EVERY_CLK BIT(3)

Quoted 20 lines
Quoted 6 lines
drivers/pwm/pwm-imx-tpm.c new file +5 -0
+#define TPM_SC_CPWMS		BIT(5)
+
+#define TPM_CnSC_CHF		BIT(7)
+#define TPM_CnSC_MSnB		BIT(5)
+#define TPM_CnSC_MSnA		BIT(4)

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

Quoted 10 lines
drivers/pwm/pwm-imx-tpm.c new file +10 -0
+#define TPM_CnSC_ELSnB		BIT(3)
+#define TPM_CnSC_ELSnA		BIT(2)
+
+#define TPM_SC_PS_MASK		0x7
+#define TPM_MOD_MOD_MASK	0xffff
+
+#define TPM_COUNT_MAX		0xffff
+
+#define TPM_CHn_ADDR_OFFSET	0x8
+#define TPM_DEFAULT_PWM_CHANNEL_NUM	2

Is this better called "..._MAX_..." instead of "..._DEFAULT_..."? This is used as array size below and when reading

Quoted 4 lines
	u32 chn_config[TPM_DEFAULT_PWM_CHANNEL_NUM];

I wonder if the actual number of PWMs can be bigger than the default.

Will use below in V4:
#define PWM_IMX_TPM_MAX_CHANNEL_NUM 6

Quoted 21 lines

drivers/pwm/pwm-imx-tpm.c

Quoted 10 lines
drivers/pwm/pwm-imx-tpm.c new file +10 -0
+struct imx_tpm_pwm_chip {
+	struct pwm_chip chip;
+	struct clk *clk;
+	void __iomem *base;
+	spinlock_t lock;
+	u32 chn_config[TPM_DEFAULT_PWM_CHANNEL_NUM];
+	bool chn_status[TPM_DEFAULT_PWM_CHANNEL_NUM];
+};
+
+#define to_imx_tpm_pwm_chip(_chip)	container_of(_chip, struct

imx_tpm_pwm_chip, chip)

Quoted 10 lines
drivers/pwm/pwm-imx-tpm.c new file +10 -0
+
+static void imx_tpm_pwm_config_counter(struct pwm_chip *chip, u32
+period) {
+	struct imx_tpm_pwm_chip *tpm = to_imx_tpm_pwm_chip(chip);
+	unsigned int period_cnt;
+	u32 val, div;
+	u64 tmp;
+
+	tmp = clk_get_rate(tpm->clk);
+	tmp *= period;
Quoted 8 lines

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

Quoted 5 lines
drivers/pwm/pwm-imx-tpm.c new file +5 -0
+	val = DIV_ROUND_CLOSEST_ULL(tmp, NSEC_PER_SEC);
+	if (val < TPM_COUNT_MAX)
+		div = 0;
+	else
+		div = ilog2(roundup_pow_of_two(val / TPM_COUNT_MAX));

Are you sure you have to divide by TPM_COUNT_MAX and not by (TPM_COUNT_MAX + 1)?

Fix it in V4.

Quoted 7 lines

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

Quoted 5 lines
drivers/pwm/pwm-imx-tpm.c new file +4 -0
+	if (div > TPM_SC_PS_MASK) {
+		dev_err(chip->dev,
+			"failed to find valid prescale value!\n");
+		return;

I think you should handle this failure and make imx_tpm_pwm_apply() fail.

I will use below for V4:

if (div > PWM_IMX_TPM_SC_PS_MASK) {
                 dev_err(chip->dev,
                         "failed to find valid prescale value!\n");
                 return -EINVAL;
Quoted 10 lines

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

Quoted 7 lines
drivers/pwm/pwm-imx-tpm.c new file +6 -0
+	}
+	/* set TPM counter prescale */
+	val = readl(tpm->base + TPM_SC);
+	val &= ~TPM_SC_PS_MASK;
+	val |= div;
+	writel(val, tpm->base + TPM_SC);

According to the documentation PS can only be written if the counter is disabled, I think this isn't ensured here.

In V4, I will disable the counter before programming PS, then restore the counter's CMOD after PS is written.

Quoted 10 lines

drivers/pwm/pwm-imx-tpm.c

Quoted 5 lines
drivers/pwm/pwm-imx-tpm.c new file +4 -0
+	/* set period counter */
+	do_div(tmp, NSEC_PER_SEC);
+	period_cnt = DIV_ROUND_CLOSEST_ULL(tmp, 1 << div);
+	writel(period_cnt & TPM_MOD_MOD_MASK, tpm->base +

TPM_MOD);

If there is already a value pending in the MOD register, another write to it is ignored. A comment, why this cannot happen, would be appropriate. (Note, I'm not sure this cannot happen.)

From the RM, when CMOD[1:0] = 2b'00, which means when counter is disabled, the MOD register will be updated once it is written, so I just put the comments there to avoid confusion, no need to check if there is value pending in MOD register, as in V4, I already make sure counter is disabled before programming MOD.

Quoted 17 lines
Quoted 17 lines
drivers/pwm/pwm-imx-tpm.c new file +16 -0
+}
+
+static void imx_tpm_pwm_config(struct pwm_chip *chip,
+			       struct pwm_device *pwm,
+			       struct pwm_state *state)
+{
+	struct imx_tpm_pwm_chip *tpm = to_imx_tpm_pwm_chip(chip);
+	static bool tpm_cnt_initialized;
+	unsigned int duty_cnt;
+	u32 val;
+	u64 tmp;
+
+	/*
+	 * TPM counter is shared by multi channels, let's make it to be
+	 * ONLY first channel can config TPM counter's precale and period
+	 * count.
Quoted 8 lines

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

Quoted 5 lines
drivers/pwm/pwm-imx-tpm.c new file +5 -0
+	 */
+	if (!tpm_cnt_initialized) {
+		imx_tpm_pwm_config_counter(chip, state->period);
+		tpm_cnt_initialized = true;
+	}

So the period can only be configured once. That is not as good as it could be. You should allow a change whenever there is exactly one PWM in use.

In V4, I added user_count to determine the PWM channels used, if there is ONLY 1 channel used, then period and prescale will can be configured anytime.

Quoted 32 lines

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

Quoted 5 lines
drivers/pwm/pwm-imx-tpm.c new file +4 -0
+	/* set duty counter */
+	tmp = readl(tpm->base + TPM_MOD) & TPM_MOD_MOD_MASK;
+	tmp *= state->duty_cycle;
+	duty_cnt = DIV_ROUND_CLOSEST_ULL(tmp, state->period);

Uah, you use state->period here even though for the 2nd PWM the Divider might not be setup appropriately.

Quoted 2 lines

[...]
+static int imx_tpm_pwm_apply(struct pwm_chip *chip, struct pwm_device

*pwm,

Quoted 7 lines
drivers/pwm/pwm-imx-tpm.c new file +7 -0
+			  struct pwm_state *state)
+{
+	struct imx_tpm_pwm_chip *tpm = to_imx_tpm_pwm_chip(chip);
+	struct pwm_state curstate;
+	unsigned long flags;
+
+	imx_tpm_pwm_get_state(chip, pwm, &curstate);

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

Quoted 10 lines
drivers/pwm/pwm-imx-tpm.c new file +10 -0
+
+	spin_lock_irqsave(&tpm->lock, flags);
+
+	if (state->period != curstate.period ||
+	    state->duty_cycle != curstate.duty_cycle ||
+	    state->polarity != curstate.polarity)
+		imx_tpm_pwm_config(chip, pwm, state);
+
+	if (state->enabled != curstate.enabled)
+		imx_tpm_pwm_enable(chip, pwm, state->enabled);

This is wrong. This sequence:

Quoted 7 lines
	pwm_apply_state(pwm, { .duty_cycle = 0, .period = 10000, .enabled =
true });
	pwm_apply_state(pwm, { .duty_cycle = 10000, .period =

10000, .enabled = false });

must keep the output pin low which isn't guaranteed here.

Already add checking to make sure the upper case NOT happen in V4.

Quoted 10 lines

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

Quoted 5 lines
drivers/pwm/pwm-imx-tpm.c new file +5 -0
+
+	spin_unlock_irqrestore(&tpm->lock, flags);
+
+	return 0;
+}

I didn't look in depth through the complete driver yet, but there is IIRC still some feedback on v1 that wasn't addressed in v2 (because v2 was sent before the last feedback). So I will look in more depth in v3 (assuming it comes late enough address the concerns from this mail).

Since there are different comments in some mails, I double checked it with the latest V4 patch and try to make sure I don't miss any comment, please help review V4 patch, if I missed any comment, then I am sorry for wasting your time. I will send out V4 patch after I done the function test on board tomorrow.

Thanks,
Anson.

Quoted 4 lines

Best regards
Uwe

Quoted 8 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%7C8a 834d252f5b4b7c446b08d6a85dde9e%7C686ea1d3bc2b4c6fa92cd99c5c30163 5%7C0%7C0%7C636881518529747245&amp;sdata=ozlYfyVaAgMP6JL%2FxS% 2FThygVfpqvuyvL7AVpJkHZRtw%3D&amp;reserved=0 |

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

#9 lore raw

PATCH v2 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 5586a50..57862c6 100644
@@ -399,6 +399,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
#10 lore raw

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

In reply to Anson Huang

Add i.MX7ULP EVK board PWM0 support.

---
Changes since V1:
- update the pad config to pull down to make sure when TPM counter is disabled, PWM output will be Low.

---
 arch/arm/boot/dts/imx7ulp-evk.dts | 12 ++++++++++++
 arch/arm/boot/dts/imx7ulp.dtsi    | 10 ++++++++++
 2 files changed, 22 insertions(+)
arch/arm/boot/dts/imx7ulp-evk.dts +12 -0
index a09026a..3f5ea18 100644
@@ -40,6 +40,12 @@
 	status = "okay";
 };
 
+&pwm0 {
+	pinctrl-names = "default";
+	pinctrl-0 = <&pinctrl_pwm0>;
+	status = "okay";
+};
+
 &usdhc0 {
 	pinctrl-names = "default";
 	pinctrl-0 = <&pinctrl_usdhc0>;
@@ -57,6 +63,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
arch/arm/boot/dts/imx7ulp.dtsi +10 -0
index fca6e50..6361c4e 100644
@@ -124,6 +124,16 @@
 			status = "disabled";
 		};
 
+		pwm0: pwm@40250000 {
+			compatible = "fsl,imx-tpm-pwm";
+			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 = <2>;
+			status = "disabled";
+		};
+
 		tpm5: tpm@40260000 {
 			compatible = "fsl,imx7ulp-tpm";
 			reg = <0x40260000 0x1000>;
Signature · 1 line
2.7.4
#11 lore raw

PATCH v2 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 | 8 ++++++++
 1 file changed, 8 insertions(+)
arch/arm/boot/dts/imx7ulp-evk.dts +8 -0
index 3f5ea18..f90f2f3 100644
@@ -22,6 +22,14 @@
 		reg = <0x60000000 0x40000000>;
 	};
 
+	backlight {
+		compatible = "pwm-backlight";
+		pwms = <&pwm0 1 50000>;
+		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";
Signature · 1 line
2.7.4