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> 5d7078df6SFelipe Balbi #include <linux/usb/usb_phy_generic.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> 11*59f042f6SBin Liu #include <linux/usb/of.h> 123bb869c8SSebastian Andrzej Siewior 135306661eSBin Liu #include "phy-am335x-control.h" 143bb869c8SSebastian Andrzej Siewior #include "phy-generic.h" 153bb869c8SSebastian Andrzej Siewior 163bb869c8SSebastian Andrzej Siewior struct am335x_phy { 174525beebSFelipe Balbi struct usb_phy_generic usb_phy_gen; 183bb869c8SSebastian Andrzej Siewior struct phy_control *phy_ctrl; 193bb869c8SSebastian Andrzej Siewior int id; 20*59f042f6SBin Liu enum usb_dr_mode dr_mode; 213bb869c8SSebastian Andrzej Siewior }; 223bb869c8SSebastian Andrzej Siewior 233bb869c8SSebastian Andrzej Siewior static int am335x_init(struct usb_phy *phy) 243bb869c8SSebastian Andrzej Siewior { 253bb869c8SSebastian Andrzej Siewior struct am335x_phy *am_phy = dev_get_drvdata(phy->dev); 263bb869c8SSebastian Andrzej Siewior 27*59f042f6SBin Liu phy_ctrl_power(am_phy->phy_ctrl, am_phy->id, am_phy->dr_mode, true); 283bb869c8SSebastian Andrzej Siewior return 0; 293bb869c8SSebastian Andrzej Siewior } 303bb869c8SSebastian Andrzej Siewior 313bb869c8SSebastian Andrzej Siewior static void am335x_shutdown(struct usb_phy *phy) 323bb869c8SSebastian Andrzej Siewior { 333bb869c8SSebastian Andrzej Siewior struct am335x_phy *am_phy = dev_get_drvdata(phy->dev); 343bb869c8SSebastian Andrzej Siewior 35*59f042f6SBin Liu phy_ctrl_power(am_phy->phy_ctrl, am_phy->id, am_phy->dr_mode, false); 363bb869c8SSebastian Andrzej Siewior } 373bb869c8SSebastian Andrzej Siewior 383bb869c8SSebastian Andrzej Siewior static int am335x_phy_probe(struct platform_device *pdev) 393bb869c8SSebastian Andrzej Siewior { 403bb869c8SSebastian Andrzej Siewior struct am335x_phy *am_phy; 413bb869c8SSebastian Andrzej Siewior struct device *dev = &pdev->dev; 423bb869c8SSebastian Andrzej Siewior int ret; 433bb869c8SSebastian Andrzej Siewior 443bb869c8SSebastian Andrzej Siewior am_phy = devm_kzalloc(dev, sizeof(*am_phy), GFP_KERNEL); 453bb869c8SSebastian Andrzej Siewior if (!am_phy) 463bb869c8SSebastian Andrzej Siewior return -ENOMEM; 473bb869c8SSebastian Andrzej Siewior 483bb869c8SSebastian Andrzej Siewior am_phy->phy_ctrl = am335x_get_phy_control(dev); 493bb869c8SSebastian Andrzej Siewior if (!am_phy->phy_ctrl) 503bb869c8SSebastian Andrzej Siewior return -EPROBE_DEFER; 51*59f042f6SBin Liu 523bb869c8SSebastian Andrzej Siewior am_phy->id = of_alias_get_id(pdev->dev.of_node, "phy"); 533bb869c8SSebastian Andrzej Siewior if (am_phy->id < 0) { 543bb869c8SSebastian Andrzej Siewior dev_err(&pdev->dev, "Missing PHY id: %d\n", am_phy->id); 553bb869c8SSebastian Andrzej Siewior return am_phy->id; 563bb869c8SSebastian Andrzej Siewior } 573bb869c8SSebastian Andrzej Siewior 58*59f042f6SBin Liu am_phy->dr_mode = of_usb_get_dr_mode_by_phy(pdev->dev.of_node); 59*59f042f6SBin Liu 60af9f51c5SFelipe Balbi ret = usb_phy_gen_create_phy(dev, &am_phy->usb_phy_gen, NULL); 613bb869c8SSebastian Andrzej Siewior if (ret) 623bb869c8SSebastian Andrzej Siewior return ret; 633bb869c8SSebastian Andrzej Siewior 643bb869c8SSebastian Andrzej Siewior ret = usb_add_phy_dev(&am_phy->usb_phy_gen.phy); 653bb869c8SSebastian Andrzej Siewior if (ret) 664d175f34SMark Brown return ret; 673bb869c8SSebastian Andrzej Siewior am_phy->usb_phy_gen.phy.init = am335x_init; 683bb869c8SSebastian Andrzej Siewior am_phy->usb_phy_gen.phy.shutdown = am335x_shutdown; 693bb869c8SSebastian Andrzej Siewior 703bb869c8SSebastian Andrzej Siewior platform_set_drvdata(pdev, am_phy); 712fc711d7SGeorge Cherian device_init_wakeup(dev, true); 722fc711d7SGeorge Cherian 732fc711d7SGeorge Cherian /* 742fc711d7SGeorge Cherian * If we leave PHY wakeup enabled then AM33XX wakes up 752fc711d7SGeorge Cherian * immediately from DS0. To avoid this we mark dev->power.can_wakeup 762fc711d7SGeorge Cherian * to false. The same is checked in suspend routine to decide 772fc711d7SGeorge Cherian * on whether to enable PHY wakeup or not. 782fc711d7SGeorge Cherian * PHY wakeup works fine in standby mode, there by allowing us to 792fc711d7SGeorge Cherian * handle remote wakeup, wakeup on disconnect and connect. 802fc711d7SGeorge Cherian */ 812fc711d7SGeorge Cherian 822fc711d7SGeorge Cherian device_set_wakeup_enable(dev, false); 83*59f042f6SBin Liu phy_ctrl_power(am_phy->phy_ctrl, am_phy->id, am_phy->dr_mode, false); 844d175f34SMark Brown 853bb869c8SSebastian Andrzej Siewior return 0; 863bb869c8SSebastian Andrzej Siewior } 873bb869c8SSebastian Andrzej Siewior 883bb869c8SSebastian Andrzej Siewior static int am335x_phy_remove(struct platform_device *pdev) 893bb869c8SSebastian Andrzej Siewior { 903bb869c8SSebastian Andrzej Siewior struct am335x_phy *am_phy = platform_get_drvdata(pdev); 913bb869c8SSebastian Andrzej Siewior 923bb869c8SSebastian Andrzej Siewior usb_remove_phy(&am_phy->usb_phy_gen.phy); 933bb869c8SSebastian Andrzej Siewior return 0; 943bb869c8SSebastian Andrzej Siewior } 953bb869c8SSebastian Andrzej Siewior 962fc711d7SGeorge Cherian #ifdef CONFIG_PM_SLEEP 972fc711d7SGeorge Cherian static int am335x_phy_suspend(struct device *dev) 98a1222639SSebastian Andrzej Siewior { 99a1222639SSebastian Andrzej Siewior struct platform_device *pdev = to_platform_device(dev); 100a1222639SSebastian Andrzej Siewior struct am335x_phy *am_phy = platform_get_drvdata(pdev); 101a1222639SSebastian Andrzej Siewior 1022fc711d7SGeorge Cherian /* 1032fc711d7SGeorge Cherian * Enable phy wakeup only if dev->power.can_wakeup is true. 1042fc711d7SGeorge Cherian * Make sure to enable wakeup to support remote wakeup in 1052fc711d7SGeorge Cherian * standby mode ( same is not supported in OFF(DS0) mode). 1062fc711d7SGeorge Cherian * Enable it by doing 1072fc711d7SGeorge Cherian * echo enabled > /sys/bus/platform/devices/<usb-phy-id>/power/wakeup 1082fc711d7SGeorge Cherian */ 1092fc711d7SGeorge Cherian 110a1222639SSebastian Andrzej Siewior if (device_may_wakeup(dev)) 111a1222639SSebastian Andrzej Siewior phy_ctrl_wkup(am_phy->phy_ctrl, am_phy->id, true); 1122fc711d7SGeorge Cherian 113*59f042f6SBin Liu phy_ctrl_power(am_phy->phy_ctrl, am_phy->id, am_phy->dr_mode, false); 1142fc711d7SGeorge Cherian 115a1222639SSebastian Andrzej Siewior return 0; 116a1222639SSebastian Andrzej Siewior } 117a1222639SSebastian Andrzej Siewior 1182fc711d7SGeorge Cherian static int am335x_phy_resume(struct device *dev) 119a1222639SSebastian Andrzej Siewior { 120a1222639SSebastian Andrzej Siewior struct platform_device *pdev = to_platform_device(dev); 121a1222639SSebastian Andrzej Siewior struct am335x_phy *am_phy = platform_get_drvdata(pdev); 122a1222639SSebastian Andrzej Siewior 123*59f042f6SBin Liu phy_ctrl_power(am_phy->phy_ctrl, am_phy->id, am_phy->dr_mode, true); 1242fc711d7SGeorge Cherian 125a1222639SSebastian Andrzej Siewior if (device_may_wakeup(dev)) 126a1222639SSebastian Andrzej Siewior phy_ctrl_wkup(am_phy->phy_ctrl, am_phy->id, false); 1272fc711d7SGeorge Cherian 128a1222639SSebastian Andrzej Siewior return 0; 129a1222639SSebastian Andrzej Siewior } 130a1222639SSebastian Andrzej Siewior #endif 131a1222639SSebastian Andrzej Siewior 1321e32cda8SJingoo Han static SIMPLE_DEV_PM_OPS(am335x_pm_ops, am335x_phy_suspend, am335x_phy_resume); 1331e32cda8SJingoo Han 1343bb869c8SSebastian Andrzej Siewior static const struct of_device_id am335x_phy_ids[] = { 1353bb869c8SSebastian Andrzej Siewior { .compatible = "ti,am335x-usb-phy" }, 1363bb869c8SSebastian Andrzej Siewior { } 1373bb869c8SSebastian Andrzej Siewior }; 1383bb869c8SSebastian Andrzej Siewior MODULE_DEVICE_TABLE(of, am335x_phy_ids); 1393bb869c8SSebastian Andrzej Siewior 1403bb869c8SSebastian Andrzej Siewior static struct platform_driver am335x_phy_driver = { 1413bb869c8SSebastian Andrzej Siewior .probe = am335x_phy_probe, 1423bb869c8SSebastian Andrzej Siewior .remove = am335x_phy_remove, 1433bb869c8SSebastian Andrzej Siewior .driver = { 1443bb869c8SSebastian Andrzej Siewior .name = "am335x-phy-driver", 1451e32cda8SJingoo Han .pm = &am335x_pm_ops, 146b99fffcaSSachin Kamat .of_match_table = am335x_phy_ids, 1473bb869c8SSebastian Andrzej Siewior }, 1483bb869c8SSebastian Andrzej Siewior }; 1493bb869c8SSebastian Andrzej Siewior 1503bb869c8SSebastian Andrzej Siewior module_platform_driver(am335x_phy_driver); 1513bb869c8SSebastian Andrzej Siewior MODULE_LICENSE("GPL v2"); 152