blob: cb1382a527654a76be0dd0ccf108ffa32a44719c [file] [log] [blame]
Sebastian Andrzej Siewior3bb869c2013-07-30 21:43:45 +02001#include <linux/module.h>
2#include <linux/platform_device.h>
3#include <linux/dma-mapping.h>
4#include <linux/usb/otg.h>
Felipe Balbid7078df2014-04-16 15:28:32 -05005#include <linux/usb/usb_phy_generic.h>
Sebastian Andrzej Siewior3bb869c2013-07-30 21:43:45 +02006#include <linux/slab.h>
7#include <linux/clk.h>
Sebastian Andrzej Siewior3bb869c2013-07-30 21:43:45 +02008#include <linux/of.h>
9#include <linux/of_address.h>
Bin Liu59f042f2015-12-08 10:31:50 -060010#include <linux/usb/of.h>
Sebastian Andrzej Siewior3bb869c2013-07-30 21:43:45 +020011
Bin Liu53066612015-11-20 16:13:07 -060012#include "phy-am335x-control.h"
Sebastian Andrzej Siewior3bb869c2013-07-30 21:43:45 +020013#include "phy-generic.h"
14
15struct am335x_phy {
Felipe Balbi4525bee2014-04-16 15:20:44 -050016 struct usb_phy_generic usb_phy_gen;
Sebastian Andrzej Siewior3bb869c2013-07-30 21:43:45 +020017 struct phy_control *phy_ctrl;
18 int id;
Bin Liu59f042f2015-12-08 10:31:50 -060019 enum usb_dr_mode dr_mode;
Sebastian Andrzej Siewior3bb869c2013-07-30 21:43:45 +020020};
21
22static int am335x_init(struct usb_phy *phy)
23{
24 struct am335x_phy *am_phy = dev_get_drvdata(phy->dev);
25
Bin Liu59f042f2015-12-08 10:31:50 -060026 phy_ctrl_power(am_phy->phy_ctrl, am_phy->id, am_phy->dr_mode, true);
Sebastian Andrzej Siewior3bb869c2013-07-30 21:43:45 +020027 return 0;
28}
29
30static void am335x_shutdown(struct usb_phy *phy)
31{
32 struct am335x_phy *am_phy = dev_get_drvdata(phy->dev);
33
Bin Liu59f042f2015-12-08 10:31:50 -060034 phy_ctrl_power(am_phy->phy_ctrl, am_phy->id, am_phy->dr_mode, false);
Sebastian Andrzej Siewior3bb869c2013-07-30 21:43:45 +020035}
36
37static int am335x_phy_probe(struct platform_device *pdev)
38{
39 struct am335x_phy *am_phy;
40 struct device *dev = &pdev->dev;
41 int ret;
42
43 am_phy = devm_kzalloc(dev, sizeof(*am_phy), GFP_KERNEL);
44 if (!am_phy)
45 return -ENOMEM;
46
47 am_phy->phy_ctrl = am335x_get_phy_control(dev);
48 if (!am_phy->phy_ctrl)
49 return -EPROBE_DEFER;
Bin Liu59f042f2015-12-08 10:31:50 -060050
Sebastian Andrzej Siewior3bb869c2013-07-30 21:43:45 +020051 am_phy->id = of_alias_get_id(pdev->dev.of_node, "phy");
52 if (am_phy->id < 0) {
53 dev_err(&pdev->dev, "Missing PHY id: %d\n", am_phy->id);
54 return am_phy->id;
55 }
56
Hans de Goedece15ed42016-06-10 11:46:25 +020057 am_phy->dr_mode = of_usb_get_dr_mode_by_phy(pdev->dev.of_node, -1);
Bin Liu59f042f2015-12-08 10:31:50 -060058
Felipe Balbiaf9f51c2013-10-24 09:45:29 -050059 ret = usb_phy_gen_create_phy(dev, &am_phy->usb_phy_gen, NULL);
Sebastian Andrzej Siewior3bb869c2013-07-30 21:43:45 +020060 if (ret)
61 return ret;
62
Sebastian Andrzej Siewior3bb869c2013-07-30 21:43:45 +020063 am_phy->usb_phy_gen.phy.init = am335x_init;
64 am_phy->usb_phy_gen.phy.shutdown = am335x_shutdown;
65
66 platform_set_drvdata(pdev, am_phy);
George Cherian2fc711d72013-12-18 18:52:09 +053067 device_init_wakeup(dev, true);
68
69 /*
70 * If we leave PHY wakeup enabled then AM33XX wakes up
71 * immediately from DS0. To avoid this we mark dev->power.can_wakeup
72 * to false. The same is checked in suspend routine to decide
73 * on whether to enable PHY wakeup or not.
74 * PHY wakeup works fine in standby mode, there by allowing us to
75 * handle remote wakeup, wakeup on disconnect and connect.
76 */
77
78 device_set_wakeup_enable(dev, false);
Bin Liu59f042f2015-12-08 10:31:50 -060079 phy_ctrl_power(am_phy->phy_ctrl, am_phy->id, am_phy->dr_mode, false);
Mark Brown4d175f32013-08-11 15:26:04 +010080
Bin Liu2fc78042019-01-16 11:54:07 -060081 return usb_add_phy_dev(&am_phy->usb_phy_gen.phy);
Sebastian Andrzej Siewior3bb869c2013-07-30 21:43:45 +020082}
83
84static int am335x_phy_remove(struct platform_device *pdev)
85{
86 struct am335x_phy *am_phy = platform_get_drvdata(pdev);
87
88 usb_remove_phy(&am_phy->usb_phy_gen.phy);
89 return 0;
90}
91
George Cherian2fc711d72013-12-18 18:52:09 +053092#ifdef CONFIG_PM_SLEEP
93static int am335x_phy_suspend(struct device *dev)
Sebastian Andrzej Siewiora1222632013-08-19 12:39:44 +020094{
95 struct platform_device *pdev = to_platform_device(dev);
96 struct am335x_phy *am_phy = platform_get_drvdata(pdev);
97
George Cherian2fc711d72013-12-18 18:52:09 +053098 /*
99 * Enable phy wakeup only if dev->power.can_wakeup is true.
100 * Make sure to enable wakeup to support remote wakeup in
101 * standby mode ( same is not supported in OFF(DS0) mode).
102 * Enable it by doing
103 * echo enabled > /sys/bus/platform/devices/<usb-phy-id>/power/wakeup
104 */
105
Sebastian Andrzej Siewiora1222632013-08-19 12:39:44 +0200106 if (device_may_wakeup(dev))
107 phy_ctrl_wkup(am_phy->phy_ctrl, am_phy->id, true);
George Cherian2fc711d72013-12-18 18:52:09 +0530108
Bin Liu59f042f2015-12-08 10:31:50 -0600109 phy_ctrl_power(am_phy->phy_ctrl, am_phy->id, am_phy->dr_mode, false);
George Cherian2fc711d72013-12-18 18:52:09 +0530110
Sebastian Andrzej Siewiora1222632013-08-19 12:39:44 +0200111 return 0;
112}
113
George Cherian2fc711d72013-12-18 18:52:09 +0530114static int am335x_phy_resume(struct device *dev)
Sebastian Andrzej Siewiora1222632013-08-19 12:39:44 +0200115{
116 struct platform_device *pdev = to_platform_device(dev);
117 struct am335x_phy *am_phy = platform_get_drvdata(pdev);
118
Bin Liu59f042f2015-12-08 10:31:50 -0600119 phy_ctrl_power(am_phy->phy_ctrl, am_phy->id, am_phy->dr_mode, true);
George Cherian2fc711d72013-12-18 18:52:09 +0530120
Sebastian Andrzej Siewiora1222632013-08-19 12:39:44 +0200121 if (device_may_wakeup(dev))
122 phy_ctrl_wkup(am_phy->phy_ctrl, am_phy->id, false);
George Cherian2fc711d72013-12-18 18:52:09 +0530123
Sebastian Andrzej Siewiora1222632013-08-19 12:39:44 +0200124 return 0;
125}
George Cherian2fc711d72013-12-18 18:52:09 +0530126#endif
127
Jingoo Han1e32cda2014-07-08 20:51:34 +0900128static SIMPLE_DEV_PM_OPS(am335x_pm_ops, am335x_phy_suspend, am335x_phy_resume);
129
Sebastian Andrzej Siewior3bb869c2013-07-30 21:43:45 +0200130static const struct of_device_id am335x_phy_ids[] = {
131 { .compatible = "ti,am335x-usb-phy" },
132 { }
133};
134MODULE_DEVICE_TABLE(of, am335x_phy_ids);
135
136static struct platform_driver am335x_phy_driver = {
137 .probe = am335x_phy_probe,
138 .remove = am335x_phy_remove,
139 .driver = {
140 .name = "am335x-phy-driver",
Jingoo Han1e32cda2014-07-08 20:51:34 +0900141 .pm = &am335x_pm_ops,
Sachin Kamatb99fffc2013-09-30 09:44:46 +0530142 .of_match_table = am335x_phy_ids,
Sebastian Andrzej Siewior3bb869c2013-07-30 21:43:45 +0200143 },
144};
145
146module_platform_driver(am335x_phy_driver);
147MODULE_LICENSE("GPL v2");