xref: /openbmc/linux/drivers/phy/ti/phy-dm816x-usb.c (revision 7f877908)
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 	int error;
86 
87 	if (clk_get_rate(phy->refclk) != 24000000)
88 		dev_warn(phy->dev, "nonstandard phy refclk\n");
89 
90 	/* Set PLL ref clock and put phys to sleep */
91 	error = regmap_update_bits(phy->syscon, phy->usb_ctrl,
92 				   DM816X_USB_CTRL_PHYCLKSRC |
93 				   DM816X_USB_CTRL_PHYSLEEP1 |
94 				   DM816X_USB_CTRL_PHYSLEEP0,
95 				   0);
96 	regmap_read(phy->syscon, phy->usb_ctrl, &val);
97 	if ((val & 3) != 0)
98 		dev_info(phy->dev,
99 			 "Working dm816x USB_CTRL! (0x%08x)\n",
100 			 val);
101 
102 	/*
103 	 * TI kernel sets these values for "symmetrical eye diagram and
104 	 * better signal quality" so let's assume somebody checked the
105 	 * values with a scope and set them here too.
106 	 */
107 	regmap_read(phy->syscon, phy->usbphy_ctrl, &val);
108 	val |= DM816X_USBPHY_CTRL_TXRISETUNE |
109 		DM816X_USBPHY_CTRL_TXVREFTUNE |
110 		DM816X_USBPHY_CTRL_TXPREEMTUNE;
111 	regmap_write(phy->syscon, phy->usbphy_ctrl, val);
112 
113 	return 0;
114 }
115 
116 static const struct phy_ops ops = {
117 	.init		= dm816x_usb_phy_init,
118 	.owner		= THIS_MODULE,
119 };
120 
121 static int __maybe_unused dm816x_usb_phy_runtime_suspend(struct device *dev)
122 {
123 	struct dm816x_usb_phy *phy = dev_get_drvdata(dev);
124 	unsigned int mask, val;
125 	int error = 0;
126 
127 	mask = BIT(phy->instance);
128 	val = ~BIT(phy->instance);
129 	error = regmap_update_bits(phy->syscon, phy->usb_ctrl,
130 				   mask, val);
131 	if (error)
132 		dev_err(phy->dev, "phy%i failed to power off\n",
133 			phy->instance);
134 	clk_disable(phy->refclk);
135 
136 	return 0;
137 }
138 
139 static int __maybe_unused dm816x_usb_phy_runtime_resume(struct device *dev)
140 {
141 	struct dm816x_usb_phy *phy = dev_get_drvdata(dev);
142 	unsigned int mask, val;
143 	int error;
144 
145 	error = clk_enable(phy->refclk);
146 	if (error)
147 		return error;
148 
149 	/*
150 	 * Note that at least dm816x rev c does not seem to do
151 	 * anything with the USB_CTRL register. But let's follow
152 	 * what the TI tree is doing in case later revisions use
153 	 * USB_CTRL.
154 	 */
155 	mask = BIT(phy->instance);
156 	val = BIT(phy->instance);
157 	error = regmap_update_bits(phy->syscon, phy->usb_ctrl,
158 				   mask, val);
159 	if (error) {
160 		dev_err(phy->dev, "phy%i failed to power on\n",
161 			phy->instance);
162 		clk_disable(phy->refclk);
163 		return error;
164 	}
165 
166 	return 0;
167 }
168 
169 static UNIVERSAL_DEV_PM_OPS(dm816x_usb_phy_pm_ops,
170 			    dm816x_usb_phy_runtime_suspend,
171 			    dm816x_usb_phy_runtime_resume,
172 			    NULL);
173 
174 #ifdef CONFIG_OF
175 static const struct of_device_id dm816x_usb_phy_id_table[] = {
176 	{
177 		.compatible = "ti,dm8168-usb-phy",
178 	},
179 	{},
180 };
181 MODULE_DEVICE_TABLE(of, dm816x_usb_phy_id_table);
182 #endif
183 
184 static int dm816x_usb_phy_probe(struct platform_device *pdev)
185 {
186 	struct dm816x_usb_phy *phy;
187 	struct resource *res;
188 	struct phy *generic_phy;
189 	struct phy_provider *phy_provider;
190 	struct usb_otg *otg;
191 	const struct of_device_id *of_id;
192 	int error;
193 
194 	of_id = of_match_device(of_match_ptr(dm816x_usb_phy_id_table),
195 				&pdev->dev);
196 	if (!of_id)
197 		return -EINVAL;
198 
199 	phy = devm_kzalloc(&pdev->dev, sizeof(*phy), GFP_KERNEL);
200 	if (!phy)
201 		return -ENOMEM;
202 
203 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
204 	if (!res)
205 		return -ENOENT;
206 
207 	phy->syscon = syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
208 						      "syscon");
209 	if (IS_ERR(phy->syscon))
210 		return PTR_ERR(phy->syscon);
211 
212 	/*
213 	 * According to sprs614e.pdf, the first usb_ctrl is shared and
214 	 * the second instance for usb_ctrl is reserved.. Also the
215 	 * register bits are different from earlier TRMs.
216 	 */
217 	phy->usb_ctrl = 0x20;
218 	phy->usbphy_ctrl = (res->start & 0xff) + 4;
219 	if (phy->usbphy_ctrl == 0x2c)
220 		phy->instance = 1;
221 
222 	otg = devm_kzalloc(&pdev->dev, sizeof(*otg), GFP_KERNEL);
223 	if (!otg)
224 		return -ENOMEM;
225 
226 	phy->dev = &pdev->dev;
227 	phy->phy.dev = phy->dev;
228 	phy->phy.label = "dm8168_usb_phy";
229 	phy->phy.otg = otg;
230 	phy->phy.type = USB_PHY_TYPE_USB2;
231 	otg->set_host = dm816x_usb_phy_set_host;
232 	otg->set_peripheral = dm816x_usb_phy_set_peripheral;
233 	otg->usb_phy = &phy->phy;
234 
235 	platform_set_drvdata(pdev, phy);
236 
237 	phy->refclk = devm_clk_get(phy->dev, "refclk");
238 	if (IS_ERR(phy->refclk))
239 		return PTR_ERR(phy->refclk);
240 	error = clk_prepare(phy->refclk);
241 	if (error)
242 		return error;
243 
244 	pm_runtime_enable(phy->dev);
245 	generic_phy = devm_phy_create(phy->dev, NULL, &ops);
246 	if (IS_ERR(generic_phy))
247 		return PTR_ERR(generic_phy);
248 
249 	phy_set_drvdata(generic_phy, phy);
250 
251 	phy_provider = devm_of_phy_provider_register(phy->dev,
252 						     of_phy_simple_xlate);
253 	if (IS_ERR(phy_provider))
254 		return PTR_ERR(phy_provider);
255 
256 	usb_add_phy_dev(&phy->phy);
257 
258 	return 0;
259 }
260 
261 static int dm816x_usb_phy_remove(struct platform_device *pdev)
262 {
263 	struct dm816x_usb_phy *phy = platform_get_drvdata(pdev);
264 
265 	usb_remove_phy(&phy->phy);
266 	pm_runtime_disable(phy->dev);
267 	clk_unprepare(phy->refclk);
268 
269 	return 0;
270 }
271 
272 static struct platform_driver dm816x_usb_phy_driver = {
273 	.probe		= dm816x_usb_phy_probe,
274 	.remove		= dm816x_usb_phy_remove,
275 	.driver		= {
276 		.name	= "dm816x-usb-phy",
277 		.pm	= &dm816x_usb_phy_pm_ops,
278 		.of_match_table = of_match_ptr(dm816x_usb_phy_id_table),
279 	},
280 };
281 
282 module_platform_driver(dm816x_usb_phy_driver);
283 
284 MODULE_ALIAS("platform:dm816x_usb");
285 MODULE_AUTHOR("Tony Lindgren <tony@atomide.com>");
286 MODULE_DESCRIPTION("dm816x usb phy driver");
287 MODULE_LICENSE("GPL v2");
288