Commit | Line | Data |
---|---|---|
3bb869c8 SAS |
1 | #include <linux/module.h> |
2 | #include <linux/platform_device.h> | |
3 | #include <linux/dma-mapping.h> | |
4 | #include <linux/usb/otg.h> | |
d7078df6 | 5 | #include <linux/usb/usb_phy_generic.h> |
3bb869c8 SAS |
6 | #include <linux/slab.h> |
7 | #include <linux/clk.h> | |
3bb869c8 SAS |
8 | #include <linux/of.h> |
9 | #include <linux/of_address.h> | |
59f042f6 | 10 | #include <linux/usb/of.h> |
3bb869c8 | 11 | |
5306661e | 12 | #include "phy-am335x-control.h" |
3bb869c8 SAS |
13 | #include "phy-generic.h" |
14 | ||
15 | struct am335x_phy { | |
4525beeb | 16 | struct usb_phy_generic usb_phy_gen; |
3bb869c8 SAS |
17 | struct phy_control *phy_ctrl; |
18 | int id; | |
59f042f6 | 19 | enum usb_dr_mode dr_mode; |
3bb869c8 SAS |
20 | }; |
21 | ||
22 | static int am335x_init(struct usb_phy *phy) | |
23 | { | |
24 | struct am335x_phy *am_phy = dev_get_drvdata(phy->dev); | |
25 | ||
59f042f6 | 26 | phy_ctrl_power(am_phy->phy_ctrl, am_phy->id, am_phy->dr_mode, true); |
3bb869c8 SAS |
27 | return 0; |
28 | } | |
29 | ||
30 | static void am335x_shutdown(struct usb_phy *phy) | |
31 | { | |
32 | struct am335x_phy *am_phy = dev_get_drvdata(phy->dev); | |
33 | ||
59f042f6 | 34 | phy_ctrl_power(am_phy->phy_ctrl, am_phy->id, am_phy->dr_mode, false); |
3bb869c8 SAS |
35 | } |
36 | ||
37 | static 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; | |
59f042f6 | 50 | |
3bb869c8 SAS |
51 | 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 | ||
ce15ed4c | 57 | am_phy->dr_mode = of_usb_get_dr_mode_by_phy(pdev->dev.of_node, -1); |
59f042f6 | 58 | |
af9f51c5 | 59 | ret = usb_phy_gen_create_phy(dev, &am_phy->usb_phy_gen, NULL); |
3bb869c8 SAS |
60 | if (ret) |
61 | return ret; | |
62 | ||
63 | ret = usb_add_phy_dev(&am_phy->usb_phy_gen.phy); | |
64 | if (ret) | |
4d175f34 | 65 | return ret; |
3bb869c8 SAS |
66 | am_phy->usb_phy_gen.phy.init = am335x_init; |
67 | am_phy->usb_phy_gen.phy.shutdown = am335x_shutdown; | |
68 | ||
69 | platform_set_drvdata(pdev, am_phy); | |
2fc711d7 GC |
70 | device_init_wakeup(dev, true); |
71 | ||
72 | /* | |
73 | * If we leave PHY wakeup enabled then AM33XX wakes up | |
74 | * immediately from DS0. To avoid this we mark dev->power.can_wakeup | |
75 | * to false. The same is checked in suspend routine to decide | |
76 | * on whether to enable PHY wakeup or not. | |
77 | * PHY wakeup works fine in standby mode, there by allowing us to | |
78 | * handle remote wakeup, wakeup on disconnect and connect. | |
79 | */ | |
80 | ||
81 | device_set_wakeup_enable(dev, false); | |
59f042f6 | 82 | phy_ctrl_power(am_phy->phy_ctrl, am_phy->id, am_phy->dr_mode, false); |
4d175f34 | 83 | |
3bb869c8 | 84 | return 0; |
3bb869c8 SAS |
85 | } |
86 | ||
87 | static int am335x_phy_remove(struct platform_device *pdev) | |
88 | { | |
89 | struct am335x_phy *am_phy = platform_get_drvdata(pdev); | |
90 | ||
91 | usb_remove_phy(&am_phy->usb_phy_gen.phy); | |
92 | return 0; | |
93 | } | |
94 | ||
2fc711d7 GC |
95 | #ifdef CONFIG_PM_SLEEP |
96 | static int am335x_phy_suspend(struct device *dev) | |
a1222639 SAS |
97 | { |
98 | struct platform_device *pdev = to_platform_device(dev); | |
99 | struct am335x_phy *am_phy = platform_get_drvdata(pdev); | |
100 | ||
2fc711d7 GC |
101 | /* |
102 | * Enable phy wakeup only if dev->power.can_wakeup is true. | |
103 | * Make sure to enable wakeup to support remote wakeup in | |
104 | * standby mode ( same is not supported in OFF(DS0) mode). | |
105 | * Enable it by doing | |
106 | * echo enabled > /sys/bus/platform/devices/<usb-phy-id>/power/wakeup | |
107 | */ | |
108 | ||
a1222639 SAS |
109 | if (device_may_wakeup(dev)) |
110 | phy_ctrl_wkup(am_phy->phy_ctrl, am_phy->id, true); | |
2fc711d7 | 111 | |
59f042f6 | 112 | phy_ctrl_power(am_phy->phy_ctrl, am_phy->id, am_phy->dr_mode, false); |
2fc711d7 | 113 | |
a1222639 SAS |
114 | return 0; |
115 | } | |
116 | ||
2fc711d7 | 117 | static int am335x_phy_resume(struct device *dev) |
a1222639 SAS |
118 | { |
119 | struct platform_device *pdev = to_platform_device(dev); | |
120 | struct am335x_phy *am_phy = platform_get_drvdata(pdev); | |
121 | ||
59f042f6 | 122 | phy_ctrl_power(am_phy->phy_ctrl, am_phy->id, am_phy->dr_mode, true); |
2fc711d7 | 123 | |
a1222639 SAS |
124 | if (device_may_wakeup(dev)) |
125 | phy_ctrl_wkup(am_phy->phy_ctrl, am_phy->id, false); | |
2fc711d7 | 126 | |
a1222639 SAS |
127 | return 0; |
128 | } | |
2fc711d7 GC |
129 | #endif |
130 | ||
1e32cda8 JH |
131 | static SIMPLE_DEV_PM_OPS(am335x_pm_ops, am335x_phy_suspend, am335x_phy_resume); |
132 | ||
3bb869c8 SAS |
133 | static const struct of_device_id am335x_phy_ids[] = { |
134 | { .compatible = "ti,am335x-usb-phy" }, | |
135 | { } | |
136 | }; | |
137 | MODULE_DEVICE_TABLE(of, am335x_phy_ids); | |
138 | ||
139 | static struct platform_driver am335x_phy_driver = { | |
140 | .probe = am335x_phy_probe, | |
141 | .remove = am335x_phy_remove, | |
142 | .driver = { | |
143 | .name = "am335x-phy-driver", | |
1e32cda8 | 144 | .pm = &am335x_pm_ops, |
b99fffca | 145 | .of_match_table = am335x_phy_ids, |
3bb869c8 SAS |
146 | }, |
147 | }; | |
148 | ||
149 | module_platform_driver(am335x_phy_driver); | |
150 | MODULE_LICENSE("GPL v2"); |