linux/drivers/phy/broadcom/phy-brcm-usb.c
<<
>>
Prefs
   1/*
   2 * phy-brcm-usb.c - Broadcom USB Phy Driver
   3 *
   4 * Copyright (C) 2015-2017 Broadcom
   5 *
   6 * This software is licensed under the terms of the GNU General Public
   7 * License version 2, as published by the Free Software Foundation, and
   8 * may be copied, distributed, and modified under those terms.
   9 *
  10 * This program is distributed in the hope that it will be useful,
  11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13 * GNU General Public License for more details.
  14 */
  15
  16#include <linux/clk.h>
  17#include <linux/delay.h>
  18#include <linux/err.h>
  19#include <linux/io.h>
  20#include <linux/module.h>
  21#include <linux/of.h>
  22#include <linux/phy/phy.h>
  23#include <linux/platform_device.h>
  24#include <linux/interrupt.h>
  25#include <linux/soc/brcmstb/brcmstb.h>
  26#include <dt-bindings/phy/phy.h>
  27
  28#include "phy-brcm-usb-init.h"
  29
  30static DEFINE_MUTEX(sysfs_lock);
  31
  32enum brcm_usb_phy_id {
  33        BRCM_USB_PHY_2_0 = 0,
  34        BRCM_USB_PHY_3_0,
  35        BRCM_USB_PHY_ID_MAX
  36};
  37
  38struct value_to_name_map {
  39        int value;
  40        const char *name;
  41};
  42
  43static struct value_to_name_map brcm_dr_mode_to_name[] = {
  44        { USB_CTLR_MODE_HOST, "host" },
  45        { USB_CTLR_MODE_DEVICE, "peripheral" },
  46        { USB_CTLR_MODE_DRD, "drd" },
  47        { USB_CTLR_MODE_TYPEC_PD, "typec-pd" }
  48};
  49
  50static struct value_to_name_map brcm_dual_mode_to_name[] = {
  51        { 0, "host" },
  52        { 1, "device" },
  53        { 2, "auto" },
  54};
  55
  56struct brcm_usb_phy {
  57        struct phy *phy;
  58        unsigned int id;
  59        bool inited;
  60};
  61
  62struct brcm_usb_phy_data {
  63        struct  brcm_usb_init_params ini;
  64        bool                    has_eohci;
  65        bool                    has_xhci;
  66        struct clk              *usb_20_clk;
  67        struct clk              *usb_30_clk;
  68        struct mutex            mutex;  /* serialize phy init */
  69        int                     init_count;
  70        struct brcm_usb_phy     phys[BRCM_USB_PHY_ID_MAX];
  71};
  72
  73static int brcm_usb_phy_init(struct phy *gphy)
  74{
  75        struct brcm_usb_phy *phy = phy_get_drvdata(gphy);
  76        struct brcm_usb_phy_data *priv =
  77                container_of(phy, struct brcm_usb_phy_data, phys[phy->id]);
  78
  79        /*
  80         * Use a lock to make sure a second caller waits until
  81         * the base phy is inited before using it.
  82         */
  83        mutex_lock(&priv->mutex);
  84        if (priv->init_count++ == 0) {
  85                clk_enable(priv->usb_20_clk);
  86                clk_enable(priv->usb_30_clk);
  87                brcm_usb_init_common(&priv->ini);
  88        }
  89        mutex_unlock(&priv->mutex);
  90        if (phy->id == BRCM_USB_PHY_2_0)
  91                brcm_usb_init_eohci(&priv->ini);
  92        else if (phy->id == BRCM_USB_PHY_3_0)
  93                brcm_usb_init_xhci(&priv->ini);
  94        phy->inited = true;
  95        dev_dbg(&gphy->dev, "INIT, id: %d, total: %d\n", phy->id,
  96                priv->init_count);
  97
  98        return 0;
  99}
 100
 101static int brcm_usb_phy_exit(struct phy *gphy)
 102{
 103        struct brcm_usb_phy *phy = phy_get_drvdata(gphy);
 104        struct brcm_usb_phy_data *priv =
 105                container_of(phy, struct brcm_usb_phy_data, phys[phy->id]);
 106
 107        dev_dbg(&gphy->dev, "EXIT\n");
 108        if (phy->id == BRCM_USB_PHY_2_0)
 109                brcm_usb_uninit_eohci(&priv->ini);
 110        if (phy->id == BRCM_USB_PHY_3_0)
 111                brcm_usb_uninit_xhci(&priv->ini);
 112
 113        /* If both xhci and eohci are gone, reset everything else */
 114        mutex_lock(&priv->mutex);
 115        if (--priv->init_count == 0) {
 116                brcm_usb_uninit_common(&priv->ini);
 117                clk_disable(priv->usb_20_clk);
 118                clk_disable(priv->usb_30_clk);
 119        }
 120        mutex_unlock(&priv->mutex);
 121        phy->inited = false;
 122        return 0;
 123}
 124
 125static struct phy_ops brcm_usb_phy_ops = {
 126        .init           = brcm_usb_phy_init,
 127        .exit           = brcm_usb_phy_exit,
 128        .owner          = THIS_MODULE,
 129};
 130
 131static struct phy *brcm_usb_phy_xlate(struct device *dev,
 132                                      struct of_phandle_args *args)
 133{
 134        struct brcm_usb_phy_data *data = dev_get_drvdata(dev);
 135
 136        /*
 137         * values 0 and 1 are for backward compatibility with
 138         * device tree nodes from older bootloaders.
 139         */
 140        switch (args->args[0]) {
 141        case 0:
 142        case PHY_TYPE_USB2:
 143                if (data->phys[BRCM_USB_PHY_2_0].phy)
 144                        return data->phys[BRCM_USB_PHY_2_0].phy;
 145                dev_warn(dev, "Error, 2.0 Phy not found\n");
 146                break;
 147        case 1:
 148        case PHY_TYPE_USB3:
 149                if (data->phys[BRCM_USB_PHY_3_0].phy)
 150                        return data->phys[BRCM_USB_PHY_3_0].phy;
 151                dev_warn(dev, "Error, 3.0 Phy not found\n");
 152                break;
 153        }
 154        return ERR_PTR(-ENODEV);
 155}
 156
 157static int name_to_value(struct value_to_name_map *table, int count,
 158                         const char *name, int *value)
 159{
 160        int x;
 161
 162        *value = 0;
 163        for (x = 0; x < count; x++) {
 164                if (sysfs_streq(name, table[x].name)) {
 165                        *value = x;
 166                        return 0;
 167                }
 168        }
 169        return -EINVAL;
 170}
 171
 172static const char *value_to_name(struct value_to_name_map *table, int count,
 173                                 int value)
 174{
 175        if (value >= count)
 176                return "unknown";
 177        return table[value].name;
 178}
 179
 180static ssize_t dr_mode_show(struct device *dev,
 181                            struct device_attribute *attr,
 182                            char *buf)
 183{
 184        struct brcm_usb_phy_data *priv = dev_get_drvdata(dev);
 185
 186        return sprintf(buf, "%s\n",
 187                value_to_name(&brcm_dr_mode_to_name[0],
 188                              ARRAY_SIZE(brcm_dr_mode_to_name),
 189                              priv->ini.mode));
 190}
 191static DEVICE_ATTR_RO(dr_mode);
 192
 193static ssize_t dual_select_store(struct device *dev,
 194                                 struct device_attribute *attr,
 195                                 const char *buf, size_t len)
 196{
 197        struct brcm_usb_phy_data *priv = dev_get_drvdata(dev);
 198        int value;
 199        int res;
 200
 201        mutex_lock(&sysfs_lock);
 202        res = name_to_value(&brcm_dual_mode_to_name[0],
 203                            ARRAY_SIZE(brcm_dual_mode_to_name), buf, &value);
 204        if (!res) {
 205                brcm_usb_init_set_dual_select(&priv->ini, value);
 206                res = len;
 207        }
 208        mutex_unlock(&sysfs_lock);
 209        return res;
 210}
 211
 212static ssize_t dual_select_show(struct device *dev,
 213                                struct device_attribute *attr,
 214                                char *buf)
 215{
 216        struct brcm_usb_phy_data *priv = dev_get_drvdata(dev);
 217        int value;
 218
 219        mutex_lock(&sysfs_lock);
 220        value = brcm_usb_init_get_dual_select(&priv->ini);
 221        mutex_unlock(&sysfs_lock);
 222        return sprintf(buf, "%s\n",
 223                value_to_name(&brcm_dual_mode_to_name[0],
 224                              ARRAY_SIZE(brcm_dual_mode_to_name),
 225                              value));
 226}
 227static DEVICE_ATTR_RW(dual_select);
 228
 229static struct attribute *brcm_usb_phy_attrs[] = {
 230        &dev_attr_dr_mode.attr,
 231        &dev_attr_dual_select.attr,
 232        NULL
 233};
 234
 235static const struct attribute_group brcm_usb_phy_group = {
 236        .attrs = brcm_usb_phy_attrs,
 237};
 238
 239static int brcm_usb_phy_dvr_init(struct device *dev,
 240                                 struct brcm_usb_phy_data *priv,
 241                                 struct device_node *dn)
 242{
 243        struct phy *gphy;
 244        int err;
 245
 246        priv->usb_20_clk = of_clk_get_by_name(dn, "sw_usb");
 247        if (IS_ERR(priv->usb_20_clk)) {
 248                dev_info(dev, "Clock not found in Device Tree\n");
 249                priv->usb_20_clk = NULL;
 250        }
 251        err = clk_prepare_enable(priv->usb_20_clk);
 252        if (err)
 253                return err;
 254
 255        if (priv->has_eohci) {
 256                gphy = devm_phy_create(dev, NULL, &brcm_usb_phy_ops);
 257                if (IS_ERR(gphy)) {
 258                        dev_err(dev, "failed to create EHCI/OHCI PHY\n");
 259                        return PTR_ERR(gphy);
 260                }
 261                priv->phys[BRCM_USB_PHY_2_0].phy = gphy;
 262                priv->phys[BRCM_USB_PHY_2_0].id = BRCM_USB_PHY_2_0;
 263                phy_set_drvdata(gphy, &priv->phys[BRCM_USB_PHY_2_0]);
 264        }
 265
 266        if (priv->has_xhci) {
 267                gphy = devm_phy_create(dev, NULL, &brcm_usb_phy_ops);
 268                if (IS_ERR(gphy)) {
 269                        dev_err(dev, "failed to create XHCI PHY\n");
 270                        return PTR_ERR(gphy);
 271                }
 272                priv->phys[BRCM_USB_PHY_3_0].phy = gphy;
 273                priv->phys[BRCM_USB_PHY_3_0].id = BRCM_USB_PHY_3_0;
 274                phy_set_drvdata(gphy, &priv->phys[BRCM_USB_PHY_3_0]);
 275
 276                priv->usb_30_clk = of_clk_get_by_name(dn, "sw_usb3");
 277                if (IS_ERR(priv->usb_30_clk)) {
 278                        dev_info(dev,
 279                                 "USB3.0 clock not found in Device Tree\n");
 280                        priv->usb_30_clk = NULL;
 281                }
 282                err = clk_prepare_enable(priv->usb_30_clk);
 283                if (err)
 284                        return err;
 285        }
 286        return 0;
 287}
 288
 289static int brcm_usb_phy_probe(struct platform_device *pdev)
 290{
 291        struct resource *res;
 292        struct device *dev = &pdev->dev;
 293        struct brcm_usb_phy_data *priv;
 294        struct phy_provider *phy_provider;
 295        struct device_node *dn = pdev->dev.of_node;
 296        int err;
 297        const char *mode;
 298
 299        priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
 300        if (!priv)
 301                return -ENOMEM;
 302        platform_set_drvdata(pdev, priv);
 303
 304        priv->ini.family_id = brcmstb_get_family_id();
 305        priv->ini.product_id = brcmstb_get_product_id();
 306        brcm_usb_set_family_map(&priv->ini);
 307        dev_dbg(dev, "Best mapping table is for %s\n",
 308                priv->ini.family_name);
 309        res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
 310        if (!res) {
 311                dev_err(dev, "can't get USB_CTRL base address\n");
 312                return -EINVAL;
 313        }
 314        priv->ini.ctrl_regs = devm_ioremap_resource(dev, res);
 315        if (IS_ERR(priv->ini.ctrl_regs)) {
 316                dev_err(dev, "can't map CTRL register space\n");
 317                return -EINVAL;
 318        }
 319
 320        /* The XHCI EC registers are optional */
 321        res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
 322        if (res) {
 323                priv->ini.xhci_ec_regs =
 324                        devm_ioremap_resource(dev, res);
 325                if (IS_ERR(priv->ini.xhci_ec_regs)) {
 326                        dev_err(dev, "can't map XHCI EC register space\n");
 327                        return -EINVAL;
 328                }
 329        }
 330
 331        of_property_read_u32(dn, "brcm,ipp", &priv->ini.ipp);
 332        of_property_read_u32(dn, "brcm,ioc", &priv->ini.ioc);
 333
 334        priv->ini.mode = USB_CTLR_MODE_HOST;
 335        err = of_property_read_string(dn, "dr_mode", &mode);
 336        if (err == 0) {
 337                name_to_value(&brcm_dr_mode_to_name[0],
 338                              ARRAY_SIZE(brcm_dr_mode_to_name),
 339                        mode, &priv->ini.mode);
 340        }
 341        if (of_property_read_bool(dn, "brcm,has-xhci"))
 342                priv->has_xhci = true;
 343        if (of_property_read_bool(dn, "brcm,has-eohci"))
 344                priv->has_eohci = true;
 345
 346        err = brcm_usb_phy_dvr_init(dev, priv, dn);
 347        if (err)
 348                return err;
 349
 350        mutex_init(&priv->mutex);
 351
 352        /* make sure invert settings are correct */
 353        brcm_usb_init_ipp(&priv->ini);
 354
 355        /*
 356         * Create sysfs entries for mode.
 357         * Remove "dual_select" attribute if not in dual mode
 358         */
 359        if (priv->ini.mode != USB_CTLR_MODE_DRD)
 360                brcm_usb_phy_attrs[1] = NULL;
 361        err = sysfs_create_group(&dev->kobj, &brcm_usb_phy_group);
 362        if (err)
 363                dev_warn(dev, "Error creating sysfs attributes\n");
 364
 365        /* start with everything off */
 366        if (priv->has_xhci)
 367                brcm_usb_uninit_xhci(&priv->ini);
 368        if (priv->has_eohci)
 369                brcm_usb_uninit_eohci(&priv->ini);
 370        brcm_usb_uninit_common(&priv->ini);
 371        clk_disable(priv->usb_20_clk);
 372        clk_disable(priv->usb_30_clk);
 373
 374        phy_provider = devm_of_phy_provider_register(dev, brcm_usb_phy_xlate);
 375        if (IS_ERR(phy_provider))
 376                return PTR_ERR(phy_provider);
 377
 378        return 0;
 379}
 380
 381#ifdef CONFIG_PM_SLEEP
 382static int brcm_usb_phy_suspend(struct device *dev)
 383{
 384        struct brcm_usb_phy_data *priv = dev_get_drvdata(dev);
 385
 386        if (priv->init_count) {
 387                clk_disable(priv->usb_20_clk);
 388                clk_disable(priv->usb_30_clk);
 389        }
 390        return 0;
 391}
 392
 393static int brcm_usb_phy_resume(struct device *dev)
 394{
 395        struct brcm_usb_phy_data *priv = dev_get_drvdata(dev);
 396
 397        clk_enable(priv->usb_20_clk);
 398        clk_enable(priv->usb_30_clk);
 399        brcm_usb_init_ipp(&priv->ini);
 400
 401        /*
 402         * Initialize anything that was previously initialized.
 403         * Uninitialize anything that wasn't previously initialized.
 404         */
 405        if (priv->init_count) {
 406                brcm_usb_init_common(&priv->ini);
 407                if (priv->phys[BRCM_USB_PHY_2_0].inited) {
 408                        brcm_usb_init_eohci(&priv->ini);
 409                } else if (priv->has_eohci) {
 410                        brcm_usb_uninit_eohci(&priv->ini);
 411                        clk_disable(priv->usb_20_clk);
 412                }
 413                if (priv->phys[BRCM_USB_PHY_3_0].inited) {
 414                        brcm_usb_init_xhci(&priv->ini);
 415                } else if (priv->has_xhci) {
 416                        brcm_usb_uninit_xhci(&priv->ini);
 417                        clk_disable(priv->usb_30_clk);
 418                }
 419        } else {
 420                if (priv->has_xhci)
 421                        brcm_usb_uninit_xhci(&priv->ini);
 422                if (priv->has_eohci)
 423                        brcm_usb_uninit_eohci(&priv->ini);
 424                brcm_usb_uninit_common(&priv->ini);
 425                clk_disable(priv->usb_20_clk);
 426                clk_disable(priv->usb_30_clk);
 427        }
 428
 429        return 0;
 430}
 431#endif /* CONFIG_PM_SLEEP */
 432
 433static const struct dev_pm_ops brcm_usb_phy_pm_ops = {
 434        SET_LATE_SYSTEM_SLEEP_PM_OPS(brcm_usb_phy_suspend, brcm_usb_phy_resume)
 435};
 436
 437static const struct of_device_id brcm_usb_dt_ids[] = {
 438        { .compatible = "brcm,brcmstb-usb-phy" },
 439        { /* sentinel */ }
 440};
 441
 442MODULE_DEVICE_TABLE(of, brcm_usb_dt_ids);
 443
 444static struct platform_driver brcm_usb_driver = {
 445        .probe          = brcm_usb_phy_probe,
 446        .driver         = {
 447                .name   = "brcmstb-usb-phy",
 448                .owner  = THIS_MODULE,
 449                .pm = &brcm_usb_phy_pm_ops,
 450                .of_match_table = brcm_usb_dt_ids,
 451        },
 452};
 453
 454module_platform_driver(brcm_usb_driver);
 455
 456MODULE_ALIAS("platform:brcmstb-usb-phy");
 457MODULE_AUTHOR("Al Cooper <acooper@broadcom.com>");
 458MODULE_DESCRIPTION("BRCM USB PHY driver");
 459MODULE_LICENSE("GPL v2");
 460