From patchwork Thu Jan 6 11:06:03 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hans de Goede X-Patchwork-Id: 530630 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1ED64C433EF for ; Thu, 6 Jan 2022 11:06:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238274AbiAFLGQ (ORCPT ); Thu, 6 Jan 2022 06:06:16 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]:49035 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238265AbiAFLGQ (ORCPT ); Thu, 6 Jan 2022 06:06:16 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1641467175; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=0a0x/yg3X1N24efHRlEKIGZaiGyQzHGcCtxoM1WjAaw=; b=M5WNW2EMXcA4wgvEhONH3oOzIvgWxh1XPcQXAmfTlAwG+7eKOEGxq0BEiASQLUK//v94xT 0Y4r3yMZKrbvDAoSTm/WAidBNWUmKedL0qqtHy6TYI1BkMbilF+X6XO8ezQZzVndGOegRG LlxemA4KI9s7lps/Cjzwa7aiMOmP+Po= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-433-bcUlInanMdC4o3EXzOmRlg-1; Thu, 06 Jan 2022 06:06:12 -0500 X-MC-Unique: bcUlInanMdC4o3EXzOmRlg-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 79A861F2DA; Thu, 6 Jan 2022 11:06:11 +0000 (UTC) Received: from shalem.redhat.com (unknown [10.39.192.137]) by smtp.corp.redhat.com (Postfix) with ESMTP id ABA4D752AE; Thu, 6 Jan 2022 11:06:10 +0000 (UTC) From: Hans de Goede To: Sebastian Reichel Cc: Hans de Goede , linux-pm@vger.kernel.org Subject: [PATCH 2/7] power: supply: axp288_fuel_gauge: Add axp288_fuel_gauge_read_initial_regs() Date: Thu, 6 Jan 2022 12:06:03 +0100 Message-Id: <20220106110608.66231-2-hdegoede@redhat.com> In-Reply-To: <20220106110608.66231-1-hdegoede@redhat.com> References: <20220106110608.66231-1-hdegoede@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org Refactor probe a bit, introducing a new axp288_fuel_gauge_read_initial_regs() helper. This replaces a whole bunch of gotos and removes the unblock_punit_i2c_access label. Signed-off-by: Hans de Goede --- drivers/power/supply/axp288_fuel_gauge.c | 109 ++++++++++++----------- 1 file changed, 56 insertions(+), 53 deletions(-) diff --git a/drivers/power/supply/axp288_fuel_gauge.c b/drivers/power/supply/axp288_fuel_gauge.c index 1495402f440c..35f9edf3da09 100644 --- a/drivers/power/supply/axp288_fuel_gauge.c +++ b/drivers/power/supply/axp288_fuel_gauge.c @@ -611,6 +611,61 @@ static const struct dmi_system_id axp288_no_battery_list[] = { {} }; +static int axp288_fuel_gauge_read_initial_regs(struct axp288_fg_info *info) +{ + unsigned int val; + int ret; + + /* + * On some devices the fuelgauge and charger parts of the axp288 are + * not used, check that the fuelgauge is enabled (CC_CTRL != 0). + */ + ret = regmap_read(info->regmap, AXP20X_CC_CTRL, &val); + if (ret < 0) + return ret; + if (val == 0) + return -ENODEV; + + ret = fuel_gauge_reg_readb(info, AXP288_FG_DES_CAP1_REG); + if (ret < 0) + return ret; + + if (!(ret & FG_DES_CAP1_VALID)) { + dev_err(info->dev, "axp288 not configured by firmware\n"); + return -ENODEV; + } + + ret = fuel_gauge_reg_readb(info, AXP20X_CHRG_CTRL1); + if (ret < 0) + return ret; + switch ((ret & CHRG_CCCV_CV_MASK) >> CHRG_CCCV_CV_BIT_POS) { + case CHRG_CCCV_CV_4100MV: + info->max_volt = 4100; + break; + case CHRG_CCCV_CV_4150MV: + info->max_volt = 4150; + break; + case CHRG_CCCV_CV_4200MV: + info->max_volt = 4200; + break; + case CHRG_CCCV_CV_4350MV: + info->max_volt = 4350; + break; + } + + ret = fuel_gauge_reg_readb(info, AXP20X_PWR_OP_MODE); + if (ret < 0) + return ret; + info->pwr_op = ret; + + ret = fuel_gauge_reg_readb(info, AXP288_FG_LOW_CAP_REG); + if (ret < 0) + return ret; + info->low_cap = ret; + + return 0; +} + static int axp288_fuel_gauge_probe(struct platform_device *pdev) { int i, ret = 0; @@ -623,7 +678,6 @@ static int axp288_fuel_gauge_probe(struct platform_device *pdev) [BAT_VOLT] = "axp288-batt-volt", }; struct device *dev = &pdev->dev; - unsigned int val; if (dmi_check_system(axp288_no_battery_list)) return -ENODEV; @@ -665,59 +719,8 @@ static int axp288_fuel_gauge_probe(struct platform_device *pdev) if (ret < 0) goto out_free_iio_chan; - /* - * On some devices the fuelgauge and charger parts of the axp288 are - * not used, check that the fuelgauge is enabled (CC_CTRL != 0). - */ - ret = regmap_read(axp20x->regmap, AXP20X_CC_CTRL, &val); - if (ret < 0) - goto unblock_punit_i2c_access; - if (val == 0) { - ret = -ENODEV; - goto unblock_punit_i2c_access; - } - - ret = fuel_gauge_reg_readb(info, AXP288_FG_DES_CAP1_REG); - if (ret < 0) - goto unblock_punit_i2c_access; - - if (!(ret & FG_DES_CAP1_VALID)) { - dev_err(&pdev->dev, "axp288 not configured by firmware\n"); - ret = -ENODEV; - goto unblock_punit_i2c_access; - } - - ret = fuel_gauge_reg_readb(info, AXP20X_CHRG_CTRL1); - if (ret < 0) - goto unblock_punit_i2c_access; - switch ((ret & CHRG_CCCV_CV_MASK) >> CHRG_CCCV_CV_BIT_POS) { - case CHRG_CCCV_CV_4100MV: - info->max_volt = 4100; - break; - case CHRG_CCCV_CV_4150MV: - info->max_volt = 4150; - break; - case CHRG_CCCV_CV_4200MV: - info->max_volt = 4200; - break; - case CHRG_CCCV_CV_4350MV: - info->max_volt = 4350; - break; - } - - ret = fuel_gauge_reg_readb(info, AXP20X_PWR_OP_MODE); - if (ret < 0) - goto unblock_punit_i2c_access; - info->pwr_op = ret; - - ret = fuel_gauge_reg_readb(info, AXP288_FG_LOW_CAP_REG); - if (ret < 0) - goto unblock_punit_i2c_access; - info->low_cap = ret; - -unblock_punit_i2c_access: + ret = axp288_fuel_gauge_read_initial_regs(info); iosf_mbi_unblock_punit_i2c_access(); - /* In case we arrive here by goto because of a register access error */ if (ret < 0) goto out_free_iio_chan; From patchwork Thu Jan 6 11:06:05 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hans de Goede X-Patchwork-Id: 530629 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 08152C433EF for ; Thu, 6 Jan 2022 11:06:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238265AbiAFLGl (ORCPT ); Thu, 6 Jan 2022 06:06:41 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:28271 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238255AbiAFLGk (ORCPT ); Thu, 6 Jan 2022 06:06:40 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1641467200; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ZpZwPW5uLZqgBcXjmX/3wTTtHQm+TlsQWAQ2uAoGqbI=; b=ShFWr2n9q2KDmeXO0pemWjUfHN6E/tTFOCKeJ5labHZCTVLa7Nl2w54N835ewYlvPsXMRA hGX3dIYQExhKPK/Xam/EJs88zG9/Gy5TI0La4Lf+cgQQYGyRrFAobmxV8h9wA8QKaDsSzc bubuoRQiy9n3Z3ZcjsWLDFg3o5Twiwo= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-310-SjJ6WmBENwKjrlb6G4Orlw-1; Thu, 06 Jan 2022 06:06:39 -0500 X-MC-Unique: SjJ6WmBENwKjrlb6G4Orlw-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 2687818C8C00; Thu, 6 Jan 2022 11:06:38 +0000 (UTC) Received: from shalem.redhat.com (unknown [10.39.192.137]) by smtp.corp.redhat.com (Postfix) with ESMTP id E103F752B4; Thu, 6 Jan 2022 11:06:12 +0000 (UTC) From: Hans de Goede To: Sebastian Reichel Cc: Hans de Goede , linux-pm@vger.kernel.org Subject: [PATCH 4/7] power: supply: axp288_fuel_gauge: Use devm_power_supply_register() Date: Thu, 6 Jan 2022 12:06:05 +0100 Message-Id: <20220106110608.66231-4-hdegoede@redhat.com> In-Reply-To: <20220106110608.66231-1-hdegoede@redhat.com> References: <20220106110608.66231-1-hdegoede@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org Use devm_power_supply_register() instead of power_supply_register(). Note as a side-effect this changes the release order so that now first the IRQs get free-ed and then the psy gets unregistered. This is actually a bug-fix since this fixes the IRQ possibly trying to reference the unregistered psy. Signed-off-by: Hans de Goede --- drivers/power/supply/axp288_fuel_gauge.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/drivers/power/supply/axp288_fuel_gauge.c b/drivers/power/supply/axp288_fuel_gauge.c index aaf2d5542316..cefde85e3309 100644 --- a/drivers/power/supply/axp288_fuel_gauge.c +++ b/drivers/power/supply/axp288_fuel_gauge.c @@ -740,7 +740,7 @@ static int axp288_fuel_gauge_probe(struct platform_device *pdev) return ret; psy_cfg.drv_data = info; - info->bat = power_supply_register(dev, &fuel_gauge_desc, &psy_cfg); + info->bat = devm_power_supply_register(dev, &fuel_gauge_desc, &psy_cfg); if (IS_ERR(info->bat)) { ret = PTR_ERR(info->bat); dev_err(dev, "failed to register battery: %d\n", ret); @@ -763,8 +763,6 @@ static int axp288_fuel_gauge_remove(struct platform_device *pdev) struct axp288_fg_info *info = platform_get_drvdata(pdev); int i; - power_supply_unregister(info->bat); - for (i = 0; i < AXP288_FG_INTR_NUM; i++) if (info->irq[i] >= 0) free_irq(info->irq[i], info); From patchwork Thu Jan 6 11:06:07 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hans de Goede X-Patchwork-Id: 530628 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 42E8EC433EF for ; Thu, 6 Jan 2022 11:06:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238270AbiAFLGs (ORCPT ); Thu, 6 Jan 2022 06:06:48 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]:32090 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238255AbiAFLGs (ORCPT ); Thu, 6 Jan 2022 06:06:48 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1641467207; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=TEZz19IJXiSYRFXaaWaN3BgN60XEt8AIczfPPTNkTVc=; b=FQt8gQdCJODQP7nEJfdK15V+fpx5uBtwyD8vbosedsKyfpnsiMQopmuT29BduoQYn7lk0v 7VBTV5/oCa+cPIg5q/Pk/VlUugSTC7l2jcOcvpCnDtMgYTJ+U5MkDysie9YLNOgeJwFx96 6R7/koNNezda/UHVOYKE0tCO8iI1AhM= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-171-E9AYhI2MO0q__YaG31u1kw-1; Thu, 06 Jan 2022 06:06:46 -0500 X-MC-Unique: E9AYhI2MO0q__YaG31u1kw-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 8FE0E1006AA7; Thu, 6 Jan 2022 11:06:45 +0000 (UTC) Received: from shalem.redhat.com (unknown [10.39.192.137]) by smtp.corp.redhat.com (Postfix) with ESMTP id F2B9167C99; Thu, 6 Jan 2022 11:06:41 +0000 (UTC) From: Hans de Goede To: Sebastian Reichel Cc: Hans de Goede , linux-pm@vger.kernel.org Subject: [PATCH 6/7] power: supply: axp288_fuel_gauge: Take lock before updating the valid flag Date: Thu, 6 Jan 2022 12:06:07 +0100 Message-Id: <20220106110608.66231-6-hdegoede@redhat.com> In-Reply-To: <20220106110608.66231-1-hdegoede@redhat.com> References: <20220106110608.66231-1-hdegoede@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org The valid flag is protected by the mutex, so code clearing it should take the mutex before cleating the valid flag. Signed-off-by: Hans de Goede --- drivers/power/supply/axp288_fuel_gauge.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/drivers/power/supply/axp288_fuel_gauge.c b/drivers/power/supply/axp288_fuel_gauge.c index f7dce029266a..53d0e82bbb3e 100644 --- a/drivers/power/supply/axp288_fuel_gauge.c +++ b/drivers/power/supply/axp288_fuel_gauge.c @@ -476,7 +476,9 @@ static irqreturn_t fuel_gauge_thread_handler(int irq, void *dev) dev_warn(info->dev, "Spurious Interrupt!!!\n"); } + mutex_lock(&info->lock); info->valid = 0; /* Force updating of the cached registers */ + mutex_unlock(&info->lock); power_supply_changed(info->bat); return IRQ_HANDLED; @@ -486,7 +488,9 @@ static void fuel_gauge_external_power_changed(struct power_supply *psy) { struct axp288_fg_info *info = power_supply_get_drvdata(psy); + mutex_lock(&info->lock); info->valid = 0; /* Force updating of the cached registers */ + mutex_unlock(&info->lock); power_supply_changed(info->bat); }