]> git.karo-electronics.de Git - linux-beck.git/commitdiff
pwm: Add BCM2835 PWM driver
authorBart Tanghe <bart.tanghe@thomasmore.be>
Wed, 8 Oct 2014 10:14:32 +0000 (12:14 +0200)
committerThierry Reding <thierry.reding@gmail.com>
Mon, 17 Nov 2014 11:20:13 +0000 (12:20 +0100)
Add PWM driver for Broadcom BCM2835 processor (Raspberry Pi)

Signed-off-by: Bart Tanghe <bart.tanghe@thomasmore.be>
Acked-by: Stephen Warren <swarren@wwwdotorg.org>
Signed-off-by: Thierry Reding <thierry.reding@gmail.com>
Documentation/devicetree/bindings/pwm/pwm-bcm2835.txt [new file with mode: 0644]
drivers/pwm/Kconfig
drivers/pwm/Makefile
drivers/pwm/pwm-bcm2835.c [new file with mode: 0644]

diff --git a/Documentation/devicetree/bindings/pwm/pwm-bcm2835.txt b/Documentation/devicetree/bindings/pwm/pwm-bcm2835.txt
new file mode 100644 (file)
index 0000000..fb6fb31
--- /dev/null
@@ -0,0 +1,30 @@
+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.
+- #pwm-cells: Should be 2. See pwm.txt in this directory for a description of
+  the cells format.
+
+Examples:
+
+pwm@2020c000 {
+       compatible = "brcm,bcm2835-pwm";
+       reg = <0x2020c000 0x28>;
+       clocks = <&clk_pwm>;
+       #pwm-cells = <2>;
+};
+
+clocks {
+       ....
+               clk_pwm: pwm {
+                       compatible = "fixed-clock";
+                       reg = <3>;
+                       #clock-cells = <0>;
+                       clock-frequency = <9200000>;
+               };
+       ....
+};
index ef2dd2e4754bacd784cd519ec7f4b66216a13e99..ddabe39835491ac7c8ee082694a01cb8f5a219dd 100644 (file)
@@ -71,6 +71,15 @@ config PWM_BCM_KONA
          To compile this driver as a module, choose M here: the module
          will be called pwm-bcm-kona.
 
+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
index c458606c3755c327ed5e648c3ddc68e2b884ffeb..88be33bbfdf6d13463ed013abe5b93eec9d7f4f5 100644 (file)
@@ -4,6 +4,7 @@ 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_BCM_KONA)     += pwm-bcm-kona.o
+obj-$(CONFIG_PWM_BCM2835)      += pwm-bcm2835.o
 obj-$(CONFIG_PWM_BFIN)         += pwm-bfin.o
 obj-$(CONFIG_PWM_CLPS711X)     += pwm-clps711x.o
 obj-$(CONFIG_PWM_EP93XX)       += pwm-ep93xx.o
diff --git a/drivers/pwm/pwm-bcm2835.c b/drivers/pwm/pwm-bcm2835.c
new file mode 100644 (file)
index 0000000..b4c7f95
--- /dev/null
@@ -0,0 +1,205 @@
+/*
+ * Copyright 2014 Bart Tanghe <bart.tanghe@thomasmore.be>
+ *
+ * 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 PWM_CONTROL            0x000
+#define PWM_CONTROL_SHIFT(x)   ((x) * 8)
+#define PWM_CONTROL_MASK       0xff
+#define PWM_MODE               0x80            /* set timer in PWM mode */
+#define PWM_ENABLE             (1 << 0)
+#define PWM_POLARITY           (1 << 4)
+
+#define PERIOD(x)              (((x) * 0x10) + 0x10)
+#define DUTY(x)                        (((x) * 0x10) + 0x14)
+
+#define MIN_PERIOD             108             /* 9.2 MHz max. PWM clock */
+
+struct bcm2835_pwm {
+       struct pwm_chip chip;
+       struct device *dev;
+       unsigned long scaler;
+       void __iomem *base;
+       struct clk *clk;
+};
+
+static inline struct bcm2835_pwm *to_bcm2835_pwm(struct pwm_chip *chip)
+{
+       return container_of(chip, struct bcm2835_pwm, chip);
+}
+
+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);
+       value &= ~(PWM_CONTROL_MASK << PWM_CONTROL_SHIFT(pwm->hwpwm));
+       value |= (PWM_MODE << PWM_CONTROL_SHIFT(pwm->hwpwm));
+       writel(value, pc->base + PWM_CONTROL);
+
+       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;
+
+       value = readl(pc->base + PWM_CONTROL);
+       value &= ~(PWM_CONTROL_MASK << PWM_CONTROL_SHIFT(pwm->hwpwm));
+       writel(value, pc->base + PWM_CONTROL);
+}
+
+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 %d not supported, minimum %d\n",
+                       period_ns, MIN_PERIOD);
+               return -EINVAL;
+       }
+
+       writel(duty_ns / pc->scaler, pc->base + DUTY(pwm->hwpwm));
+       writel(period_ns / pc->scaler, pc->base + PERIOD(pwm->hwpwm));
+
+       return 0;
+}
+
+static int bcm2835_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
+{
+       struct bcm2835_pwm *pc = to_bcm2835_pwm(chip);
+       u32 value;
+
+       value = readl(pc->base + PWM_CONTROL);
+       value |= PWM_ENABLE << PWM_CONTROL_SHIFT(pwm->hwpwm);
+       writel(value, pc->base + PWM_CONTROL);
+
+       return 0;
+}
+
+static void bcm2835_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
+{
+       struct bcm2835_pwm *pc = to_bcm2835_pwm(chip);
+       u32 value;
+
+       value = readl(pc->base + PWM_CONTROL);
+       value &= ~(PWM_ENABLE << PWM_CONTROL_SHIFT(pwm->hwpwm));
+       writel(value, pc->base + PWM_CONTROL);
+}
+
+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;
+
+       value = readl(pc->base + PWM_CONTROL);
+
+       if (polarity == PWM_POLARITY_NORMAL)
+               value &= ~(PWM_POLARITY << PWM_CONTROL_SHIFT(pwm->hwpwm));
+       else
+               value |= PWM_POLARITY << PWM_CONTROL_SHIFT(pwm->hwpwm);
+
+       writel(value, pc->base + PWM_CONTROL);
+
+       return 0;
+}
+
+static const struct pwm_ops bcm2835_pwm_ops = {
+       .request = bcm2835_pwm_request,
+       .free = bcm2835_pwm_free,
+       .config = bcm2835_pwm_config,
+       .enable = bcm2835_pwm_enable,
+       .disable = bcm2835_pwm_disable,
+       .set_polarity = bcm2835_set_polarity,
+       .owner = THIS_MODULE,
+};
+
+static int bcm2835_pwm_probe(struct platform_device *pdev)
+{
+       struct bcm2835_pwm *pc;
+       struct resource *res;
+       int ret;
+
+       pc = devm_kzalloc(&pdev->dev, sizeof(*pc), GFP_KERNEL);
+       if (!pc)
+               return -ENOMEM;
+
+       pc->dev = &pdev->dev;
+
+       res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+       pc->base = devm_ioremap_resource(&pdev->dev, res);
+       if (IS_ERR(pc->base))
+               return PTR_ERR(pc->base);
+
+       pc->clk = devm_clk_get(&pdev->dev, NULL);
+       if (IS_ERR(pc->clk)) {
+               dev_err(&pdev->dev, "clock not found: %ld\n", PTR_ERR(pc->clk));
+               return PTR_ERR(pc->clk);
+       }
+
+       ret = clk_prepare_enable(pc->clk);
+       if (ret)
+               return ret;
+
+       pc->scaler = NSEC_PER_SEC / clk_get_rate(pc->clk);
+
+       pc->chip.dev = &pdev->dev;
+       pc->chip.ops = &bcm2835_pwm_ops;
+       pc->chip.npwm = 2;
+
+       platform_set_drvdata(pdev, pc);
+
+       ret = pwmchip_add(&pc->chip);
+       if (ret < 0)
+               goto add_fail;
+
+       return 0;
+
+add_fail:
+       clk_disable_unprepare(pc->clk);
+       return ret;
+}
+
+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",
+               .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 <bart.tanghe@thomasmore.be");
+MODULE_DESCRIPTION("Broadcom BCM2835 PWM driver");
+MODULE_LICENSE("GPL v2");