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