From patchwork Mon Aug 28 12:58:52 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fabrice Gasnier X-Patchwork-Id: 9925509 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 9C6AF60383 for ; Mon, 28 Aug 2017 13:09:54 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8B8C1286C1 for ; Mon, 28 Aug 2017 13:09:54 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 7E80C286CC; Mon, 28 Aug 2017 13:09:54 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.6 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_LOW autolearn=unavailable version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [65.50.211.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id CAA15286C4 for ; Mon, 28 Aug 2017 13:09:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=pKc3msKWVrQnqSvS5h+wNvQfAySAEWsxtVFVBQLcrzU=; b=mg8o2BZxNrbDHW PNvRCx3MxQpxoKKZ8EzWfjQTpIIO2G4G7GgpUGBasuI6/zneea4gw4R8lkPUJFc5B+rn6I/C//iYv if7QAjW1Ut8l3AORJt8RNqo6GSCIXtC75zjuF2vog01PR8+k9OZXkJhoLTlH/KWyBSEGma2jdMJHR vMEuBPxxfVcGn2bLZl8yT4Ufggi6NYKgKx/wtdVdIQLZ2EJ25qgO1WQ6DqjQs6vKM/yUmWQzHctwl x4QBoU7dG1XoXy8nQxmBBckQU+HWsRjwZ2ngGp5gQ0DzQL5oEEW7lu3GXcjfL+lm5vjWx+3xBHz4G 6ha3xyilgJyeyg/Mj4Fg==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.87 #1 (Red Hat Linux)) id 1dmJnF-0001vF-Cy; Mon, 28 Aug 2017 13:09:29 +0000 Received: from mx07-00178001.pphosted.com ([62.209.51.94]) by bombadil.infradead.org with esmtps (Exim 4.87 #1 (Red Hat Linux)) id 1dmJdv-0004be-O2 for linux-arm-kernel@lists.infradead.org; Mon, 28 Aug 2017 13:00:07 +0000 Received: from pps.filterd (m0046037.ppops.net [127.0.0.1]) by mx07-.pphosted.com (8.16.0.21/8.16.0.21) with SMTP id v7SCsRd4007931; Mon, 28 Aug 2017 14:59:10 +0200 Received: from beta.dmz-eu.st.com (beta.dmz-eu.st.com [164.129.1.35]) by mx07-.pphosted.com with ESMTP id 2cjxrqt8kt-1 (version=TLSv1 cipher=ECDHE-RSA-AES256-SHA bits=256 verify=NOT); Mon, 28 Aug 2017 14:59:10 +0200 Received: from zeta.dmz-eu.st.com (zeta.dmz-eu.st.com [164.129.230.9]) by beta.dmz-eu.st.com (STMicroelectronics) with ESMTP id BF84741; Mon, 28 Aug 2017 12:59:09 +0000 (GMT) Received: from Webmail-eu.st.com (sfhdag5node3.st.com [10.75.127.15]) by zeta.dmz-eu.st.com (STMicroelectronics) with ESMTP id 9ADA02599; Mon, 28 Aug 2017 12:59:09 +0000 (GMT) Received: from localhost (10.75.127.47) by SFHDAG5NODE3.st.com (10.75.127.15) with Microsoft SMTP Server (TLS) id 15.0.1178.4; Mon, 28 Aug 2017 14:59:09 +0200 From: Fabrice Gasnier To: , , , Subject: [RESEND PATCH 2/3] regulator: Add support for stm32-vrefbuf Date: Mon, 28 Aug 2017 14:58:52 +0200 Message-ID: <1503925133-30722-3-git-send-email-fabrice.gasnier@st.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1503925133-30722-1-git-send-email-fabrice.gasnier@st.com> References: <1503925133-30722-1-git-send-email-fabrice.gasnier@st.com> MIME-Version: 1.0 X-Originating-IP: [10.75.127.47] X-ClientProxiedBy: SFHDAG3NODE1.st.com (10.75.127.7) To SFHDAG5NODE3.st.com (10.75.127.15) X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:, , definitions=2017-08-28_06:, , signatures=0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20170828_055952_263260_A2879CBA X-CRM114-Status: GOOD ( 17.54 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: mark.rutland@arm.com, devicetree@vger.kernel.org, linux@armlinux.org.uk, linux-kernel@vger.kernel.org, mcoquelin.stm32@gmail.com, fabrice.gasnier@st.com, linux-arm-kernel@lists.infradead.org Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP Add regulator driver for STM32 voltage reference buffer which can be used as voltage reference for ADCs, DACs and external components through dedicated VREF+ pin. Signed-off-by: Fabrice Gasnier --- drivers/regulator/Kconfig | 12 +++ drivers/regulator/Makefile | 1 + drivers/regulator/stm32-vrefbuf.c | 215 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 228 insertions(+) create mode 100644 drivers/regulator/stm32-vrefbuf.c diff --git a/drivers/regulator/Kconfig b/drivers/regulator/Kconfig index 99b9362..2cb2c63 100644 --- a/drivers/regulator/Kconfig +++ b/drivers/regulator/Kconfig @@ -746,6 +746,18 @@ config REGULATOR_SKY81452 This driver can also be built as a module. If so, the module will be called sky81452-regulator. +config REGULATOR_STM32_VREFBUF + tristate "STMicroelectronics STM32 VREFBUF" + depends on ARCH_STM32 || COMPILE_TEST + help + This driver supports STMicroelectronics STM32 VREFBUF (voltage + reference buffer) which can be used as voltage reference for + internal ADCs, DACs and also for external components through + dedicated Vref+ pin. + + This driver can also be built as a module. If so, the module + will be called stm32-vrefbuf. + config REGULATOR_TI_ABB tristate "TI Adaptive Body Bias on-chip LDO" depends on ARCH_OMAP diff --git a/drivers/regulator/Makefile b/drivers/regulator/Makefile index 95b1e86..5af3788 100644 --- a/drivers/regulator/Makefile +++ b/drivers/regulator/Makefile @@ -94,6 +94,7 @@ obj-$(CONFIG_REGULATOR_S2MPA01) += s2mpa01.o obj-$(CONFIG_REGULATOR_S2MPS11) += s2mps11.o obj-$(CONFIG_REGULATOR_S5M8767) += s5m8767.o obj-$(CONFIG_REGULATOR_SKY81452) += sky81452-regulator.o +obj-$(CONFIG_REGULATOR_STM32_VREFBUF) += stm32-vrefbuf.o obj-$(CONFIG_REGULATOR_STW481X_VMMC) += stw481x-vmmc.o obj-$(CONFIG_REGULATOR_TI_ABB) += ti-abb-regulator.o obj-$(CONFIG_REGULATOR_TPS6105X) += tps6105x-regulator.o diff --git a/drivers/regulator/stm32-vrefbuf.c b/drivers/regulator/stm32-vrefbuf.c new file mode 100644 index 0000000..a4efa3b --- /dev/null +++ b/drivers/regulator/stm32-vrefbuf.c @@ -0,0 +1,215 @@ +/* + * Copyright (C) STMicroelectronics 2017 + * + * Author: Fabrice Gasnier + * + * License terms: GNU General Public License (GPL), version 2 + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* STM32 VREFBUF registers */ +#define STM32_VREFBUF_CSR 0x00 + +/* STM32 VREFBUF CSR bitfields */ +#define STM32_VRS GENMASK(6, 4) +#define STM32_VRR BIT(3) +#define STM32_HIZ BIT(1) +#define STM32_ENVR BIT(0) + +struct stm32_vrefbuf { + void __iomem *base; + struct clk *clk; +}; + +static const unsigned int stm32_vrefbuf_voltages[] = { + /* Matches resp. VRS = 000b, 001b, 010b, 011b */ + 2500000, 2048000, 1800000, 1500000, +}; + +static int stm32_vrefbuf_enable(struct regulator_dev *rdev) +{ + struct stm32_vrefbuf *priv = rdev_get_drvdata(rdev); + u32 val = readl_relaxed(priv->base + STM32_VREFBUF_CSR); + int ret; + + val = (val & ~STM32_HIZ) | STM32_ENVR; + writel_relaxed(val, priv->base + STM32_VREFBUF_CSR); + + /* + * Vrefbuf startup time depends on external capacitor: wait here for + * VRR to be set. That means output has reached expected value. + * ~650us sleep should be enough for caps up to 1.5uF. Use 10ms as + * arbitrary timeout. + */ + ret = readl_poll_timeout(priv->base + STM32_VREFBUF_CSR, val, + !(val & STM32_VRR), 650, 10000); + if (ret) { + dev_err(&rdev->dev, "stm32 vrefbuf timed out!\n"); + val = readl_relaxed(priv->base + STM32_VREFBUF_CSR); + val = (val & ~STM32_ENVR) | STM32_HIZ; + writel_relaxed(val, priv->base + STM32_VREFBUF_CSR); + } + + return ret; +} + +static int stm32_vrefbuf_disable(struct regulator_dev *rdev) +{ + struct stm32_vrefbuf *priv = rdev_get_drvdata(rdev); + u32 val = readl_relaxed(priv->base + STM32_VREFBUF_CSR); + + val = (val & ~STM32_ENVR) | STM32_HIZ; + writel_relaxed(val, priv->base + STM32_VREFBUF_CSR); + + return 0; +} + +static int stm32_vrefbuf_is_enabled(struct regulator_dev *rdev) +{ + struct stm32_vrefbuf *priv = rdev_get_drvdata(rdev); + + return readl_relaxed(priv->base + STM32_VREFBUF_CSR) & STM32_ENVR; +} + +static int stm32_vrefbuf_set_voltage_sel(struct regulator_dev *rdev, + unsigned sel) +{ + struct stm32_vrefbuf *priv = rdev_get_drvdata(rdev); + u32 val = readl_relaxed(priv->base + STM32_VREFBUF_CSR); + + val = (val & ~STM32_VRS) | FIELD_PREP(STM32_VRS, sel); + writel_relaxed(val, priv->base + STM32_VREFBUF_CSR); + + return 0; +} + +static int stm32_vrefbuf_get_voltage_sel(struct regulator_dev *rdev) +{ + struct stm32_vrefbuf *priv = rdev_get_drvdata(rdev); + u32 val = readl_relaxed(priv->base + STM32_VREFBUF_CSR); + + return FIELD_GET(STM32_VRS, val); +} + +static const struct regulator_ops stm32_vrefbuf_volt_ops = { + .enable = stm32_vrefbuf_enable, + .disable = stm32_vrefbuf_disable, + .is_enabled = stm32_vrefbuf_is_enabled, + .get_voltage_sel = stm32_vrefbuf_get_voltage_sel, + .set_voltage_sel = stm32_vrefbuf_set_voltage_sel, + .list_voltage = regulator_list_voltage_table, +}; + +static const struct regulator_desc stm32_vrefbuf_regu = { + .name = "vref", + .supply_name = "vdda", + .volt_table = stm32_vrefbuf_voltages, + .n_voltages = ARRAY_SIZE(stm32_vrefbuf_voltages), + .ops = &stm32_vrefbuf_volt_ops, + .type = REGULATOR_VOLTAGE, + .owner = THIS_MODULE, +}; + +static int stm32_vrefbuf_probe(struct platform_device *pdev) +{ + struct resource *res; + struct stm32_vrefbuf *priv; + struct regulator_config config = { }; + struct regulator_dev *rdev; + int ret; + + priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL); + if (!priv) + return -ENOMEM; + + res = platform_get_resource(pdev, IORESOURCE_MEM, 0); + priv->base = devm_ioremap_resource(&pdev->dev, res); + if (IS_ERR(priv->base)) + return PTR_ERR(priv->base); + + priv->clk = devm_clk_get(&pdev->dev, NULL); + if (IS_ERR(priv->clk)) + return PTR_ERR(priv->clk); + + ret = clk_prepare_enable(priv->clk); + if (ret) { + dev_err(&pdev->dev, "clk prepare failed\n"); + return ret; + } + + config.dev = &pdev->dev; + config.driver_data = priv; + config.of_node = pdev->dev.of_node; + config.init_data = of_get_regulator_init_data(&pdev->dev, + pdev->dev.of_node, + &stm32_vrefbuf_regu); + + rdev = regulator_register(&stm32_vrefbuf_regu, &config); + if (IS_ERR(rdev)) { + dev_err(&pdev->dev, "vrefbuf register failed\n"); + ret = PTR_ERR(rdev); + goto err_clk_dis; + } + platform_set_drvdata(pdev, rdev); + + dev_info(&pdev->dev, "STM32 VREFBUF initialized\n"); + + return 0; + +err_clk_dis: + clk_disable_unprepare(priv->clk); + + return ret; +} + +static int stm32_vrefbuf_remove(struct platform_device *pdev) +{ + struct regulator_dev *rdev = platform_get_drvdata(pdev); + struct stm32_vrefbuf *priv = rdev_get_drvdata(rdev); + + regulator_unregister(rdev); + clk_disable_unprepare(priv->clk); + + return 0; +}; + +static const struct of_device_id stm32_vrefbuf_of_match[] = { + { .compatible = "st,stm32-vrefbuf", }, + {}, +}; +MODULE_DEVICE_TABLE(of, stm32_vrefbuf_of_match); + +static struct platform_driver stm32_vrefbuf_driver = { + .probe = stm32_vrefbuf_probe, + .remove = stm32_vrefbuf_remove, + .driver = { + .name = "stm32-vrefbuf", + .of_match_table = of_match_ptr(stm32_vrefbuf_of_match), + }, +}; + +static int __init stm32_vrefbuf_init(void) +{ + return platform_driver_register(&stm32_vrefbuf_driver); +} +subsys_initcall(stm32_vrefbuf_init); + +static void __exit stm32_vrefbuf_exit(void) +{ + platform_driver_unregister(&stm32_vrefbuf_driver); +} +module_exit(stm32_vrefbuf_exit) + +MODULE_LICENSE("GPL v2"); +MODULE_AUTHOR("Fabrice Gasnier "); +MODULE_DESCRIPTION("STMicroelectronics STM32 VREFBUF driver"); +MODULE_ALIAS("platform:stm32-vrefbuf");