Sebastian Andrzej Siewior | 3bb869c | 2013-07-30 21:43:45 +0200 | [diff] [blame] | 1 | #include <linux/module.h> |
| 2 | #include <linux/platform_device.h> |
Sebastian Andrzej Siewior | 8b841cb | 2013-08-13 10:57:08 +0200 | [diff] [blame] | 3 | #include <linux/err.h> |
Sebastian Andrzej Siewior | 3bb869c | 2013-07-30 21:43:45 +0200 | [diff] [blame] | 4 | #include <linux/of.h> |
| 5 | #include <linux/io.h> |
Daniel Mack | a31a942 | 2014-04-16 17:11:16 +0200 | [diff] [blame] | 6 | #include <linux/delay.h> |
Bin Liu | 59f042f | 2015-12-08 10:31:50 -0600 | [diff] [blame] | 7 | #include <linux/usb/otg.h> |
Bin Liu | 5306661 | 2015-11-20 16:13:07 -0600 | [diff] [blame] | 8 | #include "phy-am335x-control.h" |
Sebastian Andrzej Siewior | 3bb869c | 2013-07-30 21:43:45 +0200 | [diff] [blame] | 9 | |
| 10 | struct am335x_control_usb { |
| 11 | struct device *dev; |
| 12 | void __iomem *phy_reg; |
| 13 | void __iomem *wkup; |
| 14 | spinlock_t lock; |
| 15 | struct phy_control phy_ctrl; |
| 16 | }; |
| 17 | |
| 18 | #define AM335X_USB0_CTRL 0x0 |
| 19 | #define AM335X_USB1_CTRL 0x8 |
| 20 | #define AM335x_USB_WKUP 0x0 |
| 21 | |
| 22 | #define USBPHY_CM_PWRDN (1 << 0) |
| 23 | #define USBPHY_OTG_PWRDN (1 << 1) |
| 24 | #define USBPHY_OTGVDET_EN (1 << 19) |
| 25 | #define USBPHY_OTGSESSEND_EN (1 << 20) |
| 26 | |
Sebastian Andrzej Siewior | a122263 | 2013-08-19 12:39:44 +0200 | [diff] [blame] | 27 | #define AM335X_PHY0_WK_EN (1 << 0) |
| 28 | #define AM335X_PHY1_WK_EN (1 << 8) |
| 29 | |
| 30 | static void am335x_phy_wkup(struct phy_control *phy_ctrl, u32 id, bool on) |
| 31 | { |
| 32 | struct am335x_control_usb *usb_ctrl; |
| 33 | u32 val; |
| 34 | u32 reg; |
| 35 | |
| 36 | usb_ctrl = container_of(phy_ctrl, struct am335x_control_usb, phy_ctrl); |
| 37 | |
| 38 | switch (id) { |
| 39 | case 0: |
| 40 | reg = AM335X_PHY0_WK_EN; |
| 41 | break; |
| 42 | case 1: |
| 43 | reg = AM335X_PHY1_WK_EN; |
| 44 | break; |
| 45 | default: |
| 46 | WARN_ON(1); |
| 47 | return; |
| 48 | } |
| 49 | |
| 50 | spin_lock(&usb_ctrl->lock); |
| 51 | val = readl(usb_ctrl->wkup); |
| 52 | |
| 53 | if (on) |
| 54 | val |= reg; |
| 55 | else |
| 56 | val &= ~reg; |
| 57 | |
| 58 | writel(val, usb_ctrl->wkup); |
| 59 | spin_unlock(&usb_ctrl->lock); |
| 60 | } |
| 61 | |
Bin Liu | 59f042f | 2015-12-08 10:31:50 -0600 | [diff] [blame] | 62 | static void am335x_phy_power(struct phy_control *phy_ctrl, u32 id, |
| 63 | enum usb_dr_mode dr_mode, bool on) |
Sebastian Andrzej Siewior | 3bb869c | 2013-07-30 21:43:45 +0200 | [diff] [blame] | 64 | { |
| 65 | struct am335x_control_usb *usb_ctrl; |
| 66 | u32 val; |
| 67 | u32 reg; |
| 68 | |
| 69 | usb_ctrl = container_of(phy_ctrl, struct am335x_control_usb, phy_ctrl); |
| 70 | |
| 71 | switch (id) { |
| 72 | case 0: |
| 73 | reg = AM335X_USB0_CTRL; |
| 74 | break; |
| 75 | case 1: |
| 76 | reg = AM335X_USB1_CTRL; |
| 77 | break; |
| 78 | default: |
Sebastian Andrzej Siewior | 4ff7457 | 2013-08-16 17:32:49 +0200 | [diff] [blame] | 79 | WARN_ON(1); |
| 80 | return; |
Sebastian Andrzej Siewior | 3bb869c | 2013-07-30 21:43:45 +0200 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | val = readl(usb_ctrl->phy_reg + reg); |
| 84 | if (on) { |
Bin Liu | 59f042f | 2015-12-08 10:31:50 -0600 | [diff] [blame] | 85 | if (dr_mode == USB_DR_MODE_HOST) { |
| 86 | val &= ~(USBPHY_CM_PWRDN | USBPHY_OTG_PWRDN | |
| 87 | USBPHY_OTGVDET_EN); |
| 88 | val |= USBPHY_OTGSESSEND_EN; |
| 89 | } else { |
| 90 | val &= ~(USBPHY_CM_PWRDN | USBPHY_OTG_PWRDN); |
| 91 | val |= USBPHY_OTGVDET_EN | USBPHY_OTGSESSEND_EN; |
| 92 | } |
Sebastian Andrzej Siewior | 3bb869c | 2013-07-30 21:43:45 +0200 | [diff] [blame] | 93 | } else { |
| 94 | val |= USBPHY_CM_PWRDN | USBPHY_OTG_PWRDN; |
| 95 | } |
| 96 | |
| 97 | writel(val, usb_ctrl->phy_reg + reg); |
Daniel Mack | a31a942 | 2014-04-16 17:11:16 +0200 | [diff] [blame] | 98 | |
| 99 | /* |
| 100 | * Give the PHY ~1ms to complete the power up operation. |
| 101 | * Tests have shown unstable behaviour if other USB PHY related |
| 102 | * registers are written too shortly after such a transition. |
| 103 | */ |
| 104 | if (on) |
| 105 | mdelay(1); |
Sebastian Andrzej Siewior | 3bb869c | 2013-07-30 21:43:45 +0200 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | static const struct phy_control ctrl_am335x = { |
| 109 | .phy_power = am335x_phy_power, |
Sebastian Andrzej Siewior | a122263 | 2013-08-19 12:39:44 +0200 | [diff] [blame] | 110 | .phy_wkup = am335x_phy_wkup, |
Sebastian Andrzej Siewior | 3bb869c | 2013-07-30 21:43:45 +0200 | [diff] [blame] | 111 | }; |
| 112 | |
| 113 | static const struct of_device_id omap_control_usb_id_table[] = { |
| 114 | { .compatible = "ti,am335x-usb-ctrl-module", .data = &ctrl_am335x }, |
| 115 | {} |
| 116 | }; |
| 117 | MODULE_DEVICE_TABLE(of, omap_control_usb_id_table); |
| 118 | |
| 119 | static struct platform_driver am335x_control_driver; |
| 120 | static int match(struct device *dev, void *data) |
| 121 | { |
| 122 | struct device_node *node = (struct device_node *)data; |
| 123 | return dev->of_node == node && |
| 124 | dev->driver == &am335x_control_driver.driver; |
| 125 | } |
| 126 | |
| 127 | struct phy_control *am335x_get_phy_control(struct device *dev) |
| 128 | { |
| 129 | struct device_node *node; |
| 130 | struct am335x_control_usb *ctrl_usb; |
| 131 | |
| 132 | node = of_parse_phandle(dev->of_node, "ti,ctrl_mod", 0); |
| 133 | if (!node) |
| 134 | return NULL; |
| 135 | |
| 136 | dev = bus_find_device(&platform_bus_type, NULL, node, match); |
David Dueck | d0f347d | 2015-02-08 16:29:30 +0100 | [diff] [blame] | 137 | if (!dev) |
| 138 | return NULL; |
| 139 | |
Sebastian Andrzej Siewior | 3bb869c | 2013-07-30 21:43:45 +0200 | [diff] [blame] | 140 | ctrl_usb = dev_get_drvdata(dev); |
| 141 | if (!ctrl_usb) |
| 142 | return NULL; |
| 143 | return &ctrl_usb->phy_ctrl; |
| 144 | } |
| 145 | EXPORT_SYMBOL_GPL(am335x_get_phy_control); |
| 146 | |
| 147 | static int am335x_control_usb_probe(struct platform_device *pdev) |
| 148 | { |
| 149 | struct resource *res; |
| 150 | struct am335x_control_usb *ctrl_usb; |
| 151 | const struct of_device_id *of_id; |
| 152 | const struct phy_control *phy_ctrl; |
| 153 | |
| 154 | of_id = of_match_node(omap_control_usb_id_table, pdev->dev.of_node); |
| 155 | if (!of_id) |
| 156 | return -EINVAL; |
| 157 | |
| 158 | phy_ctrl = of_id->data; |
| 159 | |
| 160 | ctrl_usb = devm_kzalloc(&pdev->dev, sizeof(*ctrl_usb), GFP_KERNEL); |
Peter Chen | 9aabd03 | 2014-10-14 15:56:14 +0800 | [diff] [blame] | 161 | if (!ctrl_usb) |
Sebastian Andrzej Siewior | 3bb869c | 2013-07-30 21:43:45 +0200 | [diff] [blame] | 162 | return -ENOMEM; |
Sebastian Andrzej Siewior | 3bb869c | 2013-07-30 21:43:45 +0200 | [diff] [blame] | 163 | |
| 164 | ctrl_usb->dev = &pdev->dev; |
| 165 | |
| 166 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "phy_ctrl"); |
| 167 | ctrl_usb->phy_reg = devm_ioremap_resource(&pdev->dev, res); |
| 168 | if (IS_ERR(ctrl_usb->phy_reg)) |
| 169 | return PTR_ERR(ctrl_usb->phy_reg); |
Sebastian Andrzej Siewior | a122263 | 2013-08-19 12:39:44 +0200 | [diff] [blame] | 170 | |
| 171 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "wakeup"); |
| 172 | ctrl_usb->wkup = devm_ioremap_resource(&pdev->dev, res); |
| 173 | if (IS_ERR(ctrl_usb->wkup)) |
| 174 | return PTR_ERR(ctrl_usb->wkup); |
| 175 | |
Sebastian Andrzej Siewior | 3bb869c | 2013-07-30 21:43:45 +0200 | [diff] [blame] | 176 | spin_lock_init(&ctrl_usb->lock); |
| 177 | ctrl_usb->phy_ctrl = *phy_ctrl; |
| 178 | |
| 179 | dev_set_drvdata(ctrl_usb->dev, ctrl_usb); |
| 180 | return 0; |
| 181 | } |
| 182 | |
| 183 | static struct platform_driver am335x_control_driver = { |
| 184 | .probe = am335x_control_usb_probe, |
| 185 | .driver = { |
| 186 | .name = "am335x-control-usb", |
Sachin Kamat | c4df9ae | 2013-09-30 09:44:45 +0530 | [diff] [blame] | 187 | .of_match_table = omap_control_usb_id_table, |
Sebastian Andrzej Siewior | 3bb869c | 2013-07-30 21:43:45 +0200 | [diff] [blame] | 188 | }, |
| 189 | }; |
| 190 | |
| 191 | module_platform_driver(am335x_control_driver); |
| 192 | MODULE_LICENSE("GPL v2"); |