Received: by 10.223.164.197 with SMTP id h5csp340865wrb; Sat, 4 Nov 2017 12:10:02 -0700 (PDT) X-Google-Smtp-Source: ABhQp+QvuJmGcCIGOBOnMoqRSNC0M515+5AIQfrp+D9C0iEP9pOApaDyk0tVXLGy6yXYKhCArcW8 X-Received: by 10.84.129.34 with SMTP id 31mr10128852plb.368.1509822602096; Sat, 04 Nov 2017 12:10:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1509822602; cv=none; d=google.com; s=arc-20160816; b=WH6dyghjHIHxXGUsw/VaGjRkSImvf5s48UIZo+iUVTGBw8dvvn+NQlGtcbIW886iJD 6/JqtA4S2Nhosn71hTxXA3CZYnyONbDQ23aC8ltB/QQ47sot31LUxQrVw2d6OZ624zJ0 JdzFv8W9Zk5DZd3ghWdTL9SeRd8mjY6VeqO4kHX71CgNOxcHP9kI7sEhQntMq5ELe8xW 7U8gflsNJeUFUpjzmLoYFVJtKBdrtdIDb4JC55f+aYI5XmM6T0ap0JQQRsM6O58mktlL KR8ex0vWUT/23p3BHN7oWz9iON5dYeE2TSDi2tEA3azvP1mZxVz0sY+0oNSexMCzl3h0 uwqQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:cc:to:subject :message-id:date:from:references:in-reply-to:mime-version :dkim-signature:arc-authentication-results; bh=IdxKc4n/IUrMbPghrBYSmoiMSGaJjrUXtaGH+QmSsIk=; b=ocydtX9YOKE0XBq3tW1h+5tsKZaiRaURlPA/TxjsypVKhp1eBUa5Cy1Hqus/Tz4JcF VUiMU4CNs/Lgt0ZhDoe0sslpv4eL2vRWUOYS6jAU2ykvVeb3yHZEU3VipFZHt4lb/qgD 6Pro+/Msr354Z/jqqRjJD3mYXG3j0L44vDWNaDBWyn0rXDEEA6mFYtyp9lNb8JDHjuXK RVnHIIM73MCKSPvods4GMec+Nweyg8NAuAw/KFCr2i9x6S8A1lr4WtLmJUJqh02EiEam 2vNl84ypTvVepTjC4wv18iim1dONcNPRyvjeMNmzk6qidqho4KhepNDmFW4+j9QD2B7g bo9A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=p22ARzx3; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id v5si8933951pgt.411.2017.11.04.12.09.47; Sat, 04 Nov 2017 12:10:02 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=p22ARzx3; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751733AbdKDTJK (ORCPT + 94 others); Sat, 4 Nov 2017 15:09:10 -0400 Received: from mail-ot0-f196.google.com ([74.125.82.196]:57154 "EHLO mail-ot0-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750709AbdKDTJJ (ORCPT ); Sat, 4 Nov 2017 15:09:09 -0400 Received: by mail-ot0-f196.google.com with SMTP id i19so5319363ote.13 for ; Sat, 04 Nov 2017 12:09:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=IdxKc4n/IUrMbPghrBYSmoiMSGaJjrUXtaGH+QmSsIk=; b=p22ARzx3UOfbRwzgLUStLen+fD3fo/3ZVCMrcf/kLyASsxmmFuRExlpXbZS8d69V8/ gSiUQYA8L7gRhfaGtkd1+6xPL0crjGQ8yoxPX/lBABYy+4ib7NcDgnnZV4S2lbRlUGSS MzQy+6yqvGbiwHFyrIHdvqQuemXGrLdXIr9CE3WLpErtTtjeVJI0pLKHnqfXQH2t5tvj //MOis5yEgtXLceQp+unstrk0TVHDxP0QyVXMrT8hBVZ3VJFvlf/8uXJU1ydK3qYMGUA cA1OWqdxpPzYUyTzC0yMnLh8O79rIX8FJCjdCxkgOlh2VMu1ID6tttOhRuNI6SNJxv56 s+qA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=IdxKc4n/IUrMbPghrBYSmoiMSGaJjrUXtaGH+QmSsIk=; b=RsK9cZy6gNQq6XIEpS+xsyW1ndxvqBqWvhX49fI0mys8k2ZK/vC8pxcmb8zjmv97l2 9TQY1X/xNb9TAYh8FX2USQpTsra0N5G01fB2ykJmCWDctxxw9A1yXYWk24K1BZdFVrd5 StDFkk+bL6A+Js+G12ayeq3M8D3LVjxoY4nXurN6IX4kkCgSoh28RcTUxaMnfEHg5x9y IEy/Bsfp92vnQYZShdTnf/71V9hu0x7R24BekM3E8ROdwMOxvkGTL+NcjWdeVJbU2tQK 3yZ265wWbooqsJRhKDl/0dsYdfIh7QsebxiM+GNhca8nQd+1chipl1w68DwcJ9cBkbpK GXxQ== X-Gm-Message-State: AJaThX5/ImpmZG+MPyqhp8Vvif6dBg9L9iB2FMOUrIRWUnQJSXwCvKiL jqBZ80iu+xgTa6S4mmxBZrhrad6gEei0qzPMqqt/yQ== X-Received: by 10.157.87.202 with SMTP id q10mr7234710oti.278.1509822547875; Sat, 04 Nov 2017 12:09:07 -0700 (PDT) MIME-Version: 1.0 Received: by 10.74.168.131 with HTTP; Sat, 4 Nov 2017 12:08:27 -0700 (PDT) In-Reply-To: <20171104185953.9532-2-Badhri@google.com> References: <20171104185953.9532-1-Badhri@google.com> <20171104185953.9532-2-Badhri@google.com> From: Badhri Jagan Sridharan Date: Sat, 4 Nov 2017 12:08:27 -0700 Message-ID: Subject: Re: [PATCH 2/2 v4] typec: tcpm: Only request matching pdos To: Heikki Krogerus , Greg Kroah-Hartman , Dan Carpenter , Guenter Roeck Cc: USB , LKML , Badhri Jagan Sridharan Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Please disregard v4. Going to send another version again. On Sat, Nov 4, 2017 at 11:59 AM, Badhri Jagan Sridharan wrote: > At present, TCPM code assumes that local device supports > variable/batt pdos and always selects the pdo with highest > possible power within the board limit. This assumption > might not hold good for all devices. To overcome this, > this patch makes TCPM only accept a source_pdo when there is > a matching sink pdo. > > For Fixed pdos: The voltage should match between the > incoming source_cap and the registered snk_pdo > For Variable/Batt pdos: The incoming source_cap voltage > range should fall within the registered snk_pdo's voltage > range. > > Also, when the cap_mismatch bit is set, the max_power/current > should be set to the max_current/power of the sink_pdo. > This is according to: > > "If the Capability Mismatch bit is set to one > The Maximum Operating Current/Power field may contain a value > larger than the maximum current/power offered in the Source > Capabilities message=E2=80=99s PDO as referenced by the Object position f= ield. > This enables the Sink to indicate that it requires more current/power > than is being offered. If the Sink requires a different voltage this > will be indicated by its Sink Capabilities message. > > Signed-off-by: Badhri Jagan Sridharan > --- > Changelog since v1: > - Rebased the patch on top of drivers/usb/type/tcpm.c > - Added duplicate pdo check for variable/batt pdo. > - Fixed tabs as suggested by dan.carpenter@oracle.com > > Changelog since v2: > - Rebase > > Changelog since v2: > - Refactored code fixed formatting issues as suggested > by heikki.krogerus@linux.intel.com > > drivers/usb/typec/tcpm.c | 146 +++++++++++++++++++++++++++++++++++------= ------ > 1 file changed, 111 insertions(+), 35 deletions(-) > > diff --git a/drivers/usb/typec/tcpm.c b/drivers/usb/typec/tcpm.c > index ecdad71a6159..44a080202a70 100644 > --- a/drivers/usb/typec/tcpm.c > +++ b/drivers/usb/typec/tcpm.c > @@ -261,6 +261,9 @@ struct tcpm_port { > unsigned int nr_src_pdo; > u32 snk_pdo[PDO_MAX_OBJECTS]; > unsigned int nr_snk_pdo; > + unsigned int nr_fixed; /* number of fixed sink PDOs */ > + unsigned int nr_var; /* number of variable sink PDOs */ > + unsigned int nr_batt; /* number of battery sink PDOs */ > u32 snk_vdo[VDO_MAX_OBJECTS]; > unsigned int nr_snk_vdo; > > @@ -1761,39 +1764,86 @@ static int tcpm_pd_check_request(struct tcpm_port= *port) > return 0; > } > > -static int tcpm_pd_select_pdo(struct tcpm_port *port) > +#define min_power(x, y) min(pdo_max_power(x), pdo_max_power(y)) > +#define min_current(x, y) min(pdo_max_current(x), pdo_max_current(y)) > + > +static int tcpm_pd_select_pdo(struct tcpm_port *port, int *sink_pdo) > { > - unsigned int i, max_mw =3D 0, max_mv =3D 0; > + unsigned int i, j, max_mw =3D 0, max_mv =3D 0, mw =3D 0, mv =3D 0= , ma =3D 0; > int ret =3D -EINVAL; > > /* > - * Select the source PDO providing the most power while staying w= ithin > - * the board's voltage limits. Prefer PDO providing exp > + * Select the source PDO providing the most power which has a > + * matchig sink cap. > */ > for (i =3D 0; i < port->nr_source_caps; i++) { > u32 pdo =3D port->source_caps[i]; > enum pd_pdo_type type =3D pdo_type(pdo); > - unsigned int mv, ma, mw; > > - if (type =3D=3D PDO_TYPE_FIXED) > - mv =3D pdo_fixed_voltage(pdo); > - else > - mv =3D pdo_min_voltage(pdo); > - > - if (type =3D=3D PDO_TYPE_BATT) { > - mw =3D pdo_max_power(pdo); > - } else { > - ma =3D min(pdo_max_current(pdo), > - port->max_snk_ma); > - mw =3D ma * mv / 1000; > - } > - > - /* Perfer higher voltages if available */ > - if ((mw > max_mw || (mw =3D=3D max_mw && mv > max_mv)) && > - mv <=3D port->max_snk_mv) { > - ret =3D i; > - max_mw =3D mw; > - max_mv =3D mv; > + if (type =3D=3D PDO_TYPE_FIXED) { > + for (j =3D 0; j < port->nr_fixed; j++) { > + if (pdo_fixed_voltage(pdo) =3D=3D > + pdo_fixed_voltage(port->snk_pdo[j])) = { > + ma =3D min_current(pdo, port->snk= _pdo[j]); > + mv =3D pdo_fixed_voltage(pdo); > + mw =3D ma * mv / 1000; > + if (mw > max_mw || > + (mw =3D=3D max_mw && mv > max= _mv)) { > + ret =3D i; > + *sink_pdo =3D j; > + max_mw =3D mw; > + max_mv =3D mv; > + } > + /* There could only be one fixed = pdo > + * at a specific voltage level. > + * So breaking here. > + */ > + break; > + } > + } > + } else if (type =3D=3D PDO_TYPE_BATT) { > + for (j =3D port->nr_fixed; > + j < port->nr_fixed + > + port->nr_batt; > + j++) { > + if (pdo_min_voltage(pdo) >=3D > + pdo_min_voltage(port->snk_pdo[j]) && > + pdo_max_voltage(pdo) <=3D > + pdo_max_voltage(port->snk_pdo[j])) { > + mw =3D min_power(pdo, port->snk_p= do[j]); > + mv =3D pdo_min_voltage(pdo); > + if (mw > max_mw || > + (mw =3D=3D max_mw && mv > max= _mv)) { > + ret =3D i; > + *sink_pdo =3D j; > + max_mw =3D mw; > + max_mv =3D mv; > + } > + } > + } > + } else if (type =3D=3D PDO_TYPE_VAR) { > + for (j =3D port->nr_fixed + > + port->nr_batt; > + j < port->nr_fixed + > + port->nr_batt + > + port->nr_var; > + j++) { > + if (pdo_min_voltage(pdo) >=3D > + pdo_min_voltage(port->snk_pdo[j]) && > + pdo_max_voltage(pdo) <=3D > + pdo_max_voltage(port->snk_pdo[j])) { > + ma =3D min_current(pdo, port->snk= _pdo[j]); > + mv =3D pdo_min_voltage(pdo); > + mw =3D ma * mv / 1000; > + if (mw > max_mw || > + (mw =3D=3D max_mw && mv > max= _mv)) { > + ret =3D i; > + *sink_pdo =3D j; > + max_mw =3D mw; > + max_mv =3D mv; > + } > + } > + } > } > } > > @@ -1805,13 +1855,14 @@ static int tcpm_pd_build_request(struct tcpm_port= *port, u32 *rdo) > unsigned int mv, ma, mw, flags; > unsigned int max_ma, max_mw; > enum pd_pdo_type type; > - int index; > - u32 pdo; > + int index, snk_pdo_index; > + u32 pdo, matching_snk_pdo; > > - index =3D tcpm_pd_select_pdo(port); > + index =3D tcpm_pd_select_pdo(port, &snk_pdo_index); > if (index < 0) > return -EINVAL; > pdo =3D port->source_caps[index]; > + matching_snk_pdo =3D port->snk_pdo[snk_pdo_index]; > type =3D pdo_type(pdo); > > if (type =3D=3D PDO_TYPE_FIXED) > @@ -1819,26 +1870,29 @@ static int tcpm_pd_build_request(struct tcpm_port= *port, u32 *rdo) > else > mv =3D pdo_min_voltage(pdo); > > - /* Select maximum available current within the board's power limi= t */ > + /* Select maximum available current within the sink pdo's limit *= / > if (type =3D=3D PDO_TYPE_BATT) { > - mw =3D pdo_max_power(pdo); > - ma =3D 1000 * min(mw, port->max_snk_mw) / mv; > + mw =3D min(pdo_max_power(pdo), pdo_max_power(matching_snk= _pdo)); Going to reuse the macro here in the next version. > + ma =3D 1000 * mw / mv; > } else { > ma =3D min(pdo_max_current(pdo), > - 1000 * port->max_snk_mw / mv); > + pdo_max_current(matching_snk_pdo)); > + mw =3D ma * mv / 1000; > } > - ma =3D min(ma, port->max_snk_ma); > > flags =3D RDO_USB_COMM | RDO_NO_SUSPEND; > > /* Set mismatch bit if offered power is less than operating power= */ > - mw =3D ma * mv / 1000; > max_ma =3D ma; > max_mw =3D mw; > if (mw < port->operating_snk_mw) { > flags |=3D RDO_CAP_MISMATCH; > - max_mw =3D port->operating_snk_mw; > - max_ma =3D max_mw * 1000 / mv; > + if (type =3D=3D PDO_TYPE_BATT && > + (pdo_max_power(matching_snk_pdo) > pdo_max_power(pdo)= )) > + max_mw =3D pdo_max_power(matching_snk_pdo); Same here as well. > + else if (pdo_max_current(matching_snk_pdo) > > + pdo_max_current(pdo)) > + max_ma =3D pdo_max_current(matching_snk_pdo); > } > > tcpm_log(port, "cc=3D%d cc1=3D%d cc2=3D%d vbus=3D%d vconn=3D%s po= larity=3D%d", > @@ -3587,6 +3641,19 @@ int tcpm_update_sink_capabilities(struct tcpm_port= *port, const u32 *pdo, > } > EXPORT_SYMBOL_GPL(tcpm_update_sink_capabilities); > > +static int nr_type_pdos(const u32 *pdo, unsigned int nr_pdo, > + enum pd_pdo_type type) > +{ > + int count =3D 0; > + int i; > + > + for (i =3D 0; i < nr_pdo; i++) { > + if (pdo_type(pdo[i]) =3D=3D type) > + count++; > + } > + return count; > +} > + > struct tcpm_port *tcpm_register_port(struct device *dev, struct tcpc_dev= *tcpc) > { > struct tcpm_port *port; > @@ -3632,6 +3699,15 @@ struct tcpm_port *tcpm_register_port(struct device= *dev, struct tcpc_dev *tcpc) > tcpc->config->nr_src_pdo); > port->nr_snk_pdo =3D tcpm_copy_pdos(port->snk_pdo, tcpc->config->= snk_pdo, > tcpc->config->nr_snk_pdo); > + port->nr_fixed =3D nr_type_pdos(port->snk_pdo, > + port->nr_snk_pdo, > + PDO_TYPE_FIXED); > + port->nr_var =3D nr_type_pdos(port->snk_pdo, > + port->nr_snk_pdo, > + PDO_TYPE_VAR); > + port->nr_batt =3D nr_type_pdos(port->snk_pdo, > + port->nr_snk_pdo, > + PDO_TYPE_BATT); > port->nr_snk_vdo =3D tcpm_copy_vdos(port->snk_vdo, tcpc->config->= snk_vdo, > tcpc->config->nr_snk_vdo); > > -- > 2.15.0.403.gc27cc4dac6-goog > From 1583163200779033107@xxx Sat Nov 04 19:01:23 +0000 2017 X-GM-THRID: 1581628275778143409 X-Gmail-Labels: Inbox,Category Forums,HistoricalUnread