13bb869c8SSebastian Andrzej Siewior #include <linux/module.h> 23bb869c8SSebastian Andrzej Siewior #include <linux/platform_device.h> 33bb869c8SSebastian Andrzej Siewior #include <linux/dma-mapping.h> 43bb869c8SSebastian Andrzej Siewior #include <linux/usb/otg.h> 53bb869c8SSebastian Andrzej Siewior #include <linux/usb/usb_phy_gen_xceiv.h> 63bb869c8SSebastian Andrzej Siewior #include <linux/slab.h> 73bb869c8SSebastian Andrzej Siewior #include <linux/clk.h> 83bb869c8SSebastian Andrzej Siewior #include <linux/regulator/consumer.h> 93bb869c8SSebastian Andrzej Siewior #include <linux/of.h> 103bb869c8SSebastian Andrzej Siewior #include <linux/of_address.h> 113bb869c8SSebastian Andrzej Siewior 123bb869c8SSebastian Andrzej Siewior #include "am35x-phy-control.h" 133bb869c8SSebastian Andrzej Siewior #include "phy-generic.h" 143bb869c8SSebastian Andrzej Siewior 153bb869c8SSebastian Andrzej Siewior struct am335x_phy { 163bb869c8SSebastian Andrzej Siewior struct usb_phy_gen_xceiv usb_phy_gen; 173bb869c8SSebastian Andrzej Siewior struct phy_control *phy_ctrl; 183bb869c8SSebastian Andrzej Siewior int id; 193bb869c8SSebastian Andrzej Siewior }; 203bb869c8SSebastian Andrzej Siewior 213bb869c8SSebastian Andrzej Siewior static int am335x_init(struct usb_phy *phy) 223bb869c8SSebastian Andrzej Siewior { 233bb869c8SSebastian Andrzej Siewior struct am335x_phy *am_phy = dev_get_drvdata(phy->dev); 243bb869c8SSebastian Andrzej Siewior 253bb869c8SSebastian Andrzej Siewior phy_ctrl_power(am_phy->phy_ctrl, am_phy->id, true); 263bb869c8SSebastian Andrzej Siewior return 0; 273bb869c8SSebastian Andrzej Siewior } 283bb869c8SSebastian Andrzej Siewior 293bb869c8SSebastian Andrzej Siewior static void am335x_shutdown(struct usb_phy *phy) 303bb869c8SSebastian Andrzej Siewior { 313bb869c8SSebastian Andrzej Siewior struct am335x_phy *am_phy = dev_get_drvdata(phy->dev); 323bb869c8SSebastian Andrzej Siewior 333bb869c8SSebastian Andrzej Siewior phy_ctrl_power(am_phy->phy_ctrl, am_phy->id, false); 343bb869c8SSebastian Andrzej Siewior } 353bb869c8SSebastian Andrzej Siewior 363bb869c8SSebastian Andrzej Siewior static int am335x_phy_probe(struct platform_device *pdev) 373bb869c8SSebastian Andrzej Siewior { 383bb869c8SSebastian Andrzej Siewior struct am335x_phy *am_phy; 393bb869c8SSebastian Andrzej Siewior struct device *dev = &pdev->dev; 403bb869c8SSebastian Andrzej Siewior int ret; 413bb869c8SSebastian Andrzej Siewior 423bb869c8SSebastian Andrzej Siewior am_phy = devm_kzalloc(dev, sizeof(*am_phy), GFP_KERNEL); 433bb869c8SSebastian Andrzej Siewior if (!am_phy) 443bb869c8SSebastian Andrzej Siewior return -ENOMEM; 453bb869c8SSebastian Andrzej Siewior 463bb869c8SSebastian Andrzej Siewior am_phy->phy_ctrl = am335x_get_phy_control(dev); 473bb869c8SSebastian Andrzej Siewior if (!am_phy->phy_ctrl) 483bb869c8SSebastian Andrzej Siewior return -EPROBE_DEFER; 493bb869c8SSebastian Andrzej Siewior am_phy->id = of_alias_get_id(pdev->dev.of_node, "phy"); 503bb869c8SSebastian Andrzej Siewior if (am_phy->id < 0) { 513bb869c8SSebastian Andrzej Siewior dev_err(&pdev->dev, "Missing PHY id: %d\n", am_phy->id); 523bb869c8SSebastian Andrzej Siewior return am_phy->id; 533bb869c8SSebastian Andrzej Siewior } 543bb869c8SSebastian Andrzej Siewior 55af9f51c5SFelipe Balbi ret = usb_phy_gen_create_phy(dev, &am_phy->usb_phy_gen, NULL); 563bb869c8SSebastian Andrzej Siewior if (ret) 573bb869c8SSebastian Andrzej Siewior return ret; 583bb869c8SSebastian Andrzej Siewior 593bb869c8SSebastian Andrzej Siewior ret = usb_add_phy_dev(&am_phy->usb_phy_gen.phy); 603bb869c8SSebastian Andrzej Siewior if (ret) 614d175f34SMark Brown return ret; 623bb869c8SSebastian Andrzej Siewior am_phy->usb_phy_gen.phy.init = am335x_init; 633bb869c8SSebastian Andrzej Siewior am_phy->usb_phy_gen.phy.shutdown = am335x_shutdown; 643bb869c8SSebastian Andrzej Siewior 653bb869c8SSebastian Andrzej Siewior platform_set_drvdata(pdev, am_phy); 66*2fc711d7SGeorge Cherian device_init_wakeup(dev, true); 67*2fc711d7SGeorge Cherian 68*2fc711d7SGeorge Cherian /* 69*2fc711d7SGeorge Cherian * If we leave PHY wakeup enabled then AM33XX wakes up 70*2fc711d7SGeorge Cherian * immediately from DS0. To avoid this we mark dev->power.can_wakeup 71*2fc711d7SGeorge Cherian * to false. The same is checked in suspend routine to decide 72*2fc711d7SGeorge Cherian * on whether to enable PHY wakeup or not. 73*2fc711d7SGeorge Cherian * PHY wakeup works fine in standby mode, there by allowing us to 74*2fc711d7SGeorge Cherian * handle remote wakeup, wakeup on disconnect and connect. 75*2fc711d7SGeorge Cherian */ 76*2fc711d7SGeorge Cherian 77*2fc711d7SGeorge Cherian device_set_wakeup_enable(dev, false); 78*2fc711d7SGeorge Cherian phy_ctrl_power(am_phy->phy_ctrl, am_phy->id, false); 794d175f34SMark Brown 803bb869c8SSebastian Andrzej Siewior return 0; 813bb869c8SSebastian Andrzej Siewior } 823bb869c8SSebastian Andrzej Siewior 833bb869c8SSebastian Andrzej Siewior static int am335x_phy_remove(struct platform_device *pdev) 843bb869c8SSebastian Andrzej Siewior { 853bb869c8SSebastian Andrzej Siewior struct am335x_phy *am_phy = platform_get_drvdata(pdev); 863bb869c8SSebastian Andrzej Siewior 873bb869c8SSebastian Andrzej Siewior usb_remove_phy(&am_phy->usb_phy_gen.phy); 883bb869c8SSebastian Andrzej Siewior return 0; 893bb869c8SSebastian Andrzej Siewior } 903bb869c8SSebastian Andrzej Siewior 91*2fc711d7SGeorge Cherian #ifdef CONFIG_PM_SLEEP 92*2fc711d7SGeorge Cherian static int am335x_phy_suspend(struct device *dev) 93a1222639SSebastian Andrzej Siewior { 94a1222639SSebastian Andrzej Siewior struct platform_device *pdev = to_platform_device(dev); 95a1222639SSebastian Andrzej Siewior struct am335x_phy *am_phy = platform_get_drvdata(pdev); 96a1222639SSebastian Andrzej Siewior 97*2fc711d7SGeorge Cherian /* 98*2fc711d7SGeorge Cherian * Enable phy wakeup only if dev->power.can_wakeup is true. 99*2fc711d7SGeorge Cherian * Make sure to enable wakeup to support remote wakeup in 100*2fc711d7SGeorge Cherian * standby mode ( same is not supported in OFF(DS0) mode). 101*2fc711d7SGeorge Cherian * Enable it by doing 102*2fc711d7SGeorge Cherian * echo enabled > /sys/bus/platform/devices/<usb-phy-id>/power/wakeup 103*2fc711d7SGeorge Cherian */ 104*2fc711d7SGeorge Cherian 105a1222639SSebastian Andrzej Siewior if (device_may_wakeup(dev)) 106a1222639SSebastian Andrzej Siewior phy_ctrl_wkup(am_phy->phy_ctrl, am_phy->id, true); 107*2fc711d7SGeorge Cherian 108a1222639SSebastian Andrzej Siewior phy_ctrl_power(am_phy->phy_ctrl, am_phy->id, false); 109*2fc711d7SGeorge Cherian 110a1222639SSebastian Andrzej Siewior return 0; 111a1222639SSebastian Andrzej Siewior } 112a1222639SSebastian Andrzej Siewior 113*2fc711d7SGeorge Cherian static int am335x_phy_resume(struct device *dev) 114a1222639SSebastian Andrzej Siewior { 115a1222639SSebastian Andrzej Siewior struct platform_device *pdev = to_platform_device(dev); 116a1222639SSebastian Andrzej Siewior struct am335x_phy *am_phy = platform_get_drvdata(pdev); 117a1222639SSebastian Andrzej Siewior 118a1222639SSebastian Andrzej Siewior phy_ctrl_power(am_phy->phy_ctrl, am_phy->id, true); 119*2fc711d7SGeorge Cherian 120a1222639SSebastian Andrzej Siewior if (device_may_wakeup(dev)) 121a1222639SSebastian Andrzej Siewior phy_ctrl_wkup(am_phy->phy_ctrl, am_phy->id, false); 122*2fc711d7SGeorge Cherian 123a1222639SSebastian Andrzej Siewior return 0; 124a1222639SSebastian Andrzej Siewior } 125a1222639SSebastian Andrzej Siewior #define DEV_PM_OPS (&am335x_pm_ops) 126a1222639SSebastian Andrzej Siewior #else 127a1222639SSebastian Andrzej Siewior #define DEV_PM_OPS NULL 128a1222639SSebastian Andrzej Siewior #endif 129a1222639SSebastian Andrzej Siewior 130*2fc711d7SGeorge Cherian 131*2fc711d7SGeorge Cherian static const struct dev_pm_ops am335x_pm_ops = { 132*2fc711d7SGeorge Cherian .suspend = am335x_phy_suspend, 133*2fc711d7SGeorge Cherian .resume = am335x_phy_resume, 134*2fc711d7SGeorge Cherian }; 135*2fc711d7SGeorge Cherian 1363bb869c8SSebastian Andrzej Siewior static const struct of_device_id am335x_phy_ids[] = { 1373bb869c8SSebastian Andrzej Siewior { .compatible = "ti,am335x-usb-phy" }, 1383bb869c8SSebastian Andrzej Siewior { } 1393bb869c8SSebastian Andrzej Siewior }; 1403bb869c8SSebastian Andrzej Siewior MODULE_DEVICE_TABLE(of, am335x_phy_ids); 1413bb869c8SSebastian Andrzej Siewior 1423bb869c8SSebastian Andrzej Siewior static struct platform_driver am335x_phy_driver = { 1433bb869c8SSebastian Andrzej Siewior .probe = am335x_phy_probe, 1443bb869c8SSebastian Andrzej Siewior .remove = am335x_phy_remove, 1453bb869c8SSebastian Andrzej Siewior .driver = { 1463bb869c8SSebastian Andrzej Siewior .name = "am335x-phy-driver", 1473bb869c8SSebastian Andrzej Siewior .owner = THIS_MODULE, 148a1222639SSebastian Andrzej Siewior .pm = DEV_PM_OPS, 149b99fffcaSSachin Kamat .of_match_table = am335x_phy_ids, 1503bb869c8SSebastian Andrzej Siewior }, 1513bb869c8SSebastian Andrzej Siewior }; 1523bb869c8SSebastian Andrzej Siewior 1533bb869c8SSebastian Andrzej Siewior module_platform_driver(am335x_phy_driver); 1543bb869c8SSebastian Andrzej Siewior MODULE_LICENSE("GPL v2"); 155