From patchwork Thu Aug 29 21:31:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chris Morgan X-Patchwork-Id: 13783911 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 29595CA0EC4 for ; Thu, 29 Aug 2024 21:33:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=M7Y9irJ8Yxns+yvccXjYBMQbjwPySIIeELNHfgride8=; b=nVlur9dEuWQNm9 dOw44Pb/8WikOXOJdwlZAgH7GOzYDYbQM2J24Ch3CCZsNYVmg2emjLnC6+Q92OP+V5nXDQZ6RTU+A 1XcGRpG/ZDQg7XdOH1I9J3EyCRUddDPdeedeoJ1IC+YbBqamMH9S6hoovmXbHSwv5pCZwjW2qNm8a wFKBbq4qKgOMVM2F2e7kvsOUbH4277XRVQbqptjBoc4eSrIBsS/4JXYIZoUXIIwCjSrCn2Zx4R+RC 5fVDtv+cy+rV0MxUh1m562pZTwanfcoUcJOkzQlkOWehykcDe9XFURYoSl89n2h6zyfnESeBMserM OFvL8nK//+s35n7ZZRmQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sjmlv-00000003hzc-0MPu; Thu, 29 Aug 2024 21:33:39 +0000 Received: from mail-oi1-x22c.google.com ([2607:f8b0:4864:20::22c]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sjmll-00000003hvx-0Pbt for linux-rockchip@lists.infradead.org; Thu, 29 Aug 2024 21:33:34 +0000 Received: by mail-oi1-x22c.google.com with SMTP id 5614622812f47-3df07543ed5so768943b6e.0 for ; Thu, 29 Aug 2024 14:33:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1724967208; x=1725572008; darn=lists.infradead.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=o9Bsg4/Lc59ylXzJOYTXscF4ilwcLQTWT50V0HXE7Wc=; b=OzhK+70cipLVnmIqs3/U72X07RRo2i33IX5qdWRTn1A9iFCO2QnjRpjwT9gONok39j kqXgcejUuAir/BBUfJd23R9FHCnRd6qQMMU0jAkWChM3YRiS4Hjn9wVAQyUJZJ0jKJSx ZB0B/QMU6FRDGWrg5yAz71KvYpAcUC1p9RIFh/QD5dCPJFjNXbBV+oZDrzUcTgNz8A+M Hlv98+/sevt70uVzEpWpG1c9MzIv4vl4Spn5a0qjhLOAXlnJ/xvRNphrE1FYgiz4Bt4T 7jCpYxygHq8jd6xf3pIEaRQXYkQEHNgfdB2IvGBPwciM+9mZVGSrUCpWIy+eIKYHcXHh 7wGg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724967208; x=1725572008; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=o9Bsg4/Lc59ylXzJOYTXscF4ilwcLQTWT50V0HXE7Wc=; b=VGXKIsxttGmgcLREIkU69JTA+2uMKzyKtRQjpieO2MByaMuBYmTeRY5tOnPckGqByE frorsJ1wkW+L+TNa1ARVBudixvz8+HKebEcAtcQQJyQ9hABeIiPrfHtP6z3CudBSRMHn vr4wWkuZwazKdrv4MkYuzN+rFBaLSNMid9MNcnObIAEpWLX0IoyTC5JkIpLpwjAVmhCe ZK1NBwvsCCTifeJBG5PcJHIvFVFEfC4YGIzMH3ixAhymbhMFXhsXer+LSemRrLH3MsQC ldwaUTCAXKbcj9BrI72NX3B6byxpvG6aUC8/79eflvAa5L11EH3HZcBDGSW3LUMREx9A E56g== X-Gm-Message-State: AOJu0YzFOebfRryKjSnV7DywCu57VYPVwvUEMdmFV+Yfq3n+uIIo2arM LNsmKjYuccwugFcQ8P0cc3Jb1M1GaEazRFn34VdiRuCGDlqbH8wssmVxtA== X-Google-Smtp-Source: AGHT+IGbBwELby/4rWJTFVjlu8moFYPhiYhGSJn6fxLl+Zyci2k5LzN1h0MUN+05zq1u1fAMZ/u/WQ== X-Received: by 2002:a05:6808:2f0d:b0:3dd:807:d587 with SMTP id 5614622812f47-3df05d701bdmr5511549b6e.16.1724967207727; Thu, 29 Aug 2024 14:33:27 -0700 (PDT) Received: from localhost.localdomain ([2600:1700:fb0:1bcf::54]) by smtp.gmail.com with ESMTPSA id 5614622812f47-3df11865de3sm93097b6e.42.2024.08.29.14.33.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 29 Aug 2024 14:33:27 -0700 (PDT) From: Chris Morgan To: linux-pm@vger.kernel.org Cc: linux-rockchip@lists.infradead.org, devicetree@vger.kernel.org, broonie@kernel.org, lgirdwood@gmail.com, sre@kernel.org, heiko@sntech.de, conor+dt@kernel.org, krzk+dt@kernel.org, robh@kernel.org, lee@kernel.org, Chris Morgan Subject: [RFC 4/5] regulator: bq257xx: Add bq257xx boost regulator driver Date: Thu, 29 Aug 2024 16:31:01 -0500 Message-Id: <20240829213102.448047-5-macroalpha82@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240829213102.448047-1-macroalpha82@gmail.com> References: <20240829213102.448047-1-macroalpha82@gmail.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240829_143329_177589_B4802220 X-CRM114-Status: GOOD ( 23.97 ) X-BeenThere: linux-rockchip@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Upstream kernel work for Rockchip platforms List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "Linux-rockchip" Errors-To: linux-rockchip-bounces+linux-rockchip=archiver.kernel.org@lists.infradead.org From: Chris Morgan Add support for the boost regulator found in the Texas Instruments BQ25703. The boost regulator is capable of outputting between 4.48 and 20.8 volts and between 0 and 6.35 amps. Signed-off-by: Chris Morgan --- drivers/regulator/Kconfig | 8 ++ drivers/regulator/Makefile | 1 + drivers/regulator/bq257xx-regulator.c | 195 ++++++++++++++++++++++++++ 3 files changed, 204 insertions(+) create mode 100644 drivers/regulator/bq257xx-regulator.c diff --git a/drivers/regulator/Kconfig b/drivers/regulator/Kconfig index 4b411a09c1a6..db432e3fb37e 100644 --- a/drivers/regulator/Kconfig +++ b/drivers/regulator/Kconfig @@ -286,6 +286,14 @@ config REGULATOR_BD96801 This driver can also be built as a module. If so, the module will be called bd96801-regulator. +config REGULATOR_BQ257XX + tristate "TI BQ257XX regulator family" + depends on MFD_BQ257XX + depends on GPIOLIB || COMPILE_TEST + help + Say Y to enable support for the boost regulator function of + the BQ257XX family of charger circuits. + config REGULATOR_CPCAP tristate "Motorola CPCAP regulator" depends on MFD_CPCAP diff --git a/drivers/regulator/Makefile b/drivers/regulator/Makefile index a61fa42b13c4..6e8cdb0f554f 100644 --- a/drivers/regulator/Makefile +++ b/drivers/regulator/Makefile @@ -37,6 +37,7 @@ obj-$(CONFIG_REGULATOR_BD71828) += bd71828-regulator.o obj-$(CONFIG_REGULATOR_BD718XX) += bd718x7-regulator.o obj-$(CONFIG_REGULATOR_BD9571MWV) += bd9571mwv-regulator.o obj-$(CONFIG_REGULATOR_BD957XMUF) += bd9576-regulator.o +obj-$(CONFIG_REGULATOR_BQ257XX) += bq257xx-regulator.o obj-$(CONFIG_REGULATOR_DA903X) += da903x-regulator.o obj-$(CONFIG_REGULATOR_BD96801) += bd96801-regulator.o obj-$(CONFIG_REGULATOR_DA9052) += da9052-regulator.o diff --git a/drivers/regulator/bq257xx-regulator.c b/drivers/regulator/bq257xx-regulator.c new file mode 100644 index 000000000000..9246b091288c --- /dev/null +++ b/drivers/regulator/bq257xx-regulator.c @@ -0,0 +1,195 @@ +// SPDX-License-Identifier: GPL-2.0 +/* BQ257XX Battery Charger Driver + * Copyright (C) 2024 Chris Morgan + * Based off of BQ256XX Battery Charger driver and + * RK808 Regulator driver. + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +struct bq257xx_reg_data { + struct bq257xx_device *bq; + struct regulator_dev *bq257xx_reg; + struct gpio_desc *otg_en_gpio; + struct regulator_desc desc; +}; + +static int bq25703_vbus_get_cur_limit(struct regulator_dev *rdev) +{ + struct bq257xx_reg_data *pdata = rdev_get_drvdata(rdev); + int ret; + unsigned int reg; + + ret = regmap_read(pdata->bq->regmap, BQ25703_OTG_CURRENT, ®); + if (ret) + return ret; + return FIELD_GET(BQ25703_OTG_CUR_MASK, reg) * BQ25703_OTG_CUR_STEP_UA; +} + +/* + * Check if the minimum current and maximum current requested are + * sane values, then set the register accordingly. + */ +static int bq25703_vbus_set_cur_limit(struct regulator_dev *rdev, + int min_uA, int max_uA) +{ + struct bq257xx_reg_data *pdata = rdev_get_drvdata(rdev); + unsigned int reg; + + if ((min_uA > BQ25703_OTG_CUR_MAX_UA) || (max_uA < 0)) + return -EINVAL; + + reg = (max_uA / BQ25703_OTG_CUR_STEP_UA); + + /* Catch rounding errors since our step is 50000uA. */ + if ((reg * BQ25703_OTG_CUR_STEP_UA) < min_uA) + return -EINVAL; + + return regmap_write(pdata->bq->regmap, BQ25703_OTG_CURRENT, + FIELD_PREP(BQ25703_OTG_CUR_MASK, reg)); +} + +static int bq25703_vbus_enable(struct regulator_dev *rdev) +{ + struct bq257xx_reg_data *pdata = rdev_get_drvdata(rdev); + + if (pdata->otg_en_gpio) + gpiod_set_value_cansleep(pdata->otg_en_gpio, 1); + return regulator_enable_regmap(rdev); +} + +static int bq25703_vbus_disable(struct regulator_dev *rdev) +{ + struct bq257xx_reg_data *pdata = rdev_get_drvdata(rdev); + + if (pdata->otg_en_gpio) + gpiod_set_value_cansleep(pdata->otg_en_gpio, 0); + return regulator_disable_regmap(rdev); +} + +static const struct regulator_ops bq25703_vbus_ops = { + .enable = bq25703_vbus_enable, + .disable = bq25703_vbus_disable, + .is_enabled = regulator_is_enabled_regmap, + .list_voltage = regulator_list_voltage_linear, + .get_voltage_sel = regulator_get_voltage_sel_regmap, + .set_voltage_sel = regulator_set_voltage_sel_regmap, + .get_current_limit = bq25703_vbus_get_cur_limit, + .set_current_limit = bq25703_vbus_set_cur_limit, +}; + +static const struct regulator_desc bq25703_vbus_desc = { + .name = "usb_otg_vbus", + .of_match = of_match_ptr("usb-otg-vbus"), + .regulators_node = of_match_ptr("regulators"), + .type = REGULATOR_VOLTAGE, + .owner = THIS_MODULE, + .ops = &bq25703_vbus_ops, + .min_uV = BQ25703_OTG_VOLT_MIN_UV, + .uV_step = BQ25703_OTG_VOLT_STEP_UV, + .n_voltages = BQ25703_OTG_VOLT_NUM_VOLT, + .enable_mask = BQ25703_EN_OTG_MASK, + .enable_reg = BQ25703_CHARGE_OPTION_3, + .enable_val = BQ25703_EN_OTG_MASK, + .disable_val = 0, + .vsel_reg = BQ25703_OTG_VOLT, + .vsel_mask = BQ25703_OTG_VOLT_MASK, +}; + +/* Get optional GPIO for OTG regulator enable. */ +static void bq257xx_reg_dt_parse_gpio(struct platform_device *pdev) +{ + struct device_node *child, *subchild; + struct bq257xx_reg_data *pdata = platform_get_drvdata(pdev); + + child = of_get_child_by_name(pdev->dev.of_node, + pdata->desc.regulators_node); + if (!child) + return; + + subchild = of_get_child_by_name(child, pdata->desc.of_match); + if (!subchild) + return; + + of_node_put(child); + + pdata->otg_en_gpio = devm_fwnode_gpiod_get_index(&pdev->dev, + of_fwnode_handle(subchild), + "enable", 0, + GPIOD_OUT_LOW, + pdata->desc.of_match); + + of_node_put(subchild); + + if (IS_ERR_OR_NULL(pdata->otg_en_gpio)) { + dev_err(&pdev->dev, "Error getting enable gpio: %ld\n", + PTR_ERR(pdata->otg_en_gpio)); + } +} + +static int bq257xx_regulator_probe(struct platform_device *pdev) +{ + struct device *dev = &pdev->dev; + struct bq257xx_device *bq = dev_get_drvdata(pdev->dev.parent); + struct bq257xx_reg_data *pdata; + struct device_node *np = dev->of_node; + struct regulator_init_data *init_data; + struct regulator_config cfg = {}; + int ret; + + pdev->dev.of_node = pdev->dev.parent->of_node; + pdev->dev.of_node_reused = true; + + pdata = devm_kzalloc(&pdev->dev, sizeof(struct bq257xx_reg_data), GFP_KERNEL); + if (!pdata) + return -ENOMEM; + + pdata->bq = bq; + + switch (pdata->bq->variant) { + case BQ25703A: + pdata->desc = bq25703_vbus_desc; + break; + default: + return -EINVAL; + } + + platform_set_drvdata(pdev, pdata); + bq257xx_reg_dt_parse_gpio(pdev); + + cfg.dev = &pdev->dev; + cfg.init_data = init_data; + cfg.driver_data = pdata; + cfg.of_node = np; + cfg.regmap = dev_get_regmap(pdev->dev.parent, NULL); + if (!cfg.regmap) + return -ENODEV; + + pdata->bq257xx_reg = devm_regulator_register(dev, &pdata->desc, &cfg); + if (IS_ERR(pdata->bq257xx_reg)) { + return dev_err_probe(&pdev->dev, PTR_ERR(pdata->bq257xx_reg), + "error registering bq257xx regulator"); + } + + return ret; +} + +static struct platform_driver bq257xx_reg_driver = { + .driver = { + .name = "bq257xx-regulator", + }, + .probe = bq257xx_regulator_probe, +}; + +module_platform_driver(bq257xx_reg_driver); + +MODULE_DESCRIPTION("bq257xx regulator driver"); +MODULE_AUTHOR("Chris Morgan "); +MODULE_LICENSE("GPL");