mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Stanley Chang[昌育德]" <stanley_chang@realtek.com>
To: "Stanley Chang[昌育德]" <stanley_chang@realtek.com>,
	"Greg Kroah-Hartman" <gregkh@linuxfoundation.org>
Cc: "Vinod Koul" <vkoul@kernel.org>,
	"Johan Hovold" <johan@kernel.org>,
	"Kishon Vijay Abraham I" <kishon@kernel.org>,
	"Geert Uytterhoeven" <geert+renesas@glider.be>,
	"Rob Herring" <robh@kernel.org>,
	"Jinjie Ruan" <ruanjinjie@huawei.com>,
	"Alan Stern" <stern@rowland.harvard.edu>,
	"Yang Yingliang" <yangyingliang@huawei.com>,
	"Ricardo Cañuelo" <ricardo.canuelo@collabora.com>,
	"Roy Luo" <royluo@google.com>,
	"Heikki Krogerus" <heikki.krogerus@linux.intel.com>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
	"linux-phy@lists.infradead.org" <linux-phy@lists.infradead.org>,
	"linux-usb@vger.kernel.org" <linux-usb@vger.kernel.org>
Subject: RE: [PATCH v3 4/4] usb: core: add phy notify connect and disconnect
Date: Thu, 7 Dec 2023 04:50:10 +0000	[thread overview]
Message-ID: <4736a6933f3546c48f24a635c15a5e20@realtek.com> (raw)
In-Reply-To: <20231110054738.23515-4-stanley_chang@realtek.com>

Hi Greg,

Please help review this patch.

Thanks,
Stanley

> -----Original Message-----
> From: Stanley Chang <stanley_chang@realtek.com>
> Sent: Friday, November 10, 2023 1:45 PM
> To: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
> Cc: Stanley Chang[昌育德] <stanley_chang@realtek.com>; Vinod Koul
> <vkoul@kernel.org>; Johan Hovold <johan@kernel.org>; Kishon Vijay Abraham
> I <kishon@kernel.org>; Geert Uytterhoeven <geert+renesas@glider.be>; Rob
> Herring <robh@kernel.org>; Jinjie Ruan <ruanjinjie@huawei.com>; Alan Stern
> <stern@rowland.harvard.edu>; Yang Yingliang <yangyingliang@huawei.com>;
> Ricardo Cañuelo <ricardo.canuelo@collabora.com>; Roy Luo
> <royluo@google.com>; Heikki Krogerus <heikki.krogerus@linux.intel.com>;
> linux-kernel@vger.kernel.org; linux-phy@lists.infradead.org;
> linux-usb@vger.kernel.org
> Subject: [PATCH v3 4/4] usb: core: add phy notify connect and disconnect
> 
> In Realtek SoC, the parameter of usb phy is designed to can dynamic tuning
> base on port status. Therefore, add a notify callback of generic phy driver
> when usb device connect and disconnect change.
> 
> The Realtek phy driver is designed to dynamically adjust disconnection level
> and calibrate phy parameters. When the device connected bit changes and
> when the disconnected bit changes, do connection change notification:
> 
> Check if portstatus is USB_PORT_STAT_CONNECTION and portchange is
> USB_PORT_STAT_C_CONNECTION.
> 1. The device is connected, the driver lowers the disconnection level and
>    calibrates the phy parameters.
> 2. The device disconnects, the driver increases the disconnect level and
>    calibrates the phy parameters.
> 
> Generic phy driver in usb core framework does not support device connect and
> disconnect notifications. Therefore, we add an api to notify phy the connection
> changes.
> 
> Additionally, the generic phy only specifies primary_hcd in the original design.
> Added specific "usb2-phy" on primary_hcd and "usb3-phy" on shared_hcd.
> 
> Signed-off-by: Stanley Chang <stanley_chang@realtek.com>
> ---
> v1 to v2 change:
>     rebase the driver to remove the part of usb phy notify API
> v2 to v3:
>     No change
> ---
>  drivers/usb/core/hcd.c | 14 +++++--
>  drivers/usb/core/hub.c | 29 +++++++++++++  drivers/usb/core/phy.c | 94
> ++++++++++++++++++++++++++++++++++++++++++
>  drivers/usb/core/phy.h |  3 ++
>  4 files changed, 136 insertions(+), 4 deletions(-)
> 
> diff --git a/drivers/usb/core/hcd.c b/drivers/usb/core/hcd.c index
> 12b6dfeaf658..992284461ad8 100644
> --- a/drivers/usb/core/hcd.c
> +++ b/drivers/usb/core/hcd.c
> @@ -2794,10 +2794,16 @@ int usb_add_hcd(struct usb_hcd *hcd,
>  	struct usb_device *rhdev;
>  	struct usb_hcd *shared_hcd;
> 
> -	if (!hcd->skip_phy_initialization && usb_hcd_is_primary_hcd(hcd)) {
> -		hcd->phy_roothub = usb_phy_roothub_alloc(hcd->self.sysdev);
> -		if (IS_ERR(hcd->phy_roothub))
> -			return PTR_ERR(hcd->phy_roothub);
> +	if (!hcd->skip_phy_initialization) {
> +		if (usb_hcd_is_primary_hcd(hcd)) {
> +			hcd->phy_roothub = usb_phy_roothub_alloc(hcd->self.sysdev);
> +			if (IS_ERR(hcd->phy_roothub))
> +				return PTR_ERR(hcd->phy_roothub);
> +		} else {
> +			hcd->phy_roothub =
> usb_phy_roothub_alloc_usb3_phy(hcd->self.sysdev);
> +			if (IS_ERR(hcd->phy_roothub))
> +				return PTR_ERR(hcd->phy_roothub);
> +		}
> 
>  		retval = usb_phy_roothub_init(hcd->phy_roothub);
>  		if (retval)
> diff --git a/drivers/usb/core/hub.c b/drivers/usb/core/hub.c index
> 87480a6e6d93..65c0454ee70a 100644
> --- a/drivers/usb/core/hub.c
> +++ b/drivers/usb/core/hub.c
> @@ -37,6 +37,7 @@
>  #include <asm/byteorder.h>
> 
>  #include "hub.h"
> +#include "phy.h"
>  #include "otg_productlist.h"
> 
>  #define USB_VENDOR_GENESYS_LOGIC		0x05e3
> @@ -622,6 +623,34 @@ static int hub_ext_port_status(struct usb_hub *hub,
> int port1, int type,
>  		ret = 0;
>  	}
>  	mutex_unlock(&hub->status_mutex);
> +
> +	/*
> +	 * There is no need to lock status_mutex here, because status_mutex
> +	 * protects hub->status, and the phy driver only checks the port
> +	 * status without changing the status.
> +	 */
> +	if (!ret) {
> +		struct usb_device *hdev = hub->hdev;
> +
> +		/*
> +		 * Only roothub will be notified of connection changes,
> +		 * since the USB PHY only cares about changes at the next
> +		 * level.
> +		 */
> +		if (is_root_hub(hdev)) {
> +			struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
> +			bool connect;
> +			bool connect_change;
> +
> +			connect_change = *change &
> USB_PORT_STAT_C_CONNECTION;
> +			connect = *status & USB_PORT_STAT_CONNECTION;
> +			if (connect_change && connect)
> +				usb_phy_roothub_notify_connect(hcd->phy_roothub, port1
> - 1);
> +			else if (connect_change)
> +				usb_phy_roothub_notify_disconnect(hcd->phy_roothub,
> port1 - 1);
> +		}
> +	}
> +
>  	return ret;
>  }
> 
> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c index
> fb1588e7c282..26585fc1ec32 100644
> --- a/drivers/usb/core/phy.c
> +++ b/drivers/usb/core/phy.c
> @@ -19,6 +19,29 @@ struct usb_phy_roothub {
>  	struct list_head	list;
>  };
> 
> +static int usb_phy_roothub_add_phy_by_name(struct device *dev, const char
> *name,
> +					   struct list_head *list)
> +{
> +	struct usb_phy_roothub *roothub_entry;
> +	struct phy *phy;
> +
> +	phy = devm_of_phy_get(dev, dev->of_node, name);
> +	if (IS_ERR(phy))
> +		return PTR_ERR(phy);
> +
> +	roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
> +	if (!roothub_entry)
> +		return -ENOMEM;
> +
> +	INIT_LIST_HEAD(&roothub_entry->list);
> +
> +	roothub_entry->phy = phy;
> +
> +	list_add_tail(&roothub_entry->list, list);
> +
> +	return 0;
> +}
> +
>  static int usb_phy_roothub_add_phy(struct device *dev, int index,
>  				   struct list_head *list)
>  {
> @@ -65,6 +88,9 @@ struct usb_phy_roothub *usb_phy_roothub_alloc(struct
> device *dev)
> 
>  	INIT_LIST_HEAD(&phy_roothub->list);
> 
> +	if (!usb_phy_roothub_add_phy_by_name(dev, "usb2-phy",
> &phy_roothub->list))
> +		return phy_roothub;
> +
>  	for (i = 0; i < num_phys; i++) {
>  		err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
>  		if (err)
> @@ -75,6 +101,32 @@ struct usb_phy_roothub *usb_phy_roothub_alloc(struct
> device *dev)  }  EXPORT_SYMBOL_GPL(usb_phy_roothub_alloc);
> 
> +struct usb_phy_roothub *usb_phy_roothub_alloc_usb3_phy(struct device
> +*dev) {
> +	struct usb_phy_roothub *phy_roothub;
> +	int num_phys;
> +
> +	if (!IS_ENABLED(CONFIG_GENERIC_PHY))
> +		return NULL;
> +
> +	num_phys = of_count_phandle_with_args(dev->of_node, "phys",
> +					      "#phy-cells");
> +	if (num_phys <= 0)
> +		return NULL;
> +
> +	phy_roothub = devm_kzalloc(dev, sizeof(*phy_roothub), GFP_KERNEL);
> +	if (!phy_roothub)
> +		return ERR_PTR(-ENOMEM);
> +
> +	INIT_LIST_HEAD(&phy_roothub->list);
> +
> +	if (!usb_phy_roothub_add_phy_by_name(dev, "usb3-phy",
> &phy_roothub->list))
> +		return phy_roothub;
> +
> +	return NULL;
> +}
> +EXPORT_SYMBOL_GPL(usb_phy_roothub_alloc_usb3_phy);
> +
>  int usb_phy_roothub_init(struct usb_phy_roothub *phy_roothub)  {
>  	struct usb_phy_roothub *roothub_entry; @@ -172,6 +224,48 @@ int
> usb_phy_roothub_calibrate(struct usb_phy_roothub *phy_roothub)  }
> EXPORT_SYMBOL_GPL(usb_phy_roothub_calibrate);
> 
> +int usb_phy_roothub_notify_connect(struct usb_phy_roothub *phy_roothub,
> +int port) {
> +	struct usb_phy_roothub *roothub_entry;
> +	struct list_head *head;
> +	int err;
> +
> +	if (!phy_roothub)
> +		return 0;
> +
> +	head = &phy_roothub->list;
> +
> +	list_for_each_entry(roothub_entry, head, list) {
> +		err = phy_notify_connect(roothub_entry->phy, port);
> +		if (err)
> +			return err;
> +	}
> +
> +	return 0;
> +}
> +EXPORT_SYMBOL_GPL(usb_phy_roothub_notify_connect);
> +
> +int usb_phy_roothub_notify_disconnect(struct usb_phy_roothub
> +*phy_roothub, int port) {
> +	struct usb_phy_roothub *roothub_entry;
> +	struct list_head *head;
> +	int err;
> +
> +	if (!phy_roothub)
> +		return 0;
> +
> +	head = &phy_roothub->list;
> +
> +	list_for_each_entry(roothub_entry, head, list) {
> +		err = phy_notify_disconnect(roothub_entry->phy, port);
> +		if (err)
> +			return err;
> +	}
> +
> +	return 0;
> +}
> +EXPORT_SYMBOL_GPL(usb_phy_roothub_notify_disconnect);
> +
>  int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)  {
>  	struct usb_phy_roothub *roothub_entry; diff --git a/drivers/usb/core/phy.h
> b/drivers/usb/core/phy.h index 20a267cd986b..88b49c0ea6b5 100644
> --- a/drivers/usb/core/phy.h
> +++ b/drivers/usb/core/phy.h
> @@ -12,6 +12,7 @@ struct device;
>  struct usb_phy_roothub;
> 
>  struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev);
> +struct usb_phy_roothub *usb_phy_roothub_alloc_usb3_phy(struct device
> +*dev);
> 
>  int usb_phy_roothub_init(struct usb_phy_roothub *phy_roothub);  int
> usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub); @@ -19,6 +20,8
> @@ int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub);  int
> usb_phy_roothub_set_mode(struct usb_phy_roothub *phy_roothub,
>  			     enum phy_mode mode);
>  int usb_phy_roothub_calibrate(struct usb_phy_roothub *phy_roothub);
> +int usb_phy_roothub_notify_connect(struct usb_phy_roothub *phy_roothub,
> +int port); int usb_phy_roothub_notify_disconnect(struct usb_phy_roothub
> +*phy_roothub, int port);
>  int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub);
> void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub);
> 
> --
> 2.34.1


  reply	other threads:[~2023-12-07  4:51 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-10  5:45 [PATCH v3 1/4] phy: core: add notify_connect and notify_disconnect callback Stanley Chang
2023-11-10  5:45 ` [PATCH v3 2/4] phy: realtek: usb: add new driver for the Realtek RTD SoC USB 2.0 PHY Stanley Chang
2023-11-10  7:47   ` Johan Hovold
2023-11-10 10:32     ` Stanley Chang[昌育德]
2023-11-10  5:45 ` [PATCH v3 3/4] phy: realtek: usb: add new driver for the Realtek RTD SoC USB 3.0 PHY Stanley Chang
2023-11-10  5:45 ` [PATCH v3 4/4] usb: core: add phy notify connect and disconnect Stanley Chang
2023-12-07  4:50   ` Stanley Chang[昌育德] [this message]
2023-12-07  7:10     ` Greg Kroah-Hartman
2023-12-07  7:16       ` Stanley Chang[昌育德]
2023-11-20  2:16 ` [PATCH v3 1/4] phy: core: add notify_connect and notify_disconnect callback Stanley Chang[昌育德]
2023-11-21  5:47 ` Kishon Vijay Abraham I
2023-11-21  6:54   ` Stanley Chang[昌育德]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4736a6933f3546c48f24a635c15a5e20@realtek.com \
    --to=stanley_chang@realtek.com \
    --cc=geert+renesas@glider.be \
    --cc=gregkh@linuxfoundation.org \
    --cc=heikki.krogerus@linux.intel.com \
    --cc=johan@kernel.org \
    --cc=kishon@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-phy@lists.infradead.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=ricardo.canuelo@collabora.com \
    --cc=robh@kernel.org \
    --cc=royluo@google.com \
    --cc=ruanjinjie@huawei.com \
    --cc=stern@rowland.harvard.edu \
    --cc=vkoul@kernel.org \
    --cc=yangyingliang@huawei.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®