Skip to contentSkip to conversation

pwm: Add Amlogic Meson SoC PWM Controller

PATCH In review Neil Armstrong 5 participants 17 messages Aug 22, 2016 – Sep 7, 2016

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

v3 cover pwm: Add Amlogic Meson SoC PWM Controller

Add support for the PWM controller found in Amlogic Meson SoCs. This controller provides a dual PWM output with 4 selectable clock source and a two level divider to achieve a better PWM range.

Currently Meson8b and GXBB SoCs are supported.

Changes since v2 at http://lkml.kernel.org/r/1471718083-11516-1-git-send-email-narmstrong at baylibre.com :
- Switch to apply() callback only, this fixes the setup sequence
- Implement get_state
- Make clock parent names as DT match data

Changes since v1 at http://lkml.kernel.org/r/1466173784-15625-1-git-send-email-narmstrong at baylibre.com :
- fix meson8b dtsi

Neil Armstrong (4):
pwm: Add support for Meson PWM Controller
dt-bindings: pwm: Add bindings for Meson PWM Controller
ARM64: dts: meson-gxbb: Add Meson GXBB PWM Controller nodes
ARM: dts: meson8b: Add Meson8b PWM Controller nodes

 .../devicetree/bindings/pwm/pwm-meson.txt          |  21 +
 arch/arm/boot/dts/meson8b.dtsi                     |  21 +
 arch/arm64/boot/dts/amlogic/meson-gxbb.dtsi        |  28 ++
 drivers/pwm/Kconfig                                |   9 +
 drivers/pwm/Makefile                               |   1 +
 drivers/pwm/pwm-meson.c                            | 528 +++++++++++++++++++++
 6 files changed, 608 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/pwm/pwm-meson.txt
 create mode 100644 drivers/pwm/pwm-meson.c
Signature · 1 line
1.9.1
#1 lore raw

PATCH v3 1/4 pwm: Add support for Meson PWM Controller

Add support for the PWM controller found in the Amlogic SoCs. This driver supports the Meson8b and GXBB SoCs.

---
 drivers/pwm/Kconfig     |   9 +
 drivers/pwm/Makefile    |   1 +
 drivers/pwm/pwm-meson.c | 528 ++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 538 insertions(+)
 create mode 100644 drivers/pwm/pwm-meson.c
drivers/pwm/Kconfig +9 -0
index 80a566a..bf01288 100644
@@ -262,6 +262,15 @@ config PWM_LPSS_PLATFORM
 	  To compile this driver as a module, choose M here: the module
 	  will be called pwm-lpss-platform.
 
+config PWM_MESON
+	tristate "Amlogic Meson PWM driver"
+	depends on ARCH_MESON
+	help
+	  The platform driver for Amlogic Meson PWM controller.
+
+	  To compile this driver as a module, choose M here: the module
+	  will be called pwm-meson.
+
 config PWM_MTK_DISP
 	tristate "MediaTek display PWM driver"
 	depends on ARCH_MEDIATEK || COMPILE_TEST
drivers/pwm/Makefile +1 -0
index feef1dd..1194c54 100644
@@ -24,6 +24,7 @@ obj-$(CONFIG_PWM_LPC32XX)	+= pwm-lpc32xx.o
 obj-$(CONFIG_PWM_LPSS)		+= pwm-lpss.o
 obj-$(CONFIG_PWM_LPSS_PCI)	+= pwm-lpss-pci.o
 obj-$(CONFIG_PWM_LPSS_PLATFORM)	+= pwm-lpss-platform.o
+obj-$(CONFIG_PWM_MESON)		+= pwm-meson.o
 obj-$(CONFIG_PWM_MTK_DISP)	+= pwm-mtk-disp.o
 obj-$(CONFIG_PWM_MXS)		+= pwm-mxs.o
 obj-$(CONFIG_PWM_OMAP_DMTIMER)	+= pwm-omap-dmtimer.o
drivers/pwm/pwm-meson.c new file +528 -0

528 lines not on this page · raw message

Signature · 1 line
1.9.1
#12 lore raw

PATCH v3 2/4 dt-bindings: pwm: Add bindings for Meson PWM Controller

In reply to Neil Armstrong

Add bindings for the Amlogic PWM Controller in Meson8b and GXBB SoCs.

---
 Documentation/devicetree/bindings/pwm/pwm-meson.txt | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/pwm/pwm-meson.txt
Documentation/devicetree/bindings/pwm/pwm-meson.txt new file +21 -0
new file mode 100644
index 0000000..e06aed0
@@ -0,0 +1,21 @@
+Amlogic Meson PWM Controller
+=================================
+
+Required properties:
+- compatible: Shall contain "amlogic,meson8b-pwm" or "amlogic,meson-gxbb-pwm".
+- #pwm-cells: Should be 3. See pwm.txt in this directory for a description of
+  the cells format.
+
+Optional properties:
+- clocks: Could contain one or two parents clocks phandle for each of the two pwm channels.
+- clock-names: Could contain at least the "clkin0" and/or "clkin1" names.
+
+Example:
+pwm_ab: pwm at 8550 {
+	compatible = "amlogic,meson-gxbb-pwm";
+	reg = <0x0 0x08550 0x0 0x10>;
+	#pwm-cells = <3>;
+	status = "disabled";
+	clocks = <&xtal>, <&xtal>;
+	clock-names = "clkin0", "clkin1";
+}
Signature · 1 line
1.9.1
#13 lore raw

PATCH v3 3/4 ARM64: dts: meson-gxbb: Add Meson GXBB PWM Controller nodes

In reply to Neil Armstrong
---
 arch/arm64/boot/dts/amlogic/meson-gxbb.dtsi | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)
arch/arm64/boot/dts/amlogic/meson-gxbb.dtsi +28 -0
index e502c24..e3f4d36 100644
@@ -153,6 +153,27 @@
 				status = "disabled";
 			};
 
+			pwm_ab: pwm at 8550 {
+				compatible = "amlogic,meson-gxbb-pwm";
+				reg = <0x0 0x08550 0x0 0x10>;
+				#pwm-cells = <3>;
+				status = "disabled";
+			};
+
+			pwm_cd: pwm at 8650 {
+				compatible = "amlogic,meson-gxbb-pwm";
+				reg = <0x0 0x08650 0x0 0x10>;
+				#pwm-cells = <3>;
+				status = "disabled";
+			};
+
+			pwm_ef: pwm at 86c0 {
+				compatible = "amlogic,meson-gxbb-pwm";
+				reg = <0x0 0x086c0 0x0 0x10>;
+				#pwm-cells = <3>;
+				status = "disabled";
+			};
+
 			uart_C: serial at 8700 {
 				compatible = "amlogic,meson-uart";
 				reg = <0x0 0x8700 0x0 0x14>;
@@ -212,6 +233,13 @@
 				clocks = <&xtal>;
 				status = "disabled";
 			};
+
+			pwm_ab_AO: pwm at 550 {
+				compatible = "amlogic,meson-gxbb-pwm";
+				reg = <0x0 0x0550 0x0 0x10>;
+				#pwm-cells = <3>;
+				status = "disabled";
+			};
 		};
 
 		periphs: periphs at c8834000 {
Signature · 1 line
1.9.1
#15 lore raw

PATCH v3 4/4 ARM: dts: meson8b: Add Meson8b PWM Controller nodes

In reply to Neil Armstrong
---
 arch/arm/boot/dts/meson8b.dtsi | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
arch/arm/boot/dts/meson8b.dtsi +21 -0
index fc4080d..41fd536 100644
@@ -162,6 +162,27 @@
 			reg = <0xc1108000 0x4>, <0xc1104000 0x460>;
 		};
 
+		pwm_ab: pwm at 8550 {
+			compatible = "amlogic,meson8b-pwm";
+			reg = <0xc1108550 0x10>;
+			#pwm-cells = <3>;
+			status = "disabled";
+		};
+
+		pwm_cd: pwm at 8650 {
+			compatible = "amlogic,meson8b-pwm";
+			reg = <0xc1108650 0x10>;
+			#pwm-cells = <3>;
+			status = "disabled";
+		};
+
+		pwm_ef: pwm at 86c0 {
+			compatible = "amlogic,meson8b-pwm";
+			reg = <0xc11086c0 0x10>;
+			#pwm-cells = <3>;
+			status = "disabled";
+		};
+
 		pinctrl_cbus: pinctrl at c1109880 {
 			compatible = "amlogic,meson8b-cbus-pinctrl";
 			reg = <0xc1109880 0x10>;
Signature · 1 line
1.9.1
reviewer Tested-by on v3 3/4
#14 lore raw

Re: [PATCH v3 3/4] ARM64: dts: meson-gxbb: Add Meson GXBB PWM Controller nodes

In reply to Neil Armstrong
Quoted 23 lines · Neil Armstrong
---
 arch/arm64/boot/dts/amlogic/meson-gxbb.dtsi | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)
arch/arm64/boot/dts/amlogic/meson-gxbb.dtsi +10 -0
index e502c24..e3f4d36 100644
@@ -153,6 +153,27 @@
                                status = "disabled";
                        };
 
+                       pwm_ab: pwm at 8550 {
+                               compatible = "amlogic,meson-gxbb-pwm";
+                               reg = <0x0 0x08550 0x0 0x10>;
+                               #pwm-cells = <3>;
+                               status = "disabled";
+                       };
+
+                       pwm_cd: pwm at 8650 {
+                               compatible = "amlogic,meson-gxbb-pwm";
+                               reg = <0x0 0x08650 0x0 0x10>;

arch/arm64/boot/dts/amlogic/meson-gxbb.dtsi line 175

Quoted 10 lines
arch/arm64/boot/dts/amlogic/meson-gxbb.dtsi +10 -0
+                               #pwm-cells = <3>;
+                               status = "disabled";
+                       };
+
+                       pwm_ef: pwm at 86c0 {
+                               compatible = "amlogic,meson-gxbb-pwm";
+                               reg = <0x0 0x086c0 0x0 0x10>;
+                               #pwm-cells = <3>;
+                               status = "disabled";
+                       };

I have successfully tested pwm_e on my meson-gxbb-vega-s95-meta clone (see [0] for the corresponding patches), thus:

[0] https://github.com/xdarklight/linux/tree/meson-gxbb-integration-4.8-20160828

reviewer Tested-by on v3 1/4
#2 lore raw

Re: [PATCH v3 1/4] pwm: Add support for Meson PWM Controller

In reply to Neil Armstrong
Quoted 4 lines · Neil Armstrong

Add support for the PWM controller found in the Amlogic SoCs. This driver supports the Meson8b and GXBB SoCs.

I could enable the AP6330 SDIO wifi module with this on my meson-gxbb-vega-s95-meta clone, which uses pwm_e to generate the LPO clock for the wifi module (see [0] for the corresponding patches). So thank you Neil, and:

[0] https://github.com/xdarklight/linux/tree/meson-gxbb-integration-4.8-20160828

#5 lore raw

Re: [PATCH v3 1/4] pwm: Add support for Meson PWM Controller

In reply to Neil Armstrong
Quoted 10 lines · Neil Armstrong

Add support for the PWM controller found in the Amlogic SoCs. This driver supports the Meson8b and GXBB SoCs.

---
 drivers/pwm/Kconfig     |   9 +
 drivers/pwm/Makefile    |   1 +
 drivers/pwm/pwm-meson.c | 528 ++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 538 insertions(+)
 create mode 100644 drivers/pwm/pwm-meson.c

Hi Neil,

sorry for taking so long to review this. I had actually started to write a review email since I had noticed a couple of slight oddities about the driver structure (primarily this was about how channel-specific data was split between struct meson_pwm_channel and struct meson_pwm_chip), but I ended up making some changes to the driver in order to see what my suggestions would look like, and if they would indeed improve things. But once I had done that, I thought it a bit pointless to make that into review comments and decided to just push what I had done and ask you to take a look, and if you had no objections to the changes take the driver for a spin to see if it still worked as expected.

One other thing I noticed is that your ->get_state() implementation only reads the enable state, but from the looks of it it should be possible to read period and duty cycle information from hardware as well. I'm not going to reject the driver for that reason, just saying that it'd be good to have that implemented sometime in the future.

I've pushed my modifications to the driver to the linux-pwm repository:

https://git.kernel.org/cgit/linux/kernel/git/thierry.reding/linux-pwm.git/log/?h=for-next

Alternatively you can also take a look at the for-4.9/drivers branch, but they're currently the same thing.

Thierry
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 801 bytes
Desc: not available
URL: <http://lists.infradead.org/pipermail/linux-amlogic/attachments/20160905/79564440/attachment.sig>

#6 lore raw

Re: [PATCH v3 1/4] pwm: Add support for Meson PWM Controller

Quoted 11 lines · Thierry Reding
Quoted 10 lines · Neil Armstrong

Add support for the PWM controller found in the Amlogic SoCs. This driver supports the Meson8b and GXBB SoCs.

---
 drivers/pwm/Kconfig     |   9 +
 drivers/pwm/Makefile    |   1 +
 drivers/pwm/pwm-meson.c | 528 ++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 538 insertions(+)
 create mode 100644 drivers/pwm/pwm-meson.c

Hi Thierry,

Quoted 12 lines

Hi Neil,

sorry for taking so long to review this. I had actually started to write a review email since I had noticed a couple of slight oddities about the driver structure (primarily this was about how channel-specific data was split between struct meson_pwm_channel and struct meson_pwm_chip), but I ended up making some changes to the driver in order to see what my suggestions would look like, and if they would indeed improve things. But once I had done that, I thought it a bit pointless to make that into review comments and decided to just push what I had done and ask you to take a look, and if you had no objections to the changes take the driver for a spin to see if it still worked as expected.

Well, thanks ! I was wondering why it took so long, but the result look far best than what I achieved.

The road was very long since the original Amlogic driver...

I will try it out ASAP, but it looks very good for me. Your changes seems quite obvious, and such rework was necessary.

Quoted 6 lines

One other thing I noticed is that your ->get_state() implementation only reads the enable state, but from the looks of it it should be possible to read period and duty cycle information from hardware as well. I'm not going to reject the driver for that reason, just saying that it'd be good to have that implemented sometime in the future.

Yes, it was delayed for later since it's not a functional feature, I will certainly push an update with this later on.

Quoted 7 lines

I've pushed my modifications to the driver to the linux-pwm repository:

https://git.kernel.org/cgit/linux/kernel/git/thierry.reding/linux-pwm.git/log/?h=for-next

Alternatively you can also take a look at the for-4.9/drivers branch, but they're currently the same thing.

Great, I will give you a functional update ASAP.

Quoted 1 line

Thierry

Thanks for the review, aww, s/review/rework/ !
Neil

Quoted 1 line
#3 lore raw

Re: [PATCH v3 1/4] pwm: Add support for Meson PWM Controller

In reply to Martin Blumenstingl
Quoted 10 lines · Martin Blumenstingl
Quoted 4 lines · Neil Armstrong

Add support for the PWM controller found in the Amlogic SoCs. This driver supports the Meson8b and GXBB SoCs.

I could enable the AP6330 SDIO wifi module with this on my meson-gxbb-vega-s95-meta clone, which uses pwm_e to generate the LPO clock for the wifi module (see [0] for the corresponding patches). So thank you Neil, and:

Hi Martin,

As mentioned in another reply in this thread I ended up restructuring the driver a little before applying it to the PWM tree. I don't /think/ there are any functional changes, so I left your Tested-by in place, but it would be great if you could retest and confirm, just to make sure.

The driver is in the for-4.9/drivers branch of the PWM tree:

https://git.kernel.org/cgit/linux/kernel/git/thierry.reding/linux-pwm.git/log/?h=for-4.9/drivers

Thanks,
Thierry
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 801 bytes
Desc: not available
URL: <http://lists.infradead.org/pipermail/linux-amlogic/attachments/20160905/6455a12d/attachment.sig>

#7 lore raw

Re: [PATCH v3 1/4] pwm: Add support for Meson PWM Controller

In reply to Thierry Reding

Hi Thierry,

Quoted 14 lines · Thierry Reding
Quoted 10 lines · Neil Armstrong

Add support for the PWM controller found in the Amlogic SoCs. This driver supports the Meson8b and GXBB SoCs.

---
 drivers/pwm/Kconfig     |   9 +
 drivers/pwm/Makefile    |   1 +
 drivers/pwm/pwm-meson.c | 528 ++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 538 insertions(+)
 create mode 100644 drivers/pwm/pwm-meson.c

Hi Neil,

Quoted 10 lines

sorry for taking so long to review this. I had actually started to write a review email since I had noticed a couple of slight oddities about the driver structure (primarily this was about how channel-specific data was split between struct meson_pwm_channel and struct meson_pwm_chip), but I ended up making some changes to the driver in order to see what my suggestions would look like, and if they would indeed improve things. But once I had done that, I thought it a bit pointless to make that into review comments and decided to just push what I had done and ask you to take a look, and if you had no objections to the changes take the driver for a spin to see if it still worked as expected.

We re-run our tests and I found 2 bugs, the first one is in meson_pwm_enable(), only the channel A was setup, the fix is :

static void meson_pwm_enable(...)
- u32 value, clk_shift, clk_enable, enable;
+ u32 reg, value, clk_shift, clk_enable, enable;

switch (id) {
case 0:
[...]
+ reg = REG_PWM_A;
break;
case 1:
[...]
+ reg = REG_PWM_B;
break;
}
[...]
- writel(value, meson->base + REG_PWM_A);
+ writel(value, meson->base + reg);

The second bug is in probe(), I understand the point to allocate dynamically the channels and attach them to each pwm chip, but when calling meson_pwm_init_channels() we get an OOPS because meson->chip.pwms[i] are allocated in pwmchip_add().
Moving meson_pwm_init_channels() would fix this, but in case of a clk PROBE_DEFER, we would need to remove back the pwmchip, which is a quite a bad design decision....

The smartest fix I found was to allocate channels in probe, init them them attach them after pwmchip_add():

static int meson_pwm_init_channels(..., struct meson_pwm_channel *channels) {
+ struct meson_pwm_channel *channels;
[...]
- for (i = 0; i < meson->chip.npwm; i++) {
- struct pwm_device *pwm = &meson->chip.pwms[i];
- struct meson_pwm_channel *channel;
-
- channel = devm_kzalloc(dev, sizeof(*channel), GFP_KERNEL);
- if (!channel)
- return -ENOMEM;
+ if (!channels)
+ return -EINVAL;

+ for (i = 0; i < meson->chip.npwm; i++) {
[...]
+ memset(&channels[i], 0, sizeof(struct meson_pwm_channel));
[...]
//Rename "channel->" into "channels[i]."//
[...]
- pwm_set_chip_data(pwm, channel);
}

return 0;
}

+static void meson_pwm_add_channels_data(struct meson_pwm *meson,
+ struct meson_pwm_channel *channels)
+{
+ unsigned int i;
+
+ for (i = 0; i < meson->chip.npwm; i++)
+ pwm_set_chip_data(&meson->chip.pwms[i], &channels[i]);
+}

static int meson_pwm_probe(struct platform_device *pdev) {
+ struct meson_pwm_channel *channels;
[...]
- err = meson_pwm_init_channels(meson);
- if (err < 0)
- return err;
-
meson->chip.dev = &pdev->dev;
[...]
meson->chip.of_pwm_n_cells = 3;

+ channels = devm_kmalloc_array(&pdev->dev, 2, sizeof(*meson),
+ GFP_KERNEL);
+ if (!channels)
+ return -ENOMEM;
+
+ err = meson_pwm_init_channels(meson, channels);
+ if (err < 0)
+ return err;
+
err = pwmchip_add(&meson->chip);
[...]
+ meson_pwm_add_channels_data(meson, channels);
+
platform_set_drvdata(pdev, meson);

return 0;
}

The fix driver is in a separate branch, rebased on your for-next :
https://github.com/superna9999/linux/tree/amlogic/v4.8/pwm-for-next

and in a signed tag I can transform in a pull request if needed :
https://github.com/superna9999/linux/releases/tag/amlogic/v4.8/pwm-for-next-for-v4

[...]

Quoted 10 lines

I've pushed my modifications to the driver to the linux-pwm repository:

https://git.kernel.org/cgit/linux/kernel/git/thierry.reding/linux-pwm.git/log/?h=for-next

Alternatively you can also take a look at the for-4.9/drivers branch, but they're currently the same thing.

Thierry

Thanks,
Neil

#8 lore raw

Re: [PATCH v3 1/4] pwm: Add support for Meson PWM Controller

Quoted 38 lines · Neil Armstrong

Hi Thierry,

Quoted 14 lines · Thierry Reding
Quoted 10 lines · Neil Armstrong

Add support for the PWM controller found in the Amlogic SoCs. This driver supports the Meson8b and GXBB SoCs.

---
 drivers/pwm/Kconfig     |   9 +
 drivers/pwm/Makefile    |   1 +
 drivers/pwm/pwm-meson.c | 528 ++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 538 insertions(+)
 create mode 100644 drivers/pwm/pwm-meson.c

Hi Neil,

Quoted 10 lines

sorry for taking so long to review this. I had actually started to write a review email since I had noticed a couple of slight oddities about the driver structure (primarily this was about how channel-specific data was split between struct meson_pwm_channel and struct meson_pwm_chip), but I ended up making some changes to the driver in order to see what my suggestions would look like, and if they would indeed improve things. But once I had done that, I thought it a bit pointless to make that into review comments and decided to just push what I had done and ask you to take a look, and if you had no objections to the changes take the driver for a spin to see if it still worked as expected.

We re-run our tests and I found 2 bugs, the first one is in meson_pwm_enable(), only the channel A was setup, the fix is :

static void meson_pwm_enable(...)

(quoted diff) +1 -1
-	u32 value, clk_shift, clk_enable, enable;
+	u32 reg, value, clk_shift, clk_enable, enable;
 
 	switch (id) {
 	case 0:
[...]

Review

Quoted 10 lines
(quoted diff) +3 -1
+		reg = REG_PWM_A;
 		break;
 	case 1:
[...]
+		reg = REG_PWM_B;
 		break;
 	}
[...]
-	writel(value, meson->base + REG_PWM_A);
+	writel(value, meson->base + reg);

Ah indeed. Good catch.

Quoted 8 lines

The second bug is in probe(), I understand the point to allocate dynamically the channels and attach them to each pwm chip, but when calling meson_pwm_init_channels() we get an OOPS because meson->chip.pwms[i] are allocated in pwmchip_add(). Moving meson_pwm_init_channels() would fix this, but in case of a clk PROBE_DEFER, we would need to remove back the pwmchip, which is a quite a bad design decision....

Ah yes... that one again. I remember running into that a while ago with some other driver. To be honest, I think that's a short-coming of the PWM subsystem and the fix would be for PWM chip registration to be split into two parts: pwm_chip_init() and pwm_chip_add(). That way, a chip would be initialized using pwm_chip_init() where the pwms array would be allocated, and pwm_chip_add() would register the chip with the system.

Currently a few drivers might be vulnerable to a race condition between registration and implementation (i.e. PWM channels aren't fully set up when they are exposed to users and sysfs).

Quoted 66 lines

The smartest fix I found was to allocate channels in probe, init them them attach them after pwmchip_add():

static int meson_pwm_init_channels(..., struct meson_pwm_channel *channels) {

(quoted diff) +5 -7
+	struct meson_pwm_channel *channels;
[...]
-	for (i = 0; i < meson->chip.npwm; i++) {
-		struct pwm_device *pwm = &meson->chip.pwms[i];
-		struct meson_pwm_channel *channel;
-
-		channel = devm_kzalloc(dev, sizeof(*channel), GFP_KERNEL);
-		if (!channel)
-			return -ENOMEM;
+	if (!channels)
+		return -EINVAL;
 
+	for (i = 0; i < meson->chip.npwm; i++) {
[...]
+		memset(&channels[i], 0, sizeof(struct meson_pwm_channel));
[...]

//Rename "channel->" into "channels[i]."//
[...]

(quoted diff) +0 -1
-		pwm_set_chip_data(pwm, channel);
 	}
 
 	return 0;

}

(quoted diff) +8 -0
+static void meson_pwm_add_channels_data(struct meson_pwm *meson,
+					struct meson_pwm_channel *channels)
+{
+	unsigned int i;
+
+	for (i = 0; i < meson->chip.npwm; i++)
+		pwm_set_chip_data(&meson->chip.pwms[i], &channels[i]);
+}

static int meson_pwm_probe(struct platform_device *pdev) {

(quoted diff) +12 -4
+	struct meson_pwm_channel *channels;
[...]
-	err = meson_pwm_init_channels(meson);
-	if (err < 0)
-		return err;
-
 	meson->chip.dev = &pdev->dev;
[...]
 	meson->chip.of_pwm_n_cells = 3;
 
+	channels = devm_kmalloc_array(&pdev->dev, 2, sizeof(*meson),
+				      GFP_KERNEL);
+	if (!channels)
+		return -ENOMEM;
+
+	err = meson_pwm_init_channels(meson, channels);
+	if (err < 0)
+		return err;
+
 	err = pwmchip_add(&meson->chip);
[...]
+	meson_pwm_add_channels_data(meson, channels);
+
 	platform_set_drvdata(pdev, meson);
 
 	return 0;
Quoted 1 line

}

That's the race I was talking about above. I suppose it's not too big an issue since other drivers seem to manage, so I'm going to merge your fixed driver.

Unless you feel like taking a stab at the pwm_chip_init()/pwm_chip_add() split, in which case your driver would be the first to be race-free. =)

Thierry
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 801 bytes
Desc: not available
URL: <http://lists.infradead.org/pipermail/linux-amlogic/attachments/20160906/d1978589/attachment.sig>

#9 lore raw

Re: [PATCH v3 1/4] pwm: Add support for Meson PWM Controller

Quoted 3 lines · Thierry Reding
Quoted 2 lines · Neil Armstrong

Hi Thierry,

[...]

Quoted 24 lines
Quoted 9 lines

The second bug is in probe(), I understand the point to allocate dynamically the channels and attach them to each pwm chip, but when calling meson_pwm_init_channels() we get an OOPS because meson->chip.pwms[i] are allocated in pwmchip_add(). Moving meson_pwm_init_channels() would fix this, but in case of a clk PROBE_DEFER, we would need to remove back the pwmchip, which is a quite a bad design decision....

Ah yes... that one again. I remember running into that a while ago with some other driver. To be honest, I think that's a short-coming of the PWM subsystem and the fix would be for PWM chip registration to be split into two parts: pwm_chip_init() and pwm_chip_add(). That way, a chip would be initialized using pwm_chip_init() where the pwms array would be allocated, and pwm_chip_add() would register the chip with the system.

Currently a few drivers might be vulnerable to a race condition between registration and implementation (i.e. PWM channels aren't fully set up when they are exposed to users and sysfs).

Quoted 3 lines

The smartest fix I found was to allocate channels in probe, init them them attach them after pwmchip_add():

[...]

Quoted 4 lines

That's the race I was talking about above. I suppose it's not too big an issue since other drivers seem to manage, so I'm going to merge your fixed driver.

ok thanks !

Quoted 3 lines

Unless you feel like taking a stab at the pwm_chip_init()/pwm_chip_add() split, in which case your driver would be the first to be race-free. =)

Having he driver upstream is a priority, but having it completely race-free would be great! I'll be happy to collaborate to a race-free pwmchip probe somehow !

But there is still a glitch, when pwmadd_chip() returns, pwm_get_chip_data(pwm) will still return crap until meson_pwm_add_channels_data() is called in the following instructions...

The pwm_chip_init()/pwm_chip_add() would be the only solution !

Quoted 2 lines

Thierry

Thanks,
Neil

#10 lore raw

Re: [PATCH v3 1/4] pwm: Add support for Meson PWM Controller

Quoted 29 lines · Neil Armstrong
Quoted 3 lines · Thierry Reding
Quoted 2 lines · Neil Armstrong

Hi Thierry,

[...]

Quoted 17 lines
Quoted 9 lines

The second bug is in probe(), I understand the point to allocate dynamically the channels and attach them to each pwm chip, but when calling meson_pwm_init_channels() we get an OOPS because meson->chip.pwms[i] are allocated in pwmchip_add(). Moving meson_pwm_init_channels() would fix this, but in case of a clk PROBE_DEFER, we would need to remove back the pwmchip, which is a quite a bad design decision....

Ah yes... that one again. I remember running into that a while ago with some other driver. To be honest, I think that's a short-coming of the PWM subsystem and the fix would be for PWM chip registration to be split into two parts: pwm_chip_init() and pwm_chip_add(). That way, a chip would be initialized using pwm_chip_init() where the pwms array would be allocated, and pwm_chip_add() would register the chip with the system.

Quoted 7 lines

Currently a few drivers might be vulnerable to a race condition between registration and implementation (i.e. PWM channels aren't fully set up when they are exposed to users and sysfs).

Quoted 3 lines

The smartest fix I found was to allocate channels in probe, init them them attach them after pwmchip_add():

Quoted 8 lines

[...]

Quoted 4 lines

That's the race I was talking about above. I suppose it's not too big an issue since other drivers seem to manage, so I'm going to merge your fixed driver.

ok thanks !

I've made a few tiny changes (reg -> offset, temporary variable to track &channels[i], ...) and pushed it all out. Hopefully that now fixes any of the remaining issues.

Quoted 6 lines
Quoted 2 lines

Unless you feel like taking a stab at the pwm_chip_init()/pwm_chip_add() split, in which case your driver would be the first to be race-free. =)

Having he driver upstream is a priority, but having it completely race-free would be great! I'll be happy to collaborate to a race-free pwmchip probe somehow !

Fair enough. I'll do some prototyping and keep you in the loop if I come up with something that I think will do.

Thierry
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 801 bytes
Desc: not available
URL: <http://lists.infradead.org/pipermail/linux-amlogic/attachments/20160906/089d2a89/attachment.sig>

reviewer Tested-by
#11 lore raw

Re: [PATCH v3 1/4] pwm: Add support for Meson PWM Controller

Quoted 78 lines · Thierry Reding
Quoted 51 lines · Neil Armstrong
Quoted 5 lines · Thierry Reding
Quoted 3 lines · Neil Armstrong

Hi Thierry,

[...]

Quoted 39 lines
Quoted 4 lines
Quoted 10 lines

The second bug is in probe(), I understand the point to allocate
dynamically the channels and attach them to each pwm chip, but when
calling meson_pwm_init_channels() we get an OOPS because meson->chip.pwms[i] are allocated in pwmchip_add(). Moving meson_pwm_init_channels() would fix this, but in case of a clk PROBE_DEFER, we would need to remove back the pwmchip, which is a
quite a bad design decision....

Ah yes... that one again. I remember running into that a while ago with
some other driver. To be honest, I think that's a short-coming of the
PWM subsystem and the fix would be for PWM chip registration to be split
into two parts: pwm_chip_init() and pwm_chip_add(). That way, a chip
would be initialized using pwm_chip_init() where the pwms array would be
allocated, and pwm_chip_add() would register the chip with the system.

Currently a few drivers might be vulnerable to a race condition between
registration and implementation (i.e. PWM channels aren't fully set up
when they are exposed to users and sysfs).

Quoted 5 lines

The smartest fix I found was to allocate channels in probe, init them
them attach them after pwmchip_add():

[...]

Quoted 2 lines
Quoted 7 lines
Quoted 5 lines

That's the race I was talking about above. I suppose it's not too big an
issue since other drivers seem to manage, so I'm going to merge your
fixed driver.

ok thanks !

I've made a few tiny changes (reg -> offset, temporary variable to track
&channels[i], ...) and pushed it all out. Hopefully that now fixes any
of the remaining issues.

Quoted 11 lines
Quoted 6 lines

Unless you feel like taking a stab at the pwm_chip_init()/pwm_chip_add()
split, in which case your driver would be the first to be race- free. =)

Having he driver upstream is a priority, but having it completely race-free would be great! I'll be happy to collaborate to a race- free
pwmchip probe somehow !

Quoted 5 lines

Fair enough. I'll do some prototyping and keep you in the loop if I come
up with something that I think will do.

Thierry

Hi Thierry,

I have tested the latest version on the P200 (S905), channels E and F. It works as expected.

Regards

reviewer Tested-by
#4 lore raw

Re: [PATCH v3 1/4] pwm: Add support for Meson PWM Controller

In reply to Thierry Reding

Hi Thierry,

Quoted 8 lines · Thierry Reding

As mentioned in another reply in this thread I ended up restructuring the driver a little before applying it to the PWM tree. I don't /think/ there are any functional changes, so I left your Tested-by in place, but it would be great if you could retest and confirm, just to make sure.

The driver is in the for-4.9/drivers branch of the PWM tree:

https://git.kernel.org/cgit/linux/kernel/git/thierry.reding/linux-pwm.git/log/?h=for-4.9/drivers

I have tested it with the updated Neil sent today: [0] This is still working fine for me (driving the LPO clock of an AP6330 SDIO wifi chip).

Please note that I don't have a scope to measure the actual signal, but I guess it's fine since my AP6330 is happy.

So feel free to keep my:

Regards,
Martin

[0] https://github.com/superna9999/linux/releases/tag/amlogic/v4.8/pwm-for-next-for-v4

maintainer
#16 lore raw
In reply to Neil Armstrong
Quoted 11 lines · Neil Armstrong

Add support for the PWM controller found in Amlogic Meson SoCs. This controller provides a dual PWM output with 4 selectable clock source and a two level divider to achieve a better PWM range.

Currently Meson8b and GXBB SoCs are supported.

Changes since v2 at http://lkml.kernel.org/r/1471718083-11516-1-git-send-email-narmstrong at baylibre.com :
- Switch to apply() callback only, this fixes the setup sequence
- Implement get_state
- Make clock parent names as DT match data

Quoted 6 lines

Changes since v1 at http://lkml.kernel.org/r/1466173784-15625-1-git-send-email-narmstrong at baylibre.com :
- fix meson8b dtsi

Neil Armstrong (4):
pwm: Add support for Meson PWM Controller
dt-bindings: pwm: Add bindings for Meson PWM Controller

Looks like Thierry has picked up these two into his for-4.9/drivers branch...

Quoted 2 lines

ARM64: dts: meson-gxbb: Add Meson GXBB PWM Controller nodes
ARM: dts: meson8b: Add Meson8b PWM Controller nodes

... so I've picked up these into the amlogic tree.

Kevin