From patchwork Wed Mar 15 22:02:45 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Alvin_=C5=A0ipraga?= X-Patchwork-Id: 70480 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:604a:0:0:0:0:0 with SMTP id j10csp153407wrt; Wed, 15 Mar 2023 15:19:06 -0700 (PDT) X-Google-Smtp-Source: AK7set9qgA/e+7130X1GFdKz8hCZEPUiKU+2No7iTDXWdk0rgdtPCU9qR4UP/g1Y6GccsLDX5a/U X-Received: by 2002:a17:902:f681:b0:19f:8bbf:9c56 with SMTP id l1-20020a170902f68100b0019f8bbf9c56mr1279068plg.3.1678918746228; Wed, 15 Mar 2023 15:19:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1678918746; cv=none; d=google.com; s=arc-20160816; b=i8ydKSC3bPD7S89ZWUcAjXMBuh/eEJz7A2uKo2NKzXB2RE1m9ulxvqKMBvj78h1bdp 75E3KJezu9nS9mDb7MjHBrthpRl8nLxdMykv2HmOlsmfAEke5c7JWItCNK08ekNbE0Kl L72om+RD9hAFFJCeH194I9ADdR/X8MPeiusPh0xua+QVyVnvAMp1nvlmhKLBPhswxTPe p+jWDe0kD8oSwS5fm5uRAtJwNgRM8Isg6iR7mqLO4vehkzRjv0S2NvhWa2KHU5Ceme7r OWliH5hU9L6PwScLcH5p0c7bOkYtRh7ESPjzl+qpp3OeBdSMLVE9MFuDMvSA0IQC26hw 5fKg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=kfpHWNJNaQ0Ott7UBqanJJgVNeCpvHZ/q3wHWsCd5cY=; b=b6YgFCC6e5Y9zv9fOGLpiOmXMSS7dBwURZfY9RG5Vqqdw73Cw8JPjhhcgMZVJkTDu7 OY37OmRJoyQbc2LuasCs1ShR+tzkFBTUkPB/d5KXTr9aPp/2KiUOKtnSo7VWcpA9mTrY KKuVo0S2YRWZaddme4TIA3qhv9SkdTqlr/5EprZVqn6eq9lyuvR+NLwzAX8Df9UgpVPC 4zwL4uZqKd2iHZ+2l7ZQfq4TKxKTP5epcTadf/74OshlrWk6B4evgMQLtg4gr5EcwjZz 8EzCwn0ktnIQ9wc/MSdm59KxUeL+nrYsq6j94lDd/5zImmPrtTSvnM7o3ch+QlbYn2Np sPKw== ARC-Authentication-Results: i=1; mx.google.com; dkim=temperror (no key for signature) header.i=@pqrs.dk header.s=google header.b=HmWtBcub; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id o19-20020a170902779300b0019909c2569dsi5975691pll.528.2023.03.15.15.18.50; Wed, 15 Mar 2023 15:19:06 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=temperror (no key for signature) header.i=@pqrs.dk header.s=google header.b=HmWtBcub; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232303AbjCOWDl (ORCPT + 99 others); Wed, 15 Mar 2023 18:03:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40820 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231701AbjCOWDh (ORCPT ); Wed, 15 Mar 2023 18:03:37 -0400 Received: from mail-ed1-x52c.google.com (mail-ed1-x52c.google.com [IPv6:2a00:1450:4864:20::52c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 934E7126EC for ; Wed, 15 Mar 2023 15:03:32 -0700 (PDT) Received: by mail-ed1-x52c.google.com with SMTP id o12so163679edb.9 for ; Wed, 15 Mar 2023 15:03:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pqrs.dk; s=google; t=1678917811; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=kfpHWNJNaQ0Ott7UBqanJJgVNeCpvHZ/q3wHWsCd5cY=; b=HmWtBcub0Nnf6OHbQOPbyN/PUFXntVAh0hfEypNB/IO40UxlfiMADbMGNCNTp//1F4 OLEVxMlYaPBxCcP9Z5SezaTXB1Q8lqVU31lHK1g7rFmKPLPNjlhmTJa9hEK8ghWiwwPt k+uP/eTIjt/Of1urf5rch1GiqP20ivrbuQThw= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678917811; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=kfpHWNJNaQ0Ott7UBqanJJgVNeCpvHZ/q3wHWsCd5cY=; b=JCbvGkCMyBhby8PJL7wG5iSnO7n3Nr6zoKZ7MKV9yXRGu/23cxpz/K+7lyVrDM1Sy7 Zs+JoOkBi6VgSP3RMB6Uoutielg5OOsuzdDaB63cHV/tIySiWLrpUzyGsxiJpkLZVv6q Z9s4hOzdn4ThwYA0qeB1lFhDCLyFvuI9CGm2p2xO48lTjGjoVOVMi8mR6mrH1fuQhb35 g2zaSe88gN5JPn1KXGjvQSXnh9p7wwZSvdh6Cdu82q2dQqozLnBI/7gfIr++xLxKhwKT zMrvf033moF+ALQgQuv/IZHjf9cxLmFg223+burotcZFwaKu3GBs+ZdrBv7+BpSuXJ0G nhSQ== X-Gm-Message-State: AO0yUKVwWBDX009wp9z6wwngj4uYoU+gzRs5uAgZDhm/DYmL/GpG3Jve 9g0YP/uXpS93EQDOMdnLRQ+nIA== X-Received: by 2002:a17:907:1690:b0:878:481c:c49b with SMTP id hc16-20020a170907169000b00878481cc49bmr11525591ejc.1.1678917810736; Wed, 15 Mar 2023 15:03:30 -0700 (PDT) Received: from localhost.localdomain (80.71.142.18.ipv4.parknet.dk. [80.71.142.18]) by smtp.gmail.com with ESMTPSA id tk13-20020a170907c28d00b0090e0a4e1bacsm2995733ejc.159.2023.03.15.15.03.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Mar 2023 15:03:29 -0700 (PDT) From: =?utf-8?q?Alvin_=C5=A0ipraga?= To: Heikki Krogerus , Greg Kroah-Hartman , MyungJoo Ham , Chanwoo Choi Cc: linux-usb@vger.kernel.org, =?utf-8?q?Alvin_=C5=A0ipraga?= , linux-kernel@vger.kernel.org Subject: [PATCH 1/2] extcon: usbc-tusb320: add accessory detection support Date: Wed, 15 Mar 2023 23:02:45 +0100 Message-Id: <20230315220246.951213-1-alvin@pqrs.dk> X-Mailer: git-send-email 2.39.2 MIME-Version: 1.0 X-Spam-Status: No, score=-1.7 required=5.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_NONE,URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1760473903302668684?= X-GMAIL-MSGID: =?utf-8?q?1760473903302668684?= From: Alvin Šipraga The TUSB320 can detect the following types of accessory: - Audio Accessory - Audio Accessory with charge-thru - Debug Accessory (DFP) - Debug Accessory (UFP) Moreover, the typec subsystem can be informed of this through the typec_set_mode() function. The information will be propagated to any linked typec muxes. Add the necessary support to the driver. Note that for the Debug Accessory modes, an educated guess was made that for the USB data role, DFP implies HOST and UFP implies DEVICE. But this might want to be made configurable at a later date. Signed-off-by: Alvin Šipraga --- drivers/extcon/extcon-usbc-tusb320.c | 90 +++++++++++++++++++++------- 1 file changed, 68 insertions(+), 22 deletions(-) diff --git a/drivers/extcon/extcon-usbc-tusb320.c b/drivers/extcon/extcon-usbc-tusb320.c index 10dff1c512c4..882d1f48495e 100644 --- a/drivers/extcon/extcon-usbc-tusb320.c +++ b/drivers/extcon/extcon-usbc-tusb320.c @@ -15,6 +15,7 @@ #include #include #include +#include #define TUSB320_REG8 0x8 #define TUSB320_REG8_CURRENT_MODE_ADVERTISE GENMASK(7, 6) @@ -26,16 +27,16 @@ #define TUSB320_REG8_CURRENT_MODE_DETECT_MED 0x1 #define TUSB320_REG8_CURRENT_MODE_DETECT_ACC 0x2 #define TUSB320_REG8_CURRENT_MODE_DETECT_HI 0x3 -#define TUSB320_REG8_ACCESSORY_CONNECTED GENMASK(3, 2) +#define TUSB320_REG8_ACCESSORY_CONNECTED GENMASK(3, 1) #define TUSB320_REG8_ACCESSORY_CONNECTED_NONE 0x0 #define TUSB320_REG8_ACCESSORY_CONNECTED_AUDIO 0x4 -#define TUSB320_REG8_ACCESSORY_CONNECTED_ACC 0x5 -#define TUSB320_REG8_ACCESSORY_CONNECTED_DEBUG 0x6 +#define TUSB320_REG8_ACCESSORY_CONNECTED_ACHRG 0x5 +#define TUSB320_REG8_ACCESSORY_CONNECTED_DBGDFP 0x6 +#define TUSB320_REG8_ACCESSORY_CONNECTED_DBGUFP 0x7 #define TUSB320_REG8_ACTIVE_CABLE_DETECTION BIT(0) #define TUSB320_REG9 0x9 -#define TUSB320_REG9_ATTACHED_STATE_SHIFT 6 -#define TUSB320_REG9_ATTACHED_STATE_MASK 0x3 +#define TUSB320_REG9_ATTACHED_STATE GENMASK(7, 6) #define TUSB320_REG9_CABLE_DIRECTION BIT(5) #define TUSB320_REG9_INTERRUPT_STATUS BIT(4) @@ -250,8 +251,7 @@ static void tusb320_extcon_irq_handler(struct tusb320_priv *priv, u8 reg) { int state, polarity; - state = (reg >> TUSB320_REG9_ATTACHED_STATE_SHIFT) & - TUSB320_REG9_ATTACHED_STATE_MASK; + state = FIELD_GET(TUSB320_REG9_ATTACHED_STATE, reg); polarity = !!(reg & TUSB320_REG9_CABLE_DIRECTION); dev_dbg(priv->dev, "attached state: %s, polarity: %d\n", @@ -277,32 +277,78 @@ static void tusb320_typec_irq_handler(struct tusb320_priv *priv, u8 reg9) { struct typec_port *port = priv->port; struct device *dev = priv->dev; - u8 mode, role, state; + int typec_mode; + enum typec_role pwr_role; + enum typec_data_role data_role; + u8 state, mode, accessory; int ret, reg8; bool ori; + ret = regmap_read(priv->regmap, TUSB320_REG8, ®8); + if (ret) { + dev_err(dev, "error during reg8 i2c read, ret=%d!\n", ret); + return; + } + ori = reg9 & TUSB320_REG9_CABLE_DIRECTION; typec_set_orientation(port, ori ? TYPEC_ORIENTATION_REVERSE : TYPEC_ORIENTATION_NORMAL); - state = (reg9 >> TUSB320_REG9_ATTACHED_STATE_SHIFT) & - TUSB320_REG9_ATTACHED_STATE_MASK; - if (state == TUSB320_ATTACHED_STATE_DFP) - role = TYPEC_SOURCE; - else - role = TYPEC_SINK; + state = FIELD_GET(TUSB320_REG9_ATTACHED_STATE, reg9); + accessory = FIELD_GET(TUSB320_REG8_ACCESSORY_CONNECTED, reg8); + + switch (state) { + case TUSB320_ATTACHED_STATE_DFP: + typec_mode = TYPEC_MODE_USB2; + pwr_role = TYPEC_SOURCE; + data_role = TYPEC_HOST; + break; + case TUSB320_ATTACHED_STATE_UFP: + typec_mode = TYPEC_MODE_USB2; + pwr_role = TYPEC_SINK; + data_role = TYPEC_DEVICE; + break; + case TUSB320_ATTACHED_STATE_ACC: + /* + * Accessory detected. For debug accessories, just make some + * qualified guesses as to the role for lack of a better option. + */ + if (accessory == TUSB320_REG8_ACCESSORY_CONNECTED_AUDIO || + accessory == TUSB320_REG8_ACCESSORY_CONNECTED_ACHRG) { + typec_mode = TYPEC_MODE_AUDIO; + pwr_role = TYPEC_SINK; + data_role = TYPEC_DEVICE; + break; + } else if (accessory == + TUSB320_REG8_ACCESSORY_CONNECTED_DBGDFP) { + typec_mode = TYPEC_MODE_DEBUG; + pwr_role = TYPEC_SOURCE; + data_role = TYPEC_HOST; + break; + } else if (accessory == + TUSB320_REG8_ACCESSORY_CONNECTED_DBGUFP) { + typec_mode = TYPEC_MODE_DEBUG; + pwr_role = TYPEC_SINK; + data_role = TYPEC_DEVICE; + break; + } - typec_set_vconn_role(port, role); - typec_set_pwr_role(port, role); - typec_set_data_role(port, role == TYPEC_SOURCE ? - TYPEC_HOST : TYPEC_DEVICE); + dev_warn(priv->dev, "unexpected ACCESSORY_CONNECTED state %d\n", + accessory); - ret = regmap_read(priv->regmap, TUSB320_REG8, ®8); - if (ret) { - dev_err(dev, "error during reg8 i2c read, ret=%d!\n", ret); - return; + fallthrough; + default: + typec_mode = TYPEC_MODE_USB2; + pwr_role = TYPEC_SINK; + data_role = TYPEC_DEVICE; + break; } + typec_set_vconn_role(port, pwr_role); + typec_set_pwr_role(port, pwr_role); + typec_set_data_role(port, data_role); + typec_set_mode(port, typec_mode); + mode = FIELD_GET(TUSB320_REG8_CURRENT_MODE_DETECT, reg8); if (mode == TUSB320_REG8_CURRENT_MODE_DETECT_DEF) typec_set_pwr_opmode(port, TYPEC_PWR_MODE_USB); From patchwork Wed Mar 15 22:02:46 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Alvin_=C5=A0ipraga?= X-Patchwork-Id: 70481 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:604a:0:0:0:0:0 with SMTP id j10csp153505wrt; Wed, 15 Mar 2023 15:19:28 -0700 (PDT) X-Google-Smtp-Source: AK7set/Ateu9ODuHwnR6ImaONsBVcmr9zD9j/qU1zEUJ+WcWXJXDPP5mCLFCTiaany9kj2btBnMd X-Received: by 2002:a17:90b:3910:b0:23f:113a:1279 with SMTP id ob16-20020a17090b391000b0023f113a1279mr1332975pjb.43.1678918767682; Wed, 15 Mar 2023 15:19:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1678918767; cv=none; d=google.com; s=arc-20160816; b=LIuvh3OkDEXRlop6YGNbrFCEyJrjjkEaL1cRLZ1OrVPFWSaSW9EOnzeYLGbHHuWC1j CblNkn8CHZoCk5JAhBE8MzwGgSC4j87EVKvohbmmSQkL1B+4s886VYLezX1l/fcFzdx9 z2HCjjZoZ4noc0Q+vmvgqmefgvCT5p+m4Kb9/YMXSZ6NqKOkPok84yHLGD1shGrSsvwH ibQO64ROi61b+0hUk+NcZiPoLcqzf/rKHRxNn7FdvXDuOnfUeM7wQ7GDHMRFhBCd9vdq hgOKTc4LPYunDA3yXP9JfqcywyizVkmjtg9Fu3JU9rzeiC9RB1/haZ4pvSV1FRBKMAaT Xpxg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=hnIAkjWXLUKxgzUQABaGNBsVC0Xz294v/24zmIaU2fM=; b=DxONg71TB5mR+ZFCEf0isNv68qKGNWkzdN6bFQYlIhOpfVUT9AdwvZ3P92MAhSHCUg 5a1WCZtZuzK5zarTygsMYKSFWsWARNrlaBSq5zeQ9lQ9kUnMpIkTotoZaBF92UX0JRJu FrjZsEgKYTcMMrZpUPKVskgMTxryHIbuNXAG0agtkgm34rg8t9G/WI5zaz3nw6oJtc6+ T8NxtavgKh+TtfPVnPzeyEb8EKO1OxAYSIH001fCqe67MrbUjtZDi0b76jsinkTGmAfh Db7rAaqESZoQyJYaV4x81e1frIsoYYq/V30X6pzn5Fe9nTqgkF0PY+fDPccKjM+mqeRi zlmA== ARC-Authentication-Results: i=1; mx.google.com; dkim=temperror (no key for signature) header.i=@pqrs.dk header.s=google header.b=DpCdtNKq; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id z18-20020a17090ab11200b0023d3f32f569si2731430pjq.3.2023.03.15.15.19.14; Wed, 15 Mar 2023 15:19:27 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=temperror (no key for signature) header.i=@pqrs.dk header.s=google header.b=DpCdtNKq; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229941AbjCOWDj (ORCPT + 99 others); Wed, 15 Mar 2023 18:03:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40798 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231201AbjCOWDh (ORCPT ); Wed, 15 Mar 2023 18:03:37 -0400 Received: from mail-ed1-x52b.google.com (mail-ed1-x52b.google.com [IPv6:2a00:1450:4864:20::52b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9AADF12F34 for ; Wed, 15 Mar 2023 15:03:33 -0700 (PDT) Received: by mail-ed1-x52b.google.com with SMTP id eh3so127617edb.11 for ; Wed, 15 Mar 2023 15:03:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pqrs.dk; s=google; t=1678917812; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=hnIAkjWXLUKxgzUQABaGNBsVC0Xz294v/24zmIaU2fM=; b=DpCdtNKqrJEpglsHttbfk5MuRbcmjFPga1OslUNdmfD/UWOmZsysmL9NCLnHMOWCAC r4wyvPyQe9HU/7ppEmyHGXOpWBy9zuJQ4vM55QnC3eIiLPjqAmFmFVBUSblHdBrMMooN bqgvNadWzhZF7bBiCnO18X32jRfrofakoKD8A= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678917812; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=hnIAkjWXLUKxgzUQABaGNBsVC0Xz294v/24zmIaU2fM=; b=PvIpigRlFpbK6raFRa5oe3ZwMxp6uStXrQiU4jzEx06b9DDnVnWb7RuL64BEB+/bD5 0UZYKA+t+3+7vN4SQjuqM4qKHwrU96t1MZTMbtJ2JOb0gTHviCdBeyjNQz5j2meHgUtV zY5MtC99uM1u+c8PcDlUB/kxnhK7M7dJyHs6fzBONl7GwqTUEA74/Z9ZdLdQG06E/SXK e19HrYACWJfBP1aAtBSYfVdVHtoxb/1p5DJpEmDS5GyvC+NwfkhGo4tN7DVo+L6TyDdR NSty+eH9OqhG/mLe10fXcyafRh2haw9i9ebcv89YJE2sYBONpZVVHhSRtEj/TQKCmebg buUw== X-Gm-Message-State: AO0yUKU/5Ru1GUdLRhgJblaCIl5d3FQCMSANU41eDt9IafUQHZqdWiiw IvrOzps+h4xFJPw2/+MQxOVqvQ== X-Received: by 2002:a17:907:a649:b0:8f0:143d:ee34 with SMTP id vu9-20020a170907a64900b008f0143dee34mr9029863ejc.1.1678917811806; Wed, 15 Mar 2023 15:03:31 -0700 (PDT) Received: from localhost.localdomain (80.71.142.18.ipv4.parknet.dk. [80.71.142.18]) by smtp.gmail.com with ESMTPSA id tk13-20020a170907c28d00b0090e0a4e1bacsm2995733ejc.159.2023.03.15.15.03.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Mar 2023 15:03:31 -0700 (PDT) From: =?utf-8?q?Alvin_=C5=A0ipraga?= To: Heikki Krogerus , Greg Kroah-Hartman , MyungJoo Ham , Chanwoo Choi Cc: linux-usb@vger.kernel.org, =?utf-8?q?Alvin_=C5=A0ipraga?= , linux-kernel@vger.kernel.org Subject: [PATCH 2/2] extcon: usbc-tusb320: add usb_role_switch support Date: Wed, 15 Mar 2023 23:02:46 +0100 Message-Id: <20230315220246.951213-2-alvin@pqrs.dk> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230315220246.951213-1-alvin@pqrs.dk> References: <20230315220246.951213-1-alvin@pqrs.dk> MIME-Version: 1.0 X-Spam-Status: No, score=-1.7 required=5.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_NONE,URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1760473925984231828?= X-GMAIL-MSGID: =?utf-8?q?1760473925984231828?= From: Alvin Šipraga The connector child node of the TUSB320 device might be linked with a USB OTG controller with USB role switch capability. Add driver support for detecting a usb_role_switch and setting its state in the typec interrupt handler. This follows similar practice in other drivers in the typec subsystem, which this extcon driver can opt-in to. Signed-off-by: Alvin Šipraga --- drivers/extcon/extcon-usbc-tusb320.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/drivers/extcon/extcon-usbc-tusb320.c b/drivers/extcon/extcon-usbc-tusb320.c index 882d1f48495e..44b55650b6b4 100644 --- a/drivers/extcon/extcon-usbc-tusb320.c +++ b/drivers/extcon/extcon-usbc-tusb320.c @@ -16,6 +16,7 @@ #include #include #include +#include #define TUSB320_REG8 0x8 #define TUSB320_REG8_CURRENT_MODE_ADVERTISE GENMASK(7, 6) @@ -80,6 +81,7 @@ struct tusb320_priv { enum typec_port_type port_type; enum typec_pwr_opmode pwr_opmode; struct fwnode_handle *connector_fwnode; + struct usb_role_switch *role_sw; }; static const char * const tusb_attached_states[] = { @@ -275,9 +277,11 @@ static void tusb320_extcon_irq_handler(struct tusb320_priv *priv, u8 reg) static void tusb320_typec_irq_handler(struct tusb320_priv *priv, u8 reg9) { + struct usb_role_switch *role_sw = priv->role_sw; struct typec_port *port = priv->port; struct device *dev = priv->dev; int typec_mode; + enum usb_role usb_role; enum typec_role pwr_role; enum typec_data_role data_role; u8 state, mode, accessory; @@ -300,11 +304,13 @@ static void tusb320_typec_irq_handler(struct tusb320_priv *priv, u8 reg9) switch (state) { case TUSB320_ATTACHED_STATE_DFP: typec_mode = TYPEC_MODE_USB2; + usb_role = USB_ROLE_HOST; pwr_role = TYPEC_SOURCE; data_role = TYPEC_HOST; break; case TUSB320_ATTACHED_STATE_UFP: typec_mode = TYPEC_MODE_USB2; + usb_role = USB_ROLE_DEVICE; pwr_role = TYPEC_SINK; data_role = TYPEC_DEVICE; break; @@ -316,6 +322,7 @@ static void tusb320_typec_irq_handler(struct tusb320_priv *priv, u8 reg9) if (accessory == TUSB320_REG8_ACCESSORY_CONNECTED_AUDIO || accessory == TUSB320_REG8_ACCESSORY_CONNECTED_ACHRG) { typec_mode = TYPEC_MODE_AUDIO; + usb_role = USB_ROLE_NONE; pwr_role = TYPEC_SINK; data_role = TYPEC_DEVICE; break; @@ -323,12 +330,14 @@ static void tusb320_typec_irq_handler(struct tusb320_priv *priv, u8 reg9) TUSB320_REG8_ACCESSORY_CONNECTED_DBGDFP) { typec_mode = TYPEC_MODE_DEBUG; pwr_role = TYPEC_SOURCE; + usb_role = USB_ROLE_HOST; data_role = TYPEC_HOST; break; } else if (accessory == TUSB320_REG8_ACCESSORY_CONNECTED_DBGUFP) { typec_mode = TYPEC_MODE_DEBUG; pwr_role = TYPEC_SINK; + usb_role = USB_ROLE_DEVICE; data_role = TYPEC_DEVICE; break; } @@ -339,6 +348,7 @@ static void tusb320_typec_irq_handler(struct tusb320_priv *priv, u8 reg9) fallthrough; default: typec_mode = TYPEC_MODE_USB2; + usb_role = USB_ROLE_NONE; pwr_role = TYPEC_SINK; data_role = TYPEC_DEVICE; break; @@ -348,6 +358,7 @@ static void tusb320_typec_irq_handler(struct tusb320_priv *priv, u8 reg9) typec_set_pwr_role(port, pwr_role); typec_set_data_role(port, data_role); typec_set_mode(port, typec_mode); + usb_role_switch_set_role(priv->role_sw, usb_role); mode = FIELD_GET(TUSB320_REG8_CURRENT_MODE_DETECT, reg8); if (mode == TUSB320_REG8_CURRENT_MODE_DETECT_DEF) @@ -472,10 +483,20 @@ static int tusb320_typec_probe(struct i2c_client *client, goto err_put; } + /* Find any optional USB role switch that needs reporting to */ + priv->role_sw = fwnode_usb_role_switch_get(connector); + if (IS_ERR(priv->role_sw)) { + ret = PTR_ERR(priv->role_sw); + goto err_unreg; + } + priv->connector_fwnode = connector; return 0; +err_unreg: + typec_unregister_port(priv->port); + err_put: fwnode_handle_put(connector); @@ -484,6 +505,7 @@ static int tusb320_typec_probe(struct i2c_client *client, static void tusb320_typec_remove(struct tusb320_priv *priv) { + usb_role_switch_put(priv->role_sw); typec_unregister_port(priv->port); fwnode_handle_put(priv->connector_fwnode); }