Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Stephen Boyd | e2427b0 | 2017-01-25 14:32:45 -0800 | [diff] [blame] | 2 | /** |
| 3 | * Copyright (C) 2016 Linaro Ltd |
Stephen Boyd | e2427b0 | 2017-01-25 14:32:45 -0800 | [diff] [blame] | 4 | */ |
| 5 | #include <linux/module.h> |
| 6 | #include <linux/ulpi/driver.h> |
| 7 | #include <linux/ulpi/regs.h> |
| 8 | #include <linux/clk.h> |
| 9 | #include <linux/regulator/consumer.h> |
| 10 | #include <linux/of_device.h> |
Vivek Gautam | 706a3b6 | 2017-05-11 12:17:40 +0530 | [diff] [blame] | 11 | #include <linux/phy/phy.h> |
Stephen Boyd | e2427b0 | 2017-01-25 14:32:45 -0800 | [diff] [blame] | 12 | #include <linux/reset.h> |
| 13 | #include <linux/extcon.h> |
| 14 | #include <linux/notifier.h> |
| 15 | |
Stephen Boyd | e2427b0 | 2017-01-25 14:32:45 -0800 | [diff] [blame] | 16 | #define ULPI_PWR_CLK_MNG_REG 0x88 |
| 17 | # define ULPI_PWR_OTG_COMP_DISABLE BIT(0) |
| 18 | |
| 19 | #define ULPI_MISC_A 0x96 |
| 20 | # define ULPI_MISC_A_VBUSVLDEXTSEL BIT(1) |
| 21 | # define ULPI_MISC_A_VBUSVLDEXT BIT(0) |
| 22 | |
| 23 | |
| 24 | struct ulpi_seq { |
| 25 | u8 addr; |
| 26 | u8 val; |
| 27 | }; |
| 28 | |
| 29 | struct qcom_usb_hs_phy { |
| 30 | struct ulpi *ulpi; |
| 31 | struct phy *phy; |
| 32 | struct clk *ref_clk; |
| 33 | struct clk *sleep_clk; |
| 34 | struct regulator *v1p8; |
| 35 | struct regulator *v3p3; |
| 36 | struct reset_control *reset; |
| 37 | struct ulpi_seq *init_seq; |
| 38 | struct extcon_dev *vbus_edev; |
| 39 | struct notifier_block vbus_notify; |
| 40 | }; |
| 41 | |
Grygorii Strashko | 79a5a18 | 2018-11-19 19:24:20 -0600 | [diff] [blame] | 42 | static int qcom_usb_hs_phy_set_mode(struct phy *phy, |
| 43 | enum phy_mode mode, int submode) |
Stephen Boyd | e2427b0 | 2017-01-25 14:32:45 -0800 | [diff] [blame] | 44 | { |
| 45 | struct qcom_usb_hs_phy *uphy = phy_get_drvdata(phy); |
| 46 | u8 addr; |
| 47 | int ret; |
| 48 | |
| 49 | if (!uphy->vbus_edev) { |
| 50 | u8 val = 0; |
| 51 | |
| 52 | switch (mode) { |
| 53 | case PHY_MODE_USB_OTG: |
| 54 | case PHY_MODE_USB_HOST: |
| 55 | val |= ULPI_INT_IDGRD; |
Gustavo A. R. Silva | 01abdcc | 2018-07-05 10:00:09 -0500 | [diff] [blame] | 56 | /* fall through */ |
Stephen Boyd | e2427b0 | 2017-01-25 14:32:45 -0800 | [diff] [blame] | 57 | case PHY_MODE_USB_DEVICE: |
| 58 | val |= ULPI_INT_SESS_VALID; |
| 59 | default: |
| 60 | break; |
| 61 | } |
| 62 | |
| 63 | ret = ulpi_write(uphy->ulpi, ULPI_USB_INT_EN_RISE, val); |
| 64 | if (ret) |
| 65 | return ret; |
| 66 | ret = ulpi_write(uphy->ulpi, ULPI_USB_INT_EN_FALL, val); |
| 67 | } else { |
| 68 | switch (mode) { |
| 69 | case PHY_MODE_USB_OTG: |
| 70 | case PHY_MODE_USB_DEVICE: |
| 71 | addr = ULPI_SET(ULPI_MISC_A); |
| 72 | break; |
| 73 | case PHY_MODE_USB_HOST: |
| 74 | addr = ULPI_CLR(ULPI_MISC_A); |
| 75 | break; |
| 76 | default: |
| 77 | return -EINVAL; |
| 78 | } |
| 79 | |
| 80 | ret = ulpi_write(uphy->ulpi, ULPI_SET(ULPI_PWR_CLK_MNG_REG), |
| 81 | ULPI_PWR_OTG_COMP_DISABLE); |
| 82 | if (ret) |
| 83 | return ret; |
| 84 | ret = ulpi_write(uphy->ulpi, addr, ULPI_MISC_A_VBUSVLDEXTSEL); |
| 85 | } |
| 86 | |
| 87 | return ret; |
| 88 | } |
| 89 | |
| 90 | static int |
| 91 | qcom_usb_hs_phy_vbus_notifier(struct notifier_block *nb, unsigned long event, |
| 92 | void *ptr) |
| 93 | { |
| 94 | struct qcom_usb_hs_phy *uphy; |
| 95 | u8 addr; |
| 96 | |
| 97 | uphy = container_of(nb, struct qcom_usb_hs_phy, vbus_notify); |
| 98 | |
| 99 | if (event) |
| 100 | addr = ULPI_SET(ULPI_MISC_A); |
| 101 | else |
| 102 | addr = ULPI_CLR(ULPI_MISC_A); |
| 103 | |
| 104 | return ulpi_write(uphy->ulpi, addr, ULPI_MISC_A_VBUSVLDEXT); |
| 105 | } |
| 106 | |
| 107 | static int qcom_usb_hs_phy_power_on(struct phy *phy) |
| 108 | { |
| 109 | struct qcom_usb_hs_phy *uphy = phy_get_drvdata(phy); |
| 110 | struct ulpi *ulpi = uphy->ulpi; |
| 111 | const struct ulpi_seq *seq; |
| 112 | int ret, state; |
| 113 | |
| 114 | ret = clk_prepare_enable(uphy->ref_clk); |
| 115 | if (ret) |
| 116 | return ret; |
| 117 | |
| 118 | ret = clk_prepare_enable(uphy->sleep_clk); |
| 119 | if (ret) |
| 120 | goto err_sleep; |
| 121 | |
| 122 | ret = regulator_set_load(uphy->v1p8, 50000); |
| 123 | if (ret < 0) |
| 124 | goto err_1p8; |
| 125 | |
| 126 | ret = regulator_enable(uphy->v1p8); |
| 127 | if (ret) |
| 128 | goto err_1p8; |
| 129 | |
| 130 | ret = regulator_set_voltage_triplet(uphy->v3p3, 3050000, 3300000, |
| 131 | 3300000); |
| 132 | if (ret) |
| 133 | goto err_3p3; |
| 134 | |
| 135 | ret = regulator_set_load(uphy->v3p3, 50000); |
| 136 | if (ret < 0) |
| 137 | goto err_3p3; |
| 138 | |
| 139 | ret = regulator_enable(uphy->v3p3); |
| 140 | if (ret) |
| 141 | goto err_3p3; |
| 142 | |
| 143 | for (seq = uphy->init_seq; seq->addr; seq++) { |
| 144 | ret = ulpi_write(ulpi, ULPI_EXT_VENDOR_SPECIFIC + seq->addr, |
| 145 | seq->val); |
| 146 | if (ret) |
| 147 | goto err_ulpi; |
| 148 | } |
| 149 | |
| 150 | if (uphy->reset) { |
| 151 | ret = reset_control_reset(uphy->reset); |
| 152 | if (ret) |
| 153 | goto err_ulpi; |
| 154 | } |
| 155 | |
| 156 | if (uphy->vbus_edev) { |
Chanwoo Choi | f0b5c2c | 2017-03-22 19:08:37 +0900 | [diff] [blame] | 157 | state = extcon_get_state(uphy->vbus_edev, EXTCON_USB); |
Stephen Boyd | e2427b0 | 2017-01-25 14:32:45 -0800 | [diff] [blame] | 158 | /* setup initial state */ |
| 159 | qcom_usb_hs_phy_vbus_notifier(&uphy->vbus_notify, state, |
| 160 | uphy->vbus_edev); |
Chanwoo Choi | f0b5c2c | 2017-03-22 19:08:37 +0900 | [diff] [blame] | 161 | ret = devm_extcon_register_notifier(&ulpi->dev, uphy->vbus_edev, |
| 162 | EXTCON_USB, &uphy->vbus_notify); |
Stephen Boyd | e2427b0 | 2017-01-25 14:32:45 -0800 | [diff] [blame] | 163 | if (ret) |
| 164 | goto err_ulpi; |
| 165 | } |
| 166 | |
| 167 | return 0; |
| 168 | err_ulpi: |
| 169 | regulator_disable(uphy->v3p3); |
| 170 | err_3p3: |
| 171 | regulator_disable(uphy->v1p8); |
| 172 | err_1p8: |
| 173 | clk_disable_unprepare(uphy->sleep_clk); |
| 174 | err_sleep: |
| 175 | clk_disable_unprepare(uphy->ref_clk); |
| 176 | return ret; |
| 177 | } |
| 178 | |
| 179 | static int qcom_usb_hs_phy_power_off(struct phy *phy) |
| 180 | { |
Stephen Boyd | e2427b0 | 2017-01-25 14:32:45 -0800 | [diff] [blame] | 181 | struct qcom_usb_hs_phy *uphy = phy_get_drvdata(phy); |
| 182 | |
Stephen Boyd | e2427b0 | 2017-01-25 14:32:45 -0800 | [diff] [blame] | 183 | regulator_disable(uphy->v3p3); |
| 184 | regulator_disable(uphy->v1p8); |
| 185 | clk_disable_unprepare(uphy->sleep_clk); |
| 186 | clk_disable_unprepare(uphy->ref_clk); |
| 187 | |
| 188 | return 0; |
| 189 | } |
| 190 | |
| 191 | static const struct phy_ops qcom_usb_hs_phy_ops = { |
| 192 | .power_on = qcom_usb_hs_phy_power_on, |
| 193 | .power_off = qcom_usb_hs_phy_power_off, |
| 194 | .set_mode = qcom_usb_hs_phy_set_mode, |
| 195 | .owner = THIS_MODULE, |
| 196 | }; |
| 197 | |
| 198 | static int qcom_usb_hs_phy_probe(struct ulpi *ulpi) |
| 199 | { |
| 200 | struct qcom_usb_hs_phy *uphy; |
| 201 | struct phy_provider *p; |
| 202 | struct clk *clk; |
| 203 | struct regulator *reg; |
| 204 | struct reset_control *reset; |
| 205 | int size; |
| 206 | int ret; |
| 207 | |
| 208 | uphy = devm_kzalloc(&ulpi->dev, sizeof(*uphy), GFP_KERNEL); |
| 209 | if (!uphy) |
| 210 | return -ENOMEM; |
| 211 | ulpi_set_drvdata(ulpi, uphy); |
| 212 | uphy->ulpi = ulpi; |
| 213 | |
| 214 | size = of_property_count_u8_elems(ulpi->dev.of_node, "qcom,init-seq"); |
| 215 | if (size < 0) |
| 216 | size = 0; |
| 217 | uphy->init_seq = devm_kmalloc_array(&ulpi->dev, (size / 2) + 1, |
| 218 | sizeof(*uphy->init_seq), GFP_KERNEL); |
| 219 | if (!uphy->init_seq) |
| 220 | return -ENOMEM; |
| 221 | ret = of_property_read_u8_array(ulpi->dev.of_node, "qcom,init-seq", |
| 222 | (u8 *)uphy->init_seq, size); |
| 223 | if (ret && size) |
| 224 | return ret; |
| 225 | /* NUL terminate */ |
| 226 | uphy->init_seq[size / 2].addr = uphy->init_seq[size / 2].val = 0; |
| 227 | |
| 228 | uphy->ref_clk = clk = devm_clk_get(&ulpi->dev, "ref"); |
| 229 | if (IS_ERR(clk)) |
| 230 | return PTR_ERR(clk); |
| 231 | |
| 232 | uphy->sleep_clk = clk = devm_clk_get(&ulpi->dev, "sleep"); |
| 233 | if (IS_ERR(clk)) |
| 234 | return PTR_ERR(clk); |
| 235 | |
| 236 | uphy->v1p8 = reg = devm_regulator_get(&ulpi->dev, "v1p8"); |
| 237 | if (IS_ERR(reg)) |
| 238 | return PTR_ERR(reg); |
| 239 | |
| 240 | uphy->v3p3 = reg = devm_regulator_get(&ulpi->dev, "v3p3"); |
| 241 | if (IS_ERR(reg)) |
| 242 | return PTR_ERR(reg); |
| 243 | |
| 244 | uphy->reset = reset = devm_reset_control_get(&ulpi->dev, "por"); |
| 245 | if (IS_ERR(reset)) { |
| 246 | if (PTR_ERR(reset) == -EPROBE_DEFER) |
| 247 | return PTR_ERR(reset); |
| 248 | uphy->reset = NULL; |
| 249 | } |
| 250 | |
| 251 | uphy->phy = devm_phy_create(&ulpi->dev, ulpi->dev.of_node, |
| 252 | &qcom_usb_hs_phy_ops); |
| 253 | if (IS_ERR(uphy->phy)) |
| 254 | return PTR_ERR(uphy->phy); |
| 255 | |
| 256 | uphy->vbus_edev = extcon_get_edev_by_phandle(&ulpi->dev, 0); |
| 257 | if (IS_ERR(uphy->vbus_edev)) { |
| 258 | if (PTR_ERR(uphy->vbus_edev) != -ENODEV) |
| 259 | return PTR_ERR(uphy->vbus_edev); |
| 260 | uphy->vbus_edev = NULL; |
| 261 | } |
| 262 | |
| 263 | uphy->vbus_notify.notifier_call = qcom_usb_hs_phy_vbus_notifier; |
| 264 | phy_set_drvdata(uphy->phy, uphy); |
| 265 | |
| 266 | p = devm_of_phy_provider_register(&ulpi->dev, of_phy_simple_xlate); |
| 267 | return PTR_ERR_OR_ZERO(p); |
| 268 | } |
| 269 | |
| 270 | static const struct of_device_id qcom_usb_hs_phy_match[] = { |
| 271 | { .compatible = "qcom,usb-hs-phy", }, |
| 272 | { } |
| 273 | }; |
| 274 | MODULE_DEVICE_TABLE(of, qcom_usb_hs_phy_match); |
| 275 | |
| 276 | static struct ulpi_driver qcom_usb_hs_phy_driver = { |
| 277 | .probe = qcom_usb_hs_phy_probe, |
| 278 | .driver = { |
| 279 | .name = "qcom_usb_hs_phy", |
| 280 | .of_match_table = qcom_usb_hs_phy_match, |
| 281 | }, |
| 282 | }; |
| 283 | module_ulpi_driver(qcom_usb_hs_phy_driver); |
| 284 | |
| 285 | MODULE_DESCRIPTION("Qualcomm USB HS phy"); |
| 286 | MODULE_LICENSE("GPL v2"); |