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