From patchwork Sun Aug 1 10:07:18 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yassine Oudjana X-Patchwork-Id: 490495 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-12.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI, SPF_HELO_NONE, SPF_PASS autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id A62CEC4338F for ; Sun, 1 Aug 2021 10:07:33 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 8AA32610A6 for ; Sun, 1 Aug 2021 10:07:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231722AbhHAKHk (ORCPT ); Sun, 1 Aug 2021 06:07:40 -0400 Received: from mail4.protonmail.ch ([185.70.40.27]:29800 "EHLO mail4.protonmail.ch" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231766AbhHAKHj (ORCPT ); Sun, 1 Aug 2021 06:07:39 -0400 Date: Sun, 01 Aug 2021 10:07:18 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=protonmail.com; s=protonmail; t=1627812450; bh=cKSXPQaFS0GkTDYT3pJQpgc7Jddui/4xYTd67zjx9rs=; h=Date:To:From:Cc:Reply-To:Subject:In-Reply-To:References:From; b=No6ylmOOv6eZauDjPhJgNcexjWGxFbdlVozGnDOqN7/fA70YMCD365SYakxcQw6Xl k6CK1fPGvCO6YoX0BvrU+ssh4czvFczOfZSpvLzWPM1P22HcNnonLKRAdWuWuSYLYB qvENBbWlqxfzJ3i7OXEMcOeSsbb5/sEhtiomvFFE= To: MyungJoo Ham , Chanwoo Choi , Rob Herring From: Yassine Oudjana Cc: Yassine Oudjana , Michael Auchter , linux-kernel@vger.kernel.org, devicetree@vger.kernel.org Reply-To: Yassine Oudjana Subject: [PATCH v3 2/3] extcon: usbc-tusb320: Add support for TUSB320L Message-ID: <20210801100519.440574-3-y.oudjana@protonmail.com> In-Reply-To: <20210801100519.440574-1-y.oudjana@protonmail.com> References: <20210801100519.440574-1-y.oudjana@protonmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: devicetree@vger.kernel.org TUSB320L is a newer chip with additional features, and it has additional steps in its mode changing sequence: - Disable CC state machine, - Write to mode register, - Wait for 5 ms, - Re-enable CC state machine. It also has an additional register that a revision number can be read from. Add support for the mode changing sequence, and read the revision number during probe and print it as info. Changes since v2: - Use a separate mode setting function for each of TUSB320 and TUSB320L. Signed-off-by: Yassine Oudjana --- drivers/extcon/extcon-usbc-tusb320.c | 74 +++++++++++++++++++++++++++- 1 file changed, 72 insertions(+), 2 deletions(-) diff --git a/drivers/extcon/extcon-usbc-tusb320.c b/drivers/extcon/extcon-usbc-tusb320.c index 867fb2bf2bdc..5a80ca7a15eb 100644 --- a/drivers/extcon/extcon-usbc-tusb320.c +++ b/drivers/extcon/extcon-usbc-tusb320.c @@ -21,10 +21,13 @@ #define TUSB320_REG9_INTERRUPT_STATUS BIT(4) #define TUSB320_REGA 0xa +#define TUSB320L_REGA_DISABLE_TERM BIT(0) #define TUSB320_REGA_I2C_SOFT_RESET BIT(3) #define TUSB320_REGA_MODE_SELECT_SHIFT 4 #define TUSB320_REGA_MODE_SELECT_MASK 0x3 +#define TUSB320L_REGA0_REVISION 0xa0 + enum tusb320_attached_state { TUSB320_ATTACHED_STATE_NONE, TUSB320_ATTACHED_STATE_DFP, @@ -39,9 +42,16 @@ enum tusb320_mode { TUSB320_MODE_DRP, }; +enum tusb320_type { + TYPE_TUSB320, + TYPE_TUSB320L, +}; + + struct tusb320_priv { struct device *dev; struct regmap *regmap; + enum tusb320_type type; struct extcon_dev *edev; enum tusb320_attached_state state; @@ -106,12 +116,53 @@ static int tusb320_set_mode(struct tusb320_priv *priv, enum tusb320_mode mode) return 0; } +static int tusb320l_set_mode(struct tusb320_priv *priv, enum tusb320_mode mode) +{ + int ret; + + /* Disable CC state machine */ + ret = regmap_write_bits(priv->regmap, TUSB320_REGA, + TUSB320L_REGA_DISABLE_TERM, 1); + if (ret) { + dev_err(priv->dev, + "failed to disable CC state machine: %d\n", ret); + return ret; + } + + /* Write mode */ + ret = regmap_write_bits(priv->regmap, TUSB320_REGA, + TUSB320_REGA_MODE_SELECT_MASK << TUSB320_REGA_MODE_SELECT_SHIFT, + mode << TUSB320_REGA_MODE_SELECT_SHIFT); + if (ret) { + dev_err(priv->dev, "failed to write mode: %d\n", ret); + goto err; + } + + msleep(5); +err: + /* Re-enable CC state machine */ + ret = regmap_write_bits(priv->regmap, TUSB320_REGA, + TUSB320L_REGA_DISABLE_TERM, 0); + if (ret) + dev_err(priv->dev, + "failed to re-enable CC state machine: %d\n", ret); + + return ret; +} + static int tusb320_reset(struct tusb320_priv *priv) { int ret; /* Set mode to default (follow PORT pin) */ - ret = tusb320_set_mode(priv, TUSB320_MODE_PORT); + switch (priv->type) { + case TYPE_TUSB320: + ret = tusb320_set_mode(priv, TUSB320_MODE_PORT); + break; + case TYPE_TUSB320L: + ret = tusb320l_set_mode(priv, TUSB320_MODE_PORT); + break; + } if (ret && ret != -EBUSY) { dev_err(priv->dev, "failed to set mode to PORT: %d\n", ret); @@ -183,6 +234,8 @@ static int tusb320_extcon_probe(struct i2c_client *client, const struct i2c_device_id *id) { struct tusb320_priv *priv; + const void *match_data; + unsigned int revision; int ret; priv = devm_kzalloc(&client->dev, sizeof(*priv), GFP_KERNEL); @@ -198,12 +251,28 @@ static int tusb320_extcon_probe(struct i2c_client *client, if (ret) return ret; + match_data = device_get_match_data(&client->dev); + if (!match_data) + return -EINVAL; + + priv->type = (enum tusb320_type)match_data; + priv->edev = devm_extcon_dev_allocate(priv->dev, tusb320_extcon_cable); if (IS_ERR(priv->edev)) { dev_err(priv->dev, "failed to allocate extcon device\n"); return PTR_ERR(priv->edev); } + if (priv->type == TYPE_TUSB320L) { + ret = regmap_read(priv->regmap, TUSB320L_REGA0_REVISION, &revision); + + if (ret) + dev_warn(priv->dev, + "failed to read revision register: %d\n", ret); + else + dev_info(priv->dev, "chip revision %d\n", revision); + } + ret = devm_extcon_dev_register(priv->dev, priv->edev); if (ret < 0) { dev_err(priv->dev, "failed to register extcon device\n"); @@ -238,7 +307,8 @@ static int tusb320_extcon_probe(struct i2c_client *client, } static const struct of_device_id tusb320_extcon_dt_match[] = { - { .compatible = "ti,tusb320", }, + { .compatible = "ti,tusb320", .data = (void *)TYPE_TUSB320, }, + { .compatible = "ti,tusb320l", .data = (void *)TYPE_TUSB320L, }, { } }; MODULE_DEVICE_TABLE(of, tusb320_extcon_dt_match);