1 #include <linux/module.h> 2 #include <linux/platform_device.h> 3 #include <linux/err.h> 4 #include <linux/of.h> 5 #include <linux/io.h> 6 #include "am35x-phy-control.h" 7 8 struct am335x_control_usb { 9 struct device *dev; 10 void __iomem *phy_reg; 11 void __iomem *wkup; 12 spinlock_t lock; 13 struct phy_control phy_ctrl; 14 }; 15 16 #define AM335X_USB0_CTRL 0x0 17 #define AM335X_USB1_CTRL 0x8 18 #define AM335x_USB_WKUP 0x0 19 20 #define USBPHY_CM_PWRDN (1 << 0) 21 #define USBPHY_OTG_PWRDN (1 << 1) 22 #define USBPHY_OTGVDET_EN (1 << 19) 23 #define USBPHY_OTGSESSEND_EN (1 << 20) 24 25 #define AM335X_PHY0_WK_EN (1 << 0) 26 #define AM335X_PHY1_WK_EN (1 << 8) 27 28 static void am335x_phy_wkup(struct phy_control *phy_ctrl, u32 id, bool on) 29 { 30 struct am335x_control_usb *usb_ctrl; 31 u32 val; 32 u32 reg; 33 34 usb_ctrl = container_of(phy_ctrl, struct am335x_control_usb, phy_ctrl); 35 36 switch (id) { 37 case 0: 38 reg = AM335X_PHY0_WK_EN; 39 break; 40 case 1: 41 reg = AM335X_PHY1_WK_EN; 42 break; 43 default: 44 WARN_ON(1); 45 return; 46 } 47 48 spin_lock(&usb_ctrl->lock); 49 val = readl(usb_ctrl->wkup); 50 51 if (on) 52 val |= reg; 53 else 54 val &= ~reg; 55 56 writel(val, usb_ctrl->wkup); 57 spin_unlock(&usb_ctrl->lock); 58 } 59 60 static void am335x_phy_power(struct phy_control *phy_ctrl, u32 id, bool on) 61 { 62 struct am335x_control_usb *usb_ctrl; 63 u32 val; 64 u32 reg; 65 66 usb_ctrl = container_of(phy_ctrl, struct am335x_control_usb, phy_ctrl); 67 68 switch (id) { 69 case 0: 70 reg = AM335X_USB0_CTRL; 71 break; 72 case 1: 73 reg = AM335X_USB1_CTRL; 74 break; 75 default: 76 WARN_ON(1); 77 return; 78 } 79 80 val = readl(usb_ctrl->phy_reg + reg); 81 if (on) { 82 val &= ~(USBPHY_CM_PWRDN | USBPHY_OTG_PWRDN); 83 val |= USBPHY_OTGVDET_EN | USBPHY_OTGSESSEND_EN; 84 } else { 85 val |= USBPHY_CM_PWRDN | USBPHY_OTG_PWRDN; 86 } 87 88 writel(val, usb_ctrl->phy_reg + reg); 89 } 90 91 static const struct phy_control ctrl_am335x = { 92 .phy_power = am335x_phy_power, 93 .phy_wkup = am335x_phy_wkup, 94 }; 95 96 static const struct of_device_id omap_control_usb_id_table[] = { 97 { .compatible = "ti,am335x-usb-ctrl-module", .data = &ctrl_am335x }, 98 {} 99 }; 100 MODULE_DEVICE_TABLE(of, omap_control_usb_id_table); 101 102 static struct platform_driver am335x_control_driver; 103 static int match(struct device *dev, void *data) 104 { 105 struct device_node *node = (struct device_node *)data; 106 return dev->of_node == node && 107 dev->driver == &am335x_control_driver.driver; 108 } 109 110 struct phy_control *am335x_get_phy_control(struct device *dev) 111 { 112 struct device_node *node; 113 struct am335x_control_usb *ctrl_usb; 114 115 node = of_parse_phandle(dev->of_node, "ti,ctrl_mod", 0); 116 if (!node) 117 return NULL; 118 119 dev = bus_find_device(&platform_bus_type, NULL, node, match); 120 ctrl_usb = dev_get_drvdata(dev); 121 if (!ctrl_usb) 122 return NULL; 123 return &ctrl_usb->phy_ctrl; 124 } 125 EXPORT_SYMBOL_GPL(am335x_get_phy_control); 126 127 static int am335x_control_usb_probe(struct platform_device *pdev) 128 { 129 struct resource *res; 130 struct am335x_control_usb *ctrl_usb; 131 const struct of_device_id *of_id; 132 const struct phy_control *phy_ctrl; 133 134 of_id = of_match_node(omap_control_usb_id_table, pdev->dev.of_node); 135 if (!of_id) 136 return -EINVAL; 137 138 phy_ctrl = of_id->data; 139 140 ctrl_usb = devm_kzalloc(&pdev->dev, sizeof(*ctrl_usb), GFP_KERNEL); 141 if (!ctrl_usb) { 142 dev_err(&pdev->dev, "unable to alloc memory for control usb\n"); 143 return -ENOMEM; 144 } 145 146 ctrl_usb->dev = &pdev->dev; 147 148 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "phy_ctrl"); 149 ctrl_usb->phy_reg = devm_ioremap_resource(&pdev->dev, res); 150 if (IS_ERR(ctrl_usb->phy_reg)) 151 return PTR_ERR(ctrl_usb->phy_reg); 152 153 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "wakeup"); 154 ctrl_usb->wkup = devm_ioremap_resource(&pdev->dev, res); 155 if (IS_ERR(ctrl_usb->wkup)) 156 return PTR_ERR(ctrl_usb->wkup); 157 158 spin_lock_init(&ctrl_usb->lock); 159 ctrl_usb->phy_ctrl = *phy_ctrl; 160 161 dev_set_drvdata(ctrl_usb->dev, ctrl_usb); 162 return 0; 163 } 164 165 static struct platform_driver am335x_control_driver = { 166 .probe = am335x_control_usb_probe, 167 .driver = { 168 .name = "am335x-control-usb", 169 .owner = THIS_MODULE, 170 .of_match_table = omap_control_usb_id_table, 171 }, 172 }; 173 174 module_platform_driver(am335x_control_driver); 175 MODULE_LICENSE("GPL v2"); 176