pwm: add BCM2835 PWM driver
Subsystems (4)
PATCH v6 1/1 pwm: add BCM2835 PWM driver
Add pwm driver for Broadcom BCM2835 processor (Raspberry Pi)
---
Changes in v6:
- changed the global index of the pwm to local pwm index
- change to the mainline kernel Kconfig architecture dependency
- clean up the code (Stephen Warren comments)
Documentation/devicetree/bindings/pwm/pwm-bcm2835.txt new file +27 -0
@@ -0,0 +1,27 @@ +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 +- clock: this clock defines the base clock frequency of the pwm +hardware system, the period and the duty_cycle of the pwm signal is a +multiple of the base period + +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
@@ -29,6 +29,15 @@ menuconfig PWM if PWM +config PWM_BCM2835 + tristate "BCM2835 PWM support" + depends on ARCH_BCM2835 + 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/pwm-bcm2835.c new file +201 -0
201 lines not on this page · raw message
Quoted 34 lines · Bart Tanghe
Add pwm driver for Broadcom BCM2835 processor (Raspberry Pi)
drivers/pwm/pwm-bcm2835.c new file +29
-0@@ -0,0 +1,201 @@ +/* +* Copyright 2014 Bart Tanghe <[email protected]> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; version 2. +*/ + +#include <linux/clk.h> +#include <linux/err.h> +#include <linux/io.h> +#include <linux/module.h> +#include <linux/of.h> +#include <linux/platform_device.h> +#include <linux/pwm.h> + +#define DUTY 0x14 +#define PERIOD 0x10 +#define CHANNEL 0x10 + +#define PWM_ENABLE (1 << 0) +#define PWM_POLARITY (1 << 4) + +#define PWM_CONTROL_MASK 0xff +#define PWM_MODE 0x80/* set timer in pwm mode */ +#define DEFAULT 0xff/* set timer in default mode */ +#define PWM_CONTROL_STRIDE 8 +#define MIN_PERIOD 108/* 9.2Mhz max pwm clock */ +
drivers/pwm/pwm-bcm2835.c line 39
Quoted 10 lines
drivers/pwm/pwm-bcm2835.c new file +10
-0+struct bcm2835_pwm { +struct pwm_chip chip; +struct device *dev; +int channel; +unsigned long scaler; +void __iomem *base; +struct clk *clk; +}; + +static inline struct bcm2835_pwm *to_bcm2835_pwm (struct pwm_chip *chip)
^^
nitpick: on space is enough
Quoted 4 lines
drivers/pwm/pwm-bcm2835.c new file +4
-0+{ + return container_of(chip, struct bcm2835_pwm, chip); +} +
[...]
Quoted 13 lines
drivers/pwm/pwm-bcm2835.c new file +13
-0+static int bcm2835_pwm_probe (struct platform_device *pdev) +{ +struct bcm2835_pwm *pwm; +int ret; +struct resource *r; +struct clk *clk; + + pwm =devm_kzalloc (&pdev->dev,sizeof (*pwm),GFP_KERNEL ); +if (!pwm) +return -ENOMEM ; + + pwm->dev = &pdev->dev; +
drivers/pwm/pwm-bcm2835.c line 151
Quoted 10 lines
drivers/pwm/pwm-bcm2835.c new file +10
-0+ clk =devm_clk_get (&pdev->dev,NULL ); +if (IS_ERR (clk)) { +dev_err (&pdev->dev,"no clock found: %ld\n" ,PTR_ERR (clk)); +return PTR_ERR (clk); + } + + pwm->clk = clk; + ret =clk_prepare_enable (pwm->clk); +if (ret) { +clk_disable_unprepare (pwm->clk);
If enabling of the clock fails, don't disable it.
drivers/pwm/pwm-bcm2835.c line 160
Quoted 9 lines
drivers/pwm/pwm-bcm2835.c new file +9
-0+return ret; + } + + 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);
Here you should disable the clock instead.
drivers/pwm/pwm-bcm2835.c line 171
Quoted 11 lines
drivers/pwm/pwm-bcm2835.c new file +11
-0+ + pwm->chip.dev = &pdev->dev; + pwm->chip.ops = &bcm2835_pwm_ops; + pwm->chip.npwm =2 ; + +platform_set_drvdata (pdev, pwm); + + ret =pwmchip_add (&pwm->chip); +if (ret <0 ) { +dev_err (&pdev->dev,"pwmchip_add() failed: %d\n" , ret); +return ret;
and here....or better introduce a disable_clock jump-label at end of this function.
drivers/pwm/pwm-bcm2835.c line 54
Quoted 3 lines
drivers/pwm/pwm-bcm2835.c new file +3
-0+ } +return 0 ; +}
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 1 line · Bart Tanghe
Add pwm driver for Broadcom BCM2835 processor (Raspberry Pi)
Just a few nits below,
drivers/pwm/pwm-bcm2835.c line 26
Quoted 5 lines
drivers/pwm/pwm-bcm2835.c +3
-0+#define PWM_CONTROL_MASK 0xff +#define PWM_MODE 0x80/* set timer in pwm mode */ +#define DEFAULT 0xff/* set timer in default mode */
BTW, it'd be nice to say default *what*. Perhaps DEFAULT_PWM_MODE?
drivers/pwm/pwm-bcm2835.c line 51
Quoted 8 lines
drivers/pwm/pwm-bcm2835.c +8
-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->hwpwm); + value |= (PWM_MODE << (PWM_CONTROL_STRIDE * pwm->hwpwm));
Presence/absence of brackets around the STRIDE*pwm calculation is still inconsistent here, and in other places.
drivers/pwm/pwm-bcm2835.c line 62
Quoted 7 lines
drivers/pwm/pwm-bcm2835.c +7
-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 &= (~DEFAULT << (PWM_CONTROL_STRIDE * pwm->hwpwm));
Why clear the "DEFAULT" bits here; why not use PWM_CONTROL_MASK?