|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
| 2 | +/* |
| 3 | + * Copyright (c) 2024-2025 Qualcomm Innovation Center, Inc. All rights reserved. |
| 4 | + */ |
| 5 | + |
| 6 | +#include <linux/clk.h> |
| 7 | +#include <linux/delay.h> |
| 8 | +#include <linux/err.h> |
| 9 | +#include <linux/io.h> |
| 10 | +#include <linux/kernel.h> |
| 11 | +#include <linux/module.h> |
| 12 | +#include <linux/of.h> |
| 13 | +#include <linux/phy/phy.h> |
| 14 | +#include <linux/platform_device.h> |
| 15 | +#include <linux/reset.h> |
| 16 | +#include <linux/slab.h> |
| 17 | + |
| 18 | +#include <linux/regulator/consumer.h> |
| 19 | + |
| 20 | +#define USB_PHY_UTMI_CTRL0 (0x3c) |
| 21 | +#define SLEEPM BIT(0) |
| 22 | + |
| 23 | +#define USB_PHY_UTMI_CTRL5 (0x50) |
| 24 | +#define POR BIT(1) |
| 25 | + |
| 26 | +#define USB_PHY_HS_PHY_CTRL_COMMON0 (0x54) |
| 27 | +#define SIDDQ_SEL BIT(1) |
| 28 | +#define SIDDQ BIT(2) |
| 29 | +#define FSEL GENMASK(6, 4) |
| 30 | +#define FSEL_38_4_MHZ_VAL (0x6) |
| 31 | + |
| 32 | +#define USB_PHY_HS_PHY_CTRL2 (0x64) |
| 33 | +#define USB2_SUSPEND_N BIT(2) |
| 34 | +#define USB2_SUSPEND_N_SEL BIT(3) |
| 35 | + |
| 36 | +#define USB_PHY_CFG0 (0x94) |
| 37 | +#define UTMI_PHY_CMN_CTRL_OVERRIDE_EN BIT(1) |
| 38 | + |
| 39 | +#define USB_PHY_CFG1 (0x154) |
| 40 | +#define PLL_EN BIT(0) |
| 41 | + |
| 42 | +#define USB_PHY_FSEL_SEL (0xb8) |
| 43 | +#define FSEL_SEL BIT(0) |
| 44 | + |
| 45 | +#define USB_PHY_XCFGI_39_32 (0x16c) |
| 46 | +#define HSTX_PE GENMASK(3, 2) |
| 47 | + |
| 48 | +#define USB_PHY_XCFGI_71_64 (0x17c) |
| 49 | +#define HSTX_SWING GENMASK(3, 0) |
| 50 | + |
| 51 | +#define USB_PHY_XCFGI_31_24 (0x168) |
| 52 | +#define HSTX_SLEW GENMASK(2, 0) |
| 53 | + |
| 54 | +#define USB_PHY_XCFGI_7_0 (0x15c) |
| 55 | +#define PLL_LOCK_TIME GENMASK(1, 0) |
| 56 | + |
| 57 | +#define M31_EUSB_PHY_INIT_CFG(o, b, v) \ |
| 58 | +{ \ |
| 59 | + .off = o, \ |
| 60 | + .mask = b, \ |
| 61 | + .val = v, \ |
| 62 | +} |
| 63 | + |
| 64 | +struct m31_phy_tbl_entry { |
| 65 | + u32 off; |
| 66 | + u32 mask; |
| 67 | + u32 val; |
| 68 | +}; |
| 69 | + |
| 70 | +struct m31_eusb2_priv_data { |
| 71 | + const struct m31_phy_tbl_entry *setup_seq; |
| 72 | + unsigned int setup_seq_nregs; |
| 73 | + const struct m31_phy_tbl_entry *override_seq; |
| 74 | + unsigned int override_seq_nregs; |
| 75 | + const struct m31_phy_tbl_entry *reset_seq; |
| 76 | + unsigned int reset_seq_nregs; |
| 77 | + unsigned int fsel; |
| 78 | +}; |
| 79 | + |
| 80 | +static const struct m31_phy_tbl_entry m31_eusb2_setup_tbl[] = { |
| 81 | + M31_EUSB_PHY_INIT_CFG(USB_PHY_CFG0, UTMI_PHY_CMN_CTRL_OVERRIDE_EN, 1), |
| 82 | + M31_EUSB_PHY_INIT_CFG(USB_PHY_UTMI_CTRL5, POR, 1), |
| 83 | + M31_EUSB_PHY_INIT_CFG(USB_PHY_CFG1, PLL_EN, 1), |
| 84 | + M31_EUSB_PHY_INIT_CFG(USB_PHY_FSEL_SEL, FSEL_SEL, 1), |
| 85 | +}; |
| 86 | + |
| 87 | +static const struct m31_phy_tbl_entry m31_eusb_phy_override_tbl[] = { |
| 88 | + M31_EUSB_PHY_INIT_CFG(USB_PHY_XCFGI_39_32, HSTX_PE, 0), |
| 89 | + M31_EUSB_PHY_INIT_CFG(USB_PHY_XCFGI_71_64, HSTX_SWING, 7), |
| 90 | + M31_EUSB_PHY_INIT_CFG(USB_PHY_XCFGI_31_24, HSTX_SLEW, 0), |
| 91 | + M31_EUSB_PHY_INIT_CFG(USB_PHY_XCFGI_7_0, PLL_LOCK_TIME, 0), |
| 92 | +}; |
| 93 | + |
| 94 | +static const struct m31_phy_tbl_entry m31_eusb_phy_reset_tbl[] = { |
| 95 | + M31_EUSB_PHY_INIT_CFG(USB_PHY_HS_PHY_CTRL2, USB2_SUSPEND_N_SEL, 1), |
| 96 | + M31_EUSB_PHY_INIT_CFG(USB_PHY_HS_PHY_CTRL2, USB2_SUSPEND_N, 1), |
| 97 | + M31_EUSB_PHY_INIT_CFG(USB_PHY_UTMI_CTRL0, SLEEPM, 1), |
| 98 | + M31_EUSB_PHY_INIT_CFG(USB_PHY_HS_PHY_CTRL_COMMON0, SIDDQ_SEL, 1), |
| 99 | + M31_EUSB_PHY_INIT_CFG(USB_PHY_HS_PHY_CTRL_COMMON0, SIDDQ, 0), |
| 100 | + M31_EUSB_PHY_INIT_CFG(USB_PHY_UTMI_CTRL5, POR, 0), |
| 101 | + M31_EUSB_PHY_INIT_CFG(USB_PHY_HS_PHY_CTRL2, USB2_SUSPEND_N_SEL, 0), |
| 102 | + M31_EUSB_PHY_INIT_CFG(USB_PHY_CFG0, UTMI_PHY_CMN_CTRL_OVERRIDE_EN, 0), |
| 103 | +}; |
| 104 | + |
| 105 | +static const struct regulator_bulk_data m31_eusb_phy_vregs[] = { |
| 106 | + { .supply = "vdd" }, |
| 107 | + { .supply = "vdda12" }, |
| 108 | +}; |
| 109 | + |
| 110 | +#define M31_EUSB_NUM_VREGS ARRAY_SIZE(m31_eusb_phy_vregs) |
| 111 | + |
| 112 | +struct m31eusb2_phy { |
| 113 | + struct phy *phy; |
| 114 | + void __iomem *base; |
| 115 | + const struct m31_eusb2_priv_data *data; |
| 116 | + enum phy_mode mode; |
| 117 | + |
| 118 | + struct regulator_bulk_data *vregs; |
| 119 | + struct clk *clk; |
| 120 | + struct reset_control *reset; |
| 121 | + |
| 122 | + struct phy *repeater; |
| 123 | +}; |
| 124 | + |
| 125 | +static int m31eusb2_phy_write_readback(void __iomem *base, u32 offset, |
| 126 | + const u32 mask, u32 val) |
| 127 | +{ |
| 128 | + u32 write_val; |
| 129 | + u32 tmp; |
| 130 | + |
| 131 | + tmp = readl(base + offset); |
| 132 | + tmp &= ~mask; |
| 133 | + write_val = tmp | val; |
| 134 | + |
| 135 | + writel(write_val, base + offset); |
| 136 | + |
| 137 | + tmp = readl(base + offset); |
| 138 | + tmp &= mask; |
| 139 | + |
| 140 | + if (tmp != val) { |
| 141 | + pr_err("write: %x to offset: %x FAILED\n", val, offset); |
| 142 | + return -EINVAL; |
| 143 | + } |
| 144 | + |
| 145 | + return 0; |
| 146 | +} |
| 147 | + |
| 148 | +static int m31eusb2_phy_write_sequence(struct m31eusb2_phy *phy, |
| 149 | + const struct m31_phy_tbl_entry *tbl, |
| 150 | + int num) |
| 151 | +{ |
| 152 | + int i; |
| 153 | + int ret; |
| 154 | + |
| 155 | + for (i = 0 ; i < num; i++, tbl++) { |
| 156 | + dev_dbg(&phy->phy->dev, "Offset:%x BitMask:%x Value:%x", |
| 157 | + tbl->off, tbl->mask, tbl->val); |
| 158 | + |
| 159 | + ret = m31eusb2_phy_write_readback(phy->base, |
| 160 | + tbl->off, tbl->mask, |
| 161 | + tbl->val << __ffs(tbl->mask)); |
| 162 | + if (ret < 0) |
| 163 | + return ret; |
| 164 | + } |
| 165 | + |
| 166 | + return 0; |
| 167 | +} |
| 168 | + |
| 169 | +static int m31eusb2_phy_set_mode(struct phy *uphy, enum phy_mode mode, int submode) |
| 170 | +{ |
| 171 | + struct m31eusb2_phy *phy = phy_get_drvdata(uphy); |
| 172 | + |
| 173 | + phy->mode = mode; |
| 174 | + |
| 175 | + return phy_set_mode_ext(phy->repeater, mode, submode); |
| 176 | +} |
| 177 | + |
| 178 | +static int m31eusb2_phy_init(struct phy *uphy) |
| 179 | +{ |
| 180 | + struct m31eusb2_phy *phy = phy_get_drvdata(uphy); |
| 181 | + const struct m31_eusb2_priv_data *data = phy->data; |
| 182 | + int ret; |
| 183 | + |
| 184 | + ret = regulator_bulk_enable(M31_EUSB_NUM_VREGS, phy->vregs); |
| 185 | + if (ret) { |
| 186 | + dev_err(&uphy->dev, "failed to enable regulator, %d\n", ret); |
| 187 | + return ret; |
| 188 | + } |
| 189 | + |
| 190 | + ret = phy_init(phy->repeater); |
| 191 | + if (ret) { |
| 192 | + dev_err(&uphy->dev, "repeater init failed. %d\n", ret); |
| 193 | + goto disable_vreg; |
| 194 | + } |
| 195 | + |
| 196 | + ret = clk_prepare_enable(phy->clk); |
| 197 | + if (ret) { |
| 198 | + dev_err(&uphy->dev, "failed to enable cfg ahb clock, %d\n", ret); |
| 199 | + goto disable_repeater; |
| 200 | + } |
| 201 | + |
| 202 | + /* Perform phy reset */ |
| 203 | + reset_control_assert(phy->reset); |
| 204 | + udelay(5); |
| 205 | + reset_control_deassert(phy->reset); |
| 206 | + |
| 207 | + m31eusb2_phy_write_sequence(phy, data->setup_seq, data->setup_seq_nregs); |
| 208 | + m31eusb2_phy_write_readback(phy->base, |
| 209 | + USB_PHY_HS_PHY_CTRL_COMMON0, FSEL, |
| 210 | + FIELD_PREP(FSEL, data->fsel)); |
| 211 | + m31eusb2_phy_write_sequence(phy, data->override_seq, data->override_seq_nregs); |
| 212 | + m31eusb2_phy_write_sequence(phy, data->reset_seq, data->reset_seq_nregs); |
| 213 | + |
| 214 | + return 0; |
| 215 | + |
| 216 | +disable_repeater: |
| 217 | + phy_exit(phy->repeater); |
| 218 | +disable_vreg: |
| 219 | + regulator_bulk_disable(M31_EUSB_NUM_VREGS, phy->vregs); |
| 220 | + |
| 221 | + return 0; |
| 222 | +} |
| 223 | + |
| 224 | +static int m31eusb2_phy_exit(struct phy *uphy) |
| 225 | +{ |
| 226 | + struct m31eusb2_phy *phy = phy_get_drvdata(uphy); |
| 227 | + |
| 228 | + clk_disable_unprepare(phy->clk); |
| 229 | + regulator_bulk_disable(M31_EUSB_NUM_VREGS, phy->vregs); |
| 230 | + phy_exit(phy->repeater); |
| 231 | + |
| 232 | + return 0; |
| 233 | +} |
| 234 | + |
| 235 | +static const struct phy_ops m31eusb2_phy_gen_ops = { |
| 236 | + .init = m31eusb2_phy_init, |
| 237 | + .exit = m31eusb2_phy_exit, |
| 238 | + .set_mode = m31eusb2_phy_set_mode, |
| 239 | + .owner = THIS_MODULE, |
| 240 | +}; |
| 241 | + |
| 242 | +static int m31eusb2_phy_probe(struct platform_device *pdev) |
| 243 | +{ |
| 244 | + struct phy_provider *phy_provider; |
| 245 | + const struct m31_eusb2_priv_data *data; |
| 246 | + struct device *dev = &pdev->dev; |
| 247 | + struct m31eusb2_phy *phy; |
| 248 | + int ret; |
| 249 | + |
| 250 | + phy = devm_kzalloc(dev, sizeof(*phy), GFP_KERNEL); |
| 251 | + if (!phy) |
| 252 | + return -ENOMEM; |
| 253 | + |
| 254 | + data = device_get_match_data(dev); |
| 255 | + if (IS_ERR(data)) |
| 256 | + return -EINVAL; |
| 257 | + phy->data = data; |
| 258 | + |
| 259 | + phy->base = devm_platform_ioremap_resource(pdev, 0); |
| 260 | + if (IS_ERR(phy->base)) |
| 261 | + return PTR_ERR(phy->base); |
| 262 | + |
| 263 | + phy->reset = devm_reset_control_get_exclusive(dev, NULL); |
| 264 | + if (IS_ERR(phy->reset)) |
| 265 | + return PTR_ERR(phy->reset); |
| 266 | + |
| 267 | + phy->clk = devm_clk_get(dev, NULL); |
| 268 | + if (IS_ERR(phy->clk)) |
| 269 | + return dev_err_probe(dev, PTR_ERR(phy->clk), |
| 270 | + "failed to get clk\n"); |
| 271 | + |
| 272 | + phy->phy = devm_phy_create(dev, NULL, &m31eusb2_phy_gen_ops); |
| 273 | + if (IS_ERR(phy->phy)) |
| 274 | + return dev_err_probe(dev, PTR_ERR(phy->phy), |
| 275 | + "failed to create phy\n"); |
| 276 | + |
| 277 | + ret = devm_regulator_bulk_get_const(dev, M31_EUSB_NUM_VREGS, |
| 278 | + m31_eusb_phy_vregs, &phy->vregs); |
| 279 | + if (ret) |
| 280 | + return dev_err_probe(dev, ret, |
| 281 | + "failed to get regulator supplies\n"); |
| 282 | + |
| 283 | + phy_set_drvdata(phy->phy, phy); |
| 284 | + |
| 285 | + phy->repeater = devm_of_phy_get_by_index(dev, dev->of_node, 0); |
| 286 | + if (IS_ERR(phy->repeater)) |
| 287 | + return dev_err_probe(dev, PTR_ERR(phy->repeater), |
| 288 | + "failed to get repeater\n"); |
| 289 | + |
| 290 | + phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate); |
| 291 | + if (!IS_ERR(phy_provider)) |
| 292 | + dev_info(dev, "Registered M31 USB phy\n"); |
| 293 | + |
| 294 | + return PTR_ERR_OR_ZERO(phy_provider); |
| 295 | +} |
| 296 | + |
| 297 | +static const struct m31_eusb2_priv_data m31_eusb_v1_data = { |
| 298 | + .setup_seq = m31_eusb2_setup_tbl, |
| 299 | + .setup_seq_nregs = ARRAY_SIZE(m31_eusb2_setup_tbl), |
| 300 | + .override_seq = m31_eusb_phy_override_tbl, |
| 301 | + .override_seq_nregs = ARRAY_SIZE(m31_eusb_phy_override_tbl), |
| 302 | + .reset_seq = m31_eusb_phy_reset_tbl, |
| 303 | + .reset_seq_nregs = ARRAY_SIZE(m31_eusb_phy_reset_tbl), |
| 304 | + .fsel = FSEL_38_4_MHZ_VAL, |
| 305 | +}; |
| 306 | + |
| 307 | +static const struct of_device_id m31eusb2_phy_id_table[] = { |
| 308 | + { .compatible = "qcom,sm8750-m31-eusb2-phy", .data = &m31_eusb_v1_data }, |
| 309 | + { }, |
| 310 | +}; |
| 311 | +MODULE_DEVICE_TABLE(of, m31eusb2_phy_id_table); |
| 312 | + |
| 313 | +static struct platform_driver m31eusb2_phy_driver = { |
| 314 | + .probe = m31eusb2_phy_probe, |
| 315 | + .driver = { |
| 316 | + .name = "qcom-m31eusb2-phy", |
| 317 | + .of_match_table = m31eusb2_phy_id_table, |
| 318 | + }, |
| 319 | +}; |
| 320 | + |
| 321 | +module_platform_driver(m31eusb2_phy_driver); |
| 322 | + |
| 323 | +MODULE_AUTHOR( "Wesley Cheng <[email protected]>"); |
| 324 | +MODULE_DESCRIPTION("eUSB2 Qualcomm M31 HSPHY driver"); |
| 325 | +MODULE_LICENSE("GPL"); |
0 commit comments