Linux 5.1.15
[linux/fpc-iii.git] / drivers / phy / ti / phy-omap-usb2.c
blobe871f2983a0e99364bc21d93b7762e8acc760930
1 /*
2 * omap-usb2.c - USB PHY, talking to musb controller in OMAP.
4 * Copyright (C) 2012 Texas Instruments Incorporated - http://www.ti.com
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * Author: Kishon Vijay Abraham I <kishon@ti.com>
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
19 #include <linux/module.h>
20 #include <linux/platform_device.h>
21 #include <linux/slab.h>
22 #include <linux/of.h>
23 #include <linux/io.h>
24 #include <linux/phy/omap_usb.h>
25 #include <linux/usb/phy_companion.h>
26 #include <linux/clk.h>
27 #include <linux/err.h>
28 #include <linux/pm_runtime.h>
29 #include <linux/delay.h>
30 #include <linux/phy/omap_control_phy.h>
31 #include <linux/phy/phy.h>
32 #include <linux/mfd/syscon.h>
33 #include <linux/regmap.h>
34 #include <linux/of_platform.h>
36 #define USB2PHY_DISCON_BYP_LATCH (1 << 31)
37 #define USB2PHY_ANA_CONFIG1 0x4c
39 #define AM654_USB2_OTG_PD BIT(8)
40 #define AM654_USB2_VBUS_DET_EN BIT(5)
41 #define AM654_USB2_VBUSVALID_DET_EN BIT(4)
43 /**
44 * omap_usb2_set_comparator - links the comparator present in the sytem with
45 * this phy
46 * @comparator - the companion phy(comparator) for this phy
48 * The phy companion driver should call this API passing the phy_companion
49 * filled with set_vbus and start_srp to be used by usb phy.
51 * For use by phy companion driver
53 int omap_usb2_set_comparator(struct phy_companion *comparator)
55 struct omap_usb *phy;
56 struct usb_phy *x = usb_get_phy(USB_PHY_TYPE_USB2);
58 if (IS_ERR(x))
59 return -ENODEV;
61 phy = phy_to_omapusb(x);
62 phy->comparator = comparator;
63 return 0;
65 EXPORT_SYMBOL_GPL(omap_usb2_set_comparator);
67 static int omap_usb_set_vbus(struct usb_otg *otg, bool enabled)
69 struct omap_usb *phy = phy_to_omapusb(otg->usb_phy);
71 if (!phy->comparator)
72 return -ENODEV;
74 return phy->comparator->set_vbus(phy->comparator, enabled);
77 static int omap_usb_start_srp(struct usb_otg *otg)
79 struct omap_usb *phy = phy_to_omapusb(otg->usb_phy);
81 if (!phy->comparator)
82 return -ENODEV;
84 return phy->comparator->start_srp(phy->comparator);
87 static int omap_usb_set_host(struct usb_otg *otg, struct usb_bus *host)
89 otg->host = host;
90 if (!host)
91 otg->state = OTG_STATE_UNDEFINED;
93 return 0;
96 static int omap_usb_set_peripheral(struct usb_otg *otg,
97 struct usb_gadget *gadget)
99 otg->gadget = gadget;
100 if (!gadget)
101 otg->state = OTG_STATE_UNDEFINED;
103 return 0;
106 static int omap_usb_phy_power(struct omap_usb *phy, int on)
108 u32 val;
109 int ret;
111 if (!phy->syscon_phy_power) {
112 omap_control_phy_power(phy->control_dev, on);
113 return 0;
116 if (on)
117 val = phy->power_on;
118 else
119 val = phy->power_off;
121 ret = regmap_update_bits(phy->syscon_phy_power, phy->power_reg,
122 phy->mask, val);
123 return ret;
126 static int omap_usb_power_off(struct phy *x)
128 struct omap_usb *phy = phy_get_drvdata(x);
130 return omap_usb_phy_power(phy, false);
133 static int omap_usb_power_on(struct phy *x)
135 struct omap_usb *phy = phy_get_drvdata(x);
137 return omap_usb_phy_power(phy, true);
140 static int omap_usb2_disable_clocks(struct omap_usb *phy)
142 clk_disable_unprepare(phy->wkupclk);
143 if (!IS_ERR(phy->optclk))
144 clk_disable_unprepare(phy->optclk);
146 return 0;
149 static int omap_usb2_enable_clocks(struct omap_usb *phy)
151 int ret;
153 ret = clk_prepare_enable(phy->wkupclk);
154 if (ret < 0) {
155 dev_err(phy->dev, "Failed to enable wkupclk %d\n", ret);
156 goto err0;
159 if (!IS_ERR(phy->optclk)) {
160 ret = clk_prepare_enable(phy->optclk);
161 if (ret < 0) {
162 dev_err(phy->dev, "Failed to enable optclk %d\n", ret);
163 goto err1;
167 return 0;
169 err1:
170 clk_disable(phy->wkupclk);
172 err0:
173 return ret;
176 static int omap_usb_init(struct phy *x)
178 struct omap_usb *phy = phy_get_drvdata(x);
179 u32 val;
181 omap_usb2_enable_clocks(phy);
183 if (phy->flags & OMAP_USB2_CALIBRATE_FALSE_DISCONNECT) {
186 * Reduce the sensitivity of internal PHY by enabling the
187 * DISCON_BYP_LATCH of the USB2PHY_ANA_CONFIG1 register. This
188 * resolves issues with certain devices which can otherwise
189 * be prone to false disconnects.
192 val = omap_usb_readl(phy->phy_base, USB2PHY_ANA_CONFIG1);
193 val |= USB2PHY_DISCON_BYP_LATCH;
194 omap_usb_writel(phy->phy_base, USB2PHY_ANA_CONFIG1, val);
197 return 0;
200 static int omap_usb_exit(struct phy *x)
202 struct omap_usb *phy = phy_get_drvdata(x);
204 return omap_usb2_disable_clocks(phy);
207 static const struct phy_ops ops = {
208 .init = omap_usb_init,
209 .exit = omap_usb_exit,
210 .power_on = omap_usb_power_on,
211 .power_off = omap_usb_power_off,
212 .owner = THIS_MODULE,
215 static const struct usb_phy_data omap_usb2_data = {
216 .label = "omap_usb2",
217 .flags = OMAP_USB2_HAS_START_SRP | OMAP_USB2_HAS_SET_VBUS,
218 .mask = OMAP_DEV_PHY_PD,
219 .power_off = OMAP_DEV_PHY_PD,
222 static const struct usb_phy_data omap5_usb2_data = {
223 .label = "omap5_usb2",
224 .flags = 0,
225 .mask = OMAP_DEV_PHY_PD,
226 .power_off = OMAP_DEV_PHY_PD,
229 static const struct usb_phy_data dra7x_usb2_data = {
230 .label = "dra7x_usb2",
231 .flags = OMAP_USB2_CALIBRATE_FALSE_DISCONNECT,
232 .mask = OMAP_DEV_PHY_PD,
233 .power_off = OMAP_DEV_PHY_PD,
236 static const struct usb_phy_data dra7x_usb2_phy2_data = {
237 .label = "dra7x_usb2_phy2",
238 .flags = OMAP_USB2_CALIBRATE_FALSE_DISCONNECT,
239 .mask = OMAP_USB2_PHY_PD,
240 .power_off = OMAP_USB2_PHY_PD,
243 static const struct usb_phy_data am437x_usb2_data = {
244 .label = "am437x_usb2",
245 .flags = 0,
246 .mask = AM437X_USB2_PHY_PD | AM437X_USB2_OTG_PD |
247 AM437X_USB2_OTGVDET_EN | AM437X_USB2_OTGSESSEND_EN,
248 .power_on = AM437X_USB2_OTGVDET_EN | AM437X_USB2_OTGSESSEND_EN,
249 .power_off = AM437X_USB2_PHY_PD | AM437X_USB2_OTG_PD,
252 static const struct usb_phy_data am654_usb2_data = {
253 .label = "am654_usb2",
254 .flags = OMAP_USB2_CALIBRATE_FALSE_DISCONNECT,
255 .mask = AM654_USB2_OTG_PD | AM654_USB2_VBUS_DET_EN |
256 AM654_USB2_VBUSVALID_DET_EN,
257 .power_on = AM654_USB2_VBUS_DET_EN | AM654_USB2_VBUSVALID_DET_EN,
258 .power_off = AM654_USB2_OTG_PD,
261 static const struct of_device_id omap_usb2_id_table[] = {
263 .compatible = "ti,omap-usb2",
264 .data = &omap_usb2_data,
267 .compatible = "ti,omap5-usb2",
268 .data = &omap5_usb2_data,
271 .compatible = "ti,dra7x-usb2",
272 .data = &dra7x_usb2_data,
275 .compatible = "ti,dra7x-usb2-phy2",
276 .data = &dra7x_usb2_phy2_data,
279 .compatible = "ti,am437x-usb2",
280 .data = &am437x_usb2_data,
283 .compatible = "ti,am654-usb2",
284 .data = &am654_usb2_data,
288 MODULE_DEVICE_TABLE(of, omap_usb2_id_table);
290 static int omap_usb2_probe(struct platform_device *pdev)
292 struct omap_usb *phy;
293 struct phy *generic_phy;
294 struct resource *res;
295 struct phy_provider *phy_provider;
296 struct usb_otg *otg;
297 struct device_node *node = pdev->dev.of_node;
298 struct device_node *control_node;
299 struct platform_device *control_pdev;
300 const struct of_device_id *of_id;
301 struct usb_phy_data *phy_data;
303 of_id = of_match_device(omap_usb2_id_table, &pdev->dev);
305 if (!of_id)
306 return -EINVAL;
308 phy_data = (struct usb_phy_data *)of_id->data;
310 phy = devm_kzalloc(&pdev->dev, sizeof(*phy), GFP_KERNEL);
311 if (!phy)
312 return -ENOMEM;
314 otg = devm_kzalloc(&pdev->dev, sizeof(*otg), GFP_KERNEL);
315 if (!otg)
316 return -ENOMEM;
318 phy->dev = &pdev->dev;
320 phy->phy.dev = phy->dev;
321 phy->phy.label = phy_data->label;
322 phy->phy.otg = otg;
323 phy->phy.type = USB_PHY_TYPE_USB2;
324 phy->mask = phy_data->mask;
325 phy->power_on = phy_data->power_on;
326 phy->power_off = phy_data->power_off;
328 if (phy_data->flags & OMAP_USB2_CALIBRATE_FALSE_DISCONNECT) {
329 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
330 phy->phy_base = devm_ioremap_resource(&pdev->dev, res);
331 if (IS_ERR(phy->phy_base))
332 return PTR_ERR(phy->phy_base);
333 phy->flags |= OMAP_USB2_CALIBRATE_FALSE_DISCONNECT;
336 phy->syscon_phy_power = syscon_regmap_lookup_by_phandle(node,
337 "syscon-phy-power");
338 if (IS_ERR(phy->syscon_phy_power)) {
339 dev_dbg(&pdev->dev,
340 "can't get syscon-phy-power, using control device\n");
341 phy->syscon_phy_power = NULL;
343 control_node = of_parse_phandle(node, "ctrl-module", 0);
344 if (!control_node) {
345 dev_err(&pdev->dev,
346 "Failed to get control device phandle\n");
347 return -EINVAL;
350 control_pdev = of_find_device_by_node(control_node);
351 if (!control_pdev) {
352 dev_err(&pdev->dev, "Failed to get control device\n");
353 return -EINVAL;
355 phy->control_dev = &control_pdev->dev;
356 } else {
357 if (of_property_read_u32_index(node,
358 "syscon-phy-power", 1,
359 &phy->power_reg)) {
360 dev_err(&pdev->dev,
361 "couldn't get power reg. offset\n");
362 return -EINVAL;
367 phy->wkupclk = devm_clk_get(phy->dev, "wkupclk");
368 if (IS_ERR(phy->wkupclk)) {
369 if (PTR_ERR(phy->wkupclk) == -EPROBE_DEFER)
370 return -EPROBE_DEFER;
372 dev_warn(&pdev->dev, "unable to get wkupclk %ld, trying old name\n",
373 PTR_ERR(phy->wkupclk));
374 phy->wkupclk = devm_clk_get(phy->dev, "usb_phy_cm_clk32k");
376 if (IS_ERR(phy->wkupclk)) {
377 if (PTR_ERR(phy->wkupclk) != -EPROBE_DEFER)
378 dev_err(&pdev->dev, "unable to get usb_phy_cm_clk32k\n");
379 return PTR_ERR(phy->wkupclk);
380 } else {
381 dev_warn(&pdev->dev,
382 "found usb_phy_cm_clk32k, please fix DTS\n");
386 phy->optclk = devm_clk_get(phy->dev, "refclk");
387 if (IS_ERR(phy->optclk)) {
388 if (PTR_ERR(phy->optclk) == -EPROBE_DEFER)
389 return -EPROBE_DEFER;
391 dev_dbg(&pdev->dev, "unable to get refclk, trying old name\n");
392 phy->optclk = devm_clk_get(phy->dev, "usb_otg_ss_refclk960m");
394 if (IS_ERR(phy->optclk)) {
395 if (PTR_ERR(phy->optclk) != -EPROBE_DEFER) {
396 dev_dbg(&pdev->dev,
397 "unable to get usb_otg_ss_refclk960m\n");
399 } else {
400 dev_warn(&pdev->dev,
401 "found usb_otg_ss_refclk960m, please fix DTS\n");
405 otg->set_host = omap_usb_set_host;
406 otg->set_peripheral = omap_usb_set_peripheral;
407 if (phy_data->flags & OMAP_USB2_HAS_SET_VBUS)
408 otg->set_vbus = omap_usb_set_vbus;
409 if (phy_data->flags & OMAP_USB2_HAS_START_SRP)
410 otg->start_srp = omap_usb_start_srp;
411 otg->usb_phy = &phy->phy;
413 platform_set_drvdata(pdev, phy);
414 pm_runtime_enable(phy->dev);
416 generic_phy = devm_phy_create(phy->dev, NULL, &ops);
417 if (IS_ERR(generic_phy)) {
418 pm_runtime_disable(phy->dev);
419 return PTR_ERR(generic_phy);
422 phy_set_drvdata(generic_phy, phy);
423 omap_usb_power_off(generic_phy);
425 phy_provider = devm_of_phy_provider_register(phy->dev,
426 of_phy_simple_xlate);
427 if (IS_ERR(phy_provider)) {
428 pm_runtime_disable(phy->dev);
429 return PTR_ERR(phy_provider);
433 usb_add_phy_dev(&phy->phy);
435 return 0;
438 static int omap_usb2_remove(struct platform_device *pdev)
440 struct omap_usb *phy = platform_get_drvdata(pdev);
442 usb_remove_phy(&phy->phy);
443 pm_runtime_disable(phy->dev);
445 return 0;
448 static struct platform_driver omap_usb2_driver = {
449 .probe = omap_usb2_probe,
450 .remove = omap_usb2_remove,
451 .driver = {
452 .name = "omap-usb2",
453 .of_match_table = omap_usb2_id_table,
457 module_platform_driver(omap_usb2_driver);
459 MODULE_ALIAS("platform:omap_usb2");
460 MODULE_AUTHOR("Texas Instruments Inc.");
461 MODULE_DESCRIPTION("OMAP USB2 phy driver");
462 MODULE_LICENSE("GPL v2");