From patchwork Sat Mar 18 15:05:53 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: 665161 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 18F8EC74A5B for ; Sat, 18 Mar 2023 15:06:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229788AbjCRPGP (ORCPT ); Sat, 18 Mar 2023 11:06:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49576 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229478AbjCRPGO (ORCPT ); Sat, 18 Mar 2023 11:06:14 -0400 Received: from mail-wr1-x42c.google.com (mail-wr1-x42c.google.com [IPv6:2a00:1450:4864:20::42c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0CAE834F57 for ; Sat, 18 Mar 2023 08:06:11 -0700 (PDT) Received: by mail-wr1-x42c.google.com with SMTP id v16so6795527wrn.0 for ; Sat, 18 Mar 2023 08:06:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pqrs.dk; s=google; t=1679151969; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=HUAy70Y4flVHOO6/NxtK8gNZM/g6UbSdwdapwKKHF+s=; b=MPmDTLtLTbZA1uxoZTGV+exzuA5S4+YxgosovywX0117X5g6JrIODPWedYpcn6V0eE ihYcSVflxoVbtPirmMCJswgQj4af45oyrLk/Eyb90fmEzOTJ5uFKG5kSXUK5SofSuQmz i0x6B+0WC5hnSw+LbSO5865YHS4/mudU5UMbA= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679151969; 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=HUAy70Y4flVHOO6/NxtK8gNZM/g6UbSdwdapwKKHF+s=; b=cOr7MYaELODgUO8M8SzvPVlcmAdK4isAmv8pQ+DmiMXeo4KH3qOogvynQYsKvjJnDV TvxoxYODi+4xS1WzJp1mZDGfjGnk1fEAzx/IzSBL8ai3zOW/cPS7apH6s7VqcFv5LEz6 cTFVUlP7x0ABMOnxrEnli23Msf52fqYyUXEeeNfIKZRv/mvtLndi2xF31pJP2YjVtO0N cV2QRxgCaj51prLU5Xz1xajGqHt+sbaRa7KntTwDQBZNpXZmGleDPaj60b7AKs/ooRF4 UdsCOzD+QMSuXSFhiBgOI12IcMLVuZnbJ1VZ+w5dyRMfOz9kZAzUn+LDfqFztfOfnUDZ NKog== X-Gm-Message-State: AO0yUKW8Eq8b2RKWEH9yA39W/T6xJW4WqFnby25K2nNm4599KpAPPylC HMgLC9B7rtSWYb7fQX1o2LDLxA== X-Google-Smtp-Source: AK7set/iE8DJdLs+CnPZ9FRmHsL3P3kLuSsfEL/m40dApWv4FnUsIXIRGWdS0tNxieV73JC6GApdrQ== X-Received: by 2002:a05:6000:120d:b0:2d2:d65f:9309 with SMTP id e13-20020a056000120d00b002d2d65f9309mr5434987wrx.55.1679151969473; Sat, 18 Mar 2023 08:06:09 -0700 (PDT) Received: from localhost.localdomain ([185.248.85.32]) by smtp.gmail.com with ESMTPSA id c15-20020adfef4f000000b002c7066a6f77sm4522996wrp.31.2023.03.18.08.06.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 18 Mar 2023 08:06:09 -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 v3 1/2] extcon: usbc-tusb320: add accessory detection support Date: Sat, 18 Mar 2023 16:05:53 +0100 Message-Id: <20230318150555.1705749-1-alvin@pqrs.dk> X-Mailer: git-send-email 2.39.2 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-usb@vger.kernel.org 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 Acked-by: Heikki Krogerus --- v3: add Heikki's Acked-by v2: no change --- 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 Sat Mar 18 15:05:54 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: 664939 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 8A368C74A5B for ; Sat, 18 Mar 2023 15:06:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229602AbjCRPGQ (ORCPT ); Sat, 18 Mar 2023 11:06:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49586 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229478AbjCRPGP (ORCPT ); Sat, 18 Mar 2023 11:06:15 -0400 Received: from mail-wr1-x42b.google.com (mail-wr1-x42b.google.com [IPv6:2a00:1450:4864:20::42b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 08DDF34F63 for ; Sat, 18 Mar 2023 08:06:12 -0700 (PDT) Received: by mail-wr1-x42b.google.com with SMTP id i9so6772103wrp.3 for ; Sat, 18 Mar 2023 08:06:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pqrs.dk; s=google; t=1679151970; 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=6SEDJeUi4nsLFUSMCQcVvgjgip0UQkULl8wcmFVWidI=; b=K4NxlqA+zGeOoO2bVASSkaQPsmSwdcctBTsB3Fjsiak9v8clL/vcmPLyD8crHTtWfq AygOLoj6r0D39CqUmEq04veDmlg11hhaSZIzPKT9IBo2yCyDFbfwGLsBffcgmC14U1pp U4SWmBH5U8J68Xh6MzBuoiQVOXxowsOHlmpak= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679151970; 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=6SEDJeUi4nsLFUSMCQcVvgjgip0UQkULl8wcmFVWidI=; b=1uhIrMjD7EcsKyoiACkrXKw5h31MJMNMFKDtARRN2Lon8DaIfZSalnlLqXGFFHVMP+ iXztrsw7n2TO+KSL6xNk4MLNwTPxzdrTjcpspXdMnA5BHTSQEFxUZt4+KVIADMozwNiV B8MJuDDi8yAMPS3VxOLYb/bRoXlqMrONbORPHcCy3/HiPsFTzWV1xU3iqxDce2ME3D3K VEwnEKyzgThs8mMVZThzPTG+pMyiNIDoS2A0OXw7OE1j/ka3gSp6ivqlG2fEa6jUbprD gL1ZTEbkj0xE6ByeT619lZLt/tNJgN8FsnMim5OED2sW+nNVH+fq6PFuQTE4bRhIA7Zz yP+g== X-Gm-Message-State: AO0yUKVjbjT+fm3ILvzSE0pJL4/qdnfNI1YYOqLK8hN2kB7j+DKDTkPG Hqhj5yPH9QpUf40loYSrhBEgug== X-Google-Smtp-Source: AK7set99z1VnJ8zaSN5NqDYpRHJj9CO1VQr3K5GAROqMLEyDNsSnG2BFyZEOikTOymIDDA7SEfluXQ== X-Received: by 2002:adf:e348:0:b0:2cf:e70a:ad9e with SMTP id n8-20020adfe348000000b002cfe70aad9emr8707886wrj.46.1679151970545; Sat, 18 Mar 2023 08:06:10 -0700 (PDT) Received: from localhost.localdomain ([185.248.85.32]) by smtp.gmail.com with ESMTPSA id c15-20020adfef4f000000b002c7066a6f77sm4522996wrp.31.2023.03.18.08.06.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 18 Mar 2023 08:06:10 -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 v3 2/2] extcon: usbc-tusb320: add usb_role_switch support Date: Sat, 18 Mar 2023 16:05:54 +0100 Message-Id: <20230318150555.1705749-2-alvin@pqrs.dk> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230318150555.1705749-1-alvin@pqrs.dk> References: <20230318150555.1705749-1-alvin@pqrs.dk> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-usb@vger.kernel.org From: Alvin Šipraga The connector child node of the TUSB320 device might be linked with a dual-role capable USB controller. 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 Acked-by: Heikki Krogerus --- v3: drop legacy "USB OTG" terminology in commit message; refer to a dual-role capable USB controller instead v2: remove unused variable as reported by kernel test robot --- drivers/extcon/extcon-usbc-tusb320.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/drivers/extcon/extcon-usbc-tusb320.c b/drivers/extcon/extcon-usbc-tusb320.c index 882d1f48495e..cc2d0ac7c5f6 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[] = { @@ -278,6 +280,7 @@ static void tusb320_typec_irq_handler(struct tusb320_priv *priv, u8 reg9) 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 +303,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 +321,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 +329,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 +347,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 +357,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 +482,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 +504,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); }