blob: b2e59b6170ac99fc575495e356bb831a59f6e92c [file] [log] [blame]
David Lechnerf2e60042016-05-09 18:40:00 -05001/*
2 * phy-da8xx-usb - TI DaVinci DA8xx USB PHY driver
3 *
4 * Copyright (C) 2016 David Lechner <david@lechnology.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 */
15
16#include <linux/clk.h>
17#include <linux/io.h>
18#include <linux/of.h>
19#include <linux/mfd/da8xx-cfgchip.h>
20#include <linux/mfd/syscon.h>
21#include <linux/module.h>
22#include <linux/phy/phy.h>
23#include <linux/platform_device.h>
24#include <linux/regmap.h>
25
26struct da8xx_usb_phy {
27 struct phy_provider *phy_provider;
28 struct phy *usb11_phy;
29 struct phy *usb20_phy;
30 struct clk *usb11_clk;
31 struct clk *usb20_clk;
32 struct regmap *regmap;
33};
34
35static int da8xx_usb11_phy_power_on(struct phy *phy)
36{
37 struct da8xx_usb_phy *d_phy = phy_get_drvdata(phy);
38 int ret;
39
40 ret = clk_prepare_enable(d_phy->usb11_clk);
41 if (ret)
42 return ret;
43
44 regmap_write_bits(d_phy->regmap, CFGCHIP(2), CFGCHIP2_USB1SUSPENDM,
45 CFGCHIP2_USB1SUSPENDM);
46
47 return 0;
48}
49
50static int da8xx_usb11_phy_power_off(struct phy *phy)
51{
52 struct da8xx_usb_phy *d_phy = phy_get_drvdata(phy);
53
54 regmap_write_bits(d_phy->regmap, CFGCHIP(2), CFGCHIP2_USB1SUSPENDM, 0);
55
56 clk_disable_unprepare(d_phy->usb11_clk);
57
58 return 0;
59}
60
61static const struct phy_ops da8xx_usb11_phy_ops = {
62 .power_on = da8xx_usb11_phy_power_on,
63 .power_off = da8xx_usb11_phy_power_off,
64 .owner = THIS_MODULE,
65};
66
67static int da8xx_usb20_phy_power_on(struct phy *phy)
68{
69 struct da8xx_usb_phy *d_phy = phy_get_drvdata(phy);
70 int ret;
71
72 ret = clk_prepare_enable(d_phy->usb20_clk);
73 if (ret)
74 return ret;
75
76 regmap_write_bits(d_phy->regmap, CFGCHIP(2), CFGCHIP2_OTGPWRDN, 0);
77
78 return 0;
79}
80
81static int da8xx_usb20_phy_power_off(struct phy *phy)
82{
83 struct da8xx_usb_phy *d_phy = phy_get_drvdata(phy);
84
85 regmap_write_bits(d_phy->regmap, CFGCHIP(2), CFGCHIP2_OTGPWRDN,
86 CFGCHIP2_OTGPWRDN);
87
88 clk_disable_unprepare(d_phy->usb20_clk);
89
90 return 0;
91}
92
93static int da8xx_usb20_phy_set_mode(struct phy *phy, enum phy_mode mode)
94{
95 struct da8xx_usb_phy *d_phy = phy_get_drvdata(phy);
96 u32 val;
97
98 switch (mode) {
99 case PHY_MODE_USB_HOST: /* Force VBUS valid, ID = 0 */
100 val = CFGCHIP2_OTGMODE_FORCE_HOST;
101 break;
102 case PHY_MODE_USB_DEVICE: /* Force VBUS valid, ID = 1 */
103 val = CFGCHIP2_OTGMODE_FORCE_DEVICE;
104 break;
105 case PHY_MODE_USB_OTG: /* Don't override the VBUS/ID comparators */
106 val = CFGCHIP2_OTGMODE_NO_OVERRIDE;
107 break;
108 default:
109 return -EINVAL;
110 }
111
112 regmap_write_bits(d_phy->regmap, CFGCHIP(2), CFGCHIP2_OTGMODE_MASK,
113 val);
114
115 return 0;
116}
117
118static const struct phy_ops da8xx_usb20_phy_ops = {
119 .power_on = da8xx_usb20_phy_power_on,
120 .power_off = da8xx_usb20_phy_power_off,
121 .set_mode = da8xx_usb20_phy_set_mode,
122 .owner = THIS_MODULE,
123};
124
125static struct phy *da8xx_usb_phy_of_xlate(struct device *dev,
126 struct of_phandle_args *args)
127{
128 struct da8xx_usb_phy *d_phy = dev_get_drvdata(dev);
129
130 if (!d_phy)
131 return ERR_PTR(-ENODEV);
132
133 switch (args->args[0]) {
134 case 0:
135 return d_phy->usb20_phy;
136 case 1:
137 return d_phy->usb11_phy;
138 default:
139 return ERR_PTR(-EINVAL);
140 }
141}
142
143static int da8xx_usb_phy_probe(struct platform_device *pdev)
144{
145 struct device *dev = &pdev->dev;
146 struct device_node *node = dev->of_node;
147 struct da8xx_usb_phy *d_phy;
148
149 d_phy = devm_kzalloc(dev, sizeof(*d_phy), GFP_KERNEL);
150 if (!d_phy)
151 return -ENOMEM;
152
153 if (node)
154 d_phy->regmap = syscon_regmap_lookup_by_compatible(
155 "ti,da830-cfgchip");
156 else
157 d_phy->regmap = syscon_regmap_lookup_by_pdevname("syscon.0");
158 if (IS_ERR(d_phy->regmap)) {
159 dev_err(dev, "Failed to get syscon\n");
160 return PTR_ERR(d_phy->regmap);
161 }
162
163 d_phy->usb11_clk = devm_clk_get(dev, "usb11_phy");
164 if (IS_ERR(d_phy->usb11_clk)) {
165 dev_err(dev, "Failed to get usb11_phy clock\n");
166 return PTR_ERR(d_phy->usb11_clk);
167 }
168
169 d_phy->usb20_clk = devm_clk_get(dev, "usb20_phy");
170 if (IS_ERR(d_phy->usb20_clk)) {
171 dev_err(dev, "Failed to get usb20_phy clock\n");
172 return PTR_ERR(d_phy->usb20_clk);
173 }
174
175 d_phy->usb11_phy = devm_phy_create(dev, node, &da8xx_usb11_phy_ops);
176 if (IS_ERR(d_phy->usb11_phy)) {
177 dev_err(dev, "Failed to create usb11 phy\n");
178 return PTR_ERR(d_phy->usb11_phy);
179 }
180
181 d_phy->usb20_phy = devm_phy_create(dev, node, &da8xx_usb20_phy_ops);
182 if (IS_ERR(d_phy->usb20_phy)) {
183 dev_err(dev, "Failed to create usb20 phy\n");
184 return PTR_ERR(d_phy->usb20_phy);
185 }
186
187 platform_set_drvdata(pdev, d_phy);
188 phy_set_drvdata(d_phy->usb11_phy, d_phy);
189 phy_set_drvdata(d_phy->usb20_phy, d_phy);
190
191 if (node) {
192 d_phy->phy_provider = devm_of_phy_provider_register(dev,
193 da8xx_usb_phy_of_xlate);
194 if (IS_ERR(d_phy->phy_provider)) {
195 dev_err(dev, "Failed to create phy provider\n");
196 return PTR_ERR(d_phy->phy_provider);
197 }
198 } else {
199 int ret;
200
201 ret = phy_create_lookup(d_phy->usb11_phy, "usb-phy", "ohci.0");
202 if (ret)
203 dev_warn(dev, "Failed to create usb11 phy lookup\n");
204 ret = phy_create_lookup(d_phy->usb20_phy, "usb-phy",
205 "musb-da8xx");
206 if (ret)
207 dev_warn(dev, "Failed to create usb20 phy lookup\n");
208 }
209
210 return 0;
211}
212
213static int da8xx_usb_phy_remove(struct platform_device *pdev)
214{
215 struct da8xx_usb_phy *d_phy = platform_get_drvdata(pdev);
216
217 if (!pdev->dev.of_node) {
218 phy_remove_lookup(d_phy->usb20_phy, "usb-phy", "musb-da8xx");
219 phy_remove_lookup(d_phy->usb11_phy, "usb-phy", "ohci.0");
220 }
221
222 return 0;
223}
224
225static const struct of_device_id da8xx_usb_phy_ids[] = {
226 { .compatible = "ti,da830-usb-phy" },
227 { }
228};
229MODULE_DEVICE_TABLE(of, da8xx_usb_phy_ids);
230
231static struct platform_driver da8xx_usb_phy_driver = {
232 .probe = da8xx_usb_phy_probe,
233 .remove = da8xx_usb_phy_remove,
234 .driver = {
235 .name = "da8xx-usb-phy",
236 .of_match_table = da8xx_usb_phy_ids,
237 },
238};
239
240module_platform_driver(da8xx_usb_phy_driver);
241
242MODULE_ALIAS("platform:da8xx-usb-phy");
243MODULE_AUTHOR("David Lechner <david@lechnology.com>");
244MODULE_DESCRIPTION("TI DA8xx USB PHY driver");
245MODULE_LICENSE("GPL v2");