* [PATCHv5 0/2] Add Allwinner SoCs PWM support
@ 2014-05-19 18:10 Alexandre Belloni
2014-05-19 18:10 ` [PATCHv5 1/2] pwm: Add Allwinner SoC support Alexandre Belloni
2014-05-19 18:10 ` [PATCHv5 2/2] pwm: sunxi: document OF bindings Alexandre Belloni
0 siblings, 2 replies; 8+ messages in thread
From: Alexandre Belloni @ 2014-05-19 18:10 UTC (permalink / raw)
To: Thierry Reding, Maxime Ripard
Cc: linux-pwm, linux-doc, linux-arm-kernel, linux-kernel,
Alexandre Belloni
Hi,
This patch set adds support for the PWM controller found on the Allwinner SoCs.
The first patch adds the driver itself.
The second patch adds the DT binding documentation
Changes in v5:
- use GENMASK for all masks
- use clk_prepare_enable()/clk_disable_unprepare() as the drier may sleep
because of the mutex anyway
Alexandre Belloni (2):
pwm: Add Allwinner SoC support
pwm: sunxi: document OF bindings
.../devicetree/bindings/pwm/pwm-sunxi.txt | 20 ++
drivers/pwm/Kconfig | 9 +
drivers/pwm/Makefile | 1 +
drivers/pwm/pwm-sunxi.c | 338 +++++++++++++++++++++
4 files changed, 368 insertions(+)
create mode 100644 Documentation/devicetree/bindings/pwm/pwm-sunxi.txt
create mode 100644 drivers/pwm/pwm-sunxi.c
--
1.9.1
^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCHv5 1/2] pwm: Add Allwinner SoC support
2014-05-19 18:10 [PATCHv5 0/2] Add Allwinner SoCs PWM support Alexandre Belloni
@ 2014-05-19 18:10 ` Alexandre Belloni
2014-06-17 23:26 ` Thierry Reding
2014-05-19 18:10 ` [PATCHv5 2/2] pwm: sunxi: document OF bindings Alexandre Belloni
1 sibling, 1 reply; 8+ messages in thread
From: Alexandre Belloni @ 2014-05-19 18:10 UTC (permalink / raw)
To: Thierry Reding, Maxime Ripard
Cc: linux-pwm, linux-doc, linux-arm-kernel, linux-kernel,
Alexandre Belloni
This adds a generic PWM framework driver for the PWM controller
found on Allwinner SoCs.
Signed-off-by: Alexandre Belloni <alexandre.belloni@free-electrons.com>
Acked-by: Maxime Ripard <maxime.ripard@free-electrons.com>
---
drivers/pwm/Kconfig | 9 ++
drivers/pwm/Makefile | 1 +
drivers/pwm/pwm-sunxi.c | 338 ++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 348 insertions(+)
create mode 100644 drivers/pwm/pwm-sunxi.c
diff --git a/drivers/pwm/Kconfig b/drivers/pwm/Kconfig
index 5b34ff29ea38..178b017be827 100644
--- a/drivers/pwm/Kconfig
+++ b/drivers/pwm/Kconfig
@@ -217,6 +217,15 @@ config PWM_SPEAR
To compile this driver as a module, choose M here: the module
will be called pwm-spear.
+config PWM_SUNXI
+ tristate "Allwinner PWM support"
+ depends on ARCH_SUNXI || COMPILE_TEST
+ help
+ Generic PWM framework driver for Allwinner SoCs.
+
+ To compile this driver as a module, choose M here: the module
+ will be called pwm-sunxi.
+
config PWM_TEGRA
tristate "NVIDIA Tegra PWM support"
depends on ARCH_TEGRA
diff --git a/drivers/pwm/Makefile b/drivers/pwm/Makefile
index e57d2c38a794..39997ea2e276 100644
--- a/drivers/pwm/Makefile
+++ b/drivers/pwm/Makefile
@@ -19,6 +19,7 @@ obj-$(CONFIG_PWM_PXA) += pwm-pxa.o
obj-$(CONFIG_PWM_RENESAS_TPU) += pwm-renesas-tpu.o
obj-$(CONFIG_PWM_SAMSUNG) += pwm-samsung.o
obj-$(CONFIG_PWM_SPEAR) += pwm-spear.o
+obj-$(CONFIG_PWM_SUNXI) += pwm-sunxi.o
obj-$(CONFIG_PWM_TEGRA) += pwm-tegra.o
obj-$(CONFIG_PWM_TIECAP) += pwm-tiecap.o
obj-$(CONFIG_PWM_TIEHRPWM) += pwm-tiehrpwm.o
diff --git a/drivers/pwm/pwm-sunxi.c b/drivers/pwm/pwm-sunxi.c
new file mode 100644
index 000000000000..e7c3ca1d3c42
--- /dev/null
+++ b/drivers/pwm/pwm-sunxi.c
@@ -0,0 +1,338 @@
+/*
+ * Driver for Allwinner Pulse Width Modulation Controller
+ *
+ * Copyright (C) 2014 Alexandre Belloni <alexandre.belloni@free-electrons.com>
+ *
+ * Licensed under GPLv2.
+ */
+
+#include <linux/bitops.h>
+#include <linux/clk.h>
+#include <linux/err.h>
+#include <linux/io.h>
+#include <linux/of.h>
+#include <linux/of_device.h>
+#include <linux/platform_device.h>
+#include <linux/pwm.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/slab.h>
+
+#define PWM_CTRL_REG 0x0
+
+#define PWM_CH_PRD_BASE 0x4
+#define PWM_CH_PRD_OFF 0x4
+#define PWM_CH_PRD(x) (PWM_CH_PRD_BASE + PWM_CH_PRD_OFF * (x))
+
+#define PWMCH_OFFSET 15
+#define PWM_PRESCAL_MASK GENMASK(3, 0)
+#define PWM_PRESCAL_OFF 0
+#define PWM_EN BIT(4)
+#define PWM_ACT_STATE BIT(5)
+#define PWM_CLK_GATING BIT(6)
+#define PWM_MODE BIT(7)
+#define PWM_PULSE BIT(8)
+#define PWM_BYPASS BIT(9)
+
+#define PWM_RDY_BASE 28
+#define PWM_RDY_OFF 1
+#define PWM_RDY(x) BIT(PWM_RDY_BASE + PWM_RDY_OFF * (x))
+
+#define PWM_PRD_ACT_MASK GENMASK(7, 0)
+#define PWM_PRD(x) ((x - 1) << 16)
+#define PWM_PRD_MASK GENMASK(7, 0)
+
+#define BIT_CH(bit, chan) (bit << (chan * PWMCH_OFFSET))
+
+u32 prescal_table[] = { 120, 180, 240, 360, 480, 0, 0, 0,
+ 12000, 24000, 36000, 48000, 72000,
+ 0, 0, 1 };
+
+struct sunxi_pwm_data {
+ bool has_rdy;
+};
+
+struct sunxi_pwm_chip {
+ struct pwm_chip chip;
+ struct clk *clk;
+ void __iomem *base;
+ struct mutex ctrl_lock;
+ const struct sunxi_pwm_data *data;
+};
+
+#define to_sunxi_pwm_chip(chip) container_of(chip, struct sunxi_pwm_chip, chip)
+
+static inline u32 sunxi_pwm_readl(struct sunxi_pwm_chip *chip,
+ unsigned long offset)
+{
+ return readl(chip->base + offset);
+}
+
+static inline void sunxi_pwm_writel(struct sunxi_pwm_chip *chip,
+ unsigned long offset, unsigned long val)
+{
+ writel(val, chip->base + offset);
+}
+
+static int sunxi_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
+ int duty_ns, int period_ns)
+{
+ struct sunxi_pwm_chip *sunxi_pwm = to_sunxi_pwm_chip(chip);
+ u32 clk_rate, prd, dty;
+ u64 div;
+ u32 val, clk_gate;
+ int i, ret;
+
+ clk_rate = clk_get_rate(sunxi_pwm->clk);
+
+ /* First, test without any divider */
+ i = PWM_PRESCAL_MASK;
+ div = clk_rate * period_ns;
+ do_div(div, 1000000000);
+ if (div > PWM_PRD_MASK) {
+ /* Then go up from the first divider */
+ for (i = 0; i < PWM_PRESCAL_MASK; i++) {
+ if (!prescal_table[i])
+ continue;
+ div = clk_rate / prescal_table[i];
+ div = div * period_ns;
+ do_div(div, 1000000000);
+ if (div <= PWM_PRD_MASK)
+ break;
+ }
+ }
+
+ if (div > PWM_PRD_MASK) {
+ dev_err(chip->dev, "prescaler exceeds the maximum value\n");
+ return -EINVAL;
+ }
+
+ prd = div;
+ div *= duty_ns;
+ do_div(div, period_ns);
+ dty = div;
+
+ ret = clk_prepare_enable(sunxi_pwm->clk);
+ if (ret) {
+ dev_err(chip->dev, "failed to enable PWM clock\n");
+ return ret;
+ }
+
+ mutex_lock(&sunxi_pwm->ctrl_lock);
+ val = sunxi_pwm_readl(sunxi_pwm, PWM_CTRL_REG);
+
+ if (sunxi_pwm->data->has_rdy && (val & PWM_RDY(pwm->hwpwm))) {
+ mutex_unlock(&sunxi_pwm->ctrl_lock);
+ clk_disable_unprepare(sunxi_pwm->clk);
+ return -EBUSY;
+ }
+
+ clk_gate = val & BIT_CH(PWM_CLK_GATING, pwm->hwpwm);
+ if (clk_gate) {
+ val &= ~BIT_CH(PWM_CLK_GATING, pwm->hwpwm);
+ sunxi_pwm_writel(sunxi_pwm, PWM_CTRL_REG, val);
+ }
+
+ val = sunxi_pwm_readl(sunxi_pwm, PWM_CTRL_REG);
+ val &= ~BIT_CH(PWM_PRESCAL_MASK, pwm->hwpwm);
+ val |= i;
+ sunxi_pwm_writel(sunxi_pwm, PWM_CTRL_REG, val);
+
+ sunxi_pwm_writel(sunxi_pwm, PWM_CH_PRD(pwm->hwpwm), dty | PWM_PRD(prd));
+
+ if (clk_gate) {
+ val = sunxi_pwm_readl(sunxi_pwm, PWM_CTRL_REG);
+ val |= clk_gate;
+ sunxi_pwm_writel(sunxi_pwm, PWM_CTRL_REG, val);
+ }
+
+ mutex_unlock(&sunxi_pwm->ctrl_lock);
+ clk_disable_unprepare(sunxi_pwm->clk);
+
+ return 0;
+}
+
+static int sunxi_pwm_set_polarity(struct pwm_chip *chip, struct pwm_device *pwm,
+ enum pwm_polarity polarity)
+{
+ struct sunxi_pwm_chip *sunxi_pwm = to_sunxi_pwm_chip(chip);
+ u32 val;
+ int ret;
+
+ ret = clk_prepare_enable(sunxi_pwm->clk);
+ if (ret) {
+ dev_err(chip->dev, "failed to enable PWM clock\n");
+ return ret;
+ }
+
+ mutex_lock(&sunxi_pwm->ctrl_lock);
+ val = sunxi_pwm_readl(sunxi_pwm, PWM_CTRL_REG);
+
+ if (polarity != PWM_POLARITY_NORMAL)
+ val &= ~BIT_CH(PWM_ACT_STATE, pwm->hwpwm);
+ else
+ val |= BIT_CH(PWM_ACT_STATE, pwm->hwpwm);
+
+
+ sunxi_pwm_writel(sunxi_pwm, PWM_CTRL_REG, val);
+
+ mutex_unlock(&sunxi_pwm->ctrl_lock);
+ clk_disable_unprepare(sunxi_pwm->clk);
+
+ return 0;
+}
+
+static int sunxi_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
+{
+ struct sunxi_pwm_chip *sunxi_pwm = to_sunxi_pwm_chip(chip);
+ u32 val;
+ int ret;
+
+ ret = clk_prepare_enable(sunxi_pwm->clk);
+ if (ret) {
+ dev_err(chip->dev, "failed to enable PWM clock\n");
+ return ret;
+ }
+
+ mutex_lock(&sunxi_pwm->ctrl_lock);
+ val = sunxi_pwm_readl(sunxi_pwm, PWM_CTRL_REG);
+ val |= BIT_CH(PWM_EN, pwm->hwpwm);
+ val |= BIT_CH(PWM_CLK_GATING, pwm->hwpwm);
+ sunxi_pwm_writel(sunxi_pwm, PWM_CTRL_REG, val);
+ mutex_unlock(&sunxi_pwm->ctrl_lock);
+
+ return 0;
+}
+
+static void sunxi_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
+{
+ struct sunxi_pwm_chip *sunxi_pwm = to_sunxi_pwm_chip(chip);
+ u32 val;
+
+ mutex_lock(&sunxi_pwm->ctrl_lock);
+ val = sunxi_pwm_readl(sunxi_pwm, PWM_CTRL_REG);
+ val &= ~BIT_CH(PWM_EN, pwm->hwpwm);
+ val &= ~BIT_CH(PWM_CLK_GATING, pwm->hwpwm);
+ sunxi_pwm_writel(sunxi_pwm, PWM_CTRL_REG, val);
+ mutex_unlock(&sunxi_pwm->ctrl_lock);
+
+ clk_disable_unprepare(sunxi_pwm->clk);
+}
+
+static const struct pwm_ops sunxi_pwm_ops = {
+ .config = sunxi_pwm_config,
+ .set_polarity = sunxi_pwm_set_polarity,
+ .enable = sunxi_pwm_enable,
+ .disable = sunxi_pwm_disable,
+ .owner = THIS_MODULE,
+};
+
+static const struct sunxi_pwm_data sunxi_pwm_data_a10 = {
+ .has_rdy = false,
+};
+
+static const struct sunxi_pwm_data sunxi_pwm_data_a20 = {
+ .has_rdy = true,
+};
+
+static const struct of_device_id sunxi_pwm_dt_ids[] = {
+ {
+ .compatible = "allwinner,sun4i-a10-pwm",
+ .data = &sunxi_pwm_data_a10,
+ }, {
+ .compatible = "allwinner,sun7i-a20-pwm",
+ .data = &sunxi_pwm_data_a20,
+ }, {
+ /* sentinel */
+ },
+};
+MODULE_DEVICE_TABLE(of, sunxi_pwm_dt_ids);
+
+static int sunxi_pwm_probe(struct platform_device *pdev)
+{
+ struct sunxi_pwm_chip *sunxi_pwm;
+ struct resource *res;
+ int ret;
+
+ const struct of_device_id *match;
+
+ match = of_match_device(sunxi_pwm_dt_ids, &pdev->dev);
+ if (!match || !match->data)
+ return -ENODEV;
+
+ sunxi_pwm = devm_kzalloc(&pdev->dev, sizeof(*sunxi_pwm), GFP_KERNEL);
+ if (!sunxi_pwm)
+ return -ENOMEM;
+
+ res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+ sunxi_pwm->base = devm_ioremap_resource(&pdev->dev, res);
+ if (IS_ERR(sunxi_pwm->base))
+ return PTR_ERR(sunxi_pwm->base);
+
+ sunxi_pwm->clk = devm_clk_get(&pdev->dev, NULL);
+ if (IS_ERR(sunxi_pwm->clk))
+ return PTR_ERR(sunxi_pwm->clk);
+
+ sunxi_pwm->chip.dev = &pdev->dev;
+ sunxi_pwm->chip.ops = &sunxi_pwm_ops;
+
+ sunxi_pwm->chip.base = -1;
+ sunxi_pwm->chip.npwm = 2;
+ sunxi_pwm->chip.can_sleep = true;
+ sunxi_pwm->chip.of_xlate = of_pwm_xlate_with_flags;
+ sunxi_pwm->chip.of_pwm_n_cells = 3;
+ sunxi_pwm->data = match->data;
+
+ mutex_init(&sunxi_pwm->ctrl_lock);
+
+ ret = clk_prepare_enable(sunxi_pwm->clk);
+ if (ret) {
+ dev_err(&pdev->dev, "failed to enable PWM clock\n");
+ goto error;
+ }
+
+ /* By default, the polarity is inversed, set it to normal */
+ sunxi_pwm_writel(sunxi_pwm, PWM_CTRL_REG,
+ BIT_CH(PWM_ACT_STATE, 0) |
+ BIT_CH(PWM_ACT_STATE, 1));
+ clk_disable_unprepare(sunxi_pwm->clk);
+
+ ret = pwmchip_add(&sunxi_pwm->chip);
+ if (ret < 0) {
+ dev_err(&pdev->dev, "failed to add PWM chip %d\n", ret);
+ goto error;
+ }
+
+ platform_set_drvdata(pdev, sunxi_pwm);
+
+ return ret;
+
+error:
+ mutex_destroy(&sunxi_pwm->ctrl_lock);
+ clk_disable_unprepare(sunxi_pwm->clk);
+ return ret;
+}
+
+static int sunxi_pwm_remove(struct platform_device *pdev)
+{
+ struct sunxi_pwm_chip *sunxi_pwm = platform_get_drvdata(pdev);
+
+ mutex_destroy(&sunxi_pwm->ctrl_lock);
+
+ return pwmchip_remove(&sunxi_pwm->chip);
+}
+
+static struct platform_driver sunxi_pwm_driver = {
+ .driver = {
+ .name = "sunxi-pwm",
+ .of_match_table = sunxi_pwm_dt_ids,
+ },
+ .probe = sunxi_pwm_probe,
+ .remove = sunxi_pwm_remove,
+};
+module_platform_driver(sunxi_pwm_driver);
+
+MODULE_ALIAS("platform:sunxi-pwm");
+MODULE_AUTHOR("Alexandre Belloni <alexandre.belloni@free-electrons.com>");
+MODULE_DESCRIPTION("Allwinner PWM driver");
+MODULE_LICENSE("GPL v2");
--
1.9.1
^ permalink raw reply related [flat|nested] 8+ messages in thread
* [PATCHv5 2/2] pwm: sunxi: document OF bindings
2014-05-19 18:10 [PATCHv5 0/2] Add Allwinner SoCs PWM support Alexandre Belloni
2014-05-19 18:10 ` [PATCHv5 1/2] pwm: Add Allwinner SoC support Alexandre Belloni
@ 2014-05-19 18:10 ` Alexandre Belloni
2014-06-17 23:29 ` Thierry Reding
1 sibling, 1 reply; 8+ messages in thread
From: Alexandre Belloni @ 2014-05-19 18:10 UTC (permalink / raw)
To: Thierry Reding, Maxime Ripard
Cc: linux-pwm, linux-doc, linux-arm-kernel, linux-kernel,
Alexandre Belloni
This is the documentation for the Allwinner Socs PWM bindings.
Signed-off-by: Alexandre Belloni <alexandre.belloni@free-electrons.com>
Acked-by: Maxime Ripard <maxime.ripard@free-electrons.com>
---
Documentation/devicetree/bindings/pwm/pwm-sunxi.txt | 20 ++++++++++++++++++++
1 file changed, 20 insertions(+)
create mode 100644 Documentation/devicetree/bindings/pwm/pwm-sunxi.txt
diff --git a/Documentation/devicetree/bindings/pwm/pwm-sunxi.txt b/Documentation/devicetree/bindings/pwm/pwm-sunxi.txt
new file mode 100644
index 000000000000..9eda87e7d233
--- /dev/null
+++ b/Documentation/devicetree/bindings/pwm/pwm-sunxi.txt
@@ -0,0 +1,20 @@
+Allwinner PWM controller
+
+Required properties:
+ - compatible: should be one of:
+ - "allwinner,sun4i-a10-pwm"
+ - "allwinner,sun7i-a20-pwm"
+ - reg: physical base address and length of the controller's registers
+ - #pwm-cells: should be 3. See pwm.txt in this directory for a description of
+ the cells format.
+ - clocks: from common clock binding, handle to the parent clock.
+
+Example:
+
+ pwm: pwm@01c20e00 {
+ compatible = "allwinner,sun7i-a20-pwm";
+ reg = <0x01c20e00 0xc>;
+ clocks = <&osc24M>;
+ #pwm-cells = <3>;
+ status = "disabled";
+ };
--
1.9.1
^ permalink raw reply related [flat|nested] 8+ messages in thread
* Re: [PATCHv5 1/2] pwm: Add Allwinner SoC support
2014-05-19 18:10 ` [PATCHv5 1/2] pwm: Add Allwinner SoC support Alexandre Belloni
@ 2014-06-17 23:26 ` Thierry Reding
2014-06-23 17:01 ` Alexandre Belloni
0 siblings, 1 reply; 8+ messages in thread
From: Thierry Reding @ 2014-06-17 23:26 UTC (permalink / raw)
To: Alexandre Belloni
Cc: Maxime Ripard, linux-pwm, linux-doc, linux-arm-kernel,
linux-kernel
[-- Attachment #1: Type: text/plain, Size: 7938 bytes --]
On Mon, May 19, 2014 at 08:10:02PM +0200, Alexandre Belloni wrote:
[...]
> diff --git a/drivers/pwm/pwm-sunxi.c b/drivers/pwm/pwm-sunxi.c
[...]
> +#include <linux/bitops.h>
> +#include <linux/clk.h>
> +#include <linux/err.h>
> +#include <linux/io.h>
> +#include <linux/of.h>
> +#include <linux/of_device.h>
> +#include <linux/platform_device.h>
> +#include <linux/pwm.h>
> +#include <linux/module.h>
> +#include <linux/mutex.h>
> +#include <linux/slab.h>
These should be ordered alphabetically.
> +#define PWM_CTRL_REG 0x0
> +
> +#define PWM_CH_PRD_BASE 0x4
> +#define PWM_CH_PRD_OFF 0x4
Is this supposed to be an offset? If so, then maybe it should be named
PWM_CH_PRD_OFFSET?
> +#define PWM_CH_PRD(x) (PWM_CH_PRD_BASE + PWM_CH_PRD_OFF * (x))
x is the channel number? If so maybe call that parameter "ch"?
> +#define PWMCH_OFFSET 15
> +#define PWM_PRESCAL_MASK GENMASK(3, 0)
> +#define PWM_PRESCAL_OFF 0
> +#define PWM_EN BIT(4)
> +#define PWM_ACT_STATE BIT(5)
> +#define PWM_CLK_GATING BIT(6)
> +#define PWM_MODE BIT(7)
> +#define PWM_PULSE BIT(8)
> +#define PWM_BYPASS BIT(9)
> +
> +#define PWM_RDY_BASE 28
> +#define PWM_RDY_OFF 1
> +#define PWM_RDY(x) BIT(PWM_RDY_BASE + PWM_RDY_OFF * (x))
Some comments as for PWM_CH_PRD above.
> +#define PWM_PRD_ACT_MASK GENMASK(7, 0)
This seems to be unused.
> +#define PWM_PRD(x) ((x - 1) << 16)
x should be enclosed in ().
> +#define PWM_PRD_MASK GENMASK(7, 0)
> +
> +#define BIT_CH(bit, chan) (bit << (chan * PWMCH_OFFSET))
There should be a space instead of a tab between "define" and
"BIT_CH(...)".
> +u32 prescal_table[] = { 120, 180, 240, 360, 480, 0, 0, 0,
> + 12000, 24000, 36000, 48000, 72000,
> + 0, 0, 1 };
static const? Also this is oddly formatted. I'd prefer:
static const u32 prescal_table[] = {
...
...
};
Also for readability I'd go for "prescaler_table" or "prescale_table".
> +struct sunxi_pwm_data {
> + bool has_rdy;
> +};
> +
> +struct sunxi_pwm_chip {
> + struct pwm_chip chip;
> + struct clk *clk;
> + void __iomem *base;
> + struct mutex ctrl_lock;
> + const struct sunxi_pwm_data *data;
> +};
> +
> +#define to_sunxi_pwm_chip(chip) container_of(chip, struct sunxi_pwm_chip, chip)
This should be a static inline function.
> +static inline u32 sunxi_pwm_readl(struct sunxi_pwm_chip *chip,
> + unsigned long offset)
> +{
> + return readl(chip->base + offset);
> +}
> +
> +static inline void sunxi_pwm_writel(struct sunxi_pwm_chip *chip,
> + unsigned long offset, unsigned long val)
Make val u32 for consistency with sunxi_pwm_readl()? Also I'd prefer if
this function preserved the same parameter order as writel().
> +static int sunxi_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
> + int duty_ns, int period_ns)
> +{
> + struct sunxi_pwm_chip *sunxi_pwm = to_sunxi_pwm_chip(chip);
> + u32 clk_rate, prd, dty;
> + u64 div;
> + u32 val, clk_gate;
These can both go onto the same line as the other u32s above.
> + int i, ret;
I think i should be unsigned. And maybe rename ret to err since it's not
used as a return value at all.
> +
> + clk_rate = clk_get_rate(sunxi_pwm->clk);
> +
> + /* First, test without any divider */
> + i = PWM_PRESCAL_MASK;
I don't see where this value of I is used.
> + div = clk_rate * period_ns;
> + do_div(div, 1000000000);
> + if (div > PWM_PRD_MASK) {
> + /* Then go up from the first divider */
> + for (i = 0; i < PWM_PRESCAL_MASK; i++) {
> + if (!prescal_table[i])
> + continue;
> + div = clk_rate / prescal_table[i];
> + div = div * period_ns;
> + do_div(div, 1000000000);
Maybe:
div = clk_rate / prescal_table[i];
do_div(div * period_ns, 1000000000);
? Also 1000000000 == NSEC_PER_SEC, so maybe use that instead.
> + if (div <= PWM_PRD_MASK)
> + break;
> + }
> + }
> +
> + if (div > PWM_PRD_MASK) {
> + dev_err(chip->dev, "prescaler exceeds the maximum value\n");
Nit: the prescaler doesn't exceed anything. Rather the period exceeds
the maximum.
> + return -EINVAL;
> + }
> +
> + prd = div;
> + div *= duty_ns;
> + do_div(div, period_ns);
> + dty = div;
> +
> + ret = clk_prepare_enable(sunxi_pwm->clk);
> + if (ret) {
> + dev_err(chip->dev, "failed to enable PWM clock\n");
> + return ret;
> + }
> +
> + mutex_lock(&sunxi_pwm->ctrl_lock);
> + val = sunxi_pwm_readl(sunxi_pwm, PWM_CTRL_REG);
> +
> + if (sunxi_pwm->data->has_rdy && (val & PWM_RDY(pwm->hwpwm))) {
> + mutex_unlock(&sunxi_pwm->ctrl_lock);
> + clk_disable_unprepare(sunxi_pwm->clk);
> + return -EBUSY;
> + }
> +
> + clk_gate = val & BIT_CH(PWM_CLK_GATING, pwm->hwpwm);
> + if (clk_gate) {
> + val &= ~BIT_CH(PWM_CLK_GATING, pwm->hwpwm);
> + sunxi_pwm_writel(sunxi_pwm, PWM_CTRL_REG, val);
> + }
> +
> + val = sunxi_pwm_readl(sunxi_pwm, PWM_CTRL_REG);
> + val &= ~BIT_CH(PWM_PRESCAL_MASK, pwm->hwpwm);
> + val |= i;
Ah, this is where the initial value is used. Perhaps rename i to
prescaler to make it more obvious what it is.
> + sunxi_pwm_writel(sunxi_pwm, PWM_CTRL_REG, val);
> +
> + sunxi_pwm_writel(sunxi_pwm, PWM_CH_PRD(pwm->hwpwm), dty | PWM_PRD(prd));
Maybe split this into two lines for readability:
val = dty | PWM_PRD(prd);
sunxi_pwm_writel(...);
Also does dty need to be range-checked so it doesn't spill over into the
PRD field?
> +static int sunxi_pwm_set_polarity(struct pwm_chip *chip, struct pwm_device *pwm,
> + enum pwm_polarity polarity)
> +{
> + struct sunxi_pwm_chip *sunxi_pwm = to_sunxi_pwm_chip(chip);
> + u32 val;
> + int ret;
> +
> + ret = clk_prepare_enable(sunxi_pwm->clk);
> + if (ret) {
> + dev_err(chip->dev, "failed to enable PWM clock\n");
> + return ret;
> + }
> +
> + mutex_lock(&sunxi_pwm->ctrl_lock);
> + val = sunxi_pwm_readl(sunxi_pwm, PWM_CTRL_REG);
> +
> + if (polarity != PWM_POLARITY_NORMAL)
> + val &= ~BIT_CH(PWM_ACT_STATE, pwm->hwpwm);
> + else
> + val |= BIT_CH(PWM_ACT_STATE, pwm->hwpwm);
> +
> +
There's an extra blank line here.
> +static int sunxi_pwm_probe(struct platform_device *pdev)
> +{
> + struct sunxi_pwm_chip *sunxi_pwm;
Perhaps just "pwm" or "sunxi"? sunxi_pwm is kind of redundant.
> + struct resource *res;
> + int ret;
> +
> + const struct of_device_id *match;
There should be no blank line above this one.
> +
> + match = of_match_device(sunxi_pwm_dt_ids, &pdev->dev);
> + if (!match || !match->data)
None of this can ever happen. match would only be NULL if the table
doesn't contain a matching entry for this device. But in that case the
driver's .probe() wouldn't have been called anyway. And none of the
entries have a NULL .data field, so no need to check for that either.
> + return -ENODEV;
> +
> + sunxi_pwm = devm_kzalloc(&pdev->dev, sizeof(*sunxi_pwm), GFP_KERNEL);
> + if (!sunxi_pwm)
> + return -ENOMEM;
> +
> + res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
> + sunxi_pwm->base = devm_ioremap_resource(&pdev->dev, res);
> + if (IS_ERR(sunxi_pwm->base))
> + return PTR_ERR(sunxi_pwm->base);
> +
> + sunxi_pwm->clk = devm_clk_get(&pdev->dev, NULL);
> + if (IS_ERR(sunxi_pwm->clk))
> + return PTR_ERR(sunxi_pwm->clk);
> +
> + sunxi_pwm->chip.dev = &pdev->dev;
> + sunxi_pwm->chip.ops = &sunxi_pwm_ops;
> +
> + sunxi_pwm->chip.base = -1;
Why the blank line between the above?
> + sunxi_pwm->chip.npwm = 2;
> + sunxi_pwm->chip.can_sleep = true;
> + sunxi_pwm->chip.of_xlate = of_pwm_xlate_with_flags;
> + sunxi_pwm->chip.of_pwm_n_cells = 3;
> + sunxi_pwm->data = match->data;
> +
> + mutex_init(&sunxi_pwm->ctrl_lock);
> +
> + ret = clk_prepare_enable(sunxi_pwm->clk);
> + if (ret) {
> + dev_err(&pdev->dev, "failed to enable PWM clock\n");
> + goto error;
> + }
> +
> + /* By default, the polarity is inversed, set it to normal */
> + sunxi_pwm_writel(sunxi_pwm, PWM_CTRL_REG,
> + BIT_CH(PWM_ACT_STATE, 0) |
> + BIT_CH(PWM_ACT_STATE, 1));
> + clk_disable_unprepare(sunxi_pwm->clk);
Why do you need to do this here? Doesn't this potentially cause
transients if a bootloader had this configured with inversed polarity?
Thierry
[-- Attachment #2: Type: application/pgp-signature, Size: 836 bytes --]
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCHv5 2/2] pwm: sunxi: document OF bindings
2014-05-19 18:10 ` [PATCHv5 2/2] pwm: sunxi: document OF bindings Alexandre Belloni
@ 2014-06-17 23:29 ` Thierry Reding
0 siblings, 0 replies; 8+ messages in thread
From: Thierry Reding @ 2014-06-17 23:29 UTC (permalink / raw)
To: Alexandre Belloni
Cc: Maxime Ripard, linux-pwm, linux-doc, linux-arm-kernel,
linux-kernel
[-- Attachment #1: Type: text/plain, Size: 773 bytes --]
On Mon, May 19, 2014 at 08:10:03PM +0200, Alexandre Belloni wrote:
> This is the documentation for the Allwinner Socs PWM bindings.
"SoCs".
> diff --git a/Documentation/devicetree/bindings/pwm/pwm-sunxi.txt b/Documentation/devicetree/bindings/pwm/pwm-sunxi.txt
[...]
> +Allwinner PWM controller
"Allwinner SoC"?
> +
> +Required properties:
> + - compatible: should be one of:
> + - "allwinner,sun4i-a10-pwm"
> + - "allwinner,sun7i-a20-pwm"
> + - reg: physical base address and length of the controller's registers
> + - #pwm-cells: should be 3. See pwm.txt in this directory for a description of
> + the cells format.
> + - clocks: from common clock binding, handle to the parent clock.
This is a sentence, so should start with a capital letter.
Thierry
[-- Attachment #2: Type: application/pgp-signature, Size: 836 bytes --]
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCHv5 1/2] pwm: Add Allwinner SoC support
2014-06-17 23:26 ` Thierry Reding
@ 2014-06-23 17:01 ` Alexandre Belloni
2014-08-17 17:03 ` jonsmirl
0 siblings, 1 reply; 8+ messages in thread
From: Alexandre Belloni @ 2014-06-23 17:01 UTC (permalink / raw)
To: Thierry Reding
Cc: Maxime Ripard, linux-pwm, linux-doc, linux-arm-kernel,
linux-kernel
On 18/06/2014 at 01:26:06 +0200, Thierry Reding wrote :
> On Mon, May 19, 2014 at 08:10:02PM +0200, Alexandre Belloni wrote:
> > + /* By default, the polarity is inversed, set it to normal */
> > + sunxi_pwm_writel(sunxi_pwm, PWM_CTRL_REG,
> > + BIT_CH(PWM_ACT_STATE, 0) |
> > + BIT_CH(PWM_ACT_STATE, 1));
> > + clk_disable_unprepare(sunxi_pwm->clk);
>
> Why do you need to do this here? Doesn't this potentially cause
> transients if a bootloader had this configured with inversed polarity?
It was done a few months ago but what I remember is the following
happens:
The PWM subsystem assumes that the polarity is PWM_POLARITY_NORMAL
because of the kzalloc pwmchip_add(). Would you prefer something like:
val = sunxi_pwm_readl(sunxi_pwm, PWM_CTRL_REG);
for (i = 0; i < sunxi_pwm->chip.npwm; i++) {
if (!(val & BIT_CH(PWM_ACT_STATE, i)))
sunxi_pwm->chip.pwms[i].polarity = PWM_POLARITY_INVERSED;
}
Then, you would have a race where the PWM polarity is not correct in
sysfs between pwmchip_add() and that code.
Also, if you want to preserve the state set by the bootloader, you
actually have an issue with getting back the other members of the
pwm_device struct (duty, period) and more importantly the PWMF_ENABLED
flag. It now assumed that the PWM channel is not enabled when
registering the chip. If you now say that it may be enabled before linux
is booting and you want to keep it running, then you have an
inconsistency between the real state of the PWM (enabled, with a duty,
period and polarity set) and what the PWM susbsytem actually knows about
the PWM (not enabled, duty and period == 0 and polarity is normal).
I would agree that the usual use case would be that another driver will
take the PWM and set the duty, period and polarity anyway but the issue
with the PWMF_ENABLED flag remains.
How do you want to fix this? Would you add a new callback that would be
called by pwmchip_add(), before pwmchip_sysfs_export()?
I actually find it ugly to set the pwm_device members from the probe,
especially the flags. I would prefer they stay hidden by the API.
--
Alexandre Belloni, Free Electrons
Embedded Linux, Kernel and Android engineering
http://free-electrons.com
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCHv5 1/2] pwm: Add Allwinner SoC support
2014-06-23 17:01 ` Alexandre Belloni
@ 2014-08-17 17:03 ` jonsmirl
2014-08-17 20:20 ` jonsmirl-Re5JQEeQqe8AvxtiuMwx3w
0 siblings, 1 reply; 8+ messages in thread
From: jonsmirl @ 2014-08-17 17:03 UTC (permalink / raw)
To: Alexandre Belloni
Cc: Thierry Reding, Maxime Ripard, linux-pwm, linux-doc,
ARM Linux Mailing List, lkml, Chen-Yu Tsai
[-- Attachment #1: Type: text/plain, Size: 3151 bytes --]
I was unaware of this driver and reimplemented it. Patch is attached.
Some differences I noticed...
1) I implemented bypass mode
2) I had to do math in picoseconds to avoid round off/truncation errors.
3) counter registers are different lengths on SUN4 vs rest.
Can someone with a scope verify if the prescaler of 1 works at high
frequencies? Like cycles/active of 2/1, 3/1, 4,1...
On Mon, Jun 23, 2014 at 1:01 PM, Alexandre Belloni
<alexandre.belloni@free-electrons.com> wrote:
> On 18/06/2014 at 01:26:06 +0200, Thierry Reding wrote :
>> On Mon, May 19, 2014 at 08:10:02PM +0200, Alexandre Belloni wrote:
>> > + /* By default, the polarity is inversed, set it to normal */
>> > + sunxi_pwm_writel(sunxi_pwm, PWM_CTRL_REG,
>> > + BIT_CH(PWM_ACT_STATE, 0) |
>> > + BIT_CH(PWM_ACT_STATE, 1));
>> > + clk_disable_unprepare(sunxi_pwm->clk);
>>
>> Why do you need to do this here? Doesn't this potentially cause
>> transients if a bootloader had this configured with inversed polarity?
>
>
> It was done a few months ago but what I remember is the following
> happens:
>
> The PWM subsystem assumes that the polarity is PWM_POLARITY_NORMAL
> because of the kzalloc pwmchip_add(). Would you prefer something like:
>
> val = sunxi_pwm_readl(sunxi_pwm, PWM_CTRL_REG);
> for (i = 0; i < sunxi_pwm->chip.npwm; i++) {
> if (!(val & BIT_CH(PWM_ACT_STATE, i)))
> sunxi_pwm->chip.pwms[i].polarity = PWM_POLARITY_INVERSED;
> }
>
> Then, you would have a race where the PWM polarity is not correct in
> sysfs between pwmchip_add() and that code.
>
> Also, if you want to preserve the state set by the bootloader, you
> actually have an issue with getting back the other members of the
> pwm_device struct (duty, period) and more importantly the PWMF_ENABLED
> flag. It now assumed that the PWM channel is not enabled when
> registering the chip. If you now say that it may be enabled before linux
> is booting and you want to keep it running, then you have an
> inconsistency between the real state of the PWM (enabled, with a duty,
> period and polarity set) and what the PWM susbsytem actually knows about
> the PWM (not enabled, duty and period == 0 and polarity is normal).
>
> I would agree that the usual use case would be that another driver will
> take the PWM and set the duty, period and polarity anyway but the issue
> with the PWMF_ENABLED flag remains.
>
> How do you want to fix this? Would you add a new callback that would be
> called by pwmchip_add(), before pwmchip_sysfs_export()?
>
> I actually find it ugly to set the pwm_device members from the probe,
> especially the flags. I would prefer they stay hidden by the API.
>
>
> --
> Alexandre Belloni, Free Electrons
> Embedded Linux, Kernel and Android engineering
> http://free-electrons.com
> --
> To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at http://vger.kernel.org/majordomo-info.html
> Please read the FAQ at http://www.tux.org/lkml/
--
Jon Smirl
jonsmirl@gmail.com
[-- Attachment #2: pwm --]
[-- Type: application/octet-stream, Size: 18099 bytes --]
Add a PWM driver for Allwinner
From: Jon Smirl <jonsmirl@gmail.com>
---
drivers/pwm/Kconfig | 11 +
drivers/pwm/Makefile | 1
drivers/pwm/pwm-sunxi.c | 521 +++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 533 insertions(+)
create mode 100644 drivers/pwm/pwm-sunxi.c
diff --git a/drivers/pwm/Kconfig b/drivers/pwm/Kconfig
index 331dfca4..01f9fb2 100644
--- a/drivers/pwm/Kconfig
+++ b/drivers/pwm/Kconfig
@@ -226,6 +226,17 @@ config PWM_SPEAR
To compile this driver as a module, choose M here: the module
will be called pwm-spear.
+config PWM_SUNXI
+ tristate "Sunxi PWM Driver (pwm-sunxi)"
+ depends on ARCH_SUNXI
+ help
+ Say Y here if you want Hardware PWM Support
+
+ To compile this driver as a module, choose M here: the
+ module will be called pwm-sunxi. This driver supports
+ a sysfs interface at /sys/class/pwm-sunxi as well as the
+ kernel pwm interface.
+
config PWM_TEGRA
tristate "NVIDIA Tegra PWM support"
depends on ARCH_TEGRA
diff --git a/drivers/pwm/Makefile b/drivers/pwm/Makefile
index 5c86a19..c32f827 100644
--- a/drivers/pwm/Makefile
+++ b/drivers/pwm/Makefile
@@ -20,6 +20,7 @@ obj-$(CONFIG_PWM_PXA) += pwm-pxa.o
obj-$(CONFIG_PWM_RENESAS_TPU) += pwm-renesas-tpu.o
obj-$(CONFIG_PWM_SAMSUNG) += pwm-samsung.o
obj-$(CONFIG_PWM_SPEAR) += pwm-spear.o
+obj-$(CONFIG_PWM_SUNXI) += pwm-sunxi.o
obj-$(CONFIG_PWM_TEGRA) += pwm-tegra.o
obj-$(CONFIG_PWM_TIECAP) += pwm-tiecap.o
obj-$(CONFIG_PWM_TIEHRPWM) += pwm-tiehrpwm.o
diff --git a/drivers/pwm/pwm-sunxi.c b/drivers/pwm/pwm-sunxi.c
new file mode 100644
index 0000000..bc89a7c
--- /dev/null
+++ b/drivers/pwm/pwm-sunxi.c
@@ -0,0 +1,521 @@
+/* pwm-sunxi.c
+ *
+ * pwm module for sun4i (and others) like cubieboard and pcduino
+ *
+ * (C) Copyright 2013
+ * David H. Wilkins <dwil...@conecuh.com>
+ * (C) Copyright 2014
+ * Jon Smirl <jonsmirl@gmail.com>
+ *
+ * CHANGELOG:
+ * 8.15.2014 - Jon Smirl
+ * - Total rewrite for mainline inclusion
+ * 10.08.2013 - Stefan Voit <stefan.voit@voit-consulting.com>
+ * - Removed bug that caused the PWM to pause quickly when changing parameters
+ * - Dropped debug/dump functions
+ *
+ * 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; either version 2 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
+ * MA 02111-1307 USA
+ */
+
+#include <linux/module.h>
+#include <linux/of_platform.h>
+#include <linux/platform_device.h>
+#include <linux/err.h>
+#include <linux/pwm.h>
+#include <linux/clk.h>
+#include <linux/clk-provider.h>
+#include <linux/regmap.h>
+#include <linux/delay.h>
+
+/*------------------------------------------------------------*/
+/* REGISTER definitions */
+
+#define SUNXI_PWM_CTRL_REG 0x00 /* PWM Control Register */
+#define SUNXI_PWM_CH0_PERIOD 0x04 /* PWM Channel 0 Period Register */
+#define SUNXI_PWM_CH1_PERIOD 0x08 /* PWM Channel 1 Period Register */
+
+#define SUNXI_PWM_CHANNEL_MAX 2
+
+/* SUNXI_PWM_CTRL_REG 0x00 PWM Control Register */
+#define SUNXI_PWMCTL_PWM1_NOTRDY (1<<29)
+#define SUNXI_PWMCTL_PWM1_RDY_MASK (1<<29)
+#define SUNXI_PWMCTL_PWM1_RDY_SHIFT 29
+#define SUNXI_PWMCTL_PWM1_RDY_WIDTH 1
+#define SUNXI_PWMCTL_PWM0_NOTRDY (1<<28)
+#define SUNXI_PWMCTL_PWM0_RDY_MASK (1<<28)
+#define SUNXI_PWMCTL_PWM0_RDY_SHIFT 28
+#define SUNXI_PWMCTL_PWM0_RDY_WIDTH 1
+#define SUNXI_PWMCTL_PWM1_BYPASS (1<<24)
+#define SUNXI_PWMCTL_PWM1_BYPASS_MASK (1<<24)
+#define SUNXI_PWMCTL_PWM1_BYPASS_SHIFT 24
+#define SUNXI_PWMCTL_PWM1_BYPASS_WIDTH 1
+#define SUNXI_PWMCTL_PWM1_START (1<<23)
+#define SUNXI_PWMCTL_PWM1_START_MASK (1<<23)
+#define SUNXI_PWMCTL_PWM1_START_SHIFT 23
+#define SUNXI_PWMCTL_PWM1_START_WIDTH 1
+#define SUNXI_PWMCTL_PWM1_MODE (1<<22)
+#define SUNXI_PWMCTL_PWM1_MODE_MASK (1<<22)
+#define SUNXI_PWMCTL_PWM1_MODE_SHIFT 22
+#define SUNXI_PWMCTL_PWM1_MODE_WIDTH 1
+#define SUNXI_PWMCTL_PWM1_GATE (1<<21)
+#define SUNXI_PWMCTL_PWM1_GATE_MASK (1<<21)
+#define SUNXI_PWMCTL_PWM1_GATE_SHIFT 21
+#define SUNXI_PWMCTL_PWM1_GATE_WIDTH 1
+#define SUNXI_PWMCTL_PWM1_STATE (1<<20)
+#define SUNXI_PWMCTL_PWM1_STATE_MASK (1<<20)
+#define SUNXI_PWMCTL_PWM1_STATE_SHIFT 20
+#define SUNXI_PWMCTL_PWM1_STATE_WIDTH 1
+#define SUNXI_PWMCTL_PWM1_EN (1<<19)
+#define SUNXI_PWMCTL_PWM1_EN_MASK (1<<19)
+#define SUNXI_PWMCTL_PWM1_EN_SHIFT 19
+#define SUNXI_PWMCTL_PWM1_EN_WIDTH 1
+#define SUNXI_PWMCTL_PWM1_PRE_MASK (0xf<<15)
+#define SUNXI_PWMCTL_PWM1_PRE_120 (0<<15)
+#define SUNXI_PWMCTL_PWM1_PRE_180 (1<<15)
+#define SUNXI_PWMCTL_PWM1_PRE_240 (2<<15)
+#define SUNXI_PWMCTL_PWM1_PRE_360 (3<<15)
+#define SUNXI_PWMCTL_PWM1_PRE_480 (4<<15)
+#define SUNXI_PWMCTL_PWM1_PRE_12K (8<<15)
+#define SUNXI_PWMCTL_PWM1_PRE_24K (9<<15)
+#define SUNXI_PWMCTL_PWM1_PRE_36K (0xa<<15)
+#define SUNXI_PWMCTL_PWM1_PRE_48K (0xb<<15)
+#define SUNXI_PWMCTL_PWM1_PRE_72K (0xc<<15)
+#define SUNXI_PWMCTL_PWM1_PRE_1 (0xf<<15)
+#define SUNXI_PWMCTL_PWM1_PRE_SHIFT 15
+#define SUNXI_PWMCTL_PWM1_PRE_WIDTH 4
+#define SUNXI_PWMCTL_PWM0_BYPASS (1<<9)
+#define SUNXI_PWMCTL_PWM0_BYPASS_MASK (1<<9)
+#define SUNXI_PWMCTL_PWM0_BYPASS_SHIFT 9
+#define SUNXI_PWMCTL_PWM0_BYPASS_WIDTH 1
+#define SUNXI_PWMCTL_PWM0_START (1<<8)
+#define SUNXI_PWMCTL_PWM0_START_MASK (1<<8)
+#define SUNXI_PWMCTL_PWM0_START_SHIFT 8
+#define SUNXI_PWMCTL_PWM0_START_WIDTH 1
+#define SUNXI_PWMCTL_PWM0_MODE (1<<7)
+#define SUNXI_PWMCTL_PWM0_MODE_MASK (1<<7)
+#define SUNXI_PWMCTL_PWM0_MODE_SHIFT 7
+#define SUNXI_PWMCTL_PWM0_MODE_WIDTH 1
+#define SUNXI_PWMCTL_PWM0_GATE (1<<6)
+#define SUNXI_PWMCTL_PWM0_GATE_MASK (1<<6)
+#define SUNXI_PWMCTL_PWM0_GATE_SHIFT 6
+#define SUNXI_PWMCTL_PWM0_GATE_WIDTH 1
+#define SUNXI_PWMCTL_PWM0_STATE (1<<5)
+#define SUNXI_PWMCTL_PWM0_STATE_MASK (1<<5)
+#define SUNXI_PWMCTL_PWM0_STATE_SHIFT 5
+#define SUNXI_PWMCTL_PWM0_STATE_WIDTH 1
+#define SUNXI_PWMCTL_PWM0_EN (1<<4)
+#define SUNXI_PWMCTL_PWM0_EN_MASK (1<<4)
+#define SUNXI_PWMCTL_PWM0_EN_SHIFT 4
+#define SUNXI_PWMCTL_PWM0_EN_WIDTH 1
+#define SUNXI_PWMCTL_PWM0_PRE_MASK (0xf<<0)
+#define SUNXI_PWMCTL_PWM0_PRE_120 (0<<0)
+#define SUNXI_PWMCTL_PWM0_PRE_180 (1<<0)
+#define SUNXI_PWMCTL_PWM0_PRE_240 (2<<0)
+#define SUNXI_PWMCTL_PWM0_PRE_360 (3<<0)
+#define SUNXI_PWMCTL_PWM0_PRE_480 (4<<0)
+#define SUNXI_PWMCTL_PWM0_PRE_12K (8<<0)
+#define SUNXI_PWMCTL_PWM0_PRE_24K (9<<0)
+#define SUNXI_PWMCTL_PWM0_PRE_36K (0xa<<0)
+#define SUNXI_PWMCTL_PWM0_PRE_48K (0xb<<0)
+#define SUNXI_PWMCTL_PWM0_PRE_72K (0xc<<0)
+#define SUNXI_PWMCTL_PWM0_PRE_1 (0xf<<0)
+#define SUNXI_PWMCTL_PWM0_PRE_SHIFT 0
+#define SUNXI_PWMCTL_PWM0_PRE_WIDTH 4
+
+/* SUNXI_PWM_CH0_PERIOD 0x04 PWM Channel 0 Period Register */
+/* SUNXI_PWM_CH1_PERIOD 0x08 PWM Channel 1 Period Register */
+#define SUNXI_PWM_CYCLES_TOTAL_MASK (0xFFFF<<16)
+#define SUNXI_PWM_CYCLES_TOTAL_SHIFT 16
+#define SUNXI_PWM_CYCLES_TOTAL_WIDTH 16
+#define SUNXI_PWM_CYCLES_ACTIVE_MASK (0xFFFF<<0)
+#define SUNXI_PWM_CYCLES_ACTIVE_SHIFT 0
+#define SUNXI_PWM_CYCLES_ACTIVE_WIDTH 16
+
+#define MAX_CYCLES_SUN4I 0x0ffL /* max cycle count possible for period active and entire */
+#define MAX_CYCLES 0x0ffffL /* max cycle count possible for period active and entire */
+#define OSC24 24L /* 24Mhz system oscillator */
+
+/* Supported SoC families - used for quirks */
+enum sunxi_soc_family {
+ SUN4I, /* A10 SoC - later revisions */
+ SUN5I, /* A10S/A13 SoCs */
+ SUN7I, /* A20 SoC */
+};
+
+/*
+ * structure that defines the pwm control register
+ */
+
+static const unsigned int prescale_divisor[] = {
+ 120, 180, 240, 360, 480, 480, 480, 480,
+ 12000, 24000, 36000, 48000, 72000, 72000, 72000, 1
+};
+
+struct sunxi_pwm_chip {
+ struct pwm_chip chip;
+ struct clk *clk;
+ struct regmap *regmap;
+ struct mutex lock;
+ enum sunxi_soc_family revision;
+ unsigned long max_cycles;
+};
+
+static inline struct sunxi_pwm_chip *to_sunxi_chip(struct pwm_chip *chip)
+{
+ return container_of(chip, struct sunxi_pwm_chip, chip);
+}
+
+/*
+ * Find the best prescale value for the period
+ * We want to get the highest period cycle count possible, so we look
+ * make a run through the prescale values looking for numbers over
+ * min_optimal_period_cycles. If none are found then root though again
+ * taking anything that works
+ */
+int pwm_get_best_prescale(struct sunxi_pwm_chip *priv, int period_in)
+{
+ int i;
+ unsigned long int clk_pico, period, min_optimal_period_cycles;
+ const unsigned long min_period_cycles = 0x02;
+ int best_prescale = 0;
+
+ period = period_in * 1000; /* convert to picoseconds */
+ min_optimal_period_cycles = priv->max_cycles / 2;
+
+ best_prescale = -1;
+ for(i = 0 ; i < ARRAY_SIZE(prescale_divisor) ; i++) {
+
+ clk_pico = 1000000L * prescale_divisor[i] / OSC24;
+ if(clk_pico < 1 || clk_pico > period) {
+ continue;
+ }
+ if(((period / clk_pico) >= min_optimal_period_cycles) &&
+ ((period / clk_pico) <= priv->max_cycles)) {
+ best_prescale = i;
+ }
+ }
+
+ if(best_prescale > ARRAY_SIZE(prescale_divisor)) {
+ for(i = 0 ; i < ARRAY_SIZE(prescale_divisor) ; i++) {
+ clk_pico = 1000000L * prescale_divisor[i] / OSC24;
+ if(clk_pico < 1 || clk_pico > period) {
+ continue;
+ }
+ if(((period / clk_pico) >= min_period_cycles) &&
+ ((period / clk_pico) <= priv->max_cycles)) {
+ best_prescale = i;
+ }
+ }
+ }
+
+ if(best_prescale > ARRAY_SIZE(prescale_divisor))
+ return -EINVAL;
+
+ dev_dbg(priv->chip.dev, "Best prescale is %d\n", best_prescale);
+ return best_prescale;
+}
+
+/*
+ * return the number of cycles for the channel period computed from the nanoseconds
+ * for the period. Allwinner docs call this "entire" cycles
+ */
+unsigned int compute_cycles(struct sunxi_pwm_chip *priv, int prescale, int period)
+{
+ unsigned long int clk_pico, cycles;
+
+ clk_pico = 1000000L * prescale_divisor[prescale] / OSC24;
+ cycles = DIV_ROUND_CLOSEST(period * 1000L, clk_pico);
+ if (cycles > priv->max_cycles)
+ cycles = priv->max_cycles;
+ if (cycles < 2)
+ cycles = 2;
+
+ dev_dbg(priv->chip.dev, "Best prescale was %d, cycles is %lu\n", prescale, cycles);
+
+ return cycles;
+}
+
+static int sunxi_pwm_busy(struct sunxi_pwm_chip *priv)
+{
+ int i, reg_val;
+
+ for (i = 0; i < 50; i++) {
+ regmap_read(priv->regmap, SUNXI_PWM_CTRL_REG, ®_val);
+ if ((reg_val & (SUNXI_PWMCTL_PWM1_NOTRDY | SUNXI_PWMCTL_PWM0_NOTRDY)) == 0)
+ return 0;
+ mdelay(1);
+ }
+ dev_dbg(priv->chip.dev, "PWM busy timeout\n");
+ return -EBUSY;
+}
+
+
+static int sunxi_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
+ int duty_ns, int period_ns)
+{
+ struct sunxi_pwm_chip *priv = to_sunxi_chip(chip);
+ int prescale, entire_cycles, active_cycles, ret = 0;
+ unsigned int reg_val;
+
+
+ if ((duty_ns <= 0) || (period_ns <= 0))
+ return 0;
+
+ mutex_lock(&priv->lock);
+
+ // If period less than two cycles, just enable the OSC24 clock bypass
+ if (period_ns < (2 * 1000 / OSC24 + 1)) {
+ switch (pwm->hwpwm) {
+ case 0:
+ regmap_update_bits(priv->regmap, SUNXI_PWM_CTRL_REG,
+ SUNXI_PWMCTL_PWM0_BYPASS_MASK, SUNXI_PWMCTL_PWM0_BYPASS);
+ break;
+ case 1:
+ regmap_update_bits(priv->regmap, SUNXI_PWM_CTRL_REG,
+ SUNXI_PWMCTL_PWM1_BYPASS_MASK, SUNXI_PWMCTL_PWM1_BYPASS);
+ break;
+ }
+ goto exit;
+ }
+
+ prescale = pwm_get_best_prescale(priv, period_ns);
+ if (prescale < 0) {
+ ret = prescale;
+ goto exit;
+ }
+ entire_cycles = compute_cycles(priv, prescale, period_ns);
+ active_cycles = compute_cycles(priv, prescale, duty_ns);
+
+ reg_val = (entire_cycles << SUNXI_PWM_CYCLES_TOTAL_SHIFT) & SUNXI_PWM_CYCLES_TOTAL_MASK;
+ reg_val = (active_cycles << SUNXI_PWM_CYCLES_ACTIVE_SHIFT) & SUNXI_PWM_CYCLES_ACTIVE_MASK;
+
+ switch (pwm->hwpwm) {
+ case 0:
+ regmap_update_bits(priv->regmap, SUNXI_PWM_CTRL_REG,
+ SUNXI_PWMCTL_PWM0_BYPASS_MASK | SUNXI_PWMCTL_PWM0_PRE_MASK | SUNXI_PWMCTL_PWM0_EN_MASK,
+ prescale << SUNXI_PWMCTL_PWM0_PRE_SHIFT | SUNXI_PWMCTL_PWM0_EN);
+ regmap_write(priv->regmap, SUNXI_PWM_CH0_PERIOD, reg_val);
+ break;
+ case 1:
+ regmap_update_bits(priv->regmap, SUNXI_PWM_CTRL_REG,
+ SUNXI_PWMCTL_PWM1_BYPASS_MASK | SUNXI_PWMCTL_PWM1_PRE_MASK | SUNXI_PWMCTL_PWM1_EN_MASK,
+ prescale << SUNXI_PWMCTL_PWM1_PRE_SHIFT | SUNXI_PWMCTL_PWM1_EN);
+ regmap_write(priv->regmap, SUNXI_PWM_CH1_PERIOD, reg_val);
+ break;
+ default:
+ ret = -EINVAL;
+ }
+exit:
+ mutex_unlock(&priv->lock);
+ return ret;
+}
+
+static int sunxi_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
+{
+ struct sunxi_pwm_chip *priv = to_sunxi_chip(chip);
+ int ret = 0;
+
+ mutex_lock(&priv->lock);
+ switch (pwm->hwpwm) {
+ case 0:
+ regmap_update_bits(priv->regmap, SUNXI_PWM_CTRL_REG,
+ SUNXI_PWMCTL_PWM0_GATE_MASK | SUNXI_PWMCTL_PWM0_EN_MASK,
+ SUNXI_PWMCTL_PWM0_GATE | SUNXI_PWMCTL_PWM0_EN);
+ break;
+ case 1:
+ regmap_update_bits(priv->regmap, SUNXI_PWM_CTRL_REG,
+ SUNXI_PWMCTL_PWM1_GATE_MASK | SUNXI_PWMCTL_PWM1_EN_MASK,
+ SUNXI_PWMCTL_PWM1_GATE | SUNXI_PWMCTL_PWM1_EN);
+ break;
+ default:
+ ret = -EINVAL;
+ goto exit;
+ }
+ if ((priv->revision == SUN5I) || (priv->revision == SUN7I))
+ ret = sunxi_pwm_busy(priv);
+exit:
+ mutex_unlock(&priv->lock);
+ return ret;
+}
+
+static void sunxi_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
+{
+ struct sunxi_pwm_chip *priv = to_sunxi_chip(chip);
+
+ mutex_lock(&priv->lock);
+ switch (pwm->hwpwm) {
+ case 0:
+ regmap_update_bits(priv->regmap, SUNXI_PWM_CTRL_REG,
+ SUNXI_PWMCTL_PWM0_GATE_MASK | SUNXI_PWMCTL_PWM0_EN_MASK, 0);
+ break;
+ case 1:
+ regmap_update_bits(priv->regmap, SUNXI_PWM_CTRL_REG,
+ SUNXI_PWMCTL_PWM1_GATE_MASK | SUNXI_PWMCTL_PWM1_EN_MASK, 0);
+ break;
+ }
+ mutex_unlock(&priv->lock);
+ return;
+}
+
+static int sunxi_pwm_polarity(struct pwm_chip *chip, struct pwm_device *pwm,
+ enum pwm_polarity polarity)
+{
+ struct sunxi_pwm_chip *priv = to_sunxi_chip(chip);
+ int ret = 0;
+
+ mutex_lock(&priv->lock);
+ switch (pwm->hwpwm) {
+ case 0:
+ regmap_update_bits(priv->regmap, SUNXI_PWM_CTRL_REG,
+ SUNXI_PWMCTL_PWM0_STATE_MASK,
+ (polarity == PWM_POLARITY_INVERSED) << SUNXI_PWMCTL_PWM0_STATE_SHIFT);
+ break;
+ case 1:
+ regmap_update_bits(priv->regmap, SUNXI_PWM_CTRL_REG,
+ SUNXI_PWMCTL_PWM1_STATE_MASK,
+ (polarity == PWM_POLARITY_INVERSED) << SUNXI_PWMCTL_PWM1_STATE_SHIFT);
+ break;
+ default:
+ ret = -EINVAL;
+ }
+ mutex_unlock(&priv->lock);
+ return ret;
+}
+
+static const struct pwm_ops sunxi_pwm_ops = {
+ .config = sunxi_pwm_config,
+ .enable = sunxi_pwm_enable,
+ .disable = sunxi_pwm_disable,
+ .set_polarity = sunxi_pwm_polarity,
+ .owner = THIS_MODULE,
+};
+
+static const struct regmap_range sunxi_pwm_volatile_regs_range[] = {
+ regmap_reg_range(SUNXI_PWM_CTRL_REG, SUNXI_PWM_CTRL_REG),
+};
+
+static const struct regmap_access_table sunxi_pwm_volatile_regs = {
+ .yes_ranges = sunxi_pwm_volatile_regs_range,
+ .n_yes_ranges = ARRAY_SIZE(sunxi_pwm_volatile_regs_range),
+};
+
+static const struct regmap_config sunxi_pwm_regmap_config = {
+ .reg_bits = 32,
+ .reg_stride = 4,
+ .val_bits = 32,
+ .max_register = SUNXI_PWM_CH1_PERIOD,
+ .volatile_table = &sunxi_pwm_volatile_regs,
+};
+
+static const struct of_device_id sunxi_pwm_of_match[] = {
+ { .compatible = "allwinner,sun4i-a10-pwm", .data = (void *)SUN4I},
+ { .compatible = "allwinner,sun5i-a13-pwm", .data = (void *)SUN5I},
+ { .compatible = "allwinner,sun7i-a20-pwm", .data = (void *)SUN7I},
+ {}
+};
+MODULE_DEVICE_TABLE(of, sunxi_pwm_of_match);
+
+static int sunxi_pwm_probe(struct platform_device *pdev)
+{
+ const struct of_device_id *of_id;
+ void __iomem *base;
+ struct sunxi_pwm_chip *priv;
+ struct resource *res;
+ int ret;
+
+ of_id = of_match_device(sunxi_pwm_of_match, &pdev->dev);
+ if (!of_id)
+ return -EINVAL;
+
+ priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
+ if (!priv)
+ return -ENOMEM;
+
+ priv->chip.dev = &pdev->dev;
+ priv->revision = (enum sunxi_soc_family)of_id->data;
+ if (priv->revision == SUN4I)
+ priv->max_cycles = MAX_CYCLES_SUN4I;
+ else
+ priv->max_cycles = MAX_CYCLES;
+
+ res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+ base = devm_ioremap_resource(&pdev->dev, res);
+ if (IS_ERR(base))
+ return PTR_ERR(base);
+
+ priv->regmap = devm_regmap_init_mmio(&pdev->dev, base,
+ &sunxi_pwm_regmap_config);
+ if (IS_ERR(priv->regmap))
+ return PTR_ERR(priv->regmap);
+
+ priv->clk = devm_clk_get(&pdev->dev, NULL);
+ if (IS_ERR(priv->clk)) {
+ dev_err(&pdev->dev, "failed to get Osc24M clock\n");
+ return PTR_ERR(priv->clk);
+ }
+ ret = clk_prepare_enable(priv->clk);
+ if (ret) {
+ dev_err(&pdev->dev, "failed to enable Osc24M clock\n");
+ return ret;
+ }
+
+ mutex_init(&priv->lock);
+
+ priv->chip.ops = &sunxi_pwm_ops;
+ priv->chip.base = -1;
+ priv->chip.npwm = 2;
+ priv->chip.of_xlate = of_pwm_xlate_with_flags;
+ priv->chip.of_pwm_n_cells = 3;
+
+ ret = pwmchip_add(&priv->chip);
+ if (ret < 0) {
+ dev_err(&pdev->dev, "failed to add PWM chip: %d\n", ret);
+ goto error;
+ }
+
+ platform_set_drvdata(pdev, priv);
+ return 0;
+
+error:
+ mutex_destroy(&priv->lock);
+ clk_disable_unprepare(priv->clk);
+ return ret;
+}
+
+static int sunxi_pwm_remove(struct platform_device *pdev)
+{
+ struct sunxi_pwm_chip *priv = platform_get_drvdata(pdev);
+
+ mutex_destroy(&priv->lock);
+ clk_disable_unprepare(priv->clk);
+ return pwmchip_remove(&priv->chip);
+}
+
+static struct platform_driver sunxi_pwm_driver = {
+ .driver = {
+ .name = "sunxi-pwm",
+ .of_match_table = sunxi_pwm_of_match,
+ },
+ .probe = sunxi_pwm_probe,
+ .remove = sunxi_pwm_remove,
+};
+module_platform_driver(sunxi_pwm_driver);
+
+MODULE_DESCRIPTION("Allwinner PWM Driver");
+MODULE_ALIAS("platform:sunxi-pwm");
+MODULE_LICENSE("GPL");
+
+
^ permalink raw reply related [flat|nested] 8+ messages in thread
* Re: [PATCHv5 1/2] pwm: Add Allwinner SoC support
2014-08-17 17:03 ` jonsmirl
@ 2014-08-17 20:20 ` jonsmirl-Re5JQEeQqe8AvxtiuMwx3w
0 siblings, 0 replies; 8+ messages in thread
From: jonsmirl-Re5JQEeQqe8AvxtiuMwx3w @ 2014-08-17 20:20 UTC (permalink / raw)
To: Alexandre Belloni, linux-sunxi
Cc: Thierry Reding, Maxime Ripard, linux-pwm-u79uwXL29TY76Z2rM5mHXA,
linux-doc-u79uwXL29TY76Z2rM5mHXA, ARM Linux Mailing List, lkml,
Chen-Yu Tsai
[-- Attachment #1: Type: text/plain, Size: 537 bytes --]
New version that adds better support for the A10 plus it fixes an
issue in the previous version.
Note that the A10 does not support the 24Mhz bypass mode.
--
Jon Smirl
jonsmirl-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
--
You received this message because you are subscribed to the Google Groups "linux-sunxi" group.
To unsubscribe from this group and stop receiving emails from it, send an email to linux-sunxi+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit https://groups.google.com/d/optout.
[-- Attachment #2: pwm --]
[-- Type: application/octet-stream, Size: 18574 bytes --]
Add a PWM driver for Allwinner
From: Jon Smirl <jonsmirl@gmail.com>
---
drivers/pwm/Kconfig | 11 +
drivers/pwm/Makefile | 1
drivers/pwm/pwm-sunxi.c | 534 +++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 546 insertions(+)
create mode 100644 drivers/pwm/pwm-sunxi.c
diff --git a/drivers/pwm/Kconfig b/drivers/pwm/Kconfig
index 331dfca4..01f9fb2 100644
--- a/drivers/pwm/Kconfig
+++ b/drivers/pwm/Kconfig
@@ -226,6 +226,17 @@ config PWM_SPEAR
To compile this driver as a module, choose M here: the module
will be called pwm-spear.
+config PWM_SUNXI
+ tristate "Sunxi PWM Driver (pwm-sunxi)"
+ depends on ARCH_SUNXI
+ help
+ Say Y here if you want Hardware PWM Support
+
+ To compile this driver as a module, choose M here: the
+ module will be called pwm-sunxi. This driver supports
+ a sysfs interface at /sys/class/pwm-sunxi as well as the
+ kernel pwm interface.
+
config PWM_TEGRA
tristate "NVIDIA Tegra PWM support"
depends on ARCH_TEGRA
diff --git a/drivers/pwm/Makefile b/drivers/pwm/Makefile
index 5c86a19..c32f827 100644
--- a/drivers/pwm/Makefile
+++ b/drivers/pwm/Makefile
@@ -20,6 +20,7 @@ obj-$(CONFIG_PWM_PXA) += pwm-pxa.o
obj-$(CONFIG_PWM_RENESAS_TPU) += pwm-renesas-tpu.o
obj-$(CONFIG_PWM_SAMSUNG) += pwm-samsung.o
obj-$(CONFIG_PWM_SPEAR) += pwm-spear.o
+obj-$(CONFIG_PWM_SUNXI) += pwm-sunxi.o
obj-$(CONFIG_PWM_TEGRA) += pwm-tegra.o
obj-$(CONFIG_PWM_TIECAP) += pwm-tiecap.o
obj-$(CONFIG_PWM_TIEHRPWM) += pwm-tiehrpwm.o
diff --git a/drivers/pwm/pwm-sunxi.c b/drivers/pwm/pwm-sunxi.c
new file mode 100644
index 0000000..52f9c95
--- /dev/null
+++ b/drivers/pwm/pwm-sunxi.c
@@ -0,0 +1,534 @@
+/* pwm-sunxi.c
+ *
+ * pwm module for sun4i (and others) like cubieboard and pcduino
+ *
+ * (C) Copyright 2013
+ * David H. Wilkins <dwil...@conecuh.com>
+ * (C) Copyright 2014
+ * Jon Smirl <jonsmirl@gmail.com>
+ *
+ * CHANGELOG:
+ * 8.15.2014 - Jon Smirl
+ * - Total rewrite for mainline inclusion
+ * 10.08.2013 - Stefan Voit <stefan.voit@voit-consulting.com>
+ * - Removed bug that caused the PWM to pause quickly when changing parameters
+ * - Dropped debug/dump functions
+ *
+ * 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; either version 2 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
+ * MA 02111-1307 USA
+ */
+
+#include <linux/module.h>
+#include <linux/of_platform.h>
+#include <linux/platform_device.h>
+#include <linux/err.h>
+#include <linux/pwm.h>
+#include <linux/clk.h>
+#include <linux/clk-provider.h>
+#include <linux/regmap.h>
+#include <linux/delay.h>
+
+/*------------------------------------------------------------*/
+/* REGISTER definitions */
+
+#define SUNXI_PWM_CTRL_REG 0x00 /* PWM Control Register */
+#define SUNXI_PWM_CH0_PERIOD 0x04 /* PWM Channel 0 Period Register */
+#define SUNXI_PWM_CH1_PERIOD 0x08 /* PWM Channel 1 Period Register */
+
+#define SUNXI_PWM_CHANNEL_MAX 2
+
+/* SUNXI_PWM_CTRL_REG 0x00 PWM Control Register */
+#define SUNXI_PWMCTL_PWM1_NOTRDY (1<<29)
+#define SUNXI_PWMCTL_PWM1_RDY_MASK (1<<29)
+#define SUNXI_PWMCTL_PWM1_RDY_SHIFT 29
+#define SUNXI_PWMCTL_PWM1_RDY_WIDTH 1
+#define SUNXI_PWMCTL_PWM0_NOTRDY (1<<28)
+#define SUNXI_PWMCTL_PWM0_RDY_MASK (1<<28)
+#define SUNXI_PWMCTL_PWM0_RDY_SHIFT 28
+#define SUNXI_PWMCTL_PWM0_RDY_WIDTH 1
+#define SUNXI_PWMCTL_PWM1_BYPASS (1<<24)
+#define SUNXI_PWMCTL_PWM1_BYPASS_MASK (1<<24)
+#define SUNXI_PWMCTL_PWM1_BYPASS_SHIFT 24
+#define SUNXI_PWMCTL_PWM1_BYPASS_WIDTH 1
+#define SUNXI_PWMCTL_PWM1_START (1<<23)
+#define SUNXI_PWMCTL_PWM1_START_MASK (1<<23)
+#define SUNXI_PWMCTL_PWM1_START_SHIFT 23
+#define SUNXI_PWMCTL_PWM1_START_WIDTH 1
+#define SUNXI_PWMCTL_PWM1_MODE (1<<22)
+#define SUNXI_PWMCTL_PWM1_MODE_MASK (1<<22)
+#define SUNXI_PWMCTL_PWM1_MODE_SHIFT 22
+#define SUNXI_PWMCTL_PWM1_MODE_WIDTH 1
+#define SUNXI_PWMCTL_PWM1_GATE (1<<21)
+#define SUNXI_PWMCTL_PWM1_GATE_MASK (1<<21)
+#define SUNXI_PWMCTL_PWM1_GATE_SHIFT 21
+#define SUNXI_PWMCTL_PWM1_GATE_WIDTH 1
+#define SUNXI_PWMCTL_PWM1_STATE (1<<20)
+#define SUNXI_PWMCTL_PWM1_STATE_MASK (1<<20)
+#define SUNXI_PWMCTL_PWM1_STATE_SHIFT 20
+#define SUNXI_PWMCTL_PWM1_STATE_WIDTH 1
+#define SUNXI_PWMCTL_PWM1_EN (1<<19)
+#define SUNXI_PWMCTL_PWM1_EN_MASK (1<<19)
+#define SUNXI_PWMCTL_PWM1_EN_SHIFT 19
+#define SUNXI_PWMCTL_PWM1_EN_WIDTH 1
+#define SUNXI_PWMCTL_PWM1_PRE_MASK (0xf<<15)
+#define SUNXI_PWMCTL_PWM1_PRE_120 (0<<15)
+#define SUNXI_PWMCTL_PWM1_PRE_180 (1<<15)
+#define SUNXI_PWMCTL_PWM1_PRE_240 (2<<15)
+#define SUNXI_PWMCTL_PWM1_PRE_360 (3<<15)
+#define SUNXI_PWMCTL_PWM1_PRE_480 (4<<15)
+#define SUNXI_PWMCTL_PWM1_PRE_12K (8<<15)
+#define SUNXI_PWMCTL_PWM1_PRE_24K (9<<15)
+#define SUNXI_PWMCTL_PWM1_PRE_36K (0xa<<15)
+#define SUNXI_PWMCTL_PWM1_PRE_48K (0xb<<15)
+#define SUNXI_PWMCTL_PWM1_PRE_72K (0xc<<15)
+#define SUNXI_PWMCTL_PWM1_PRE_1 (0xf<<15)
+#define SUNXI_PWMCTL_PWM1_PRE_SHIFT 15
+#define SUNXI_PWMCTL_PWM1_PRE_WIDTH 4
+#define SUNXI_PWMCTL_PWM0_BYPASS (1<<9)
+#define SUNXI_PWMCTL_PWM0_BYPASS_MASK (1<<9)
+#define SUNXI_PWMCTL_PWM0_BYPASS_SHIFT 9
+#define SUNXI_PWMCTL_PWM0_BYPASS_WIDTH 1
+#define SUNXI_PWMCTL_PWM0_START (1<<8)
+#define SUNXI_PWMCTL_PWM0_START_MASK (1<<8)
+#define SUNXI_PWMCTL_PWM0_START_SHIFT 8
+#define SUNXI_PWMCTL_PWM0_START_WIDTH 1
+#define SUNXI_PWMCTL_PWM0_MODE (1<<7)
+#define SUNXI_PWMCTL_PWM0_MODE_MASK (1<<7)
+#define SUNXI_PWMCTL_PWM0_MODE_SHIFT 7
+#define SUNXI_PWMCTL_PWM0_MODE_WIDTH 1
+#define SUNXI_PWMCTL_PWM0_GATE (1<<6)
+#define SUNXI_PWMCTL_PWM0_GATE_MASK (1<<6)
+#define SUNXI_PWMCTL_PWM0_GATE_SHIFT 6
+#define SUNXI_PWMCTL_PWM0_GATE_WIDTH 1
+#define SUNXI_PWMCTL_PWM0_STATE (1<<5)
+#define SUNXI_PWMCTL_PWM0_STATE_MASK (1<<5)
+#define SUNXI_PWMCTL_PWM0_STATE_SHIFT 5
+#define SUNXI_PWMCTL_PWM0_STATE_WIDTH 1
+#define SUNXI_PWMCTL_PWM0_EN (1<<4)
+#define SUNXI_PWMCTL_PWM0_EN_MASK (1<<4)
+#define SUNXI_PWMCTL_PWM0_EN_SHIFT 4
+#define SUNXI_PWMCTL_PWM0_EN_WIDTH 1
+#define SUNXI_PWMCTL_PWM0_PRE_MASK (0xf<<0)
+#define SUNXI_PWMCTL_PWM0_PRE_120 (0<<0)
+#define SUNXI_PWMCTL_PWM0_PRE_180 (1<<0)
+#define SUNXI_PWMCTL_PWM0_PRE_240 (2<<0)
+#define SUNXI_PWMCTL_PWM0_PRE_360 (3<<0)
+#define SUNXI_PWMCTL_PWM0_PRE_480 (4<<0)
+#define SUNXI_PWMCTL_PWM0_PRE_12K (8<<0)
+#define SUNXI_PWMCTL_PWM0_PRE_24K (9<<0)
+#define SUNXI_PWMCTL_PWM0_PRE_36K (0xa<<0)
+#define SUNXI_PWMCTL_PWM0_PRE_48K (0xb<<0)
+#define SUNXI_PWMCTL_PWM0_PRE_72K (0xc<<0)
+#define SUNXI_PWMCTL_PWM0_PRE_1 (0xf<<0)
+#define SUNXI_PWMCTL_PWM0_PRE_SHIFT 0
+#define SUNXI_PWMCTL_PWM0_PRE_WIDTH 4
+
+/* SUNXI_PWM_CH0_PERIOD 0x04 PWM Channel 0 Period Register */
+/* SUNXI_PWM_CH1_PERIOD 0x08 PWM Channel 1 Period Register */
+#define SUNXI_PWM_CYCLES_TOTAL_MASK (0xFFFFL<<16)
+#define SUNXI_PWM_CYCLES_TOTAL_SHIFT 16
+#define SUNXI_PWM_CYCLES_TOTAL_WIDTH 16
+#define SUNXI_PWM_CYCLES_ACTIVE_MASK (0xFFFF<<0)
+#define SUNXI_PWM_CYCLES_ACTIVE_SHIFT 0
+#define SUNXI_PWM_CYCLES_ACTIVE_WIDTH 16
+
+#define MAX_CYCLES_SUN4I 0x0ffL /* max cycle count possible for period active and entire */
+#define MAX_CYCLES 0x0ffffL /* max cycle count possible for period active and entire */
+#define OSC24 24L /* 24Mhz system oscillator */
+
+/* Supported SoC families - used for quirks */
+enum sunxi_soc_family {
+ SUN4I, /* A10 SoC - later revisions */
+ SUN5I, /* A10S/A13 SoCs */
+ SUN7I, /* A20 SoC */
+};
+
+/*
+ * structure that defines the pwm control register
+ */
+
+static unsigned int prescale_divisor[] = {
+ 120, 180, 240, 360, 480, 480, 480, 480,
+ 12000, 24000, 36000, 48000, 72000, 72000, 72000, 1
+};
+
+struct sunxi_pwm_chip {
+ struct pwm_chip chip;
+ struct clk *clk;
+ struct regmap *regmap;
+ struct mutex lock;
+ enum sunxi_soc_family revision;
+ unsigned long max_cycles;
+};
+
+static inline struct sunxi_pwm_chip *to_sunxi_chip(struct pwm_chip *chip)
+{
+ return container_of(chip, struct sunxi_pwm_chip, chip);
+}
+
+/*
+ * Find the best prescale value for the period
+ * We want to get the highest period cycle count possible, so we look
+ * make a run through the prescale values looking for numbers over
+ * min_optimal_period_cycles. If none are found then root though again
+ * taking anything that works
+ */
+int pwm_get_best_prescale(struct sunxi_pwm_chip *priv, int period_in)
+{
+ int i;
+ unsigned long int clk_pico, period, min_optimal_period_cycles;
+ const unsigned long min_period_cycles = 0x02;
+ int best_prescale = 0;
+
+ period = period_in * 1000; /* convert to picoseconds */
+ min_optimal_period_cycles = priv->max_cycles / 2;
+
+ best_prescale = -1;
+ for(i = 0 ; i < ARRAY_SIZE(prescale_divisor) ; i++) {
+
+ clk_pico = 1000000L * prescale_divisor[i] / OSC24;
+ if(clk_pico < 1 || clk_pico > period) {
+ continue;
+ }
+ if(((period / clk_pico) >= min_optimal_period_cycles) &&
+ ((period / clk_pico) <= priv->max_cycles)) {
+ best_prescale = i;
+ }
+ }
+
+ if(best_prescale > ARRAY_SIZE(prescale_divisor)) {
+ for(i = 0 ; i < ARRAY_SIZE(prescale_divisor) ; i++) {
+ clk_pico = 1000000L * prescale_divisor[i] / OSC24;
+ if(clk_pico < 1 || clk_pico > period) {
+ continue;
+ }
+ if(((period / clk_pico) >= min_period_cycles) &&
+ ((period / clk_pico) <= priv->max_cycles)) {
+ best_prescale = i;
+ }
+ }
+ }
+
+ if(best_prescale > ARRAY_SIZE(prescale_divisor))
+ return -EINVAL;
+
+ dev_dbg(priv->chip.dev, "Best prescale is %d\n", best_prescale);
+ return best_prescale;
+}
+
+/*
+ * return the number of cycles for the channel period computed from the nanoseconds
+ * for the period. Allwinner docs call this "entire" cycles
+ */
+unsigned int compute_cycles(struct sunxi_pwm_chip *priv, int prescale, int period)
+{
+ unsigned long int clk_pico, cycles;
+
+ clk_pico = 1000000L * prescale_divisor[prescale] / OSC24;
+ cycles = DIV_ROUND_CLOSEST(period * 1000L, clk_pico);
+ if (cycles > priv->max_cycles)
+ cycles = priv->max_cycles;
+ if (cycles < 2)
+ cycles = 2;
+
+ dev_dbg(priv->chip.dev, "Best prescale was %d, cycles is %lu\n", prescale, cycles);
+
+ return cycles;
+}
+
+static int sunxi_pwm_busy(struct sunxi_pwm_chip *priv)
+{
+ int i, reg_val;
+
+ for (i = 0; i < 50; i++) {
+ regmap_read(priv->regmap, SUNXI_PWM_CTRL_REG, ®_val);
+ if ((reg_val & (SUNXI_PWMCTL_PWM1_NOTRDY | SUNXI_PWMCTL_PWM0_NOTRDY)) == 0)
+ return 0;
+ mdelay(1);
+ }
+ dev_dbg(priv->chip.dev, "PWM busy timeout\n");
+ return -EBUSY;
+}
+
+
+static int sunxi_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
+ int duty_ns, int period_ns)
+{
+ struct sunxi_pwm_chip *priv = to_sunxi_chip(chip);
+ int prescale, entire_cycles, active_cycles, ret = 0;
+ unsigned int reg_val;
+
+
+ if ((duty_ns <= 0) || (period_ns <= 0))
+ return 0;
+
+ mutex_lock(&priv->lock);
+
+ // If period less than two cycles, just enable the OSC24 clock bypass
+ if ((priv->revision != SUN4I) && (period_ns < (2 * 1000 / OSC24 + 1))) {
+ switch (pwm->hwpwm) {
+ case 0:
+ regmap_update_bits(priv->regmap, SUNXI_PWM_CTRL_REG,
+ SUNXI_PWMCTL_PWM0_BYPASS_MASK, SUNXI_PWMCTL_PWM0_BYPASS);
+ break;
+ case 1:
+ regmap_update_bits(priv->regmap, SUNXI_PWM_CTRL_REG,
+ SUNXI_PWMCTL_PWM1_BYPASS_MASK, SUNXI_PWMCTL_PWM1_BYPASS);
+ break;
+ }
+ goto exit;
+ }
+
+ prescale = pwm_get_best_prescale(priv, period_ns);
+ if (prescale < 0) {
+ ret = prescale;
+ goto exit;
+ }
+ entire_cycles = compute_cycles(priv, prescale, period_ns);
+ active_cycles = compute_cycles(priv, prescale, duty_ns);
+
+ reg_val = (entire_cycles << SUNXI_PWM_CYCLES_TOTAL_SHIFT) & SUNXI_PWM_CYCLES_TOTAL_MASK;
+ reg_val |= (active_cycles << SUNXI_PWM_CYCLES_ACTIVE_SHIFT) & SUNXI_PWM_CYCLES_ACTIVE_MASK;
+
+ switch (pwm->hwpwm) {
+ case 0:
+ regmap_update_bits(priv->regmap, SUNXI_PWM_CTRL_REG,
+ SUNXI_PWMCTL_PWM0_BYPASS_MASK | SUNXI_PWMCTL_PWM0_PRE_MASK | SUNXI_PWMCTL_PWM0_EN_MASK,
+ prescale << SUNXI_PWMCTL_PWM0_PRE_SHIFT | SUNXI_PWMCTL_PWM0_EN);
+ regmap_write(priv->regmap, SUNXI_PWM_CH0_PERIOD, reg_val);
+ break;
+ case 1:
+ regmap_update_bits(priv->regmap, SUNXI_PWM_CTRL_REG,
+ SUNXI_PWMCTL_PWM1_BYPASS_MASK | SUNXI_PWMCTL_PWM1_PRE_MASK | SUNXI_PWMCTL_PWM1_EN_MASK,
+ prescale << SUNXI_PWMCTL_PWM1_PRE_SHIFT | SUNXI_PWMCTL_PWM1_EN);
+ regmap_write(priv->regmap, SUNXI_PWM_CH1_PERIOD, reg_val);
+ break;
+ default:
+ ret = -EINVAL;
+ }
+exit:
+ mutex_unlock(&priv->lock);
+ return ret;
+}
+
+static int sunxi_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
+{
+ struct sunxi_pwm_chip *priv = to_sunxi_chip(chip);
+ int ret = 0;
+
+ mutex_lock(&priv->lock);
+ switch (pwm->hwpwm) {
+ case 0:
+ regmap_update_bits(priv->regmap, SUNXI_PWM_CTRL_REG,
+ SUNXI_PWMCTL_PWM0_GATE_MASK | SUNXI_PWMCTL_PWM0_EN_MASK,
+ SUNXI_PWMCTL_PWM0_GATE | SUNXI_PWMCTL_PWM0_EN);
+ break;
+ case 1:
+ regmap_update_bits(priv->regmap, SUNXI_PWM_CTRL_REG,
+ SUNXI_PWMCTL_PWM1_GATE_MASK | SUNXI_PWMCTL_PWM1_EN_MASK,
+ SUNXI_PWMCTL_PWM1_GATE | SUNXI_PWMCTL_PWM1_EN);
+ break;
+ default:
+ ret = -EINVAL;
+ goto exit;
+ }
+ if ((priv->revision == SUN5I) || (priv->revision == SUN7I))
+ ret = sunxi_pwm_busy(priv);
+
+{
+ int reg_val;
+ regmap_read(priv->regmap, SUNXI_PWM_CTRL_REG, ®_val);
+ printk("JDS - SUNXI_PWM_CTRL_REG %08x\n", reg_val);
+ regmap_read(priv->regmap, SUNXI_PWM_CH0_PERIOD, ®_val);
+ printk("JDS - SUNXI_PWM_CH0_PERIOD %08x\n", reg_val);
+ regmap_read(priv->regmap, SUNXI_PWM_CH1_PERIOD, ®_val);
+ printk("JDS - SUNXI_PWM_CH1_PERIOD %08x\n", reg_val);
+
+}
+
+exit:
+ mutex_unlock(&priv->lock);
+ return ret;
+}
+
+static void sunxi_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
+{
+ struct sunxi_pwm_chip *priv = to_sunxi_chip(chip);
+
+ mutex_lock(&priv->lock);
+ switch (pwm->hwpwm) {
+ case 0:
+ regmap_update_bits(priv->regmap, SUNXI_PWM_CTRL_REG,
+ SUNXI_PWMCTL_PWM0_GATE_MASK | SUNXI_PWMCTL_PWM0_EN_MASK, 0);
+ break;
+ case 1:
+ regmap_update_bits(priv->regmap, SUNXI_PWM_CTRL_REG,
+ SUNXI_PWMCTL_PWM1_GATE_MASK | SUNXI_PWMCTL_PWM1_EN_MASK, 0);
+ break;
+ }
+ mutex_unlock(&priv->lock);
+ return;
+}
+
+static int sunxi_pwm_polarity(struct pwm_chip *chip, struct pwm_device *pwm,
+ enum pwm_polarity polarity)
+{
+ struct sunxi_pwm_chip *priv = to_sunxi_chip(chip);
+ int ret = 0;
+
+ mutex_lock(&priv->lock);
+ switch (pwm->hwpwm) {
+ case 0:
+ regmap_update_bits(priv->regmap, SUNXI_PWM_CTRL_REG,
+ SUNXI_PWMCTL_PWM0_STATE_MASK,
+ (polarity == PWM_POLARITY_INVERSED) << SUNXI_PWMCTL_PWM0_STATE_SHIFT);
+ break;
+ case 1:
+ regmap_update_bits(priv->regmap, SUNXI_PWM_CTRL_REG,
+ SUNXI_PWMCTL_PWM1_STATE_MASK,
+ (polarity == PWM_POLARITY_INVERSED) << SUNXI_PWMCTL_PWM1_STATE_SHIFT);
+ break;
+ default:
+ ret = -EINVAL;
+ }
+ mutex_unlock(&priv->lock);
+ return ret;
+}
+
+static const struct pwm_ops sunxi_pwm_ops = {
+ .config = sunxi_pwm_config,
+ .enable = sunxi_pwm_enable,
+ .disable = sunxi_pwm_disable,
+ .set_polarity = sunxi_pwm_polarity,
+ .owner = THIS_MODULE,
+};
+
+static const struct regmap_range sunxi_pwm_volatile_regs_range[] = {
+ regmap_reg_range(SUNXI_PWM_CTRL_REG, SUNXI_PWM_CTRL_REG),
+};
+
+static const struct regmap_access_table sunxi_pwm_volatile_regs = {
+ .yes_ranges = sunxi_pwm_volatile_regs_range,
+ .n_yes_ranges = ARRAY_SIZE(sunxi_pwm_volatile_regs_range),
+};
+
+static const struct regmap_config sunxi_pwm_regmap_config = {
+ .reg_bits = 32,
+ .reg_stride = 4,
+ .val_bits = 32,
+ .max_register = SUNXI_PWM_CH1_PERIOD,
+ .volatile_table = &sunxi_pwm_volatile_regs,
+};
+
+static const struct of_device_id sunxi_pwm_of_match[] = {
+ { .compatible = "allwinner,sun4i-a10-pwm", .data = (void *)SUN4I},
+ { .compatible = "allwinner,sun5i-a13-pwm", .data = (void *)SUN5I},
+ { .compatible = "allwinner,sun7i-a20-pwm", .data = (void *)SUN7I},
+ {}
+};
+MODULE_DEVICE_TABLE(of, sunxi_pwm_of_match);
+
+static int sunxi_pwm_probe(struct platform_device *pdev)
+{
+ const struct of_device_id *of_id;
+ void __iomem *base;
+ struct sunxi_pwm_chip *priv;
+ struct resource *res;
+ int ret;
+
+ of_id = of_match_device(sunxi_pwm_of_match, &pdev->dev);
+ if (!of_id)
+ return -EINVAL;
+
+ priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
+ if (!priv)
+ return -ENOMEM;
+
+ priv->chip.dev = &pdev->dev;
+ priv->revision = (enum sunxi_soc_family)of_id->data;
+ if (priv->revision == SUN4I) {
+ priv->max_cycles = MAX_CYCLES_SUN4I;
+ prescale_divisor[15] = 72000; //A10 does not support prescale = 1
+ } else
+ priv->max_cycles = MAX_CYCLES;
+
+ res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+ base = devm_ioremap_resource(&pdev->dev, res);
+ if (IS_ERR(base))
+ return PTR_ERR(base);
+
+ priv->regmap = devm_regmap_init_mmio(&pdev->dev, base,
+ &sunxi_pwm_regmap_config);
+ if (IS_ERR(priv->regmap))
+ return PTR_ERR(priv->regmap);
+
+ priv->clk = devm_clk_get(&pdev->dev, NULL);
+ if (IS_ERR(priv->clk)) {
+ dev_err(&pdev->dev, "failed to get Osc24M clock\n");
+ return PTR_ERR(priv->clk);
+ }
+ ret = clk_prepare_enable(priv->clk);
+ if (ret) {
+ dev_err(&pdev->dev, "failed to enable Osc24M clock\n");
+ return ret;
+ }
+
+ mutex_init(&priv->lock);
+
+ priv->chip.ops = &sunxi_pwm_ops;
+ priv->chip.base = -1;
+ priv->chip.npwm = 2;
+ priv->chip.of_xlate = of_pwm_xlate_with_flags;
+ priv->chip.of_pwm_n_cells = 3;
+
+ ret = pwmchip_add(&priv->chip);
+ if (ret < 0) {
+ dev_err(&pdev->dev, "failed to add PWM chip: %d\n", ret);
+ goto error;
+ }
+
+ platform_set_drvdata(pdev, priv);
+ return 0;
+
+error:
+ mutex_destroy(&priv->lock);
+ clk_disable_unprepare(priv->clk);
+ return ret;
+}
+
+static int sunxi_pwm_remove(struct platform_device *pdev)
+{
+ struct sunxi_pwm_chip *priv = platform_get_drvdata(pdev);
+
+ mutex_destroy(&priv->lock);
+ clk_disable_unprepare(priv->clk);
+ return pwmchip_remove(&priv->chip);
+}
+
+static struct platform_driver sunxi_pwm_driver = {
+ .driver = {
+ .name = "sunxi-pwm",
+ .of_match_table = sunxi_pwm_of_match,
+ },
+ .probe = sunxi_pwm_probe,
+ .remove = sunxi_pwm_remove,
+};
+module_platform_driver(sunxi_pwm_driver);
+
+MODULE_DESCRIPTION("Allwinner PWM Driver");
+MODULE_ALIAS("platform:sunxi-pwm");
+MODULE_LICENSE("GPL");
+
+
^ permalink raw reply related [flat|nested] 8+ messages in thread
end of thread, other threads:[~2014-08-17 20:20 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-05-19 18:10 [PATCHv5 0/2] Add Allwinner SoCs PWM support Alexandre Belloni
2014-05-19 18:10 ` [PATCHv5 1/2] pwm: Add Allwinner SoC support Alexandre Belloni
2014-06-17 23:26 ` Thierry Reding
2014-06-23 17:01 ` Alexandre Belloni
2014-08-17 17:03 ` jonsmirl
2014-08-17 20:20 ` jonsmirl-Re5JQEeQqe8AvxtiuMwx3w
2014-05-19 18:10 ` [PATCHv5 2/2] pwm: sunxi: document OF bindings Alexandre Belloni
2014-06-17 23:29 ` Thierry Reding
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).