From patchwork Thu May 21 13:29:24 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: YH Huang X-Patchwork-Id: 6455081 Return-Path: X-Original-To: patchwork-linux-mediatek@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id F1CF2C0432 for ; Thu, 21 May 2015 13:30:07 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id C95812045B for ; Thu, 21 May 2015 13:30:06 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.9]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 95AA920459 for ; Thu, 21 May 2015 13:30:05 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1YvQY0-0005z1-Tr; Thu, 21 May 2015 13:30:04 +0000 Received: from [210.61.82.184] (helo=mailgw02.mediatek.com) by bombadil.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1YvQXv-0005eL-De; Thu, 21 May 2015 13:30:01 +0000 X-Listener-Flag: 11101 Received: from mtkhts07.mediatek.inc [(172.21.101.69)] by mailgw02.mediatek.com (envelope-from ) (mhqrelay.mediatek.com ESMTP with TLS) with ESMTP id 573861721; Thu, 21 May 2015 21:29:33 +0800 Received: from mtkslt302.mediatek.inc (10.21.14.115) by mtkhts07.mediatek.inc (172.21.101.73) with Microsoft SMTP Server id 14.3.181.6; Thu, 21 May 2015 21:29:29 +0800 From: YH Huang To: Matthias Brugger , Mark Rutland , Thierry Reding Subject: [PATCH v2 2/2] pwm: add MediaTek display PWM driver support Date: Thu, 21 May 2015 21:29:24 +0800 Message-ID: <1432214964-40644-3-git-send-email-yh.huang@mediatek.com> X-Mailer: git-send-email 1.8.1.1.dirty In-Reply-To: <1432214964-40644-1-git-send-email-yh.huang@mediatek.com> References: <1432214964-40644-1-git-send-email-yh.huang@mediatek.com> MIME-Version: 1.0 X-MTK: N X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20150521_062959_904471_8DE0910F X-CRM114-Status: GOOD ( 16.15 ) X-Spam-Score: 1.3 (+) Cc: linux-pwm@vger.kernel.org, YH Huang , srv_heupstream@mediatek.com, Pawel Moll , devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, Rob Herring , linux-mediatek@lists.infradead.org, Sascha Hauer , linux-arm-kernel@lists.infradead.org X-BeenThere: linux-mediatek@lists.infradead.org X-Mailman-Version: 2.1.18-1 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "Linux-mediatek" Errors-To: linux-mediatek-bounces+patchwork-linux-mediatek=patchwork.kernel.org@lists.infradead.org X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_MED, T_RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Add display PWM driver support to modify backlight for MT8173. Signed-off-by: YH Huang --- drivers/pwm/Kconfig | 10 ++ drivers/pwm/Makefile | 1 + drivers/pwm/pwm-mtk-disp.c | 228 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 239 insertions(+) create mode 100644 drivers/pwm/pwm-mtk-disp.c diff --git a/drivers/pwm/Kconfig b/drivers/pwm/Kconfig index b1541f4..90e3c079 100644 --- a/drivers/pwm/Kconfig +++ b/drivers/pwm/Kconfig @@ -211,6 +211,16 @@ config PWM_LPSS_PLATFORM To compile this driver as a module, choose M here: the module will be called pwm-lpss-platform. +config PWM_MTK_DISP + tristate "MediaTek display PWM driver" + depends on HAS_IOMEM + help + Generic PWM framework driver for MediaTek disp-pwm device. + The PWM is used to control the backlight brightness for display. + + To compile this driver as a module, choose M here: the module + will be called pwm-mtk-disp. + config PWM_MXS tristate "Freescale MXS PWM support" depends on ARCH_MXS && OF diff --git a/drivers/pwm/Makefile b/drivers/pwm/Makefile index ec50eb5..99c9e75 100644 --- a/drivers/pwm/Makefile +++ b/drivers/pwm/Makefile @@ -18,6 +18,7 @@ obj-$(CONFIG_PWM_LPC32XX) += pwm-lpc32xx.o obj-$(CONFIG_PWM_LPSS) += pwm-lpss.o obj-$(CONFIG_PWM_LPSS_PCI) += pwm-lpss-pci.o obj-$(CONFIG_PWM_LPSS_PLATFORM) += pwm-lpss-platform.o +obj-$(CONFIG_PWM_MTK_DISP) += pwm-mtk-disp.o obj-$(CONFIG_PWM_MXS) += pwm-mxs.o obj-$(CONFIG_PWM_PCA9685) += pwm-pca9685.o obj-$(CONFIG_PWM_PUV3) += pwm-puv3.o diff --git a/drivers/pwm/pwm-mtk-disp.c b/drivers/pwm/pwm-mtk-disp.c new file mode 100644 index 0000000..d4e4cb6 --- /dev/null +++ b/drivers/pwm/pwm-mtk-disp.c @@ -0,0 +1,228 @@ +/* + * MediaTek display pulse-width-modulation controller driver. + * Copyright (c) 2015 MediaTek Inc. + * Author: YH Huang + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * 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. + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#define DISP_PWM_EN 0x0 +#define PWM_ENABLE_MASK 0x1 + +#define DISP_PWM_COMMIT 0x08 +#define PWM_COMMIT_MASK 0x1 + +#define DISP_PWM_CON_0 0x10 +#define PWM_CLKDIV_SHIFT 16 +#define PWM_CLKDIV_MASK (0x3ff << PWM_CLKDIV_SHIFT) +#define PWM_CLKDIV_MAX 0x000003ff + +#define DISP_PWM_CON_1 0x14 +#define PWM_PERIOD_MASK 0xfff +#define PWM_PERIOD_MAX 0x00000fff +/* Shift log2(PWM_PERIOD_MAX + 1) as divisor */ +#define PWM_PERIOD_BIT_SHIFT 12 + +#define PWM_HIGH_WIDTH_SHIFT 16 +#define PWM_HIGH_WIDTH_MASK (0x1fff << PWM_HIGH_WIDTH_SHIFT) + +struct mtk_disp_pwm { + struct pwm_chip chip; + struct device *dev; + struct clk *clk_main; + struct clk *clk_mm; + void __iomem *mmio_base; +}; + +static inline struct mtk_disp_pwm *to_mtk_disp_pwm(struct pwm_chip *chip) +{ + return container_of(chip, struct mtk_disp_pwm, chip); +} + +static void mtk_disp_pwm_update_bits(void __iomem *address, u32 mask, u32 value) +{ + u32 val; + + val = readl(address); + val &= ~mask; + val |= value; + writel(val, address); +} + +static int mtk_disp_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm, + int duty_ns, int period_ns) +{ + struct mtk_disp_pwm *mdp; + u64 div, rate; + u32 clk_div, period, high_width, value; + + /* + * Find period, high_width and clk_div to suit duty_ns and period_ns. + * Calculate proper div value to keep period value in the bound. + * + * period_ns = 10^9 * (clk_div + 1) * (period +1) / PWM_CLK_RATE + * duty_ns = 10^9 * (clk_div + 1) * (high_width + 1) / PWM_CLK_RATE + * + * period = (PWM_CLK_RATE * period_ns) / (10^9 * (clk_div + 1)) - 1 + * high_width = (PWM_CLK_RATE * duty_ns) / (10^9 * (clk_div + 1)) - 1 + */ + mdp = to_mtk_disp_pwm(chip); + rate = clk_get_rate(mdp->clk_main); + clk_div = div_u64(rate * period_ns, NSEC_PER_SEC) >> + PWM_PERIOD_BIT_SHIFT; + if (clk_div > PWM_CLKDIV_MAX) + return -EINVAL; + + div = clk_div + 1; + period = div64_u64(rate * period_ns, NSEC_PER_SEC * div); + if (period > 0) + period--; + + high_width = div64_u64(rate * duty_ns, NSEC_PER_SEC * div); + if (high_width > 0) + high_width--; + + mtk_disp_pwm_update_bits(mdp->mmio_base + DISP_PWM_CON_0, + PWM_CLKDIV_MASK, clk_div << PWM_CLKDIV_SHIFT); + + value = period | (high_width << PWM_HIGH_WIDTH_SHIFT); + mtk_disp_pwm_update_bits(mdp->mmio_base + DISP_PWM_CON_1, + PWM_PERIOD_MASK | PWM_HIGH_WIDTH_MASK, value); + + mtk_disp_pwm_update_bits(mdp->mmio_base + DISP_PWM_COMMIT, + PWM_COMMIT_MASK, 1); + mtk_disp_pwm_update_bits(mdp->mmio_base + DISP_PWM_COMMIT, + PWM_COMMIT_MASK, 0); + + return 0; +} + +static int mtk_disp_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm) +{ + struct mtk_disp_pwm *mdp; + + mdp = to_mtk_disp_pwm(chip); + mtk_disp_pwm_update_bits(mdp->mmio_base + DISP_PWM_EN, + PWM_ENABLE_MASK, 1); + + return 0; +} + +static void mtk_disp_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm) +{ + struct mtk_disp_pwm *mdp; + + mdp = to_mtk_disp_pwm(chip); + mtk_disp_pwm_update_bits(mdp->mmio_base + DISP_PWM_EN, + PWM_ENABLE_MASK, 0); +} + +static const struct pwm_ops mtk_disp_pwm_ops = { + .config = mtk_disp_pwm_config, + .enable = mtk_disp_pwm_enable, + .disable = mtk_disp_pwm_disable, + .owner = THIS_MODULE, +}; + +static int mtk_disp_pwm_probe(struct platform_device *pdev) +{ + struct mtk_disp_pwm *mdp; + struct resource *r; + int ret; + + mdp = devm_kzalloc(&pdev->dev, sizeof(*mdp), GFP_KERNEL); + if (!mdp) + return -ENOMEM; + + mdp->dev = &pdev->dev; + + r = platform_get_resource(pdev, IORESOURCE_MEM, 0); + mdp->mmio_base = devm_ioremap_resource(&pdev->dev, r); + if (IS_ERR(mdp->mmio_base)) + return PTR_ERR(mdp->mmio_base); + + mdp->clk_main = devm_clk_get(&pdev->dev, "main"); + if (IS_ERR(mdp->clk_main)) + return PTR_ERR(mdp->clk_main); + + mdp->clk_mm = devm_clk_get(&pdev->dev, "mm"); + if (IS_ERR(mdp->clk_mm)) + return PTR_ERR(mdp->clk_mm); + + ret = clk_prepare_enable(mdp->clk_main); + if (ret < 0) + return ret; + + ret = clk_prepare_enable(mdp->clk_mm); + if (ret < 0) { + clk_disable_unprepare(mdp->clk_main); + return ret; + } + + platform_set_drvdata(pdev, mdp); + + mdp->chip.dev = &pdev->dev; + mdp->chip.ops = &mtk_disp_pwm_ops; + mdp->chip.base = -1; + mdp->chip.npwm = 1; + + ret = pwmchip_add(&mdp->chip); + if (ret < 0) { + dev_err(&pdev->dev, "pwmchip_add() failed: %d\n", ret); + clk_disable_unprepare(mdp->clk_main); + clk_disable_unprepare(mdp->clk_mm); + return ret; + } + + return 0; +} + +static int mtk_disp_pwm_remove(struct platform_device *pdev) +{ + struct mtk_disp_pwm *mdp; + int ret; + + mdp = platform_get_drvdata(pdev); + ret = pwmchip_remove(&mdp->chip); + clk_disable_unprepare(mdp->clk_main); + clk_disable_unprepare(mdp->clk_mm); + + return ret; +} + +static const struct of_device_id mtk_disp_pwm_of_match[] = { + { .compatible = "mediatek,mt8173-disp-pwm" }, + { .compatible = "mediatek,mt6595-disp-pwm" }, + { } +}; +MODULE_DEVICE_TABLE(of, mtk_disp_pwm_of_match); + +static struct platform_driver mtk_disp_pwm_driver = { + .driver = { + .name = "mediatek-disp-pwm", + .of_match_table = mtk_disp_pwm_of_match, + }, + .probe = mtk_disp_pwm_probe, + .remove = mtk_disp_pwm_remove, +}; +module_platform_driver(mtk_disp_pwm_driver); + +MODULE_AUTHOR("YH Huang "); +MODULE_DESCRIPTION("MediaTek SoC display PWM driver"); +MODULE_LICENSE("GPL v2");