Received: by 2002:ab2:6991:0:b0:1f7:f6c3:9cb1 with SMTP id v17csp1245873lqo; Thu, 9 May 2024 08:31:36 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXTQTs9xtnIC3hqRPzTWnT5AgPvC7CeNXXIVhf0A9tLaLQqx+K2BA2qs3AyCL4KeORukEsOLkSh+wvjBhpmakqc9jSaDeH9t+DSrdTEhA== X-Google-Smtp-Source: AGHT+IFMph5MjsDUCgEITSUwOeobXjeWHOO/z6lmprt//goeZ1WCdS6+QmE3DRrp/iX91hvegYpF X-Received: by 2002:a05:6870:5b8f:b0:23f:2cb:7c12 with SMTP id 586e51a60fabf-240981fd8e1mr6810239fac.27.1715268696162; Thu, 09 May 2024 08:31:36 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1715268696; cv=pass; d=google.com; s=arc-20160816; b=xiyKRvi9rDRKQIubCMx59yIoigq/h9VjqUVCRcKuSjFP8YzGkpF5Eh4xFq2i65IOFp jt3TMVAUnIpJmVZmzU3fflIIMxKVjHc9Q6FX6JGq9ekn/2KmUEfHlHy9+8nWZ6HdeoAC VIp7HuNJYL0zVK0EXHoTx45CrInr5FQBlFC9FZG9daPPkGesEsA3zQKB0g4nbb2eRovA NiX+jph1DCxgV91noRMS9zxpD2jlv9LLgnbCdYU7KJYa3ddhCe6Aj1Gq3AdmSUlFequ+ 1kb07NmdihOenyYJo9aNNMz/nvNK1KUY4aIA1GzNdgwhE78izybmhrxmm4QqI9WFktod s3lg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=VhkMuvrSmewCFv9nWstAIIyvWWF9nwIJA+JDNp2RDHk=; fh=1U0KR8irj7zGSDz2C//LyxqJQUSLTEsVSzBGLjr+RXU=; b=rqtr4rEfFxsa8ZdT+O1hWMoYzOKRAdZXKYZhOYXEXpr2tV+rMSnQ5gm4aBR5XeNYkS uiixcBfcHIOmCauYJ8W1YxQaWGbScyucXddZSIupssOlXZAUIJ/6OiaguXPhzQ2gzB66 v+GpZf2UkIq1otdrm7mCC2DodHQflKpGsrb1SvOadWd71unvTBfue3y3+4QJfKHOlBn3 3dFN+Tw08SAS7748hsqSuuAxm67wt2+dg2nlPK4FtWR8440p+NXFcv/UJMHxzTh/VOcP TF7RXwmWPAtdPtualxng8H//evjc6kxvXp06OHsWYsY8jC0Y2x4sqCevauHGjg6Npvy0 UQ8A==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="kRya/aK2"; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-174637-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-174637-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id d75a77b69052e-43df56c21c8si15817421cf.617.2024.05.09.08.31.35 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 May 2024 08:31:36 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-174637-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="kRya/aK2"; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-174637-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-174637-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id CDE6A1C20EF7 for ; Thu, 9 May 2024 15:31:35 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id B29C716F838; Thu, 9 May 2024 15:31:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="kRya/aK2" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9F39A16F0E0; Thu, 9 May 2024 15:31:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715268676; cv=none; b=AagUb+UdwOVR2pYNwtDsTMAPQAcj6oEbPpv5ArSAU0EiwPf8gyrFHmZaeM1d1ocxEVN2CMKBphA1r6mPhG6z2Dn3AUx5u+XeyjGnFqeqrn5lVSjOIgmNc1HASIF7s5qJvlE/sSvKSjSImueayT603jEUikKBzLQcguP6NtbO35M= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715268676; c=relaxed/simple; bh=6dJFzoz5/zPfJ2iN6WbR60T4ax8pQQwJWw+X5KaXXVM=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=RqvQQnqS/46zOmGk9gsK5VdKbne6JUx9eAY6dZpjvFx4Zh9O6v/Y4f4Gw7722RXJ4OmAqerj5Yng+gAVBC27Axn/iIEHAILC1It2XUQhjervp/8gl63WvFLmd01odE1AhrpknbegewlMfDH8DDV9KlNSEDGdldIkFaVuHJ4YYPo= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=kRya/aK2; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 256F8C4AF0B; Thu, 9 May 2024 15:31:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1715268676; bh=6dJFzoz5/zPfJ2iN6WbR60T4ax8pQQwJWw+X5KaXXVM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=kRya/aK2RBV9u5OnjE/IwaEJG7bykOEM9DXVA/obwlUPNN0NTHgsBY+8B8V3bOljW D3PZzukHqR7s+Q0qujXF8jQR5Bu+BAy/BQCR3Yzt4VKAhx31Ytu5nqduqPt59PrJEk BzOPKmDLQ49Hsytwea0qHLHilTsFsOVqp8IFVgn41X0iXiofWJzth6INYNWbQh9NEb 9ZbY/hGReOUWR9ezk0iaanZeKdgQjSbPp3PNWINLMDDT7EayWHBMuZapN5h8kHs/MN 3xmSqn7ZgYMmt4m6pz+nF84/GYPJyGv3HLv2xtKE21oifKoSQP6E9YemOt0Y8O7C8q a+5Th+alpxFWQ== Received: by wens.tw (Postfix, from userid 1000) id BB4A95FD4B; Thu, 9 May 2024 23:31:13 +0800 (CST) From: Chen-Yu Tsai To: Mark Brown Cc: Samuel Holland , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Jernej Skrabec , linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-sunxi@lists.linux.dev, Andre Przywara , Chen-Yu Tsai Subject: [PATCH v6 2/2] regulator: sun20i: Add Allwinner D1 LDOs driver Date: Thu, 9 May 2024 23:31:07 +0800 Message-Id: <20240509153107.438220-3-wens@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240509153107.438220-1-wens@kernel.org> References: <20240509153107.438220-1-wens@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Samuel Holland D1 contains two pairs of LDOs, "analog" LDOs and "system" LDOs. They are similar and can share a driver, but only the system LDOs have a DT binding defined so far. The system LDOs have a single linear range. The voltage step is not an integer, so a custom .list_voltage is needed to get the rounding right. Signed-off-by: Samuel Holland Reviewed-by: Andre Przywara Reviewed-by: Jernej Skrabec Signed-off-by: Chen-Yu Tsai --- drivers/regulator/Kconfig | 8 ++ drivers/regulator/Makefile | 1 + drivers/regulator/sun20i-regulator.c | 157 +++++++++++++++++++++++++++ 3 files changed, 166 insertions(+) create mode 100644 drivers/regulator/sun20i-regulator.c diff --git a/drivers/regulator/Kconfig b/drivers/regulator/Kconfig index 7db0a29b5b8d..acdb02a4ac0c 100644 --- a/drivers/regulator/Kconfig +++ b/drivers/regulator/Kconfig @@ -1415,6 +1415,14 @@ config REGULATOR_STW481X_VMMC This driver supports the internal VMMC regulator in the STw481x PMIC chips. +config REGULATOR_SUN20I + tristate "Allwinner D1 internal LDOs" + depends on ARCH_SUNXI || COMPILE_TEST + select MFD_SYSCON + default ARCH_SUNXI + help + This driver supports the internal LDOs in the Allwinner D1 SoC. + config REGULATOR_SY7636A tristate "Silergy SY7636A voltage regulator" depends on MFD_SY7636A diff --git a/drivers/regulator/Makefile b/drivers/regulator/Makefile index 46fb569e6be8..ba15fa5f30ad 100644 --- a/drivers/regulator/Makefile +++ b/drivers/regulator/Makefile @@ -163,6 +163,7 @@ obj-$(CONFIG_REGULATOR_STM32_VREFBUF) += stm32-vrefbuf.o obj-$(CONFIG_REGULATOR_STM32_PWR) += stm32-pwr.o obj-$(CONFIG_REGULATOR_STPMIC1) += stpmic1_regulator.o obj-$(CONFIG_REGULATOR_STW481X_VMMC) += stw481x-vmmc.o +obj-$(CONFIG_REGULATOR_SUN20I) += sun20i-regulator.o obj-$(CONFIG_REGULATOR_SY7636A) += sy7636a-regulator.o obj-$(CONFIG_REGULATOR_SY8106A) += sy8106a-regulator.o obj-$(CONFIG_REGULATOR_SY8824X) += sy8824x.o diff --git a/drivers/regulator/sun20i-regulator.c b/drivers/regulator/sun20i-regulator.c new file mode 100644 index 000000000000..e09be44859e7 --- /dev/null +++ b/drivers/regulator/sun20i-regulator.c @@ -0,0 +1,157 @@ +// SPDX-License-Identifier: GPL-2.0-only +// +// Copyright (c) 2021-2022 Samuel Holland +// + +#include +#include +#include +#include +#include +#include +#include + +#define SUN20I_SYS_LDO_CTRL_REG 0x150 + +struct sun20i_regulator_data { + const struct regulator_desc *descs; + unsigned int ndescs; +}; + +/* regulator_list_voltage_linear() modified for the non-integral uV_step. */ +static int sun20i_d1_system_ldo_list_voltage(struct regulator_dev *rdev, + unsigned int selector) +{ + const struct regulator_desc *desc = rdev->desc; + unsigned int fraction, uV; + + if (selector >= desc->n_voltages) + return -EINVAL; + + uV = desc->min_uV + (desc->uV_step * selector); + fraction = selector + (desc->min_uV % 4); + + if (uV > 1606667) + uV += 6667; + else + fraction++; + + /* Produce correctly-rounded absolute voltages. */ + return uV + (fraction / 3); +} + +static const struct regulator_ops sun20i_d1_system_ldo_ops = { + .list_voltage = sun20i_d1_system_ldo_list_voltage, + .map_voltage = regulator_map_voltage_ascend, + .set_voltage_sel = regulator_set_voltage_sel_regmap, + .get_voltage_sel = regulator_get_voltage_sel_regmap, +}; + +static const struct regulator_desc sun20i_d1_system_ldo_descs[] = { + { + .name = "ldoa", + .supply_name = "ldo-in", + .of_match = "ldoa", + .ops = &sun20i_d1_system_ldo_ops, + .type = REGULATOR_VOLTAGE, + .owner = THIS_MODULE, + .n_voltages = 32, + .min_uV = 1593333, + .uV_step = 13333, /* repeating */ + .vsel_reg = SUN20I_SYS_LDO_CTRL_REG, + .vsel_mask = GENMASK(7, 0), + }, + { + .name = "ldob", + .supply_name = "ldo-in", + .of_match = "ldob", + .ops = &sun20i_d1_system_ldo_ops, + .type = REGULATOR_VOLTAGE, + .owner = THIS_MODULE, + .n_voltages = 64, + .min_uV = 1166666, + .uV_step = 13333, /* repeating */ + .vsel_reg = SUN20I_SYS_LDO_CTRL_REG, + .vsel_mask = GENMASK(15, 8), + }, +}; + +static const struct sun20i_regulator_data sun20i_d1_system_ldos = { + .descs = sun20i_d1_system_ldo_descs, + .ndescs = ARRAY_SIZE(sun20i_d1_system_ldo_descs), +}; + +static struct regmap *sun20i_regulator_get_regmap(struct device *dev) +{ + struct regmap *regmap; + + /* + * First try the syscon interface. The system control device is not + * compatible with "syscon", so fall back to getting the regmap from + * its platform device. This is ugly, but required for devicetree + * backward compatibility. + */ + regmap = syscon_node_to_regmap(dev->parent->of_node); + if (!IS_ERR(regmap)) + return regmap; + + regmap = dev_get_regmap(dev->parent, NULL); + if (regmap) + return regmap; + + return ERR_PTR(-EPROBE_DEFER); +} + +static int sun20i_regulator_probe(struct platform_device *pdev) +{ + const struct sun20i_regulator_data *data; + struct device *dev = &pdev->dev; + struct regulator_config config; + struct regmap *regmap; + + data = of_device_get_match_data(dev); + if (!data) + return -EINVAL; + + regmap = sun20i_regulator_get_regmap(dev); + if (IS_ERR(regmap)) + return dev_err_probe(dev, PTR_ERR(regmap), "Failed to get regmap\n"); + + config = (struct regulator_config) { + .dev = dev, + .regmap = regmap, + }; + + for (unsigned int i = 0; i < data->ndescs; ++i) { + const struct regulator_desc *desc = &data->descs[i]; + struct regulator_dev *rdev; + + rdev = devm_regulator_register(dev, desc, &config); + if (IS_ERR(rdev)) + return PTR_ERR(rdev); + } + + return 0; +} + +static const struct of_device_id sun20i_regulator_of_match[] = { + { + .compatible = "allwinner,sun20i-d1-system-ldos", + .data = &sun20i_d1_system_ldos, + }, + { }, +}; +MODULE_DEVICE_TABLE(of, sun20i_regulator_of_match); + +static struct platform_driver sun20i_regulator_driver = { + .probe = sun20i_regulator_probe, + .driver = { + .name = "sun20i-regulator", + .of_match_table = sun20i_regulator_of_match, + }, +}; +module_platform_driver(sun20i_regulator_driver); + +MODULE_AUTHOR("Samuel Holland "); +MODULE_DESCRIPTION("Allwinner D1 internal LDO driver"); +MODULE_LICENSE("GPL"); -- 2.39.2