From patchwork Fri Aug 16 08:19:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Artur Weber X-Patchwork-Id: 13765687 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 DD5D4C531DC for ; Fri, 16 Aug 2024 08:22:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Cc:To:In-Reply-To:References :Message-Id:Content-Transfer-Encoding:Content-Type:MIME-Version:Subject:Date: From:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=1WWrU/IhZI7ilrsVlKcVr6cPaQA4CGMInWjIGFs7Ke0=; b=2TeSx6uo7VHfFNn8BT8GO3rOjO Q9oLN65f0KMPPLkL/G+R4hN1SnM0kJ//Z2IlrjhZVTHjzAHxG6fBggUoSS1iTk/Ulp9OjVOIy2IKC o0xLvxlCZyZfvxWKz+UtVgfXHJUI3AhEI7X27dTWrDgYKx7HbFsVR88NIwkmRV59TzC68GHRTc0N4 tRFSPm+jWjJ82zzHEFsdho413e0hmOzydxe+RmuZ7k/T+Xj3h95jVlq4/OLvead1rDMKuDTFc2PdH Ry3nJK0rikfERphcl8MmGAw1rj90VcM3ai7M4I8yHF/MCjpa5AshE0lnYQWsuJCUMGKzcQylP0ahC ayl5M2bA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sesEH-0000000CFuU-3KZE; Fri, 16 Aug 2024 08:22:37 +0000 Received: from mail-wm1-x32e.google.com ([2a00:1450:4864:20::32e]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sesB8-0000000CF0M-48xI for linux-arm-kernel@lists.infradead.org; Fri, 16 Aug 2024 08:19:24 +0000 Received: by mail-wm1-x32e.google.com with SMTP id 5b1f17b1804b1-4281faefea9so11804205e9.2 for ; Fri, 16 Aug 2024 01:19:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723796361; x=1724401161; darn=lists.infradead.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=1WWrU/IhZI7ilrsVlKcVr6cPaQA4CGMInWjIGFs7Ke0=; b=cMR5uUdMhhsYMZ9pq+SjBCWhuvBBcelmJz41oZtDSZ2M/2czxtzfFh1+wawPsFJaop 12w62CAxLAHJRIJ1qM9Hwc1uKQdMSwjmco03Kc4NAEsO9D0V0WZANk3T3qSuEi5muWSF Ns5lIoATo1BvKPFQQI3PApWdMQJcQbKgU9eck74/ooJVv7/Pn2eGa+gE/ldmS92PlUNn 1bPpo6iPym0g8k94d3V6VgUbuPjdDXfiaTjYh+Zt2AYdaERvpfbHtZdHn1spqpvmGkkY PlvXzUhQ7ZW0OQte0bR6youHsrU4rBJx2l5HVVQtr1IPo6ZhF9xDmfVUoAQKb+yFsaDB CU5Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723796361; x=1724401161; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=1WWrU/IhZI7ilrsVlKcVr6cPaQA4CGMInWjIGFs7Ke0=; b=nPX19tSrp7jE8xQlZNfty3HmztViwduALWTgC3Oolr6fZK9ZLUs4OStwHIxVc3ge03 sJrTMYDS8fDRKheJZxx8E1vnxaWfGxPhbvKaZ0HnT1VMg/Va1PNu4gtk9CzGYvzvbplU b9ApnY0zSwrpZWTtqKqhUw5FjVIUJ6cJF7wPgiEMZPJ9Tqc0Q3ZeKsSQCnNnviHL6pP5 Dqw6KuvViVA7hGUAFin8MElSTxh01YmY1mcdt3/9t6HqBAh+/3+dxz9cbCWxVbz4Vp0s Zjs1JFu6HQtzBD0rjvrdMPeGh9wauZNhPEN5ac9su0qt8D5tIAEEsh4QVeTxcv5lTi7/ F9wA== X-Forwarded-Encrypted: i=1; AJvYcCWOJO4dWzjPhUodBqmMds5Rr6kWNeT1NhBI2X+/HyMu5MKqFGYEe/1tXff8gAuPFjnxvlaXiVEzW9tuSlXnZGb/ZNgMOUUqRTlSKWYgGCpB0qgv4K4= X-Gm-Message-State: AOJu0Yw8LLBGmfUW6lqv4JlKSiFOvsvimXTjMck7W+e8wB/y0Kz1wpnK O5o6EkrcsG3vAGa6CCTzs1NMZPdq57JXEor4FXIMAnRQEqIQGWEorS/m5Q== X-Google-Smtp-Source: AGHT+IHZEwmPcEwGMYSPrvNQrSpzwEB8NrVEx6QaPgbgtcHMLWLLrST5VTOPheD4XXDO21urabek5w== X-Received: by 2002:a05:600c:314d:b0:428:18d9:9963 with SMTP id 5b1f17b1804b1-429ed7af165mr13909755e9.22.1723796361274; Fri, 16 Aug 2024 01:19:21 -0700 (PDT) Received: from tablet.my.domain ([37.30.8.221]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-429ded35991sm69732645e9.21.2024.08.16.01.19.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 16 Aug 2024 01:19:20 -0700 (PDT) From: Artur Weber Date: Fri, 16 Aug 2024 10:19:10 +0200 Subject: [PATCH v4 04/10] power: supply: max77693: Set charge current limits during init MIME-Version: 1.0 Message-Id: <20240816-max77693-charger-extcon-v4-4-050a0a9bfea0@gmail.com> References: <20240816-max77693-charger-extcon-v4-0-050a0a9bfea0@gmail.com> In-Reply-To: <20240816-max77693-charger-extcon-v4-0-050a0a9bfea0@gmail.com> To: Krzysztof Kozlowski , Chanwoo Choi Cc: Sebastian Reichel , Rob Herring , Conor Dooley , Lee Jones , Krzysztof Kozlowski , Alim Akhtar , linux-pm@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-samsung-soc@vger.kernel.org, ~postmarketos/upstreaming@lists.sr.ht, Henrik Grimler , Wolfgang Wiedmeyer , Denis 'GNUtoo' Carikli , Artur Weber X-Mailer: b4 0.14.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=6622; i=aweber.kernel@gmail.com; h=from:subject:message-id; bh=O9gPJjPhBFB6wY6JOoyF2PfCabTLSsjOOHWK2XU8fpA=; b=owEBbQKS/ZANAwAKAbO7+KEToFFoAcsmYgBmvwt+rPUakPqDDCGIs8ZJ8QcWzhNDAs+vmcxAP svzhcZgo0aJAjMEAAEKAB0WIQTmYwAOrB3szWrSiQ2zu/ihE6BRaAUCZr8LfgAKCRCzu/ihE6BR aJ5hD/90NIWNX1ccu43LK3y/OwbWeDT2FKDFRk0LhT0wgPl4V2Lr0nyP7zAhA8w9SMPIM2SlAuC Ohz0ZumUfuzpgKC2UUaVYqMlTy5tPb4L6f8fzOw9e6z4FUq3Q6/xwWE+FKxmD1UvcauB0S5+hYr L4sZrrk5mo30hYsEmOLPuv92T2zuHx0q3iEuKj2JVusmj+nGZuVuiEXBVKhCJ3yiVfF79Ve3dm5 oPGt3mFDOW0oBCahx9RlWn0NsNVOpvA+eMED+3/go/3CeV44YzAUxSqXR+7rNfVF7w8SzXVpBq+ 7K/e2NHi/zlpbSzOt0MsZkCgpUK9uvauQm9oP+Q6WEqUIa6IxfRPl/ExjdEbjzAN5ZfGlCrDc0v l/hcWCNcIgxRYVIlwAUe6dhiCbNWf4F0XZvTzqBOKwxC28U4liQ8SEmhqUo5k5tDIbDxglFcLWC JUkFllKmOOKtg8OBTtR8iBxJ1D/Rl3a0qCMQHXhgximy0dcjVU23fDCrm5037eUHT44XbWGnjBk 3VWiMKZsGJ9XrVDnXFdG41zMMr4FcWPIeXOssy4fKmD/NBj/Er8qbrJof8YZfDfZ43/TBbvBZGe c7CGJSsY8D0Hm7ALYiZ3za25VacFBiPmkVDStIWRusQIFnESxAWqluQxm4KaTC0tNcD675L3I/v vTbVtWc3xC+Tr/w== X-Developer-Key: i=aweber.kernel@gmail.com; a=openpgp; fpr=E663000EAC1DECCD6AD2890DB3BBF8A113A05168 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240816_011923_069741_FDCD1AD9 X-CRM114-Status: GOOD ( 23.29 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org There are two charger current limit registers: - Fast charge current limit (which controls current going from the charger to the battery); - CHGIN input current limit (which controls current going into the charger through the cable). Add functions for setting both of the values, and set them to a safe default value of 500mA at initialization. The value for the fast charge current limit can be modified by setting the constant-charge-current-max-ua DT property of the battery node specified in the monitored-battery charger DT property; the CHGIN input current limit will be set up later in the charger detection mechanism (in the future, the INPUT_CURRENT_LIMIT property could also be made writeable for userspace control of the current limit, while keeping the actual current limit from the charger to the battery intact so that users don't accidentally blow up their batteries with a bad value). Acked-by: Lee Jones Tested-by: Henrik Grimler Signed-off-by: Artur Weber --- Changes in v3: - Dropped CHARGER reg in favor of managing the registers directly Changes in v2: - Squashed mfd include register additions into this commit - Changed from custom fast charge current property to monitored-battery (devm_power_supply_register call has been moved up as it is needed by the DT init function now) - Changed to adapt to both current limit values being managed by the CHARGER regulator --- drivers/power/supply/max77693_charger.c | 82 ++++++++++++++++++++++++++++----- include/linux/mfd/max77693-private.h | 2 + 2 files changed, 73 insertions(+), 11 deletions(-) diff --git a/drivers/power/supply/max77693_charger.c b/drivers/power/supply/max77693_charger.c index 4caac142c428..17975ec69a6a 100644 --- a/drivers/power/supply/max77693_charger.c +++ b/drivers/power/supply/max77693_charger.c @@ -26,6 +26,7 @@ struct max77693_charger { u32 min_system_volt; u32 thermal_regulation_temp; u32 batttery_overcurrent; + u32 fast_charge_current; u32 charge_input_threshold_volt; }; @@ -596,6 +597,48 @@ static int max77693_set_batttery_overcurrent(struct max77693_charger *chg, CHG_CNFG_12_B2SOVRC_MASK, data); } +static int max77693_set_input_current_limit(struct max77693_charger *chg, + unsigned int uamp) +{ + unsigned int data; + + if (uamp < 60000 || uamp > 2540000) { + dev_err(chg->dev, "Wrong value for input current limit"); + return -EINVAL; + }; + + data = uamp / 20000; /* 20mA steps */ + + data <<= CHG_CNFG_09_CHGIN_ILIM_SHIFT; + + dev_dbg(chg->dev, "Input current limit: %u (0x%x)\n", uamp, data); + + return regmap_update_bits(chg->max77693->regmap, + MAX77693_CHG_REG_CHG_CNFG_09, + CHG_CNFG_09_CHGIN_ILIM_MASK, data); +} + +static int max77693_set_fast_charge_current(struct max77693_charger *chg, + unsigned int uamp) +{ + unsigned int data; + + if (uamp > 2100000) { + dev_err(chg->dev, "Wrong value for fast charge current\n"); + return -EINVAL; + } + + data = uamp / 33300; /* 0.1A/3 steps */ + + data <<= CHG_CNFG_02_CC_SHIFT; + + dev_dbg(chg->dev, "Fast charge current: %u (0x%x)\n", uamp, data); + + return regmap_update_bits(chg->max77693->regmap, + MAX77693_CHG_REG_CHG_CNFG_02, + CHG_CNFG_02_CC_MASK, data); +} + static int max77693_set_charge_input_threshold_volt(struct max77693_charger *chg, unsigned int uvolt) { @@ -673,6 +716,15 @@ static int max77693_reg_init(struct max77693_charger *chg) if (ret) return ret; + ret = max77693_set_fast_charge_current(chg, chg->fast_charge_current); + if (ret) + return ret; + + ret = max77693_set_input_current_limit(chg, + DEFAULT_FAST_CHARGE_CURRENT); + if (ret) + return ret; + return max77693_set_charge_input_threshold_volt(chg, chg->charge_input_threshold_volt); } @@ -681,6 +733,7 @@ static int max77693_reg_init(struct max77693_charger *chg) static int max77693_dt_init(struct device *dev, struct max77693_charger *chg) { struct device_node *np = dev->of_node; + struct power_supply_battery_info *battery_info; if (!np) { dev_err(dev, "no charger OF node\n"); @@ -708,11 +761,20 @@ static int max77693_dt_init(struct device *dev, struct max77693_charger *chg) chg->charge_input_threshold_volt = DEFAULT_CHARGER_INPUT_THRESHOLD_VOLT; + if (power_supply_get_battery_info(chg->charger, &battery_info) || + !battery_info->constant_charge_current_max_ua) + chg->fast_charge_current = DEFAULT_FAST_CHARGE_CURRENT; + else + chg->fast_charge_current = + battery_info->constant_charge_current_max_ua; + return 0; } #else /* CONFIG_OF */ static int max77693_dt_init(struct device *dev, struct max77693_charger *chg) { + chg->fast_charge_current = DEFAULT_FAST_CHARGE_CURRENT; + return 0; } #endif /* CONFIG_OF */ @@ -732,6 +794,15 @@ static int max77693_charger_probe(struct platform_device *pdev) chg->dev = &pdev->dev; chg->max77693 = max77693; + psy_cfg.drv_data = chg; + + chg->charger = devm_power_supply_register(&pdev->dev, + &max77693_charger_desc, + &psy_cfg); + if (IS_ERR(chg->charger)) + return dev_err_probe(&pdev->dev, PTR_ERR(chg->charger), + "failed: power supply register\n"); + ret = max77693_dt_init(&pdev->dev, chg); if (ret) return ret; @@ -740,8 +811,6 @@ static int max77693_charger_probe(struct platform_device *pdev) if (ret) return ret; - psy_cfg.drv_data = chg; - ret = device_create_file(&pdev->dev, &dev_attr_fast_charge_timer); if (ret) { dev_err(&pdev->dev, "failed: create fast charge timer sysfs entry\n"); @@ -761,15 +830,6 @@ static int max77693_charger_probe(struct platform_device *pdev) goto err; } - chg->charger = devm_power_supply_register(&pdev->dev, - &max77693_charger_desc, - &psy_cfg); - if (IS_ERR(chg->charger)) { - dev_err(&pdev->dev, "failed: power supply register\n"); - ret = PTR_ERR(chg->charger); - goto err; - } - return 0; err: diff --git a/include/linux/mfd/max77693-private.h b/include/linux/mfd/max77693-private.h index 20c5e02ed9da..0819cf0a4f5f 100644 --- a/include/linux/mfd/max77693-private.h +++ b/include/linux/mfd/max77693-private.h @@ -145,6 +145,8 @@ enum max77693_pmic_reg { #define DEFAULT_THERMAL_REGULATION_TEMP 100 /* microamps */ #define DEFAULT_BATTERY_OVERCURRENT 3500000 +/* microamps */ +#define DEFAULT_FAST_CHARGE_CURRENT 500000 /* microvolts */ #define DEFAULT_CHARGER_INPUT_THRESHOLD_VOLT 4300000