0001
0002
0003
0004
0005
0006
0007
0008 #include <linux/bitfield.h>
0009 #include <linux/delay.h>
0010 #include <linux/iopoll.h>
0011 #include <linux/module.h>
0012 #include <linux/of.h>
0013 #include <linux/platform_device.h>
0014 #include <linux/regulator/consumer.h>
0015 #include <linux/reset.h>
0016 #include <linux/usb/phy.h>
0017 #include <linux/workqueue.h>
0018
0019 #define CTRL1_OFFSET 0x14
0020 #define SRAM_EXT_LD_DONE BIT(25)
0021 #define SRAM_INIT_DONE BIT(26)
0022
0023 #define TCPC_OFFSET 0x1014
0024 #define TCPC_MUX_CTL GENMASK(1, 0)
0025 #define MUX_NC 0
0026 #define MUX_USB 1
0027 #define MUX_DP 2
0028 #define MUX_USBDP 3
0029 #define TCPC_FLIPPED BIT(2)
0030 #define TCPC_LOW_POWER_EN BIT(3)
0031 #define TCPC_VALID BIT(4)
0032 #define TCPC_CONN \
0033 (TCPC_VALID | FIELD_PREP(TCPC_MUX_CTL, MUX_USB))
0034 #define TCPC_DISCONN \
0035 (TCPC_VALID | FIELD_PREP(TCPC_MUX_CTL, MUX_NC) | TCPC_LOW_POWER_EN)
0036
0037 static const char *const PHY_RESETS[] = { "phy31", "phy", };
0038 static const char *const CTL_RESETS[] = { "apb", "ctrl", };
0039
0040 struct tca_apb {
0041 struct reset_control *resets[ARRAY_SIZE(PHY_RESETS)];
0042 struct regulator *vbus;
0043 struct work_struct wk;
0044 struct usb_phy phy;
0045
0046 bool regulator_enabled;
0047 bool phy_initialized;
0048 bool connected;
0049 };
0050
0051 static int get_flipped(struct tca_apb *ta, bool *flipped)
0052 {
0053 union extcon_property_value property;
0054 int ret;
0055
0056 ret = extcon_get_property(ta->phy.edev, EXTCON_USB_HOST,
0057 EXTCON_PROP_USB_TYPEC_POLARITY, &property);
0058 if (ret) {
0059 dev_err(ta->phy.dev, "no polarity property from extcon\n");
0060 return ret;
0061 }
0062
0063 *flipped = property.intval;
0064
0065 return 0;
0066 }
0067
0068 static int phy_init(struct usb_phy *phy)
0069 {
0070 struct tca_apb *ta = container_of(phy, struct tca_apb, phy);
0071 void __iomem *ctrl1 = phy->io_priv + CTRL1_OFFSET;
0072 int val, ret, i;
0073
0074 if (ta->phy_initialized)
0075 return 0;
0076
0077 for (i = 0; i < ARRAY_SIZE(PHY_RESETS); i++)
0078 reset_control_deassert(ta->resets[i]);
0079
0080 ret = readl_poll_timeout(ctrl1, val, val & SRAM_INIT_DONE, 10, 10 * 1000);
0081 if (ret) {
0082 dev_err(ta->phy.dev, "SRAM init failed, 0x%x\n", val);
0083 return ret;
0084 }
0085
0086 writel(readl(ctrl1) | SRAM_EXT_LD_DONE, ctrl1);
0087
0088 ta->phy_initialized = true;
0089 if (!ta->phy.edev) {
0090 writel(TCPC_CONN, ta->phy.io_priv + TCPC_OFFSET);
0091 return phy->set_vbus(phy, true);
0092 }
0093
0094 schedule_work(&ta->wk);
0095
0096 return ret;
0097 }
0098
0099 static void phy_shutdown(struct usb_phy *phy)
0100 {
0101 struct tca_apb *ta = container_of(phy, struct tca_apb, phy);
0102 int i;
0103
0104 if (!ta->phy_initialized)
0105 return;
0106
0107 ta->phy_initialized = false;
0108 flush_work(&ta->wk);
0109 ta->phy.set_vbus(&ta->phy, false);
0110
0111 ta->connected = false;
0112 writel(TCPC_DISCONN, ta->phy.io_priv + TCPC_OFFSET);
0113
0114 for (i = 0; i < ARRAY_SIZE(PHY_RESETS); i++)
0115 reset_control_assert(ta->resets[i]);
0116 }
0117
0118 static int phy_set_vbus(struct usb_phy *phy, int on)
0119 {
0120 struct tca_apb *ta = container_of(phy, struct tca_apb, phy);
0121 int ret;
0122
0123 if (!!on == ta->regulator_enabled)
0124 return 0;
0125
0126 if (on)
0127 ret = regulator_enable(ta->vbus);
0128 else
0129 ret = regulator_disable(ta->vbus);
0130
0131 if (!ret)
0132 ta->regulator_enabled = on;
0133
0134 dev_dbg(ta->phy.dev, "set vbus: %d\n", on);
0135 return ret;
0136 }
0137
0138 static void tca_work(struct work_struct *work)
0139 {
0140 struct tca_apb *ta = container_of(work, struct tca_apb, wk);
0141 bool connected;
0142 bool flipped = false;
0143 u32 val;
0144 int ret;
0145
0146 ret = get_flipped(ta, &flipped);
0147 if (ret)
0148 return;
0149
0150 connected = extcon_get_state(ta->phy.edev, EXTCON_USB_HOST);
0151 if (connected == ta->connected)
0152 return;
0153
0154 ta->connected = connected;
0155 if (connected) {
0156 val = TCPC_CONN;
0157 if (flipped)
0158 val |= TCPC_FLIPPED;
0159 dev_dbg(ta->phy.dev, "connected%s\n", flipped ? " flipped" : "");
0160 } else {
0161 val = TCPC_DISCONN;
0162 dev_dbg(ta->phy.dev, "disconnected\n");
0163 }
0164
0165 writel(val, ta->phy.io_priv + TCPC_OFFSET);
0166
0167 ret = ta->phy.set_vbus(&ta->phy, connected);
0168 if (ret)
0169 dev_err(ta->phy.dev, "failed to set VBUS\n");
0170 }
0171
0172 static int id_notifier(struct notifier_block *nb, unsigned long event, void *ptr)
0173 {
0174 struct tca_apb *ta = container_of(nb, struct tca_apb, phy.id_nb);
0175
0176 if (ta->phy_initialized)
0177 schedule_work(&ta->wk);
0178
0179 return NOTIFY_DONE;
0180 }
0181
0182 static int vbus_notifier(struct notifier_block *nb, unsigned long evnt, void *ptr)
0183 {
0184 return NOTIFY_DONE;
0185 }
0186
0187 static int phy_probe(struct platform_device *pdev)
0188 {
0189 struct reset_control *resets[ARRAY_SIZE(CTL_RESETS)];
0190 struct device *dev = &pdev->dev;
0191 struct usb_phy *phy;
0192 struct tca_apb *ta;
0193 int i;
0194
0195 ta = devm_kzalloc(dev, sizeof(*ta), GFP_KERNEL);
0196 if (!ta)
0197 return -ENOMEM;
0198
0199 platform_set_drvdata(pdev, ta);
0200 INIT_WORK(&ta->wk, tca_work);
0201
0202 phy = &ta->phy;
0203 phy->dev = dev;
0204 phy->label = dev_name(dev);
0205 phy->type = USB_PHY_TYPE_USB3;
0206 phy->init = phy_init;
0207 phy->shutdown = phy_shutdown;
0208 phy->set_vbus = phy_set_vbus;
0209 phy->id_nb.notifier_call = id_notifier;
0210 phy->vbus_nb.notifier_call = vbus_notifier;
0211
0212 phy->io_priv = devm_platform_ioremap_resource(pdev, 0);
0213 if (IS_ERR(phy->io_priv))
0214 return PTR_ERR(phy->io_priv);
0215
0216 ta->vbus = devm_regulator_get(dev, "vbus");
0217 if (IS_ERR(ta->vbus))
0218 return PTR_ERR(ta->vbus);
0219
0220 for (i = 0; i < ARRAY_SIZE(CTL_RESETS); i++) {
0221 resets[i] = devm_reset_control_get_exclusive(dev, CTL_RESETS[i]);
0222 if (IS_ERR(resets[i])) {
0223 dev_err(dev, "%s reset not found\n", CTL_RESETS[i]);
0224 return PTR_ERR(resets[i]);
0225 }
0226 }
0227
0228 for (i = 0; i < ARRAY_SIZE(PHY_RESETS); i++) {
0229 ta->resets[i] = devm_reset_control_get_exclusive(dev, PHY_RESETS[i]);
0230 if (IS_ERR(ta->resets[i])) {
0231 dev_err(dev, "%s reset not found\n", PHY_RESETS[i]);
0232 return PTR_ERR(ta->resets[i]);
0233 }
0234 }
0235
0236 for (i = 0; i < ARRAY_SIZE(CTL_RESETS); i++)
0237 reset_control_assert(resets[i]);
0238
0239 for (i = 0; i < ARRAY_SIZE(PHY_RESETS); i++)
0240 reset_control_assert(ta->resets[i]);
0241
0242
0243
0244
0245
0246 for (i = 0; i < ARRAY_SIZE(CTL_RESETS); i++)
0247 reset_control_deassert(resets[i]);
0248
0249
0250 usleep_range(20, 100);
0251
0252 return usb_add_phy_dev(phy);
0253 }
0254
0255 static int phy_remove(struct platform_device *pdev)
0256 {
0257 struct tca_apb *ta = platform_get_drvdata(pdev);
0258
0259 usb_remove_phy(&ta->phy);
0260
0261 return 0;
0262 }
0263
0264 static const struct of_device_id intel_usb_phy_dt_ids[] = {
0265 { .compatible = "intel,lgm-usb-phy" },
0266 { }
0267 };
0268 MODULE_DEVICE_TABLE(of, intel_usb_phy_dt_ids);
0269
0270 static struct platform_driver lgm_phy_driver = {
0271 .driver = {
0272 .name = "lgm-usb-phy",
0273 .of_match_table = intel_usb_phy_dt_ids,
0274 },
0275 .probe = phy_probe,
0276 .remove = phy_remove,
0277 };
0278
0279 module_platform_driver(lgm_phy_driver);
0280
0281 MODULE_DESCRIPTION("Intel LGM USB PHY driver");
0282 MODULE_AUTHOR("Li Yin <yin1.li@intel.com>");
0283 MODULE_AUTHOR("Vadivel Murugan R <vadivel.muruganx.ramuthevar@linux.intel.com>");
0284 MODULE_LICENSE("GPL v2");