xref: /openbmc/linux/drivers/phy/ti/phy-dm816x-usb.c (revision cd6d421e)
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License as
4  * published by the Free Software Foundation version 2.
5  *
6  * This program is distributed "as is" WITHOUT ANY WARRANTY of any
7  * kind, whether express or implied; without even the implied warranty
8  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9  * GNU General Public License for more details.
10  */
11 
12 #include <linux/module.h>
13 #include <linux/platform_device.h>
14 #include <linux/regmap.h>
15 
16 #include <linux/slab.h>
17 #include <linux/of.h>
18 #include <linux/io.h>
19 #include <linux/usb/phy_companion.h>
20 #include <linux/clk.h>
21 #include <linux/err.h>
22 #include <linux/pm_runtime.h>
23 #include <linux/delay.h>
24 #include <linux/phy/phy.h>
25 #include <linux/of_platform.h>
26 
27 #include <linux/mfd/syscon.h>
28 
29 /*
30  * TRM has two sets of USB_CTRL registers.. The correct register bits
31  * are in TRM section 24.9.8.2 USB_CTRL Register. The TRM documents the
32  * phy as being SR70LX Synopsys USB 2.0 OTG nanoPHY. It also seems at
33  * least dm816x rev c ignores writes to USB_CTRL register, but the TI
34  * kernel is writing to those so it's possible that later revisions
35  * have worknig USB_CTRL register.
36  *
37  * Also note that At least USB_CTRL register seems to be dm816x specific
38  * according to the TRM. It's possible that USBPHY_CTRL is more generic,
39  * but that would have to be checked against the SR70LX documentation
40  * which does not seem to be publicly available.
41  *
42  * Finally, the phy on dm814x and am335x is different from dm816x.
43  */
44 #define DM816X_USB_CTRL_PHYCLKSRC	BIT(8)	/* 1 = PLL ref clock */
45 #define DM816X_USB_CTRL_PHYSLEEP1	BIT(1)	/* Enable the first phy */
46 #define DM816X_USB_CTRL_PHYSLEEP0	BIT(0)	/* Enable the second phy */
47 
48 #define DM816X_USBPHY_CTRL_TXRISETUNE	1
49 #define DM816X_USBPHY_CTRL_TXVREFTUNE	0xc
50 #define DM816X_USBPHY_CTRL_TXPREEMTUNE	0x2
51 
52 struct dm816x_usb_phy {
53 	struct regmap *syscon;
54 	struct device *dev;
55 	unsigned int instance;
56 	struct clk *refclk;
57 	struct usb_phy phy;
58 	unsigned int usb_ctrl;		/* Shared between phy0 and phy1 */
59 	unsigned int usbphy_ctrl;
60 };
61 
62 static int dm816x_usb_phy_set_host(struct usb_otg *otg, struct usb_bus *host)
63 {
64 	otg->host = host;
65 	if (!host)
66 		otg->state = OTG_STATE_UNDEFINED;
67 
68 	return 0;
69 }
70 
71 static int dm816x_usb_phy_set_peripheral(struct usb_otg *otg,
72 					 struct usb_gadget *gadget)
73 {
74 	otg->gadget = gadget;
75 	if (!gadget)
76 		otg->state = OTG_STATE_UNDEFINED;
77 
78 	return 0;
79 }
80 
81 static int dm816x_usb_phy_init(struct phy *x)
82 {
83 	struct dm816x_usb_phy *phy = phy_get_drvdata(x);
84 	unsigned int val;
85 
86 	if (clk_get_rate(phy->refclk) != 24000000)
87 		dev_warn(phy->dev, "nonstandard phy refclk\n");
88 
89 	/* Set PLL ref clock and put phys to sleep */
90 	regmap_update_bits(phy->syscon, phy->usb_ctrl,
91 			   DM816X_USB_CTRL_PHYCLKSRC |
92 			   DM816X_USB_CTRL_PHYSLEEP1 |
93 			   DM816X_USB_CTRL_PHYSLEEP0,
94 			   0);
95 	regmap_read(phy->syscon, phy->usb_ctrl, &val);
96 	if ((val & 3) != 0)
97 		dev_info(phy->dev,
98 			 "Working dm816x USB_CTRL! (0x%08x)\n",
99 			 val);
100 
101 	/*
102 	 * TI kernel sets these values for "symmetrical eye diagram and
103 	 * better signal quality" so let's assume somebody checked the
104 	 * values with a scope and set them here too.
105 	 */
106 	regmap_read(phy->syscon, phy->usbphy_ctrl, &val);
107 	val |= DM816X_USBPHY_CTRL_TXRISETUNE |
108 		DM816X_USBPHY_CTRL_TXVREFTUNE |
109 		DM816X_USBPHY_CTRL_TXPREEMTUNE;
110 	regmap_write(phy->syscon, phy->usbphy_ctrl, val);
111 
112 	return 0;
113 }
114 
115 static const struct phy_ops ops = {
116 	.init		= dm816x_usb_phy_init,
117 	.owner		= THIS_MODULE,
118 };
119 
120 static int __maybe_unused dm816x_usb_phy_runtime_suspend(struct device *dev)
121 {
122 	struct dm816x_usb_phy *phy = dev_get_drvdata(dev);
123 	unsigned int mask, val;
124 	int error = 0;
125 
126 	mask = BIT(phy->instance);
127 	val = ~BIT(phy->instance);
128 	error = regmap_update_bits(phy->syscon, phy->usb_ctrl,
129 				   mask, val);
130 	if (error)
131 		dev_err(phy->dev, "phy%i failed to power off\n",
132 			phy->instance);
133 	clk_disable(phy->refclk);
134 
135 	return 0;
136 }
137 
138 static int __maybe_unused dm816x_usb_phy_runtime_resume(struct device *dev)
139 {
140 	struct dm816x_usb_phy *phy = dev_get_drvdata(dev);
141 	unsigned int mask, val;
142 	int error;
143 
144 	error = clk_enable(phy->refclk);
145 	if (error)
146 		return error;
147 
148 	/*
149 	 * Note that at least dm816x rev c does not seem to do
150 	 * anything with the USB_CTRL register. But let's follow
151 	 * what the TI tree is doing in case later revisions use
152 	 * USB_CTRL.
153 	 */
154 	mask = BIT(phy->instance);
155 	val = BIT(phy->instance);
156 	error = regmap_update_bits(phy->syscon, phy->usb_ctrl,
157 				   mask, val);
158 	if (error) {
159 		dev_err(phy->dev, "phy%i failed to power on\n",
160 			phy->instance);
161 		clk_disable(phy->refclk);
162 		return error;
163 	}
164 
165 	return 0;
166 }
167 
168 static UNIVERSAL_DEV_PM_OPS(dm816x_usb_phy_pm_ops,
169 			    dm816x_usb_phy_runtime_suspend,
170 			    dm816x_usb_phy_runtime_resume,
171 			    NULL);
172 
173 #ifdef CONFIG_OF
174 static const struct of_device_id dm816x_usb_phy_id_table[] = {
175 	{
176 		.compatible = "ti,dm8168-usb-phy",
177 	},
178 	{},
179 };
180 MODULE_DEVICE_TABLE(of, dm816x_usb_phy_id_table);
181 #endif
182 
183 static int dm816x_usb_phy_probe(struct platform_device *pdev)
184 {
185 	struct dm816x_usb_phy *phy;
186 	struct resource *res;
187 	struct phy *generic_phy;
188 	struct phy_provider *phy_provider;
189 	struct usb_otg *otg;
190 	const struct of_device_id *of_id;
191 	int error;
192 
193 	of_id = of_match_device(of_match_ptr(dm816x_usb_phy_id_table),
194 				&pdev->dev);
195 	if (!of_id)
196 		return -EINVAL;
197 
198 	phy = devm_kzalloc(&pdev->dev, sizeof(*phy), GFP_KERNEL);
199 	if (!phy)
200 		return -ENOMEM;
201 
202 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
203 	if (!res)
204 		return -ENOENT;
205 
206 	phy->syscon = syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
207 						      "syscon");
208 	if (IS_ERR(phy->syscon))
209 		return PTR_ERR(phy->syscon);
210 
211 	/*
212 	 * According to sprs614e.pdf, the first usb_ctrl is shared and
213 	 * the second instance for usb_ctrl is reserved.. Also the
214 	 * register bits are different from earlier TRMs.
215 	 */
216 	phy->usb_ctrl = 0x20;
217 	phy->usbphy_ctrl = (res->start & 0xff) + 4;
218 	if (phy->usbphy_ctrl == 0x2c)
219 		phy->instance = 1;
220 
221 	otg = devm_kzalloc(&pdev->dev, sizeof(*otg), GFP_KERNEL);
222 	if (!otg)
223 		return -ENOMEM;
224 
225 	phy->dev = &pdev->dev;
226 	phy->phy.dev = phy->dev;
227 	phy->phy.label = "dm8168_usb_phy";
228 	phy->phy.otg = otg;
229 	phy->phy.type = USB_PHY_TYPE_USB2;
230 	otg->set_host = dm816x_usb_phy_set_host;
231 	otg->set_peripheral = dm816x_usb_phy_set_peripheral;
232 	otg->usb_phy = &phy->phy;
233 
234 	platform_set_drvdata(pdev, phy);
235 
236 	phy->refclk = devm_clk_get(phy->dev, "refclk");
237 	if (IS_ERR(phy->refclk))
238 		return PTR_ERR(phy->refclk);
239 	error = clk_prepare(phy->refclk);
240 	if (error)
241 		return error;
242 
243 	pm_runtime_enable(phy->dev);
244 	generic_phy = devm_phy_create(phy->dev, NULL, &ops);
245 	if (IS_ERR(generic_phy))
246 		return PTR_ERR(generic_phy);
247 
248 	phy_set_drvdata(generic_phy, phy);
249 
250 	phy_provider = devm_of_phy_provider_register(phy->dev,
251 						     of_phy_simple_xlate);
252 	if (IS_ERR(phy_provider))
253 		return PTR_ERR(phy_provider);
254 
255 	usb_add_phy_dev(&phy->phy);
256 
257 	return 0;
258 }
259 
260 static int dm816x_usb_phy_remove(struct platform_device *pdev)
261 {
262 	struct dm816x_usb_phy *phy = platform_get_drvdata(pdev);
263 
264 	usb_remove_phy(&phy->phy);
265 	pm_runtime_disable(phy->dev);
266 	clk_unprepare(phy->refclk);
267 
268 	return 0;
269 }
270 
271 static struct platform_driver dm816x_usb_phy_driver = {
272 	.probe		= dm816x_usb_phy_probe,
273 	.remove		= dm816x_usb_phy_remove,
274 	.driver		= {
275 		.name	= "dm816x-usb-phy",
276 		.pm	= &dm816x_usb_phy_pm_ops,
277 		.of_match_table = of_match_ptr(dm816x_usb_phy_id_table),
278 	},
279 };
280 
281 module_platform_driver(dm816x_usb_phy_driver);
282 
283 MODULE_ALIAS("platform:dm816x_usb");
284 MODULE_AUTHOR("Tony Lindgren <tony@atomide.com>");
285 MODULE_DESCRIPTION("dm816x usb phy driver");
286 MODULE_LICENSE("GPL v2");
287