linux/drivers/usb/typec/typec_wcove.c
<<
>>
Prefs
   1/**
   2 * typec_wcove.c - WhiskeyCove PMIC USB Type-C PHY driver
   3 *
   4 * Copyright (C) 2017 Intel Corporation
   5 * Author: Heikki Krogerus <heikki.krogerus@linux.intel.com>
   6 *
   7 * This program is free software; you can redistribute it and/or modify
   8 * it under the terms of the GNU General Public License version 2 as
   9 * published by the Free Software Foundation.
  10 */
  11
  12#include <linux/acpi.h>
  13#include <linux/module.h>
  14#include <linux/interrupt.h>
  15#include <linux/usb/typec.h>
  16#include <linux/platform_device.h>
  17#include <linux/mfd/intel_soc_pmic.h>
  18
  19/* Register offsets */
  20#define WCOVE_CHGRIRQ0          0x4e09
  21#define WCOVE_PHYCTRL           0x5e07
  22
  23#define USBC_CONTROL1           0x7001
  24#define USBC_CONTROL2           0x7002
  25#define USBC_CONTROL3           0x7003
  26#define USBC_CC1_CTRL           0x7004
  27#define USBC_CC2_CTRL           0x7005
  28#define USBC_STATUS1            0x7007
  29#define USBC_STATUS2            0x7008
  30#define USBC_STATUS3            0x7009
  31#define USBC_IRQ1               0x7015
  32#define USBC_IRQ2               0x7016
  33#define USBC_IRQMASK1           0x7017
  34#define USBC_IRQMASK2           0x7018
  35
  36/* Register bits */
  37
  38#define USBC_CONTROL1_MODE_DRP(r)       (((r) & ~0x7) | 4)
  39
  40#define USBC_CONTROL2_UNATT_SNK         BIT(0)
  41#define USBC_CONTROL2_UNATT_SRC         BIT(1)
  42#define USBC_CONTROL2_DIS_ST            BIT(2)
  43
  44#define USBC_CONTROL3_PD_DIS            BIT(1)
  45
  46#define USBC_CC_CTRL_VCONN_EN           BIT(1)
  47
  48#define USBC_STATUS1_DET_ONGOING        BIT(6)
  49#define USBC_STATUS1_RSLT(r)            ((r) & 0xf)
  50#define USBC_RSLT_NOTHING               0
  51#define USBC_RSLT_SRC_DEFAULT           1
  52#define USBC_RSLT_SRC_1_5A              2
  53#define USBC_RSLT_SRC_3_0A              3
  54#define USBC_RSLT_SNK                   4
  55#define USBC_RSLT_DEBUG_ACC             5
  56#define USBC_RSLT_AUDIO_ACC             6
  57#define USBC_RSLT_UNDEF                 15
  58#define USBC_STATUS1_ORIENT(r)          (((r) >> 4) & 0x3)
  59#define USBC_ORIENT_NORMAL              1
  60#define USBC_ORIENT_REVERSE             2
  61
  62#define USBC_STATUS2_VBUS_REQ           BIT(5)
  63
  64#define USBC_IRQ1_ADCDONE1              BIT(2)
  65#define USBC_IRQ1_OVERTEMP              BIT(1)
  66#define USBC_IRQ1_SHORT                 BIT(0)
  67
  68#define USBC_IRQ2_CC_CHANGE             BIT(7)
  69#define USBC_IRQ2_RX_PD                 BIT(6)
  70#define USBC_IRQ2_RX_HR                 BIT(5)
  71#define USBC_IRQ2_RX_CR                 BIT(4)
  72#define USBC_IRQ2_TX_SUCCESS            BIT(3)
  73#define USBC_IRQ2_TX_FAIL               BIT(2)
  74
  75#define USBC_IRQMASK1_ALL       (USBC_IRQ1_ADCDONE1 | USBC_IRQ1_OVERTEMP | \
  76                                 USBC_IRQ1_SHORT)
  77
  78#define USBC_IRQMASK2_ALL       (USBC_IRQ2_CC_CHANGE | USBC_IRQ2_RX_PD | \
  79                                 USBC_IRQ2_RX_HR | USBC_IRQ2_RX_CR | \
  80                                 USBC_IRQ2_TX_SUCCESS | USBC_IRQ2_TX_FAIL)
  81
  82struct wcove_typec {
  83        struct mutex lock; /* device lock */
  84        struct device *dev;
  85        struct regmap *regmap;
  86        struct typec_port *port;
  87        struct typec_capability cap;
  88        struct typec_partner *partner;
  89};
  90
  91enum wcove_typec_func {
  92        WCOVE_FUNC_DRIVE_VBUS = 1,
  93        WCOVE_FUNC_ORIENTATION,
  94        WCOVE_FUNC_ROLE,
  95        WCOVE_FUNC_DRIVE_VCONN,
  96};
  97
  98enum wcove_typec_orientation {
  99        WCOVE_ORIENTATION_NORMAL,
 100        WCOVE_ORIENTATION_REVERSE,
 101};
 102
 103enum wcove_typec_role {
 104        WCOVE_ROLE_HOST,
 105        WCOVE_ROLE_DEVICE,
 106};
 107
 108static guid_t guid = GUID_INIT(0x482383f0, 0x2876, 0x4e49,
 109                               0x86, 0x85, 0xdb, 0x66, 0x21, 0x1a, 0xf0, 0x37);
 110
 111static int wcove_typec_func(struct wcove_typec *wcove,
 112                            enum wcove_typec_func func, int param)
 113{
 114        union acpi_object *obj;
 115        union acpi_object tmp;
 116        union acpi_object argv4 = ACPI_INIT_DSM_ARGV4(1, &tmp);
 117
 118        tmp.type = ACPI_TYPE_INTEGER;
 119        tmp.integer.value = param;
 120
 121        obj = acpi_evaluate_dsm(ACPI_HANDLE(wcove->dev), &guid, 1, func,
 122                                &argv4);
 123        if (!obj) {
 124                dev_err(wcove->dev, "%s: failed to evaluate _DSM\n", __func__);
 125                return -EIO;
 126        }
 127
 128        ACPI_FREE(obj);
 129        return 0;
 130}
 131
 132static irqreturn_t wcove_typec_irq(int irq, void *data)
 133{
 134        enum typec_role role = TYPEC_SINK;
 135        struct typec_partner_desc partner;
 136        struct wcove_typec *wcove = data;
 137        unsigned int cc1_ctrl;
 138        unsigned int cc2_ctrl;
 139        unsigned int cc_irq1;
 140        unsigned int cc_irq2;
 141        unsigned int status1;
 142        unsigned int status2;
 143        int ret;
 144
 145        mutex_lock(&wcove->lock);
 146
 147        ret = regmap_read(wcove->regmap, USBC_IRQ1, &cc_irq1);
 148        if (ret)
 149                goto err;
 150
 151        ret = regmap_read(wcove->regmap, USBC_IRQ2, &cc_irq2);
 152        if (ret)
 153                goto err;
 154
 155        ret = regmap_read(wcove->regmap, USBC_STATUS1, &status1);
 156        if (ret)
 157                goto err;
 158
 159        ret = regmap_read(wcove->regmap, USBC_STATUS2, &status2);
 160        if (ret)
 161                goto err;
 162
 163        ret = regmap_read(wcove->regmap, USBC_CC1_CTRL, &cc1_ctrl);
 164        if (ret)
 165                goto err;
 166
 167        ret = regmap_read(wcove->regmap, USBC_CC2_CTRL, &cc2_ctrl);
 168        if (ret)
 169                goto err;
 170
 171        if (cc_irq1) {
 172                if (cc_irq1 & USBC_IRQ1_OVERTEMP)
 173                        dev_err(wcove->dev, "VCONN Switch Over Temperature!\n");
 174                if (cc_irq1 & USBC_IRQ1_SHORT)
 175                        dev_err(wcove->dev, "VCONN Switch Short Circuit!\n");
 176                ret = regmap_write(wcove->regmap, USBC_IRQ1, cc_irq1);
 177                if (ret)
 178                        goto err;
 179        }
 180
 181        if (cc_irq2) {
 182                ret = regmap_write(wcove->regmap, USBC_IRQ2, cc_irq2);
 183                if (ret)
 184                        goto err;
 185                /*
 186                 * Ignoring any PD communication interrupts until the PD support
 187                 * is available
 188                 */
 189                if (cc_irq2 & ~USBC_IRQ2_CC_CHANGE) {
 190                        dev_WARN(wcove->dev, "USB PD handling missing\n");
 191                        goto err;
 192                }
 193        }
 194
 195        if (status1 & USBC_STATUS1_DET_ONGOING)
 196                goto out;
 197
 198        if (USBC_STATUS1_RSLT(status1) == USBC_RSLT_NOTHING) {
 199                if (wcove->partner) {
 200                        typec_unregister_partner(wcove->partner);
 201                        wcove->partner = NULL;
 202                }
 203
 204                wcove_typec_func(wcove, WCOVE_FUNC_ORIENTATION,
 205                                 WCOVE_ORIENTATION_NORMAL);
 206
 207                /* This makes sure the device controller is disconnected */
 208                wcove_typec_func(wcove, WCOVE_FUNC_ROLE, WCOVE_ROLE_HOST);
 209
 210                /* Port to default role */
 211                typec_set_data_role(wcove->port, TYPEC_DEVICE);
 212                typec_set_pwr_role(wcove->port, TYPEC_SINK);
 213                typec_set_pwr_opmode(wcove->port, TYPEC_PWR_MODE_USB);
 214
 215                goto out;
 216        }
 217
 218        if (wcove->partner)
 219                goto out;
 220
 221        switch (USBC_STATUS1_ORIENT(status1)) {
 222        case USBC_ORIENT_NORMAL:
 223                wcove_typec_func(wcove, WCOVE_FUNC_ORIENTATION,
 224                                 WCOVE_ORIENTATION_NORMAL);
 225                break;
 226        case USBC_ORIENT_REVERSE:
 227                wcove_typec_func(wcove, WCOVE_FUNC_ORIENTATION,
 228                                 WCOVE_ORIENTATION_REVERSE);
 229        default:
 230                break;
 231        }
 232
 233        memset(&partner, 0, sizeof(partner));
 234
 235        switch (USBC_STATUS1_RSLT(status1)) {
 236        case USBC_RSLT_SRC_DEFAULT:
 237                typec_set_pwr_opmode(wcove->port, TYPEC_PWR_MODE_USB);
 238                break;
 239        case USBC_RSLT_SRC_1_5A:
 240                typec_set_pwr_opmode(wcove->port, TYPEC_PWR_MODE_1_5A);
 241                break;
 242        case USBC_RSLT_SRC_3_0A:
 243                typec_set_pwr_opmode(wcove->port, TYPEC_PWR_MODE_3_0A);
 244                break;
 245        case USBC_RSLT_SNK:
 246                role = TYPEC_SOURCE;
 247                break;
 248        case USBC_RSLT_DEBUG_ACC:
 249                partner.accessory = TYPEC_ACCESSORY_DEBUG;
 250                break;
 251        case USBC_RSLT_AUDIO_ACC:
 252                partner.accessory = TYPEC_ACCESSORY_AUDIO;
 253                break;
 254        default:
 255                dev_WARN(wcove->dev, "%s Undefined result\n", __func__);
 256                goto err;
 257        }
 258
 259        if (role == TYPEC_SINK) {
 260                wcove_typec_func(wcove, WCOVE_FUNC_ROLE, WCOVE_ROLE_DEVICE);
 261                typec_set_data_role(wcove->port, TYPEC_DEVICE);
 262                typec_set_pwr_role(wcove->port, TYPEC_SINK);
 263        } else {
 264                wcove_typec_func(wcove, WCOVE_FUNC_ROLE, WCOVE_ROLE_HOST);
 265                typec_set_pwr_role(wcove->port, TYPEC_SOURCE);
 266                typec_set_data_role(wcove->port, TYPEC_HOST);
 267        }
 268
 269        wcove->partner = typec_register_partner(wcove->port, &partner);
 270        if (!wcove->partner)
 271                dev_err(wcove->dev, "failed register partner\n");
 272out:
 273        /* If either CC pins is requesting VCONN, we turn it on */
 274        if ((cc1_ctrl & USBC_CC_CTRL_VCONN_EN) ||
 275            (cc2_ctrl & USBC_CC_CTRL_VCONN_EN))
 276                wcove_typec_func(wcove, WCOVE_FUNC_DRIVE_VCONN, true);
 277        else
 278                wcove_typec_func(wcove, WCOVE_FUNC_DRIVE_VCONN, false);
 279
 280        /* Relying on the FSM to know when we need to drive VBUS. */
 281        wcove_typec_func(wcove, WCOVE_FUNC_DRIVE_VBUS,
 282                         !!(status2 & USBC_STATUS2_VBUS_REQ));
 283err:
 284        /* REVISIT: Clear WhiskeyCove CHGR Type-C interrupt */
 285        regmap_write(wcove->regmap, WCOVE_CHGRIRQ0, BIT(5));
 286
 287        mutex_unlock(&wcove->lock);
 288        return IRQ_HANDLED;
 289}
 290
 291static int wcove_typec_probe(struct platform_device *pdev)
 292{
 293        struct intel_soc_pmic *pmic = dev_get_drvdata(pdev->dev.parent);
 294        struct wcove_typec *wcove;
 295        unsigned int val;
 296        int ret;
 297
 298        wcove = devm_kzalloc(&pdev->dev, sizeof(*wcove), GFP_KERNEL);
 299        if (!wcove)
 300                return -ENOMEM;
 301
 302        mutex_init(&wcove->lock);
 303        wcove->dev = &pdev->dev;
 304        wcove->regmap = pmic->regmap;
 305
 306        ret = regmap_irq_get_virq(pmic->irq_chip_data_chgr,
 307                                  platform_get_irq(pdev, 0));
 308        if (ret < 0)
 309                return ret;
 310
 311        ret = devm_request_threaded_irq(&pdev->dev, ret, NULL,
 312                                        wcove_typec_irq, IRQF_ONESHOT,
 313                                        "wcove_typec", wcove);
 314        if (ret)
 315                return ret;
 316
 317        if (!acpi_check_dsm(ACPI_HANDLE(&pdev->dev), &guid, 0, 0x1f)) {
 318                dev_err(&pdev->dev, "Missing _DSM functions\n");
 319                return -ENODEV;
 320        }
 321
 322        wcove->cap.type = TYPEC_PORT_DRP;
 323        wcove->cap.revision = USB_TYPEC_REV_1_1;
 324        wcove->cap.prefer_role = TYPEC_NO_PREFERRED_ROLE;
 325
 326        /* Make sure the PD PHY is disabled until USB PD is available */
 327        regmap_read(wcove->regmap, USBC_CONTROL3, &val);
 328        regmap_write(wcove->regmap, USBC_CONTROL3, val | USBC_CONTROL3_PD_DIS);
 329
 330        /* DRP mode without accessory support */
 331        regmap_read(wcove->regmap, USBC_CONTROL1, &val);
 332        regmap_write(wcove->regmap, USBC_CONTROL1, USBC_CONTROL1_MODE_DRP(val));
 333
 334        wcove->port = typec_register_port(&pdev->dev, &wcove->cap);
 335        if (!wcove->port)
 336                return -ENODEV;
 337
 338        /* Unmask everything */
 339        regmap_read(wcove->regmap, USBC_IRQMASK1, &val);
 340        regmap_write(wcove->regmap, USBC_IRQMASK1, val & ~USBC_IRQMASK1_ALL);
 341        regmap_read(wcove->regmap, USBC_IRQMASK2, &val);
 342        regmap_write(wcove->regmap, USBC_IRQMASK2, val & ~USBC_IRQMASK2_ALL);
 343
 344        platform_set_drvdata(pdev, wcove);
 345        return 0;
 346}
 347
 348static int wcove_typec_remove(struct platform_device *pdev)
 349{
 350        struct wcove_typec *wcove = platform_get_drvdata(pdev);
 351        unsigned int val;
 352
 353        /* Mask everything */
 354        regmap_read(wcove->regmap, USBC_IRQMASK1, &val);
 355        regmap_write(wcove->regmap, USBC_IRQMASK1, val | USBC_IRQMASK1_ALL);
 356        regmap_read(wcove->regmap, USBC_IRQMASK2, &val);
 357        regmap_write(wcove->regmap, USBC_IRQMASK2, val | USBC_IRQMASK2_ALL);
 358
 359        typec_unregister_partner(wcove->partner);
 360        typec_unregister_port(wcove->port);
 361        return 0;
 362}
 363
 364static struct platform_driver wcove_typec_driver = {
 365        .driver = {
 366                .name           = "bxt_wcove_usbc",
 367        },
 368        .probe                  = wcove_typec_probe,
 369        .remove                 = wcove_typec_remove,
 370};
 371
 372module_platform_driver(wcove_typec_driver);
 373
 374MODULE_AUTHOR("Intel Corporation");
 375MODULE_LICENSE("GPL v2");
 376MODULE_DESCRIPTION("WhiskeyCove PMIC USB Type-C PHY driver");
 377MODULE_ALIAS("platform:bxt_wcove_usbc");
 378