Merge tag 'drm-x86-pat-regression-fix' of git://people.freedesktop.org/~airlied/linux
[cascardo/linux.git] / drivers / pwm / pwm-tegra.c
index ec5b79f..e464784 100644 (file)
 #include <linux/io.h>
 #include <linux/module.h>
 #include <linux/of.h>
+#include <linux/of_device.h>
 #include <linux/pwm.h>
 #include <linux/platform_device.h>
 #include <linux/slab.h>
+#include <linux/reset.h>
 
 #define PWM_ENABLE     (1 << 31)
 #define PWM_DUTY_WIDTH 8
 #define PWM_SCALE_WIDTH        13
 #define PWM_SCALE_SHIFT        0
 
+struct tegra_pwm_soc {
+       unsigned int num_channels;
+};
+
 struct tegra_pwm_chip {
        struct pwm_chip chip;
        struct device *dev;
 
        struct clk *clk;
+       struct reset_control*rst;
 
-       void __iomem *mmio_base;
+       void __iomem *regs;
+
+       const struct tegra_pwm_soc *soc;
 };
 
 static inline struct tegra_pwm_chip *to_tegra_pwm_chip(struct pwm_chip *chip)
@@ -52,20 +61,20 @@ static inline struct tegra_pwm_chip *to_tegra_pwm_chip(struct pwm_chip *chip)
 
 static inline u32 pwm_readl(struct tegra_pwm_chip *chip, unsigned int num)
 {
-       return readl(chip->mmio_base + (num << 4));
+       return readl(chip->regs + (num << 4));
 }
 
 static inline void pwm_writel(struct tegra_pwm_chip *chip, unsigned int num,
                             unsigned long val)
 {
-       writel(val, chip->mmio_base + (num << 4));
+       writel(val, chip->regs + (num << 4));
 }
 
 static int tegra_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
                            int duty_ns, int period_ns)
 {
        struct tegra_pwm_chip *pc = to_tegra_pwm_chip(chip);
-       unsigned long long c;
+       unsigned long long c = duty_ns;
        unsigned long rate, hz;
        u32 val = 0;
        int err;
@@ -75,7 +84,8 @@ static int tegra_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
         * per (1 << PWM_DUTY_WIDTH) cycles and make sure to round to the
         * nearest integer during division.
         */
-       c = duty_ns * ((1 << PWM_DUTY_WIDTH) - 1) + period_ns / 2;
+       c *= (1 << PWM_DUTY_WIDTH);
+       c += period_ns / 2;
        do_div(c, period_ns);
 
        val = (u32)c << PWM_DUTY_SHIFT;
@@ -174,12 +184,13 @@ static int tegra_pwm_probe(struct platform_device *pdev)
        if (!pwm)
                return -ENOMEM;
 
+       pwm->soc = of_device_get_match_data(&pdev->dev);
        pwm->dev = &pdev->dev;
 
        r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
-       pwm->mmio_base = devm_ioremap_resource(&pdev->dev, r);
-       if (IS_ERR(pwm->mmio_base))
-               return PTR_ERR(pwm->mmio_base);
+       pwm->regs = devm_ioremap_resource(&pdev->dev, r);
+       if (IS_ERR(pwm->regs))
+               return PTR_ERR(pwm->regs);
 
        platform_set_drvdata(pdev, pwm);
 
@@ -187,14 +198,24 @@ static int tegra_pwm_probe(struct platform_device *pdev)
        if (IS_ERR(pwm->clk))
                return PTR_ERR(pwm->clk);
 
+       pwm->rst = devm_reset_control_get(&pdev->dev, "pwm");
+       if (IS_ERR(pwm->rst)) {
+               ret = PTR_ERR(pwm->rst);
+               dev_err(&pdev->dev, "Reset control is not found: %d\n", ret);
+               return ret;
+       }
+
+       reset_control_deassert(pwm->rst);
+
        pwm->chip.dev = &pdev->dev;
        pwm->chip.ops = &tegra_pwm_ops;
        pwm->chip.base = -1;
-       pwm->chip.npwm = 4;
+       pwm->chip.npwm = pwm->soc->num_channels;
 
        ret = pwmchip_add(&pwm->chip);
        if (ret < 0) {
                dev_err(&pdev->dev, "pwmchip_add() failed: %d\n", ret);
+               reset_control_assert(pwm->rst);
                return ret;
        }
 
@@ -205,10 +226,15 @@ static int tegra_pwm_remove(struct platform_device *pdev)
 {
        struct tegra_pwm_chip *pc = platform_get_drvdata(pdev);
        unsigned int i;
+       int err;
 
        if (WARN_ON(!pc))
                return -ENODEV;
 
+       err = clk_prepare_enable(pc->clk);
+       if (err < 0)
+               return err;
+
        for (i = 0; i < pc->chip.npwm; i++) {
                struct pwm_device *pwm = &pc->chip.pwms[i];
 
@@ -221,12 +247,23 @@ static int tegra_pwm_remove(struct platform_device *pdev)
                clk_disable_unprepare(pc->clk);
        }
 
+       reset_control_assert(pc->rst);
+       clk_disable_unprepare(pc->clk);
+
        return pwmchip_remove(&pc->chip);
 }
 
+static const struct tegra_pwm_soc tegra20_pwm_soc = {
+       .num_channels = 4,
+};
+
+static const struct tegra_pwm_soc tegra186_pwm_soc = {
+       .num_channels = 1,
+};
+
 static const struct of_device_id tegra_pwm_of_match[] = {
-       { .compatible = "nvidia,tegra20-pwm" },
-       { .compatible = "nvidia,tegra30-pwm" },
+       { .compatible = "nvidia,tegra20-pwm", .data = &tegra20_pwm_soc },
+       { .compatible = "nvidia,tegra186-pwm", .data = &tegra186_pwm_soc },
        { }
 };