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>
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
40 * omap_usb2_set_comparator - links the comparator present in the sytem with
42 * @comparator - the companion phy(comparator) for this phy
44 * The phy companion driver should call this API passing the phy_companion
45 * filled with set_vbus and start_srp to be used by usb phy.
47 * For use by phy companion driver
49 int omap_usb2_set_comparator(struct phy_companion *comparator)
52 struct usb_phy *x = usb_get_phy(USB_PHY_TYPE_USB2);
57 phy = phy_to_omapusb(x);
58 phy->comparator = comparator;
61 EXPORT_SYMBOL_GPL(omap_usb2_set_comparator);
63 static int omap_usb_set_vbus(struct usb_otg *otg, bool enabled)
65 struct omap_usb *phy = phy_to_omapusb(otg->usb_phy);
70 return phy->comparator->set_vbus(phy->comparator, enabled);
73 static int omap_usb_start_srp(struct usb_otg *otg)
75 struct omap_usb *phy = phy_to_omapusb(otg->usb_phy);
80 return phy->comparator->start_srp(phy->comparator);
83 static int omap_usb_set_host(struct usb_otg *otg, struct usb_bus *host)
87 otg->state = OTG_STATE_UNDEFINED;
92 static int omap_usb_set_peripheral(struct usb_otg *otg,
93 struct usb_gadget *gadget)
97 otg->state = OTG_STATE_UNDEFINED;
102 static int omap_usb_phy_power(struct omap_usb *phy, int on)
107 if (!phy->syscon_phy_power) {
108 omap_control_phy_power(phy->control_dev, on);
115 val = phy->power_off;
117 ret = regmap_update_bits(phy->syscon_phy_power, phy->power_reg,
122 static int omap_usb_power_off(struct phy *x)
124 struct omap_usb *phy = phy_get_drvdata(x);
126 return omap_usb_phy_power(phy, false);
129 static int omap_usb_power_on(struct phy *x)
131 struct omap_usb *phy = phy_get_drvdata(x);
133 return omap_usb_phy_power(phy, true);
136 static int omap_usb_init(struct phy *x)
138 struct omap_usb *phy = phy_get_drvdata(x);
141 if (phy->flags & OMAP_USB2_CALIBRATE_FALSE_DISCONNECT) {
144 * Reduce the sensitivity of internal PHY by enabling the
145 * DISCON_BYP_LATCH of the USB2PHY_ANA_CONFIG1 register. This
146 * resolves issues with certain devices which can otherwise
147 * be prone to false disconnects.
150 val = omap_usb_readl(phy->phy_base, USB2PHY_ANA_CONFIG1);
151 val |= USB2PHY_DISCON_BYP_LATCH;
152 omap_usb_writel(phy->phy_base, USB2PHY_ANA_CONFIG1, val);
158 static const struct phy_ops ops = {
159 .init = omap_usb_init,
160 .power_on = omap_usb_power_on,
161 .power_off = omap_usb_power_off,
162 .owner = THIS_MODULE,
165 static const struct usb_phy_data omap_usb2_data = {
166 .label = "omap_usb2",
167 .flags = OMAP_USB2_HAS_START_SRP | OMAP_USB2_HAS_SET_VBUS,
168 .mask = OMAP_DEV_PHY_PD,
169 .power_off = OMAP_DEV_PHY_PD,
172 static const struct usb_phy_data omap5_usb2_data = {
173 .label = "omap5_usb2",
175 .mask = OMAP_DEV_PHY_PD,
176 .power_off = OMAP_DEV_PHY_PD,
179 static const struct usb_phy_data dra7x_usb2_data = {
180 .label = "dra7x_usb2",
181 .flags = OMAP_USB2_CALIBRATE_FALSE_DISCONNECT,
182 .mask = OMAP_DEV_PHY_PD,
183 .power_off = OMAP_DEV_PHY_PD,
186 static const struct usb_phy_data dra7x_usb2_phy2_data = {
187 .label = "dra7x_usb2_phy2",
188 .flags = OMAP_USB2_CALIBRATE_FALSE_DISCONNECT,
189 .mask = OMAP_USB2_PHY_PD,
190 .power_off = OMAP_USB2_PHY_PD,
193 static const struct usb_phy_data am437x_usb2_data = {
194 .label = "am437x_usb2",
196 .mask = AM437X_USB2_PHY_PD | AM437X_USB2_OTG_PD |
197 AM437X_USB2_OTGVDET_EN | AM437X_USB2_OTGSESSEND_EN,
198 .power_on = AM437X_USB2_OTGVDET_EN | AM437X_USB2_OTGSESSEND_EN,
199 .power_off = AM437X_USB2_PHY_PD | AM437X_USB2_OTG_PD,
202 static const struct of_device_id omap_usb2_id_table[] = {
204 .compatible = "ti,omap-usb2",
205 .data = &omap_usb2_data,
208 .compatible = "ti,omap5-usb2",
209 .data = &omap5_usb2_data,
212 .compatible = "ti,dra7x-usb2",
213 .data = &dra7x_usb2_data,
216 .compatible = "ti,dra7x-usb2-phy2",
217 .data = &dra7x_usb2_phy2_data,
220 .compatible = "ti,am437x-usb2",
221 .data = &am437x_usb2_data,
225 MODULE_DEVICE_TABLE(of, omap_usb2_id_table);
227 static int omap_usb2_probe(struct platform_device *pdev)
229 struct omap_usb *phy;
230 struct phy *generic_phy;
231 struct resource *res;
232 struct phy_provider *phy_provider;
234 struct device_node *node = pdev->dev.of_node;
235 struct device_node *control_node;
236 struct platform_device *control_pdev;
237 const struct of_device_id *of_id;
238 struct usb_phy_data *phy_data;
240 of_id = of_match_device(omap_usb2_id_table, &pdev->dev);
245 phy_data = (struct usb_phy_data *)of_id->data;
247 phy = devm_kzalloc(&pdev->dev, sizeof(*phy), GFP_KERNEL);
251 otg = devm_kzalloc(&pdev->dev, sizeof(*otg), GFP_KERNEL);
255 phy->dev = &pdev->dev;
257 phy->phy.dev = phy->dev;
258 phy->phy.label = phy_data->label;
260 phy->phy.type = USB_PHY_TYPE_USB2;
261 phy->mask = phy_data->mask;
262 phy->power_on = phy_data->power_on;
263 phy->power_off = phy_data->power_off;
265 if (phy_data->flags & OMAP_USB2_CALIBRATE_FALSE_DISCONNECT) {
266 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
267 phy->phy_base = devm_ioremap_resource(&pdev->dev, res);
268 if (IS_ERR(phy->phy_base))
269 return PTR_ERR(phy->phy_base);
270 phy->flags |= OMAP_USB2_CALIBRATE_FALSE_DISCONNECT;
273 phy->syscon_phy_power = syscon_regmap_lookup_by_phandle(node,
275 if (IS_ERR(phy->syscon_phy_power)) {
277 "can't get syscon-phy-power, using control device\n");
278 phy->syscon_phy_power = NULL;
280 control_node = of_parse_phandle(node, "ctrl-module", 0);
283 "Failed to get control device phandle\n");
287 control_pdev = of_find_device_by_node(control_node);
289 dev_err(&pdev->dev, "Failed to get control device\n");
292 phy->control_dev = &control_pdev->dev;
294 if (of_property_read_u32_index(node,
295 "syscon-phy-power", 1,
298 "couldn't get power reg. offset\n");
303 otg->set_host = omap_usb_set_host;
304 otg->set_peripheral = omap_usb_set_peripheral;
305 if (phy_data->flags & OMAP_USB2_HAS_SET_VBUS)
306 otg->set_vbus = omap_usb_set_vbus;
307 if (phy_data->flags & OMAP_USB2_HAS_START_SRP)
308 otg->start_srp = omap_usb_start_srp;
309 otg->usb_phy = &phy->phy;
311 platform_set_drvdata(pdev, phy);
312 pm_runtime_enable(phy->dev);
314 generic_phy = devm_phy_create(phy->dev, NULL, &ops);
315 if (IS_ERR(generic_phy)) {
316 pm_runtime_disable(phy->dev);
317 return PTR_ERR(generic_phy);
320 phy_set_drvdata(generic_phy, phy);
321 omap_usb_power_off(generic_phy);
323 phy_provider = devm_of_phy_provider_register(phy->dev,
324 of_phy_simple_xlate);
325 if (IS_ERR(phy_provider)) {
326 pm_runtime_disable(phy->dev);
327 return PTR_ERR(phy_provider);
330 phy->wkupclk = devm_clk_get(phy->dev, "wkupclk");
331 if (IS_ERR(phy->wkupclk)) {
332 dev_warn(&pdev->dev, "unable to get wkupclk, trying old name\n");
333 phy->wkupclk = devm_clk_get(phy->dev, "usb_phy_cm_clk32k");
334 if (IS_ERR(phy->wkupclk)) {
335 dev_err(&pdev->dev, "unable to get usb_phy_cm_clk32k\n");
336 pm_runtime_disable(phy->dev);
337 return PTR_ERR(phy->wkupclk);
340 "found usb_phy_cm_clk32k, please fix DTS\n");
343 clk_prepare(phy->wkupclk);
345 phy->optclk = devm_clk_get(phy->dev, "refclk");
346 if (IS_ERR(phy->optclk)) {
347 dev_dbg(&pdev->dev, "unable to get refclk, trying old name\n");
348 phy->optclk = devm_clk_get(phy->dev, "usb_otg_ss_refclk960m");
349 if (IS_ERR(phy->optclk)) {
351 "unable to get usb_otg_ss_refclk960m\n");
354 "found usb_otg_ss_refclk960m, please fix DTS\n");
358 if (!IS_ERR(phy->optclk))
359 clk_prepare(phy->optclk);
361 usb_add_phy_dev(&phy->phy);
366 static int omap_usb2_remove(struct platform_device *pdev)
368 struct omap_usb *phy = platform_get_drvdata(pdev);
370 clk_unprepare(phy->wkupclk);
371 if (!IS_ERR(phy->optclk))
372 clk_unprepare(phy->optclk);
373 usb_remove_phy(&phy->phy);
374 pm_runtime_disable(phy->dev);
381 static int omap_usb2_runtime_suspend(struct device *dev)
383 struct platform_device *pdev = to_platform_device(dev);
384 struct omap_usb *phy = platform_get_drvdata(pdev);
386 clk_disable(phy->wkupclk);
387 if (!IS_ERR(phy->optclk))
388 clk_disable(phy->optclk);
393 static int omap_usb2_runtime_resume(struct device *dev)
395 struct platform_device *pdev = to_platform_device(dev);
396 struct omap_usb *phy = platform_get_drvdata(pdev);
399 ret = clk_enable(phy->wkupclk);
401 dev_err(phy->dev, "Failed to enable wkupclk %d\n", ret);
405 if (!IS_ERR(phy->optclk)) {
406 ret = clk_enable(phy->optclk);
408 dev_err(phy->dev, "Failed to enable optclk %d\n", ret);
416 clk_disable(phy->wkupclk);
422 static const struct dev_pm_ops omap_usb2_pm_ops = {
423 SET_RUNTIME_PM_OPS(omap_usb2_runtime_suspend, omap_usb2_runtime_resume,
427 #define DEV_PM_OPS (&omap_usb2_pm_ops)
429 #define DEV_PM_OPS NULL
432 static struct platform_driver omap_usb2_driver = {
433 .probe = omap_usb2_probe,
434 .remove = omap_usb2_remove,
438 .of_match_table = omap_usb2_id_table,
442 module_platform_driver(omap_usb2_driver);
444 MODULE_ALIAS("platform:omap_usb2");
445 MODULE_AUTHOR("Texas Instruments Inc.");
446 MODULE_DESCRIPTION("OMAP USB2 phy driver");
447 MODULE_LICENSE("GPL v2");