devicetree.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
To: unlisted-recipients:; (no To-header on input)
Cc: Alexandre Courbot <acourbot@nvidia.com>,
	Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>,
	azkali <a.ffcc7@gmail.com>, CTCaer <ctcaer@gmail.com>,
	Sebastian Reichel <sre@kernel.org>,
	Rob Herring <robh+dt@kernel.org>,
	Krzysztof Kozlowski <krzysztof.kozlowski+dt@linaro.org>,
	Conor Dooley <conor+dt@kernel.org>,
	linux-pm@vger.kernel.org, devicetree@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: [PATCH v2 3/3] power: supply: bq24190_charger: Export current regulator
Date: Thu, 24 Aug 2023 15:13:31 +0200	[thread overview]
Message-ID: <20230824131342.206784-4-linkmauve@linkmauve.fr> (raw)
In-Reply-To: <20230824131342.206784-1-linkmauve@linkmauve.fr>

From: Alexandre Courbot <acourbot@nvidia.com>

This prevents the charger from ever going over the current limit.

Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
Signed-off-by: Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
---
 drivers/power/supply/bq24190_charger.c | 82 ++++++++++++++++++++++++++
 1 file changed, 82 insertions(+)

diff --git a/drivers/power/supply/bq24190_charger.c b/drivers/power/supply/bq24190_charger.c
index a56122b39687..cc1bd87f4982 100644
--- a/drivers/power/supply/bq24190_charger.c
+++ b/drivers/power/supply/bq24190_charger.c
@@ -530,6 +530,79 @@ static int bq24190_set_otg_vbus(struct bq24190_dev_info *bdi, bool enable)
 }
 
 #ifdef CONFIG_REGULATOR
+static int bq24190_set_charging_current(struct regulator_dev *dev,
+			int min_uA, int max_uA)
+{
+	struct bq24190_dev_info *bdi = rdev_get_drvdata(dev);
+	u8 ss_reg;
+	int in_current_limit;
+	int ret = 0;
+
+	ret = bq24190_read(bdi, BQ24190_REG_SS, &ss_reg);
+	if (ret < 0)
+		goto error;
+
+	if (max_uA == 0 && ss_reg != 0)
+		return ret;
+
+	if (!(ss_reg & BQ24190_REG_SS_VBUS_STAT_MASK))
+		in_current_limit = 500;
+	else
+		in_current_limit = max_uA / 1000;
+
+	return bq24190_set_field_val(bdi, BQ24190_REG_ISC,
+			BQ24190_REG_ISC_IINLIM_MASK,
+			BQ24190_REG_ISC_IINLIM_SHIFT,
+			bq24190_isc_iinlim_values,
+			ARRAY_SIZE(bq24190_isc_iinlim_values),
+			in_current_limit);
+error:
+	dev_err(bdi->dev, "Charger enable failed, err = %d\n", ret);
+	return ret;
+}
+
+static const struct regulator_ops bq24190_chrg_ops = {
+	.set_current_limit = bq24190_set_charging_current,
+};
+
+static const struct regulator_desc bq24190_chrg_desc = {
+	.name = "charger",
+	.of_match = "charger",
+	.type = REGULATOR_CURRENT,
+	.owner = THIS_MODULE,
+	.ops = &bq24190_chrg_ops,
+};
+
+static const struct regulator_init_data bq24190_chrg_init_data = {
+	.constraints = {
+		.valid_ops_mask = REGULATOR_CHANGE_STATUS | REGULATOR_CHANGE_CURRENT,
+		.min_uA = 0,
+		.max_uA = 3000000,
+	},
+};
+
+static int bq24190_register_chrg_regulator(struct bq24190_dev_info *bdi)
+{
+	struct bq24190_platform_data *pdata = bdi->dev->platform_data;
+	struct regulator_config cfg = { };
+	struct regulator_dev *reg;
+	int ret = 0;
+
+	cfg.dev = bdi->dev;
+	if (pdata && pdata->regulator_init_data)
+		cfg.init_data = pdata->regulator_init_data;
+	else
+		cfg.init_data = &bq24190_chrg_init_data;
+	cfg.driver_data = bdi;
+	reg = devm_regulator_register(bdi->dev, &bq24190_chrg_desc, &cfg);
+	if (IS_ERR(reg)) {
+		ret = PTR_ERR(reg);
+		dev_err(bdi->dev, "Can't register regulator: %d\n", ret);
+	}
+
+	return ret;
+}
+
 static int bq24190_vbus_enable(struct regulator_dev *dev)
 {
 	return bq24190_set_otg_vbus(rdev_get_drvdata(dev), true);
@@ -611,6 +684,11 @@ static int bq24190_register_vbus_regulator(struct bq24190_dev_info *bdi)
 	return ret;
 }
 #else
+static int bq24190_register_chrg_regulator(struct bq24190_dev_info *bdi)
+{
+	return 0;
+}
+
 static int bq24190_register_vbus_regulator(struct bq24190_dev_info *bdi)
 {
 	return 0;
@@ -1879,6 +1957,10 @@ static int bq24190_probe(struct i2c_client *client)
 		goto out_charger;
 	}
 
+	ret = bq24190_register_chrg_regulator(bdi);
+	if (ret < 0)
+		goto out_charger;
+
 	ret = bq24190_register_vbus_regulator(bdi);
 	if (ret < 0)
 		goto out_charger;
-- 
2.42.0


  parent reply	other threads:[~2023-08-24 13:14 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-24 11:27 [PATCH 0/2] Add charger support for the Nintendo Switch Emmanuel Gil Peyrot
2023-08-24 11:27 ` [PATCH 1/2] power: supply: bq24190: Support bq24193 Emmanuel Gil Peyrot
2023-08-24 12:40   ` Krzysztof Kozlowski
2023-08-24 13:00     ` Emmanuel Gil Peyrot
2023-08-24 13:28       ` Krzysztof Kozlowski
2023-08-31 13:36         ` Linus Walleij
2023-08-24 11:27 ` [PATCH 2/2] power: supply: bq24190_charger: Export current regulator Emmanuel Gil Peyrot
2023-08-24 12:41   ` Krzysztof Kozlowski
2023-08-24 13:02     ` Emmanuel Gil Peyrot
2023-08-24 13:28       ` Krzysztof Kozlowski
2023-08-24 13:13 ` [PATCH v2 0/3] Add charger support for the Nintendo Switch Emmanuel Gil Peyrot
2023-08-24 13:13   ` [PATCH v2 1/3] dt-bindings: power: supply: bq24190: Add ti,bq24193 Emmanuel Gil Peyrot
2023-08-24 13:29     ` Krzysztof Kozlowski
2023-08-24 13:13   ` [PATCH v2 2/3] power: supply: bq24190_charger: Support bq24193 Emmanuel Gil Peyrot
2023-08-24 13:13   ` Emmanuel Gil Peyrot [this message]
2023-09-12 19:14     ` [PATCH v2 3/3] power: supply: bq24190_charger: Export current regulator Sebastian Reichel
2023-09-12 20:37     ` Christophe JAILLET

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230824131342.206784-4-linkmauve@linkmauve.fr \
    --to=linkmauve@linkmauve.fr \
    --cc=a.ffcc7@gmail.com \
    --cc=acourbot@nvidia.com \
    --cc=conor+dt@kernel.org \
    --cc=ctcaer@gmail.com \
    --cc=devicetree@vger.kernel.org \
    --cc=krzysztof.kozlowski+dt@linaro.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pm@vger.kernel.org \
    --cc=robh+dt@kernel.org \
    --cc=sre@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).