Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 #include <linux/module.h>
0003 #include <linux/platform_device.h>
0004 #include <linux/err.h>
0005 #include <linux/of.h>
0006 #include <linux/io.h>
0007 #include <linux/delay.h>
0008 #include <linux/usb/otg.h>
0009 #include "phy-am335x-control.h"
0010 
0011 struct am335x_control_usb {
0012     struct device *dev;
0013     void __iomem *phy_reg;
0014     void __iomem *wkup;
0015     spinlock_t lock;
0016     struct phy_control phy_ctrl;
0017 };
0018 
0019 #define AM335X_USB0_CTRL        0x0
0020 #define AM335X_USB1_CTRL        0x8
0021 #define AM335x_USB_WKUP         0x0
0022 
0023 #define USBPHY_CM_PWRDN     (1 << 0)
0024 #define USBPHY_OTG_PWRDN    (1 << 1)
0025 #define USBPHY_OTGVDET_EN   (1 << 19)
0026 #define USBPHY_OTGSESSEND_EN    (1 << 20)
0027 
0028 #define AM335X_PHY0_WK_EN   (1 << 0)
0029 #define AM335X_PHY1_WK_EN   (1 << 8)
0030 
0031 static void am335x_phy_wkup(struct  phy_control *phy_ctrl, u32 id, bool on)
0032 {
0033     struct am335x_control_usb *usb_ctrl;
0034     u32 val;
0035     u32 reg;
0036 
0037     usb_ctrl = container_of(phy_ctrl, struct am335x_control_usb, phy_ctrl);
0038 
0039     switch (id) {
0040     case 0:
0041         reg = AM335X_PHY0_WK_EN;
0042         break;
0043     case 1:
0044         reg = AM335X_PHY1_WK_EN;
0045         break;
0046     default:
0047         WARN_ON(1);
0048         return;
0049     }
0050 
0051     spin_lock(&usb_ctrl->lock);
0052     val = readl(usb_ctrl->wkup);
0053 
0054     if (on)
0055         val |= reg;
0056     else
0057         val &= ~reg;
0058 
0059     writel(val, usb_ctrl->wkup);
0060     spin_unlock(&usb_ctrl->lock);
0061 }
0062 
0063 static void am335x_phy_power(struct phy_control *phy_ctrl, u32 id,
0064                 enum usb_dr_mode dr_mode, bool on)
0065 {
0066     struct am335x_control_usb *usb_ctrl;
0067     u32 val;
0068     u32 reg;
0069 
0070     usb_ctrl = container_of(phy_ctrl, struct am335x_control_usb, phy_ctrl);
0071 
0072     switch (id) {
0073     case 0:
0074         reg = AM335X_USB0_CTRL;
0075         break;
0076     case 1:
0077         reg = AM335X_USB1_CTRL;
0078         break;
0079     default:
0080         WARN_ON(1);
0081         return;
0082     }
0083 
0084     val = readl(usb_ctrl->phy_reg + reg);
0085     if (on) {
0086         if (dr_mode == USB_DR_MODE_HOST) {
0087             val &= ~(USBPHY_CM_PWRDN | USBPHY_OTG_PWRDN |
0088                     USBPHY_OTGVDET_EN);
0089             val |= USBPHY_OTGSESSEND_EN;
0090         } else {
0091             val &= ~(USBPHY_CM_PWRDN | USBPHY_OTG_PWRDN);
0092             val |= USBPHY_OTGVDET_EN | USBPHY_OTGSESSEND_EN;
0093         }
0094     } else {
0095         val |= USBPHY_CM_PWRDN | USBPHY_OTG_PWRDN;
0096     }
0097 
0098     writel(val, usb_ctrl->phy_reg + reg);
0099 
0100     /*
0101      * Give the PHY ~1ms to complete the power up operation.
0102      * Tests have shown unstable behaviour if other USB PHY related
0103      * registers are written too shortly after such a transition.
0104      */
0105     if (on)
0106         mdelay(1);
0107 }
0108 
0109 static const struct phy_control ctrl_am335x = {
0110     .phy_power = am335x_phy_power,
0111     .phy_wkup = am335x_phy_wkup,
0112 };
0113 
0114 static const struct of_device_id omap_control_usb_id_table[] = {
0115     { .compatible = "ti,am335x-usb-ctrl-module", .data = &ctrl_am335x },
0116     {}
0117 };
0118 MODULE_DEVICE_TABLE(of, omap_control_usb_id_table);
0119 
0120 static struct platform_driver am335x_control_driver;
0121 static int match(struct device *dev, const void *data)
0122 {
0123     const struct device_node *node = (const struct device_node *)data;
0124     return dev->of_node == node &&
0125         dev->driver == &am335x_control_driver.driver;
0126 }
0127 
0128 struct phy_control *am335x_get_phy_control(struct device *dev)
0129 {
0130     struct device_node *node;
0131     struct am335x_control_usb *ctrl_usb;
0132 
0133     node = of_parse_phandle(dev->of_node, "ti,ctrl_mod", 0);
0134     if (!node)
0135         return NULL;
0136 
0137     dev = bus_find_device(&platform_bus_type, NULL, node, match);
0138     of_node_put(node);
0139     if (!dev)
0140         return NULL;
0141 
0142     ctrl_usb = dev_get_drvdata(dev);
0143     put_device(dev);
0144     if (!ctrl_usb)
0145         return NULL;
0146     return &ctrl_usb->phy_ctrl;
0147 }
0148 EXPORT_SYMBOL_GPL(am335x_get_phy_control);
0149 
0150 static int am335x_control_usb_probe(struct platform_device *pdev)
0151 {
0152     struct am335x_control_usb *ctrl_usb;
0153     const struct of_device_id *of_id;
0154     const struct phy_control *phy_ctrl;
0155 
0156     of_id = of_match_node(omap_control_usb_id_table, pdev->dev.of_node);
0157     if (!of_id)
0158         return -EINVAL;
0159 
0160     phy_ctrl = of_id->data;
0161 
0162     ctrl_usb = devm_kzalloc(&pdev->dev, sizeof(*ctrl_usb), GFP_KERNEL);
0163     if (!ctrl_usb)
0164         return -ENOMEM;
0165 
0166     ctrl_usb->dev = &pdev->dev;
0167 
0168     ctrl_usb->phy_reg = devm_platform_ioremap_resource_byname(pdev, "phy_ctrl");
0169     if (IS_ERR(ctrl_usb->phy_reg))
0170         return PTR_ERR(ctrl_usb->phy_reg);
0171 
0172     ctrl_usb->wkup = devm_platform_ioremap_resource_byname(pdev, "wakeup");
0173     if (IS_ERR(ctrl_usb->wkup))
0174         return PTR_ERR(ctrl_usb->wkup);
0175 
0176     spin_lock_init(&ctrl_usb->lock);
0177     ctrl_usb->phy_ctrl = *phy_ctrl;
0178 
0179     dev_set_drvdata(ctrl_usb->dev, ctrl_usb);
0180     return 0;
0181 }
0182 
0183 static struct platform_driver am335x_control_driver = {
0184     .probe      = am335x_control_usb_probe,
0185     .driver     = {
0186         .name   = "am335x-control-usb",
0187         .of_match_table = omap_control_usb_id_table,
0188     },
0189 };
0190 
0191 module_platform_driver(am335x_control_driver);
0192 MODULE_LICENSE("GPL v2");