From patchwork Thu May 30 08:55:50 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Artur Weber X-Patchwork-Id: 13679970 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 26255C27C43 for ; Thu, 30 May 2024 08:56:45 +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:MIME-Version:Message-Id:Date: Subject:From:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References: List-Owner; bh=0hbE9pgYo00SWnxc8HeAb6Sxrf2zJSifs8nFCnUXA0U=; b=ArlY9NhVEf2qzk 83+FU1WKgo/OcfeaxyHKxihjNjlD4xjyZM/V2qNt6NVG+4CjQXRID0eh4d5yzTJECsXOdFsuZ15fe ZqyaYlo+my/vPoFMLAkWtKfyVTEOvirUjuQ/vx9rg0QEpxR7e110B0QnOKnciMYwKyPNINWC7amrV BZ7JO+qaO+Q9Zi1t6xBvCajMoHCv2gsVBBlDoE3nR0Lub9zgtj/jMUyNFLWzNvQGgejCGM9XDnKRi hnhghybYedMsfoAajoEbjjvhfbrddQTOd19Ngc8Zo2cYlB43wVtlSS0gyRzmZl+I5ec+L0f+2JgbB NgWQVWNhGCxtwTdgbxeg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sCbaJ-00000006hZL-0yeh; Thu, 30 May 2024 08:56:31 +0000 Received: from mail-ej1-x62c.google.com ([2a00:1450:4864:20::62c]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sCbaG-00000006hYN-35Fn for linux-arm-kernel@lists.infradead.org; Thu, 30 May 2024 08:56:30 +0000 Received: by mail-ej1-x62c.google.com with SMTP id a640c23a62f3a-a63036f2daaso54429166b.1 for ; Thu, 30 May 2024 01:56:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1717059386; x=1717664186; darn=lists.infradead.org; h=cc:to:content-transfer-encoding:mime-version:message-id:date :subject:from:from:to:cc:subject:date:message-id:reply-to; bh=lJbFOMieqUJ1HvhG4PnEYVJKF/U/LFck+FVCHvfSXS0=; b=GW4dK7K3fP1Y84NZ93kNnx6FwJkjnYTC5280uUuCNEsxTteS4nxsSTVJvnW76JFA5a C85YgEzDVkWO45284wBU7NLkMZhrVyt7WLSgv0uVMKrSc3OkqKNS9Lpr1lN98/J++mZB 1KP+J3HBp230KGZf5alu59W9kJczHa4TwtQufMkUFRL7yGS/ocq0GfqgdEUcmXWXrVdU Gchund8wtqE7G79wtMoTY5rorMOCtIFgYaC+TzWaiRCurD1pd1oPfRkp+NvI6btohzlE RlfP5EWCl/MfF12tUVxCyYJWeCZqIcS/HICMz5MsjFOn2F1xepRlxb/dBTgrJ3RHUVQc AhaA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717059386; x=1717664186; h=cc:to:content-transfer-encoding:mime-version:message-id:date :subject:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=lJbFOMieqUJ1HvhG4PnEYVJKF/U/LFck+FVCHvfSXS0=; b=pV7g3Ao7EpsUOoGHCv0CCIKMEeokaw4RcT56CFjbRRDKC2fJg/EQ5/pz6HCDA3UG+U sQ5CX2hK2dhV8RiWnOJzMNAcvH0JBsHMGKhhml9AQn9SW72rATJy6guLWonK2cd8q3M6 +rHA3ngyX6pP6R5qdzynZrNM6r6qwRe/2Mn+ajmzQBuFPBtrcWddTwhTuh65WTw6khik cqUW1fFyMNBRM/Tx/ey0ywiYxkhZOd++8gK28lKlrccuKlsJ+s6BOL/ELnKDvxuE1gJQ AY/8XdKu1rtoCIXzf2bMMqGRgDOxFDDsMV6rDR6OjD8s+Lt9hKu2nVjEbv3yNeQ4F0Ip JJfg== X-Forwarded-Encrypted: i=1; AJvYcCX1hLoOgPxznv4OhxOF0lS1hpAxFpu4doIKMXhwPzx6PALF+wDeh1cOVxk4Jn0pZZgNK7mmkJUGjI18bdzkhbEbM1/tnfZI/jtxnnEiIq5ij8wZ1Vw= X-Gm-Message-State: AOJu0Yx51MhxvX4qo6Xk0WYbjgpco2jGiqTiBer5NOTkR3mM8bUFqIxF 7C5bWoSZngm29Qz1eVO9cA4UKkvmCSt3tnlS/07kAfvEibxUtLh9 X-Google-Smtp-Source: AGHT+IEKymx3ifIQOa5BXuhl+5X0zN/+cM7GQF9Ej5dDfbtdj4YsozuxlRJoa3tJo6QcZv/W0pOI2A== X-Received: by 2002:a17:906:70c:b0:a5a:5b8b:d14 with SMTP id a640c23a62f3a-a65e8f7b58fmr91646866b.40.1717059385883; Thu, 30 May 2024 01:56:25 -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.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 May 2024 01:56:25 -0700 (PDT) From: Artur Weber Subject: [PATCH RFC 00/11] power: supply: max77693: Toggle charging/OTG based on extcon status Date: Thu, 30 May 2024 10:55:50 +0200 Message-Id: <20240530-max77693-charger-extcon-v1-0-dc2a9e5bdf30@gmail.com> MIME-Version: 1.0 X-B4-Tracking: v=1; b=H4sIABY/WGYC/x2MzQqDMBAGX0X23AUbf1L7KsVDEj91D41lUyQgv rvB4zDMHJSggkTv6iDFLkm2WOD5qCisLi5gmQqTqU1bd6bjr8vW9kPDResCZeR/2CIP8N56N72 aACr1TzFLvs+f8TwvQw6C9GkAAAA= 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=7625; i=aweber.kernel@gmail.com; s=20231030; h=from:subject:message-id; bh=qOadE76vLdaIplciF8D6zHAXndcfCGT/osoFvr6KlEk=; b=MMtU+GPWlwCX0PoSEku/V91gjQOS+G/MXBGM8VPiwgMRwiRu56xv0CqcQUQT5CiqwS3RIBZBn nd+dmF9GoxmCzDiJrOArIdCmggW/bE8oKrBrk0QPkcIrP/w0iqPrpRp 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_015628_833622_F7EC1D21 X-CRM114-Status: GOOD ( 30.55 ) 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 This patchset does the following: - Add CURRENT_MAX and INPUT_CURRENT_MAX power supply properties to expose the "fast charge current" (maximum current from charger to battery) and "CHGIN input current limit" (maximum current from external supply to charger). - Add functions for toggling charging and OTG modes. - Add an extcon-based handler that enables charging or OTG depending on the cable type plugged in. The extcon device to use for cable detection can be specified in the device tree, and is entirely optional. The extcon listener implementation is inspired by the rt5033 charger driver (commit 8242336dc8a8 ("power: supply: rt5033_charger: Add cable detection and USB OTG supply")). Signed-off-by: Artur Weber --- This started off as a cleanup of "[PATCH 0/3] max77693: USB event listener for charger"[1] (and its later iterations[2]), but I ended up replacing 99% of the code and adding a few more additions on top. This patchset was made for two reasons: - I noticed that charging on my Galaxy Tab 3 8.0 seems to "work", but is so slow that it starts to discharge during normal usage. After some investigation I found that under the downstream kernel, the charging limit registers (two of them! see patch descriptions) are changed to a higher value when plugged into a charger, and adjusted based on the cable type. - I was investigating what was needed to get OTG support running, and found that some OTG mode bits had to be set in the charger when an OTG cable is detected, and that required me to modify the charging driver. That's why the patchset includes *both* charging detection and OTG support. == Kernel-side charging management == The original patchset this was meant to supersede was denied because, among other reasons, upstream did not agree to having the charging be controlled this way in the charger driver, suggesting that either user-space or the charger-manager driver should do it[3][4]. charger-manager is not used in any devices, plus the already mentioned patchset comments suggest that it's not the correct way to do things ("DT should be used for concrete hardware, not glue drivers")[4]. There are also some drivers that already do this sort of kernel-side charger toggling/management: other than the aforementioned RT5033 driver which this new patchset is based off[5], there's also the MAX8997 charger[6] (the patch for which was merged just a year after the MAX77693 discussion[7]!) and the AXP288 driver[8]. Still, that's just 3 out of many more drivers... For this reason, I'm sending this first version of the patchset as an RFC for further discussion. I've done some thinking about what a potential implementation allowing for controlling charging from userspace might look like, and here's what I came up with: - Allow for setting the charging input current by setting the INPUT_CURRENT_ MAX property of the power supply. (Leave current from charger to battery set by the device tree in the kernel, since it's arguably more dangerous.) - Allow for setting the CHARGER regulator on/off through some sysfs property. How this should be done is tough to specify; there seems to be a few ways to do it (e.g. setting the STATUS property[9]), we could use a custom sysfs property. - Add a custom sysfs property to disable kernel-side charging. About that last one - I'm convinced at least *some* level of kernel-side charging control should exist, and that's for a practical reason: A reccuring complaint[10] is devices that are low on battery and plugged into a charger shutting down before they can fully boot. If we set up charging during boot, the charger driver can handle charging until a userspace daemon appears and takes over. Of course, this is all up for discussion - comments appreciated ;) == CHARGER regulator == I think it's worth also analysing the role that the CHARGER regulator plays here. At the moment, the CHARGER regulator (managed in drivers/regulator/ max77693-regulator.c) handles: * the CHGIN input limit as the regulator current limit; * charger enable/disable as the regulator enable/disable. A comment in the regulator driver suggests that this should in fact manage the *fast charge* current, not the CHGIN input current, and that "this should be fixed"[11]. But now we're managing the fast charge current in the charger driver... and, if the regulator driver ever was to be "fixed" this way, it would break the charger driver's ability to set input current. We could potentially drop the CHARGER regulator and manage these registers within the charger driver, but since it's not causing us any real problems at the moment (and the "fix" scenario mentioned earlier is very unlikely), I decided to keep it and use it to handle the two functions mentioned above. (Although one of the reasons for the superseded patchset being denied was that "[this] power supply driver should not manage regulators"[3]...) --- Special thanks to Wolfgang Wiedmeyer whose original patchset[1] served as the base for this one, and to Henrik Grimler for helping me out with figuring out how the input current/fast charge current registers are used. [1] https://lore.kernel.org/all/1474932670-11953-1-git-send-email-wolfgit@wiedmeyer.de/ [2] https://lore.kernel.org/all/20190910200233.3195-1-GNUtoo@cyberdimension.org/ [3] https://lore.kernel.org/all/20160927081344.GC4394@kozik-lap/ [4] https://lore.kernel.org/all/298d81d5-fe41-e2d1-32a7-d3dc35b0fe25@kernel.org/ [5] https://github.com/torvalds/linux/blob/v6.9/drivers/power/supply/rt5033_charger.c [6] https://github.com/torvalds/linux/blob/v6.9/drivers/power/supply/max8997_charger.c#L98-L141 [7] https://github.com/torvalds/linux/commit/f384989e88d4484fc9a9e31b0cf0a36e6f172136 [8] https://github.com/torvalds/linux/blob/v6.9/drivers/power/supply/axp288_charger.c#L617-L673 [9] https://github.com/torvalds/linux/blob/v6.9/drivers/power/supply/rt9471.c#L370-L371 [10] https://gitlab.com/postmarketOS/pmaports/-/issues/2594 [11] https://github.com/torvalds/linux/blob/v6.9/drivers/regulator/max77693-regulator.c#L45-L54 --- Artur Weber (10): dt-bindings: power: supply: max77693: Add fast charge current property dt-bindings: power: supply: max77693: Add maxim,usb-connector property mfd: max77693: Add defines for OTG control power: supply: max77693: Expose INPUT_CURRENT_LIMIT and CURRENT_MAX power: supply: max77693: Set charge current limits during init power: supply: max77693: Add USB extcon detection for enabling charging power: supply: max77693: Add support for detecting and enabling OTG power: supply: max77693: Set up charge/input current according to cable type ARM: dts: samsung: exynos4212-tab3: Set fast charge current for MAX77693 ARM: dts: samsung: exynos4212-tab3: Add USB connector node Wolfgang Wiedmeyer (1): mfd: max77693: Add defines for charger current control .../bindings/power/supply/maxim,max77693.yaml | 13 + arch/arm/boot/dts/samsung/exynos4212-tab3.dtsi | 14 + drivers/power/supply/Kconfig | 1 + drivers/power/supply/max77693_charger.c | 291 +++++++++++++++++++++ include/linux/mfd/max77693-private.h | 9 + 5 files changed, 328 insertions(+) --- base-commit: a38297e3fb012ddfa7ce0321a7e5a8daeb1872b6 change-id: 20240525-max77693-charger-extcon-9ebb7bad83ce Best regards,