xref: /linux/drivers/usb/phy/phy-am335x.c (revision b99fffca7ddd46b78322757cfd77e9d2cdcdc7ad)
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 
553bb869c8SSebastian Andrzej Siewior 	ret = usb_phy_gen_create_phy(dev, &am_phy->usb_phy_gen,
563bb869c8SSebastian Andrzej Siewior 			USB_PHY_TYPE_USB2, 0, false, false);
573bb869c8SSebastian Andrzej Siewior 	if (ret)
583bb869c8SSebastian Andrzej Siewior 		return ret;
593bb869c8SSebastian Andrzej Siewior 
603bb869c8SSebastian Andrzej Siewior 	ret = usb_add_phy_dev(&am_phy->usb_phy_gen.phy);
613bb869c8SSebastian Andrzej Siewior 	if (ret)
624d175f34SMark Brown 		return ret;
633bb869c8SSebastian Andrzej Siewior 	am_phy->usb_phy_gen.phy.init = am335x_init;
643bb869c8SSebastian Andrzej Siewior 	am_phy->usb_phy_gen.phy.shutdown = am335x_shutdown;
653bb869c8SSebastian Andrzej Siewior 
663bb869c8SSebastian Andrzej Siewior 	platform_set_drvdata(pdev, am_phy);
674d175f34SMark Brown 
683bb869c8SSebastian Andrzej Siewior 	return 0;
693bb869c8SSebastian Andrzej Siewior 
703bb869c8SSebastian Andrzej Siewior 	return ret;
713bb869c8SSebastian Andrzej Siewior }
723bb869c8SSebastian Andrzej Siewior 
733bb869c8SSebastian Andrzej Siewior static int am335x_phy_remove(struct platform_device *pdev)
743bb869c8SSebastian Andrzej Siewior {
753bb869c8SSebastian Andrzej Siewior 	struct am335x_phy *am_phy = platform_get_drvdata(pdev);
763bb869c8SSebastian Andrzej Siewior 
773bb869c8SSebastian Andrzej Siewior 	usb_remove_phy(&am_phy->usb_phy_gen.phy);
783bb869c8SSebastian Andrzej Siewior 	return 0;
793bb869c8SSebastian Andrzej Siewior }
803bb869c8SSebastian Andrzej Siewior 
81a1222639SSebastian Andrzej Siewior #ifdef CONFIG_PM_RUNTIME
82a1222639SSebastian Andrzej Siewior 
83a1222639SSebastian Andrzej Siewior static int am335x_phy_runtime_suspend(struct device *dev)
84a1222639SSebastian Andrzej Siewior {
85a1222639SSebastian Andrzej Siewior 	struct platform_device	*pdev = to_platform_device(dev);
86a1222639SSebastian Andrzej Siewior 	struct am335x_phy *am_phy = platform_get_drvdata(pdev);
87a1222639SSebastian Andrzej Siewior 
88a1222639SSebastian Andrzej Siewior 	if (device_may_wakeup(dev))
89a1222639SSebastian Andrzej Siewior 		phy_ctrl_wkup(am_phy->phy_ctrl, am_phy->id, true);
90a1222639SSebastian Andrzej Siewior 	phy_ctrl_power(am_phy->phy_ctrl, am_phy->id, false);
91a1222639SSebastian Andrzej Siewior 	return 0;
92a1222639SSebastian Andrzej Siewior }
93a1222639SSebastian Andrzej Siewior 
94a1222639SSebastian Andrzej Siewior static int am335x_phy_runtime_resume(struct device *dev)
95a1222639SSebastian Andrzej Siewior {
96a1222639SSebastian Andrzej Siewior 	struct platform_device	*pdev = to_platform_device(dev);
97a1222639SSebastian Andrzej Siewior 	struct am335x_phy	*am_phy = platform_get_drvdata(pdev);
98a1222639SSebastian Andrzej Siewior 
99a1222639SSebastian Andrzej Siewior 	phy_ctrl_power(am_phy->phy_ctrl, am_phy->id, true);
100a1222639SSebastian Andrzej Siewior 	if (device_may_wakeup(dev))
101a1222639SSebastian Andrzej Siewior 		phy_ctrl_wkup(am_phy->phy_ctrl, am_phy->id, false);
102a1222639SSebastian Andrzej Siewior 	return 0;
103a1222639SSebastian Andrzej Siewior }
104a1222639SSebastian Andrzej Siewior 
105a1222639SSebastian Andrzej Siewior static const struct dev_pm_ops am335x_pm_ops = {
106a1222639SSebastian Andrzej Siewior 	SET_RUNTIME_PM_OPS(am335x_phy_runtime_suspend,
107a1222639SSebastian Andrzej Siewior 			am335x_phy_runtime_resume, NULL)
108a1222639SSebastian Andrzej Siewior };
109a1222639SSebastian Andrzej Siewior 
110a1222639SSebastian Andrzej Siewior #define DEV_PM_OPS	(&am335x_pm_ops)
111a1222639SSebastian Andrzej Siewior #else
112a1222639SSebastian Andrzej Siewior #define DEV_PM_OPS	NULL
113a1222639SSebastian Andrzej Siewior #endif
114a1222639SSebastian Andrzej Siewior 
1153bb869c8SSebastian Andrzej Siewior static const struct of_device_id am335x_phy_ids[] = {
1163bb869c8SSebastian Andrzej Siewior 	{ .compatible = "ti,am335x-usb-phy" },
1173bb869c8SSebastian Andrzej Siewior 	{ }
1183bb869c8SSebastian Andrzej Siewior };
1193bb869c8SSebastian Andrzej Siewior MODULE_DEVICE_TABLE(of, am335x_phy_ids);
1203bb869c8SSebastian Andrzej Siewior 
1213bb869c8SSebastian Andrzej Siewior static struct platform_driver am335x_phy_driver = {
1223bb869c8SSebastian Andrzej Siewior 	.probe          = am335x_phy_probe,
1233bb869c8SSebastian Andrzej Siewior 	.remove         = am335x_phy_remove,
1243bb869c8SSebastian Andrzej Siewior 	.driver         = {
1253bb869c8SSebastian Andrzej Siewior 		.name   = "am335x-phy-driver",
1263bb869c8SSebastian Andrzej Siewior 		.owner  = THIS_MODULE,
127a1222639SSebastian Andrzej Siewior 		.pm = DEV_PM_OPS,
128*b99fffcaSSachin Kamat 		.of_match_table = am335x_phy_ids,
1293bb869c8SSebastian Andrzej Siewior 	},
1303bb869c8SSebastian Andrzej Siewior };
1313bb869c8SSebastian Andrzej Siewior 
1323bb869c8SSebastian Andrzej Siewior module_platform_driver(am335x_phy_driver);
1333bb869c8SSebastian Andrzej Siewior MODULE_LICENSE("GPL v2");
134