Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp2185270imu; Sun, 16 Dec 2018 19:16:59 -0800 (PST) X-Google-Smtp-Source: AFSGD/ULiAsYmzuYz9MemOQE6FAdKIhzaQcV8nUvKymMcOEiUUrTWAvYU6gv834HQ6yJC7W1osdN X-Received: by 2002:a62:8949:: with SMTP id v70mr11123308pfd.85.1545016619240; Sun, 16 Dec 2018 19:16:59 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1545016619; cv=none; d=google.com; s=arc-20160816; b=zPBRifbXzCtmI7Xqf+vpRC0QANmZqbFQKivyM7OclK9CwyqsMr6kh//1378CK8kBV6 0fkfEh3irTZbEAaPQcRGiMzfn5cRxfD5k7lOY9CG4XA57Ep6wSJIYUwRDT8wWHl6oX61 1U27Iodo+A24v2l1wCN1x1/AgM2LaWGX82e7+W9mN/Abq1MC00CZ44lzHx+TAVH7j47T fs9dK144/wds9bMRDAuDuvE/yNDrJunMn79cSnL62nDPHYQHoQNYuL9MOGHW1IwcQLwV 8sro/2B2RvUOx9kHCEaCVN6T2cQly5ZSVKFoPhRlXujIOIDZVyqWidMheJijcupRmIdR 3Kww== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:from:subject:mime-version :message-id:date:dkim-signature; bh=KMSD3HZeH7+p+uIpf0LJMvP3U8jMEzBCD/6rZaSkLsc=; b=x3ikPLiBCS7/93ApgjaUvy8SNN0gFMleCTOptfKIfwvhpCDphhkz7kmci/Sq3bB6cP 9CaqT/q9btl85YpKBkgf0xRxAM8BSfkT7UgF5J0P8+Ba9k4t8NFNiXzjqDOSEjX9SiV/ eY0SOOD1FrpIa5U6aNs//OFpNoEEuR5gJzletoRr1dOPQ2MYFLRq9pcTapv6lZqeFbQa m0ucPWZzB+ebn0mCBts+Nf7Y8JEIyDX6q822+AU+ZwDjIyIK97zcNOIbEKv4wlPU1Aao j4W/QnzkIu6NDT5ysL/a2weduWFjZ400/V/pdsb8/+AkBnexzS8PNo2qlZ6sVSdBO+1D dX6w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=ACECHN3E; 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 o27si10188905pgl.53.2018.12.16.19.16.43; Sun, 16 Dec 2018 19:16:59 -0800 (PST) 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=ACECHN3E; 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 S1726384AbeLQCsP (ORCPT + 99 others); Sun, 16 Dec 2018 21:48:15 -0500 Received: from mail-vs1-f74.google.com ([209.85.217.74]:53568 "EHLO mail-vs1-f74.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726267AbeLQCsN (ORCPT ); Sun, 16 Dec 2018 21:48:13 -0500 Received: by mail-vs1-f74.google.com with SMTP id b203so6935203vsd.20 for ; Sun, 16 Dec 2018 18:48:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:message-id:mime-version:subject:from:to:cc; bh=KMSD3HZeH7+p+uIpf0LJMvP3U8jMEzBCD/6rZaSkLsc=; b=ACECHN3EpIjCSGZ9Itbm1Z9Q4Ms9qp56xEkrn13GjALavz+GyUkF5ay14GUP+9LwRR wFxRYCoik3buPZ+aas9oo8FmAhlt6TIHvVdzrsZJ3QN8SHQPkD3J5uuPtl6PHN5oNIk8 IYX5pUiG/UGhQbqMSAzxGRYF+qwW7nJP5eyanC6aWRH4kC2qMuZ1NyiZkMfhDHBxeafT 84zJZrG/aQ44ptjeCyIF+7Too/LNb0t3XuS/8DXcbncuZ5T46vyOHE0AtfFzojmgthDN lujGG80+1EXFYt8+jsv52eMtAcfYGruzg3hGW7Tk7FP2KGVbbgz8AB4+MZ2psesRAu0W qK8A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:message-id:mime-version:subject:from:to:cc; bh=KMSD3HZeH7+p+uIpf0LJMvP3U8jMEzBCD/6rZaSkLsc=; b=tWz2Ve7GrjcM2oAgRk1JxuKFC+S/8KjeLytsel8XdIW/zlKG7+OlHe5X9OSkZW/Q+8 ps5nQ4an21YA38OAVARudXuko9pJL6hTL5Fi8pNOM2Jxvl5ccpR0DjW3YWJPaJYqHzF+ ml+cnM2mtJAxAh/0RRWHw6TEKmb5d8qfOCK4KPTpF+K5cpv0gBxIjy8gGmRmb7r5xoAj Ry48r8FOa9SLiziFIl5d2DYVC2jSn+U2LynTrvO9OkNJGJbHJp+bzMa437+oQ+juZo2o SfJNgvlUT8MJCQV94/fjK77/dpysYlyLrb+LZSw46LkmKVpsqzCVvwtVGVpYMCXOs4V8 GT+Q== X-Gm-Message-State: AA+aEWYXXCt9uC/1yJkVE037eql9CLEZnypQKQjJ2NY/jB6i9BWcVI8/ EatSw5yoVl46TOzMxj/t6qFsGyE0XrNx X-Received: by 2002:ab0:61d2:: with SMTP id m18mr12132095uan.10.1545014892207; Sun, 16 Dec 2018 18:48:12 -0800 (PST) Date: Mon, 17 Dec 2018 10:48:05 +0800 Message-Id: <20181217024805.122897-1-kyletso@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.20.0.405.gbc1bbc6f85-goog Subject: [PATCH] usb: typec: tcpm: Extend the matching rules on PPS APDO selection From: Kyle Tso To: linux@roeck-us.net, heikki.krogerus@linux.intel.com, gregkh@linuxfoundation.org, Adam.Thomson.Opensource@diasemi.com Cc: badhri@google.com, linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org, Kyle Tso Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Current matching rules ensure that the voltage range of selected Source Capability is entirely within the range defined in one of the Sink Capabilities. This is reasonable but not practical because Sink may not support wide range of voltage when sinking power while Source could advertise its capabilities in raletively wider range. For example, a Source PDO advertising 3.3V-11V@3A (9V Prog of Fixed Nominal Voltage) will not be selected if the Sink requires 5V-12V@3A PPS power. However, the Sink could work well if the requested voltage range in RDOs is 5V-11V@3A. Currently accepted: |--------- source -----| |----------- sink ---------------| Currently not accepted: |--------- source -----| |----------- sink ---------------| |--------- source -----| |----------- sink ---------------| |--------- source -----------------| |------ sink -------| To improve the usability, change the matching rules to what listed below: a. The Source PDO is selectable if any portion of the voltage range overlaps one of the Sink PDO's voltage range. b. The maximum operational voltage will be the lower one between the selected Source PDO and the matching Sink PDO. c. The maximum power will be the maximum operational voltage times the maximum current defined in the selected Source PDO d. Select the Source PDO with the highest maximum power Signed-off-by: Kyle Tso --- Changelog since v1: - updated the commit message as suggested by Guenter Roeck --- drivers/usb/typec/tcpm/tcpm.c | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/drivers/usb/typec/tcpm/tcpm.c b/drivers/usb/typec/tcpm/tcpm.c index 3620efee2688..3001df7bd602 100644 --- a/drivers/usb/typec/tcpm/tcpm.c +++ b/drivers/usb/typec/tcpm/tcpm.c @@ -2213,7 +2213,8 @@ static unsigned int tcpm_pd_select_pps_apdo(struct tcpm_port *port) unsigned int i, j, max_mw = 0, max_mv = 0; unsigned int min_src_mv, max_src_mv, src_ma, src_mw; unsigned int min_snk_mv, max_snk_mv; - u32 pdo; + unsigned int max_op_mv; + u32 pdo, src, snk; unsigned int src_pdo = 0, snk_pdo = 0; /* @@ -2263,16 +2264,18 @@ static unsigned int tcpm_pd_select_pps_apdo(struct tcpm_port *port) continue; } - if (max_src_mv <= max_snk_mv && - min_src_mv >= min_snk_mv) { + if (min_src_mv <= max_snk_mv && + max_src_mv >= min_snk_mv) { + max_op_mv = min(max_src_mv, max_snk_mv); + src_mw = (max_op_mv * src_ma) / 1000; /* Prefer higher voltages if available */ if ((src_mw == max_mw && - min_src_mv > max_mv) || + max_op_mv > max_mv) || src_mw > max_mw) { src_pdo = i; snk_pdo = j; max_mw = src_mw; - max_mv = max_src_mv; + max_mv = max_op_mv; } } } @@ -2285,14 +2288,16 @@ static unsigned int tcpm_pd_select_pps_apdo(struct tcpm_port *port) } if (src_pdo) { - pdo = port->source_caps[src_pdo]; - - port->pps_data.min_volt = pdo_pps_apdo_min_voltage(pdo); - port->pps_data.max_volt = pdo_pps_apdo_max_voltage(pdo); - port->pps_data.max_curr = - min_pps_apdo_current(pdo, port->snk_pdo[snk_pdo]); + src = port->source_caps[src_pdo]; + snk = port->snk_pdo[snk_pdo]; + + port->pps_data.min_volt = max(pdo_pps_apdo_min_voltage(src), + pdo_pps_apdo_min_voltage(snk)); + port->pps_data.max_volt = min(pdo_pps_apdo_max_voltage(src), + pdo_pps_apdo_max_voltage(snk)); + port->pps_data.max_curr = min_pps_apdo_current(src, snk); port->pps_data.out_volt = - min(pdo_pps_apdo_max_voltage(pdo), port->pps_data.out_volt); + min(port->pps_data.max_volt, port->pps_data.out_volt); port->pps_data.op_curr = min(port->pps_data.max_curr, port->pps_data.op_curr); } -- 2.20.0.405.gbc1bbc6f85-goog