From patchwork Fri Sep 15 05:01:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nikita Travkin X-Patchwork-Id: 140495 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:6358:a55:b0:13f:353d:d1ed with SMTP id 21csp379248rwb; Fri, 15 Sep 2023 08:05:27 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGv0g5AAwCDBo0K5AhnFuZYk49RDDc961AAiNsHsfULWomPWJukfcg+8bw3N1R4u01ScezF X-Received: by 2002:a05:6a20:938a:b0:14d:7b6:cf3b with SMTP id x10-20020a056a20938a00b0014d07b6cf3bmr7026706pzh.6.1694790327127; Fri, 15 Sep 2023 08:05:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694790327; cv=none; d=google.com; s=arc-20160816; b=diiIFStNAbdyf20kEPWfkrLFnpOIXCxkqY/c9aoM5nJ8GOdF7hw/kxYnYvaa8oMQW3 GXXHLM0PaM53DA5aVqJZFCY9W3PKw9YA2KPfh9vI34n54OOZOu0QgiWQU2EyTrXNlwNn cnd0IqRumBIiiWDQTl5DwdW+q7ljRmJ4LAN6bWJKbaIZvlJguloBpnobWzQuRM0/CJ/m tACrnMuszcMCxFwTkvfOqIq6Bln4VfEv0kBL5Mz4ARrUEjtgdK72eot07qs4Efed2nkg 2BIEawaw7od2yOhsIvBxd9VnMNioWN50tOhnRn/wXUu8XHFjCTfUKF8gbLMMvbkVZ2yC Pdhw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:in-reply-to:references:message-id :content-transfer-encoding:mime-version:subject:date:from :dkim-signature; bh=f1tbwWFnHM3AUN173q1uCD3v69aGN9Xvn93HKEqU8kw=; fh=8ChIgZP2C0uLMkY6q9m5BeYM4aktykEoN9RArRJ3GBg=; b=zG0cF2lsEVw5v7YlVLMDdrnAQAQh8B96OcRpEadpG4I99l8k5XspqXOLpL78k6JIL6 NcOHgrPSHqyWsKVdCX/+7vSyJzvMXKMnkYpum9uwX1cVgt7z3hUolZP+T35xhd1TrdgI kRwwqAjhn/VvN4Nc7gEEMk3rs3cRWkBDhzTo+mzGED0eLmAHOrEodpqveWyxk2cXljOb AcynlNUQpaZS9PVPY1xizEkb92FVOXsj6MFbAUtVKLpX6HK74adfM/cs8E5rTFJEfanr YSNgPP0MLbPOMPVS9e1IaNoz6xSAstlZyULPDDkjGL/LewNePtu/vqF7DQnc+JkBgzLk 5PBg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@trvn.ru header.s=mail header.b=1C8JozA3; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=trvn.ru Received: from groat.vger.email (groat.vger.email. [23.128.96.35]) by mx.google.com with ESMTPS id bx32-20020a056a02052000b00565f2d55050si3672875pgb.737.2023.09.15.08.05.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Sep 2023 08:05:27 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) client-ip=23.128.96.35; Authentication-Results: mx.google.com; dkim=pass header.i=@trvn.ru header.s=mail header.b=1C8JozA3; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=trvn.ru Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id EA8ED838162B; Thu, 14 Sep 2023 22:03:01 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232075AbjIOFCK (ORCPT + 33 others); Fri, 15 Sep 2023 01:02:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33652 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231997AbjIOFCB (ORCPT ); Fri, 15 Sep 2023 01:02:01 -0400 Received: from box.trvn.ru (box.trvn.ru [194.87.146.52]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5E4C52718; Thu, 14 Sep 2023 22:01:55 -0700 (PDT) Received: from authenticated-user (box.trvn.ru [194.87.146.52]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (No client certificate requested) by box.trvn.ru (Postfix) with ESMTPSA id 8A9CC42264; Fri, 15 Sep 2023 10:01:49 +0500 (+05) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=trvn.ru; s=mail; t=1694754109; bh=Gl5DvmVj086xnyzDaqlZ88oRMAhoEK8Os/bTYN02ZMI=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=1C8JozA3QaTBXohhDDVDkzC5EyVkH43YxO0Em8b9bfk+kXf2t4yG1Vj5t3xesrpbc p7ToOAjIjOexG+8qSrD1zudsK+kQdmcKq5lYeFx16oYRXMG2x2bNr4Budna7MjYzYT hLMQ9LY5E25dk9ALkIWglfpmtmuYAcIzzg+tNsT9TSpJZ8jcU8nuk5AxDwWey5B8p5 TUOJDUasywhCbNB6n7HbyFP5jDI8LOYH0UVKd6cBCgg0EZer0UQwzet9gMcUyB24NE vsZpvIsT2bMbULZjvup3OvvaptSHUmVN3pxZCBTIrElWkRMnqSO4GKVT8gjIujglIu Oo/vmpuylqgkA== From: Nikita Travkin Date: Fri, 15 Sep 2023 10:01:18 +0500 Subject: [PATCH v3 1/4] dt-bindings: power: supply: Add pm8916 VM-BMS MIME-Version: 1.0 Message-Id: <20230915-pm8916-bms-lbc-v3-1-f30881e951a0@trvn.ru> References: <20230915-pm8916-bms-lbc-v3-0-f30881e951a0@trvn.ru> In-Reply-To: <20230915-pm8916-bms-lbc-v3-0-f30881e951a0@trvn.ru> To: Sebastian Reichel , Rob Herring , Krzysztof Kozlowski , Conor Dooley Cc: linux-pm@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-msm@vger.kernel.org, Nikita Travkin , Rob Herring X-Developer-Signature: v=1; a=openpgp-sha256; l=2924; i=nikita@trvn.ru; h=from:subject:message-id; bh=Gl5DvmVj086xnyzDaqlZ88oRMAhoEK8Os/bTYN02ZMI=; b=owEBbQKS/ZANAwAIAUMc7O4oGb91AcsmYgBlA+U7tGo/Utzh6z/qN1R/oNKh0bnlFd4/O5ele zb6mZY4m4qJAjMEAAEIAB0WIQTAhK9UUj+qg34uxUdDHOzuKBm/dQUCZQPlOwAKCRBDHOzuKBm/ dUyzD/9LWazefx635QmVxNQPwp7s/tG+BSHoNbrm8rR538owfXzkFj8WVpRz8COset9KWtvmX68 bPcgNTbe9KoDXijWR3bYdU3wl1g8RI+1fN3ukypkAZLgtG0ooE85s2CSDwi6SeEJ9b/1fybW3Lo ggB9rbGkgH6ixTNuJgoBkgQFtS+hyIfFjEGDyvKJ6eXDAPFtMbJIYaoTh7iDFy4MWXmhxu3oger /1GkaQrJgbNx6AJkW5qHOUXT7fUa69WZ0m90W5ghJsH6M5utmQZ1ANbsdW5mOy06Zr582h4P1Hy 1PzWzmwM9zb2y+ZfhGwVcpZNrKu0FKR2U9UiZSnlYu/bnjcbhxpmKT0rUVADLTN2Vci/Pmj7JM1 e2esONMHuavgB8n5WuUjw0sxO9fIe8zQSYK9vEsEdvUkcHj/5JUx2XuyVRRpih6WoyQTGJ1b9RT WCcrdxq6xijEmRk/CmsOgaCvC64WLfBZexyrjOHfNwpT3Qc3prbQrvpaQ6III46ur8vJjCuqo99 3tpcAQFswDcLX5OV6UMvuPEso+rS0Sy6fmsbE1AWAWCBKBhUbf+guktNQG65hSNuT1b9w4o2/ym uKrwgBpb28KBcPq0DEdjx/HFxew8jvjQZkmrptxNCKh+2xj/6wkGJVkldWZPm7h1r/vhhXwZXq/ ZsA0eIqQ5z4nVWQ== X-Developer-Key: i=nikita@trvn.ru; a=openpgp; fpr=C084AF54523FAA837E2EC547431CECEE2819BF75 X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on groat.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (groat.vger.email [0.0.0.0]); Thu, 14 Sep 2023 22:03:02 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1777116461753292126 X-GMAIL-MSGID: 1777116461753292126 Qualcomm Voltage Mode BMS is a battery monitoring block in PM8916 PMIC. Document it's DT binding. Signed-off-by: Nikita Travkin Reviewed-by: Rob Herring --- v2: Describe all interrupts. (Conor) --- .../bindings/power/supply/qcom,pm8916-bms-vm.yaml | 83 ++++++++++++++++++++++ 1 file changed, 83 insertions(+) diff --git a/Documentation/devicetree/bindings/power/supply/qcom,pm8916-bms-vm.yaml b/Documentation/devicetree/bindings/power/supply/qcom,pm8916-bms-vm.yaml new file mode 100644 index 000000000000..ad764e69ab57 --- /dev/null +++ b/Documentation/devicetree/bindings/power/supply/qcom,pm8916-bms-vm.yaml @@ -0,0 +1,83 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/power/supply/qcom,pm8916-bms-vm.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Qualcomm Voltage Mode BMS + +maintainers: + - Nikita Travkin + +description: + Voltage Mode BMS is a hardware block found in some Qualcomm PMICs + such as pm8916. This block performs battery voltage monitoring. + +allOf: + - $ref: power-supply.yaml# + +properties: + compatible: + const: qcom,pm8916-bms-vm + + reg: + maxItems: 1 + + interrupts: + items: + - description: BMS FSM left S3 mode + - description: BMS FSM entered S2 mode + - description: OCV measured in S3 mode + - description: OCV below threshold + - description: FIFO update done + - description: BMS FSM switched state + + interrupt-names: + items: + - const: cv_leave + - const: cv_enter + - const: ocv_good + - const: ocv_thr + - const: fifo + - const: state_chg + + monitored-battery: true + + power-supplies: true + +required: + - compatible + - reg + - interrupts + - interrupt-names + - monitored-battery + +additionalProperties: false + +examples: + - | + #include + pmic { + #address-cells = <1>; + #size-cells = <0>; + + battery@4000 { + compatible = "qcom,pm8916-bms-vm"; + reg = <0x4000>; + interrupts = <0x0 0x40 0 IRQ_TYPE_EDGE_RISING>, + <0x0 0x40 1 IRQ_TYPE_EDGE_RISING>, + <0x0 0x40 2 IRQ_TYPE_EDGE_RISING>, + <0x0 0x40 3 IRQ_TYPE_EDGE_RISING>, + <0x0 0x40 4 IRQ_TYPE_EDGE_RISING>, + <0x0 0x40 5 IRQ_TYPE_EDGE_RISING>; + interrupt-names = "cv_leave", + "cv_enter", + "ocv_good", + "ocv_thr", + "fifo", + "state_chg"; + + monitored-battery = <&battery>; + power-supplies = <&pm8916_charger>; + }; + }; From patchwork Fri Sep 15 05:01:19 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nikita Travkin X-Patchwork-Id: 140162 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:172:b0:3f2:4152:657d with SMTP id h50csp842656vqi; Thu, 14 Sep 2023 23:26:01 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEYLBx8NWv03k7XBLR2FD60evBD1rzyHi16TkzjaQw1kaEciq2ZO77telpZ4BAvbaPJrkOT X-Received: by 2002:a17:90a:17cd:b0:268:e43a:dbfd with SMTP id q71-20020a17090a17cd00b00268e43adbfdmr659944pja.1.1694759160914; Thu, 14 Sep 2023 23:26:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694759160; cv=none; d=google.com; s=arc-20160816; b=HobgDwiTtAWoiCkWOwg0ZduvAJ5Hr29j+Q6d1Sgis1d9fw8ewRbAClFgOzcM26Sbiy tT571EExdbQ82CwFl3Kt4zx5SGuZ28fa5dElfRwIQPmmTipaYaCXvdK3rP0NnynIvAih c/oNSvu3+m8/gKSIr0Gzf1f29lkIn26zdlFFU2yWWuvu6m5xQ9BWvllr9jv46ba+9U89 wC12cv8P+XnJY4yZpYK/8XqBL74mkzmOMz4SJ5HKdU0JUJgnXNtmOIYysELmFUQ8Z9ln C9bjzQqrgj6OdHe9H8TCDvgmNHZaor5jZJETqngNN2+nJ2jchg39NkRw1E678fJzcNFD s/wA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:in-reply-to:references:message-id :content-transfer-encoding:mime-version:subject:date:from :dkim-signature; bh=QR6GIP/3tRnrs06ez+emilveEQMG33KGm2b5rHO5VgM=; fh=8ChIgZP2C0uLMkY6q9m5BeYM4aktykEoN9RArRJ3GBg=; b=A5rUDCtNFXpsCatuJFCBd/3o1wbaCkspuk87kURK4ekyigecArjAiAy+06YrgN8P5p XSSbzTcry4iebT+0Eyr8jn+AE7RKUuu4n4hlbQbHyc4STf9kBUzq2PFWXXQvHzOtWvST YtWCcspNp1gs/gKPqjTsoGyEMAg82YC5tLR/S2xdZ4FYwaGZHlIGd9ZOenAp/Q6vb7Gl ANRksid0fPRLbbhnP9RO6YvkMQsY/nTfVVVQ59Y+950EQ8ySVBbVxRPQ1Se22ih908Zw X/BUE5Gw3CG4W8iizRFfuPRuXMu1jlYmqRD0KYh0YfL/gYr0AB4f8O5k25SasGoHgHdZ vTDA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@trvn.ru header.s=mail header.b=lXRm7QUV; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=trvn.ru Received: from agentk.vger.email (agentk.vger.email. [2620:137:e000::3:2]) by mx.google.com with ESMTPS id e11-20020a17090a804b00b002746ab58dfcsi2586165pjw.18.2023.09.14.23.26.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Sep 2023 23:26:00 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) client-ip=2620:137:e000::3:2; Authentication-Results: mx.google.com; dkim=pass header.i=@trvn.ru header.s=mail header.b=lXRm7QUV; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=trvn.ru Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id 95226825A3E6; Thu, 14 Sep 2023 22:02:21 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232019AbjIOFCH (ORCPT + 33 others); Fri, 15 Sep 2023 01:02:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33640 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232003AbjIOFCB (ORCPT ); Fri, 15 Sep 2023 01:02:01 -0400 Received: from box.trvn.ru (box.trvn.ru [194.87.146.52]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DFD7C2710; Thu, 14 Sep 2023 22:01:54 -0700 (PDT) Received: from authenticated-user (box.trvn.ru [194.87.146.52]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (No client certificate requested) by box.trvn.ru (Postfix) with ESMTPSA id 6F70F424AE; Fri, 15 Sep 2023 10:01:50 +0500 (+05) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=trvn.ru; s=mail; t=1694754110; bh=rGP+tXUnWOMGHdQJ8R0srdv2Kny66mnF5MC7ifV715k=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=lXRm7QUV1IYXq/QeuTPCugFytuP2FrCDCNtJ73bE2ayi/Gh7Q0pHZGmLjvcvuVTQD hBi2u3FTzNq5ZcLnvcFZCOzx9QbHPWDQNi16/CBWi9x+CwddhlkbaMX0odDAJE3TfS 2RNYQmWc2WYkse6hbQd7gX6eWI2YbPeI2VZrWpmIyachwjf1L62YFslUJDbuN78itk KSPBmVJxuSzQp5sDZyFrV9GivFbP1L/g2ayUJ20iMSZXsn8RruAMLQf0s6vOqnb3dB uVlCFpdD9eKKebNgH8uDnFpv6ZKQTRc0wMXpQBrFbW6YR0nPAxhhaBADpccQ1jzN5t vYNcaAMUp4Rsg== From: Nikita Travkin Date: Fri, 15 Sep 2023 10:01:19 +0500 Subject: [PATCH v3 2/4] dt-bindings: power: supply: Add pm8916 LBC MIME-Version: 1.0 Message-Id: <20230915-pm8916-bms-lbc-v3-2-f30881e951a0@trvn.ru> References: <20230915-pm8916-bms-lbc-v3-0-f30881e951a0@trvn.ru> In-Reply-To: <20230915-pm8916-bms-lbc-v3-0-f30881e951a0@trvn.ru> To: Sebastian Reichel , Rob Herring , Krzysztof Kozlowski , Conor Dooley Cc: linux-pm@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-msm@vger.kernel.org, Nikita Travkin , Rob Herring X-Developer-Signature: v=1; a=openpgp-sha256; l=4501; i=nikita@trvn.ru; h=from:subject:message-id; bh=rGP+tXUnWOMGHdQJ8R0srdv2Kny66mnF5MC7ifV715k=; b=owEBbQKS/ZANAwAIAUMc7O4oGb91AcsmYgBlA+U78/A2JnnlDCpflyrOzd5BNNlzgR8hOvd4w CkCp1IC6BaJAjMEAAEIAB0WIQTAhK9UUj+qg34uxUdDHOzuKBm/dQUCZQPlOwAKCRBDHOzuKBm/ dYGOD/9yiqmvm1IdeYt3zmmsn4Bos6U3t5HGejjkolM32oggULuD+g1Hum8m+vC++bdvPit5bhA lM1qH8FzdfDa8AXIe7LNqhKdb3Y+Kh/6d3DSZuZ53yet8Tqcn1RKe78d4lIzkAGbWhXq1VdgjOw DthtOHSMu4dyo9IYnGYprD//6ttuwM3y6vSpiguC1mHbq0104TtgGWHgPF0THFwO94qrnMx1YxT snKVkn9GUEomrGsb4e+c35J6tdjNspPSmHatQvmWwitixWdVUSJC3bAi3CuuH7f5oufUwVYPRiv 9s4KUgdtXo7igFKucSmCtevBF99tUmNJzQR8ZKPrb0reCJjQ8PpduO2ASQxSPIz6AOkJHK527fo aEjCfotm/LHUS/L3DgirzQyMtr2qolBuO66yUmvn/vbxhVABk/p5/BIIxrBYT27/AuSTaZhECmV zqugOlM12NvePEOVhZ92GjxV3TmDa7dIm/jNZ/dV+Qh7nm8dzp2wNPU4y0MnEQ9WO5LCitruity HkVTv6NhRQAQTeVC/JKott/YKn+BN+irW9tD0Js3NswsO0BUhPvFbuDyWzU0bQgkKcI5vMfrrhd Mwzdk9pj/4Xm2w8pPxE7yjBRzFLrV9LXy+QDkEso9kYumX5zds+GrPyhG74ARiV0CO1arCHHCxQ Xzw1nk+mnQC3eCw== X-Developer-Key: i=nikita@trvn.ru; a=openpgp; fpr=C084AF54523FAA837E2EC547431CECEE2819BF75 X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (agentk.vger.email [0.0.0.0]); Thu, 14 Sep 2023 22:02:21 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1777083782292393918 X-GMAIL-MSGID: 1777083782292393918 Qualcomm Linear Battery Charger is a CC/CV charger block in PM8916 PMIC. Document it's DT binding. Signed-off-by: Nikita Travkin Reviewed-by: Rob Herring --- v2: List all interrupts (Conor) --- .../bindings/power/supply/qcom,pm8916-lbc.yaml | 128 +++++++++++++++++++++ 1 file changed, 128 insertions(+) diff --git a/Documentation/devicetree/bindings/power/supply/qcom,pm8916-lbc.yaml b/Documentation/devicetree/bindings/power/supply/qcom,pm8916-lbc.yaml new file mode 100644 index 000000000000..cdf14e5ed119 --- /dev/null +++ b/Documentation/devicetree/bindings/power/supply/qcom,pm8916-lbc.yaml @@ -0,0 +1,128 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/power/supply/qcom,pm8916-lbc.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Qualcomm Linear Battery Charger + +maintainers: + - Nikita Travkin + +description: + Linear Battery Charger hardware block, found in some Qualcomm PMICs + such as pm8916. Implements a simple, autonomous CC/CV charger. + +allOf: + - $ref: power-supply.yaml# + +properties: + compatible: + const: qcom,pm8916-lbc + + reg: + items: + - description: Charger + - description: Battery + - description: USB + - description: MISC + + reg-names: + items: + - const: chgr + - const: bat_if + - const: usb + - const: misc + + interrupts: + items: + - description: Battery detection + - description: Fast charging + - description: Charging failed + - description: Charging done + - description: Battery present + - description: Battery temperature OK + - description: USB coarse detection + - description: USB IN valid + - description: Charger gone + - description: Overtemperature + + interrupt-names: + items: + - const: vbat_det + - const: fast_chg + - const: chg_fail + - const: chg_done + - const: bat_pres + - const: temp_ok + - const: coarse_det + - const: usb_vbus + - const: chg_gone + - const: overtemp + + qcom,fast-charge-safe-voltage: + $ref: /schemas/types.yaml#/definitions/uint32 + minimum: 4000000 + maximum: 4775000 + description: + Maximum safe battery voltage in uV; May be pre-set by bootloader, + in which case, setting this will harmlessly fail. + + qcom,fast-charge-safe-current: + $ref: /schemas/types.yaml#/definitions/uint32 + minimum: 90000 + maximum: 1440000 + description: + Maximum safe battery charge current in uA; May be pre-set by + bootloader, in which case setting this will harmlessly fail. + + monitored-battery: true + +required: + - compatible + - reg + - interrupts + - interrupt-names + - qcom,fast-charge-safe-voltage + - qcom,fast-charge-safe-current + +additionalProperties: false + +examples: + - | + #include + pmic { + #address-cells = <1>; + #size-cells = <0>; + + charger@1000 { + compatible = "qcom,pm8916-lbc"; + reg = <0x1000>, <0x1200>, <0x1300>, <0x1600>; + reg-names = "chgr", "bat_if", "usb", "misc"; + + interrupts = <0x0 0x10 0 IRQ_TYPE_EDGE_BOTH>, + <0x0 0x10 5 IRQ_TYPE_EDGE_BOTH>, + <0x0 0x10 6 IRQ_TYPE_EDGE_BOTH>, + <0x0 0x10 7 IRQ_TYPE_EDGE_BOTH>, + <0x0 0x12 0 IRQ_TYPE_EDGE_BOTH>, + <0x0 0x12 1 IRQ_TYPE_EDGE_BOTH>, + <0x0 0x13 0 IRQ_TYPE_EDGE_BOTH>, + <0x0 0x13 1 IRQ_TYPE_EDGE_BOTH>, + <0x0 0x13 2 IRQ_TYPE_EDGE_BOTH>, + <0x0 0x13 4 IRQ_TYPE_EDGE_BOTH>; + interrupt-names = "vbat_det", + "fast_chg", + "chg_fail", + "chg_done", + "bat_pres", + "temp_ok", + "coarse_det", + "usb_vbus", + "chg_gone", + "overtemp"; + monitored-battery = <&battery>; + + qcom,fast-charge-safe-current = <900000>; + qcom,fast-charge-safe-voltage = <4300000>; + }; + }; From patchwork Fri Sep 15 05:01:20 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nikita Travkin X-Patchwork-Id: 140514 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:172:b0:3f2:4152:657d with SMTP id h50csp1142743vqi; Fri, 15 Sep 2023 08:49:30 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEf3RK+vWHplFgQ4Sahd3nsnNHZP+0ilBWZrKIW13UOz5XhkibkL4nPMD0H5yU8RzLCCtNl X-Received: by 2002:a17:90b:33cc:b0:274:6cc9:ec69 with SMTP id lk12-20020a17090b33cc00b002746cc9ec69mr1661061pjb.49.1694792970376; Fri, 15 Sep 2023 08:49:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694792970; cv=none; d=google.com; s=arc-20160816; b=D6iGh3FMki+qaZ6NOLi0Ch7S+5OfcYN5aHXTuadV8iGJMEG/AxQawKrGKNdI4kYZE+ ReteTM4mjFPm5XIRqpuvstVcbIiHN5zdbg2ogVPo6u2it+R3vcwxvNHyFlMppwB9Imyj J8LIBDvrqSHENLyzklfF/3/MRF+xsJ0nzxRqtQ7KflHBEVYSL9/oRuQ5bThXhoxvMQgC RdfFCKdbPOkJx+fqaBTV2tsXnoS8t8r7ha8A2MOvfLu6fEp3qdtoxWCFRn9MhdhHr+ol pufWOFOYgzYwe6O/57hpjecVU3qVa2ulvvRPElHoFlmFhxRuh27nwcTDs3XvZ0g31ksC I+9Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:in-reply-to:references:message-id :content-transfer-encoding:mime-version:subject:date:from :dkim-signature; bh=ucWYDqhtTfMgL/flC44qCDLigKvzMH8KBLkKcPiUgYU=; fh=BSLXS125dVr4kLDkF4IUVgNpxJpQ3MjRYi95z03Jk3Y=; b=eR8cscT/8ZI0PFGgLCEV+fpmUKOhd5strXDNpRQ2rjoy4TR7Tt/XpqeE7JUoUbtkd+ uCUghVmKEhnbtPiW0xcG9SzScVJIGiEoUji1xtJLhqC3V3EnB7f2PWHtw7EbYhU/1bUO RIWupKvE+CJSuURhxj5udVTWXGEBo9MDAtYvQgbb+5h5seM8vf0TyFHYN1aLqf7QwGni UgIf6FjEJTzPs+yQ4fBGdBzsS76rN8uvPRky71+CB73JymIVrhbmsfIH70j/GagLSprI 9A6ZzxQanbqG23w+yX3/b0hMM880G3KcPH8l73AVqYm7YFGHQHJf+Co3NqdiY86SXadh 43tg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@trvn.ru header.s=mail header.b="1QQQL/G+"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=trvn.ru Received: from snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id x6-20020a17090a8a8600b0026b6a7d9e43si3500133pjn.14.2023.09.15.08.49.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Sep 2023 08:49:30 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; dkim=pass header.i=@trvn.ru header.s=mail header.b="1QQQL/G+"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=trvn.ru Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id A360C82F34B3; Thu, 14 Sep 2023 22:02:17 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232083AbjIOFCN (ORCPT + 33 others); Fri, 15 Sep 2023 01:02:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33688 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231998AbjIOFCC (ORCPT ); Fri, 15 Sep 2023 01:02:02 -0400 Received: from box.trvn.ru (box.trvn.ru [194.87.146.52]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B9901271C; Thu, 14 Sep 2023 22:01:55 -0700 (PDT) Received: from authenticated-user (box.trvn.ru [194.87.146.52]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (No client certificate requested) by box.trvn.ru (Postfix) with ESMTPSA id 08B3B424AF; Fri, 15 Sep 2023 10:01:51 +0500 (+05) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=trvn.ru; s=mail; t=1694754111; bh=OR/0+PNWCI1ThG5+btkJbl8fhYjaosObwKJhq9vZJq8=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=1QQQL/G+HlBo8vXXetu/UvTDfJ24SqiqBYKvKR19NDTnZgQkNhMYTKvjkztSBTW0Q IjSz009NZC811VXGO5LqYZx/MPbaVZvjqNaxTLtLTM79mSYPWKjHOmV120eRKGwgBz x9OjjRNuppPklfpqPgOe18MW9GlyleacEiRuWMk6/tLP19v/6/fMdA7L+5VqmnFZh6 bl6iN0mrRJzlENmG83FSzb8RzK9bAhqQkvc1jPcgZmKlkMLZX2X/461MqX2czD3ZQa NiGsEHYZZrU200LWoj9nMxl8f02JOmTz5kjWSfxDqAGdmJmiR2r6jSugmUYjNHbUED 5hXt2L4s+eHmQ== From: Nikita Travkin Date: Fri, 15 Sep 2023 10:01:20 +0500 Subject: [PATCH v3 3/4] power: supply: Add pm8916 VM-BMS support MIME-Version: 1.0 Message-Id: <20230915-pm8916-bms-lbc-v3-3-f30881e951a0@trvn.ru> References: <20230915-pm8916-bms-lbc-v3-0-f30881e951a0@trvn.ru> In-Reply-To: <20230915-pm8916-bms-lbc-v3-0-f30881e951a0@trvn.ru> To: Sebastian Reichel , Rob Herring , Krzysztof Kozlowski , Conor Dooley Cc: linux-pm@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-msm@vger.kernel.org, Nikita Travkin X-Developer-Signature: v=1; a=openpgp-sha256; l=11568; i=nikita@trvn.ru; h=from:subject:message-id; bh=OR/0+PNWCI1ThG5+btkJbl8fhYjaosObwKJhq9vZJq8=; b=owEBbQKS/ZANAwAIAUMc7O4oGb91AcsmYgBlA+U7M1eGKdRGinZCWrPmdtdoxyxGIKhokAR3f 18D4FnXceWJAjMEAAEIAB0WIQTAhK9UUj+qg34uxUdDHOzuKBm/dQUCZQPlOwAKCRBDHOzuKBm/ dU7DEACm1ecVGMYvz6DLOlHzPqn8yFAX9WghsqOjYCS2+NHLQLwWin65jetJaDiq9qlbQZJikeF 3SqpwQq+Fw0ioS/STzOJeK6yCNJikAcAbYONJU883n8MTu0sEoouAwdFVbtx5nSZMETXHMtF3+m kERXeKVTG3fUhgjI4thez9aDte7mGOyjOTCeOrgsizlgHTHsvn7Cfa3zu1PijgslPAvuRpFGLgt /2d/huMQLK9049utR+FSCQo8kjKxxAh/NF2/15icPvv8coXtIBtOqLjFO7Q8gYUeAsPWVqeYic1 8h2TzMGoP66Uuv9GEP/zgnigTCx0Zv4+BQk4Yemv+c3bAb0cvU1lUYKEMiJjvWoervC4vFPVZWS zW22/ssTe7CdDmpTq39TeD/O/xCpYvCeR13E3JyN0RdLOsghhpYSufmixSFX3FE4Xt6NCUOziOg KExxSIcqYx8aLa65LJj5U5jgkVta41LnL7jxwsfDEQ9op+p31/ybkwEixGaUNox7Su+Oj8ObQxa NS6hMY+P+JDFK88Ql8QhQ5/giGBsvlFkQS5wTi3/oq2GcqvamcEUXG1Y4Ix6x4wqrFLd+nGOzVA 9K3W4GjRRuIl/jRH74krcajYq31Z9wzoRZ+nsZ7PVQ7fO0R+bK9Z8BZe2Mv5tMyFvHpefmPZxqn YpYDnOgHaeOPU6A== X-Developer-Key: i=nikita@trvn.ru; a=openpgp; fpr=C084AF54523FAA837E2EC547431CECEE2819BF75 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_PASS,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Thu, 14 Sep 2023 22:02:17 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1777119234090322841 X-GMAIL-MSGID: 1777119234090322841 This driver adds basic support for VM-BMS found in pm8916. VM-BMS is a very basic fuel-gauge hardware block that is, sadly, incapable of any gauging. The hardware supports measuring OCV in sleep mode, where the battery is not in use, or measuring average voltage over time when the device is active. This driver implements basic value readout from this block. Signed-off-by: Nikita Travkin --- v2: Get irq by name v3: Use device_property_read, report OCV with timeout, minor styling --- drivers/power/supply/Kconfig | 11 ++ drivers/power/supply/Makefile | 1 + drivers/power/supply/pm8916_bms_vm.c | 305 +++++++++++++++++++++++++++++++++++ 3 files changed, 317 insertions(+) diff --git a/drivers/power/supply/Kconfig b/drivers/power/supply/Kconfig index 0c80c68fb0d7..c6375c03e5ce 100644 --- a/drivers/power/supply/Kconfig +++ b/drivers/power/supply/Kconfig @@ -629,6 +629,17 @@ config CHARGER_QCOM_SMBB documentation for more detail. The base name for this driver is 'pm8941_charger'. +config BATTERY_PM8916_BMS_VM + tristate "Qualcomm PM8916 BMS-VM support" + depends on MFD_SPMI_PMIC || COMPILE_TEST + help + Say Y to add support for Voltage Mode BMS block found in some + Qualcomm PMICs such as PM8916. This hardware block provides + battery voltage monitoring for the system. + + To compile this driver as module, choose M here: the + module will be called pm8916_bms_vm. + config CHARGER_BQ2415X tristate "TI BQ2415x battery charger driver" depends on I2C diff --git a/drivers/power/supply/Makefile b/drivers/power/supply/Makefile index a8a9fa6de1e9..fdf7916f80ed 100644 --- a/drivers/power/supply/Makefile +++ b/drivers/power/supply/Makefile @@ -84,6 +84,7 @@ obj-$(CONFIG_CHARGER_MP2629) += mp2629_charger.o obj-$(CONFIG_CHARGER_MT6360) += mt6360_charger.o obj-$(CONFIG_CHARGER_MT6370) += mt6370-charger.o obj-$(CONFIG_CHARGER_QCOM_SMBB) += qcom_smbb.o +obj-$(CONFIG_BATTERY_PM8916_BMS_VM) += pm8916_bms_vm.o obj-$(CONFIG_CHARGER_BQ2415X) += bq2415x_charger.o obj-$(CONFIG_CHARGER_BQ24190) += bq24190_charger.o obj-$(CONFIG_CHARGER_BQ24257) += bq24257_charger.o diff --git a/drivers/power/supply/pm8916_bms_vm.c b/drivers/power/supply/pm8916_bms_vm.c new file mode 100644 index 000000000000..5d0dd842509c --- /dev/null +++ b/drivers/power/supply/pm8916_bms_vm.c @@ -0,0 +1,305 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (c) 2023, Nikita Travkin + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define PM8916_PERPH_TYPE 0x04 +#define PM8916_BMS_VM_TYPE 0x020D + +#define PM8916_SEC_ACCESS 0xD0 +#define PM8916_SEC_MAGIC 0xA5 + +#define PM8916_BMS_VM_STATUS1 0x08 +#define PM8916_BMS_VM_FSM_STATE(x) (((x) & 0b00111000) >> 3) +#define PM8916_BMS_VM_FSM_STATE_S2 0x2 + +#define PM8916_BMS_VM_MODE_CTL 0x40 +#define PM8916_BMS_VM_MODE_FORCE_S3 (BIT(0) | BIT(1)) +#define PM8916_BMS_VM_MODE_NORMAL (BIT(1) | BIT(3)) + +#define PM8916_BMS_VM_EN_CTL 0x46 +#define PM8916_BMS_ENABLED BIT(7) + +#define PM8916_BMS_VM_FIFO_LENGTH_CTL 0x47 +#define PM8916_BMS_VM_S1_SAMPLE_INTERVAL_CTL 0x55 +#define PM8916_BMS_VM_S2_SAMPLE_INTERVAL_CTL 0x56 +#define PM8916_BMS_VM_S3_S7_OCV_DATA0 0x6A +#define PM8916_BMS_VM_BMS_FIFO_REG_0_LSB 0xC0 + +/* Using only 1 fifo is broken in hardware */ +#define PM8916_BMS_VM_FIFO_COUNT 2 /* 2 .. 8 */ + +#define PM8916_BMS_VM_S1_SAMPLE_INTERVAL 10 +#define PM8916_BMS_VM_S2_SAMPLE_INTERVAL 10 + +struct pm8916_bms_vm_battery { + struct device *dev; + struct power_supply *battery; + struct power_supply_battery_info *info; + struct regmap *regmap; + unsigned int reg; + unsigned int last_ocv; + time64_t last_ocv_time; + unsigned int vbat_now; +}; + +static int pm8916_bms_vm_battery_get_property(struct power_supply *psy, + enum power_supply_property psp, + union power_supply_propval *val) +{ + struct pm8916_bms_vm_battery *bat = power_supply_get_drvdata(psy); + struct power_supply_battery_info *info = bat->info; + int supplied; + + switch (psp) { + case POWER_SUPPLY_PROP_STATUS: + supplied = power_supply_am_i_supplied(psy); + + if (supplied < 0 && supplied != -ENODEV) + return supplied; + else if (supplied && supplied != -ENODEV) + val->intval = POWER_SUPPLY_STATUS_CHARGING; + else + val->intval = POWER_SUPPLY_STATUS_DISCHARGING; + return 0; + + case POWER_SUPPLY_PROP_HEALTH: + if (bat->vbat_now < info->voltage_min_design_uv) + val->intval = POWER_SUPPLY_HEALTH_DEAD; + else if (bat->vbat_now > info->voltage_max_design_uv) + val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE; + else + val->intval = POWER_SUPPLY_HEALTH_GOOD; + return 0; + + case POWER_SUPPLY_PROP_VOLTAGE_NOW: + val->intval = bat->vbat_now; + return 0; + + case POWER_SUPPLY_PROP_VOLTAGE_OCV: + /* + * Hardware only reliably measures OCV when the system is off or suspended. + * We expose the last known OCV value on boot, invalidating it after 180 seconds. + */ + if (ktime_get_seconds() - bat->last_ocv_time > 180) + return -ENODATA; + + val->intval = bat->last_ocv; + return 0; + + default: + return -EINVAL; + } +} + +static enum power_supply_property pm8916_bms_vm_battery_properties[] = { + POWER_SUPPLY_PROP_STATUS, + POWER_SUPPLY_PROP_VOLTAGE_NOW, + POWER_SUPPLY_PROP_VOLTAGE_OCV, + POWER_SUPPLY_PROP_HEALTH, +}; + +static irqreturn_t pm8916_bms_vm_fifo_update_done_irq(int irq, void *data) +{ + struct pm8916_bms_vm_battery *bat = data; + u16 vbat_data[PM8916_BMS_VM_FIFO_COUNT]; + int ret; + + ret = regmap_bulk_read(bat->regmap, bat->reg + PM8916_BMS_VM_BMS_FIFO_REG_0_LSB, + &vbat_data, PM8916_BMS_VM_FIFO_COUNT * 2); + if (ret) + return IRQ_HANDLED; + + /* + * The VM-BMS hardware only collects voltage data and the software + * has to process it to calculate the OCV and SoC. Hardware provides + * up to 8 averaged measurements for software to take in account. + * + * Just use the last measured value for now to report the current + * battery voltage. + */ + bat->vbat_now = vbat_data[PM8916_BMS_VM_FIFO_COUNT - 1] * 300; + + power_supply_changed(bat->battery); + + return IRQ_HANDLED; +} + +static const struct power_supply_desc pm8916_bms_vm_battery_psy_desc = { + .name = "pm8916-bms-vm", + .type = POWER_SUPPLY_TYPE_BATTERY, + .properties = pm8916_bms_vm_battery_properties, + .num_properties = ARRAY_SIZE(pm8916_bms_vm_battery_properties), + .get_property = pm8916_bms_vm_battery_get_property, +}; + +static int pm8916_bms_vm_battery_probe(struct platform_device *pdev) +{ + struct device *dev = &pdev->dev; + struct pm8916_bms_vm_battery *bat; + struct power_supply_config psy_cfg = {}; + int ret, irq; + unsigned int tmp; + + bat = devm_kzalloc(dev, sizeof(*bat), GFP_KERNEL); + if (!bat) + return -ENOMEM; + + bat->dev = dev; + + bat->regmap = dev_get_regmap(pdev->dev.parent, NULL); + if (!bat->regmap) + return -ENODEV; + + ret = device_property_read_u32(dev, "reg", &bat->reg); + if (ret < 0) + return -EINVAL; + + irq = platform_get_irq_byname(pdev, "fifo"); + if (irq < 0) + return irq; + + ret = devm_request_threaded_irq(dev, irq, NULL, pm8916_bms_vm_fifo_update_done_irq, + IRQF_ONESHOT, "pm8916_vm_bms", bat); + if (ret) + return ret; + + ret = regmap_bulk_read(bat->regmap, bat->reg + PM8916_PERPH_TYPE, &tmp, 2); + if (ret) + goto comm_error; + + if (tmp != PM8916_BMS_VM_TYPE) + return dev_err_probe(dev, -ENODEV, "Device reported wrong type: 0x%X\n", tmp); + + ret = regmap_write(bat->regmap, bat->reg + PM8916_BMS_VM_S1_SAMPLE_INTERVAL_CTL, + PM8916_BMS_VM_S1_SAMPLE_INTERVAL); + if (ret) + goto comm_error; + ret = regmap_write(bat->regmap, bat->reg + PM8916_BMS_VM_S2_SAMPLE_INTERVAL_CTL, + PM8916_BMS_VM_S2_SAMPLE_INTERVAL); + if (ret) + goto comm_error; + ret = regmap_write(bat->regmap, bat->reg + PM8916_BMS_VM_FIFO_LENGTH_CTL, + PM8916_BMS_VM_FIFO_COUNT << 4 | PM8916_BMS_VM_FIFO_COUNT); + if (ret) + goto comm_error; + ret = regmap_write(bat->regmap, + bat->reg + PM8916_BMS_VM_EN_CTL, PM8916_BMS_ENABLED); + if (ret) + goto comm_error; + + ret = regmap_bulk_read(bat->regmap, + bat->reg + PM8916_BMS_VM_S3_S7_OCV_DATA0, &tmp, 2); + if (ret) + goto comm_error; + + bat->last_ocv_time = ktime_get_seconds(); + bat->last_ocv = tmp * 300; + bat->vbat_now = bat->last_ocv; + + psy_cfg.drv_data = bat; + psy_cfg.of_node = dev->of_node; + + bat->battery = devm_power_supply_register(dev, &pm8916_bms_vm_battery_psy_desc, &psy_cfg); + if (IS_ERR(bat->battery)) + return dev_err_probe(dev, PTR_ERR(bat->battery), "Unable to register battery\n"); + + ret = power_supply_get_battery_info(bat->battery, &bat->info); + if (ret) + return dev_err_probe(dev, ret, "Unable to get battery info\n"); + + platform_set_drvdata(pdev, bat); + + return 0; + +comm_error: + return dev_err_probe(dev, ret, "Unable to communicate with device\n"); +} + +static int pm8916_bms_vm_battery_suspend(struct platform_device *pdev, pm_message_t state) +{ + struct pm8916_bms_vm_battery *bat = platform_get_drvdata(pdev); + int ret; + + /* + * Due to a hardware quirk the FSM doesn't switch states normally. + * Instead we unlock the debug registers and force S3 (Measure OCV/Sleep) + * mode every time we suspend. + */ + + ret = regmap_write(bat->regmap, + bat->reg + PM8916_SEC_ACCESS, PM8916_SEC_MAGIC); + if (ret) + goto error; + ret = regmap_write(bat->regmap, + bat->reg + PM8916_BMS_VM_MODE_CTL, PM8916_BMS_VM_MODE_FORCE_S3); + if (ret) + goto error; + + return 0; + +error: + dev_err(bat->dev, "Failed to force S3 mode: %pe\n", ERR_PTR(ret)); + return ret; +} + +static int pm8916_bms_vm_battery_resume(struct platform_device *pdev) +{ + struct pm8916_bms_vm_battery *bat = platform_get_drvdata(pdev); + int ret; + unsigned int tmp; + + ret = regmap_bulk_read(bat->regmap, + bat->reg + PM8916_BMS_VM_S3_S7_OCV_DATA0, &tmp, 2); + + bat->last_ocv_time = ktime_get_seconds(); + bat->last_ocv = tmp * 300; + + ret = regmap_write(bat->regmap, + bat->reg + PM8916_SEC_ACCESS, PM8916_SEC_MAGIC); + if (ret) + goto error; + ret = regmap_write(bat->regmap, + bat->reg + PM8916_BMS_VM_MODE_CTL, PM8916_BMS_VM_MODE_NORMAL); + if (ret) + goto error; + + return 0; + +error: + dev_err(bat->dev, "Failed to return normal mode: %pe\n", ERR_PTR(ret)); + return ret; +} + +static const struct of_device_id pm8916_bms_vm_battery_of_match[] = { + { .compatible = "qcom,pm8916-bms-vm", }, + {} +}; +MODULE_DEVICE_TABLE(of, pm8916_bms_vm_battery_of_match); + +static struct platform_driver pm8916_bms_vm_battery_driver = { + .driver = { + .name = "pm8916-bms-vm", + .of_match_table = pm8916_bms_vm_battery_of_match, + }, + .probe = pm8916_bms_vm_battery_probe, + .suspend = pm8916_bms_vm_battery_suspend, + .resume = pm8916_bms_vm_battery_resume, +}; +module_platform_driver(pm8916_bms_vm_battery_driver); + +MODULE_DESCRIPTION("pm8916 BMS-VM driver"); +MODULE_AUTHOR("Nikita Travkin "); +MODULE_LICENSE("GPL"); From patchwork Fri Sep 15 05:01:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nikita Travkin X-Patchwork-Id: 140533 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:172:b0:3f2:4152:657d with SMTP id h50csp1167816vqi; Fri, 15 Sep 2023 09:23:25 -0700 (PDT) X-Google-Smtp-Source: AGHT+IE7482bZnbS6lRxH1aaj8gpA8iGbbSlFDhMEmnVzJf9KkXIFWqQc45Y7cF6ojQYWW3XGVVH X-Received: by 2002:a17:90a:4e81:b0:262:f449:4497 with SMTP id o1-20020a17090a4e8100b00262f4494497mr2012699pjh.2.1694795005503; Fri, 15 Sep 2023 09:23:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694795005; cv=none; d=google.com; s=arc-20160816; b=jjHheU1rL6OG5MIluj0jJkuyiFC6cVf/ne3gRzio+jIVSgCqNYRzzfDBcpZDraJpxa iC6CCvRKA9xhnJxmXJ7IWVSHqr0PXafmkf5Hjz9WFeBTALq5QHD14jPNP/TVUHJ4tUQo +5Rw6bxZx5Iooiif/uspbzURp73aHFVUJlItI7SEByIPp9CiFSBZhtEGPsVPFVb+xnNT GjW6Att6MHvpiEJnc3cBLBbBGpmb5P84K/ZBG/HLlmwbcTyC3YoCl+wx8yZJ5rikvs4t CB4pMljnvHdjKcZfgcZc4qLtM0R0vGQjto/eVbSlk7f77L7qVKPNN+ufW4omcj8elCT2 E40Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:in-reply-to:references:message-id :content-transfer-encoding:mime-version:subject:date:from :dkim-signature; bh=sG0ANEMc3p7F6Yl7NVkFaZWR7GBLjhAJZwZulgKqGPg=; fh=BSLXS125dVr4kLDkF4IUVgNpxJpQ3MjRYi95z03Jk3Y=; b=0DVhzMvmJ7jwSYQdcrNXsf6qkxOCtdUKxuEkCn3+oveGbY2Ut4PQ1Qt8z/LUeLJP7n pvuRxC/s7e4tDhU8EUFbmFIQLmhb9/cQDCFAzLXIfFdOH5rNQB7K+KvsuZylvPIhgGBw fgoQjvMJm8/NAdun4W542tZtmqJIA5BO0GNNw5qOVFjzWXfwBLEfxkWb6l0ONDHtJJo/ oQzeSBYAsSErT+BQ4VnqEt9Ojke94SDjy38xvRXZg7tJSaiCzo8FHdvvzcQfhpSZdgsJ PwrPPRRY7+lP3IrAeWYqExoCBt9Vc8XvfZm+flBI4nbXhHtqbOYSWhU3Wzj+Fnp/RBpP 64yw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@trvn.ru header.s=mail header.b=iDiDsx1m; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=trvn.ru Received: from howler.vger.email (howler.vger.email. [23.128.96.34]) by mx.google.com with ESMTPS id v15-20020a17090abb8f00b002568a675b65si5704523pjr.141.2023.09.15.09.23.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Sep 2023 09:23:25 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) client-ip=23.128.96.34; Authentication-Results: mx.google.com; dkim=pass header.i=@trvn.ru header.s=mail header.b=iDiDsx1m; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=trvn.ru Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by howler.vger.email (Postfix) with ESMTP id 2D6E282A677E; Thu, 14 Sep 2023 22:02:34 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232101AbjIOFCQ (ORCPT + 33 others); Fri, 15 Sep 2023 01:02:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42730 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232027AbjIOFCD (ORCPT ); Fri, 15 Sep 2023 01:02:03 -0400 Received: from box.trvn.ru (box.trvn.ru [194.87.146.52]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E2DF02720; Thu, 14 Sep 2023 22:01:55 -0700 (PDT) Received: from authenticated-user (box.trvn.ru [194.87.146.52]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (No client certificate requested) by box.trvn.ru (Postfix) with ESMTPSA id 8C56A424B0; Fri, 15 Sep 2023 10:01:51 +0500 (+05) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=trvn.ru; s=mail; t=1694754112; bh=49MaGxNQqyAPhOgveIuAGVdV6GmbhsB4KycALCG40Ns=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=iDiDsx1m068luOVjQClVdWY2bJdlgk4nUzBHYhT71OHfXnef7REhdtdLRm0G90THh QdfLb8AVZLxT6k+tdkTRHQzp6FcJU5Q6oE51nGKLyjtVdU/Jt45PqdWjOzEkFHSbip mudzeH4zUGo/e334iXB3lOWryIvfPnerIwaUfAqVOVTxLiDMEawk2e63kTb1aHwW7x 6h9Yrs6ItEP3gmxXVN3xnIsxirKDbCrhvTGOdsH1AZZTA19twVpMX9b/mdvgeQlprx eHtSOtFYBfTEjy8TTc7hPuVPS6/DtkR/NGgAESYcbADkEoisOGPqfrkVouzbZgC7Np sfiaDA5fFo7tg== From: Nikita Travkin Date: Fri, 15 Sep 2023 10:01:21 +0500 Subject: [PATCH v3 4/4] power: supply: Add driver for pm8916 lbc MIME-Version: 1.0 Message-Id: <20230915-pm8916-bms-lbc-v3-4-f30881e951a0@trvn.ru> References: <20230915-pm8916-bms-lbc-v3-0-f30881e951a0@trvn.ru> In-Reply-To: <20230915-pm8916-bms-lbc-v3-0-f30881e951a0@trvn.ru> To: Sebastian Reichel , Rob Herring , Krzysztof Kozlowski , Conor Dooley Cc: linux-pm@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-msm@vger.kernel.org, Nikita Travkin X-Developer-Signature: v=1; a=openpgp-sha256; l=13852; i=nikita@trvn.ru; h=from:subject:message-id; bh=49MaGxNQqyAPhOgveIuAGVdV6GmbhsB4KycALCG40Ns=; b=owEBbQKS/ZANAwAIAUMc7O4oGb91AcsmYgBlA+U7xqFwtrvgnaPf9znUOoIED+nLJnMDFVlni wrnoh/H61uJAjMEAAEIAB0WIQTAhK9UUj+qg34uxUdDHOzuKBm/dQUCZQPlOwAKCRBDHOzuKBm/ dVKmEACStgqDq6p2UvOMcGb5V8AhZUAsrUqApHzvDgIqZaxXvkv7Z0MMMFuR1wfC6mOHmCSJz9L no9VR8fZaZ3VH0jUu5TYvxV/GHuTqyfI0pgFiAaVEQedNjTWG+FB1sWg9QOo0UdxoMdsrqlOCI/ JsH792mxSpF6FjDjAD0GuVBLI8zDOn+Zad8VDoTQ1B01/Hsn1aH7P1IEFLHcEwmhKH7TMcDOPvM 1GNOWcLbs29gzeJeuMoNrnO65t87Yee/ImroFoKwepnhr5xZQ5TxN6sUEYhBmhqCx99gNOLMook 3LvR1OWYf1VQ7wMOnIlWRCp/lEo8/aXAXURhOgPC/V6Tqx5Ln7R5q+zK14PG9czMI3yRMtcc1Yx L8e+iu9whjKs8gI6eWLFGEge38xkDtaxwOSBr5GJUi4uhBLLKICZcZ8E6ObtD8RKjus+p1jEDl7 pMVmkcZUL15l+tPbj9V8CI2KNbg3hxbVphVzfcDXKN5YO7k1++mp5lsfPN26cQhSv2S0/CXT0BC SU+EOb1rAhBSGTQDud/f8bjO2l+OziJfC5xkP6xw3QqejVLJP/MGhXroaAAlHnoARxjGs1bPwV6 Sf8XlM+hYCHiuuHPbupeWuGRdZ/54cZkLlZXcSmMV7xOPzc3ip9iScWFuKlnhGpz/IJ7C6cJhys s589nn4l1J5auvg== X-Developer-Key: i=nikita@trvn.ru; a=openpgp; fpr=C084AF54523FAA837E2EC547431CECEE2819BF75 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_PASS,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (howler.vger.email [0.0.0.0]); Thu, 14 Sep 2023 22:02:34 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1777121368101698887 X-GMAIL-MSGID: 1777121368101698887 pm8916 LBC is a Linear Battery Charger hardware block in pm8916 PMIC. This block implements simple CC/CV charging for Li-Po batteries. The hardware has internal state machine to switch between modes and works mostly autonomously, only needing the limits and targets to be set to operate. This driver allows setting limits and enabling the LBC block, monitoring it's state. Signed-off-by: Nikita Travkin --- v2: Fix missed warnings, get irq by name v3: INPUT_CURRENT_LIMIT -> CONSTANT_CHARGE_CURRENT, use device_property_* --- drivers/power/supply/Kconfig | 11 ++ drivers/power/supply/Makefile | 1 + drivers/power/supply/pm8916_lbc.c | 381 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 393 insertions(+) diff --git a/drivers/power/supply/Kconfig b/drivers/power/supply/Kconfig index c6375c03e5ce..95e296b80550 100644 --- a/drivers/power/supply/Kconfig +++ b/drivers/power/supply/Kconfig @@ -640,6 +640,17 @@ config BATTERY_PM8916_BMS_VM To compile this driver as module, choose M here: the module will be called pm8916_bms_vm. +config CHARGER_PM8916_LBC + tristate "Qualcomm PM8916 Linear Battery Charger support" + depends on MFD_SPMI_PMIC || COMPILE_TEST + help + Say Y here to add support for Linear Battery Charger block + found in some Qualcomm PMICs such as PM8916. This hardware + blokc provides simple CC/CV battery charger. + + To compile this driver as module, choose M here: the + module will be called pm8916_lbc. + config CHARGER_BQ2415X tristate "TI BQ2415x battery charger driver" depends on I2C diff --git a/drivers/power/supply/Makefile b/drivers/power/supply/Makefile index fdf7916f80ed..e4bd9eb1261b 100644 --- a/drivers/power/supply/Makefile +++ b/drivers/power/supply/Makefile @@ -85,6 +85,7 @@ obj-$(CONFIG_CHARGER_MT6360) += mt6360_charger.o obj-$(CONFIG_CHARGER_MT6370) += mt6370-charger.o obj-$(CONFIG_CHARGER_QCOM_SMBB) += qcom_smbb.o obj-$(CONFIG_BATTERY_PM8916_BMS_VM) += pm8916_bms_vm.o +obj-$(CONFIG_CHARGER_PM8916_LBC) += pm8916_lbc.o obj-$(CONFIG_CHARGER_BQ2415X) += bq2415x_charger.o obj-$(CONFIG_CHARGER_BQ24190) += bq24190_charger.o obj-$(CONFIG_CHARGER_BQ24257) += bq24257_charger.o diff --git a/drivers/power/supply/pm8916_lbc.c b/drivers/power/supply/pm8916_lbc.c new file mode 100644 index 000000000000..6d92e98cbecc --- /dev/null +++ b/drivers/power/supply/pm8916_lbc.c @@ -0,0 +1,381 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (c) 2023, Nikita Travkin + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* Two bytes: type + subtype */ +#define PM8916_PERPH_TYPE 0x04 +#define PM8916_LBC_CHGR_TYPE 0x1502 +#define PM8916_LBC_BAT_IF_TYPE 0x1602 +#define PM8916_LBC_USB_TYPE 0x1702 +#define PM8916_LBC_MISC_TYPE 0x1802 + +#define PM8916_LBC_CHGR_CHG_OPTION 0x08 +#define PM8916_LBC_CHGR_PMIC_CHARGER BIT(7) + +#define PM8916_LBC_CHGR_CHG_STATUS 0x09 + +#define PM8916_INT_RT_STS 0x10 + +#define PM8916_LBC_USB_USBIN_VALID BIT(1) + +#define PM8916_LBC_CHGR_VDD_MAX 0x40 +#define PM8916_LBC_CHGR_VDD_SAFE 0x41 +#define PM8916_LBC_CHGR_IBAT_MAX 0x44 +#define PM8916_LBC_CHGR_IBAT_SAFE 0x45 + +#define PM8916_LBC_CHGR_TCHG_MAX_EN 0x60 +#define PM8916_LBC_CHGR_TCHG_MAX_ENABLED BIT(7) +#define PM8916_LBC_CHGR_TCHG_MAX 0x61 + +#define PM8916_LBC_CHGR_CHG_CTRL 0x49 +#define PM8916_LBC_CHGR_CHG_EN BIT(7) +#define PM8916_LBC_CHGR_PSTG_EN BIT(5) + +#define PM8916_LBC_CHGR_MIN_CURRENT 90000 +#define PM8916_LBC_CHGR_MAX_CURRENT 1440000 + +#define PM8916_LBC_CHGR_MIN_VOLTAGE 4000000 +#define PM8916_LBC_CHGR_MAX_VOLTAGE 4775000 +#define PM8916_LBC_CHGR_VOLTAGE_STEP 25000 + +#define PM8916_LBC_CHGR_MIN_TIME 4 +#define PM8916_LBC_CHGR_MAX_TIME 256 + +struct pm8916_lbc_charger { + struct device *dev; + struct extcon_dev *edev; + struct power_supply *charger; + struct power_supply_battery_info *info; + struct regmap *regmap; + unsigned int reg[4]; + bool online; + unsigned int charge_voltage_max; + unsigned int charge_voltage_safe; + unsigned int charge_current_max; + unsigned int charge_current_safe; +}; + +static const unsigned int pm8916_lbc_charger_cable[] = { + EXTCON_USB, + EXTCON_NONE, +}; + +enum { + LBC_CHGR = 0, + LBC_BAT_IF, + LBC_USB, + LBC_MISC, +}; + +static int pm8916_lbc_charger_configure(struct pm8916_lbc_charger *chg) +{ + int ret = 0; + unsigned int tmp; + + chg->charge_voltage_max = clamp_t(u32, chg->charge_voltage_max, + PM8916_LBC_CHGR_MIN_VOLTAGE, chg->charge_voltage_safe); + + tmp = chg->charge_voltage_max - PM8916_LBC_CHGR_MIN_VOLTAGE; + tmp /= PM8916_LBC_CHGR_VOLTAGE_STEP; + chg->charge_voltage_max = PM8916_LBC_CHGR_MIN_VOLTAGE + tmp * PM8916_LBC_CHGR_VOLTAGE_STEP; + + ret = regmap_write(chg->regmap, chg->reg[LBC_CHGR] + PM8916_LBC_CHGR_VDD_MAX, tmp); + if (ret) + goto error; + + chg->charge_current_max = min(chg->charge_current_max, chg->charge_current_safe); + + tmp = clamp_t(u32, chg->charge_current_max, + PM8916_LBC_CHGR_MIN_CURRENT, PM8916_LBC_CHGR_MAX_CURRENT); + + tmp = chg->charge_current_max / PM8916_LBC_CHGR_MIN_CURRENT - 1; + chg->charge_current_max = (tmp + 1) * PM8916_LBC_CHGR_MIN_CURRENT; + + ret = regmap_write(chg->regmap, chg->reg[LBC_CHGR] + PM8916_LBC_CHGR_IBAT_MAX, tmp); + if (ret) + goto error; + + ret = regmap_write(chg->regmap, chg->reg[LBC_CHGR] + PM8916_LBC_CHGR_CHG_CTRL, + PM8916_LBC_CHGR_CHG_EN | PM8916_LBC_CHGR_PSTG_EN); + if (ret) + goto error; + + return ret; + +error: + dev_err(chg->dev, "Failed to configure charging: %pe\n", ERR_PTR(ret)); + return ret; +} + +static int pm8916_lbc_charger_get_property(struct power_supply *psy, + enum power_supply_property psp, + union power_supply_propval *val) +{ + struct pm8916_lbc_charger *chg = power_supply_get_drvdata(psy); + + switch (psp) { + case POWER_SUPPLY_PROP_ONLINE: + val->intval = chg->online; + return 0; + + case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX: + val->intval = chg->charge_voltage_max; + return 0; + + case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT: + val->intval = chg->charge_current_max; + return 0; + + default: + return -EINVAL; + }; +} + +static int pm8916_lbc_charger_set_property(struct power_supply *psy, + enum power_supply_property prop, + const union power_supply_propval *val) +{ + struct pm8916_lbc_charger *chg = power_supply_get_drvdata(psy); + + switch (prop) { + case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT: + chg->charge_current_max = val->intval; + return pm8916_lbc_charger_configure(chg); + default: + return -EINVAL; + } +} + +static int pm8916_lbc_charger_property_is_writeable(struct power_supply *psy, + enum power_supply_property psp) +{ + switch (psp) { + case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT: + return true; + default: + return false; + } +} + +static enum power_supply_property pm8916_lbc_charger_properties[] = { + POWER_SUPPLY_PROP_ONLINE, + POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX, + POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT, +}; + +static irqreturn_t pm8916_lbc_charger_state_changed_irq(int irq, void *data) +{ + struct pm8916_lbc_charger *chg = data; + unsigned int tmp; + int ret; + + ret = regmap_read(chg->regmap, chg->reg[LBC_USB] + PM8916_INT_RT_STS, &tmp); + if (ret) + return IRQ_HANDLED; + + chg->online = !!(tmp & PM8916_LBC_USB_USBIN_VALID); + extcon_set_state_sync(chg->edev, EXTCON_USB, chg->online); + + power_supply_changed(chg->charger); + + return IRQ_HANDLED; +} + +static int pm8916_lbc_charger_probe_dt(struct pm8916_lbc_charger *chg) +{ + struct device *dev = chg->dev; + int ret = 0; + unsigned int tmp; + + ret = device_property_read_u32(dev, "qcom,fast-charge-safe-voltage", &chg->charge_voltage_safe); + if (ret) + return ret; + if (chg->charge_voltage_safe < PM8916_LBC_CHGR_MIN_VOLTAGE) + return -EINVAL; + + chg->charge_voltage_safe = clamp_t(u32, chg->charge_voltage_safe, + PM8916_LBC_CHGR_MIN_VOLTAGE, PM8916_LBC_CHGR_MAX_VOLTAGE); + + tmp = chg->charge_voltage_safe - PM8916_LBC_CHGR_MIN_VOLTAGE; + tmp /= PM8916_LBC_CHGR_VOLTAGE_STEP; + ret = regmap_write(chg->regmap, chg->reg[LBC_CHGR] + PM8916_LBC_CHGR_VDD_SAFE, tmp); + if (ret) + return ret; + + ret = device_property_read_u32(dev, "qcom,fast-charge-safe-current", &chg->charge_current_safe); + if (ret) + return ret; + if (chg->charge_current_safe < PM8916_LBC_CHGR_MIN_CURRENT) + return -EINVAL; + + chg->charge_current_safe = clamp_t(u32, chg->charge_current_safe, + PM8916_LBC_CHGR_MIN_CURRENT, PM8916_LBC_CHGR_MAX_CURRENT); + + chg->charge_current_max = chg->charge_current_safe; + + tmp = chg->charge_current_safe / PM8916_LBC_CHGR_MIN_CURRENT - 1; + ret = regmap_write(chg->regmap, chg->reg[LBC_CHGR] + PM8916_LBC_CHGR_IBAT_SAFE, tmp); + if (ret) + return ret; + + /* Disable charger timeout. */ + ret = regmap_write(chg->regmap, chg->reg[LBC_CHGR] + PM8916_LBC_CHGR_TCHG_MAX_EN, 0x00); + if (ret) + return ret; + + return ret; +} + +static const struct power_supply_desc pm8916_lbc_charger_psy_desc = { + .name = "pm8916-lbc-chgr", + .type = POWER_SUPPLY_TYPE_USB, + .properties = pm8916_lbc_charger_properties, + .num_properties = ARRAY_SIZE(pm8916_lbc_charger_properties), + .get_property = pm8916_lbc_charger_get_property, + .set_property = pm8916_lbc_charger_set_property, + .property_is_writeable = pm8916_lbc_charger_property_is_writeable, +}; + +static int pm8916_lbc_charger_probe(struct platform_device *pdev) +{ + struct device *dev = &pdev->dev; + struct pm8916_lbc_charger *chg; + struct power_supply_config psy_cfg = {}; + int ret, len, irq; + unsigned int tmp; + + chg = devm_kzalloc(dev, sizeof(*chg), GFP_KERNEL); + if (!chg) + return -ENOMEM; + + chg->dev = dev; + + chg->regmap = dev_get_regmap(pdev->dev.parent, NULL); + if (!chg->regmap) + return -ENODEV; + + len = device_property_count_u32(dev, "reg"); + if (len < 0) + return len; + if (len != 4) + return dev_err_probe(dev, -EINVAL, + "Wrong amount of reg values: %d (4 expected)\n", len); + + irq = platform_get_irq_byname(pdev, "usb_vbus"); + if (irq < 0) + return irq; + + ret = devm_request_threaded_irq(dev, irq, NULL, pm8916_lbc_charger_state_changed_irq, + IRQF_ONESHOT, "pm8916_lbc", chg); + if (ret) + return ret; + + ret = device_property_read_u32_array(dev, "reg", chg->reg, len); + if (ret) + return ret; + + ret = regmap_bulk_read(chg->regmap, chg->reg[LBC_CHGR] + PM8916_PERPH_TYPE, &tmp, 2); + if (ret) + goto comm_error; + if (tmp != PM8916_LBC_CHGR_TYPE) + goto type_error; + + ret = regmap_bulk_read(chg->regmap, chg->reg[LBC_BAT_IF] + PM8916_PERPH_TYPE, &tmp, 2); + if (ret) + goto comm_error; + if (tmp != PM8916_LBC_BAT_IF_TYPE) + goto type_error; + + ret = regmap_bulk_read(chg->regmap, chg->reg[LBC_USB] + PM8916_PERPH_TYPE, &tmp, 2); + if (ret) + goto comm_error; + if (tmp != PM8916_LBC_USB_TYPE) + goto type_error; + + ret = regmap_bulk_read(chg->regmap, chg->reg[LBC_MISC] + PM8916_PERPH_TYPE, &tmp, 2); + if (ret) + goto comm_error; + if (tmp != PM8916_LBC_MISC_TYPE) + goto type_error; + + ret = regmap_read(chg->regmap, chg->reg[LBC_CHGR] + PM8916_LBC_CHGR_CHG_OPTION, &tmp); + if (ret) + goto comm_error; + if (tmp != PM8916_LBC_CHGR_PMIC_CHARGER) + dev_err_probe(dev, -ENODEV, "The system is using an external charger\n"); + + ret = pm8916_lbc_charger_probe_dt(chg); + if (ret) + dev_err_probe(dev, ret, "Error while parsing device tree\n"); + + psy_cfg.drv_data = chg; + psy_cfg.of_node = dev->of_node; + + chg->charger = devm_power_supply_register(dev, &pm8916_lbc_charger_psy_desc, &psy_cfg); + if (IS_ERR(chg->charger)) + return dev_err_probe(dev, PTR_ERR(chg->charger), "Unable to register charger\n"); + + ret = power_supply_get_battery_info(chg->charger, &chg->info); + if (ret) + return dev_err_probe(dev, ret, "Unable to get battery info\n"); + + chg->edev = devm_extcon_dev_allocate(dev, pm8916_lbc_charger_cable); + if (IS_ERR(chg->edev)) + return PTR_ERR(chg->edev); + + ret = devm_extcon_dev_register(dev, chg->edev); + if (ret < 0) + return dev_err_probe(dev, ret, "failed to register extcon device\n"); + + ret = regmap_read(chg->regmap, chg->reg[LBC_USB] + PM8916_INT_RT_STS, &tmp); + if (ret) + goto comm_error; + + chg->online = !!(tmp & PM8916_LBC_USB_USBIN_VALID); + extcon_set_state_sync(chg->edev, EXTCON_USB, chg->online); + + chg->charge_voltage_max = chg->info->voltage_max_design_uv; + ret = pm8916_lbc_charger_configure(chg); + if (ret) + return ret; + + return 0; + +comm_error: + return dev_err_probe(dev, ret, "Unable to communicate with device\n"); + +type_error: + return dev_err_probe(dev, -ENODEV, "Device reported wrong type: 0x%X\n", tmp); +} + +static const struct of_device_id pm8916_lbc_charger_of_match[] = { + { .compatible = "qcom,pm8916-lbc", }, + {} +}; +MODULE_DEVICE_TABLE(of, pm8916_lbc_charger_of_match); + +static struct platform_driver pm8916_lbc_charger_driver = { + .driver = { + .name = "pm8916-lbc", + .of_match_table = pm8916_lbc_charger_of_match, + }, + .probe = pm8916_lbc_charger_probe, +}; +module_platform_driver(pm8916_lbc_charger_driver); + +MODULE_DESCRIPTION("pm8916 LBC driver"); +MODULE_AUTHOR("Nikita Travkin "); +MODULE_LICENSE("GPL");