From patchwork Thu May 30 08:55:58 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Artur Weber X-Patchwork-Id: 13679979 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 DD92FC25B74 for ; Thu, 30 May 2024 08:57:44 +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:Cc:To:In-Reply-To:References:Message-Id :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=bai9VDX9qoZaKLOjtWwOD868iU8aov0VZkseKN5lAtQ=; b=zKFTOcgxQvu4iE wMoZ2m3KplEfJtPnplglHzYtUvdW3cxwX+UxB4HebwIZOd1m3/wy7wBzjQi5/lVz9FT/S/GNFrxql y83wAhbHyhlYHycHAxmR2H74yWgWepRwduLwZMVtlefZcUofk8ciGZaC5defGy19IecXCqBqzB0I1 zXJuvOSWeqoH20Zox2UKC27CS28xgHhJrmRGB+1rx/nEydTIUXb4m90kFFY99/IKxcjEpuG0Lop8H DkOUaCPo3gj/CHQQAsPaIKmFZR9+dhkzjlAMBH3KuD/NnNsnwk9b6kjzK/OcdTel21ZoYfTwWMwHX CaGgA+gcKco3OrpjS9WQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sCbbE-00000006hzD-1ue9; Thu, 30 May 2024 08:57:28 +0000 Received: from mail-ej1-x62b.google.com ([2a00:1450:4864:20::62b]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sCbaQ-00000006hb9-2r3N for linux-arm-kernel@lists.infradead.org; Thu, 30 May 2024 08:56:41 +0000 Received: by mail-ej1-x62b.google.com with SMTP id a640c23a62f3a-a653972487fso34130166b.1 for ; Thu, 30 May 2024 01:56:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1717059397; x=1717664197; 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=y515HyBE05tinjYLW0X+UrEG/VkT72f1VOdrq3/W9HM=; b=Gx+rgs8tC9lCk8LT86+RBRPLgz2H5rVFUBow4LNNAFq+h62R1kURKhncdb3v9qNLwY ZfwfRBvwmymohlrmeE/EFhpMiqzwOb2ZWmtVvczvPIwcTkhBamgOYOYFX1EDqottTQYk EiVbcXroFT+cGFnCywYHV8AfwRAoYjmSX+UU0tb12gyGQz3ffX+zv9Q0NryvTKnR5Wli nEjuKoQN2FV31iH6CetNsJq69/xnaoprT1qewPcDqiLO5C7e/yC/x4tcTwlEbLppjOCT 1aWnLkXzqugoQsA3Qokb5cOXnf9I6JqmiaVlAnpHb/qfJqs8XREwADHccrM3KxcfLb/h fQuw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717059397; x=1717664197; 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=y515HyBE05tinjYLW0X+UrEG/VkT72f1VOdrq3/W9HM=; b=o5wx60Mue8TdBDMd047h17UmF7WudU+ru9p/gOm6FyfmPbEpDC0KnJVINtR1P7IjUK 7TauHpSypKO18Uat0NPorbosjbifpS5DQN7x+Bx5lxcoWl1C9UoW/o3vKxiABP4HHCjM r7unDe9OyKWav/pe9AJfdsR84oRJS8xpay9XrxRYcLg9N7FUVIHyrcVjhLpHA9nsfDZ2 UgZV3qxsqna+5pj5g21ZE86LoTrJn3u9JR9B+S/Q8yQXOLrWJjSuEpqAvNsee/h2et6m DzknUTG+8+GH317vJCdVG9titJK0b4SQD6TmXUfMWrjnL1RXinvPUQOjZcVgIYU/Nn9h JJlQ== X-Forwarded-Encrypted: i=1; AJvYcCUwY0Jei7+5YH/STR6NACZSSWjHk+vZX+Wva6UmGMMxg61SllDwxuTq761KHdwOCmq6ZS46iJq396a0rQx19TRfniEuoOBV8KRUGTiucJQjT9UbbWE= X-Gm-Message-State: AOJu0YzoPY4KXtOwgOkN27nyIaksBvpXx+/PBQyhcMKkmAj4Et6PXywQ ZB59hTMQe7dihLctWVTmzm2vhDd7nEOX15iyTw7HLXMg9qmMWAcS X-Google-Smtp-Source: AGHT+IEOoR0GhaKcDjy0VMgU2i9ZuhLM0mYFVsj2XeXlc9nnyB4fJ48jkxmmdzBaeVAKSQ8zfT79vA== X-Received: by 2002:a17:906:bcf1:b0:a62:1ac8:5752 with SMTP id a640c23a62f3a-a65e8e317e8mr84844066b.8.1717059396642; Thu, 30 May 2024 01:56:36 -0700 (PDT) Received: from hex.my.domain (83.8.128.191.ipv4.supernova.orange.pl. [83.8.128.191]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a626cc500a1sm798430166b.125.2024.05.30.01.56.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 May 2024 01:56:36 -0700 (PDT) From: Artur Weber Date: Thu, 30 May 2024 10:55:58 +0200 Subject: [PATCH RFC 08/11] power: supply: max77693: Add support for detecting and enabling OTG MIME-Version: 1.0 Message-Id: <20240530-max77693-charger-extcon-v1-8-dc2a9e5bdf30@gmail.com> References: <20240530-max77693-charger-extcon-v1-0-dc2a9e5bdf30@gmail.com> In-Reply-To: <20240530-max77693-charger-extcon-v1-0-dc2a9e5bdf30@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.13.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1717059384; l=3575; i=aweber.kernel@gmail.com; s=20231030; h=from:subject:message-id; bh=ASIwzdEZQZXRm8Gyc8ivplNNZTHwY1NZ3AUZqjW/FCw=; b=MXc4Bb2/SN4z7AvEEeeSSMbRT9iuCtg3ixSez6kz6cE5oO+N08S58aiJ0Lfic/PE6EWcO4BIc Q+aEXmRvbfFDSMFAdElLbXdf7WM0IdllawF8Jzg0BN+OKcPfhzte9rM X-Developer-Key: i=aweber.kernel@gmail.com; a=ed25519; pk=RhDBfWbJEHqDibXbhNEBAnc9FMkyznGxX/hwfhL8bv8= X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240530_015639_579750_D81FC42D X-CRM114-Status: GOOD ( 19.88 ) 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 Building upon the newly added extcon detection support, add detection for USB OTG cables (EXTCON_USB_HOST type), and enable/disable the OTG bits as needed. Signed-off-by: Artur Weber --- Downstream also sets "CHGIN output current limit in OTG mode" to 900mA by writing (1 << 7) to the CHG_CNFG_02 register; while I would try to add this here, I do not know which exact bits control the current limit and how their value affects it (downstream has no docs other than what I just mentioned), so it's impossible for me to know what the mask is. --- drivers/power/supply/max77693_charger.c | 70 +++++++++++++++++++++++++++------ 1 file changed, 59 insertions(+), 11 deletions(-) diff --git a/drivers/power/supply/max77693_charger.c b/drivers/power/supply/max77693_charger.c index c2e8ae367381..e548fd420e78 100644 --- a/drivers/power/supply/max77693_charger.c +++ b/drivers/power/supply/max77693_charger.c @@ -737,11 +737,41 @@ static int max77693_set_charging(struct max77693_charger *chg, bool enable) return ret; } +static int max77693_set_otg(struct max77693_charger *chg, bool enable) +{ + struct regmap *regmap = chg->max77693->regmap; + unsigned int data; + bool is_enabled; + int ret; + + ret = regmap_read(regmap, MAX77693_CHG_REG_CHG_CNFG_00, &data); + if (ret) + return ret; + + is_enabled = !!(data & CHG_CNFG_00_OTG_MASK); + + if (enable && !is_enabled) { + /* OTG on, boost on, DIS_MUIC_CTRL on */ + data |= CHG_CNFG_00_OTG_MASK | CHG_CNFG_00_BOOST_MASK \ + | CHG_CNFG_00_DIS_MUIC_CTRL_MASK; + + } else if (!enable && is_enabled) { + /* OTG off, boost off, DIS_MUIC_CTRL off */ + data &= ~(CHG_CNFG_00_OTG_MASK | CHG_CNFG_00_BOOST_MASK \ + | CHG_CNFG_00_DIS_MUIC_CTRL_MASK); + } + + return regmap_write(chg->max77693->regmap, + MAX77693_CHG_REG_CHG_CNFG_00, + data); +} + static void max77693_charger_extcon_work(struct work_struct *work) { struct max77693_charger *chg = container_of(work, struct max77693_charger, cable.work); struct extcon_dev *edev = chg->cable.edev; + bool set_charging, set_otg; int connector, state; int ret; @@ -760,25 +790,43 @@ static void max77693_charger_extcon_work(struct work_struct *work) case EXTCON_CHG_USB_FAST: case EXTCON_CHG_USB_SLOW: case EXTCON_CHG_USB_PD: - ret = max77693_set_charging(chg, true); - if (ret) { - dev_err(chg->dev, "failed to enable charging\n"); - break; - } + set_charging = true; + set_otg = false; + dev_info(chg->dev, "charging. connector type: %d\n", connector); break; + case EXTCON_USB_HOST: + set_charging = false; + set_otg = true; + + dev_info(chg->dev, "USB host. connector type: %d\n", + connector); + break; default: - ret = max77693_set_charging(chg, false); - if (ret) { - dev_err(chg->dev, "failed to disable charging\n"); - break; - } - dev_info(chg->dev, "charging. connector type: %d\n", + set_charging = false; + set_otg = false; + + dev_info(chg->dev, "disconnected. connector type: %d\n", connector); break; } + /* + * The functions below already check if the change is necessary, + * so we don't need to do so here. + */ + ret = max77693_set_charging(chg, set_charging); + if (ret) { + dev_err(chg->dev, "failed to set charging (%d)\n", ret); + goto out; + } + + ret = max77693_set_otg(chg, set_otg); + if (ret) + dev_err(chg->dev, "failed to set OTG (%d)\n", ret); + +out: power_supply_changed(chg->charger); }