From patchwork Tue Nov 1 19:53:43 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Ivaylo Dimitrov X-Patchwork-Id: 13857 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp3176241wru; Tue, 1 Nov 2022 12:57:53 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5AOWDrTy8bmw9WXZlMKk7kE4bGMmVY5/ZwLaEY6delg0yRKuEZDZZYobf6aZvA0mMw2pmm X-Received: by 2002:a17:902:a612:b0:17e:539:c415 with SMTP id u18-20020a170902a61200b0017e0539c415mr20344568plq.173.1667332673583; Tue, 01 Nov 2022 12:57:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1667332673; cv=none; d=google.com; s=arc-20160816; b=kAY6HBOSntyhPNfJQbUqu8c7U2JsOSFKCy9KsxrR1tZBD2qjp4gPz0wX9+2hMYSGTV vD7MLVK9vM8NZQKVAr1mdyGSsoONDFq++BwAs81Nwf1FiGgEAC83TT9rcsoXSRemaXAS Zg8T8Dr7xqrZi5+FFcSPH5Bex6/kUBn+GmygoKreuMg8X1HD2pk/T5GCYZE4NbWyJuMV tK0jjC3jeSibe9czAfCwrDUYNzVkQtBKDe0mQMKHk1aaHnOp3LrICcSQ8GBcUzGNwCcY o8GJ9Qd4iF4MIglbMCcFQYCvhZOPc5DYupKGy2exwWeBAFygpVuO3OGQ9Hopn8Wx/kSv P2ng== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=qMLvz91JxdD1ON81vyAGas2R1qe681rjzzgx71DZVdI=; b=Le3z1Y1Yylm89b96/LZILcUQ5Q9YFPilzvuEK7cBaSpqxXS2AOwfBF7qmOuO1HKZSt CSLzAgr6wN828HcM9CCKV7PY2zyx+EO0LXDfdAPMK7ewf8kUUkBGndSfl5TcL79CZrla uaA2sHJVDRs11OmjcCh3RkSLsmvAbmjTFEssT7Bpnu5x8FEHRLMGB+wogKPGcsJ8SSSK QdUE/Hst4lWukkjbdkThFnSJiaAYqLtACZvabb1iDl8wM0OyJjeSw2IEYFij8JAhHLqf f/64c4WaW+XIgBCV1wl9FUnp++SUC/EmH6GRbjE/8Vw784HA/tzYgxZYx0ECEVsldUYP WmwQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=AsdJI5HU; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id e16-20020a170902ef5000b00186e114bedfsi14024106plx.126.2022.11.01.12.57.39; Tue, 01 Nov 2022 12:57:53 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=AsdJI5HU; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229975AbiKATyO (ORCPT + 99 others); Tue, 1 Nov 2022 15:54:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44192 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229670AbiKATyK (ORCPT ); Tue, 1 Nov 2022 15:54:10 -0400 Received: from mail-wm1-x32f.google.com (mail-wm1-x32f.google.com [IPv6:2a00:1450:4864:20::32f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B3A251B9E6; Tue, 1 Nov 2022 12:54:07 -0700 (PDT) Received: by mail-wm1-x32f.google.com with SMTP id l16-20020a05600c4f1000b003c6c0d2a445so10532270wmq.4; Tue, 01 Nov 2022 12:54:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=qMLvz91JxdD1ON81vyAGas2R1qe681rjzzgx71DZVdI=; b=AsdJI5HULHCv9VmdCc1ZdAEq2+6JDar7zcys5fSi//ueKHj00uKnY+5eADzPW4qjrT TDPm3e+QZfXB82yAzyptOsINAZBw1BcML1pjhTMw86prwCtl9AZWwqwqry1aDJ0lr6CO RSUqHcZaZZP4qAe+TrAhVnWuikr9jBXMdN3McRN6dOHk8pRRXA7snsDRPREpTsOllddB rtCfYmqOeaKsi1Co4mX76wSXaReBfRkuU+M3n/6L36eXOnTVyqdUU2/qNwD8apAM5VpU OjX7SH1yRx9Vom7TAcj7KG5Nc+R48ZOaUfD2TXy0Up6QPs9BsgM4UGhHnUrspI+AYyO7 RXXg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=qMLvz91JxdD1ON81vyAGas2R1qe681rjzzgx71DZVdI=; b=cxJmmxFKKF0MQrwZRFjz3pM066z5COEB4zT2yJJsTp9OLVvFs2t+fuLmoVYsr4kOA1 XNOSFfwZ6M52/5ZeyULBNszE7LxXt5U/2xQW5P4GxoFaa0BWXbwRjrUq5SHSA7ZunL/8 Q4HiqI/v0QLA3v9/AUbJOxSG7emG+Jc/51q0CdWZUMB7QpZMk18q8u9aQCQ/dQ7p+e3W mQcpm3u/Vr1dD9uNJvHfLuwSHbAoRordpFVI0R5UvIErUdnriAukGmQZKvlXB985Vz3F 6bMRN9gLC7SiD0cz9aCQPv1Qh0jwrr4jFcwD+KlnmodLvfreSIPJDKKXoU21DWyurl8t oEdQ== X-Gm-Message-State: ACrzQf0eIR3GMa4exUQc+JTvj8t8U8tWhCu4s1WxtoQKdgvg+sD+sLNq FscqFec2TO1BrkG3gl/yE34= X-Received: by 2002:a05:600c:4e47:b0:3c6:fc59:5eff with SMTP id e7-20020a05600c4e4700b003c6fc595effmr23083213wmq.18.1667332446282; Tue, 01 Nov 2022 12:54:06 -0700 (PDT) Received: from localhost.localdomain ([46.249.74.23]) by smtp.gmail.com with ESMTPSA id p11-20020a05600c468b00b003cf75213bb9sm6092617wmo.8.2022.11.01.12.54.05 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 01 Nov 2022 12:54:05 -0700 (PDT) From: Ivaylo Dimitrov To: sre@kernel.org Cc: linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org, tony@atomide.com, philipp@uvos.xyz, Ivaylo Dimitrov Subject: [PATCH 1/3] power: cpcap-battery: Do not issue low signal too frequently Date: Tue, 1 Nov 2022 21:53:43 +0200 Message-Id: <1667332425-12536-2-git-send-email-ivo.g.dimitrov.75@gmail.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1667332425-12536-1-git-send-email-ivo.g.dimitrov.75@gmail.com> References: <1667332425-12536-1-git-send-email-ivo.g.dimitrov.75@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-1.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,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-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1748325025929281124?= X-GMAIL-MSGID: =?utf-8?q?1748325025929281124?= It seems that low battery irq may be generated tens of times per second, leading to userspace being flooded with unnecessary events. Fix that by preventing such events being generated more than once every 30 seconds. Signed-off-by: Ivaylo Dimitrov --- drivers/power/supply/cpcap-battery.c | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/drivers/power/supply/cpcap-battery.c b/drivers/power/supply/cpcap-battery.c index 4676560..2659df7 100644 --- a/drivers/power/supply/cpcap-battery.c +++ b/drivers/power/supply/cpcap-battery.c @@ -137,6 +137,7 @@ struct cpcap_battery_ddata { struct power_supply *psy; struct cpcap_battery_config config; struct cpcap_battery_state_data state[CPCAP_BATTERY_STATE_NR]; + struct timer_list low_timer; u32 cc_lsb; /* μAms per LSB */ atomic_t active; int charge_full; @@ -914,9 +915,14 @@ static irqreturn_t cpcap_battery_irq_thread(int irq, void *data) dev_info(ddata->dev, "Coulomb counter calibration done\n"); break; case CPCAP_BATTERY_IRQ_ACTION_BATTERY_LOW: - if (latest->current_ua >= 0) + if (latest->current_ua >= 0 && + !timer_pending(&ddata->low_timer)) { dev_warn(ddata->dev, "Battery low at %imV!\n", latest->voltage / 1000); + mod_timer(&ddata->low_timer, + jiffies + msecs_to_jiffies(30000)); + disable_irq_nosync(d->irq); + } break; case CPCAP_BATTERY_IRQ_ACTION_POWEROFF: if (latest->current_ua >= 0 && latest->voltage <= 3200000) { @@ -1087,6 +1093,19 @@ static int cpcap_battery_calibrate(struct cpcap_battery_ddata *ddata) return error; } +static void cpcap_battery_lowbph_enable(struct timer_list *t) +{ + struct cpcap_battery_ddata *ddata = from_timer(ddata, t, low_timer); + struct cpcap_interrupt_desc *d; + + list_for_each_entry(d, &ddata->irq_list, node) { + if (d->action == CPCAP_BATTERY_IRQ_ACTION_BATTERY_LOW) + break; + } + + enable_irq(d->irq); +} + #ifdef CONFIG_OF static const struct of_device_id cpcap_battery_id_table[] = { { @@ -1118,6 +1137,8 @@ static int cpcap_battery_probe(struct platform_device *pdev) if (!ddata) return -ENOMEM; + timer_setup(&ddata->low_timer, cpcap_battery_lowbph_enable, 0); + cpcap_battery_detect_battery_type(ddata); INIT_LIST_HEAD(&ddata->irq_list); @@ -1185,6 +1206,8 @@ static int cpcap_battery_remove(struct platform_device *pdev) if (error) dev_err(&pdev->dev, "could not disable: %i\n", error); + del_timer_sync(&ddata->low_timer); + return 0; } From patchwork Tue Nov 1 19:53:44 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ivaylo Dimitrov X-Patchwork-Id: 13858 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp3176388wru; Tue, 1 Nov 2022 12:58:12 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7LbTOXegP8ojVMIaOdtG9xwRpPVOv4TRAv7sZ3Tqu70MBPB73mabrL5x6BO9QHwXVv19hn X-Received: by 2002:a17:902:7d87:b0:17f:9cd0:2e03 with SMTP id a7-20020a1709027d8700b0017f9cd02e03mr21053161plm.164.1667332692644; Tue, 01 Nov 2022 12:58:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1667332692; cv=none; d=google.com; s=arc-20160816; b=enhO4jixIi8BKeakfs54b4gXgxDFDP6TsF9FjbgSSojUotMm8FzkgcCFbnIMvPpG1a 1OKosIbXCjXPzfPokeVKM/Vk+ndsvr7DCmklF/urUWu5HpYh/G16tv+myWjAkEgEgrGk 8ga/29uQi4U70vzr0HkCewIdvdLkwJrH9Lp3oAxxKJkXtXLQ89DsOGkZ/e9+JGNIlK9A 1N+koCnH26vJOVJj7UIFBsVDSJug2AR9+rmg0RHbP0sttgmcqKFirkIrJcNnV/VuQ77K zN2XkuK37Pqog8Q7x+FXIlyxjdC6ctvGcm2+7Tp7qTa3sDORGOeI75mS+miw0T72aAe9 GMfg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:references:in-reply-to:message-id:date:subject :cc:to:from:dkim-signature; bh=vye8HxSgbJ5V+jvH1roXnBTgLilfVcEjREHvPLd1+J4=; b=Zs4R2QaZ32yQWBvDCmUwXkBUIuTiZmWdUV8QYIVpVe1RyjGG+zE8C85I0lw48y2MID CZ8f9cNe1pk2mQ+L79/IYw3K+KjBZH53qPYbg+JSEM5U52sRf2DQqwyv2LuEnXiSM6LU u5WiWO1joWnw3l0ORig6246D8W4vmjGx96SpupeRUs6307vJ5wRFQC9PEp4RmFcmuJqN /Xbqm2wkxD3b8e2uzuSX4ukM3TgRCrYILx/+0c1jOr/28C2YAkd5LsZdlrCCRwhgbYpg 8Z0V6Krh1HOakxWILzscsdesdBUMPvGJ6wWcPuR9uwZA55ylpSWMIrTi8BLM0x9iO+dR CDNg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=ZKjLYhzC; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id cn9-20020a056a00340900b00565c337c562si12987706pfb.100.2022.11.01.12.57.59; Tue, 01 Nov 2022 12:58:12 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=ZKjLYhzC; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230010AbiKATyU (ORCPT + 99 others); Tue, 1 Nov 2022 15:54:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44198 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229819AbiKATyL (ORCPT ); Tue, 1 Nov 2022 15:54:11 -0400 Received: from mail-wm1-x32a.google.com (mail-wm1-x32a.google.com [IPv6:2a00:1450:4864:20::32a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AEE321838B; Tue, 1 Nov 2022 12:54:08 -0700 (PDT) Received: by mail-wm1-x32a.google.com with SMTP id m29-20020a05600c3b1d00b003c6bf423c71so13378wms.0; Tue, 01 Nov 2022 12:54:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=references:in-reply-to:message-id:date:subject:cc:to:from:from:to :cc:subject:date:message-id:reply-to; bh=vye8HxSgbJ5V+jvH1roXnBTgLilfVcEjREHvPLd1+J4=; b=ZKjLYhzCb5kTc/pGMmnAS+QjR79KN9xMzxUqE9fSAXuiSt1COUtdggm2+he/fVBjDa UffrpFN+eIv7RsNliCrL6iST546Gm1g7qlnvFoMYmS4yGMTyUCTBhl2Xy0G9lJR6HkKZ WI90Zk+RDl8caGilT5dFVlcaaisOfw1t+TfZzooPW9ZD4we02xZKRHsNspb5mNqZrihY 6X2+A0XBLeSZSco4dafmOQ5SBzya0LOS5e3Cb8fJRa2fO7CVoO9oRIHP5K9aMeOFbN76 QIam82coNksFbpinnUe6jEV/WrdaeKDXBEijrPI0YVs89kBirAnfVT9GzWUPi4egyvQC Dl3g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=references:in-reply-to:message-id:date:subject:cc:to:from :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=vye8HxSgbJ5V+jvH1roXnBTgLilfVcEjREHvPLd1+J4=; b=m7yegr31mTC1R6ziJUC3NvEs47TTI/bfNBIK3qmqbtBWSKEn1+e0dq9j2fKFMfI4qS FHAsdU+O9UvTNqJElF9zpnyQOTYN1J8CO9qNH6Fmb/RJEgPwx17oB9c/3etDoMEkbMum ngfKukWjJXL1xvCajY31U5Yj+7Iw0sUD3At2rHeqoM0+7mBeTm4olHh/C6wWn5+Kqw6R dRrtLsi08QVy/oR5731yNcvbZ2gux0kq0DOfJYMjnD6lVL23VESZ13tA8lBtEYPyz7fz g0bMJrRHQFQHg2J77YJvCdnCIIL3K7RB5OWbQyz7Hp2xDWUVSceb3dBDkdut1bfbXA+U Zw5A== X-Gm-Message-State: ACrzQf0oauxjTCZ3gM2VtMC7AK7GDIe0goUpll85ULCSrrKD3F5WkbZF yyjuf2KYm3MQUOQPDwxlihA= X-Received: by 2002:a7b:c5d6:0:b0:3c6:f970:e755 with SMTP id n22-20020a7bc5d6000000b003c6f970e755mr13080991wmk.132.1667332447328; Tue, 01 Nov 2022 12:54:07 -0700 (PDT) Received: from localhost.localdomain ([46.249.74.23]) by smtp.gmail.com with ESMTPSA id p11-20020a05600c468b00b003cf75213bb9sm6092617wmo.8.2022.11.01.12.54.06 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 01 Nov 2022 12:54:06 -0700 (PDT) From: Ivaylo Dimitrov To: sre@kernel.org Cc: linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org, tony@atomide.com, philipp@uvos.xyz, Ivaylo Dimitrov Subject: [PATCH 2/3] power: supply: cpcap-battery: Fix battery identification Date: Tue, 1 Nov 2022 21:53:44 +0200 Message-Id: <1667332425-12536-3-git-send-email-ivo.g.dimitrov.75@gmail.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1667332425-12536-1-git-send-email-ivo.g.dimitrov.75@gmail.com> References: <1667332425-12536-1-git-send-email-ivo.g.dimitrov.75@gmail.com> X-Spam-Status: No, score=-1.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,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-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1748325045826315889?= X-GMAIL-MSGID: =?utf-8?q?1748325045826315889?= Use the same logic to identify genuine batteries as Android does. Signed-off-by: Ivaylo Dimitrov --- drivers/power/supply/cpcap-battery.c | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/drivers/power/supply/cpcap-battery.c b/drivers/power/supply/cpcap-battery.c index 2659df7..4cfc325 100644 --- a/drivers/power/supply/cpcap-battery.c +++ b/drivers/power/supply/cpcap-battery.c @@ -422,7 +422,7 @@ static int cpcap_battery_cc_to_ua(struct cpcap_battery_ddata *ddata, static int cpcap_battery_match_nvmem(struct device *dev, const void *data) { - if (strcmp(dev_name(dev), "89-500029ba0f73") == 0) + if (strncmp(dev_name(dev), "89-500", 6) == 0) return 1; else return 0; @@ -439,10 +439,19 @@ static void cpcap_battery_detect_battery_type(struct cpcap_battery_ddata *ddata) if (IS_ERR_OR_NULL(nvmem)) { ddata->check_nvmem = true; dev_info_once(ddata->dev, "Can not find battery nvmem device. Assuming generic lipo battery\n"); - } else if (nvmem_device_read(nvmem, 2, 1, &battery_id) < 0) { - battery_id = 0; - ddata->check_nvmem = true; - dev_warn(ddata->dev, "Can not read battery nvmem device. Assuming generic lipo battery\n"); + } else { + char buf[24]; + + if (nvmem_device_read(nvmem, 96, 4, buf) < 0 || + strncmp(buf, "COPR", 4) != 0 || + nvmem_device_read(nvmem, 104, 24, buf) < 0 || + strncmp(buf, "MOTOROLA E.P CHARGE ONLY", 24) != 0 || + nvmem_device_read(nvmem, 2, 1, &battery_id) < 0) { + battery_id = 0; + ddata->check_nvmem = true; + dev_warn(ddata->dev, "Can not read battery nvmem device. Assuming generic lipo battery\n"); + } + } switch (battery_id) { From patchwork Tue Nov 1 19:53:45 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ivaylo Dimitrov X-Patchwork-Id: 13856 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp3175564wru; Tue, 1 Nov 2022 12:56:26 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6kPlsL9q62EUOQLG1CvI938VduGjrekB6ffyhhR6TaNcDqDymQH89g/XgUhKNjX3lgin3K X-Received: by 2002:a50:c302:0:b0:463:26d6:25fb with SMTP id a2-20020a50c302000000b0046326d625fbmr16133619edb.204.1667332586602; Tue, 01 Nov 2022 12:56:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1667332586; cv=none; d=google.com; s=arc-20160816; b=F6ntzdbGusA9IB4s97dETl4YE2b35JU4QlAeQHlH3KM6x3V2nnsYDpb3KWOTwPXSf9 keHS4RM8nmwcuc2X1ldI19R9SrJ7YNk1DaSapnTNUuqfeqoePdWjAwZAXqeQMlHPNXMm ufD1ZhDNnS/8Hrv11/R1JxcIP4S7n5kXsHpAfKCp0AuCp4rAdPka4EbLCJdxj9iWghK6 +0QoSJl05cE8PiCbT60rHUW0JFlQUF3jylL9eD9rQaseNngcby3ZXaIDU0wgqTOHKe7f 1IgRZUFqm6H04tb+XO47as3WZKOnyczb30hzjpe7bzjFo/bSCb8oK37ytvStCIvBI2hP wJdQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:references:in-reply-to:message-id:date:subject :cc:to:from:dkim-signature; bh=DqgDcLSZNA5Saaon/Fmk29zadvFURd3kHj533JQMAZI=; b=BAgjZ4YfaFWp+efdO+Z0GRXTOTMexeu8DUe7c6guS6Lmb7XkkPcGc8fQGZFuOtpxpm PU+8MAHf+1B2YvjhCNRhEqpSthjHEHP9A5EoqmxikN323mp1Ztu2aG6veUdiyy+ES7KB OzKIn5cAOcmbeEbcjOw9mBx/aH3npkY9qW5s0o2szY2WwlO2dHoygPVlccwxuwC/L99/ Y6QHiyrG9L/pcmJyMambuy/MNNtqUJ3feSXfIKFoSrKme0Ypw/UVTOtxqaiZqP7FjmdJ 8Ei6xi+CVlrlU/ITPfMXg9pzZuOSK9CZB6sSIsAOZSrQj/o/+3/MdqjGXmsXkX9exXtC G8lQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=L3wtkvYX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id d7-20020aa7ce07000000b00458d1c48708si10727298edv.303.2022.11.01.12.56.02; Tue, 01 Nov 2022 12:56:26 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=L3wtkvYX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230003AbiKATyQ (ORCPT + 99 others); Tue, 1 Nov 2022 15:54:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44196 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229846AbiKATyL (ORCPT ); Tue, 1 Nov 2022 15:54:11 -0400 Received: from mail-wm1-x330.google.com (mail-wm1-x330.google.com [IPv6:2a00:1450:4864:20::330]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9F32918E27; Tue, 1 Nov 2022 12:54:09 -0700 (PDT) Received: by mail-wm1-x330.google.com with SMTP id a11-20020a05600c2d4b00b003cf6f5fd9f1so4342512wmg.2; Tue, 01 Nov 2022 12:54:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=references:in-reply-to:message-id:date:subject:cc:to:from:from:to :cc:subject:date:message-id:reply-to; bh=DqgDcLSZNA5Saaon/Fmk29zadvFURd3kHj533JQMAZI=; b=L3wtkvYXHlDG15kqSgQX9OEeEdTYNSkh7poQs18k8dBe/Uz6uEfZIzt16BhbzPKd2p RTz32NgKZY16G9g2S8SMpAqi8L/9Lp+yyq1nNeIKhwP1seuGjpowo1Tz7RDQnZY2ssA6 haFJkb603fzoXpaGGzFWPtaTvQ0rP8lG2xyFUZHYjSgIGg9LRom+F51XEcU1cq4/Nbhu ATrgsKgpyZp2vhT/MARL3WVLhEsrHbWfUoIPb3cjfHwCrOyU+080G75vMs2UCmyV6tpM 5o6TwpDMjQK8zWChJxJaWF1lf3GPBdtQPcPUeht+lmwvI2DJd6hOuLhbpNhnpwdnCMw9 2Fcg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=references:in-reply-to:message-id:date:subject:cc:to:from :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=DqgDcLSZNA5Saaon/Fmk29zadvFURd3kHj533JQMAZI=; b=acStzKqrC6EQnA0j6Kvhg6NJQNAWQ4WdIFMVrxsN+H1fRemJRr2j16qqSOGLlxE22o DEbU4X+TI19tGDbVDYRIHdZBn4KI9ZI3L328n/eeExQZ4lPL5iUnxDC2dZ8DkjyR9bK0 5PxgZil9gFB3S/aUHkNEl3g08yUuffyBn6R5t4Y94HFCcAASd4Wlq0sghxYgpd6Fq5BQ ExgvhxhSx1U75m4n1aEV80nThQnZNCbHNV2xyBV7tRWSU0hDrOpaTI5TJsj0Rct1NOXL ssvcF77jeBAkonElSkMkBn9KI5ymHXK8feCbSSOpQ7dqlPY76lnFwJen1bOlkb6E7UDM 9KoA== X-Gm-Message-State: ACrzQf1r5qOgmkC0ULZalFXwMUh/cKhtQNR/Uv05BohfdSG9JURSnsiI tB49viW9AByOstsWDT+Wkiw= X-Received: by 2002:a05:600c:3147:b0:3c6:f860:9610 with SMTP id h7-20020a05600c314700b003c6f8609610mr23655070wmo.170.1667332448210; Tue, 01 Nov 2022 12:54:08 -0700 (PDT) Received: from localhost.localdomain ([46.249.74.23]) by smtp.gmail.com with ESMTPSA id p11-20020a05600c468b00b003cf75213bb9sm6092617wmo.8.2022.11.01.12.54.07 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 01 Nov 2022 12:54:07 -0700 (PDT) From: Ivaylo Dimitrov To: sre@kernel.org Cc: linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org, tony@atomide.com, philipp@uvos.xyz, Ivaylo Dimitrov Subject: [PATCH 3/3] power: supply: cpcap_battery: Read battery parameters from nvram Date: Tue, 1 Nov 2022 21:53:45 +0200 Message-Id: <1667332425-12536-4-git-send-email-ivo.g.dimitrov.75@gmail.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1667332425-12536-1-git-send-email-ivo.g.dimitrov.75@gmail.com> References: <1667332425-12536-1-git-send-email-ivo.g.dimitrov.75@gmail.com> X-Spam-Status: No, score=-1.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,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-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1748324934410668047?= X-GMAIL-MSGID: =?utf-8?q?1748324934410668047?= Formulas were taken from android blob Signed-off-by: Ivaylo Dimitrov --- drivers/power/supply/cpcap-battery.c | 88 ++++++++++++++++++++---------------- 1 file changed, 48 insertions(+), 40 deletions(-) diff --git a/drivers/power/supply/cpcap-battery.c b/drivers/power/supply/cpcap-battery.c index 4cfc325..5c51685 100644 --- a/drivers/power/supply/cpcap-battery.c +++ b/drivers/power/supply/cpcap-battery.c @@ -74,9 +74,6 @@ #define CPCAP_BATTERY_CC_SAMPLE_PERIOD_MS 250 -#define CPCAP_BATTERY_EB41_HW4X_ID 0x9E -#define CPCAP_BATTERY_BW8X_ID 0x98 - enum { CPCAP_BATTERY_IIO_BATTDET, CPCAP_BATTERY_IIO_VOLTAGE, @@ -388,22 +385,9 @@ static int cpcap_battery_cc_to_ua(struct cpcap_battery_ddata *ddata, * kernel on droid 4, full is 4351000 and software initiates shutdown * at 3078000. The device will die around 2743000. */ -static const struct cpcap_battery_config cpcap_battery_eb41_data = { - .cd_factor = 0x3cc, - .info.technology = POWER_SUPPLY_TECHNOLOGY_LION, - .info.voltage_max_design = 4351000, - .info.voltage_min_design = 3100000, - .info.charge_full_design = 1740000, - .bat.constant_charge_voltage_max_uv = 4200000, -}; - -/* Values for the extended Droid Bionic battery bw8x. */ -static const struct cpcap_battery_config cpcap_battery_bw8x_data = { +static struct cpcap_battery_config cpcap_battery_mot_data = { .cd_factor = 0x3cc, .info.technology = POWER_SUPPLY_TECHNOLOGY_LION, - .info.voltage_max_design = 4200000, - .info.voltage_min_design = 3200000, - .info.charge_full_design = 2760000, .bat.constant_charge_voltage_max_uv = 4200000, }; @@ -431,39 +415,63 @@ static int cpcap_battery_match_nvmem(struct device *dev, const void *data) static void cpcap_battery_detect_battery_type(struct cpcap_battery_ddata *ddata) { struct nvmem_device *nvmem; - u8 battery_id = 0; + char buf[24]; + u8 capacity; + u8 mul_idx; + u8 charge_voltage; + u32 v; + static const u32 multipliers[] = {20, 10, 10, 10, 10, 40, 10, 20, 40}; ddata->check_nvmem = false; nvmem = nvmem_device_find(NULL, &cpcap_battery_match_nvmem); if (IS_ERR_OR_NULL(nvmem)) { - ddata->check_nvmem = true; dev_info_once(ddata->dev, "Can not find battery nvmem device. Assuming generic lipo battery\n"); - } else { - char buf[24]; - - if (nvmem_device_read(nvmem, 96, 4, buf) < 0 || - strncmp(buf, "COPR", 4) != 0 || - nvmem_device_read(nvmem, 104, 24, buf) < 0 || - strncmp(buf, "MOTOROLA E.P CHARGE ONLY", 24) != 0 || - nvmem_device_read(nvmem, 2, 1, &battery_id) < 0) { - battery_id = 0; - ddata->check_nvmem = true; - dev_warn(ddata->dev, "Can not read battery nvmem device. Assuming generic lipo battery\n"); - } + goto unknown; + } + if (nvmem_device_read(nvmem, 96, 4, buf) < 0 || + strncmp(buf, "COPR", 4) != 0 || + nvmem_device_read(nvmem, 104, 24, buf) < 0 || + strncmp(buf, "MOTOROLA E.P CHARGE ONLY", 24) != 0) { + dev_warn(ddata->dev, "Unknown battery nvmem device. Assuming generic lipo battery\n"); + goto unknown; } - switch (battery_id) { - case CPCAP_BATTERY_EB41_HW4X_ID: - ddata->config = cpcap_battery_eb41_data; - break; - case CPCAP_BATTERY_BW8X_ID: - ddata->config = cpcap_battery_bw8x_data; - break; - default: - ddata->config = cpcap_battery_unkown_data; + if (nvmem_device_read(nvmem, 49, 1, &mul_idx) < 0 || + nvmem_device_read(nvmem, 34, 1, &capacity) < 0 || + nvmem_device_read(nvmem, 65, 1, &charge_voltage) < 0) { + dev_warn(ddata->dev, "Can not read battery nvmem device. Assuming generic lipo battery\n"); + goto unknown; } + + /* design capacity */ + mul_idx -= 2; + + if (mul_idx < ARRAY_SIZE(multipliers)) + v = multipliers[mul_idx]; + else + v = 10; + + cpcap_battery_mot_data.info.charge_full_design = 1000 * v * capacity; + + /* design max voltage */ + v = 1000 * ((16702 * charge_voltage) / 1000 + 1260); + cpcap_battery_mot_data.info.voltage_max_design = v; + + /* design min voltage */ + if (v > 4200000) + cpcap_battery_mot_data.info.voltage_min_design = 3100000; + else + cpcap_battery_mot_data.info.voltage_min_design = 3200000; + + ddata->config = cpcap_battery_mot_data; + + return; + +unknown: + ddata->check_nvmem = true; + ddata->config = cpcap_battery_unkown_data; } /**