From patchwork Mon Nov 28 09:28:48 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: 629393 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 B1071C433FE for ; Mon, 28 Nov 2022 09:30:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229908AbiK1JaH (ORCPT ); Mon, 28 Nov 2022 04:30:07 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54124 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230026AbiK1JaG (ORCPT ); Mon, 28 Nov 2022 04:30:06 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B035FE5F for ; Mon, 28 Nov 2022 01:29:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1669627746; 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=SDskJ7vo1a1Hiq3qrkNtaRfwZXgO3pqn8Y5UzwULJ20=; b=Dfgmmi8sWr92LG4G2DCOhX9gtO044djpkrofbqEl3XsRqZ2MIbOGIAzlPB4W8MoUu0fu43 2U/vfTXKjN78BsxOOPEdJh7rIxjjMlSEHPKNz8ZqUYc16mODbbFoF4mwj7k1jaKuHRbpgp UC3ylNE8fgy3Y+C1c1zpJTAlZPsC2J4= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-259-xs28wl66NjmOt_juApACWg-1; Mon, 28 Nov 2022 04:29:01 -0500 X-MC-Unique: xs28wl66NjmOt_juApACWg-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 91C43886063; Mon, 28 Nov 2022 09:29:01 +0000 (UTC) Received: from x1.localdomain.com (unknown [10.39.195.70]) by smtp.corp.redhat.com (Postfix) with ESMTP id A184CFD48; Mon, 28 Nov 2022 09:29:00 +0000 (UTC) From: Hans de Goede To: Sebastian Reichel , Marek Vasut Cc: Hans de Goede , linux-pm@vger.kernel.org Subject: [PATCH v2 1/9] power: supply: bq25890: Ensure pump_express_work is cancelled on remove Date: Mon, 28 Nov 2022 10:28:48 +0100 Message-Id: <20221128092856.71619-2-hdegoede@redhat.com> In-Reply-To: <20221128092856.71619-1-hdegoede@redhat.com> References: <20221128092856.71619-1-hdegoede@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.5 Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org The pump_express_work which gets queued from an external_power_changed callback might be pending / running on remove() (or on probe failure). Add a devm action cancelling the work, to ensure that it is cancelled. Note the devm action is added before devm_power_supply_register(), making it run after devm unregisters the power_supply, so that the work cannot be queued anymore (this is also why a devm action is used for this). Fixes: 48f45b094dbb ("power: supply: bq25890: Support higher charging voltages through Pump Express+ protocol") Reviewed-by: Marek Vasut Signed-off-by: Hans de Goede --- Changes in v2: - Add comment that the devm_add_action() call must be done before devm registering the power_supply, to guarantee it running after the unregister --- drivers/power/supply/bq25890_charger.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/drivers/power/supply/bq25890_charger.c b/drivers/power/supply/bq25890_charger.c index 512c81662eea..866c475bb735 100644 --- a/drivers/power/supply/bq25890_charger.c +++ b/drivers/power/supply/bq25890_charger.c @@ -1317,6 +1317,13 @@ static int bq25890_fw_probe(struct bq25890_device *bq) return 0; } +static void bq25890_non_devm_cleanup(void *data) +{ + struct bq25890_device *bq = data; + + cancel_delayed_work_sync(&bq->pump_express_work); +} + static int bq25890_probe(struct i2c_client *client) { struct device *dev = &client->dev; @@ -1372,6 +1379,14 @@ static int bq25890_probe(struct i2c_client *client) /* OTG reporting */ bq->usb_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2); + /* + * This must be before bq25890_power_supply_init(), so that it runs + * after devm unregisters the power_supply. + */ + ret = devm_add_action_or_reset(dev, bq25890_non_devm_cleanup, bq); + if (ret) + return ret; + ret = bq25890_register_regulator(bq); if (ret) return ret; From patchwork Mon Nov 28 09:28:50 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: 629394 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 6BBFFC43217 for ; Mon, 28 Nov 2022 09:30:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229776AbiK1JaE (ORCPT ); Mon, 28 Nov 2022 04:30:04 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54164 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229908AbiK1JaD (ORCPT ); Mon, 28 Nov 2022 04:30:03 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5D6CAB86D for ; Mon, 28 Nov 2022 01:29:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1669627746; 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=243iB7U9lv4nhx9+pqyOLyLiMw7uTKRZEX4fAFDSLBA=; b=gjb5QAwuz0dGH7fQ0kjbhqOoq6bGdZj089Oz/Z3a9wd5FL4Yc1zrDVmdMlQlCeMYbpG7Ky QePh+k06ptFElQOXNwMdOCKea0sqEoWpbi2CY2ugrKGn18XcWMZsCuklS5/CviqHwYV2qz 5O+VhDb9AE3fEE2CbsDbDXGorMatIe0= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-401-ahFtzbS7PIK98F9Fe1PyzA-1; Mon, 28 Nov 2022 04:29:04 -0500 X-MC-Unique: ahFtzbS7PIK98F9Fe1PyzA-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 19A85857AB7; Mon, 28 Nov 2022 09:29:04 +0000 (UTC) Received: from x1.localdomain.com (unknown [10.39.195.70]) by smtp.corp.redhat.com (Postfix) with ESMTP id 080E7FD48; Mon, 28 Nov 2022 09:29:02 +0000 (UTC) From: Hans de Goede To: Sebastian Reichel , Marek Vasut Cc: Hans de Goede , linux-pm@vger.kernel.org Subject: [PATCH v2 3/9] power: supply: bq25890: Factor out chip state update Date: Mon, 28 Nov 2022 10:28:50 +0100 Message-Id: <20221128092856.71619-4-hdegoede@redhat.com> In-Reply-To: <20221128092856.71619-1-hdegoede@redhat.com> References: <20221128092856.71619-1-hdegoede@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.5 Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org From: Marek Vasut Pull the chip state and ADC conversion update functionality out into separate function, so it can be reused elsewhere in the driver. This is a preparatory patch, no functional change. Reviewed-by: Hans de Goede Signed-off-by: Marek Vasut Signed-off-by: Hans de Goede --- drivers/power/supply/bq25890_charger.c | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/drivers/power/supply/bq25890_charger.c b/drivers/power/supply/bq25890_charger.c index 2d731ea58323..e9f5964f9dcd 100644 --- a/drivers/power/supply/bq25890_charger.c +++ b/drivers/power/supply/bq25890_charger.c @@ -454,20 +454,18 @@ static int bq25890_get_vbus_voltage(struct bq25890_device *bq) return bq25890_find_val(ret, TBL_VBUSV); } -static int bq25890_power_supply_get_property(struct power_supply *psy, - enum power_supply_property psp, - union power_supply_propval *val) +static void bq25890_update_state(struct bq25890_device *bq, + enum power_supply_property psp, + struct bq25890_state *state) { - struct bq25890_device *bq = power_supply_get_drvdata(psy); - struct bq25890_state state; bool do_adc_conv; int ret; mutex_lock(&bq->lock); /* update state in case we lost an interrupt */ __bq25890_handle_irq(bq); - state = bq->state; - do_adc_conv = !state.online && bq25890_is_adc_property(psp); + *state = bq->state; + do_adc_conv = !state->online && bq25890_is_adc_property(psp); if (do_adc_conv) bq25890_field_write(bq, F_CONV_START, 1); mutex_unlock(&bq->lock); @@ -475,6 +473,17 @@ static int bq25890_power_supply_get_property(struct power_supply *psy, if (do_adc_conv) regmap_field_read_poll_timeout(bq->rmap_fields[F_CONV_START], ret, !ret, 25000, 1000000); +} + +static int bq25890_power_supply_get_property(struct power_supply *psy, + enum power_supply_property psp, + union power_supply_propval *val) +{ + struct bq25890_device *bq = power_supply_get_drvdata(psy); + struct bq25890_state state; + int ret; + + bq25890_update_state(bq, psp, &state); switch (psp) { case POWER_SUPPLY_PROP_STATUS: From patchwork Mon Nov 28 09:28:52 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: 629390 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 8FB56C46467 for ; Mon, 28 Nov 2022 09:30:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230174AbiK1JaO (ORCPT ); Mon, 28 Nov 2022 04:30:14 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54182 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230088AbiK1JaM (ORCPT ); Mon, 28 Nov 2022 04:30:12 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6B5E3B7E1 for ; Mon, 28 Nov 2022 01:29:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1669627750; 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=5djBAAxpvMvf3hkruGFEa517Y731hVNn5P2G+FLGcDg=; b=A8kmEjFFrtTbCDVfgAKh54W8r953GDJOHEBZZs4PzzRy+RqhDyUEZPIALhL+oq83Qwv1oQ Qduf6gr6cJ8ZCcmpi21x9fXu5FrFPi2pyY6R+oIc28ZGkyvcOMl9YB8UuUl9MTFpUGWs3E sBd+BZVIGoo9YUmBcn96NLF+0+REQNY= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-382-BaHB9ZWAMW2kcL4paH6i7A-1; Mon, 28 Nov 2022 04:29:06 -0500 X-MC-Unique: BaHB9ZWAMW2kcL4paH6i7A-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id A21D729ABA0F; Mon, 28 Nov 2022 09:29:06 +0000 (UTC) Received: from x1.localdomain.com (unknown [10.39.195.70]) by smtp.corp.redhat.com (Postfix) with ESMTP id 7A2F435429; Mon, 28 Nov 2022 09:29:05 +0000 (UTC) From: Hans de Goede To: Sebastian Reichel , Marek Vasut Cc: Hans de Goede , linux-pm@vger.kernel.org Subject: [PATCH v2 5/9] power: supply: bq25890: Fix setting of F_CONV_RATE rate when disabling HiZ mode Date: Mon, 28 Nov 2022 10:28:52 +0100 Message-Id: <20221128092856.71619-6-hdegoede@redhat.com> In-Reply-To: <20221128092856.71619-1-hdegoede@redhat.com> References: <20221128092856.71619-1-hdegoede@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.5 Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org The recent "power: supply: bq25890: Add HiZ mode support" change leaves F_CONV_RATE rate unset when disabling HiZ mode (setting POWER_SUPPLY_PROP_ONLINE to 1) while a charger is connected. Separate the resetting HiZ mode (when necessary because of a charger (re)plug event) into its own "if {}" block which runs first. And fix the setting of F_CONV_RATE rate by adding helper variables for the old and new F_CONV_RATE state which check both the online and hiz bits and then compare the helper variables to see if a F_CONV_RATE update is necessary. Reviewed-by: Marek Vasut Signed-off-by: Hans de Goede --- drivers/power/supply/bq25890_charger.c | 41 +++++++++++--------------- 1 file changed, 17 insertions(+), 24 deletions(-) diff --git a/drivers/power/supply/bq25890_charger.c b/drivers/power/supply/bq25890_charger.c index f5fa39dca832..0d188c0d94ff 100644 --- a/drivers/power/supply/bq25890_charger.c +++ b/drivers/power/supply/bq25890_charger.c @@ -795,6 +795,7 @@ static int bq25890_get_chip_state(struct bq25890_device *bq, static irqreturn_t __bq25890_handle_irq(struct bq25890_device *bq) { + bool adc_conv_rate, new_adc_conv_rate; struct bq25890_state new_state; int ret; @@ -805,33 +806,25 @@ static irqreturn_t __bq25890_handle_irq(struct bq25890_device *bq) if (!memcmp(&bq->state, &new_state, sizeof(new_state))) return IRQ_NONE; - /* power removed or HiZ */ - if ((!new_state.online || new_state.hiz) && bq->state.online) { - /* disable ADC */ - ret = bq25890_field_write(bq, F_CONV_RATE, 0); + /* + * Restore HiZ bit in case it was set by user. The chip does not retain + * this bit on cable replug, hence the bit must be reset manually here. + */ + if (new_state.online && !bq->state.online && bq->force_hiz) { + ret = bq25890_field_write(bq, F_EN_HIZ, bq->force_hiz); if (ret < 0) goto error; - } else if (new_state.online && !bq->state.online) { - /* - * Restore HiZ bit in case it was set by user. - * The chip does not retain this bit once the - * cable is re-plugged, hence the bit must be - * reset manually here. - */ - if (bq->force_hiz) { - ret = bq25890_field_write(bq, F_EN_HIZ, bq->force_hiz); - if (ret < 0) - goto error; - new_state.hiz = 1; - } + new_state.hiz = 1; + } - if (!new_state.hiz) { - /* power inserted and not HiZ */ - /* enable ADC, to have control of charge current/voltage */ - ret = bq25890_field_write(bq, F_CONV_RATE, 1); - if (ret < 0) - goto error; - } + /* Should period ADC sampling be enabled? */ + adc_conv_rate = bq->state.online && !bq->state.hiz; + new_adc_conv_rate = new_state.online && !new_state.hiz; + + if (new_adc_conv_rate != adc_conv_rate) { + ret = bq25890_field_write(bq, F_CONV_RATE, new_adc_conv_rate); + if (ret < 0) + goto error; } bq->state = new_state; From patchwork Mon Nov 28 09:28:54 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: 629392 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 18CE6C43217 for ; Mon, 28 Nov 2022 09:30:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230016AbiK1JaL (ORCPT ); Mon, 28 Nov 2022 04:30:11 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54304 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230064AbiK1JaJ (ORCPT ); Mon, 28 Nov 2022 04:30:09 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 09E8E12D1A for ; Mon, 28 Nov 2022 01:29:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1669627752; 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=MYue9vNH+4tqOqrnK18S1M7Ioxkt9jY9OIFGxyJaw5U=; b=GnIfarFR2xHOmpf/JwBsAkW5TLuFYzpJ6GgZ8dfXFBV1sWYeGsezB+HlOEo4JnfwEp4cSl MSVsU6jiz2ld1fFyBhgMQVqzgllWvVUJrEqWqiyZumOo94a20m651PVI6rkmwOiYV/Civ3 D2ZdmHmHcKQuKYWLIaeEHof8/xQaXx0= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-344-CTU3_-iwMBWLtZ3zp8ehqg-1; Mon, 28 Nov 2022 04:29:09 -0500 X-MC-Unique: CTU3_-iwMBWLtZ3zp8ehqg-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 984F2802314; Mon, 28 Nov 2022 09:29:09 +0000 (UTC) Received: from x1.localdomain.com (unknown [10.39.195.70]) by smtp.corp.redhat.com (Postfix) with ESMTP id 40384FD48; Mon, 28 Nov 2022 09:29:08 +0000 (UTC) From: Hans de Goede To: Sebastian Reichel , Marek Vasut Cc: Hans de Goede , linux-pm@vger.kernel.org Subject: [PATCH v2 7/9] power: supply: bq25890: Support boards with more then one charger IC Date: Mon, 28 Nov 2022 10:28:54 +0100 Message-Id: <20221128092856.71619-8-hdegoede@redhat.com> In-Reply-To: <20221128092856.71619-1-hdegoede@redhat.com> References: <20221128092856.71619-1-hdegoede@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.5 Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org Some devices, such as the Lenovo Yoga Tab 3 Pro (YT3-X90F) have multiple batteries with a separate bq25890 charger for each battery. This requires the bq25890_charger code to use a unique name per registered power_supply class device, rather then hardcoding "bq25890-charger" as power_supply class device name. Add a "-%d" prefix to the name, allocated through idr in the same way as several other power_supply drivers are already doing this. Note this also updates: drivers/platform/x86/x86-android-tablets.c which refers to the charger by power_supply-class-device-name for the purpose of setting the "supplied-from" property on the fuel-gauge to this name. Reviewed-by: Marek Vasut Signed-off-by: Hans de Goede --- As the subsystem maintainer for drivers/platform/x86 I'm fine with the small x86-android-tablets.c being merged through the linux-power-supply tree. Also note I did a full grep for "bq25890-charger" and x86-android-tablets.c is the only file in the kernel tree referring to this name. --- drivers/platform/x86/x86-android-tablets.c | 2 +- drivers/power/supply/bq25890_charger.c | 29 +++++++++++++++++++--- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/drivers/platform/x86/x86-android-tablets.c b/drivers/platform/x86/x86-android-tablets.c index ad98b557cd75..5a7adcc76b1e 100644 --- a/drivers/platform/x86/x86-android-tablets.c +++ b/drivers/platform/x86/x86-android-tablets.c @@ -187,7 +187,7 @@ struct x86_dev_info { /* Generic / shared charger / battery settings */ static const char * const tusb1211_chg_det_psy[] = { "tusb1211-charger-detect" }; static const char * const bq24190_psy[] = { "bq24190-charger" }; -static const char * const bq25890_psy[] = { "bq25890-charger" }; +static const char * const bq25890_psy[] = { "bq25890-charger-0" }; static const struct property_entry fg_bq24190_supply_props[] = { PROPERTY_ENTRY_STRING_ARRAY("supplied-from", bq24190_psy), diff --git a/drivers/power/supply/bq25890_charger.c b/drivers/power/supply/bq25890_charger.c index 9b3a173b316a..30854d08bba9 100644 --- a/drivers/power/supply/bq25890_charger.c +++ b/drivers/power/supply/bq25890_charger.c @@ -108,6 +108,9 @@ struct bq25890_device { struct i2c_client *client; struct device *dev; struct power_supply *charger; + struct power_supply_desc desc; + char name[28]; /* "bq25890-charger-%d" */ + int id; struct usb_phy *usb_phy; struct notifier_block usb_nb; @@ -129,6 +132,9 @@ struct bq25890_device { struct mutex lock; /* protect state data */ }; +static DEFINE_IDR(bq25890_id); +static DEFINE_MUTEX(bq25890_id_mutex); + static const struct regmap_range bq25890_readonly_reg_ranges[] = { regmap_reg_range(0x0b, 0x0c), regmap_reg_range(0x0e, 0x13), @@ -989,7 +995,6 @@ static char *bq25890_charger_supplied_to[] = { }; static const struct power_supply_desc bq25890_power_supply_desc = { - .name = "bq25890-charger", .type = POWER_SUPPLY_TYPE_USB, .properties = bq25890_power_supply_props, .num_properties = ARRAY_SIZE(bq25890_power_supply_props), @@ -1003,12 +1008,21 @@ static int bq25890_power_supply_init(struct bq25890_device *bq) { struct power_supply_config psy_cfg = { .drv_data = bq, }; + /* Get ID for the device */ + mutex_lock(&bq25890_id_mutex); + bq->id = idr_alloc(&bq25890_id, bq, 0, 0, GFP_KERNEL); + mutex_unlock(&bq25890_id_mutex); + if (bq->id < 0) + return bq->id; + + snprintf(bq->name, sizeof(bq->name), "bq25890-charger-%d", bq->id); + bq->desc = bq25890_power_supply_desc; + bq->desc.name = bq->name; + psy_cfg.supplied_to = bq25890_charger_supplied_to; psy_cfg.num_supplicants = ARRAY_SIZE(bq25890_charger_supplied_to); - bq->charger = devm_power_supply_register(bq->dev, - &bq25890_power_supply_desc, - &psy_cfg); + bq->charger = devm_power_supply_register(bq->dev, &bq->desc, &psy_cfg); return PTR_ERR_OR_ZERO(bq->charger); } @@ -1354,6 +1368,12 @@ static void bq25890_non_devm_cleanup(void *data) struct bq25890_device *bq = data; cancel_delayed_work_sync(&bq->pump_express_work); + + if (bq->id >= 0) { + mutex_lock(&bq25890_id_mutex); + idr_remove(&bq25890_id, bq->id); + mutex_unlock(&bq25890_id_mutex); + } } static int bq25890_probe(struct i2c_client *client) @@ -1368,6 +1388,7 @@ static int bq25890_probe(struct i2c_client *client) bq->client = client; bq->dev = dev; + bq->id = -1; mutex_init(&bq->lock); INIT_DELAYED_WORK(&bq->pump_express_work, bq25890_pump_express_work); From patchwork Mon Nov 28 09:28:56 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: 629391 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 0BA1DC43217 for ; Mon, 28 Nov 2022 09:30:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229968AbiK1JaO (ORCPT ); Mon, 28 Nov 2022 04:30:14 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54362 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230087AbiK1JaM (ORCPT ); Mon, 28 Nov 2022 04:30:12 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AC155B857 for ; Mon, 28 Nov 2022 01:29:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1669627759; 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=y6zo7UshplGCzPVwuZtrAadrm7Qqig9g2tLMRoXZank=; b=jRJEmwfXhqYaNTb7MDDoaooxPbBYvO9MuiFv78+Y7oxjMBwnpxHakPu1wySQMQ0tqOGwd4 s2E2QA3lxihIeIby0gSuH18ot41cJIvS6aUN+TQLOe3d5yulzMtJEzjNa6uHuHove3E/vT A3UDXEesgtYWZPUx9DwQpYDJjeucqZo= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-452-cf37KDH_O2Wob8oN6qjovA-1; Mon, 28 Nov 2022 04:29:13 -0500 X-MC-Unique: cf37KDH_O2Wob8oN6qjovA-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id A0723801585; Mon, 28 Nov 2022 09:29:12 +0000 (UTC) Received: from x1.localdomain.com (unknown [10.39.195.70]) by smtp.corp.redhat.com (Postfix) with ESMTP id B571335429; Mon, 28 Nov 2022 09:29:11 +0000 (UTC) From: Hans de Goede To: Sebastian Reichel , Marek Vasut Cc: Hans de Goede , linux-pm@vger.kernel.org Subject: [PATCH v2 9/9] power: supply: bq25890: Add new linux,iinlim-percentage property Date: Mon, 28 Nov 2022 10:28:56 +0100 Message-Id: <20221128092856.71619-10-hdegoede@redhat.com> In-Reply-To: <20221128092856.71619-1-hdegoede@redhat.com> References: <20221128092856.71619-1-hdegoede@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.5 Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org Some devices, such as the Lenovo Yoga Tab 3 Pro (YT3-X90F) have multiple batteries with a separate bq25890 charger for each battery. This requires the maximum current the external power-supply can deliver to be divided over the chargers. The Android vendor kernel shipped on the YT3-X90F divides this current with a 40/60 percent split so that batteries are done charging at approx. the same time if both were fully empty at the start. Add support for a new "linux,iinlim-percentage" percentage property which can be set to indicate that a bq25890 charger should only use that percentage of the external power-supply's maximum current. So far this new property is only used on x86/ACPI (non devicetree) devs, IOW it is not used in actual devicetree files. The devicetree-bindings maintainers have requested properties like these to not be added to the devicetree-bindings, so the new property is deliberately not added to the existing devicetree-bindings. Signed-off-by: Hans de Goede --- Changes in v2: - Check that "linux,iinlim-percentage" is not > 100 when reading it --- drivers/power/supply/bq25890_charger.c | 31 +++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/drivers/power/supply/bq25890_charger.c b/drivers/power/supply/bq25890_charger.c index aff55bf3ecc3..bfe08d7bfaf3 100644 --- a/drivers/power/supply/bq25890_charger.c +++ b/drivers/power/supply/bq25890_charger.c @@ -126,6 +126,7 @@ struct bq25890_device { bool read_back_init_data; bool force_hiz; u32 pump_express_vbus_max; + u32 iinlim_percentage; enum bq25890_chip_version chip_version; struct bq25890_init_data init_data; struct bq25890_state state; @@ -727,6 +728,18 @@ static int bq25890_power_supply_property_is_writeable(struct power_supply *psy, } } +/* + * If there are multiple chargers the maximum current the external power-supply + * can deliver needs to be divided over the chargers. This is done according + * to the bq->iinlim_percentage setting. + */ +static int bq25890_charger_get_scaled_iinlim_regval(struct bq25890_device *bq, + int iinlim_ua) +{ + iinlim_ua = iinlim_ua * bq->iinlim_percentage / 100; + return bq25890_find_idx(iinlim_ua, TBL_IINLIM); +} + /* On the BQ25892 try to get charger-type info from our supplier */ static void bq25890_charger_external_power_changed(struct power_supply *psy) { @@ -745,7 +758,7 @@ static void bq25890_charger_external_power_changed(struct power_supply *psy) switch (val.intval) { case POWER_SUPPLY_USB_TYPE_DCP: - input_current_limit = bq25890_find_idx(2000000, TBL_IINLIM); + input_current_limit = bq25890_charger_get_scaled_iinlim_regval(bq, 2000000); if (bq->pump_express_vbus_max) { queue_delayed_work(system_power_efficient_wq, &bq->pump_express_work, @@ -754,11 +767,11 @@ static void bq25890_charger_external_power_changed(struct power_supply *psy) break; case POWER_SUPPLY_USB_TYPE_CDP: case POWER_SUPPLY_USB_TYPE_ACA: - input_current_limit = bq25890_find_idx(1500000, TBL_IINLIM); + input_current_limit = bq25890_charger_get_scaled_iinlim_regval(bq, 1500000); break; case POWER_SUPPLY_USB_TYPE_SDP: default: - input_current_limit = bq25890_find_idx(500000, TBL_IINLIM); + input_current_limit = bq25890_charger_get_scaled_iinlim_regval(bq, 500000); } bq25890_field_write(bq, F_IINLIM, input_current_limit); @@ -1378,6 +1391,7 @@ static int bq25890_fw_probe(struct bq25890_device *bq) int ret; struct bq25890_init_data *init = &bq->init_data; const char *str; + u32 val; ret = device_property_read_string(bq->dev, "linux,secondary-charger-name", &str); if (ret == 0) { @@ -1390,6 +1404,17 @@ static int bq25890_fw_probe(struct bq25890_device *bq) device_property_read_u32(bq->dev, "linux,pump-express-vbus-max", &bq->pump_express_vbus_max); + ret = device_property_read_u32(bq->dev, "linux,iinlim-percentage", &val); + if (ret == 0) { + if (val > 100) { + dev_err(bq->dev, "Error linux,iinlim-percentage %u > 100\n", val); + return -EINVAL; + } + bq->iinlim_percentage = val; + } else { + bq->iinlim_percentage = 100; + } + bq->skip_reset = device_property_read_bool(bq->dev, "linux,skip-reset"); bq->read_back_init_data = device_property_read_bool(bq->dev, "linux,read-back-settings");