OpenCloudOS-Kernel/drivers/usb/core/phy.c

198 lines
4.2 KiB
C
Raw Normal View History

// SPDX-License-Identifier: GPL-2.0+
/*
* A wrapper for multiple PHYs which passes all phy_* function calls to
* multiple (actual) PHY devices. This is comes handy when initializing
* all PHYs on a HCD and to keep them all in the same state.
*
* Copyright (C) 2018 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
*/
#include <linux/device.h>
#include <linux/list.h>
#include <linux/phy/phy.h>
#include <linux/of.h>
#include "phy.h"
struct usb_phy_roothub {
struct phy *phy;
struct list_head list;
};
static int usb_phy_roothub_add_phy(struct device *dev, int index,
struct list_head *list)
{
struct usb_phy_roothub *roothub_entry;
struct phy *phy = devm_of_phy_get_by_index(dev, dev->of_node, index);
if (IS_ERR_OR_NULL(phy)) {
if (!phy || PTR_ERR(phy) == -ENODEV)
return 0;
else
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;
}
struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev)
{
struct usb_phy_roothub *phy_roothub;
int i, num_phys, err;
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);
for (i = 0; i < num_phys; i++) {
err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
if (err)
return ERR_PTR(err);
}
return phy_roothub;
}
EXPORT_SYMBOL_GPL(usb_phy_roothub_alloc);
int usb_phy_roothub_init(struct usb_phy_roothub *phy_roothub)
{
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_init(roothub_entry->phy);
if (err)
goto err_exit_phys;
}
return 0;
err_exit_phys:
list_for_each_entry_continue_reverse(roothub_entry, head, list)
phy_exit(roothub_entry->phy);
return err;
}
EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
{
struct usb_phy_roothub *roothub_entry;
struct list_head *head;
int err, ret = 0;
if (!phy_roothub)
return 0;
head = &phy_roothub->list;
list_for_each_entry(roothub_entry, head, list) {
err = phy_exit(roothub_entry->phy);
if (err)
ret = err;
}
return ret;
}
EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
{
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_power_on(roothub_entry->phy);
if (err)
goto err_out;
}
return 0;
err_out:
list_for_each_entry_continue_reverse(roothub_entry, head, list)
phy_power_off(roothub_entry->phy);
return err;
}
EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
{
struct usb_phy_roothub *roothub_entry;
if (!phy_roothub)
return;
list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
phy_power_off(roothub_entry->phy);
}
EXPORT_SYMBOL_GPL(usb_phy_roothub_power_off);
usb: core: use phy_exit during suspend if wake up is not supported If the USB controller can wake up the system (which is the case for example with the Mediatek USB3 IP) then we must not call phy_exit during suspend to ensure that the USB controller doesn't have to re-enumerate the devices during resume. However, if the USB controller cannot wake up the system (which is the case for example on various TI platforms using a dwc3 controller) then we must call phy_exit during suspend. Otherwise the PHY driver keeps the clocks enabled, which prevents the system from reaching the lowest power levels in the suspend state. Solve this by introducing two new functions in the PHY wrapper which are dedicated to the suspend and resume handling. If the controller can wake up the system the new usb_phy_roothub_suspend function will simply call usb_phy_roothub_power_off. However, if wake up is not supported by the controller it will also call usb_phy_roothub_exit. The also new usb_phy_roothub_resume function takes care of calling usb_phy_roothub_init (if the controller can't wake up the system) in addition to usb_phy_roothub_power_on. Fixes: 07dbff0ddbd86c ("usb: core: add a wrapper for the USB PHYs on the HCD") Fixes: 178a0bce05cbc1 ("usb: core: hcd: integrate the PHY wrapper into the HCD core") Reported-by: Roger Quadros <rogerq@ti.com> Suggested-by: Roger Quadros <rogerq@ti.com> Suggested-by: Chunfeng Yun <chunfeng.yun@mediatek.com> Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com> Tested-by: Chunfeng Yun <chunfeng.yun@mediatek.com> Reviewed-by: Roger Quadros <rogerq@ti.com> Tested-by: Keerthy <j-keerthy@ti.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2018-04-19 03:39:48 +08:00
int usb_phy_roothub_suspend(struct device *controller_dev,
struct usb_phy_roothub *phy_roothub)
{
usb_phy_roothub_power_off(phy_roothub);
/* keep the PHYs initialized so the device can wake up the system */
if (device_may_wakeup(controller_dev))
return 0;
return usb_phy_roothub_exit(phy_roothub);
}
EXPORT_SYMBOL_GPL(usb_phy_roothub_suspend);
int usb_phy_roothub_resume(struct device *controller_dev,
struct usb_phy_roothub *phy_roothub)
{
int err;
/* if the device can't wake up the system _exit was called */
if (!device_may_wakeup(controller_dev)) {
err = usb_phy_roothub_init(phy_roothub);
if (err)
return err;
}
err = usb_phy_roothub_power_on(phy_roothub);
/* undo _init if _power_on failed */
if (err && !device_may_wakeup(controller_dev))
usb_phy_roothub_exit(phy_roothub);
return err;
}
EXPORT_SYMBOL_GPL(usb_phy_roothub_resume);