From patchwork Tue Jan 30 20:27:57 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aren X-Patchwork-Id: 768769 Received: from a.peacevolution.org (a.peacevolution.org [206.189.193.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9D44571B2C; Tue, 30 Jan 2024 20:37:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=206.189.193.133 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706647067; cv=none; b=KPwGgNcAmFQe5Sv6cqlwoT+6HKftMvx7/MF5K5f48gwuVvu4z5xpprTZpsScjvr8QFslJd1snirSR3X5ilOcDSqnqpBj800J+Kx1hZ6+1LNAFcB8mjRTjNffZ12QpxALpisV99aSO2N/iueOdsbiBzjiZNiQrryon05myHsgdvA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706647067; c=relaxed/simple; bh=s8gDv1BRbZ4lq/K0LWRSztHuSeHvlnMI3UYO7zLzfo8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=daD2+zszvr250mvjYAc8mCM0MNIZZkbcAYKo3WVJyMEN4LDkiVKZeFb3CmD2L7FjL/m+TeEd6z47bPkq9On8HYI3YNH0rFNeZ2ecYrimSo3Arakh2/3wEqA8tKyQbM7LvZb4tWKt/OYRP3g8T2O2Tsym1XyAlT5Hj0jU0HoPlAU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=peacevolution.org; spf=pass smtp.mailfrom=peacevolution.org; dkim=pass (1024-bit key) header.d=peacevolution.org header.i=@peacevolution.org header.b=OLUhxc3O; arc=none smtp.client-ip=206.189.193.133 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=peacevolution.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=peacevolution.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=peacevolution.org header.i=@peacevolution.org header.b="OLUhxc3O" Received: from authenticated-user (PRIMARY_HOSTNAME [PUBLIC_IP]) by a.peacevolution.org (Postfix) with ESMTPA id B03E14661E; Tue, 30 Jan 2024 20:37:43 +0000 (UTC) From: Aren Moynihan To: linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: =?utf-8?q?Ond=C5=99ej_Jirman?= , Hans de Goede , Aidan MacDonald , Aren Moynihan , Chen-Yu Tsai , Sebastian Reichel Subject: [PATCH v2 1/5] power: supply: axp20x_usb_power: replace current_max with input_current_limit Date: Tue, 30 Jan 2024 15:27:57 -0500 Message-ID: <20240130203714.3020464-2-aren@peacevolution.org> In-Reply-To: <20240130203714.3020464-1-aren@peacevolution.org> References: <20240130203714.3020464-1-aren@peacevolution.org> Precedence: bulk X-Mailing-List: linux-pm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Authentication-Results: auth=pass smtp.auth=aren@peacevolution.org smtp.mailfrom=aren@peacevolution.org X-Spam-Level: ** X-Spamd-Bar: ++ DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=peacevolution.org; s=dkim; t=1706647064; h=from:subject:date:message-id:to:cc:mime-version:content-transfer-encoding:in-reply-to:references; bh=NAxGv9qMri1hf0JDQHUznnkmw1bSeEqz6JkBTbreKjQ=; b=OLUhxc3OTR3e4qs7eMABcduOJHq7KXwvnRR8iPxFsdEXZ4NX+B1gzVzDk2Qqm2xp9fdlzO MJUTRvNGYohFGLGU8kTiMzEcsVg5V3imJotjTz++QWNpa2xHRoSrqb2UxC4ns2B1zJ5mBu LieV+Y1RT8VDeszaAEmhY+rH2caESaI= The current_max property is supposed to be read-only, and represent the maximum current the supply can provide. input_current_limit is the limit that is currently set, which is what we have here. When determining what value to write to the register, we need to pick a reasonable value if the requested limit doesn't exactly match one supported by the hardware. If the requested limit is less than the lowest value we can set, round up to the lowest value. Otherwise round down to the nearest value supported by hardware. Also add a dev field to the axp20x_usb_power struct, so we can use dev_dbg and dev_err in more places. Signed-off-by: Aren Moynihan --- Changes in v2: - Values less than the lowest supported limit are rounded up instead of returning -EINVAL when setting the input current limit. drivers/power/supply/axp20x_usb_power.c | 29 +++++++++++++++---------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/drivers/power/supply/axp20x_usb_power.c b/drivers/power/supply/axp20x_usb_power.c index e23308ad4cc7..f7f2ac2b7dae 100644 --- a/drivers/power/supply/axp20x_usb_power.c +++ b/drivers/power/supply/axp20x_usb_power.c @@ -59,6 +59,7 @@ struct axp_data { }; struct axp20x_usb_power { + struct device *dev; struct regmap *regmap; struct regmap_field *curr_lim_fld; struct regmap_field *vbus_valid_bit; @@ -160,7 +161,7 @@ static int axp20x_usb_power_get_property(struct power_supply *psy, val->intval = ret * 1700; /* 1 step = 1.7 mV */ return 0; - case POWER_SUPPLY_PROP_CURRENT_MAX: + case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT: ret = regmap_field_read(power->curr_lim_fld, &v); if (ret) return ret; @@ -256,19 +257,24 @@ static int axp20x_usb_power_set_voltage_min(struct axp20x_usb_power *power, return -EINVAL; } -static int axp20x_usb_power_set_current_max(struct axp20x_usb_power *power, int intval) +static int axp20x_usb_power_set_input_current_limit(struct axp20x_usb_power *power, + int intval) { + unsigned int reg; const unsigned int max = GENMASK(power->axp_data->curr_lim_fld.msb, power->axp_data->curr_lim_fld.lsb); if (intval == -1) return -EINVAL; - for (unsigned int i = 0; i <= max; ++i) - if (power->axp_data->curr_lim_table[i] == intval) - return regmap_field_write(power->curr_lim_fld, i); + for (reg = max - 1; reg > 0; reg--) + if (power->axp_data->curr_lim_table[reg] <= intval) + break; - return -EINVAL; + dev_dbg(power->dev, "setting input current limit reg to %d (%d uA), requested %d uA", + reg, power->axp_data->curr_lim_table[reg], intval); + + return regmap_field_write(power->curr_lim_fld, reg); } static int axp20x_usb_power_set_property(struct power_supply *psy, @@ -287,8 +293,8 @@ static int axp20x_usb_power_set_property(struct power_supply *psy, case POWER_SUPPLY_PROP_VOLTAGE_MIN: return axp20x_usb_power_set_voltage_min(power, val->intval); - case POWER_SUPPLY_PROP_CURRENT_MAX: - return axp20x_usb_power_set_current_max(power, val->intval); + case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT: + return axp20x_usb_power_set_input_current_limit(power, val->intval); default: return -EINVAL; @@ -313,7 +319,7 @@ static int axp20x_usb_power_prop_writeable(struct power_supply *psy, return power->vbus_disable_bit != NULL; return psp == POWER_SUPPLY_PROP_VOLTAGE_MIN || - psp == POWER_SUPPLY_PROP_CURRENT_MAX; + psp == POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT; } static enum power_supply_property axp20x_usb_power_properties[] = { @@ -322,7 +328,7 @@ static enum power_supply_property axp20x_usb_power_properties[] = { POWER_SUPPLY_PROP_ONLINE, POWER_SUPPLY_PROP_VOLTAGE_MIN, POWER_SUPPLY_PROP_VOLTAGE_NOW, - POWER_SUPPLY_PROP_CURRENT_MAX, + POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT, POWER_SUPPLY_PROP_CURRENT_NOW, }; @@ -331,7 +337,7 @@ static enum power_supply_property axp22x_usb_power_properties[] = { POWER_SUPPLY_PROP_PRESENT, POWER_SUPPLY_PROP_ONLINE, POWER_SUPPLY_PROP_VOLTAGE_MIN, - POWER_SUPPLY_PROP_CURRENT_MAX, + POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT, }; static const struct power_supply_desc axp20x_usb_power_desc = { @@ -558,6 +564,7 @@ static int axp20x_usb_power_probe(struct platform_device *pdev) platform_set_drvdata(pdev, power); + power->dev = &pdev->dev; power->axp_data = axp_data; power->regmap = axp20x->regmap; power->num_irqs = axp_data->num_irq_names; From patchwork Tue Jan 30 20:27:58 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aren X-Patchwork-Id: 768328 Received: from a.peacevolution.org (a.peacevolution.org [206.189.193.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 974CC5F555; Tue, 30 Jan 2024 20:37:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=206.189.193.133 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706647075; cv=none; b=XCoWMkl/v4hm3SdymA+GstQweqwlmVP3USp0Ni2LRMWtWo1sPbC7AG/2GPMLRK30WSinMjuUC6c3YTecj9y944axtPpa5wLwycHnhlAXgwthsiF9+gHmQG8DAaOEBkMV55GqZuN+uRuOJHs3LLNBP2XqBamxmBHUPhrd3ag3iZE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706647075; c=relaxed/simple; bh=GIUIG7+EEVMTtkYD8ypmqtjqn29llrcgXCn39fpUmBE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=LCcwests+099a+gZUoptKTeQwDFgrHvaNT3jjzHer1Ttj+e/V9JBlrvoKOCgWMwS8YFGZ/yiqvWmZvqqUO7Jrq5I4Ob6vfr8ClTcaEUnNhnp6cMK6K06oE/K5+a/JzPib2ZjAbTcGuPxECPT/alIw1kU9T9L+Y0tnrGzWGmtlxY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=peacevolution.org; spf=pass smtp.mailfrom=peacevolution.org; dkim=pass (1024-bit key) header.d=peacevolution.org header.i=@peacevolution.org header.b=peh0abvS; arc=none smtp.client-ip=206.189.193.133 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=peacevolution.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=peacevolution.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=peacevolution.org header.i=@peacevolution.org header.b="peh0abvS" Received: from authenticated-user (PRIMARY_HOSTNAME [PUBLIC_IP]) by a.peacevolution.org (Postfix) with ESMTPA id 5F6304661E; Tue, 30 Jan 2024 20:37:52 +0000 (UTC) From: Aren Moynihan To: linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: =?utf-8?q?Ond=C5=99ej_Jirman?= , Hans de Goede , Aidan MacDonald , Aren Moynihan , Chen-Yu Tsai , Sebastian Reichel Subject: [PATCH v2 2/5] power: supply: axp20x_usb_power: use correct register for input current limit Date: Tue, 30 Jan 2024 15:27:58 -0500 Message-ID: <20240130203714.3020464-3-aren@peacevolution.org> In-Reply-To: <20240130203714.3020464-1-aren@peacevolution.org> References: <20240130203714.3020464-1-aren@peacevolution.org> Precedence: bulk X-Mailing-List: linux-pm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Authentication-Results: auth=pass smtp.auth=aren@peacevolution.org smtp.mailfrom=aren@peacevolution.org X-Spam-Level: ** X-Spamd-Bar: ++ DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=peacevolution.org; s=dkim; t=1706647072; h=from:subject:date:message-id:to:cc:mime-version:content-transfer-encoding:in-reply-to:references; bh=ivPm1WNR8FbrvYRzWgVfDM6fYhcsm1TJs7pHZ7Ec81E=; b=peh0abvSNH8vlLhCN6FjDCVIefhI1lavzA6yZ4ePRVf9uszqJQl7pvbQas7VjPk5ltE059 H8xonDx9V+IngLtipZw7Wa0I/Sg/CThpmn/REyWvz4ZO1WjXMNaR9Vh2jYfcT2wg4ky8rk kkW4T9Lxmb5VrFDjvmmhqu1SPF3Tsis= On the axp803 and axp813 chips register 0x30 bits 0-1 is the default current limit that gets applied after the pmic detects a CDP or DCP port. The correct field to set is 0x35 bits 4-7. This field only has nine values (out of the 16 possible if it used all the bits), so introduce a field size variable to take that into account. Signed-off-by: Aren Moynihan --- Changes in v2: - Inline get input current logic. It's not that complicated and this helps to illustrate what changed more clearly. - Split into separate commit, it was part of adding the input current limit before. drivers/power/supply/axp20x_usb_power.c | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/drivers/power/supply/axp20x_usb_power.c b/drivers/power/supply/axp20x_usb_power.c index f7f2ac2b7dae..923121b23d5f 100644 --- a/drivers/power/supply/axp20x_usb_power.c +++ b/drivers/power/supply/axp20x_usb_power.c @@ -50,6 +50,7 @@ struct axp_data { const char * const *irq_names; unsigned int num_irq_names; const int *curr_lim_table; + int curr_lim_table_size; struct reg_field curr_lim_fld; struct reg_field vbus_valid_bit; struct reg_field vbus_mon_bit; @@ -166,7 +167,11 @@ static int axp20x_usb_power_get_property(struct power_supply *psy, if (ret) return ret; - val->intval = power->axp_data->curr_lim_table[v]; + if (v < power->axp_data->curr_lim_table_size) + val->intval = power->axp_data->curr_lim_table[v]; + else + val->intval = power->axp_data->curr_lim_table[ + power->axp_data->curr_lim_table_size - 1]; return 0; case POWER_SUPPLY_PROP_CURRENT_NOW: if (IS_ENABLED(CONFIG_AXP20X_ADC)) { @@ -261,8 +266,7 @@ static int axp20x_usb_power_set_input_current_limit(struct axp20x_usb_power *pow int intval) { unsigned int reg; - const unsigned int max = GENMASK(power->axp_data->curr_lim_fld.msb, - power->axp_data->curr_lim_fld.lsb); + const unsigned int max = power->axp_data->curr_lim_table_size; if (intval == -1) return -EINVAL; @@ -394,10 +398,15 @@ static int axp221_usb_curr_lim_table[] = { }; static int axp813_usb_curr_lim_table[] = { + 100000, + 500000, 900000, 1500000, 2000000, 2500000, + 3000000, + 3500000, + 4000000, }; static const struct axp_data axp192_data = { @@ -405,6 +414,7 @@ static const struct axp_data axp192_data = { .irq_names = axp20x_irq_names, .num_irq_names = ARRAY_SIZE(axp20x_irq_names), .curr_lim_table = axp192_usb_curr_lim_table, + .curr_lim_table_size = ARRAY_SIZE(axp192_usb_curr_lim_table), .curr_lim_fld = REG_FIELD(AXP20X_VBUS_IPSOUT_MGMT, 0, 1), .vbus_valid_bit = REG_FIELD(AXP192_USB_OTG_STATUS, 2, 2), .vbus_mon_bit = REG_FIELD(AXP20X_VBUS_MON, 3, 3), @@ -415,6 +425,7 @@ static const struct axp_data axp202_data = { .irq_names = axp20x_irq_names, .num_irq_names = ARRAY_SIZE(axp20x_irq_names), .curr_lim_table = axp20x_usb_curr_lim_table, + .curr_lim_table_size = ARRAY_SIZE(axp20x_usb_curr_lim_table), .curr_lim_fld = REG_FIELD(AXP20X_VBUS_IPSOUT_MGMT, 0, 1), .vbus_valid_bit = REG_FIELD(AXP20X_USB_OTG_STATUS, 2, 2), .vbus_mon_bit = REG_FIELD(AXP20X_VBUS_MON, 3, 3), @@ -425,6 +436,7 @@ static const struct axp_data axp221_data = { .irq_names = axp22x_irq_names, .num_irq_names = ARRAY_SIZE(axp22x_irq_names), .curr_lim_table = axp221_usb_curr_lim_table, + .curr_lim_table_size = ARRAY_SIZE(axp221_usb_curr_lim_table), .curr_lim_fld = REG_FIELD(AXP20X_VBUS_IPSOUT_MGMT, 0, 1), .vbus_needs_polling = true, }; @@ -434,6 +446,7 @@ static const struct axp_data axp223_data = { .irq_names = axp22x_irq_names, .num_irq_names = ARRAY_SIZE(axp22x_irq_names), .curr_lim_table = axp20x_usb_curr_lim_table, + .curr_lim_table_size = ARRAY_SIZE(axp20x_usb_curr_lim_table), .curr_lim_fld = REG_FIELD(AXP20X_VBUS_IPSOUT_MGMT, 0, 1), .vbus_needs_polling = true, }; @@ -443,7 +456,8 @@ static const struct axp_data axp813_data = { .irq_names = axp22x_irq_names, .num_irq_names = ARRAY_SIZE(axp22x_irq_names), .curr_lim_table = axp813_usb_curr_lim_table, - .curr_lim_fld = REG_FIELD(AXP20X_VBUS_IPSOUT_MGMT, 0, 1), + .curr_lim_table_size = ARRAY_SIZE(axp813_usb_curr_lim_table), + .curr_lim_fld = REG_FIELD(AXP22X_CHRG_CTRL3, 4, 7), .usb_bc_en_bit = REG_FIELD(AXP288_BC_GLOBAL, 0, 0), .vbus_disable_bit = REG_FIELD(AXP20X_VBUS_IPSOUT_MGMT, 7, 7), .vbus_needs_polling = true, From patchwork Tue Jan 30 20:27:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aren X-Patchwork-Id: 768768 Received: from a.peacevolution.org (a.peacevolution.org [206.189.193.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B4285762E3; Tue, 30 Jan 2024 20:37:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=206.189.193.133 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706647079; cv=none; b=n1khRLRVbXefZss6W3M2X+9hVg7im29sO8OxHw8MGCZD/JmT3upED5G/Xr5G95oC2dOAnbgU2LZ0+zOESbuiM3Yt82H40cxhHuMbqD4i6e6Rb4zmqzyGwZL9AZ4rz+HSK7TtzpvML8mGQMw2pomkBwqtyLPNJZMzjejql58LvQs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706647079; c=relaxed/simple; bh=5wkPN+b1GbrWti66iOp/WvMt1+CJTyRFqQ9LVlL8Vbg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=mL4UoaMK/V/TOEhNuLQsHOES8E70IFGSugX71ObtZ7gI+mXroIjCdt6Eu3X8ExEgikDWjYibZ2oYmeNn0JxNFkU6+YfayDvSTWm+dxpSBAYJQrc2+YrJSMlI1Yjsa7fhZIamYa/7My8sFZMc3vScxjpQvK1NaizjsEYjm4ulVws= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=peacevolution.org; spf=pass smtp.mailfrom=peacevolution.org; dkim=pass (1024-bit key) header.d=peacevolution.org header.i=@peacevolution.org header.b=U+/RvjVy; arc=none smtp.client-ip=206.189.193.133 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=peacevolution.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=peacevolution.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=peacevolution.org header.i=@peacevolution.org header.b="U+/RvjVy" Received: from authenticated-user (PRIMARY_HOSTNAME [PUBLIC_IP]) by a.peacevolution.org (Postfix) with ESMTPA id 34C324661E; Tue, 30 Jan 2024 20:37:56 +0000 (UTC) From: Aren Moynihan To: linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: =?utf-8?q?Ond=C5=99ej_Jirman?= , Hans de Goede , Aidan MacDonald , Aren Moynihan , Chen-Yu Tsai , Sebastian Reichel Subject: [PATCH v2 3/5] power: supply: axp20x_usb_power: fix race condition with usb bc Date: Tue, 30 Jan 2024 15:27:59 -0500 Message-ID: <20240130203714.3020464-4-aren@peacevolution.org> In-Reply-To: <20240130203714.3020464-1-aren@peacevolution.org> References: <20240130203714.3020464-1-aren@peacevolution.org> Precedence: bulk X-Mailing-List: linux-pm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Authentication-Results: auth=pass smtp.auth=aren@peacevolution.org smtp.mailfrom=aren@peacevolution.org X-Spam-Level: ** X-Spamd-Bar: ++ DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=peacevolution.org; s=dkim; t=1706647076; h=from:subject:date:message-id:to:cc:mime-version:content-transfer-encoding:in-reply-to:references; bh=Qh62TYzJLJF3IVftKmbgHbTDldqXGopKOhOQHQ3kQso=; b=U+/RvjVyLg9rgo6Oo3xomEgxwmZY4yPcXiiG5NSEHVPEGGvkIh7KC08MSXFRMuYPUBjLfm pAbKcOHABZ0sew05lCJqlptrUvklUCjBX/HtAA1DkoLGoEzcJUlH1IZggCEbQh//6TIo06 gB9uo4oy3n2R9pYmuiNPvWBQ1SL2eGA= When input_current_limit is set while USB BC is in progress, the BC module will overwrite the value that was set when it finishes detection. Signed-off-by: Aren Moynihan --- Changes in v2: - Split into sepereate commit, it was part of addig the input current limit before. drivers/power/supply/axp20x_usb_power.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/drivers/power/supply/axp20x_usb_power.c b/drivers/power/supply/axp20x_usb_power.c index 923121b23d5f..ac5a3f126df6 100644 --- a/drivers/power/supply/axp20x_usb_power.c +++ b/drivers/power/supply/axp20x_usb_power.c @@ -117,6 +117,15 @@ static void axp20x_usb_power_poll_vbus(struct work_struct *work) if (val != power->old_status) power_supply_changed(power->supply); + if (power->usb_bc_en_bit && (val & AXP20X_PWR_STATUS_VBUS_PRESENT) != + (power->old_status & AXP20X_PWR_STATUS_VBUS_PRESENT)) { + dev_dbg(power->dev, "Cable status changed, re-enabling USB BC"); + ret = regmap_field_write(power->usb_bc_en_bit, 1); + if (ret) + dev_err(power->dev, "failed to enable USB BC: errno %d", + ret); + } + power->old_status = val; power->online = val & AXP20X_PWR_STATUS_VBUS_USED; @@ -265,12 +274,26 @@ static int axp20x_usb_power_set_voltage_min(struct axp20x_usb_power *power, static int axp20x_usb_power_set_input_current_limit(struct axp20x_usb_power *power, int intval) { + int ret; unsigned int reg; const unsigned int max = power->axp_data->curr_lim_table_size; if (intval == -1) return -EINVAL; + /* + * BC1.2 detection can cause a race condition if we try to set a current + * limit while it's in progress. When it finishes it will overwrite the + * current limit we just set. + */ + if (power->usb_bc_en_bit) { + dev_dbg(power->dev, + "disabling BC1.2 detection because current limit was set"); + ret = regmap_field_write(power->usb_bc_en_bit, 0); + if (ret) + return ret; + } + for (reg = max - 1; reg > 0; reg--) if (power->axp_data->curr_lim_table[reg] <= intval) break; From patchwork Tue Jan 30 20:28:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aren X-Patchwork-Id: 768327 Received: from a.peacevolution.org (a.peacevolution.org [206.189.193.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BEA02762EE; Tue, 30 Jan 2024 20:38:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=206.189.193.133 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706647082; cv=none; b=Q0NP8BhjHThBBxFGE0scZv6Qkf1JKb0rn2XOSL9jCXgKb8rQ0tOEwyJpTrU8lHbwcFMLwxLf+h1mo+0QLiGuj07VeRs63RWtZCG2e/CncHW+xFDeFTr9x0abC7zgnh1w5pWLY9gty7BzeOFiABwA5dk/nYU56cdD5V4Mr72/paM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706647082; c=relaxed/simple; bh=zXXGsJFbgiYzogwaULyPk2kphg0Ch0Uphvq2gjdcWj8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=hAmqZXpsxL3Ei1wKw9b0staiwk6HVozx6Sm6XHA07Z7DeYgjC7KQ+T/rzXiYxI4uhkWQDAQgNwUrOgNYA+1xL3OHC+8/JEWaWxUsriOzn0pjk3+JfQeGCqyuKISa7u1tK+AMmP4BF/d5yXKnOnRGKD7KTOiFtNhqudlp5SlPecs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=peacevolution.org; spf=pass smtp.mailfrom=peacevolution.org; dkim=pass (1024-bit key) header.d=peacevolution.org header.i=@peacevolution.org header.b=RdZPi4ul; arc=none smtp.client-ip=206.189.193.133 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=peacevolution.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=peacevolution.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=peacevolution.org header.i=@peacevolution.org header.b="RdZPi4ul" Received: from authenticated-user (PRIMARY_HOSTNAME [PUBLIC_IP]) by a.peacevolution.org (Postfix) with ESMTPA id 2EA8046B74; Tue, 30 Jan 2024 20:37:59 +0000 (UTC) From: Aren Moynihan To: linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: =?utf-8?q?Ond=C5=99ej_Jirman?= , Hans de Goede , Aidan MacDonald , Aren Moynihan , Chen-Yu Tsai , Sebastian Reichel Subject: [PATCH v2 4/5] power: supply: axp20x_usb_power: enable usb_type reporting Date: Tue, 30 Jan 2024 15:28:00 -0500 Message-ID: <20240130203714.3020464-5-aren@peacevolution.org> In-Reply-To: <20240130203714.3020464-1-aren@peacevolution.org> References: <20240130203714.3020464-1-aren@peacevolution.org> Precedence: bulk X-Mailing-List: linux-pm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Authentication-Results: auth=pass smtp.auth=aren@peacevolution.org smtp.mailfrom=aren@peacevolution.org X-Spam-Level: *** X-Spamd-Bar: +++ DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=peacevolution.org; s=dkim; t=1706647079; h=from:subject:date:message-id:to:cc:mime-version:content-transfer-encoding:in-reply-to:references; bh=6G0YmOD50/3sb0eb05V/Ra/p+656o7Ad1EWSfIxoCZI=; b=RdZPi4ul39i/V1pJazsVpkPeFOvR3+beShpiRU91OnRSd+nbRXbU2OYXRw/0MrUjD5TZ9+ Qk6gvexMDXB+HIAp73g+/9Zsll64rkCaDMYD60kWRN8MkMorrj9J7sJj1ktlveT36DRvZ+ VbgZPODoGJrA9lrmOvGmfjBzPvT6Hos= The axp803 and axp813 chips can report the detected USB BC mode. SDP, CDP, and DCP are supported. Signed-off-by: Aren Moynihan --- (no changes since v1) drivers/power/supply/axp20x_usb_power.c | 73 ++++++++++++++++++++++++- 1 file changed, 72 insertions(+), 1 deletion(-) diff --git a/drivers/power/supply/axp20x_usb_power.c b/drivers/power/supply/axp20x_usb_power.c index ac5a3f126df6..dae7e5cfc54e 100644 --- a/drivers/power/supply/axp20x_usb_power.c +++ b/drivers/power/supply/axp20x_usb_power.c @@ -55,6 +55,7 @@ struct axp_data { struct reg_field vbus_valid_bit; struct reg_field vbus_mon_bit; struct reg_field usb_bc_en_bit; + struct reg_field usb_bc_det_fld; struct reg_field vbus_disable_bit; bool vbus_needs_polling: 1; }; @@ -66,6 +67,7 @@ struct axp20x_usb_power { struct regmap_field *vbus_valid_bit; struct regmap_field *vbus_mon_bit; struct regmap_field *usb_bc_en_bit; + struct regmap_field *usb_bc_det_fld; struct regmap_field *vbus_disable_bit; struct power_supply *supply; const struct axp_data *axp_data; @@ -134,6 +136,37 @@ static void axp20x_usb_power_poll_vbus(struct work_struct *work) mod_delayed_work(system_power_efficient_wq, &power->vbus_detect, DEBOUNCE_TIME); } +static int axp20x_get_usb_type(struct axp20x_usb_power *power, + union power_supply_propval *val) +{ + unsigned int reg; + int ret; + + if (!power->usb_bc_det_fld) + return -EINVAL; + + ret = regmap_field_read(power->usb_bc_det_fld, ®); + if (ret) + return ret; + + switch (reg) { + case 1: + val->intval = POWER_SUPPLY_USB_TYPE_SDP; + break; + case 2: + val->intval = POWER_SUPPLY_USB_TYPE_CDP; + break; + case 3: + val->intval = POWER_SUPPLY_USB_TYPE_DCP; + break; + default: + val->intval = POWER_SUPPLY_USB_TYPE_UNKNOWN; + break; + } + + return 0; +} + static int axp20x_usb_power_get_property(struct power_supply *psy, enum power_supply_property psp, union power_supply_propval *val) { @@ -204,6 +237,9 @@ static int axp20x_usb_power_get_property(struct power_supply *psy, val->intval = ret * 375; /* 1 step = 0.375 mA */ return 0; + + case POWER_SUPPLY_PROP_USB_TYPE: + return axp20x_get_usb_type(power, val); default: break; } @@ -367,6 +403,22 @@ static enum power_supply_property axp22x_usb_power_properties[] = { POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT, }; +static enum power_supply_property axp813_usb_power_properties[] = { + POWER_SUPPLY_PROP_HEALTH, + POWER_SUPPLY_PROP_PRESENT, + POWER_SUPPLY_PROP_ONLINE, + POWER_SUPPLY_PROP_VOLTAGE_MIN, + POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT, + POWER_SUPPLY_PROP_USB_TYPE, +}; + +static enum power_supply_usb_type axp813_usb_types[] = { + POWER_SUPPLY_USB_TYPE_SDP, + POWER_SUPPLY_USB_TYPE_DCP, + POWER_SUPPLY_USB_TYPE_CDP, + POWER_SUPPLY_USB_TYPE_UNKNOWN, +}; + static const struct power_supply_desc axp20x_usb_power_desc = { .name = "axp20x-usb", .type = POWER_SUPPLY_TYPE_USB, @@ -387,6 +439,18 @@ static const struct power_supply_desc axp22x_usb_power_desc = { .set_property = axp20x_usb_power_set_property, }; +static const struct power_supply_desc axp813_usb_power_desc = { + .name = "axp20x-usb", + .type = POWER_SUPPLY_TYPE_USB, + .properties = axp813_usb_power_properties, + .num_properties = ARRAY_SIZE(axp813_usb_power_properties), + .property_is_writeable = axp20x_usb_power_prop_writeable, + .get_property = axp20x_usb_power_get_property, + .set_property = axp20x_usb_power_set_property, + .usb_types = axp813_usb_types, + .num_usb_types = ARRAY_SIZE(axp813_usb_types), +}; + static const char * const axp20x_irq_names[] = { "VBUS_PLUGIN", "VBUS_REMOVAL", @@ -475,13 +539,14 @@ static const struct axp_data axp223_data = { }; static const struct axp_data axp813_data = { - .power_desc = &axp22x_usb_power_desc, + .power_desc = &axp813_usb_power_desc, .irq_names = axp22x_irq_names, .num_irq_names = ARRAY_SIZE(axp22x_irq_names), .curr_lim_table = axp813_usb_curr_lim_table, .curr_lim_table_size = ARRAY_SIZE(axp813_usb_curr_lim_table), .curr_lim_fld = REG_FIELD(AXP22X_CHRG_CTRL3, 4, 7), .usb_bc_en_bit = REG_FIELD(AXP288_BC_GLOBAL, 0, 0), + .usb_bc_det_fld = REG_FIELD(AXP288_BC_DET_STAT, 5, 7), .vbus_disable_bit = REG_FIELD(AXP20X_VBUS_IPSOUT_MGMT, 7, 7), .vbus_needs_polling = true, }; @@ -629,6 +694,12 @@ static int axp20x_usb_power_probe(struct platform_device *pdev) if (ret) return ret; + ret = axp20x_regmap_field_alloc_optional(&pdev->dev, power->regmap, + axp_data->usb_bc_det_fld, + &power->usb_bc_det_fld); + if (ret) + return ret; + ret = axp20x_regmap_field_alloc_optional(&pdev->dev, power->regmap, axp_data->vbus_disable_bit, &power->vbus_disable_bit); From patchwork Tue Jan 30 20:28:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aren X-Patchwork-Id: 768767 Received: from a.peacevolution.org (a.peacevolution.org [206.189.193.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8F6AB78B7B; Tue, 30 Jan 2024 20:38:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=206.189.193.133 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706647087; cv=none; b=Tvbyb7xDcMU+d9gtm190sWumB/wGJ6JHwTV851UDbs02EHAa/Zg2bHFfoY8FomvydBnL4qeX+/sTQ9bycLKLlp5xk5M366IpDXbG841Q1i7TSUJ7w2eo7COUplMMCqwX+37nmXdrRV+RHoZIsvV5j1dVYbEiflbeyXGnmp+k4+w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706647087; c=relaxed/simple; bh=ItXbRSSuRULGHehgAkFOPt/p0MSJUvWQ3KbcLpvFS5g=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=F7pkK2zK2fS251z62x+ZlkNlbQpr/vdbrqmktPSRtedBZs1aFKgdnGVOsHGXt15m3nm0TCYTusrq/d+YEFFP+mmg4J0TrTMutW3IG//EWjvOlyMR+uMDHWwPIhBlOj0JUrfccdJFINIPv4QpzBP9+5LpqUWuDa5DyuBhhtoh4PM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=peacevolution.org; spf=pass smtp.mailfrom=peacevolution.org; dkim=pass (1024-bit key) header.d=peacevolution.org header.i=@peacevolution.org header.b=M/8nUGKs; arc=none smtp.client-ip=206.189.193.133 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=peacevolution.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=peacevolution.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=peacevolution.org header.i=@peacevolution.org header.b="M/8nUGKs" Received: from authenticated-user (PRIMARY_HOSTNAME [PUBLIC_IP]) by a.peacevolution.org (Postfix) with ESMTPA id D42EA4661E; Tue, 30 Jan 2024 20:38:03 +0000 (UTC) From: Aren Moynihan To: linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: =?utf-8?q?Ond=C5=99ej_Jirman?= , Hans de Goede , Aidan MacDonald , Aren Moynihan , Chen-Yu Tsai , Quentin Schulz , Sebastian Reichel Subject: [PATCH v2 5/5] power: supply: axp20x_usb_power: set input current limit in probe Date: Tue, 30 Jan 2024 15:28:01 -0500 Message-ID: <20240130203714.3020464-6-aren@peacevolution.org> In-Reply-To: <20240130203714.3020464-1-aren@peacevolution.org> References: <20240130203714.3020464-1-aren@peacevolution.org> Precedence: bulk X-Mailing-List: linux-pm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Spamd-Bar: - Authentication-Results: auth=pass smtp.auth=aren@peacevolution.org smtp.mailfrom=aren@peacevolution.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=peacevolution.org; s=dkim; t=1706647084; h=from:subject:date:message-id:to:cc:mime-version:content-type:content-transfer-encoding:in-reply-to:references; bh=eZlM22aJvF3MR5EwxjV3X6ckVSfAYjgJvEHp2kxoO6k=; b=M/8nUGKsclChSRKnGxLzS4IoO1E9Nt8BRog8y+x/fzhn6ORnN4xP84cIDZ4K93ex/n0XjY sD0Rk0yEGcDFyekb7xdkqxCLD6MFP4pqDTHf/vJ+e919wrUuYmGdFDYTggTOVkjv4d5JsR fizhJ0M8fVYbAy4RwiCrGadYlLng8SQ= axp803 sets the current limit to 3A by default if it doesn't detect a battery. The datasheet says that register 0x2D bit 6 is used to indicate first power on status. According to it, if that bit is 0 and the battery is not detected, it will set the input current limit to 3A, however setting that bit to 1 doesn't to prevent the pmic from setting the current limit to 3A. Waiting for USB BC detection doesn't work either, because (as far as I can tell) USB BC detection isn't performed when there isn't a battery detected. Fixes: c279adafe6ab ("power: supply: axp20x_usb_power: add support for AXP813") Signed-off-by: Aren Moynihan --- I'm not sure if the pmic simply ignores the first power on field, or if it needs to be set in a specific way / at a specific time. I tried setting it in arm-trusted-firmware, and the pmic still set the input current limit to 3A. The datasheet for axp813 says it has the same first power on bit, but I don't have hardware to test if it behaves the same way. This driver uses the same platform data for axp803 and axp813. (no changes since v1) drivers/power/supply/axp20x_usb_power.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/drivers/power/supply/axp20x_usb_power.c b/drivers/power/supply/axp20x_usb_power.c index dae7e5cfc54e..751b9f02d36f 100644 --- a/drivers/power/supply/axp20x_usb_power.c +++ b/drivers/power/supply/axp20x_usb_power.c @@ -51,6 +51,7 @@ struct axp_data { unsigned int num_irq_names; const int *curr_lim_table; int curr_lim_table_size; + int force_curr_lim; struct reg_field curr_lim_fld; struct reg_field vbus_valid_bit; struct reg_field vbus_mon_bit; @@ -545,6 +546,7 @@ static const struct axp_data axp813_data = { .curr_lim_table = axp813_usb_curr_lim_table, .curr_lim_table_size = ARRAY_SIZE(axp813_usb_curr_lim_table), .curr_lim_fld = REG_FIELD(AXP22X_CHRG_CTRL3, 4, 7), + .force_curr_lim = 500000, .usb_bc_en_bit = REG_FIELD(AXP288_BC_GLOBAL, 0, 0), .usb_bc_det_fld = REG_FIELD(AXP288_BC_DET_STAT, 5, 7), .vbus_disable_bit = REG_FIELD(AXP20X_VBUS_IPSOUT_MGMT, 7, 7), @@ -726,6 +728,17 @@ static int axp20x_usb_power_probe(struct platform_device *pdev) return ret; } + if (power->axp_data->force_curr_lim) { + /* + * Some chips set the input current limit to 3A when there is no + * battery connected. Normally the default is 500mA. + */ + ret = axp20x_usb_power_set_input_current_limit(power, + power->axp_data->force_curr_lim); + if (ret) + return ret; + } + if (power->usb_bc_en_bit) { /* Enable USB Battery Charging specification detection */ ret = regmap_field_write(power->usb_bc_en_bit, 1);