pwm: add BCM2835 PWM driver
Subsystems (4)
RFC v4 1/1 pwm: add BCM2835 PWM driver
Add pwm driver for Broadcom BCM2835 processor (Raspberry Pi)
---
Changes in v4:
- add the second pwm channel (tested on raspberry pi B+)
- add the code and code style recommendations
Documentation/devicetree/bindings/pwm/pwm-bcm2835.txt new file +13 -0
@@ -0,0 +1,13 @@ +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 = "bcrm,bcm2835-pwm"; + reg = <0x2020c000 0x28>; + clocks = <&clk_pwm>; +};
drivers/pwm/Kconfig +9 -0
@@ -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
@@ -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 +198 -0
198 lines not on this page · raw message
Quoted 4 lines · Bart Tanghe
drivers/pwm/pwm-bcm2835.c new file +4
-0+static int bcm2835_pwm_probe (struct platform_device *pdev) +{ +struct bcm2835_pwm *pwm; +int ret;
drivers/pwm/pwm-bcm2835.c line 145
Quoted 10 lines
drivers/pwm/pwm-bcm2835.c new file +10
-0+struct resource *r; +struct clk *clk; + + pwm =devm_kzalloc (&pdev->dev,sizeof (*pwm),GFP_KERNEL ); +if (!pwm) +return -ENOMEM ; + + pwm->dev = &pdev->dev; + + clk =clk_get (&pdev->dev,NULL );
Better use devm_, as you don't free the clock in case of an error and during remove()
drivers/pwm/pwm-bcm2835.c line 152
Quoted 7 lines
drivers/pwm/pwm-bcm2835.c new file +7
-0+if (IS_ERR (clk)) { +dev_err (&pdev->dev,"no clock found: %ld\n" ,PTR_ERR (clk)); +return PTR_ERR (clk); + } + + pwm->clk = clk; +clk_prepare_enable (pwm->clk);
Please check the return value of clk_prepare_enable()
Quoted 5 lines
drivers/pwm/pwm-bcm2835.c new file +5
-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))
drivers/pwm/pwm-bcm2835.c line 167
Quoted 10 lines
drivers/pwm/pwm-bcm2835.c new file +10
-0+return PTR_ERR (pwm->base); + + pwm->chip.dev = &pdev->dev; + pwm->chip.ops = &bcm2835_pwm_ops; + pwm->chip.npwm =2 ; + + ret =pwmchip_add (&pwm->chip); +if (ret <0 ) { +dev_err (&pdev->dev,"pwmchip_add() failed: %d\n" , ret); +return -1 ;
Please propagate the error vlaue:
return ret;
Review
Quoted 2 lines
(quoted diff) +2
-0+ } + platform_set_drvdata(pdev, pwm);
Better do this before pwmchip_add()
Quoted 19 lines
drivers/pwm/pwm-bcm2835.c new file +19
-0+return 0 ; +} + +static int bcm2835_pwm_remove (struct platform_device *pdev) +{ +struct bcm2835_pwm *pc =platform_get_drvdata (pdev); +clk_disable_unprepare (pc->clk); +return pwmchip_remove (&pc->chip); +} + +static const struct of_device_id bcm2835_pwm_of_match[] = { + { .compatible ="brcm,bcm2835-pwm" , }, + {/* sentinel */ } +}; +MODULE_DEVICE_TABLE (of, bcm2835_pwm_of_match); + +static struct platform_driver bcm2835_pwm_driver = { + .driver = { + .name ="bcm2835-pwm" ,
drivers/pwm/pwm-bcm2835.c line 198
Quoted 10 lines
drivers/pwm/pwm-bcm2835.c new file +10
-0+ .of_match_table = bcm2835_pwm_of_match, + }, + .probe = bcm2835_pwm_probe, + .remove = bcm2835_pwm_remove, +}; +module_platform_driver (bcm2835_pwm_driver); + +MODULE_AUTHOR ("Bart Tanghe <[email protected]" ); +MODULE_DESCRIPTION ("Broadcom BCM2835 PWM driver" ); +MODULE_LICENSE ("GPL v2" );
regards,
Marc
Signature · 4 lines
Pengutronix e.K. | Marc Kleine-Budde | Industrial Linux Solutions | Phone: +49-231-2826-924 | Vertretung West/Dortmund | Fax: +49-5121-206917-5555 | Amtsgericht Hildesheim, HRA 2686 | http://www.pengutronix.de |
Quoted 2 lines · Bart Tanghe
Add pwm driver for Broadcom BCM2835 processor (Raspberry Pi)
Signed-off-by Bart Tanghe <[email protected]>
There needs to be a blank line between the description and the tags.
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
This needs to document the clock property too.
It'd be nice to require clock-names rather than doing clock lookup by index, but I suppose it's not too much of a problem with this device.
drivers/pwm/pwm-bcm2835.c line 2
Quoted 4 lines
drivers/pwm/pwm-bcm2835.c +2
-0+/* +* Copyright (C) 2014 Thomas more
But the git commit doesn't have "Thomas More" as the author, nor Thomas' Signed-off-by line. Can you explain the history of this code?
drivers/pwm/pwm-bcm2835.c line 49
Quoted 7 lines
drivers/pwm/pwm-bcm2835.c +7
-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) & ~(PWM_CONTROL_MASK <<8 * pwm->pwm); + value |= (PWM_CONTROL_ENABLE << (8 * pwm->pwm));
It'd be nice to use a #define rather than hard-coded "8" here. Perhaps:
#define PWM_CONTROL_STRIDE 8
Why does _request() enable the PWM output; shouldn't that be deferred until _enable()? _free() might not want to disable the output, if the PWM core guarantees that _disable() is always called.
drivers/pwm/pwm-bcm2835.c line 76
Quoted 13 lines
drivers/pwm/pwm-bcm2835.c +13
-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 ) { +writel (duty_ns / pc->scaler, + pc->base +DUTY + pwm->pwm *CHANNEL ); +writel (period_ns / pc->scaler, + pc->base +PERIOD + pwm->pwm *CHANNEL ); + }else { +dev_err (pc->dev,"Period not supported\n" ); + }
The "else" case should propagate the error. It'd be better to do the error-checking first to remove an indent level from the rest of the code:
if (period_ns <= MIN_PERIOD) {
dev_err(...);
return -EINVAL;
}
writel(...);Quoted 5 lines
drivers/pwm/pwm-bcm2835.c +5
-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;
drivers/pwm/pwm-bcm2835.c line 118
Quoted 10 lines
drivers/pwm/pwm-bcm2835.c +10
-0+ +if (polarity ==PWM_POLARITY_NORMAL ) { + value =readl (pc->base); + value &= ~(PWM_POLARITY <<8 * pwm->pwm); +writel (value, pc->base); + }else if (polarity ==PWM_POLARITY_INVERSED ) { + value =readl (pc->base); + value |=PWM_POLARITY << (8 * pwm->pwm); +writel (value, pc->base); + }
If you move the readl/writel outside the if statement, you remove the duplication of code.
Quoted 1 line
+static const struct
pwm_opsbcm2835_pwm_ops= {
...
drivers/pwm/pwm-bcm2835.c
Quoted 2 lines
drivers/pwm/pwm-bcm2835.c +2
-0+ .owner = THIS_MODULE, +};
Doesn't something in the driver core automatically set .owner now? Perhaps that's only for certain subsystems though?
Quoted 1 line
+MODULE_AUTHOR("Bart Tanghe <[email protected]");
That doesn't match the (c) message at the start of the file.