Received: by 10.223.185.116 with SMTP id b49csp779141wrg; Wed, 14 Feb 2018 06:55:10 -0800 (PST) X-Google-Smtp-Source: AH8x226X5LgH2QQP7HOgAvo/SDWjEz97ThmjYpS8DiZAd/sLdVCTiiWT9m6g17mQWYc5ym6eti6v X-Received: by 10.98.171.12 with SMTP id p12mr4985725pff.71.1518620110359; Wed, 14 Feb 2018 06:55:10 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1518620110; cv=none; d=google.com; s=arc-20160816; b=n+aXLrI30iCUTRQM9ywetWOTca/4HKXgod8+hoMa9EohBAPIzxB/TdsqXsv4b0B7ks MzVk9Md3seXVGEpXvalPPA2ob+WHeUY7SeRH/uJrnq8L+sq8+6QxZJRdDHVQNBGLv06s x0qW3kHTAS1Je7Q9BtQFcq5nck2Sb995zNOfDvzfAxdnSNFoslJ0PgpSljM4XcpHquAk 2uqpttcTeBef9mX8u+FwDDx5bqPMHTanzVu3aISV5GlztG4Mw+vSzj7gglSh9AEHOW0+ 9IcXyp3c/1ysRc9okK6xvEW91j2N8HJnd11guZz2EOxoMrJ4gB4YAUeES4rbFMrUNIcs 1/oA== 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 :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject:dkim-signature :arc-authentication-results; bh=6ESO099qt84QgLjxijNt9rRPuQId6rWH1dxroM/L1pM=; b=gmIgqIw4VYINtHJ1vyz4lXzcCvP7QAa54da/7n32h2ztDF1T7ActmfFPPI6Cvi0tEV ecJOIUN3eBLOoWSr3fL9LjSUDL9kfmM7DbBbysc14N/YecEx/3u0DMVPGuU16N7Rdr8b KvEc1LVastEwlMMkQ5vHXuVhL6mFSSynqXlWpNEwU7usk47JCEHqhX+ErzNBQYuvlzNw 0/GWjTE8fx48bqLAvk0uohrOTjPia2yststtNhUhafDBFGcGn3NxQHYcPpGRO+1Jleot YVjnNdwlSonegOPJd774t7D2XGv88vw83pcZmtzVA2RBv9e7aET6Hj4kUl8ARJMUoZwZ nt5Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@gmail.com header.s=20161025 header.b=FyCDD4or; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id k79si699756pfj.192.2018.02.14.06.54.55; Wed, 14 Feb 2018 06:55:10 -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=fail header.i=@gmail.com header.s=20161025 header.b=FyCDD4or; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1031081AbeBNOw0 (ORCPT + 99 others); Wed, 14 Feb 2018 09:52:26 -0500 Received: from mail-pf0-f195.google.com ([209.85.192.195]:36892 "EHLO mail-pf0-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1030822AbeBNOqk (ORCPT ); Wed, 14 Feb 2018 09:46:40 -0500 Received: by mail-pf0-f195.google.com with SMTP id p1so4021103pfh.4; Wed, 14 Feb 2018 06:46:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-language:content-transfer-encoding; bh=6ESO099qt84QgLjxijNt9rRPuQId6rWH1dxroM/L1pM=; b=FyCDD4ord7lKwkJmaYyPNoXcOeQPqMXwRHaO1Wev9bnpjOVvrbIY7wXFE+YMcB+0TA jotxj1uVsegvACRQOKY9kqxYF50/tkXZYSWH5+CnvgrJk1yCuJuEwJy0CsdfsgvXxoPd B8HIYawCu85RtjTiLSbC0tqwsq2o//PAgeGBq9IHY8QJvx08zSTykhwqF+A6QUcWdPEU pAads7uabCWyEKqM4hLYofdneWaQ5XVQbRm/6fnR5dQOFj2Kior/Jiqc0JvEzXW8mIxv b8X4iJXGa9tDbBWXFB66QLop7LhVzQ1HWqXEeoHC0SajCQ04veokb2l6lbArereh2Cqf AcUg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:subject:to:cc:references:from:message-id :date:user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=6ESO099qt84QgLjxijNt9rRPuQId6rWH1dxroM/L1pM=; b=b3bR6CRKLFBuqGcx/fl8LE3LzTq+SLhA2+xVNyKGakGtCJ0UycaPeSG8TgJMCc8Usc vhgQ2ezYQ1Ac8bKU0pkq2I1IoP1DzuoKeQodHZAxiEyq3d6NyR4km7uMh+RuLX9gi2Mb G7S/1+BsTU29LWQhLwyEOyAk+Fem/bPKg3+/QZ+uvQZudtlmrw/VnkhjQANWlX0Ifa07 Eh9vtGWGadBTIGNjZKmf0Q5eqJNNMaReLKLVuVfPSdQIeNJC/NPJ/jf+4kqtCJdBCYCO xUqHWZIQJTIW+U4rMS/Iz25mkqEV182UwRnnlSzKMMQdfMWzVLWzJtnPRGxX6JEVKoKc lKiw== X-Gm-Message-State: APf1xPDCzVmxH3g1GBSwptsP3IxYZCkh5+EzWq44mjBBssi8138Sq1DN X2NvasUjD8eiusE3kNgkHMlU0A== X-Received: by 10.101.98.23 with SMTP id d23mr4140458pgv.275.1518619599911; Wed, 14 Feb 2018 06:46:39 -0800 (PST) Received: from server.roeck-us.net (108-223-40-66.lightspeed.sntcca.sbcglobal.net. [108.223.40.66]) by smtp.gmail.com with ESMTPSA id v2sm36310850pfe.171.2018.02.14.06.46.37 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 14 Feb 2018 06:46:38 -0800 (PST) Subject: Re: [PATCH v2] staging: typec: handle vendor defined part and modify drp toggling flow To: ShuFanLee , heikki.krogerus@linux.intel.com Cc: linux-kernel@vger.kernel.org, linux-usb@vger.kernel.org, cy_huang@richtek.com, shufan_lee@richtek.com References: <1518612334-24140-1-git-send-email-leechu729@gmail.com> From: Guenter Roeck Message-ID: <71b01ca3-99ce-8af4-0967-0aaca4ea6fcf@roeck-us.net> Date: Wed, 14 Feb 2018 06:46:36 -0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.6.0 MIME-Version: 1.0 In-Reply-To: <1518612334-24140-1-git-send-email-leechu729@gmail.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 02/14/2018 04:45 AM, ShuFanLee wrote: > From: ShuFanLee > > Handle vendor defined behavior in tcpci_init, tcpci_set_vconn and export tcpci_irq. > More operations can be extended in tcpci_data if needed. > According to TCPCI specification, 4.4.5.2 ROLE_CONTROL, > TCPC shall not start DRP toggling until subsequently the TCPM > writes to the COMMAND register to start DRP toggling. > DRP toggling flow is chagned as following: > - Write DRP = 0 & Rd/Rd > - Write DRP = 1 > - Set LOOK4CONNECTION command > > Signed-off-by: ShuFanLee > --- > drivers/staging/typec/tcpci.c | 111 ++++++++++++++++++++++++++++++++++-------- > drivers/staging/typec/tcpci.h | 13 +++++ > 2 files changed, 103 insertions(+), 21 deletions(-) > > diff --git a/drivers/staging/typec/tcpci.c b/drivers/staging/typec/tcpci.c > index 9bd4412..461fbd48 100644 > --- a/drivers/staging/typec/tcpci.c > +++ b/drivers/staging/typec/tcpci.c > @@ -21,7 +21,6 @@ > > struct tcpci { > struct device *dev; > - struct i2c_client *client; > > struct tcpm_port *port; > > @@ -30,6 +29,7 @@ struct tcpci { > bool controls_vbus; > > struct tcpc_dev tcpc; > + struct tcpci_data *data; > }; > > static inline struct tcpci *tcpc_to_tcpci(struct tcpc_dev *tcpc) > @@ -98,8 +98,10 @@ static int tcpci_set_cc(struct tcpc_dev *tcpc, enum typec_cc_status cc) > static int tcpci_start_drp_toggling(struct tcpc_dev *tcpc, > enum typec_cc_status cc) > { > + int ret; > struct tcpci *tcpci = tcpc_to_tcpci(tcpc); > - unsigned int reg = TCPC_ROLE_CTRL_DRP; > + unsigned int reg = (TCPC_ROLE_CTRL_CC_RD << TCPC_ROLE_CTRL_CC1_SHIFT) | > + (TCPC_ROLE_CTRL_CC_RD << TCPC_ROLE_CTRL_CC2_SHIFT); > > switch (cc) { > default: > @@ -116,8 +118,19 @@ static int tcpci_start_drp_toggling(struct tcpc_dev *tcpc, > TCPC_ROLE_CTRL_RP_VAL_SHIFT); > break; > } > - > - return regmap_write(tcpci->regmap, TCPC_ROLE_CTRL, reg); > + ret = regmap_write(tcpci->regmap, TCPC_ROLE_CTRL, reg); > + if (ret < 0) > + return ret; > + usleep_range(500, 1000); > + reg |= TCPC_ROLE_CTRL_DRP; > + ret = regmap_write(tcpci->regmap, TCPC_ROLE_CTRL, reg); > + if (ret < 0) > + return ret; > + ret = regmap_write(tcpci->regmap, TCPC_COMMAND, > + TCPC_CMD_LOOK4CONNECTION); > + if (ret < 0) > + return ret; > + return 0; > } > > static enum typec_cc_status tcpci_to_typec_cc(unsigned int cc, bool sink) > @@ -178,6 +191,16 @@ static int tcpci_set_vconn(struct tcpc_dev *tcpc, bool enable) > struct tcpci *tcpci = tcpc_to_tcpci(tcpc); > int ret; > > + /* Handle vendor set vconn */ > + if (tcpci->data) { > + if (tcpci->data->set_vconn) { > + ret = tcpci->data->set_vconn(tcpci, tcpci->data, > + enable); > + if (ret < 0) > + return ret; > + } > + } > + > ret = regmap_write(tcpci->regmap, TCPC_POWER_CTRL, > enable ? TCPC_POWER_CTRL_VCONN_ENABLE : 0); > if (ret < 0) > @@ -323,6 +346,15 @@ static int tcpci_init(struct tcpc_dev *tcpc) > if (time_after(jiffies, timeout)) > return -ETIMEDOUT; > > + /* Handle vendor init */ > + if (tcpci->data) { > + if (tcpci->data->init) { > + ret = tcpci->data->init(tcpci, tcpci->data); > + if (ret < 0) > + return ret; > + } > + } > + > /* Clear all events */ > ret = tcpci_write16(tcpci, TCPC_ALERT, 0xffff); > if (ret < 0) > @@ -344,9 +376,16 @@ static int tcpci_init(struct tcpc_dev *tcpc) > return tcpci_write16(tcpci, TCPC_ALERT_MASK, reg); > } > > -static irqreturn_t tcpci_irq(int irq, void *dev_id) > +static irqreturn_t _tcpci_irq(int irq, void *dev_id) > { > struct tcpci *tcpci = dev_id; > + > + tcpci_irq(tcpci); > + return IRQ_HANDLED; This should probably be return tcpci_irq(tcpci); > +} > + > +int tcpci_irq(struct tcpci *tcpci) and this should be irqreturn_t tcpci_irq(struct tcpci *tcpci) Otherwise it doesn't add value to have tcpci_irq() return anything. Alternative would be to make it void tcpci_irq(struct tcpci *tcpci) if it will always only return IRQ_HANDLED. Guenter > +{ > u16 status; > > tcpci_read16(tcpci, TCPC_ALERT, &status); > @@ -412,6 +451,7 @@ static irqreturn_t tcpci_irq(int irq, void *dev_id) > > return IRQ_HANDLED; > } > +EXPORT_SYMBOL_GPL(tcpci_irq); > > static const struct regmap_config tcpci_regmap_config = { > .reg_bits = 8, > @@ -435,22 +475,18 @@ static int tcpci_parse_config(struct tcpci *tcpci) > return 0; > } > > -static int tcpci_probe(struct i2c_client *client, > - const struct i2c_device_id *i2c_id) > +struct tcpci *tcpci_register_port(struct device *dev, struct tcpci_data *data) > { > struct tcpci *tcpci; > int err; > > - tcpci = devm_kzalloc(&client->dev, sizeof(*tcpci), GFP_KERNEL); > + tcpci = devm_kzalloc(dev, sizeof(*tcpci), GFP_KERNEL); > if (!tcpci) > - return -ENOMEM; > + return ERR_PTR(-ENOMEM); > > - tcpci->client = client; > - tcpci->dev = &client->dev; > - i2c_set_clientdata(client, tcpci); > - tcpci->regmap = devm_regmap_init_i2c(client, &tcpci_regmap_config); > - if (IS_ERR(tcpci->regmap)) > - return PTR_ERR(tcpci->regmap); > + tcpci->dev = dev; > + tcpci->data = data; > + tcpci->regmap = data->regmap; > > tcpci->tcpc.init = tcpci_init; > tcpci->tcpc.get_vbus = tcpci_get_vbus; > @@ -467,27 +503,60 @@ static int tcpci_probe(struct i2c_client *client, > > err = tcpci_parse_config(tcpci); > if (err < 0) > - return err; > + return ERR_PTR(err); > > /* Disable chip interrupts */ > tcpci_write16(tcpci, TCPC_ALERT_MASK, 0); > > - err = devm_request_threaded_irq(tcpci->dev, client->irq, NULL, > - tcpci_irq, > + tcpci->port = tcpm_register_port(tcpci->dev, &tcpci->tcpc); > + if (PTR_ERR_OR_ZERO(tcpci->port)) > + return ERR_CAST(tcpci->port); > + > + return tcpci; > +} > +EXPORT_SYMBOL_GPL(tcpci_register_port); > + > +void tcpci_unregister_port(struct tcpci *tcpci) > +{ > + tcpm_unregister_port(tcpci->port); > +} > +EXPORT_SYMBOL_GPL(tcpci_unregister_port); > + > +static int tcpci_probe(struct i2c_client *client, > + const struct i2c_device_id *i2c_id) > +{ > + struct tcpci *tcpci; > + struct tcpci_data *data; > + int err; > + > + data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL); > + if (!data) > + return -ENOMEM; > + > + data->regmap = devm_regmap_init_i2c(client, &tcpci_regmap_config); > + if (IS_ERR(data->regmap)) > + return PTR_ERR(data->regmap); > + > + tcpci = tcpci_register_port(&client->dev, data); > + if (PTR_ERR_OR_ZERO(tcpci)) > + return PTR_ERR(tcpci); > + > + err = devm_request_threaded_irq(&client->dev, client->irq, NULL, > + _tcpci_irq, > IRQF_ONESHOT | IRQF_TRIGGER_LOW, > dev_name(tcpci->dev), tcpci); I am a bit concerned that this may cause problems, since tcpm_register_port() will send messages to the driver, which may trigger interrupts. It may be necessary to register interrupts early, prior to calling tcpci_register_port(). > if (err < 0) > return err; You'll need to call tcpm_unregister_port() here (or register the interrupt first). Guenter > > - tcpci->port = tcpm_register_port(tcpci->dev, &tcpci->tcpc); > - return PTR_ERR_OR_ZERO(tcpci->port); > + i2c_set_clientdata(client, tcpci); > + return 0; > } > > static int tcpci_remove(struct i2c_client *client) > { > struct tcpci *tcpci = i2c_get_clientdata(client); > > - tcpm_unregister_port(tcpci->port); > + tcpci_unregister_port(tcpci); > > return 0; > } > diff --git a/drivers/staging/typec/tcpci.h b/drivers/staging/typec/tcpci.h > index fdfb06c..c838723 100644 > --- a/drivers/staging/typec/tcpci.h > +++ b/drivers/staging/typec/tcpci.h > @@ -59,6 +59,7 @@ > #define TCPC_POWER_CTRL_VCONN_ENABLE BIT(0) > > #define TCPC_CC_STATUS 0x1d > +#define TCPC_CC_STATUS_DRPRST BIT(5) > #define TCPC_CC_STATUS_TERM BIT(4) > #define TCPC_CC_STATUS_CC2_SHIFT 2 > #define TCPC_CC_STATUS_CC2_MASK 0x3 > @@ -121,4 +122,16 @@ > #define TCPC_VBUS_VOLTAGE_ALARM_HI_CFG 0x76 > #define TCPC_VBUS_VOLTAGE_ALARM_LO_CFG 0x78 > > +struct tcpci; > +struct tcpci_data { > + struct regmap *regmap; > + int (*init)(struct tcpci *tcpci, struct tcpci_data *data); > + int (*set_vconn)(struct tcpci *tcpci, struct tcpci_data *data, > + bool enable); > +}; > + > +struct tcpci *tcpci_register_port(struct device *dev, struct tcpci_data *data); > +void tcpci_unregister_port(struct tcpci *tcpci); > +int tcpci_irq(struct tcpci *tcpci); > + > #endif /* __LINUX_USB_TCPCI_H */ >