linux/drivers/usb/typec/tipd/core.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 * Driver for TI TPS6598x USB Power Delivery controller family
   4 *
   5 * Copyright (C) 2017, Intel Corporation
   6 * Author: Heikki Krogerus <heikki.krogerus@linux.intel.com>
   7 */
   8
   9#include <linux/i2c.h>
  10#include <linux/acpi.h>
  11#include <linux/module.h>
  12#include <linux/power_supply.h>
  13#include <linux/regmap.h>
  14#include <linux/interrupt.h>
  15#include <linux/usb/typec.h>
  16#include <linux/usb/role.h>
  17
  18#include "tps6598x.h"
  19#include "trace.h"
  20
  21/* Register offsets */
  22#define TPS_REG_VID                     0x00
  23#define TPS_REG_MODE                    0x03
  24#define TPS_REG_CMD1                    0x08
  25#define TPS_REG_DATA1                   0x09
  26#define TPS_REG_INT_EVENT1              0x14
  27#define TPS_REG_INT_EVENT2              0x15
  28#define TPS_REG_INT_MASK1               0x16
  29#define TPS_REG_INT_MASK2               0x17
  30#define TPS_REG_INT_CLEAR1              0x18
  31#define TPS_REG_INT_CLEAR2              0x19
  32#define TPS_REG_STATUS                  0x1a
  33#define TPS_REG_SYSTEM_CONF             0x28
  34#define TPS_REG_CTRL_CONF               0x29
  35#define TPS_REG_POWER_STATUS            0x3f
  36#define TPS_REG_RX_IDENTITY_SOP         0x48
  37#define TPS_REG_DATA_STATUS             0x5f
  38
  39/* TPS_REG_SYSTEM_CONF bits */
  40#define TPS_SYSCONF_PORTINFO(c)         ((c) & 7)
  41
  42enum {
  43        TPS_PORTINFO_SINK,
  44        TPS_PORTINFO_SINK_ACCESSORY,
  45        TPS_PORTINFO_DRP_UFP,
  46        TPS_PORTINFO_DRP_UFP_DRD,
  47        TPS_PORTINFO_DRP_DFP,
  48        TPS_PORTINFO_DRP_DFP_DRD,
  49        TPS_PORTINFO_SOURCE,
  50};
  51
  52/* TPS_REG_RX_IDENTITY_SOP */
  53struct tps6598x_rx_identity_reg {
  54        u8 status;
  55        struct usb_pd_identity identity;
  56} __packed;
  57
  58/* Standard Task return codes */
  59#define TPS_TASK_TIMEOUT                1
  60#define TPS_TASK_REJECTED               3
  61
  62enum {
  63        TPS_MODE_APP,
  64        TPS_MODE_BOOT,
  65        TPS_MODE_BIST,
  66        TPS_MODE_DISC,
  67};
  68
  69static const char *const modes[] = {
  70        [TPS_MODE_APP]  = "APP ",
  71        [TPS_MODE_BOOT] = "BOOT",
  72        [TPS_MODE_BIST] = "BIST",
  73        [TPS_MODE_DISC] = "DISC",
  74};
  75
  76/* Unrecognized commands will be replaced with "!CMD" */
  77#define INVALID_CMD(_cmd_)              (_cmd_ == 0x444d4321)
  78
  79struct tps6598x {
  80        struct device *dev;
  81        struct regmap *regmap;
  82        struct mutex lock; /* device lock */
  83        u8 i2c_protocol:1;
  84
  85        struct typec_port *port;
  86        struct typec_partner *partner;
  87        struct usb_pd_identity partner_identity;
  88        struct usb_role_switch *role_sw;
  89        struct typec_capability typec_cap;
  90
  91        struct power_supply *psy;
  92        struct power_supply_desc psy_desc;
  93        enum power_supply_usb_type usb_type;
  94};
  95
  96static enum power_supply_property tps6598x_psy_props[] = {
  97        POWER_SUPPLY_PROP_USB_TYPE,
  98        POWER_SUPPLY_PROP_ONLINE,
  99};
 100
 101static enum power_supply_usb_type tps6598x_psy_usb_types[] = {
 102        POWER_SUPPLY_USB_TYPE_C,
 103        POWER_SUPPLY_USB_TYPE_PD,
 104};
 105
 106static const char *tps6598x_psy_name_prefix = "tps6598x-source-psy-";
 107
 108/*
 109 * Max data bytes for Data1, Data2, and other registers. See ch 1.3.2:
 110 * https://www.ti.com/lit/ug/slvuan1a/slvuan1a.pdf
 111 */
 112#define TPS_MAX_LEN     64
 113
 114static int
 115tps6598x_block_read(struct tps6598x *tps, u8 reg, void *val, size_t len)
 116{
 117        u8 data[TPS_MAX_LEN + 1];
 118        int ret;
 119
 120        if (WARN_ON(len + 1 > sizeof(data)))
 121                return -EINVAL;
 122
 123        if (!tps->i2c_protocol)
 124                return regmap_raw_read(tps->regmap, reg, val, len);
 125
 126        ret = regmap_raw_read(tps->regmap, reg, data, sizeof(data));
 127        if (ret)
 128                return ret;
 129
 130        if (data[0] < len)
 131                return -EIO;
 132
 133        memcpy(val, &data[1], len);
 134        return 0;
 135}
 136
 137static int tps6598x_block_write(struct tps6598x *tps, u8 reg,
 138                                const void *val, size_t len)
 139{
 140        u8 data[TPS_MAX_LEN + 1];
 141
 142        if (!tps->i2c_protocol)
 143                return regmap_raw_write(tps->regmap, reg, val, len);
 144
 145        data[0] = len;
 146        memcpy(&data[1], val, len);
 147
 148        return regmap_raw_write(tps->regmap, reg, data, sizeof(data));
 149}
 150
 151static inline int tps6598x_read16(struct tps6598x *tps, u8 reg, u16 *val)
 152{
 153        return tps6598x_block_read(tps, reg, val, sizeof(u16));
 154}
 155
 156static inline int tps6598x_read32(struct tps6598x *tps, u8 reg, u32 *val)
 157{
 158        return tps6598x_block_read(tps, reg, val, sizeof(u32));
 159}
 160
 161static inline int tps6598x_read64(struct tps6598x *tps, u8 reg, u64 *val)
 162{
 163        return tps6598x_block_read(tps, reg, val, sizeof(u64));
 164}
 165
 166static inline int tps6598x_write16(struct tps6598x *tps, u8 reg, u16 val)
 167{
 168        return tps6598x_block_write(tps, reg, &val, sizeof(u16));
 169}
 170
 171static inline int tps6598x_write32(struct tps6598x *tps, u8 reg, u32 val)
 172{
 173        return tps6598x_block_write(tps, reg, &val, sizeof(u32));
 174}
 175
 176static inline int tps6598x_write64(struct tps6598x *tps, u8 reg, u64 val)
 177{
 178        return tps6598x_block_write(tps, reg, &val, sizeof(u64));
 179}
 180
 181static inline int
 182tps6598x_write_4cc(struct tps6598x *tps, u8 reg, const char *val)
 183{
 184        return tps6598x_block_write(tps, reg, val, 4);
 185}
 186
 187static int tps6598x_read_partner_identity(struct tps6598x *tps)
 188{
 189        struct tps6598x_rx_identity_reg id;
 190        int ret;
 191
 192        ret = tps6598x_block_read(tps, TPS_REG_RX_IDENTITY_SOP,
 193                                  &id, sizeof(id));
 194        if (ret)
 195                return ret;
 196
 197        tps->partner_identity = id.identity;
 198
 199        return 0;
 200}
 201
 202static void tps6598x_set_data_role(struct tps6598x *tps,
 203                                   enum typec_data_role role, bool connected)
 204{
 205        enum usb_role role_val;
 206
 207        if (role == TYPEC_HOST)
 208                role_val = USB_ROLE_HOST;
 209        else
 210                role_val = USB_ROLE_DEVICE;
 211
 212        if (!connected)
 213                role_val = USB_ROLE_NONE;
 214
 215        usb_role_switch_set_role(tps->role_sw, role_val);
 216        typec_set_data_role(tps->port, role);
 217}
 218
 219static int tps6598x_connect(struct tps6598x *tps, u32 status)
 220{
 221        struct typec_partner_desc desc;
 222        enum typec_pwr_opmode mode;
 223        u16 pwr_status;
 224        int ret;
 225
 226        if (tps->partner)
 227                return 0;
 228
 229        ret = tps6598x_read16(tps, TPS_REG_POWER_STATUS, &pwr_status);
 230        if (ret < 0)
 231                return ret;
 232
 233        mode = TPS_POWER_STATUS_PWROPMODE(pwr_status);
 234
 235        desc.usb_pd = mode == TYPEC_PWR_MODE_PD;
 236        desc.accessory = TYPEC_ACCESSORY_NONE; /* XXX: handle accessories */
 237        desc.identity = NULL;
 238
 239        if (desc.usb_pd) {
 240                ret = tps6598x_read_partner_identity(tps);
 241                if (ret)
 242                        return ret;
 243                desc.identity = &tps->partner_identity;
 244        }
 245
 246        typec_set_pwr_opmode(tps->port, mode);
 247        typec_set_pwr_role(tps->port, TPS_STATUS_TO_TYPEC_PORTROLE(status));
 248        typec_set_vconn_role(tps->port, TPS_STATUS_TO_TYPEC_VCONN(status));
 249        tps6598x_set_data_role(tps, TPS_STATUS_TO_TYPEC_DATAROLE(status), true);
 250
 251        tps->partner = typec_register_partner(tps->port, &desc);
 252        if (IS_ERR(tps->partner))
 253                return PTR_ERR(tps->partner);
 254
 255        if (desc.identity)
 256                typec_partner_set_identity(tps->partner);
 257
 258        power_supply_changed(tps->psy);
 259
 260        return 0;
 261}
 262
 263static void tps6598x_disconnect(struct tps6598x *tps, u32 status)
 264{
 265        if (!IS_ERR(tps->partner))
 266                typec_unregister_partner(tps->partner);
 267        tps->partner = NULL;
 268        typec_set_pwr_opmode(tps->port, TYPEC_PWR_MODE_USB);
 269        typec_set_pwr_role(tps->port, TPS_STATUS_TO_TYPEC_PORTROLE(status));
 270        typec_set_vconn_role(tps->port, TPS_STATUS_TO_TYPEC_VCONN(status));
 271        tps6598x_set_data_role(tps, TPS_STATUS_TO_TYPEC_DATAROLE(status), false);
 272
 273        power_supply_changed(tps->psy);
 274}
 275
 276static int tps6598x_exec_cmd(struct tps6598x *tps, const char *cmd,
 277                             size_t in_len, u8 *in_data,
 278                             size_t out_len, u8 *out_data)
 279{
 280        unsigned long timeout;
 281        u32 val;
 282        int ret;
 283
 284        ret = tps6598x_read32(tps, TPS_REG_CMD1, &val);
 285        if (ret)
 286                return ret;
 287        if (val && !INVALID_CMD(val))
 288                return -EBUSY;
 289
 290        if (in_len) {
 291                ret = tps6598x_block_write(tps, TPS_REG_DATA1,
 292                                           in_data, in_len);
 293                if (ret)
 294                        return ret;
 295        }
 296
 297        ret = tps6598x_write_4cc(tps, TPS_REG_CMD1, cmd);
 298        if (ret < 0)
 299                return ret;
 300
 301        /* XXX: Using 1s for now, but it may not be enough for every command. */
 302        timeout = jiffies + msecs_to_jiffies(1000);
 303
 304        do {
 305                ret = tps6598x_read32(tps, TPS_REG_CMD1, &val);
 306                if (ret)
 307                        return ret;
 308                if (INVALID_CMD(val))
 309                        return -EINVAL;
 310
 311                if (time_is_before_jiffies(timeout))
 312                        return -ETIMEDOUT;
 313        } while (val);
 314
 315        if (out_len) {
 316                ret = tps6598x_block_read(tps, TPS_REG_DATA1,
 317                                          out_data, out_len);
 318                if (ret)
 319                        return ret;
 320                val = out_data[0];
 321        } else {
 322                ret = tps6598x_block_read(tps, TPS_REG_DATA1, &val, sizeof(u8));
 323                if (ret)
 324                        return ret;
 325        }
 326
 327        switch (val) {
 328        case TPS_TASK_TIMEOUT:
 329                return -ETIMEDOUT;
 330        case TPS_TASK_REJECTED:
 331                return -EPERM;
 332        default:
 333                break;
 334        }
 335
 336        return 0;
 337}
 338
 339static int tps6598x_dr_set(struct typec_port *port, enum typec_data_role role)
 340{
 341        const char *cmd = (role == TYPEC_DEVICE) ? "SWUF" : "SWDF";
 342        struct tps6598x *tps = typec_get_drvdata(port);
 343        u32 status;
 344        int ret;
 345
 346        mutex_lock(&tps->lock);
 347
 348        ret = tps6598x_exec_cmd(tps, cmd, 0, NULL, 0, NULL);
 349        if (ret)
 350                goto out_unlock;
 351
 352        ret = tps6598x_read32(tps, TPS_REG_STATUS, &status);
 353        if (ret)
 354                goto out_unlock;
 355
 356        if (role != TPS_STATUS_TO_TYPEC_DATAROLE(status)) {
 357                ret = -EPROTO;
 358                goto out_unlock;
 359        }
 360
 361        tps6598x_set_data_role(tps, role, true);
 362
 363out_unlock:
 364        mutex_unlock(&tps->lock);
 365
 366        return ret;
 367}
 368
 369static int tps6598x_pr_set(struct typec_port *port, enum typec_role role)
 370{
 371        const char *cmd = (role == TYPEC_SINK) ? "SWSk" : "SWSr";
 372        struct tps6598x *tps = typec_get_drvdata(port);
 373        u32 status;
 374        int ret;
 375
 376        mutex_lock(&tps->lock);
 377
 378        ret = tps6598x_exec_cmd(tps, cmd, 0, NULL, 0, NULL);
 379        if (ret)
 380                goto out_unlock;
 381
 382        ret = tps6598x_read32(tps, TPS_REG_STATUS, &status);
 383        if (ret)
 384                goto out_unlock;
 385
 386        if (role != TPS_STATUS_TO_TYPEC_PORTROLE(status)) {
 387                ret = -EPROTO;
 388                goto out_unlock;
 389        }
 390
 391        typec_set_pwr_role(tps->port, role);
 392
 393out_unlock:
 394        mutex_unlock(&tps->lock);
 395
 396        return ret;
 397}
 398
 399static const struct typec_operations tps6598x_ops = {
 400        .dr_set = tps6598x_dr_set,
 401        .pr_set = tps6598x_pr_set,
 402};
 403
 404static irqreturn_t tps6598x_interrupt(int irq, void *data)
 405{
 406        struct tps6598x *tps = data;
 407        u64 event1;
 408        u64 event2;
 409        u32 status, data_status;
 410        u16 pwr_status;
 411        int ret;
 412
 413        mutex_lock(&tps->lock);
 414
 415        ret = tps6598x_read64(tps, TPS_REG_INT_EVENT1, &event1);
 416        ret |= tps6598x_read64(tps, TPS_REG_INT_EVENT2, &event2);
 417        if (ret) {
 418                dev_err(tps->dev, "%s: failed to read events\n", __func__);
 419                goto err_unlock;
 420        }
 421        trace_tps6598x_irq(event1, event2);
 422
 423        ret = tps6598x_read32(tps, TPS_REG_STATUS, &status);
 424        if (ret) {
 425                dev_err(tps->dev, "%s: failed to read status\n", __func__);
 426                goto err_clear_ints;
 427        }
 428        trace_tps6598x_status(status);
 429
 430        if ((event1 | event2) & TPS_REG_INT_POWER_STATUS_UPDATE) {
 431                ret = tps6598x_read16(tps, TPS_REG_POWER_STATUS, &pwr_status);
 432                if (ret < 0) {
 433                        dev_err(tps->dev, "failed to read power status: %d\n", ret);
 434                        goto err_clear_ints;
 435                }
 436                trace_tps6598x_power_status(pwr_status);
 437        }
 438
 439        if ((event1 | event2) & TPS_REG_INT_DATA_STATUS_UPDATE) {
 440                ret = tps6598x_read32(tps, TPS_REG_DATA_STATUS, &data_status);
 441                if (ret < 0) {
 442                        dev_err(tps->dev, "failed to read data status: %d\n", ret);
 443                        goto err_clear_ints;
 444                }
 445                trace_tps6598x_data_status(data_status);
 446        }
 447
 448        /* Handle plug insert or removal */
 449        if ((event1 | event2) & TPS_REG_INT_PLUG_EVENT) {
 450                if (status & TPS_STATUS_PLUG_PRESENT) {
 451                        ret = tps6598x_connect(tps, status);
 452                        if (ret)
 453                                dev_err(tps->dev,
 454                                        "failed to register partner\n");
 455                } else {
 456                        tps6598x_disconnect(tps, status);
 457                }
 458        }
 459
 460err_clear_ints:
 461        tps6598x_write64(tps, TPS_REG_INT_CLEAR1, event1);
 462        tps6598x_write64(tps, TPS_REG_INT_CLEAR2, event2);
 463
 464err_unlock:
 465        mutex_unlock(&tps->lock);
 466
 467        return IRQ_HANDLED;
 468}
 469
 470static int tps6598x_check_mode(struct tps6598x *tps)
 471{
 472        char mode[5] = { };
 473        int ret;
 474
 475        ret = tps6598x_read32(tps, TPS_REG_MODE, (void *)mode);
 476        if (ret)
 477                return ret;
 478
 479        switch (match_string(modes, ARRAY_SIZE(modes), mode)) {
 480        case TPS_MODE_APP:
 481                return 0;
 482        case TPS_MODE_BOOT:
 483                dev_warn(tps->dev, "dead-battery condition\n");
 484                return 0;
 485        case TPS_MODE_BIST:
 486        case TPS_MODE_DISC:
 487        default:
 488                dev_err(tps->dev, "controller in unsupported mode \"%s\"\n",
 489                        mode);
 490                break;
 491        }
 492
 493        return -ENODEV;
 494}
 495
 496static const struct regmap_config tps6598x_regmap_config = {
 497        .reg_bits = 8,
 498        .val_bits = 8,
 499        .max_register = 0x7F,
 500};
 501
 502static int tps6598x_psy_get_online(struct tps6598x *tps,
 503                                   union power_supply_propval *val)
 504{
 505        int ret;
 506        u16 pwr_status;
 507
 508        ret = tps6598x_read16(tps, TPS_REG_POWER_STATUS, &pwr_status);
 509        if (ret < 0)
 510                return ret;
 511
 512        if (TPS_POWER_STATUS_CONNECTION(pwr_status) &&
 513            TPS_POWER_STATUS_SOURCESINK(pwr_status)) {
 514                val->intval = 1;
 515        } else {
 516                val->intval = 0;
 517        }
 518        return 0;
 519}
 520
 521static int tps6598x_psy_get_prop(struct power_supply *psy,
 522                                 enum power_supply_property psp,
 523                                 union power_supply_propval *val)
 524{
 525        struct tps6598x *tps = power_supply_get_drvdata(psy);
 526        u16 pwr_status;
 527        int ret = 0;
 528
 529        switch (psp) {
 530        case POWER_SUPPLY_PROP_USB_TYPE:
 531                ret = tps6598x_read16(tps, TPS_REG_POWER_STATUS, &pwr_status);
 532                if (ret < 0)
 533                        return ret;
 534                if (TPS_POWER_STATUS_PWROPMODE(pwr_status) == TYPEC_PWR_MODE_PD)
 535                        val->intval = POWER_SUPPLY_USB_TYPE_PD;
 536                else
 537                        val->intval = POWER_SUPPLY_USB_TYPE_C;
 538                break;
 539        case POWER_SUPPLY_PROP_ONLINE:
 540                ret = tps6598x_psy_get_online(tps, val);
 541                break;
 542        default:
 543                ret = -EINVAL;
 544                break;
 545        }
 546
 547        return ret;
 548}
 549
 550static int devm_tps6598_psy_register(struct tps6598x *tps)
 551{
 552        struct power_supply_config psy_cfg = {};
 553        const char *port_dev_name = dev_name(tps->dev);
 554        char *psy_name;
 555
 556        psy_cfg.drv_data = tps;
 557        psy_cfg.fwnode = dev_fwnode(tps->dev);
 558
 559        psy_name = devm_kasprintf(tps->dev, GFP_KERNEL, "%s%s", tps6598x_psy_name_prefix,
 560                                  port_dev_name);
 561        if (!psy_name)
 562                return -ENOMEM;
 563
 564        tps->psy_desc.name = psy_name;
 565        tps->psy_desc.type = POWER_SUPPLY_TYPE_USB;
 566        tps->psy_desc.usb_types = tps6598x_psy_usb_types;
 567        tps->psy_desc.num_usb_types = ARRAY_SIZE(tps6598x_psy_usb_types);
 568        tps->psy_desc.properties = tps6598x_psy_props;
 569        tps->psy_desc.num_properties = ARRAY_SIZE(tps6598x_psy_props);
 570        tps->psy_desc.get_property = tps6598x_psy_get_prop;
 571
 572        tps->usb_type = POWER_SUPPLY_USB_TYPE_C;
 573
 574        tps->psy = devm_power_supply_register(tps->dev, &tps->psy_desc,
 575                                               &psy_cfg);
 576        return PTR_ERR_OR_ZERO(tps->psy);
 577}
 578
 579static int tps6598x_probe(struct i2c_client *client)
 580{
 581        struct typec_capability typec_cap = { };
 582        struct tps6598x *tps;
 583        struct fwnode_handle *fwnode;
 584        u32 status;
 585        u32 conf;
 586        u32 vid;
 587        int ret;
 588
 589        tps = devm_kzalloc(&client->dev, sizeof(*tps), GFP_KERNEL);
 590        if (!tps)
 591                return -ENOMEM;
 592
 593        mutex_init(&tps->lock);
 594        tps->dev = &client->dev;
 595
 596        tps->regmap = devm_regmap_init_i2c(client, &tps6598x_regmap_config);
 597        if (IS_ERR(tps->regmap))
 598                return PTR_ERR(tps->regmap);
 599
 600        ret = tps6598x_read32(tps, TPS_REG_VID, &vid);
 601        if (ret < 0 || !vid)
 602                return -ENODEV;
 603
 604        /*
 605         * Checking can the adapter handle SMBus protocol. If it can not, the
 606         * driver needs to take care of block reads separately.
 607         *
 608         * FIXME: Testing with I2C_FUNC_I2C. regmap-i2c uses I2C protocol
 609         * unconditionally if the adapter has I2C_FUNC_I2C set.
 610         */
 611        if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
 612                tps->i2c_protocol = true;
 613
 614        /* Make sure the controller has application firmware running */
 615        ret = tps6598x_check_mode(tps);
 616        if (ret)
 617                return ret;
 618
 619        ret = tps6598x_read32(tps, TPS_REG_STATUS, &status);
 620        if (ret < 0)
 621                return ret;
 622        trace_tps6598x_status(status);
 623
 624        ret = tps6598x_read32(tps, TPS_REG_SYSTEM_CONF, &conf);
 625        if (ret < 0)
 626                return ret;
 627
 628        /*
 629         * This fwnode has a "compatible" property, but is never populated as a
 630         * struct device. Instead we simply parse it to read the properties.
 631         * This breaks fw_devlink=on. To maintain backward compatibility
 632         * with existing DT files, we work around this by deleting any
 633         * fwnode_links to/from this fwnode.
 634         */
 635        fwnode = device_get_named_child_node(&client->dev, "connector");
 636        if (fwnode)
 637                fw_devlink_purge_absent_suppliers(fwnode);
 638
 639        tps->role_sw = fwnode_usb_role_switch_get(fwnode);
 640        if (IS_ERR(tps->role_sw)) {
 641                ret = PTR_ERR(tps->role_sw);
 642                goto err_fwnode_put;
 643        }
 644
 645        typec_cap.revision = USB_TYPEC_REV_1_2;
 646        typec_cap.pd_revision = 0x200;
 647        typec_cap.prefer_role = TYPEC_NO_PREFERRED_ROLE;
 648        typec_cap.driver_data = tps;
 649        typec_cap.ops = &tps6598x_ops;
 650        typec_cap.fwnode = fwnode;
 651
 652        switch (TPS_SYSCONF_PORTINFO(conf)) {
 653        case TPS_PORTINFO_SINK_ACCESSORY:
 654        case TPS_PORTINFO_SINK:
 655                typec_cap.type = TYPEC_PORT_SNK;
 656                typec_cap.data = TYPEC_PORT_UFP;
 657                break;
 658        case TPS_PORTINFO_DRP_UFP_DRD:
 659        case TPS_PORTINFO_DRP_DFP_DRD:
 660                typec_cap.type = TYPEC_PORT_DRP;
 661                typec_cap.data = TYPEC_PORT_DRD;
 662                break;
 663        case TPS_PORTINFO_DRP_UFP:
 664                typec_cap.type = TYPEC_PORT_DRP;
 665                typec_cap.data = TYPEC_PORT_UFP;
 666                break;
 667        case TPS_PORTINFO_DRP_DFP:
 668                typec_cap.type = TYPEC_PORT_DRP;
 669                typec_cap.data = TYPEC_PORT_DFP;
 670                break;
 671        case TPS_PORTINFO_SOURCE:
 672                typec_cap.type = TYPEC_PORT_SRC;
 673                typec_cap.data = TYPEC_PORT_DFP;
 674                break;
 675        default:
 676                ret = -ENODEV;
 677                goto err_role_put;
 678        }
 679
 680        ret = devm_tps6598_psy_register(tps);
 681        if (ret)
 682                return ret;
 683
 684        tps->port = typec_register_port(&client->dev, &typec_cap);
 685        if (IS_ERR(tps->port)) {
 686                ret = PTR_ERR(tps->port);
 687                goto err_role_put;
 688        }
 689        fwnode_handle_put(fwnode);
 690
 691        if (status & TPS_STATUS_PLUG_PRESENT) {
 692                ret = tps6598x_connect(tps, status);
 693                if (ret)
 694                        dev_err(&client->dev, "failed to register partner\n");
 695        }
 696
 697        ret = devm_request_threaded_irq(&client->dev, client->irq, NULL,
 698                                        tps6598x_interrupt,
 699                                        IRQF_SHARED | IRQF_ONESHOT,
 700                                        dev_name(&client->dev), tps);
 701        if (ret) {
 702                tps6598x_disconnect(tps, 0);
 703                typec_unregister_port(tps->port);
 704                goto err_role_put;
 705        }
 706
 707        i2c_set_clientdata(client, tps);
 708
 709        return 0;
 710
 711err_role_put:
 712        usb_role_switch_put(tps->role_sw);
 713err_fwnode_put:
 714        fwnode_handle_put(fwnode);
 715
 716        return ret;
 717}
 718
 719static int tps6598x_remove(struct i2c_client *client)
 720{
 721        struct tps6598x *tps = i2c_get_clientdata(client);
 722
 723        tps6598x_disconnect(tps, 0);
 724        typec_unregister_port(tps->port);
 725        usb_role_switch_put(tps->role_sw);
 726
 727        return 0;
 728}
 729
 730static const struct of_device_id tps6598x_of_match[] = {
 731        { .compatible = "ti,tps6598x", },
 732        {}
 733};
 734MODULE_DEVICE_TABLE(of, tps6598x_of_match);
 735
 736static const struct i2c_device_id tps6598x_id[] = {
 737        { "tps6598x" },
 738        { }
 739};
 740MODULE_DEVICE_TABLE(i2c, tps6598x_id);
 741
 742static struct i2c_driver tps6598x_i2c_driver = {
 743        .driver = {
 744                .name = "tps6598x",
 745                .of_match_table = tps6598x_of_match,
 746        },
 747        .probe_new = tps6598x_probe,
 748        .remove = tps6598x_remove,
 749        .id_table = tps6598x_id,
 750};
 751module_i2c_driver(tps6598x_i2c_driver);
 752
 753MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
 754MODULE_LICENSE("GPL v2");
 755MODULE_DESCRIPTION("TI TPS6598x USB Power Delivery Controller Driver");
 756