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