Skip to contentSkip to conversation

pwm: add BCM2835 PWM driver

RFC Superseded Bart Tanghe 3 participants 3 messages Oct 2, 2014 – Oct 7, 2014

Subsystems (4)

v5 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

RFC v5 1/1 pwm: add BCM2835 PWM driver

Add pwm driver for Broadcom BCM2835 processor (Raspberry Pi)

---
Changes in v5:
- add the correct copyright comments according the university college philosophy
- better error handling
- use devm clk lookup
- define some variables
- add the clock description

Documentation/devicetree/bindings/pwm/pwm-bcm2835.txt new file +24 -0
new file mode 100644
index 0000000..eea1e8c
@@ -0,0 +1,24 @@
+BCM2835 PWM controller (Raspberry Pi controller)
+
+Required properties:
+- compatible: should be "brcm,bcm2835-pwm"
+- reg: physical base address and length of the controller's registers
+
+Examples:
+
+pwm@2020c000 {
+	compatible = "brcm,bcm2835-pwm";
+	reg = <0x2020c000 0x28>;
+	clocks = <&clk_pwm>;
+};
+
+clocks {
+	....
+		clk_pwm: pwm {
+			compatible = "fixed-clock";
+			reg = <3>;
+			#clock-cells = <0>;
+			clock-frequency = <9200000>;
+		};
+	....
+};
drivers/pwm/Kconfig +9 -0
index 22f2f28..a277256 100644
@@ -62,6 +62,15 @@ config PWM_ATMEL_TCB
 	  To compile this driver as a module, choose M here: the module
 	  will be called pwm-atmel-tcb.
 
+config PWM_BCM2835
+	tristate "BCM2835 PWM support"
+	depends on MACH_BCM2835 || MACH_BCM2708
+	help
+	  PWM framework driver for BCM2835 controller (Raspberry Pi)
+
+	  To compile this driver as a module, choose M here: the module
+	  will be called pwm-bcm2835.
+
 config PWM_BFIN
 	tristate "Blackfin PWM support"
 	depends on BFIN_GPTIMERS
drivers/pwm/Makefile +1 -0
index d8906ec..9863590 100644
@@ -3,6 +3,7 @@ obj-$(CONFIG_PWM_SYSFS)		+= sysfs.o
 obj-$(CONFIG_PWM_AB8500)	+= pwm-ab8500.o
 obj-$(CONFIG_PWM_ATMEL)		+= pwm-atmel.o
 obj-$(CONFIG_PWM_ATMEL_TCB)	+= pwm-atmel-tcb.o
+obj-$(CONFIG_PWM_BCM2835)	+= pwm-bcm2835.o
 obj-$(CONFIG_PWM_BFIN)		+= pwm-bfin.o
 obj-$(CONFIG_PWM_EP93XX)	+= pwm-ep93xx.o
 obj-$(CONFIG_PWM_IMX)		+= pwm-imx.o
drivers/pwm/pwm-bcm2835.c new file +204 -0

204 lines not on this page · raw message

#1 lore raw
Quoted 5 lines · Bart Tanghe

Add pwm driver for Broadcom BCM2835 processor (Raspberry Pi)

---
Changes in v5:

By v5, I would drop "rfc" from the email subject.

Documentation/devicetree/bindings/pwm/pwm-bcm2835.txt line 5

Quoted 5 lines
Documentation/devicetree/bindings/pwm/pwm-bcm2835.txt +3 -0
 
+Required properties:
+- compatible: should be "brcm,bcm2835-pwm"
+- reg: physical base address and length of the controller's registers

You need to document the clocks property here too.

Quoted 8 lines
Documentation/devicetree/bindings/pwm/pwm-bcm2835.txt +7 -0
+Examples:
+
+pwm@2020c000 {
+	compatible = "brcm,bcm2835-pwm";
+	reg = <0x2020c000 0x28>;
+	clocks = <&clk_pwm>;
+};
 

Documentation/devicetree/bindings/pwm/pwm-bcm2835.txt line 24

Quoted 10 lines
Documentation/devicetree/bindings/pwm/pwm-bcm2835.txt +10 -0
+clocks {
+	....
+		clk_pwm: pwm {
+			compatible = "fixed-clock";
+			reg = <3>;
+			#clock-cells = <0>;
+			clock-frequency = <9200000>;
+		};
+	....
+};

You typically wouldn't bother including an example for the nodes references by phandles, but it's not a big deal.

drivers/pwm/Kconfig config PWM_ATMEL_TCB · line 67

Quoted 5 lines
drivers/pwm/Kconfig +3 -0
 
+config PWM_BCM2835
+	tristate "BCM2835 PWM support"
+	depends on MACH_BCM2835 || MACH_BCM2708

There is no MACH_BCM2708 in the mainline kernel, just MACH_BCM2835. Actually, it looks like that should be ARCH_BCM2835 not MACH_BCM2835.

Quoted 12 lines
drivers/pwm/pwm-bcm2835.c +10 -0
 
+static int bcm2835_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
+{
+	struct bcm2835_pwm *pc = to_bcm2835_pwm(chip);
+	u32 value;
+
+	value = readl(pc->base);
+	value &= ~(PWM_CONTROL_MASK << PWM_CONTROL_STRIDE * pwm->pwm);
+	value |= (PWM_MODE << (PWM_CONTROL_STRIDE * pwm->pwm));
+	writel(value, pc->base);
+	return 0;

drivers/pwm/pwm-bcm2835.c line 63

Quoted 10 lines
drivers/pwm/pwm-bcm2835.c +10 -0
+}
+
+static void bcm2835_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
+{
+	struct bcm2835_pwm *pc = to_bcm2835_pwm(chip);
+	u32 value;
+
+	value = readl(pc->base)
+	value &= ~(PWM_CONTROL_MASK << PWM_CONTROL_STRIDE * pwm->pwm);
+	value &= (~DEFAULT << (PWM_CONTROL_STRIDE * pwm->pwm));

What is this second mask operation intended to do? The first mask operation already clears all the control bits, so clearing them again doesn't seem useful.

drivers/pwm/pwm-bcm2835.c line 75

Quoted 9 lines
drivers/pwm/pwm-bcm2835.c +9 -0
+static int bcm2835_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
+			      int duty_ns, int period_ns)
+{
+	struct bcm2835_pwm *pc = to_bcm2835_pwm(chip);
+
+	if (period_ns <= MIN_PERIOD) {
+		dev_err(pc->dev, "Period not supported\n");
+		return -EINVAL;
+	} else {

There's no need for the else { here; simply close the if with }, and put the rest of the code at the top-level of the function.

drivers/pwm/pwm-bcm2835.c line 79

Quoted 4 lines
drivers/pwm/pwm-bcm2835.c +4 -0
+		writel(duty_ns / pc->scaler,
+			 pc->base + DUTY + pwm->pwm * CHANNEL);
+		writel(period_ns / pc->scaler,
+			pc->base + PERIOD + pwm->pwm * CHANNEL);

It looks like CHANNEL should be CHANNEL_STRIDE?

Quoted 2 lines
drivers/pwm/pwm-bcm2835.c +2 -0
+static void bcm2835_pwm_disable(struct pwm_chip *chip,
+				struct pwm_device *pwm)

...

drivers/pwm/pwm-bcm2835.c line 103

Quoted 1 line
drivers/pwm/pwm-bcm2835.c +1 -0
+	value &= ~(PWM_ENABLE << (PWM_CONTROL_STRIDE * pwm->pwm));

It's not a big deal, but this code has brackets around << (PWM_CONTROL_STRIDE * pwm->pwm), but other places don't.

drivers/pwm/pwm-bcm2835.c line 119

Quoted 13 lines
drivers/pwm/pwm-bcm2835.c +13 -0
+static int bcm2835_set_polarity(struct pwm_chip *chip, struct pwm_device *pwm,
+				enum pwm_polarity polarity)
+{
+	struct bcm2835_pwm *pc = to_bcm2835_pwm(chip);
+	u32 value;
+
+	if (polarity == PWM_POLARITY_NORMAL) {
+		value = readl(pc->base);
+		value &= ~(PWM_POLARITY << PWM_CONTROL_STRIDE * pwm->pwm);
+	} else if (polarity == PWM_POLARITY_INVERSED) {
+		value = readl(pc->base);
+		value |= PWM_POLARITY << (PWM_CONTROL_STRIDE * pwm->pwm);
+	}

The readl() call is identical in both branches; it can come before the if.

drivers/pwm/pwm-bcm2835.c line 156

Quoted 6 lines
drivers/pwm/pwm-bcm2835.c +5 -0
+static int bcm2835_pwm_probe(struct platform_device *pdev)
 
+	pwm->clk = clk;
+	ret = clk_prepare_enable(pwm->clk);
+	if (ret)
+		return ret;

The error paths after this point don't call clk_disable_unprepare(). Perhaps there's a devm_clk_prepare_enable() you can use to solve this, or the error handling paths need to do more.

drivers/pwm/pwm-bcm2835.c line 169

Quoted 8 lines
drivers/pwm/pwm-bcm2835.c +7 -0
+	pwm->scaler = NSEC_PER_SEC / clk_get_rate(clk);
+
+	r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+	pwm->base = devm_ioremap_resource(&pdev->dev, r);
+	if (IS_ERR(pwm->base))
+		return PTR_ERR(pwm->base);
 
+	platform_set_drvdata(pdev, pwm);

Personally, I'd put that right after pwm is allocated, but it's not a big deal.

#2 lore raw
Quoted 8 lines · Stephen Warren
Quoted 5 lines · Bart Tanghe

Add pwm driver for Broadcom BCM2835 processor (Raspberry Pi)

---
Changes in v5:

By v5, I would drop "rfc" from the email subject.

And resend as well. Use resend only if you're resending without having made any changes.

Quoted 18 lines
Quoted 18 lines
drivers/pwm/pwm-bcm2835.c +16 -0
 
+static int bcm2835_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
+{
+	struct bcm2835_pwm *pc = to_bcm2835_pwm(chip);
+	u32 value;
+
+	value = readl(pc->base);
+	value &= ~(PWM_CONTROL_MASK << PWM_CONTROL_STRIDE * pwm->pwm);
+	value |= (PWM_MODE << (PWM_CONTROL_STRIDE * pwm->pwm));
+	writel(value, pc->base);
+	return 0;
+}
+
+static void bcm2835_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
+{
+	struct bcm2835_pwm *pc = to_bcm2835_pwm(chip);
+	u32 value;
Quoted 8 lines

drivers/pwm/pwm-bcm2835.c line 63

Quoted 4 lines
drivers/pwm/pwm-bcm2835.c +4 -0
+
+	value = readl(pc->base)
+	value &= ~(PWM_CONTROL_MASK << PWM_CONTROL_STRIDE * pwm->pwm);
+	value &= (~DEFAULT << (PWM_CONTROL_STRIDE * pwm->pwm));

What is this second mask operation intended to do? The first mask operation already clears all the control bits, so clearing them again doesn't seem useful.

I suspend that you'll also want to use pwm->hwpwm instead of pwm->pwm. pwm->hwpwm is the index of the PWM for the chip, whereas pwm->pwm is a global index so the above breaks if another driver registered a PWM chip before this driver.

Thierry