162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+ 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * A wrapper for multiple PHYs which passes all phy_* function calls to 462306a36Sopenharmony_ci * multiple (actual) PHY devices. This is comes handy when initializing 562306a36Sopenharmony_ci * all PHYs on a HCD and to keep them all in the same state. 662306a36Sopenharmony_ci * 762306a36Sopenharmony_ci * Copyright (C) 2018 Martin Blumenstingl <martin.blumenstingl@googlemail.com> 862306a36Sopenharmony_ci */ 962306a36Sopenharmony_ci 1062306a36Sopenharmony_ci#include <linux/device.h> 1162306a36Sopenharmony_ci#include <linux/list.h> 1262306a36Sopenharmony_ci#include <linux/phy/phy.h> 1362306a36Sopenharmony_ci#include <linux/of.h> 1462306a36Sopenharmony_ci 1562306a36Sopenharmony_ci#include "phy.h" 1662306a36Sopenharmony_ci 1762306a36Sopenharmony_cistruct usb_phy_roothub { 1862306a36Sopenharmony_ci struct phy *phy; 1962306a36Sopenharmony_ci struct list_head list; 2062306a36Sopenharmony_ci}; 2162306a36Sopenharmony_ci 2262306a36Sopenharmony_cistatic int usb_phy_roothub_add_phy(struct device *dev, int index, 2362306a36Sopenharmony_ci struct list_head *list) 2462306a36Sopenharmony_ci{ 2562306a36Sopenharmony_ci struct usb_phy_roothub *roothub_entry; 2662306a36Sopenharmony_ci struct phy *phy; 2762306a36Sopenharmony_ci 2862306a36Sopenharmony_ci phy = devm_of_phy_get_by_index(dev, dev->of_node, index); 2962306a36Sopenharmony_ci if (IS_ERR(phy)) { 3062306a36Sopenharmony_ci if (PTR_ERR(phy) == -ENODEV) 3162306a36Sopenharmony_ci return 0; 3262306a36Sopenharmony_ci else 3362306a36Sopenharmony_ci return PTR_ERR(phy); 3462306a36Sopenharmony_ci } 3562306a36Sopenharmony_ci 3662306a36Sopenharmony_ci roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL); 3762306a36Sopenharmony_ci if (!roothub_entry) 3862306a36Sopenharmony_ci return -ENOMEM; 3962306a36Sopenharmony_ci 4062306a36Sopenharmony_ci INIT_LIST_HEAD(&roothub_entry->list); 4162306a36Sopenharmony_ci 4262306a36Sopenharmony_ci roothub_entry->phy = phy; 4362306a36Sopenharmony_ci 4462306a36Sopenharmony_ci list_add_tail(&roothub_entry->list, list); 4562306a36Sopenharmony_ci 4662306a36Sopenharmony_ci return 0; 4762306a36Sopenharmony_ci} 4862306a36Sopenharmony_ci 4962306a36Sopenharmony_cistruct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev) 5062306a36Sopenharmony_ci{ 5162306a36Sopenharmony_ci struct usb_phy_roothub *phy_roothub; 5262306a36Sopenharmony_ci int i, num_phys, err; 5362306a36Sopenharmony_ci 5462306a36Sopenharmony_ci if (!IS_ENABLED(CONFIG_GENERIC_PHY)) 5562306a36Sopenharmony_ci return NULL; 5662306a36Sopenharmony_ci 5762306a36Sopenharmony_ci num_phys = of_count_phandle_with_args(dev->of_node, "phys", 5862306a36Sopenharmony_ci "#phy-cells"); 5962306a36Sopenharmony_ci if (num_phys <= 0) 6062306a36Sopenharmony_ci return NULL; 6162306a36Sopenharmony_ci 6262306a36Sopenharmony_ci phy_roothub = devm_kzalloc(dev, sizeof(*phy_roothub), GFP_KERNEL); 6362306a36Sopenharmony_ci if (!phy_roothub) 6462306a36Sopenharmony_ci return ERR_PTR(-ENOMEM); 6562306a36Sopenharmony_ci 6662306a36Sopenharmony_ci INIT_LIST_HEAD(&phy_roothub->list); 6762306a36Sopenharmony_ci 6862306a36Sopenharmony_ci for (i = 0; i < num_phys; i++) { 6962306a36Sopenharmony_ci err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list); 7062306a36Sopenharmony_ci if (err) 7162306a36Sopenharmony_ci return ERR_PTR(err); 7262306a36Sopenharmony_ci } 7362306a36Sopenharmony_ci 7462306a36Sopenharmony_ci return phy_roothub; 7562306a36Sopenharmony_ci} 7662306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_phy_roothub_alloc); 7762306a36Sopenharmony_ci 7862306a36Sopenharmony_ciint usb_phy_roothub_init(struct usb_phy_roothub *phy_roothub) 7962306a36Sopenharmony_ci{ 8062306a36Sopenharmony_ci struct usb_phy_roothub *roothub_entry; 8162306a36Sopenharmony_ci struct list_head *head; 8262306a36Sopenharmony_ci int err; 8362306a36Sopenharmony_ci 8462306a36Sopenharmony_ci if (!phy_roothub) 8562306a36Sopenharmony_ci return 0; 8662306a36Sopenharmony_ci 8762306a36Sopenharmony_ci head = &phy_roothub->list; 8862306a36Sopenharmony_ci 8962306a36Sopenharmony_ci list_for_each_entry(roothub_entry, head, list) { 9062306a36Sopenharmony_ci err = phy_init(roothub_entry->phy); 9162306a36Sopenharmony_ci if (err) 9262306a36Sopenharmony_ci goto err_exit_phys; 9362306a36Sopenharmony_ci } 9462306a36Sopenharmony_ci 9562306a36Sopenharmony_ci return 0; 9662306a36Sopenharmony_ci 9762306a36Sopenharmony_cierr_exit_phys: 9862306a36Sopenharmony_ci list_for_each_entry_continue_reverse(roothub_entry, head, list) 9962306a36Sopenharmony_ci phy_exit(roothub_entry->phy); 10062306a36Sopenharmony_ci 10162306a36Sopenharmony_ci return err; 10262306a36Sopenharmony_ci} 10362306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_phy_roothub_init); 10462306a36Sopenharmony_ci 10562306a36Sopenharmony_ciint usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub) 10662306a36Sopenharmony_ci{ 10762306a36Sopenharmony_ci struct usb_phy_roothub *roothub_entry; 10862306a36Sopenharmony_ci struct list_head *head; 10962306a36Sopenharmony_ci int err, ret = 0; 11062306a36Sopenharmony_ci 11162306a36Sopenharmony_ci if (!phy_roothub) 11262306a36Sopenharmony_ci return 0; 11362306a36Sopenharmony_ci 11462306a36Sopenharmony_ci head = &phy_roothub->list; 11562306a36Sopenharmony_ci 11662306a36Sopenharmony_ci list_for_each_entry(roothub_entry, head, list) { 11762306a36Sopenharmony_ci err = phy_exit(roothub_entry->phy); 11862306a36Sopenharmony_ci if (err) 11962306a36Sopenharmony_ci ret = err; 12062306a36Sopenharmony_ci } 12162306a36Sopenharmony_ci 12262306a36Sopenharmony_ci return ret; 12362306a36Sopenharmony_ci} 12462306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_phy_roothub_exit); 12562306a36Sopenharmony_ci 12662306a36Sopenharmony_ciint usb_phy_roothub_set_mode(struct usb_phy_roothub *phy_roothub, 12762306a36Sopenharmony_ci enum phy_mode mode) 12862306a36Sopenharmony_ci{ 12962306a36Sopenharmony_ci struct usb_phy_roothub *roothub_entry; 13062306a36Sopenharmony_ci struct list_head *head; 13162306a36Sopenharmony_ci int err; 13262306a36Sopenharmony_ci 13362306a36Sopenharmony_ci if (!phy_roothub) 13462306a36Sopenharmony_ci return 0; 13562306a36Sopenharmony_ci 13662306a36Sopenharmony_ci head = &phy_roothub->list; 13762306a36Sopenharmony_ci 13862306a36Sopenharmony_ci list_for_each_entry(roothub_entry, head, list) { 13962306a36Sopenharmony_ci err = phy_set_mode(roothub_entry->phy, mode); 14062306a36Sopenharmony_ci if (err) 14162306a36Sopenharmony_ci goto err_out; 14262306a36Sopenharmony_ci } 14362306a36Sopenharmony_ci 14462306a36Sopenharmony_ci return 0; 14562306a36Sopenharmony_ci 14662306a36Sopenharmony_cierr_out: 14762306a36Sopenharmony_ci list_for_each_entry_continue_reverse(roothub_entry, head, list) 14862306a36Sopenharmony_ci phy_power_off(roothub_entry->phy); 14962306a36Sopenharmony_ci 15062306a36Sopenharmony_ci return err; 15162306a36Sopenharmony_ci} 15262306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_phy_roothub_set_mode); 15362306a36Sopenharmony_ci 15462306a36Sopenharmony_ciint usb_phy_roothub_calibrate(struct usb_phy_roothub *phy_roothub) 15562306a36Sopenharmony_ci{ 15662306a36Sopenharmony_ci struct usb_phy_roothub *roothub_entry; 15762306a36Sopenharmony_ci struct list_head *head; 15862306a36Sopenharmony_ci int err; 15962306a36Sopenharmony_ci 16062306a36Sopenharmony_ci if (!phy_roothub) 16162306a36Sopenharmony_ci return 0; 16262306a36Sopenharmony_ci 16362306a36Sopenharmony_ci head = &phy_roothub->list; 16462306a36Sopenharmony_ci 16562306a36Sopenharmony_ci list_for_each_entry(roothub_entry, head, list) { 16662306a36Sopenharmony_ci err = phy_calibrate(roothub_entry->phy); 16762306a36Sopenharmony_ci if (err) 16862306a36Sopenharmony_ci return err; 16962306a36Sopenharmony_ci } 17062306a36Sopenharmony_ci 17162306a36Sopenharmony_ci return 0; 17262306a36Sopenharmony_ci} 17362306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_phy_roothub_calibrate); 17462306a36Sopenharmony_ci 17562306a36Sopenharmony_ciint usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub) 17662306a36Sopenharmony_ci{ 17762306a36Sopenharmony_ci struct usb_phy_roothub *roothub_entry; 17862306a36Sopenharmony_ci struct list_head *head; 17962306a36Sopenharmony_ci int err; 18062306a36Sopenharmony_ci 18162306a36Sopenharmony_ci if (!phy_roothub) 18262306a36Sopenharmony_ci return 0; 18362306a36Sopenharmony_ci 18462306a36Sopenharmony_ci head = &phy_roothub->list; 18562306a36Sopenharmony_ci 18662306a36Sopenharmony_ci list_for_each_entry(roothub_entry, head, list) { 18762306a36Sopenharmony_ci err = phy_power_on(roothub_entry->phy); 18862306a36Sopenharmony_ci if (err) 18962306a36Sopenharmony_ci goto err_out; 19062306a36Sopenharmony_ci } 19162306a36Sopenharmony_ci 19262306a36Sopenharmony_ci return 0; 19362306a36Sopenharmony_ci 19462306a36Sopenharmony_cierr_out: 19562306a36Sopenharmony_ci list_for_each_entry_continue_reverse(roothub_entry, head, list) 19662306a36Sopenharmony_ci phy_power_off(roothub_entry->phy); 19762306a36Sopenharmony_ci 19862306a36Sopenharmony_ci return err; 19962306a36Sopenharmony_ci} 20062306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_phy_roothub_power_on); 20162306a36Sopenharmony_ci 20262306a36Sopenharmony_civoid usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub) 20362306a36Sopenharmony_ci{ 20462306a36Sopenharmony_ci struct usb_phy_roothub *roothub_entry; 20562306a36Sopenharmony_ci 20662306a36Sopenharmony_ci if (!phy_roothub) 20762306a36Sopenharmony_ci return; 20862306a36Sopenharmony_ci 20962306a36Sopenharmony_ci list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list) 21062306a36Sopenharmony_ci phy_power_off(roothub_entry->phy); 21162306a36Sopenharmony_ci} 21262306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_phy_roothub_power_off); 21362306a36Sopenharmony_ci 21462306a36Sopenharmony_ciint usb_phy_roothub_suspend(struct device *controller_dev, 21562306a36Sopenharmony_ci struct usb_phy_roothub *phy_roothub) 21662306a36Sopenharmony_ci{ 21762306a36Sopenharmony_ci usb_phy_roothub_power_off(phy_roothub); 21862306a36Sopenharmony_ci 21962306a36Sopenharmony_ci /* keep the PHYs initialized so the device can wake up the system */ 22062306a36Sopenharmony_ci if (device_may_wakeup(controller_dev)) 22162306a36Sopenharmony_ci return 0; 22262306a36Sopenharmony_ci 22362306a36Sopenharmony_ci return usb_phy_roothub_exit(phy_roothub); 22462306a36Sopenharmony_ci} 22562306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_phy_roothub_suspend); 22662306a36Sopenharmony_ci 22762306a36Sopenharmony_ciint usb_phy_roothub_resume(struct device *controller_dev, 22862306a36Sopenharmony_ci struct usb_phy_roothub *phy_roothub) 22962306a36Sopenharmony_ci{ 23062306a36Sopenharmony_ci int err; 23162306a36Sopenharmony_ci 23262306a36Sopenharmony_ci /* if the device can't wake up the system _exit was called */ 23362306a36Sopenharmony_ci if (!device_may_wakeup(controller_dev)) { 23462306a36Sopenharmony_ci err = usb_phy_roothub_init(phy_roothub); 23562306a36Sopenharmony_ci if (err) 23662306a36Sopenharmony_ci return err; 23762306a36Sopenharmony_ci } 23862306a36Sopenharmony_ci 23962306a36Sopenharmony_ci err = usb_phy_roothub_power_on(phy_roothub); 24062306a36Sopenharmony_ci 24162306a36Sopenharmony_ci /* undo _init if _power_on failed */ 24262306a36Sopenharmony_ci if (err && !device_may_wakeup(controller_dev)) 24362306a36Sopenharmony_ci usb_phy_roothub_exit(phy_roothub); 24462306a36Sopenharmony_ci 24562306a36Sopenharmony_ci return err; 24662306a36Sopenharmony_ci} 24762306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_phy_roothub_resume); 248