From patchwork Wed Feb 22 15:17:17 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thierry Reding X-Patchwork-Id: 142488 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 94AE1B6FE8 for ; Thu, 23 Feb 2012 02:18:14 +1100 (EST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753089Ab2BVPSO (ORCPT ); Wed, 22 Feb 2012 10:18:14 -0500 Received: from moutng.kundenserver.de ([212.227.17.10]:55002 "EHLO moutng.kundenserver.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752863Ab2BVPSN (ORCPT ); Wed, 22 Feb 2012 10:18:13 -0500 Received: from benhur.adnet.avionic-design.de (p548E0EA0.dip0.t-ipconnect.de [84.142.14.160]) by mrelayeu.kundenserver.de (node=mreu2) with ESMTP (Nemesis) id 0MM2cS-1S5kGY39Ku-008EWa; Wed, 22 Feb 2012 16:18:07 +0100 Received: from mailbox.adnet.avionic-design.de (add-virt-zarafa.adnet.avionic-design.de [172.20.129.9]) by benhur.adnet.avionic-design.de (Postfix) with ESMTP id F3B6E2C411D; Wed, 22 Feb 2012 16:18:08 +0100 (CET) Received: from localhost (localhost [127.0.0.1]) by mailbox.adnet.avionic-design.de (Postfix) with ESMTP id C5D092920004; Wed, 22 Feb 2012 16:18:05 +0100 (CET) X-Virus-Scanned: amavisd-new at avionic-design.de Received: from mailbox.adnet.avionic-design.de ([127.0.0.1]) by localhost (mailbox.avionic-design.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 9w3TNl9Zd4lP; Wed, 22 Feb 2012 16:17:56 +0100 (CET) Received: from localhost (avionic-0098.adnet.avionic-design.de [172.20.31.233]) (Authenticated sender: thierry.reding) by mailbox.adnet.avionic-design.de (Postfix) with ESMTPA id 955542920006; Wed, 22 Feb 2012 16:17:26 +0100 (CET) From: Thierry Reding To: devicetree-discuss@lists.ozlabs.org Cc: linux-arm-kernel@lists.infradead.org, linux-tegra@vger.kernel.org, Sascha Hauer , Arnd Bergmann , Matthias Kaehlcke , Kurt Van Dijck , Rob Herring , Grant Likely , Colin Cross , Olof Johansson , Richard Purdie , Mark Brown , Mitch Bradley , Mike Frysinger , Eric Miao , Lars-Peter Clausen , Ryan Mallon Subject: [PATCH v3 06/10] pwm: Add NVIDIA Tegra SoC support Date: Wed, 22 Feb 2012 16:17:17 +0100 Message-Id: <1329923841-32017-7-git-send-email-thierry.reding@avionic-design.de> X-Mailer: git-send-email 1.7.9.1 In-Reply-To: <1329923841-32017-1-git-send-email-thierry.reding@avionic-design.de> References: <1329923841-32017-1-git-send-email-thierry.reding@avionic-design.de> X-Provags-ID: V02:K0:Lv9rOuY+fb5OLVQh5aD3PN6JnIQ5dOr/DAsk5Voi2j0 w+xqnVNpjc6JoCSpuLI/GMLrXjmIW6oOseZVcgM7JA2ToUg4rU qA/JODk2c7aUyIqnLVXLaDWaeyd77I79R6/G5b9lu/+Zte4VGQ aYhRDwUF9EynIXpwwCz2P5femfG5Lk4yB6RW2Q2fXo+C0PC1/c GADOttrrsJzIr7HoMqVRDrT80D5H0L2KUaSBiBhlcba6EVtqQp KMtmBbxk1tOOALxxTyBPMnyhCswhFASbmPz5HQQLc0DQwVd2WM 5mY94eJyaa0plJ0PgKWleFDeXCAb9CguCy8yTQuJezKU4dI5+7 M1iTRDaBd1cNJN+xNLc3is5jxwJ40NDVY5OmNuDG4LQUwTjtg7 +r2uMvp9eGq/hU5c07d34ANwwKN90fMAyxuh4KLpmkgDTmRQ/h 7jJfV Sender: linux-tegra-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-tegra@vger.kernel.org This commit adds a generic PWM framework driver for the PWFM controller found on NVIDIA Tegra SoCs. The driver is based on code from the Chromium kernel tree and was originally written by Gary King (NVIDIA) and later modified by Simon Que (Chromium). Signed-off-by: Thierry Reding Acked-by: Stephen Warren --- Changes in v3: - use pwm_device.hwpwm instead of recomputing it - update pwm_ops for changes in patch 2 Changes in v2: - set pwm_chip.dev field - rename clk_enb to enable - introduce NUM_PWM macro instead of hard-coding the number of PWM devices - update comment in tegra_pwm_config - fix coding-style for multi-line comments - use module_platform_driver - change license to GPL drivers/pwm/Kconfig | 10 ++ drivers/pwm/Makefile | 1 + drivers/pwm/pwm-tegra.c | 263 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 274 insertions(+), 0 deletions(-) create mode 100644 drivers/pwm/pwm-tegra.c diff --git a/drivers/pwm/Kconfig b/drivers/pwm/Kconfig index 93c1052..bda6f23 100644 --- a/drivers/pwm/Kconfig +++ b/drivers/pwm/Kconfig @@ -9,4 +9,14 @@ menuconfig PWM if PWM +config PWM_TEGRA + tristate "NVIDIA Tegra PWM support" + depends on ARCH_TEGRA + help + Generic PWM framework driver for the PWFM controller found on NVIDIA + Tegra SoCs. + + To compile this driver as a module, choose M here: the module + will be called pwm-tegra. + endif diff --git a/drivers/pwm/Makefile b/drivers/pwm/Makefile index 3469c3d..12300f5 100644 --- a/drivers/pwm/Makefile +++ b/drivers/pwm/Makefile @@ -1 +1,2 @@ obj-$(CONFIG_PWM) += core.o +obj-$(CONFIG_PWM_TEGRA) += pwm-tegra.o diff --git a/drivers/pwm/pwm-tegra.c b/drivers/pwm/pwm-tegra.c new file mode 100644 index 0000000..1c95e08 --- /dev/null +++ b/drivers/pwm/pwm-tegra.c @@ -0,0 +1,263 @@ +/* + * drivers/pwm/pwm-tegra.c + * + * Tegra pulse-width-modulation controller driver + * + * Copyright (c) 2010, NVIDIA Corporation. + * Based on arch/arm/plat-mxc/pwm.c by Sascha Hauer + * + * 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., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#define PWM_ENABLE (1 << 31) +#define PWM_DUTY_WIDTH 8 +#define PWM_DUTY_SHIFT 16 +#define PWM_SCALE_WIDTH 13 +#define PWM_SCALE_SHIFT 0 + +#define NUM_PWM 4 + +struct tegra_pwm_chip { + struct pwm_chip chip; + struct device *dev; + + struct clk *clk; + + int enable[NUM_PWM]; + void __iomem *mmio_base; +}; + +static inline struct tegra_pwm_chip *to_tegra_pwm_chip(struct pwm_chip *chip) +{ + return container_of(chip, struct tegra_pwm_chip, chip); +} + +static inline int pwm_writel(struct tegra_pwm_chip *chip, unsigned int num, + unsigned long val) +{ + unsigned long offset = num << 4; + int rc; + + rc = clk_enable(chip->clk); + if (WARN_ON(rc)) + return rc; + + writel(val, chip->mmio_base + offset); + clk_disable(chip->clk); + + return 0; +} + +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 rate, hz; + u32 val = 0; + + /* + * Convert from duty_ns / period_ns to a fixed number of duty + * ticks per (1 << PWM_DUTY_WIDTH) cycles. + */ + c = duty_ns * ((1 << PWM_DUTY_WIDTH) - 1); + do_div(c, period_ns); + + val = (u32)c << PWM_DUTY_SHIFT; + + /* + * Compute the prescaler value for which (1 << PWM_DUTY_WIDTH) + * cycles at the PWM clock rate will take period_ns nanoseconds. + */ + rate = clk_get_rate(pc->clk) >> PWM_DUTY_WIDTH; + hz = 1000000000ul / period_ns; + + rate = (rate + (hz / 2)) / hz; + + /* + * Since the actual PWM divider is the register's frequency divider + * field minus 1, we need to decrement to get the correct value to + * write to the register. + */ + if (rate > 0) + rate--; + + /* + * Make sure that the rate will fit in the register's frequency + * divider field. + */ + if (rate >> PWM_SCALE_WIDTH) + return -EINVAL; + + val |= (rate << PWM_SCALE_SHIFT); + + /* If the PWM channel is enabled, keep it enabled */ + if (pc->enable[pwm->hwpwm]) + val |= PWM_ENABLE; + + return pwm_writel(pc, pwm->hwpwm, val); +} + +static int tegra_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm) +{ + struct tegra_pwm_chip *pc = to_tegra_pwm_chip(chip); + int rc = 0; + + if (!pc->enable[pwm->hwpwm]) { + rc = clk_enable(pc->clk); + if (!rc) { + unsigned long offset = pwm->hwpwm << 4; + u32 val; + + val = readl(pc->mmio_base + offset); + val |= PWM_ENABLE; + writel(val, pc->mmio_base + offset); + + pc->enable[pwm->hwpwm] = 1; + } + } + + return 0; +} + +static void tegra_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm) +{ + struct tegra_pwm_chip *pc = to_tegra_pwm_chip(chip); + + if (pc->enable[pwm->hwpwm]) { + unsigned long offset = pwm->hwpwm << 4; + u32 val; + + val = readl(pc->mmio_base + offset); + val &= ~PWM_ENABLE; + writel(val, pc->mmio_base + offset); + + clk_disable(pc->clk); + pc->enable[pwm->hwpwm] = 0; + } +} + +static struct pwm_ops tegra_pwm_ops = { + .config = tegra_pwm_config, + .enable = tegra_pwm_enable, + .disable = tegra_pwm_disable, + .owner = THIS_MODULE, +}; + +static int tegra_pwm_probe(struct platform_device *pdev) +{ + struct tegra_pwm_chip *pwm; + struct resource *r; + int ret; + + pwm = devm_kzalloc(&pdev->dev, sizeof(*pwm), GFP_KERNEL); + if (!pwm) { + dev_err(&pdev->dev, "failed to allocate memory\n"); + return -ENOMEM; + } + + pwm->dev = &pdev->dev; + + r = platform_get_resource(pdev, IORESOURCE_MEM, 0); + if (!r) { + dev_err(&pdev->dev, "no memory resources defined\n"); + return -ENODEV; + } + + r = devm_request_mem_region(&pdev->dev, r->start, resource_size(r), + pdev->name); + if (!r) { + dev_err(&pdev->dev, "failed to request memory\n"); + return -EBUSY; + } + + pwm->mmio_base = devm_ioremap(&pdev->dev, r->start, resource_size(r)); + if (!pwm->mmio_base) { + dev_err(&pdev->dev, "failed to ioremap() region\n"); + return -ENODEV; + } + + platform_set_drvdata(pdev, pwm); + + pwm->clk = clk_get(&pdev->dev, NULL); + if (IS_ERR(pwm->clk)) + return PTR_ERR(pwm->clk); + + pwm->chip.dev = &pdev->dev; + pwm->chip.ops = &tegra_pwm_ops; + pwm->chip.base = -1; + pwm->chip.npwm = NUM_PWM; + + ret = pwmchip_add(&pwm->chip); + if (ret < 0) { + dev_err(&pdev->dev, "pwmchip_add() failed: %d\n", ret); + clk_put(pwm->clk); + return ret; + } + + return 0; +} + +static int __devexit tegra_pwm_remove(struct platform_device *pdev) +{ + struct tegra_pwm_chip *pwm = platform_get_drvdata(pdev); + int i; + + if (WARN_ON(!pwm)) + return -ENODEV; + + pwmchip_remove(&pwm->chip); + + for (i = 0; i < NUM_PWM; i++) { + pwm_writel(pwm, i, 0); + + if (pwm->enable[i]) + clk_disable(pwm->clk); + } + + clk_put(pwm->clk); + + return 0; +} + +#ifdef CONFIG_OF +static struct of_device_id tegra_pwm_of_match[] = { + { .compatible = "nvidia,tegra20-pwm" }, + { } +}; +#endif + +static struct platform_driver tegra_pwm_driver = { + .driver = { + .name = "tegra-pwm", + .of_match_table = of_match_ptr(tegra_pwm_of_match), + }, + .probe = tegra_pwm_probe, + .remove = __devexit_p(tegra_pwm_remove), +}; + +module_platform_driver(tegra_pwm_driver); + +MODULE_LICENSE("GPL"); +MODULE_AUTHOR("NVIDIA Corporation");